mirror of
https://github.com/riscv-software-src/opensbi.git
synced 2025-08-24 15:31:22 +01:00
include: sbi: Use array for struct sbi_trap_regs and GET/SET macros
Rather than hand-rolling scaled pointer arithmetic with casts and shifts, let the compiler do so by indexing an array of GPRs, taking advantage of the language's type system to scale based on whatever type the register happens to be. This makes it easier to support CHERI where the registers are capabilities, not plain integers, and so this pointer arithmetic would need to change (and currently REGBYTES is both the size of a register and the size of an integer word upstream). Signed-off-by: Jessica Clarke <jrtc27@jrtc27.com> Reviewed-by: Anup Patel <anup@brainfault.org> Link: https://lore.kernel.org/r/20250709232932.37622-1-jrtc27@jrtc27.com Signed-off-by: Anup Patel <anup@brainfault.org>
This commit is contained in:

committed by
Anup Patel

parent
0b7c2e0d60
commit
ffd3ed976d
@@ -1291,6 +1291,8 @@
|
||||
#define SHIFT_FUNCT3 12
|
||||
|
||||
#define MASK_RS1 0xf8000
|
||||
#define MASK_RS2 0x1f00000
|
||||
#define MASK_RD 0xf80
|
||||
|
||||
#define MASK_CSR 0xfff00000
|
||||
#define SHIFT_CSR 20
|
||||
@@ -1356,28 +1358,17 @@
|
||||
#define SHIFT_RIGHT(x, y) \
|
||||
((y) < 0 ? ((x) << -(y)) : ((x) >> (y)))
|
||||
|
||||
#define REG_MASK \
|
||||
((1 << (5 + LOG_REGBYTES)) - (1 << LOG_REGBYTES))
|
||||
|
||||
#define REG_OFFSET(insn, pos) \
|
||||
(SHIFT_RIGHT((insn), (pos) - LOG_REGBYTES) & REG_MASK)
|
||||
|
||||
#define REG_PTR(insn, pos, regs) \
|
||||
(ulong *)((ulong)(regs) + REG_OFFSET(insn, pos))
|
||||
|
||||
#define GET_FUNC3(insn) ((insn & MASK_FUNCT3) >> SHIFT_FUNCT3)
|
||||
#define GET_RM(insn) GET_FUNC3(insn)
|
||||
#define GET_RS1_NUM(insn) ((insn & MASK_RS1) >> 15)
|
||||
#define GET_RS1_NUM(insn) ((insn & MASK_RS1) >> SH_RS1)
|
||||
#define GET_RS2_NUM(insn) ((insn & MASK_RS2) >> SH_RS2)
|
||||
#define GET_RS1S_NUM(insn) RVC_RS1S(insn)
|
||||
#define GET_RS2S_NUM(insn) RVC_RS2S(insn)
|
||||
#define GET_RS2C_NUM(insn) RVC_RS2(insn)
|
||||
#define GET_RD_NUM(insn) ((insn & MASK_RD) >> SH_RD)
|
||||
#define GET_CSR_NUM(insn) ((insn & MASK_CSR) >> SHIFT_CSR)
|
||||
#define GET_AQRL(insn) ((insn & MASK_AQRL) >> SHIFT_AQRL)
|
||||
|
||||
#define GET_RS1(insn, regs) (*REG_PTR(insn, SH_RS1, regs))
|
||||
#define GET_RS2(insn, regs) (*REG_PTR(insn, SH_RS2, regs))
|
||||
#define GET_RS1S(insn, regs) (*REG_PTR(RVC_RS1S(insn), 0, regs))
|
||||
#define GET_RS2S(insn, regs) (*REG_PTR(RVC_RS2S(insn), 0, regs))
|
||||
#define GET_RS2C(insn, regs) (*REG_PTR(insn, SH_RS2C, regs))
|
||||
#define GET_SP(regs) (*REG_PTR(2, 0, regs))
|
||||
#define SET_RD(insn, regs, val) (*REG_PTR(insn, SH_RD, regs) = (val))
|
||||
#define IMM_I(insn) ((s32)(insn) >> 20)
|
||||
#define IMM_S(insn) (((s32)(insn) >> 25 << 5) | \
|
||||
(s32)(((insn) >> 7) & 0x1f))
|
||||
|
@@ -127,6 +127,9 @@
|
||||
|
||||
/** Representation of register state at time of trap/interrupt */
|
||||
struct sbi_trap_regs {
|
||||
union {
|
||||
unsigned long gprs[32];
|
||||
struct {
|
||||
/** zero register state */
|
||||
unsigned long zero;
|
||||
/** ra register state */
|
||||
@@ -191,6 +194,8 @@ struct sbi_trap_regs {
|
||||
unsigned long t5;
|
||||
/** t6 register state */
|
||||
unsigned long t6;
|
||||
};
|
||||
};
|
||||
/** mepc register state */
|
||||
unsigned long mepc;
|
||||
/** mstatus register state */
|
||||
@@ -199,6 +204,21 @@ struct sbi_trap_regs {
|
||||
unsigned long mstatusH;
|
||||
};
|
||||
|
||||
_Static_assert(
|
||||
sizeof(((struct sbi_trap_regs *)0)->gprs) ==
|
||||
offsetof(struct sbi_trap_regs, t6) +
|
||||
sizeof(((struct sbi_trap_regs *)0)->t6),
|
||||
"struct sbi_trap_regs's layout differs between gprs and named members");
|
||||
|
||||
#define REG_VAL(idx, regs) ((regs)->gprs[(idx)])
|
||||
|
||||
#define GET_RS1(insn, regs) REG_VAL(GET_RS1_NUM(insn), regs)
|
||||
#define GET_RS2(insn, regs) REG_VAL(GET_RS2_NUM(insn), regs)
|
||||
#define GET_RS1S(insn, regs) REG_VAL(GET_RS1S_NUM(insn), regs)
|
||||
#define GET_RS2S(insn, regs) REG_VAL(GET_RS2S_NUM(insn), regs)
|
||||
#define GET_RS2C(insn, regs) REG_VAL(GET_RS2C_NUM(insn), regs)
|
||||
#define SET_RD(insn, regs, val) (REG_VAL(GET_RD_NUM(insn), regs) = (val))
|
||||
|
||||
/** Representation of trap details */
|
||||
struct sbi_trap_info {
|
||||
/** cause Trap exception cause */
|
||||
|
Reference in New Issue
Block a user