Merge tag 'v3.10.56' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / arm64 / kernel / ptrace.c
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.h>
26 #include <linux/mm.h>
27 #include <linux/smp.h>
28 #include <linux/ptrace.h>
29 #include <linux/user.h>
30 #include <linux/seccomp.h>
31 #include <linux/security.h>
32 #include <linux/init.h>
33 #include <linux/signal.h>
34 #include <linux/uaccess.h>
35 #include <linux/perf_event.h>
36 #include <linux/hw_breakpoint.h>
37 #include <linux/regset.h>
38 #include <linux/tracehook.h>
39 #include <linux/elf.h>
40
41 #include <asm/compat.h>
42 #include <asm/debug-monitors.h>
43 #include <asm/pgtable.h>
44 #include <asm/syscall.h>
45 #include <asm/traps.h>
46 #include <asm/system_misc.h>
47
48 #define CREATE_TRACE_POINTS
49 #include <trace/events/syscalls.h>
50
51 /*
52 * TODO: does not yet catch signals sent when the child dies.
53 * in exit.c or in signal.c.
54 */
55
56 /*
57 * Called by kernel/ptrace.c when detaching..
58 */
59 void ptrace_disable(struct task_struct *child)
60 {
61 }
62
63 #ifdef CONFIG_HAVE_HW_BREAKPOINT
64 /*
65 * Handle hitting a HW-breakpoint.
66 */
67 static void ptrace_hbptriggered(struct perf_event *bp,
68 struct perf_sample_data *data,
69 struct pt_regs *regs)
70 {
71 struct arch_hw_breakpoint *bkpt = counter_arch_bp(bp);
72 siginfo_t info = {
73 .si_signo = SIGTRAP,
74 .si_errno = 0,
75 .si_code = TRAP_HWBKPT,
76 .si_addr = (void __user *)(bkpt->trigger),
77 };
78
79 #ifdef CONFIG_COMPAT
80 int i;
81
82 if (!is_compat_task())
83 goto send_sig;
84
85 for (i = 0; i < ARM_MAX_BRP; ++i) {
86 if (current->thread.debug.hbp_break[i] == bp) {
87 info.si_errno = (i << 1) + 1;
88 break;
89 }
90 }
91
92 for (i = 0; i < ARM_MAX_WRP; ++i) {
93 if (current->thread.debug.hbp_watch[i] == bp) {
94 info.si_errno = -((i << 1) + 1);
95 break;
96 }
97 }
98
99 send_sig:
100 #endif
101 force_sig_info(SIGTRAP, &info, current);
102 }
103
104 /*
105 * Unregister breakpoints from this task and reset the pointers in
106 * the thread_struct.
107 */
108 void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
109 {
110 int i;
111 struct thread_struct *t = &tsk->thread;
112
113 for (i = 0; i < ARM_MAX_BRP; i++) {
114 if (t->debug.hbp_break[i]) {
115 unregister_hw_breakpoint(t->debug.hbp_break[i]);
116 t->debug.hbp_break[i] = NULL;
117 }
118 }
119
120 for (i = 0; i < ARM_MAX_WRP; i++) {
121 if (t->debug.hbp_watch[i]) {
122 unregister_hw_breakpoint(t->debug.hbp_watch[i]);
123 t->debug.hbp_watch[i] = NULL;
124 }
125 }
126 }
127
128 void ptrace_hw_copy_thread(struct task_struct *tsk)
129 {
130 memset(&tsk->thread.debug, 0, sizeof(struct debug_info));
131 }
132
133 static struct perf_event *ptrace_hbp_get_event(unsigned int note_type,
134 struct task_struct *tsk,
135 unsigned long idx)
136 {
137 struct perf_event *bp = ERR_PTR(-EINVAL);
138
139 switch (note_type) {
140 case NT_ARM_HW_BREAK:
141 if (idx < ARM_MAX_BRP)
142 bp = tsk->thread.debug.hbp_break[idx];
143 break;
144 case NT_ARM_HW_WATCH:
145 if (idx < ARM_MAX_WRP)
146 bp = tsk->thread.debug.hbp_watch[idx];
147 break;
148 }
149
150 return bp;
151 }
152
153 static int ptrace_hbp_set_event(unsigned int note_type,
154 struct task_struct *tsk,
155 unsigned long idx,
156 struct perf_event *bp)
157 {
158 int err = -EINVAL;
159
160 switch (note_type) {
161 case NT_ARM_HW_BREAK:
162 if (idx < ARM_MAX_BRP) {
163 tsk->thread.debug.hbp_break[idx] = bp;
164 err = 0;
165 }
166 break;
167 case NT_ARM_HW_WATCH:
168 if (idx < ARM_MAX_WRP) {
169 tsk->thread.debug.hbp_watch[idx] = bp;
170 err = 0;
171 }
172 break;
173 }
174
175 return err;
176 }
177
178 static struct perf_event *ptrace_hbp_create(unsigned int note_type,
179 struct task_struct *tsk,
180 unsigned long idx)
181 {
182 struct perf_event *bp;
183 struct perf_event_attr attr;
184 int err, type;
185
186 switch (note_type) {
187 case NT_ARM_HW_BREAK:
188 type = HW_BREAKPOINT_X;
189 break;
190 case NT_ARM_HW_WATCH:
191 type = HW_BREAKPOINT_RW;
192 break;
193 default:
194 return ERR_PTR(-EINVAL);
195 }
196
197 ptrace_breakpoint_init(&attr);
198
199 /*
200 * Initialise fields to sane defaults
201 * (i.e. values that will pass validation).
202 */
203 attr.bp_addr = 0;
204 attr.bp_len = HW_BREAKPOINT_LEN_4;
205 attr.bp_type = type;
206 attr.disabled = 1;
207
208 bp = register_user_hw_breakpoint(&attr, ptrace_hbptriggered, NULL, tsk);
209 if (IS_ERR(bp))
210 return bp;
211
212 err = ptrace_hbp_set_event(note_type, tsk, idx, bp);
213 if (err)
214 return ERR_PTR(err);
215
216 return bp;
217 }
218
219 static int ptrace_hbp_fill_attr_ctrl(unsigned int note_type,
220 struct arch_hw_breakpoint_ctrl ctrl,
221 struct perf_event_attr *attr)
222 {
223 int err, len, type, disabled = !ctrl.enabled;
224
225 attr->disabled = disabled;
226 if (disabled)
227 return 0;
228
229 err = arch_bp_generic_fields(ctrl, &len, &type);
230 if (err)
231 return err;
232
233 switch (note_type) {
234 case NT_ARM_HW_BREAK:
235 if ((type & HW_BREAKPOINT_X) != type)
236 return -EINVAL;
237 break;
238 case NT_ARM_HW_WATCH:
239 if ((type & HW_BREAKPOINT_RW) != type)
240 return -EINVAL;
241 break;
242 default:
243 return -EINVAL;
244 }
245
246 attr->bp_len = len;
247 attr->bp_type = type;
248
249 return 0;
250 }
251
252 static int ptrace_hbp_get_resource_info(unsigned int note_type, u32 *info)
253 {
254 u8 num;
255 u32 reg = 0;
256
257 switch (note_type) {
258 case NT_ARM_HW_BREAK:
259 num = hw_breakpoint_slots(TYPE_INST);
260 break;
261 case NT_ARM_HW_WATCH:
262 num = hw_breakpoint_slots(TYPE_DATA);
263 break;
264 default:
265 return -EINVAL;
266 }
267
268 reg |= debug_monitors_arch();
269 reg <<= 8;
270 reg |= num;
271
272 *info = reg;
273 return 0;
274 }
275
276 static int ptrace_hbp_get_ctrl(unsigned int note_type,
277 struct task_struct *tsk,
278 unsigned long idx,
279 u32 *ctrl)
280 {
281 struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
282
283 if (IS_ERR(bp))
284 return PTR_ERR(bp);
285
286 *ctrl = bp ? encode_ctrl_reg(counter_arch_bp(bp)->ctrl) : 0;
287 return 0;
288 }
289
290 static int ptrace_hbp_get_addr(unsigned int note_type,
291 struct task_struct *tsk,
292 unsigned long idx,
293 u64 *addr)
294 {
295 struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
296
297 if (IS_ERR(bp))
298 return PTR_ERR(bp);
299
300 *addr = bp ? bp->attr.bp_addr : 0;
301 return 0;
302 }
303
304 static struct perf_event *ptrace_hbp_get_initialised_bp(unsigned int note_type,
305 struct task_struct *tsk,
306 unsigned long idx)
307 {
308 struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
309
310 if (!bp)
311 bp = ptrace_hbp_create(note_type, tsk, idx);
312
313 return bp;
314 }
315
316 static int ptrace_hbp_set_ctrl(unsigned int note_type,
317 struct task_struct *tsk,
318 unsigned long idx,
319 u32 uctrl)
320 {
321 int err;
322 struct perf_event *bp;
323 struct perf_event_attr attr;
324 struct arch_hw_breakpoint_ctrl ctrl;
325
326 bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
327 if (IS_ERR(bp)) {
328 err = PTR_ERR(bp);
329 return err;
330 }
331
332 attr = bp->attr;
333 decode_ctrl_reg(uctrl, &ctrl);
334 err = ptrace_hbp_fill_attr_ctrl(note_type, ctrl, &attr);
335 if (err)
336 return err;
337
338 return modify_user_hw_breakpoint(bp, &attr);
339 }
340
341 static int ptrace_hbp_set_addr(unsigned int note_type,
342 struct task_struct *tsk,
343 unsigned long idx,
344 u64 addr)
345 {
346 int err;
347 struct perf_event *bp;
348 struct perf_event_attr attr;
349
350 bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
351 if (IS_ERR(bp)) {
352 err = PTR_ERR(bp);
353 return err;
354 }
355
356 attr = bp->attr;
357 attr.bp_addr = addr;
358 err = modify_user_hw_breakpoint(bp, &attr);
359 return err;
360 }
361
362 #define PTRACE_HBP_ADDR_SZ sizeof(u64)
363 #define PTRACE_HBP_CTRL_SZ sizeof(u32)
364 #define PTRACE_HBP_PAD_SZ sizeof(u32)
365
366 static int hw_break_get(struct task_struct *target,
367 const struct user_regset *regset,
368 unsigned int pos, unsigned int count,
369 void *kbuf, void __user *ubuf)
370 {
371 unsigned int note_type = regset->core_note_type;
372 int ret, idx = 0, offset, limit;
373 u32 info, ctrl;
374 u64 addr;
375
376 /* Resource info */
377 ret = ptrace_hbp_get_resource_info(note_type, &info);
378 if (ret)
379 return ret;
380
381 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &info, 0,
382 sizeof(info));
383 if (ret)
384 return ret;
385
386 /* Pad */
387 offset = offsetof(struct user_hwdebug_state, pad);
388 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf, offset,
389 offset + PTRACE_HBP_PAD_SZ);
390 if (ret)
391 return ret;
392
393 /* (address, ctrl) registers */
394 offset = offsetof(struct user_hwdebug_state, dbg_regs);
395 limit = regset->n * regset->size;
396 while (count && offset < limit) {
397 ret = ptrace_hbp_get_addr(note_type, target, idx, &addr);
398 if (ret)
399 return ret;
400 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &addr,
401 offset, offset + PTRACE_HBP_ADDR_SZ);
402 if (ret)
403 return ret;
404 offset += PTRACE_HBP_ADDR_SZ;
405
406 ret = ptrace_hbp_get_ctrl(note_type, target, idx, &ctrl);
407 if (ret)
408 return ret;
409 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &ctrl,
410 offset, offset + PTRACE_HBP_CTRL_SZ);
411 if (ret)
412 return ret;
413 offset += PTRACE_HBP_CTRL_SZ;
414
415 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
416 offset,
417 offset + PTRACE_HBP_PAD_SZ);
418 if (ret)
419 return ret;
420 offset += PTRACE_HBP_PAD_SZ;
421 idx++;
422 }
423
424 return 0;
425 }
426
427 static int hw_break_set(struct task_struct *target,
428 const struct user_regset *regset,
429 unsigned int pos, unsigned int count,
430 const void *kbuf, const void __user *ubuf)
431 {
432 unsigned int note_type = regset->core_note_type;
433 int ret, idx = 0, offset, limit;
434 u32 ctrl;
435 u64 addr;
436
437 /* Resource info and pad */
438 offset = offsetof(struct user_hwdebug_state, dbg_regs);
439 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, 0, offset);
440 if (ret)
441 return ret;
442
443 /* (address, ctrl) registers */
444 limit = regset->n * regset->size;
445 while (count && offset < limit) {
446 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &addr,
447 offset, offset + PTRACE_HBP_ADDR_SZ);
448 if (ret)
449 return ret;
450 ret = ptrace_hbp_set_addr(note_type, target, idx, addr);
451 if (ret)
452 return ret;
453 offset += PTRACE_HBP_ADDR_SZ;
454
455 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ctrl,
456 offset, offset + PTRACE_HBP_CTRL_SZ);
457 if (ret)
458 return ret;
459 ret = ptrace_hbp_set_ctrl(note_type, target, idx, ctrl);
460 if (ret)
461 return ret;
462 offset += PTRACE_HBP_CTRL_SZ;
463
464 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
465 offset,
466 offset + PTRACE_HBP_PAD_SZ);
467 if (ret)
468 return ret;
469 offset += PTRACE_HBP_PAD_SZ;
470 idx++;
471 }
472
473 return 0;
474 }
475 #endif /* CONFIG_HAVE_HW_BREAKPOINT */
476
477 static int gpr_get(struct task_struct *target,
478 const struct user_regset *regset,
479 unsigned int pos, unsigned int count,
480 void *kbuf, void __user *ubuf)
481 {
482 struct user_pt_regs *uregs = &task_pt_regs(target)->user_regs;
483 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0, -1);
484 }
485
486 static int gpr_set(struct task_struct *target, const struct user_regset *regset,
487 unsigned int pos, unsigned int count,
488 const void *kbuf, const void __user *ubuf)
489 {
490 int ret;
491 struct user_pt_regs newregs;
492
493 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newregs, 0, -1);
494 if (ret)
495 return ret;
496
497 if (!valid_user_regs(&newregs))
498 return -EINVAL;
499
500 task_pt_regs(target)->user_regs = newregs;
501 return 0;
502 }
503
504 /*
505 * TODO: update fp accessors for lazy context switching (sync/flush hwstate)
506 */
507 static int fpr_get(struct task_struct *target, const struct user_regset *regset,
508 unsigned int pos, unsigned int count,
509 void *kbuf, void __user *ubuf)
510 {
511 struct user_fpsimd_state *uregs;
512 uregs = &target->thread.fpsimd_state.user_fpsimd;
513 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0, -1);
514 }
515
516 static int fpr_set(struct task_struct *target, const struct user_regset *regset,
517 unsigned int pos, unsigned int count,
518 const void *kbuf, const void __user *ubuf)
519 {
520 int ret;
521 struct user_fpsimd_state newstate;
522
523 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newstate, 0, -1);
524 if (ret)
525 return ret;
526
527 target->thread.fpsimd_state.user_fpsimd = newstate;
528 fpsimd_flush_task_state(target);
529 return ret;
530 }
531
532 static int tls_get(struct task_struct *target, const struct user_regset *regset,
533 unsigned int pos, unsigned int count,
534 void *kbuf, void __user *ubuf)
535 {
536 unsigned long *tls = &target->thread.tp_value;
537 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, tls, 0, -1);
538 }
539
540 static int tls_set(struct task_struct *target, const struct user_regset *regset,
541 unsigned int pos, unsigned int count,
542 const void *kbuf, const void __user *ubuf)
543 {
544 int ret;
545 unsigned long tls;
546
547 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
548 if (ret)
549 return ret;
550
551 target->thread.tp_value = tls;
552 return ret;
553 }
554
555 enum aarch64_regset {
556 REGSET_GPR,
557 REGSET_FPR,
558 REGSET_TLS,
559 #ifdef CONFIG_HAVE_HW_BREAKPOINT
560 REGSET_HW_BREAK,
561 REGSET_HW_WATCH,
562 #endif
563 };
564
565 static const struct user_regset aarch64_regsets[] = {
566 [REGSET_GPR] = {
567 .core_note_type = NT_PRSTATUS,
568 .n = sizeof(struct user_pt_regs) / sizeof(u64),
569 .size = sizeof(u64),
570 .align = sizeof(u64),
571 .get = gpr_get,
572 .set = gpr_set
573 },
574 [REGSET_FPR] = {
575 .core_note_type = NT_PRFPREG,
576 .n = sizeof(struct user_fpsimd_state) / sizeof(u32),
577 /*
578 * We pretend we have 32-bit registers because the fpsr and
579 * fpcr are 32-bits wide.
580 */
581 .size = sizeof(u32),
582 .align = sizeof(u32),
583 .get = fpr_get,
584 .set = fpr_set
585 },
586 [REGSET_TLS] = {
587 .core_note_type = NT_ARM_TLS,
588 .n = 1,
589 .size = sizeof(void *),
590 .align = sizeof(void *),
591 .get = tls_get,
592 .set = tls_set,
593 },
594 #ifdef CONFIG_HAVE_HW_BREAKPOINT
595 [REGSET_HW_BREAK] = {
596 .core_note_type = NT_ARM_HW_BREAK,
597 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
598 .size = sizeof(u32),
599 .align = sizeof(u32),
600 .get = hw_break_get,
601 .set = hw_break_set,
602 },
603 [REGSET_HW_WATCH] = {
604 .core_note_type = NT_ARM_HW_WATCH,
605 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
606 .size = sizeof(u32),
607 .align = sizeof(u32),
608 .get = hw_break_get,
609 .set = hw_break_set,
610 },
611 #endif
612 };
613
614 static const struct user_regset_view user_aarch64_view = {
615 .name = "aarch64", .e_machine = EM_AARCH64,
616 .regsets = aarch64_regsets, .n = ARRAY_SIZE(aarch64_regsets)
617 };
618
619 #ifdef CONFIG_COMPAT
620 #include <linux/compat.h>
621
622 enum compat_regset {
623 REGSET_COMPAT_GPR,
624 REGSET_COMPAT_VFP,
625 };
626
627 static int compat_gpr_get(struct task_struct *target,
628 const struct user_regset *regset,
629 unsigned int pos, unsigned int count,
630 void *kbuf, void __user *ubuf)
631 {
632 int ret = 0;
633 unsigned int i, start, num_regs;
634
635 /* Calculate the number of AArch32 registers contained in count */
636 num_regs = count / regset->size;
637
638 /* Convert pos into an register number */
639 start = pos / regset->size;
640
641 if (start + num_regs > regset->n)
642 return -EIO;
643
644 for (i = 0; i < num_regs; ++i) {
645 unsigned int idx = start + i;
646 void *reg;
647
648 switch (idx) {
649 case 15:
650 reg = (void *)&task_pt_regs(target)->pc;
651 break;
652 case 16:
653 reg = (void *)&task_pt_regs(target)->pstate;
654 break;
655 case 17:
656 reg = (void *)&task_pt_regs(target)->orig_x0;
657 break;
658 default:
659 reg = (void *)&task_pt_regs(target)->regs[idx];
660 }
661
662 if (!ubuf && kbuf) {
663 if (i == 0 && NULL != target && target->pid == current->pid)
664 printk(KERN_WARNING "coredump(%d) copy registers to kbuf\n", current->pid);
665 memcpy(kbuf, reg, sizeof(compat_ulong_t));
666 kbuf += sizeof(compat_ulong_t);
667 }
668 else {
669 ret = copy_to_user(ubuf, reg, sizeof(compat_ulong_t));
670
671 if (ret)
672 break;
673 else
674 ubuf += sizeof(compat_ulong_t);
675 }
676 }
677
678 return ret;
679 }
680
681 static int compat_gpr_set(struct task_struct *target,
682 const struct user_regset *regset,
683 unsigned int pos, unsigned int count,
684 const void *kbuf, const void __user *ubuf)
685 {
686 struct pt_regs newregs;
687 int ret = 0;
688 unsigned int i, start, num_regs;
689
690 /* Calculate the number of AArch32 registers contained in count */
691 num_regs = count / regset->size;
692
693 /* Convert pos into an register number */
694 start = pos / regset->size;
695
696 if (start + num_regs > regset->n)
697 return -EIO;
698
699 newregs = *task_pt_regs(target);
700
701 for (i = 0; i < num_regs; ++i) {
702 unsigned int idx = start + i;
703 void *reg;
704
705 switch (idx) {
706 case 15:
707 reg = (void *)&newregs.pc;
708 break;
709 case 16:
710 reg = (void *)&newregs.pstate;
711 break;
712 case 17:
713 reg = (void *)&newregs.orig_x0;
714 break;
715 default:
716 reg = (void *)&newregs.regs[idx];
717 }
718
719 ret = copy_from_user(reg, ubuf, sizeof(compat_ulong_t));
720
721 if (ret)
722 goto out;
723 else
724 ubuf += sizeof(compat_ulong_t);
725 }
726
727 if (valid_user_regs(&newregs.user_regs))
728 *task_pt_regs(target) = newregs;
729 else
730 ret = -EINVAL;
731
732 out:
733 return ret;
734 }
735
736 static int compat_vfp_get(struct task_struct *target,
737 const struct user_regset *regset,
738 unsigned int pos, unsigned int count,
739 void *kbuf, void __user *ubuf)
740 {
741 struct user_fpsimd_state *uregs;
742 compat_ulong_t fpscr;
743 int ret;
744
745 uregs = &target->thread.fpsimd_state.user_fpsimd;
746
747 /*
748 * The VFP registers are packed into the fpsimd_state, so they all sit
749 * nicely together for us. We just need to create the fpscr separately.
750 */
751 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0,
752 VFP_STATE_SIZE - sizeof(compat_ulong_t));
753
754 if (count && !ret) {
755 fpscr = (uregs->fpsr & VFP_FPSCR_STAT_MASK) |
756 (uregs->fpcr & VFP_FPSCR_CTRL_MASK);
757 ret = put_user(fpscr, (compat_ulong_t *)ubuf);
758 }
759
760 return ret;
761 }
762
763 static int compat_vfp_set(struct task_struct *target,
764 const struct user_regset *regset,
765 unsigned int pos, unsigned int count,
766 const void *kbuf, const void __user *ubuf)
767 {
768 struct user_fpsimd_state *uregs;
769 compat_ulong_t fpscr;
770 int ret;
771
772 if (pos + count > VFP_STATE_SIZE)
773 return -EIO;
774
775 uregs = &target->thread.fpsimd_state.user_fpsimd;
776
777 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, uregs, 0,
778 VFP_STATE_SIZE - sizeof(compat_ulong_t));
779
780 if (count && !ret) {
781 ret = get_user(fpscr, (compat_ulong_t *)ubuf);
782 uregs->fpsr = fpscr & VFP_FPSCR_STAT_MASK;
783 uregs->fpcr = fpscr & VFP_FPSCR_CTRL_MASK;
784 }
785
786 fpsimd_flush_task_state(target);
787 return ret;
788 }
789
790 static const struct user_regset aarch32_regsets[] = {
791 [REGSET_COMPAT_GPR] = {
792 .core_note_type = NT_PRSTATUS,
793 .n = COMPAT_ELF_NGREG,
794 .size = sizeof(compat_elf_greg_t),
795 .align = sizeof(compat_elf_greg_t),
796 .get = compat_gpr_get,
797 .set = compat_gpr_set
798 },
799 [REGSET_COMPAT_VFP] = {
800 .core_note_type = NT_ARM_VFP,
801 .n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
802 .size = sizeof(compat_ulong_t),
803 .align = sizeof(compat_ulong_t),
804 .get = compat_vfp_get,
805 .set = compat_vfp_set
806 },
807 };
808
809 static const struct user_regset_view user_aarch32_view = {
810 .name = "aarch32", .e_machine = EM_ARM,
811 .regsets = aarch32_regsets, .n = ARRAY_SIZE(aarch32_regsets)
812 };
813
814 static int compat_ptrace_read_user(struct task_struct *tsk, compat_ulong_t off,
815 compat_ulong_t __user *ret)
816 {
817 compat_ulong_t tmp;
818
819 if (off & 3)
820 return -EIO;
821
822 if (off == COMPAT_PT_TEXT_ADDR)
823 tmp = tsk->mm->start_code;
824 else if (off == COMPAT_PT_DATA_ADDR)
825 tmp = tsk->mm->start_data;
826 else if (off == COMPAT_PT_TEXT_END_ADDR)
827 tmp = tsk->mm->end_code;
828 else if (off < sizeof(compat_elf_gregset_t))
829 return copy_regset_to_user(tsk, &user_aarch32_view,
830 REGSET_COMPAT_GPR, off,
831 sizeof(compat_ulong_t), ret);
832 else if (off >= COMPAT_USER_SZ)
833 return -EIO;
834 else
835 tmp = 0;
836
837 return put_user(tmp, ret);
838 }
839
840 static int compat_ptrace_write_user(struct task_struct *tsk, compat_ulong_t off,
841 compat_ulong_t val)
842 {
843 int ret;
844 mm_segment_t old_fs = get_fs();
845
846 if (off & 3 || off >= COMPAT_USER_SZ)
847 return -EIO;
848
849 if (off >= sizeof(compat_elf_gregset_t))
850 return 0;
851
852 set_fs(KERNEL_DS);
853 ret = copy_regset_from_user(tsk, &user_aarch32_view,
854 REGSET_COMPAT_GPR, off,
855 sizeof(compat_ulong_t),
856 &val);
857 set_fs(old_fs);
858
859 return ret;
860 }
861
862 #ifdef CONFIG_HAVE_HW_BREAKPOINT
863
864 /*
865 * Convert a virtual register number into an index for a thread_info
866 * breakpoint array. Breakpoints are identified using positive numbers
867 * whilst watchpoints are negative. The registers are laid out as pairs
868 * of (address, control), each pair mapping to a unique hw_breakpoint struct.
869 * Register 0 is reserved for describing resource information.
870 */
871 static int compat_ptrace_hbp_num_to_idx(compat_long_t num)
872 {
873 return (abs(num) - 1) >> 1;
874 }
875
876 static int compat_ptrace_hbp_get_resource_info(u32 *kdata)
877 {
878 u8 num_brps, num_wrps, debug_arch, wp_len;
879 u32 reg = 0;
880
881 num_brps = hw_breakpoint_slots(TYPE_INST);
882 num_wrps = hw_breakpoint_slots(TYPE_DATA);
883
884 debug_arch = debug_monitors_arch();
885 wp_len = 8;
886 reg |= debug_arch;
887 reg <<= 8;
888 reg |= wp_len;
889 reg <<= 8;
890 reg |= num_wrps;
891 reg <<= 8;
892 reg |= num_brps;
893
894 *kdata = reg;
895 return 0;
896 }
897
898 static int compat_ptrace_hbp_get(unsigned int note_type,
899 struct task_struct *tsk,
900 compat_long_t num,
901 u32 *kdata)
902 {
903 u64 addr = 0;
904 u32 ctrl = 0;
905
906 int err, idx = compat_ptrace_hbp_num_to_idx(num);;
907
908 if (num & 1) {
909 err = ptrace_hbp_get_addr(note_type, tsk, idx, &addr);
910 *kdata = (u32)addr;
911 } else {
912 err = ptrace_hbp_get_ctrl(note_type, tsk, idx, &ctrl);
913 *kdata = ctrl;
914 }
915
916 return err;
917 }
918
919 static int compat_ptrace_hbp_set(unsigned int note_type,
920 struct task_struct *tsk,
921 compat_long_t num,
922 u32 *kdata)
923 {
924 u64 addr;
925 u32 ctrl;
926
927 int err, idx = compat_ptrace_hbp_num_to_idx(num);
928
929 if (num & 1) {
930 addr = *kdata;
931 err = ptrace_hbp_set_addr(note_type, tsk, idx, addr);
932 } else {
933 ctrl = *kdata;
934 err = ptrace_hbp_set_ctrl(note_type, tsk, idx, ctrl);
935 }
936
937 return err;
938 }
939
940 static int compat_ptrace_gethbpregs(struct task_struct *tsk, compat_long_t num,
941 compat_ulong_t __user *data)
942 {
943 int ret;
944 u32 kdata;
945 mm_segment_t old_fs = get_fs();
946
947 set_fs(KERNEL_DS);
948 /* Watchpoint */
949 if (num < 0) {
950 ret = compat_ptrace_hbp_get(NT_ARM_HW_WATCH, tsk, num, &kdata);
951 /* Resource info */
952 } else if (num == 0) {
953 ret = compat_ptrace_hbp_get_resource_info(&kdata);
954 /* Breakpoint */
955 } else {
956 ret = compat_ptrace_hbp_get(NT_ARM_HW_BREAK, tsk, num, &kdata);
957 }
958 set_fs(old_fs);
959
960 if (!ret)
961 ret = put_user(kdata, data);
962
963 return ret;
964 }
965
966 static int compat_ptrace_sethbpregs(struct task_struct *tsk, compat_long_t num,
967 compat_ulong_t __user *data)
968 {
969 int ret;
970 u32 kdata = 0;
971 mm_segment_t old_fs = get_fs();
972
973 if (num == 0)
974 return 0;
975
976 ret = get_user(kdata, data);
977 if (ret)
978 return ret;
979
980 set_fs(KERNEL_DS);
981 if (num < 0)
982 ret = compat_ptrace_hbp_set(NT_ARM_HW_WATCH, tsk, num, &kdata);
983 else
984 ret = compat_ptrace_hbp_set(NT_ARM_HW_BREAK, tsk, num, &kdata);
985 set_fs(old_fs);
986
987 return ret;
988 }
989 #endif /* CONFIG_HAVE_HW_BREAKPOINT */
990
991 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
992 compat_ulong_t caddr, compat_ulong_t cdata)
993 {
994 unsigned long addr = caddr;
995 unsigned long data = cdata;
996 void __user *datap = compat_ptr(data);
997 int ret;
998
999 switch (request) {
1000 case PTRACE_PEEKUSR:
1001 ret = compat_ptrace_read_user(child, addr, datap);
1002 break;
1003
1004 case PTRACE_POKEUSR:
1005 ret = compat_ptrace_write_user(child, addr, data);
1006 break;
1007
1008 case COMPAT_PTRACE_GETREGS:
1009 ret = copy_regset_to_user(child,
1010 &user_aarch32_view,
1011 REGSET_COMPAT_GPR,
1012 0, sizeof(compat_elf_gregset_t),
1013 datap);
1014 break;
1015
1016 case COMPAT_PTRACE_SETREGS:
1017 ret = copy_regset_from_user(child,
1018 &user_aarch32_view,
1019 REGSET_COMPAT_GPR,
1020 0, sizeof(compat_elf_gregset_t),
1021 datap);
1022 break;
1023
1024 case COMPAT_PTRACE_GET_THREAD_AREA:
1025 ret = put_user((compat_ulong_t)child->thread.tp_value,
1026 (compat_ulong_t __user *)datap);
1027 break;
1028
1029 case COMPAT_PTRACE_SET_SYSCALL:
1030 task_pt_regs(child)->syscallno = data;
1031 ret = 0;
1032 break;
1033
1034 case COMPAT_PTRACE_GETVFPREGS:
1035 ret = copy_regset_to_user(child,
1036 &user_aarch32_view,
1037 REGSET_COMPAT_VFP,
1038 0, VFP_STATE_SIZE,
1039 datap);
1040 break;
1041
1042 case COMPAT_PTRACE_SETVFPREGS:
1043 ret = copy_regset_from_user(child,
1044 &user_aarch32_view,
1045 REGSET_COMPAT_VFP,
1046 0, VFP_STATE_SIZE,
1047 datap);
1048 break;
1049
1050 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1051 case COMPAT_PTRACE_GETHBPREGS:
1052 ret = compat_ptrace_gethbpregs(child, addr, datap);
1053 break;
1054
1055 case COMPAT_PTRACE_SETHBPREGS:
1056 ret = compat_ptrace_sethbpregs(child, addr, datap);
1057 break;
1058 #endif
1059
1060 default:
1061 ret = compat_ptrace_request(child, request, addr,
1062 data);
1063 break;
1064 }
1065
1066 return ret;
1067 }
1068 #endif /* CONFIG_COMPAT */
1069
1070 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1071 {
1072 #ifdef CONFIG_COMPAT
1073 if (is_compat_thread(task_thread_info(task)))
1074 return &user_aarch32_view;
1075 #endif
1076 return &user_aarch64_view;
1077 }
1078
1079 long arch_ptrace(struct task_struct *child, long request,
1080 unsigned long addr, unsigned long data)
1081 {
1082 int ret;
1083
1084 switch (request) {
1085 case PTRACE_SET_SYSCALL:
1086 task_pt_regs(child)->syscallno = data;
1087 ret = 0;
1088 break;
1089 default:
1090 ret = ptrace_request(child, request, addr, data);
1091 break;
1092 }
1093
1094 return ret;
1095 }
1096
1097 enum ptrace_syscall_dir {
1098 PTRACE_SYSCALL_ENTER = 0,
1099 PTRACE_SYSCALL_EXIT,
1100 };
1101
1102 static void tracehook_report_syscall(struct pt_regs *regs,
1103 enum ptrace_syscall_dir dir)
1104 {
1105 int regno;
1106 unsigned long saved_reg;
1107
1108 /*
1109 * A scratch register (ip(r12) on AArch32, x7 on AArch64) is
1110 * used to denote syscall entry/exit:
1111 */
1112 regno = (is_compat_task() ? 12 : 7);
1113 saved_reg = regs->regs[regno];
1114 regs->regs[regno] = dir;
1115
1116 if (dir == PTRACE_SYSCALL_EXIT)
1117 tracehook_report_syscall_exit(regs, 0);
1118 else if (tracehook_report_syscall_entry(regs))
1119 regs->syscallno = ~0UL;
1120
1121 regs->regs[regno] = saved_reg;
1122 }
1123
1124 asmlinkage int syscall_trace_enter(struct pt_regs *regs)
1125 {
1126 unsigned int saved_syscallno = regs->syscallno;
1127
1128 /* Do the secure computing check first; failures should be fast. */
1129 if (secure_computing(regs->syscallno) == -1)
1130 return RET_SKIP_SYSCALL_TRACE;
1131
1132 if (test_thread_flag(TIF_SYSCALL_TRACE))
1133 tracehook_report_syscall(regs, PTRACE_SYSCALL_ENTER);
1134
1135 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
1136 trace_sys_enter(regs, regs->syscallno);
1137
1138 if (IS_SKIP_SYSCALL(regs->syscallno)) {
1139 /*
1140 * RESTRICTION: we can't modify a return value of user
1141 * issued syscall(-1) here. In order to ease this flavor,
1142 * we need to treat whatever value in x0 as a return value,
1143 * but this might result in a bogus value being returned.
1144 */
1145 /*
1146 * NOTE: syscallno may also be set to -1 if fatal signal is
1147 * detected in tracehook_report_syscall_entry(), but since
1148 * a value set to x0 here is not used in this case, we may
1149 * neglect the case.
1150 */
1151 if (!test_thread_flag(TIF_SYSCALL_TRACE) ||
1152 (IS_SKIP_SYSCALL(saved_syscallno)))
1153 regs->regs[0] = -ENOSYS;
1154 }
1155
1156 audit_syscall_entry(syscall_get_arch(), regs->syscallno,
1157 regs->orig_x0, regs->regs[1], regs->regs[2], regs->regs[3]);
1158
1159 return regs->syscallno;
1160 }
1161
1162 asmlinkage void syscall_trace_exit(struct pt_regs *regs)
1163 {
1164 audit_syscall_exit(regs);
1165
1166 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
1167 trace_sys_exit(regs, regs_return_value(regs));
1168
1169 if (test_thread_flag(TIF_SYSCALL_TRACE))
1170 tracehook_report_syscall(regs, PTRACE_SYSCALL_EXIT);
1171 }