Skip to content

Commit 8ee0b41

Browse files
greentimepalmer-dabbelt
authored andcommitted
riscv: signal: Add sigcontext save/restore for vector
This patch facilitates the existing fp-reserved words for placement of the first extension's context header on the user's sigframe. A context header consists of a distinct magic word and the size, including the header itself, of an extension on the stack. Then, the frame is followed by the context of that extension, and then a header + context body for another extension if exists. If there is no more extension to come, then the frame must be ended with a null context header. A special case is rv64gc, where the kernel support no extensions requiring to expose additional regfile to the user. In such case the kernel would place the null context header right after the first reserved word of __riscv_q_ext_state when saving sigframe. And the kernel would check if all reserved words are zeros when a signal handler returns. __riscv_q_ext_state---->| |<-__riscv_extra_ext_header ~ ~ .reserved[0]--->|0 |<- .reserved <-------|magic |<- .hdr | |size |_______ end of sc_fpregs | |ext-bdy| | ~ ~ +)size ------->|magic |<- another context header |size | |ext-bdy| ~ ~ |magic:0|<- null context header |size:0 | The vector registers will be saved in datap pointer. The datap pointer will be allocated dynamically when the task needs in kernel space. On the other hand, datap pointer on the sigframe will be set right after the __riscv_v_ext_state data structure. Co-developed-by: Vincent Chen <[email protected]> Signed-off-by: Vincent Chen <[email protected]> Signed-off-by: Greentime Hu <[email protected]> Suggested-by: Vineet Gupta <[email protected]> Suggested-by: Richard Henderson <[email protected]> Co-developed-by: Andy Chiu <[email protected]> Signed-off-by: Andy Chiu <[email protected]> Acked-by: Conor Dooley <[email protected]> Acked-by: Heiko Stuebner <[email protected]> Tested-by: Heiko Stuebner <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent a45ceda commit 8ee0b41

File tree

4 files changed

+193
-15
lines changed

4 files changed

+193
-15
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,21 @@ struct __riscv_q_ext_state {
7171
__u32 reserved[3];
7272
};
7373

74+
struct __riscv_ctx_hdr {
75+
__u32 magic;
76+
__u32 size;
77+
};
78+
79+
struct __riscv_extra_ext_header {
80+
__u32 __padding[129] __attribute__((aligned(16)));
81+
/*
82+
* Reserved for expansion of sigcontext structure. Currently zeroed
83+
* upon signal, and must be zero upon sigreturn.
84+
*/
85+
__u32 reserved;
86+
struct __riscv_ctx_hdr hdr;
87+
};
88+
7489
union __riscv_fp_state {
7590
struct __riscv_f_ext_state f;
7691
struct __riscv_d_ext_state d;

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@
88

99
#include <asm/ptrace.h>
1010

11+
/* The Magic number for signal context frame header. */
12+
#define RISCV_V_MAGIC 0x53465457
13+
#define END_MAGIC 0x0
14+
15+
/* The size of END signal context header. */
16+
#define END_HDR_SIZE 0x0
17+
18+
struct __sc_riscv_v_state {
19+
struct __riscv_v_ext_state v_state;
20+
} __attribute__((aligned(16)));
21+
1122
/*
1223
* Signal context structure
1324
*
@@ -16,7 +27,10 @@
1627
*/
1728
struct sigcontext {
1829
struct user_regs_struct sc_regs;
19-
union __riscv_fp_state sc_fpregs;
30+
union {
31+
union __riscv_fp_state sc_fpregs;
32+
struct __riscv_extra_ext_header sc_extdesc;
33+
};
2034
};
2135

2236
#endif /* _UAPI_ASM_RISCV_SIGCONTEXT_H */

arch/riscv/kernel/setup.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ static void __init parse_dtb(void)
262262
#endif
263263
}
264264

