treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / arch / arm64 / kernel / signal.c
blob339882db5a9159bfd888d933dc5fff235408e475
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Based on arch/arm/kernel/signal.c
5 * Copyright (C) 1995-2009 Russell King
6 * Copyright (C) 2012 ARM Ltd.
7 */
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/personality.h>
15 #include <linux/freezer.h>
16 #include <linux/stddef.h>
17 #include <linux/uaccess.h>
18 #include <linux/sizes.h>
19 #include <linux/string.h>
20 #include <linux/tracehook.h>
21 #include <linux/ratelimit.h>
22 #include <linux/syscalls.h>
24 #include <asm/daifflags.h>
25 #include <asm/debug-monitors.h>
26 #include <asm/elf.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/signal32.h>
33 #include <asm/traps.h>
34 #include <asm/vdso.h>
37 * Do a signal return; undo the signal stack. These are aligned to 128-bit.
39 struct rt_sigframe {
40 struct siginfo info;
41 struct ucontext uc;
44 struct frame_record {
45 u64 fp;
46 u64 lr;
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 extra_offset;
60 unsigned long end_offset;
63 #define BASE_SIGFRAME_SIZE round_up(sizeof(struct rt_sigframe), 16)
64 #define TERMINATOR_SIZE round_up(sizeof(struct _aarch64_ctx), 16)
65 #define EXTRA_CONTEXT_SIZE round_up(sizeof(struct extra_context), 16)
67 static void init_user_layout(struct rt_sigframe_user_layout *user)
69 const size_t reserved_size =
70 sizeof(user->sigframe->uc.uc_mcontext.__reserved);
72 memset(user, 0, sizeof(*user));
73 user->size = offsetof(struct rt_sigframe, uc.uc_mcontext.__reserved);
75 user->limit = user->size + reserved_size;
77 user->limit -= TERMINATOR_SIZE;
78 user->limit -= EXTRA_CONTEXT_SIZE;
79 /* Reserve space for extension and terminator ^ */
82 static size_t sigframe_size(struct rt_sigframe_user_layout const *user)
84 return round_up(max(user->size, sizeof(struct rt_sigframe)), 16);
88 * Sanity limit on the approximate maximum size of signal frame we'll
89 * try to generate. Stack alignment padding and the frame record are
90 * not taken into account. This limit is not a guarantee and is
91 * NOT ABI.
93 #define SIGFRAME_MAXSZ SZ_64K
95 static int __sigframe_alloc(struct rt_sigframe_user_layout *user,
96 unsigned long *offset, size_t size, bool extend)
98 size_t padded_size = round_up(size, 16);
100 if (padded_size > user->limit - user->size &&
101 !user->extra_offset &&
102 extend) {
103 int ret;
105 user->limit += EXTRA_CONTEXT_SIZE;
106 ret = __sigframe_alloc(user, &user->extra_offset,
107 sizeof(struct extra_context), false);
108 if (ret) {
109 user->limit -= EXTRA_CONTEXT_SIZE;
110 return ret;
113 /* Reserve space for the __reserved[] terminator */
114 user->size += TERMINATOR_SIZE;
117 * Allow expansion up to SIGFRAME_MAXSZ, ensuring space for
118 * the terminator:
120 user->limit = SIGFRAME_MAXSZ - TERMINATOR_SIZE;
123 /* Still not enough space? Bad luck! */
124 if (padded_size > user->limit - user->size)
125 return -ENOMEM;
127 *offset = user->size;
128 user->size += padded_size;
130 return 0;
134 * Allocate space for an optional record of <size> bytes in the user
135 * signal frame. The offset from the signal frame base address to the
136 * allocated block is assigned to *offset.
138 static int sigframe_alloc(struct rt_sigframe_user_layout *user,
139 unsigned long *offset, size_t size)
141 return __sigframe_alloc(user, offset, size, true);
144 /* Allocate the null terminator record and prevent further allocations */
145 static int sigframe_alloc_end(struct rt_sigframe_user_layout *user)
147 int ret;
149 /* Un-reserve the space reserved for the terminator: */
150 user->limit += TERMINATOR_SIZE;
152 ret = sigframe_alloc(user, &user->end_offset,
153 sizeof(struct _aarch64_ctx));
154 if (ret)
155 return ret;
157 /* Prevent further allocation: */
158 user->limit = user->size;
159 return 0;
162 static void __user *apply_user_offset(
163 struct rt_sigframe_user_layout const *user, unsigned long offset)
165 char __user *base = (char __user *)user->sigframe;
167 return base + offset;
170 static int preserve_fpsimd_context(struct fpsimd_context __user *ctx)
172 struct user_fpsimd_state const *fpsimd =
173 &current->thread.uw.fpsimd_state;
174 int err;
176 /* copy the FP and status/control registers */
177 err = __copy_to_user(ctx->vregs, fpsimd->vregs, sizeof(fpsimd->vregs));
178 __put_user_error(fpsimd->fpsr, &ctx->fpsr, err);
179 __put_user_error(fpsimd->fpcr, &ctx->fpcr, err);
181 /* copy the magic/size information */
182 __put_user_error(FPSIMD_MAGIC, &ctx->head.magic, err);
183 __put_user_error(sizeof(struct fpsimd_context), &ctx->head.size, err);
185 return err ? -EFAULT : 0;
188 static int restore_fpsimd_context(struct fpsimd_context __user *ctx)
190 struct user_fpsimd_state fpsimd;
191 __u32 magic, size;
192 int err = 0;
194 /* check the magic/size information */
195 __get_user_error(magic, &ctx->head.magic, err);
196 __get_user_error(size, &ctx->head.size, err);
197 if (err)
198 return -EFAULT;
199 if (magic != FPSIMD_MAGIC || size != sizeof(struct fpsimd_context))
200 return -EINVAL;
202 /* copy the FP and status/control registers */
203 err = __copy_from_user(fpsimd.vregs, ctx->vregs,
204 sizeof(fpsimd.vregs));
205 __get_user_error(fpsimd.fpsr, &ctx->fpsr, err);
206 __get_user_error(fpsimd.fpcr, &ctx->fpcr, err);
208 clear_thread_flag(TIF_SVE);
210 /* load the hardware registers from the fpsimd_state structure */
211 if (!err)
212 fpsimd_update_current_state(&fpsimd);
214 return err ? -EFAULT : 0;
218 struct user_ctxs {
219 struct fpsimd_context __user *fpsimd;
220 struct sve_context __user *sve;
223 #ifdef CONFIG_ARM64_SVE
225 static int preserve_sve_context(struct sve_context __user *ctx)
227 int err = 0;
228 u16 reserved[ARRAY_SIZE(ctx->__reserved)];
229 unsigned int vl = current->thread.sve_vl;
230 unsigned int vq = 0;
232 if (test_thread_flag(TIF_SVE))
233 vq = sve_vq_from_vl(vl);
235 memset(reserved, 0, sizeof(reserved));
237 __put_user_error(SVE_MAGIC, &ctx->head.magic, err);
238 __put_user_error(round_up(SVE_SIG_CONTEXT_SIZE(vq), 16),
239 &ctx->head.size, err);
240 __put_user_error(vl, &ctx->vl, err);
241 BUILD_BUG_ON(sizeof(ctx->__reserved) != sizeof(reserved));
242 err |= __copy_to_user(&ctx->__reserved, reserved, sizeof(reserved));
244 if (vq) {
246 * This assumes that the SVE state has already been saved to
247 * the task struct by calling preserve_fpsimd_context().
249 err |= __copy_to_user((char __user *)ctx + SVE_SIG_REGS_OFFSET,
250 current->thread.sve_state,
251 SVE_SIG_REGS_SIZE(vq));
254 return err ? -EFAULT : 0;
257 static int restore_sve_fpsimd_context(struct user_ctxs *user)
259 int err;
260 unsigned int vq;
261 struct user_fpsimd_state fpsimd;
262 struct sve_context sve;
264 if (__copy_from_user(&sve, user->sve, sizeof(sve)))
265 return -EFAULT;
267 if (sve.vl != current->thread.sve_vl)
268 return -EINVAL;
270 if (sve.head.size <= sizeof(*user->sve)) {
271 clear_thread_flag(TIF_SVE);
272 goto fpsimd_only;
275 vq = sve_vq_from_vl(sve.vl);
277 if (sve.head.size < SVE_SIG_CONTEXT_SIZE(vq))
278 return -EINVAL;
281 * Careful: we are about __copy_from_user() directly into
282 * thread.sve_state with preemption enabled, so protection is
283 * needed to prevent a racing context switch from writing stale
284 * registers back over the new data.
287 fpsimd_flush_task_state(current);
288 /* From now, fpsimd_thread_switch() won't touch thread.sve_state */
290 sve_alloc(current);
291 err = __copy_from_user(current->thread.sve_state,
292 (char __user const *)user->sve +
293 SVE_SIG_REGS_OFFSET,
294 SVE_SIG_REGS_SIZE(vq));
295 if (err)
296 return -EFAULT;
298 set_thread_flag(TIF_SVE);
300 fpsimd_only:
301 /* copy the FP and status/control registers */
302 /* restore_sigframe() already checked that user->fpsimd != NULL. */
303 err = __copy_from_user(fpsimd.vregs, user->fpsimd->vregs,
304 sizeof(fpsimd.vregs));
305 __get_user_error(fpsimd.fpsr, &user->fpsimd->fpsr, err);
306 __get_user_error(fpsimd.fpcr, &user->fpsimd->fpcr, err);
308 /* load the hardware registers from the fpsimd_state structure */
309 if (!err)
310 fpsimd_update_current_state(&fpsimd);
312 return err ? -EFAULT : 0;
315 #else /* ! CONFIG_ARM64_SVE */
317 /* Turn any non-optimised out attempts to use these into a link error: */
318 extern int preserve_sve_context(void __user *ctx);
319 extern int restore_sve_fpsimd_context(struct user_ctxs *user);
321 #endif /* ! CONFIG_ARM64_SVE */
324 static int parse_user_sigframe(struct user_ctxs *user,
325 struct rt_sigframe __user *sf)
327 struct sigcontext __user *const sc = &sf->uc.uc_mcontext;
328 struct _aarch64_ctx __user *head;
329 char __user *base = (char __user *)&sc->__reserved;
330 size_t offset = 0;
331 size_t limit = sizeof(sc->__reserved);
332 bool have_extra_context = false;
333 char const __user *const sfp = (char const __user *)sf;
335 user->fpsimd = NULL;
336 user->sve = NULL;
338 if (!IS_ALIGNED((unsigned long)base, 16))
339 goto invalid;
341 while (1) {
342 int err = 0;
343 u32 magic, size;
344 char const __user *userp;
345 struct extra_context const __user *extra;
346 u64 extra_datap;
347 u32 extra_size;
348 struct _aarch64_ctx const __user *end;
349 u32 end_magic, end_size;
351 if (limit - offset < sizeof(*head))
352 goto invalid;
354 if (!IS_ALIGNED(offset, 16))
355 goto invalid;
357 head = (struct _aarch64_ctx __user *)(base + offset);
358 __get_user_error(magic, &head->magic, err);
359 __get_user_error(size, &head->size, err);
360 if (err)
361 return err;
363 if (limit - offset < size)
364 goto invalid;
366 switch (magic) {
367 case 0:
368 if (size)
369 goto invalid;
371 goto done;
373 case FPSIMD_MAGIC:
374 if (!system_supports_fpsimd())
375 goto invalid;
376 if (user->fpsimd)
377 goto invalid;
379 if (size < sizeof(*user->fpsimd))
380 goto invalid;
382 user->fpsimd = (struct fpsimd_context __user *)head;
383 break;
385 case ESR_MAGIC:
386 /* ignore */
387 break;
389 case SVE_MAGIC:
390 if (!system_supports_sve())
391 goto invalid;
393 if (user->sve)
394 goto invalid;
396 if (size < sizeof(*user->sve))
397 goto invalid;
399 user->sve = (struct sve_context __user *)head;
400 break;
402 case EXTRA_MAGIC:
403 if (have_extra_context)
404 goto invalid;
406 if (size < sizeof(*extra))
407 goto invalid;
409 userp = (char const __user *)head;
411 extra = (struct extra_context const __user *)userp;
412 userp += size;
414 __get_user_error(extra_datap, &extra->datap, err);
415 __get_user_error(extra_size, &extra->size, err);
416 if (err)
417 return err;
419 /* Check for the dummy terminator in __reserved[]: */
421 if (limit - offset - size < TERMINATOR_SIZE)
422 goto invalid;
424 end = (struct _aarch64_ctx const __user *)userp;
425 userp += TERMINATOR_SIZE;
427 __get_user_error(end_magic, &end->magic, err);
428 __get_user_error(end_size, &end->size, err);
429 if (err)
430 return err;
432 if (end_magic || end_size)
433 goto invalid;
435 /* Prevent looping/repeated parsing of extra_context */
436 have_extra_context = true;
438 base = (__force void __user *)extra_datap;
439 if (!IS_ALIGNED((unsigned long)base, 16))
440 goto invalid;
442 if (!IS_ALIGNED(extra_size, 16))
443 goto invalid;
445 if (base != userp)
446 goto invalid;
448 /* Reject "unreasonably large" frames: */
449 if (extra_size > sfp + SIGFRAME_MAXSZ - userp)
450 goto invalid;
453 * Ignore trailing terminator in __reserved[]
454 * and start parsing extra data:
456 offset = 0;
457 limit = extra_size;
459 if (!access_ok(base, limit))
460 goto invalid;
462 continue;
464 default:
465 goto invalid;
468 if (size < sizeof(*head))
469 goto invalid;
471 if (limit - offset < size)
472 goto invalid;
474 offset += size;
477 done:
478 return 0;
480 invalid:
481 return -EINVAL;
484 static int restore_sigframe(struct pt_regs *regs,
485 struct rt_sigframe __user *sf)
487 sigset_t set;
488 int i, err;
489 struct user_ctxs user;
491 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
492 if (err == 0)
493 set_current_blocked(&set);
495 for (i = 0; i < 31; i++)
496 __get_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i],
497 err);
498 __get_user_error(regs->sp, &sf->uc.uc_mcontext.sp, err);
499 __get_user_error(regs->pc, &sf->uc.uc_mcontext.pc, err);
500 __get_user_error(regs->pstate, &sf->uc.uc_mcontext.pstate, err);
503 * Avoid sys_rt_sigreturn() restarting.
505 forget_syscall(regs);
507 err |= !valid_user_regs(&regs->user_regs, current);
508 if (err == 0)
509 err = parse_user_sigframe(&user, sf);
511 if (err == 0 && system_supports_fpsimd()) {
512 if (!user.fpsimd)
513 return -EINVAL;
515 if (user.sve) {
516 if (!system_supports_sve())
517 return -EINVAL;
519 err = restore_sve_fpsimd_context(&user);
520 } else {
521 err = restore_fpsimd_context(user.fpsimd);
525 return err;
528 SYSCALL_DEFINE0(rt_sigreturn)
530 struct pt_regs *regs = current_pt_regs();
531 struct rt_sigframe __user *frame;
533 /* Always make any pending restarted system calls return -EINTR */
534 current->restart_block.fn = do_no_restart_syscall;
537 * Since we stacked the signal on a 128-bit boundary, then 'sp' should
538 * be word aligned here.
540 if (regs->sp & 15)
541 goto badframe;
543 frame = (struct rt_sigframe __user *)regs->sp;
545 if (!access_ok(frame, sizeof (*frame)))
546 goto badframe;
548 if (restore_sigframe(regs, frame))
549 goto badframe;
551 if (restore_altstack(&frame->uc.uc_stack))
552 goto badframe;
554 return regs->regs[0];
556 badframe:
557 arm64_notify_segfault(regs->sp);
558 return 0;
562 * Determine the layout of optional records in the signal frame
564 * add_all: if true, lays out the biggest possible signal frame for
565 * this task; otherwise, generates a layout for the current state
566 * of the task.
568 static int setup_sigframe_layout(struct rt_sigframe_user_layout *user,
569 bool add_all)
571 int err;
573 err = sigframe_alloc(user, &user->fpsimd_offset,
574 sizeof(struct fpsimd_context));
575 if (err)
576 return err;
578 /* fault information, if valid */
579 if (add_all || current->thread.fault_code) {
580 err = sigframe_alloc(user, &user->esr_offset,
581 sizeof(struct esr_context));
582 if (err)
583 return err;
586 if (system_supports_sve()) {
587 unsigned int vq = 0;
589 if (add_all || test_thread_flag(TIF_SVE)) {
590 int vl = sve_max_vl;
592 if (!add_all)
593 vl = current->thread.sve_vl;
595 vq = sve_vq_from_vl(vl);
598 err = sigframe_alloc(user, &user->sve_offset,
599 SVE_SIG_CONTEXT_SIZE(vq));
600 if (err)
601 return err;
604 return sigframe_alloc_end(user);
607 static int setup_sigframe(struct rt_sigframe_user_layout *user,
608 struct pt_regs *regs, sigset_t *set)
610 int i, err = 0;
611 struct rt_sigframe __user *sf = user->sigframe;
613 /* set up the stack frame for unwinding */
614 __put_user_error(regs->regs[29], &user->next_frame->fp, err);
615 __put_user_error(regs->regs[30], &user->next_frame->lr, err);
617 for (i = 0; i < 31; i++)
618 __put_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i],
619 err);
620 __put_user_error(regs->sp, &sf->uc.uc_mcontext.sp, err);
621 __put_user_error(regs->pc, &sf->uc.uc_mcontext.pc, err);
622 __put_user_error(regs->pstate, &sf->uc.uc_mcontext.pstate, err);
624 __put_user_error(current->thread.fault_address, &sf->uc.uc_mcontext.fault_address, err);
626 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
628 if (err == 0 && system_supports_fpsimd()) {
629 struct fpsimd_context __user *fpsimd_ctx =
630 apply_user_offset(user, user->fpsimd_offset);
631 err |= preserve_fpsimd_context(fpsimd_ctx);
634 /* fault information, if valid */
635 if (err == 0 && user->esr_offset) {
636 struct esr_context __user *esr_ctx =
637 apply_user_offset(user, user->esr_offset);
639 __put_user_error(ESR_MAGIC, &esr_ctx->head.magic, err);
640 __put_user_error(sizeof(*esr_ctx), &esr_ctx->head.size, err);
641 __put_user_error(current->thread.fault_code, &esr_ctx->esr, err);
644 /* Scalable Vector Extension state, if present */
645 if (system_supports_sve() && err == 0 && user->sve_offset) {
646 struct sve_context __user *sve_ctx =
647 apply_user_offset(user, user->sve_offset);
648 err |= preserve_sve_context(sve_ctx);
651 if (err == 0 && user->extra_offset) {
652 char __user *sfp = (char __user *)user->sigframe;
653 char __user *userp =
654 apply_user_offset(user, user->extra_offset);
656 struct extra_context __user *extra;
657 struct _aarch64_ctx __user *end;
658 u64 extra_datap;
659 u32 extra_size;
661 extra = (struct extra_context __user *)userp;
662 userp += EXTRA_CONTEXT_SIZE;
664 end = (struct _aarch64_ctx __user *)userp;
665 userp += TERMINATOR_SIZE;
668 * extra_datap is just written to the signal frame.
669 * The value gets cast back to a void __user *
670 * during sigreturn.
672 extra_datap = (__force u64)userp;
673 extra_size = sfp + round_up(user->size, 16) - userp;
675 __put_user_error(EXTRA_MAGIC, &extra->head.magic, err);
676 __put_user_error(EXTRA_CONTEXT_SIZE, &extra->head.size, err);
677 __put_user_error(extra_datap, &extra->datap, err);
678 __put_user_error(extra_size, &extra->size, err);
680 /* Add the terminator */
681 __put_user_error(0, &end->magic, err);
682 __put_user_error(0, &end->size, err);
685 /* set the "end" magic */
686 if (err == 0) {
687 struct _aarch64_ctx __user *end =
688 apply_user_offset(user, user->end_offset);
690 __put_user_error(0, &end->magic, err);
691 __put_user_error(0, &end->size, err);
694 return err;
697 static int get_sigframe(struct rt_sigframe_user_layout *user,
698 struct ksignal *ksig, struct pt_regs *regs)
700 unsigned long sp, sp_top;
701 int err;
703 init_user_layout(user);
704 err = setup_sigframe_layout(user, false);
705 if (err)
706 return err;
708 sp = sp_top = sigsp(regs->sp, ksig);
710 sp = round_down(sp - sizeof(struct frame_record), 16);
711 user->next_frame = (struct frame_record __user *)sp;
713 sp = round_down(sp, 16) - sigframe_size(user);
714 user->sigframe = (struct rt_sigframe __user *)sp;
717 * Check that we can actually write to the signal frame.
719 if (!access_ok(user->sigframe, sp_top - sp))
720 return -EFAULT;
722 return 0;
725 static void setup_return(struct pt_regs *regs, struct k_sigaction *ka,
726 struct rt_sigframe_user_layout *user, int usig)
728 __sigrestore_t sigtramp;
730 regs->regs[0] = usig;
731 regs->sp = (unsigned long)user->sigframe;
732 regs->regs[29] = (unsigned long)&user->next_frame->fp;
733 regs->pc = (unsigned long)ka->sa.sa_handler;
735 if (ka->sa.sa_flags & SA_RESTORER)
736 sigtramp = ka->sa.sa_restorer;
737 else
738 sigtramp = VDSO_SYMBOL(current->mm->context.vdso, sigtramp);
740 regs->regs[30] = (unsigned long)sigtramp;
743 static int setup_rt_frame(int usig, struct ksignal *ksig, sigset_t *set,
744 struct pt_regs *regs)
746 struct rt_sigframe_user_layout user;
747 struct rt_sigframe __user *frame;
748 int err = 0;
750 fpsimd_signal_preserve_current_state();
752 if (get_sigframe(&user, ksig, regs))
753 return 1;
755 frame = user.sigframe;
757 __put_user_error(0, &frame->uc.uc_flags, err);
758 __put_user_error(NULL, &frame->uc.uc_link, err);
760 err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
761 err |= setup_sigframe(&user, regs, set);
762 if (err == 0) {
763 setup_return(regs, &ksig->ka, &user, usig);
764 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
765 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
766 regs->regs[1] = (unsigned long)&frame->info;
767 regs->regs[2] = (unsigned long)&frame->uc;
771 return err;
774 static void setup_restart_syscall(struct pt_regs *regs)
776 if (is_compat_task())
777 compat_setup_restart_syscall(regs);
778 else
779 regs->regs[8] = __NR_restart_syscall;
783 * OK, we're invoking a handler
785 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
787 struct task_struct *tsk = current;
788 sigset_t *oldset = sigmask_to_save();
789 int usig = ksig->sig;
790 int ret;
792 rseq_signal_deliver(ksig, regs);
795 * Set up the stack frame
797 if (is_compat_task()) {
798 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
799 ret = compat_setup_rt_frame(usig, ksig, oldset, regs);
800 else
801 ret = compat_setup_frame(usig, ksig, oldset, regs);
802 } else {
803 ret = setup_rt_frame(usig, ksig, oldset, regs);
807 * Check that the resulting registers are actually sane.
809 ret |= !valid_user_regs(&regs->user_regs, current);
812 * Fast forward the stepping logic so we step into the signal
813 * handler.
815 if (!ret)
816 user_fastforward_single_step(tsk);
818 signal_setup_done(ret, ksig, 0);
822 * Note that 'init' is a special process: it doesn't get signals it doesn't
823 * want to handle. Thus you cannot kill init even with a SIGKILL even by
824 * mistake.
826 * Note that we go through the signals twice: once to check the signals that
827 * the kernel can handle, and then we build all the user-level signal handling
828 * stack-frames in one go after that.
830 static void do_signal(struct pt_regs *regs)
832 unsigned long continue_addr = 0, restart_addr = 0;
833 int retval = 0;
834 struct ksignal ksig;
835 bool syscall = in_syscall(regs);
838 * If we were from a system call, check for system call restarting...
840 if (syscall) {
841 continue_addr = regs->pc;
842 restart_addr = continue_addr - (compat_thumb_mode(regs) ? 2 : 4);
843 retval = regs->regs[0];
846 * Avoid additional syscall restarting via ret_to_user.
848 forget_syscall(regs);
851 * Prepare for system call restart. We do this here so that a
852 * debugger will see the already changed PC.
854 switch (retval) {
855 case -ERESTARTNOHAND:
856 case -ERESTARTSYS:
857 case -ERESTARTNOINTR:
858 case -ERESTART_RESTARTBLOCK:
859 regs->regs[0] = regs->orig_x0;
860 regs->pc = restart_addr;
861 break;
866 * Get the signal to deliver. When running under ptrace, at this point
867 * the debugger may change all of our registers.
869 if (get_signal(&ksig)) {
871 * Depending on the signal settings, we may need to revert the
872 * decision to restart the system call, but skip this if a
873 * debugger has chosen to restart at a different PC.
875 if (regs->pc == restart_addr &&
876 (retval == -ERESTARTNOHAND ||
877 retval == -ERESTART_RESTARTBLOCK ||
878 (retval == -ERESTARTSYS &&
879 !(ksig.ka.sa.sa_flags & SA_RESTART)))) {
880 regs->regs[0] = -EINTR;
881 regs->pc = continue_addr;
884 handle_signal(&ksig, regs);
885 return;
889 * Handle restarting a different system call. As above, if a debugger
890 * has chosen to restart at a different PC, ignore the restart.
892 if (syscall && regs->pc == restart_addr) {
893 if (retval == -ERESTART_RESTARTBLOCK)
894 setup_restart_syscall(regs);
895 user_rewind_single_step(current);
898 restore_saved_sigmask();
901 asmlinkage void do_notify_resume(struct pt_regs *regs,
902 unsigned long thread_flags)
905 * The assembly code enters us with IRQs off, but it hasn't
906 * informed the tracing code of that for efficiency reasons.
907 * Update the trace code with the current status.
909 trace_hardirqs_off();
911 do {
912 /* Check valid user FS if needed */
913 addr_limit_user_check();
915 if (thread_flags & _TIF_NEED_RESCHED) {
916 /* Unmask Debug and SError for the next task */
917 local_daif_restore(DAIF_PROCCTX_NOIRQ);
919 schedule();
920 } else {
921 local_daif_restore(DAIF_PROCCTX);
923 if (thread_flags & _TIF_UPROBE)
924 uprobe_notify_resume(regs);
926 if (thread_flags & _TIF_SIGPENDING)
927 do_signal(regs);
929 if (thread_flags & _TIF_NOTIFY_RESUME) {
930 clear_thread_flag(TIF_NOTIFY_RESUME);
931 tracehook_notify_resume(regs);
932 rseq_handle_notify_resume(NULL, regs);
935 if (thread_flags & _TIF_FOREIGN_FPSTATE)
936 fpsimd_restore_current_state();
939 local_daif_mask();
940 thread_flags = READ_ONCE(current_thread_info()->flags);
941 } while (thread_flags & _TIF_WORK_MASK);
944 unsigned long __ro_after_init signal_minsigstksz;
947 * Determine the stack space required for guaranteed signal devliery.
948 * This function is used to populate AT_MINSIGSTKSZ at process startup.
949 * cpufeatures setup is assumed to be complete.
951 void __init minsigstksz_setup(void)
953 struct rt_sigframe_user_layout user;
955 init_user_layout(&user);
958 * If this fails, SIGFRAME_MAXSZ needs to be enlarged. It won't
959 * be big enough, but it's our best guess:
961 if (WARN_ON(setup_sigframe_layout(&user, true)))
962 return;
964 signal_minsigstksz = sigframe_size(&user) +
965 round_up(sizeof(struct frame_record), 16) +
966 16; /* max alignment padding */