File Index
Files and directories
Sign in
FILE

signal.c

File preview with syntax highlighting.

linux-6.19.14/arch/riscv/kernel/signal.c C
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
  4 *  Chen Liqin <liqin.chen@sunplusct.com>
  5 *  Lennox Wu <lennox.wu@sunplusct.com>
  6 * Copyright (C) 2012 Regents of the University of California
  7 */
  8
  9#include <linux/compat.h>
 10#include <linux/signal.h>
 11#include <linux/uaccess.h>
 12#include <linux/syscalls.h>
 13#include <linux/resume_user_mode.h>
 14#include <linux/linkage.h>
 15#include <linux/entry-common.h>
 16
 17#include <asm/ucontext.h>
 18#include <asm/vdso.h>
 19#include <asm/signal.h>
 20#include <asm/signal32.h>
 21#include <asm/switch_to.h>
 22#include <asm/vector.h>
 23#include <asm/csr.h>
 24#include <asm/cacheflush.h>
 25
 26unsigned long signal_minsigstksz __ro_after_init;
 27
 28extern u32 __user_rt_sigreturn[2];
 29static size_t riscv_v_sc_size __ro_after_init;
 30
 31#define DEBUG_SIG 0
 32
 33struct rt_sigframe {
 34	struct siginfo info;
 35	struct ucontext uc;
 36#ifndef CONFIG_MMU
 37	u32 sigreturn_code[2];
 38#endif
 39};
 40
 41#ifdef CONFIG_FPU
 42static long restore_fp_state(struct pt_regs *regs,
 43			     union __riscv_fp_state __user *sc_fpregs)
 44{
 45	long err;
 46	struct __riscv_d_ext_state __user *state = &sc_fpregs->d;
 47
 48	err = __copy_from_user(&current->thread.fstate, state, sizeof(*state));
 49	if (unlikely(err))
 50		return err;
 51
 52	fstate_restore(current, regs);
 53	return 0;
 54}
 55
 56static long save_fp_state(struct pt_regs *regs,
 57			  union __riscv_fp_state __user *sc_fpregs)
 58{
 59	long err;
 60	struct __riscv_d_ext_state __user *state = &sc_fpregs->d;
 61
 62	fstate_save(current, regs);
 63	err = __copy_to_user(state, &current->thread.fstate, sizeof(*state));
 64	return err;
 65}
 66#else
 67#define save_fp_state(task, regs) (0)
 68#define restore_fp_state(task, regs) (0)
 69#endif
 70
 71static long save_v_state(struct pt_regs *regs, void __user *sc_vec)
 72{
 73	struct __sc_riscv_v_state __user *state;
 74	void __user *datap;
 75	long err;
 76
 77	if (!IS_ENABLED(CONFIG_RISCV_ISA_V) ||
 78	    !((has_vector() || has_xtheadvector()) &&
 79	    riscv_v_vstate_query(regs)))
 80		return 0;
 81
 82	/* Place state to the user's signal context space */
 83	state = (struct __sc_riscv_v_state __user *)sc_vec;
 84	/* Point datap right after the end of __sc_riscv_v_state */
 85	datap = state + 1;
 86
 87	/* datap is designed to be 16 byte aligned for better performance */
 88	WARN_ON(!IS_ALIGNED((unsigned long)datap, 16));
 89
 90	get_cpu_vector_context();
 91	riscv_v_vstate_save(&current->thread.vstate, regs);
 92	put_cpu_vector_context();
 93
 94	/* Copy everything of vstate but datap. */
 95	err = __copy_to_user(&state->v_state, &current->thread.vstate,
 96			     offsetof(struct __riscv_v_ext_state, datap));
 97	/* Copy the pointer datap itself. */
 98	err |= __put_user((__force void *)datap, &state->v_state.datap);
 99	/* Copy the whole vector content to user space datap. */
100	err |= __copy_to_user(datap, current->thread.vstate.datap, riscv_v_vsize);
101	if (unlikely(err))
102		return -EFAULT;
103
104	/* Only return the size if everything has done successfully  */
105	return riscv_v_sc_size;
106}
107
108/*
109 * Restore Vector extension context from the user's signal frame. This function
110 * assumes a valid extension header. So magic and size checking must be done by
111 * the caller.
112 */
113static long __restore_v_state(struct pt_regs *regs, void __user *sc_vec)
114{
115	long err;
116	struct __sc_riscv_v_state __user *state = sc_vec;
117	void __user *datap;
118
119	/*
120	 * Mark the vstate as clean prior performing the actual copy,
121	 * to avoid getting the vstate incorrectly clobbered by the
122	 *  discarded vector state.
123	 */
124	riscv_v_vstate_set_restore(current, regs);
125
126	/* Copy everything of __sc_riscv_v_state except datap. */
127	err = __copy_from_user(&current->thread.vstate, &state->v_state,
128			       offsetof(struct __riscv_v_ext_state, datap));
129	if (unlikely(err))
130		return err;
131
132	/* Copy the pointer datap itself. */
133	err = __get_user(datap, &state->v_state.datap);
134	if (unlikely(err))
135		return err;
136	/*
137	 * Copy the whole vector content from user space datap. Use
138	 * copy_from_user to prevent information leak.
139	 */
140	return copy_from_user(current->thread.vstate.datap, datap, riscv_v_vsize);
141}
142
143struct arch_ext_priv {
144	__u32 magic;
145	long (*save)(struct pt_regs *regs, void __user *sc_vec);
146};
147
148static struct arch_ext_priv arch_ext_list[] = {
149	{
150		.magic = RISCV_V_MAGIC,
151		.save = &save_v_state,
152	},
153};
154
155static const size_t nr_arch_exts = ARRAY_SIZE(arch_ext_list);
156
157static long restore_sigcontext(struct pt_regs *regs,
158	struct sigcontext __user *sc)
159{
160	void __user *sc_ext_ptr = &sc->sc_extdesc.hdr;
161	__u32 rsvd;
162	long err;
163	/* sc_regs is structured the same as the start of pt_regs */
164	err = __copy_from_user(regs, &sc->sc_regs, sizeof(sc->sc_regs));
165	if (unlikely(err))
166		return err;
167
168	/* Restore the floating-point state. */
169	if (has_fpu()) {
170		err = restore_fp_state(regs, &sc->sc_fpregs);
171		if (unlikely(err))
172			return err;
173	}
174
175	/* Check the reserved word before extensions parsing */
176	err = __get_user(rsvd, &sc->sc_extdesc.reserved);
177	if (unlikely(err))
178		return err;
179	if (unlikely(rsvd))
180		return -EINVAL;
181
182	while (!err) {
183		__u32 magic, size;
184		struct __riscv_ctx_hdr __user *head = sc_ext_ptr;
185
186		err |= __get_user(magic, &head->magic);
187		err |= __get_user(size, &head->size);
188		if (unlikely(err))
189			return err;
190
191		sc_ext_ptr += sizeof(*head);
192		switch (magic) {
193		case END_MAGIC:
194			if (size != END_HDR_SIZE)
195				return -EINVAL;
196
197			return 0;
198		case RISCV_V_MAGIC:
199			if (!(has_vector() || has_xtheadvector()) || !riscv_v_vstate_query(regs) ||
200			    size != riscv_v_sc_size)
201				return -EINVAL;
202
203			err = __restore_v_state(regs, sc_ext_ptr);
204			break;
205		default:
206			return -EINVAL;
207		}
208		sc_ext_ptr = (void __user *)head + size;
209	}
210	return err;
211}
212
213static size_t get_rt_frame_size(bool cal_all)
214{
215	struct rt_sigframe __user *frame;
216	size_t frame_size;
217	size_t total_context_size = 0;
218
219	frame_size = sizeof(*frame);
220
221	if (has_vector() || has_xtheadvector()) {
222		if (cal_all || riscv_v_vstate_query(task_pt_regs(current)))
223			total_context_size += riscv_v_sc_size;
224	}
225
226	frame_size += total_context_size;
227
228	frame_size = round_up(frame_size, 16);
229	return frame_size;
230}
231
232SYSCALL_DEFINE0(rt_sigreturn)
233{
234	struct pt_regs *regs = current_pt_regs();
235	struct rt_sigframe __user *frame;
236	struct task_struct *task;
237	sigset_t set;
238	size_t frame_size = get_rt_frame_size(false);
239
240	/* Always make any pending restarted system calls return -EINTR */
241	current->restart_block.fn = do_no_restart_syscall;
242
243	frame = (struct rt_sigframe __user *)regs->sp;
244
245	if (!access_ok(frame, frame_size))
246		goto badframe;
247
248	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
249		goto badframe;
250
251	set_current_blocked(&set);
252
253	if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
254		goto badframe;
255
256	if (restore_altstack(&frame->uc.uc_stack))
257		goto badframe;
258
259	regs->cause = -1UL;
260
261	return regs->a0;
262
263badframe:
264	task = current;
265	if (show_unhandled_signals) {
266		pr_info_ratelimited(
267			"%s[%d]: bad frame in %s: frame=%p pc=%p sp=%p\n",
268			task->comm, task_pid_nr(task), __func__,
269			frame, (void *)regs->epc, (void *)regs->sp);
270	}
271	force_sig(SIGSEGV);
272	return 0;
273}
274
275static long setup_sigcontext(struct rt_sigframe __user *frame,
276	struct pt_regs *regs)
277{
278	struct sigcontext __user *sc = &frame->uc.uc_mcontext;
279	struct __riscv_ctx_hdr __user *sc_ext_ptr = &sc->sc_extdesc.hdr;
280	struct arch_ext_priv *arch_ext;
281	long err, i, ext_size;
282
283	/* sc_regs is structured the same as the start of pt_regs */
284	err = __copy_to_user(&sc->sc_regs, regs, sizeof(sc->sc_regs));
285	/* Save the floating-point state. */
286	if (has_fpu())
287		err |= save_fp_state(regs, &sc->sc_fpregs);
288	/* Save the vector state. */
289	for (i = 0; i < nr_arch_exts; i++) {
290		arch_ext = &arch_ext_list[i];
291		if (!arch_ext->save)
292			continue;
293
294		ext_size = arch_ext->save(regs, sc_ext_ptr + 1);
295		if (ext_size <= 0) {
296			err |= ext_size;
297		} else {
298			err |= __put_user(arch_ext->magic, &sc_ext_ptr->magic);
299			err |= __put_user(ext_size, &sc_ext_ptr->size);
300			sc_ext_ptr = (void __user *)sc_ext_ptr + ext_size;
301		}
302	}
303	/* Write zero to fp-reserved space and check it on restore_sigcontext */
304	err |= __put_user(0, &sc->sc_extdesc.reserved);
305	/* And put END __riscv_ctx_hdr at the end. */
306	err |= __put_user(END_MAGIC, &sc_ext_ptr->magic);
307	err |= __put_user(END_HDR_SIZE, &sc_ext_ptr->size);
308
309	return err;
310}
311
312static inline void __user *get_sigframe(struct ksignal *ksig,
313	struct pt_regs *regs, size_t framesize)
314{
315	unsigned long sp;
316	/* Default to using normal stack */
317	sp = regs->sp;
318
319	/*
320	 * If we are on the alternate signal stack and would overflow it, don't.
321	 * Return an always-bogus address instead so we will die with SIGSEGV.
322	 */
323	if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
324		return (void __user __force *)(-1UL);
325
326	/* This is the X/Open sanctioned signal stack switching. */
327	sp = sigsp(sp, ksig) - framesize;
328
329	/* Align the stack frame. */
330	sp &= ~0xfUL;
331
332	return (void __user *)sp;
333}
334
335static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
336	struct pt_regs *regs)
337{
338	struct rt_sigframe __user *frame;
339	long err = 0;
340	unsigned long __maybe_unused addr;
341	size_t frame_size = get_rt_frame_size(false);
342
343	frame = get_sigframe(ksig, regs, frame_size);
344	if (!access_ok(frame, frame_size))
345		return -EFAULT;
346
347	err |= copy_siginfo_to_user(&frame->info, &ksig->info);
348
349	/* Create the ucontext. */
350	err |= __put_user(0, &frame->uc.uc_flags);
351	err |= __put_user(NULL, &frame->uc.uc_link);
352	err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
353	err |= setup_sigcontext(frame, regs);
354	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
355	if (err)
356		return -EFAULT;
357
358	/* Set up to return from userspace. */
359#ifdef CONFIG_MMU
360	regs->ra = (unsigned long)VDSO_SYMBOL(
361		current->mm->context.vdso, rt_sigreturn);
362#else
363	/*
364	 * For the nommu case we don't have a VDSO.  Instead we push two
365	 * instructions to call the rt_sigreturn syscall onto the user stack.
366	 */
367	if (copy_to_user(&frame->sigreturn_code, __user_rt_sigreturn,
368			 sizeof(frame->sigreturn_code)))
369		return -EFAULT;
370
371	addr = (unsigned long)&frame->sigreturn_code;
372	/* Make sure the two instructions are pushed to icache. */
373	flush_icache_range(addr, addr + sizeof(frame->sigreturn_code));
374
375	regs->ra = addr;
376#endif /* CONFIG_MMU */
377
378	/*
379	 * Set up registers for signal handler.
380	 * Registers that we don't modify keep the value they had from
381	 * user-space at the time we took the signal.
382	 * We always pass siginfo and mcontext, regardless of SA_SIGINFO,
383	 * since some things rely on this (e.g. glibc's debug/segfault.c).
384	 */
385	regs->epc = (unsigned long)ksig->ka.sa.sa_handler;
386	regs->sp = (unsigned long)frame;
387	regs->a0 = ksig->sig;                     /* a0: signal number */
388	regs->a1 = (unsigned long)(&frame->info); /* a1: siginfo pointer */
389	regs->a2 = (unsigned long)(&frame->uc);   /* a2: ucontext pointer */
390
391#if DEBUG_SIG
392	pr_info("SIG deliver (%s:%d): sig=%d pc=%p ra=%p sp=%p\n",
393		current->comm, task_pid_nr(current), ksig->sig,
394		(void *)regs->epc, (void *)regs->ra, frame);
395#endif
396
397	return 0;
398}
399
400static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
401{
402	sigset_t *oldset = sigmask_to_save();
403	int ret;
404
405	rseq_signal_deliver(ksig, regs);
406
407	/* Set up the stack frame */
408	if (is_compat_task())
409		ret = compat_setup_rt_frame(ksig, oldset, regs);
410	else
411		ret = setup_rt_frame(ksig, oldset, regs);
412
413	signal_setup_done(ret, ksig, 0);
414}
415
416void arch_do_signal_or_restart(struct pt_regs *regs)
417{
418	unsigned long continue_addr = 0, restart_addr = 0;
419	int retval = 0;
420	struct ksignal ksig;
421	bool syscall = (regs->cause == EXC_SYSCALL);
422
423	/* If we were from a system call, check for system call restarting */
424	if (syscall) {
425		continue_addr = regs->epc;
426		restart_addr = continue_addr - 4;
427		retval = regs->a0;
428
429		/* Avoid additional syscall restarting via ret_from_exception */
430		regs->cause = -1UL;
431
432		/*
433		 * Prepare for system call restart. We do this here so that a
434		 * debugger will see the already changed PC.
435		 */
436		switch (retval) {
437		case -ERESTARTNOHAND:
438		case -ERESTARTSYS:
439		case -ERESTARTNOINTR:
440		case -ERESTART_RESTARTBLOCK:
441			regs->a0 = regs->orig_a0;
442			regs->epc = restart_addr;
443			break;
444		}
445	}
446
447	/*
448	 * Get the signal to deliver. When running under ptrace, at this point
449	 * the debugger may change all of our registers.
450	 */
451	if (get_signal(&ksig)) {
452		/*
453		 * Depending on the signal settings, we may need to revert the
454		 * decision to restart the system call, but skip this if a
455		 * debugger has chosen to restart at a different PC.
456		 */
457		if (regs->epc == restart_addr &&
458		    (retval == -ERESTARTNOHAND ||
459		     retval == -ERESTART_RESTARTBLOCK ||
460		     (retval == -ERESTARTSYS &&
461		      !(ksig.ka.sa.sa_flags & SA_RESTART)))) {
462			regs->a0 = -EINTR;
463			regs->epc = continue_addr;
464		}
465
466		/* Actually deliver the signal */
467		handle_signal(&ksig, regs);
468		return;
469	}
470
471	/*
472	 * Handle restarting a different system call. As above, if a debugger
473	 * has chosen to restart at a different PC, ignore the restart.
474	 */
475	if (syscall && regs->epc == restart_addr && retval == -ERESTART_RESTARTBLOCK)
476		regs->a7 = __NR_restart_syscall;
477
478	/*
479	 * If there is no signal to deliver, we just put the saved
480	 * sigmask back.
481	 */
482	restore_saved_sigmask();
483}
484
485void init_rt_signal_env(void);
486void __init init_rt_signal_env(void)
487{
488	riscv_v_sc_size = sizeof(struct __riscv_ctx_hdr) +
489			  sizeof(struct __sc_riscv_v_state) + riscv_v_vsize;
490	/*
491	 * Determine the stack space required for guaranteed signal delivery.
492	 * The signal_minsigstksz will be populated into the AT_MINSIGSTKSZ entry
493	 * in the auxiliary array at process startup.
494	 */
495	signal_minsigstksz = get_rt_frame_size(true);
496}
497
498#ifdef CONFIG_DYNAMIC_SIGFRAME
499bool sigaltstack_size_valid(size_t ss_size)
500{
501	return ss_size > get_rt_frame_size(false);
502}
503#endif /* CONFIG_DYNAMIC_SIGFRAME */
~/ Read-only file preview.