1 /*
2 * Based on arch/arm/kernel/ptrace.c
3 *
4 * By Ross Biro 1/23/92
5 * edited by Linus Torvalds
6 * ARM modifications Copyright (C) 2000 Russell King
7 * Copyright (C) 2012 ARM Ltd.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include <linux/audit.h>
23 #include <linux/compat.h>
24 #include <linux/kernel.h>
25 #include <linux/sched/signal.h>
26 #include <linux/sched/task_stack.h>
27 #include <linux/mm.h>
28 #include <linux/nospec.h>
29 #include <linux/smp.h>
30 #include <linux/ptrace.h>
31 #include <linux/user.h>
32 #include <linux/seccomp.h>
33 #include <linux/security.h>
34 #include <linux/init.h>
35 #include <linux/signal.h>
36 #include <linux/uaccess.h>
37 #include <linux/perf_event.h>
38 #include <linux/hw_breakpoint.h>
39 #include <linux/regset.h>
40 #include <linux/tracehook.h>
41 #include <linux/elf.h>
42
43 #include <asm/compat.h>
44 #include <asm/debug-monitors.h>
45 #include <asm/pgtable.h>
46 #include <asm/stacktrace.h>
47 #include <asm/syscall.h>
48 #include <asm/traps.h>
49 #include <asm/system_misc.h>
50
51 #define CREATE_TRACE_POINTS
52 #include <trace/events/syscalls.h>
53
54 struct pt_regs_offset {
55 const char *name;
56 int offset;
57 };
58
59 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
60 #define REG_OFFSET_END {.name = NULL, .offset = 0}
61 #define GPR_OFFSET_NAME(r) \
62 {.name = "x" #r, .offset = offsetof(struct pt_regs, regs[r])}
63
64 static const struct pt_regs_offset regoffset_table[] = {
65 GPR_OFFSET_NAME(0),
66 GPR_OFFSET_NAME(1),
67 GPR_OFFSET_NAME(2),
68 GPR_OFFSET_NAME(3),
69 GPR_OFFSET_NAME(4),
70 GPR_OFFSET_NAME(5),
71 GPR_OFFSET_NAME(6),
72 GPR_OFFSET_NAME(7),
73 GPR_OFFSET_NAME(8),
74 GPR_OFFSET_NAME(9),
75 GPR_OFFSET_NAME(10),
76 GPR_OFFSET_NAME(11),
77 GPR_OFFSET_NAME(12),
78 GPR_OFFSET_NAME(13),
79 GPR_OFFSET_NAME(14),
80 GPR_OFFSET_NAME(15),
81 GPR_OFFSET_NAME(16),
82 GPR_OFFSET_NAME(17),
83 GPR_OFFSET_NAME(18),
84 GPR_OFFSET_NAME(19),
85 GPR_OFFSET_NAME(20),
86 GPR_OFFSET_NAME(21),
87 GPR_OFFSET_NAME(22),
88 GPR_OFFSET_NAME(23),
89 GPR_OFFSET_NAME(24),
90 GPR_OFFSET_NAME(25),
91 GPR_OFFSET_NAME(26),
92 GPR_OFFSET_NAME(27),
93 GPR_OFFSET_NAME(28),
94 GPR_OFFSET_NAME(29),
95 GPR_OFFSET_NAME(30),
96 {.name = "lr", .offset = offsetof(struct pt_regs, regs[30])},
97 REG_OFFSET_NAME(sp),
98 REG_OFFSET_NAME(pc),
99 REG_OFFSET_NAME(pstate),
100 REG_OFFSET_END,
101 };
102
103 /**
104 * regs_query_register_offset() - query register offset from its name
105 * @name: the name of a register
106 *
107 * regs_query_register_offset() returns the offset of a register in struct
108 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
109 */
regs_query_register_offset(const char * name)110 int regs_query_register_offset(const char *name)
111 {
112 const struct pt_regs_offset *roff;
113
114 for (roff = regoffset_table; roff->name != NULL; roff++)
115 if (!strcmp(roff->name, name))
116 return roff->offset;
117 return -EINVAL;
118 }
119
120 /**
121 * regs_within_kernel_stack() - check the address in the stack
122 * @regs: pt_regs which contains kernel stack pointer.
123 * @addr: address which is checked.
124 *
125 * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
126 * If @addr is within the kernel stack, it returns true. If not, returns false.
127 */
regs_within_kernel_stack(struct pt_regs * regs,unsigned long addr)128 static bool regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
129 {
130 return ((addr & ~(THREAD_SIZE - 1)) ==
131 (kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1))) ||
132 on_irq_stack(addr);
133 }
134
135 /**
136 * regs_get_kernel_stack_nth() - get Nth entry of the stack
137 * @regs: pt_regs which contains kernel stack pointer.
138 * @n: stack entry number.
139 *
140 * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
141 * is specified by @regs. If the @n th entry is NOT in the kernel stack,
142 * this returns 0.
143 */
regs_get_kernel_stack_nth(struct pt_regs * regs,unsigned int n)144 unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
145 {
146 unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
147
148 addr += n;
149 if (regs_within_kernel_stack(regs, (unsigned long)addr))
150 return *addr;
151 else
152 return 0;
153 }
154
155 /*
156 * TODO: does not yet catch signals sent when the child dies.
157 * in exit.c or in signal.c.
158 */
159
160 /*
161 * Called by kernel/ptrace.c when detaching..
162 */
ptrace_disable(struct task_struct * child)163 void ptrace_disable(struct task_struct *child)
164 {
165 /*
166 * This would be better off in core code, but PTRACE_DETACH has
167 * grown its fair share of arch-specific worts and changing it
168 * is likely to cause regressions on obscure architectures.
169 */
170 user_disable_single_step(child);
171 }
172
173 #ifdef CONFIG_HAVE_HW_BREAKPOINT
174 /*
175 * Handle hitting a HW-breakpoint.
176 */
ptrace_hbptriggered(struct perf_event * bp,struct perf_sample_data * data,struct pt_regs * regs)177 static void ptrace_hbptriggered(struct perf_event *bp,
178 struct perf_sample_data *data,
179 struct pt_regs *regs)
180 {
181 struct arch_hw_breakpoint *bkpt = counter_arch_bp(bp);
182 siginfo_t info = {
183 .si_signo = SIGTRAP,
184 .si_errno = 0,
185 .si_code = TRAP_HWBKPT,
186 .si_addr = (void __user *)(bkpt->trigger),
187 };
188
189 #ifdef CONFIG_COMPAT
190 int i;
191
192 if (!is_compat_task())
193 goto send_sig;
194
195 for (i = 0; i < ARM_MAX_BRP; ++i) {
196 if (current->thread.debug.hbp_break[i] == bp) {
197 info.si_errno = (i << 1) + 1;
198 break;
199 }
200 }
201
202 for (i = 0; i < ARM_MAX_WRP; ++i) {
203 if (current->thread.debug.hbp_watch[i] == bp) {
204 info.si_errno = -((i << 1) + 1);
205 break;
206 }
207 }
208
209 send_sig:
210 #endif
211 force_sig_info(SIGTRAP, &info, current);
212 }
213
214 /*
215 * Unregister breakpoints from this task and reset the pointers in
216 * the thread_struct.
217 */
flush_ptrace_hw_breakpoint(struct task_struct * tsk)218 void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
219 {
220 int i;
221 struct thread_struct *t = &tsk->thread;
222
223 for (i = 0; i < ARM_MAX_BRP; i++) {
224 if (t->debug.hbp_break[i]) {
225 unregister_hw_breakpoint(t->debug.hbp_break[i]);
226 t->debug.hbp_break[i] = NULL;
227 }
228 }
229
230 for (i = 0; i < ARM_MAX_WRP; i++) {
231 if (t->debug.hbp_watch[i]) {
232 unregister_hw_breakpoint(t->debug.hbp_watch[i]);
233 t->debug.hbp_watch[i] = NULL;
234 }
235 }
236 }
237
ptrace_hw_copy_thread(struct task_struct * tsk)238 void ptrace_hw_copy_thread(struct task_struct *tsk)
239 {
240 memset(&tsk->thread.debug, 0, sizeof(struct debug_info));
241 }
242
ptrace_hbp_get_event(unsigned int note_type,struct task_struct * tsk,unsigned long idx)243 static struct perf_event *ptrace_hbp_get_event(unsigned int note_type,
244 struct task_struct *tsk,
245 unsigned long idx)
246 {
247 struct perf_event *bp = ERR_PTR(-EINVAL);
248
249 switch (note_type) {
250 case NT_ARM_HW_BREAK:
251 if (idx >= ARM_MAX_BRP)
252 goto out;
253 idx = array_index_nospec(idx, ARM_MAX_BRP);
254 bp = tsk->thread.debug.hbp_break[idx];
255 break;
256 case NT_ARM_HW_WATCH:
257 if (idx >= ARM_MAX_WRP)
258 goto out;
259 idx = array_index_nospec(idx, ARM_MAX_WRP);
260 bp = tsk->thread.debug.hbp_watch[idx];
261 break;
262 }
263
264 out:
265 return bp;
266 }
267
ptrace_hbp_set_event(unsigned int note_type,struct task_struct * tsk,unsigned long idx,struct perf_event * bp)268 static int ptrace_hbp_set_event(unsigned int note_type,
269 struct task_struct *tsk,
270 unsigned long idx,
271 struct perf_event *bp)
272 {
273 int err = -EINVAL;
274
275 switch (note_type) {
276 case NT_ARM_HW_BREAK:
277 if (idx >= ARM_MAX_BRP)
278 goto out;
279 idx = array_index_nospec(idx, ARM_MAX_BRP);
280 tsk->thread.debug.hbp_break[idx] = bp;
281 err = 0;
282 break;
283 case NT_ARM_HW_WATCH:
284 if (idx >= ARM_MAX_WRP)
285 goto out;
286 idx = array_index_nospec(idx, ARM_MAX_WRP);
287 tsk->thread.debug.hbp_watch[idx] = bp;
288 err = 0;
289 break;
290 }
291
292 out:
293 return err;
294 }
295
ptrace_hbp_create(unsigned int note_type,struct task_struct * tsk,unsigned long idx)296 static struct perf_event *ptrace_hbp_create(unsigned int note_type,
297 struct task_struct *tsk,
298 unsigned long idx)
299 {
300 struct perf_event *bp;
301 struct perf_event_attr attr;
302 int err, type;
303
304 switch (note_type) {
305 case NT_ARM_HW_BREAK:
306 type = HW_BREAKPOINT_X;
307 break;
308 case NT_ARM_HW_WATCH:
309 type = HW_BREAKPOINT_RW;
310 break;
311 default:
312 return ERR_PTR(-EINVAL);
313 }
314
315 ptrace_breakpoint_init(&attr);
316
317 /*
318 * Initialise fields to sane defaults
319 * (i.e. values that will pass validation).
320 */
321 attr.bp_addr = 0;
322 attr.bp_len = HW_BREAKPOINT_LEN_4;
323 attr.bp_type = type;
324 attr.disabled = 1;
325
326 bp = register_user_hw_breakpoint(&attr, ptrace_hbptriggered, NULL, tsk);
327 if (IS_ERR(bp))
328 return bp;
329
330 err = ptrace_hbp_set_event(note_type, tsk, idx, bp);
331 if (err)
332 return ERR_PTR(err);
333
334 return bp;
335 }
336
ptrace_hbp_fill_attr_ctrl(unsigned int note_type,struct arch_hw_breakpoint_ctrl ctrl,struct perf_event_attr * attr)337 static int ptrace_hbp_fill_attr_ctrl(unsigned int note_type,
338 struct arch_hw_breakpoint_ctrl ctrl,
339 struct perf_event_attr *attr)
340 {
341 int err, len, type, offset, disabled = !ctrl.enabled;
342
343 attr->disabled = disabled;
344 if (disabled)
345 return 0;
346
347 err = arch_bp_generic_fields(ctrl, &len, &type, &offset);
348 if (err)
349 return err;
350
351 switch (note_type) {
352 case NT_ARM_HW_BREAK:
353 if ((type & HW_BREAKPOINT_X) != type)
354 return -EINVAL;
355 break;
356 case NT_ARM_HW_WATCH:
357 if ((type & HW_BREAKPOINT_RW) != type)
358 return -EINVAL;
359 break;
360 default:
361 return -EINVAL;
362 }
363
364 attr->bp_len = len;
365 attr->bp_type = type;
366 attr->bp_addr += offset;
367
368 return 0;
369 }
370
ptrace_hbp_get_resource_info(unsigned int note_type,u32 * info)371 static int ptrace_hbp_get_resource_info(unsigned int note_type, u32 *info)
372 {
373 u8 num;
374 u32 reg = 0;
375
376 switch (note_type) {
377 case NT_ARM_HW_BREAK:
378 num = hw_breakpoint_slots(TYPE_INST);
379 break;
380 case NT_ARM_HW_WATCH:
381 num = hw_breakpoint_slots(TYPE_DATA);
382 break;
383 default:
384 return -EINVAL;
385 }
386
387 reg |= debug_monitors_arch();
388 reg <<= 8;
389 reg |= num;
390
391 *info = reg;
392 return 0;
393 }
394
ptrace_hbp_get_ctrl(unsigned int note_type,struct task_struct * tsk,unsigned long idx,u32 * ctrl)395 static int ptrace_hbp_get_ctrl(unsigned int note_type,
396 struct task_struct *tsk,
397 unsigned long idx,
398 u32 *ctrl)
399 {
400 struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
401
402 if (IS_ERR(bp))
403 return PTR_ERR(bp);
404
405 *ctrl = bp ? encode_ctrl_reg(counter_arch_bp(bp)->ctrl) : 0;
406 return 0;
407 }
408
ptrace_hbp_get_addr(unsigned int note_type,struct task_struct * tsk,unsigned long idx,u64 * addr)409 static int ptrace_hbp_get_addr(unsigned int note_type,
410 struct task_struct *tsk,
411 unsigned long idx,
412 u64 *addr)
413 {
414 struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
415
416 if (IS_ERR(bp))
417 return PTR_ERR(bp);
418
419 *addr = bp ? counter_arch_bp(bp)->address : 0;
420 return 0;
421 }
422
ptrace_hbp_get_initialised_bp(unsigned int note_type,struct task_struct * tsk,unsigned long idx)423 static struct perf_event *ptrace_hbp_get_initialised_bp(unsigned int note_type,
424 struct task_struct *tsk,
425 unsigned long idx)
426 {
427 struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
428
429 if (!bp)
430 bp = ptrace_hbp_create(note_type, tsk, idx);
431
432 return bp;
433 }
434
ptrace_hbp_set_ctrl(unsigned int note_type,struct task_struct * tsk,unsigned long idx,u32 uctrl)435 static int ptrace_hbp_set_ctrl(unsigned int note_type,
436 struct task_struct *tsk,
437 unsigned long idx,
438 u32 uctrl)
439 {
440 int err;
441 struct perf_event *bp;
442 struct perf_event_attr attr;
443 struct arch_hw_breakpoint_ctrl ctrl;
444
445 bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
446 if (IS_ERR(bp)) {
447 err = PTR_ERR(bp);
448 return err;
449 }
450
451 attr = bp->attr;
452 decode_ctrl_reg(uctrl, &ctrl);
453 err = ptrace_hbp_fill_attr_ctrl(note_type, ctrl, &attr);
454 if (err)
455 return err;
456
457 return modify_user_hw_breakpoint(bp, &attr);
458 }
459
ptrace_hbp_set_addr(unsigned int note_type,struct task_struct * tsk,unsigned long idx,u64 addr)460 static int ptrace_hbp_set_addr(unsigned int note_type,
461 struct task_struct *tsk,
462 unsigned long idx,
463 u64 addr)
464 {
465 int err;
466 struct perf_event *bp;
467 struct perf_event_attr attr;
468
469 bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
470 if (IS_ERR(bp)) {
471 err = PTR_ERR(bp);
472 return err;
473 }
474
475 attr = bp->attr;
476 attr.bp_addr = addr;
477 err = modify_user_hw_breakpoint(bp, &attr);
478 return err;
479 }
480
481 #define PTRACE_HBP_ADDR_SZ sizeof(u64)
482 #define PTRACE_HBP_CTRL_SZ sizeof(u32)
483 #define PTRACE_HBP_PAD_SZ sizeof(u32)
484
hw_break_get(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,void * kbuf,void __user * ubuf)485 static int hw_break_get(struct task_struct *target,
486 const struct user_regset *regset,
487 unsigned int pos, unsigned int count,
488 void *kbuf, void __user *ubuf)
489 {
490 unsigned int note_type = regset->core_note_type;
491 int ret, idx = 0, offset, limit;
492 u32 info, ctrl;
493 u64 addr;
494
495 /* Resource info */
496 ret = ptrace_hbp_get_resource_info(note_type, &info);
497 if (ret)
498 return ret;
499
500 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &info, 0,
501 sizeof(info));
502 if (ret)
503 return ret;
504
505 /* Pad */
506 offset = offsetof(struct user_hwdebug_state, pad);
507 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf, offset,
508 offset + PTRACE_HBP_PAD_SZ);
509 if (ret)
510 return ret;
511
512 /* (address, ctrl) registers */
513 offset = offsetof(struct user_hwdebug_state, dbg_regs);
514 limit = regset->n * regset->size;
515 while (count && offset < limit) {
516 ret = ptrace_hbp_get_addr(note_type, target, idx, &addr);
517 if (ret)
518 return ret;
519 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &addr,
520 offset, offset + PTRACE_HBP_ADDR_SZ);
521 if (ret)
522 return ret;
523 offset += PTRACE_HBP_ADDR_SZ;
524
525 ret = ptrace_hbp_get_ctrl(note_type, target, idx, &ctrl);
526 if (ret)
527 return ret;
528 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &ctrl,
529 offset, offset + PTRACE_HBP_CTRL_SZ);
530 if (ret)
531 return ret;
532 offset += PTRACE_HBP_CTRL_SZ;
533
534 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
535 offset,
536 offset + PTRACE_HBP_PAD_SZ);
537 if (ret)
538 return ret;
539 offset += PTRACE_HBP_PAD_SZ;
540 idx++;
541 }
542
543 return 0;
544 }
545
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)546 static int hw_break_set(struct task_struct *target,
547 const struct user_regset *regset,
548 unsigned int pos, unsigned int count,
549 const void *kbuf, const void __user *ubuf)
550 {
551 unsigned int note_type = regset->core_note_type;
552 int ret, idx = 0, offset, limit;
553 u32 ctrl;
554 u64 addr;
555
556 /* Resource info and pad */
557 offset = offsetof(struct user_hwdebug_state, dbg_regs);
558 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, 0, offset);
559 if (ret)
560 return ret;
561
562 /* (address, ctrl) registers */
563 limit = regset->n * regset->size;
564 while (count && offset < limit) {
565 if (count < PTRACE_HBP_ADDR_SZ)
566 return -EINVAL;
567 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &addr,
568 offset, offset + PTRACE_HBP_ADDR_SZ);
569 if (ret)
570 return ret;
571 ret = ptrace_hbp_set_addr(note_type, target, idx, addr);
572 if (ret)
573 return ret;
574 offset += PTRACE_HBP_ADDR_SZ;
575
576 if (!count)
577 break;
578 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ctrl,
579 offset, offset + PTRACE_HBP_CTRL_SZ);
580 if (ret)
581 return ret;
582 ret = ptrace_hbp_set_ctrl(note_type, target, idx, ctrl);
583 if (ret)
584 return ret;
585 offset += PTRACE_HBP_CTRL_SZ;
586
587 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
588 offset,
589 offset + PTRACE_HBP_PAD_SZ);
590 if (ret)
591 return ret;
592 offset += PTRACE_HBP_PAD_SZ;
593 idx++;
594 }
595
596 return 0;
597 }
598 #endif /* CONFIG_HAVE_HW_BREAKPOINT */
599
gpr_get(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,void * kbuf,void __user * ubuf)600 static int gpr_get(struct task_struct *target,
601 const struct user_regset *regset,
602 unsigned int pos, unsigned int count,
603 void *kbuf, void __user *ubuf)
604 {
605 struct user_pt_regs *uregs = &task_pt_regs(target)->user_regs;
606 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0, -1);
607 }
608
gpr_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)609 static int gpr_set(struct task_struct *target, const struct user_regset *regset,
610 unsigned int pos, unsigned int count,
611 const void *kbuf, const void __user *ubuf)
612 {
613 int ret;
614 struct user_pt_regs newregs = task_pt_regs(target)->user_regs;
615
616 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newregs, 0, -1);
617 if (ret)
618 return ret;
619
620 if (!valid_user_regs(&newregs, target))
621 return -EINVAL;
622
623 task_pt_regs(target)->user_regs = newregs;
624 return 0;
625 }
626
fpr_active(struct task_struct * target,const struct user_regset * regset)627 static int fpr_active(struct task_struct *target, const struct user_regset *regset)
628 {
629 if (!system_supports_fpsimd())
630 return -ENODEV;
631 return regset->n;
632 }
633
634 /*
635 * TODO: update fp accessors for lazy context switching (sync/flush hwstate)
636 */
fpr_get(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,void * kbuf,void __user * ubuf)637 static int fpr_get(struct task_struct *target, const struct user_regset *regset,
638 unsigned int pos, unsigned int count,
639 void *kbuf, void __user *ubuf)
640 {
641 struct user_fpsimd_state *uregs;
642 uregs = &target->thread.fpsimd_state.user_fpsimd;
643
644 if (!system_supports_fpsimd())
645 return -EINVAL;
646
647 if (target == current)
648 fpsimd_preserve_current_state();
649
650 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0, -1);
651 }
652
fpr_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)653 static int fpr_set(struct task_struct *target, const struct user_regset *regset,
654 unsigned int pos, unsigned int count,
655 const void *kbuf, const void __user *ubuf)
656 {
657 int ret;
658 struct user_fpsimd_state newstate =
659 target->thread.fpsimd_state.user_fpsimd;
660
661 if (!system_supports_fpsimd())
662 return -EINVAL;
663
664 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newstate, 0, -1);
665 if (ret)
666 return ret;
667
668 target->thread.fpsimd_state.user_fpsimd = newstate;
669 fpsimd_flush_task_state(target);
670 return ret;
671 }
672
tls_get(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,void * kbuf,void __user * ubuf)673 static int tls_get(struct task_struct *target, const struct user_regset *regset,
674 unsigned int pos, unsigned int count,
675 void *kbuf, void __user *ubuf)
676 {
677 unsigned long *tls = &target->thread.tp_value;
678
679 if (target == current)
680 tls_preserve_current_state();
681
682 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, tls, 0, -1);
683 }
684
tls_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)685 static int tls_set(struct task_struct *target, const struct user_regset *regset,
686 unsigned int pos, unsigned int count,
687 const void *kbuf, const void __user *ubuf)
688 {
689 int ret;
690 unsigned long tls = target->thread.tp_value;
691
692 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
693 if (ret)
694 return ret;
695
696 target->thread.tp_value = tls;
697 return ret;
698 }
699
system_call_get(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,void * kbuf,void __user * ubuf)700 static int system_call_get(struct task_struct *target,
701 const struct user_regset *regset,
702 unsigned int pos, unsigned int count,
703 void *kbuf, void __user *ubuf)
704 {
705 int syscallno = task_pt_regs(target)->syscallno;
706
707 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
708 &syscallno, 0, -1);
709 }
710
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)711 static int system_call_set(struct task_struct *target,
712 const struct user_regset *regset,
713 unsigned int pos, unsigned int count,
714 const void *kbuf, const void __user *ubuf)
715 {
716 int syscallno = task_pt_regs(target)->syscallno;
717 int ret;
718
719 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &syscallno, 0, -1);
720 if (ret)
721 return ret;
722
723 task_pt_regs(target)->syscallno = syscallno;
724 return ret;
725 }
726
727 enum aarch64_regset {
728 REGSET_GPR,
729 REGSET_FPR,
730 REGSET_TLS,
731 #ifdef CONFIG_HAVE_HW_BREAKPOINT
732 REGSET_HW_BREAK,
733 REGSET_HW_WATCH,
734 #endif
735 REGSET_SYSTEM_CALL,
736 };
737
738 static const struct user_regset aarch64_regsets[] = {
739 [REGSET_GPR] = {
740 .core_note_type = NT_PRSTATUS,
741 .n = sizeof(struct user_pt_regs) / sizeof(u64),
742 .size = sizeof(u64),
743 .align = sizeof(u64),
744 .get = gpr_get,
745 .set = gpr_set
746 },
747 [REGSET_FPR] = {
748 .core_note_type = NT_PRFPREG,
749 .n = sizeof(struct user_fpsimd_state) / sizeof(u32),
750 /*
751 * We pretend we have 32-bit registers because the fpsr and
752 * fpcr are 32-bits wide.
753 */
754 .size = sizeof(u32),
755 .align = sizeof(u32),
756 .active = fpr_active,
757 .get = fpr_get,
758 .set = fpr_set
759 },
760 [REGSET_TLS] = {
761 .core_note_type = NT_ARM_TLS,
762 .n = 1,
763 .size = sizeof(void *),
764 .align = sizeof(void *),
765 .get = tls_get,
766 .set = tls_set,
767 },
768 #ifdef CONFIG_HAVE_HW_BREAKPOINT
769 [REGSET_HW_BREAK] = {
770 .core_note_type = NT_ARM_HW_BREAK,
771 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
772 .size = sizeof(u32),
773 .align = sizeof(u32),
774 .get = hw_break_get,
775 .set = hw_break_set,
776 },
777 [REGSET_HW_WATCH] = {
778 .core_note_type = NT_ARM_HW_WATCH,
779 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
780 .size = sizeof(u32),
781 .align = sizeof(u32),
782 .get = hw_break_get,
783 .set = hw_break_set,
784 },
785 #endif
786 [REGSET_SYSTEM_CALL] = {
787 .core_note_type = NT_ARM_SYSTEM_CALL,
788 .n = 1,
789 .size = sizeof(int),
790 .align = sizeof(int),
791 .get = system_call_get,
792 .set = system_call_set,
793 },
794 };
795
796 static const struct user_regset_view user_aarch64_view = {
797 .name = "aarch64", .e_machine = EM_AARCH64,
798 .regsets = aarch64_regsets, .n = ARRAY_SIZE(aarch64_regsets)
799 };
800
801 #ifdef CONFIG_COMPAT
802 #include <linux/compat.h>
803
804 enum compat_regset {
805 REGSET_COMPAT_GPR,
806 REGSET_COMPAT_VFP,
807 };
808
compat_gpr_get(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,void * kbuf,void __user * ubuf)809 static int compat_gpr_get(struct task_struct *target,
810 const struct user_regset *regset,
811 unsigned int pos, unsigned int count,
812 void *kbuf, void __user *ubuf)
813 {
814 int ret = 0;
815 unsigned int i, start, num_regs;
816
817 /* Calculate the number of AArch32 registers contained in count */
818 num_regs = count / regset->size;
819
820 /* Convert pos into an register number */
821 start = pos / regset->size;
822
823 if (start + num_regs > regset->n)
824 return -EIO;
825
826 for (i = 0; i < num_regs; ++i) {
827 unsigned int idx = start + i;
828 compat_ulong_t reg;
829
830 switch (idx) {
831 case 15:
832 reg = task_pt_regs(target)->pc;
833 break;
834 case 16:
835 reg = task_pt_regs(target)->pstate;
836 reg = pstate_to_compat_psr(reg);
837 break;
838 case 17:
839 reg = task_pt_regs(target)->orig_x0;
840 break;
841 default:
842 reg = task_pt_regs(target)->regs[idx];
843 }
844
845 if (kbuf) {
846 memcpy(kbuf, ®, sizeof(reg));
847 kbuf += sizeof(reg);
848 } else {
849 ret = copy_to_user(ubuf, ®, sizeof(reg));
850 if (ret) {
851 ret = -EFAULT;
852 break;
853 }
854
855 ubuf += sizeof(reg);
856 }
857 }
858
859 return ret;
860 }
861
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)862 static int compat_gpr_set(struct task_struct *target,
863 const struct user_regset *regset,
864 unsigned int pos, unsigned int count,
865 const void *kbuf, const void __user *ubuf)
866 {
867 struct pt_regs newregs;
868 int ret = 0;
869 unsigned int i, start, num_regs;
870
871 /* Calculate the number of AArch32 registers contained in count */
872 num_regs = count / regset->size;
873
874 /* Convert pos into an register number */
875 start = pos / regset->size;
876
877 if (start + num_regs > regset->n)
878 return -EIO;
879
880 newregs = *task_pt_regs(target);
881
882 for (i = 0; i < num_regs; ++i) {
883 unsigned int idx = start + i;
884 compat_ulong_t reg;
885
886 if (kbuf) {
887 memcpy(®, kbuf, sizeof(reg));
888 kbuf += sizeof(reg);
889 } else {
890 ret = copy_from_user(®, ubuf, sizeof(reg));
891 if (ret) {
892 ret = -EFAULT;
893 break;
894 }
895
896 ubuf += sizeof(reg);
897 }
898
899 switch (idx) {
900 case 15:
901 newregs.pc = reg;
902 break;
903 case 16:
904 reg = compat_psr_to_pstate(reg);
905 newregs.pstate = reg;
906 break;
907 case 17:
908 newregs.orig_x0 = reg;
909 break;
910 default:
911 newregs.regs[idx] = reg;
912 }
913
914 }
915
916 if (valid_user_regs(&newregs.user_regs, target))
917 *task_pt_regs(target) = newregs;
918 else
919 ret = -EINVAL;
920
921 return ret;
922 }
923
compat_vfp_get(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,void * kbuf,void __user * ubuf)924 static int compat_vfp_get(struct task_struct *target,
925 const struct user_regset *regset,
926 unsigned int pos, unsigned int count,
927 void *kbuf, void __user *ubuf)
928 {
929 struct user_fpsimd_state *uregs;
930 compat_ulong_t fpscr;
931 int ret, vregs_end_pos;
932
933 if (!system_supports_fpsimd())
934 return -EINVAL;
935
936 uregs = &target->thread.fpsimd_state.user_fpsimd;
937
938 if (target == current)
939 fpsimd_preserve_current_state();
940
941 /*
942 * The VFP registers are packed into the fpsimd_state, so they all sit
943 * nicely together for us. We just need to create the fpscr separately.
944 */
945 vregs_end_pos = VFP_STATE_SIZE - sizeof(compat_ulong_t);
946 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs,
947 0, vregs_end_pos);
948
949 if (count && !ret) {
950 fpscr = (uregs->fpsr & VFP_FPSCR_STAT_MASK) |
951 (uregs->fpcr & VFP_FPSCR_CTRL_MASK);
952
953 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &fpscr,
954 vregs_end_pos, VFP_STATE_SIZE);
955 }
956
957 return ret;
958 }
959
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)960 static int compat_vfp_set(struct task_struct *target,
961 const struct user_regset *regset,
962 unsigned int pos, unsigned int count,
963 const void *kbuf, const void __user *ubuf)
964 {
965 struct user_fpsimd_state *uregs;
966 compat_ulong_t fpscr;
967 int ret, vregs_end_pos;
968
969 if (!system_supports_fpsimd())
970 return -EINVAL;
971
972 uregs = &target->thread.fpsimd_state.user_fpsimd;
973
974 vregs_end_pos = VFP_STATE_SIZE - sizeof(compat_ulong_t);
975 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, uregs, 0,
976 vregs_end_pos);
977
978 if (count && !ret) {
979 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &fpscr,
980 vregs_end_pos, VFP_STATE_SIZE);
981 if (!ret) {
982 uregs->fpsr = fpscr & VFP_FPSCR_STAT_MASK;
983 uregs->fpcr = fpscr & VFP_FPSCR_CTRL_MASK;
984 }
985 }
986
987 fpsimd_flush_task_state(target);
988 return ret;
989 }
990
compat_tls_get(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,void * kbuf,void __user * ubuf)991 static int compat_tls_get(struct task_struct *target,
992 const struct user_regset *regset, unsigned int pos,
993 unsigned int count, void *kbuf, void __user *ubuf)
994 {
995 compat_ulong_t tls = (compat_ulong_t)target->thread.tp_value;
996 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
997 }
998
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)999 static int compat_tls_set(struct task_struct *target,
1000 const struct user_regset *regset, unsigned int pos,
1001 unsigned int count, const void *kbuf,
1002 const void __user *ubuf)
1003 {
1004 int ret;
1005 compat_ulong_t tls = target->thread.tp_value;
1006
1007 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
1008 if (ret)
1009 return ret;
1010
1011 target->thread.tp_value = tls;
1012 return ret;
1013 }
1014
1015 static const struct user_regset aarch32_regsets[] = {
1016 [REGSET_COMPAT_GPR] = {
1017 .core_note_type = NT_PRSTATUS,
1018 .n = COMPAT_ELF_NGREG,
1019 .size = sizeof(compat_elf_greg_t),
1020 .align = sizeof(compat_elf_greg_t),
1021 .get = compat_gpr_get,
1022 .set = compat_gpr_set
1023 },
1024 [REGSET_COMPAT_VFP] = {
1025 .core_note_type = NT_ARM_VFP,
1026 .n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
1027 .size = sizeof(compat_ulong_t),
1028 .align = sizeof(compat_ulong_t),
1029 .active = fpr_active,
1030 .get = compat_vfp_get,
1031 .set = compat_vfp_set
1032 },
1033 };
1034
1035 static const struct user_regset_view user_aarch32_view = {
1036 .name = "aarch32", .e_machine = EM_ARM,
1037 .regsets = aarch32_regsets, .n = ARRAY_SIZE(aarch32_regsets)
1038 };
1039
1040 static const struct user_regset aarch32_ptrace_regsets[] = {
1041 [REGSET_GPR] = {
1042 .core_note_type = NT_PRSTATUS,
1043 .n = COMPAT_ELF_NGREG,
1044 .size = sizeof(compat_elf_greg_t),
1045 .align = sizeof(compat_elf_greg_t),
1046 .get = compat_gpr_get,
1047 .set = compat_gpr_set
1048 },
1049 [REGSET_FPR] = {
1050 .core_note_type = NT_ARM_VFP,
1051 .n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
1052 .size = sizeof(compat_ulong_t),
1053 .align = sizeof(compat_ulong_t),
1054 .get = compat_vfp_get,
1055 .set = compat_vfp_set
1056 },
1057 [REGSET_TLS] = {
1058 .core_note_type = NT_ARM_TLS,
1059 .n = 1,
1060 .size = sizeof(compat_ulong_t),
1061 .align = sizeof(compat_ulong_t),
1062 .get = compat_tls_get,
1063 .set = compat_tls_set,
1064 },
1065 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1066 [REGSET_HW_BREAK] = {
1067 .core_note_type = NT_ARM_HW_BREAK,
1068 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
1069 .size = sizeof(u32),
1070 .align = sizeof(u32),
1071 .get = hw_break_get,
1072 .set = hw_break_set,
1073 },
1074 [REGSET_HW_WATCH] = {
1075 .core_note_type = NT_ARM_HW_WATCH,
1076 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
1077 .size = sizeof(u32),
1078 .align = sizeof(u32),
1079 .get = hw_break_get,
1080 .set = hw_break_set,
1081 },
1082 #endif
1083 [REGSET_SYSTEM_CALL] = {
1084 .core_note_type = NT_ARM_SYSTEM_CALL,
1085 .n = 1,
1086 .size = sizeof(int),
1087 .align = sizeof(int),
1088 .get = system_call_get,
1089 .set = system_call_set,
1090 },
1091 };
1092
1093 static const struct user_regset_view user_aarch32_ptrace_view = {
1094 .name = "aarch32", .e_machine = EM_ARM,
1095 .regsets = aarch32_ptrace_regsets, .n = ARRAY_SIZE(aarch32_ptrace_regsets)
1096 };
1097
compat_ptrace_read_user(struct task_struct * tsk,compat_ulong_t off,compat_ulong_t __user * ret)1098 static int compat_ptrace_read_user(struct task_struct *tsk, compat_ulong_t off,
1099 compat_ulong_t __user *ret)
1100 {
1101 compat_ulong_t tmp;
1102
1103 if (off & 3)
1104 return -EIO;
1105
1106 if (off == COMPAT_PT_TEXT_ADDR)
1107 tmp = tsk->mm->start_code;
1108 else if (off == COMPAT_PT_DATA_ADDR)
1109 tmp = tsk->mm->start_data;
1110 else if (off == COMPAT_PT_TEXT_END_ADDR)
1111 tmp = tsk->mm->end_code;
1112 else if (off < sizeof(compat_elf_gregset_t))
1113 return copy_regset_to_user(tsk, &user_aarch32_view,
1114 REGSET_COMPAT_GPR, off,
1115 sizeof(compat_ulong_t), ret);
1116 else if (off >= COMPAT_USER_SZ)
1117 return -EIO;
1118 else
1119 tmp = 0;
1120
1121 return put_user(tmp, ret);
1122 }
1123
compat_ptrace_write_user(struct task_struct * tsk,compat_ulong_t off,compat_ulong_t val)1124 static int compat_ptrace_write_user(struct task_struct *tsk, compat_ulong_t off,
1125 compat_ulong_t val)
1126 {
1127 int ret;
1128 mm_segment_t old_fs = get_fs();
1129
1130 if (off & 3 || off >= COMPAT_USER_SZ)
1131 return -EIO;
1132
1133 if (off >= sizeof(compat_elf_gregset_t))
1134 return 0;
1135
1136 set_fs(KERNEL_DS);
1137 ret = copy_regset_from_user(tsk, &user_aarch32_view,
1138 REGSET_COMPAT_GPR, off,
1139 sizeof(compat_ulong_t),
1140 &val);
1141 set_fs(old_fs);
1142
1143 return ret;
1144 }
1145
1146 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1147
1148 /*
1149 * Convert a virtual register number into an index for a thread_info
1150 * breakpoint array. Breakpoints are identified using positive numbers
1151 * whilst watchpoints are negative. The registers are laid out as pairs
1152 * of (address, control), each pair mapping to a unique hw_breakpoint struct.
1153 * Register 0 is reserved for describing resource information.
1154 */
compat_ptrace_hbp_num_to_idx(compat_long_t num)1155 static int compat_ptrace_hbp_num_to_idx(compat_long_t num)
1156 {
1157 return (abs(num) - 1) >> 1;
1158 }
1159
compat_ptrace_hbp_get_resource_info(u32 * kdata)1160 static int compat_ptrace_hbp_get_resource_info(u32 *kdata)
1161 {
1162 u8 num_brps, num_wrps, debug_arch, wp_len;
1163 u32 reg = 0;
1164
1165 num_brps = hw_breakpoint_slots(TYPE_INST);
1166 num_wrps = hw_breakpoint_slots(TYPE_DATA);
1167
1168 debug_arch = debug_monitors_arch();
1169 wp_len = 8;
1170 reg |= debug_arch;
1171 reg <<= 8;
1172 reg |= wp_len;
1173 reg <<= 8;
1174 reg |= num_wrps;
1175 reg <<= 8;
1176 reg |= num_brps;
1177
1178 *kdata = reg;
1179 return 0;
1180 }
1181
compat_ptrace_hbp_get(unsigned int note_type,struct task_struct * tsk,compat_long_t num,u32 * kdata)1182 static int compat_ptrace_hbp_get(unsigned int note_type,
1183 struct task_struct *tsk,
1184 compat_long_t num,
1185 u32 *kdata)
1186 {
1187 u64 addr = 0;
1188 u32 ctrl = 0;
1189
1190 int err, idx = compat_ptrace_hbp_num_to_idx(num);;
1191
1192 if (num & 1) {
1193 err = ptrace_hbp_get_addr(note_type, tsk, idx, &addr);
1194 *kdata = (u32)addr;
1195 } else {
1196 err = ptrace_hbp_get_ctrl(note_type, tsk, idx, &ctrl);
1197 *kdata = ctrl;
1198 }
1199
1200 return err;
1201 }
1202
compat_ptrace_hbp_set(unsigned int note_type,struct task_struct * tsk,compat_long_t num,u32 * kdata)1203 static int compat_ptrace_hbp_set(unsigned int note_type,
1204 struct task_struct *tsk,
1205 compat_long_t num,
1206 u32 *kdata)
1207 {
1208 u64 addr;
1209 u32 ctrl;
1210
1211 int err, idx = compat_ptrace_hbp_num_to_idx(num);
1212
1213 if (num & 1) {
1214 addr = *kdata;
1215 err = ptrace_hbp_set_addr(note_type, tsk, idx, addr);
1216 } else {
1217 ctrl = *kdata;
1218 err = ptrace_hbp_set_ctrl(note_type, tsk, idx, ctrl);
1219 }
1220
1221 return err;
1222 }
1223
compat_ptrace_gethbpregs(struct task_struct * tsk,compat_long_t num,compat_ulong_t __user * data)1224 static int compat_ptrace_gethbpregs(struct task_struct *tsk, compat_long_t num,
1225 compat_ulong_t __user *data)
1226 {
1227 int ret;
1228 u32 kdata;
1229
1230 /* Watchpoint */
1231 if (num < 0) {
1232 ret = compat_ptrace_hbp_get(NT_ARM_HW_WATCH, tsk, num, &kdata);
1233 /* Resource info */
1234 } else if (num == 0) {
1235 ret = compat_ptrace_hbp_get_resource_info(&kdata);
1236 /* Breakpoint */
1237 } else {
1238 ret = compat_ptrace_hbp_get(NT_ARM_HW_BREAK, tsk, num, &kdata);
1239 }
1240
1241 if (!ret)
1242 ret = put_user(kdata, data);
1243
1244 return ret;
1245 }
1246
compat_ptrace_sethbpregs(struct task_struct * tsk,compat_long_t num,compat_ulong_t __user * data)1247 static int compat_ptrace_sethbpregs(struct task_struct *tsk, compat_long_t num,
1248 compat_ulong_t __user *data)
1249 {
1250 int ret;
1251 u32 kdata = 0;
1252
1253 if (num == 0)
1254 return 0;
1255
1256 ret = get_user(kdata, data);
1257 if (ret)
1258 return ret;
1259
1260 if (num < 0)
1261 ret = compat_ptrace_hbp_set(NT_ARM_HW_WATCH, tsk, num, &kdata);
1262 else
1263 ret = compat_ptrace_hbp_set(NT_ARM_HW_BREAK, tsk, num, &kdata);
1264
1265 return ret;
1266 }
1267 #endif /* CONFIG_HAVE_HW_BREAKPOINT */
1268
compat_arch_ptrace(struct task_struct * child,compat_long_t request,compat_ulong_t caddr,compat_ulong_t cdata)1269 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1270 compat_ulong_t caddr, compat_ulong_t cdata)
1271 {
1272 unsigned long addr = caddr;
1273 unsigned long data = cdata;
1274 void __user *datap = compat_ptr(data);
1275 int ret;
1276
1277 switch (request) {
1278 case PTRACE_PEEKUSR:
1279 ret = compat_ptrace_read_user(child, addr, datap);
1280 break;
1281
1282 case PTRACE_POKEUSR:
1283 ret = compat_ptrace_write_user(child, addr, data);
1284 break;
1285
1286 case COMPAT_PTRACE_GETREGS:
1287 ret = copy_regset_to_user(child,
1288 &user_aarch32_view,
1289 REGSET_COMPAT_GPR,
1290 0, sizeof(compat_elf_gregset_t),
1291 datap);
1292 break;
1293
1294 case COMPAT_PTRACE_SETREGS:
1295 ret = copy_regset_from_user(child,
1296 &user_aarch32_view,
1297 REGSET_COMPAT_GPR,
1298 0, sizeof(compat_elf_gregset_t),
1299 datap);
1300 break;
1301
1302 case COMPAT_PTRACE_GET_THREAD_AREA:
1303 ret = put_user((compat_ulong_t)child->thread.tp_value,
1304 (compat_ulong_t __user *)datap);
1305 break;
1306
1307 case COMPAT_PTRACE_SET_SYSCALL:
1308 task_pt_regs(child)->syscallno = data;
1309 ret = 0;
1310 break;
1311
1312 case COMPAT_PTRACE_GETVFPREGS:
1313 ret = copy_regset_to_user(child,
1314 &user_aarch32_view,
1315 REGSET_COMPAT_VFP,
1316 0, VFP_STATE_SIZE,
1317 datap);
1318 break;
1319
1320 case COMPAT_PTRACE_SETVFPREGS:
1321 ret = copy_regset_from_user(child,
1322 &user_aarch32_view,
1323 REGSET_COMPAT_VFP,
1324 0, VFP_STATE_SIZE,
1325 datap);
1326 break;
1327
1328 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1329 case COMPAT_PTRACE_GETHBPREGS:
1330 ret = compat_ptrace_gethbpregs(child, addr, datap);
1331 break;
1332
1333 case COMPAT_PTRACE_SETHBPREGS:
1334 ret = compat_ptrace_sethbpregs(child, addr, datap);
1335 break;
1336 #endif
1337
1338 default:
1339 ret = compat_ptrace_request(child, request, addr,
1340 data);
1341 break;
1342 }
1343
1344 return ret;
1345 }
1346 #endif /* CONFIG_COMPAT */
1347
task_user_regset_view(struct task_struct * task)1348 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1349 {
1350 #ifdef CONFIG_COMPAT
1351 /*
1352 * Core dumping of 32-bit tasks or compat ptrace requests must use the
1353 * user_aarch32_view compatible with arm32. Native ptrace requests on
1354 * 32-bit children use an extended user_aarch32_ptrace_view to allow
1355 * access to the TLS register.
1356 */
1357 if (is_compat_task())
1358 return &user_aarch32_view;
1359 else if (is_compat_thread(task_thread_info(task)))
1360 return &user_aarch32_ptrace_view;
1361 #endif
1362 return &user_aarch64_view;
1363 }
1364
arch_ptrace(struct task_struct * child,long request,unsigned long addr,unsigned long data)1365 long arch_ptrace(struct task_struct *child, long request,
1366 unsigned long addr, unsigned long data)
1367 {
1368 return ptrace_request(child, request, addr, data);
1369 }
1370
1371 enum ptrace_syscall_dir {
1372 PTRACE_SYSCALL_ENTER = 0,
1373 PTRACE_SYSCALL_EXIT,
1374 };
1375
tracehook_report_syscall(struct pt_regs * regs,enum ptrace_syscall_dir dir)1376 static void tracehook_report_syscall(struct pt_regs *regs,
1377 enum ptrace_syscall_dir dir)
1378 {
1379 int regno;
1380 unsigned long saved_reg;
1381
1382 /*
1383 * A scratch register (ip(r12) on AArch32, x7 on AArch64) is
1384 * used to denote syscall entry/exit:
1385 */
1386 regno = (is_compat_task() ? 12 : 7);
1387 saved_reg = regs->regs[regno];
1388 regs->regs[regno] = dir;
1389
1390 if (dir == PTRACE_SYSCALL_EXIT)
1391 tracehook_report_syscall_exit(regs, 0);
1392 else if (tracehook_report_syscall_entry(regs))
1393 forget_syscall(regs);
1394
1395 regs->regs[regno] = saved_reg;
1396 }
1397
syscall_trace_enter(struct pt_regs * regs)1398 asmlinkage int syscall_trace_enter(struct pt_regs *regs)
1399 {
1400 if (test_thread_flag(TIF_SYSCALL_TRACE))
1401 tracehook_report_syscall(regs, PTRACE_SYSCALL_ENTER);
1402
1403 /* Do the secure computing after ptrace; failures should be fast. */
1404 if (secure_computing(NULL) == -1)
1405 return -1;
1406
1407 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
1408 trace_sys_enter(regs, regs->syscallno);
1409
1410 audit_syscall_entry(regs->syscallno, regs->orig_x0, regs->regs[1],
1411 regs->regs[2], regs->regs[3]);
1412
1413 return regs->syscallno;
1414 }
1415
syscall_trace_exit(struct pt_regs * regs)1416 asmlinkage void syscall_trace_exit(struct pt_regs *regs)
1417 {
1418 audit_syscall_exit(regs);
1419
1420 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
1421 trace_sys_exit(regs, regs_return_value(regs));
1422
1423 if (test_thread_flag(TIF_SYSCALL_TRACE))
1424 tracehook_report_syscall(regs, PTRACE_SYSCALL_EXIT);
1425 }
1426
1427 /*
1428 * SPSR_ELx bits which are always architecturally RES0 per ARM DDI 0487D.a.
1429 * We permit userspace to set SSBS (AArch64 bit 12, AArch32 bit 23) which is
1430 * not described in ARM DDI 0487D.a.
1431 * We treat PAN and UAO as RES0 bits, as they are meaningless at EL0, and may
1432 * be allocated an EL0 meaning in future.
1433 * Userspace cannot use these until they have an architectural meaning.
1434 * Note that this follows the SPSR_ELx format, not the AArch32 PSR format.
1435 * We also reserve IL for the kernel; SS is handled dynamically.
1436 */
1437 #define SPSR_EL1_AARCH64_RES0_BITS \
1438 (GENMASK_ULL(63, 32) | GENMASK_ULL(27, 25) | GENMASK_ULL(23, 22) | \
1439 GENMASK_ULL(20, 13) | GENMASK_ULL(11, 10) | GENMASK_ULL(5, 5))
1440 #define SPSR_EL1_AARCH32_RES0_BITS \
1441 (GENMASK_ULL(63, 32) | GENMASK_ULL(22, 22) | GENMASK_ULL(20, 20))
1442
valid_compat_regs(struct user_pt_regs * regs)1443 static int valid_compat_regs(struct user_pt_regs *regs)
1444 {
1445 regs->pstate &= ~SPSR_EL1_AARCH32_RES0_BITS;
1446
1447 if (!system_supports_mixed_endian_el0()) {
1448 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
1449 regs->pstate |= COMPAT_PSR_E_BIT;
1450 else
1451 regs->pstate &= ~COMPAT_PSR_E_BIT;
1452 }
1453
1454 if (user_mode(regs) && (regs->pstate & PSR_MODE32_BIT) &&
1455 (regs->pstate & COMPAT_PSR_A_BIT) == 0 &&
1456 (regs->pstate & COMPAT_PSR_I_BIT) == 0 &&
1457 (regs->pstate & COMPAT_PSR_F_BIT) == 0) {
1458 return 1;
1459 }
1460
1461 /*
1462 * Force PSR to a valid 32-bit EL0t, preserving the same bits as
1463 * arch/arm.
1464 */
1465 regs->pstate &= COMPAT_PSR_N_BIT | COMPAT_PSR_Z_BIT |
1466 COMPAT_PSR_C_BIT | COMPAT_PSR_V_BIT |
1467 COMPAT_PSR_Q_BIT | COMPAT_PSR_IT_MASK |
1468 COMPAT_PSR_GE_MASK | COMPAT_PSR_E_BIT |
1469 COMPAT_PSR_T_BIT;
1470 regs->pstate |= PSR_MODE32_BIT;
1471
1472 return 0;
1473 }
1474
valid_native_regs(struct user_pt_regs * regs)1475 static int valid_native_regs(struct user_pt_regs *regs)
1476 {
1477 regs->pstate &= ~SPSR_EL1_AARCH64_RES0_BITS;
1478
1479 if (user_mode(regs) && !(regs->pstate & PSR_MODE32_BIT) &&
1480 (regs->pstate & PSR_D_BIT) == 0 &&
1481 (regs->pstate & PSR_A_BIT) == 0 &&
1482 (regs->pstate & PSR_I_BIT) == 0 &&
1483 (regs->pstate & PSR_F_BIT) == 0) {
1484 return 1;
1485 }
1486
1487 /* Force PSR to a valid 64-bit EL0t */
1488 regs->pstate &= PSR_N_BIT | PSR_Z_BIT | PSR_C_BIT | PSR_V_BIT;
1489
1490 return 0;
1491 }
1492
1493 /*
1494 * Are the current registers suitable for user mode? (used to maintain
1495 * security in signal handlers)
1496 */
valid_user_regs(struct user_pt_regs * regs,struct task_struct * task)1497 int valid_user_regs(struct user_pt_regs *regs, struct task_struct *task)
1498 {
1499 if (!test_tsk_thread_flag(task, TIF_SINGLESTEP))
1500 regs->pstate &= ~DBG_SPSR_SS;
1501
1502 if (is_compat_thread(task_thread_info(task)))
1503 return valid_compat_regs(regs);
1504 else
1505 return valid_native_regs(regs);
1506 }
1507