xref: /openbmc/linux/arch/arm64/kernel/signal.c (revision a93fbb00)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Based on arch/arm/kernel/signal.c
4  *
5  * Copyright (C) 1995-2009 Russell King
6  * Copyright (C) 2012 ARM Ltd.
7  */
8 
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>
22 
23 #include <asm/daifflags.h>
24 #include <asm/debug-monitors.h>
25 #include <asm/elf.h>
26 #include <asm/cacheflush.h>
27 #include <asm/ucontext.h>
28 #include <asm/unistd.h>
29 #include <asm/fpsimd.h>
30 #include <asm/ptrace.h>
31 #include <asm/syscall.h>
32 #include <asm/signal32.h>
33 #include <asm/traps.h>
34 #include <asm/vdso.h>
35 
36 /*
37  * Do a signal return; undo the signal stack. These are aligned to 128-bit.
38  */
39 struct rt_sigframe {
40 	struct siginfo info;
41 	struct ucontext uc;
42 };
43 
44 struct frame_record {
45 	u64 fp;
46 	u64 lr;
47 };
48 
49 struct rt_sigframe_user_layout {
50 	struct rt_sigframe __user *sigframe;
51 	struct frame_record __user *next_frame;
52 
53 	unsigned long size;	/* size of allocated sigframe data */
54 	unsigned long limit;	/* largest allowed size */
55 
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;
61 };
62 
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)
66 
67 static void init_user_layout(struct rt_sigframe_user_layout *user)
68 {
69 	const size_t reserved_size =
70 		sizeof(user->sigframe->uc.uc_mcontext.__reserved);
71 
72 	memset(user, 0, sizeof(*user));
73 	user->size = offsetof(struct rt_sigframe, uc.uc_mcontext.__reserved);
74 
75 	user->limit = user->size + reserved_size;
76 
77 	user->limit -= TERMINATOR_SIZE;
78 	user->limit -= EXTRA_CONTEXT_SIZE;
79 	/* Reserve space for extension and terminator ^ */
80 }
81 
82 static size_t sigframe_size(struct rt_sigframe_user_layout const *user)
83 {
84 	return round_up(max(user->size, sizeof(struct rt_sigframe)), 16);
85 }
86 
87 /*
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.
92  */
93 #define SIGFRAME_MAXSZ SZ_64K
94 
95 static int __sigframe_alloc(struct rt_sigframe_user_layout *user,
96 			    unsigned long *offset, size_t size, bool extend)
97 {
98 	size_t padded_size = round_up(size, 16);
99 
100 	if (padded_size > user->limit - user->size &&
101 	    !user->extra_offset &&
102 	    extend) {
103 		int ret;
104 
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;
111 		}
112 
113 		/* Reserve space for the __reserved[] terminator */
114 		user->size += TERMINATOR_SIZE;
115 
116 		/*
117 		 * Allow expansion up to SIGFRAME_MAXSZ, ensuring space for
118 		 * the terminator:
119 		 */
120 		user->limit = SIGFRAME_MAXSZ - TERMINATOR_SIZE;
121 	}
122 
123 	/* Still not enough space?  Bad luck! */
124 	if (padded_size > user->limit - user->size)
125 		return -ENOMEM;
126 
127 	*offset = user->size;
128 	user->size += padded_size;
129 
130 	return 0;
131 }
132 
133 /*
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.
137  */
138 static int sigframe_alloc(struct rt_sigframe_user_layout *user,
139 			  unsigned long *offset, size_t size)
140 {
141 	return __sigframe_alloc(user, offset, size, true);
142 }
143 
144 /* Allocate the null terminator record and prevent further allocations */
145 static int sigframe_alloc_end(struct rt_sigframe_user_layout *user)
146 {
147 	int ret;
148 
149 	/* Un-reserve the space reserved for the terminator: */
150 	user->limit += TERMINATOR_SIZE;
151 
152 	ret = sigframe_alloc(user, &user->end_offset,
153 			     sizeof(struct _aarch64_ctx));
154 	if (ret)
155 		return ret;
156 
157 	/* Prevent further allocation: */
158 	user->limit = user->size;
159 	return 0;
160 }
161 
162 static void __user *apply_user_offset(
163 	struct rt_sigframe_user_layout const *user, unsigned long offset)
164 {
165 	char __user *base = (char __user *)user->sigframe;
166 
167 	return base + offset;
168 }
169 
170 static int preserve_fpsimd_context(struct fpsimd_context __user *ctx)
171 {
172 	struct user_fpsimd_state const *fpsimd =
173 		&current->thread.uw.fpsimd_state;
174 	int err;
175 
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);
180 
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);
184 
185 	return err ? -EFAULT : 0;
186 }
187 
188 static int restore_fpsimd_context(struct fpsimd_context __user *ctx)
189 {
190 	struct user_fpsimd_state fpsimd;
191 	__u32 magic, size;
192 	int err = 0;
193 
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;
201 
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);
207 
208 	clear_thread_flag(TIF_SVE);
209 
210 	/* load the hardware registers from the fpsimd_state structure */
211 	if (!err)
212 		fpsimd_update_current_state(&fpsimd);
213 
214 	return err ? -EFAULT : 0;
215 }
216 
217 
218 struct user_ctxs {
219 	struct fpsimd_context __user *fpsimd;
220 	struct sve_context __user *sve;
221 };
222 
223 #ifdef CONFIG_ARM64_SVE
224 
225 static int preserve_sve_context(struct sve_context __user *ctx)
226 {
227 	int err = 0;
228 	u16 reserved[ARRAY_SIZE(ctx->__reserved)];
229 	unsigned int vl = task_get_sve_vl(current);
230 	unsigned int vq = 0;
231 
232 	if (test_thread_flag(TIF_SVE))
233 		vq = sve_vq_from_vl(vl);
234 
235 	memset(reserved, 0, sizeof(reserved));
236 
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));
243 
244 	if (vq) {
245 		/*
246 		 * This assumes that the SVE state has already been saved to
247 		 * the task struct by calling the function
248 		 * fpsimd_signal_preserve_current_state().
249 		 */
250 		err |= __copy_to_user((char __user *)ctx + SVE_SIG_REGS_OFFSET,
251 				      current->thread.sve_state,
252 				      SVE_SIG_REGS_SIZE(vq));
253 	}
254 
255 	return err ? -EFAULT : 0;
256 }
257 
258 static int restore_sve_fpsimd_context(struct user_ctxs *user)
259 {
260 	int err;
261 	unsigned int vq;
262 	struct user_fpsimd_state fpsimd;
263 	struct sve_context sve;
264 
265 	if (__copy_from_user(&sve, user->sve, sizeof(sve)))
266 		return -EFAULT;
267 
268 	if (sve.vl != task_get_sve_vl(current))
269 		return -EINVAL;
270 
271 	if (sve.head.size <= sizeof(*user->sve)) {
272 		clear_thread_flag(TIF_SVE);
273 		goto fpsimd_only;
274 	}
275 
276 	vq = sve_vq_from_vl(sve.vl);
277 
278 	if (sve.head.size < SVE_SIG_CONTEXT_SIZE(vq))
279 		return -EINVAL;
280 
281 	/*
282 	 * Careful: we are about __copy_from_user() directly into
283 	 * thread.sve_state with preemption enabled, so protection is
284 	 * needed to prevent a racing context switch from writing stale
285 	 * registers back over the new data.
286 	 */
287 
288 	fpsimd_flush_task_state(current);
289 	/* From now, fpsimd_thread_switch() won't touch thread.sve_state */
290 
291 	sve_alloc(current);
292 	if (!current->thread.sve_state) {
293 		clear_thread_flag(TIF_SVE);
294 		return -ENOMEM;
295 	}
296 
297 	err = __copy_from_user(current->thread.sve_state,
298 			       (char __user const *)user->sve +
299 					SVE_SIG_REGS_OFFSET,
300 			       SVE_SIG_REGS_SIZE(vq));
301 	if (err)
302 		return -EFAULT;
303 
304 	set_thread_flag(TIF_SVE);
305 
306 fpsimd_only:
307 	/* copy the FP and status/control registers */
308 	/* restore_sigframe() already checked that user->fpsimd != NULL. */
309 	err = __copy_from_user(fpsimd.vregs, user->fpsimd->vregs,
310 			       sizeof(fpsimd.vregs));
311 	__get_user_error(fpsimd.fpsr, &user->fpsimd->fpsr, err);
312 	__get_user_error(fpsimd.fpcr, &user->fpsimd->fpcr, err);
313 
314 	/* load the hardware registers from the fpsimd_state structure */
315 	if (!err)
316 		fpsimd_update_current_state(&fpsimd);
317 
318 	return err ? -EFAULT : 0;
319 }
320 
321 #else /* ! CONFIG_ARM64_SVE */
322 
323 /* Turn any non-optimised out attempts to use these into a link error: */
324 extern int preserve_sve_context(void __user *ctx);
325 extern int restore_sve_fpsimd_context(struct user_ctxs *user);
326 
327 #endif /* ! CONFIG_ARM64_SVE */
328 
329 
330 static int parse_user_sigframe(struct user_ctxs *user,
331 			       struct rt_sigframe __user *sf)
332 {
333 	struct sigcontext __user *const sc = &sf->uc.uc_mcontext;
334 	struct _aarch64_ctx __user *head;
335 	char __user *base = (char __user *)&sc->__reserved;
336 	size_t offset = 0;
337 	size_t limit = sizeof(sc->__reserved);
338 	bool have_extra_context = false;
339 	char const __user *const sfp = (char const __user *)sf;
340 
341 	user->fpsimd = NULL;
342 	user->sve = NULL;
343 
344 	if (!IS_ALIGNED((unsigned long)base, 16))
345 		goto invalid;
346 
347 	while (1) {
348 		int err = 0;
349 		u32 magic, size;
350 		char const __user *userp;
351 		struct extra_context const __user *extra;
352 		u64 extra_datap;
353 		u32 extra_size;
354 		struct _aarch64_ctx const __user *end;
355 		u32 end_magic, end_size;
356 
357 		if (limit - offset < sizeof(*head))
358 			goto invalid;
359 
360 		if (!IS_ALIGNED(offset, 16))
361 			goto invalid;
362 
363 		head = (struct _aarch64_ctx __user *)(base + offset);
364 		__get_user_error(magic, &head->magic, err);
365 		__get_user_error(size, &head->size, err);
366 		if (err)
367 			return err;
368 
369 		if (limit - offset < size)
370 			goto invalid;
371 
372 		switch (magic) {
373 		case 0:
374 			if (size)
375 				goto invalid;
376 
377 			goto done;
378 
379 		case FPSIMD_MAGIC:
380 			if (!system_supports_fpsimd())
381 				goto invalid;
382 			if (user->fpsimd)
383 				goto invalid;
384 
385 			if (size < sizeof(*user->fpsimd))
386 				goto invalid;
387 
388 			user->fpsimd = (struct fpsimd_context __user *)head;
389 			break;
390 
391 		case ESR_MAGIC:
392 			/* ignore */
393 			break;
394 
395 		case SVE_MAGIC:
396 			if (!system_supports_sve())
397 				goto invalid;
398 
399 			if (user->sve)
400 				goto invalid;
401 
402 			if (size < sizeof(*user->sve))
403 				goto invalid;
404 
405 			user->sve = (struct sve_context __user *)head;
406 			break;
407 
408 		case EXTRA_MAGIC:
409 			if (have_extra_context)
410 				goto invalid;
411 
412 			if (size < sizeof(*extra))
413 				goto invalid;
414 
415 			userp = (char const __user *)head;
416 
417 			extra = (struct extra_context const __user *)userp;
418 			userp += size;
419 
420 			__get_user_error(extra_datap, &extra->datap, err);
421 			__get_user_error(extra_size, &extra->size, err);
422 			if (err)
423 				return err;
424 
425 			/* Check for the dummy terminator in __reserved[]: */
426 
427 			if (limit - offset - size < TERMINATOR_SIZE)
428 				goto invalid;
429 
430 			end = (struct _aarch64_ctx const __user *)userp;
431 			userp += TERMINATOR_SIZE;
432 
433 			__get_user_error(end_magic, &end->magic, err);
434 			__get_user_error(end_size, &end->size, err);
435 			if (err)
436 				return err;
437 
438 			if (end_magic || end_size)
439 				goto invalid;
440 
441 			/* Prevent looping/repeated parsing of extra_context */
442 			have_extra_context = true;
443 
444 			base = (__force void __user *)extra_datap;
445 			if (!IS_ALIGNED((unsigned long)base, 16))
446 				goto invalid;
447 
448 			if (!IS_ALIGNED(extra_size, 16))
449 				goto invalid;
450 
451 			if (base != userp)
452 				goto invalid;
453 
454 			/* Reject "unreasonably large" frames: */
455 			if (extra_size > sfp + SIGFRAME_MAXSZ - userp)
456 				goto invalid;
457 
458 			/*
459 			 * Ignore trailing terminator in __reserved[]
460 			 * and start parsing extra data:
461 			 */
462 			offset = 0;
463 			limit = extra_size;
464 
465 			if (!access_ok(base, limit))
466 				goto invalid;
467 
468 			continue;
469 
470 		default:
471 			goto invalid;
472 		}
473 
474 		if (size < sizeof(*head))
475 			goto invalid;
476 
477 		if (limit - offset < size)
478 			goto invalid;
479 
480 		offset += size;
481 	}
482 
483 done:
484 	return 0;
485 
486 invalid:
487 	return -EINVAL;
488 }
489 
490 static int restore_sigframe(struct pt_regs *regs,
491 			    struct rt_sigframe __user *sf)
492 {
493 	sigset_t set;
494 	int i, err;
495 	struct user_ctxs user;
496 
497 	err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
498 	if (err == 0)
499 		set_current_blocked(&set);
500 
501 	for (i = 0; i < 31; i++)
502 		__get_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i],
503 				 err);
504 	__get_user_error(regs->sp, &sf->uc.uc_mcontext.sp, err);
505 	__get_user_error(regs->pc, &sf->uc.uc_mcontext.pc, err);
506 	__get_user_error(regs->pstate, &sf->uc.uc_mcontext.pstate, err);
507 
508 	/*
509 	 * Avoid sys_rt_sigreturn() restarting.
510 	 */
511 	forget_syscall(regs);
512 
513 	err |= !valid_user_regs(&regs->user_regs, current);
514 	if (err == 0)
515 		err = parse_user_sigframe(&user, sf);
516 
517 	if (err == 0 && system_supports_fpsimd()) {
518 		if (!user.fpsimd)
519 			return -EINVAL;
520 
521 		if (user.sve) {
522 			if (!system_supports_sve())
523 				return -EINVAL;
524 
525 			err = restore_sve_fpsimd_context(&user);
526 		} else {
527 			err = restore_fpsimd_context(user.fpsimd);
528 		}
529 	}
530 
531 	return err;
532 }
533 
534 SYSCALL_DEFINE0(rt_sigreturn)
535 {
536 	struct pt_regs *regs = current_pt_regs();
537 	struct rt_sigframe __user *frame;
538 
539 	/* Always make any pending restarted system calls return -EINTR */
540 	current->restart_block.fn = do_no_restart_syscall;
541 
542 	/*
543 	 * Since we stacked the signal on a 128-bit boundary, then 'sp' should
544 	 * be word aligned here.
545 	 */
546 	if (regs->sp & 15)
547 		goto badframe;
548 
549 	frame = (struct rt_sigframe __user *)regs->sp;
550 
551 	if (!access_ok(frame, sizeof (*frame)))
552 		goto badframe;
553 
554 	if (restore_sigframe(regs, frame))
555 		goto badframe;
556 
557 	if (restore_altstack(&frame->uc.uc_stack))
558 		goto badframe;
559 
560 	return regs->regs[0];
561 
562 badframe:
563 	arm64_notify_segfault(regs->sp);
564 	return 0;
565 }
566 
567 /*
568  * Determine the layout of optional records in the signal frame
569  *
570  * add_all: if true, lays out the biggest possible signal frame for
571  *	this task; otherwise, generates a layout for the current state
572  *	of the task.
573  */
574 static int setup_sigframe_layout(struct rt_sigframe_user_layout *user,
575 				 bool add_all)
576 {
577 	int err;
578 
579 	if (system_supports_fpsimd()) {
580 		err = sigframe_alloc(user, &user->fpsimd_offset,
581 				     sizeof(struct fpsimd_context));
582 		if (err)
583 			return err;
584 	}
585 
586 	/* fault information, if valid */
587 	if (add_all || current->thread.fault_code) {
588 		err = sigframe_alloc(user, &user->esr_offset,
589 				     sizeof(struct esr_context));
590 		if (err)
591 			return err;
592 	}
593 
594 	if (system_supports_sve()) {
595 		unsigned int vq = 0;
596 
597 		if (add_all || test_thread_flag(TIF_SVE)) {
598 			int vl = sve_max_vl();
599 
600 			if (!add_all)
601 				vl = task_get_sve_vl(current);
602 
603 			vq = sve_vq_from_vl(vl);
604 		}
605 
606 		err = sigframe_alloc(user, &user->sve_offset,
607 				     SVE_SIG_CONTEXT_SIZE(vq));
608 		if (err)
609 			return err;
610 	}
611 
612 	return sigframe_alloc_end(user);
613 }
614 
615 static int setup_sigframe(struct rt_sigframe_user_layout *user,
616 			  struct pt_regs *regs, sigset_t *set)
617 {
618 	int i, err = 0;
619 	struct rt_sigframe __user *sf = user->sigframe;
620 
621 	/* set up the stack frame for unwinding */
622 	__put_user_error(regs->regs[29], &user->next_frame->fp, err);
623 	__put_user_error(regs->regs[30], &user->next_frame->lr, err);
624 
625 	for (i = 0; i < 31; i++)
626 		__put_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i],
627 				 err);
628 	__put_user_error(regs->sp, &sf->uc.uc_mcontext.sp, err);
629 	__put_user_error(regs->pc, &sf->uc.uc_mcontext.pc, err);
630 	__put_user_error(regs->pstate, &sf->uc.uc_mcontext.pstate, err);
631 
632 	__put_user_error(current->thread.fault_address, &sf->uc.uc_mcontext.fault_address, err);
633 
634 	err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
635 
636 	if (err == 0 && system_supports_fpsimd()) {
637 		struct fpsimd_context __user *fpsimd_ctx =
638 			apply_user_offset(user, user->fpsimd_offset);
639 		err |= preserve_fpsimd_context(fpsimd_ctx);
640 	}
641 
642 	/* fault information, if valid */
643 	if (err == 0 && user->esr_offset) {
644 		struct esr_context __user *esr_ctx =
645 			apply_user_offset(user, user->esr_offset);
646 
647 		__put_user_error(ESR_MAGIC, &esr_ctx->head.magic, err);
648 		__put_user_error(sizeof(*esr_ctx), &esr_ctx->head.size, err);
649 		__put_user_error(current->thread.fault_code, &esr_ctx->esr, err);
650 	}
651 
652 	/* Scalable Vector Extension state, if present */
653 	if (system_supports_sve() && err == 0 && user->sve_offset) {
654 		struct sve_context __user *sve_ctx =
655 			apply_user_offset(user, user->sve_offset);
656 		err |= preserve_sve_context(sve_ctx);
657 	}
658 
659 	if (err == 0 && user->extra_offset) {
660 		char __user *sfp = (char __user *)user->sigframe;
661 		char __user *userp =
662 			apply_user_offset(user, user->extra_offset);
663 
664 		struct extra_context __user *extra;
665 		struct _aarch64_ctx __user *end;
666 		u64 extra_datap;
667 		u32 extra_size;
668 
669 		extra = (struct extra_context __user *)userp;
670 		userp += EXTRA_CONTEXT_SIZE;
671 
672 		end = (struct _aarch64_ctx __user *)userp;
673 		userp += TERMINATOR_SIZE;
674 
675 		/*
676 		 * extra_datap is just written to the signal frame.
677 		 * The value gets cast back to a void __user *
678 		 * during sigreturn.
679 		 */
680 		extra_datap = (__force u64)userp;
681 		extra_size = sfp + round_up(user->size, 16) - userp;
682 
683 		__put_user_error(EXTRA_MAGIC, &extra->head.magic, err);
684 		__put_user_error(EXTRA_CONTEXT_SIZE, &extra->head.size, err);
685 		__put_user_error(extra_datap, &extra->datap, err);
686 		__put_user_error(extra_size, &extra->size, err);
687 
688 		/* Add the terminator */
689 		__put_user_error(0, &end->magic, err);
690 		__put_user_error(0, &end->size, err);
691 	}
692 
693 	/* set the "end" magic */
694 	if (err == 0) {
695 		struct _aarch64_ctx __user *end =
696 			apply_user_offset(user, user->end_offset);
697 
698 		__put_user_error(0, &end->magic, err);
699 		__put_user_error(0, &end->size, err);
700 	}
701 
702 	return err;
703 }
704 
705 static int get_sigframe(struct rt_sigframe_user_layout *user,
706 			 struct ksignal *ksig, struct pt_regs *regs)
707 {
708 	unsigned long sp, sp_top;
709 	int err;
710 
711 	init_user_layout(user);
712 	err = setup_sigframe_layout(user, false);
713 	if (err)
714 		return err;
715 
716 	sp = sp_top = sigsp(regs->sp, ksig);
717 
718 	sp = round_down(sp - sizeof(struct frame_record), 16);
719 	user->next_frame = (struct frame_record __user *)sp;
720 
721 	sp = round_down(sp, 16) - sigframe_size(user);
722 	user->sigframe = (struct rt_sigframe __user *)sp;
723 
724 	/*
725 	 * Check that we can actually write to the signal frame.
726 	 */
727 	if (!access_ok(user->sigframe, sp_top - sp))
728 		return -EFAULT;
729 
730 	return 0;
731 }
732 
733 static void setup_return(struct pt_regs *regs, struct k_sigaction *ka,
734 			 struct rt_sigframe_user_layout *user, int usig)
735 {
736 	__sigrestore_t sigtramp;
737 
738 	regs->regs[0] = usig;
739 	regs->sp = (unsigned long)user->sigframe;
740 	regs->regs[29] = (unsigned long)&user->next_frame->fp;
741 	regs->pc = (unsigned long)ka->sa.sa_handler;
742 
743 	/*
744 	 * Signal delivery is a (wacky) indirect function call in
745 	 * userspace, so simulate the same setting of BTYPE as a BLR
746 	 * <register containing the signal handler entry point>.
747 	 * Signal delivery to a location in a PROT_BTI guarded page
748 	 * that is not a function entry point will now trigger a
749 	 * SIGILL in userspace.
750 	 *
751 	 * If the signal handler entry point is not in a PROT_BTI
752 	 * guarded page, this is harmless.
753 	 */
754 	if (system_supports_bti()) {
755 		regs->pstate &= ~PSR_BTYPE_MASK;
756 		regs->pstate |= PSR_BTYPE_C;
757 	}
758 
759 	/* TCO (Tag Check Override) always cleared for signal handlers */
760 	regs->pstate &= ~PSR_TCO_BIT;
761 
762 	if (ka->sa.sa_flags & SA_RESTORER)
763 		sigtramp = ka->sa.sa_restorer;
764 	else
765 		sigtramp = VDSO_SYMBOL(current->mm->context.vdso, sigtramp);
766 
767 	regs->regs[30] = (unsigned long)sigtramp;
768 }
769 
770 static int setup_rt_frame(int usig, struct ksignal *ksig, sigset_t *set,
771 			  struct pt_regs *regs)
772 {
773 	struct rt_sigframe_user_layout user;
774 	struct rt_sigframe __user *frame;
775 	int err = 0;
776 
777 	fpsimd_signal_preserve_current_state();
778 
779 	if (get_sigframe(&user, ksig, regs))
780 		return 1;
781 
782 	frame = user.sigframe;
783 
784 	__put_user_error(0, &frame->uc.uc_flags, err);
785 	__put_user_error(NULL, &frame->uc.uc_link, err);
786 
787 	err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
788 	err |= setup_sigframe(&user, regs, set);
789 	if (err == 0) {
790 		setup_return(regs, &ksig->ka, &user, usig);
791 		if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
792 			err |= copy_siginfo_to_user(&frame->info, &ksig->info);
793 			regs->regs[1] = (unsigned long)&frame->info;
794 			regs->regs[2] = (unsigned long)&frame->uc;
795 		}
796 	}
797 
798 	return err;
799 }
800 
801 static void setup_restart_syscall(struct pt_regs *regs)
802 {
803 	if (is_compat_task())
804 		compat_setup_restart_syscall(regs);
805 	else
806 		regs->regs[8] = __NR_restart_syscall;
807 }
808 
809 /*
810  * OK, we're invoking a handler
811  */
812 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
813 {
814 	sigset_t *oldset = sigmask_to_save();
815 	int usig = ksig->sig;
816 	int ret;
817 
818 	rseq_signal_deliver(ksig, regs);
819 
820 	/*
821 	 * Set up the stack frame
822 	 */
823 	if (is_compat_task()) {
824 		if (ksig->ka.sa.sa_flags & SA_SIGINFO)
825 			ret = compat_setup_rt_frame(usig, ksig, oldset, regs);
826 		else
827 			ret = compat_setup_frame(usig, ksig, oldset, regs);
828 	} else {
829 		ret = setup_rt_frame(usig, ksig, oldset, regs);
830 	}
831 
832 	/*
833 	 * Check that the resulting registers are actually sane.
834 	 */
835 	ret |= !valid_user_regs(&regs->user_regs, current);
836 
837 	/* Step into the signal handler if we are stepping */
838 	signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
839 }
840 
841 /*
842  * Note that 'init' is a special process: it doesn't get signals it doesn't
843  * want to handle. Thus you cannot kill init even with a SIGKILL even by
844  * mistake.
845  *
846  * Note that we go through the signals twice: once to check the signals that
847  * the kernel can handle, and then we build all the user-level signal handling
848  * stack-frames in one go after that.
849  */
850 static void do_signal(struct pt_regs *regs)
851 {
852 	unsigned long continue_addr = 0, restart_addr = 0;
853 	int retval = 0;
854 	struct ksignal ksig;
855 	bool syscall = in_syscall(regs);
856 
857 	/*
858 	 * If we were from a system call, check for system call restarting...
859 	 */
860 	if (syscall) {
861 		continue_addr = regs->pc;
862 		restart_addr = continue_addr - (compat_thumb_mode(regs) ? 2 : 4);
863 		retval = regs->regs[0];
864 
865 		/*
866 		 * Avoid additional syscall restarting via ret_to_user.
867 		 */
868 		forget_syscall(regs);
869 
870 		/*
871 		 * Prepare for system call restart. We do this here so that a
872 		 * debugger will see the already changed PC.
873 		 */
874 		switch (retval) {
875 		case -ERESTARTNOHAND:
876 		case -ERESTARTSYS:
877 		case -ERESTARTNOINTR:
878 		case -ERESTART_RESTARTBLOCK:
879 			regs->regs[0] = regs->orig_x0;
880 			regs->pc = restart_addr;
881 			break;
882 		}
883 	}
884 
885 	/*
886 	 * Get the signal to deliver. When running under ptrace, at this point
887 	 * the debugger may change all of our registers.
888 	 */
889 	if (get_signal(&ksig)) {
890 		/*
891 		 * Depending on the signal settings, we may need to revert the
892 		 * decision to restart the system call, but skip this if a
893 		 * debugger has chosen to restart at a different PC.
894 		 */
895 		if (regs->pc == restart_addr &&
896 		    (retval == -ERESTARTNOHAND ||
897 		     retval == -ERESTART_RESTARTBLOCK ||
898 		     (retval == -ERESTARTSYS &&
899 		      !(ksig.ka.sa.sa_flags & SA_RESTART)))) {
900 			syscall_set_return_value(current, regs, -EINTR, 0);
901 			regs->pc = continue_addr;
902 		}
903 
904 		handle_signal(&ksig, regs);
905 		return;
906 	}
907 
908 	/*
909 	 * Handle restarting a different system call. As above, if a debugger
910 	 * has chosen to restart at a different PC, ignore the restart.
911 	 */
912 	if (syscall && regs->pc == restart_addr) {
913 		if (retval == -ERESTART_RESTARTBLOCK)
914 			setup_restart_syscall(regs);
915 		user_rewind_single_step(current);
916 	}
917 
918 	restore_saved_sigmask();
919 }
920 
921 void do_notify_resume(struct pt_regs *regs, unsigned long thread_flags)
922 {
923 	do {
924 		if (thread_flags & _TIF_NEED_RESCHED) {
925 			/* Unmask Debug and SError for the next task */
926 			local_daif_restore(DAIF_PROCCTX_NOIRQ);
927 
928 			schedule();
929 		} else {
930 			local_daif_restore(DAIF_PROCCTX);
931 
932 			if (thread_flags & _TIF_UPROBE)
933 				uprobe_notify_resume(regs);
934 
935 			if (thread_flags & _TIF_MTE_ASYNC_FAULT) {
936 				clear_thread_flag(TIF_MTE_ASYNC_FAULT);
937 				send_sig_fault(SIGSEGV, SEGV_MTEAERR,
938 					       (void __user *)NULL, current);
939 			}
940 
941 			if (thread_flags & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL))
942 				do_signal(regs);
943 
944 			if (thread_flags & _TIF_NOTIFY_RESUME)
945 				resume_user_mode_work(regs);
946 
947 			if (thread_flags & _TIF_FOREIGN_FPSTATE)
948 				fpsimd_restore_current_state();
949 		}
950 
951 		local_daif_mask();
952 		thread_flags = read_thread_flags();
953 	} while (thread_flags & _TIF_WORK_MASK);
954 }
955 
956 unsigned long __ro_after_init signal_minsigstksz;
957 
958 /*
959  * Determine the stack space required for guaranteed signal devliery.
960  * This function is used to populate AT_MINSIGSTKSZ at process startup.
961  * cpufeatures setup is assumed to be complete.
962  */
963 void __init minsigstksz_setup(void)
964 {
965 	struct rt_sigframe_user_layout user;
966 
967 	init_user_layout(&user);
968 
969 	/*
970 	 * If this fails, SIGFRAME_MAXSZ needs to be enlarged.  It won't
971 	 * be big enough, but it's our best guess:
972 	 */
973 	if (WARN_ON(setup_sigframe_layout(&user, true)))
974 		return;
975 
976 	signal_minsigstksz = sigframe_size(&user) +
977 		round_up(sizeof(struct frame_record), 16) +
978 		16; /* max alignment padding */
979 }
980 
981 /*
982  * Compile-time assertions for siginfo_t offsets. Check NSIG* as well, as
983  * changes likely come with new fields that should be added below.
984  */
985 static_assert(NSIGILL	== 11);
986 static_assert(NSIGFPE	== 15);
987 static_assert(NSIGSEGV	== 9);
988 static_assert(NSIGBUS	== 5);
989 static_assert(NSIGTRAP	== 6);
990 static_assert(NSIGCHLD	== 6);
991 static_assert(NSIGSYS	== 2);
992 static_assert(sizeof(siginfo_t) == 128);
993 static_assert(__alignof__(siginfo_t) == 8);
994 static_assert(offsetof(siginfo_t, si_signo)	== 0x00);
995 static_assert(offsetof(siginfo_t, si_errno)	== 0x04);
996 static_assert(offsetof(siginfo_t, si_code)	== 0x08);
997 static_assert(offsetof(siginfo_t, si_pid)	== 0x10);
998 static_assert(offsetof(siginfo_t, si_uid)	== 0x14);
999 static_assert(offsetof(siginfo_t, si_tid)	== 0x10);
1000 static_assert(offsetof(siginfo_t, si_overrun)	== 0x14);
1001 static_assert(offsetof(siginfo_t, si_status)	== 0x18);
1002 static_assert(offsetof(siginfo_t, si_utime)	== 0x20);
1003 static_assert(offsetof(siginfo_t, si_stime)	== 0x28);
1004 static_assert(offsetof(siginfo_t, si_value)	== 0x18);
1005 static_assert(offsetof(siginfo_t, si_int)	== 0x18);
1006 static_assert(offsetof(siginfo_t, si_ptr)	== 0x18);
1007 static_assert(offsetof(siginfo_t, si_addr)	== 0x10);
1008 static_assert(offsetof(siginfo_t, si_addr_lsb)	== 0x18);
1009 static_assert(offsetof(siginfo_t, si_lower)	== 0x20);
1010 static_assert(offsetof(siginfo_t, si_upper)	== 0x28);
1011 static_assert(offsetof(siginfo_t, si_pkey)	== 0x20);
1012 static_assert(offsetof(siginfo_t, si_perf_data)	== 0x18);
1013 static_assert(offsetof(siginfo_t, si_perf_type)	== 0x20);
1014 static_assert(offsetof(siginfo_t, si_band)	== 0x10);
1015 static_assert(offsetof(siginfo_t, si_fd)	== 0x18);
1016 static_assert(offsetof(siginfo_t, si_call_addr)	== 0x10);
1017 static_assert(offsetof(siginfo_t, si_syscall)	== 0x18);
1018 static_assert(offsetof(siginfo_t, si_arch)	== 0x1c);
1019