From: Charlie Jenkins <charlie@rivosinc.com>
To: Samuel Holland <samuel.holland@sifive.com>
Cc: linux-riscv@lists.infradead.org,
"Palmer Dabbelt" <palmer@dabbelt.com>,
"Andrew Jones" <ajones@ventanamicro.com>,
"Conor Dooley" <conor@kernel.org>,
linux-kernel@vger.kernel.org, "Deepak Gupta" <debug@rivosinc.com>,
"Albert Ou" <aou@eecs.berkeley.edu>,
"Andy Chiu" <andy.chiu@sifive.com>,
"Clément Léger" <cleger@rivosinc.com>,
"Evan Green" <evan@rivosinc.com>,
"Greentime Hu" <greentime.hu@sifive.com>,
"Guo Ren" <guoren@kernel.org>,
"Leonardo Bras" <leobras@redhat.com>,
"Paul Walmsley" <paul.walmsley@sifive.com>,
"Xiao Wang" <xiao.w.wang@intel.com>
Subject: Re: [PATCH v3 2/3] riscv: Add support for per-thread envcfg CSR values
Date: Thu, 12 Sep 2024 15:56:54 -0700 [thread overview]
Message-ID: <ZuNxtoZqvbzGptyp@ghost> (raw)
In-Reply-To: <20240718004808.2246354-3-samuel.holland@sifive.com>
On Wed, Jul 17, 2024 at 05:47:55PM -0700, Samuel Holland wrote:
> 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@ventanamicro.com>
> Reviewed-by: Deepak Gupta <debug@rivosinc.com>
> Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
Reviewed-by: Charlie Jenkins <charlie@rivosinc.com>
> ---
>
> (no changes since v1)
>
> arch/riscv/include/asm/processor.h | 1 +
> arch/riscv/include/asm/switch_to.h | 8 ++++++++
> arch/riscv/kernel/cpufeature.c | 2 +-
> 3 files changed, 10 insertions(+), 1 deletion(-)
>
> diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h
> index 8702b8721a27..586e4ab701c4 100644
> --- a/arch/riscv/include/asm/processor.h
> +++ b/arch/riscv/include/asm/processor.h
> @@ -124,6 +124,7 @@ struct thread_struct {
> unsigned long s[12]; /* s[0]: frame pointer */
> struct __riscv_d_ext_state fstate;
> unsigned long bad_cause;
> + unsigned long envcfg;
> u32 riscv_v_flags;
> u32 vstate_ctrl;
> struct __riscv_v_ext_state vstate;
> diff --git a/arch/riscv/include/asm/switch_to.h b/arch/riscv/include/asm/switch_to.h
> index 7594df37cc9f..9685cd85e57c 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.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/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c
> index 4efaf12b470e..43fdae953379 100644
> --- a/arch/riscv/kernel/cpufeature.c
> +++ b/arch/riscv/kernel/cpufeature.c
> @@ -878,7 +878,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.envcfg |= ENVCFG_CBZE;
> else if (any_cpu_has_zicboz)
> pr_warn_once("Zicboz disabled as it is unavailable on some harts\n");
> }
> --
> 2.45.1
>
next prev parent reply other threads:[~2024-09-12 22:56 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-18 0:47 [PATCH v3 0/3] riscv: Per-thread envcfg CSR support Samuel Holland
2024-07-18 0:47 ` [PATCH v3 1/3] riscv: Enable cbo.zero only when all harts support Zicboz Samuel Holland
2024-09-12 22:48 ` Charlie Jenkins
2024-07-18 0:47 ` [PATCH v3 2/3] riscv: Add support for per-thread envcfg CSR values Samuel Holland
2024-09-12 22:56 ` Charlie Jenkins [this message]
2024-07-18 0:47 ` [PATCH v3 3/3] riscv: Call riscv_user_isa_enable() only on the boot hart Samuel Holland
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZuNxtoZqvbzGptyp@ghost \
--to=charlie@rivosinc.com \
--cc=ajones@ventanamicro.com \
--cc=andy.chiu@sifive.com \
--cc=aou@eecs.berkeley.edu \
--cc=cleger@rivosinc.com \
--cc=conor@kernel.org \
--cc=debug@rivosinc.com \
--cc=evan@rivosinc.com \
--cc=greentime.hu@sifive.com \
--cc=guoren@kernel.org \
--cc=leobras@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=palmer@dabbelt.com \
--cc=paul.walmsley@sifive.com \
--cc=samuel.holland@sifive.com \
--cc=xiao.w.wang@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox