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