riscv: fix ptrace and export VLENB
Subsystems (3)
cover riscv: fix ptrace and export VLENB
We add a vlenb field in Vector context and save it with the riscv_vstate_save() macro. It should not cause performance regression as VLENB is a design-time constant and is frequently used by hardware. Also, adding this field into the __sc_riscv_v_state may benifit us on a future compatibility issue becuse a hardware may have writable VLENB.
Adding and saving VLENB have an immediate benifit as it gives ptrace a better view of the Vector extension and makes it possible to reconstruct Vector register files from the dump without doing an additional csr read.
This patchset also sync the number of note types between us and gdb for riscv to solve a conflicting note.
This is not an ABI break given that 6.5 has not been released yet.
The series is tested on a virt QEMU by verifying VLENB is saved in ptrace, coredump, and signal stack.
[1] https://sourceware.org/pipermail/gdb-patches/2023-August/201492.html
Andy Chiu (2):
RISC-V: vector: export VLENB csr in __sc_riscv_v_state
RISC-V: Add ptrace support for vectors
Palmer Dabbelt (1):
RISC-V: Remove ptrace support for vectors
arch/riscv/include/asm/vector.h | 3 ++- arch/riscv/include/uapi/asm/ptrace.h | 1 + include/uapi/linux/elf.h | 3 ++- 3 files changed, 5 insertions(+), 2 deletions(-)
Signature · 7 lines
2.17.1 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
PATCH 1/3 RISC-V: Remove ptrace support for vectors
From: Palmer Dabbelt <[email protected]>
We've found two bugs here: NT_RISCV_VECTOR steps on NT_RISCV_CSR (which is only for embedded), and we don't have vlenb in the core dumps. Given that we've have a pair of bugs croup up as part of the GDB review we've probably got other issues, so let's just cut this for 6.5 and get it right.
--- arch/riscv/kernel/ptrace.c | 69 -------------------------------------- include/uapi/linux/elf.h | 1 - 2 files changed, 70 deletions(-)
arch/riscv/kernel/ptrace.c +0 -69
@@ -25,9 +25,6 @@ enum riscv_regset {#ifdef CONFIG_FPU REGSET_F,#endif -#ifdef CONFIG_RISCV_ISA_V - REGSET_V, -#endif }; static int riscv_gpr_get(struct task_struct *target,
@@ -84,61 +81,6 @@ static int riscv_fpr_set(struct task_struct *target, }#endif -#ifdef CONFIG_RISCV_ISA_V -static int riscv_vr_get (struct task_struct *target, -const struct user_regset *regset, -struct membuf to) -{ -struct __riscv_v_ext_state *vstate = &target->thread.vstate; - -if (!riscv_v_vstate_query (task_pt_regs (target))) -return -EINVAL ; - -/* -* Ensure the vector registers have been saved to the memory before -* copying them to membuf. -*/ -if (target == current) -riscv_v_vstate_save (current,task_pt_regs (current)); - -/* Copy vector header from vstate. */ -membuf_write (&to, vstate, offsetof(struct __riscv_v_ext_state , datap)); -membuf_zero (&to,sizeof (vstate->datap)); - -/* Copy all the vector registers from vstate. */ -return membuf_write (&to, vstate->datap, riscv_v_vsize); -} - -static int riscv_vr_set (struct task_struct *target, -const struct user_regset *regset, -unsigned int pos,unsigned int count, -const void *kbuf,const void __user *ubuf) -{ -int ret, size; -struct __riscv_v_ext_state *vstate = &target->thread.vstate; - -if (!riscv_v_vstate_query (task_pt_regs (target))) -return -EINVAL ; - -/* Copy rest of the vstate except datap */ - ret =user_regset_copyin (&pos, &count, &kbuf, &ubuf, vstate,0 , - offsetof(struct __riscv_v_ext_state , datap)); -if (unlikely (ret)) -return ret; - -/* Skip copy datap. */ - size =sizeof (vstate->datap); - count -= size; - ubuf += size; - -/* Copy all the vector registers. */ - pos =0 ; - ret =user_regset_copyin (&pos, &count, &kbuf, &ubuf, vstate->datap, -0 , riscv_v_vsize); -return ret; -} -#endif -static const struct user_regset riscv_user_regset[] = { [REGSET_X ] = { .core_note_type =NT_PRSTATUS ,
@@ -158,17 +100,6 @@ static const struct user_regset riscv_user_regset[] = { .set = riscv_fpr_set, },#endif -#ifdef CONFIG_RISCV_ISA_V - [REGSET_V] = { - .core_note_type = NT_RISCV_VECTOR, - .align = 16, - .n = ((32 * RISCV_MAX_VLENB) + - sizeof(struct __riscv_v_ext_state)) / sizeof(__u32), - .size = sizeof(__u32), - .regset_get = riscv_vr_get, - .set = riscv_vr_set, - }, -#endif }; static const struct user_regset_view riscv_user_native_view = {
include/uapi/linux/elf.h +0 -1
@@ -443,7 +443,6 @@ typedef struct elf64_shdr {#define NT_MIPS_DSP 0x800/* MIPS DSP ASE registers */ #define NT_MIPS_FP_MODE 0x801/* MIPS floating-point mode */ #define NT_MIPS_MSA 0x802/* MIPS SIMD registers */ -#define NT_RISCV_VECTOR 0x900/* RISC-V vector registers */ #define NT_LOONGARCH_CPUCFG 0xa00/* LoongArch CPU config registers */ #define NT_LOONGARCH_CSR 0xa01/* LoongArch control and status registers */ #define NT_LOONGARCH_LSX 0xa02/* LoongArch Loongson SIMD Extension registers */
Signature · 7 lines
2.17.1 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
PATCH 2/3 RISC-V: vector: export VLENB csr in __sc_riscv_v_state
In reply to Andy ChiuVLENB is critical for callers of ptrace to reconstruct Vector register files from the register dump of NT_RISCV_VECTOR. Also, future systems may will have a writable VLENB, so add it now to potentially save future compatibility issue.
--- arch/riscv/include/asm/vector.h | 3 ++- arch/riscv/include/uapi/asm/ptrace.h | 1 + 2 files changed, 3 insertions(+), 1 deletion(-)
arch/riscv/include/asm/vector.h +2 -1
@@ -70,8 +70,9 @@ static __always_inline void __vstate_csr_save(struct __riscv_v_ext_state *dest) "csrr %1, " __stringify(CSR_VTYPE) "\n\t" "csrr %2, " __stringify(CSR_VL) "\n\t" "csrr %3, " __stringify(CSR_VCSR) "\n\t" + "csrr %4, " __stringify(CSR_VLENB) "\n\t" : "=r" (dest->vstart), "=r" (dest->vtype), "=r" (dest->vl), - "=r" (dest->vcsr) : :); + "=r" (dest->vcsr), "=r" (dest->vlenb) : :); } static __always_inline void __vstate_csr_restore(struct __riscv_v_ext_state *src)
arch/riscv/include/uapi/asm/ptrace.h +1 -0
@@ -97,6 +97,7 @@ struct __riscv_v_ext_state {unsigned long vl;unsigned long vtype;unsigned long vcsr; +unsigned long vlenb;void *datap;/* * In signal handler, datap will be set a correct user stack offset
Signature · 7 lines
2.17.1 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
Re: [v1, 2/3] RISC-V: vector: export VLENB csr in __sc_riscv_v_state
arch/riscv/include/uapi/asm/ptrace.h struct __riscv_v_ext_state { · line 101
Quoted 10 lines · Andy Chiu
arch/riscv/include/uapi/asm/ptrace.h +1
-0@@ -97,6 +97,7 @@ struct __riscv_v_ext_state {unsigned long vl;unsigned long vtype;unsigned long vcsr; +unsigned long vlenb;void *datap;
I think we really ought to make a distinct structure holding the vector
CSR state only, and then have it included as a leading member of a pair of other structures, one for the signal context with a trailing datap' (or vregp' or vreg') member and another one for the regset with a flexible array member of the char' type, e.g. (actual names TBD):
struct __riscv_v_csr_state {
unsigned long vstart;
unsigned long vl;
unsigned long vtype;
unsigned long vcsr;
unsigned long vlenb;
};struct __riscv_v_signal_state {
struct __riscv_v_csr_state csr;
void *vregp;
};struct __riscv_v_regset_state {
struct __riscv_v_csr_state csr;
char vreg[];
};This will make the API cleaner and avoid both UB with making accesses beyond the end of a structure and clutter with an unused entry in core files and data exchanged via ptrace(2).
Since this is a part of the UAPI I suggest consulting with libc people,
possibly by posting an RFC to <[email protected]>.
Maciej
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Re: [v1, 2/3] RISC-V: vector: export VLENB csr in __sc_riscv_v_state
Hi,
Quoted 33 lines · Maciej W. Rozycki
arch/riscv/include/uapi/asm/ptrace.h struct __riscv_v_ext_state { · line 101
Quoted 10 lines · Andy Chiu
arch/riscv/include/uapi/asm/ptrace.h +1
-0@@ -97,6 +97,7 @@ struct __riscv_v_ext_state {unsigned long vl;unsigned long vtype;unsigned long vcsr; +unsigned long vlenb;void *datap;I think we really ought to make a distinct structure holding the vector
CSR state only, and then have it included as a leading member of a pair of other structures, one for the signal context with a trailingdatap' (orvregp' orvreg') member and another one for the regset with a flexible array member of thechar' type, e.g. (actual names TBD):struct __riscv_v_csr_state { unsigned long vstart; unsigned long vl; unsigned long vtype; unsigned long vcsr; unsigned long vlenb; };struct __riscv_v_signal_state { struct __riscv_v_csr_state csr; void *vregp; };
Quoted 8 lines
struct __riscv_v_regset_state { struct __riscv_v_csr_state csr; char vreg[]; };This will make the API cleaner and avoid both UB with making accesses beyond the end of a structure and clutter with an unused entry in core files and data exchanged via ptrace(2).
Yes, and may I understand why there is a need for having struct __riscv_v_csr_state? Unless there is a need for getting CSRs only, yet vector CSRs are not meaningful without the content of Vector registers. Personally I'd like to have one universal structure for both ptrace/signal/context-swicth(internal to the kernel), or one for UAPI and the other for kernel internal-used. Because then we don't have to mess with all kinds of access helpers for similar things. Maybe I lost something or just haven't read enough but doesn't it sound confusing that we create two structures in UAPI just for the Vector registers dump?
Quoted 5 lines
Since this is a part of the UAPI I suggest consulting with libc people,
possibly by posting an RFC to <[email protected]>.Maciej
Thanks,
Andy
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Re: [v1, 2/3] RISC-V: vector: export VLENB csr in __sc_riscv_v_state
Quoted 35 lines · Andy Chiu
Quoted 35 lines
arch/riscv/include/uapi/asm/ptrace.h struct __riscv_v_ext_state { · line 101
Quoted 10 lines
arch/riscv/include/uapi/asm/ptrace.h +1
-0@@ -97,6 +97,7 @@ struct __riscv_v_ext_state {unsigned long vl;unsigned long vtype;unsigned long vcsr; +unsigned long vlenb;void *datap;I think we really ought to make a distinct structure holding the vector
CSR state only, and then have it included as a leading member of a pair of other structures, one for the signal context with a trailingdatap' (orvregp' orvreg') member and another one for the regset with a flexible array member of thechar' type, e.g. (actual names TBD):struct __riscv_v_csr_state { unsigned long vstart; unsigned long vl; unsigned long vtype; unsigned long vcsr; unsigned long vlenb; };struct __riscv_v_signal_state { struct __riscv_v_csr_state csr; void *vregp; };struct __riscv_v_regset_state { struct __riscv_v_csr_state csr; char vreg[]; };
Quoted 8 lines
Quoted 3 lines
This will make the API cleaner and avoid both UB with making accesses beyond the end of a structure and clutter with an unused entry in core files and data exchanged via ptrace(2).
Yes, and may I understand why there is a need for having struct
__riscv_v_csr_state? Unless there is a need for getting CSRs only, yet vector CSRs are not meaningful without the content of Vector registers.
Well, it's a data type only, it doesn't *have* to be used on it's own
just because it exists.
Quoted 4 lines
Personally I'd like to have one universal structure for both ptrace/signal/context-swicth(internal to the kernel), or one for UAPI and the other for kernel internal-used. Because then we don't have to mess with all kinds of access helpers for similar things.
I'm not sure what kind of access helpers you mean, please elaborate.
Quoted 3 lines
Maybe I lost something or just haven't read enough but doesn't it sound confusing that we create two structures in UAPI just for the Vector registers dump?
AFAICT we need two structures, one for the signal context and another for
the debug stuff, because we represent the vector context differently in each of these two cases. I proposed the embedded `__riscv_v_csr_state' structure as a named member, because C doesn't have syntax available for embedding an already defined structure as an anonymous member and I didn't want to make use of a macro (which would then become a part of the uAPI) as means for the same data definition not to be repeated.
Maybe it's not a big deal though. If we inlined the CSR context in both
structures, then the definitions could look like:
struct __riscv_v_signal_state {
unsigned long vstart;
unsigned long vl;
unsigned long vtype;
unsigned long vcsr;
unsigned long vlenb;
void *vregp;
};struct __riscv_v_regset_state {
unsigned long vstart;
unsigned long vl;
unsigned long vtype;
unsigned long vcsr;
unsigned long vlenb;
char vreg[];
};OTOH I'm not fully convinced this is actually cleaner. And the CSR state is distinct in a way here.
NB I'm only concerned about the user API and ABI here, because once we've
set them they'll have been cast in stone. Conversely we can change an internal representation of the vector context at any time, so if we make a mistake or change our minds for whatever reason, it is not going to be a big deal.
Cc-ing LKML in case someone not subscribed to linux-riscv wanted to chime
in. It's always a good idea to cc LKML on patch submissions anyway.
Maciej
PATCH 3/3 RISC-V: Add ptrace support for vectors
In reply to Andy ChiuThis patch add back the ptrace support with the following fix:
- Define NT_RISCV_CSR and re-number NT_RISCV_VECTOR to prevent conflicting with gdb's NT_RISCV_CSR.
Since gdb does not directly include the note description header in Linux and has already defined NT_RISCV_CSR as 0x900, we decide to sync with gdb and renumber NT_RISCV_VECTOR to solve and prevent future conflicts.
---
Hey Palmer, it is possible to merge this into the [1/3] patch so it looks prettier. Or, please tell me which one would you prefer if a respin is needed, thanks!
arch/riscv/kernel/ptrace.c | 69 ++++++++++++++++++++++++++++++++++++++ include/uapi/linux/elf.h | 2 ++ 2 files changed, 71 insertions(+)
arch/riscv/kernel/ptrace.c +69 -0
@@ -25,6 +25,9 @@ enum riscv_regset {#ifdef CONFIG_FPU REGSET_F,#endif +#ifdef CONFIG_RISCV_ISA_V + REGSET_V, +#endif }; static int riscv_gpr_get(struct task_struct *target,
@@ -81,6 +84,61 @@ static int riscv_fpr_set(struct task_struct *target, }#endif +#ifdef CONFIG_RISCV_ISA_V +static int riscv_vr_get (struct task_struct *target, +const struct user_regset *regset, +struct membuf to) +{ +struct __riscv_v_ext_state *vstate = &target->thread.vstate; + +if (!riscv_v_vstate_query (task_pt_regs (target))) +return -EINVAL ; + +/* +* Ensure the vector registers have been saved to the memory before +* copying them to membuf. +*/ +if (target == current) +riscv_v_vstate_save (current,task_pt_regs (current)); + +/* Copy vector header from vstate. */ +membuf_write (&to, vstate, offsetof(struct __riscv_v_ext_state , datap)); +membuf_zero (&to,sizeof (vstate->datap)); + +/* Copy all the vector registers from vstate. */ +return membuf_write (&to, vstate->datap, riscv_v_vsize); +} + +static int riscv_vr_set (struct task_struct *target, +const struct user_regset *regset, +unsigned int pos,unsigned int count, +const void *kbuf,const void __user *ubuf) +{ +int ret, size; +struct __riscv_v_ext_state *vstate = &target->thread.vstate; + +if (!riscv_v_vstate_query (task_pt_regs (target))) +return -EINVAL ; + +/* Copy rest of the vstate except datap */ + ret =user_regset_copyin (&pos, &count, &kbuf, &ubuf, vstate,0 , + offsetof(struct __riscv_v_ext_state , datap)); +if (unlikely (ret)) +return ret; + +/* Skip copy datap. */ + size =sizeof (vstate->datap); + count -= size; + ubuf += size; + +/* Copy all the vector registers. */ + pos =0 ; + ret =user_regset_copyin (&pos, &count, &kbuf, &ubuf, vstate->datap, +0 , riscv_v_vsize); +return ret; +} +#endif +static const struct user_regset riscv_user_regset[] = { [REGSET_X ] = { .core_note_type =NT_PRSTATUS ,
@@ -100,6 +158,17 @@ static const struct user_regset riscv_user_regset[] = { .set = riscv_fpr_set, },#endif +#ifdef CONFIG_RISCV_ISA_V + [REGSET_V] = { + .core_note_type = NT_RISCV_VECTOR, + .align = 16, + .n = ((32 * RISCV_MAX_VLENB) + + sizeof(struct __riscv_v_ext_state)) / sizeof(__u32), + .size = sizeof(__u32), + .regset_get = riscv_vr_get, + .set = riscv_vr_set, + }, +#endif }; static const struct user_regset_view riscv_user_native_view = {
include/uapi/linux/elf.h +2 -0
@@ -443,6 +443,8 @@ typedef struct elf64_shdr {#define NT_MIPS_DSP 0x800/* MIPS DSP ASE registers */ #define NT_MIPS_FP_MODE 0x801/* MIPS floating-point mode */ #define NT_MIPS_MSA 0x802/* MIPS SIMD registers */ +#define NT_RISCV_CSR 0x900/* RISC-V Control and Status Registers */ +#define NT_RISCV_VECTOR 0x901/* RISC-V vector registers */ #define NT_LOONGARCH_CPUCFG 0xa00/* LoongArch CPU config registers */ #define NT_LOONGARCH_CSR 0xa01/* LoongArch control and status registers */ #define NT_LOONGARCH_LSX 0xa02/* LoongArch Loongson SIMD Extension registers */
Signature · 7 lines
2.17.1 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
Quoted 9 lines · Andy Chiu
We add a vlenb field in Vector context and save it with the
riscv_vstate_save()macro. It should not cause performance regression as VLENB is a design-time constant and is frequently used by hardware. Also, adding this field into the__sc_riscv_v_statemay benifit us on a future compatibility issue becuse a hardware may have writable VLENB.Adding and saving VLENB have an immediate benifit as it gives ptrace a better view of the Vector extension and makes it possible to reconstruct Vector register files from the dump without doing an additional csr read.
I think it is an incorrect view of the situation.
We need to include VLENB not to let a debugger avoid reading VLENB itself
in its own context, but to have a complete view of the debuggee's register file. While the ptrace(2) API has not been formally standardised by a body such as IEEE or The Open Group, our practice across ports has been to provide a complete view of the debuggee via ptrace(2) without a need to resort to examine the machine and OS on the debugger's side via other means.
So we're not improving ptrace(2), but rather we're removing a defect.
Maciej
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
This series was applied to riscv/linux.git (fixes) by Palmer Dabbelt <[email protected]>:
Hello:
This series was applied to riscv/linux.git (fixes) by Palmer Dabbelt <[email protected]>:
Quoted 11 lines · On Wed, 16 Aug 2023 15:54:47 +0000 you wrote:
We add a vlenb field in Vector context and save it with the
riscv_vstate_save()macro. It should not cause performance regression as VLENB is a design-time constant and is frequently used by hardware. Also, adding this field into the__sc_riscv_v_statemay benifit us on a future compatibility issue becuse a hardware may have writable VLENB.Adding and saving VLENB have an immediate benifit as it gives ptrace a better view of the Vector extension and makes it possible to reconstruct Vector register files from the dump without doing an additional csr read.
[...]
Here is the summary with links:
- [v1,1/3] RISC-V: Remove ptrace support for vectors
https://git.kernel.org/riscv/c/e3f9324b231a - [v1,2/3] RISC-V: vector: export VLENB csr in __sc_riscv_v_state https://git.kernel.org/riscv/c/c35f3aa34509
- [v1,3/3] RISC-V: Add ptrace support for vectors (no matching commit)
You are awesome, thank you!
Signature · 9 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
Palmer Dabbelt
AppliedRe: (subset) [v1, 0/3] riscv: fix ptrace and export VLENB
In reply to Andy ChiuQuoted 11 lines · Andy Chiu
We add a vlenb field in Vector context and save it with the
riscv_vstate_save()macro. It should not cause performance regression as VLENB is a design-time constant and is frequently used by hardware. Also, adding this field into the__sc_riscv_v_statemay benifit us on a future compatibility issue becuse a hardware may have writable VLENB.Adding and saving VLENB have an immediate benifit as it gives ptrace a better view of the Vector extension and makes it possible to reconstruct Vector register files from the dump without doing an additional csr read.
[...]
Applied, thanks!
[1/3] RISC-V: Remove ptrace support for vectors
https://git.kernel.org/palmer/c/e3f9324b231a [2/3] RISC-V: vector: export VLENB csr in __sc_riscv_v_state https://git.kernel.org/palmer/c/c35f3aa34509
Best regards,
Signature · 7 lines
Palmer Dabbelt <[email protected]> _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv