riscv support for control flow integrity extensions
v4 cover riscv support for control flow integrity extensions
v4 for riscv zicfilp and zicfiss extensions support in qemu.
Links for previous versions
[1] - v1 https://lists.nongnu.org/archive/html/qemu-devel/2024-07/msg06017.html
[2] - v2 https://lore.kernel.org/all/[email protected]/T/
[3] - v3 https://lists.nongnu.org/archive/html/qemu-devel/2024-08/msg01005.html
---
v4:
- elp state in cpu is true/false instead of enum and elp cleared unconditionally on trap entry. elp in *status cleared unconditionally on trap return.
- Moved logic for branch tracking in instruction translation from tb_start.
- fixed zicfiss dependency on 'A'
- cpu_get_fcfien/bcfien helpers checks fixed to check for extension first.
- removed trace hook enums. Instead added dedicated trace helpers wherever needed.
- fixed/simplified instruction format in decoder for lpad, sspush, sspopchk
- simplified tlb index logic for shadow stack instructions. Removed SUM TB_FLAG
- access to ssp CSR is gated on cpu_get_bcfien instead of duplicated logic
- removed vDSO related changes for now.
v3:
- Removed prctl specific patches because they need to be upstream
in kernel first.
- As suggested by Richard, added TB flag if fcfi enabled
- Re-worked translation for landing pad and shadow stack instructions to not require helper.
- tcg helpers only for cfi violation cases so that trace hooks can be placed.
- Style changes.
- fixes assert condition in accel/tcg
v2:
- added missed file (in v1) for shadow stack instructions implementation.
Deepak Gupta (16):
target/riscv: Add zicfilp extension
target/riscv: Introduce elp state and enabling controls for zicfilp target/riscv: save and restore elp state on priv transitions target/riscv: additional code information for sw check target/riscv: tracking indirect branches (fcfi) for zicfilp target/riscv: zicfilp lpad impl and branch tracking disas/riscv: enabled lpad disassembly
target/riscv: Add zicfiss extension
target/riscv: introduce ssp and enabling controls for zicfiss target/riscv: tb flag for shadow stack instructions target/riscv: mmu changes for zicfiss shadow stack protection target/riscv: implement zicfiss instructions target/riscv: compressed encodings for sspush and sspopchk disas/riscv: enable disassembly for zicfiss instructions disas/riscv: enable disassembly for compressed sspush/sspopchk target/riscv: add trace-hooks for each case of sw-check exception
disas/riscv.c | 77 ++++++++- disas/riscv.h | 4 + include/tcg/tcg.h | 1 + target/riscv/cpu.c | 17 ++ target/riscv/cpu.h | 15 ++ target/riscv/cpu_bits.h | 17 ++ target/riscv/cpu_cfg.h | 2 + target/riscv/cpu_helper.c | 150 +++++++++++++++++- target/riscv/cpu_user.h | 1 + target/riscv/csr.c | 84 ++++++++++ target/riscv/helper.h | 6 + target/riscv/insn16.decode | 2 + target/riscv/insn32.decode | 26 ++- target/riscv/insn_trans/trans_rva.c.inc | 43 +++++ target/riscv/insn_trans/trans_rvi.c.inc | 56 +++++++ target/riscv/insn_trans/trans_rvzicfiss.c.inc | 124 +++++++++++++++ target/riscv/internals.h | 3 + target/riscv/op_helper.c | 37 +++++ target/riscv/pmp.c | 5 + target/riscv/pmp.h | 3 +- target/riscv/tcg/tcg-cpu.c | 24 +++ target/riscv/trace-events | 6 + target/riscv/translate.c | 48 ++++++ 23 files changed, 742 insertions(+), 9 deletions(-) create mode 100644 target/riscv/insn_trans/trans_rvzicfiss.c.inc
Signature · 1 line
2.44.0
PATCH v4 9/16 target/riscv: introduce ssp and enabling controls for zicfiss
zicfiss introduces a new state ssp ("shadow stack register") in cpu. ssp is expressed as a new unprivileged csr (CSR_SSP=0x11) and holds virtual address for shadow stack as programmed by software.
Shadow stack (for each mode) is enabled via bit3 in *envcfg CSRs. Shadow stack can be enabled for a mode only if it's higher privileged mode had it enabled for itself. M mode doesn't need enabling control, it's always available if extension is available on cpu.
This patch also implements helper bcfi function which determines if bcfi is enabled at current privilege or not. qemu-user also gets field ubcfien indicating whether qemu user has shadow stack enabled or not.
--- target/riscv/cpu.c | 5 ++++ target/riscv/cpu.h | 4 +++ target/riscv/cpu_bits.h | 6 +++++ target/riscv/cpu_helper.c | 25 +++++++++++++++++++ target/riscv/csr.c | 52 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 92 insertions(+)
target/riscv/cpu.c +5 -0
@@ -998,6 +998,9 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type)/* on reset elp is clear */ env->elp = false; +/* on reset ssp is set to 0 */ + env->ssp =0 ; +/* * Bits 10, 6, 2 and 12 of mideleg are read only 1 when the Hypervisor * extension is enabled.
@@ -1026,6 +1029,8 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type)#ifdef CONFIG_USER_ONLY /* qemu-user for riscv, fcfi is off by default */ env->ufcfien = false; +/* qemu-user for riscv, bcfi is off by default */ + env->ubcfien = false;#endif #ifndef CONFIG_USER_ONLY
target/riscv/cpu.h +4 -0
@@ -224,11 +224,14 @@ struct CPUArchState {/* elp state for zicfilp extension */ bool elp; +/* shadow stack register for zicfiss extension */ +target_ulong ssp;/* sw check code for sw check exception */ target_ulong sw_check_code;#ifdef CONFIG_USER_ONLY uint32_t elf_flags;bool ufcfien; +bool ubcfien;#endif #ifndef CONFIG_USER_ONLY
@@ -534,6 +537,7 @@ bool riscv_cpu_vector_enabled(CPURISCVState *env);void riscv_cpu_set_virt_enabled (CPURISCVState *env,bool enable);int riscv_env_mmu_index (CPURISCVState *env,bool ifetch);bool cpu_get_fcfien (CPURISCVState *env); +bool cpu_get_bcfien (CPURISCVState *env); G_NORETURN void riscv_cpu_do_unaligned_access(CPUState *cs, vaddr addr,MMUAccessType access_type,int mmu_idx,uintptr_t retaddr);
target/riscv/cpu_bits.h +6 -0
@@ -34,6 +34,9 @@/* Control and Status Registers */ +/* zicfiss user ssp csr */ +#define CSR_SSP 0x011 +/* User Trap Setup */ #define CSR_USTATUS 0x000#define CSR_UIE 0x004
@@ -754,6 +757,7 @@ typedef enum RISCVException {/* Execution environment configuration bits */ #define MENVCFG_FIOM BIT(0)#define MENVCFG_LPE BIT(2)/* zicfilp */ +#define MENVCFG_SSE BIT(3)/* zicfiss */ #define MENVCFG_CBIE (3UL << 4)#define MENVCFG_CBCFE BIT(6)#define MENVCFG_CBZE BIT(7)
@@ -768,12 +772,14 @@ typedef enum RISCVException {#define SENVCFG_FIOM MENVCFG_FIOM#define SENVCFG_LPE MENVCFG_LPE +#define SENVCFG_SSE MENVCFG_SSE#define SENVCFG_CBIE MENVCFG_CBIE#define SENVCFG_CBCFE MENVCFG_CBCFE#define SENVCFG_CBZE MENVCFG_CBZE#define HENVCFG_FIOM MENVCFG_FIOM#define HENVCFG_LPE MENVCFG_LPE +#define HENVCFG_SSE MENVCFG_SSE#define HENVCFG_CBIE MENVCFG_CBIE#define HENVCFG_CBCFE MENVCFG_CBCFE#define HENVCFG_CBZE MENVCFG_CBZE
target/riscv/cpu_helper.c +25 -0
@@ -92,6 +92,31 @@ bool cpu_get_fcfien(CPURISCVState *env)#endif } +bool cpu_get_bcfien (CPURISCVState *env) +{ +/* no cfi extension, return false */ +if (!env_archcpu (env)->cfg.ext_zicfiss) { +return false; + } +#ifdef CONFIG_USER_ONLY +return env->ubcfien; +#else +switch (env->priv) { +case PRV_U : +return env->senvcfg &SENVCFG_SSE ; +case PRV_S : +if (env->virt_enabled) { +return env->henvcfg &HENVCFG_SSE ; + } +return env->menvcfg &MENVCFG_SSE ; +case PRV_M :/* M-mode shadow stack is always on if hart implements */ +return true; +default : +g_assert_not_reached (); + } +#endif +} +void cpu_get_tb_cpu_state (CPURISCVState *env,vaddr *pc,uint64_t *cs_base,uint32_t *pflags) {
target/riscv/csr.c +52 -0
88 lines not on this page · raw message
Signature · 1 line
2.44.0
PATCH v4 10/16 target/riscv: tb flag for shadow stack instructions
In reply to Deepak GuptaShadow stack instructions can be decoded as zimop / zcmop or shadow stack instructions depending on whether shadow stack are enabled at current privilege. This requires a TB flag so that correct TB generation and correct TB lookup happens. DisasContext gets a field indicating whether bcfi is enabled or not.
--- target/riscv/cpu.h | 2 ++ target/riscv/cpu_helper.c | 4 ++++ target/riscv/translate.c | 4 ++++ 3 files changed, 10 insertions(+)
target/riscv/cpu.h +2 -0
@@ -613,6 +613,8 @@ FIELD(TB_FLAGS, AXL, 26, 2)/* zicfilp needs a TB flag to track indirect branches */ FIELD(TB_FLAGS, FCFI_ENABLED, 28, 1)FIELD (TB_FLAGS ,FCFI_LP_EXPECTED ,29 ,1 ) +/* zicfiss needs a TB flag so that correct TB is located based on tb flags */ +FIELD(TB_FLAGS, BCFI_ENABLED, 30, 1)#ifdef TARGET_RISCV32 #define riscv_cpu_mxl (env) ((void)(env), MXL_RV32)
target/riscv/cpu_helper.c +4 -0
@@ -170,6 +170,10 @@ void cpu_get_tb_cpu_state(CPURISCVState *env, vaddr *pc, flags =FIELD_DP32 (flags,TB_FLAGS ,FCFI_ENABLED ,1 ); } +if (cpu_get_bcfien (env)) { + flags =FIELD_DP32 (flags,TB_FLAGS ,BCFI_ENABLED ,1 ); + } +#ifdef CONFIG_USER_ONLY fs =EXT_STATUS_DIRTY ; vs =EXT_STATUS_DIRTY ;
target/riscv/translate.c +4 -0
@@ -119,6 +119,8 @@ typedef struct DisasContext {/* zicfilp extension. fcfi_enabled, lp expected or not */ bool fcfi_enabled;bool fcfi_lp_expected; +/* zicfiss extension, if shadow stack was enabled during TB gen */ +bool bcfi_enabled; } DisasContext;static inline bool has_ext (DisasContext *ctx,uint32_t ext)
@@ -1241,6 +1243,8 @@ static void riscv_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs) ctx->pm_base_enabled =FIELD_EX32 (tb_flags,TB_FLAGS ,PM_BASE_ENABLED ); ctx->ztso = cpu->cfg.ext_ztso; ctx->itrigger =FIELD_EX32 (tb_flags,TB_FLAGS ,ITRIGGER ); + ctx->bcfi_enabled =cpu_get_bcfien (env) && +FIELD_EX32 (tb_flags,TB_FLAGS ,BCFI_ENABLED ); ctx->fcfi_lp_expected =FIELD_EX32 (tb_flags,TB_FLAGS ,FCFI_LP_EXPECTED ); ctx->fcfi_enabled =FIELD_EX32 (tb_flags,TB_FLAGS ,FCFI_ENABLED ); ctx->zero =tcg_constant_tl (0 );
Signature · 1 line
2.44.0
PATCH v4 6/16 target/riscv: zicfilp `lpad` impl and branch tracking
In reply to Deepak GuptaImplements setting lp expected when jalr is encountered and implements lpad instruction of zicfilp. lpad instruction is taken out of auipc x0, <imm_20>. This is an existing HINTNOP space. If lpad is target of an indirect branch, cpu checks for 20 bit value in x7 upper with 20 bit value embedded in lpad. If they don't match, cpu raises a sw check exception with tval = 2.
--- target/riscv/cpu_user.h | 1 + target/riscv/insn32.decode | 5 ++- target/riscv/insn_trans/trans_rvi.c.inc | 53 +++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-)
target/riscv/cpu_user.h +1 -0
@@ -15,5 +15,6 @@#define xA6 16#define xA7 17/* syscall number for RVI ABI */ #define xT0 5/* syscall number for RVE ABI */ +#define xT2 7#endif
target/riscv/insn32.decode +4 -1
@@ -123,7 +123,10 @@ sfence_vm 0001000 00100 ..... 000 00000 1110011 @sfence_vm # *** RV32I Base Instruction Set *** lui .................... ..... 0110111 @u -auipc .................... ..... 0010111 @u +{ + lpad label:20 00000 0010111 + auipc .................... ..... 0010111 @u +} jal .................... ..... 1101111 @j jalr ............ ..... 000 ..... 1100111 @i beq ....... ..... ..... 000 ..... 1100011 @b
target/riscv/insn_trans/trans_rvi.c.inc +53 -0
65 lines not on this page · raw message
Signature · 1 line
2.44.0
PATCH v4 1/16 target/riscv: Add zicfilp extension
In reply to Deepak Guptazicfilp [1] riscv cpu extension enables forward control flow integrity. If enabled, all indirect calls must land on a landing pad instruction.
This patch sets up space for zicfilp extension in cpuconfig. zicfilp is dependend on zicsr.
[1] - https://github.com/riscv/riscv-cfi
--- target/riscv/cpu.c | 2 ++ target/riscv/cpu_cfg.h | 1 + target/riscv/tcg/tcg-cpu.c | 5 +++++ 3 files changed, 8 insertions(+)
target/riscv/cpu.c +2 -0
@@ -106,6 +106,7 @@ const RISCVIsaExtData isa_edata_arr[] = {ISA_EXT_DATA_ENTRY (ziccif,PRIV_VERSION_1_11_0 , has_priv_1_11),ISA_EXT_DATA_ENTRY (zicclsm,PRIV_VERSION_1_11_0 , has_priv_1_11),ISA_EXT_DATA_ENTRY (ziccrse,PRIV_VERSION_1_11_0 , has_priv_1_11), +ISA_EXT_DATA_ENTRY (zicfilp,PRIV_VERSION_1_12_0 , ext_zicfilp),ISA_EXT_DATA_ENTRY (zicond,PRIV_VERSION_1_12_0 , ext_zicond),ISA_EXT_DATA_ENTRY (zicntr,PRIV_VERSION_1_12_0 , ext_zicntr), ISA_EXT_DATA_ENTRY(zicsr, PRIV_VERSION_1_10_0, ext_zicsr),
@@ -1472,6 +1473,7 @@ const RISCVCPUMultiExtConfig riscv_cpu_extensions[] = {/* Defaults for standard extensions */ MULTI_EXT_CFG_BOOL ("sscofpmf" , ext_sscofpmf, false),MULTI_EXT_CFG_BOOL ("zifencei" , ext_zifencei, true), +MULTI_EXT_CFG_BOOL ("zicfilp" , ext_zicfilp, false),MULTI_EXT_CFG_BOOL ("zicsr" , ext_zicsr, true),MULTI_EXT_CFG_BOOL ("zihintntl" , ext_zihintntl, true), MULTI_EXT_CFG_BOOL("zihintpause", ext_zihintpause, true),
target/riscv/cpu_cfg.h +1 -0
@@ -67,6 +67,7 @@ struct RISCVCPUConfig {bool ext_zicbom;bool ext_zicbop;bool ext_zicboz; +bool ext_zicfilp;bool ext_zicond;bool ext_zihintntl;bool ext_zihintpause;
target/riscv/tcg/tcg-cpu.c +5 -0
@@ -623,6 +623,11 @@ void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp) cpu->pmu_avail_ctrs =0 ; } +if (cpu->cfg.ext_zicfilp && !cpu->cfg.ext_zicsr) { +error_setg (errp,"zicfilp extension requires zicsr extension" ); +return ; + } +/* * Disable isa extensions based on priv spec after we * validated and set everything we need.
Signature · 1 line
2.44.0
PATCH v4 2/16 target/riscv: Introduce elp state and enabling controls for zicfilp
In reply to Deepak Guptazicfilp introduces a new state elp ("expected landing pad") in cpu. During normal execution, elp is idle (NO_LP_EXPECTED) i.e not expecting landing pad. On an indirect call, elp moves LP_EXPECTED. When elp is LP_EXPECTED, only a subsquent landing pad instruction can set state back to NO_LP_EXPECTED. On reset, elp is set to NO_LP_EXPECTED.
zicfilp is enabled via bit2 in *envcfg CSRs. Enabling control for M-mode is in mseccfg CSR at bit position 10.
On trap, elp state is saved away in *status.
--- target/riscv/cpu.c | 3 +++ target/riscv/cpu.h | 2 ++ target/riscv/cpu_bits.h | 6 ++++++ target/riscv/csr.c | 31 +++++++++++++++++++++++++++++++ target/riscv/pmp.c | 5 +++++ target/riscv/pmp.h | 3 ++- 6 files changed, 49 insertions(+), 1 deletion(-)
target/riscv/cpu.c +3 -0
@@ -994,6 +994,9 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type)/* mmte is supposed to have pm.current hardwired to 1 */ env->mmte |= (EXT_STATUS_INITIAL |MMTE_M_PM_CURRENT ); +/* on reset elp is clear */ + env->elp = false; +/* * Bits 10, 6, 2 and 12 of mideleg are read only 1 when the Hypervisor * extension is enabled.
target/riscv/cpu.h +2 -0
@@ -222,6 +222,8 @@ struct CPUArchState {target_ulong jvt; +/* elp state for zicfilp extension */ +bool elp;#ifdef CONFIG_USER_ONLY uint32_t elf_flags;#endif
target/riscv/cpu_bits.h +6 -0
@@ -545,6 +545,8 @@#define MSTATUS_TVM 0x00100000/* since: priv-1.10 */ #define MSTATUS_TW 0x00200000/* since: priv-1.10 */ #define MSTATUS_TSR 0x00400000/* since: priv-1.10 */ +#define MSTATUS_SPELP 0x00800000/* zicfilp */ +#define MSTATUS_MPELP 0x020000000000/* zicfilp */ #define MSTATUS_GVA 0x4000000000ULL#define MSTATUS_MPV 0x8000000000ULL
@@ -575,6 +577,7 @@ typedef enum {#define SSTATUS_XS 0x00018000#define SSTATUS_SUM 0x00040000/* since: priv-1.10 */ #define SSTATUS_MXR 0x00080000 +#define SSTATUS_SPELP MSTATUS_SPELP/* zicfilp */ #define SSTATUS64_UXL 0x0000000300000000ULL
@@ -747,6 +750,7 @@ typedef enum RISCVException {/* Execution environment configuration bits */ #define MENVCFG_FIOM BIT(0) +#define MENVCFG_LPE BIT(2)/* zicfilp */ #define MENVCFG_CBIE (3UL << 4)#define MENVCFG_CBCFE BIT(6)#define MENVCFG_CBZE BIT(7)
@@ -760,11 +764,13 @@ typedef enum RISCVException {#define MENVCFGH_STCE BIT(31)#define SENVCFG_FIOM MENVCFG_FIOM +#define SENVCFG_LPE MENVCFG_LPE#define SENVCFG_CBIE MENVCFG_CBIE#define SENVCFG_CBCFE MENVCFG_CBCFE#define SENVCFG_CBZE MENVCFG_CBZE#define HENVCFG_FIOM MENVCFG_FIOM +#define HENVCFG_LPE MENVCFG_LPE#define HENVCFG_CBIE MENVCFG_CBIE#define HENVCFG_CBCFE MENVCFG_CBCFE#define HENVCFG_CBZE MENVCFG_CBZE
target/riscv/csr.c +31 -0
@@ -1400,6 +1400,11 @@ static RISCVException write_mstatus(CPURISCVState *env, int csrno, } } +/* If cfi lp extension is available, then apply cfi lp mask */ +if (env_archcpu (env)->cfg.ext_zicfilp) { + mask |= (MSTATUS_MPELP |MSTATUS_SPELP ); + } + mstatus = (mstatus & ~mask) | (val & mask); env->mstatus = mstatus;
@@ -2101,6 +2106,10 @@ static RISCVException write_menvcfg(CPURISCVState *env, int csrno, mask |= (cfg->ext_svpbmt ?MENVCFG_PBMTE :0 ) | (cfg->ext_sstc ?MENVCFG_STCE :0 ) | (cfg->ext_svadu ?MENVCFG_ADUE :0 ); + +if (env_archcpu (env)->cfg.ext_zicfilp) { + mask |=MENVCFG_LPE ; + } } env->menvcfg = (env->menvcfg & ~mask) | (val & mask);
@@ -2153,6 +2162,10 @@ static RISCVException write_senvcfg(CPURISCVState *env, int csrno,return ret; } +if (env_archcpu (env)->cfg.ext_zicfilp) { + mask |=SENVCFG_LPE ; + } + env->senvcfg = (env->senvcfg & ~mask) | (val & mask);return RISCV_EXCP_NONE ; }
@@ -2190,6 +2203,10 @@ static RISCVException write_henvcfg(CPURISCVState *env, int csrno,if (riscv_cpu_mxl (env) ==MXL_RV64 ) { mask |= env->menvcfg & (HENVCFG_PBMTE |HENVCFG_STCE |HENVCFG_ADUE ); + +if (env_archcpu (env)->cfg.ext_zicfilp) { + mask |=HENVCFG_LPE ; + } } env->henvcfg = (env->henvcfg & ~mask) | (val & mask);
@@ -2654,6 +2671,10 @@ static RISCVException read_sstatus_i128(CPURISCVState *env, int csrno, mask |=SSTATUS64_UXL ; } +if (env_archcpu (env)->cfg.ext_zicfilp) { + mask |=SSTATUS_SPELP ; + } + *val =int128_make128 (sstatus,add_status_sd (MXL_RV128 , sstatus));return RISCV_EXCP_NONE ; }
@@ -2665,6 +2686,11 @@ static RISCVException read_sstatus(CPURISCVState *env, int csrno,if (env->xl !=MXL_RV32 || env->debugger) { mask |=SSTATUS64_UXL ; } + +if (env_archcpu (env)->cfg.ext_zicfilp) { + mask |=SSTATUS_SPELP ; + } +/* TODO: Use SXL not MXL. */ *val =add_status_sd (riscv_cpu_mxl (env), env->mstatus & mask);return RISCV_EXCP_NONE ;
@@ -2680,6 +2706,11 @@ static RISCVException write_sstatus(CPURISCVState *env, int csrno, mask |=SSTATUS64_UXL ; } } + +if (env_archcpu (env)->cfg.ext_zicfilp) { + mask |=SSTATUS_SPELP ; + } +target_ulong newval = (env->mstatus & ~mask) | (val & mask);return write_mstatus (env,CSR_MSTATUS , newval); }
target/riscv/pmp.c +5 -0
@@ -598,6 +598,11 @@ void mseccfg_csr_write(CPURISCVState *env, target_ulong val) val &= ~(MSECCFG_MMWP |MSECCFG_MML |MSECCFG_RLB ); } +/* M-mode forward cfi to be enabled if cfi extension is implemented */ +if (env_archcpu (env)->cfg.ext_zicfilp) { + val |= (val &MSECCFG_MLPE ); + } + env->mseccfg = val; }
target/riscv/pmp.h +2 -1
@@ -44,7 +44,8 @@ typedef enum {MSECCFG_MMWP =1 <<1 ,MSECCFG_RLB =1 <<2 ,MSECCFG_USEED =1 <<8 , - MSECCFG_SSEED = 1 << 9 +MSECCFG_SSEED =1 <<9 , +MSECCFG_MLPE =1 <<10 , } mseccfg_field_t; typedef struct {
Signature · 1 line
2.44.0
PATCH v4 4/16 target/riscv: additional code information for sw check
In reply to Deepak Guptasw check exception support was recently added. This patch further augments sw check exception by providing support for additional code which is provided in *tval. Adds sw_check_code field in cpuarchstate. Whenever sw check exception is raised *tval gets the value deposited in sw_check_code.
--- target/riscv/cpu.h | 2 ++ target/riscv/cpu_helper.c | 2 ++ target/riscv/csr.c | 1 + 3 files changed, 5 insertions(+)
target/riscv/cpu.h +2 -0
@@ -224,6 +224,8 @@ struct CPUArchState {/* elp state for zicfilp extension */ bool elp; +/* sw check code for sw check exception */ +target_ulong sw_check_code;#ifdef CONFIG_USER_ONLY uint32_t elf_flags;bool ufcfien;
target/riscv/cpu_helper.c +2 -0
@@ -1761,6 +1761,8 @@ void riscv_cpu_do_interrupt(CPUState *cs) cs->watchpoint_hit =NULL ; }break ; +case RISCV_EXCP_SW_CHECK : + tval = env->sw_check_code;default :break ; }
target/riscv/csr.c +1 -0
@@ -1179,6 +1179,7 @@ static const uint64_t all_ints = M_MODE_INTERRUPTS | S_MODE_INTERRUPTS | (1ULL << (RISCV_EXCP_INST_PAGE_FAULT )) | \ (1ULL << (RISCV_EXCP_LOAD_PAGE_FAULT )) | \ (1ULL << (RISCV_EXCP_STORE_PAGE_FAULT )) | \ + (1ULL << (RISCV_EXCP_SW_CHECK )) | \ (1ULL << (RISCV_EXCP_INST_GUEST_PAGE_FAULT )) | \ (1ULL << (RISCV_EXCP_LOAD_GUEST_ACCESS_FAULT )) | \ (1ULL << (RISCV_EXCP_VIRT_INSTRUCTION_FAULT)) | \
Signature · 1 line
2.44.0
PATCH v4 5/16 target/riscv: tracking indirect branches (fcfi) for zicfilp
In reply to Deepak Guptazicfilp protects forward control flow (if enabled) by enforcing all indirect call and jmp must land on a landing pad instruction lpad. If target of an indirect call or jmp is not lpad then cpu/hart must raise a sw check exception with tval = 2.
This patch implements the mechanism using TCG. Target architecture branch instruction must define the end of a TB. Using this property, during translation of branch instruction, TB flag = FCFI_LP_EXPECTED can be set. Translation of target TB can check if FCFI_LP_EXPECTED flag is set and a flag (fcfi_lp_expected) can be set in DisasContext. If lpad gets translated, fcfi_lp_expected flag in DisasContext can be cleared. Else it'll fault.
--- include/tcg/tcg.h | 1 + target/riscv/cpu.h | 3 +++ target/riscv/cpu_bits.h | 3 +++ target/riscv/cpu_helper.c | 12 ++++++++++++ target/riscv/helper.h | 3 +++ target/riscv/op_helper.c | 6 ++++++ target/riscv/translate.c | 37 +++++++++++++++++++++++++++++++++++++ 7 files changed, 65 insertions(+)
include/tcg/tcg.h +1 -0
@@ -528,6 +528,7 @@ struct TCGContext {#endif TCGLabel *exitreq_label; +TCGOp *cfi_lp_check;#ifdef CONFIG_PLUGIN /*
target/riscv/cpu.h +3 -0
@@ -606,6 +606,9 @@ FIELD(TB_FLAGS, ITRIGGER, 22, 1) FIELD(TB_FLAGS, VIRT_ENABLED, 23, 1) FIELD(TB_FLAGS, PRIV, 24, 2)FIELD (TB_FLAGS ,AXL ,26 ,2 ) +/* zicfilp needs a TB flag to track indirect branches */ +FIELD(TB_FLAGS, FCFI_ENABLED, 28, 1) +FIELD(TB_FLAGS, FCFI_LP_EXPECTED, 29, 1)#ifdef TARGET_RISCV32 #define riscv_cpu_mxl (env) ((void)(env), MXL_RV32)
target/riscv/cpu_bits.h +3 -0
@@ -685,6 +685,9 @@ typedef enum RISCVException {RISCV_EXCP_SEMIHOST =0x3f , } RISCVException; +/* zicfilp defines lp violation results in sw check with tval = 2*/ +#define RISCV_EXCP_SW_CHECK_FCFI_TVAL 2 +#define RISCV_EXCP_INT_FLAG 0x80000000#define RISCV_EXCP_INT_MASK 0x7fffffff
target/riscv/cpu_helper.c +12 -0
@@ -133,6 +133,18 @@ void cpu_get_tb_cpu_state(CPURISCVState *env, vaddr *pc, flags =FIELD_DP32 (flags,TB_FLAGS ,VILL ,1 ); } +if (cpu_get_fcfien (env)) { +/* +* For Forward CFI, only the expectation of a lpcll at +* the start of the block is tracked (which can only happen +* when FCFI is enabled for the current processor mode). A jump +* or call at the end of the previous TB will have updated +* env->elp to indicate the expectation. +*/ + flags =FIELD_DP32 (flags,TB_FLAGS ,FCFI_LP_EXPECTED , env->elp); + flags =FIELD_DP32 (flags,TB_FLAGS ,FCFI_ENABLED ,1 ); + } +#ifdef CONFIG_USER_ONLY fs =EXT_STATUS_DIRTY ; vs =EXT_STATUS_DIRTY ;
target/riscv/helper.h +3 -0
@@ -121,6 +121,9 @@ DEF_HELPER_2(cbo_clean_flush, void, env, tl) DEF_HELPER_2(cbo_inval, void, env, tl)DEF_HELPER_2 (cbo_zero, void, env, tl) +/* helper to raise sw check exception */ +DEF_HELPER_2 (raise_sw_check_excep, void, env, tl) + /* Special functions */ DEF_HELPER_2(csrr, tl, env, int)DEF_HELPER_3 (csrw, void, env, int, tl)
target/riscv/op_helper.c +6 -0
@@ -259,6 +259,12 @@ void helper_cbo_inval(CPURISCVState *env, target_ulong address)/* We don't emulate the cache-hierarchy, so we're done. */ } +void helper_raise_sw_check_excep (CPURISCVState *env,target_ulong swcheck_code) +{ + env->sw_check_code = swcheck_code; +riscv_raise_exception (env,RISCV_EXCP_SW_CHECK ,GETPC ()); +} +#ifndef CONFIG_USER_ONLY target_ulong helper_sret (CPURISCVState *env)
target/riscv/translate.c +37 -0
@@ -116,6 +116,9 @@ typedef struct DisasContext {bool frm_valid;bool insn_start_updated;const GPtrArray *decoders; +/* zicfilp extension. fcfi_enabled, lp expected or not */ +bool fcfi_enabled; +bool fcfi_lp_expected; } DisasContext;static inline bool has_ext (DisasContext *ctx,uint32_t ext)
@@ -1238,6 +1241,8 @@ static void riscv_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs) ctx->pm_base_enabled =FIELD_EX32 (tb_flags,TB_FLAGS ,PM_BASE_ENABLED ); ctx->ztso = cpu->cfg.ext_ztso; ctx->itrigger =FIELD_EX32 (tb_flags,TB_FLAGS ,ITRIGGER ); + ctx->fcfi_lp_expected =FIELD_EX32 (tb_flags,TB_FLAGS ,FCFI_LP_EXPECTED ); + ctx->fcfi_enabled =FIELD_EX32 (tb_flags,TB_FLAGS ,FCFI_ENABLED ); ctx->zero =tcg_constant_tl (0 ); ctx->virt_inst_excp = false; ctx->decoders = cpu->decoders;
@@ -1245,6 +1250,7 @@ static void riscv_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs)static void riscv_tr_tb_start (DisasContextBase *db,CPUState *cpu) { + }static void riscv_tr_insn_start (DisasContextBase *dcbase,CPUState *cpu)
@@ -1266,6 +1272,28 @@ static void riscv_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)CPURISCVState *env =cpu_env (cpu);uint16_t opcode16 =translator_lduw (env, &ctx->base, ctx->base.pc_next); +if (ctx->fcfi_lp_expected) { +/* +* Since we can't look ahead to confirm that the first +* instruction is a legal landing pad instruction, emit +* compare-and-branch sequence that will be fixed-up in +* riscv_tr_tb_stop() to either statically hit or skip an +* illegal instruction exception depending on whether the +* flag was lowered by translation of a CJLP or JLP as +* the first instruction in the block. +*/ +TCGv_i32 immediate; +TCGLabel *l; + l =gen_new_label (); + immediate =tcg_temp_new_i32 (); +tcg_gen_movi_i32 (immediate,0 ); + tcg_ctx->cfi_lp_check =tcg_last_op (); +tcg_gen_brcondi_i32 (TCG_COND_EQ , immediate,0 , l); +gen_helper_raise_sw_check_excep (tcg_env, +tcg_constant_tl (RISCV_EXCP_SW_CHECK_FCFI_TVAL )); +gen_set_label (l); + } + ctx->ol = ctx->xl;decode_opc (env, ctx, opcode16); ctx->base.pc_next += ctx->cur_insn_len;
@@ -1303,6 +1331,15 @@ static void riscv_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)default :g_assert_not_reached (); } + +if (ctx->fcfi_lp_expected) { +/* +* If the "lp expected" flag is still up, the block needs to take an +* illegal instruction exception. +*/ +tcg_set_insn_param (tcg_ctx->cfi_lp_check,1 , +tcgv_i32_arg (tcg_constant_i32 (1 ))); + } }static const TranslatorOps riscv_tr_ops = {
Signature · 1 line
2.44.0
PATCH v4 3/16 target/riscv: save and restore elp state on priv transitions
In reply to Deepak Guptaelp state is recorded in *status on trap entry (less privilege to higher privilege) and restored in elp from *status on trap exit (higher to less privilege).
Additionally this patch introduces a forward cfi helper function to determine if current privilege has forward cfi is enabled or not based on *envcfg (for U, VU, S, VU, HS) or mseccfg csr (for M). For qemu-user, a new field ufcfien is introduced which is by default set to false and helper function returns value deposited in ufcfien for qemu-user.
--- target/riscv/cpu.c | 5 ++++ target/riscv/cpu.h | 2 ++ target/riscv/cpu_helper.c | 55 +++++++++++++++++++++++++++++++++++++++ target/riscv/op_helper.c | 18 +++++++++++++ 4 files changed, 80 insertions(+)
target/riscv/cpu.c +5 -0
@@ -1022,6 +1022,11 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type) env->load_res =-1 ;set_default_nan_mode (1 , &env->fp_status); +#ifdef CONFIG_USER_ONLY +/* qemu-user for riscv, fcfi is off by default */ + env->ufcfien = false; +#endif +#ifndef CONFIG_USER_ONLY if (cpu->cfg.debug) {riscv_trigger_reset_hold (env);
target/riscv/cpu.h +2 -0
@@ -226,6 +226,7 @@ struct CPUArchState {bool elp;#ifdef CONFIG_USER_ONLY uint32_t elf_flags; +bool ufcfien;#endif #ifndef CONFIG_USER_ONLY
@@ -530,6 +531,7 @@ void riscv_cpu_set_geilen(CPURISCVState *env, target_ulong geilen);bool riscv_cpu_vector_enabled (CPURISCVState *env);void riscv_cpu_set_virt_enabled (CPURISCVState *env,bool enable);int riscv_env_mmu_index (CPURISCVState *env,bool ifetch); +bool cpu_get_fcfien (CPURISCVState *env); G_NORETURN void riscv_cpu_do_unaligned_access(CPUState *cs, vaddr addr,MMUAccessType access_type,int mmu_idx,uintptr_t retaddr);
target/riscv/cpu_helper.c +55 -0
91 lines not on this page · raw message
target/riscv/op_helper.c +18 -0
@@ -313,6 +313,15 @@ target_ulong helper_sret(CPURISCVState *env)riscv_cpu_set_mode (env, prev_priv); +/* +* If forward cfi enabled for new priv, restore elp status +* and clear spelp in mstatus +*/ +if (cpu_get_fcfien (env)) { + env->elp =get_field (env->mstatus,MSTATUS_SPELP ); + } + env->mstatus =set_field (env->mstatus,MSTATUS_SPELP ,0 ); +return retpc; }
@@ -357,6 +366,15 @@ target_ulong helper_mret(CPURISCVState *env)riscv_cpu_set_virt_enabled (env, prev_virt); } +/* +* If forward cfi enabled for new priv, restore elp status +* and clear mpelp in mstatus +*/ +if (cpu_get_fcfien (env)) { + env->elp =get_field (env->mstatus,MSTATUS_MPELP ); + } + env->mstatus =set_field (env->mstatus,MSTATUS_MPELP ,0 ); +return retpc; }
Signature · 1 line
2.44.0
PATCH v4 16/16 target/riscv: add trace-hooks for each case of sw-check exception
In reply to Deepak GuptaViolations to control flow rules setup by zicfilp and zicfiss lead to software check exceptions. To debug and fix such sw check issues in guest , add trace-hooks for each case.
--- target/riscv/helper.h | 3 +++ target/riscv/insn_trans/trans_rvi.c.inc | 3 +++ target/riscv/insn_trans/trans_rvzicfiss.c.inc | 1 + target/riscv/op_helper.c | 13 +++++++++++++ target/riscv/trace-events | 6 ++++++ target/riscv/translate.c | 2 ++ 6 files changed, 28 insertions(+)
target/riscv/helper.h +3 -0
@@ -123,6 +123,9 @@ DEF_HELPER_2(cbo_zero, void, env, tl)/* helper to raise sw check exception */ DEF_HELPER_2 (raise_sw_check_excep, void, env, tl) +/* helper functions to trace riscv cfi violations */ +DEF_HELPER_3(zicfilp_label_mismatch, void, env, tl, tl) +DEF_HELPER_3 (zicfiss_ra_mismatch, void, env, tl, tl) /* Special functions */DEF_HELPER_2 (csrr, tl, env, int)
target/riscv/insn_trans/trans_rvi.c.inc +3 -0
@@ -54,6 +54,7 @@ static bool trans_lpad(DisasContext *ctx, arg_lpad *a) /* * misaligned, according to spec we should raise sw check exception */ + trace_zicfilp_unaligned_lpad_instr(ctx->base.pc_first); gen_helper_raise_sw_check_excep(tcg_env, tcg_constant_tl(RISCV_EXCP_SW_CHECK_FCFI_TVAL)); return true;
@@ -66,6 +67,8 @@ static bool trans_lpad(DisasContext *ctx, arg_lpad *a) TCGv tmp = tcg_temp_new(); tcg_gen_extract_tl(tmp, get_gpr(ctx, xT2, EXT_NONE), 12, 20); tcg_gen_brcondi_tl(TCG_COND_EQ, tmp, a->label, skip); + gen_helper_zicfilp_label_mismatch(tcg_env, tcg_constant_tl(a->label), + tmp); gen_helper_raise_sw_check_excep(tcg_env, tcg_constant_tl(RISCV_EXCP_SW_CHECK_FCFI_TVAL)); gen_set_label(skip);
target/riscv/insn_trans/trans_rvzicfiss.c.inc +1 -0
@@ -45,6 +45,7 @@ static bool gen_sspopchk(DisasContext *ctx, int rs1_reg) mxl_memop(ctx) | MO_ALIGN); TCGv rs1 = get_gpr(ctx, rs1_reg, EXT_NONE); tcg_gen_brcond_tl(TCG_COND_EQ, data, rs1, skip); + gen_helper_zicfiss_ra_mismatch(tcg_env, data, rs1); gen_helper_raise_sw_check_excep(tcg_env, tcg_constant_tl(RISCV_EXCP_SW_CHECK_BCFI_TVAL)); gen_set_label(skip);
target/riscv/op_helper.c +13 -0
@@ -24,6 +24,7 @@#include "exec/exec-all.h" #include "exec/cpu_ldst.h" #include "exec/helper-proto.h" +#include "trace.h" /* Exceptions processing helpers */ G_NORETURN void riscv_raise_exception(CPURISCVState *env,
@@ -265,6 +266,18 @@ void helper_raise_sw_check_excep(CPURISCVState *env, target_ulong swcheck_code)riscv_raise_exception (env,RISCV_EXCP_SW_CHECK ,GETPC ()); } +void helper_zicfilp_label_mismatch (CPURISCVState *env,target_ulong lpad_label, +target_ulong t2_label) +{ +trace_zicfilp_lpad_reg_mismatch (lpad_label, t2_label); +} + +void helper_zicfiss_ra_mismatch (CPURISCVState *env,target_ulong ssra, +target_ulong rs1) +{ +trace_zicfiss_sspopchk_reg_mismatch (ssra, rs1); +} +#ifndef CONFIG_USER_ONLY target_ulong helper_sret (CPURISCVState *env)
target/riscv/trace-events +6 -0
@@ -9,3 +9,9 @@ pmpaddr_csr_write(uint64_t mhartid, uint32_t addr_index, uint64_t val) "hart %" mseccfg_csr_read(uint64_t mhartid, uint64_t val) "hart %" PRIu64 ": read mseccfg, val: 0x%" PRIx64 mseccfg_csr_write(uint64_t mhartid, uint64_t val) "hart %" PRIu64 ": write mseccfg, val: 0x%" PRIx64 + +# zicfiss/lp +zicfiss_sspopchk_reg_mismatch(uint64_t ssra, uint64_t rs1) "shadow_stack_ra: 0x%" PRIx64 ", rs1: 0x%" PRIx64 +zicfilp_missing_lpad_instr(uint64_t pc_first) "pc_first: 0x%" PRIx64 +zicfilp_unaligned_lpad_instr(uint64_t pc_next) "pc_next: 0x%" PRIx64 +zicfilp_lpad_reg_mismatch(uint64_t lpad_label, uint64_t t2_label) "lpad_label: 0x%" PRIx64 ", t2_label: 0x%" PRIx64
target/riscv/translate.c +2 -0
@@ -35,6 +35,7 @@#undef HELPER_H#include "tcg/tcg-cpu.h" +#include "trace.h" /* global register indices */ static TCGv cpu_gpr[32 ], cpu_gprh[32 ], cpu_pc, cpu_vl, cpu_vstart;
@@ -1348,6 +1349,7 @@ static void riscv_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)*/ tcg_set_insn_param (tcg_ctx->cfi_lp_check,1 ,tcgv_i32_arg (tcg_constant_i32 (1 ))); +trace_zicfilp_missing_lpad_instr (ctx->base.pc_first); } }
Signature · 1 line
2.44.0
PATCH v4 12/16 target/riscv: implement zicfiss instructions
In reply to Deepak Guptazicfiss has following instructions
- sspopchk: pops a value from shadow stack and compares with x1/x5. If they dont match, reports a sw check exception with tval = 3.
- sspush: pushes value in x1/x5 on shadow stack
- ssrdp: reads current shadow stack
- ssamoswap: swaps contents of shadow stack atomically
sspopchk/sspush/ssrdp default to zimop if zimop implemented and SSE=0
If SSE=0, ssamoswap is illegal instruction exception.
This patch implements shadow stack operations for qemu-user and shadow stack is not protected.
--- target/riscv/cpu_bits.h | 2 + target/riscv/insn32.decode | 21 +++- target/riscv/insn_trans/trans_rva.c.inc | 43 +++++++ target/riscv/insn_trans/trans_rvzicfiss.c.inc | 111 ++++++++++++++++++ target/riscv/translate.c | 5 + 5 files changed, 180 insertions(+), 2 deletions(-) create mode 100644 target/riscv/insn_trans/trans_rvzicfiss.c.inc
target/riscv/cpu_bits.h +2 -0
@@ -690,6 +690,8 @@ typedef enum RISCVException {/* zicfilp defines lp violation results in sw check with tval = 2*/ #define RISCV_EXCP_SW_CHECK_FCFI_TVAL 2 +/* zicfiss defines ss violation results in sw check with tval = 3*/ +#define RISCV_EXCP_SW_CHECK_BCFI_TVAL 3#define RISCV_EXCP_INT_FLAG 0x80000000#define RISCV_EXCP_INT_MASK 0x7fffffff
target/riscv/insn32.decode +19 -2
@@ -246,6 +246,7 @@ remud 0000001 ..... ..... 111 ..... 1111011 @r lr_w 00010 . . 00000 ..... 010 ..... 0101111 @atom_ld sc_w 00011 . . ..... ..... 010 ..... 0101111 @atom_st amoswap_w 00001 . . ..... ..... 010 ..... 0101111 @atom_st +ssamoswap_w 01001 . . ..... ..... 010 ..... 0101111 @atom_st amoadd_w 00000 . . ..... ..... 010 ..... 0101111 @atom_st amoxor_w 00100 . . ..... ..... 010 ..... 0101111 @atom_st amoand_w 01100 . . ..... ..... 010 ..... 0101111 @atom_st
@@ -259,6 +260,7 @@ amomaxu_w 11100 . . ..... ..... 010 ..... 0101111 @atom_st lr_d 00010 . . 00000 ..... 011 ..... 0101111 @atom_ld sc_d 00011 . . ..... ..... 011 ..... 0101111 @atom_st amoswap_d 00001 . . ..... ..... 011 ..... 0101111 @atom_st +ssamoswap_d 01001 . . ..... ..... 011 ..... 0101111 @atom_st amoadd_d 00000 . . ..... ..... 011 ..... 0101111 @atom_st amoxor_d 00100 . . ..... ..... 011 ..... 0101111 @atom_st amoand_d 01100 . . ..... ..... 011 ..... 0101111 @atom_st
@@ -1022,8 +1024,23 @@ amocas_d 00101 . . ..... ..... 011 ..... 0101111 @atom_st amocas_q 00101 . . ..... ..... 100 ..... 0101111 @atom_st # *** Zimop may-be-operation extension *** -mop_r_n 1 . 00 .. 0111 .. ..... 100 ..... 1110011 @mop5 -mop_rr_n 1 . 00 .. 1 ..... ..... 100 ..... 1110011 @mop3 +{ + # zicfiss instructions carved out of mop.r + [ + ssrdp 1100110 11100 00000 100 rd:5 1110011 + sspopchk 1100110 11100 00001 100 00000 1110011 rs1=1 + sspopchk 1100110 11100 00101 100 00000 1110011 rs1=5 + ] + mop_r_n 1 . 00 .. 0111 .. ..... 100 ..... 1110011 @mop5 +} +{ + # zicfiss instruction carved out of mop.rr + [ + sspush 1100111 00001 00000 100 00000 1110011 rs2=1 + sspush 1100111 00101 00000 100 00000 1110011 rs2=5 + ] + mop_rr_n 1 . 00 .. 1 ..... ..... 100 ..... 1110011 @mop3 +} # *** Zabhb Standard Extension *** amoswap_b 00001 . . ..... ..... 000 ..... 0101111 @atom_st
target/riscv/insn_trans/trans_rva.c.inc +43 -0
61 lines not on this page · raw message
target/riscv/insn_trans/trans_rvzicfiss.c.inc new file +111 -0
111 lines not on this page · raw message
target/riscv/translate.c +5 -0
@@ -144,6 +144,8 @@ static inline bool has_ext(DisasContext *ctx, uint32_t ext)#define get_address_xl (ctx) ((ctx)->address_xl)#endif +#define mxl_memop (ctx) ((get_xl(ctx) + 1) | MO_TE) +/* The word size for this machine mode. */ static inline int __attribute__((unused))get_xlen (DisasContext *ctx) {
@@ -1126,6 +1128,8 @@ static uint32_t opcode_at(DisasContextBase *dcbase, target_ulong pc)return translator_ldl (env, &ctx->base, pc); } +#define SS_MMU_INDEX (ctx) (ctx->mem_idx | MMU_IDX_SS_WRITE) +/* Include insn module translation function */ #include "insn_trans/trans_rvi.c.inc" #include "insn_trans/trans_rvm.c.inc"
@@ -1156,6 +1160,7 @@ static uint32_t opcode_at(DisasContextBase *dcbase, target_ulong pc)#include "decode-insn16.c.inc" #include "insn_trans/trans_rvzce.c.inc" #include "insn_trans/trans_rvzcmop.c.inc" +#include "insn_trans/trans_rvzicfiss.c.inc" /* Include decoders for factored-out extensions */ #include "decode-XVentanaCondOps.c.inc"
Signature · 1 line
2.44.0
PATCH v4 7/16 disas/riscv: enabled `lpad` disassembly
In reply to Deepak Gupta--- disas/riscv.c | 18 +++++++++++++++++- disas/riscv.h | 2 ++ 2 files changed, 19 insertions(+), 1 deletion(-)
disas/riscv.c +17 -1
@@ -974,6 +974,7 @@ typedef enum { rv_op_amomaxu_h =943 , rv_op_amocas_b =944 , rv_op_amocas_h =945 , + rv_op_lpad =946 , } rv_op;/* register names */
@@ -2232,6 +2233,7 @@ const rv_opcode_data rvi_opcode_data[] = { { "amomaxu.h", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, { "amocas.b", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, { "amocas.h", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, + { "lpad", rv_codec_lp, rv_fmt_imm, NULL, 0, 0, 0 }, };/* CSR names */
@@ -2925,7 +2927,13 @@ static void decode_inst_opcode(rv_decode *dec, rv_isa isa)case 7 : op = rv_op_andi;break ; }break ; -case 5 : op = rv_op_auipc;break ; +case 5 : + op = rv_op_auipc; +if (dec->cfg->ext_zicfilp && + (((inst >>7 ) &0b11111 ) ==0b00000 )) { + op = rv_op_lpad; + } +break ;case 6 :switch ((inst >>12 ) &0b111 ) {case 0 : op = rv_op_addiw;break ;
@@ -4482,6 +4490,11 @@ static uint32_t operand_tbl_index(rv_inst inst)return ((inst <<54 ) >>56 ); } +static uint32_t operand_lpl (rv_inst inst) +{ +return inst >>12 ; +} +/* decode operands */ static void decode_inst_operands (rv_decode *dec,rv_isa isa)
@@ -4869,6 +4882,9 @@ static void decode_inst_operands(rv_decode *dec, rv_isa isa) dec->imm =sextract32 (operand_rs2 (inst),0 ,5 ); dec->imm1 =operand_imm2 (inst);break ; +case rv_codec_lp: + dec->imm =operand_lpl (inst); +break ; }; }
disas/riscv.h +2 -0
@@ -166,6 +166,7 @@ typedef enum { rv_codec_r2_immhl, rv_codec_r2_imm2_imm5, rv_codec_fli, + rv_codec_lp, } rv_codec;/* structures */
@@ -228,6 +229,7 @@ enum {#define rv_fmt_rs1_rs2 "O\t1,2"#define rv_fmt_rd_imm "O\t0,i"#define rv_fmt_rd_uimm "O\t0,Ui" +#define rv_fmt_imm "O\ti"#define rv_fmt_rd_offset "O\t0,o"#define rv_fmt_rd_uoffset "O\t0,Uo"#define rv_fmt_rd_rs1_rs2 "O\t0,1,2"
Signature · 1 line
2.44.0
PATCH v4 15/16 disas/riscv: enable disassembly for compressed sspush/sspopchk
In reply to Deepak Guptasspush and sspopchk have equivalent compressed encoding taken from zcmop. cmop.1 is sspush x1 while cmop.5 is sspopchk x5. Due to unusual encoding for both rs1 and rs2 from space bitfield, this required a new codec.
--- disas/riscv.c | 19 ++++++++++++++++++- disas/riscv.h | 1 + 2 files changed, 19 insertions(+), 1 deletion(-)
disas/riscv.c +18 -1
@@ -980,6 +980,8 @@ typedef enum { rv_op_ssrdp =949 , rv_op_ssamoswap_w =950 , rv_op_ssamoswap_d =951 , + rv_op_c_sspush =952 , + rv_op_c_sspopchk =953 , } rv_op;/* register names */
@@ -2244,6 +2246,10 @@ const rv_opcode_data rvi_opcode_data[] = { { "ssrdp", rv_codec_r, rv_fmt_rd, NULL, 0, 0, 0 }, { "ssamoswap.w", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, { "ssamoswap.d", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, + { "c.sspush", rv_codec_cmop_ss, rv_fmt_rs2, NULL, rv_op_sspush, + rv_op_sspush, 0 }, + { "c.sspopchk", rv_codec_cmop_ss, rv_fmt_rs1, NULL, rv_op_sspopchk, + rv_op_sspopchk, 0 }, };/* CSR names */
@@ -2604,7 +2610,13 @@ static void decode_inst_opcode(rv_decode *dec, rv_isa isa)if (dec->cfg->ext_zcmop) {if ((((inst >>2 ) &0b111111 ) ==0b100000 ) && (((inst >>11 ) &0b11 ) ==0b0 )) { - op = rv_c_mop_1 + ((inst >>8 ) &0b111 ); +unsigned int cmop_code =0 ; + cmop_code = ((inst >>8 ) &0b111 ); + op = rv_c_mop_1 + cmop_code; +if (dec->cfg->ext_zicfiss) { + op = (cmop_code ==0 ) ? rv_op_c_sspush : op; + op = (cmop_code ==2 ) ? rv_op_c_sspopchk : op; + }break ; } }
@@ -4923,6 +4935,11 @@ static void decode_inst_operands(rv_decode *dec, rv_isa isa)case rv_codec_lp: dec->imm =operand_lpl (inst);break ; +case rv_codec_cmop_ss: + dec->rd = rv_ireg_zero; + dec->rs1 = dec->rs2 =operand_crs1 (inst); + dec->imm =0 ; +break ; }; }
disas/riscv.h +1 -0
@@ -167,6 +167,7 @@ typedef enum { rv_codec_r2_imm2_imm5, rv_codec_fli, rv_codec_lp, + rv_codec_cmop_ss, } rv_codec;/* structures */
Signature · 1 line
2.44.0
PATCH v4 11/16 target/riscv: mmu changes for zicfiss shadow stack protection
In reply to Deepak Guptazicfiss protects shadow stack using new page table encodings PTE.W=0, PTE.R=0 and PTE.X=0. This encoding is reserved if zicfiss is not implemented or if shadow stack are not enabled.
Loads on shadow stack memory are allowed while stores to shadow stack memory leads to access faults. Shadow stack accesses to RO memory leads to store page fault.
To implement special nature of shadow stack memory where only selected stores (shadow stack stores from sspush) have to be allowed while rest of regular stores disallowed, new MMU TLB index is created for shadow stack.
--- target/riscv/cpu_helper.c | 52 +++++++++++++++++++++++++++++++++++++-- target/riscv/internals.h | 3 +++ 2 files changed, 53 insertions(+), 2 deletions(-)
target/riscv/cpu_helper.c +50 -2
@@ -817,6 +817,18 @@ void riscv_cpu_set_mode(CPURISCVState *env, target_ulong newpriv) env->load_res =-1 ; } +static bool legal_sstack_access (int access_type,bool sstack_inst, +bool sstack_attribute) +{ +/* +* Read/write/execution permissions are checked as usual. Shadow +* stack enforcement is just that (1) instruction type must match +* the attribute unless (2) a non-SS load to an SS region. +*/ +return (sstack_inst == sstack_attribute) || + ((access_type ==MMU_DATA_LOAD ) && sstack_attribute); +} +/* * get_physical_address_pmp - check PMP permission for this physical address *
@@ -894,6 +906,8 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,hwaddr ppn;int napot_bits =0 ;target_ulong napot_mask; +bool is_sstack_insn = ((mmu_idx &MMU_IDX_SS_WRITE ) ==MMU_IDX_SS_WRITE ); +bool sstack_page = false;/* * Check if we should use the background registers for the two
@@ -1104,13 +1118,34 @@ restart:/* Check for reserved combinations of RWX flags. */ switch (pte & (PTE_R |PTE_W |PTE_X )) { -case PTE_W :case PTE_W |PTE_X :return TRANSLATE_FAIL ; +case PTE_W : +/* if bcfi enabled, PTE_W is not reserved and shadow stack page */ +if (cpu_get_bcfien (env) && first_stage) { + sstack_page = true; +break ; + } +return TRANSLATE_FAIL ; + } + +/* Illegal combo of instruction type and page attribute */ +if (!legal_sstack_access (access_type, is_sstack_insn, + sstack_page)) { +/* shadow stack instruction and RO page then it's a page fault */ +if (is_sstack_insn && ((pte & (PTE_R |PTE_W |PTE_X )) ==PTE_R )) { +return TRANSLATE_FAIL ; + } +/* In all other cases it's an access fault, so raise PMP_FAIL */ +return TRANSLATE_PMP_FAIL ; }int prot =0 ; -if (pte &PTE_R ) { +/* +* If PTE has read bit in it or it's shadow stack page, +* then reads allowed +*/ +if ((pte &PTE_R ) || sstack_page) { prot |=PAGE_READ ; }if (pte &PTE_W ) {
@@ -1348,9 +1383,17 @@ void riscv_cpu_do_unaligned_access(CPUState *cs, vaddr addr,break ;case MMU_DATA_LOAD : cs->exception_index =RISCV_EXCP_LOAD_ADDR_MIS ; +/* shadow stack mis aligned accesses are access faults */ +if (mmu_idx &MMU_IDX_SS_WRITE ) { + cs->exception_index =RISCV_EXCP_LOAD_ACCESS_FAULT ; + }break ;case MMU_DATA_STORE : cs->exception_index =RISCV_EXCP_STORE_AMO_ADDR_MIS ; +/* shadow stack mis aligned accesses are access faults */ +if (mmu_idx &MMU_IDX_SS_WRITE ) { + cs->exception_index =RISCV_EXCP_STORE_AMO_ACCESS_FAULT ; + }break ;default :g_assert_not_reached ();
@@ -1406,6 +1449,11 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,qemu_log_mask (CPU_LOG_MMU ,"%s ad %" VADDR_PRIx" rw %d mmu_idx %d\n" , __func__, address, access_type, mmu_idx); +/* If shadow stack instruction initiated this access, treat it as store */ +if (mmu_idx &MMU_IDX_SS_WRITE ) { + access_type =MMU_DATA_STORE ; + } +pmu_tlb_fill_incr_ctr (cpu, access_type);if (two_stage_lookup) {/* Two stage lookup */
target/riscv/internals.h +3 -0
@@ -30,12 +30,15 @@* - U+2STAGE 0b100 * - S+2STAGE 0b101 * - S+SUM+2STAGE 0b110 +* - Shadow stack+U 0b1000 +* - Shadow stack+S 0b1001 */ #define MMUIdx_U 0#define MMUIdx_S 1#define MMUIdx_S_SUM 2#define MMUIdx_M 3#define MMU_2STAGE_BIT (1 << 2) +#define MMU_IDX_SS_WRITE (1 << 3)static inline int mmuidx_priv (int mmu_idx) {
Signature · 1 line
2.44.0
PATCH v4 8/16 target/riscv: Add zicfiss extension
In reply to Deepak Guptazicfiss [1] riscv cpu extension enables backward control flow integrity.
This patch sets up space for zicfiss extension in cpuconfig. And imple- ments dependency on A, zicsr, zimop and zcmop extensions.
[1] - https://github.com/riscv/riscv-cfi
--- target/riscv/cpu.c | 2 ++ target/riscv/cpu_cfg.h | 1 + target/riscv/tcg/tcg-cpu.c | 19 +++++++++++++++++++ 3 files changed, 22 insertions(+)
target/riscv/cpu.c +2 -0
@@ -107,6 +107,7 @@ const RISCVIsaExtData isa_edata_arr[] = {ISA_EXT_DATA_ENTRY (zicclsm,PRIV_VERSION_1_11_0 , has_priv_1_11),ISA_EXT_DATA_ENTRY (ziccrse,PRIV_VERSION_1_11_0 , has_priv_1_11),ISA_EXT_DATA_ENTRY (zicfilp,PRIV_VERSION_1_12_0 , ext_zicfilp), +ISA_EXT_DATA_ENTRY (zicfiss,PRIV_VERSION_1_13_0 , ext_zicfiss),ISA_EXT_DATA_ENTRY (zicond,PRIV_VERSION_1_12_0 , ext_zicond),ISA_EXT_DATA_ENTRY (zicntr,PRIV_VERSION_1_12_0 , ext_zicntr), ISA_EXT_DATA_ENTRY(zicsr, PRIV_VERSION_1_10_0, ext_zicsr),
@@ -1482,6 +1483,7 @@ const RISCVCPUMultiExtConfig riscv_cpu_extensions[] = {MULTI_EXT_CFG_BOOL ("sscofpmf" , ext_sscofpmf, false),MULTI_EXT_CFG_BOOL ("zifencei" , ext_zifencei, true),MULTI_EXT_CFG_BOOL ("zicfilp" , ext_zicfilp, false), +MULTI_EXT_CFG_BOOL ("zicfiss" , ext_zicfiss, false),MULTI_EXT_CFG_BOOL ("zicsr" , ext_zicsr, true),MULTI_EXT_CFG_BOOL ("zihintntl" , ext_zihintntl, true), MULTI_EXT_CFG_BOOL("zihintpause", ext_zihintpause, true),
target/riscv/cpu_cfg.h +1 -0
@@ -68,6 +68,7 @@ struct RISCVCPUConfig {bool ext_zicbop;bool ext_zicboz;bool ext_zicfilp; +bool ext_zicfiss;bool ext_zicond;bool ext_zihintntl;bool ext_zihintpause;
target/riscv/tcg/tcg-cpu.c +19 -0
@@ -618,6 +618,25 @@ void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp) cpu->cfg.ext_zihpm = false; } +if (cpu->cfg.ext_zicfiss) { +if (!cpu->cfg.ext_zicsr) { +error_setg (errp,"zicfiss extension requires zicsr extension" ); +return ; + } +if (!riscv_has_ext (env,RVA )) { +error_setg (errp,"zicfiss extension requires A extension" ); +return ; + } +if (!cpu->cfg.ext_zimop) { +error_setg (errp,"zicfiss extension requires zimop extension" ); +return ; + } +if (cpu->cfg.ext_zca && !cpu->cfg.ext_zcmop) { +error_setg (errp,"zicfiss with zca requires zcmop extension" ); +return ; + } + } +if (!cpu->cfg.ext_zihpm) { cpu->cfg.pmu_mask =0 ; cpu->pmu_avail_ctrs =0 ;
Signature · 1 line
2.44.0
PATCH v4 13/16 target/riscv: compressed encodings for sspush and sspopchk
In reply to Deepak Guptasspush/sspopchk have compressed encodings carved out of zcmops. compressed sspush is designated as c.mop.1 while compressed sspopchk is designated as c.mop.5.
Note that c.sspush x1 exists while c.sspush x5 doesn't. Similarly c.sspopchk x5 exists while c.sspopchk x1 doesn't.
--- target/riscv/insn16.decode | 2 ++ target/riscv/insn_trans/trans_rvzicfiss.c.inc | 12 ++++++++++++ 2 files changed, 14 insertions(+)
target/riscv/insn16.decode +2 -0
@@ -140,6 +140,8 @@ sw 110 ... ... .. ... 00 @cs_w addi 000 . ..... ..... 01 @ci addi 010 . ..... ..... 01 @c_li { + c_sspush 011 0 00001 00000 01 rs2=1 rs1=0 # c.sspush x1 carving out of zcmops + c_sspopchk 011 0 00101 00000 01 rs1=5 rd=0 # c.sspopchk x5 carving out of zcmops c_mop_n 011 0 0 n:3 1 00000 01 illegal 011 0 ----- 00000 01 # c.addi16sp and c.lui, RES nzimm=0 addi 011 . 00010 ..... 01 @c_addi16sp
target/riscv/insn_trans/trans_rvzicfiss.c.inc +12 -0
@@ -109,3 +109,15 @@ static bool trans_sspush(DisasContext *ctx, arg_sspush *a) { return gen_sspush(ctx, a->rs2); } + +static bool trans_c_sspopchk(DisasContext *ctx, arg_c_sspopchk *a) +{ + assert(a->rs1 == 5); + return gen_sspopchk(ctx, a->rs1); +} + +static bool trans_c_sspush(DisasContext *ctx, arg_c_sspush *a) +{ + assert(a->rs2 == 1); + return gen_sspush(ctx, a->rs2); +}
Signature · 1 line
2.44.0
PATCH v4 14/16 disas/riscv: enable disassembly for zicfiss instructions
In reply to Deepak GuptaEnable disassembly for sspush, sspopchk, ssrdp & ssamoswap. Disasembly is only enabled if zimop and zicfiss ext is set to true.
--- disas/riscv.c | 40 +++++++++++++++++++++++++++++++++++++++- disas/riscv.h | 1 + 2 files changed, 40 insertions(+), 1 deletion(-)
disas/riscv.c +39 -1
@@ -975,6 +975,11 @@ typedef enum { rv_op_amocas_b =944 , rv_op_amocas_h =945 , rv_op_lpad =946 , + rv_op_sspush =947 , + rv_op_sspopchk =948 , + rv_op_ssrdp =949 , + rv_op_ssamoswap_w =950 , + rv_op_ssamoswap_d =951 , } rv_op;/* register names */
@@ -2234,6 +2239,11 @@ const rv_opcode_data rvi_opcode_data[] = { { "amocas.b", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, { "amocas.h", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, { "lpad", rv_codec_lp, rv_fmt_imm, NULL, 0, 0, 0 }, + { "sspush", rv_codec_r, rv_fmt_rs2, NULL, 0, 0, 0 }, + { "sspopchk", rv_codec_r, rv_fmt_rs1, NULL, 0, 0, 0 }, + { "ssrdp", rv_codec_r, rv_fmt_rd, NULL, 0, 0, 0 }, + { "ssamoswap.w", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, + { "ssamoswap.d", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, };/* CSR names */
@@ -2251,6 +2261,7 @@ static const char *csr_name(int csrno)case 0x0009 :return "vxsat" ;case 0x000a :return "vxrm" ;case 0x000f :return "vcsr" ; +case 0x0011 :return "ssp" ;case 0x0015 :return "seed" ;case 0x0017 :return "jvt" ;case 0x0040 :return "uscratch" ;
@@ -3077,6 +3088,8 @@ static void decode_inst_opcode(rv_decode *dec, rv_isa isa)case 66 : op = rv_op_amoor_w;break ;case 67 : op = rv_op_amoor_d;break ;case 68 : op = rv_op_amoor_q;break ; +case 74 : op = rv_op_ssamoswap_w;break ; +case 75 : op = rv_op_ssamoswap_d;break ;case 96 : op = rv_op_amoand_b;break ;case 97 : op = rv_op_amoand_h;break ;case 98 : op = rv_op_amoand_w;break ;
@@ -4028,7 +4041,7 @@ static void decode_inst_opcode(rv_decode *dec, rv_isa isa)case 3 : op = rv_op_csrrc;break ;case 4 :if (dec->cfg->ext_zimop) { -int imm_mop5, imm_mop3; +int imm_mop5, imm_mop3, reg_num;if ((extract32 (inst,22 ,10 ) &0b1011001111 ) ==0b1000000111 ) { imm_mop5 = deposit32(deposit32(extract32(inst, 20, 2),
@@ -4036,11 +4049,36 @@ static void decode_inst_opcode(rv_decode *dec, rv_isa isa) extract32(inst, 26, 2)), 4, 1, extract32(inst, 30, 1)); op = rv_mop_r_0 + imm_mop5; +/* if zicfiss enabled and mop5 is shadow stack */ +if (dec->cfg->ext_zicfiss && + ((imm_mop5 &0b11100 ) ==0b11100 )) { +/* rs1=0 means ssrdp */ +if ((inst & (0b011111 <<15 )) ==0 ) { + op = rv_op_ssrdp; + } +/* rd=0 means sspopchk */ + reg_num = (inst >>15 ) &0b011111 ; +if (((inst & (0b011111 <<7 )) ==0 ) && + ((reg_num ==1 ) || (reg_num ==5 ))) { + op = rv_op_sspopchk; + } + } }else if ((extract32 (inst,25 ,7 ) &0b1011001 ) ==0b1000001 ) { imm_mop3 =deposit32 (extract32 (inst,26 ,2 ),2 ,1 ,extract32 (inst,30 ,1 )); op = rv_mop_rr_0 + imm_mop3; +/* if zicfiss enabled and mop3 is shadow stack */ +if (dec->cfg->ext_zicfiss && + ((imm_mop3 &0b111 ) ==0b111 )) { +/* rs1=0 and rd=0 means sspush */ + reg_num = (inst >>20 ) &0b011111 ; +if (((inst & (0b011111 <<15 )) ==0 ) && + ((inst & (0b011111 <<7 )) ==0 ) && + ((reg_num ==1 ) || (reg_num ==5 ))) { + op = rv_op_sspush; + } + } } }break ;
disas/riscv.h +1 -0
@@ -224,6 +224,7 @@ enum {#define rv_fmt_none "O\t"#define rv_fmt_rs1 "O\t1" +#define rv_fmt_rs2 "O\t2"#define rv_fmt_offset "O\to"#define rv_fmt_pred_succ "O\tp,s"#define rv_fmt_rs1_rs2 "O\t1,2"
Signature · 1 line
2.44.0
Re: [PATCH v4 02/16] target/riscv: Introduce elp state and enabling controls for zicfilp
In reply to Deepak GuptaQuoted 14 lines · Deepak Gupta
zicfilp introduces a new state elp ("expected landing pad") in cpu. During normal execution, elp is idle (
NO_LP_EXPECTED) i.e not expecting landing pad. On an indirect call, elp movesLP_EXPECTED. When elp isLP_EXPECTED, only a subsquent landing pad instruction can set state back toNO_LP_EXPECTED. On reset, elp is set toNO_LP_EXPECTED.zicfilp is enabled via bit2 in *envcfg CSRs. Enabling control for M-mode is in mseccfg CSR at bit position 10.
On trap, elp state is saved away in *status.
Signed-off-by Deepak Gupta<[email protected]>Co-developed-by Jim Shu<[email protected]>Co-developed-by Andy Chiu<[email protected]>
Quoted 8 lines
--- target/riscv/cpu.c | 3 +++ target/riscv/cpu.h | 2 ++ target/riscv/cpu_bits.h | 6 ++++++ target/riscv/csr.c | 31 +++++++++++++++++++++++++++++++ target/riscv/pmp.c | 5 +++++ target/riscv/pmp.h | 3 ++- 6 files changed, 49 insertions(+), 1 deletion(-)
r~
Re: [PATCH v4 03/16] target/riscv: save and restore elp state on priv transitions
In reply to Deepak Guptatarget/riscv/cpu_helper.c void riscv_cpu_swap_hypervisor_regs(CPURISCVState *env) · line 586
Quoted 13 lines · Deepak Gupta
target/riscv/cpu_helper.c +9
-0@@ -546,6 +575,15 @@ void riscv_cpu_swap_hypervisor_regs(CPURISCVState *env) }bool current_virt = env->virt_enabled; +/* +* If zicfilp extension available and henvcfg.LPE = 1, +* then apply SPELP mask on mstatus +*/ +if (env_archcpu (env)->cfg.ext_zicfilp && +get_field (env->henvcfg,HENVCFG_LPE )) { + mstatus_mask |=SSTATUS_SPELP ; + } +
I think this hunk belongs with the previous patch.
Otherwise.
r~
Re: [PATCH v4 05/16] target/riscv: tracking indirect branches (fcfi) for zicfilp
In reply to Deepak Guptatarget/riscv/translate.c static void riscv_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs) · line 1254
Quoted 6 lines · Deepak Gupta
target/riscv/translate.c +1
-0@@ -1245,6 +1250,7 @@ static void riscv_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs)static void riscv_tr_tb_start (DisasContextBase *db,CPUState *cpu) { + }
Watch the unrelated changes.
Quoted 16 lines
target/riscv/translate.c +12
-0@@ -1266,6 +1272,28 @@ static void riscv_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)CPURISCVState *env =cpu_env (cpu);uint16_t opcode16 =translator_lduw (env, &ctx->base, ctx->base.pc_next); +if (ctx->fcfi_lp_expected) { +/* +* Since we can't look ahead to confirm that the first +* instruction is a legal landing pad instruction, emit +* compare-and-branch sequence that will be fixed-up in +* riscv_tr_tb_stop() to either statically hit or skip an +* illegal instruction exception depending on whether the +* flag was lowered by translation of a CJLP or JLP as +* the first instruction in the block. +*/ +TCGv_i32 immediate; +TCGLabel *l;
target/riscv/translate.c static void riscv_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu) · line 1296
Quoted 10 lines
target/riscv/translate.c +10
-0+ l =gen_new_label (); + immediate =tcg_temp_new_i32 (); +tcg_gen_movi_i32 (immediate,0 ); + tcg_ctx->cfi_lp_check =tcg_last_op (); +tcg_gen_brcondi_i32 (TCG_COND_EQ , immediate,0 , l); +gen_helper_raise_sw_check_excep (tcg_env, +tcg_constant_tl (RISCV_EXCP_SW_CHECK_FCFI_TVAL )); +gen_set_label (l); + } +
I think this is over-complicated.
target/riscv/translate.c static void riscv_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu) · line 1299
Quoted 3 lines
target/riscv/translate.c +0
-0ctx->ol = ctx->xl;decode_opc (env, ctx, opcode16); ctx->base.pc_next += ctx->cur_insn_len;
If we delay the check until here, then
(1) we've decoded the opcode, and processed lpad or not.
(2) we can know that lpad will have cleared ctx->fcfi_lp_expected, so that if it is still set here, then we didn't see an lpad.
We can go back an insert the exception like so:
if (ctx->fcfi_lp_expected) {
/* Emit after insn_start, i.e. before the op following insn_start. */
tcg_ctx->emit_before_op = QTAILQ_NEXT(ctx->base.insn_start, link);
tcg_gen_st_tl(tcg_constant_tl(RISCV_EXCP_SW_CHECK_FCFI_TVAL),
tcg_env, offsetof(CPURISCVState, sw_check_code));
gen_helper_raise_exception(tcg_env, tcg_constant_i32(RISCV_EXCP_SW_CHECK));
tcg_ctx->emit_before_op = NULL;
ctx->base.is_jmp = DISAS_NORETURN;
}Emit the store to sw_check_code directly; no need for an extra helper. Using gen_helper_raise_exception instead of generate_exception means we don't get a spurious pc update.
r~
Re: [PATCH v4 06/16] target/riscv: zicfilp `lpad` impl and branch tracking
In reply to Deepak GuptaQuoted 32 lines · Deepak Gupta
Implements setting lp expected when
jalris encountered and implementslpadinstruction of zicfilp.lpadinstruction is taken out of auipc x0, <imm_20>. This is an existing HINTNOP space. Iflpadis target of an indirect branch, cpu checks for 20 bit value in x7 upper with 20 bit value embedded inlpad. If they don't match, cpu raises a sw check exception with tval = 2.Signed-off-by Deepak Gupta <[email protected]>Co-developed-by Jim Shu <[email protected]>Co-developed-by Andy Chiu <[email protected]>--- target/riscv/cpu_user.h | 1 + target/riscv/insn32.decode | 5 ++- target/riscv/insn_trans/trans_rvi.c.inc | 53 +++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-)target/riscv/cpu_user.h +1
-0@@ -15,5 +15,6 @@#define xA6 16#define xA7 17/* syscall number for RVI ABI */ #define xT0 5/* syscall number for RVE ABI */ +#define xT2 7#endif target/riscv/insn32.decode +0
-0
target/riscv/insn32.decode sfence_vm 0001000 00100 ..... 000 00000 1110011 @sfence_vm · line 128
Quoted 7 lines
target/riscv/insn32.decode +3
-1@@ -123,7 +123,10 @@ sfence_vm 0001000 00100 ..... 000 00000 1110011 @sfence_vm # *** RV32I Base Instruction Set *** lui .................... ..... 0110111 @u -auipc .................... ..... 0010111 @u +{ + lpad label:20 00000 0010111 + auipc .................... ..... 0010111 @u
It's often helpful to align the fields.
lpad label:20 00000 0010111 auipc .................... ..... 0010111 @u
Quoted 16 lines
target/riscv/insn32.decode +16
-0+static bool trans_lpad(DisasContext *ctx, arg_lpad *a) +{ + bool lp_expected = ctx->fcfi_lp_expected; + /* + * If zicfilp not present, the encoding is a nop. + * If forward cfi is not enabled, implementation is a nop. + */ + if (!ctx->fcfi_enabled) { + return true; + } + + if (ctx->base.pc_next == ctx->base.pc_first) { + ctx->fcfi_lp_expected = false; + /* If landing pad was expected, PC must be 4 byte aligned */ + if (lp_expected && ((ctx->base.pc_next) & 0x3)) { + /*
target/riscv/insn32.decode sfence_vm 0001000 00100 ..... 000 00000 1110011 @sfence_vm
Quoted 10 lines
target/riscv/insn32.decode +10
-0+ * misaligned, according to spec we should raise sw check exception + */ + gen_helper_raise_sw_check_excep(tcg_env, + tcg_constant_tl(RISCV_EXCP_SW_CHECK_FCFI_TVAL)); + return true; + } + } + + /* if lp was expected and lpad_label non-zero, do label check */ + if (lp_expected && (a->label != 0)) {
This is over-complicated.
(1) lp_expected is true if and only if fcfi_enabled is true.
(2) if lp_expected is false, the implementation is a nop.
Therefore, just begin with
if (!ctx->fcfi_lp_expected) {
return true;
}and drop all other references to lp_expected.
(3) lp_expected is true if and only if ctx->base.pc_next == ctx->base.pc_first. Beyond the first instruction of the TranslationBlock, trans_lpad itself will clear lp_expected. It cannot be set again except by ending a TranslationBlock.
target/riscv/insn32.decode
Quoted 2 lines
target/riscv/insn32.decode +2
-0+ tcg_gen_st_tl(tcg_constant_tl(0), tcg_env, + offsetof(CPURISCVState, elp));
Incorrect: this stores to target_(u)long, whereas elp is a bool. You need tcg_gen_st8_i32.
target/riscv/insn32.decode
Quoted 9 lines
target/riscv/insn32.decode +9
-0+ if (ctx->fcfi_enabled) { + /* + * return from functions (i.e. rs1 == xRA || rs1 == xT0) are not + * tracked. zicfilp introduces sw guarded branch as well. sw guarded + * branch are not tracked. rs1 == xT2 is a sw guarded branch. + */ + if (a->rs1 != xRA && a->rs1 != xT0 && a->rs1 != xT2) { + tcg_gen_st_tl(tcg_constant_tl(1), + tcg_env, offsetof(CPURISCVState, elp));
Likewise.
You may wish to add a utility function to set elp.
r~
Re: [PATCH v4 07/16] disas/riscv: enabled `lpad` disassembly
In reply to Deepak GuptaQuoted 4 lines · Deepak Gupta
Signed-off-by Deepak Gupta <[email protected]>Co-developed-by Jim Shu <[email protected]>Co-developed-by Andy Chiu <[email protected]>Reviewed-by Richard Henderson <[email protected]>
typo in subject: s/enabled/enable/
r~
Re: [PATCH v4 13/16] target/riscv: compressed encodings for sspush and sspopchk
In reply to Deepak GuptaQuoted 38 lines · Deepak Gupta
sspush/sspopchk have compressed encodings carved out of zcmops. compressed sspush is designated as c.mop.1 while compressed sspopchk is designated as c.mop.5.
Note that c.sspush x1 exists while c.sspush x5 doesn't. Similarly c.sspopchk x5 exists while c.sspopchk x1 doesn't.
Signed-off-by Deepak Gupta <[email protected]>Co-developed-by Jim Shu <[email protected]>Co-developed-by Andy Chiu <[email protected]>--- target/riscv/insn16.decode | 2 ++ target/riscv/insn_trans/trans_rvzicfiss.c.inc | 12 ++++++++++++ 2 files changed, 14 insertions(+)target/riscv/insn16.decode +2
-0@@ -140,6 +140,8 @@ sw 110 ... ... .. ... 00 @cs_w addi 000 . ..... ..... 01 @ci addi 010 . ..... ..... 01 @c_li { + c_sspush 011 0 00001 00000 01 rs2=1 rs1=0 # c.sspush x1 carving out of zcmops + c_sspopchk 011 0 00101 00000 01 rs1=5 rd=0 # c.sspopchk x5 carving out of zcmops c_mop_n 011 0 0 n:3 1 00000 01 illegal 011 0 ----- 00000 01 # c.addi16sp and c.lui, RES nzimm=0 addi 011 . 00010 ..... 01 @c_addi16sptarget/riscv/insn_trans/trans_rvzicfiss.c.inc +2
-0@@ -109,3 +109,15 @@ static bool trans_sspush(DisasContext *ctx, arg_sspush *a) { return gen_sspush(ctx, a->rs2); } + +static bool trans_c_sspopchk(DisasContext *ctx, arg_c_sspopchk *a)
target/riscv/insn_trans/trans_rvzicfiss.c.inc static bool trans_sspush(DisasContext *ctx, arg_sspush *a) · line 123
Quoted 10 lines
target/riscv/insn_trans/trans_rvzicfiss.c.inc +10
-0+{ + assert(a->rs1 == 5); + return gen_sspopchk(ctx, a->rs1); +} + +static bool trans_c_sspush(DisasContext *ctx, arg_c_sspush *a) +{ + assert(a->rs2 == 1); + return gen_sspush(ctx, a->rs2); +}
This indirection is pointless. Have the decoder invoke the proper insn in the first place. Identically with how we're treating 'addi', for instance.
r~
Re: [PATCH v4 11/16] target/riscv: mmu changes for zicfiss shadow stack protection
In reply to Deepak GuptaQuoted 27 lines · Deepak Gupta
zicfiss protects shadow stack using new page table encodings PTE.W=0, PTE.R=0 and PTE.X=0. This encoding is reserved if zicfiss is not implemented or if shadow stack are not enabled.
Loads on shadow stack memory are allowed while stores to shadow stack memory leads to access faults. Shadow stack accesses to RO memory leads to store page fault.To implement special nature of shadow stack memory where only selected stores (shadow stack stores from sspush) have to be allowed while rest of regular stores disallowed, new MMU TLB index is created for shadow stack.
Signed-off-by Deepak Gupta <[email protected]>--- target/riscv/cpu_helper.c | 52 +++++++++++++++++++++++++++++++++++++-- target/riscv/internals.h | 3 +++ 2 files changed, 53 insertions(+), 2 deletions(-)target/riscv/cpu_helper.c +1
-0@@ -817,6 +817,18 @@ void riscv_cpu_set_mode(CPURISCVState *env, target_ulong newpriv) env->load_res =-1 ; } +static bool legal_sstack_access(int access_type, bool sstack_inst,
target/riscv/cpu_helper.c void riscv_cpu_set_mode(CPURISCVState *env, target_ulong newpriv) · line 830
Quoted 10 lines
target/riscv/cpu_helper.c +10
-0+ bool sstack_attribute) +{ +/* +* Read/write/execution permissions are checked as usual. Shadow +* stack enforcement is just that (1) instruction type must match +* the attribute unless (2) a non-SS load to an SS region. +*/ +return (sstack_inst == sstack_attribute) || + ((access_type ==MMU_DATA_LOAD ) && sstack_attribute); +}
While helpers are usually helpful, the invocation and logic of this one isn't.
target/riscv/cpu_helper.c static int get_physical_address(CPURISCVState *env, hwaddr *physical, · line 909
Quoted 5 lines
target/riscv/cpu_helper.c +1
-0@@ -894,6 +906,8 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,hwaddr ppn;int napot_bits =0 ;target_ulong napot_mask; +bool is_sstack_insn = ((mmu_idx &MMU_IDX_SS_WRITE ) ==MMU_IDX_SS_WRITE );
is_sstack_idx is a better name, because we are testing the idx.
The fact that this idx is intended to be used only by sstack insns is beside the point.
target/riscv/cpu_helper.c restart: · line 1120
Quoted 8 lines
target/riscv/cpu_helper.c +1
-0+bool sstack_page = false;/* * Check if we should use the background registers for the two @@ -1104,13 +1118,34 @@ restart:/* Check for reserved combinations of RWX flags. */ switch (pte & (PTE_R |PTE_W |PTE_X )) {
Perhaps
rwx = ptw & ...;
switch (rwx)target/riscv/cpu_helper.c restart: · line 1126
Quoted 7 lines
target/riscv/cpu_helper.c +4
-1-case PTE_W :case PTE_W |PTE_X :return TRANSLATE_FAIL ; +case PTE_W : +/* if bcfi enabled, PTE_W is not reserved and shadow stack page */ +if (cpu_get_bcfien (env) && first_stage) { + sstack_page = true;
Extra space.
Perhaps, at this point,
/* Shadow stack pages are globally readable,
but only writable from the sstack idx. */
rwx = is_sstack_idx ? RWX_R | RWX_W : RWX_R;target/riscv/cpu_helper.c restart: · line 1140
Quoted 9 lines
target/riscv/cpu_helper.c +9
-0+/* Illegal combo of instruction type and page attribute */ +if (!legal_sstack_access (access_type, is_sstack_insn, + sstack_page)) { +/* shadow stack instruction and RO page then it's a page fault */ +if (is_sstack_insn && ((pte & (PTE_R |PTE_W |PTE_X )) ==PTE_R )) { +return TRANSLATE_FAIL ; + } +/* In all other cases it's an access fault, so raise PMP_FAIL */ +return TRANSLATE_PMP_FAIL ;
Continuing the above switch,
case PTE_R:
/* Shadow stack writes to read-only pages are page faults. */
if (is_sstack_idx && access_type == MMU_DATA_STORE) {
return TRANSLATE_FAIL;
}
break;target/riscv/cpu_helper.c restart: · line 1148
Quoted 6 lines
target/riscv/cpu_helper.c +5
-1-if (pte &PTE_R ) { +/* +* If PTE has read bit in it or it's shadow stack page, +* then reads allowed +*/ +if ((pte &PTE_R ) || sstack_page) {
Using rwx, no longer need to special case sstack_page.
The second half of leval_sstack_access can be handled by adjusting
if (!((prot >> access_type) & 1)) {
/* Access check failed */
return sstack_page ? TRANSLATE_PMP_FAIL : TRANSLATE_FAIL;
}I don't think you need or want to move this ahead of the user/supervisor check. I see "U and SUM bit enforcement is performed normally", which makes sense. Differing behavior for user/supervisor could allow a timing attack against kernel pages to identify where the kernel shadow stack is located. (Not an observation on qemu, but an observation on the architecture spec.)
r~
Re: [PATCH v4 12/16] target/riscv: implement zicfiss instructions
In reply to Deepak Guptatarget/riscv/insn_trans/trans_rva.c.inc static bool trans_amoswap_w(DisasContext *ctx, arg_amoswap_w *a) · line 125
Quoted 7 lines · Deepak Gupta
target/riscv/insn_trans/trans_rva.c.inc +7
-0+static bool trans_ssamoswap_w(DisasContext *ctx, arg_amoswap_w *a) +{ + REQUIRE_A_OR_ZAAMO(ctx); + /* back cfi was not enabled, return false */ + if (!ctx->bcfi_enabled) { + return false; + }
Please drop all of these comments which exactly mirror the code and provide no additional insight.
target/riscv/insn_trans/trans_rvzicfiss.c.inc line 30
Quoted 13 lines
target/riscv/insn_trans/trans_rvzicfiss.c.inc new file +13
-0+static bool gen_sspopchk(DisasContext *ctx, int rs1_reg) +{ + /* back cfi was not enabled, return false */ + if (!ctx->bcfi_enabled) { + return false; + } + + /* + * sspopchk can only compare with x1 or x5. Everything else defaults to + * zimops + */ + + assert(rs1_reg == 1 || rs1_reg == 5);
These asserts seems redundant. Yes, this is true and a result of the encoding, but nothing in this function would misbehave if some future extension allowed any other register number.
r~
Re: [PATCH v4 16/16] target/riscv: add trace-hooks for each case of sw-check exception
In reply to Deepak GuptaQuoted 34 lines · Deepak Gupta
Violations to control flow rules setup by zicfilp and zicfiss lead to software check exceptions. To debug and fix such sw check issues in guest , add trace-hooks for each case.
Signed-off-by Jim Shu <[email protected]>Signed-off-by Deepak Gupta <[email protected]>--- target/riscv/helper.h | 3 +++ target/riscv/insn_trans/trans_rvi.c.inc | 3 +++ target/riscv/insn_trans/trans_rvzicfiss.c.inc | 1 + target/riscv/op_helper.c | 13 +++++++++++++ target/riscv/trace-events | 6 ++++++ target/riscv/translate.c | 2 ++ 6 files changed, 28 insertions(+)target/riscv/helper.h +3
-0@@ -123,6 +123,9 @@ DEF_HELPER_2(cbo_zero, void, env, tl)/* helper to raise sw check exception */ DEF_HELPER_2 (raise_sw_check_excep, void, env, tl) +/* helper functions to trace riscv cfi violations */ +DEF_HELPER_3(zicfilp_label_mismatch, void, env, tl, tl) +DEF_HELPER_3 (zicfiss_ra_mismatch, void, env, tl, tl) /* Special functions */DEF_HELPER_2 (csrr, tl, env, int)target/riscv/insn_trans/trans_rvi.c.inc +0
-0
target/riscv/insn_trans/trans_rvi.c.inc static bool trans_lpad(DisasContext *ctx, arg_lpad *a) · line 59
Quoted 6 lines
target/riscv/insn_trans/trans_rvi.c.inc +1
-0@@ -54,6 +54,7 @@ static bool trans_lpad(DisasContext *ctx, arg_lpad *a) /* * misaligned, according to spec we should raise sw check exception */ + trace_zicfilp_unaligned_lpad_instr(ctx->base.pc_first); gen_helper_raise_sw_check_excep(tcg_env, tcg_constant_tl(RISCV_EXCP_SW_CHECK_FCFI_TVAL));
Ah, no.
This performs the trace at translation time.
You want the trace at execution time.
gen_update_pc(ctx, 0);
gen_helper_zicfilp_unaligned_lpad(tcg_env);
ctx->base.is_jmp = DISAS_NORETURN;void HELPER(zicfilp_unaligned_lpad)(CPURISCVState *env)
{
trace_zicfilp_unaligned_lpad(env->pc);
env->sw_check_code = RISCV_EXCP_SW_CHECK_FCFI_TVAL;
riscv_raise_exception(RISCV_EXCP_SW_CHECK, 0);
}etc.
Nevermind the previous advice vs patch 5 saying you could inline everything; I had forgotten the desire for tracepoints.
You should probably add these helpers and tracepoints as you add the code. Anything else is going to be a bit confusing.
r~
Re: [PATCH v4 03/16] target/riscv: save and restore elp state on priv transitions
In reply to Richard HendersonQuoted 16 lines · Richard Henderson
target/riscv/cpu_helper.c void riscv_cpu_swap_hypervisor_regs(CPURISCVState *env) · line 585
Quoted 12 lines · Deepak Gupta
target/riscv/cpu_helper.c +9
-0@@ -546,6 +575,15 @@ void riscv_cpu_swap_hypervisor_regs(CPURISCVState *env) }bool current_virt = env->virt_enabled; +/* +* If zicfilp extension available and henvcfg.LPE = 1, +* then apply SPELP mask on mstatus +*/ +if (env_archcpu (env)->cfg.ext_zicfilp && +get_field (env->henvcfg,HENVCFG_LPE )) { + mstatus_mask |=SSTATUS_SPELP ; + } +I think this hunk belongs with the previous patch.
I kept it here because this save/restore of vsstatus with effective sstatus during VS/VU <--> HS transition. ELP also needs to get saved/restored during these transitions.
Quoted 5 lines
Otherwise.
Reviewed-by Richard Henderson <[email protected]>r~
Re: [PATCH v4 05/16] target/riscv: tracking indirect branches (fcfi) for zicfilp
In reply to Richard HendersonQuoted 53 lines · Richard Henderson
target/riscv/translate.c static void riscv_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs) · line 1253
Quoted 5 lines · Deepak Gupta
target/riscv/translate.c +1
-0@@ -1245,6 +1250,7 @@ static void riscv_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs)static void riscv_tr_tb_start (DisasContextBase *db,CPUState *cpu) { + }Watch the unrelated changes.
Quoted 15 lines
target/riscv/translate.c +12
-0@@ -1266,6 +1272,28 @@ static void riscv_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)CPURISCVState *env =cpu_env (cpu);uint16_t opcode16 =translator_lduw (env, &ctx->base, ctx->base.pc_next); +if (ctx->fcfi_lp_expected) { +/* +* Since we can't look ahead to confirm that the first +* instruction is a legal landing pad instruction, emit +* compare-and-branch sequence that will be fixed-up in +* riscv_tr_tb_stop() to either statically hit or skip an +* illegal instruction exception depending on whether the +* flag was lowered by translation of a CJLP or JLP as +* the first instruction in the block. +*/ +TCGv_i32 immediate; +TCGLabel *l;target/riscv/translate.c static void riscv_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu) · line 1295
Quoted 10 lines
target/riscv/translate.c +10
-0+ l =gen_new_label (); + immediate =tcg_temp_new_i32 (); +tcg_gen_movi_i32 (immediate,0 ); + tcg_ctx->cfi_lp_check =tcg_last_op (); +tcg_gen_brcondi_i32 (TCG_COND_EQ , immediate,0 , l); +gen_helper_raise_sw_check_excep (tcg_env, +tcg_constant_tl (RISCV_EXCP_SW_CHECK_FCFI_TVAL )); +gen_set_label (l); + } +I think this is over-complicated.
target/riscv/translate.c static void riscv_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu) · line 1299
Quoted 3 lines
target/riscv/translate.c +0
-0ctx->ol = ctx->xl;decode_opc (env, ctx, opcode16); ctx->base.pc_next += ctx->cur_insn_len;If we delay the check until here, then
(1) we've decoded the opcode, and processed lpad or not.
(2) we can know that lpad will have clearedctx->fcfi_lp_expected, so that if it is still set here, then we didn't see an lpad.We can go back an insert the exception like so:
if (ctx->fcfi_lp_expected) { /* Emit after insn_start, i.e. before the op following insn_start. */ tcg_ctx->emit_before_op = QTAILQ_NEXT(ctx->base.insn_start, link);
Quoted 8 lines
tcg_gen_st_tl(tcg_constant_tl(RISCV_EXCP_SW_CHECK_FCFI_TVAL), tcg_env, offsetof(CPURISCVState, sw_check_code)); gen_helper_raise_exception(tcg_env, tcg_constant_i32(RISCV_EXCP_SW_CHECK)); tcg_ctx->emit_before_op = NULL; ctx->base.is_jmp = DISAS_NORETURN;}
Hmm. Yes this reduces complication of check. Let me do that.
Quoted 4 lines
Emit the store to
sw_check_codedirectly; no need for an extra helper. Usinggen_helper_raise_exceptioninstead ofgenerate_exceptionmeans we don't get a spurious pc update.r~
Re: [PATCH v4 13/16] target/riscv: compressed encodings for sspush and sspopchk
In reply to Richard HendersonQuoted 43 lines · Richard Henderson
Quoted 42 lines · Deepak Gupta
sspush/sspopchk have compressed encodings carved out of zcmops. compressed sspush is designated as c.mop.1 while compressed sspopchk is designated as c.mop.5.
Note that c.sspush x1 exists while c.sspush x5 doesn't. Similarly c.sspopchk x5 exists while c.sspopchk x1 doesn't.
Signed-off-by Deepak Gupta <[email protected]>Co-developed-by Jim Shu <[email protected]>Co-developed-by Andy Chiu <[email protected]>--- target/riscv/insn16.decode | 2 ++ target/riscv/insn_trans/trans_rvzicfiss.c.inc | 12 ++++++++++++ 2 files changed, 14 insertions(+)target/riscv/insn16.decode +2
-0@@ -140,6 +140,8 @@ sw 110 ... ... .. ... 00 @cs_w addi 000 . ..... ..... 01 @ci addi 010 . ..... ..... 01 @c_li { + c_sspush 011 0 00001 00000 01 rs2=1 rs1=0 # c.sspush x1 carving out of zcmops + c_sspopchk 011 0 00101 00000 01 rs1=5 rd=0 # c.sspopchk x5 carving out of zcmops c_mop_n 011 0 0 n:3 1 00000 01 illegal 011 0 ----- 00000 01 # c.addi16sp and c.lui, RES nzimm=0 addi 011 . 00010 ..... 01 @c_addi16sptarget/riscv/insn_trans/trans_rvzicfiss.c.inc +6
-0@@ -109,3 +109,15 @@ static bool trans_sspush(DisasContext *ctx, arg_sspush *a) { return gen_sspush(ctx, a->rs2); } + +static bool trans_c_sspopchk(DisasContext *ctx, arg_c_sspopchk *a) +{ + assert(a->rs1 == 5); + return gen_sspopchk(ctx, a->rs1); +}
Quoted 9 lines
target/riscv/insn_trans/trans_rvzicfiss.c.inc static bool trans_sspush(DisasContext *ctx, arg_sspush *a) · line 123
Quoted 6 lines
target/riscv/insn_trans/trans_rvzicfiss.c.inc +6
-0+ +static bool trans_c_sspush(DisasContext *ctx, arg_c_sspush *a) +{ + assert(a->rs2 == 1); + return gen_sspush(ctx, a->rs2); +}This indirection is pointless. Have the decoder invoke the proper insn in the first place. Identically with how we're treating 'addi', for instance.
I was getting compilation error. How to reconcile with arugment sets between insn32.decode and insn16.decode. Earlier I was doing that and didn't need it. But after removing indirection in arguments and using single use format, type for structs instruction arguments ends up conflicting and compiler complains.
Quoted 3 lines
r~
Re: [PATCH v4 16/16] target/riscv: add trace-hooks for each case of sw-check exception
In reply to Richard HendersonQuoted 50 lines · Richard Henderson
Quoted 32 lines · Deepak Gupta
Violations to control flow rules setup by zicfilp and zicfiss lead to software check exceptions. To debug and fix such sw check issues in guest , add trace-hooks for each case.
Signed-off-by Jim Shu <[email protected]>Signed-off-by Deepak Gupta <[email protected]>--- target/riscv/helper.h | 3 +++ target/riscv/insn_trans/trans_rvi.c.inc | 3 +++ target/riscv/insn_trans/trans_rvzicfiss.c.inc | 1 + target/riscv/op_helper.c | 13 +++++++++++++ target/riscv/trace-events | 6 ++++++ target/riscv/translate.c | 2 ++ 6 files changed, 28 insertions(+)target/riscv/helper.h +3
-0@@ -123,6 +123,9 @@ DEF_HELPER_2(cbo_zero, void, env, tl)/* helper to raise sw check exception */ DEF_HELPER_2 (raise_sw_check_excep, void, env, tl) +/* helper functions to trace riscv cfi violations */ +DEF_HELPER_3(zicfilp_label_mismatch, void, env, tl, tl) +DEF_HELPER_3 (zicfiss_ra_mismatch, void, env, tl, tl) /* Special functions */DEF_HELPER_2 (csrr, tl, env, int)target/riscv/insn_trans/trans_rvi.c.inc +0
-0target/riscv/insn_trans/trans_rvi.c.inc static bool trans_lpad(DisasContext *ctx, arg_lpad *a) · line 59
Quoted 6 lines
target/riscv/insn_trans/trans_rvi.c.inc +1
-0@@ -54,6 +54,7 @@ static bool trans_lpad(DisasContext *ctx, arg_lpad *a) /* * misaligned, according to spec we should raise sw check exception */ + trace_zicfilp_unaligned_lpad_instr(ctx->base.pc_first); gen_helper_raise_sw_check_excep(tcg_env, tcg_constant_tl(RISCV_EXCP_SW_CHECK_FCFI_TVAL));Ah, no.
This performs the trace at translation time.
You want the trace at execution time.gen_update_pc(ctx, 0); gen_helper_zicfilp_unaligned_lpad(tcg_env); ctx->base.is_jmp = DISAS_NORETURN;
Quoted 7 lines
void HELPER(zicfilp_unaligned_lpad)(CPURISCVState *env)
{ trace_zicfilp_unaligned_lpad(env->pc); env->sw_check_code = RISCV_EXCP_SW_CHECK_FCFI_TVAL; riscv_raise_exception(RISCV_EXCP_SW_CHECK, 0); }
facepalm on me. sorry.
Quoted 3 lines
etc.
Nevermind the previous advice vs patch 5 saying you could inline everything; I had forgotten the desire for tracepoints.
It helps locate finding control flow violations faster and fix such issues in libc, libraries, and other pieces of software faster.
So desire is basically fixing guest software faster.
Quoted 2 lines
You should probably add these helpers and tracepoints as you add the code. Anything else is going to be a bit confusing.
Or I can just drop this for now for upstreaming purpose. I'll think about it.
Quoted 3 lines
r~
Re: [PATCH v4 13/16] target/riscv: compressed encodings for sspush and sspopchk
In reply to Deepak GuptaQuoted 2 lines · Deepak Gupta
Quoted 2 lines
Quoted 2 lines
+ c_sspush 011 0 00001 00000 01 rs2=1 rs1=0 # c.sspush x1 carving out of zcmops + c_sspopchk 011 0 00101 00000 01 rs1=5 rd=0 # c.sspopchk x5 carving out of zcmops
...
Quoted 7 lines
Quoted 2 lines
This indirection is pointless. Have the decoder invoke the proper insn in the first place. Identically with how we're treating 'addi', for instance.
I was getting compilation error. How to reconcile with arugment sets between insn32.decode and insn16.decode. Earlier I was doing that and didn't need it. But after removing indirection in arguments and using single use format, type for structs instruction arguments ends up conflicting and compiler complains.
Ah, for that you need to use named argument sets. In this case,
sspush .... &r2_s rs2=1 rs1=0
sspopchk .... &r2 rs2=5 rd=0
in both insn32.decode and insn16.decode
r~