xref: /linux/arch/arm64/kernel/ptrace.c (revision 778e73d2411abc8f3a2d60dbf038acaec218792e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Based on arch/arm/kernel/ptrace.c
4  *
5  * By Ross Biro 1/23/92
6  * edited by Linus Torvalds
7  * ARM modifications Copyright (C) 2000 Russell King
8  * Copyright (C) 2012 ARM Ltd.
9  */
10 
11 #include <linux/audit.h>
12 #include <linux/compat.h>
13 #include <linux/kernel.h>
14 #include <linux/sched/signal.h>
15 #include <linux/sched/task_stack.h>
16 #include <linux/mm.h>
17 #include <linux/nospec.h>
18 #include <linux/smp.h>
19 #include <linux/ptrace.h>
20 #include <linux/user.h>
21 #include <linux/seccomp.h>
22 #include <linux/security.h>
23 #include <linux/init.h>
24 #include <linux/signal.h>
25 #include <linux/string.h>
26 #include <linux/uaccess.h>
27 #include <linux/perf_event.h>
28 #include <linux/hw_breakpoint.h>
29 #include <linux/regset.h>
30 #include <linux/elf.h>
31 #include <linux/rseq.h>
32 
33 #include <asm/compat.h>
34 #include <asm/cpufeature.h>
35 #include <asm/debug-monitors.h>
36 #include <asm/fpsimd.h>
37 #include <asm/mte.h>
38 #include <asm/pointer_auth.h>
39 #include <asm/stacktrace.h>
40 #include <asm/syscall.h>
41 #include <asm/traps.h>
42 #include <asm/system_misc.h>
43 
44 #define CREATE_TRACE_POINTS
45 #include <trace/events/syscalls.h>
46 
47 struct pt_regs_offset {
48 	const char *name;
49 	int offset;
50 };
51 
52 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
53 #define REG_OFFSET_END {.name = NULL, .offset = 0}
54 #define GPR_OFFSET_NAME(r) \
55 	{.name = "x" #r, .offset = offsetof(struct pt_regs, regs[r])}
56 
57 static const struct pt_regs_offset regoffset_table[] = {
58 	GPR_OFFSET_NAME(0),
59 	GPR_OFFSET_NAME(1),
60 	GPR_OFFSET_NAME(2),
61 	GPR_OFFSET_NAME(3),
62 	GPR_OFFSET_NAME(4),
63 	GPR_OFFSET_NAME(5),
64 	GPR_OFFSET_NAME(6),
65 	GPR_OFFSET_NAME(7),
66 	GPR_OFFSET_NAME(8),
67 	GPR_OFFSET_NAME(9),
68 	GPR_OFFSET_NAME(10),
69 	GPR_OFFSET_NAME(11),
70 	GPR_OFFSET_NAME(12),
71 	GPR_OFFSET_NAME(13),
72 	GPR_OFFSET_NAME(14),
73 	GPR_OFFSET_NAME(15),
74 	GPR_OFFSET_NAME(16),
75 	GPR_OFFSET_NAME(17),
76 	GPR_OFFSET_NAME(18),
77 	GPR_OFFSET_NAME(19),
78 	GPR_OFFSET_NAME(20),
79 	GPR_OFFSET_NAME(21),
80 	GPR_OFFSET_NAME(22),
81 	GPR_OFFSET_NAME(23),
82 	GPR_OFFSET_NAME(24),
83 	GPR_OFFSET_NAME(25),
84 	GPR_OFFSET_NAME(26),
85 	GPR_OFFSET_NAME(27),
86 	GPR_OFFSET_NAME(28),
87 	GPR_OFFSET_NAME(29),
88 	GPR_OFFSET_NAME(30),
89 	{.name = "lr", .offset = offsetof(struct pt_regs, regs[30])},
90 	REG_OFFSET_NAME(sp),
91 	REG_OFFSET_NAME(pc),
92 	REG_OFFSET_NAME(pstate),
93 	REG_OFFSET_END,
94 };
95 
96 /**
97  * regs_query_register_offset() - query register offset from its name
98  * @name:	the name of a register
99  *
100  * regs_query_register_offset() returns the offset of a register in struct
101  * pt_regs from its name. If the name is invalid, this returns -EINVAL;
102  */
103 int regs_query_register_offset(const char *name)
104 {
105 	const struct pt_regs_offset *roff;
106 
107 	for (roff = regoffset_table; roff->name != NULL; roff++)
108 		if (!strcmp(roff->name, name))
109 			return roff->offset;
110 	return -EINVAL;
111 }
112 
113 /**
114  * regs_within_kernel_stack() - check the address in the stack
115  * @regs:      pt_regs which contains kernel stack pointer.
116  * @addr:      address which is checked.
117  *
118  * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
119  * If @addr is within the kernel stack, it returns true. If not, returns false.
120  */
121 static bool regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
122 {
123 	return ((addr & ~(THREAD_SIZE - 1))  ==
124 		(kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1))) ||
125 		on_irq_stack(addr, sizeof(unsigned long));
126 }
127 
128 /**
129  * regs_get_kernel_stack_nth() - get Nth entry of the stack
130  * @regs:	pt_regs which contains kernel stack pointer.
131  * @n:		stack entry number.
132  *
133  * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
134  * is specified by @regs. If the @n th entry is NOT in the kernel stack,
135  * this returns 0.
136  */
137 unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
138 {
139 	unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
140 
141 	addr += n;
142 	if (regs_within_kernel_stack(regs, (unsigned long)addr))
143 		return *addr;
144 	else
145 		return 0;
146 }
147 
148 /*
149  * TODO: does not yet catch signals sent when the child dies.
150  * in exit.c or in signal.c.
151  */
152 
153 /*
154  * Called by kernel/ptrace.c when detaching..
155  */
156 void ptrace_disable(struct task_struct *child)
157 {
158 	/*
159 	 * This would be better off in core code, but PTRACE_DETACH has
160 	 * grown its fair share of arch-specific worts and changing it
161 	 * is likely to cause regressions on obscure architectures.
162 	 */
163 	user_disable_single_step(child);
164 }
165 
166 #ifdef CONFIG_HAVE_HW_BREAKPOINT
167 /*
168  * Handle hitting a HW-breakpoint.
169  */
170 static void ptrace_hbptriggered(struct perf_event *bp,
171 				struct perf_sample_data *data,
172 				struct pt_regs *regs)
173 {
174 	struct arch_hw_breakpoint *bkpt = counter_arch_bp(bp);
175 	const char *desc = "Hardware breakpoint trap (ptrace)";
176 
177 #ifdef CONFIG_COMPAT
178 	if (is_compat_task()) {
179 		int si_errno = 0;
180 		int i;
181 
182 		for (i = 0; i < ARM_MAX_BRP; ++i) {
183 			if (current->thread.debug.hbp_break[i] == bp) {
184 				si_errno = (i << 1) + 1;
185 				break;
186 			}
187 		}
188 
189 		for (i = 0; i < ARM_MAX_WRP; ++i) {
190 			if (current->thread.debug.hbp_watch[i] == bp) {
191 				si_errno = -((i << 1) + 1);
192 				break;
193 			}
194 		}
195 		arm64_force_sig_ptrace_errno_trap(si_errno, bkpt->trigger,
196 						  desc);
197 		return;
198 	}
199 #endif
200 	arm64_force_sig_fault(SIGTRAP, TRAP_HWBKPT, bkpt->trigger, desc);
201 }
202 
203 /*
204  * Unregister breakpoints from this task and reset the pointers in
205  * the thread_struct.
206  */
207 void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
208 {
209 	int i;
210 	struct thread_struct *t = &tsk->thread;
211 
212 	for (i = 0; i < ARM_MAX_BRP; i++) {
213 		if (t->debug.hbp_break[i]) {
214 			unregister_hw_breakpoint(t->debug.hbp_break[i]);
215 			t->debug.hbp_break[i] = NULL;
216 		}
217 	}
218 
219 	for (i = 0; i < ARM_MAX_WRP; i++) {
220 		if (t->debug.hbp_watch[i]) {
221 			unregister_hw_breakpoint(t->debug.hbp_watch[i]);
222 			t->debug.hbp_watch[i] = NULL;
223 		}
224 	}
225 }
226 
227 void ptrace_hw_copy_thread(struct task_struct *tsk)
228 {
229 	memset(&tsk->thread.debug, 0, sizeof(struct debug_info));
230 }
231 
232 static struct perf_event *ptrace_hbp_get_event(unsigned int note_type,
233 					       struct task_struct *tsk,
234 					       unsigned long idx)
235 {
236 	struct perf_event *bp = ERR_PTR(-EINVAL);
237 
238 	switch (note_type) {
239 	case NT_ARM_HW_BREAK:
240 		if (idx >= ARM_MAX_BRP)
241 			goto out;
242 		idx = array_index_nospec(idx, ARM_MAX_BRP);
243 		bp = tsk->thread.debug.hbp_break[idx];
244 		break;
245 	case NT_ARM_HW_WATCH:
246 		if (idx >= ARM_MAX_WRP)
247 			goto out;
248 		idx = array_index_nospec(idx, ARM_MAX_WRP);
249 		bp = tsk->thread.debug.hbp_watch[idx];
250 		break;
251 	}
252 
253 out:
254 	return bp;
255 }
256 
257 static int ptrace_hbp_set_event(unsigned int note_type,
258 				struct task_struct *tsk,
259 				unsigned long idx,
260 				struct perf_event *bp)
261 {
262 	int err = -EINVAL;
263 
264 	switch (note_type) {
265 	case NT_ARM_HW_BREAK:
266 		if (idx >= ARM_MAX_BRP)
267 			goto out;
268 		idx = array_index_nospec(idx, ARM_MAX_BRP);
269 		tsk->thread.debug.hbp_break[idx] = bp;
270 		err = 0;
271 		break;
272 	case NT_ARM_HW_WATCH:
273 		if (idx >= ARM_MAX_WRP)
274 			goto out;
275 		idx = array_index_nospec(idx, ARM_MAX_WRP);
276 		tsk->thread.debug.hbp_watch[idx] = bp;
277 		err = 0;
278 		break;
279 	}
280 
281 out:
282 	return err;
283 }
284 
285 static struct perf_event *ptrace_hbp_create(unsigned int note_type,
286 					    struct task_struct *tsk,
287 					    unsigned long idx)
288 {
289 	struct perf_event *bp;
290 	struct perf_event_attr attr;
291 	int err, type;
292 
293 	switch (note_type) {
294 	case NT_ARM_HW_BREAK:
295 		type = HW_BREAKPOINT_X;
296 		break;
297 	case NT_ARM_HW_WATCH:
298 		type = HW_BREAKPOINT_RW;
299 		break;
300 	default:
301 		return ERR_PTR(-EINVAL);
302 	}
303 
304 	ptrace_breakpoint_init(&attr);
305 
306 	/*
307 	 * Initialise fields to sane defaults
308 	 * (i.e. values that will pass validation).
309 	 */
310 	attr.bp_addr	= 0;
311 	attr.bp_len	= HW_BREAKPOINT_LEN_4;
312 	attr.bp_type	= type;
313 	attr.disabled	= 1;
314 
315 	bp = register_user_hw_breakpoint(&attr, ptrace_hbptriggered, NULL, tsk);
316 	if (IS_ERR(bp))
317 		return bp;
318 
319 	err = ptrace_hbp_set_event(note_type, tsk, idx, bp);
320 	if (err)
321 		return ERR_PTR(err);
322 
323 	return bp;
324 }
325 
326 static int ptrace_hbp_fill_attr_ctrl(unsigned int note_type,
327 				     struct arch_hw_breakpoint_ctrl ctrl,
328 				     struct perf_event_attr *attr)
329 {
330 	int err, len, type, offset, disabled = !ctrl.enabled;
331 
332 	attr->disabled = disabled;
333 	if (disabled)
334 		return 0;
335 
336 	err = arch_bp_generic_fields(ctrl, &len, &type, &offset);
337 	if (err)
338 		return err;
339 
340 	switch (note_type) {
341 	case NT_ARM_HW_BREAK:
342 		if ((type & HW_BREAKPOINT_X) != type)
343 			return -EINVAL;
344 		break;
345 	case NT_ARM_HW_WATCH:
346 		if ((type & HW_BREAKPOINT_RW) != type)
347 			return -EINVAL;
348 		break;
349 	default:
350 		return -EINVAL;
351 	}
352 
353 	attr->bp_len	= len;
354 	attr->bp_type	= type;
355 	attr->bp_addr	+= offset;
356 
357 	return 0;
358 }
359 
360 static int ptrace_hbp_get_resource_info(unsigned int note_type, u32 *info)
361 {
362 	u8 num;
363 	u32 reg = 0;
364 
365 	switch (note_type) {
366 	case NT_ARM_HW_BREAK:
367 		num = hw_breakpoint_slots(TYPE_INST);
368 		break;
369 	case NT_ARM_HW_WATCH:
370 		num = hw_breakpoint_slots(TYPE_DATA);
371 		break;
372 	default:
373 		return -EINVAL;
374 	}
375 
376 	reg |= debug_monitors_arch();
377 	reg <<= 8;
378 	reg |= num;
379 
380 	*info = reg;
381 	return 0;
382 }
383 
384 static int ptrace_hbp_get_ctrl(unsigned int note_type,
385 			       struct task_struct *tsk,
386 			       unsigned long idx,
387 			       u32 *ctrl)
388 {
389 	struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
390 
391 	if (IS_ERR(bp))
392 		return PTR_ERR(bp);
393 
394 	*ctrl = bp ? encode_ctrl_reg(counter_arch_bp(bp)->ctrl) : 0;
395 	return 0;
396 }
397 
398 static int ptrace_hbp_get_addr(unsigned int note_type,
399 			       struct task_struct *tsk,
400 			       unsigned long idx,
401 			       u64 *addr)
402 {
403 	struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
404 
405 	if (IS_ERR(bp))
406 		return PTR_ERR(bp);
407 
408 	*addr = bp ? counter_arch_bp(bp)->address : 0;
409 	return 0;
410 }
411 
412 static struct perf_event *ptrace_hbp_get_initialised_bp(unsigned int note_type,
413 							struct task_struct *tsk,
414 							unsigned long idx)
415 {
416 	struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
417 
418 	if (!bp)
419 		bp = ptrace_hbp_create(note_type, tsk, idx);
420 
421 	return bp;
422 }
423 
424 static int ptrace_hbp_set_ctrl(unsigned int note_type,
425 			       struct task_struct *tsk,
426 			       unsigned long idx,
427 			       u32 uctrl)
428 {
429 	int err;
430 	struct perf_event *bp;
431 	struct perf_event_attr attr;
432 	struct arch_hw_breakpoint_ctrl ctrl;
433 
434 	bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
435 	if (IS_ERR(bp)) {
436 		err = PTR_ERR(bp);
437 		return err;
438 	}
439 
440 	attr = bp->attr;
441 	decode_ctrl_reg(uctrl, &ctrl);
442 	err = ptrace_hbp_fill_attr_ctrl(note_type, ctrl, &attr);
443 	if (err)
444 		return err;
445 
446 	return modify_user_hw_breakpoint(bp, &attr);
447 }
448 
449 static int ptrace_hbp_set_addr(unsigned int note_type,
450 			       struct task_struct *tsk,
451 			       unsigned long idx,
452 			       u64 addr)
453 {
454 	int err;
455 	struct perf_event *bp;
456 	struct perf_event_attr attr;
457 
458 	bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
459 	if (IS_ERR(bp)) {
460 		err = PTR_ERR(bp);
461 		return err;
462 	}
463 
464 	attr = bp->attr;
465 	attr.bp_addr = addr;
466 	err = modify_user_hw_breakpoint(bp, &attr);
467 	return err;
468 }
469 
470 #define PTRACE_HBP_ADDR_SZ	sizeof(u64)
471 #define PTRACE_HBP_CTRL_SZ	sizeof(u32)
472 #define PTRACE_HBP_PAD_SZ	sizeof(u32)
473 
474 static int hw_break_get(struct task_struct *target,
475 			const struct user_regset *regset,
476 			struct membuf to)
477 {
478 	unsigned int note_type = regset->core_note_type;
479 	int ret, idx = 0;
480 	u32 info, ctrl;
481 	u64 addr;
482 
483 	/* Resource info */
484 	ret = ptrace_hbp_get_resource_info(note_type, &info);
485 	if (ret)
486 		return ret;
487 
488 	membuf_write(&to, &info, sizeof(info));
489 	membuf_zero(&to, sizeof(u32));
490 	/* (address, ctrl) registers */
491 	while (to.left) {
492 		ret = ptrace_hbp_get_addr(note_type, target, idx, &addr);
493 		if (ret)
494 			return ret;
495 		ret = ptrace_hbp_get_ctrl(note_type, target, idx, &ctrl);
496 		if (ret)
497 			return ret;
498 		membuf_store(&to, addr);
499 		membuf_store(&to, ctrl);
500 		membuf_zero(&to, sizeof(u32));
501 		idx++;
502 	}
503 	return 0;
504 }
505 
506 static int hw_break_set(struct task_struct *target,
507 			const struct user_regset *regset,
508 			unsigned int pos, unsigned int count,
509 			const void *kbuf, const void __user *ubuf)
510 {
511 	unsigned int note_type = regset->core_note_type;
512 	int ret, idx = 0, offset, limit;
513 	u32 ctrl;
514 	u64 addr;
515 
516 	/* Resource info and pad */
517 	offset = offsetof(struct user_hwdebug_state, dbg_regs);
518 	user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, 0, offset);
519 
520 	/* (address, ctrl) registers */
521 	limit = regset->n * regset->size;
522 	while (count && offset < limit) {
523 		if (count < PTRACE_HBP_ADDR_SZ)
524 			return -EINVAL;
525 		ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &addr,
526 					 offset, offset + PTRACE_HBP_ADDR_SZ);
527 		if (ret)
528 			return ret;
529 		ret = ptrace_hbp_set_addr(note_type, target, idx, addr);
530 		if (ret)
531 			return ret;
532 		offset += PTRACE_HBP_ADDR_SZ;
533 
534 		if (!count)
535 			break;
536 		ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ctrl,
537 					 offset, offset + PTRACE_HBP_CTRL_SZ);
538 		if (ret)
539 			return ret;
540 		ret = ptrace_hbp_set_ctrl(note_type, target, idx, ctrl);
541 		if (ret)
542 			return ret;
543 		offset += PTRACE_HBP_CTRL_SZ;
544 
545 		user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
546 					  offset, offset + PTRACE_HBP_PAD_SZ);
547 		offset += PTRACE_HBP_PAD_SZ;
548 		idx++;
549 	}
550 
551 	return 0;
552 }
553 #endif	/* CONFIG_HAVE_HW_BREAKPOINT */
554 
555 static int gpr_get(struct task_struct *target,
556 		   const struct user_regset *regset,
557 		   struct membuf to)
558 {
559 	struct user_pt_regs *uregs = &task_pt_regs(target)->user_regs;
560 	return membuf_write(&to, uregs, sizeof(*uregs));
561 }
562 
563 static int gpr_set(struct task_struct *target, const struct user_regset *regset,
564 		   unsigned int pos, unsigned int count,
565 		   const void *kbuf, const void __user *ubuf)
566 {
567 	int ret;
568 	struct user_pt_regs newregs = task_pt_regs(target)->user_regs;
569 
570 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newregs, 0, -1);
571 	if (ret)
572 		return ret;
573 
574 	if (!valid_user_regs(&newregs, target))
575 		return -EINVAL;
576 
577 	task_pt_regs(target)->user_regs = newregs;
578 	return 0;
579 }
580 
581 static int fpr_active(struct task_struct *target, const struct user_regset *regset)
582 {
583 	if (!system_supports_fpsimd())
584 		return -ENODEV;
585 	return regset->n;
586 }
587 
588 /*
589  * TODO: update fp accessors for lazy context switching (sync/flush hwstate)
590  */
591 static int __fpr_get(struct task_struct *target,
592 		     const struct user_regset *regset,
593 		     struct membuf to)
594 {
595 	struct user_fpsimd_state *uregs;
596 
597 	sve_sync_to_fpsimd(target);
598 
599 	uregs = &target->thread.uw.fpsimd_state;
600 
601 	return membuf_write(&to, uregs, sizeof(*uregs));
602 }
603 
604 static int fpr_get(struct task_struct *target, const struct user_regset *regset,
605 		   struct membuf to)
606 {
607 	if (!system_supports_fpsimd())
608 		return -EINVAL;
609 
610 	if (target == current)
611 		fpsimd_preserve_current_state();
612 
613 	return __fpr_get(target, regset, to);
614 }
615 
616 static int __fpr_set(struct task_struct *target,
617 		     const struct user_regset *regset,
618 		     unsigned int pos, unsigned int count,
619 		     const void *kbuf, const void __user *ubuf,
620 		     unsigned int start_pos)
621 {
622 	int ret;
623 	struct user_fpsimd_state newstate;
624 
625 	/*
626 	 * Ensure target->thread.uw.fpsimd_state is up to date, so that a
627 	 * short copyin can't resurrect stale data.
628 	 */
629 	sve_sync_to_fpsimd(target);
630 
631 	newstate = target->thread.uw.fpsimd_state;
632 
633 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newstate,
634 				 start_pos, start_pos + sizeof(newstate));
635 	if (ret)
636 		return ret;
637 
638 	target->thread.uw.fpsimd_state = newstate;
639 
640 	return ret;
641 }
642 
643 static int fpr_set(struct task_struct *target, const struct user_regset *regset,
644 		   unsigned int pos, unsigned int count,
645 		   const void *kbuf, const void __user *ubuf)
646 {
647 	int ret;
648 
649 	if (!system_supports_fpsimd())
650 		return -EINVAL;
651 
652 	ret = __fpr_set(target, regset, pos, count, kbuf, ubuf, 0);
653 	if (ret)
654 		return ret;
655 
656 	sve_sync_from_fpsimd_zeropad(target);
657 	fpsimd_flush_task_state(target);
658 
659 	return ret;
660 }
661 
662 static int tls_get(struct task_struct *target, const struct user_regset *regset,
663 		   struct membuf to)
664 {
665 	int ret;
666 
667 	if (target == current)
668 		tls_preserve_current_state();
669 
670 	ret = membuf_store(&to, target->thread.uw.tp_value);
671 	if (system_supports_tpidr2())
672 		ret = membuf_store(&to, target->thread.tpidr2_el0);
673 	else
674 		ret = membuf_zero(&to, sizeof(u64));
675 
676 	return ret;
677 }
678 
679 static int tls_set(struct task_struct *target, const struct user_regset *regset,
680 		   unsigned int pos, unsigned int count,
681 		   const void *kbuf, const void __user *ubuf)
682 {
683 	int ret;
684 	unsigned long tls[2];
685 
686 	tls[0] = target->thread.uw.tp_value;
687 	if (system_supports_tpidr2())
688 		tls[1] = target->thread.tpidr2_el0;
689 
690 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, tls, 0, count);
691 	if (ret)
692 		return ret;
693 
694 	target->thread.uw.tp_value = tls[0];
695 	if (system_supports_tpidr2())
696 		target->thread.tpidr2_el0 = tls[1];
697 
698 	return ret;
699 }
700 
701 static int system_call_get(struct task_struct *target,
702 			   const struct user_regset *regset,
703 			   struct membuf to)
704 {
705 	return membuf_store(&to, task_pt_regs(target)->syscallno);
706 }
707 
708 static int system_call_set(struct task_struct *target,
709 			   const struct user_regset *regset,
710 			   unsigned int pos, unsigned int count,
711 			   const void *kbuf, const void __user *ubuf)
712 {
713 	int syscallno = task_pt_regs(target)->syscallno;
714 	int ret;
715 
716 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &syscallno, 0, -1);
717 	if (ret)
718 		return ret;
719 
720 	task_pt_regs(target)->syscallno = syscallno;
721 	return ret;
722 }
723 
724 #ifdef CONFIG_ARM64_SVE
725 
726 static void sve_init_header_from_task(struct user_sve_header *header,
727 				      struct task_struct *target,
728 				      enum vec_type type)
729 {
730 	unsigned int vq;
731 	bool active;
732 	bool fpsimd_only;
733 	enum vec_type task_type;
734 
735 	memset(header, 0, sizeof(*header));
736 
737 	/* Check if the requested registers are active for the task */
738 	if (thread_sm_enabled(&target->thread))
739 		task_type = ARM64_VEC_SME;
740 	else
741 		task_type = ARM64_VEC_SVE;
742 	active = (task_type == type);
743 
744 	switch (type) {
745 	case ARM64_VEC_SVE:
746 		if (test_tsk_thread_flag(target, TIF_SVE_VL_INHERIT))
747 			header->flags |= SVE_PT_VL_INHERIT;
748 		fpsimd_only = !test_tsk_thread_flag(target, TIF_SVE);
749 		break;
750 	case ARM64_VEC_SME:
751 		if (test_tsk_thread_flag(target, TIF_SME_VL_INHERIT))
752 			header->flags |= SVE_PT_VL_INHERIT;
753 		fpsimd_only = false;
754 		break;
755 	default:
756 		WARN_ON_ONCE(1);
757 		return;
758 	}
759 
760 	if (active) {
761 		if (fpsimd_only) {
762 			header->flags |= SVE_PT_REGS_FPSIMD;
763 		} else {
764 			header->flags |= SVE_PT_REGS_SVE;
765 		}
766 	}
767 
768 	header->vl = task_get_vl(target, type);
769 	vq = sve_vq_from_vl(header->vl);
770 
771 	header->max_vl = vec_max_vl(type);
772 	header->size = SVE_PT_SIZE(vq, header->flags);
773 	header->max_size = SVE_PT_SIZE(sve_vq_from_vl(header->max_vl),
774 				      SVE_PT_REGS_SVE);
775 }
776 
777 static unsigned int sve_size_from_header(struct user_sve_header const *header)
778 {
779 	return ALIGN(header->size, SVE_VQ_BYTES);
780 }
781 
782 static int sve_get_common(struct task_struct *target,
783 			  const struct user_regset *regset,
784 			  struct membuf to,
785 			  enum vec_type type)
786 {
787 	struct user_sve_header header;
788 	unsigned int vq;
789 	unsigned long start, end;
790 
791 	/* Header */
792 	sve_init_header_from_task(&header, target, type);
793 	vq = sve_vq_from_vl(header.vl);
794 
795 	membuf_write(&to, &header, sizeof(header));
796 
797 	if (target == current)
798 		fpsimd_preserve_current_state();
799 
800 	BUILD_BUG_ON(SVE_PT_FPSIMD_OFFSET != sizeof(header));
801 	BUILD_BUG_ON(SVE_PT_SVE_OFFSET != sizeof(header));
802 
803 	switch ((header.flags & SVE_PT_REGS_MASK)) {
804 	case SVE_PT_REGS_FPSIMD:
805 		return __fpr_get(target, regset, to);
806 
807 	case SVE_PT_REGS_SVE:
808 		start = SVE_PT_SVE_OFFSET;
809 		end = SVE_PT_SVE_FFR_OFFSET(vq) + SVE_PT_SVE_FFR_SIZE(vq);
810 		membuf_write(&to, target->thread.sve_state, end - start);
811 
812 		start = end;
813 		end = SVE_PT_SVE_FPSR_OFFSET(vq);
814 		membuf_zero(&to, end - start);
815 
816 		/*
817 		 * Copy fpsr, and fpcr which must follow contiguously in
818 		 * struct fpsimd_state:
819 		 */
820 		start = end;
821 		end = SVE_PT_SVE_FPCR_OFFSET(vq) + SVE_PT_SVE_FPCR_SIZE;
822 		membuf_write(&to, &target->thread.uw.fpsimd_state.fpsr,
823 			     end - start);
824 
825 		start = end;
826 		end = sve_size_from_header(&header);
827 		return membuf_zero(&to, end - start);
828 
829 	default:
830 		return 0;
831 	}
832 }
833 
834 static int sve_get(struct task_struct *target,
835 		   const struct user_regset *regset,
836 		   struct membuf to)
837 {
838 	if (!system_supports_sve())
839 		return -EINVAL;
840 
841 	return sve_get_common(target, regset, to, ARM64_VEC_SVE);
842 }
843 
844 static int sve_set_common(struct task_struct *target,
845 			  const struct user_regset *regset,
846 			  unsigned int pos, unsigned int count,
847 			  const void *kbuf, const void __user *ubuf,
848 			  enum vec_type type)
849 {
850 	int ret;
851 	struct user_sve_header header;
852 	unsigned int vq;
853 	unsigned long start, end;
854 
855 	/* Header */
856 	if (count < sizeof(header))
857 		return -EINVAL;
858 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &header,
859 				 0, sizeof(header));
860 	if (ret)
861 		goto out;
862 
863 	/*
864 	 * Apart from SVE_PT_REGS_MASK, all SVE_PT_* flags are consumed by
865 	 * vec_set_vector_length(), which will also validate them for us:
866 	 */
867 	ret = vec_set_vector_length(target, type, header.vl,
868 		((unsigned long)header.flags & ~SVE_PT_REGS_MASK) << 16);
869 	if (ret)
870 		goto out;
871 
872 	/* Actual VL set may be less than the user asked for: */
873 	vq = sve_vq_from_vl(task_get_vl(target, type));
874 
875 	/* Enter/exit streaming mode */
876 	if (system_supports_sme()) {
877 		u64 old_svcr = target->thread.svcr;
878 
879 		switch (type) {
880 		case ARM64_VEC_SVE:
881 			target->thread.svcr &= ~SVCR_SM_MASK;
882 			break;
883 		case ARM64_VEC_SME:
884 			target->thread.svcr |= SVCR_SM_MASK;
885 
886 			/*
887 			 * Disable traps and ensure there is SME storage but
888 			 * preserve any currently set values in ZA/ZT.
889 			 */
890 			sme_alloc(target, false);
891 			set_tsk_thread_flag(target, TIF_SME);
892 			break;
893 		default:
894 			WARN_ON_ONCE(1);
895 			ret = -EINVAL;
896 			goto out;
897 		}
898 
899 		/*
900 		 * If we switched then invalidate any existing SVE
901 		 * state and ensure there's storage.
902 		 */
903 		if (target->thread.svcr != old_svcr)
904 			sve_alloc(target, true);
905 	}
906 
907 	/* Registers: FPSIMD-only case */
908 
909 	BUILD_BUG_ON(SVE_PT_FPSIMD_OFFSET != sizeof(header));
910 	if ((header.flags & SVE_PT_REGS_MASK) == SVE_PT_REGS_FPSIMD) {
911 		ret = __fpr_set(target, regset, pos, count, kbuf, ubuf,
912 				SVE_PT_FPSIMD_OFFSET);
913 		clear_tsk_thread_flag(target, TIF_SVE);
914 		target->thread.fp_type = FP_STATE_FPSIMD;
915 		goto out;
916 	}
917 
918 	/*
919 	 * Otherwise: no registers or full SVE case.  For backwards
920 	 * compatibility reasons we treat empty flags as SVE registers.
921 	 */
922 
923 	/*
924 	 * If setting a different VL from the requested VL and there is
925 	 * register data, the data layout will be wrong: don't even
926 	 * try to set the registers in this case.
927 	 */
928 	if (count && vq != sve_vq_from_vl(header.vl)) {
929 		ret = -EIO;
930 		goto out;
931 	}
932 
933 	sve_alloc(target, true);
934 	if (!target->thread.sve_state) {
935 		ret = -ENOMEM;
936 		clear_tsk_thread_flag(target, TIF_SVE);
937 		target->thread.fp_type = FP_STATE_FPSIMD;
938 		goto out;
939 	}
940 
941 	/*
942 	 * Ensure target->thread.sve_state is up to date with target's
943 	 * FPSIMD regs, so that a short copyin leaves trailing
944 	 * registers unmodified.  Only enable SVE if we are
945 	 * configuring normal SVE, a system with streaming SVE may not
946 	 * have normal SVE.
947 	 */
948 	fpsimd_sync_to_sve(target);
949 	if (type == ARM64_VEC_SVE)
950 		set_tsk_thread_flag(target, TIF_SVE);
951 	target->thread.fp_type = FP_STATE_SVE;
952 
953 	BUILD_BUG_ON(SVE_PT_SVE_OFFSET != sizeof(header));
954 	start = SVE_PT_SVE_OFFSET;
955 	end = SVE_PT_SVE_FFR_OFFSET(vq) + SVE_PT_SVE_FFR_SIZE(vq);
956 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
957 				 target->thread.sve_state,
958 				 start, end);
959 	if (ret)
960 		goto out;
961 
962 	start = end;
963 	end = SVE_PT_SVE_FPSR_OFFSET(vq);
964 	user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, start, end);
965 
966 	/*
967 	 * Copy fpsr, and fpcr which must follow contiguously in
968 	 * struct fpsimd_state:
969 	 */
970 	start = end;
971 	end = SVE_PT_SVE_FPCR_OFFSET(vq) + SVE_PT_SVE_FPCR_SIZE;
972 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
973 				 &target->thread.uw.fpsimd_state.fpsr,
974 				 start, end);
975 
976 out:
977 	fpsimd_flush_task_state(target);
978 	return ret;
979 }
980 
981 static int sve_set(struct task_struct *target,
982 		   const struct user_regset *regset,
983 		   unsigned int pos, unsigned int count,
984 		   const void *kbuf, const void __user *ubuf)
985 {
986 	if (!system_supports_sve())
987 		return -EINVAL;
988 
989 	return sve_set_common(target, regset, pos, count, kbuf, ubuf,
990 			      ARM64_VEC_SVE);
991 }
992 
993 #endif /* CONFIG_ARM64_SVE */
994 
995 #ifdef CONFIG_ARM64_SME
996 
997 static int ssve_get(struct task_struct *target,
998 		   const struct user_regset *regset,
999 		   struct membuf to)
1000 {
1001 	if (!system_supports_sme())
1002 		return -EINVAL;
1003 
1004 	return sve_get_common(target, regset, to, ARM64_VEC_SME);
1005 }
1006 
1007 static int ssve_set(struct task_struct *target,
1008 		    const struct user_regset *regset,
1009 		    unsigned int pos, unsigned int count,
1010 		    const void *kbuf, const void __user *ubuf)
1011 {
1012 	if (!system_supports_sme())
1013 		return -EINVAL;
1014 
1015 	return sve_set_common(target, regset, pos, count, kbuf, ubuf,
1016 			      ARM64_VEC_SME);
1017 }
1018 
1019 static int za_get(struct task_struct *target,
1020 		  const struct user_regset *regset,
1021 		  struct membuf to)
1022 {
1023 	struct user_za_header header;
1024 	unsigned int vq;
1025 	unsigned long start, end;
1026 
1027 	if (!system_supports_sme())
1028 		return -EINVAL;
1029 
1030 	/* Header */
1031 	memset(&header, 0, sizeof(header));
1032 
1033 	if (test_tsk_thread_flag(target, TIF_SME_VL_INHERIT))
1034 		header.flags |= ZA_PT_VL_INHERIT;
1035 
1036 	header.vl = task_get_sme_vl(target);
1037 	vq = sve_vq_from_vl(header.vl);
1038 	header.max_vl = sme_max_vl();
1039 	header.max_size = ZA_PT_SIZE(vq);
1040 
1041 	/* If ZA is not active there is only the header */
1042 	if (thread_za_enabled(&target->thread))
1043 		header.size = ZA_PT_SIZE(vq);
1044 	else
1045 		header.size = ZA_PT_ZA_OFFSET;
1046 
1047 	membuf_write(&to, &header, sizeof(header));
1048 
1049 	BUILD_BUG_ON(ZA_PT_ZA_OFFSET != sizeof(header));
1050 	end = ZA_PT_ZA_OFFSET;
1051 
1052 	if (target == current)
1053 		fpsimd_preserve_current_state();
1054 
1055 	/* Any register data to include? */
1056 	if (thread_za_enabled(&target->thread)) {
1057 		start = end;
1058 		end = ZA_PT_SIZE(vq);
1059 		membuf_write(&to, target->thread.sme_state, end - start);
1060 	}
1061 
1062 	/* Zero any trailing padding */
1063 	start = end;
1064 	end = ALIGN(header.size, SVE_VQ_BYTES);
1065 	return membuf_zero(&to, end - start);
1066 }
1067 
1068 static int za_set(struct task_struct *target,
1069 		  const struct user_regset *regset,
1070 		  unsigned int pos, unsigned int count,
1071 		  const void *kbuf, const void __user *ubuf)
1072 {
1073 	int ret;
1074 	struct user_za_header header;
1075 	unsigned int vq;
1076 	unsigned long start, end;
1077 
1078 	if (!system_supports_sme())
1079 		return -EINVAL;
1080 
1081 	/* Header */
1082 	if (count < sizeof(header))
1083 		return -EINVAL;
1084 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &header,
1085 				 0, sizeof(header));
1086 	if (ret)
1087 		goto out;
1088 
1089 	/*
1090 	 * All current ZA_PT_* flags are consumed by
1091 	 * vec_set_vector_length(), which will also validate them for
1092 	 * us:
1093 	 */
1094 	ret = vec_set_vector_length(target, ARM64_VEC_SME, header.vl,
1095 		((unsigned long)header.flags) << 16);
1096 	if (ret)
1097 		goto out;
1098 
1099 	/* Actual VL set may be less than the user asked for: */
1100 	vq = sve_vq_from_vl(task_get_sme_vl(target));
1101 
1102 	/* Ensure there is some SVE storage for streaming mode */
1103 	if (!target->thread.sve_state) {
1104 		sve_alloc(target, false);
1105 		if (!target->thread.sve_state) {
1106 			ret = -ENOMEM;
1107 			goto out;
1108 		}
1109 	}
1110 
1111 	/* Allocate/reinit ZA storage */
1112 	sme_alloc(target, true);
1113 	if (!target->thread.sme_state) {
1114 		ret = -ENOMEM;
1115 		goto out;
1116 	}
1117 
1118 	/* If there is no data then disable ZA */
1119 	if (!count) {
1120 		target->thread.svcr &= ~SVCR_ZA_MASK;
1121 		goto out;
1122 	}
1123 
1124 	/*
1125 	 * If setting a different VL from the requested VL and there is
1126 	 * register data, the data layout will be wrong: don't even
1127 	 * try to set the registers in this case.
1128 	 */
1129 	if (vq != sve_vq_from_vl(header.vl)) {
1130 		ret = -EIO;
1131 		goto out;
1132 	}
1133 
1134 	BUILD_BUG_ON(ZA_PT_ZA_OFFSET != sizeof(header));
1135 	start = ZA_PT_ZA_OFFSET;
1136 	end = ZA_PT_SIZE(vq);
1137 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
1138 				 target->thread.sme_state,
1139 				 start, end);
1140 	if (ret)
1141 		goto out;
1142 
1143 	/* Mark ZA as active and let userspace use it */
1144 	set_tsk_thread_flag(target, TIF_SME);
1145 	target->thread.svcr |= SVCR_ZA_MASK;
1146 
1147 out:
1148 	fpsimd_flush_task_state(target);
1149 	return ret;
1150 }
1151 
1152 static int zt_get(struct task_struct *target,
1153 		  const struct user_regset *regset,
1154 		  struct membuf to)
1155 {
1156 	if (!system_supports_sme2())
1157 		return -EINVAL;
1158 
1159 	/*
1160 	 * If PSTATE.ZA is not set then ZT will be zeroed when it is
1161 	 * enabled so report the current register value as zero.
1162 	 */
1163 	if (thread_za_enabled(&target->thread))
1164 		membuf_write(&to, thread_zt_state(&target->thread),
1165 			     ZT_SIG_REG_BYTES);
1166 	else
1167 		membuf_zero(&to, ZT_SIG_REG_BYTES);
1168 
1169 	return 0;
1170 }
1171 
1172 static int zt_set(struct task_struct *target,
1173 		  const struct user_regset *regset,
1174 		  unsigned int pos, unsigned int count,
1175 		  const void *kbuf, const void __user *ubuf)
1176 {
1177 	int ret;
1178 
1179 	if (!system_supports_sme2())
1180 		return -EINVAL;
1181 
1182 	/* Ensure SVE storage in case this is first use of SME */
1183 	sve_alloc(target, false);
1184 	if (!target->thread.sve_state)
1185 		return -ENOMEM;
1186 
1187 	if (!thread_za_enabled(&target->thread)) {
1188 		sme_alloc(target, true);
1189 		if (!target->thread.sme_state)
1190 			return -ENOMEM;
1191 	}
1192 
1193 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
1194 				 thread_zt_state(&target->thread),
1195 				 0, ZT_SIG_REG_BYTES);
1196 	if (ret == 0) {
1197 		target->thread.svcr |= SVCR_ZA_MASK;
1198 		set_tsk_thread_flag(target, TIF_SME);
1199 	}
1200 
1201 	fpsimd_flush_task_state(target);
1202 
1203 	return ret;
1204 }
1205 
1206 #endif /* CONFIG_ARM64_SME */
1207 
1208 #ifdef CONFIG_ARM64_PTR_AUTH
1209 static int pac_mask_get(struct task_struct *target,
1210 			const struct user_regset *regset,
1211 			struct membuf to)
1212 {
1213 	/*
1214 	 * The PAC bits can differ across data and instruction pointers
1215 	 * depending on TCR_EL1.TBID*, which we may make use of in future, so
1216 	 * we expose separate masks.
1217 	 */
1218 	unsigned long mask = ptrauth_user_pac_mask();
1219 	struct user_pac_mask uregs = {
1220 		.data_mask = mask,
1221 		.insn_mask = mask,
1222 	};
1223 
1224 	if (!system_supports_address_auth())
1225 		return -EINVAL;
1226 
1227 	return membuf_write(&to, &uregs, sizeof(uregs));
1228 }
1229 
1230 static int pac_enabled_keys_get(struct task_struct *target,
1231 				const struct user_regset *regset,
1232 				struct membuf to)
1233 {
1234 	long enabled_keys = ptrauth_get_enabled_keys(target);
1235 
1236 	if (IS_ERR_VALUE(enabled_keys))
1237 		return enabled_keys;
1238 
1239 	return membuf_write(&to, &enabled_keys, sizeof(enabled_keys));
1240 }
1241 
1242 static int pac_enabled_keys_set(struct task_struct *target,
1243 				const struct user_regset *regset,
1244 				unsigned int pos, unsigned int count,
1245 				const void *kbuf, const void __user *ubuf)
1246 {
1247 	int ret;
1248 	long enabled_keys = ptrauth_get_enabled_keys(target);
1249 
1250 	if (IS_ERR_VALUE(enabled_keys))
1251 		return enabled_keys;
1252 
1253 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &enabled_keys, 0,
1254 				 sizeof(long));
1255 	if (ret)
1256 		return ret;
1257 
1258 	return ptrauth_set_enabled_keys(target, PR_PAC_ENABLED_KEYS_MASK,
1259 					enabled_keys);
1260 }
1261 
1262 #ifdef CONFIG_CHECKPOINT_RESTORE
1263 static __uint128_t pac_key_to_user(const struct ptrauth_key *key)
1264 {
1265 	return (__uint128_t)key->hi << 64 | key->lo;
1266 }
1267 
1268 static struct ptrauth_key pac_key_from_user(__uint128_t ukey)
1269 {
1270 	struct ptrauth_key key = {
1271 		.lo = (unsigned long)ukey,
1272 		.hi = (unsigned long)(ukey >> 64),
1273 	};
1274 
1275 	return key;
1276 }
1277 
1278 static void pac_address_keys_to_user(struct user_pac_address_keys *ukeys,
1279 				     const struct ptrauth_keys_user *keys)
1280 {
1281 	ukeys->apiakey = pac_key_to_user(&keys->apia);
1282 	ukeys->apibkey = pac_key_to_user(&keys->apib);
1283 	ukeys->apdakey = pac_key_to_user(&keys->apda);
1284 	ukeys->apdbkey = pac_key_to_user(&keys->apdb);
1285 }
1286 
1287 static void pac_address_keys_from_user(struct ptrauth_keys_user *keys,
1288 				       const struct user_pac_address_keys *ukeys)
1289 {
1290 	keys->apia = pac_key_from_user(ukeys->apiakey);
1291 	keys->apib = pac_key_from_user(ukeys->apibkey);
1292 	keys->apda = pac_key_from_user(ukeys->apdakey);
1293 	keys->apdb = pac_key_from_user(ukeys->apdbkey);
1294 }
1295 
1296 static int pac_address_keys_get(struct task_struct *target,
1297 				const struct user_regset *regset,
1298 				struct membuf to)
1299 {
1300 	struct ptrauth_keys_user *keys = &target->thread.keys_user;
1301 	struct user_pac_address_keys user_keys;
1302 
1303 	if (!system_supports_address_auth())
1304 		return -EINVAL;
1305 
1306 	pac_address_keys_to_user(&user_keys, keys);
1307 
1308 	return membuf_write(&to, &user_keys, sizeof(user_keys));
1309 }
1310 
1311 static int pac_address_keys_set(struct task_struct *target,
1312 				const struct user_regset *regset,
1313 				unsigned int pos, unsigned int count,
1314 				const void *kbuf, const void __user *ubuf)
1315 {
1316 	struct ptrauth_keys_user *keys = &target->thread.keys_user;
1317 	struct user_pac_address_keys user_keys;
1318 	int ret;
1319 
1320 	if (!system_supports_address_auth())
1321 		return -EINVAL;
1322 
1323 	pac_address_keys_to_user(&user_keys, keys);
1324 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
1325 				 &user_keys, 0, -1);
1326 	if (ret)
1327 		return ret;
1328 	pac_address_keys_from_user(keys, &user_keys);
1329 
1330 	return 0;
1331 }
1332 
1333 static void pac_generic_keys_to_user(struct user_pac_generic_keys *ukeys,
1334 				     const struct ptrauth_keys_user *keys)
1335 {
1336 	ukeys->apgakey = pac_key_to_user(&keys->apga);
1337 }
1338 
1339 static void pac_generic_keys_from_user(struct ptrauth_keys_user *keys,
1340 				       const struct user_pac_generic_keys *ukeys)
1341 {
1342 	keys->apga = pac_key_from_user(ukeys->apgakey);
1343 }
1344 
1345 static int pac_generic_keys_get(struct task_struct *target,
1346 				const struct user_regset *regset,
1347 				struct membuf to)
1348 {
1349 	struct ptrauth_keys_user *keys = &target->thread.keys_user;
1350 	struct user_pac_generic_keys user_keys;
1351 
1352 	if (!system_supports_generic_auth())
1353 		return -EINVAL;
1354 
1355 	pac_generic_keys_to_user(&user_keys, keys);
1356 
1357 	return membuf_write(&to, &user_keys, sizeof(user_keys));
1358 }
1359 
1360 static int pac_generic_keys_set(struct task_struct *target,
1361 				const struct user_regset *regset,
1362 				unsigned int pos, unsigned int count,
1363 				const void *kbuf, const void __user *ubuf)
1364 {
1365 	struct ptrauth_keys_user *keys = &target->thread.keys_user;
1366 	struct user_pac_generic_keys user_keys;
1367 	int ret;
1368 
1369 	if (!system_supports_generic_auth())
1370 		return -EINVAL;
1371 
1372 	pac_generic_keys_to_user(&user_keys, keys);
1373 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
1374 				 &user_keys, 0, -1);
1375 	if (ret)
1376 		return ret;
1377 	pac_generic_keys_from_user(keys, &user_keys);
1378 
1379 	return 0;
1380 }
1381 #endif /* CONFIG_CHECKPOINT_RESTORE */
1382 #endif /* CONFIG_ARM64_PTR_AUTH */
1383 
1384 #ifdef CONFIG_ARM64_TAGGED_ADDR_ABI
1385 static int tagged_addr_ctrl_get(struct task_struct *target,
1386 				const struct user_regset *regset,
1387 				struct membuf to)
1388 {
1389 	long ctrl = get_tagged_addr_ctrl(target);
1390 
1391 	if (IS_ERR_VALUE(ctrl))
1392 		return ctrl;
1393 
1394 	return membuf_write(&to, &ctrl, sizeof(ctrl));
1395 }
1396 
1397 static int tagged_addr_ctrl_set(struct task_struct *target, const struct
1398 				user_regset *regset, unsigned int pos,
1399 				unsigned int count, const void *kbuf, const
1400 				void __user *ubuf)
1401 {
1402 	int ret;
1403 	long ctrl;
1404 
1405 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ctrl, 0, -1);
1406 	if (ret)
1407 		return ret;
1408 
1409 	return set_tagged_addr_ctrl(target, ctrl);
1410 }
1411 #endif
1412 
1413 enum aarch64_regset {
1414 	REGSET_GPR,
1415 	REGSET_FPR,
1416 	REGSET_TLS,
1417 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1418 	REGSET_HW_BREAK,
1419 	REGSET_HW_WATCH,
1420 #endif
1421 	REGSET_SYSTEM_CALL,
1422 #ifdef CONFIG_ARM64_SVE
1423 	REGSET_SVE,
1424 #endif
1425 #ifdef CONFIG_ARM64_SME
1426 	REGSET_SSVE,
1427 	REGSET_ZA,
1428 	REGSET_ZT,
1429 #endif
1430 #ifdef CONFIG_ARM64_PTR_AUTH
1431 	REGSET_PAC_MASK,
1432 	REGSET_PAC_ENABLED_KEYS,
1433 #ifdef CONFIG_CHECKPOINT_RESTORE
1434 	REGSET_PACA_KEYS,
1435 	REGSET_PACG_KEYS,
1436 #endif
1437 #endif
1438 #ifdef CONFIG_ARM64_TAGGED_ADDR_ABI
1439 	REGSET_TAGGED_ADDR_CTRL,
1440 #endif
1441 };
1442 
1443 static const struct user_regset aarch64_regsets[] = {
1444 	[REGSET_GPR] = {
1445 		.core_note_type = NT_PRSTATUS,
1446 		.n = sizeof(struct user_pt_regs) / sizeof(u64),
1447 		.size = sizeof(u64),
1448 		.align = sizeof(u64),
1449 		.regset_get = gpr_get,
1450 		.set = gpr_set
1451 	},
1452 	[REGSET_FPR] = {
1453 		.core_note_type = NT_PRFPREG,
1454 		.n = sizeof(struct user_fpsimd_state) / sizeof(u32),
1455 		/*
1456 		 * We pretend we have 32-bit registers because the fpsr and
1457 		 * fpcr are 32-bits wide.
1458 		 */
1459 		.size = sizeof(u32),
1460 		.align = sizeof(u32),
1461 		.active = fpr_active,
1462 		.regset_get = fpr_get,
1463 		.set = fpr_set
1464 	},
1465 	[REGSET_TLS] = {
1466 		.core_note_type = NT_ARM_TLS,
1467 		.n = 2,
1468 		.size = sizeof(void *),
1469 		.align = sizeof(void *),
1470 		.regset_get = tls_get,
1471 		.set = tls_set,
1472 	},
1473 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1474 	[REGSET_HW_BREAK] = {
1475 		.core_note_type = NT_ARM_HW_BREAK,
1476 		.n = sizeof(struct user_hwdebug_state) / sizeof(u32),
1477 		.size = sizeof(u32),
1478 		.align = sizeof(u32),
1479 		.regset_get = hw_break_get,
1480 		.set = hw_break_set,
1481 	},
1482 	[REGSET_HW_WATCH] = {
1483 		.core_note_type = NT_ARM_HW_WATCH,
1484 		.n = sizeof(struct user_hwdebug_state) / sizeof(u32),
1485 		.size = sizeof(u32),
1486 		.align = sizeof(u32),
1487 		.regset_get = hw_break_get,
1488 		.set = hw_break_set,
1489 	},
1490 #endif
1491 	[REGSET_SYSTEM_CALL] = {
1492 		.core_note_type = NT_ARM_SYSTEM_CALL,
1493 		.n = 1,
1494 		.size = sizeof(int),
1495 		.align = sizeof(int),
1496 		.regset_get = system_call_get,
1497 		.set = system_call_set,
1498 	},
1499 #ifdef CONFIG_ARM64_SVE
1500 	[REGSET_SVE] = { /* Scalable Vector Extension */
1501 		.core_note_type = NT_ARM_SVE,
1502 		.n = DIV_ROUND_UP(SVE_PT_SIZE(SVE_VQ_MAX, SVE_PT_REGS_SVE),
1503 				  SVE_VQ_BYTES),
1504 		.size = SVE_VQ_BYTES,
1505 		.align = SVE_VQ_BYTES,
1506 		.regset_get = sve_get,
1507 		.set = sve_set,
1508 	},
1509 #endif
1510 #ifdef CONFIG_ARM64_SME
1511 	[REGSET_SSVE] = { /* Streaming mode SVE */
1512 		.core_note_type = NT_ARM_SSVE,
1513 		.n = DIV_ROUND_UP(SVE_PT_SIZE(SME_VQ_MAX, SVE_PT_REGS_SVE),
1514 				  SVE_VQ_BYTES),
1515 		.size = SVE_VQ_BYTES,
1516 		.align = SVE_VQ_BYTES,
1517 		.regset_get = ssve_get,
1518 		.set = ssve_set,
1519 	},
1520 	[REGSET_ZA] = { /* SME ZA */
1521 		.core_note_type = NT_ARM_ZA,
1522 		/*
1523 		 * ZA is a single register but it's variably sized and
1524 		 * the ptrace core requires that the size of any data
1525 		 * be an exact multiple of the configured register
1526 		 * size so report as though we had SVE_VQ_BYTES
1527 		 * registers. These values aren't exposed to
1528 		 * userspace.
1529 		 */
1530 		.n = DIV_ROUND_UP(ZA_PT_SIZE(SME_VQ_MAX), SVE_VQ_BYTES),
1531 		.size = SVE_VQ_BYTES,
1532 		.align = SVE_VQ_BYTES,
1533 		.regset_get = za_get,
1534 		.set = za_set,
1535 	},
1536 	[REGSET_ZT] = { /* SME ZT */
1537 		.core_note_type = NT_ARM_ZT,
1538 		.n = 1,
1539 		.size = ZT_SIG_REG_BYTES,
1540 		.align = sizeof(u64),
1541 		.regset_get = zt_get,
1542 		.set = zt_set,
1543 	},
1544 #endif
1545 #ifdef CONFIG_ARM64_PTR_AUTH
1546 	[REGSET_PAC_MASK] = {
1547 		.core_note_type = NT_ARM_PAC_MASK,
1548 		.n = sizeof(struct user_pac_mask) / sizeof(u64),
1549 		.size = sizeof(u64),
1550 		.align = sizeof(u64),
1551 		.regset_get = pac_mask_get,
1552 		/* this cannot be set dynamically */
1553 	},
1554 	[REGSET_PAC_ENABLED_KEYS] = {
1555 		.core_note_type = NT_ARM_PAC_ENABLED_KEYS,
1556 		.n = 1,
1557 		.size = sizeof(long),
1558 		.align = sizeof(long),
1559 		.regset_get = pac_enabled_keys_get,
1560 		.set = pac_enabled_keys_set,
1561 	},
1562 #ifdef CONFIG_CHECKPOINT_RESTORE
1563 	[REGSET_PACA_KEYS] = {
1564 		.core_note_type = NT_ARM_PACA_KEYS,
1565 		.n = sizeof(struct user_pac_address_keys) / sizeof(__uint128_t),
1566 		.size = sizeof(__uint128_t),
1567 		.align = sizeof(__uint128_t),
1568 		.regset_get = pac_address_keys_get,
1569 		.set = pac_address_keys_set,
1570 	},
1571 	[REGSET_PACG_KEYS] = {
1572 		.core_note_type = NT_ARM_PACG_KEYS,
1573 		.n = sizeof(struct user_pac_generic_keys) / sizeof(__uint128_t),
1574 		.size = sizeof(__uint128_t),
1575 		.align = sizeof(__uint128_t),
1576 		.regset_get = pac_generic_keys_get,
1577 		.set = pac_generic_keys_set,
1578 	},
1579 #endif
1580 #endif
1581 #ifdef CONFIG_ARM64_TAGGED_ADDR_ABI
1582 	[REGSET_TAGGED_ADDR_CTRL] = {
1583 		.core_note_type = NT_ARM_TAGGED_ADDR_CTRL,
1584 		.n = 1,
1585 		.size = sizeof(long),
1586 		.align = sizeof(long),
1587 		.regset_get = tagged_addr_ctrl_get,
1588 		.set = tagged_addr_ctrl_set,
1589 	},
1590 #endif
1591 };
1592 
1593 static const struct user_regset_view user_aarch64_view = {
1594 	.name = "aarch64", .e_machine = EM_AARCH64,
1595 	.regsets = aarch64_regsets, .n = ARRAY_SIZE(aarch64_regsets)
1596 };
1597 
1598 #ifdef CONFIG_COMPAT
1599 enum compat_regset {
1600 	REGSET_COMPAT_GPR,
1601 	REGSET_COMPAT_VFP,
1602 };
1603 
1604 static inline compat_ulong_t compat_get_user_reg(struct task_struct *task, int idx)
1605 {
1606 	struct pt_regs *regs = task_pt_regs(task);
1607 
1608 	switch (idx) {
1609 	case 15:
1610 		return regs->pc;
1611 	case 16:
1612 		return pstate_to_compat_psr(regs->pstate);
1613 	case 17:
1614 		return regs->orig_x0;
1615 	default:
1616 		return regs->regs[idx];
1617 	}
1618 }
1619 
1620 static int compat_gpr_get(struct task_struct *target,
1621 			  const struct user_regset *regset,
1622 			  struct membuf to)
1623 {
1624 	int i = 0;
1625 
1626 	while (to.left)
1627 		membuf_store(&to, compat_get_user_reg(target, i++));
1628 	return 0;
1629 }
1630 
1631 static int compat_gpr_set(struct task_struct *target,
1632 			  const struct user_regset *regset,
1633 			  unsigned int pos, unsigned int count,
1634 			  const void *kbuf, const void __user *ubuf)
1635 {
1636 	struct pt_regs newregs;
1637 	int ret = 0;
1638 	unsigned int i, start, num_regs;
1639 
1640 	/* Calculate the number of AArch32 registers contained in count */
1641 	num_regs = count / regset->size;
1642 
1643 	/* Convert pos into an register number */
1644 	start = pos / regset->size;
1645 
1646 	if (start + num_regs > regset->n)
1647 		return -EIO;
1648 
1649 	newregs = *task_pt_regs(target);
1650 
1651 	for (i = 0; i < num_regs; ++i) {
1652 		unsigned int idx = start + i;
1653 		compat_ulong_t reg;
1654 
1655 		if (kbuf) {
1656 			memcpy(&reg, kbuf, sizeof(reg));
1657 			kbuf += sizeof(reg);
1658 		} else {
1659 			ret = copy_from_user(&reg, ubuf, sizeof(reg));
1660 			if (ret) {
1661 				ret = -EFAULT;
1662 				break;
1663 			}
1664 
1665 			ubuf += sizeof(reg);
1666 		}
1667 
1668 		switch (idx) {
1669 		case 15:
1670 			newregs.pc = reg;
1671 			break;
1672 		case 16:
1673 			reg = compat_psr_to_pstate(reg);
1674 			newregs.pstate = reg;
1675 			break;
1676 		case 17:
1677 			newregs.orig_x0 = reg;
1678 			break;
1679 		default:
1680 			newregs.regs[idx] = reg;
1681 		}
1682 
1683 	}
1684 
1685 	if (valid_user_regs(&newregs.user_regs, target))
1686 		*task_pt_regs(target) = newregs;
1687 	else
1688 		ret = -EINVAL;
1689 
1690 	return ret;
1691 }
1692 
1693 static int compat_vfp_get(struct task_struct *target,
1694 			  const struct user_regset *regset,
1695 			  struct membuf to)
1696 {
1697 	struct user_fpsimd_state *uregs;
1698 	compat_ulong_t fpscr;
1699 
1700 	if (!system_supports_fpsimd())
1701 		return -EINVAL;
1702 
1703 	uregs = &target->thread.uw.fpsimd_state;
1704 
1705 	if (target == current)
1706 		fpsimd_preserve_current_state();
1707 
1708 	/*
1709 	 * The VFP registers are packed into the fpsimd_state, so they all sit
1710 	 * nicely together for us. We just need to create the fpscr separately.
1711 	 */
1712 	membuf_write(&to, uregs, VFP_STATE_SIZE - sizeof(compat_ulong_t));
1713 	fpscr = (uregs->fpsr & VFP_FPSCR_STAT_MASK) |
1714 		(uregs->fpcr & VFP_FPSCR_CTRL_MASK);
1715 	return membuf_store(&to, fpscr);
1716 }
1717 
1718 static int compat_vfp_set(struct task_struct *target,
1719 			  const struct user_regset *regset,
1720 			  unsigned int pos, unsigned int count,
1721 			  const void *kbuf, const void __user *ubuf)
1722 {
1723 	struct user_fpsimd_state *uregs;
1724 	compat_ulong_t fpscr;
1725 	int ret, vregs_end_pos;
1726 
1727 	if (!system_supports_fpsimd())
1728 		return -EINVAL;
1729 
1730 	uregs = &target->thread.uw.fpsimd_state;
1731 
1732 	vregs_end_pos = VFP_STATE_SIZE - sizeof(compat_ulong_t);
1733 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, uregs, 0,
1734 				 vregs_end_pos);
1735 
1736 	if (count && !ret) {
1737 		ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &fpscr,
1738 					 vregs_end_pos, VFP_STATE_SIZE);
1739 		if (!ret) {
1740 			uregs->fpsr = fpscr & VFP_FPSCR_STAT_MASK;
1741 			uregs->fpcr = fpscr & VFP_FPSCR_CTRL_MASK;
1742 		}
1743 	}
1744 
1745 	fpsimd_flush_task_state(target);
1746 	return ret;
1747 }
1748 
1749 static int compat_tls_get(struct task_struct *target,
1750 			  const struct user_regset *regset,
1751 			  struct membuf to)
1752 {
1753 	return membuf_store(&to, (compat_ulong_t)target->thread.uw.tp_value);
1754 }
1755 
1756 static int compat_tls_set(struct task_struct *target,
1757 			  const struct user_regset *regset, unsigned int pos,
1758 			  unsigned int count, const void *kbuf,
1759 			  const void __user *ubuf)
1760 {
1761 	int ret;
1762 	compat_ulong_t tls = target->thread.uw.tp_value;
1763 
1764 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
1765 	if (ret)
1766 		return ret;
1767 
1768 	target->thread.uw.tp_value = tls;
1769 	return ret;
1770 }
1771 
1772 static const struct user_regset aarch32_regsets[] = {
1773 	[REGSET_COMPAT_GPR] = {
1774 		.core_note_type = NT_PRSTATUS,
1775 		.n = COMPAT_ELF_NGREG,
1776 		.size = sizeof(compat_elf_greg_t),
1777 		.align = sizeof(compat_elf_greg_t),
1778 		.regset_get = compat_gpr_get,
1779 		.set = compat_gpr_set
1780 	},
1781 	[REGSET_COMPAT_VFP] = {
1782 		.core_note_type = NT_ARM_VFP,
1783 		.n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
1784 		.size = sizeof(compat_ulong_t),
1785 		.align = sizeof(compat_ulong_t),
1786 		.active = fpr_active,
1787 		.regset_get = compat_vfp_get,
1788 		.set = compat_vfp_set
1789 	},
1790 };
1791 
1792 static const struct user_regset_view user_aarch32_view = {
1793 	.name = "aarch32", .e_machine = EM_ARM,
1794 	.regsets = aarch32_regsets, .n = ARRAY_SIZE(aarch32_regsets)
1795 };
1796 
1797 static const struct user_regset aarch32_ptrace_regsets[] = {
1798 	[REGSET_GPR] = {
1799 		.core_note_type = NT_PRSTATUS,
1800 		.n = COMPAT_ELF_NGREG,
1801 		.size = sizeof(compat_elf_greg_t),
1802 		.align = sizeof(compat_elf_greg_t),
1803 		.regset_get = compat_gpr_get,
1804 		.set = compat_gpr_set
1805 	},
1806 	[REGSET_FPR] = {
1807 		.core_note_type = NT_ARM_VFP,
1808 		.n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
1809 		.size = sizeof(compat_ulong_t),
1810 		.align = sizeof(compat_ulong_t),
1811 		.regset_get = compat_vfp_get,
1812 		.set = compat_vfp_set
1813 	},
1814 	[REGSET_TLS] = {
1815 		.core_note_type = NT_ARM_TLS,
1816 		.n = 1,
1817 		.size = sizeof(compat_ulong_t),
1818 		.align = sizeof(compat_ulong_t),
1819 		.regset_get = compat_tls_get,
1820 		.set = compat_tls_set,
1821 	},
1822 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1823 	[REGSET_HW_BREAK] = {
1824 		.core_note_type = NT_ARM_HW_BREAK,
1825 		.n = sizeof(struct user_hwdebug_state) / sizeof(u32),
1826 		.size = sizeof(u32),
1827 		.align = sizeof(u32),
1828 		.regset_get = hw_break_get,
1829 		.set = hw_break_set,
1830 	},
1831 	[REGSET_HW_WATCH] = {
1832 		.core_note_type = NT_ARM_HW_WATCH,
1833 		.n = sizeof(struct user_hwdebug_state) / sizeof(u32),
1834 		.size = sizeof(u32),
1835 		.align = sizeof(u32),
1836 		.regset_get = hw_break_get,
1837 		.set = hw_break_set,
1838 	},
1839 #endif
1840 	[REGSET_SYSTEM_CALL] = {
1841 		.core_note_type = NT_ARM_SYSTEM_CALL,
1842 		.n = 1,
1843 		.size = sizeof(int),
1844 		.align = sizeof(int),
1845 		.regset_get = system_call_get,
1846 		.set = system_call_set,
1847 	},
1848 };
1849 
1850 static const struct user_regset_view user_aarch32_ptrace_view = {
1851 	.name = "aarch32", .e_machine = EM_ARM,
1852 	.regsets = aarch32_ptrace_regsets, .n = ARRAY_SIZE(aarch32_ptrace_regsets)
1853 };
1854 
1855 static int compat_ptrace_read_user(struct task_struct *tsk, compat_ulong_t off,
1856 				   compat_ulong_t __user *ret)
1857 {
1858 	compat_ulong_t tmp;
1859 
1860 	if (off & 3)
1861 		return -EIO;
1862 
1863 	if (off == COMPAT_PT_TEXT_ADDR)
1864 		tmp = tsk->mm->start_code;
1865 	else if (off == COMPAT_PT_DATA_ADDR)
1866 		tmp = tsk->mm->start_data;
1867 	else if (off == COMPAT_PT_TEXT_END_ADDR)
1868 		tmp = tsk->mm->end_code;
1869 	else if (off < sizeof(compat_elf_gregset_t))
1870 		tmp = compat_get_user_reg(tsk, off >> 2);
1871 	else if (off >= COMPAT_USER_SZ)
1872 		return -EIO;
1873 	else
1874 		tmp = 0;
1875 
1876 	return put_user(tmp, ret);
1877 }
1878 
1879 static int compat_ptrace_write_user(struct task_struct *tsk, compat_ulong_t off,
1880 				    compat_ulong_t val)
1881 {
1882 	struct pt_regs newregs = *task_pt_regs(tsk);
1883 	unsigned int idx = off / 4;
1884 
1885 	if (off & 3 || off >= COMPAT_USER_SZ)
1886 		return -EIO;
1887 
1888 	if (off >= sizeof(compat_elf_gregset_t))
1889 		return 0;
1890 
1891 	switch (idx) {
1892 	case 15:
1893 		newregs.pc = val;
1894 		break;
1895 	case 16:
1896 		newregs.pstate = compat_psr_to_pstate(val);
1897 		break;
1898 	case 17:
1899 		newregs.orig_x0 = val;
1900 		break;
1901 	default:
1902 		newregs.regs[idx] = val;
1903 	}
1904 
1905 	if (!valid_user_regs(&newregs.user_regs, tsk))
1906 		return -EINVAL;
1907 
1908 	*task_pt_regs(tsk) = newregs;
1909 	return 0;
1910 }
1911 
1912 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1913 
1914 /*
1915  * Convert a virtual register number into an index for a thread_info
1916  * breakpoint array. Breakpoints are identified using positive numbers
1917  * whilst watchpoints are negative. The registers are laid out as pairs
1918  * of (address, control), each pair mapping to a unique hw_breakpoint struct.
1919  * Register 0 is reserved for describing resource information.
1920  */
1921 static int compat_ptrace_hbp_num_to_idx(compat_long_t num)
1922 {
1923 	return (abs(num) - 1) >> 1;
1924 }
1925 
1926 static int compat_ptrace_hbp_get_resource_info(u32 *kdata)
1927 {
1928 	u8 num_brps, num_wrps, debug_arch, wp_len;
1929 	u32 reg = 0;
1930 
1931 	num_brps	= hw_breakpoint_slots(TYPE_INST);
1932 	num_wrps	= hw_breakpoint_slots(TYPE_DATA);
1933 
1934 	debug_arch	= debug_monitors_arch();
1935 	wp_len		= 8;
1936 	reg		|= debug_arch;
1937 	reg		<<= 8;
1938 	reg		|= wp_len;
1939 	reg		<<= 8;
1940 	reg		|= num_wrps;
1941 	reg		<<= 8;
1942 	reg		|= num_brps;
1943 
1944 	*kdata = reg;
1945 	return 0;
1946 }
1947 
1948 static int compat_ptrace_hbp_get(unsigned int note_type,
1949 				 struct task_struct *tsk,
1950 				 compat_long_t num,
1951 				 u32 *kdata)
1952 {
1953 	u64 addr = 0;
1954 	u32 ctrl = 0;
1955 
1956 	int err, idx = compat_ptrace_hbp_num_to_idx(num);
1957 
1958 	if (num & 1) {
1959 		err = ptrace_hbp_get_addr(note_type, tsk, idx, &addr);
1960 		*kdata = (u32)addr;
1961 	} else {
1962 		err = ptrace_hbp_get_ctrl(note_type, tsk, idx, &ctrl);
1963 		*kdata = ctrl;
1964 	}
1965 
1966 	return err;
1967 }
1968 
1969 static int compat_ptrace_hbp_set(unsigned int note_type,
1970 				 struct task_struct *tsk,
1971 				 compat_long_t num,
1972 				 u32 *kdata)
1973 {
1974 	u64 addr;
1975 	u32 ctrl;
1976 
1977 	int err, idx = compat_ptrace_hbp_num_to_idx(num);
1978 
1979 	if (num & 1) {
1980 		addr = *kdata;
1981 		err = ptrace_hbp_set_addr(note_type, tsk, idx, addr);
1982 	} else {
1983 		ctrl = *kdata;
1984 		err = ptrace_hbp_set_ctrl(note_type, tsk, idx, ctrl);
1985 	}
1986 
1987 	return err;
1988 }
1989 
1990 static int compat_ptrace_gethbpregs(struct task_struct *tsk, compat_long_t num,
1991 				    compat_ulong_t __user *data)
1992 {
1993 	int ret;
1994 	u32 kdata;
1995 
1996 	/* Watchpoint */
1997 	if (num < 0) {
1998 		ret = compat_ptrace_hbp_get(NT_ARM_HW_WATCH, tsk, num, &kdata);
1999 	/* Resource info */
2000 	} else if (num == 0) {
2001 		ret = compat_ptrace_hbp_get_resource_info(&kdata);
2002 	/* Breakpoint */
2003 	} else {
2004 		ret = compat_ptrace_hbp_get(NT_ARM_HW_BREAK, tsk, num, &kdata);
2005 	}
2006 
2007 	if (!ret)
2008 		ret = put_user(kdata, data);
2009 
2010 	return ret;
2011 }
2012 
2013 static int compat_ptrace_sethbpregs(struct task_struct *tsk, compat_long_t num,
2014 				    compat_ulong_t __user *data)
2015 {
2016 	int ret;
2017 	u32 kdata = 0;
2018 
2019 	if (num == 0)
2020 		return 0;
2021 
2022 	ret = get_user(kdata, data);
2023 	if (ret)
2024 		return ret;
2025 
2026 	if (num < 0)
2027 		ret = compat_ptrace_hbp_set(NT_ARM_HW_WATCH, tsk, num, &kdata);
2028 	else
2029 		ret = compat_ptrace_hbp_set(NT_ARM_HW_BREAK, tsk, num, &kdata);
2030 
2031 	return ret;
2032 }
2033 #endif	/* CONFIG_HAVE_HW_BREAKPOINT */
2034 
2035 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
2036 			compat_ulong_t caddr, compat_ulong_t cdata)
2037 {
2038 	unsigned long addr = caddr;
2039 	unsigned long data = cdata;
2040 	void __user *datap = compat_ptr(data);
2041 	int ret;
2042 
2043 	switch (request) {
2044 		case PTRACE_PEEKUSR:
2045 			ret = compat_ptrace_read_user(child, addr, datap);
2046 			break;
2047 
2048 		case PTRACE_POKEUSR:
2049 			ret = compat_ptrace_write_user(child, addr, data);
2050 			break;
2051 
2052 		case COMPAT_PTRACE_GETREGS:
2053 			ret = copy_regset_to_user(child,
2054 						  &user_aarch32_view,
2055 						  REGSET_COMPAT_GPR,
2056 						  0, sizeof(compat_elf_gregset_t),
2057 						  datap);
2058 			break;
2059 
2060 		case COMPAT_PTRACE_SETREGS:
2061 			ret = copy_regset_from_user(child,
2062 						    &user_aarch32_view,
2063 						    REGSET_COMPAT_GPR,
2064 						    0, sizeof(compat_elf_gregset_t),
2065 						    datap);
2066 			break;
2067 
2068 		case COMPAT_PTRACE_GET_THREAD_AREA:
2069 			ret = put_user((compat_ulong_t)child->thread.uw.tp_value,
2070 				       (compat_ulong_t __user *)datap);
2071 			break;
2072 
2073 		case COMPAT_PTRACE_SET_SYSCALL:
2074 			task_pt_regs(child)->syscallno = data;
2075 			ret = 0;
2076 			break;
2077 
2078 		case COMPAT_PTRACE_GETVFPREGS:
2079 			ret = copy_regset_to_user(child,
2080 						  &user_aarch32_view,
2081 						  REGSET_COMPAT_VFP,
2082 						  0, VFP_STATE_SIZE,
2083 						  datap);
2084 			break;
2085 
2086 		case COMPAT_PTRACE_SETVFPREGS:
2087 			ret = copy_regset_from_user(child,
2088 						    &user_aarch32_view,
2089 						    REGSET_COMPAT_VFP,
2090 						    0, VFP_STATE_SIZE,
2091 						    datap);
2092 			break;
2093 
2094 #ifdef CONFIG_HAVE_HW_BREAKPOINT
2095 		case COMPAT_PTRACE_GETHBPREGS:
2096 			ret = compat_ptrace_gethbpregs(child, addr, datap);
2097 			break;
2098 
2099 		case COMPAT_PTRACE_SETHBPREGS:
2100 			ret = compat_ptrace_sethbpregs(child, addr, datap);
2101 			break;
2102 #endif
2103 
2104 		default:
2105 			ret = compat_ptrace_request(child, request, addr,
2106 						    data);
2107 			break;
2108 	}
2109 
2110 	return ret;
2111 }
2112 #endif /* CONFIG_COMPAT */
2113 
2114 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
2115 {
2116 #ifdef CONFIG_COMPAT
2117 	/*
2118 	 * Core dumping of 32-bit tasks or compat ptrace requests must use the
2119 	 * user_aarch32_view compatible with arm32. Native ptrace requests on
2120 	 * 32-bit children use an extended user_aarch32_ptrace_view to allow
2121 	 * access to the TLS register.
2122 	 */
2123 	if (is_compat_task())
2124 		return &user_aarch32_view;
2125 	else if (is_compat_thread(task_thread_info(task)))
2126 		return &user_aarch32_ptrace_view;
2127 #endif
2128 	return &user_aarch64_view;
2129 }
2130 
2131 long arch_ptrace(struct task_struct *child, long request,
2132 		 unsigned long addr, unsigned long data)
2133 {
2134 	switch (request) {
2135 	case PTRACE_PEEKMTETAGS:
2136 	case PTRACE_POKEMTETAGS:
2137 		return mte_ptrace_copy_tags(child, request, addr, data);
2138 	}
2139 
2140 	return ptrace_request(child, request, addr, data);
2141 }
2142 
2143 enum ptrace_syscall_dir {
2144 	PTRACE_SYSCALL_ENTER = 0,
2145 	PTRACE_SYSCALL_EXIT,
2146 };
2147 
2148 static void report_syscall(struct pt_regs *regs, enum ptrace_syscall_dir dir)
2149 {
2150 	int regno;
2151 	unsigned long saved_reg;
2152 
2153 	/*
2154 	 * We have some ABI weirdness here in the way that we handle syscall
2155 	 * exit stops because we indicate whether or not the stop has been
2156 	 * signalled from syscall entry or syscall exit by clobbering a general
2157 	 * purpose register (ip/r12 for AArch32, x7 for AArch64) in the tracee
2158 	 * and restoring its old value after the stop. This means that:
2159 	 *
2160 	 * - Any writes by the tracer to this register during the stop are
2161 	 *   ignored/discarded.
2162 	 *
2163 	 * - The actual value of the register is not available during the stop,
2164 	 *   so the tracer cannot save it and restore it later.
2165 	 *
2166 	 * - Syscall stops behave differently to seccomp and pseudo-step traps
2167 	 *   (the latter do not nobble any registers).
2168 	 */
2169 	regno = (is_compat_task() ? 12 : 7);
2170 	saved_reg = regs->regs[regno];
2171 	regs->regs[regno] = dir;
2172 
2173 	if (dir == PTRACE_SYSCALL_ENTER) {
2174 		if (ptrace_report_syscall_entry(regs))
2175 			forget_syscall(regs);
2176 		regs->regs[regno] = saved_reg;
2177 	} else if (!test_thread_flag(TIF_SINGLESTEP)) {
2178 		ptrace_report_syscall_exit(regs, 0);
2179 		regs->regs[regno] = saved_reg;
2180 	} else {
2181 		regs->regs[regno] = saved_reg;
2182 
2183 		/*
2184 		 * Signal a pseudo-step exception since we are stepping but
2185 		 * tracer modifications to the registers may have rewound the
2186 		 * state machine.
2187 		 */
2188 		ptrace_report_syscall_exit(regs, 1);
2189 	}
2190 }
2191 
2192 int syscall_trace_enter(struct pt_regs *regs)
2193 {
2194 	unsigned long flags = read_thread_flags();
2195 
2196 	if (flags & (_TIF_SYSCALL_EMU | _TIF_SYSCALL_TRACE)) {
2197 		report_syscall(regs, PTRACE_SYSCALL_ENTER);
2198 		if (flags & _TIF_SYSCALL_EMU)
2199 			return NO_SYSCALL;
2200 	}
2201 
2202 	/* Do the secure computing after ptrace; failures should be fast. */
2203 	if (secure_computing() == -1)
2204 		return NO_SYSCALL;
2205 
2206 	if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
2207 		trace_sys_enter(regs, regs->syscallno);
2208 
2209 	audit_syscall_entry(regs->syscallno, regs->orig_x0, regs->regs[1],
2210 			    regs->regs[2], regs->regs[3]);
2211 
2212 	return regs->syscallno;
2213 }
2214 
2215 void syscall_trace_exit(struct pt_regs *regs)
2216 {
2217 	unsigned long flags = read_thread_flags();
2218 
2219 	audit_syscall_exit(regs);
2220 
2221 	if (flags & _TIF_SYSCALL_TRACEPOINT)
2222 		trace_sys_exit(regs, syscall_get_return_value(current, regs));
2223 
2224 	if (flags & (_TIF_SYSCALL_TRACE | _TIF_SINGLESTEP))
2225 		report_syscall(regs, PTRACE_SYSCALL_EXIT);
2226 
2227 	rseq_syscall(regs);
2228 }
2229 
2230 /*
2231  * SPSR_ELx bits which are always architecturally RES0 per ARM DDI 0487D.a.
2232  * We permit userspace to set SSBS (AArch64 bit 12, AArch32 bit 23) which is
2233  * not described in ARM DDI 0487D.a.
2234  * We treat PAN and UAO as RES0 bits, as they are meaningless at EL0, and may
2235  * be allocated an EL0 meaning in future.
2236  * Userspace cannot use these until they have an architectural meaning.
2237  * Note that this follows the SPSR_ELx format, not the AArch32 PSR format.
2238  * We also reserve IL for the kernel; SS is handled dynamically.
2239  */
2240 #define SPSR_EL1_AARCH64_RES0_BITS \
2241 	(GENMASK_ULL(63, 32) | GENMASK_ULL(27, 26) | GENMASK_ULL(23, 22) | \
2242 	 GENMASK_ULL(20, 13) | GENMASK_ULL(5, 5))
2243 #define SPSR_EL1_AARCH32_RES0_BITS \
2244 	(GENMASK_ULL(63, 32) | GENMASK_ULL(22, 22) | GENMASK_ULL(20, 20))
2245 
2246 static int valid_compat_regs(struct user_pt_regs *regs)
2247 {
2248 	regs->pstate &= ~SPSR_EL1_AARCH32_RES0_BITS;
2249 
2250 	if (!system_supports_mixed_endian_el0()) {
2251 		if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
2252 			regs->pstate |= PSR_AA32_E_BIT;
2253 		else
2254 			regs->pstate &= ~PSR_AA32_E_BIT;
2255 	}
2256 
2257 	if (user_mode(regs) && (regs->pstate & PSR_MODE32_BIT) &&
2258 	    (regs->pstate & PSR_AA32_A_BIT) == 0 &&
2259 	    (regs->pstate & PSR_AA32_I_BIT) == 0 &&
2260 	    (regs->pstate & PSR_AA32_F_BIT) == 0) {
2261 		return 1;
2262 	}
2263 
2264 	/*
2265 	 * Force PSR to a valid 32-bit EL0t, preserving the same bits as
2266 	 * arch/arm.
2267 	 */
2268 	regs->pstate &= PSR_AA32_N_BIT | PSR_AA32_Z_BIT |
2269 			PSR_AA32_C_BIT | PSR_AA32_V_BIT |
2270 			PSR_AA32_Q_BIT | PSR_AA32_IT_MASK |
2271 			PSR_AA32_GE_MASK | PSR_AA32_E_BIT |
2272 			PSR_AA32_T_BIT;
2273 	regs->pstate |= PSR_MODE32_BIT;
2274 
2275 	return 0;
2276 }
2277 
2278 static int valid_native_regs(struct user_pt_regs *regs)
2279 {
2280 	regs->pstate &= ~SPSR_EL1_AARCH64_RES0_BITS;
2281 
2282 	if (user_mode(regs) && !(regs->pstate & PSR_MODE32_BIT) &&
2283 	    (regs->pstate & PSR_D_BIT) == 0 &&
2284 	    (regs->pstate & PSR_A_BIT) == 0 &&
2285 	    (regs->pstate & PSR_I_BIT) == 0 &&
2286 	    (regs->pstate & PSR_F_BIT) == 0) {
2287 		return 1;
2288 	}
2289 
2290 	/* Force PSR to a valid 64-bit EL0t */
2291 	regs->pstate &= PSR_N_BIT | PSR_Z_BIT | PSR_C_BIT | PSR_V_BIT;
2292 
2293 	return 0;
2294 }
2295 
2296 /*
2297  * Are the current registers suitable for user mode? (used to maintain
2298  * security in signal handlers)
2299  */
2300 int valid_user_regs(struct user_pt_regs *regs, struct task_struct *task)
2301 {
2302 	/* https://lore.kernel.org/lkml/20191118131525.GA4180@willie-the-truck */
2303 	user_regs_reset_single_step(regs, task);
2304 
2305 	if (is_compat_thread(task_thread_info(task)))
2306 		return valid_compat_regs(regs);
2307 	else
2308 		return valid_native_regs(regs);
2309 }
2310