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