mirror of
https://github.com/riscv-software-src/opensbi.git
synced 2026-11-19 10:21:38 +00:00
lib: sbi: select expected trap handler per hart
The expected trap handler is selected once by the cold boot hart. This
breaks heterogeneous systems where the cold boot hart implements H but
another hart does not, because the H-aware handler accesses mtval2 and
mtinst.
Store the handler in each hart's feature data and select it from that
hart's MISA before feature detection. Load the address without function
calls, avoiding stack-protector instrumentation in the CSR probing and
unprivileged memory access paths.
Early M-mode CSR probes, such as those in the IMSIC nascent hook, can run
before hart feature data is ready. Use the non-H handler until the current
hart's handler has been initialized, and publish the feature-data offset
only after every hart's storage has been cleared. This leaves the fixed
scratch layout and allocator unchanged.
Fixes: 1de66d170e ("lib: Optimize unpriv load/store implementation")
Reported-by: Bo Gan <ganboing@gmail.com>
Link: https://lore.kernel.org/r/e702f291-dde8-4b99-a65e-182d2b847720@gmail.com
Suggested-by: Bo Gan <ganboing@gmail.com>
Cc: Alvin Chang <alvinga@andestech.com>
Link: https://lore.kernel.org/r/20260831022156.2060561-1-alvinga@andestech.com
Signed-off-by: Troy Mitchell <troy.mitchell@linux.spacemit.com>
Tested-by: E Shattow <e@freeshell.de>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20261028-spacemit-k3-v6-1-01d984f60941@linux.spacemit.com
Signed-off-by: Anup Patel <anup@brainfault.org>
This commit is contained in:
1 parent
2bbee7d82a
commit
5dd2a0619e
5 files changed
+38
-19
No files matched your search
@@ -16,9 +16,9 @@
|
||||
|
||||
#define csr_read_allowed(csr_num, trap) \
|
||||
({ \
|
||||
register ulong mtvec = sbi_hart_expected_trap_addr(); \
|
||||
register ulong tinfo asm("a3") = (ulong)trap; \
|
||||
register ulong ttmp asm("a4"); \
|
||||
register ulong mtvec = (ulong)sbi_hart_expected_trap; \
|
||||
register ulong ret = 0; \
|
||||
((struct sbi_trap_info *)(trap))->cause = 0; \
|
||||
asm volatile( \
|
||||
@@ -35,9 +35,9 @@
|
||||
|
||||
#define csr_write_allowed(csr_num, trap, value) \
|
||||
({ \
|
||||
register ulong mtvec = sbi_hart_expected_trap_addr(); \
|
||||
register ulong tinfo asm("a3") = (ulong)trap; \
|
||||
register ulong ttmp asm("a4"); \
|
||||
register ulong mtvec = (ulong)sbi_hart_expected_trap; \
|
||||
((struct sbi_trap_info *)(trap))->cause = 0; \
|
||||
asm volatile( \
|
||||
"add %[ttmp], %[tinfo], zero\n" \
|
||||
|
||||
+22
-1
@@ -10,8 +10,10 @@
|
||||
#ifndef __SBI_HART_H__
|
||||
#define __SBI_HART_H__
|
||||
|
||||
#include <sbi/riscv_barrier.h>
|
||||
#include <sbi/sbi_types.h>
|
||||
#include <sbi/sbi_bitops.h>
|
||||
#include <sbi/sbi_scratch.h>
|
||||
|
||||
/** Possible privileged specification versions of a hart */
|
||||
enum sbi_hart_priv_versions {
|
||||
@@ -125,6 +127,7 @@ struct sbi_hart_features {
|
||||
unsigned int pmp_log2gran;
|
||||
unsigned int mhpm_mask;
|
||||
unsigned int mhpm_bits;
|
||||
unsigned long expected_trap;
|
||||
};
|
||||
|
||||
extern unsigned long hart_features_offset;
|
||||
@@ -135,7 +138,25 @@ struct sbi_scratch;
|
||||
int sbi_hart_reinit(struct sbi_scratch *scratch);
|
||||
int sbi_hart_init(struct sbi_scratch *scratch, bool cold_boot);
|
||||
|
||||
extern void (*sbi_hart_expected_trap)(void);
|
||||
extern void __sbi_expected_trap(void);
|
||||
extern void __sbi_expected_trap_hext(void);
|
||||
|
||||
/*
|
||||
* Keep the load free of function calls, including stack-protector code.
|
||||
* Early CSR probes can use the non-H handler before hart features are ready.
|
||||
*/
|
||||
#define sbi_hart_expected_trap_addr() \
|
||||
({ \
|
||||
unsigned long __offset = __smp_load_acquire(&hart_features_offset); \
|
||||
unsigned long __addr = (unsigned long)&__sbi_expected_trap; \
|
||||
if (__offset) { \
|
||||
struct sbi_hart_features *__features = \
|
||||
sbi_scratch_thishart_offset_ptr(__offset); \
|
||||
if (__features->expected_trap) \
|
||||
__addr = __features->expected_trap; \
|
||||
} \
|
||||
__addr; \
|
||||
})
|
||||
|
||||
unsigned int sbi_hart_mhpm_mask(struct sbi_scratch *scratch);
|
||||
void sbi_hart_delegation_dump(struct sbi_scratch *scratch,
|
||||
|
||||
Reference in new issue
Block a user