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