Skip to content

Commit 0c59922

Browse files
greentimepalmer-dabbelt
authored andcommitted
riscv: Add ptrace vector support
This patch adds ptrace support for riscv vector. The vector registers will be saved in datap pointer of __riscv_v_ext_state. This pointer will be set right after the __riscv_v_ext_state data structure then it will be put in ubuf for ptrace system call to get or set. It will check if the datap got from ubuf is set to the correct address or not when the ptrace system call is trying to set the vector registers. Co-developed-by: Vincent Chen <[email protected]> Signed-off-by: Vincent Chen <[email protected]> Signed-off-by: Greentime Hu <[email protected]> Signed-off-by: Andy Chiu <[email protected]> Reviewed-by: Conor Dooley <[email protected]> Reviewed-by: Palmer Dabbelt <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent cd05483 commit 0c59922

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

arch/riscv/include/uapi/asm/ptrace.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ struct __riscv_v_ext_state {
9494
*/
9595
};
9696

97+
/*
98+
* According to spec: The number of bits in a single vector register,
99+
* VLEN >= ELEN, which must be a power of 2, and must be no greater than
100+
* 2^16 = 65536bits = 8192bytes
101+
*/
102+
#define RISCV_MAX_VLENB (8192)
103+
97104
#endif /* __ASSEMBLY__ */
98105

99106
#endif /* _UAPI_ASM_RISCV_PTRACE_H */

arch/riscv/kernel/ptrace.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Copied from arch/tile/kernel/ptrace.c
88
*/
99

10+
#include <asm/vector.h>
1011
#include <asm/ptrace.h>
1112
#include <asm/syscall.h>
1213
#include <asm/thread_info.h>
@@ -24,6 +25,9 @@ enum riscv_regset {
2425
#ifdef CONFIG_FPU
2526
REGSET_F,
2627
#endif
28+
#ifdef CONFIG_RISCV_ISA_V
29+
REGSET_V,
30+
#endif
2731
};
2832

2933
static int riscv_gpr_get(struct task_struct *target,
@@ -80,6 +84,61 @@ static int riscv_fpr_set(struct task_struct *target,
8084
}
8185
#endif
8286

87+
#ifdef CONFIG_RISCV_ISA_V
88+
static int riscv_vr_get(struct task_struct *target,
89+
const struct user_regset *regset,
90+
struct membuf to)
91+
{
92+
struct __riscv_v_ext_state *vstate = &target->thread.vstate;
93+
94+
if (!riscv_v_vstate_query(task_pt_regs(target)))
95+
return -EINVAL;
96+
97+
/*
98+
* Ensure the vector registers have been saved to the memory before
99+
* copying them to membuf.
100+
*/
101+
if (target == current)
102+
riscv_v_vstate_save(current, task_pt_regs(current));
103+
104+
/* Copy vector header from vstate. */
105+
membuf_write(&to, vstate, offsetof(struct __riscv_v_ext_state, datap));
106+
membuf_zero(&to, sizeof(vstate->datap));
107+
108+
/* Copy all the vector registers from vstate. */
109+
return membuf_write(&to, vstate->datap, riscv_v_vsize);
110+
}
111+
112+
static int riscv_vr_set(struct task_struct *target,
113+
const struct user_regset *regset,
114+
unsigned int pos, unsigned int count,
115+
const void *kbuf, const void __user *ubuf)
116+
{
117+
int ret, size;
118+
struct __riscv_v_ext_state *vstate = &target->thread.vstate;
119+
120+
if (!riscv_v_vstate_query(task_pt_regs(target)))
121+
return -EINVAL;
122+
123+
/* Copy rest of the vstate except datap */
124+
ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, vstate, 0,
125+
offsetof(struct __riscv_v_ext_state, datap));
126+
if (unlikely(ret))
127+
return ret;
128+
129+
/* Skip copy datap. */
130+
size = sizeof(vstate->datap);
131+
count -= size;
132+
ubuf += size;
133+
134+
/* Copy all the vector registers. */
135+
pos = 0;
136+
ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, vstate->datap,
137+
0, riscv_v_vsize);
138+
return ret;
139+
}
140+
#endif
141+
83142
static const struct user_regset riscv_user_regset[] = {
84143
[REGSET_X] = {
85144
.core_note_type = NT_PRSTATUS,
@@ -99,6 +158,17 @@ static const struct user_regset riscv_user_regset[] = {
99158
.set = riscv_fpr_set,
100159
},
101160
#endif
161+
#ifdef CONFIG_RISCV_ISA_V
162+
[REGSET_V] = {
163+
.core_note_type = NT_RISCV_VECTOR,
164+
.align = 16,
165+
.n = ((32 * RISCV_MAX_VLENB) +
166+
sizeof(struct __riscv_v_ext_state)) / sizeof(__u32),
167+
.size = sizeof(__u32),
168+
.regset_get = riscv_vr_get,
169+
.set = riscv_vr_set,
170+
},
171+
#endif
102172
};
103173

104174
static const struct user_regset_view riscv_user_native_view = {

include/uapi/linux/elf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ typedef struct elf64_shdr {
440440
#define NT_MIPS_DSP 0x800 /* MIPS DSP ASE registers */
441441
#define NT_MIPS_FP_MODE 0x801 /* MIPS floating-point mode */
442442
#define NT_MIPS_MSA 0x802 /* MIPS SIMD registers */
443+
#define NT_RISCV_VECTOR 0x900 /* RISC-V vector registers */
443444
#define NT_LOONGARCH_CPUCFG 0xa00 /* LoongArch CPU config registers */
444445
#define NT_LOONGARCH_CSR 0xa01 /* LoongArch control and status registers */
445446
#define NT_LOONGARCH_LSX 0xa02 /* LoongArch Loongson SIMD Extension registers */

0 commit comments

Comments
 (0)