x86/efi: Enforce CONFIG_RELOCATABLE for EFI boot stub
[linux/fpc-iii.git] / arch / arm64 / kernel / ptrace.c
blob9fa78cd0f092dcd35646d0ef4edc8f3cdf0f75be
1 /*
2 * Based on arch/arm/kernel/ptrace.c
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.
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.
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.
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/>.
22 #include <linux/kernel.h>
23 #include <linux/sched.h>
24 #include <linux/mm.h>
25 #include <linux/smp.h>
26 #include <linux/ptrace.h>
27 #include <linux/user.h>
28 #include <linux/security.h>
29 #include <linux/init.h>
30 #include <linux/signal.h>
31 #include <linux/uaccess.h>
32 #include <linux/perf_event.h>
33 #include <linux/hw_breakpoint.h>
34 #include <linux/regset.h>
35 #include <linux/tracehook.h>
36 #include <linux/elf.h>
38 #include <asm/compat.h>
39 #include <asm/debug-monitors.h>
40 #include <asm/pgtable.h>
41 #include <asm/traps.h>
42 #include <asm/system_misc.h>
45 * TODO: does not yet catch signals sent when the child dies.
46 * in exit.c or in signal.c.
50 * Called by kernel/ptrace.c when detaching..
52 void ptrace_disable(struct task_struct *child)
56 #ifdef CONFIG_HAVE_HW_BREAKPOINT
58 * Handle hitting a HW-breakpoint.
60 static void ptrace_hbptriggered(struct perf_event *bp,
61 struct perf_sample_data *data,
62 struct pt_regs *regs)
64 struct arch_hw_breakpoint *bkpt = counter_arch_bp(bp);
65 siginfo_t info = {
66 .si_signo = SIGTRAP,
67 .si_errno = 0,
68 .si_code = TRAP_HWBKPT,
69 .si_addr = (void __user *)(bkpt->trigger),
72 #ifdef CONFIG_COMPAT
73 int i;
75 if (!is_compat_task())
76 goto send_sig;
78 for (i = 0; i < ARM_MAX_BRP; ++i) {
79 if (current->thread.debug.hbp_break[i] == bp) {
80 info.si_errno = (i << 1) + 1;
81 break;
84 for (i = ARM_MAX_BRP; i < ARM_MAX_HBP_SLOTS && !bp; ++i) {
85 if (current->thread.debug.hbp_watch[i] == bp) {
86 info.si_errno = -((i << 1) + 1);
87 break;
91 send_sig:
92 #endif
93 force_sig_info(SIGTRAP, &info, current);
97 * Unregister breakpoints from this task and reset the pointers in
98 * the thread_struct.
100 void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
102 int i;
103 struct thread_struct *t = &tsk->thread;
105 for (i = 0; i < ARM_MAX_BRP; i++) {
106 if (t->debug.hbp_break[i]) {
107 unregister_hw_breakpoint(t->debug.hbp_break[i]);
108 t->debug.hbp_break[i] = NULL;
112 for (i = 0; i < ARM_MAX_WRP; i++) {
113 if (t->debug.hbp_watch[i]) {
114 unregister_hw_breakpoint(t->debug.hbp_watch[i]);
115 t->debug.hbp_watch[i] = NULL;
120 void ptrace_hw_copy_thread(struct task_struct *tsk)
122 memset(&tsk->thread.debug, 0, sizeof(struct debug_info));
125 static struct perf_event *ptrace_hbp_get_event(unsigned int note_type,
126 struct task_struct *tsk,
127 unsigned long idx)
129 struct perf_event *bp = ERR_PTR(-EINVAL);
131 switch (note_type) {
132 case NT_ARM_HW_BREAK:
133 if (idx < ARM_MAX_BRP)
134 bp = tsk->thread.debug.hbp_break[idx];
135 break;
136 case NT_ARM_HW_WATCH:
137 if (idx < ARM_MAX_WRP)
138 bp = tsk->thread.debug.hbp_watch[idx];
139 break;
142 return bp;
145 static int ptrace_hbp_set_event(unsigned int note_type,
146 struct task_struct *tsk,
147 unsigned long idx,
148 struct perf_event *bp)
150 int err = -EINVAL;
152 switch (note_type) {
153 case NT_ARM_HW_BREAK:
154 if (idx < ARM_MAX_BRP) {
155 tsk->thread.debug.hbp_break[idx] = bp;
156 err = 0;
158 break;
159 case NT_ARM_HW_WATCH:
160 if (idx < ARM_MAX_WRP) {
161 tsk->thread.debug.hbp_watch[idx] = bp;
162 err = 0;
164 break;
167 return err;
170 static struct perf_event *ptrace_hbp_create(unsigned int note_type,
171 struct task_struct *tsk,
172 unsigned long idx)
174 struct perf_event *bp;
175 struct perf_event_attr attr;
176 int err, type;
178 switch (note_type) {
179 case NT_ARM_HW_BREAK:
180 type = HW_BREAKPOINT_X;
181 break;
182 case NT_ARM_HW_WATCH:
183 type = HW_BREAKPOINT_RW;
184 break;
185 default:
186 return ERR_PTR(-EINVAL);
189 ptrace_breakpoint_init(&attr);
192 * Initialise fields to sane defaults
193 * (i.e. values that will pass validation).
195 attr.bp_addr = 0;
196 attr.bp_len = HW_BREAKPOINT_LEN_4;
197 attr.bp_type = type;
198 attr.disabled = 1;
200 bp = register_user_hw_breakpoint(&attr, ptrace_hbptriggered, NULL, tsk);
201 if (IS_ERR(bp))
202 return bp;
204 err = ptrace_hbp_set_event(note_type, tsk, idx, bp);
205 if (err)
206 return ERR_PTR(err);
208 return bp;
211 static int ptrace_hbp_fill_attr_ctrl(unsigned int note_type,
212 struct arch_hw_breakpoint_ctrl ctrl,
213 struct perf_event_attr *attr)
215 int err, len, type, disabled = !ctrl.enabled;
217 attr->disabled = disabled;
218 if (disabled)
219 return 0;
221 err = arch_bp_generic_fields(ctrl, &len, &type);
222 if (err)
223 return err;
225 switch (note_type) {
226 case NT_ARM_HW_BREAK:
227 if ((type & HW_BREAKPOINT_X) != type)
228 return -EINVAL;
229 break;
230 case NT_ARM_HW_WATCH:
231 if ((type & HW_BREAKPOINT_RW) != type)
232 return -EINVAL;
233 break;
234 default:
235 return -EINVAL;
238 attr->bp_len = len;
239 attr->bp_type = type;
241 return 0;
244 static int ptrace_hbp_get_resource_info(unsigned int note_type, u32 *info)
246 u8 num;
247 u32 reg = 0;
249 switch (note_type) {
250 case NT_ARM_HW_BREAK:
251 num = hw_breakpoint_slots(TYPE_INST);
252 break;
253 case NT_ARM_HW_WATCH:
254 num = hw_breakpoint_slots(TYPE_DATA);
255 break;
256 default:
257 return -EINVAL;
260 reg |= debug_monitors_arch();
261 reg <<= 8;
262 reg |= num;
264 *info = reg;
265 return 0;
268 static int ptrace_hbp_get_ctrl(unsigned int note_type,
269 struct task_struct *tsk,
270 unsigned long idx,
271 u32 *ctrl)
273 struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
275 if (IS_ERR(bp))
276 return PTR_ERR(bp);
278 *ctrl = bp ? encode_ctrl_reg(counter_arch_bp(bp)->ctrl) : 0;
279 return 0;
282 static int ptrace_hbp_get_addr(unsigned int note_type,
283 struct task_struct *tsk,
284 unsigned long idx,
285 u64 *addr)
287 struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
289 if (IS_ERR(bp))
290 return PTR_ERR(bp);
292 *addr = bp ? bp->attr.bp_addr : 0;
293 return 0;
296 static struct perf_event *ptrace_hbp_get_initialised_bp(unsigned int note_type,
297 struct task_struct *tsk,
298 unsigned long idx)
300 struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
302 if (!bp)
303 bp = ptrace_hbp_create(note_type, tsk, idx);
305 return bp;
308 static int ptrace_hbp_set_ctrl(unsigned int note_type,
309 struct task_struct *tsk,
310 unsigned long idx,
311 u32 uctrl)
313 int err;
314 struct perf_event *bp;
315 struct perf_event_attr attr;
316 struct arch_hw_breakpoint_ctrl ctrl;
318 bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
319 if (IS_ERR(bp)) {
320 err = PTR_ERR(bp);
321 return err;
324 attr = bp->attr;
325 decode_ctrl_reg(uctrl, &ctrl);
326 err = ptrace_hbp_fill_attr_ctrl(note_type, ctrl, &attr);
327 if (err)
328 return err;
330 return modify_user_hw_breakpoint(bp, &attr);
333 static int ptrace_hbp_set_addr(unsigned int note_type,
334 struct task_struct *tsk,
335 unsigned long idx,
336 u64 addr)
338 int err;
339 struct perf_event *bp;
340 struct perf_event_attr attr;
342 bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
343 if (IS_ERR(bp)) {
344 err = PTR_ERR(bp);
345 return err;
348 attr = bp->attr;
349 attr.bp_addr = addr;
350 err = modify_user_hw_breakpoint(bp, &attr);
351 return err;
354 #define PTRACE_HBP_ADDR_SZ sizeof(u64)
355 #define PTRACE_HBP_CTRL_SZ sizeof(u32)
356 #define PTRACE_HBP_PAD_SZ sizeof(u32)
358 static int hw_break_get(struct task_struct *target,
359 const struct user_regset *regset,
360 unsigned int pos, unsigned int count,
361 void *kbuf, void __user *ubuf)
363 unsigned int note_type = regset->core_note_type;
364 int ret, idx = 0, offset, limit;
365 u32 info, ctrl;
366 u64 addr;
368 /* Resource info */
369 ret = ptrace_hbp_get_resource_info(note_type, &info);
370 if (ret)
371 return ret;
373 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &info, 0,
374 sizeof(info));
375 if (ret)
376 return ret;
378 /* Pad */
379 offset = offsetof(struct user_hwdebug_state, pad);
380 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf, offset,
381 offset + PTRACE_HBP_PAD_SZ);
382 if (ret)
383 return ret;
385 /* (address, ctrl) registers */
386 offset = offsetof(struct user_hwdebug_state, dbg_regs);
387 limit = regset->n * regset->size;
388 while (count && offset < limit) {
389 ret = ptrace_hbp_get_addr(note_type, target, idx, &addr);
390 if (ret)
391 return ret;
392 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &addr,
393 offset, offset + PTRACE_HBP_ADDR_SZ);
394 if (ret)
395 return ret;
396 offset += PTRACE_HBP_ADDR_SZ;
398 ret = ptrace_hbp_get_ctrl(note_type, target, idx, &ctrl);
399 if (ret)
400 return ret;
401 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &ctrl,
402 offset, offset + PTRACE_HBP_CTRL_SZ);
403 if (ret)
404 return ret;
405 offset += PTRACE_HBP_CTRL_SZ;
407 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
408 offset,
409 offset + PTRACE_HBP_PAD_SZ);
410 if (ret)
411 return ret;
412 offset += PTRACE_HBP_PAD_SZ;
413 idx++;
416 return 0;
419 static int hw_break_set(struct task_struct *target,
420 const struct user_regset *regset,
421 unsigned int pos, unsigned int count,
422 const void *kbuf, const void __user *ubuf)
424 unsigned int note_type = regset->core_note_type;
425 int ret, idx = 0, offset, limit;
426 u32 ctrl;
427 u64 addr;
429 /* Resource info and pad */
430 offset = offsetof(struct user_hwdebug_state, dbg_regs);
431 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, 0, offset);
432 if (ret)
433 return ret;
435 /* (address, ctrl) registers */
436 limit = regset->n * regset->size;
437 while (count && offset < limit) {
438 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &addr,
439 offset, offset + PTRACE_HBP_ADDR_SZ);
440 if (ret)
441 return ret;
442 ret = ptrace_hbp_set_addr(note_type, target, idx, addr);
443 if (ret)
444 return ret;
445 offset += PTRACE_HBP_ADDR_SZ;
447 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ctrl,
448 offset, offset + PTRACE_HBP_CTRL_SZ);
449 if (ret)
450 return ret;
451 ret = ptrace_hbp_set_ctrl(note_type, target, idx, ctrl);
452 if (ret)
453 return ret;
454 offset += PTRACE_HBP_CTRL_SZ;
456 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
457 offset,
458 offset + PTRACE_HBP_PAD_SZ);
459 if (ret)
460 return ret;
461 offset += PTRACE_HBP_PAD_SZ;
462 idx++;
465 return 0;
467 #endif /* CONFIG_HAVE_HW_BREAKPOINT */
469 static int gpr_get(struct task_struct *target,
470 const struct user_regset *regset,
471 unsigned int pos, unsigned int count,
472 void *kbuf, void __user *ubuf)
474 struct user_pt_regs *uregs = &task_pt_regs(target)->user_regs;
475 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0, -1);
478 static int gpr_set(struct task_struct *target, const struct user_regset *regset,
479 unsigned int pos, unsigned int count,
480 const void *kbuf, const void __user *ubuf)
482 int ret;
483 struct user_pt_regs newregs;
485 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newregs, 0, -1);
486 if (ret)
487 return ret;
489 if (!valid_user_regs(&newregs))
490 return -EINVAL;
492 task_pt_regs(target)->user_regs = newregs;
493 return 0;
497 * TODO: update fp accessors for lazy context switching (sync/flush hwstate)
499 static int fpr_get(struct task_struct *target, const struct user_regset *regset,
500 unsigned int pos, unsigned int count,
501 void *kbuf, void __user *ubuf)
503 struct user_fpsimd_state *uregs;
504 uregs = &target->thread.fpsimd_state.user_fpsimd;
505 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0, -1);
508 static int fpr_set(struct task_struct *target, const struct user_regset *regset,
509 unsigned int pos, unsigned int count,
510 const void *kbuf, const void __user *ubuf)
512 int ret;
513 struct user_fpsimd_state newstate;
515 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newstate, 0, -1);
516 if (ret)
517 return ret;
519 target->thread.fpsimd_state.user_fpsimd = newstate;
520 return ret;
523 static int tls_get(struct task_struct *target, const struct user_regset *regset,
524 unsigned int pos, unsigned int count,
525 void *kbuf, void __user *ubuf)
527 unsigned long *tls = &target->thread.tp_value;
528 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, tls, 0, -1);
531 static int tls_set(struct task_struct *target, const struct user_regset *regset,
532 unsigned int pos, unsigned int count,
533 const void *kbuf, const void __user *ubuf)
535 int ret;
536 unsigned long tls;
538 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
539 if (ret)
540 return ret;
542 target->thread.tp_value = tls;
543 return ret;
546 enum aarch64_regset {
547 REGSET_GPR,
548 REGSET_FPR,
549 REGSET_TLS,
550 #ifdef CONFIG_HAVE_HW_BREAKPOINT
551 REGSET_HW_BREAK,
552 REGSET_HW_WATCH,
553 #endif
556 static const struct user_regset aarch64_regsets[] = {
557 [REGSET_GPR] = {
558 .core_note_type = NT_PRSTATUS,
559 .n = sizeof(struct user_pt_regs) / sizeof(u64),
560 .size = sizeof(u64),
561 .align = sizeof(u64),
562 .get = gpr_get,
563 .set = gpr_set
565 [REGSET_FPR] = {
566 .core_note_type = NT_PRFPREG,
567 .n = sizeof(struct user_fpsimd_state) / sizeof(u32),
569 * We pretend we have 32-bit registers because the fpsr and
570 * fpcr are 32-bits wide.
572 .size = sizeof(u32),
573 .align = sizeof(u32),
574 .get = fpr_get,
575 .set = fpr_set
577 [REGSET_TLS] = {
578 .core_note_type = NT_ARM_TLS,
579 .n = 1,
580 .size = sizeof(void *),
581 .align = sizeof(void *),
582 .get = tls_get,
583 .set = tls_set,
585 #ifdef CONFIG_HAVE_HW_BREAKPOINT
586 [REGSET_HW_BREAK] = {
587 .core_note_type = NT_ARM_HW_BREAK,
588 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
589 .size = sizeof(u32),
590 .align = sizeof(u32),
591 .get = hw_break_get,
592 .set = hw_break_set,
594 [REGSET_HW_WATCH] = {
595 .core_note_type = NT_ARM_HW_WATCH,
596 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
597 .size = sizeof(u32),
598 .align = sizeof(u32),
599 .get = hw_break_get,
600 .set = hw_break_set,
602 #endif
605 static const struct user_regset_view user_aarch64_view = {
606 .name = "aarch64", .e_machine = EM_AARCH64,
607 .regsets = aarch64_regsets, .n = ARRAY_SIZE(aarch64_regsets)
610 #ifdef CONFIG_COMPAT
611 #include <linux/compat.h>
613 enum compat_regset {
614 REGSET_COMPAT_GPR,
615 REGSET_COMPAT_VFP,
618 static int compat_gpr_get(struct task_struct *target,
619 const struct user_regset *regset,
620 unsigned int pos, unsigned int count,
621 void *kbuf, void __user *ubuf)
623 int ret = 0;
624 unsigned int i, start, num_regs;
626 /* Calculate the number of AArch32 registers contained in count */
627 num_regs = count / regset->size;
629 /* Convert pos into an register number */
630 start = pos / regset->size;
632 if (start + num_regs > regset->n)
633 return -EIO;
635 for (i = 0; i < num_regs; ++i) {
636 unsigned int idx = start + i;
637 void *reg;
639 switch (idx) {
640 case 15:
641 reg = (void *)&task_pt_regs(target)->pc;
642 break;
643 case 16:
644 reg = (void *)&task_pt_regs(target)->pstate;
645 break;
646 case 17:
647 reg = (void *)&task_pt_regs(target)->orig_x0;
648 break;
649 default:
650 reg = (void *)&task_pt_regs(target)->regs[idx];
653 ret = copy_to_user(ubuf, reg, sizeof(compat_ulong_t));
655 if (ret)
656 break;
657 else
658 ubuf += sizeof(compat_ulong_t);
661 return ret;
664 static int compat_gpr_set(struct task_struct *target,
665 const struct user_regset *regset,
666 unsigned int pos, unsigned int count,
667 const void *kbuf, const void __user *ubuf)
669 struct pt_regs newregs;
670 int ret = 0;
671 unsigned int i, start, num_regs;
673 /* Calculate the number of AArch32 registers contained in count */
674 num_regs = count / regset->size;
676 /* Convert pos into an register number */
677 start = pos / regset->size;
679 if (start + num_regs > regset->n)
680 return -EIO;
682 newregs = *task_pt_regs(target);
684 for (i = 0; i < num_regs; ++i) {
685 unsigned int idx = start + i;
686 void *reg;
688 switch (idx) {
689 case 15:
690 reg = (void *)&newregs.pc;
691 break;
692 case 16:
693 reg = (void *)&newregs.pstate;
694 break;
695 case 17:
696 reg = (void *)&newregs.orig_x0;
697 break;
698 default:
699 reg = (void *)&newregs.regs[idx];
702 ret = copy_from_user(reg, ubuf, sizeof(compat_ulong_t));
704 if (ret)
705 goto out;
706 else
707 ubuf += sizeof(compat_ulong_t);
710 if (valid_user_regs(&newregs.user_regs))
711 *task_pt_regs(target) = newregs;
712 else
713 ret = -EINVAL;
715 out:
716 return ret;
719 static int compat_vfp_get(struct task_struct *target,
720 const struct user_regset *regset,
721 unsigned int pos, unsigned int count,
722 void *kbuf, void __user *ubuf)
724 struct user_fpsimd_state *uregs;
725 compat_ulong_t fpscr;
726 int ret;
728 uregs = &target->thread.fpsimd_state.user_fpsimd;
731 * The VFP registers are packed into the fpsimd_state, so they all sit
732 * nicely together for us. We just need to create the fpscr separately.
734 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0,
735 VFP_STATE_SIZE - sizeof(compat_ulong_t));
737 if (count && !ret) {
738 fpscr = (uregs->fpsr & VFP_FPSCR_STAT_MASK) |
739 (uregs->fpcr & VFP_FPSCR_CTRL_MASK);
740 ret = put_user(fpscr, (compat_ulong_t *)ubuf);
743 return ret;
746 static int compat_vfp_set(struct task_struct *target,
747 const struct user_regset *regset,
748 unsigned int pos, unsigned int count,
749 const void *kbuf, const void __user *ubuf)
751 struct user_fpsimd_state *uregs;
752 compat_ulong_t fpscr;
753 int ret;
755 if (pos + count > VFP_STATE_SIZE)
756 return -EIO;
758 uregs = &target->thread.fpsimd_state.user_fpsimd;
760 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, uregs, 0,
761 VFP_STATE_SIZE - sizeof(compat_ulong_t));
763 if (count && !ret) {
764 ret = get_user(fpscr, (compat_ulong_t *)ubuf);
765 uregs->fpsr = fpscr & VFP_FPSCR_STAT_MASK;
766 uregs->fpcr = fpscr & VFP_FPSCR_CTRL_MASK;
769 return ret;
772 static const struct user_regset aarch32_regsets[] = {
773 [REGSET_COMPAT_GPR] = {
774 .core_note_type = NT_PRSTATUS,
775 .n = COMPAT_ELF_NGREG,
776 .size = sizeof(compat_elf_greg_t),
777 .align = sizeof(compat_elf_greg_t),
778 .get = compat_gpr_get,
779 .set = compat_gpr_set
781 [REGSET_COMPAT_VFP] = {
782 .core_note_type = NT_ARM_VFP,
783 .n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
784 .size = sizeof(compat_ulong_t),
785 .align = sizeof(compat_ulong_t),
786 .get = compat_vfp_get,
787 .set = compat_vfp_set
791 static const struct user_regset_view user_aarch32_view = {
792 .name = "aarch32", .e_machine = EM_ARM,
793 .regsets = aarch32_regsets, .n = ARRAY_SIZE(aarch32_regsets)
796 static int compat_ptrace_read_user(struct task_struct *tsk, compat_ulong_t off,
797 compat_ulong_t __user *ret)
799 compat_ulong_t tmp;
801 if (off & 3)
802 return -EIO;
804 if (off == COMPAT_PT_TEXT_ADDR)
805 tmp = tsk->mm->start_code;
806 else if (off == COMPAT_PT_DATA_ADDR)
807 tmp = tsk->mm->start_data;
808 else if (off == COMPAT_PT_TEXT_END_ADDR)
809 tmp = tsk->mm->end_code;
810 else if (off < sizeof(compat_elf_gregset_t))
811 return copy_regset_to_user(tsk, &user_aarch32_view,
812 REGSET_COMPAT_GPR, off,
813 sizeof(compat_ulong_t), ret);
814 else if (off >= COMPAT_USER_SZ)
815 return -EIO;
816 else
817 tmp = 0;
819 return put_user(tmp, ret);
822 static int compat_ptrace_write_user(struct task_struct *tsk, compat_ulong_t off,
823 compat_ulong_t val)
825 int ret;
826 mm_segment_t old_fs = get_fs();
828 if (off & 3 || off >= COMPAT_USER_SZ)
829 return -EIO;
831 if (off >= sizeof(compat_elf_gregset_t))
832 return 0;
834 set_fs(KERNEL_DS);
835 ret = copy_regset_from_user(tsk, &user_aarch32_view,
836 REGSET_COMPAT_GPR, off,
837 sizeof(compat_ulong_t),
838 &val);
839 set_fs(old_fs);
841 return ret;
844 #ifdef CONFIG_HAVE_HW_BREAKPOINT
847 * Convert a virtual register number into an index for a thread_info
848 * breakpoint array. Breakpoints are identified using positive numbers
849 * whilst watchpoints are negative. The registers are laid out as pairs
850 * of (address, control), each pair mapping to a unique hw_breakpoint struct.
851 * Register 0 is reserved for describing resource information.
853 static int compat_ptrace_hbp_num_to_idx(compat_long_t num)
855 return (abs(num) - 1) >> 1;
858 static int compat_ptrace_hbp_get_resource_info(u32 *kdata)
860 u8 num_brps, num_wrps, debug_arch, wp_len;
861 u32 reg = 0;
863 num_brps = hw_breakpoint_slots(TYPE_INST);
864 num_wrps = hw_breakpoint_slots(TYPE_DATA);
866 debug_arch = debug_monitors_arch();
867 wp_len = 8;
868 reg |= debug_arch;
869 reg <<= 8;
870 reg |= wp_len;
871 reg <<= 8;
872 reg |= num_wrps;
873 reg <<= 8;
874 reg |= num_brps;
876 *kdata = reg;
877 return 0;
880 static int compat_ptrace_hbp_get(unsigned int note_type,
881 struct task_struct *tsk,
882 compat_long_t num,
883 u32 *kdata)
885 u64 addr = 0;
886 u32 ctrl = 0;
888 int err, idx = compat_ptrace_hbp_num_to_idx(num);;
890 if (num & 1) {
891 err = ptrace_hbp_get_addr(note_type, tsk, idx, &addr);
892 *kdata = (u32)addr;
893 } else {
894 err = ptrace_hbp_get_ctrl(note_type, tsk, idx, &ctrl);
895 *kdata = ctrl;
898 return err;
901 static int compat_ptrace_hbp_set(unsigned int note_type,
902 struct task_struct *tsk,
903 compat_long_t num,
904 u32 *kdata)
906 u64 addr;
907 u32 ctrl;
909 int err, idx = compat_ptrace_hbp_num_to_idx(num);
911 if (num & 1) {
912 addr = *kdata;
913 err = ptrace_hbp_set_addr(note_type, tsk, idx, addr);
914 } else {
915 ctrl = *kdata;
916 err = ptrace_hbp_set_ctrl(note_type, tsk, idx, ctrl);
919 return err;
922 static int compat_ptrace_gethbpregs(struct task_struct *tsk, compat_long_t num,
923 compat_ulong_t __user *data)
925 int ret;
926 u32 kdata;
927 mm_segment_t old_fs = get_fs();
929 set_fs(KERNEL_DS);
930 /* Watchpoint */
931 if (num < 0) {
932 ret = compat_ptrace_hbp_get(NT_ARM_HW_WATCH, tsk, num, &kdata);
933 /* Resource info */
934 } else if (num == 0) {
935 ret = compat_ptrace_hbp_get_resource_info(&kdata);
936 /* Breakpoint */
937 } else {
938 ret = compat_ptrace_hbp_get(NT_ARM_HW_BREAK, tsk, num, &kdata);
940 set_fs(old_fs);
942 if (!ret)
943 ret = put_user(kdata, data);
945 return ret;
948 static int compat_ptrace_sethbpregs(struct task_struct *tsk, compat_long_t num,
949 compat_ulong_t __user *data)
951 int ret;
952 u32 kdata = 0;
953 mm_segment_t old_fs = get_fs();
955 if (num == 0)
956 return 0;
958 ret = get_user(kdata, data);
959 if (ret)
960 return ret;
962 set_fs(KERNEL_DS);
963 if (num < 0)
964 ret = compat_ptrace_hbp_set(NT_ARM_HW_WATCH, tsk, num, &kdata);
965 else
966 ret = compat_ptrace_hbp_set(NT_ARM_HW_BREAK, tsk, num, &kdata);
967 set_fs(old_fs);
969 return ret;
971 #endif /* CONFIG_HAVE_HW_BREAKPOINT */
973 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
974 compat_ulong_t caddr, compat_ulong_t cdata)
976 unsigned long addr = caddr;
977 unsigned long data = cdata;
978 void __user *datap = compat_ptr(data);
979 int ret;
981 switch (request) {
982 case PTRACE_PEEKUSR:
983 ret = compat_ptrace_read_user(child, addr, datap);
984 break;
986 case PTRACE_POKEUSR:
987 ret = compat_ptrace_write_user(child, addr, data);
988 break;
990 case COMPAT_PTRACE_GETREGS:
991 ret = copy_regset_to_user(child,
992 &user_aarch32_view,
993 REGSET_COMPAT_GPR,
994 0, sizeof(compat_elf_gregset_t),
995 datap);
996 break;
998 case COMPAT_PTRACE_SETREGS:
999 ret = copy_regset_from_user(child,
1000 &user_aarch32_view,
1001 REGSET_COMPAT_GPR,
1002 0, sizeof(compat_elf_gregset_t),
1003 datap);
1004 break;
1006 case COMPAT_PTRACE_GET_THREAD_AREA:
1007 ret = put_user((compat_ulong_t)child->thread.tp_value,
1008 (compat_ulong_t __user *)datap);
1009 break;
1011 case COMPAT_PTRACE_SET_SYSCALL:
1012 task_pt_regs(child)->syscallno = data;
1013 ret = 0;
1014 break;
1016 case COMPAT_PTRACE_GETVFPREGS:
1017 ret = copy_regset_to_user(child,
1018 &user_aarch32_view,
1019 REGSET_COMPAT_VFP,
1020 0, VFP_STATE_SIZE,
1021 datap);
1022 break;
1024 case COMPAT_PTRACE_SETVFPREGS:
1025 ret = copy_regset_from_user(child,
1026 &user_aarch32_view,
1027 REGSET_COMPAT_VFP,
1028 0, VFP_STATE_SIZE,
1029 datap);
1030 break;
1032 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1033 case COMPAT_PTRACE_GETHBPREGS:
1034 ret = compat_ptrace_gethbpregs(child, addr, datap);
1035 break;
1037 case COMPAT_PTRACE_SETHBPREGS:
1038 ret = compat_ptrace_sethbpregs(child, addr, datap);
1039 break;
1040 #endif
1042 default:
1043 ret = compat_ptrace_request(child, request, addr,
1044 data);
1045 break;
1048 return ret;
1050 #endif /* CONFIG_COMPAT */
1052 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1054 #ifdef CONFIG_COMPAT
1055 if (is_compat_thread(task_thread_info(task)))
1056 return &user_aarch32_view;
1057 #endif
1058 return &user_aarch64_view;
1061 long arch_ptrace(struct task_struct *child, long request,
1062 unsigned long addr, unsigned long data)
1064 return ptrace_request(child, request, addr, data);
1067 asmlinkage int syscall_trace(int dir, struct pt_regs *regs)
1069 unsigned long saved_reg;
1071 if (!test_thread_flag(TIF_SYSCALL_TRACE))
1072 return regs->syscallno;
1074 if (is_compat_task()) {
1075 /* AArch32 uses ip (r12) for scratch */
1076 saved_reg = regs->regs[12];
1077 regs->regs[12] = dir;
1078 } else {
1080 * Save X7. X7 is used to denote syscall entry/exit:
1081 * X7 = 0 -> entry, = 1 -> exit
1083 saved_reg = regs->regs[7];
1084 regs->regs[7] = dir;
1087 if (dir)
1088 tracehook_report_syscall_exit(regs, 0);
1089 else if (tracehook_report_syscall_entry(regs))
1090 regs->syscallno = ~0UL;
1092 if (is_compat_task())
1093 regs->regs[12] = saved_reg;
1094 else
1095 regs->regs[7] = saved_reg;
1097 return regs->syscallno;