265+
extern void __init init_rt_signal_env(void);
266+
265267
void __init setup_arch(char **cmdline_p)
266268
{
267269
parse_dtb();
@@ -295,6 +297,7 @@ void __init setup_arch(char **cmdline_p)
295297

296298
riscv_init_cbo_blocksizes();
297299
riscv_fill_hwcap();
300+
init_rt_signal_env();
298301
apply_boot_alternatives();
299302
if (IS_ENABLED(CONFIG_RISCV_ISA_ZICBOM) &&
300303
riscv_isa_extension_available(NULL, ZICBOM))

arch/riscv/kernel/signal.c

Lines changed: 160 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
#include <asm/signal.h>
2020
#include <asm/signal32.h>
2121
#include <asm/switch_to.h>
22+
#include <asm/vector.h>
2223
#include <asm/csr.h>
2324
#include <asm/cacheflush.h>
2425

2526
extern u32 __user_rt_sigreturn[2];
27+
static size_t riscv_v_sc_size __ro_after_init;
2628

2729
#define DEBUG_SIG 0
2830

@@ -64,12 +66,87 @@ static long save_fp_state(struct pt_regs *regs,
6466
#define restore_fp_state(task, regs) (0)
6567
#endif
6668

69+
#ifdef CONFIG_RISCV_ISA_V
70+
71+
static long save_v_state(struct pt_regs *regs, void __user **sc_vec)
72+
{
73+
struct __riscv_ctx_hdr __user *hdr;
74+
struct __sc_riscv_v_state __user *state;
75+
void __user *datap;
76+
long err;
77+
78+
hdr = *sc_vec;
79+
/* Place state to the user's signal context space after the hdr */
80+
state = (struct __sc_riscv_v_state __user *)(hdr + 1);
81+
/* Point datap right after the end of __sc_riscv_v_state */
82+
datap = state + 1;
83+
84+
/* datap is designed to be 16 byte aligned for better performance */
85+
WARN_ON(unlikely(!IS_ALIGNED((unsigned long)datap, 16)));
86+
87+
riscv_v_vstate_save(current, regs);
88+
/* Copy everything of vstate but datap. */
89+
err = __copy_to_user(&state->v_state, &current->thread.vstate,
90+
offsetof(struct __riscv_v_ext_state, datap));
91+
/* Copy the pointer datap itself. */
92+
err |= __put_user(datap, &state->v_state.datap);
93+
/* Copy the whole vector content to user space datap. */
94+
err |= __copy_to_user(datap, current->thread.vstate.datap, riscv_v_vsize);
95+
/* Copy magic to the user space after saving all vector conetext */
96+
err |= __put_user(RISCV_V_MAGIC, &hdr->magic);
97+
err |= __put_user(riscv_v_sc_size, &hdr->size);
98+
if (unlikely(err))
99+
return err;
100+
101+
/* Only progress the sv_vec if everything has done successfully */
102+
*sc_vec += riscv_v_sc_size;
103+
return 0;
104+
}
105+
106+
/*
107+
* Restore Vector extension context from the user's signal frame. This function
108+
* assumes a valid extension header. So magic and size checking must be done by
109+
* the caller.
110+
*/
111+
static long __restore_v_state(struct pt_regs *regs, void __user *sc_vec)
112+
{
113+
long err;
114+
struct __sc_riscv_v_state __user *state = sc_vec;
115+
void __user *datap;
116+
117+
/* Copy everything of __sc_riscv_v_state except datap. */
118+
err = __copy_from_user(&current->thread.vstate, &state->v_state,
119+
offsetof(struct __riscv_v_ext_state, datap));
120+
if (unlikely(err))
121+
return err;
122+
123+
/* Copy the pointer datap itself. */
124+
err = __get_user(datap, &state->v_state.datap);
125+
if (unlikely(err))
126+
return err;
127+
/*
128+
* Copy the whole vector content from user space datap. Use
129+
* copy_from_user to prevent information leak.
130+
*/
131+
err = copy_from_user(current->thread.vstate.datap, datap, riscv_v_vsize);
132+
if (unlikely(err))
133+
return err;
134+
135+
riscv_v_vstate_restore(current, regs);
136+
137+
return err;
138+
}
139+
#else
140+
#define save_v_state(task, regs) (0)
141+
#define __restore_v_state(task, regs) (0)
142+
#endif
143+
67144
static long restore_sigcontext(struct pt_regs *regs,
68145
struct sigcontext __user *sc)
69146
{
147+
void __user *sc_ext_ptr = &sc->sc_extdesc.hdr;
148+
__u32 rsvd;
70149
long err;
71-
size_t i;
72-
73150
/* sc_regs is structured the same as the start of pt_regs */
74151
err = __copy_from_user(regs, &sc->sc_regs, sizeof(sc->sc_regs));
75152
if (unlikely(err))
@@ -82,32 +159,81 @@ static long restore_sigcontext(struct pt_regs *regs,
82159
return err;
83160
}
84161

85-
/* We support no other extension state at this time. */
86-
for (i = 0; i < ARRAY_SIZE(sc->sc_fpregs.q.reserved); i++) {
87-
u32 value;
162+
/* Check the reserved word before extensions parsing */
163+
err = __get_user(rsvd, &sc->sc_extdesc.reserved);
164+
if (unlikely(err))
165+
return err;
166+
if (unlikely(rsvd))
167+
return -EINVAL;
168+
169+
while (!err) {
170+
__u32 magic, size;
171+
struct __riscv_ctx_hdr __user *head = sc_ext_ptr;
88172

89-
err = __get_user(value, &sc->sc_fpregs.q.reserved[i]);
173+
err |= __get_user(magic, &head->magic);
174+
err |= __get_user(size, &head->size);
90175
if (unlikely(err))
176+
return err;
177+
178+
sc_ext_ptr += sizeof(*head);
179+
switch (magic) {
180+
case END_MAGIC:
181+
if (size != END_HDR_SIZE)
182+
return -EINVAL;
183+
184+
return 0;
185+
case RISCV_V_MAGIC:
186+
if (!has_vector() || !riscv_v_vstate_query(regs) ||
187+
size != riscv_v_sc_size)
188+
return -EINVAL;
189+
190+
err = __restore_v_state(regs, sc_ext_ptr);
91191
break;
92-
if (value != 0)
192+
default:
93193
return -EINVAL;
194+
}
195+
sc_ext_ptr = (void __user *)head + size;
94196
}
95197
return err;
96198
}
97199

200+
static size_t get_rt_frame_size(void)
201+
{
202+
struct rt_sigframe __user *frame;
203+
size_t frame_size;
204+
size_t total_context_size = 0;
205+
206+
frame_size = sizeof(*frame);
207+
208+
if (has_vector() && riscv_v_vstate_query(task_pt_regs(current)))
209+
total_context_size += riscv_v_sc_size;
210+
/*
211+
* Preserved a __riscv_ctx_hdr for END signal context header if an
212+
* extension uses __riscv_extra_ext_header
213+
*/
214+
if (total_context_size)
215+
total_context_size += sizeof(struct __riscv_ctx_hdr);
216+
217+
frame_size += total_context_size;
218+
219+
frame_size = round_up(frame_size, 16);
220+
return frame_size;
221+
}
222+
98223
SYSCALL_DEFINE0(rt_sigreturn)
99224
{
100225
struct pt_regs *regs = current_pt_regs();
101226
struct rt_sigframe __user *frame;
102227
struct task_struct *task;
103228
sigset_t set;
229+
size_t frame_size = get_rt_frame_size();
104230

105231
/* Always make any pending restarted system calls return -EINTR */
106232
current->restart_block.fn = do_no_restart_syscall;
107233

108234
frame = (struct rt_sigframe __user *)regs->sp;
109235

110-
if (!access_ok(frame, sizeof(*frame)))
236+
if (!access_ok(frame, frame_size))
111237
goto badframe;
112238

113239
if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
@@ -141,17 +267,22 @@ static long setup_sigcontext(struct rt_sigframe __user *frame,
141267
struct pt_regs *regs)
142268
{
143269
struct sigcontext __user *sc = &frame->uc.uc_mcontext;
270+
struct __riscv_ctx_hdr __user *sc_ext_ptr = &sc->sc_extdesc.hdr;
144271
long err;
145-
size_t i;
146272

147273
/* sc_regs is structured the same as the start of pt_regs */
148274
err = __copy_to_user(&sc->sc_regs, regs, sizeof(sc->sc_regs));
149275
/* Save the floating-point state. */
150276
if (has_fpu())
151277
err |= save_fp_state(regs, &sc->sc_fpregs);
152-
/* We support no other extension state at this time. */
153-
for (i = 0; i < ARRAY_SIZE(sc->sc_fpregs.q.reserved); i++)
154-
err |= __put_user(0, &sc->sc_fpregs.q.reserved[i]);
278+
/* Save the vector state. */
279+
if (has_vector() && riscv_v_vstate_query(regs))
280+
err |= save_v_state(regs, (void __user **)&sc_ext_ptr);
281+
/* Write zero to fp-reserved space and check it on restore_sigcontext */
282+
err |= __put_user(0, &sc->sc_extdesc.reserved);
283+
/* And put END __riscv_ctx_hdr at the end. */
284+
err |= __put_user(END_MAGIC, &sc_ext_ptr->magic);
285+
err |= __put_user(END_HDR_SIZE, &sc_ext_ptr->size);
155286

156287
return err;
157288
}
@@ -176,6 +307,13 @@ static inline void __user *get_sigframe(struct ksignal *ksig,
176307
/* Align the stack frame. */
177308
sp &= ~0xfUL;
178309

310+
/*
311+
* Fail if the size of the altstack is not large enough for the
312+
* sigframe construction.
313+
*/
314+
if (current->sas_ss_size && sp < current->sas_ss_sp)
315+
return (void __user __force *)-1UL;
316+
179317
return (void __user *)sp;
180318
}
181319

@@ -185,9 +323,10 @@ static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
185323
struct rt_sigframe __user *frame;
186324
long err = 0;
187325
unsigned long __maybe_unused addr;
326+
size_t frame_size = get_rt_frame_size();
188327

189-
frame = get_sigframe(ksig, regs, sizeof(*frame));
190-
if (!access_ok(frame, sizeof(*frame)))
328+
frame = get_sigframe(ksig, regs, frame_size);
329+
if (!access_ok(frame, frame_size))
191330
return -EFAULT;
192331

193332
err |= copy_siginfo_to_user(&frame->info, &ksig->info);
@@ -320,3 +459,10 @@ void arch_do_signal_or_restart(struct pt_regs *regs)
320459
*/
321460
restore_saved_sigmask();
322461
}
462+
463+
void init_rt_signal_env(void);
464+
void __init init_rt_signal_env(void)
465+
{
466+
riscv_v_sc_size = sizeof(struct __riscv_ctx_hdr) +
467+
sizeof(struct __sc_riscv_v_state) + riscv_v_vsize;
468+
}

0 commit comments

Comments
 (0)