RISC-V Pointer Masking implementation
v16 cover RISC-V Pointer Masking implementation
v15:
Renamed pm into pointer_masking in machine state.
v14:
Addressed Richard's comments from previous series.
v13:
Rebased QEMU and addressed Richard's comment.
v12:
Updated function for adjusting address with pointer masking to allocate and use temp register.
v11:
Addressed a few style issues Alistair mentioned in the previous review.
If this patch series would be accepted, I think my further attention would be to:
- Support pm for memory operations for RVV
- Add proper csr and support pm for memory operations for Hypervisor mode
- Support address wrapping on unaligned accesses as @Richard mentioned previously
Thanks!
Alexey Baturo (7):
[RISCV_PM] Add J-extension into RISC-V
[RISCV_PM] Add CSR defines for RISC-V PM extension [RISCV_PM] Support CSRs required for RISC-V PM extension except for
the h-mode
[RISCV_PM] Add J extension state description
[RISCV_PM] Print new PM CSRs in QEMU logs
[RISCV_PM] Support pointer masking for RISC-V for i/c/f/d/a types of
instructions
[RISCV_PM] Allow experimental J-ext to be turned on
Anatoly Parshintsev (1):
[RISCV_PM] Implement address masking functions required for RISC-V
Pointer Masking extension
target/riscv/cpu.c | 13 ++ target/riscv/cpu.h | 15 ++ target/riscv/cpu_bits.h | 96 ++++++++ target/riscv/cpu_helper.c | 18 ++ target/riscv/csr.c | 285 ++++++++++++++++++++++++ target/riscv/insn_trans/trans_rva.c.inc | 3 + target/riscv/insn_trans/trans_rvd.c.inc | 2 + target/riscv/insn_trans/trans_rvf.c.inc | 2 + target/riscv/insn_trans/trans_rvi.c.inc | 2 + target/riscv/machine.c | 27 +++ target/riscv/translate.c | 43 ++++ 11 files changed, 506 insertions(+)
Signature · 1 line
2.30.2
PATCH v16 1/8 Add J-extension into RISC-V
--- target/riscv/cpu.h | 2 ++ 1 file changed, 2 insertions(+)
target/riscv/cpu.h +2 -0
@@ -65,6 +65,7 @@#define RVS RV('S')#define RVU RV('U')#define RVH RV('H') +#define RVJ RV('J')/* S extension denotes that Supervisor mode exists, however it is possible to have a core that support S mode but does not have an MMU and there
@@ -291,6 +292,7 @@ struct RISCVCPU {bool ext_s;bool ext_u;bool ext_h; +bool ext_j;bool ext_v;bool ext_zba;bool ext_zbb;
Signature · 1 line
2.30.2
PATCH v16 2/8 Add CSR defines for RISC-V PM extension
In reply to Alexey Baturo--- target/riscv/cpu_bits.h | 96 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+)
target/riscv/cpu_bits.h +96 -0
@@ -334,6 +334,38 @@#define CSR_MHPMCOUNTER30H 0xb9e#define CSR_MHPMCOUNTER31H 0xb9f +/* +* User PointerMasking registers +* NB: actual CSR numbers might be changed in future +*/ +#define CSR_UMTE 0x4c0 +#define CSR_UPMMASK 0x4c1 +#define CSR_UPMBASE 0x4c2 + +/* +* Machine PointerMasking registers +* NB: actual CSR numbers might be changed in future +*/ +#define CSR_MMTE 0x3c0 +#define CSR_MPMMASK 0x3c1 +#define CSR_MPMBASE 0x3c2 + +/* +* Supervisor PointerMaster registers +* NB: actual CSR numbers might be changed in future +*/ +#define CSR_SMTE 0x1c0 +#define CSR_SPMMASK 0x1c1 +#define CSR_SPMBASE 0x1c2 + +/* +* Hypervisor PointerMaster registers +* NB: actual CSR numbers might be changed in future +*/ +#define CSR_VSMTE 0x2c0 +#define CSR_VSPMMASK 0x2c1 +#define CSR_VSPMBASE 0x2c2 +/* mstatus CSR bits */ #define MSTATUS_UIE 0x00000001#define MSTATUS_SIE 0x00000002
@@ -525,4 +557,68 @@ typedef enum RISCVException {#define MIE_UTIE (1 << IRQ_U_TIMER)#define MIE_SSIE (1 << IRQ_S_SOFT)#define MIE_USIE (1 << IRQ_U_SOFT) + +/* General PointerMasking CSR bits*/ +#define PM_ENABLE 0x00000001ULL +#define PM_CURRENT 0x00000002ULL +#define PM_INSN 0x00000004ULL +#define PM_XS_MASK 0x00000003ULL + +/* PointerMasking XS bits values */ +#define PM_EXT_DISABLE 0x00000000ULL +#define PM_EXT_INITIAL 0x00000001ULL +#define PM_EXT_CLEAN 0x00000002ULL +#define PM_EXT_DIRTY 0x00000003ULL + +/* Offsets for every pair of control bits per each priv level */ +#define XS_OFFSET 0ULL +#define U_OFFSET 2ULL +#define S_OFFSET 5ULL +#define M_OFFSET 8ULL + +#define PM_XS_BITS (PM_XS_MASK << XS_OFFSET) +#define U_PM_ENABLE (PM_ENABLE << U_OFFSET) +#define U_PM_CURRENT (PM_CURRENT << U_OFFSET) +#define U_PM_INSN (PM_INSN << U_OFFSET) +#define S_PM_ENABLE (PM_ENABLE << S_OFFSET) +#define S_PM_CURRENT (PM_CURRENT << S_OFFSET) +#define S_PM_INSN (PM_INSN << S_OFFSET) +#define M_PM_ENABLE (PM_ENABLE << M_OFFSET) +#define M_PM_CURRENT (PM_CURRENT << M_OFFSET) +#define M_PM_INSN (PM_INSN << M_OFFSET) + +/* mmte CSR bits */ +#define MMTE_PM_XS_BITS PM_XS_BITS +#define MMTE_U_PM_ENABLE U_PM_ENABLE +#define MMTE_U_PM_CURRENT U_PM_CURRENT +#define MMTE_U_PM_INSN U_PM_INSN +#define MMTE_S_PM_ENABLE S_PM_ENABLE +#define MMTE_S_PM_CURRENT S_PM_CURRENT +#define MMTE_S_PM_INSN S_PM_INSN +#define MMTE_M_PM_ENABLE M_PM_ENABLE +#define MMTE_M_PM_CURRENT M_PM_CURRENT +#define MMTE_M_PM_INSN M_PM_INSN +#define MMTE_MASK (MMTE_U_PM_ENABLE | MMTE_U_PM_CURRENT | MMTE_U_PM_INSN | \ + MMTE_S_PM_ENABLE | MMTE_S_PM_CURRENT | MMTE_S_PM_INSN | \ + MMTE_M_PM_ENABLE | MMTE_M_PM_CURRENT | MMTE_M_PM_INSN | \ + MMTE_PM_XS_BITS) + +/* (v)smte CSR bits */ +#define SMTE_PM_XS_BITS PM_XS_BITS +#define SMTE_U_PM_ENABLE U_PM_ENABLE +#define SMTE_U_PM_CURRENT U_PM_CURRENT +#define SMTE_U_PM_INSN U_PM_INSN +#define SMTE_S_PM_ENABLE S_PM_ENABLE +#define SMTE_S_PM_CURRENT S_PM_CURRENT +#define SMTE_S_PM_INSN S_PM_INSN +#define SMTE_MASK (SMTE_U_PM_ENABLE | SMTE_U_PM_CURRENT | SMTE_U_PM_INSN | \ + SMTE_S_PM_ENABLE | SMTE_S_PM_CURRENT | SMTE_S_PM_INSN | \ + SMTE_PM_XS_BITS) + +/* umte CSR bits */ +#define UMTE_U_PM_ENABLE U_PM_ENABLE +#define UMTE_U_PM_CURRENT U_PM_CURRENT +#define UMTE_U_PM_INSN U_PM_INSN +#define UMTE_MASK (UMTE_U_PM_ENABLE | MMTE_U_PM_CURRENT | UMTE_U_PM_INSN) +#endif
Signature · 1 line
2.30.2
PATCH v16 3/8 Support CSRs required for RISC-V PM extension except for the h-mode
In reply to Alexey Baturo--- target/riscv/cpu.c | 2 + target/riscv/cpu.h | 11 ++ target/riscv/csr.c | 285 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 298 insertions(+)
target/riscv/cpu.c +2 -0
@@ -367,6 +367,8 @@ static void riscv_cpu_reset(DeviceState *dev) env->mcause =0 ; env->pc = env->resetvec; env->two_stage_lookup = false; +/* mmte is supposed to have pm.current hardwired to 1 */ + env->mmte |= (PM_EXT_INITIAL |MMTE_M_PM_CURRENT );#endif cs->exception_index =RISCV_EXCP_NONE ; env->load_res =-1 ;
target/riscv/cpu.h +11 -0
@@ -238,6 +238,17 @@ struct CPURISCVState {/* True if in debugger mode. */ bool debugger; + +/* +* CSRs for PointerMasking extension +*/ +target_ulong mmte; +target_ulong mpmmask; +target_ulong mpmbase; +target_ulong spmmask; +target_ulong spmbase; +target_ulong upmmask; +target_ulong upmbase;#endif float_status fp_status;
target/riscv/csr.c +285 -0
303 lines not on this page · raw message
Signature · 1 line
2.30.2
Re: [PATCH v16 3/8] [RISCV_PM] Support CSRs required for RISC-V PM extension except for the h-mode
Quoted 110 lines · Alexey Baturo
Signed-off-by Alexey Baturo <[email protected]>Reviewed-by Alistair Francis <[email protected]>--- target/riscv/cpu.c | 2 + target/riscv/cpu.h | 11 ++ target/riscv/csr.c | 285 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 298 insertions(+)target/riscv/cpu.c +2
-0@@ -367,6 +367,8 @@ static void riscv_cpu_reset(DeviceState *dev) env->mcause =0 ; env->pc = env->resetvec; env->two_stage_lookup = false; +/* mmte is supposed to have pm.current hardwired to 1 */ + env->mmte |= (PM_EXT_INITIAL |MMTE_M_PM_CURRENT );#endif cs->exception_index =RISCV_EXCP_NONE ; env->load_res =-1 ;target/riscv/cpu.h +11
-0@@ -238,6 +238,17 @@ struct CPURISCVState {/* True if in debugger mode. */ bool debugger; + +/* +* CSRs for PointerMasking extension +*/ +target_ulong mmte; +target_ulong mpmmask; +target_ulong mpmbase; +target_ulong spmmask; +target_ulong spmbase; +target_ulong upmmask; +target_ulong upmbase;#endif float_status fp_status;target/riscv/csr.c +51
-0@@ -192,6 +192,16 @@ static RISCVException hmode32(CPURISCVState *env, int csrno) } +/* Checks if PointerMasking registers could be accessed */ +static RISCVException pointer_masking (CPURISCVState *env,int csrno) +{ +/* Check if j-ext is present */ +if (riscv_has_ext (env,RVJ )) { +return RISCV_EXCP_NONE ; + } +return RISCV_EXCP_ILLEGAL_INST ; +} +static RISCVException pmp (CPURISCVState *env,int csrno) {if (riscv_feature (env,RISCV_FEATURE_PMP )) {@@ -1425,6 +1435,268 @@ static RISCVException write_pmpaddr(CPURISCVState *env, int csrno,return RISCV_EXCP_NONE ; } +/* +* Functions to access Pointer Masking feature registers +* We have to check if current priv lvl could modify +* csr in given mode +*/ +static bool check_pm_current_disabled (CPURISCVState *env,int csrno) +{ +int csr_priv =get_field (csrno,0x300 ); +int pm_current; + +/* +* If priv lvls differ that means we're accessing csr from higher priv lvl, +* so allow the access +*/ +if (env->priv != csr_priv) { +return false; + } +switch (env->priv) { +case PRV_M : + pm_current =get_field (env->mmte,M_PM_CURRENT ); +break ; +case PRV_S : + pm_current =get_field (env->mmte,S_PM_CURRENT ); +break ; +case PRV_U : + pm_current =get_field (env->mmte,U_PM_CURRENT ); +break ; +default : +g_assert_not_reached (); + } +/* It's same priv lvl, so we allow to modify csr only if pm.current==1 */ +return !pm_current; +} + +static RISCVException read_mmte (CPURISCVState *env,int csrno, +target_ulong *val) +{ + *val = env->mmte &MMTE_MASK ; +return RISCV_EXCP_NONE ; +} +
target/riscv/csr.c static RISCVException write_pmpaddr(CPURISCVState *env, int csrno, · line 1488
Quoted 10 lines
target/riscv/csr.c +10
-0+static RISCVException write_mmte (CPURISCVState *env,int csrno, +target_ulong val) +{ +uint64_t mstatus; +target_ulong wpri_val = val &MMTE_MASK ; + +if (val != wpri_val) { +qemu_log_mask (LOG_GUEST_ERROR , +"MMTE: WPRI violation written 0x%lx vs expected 0x%lx\n" , + val, wpri_val);
This and other log messages fail to build for both RV64 and RV32.
The target_ulong can either be 32-bit or 64-bit, so printing with %lx doesn't work. You can use TARGET_FMT_lx instead
Also, while re-sending this can you prefix the commit messages with "target/riscv: "
Alistair
Quoted 238 lines
target/riscv/csr.c +224
-1+ } +/* for machine mode pm.current is hardwired to 1 */ + wpri_val |=MMTE_M_PM_CURRENT ; + +/* hardwiring pm.instruction bit to 0, since it's not supported yet */ + wpri_val &= ~(MMTE_M_PM_INSN |MMTE_S_PM_INSN |MMTE_U_PM_INSN ); + env->mmte = wpri_val |PM_EXT_DIRTY ; + +/* Set XS and SD bits, since PM CSRs are dirty */ + mstatus = env->mstatus |MSTATUS_XS ; +write_mstatus (env, csrno, mstatus); +return RISCV_EXCP_NONE ; +} + +static RISCVException read_smte (CPURISCVState *env,int csrno, +target_ulong *val) +{ + *val = env->mmte &SMTE_MASK ; +return RISCV_EXCP_NONE ; +} + +static RISCVException write_smte (CPURISCVState *env,int csrno, +target_ulong val) +{ +target_ulong wpri_val = val &SMTE_MASK ; + +if (val != wpri_val) { +qemu_log_mask (LOG_GUEST_ERROR , +"SMTE: WPRI violation written 0x%lx vs expected 0x%lx\n" , + val, wpri_val); + } + +/* if pm.current==0 we can't modify current PM CSRs */ +if (check_pm_current_disabled (env, csrno)) { +return RISCV_EXCP_NONE ; + } + + wpri_val |= (env->mmte & ~SMTE_MASK ); +write_mmte (env, csrno, wpri_val); +return RISCV_EXCP_NONE ; +} + +static RISCVException read_umte (CPURISCVState *env,int csrno, +target_ulong *val) +{ + *val = env->mmte &UMTE_MASK ; +return RISCV_EXCP_NONE ; +} + +static RISCVException write_umte (CPURISCVState *env,int csrno, +target_ulong val) +{ +target_ulong wpri_val = val &UMTE_MASK ; + +if (val != wpri_val) { +qemu_log_mask (LOG_GUEST_ERROR , +"UMTE: WPRI violation written 0x%lx vs expected 0x%lx\n" , + val, wpri_val); + } + +if (check_pm_current_disabled (env, csrno)) { +return RISCV_EXCP_NONE ; + } + + wpri_val |= (env->mmte & ~UMTE_MASK ); +write_mmte (env, csrno, wpri_val); +return RISCV_EXCP_NONE ; +} + +static RISCVException read_mpmmask (CPURISCVState *env,int csrno, +target_ulong *val) +{ + *val = env->mpmmask; +return RISCV_EXCP_NONE ; +} + +static RISCVException write_mpmmask (CPURISCVState *env,int csrno, +target_ulong val) +{ +uint64_t mstatus; + + env->mpmmask = val; + env->mmte |=PM_EXT_DIRTY ; + +/* Set XS and SD bits, since PM CSRs are dirty */ + mstatus = env->mstatus |MSTATUS_XS ; +write_mstatus (env, csrno, mstatus); +return RISCV_EXCP_NONE ; +} + +static RISCVException read_spmmask (CPURISCVState *env,int csrno, +target_ulong *val) +{ + *val = env->spmmask; +return RISCV_EXCP_NONE ; +} + +static RISCVException write_spmmask (CPURISCVState *env,int csrno, +target_ulong val) +{ +uint64_t mstatus; + +/* if pm.current==0 we can't modify current PM CSRs */ +if (check_pm_current_disabled (env, csrno)) { +return RISCV_EXCP_NONE ; + } + env->spmmask = val; + env->mmte |=PM_EXT_DIRTY ; + +/* Set XS and SD bits, since PM CSRs are dirty */ + mstatus = env->mstatus |MSTATUS_XS ; +write_mstatus (env, csrno, mstatus); +return RISCV_EXCP_NONE ; +} + +static RISCVException read_upmmask (CPURISCVState *env,int csrno, +target_ulong *val) +{ + *val = env->upmmask; +return RISCV_EXCP_NONE ; +} + +static RISCVException write_upmmask (CPURISCVState *env,int csrno, +target_ulong val) +{ +uint64_t mstatus; + +/* if pm.current==0 we can't modify current PM CSRs */ +if (check_pm_current_disabled (env, csrno)) { +return RISCV_EXCP_NONE ; + } + env->upmmask = val; + env->mmte |=PM_EXT_DIRTY ; + +/* Set XS and SD bits, since PM CSRs are dirty */ + mstatus = env->mstatus |MSTATUS_XS ; +write_mstatus (env, csrno, mstatus); +return RISCV_EXCP_NONE ; +} + +static RISCVException read_mpmbase (CPURISCVState *env,int csrno, +target_ulong *val) +{ + *val = env->mpmbase; +return RISCV_EXCP_NONE ; +} + +static RISCVException write_mpmbase (CPURISCVState *env,int csrno, +target_ulong val) +{ +uint64_t mstatus; + + env->mpmbase = val; + env->mmte |=PM_EXT_DIRTY ; + +/* Set XS and SD bits, since PM CSRs are dirty */ + mstatus = env->mstatus |MSTATUS_XS ; +write_mstatus (env, csrno, mstatus); +return RISCV_EXCP_NONE ; +} + +static RISCVException read_spmbase (CPURISCVState *env,int csrno, +target_ulong *val) +{ + *val = env->spmbase; +return RISCV_EXCP_NONE ; +} + +static RISCVException write_spmbase (CPURISCVState *env,int csrno, +target_ulong val) +{ +uint64_t mstatus; + +/* if pm.current==0 we can't modify current PM CSRs */ +if (check_pm_current_disabled (env, csrno)) { +return RISCV_EXCP_NONE ; + } + env->spmbase = val; + env->mmte |=PM_EXT_DIRTY ; + +/* Set XS and SD bits, since PM CSRs are dirty */ + mstatus = env->mstatus |MSTATUS_XS ; +write_mstatus (env, csrno, mstatus); +return RISCV_EXCP_NONE ; +} + +static RISCVException read_upmbase (CPURISCVState *env,int csrno, +target_ulong *val) +{ + *val = env->upmbase; +return RISCV_EXCP_NONE ; +} + +static RISCVException write_upmbase (CPURISCVState *env,int csrno, +target_ulong val) +{ +uint64_t mstatus; + +/* if pm.current==0 we can't modify current PM CSRs */ +if (check_pm_current_disabled (env, csrno)) { +return RISCV_EXCP_NONE ; + } + env->upmbase = val; + env->mmte |=PM_EXT_DIRTY ; + +/* Set XS and SD bits, since PM CSRs are dirty */ + mstatus = env->mstatus |MSTATUS_XS ; +write_mstatus (env, csrno, mstatus); +return RISCV_EXCP_NONE ; +} +#endif /* @@ -1659,6 +1931,19 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_PMPADDR14] = { "pmpaddr14", pmp, read_pmpaddr, write_pmpaddr }, [CSR_PMPADDR15] = { "pmpaddr15", pmp, read_pmpaddr, write_pmpaddr }, + /* User Pointer Masking */ + [CSR_UMTE] = { "umte", pointer_masking, read_umte, write_umte }, + [CSR_UPMMASK] = { "upmmask", pointer_masking, read_upmmask, write_upmmask }, + [CSR_UPMBASE] = { "upmbase", pointer_masking, read_upmbase, write_upmbase }, + /* Machine Pointer Masking */ + [CSR_MMTE] = { "mmte", pointer_masking, read_mmte, write_mmte }, + [CSR_MPMMASK] = { "mpmmask", pointer_masking, read_mpmmask, write_mpmmask }, + [CSR_MPMBASE] = { "mpmbase", pointer_masking, read_mpmbase, write_mpmbase }, + /* Supervisor Pointer Masking */ + [CSR_SMTE] = { "smte", pointer_masking, read_smte, write_smte }, + [CSR_SPMMASK] = { "spmmask", pointer_masking, read_spmmask, write_spmmask }, + [CSR_SPMBASE] = { "spmbase", pointer_masking, read_spmbase, write_spmbase }, + /* Performance Counters */ [CSR_HPMCOUNTER3] = { "hpmcounter3", ctr, read_zero }, [CSR_HPMCOUNTER4] = { "hpmcounter4", ctr, read_zero }, --2.30.2
PATCH v16 4/8 Add J extension state description
In reply to Alexey Baturo--- target/riscv/machine.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)
target/riscv/machine.c +27 -0
@@ -84,6 +84,14 @@ static bool vector_needed(void *opaque)return riscv_has_ext (env,RVV ); } +static bool pointermasking_needed (void *opaque) +{ +RISCVCPU *cpu = opaque; +CPURISCVState *env = &cpu->env; + +return riscv_has_ext (env,RVJ ); +} +static const VMStateDescription vmstate_vector = { .name ="cpu/vector" , .version_id =1 ,
@@ -100,6 +108,24 @@ static const VMStateDescription vmstate_vector = { } }; +static const VMStateDescription vmstate_pointermasking = { + .name ="cpu/pointer_masking" , + .version_id =1 , + .minimum_version_id =1 , + .needed = pointermasking_needed, + .fields = (VMStateField []) { +VMSTATE_UINTTL (env.mmte,RISCVCPU ), +VMSTATE_UINTTL (env.mpmmask,RISCVCPU ), +VMSTATE_UINTTL (env.mpmbase,RISCVCPU ), +VMSTATE_UINTTL (env.spmmask,RISCVCPU ), +VMSTATE_UINTTL (env.spmbase,RISCVCPU ), +VMSTATE_UINTTL (env.upmmask,RISCVCPU ), +VMSTATE_UINTTL (env.upmbase,RISCVCPU ), + +VMSTATE_END_OF_LIST () + } +}; +static const VMStateDescription vmstate_hyper = { .name ="cpu/hyper" , .version_id =1 ,
@@ -191,6 +217,7 @@ const VMStateDescription vmstate_riscv_cpu = { &vmstate_pmp, &vmstate_hyper, &vmstate_vector, + &vmstate_pointermasking, NULL } };
Signature · 1 line
2.30.2
PATCH v16 5/8 Print new PM CSRs in QEMU logs
In reply to Alexey Baturo--- target/riscv/cpu.c | 7 +++++++ 1 file changed, 7 insertions(+)
target/riscv/cpu.c +7 -0
@@ -271,6 +271,13 @@ static void riscv_cpu_dump_state(CPUState *cs, FILE *f, int flags)CSR_MSCRATCH ,CSR_SSCRATCH ,CSR_SATP , +CSR_MMTE , +CSR_UPMBASE , +CSR_UPMMASK , +CSR_SPMBASE , +CSR_SPMMASK , +CSR_MPMBASE , + CSR_MPMMASK, };for (int i =0 ; i <ARRAY_SIZE (dump_csrs); ++i) {
Signature · 1 line
2.30.2
Re: [PATCH v16 5/8] [RISCV_PM] Print new PM CSRs in QEMU logs
Quoted 2 lines · Alexey Baturo
Signed-off-by Alexey Baturo <[email protected]>
Alistair
Quoted 26 lines
--- target/riscv/cpu.c | 7 +++++++ 1 file changed, 7 insertions(+)target/riscv/cpu.c +7
-1@@ -271,6 +271,13 @@ static void riscv_cpu_dump_state(CPUState *cs, FILE *f, int flags)CSR_MSCRATCH ,CSR_SSCRATCH ,CSR_SATP , +CSR_MMTE , +CSR_UPMBASE , +CSR_UPMMASK , +CSR_SPMBASE , +CSR_SPMMASK , +CSR_MPMBASE , + CSR_MPMMASK, };for (int i =0 ; i <ARRAY_SIZE (dump_csrs); ++i) { --2.30.2
PATCH v16 6/8 Support pointer masking for RISC-V for i/c/f/d/a types of instructions
In reply to Alexey Baturo--- target/riscv/insn_trans/trans_rva.c.inc | 3 +++ target/riscv/insn_trans/trans_rvd.c.inc | 2 ++ target/riscv/insn_trans/trans_rvf.c.inc | 2 ++ target/riscv/insn_trans/trans_rvi.c.inc | 2 ++ target/riscv/translate.c | 8 ++++++++ 5 files changed, 17 insertions(+)
target/riscv/insn_trans/trans_rva.c.inc +3 -0
@@ -25,6 +25,7 @@ static bool gen_lr(DisasContext *ctx, arg_atomic *a, MemOp mop) if (a->rl) { tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL); } + src1 = gen_pm_adjust_address(ctx, src1); tcg_gen_qemu_ld_tl(load_val, src1, ctx->mem_idx, mop); if (a->aq) { tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
@@ -44,6 +45,7 @@ static bool gen_sc(DisasContext *ctx, arg_atomic *a, MemOp mop) TCGLabel *l2 = gen_new_label(); src1 = get_gpr(ctx, a->rs1, EXT_ZERO); + src1 = gen_pm_adjust_address(ctx, src1); tcg_gen_brcond_tl(TCG_COND_NE, load_res, src1, l1); /*
@@ -84,6 +86,7 @@ static bool gen_amo(DisasContext *ctx, arg_atomic *a, TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE); + src1 = gen_pm_adjust_address(ctx, src1); func(dest, src1, src2, ctx->mem_idx, mop); gen_set_gpr(ctx, a->rd, dest);
target/riscv/insn_trans/trans_rvd.c.inc +2 -0
@@ -31,6 +31,7 @@ static bool trans_fld(DisasContext *ctx, arg_fld *a) tcg_gen_addi_tl(temp, addr, a->imm); addr = temp; } + addr = gen_pm_adjust_address(ctx, addr); tcg_gen_qemu_ld_i64(cpu_fpr[a->rd], addr, ctx->mem_idx, MO_TEQ);
@@ -51,6 +52,7 @@ static bool trans_fsd(DisasContext *ctx, arg_fsd *a) tcg_gen_addi_tl(temp, addr, a->imm); addr = temp; } + addr = gen_pm_adjust_address(ctx, addr); tcg_gen_qemu_st_i64(cpu_fpr[a->rs2], addr, ctx->mem_idx, MO_TEQ);
target/riscv/insn_trans/trans_rvf.c.inc +2 -0
@@ -37,6 +37,7 @@ static bool trans_flw(DisasContext *ctx, arg_flw *a) tcg_gen_addi_tl(temp, addr, a->imm); addr = temp; } + addr = gen_pm_adjust_address(ctx, addr); dest = cpu_fpr[a->rd]; tcg_gen_qemu_ld_i64(dest, addr, ctx->mem_idx, MO_TEUL);
@@ -59,6 +60,7 @@ static bool trans_fsw(DisasContext *ctx, arg_fsw *a) tcg_gen_addi_tl(temp, addr, a->imm); addr = temp; } + addr = gen_pm_adjust_address(ctx, addr); tcg_gen_qemu_st_i64(cpu_fpr[a->rs2], addr, ctx->mem_idx, MO_TEUL);
target/riscv/insn_trans/trans_rvi.c.inc +2 -0
@@ -144,6 +144,7 @@ static bool gen_load(DisasContext *ctx, arg_lb *a, MemOp memop) tcg_gen_addi_tl(temp, addr, a->imm); addr = temp; } + addr = gen_pm_adjust_address(ctx, addr); tcg_gen_qemu_ld_tl(dest, addr, ctx->mem_idx, memop); gen_set_gpr(ctx, a->rd, dest);
@@ -185,6 +186,7 @@ static bool gen_store(DisasContext *ctx, arg_sb *a, MemOp memop) tcg_gen_addi_tl(temp, addr, a->imm); addr = temp; } + addr = gen_pm_adjust_address(ctx, addr); tcg_gen_qemu_st_tl(data, addr, ctx->mem_idx, memop); return true;
target/riscv/translate.c +8 -0
@@ -271,6 +271,14 @@ static void gen_jal(DisasContext *ctx, int rd, target_ulong imm) ctx->base.is_jmp =DISAS_NORETURN ; } +/* +* Temp stub: generates address adjustment for PointerMasking +*/ +static TCGv gen_pm_adjust_address (DisasContext *s,TCGv src) +{ +return src; +} +#ifndef CONFIG_USER_ONLY /* The states of mstatus_fs are: * 0 = disabled, 1 = initial, 2 = clean, 3 = dirty
Signature · 1 line
2.30.2
PATCH v16 7/8 Implement address masking functions required for RISC-V Pointer Masking extension
In reply to Alexey BaturoFrom: Anatoly Parshintsev <[email protected]>
--- target/riscv/cpu.h | 2 ++ target/riscv/cpu_helper.c | 18 ++++++++++++++++++ target/riscv/translate.c | 39 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 57 insertions(+), 2 deletions(-)
target/riscv/cpu.h +2 -0
@@ -410,6 +410,8 @@ FIELD(TB_FLAGS, HLSX, 10, 1)FIELD (TB_FLAGS ,MSTATUS_HS_FS ,11 ,2 ) /* The combination of MXL/SXL/UXL that applies to the current cpu mode. */FIELD (TB_FLAGS ,XL ,13 ,2 ) +/* If PointerMasking should be applied */ +FIELD(TB_FLAGS, PM_ENABLED, 15, 1)#ifdef TARGET_RISCV32 #define riscv_cpu_mxl (env) ((void)(env), MXL_RV32)
target/riscv/cpu_helper.c +18 -0
@@ -107,6 +107,24 @@ void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc, flags =FIELD_DP32 (flags,TB_FLAGS ,MSTATUS_HS_FS , get_field(env->mstatus_hs, MSTATUS_FS)); } +if (riscv_has_ext (env,RVJ )) { +int priv = flags &TB_FLAGS_PRIV_MMU_MASK ; +bool pm_enabled = false; +switch (priv) { +case PRV_U : + pm_enabled = env->mmte &U_PM_ENABLE ; +break ; +case PRV_S : + pm_enabled = env->mmte &S_PM_ENABLE ; +break ; +case PRV_M : + pm_enabled = env->mmte &M_PM_ENABLE ; +break ; +default : +g_assert_not_reached (); + } + flags =FIELD_DP32 (flags,TB_FLAGS ,PM_ENABLED , pm_enabled); + }#endif flags =FIELD_DP32 (flags,TB_FLAGS ,XL ,cpu_get_xl (env));
target/riscv/translate.c +37 -2
@@ -36,6 +36,9 @@ static TCGv cpu_gpr[32], cpu_pc, cpu_vl;static TCGv_i64 cpu_fpr[32 ];/* assume F and D extensions */ static TCGv load_res;static TCGv load_val; +/* globals for PM CSRs */ +static TCGv pm_mask[4 ]; +static TCGv pm_base[4 ];#include "exec/gen-icount.h"
@@ -83,6 +86,10 @@ typedef struct DisasContext {TCGv zero;/* Space for 3 operands plus 1 extra for address computation. */ TCGv temp[4 ]; +/* PointerMasking extension */ +bool pm_enabled; +TCGv pm_mask; +TCGv pm_base; } DisasContext;static inline bool has_ext (DisasContext *ctx,uint32_t ext)
@@ -272,11 +279,20 @@ static void gen_jal(DisasContext *ctx, int rd, target_ulong imm) }/* -* Temp stub: generates address adjustment for PointerMasking +* Generates address adjustment for PointerMasking */ static TCGv gen_pm_adjust_address (DisasContext *s,TCGv src) { -return src; +TCGv temp; +if (!s->pm_enabled) { +/* Load unmodified address */ +return src; + }else { + temp =temp_new (s); +tcg_gen_andc_tl (temp, src, s->pm_mask); +tcg_gen_or_tl (temp, temp, s->pm_base); +return temp; + } }#ifndef CONFIG_USER_ONLY
@@ -622,6 +638,10 @@ static void riscv_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs) ctx->cs = cs; ctx->ntemp =0 ;memset (ctx->temp,0 ,sizeof (ctx->temp)); + ctx->pm_enabled =FIELD_EX32 (tb_flags,TB_FLAGS ,PM_ENABLED ); +int priv = tb_flags &TB_FLAGS_PRIV_MMU_MASK ; + ctx->pm_mask = pm_mask[priv]; + ctx->pm_base = pm_base[priv]; ctx->zero =tcg_constant_tl (0 ); }
@@ -735,4 +755,19 @@ void riscv_translate_init(void) "load_res"); load_val =tcg_global_mem_new (cpu_env, offsetof(CPURISCVState , load_val),"load_val" ); +#ifndef CONFIG_USER_ONLY +/* Assign PM CSRs to tcg globals */ + pm_mask[PRV_U ] = +tcg_global_mem_new (cpu_env, offsetof(CPURISCVState , upmmask),"upmmask" ); + pm_base[PRV_U ] = +tcg_global_mem_new (cpu_env, offsetof(CPURISCVState , upmbase),"upmbase" ); + pm_mask[PRV_S ] = +tcg_global_mem_new (cpu_env, offsetof(CPURISCVState , spmmask),"spmmask" ); + pm_base[PRV_S ] = +tcg_global_mem_new (cpu_env, offsetof(CPURISCVState , spmbase),"spmbase" ); + pm_mask[PRV_M ] = +tcg_global_mem_new (cpu_env, offsetof(CPURISCVState , mpmmask),"mpmmask" ); + pm_base[PRV_M ] = +tcg_global_mem_new (cpu_env, offsetof(CPURISCVState , mpmbase),"mpmbase" ); +#endif }
Signature · 1 line
2.30.2
PATCH v16 8/8 Allow experimental J-ext to be turned on
In reply to Alexey Baturo--- target/riscv/cpu.c | 4 ++++ 1 file changed, 4 insertions(+)
target/riscv/cpu.c +4 -0
@@ -562,6 +562,9 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp) }set_vext_version (env, vext_version); } +if (cpu->cfg.ext_j) { + ext |=RVJ ; + }set_misa (env, env->misa_mxl, ext); }
@@ -637,6 +640,7 @@ static Property riscv_cpu_properties[] = {DEFINE_PROP_BOOL ("x-zbc" ,RISCVCPU , cfg.ext_zbc, false),DEFINE_PROP_BOOL ("x-zbs" ,RISCVCPU , cfg.ext_zbs, false),DEFINE_PROP_BOOL ("x-h" ,RISCVCPU , cfg.ext_h, false), +DEFINE_PROP_BOOL ("x-j" ,RISCVCPU , cfg.ext_j, false),DEFINE_PROP_BOOL ("x-v" ,RISCVCPU , cfg.ext_v, false),DEFINE_PROP_STRING ("vext_spec" ,RISCVCPU , cfg.vext_spec), DEFINE_PROP_UINT16("vlen", RISCVCPU, cfg.vlen, 128),
Signature · 1 line
2.30.2
Alistair Francis
AppliedQuoted 34 lines · Alexey Baturo
v15:
Renamed pm intopointer_maskingin machine state.v14:
Addressed Richard's comments from previous series.v13:
Rebased QEMU and addressed Richard's comment.v12:
Updated function for adjusting address with pointer masking to allocate and use temp register.v11:
Addressed a few style issues Alistair mentioned in the previous review.If this patch series would be accepted, I think my further attention would be to:
- Support pm for memory operations for RVV
- Add proper csr and support pm for memory operations for Hypervisor mode
- Support address wrapping on unaligned accesses as @Richard mentioned previouslyThanks!
Alexey Baturo (7):
[RISCV_PM] Add J-extension into RISC-V
[RISCV_PM] Add CSR defines for RISC-V PM extension [RISCV_PM] Support CSRs required for RISC-V PM extension except for
the h-mode
[RISCV_PM] Add J extension state description
[RISCV_PM] Print new PM CSRs in QEMU logs
[RISCV_PM] Support pointer masking for RISC-V for i/c/f/d/a types of
instructions
[RISCV_PM] Allow experimental J-ext to be turned on
Quoted 3 lines
Anatoly Parshintsev (1):
[RISCV_PM] Implement address masking functions required for RISC-V
Pointer Masking extension
Thanks!
Applied to riscv-to-apply.next
I added a "target/riscv: " prefix to each patch title to indicate that the patches are for RISC-V.
Alistair
Quoted 18 lines
target/riscv/cpu.c | 13 ++ target/riscv/cpu.h | 15 ++ target/riscv/cpu_bits.h | 96 ++++++++ target/riscv/cpu_helper.c | 18 ++ target/riscv/csr.c | 285 ++++++++++++++++++++++++ target/riscv/insn_trans/trans_rva.c.inc | 3 + target/riscv/insn_trans/trans_rvd.c.inc | 2 + target/riscv/insn_trans/trans_rvf.c.inc | 2 + target/riscv/insn_trans/trans_rvi.c.inc | 2 + target/riscv/machine.c | 27 +++ target/riscv/translate.c | 43 ++++ 11 files changed, 506 insertions(+)--
2.30.2