2 * Based on arch/arm/kernel/signal.c
4 * Copyright (C) 1995-2009 Russell King
5 * Copyright (C) 2012 ARM Ltd.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <linux/compat.h>
21 #include <linux/errno.h>
22 #include <linux/signal.h>
23 #include <linux/personality.h>
24 #include <linux/freezer.h>
25 #include <linux/uaccess.h>
26 #include <linux/tracehook.h>
27 #include <linux/ratelimit.h>
28 #include <linux/syscalls.h>
30 #include <asm/debug-monitors.h>
32 #include <asm/cacheflush.h>
33 #include <asm/ucontext.h>
34 #include <asm/unistd.h>
35 #include <asm/fpsimd.h>
36 #include <asm/signal32.h>
40 * Do a signal return; undo the signal stack. These are aligned to 128-bit.
49 static int preserve_fpsimd_context(struct fpsimd_context __user *ctx)
51 struct fpsimd_state *fpsimd = ¤t->thread.fpsimd_state;
54 /* dump the hardware registers to the fpsimd_state structure */
55 fpsimd_preserve_current_state();
57 /* copy the FP and status/control registers */
58 err = __copy_to_user(ctx->vregs, fpsimd->vregs, sizeof(fpsimd->vregs));
59 __put_user_error(fpsimd->fpsr, &ctx->fpsr, err);
60 __put_user_error(fpsimd->fpcr, &ctx->fpcr, err);
62 /* copy the magic/size information */
63 __put_user_error(FPSIMD_MAGIC, &ctx->head.magic, err);
64 __put_user_error(sizeof(struct fpsimd_context), &ctx->head.size, err);
66 return err ? -EFAULT : 0;
69 static int restore_fpsimd_context(struct fpsimd_context __user *ctx)
71 struct fpsimd_state fpsimd;
75 /* check the magic/size information */
76 __get_user_error(magic, &ctx->head.magic, err);
77 __get_user_error(size, &ctx->head.size, err);
80 if (magic != FPSIMD_MAGIC || size != sizeof(struct fpsimd_context))
83 /* copy the FP and status/control registers */
84 err = __copy_from_user(fpsimd.vregs, ctx->vregs,
85 sizeof(fpsimd.vregs));
86 __get_user_error(fpsimd.fpsr, &ctx->fpsr, err);
87 __get_user_error(fpsimd.fpcr, &ctx->fpcr, err);
89 /* load the hardware registers from the fpsimd_state structure */
91 fpsimd_update_current_state(&fpsimd);
93 return err ? -EFAULT : 0;
96 static int restore_sigframe(struct pt_regs *regs,
97 struct rt_sigframe __user *sf)
101 void *aux = sf->uc.uc_mcontext.__reserved;
103 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
105 set_current_blocked(&set);
107 for (i = 0; i < 31; i++)
108 __get_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i],
110 __get_user_error(regs->sp, &sf->uc.uc_mcontext.sp, err);
111 __get_user_error(regs->pc, &sf->uc.uc_mcontext.pc, err);
112 __get_user_error(regs->pstate, &sf->uc.uc_mcontext.pstate, err);
115 * Avoid sys_rt_sigreturn() restarting.
117 regs->syscallno = ~0UL;
119 err |= !valid_user_regs(®s->user_regs, current);
122 struct fpsimd_context *fpsimd_ctx =
123 container_of(aux, struct fpsimd_context, head);
124 err |= restore_fpsimd_context(fpsimd_ctx);
130 asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
132 struct rt_sigframe __user *frame;
134 /* Always make any pending restarted system calls return -EINTR */
135 current->restart_block.fn = do_no_restart_syscall;
138 * Since we stacked the signal on a 128-bit boundary, then 'sp' should
139 * be word aligned here.
144 frame = (struct rt_sigframe __user *)regs->sp;
146 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
149 if (restore_sigframe(regs, frame))
152 if (restore_altstack(&frame->uc.uc_stack))
155 return regs->regs[0];
158 if (show_unhandled_signals)
159 pr_info_ratelimited("%s[%d]: bad frame in %s: pc=%08llx sp=%08llx\n",
160 current->comm, task_pid_nr(current), __func__,
162 force_sig(SIGSEGV, current);
166 static int setup_sigframe(struct rt_sigframe __user *sf,
167 struct pt_regs *regs, sigset_t *set)
170 void *aux = sf->uc.uc_mcontext.__reserved;
171 struct _aarch64_ctx *end;
173 /* set up the stack frame for unwinding */
174 __put_user_error(regs->regs[29], &sf->fp, err);
175 __put_user_error(regs->regs[30], &sf->lr, err);
177 for (i = 0; i < 31; i++)
178 __put_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i],
180 __put_user_error(regs->sp, &sf->uc.uc_mcontext.sp, err);
181 __put_user_error(regs->pc, &sf->uc.uc_mcontext.pc, err);
182 __put_user_error(regs->pstate, &sf->uc.uc_mcontext.pstate, err);
184 __put_user_error(current->thread.fault_address, &sf->uc.uc_mcontext.fault_address, err);
186 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
189 struct fpsimd_context *fpsimd_ctx =
190 container_of(aux, struct fpsimd_context, head);
191 err |= preserve_fpsimd_context(fpsimd_ctx);
192 aux += sizeof(*fpsimd_ctx);
195 /* fault information, if valid */
196 if (current->thread.fault_code) {
197 struct esr_context *esr_ctx =
198 container_of(aux, struct esr_context, head);
199 __put_user_error(ESR_MAGIC, &esr_ctx->head.magic, err);
200 __put_user_error(sizeof(*esr_ctx), &esr_ctx->head.size, err);
201 __put_user_error(current->thread.fault_code, &esr_ctx->esr, err);
202 aux += sizeof(*esr_ctx);
205 /* set the "end" magic */
207 __put_user_error(0, &end->magic, err);
208 __put_user_error(0, &end->size, err);
213 static struct rt_sigframe __user *get_sigframe(struct ksignal *ksig,
214 struct pt_regs *regs)
216 unsigned long sp, sp_top;
217 struct rt_sigframe __user *frame;
219 sp = sp_top = sigsp(regs->sp, ksig);
221 sp = (sp - sizeof(struct rt_sigframe)) & ~15;
222 frame = (struct rt_sigframe __user *)sp;
225 * Check that we can actually write to the signal frame.
227 if (!access_ok(VERIFY_WRITE, frame, sp_top - sp))
233 static void setup_return(struct pt_regs *regs, struct k_sigaction *ka,
234 void __user *frame, int usig)
236 __sigrestore_t sigtramp;
238 regs->regs[0] = usig;
239 regs->sp = (unsigned long)frame;
240 regs->regs[29] = regs->sp + offsetof(struct rt_sigframe, fp);
241 regs->pc = (unsigned long)ka->sa.sa_handler;
243 if (ka->sa.sa_flags & SA_RESTORER)
244 sigtramp = ka->sa.sa_restorer;
246 sigtramp = VDSO_SYMBOL(current->mm->context.vdso, sigtramp);
248 regs->regs[30] = (unsigned long)sigtramp;
251 static int setup_rt_frame(int usig, struct ksignal *ksig, sigset_t *set,
252 struct pt_regs *regs)
254 struct rt_sigframe __user *frame;
257 frame = get_sigframe(ksig, regs);
261 __put_user_error(0, &frame->uc.uc_flags, err);
262 __put_user_error(NULL, &frame->uc.uc_link, err);
264 err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
265 err |= setup_sigframe(frame, regs, set);
267 setup_return(regs, &ksig->ka, frame, usig);
268 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
269 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
270 regs->regs[1] = (unsigned long)&frame->info;
271 regs->regs[2] = (unsigned long)&frame->uc;
278 static void setup_restart_syscall(struct pt_regs *regs)
280 if (is_compat_task())
281 compat_setup_restart_syscall(regs);
283 regs->regs[8] = __NR_restart_syscall;
287 * OK, we're invoking a handler
289 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
291 struct task_struct *tsk = current;
292 sigset_t *oldset = sigmask_to_save();
293 int usig = ksig->sig;
297 * Set up the stack frame
299 if (is_compat_task()) {
300 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
301 ret = compat_setup_rt_frame(usig, ksig, oldset, regs);
303 ret = compat_setup_frame(usig, ksig, oldset, regs);
305 ret = setup_rt_frame(usig, ksig, oldset, regs);
309 * Check that the resulting registers are actually sane.
311 ret |= !valid_user_regs(®s->user_regs, current);
314 * Fast forward the stepping logic so we step into the signal
318 user_fastforward_single_step(tsk);
320 signal_setup_done(ret, ksig, 0);
324 * Note that 'init' is a special process: it doesn't get signals it doesn't
325 * want to handle. Thus you cannot kill init even with a SIGKILL even by
328 * Note that we go through the signals twice: once to check the signals that
329 * the kernel can handle, and then we build all the user-level signal handling
330 * stack-frames in one go after that.
332 static void do_signal(struct pt_regs *regs)
334 unsigned long continue_addr = 0, restart_addr = 0;
336 int syscall = (int)regs->syscallno;
340 * If we were from a system call, check for system call restarting...
343 continue_addr = regs->pc;
344 restart_addr = continue_addr - (compat_thumb_mode(regs) ? 2 : 4);
345 retval = regs->regs[0];
348 * Avoid additional syscall restarting via ret_to_user.
350 regs->syscallno = ~0UL;
353 * Prepare for system call restart. We do this here so that a
354 * debugger will see the already changed PC.
357 case -ERESTARTNOHAND:
359 case -ERESTARTNOINTR:
360 case -ERESTART_RESTARTBLOCK:
361 regs->regs[0] = regs->orig_x0;
362 regs->pc = restart_addr;
368 * Get the signal to deliver. When running under ptrace, at this point
369 * the debugger may change all of our registers.
371 if (get_signal(&ksig)) {
373 * Depending on the signal settings, we may need to revert the
374 * decision to restart the system call, but skip this if a
375 * debugger has chosen to restart at a different PC.
377 if (regs->pc == restart_addr &&
378 (retval == -ERESTARTNOHAND ||
379 retval == -ERESTART_RESTARTBLOCK ||
380 (retval == -ERESTARTSYS &&
381 !(ksig.ka.sa.sa_flags & SA_RESTART)))) {
382 regs->regs[0] = -EINTR;
383 regs->pc = continue_addr;
386 handle_signal(&ksig, regs);
391 * Handle restarting a different system call. As above, if a debugger
392 * has chosen to restart at a different PC, ignore the restart.
394 if (syscall >= 0 && regs->pc == restart_addr) {
395 if (retval == -ERESTART_RESTARTBLOCK)
396 setup_restart_syscall(regs);
397 user_rewind_single_step(current);
400 restore_saved_sigmask();
403 asmlinkage void do_notify_resume(struct pt_regs *regs,
404 unsigned int thread_flags)
407 * The assembly code enters us with IRQs off, but it hasn't
408 * informed the tracing code of that for efficiency reasons.
409 * Update the trace code with the current status.
411 trace_hardirqs_off();
413 /* Check valid user FS if needed */
414 addr_limit_user_check();
417 if (thread_flags & _TIF_NEED_RESCHED) {
422 if (thread_flags & _TIF_UPROBE)
423 uprobe_notify_resume(regs);
425 if (thread_flags & _TIF_SIGPENDING)
428 if (thread_flags & _TIF_NOTIFY_RESUME) {
429 clear_thread_flag(TIF_NOTIFY_RESUME);
430 tracehook_notify_resume(regs);
433 if (thread_flags & _TIF_FOREIGN_FPSTATE)
434 fpsimd_restore_current_state();
438 thread_flags = READ_ONCE(current_thread_info()->flags);
439 } while (thread_flags & _TIF_WORK_MASK);