[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20240912231650.3740732-5-debug@rivosinc.com>
Date: Thu, 12 Sep 2024 16:16:23 -0700
From: Deepak Gupta <debug@...osinc.com>
To: paul.walmsley@...ive.com,
palmer@...ive.com,
conor@...nel.org,
linux-doc@...r.kernel.org,
linux-riscv@...ts.infradead.org,
linux-kernel@...r.kernel.org,
devicetree@...r.kernel.org,
linux-fsdevel@...r.kernel.org,
linux-mm@...ck.org,
linux-arch@...r.kernel.org,
linux-kselftest@...r.kernel.org
Cc: corbet@....net,
palmer@...belt.com,
aou@...s.berkeley.edu,
robh@...nel.org,
krzk+dt@...nel.org,
oleg@...hat.com,
tglx@...utronix.de,
mingo@...hat.com,
bp@...en8.de,
dave.hansen@...ux.intel.com,
x86@...nel.org,
hpa@...or.com,
peterz@...radead.org,
akpm@...ux-foundation.org,
arnd@...db.de,
ebiederm@...ssion.com,
kees@...nel.org,
Liam.Howlett@...cle.com,
vbabka@...e.cz,
lorenzo.stoakes@...cle.com,
shuah@...nel.org,
brauner@...nel.org,
samuel.holland@...ive.com,
debug@...osinc.com,
andy.chiu@...ive.com,
jerry.shih@...ive.com,
greentime.hu@...ive.com,
charlie@...osinc.com,
evan@...osinc.com,
cleger@...osinc.com,
xiao.w.wang@...el.com,
ajones@...tanamicro.com,
anup@...infault.org,
mchitale@...tanamicro.com,
atishp@...osinc.com,
sameo@...osinc.com,
bjorn@...osinc.com,
alexghiti@...osinc.com,
david@...hat.com,
libang.li@...group.com,
jszhang@...nel.org,
leobras@...hat.com,
guoren@...nel.org,
samitolvanen@...gle.com,
songshuaishuai@...ylab.org,
costa.shul@...hat.com,
bhe@...hat.com,
zong.li@...ive.com,
puranjay@...nel.org,
namcaov@...il.com,
antonb@...storrent.com,
sorear@...tmail.com,
quic_bjorande@...cinc.com,
ancientmodern4@...il.com,
ben.dooks@...ethink.co.uk,
quic_zhonhan@...cinc.com,
cuiyunhui@...edance.com,
yang.lee@...ux.alibaba.com,
ke.zhao@...ngroup.cn,
sunilvl@...tanamicro.com,
tanzhasanwork@...il.com,
schwab@...e.de,
dawei.li@...ngroup.cn,
rppt@...nel.org,
willy@...radead.org,
usama.anjum@...labora.com,
osalvador@...e.de,
ryan.roberts@....com,
andrii@...nel.org,
alx@...nel.org,
catalin.marinas@....com,
broonie@...nel.org,
revest@...omium.org,
bgray@...ux.ibm.com,
deller@....de,
zev@...ilderbeest.net
Subject: [PATCH v4 04/30] riscv: Add support for per-thread envcfg CSR values
From: Samuel Holland <samuel.holland@...ive.com>
Some bits in the [ms]envcfg CSR, such as the CFI state and pointer
masking mode, need to be controlled on a per-thread basis. Support this
by keeping a copy of the CSR value in struct thread_struct and writing
it during context switches. It is safe to discard the old CSR value
during the context switch because the CSR is modified only by software,
so the CSR will remain in sync with the copy in thread_struct.
Use ALTERNATIVE directly instead of riscv_has_extension_unlikely() to
minimize branchiness in the context switching code.
Since thread_struct is copied during fork(), setting the value for the
init task sets the default value for all other threads.
Reviewed-by: Andrew Jones <ajones@...tanamicro.com>
Reviewed-by: Deepak Gupta <debug@...osinc.com>
Signed-off-by: Samuel Holland <samuel.holland@...ive.com>
Signed-off-by: Deepak Gupta <debug@...osinc.com>
---
arch/riscv/include/asm/switch_to.h | 8 ++++++++
arch/riscv/include/asm/thread_info.h | 1 +
arch/riscv/kernel/cpufeature.c | 2 +-
3 files changed, 10 insertions(+), 1 deletion(-)
diff --git a/arch/riscv/include/asm/switch_to.h b/arch/riscv/include/asm/switch_to.h
index 7594df37cc9f..dd4a36ff4356 100644
--- a/arch/riscv/include/asm/switch_to.h
+++ b/arch/riscv/include/asm/switch_to.h
@@ -70,6 +70,13 @@ static __always_inline bool has_fpu(void) { return false; }
#define __switch_to_fpu(__prev, __next) do { } while (0)
#endif
+static inline void __switch_to_envcfg(struct task_struct *next)
+{
+ asm volatile (ALTERNATIVE("nop", "csrw " __stringify(CSR_ENVCFG) ", %0",
+ 0, RISCV_ISA_EXT_XLINUXENVCFG, 1)
+ :: "r" (next->thread_info.envcfg) : "memory");
+}
+
extern struct task_struct *__switch_to(struct task_struct *,
struct task_struct *);
@@ -103,6 +110,7 @@ do { \
__switch_to_vector(__prev, __next); \
if (switch_to_should_flush_icache(__next)) \
local_flush_icache_all(); \
+ __switch_to_envcfg(__next); \
((last) = __switch_to(__prev, __next)); \
} while (0)
diff --git a/arch/riscv/include/asm/thread_info.h b/arch/riscv/include/asm/thread_info.h
index fca5c6be2b81..c74536194626 100644
--- a/arch/riscv/include/asm/thread_info.h
+++ b/arch/riscv/include/asm/thread_info.h
@@ -57,6 +57,7 @@ struct thread_info {
long user_sp; /* User stack pointer */
int cpu;
unsigned long syscall_work; /* SYSCALL_WORK_ flags */
+ unsigned long envcfg;
#ifdef CONFIG_SHADOW_CALL_STACK
void *scs_base;
void *scs_sp;
diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c
index 0139d4ea8426..f7fcd23d55de 100644
--- a/arch/riscv/kernel/cpufeature.c
+++ b/arch/riscv/kernel/cpufeature.c
@@ -922,7 +922,7 @@ unsigned long riscv_get_elf_hwcap(void)
void riscv_user_isa_enable(void)
{
if (riscv_has_extension_unlikely(RISCV_ISA_EXT_ZICBOZ))
- csr_set(CSR_ENVCFG, ENVCFG_CBZE);
+ current->thread_info.envcfg |= ENVCFG_CBZE;
else if (any_cpu_has_zicboz)
pr_warn_once("Zicboz disabled as it is unavailable on some harts\n");
}
--
2.45.0
Powered by blists - more mailing lists