Linux 4.18.10
[linux/fpc-iii.git] / arch / arm64 / kernel / signal.c
blob511af13e8d8fe2a8d921ff4cd35402b92977501d
1 /*
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/cache.h>
21 #include <linux/compat.h>
22 #include <linux/errno.h>
23 #include <linux/kernel.h>
24 #include <linux/signal.h>
25 #include <linux/personality.h>
26 #include <linux/freezer.h>
27 #include <linux/stddef.h>
28 #include <linux/uaccess.h>
29 #include <linux/sizes.h>
30 #include <linux/string.h>
31 #include <linux/tracehook.h>
32 #include <linux/ratelimit.h>
33 #include <linux/syscalls.h>
35 #include <asm/daifflags.h>
36 #include <asm/debug-monitors.h>
37 #include <asm/elf.h>
38 #include <asm/cacheflush.h>
39 #include <asm/ucontext.h>
40 #include <asm/unistd.h>
41 #include <asm/fpsimd.h>
42 #include <asm/ptrace.h>
43 #include <asm/signal32.h>
44 #include <asm/traps.h>
45 #include <asm/vdso.h>
48 * Do a signal return; undo the signal stack. These are aligned to 128-bit.
50 struct rt_sigframe {
51 struct siginfo info;
52 struct ucontext uc;
55 struct frame_record {
56 u64 fp;
57 u64 lr;
60 struct rt_sigframe_user_layout {
61 struct rt_sigframe __user *sigframe;
62 struct frame_record __user *next_frame;
64 unsigned long size; /* size of allocated sigframe data */
65 unsigned long limit; /* largest allowed size */
67 unsigned long fpsimd_offset;
68 unsigned long esr_offset;
69 unsigned long sve_offset;
70 unsigned long extra_offset;
71 unsigned long end_offset;
74 #define BASE_SIGFRAME_SIZE round_up(sizeof(struct rt_sigframe), 16)
75 #define TERMINATOR_SIZE round_up(sizeof(struct _aarch64_ctx), 16)
76 #define EXTRA_CONTEXT_SIZE round_up(sizeof(struct extra_context), 16)
78 static void init_user_layout(struct rt_sigframe_user_layout *user)
80 const size_t reserved_size =
81 sizeof(user->sigframe->uc.uc_mcontext.__reserved);
83 memset(user, 0, sizeof(*user));
84 user->size = offsetof(struct rt_sigframe, uc.uc_mcontext.__reserved);
86 user->limit = user->size + reserved_size;
88 user->limit -= TERMINATOR_SIZE;
89 user->limit -= EXTRA_CONTEXT_SIZE;
90 /* Reserve space for extension and terminator ^ */
93 static size_t sigframe_size(struct rt_sigframe_user_layout const *user)
95 return round_up(max(user->size, sizeof(struct rt_sigframe)), 16);
99 * Sanity limit on the approximate maximum size of signal frame we'll
100 * try to generate. Stack alignment padding and the frame record are
101 * not taken into account. This limit is not a guarantee and is
102 * NOT ABI.
104 #define SIGFRAME_MAXSZ SZ_64K
106 static int __sigframe_alloc(struct rt_sigframe_user_layout *user,
107 unsigned long *offset, size_t size, bool extend)
109 size_t padded_size = round_up(size, 16);
111 if (padded_size > user->limit - user->size &&
112 !user->extra_offset &&
113 extend) {
114 int ret;
116 user->limit += EXTRA_CONTEXT_SIZE;
117 ret = __sigframe_alloc(user, &user->extra_offset,
118 sizeof(struct extra_context), false);
119 if (ret) {
120 user->limit -= EXTRA_CONTEXT_SIZE;
121 return ret;
124 /* Reserve space for the __reserved[] terminator */
125 user->size += TERMINATOR_SIZE;
128 * Allow expansion up to SIGFRAME_MAXSZ, ensuring space for
129 * the terminator:
131 user->limit = SIGFRAME_MAXSZ - TERMINATOR_SIZE;
134 /* Still not enough space? Bad luck! */
135 if (padded_size > user->limit - user->size)
136 return -ENOMEM;
138 *offset = user->size;
139 user->size += padded_size;
141 return 0;
145 * Allocate space for an optional record of <size> bytes in the user
146 * signal frame. The offset from the signal frame base address to the
147 * allocated block is assigned to *offset.
149 static int sigframe_alloc(struct rt_sigframe_user_layout *user,
150 unsigned long *offset, size_t size)
152 return __sigframe_alloc(user, offset, size, true);
155 /* Allocate the null terminator record and prevent further allocations */
156 static int sigframe_alloc_end(struct rt_sigframe_user_layout *user)
158 int ret;
160 /* Un-reserve the space reserved for the terminator: */
161 user->limit += TERMINATOR_SIZE;
163 ret = sigframe_alloc(user, &user->end_offset,
164 sizeof(struct _aarch64_ctx));
165 if (ret)
166 return ret;
168 /* Prevent further allocation: */
169 user->limit = user->size;
170 return 0;
173 static void __user *apply_user_offset(
174 struct rt_sigframe_user_layout const *user, unsigned long offset)
176 char __user *base = (char __user *)user->sigframe;
178 return base + offset;
181 static int preserve_fpsimd_context(struct fpsimd_context __user *ctx)
183 struct user_fpsimd_state const *fpsimd =
184 &current->thread.uw.fpsimd_state;
185 int err;
187 /* copy the FP and status/control registers */
188 err = __copy_to_user(ctx->vregs, fpsimd->vregs, sizeof(fpsimd->vregs));
189 __put_user_error(fpsimd->fpsr, &ctx->fpsr, err);
190 __put_user_error(fpsimd->fpcr, &ctx->fpcr, err);
192 /* copy the magic/size information */
193 __put_user_error(FPSIMD_MAGIC, &ctx->head.magic, err);
194 __put_user_error(sizeof(struct fpsimd_context), &ctx->head.size, err);
196 return err ? -EFAULT : 0;
199 static int restore_fpsimd_context(struct fpsimd_context __user *ctx)
201 struct user_fpsimd_state fpsimd;
202 __u32 magic, size;
203 int err = 0;
205 /* check the magic/size information */
206 __get_user_error(magic, &ctx->head.magic, err);
207 __get_user_error(size, &ctx->head.size, err);
208 if (err)
209 return -EFAULT;
210 if (magic != FPSIMD_MAGIC || size != sizeof(struct fpsimd_context))
211 return -EINVAL;
213 /* copy the FP and status/control registers */
214 err = __copy_from_user(fpsimd.vregs, ctx->vregs,
215 sizeof(fpsimd.vregs));
216 __get_user_error(fpsimd.fpsr, &ctx->fpsr, err);
217 __get_user_error(fpsimd.fpcr, &ctx->fpcr, err);
219 clear_thread_flag(TIF_SVE);
221 /* load the hardware registers from the fpsimd_state structure */
222 if (!err)
223 fpsimd_update_current_state(&fpsimd);
225 return err ? -EFAULT : 0;
229 struct user_ctxs {
230 struct fpsimd_context __user *fpsimd;
231 struct sve_context __user *sve;
234 #ifdef CONFIG_ARM64_SVE
236 static int preserve_sve_context(struct sve_context __user *ctx)
238 int err = 0;
239 u16 reserved[ARRAY_SIZE(ctx->__reserved)];
240 unsigned int vl = current->thread.sve_vl;
241 unsigned int vq = 0;
243 if (test_thread_flag(TIF_SVE))
244 vq = sve_vq_from_vl(vl);
246 memset(reserved, 0, sizeof(reserved));
248 __put_user_error(SVE_MAGIC, &ctx->head.magic, err);
249 __put_user_error(round_up(SVE_SIG_CONTEXT_SIZE(vq), 16),
250 &ctx->head.size, err);
251 __put_user_error(vl, &ctx->vl, err);
252 BUILD_BUG_ON(sizeof(ctx->__reserved) != sizeof(reserved));
253 err |= __copy_to_user(&ctx->__reserved, reserved, sizeof(reserved));
255 if (vq) {
257 * This assumes that the SVE state has already been saved to
258 * the task struct by calling preserve_fpsimd_context().
260 err |= __copy_to_user((char __user *)ctx + SVE_SIG_REGS_OFFSET,
261 current->thread.sve_state,
262 SVE_SIG_REGS_SIZE(vq));
265 return err ? -EFAULT : 0;
268 static int restore_sve_fpsimd_context(struct user_ctxs *user)
270 int err;
271 unsigned int vq;
272 struct user_fpsimd_state fpsimd;
273 struct sve_context sve;
275 if (__copy_from_user(&sve, user->sve, sizeof(sve)))
276 return -EFAULT;
278 if (sve.vl != current->thread.sve_vl)
279 return -EINVAL;
281 if (sve.head.size <= sizeof(*user->sve)) {
282 clear_thread_flag(TIF_SVE);
283 goto fpsimd_only;
286 vq = sve_vq_from_vl(sve.vl);
288 if (sve.head.size < SVE_SIG_CONTEXT_SIZE(vq))
289 return -EINVAL;
292 * Careful: we are about __copy_from_user() directly into
293 * thread.sve_state with preemption enabled, so protection is
294 * needed to prevent a racing context switch from writing stale
295 * registers back over the new data.
298 fpsimd_flush_task_state(current);
299 barrier();
300 /* From now, fpsimd_thread_switch() won't clear TIF_FOREIGN_FPSTATE */
302 set_thread_flag(TIF_FOREIGN_FPSTATE);
303 barrier();
304 /* From now, fpsimd_thread_switch() won't touch thread.sve_state */
306 sve_alloc(current);
307 err = __copy_from_user(current->thread.sve_state,
308 (char __user const *)user->sve +
309 SVE_SIG_REGS_OFFSET,
310 SVE_SIG_REGS_SIZE(vq));
311 if (err)
312 return -EFAULT;
314 set_thread_flag(TIF_SVE);
316 fpsimd_only:
317 /* copy the FP and status/control registers */
318 /* restore_sigframe() already checked that user->fpsimd != NULL. */
319 err = __copy_from_user(fpsimd.vregs, user->fpsimd->vregs,
320 sizeof(fpsimd.vregs));
321 __get_user_error(fpsimd.fpsr, &user->fpsimd->fpsr, err);
322 __get_user_error(fpsimd.fpcr, &user->fpsimd->fpcr, err);
324 /* load the hardware registers from the fpsimd_state structure */
325 if (!err)
326 fpsimd_update_current_state(&fpsimd);
328 return err ? -EFAULT : 0;
331 #else /* ! CONFIG_ARM64_SVE */
333 /* Turn any non-optimised out attempts to use these into a link error: */
334 extern int preserve_sve_context(void __user *ctx);
335 extern int restore_sve_fpsimd_context(struct user_ctxs *user);
337 #endif /* ! CONFIG_ARM64_SVE */
340 static int parse_user_sigframe(struct user_ctxs *user,
341 struct rt_sigframe __user *sf)
343 struct sigcontext __user *const sc = &sf->uc.uc_mcontext;
344 struct _aarch64_ctx __user *head;
345 char __user *base = (char __user *)&sc->__reserved;
346 size_t offset = 0;
347 size_t limit = sizeof(sc->__reserved);
348 bool have_extra_context = false;
349 char const __user *const sfp = (char const __user *)sf;
351 user->fpsimd = NULL;
352 user->sve = NULL;
354 if (!IS_ALIGNED((unsigned long)base, 16))
355 goto invalid;
357 while (1) {
358 int err = 0;
359 u32 magic, size;
360 char const __user *userp;
361 struct extra_context const __user *extra;
362 u64 extra_datap;
363 u32 extra_size;
364 struct _aarch64_ctx const __user *end;
365 u32 end_magic, end_size;
367 if (limit - offset < sizeof(*head))
368 goto invalid;
370 if (!IS_ALIGNED(offset, 16))
371 goto invalid;
373 head = (struct _aarch64_ctx __user *)(base + offset);
374 __get_user_error(magic, &head->magic, err);
375 __get_user_error(size, &head->size, err);
376 if (err)
377 return err;
379 if (limit - offset < size)
380 goto invalid;
382 switch (magic) {
383 case 0:
384 if (size)
385 goto invalid;
387 goto done;
389 case FPSIMD_MAGIC:
390 if (user->fpsimd)
391 goto invalid;
393 if (size < sizeof(*user->fpsimd))
394 goto invalid;
396 user->fpsimd = (struct fpsimd_context __user *)head;
397 break;
399 case ESR_MAGIC:
400 /* ignore */
401 break;
403 case SVE_MAGIC:
404 if (!system_supports_sve())
405 goto invalid;
407 if (user->sve)
408 goto invalid;
410 if (size < sizeof(*user->sve))
411 goto invalid;
413 user->sve = (struct sve_context __user *)head;
414 break;
416 case EXTRA_MAGIC:
417 if (have_extra_context)
418 goto invalid;
420 if (size < sizeof(*extra))
421 goto invalid;
423 userp = (char const __user *)head;
425 extra = (struct extra_context const __user *)userp;
426 userp += size;
428 __get_user_error(extra_datap, &extra->datap, err);
429 __get_user_error(extra_size, &extra->size, err);
430 if (err)
431 return err;
433 /* Check for the dummy terminator in __reserved[]: */
435 if (limit - offset - size < TERMINATOR_SIZE)
436 goto invalid;
438 end = (struct _aarch64_ctx const __user *)userp;
439 userp += TERMINATOR_SIZE;
441 __get_user_error(end_magic, &end->magic, err);
442 __get_user_error(end_size, &end->size, err);
443 if (err)
444 return err;
446 if (end_magic || end_size)
447 goto invalid;
449 /* Prevent looping/repeated parsing of extra_context */
450 have_extra_context = true;
452 base = (__force void __user *)extra_datap;
453 if (!IS_ALIGNED((unsigned long)base, 16))
454 goto invalid;
456 if (!IS_ALIGNED(extra_size, 16))
457 goto invalid;
459 if (base != userp)
460 goto invalid;
462 /* Reject "unreasonably large" frames: */
463 if (extra_size > sfp + SIGFRAME_MAXSZ - userp)
464 goto invalid;
467 * Ignore trailing terminator in __reserved[]
468 * and start parsing extra data:
470 offset = 0;
471 limit = extra_size;
473 if (!access_ok(VERIFY_READ, base, limit))
474 goto invalid;
476 continue;
478 default:
479 goto invalid;
482 if (size < sizeof(*head))
483 goto invalid;
485 if (limit - offset < size)
486 goto invalid;
488 offset += size;
491 done:
492 return 0;
494 invalid:
495 return -EINVAL;
498 static int restore_sigframe(struct pt_regs *regs,
499 struct rt_sigframe __user *sf)
501 sigset_t set;
502 int i, err;
503 struct user_ctxs user;
505 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
506 if (err == 0)
507 set_current_blocked(&set);
509 for (i = 0; i < 31; i++)
510 __get_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i],
511 err);
512 __get_user_error(regs->sp, &sf->uc.uc_mcontext.sp, err);
513 __get_user_error(regs->pc, &sf->uc.uc_mcontext.pc, err);
514 __get_user_error(regs->pstate, &sf->uc.uc_mcontext.pstate, err);
517 * Avoid sys_rt_sigreturn() restarting.
519 forget_syscall(regs);
521 err |= !valid_user_regs(&regs->user_regs, current);
522 if (err == 0)
523 err = parse_user_sigframe(&user, sf);
525 if (err == 0) {
526 if (!user.fpsimd)
527 return -EINVAL;
529 if (user.sve) {
530 if (!system_supports_sve())
531 return -EINVAL;
533 err = restore_sve_fpsimd_context(&user);
534 } else {
535 err = restore_fpsimd_context(user.fpsimd);
539 return err;
542 asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
544 struct rt_sigframe __user *frame;
546 /* Always make any pending restarted system calls return -EINTR */
547 current->restart_block.fn = do_no_restart_syscall;
550 * Since we stacked the signal on a 128-bit boundary, then 'sp' should
551 * be word aligned here.
553 if (regs->sp & 15)
554 goto badframe;
556 frame = (struct rt_sigframe __user *)regs->sp;
558 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
559 goto badframe;
561 if (restore_sigframe(regs, frame))
562 goto badframe;
564 if (restore_altstack(&frame->uc.uc_stack))
565 goto badframe;
567 return regs->regs[0];
569 badframe:
570 arm64_notify_segfault(regs->sp);
571 return 0;
575 * Determine the layout of optional records in the signal frame
577 * add_all: if true, lays out the biggest possible signal frame for
578 * this task; otherwise, generates a layout for the current state
579 * of the task.
581 static int setup_sigframe_layout(struct rt_sigframe_user_layout *user,
582 bool add_all)
584 int err;
586 err = sigframe_alloc(user, &user->fpsimd_offset,
587 sizeof(struct fpsimd_context));
588 if (err)
589 return err;
591 /* fault information, if valid */
592 if (add_all || current->thread.fault_code) {
593 err = sigframe_alloc(user, &user->esr_offset,
594 sizeof(struct esr_context));
595 if (err)
596 return err;
599 if (system_supports_sve()) {
600 unsigned int vq = 0;
602 if (add_all || test_thread_flag(TIF_SVE)) {
603 int vl = sve_max_vl;
605 if (!add_all)
606 vl = current->thread.sve_vl;
608 vq = sve_vq_from_vl(vl);
611 err = sigframe_alloc(user, &user->sve_offset,
612 SVE_SIG_CONTEXT_SIZE(vq));
613 if (err)
614 return err;
617 return sigframe_alloc_end(user);
620 static int setup_sigframe(struct rt_sigframe_user_layout *user,
621 struct pt_regs *regs, sigset_t *set)
623 int i, err = 0;
624 struct rt_sigframe __user *sf = user->sigframe;
626 /* set up the stack frame for unwinding */
627 __put_user_error(regs->regs[29], &user->next_frame->fp, err);
628 __put_user_error(regs->regs[30], &user->next_frame->lr, err);
630 for (i = 0; i < 31; i++)
631 __put_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i],
632 err);
633 __put_user_error(regs->sp, &sf->uc.uc_mcontext.sp, err);
634 __put_user_error(regs->pc, &sf->uc.uc_mcontext.pc, err);
635 __put_user_error(regs->pstate, &sf->uc.uc_mcontext.pstate, err);
637 __put_user_error(current->thread.fault_address, &sf->uc.uc_mcontext.fault_address, err);
639 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
641 if (err == 0) {
642 struct fpsimd_context __user *fpsimd_ctx =
643 apply_user_offset(user, user->fpsimd_offset);
644 err |= preserve_fpsimd_context(fpsimd_ctx);
647 /* fault information, if valid */
648 if (err == 0 && user->esr_offset) {
649 struct esr_context __user *esr_ctx =
650 apply_user_offset(user, user->esr_offset);
652 __put_user_error(ESR_MAGIC, &esr_ctx->head.magic, err);
653 __put_user_error(sizeof(*esr_ctx), &esr_ctx->head.size, err);
654 __put_user_error(current->thread.fault_code, &esr_ctx->esr, err);
657 /* Scalable Vector Extension state, if present */
658 if (system_supports_sve() && err == 0 && user->sve_offset) {
659 struct sve_context __user *sve_ctx =
660 apply_user_offset(user, user->sve_offset);
661 err |= preserve_sve_context(sve_ctx);
664 if (err == 0 && user->extra_offset) {
665 char __user *sfp = (char __user *)user->sigframe;
666 char __user *userp =
667 apply_user_offset(user, user->extra_offset);
669 struct extra_context __user *extra;
670 struct _aarch64_ctx __user *end;
671 u64 extra_datap;
672 u32 extra_size;
674 extra = (struct extra_context __user *)userp;
675 userp += EXTRA_CONTEXT_SIZE;
677 end = (struct _aarch64_ctx __user *)userp;
678 userp += TERMINATOR_SIZE;
681 * extra_datap is just written to the signal frame.
682 * The value gets cast back to a void __user *
683 * during sigreturn.
685 extra_datap = (__force u64)userp;
686 extra_size = sfp + round_up(user->size, 16) - userp;
688 __put_user_error(EXTRA_MAGIC, &extra->head.magic, err);
689 __put_user_error(EXTRA_CONTEXT_SIZE, &extra->head.size, err);
690 __put_user_error(extra_datap, &extra->datap, err);
691 __put_user_error(extra_size, &extra->size, err);
693 /* Add the terminator */
694 __put_user_error(0, &end->magic, err);
695 __put_user_error(0, &end->size, err);
698 /* set the "end" magic */
699 if (err == 0) {
700 struct _aarch64_ctx __user *end =
701 apply_user_offset(user, user->end_offset);
703 __put_user_error(0, &end->magic, err);
704 __put_user_error(0, &end->size, err);
707 return err;
710 static int get_sigframe(struct rt_sigframe_user_layout *user,
711 struct ksignal *ksig, struct pt_regs *regs)
713 unsigned long sp, sp_top;
714 int err;
716 init_user_layout(user);
717 err = setup_sigframe_layout(user, false);
718 if (err)
719 return err;
721 sp = sp_top = sigsp(regs->sp, ksig);
723 sp = round_down(sp - sizeof(struct frame_record), 16);
724 user->next_frame = (struct frame_record __user *)sp;
726 sp = round_down(sp, 16) - sigframe_size(user);
727 user->sigframe = (struct rt_sigframe __user *)sp;
730 * Check that we can actually write to the signal frame.
732 if (!access_ok(VERIFY_WRITE, user->sigframe, sp_top - sp))
733 return -EFAULT;
735 return 0;
738 static void setup_return(struct pt_regs *regs, struct k_sigaction *ka,
739 struct rt_sigframe_user_layout *user, int usig)
741 __sigrestore_t sigtramp;
743 regs->regs[0] = usig;
744 regs->sp = (unsigned long)user->sigframe;
745 regs->regs[29] = (unsigned long)&user->next_frame->fp;
746 regs->pc = (unsigned long)ka->sa.sa_handler;
748 if (ka->sa.sa_flags & SA_RESTORER)
749 sigtramp = ka->sa.sa_restorer;
750 else
751 sigtramp = VDSO_SYMBOL(current->mm->context.vdso, sigtramp);
753 regs->regs[30] = (unsigned long)sigtramp;
756 static int setup_rt_frame(int usig, struct ksignal *ksig, sigset_t *set,
757 struct pt_regs *regs)
759 struct rt_sigframe_user_layout user;
760 struct rt_sigframe __user *frame;
761 int err = 0;
763 fpsimd_signal_preserve_current_state();
765 if (get_sigframe(&user, ksig, regs))
766 return 1;
768 frame = user.sigframe;
770 __put_user_error(0, &frame->uc.uc_flags, err);
771 __put_user_error(NULL, &frame->uc.uc_link, err);
773 err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
774 err |= setup_sigframe(&user, regs, set);
775 if (err == 0) {
776 setup_return(regs, &ksig->ka, &user, usig);
777 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
778 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
779 regs->regs[1] = (unsigned long)&frame->info;
780 regs->regs[2] = (unsigned long)&frame->uc;
784 return err;
787 static void setup_restart_syscall(struct pt_regs *regs)
789 if (is_compat_task())
790 compat_setup_restart_syscall(regs);
791 else
792 regs->regs[8] = __NR_restart_syscall;
796 * OK, we're invoking a handler
798 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
800 struct task_struct *tsk = current;
801 sigset_t *oldset = sigmask_to_save();
802 int usig = ksig->sig;
803 int ret;
806 * Set up the stack frame
808 if (is_compat_task()) {
809 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
810 ret = compat_setup_rt_frame(usig, ksig, oldset, regs);
811 else
812 ret = compat_setup_frame(usig, ksig, oldset, regs);
813 } else {
814 ret = setup_rt_frame(usig, ksig, oldset, regs);
818 * Check that the resulting registers are actually sane.
820 ret |= !valid_user_regs(&regs->user_regs, current);
823 * Fast forward the stepping logic so we step into the signal
824 * handler.
826 if (!ret)
827 user_fastforward_single_step(tsk);
829 signal_setup_done(ret, ksig, 0);
833 * Note that 'init' is a special process: it doesn't get signals it doesn't
834 * want to handle. Thus you cannot kill init even with a SIGKILL even by
835 * mistake.
837 * Note that we go through the signals twice: once to check the signals that
838 * the kernel can handle, and then we build all the user-level signal handling
839 * stack-frames in one go after that.
841 static void do_signal(struct pt_regs *regs)
843 unsigned long continue_addr = 0, restart_addr = 0;
844 int retval = 0;
845 struct ksignal ksig;
846 bool syscall = in_syscall(regs);
849 * If we were from a system call, check for system call restarting...
851 if (syscall) {
852 continue_addr = regs->pc;
853 restart_addr = continue_addr - (compat_thumb_mode(regs) ? 2 : 4);
854 retval = regs->regs[0];
857 * Avoid additional syscall restarting via ret_to_user.
859 forget_syscall(regs);
862 * Prepare for system call restart. We do this here so that a
863 * debugger will see the already changed PC.
865 switch (retval) {
866 case -ERESTARTNOHAND:
867 case -ERESTARTSYS:
868 case -ERESTARTNOINTR:
869 case -ERESTART_RESTARTBLOCK:
870 regs->regs[0] = regs->orig_x0;
871 regs->pc = restart_addr;
872 break;
877 * Get the signal to deliver. When running under ptrace, at this point
878 * the debugger may change all of our registers.
880 if (get_signal(&ksig)) {
882 * Depending on the signal settings, we may need to revert the
883 * decision to restart the system call, but skip this if a
884 * debugger has chosen to restart at a different PC.
886 if (regs->pc == restart_addr &&
887 (retval == -ERESTARTNOHAND ||
888 retval == -ERESTART_RESTARTBLOCK ||
889 (retval == -ERESTARTSYS &&
890 !(ksig.ka.sa.sa_flags & SA_RESTART)))) {
891 regs->regs[0] = -EINTR;
892 regs->pc = continue_addr;
895 handle_signal(&ksig, regs);
896 return;
900 * Handle restarting a different system call. As above, if a debugger
901 * has chosen to restart at a different PC, ignore the restart.
903 if (syscall && regs->pc == restart_addr) {
904 if (retval == -ERESTART_RESTARTBLOCK)
905 setup_restart_syscall(regs);
906 user_rewind_single_step(current);
909 restore_saved_sigmask();
912 asmlinkage void do_notify_resume(struct pt_regs *regs,
913 unsigned int thread_flags)
916 * The assembly code enters us with IRQs off, but it hasn't
917 * informed the tracing code of that for efficiency reasons.
918 * Update the trace code with the current status.
920 trace_hardirqs_off();
922 do {
923 /* Check valid user FS if needed */
924 addr_limit_user_check();
926 if (thread_flags & _TIF_NEED_RESCHED) {
927 /* Unmask Debug and SError for the next task */
928 local_daif_restore(DAIF_PROCCTX_NOIRQ);
930 schedule();
931 } else {
932 local_daif_restore(DAIF_PROCCTX);
934 if (thread_flags & _TIF_UPROBE)
935 uprobe_notify_resume(regs);
937 if (thread_flags & _TIF_SIGPENDING)
938 do_signal(regs);
940 if (thread_flags & _TIF_NOTIFY_RESUME) {
941 clear_thread_flag(TIF_NOTIFY_RESUME);
942 tracehook_notify_resume(regs);
945 if (thread_flags & _TIF_FOREIGN_FPSTATE)
946 fpsimd_restore_current_state();
949 local_daif_mask();
950 thread_flags = READ_ONCE(current_thread_info()->flags);
951 } while (thread_flags & _TIF_WORK_MASK);
954 unsigned long __ro_after_init signal_minsigstksz;
957 * Determine the stack space required for guaranteed signal devliery.
958 * This function is used to populate AT_MINSIGSTKSZ at process startup.
959 * cpufeatures setup is assumed to be complete.
961 void __init minsigstksz_setup(void)
963 struct rt_sigframe_user_layout user;
965 init_user_layout(&user);
968 * If this fails, SIGFRAME_MAXSZ needs to be enlarged. It won't
969 * be big enough, but it's our best guess:
971 if (WARN_ON(setup_sigframe_layout(&user, true)))
972 return;
974 signal_minsigstksz = sigframe_size(&user) +
975 round_up(sizeof(struct frame_record), 16) +
976 16; /* max alignment padding */