mirror of
https://github.com/riscv-software-src/opensbi.git
synced 2026-11-19 18:21:41 +00:00
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>
54 lines
1.6 KiB
C
54 lines
1.6 KiB
C
/*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*
|
|
* Copyright (c) 2020 Western Digital Corporation or its affiliates.
|
|
*
|
|
* Authors:
|
|
* Atish Patra <atish.patra@wdc.com>
|
|
*/
|
|
|
|
#ifndef __SBI_CSR_DETECT__H
|
|
#define __SBI_CSR_DETECT__H
|
|
|
|
#include <sbi/riscv_encoding.h>
|
|
#include <sbi/sbi_hart.h>
|
|
#include <sbi/sbi_trap.h>
|
|
|
|
#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 ret = 0; \
|
|
((struct sbi_trap_info *)(trap))->cause = 0; \
|
|
asm volatile( \
|
|
"add %[ttmp], %[tinfo], zero\n" \
|
|
"csrrw %[mtvec], " STR(CSR_MTVEC) ", %[mtvec]\n" \
|
|
"csrr %[ret], %[csr]\n" \
|
|
"csrw " STR(CSR_MTVEC) ", %[mtvec]" \
|
|
: [mtvec] "+&r"(mtvec), [tinfo] "+&r"(tinfo), \
|
|
[ttmp] "+&r"(ttmp), [ret] "=&r" (ret) \
|
|
: [csr] "i" (csr_num) \
|
|
: "memory"); \
|
|
ret; \
|
|
}) \
|
|
|
|
#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"); \
|
|
((struct sbi_trap_info *)(trap))->cause = 0; \
|
|
asm volatile( \
|
|
"add %[ttmp], %[tinfo], zero\n" \
|
|
"csrrw %[mtvec], " STR(CSR_MTVEC) ", %[mtvec]\n" \
|
|
"csrw %[csr], %[val]\n" \
|
|
"csrw " STR(CSR_MTVEC) ", %[mtvec]" \
|
|
: [mtvec] "+&r"(mtvec), \
|
|
[tinfo] "+&r"(tinfo), [ttmp] "+&r"(ttmp) \
|
|
: [csr] "i" (csr_num), [val] "r" (value) \
|
|
: "memory"); \
|
|
}) \
|
|
|
|
#endif
|