xref: /OK3568_Linux_fs/kernel/arch/arm64/kernel/ptrace.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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/tracehook.h>
31 #include <linux/elf.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  */
regs_query_register_offset(const char * name)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  */
regs_within_kernel_stack(struct pt_regs * regs,unsigned long addr)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, NULL);
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  */
regs_get_kernel_stack_nth(struct pt_regs * regs,unsigned int n)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  */
ptrace_disable(struct task_struct * child)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  */
ptrace_hbptriggered(struct perf_event * bp,struct perf_sample_data * data,struct pt_regs * regs)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 	}
198 #endif
199 	arm64_force_sig_fault(SIGTRAP, TRAP_HWBKPT, bkpt->trigger, desc);
200 }
201 
202 /*
203  * Unregister breakpoints from this task and reset the pointers in
204  * the thread_struct.
205  */
flush_ptrace_hw_breakpoint(struct task_struct * tsk)206 void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
207 {
208 	int i;
209 	struct thread_struct *t = &tsk->thread;
210 
211 	for (i = 0; i < ARM_MAX_BRP; i++) {
212 		if (t->debug.hbp_break[i]) {
213 			unregister_hw_breakpoint(t->debug.hbp_break[i]);
214 			t->debug.hbp_break[i] = NULL;
215 		}
216 	}
217 
218 	for (i = 0; i < ARM_MAX_WRP; i++) {
219 		if (t->debug.hbp_watch[i]) {
220 			unregister_hw_breakpoint(t->debug.hbp_watch[i]);
221 			t->debug.hbp_watch[i] = NULL;
222 		}
223 	}
224 }
225 
ptrace_hw_copy_thread(struct task_struct * tsk)226 void ptrace_hw_copy_thread(struct task_struct *tsk)
227 {
228 	memset(&tsk->thread.debug, 0, sizeof(struct debug_info));
229 }
230 
ptrace_hbp_get_event(unsigned int note_type,struct task_struct * tsk,unsigned long idx)231 static struct perf_event *ptrace_hbp_get_event(unsigned int note_type,
232 					       struct task_struct *tsk,
233 					       unsigned long idx)
234 {
235 	struct perf_event *bp = ERR_PTR(-EINVAL);
236 
237 	switch (note_type) {
238 	case NT_ARM_HW_BREAK:
239 		if (idx >= ARM_MAX_BRP)
240 			goto out;
241 		idx = array_index_nospec(idx, ARM_MAX_BRP);
242 		bp = tsk->thread.debug.hbp_break[idx];
243 		break;
244 	case NT_ARM_HW_WATCH:
245 		if (idx >= ARM_MAX_WRP)
246 			goto out;
247 		idx = array_index_nospec(idx, ARM_MAX_WRP);
248 		bp = tsk->thread.debug.hbp_watch[idx];
249 		break;
250 	}
251 
252 out:
253 	return bp;
254 }
255 
ptrace_hbp_set_event(unsigned int note_type,struct task_struct * tsk,unsigned long idx,struct perf_event * bp)256 static int ptrace_hbp_set_event(unsigned int note_type,
257 				struct task_struct *tsk,
258 				unsigned long idx,
259 				struct perf_event *bp)
260 {
261 	int err = -EINVAL;
262 
263 	switch (note_type) {
264 	case NT_ARM_HW_BREAK:
265 		if (idx >= ARM_MAX_BRP)
266 			goto out;
267 		idx = array_index_nospec(idx, ARM_MAX_BRP);
268 		tsk->thread.debug.hbp_break[idx] = bp;
269 		err = 0;
270 		break;
271 	case NT_ARM_HW_WATCH:
272 		if (idx >= ARM_MAX_WRP)
273 			goto out;
274 		idx = array_index_nospec(idx, ARM_MAX_WRP);
275 		tsk->thread.debug.hbp_watch[idx] = bp;
276 		err = 0;
277 		break;
278 	}
279 
280 out:
281 	return err;
282 }
283 
ptrace_hbp_create(unsigned int note_type,struct task_struct * tsk,unsigned long idx)284 static struct perf_event *ptrace_hbp_create(unsigned int note_type,
285 					    struct task_struct *tsk,
286 					    unsigned long idx)
287 {
288 	struct perf_event *bp;
289 	struct perf_event_attr attr;
290 	int err, type;
291 
292 	switch (note_type) {
293 	case NT_ARM_HW_BREAK:
294 		type = HW_BREAKPOINT_X;
295 		break;
296 	case NT_ARM_HW_WATCH:
297 		type = HW_BREAKPOINT_RW;
298 		break;
299 	default:
300 		return ERR_PTR(-EINVAL);
301 	}
302 
303 	ptrace_breakpoint_init(&attr);
304 
305 	/*
306 	 * Initialise fields to sane defaults
307 	 * (i.e. values that will pass validation).
308 	 */
309 	attr.bp_addr	= 0;
310 	attr.bp_len	= HW_BREAKPOINT_LEN_4;
311 	attr.bp_type	= type;
312 	attr.disabled	= 1;
313 
314 	bp = register_user_hw_breakpoint(&attr, ptrace_hbptriggered, NULL, tsk);
315 	if (IS_ERR(bp))
316 		return bp;
317 
318 	err = ptrace_hbp_set_event(note_type, tsk, idx, bp);
319 	if (err)
320 		return ERR_PTR(err);
321 
322 	return bp;
323 }
324 
ptrace_hbp_fill_attr_ctrl(unsigned int note_type,struct arch_hw_breakpoint_ctrl ctrl,struct perf_event_attr * attr)325 static int ptrace_hbp_fill_attr_ctrl(unsigned int note_type,
326 				     struct arch_hw_breakpoint_ctrl ctrl,
327 				     struct perf_event_attr *attr)
328 {
329 	int err, len, type, offset, disabled = !ctrl.enabled;
330 
331 	attr->disabled = disabled;
332 	if (disabled)
333 		return 0;
334 
335 	err = arch_bp_generic_fields(ctrl, &len, &type, &offset);
336 	if (err)
337 		return err;
338 
339 	switch (note_type) {
340 	case NT_ARM_HW_BREAK:
341 		if ((type & HW_BREAKPOINT_X) != type)
342 			return -EINVAL;
343 		break;
344 	case NT_ARM_HW_WATCH:
345 		if ((type & HW_BREAKPOINT_RW) != type)
346 			return -EINVAL;
347 		break;
348 	default:
349 		return -EINVAL;
350 	}
351 
352 	attr->bp_len	= len;
353 	attr->bp_type	= type;
354 	attr->bp_addr	+= offset;
355 
356 	return 0;
357 }
358 
ptrace_hbp_get_resource_info(unsigned int note_type,u32 * info)359 static int ptrace_hbp_get_resource_info(unsigned int note_type, u32 *info)
360 {
361 	u8 num;
362 	u32 reg = 0;
363 
364 	switch (note_type) {
365 	case NT_ARM_HW_BREAK:
366 		num = hw_breakpoint_slots(TYPE_INST);
367 		break;
368 	case NT_ARM_HW_WATCH:
369 		num = hw_breakpoint_slots(TYPE_DATA);
370 		break;
371 	default:
372 		return -EINVAL;
373 	}
374 
375 	reg |= debug_monitors_arch();
376 	reg <<= 8;
377 	reg |= num;
378 
379 	*info = reg;
380 	return 0;
381 }
382 
ptrace_hbp_get_ctrl(unsigned int note_type,struct task_struct * tsk,unsigned long idx,u32 * ctrl)383 static int ptrace_hbp_get_ctrl(unsigned int note_type,
384 			       struct task_struct *tsk,
385 			       unsigned long idx,
386 			       u32 *ctrl)
387 {
388 	struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
389 
390 	if (IS_ERR(bp))
391 		return PTR_ERR(bp);
392 
393 	*ctrl = bp ? encode_ctrl_reg(counter_arch_bp(bp)->ctrl) : 0;
394 	return 0;
395 }
396 
ptrace_hbp_get_addr(unsigned int note_type,struct task_struct * tsk,unsigned long idx,u64 * addr)397 static int ptrace_hbp_get_addr(unsigned int note_type,
398 			       struct task_struct *tsk,
399 			       unsigned long idx,
400 			       u64 *addr)
401 {
402 	struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
403 
404 	if (IS_ERR(bp))
405 		return PTR_ERR(bp);
406 
407 	*addr = bp ? counter_arch_bp(bp)->address : 0;
408 	return 0;
409 }
410 
ptrace_hbp_get_initialised_bp(unsigned int note_type,struct task_struct * tsk,unsigned long idx)411 static struct perf_event *ptrace_hbp_get_initialised_bp(unsigned int note_type,
412 							struct task_struct *tsk,
413 							unsigned long idx)
414 {
415 	struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
416 
417 	if (!bp)
418 		bp = ptrace_hbp_create(note_type, tsk, idx);
419 
420 	return bp;
421 }
422 
ptrace_hbp_set_ctrl(unsigned int note_type,struct task_struct * tsk,unsigned long idx,u32 uctrl)423 static int ptrace_hbp_set_ctrl(unsigned int note_type,
424 			       struct task_struct *tsk,
425 			       unsigned long idx,
426 			       u32 uctrl)
427 {
428 	int err;
429 	struct perf_event *bp;
430 	struct perf_event_attr attr;
431 	struct arch_hw_breakpoint_ctrl ctrl;
432 
433 	bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
434 	if (IS_ERR(bp)) {
435 		err = PTR_ERR(bp);
436 		return err;
437 	}
438 
439 	attr = bp->attr;
440 	decode_ctrl_reg(uctrl, &ctrl);
441 	err = ptrace_hbp_fill_attr_ctrl(note_type, ctrl, &attr);
442 	if (err)
443 		return err;
444 
445 	return modify_user_hw_breakpoint(bp, &attr);
446 }
447 
ptrace_hbp_set_addr(unsigned int note_type,struct task_struct * tsk,unsigned long idx,u64 addr)448 static int ptrace_hbp_set_addr(unsigned int note_type,
449 			       struct task_struct *tsk,
450 			       unsigned long idx,
451 			       u64 addr)
452 {
453 	int err;
454 	struct perf_event *bp;
455 	struct perf_event_attr attr;
456 
457 	bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
458 	if (IS_ERR(bp)) {
459 		err = PTR_ERR(bp);
460 		return err;
461 	}
462 
463 	attr = bp->attr;
464 	attr.bp_addr = addr;
465 	err = modify_user_hw_breakpoint(bp, &attr);
466 	return err;
467 }
468 
469 #define PTRACE_HBP_ADDR_SZ	sizeof(u64)
470 #define PTRACE_HBP_CTRL_SZ	sizeof(u32)
471 #define PTRACE_HBP_PAD_SZ	sizeof(u32)
472 
hw_break_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)473 static int hw_break_get(struct task_struct *target,
474 			const struct user_regset *regset,
475 			struct membuf to)
476 {
477 	unsigned int note_type = regset->core_note_type;
478 	int ret, idx = 0;
479 	u32 info, ctrl;
480 	u64 addr;
481 
482 	/* Resource info */
483 	ret = ptrace_hbp_get_resource_info(note_type, &info);
484 	if (ret)
485 		return ret;
486 
487 	membuf_write(&to, &info, sizeof(info));
488 	membuf_zero(&to, sizeof(u32));
489 	/* (address, ctrl) registers */
490 	while (to.left) {
491 		ret = ptrace_hbp_get_addr(note_type, target, idx, &addr);
492 		if (ret)
493 			return ret;
494 		ret = ptrace_hbp_get_ctrl(note_type, target, idx, &ctrl);
495 		if (ret)
496 			return ret;
497 		membuf_store(&to, addr);
498 		membuf_store(&to, ctrl);
499 		membuf_zero(&to, sizeof(u32));
500 		idx++;
501 	}
502 	return 0;
503 }
504 
hw_break_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)505 static int hw_break_set(struct task_struct *target,
506 			const struct user_regset *regset,
507 			unsigned int pos, unsigned int count,
508 			const void *kbuf, const void __user *ubuf)
509 {
510 	unsigned int note_type = regset->core_note_type;
511 	int ret, idx = 0, offset, limit;
512 	u32 ctrl;
513 	u64 addr;
514 
515 	/* Resource info and pad */
516 	offset = offsetof(struct user_hwdebug_state, dbg_regs);
517 	ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, 0, offset);
518 	if (ret)
519 		return ret;
520 
521 	/* (address, ctrl) registers */
522 	limit = regset->n * regset->size;
523 	while (count && offset < limit) {
524 		if (count < PTRACE_HBP_ADDR_SZ)
525 			return -EINVAL;
526 		ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &addr,
527 					 offset, offset + PTRACE_HBP_ADDR_SZ);
528 		if (ret)
529 			return ret;
530 		ret = ptrace_hbp_set_addr(note_type, target, idx, addr);
531 		if (ret)
532 			return ret;
533 		offset += PTRACE_HBP_ADDR_SZ;
534 
535 		if (!count)
536 			break;
537 		ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ctrl,
538 					 offset, offset + PTRACE_HBP_CTRL_SZ);
539 		if (ret)
540 			return ret;
541 		ret = ptrace_hbp_set_ctrl(note_type, target, idx, ctrl);
542 		if (ret)
543 			return ret;
544 		offset += PTRACE_HBP_CTRL_SZ;
545 
546 		ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
547 						offset,
548 						offset + PTRACE_HBP_PAD_SZ);
549 		if (ret)
550 			return ret;
551 		offset += PTRACE_HBP_PAD_SZ;
552 		idx++;
553 	}
554 
555 	return 0;
556 }
557 #endif	/* CONFIG_HAVE_HW_BREAKPOINT */
558 
gpr_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)559 static int gpr_get(struct task_struct *target,
560 		   const struct user_regset *regset,
561 		   struct membuf to)
562 {
563 	struct user_pt_regs *uregs = &task_pt_regs(target)->user_regs;
564 	return membuf_write(&to, uregs, sizeof(*uregs));
565 }
566 
gpr_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)567 static int gpr_set(struct task_struct *target, const struct user_regset *regset,
568 		   unsigned int pos, unsigned int count,
569 		   const void *kbuf, const void __user *ubuf)
570 {
571 	int ret;
572 	struct user_pt_regs newregs = task_pt_regs(target)->user_regs;
573 
574 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newregs, 0, -1);
575 	if (ret)
576 		return ret;
577 
578 	if (!valid_user_regs(&newregs, target))
579 		return -EINVAL;
580 
581 	task_pt_regs(target)->user_regs = newregs;
582 	return 0;
583 }
584 
fpr_active(struct task_struct * target,const struct user_regset * regset)585 static int fpr_active(struct task_struct *target, const struct user_regset *regset)
586 {
587 	if (!system_supports_fpsimd())
588 		return -ENODEV;
589 	return regset->n;
590 }
591 
592 /*
593  * TODO: update fp accessors for lazy context switching (sync/flush hwstate)
594  */
__fpr_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)595 static int __fpr_get(struct task_struct *target,
596 		     const struct user_regset *regset,
597 		     struct membuf to)
598 {
599 	struct user_fpsimd_state *uregs;
600 
601 	sve_sync_to_fpsimd(target);
602 
603 	uregs = &target->thread.uw.fpsimd_state;
604 
605 	return membuf_write(&to, uregs, sizeof(*uregs));
606 }
607 
fpr_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)608 static int fpr_get(struct task_struct *target, const struct user_regset *regset,
609 		   struct membuf to)
610 {
611 	if (!system_supports_fpsimd())
612 		return -EINVAL;
613 
614 	if (target == current)
615 		fpsimd_preserve_current_state();
616 
617 	return __fpr_get(target, regset, to);
618 }
619 
__fpr_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf,unsigned int start_pos)620 static int __fpr_set(struct task_struct *target,
621 		     const struct user_regset *regset,
622 		     unsigned int pos, unsigned int count,
623 		     const void *kbuf, const void __user *ubuf,
624 		     unsigned int start_pos)
625 {
626 	int ret;
627 	struct user_fpsimd_state newstate;
628 
629 	/*
630 	 * Ensure target->thread.uw.fpsimd_state is up to date, so that a
631 	 * short copyin can't resurrect stale data.
632 	 */
633 	sve_sync_to_fpsimd(target);
634 
635 	newstate = target->thread.uw.fpsimd_state;
636 
637 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newstate,
638 				 start_pos, start_pos + sizeof(newstate));
639 	if (ret)
640 		return ret;
641 
642 	target->thread.uw.fpsimd_state = newstate;
643 
644 	return ret;
645 }
646 
fpr_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)647 static int fpr_set(struct task_struct *target, const struct user_regset *regset,
648 		   unsigned int pos, unsigned int count,
649 		   const void *kbuf, const void __user *ubuf)
650 {
651 	int ret;
652 
653 	if (!system_supports_fpsimd())
654 		return -EINVAL;
655 
656 	ret = __fpr_set(target, regset, pos, count, kbuf, ubuf, 0);
657 	if (ret)
658 		return ret;
659 
660 	sve_sync_from_fpsimd_zeropad(target);
661 	fpsimd_flush_task_state(target);
662 
663 	return ret;
664 }
665 
tls_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)666 static int tls_get(struct task_struct *target, const struct user_regset *regset,
667 		   struct membuf to)
668 {
669 	if (target == current)
670 		tls_preserve_current_state();
671 
672 	return membuf_store(&to, target->thread.uw.tp_value);
673 }
674 
tls_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)675 static int tls_set(struct task_struct *target, const struct user_regset *regset,
676 		   unsigned int pos, unsigned int count,
677 		   const void *kbuf, const void __user *ubuf)
678 {
679 	int ret;
680 	unsigned long tls = target->thread.uw.tp_value;
681 
682 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
683 	if (ret)
684 		return ret;
685 
686 	target->thread.uw.tp_value = tls;
687 	return ret;
688 }
689 
system_call_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)690 static int system_call_get(struct task_struct *target,
691 			   const struct user_regset *regset,
692 			   struct membuf to)
693 {
694 	return membuf_store(&to, task_pt_regs(target)->syscallno);
695 }
696 
system_call_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)697 static int system_call_set(struct task_struct *target,
698 			   const struct user_regset *regset,
699 			   unsigned int pos, unsigned int count,
700 			   const void *kbuf, const void __user *ubuf)
701 {
702 	int syscallno = task_pt_regs(target)->syscallno;
703 	int ret;
704 
705 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &syscallno, 0, -1);
706 	if (ret)
707 		return ret;
708 
709 	task_pt_regs(target)->syscallno = syscallno;
710 	return ret;
711 }
712 
713 #ifdef CONFIG_ARM64_SVE
714 
sve_init_header_from_task(struct user_sve_header * header,struct task_struct * target)715 static void sve_init_header_from_task(struct user_sve_header *header,
716 				      struct task_struct *target)
717 {
718 	unsigned int vq;
719 
720 	memset(header, 0, sizeof(*header));
721 
722 	header->flags = test_tsk_thread_flag(target, TIF_SVE) ?
723 		SVE_PT_REGS_SVE : SVE_PT_REGS_FPSIMD;
724 	if (test_tsk_thread_flag(target, TIF_SVE_VL_INHERIT))
725 		header->flags |= SVE_PT_VL_INHERIT;
726 
727 	header->vl = target->thread.sve_vl;
728 	vq = sve_vq_from_vl(header->vl);
729 
730 	header->max_vl = sve_max_vl;
731 	header->size = SVE_PT_SIZE(vq, header->flags);
732 	header->max_size = SVE_PT_SIZE(sve_vq_from_vl(header->max_vl),
733 				      SVE_PT_REGS_SVE);
734 }
735 
sve_size_from_header(struct user_sve_header const * header)736 static unsigned int sve_size_from_header(struct user_sve_header const *header)
737 {
738 	return ALIGN(header->size, SVE_VQ_BYTES);
739 }
740 
sve_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)741 static int sve_get(struct task_struct *target,
742 		   const struct user_regset *regset,
743 		   struct membuf to)
744 {
745 	struct user_sve_header header;
746 	unsigned int vq;
747 	unsigned long start, end;
748 
749 	if (!system_supports_sve())
750 		return -EINVAL;
751 
752 	/* Header */
753 	sve_init_header_from_task(&header, target);
754 	vq = sve_vq_from_vl(header.vl);
755 
756 	membuf_write(&to, &header, sizeof(header));
757 
758 	if (target == current)
759 		fpsimd_preserve_current_state();
760 
761 	/* Registers: FPSIMD-only case */
762 
763 	BUILD_BUG_ON(SVE_PT_FPSIMD_OFFSET != sizeof(header));
764 	if ((header.flags & SVE_PT_REGS_MASK) == SVE_PT_REGS_FPSIMD)
765 		return __fpr_get(target, regset, to);
766 
767 	/* Otherwise: full SVE case */
768 
769 	BUILD_BUG_ON(SVE_PT_SVE_OFFSET != sizeof(header));
770 	start = SVE_PT_SVE_OFFSET;
771 	end = SVE_PT_SVE_FFR_OFFSET(vq) + SVE_PT_SVE_FFR_SIZE(vq);
772 	membuf_write(&to, target->thread.sve_state, end - start);
773 
774 	start = end;
775 	end = SVE_PT_SVE_FPSR_OFFSET(vq);
776 	membuf_zero(&to, end - start);
777 
778 	/*
779 	 * Copy fpsr, and fpcr which must follow contiguously in
780 	 * struct fpsimd_state:
781 	 */
782 	start = end;
783 	end = SVE_PT_SVE_FPCR_OFFSET(vq) + SVE_PT_SVE_FPCR_SIZE;
784 	membuf_write(&to, &target->thread.uw.fpsimd_state.fpsr, end - start);
785 
786 	start = end;
787 	end = sve_size_from_header(&header);
788 	return membuf_zero(&to, end - start);
789 }
790 
sve_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)791 static int sve_set(struct task_struct *target,
792 		   const struct user_regset *regset,
793 		   unsigned int pos, unsigned int count,
794 		   const void *kbuf, const void __user *ubuf)
795 {
796 	int ret;
797 	struct user_sve_header header;
798 	unsigned int vq;
799 	unsigned long start, end;
800 
801 	if (!system_supports_sve())
802 		return -EINVAL;
803 
804 	/* Header */
805 	if (count < sizeof(header))
806 		return -EINVAL;
807 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &header,
808 				 0, sizeof(header));
809 	if (ret)
810 		goto out;
811 
812 	/*
813 	 * Apart from SVE_PT_REGS_MASK, all SVE_PT_* flags are consumed by
814 	 * sve_set_vector_length(), which will also validate them for us:
815 	 */
816 	ret = sve_set_vector_length(target, header.vl,
817 		((unsigned long)header.flags & ~SVE_PT_REGS_MASK) << 16);
818 	if (ret)
819 		goto out;
820 
821 	/* Actual VL set may be less than the user asked for: */
822 	vq = sve_vq_from_vl(target->thread.sve_vl);
823 
824 	/* Registers: FPSIMD-only case */
825 
826 	BUILD_BUG_ON(SVE_PT_FPSIMD_OFFSET != sizeof(header));
827 	if ((header.flags & SVE_PT_REGS_MASK) == SVE_PT_REGS_FPSIMD) {
828 		ret = __fpr_set(target, regset, pos, count, kbuf, ubuf,
829 				SVE_PT_FPSIMD_OFFSET);
830 		clear_tsk_thread_flag(target, TIF_SVE);
831 		goto out;
832 	}
833 
834 	/* Otherwise: full SVE case */
835 
836 	/*
837 	 * If setting a different VL from the requested VL and there is
838 	 * register data, the data layout will be wrong: don't even
839 	 * try to set the registers in this case.
840 	 */
841 	if (count && vq != sve_vq_from_vl(header.vl)) {
842 		ret = -EIO;
843 		goto out;
844 	}
845 
846 	sve_alloc(target);
847 
848 	/*
849 	 * Ensure target->thread.sve_state is up to date with target's
850 	 * FPSIMD regs, so that a short copyin leaves trailing registers
851 	 * unmodified.
852 	 */
853 	fpsimd_sync_to_sve(target);
854 	set_tsk_thread_flag(target, TIF_SVE);
855 
856 	BUILD_BUG_ON(SVE_PT_SVE_OFFSET != sizeof(header));
857 	start = SVE_PT_SVE_OFFSET;
858 	end = SVE_PT_SVE_FFR_OFFSET(vq) + SVE_PT_SVE_FFR_SIZE(vq);
859 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
860 				 target->thread.sve_state,
861 				 start, end);
862 	if (ret)
863 		goto out;
864 
865 	start = end;
866 	end = SVE_PT_SVE_FPSR_OFFSET(vq);
867 	ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
868 					start, end);
869 	if (ret)
870 		goto out;
871 
872 	/*
873 	 * Copy fpsr, and fpcr which must follow contiguously in
874 	 * struct fpsimd_state:
875 	 */
876 	start = end;
877 	end = SVE_PT_SVE_FPCR_OFFSET(vq) + SVE_PT_SVE_FPCR_SIZE;
878 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
879 				 &target->thread.uw.fpsimd_state.fpsr,
880 				 start, end);
881 
882 out:
883 	fpsimd_flush_task_state(target);
884 	return ret;
885 }
886 
887 #endif /* CONFIG_ARM64_SVE */
888 
889 #ifdef CONFIG_ARM64_PTR_AUTH
pac_mask_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)890 static int pac_mask_get(struct task_struct *target,
891 			const struct user_regset *regset,
892 			struct membuf to)
893 {
894 	/*
895 	 * The PAC bits can differ across data and instruction pointers
896 	 * depending on TCR_EL1.TBID*, which we may make use of in future, so
897 	 * we expose separate masks.
898 	 */
899 	unsigned long mask = ptrauth_user_pac_mask();
900 	struct user_pac_mask uregs = {
901 		.data_mask = mask,
902 		.insn_mask = mask,
903 	};
904 
905 	if (!system_supports_address_auth())
906 		return -EINVAL;
907 
908 	return membuf_write(&to, &uregs, sizeof(uregs));
909 }
910 
pac_enabled_keys_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)911 static int pac_enabled_keys_get(struct task_struct *target,
912 				const struct user_regset *regset,
913 				struct membuf to)
914 {
915 	long enabled_keys = ptrauth_get_enabled_keys(target);
916 
917 	if (IS_ERR_VALUE(enabled_keys))
918 		return enabled_keys;
919 
920 	return membuf_write(&to, &enabled_keys, sizeof(enabled_keys));
921 }
922 
pac_enabled_keys_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)923 static int pac_enabled_keys_set(struct task_struct *target,
924 				const struct user_regset *regset,
925 				unsigned int pos, unsigned int count,
926 				const void *kbuf, const void __user *ubuf)
927 {
928 	int ret;
929 	long enabled_keys = ptrauth_get_enabled_keys(target);
930 
931 	if (IS_ERR_VALUE(enabled_keys))
932 		return enabled_keys;
933 
934 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &enabled_keys, 0,
935 				 sizeof(long));
936 	if (ret)
937 		return ret;
938 
939 	return ptrauth_set_enabled_keys(target, PR_PAC_ENABLED_KEYS_MASK,
940 					enabled_keys);
941 }
942 
943 #ifdef CONFIG_CHECKPOINT_RESTORE
pac_key_to_user(const struct ptrauth_key * key)944 static __uint128_t pac_key_to_user(const struct ptrauth_key *key)
945 {
946 	return (__uint128_t)key->hi << 64 | key->lo;
947 }
948 
pac_key_from_user(__uint128_t ukey)949 static struct ptrauth_key pac_key_from_user(__uint128_t ukey)
950 {
951 	struct ptrauth_key key = {
952 		.lo = (unsigned long)ukey,
953 		.hi = (unsigned long)(ukey >> 64),
954 	};
955 
956 	return key;
957 }
958 
pac_address_keys_to_user(struct user_pac_address_keys * ukeys,const struct ptrauth_keys_user * keys)959 static void pac_address_keys_to_user(struct user_pac_address_keys *ukeys,
960 				     const struct ptrauth_keys_user *keys)
961 {
962 	ukeys->apiakey = pac_key_to_user(&keys->apia);
963 	ukeys->apibkey = pac_key_to_user(&keys->apib);
964 	ukeys->apdakey = pac_key_to_user(&keys->apda);
965 	ukeys->apdbkey = pac_key_to_user(&keys->apdb);
966 }
967 
pac_address_keys_from_user(struct ptrauth_keys_user * keys,const struct user_pac_address_keys * ukeys)968 static void pac_address_keys_from_user(struct ptrauth_keys_user *keys,
969 				       const struct user_pac_address_keys *ukeys)
970 {
971 	keys->apia = pac_key_from_user(ukeys->apiakey);
972 	keys->apib = pac_key_from_user(ukeys->apibkey);
973 	keys->apda = pac_key_from_user(ukeys->apdakey);
974 	keys->apdb = pac_key_from_user(ukeys->apdbkey);
975 }
976 
pac_address_keys_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)977 static int pac_address_keys_get(struct task_struct *target,
978 				const struct user_regset *regset,
979 				struct membuf to)
980 {
981 	struct ptrauth_keys_user *keys = &target->thread.keys_user;
982 	struct user_pac_address_keys user_keys;
983 
984 	if (!system_supports_address_auth())
985 		return -EINVAL;
986 
987 	pac_address_keys_to_user(&user_keys, keys);
988 
989 	return membuf_write(&to, &user_keys, sizeof(user_keys));
990 }
991 
pac_address_keys_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)992 static int pac_address_keys_set(struct task_struct *target,
993 				const struct user_regset *regset,
994 				unsigned int pos, unsigned int count,
995 				const void *kbuf, const void __user *ubuf)
996 {
997 	struct ptrauth_keys_user *keys = &target->thread.keys_user;
998 	struct user_pac_address_keys user_keys;
999 	int ret;
1000 
1001 	if (!system_supports_address_auth())
1002 		return -EINVAL;
1003 
1004 	pac_address_keys_to_user(&user_keys, keys);
1005 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
1006 				 &user_keys, 0, -1);
1007 	if (ret)
1008 		return ret;
1009 	pac_address_keys_from_user(keys, &user_keys);
1010 
1011 	return 0;
1012 }
1013 
pac_generic_keys_to_user(struct user_pac_generic_keys * ukeys,const struct ptrauth_keys_user * keys)1014 static void pac_generic_keys_to_user(struct user_pac_generic_keys *ukeys,
1015 				     const struct ptrauth_keys_user *keys)
1016 {
1017 	ukeys->apgakey = pac_key_to_user(&keys->apga);
1018 }
1019 
pac_generic_keys_from_user(struct ptrauth_keys_user * keys,const struct user_pac_generic_keys * ukeys)1020 static void pac_generic_keys_from_user(struct ptrauth_keys_user *keys,
1021 				       const struct user_pac_generic_keys *ukeys)
1022 {
1023 	keys->apga = pac_key_from_user(ukeys->apgakey);
1024 }
1025 
pac_generic_keys_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)1026 static int pac_generic_keys_get(struct task_struct *target,
1027 				const struct user_regset *regset,
1028 				struct membuf to)
1029 {
1030 	struct ptrauth_keys_user *keys = &target->thread.keys_user;
1031 	struct user_pac_generic_keys user_keys;
1032 
1033 	if (!system_supports_generic_auth())
1034 		return -EINVAL;
1035 
1036 	pac_generic_keys_to_user(&user_keys, keys);
1037 
1038 	return membuf_write(&to, &user_keys, sizeof(user_keys));
1039 }
1040 
pac_generic_keys_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)1041 static int pac_generic_keys_set(struct task_struct *target,
1042 				const struct user_regset *regset,
1043 				unsigned int pos, unsigned int count,
1044 				const void *kbuf, const void __user *ubuf)
1045 {
1046 	struct ptrauth_keys_user *keys = &target->thread.keys_user;
1047 	struct user_pac_generic_keys user_keys;
1048 	int ret;
1049 
1050 	if (!system_supports_generic_auth())
1051 		return -EINVAL;
1052 
1053 	pac_generic_keys_to_user(&user_keys, keys);
1054 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
1055 				 &user_keys, 0, -1);
1056 	if (ret)
1057 		return ret;
1058 	pac_generic_keys_from_user(keys, &user_keys);
1059 
1060 	return 0;
1061 }
1062 #endif /* CONFIG_CHECKPOINT_RESTORE */
1063 #endif /* CONFIG_ARM64_PTR_AUTH */
1064 
1065 #ifdef CONFIG_ARM64_TAGGED_ADDR_ABI
tagged_addr_ctrl_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)1066 static int tagged_addr_ctrl_get(struct task_struct *target,
1067 				const struct user_regset *regset,
1068 				struct membuf to)
1069 {
1070 	long ctrl = get_tagged_addr_ctrl(target);
1071 
1072 	if (IS_ERR_VALUE(ctrl))
1073 		return ctrl;
1074 
1075 	return membuf_write(&to, &ctrl, sizeof(ctrl));
1076 }
1077 
tagged_addr_ctrl_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)1078 static int tagged_addr_ctrl_set(struct task_struct *target, const struct
1079 				user_regset *regset, unsigned int pos,
1080 				unsigned int count, const void *kbuf, const
1081 				void __user *ubuf)
1082 {
1083 	int ret;
1084 	long ctrl;
1085 
1086 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ctrl, 0, -1);
1087 	if (ret)
1088 		return ret;
1089 
1090 	return set_tagged_addr_ctrl(target, ctrl);
1091 }
1092 #endif
1093 
1094 enum aarch64_regset {
1095 	REGSET_GPR,
1096 	REGSET_FPR,
1097 	REGSET_TLS,
1098 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1099 	REGSET_HW_BREAK,
1100 	REGSET_HW_WATCH,
1101 #endif
1102 	REGSET_SYSTEM_CALL,
1103 #ifdef CONFIG_ARM64_SVE
1104 	REGSET_SVE,
1105 #endif
1106 #ifdef CONFIG_ARM64_PTR_AUTH
1107 	REGSET_PAC_MASK,
1108 	REGSET_PAC_ENABLED_KEYS,
1109 #ifdef CONFIG_CHECKPOINT_RESTORE
1110 	REGSET_PACA_KEYS,
1111 	REGSET_PACG_KEYS,
1112 #endif
1113 #endif
1114 #ifdef CONFIG_ARM64_TAGGED_ADDR_ABI
1115 	REGSET_TAGGED_ADDR_CTRL,
1116 #endif
1117 };
1118 
1119 static const struct user_regset aarch64_regsets[] = {
1120 	[REGSET_GPR] = {
1121 		.core_note_type = NT_PRSTATUS,
1122 		.n = sizeof(struct user_pt_regs) / sizeof(u64),
1123 		.size = sizeof(u64),
1124 		.align = sizeof(u64),
1125 		.regset_get = gpr_get,
1126 		.set = gpr_set
1127 	},
1128 	[REGSET_FPR] = {
1129 		.core_note_type = NT_PRFPREG,
1130 		.n = sizeof(struct user_fpsimd_state) / sizeof(u32),
1131 		/*
1132 		 * We pretend we have 32-bit registers because the fpsr and
1133 		 * fpcr are 32-bits wide.
1134 		 */
1135 		.size = sizeof(u32),
1136 		.align = sizeof(u32),
1137 		.active = fpr_active,
1138 		.regset_get = fpr_get,
1139 		.set = fpr_set
1140 	},
1141 	[REGSET_TLS] = {
1142 		.core_note_type = NT_ARM_TLS,
1143 		.n = 1,
1144 		.size = sizeof(void *),
1145 		.align = sizeof(void *),
1146 		.regset_get = tls_get,
1147 		.set = tls_set,
1148 	},
1149 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1150 	[REGSET_HW_BREAK] = {
1151 		.core_note_type = NT_ARM_HW_BREAK,
1152 		.n = sizeof(struct user_hwdebug_state) / sizeof(u32),
1153 		.size = sizeof(u32),
1154 		.align = sizeof(u32),
1155 		.regset_get = hw_break_get,
1156 		.set = hw_break_set,
1157 	},
1158 	[REGSET_HW_WATCH] = {
1159 		.core_note_type = NT_ARM_HW_WATCH,
1160 		.n = sizeof(struct user_hwdebug_state) / sizeof(u32),
1161 		.size = sizeof(u32),
1162 		.align = sizeof(u32),
1163 		.regset_get = hw_break_get,
1164 		.set = hw_break_set,
1165 	},
1166 #endif
1167 	[REGSET_SYSTEM_CALL] = {
1168 		.core_note_type = NT_ARM_SYSTEM_CALL,
1169 		.n = 1,
1170 		.size = sizeof(int),
1171 		.align = sizeof(int),
1172 		.regset_get = system_call_get,
1173 		.set = system_call_set,
1174 	},
1175 #ifdef CONFIG_ARM64_SVE
1176 	[REGSET_SVE] = { /* Scalable Vector Extension */
1177 		.core_note_type = NT_ARM_SVE,
1178 		.n = DIV_ROUND_UP(SVE_PT_SIZE(SVE_VQ_MAX, SVE_PT_REGS_SVE),
1179 				  SVE_VQ_BYTES),
1180 		.size = SVE_VQ_BYTES,
1181 		.align = SVE_VQ_BYTES,
1182 		.regset_get = sve_get,
1183 		.set = sve_set,
1184 	},
1185 #endif
1186 #ifdef CONFIG_ARM64_PTR_AUTH
1187 	[REGSET_PAC_MASK] = {
1188 		.core_note_type = NT_ARM_PAC_MASK,
1189 		.n = sizeof(struct user_pac_mask) / sizeof(u64),
1190 		.size = sizeof(u64),
1191 		.align = sizeof(u64),
1192 		.regset_get = pac_mask_get,
1193 		/* this cannot be set dynamically */
1194 	},
1195 	[REGSET_PAC_ENABLED_KEYS] = {
1196 		.core_note_type = NT_ARM_PAC_ENABLED_KEYS,
1197 		.n = 1,
1198 		.size = sizeof(long),
1199 		.align = sizeof(long),
1200 		.regset_get = pac_enabled_keys_get,
1201 		.set = pac_enabled_keys_set,
1202 	},
1203 #ifdef CONFIG_CHECKPOINT_RESTORE
1204 	[REGSET_PACA_KEYS] = {
1205 		.core_note_type = NT_ARM_PACA_KEYS,
1206 		.n = sizeof(struct user_pac_address_keys) / sizeof(__uint128_t),
1207 		.size = sizeof(__uint128_t),
1208 		.align = sizeof(__uint128_t),
1209 		.regset_get = pac_address_keys_get,
1210 		.set = pac_address_keys_set,
1211 	},
1212 	[REGSET_PACG_KEYS] = {
1213 		.core_note_type = NT_ARM_PACG_KEYS,
1214 		.n = sizeof(struct user_pac_generic_keys) / sizeof(__uint128_t),
1215 		.size = sizeof(__uint128_t),
1216 		.align = sizeof(__uint128_t),
1217 		.regset_get = pac_generic_keys_get,
1218 		.set = pac_generic_keys_set,
1219 	},
1220 #endif
1221 #endif
1222 #ifdef CONFIG_ARM64_TAGGED_ADDR_ABI
1223 	[REGSET_TAGGED_ADDR_CTRL] = {
1224 		.core_note_type = NT_ARM_TAGGED_ADDR_CTRL,
1225 		.n = 1,
1226 		.size = sizeof(long),
1227 		.align = sizeof(long),
1228 		.regset_get = tagged_addr_ctrl_get,
1229 		.set = tagged_addr_ctrl_set,
1230 	},
1231 #endif
1232 };
1233 
1234 static const struct user_regset_view user_aarch64_view = {
1235 	.name = "aarch64", .e_machine = EM_AARCH64,
1236 	.regsets = aarch64_regsets, .n = ARRAY_SIZE(aarch64_regsets)
1237 };
1238 
1239 #ifdef CONFIG_COMPAT
1240 enum compat_regset {
1241 	REGSET_COMPAT_GPR,
1242 	REGSET_COMPAT_VFP,
1243 };
1244 
compat_get_user_reg(struct task_struct * task,int idx)1245 static inline compat_ulong_t compat_get_user_reg(struct task_struct *task, int idx)
1246 {
1247 	struct pt_regs *regs = task_pt_regs(task);
1248 
1249 	switch (idx) {
1250 	case 15:
1251 		return regs->pc;
1252 	case 16:
1253 		return pstate_to_compat_psr(regs->pstate);
1254 	case 17:
1255 		return regs->orig_x0;
1256 	default:
1257 		return regs->regs[idx];
1258 	}
1259 }
1260 
compat_gpr_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)1261 static int compat_gpr_get(struct task_struct *target,
1262 			  const struct user_regset *regset,
1263 			  struct membuf to)
1264 {
1265 	int i = 0;
1266 
1267 	while (to.left)
1268 		membuf_store(&to, compat_get_user_reg(target, i++));
1269 	return 0;
1270 }
1271 
compat_gpr_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)1272 static int compat_gpr_set(struct task_struct *target,
1273 			  const struct user_regset *regset,
1274 			  unsigned int pos, unsigned int count,
1275 			  const void *kbuf, const void __user *ubuf)
1276 {
1277 	struct pt_regs newregs;
1278 	int ret = 0;
1279 	unsigned int i, start, num_regs;
1280 
1281 	/* Calculate the number of AArch32 registers contained in count */
1282 	num_regs = count / regset->size;
1283 
1284 	/* Convert pos into an register number */
1285 	start = pos / regset->size;
1286 
1287 	if (start + num_regs > regset->n)
1288 		return -EIO;
1289 
1290 	newregs = *task_pt_regs(target);
1291 
1292 	for (i = 0; i < num_regs; ++i) {
1293 		unsigned int idx = start + i;
1294 		compat_ulong_t reg;
1295 
1296 		if (kbuf) {
1297 			memcpy(&reg, kbuf, sizeof(reg));
1298 			kbuf += sizeof(reg);
1299 		} else {
1300 			ret = copy_from_user(&reg, ubuf, sizeof(reg));
1301 			if (ret) {
1302 				ret = -EFAULT;
1303 				break;
1304 			}
1305 
1306 			ubuf += sizeof(reg);
1307 		}
1308 
1309 		switch (idx) {
1310 		case 15:
1311 			newregs.pc = reg;
1312 			break;
1313 		case 16:
1314 			reg = compat_psr_to_pstate(reg);
1315 			newregs.pstate = reg;
1316 			break;
1317 		case 17:
1318 			newregs.orig_x0 = reg;
1319 			break;
1320 		default:
1321 			newregs.regs[idx] = reg;
1322 		}
1323 
1324 	}
1325 
1326 	if (valid_user_regs(&newregs.user_regs, target))
1327 		*task_pt_regs(target) = newregs;
1328 	else
1329 		ret = -EINVAL;
1330 
1331 	return ret;
1332 }
1333 
compat_vfp_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)1334 static int compat_vfp_get(struct task_struct *target,
1335 			  const struct user_regset *regset,
1336 			  struct membuf to)
1337 {
1338 	struct user_fpsimd_state *uregs;
1339 	compat_ulong_t fpscr;
1340 
1341 	if (!system_supports_fpsimd())
1342 		return -EINVAL;
1343 
1344 	uregs = &target->thread.uw.fpsimd_state;
1345 
1346 	if (target == current)
1347 		fpsimd_preserve_current_state();
1348 
1349 	/*
1350 	 * The VFP registers are packed into the fpsimd_state, so they all sit
1351 	 * nicely together for us. We just need to create the fpscr separately.
1352 	 */
1353 	membuf_write(&to, uregs, VFP_STATE_SIZE - sizeof(compat_ulong_t));
1354 	fpscr = (uregs->fpsr & VFP_FPSCR_STAT_MASK) |
1355 		(uregs->fpcr & VFP_FPSCR_CTRL_MASK);
1356 	return membuf_store(&to, fpscr);
1357 }
1358 
compat_vfp_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)1359 static int compat_vfp_set(struct task_struct *target,
1360 			  const struct user_regset *regset,
1361 			  unsigned int pos, unsigned int count,
1362 			  const void *kbuf, const void __user *ubuf)
1363 {
1364 	struct user_fpsimd_state *uregs;
1365 	compat_ulong_t fpscr;
1366 	int ret, vregs_end_pos;
1367 
1368 	if (!system_supports_fpsimd())
1369 		return -EINVAL;
1370 
1371 	uregs = &target->thread.uw.fpsimd_state;
1372 
1373 	vregs_end_pos = VFP_STATE_SIZE - sizeof(compat_ulong_t);
1374 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, uregs, 0,
1375 				 vregs_end_pos);
1376 
1377 	if (count && !ret) {
1378 		ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &fpscr,
1379 					 vregs_end_pos, VFP_STATE_SIZE);
1380 		if (!ret) {
1381 			uregs->fpsr = fpscr & VFP_FPSCR_STAT_MASK;
1382 			uregs->fpcr = fpscr & VFP_FPSCR_CTRL_MASK;
1383 		}
1384 	}
1385 
1386 	fpsimd_flush_task_state(target);
1387 	return ret;
1388 }
1389 
compat_tls_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)1390 static int compat_tls_get(struct task_struct *target,
1391 			  const struct user_regset *regset,
1392 			  struct membuf to)
1393 {
1394 	return membuf_store(&to, (compat_ulong_t)target->thread.uw.tp_value);
1395 }
1396 
compat_tls_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)1397 static int compat_tls_set(struct task_struct *target,
1398 			  const struct user_regset *regset, unsigned int pos,
1399 			  unsigned int count, const void *kbuf,
1400 			  const void __user *ubuf)
1401 {
1402 	int ret;
1403 	compat_ulong_t tls = target->thread.uw.tp_value;
1404 
1405 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
1406 	if (ret)
1407 		return ret;
1408 
1409 	target->thread.uw.tp_value = tls;
1410 	return ret;
1411 }
1412 
1413 static const struct user_regset aarch32_regsets[] = {
1414 	[REGSET_COMPAT_GPR] = {
1415 		.core_note_type = NT_PRSTATUS,
1416 		.n = COMPAT_ELF_NGREG,
1417 		.size = sizeof(compat_elf_greg_t),
1418 		.align = sizeof(compat_elf_greg_t),
1419 		.regset_get = compat_gpr_get,
1420 		.set = compat_gpr_set
1421 	},
1422 	[REGSET_COMPAT_VFP] = {
1423 		.core_note_type = NT_ARM_VFP,
1424 		.n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
1425 		.size = sizeof(compat_ulong_t),
1426 		.align = sizeof(compat_ulong_t),
1427 		.active = fpr_active,
1428 		.regset_get = compat_vfp_get,
1429 		.set = compat_vfp_set
1430 	},
1431 };
1432 
1433 static const struct user_regset_view user_aarch32_view = {
1434 	.name = "aarch32", .e_machine = EM_ARM,
1435 	.regsets = aarch32_regsets, .n = ARRAY_SIZE(aarch32_regsets)
1436 };
1437 
1438 static const struct user_regset aarch32_ptrace_regsets[] = {
1439 	[REGSET_GPR] = {
1440 		.core_note_type = NT_PRSTATUS,
1441 		.n = COMPAT_ELF_NGREG,
1442 		.size = sizeof(compat_elf_greg_t),
1443 		.align = sizeof(compat_elf_greg_t),
1444 		.regset_get = compat_gpr_get,
1445 		.set = compat_gpr_set
1446 	},
1447 	[REGSET_FPR] = {
1448 		.core_note_type = NT_ARM_VFP,
1449 		.n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
1450 		.size = sizeof(compat_ulong_t),
1451 		.align = sizeof(compat_ulong_t),
1452 		.regset_get = compat_vfp_get,
1453 		.set = compat_vfp_set
1454 	},
1455 	[REGSET_TLS] = {
1456 		.core_note_type = NT_ARM_TLS,
1457 		.n = 1,
1458 		.size = sizeof(compat_ulong_t),
1459 		.align = sizeof(compat_ulong_t),
1460 		.regset_get = compat_tls_get,
1461 		.set = compat_tls_set,
1462 	},
1463 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1464 	[REGSET_HW_BREAK] = {
1465 		.core_note_type = NT_ARM_HW_BREAK,
1466 		.n = sizeof(struct user_hwdebug_state) / sizeof(u32),
1467 		.size = sizeof(u32),
1468 		.align = sizeof(u32),
1469 		.regset_get = hw_break_get,
1470 		.set = hw_break_set,
1471 	},
1472 	[REGSET_HW_WATCH] = {
1473 		.core_note_type = NT_ARM_HW_WATCH,
1474 		.n = sizeof(struct user_hwdebug_state) / sizeof(u32),
1475 		.size = sizeof(u32),
1476 		.align = sizeof(u32),
1477 		.regset_get = hw_break_get,
1478 		.set = hw_break_set,
1479 	},
1480 #endif
1481 	[REGSET_SYSTEM_CALL] = {
1482 		.core_note_type = NT_ARM_SYSTEM_CALL,
1483 		.n = 1,
1484 		.size = sizeof(int),
1485 		.align = sizeof(int),
1486 		.regset_get = system_call_get,
1487 		.set = system_call_set,
1488 	},
1489 };
1490 
1491 static const struct user_regset_view user_aarch32_ptrace_view = {
1492 	.name = "aarch32", .e_machine = EM_ARM,
1493 	.regsets = aarch32_ptrace_regsets, .n = ARRAY_SIZE(aarch32_ptrace_regsets)
1494 };
1495 
compat_ptrace_read_user(struct task_struct * tsk,compat_ulong_t off,compat_ulong_t __user * ret)1496 static int compat_ptrace_read_user(struct task_struct *tsk, compat_ulong_t off,
1497 				   compat_ulong_t __user *ret)
1498 {
1499 	compat_ulong_t tmp;
1500 
1501 	if (off & 3)
1502 		return -EIO;
1503 
1504 	if (off == COMPAT_PT_TEXT_ADDR)
1505 		tmp = tsk->mm->start_code;
1506 	else if (off == COMPAT_PT_DATA_ADDR)
1507 		tmp = tsk->mm->start_data;
1508 	else if (off == COMPAT_PT_TEXT_END_ADDR)
1509 		tmp = tsk->mm->end_code;
1510 	else if (off < sizeof(compat_elf_gregset_t))
1511 		tmp = compat_get_user_reg(tsk, off >> 2);
1512 	else if (off >= COMPAT_USER_SZ)
1513 		return -EIO;
1514 	else
1515 		tmp = 0;
1516 
1517 	return put_user(tmp, ret);
1518 }
1519 
compat_ptrace_write_user(struct task_struct * tsk,compat_ulong_t off,compat_ulong_t val)1520 static int compat_ptrace_write_user(struct task_struct *tsk, compat_ulong_t off,
1521 				    compat_ulong_t val)
1522 {
1523 	struct pt_regs newregs = *task_pt_regs(tsk);
1524 	unsigned int idx = off / 4;
1525 
1526 	if (off & 3 || off >= COMPAT_USER_SZ)
1527 		return -EIO;
1528 
1529 	if (off >= sizeof(compat_elf_gregset_t))
1530 		return 0;
1531 
1532 	switch (idx) {
1533 	case 15:
1534 		newregs.pc = val;
1535 		break;
1536 	case 16:
1537 		newregs.pstate = compat_psr_to_pstate(val);
1538 		break;
1539 	case 17:
1540 		newregs.orig_x0 = val;
1541 		break;
1542 	default:
1543 		newregs.regs[idx] = val;
1544 	}
1545 
1546 	if (!valid_user_regs(&newregs.user_regs, tsk))
1547 		return -EINVAL;
1548 
1549 	*task_pt_regs(tsk) = newregs;
1550 	return 0;
1551 }
1552 
1553 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1554 
1555 /*
1556  * Convert a virtual register number into an index for a thread_info
1557  * breakpoint array. Breakpoints are identified using positive numbers
1558  * whilst watchpoints are negative. The registers are laid out as pairs
1559  * of (address, control), each pair mapping to a unique hw_breakpoint struct.
1560  * Register 0 is reserved for describing resource information.
1561  */
compat_ptrace_hbp_num_to_idx(compat_long_t num)1562 static int compat_ptrace_hbp_num_to_idx(compat_long_t num)
1563 {
1564 	return (abs(num) - 1) >> 1;
1565 }
1566 
compat_ptrace_hbp_get_resource_info(u32 * kdata)1567 static int compat_ptrace_hbp_get_resource_info(u32 *kdata)
1568 {
1569 	u8 num_brps, num_wrps, debug_arch, wp_len;
1570 	u32 reg = 0;
1571 
1572 	num_brps	= hw_breakpoint_slots(TYPE_INST);
1573 	num_wrps	= hw_breakpoint_slots(TYPE_DATA);
1574 
1575 	debug_arch	= debug_monitors_arch();
1576 	wp_len		= 8;
1577 	reg		|= debug_arch;
1578 	reg		<<= 8;
1579 	reg		|= wp_len;
1580 	reg		<<= 8;
1581 	reg		|= num_wrps;
1582 	reg		<<= 8;
1583 	reg		|= num_brps;
1584 
1585 	*kdata = reg;
1586 	return 0;
1587 }
1588 
compat_ptrace_hbp_get(unsigned int note_type,struct task_struct * tsk,compat_long_t num,u32 * kdata)1589 static int compat_ptrace_hbp_get(unsigned int note_type,
1590 				 struct task_struct *tsk,
1591 				 compat_long_t num,
1592 				 u32 *kdata)
1593 {
1594 	u64 addr = 0;
1595 	u32 ctrl = 0;
1596 
1597 	int err, idx = compat_ptrace_hbp_num_to_idx(num);
1598 
1599 	if (num & 1) {
1600 		err = ptrace_hbp_get_addr(note_type, tsk, idx, &addr);
1601 		*kdata = (u32)addr;
1602 	} else {
1603 		err = ptrace_hbp_get_ctrl(note_type, tsk, idx, &ctrl);
1604 		*kdata = ctrl;
1605 	}
1606 
1607 	return err;
1608 }
1609 
compat_ptrace_hbp_set(unsigned int note_type,struct task_struct * tsk,compat_long_t num,u32 * kdata)1610 static int compat_ptrace_hbp_set(unsigned int note_type,
1611 				 struct task_struct *tsk,
1612 				 compat_long_t num,
1613 				 u32 *kdata)
1614 {
1615 	u64 addr;
1616 	u32 ctrl;
1617 
1618 	int err, idx = compat_ptrace_hbp_num_to_idx(num);
1619 
1620 	if (num & 1) {
1621 		addr = *kdata;
1622 		err = ptrace_hbp_set_addr(note_type, tsk, idx, addr);
1623 	} else {
1624 		ctrl = *kdata;
1625 		err = ptrace_hbp_set_ctrl(note_type, tsk, idx, ctrl);
1626 	}
1627 
1628 	return err;
1629 }
1630 
compat_ptrace_gethbpregs(struct task_struct * tsk,compat_long_t num,compat_ulong_t __user * data)1631 static int compat_ptrace_gethbpregs(struct task_struct *tsk, compat_long_t num,
1632 				    compat_ulong_t __user *data)
1633 {
1634 	int ret;
1635 	u32 kdata;
1636 
1637 	/* Watchpoint */
1638 	if (num < 0) {
1639 		ret = compat_ptrace_hbp_get(NT_ARM_HW_WATCH, tsk, num, &kdata);
1640 	/* Resource info */
1641 	} else if (num == 0) {
1642 		ret = compat_ptrace_hbp_get_resource_info(&kdata);
1643 	/* Breakpoint */
1644 	} else {
1645 		ret = compat_ptrace_hbp_get(NT_ARM_HW_BREAK, tsk, num, &kdata);
1646 	}
1647 
1648 	if (!ret)
1649 		ret = put_user(kdata, data);
1650 
1651 	return ret;
1652 }
1653 
compat_ptrace_sethbpregs(struct task_struct * tsk,compat_long_t num,compat_ulong_t __user * data)1654 static int compat_ptrace_sethbpregs(struct task_struct *tsk, compat_long_t num,
1655 				    compat_ulong_t __user *data)
1656 {
1657 	int ret;
1658 	u32 kdata = 0;
1659 
1660 	if (num == 0)
1661 		return 0;
1662 
1663 	ret = get_user(kdata, data);
1664 	if (ret)
1665 		return ret;
1666 
1667 	if (num < 0)
1668 		ret = compat_ptrace_hbp_set(NT_ARM_HW_WATCH, tsk, num, &kdata);
1669 	else
1670 		ret = compat_ptrace_hbp_set(NT_ARM_HW_BREAK, tsk, num, &kdata);
1671 
1672 	return ret;
1673 }
1674 #endif	/* CONFIG_HAVE_HW_BREAKPOINT */
1675 
compat_arch_ptrace(struct task_struct * child,compat_long_t request,compat_ulong_t caddr,compat_ulong_t cdata)1676 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1677 			compat_ulong_t caddr, compat_ulong_t cdata)
1678 {
1679 	unsigned long addr = caddr;
1680 	unsigned long data = cdata;
1681 	void __user *datap = compat_ptr(data);
1682 	int ret;
1683 
1684 	switch (request) {
1685 		case PTRACE_PEEKUSR:
1686 			ret = compat_ptrace_read_user(child, addr, datap);
1687 			break;
1688 
1689 		case PTRACE_POKEUSR:
1690 			ret = compat_ptrace_write_user(child, addr, data);
1691 			break;
1692 
1693 		case COMPAT_PTRACE_GETREGS:
1694 			ret = copy_regset_to_user(child,
1695 						  &user_aarch32_view,
1696 						  REGSET_COMPAT_GPR,
1697 						  0, sizeof(compat_elf_gregset_t),
1698 						  datap);
1699 			break;
1700 
1701 		case COMPAT_PTRACE_SETREGS:
1702 			ret = copy_regset_from_user(child,
1703 						    &user_aarch32_view,
1704 						    REGSET_COMPAT_GPR,
1705 						    0, sizeof(compat_elf_gregset_t),
1706 						    datap);
1707 			break;
1708 
1709 		case COMPAT_PTRACE_GET_THREAD_AREA:
1710 			ret = put_user((compat_ulong_t)child->thread.uw.tp_value,
1711 				       (compat_ulong_t __user *)datap);
1712 			break;
1713 
1714 		case COMPAT_PTRACE_SET_SYSCALL:
1715 			task_pt_regs(child)->syscallno = data;
1716 			ret = 0;
1717 			break;
1718 
1719 		case COMPAT_PTRACE_GETVFPREGS:
1720 			ret = copy_regset_to_user(child,
1721 						  &user_aarch32_view,
1722 						  REGSET_COMPAT_VFP,
1723 						  0, VFP_STATE_SIZE,
1724 						  datap);
1725 			break;
1726 
1727 		case COMPAT_PTRACE_SETVFPREGS:
1728 			ret = copy_regset_from_user(child,
1729 						    &user_aarch32_view,
1730 						    REGSET_COMPAT_VFP,
1731 						    0, VFP_STATE_SIZE,
1732 						    datap);
1733 			break;
1734 
1735 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1736 		case COMPAT_PTRACE_GETHBPREGS:
1737 			ret = compat_ptrace_gethbpregs(child, addr, datap);
1738 			break;
1739 
1740 		case COMPAT_PTRACE_SETHBPREGS:
1741 			ret = compat_ptrace_sethbpregs(child, addr, datap);
1742 			break;
1743 #endif
1744 
1745 		default:
1746 			ret = compat_ptrace_request(child, request, addr,
1747 						    data);
1748 			break;
1749 	}
1750 
1751 	return ret;
1752 }
1753 #endif /* CONFIG_COMPAT */
1754 
task_user_regset_view(struct task_struct * task)1755 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1756 {
1757 #ifdef CONFIG_COMPAT
1758 	/*
1759 	 * Core dumping of 32-bit tasks or compat ptrace requests must use the
1760 	 * user_aarch32_view compatible with arm32. Native ptrace requests on
1761 	 * 32-bit children use an extended user_aarch32_ptrace_view to allow
1762 	 * access to the TLS register.
1763 	 */
1764 	if (is_compat_task())
1765 		return &user_aarch32_view;
1766 	else if (is_compat_thread(task_thread_info(task)))
1767 		return &user_aarch32_ptrace_view;
1768 #endif
1769 	return &user_aarch64_view;
1770 }
1771 
arch_ptrace(struct task_struct * child,long request,unsigned long addr,unsigned long data)1772 long arch_ptrace(struct task_struct *child, long request,
1773 		 unsigned long addr, unsigned long data)
1774 {
1775 	switch (request) {
1776 	case PTRACE_PEEKMTETAGS:
1777 	case PTRACE_POKEMTETAGS:
1778 		return mte_ptrace_copy_tags(child, request, addr, data);
1779 	}
1780 
1781 	return ptrace_request(child, request, addr, data);
1782 }
1783 
1784 enum ptrace_syscall_dir {
1785 	PTRACE_SYSCALL_ENTER = 0,
1786 	PTRACE_SYSCALL_EXIT,
1787 };
1788 
tracehook_report_syscall(struct pt_regs * regs,enum ptrace_syscall_dir dir)1789 static void tracehook_report_syscall(struct pt_regs *regs,
1790 				     enum ptrace_syscall_dir dir)
1791 {
1792 	int regno;
1793 	unsigned long saved_reg;
1794 
1795 	/*
1796 	 * We have some ABI weirdness here in the way that we handle syscall
1797 	 * exit stops because we indicate whether or not the stop has been
1798 	 * signalled from syscall entry or syscall exit by clobbering a general
1799 	 * purpose register (ip/r12 for AArch32, x7 for AArch64) in the tracee
1800 	 * and restoring its old value after the stop. This means that:
1801 	 *
1802 	 * - Any writes by the tracer to this register during the stop are
1803 	 *   ignored/discarded.
1804 	 *
1805 	 * - The actual value of the register is not available during the stop,
1806 	 *   so the tracer cannot save it and restore it later.
1807 	 *
1808 	 * - Syscall stops behave differently to seccomp and pseudo-step traps
1809 	 *   (the latter do not nobble any registers).
1810 	 */
1811 	regno = (is_compat_task() ? 12 : 7);
1812 	saved_reg = regs->regs[regno];
1813 	regs->regs[regno] = dir;
1814 
1815 	if (dir == PTRACE_SYSCALL_ENTER) {
1816 		if (tracehook_report_syscall_entry(regs))
1817 			forget_syscall(regs);
1818 		regs->regs[regno] = saved_reg;
1819 	} else if (!test_thread_flag(TIF_SINGLESTEP)) {
1820 		tracehook_report_syscall_exit(regs, 0);
1821 		regs->regs[regno] = saved_reg;
1822 	} else {
1823 		regs->regs[regno] = saved_reg;
1824 
1825 		/*
1826 		 * Signal a pseudo-step exception since we are stepping but
1827 		 * tracer modifications to the registers may have rewound the
1828 		 * state machine.
1829 		 */
1830 		tracehook_report_syscall_exit(regs, 1);
1831 	}
1832 }
1833 
syscall_trace_enter(struct pt_regs * regs)1834 int syscall_trace_enter(struct pt_regs *regs)
1835 {
1836 	unsigned long flags = READ_ONCE(current_thread_info()->flags);
1837 
1838 	if (flags & (_TIF_SYSCALL_EMU | _TIF_SYSCALL_TRACE)) {
1839 		tracehook_report_syscall(regs, PTRACE_SYSCALL_ENTER);
1840 		if (flags & _TIF_SYSCALL_EMU)
1841 			return NO_SYSCALL;
1842 	}
1843 
1844 	/* Do the secure computing after ptrace; failures should be fast. */
1845 	if (secure_computing() == -1)
1846 		return NO_SYSCALL;
1847 
1848 	if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
1849 		trace_sys_enter(regs, regs->syscallno);
1850 
1851 	audit_syscall_entry(regs->syscallno, regs->orig_x0, regs->regs[1],
1852 			    regs->regs[2], regs->regs[3]);
1853 
1854 	return regs->syscallno;
1855 }
1856 
syscall_trace_exit(struct pt_regs * regs)1857 void syscall_trace_exit(struct pt_regs *regs)
1858 {
1859 	unsigned long flags = READ_ONCE(current_thread_info()->flags);
1860 
1861 	audit_syscall_exit(regs);
1862 
1863 	if (flags & _TIF_SYSCALL_TRACEPOINT)
1864 		trace_sys_exit(regs, syscall_get_return_value(current, regs));
1865 
1866 	if (flags & (_TIF_SYSCALL_TRACE | _TIF_SINGLESTEP))
1867 		tracehook_report_syscall(regs, PTRACE_SYSCALL_EXIT);
1868 
1869 	rseq_syscall(regs);
1870 }
1871 
1872 /*
1873  * SPSR_ELx bits which are always architecturally RES0 per ARM DDI 0487D.a.
1874  * We permit userspace to set SSBS (AArch64 bit 12, AArch32 bit 23) which is
1875  * not described in ARM DDI 0487D.a.
1876  * We treat PAN and UAO as RES0 bits, as they are meaningless at EL0, and may
1877  * be allocated an EL0 meaning in future.
1878  * Userspace cannot use these until they have an architectural meaning.
1879  * Note that this follows the SPSR_ELx format, not the AArch32 PSR format.
1880  * We also reserve IL for the kernel; SS is handled dynamically.
1881  */
1882 #define SPSR_EL1_AARCH64_RES0_BITS \
1883 	(GENMASK_ULL(63, 32) | GENMASK_ULL(27, 26) | GENMASK_ULL(23, 22) | \
1884 	 GENMASK_ULL(20, 13) | GENMASK_ULL(5, 5))
1885 #define SPSR_EL1_AARCH32_RES0_BITS \
1886 	(GENMASK_ULL(63, 32) | GENMASK_ULL(22, 22) | GENMASK_ULL(20, 20))
1887 
valid_compat_regs(struct user_pt_regs * regs)1888 static int valid_compat_regs(struct user_pt_regs *regs)
1889 {
1890 	regs->pstate &= ~SPSR_EL1_AARCH32_RES0_BITS;
1891 
1892 	if (!system_supports_mixed_endian_el0()) {
1893 		if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
1894 			regs->pstate |= PSR_AA32_E_BIT;
1895 		else
1896 			regs->pstate &= ~PSR_AA32_E_BIT;
1897 	}
1898 
1899 	if (user_mode(regs) && (regs->pstate & PSR_MODE32_BIT) &&
1900 	    (regs->pstate & PSR_AA32_A_BIT) == 0 &&
1901 	    (regs->pstate & PSR_AA32_I_BIT) == 0 &&
1902 	    (regs->pstate & PSR_AA32_F_BIT) == 0) {
1903 		return 1;
1904 	}
1905 
1906 	/*
1907 	 * Force PSR to a valid 32-bit EL0t, preserving the same bits as
1908 	 * arch/arm.
1909 	 */
1910 	regs->pstate &= PSR_AA32_N_BIT | PSR_AA32_Z_BIT |
1911 			PSR_AA32_C_BIT | PSR_AA32_V_BIT |
1912 			PSR_AA32_Q_BIT | PSR_AA32_IT_MASK |
1913 			PSR_AA32_GE_MASK | PSR_AA32_E_BIT |
1914 			PSR_AA32_T_BIT;
1915 	regs->pstate |= PSR_MODE32_BIT;
1916 
1917 	return 0;
1918 }
1919 
valid_native_regs(struct user_pt_regs * regs)1920 static int valid_native_regs(struct user_pt_regs *regs)
1921 {
1922 	regs->pstate &= ~SPSR_EL1_AARCH64_RES0_BITS;
1923 
1924 	if (user_mode(regs) && !(regs->pstate & PSR_MODE32_BIT) &&
1925 	    (regs->pstate & PSR_D_BIT) == 0 &&
1926 	    (regs->pstate & PSR_A_BIT) == 0 &&
1927 	    (regs->pstate & PSR_I_BIT) == 0 &&
1928 	    (regs->pstate & PSR_F_BIT) == 0) {
1929 		return 1;
1930 	}
1931 
1932 	/* Force PSR to a valid 64-bit EL0t */
1933 	regs->pstate &= PSR_N_BIT | PSR_Z_BIT | PSR_C_BIT | PSR_V_BIT;
1934 
1935 	return 0;
1936 }
1937 
1938 /*
1939  * Are the current registers suitable for user mode? (used to maintain
1940  * security in signal handlers)
1941  */
valid_user_regs(struct user_pt_regs * regs,struct task_struct * task)1942 int valid_user_regs(struct user_pt_regs *regs, struct task_struct *task)
1943 {
1944 	/* https://lore.kernel.org/lkml/20191118131525.GA4180@willie-the-truck */
1945 	user_regs_reset_single_step(regs, task);
1946 
1947 	if (is_compat_thread(task_thread_info(task)))
1948 		return valid_compat_regs(regs);
1949 	else
1950 		return valid_native_regs(regs);
1951 }
1952