lib: sbi: factorize previous virtualization mode read from regs

The same pattern is used at multiple places to verify in which mode
the exception was actually taken. Factorize it.

Signed-off-by: Clément Léger <cleger@rivosinc.com>
Reviewed-by: Samuel Holland <samuel.holland@sifive.com>
This commit is contained in:
Clément Léger
2024-10-18 10:40:02 +02:00
committed by Anup Patel
parent b919daf495
commit daa282573f
3 changed files with 12 additions and 15 deletions

View File

@@ -236,6 +236,15 @@ static inline unsigned long sbi_regs_gva(const struct sbi_trap_regs *regs)
#endif #endif
} }
static inline bool sbi_regs_from_virt(const struct sbi_trap_regs *regs)
{
#if __riscv_xlen == 32
return (regs->mstatusH & MSTATUSH_MPV) ? true : false;
#else
return (regs->mstatus & MSTATUS_MPV) ? true : false;
#endif
}
int sbi_trap_redirect(struct sbi_trap_regs *regs, int sbi_trap_redirect(struct sbi_trap_regs *regs,
const struct sbi_trap_info *trap); const struct sbi_trap_info *trap);

View File

@@ -47,11 +47,7 @@ int sbi_emulate_csr_read(int csr_num, struct sbi_trap_regs *regs,
int ret = 0; int ret = 0;
struct sbi_scratch *scratch = sbi_scratch_thishart_ptr(); struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
ulong prev_mode = (regs->mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT; ulong prev_mode = (regs->mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT;
#if __riscv_xlen == 32 bool virt = sbi_regs_from_virt(regs);
bool virt = (regs->mstatusH & MSTATUSH_MPV) ? true : false;
#else
bool virt = (regs->mstatus & MSTATUS_MPV) ? true : false;
#endif
switch (csr_num) { switch (csr_num) {
case CSR_HTIMEDELTA: case CSR_HTIMEDELTA:
@@ -157,11 +153,7 @@ int sbi_emulate_csr_write(int csr_num, struct sbi_trap_regs *regs,
{ {
int ret = 0; int ret = 0;
ulong prev_mode = (regs->mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT; ulong prev_mode = (regs->mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT;
#if __riscv_xlen == 32 bool virt = sbi_regs_from_virt(regs);
bool virt = (regs->mstatusH & MSTATUSH_MPV) ? true : false;
#else
bool virt = (regs->mstatus & MSTATUS_MPV) ? true : false;
#endif
switch (csr_num) { switch (csr_num) {
case CSR_HTIMEDELTA: case CSR_HTIMEDELTA:

View File

@@ -104,11 +104,7 @@ int sbi_trap_redirect(struct sbi_trap_regs *regs,
{ {
ulong hstatus, vsstatus, prev_mode; ulong hstatus, vsstatus, prev_mode;
bool elp = false; bool elp = false;
#if __riscv_xlen == 32 bool prev_virt = sbi_regs_from_virt(regs);
bool prev_virt = (regs->mstatusH & MSTATUSH_MPV) ? true : false;
#else
bool prev_virt = (regs->mstatus & MSTATUS_MPV) ? true : false;
#endif
/* By default, we redirect to HS-mode */ /* By default, we redirect to HS-mode */
bool next_virt = false; bool next_virt = false;