target/riscv: add RNMI support
v2 cover target/riscv: add RNMI support
From: Frank Chang <[email protected]>
This patchset add suport of Resumable NMI (RNMI) in RISC-V.
There are four new CSRs and one new instruction added to allow NMI to be resumable in RISC-V, which are:
=============================================================
* mnscratch (0x350)
* mnepc (0x351) * mncause (0x352) * mnstatus (0x353)
=============================================================
* mnret: To return from RNMI interrupt/exception handler.
=============================================================
RNMI also has higher priority than any other interrupts or exceptions and cannot be disable by software.
RNMI may be used to route to other devices such as Bus Error Unit or Watchdog Timer in the future.
The interrupt/exception trap handler addresses of RNMI are implementation defined.
The technical proposal of RNMI can be found:
https://lists.riscv.org/g/tech-privileged/message/421
The port is available here:
https://github.com/sifive/qemu/tree/nmi-upstream-v2
To test RNMI, we have created another QEMU branch to have RNMI feature enabled and also both SiFive Bus Error Unit and Error Device included on sifive_e machine.
Bus Error Unit (BEU) is routed to RNMI with mncause value set to 3. When any reads or writes to Error Device, it will drive BEU RNMI. A freedom-e-sdk RNMI example is also provided for testing. (We will also upstream BEU and Error Device in the near future.)
Two -cpu options are added for RNMI and BEU:
* rnmi=true to enable RNMI feature
* beu=true to enable BEU feature
Download and build freedom-e-sdk rnmi example:
1. git clone [email protected]:sifive/freedom-e-sdk.git
2. cd freedom-e-sdk
3. git checkout origin/dev/yihaoc/nmi -b nmi
4. git submodule init
5. git submodule update --recursive
6. Follow freedom-e-sdk guide to install freedom-e-sdk:
https://sifive.github.io/freedom-e-sdk-docs/index.html
7. make PROGRAM=example-rnmi TARGET=qemu-sifive-e31 \
CONFIGURATION=release software
Download, build and run freedom-e-sdk RNMI example on QEMU:
1. git clone [email protected]:sifive/qemu.git
2. cd qemu
3. git checkout origin/upstream-nmi-beu-error-device -b nmi-beu-error-device
4. git submodule init
5. git submodule update --recursive
6. ./configure --target-list=riscv32-softmmu
7. make -j
8. <path to qemu-system-riscv32> -nographic -M sifive_e \
-cpu rv32,rnmi=true,beu=true \
--bios none -kernel <path to example-rnmi.elf>
Output example:
RNMI Driver Example.
Cleared accrued bus error
Enter RNMI interrupt ISR.
mnscratch: 0x00000000
mnepc: 0x20401178
mncause: 0x00000003
mnstatus: 0x00001800
Try to trigger illegal instruction exception.
Enter RNMI exception ISR.
mscratch: 0x00000000
mepc: 0x20401208
mcause: 0x00000002
mstatus: 0x80007800
Return from RNMI exception ISR.
Handled TileLink bus error
Return from RNMI interrupt ISR.
Test passed!!
Changelog:
v2
* split up the series into more commits for convenience of review.
* add missing rnmi_irqvec and rnmi_excpvec properties to riscv_harts.
Frank Chang (4):
target/riscv: add RNMI cpu feature target/riscv: add RNMI CSRs
target/riscv: handle RNMI interrupt and exception target/riscv: add RNMI mnret instruction
hw/riscv/riscv_hart.c | 8 +++ include/hw/riscv/riscv_hart.h | 2 + target/riscv/cpu.c | 40 +++++++++++++ target/riscv/cpu.h | 16 ++++- target/riscv/cpu_bits.h | 19 ++++++ target/riscv/cpu_helper.c | 49 +++++++++++++-- target/riscv/csr.c | 59 +++++++++++++++++++ target/riscv/helper.h | 1 + target/riscv/insn32.decode | 3 + .../riscv/insn_trans/trans_privileged.c.inc | 13 ++++ target/riscv/op_helper.c | 31 ++++++++++ 11 files changed, 236 insertions(+), 5 deletions(-)
--
2.17.1
RFC v2 1/4 target/riscv: add RNMI cpu feature
From: Frank Chang <[email protected]>
--- hw/riscv/riscv_hart.c | 8 +++++++ include/hw/riscv/riscv_hart.h | 2 ++ target/riscv/cpu.c | 40 +++++++++++++++++++++++++++++++++++ target/riscv/cpu.h | 12 ++++++++++- target/riscv/cpu_bits.h | 6 ++++++ 5 files changed, 67 insertions(+), 1 deletion(-)
hw/riscv/riscv_hart.c +8 -0
@@ -33,6 +33,10 @@ static Property riscv_harts_props[] = {DEFINE_PROP_STRING ("cpu-type" , RISCVHartArrayState, cpu_type),DEFINE_PROP_UINT64 ("resetvec" , RISCVHartArrayState, resetvec,DEFAULT_RSTVEC ), +DEFINE_PROP_UINT64 ("rnmi_irqvec" , RISCVHartArrayState, rnmi_irqvec, +DEFAULT_RNMI_IRQVEC ), +DEFINE_PROP_UINT64 ("rnmi_excpvec" , RISCVHartArrayState, rnmi_excpvec, +DEFAULT_RNMI_EXCPVEC ), DEFINE_PROP_END_OF_LIST(), };
@@ -47,6 +51,10 @@ static bool riscv_hart_realize(RISCVHartArrayState *s, int idx, {object_initialize_child (OBJECT (s),"harts[*]" , &s->harts[idx], cpu_type);qdev_prop_set_uint64 (DEVICE (&s->harts[idx]),"resetvec" , s->resetvec); +qdev_prop_set_uint64 (DEVICE (&s->harts[idx]),"rnmi_irqvec" , + s->rnmi_irqvec); +qdev_prop_set_uint64 (DEVICE (&s->harts[idx]),"rnmi_excpvec" , + s->rnmi_excpvec); s->harts[idx].env.mhartid = s->hartid_base + idx;qemu_register_reset (riscv_harts_cpu_reset, &s->harts[idx]);return qdev_realize (DEVICE (&s->harts[idx]),NULL , errp);
include/hw/riscv/riscv_hart.h +2 -0
@@ -38,6 +38,8 @@ struct RISCVHartArrayState {uint32_t hartid_base;char *cpu_type;uint64_t resetvec; +uint64_t rnmi_irqvec; +uint64_t rnmi_excpvec;RISCVCPU *harts; };
target/riscv/cpu.c +40 -0
@@ -137,6 +137,14 @@ static void set_feature(CPURISCVState *env, int feature) env->features |= (1ULL << feature); } +static void set_rnmi_vectors (CPURISCVState *env,int irqvec,int excpvec) +{ +#ifndef CONFIG_USER_ONLY + env->rnmi_irqvec = irqvec; + env->rnmi_excpvec = excpvec; +#endif +} +static void set_resetvec (CPURISCVState *env,int resetvec) {#ifndef CONFIG_USER_ONLY
@@ -373,6 +381,23 @@ static void riscv_cpu_disas_set_info(CPUState *s, disassemble_info *info) } } +#ifndef CONFIG_USER_ONLY +static void riscv_cpu_set_rnmi (void *opaque,int irq,int level) +{ +RISCVCPU *cpu = opaque; +CPURISCVState *env = &cpu->env; +CPUState *cs =CPU (cpu); + +if (level) { + env->nmip |=1 << irq; +cpu_interrupt (cs,CPU_INTERRUPT_RNMI ); + }else { + env->nmip &= ~(1 << irq); +cpu_reset_interrupt (cs,CPU_INTERRUPT_RNMI ); + } +} +#endif +static void riscv_cpu_realize (DeviceState *dev,Error **errp) {CPUState *cs =CPU (dev);
@@ -416,6 +441,16 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)set_resetvec (env, cpu->cfg.resetvec); +if (cpu->cfg.rnmi) { +set_feature (env,RISCV_FEATURE_RNMI ); +set_rnmi_vectors (env, cpu->cfg.rnmi_irqvec, cpu->cfg.rnmi_excpvec); +#ifndef CONFIG_USER_ONLY + env->nmie = true; +qdev_init_gpio_in_named (DEVICE (cpu), riscv_cpu_set_rnmi, +"rnmi" ,TARGET_LONG_BITS ); +#endif + } +/* If only XLEN is set for misa, then set misa from properties */ if (env->misa ==RV32 || env->misa ==RV64 ) {/* Do some ISA extension error checking */
@@ -555,6 +590,11 @@ static Property riscv_cpu_properties[] = {DEFINE_PROP_BOOL ("mmu" ,RISCVCPU , cfg.mmu, true),DEFINE_PROP_BOOL ("pmp" ,RISCVCPU , cfg.pmp, true),DEFINE_PROP_UINT64 ("resetvec" ,RISCVCPU , cfg.resetvec,DEFAULT_RSTVEC ), +DEFINE_PROP_BOOL ("rnmi" ,RISCVCPU , cfg.rnmi, false), +DEFINE_PROP_UINT64 ("rnmi_irqvec" ,RISCVCPU , cfg.rnmi_irqvec, +DEFAULT_RNMI_IRQVEC ), +DEFINE_PROP_UINT64 ("rnmi_excpvec" ,RISCVCPU , cfg.rnmi_excpvec, +DEFAULT_RNMI_EXCPVEC ), DEFINE_PROP_END_OF_LIST(), };
target/riscv/cpu.h +11 -1
@@ -80,7 +80,8 @@enum {RISCV_FEATURE_MMU ,RISCV_FEATURE_PMP , -RISCV_FEATURE_MISA +RISCV_FEATURE_MISA , +RISCV_FEATURE_RNMI , };#define PRIV_VERSION_1_10_0 0x00011000
@@ -178,6 +179,12 @@ struct CPURISCVState {target_ulong mcause;target_ulong mtval;/* since: priv-1.10.0 */ +/* NMI */ +bool nmie; +target_ulong nmip; +target_ulong rnmi_irqvec; +target_ulong rnmi_excpvec; +/* Hypervisor CSRs */ target_ulong hstatus;target_ulong hedeleg;
@@ -304,6 +311,9 @@ struct RISCVCPU {bool mmu;bool pmp;uint64_t resetvec; +bool rnmi; +uint64_t rnmi_irqvec; +uint64_t rnmi_excpvec; } cfg; };
target/riscv/cpu_bits.h +6 -0
@@ -526,6 +526,12 @@/* Default Reset Vector adress */ #define DEFAULT_RSTVEC 0x1000 +/* Default RNMI Interrupt Vector address */ +#define DEFAULT_RNMI_IRQVEC 0x1000 + +/* Default RNMI Exception Vector address */ +#define DEFAULT_RNMI_EXCPVEC 0x1000 +/* Exception causes */ #define EXCP_NONE -1/* sentinel value */ #define RISCV_EXCP_INST_ADDR_MIS 0x0
Signature · 1 line
2.17.1
RFC v2 2/4 target/riscv: add RNMI CSRs
In reply to [email protected]From: Frank Chang <[email protected]>
--- target/riscv/cpu.h | 4 +++ target/riscv/cpu_bits.h | 9 +++++++ target/riscv/csr.c | 59 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+)
target/riscv/cpu.h +4 -0
@@ -180,6 +180,10 @@ struct CPURISCVState {target_ulong mtval;/* since: priv-1.10.0 */ /* NMI */ +target_ulong mnscratch; +target_ulong mnepc; +target_ulong mncause;/* mncause without bit XLEN-1 set to 1 */ +target_ulong mnstatus;bool nmie;target_ulong nmip;target_ulong rnmi_irqvec;
target/riscv/cpu_bits.h +9 -0
@@ -166,6 +166,12 @@#define CSR_MTVAL 0x343#define CSR_MIP 0x344 +/* NMI */ +#define CSR_MNSCRATCH 0x350 +#define CSR_MNEPC 0x351 +#define CSR_MNCAUSE 0x352 +#define CSR_MNSTATUS 0x353 +/* Legacy Machine Trap Handling (priv v1.9.1) */ #define CSR_MBADADDR 0x343
@@ -558,6 +564,9 @@#define RISCV_EXCP_INT_FLAG 0x80000000#define RISCV_EXCP_INT_MASK 0x7fffffff +/* RNMI mnstatus CSR mask */ +#define MNSTATUS_MPP MSTATUS_MPP +/* Interrupt causes */ #define IRQ_U_SOFT 0#define IRQ_S_SOFT 1
target/riscv/csr.c +59 -0
77 lines not on this page · raw message
Signature · 1 line
2.17.1
RFC v2 3/4 target/riscv: handle RNMI interrupt and exception
In reply to [email protected]From: Frank Chang <[email protected]>
--- target/riscv/cpu_bits.h | 4 ++++ target/riscv/cpu_helper.c | 49 +++++++++++++++++++++++++++++++++++---- 2 files changed, 49 insertions(+), 4 deletions(-)
target/riscv/cpu_bits.h +4 -0
@@ -607,4 +607,8 @@#define MIE_UTIE (1 << IRQ_U_TIMER)#define MIE_SSIE (1 << IRQ_S_SOFT)#define MIE_USIE (1 << IRQ_U_SOFT) + +/* RISC-V-specific interrupt pending bits. */ +#define CPU_INTERRUPT_RNMI CPU_INTERRUPT_TGT_EXT_0 +#endif
target/riscv/cpu_helper.c +45 -4
85 lines not on this page · raw message
Signature · 1 line
2.17.1
RFC v2 4/4 target/riscv: add RNMI mnret instruction
In reply to [email protected]From: Frank Chang <[email protected]>
--- target/riscv/helper.h | 1 + target/riscv/insn32.decode | 3 ++ .../riscv/insn_trans/trans_privileged.c.inc | 13 ++++++++ target/riscv/op_helper.c | 31 +++++++++++++++++++ 4 files changed, 48 insertions(+)
target/riscv/helper.h +1 -0
@@ -65,6 +65,7 @@ DEF_HELPER_4(csrrc, tl, env, tl, tl, tl)#ifndef CONFIG_USER_ONLY DEF_HELPER_2(sret, tl, env, tl) DEF_HELPER_2(mret, tl, env, tl) +DEF_HELPER_2(mnret, tl, env, tl) DEF_HELPER_1(wfi, void, env) DEF_HELPER_1(tlb_flush, void, env)#endif
target/riscv/insn32.decode +3 -0
@@ -97,6 +97,9 @@ wfi 0001000 00101 00000 000 00000 1110011 sfence_vma 0001001 ..... ..... 000 00000 1110011 @sfence_vma sfence_vm 0001000 00100 ..... 000 00000 1110011 @sfence_vm +# *** NMI *** +mnret 0111000 00010 00000 000 00000 1110011 + # *** RV32I Base Instruction Set *** lui .................... ..... 0110111 @u auipc .................... ..... 0010111 @u
target/riscv/insn_trans/trans_privileged.c.inc +13 -0
@@ -106,6 +106,19 @@ static bool trans_mret(DisasContext *ctx, arg_mret *a) #endif } +static bool trans_mnret(DisasContext *ctx, arg_mnret *a) +{ +#ifndef CONFIG_USER_ONLY + tcg_gen_movi_tl(cpu_pc, ctx->base.pc_next); + gen_helper_mnret(cpu_pc, cpu_env, cpu_pc); + exit_tb(ctx); /* no chaining */ + ctx->base.is_jmp = DISAS_NORETURN; + return true; +#else + return false; +#endif +} + static bool trans_wfi(DisasContext *ctx, arg_wfi *a) { #ifndef CONFIG_USER_ONLY
target/riscv/op_helper.c +31 -0
@@ -175,6 +175,37 @@ target_ulong helper_mret(CPURISCVState *env, target_ulong cpu_pc_deb)return retpc; } +target_ulong helper_mnret (CPURISCVState *env,target_ulong cpu_pc_deb) +{ +if (!riscv_feature (env,RISCV_FEATURE_RNMI )) { +/* RNMI feature is not presented. */ +riscv_raise_exception (env,RISCV_EXCP_ILLEGAL_INST ,GETPC ()); + } + +if (!(env->priv >=PRV_M )) { +riscv_raise_exception (env,RISCV_EXCP_ILLEGAL_INST ,GETPC ()); + } + +/* Get return PC from mnepc CSR. */ +target_ulong retpc = env->mnepc; +if (!riscv_has_ext (env,RVC ) && (retpc &0x3 )) { +riscv_raise_exception (env,RISCV_EXCP_INST_ADDR_MIS ,GETPC ()); + } + +/* Get previous privilege level from mnstatus CSR. */ +target_ulong prev_priv =get_field (env->mnstatus,MNSTATUS_MPP ); + +if (!pmp_get_num_rules (env) && (prev_priv !=PRV_M )) { +riscv_raise_exception (env,RISCV_EXCP_ILLEGAL_INST ,GETPC ()); + } + +riscv_cpu_set_mode (env, prev_priv); + + env->nmie = true; + +return retpc; +} +void helper_wfi (CPURISCVState *env) {CPUState *cs =env_cpu (env);
Signature · 1 line
2.17.1
<[email protected]> 於 2021年4月1日 週四 下午5:27寫道:
Quoted 46 lines
From: Frank Chang <[email protected]>
This patchset add suport of Resumable NMI (RNMI) in RISC-V.
There are four new CSRs and one new instruction added to allow NMI to be resumable in RISC-V, which are:
=============================================================
* mnscratch (0x350)* mnepc (0x351) * mncause (0x352) * mnstatus (0x353)=============================================================
* mnret: To return from RNMI interrupt/exception handler.
=============================================================RNMI also has higher priority than any other interrupts or exceptions and cannot be disable by software.
RNMI may be used to route to other devices such as Bus Error Unit or Watchdog Timer in the future.
The interrupt/exception trap handler addresses of RNMI are implementation defined.
The technical proposal of RNMI can be found:
https://lists.riscv.org/g/tech-privileged/message/421The port is available here:
https://github.com/sifive/qemu/tree/nmi-upstream-v2To test RNMI, we have created another QEMU branch to have RNMI feature enabled and also both SiFive Bus Error Unit and Error Device included on
sifive_emachine.Bus Error Unit (BEU) is routed to RNMI with mncause value set to 3. When any reads or writes to Error Device, it will drive BEU RNMI. A freedom-e-sdk RNMI example is also provided for testing. (We will also upstream BEU and Error Device in the near future.)
Two -cpu options are added for RNMI and BEU:
* rnmi=true to enable RNMI feature
* beu=true to enable BEU featureDownload and build freedom-e-sdk rnmi example:
Quoted 10 lines
1. git clone [email protected]:sifive/freedom-e-sdk.git
2. cd freedom-e-sdk
3. git checkout origin/dev/yihaoc/nmi -b nmi
4. git submodule init
5. git submodule update --recursive
6. Follow freedom-e-sdk guide to install freedom-e-sdk:
https://sifive.github.io/freedom-e-sdk-docs/index.html
7. make PROGRAM=example-rnmi TARGET=qemu-sifive-e31 \
CONFIGURATION=release software
freedom-e-sdk RNMI example code is available at:
https://github.com/sifive/freedom-e-sdk/tree/dev/yihaoc/nmi/software/example-rnmi
Quoted 65 lines
Download, build and run freedom-e-sdk RNMI example on QEMU:
1. git clone [email protected]:sifive/qemu.git
2. cd qemu
3. git checkout origin/upstream-nmi-beu-error-device -b nmi-beu-error-device
4. git submodule init
5. git submodule update --recursive
6. ./configure --target-list=riscv32-softmmu
7. make -j
8. <path to qemu-system-riscv32> -nographic -Msifive_e\
-cpu rv32,rnmi=true,beu=true \
--bios none -kernel <path to example-rnmi.elf>Output example:
RNMI Driver Example.
Cleared accrued bus error
Enter RNMI interrupt ISR.
mnscratch:0x00000000
mnepc:0x20401178
mncause:0x00000003
mnstatus:0x00001800
Try to trigger illegal instruction exception.
Enter RNMI exception ISR.
mscratch:0x00000000
mepc:0x20401208
mcause:0x00000002
mstatus:0x80007800
Return from RNMI exception ISR.
Handled TileLink bus error
Return from RNMI interrupt ISR.
Test passed!!Changelog:
v2
* split up the series into more commits for convenience of review.
* add missingrnmi_irqvecandrnmi_excpvecproperties toriscv_harts.Frank Chang (4):
target/riscv: add RNMI cpu feature target/riscv: add RNMI CSRs
target/riscv: handle RNMI interrupt and exception target/riscv: add RNMI mnret instructionhw/riscv/riscv_hart.c | 8 +++ include/hw/riscv/riscv_hart.h | 2 + target/riscv/cpu.c | 40 +++++++++++++ target/riscv/cpu.h | 16 ++++- target/riscv/cpu_bits.h | 19 ++++++ target/riscv/cpu_helper.c | 49 +++++++++++++-- target/riscv/csr.c | 59 +++++++++++++++++++ target/riscv/helper.h | 1 + target/riscv/insn32.decode | 3 + .../riscv/insn_trans/trans_privileged.c.inc | 13 ++++ target/riscv/op_helper.c | 31 ++++++++++ 11 files changed, 236 insertions(+), 5 deletions(-)--
2.17.1