sbi: sbi_domain_context: Check privilege spec version before accessing S-mode CSRs
authorYu Chien Peter Lin <peterlin@andestech.com>
Thu, 28 Mar 2024 03:33:02 +0000 (11:33 +0800)
committerAnup Patel <anup@brainfault.org>
Fri, 5 Apr 2024 12:32:29 +0000 (18:02 +0530)
SCOUNTEREN and SENVCFG may not be supported on certain RISC-V core,
so check the existence of these CSRs via privilege spec version to
prevent illegal instructions.

Signed-off-by: Yu Chien Peter Lin <peterlin@andestech.com>
Signed-off-by: Alvin Chang <alvinga@andestech.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
lib/sbi/sbi_domain_context.c

index d6843a6a9060a38753d33afc60595e8603b04da1..8daf629cd5886d2dcb4c2ccf7d7ea11ad7fdf71d 100755 (executable)
@@ -55,8 +55,10 @@ static void switch_to_next_domain_context(struct sbi_context *ctx,
        ctx->stval      = csr_swap(CSR_STVAL, dom_ctx->stval);
        ctx->sip        = csr_swap(CSR_SIP, dom_ctx->sip);
        ctx->satp       = csr_swap(CSR_SATP, dom_ctx->satp);
-       ctx->scounteren = csr_swap(CSR_SCOUNTEREN, dom_ctx->scounteren);
-       ctx->senvcfg    = csr_swap(CSR_SENVCFG, dom_ctx->senvcfg);
+       if (sbi_hart_priv_version(scratch) >= SBI_HART_PRIV_VER_1_10)
+               ctx->scounteren = csr_swap(CSR_SCOUNTEREN, dom_ctx->scounteren);
+       if (sbi_hart_priv_version(scratch) >= SBI_HART_PRIV_VER_1_12)
+               ctx->senvcfg    = csr_swap(CSR_SENVCFG, dom_ctx->senvcfg);
 
        /* Save current trap state and restore target domain's trap state */
        trap_regs = (struct sbi_trap_regs *)(csr_read(CSR_MSCRATCH) -