1 // SPDX-License-Identifier: GPL-2.0-only
3 * Based on arch/arm/kernel/signal.c
5 * Copyright (C) 1995-2009 Russell King
6 * Copyright (C) 2012 ARM Ltd.
9 #include <linux/cache.h>
10 #include <linux/compat.h>
11 #include <linux/errno.h>
12 #include <linux/kernel.h>
13 #include <linux/signal.h>
14 #include <linux/freezer.h>
15 #include <linux/stddef.h>
16 #include <linux/uaccess.h>
17 #include <linux/sizes.h>
18 #include <linux/string.h>
19 #include <linux/resume_user_mode.h>
20 #include <linux/ratelimit.h>
21 #include <linux/syscalls.h>
23 #include <asm/daifflags.h>
24 #include <asm/debug-monitors.h>
26 #include <asm/cacheflush.h>
27 #include <asm/ucontext.h>
28 #include <asm/unistd.h>
29 #include <asm/fpsimd.h>
30 #include <asm/ptrace.h>
31 #include <asm/syscall.h>
32 #include <asm/signal32.h>
33 #include <asm/traps.h>
37 * Do a signal return; undo the signal stack. These are aligned to 128-bit.
49 struct rt_sigframe_user_layout {
50 struct rt_sigframe __user *sigframe;
51 struct frame_record __user *next_frame;
53 unsigned long size; /* size of allocated sigframe data */
54 unsigned long limit; /* largest allowed size */
56 unsigned long fpsimd_offset;
57 unsigned long esr_offset;
58 unsigned long sve_offset;
59 unsigned long za_offset;
60 unsigned long extra_offset;
61 unsigned long end_offset;
64 #define BASE_SIGFRAME_SIZE round_up(sizeof(struct rt_sigframe), 16)
65 #define TERMINATOR_SIZE round_up(sizeof(struct _aarch64_ctx), 16)
66 #define EXTRA_CONTEXT_SIZE round_up(sizeof(struct extra_context), 16)
68 static void init_user_layout(struct rt_sigframe_user_layout *user)
70 const size_t reserved_size =
71 sizeof(user->sigframe->uc.uc_mcontext.__reserved);
73 memset(user, 0, sizeof(*user));
74 user->size = offsetof(struct rt_sigframe, uc.uc_mcontext.__reserved);
76 user->limit = user->size + reserved_size;
78 user->limit -= TERMINATOR_SIZE;
79 user->limit -= EXTRA_CONTEXT_SIZE;
80 /* Reserve space for extension and terminator ^ */
83 static size_t sigframe_size(struct rt_sigframe_user_layout const *user)
85 return round_up(max(user->size, sizeof(struct rt_sigframe)), 16);
89 * Sanity limit on the approximate maximum size of signal frame we'll
90 * try to generate. Stack alignment padding and the frame record are
91 * not taken into account. This limit is not a guarantee and is
94 #define SIGFRAME_MAXSZ SZ_64K
96 static int __sigframe_alloc(struct rt_sigframe_user_layout *user,
97 unsigned long *offset, size_t size, bool extend)
99 size_t padded_size = round_up(size, 16);
101 if (padded_size > user->limit - user->size &&
102 !user->extra_offset &&
106 user->limit += EXTRA_CONTEXT_SIZE;
107 ret = __sigframe_alloc(user, &user->extra_offset,
108 sizeof(struct extra_context), false);
110 user->limit -= EXTRA_CONTEXT_SIZE;
114 /* Reserve space for the __reserved[] terminator */
115 user->size += TERMINATOR_SIZE;
118 * Allow expansion up to SIGFRAME_MAXSZ, ensuring space for
121 user->limit = SIGFRAME_MAXSZ - TERMINATOR_SIZE;
124 /* Still not enough space? Bad luck! */
125 if (padded_size > user->limit - user->size)
128 *offset = user->size;
129 user->size += padded_size;
135 * Allocate space for an optional record of <size> bytes in the user
136 * signal frame. The offset from the signal frame base address to the
137 * allocated block is assigned to *offset.
139 static int sigframe_alloc(struct rt_sigframe_user_layout *user,
140 unsigned long *offset, size_t size)
142 return __sigframe_alloc(user, offset, size, true);
145 /* Allocate the null terminator record and prevent further allocations */
146 static int sigframe_alloc_end(struct rt_sigframe_user_layout *user)
150 /* Un-reserve the space reserved for the terminator: */
151 user->limit += TERMINATOR_SIZE;
153 ret = sigframe_alloc(user, &user->end_offset,
154 sizeof(struct _aarch64_ctx));
158 /* Prevent further allocation: */
159 user->limit = user->size;
163 static void __user *apply_user_offset(
164 struct rt_sigframe_user_layout const *user, unsigned long offset)
166 char __user *base = (char __user *)user->sigframe;
168 return base + offset;
171 static int preserve_fpsimd_context(struct fpsimd_context __user *ctx)
173 struct user_fpsimd_state const *fpsimd =
174 ¤t->thread.uw.fpsimd_state;
177 /* copy the FP and status/control registers */
178 err = __copy_to_user(ctx->vregs, fpsimd->vregs, sizeof(fpsimd->vregs));
179 __put_user_error(fpsimd->fpsr, &ctx->fpsr, err);
180 __put_user_error(fpsimd->fpcr, &ctx->fpcr, err);
182 /* copy the magic/size information */
183 __put_user_error(FPSIMD_MAGIC, &ctx->head.magic, err);
184 __put_user_error(sizeof(struct fpsimd_context), &ctx->head.size, err);
186 return err ? -EFAULT : 0;
189 static int restore_fpsimd_context(struct fpsimd_context __user *ctx)
191 struct user_fpsimd_state fpsimd;
195 /* check the magic/size information */
196 __get_user_error(magic, &ctx->head.magic, err);
197 __get_user_error(size, &ctx->head.size, err);
200 if (magic != FPSIMD_MAGIC || size != sizeof(struct fpsimd_context))
203 /* copy the FP and status/control registers */
204 err = __copy_from_user(fpsimd.vregs, ctx->vregs,
205 sizeof(fpsimd.vregs));
206 __get_user_error(fpsimd.fpsr, &ctx->fpsr, err);
207 __get_user_error(fpsimd.fpcr, &ctx->fpcr, err);
209 clear_thread_flag(TIF_SVE);
211 /* load the hardware registers from the fpsimd_state structure */
213 fpsimd_update_current_state(&fpsimd);
215 return err ? -EFAULT : 0;
220 struct fpsimd_context __user *fpsimd;
221 struct sve_context __user *sve;
222 struct za_context __user *za;
225 #ifdef CONFIG_ARM64_SVE
227 static int preserve_sve_context(struct sve_context __user *ctx)
230 u16 reserved[ARRAY_SIZE(ctx->__reserved)];
232 unsigned int vl = task_get_sve_vl(current);
235 if (thread_sm_enabled(¤t->thread)) {
236 vl = task_get_sme_vl(current);
237 vq = sve_vq_from_vl(vl);
238 flags |= SVE_SIG_FLAG_SM;
239 } else if (test_thread_flag(TIF_SVE)) {
240 vq = sve_vq_from_vl(vl);
243 memset(reserved, 0, sizeof(reserved));
245 __put_user_error(SVE_MAGIC, &ctx->head.magic, err);
246 __put_user_error(round_up(SVE_SIG_CONTEXT_SIZE(vq), 16),
247 &ctx->head.size, err);
248 __put_user_error(vl, &ctx->vl, err);
249 __put_user_error(flags, &ctx->flags, err);
250 BUILD_BUG_ON(sizeof(ctx->__reserved) != sizeof(reserved));
251 err |= __copy_to_user(&ctx->__reserved, reserved, sizeof(reserved));
255 * This assumes that the SVE state has already been saved to
256 * the task struct by calling the function
257 * fpsimd_signal_preserve_current_state().
259 err |= __copy_to_user((char __user *)ctx + SVE_SIG_REGS_OFFSET,
260 current->thread.sve_state,
261 SVE_SIG_REGS_SIZE(vq));
264 return err ? -EFAULT : 0;
267 static int restore_sve_fpsimd_context(struct user_ctxs *user)
271 struct user_fpsimd_state fpsimd;
272 struct sve_context sve;
274 if (__copy_from_user(&sve, user->sve, sizeof(sve)))
277 if (sve.flags & SVE_SIG_FLAG_SM) {
278 if (!system_supports_sme())
281 vl = task_get_sme_vl(current);
283 if (!system_supports_sve())
286 vl = task_get_sve_vl(current);
292 if (sve.head.size <= sizeof(*user->sve)) {
293 clear_thread_flag(TIF_SVE);
294 current->thread.svcr &= ~SVCR_SM_MASK;
298 vq = sve_vq_from_vl(sve.vl);
300 if (sve.head.size < SVE_SIG_CONTEXT_SIZE(vq))
304 * Careful: we are about __copy_from_user() directly into
305 * thread.sve_state with preemption enabled, so protection is
306 * needed to prevent a racing context switch from writing stale
307 * registers back over the new data.
310 fpsimd_flush_task_state(current);
311 /* From now, fpsimd_thread_switch() won't touch thread.sve_state */
314 if (!current->thread.sve_state) {
315 clear_thread_flag(TIF_SVE);
319 err = __copy_from_user(current->thread.sve_state,
320 (char __user const *)user->sve +
322 SVE_SIG_REGS_SIZE(vq));
326 if (sve.flags & SVE_SIG_FLAG_SM)
327 current->thread.svcr |= SVCR_SM_MASK;
329 set_thread_flag(TIF_SVE);
332 /* copy the FP and status/control registers */
333 /* restore_sigframe() already checked that user->fpsimd != NULL. */
334 err = __copy_from_user(fpsimd.vregs, user->fpsimd->vregs,
335 sizeof(fpsimd.vregs));
336 __get_user_error(fpsimd.fpsr, &user->fpsimd->fpsr, err);
337 __get_user_error(fpsimd.fpcr, &user->fpsimd->fpcr, err);
339 /* load the hardware registers from the fpsimd_state structure */
341 fpsimd_update_current_state(&fpsimd);
343 return err ? -EFAULT : 0;
346 #else /* ! CONFIG_ARM64_SVE */
348 static int restore_sve_fpsimd_context(struct user_ctxs *user)
354 /* Turn any non-optimised out attempts to use this into a link error: */
355 extern int preserve_sve_context(void __user *ctx);
357 #endif /* ! CONFIG_ARM64_SVE */
359 #ifdef CONFIG_ARM64_SME
361 static int preserve_za_context(struct za_context __user *ctx)
364 u16 reserved[ARRAY_SIZE(ctx->__reserved)];
365 unsigned int vl = task_get_sme_vl(current);
368 if (thread_za_enabled(¤t->thread))
369 vq = sve_vq_from_vl(vl);
373 memset(reserved, 0, sizeof(reserved));
375 __put_user_error(ZA_MAGIC, &ctx->head.magic, err);
376 __put_user_error(round_up(ZA_SIG_CONTEXT_SIZE(vq), 16),
377 &ctx->head.size, err);
378 __put_user_error(vl, &ctx->vl, err);
379 BUILD_BUG_ON(sizeof(ctx->__reserved) != sizeof(reserved));
380 err |= __copy_to_user(&ctx->__reserved, reserved, sizeof(reserved));
384 * This assumes that the ZA state has already been saved to
385 * the task struct by calling the function
386 * fpsimd_signal_preserve_current_state().
388 err |= __copy_to_user((char __user *)ctx + ZA_SIG_REGS_OFFSET,
389 current->thread.za_state,
390 ZA_SIG_REGS_SIZE(vq));
393 return err ? -EFAULT : 0;
396 static int restore_za_context(struct user_ctxs *user)
400 struct za_context za;
402 if (__copy_from_user(&za, user->za, sizeof(za)))
405 if (za.vl != task_get_sme_vl(current))
408 if (za.head.size <= sizeof(*user->za)) {
409 current->thread.svcr &= ~SVCR_ZA_MASK;
413 vq = sve_vq_from_vl(za.vl);
415 if (za.head.size < ZA_SIG_CONTEXT_SIZE(vq))
419 * Careful: we are about __copy_from_user() directly into
420 * thread.za_state with preemption enabled, so protection is
421 * needed to prevent a racing context switch from writing stale
422 * registers back over the new data.
425 fpsimd_flush_task_state(current);
426 /* From now, fpsimd_thread_switch() won't touch thread.sve_state */
429 if (!current->thread.za_state) {
430 current->thread.svcr &= ~SVCR_ZA_MASK;
431 clear_thread_flag(TIF_SME);
435 err = __copy_from_user(current->thread.za_state,
436 (char __user const *)user->za +
438 ZA_SIG_REGS_SIZE(vq));
442 set_thread_flag(TIF_SME);
443 current->thread.svcr |= SVCR_ZA_MASK;
447 #else /* ! CONFIG_ARM64_SME */
449 /* Turn any non-optimised out attempts to use these into a link error: */
450 extern int preserve_za_context(void __user *ctx);
451 extern int restore_za_context(struct user_ctxs *user);
453 #endif /* ! CONFIG_ARM64_SME */
455 static int parse_user_sigframe(struct user_ctxs *user,
456 struct rt_sigframe __user *sf)
458 struct sigcontext __user *const sc = &sf->uc.uc_mcontext;
459 struct _aarch64_ctx __user *head;
460 char __user *base = (char __user *)&sc->__reserved;
462 size_t limit = sizeof(sc->__reserved);
463 bool have_extra_context = false;
464 char const __user *const sfp = (char const __user *)sf;
470 if (!IS_ALIGNED((unsigned long)base, 16))
476 char const __user *userp;
477 struct extra_context const __user *extra;
480 struct _aarch64_ctx const __user *end;
481 u32 end_magic, end_size;
483 if (limit - offset < sizeof(*head))
486 if (!IS_ALIGNED(offset, 16))
489 head = (struct _aarch64_ctx __user *)(base + offset);
490 __get_user_error(magic, &head->magic, err);
491 __get_user_error(size, &head->size, err);
495 if (limit - offset < size)
506 if (!system_supports_fpsimd())
511 if (size < sizeof(*user->fpsimd))
514 user->fpsimd = (struct fpsimd_context __user *)head;
522 if (!system_supports_sve() && !system_supports_sme())
528 if (size < sizeof(*user->sve))
531 user->sve = (struct sve_context __user *)head;
535 if (!system_supports_sme())
541 if (size < sizeof(*user->za))
544 user->za = (struct za_context __user *)head;
548 if (have_extra_context)
551 if (size < sizeof(*extra))
554 userp = (char const __user *)head;
556 extra = (struct extra_context const __user *)userp;
559 __get_user_error(extra_datap, &extra->datap, err);
560 __get_user_error(extra_size, &extra->size, err);
564 /* Check for the dummy terminator in __reserved[]: */
566 if (limit - offset - size < TERMINATOR_SIZE)
569 end = (struct _aarch64_ctx const __user *)userp;
570 userp += TERMINATOR_SIZE;
572 __get_user_error(end_magic, &end->magic, err);
573 __get_user_error(end_size, &end->size, err);
577 if (end_magic || end_size)
580 /* Prevent looping/repeated parsing of extra_context */
581 have_extra_context = true;
583 base = (__force void __user *)extra_datap;
584 if (!IS_ALIGNED((unsigned long)base, 16))
587 if (!IS_ALIGNED(extra_size, 16))
593 /* Reject "unreasonably large" frames: */
594 if (extra_size > sfp + SIGFRAME_MAXSZ - userp)
598 * Ignore trailing terminator in __reserved[]
599 * and start parsing extra data:
604 if (!access_ok(base, limit))
613 if (size < sizeof(*head))
616 if (limit - offset < size)
629 static int restore_sigframe(struct pt_regs *regs,
630 struct rt_sigframe __user *sf)
634 struct user_ctxs user;
636 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
638 set_current_blocked(&set);
640 for (i = 0; i < 31; i++)
641 __get_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i],
643 __get_user_error(regs->sp, &sf->uc.uc_mcontext.sp, err);
644 __get_user_error(regs->pc, &sf->uc.uc_mcontext.pc, err);
645 __get_user_error(regs->pstate, &sf->uc.uc_mcontext.pstate, err);
648 * Avoid sys_rt_sigreturn() restarting.
650 forget_syscall(regs);
652 err |= !valid_user_regs(®s->user_regs, current);
654 err = parse_user_sigframe(&user, sf);
656 if (err == 0 && system_supports_fpsimd()) {
661 err = restore_sve_fpsimd_context(&user);
663 err = restore_fpsimd_context(user.fpsimd);
666 if (err == 0 && system_supports_sme() && user.za)
667 err = restore_za_context(&user);
672 SYSCALL_DEFINE0(rt_sigreturn)
674 struct pt_regs *regs = current_pt_regs();
675 struct rt_sigframe __user *frame;
677 /* Always make any pending restarted system calls return -EINTR */
678 current->restart_block.fn = do_no_restart_syscall;
681 * Since we stacked the signal on a 128-bit boundary, then 'sp' should
682 * be word aligned here.
687 frame = (struct rt_sigframe __user *)regs->sp;
689 if (!access_ok(frame, sizeof (*frame)))
692 if (restore_sigframe(regs, frame))
695 if (restore_altstack(&frame->uc.uc_stack))
698 return regs->regs[0];
701 arm64_notify_segfault(regs->sp);
706 * Determine the layout of optional records in the signal frame
708 * add_all: if true, lays out the biggest possible signal frame for
709 * this task; otherwise, generates a layout for the current state
712 static int setup_sigframe_layout(struct rt_sigframe_user_layout *user,
717 if (system_supports_fpsimd()) {
718 err = sigframe_alloc(user, &user->fpsimd_offset,
719 sizeof(struct fpsimd_context));
724 /* fault information, if valid */
725 if (add_all || current->thread.fault_code) {
726 err = sigframe_alloc(user, &user->esr_offset,
727 sizeof(struct esr_context));
732 if (system_supports_sve()) {
735 if (add_all || test_thread_flag(TIF_SVE) ||
736 thread_sm_enabled(¤t->thread)) {
737 int vl = max(sve_max_vl(), sme_max_vl());
740 vl = thread_get_cur_vl(¤t->thread);
742 vq = sve_vq_from_vl(vl);
745 err = sigframe_alloc(user, &user->sve_offset,
746 SVE_SIG_CONTEXT_SIZE(vq));
751 if (system_supports_sme()) {
758 vl = task_get_sme_vl(current);
760 if (thread_za_enabled(¤t->thread))
761 vq = sve_vq_from_vl(vl);
763 err = sigframe_alloc(user, &user->za_offset,
764 ZA_SIG_CONTEXT_SIZE(vq));
769 return sigframe_alloc_end(user);
772 static int setup_sigframe(struct rt_sigframe_user_layout *user,
773 struct pt_regs *regs, sigset_t *set)
776 struct rt_sigframe __user *sf = user->sigframe;
778 /* set up the stack frame for unwinding */
779 __put_user_error(regs->regs[29], &user->next_frame->fp, err);
780 __put_user_error(regs->regs[30], &user->next_frame->lr, err);
782 for (i = 0; i < 31; i++)
783 __put_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i],
785 __put_user_error(regs->sp, &sf->uc.uc_mcontext.sp, err);
786 __put_user_error(regs->pc, &sf->uc.uc_mcontext.pc, err);
787 __put_user_error(regs->pstate, &sf->uc.uc_mcontext.pstate, err);
789 __put_user_error(current->thread.fault_address, &sf->uc.uc_mcontext.fault_address, err);
791 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
793 if (err == 0 && system_supports_fpsimd()) {
794 struct fpsimd_context __user *fpsimd_ctx =
795 apply_user_offset(user, user->fpsimd_offset);
796 err |= preserve_fpsimd_context(fpsimd_ctx);
799 /* fault information, if valid */
800 if (err == 0 && user->esr_offset) {
801 struct esr_context __user *esr_ctx =
802 apply_user_offset(user, user->esr_offset);
804 __put_user_error(ESR_MAGIC, &esr_ctx->head.magic, err);
805 __put_user_error(sizeof(*esr_ctx), &esr_ctx->head.size, err);
806 __put_user_error(current->thread.fault_code, &esr_ctx->esr, err);
809 /* Scalable Vector Extension state (including streaming), if present */
810 if ((system_supports_sve() || system_supports_sme()) &&
811 err == 0 && user->sve_offset) {
812 struct sve_context __user *sve_ctx =
813 apply_user_offset(user, user->sve_offset);
814 err |= preserve_sve_context(sve_ctx);
817 /* ZA state if present */
818 if (system_supports_sme() && err == 0 && user->za_offset) {
819 struct za_context __user *za_ctx =
820 apply_user_offset(user, user->za_offset);
821 err |= preserve_za_context(za_ctx);
824 if (err == 0 && user->extra_offset) {
825 char __user *sfp = (char __user *)user->sigframe;
827 apply_user_offset(user, user->extra_offset);
829 struct extra_context __user *extra;
830 struct _aarch64_ctx __user *end;
834 extra = (struct extra_context __user *)userp;
835 userp += EXTRA_CONTEXT_SIZE;
837 end = (struct _aarch64_ctx __user *)userp;
838 userp += TERMINATOR_SIZE;
841 * extra_datap is just written to the signal frame.
842 * The value gets cast back to a void __user *
845 extra_datap = (__force u64)userp;
846 extra_size = sfp + round_up(user->size, 16) - userp;
848 __put_user_error(EXTRA_MAGIC, &extra->head.magic, err);
849 __put_user_error(EXTRA_CONTEXT_SIZE, &extra->head.size, err);
850 __put_user_error(extra_datap, &extra->datap, err);
851 __put_user_error(extra_size, &extra->size, err);
853 /* Add the terminator */
854 __put_user_error(0, &end->magic, err);
855 __put_user_error(0, &end->size, err);
858 /* set the "end" magic */
860 struct _aarch64_ctx __user *end =
861 apply_user_offset(user, user->end_offset);
863 __put_user_error(0, &end->magic, err);
864 __put_user_error(0, &end->size, err);
870 static int get_sigframe(struct rt_sigframe_user_layout *user,
871 struct ksignal *ksig, struct pt_regs *regs)
873 unsigned long sp, sp_top;
876 init_user_layout(user);
877 err = setup_sigframe_layout(user, false);
881 sp = sp_top = sigsp(regs->sp, ksig);
883 sp = round_down(sp - sizeof(struct frame_record), 16);
884 user->next_frame = (struct frame_record __user *)sp;
886 sp = round_down(sp, 16) - sigframe_size(user);
887 user->sigframe = (struct rt_sigframe __user *)sp;
890 * Check that we can actually write to the signal frame.
892 if (!access_ok(user->sigframe, sp_top - sp))
898 static void setup_return(struct pt_regs *regs, struct k_sigaction *ka,
899 struct rt_sigframe_user_layout *user, int usig)
901 __sigrestore_t sigtramp;
903 regs->regs[0] = usig;
904 regs->sp = (unsigned long)user->sigframe;
905 regs->regs[29] = (unsigned long)&user->next_frame->fp;
906 regs->pc = (unsigned long)ka->sa.sa_handler;
909 * Signal delivery is a (wacky) indirect function call in
910 * userspace, so simulate the same setting of BTYPE as a BLR
911 * <register containing the signal handler entry point>.
912 * Signal delivery to a location in a PROT_BTI guarded page
913 * that is not a function entry point will now trigger a
914 * SIGILL in userspace.
916 * If the signal handler entry point is not in a PROT_BTI
917 * guarded page, this is harmless.
919 if (system_supports_bti()) {
920 regs->pstate &= ~PSR_BTYPE_MASK;
921 regs->pstate |= PSR_BTYPE_C;
924 /* TCO (Tag Check Override) always cleared for signal handlers */
925 regs->pstate &= ~PSR_TCO_BIT;
927 /* Signal handlers are invoked with ZA and streaming mode disabled */
928 if (system_supports_sme()) {
929 current->thread.svcr &= ~(SVCR_ZA_MASK |
934 if (ka->sa.sa_flags & SA_RESTORER)
935 sigtramp = ka->sa.sa_restorer;
937 sigtramp = VDSO_SYMBOL(current->mm->context.vdso, sigtramp);
939 regs->regs[30] = (unsigned long)sigtramp;
942 static int setup_rt_frame(int usig, struct ksignal *ksig, sigset_t *set,
943 struct pt_regs *regs)
945 struct rt_sigframe_user_layout user;
946 struct rt_sigframe __user *frame;
949 fpsimd_signal_preserve_current_state();
951 if (get_sigframe(&user, ksig, regs))
954 frame = user.sigframe;
956 __put_user_error(0, &frame->uc.uc_flags, err);
957 __put_user_error(NULL, &frame->uc.uc_link, err);
959 err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
960 err |= setup_sigframe(&user, regs, set);
962 setup_return(regs, &ksig->ka, &user, usig);
963 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
964 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
965 regs->regs[1] = (unsigned long)&frame->info;
966 regs->regs[2] = (unsigned long)&frame->uc;
973 static void setup_restart_syscall(struct pt_regs *regs)
975 if (is_compat_task())
976 compat_setup_restart_syscall(regs);
978 regs->regs[8] = __NR_restart_syscall;
982 * OK, we're invoking a handler
984 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
986 sigset_t *oldset = sigmask_to_save();
987 int usig = ksig->sig;
990 rseq_signal_deliver(ksig, regs);
993 * Set up the stack frame
995 if (is_compat_task()) {
996 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
997 ret = compat_setup_rt_frame(usig, ksig, oldset, regs);
999 ret = compat_setup_frame(usig, ksig, oldset, regs);
1001 ret = setup_rt_frame(usig, ksig, oldset, regs);
1005 * Check that the resulting registers are actually sane.
1007 ret |= !valid_user_regs(®s->user_regs, current);
1009 /* Step into the signal handler if we are stepping */
1010 signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
1014 * Note that 'init' is a special process: it doesn't get signals it doesn't
1015 * want to handle. Thus you cannot kill init even with a SIGKILL even by
1018 * Note that we go through the signals twice: once to check the signals that
1019 * the kernel can handle, and then we build all the user-level signal handling
1020 * stack-frames in one go after that.
1022 static void do_signal(struct pt_regs *regs)
1024 unsigned long continue_addr = 0, restart_addr = 0;
1026 struct ksignal ksig;
1027 bool syscall = in_syscall(regs);
1030 * If we were from a system call, check for system call restarting...
1033 continue_addr = regs->pc;
1034 restart_addr = continue_addr - (compat_thumb_mode(regs) ? 2 : 4);
1035 retval = regs->regs[0];
1038 * Avoid additional syscall restarting via ret_to_user.
1040 forget_syscall(regs);
1043 * Prepare for system call restart. We do this here so that a
1044 * debugger will see the already changed PC.
1047 case -ERESTARTNOHAND:
1049 case -ERESTARTNOINTR:
1050 case -ERESTART_RESTARTBLOCK:
1051 regs->regs[0] = regs->orig_x0;
1052 regs->pc = restart_addr;
1058 * Get the signal to deliver. When running under ptrace, at this point
1059 * the debugger may change all of our registers.
1061 if (get_signal(&ksig)) {
1063 * Depending on the signal settings, we may need to revert the
1064 * decision to restart the system call, but skip this if a
1065 * debugger has chosen to restart at a different PC.
1067 if (regs->pc == restart_addr &&
1068 (retval == -ERESTARTNOHAND ||
1069 retval == -ERESTART_RESTARTBLOCK ||
1070 (retval == -ERESTARTSYS &&
1071 !(ksig.ka.sa.sa_flags & SA_RESTART)))) {
1072 syscall_set_return_value(current, regs, -EINTR, 0);
1073 regs->pc = continue_addr;
1076 handle_signal(&ksig, regs);
1081 * Handle restarting a different system call. As above, if a debugger
1082 * has chosen to restart at a different PC, ignore the restart.
1084 if (syscall && regs->pc == restart_addr) {
1085 if (retval == -ERESTART_RESTARTBLOCK)
1086 setup_restart_syscall(regs);
1087 user_rewind_single_step(current);
1090 restore_saved_sigmask();
1093 void do_notify_resume(struct pt_regs *regs, unsigned long thread_flags)
1096 if (thread_flags & _TIF_NEED_RESCHED) {
1097 /* Unmask Debug and SError for the next task */
1098 local_daif_restore(DAIF_PROCCTX_NOIRQ);
1102 local_daif_restore(DAIF_PROCCTX);
1104 if (thread_flags & _TIF_UPROBE)
1105 uprobe_notify_resume(regs);
1107 if (thread_flags & _TIF_MTE_ASYNC_FAULT) {
1108 clear_thread_flag(TIF_MTE_ASYNC_FAULT);
1109 send_sig_fault(SIGSEGV, SEGV_MTEAERR,
1110 (void __user *)NULL, current);
1113 if (thread_flags & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL))
1116 if (thread_flags & _TIF_NOTIFY_RESUME)
1117 resume_user_mode_work(regs);
1119 if (thread_flags & _TIF_FOREIGN_FPSTATE)
1120 fpsimd_restore_current_state();
1124 thread_flags = read_thread_flags();
1125 } while (thread_flags & _TIF_WORK_MASK);
1128 unsigned long __ro_after_init signal_minsigstksz;
1131 * Determine the stack space required for guaranteed signal devliery.
1132 * This function is used to populate AT_MINSIGSTKSZ at process startup.
1133 * cpufeatures setup is assumed to be complete.
1135 void __init minsigstksz_setup(void)
1137 struct rt_sigframe_user_layout user;
1139 init_user_layout(&user);
1142 * If this fails, SIGFRAME_MAXSZ needs to be enlarged. It won't
1143 * be big enough, but it's our best guess:
1145 if (WARN_ON(setup_sigframe_layout(&user, true)))
1148 signal_minsigstksz = sigframe_size(&user) +
1149 round_up(sizeof(struct frame_record), 16) +
1150 16; /* max alignment padding */
1154 * Compile-time assertions for siginfo_t offsets. Check NSIG* as well, as
1155 * changes likely come with new fields that should be added below.
1157 static_assert(NSIGILL == 11);
1158 static_assert(NSIGFPE == 15);
1159 static_assert(NSIGSEGV == 9);
1160 static_assert(NSIGBUS == 5);
1161 static_assert(NSIGTRAP == 6);
1162 static_assert(NSIGCHLD == 6);
1163 static_assert(NSIGSYS == 2);
1164 static_assert(sizeof(siginfo_t) == 128);
1165 static_assert(__alignof__(siginfo_t) == 8);
1166 static_assert(offsetof(siginfo_t, si_signo) == 0x00);
1167 static_assert(offsetof(siginfo_t, si_errno) == 0x04);
1168 static_assert(offsetof(siginfo_t, si_code) == 0x08);
1169 static_assert(offsetof(siginfo_t, si_pid) == 0x10);
1170 static_assert(offsetof(siginfo_t, si_uid) == 0x14);
1171 static_assert(offsetof(siginfo_t, si_tid) == 0x10);
1172 static_assert(offsetof(siginfo_t, si_overrun) == 0x14);
1173 static_assert(offsetof(siginfo_t, si_status) == 0x18);
1174 static_assert(offsetof(siginfo_t, si_utime) == 0x20);
1175 static_assert(offsetof(siginfo_t, si_stime) == 0x28);
1176 static_assert(offsetof(siginfo_t, si_value) == 0x18);
1177 static_assert(offsetof(siginfo_t, si_int) == 0x18);
1178 static_assert(offsetof(siginfo_t, si_ptr) == 0x18);
1179 static_assert(offsetof(siginfo_t, si_addr) == 0x10);
1180 static_assert(offsetof(siginfo_t, si_addr_lsb) == 0x18);
1181 static_assert(offsetof(siginfo_t, si_lower) == 0x20);
1182 static_assert(offsetof(siginfo_t, si_upper) == 0x28);
1183 static_assert(offsetof(siginfo_t, si_pkey) == 0x20);
1184 static_assert(offsetof(siginfo_t, si_perf_data) == 0x18);
1185 static_assert(offsetof(siginfo_t, si_perf_type) == 0x20);
1186 static_assert(offsetof(siginfo_t, si_perf_flags) == 0x24);
1187 static_assert(offsetof(siginfo_t, si_band) == 0x10);
1188 static_assert(offsetof(siginfo_t, si_fd) == 0x18);
1189 static_assert(offsetof(siginfo_t, si_call_addr) == 0x10);
1190 static_assert(offsetof(siginfo_t, si_syscall) == 0x18);
1191 static_assert(offsetof(siginfo_t, si_arch) == 0x1c);