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/exception.h>
27 #include <asm/cacheflush.h>
28 #include <asm/ucontext.h>
29 #include <asm/unistd.h>
30 #include <asm/fpsimd.h>
31 #include <asm/ptrace.h>
32 #include <asm/syscall.h>
33 #include <asm/signal32.h>
34 #include <asm/traps.h>
38 * Do a signal return; undo the signal stack. These are aligned to 128-bit.
50 struct rt_sigframe_user_layout {
51 struct rt_sigframe __user *sigframe;
52 struct frame_record __user *next_frame;
54 unsigned long size; /* size of allocated sigframe data */
55 unsigned long limit; /* largest allowed size */
57 unsigned long fpsimd_offset;
58 unsigned long esr_offset;
59 unsigned long sve_offset;
60 unsigned long tpidr2_offset;
61 unsigned long za_offset;
62 unsigned long zt_offset;
63 unsigned long extra_offset;
64 unsigned long end_offset;
67 #define BASE_SIGFRAME_SIZE round_up(sizeof(struct rt_sigframe), 16)
68 #define TERMINATOR_SIZE round_up(sizeof(struct _aarch64_ctx), 16)
69 #define EXTRA_CONTEXT_SIZE round_up(sizeof(struct extra_context), 16)
71 static void init_user_layout(struct rt_sigframe_user_layout *user)
73 const size_t reserved_size =
74 sizeof(user->sigframe->uc.uc_mcontext.__reserved);
76 memset(user, 0, sizeof(*user));
77 user->size = offsetof(struct rt_sigframe, uc.uc_mcontext.__reserved);
79 user->limit = user->size + reserved_size;
81 user->limit -= TERMINATOR_SIZE;
82 user->limit -= EXTRA_CONTEXT_SIZE;
83 /* Reserve space for extension and terminator ^ */
86 static size_t sigframe_size(struct rt_sigframe_user_layout const *user)
88 return round_up(max(user->size, sizeof(struct rt_sigframe)), 16);
92 * Sanity limit on the approximate maximum size of signal frame we'll
93 * try to generate. Stack alignment padding and the frame record are
94 * not taken into account. This limit is not a guarantee and is
97 #define SIGFRAME_MAXSZ SZ_256K
99 static int __sigframe_alloc(struct rt_sigframe_user_layout *user,
100 unsigned long *offset, size_t size, bool extend)
102 size_t padded_size = round_up(size, 16);
104 if (padded_size > user->limit - user->size &&
105 !user->extra_offset &&
109 user->limit += EXTRA_CONTEXT_SIZE;
110 ret = __sigframe_alloc(user, &user->extra_offset,
111 sizeof(struct extra_context), false);
113 user->limit -= EXTRA_CONTEXT_SIZE;
117 /* Reserve space for the __reserved[] terminator */
118 user->size += TERMINATOR_SIZE;
121 * Allow expansion up to SIGFRAME_MAXSZ, ensuring space for
124 user->limit = SIGFRAME_MAXSZ - TERMINATOR_SIZE;
127 /* Still not enough space? Bad luck! */
128 if (padded_size > user->limit - user->size)
131 *offset = user->size;
132 user->size += padded_size;
138 * Allocate space for an optional record of <size> bytes in the user
139 * signal frame. The offset from the signal frame base address to the
140 * allocated block is assigned to *offset.
142 static int sigframe_alloc(struct rt_sigframe_user_layout *user,
143 unsigned long *offset, size_t size)
145 return __sigframe_alloc(user, offset, size, true);
148 /* Allocate the null terminator record and prevent further allocations */
149 static int sigframe_alloc_end(struct rt_sigframe_user_layout *user)
153 /* Un-reserve the space reserved for the terminator: */
154 user->limit += TERMINATOR_SIZE;
156 ret = sigframe_alloc(user, &user->end_offset,
157 sizeof(struct _aarch64_ctx));
161 /* Prevent further allocation: */
162 user->limit = user->size;
166 static void __user *apply_user_offset(
167 struct rt_sigframe_user_layout const *user, unsigned long offset)
169 char __user *base = (char __user *)user->sigframe;
171 return base + offset;
175 struct fpsimd_context __user *fpsimd;
177 struct sve_context __user *sve;
179 struct tpidr2_context __user *tpidr2;
181 struct za_context __user *za;
183 struct zt_context __user *zt;
187 static int preserve_fpsimd_context(struct fpsimd_context __user *ctx)
189 struct user_fpsimd_state const *fpsimd =
190 ¤t->thread.uw.fpsimd_state;
193 /* copy the FP and status/control registers */
194 err = __copy_to_user(ctx->vregs, fpsimd->vregs, sizeof(fpsimd->vregs));
195 __put_user_error(fpsimd->fpsr, &ctx->fpsr, err);
196 __put_user_error(fpsimd->fpcr, &ctx->fpcr, err);
198 /* copy the magic/size information */
199 __put_user_error(FPSIMD_MAGIC, &ctx->head.magic, err);
200 __put_user_error(sizeof(struct fpsimd_context), &ctx->head.size, err);
202 return err ? -EFAULT : 0;
205 static int restore_fpsimd_context(struct user_ctxs *user)
207 struct user_fpsimd_state fpsimd;
210 /* check the size information */
211 if (user->fpsimd_size != sizeof(struct fpsimd_context))
214 /* copy the FP and status/control registers */
215 err = __copy_from_user(fpsimd.vregs, &(user->fpsimd->vregs),
216 sizeof(fpsimd.vregs));
217 __get_user_error(fpsimd.fpsr, &(user->fpsimd->fpsr), err);
218 __get_user_error(fpsimd.fpcr, &(user->fpsimd->fpcr), err);
220 clear_thread_flag(TIF_SVE);
221 current->thread.fp_type = FP_STATE_FPSIMD;
223 /* load the hardware registers from the fpsimd_state structure */
225 fpsimd_update_current_state(&fpsimd);
227 return err ? -EFAULT : 0;
231 #ifdef CONFIG_ARM64_SVE
233 static int preserve_sve_context(struct sve_context __user *ctx)
236 u16 reserved[ARRAY_SIZE(ctx->__reserved)];
238 unsigned int vl = task_get_sve_vl(current);
241 if (thread_sm_enabled(¤t->thread)) {
242 vl = task_get_sme_vl(current);
243 vq = sve_vq_from_vl(vl);
244 flags |= SVE_SIG_FLAG_SM;
245 } else if (test_thread_flag(TIF_SVE)) {
246 vq = sve_vq_from_vl(vl);
249 memset(reserved, 0, sizeof(reserved));
251 __put_user_error(SVE_MAGIC, &ctx->head.magic, err);
252 __put_user_error(round_up(SVE_SIG_CONTEXT_SIZE(vq), 16),
253 &ctx->head.size, err);
254 __put_user_error(vl, &ctx->vl, err);
255 __put_user_error(flags, &ctx->flags, err);
256 BUILD_BUG_ON(sizeof(ctx->__reserved) != sizeof(reserved));
257 err |= __copy_to_user(&ctx->__reserved, reserved, sizeof(reserved));
261 * This assumes that the SVE state has already been saved to
262 * the task struct by calling the function
263 * fpsimd_signal_preserve_current_state().
265 err |= __copy_to_user((char __user *)ctx + SVE_SIG_REGS_OFFSET,
266 current->thread.sve_state,
267 SVE_SIG_REGS_SIZE(vq));
270 return err ? -EFAULT : 0;
273 static int restore_sve_fpsimd_context(struct user_ctxs *user)
277 struct user_fpsimd_state fpsimd;
280 if (user->sve_size < sizeof(*user->sve))
283 __get_user_error(user_vl, &(user->sve->vl), err);
284 __get_user_error(flags, &(user->sve->flags), err);
288 if (flags & SVE_SIG_FLAG_SM) {
289 if (!system_supports_sme())
292 vl = task_get_sme_vl(current);
295 * A SME only system use SVE for streaming mode so can
296 * have a SVE formatted context with a zero VL and no
299 if (!system_supports_sve() && !system_supports_sme())
302 vl = task_get_sve_vl(current);
308 if (user->sve_size == sizeof(*user->sve)) {
309 clear_thread_flag(TIF_SVE);
310 current->thread.svcr &= ~SVCR_SM_MASK;
311 current->thread.fp_type = FP_STATE_FPSIMD;
315 vq = sve_vq_from_vl(vl);
317 if (user->sve_size < SVE_SIG_CONTEXT_SIZE(vq))
321 * Careful: we are about __copy_from_user() directly into
322 * thread.sve_state with preemption enabled, so protection is
323 * needed to prevent a racing context switch from writing stale
324 * registers back over the new data.
327 fpsimd_flush_task_state(current);
328 /* From now, fpsimd_thread_switch() won't touch thread.sve_state */
330 sve_alloc(current, true);
331 if (!current->thread.sve_state) {
332 clear_thread_flag(TIF_SVE);
336 err = __copy_from_user(current->thread.sve_state,
337 (char __user const *)user->sve +
339 SVE_SIG_REGS_SIZE(vq));
343 if (flags & SVE_SIG_FLAG_SM)
344 current->thread.svcr |= SVCR_SM_MASK;
346 set_thread_flag(TIF_SVE);
347 current->thread.fp_type = FP_STATE_SVE;
350 /* copy the FP and status/control registers */
351 /* restore_sigframe() already checked that user->fpsimd != NULL. */
352 err = __copy_from_user(fpsimd.vregs, user->fpsimd->vregs,
353 sizeof(fpsimd.vregs));
354 __get_user_error(fpsimd.fpsr, &user->fpsimd->fpsr, err);
355 __get_user_error(fpsimd.fpcr, &user->fpsimd->fpcr, err);
357 /* load the hardware registers from the fpsimd_state structure */
359 fpsimd_update_current_state(&fpsimd);
361 return err ? -EFAULT : 0;
364 #else /* ! CONFIG_ARM64_SVE */
366 static int restore_sve_fpsimd_context(struct user_ctxs *user)
372 /* Turn any non-optimised out attempts to use this into a link error: */
373 extern int preserve_sve_context(void __user *ctx);
375 #endif /* ! CONFIG_ARM64_SVE */
377 #ifdef CONFIG_ARM64_SME
379 static int preserve_tpidr2_context(struct tpidr2_context __user *ctx)
383 current->thread.tpidr2_el0 = read_sysreg_s(SYS_TPIDR2_EL0);
385 __put_user_error(TPIDR2_MAGIC, &ctx->head.magic, err);
386 __put_user_error(sizeof(*ctx), &ctx->head.size, err);
387 __put_user_error(current->thread.tpidr2_el0, &ctx->tpidr2, err);
392 static int restore_tpidr2_context(struct user_ctxs *user)
397 if (user->tpidr2_size != sizeof(*user->tpidr2))
400 __get_user_error(tpidr2_el0, &user->tpidr2->tpidr2, err);
402 write_sysreg_s(tpidr2_el0, SYS_TPIDR2_EL0);
407 static int preserve_za_context(struct za_context __user *ctx)
410 u16 reserved[ARRAY_SIZE(ctx->__reserved)];
411 unsigned int vl = task_get_sme_vl(current);
414 if (thread_za_enabled(¤t->thread))
415 vq = sve_vq_from_vl(vl);
419 memset(reserved, 0, sizeof(reserved));
421 __put_user_error(ZA_MAGIC, &ctx->head.magic, err);
422 __put_user_error(round_up(ZA_SIG_CONTEXT_SIZE(vq), 16),
423 &ctx->head.size, err);
424 __put_user_error(vl, &ctx->vl, err);
425 BUILD_BUG_ON(sizeof(ctx->__reserved) != sizeof(reserved));
426 err |= __copy_to_user(&ctx->__reserved, reserved, sizeof(reserved));
430 * This assumes that the ZA state has already been saved to
431 * the task struct by calling the function
432 * fpsimd_signal_preserve_current_state().
434 err |= __copy_to_user((char __user *)ctx + ZA_SIG_REGS_OFFSET,
435 current->thread.sme_state,
436 ZA_SIG_REGS_SIZE(vq));
439 return err ? -EFAULT : 0;
442 static int restore_za_context(struct user_ctxs *user)
448 if (user->za_size < sizeof(*user->za))
451 __get_user_error(user_vl, &(user->za->vl), err);
455 if (user_vl != task_get_sme_vl(current))
458 if (user->za_size == sizeof(*user->za)) {
459 current->thread.svcr &= ~SVCR_ZA_MASK;
463 vq = sve_vq_from_vl(user_vl);
465 if (user->za_size < ZA_SIG_CONTEXT_SIZE(vq))
469 * Careful: we are about __copy_from_user() directly into
470 * thread.sme_state with preemption enabled, so protection is
471 * needed to prevent a racing context switch from writing stale
472 * registers back over the new data.
475 fpsimd_flush_task_state(current);
476 /* From now, fpsimd_thread_switch() won't touch thread.sve_state */
479 if (!current->thread.sme_state) {
480 current->thread.svcr &= ~SVCR_ZA_MASK;
481 clear_thread_flag(TIF_SME);
485 err = __copy_from_user(current->thread.sme_state,
486 (char __user const *)user->za +
488 ZA_SIG_REGS_SIZE(vq));
492 set_thread_flag(TIF_SME);
493 current->thread.svcr |= SVCR_ZA_MASK;
498 static int preserve_zt_context(struct zt_context __user *ctx)
501 u16 reserved[ARRAY_SIZE(ctx->__reserved)];
503 if (WARN_ON(!thread_za_enabled(¤t->thread)))
506 memset(reserved, 0, sizeof(reserved));
508 __put_user_error(ZT_MAGIC, &ctx->head.magic, err);
509 __put_user_error(round_up(ZT_SIG_CONTEXT_SIZE(1), 16),
510 &ctx->head.size, err);
511 __put_user_error(1, &ctx->nregs, err);
512 BUILD_BUG_ON(sizeof(ctx->__reserved) != sizeof(reserved));
513 err |= __copy_to_user(&ctx->__reserved, reserved, sizeof(reserved));
516 * This assumes that the ZT state has already been saved to
517 * the task struct by calling the function
518 * fpsimd_signal_preserve_current_state().
520 err |= __copy_to_user((char __user *)ctx + ZT_SIG_REGS_OFFSET,
521 thread_zt_state(¤t->thread),
522 ZT_SIG_REGS_SIZE(1));
524 return err ? -EFAULT : 0;
527 static int restore_zt_context(struct user_ctxs *user)
532 /* ZA must be restored first for this check to be valid */
533 if (!thread_za_enabled(¤t->thread))
536 if (user->zt_size != ZT_SIG_CONTEXT_SIZE(1))
539 if (__copy_from_user(&nregs, &(user->zt->nregs), sizeof(nregs)))
546 * Careful: we are about __copy_from_user() directly into
547 * thread.zt_state with preemption enabled, so protection is
548 * needed to prevent a racing context switch from writing stale
549 * registers back over the new data.
552 fpsimd_flush_task_state(current);
553 /* From now, fpsimd_thread_switch() won't touch ZT in thread state */
555 err = __copy_from_user(thread_zt_state(¤t->thread),
556 (char __user const *)user->zt +
558 ZT_SIG_REGS_SIZE(1));
565 #else /* ! CONFIG_ARM64_SME */
567 /* Turn any non-optimised out attempts to use these into a link error: */
568 extern int preserve_tpidr2_context(void __user *ctx);
569 extern int restore_tpidr2_context(struct user_ctxs *user);
570 extern int preserve_za_context(void __user *ctx);
571 extern int restore_za_context(struct user_ctxs *user);
572 extern int preserve_zt_context(void __user *ctx);
573 extern int restore_zt_context(struct user_ctxs *user);
575 #endif /* ! CONFIG_ARM64_SME */
577 static int parse_user_sigframe(struct user_ctxs *user,
578 struct rt_sigframe __user *sf)
580 struct sigcontext __user *const sc = &sf->uc.uc_mcontext;
581 struct _aarch64_ctx __user *head;
582 char __user *base = (char __user *)&sc->__reserved;
584 size_t limit = sizeof(sc->__reserved);
585 bool have_extra_context = false;
586 char const __user *const sfp = (char const __user *)sf;
594 if (!IS_ALIGNED((unsigned long)base, 16))
600 char const __user *userp;
601 struct extra_context const __user *extra;
604 struct _aarch64_ctx const __user *end;
605 u32 end_magic, end_size;
607 if (limit - offset < sizeof(*head))
610 if (!IS_ALIGNED(offset, 16))
613 head = (struct _aarch64_ctx __user *)(base + offset);
614 __get_user_error(magic, &head->magic, err);
615 __get_user_error(size, &head->size, err);
619 if (limit - offset < size)
630 if (!system_supports_fpsimd())
635 user->fpsimd = (struct fpsimd_context __user *)head;
636 user->fpsimd_size = size;
644 if (!system_supports_sve() && !system_supports_sme())
650 user->sve = (struct sve_context __user *)head;
651 user->sve_size = size;
655 if (!system_supports_tpidr2())
661 user->tpidr2 = (struct tpidr2_context __user *)head;
662 user->tpidr2_size = size;
666 if (!system_supports_sme())
672 user->za = (struct za_context __user *)head;
673 user->za_size = size;
677 if (!system_supports_sme2())
683 user->zt = (struct zt_context __user *)head;
684 user->zt_size = size;
688 if (have_extra_context)
691 if (size < sizeof(*extra))
694 userp = (char const __user *)head;
696 extra = (struct extra_context const __user *)userp;
699 __get_user_error(extra_datap, &extra->datap, err);
700 __get_user_error(extra_size, &extra->size, err);
704 /* Check for the dummy terminator in __reserved[]: */
706 if (limit - offset - size < TERMINATOR_SIZE)
709 end = (struct _aarch64_ctx const __user *)userp;
710 userp += TERMINATOR_SIZE;
712 __get_user_error(end_magic, &end->magic, err);
713 __get_user_error(end_size, &end->size, err);
717 if (end_magic || end_size)
720 /* Prevent looping/repeated parsing of extra_context */
721 have_extra_context = true;
723 base = (__force void __user *)extra_datap;
724 if (!IS_ALIGNED((unsigned long)base, 16))
727 if (!IS_ALIGNED(extra_size, 16))
733 /* Reject "unreasonably large" frames: */
734 if (extra_size > sfp + SIGFRAME_MAXSZ - userp)
738 * Ignore trailing terminator in __reserved[]
739 * and start parsing extra data:
744 if (!access_ok(base, limit))
753 if (size < sizeof(*head))
756 if (limit - offset < size)
769 static int restore_sigframe(struct pt_regs *regs,
770 struct rt_sigframe __user *sf)
774 struct user_ctxs user;
776 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
778 set_current_blocked(&set);
780 for (i = 0; i < 31; i++)
781 __get_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i],
783 __get_user_error(regs->sp, &sf->uc.uc_mcontext.sp, err);
784 __get_user_error(regs->pc, &sf->uc.uc_mcontext.pc, err);
785 __get_user_error(regs->pstate, &sf->uc.uc_mcontext.pstate, err);
788 * Avoid sys_rt_sigreturn() restarting.
790 forget_syscall(regs);
792 err |= !valid_user_regs(®s->user_regs, current);
794 err = parse_user_sigframe(&user, sf);
796 if (err == 0 && system_supports_fpsimd()) {
801 err = restore_sve_fpsimd_context(&user);
803 err = restore_fpsimd_context(&user);
806 if (err == 0 && system_supports_tpidr2() && user.tpidr2)
807 err = restore_tpidr2_context(&user);
809 if (err == 0 && system_supports_sme() && user.za)
810 err = restore_za_context(&user);
812 if (err == 0 && system_supports_sme2() && user.zt)
813 err = restore_zt_context(&user);
818 SYSCALL_DEFINE0(rt_sigreturn)
820 struct pt_regs *regs = current_pt_regs();
821 struct rt_sigframe __user *frame;
823 /* Always make any pending restarted system calls return -EINTR */
824 current->restart_block.fn = do_no_restart_syscall;
827 * Since we stacked the signal on a 128-bit boundary, then 'sp' should
828 * be word aligned here.
833 frame = (struct rt_sigframe __user *)regs->sp;
835 if (!access_ok(frame, sizeof (*frame)))
838 if (restore_sigframe(regs, frame))
841 if (restore_altstack(&frame->uc.uc_stack))
844 return regs->regs[0];
847 arm64_notify_segfault(regs->sp);
852 * Determine the layout of optional records in the signal frame
854 * add_all: if true, lays out the biggest possible signal frame for
855 * this task; otherwise, generates a layout for the current state
858 static int setup_sigframe_layout(struct rt_sigframe_user_layout *user,
863 if (system_supports_fpsimd()) {
864 err = sigframe_alloc(user, &user->fpsimd_offset,
865 sizeof(struct fpsimd_context));
870 /* fault information, if valid */
871 if (add_all || current->thread.fault_code) {
872 err = sigframe_alloc(user, &user->esr_offset,
873 sizeof(struct esr_context));
878 if (system_supports_sve() || system_supports_sme()) {
881 if (add_all || test_thread_flag(TIF_SVE) ||
882 thread_sm_enabled(¤t->thread)) {
883 int vl = max(sve_max_vl(), sme_max_vl());
886 vl = thread_get_cur_vl(¤t->thread);
888 vq = sve_vq_from_vl(vl);
891 err = sigframe_alloc(user, &user->sve_offset,
892 SVE_SIG_CONTEXT_SIZE(vq));
897 if (system_supports_tpidr2()) {
898 err = sigframe_alloc(user, &user->tpidr2_offset,
899 sizeof(struct tpidr2_context));
904 if (system_supports_sme()) {
911 vl = task_get_sme_vl(current);
913 if (thread_za_enabled(¤t->thread))
914 vq = sve_vq_from_vl(vl);
916 err = sigframe_alloc(user, &user->za_offset,
917 ZA_SIG_CONTEXT_SIZE(vq));
922 if (system_supports_sme2()) {
923 if (add_all || thread_za_enabled(¤t->thread)) {
924 err = sigframe_alloc(user, &user->zt_offset,
925 ZT_SIG_CONTEXT_SIZE(1));
931 return sigframe_alloc_end(user);
934 static int setup_sigframe(struct rt_sigframe_user_layout *user,
935 struct pt_regs *regs, sigset_t *set)
938 struct rt_sigframe __user *sf = user->sigframe;
940 /* set up the stack frame for unwinding */
941 __put_user_error(regs->regs[29], &user->next_frame->fp, err);
942 __put_user_error(regs->regs[30], &user->next_frame->lr, err);
944 for (i = 0; i < 31; i++)
945 __put_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i],
947 __put_user_error(regs->sp, &sf->uc.uc_mcontext.sp, err);
948 __put_user_error(regs->pc, &sf->uc.uc_mcontext.pc, err);
949 __put_user_error(regs->pstate, &sf->uc.uc_mcontext.pstate, err);
951 __put_user_error(current->thread.fault_address, &sf->uc.uc_mcontext.fault_address, err);
953 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
955 if (err == 0 && system_supports_fpsimd()) {
956 struct fpsimd_context __user *fpsimd_ctx =
957 apply_user_offset(user, user->fpsimd_offset);
958 err |= preserve_fpsimd_context(fpsimd_ctx);
961 /* fault information, if valid */
962 if (err == 0 && user->esr_offset) {
963 struct esr_context __user *esr_ctx =
964 apply_user_offset(user, user->esr_offset);
966 __put_user_error(ESR_MAGIC, &esr_ctx->head.magic, err);
967 __put_user_error(sizeof(*esr_ctx), &esr_ctx->head.size, err);
968 __put_user_error(current->thread.fault_code, &esr_ctx->esr, err);
971 /* Scalable Vector Extension state (including streaming), if present */
972 if ((system_supports_sve() || system_supports_sme()) &&
973 err == 0 && user->sve_offset) {
974 struct sve_context __user *sve_ctx =
975 apply_user_offset(user, user->sve_offset);
976 err |= preserve_sve_context(sve_ctx);
979 /* TPIDR2 if supported */
980 if (system_supports_tpidr2() && err == 0) {
981 struct tpidr2_context __user *tpidr2_ctx =
982 apply_user_offset(user, user->tpidr2_offset);
983 err |= preserve_tpidr2_context(tpidr2_ctx);
986 /* ZA state if present */
987 if (system_supports_sme() && err == 0 && user->za_offset) {
988 struct za_context __user *za_ctx =
989 apply_user_offset(user, user->za_offset);
990 err |= preserve_za_context(za_ctx);
993 /* ZT state if present */
994 if (system_supports_sme2() && err == 0 && user->zt_offset) {
995 struct zt_context __user *zt_ctx =
996 apply_user_offset(user, user->zt_offset);
997 err |= preserve_zt_context(zt_ctx);
1000 if (err == 0 && user->extra_offset) {
1001 char __user *sfp = (char __user *)user->sigframe;
1002 char __user *userp =
1003 apply_user_offset(user, user->extra_offset);
1005 struct extra_context __user *extra;
1006 struct _aarch64_ctx __user *end;
1010 extra = (struct extra_context __user *)userp;
1011 userp += EXTRA_CONTEXT_SIZE;
1013 end = (struct _aarch64_ctx __user *)userp;
1014 userp += TERMINATOR_SIZE;
1017 * extra_datap is just written to the signal frame.
1018 * The value gets cast back to a void __user *
1021 extra_datap = (__force u64)userp;
1022 extra_size = sfp + round_up(user->size, 16) - userp;
1024 __put_user_error(EXTRA_MAGIC, &extra->head.magic, err);
1025 __put_user_error(EXTRA_CONTEXT_SIZE, &extra->head.size, err);
1026 __put_user_error(extra_datap, &extra->datap, err);
1027 __put_user_error(extra_size, &extra->size, err);
1029 /* Add the terminator */
1030 __put_user_error(0, &end->magic, err);
1031 __put_user_error(0, &end->size, err);
1034 /* set the "end" magic */
1036 struct _aarch64_ctx __user *end =
1037 apply_user_offset(user, user->end_offset);
1039 __put_user_error(0, &end->magic, err);
1040 __put_user_error(0, &end->size, err);
1046 static int get_sigframe(struct rt_sigframe_user_layout *user,
1047 struct ksignal *ksig, struct pt_regs *regs)
1049 unsigned long sp, sp_top;
1052 init_user_layout(user);
1053 err = setup_sigframe_layout(user, false);
1057 sp = sp_top = sigsp(regs->sp, ksig);
1059 sp = round_down(sp - sizeof(struct frame_record), 16);
1060 user->next_frame = (struct frame_record __user *)sp;
1062 sp = round_down(sp, 16) - sigframe_size(user);
1063 user->sigframe = (struct rt_sigframe __user *)sp;
1066 * Check that we can actually write to the signal frame.
1068 if (!access_ok(user->sigframe, sp_top - sp))
1074 static void setup_return(struct pt_regs *regs, struct k_sigaction *ka,
1075 struct rt_sigframe_user_layout *user, int usig)
1077 __sigrestore_t sigtramp;
1079 regs->regs[0] = usig;
1080 regs->sp = (unsigned long)user->sigframe;
1081 regs->regs[29] = (unsigned long)&user->next_frame->fp;
1082 regs->pc = (unsigned long)ka->sa.sa_handler;
1085 * Signal delivery is a (wacky) indirect function call in
1086 * userspace, so simulate the same setting of BTYPE as a BLR
1087 * <register containing the signal handler entry point>.
1088 * Signal delivery to a location in a PROT_BTI guarded page
1089 * that is not a function entry point will now trigger a
1090 * SIGILL in userspace.
1092 * If the signal handler entry point is not in a PROT_BTI
1093 * guarded page, this is harmless.
1095 if (system_supports_bti()) {
1096 regs->pstate &= ~PSR_BTYPE_MASK;
1097 regs->pstate |= PSR_BTYPE_C;
1100 /* TCO (Tag Check Override) always cleared for signal handlers */
1101 regs->pstate &= ~PSR_TCO_BIT;
1103 /* Signal handlers are invoked with ZA and streaming mode disabled */
1104 if (system_supports_sme()) {
1106 * If we were in streaming mode the saved register
1107 * state was SVE but we will exit SM and use the
1108 * FPSIMD register state - flush the saved FPSIMD
1109 * register state in case it gets loaded.
1111 if (current->thread.svcr & SVCR_SM_MASK) {
1112 memset(¤t->thread.uw.fpsimd_state, 0,
1113 sizeof(current->thread.uw.fpsimd_state));
1114 current->thread.fp_type = FP_STATE_FPSIMD;
1117 current->thread.svcr &= ~(SVCR_ZA_MASK |
1122 if (ka->sa.sa_flags & SA_RESTORER)
1123 sigtramp = ka->sa.sa_restorer;
1125 sigtramp = VDSO_SYMBOL(current->mm->context.vdso, sigtramp);
1127 regs->regs[30] = (unsigned long)sigtramp;
1130 static int setup_rt_frame(int usig, struct ksignal *ksig, sigset_t *set,
1131 struct pt_regs *regs)
1133 struct rt_sigframe_user_layout user;
1134 struct rt_sigframe __user *frame;
1137 fpsimd_signal_preserve_current_state();
1139 if (get_sigframe(&user, ksig, regs))
1142 frame = user.sigframe;
1144 __put_user_error(0, &frame->uc.uc_flags, err);
1145 __put_user_error(NULL, &frame->uc.uc_link, err);
1147 err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
1148 err |= setup_sigframe(&user, regs, set);
1150 setup_return(regs, &ksig->ka, &user, usig);
1151 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
1152 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
1153 regs->regs[1] = (unsigned long)&frame->info;
1154 regs->regs[2] = (unsigned long)&frame->uc;
1161 static void setup_restart_syscall(struct pt_regs *regs)
1163 if (is_compat_task())
1164 compat_setup_restart_syscall(regs);
1166 regs->regs[8] = __NR_restart_syscall;
1170 * OK, we're invoking a handler
1172 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
1174 sigset_t *oldset = sigmask_to_save();
1175 int usig = ksig->sig;
1178 rseq_signal_deliver(ksig, regs);
1181 * Set up the stack frame
1183 if (is_compat_task()) {
1184 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
1185 ret = compat_setup_rt_frame(usig, ksig, oldset, regs);
1187 ret = compat_setup_frame(usig, ksig, oldset, regs);
1189 ret = setup_rt_frame(usig, ksig, oldset, regs);
1193 * Check that the resulting registers are actually sane.
1195 ret |= !valid_user_regs(®s->user_regs, current);
1197 /* Step into the signal handler if we are stepping */
1198 signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
1202 * Note that 'init' is a special process: it doesn't get signals it doesn't
1203 * want to handle. Thus you cannot kill init even with a SIGKILL even by
1206 * Note that we go through the signals twice: once to check the signals that
1207 * the kernel can handle, and then we build all the user-level signal handling
1208 * stack-frames in one go after that.
1210 static void do_signal(struct pt_regs *regs)
1212 unsigned long continue_addr = 0, restart_addr = 0;
1214 struct ksignal ksig;
1215 bool syscall = in_syscall(regs);
1218 * If we were from a system call, check for system call restarting...
1221 continue_addr = regs->pc;
1222 restart_addr = continue_addr - (compat_thumb_mode(regs) ? 2 : 4);
1223 retval = regs->regs[0];
1226 * Avoid additional syscall restarting via ret_to_user.
1228 forget_syscall(regs);
1231 * Prepare for system call restart. We do this here so that a
1232 * debugger will see the already changed PC.
1235 case -ERESTARTNOHAND:
1237 case -ERESTARTNOINTR:
1238 case -ERESTART_RESTARTBLOCK:
1239 regs->regs[0] = regs->orig_x0;
1240 regs->pc = restart_addr;
1246 * Get the signal to deliver. When running under ptrace, at this point
1247 * the debugger may change all of our registers.
1249 if (get_signal(&ksig)) {
1251 * Depending on the signal settings, we may need to revert the
1252 * decision to restart the system call, but skip this if a
1253 * debugger has chosen to restart at a different PC.
1255 if (regs->pc == restart_addr &&
1256 (retval == -ERESTARTNOHAND ||
1257 retval == -ERESTART_RESTARTBLOCK ||
1258 (retval == -ERESTARTSYS &&
1259 !(ksig.ka.sa.sa_flags & SA_RESTART)))) {
1260 syscall_set_return_value(current, regs, -EINTR, 0);
1261 regs->pc = continue_addr;
1264 handle_signal(&ksig, regs);
1269 * Handle restarting a different system call. As above, if a debugger
1270 * has chosen to restart at a different PC, ignore the restart.
1272 if (syscall && regs->pc == restart_addr) {
1273 if (retval == -ERESTART_RESTARTBLOCK)
1274 setup_restart_syscall(regs);
1275 user_rewind_single_step(current);
1278 restore_saved_sigmask();
1281 void do_notify_resume(struct pt_regs *regs, unsigned long thread_flags)
1284 if (thread_flags & _TIF_NEED_RESCHED) {
1285 /* Unmask Debug and SError for the next task */
1286 local_daif_restore(DAIF_PROCCTX_NOIRQ);
1290 local_daif_restore(DAIF_PROCCTX);
1292 if (thread_flags & _TIF_UPROBE)
1293 uprobe_notify_resume(regs);
1295 if (thread_flags & _TIF_MTE_ASYNC_FAULT) {
1296 clear_thread_flag(TIF_MTE_ASYNC_FAULT);
1297 send_sig_fault(SIGSEGV, SEGV_MTEAERR,
1298 (void __user *)NULL, current);
1301 if (thread_flags & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL))
1304 if (thread_flags & _TIF_NOTIFY_RESUME)
1305 resume_user_mode_work(regs);
1307 if (thread_flags & _TIF_FOREIGN_FPSTATE)
1308 fpsimd_restore_current_state();
1312 thread_flags = read_thread_flags();
1313 } while (thread_flags & _TIF_WORK_MASK);
1316 unsigned long __ro_after_init signal_minsigstksz;
1319 * Determine the stack space required for guaranteed signal devliery.
1320 * This function is used to populate AT_MINSIGSTKSZ at process startup.
1321 * cpufeatures setup is assumed to be complete.
1323 void __init minsigstksz_setup(void)
1325 struct rt_sigframe_user_layout user;
1327 init_user_layout(&user);
1330 * If this fails, SIGFRAME_MAXSZ needs to be enlarged. It won't
1331 * be big enough, but it's our best guess:
1333 if (WARN_ON(setup_sigframe_layout(&user, true)))
1336 signal_minsigstksz = sigframe_size(&user) +
1337 round_up(sizeof(struct frame_record), 16) +
1338 16; /* max alignment padding */
1342 * Compile-time assertions for siginfo_t offsets. Check NSIG* as well, as
1343 * changes likely come with new fields that should be added below.
1345 static_assert(NSIGILL == 11);
1346 static_assert(NSIGFPE == 15);
1347 static_assert(NSIGSEGV == 9);
1348 static_assert(NSIGBUS == 5);
1349 static_assert(NSIGTRAP == 6);
1350 static_assert(NSIGCHLD == 6);
1351 static_assert(NSIGSYS == 2);
1352 static_assert(sizeof(siginfo_t) == 128);
1353 static_assert(__alignof__(siginfo_t) == 8);
1354 static_assert(offsetof(siginfo_t, si_signo) == 0x00);
1355 static_assert(offsetof(siginfo_t, si_errno) == 0x04);
1356 static_assert(offsetof(siginfo_t, si_code) == 0x08);
1357 static_assert(offsetof(siginfo_t, si_pid) == 0x10);
1358 static_assert(offsetof(siginfo_t, si_uid) == 0x14);
1359 static_assert(offsetof(siginfo_t, si_tid) == 0x10);
1360 static_assert(offsetof(siginfo_t, si_overrun) == 0x14);
1361 static_assert(offsetof(siginfo_t, si_status) == 0x18);
1362 static_assert(offsetof(siginfo_t, si_utime) == 0x20);
1363 static_assert(offsetof(siginfo_t, si_stime) == 0x28);
1364 static_assert(offsetof(siginfo_t, si_value) == 0x18);
1365 static_assert(offsetof(siginfo_t, si_int) == 0x18);
1366 static_assert(offsetof(siginfo_t, si_ptr) == 0x18);
1367 static_assert(offsetof(siginfo_t, si_addr) == 0x10);
1368 static_assert(offsetof(siginfo_t, si_addr_lsb) == 0x18);
1369 static_assert(offsetof(siginfo_t, si_lower) == 0x20);
1370 static_assert(offsetof(siginfo_t, si_upper) == 0x28);
1371 static_assert(offsetof(siginfo_t, si_pkey) == 0x20);
1372 static_assert(offsetof(siginfo_t, si_perf_data) == 0x18);
1373 static_assert(offsetof(siginfo_t, si_perf_type) == 0x20);
1374 static_assert(offsetof(siginfo_t, si_perf_flags) == 0x24);
1375 static_assert(offsetof(siginfo_t, si_band) == 0x10);
1376 static_assert(offsetof(siginfo_t, si_fd) == 0x18);
1377 static_assert(offsetof(siginfo_t, si_call_addr) == 0x10);
1378 static_assert(offsetof(siginfo_t, si_syscall) == 0x18);
1379 static_assert(offsetof(siginfo_t, si_arch) == 0x1c);