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/kernel.h>
23 #include <linux/signal.h>
24 #include <linux/personality.h>
25 #include <linux/freezer.h>
26 #include <linux/stddef.h>
27 #include <linux/uaccess.h>
28 #include <linux/sizes.h>
29 #include <linux/string.h>
30 #include <linux/tracehook.h>
31 #include <linux/ratelimit.h>
32 #include <linux/syscalls.h>
34 #include <asm/debug-monitors.h>
36 #include <asm/cacheflush.h>
37 #include <asm/ucontext.h>
38 #include <asm/unistd.h>
39 #include <asm/fpsimd.h>
40 #include <asm/signal32.h>
44 * Do a signal return; undo the signal stack. These are aligned to 128-bit.
56 struct rt_sigframe_user_layout
{
57 struct rt_sigframe __user
*sigframe
;
58 struct frame_record __user
*next_frame
;
60 unsigned long size
; /* size of allocated sigframe data */
61 unsigned long limit
; /* largest allowed size */
63 unsigned long fpsimd_offset
;
64 unsigned long esr_offset
;
65 unsigned long extra_offset
;
66 unsigned long end_offset
;
69 #define BASE_SIGFRAME_SIZE round_up(sizeof(struct rt_sigframe), 16)
70 #define TERMINATOR_SIZE round_up(sizeof(struct _aarch64_ctx), 16)
71 #define EXTRA_CONTEXT_SIZE round_up(sizeof(struct extra_context), 16)
73 static void init_user_layout(struct rt_sigframe_user_layout
*user
)
75 const size_t reserved_size
=
76 sizeof(user
->sigframe
->uc
.uc_mcontext
.__reserved
);
78 memset(user
, 0, sizeof(*user
));
79 user
->size
= offsetof(struct rt_sigframe
, uc
.uc_mcontext
.__reserved
);
81 user
->limit
= user
->size
+ reserved_size
;
83 user
->limit
-= TERMINATOR_SIZE
;
84 user
->limit
-= EXTRA_CONTEXT_SIZE
;
85 /* Reserve space for extension and terminator ^ */
88 static size_t sigframe_size(struct rt_sigframe_user_layout
const *user
)
90 return round_up(max(user
->size
, sizeof(struct rt_sigframe
)), 16);
94 * Sanity limit on the approximate maximum size of signal frame we'll
95 * try to generate. Stack alignment padding and the frame record are
96 * not taken into account. This limit is not a guarantee and is
99 #define SIGFRAME_MAXSZ SZ_64K
101 static int __sigframe_alloc(struct rt_sigframe_user_layout
*user
,
102 unsigned long *offset
, size_t size
, bool extend
)
104 size_t padded_size
= round_up(size
, 16);
106 if (padded_size
> user
->limit
- user
->size
&&
107 !user
->extra_offset
&&
111 user
->limit
+= EXTRA_CONTEXT_SIZE
;
112 ret
= __sigframe_alloc(user
, &user
->extra_offset
,
113 sizeof(struct extra_context
), false);
115 user
->limit
-= EXTRA_CONTEXT_SIZE
;
119 /* Reserve space for the __reserved[] terminator */
120 user
->size
+= TERMINATOR_SIZE
;
123 * Allow expansion up to SIGFRAME_MAXSZ, ensuring space for
126 user
->limit
= SIGFRAME_MAXSZ
- TERMINATOR_SIZE
;
129 /* Still not enough space? Bad luck! */
130 if (padded_size
> user
->limit
- user
->size
)
133 *offset
= user
->size
;
134 user
->size
+= padded_size
;
140 * Allocate space for an optional record of <size> bytes in the user
141 * signal frame. The offset from the signal frame base address to the
142 * allocated block is assigned to *offset.
144 static int sigframe_alloc(struct rt_sigframe_user_layout
*user
,
145 unsigned long *offset
, size_t size
)
147 return __sigframe_alloc(user
, offset
, size
, true);
150 /* Allocate the null terminator record and prevent further allocations */
151 static int sigframe_alloc_end(struct rt_sigframe_user_layout
*user
)
155 /* Un-reserve the space reserved for the terminator: */
156 user
->limit
+= TERMINATOR_SIZE
;
158 ret
= sigframe_alloc(user
, &user
->end_offset
,
159 sizeof(struct _aarch64_ctx
));
163 /* Prevent further allocation: */
164 user
->limit
= user
->size
;
168 static void __user
*apply_user_offset(
169 struct rt_sigframe_user_layout
const *user
, unsigned long offset
)
171 char __user
*base
= (char __user
*)user
->sigframe
;
173 return base
+ offset
;
176 static int preserve_fpsimd_context(struct fpsimd_context __user
*ctx
)
178 struct fpsimd_state
*fpsimd
= ¤t
->thread
.fpsimd_state
;
181 /* dump the hardware registers to the fpsimd_state structure */
182 fpsimd_preserve_current_state();
184 /* copy the FP and status/control registers */
185 err
= __copy_to_user(ctx
->vregs
, fpsimd
->vregs
, sizeof(fpsimd
->vregs
));
186 __put_user_error(fpsimd
->fpsr
, &ctx
->fpsr
, err
);
187 __put_user_error(fpsimd
->fpcr
, &ctx
->fpcr
, err
);
189 /* copy the magic/size information */
190 __put_user_error(FPSIMD_MAGIC
, &ctx
->head
.magic
, err
);
191 __put_user_error(sizeof(struct fpsimd_context
), &ctx
->head
.size
, err
);
193 return err
? -EFAULT
: 0;
196 static int restore_fpsimd_context(struct fpsimd_context __user
*ctx
)
198 struct fpsimd_state fpsimd
;
202 /* check the magic/size information */
203 __get_user_error(magic
, &ctx
->head
.magic
, err
);
204 __get_user_error(size
, &ctx
->head
.size
, err
);
207 if (magic
!= FPSIMD_MAGIC
|| size
!= sizeof(struct fpsimd_context
))
210 /* copy the FP and status/control registers */
211 err
= __copy_from_user(fpsimd
.vregs
, ctx
->vregs
,
212 sizeof(fpsimd
.vregs
));
213 __get_user_error(fpsimd
.fpsr
, &ctx
->fpsr
, err
);
214 __get_user_error(fpsimd
.fpcr
, &ctx
->fpcr
, err
);
216 /* load the hardware registers from the fpsimd_state structure */
218 fpsimd_update_current_state(&fpsimd
);
220 return err
? -EFAULT
: 0;
224 struct fpsimd_context __user
*fpsimd
;
227 static int parse_user_sigframe(struct user_ctxs
*user
,
228 struct rt_sigframe __user
*sf
)
230 struct sigcontext __user
*const sc
= &sf
->uc
.uc_mcontext
;
231 struct _aarch64_ctx __user
*head
;
232 char __user
*base
= (char __user
*)&sc
->__reserved
;
234 size_t limit
= sizeof(sc
->__reserved
);
235 bool have_extra_context
= false;
236 char const __user
*const sfp
= (char const __user
*)sf
;
240 if (!IS_ALIGNED((unsigned long)base
, 16))
246 char const __user
*userp
;
247 struct extra_context
const __user
*extra
;
250 struct _aarch64_ctx
const __user
*end
;
251 u32 end_magic
, end_size
;
253 if (limit
- offset
< sizeof(*head
))
256 if (!IS_ALIGNED(offset
, 16))
259 head
= (struct _aarch64_ctx __user
*)(base
+ offset
);
260 __get_user_error(magic
, &head
->magic
, err
);
261 __get_user_error(size
, &head
->size
, err
);
265 if (limit
- offset
< size
)
279 if (size
< sizeof(*user
->fpsimd
))
282 user
->fpsimd
= (struct fpsimd_context __user
*)head
;
290 if (have_extra_context
)
293 if (size
< sizeof(*extra
))
296 userp
= (char const __user
*)head
;
298 extra
= (struct extra_context
const __user
*)userp
;
301 __get_user_error(extra_datap
, &extra
->datap
, err
);
302 __get_user_error(extra_size
, &extra
->size
, err
);
306 /* Check for the dummy terminator in __reserved[]: */
308 if (limit
- offset
- size
< TERMINATOR_SIZE
)
311 end
= (struct _aarch64_ctx
const __user
*)userp
;
312 userp
+= TERMINATOR_SIZE
;
314 __get_user_error(end_magic
, &end
->magic
, err
);
315 __get_user_error(end_size
, &end
->size
, err
);
319 if (end_magic
|| end_size
)
322 /* Prevent looping/repeated parsing of extra_context */
323 have_extra_context
= true;
325 base
= (__force
void __user
*)extra_datap
;
326 if (!IS_ALIGNED((unsigned long)base
, 16))
329 if (!IS_ALIGNED(extra_size
, 16))
335 /* Reject "unreasonably large" frames: */
336 if (extra_size
> sfp
+ SIGFRAME_MAXSZ
- userp
)
340 * Ignore trailing terminator in __reserved[]
341 * and start parsing extra data:
351 if (size
< sizeof(*head
))
354 if (limit
- offset
< size
)
370 static int restore_sigframe(struct pt_regs
*regs
,
371 struct rt_sigframe __user
*sf
)
375 struct user_ctxs user
;
377 err
= __copy_from_user(&set
, &sf
->uc
.uc_sigmask
, sizeof(set
));
379 set_current_blocked(&set
);
381 for (i
= 0; i
< 31; i
++)
382 __get_user_error(regs
->regs
[i
], &sf
->uc
.uc_mcontext
.regs
[i
],
384 __get_user_error(regs
->sp
, &sf
->uc
.uc_mcontext
.sp
, err
);
385 __get_user_error(regs
->pc
, &sf
->uc
.uc_mcontext
.pc
, err
);
386 __get_user_error(regs
->pstate
, &sf
->uc
.uc_mcontext
.pstate
, err
);
389 * Avoid sys_rt_sigreturn() restarting.
391 regs
->syscallno
= ~0;
393 err
|= !valid_user_regs(®s
->user_regs
, current
);
395 err
= parse_user_sigframe(&user
, sf
);
398 err
= restore_fpsimd_context(user
.fpsimd
);
403 asmlinkage
long sys_rt_sigreturn(struct pt_regs
*regs
)
405 struct rt_sigframe __user
*frame
;
407 /* Always make any pending restarted system calls return -EINTR */
408 current
->restart_block
.fn
= do_no_restart_syscall
;
411 * Since we stacked the signal on a 128-bit boundary, then 'sp' should
412 * be word aligned here.
417 frame
= (struct rt_sigframe __user
*)regs
->sp
;
419 if (!access_ok(VERIFY_READ
, frame
, sizeof (*frame
)))
422 if (restore_sigframe(regs
, frame
))
425 if (restore_altstack(&frame
->uc
.uc_stack
))
428 return regs
->regs
[0];
431 if (show_unhandled_signals
)
432 pr_info_ratelimited("%s[%d]: bad frame in %s: pc=%08llx sp=%08llx\n",
433 current
->comm
, task_pid_nr(current
), __func__
,
435 force_sig(SIGSEGV
, current
);
439 /* Determine the layout of optional records in the signal frame */
440 static int setup_sigframe_layout(struct rt_sigframe_user_layout
*user
)
444 err
= sigframe_alloc(user
, &user
->fpsimd_offset
,
445 sizeof(struct fpsimd_context
));
449 /* fault information, if valid */
450 if (current
->thread
.fault_code
) {
451 err
= sigframe_alloc(user
, &user
->esr_offset
,
452 sizeof(struct esr_context
));
457 return sigframe_alloc_end(user
);
461 static int setup_sigframe(struct rt_sigframe_user_layout
*user
,
462 struct pt_regs
*regs
, sigset_t
*set
)
465 struct rt_sigframe __user
*sf
= user
->sigframe
;
467 /* set up the stack frame for unwinding */
468 __put_user_error(regs
->regs
[29], &user
->next_frame
->fp
, err
);
469 __put_user_error(regs
->regs
[30], &user
->next_frame
->lr
, err
);
471 for (i
= 0; i
< 31; i
++)
472 __put_user_error(regs
->regs
[i
], &sf
->uc
.uc_mcontext
.regs
[i
],
474 __put_user_error(regs
->sp
, &sf
->uc
.uc_mcontext
.sp
, err
);
475 __put_user_error(regs
->pc
, &sf
->uc
.uc_mcontext
.pc
, err
);
476 __put_user_error(regs
->pstate
, &sf
->uc
.uc_mcontext
.pstate
, err
);
478 __put_user_error(current
->thread
.fault_address
, &sf
->uc
.uc_mcontext
.fault_address
, err
);
480 err
|= __copy_to_user(&sf
->uc
.uc_sigmask
, set
, sizeof(*set
));
483 struct fpsimd_context __user
*fpsimd_ctx
=
484 apply_user_offset(user
, user
->fpsimd_offset
);
485 err
|= preserve_fpsimd_context(fpsimd_ctx
);
488 /* fault information, if valid */
489 if (err
== 0 && user
->esr_offset
) {
490 struct esr_context __user
*esr_ctx
=
491 apply_user_offset(user
, user
->esr_offset
);
493 __put_user_error(ESR_MAGIC
, &esr_ctx
->head
.magic
, err
);
494 __put_user_error(sizeof(*esr_ctx
), &esr_ctx
->head
.size
, err
);
495 __put_user_error(current
->thread
.fault_code
, &esr_ctx
->esr
, err
);
498 if (err
== 0 && user
->extra_offset
) {
499 char __user
*sfp
= (char __user
*)user
->sigframe
;
501 apply_user_offset(user
, user
->extra_offset
);
503 struct extra_context __user
*extra
;
504 struct _aarch64_ctx __user
*end
;
508 extra
= (struct extra_context __user
*)userp
;
509 userp
+= EXTRA_CONTEXT_SIZE
;
511 end
= (struct _aarch64_ctx __user
*)userp
;
512 userp
+= TERMINATOR_SIZE
;
515 * extra_datap is just written to the signal frame.
516 * The value gets cast back to a void __user *
519 extra_datap
= (__force u64
)userp
;
520 extra_size
= sfp
+ round_up(user
->size
, 16) - userp
;
522 __put_user_error(EXTRA_MAGIC
, &extra
->head
.magic
, err
);
523 __put_user_error(EXTRA_CONTEXT_SIZE
, &extra
->head
.size
, err
);
524 __put_user_error(extra_datap
, &extra
->datap
, err
);
525 __put_user_error(extra_size
, &extra
->size
, err
);
527 /* Add the terminator */
528 __put_user_error(0, &end
->magic
, err
);
529 __put_user_error(0, &end
->size
, err
);
532 /* set the "end" magic */
534 struct _aarch64_ctx __user
*end
=
535 apply_user_offset(user
, user
->end_offset
);
537 __put_user_error(0, &end
->magic
, err
);
538 __put_user_error(0, &end
->size
, err
);
544 static int get_sigframe(struct rt_sigframe_user_layout
*user
,
545 struct ksignal
*ksig
, struct pt_regs
*regs
)
547 unsigned long sp
, sp_top
;
550 init_user_layout(user
);
551 err
= setup_sigframe_layout(user
);
555 sp
= sp_top
= sigsp(regs
->sp
, ksig
);
557 sp
= round_down(sp
- sizeof(struct frame_record
), 16);
558 user
->next_frame
= (struct frame_record __user
*)sp
;
560 sp
= round_down(sp
, 16) - sigframe_size(user
);
561 user
->sigframe
= (struct rt_sigframe __user
*)sp
;
564 * Check that we can actually write to the signal frame.
566 if (!access_ok(VERIFY_WRITE
, user
->sigframe
, sp_top
- sp
))
572 static void setup_return(struct pt_regs
*regs
, struct k_sigaction
*ka
,
573 struct rt_sigframe_user_layout
*user
, int usig
)
575 __sigrestore_t sigtramp
;
577 regs
->regs
[0] = usig
;
578 regs
->sp
= (unsigned long)user
->sigframe
;
579 regs
->regs
[29] = (unsigned long)&user
->next_frame
->fp
;
580 regs
->pc
= (unsigned long)ka
->sa
.sa_handler
;
582 if (ka
->sa
.sa_flags
& SA_RESTORER
)
583 sigtramp
= ka
->sa
.sa_restorer
;
585 sigtramp
= VDSO_SYMBOL(current
->mm
->context
.vdso
, sigtramp
);
587 regs
->regs
[30] = (unsigned long)sigtramp
;
590 static int setup_rt_frame(int usig
, struct ksignal
*ksig
, sigset_t
*set
,
591 struct pt_regs
*regs
)
593 struct rt_sigframe_user_layout user
;
594 struct rt_sigframe __user
*frame
;
597 if (get_sigframe(&user
, ksig
, regs
))
600 frame
= user
.sigframe
;
602 __put_user_error(0, &frame
->uc
.uc_flags
, err
);
603 __put_user_error(NULL
, &frame
->uc
.uc_link
, err
);
605 err
|= __save_altstack(&frame
->uc
.uc_stack
, regs
->sp
);
606 err
|= setup_sigframe(&user
, regs
, set
);
608 setup_return(regs
, &ksig
->ka
, &user
, usig
);
609 if (ksig
->ka
.sa
.sa_flags
& SA_SIGINFO
) {
610 err
|= copy_siginfo_to_user(&frame
->info
, &ksig
->info
);
611 regs
->regs
[1] = (unsigned long)&frame
->info
;
612 regs
->regs
[2] = (unsigned long)&frame
->uc
;
619 static void setup_restart_syscall(struct pt_regs
*regs
)
621 if (is_compat_task())
622 compat_setup_restart_syscall(regs
);
624 regs
->regs
[8] = __NR_restart_syscall
;
628 * OK, we're invoking a handler
630 static void handle_signal(struct ksignal
*ksig
, struct pt_regs
*regs
)
632 struct task_struct
*tsk
= current
;
633 sigset_t
*oldset
= sigmask_to_save();
634 int usig
= ksig
->sig
;
638 * Set up the stack frame
640 if (is_compat_task()) {
641 if (ksig
->ka
.sa
.sa_flags
& SA_SIGINFO
)
642 ret
= compat_setup_rt_frame(usig
, ksig
, oldset
, regs
);
644 ret
= compat_setup_frame(usig
, ksig
, oldset
, regs
);
646 ret
= setup_rt_frame(usig
, ksig
, oldset
, regs
);
650 * Check that the resulting registers are actually sane.
652 ret
|= !valid_user_regs(®s
->user_regs
, current
);
655 * Fast forward the stepping logic so we step into the signal
659 user_fastforward_single_step(tsk
);
661 signal_setup_done(ret
, ksig
, 0);
665 * Note that 'init' is a special process: it doesn't get signals it doesn't
666 * want to handle. Thus you cannot kill init even with a SIGKILL even by
669 * Note that we go through the signals twice: once to check the signals that
670 * the kernel can handle, and then we build all the user-level signal handling
671 * stack-frames in one go after that.
673 static void do_signal(struct pt_regs
*regs
)
675 unsigned long continue_addr
= 0, restart_addr
= 0;
677 int syscall
= regs
->syscallno
;
681 * If we were from a system call, check for system call restarting...
684 continue_addr
= regs
->pc
;
685 restart_addr
= continue_addr
- (compat_thumb_mode(regs
) ? 2 : 4);
686 retval
= regs
->regs
[0];
689 * Avoid additional syscall restarting via ret_to_user.
691 regs
->syscallno
= ~0;
694 * Prepare for system call restart. We do this here so that a
695 * debugger will see the already changed PC.
698 case -ERESTARTNOHAND
:
700 case -ERESTARTNOINTR
:
701 case -ERESTART_RESTARTBLOCK
:
702 regs
->regs
[0] = regs
->orig_x0
;
703 regs
->pc
= restart_addr
;
709 * Get the signal to deliver. When running under ptrace, at this point
710 * the debugger may change all of our registers.
712 if (get_signal(&ksig
)) {
714 * Depending on the signal settings, we may need to revert the
715 * decision to restart the system call, but skip this if a
716 * debugger has chosen to restart at a different PC.
718 if (regs
->pc
== restart_addr
&&
719 (retval
== -ERESTARTNOHAND
||
720 retval
== -ERESTART_RESTARTBLOCK
||
721 (retval
== -ERESTARTSYS
&&
722 !(ksig
.ka
.sa
.sa_flags
& SA_RESTART
)))) {
723 regs
->regs
[0] = -EINTR
;
724 regs
->pc
= continue_addr
;
727 handle_signal(&ksig
, regs
);
732 * Handle restarting a different system call. As above, if a debugger
733 * has chosen to restart at a different PC, ignore the restart.
735 if (syscall
>= 0 && regs
->pc
== restart_addr
) {
736 if (retval
== -ERESTART_RESTARTBLOCK
)
737 setup_restart_syscall(regs
);
738 user_rewind_single_step(current
);
741 restore_saved_sigmask();
744 asmlinkage
void do_notify_resume(struct pt_regs
*regs
,
745 unsigned int thread_flags
)
748 * The assembly code enters us with IRQs off, but it hasn't
749 * informed the tracing code of that for efficiency reasons.
750 * Update the trace code with the current status.
752 trace_hardirqs_off();
755 /* Check valid user FS if needed */
756 addr_limit_user_check();
758 if (thread_flags
& _TIF_NEED_RESCHED
) {
763 if (thread_flags
& _TIF_UPROBE
)
764 uprobe_notify_resume(regs
);
766 if (thread_flags
& _TIF_SIGPENDING
)
769 if (thread_flags
& _TIF_NOTIFY_RESUME
) {
770 clear_thread_flag(TIF_NOTIFY_RESUME
);
771 tracehook_notify_resume(regs
);
774 if (thread_flags
& _TIF_FOREIGN_FPSTATE
)
775 fpsimd_restore_current_state();
779 thread_flags
= READ_ONCE(current_thread_info()->flags
);
780 } while (thread_flags
& _TIF_WORK_MASK
);