Minor fixes for domain context switch
v2 cover Minor fixes for domain context switch
Fix illegal instruction caused by non-existent S-mode CSRs and race condition when updating domain assigned_harts.
Yu Chien Peter Lin (2):
sbi: sbi_domain_context: Check privilege spec version before accessing
S-mode CSRs
sbi: sbi_domain_context: Add spinlock for updating domain
assigned_harts
lib/sbi/sbi_domain_context.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
Signature · 1 line
2.34.1
PATCH v2 1/2 sbi: sbi_domain_context: Check privilege spec version before accessing S-mode CSRs
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.
---
Changes v2:
- No change
--- lib/sbi/sbi_domain_context.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
lib/sbi/sbi_domain_context.c +4 -2
@@ -54,8 +54,10 @@ static void switch_to_next_domain_context(struct sbi_context *ctx, ctx->scause =csr_swap (CSR_SCAUSE , dom_ctx->scause); ctx->stval =csr_swap (CSR_STVAL , dom_ctx->stval); 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) -
Signature · 1 line
2.34.1
Anup Patel
AppliedPATCH v2 1/2 sbi: sbi_domain_context: Check privilege spec version before accessing S-mode CSRs
Quoted 7 lines · On Thu, Mar 28, 2024 at 9:03?AM Yu Chien Peter Lin <[email protected]>…
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 <[email protected]>Signed-off-by Alvin Chang <[email protected]>
LGTM.
Applied this patch to the riscv/opensbi repo.
Thanks,
Anup
Quoted 27 lines
---
Changes v2:
- No change--- lib/sbi/sbi_domain_context.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)lib/sbi/sbi_domain_context.c +4
-3@@ -54,8 +54,10 @@ static void switch_to_next_domain_context(struct sbi_context *ctx, ctx->scause =csr_swap (CSR_SCAUSE , dom_ctx->scause); ctx->stval =csr_swap (CSR_STVAL , dom_ctx->stval); 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) - --2.34.1
PATCH v2 2/2 sbi: sbi_domain_context: Add spinlock for updating domain assigned_harts
In reply to Yu Chien Peter LinAdd spinlock protection to avoid race condition on assigned_harts during domain context switching.
---
Changes v2:
- New patch
--- lib/sbi/sbi_domain_context.c | 4 ++++ 1 file changed, 4 insertions(+)
lib/sbi/sbi_domain_context.c +4 -0
@@ -15,6 +15,8 @@#include <sbi/sbi_string.h> #include <sbi/sbi_domain_context.h> +static spinlock_t assigned_harts_lock =SPIN_LOCK_INITIALIZER ; +/** * Switches the HART context from the current domain to the target domain. * This includes changing domain assignments and reconfiguring PMP, as well
@@ -34,10 +36,12 @@ static void switch_to_next_domain_context(struct sbi_context *ctx,/* Assign current hart to target domain */ hartindex =sbi_hartid_to_hartindex (current_hartid ()); +spin_lock (&assigned_harts_lock);sbi_hartmask_clear_hartindex ( hartindex, &sbi_domain_thishart_ptr ()->assigned_harts);sbi_update_hartindex_to_domain (hartindex, dom);sbi_hartmask_set_hartindex (hartindex, &dom->assigned_harts); +spin_unlock (&assigned_harts_lock);/* Reconfigure PMP settings for the new domain */ for (int i =0 ; i < pmp_count; i++) {
Signature · 1 line
2.34.1
PATCH v2 2/2 sbi: sbi_domain_context: Add spinlock for updating domain assigned_harts
Quoted 18 lines · On Thu, Mar 28, 2024 at 9:03?AM Yu Chien Peter Lin <[email protected]>…
Add spinlock protection to avoid race condition on
assigned_hartsduring domain context switching.Signed-off-by Yu Chien Peter Lin <[email protected]>Signed-off-by Alvin Chang <[email protected]>---
Changes v2:
- New patch--- lib/sbi/sbi_domain_context.c | 4 ++++ 1 file changed, 4 insertions(+)lib/sbi/sbi_domain_context.c +0
-0
lib/sbi/sbi_domain_context.c line 19
Quoted 5 lines
lib/sbi/sbi_domain_context.c +2
-0@@ -15,6 +15,8 @@#include <sbi/sbi_string.h> #include <sbi/sbi_domain_context.h> +static spinlock_t assigned_harts_lock =SPIN_LOCK_INITIALIZER ; +
I would rather have assigned_harts_lock in "struct sbi_domain" so that it is not a global lock like this one.
Quoted 17 lines
lib/sbi/sbi_domain_context.c +2
-1/** * Switches the HART context from the current domain to the target domain. * This includes changing domain assignments and reconfiguring PMP, as well @@ -34,10 +36,12 @@ static void switch_to_next_domain_context(struct sbi_context *ctx,/* Assign current hart to target domain */ hartindex =sbi_hartid_to_hartindex (current_hartid ()); +spin_lock (&assigned_harts_lock);sbi_hartmask_clear_hartindex ( hartindex, &sbi_domain_thishart_ptr ()->assigned_harts);sbi_update_hartindex_to_domain (hartindex, dom);sbi_hartmask_set_hartindex (hartindex, &dom->assigned_harts); +spin_unlock (&assigned_harts_lock);/* Reconfigure PMP settings for the new domain */ for (int i =0 ; i < pmp_count; i++) { --
Quoted 2 lines
2.34.1
Regards,
Anup