xref: /openbmc/linux/arch/arm64/kernel/signal.c (revision 2359ccdd)
1 /*
2  * Based on arch/arm/kernel/signal.c
3  *
4  * Copyright (C) 1995-2009 Russell King
5  * Copyright (C) 2012 ARM Ltd.
6  *
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.
10  *
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.
15  *
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/>.
18  */
19 
20 #include <linux/compat.h>
21 #include <linux/errno.h>
22 #include <linux/kernel.h>
23 #include <linux/signal.h>
24 #include <linux/personality.h>
25 #include <linux/freezer.h>
26 #include <linux/stddef.h>
27 #include <linux/uaccess.h>
28 #include <linux/sizes.h>
29 #include <linux/string.h>
30 #include <linux/tracehook.h>
31 #include <linux/ratelimit.h>
32 #include <linux/syscalls.h>
33 
34 #include <asm/daifflags.h>
35 #include <asm/debug-monitors.h>
36 #include <asm/elf.h>
37 #include <asm/cacheflush.h>
38 #include <asm/ucontext.h>
39 #include <asm/unistd.h>
40 #include <asm/fpsimd.h>
41 #include <asm/ptrace.h>
42 #include <asm/signal32.h>
43 #include <asm/traps.h>
44 #include <asm/vdso.h>
45 
46 /*
47  * Do a signal return; undo the signal stack. These are aligned to 128-bit.
48  */
49 struct rt_sigframe {
50 	struct siginfo info;
51 	struct ucontext uc;
52 };
53 
54 struct frame_record {
55 	u64 fp;
56 	u64 lr;
57 };
58 
59 struct rt_sigframe_user_layout {
60 	struct rt_sigframe __user *sigframe;
61 	struct frame_record __user *next_frame;
62 
63 	unsigned long size;	/* size of allocated sigframe data */
64 	unsigned long limit;	/* largest allowed size */
65 
66 	unsigned long fpsimd_offset;
67 	unsigned long esr_offset;
68 	unsigned long sve_offset;
69 	unsigned long extra_offset;
70 	unsigned long end_offset;
71 };
72 
73 #define BASE_SIGFRAME_SIZE round_up(sizeof(struct rt_sigframe), 16)
74 #define TERMINATOR_SIZE round_up(sizeof(struct _aarch64_ctx), 16)
75 #define EXTRA_CONTEXT_SIZE round_up(sizeof(struct extra_context), 16)
76 
77 static void init_user_layout(struct rt_sigframe_user_layout *user)
78 {
79 	const size_t reserved_size =
80 		sizeof(user->sigframe->uc.uc_mcontext.__reserved);
81 
82 	memset(user, 0, sizeof(*user));
83 	user->size = offsetof(struct rt_sigframe, uc.uc_mcontext.__reserved);
84 
85 	user->limit = user->size + reserved_size;
86 
87 	user->limit -= TERMINATOR_SIZE;
88 	user->limit -= EXTRA_CONTEXT_SIZE;
89 	/* Reserve space for extension and terminator ^ */
90 }
91 
92 static size_t sigframe_size(struct rt_sigframe_user_layout const *user)
93 {
94 	return round_up(max(user->size, sizeof(struct rt_sigframe)), 16);
95 }
96 
97 /*
98  * Sanity limit on the approximate maximum size of signal frame we'll
99  * try to generate.  Stack alignment padding and the frame record are
100  * not taken into account.  This limit is not a guarantee and is
101  * NOT ABI.
102  */
103 #define SIGFRAME_MAXSZ SZ_64K
104 
105 static int __sigframe_alloc(struct rt_sigframe_user_layout *user,
106 			    unsigned long *offset, size_t size, bool extend)
107 {
108 	size_t padded_size = round_up(size, 16);
109 
110 	if (padded_size > user->limit - user->size &&
111 	    !user->extra_offset &&
112 	    extend) {
113 		int ret;
114 
115 		user->limit += EXTRA_CONTEXT_SIZE;
116 		ret = __sigframe_alloc(user, &user->extra_offset,
117 				       sizeof(struct extra_context), false);
118 		if (ret) {
119 			user->limit -= EXTRA_CONTEXT_SIZE;
120 			return ret;
121 		}
122 
123 		/* Reserve space for the __reserved[] terminator */
124 		user->size += TERMINATOR_SIZE;
125 
126 		/*
127 		 * Allow expansion up to SIGFRAME_MAXSZ, ensuring space for
128 		 * the terminator:
129 		 */
130 		user->limit = SIGFRAME_MAXSZ - TERMINATOR_SIZE;
131 	}
132 
133 	/* Still not enough space?  Bad luck! */
134 	if (padded_size > user->limit - user->size)
135 		return -ENOMEM;
136 
137 	*offset = user->size;
138 	user->size += padded_size;
139 
140 	return 0;
141 }
142 
143 /*
144  * Allocate space for an optional record of <size> bytes in the user
145  * signal frame.  The offset from the signal frame base address to the
146  * allocated block is assigned to *offset.
147  */
148 static int sigframe_alloc(struct rt_sigframe_user_layout *user,
149 			  unsigned long *offset, size_t size)
150 {
151 	return __sigframe_alloc(user, offset, size, true);
152 }
153 
154 /* Allocate the null terminator record and prevent further allocations */
155 static int sigframe_alloc_end(struct rt_sigframe_user_layout *user)
156 {
157 	int ret;
158 
159 	/* Un-reserve the space reserved for the terminator: */
160 	user->limit += TERMINATOR_SIZE;
161 
162 	ret = sigframe_alloc(user, &user->end_offset,
163 			     sizeof(struct _aarch64_ctx));
164 	if (ret)
165 		return ret;
166 
167 	/* Prevent further allocation: */
168 	user->limit = user->size;
169 	return 0;
170 }
171 
172 static void __user *apply_user_offset(
173 	struct rt_sigframe_user_layout const *user, unsigned long offset)
174 {
175 	char __user *base = (char __user *)user->sigframe;
176 
177 	return base + offset;
178 }
179 
180 static int preserve_fpsimd_context(struct fpsimd_context __user *ctx)
181 {
182 	struct user_fpsimd_state const *fpsimd =
183 		&current->thread.uw.fpsimd_state;
184 	int err;
185 
186 	/* copy the FP and status/control registers */
187 	err = __copy_to_user(ctx->vregs, fpsimd->vregs, sizeof(fpsimd->vregs));
188 	__put_user_error(fpsimd->fpsr, &ctx->fpsr, err);
189 	__put_user_error(fpsimd->fpcr, &ctx->fpcr, err);
190 
191 	/* copy the magic/size information */
192 	__put_user_error(FPSIMD_MAGIC, &ctx->head.magic, err);
193 	__put_user_error(sizeof(struct fpsimd_context), &ctx->head.size, err);
194 
195 	return err ? -EFAULT : 0;
196 }
197 
198 static int restore_fpsimd_context(struct fpsimd_context __user *ctx)
199 {
200 	struct user_fpsimd_state fpsimd;
201 	__u32 magic, size;
202 	int err = 0;
203 
204 	/* check the magic/size information */
205 	__get_user_error(magic, &ctx->head.magic, err);
206 	__get_user_error(size, &ctx->head.size, err);
207 	if (err)
208 		return -EFAULT;
209 	if (magic != FPSIMD_MAGIC || size != sizeof(struct fpsimd_context))
210 		return -EINVAL;
211 
212 	/* copy the FP and status/control registers */
213 	err = __copy_from_user(fpsimd.vregs, ctx->vregs,
214 			       sizeof(fpsimd.vregs));
215 	__get_user_error(fpsimd.fpsr, &ctx->fpsr, err);
216 	__get_user_error(fpsimd.fpcr, &ctx->fpcr, err);
217 
218 	clear_thread_flag(TIF_SVE);
219 
220 	/* load the hardware registers from the fpsimd_state structure */
221 	if (!err)
222 		fpsimd_update_current_state(&fpsimd);
223 
224 	return err ? -EFAULT : 0;
225 }
226 
227 
228 struct user_ctxs {
229 	struct fpsimd_context __user *fpsimd;
230 	struct sve_context __user *sve;
231 };
232 
233 #ifdef CONFIG_ARM64_SVE
234 
235 static int preserve_sve_context(struct sve_context __user *ctx)
236 {
237 	int err = 0;
238 	u16 reserved[ARRAY_SIZE(ctx->__reserved)];
239 	unsigned int vl = current->thread.sve_vl;
240 	unsigned int vq = 0;
241 
242 	if (test_thread_flag(TIF_SVE))
243 		vq = sve_vq_from_vl(vl);
244 
245 	memset(reserved, 0, sizeof(reserved));
246 
247 	__put_user_error(SVE_MAGIC, &ctx->head.magic, err);
248 	__put_user_error(round_up(SVE_SIG_CONTEXT_SIZE(vq), 16),
249 			 &ctx->head.size, err);
250 	__put_user_error(vl, &ctx->vl, err);
251 	BUILD_BUG_ON(sizeof(ctx->__reserved) != sizeof(reserved));
252 	err |= __copy_to_user(&ctx->__reserved, reserved, sizeof(reserved));
253 
254 	if (vq) {
255 		/*
256 		 * This assumes that the SVE state has already been saved to
257 		 * the task struct by calling preserve_fpsimd_context().
258 		 */
259 		err |= __copy_to_user((char __user *)ctx + SVE_SIG_REGS_OFFSET,
260 				      current->thread.sve_state,
261 				      SVE_SIG_REGS_SIZE(vq));
262 	}
263 
264 	return err ? -EFAULT : 0;
265 }
266 
267 static int restore_sve_fpsimd_context(struct user_ctxs *user)
268 {
269 	int err;
270 	unsigned int vq;
271 	struct user_fpsimd_state fpsimd;
272 	struct sve_context sve;
273 
274 	if (__copy_from_user(&sve, user->sve, sizeof(sve)))
275 		return -EFAULT;
276 
277 	if (sve.vl != current->thread.sve_vl)
278 		return -EINVAL;
279 
280 	if (sve.head.size <= sizeof(*user->sve)) {
281 		clear_thread_flag(TIF_SVE);
282 		goto fpsimd_only;
283 	}
284 
285 	vq = sve_vq_from_vl(sve.vl);
286 
287 	if (sve.head.size < SVE_SIG_CONTEXT_SIZE(vq))
288 		return -EINVAL;
289 
290 	/*
291 	 * Careful: we are about __copy_from_user() directly into
292 	 * thread.sve_state with preemption enabled, so protection is
293 	 * needed to prevent a racing context switch from writing stale
294 	 * registers back over the new data.
295 	 */
296 
297 	fpsimd_flush_task_state(current);
298 	barrier();
299 	/* From now, fpsimd_thread_switch() won't clear TIF_FOREIGN_FPSTATE */
300 
301 	set_thread_flag(TIF_FOREIGN_FPSTATE);
302 	barrier();
303 	/* From now, fpsimd_thread_switch() won't touch thread.sve_state */
304 
305 	sve_alloc(current);
306 	err = __copy_from_user(current->thread.sve_state,
307 			       (char __user const *)user->sve +
308 					SVE_SIG_REGS_OFFSET,
309 			       SVE_SIG_REGS_SIZE(vq));
310 	if (err)
311 		return -EFAULT;
312 
313 	set_thread_flag(TIF_SVE);
314 
315 fpsimd_only:
316 	/* copy the FP and status/control registers */
317 	/* restore_sigframe() already checked that user->fpsimd != NULL. */
318 	err = __copy_from_user(fpsimd.vregs, user->fpsimd->vregs,
319 			       sizeof(fpsimd.vregs));
320 	__get_user_error(fpsimd.fpsr, &user->fpsimd->fpsr, err);
321 	__get_user_error(fpsimd.fpcr, &user->fpsimd->fpcr, err);
322 
323 	/* load the hardware registers from the fpsimd_state structure */
324 	if (!err)
325 		fpsimd_update_current_state(&fpsimd);
326 
327 	return err ? -EFAULT : 0;
328 }
329 
330 #else /* ! CONFIG_ARM64_SVE */
331 
332 /* Turn any non-optimised out attempts to use these into a link error: */
333 extern int preserve_sve_context(void __user *ctx);
334 extern int restore_sve_fpsimd_context(struct user_ctxs *user);
335 
336 #endif /* ! CONFIG_ARM64_SVE */
337 
338 
339 static int parse_user_sigframe(struct user_ctxs *user,
340 			       struct rt_sigframe __user *sf)
341 {
342 	struct sigcontext __user *const sc = &sf->uc.uc_mcontext;
343 	struct _aarch64_ctx __user *head;
344 	char __user *base = (char __user *)&sc->__reserved;
345 	size_t offset = 0;
346 	size_t limit = sizeof(sc->__reserved);
347 	bool have_extra_context = false;
348 	char const __user *const sfp = (char const __user *)sf;
349 
350 	user->fpsimd = NULL;
351 	user->sve = NULL;
352 
353 	if (!IS_ALIGNED((unsigned long)base, 16))
354 		goto invalid;
355 
356 	while (1) {
357 		int err = 0;
358 		u32 magic, size;
359 		char const __user *userp;
360 		struct extra_context const __user *extra;
361 		u64 extra_datap;
362 		u32 extra_size;
363 		struct _aarch64_ctx const __user *end;
364 		u32 end_magic, end_size;
365 
366 		if (limit - offset < sizeof(*head))
367 			goto invalid;
368 
369 		if (!IS_ALIGNED(offset, 16))
370 			goto invalid;
371 
372 		head = (struct _aarch64_ctx __user *)(base + offset);
373 		__get_user_error(magic, &head->magic, err);
374 		__get_user_error(size, &head->size, err);
375 		if (err)
376 			return err;
377 
378 		if (limit - offset < size)
379 			goto invalid;
380 
381 		switch (magic) {
382 		case 0:
383 			if (size)
384 				goto invalid;
385 
386 			goto done;
387 
388 		case FPSIMD_MAGIC:
389 			if (user->fpsimd)
390 				goto invalid;
391 
392 			if (size < sizeof(*user->fpsimd))
393 				goto invalid;
394 
395 			user->fpsimd = (struct fpsimd_context __user *)head;
396 			break;
397 
398 		case ESR_MAGIC:
399 			/* ignore */
400 			break;
401 
402 		case SVE_MAGIC:
403 			if (!system_supports_sve())
404 				goto invalid;
405 
406 			if (user->sve)
407 				goto invalid;
408 
409 			if (size < sizeof(*user->sve))
410 				goto invalid;
411 
412 			user->sve = (struct sve_context __user *)head;
413 			break;
414 
415 		case EXTRA_MAGIC:
416 			if (have_extra_context)
417 				goto invalid;
418 
419 			if (size < sizeof(*extra))
420 				goto invalid;
421 
422 			userp = (char const __user *)head;
423 
424 			extra = (struct extra_context const __user *)userp;
425 			userp += size;
426 
427 			__get_user_error(extra_datap, &extra->datap, err);
428 			__get_user_error(extra_size, &extra->size, err);
429 			if (err)
430 				return err;
431 
432 			/* Check for the dummy terminator in __reserved[]: */
433 
434 			if (limit - offset - size < TERMINATOR_SIZE)
435 				goto invalid;
436 
437 			end = (struct _aarch64_ctx const __user *)userp;
438 			userp += TERMINATOR_SIZE;
439 
440 			__get_user_error(end_magic, &end->magic, err);
441 			__get_user_error(end_size, &end->size, err);
442 			if (err)
443 				return err;
444 
445 			if (end_magic || end_size)
446 				goto invalid;
447 
448 			/* Prevent looping/repeated parsing of extra_context */
449 			have_extra_context = true;
450 
451 			base = (__force void __user *)extra_datap;
452 			if (!IS_ALIGNED((unsigned long)base, 16))
453 				goto invalid;
454 
455 			if (!IS_ALIGNED(extra_size, 16))
456 				goto invalid;
457 
458 			if (base != userp)
459 				goto invalid;
460 
461 			/* Reject "unreasonably large" frames: */
462 			if (extra_size > sfp + SIGFRAME_MAXSZ - userp)
463 				goto invalid;
464 
465 			/*
466 			 * Ignore trailing terminator in __reserved[]
467 			 * and start parsing extra data:
468 			 */
469 			offset = 0;
470 			limit = extra_size;
471 
472 			if (!access_ok(VERIFY_READ, base, limit))
473 				goto invalid;
474 
475 			continue;
476 
477 		default:
478 			goto invalid;
479 		}
480 
481 		if (size < sizeof(*head))
482 			goto invalid;
483 
484 		if (limit - offset < size)
485 			goto invalid;
486 
487 		offset += size;
488 	}
489 
490 done:
491 	return 0;
492 
493 invalid:
494 	return -EINVAL;
495 }
496 
497 static int restore_sigframe(struct pt_regs *regs,
498 			    struct rt_sigframe __user *sf)
499 {
500 	sigset_t set;
501 	int i, err;
502 	struct user_ctxs user;
503 
504 	err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
505 	if (err == 0)
506 		set_current_blocked(&set);
507 
508 	for (i = 0; i < 31; i++)
509 		__get_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i],
510 				 err);
511 	__get_user_error(regs->sp, &sf->uc.uc_mcontext.sp, err);
512 	__get_user_error(regs->pc, &sf->uc.uc_mcontext.pc, err);
513 	__get_user_error(regs->pstate, &sf->uc.uc_mcontext.pstate, err);
514 
515 	/*
516 	 * Avoid sys_rt_sigreturn() restarting.
517 	 */
518 	forget_syscall(regs);
519 
520 	err |= !valid_user_regs(&regs->user_regs, current);
521 	if (err == 0)
522 		err = parse_user_sigframe(&user, sf);
523 
524 	if (err == 0) {
525 		if (!user.fpsimd)
526 			return -EINVAL;
527 
528 		if (user.sve) {
529 			if (!system_supports_sve())
530 				return -EINVAL;
531 
532 			err = restore_sve_fpsimd_context(&user);
533 		} else {
534 			err = restore_fpsimd_context(user.fpsimd);
535 		}
536 	}
537 
538 	return err;
539 }
540 
541 asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
542 {
543 	struct rt_sigframe __user *frame;
544 
545 	/* Always make any pending restarted system calls return -EINTR */
546 	current->restart_block.fn = do_no_restart_syscall;
547 
548 	/*
549 	 * Since we stacked the signal on a 128-bit boundary, then 'sp' should
550 	 * be word aligned here.
551 	 */
552 	if (regs->sp & 15)
553 		goto badframe;
554 
555 	frame = (struct rt_sigframe __user *)regs->sp;
556 
557 	if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
558 		goto badframe;
559 
560 	if (restore_sigframe(regs, frame))
561 		goto badframe;
562 
563 	if (restore_altstack(&frame->uc.uc_stack))
564 		goto badframe;
565 
566 	return regs->regs[0];
567 
568 badframe:
569 	arm64_notify_segfault(regs->sp);
570 	return 0;
571 }
572 
573 /* Determine the layout of optional records in the signal frame */
574 static int setup_sigframe_layout(struct rt_sigframe_user_layout *user)
575 {
576 	int err;
577 
578 	err = sigframe_alloc(user, &user->fpsimd_offset,
579 			     sizeof(struct fpsimd_context));
580 	if (err)
581 		return err;
582 
583 	/* fault information, if valid */
584 	if (current->thread.fault_code) {
585 		err = sigframe_alloc(user, &user->esr_offset,
586 				     sizeof(struct esr_context));
587 		if (err)
588 			return err;
589 	}
590 
591 	if (system_supports_sve()) {
592 		unsigned int vq = 0;
593 
594 		if (test_thread_flag(TIF_SVE))
595 			vq = sve_vq_from_vl(current->thread.sve_vl);
596 
597 		err = sigframe_alloc(user, &user->sve_offset,
598 				     SVE_SIG_CONTEXT_SIZE(vq));
599 		if (err)
600 			return err;
601 	}
602 
603 	return sigframe_alloc_end(user);
604 }
605 
606 
607 static int setup_sigframe(struct rt_sigframe_user_layout *user,
608 			  struct pt_regs *regs, sigset_t *set)
609 {
610 	int i, err = 0;
611 	struct rt_sigframe __user *sf = user->sigframe;
612 
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);
616 
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);
623 
624 	__put_user_error(current->thread.fault_address, &sf->uc.uc_mcontext.fault_address, err);
625 
626 	err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
627 
628 	if (err == 0) {
629 		struct fpsimd_context __user *fpsimd_ctx =
630 			apply_user_offset(user, user->fpsimd_offset);
631 		err |= preserve_fpsimd_context(fpsimd_ctx);
632 	}
633 
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);
638 
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);
642 	}
643 
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);
649 	}
650 
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);
655 
656 		struct extra_context __user *extra;
657 		struct _aarch64_ctx __user *end;
658 		u64 extra_datap;
659 		u32 extra_size;
660 
661 		extra = (struct extra_context __user *)userp;
662 		userp += EXTRA_CONTEXT_SIZE;
663 
664 		end = (struct _aarch64_ctx __user *)userp;
665 		userp += TERMINATOR_SIZE;
666 
667 		/*
668 		 * extra_datap is just written to the signal frame.
669 		 * The value gets cast back to a void __user *
670 		 * during sigreturn.
671 		 */
672 		extra_datap = (__force u64)userp;
673 		extra_size = sfp + round_up(user->size, 16) - userp;
674 
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);
679 
680 		/* Add the terminator */
681 		__put_user_error(0, &end->magic, err);
682 		__put_user_error(0, &end->size, err);
683 	}
684 
685 	/* set the "end" magic */
686 	if (err == 0) {
687 		struct _aarch64_ctx __user *end =
688 			apply_user_offset(user, user->end_offset);
689 
690 		__put_user_error(0, &end->magic, err);
691 		__put_user_error(0, &end->size, err);
692 	}
693 
694 	return err;
695 }
696 
697 static int get_sigframe(struct rt_sigframe_user_layout *user,
698 			 struct ksignal *ksig, struct pt_regs *regs)
699 {
700 	unsigned long sp, sp_top;
701 	int err;
702 
703 	init_user_layout(user);
704 	err = setup_sigframe_layout(user);
705 	if (err)
706 		return err;
707 
708 	sp = sp_top = sigsp(regs->sp, ksig);
709 
710 	sp = round_down(sp - sizeof(struct frame_record), 16);
711 	user->next_frame = (struct frame_record __user *)sp;
712 
713 	sp = round_down(sp, 16) - sigframe_size(user);
714 	user->sigframe = (struct rt_sigframe __user *)sp;
715 
716 	/*
717 	 * Check that we can actually write to the signal frame.
718 	 */
719 	if (!access_ok(VERIFY_WRITE, user->sigframe, sp_top - sp))
720 		return -EFAULT;
721 
722 	return 0;
723 }
724 
725 static void setup_return(struct pt_regs *regs, struct k_sigaction *ka,
726 			 struct rt_sigframe_user_layout *user, int usig)
727 {
728 	__sigrestore_t sigtramp;
729 
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;
734 
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);
739 
740 	regs->regs[30] = (unsigned long)sigtramp;
741 }
742 
743 static int setup_rt_frame(int usig, struct ksignal *ksig, sigset_t *set,
744 			  struct pt_regs *regs)
745 {
746 	struct rt_sigframe_user_layout user;
747 	struct rt_sigframe __user *frame;
748 	int err = 0;
749 
750 	fpsimd_signal_preserve_current_state();
751 
752 	if (get_sigframe(&user, ksig, regs))
753 		return 1;
754 
755 	frame = user.sigframe;
756 
757 	__put_user_error(0, &frame->uc.uc_flags, err);
758 	__put_user_error(NULL, &frame->uc.uc_link, err);
759 
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;
768 		}
769 	}
770 
771 	return err;
772 }
773 
774 static void setup_restart_syscall(struct pt_regs *regs)
775 {
776 	if (is_compat_task())
777 		compat_setup_restart_syscall(regs);
778 	else
779 		regs->regs[8] = __NR_restart_syscall;
780 }
781 
782 /*
783  * OK, we're invoking a handler
784  */
785 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
786 {
787 	struct task_struct *tsk = current;
788 	sigset_t *oldset = sigmask_to_save();
789 	int usig = ksig->sig;
790 	int ret;
791 
792 	/*
793 	 * Set up the stack frame
794 	 */
795 	if (is_compat_task()) {
796 		if (ksig->ka.sa.sa_flags & SA_SIGINFO)
797 			ret = compat_setup_rt_frame(usig, ksig, oldset, regs);
798 		else
799 			ret = compat_setup_frame(usig, ksig, oldset, regs);
800 	} else {
801 		ret = setup_rt_frame(usig, ksig, oldset, regs);
802 	}
803 
804 	/*
805 	 * Check that the resulting registers are actually sane.
806 	 */
807 	ret |= !valid_user_regs(&regs->user_regs, current);
808 
809 	/*
810 	 * Fast forward the stepping logic so we step into the signal
811 	 * handler.
812 	 */
813 	if (!ret)
814 		user_fastforward_single_step(tsk);
815 
816 	signal_setup_done(ret, ksig, 0);
817 }
818 
819 /*
820  * Note that 'init' is a special process: it doesn't get signals it doesn't
821  * want to handle. Thus you cannot kill init even with a SIGKILL even by
822  * mistake.
823  *
824  * Note that we go through the signals twice: once to check the signals that
825  * the kernel can handle, and then we build all the user-level signal handling
826  * stack-frames in one go after that.
827  */
828 static void do_signal(struct pt_regs *regs)
829 {
830 	unsigned long continue_addr = 0, restart_addr = 0;
831 	int retval = 0;
832 	struct ksignal ksig;
833 
834 	/*
835 	 * If we were from a system call, check for system call restarting...
836 	 */
837 	if (in_syscall(regs)) {
838 		continue_addr = regs->pc;
839 		restart_addr = continue_addr - (compat_thumb_mode(regs) ? 2 : 4);
840 		retval = regs->regs[0];
841 
842 		/*
843 		 * Avoid additional syscall restarting via ret_to_user.
844 		 */
845 		forget_syscall(regs);
846 
847 		/*
848 		 * Prepare for system call restart. We do this here so that a
849 		 * debugger will see the already changed PC.
850 		 */
851 		switch (retval) {
852 		case -ERESTARTNOHAND:
853 		case -ERESTARTSYS:
854 		case -ERESTARTNOINTR:
855 		case -ERESTART_RESTARTBLOCK:
856 			regs->regs[0] = regs->orig_x0;
857 			regs->pc = restart_addr;
858 			break;
859 		}
860 	}
861 
862 	/*
863 	 * Get the signal to deliver. When running under ptrace, at this point
864 	 * the debugger may change all of our registers.
865 	 */
866 	if (get_signal(&ksig)) {
867 		/*
868 		 * Depending on the signal settings, we may need to revert the
869 		 * decision to restart the system call, but skip this if a
870 		 * debugger has chosen to restart at a different PC.
871 		 */
872 		if (regs->pc == restart_addr &&
873 		    (retval == -ERESTARTNOHAND ||
874 		     retval == -ERESTART_RESTARTBLOCK ||
875 		     (retval == -ERESTARTSYS &&
876 		      !(ksig.ka.sa.sa_flags & SA_RESTART)))) {
877 			regs->regs[0] = -EINTR;
878 			regs->pc = continue_addr;
879 		}
880 
881 		handle_signal(&ksig, regs);
882 		return;
883 	}
884 
885 	/*
886 	 * Handle restarting a different system call. As above, if a debugger
887 	 * has chosen to restart at a different PC, ignore the restart.
888 	 */
889 	if (in_syscall(regs) && regs->pc == restart_addr) {
890 		if (retval == -ERESTART_RESTARTBLOCK)
891 			setup_restart_syscall(regs);
892 		user_rewind_single_step(current);
893 	}
894 
895 	restore_saved_sigmask();
896 }
897 
898 asmlinkage void do_notify_resume(struct pt_regs *regs,
899 				 unsigned int thread_flags)
900 {
901 	/*
902 	 * The assembly code enters us with IRQs off, but it hasn't
903 	 * informed the tracing code of that for efficiency reasons.
904 	 * Update the trace code with the current status.
905 	 */
906 	trace_hardirqs_off();
907 
908 	do {
909 		/* Check valid user FS if needed */
910 		addr_limit_user_check();
911 
912 		if (thread_flags & _TIF_NEED_RESCHED) {
913 			/* Unmask Debug and SError for the next task */
914 			local_daif_restore(DAIF_PROCCTX_NOIRQ);
915 
916 			schedule();
917 		} else {
918 			local_daif_restore(DAIF_PROCCTX);
919 
920 			if (thread_flags & _TIF_UPROBE)
921 				uprobe_notify_resume(regs);
922 
923 			if (thread_flags & _TIF_SIGPENDING)
924 				do_signal(regs);
925 
926 			if (thread_flags & _TIF_NOTIFY_RESUME) {
927 				clear_thread_flag(TIF_NOTIFY_RESUME);
928 				tracehook_notify_resume(regs);
929 			}
930 
931 			if (thread_flags & _TIF_FOREIGN_FPSTATE)
932 				fpsimd_restore_current_state();
933 		}
934 
935 		local_daif_mask();
936 		thread_flags = READ_ONCE(current_thread_info()->flags);
937 	} while (thread_flags & _TIF_WORK_MASK);
938 }
939