2 * Based on arch/arm/kernel/ptrace.c
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/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>
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/string.h>
37 #include <linux/uaccess.h>
38 #include <linux/perf_event.h>
39 #include <linux/hw_breakpoint.h>
40 #include <linux/regset.h>
41 #include <linux/tracehook.h>
42 #include <linux/elf.h>
44 #include <asm/compat.h>
45 #include <asm/cpufeature.h>
46 #include <asm/debug-monitors.h>
47 #include <asm/pgtable.h>
48 #include <asm/stacktrace.h>
49 #include <asm/syscall.h>
50 #include <asm/traps.h>
51 #include <asm/system_misc.h>
53 #define CREATE_TRACE_POINTS
54 #include <trace/events/syscalls.h>
56 struct pt_regs_offset {
61 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
62 #define REG_OFFSET_END {.name = NULL, .offset = 0}
63 #define GPR_OFFSET_NAME(r) \
64 {.name = "x" #r, .offset = offsetof(struct pt_regs, regs[r])}
66 static const struct pt_regs_offset regoffset_table[] = {
98 {.name = "lr", .offset = offsetof(struct pt_regs, regs[30])},
101 REG_OFFSET_NAME(pstate),
106 * regs_query_register_offset() - query register offset from its name
107 * @name: the name of a register
109 * regs_query_register_offset() returns the offset of a register in struct
110 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
112 int regs_query_register_offset(const char *name)
114 const struct pt_regs_offset *roff;
116 for (roff = regoffset_table; roff->name != NULL; roff++)
117 if (!strcmp(roff->name, name))
123 * regs_within_kernel_stack() - check the address in the stack
124 * @regs: pt_regs which contains kernel stack pointer.
125 * @addr: address which is checked.
127 * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
128 * If @addr is within the kernel stack, it returns true. If not, returns false.
130 static bool regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
132 return ((addr & ~(THREAD_SIZE - 1)) ==
133 (kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1))) ||
138 * regs_get_kernel_stack_nth() - get Nth entry of the stack
139 * @regs: pt_regs which contains kernel stack pointer.
140 * @n: stack entry number.
142 * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
143 * is specified by @regs. If the @n th entry is NOT in the kernel stack,
146 unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
148 unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
151 if (regs_within_kernel_stack(regs, (unsigned long)addr))
158 * TODO: does not yet catch signals sent when the child dies.
159 * in exit.c or in signal.c.
163 * Called by kernel/ptrace.c when detaching..
165 void ptrace_disable(struct task_struct *child)
168 * This would be better off in core code, but PTRACE_DETACH has
169 * grown its fair share of arch-specific worts and changing it
170 * is likely to cause regressions on obscure architectures.
172 user_disable_single_step(child);
175 #ifdef CONFIG_HAVE_HW_BREAKPOINT
177 * Handle hitting a HW-breakpoint.
179 static void ptrace_hbptriggered(struct perf_event *bp,
180 struct perf_sample_data *data,
181 struct pt_regs *regs)
183 struct arch_hw_breakpoint *bkpt = counter_arch_bp(bp);
186 clear_siginfo(&info);
187 info.si_signo = SIGTRAP;
189 info.si_code = TRAP_HWBKPT;
190 info.si_addr = (void __user *)(bkpt->trigger);
193 if (is_compat_task()) {
197 for (i = 0; i < ARM_MAX_BRP; ++i) {
198 if (current->thread.debug.hbp_break[i] == bp) {
199 si_errno = (i << 1) + 1;
204 for (i = 0; i < ARM_MAX_WRP; ++i) {
205 if (current->thread.debug.hbp_watch[i] == bp) {
206 si_errno = -((i << 1) + 1);
210 force_sig_ptrace_errno_trap(si_errno, (void __user *)bkpt->trigger);
213 arm64_force_sig_info(&info, "Hardware breakpoint trap (ptrace)", current);
217 * Unregister breakpoints from this task and reset the pointers in
220 void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
223 struct thread_struct *t = &tsk->thread;
225 for (i = 0; i < ARM_MAX_BRP; i++) {
226 if (t->debug.hbp_break[i]) {
227 unregister_hw_breakpoint(t->debug.hbp_break[i]);
228 t->debug.hbp_break[i] = NULL;
232 for (i = 0; i < ARM_MAX_WRP; i++) {
233 if (t->debug.hbp_watch[i]) {
234 unregister_hw_breakpoint(t->debug.hbp_watch[i]);
235 t->debug.hbp_watch[i] = NULL;
240 void ptrace_hw_copy_thread(struct task_struct *tsk)
242 memset(&tsk->thread.debug, 0, sizeof(struct debug_info));
245 static struct perf_event *ptrace_hbp_get_event(unsigned int note_type,
246 struct task_struct *tsk,
249 struct perf_event *bp = ERR_PTR(-EINVAL);
252 case NT_ARM_HW_BREAK:
253 if (idx >= ARM_MAX_BRP)
255 idx = array_index_nospec(idx, ARM_MAX_BRP);
256 bp = tsk->thread.debug.hbp_break[idx];
258 case NT_ARM_HW_WATCH:
259 if (idx >= ARM_MAX_WRP)
261 idx = array_index_nospec(idx, ARM_MAX_WRP);
262 bp = tsk->thread.debug.hbp_watch[idx];
270 static int ptrace_hbp_set_event(unsigned int note_type,
271 struct task_struct *tsk,
273 struct perf_event *bp)
278 case NT_ARM_HW_BREAK:
279 if (idx < ARM_MAX_BRP) {
280 tsk->thread.debug.hbp_break[idx] = bp;
284 case NT_ARM_HW_WATCH:
285 if (idx < ARM_MAX_WRP) {
286 tsk->thread.debug.hbp_watch[idx] = bp;
295 static struct perf_event *ptrace_hbp_create(unsigned int note_type,
296 struct task_struct *tsk,
299 struct perf_event *bp;
300 struct perf_event_attr attr;
304 case NT_ARM_HW_BREAK:
305 type = HW_BREAKPOINT_X;
307 case NT_ARM_HW_WATCH:
308 type = HW_BREAKPOINT_RW;
311 return ERR_PTR(-EINVAL);
314 ptrace_breakpoint_init(&attr);
317 * Initialise fields to sane defaults
318 * (i.e. values that will pass validation).
321 attr.bp_len = HW_BREAKPOINT_LEN_4;
325 bp = register_user_hw_breakpoint(&attr, ptrace_hbptriggered, NULL, tsk);
329 err = ptrace_hbp_set_event(note_type, tsk, idx, bp);
336 static int ptrace_hbp_fill_attr_ctrl(unsigned int note_type,
337 struct arch_hw_breakpoint_ctrl ctrl,
338 struct perf_event_attr *attr)
340 int err, len, type, offset, disabled = !ctrl.enabled;
342 attr->disabled = disabled;
346 err = arch_bp_generic_fields(ctrl, &len, &type, &offset);
351 case NT_ARM_HW_BREAK:
352 if ((type & HW_BREAKPOINT_X) != type)
355 case NT_ARM_HW_WATCH:
356 if ((type & HW_BREAKPOINT_RW) != type)
364 attr->bp_type = type;
365 attr->bp_addr += offset;
370 static int ptrace_hbp_get_resource_info(unsigned int note_type, u32 *info)
376 case NT_ARM_HW_BREAK:
377 num = hw_breakpoint_slots(TYPE_INST);
379 case NT_ARM_HW_WATCH:
380 num = hw_breakpoint_slots(TYPE_DATA);
386 reg |= debug_monitors_arch();
394 static int ptrace_hbp_get_ctrl(unsigned int note_type,
395 struct task_struct *tsk,
399 struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
404 *ctrl = bp ? encode_ctrl_reg(counter_arch_bp(bp)->ctrl) : 0;
408 static int ptrace_hbp_get_addr(unsigned int note_type,
409 struct task_struct *tsk,
413 struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
418 *addr = bp ? counter_arch_bp(bp)->address : 0;
422 static struct perf_event *ptrace_hbp_get_initialised_bp(unsigned int note_type,
423 struct task_struct *tsk,
426 struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
429 bp = ptrace_hbp_create(note_type, tsk, idx);
434 static int ptrace_hbp_set_ctrl(unsigned int note_type,
435 struct task_struct *tsk,
440 struct perf_event *bp;
441 struct perf_event_attr attr;
442 struct arch_hw_breakpoint_ctrl ctrl;
444 bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
451 decode_ctrl_reg(uctrl, &ctrl);
452 err = ptrace_hbp_fill_attr_ctrl(note_type, ctrl, &attr);
456 return modify_user_hw_breakpoint(bp, &attr);
459 static int ptrace_hbp_set_addr(unsigned int note_type,
460 struct task_struct *tsk,
465 struct perf_event *bp;
466 struct perf_event_attr attr;
468 bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
476 err = modify_user_hw_breakpoint(bp, &attr);
480 #define PTRACE_HBP_ADDR_SZ sizeof(u64)
481 #define PTRACE_HBP_CTRL_SZ sizeof(u32)
482 #define PTRACE_HBP_PAD_SZ sizeof(u32)
484 static int hw_break_get(struct task_struct *target,
485 const struct user_regset *regset,
486 unsigned int pos, unsigned int count,
487 void *kbuf, void __user *ubuf)
489 unsigned int note_type = regset->core_note_type;
490 int ret, idx = 0, offset, limit;
495 ret = ptrace_hbp_get_resource_info(note_type, &info);
499 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &info, 0,
505 offset = offsetof(struct user_hwdebug_state, pad);
506 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf, offset,
507 offset + PTRACE_HBP_PAD_SZ);
511 /* (address, ctrl) registers */
512 offset = offsetof(struct user_hwdebug_state, dbg_regs);
513 limit = regset->n * regset->size;
514 while (count && offset < limit) {
515 ret = ptrace_hbp_get_addr(note_type, target, idx, &addr);
518 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &addr,
519 offset, offset + PTRACE_HBP_ADDR_SZ);
522 offset += PTRACE_HBP_ADDR_SZ;
524 ret = ptrace_hbp_get_ctrl(note_type, target, idx, &ctrl);
527 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &ctrl,
528 offset, offset + PTRACE_HBP_CTRL_SZ);
531 offset += PTRACE_HBP_CTRL_SZ;
533 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
535 offset + PTRACE_HBP_PAD_SZ);
538 offset += PTRACE_HBP_PAD_SZ;
545 static int hw_break_set(struct task_struct *target,
546 const struct user_regset *regset,
547 unsigned int pos, unsigned int count,
548 const void *kbuf, const void __user *ubuf)
550 unsigned int note_type = regset->core_note_type;
551 int ret, idx = 0, offset, limit;
555 /* Resource info and pad */
556 offset = offsetof(struct user_hwdebug_state, dbg_regs);
557 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, 0, offset);
561 /* (address, ctrl) registers */
562 limit = regset->n * regset->size;
563 while (count && offset < limit) {
564 if (count < PTRACE_HBP_ADDR_SZ)
566 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &addr,
567 offset, offset + PTRACE_HBP_ADDR_SZ);
570 ret = ptrace_hbp_set_addr(note_type, target, idx, addr);
573 offset += PTRACE_HBP_ADDR_SZ;
577 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ctrl,
578 offset, offset + PTRACE_HBP_CTRL_SZ);
581 ret = ptrace_hbp_set_ctrl(note_type, target, idx, ctrl);
584 offset += PTRACE_HBP_CTRL_SZ;
586 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
588 offset + PTRACE_HBP_PAD_SZ);
591 offset += PTRACE_HBP_PAD_SZ;
597 #endif /* CONFIG_HAVE_HW_BREAKPOINT */
599 static int gpr_get(struct task_struct *target,
600 const struct user_regset *regset,
601 unsigned int pos, unsigned int count,
602 void *kbuf, void __user *ubuf)
604 struct user_pt_regs *uregs = &task_pt_regs(target)->user_regs;
605 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0, -1);
608 static int gpr_set(struct task_struct *target, const struct user_regset *regset,
609 unsigned int pos, unsigned int count,
610 const void *kbuf, const void __user *ubuf)
613 struct user_pt_regs newregs = task_pt_regs(target)->user_regs;
615 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newregs, 0, -1);
619 if (!valid_user_regs(&newregs, target))
622 task_pt_regs(target)->user_regs = newregs;
627 * TODO: update fp accessors for lazy context switching (sync/flush hwstate)
629 static int __fpr_get(struct task_struct *target,
630 const struct user_regset *regset,
631 unsigned int pos, unsigned int count,
632 void *kbuf, void __user *ubuf, unsigned int start_pos)
634 struct user_fpsimd_state *uregs;
636 sve_sync_to_fpsimd(target);
638 uregs = &target->thread.uw.fpsimd_state;
640 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs,
641 start_pos, start_pos + sizeof(*uregs));
644 static int fpr_get(struct task_struct *target, const struct user_regset *regset,
645 unsigned int pos, unsigned int count,
646 void *kbuf, void __user *ubuf)
648 if (target == current)
649 fpsimd_preserve_current_state();
651 return __fpr_get(target, regset, pos, count, kbuf, ubuf, 0);
654 static int __fpr_set(struct task_struct *target,
655 const struct user_regset *regset,
656 unsigned int pos, unsigned int count,
657 const void *kbuf, const void __user *ubuf,
658 unsigned int start_pos)
661 struct user_fpsimd_state newstate;
664 * Ensure target->thread.uw.fpsimd_state is up to date, so that a
665 * short copyin can't resurrect stale data.
667 sve_sync_to_fpsimd(target);
669 newstate = target->thread.uw.fpsimd_state;
671 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newstate,
672 start_pos, start_pos + sizeof(newstate));
676 target->thread.uw.fpsimd_state = newstate;
681 static int fpr_set(struct task_struct *target, const struct user_regset *regset,
682 unsigned int pos, unsigned int count,
683 const void *kbuf, const void __user *ubuf)
687 ret = __fpr_set(target, regset, pos, count, kbuf, ubuf, 0);
691 sve_sync_from_fpsimd_zeropad(target);
692 fpsimd_flush_task_state(target);
697 static int tls_get(struct task_struct *target, const struct user_regset *regset,
698 unsigned int pos, unsigned int count,
699 void *kbuf, void __user *ubuf)
701 unsigned long *tls = &target->thread.uw.tp_value;
703 if (target == current)
704 tls_preserve_current_state();
706 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, tls, 0, -1);
709 static int tls_set(struct task_struct *target, const struct user_regset *regset,
710 unsigned int pos, unsigned int count,
711 const void *kbuf, const void __user *ubuf)
714 unsigned long tls = target->thread.uw.tp_value;
716 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
720 target->thread.uw.tp_value = tls;
724 static int system_call_get(struct task_struct *target,
725 const struct user_regset *regset,
726 unsigned int pos, unsigned int count,
727 void *kbuf, void __user *ubuf)
729 int syscallno = task_pt_regs(target)->syscallno;
731 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
735 static int system_call_set(struct task_struct *target,
736 const struct user_regset *regset,
737 unsigned int pos, unsigned int count,
738 const void *kbuf, const void __user *ubuf)
740 int syscallno = task_pt_regs(target)->syscallno;
743 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &syscallno, 0, -1);
747 task_pt_regs(target)->syscallno = syscallno;
751 #ifdef CONFIG_ARM64_SVE
753 static void sve_init_header_from_task(struct user_sve_header *header,
754 struct task_struct *target)
758 memset(header, 0, sizeof(*header));
760 header->flags = test_tsk_thread_flag(target, TIF_SVE) ?
761 SVE_PT_REGS_SVE : SVE_PT_REGS_FPSIMD;
762 if (test_tsk_thread_flag(target, TIF_SVE_VL_INHERIT))
763 header->flags |= SVE_PT_VL_INHERIT;
765 header->vl = target->thread.sve_vl;
766 vq = sve_vq_from_vl(header->vl);
768 header->max_vl = sve_max_vl;
769 if (WARN_ON(!sve_vl_valid(sve_max_vl)))
770 header->max_vl = header->vl;
772 header->size = SVE_PT_SIZE(vq, header->flags);
773 header->max_size = SVE_PT_SIZE(sve_vq_from_vl(header->max_vl),
777 static unsigned int sve_size_from_header(struct user_sve_header const *header)
779 return ALIGN(header->size, SVE_VQ_BYTES);
782 static unsigned int sve_get_size(struct task_struct *target,
783 const struct user_regset *regset)
785 struct user_sve_header header;
787 if (!system_supports_sve())
790 sve_init_header_from_task(&header, target);
791 return sve_size_from_header(&header);
794 static int sve_get(struct task_struct *target,
795 const struct user_regset *regset,
796 unsigned int pos, unsigned int count,
797 void *kbuf, void __user *ubuf)
800 struct user_sve_header header;
802 unsigned long start, end;
804 if (!system_supports_sve())
808 sve_init_header_from_task(&header, target);
809 vq = sve_vq_from_vl(header.vl);
811 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &header,
816 if (target == current)
817 fpsimd_preserve_current_state();
819 /* Registers: FPSIMD-only case */
821 BUILD_BUG_ON(SVE_PT_FPSIMD_OFFSET != sizeof(header));
822 if ((header.flags & SVE_PT_REGS_MASK) == SVE_PT_REGS_FPSIMD)
823 return __fpr_get(target, regset, pos, count, kbuf, ubuf,
824 SVE_PT_FPSIMD_OFFSET);
826 /* Otherwise: full SVE case */
828 BUILD_BUG_ON(SVE_PT_SVE_OFFSET != sizeof(header));
829 start = SVE_PT_SVE_OFFSET;
830 end = SVE_PT_SVE_FFR_OFFSET(vq) + SVE_PT_SVE_FFR_SIZE(vq);
831 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
832 target->thread.sve_state,
838 end = SVE_PT_SVE_FPSR_OFFSET(vq);
839 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
845 * Copy fpsr, and fpcr which must follow contiguously in
846 * struct fpsimd_state:
849 end = SVE_PT_SVE_FPCR_OFFSET(vq) + SVE_PT_SVE_FPCR_SIZE;
850 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
851 &target->thread.uw.fpsimd_state.fpsr,
857 end = sve_size_from_header(&header);
858 return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
862 static int sve_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)
868 struct user_sve_header header;
870 unsigned long start, end;
872 if (!system_supports_sve())
876 if (count < sizeof(header))
878 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &header,
884 * Apart from PT_SVE_REGS_MASK, all PT_SVE_* flags are consumed by
885 * sve_set_vector_length(), which will also validate them for us:
887 ret = sve_set_vector_length(target, header.vl,
888 ((unsigned long)header.flags & ~SVE_PT_REGS_MASK) << 16);
892 /* Actual VL set may be less than the user asked for: */
893 vq = sve_vq_from_vl(target->thread.sve_vl);
895 /* Registers: FPSIMD-only case */
897 BUILD_BUG_ON(SVE_PT_FPSIMD_OFFSET != sizeof(header));
898 if ((header.flags & SVE_PT_REGS_MASK) == SVE_PT_REGS_FPSIMD) {
899 ret = __fpr_set(target, regset, pos, count, kbuf, ubuf,
900 SVE_PT_FPSIMD_OFFSET);
901 clear_tsk_thread_flag(target, TIF_SVE);
905 /* Otherwise: full SVE case */
908 * If setting a different VL from the requested VL and there is
909 * register data, the data layout will be wrong: don't even
910 * try to set the registers in this case.
912 if (count && vq != sve_vq_from_vl(header.vl)) {
920 * Ensure target->thread.sve_state is up to date with target's
921 * FPSIMD regs, so that a short copyin leaves trailing registers
924 fpsimd_sync_to_sve(target);
925 set_tsk_thread_flag(target, TIF_SVE);
927 BUILD_BUG_ON(SVE_PT_SVE_OFFSET != sizeof(header));
928 start = SVE_PT_SVE_OFFSET;
929 end = SVE_PT_SVE_FFR_OFFSET(vq) + SVE_PT_SVE_FFR_SIZE(vq);
930 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
931 target->thread.sve_state,
937 end = SVE_PT_SVE_FPSR_OFFSET(vq);
938 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
944 * Copy fpsr, and fpcr which must follow contiguously in
945 * struct fpsimd_state:
948 end = SVE_PT_SVE_FPCR_OFFSET(vq) + SVE_PT_SVE_FPCR_SIZE;
949 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
950 &target->thread.uw.fpsimd_state.fpsr,
954 fpsimd_flush_task_state(target);
958 #endif /* CONFIG_ARM64_SVE */
960 enum aarch64_regset {
964 #ifdef CONFIG_HAVE_HW_BREAKPOINT
969 #ifdef CONFIG_ARM64_SVE
974 static const struct user_regset aarch64_regsets[] = {
976 .core_note_type = NT_PRSTATUS,
977 .n = sizeof(struct user_pt_regs) / sizeof(u64),
979 .align = sizeof(u64),
984 .core_note_type = NT_PRFPREG,
985 .n = sizeof(struct user_fpsimd_state) / sizeof(u32),
987 * We pretend we have 32-bit registers because the fpsr and
988 * fpcr are 32-bits wide.
991 .align = sizeof(u32),
996 .core_note_type = NT_ARM_TLS,
998 .size = sizeof(void *),
999 .align = sizeof(void *),
1003 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1004 [REGSET_HW_BREAK] = {
1005 .core_note_type = NT_ARM_HW_BREAK,
1006 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
1007 .size = sizeof(u32),
1008 .align = sizeof(u32),
1009 .get = hw_break_get,
1010 .set = hw_break_set,
1012 [REGSET_HW_WATCH] = {
1013 .core_note_type = NT_ARM_HW_WATCH,
1014 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
1015 .size = sizeof(u32),
1016 .align = sizeof(u32),
1017 .get = hw_break_get,
1018 .set = hw_break_set,
1021 [REGSET_SYSTEM_CALL] = {
1022 .core_note_type = NT_ARM_SYSTEM_CALL,
1024 .size = sizeof(int),
1025 .align = sizeof(int),
1026 .get = system_call_get,
1027 .set = system_call_set,
1029 #ifdef CONFIG_ARM64_SVE
1030 [REGSET_SVE] = { /* Scalable Vector Extension */
1031 .core_note_type = NT_ARM_SVE,
1032 .n = DIV_ROUND_UP(SVE_PT_SIZE(SVE_VQ_MAX, SVE_PT_REGS_SVE),
1034 .size = SVE_VQ_BYTES,
1035 .align = SVE_VQ_BYTES,
1038 .get_size = sve_get_size,
1043 static const struct user_regset_view user_aarch64_view = {
1044 .name = "aarch64", .e_machine = EM_AARCH64,
1045 .regsets = aarch64_regsets, .n = ARRAY_SIZE(aarch64_regsets)
1048 #ifdef CONFIG_COMPAT
1049 #include <linux/compat.h>
1051 enum compat_regset {
1056 static int compat_gpr_get(struct task_struct *target,
1057 const struct user_regset *regset,
1058 unsigned int pos, unsigned int count,
1059 void *kbuf, void __user *ubuf)
1062 unsigned int i, start, num_regs;
1064 /* Calculate the number of AArch32 registers contained in count */
1065 num_regs = count / regset->size;
1067 /* Convert pos into an register number */
1068 start = pos / regset->size;
1070 if (start + num_regs > regset->n)
1073 for (i = 0; i < num_regs; ++i) {
1074 unsigned int idx = start + i;
1079 reg = task_pt_regs(target)->pc;
1082 reg = task_pt_regs(target)->pstate;
1085 reg = task_pt_regs(target)->orig_x0;
1088 reg = task_pt_regs(target)->regs[idx];
1092 memcpy(kbuf, ®, sizeof(reg));
1093 kbuf += sizeof(reg);
1095 ret = copy_to_user(ubuf, ®, sizeof(reg));
1101 ubuf += sizeof(reg);
1108 static int compat_gpr_set(struct task_struct *target,
1109 const struct user_regset *regset,
1110 unsigned int pos, unsigned int count,
1111 const void *kbuf, const void __user *ubuf)
1113 struct pt_regs newregs;
1115 unsigned int i, start, num_regs;
1117 /* Calculate the number of AArch32 registers contained in count */
1118 num_regs = count / regset->size;
1120 /* Convert pos into an register number */
1121 start = pos / regset->size;
1123 if (start + num_regs > regset->n)
1126 newregs = *task_pt_regs(target);
1128 for (i = 0; i < num_regs; ++i) {
1129 unsigned int idx = start + i;
1133 memcpy(®, kbuf, sizeof(reg));
1134 kbuf += sizeof(reg);
1136 ret = copy_from_user(®, ubuf, sizeof(reg));
1142 ubuf += sizeof(reg);
1150 newregs.pstate = reg;
1153 newregs.orig_x0 = reg;
1156 newregs.regs[idx] = reg;
1161 if (valid_user_regs(&newregs.user_regs, target))
1162 *task_pt_regs(target) = newregs;
1169 static int compat_vfp_get(struct task_struct *target,
1170 const struct user_regset *regset,
1171 unsigned int pos, unsigned int count,
1172 void *kbuf, void __user *ubuf)
1174 struct user_fpsimd_state *uregs;
1175 compat_ulong_t fpscr;
1176 int ret, vregs_end_pos;
1178 uregs = &target->thread.uw.fpsimd_state;
1180 if (target == current)
1181 fpsimd_preserve_current_state();
1184 * The VFP registers are packed into the fpsimd_state, so they all sit
1185 * nicely together for us. We just need to create the fpscr separately.
1187 vregs_end_pos = VFP_STATE_SIZE - sizeof(compat_ulong_t);
1188 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs,
1191 if (count && !ret) {
1192 fpscr = (uregs->fpsr & VFP_FPSCR_STAT_MASK) |
1193 (uregs->fpcr & VFP_FPSCR_CTRL_MASK);
1195 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &fpscr,
1196 vregs_end_pos, VFP_STATE_SIZE);
1202 static int compat_vfp_set(struct task_struct *target,
1203 const struct user_regset *regset,
1204 unsigned int pos, unsigned int count,
1205 const void *kbuf, const void __user *ubuf)
1207 struct user_fpsimd_state *uregs;
1208 compat_ulong_t fpscr;
1209 int ret, vregs_end_pos;
1211 uregs = &target->thread.uw.fpsimd_state;
1213 vregs_end_pos = VFP_STATE_SIZE - sizeof(compat_ulong_t);
1214 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, uregs, 0,
1217 if (count && !ret) {
1218 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &fpscr,
1219 vregs_end_pos, VFP_STATE_SIZE);
1221 uregs->fpsr = fpscr & VFP_FPSCR_STAT_MASK;
1222 uregs->fpcr = fpscr & VFP_FPSCR_CTRL_MASK;
1226 fpsimd_flush_task_state(target);
1230 static int compat_tls_get(struct task_struct *target,
1231 const struct user_regset *regset, unsigned int pos,
1232 unsigned int count, void *kbuf, void __user *ubuf)
1234 compat_ulong_t tls = (compat_ulong_t)target->thread.uw.tp_value;
1235 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
1238 static int compat_tls_set(struct task_struct *target,
1239 const struct user_regset *regset, unsigned int pos,
1240 unsigned int count, const void *kbuf,
1241 const void __user *ubuf)
1244 compat_ulong_t tls = target->thread.uw.tp_value;
1246 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
1250 target->thread.uw.tp_value = tls;
1254 static const struct user_regset aarch32_regsets[] = {
1255 [REGSET_COMPAT_GPR] = {
1256 .core_note_type = NT_PRSTATUS,
1257 .n = COMPAT_ELF_NGREG,
1258 .size = sizeof(compat_elf_greg_t),
1259 .align = sizeof(compat_elf_greg_t),
1260 .get = compat_gpr_get,
1261 .set = compat_gpr_set
1263 [REGSET_COMPAT_VFP] = {
1264 .core_note_type = NT_ARM_VFP,
1265 .n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
1266 .size = sizeof(compat_ulong_t),
1267 .align = sizeof(compat_ulong_t),
1268 .get = compat_vfp_get,
1269 .set = compat_vfp_set
1273 static const struct user_regset_view user_aarch32_view = {
1274 .name = "aarch32", .e_machine = EM_ARM,
1275 .regsets = aarch32_regsets, .n = ARRAY_SIZE(aarch32_regsets)
1278 static const struct user_regset aarch32_ptrace_regsets[] = {
1280 .core_note_type = NT_PRSTATUS,
1281 .n = COMPAT_ELF_NGREG,
1282 .size = sizeof(compat_elf_greg_t),
1283 .align = sizeof(compat_elf_greg_t),
1284 .get = compat_gpr_get,
1285 .set = compat_gpr_set
1288 .core_note_type = NT_ARM_VFP,
1289 .n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
1290 .size = sizeof(compat_ulong_t),
1291 .align = sizeof(compat_ulong_t),
1292 .get = compat_vfp_get,
1293 .set = compat_vfp_set
1296 .core_note_type = NT_ARM_TLS,
1298 .size = sizeof(compat_ulong_t),
1299 .align = sizeof(compat_ulong_t),
1300 .get = compat_tls_get,
1301 .set = compat_tls_set,
1303 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1304 [REGSET_HW_BREAK] = {
1305 .core_note_type = NT_ARM_HW_BREAK,
1306 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
1307 .size = sizeof(u32),
1308 .align = sizeof(u32),
1309 .get = hw_break_get,
1310 .set = hw_break_set,
1312 [REGSET_HW_WATCH] = {
1313 .core_note_type = NT_ARM_HW_WATCH,
1314 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
1315 .size = sizeof(u32),
1316 .align = sizeof(u32),
1317 .get = hw_break_get,
1318 .set = hw_break_set,
1321 [REGSET_SYSTEM_CALL] = {
1322 .core_note_type = NT_ARM_SYSTEM_CALL,
1324 .size = sizeof(int),
1325 .align = sizeof(int),
1326 .get = system_call_get,
1327 .set = system_call_set,
1331 static const struct user_regset_view user_aarch32_ptrace_view = {
1332 .name = "aarch32", .e_machine = EM_ARM,
1333 .regsets = aarch32_ptrace_regsets, .n = ARRAY_SIZE(aarch32_ptrace_regsets)
1336 static int compat_ptrace_read_user(struct task_struct *tsk, compat_ulong_t off,
1337 compat_ulong_t __user *ret)
1344 if (off == COMPAT_PT_TEXT_ADDR)
1345 tmp = tsk->mm->start_code;
1346 else if (off == COMPAT_PT_DATA_ADDR)
1347 tmp = tsk->mm->start_data;
1348 else if (off == COMPAT_PT_TEXT_END_ADDR)
1349 tmp = tsk->mm->end_code;
1350 else if (off < sizeof(compat_elf_gregset_t))
1351 return copy_regset_to_user(tsk, &user_aarch32_view,
1352 REGSET_COMPAT_GPR, off,
1353 sizeof(compat_ulong_t), ret);
1354 else if (off >= COMPAT_USER_SZ)
1359 return put_user(tmp, ret);
1362 static int compat_ptrace_write_user(struct task_struct *tsk, compat_ulong_t off,
1366 mm_segment_t old_fs = get_fs();
1368 if (off & 3 || off >= COMPAT_USER_SZ)
1371 if (off >= sizeof(compat_elf_gregset_t))
1375 ret = copy_regset_from_user(tsk, &user_aarch32_view,
1376 REGSET_COMPAT_GPR, off,
1377 sizeof(compat_ulong_t),
1384 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1387 * Convert a virtual register number into an index for a thread_info
1388 * breakpoint array. Breakpoints are identified using positive numbers
1389 * whilst watchpoints are negative. The registers are laid out as pairs
1390 * of (address, control), each pair mapping to a unique hw_breakpoint struct.
1391 * Register 0 is reserved for describing resource information.
1393 static int compat_ptrace_hbp_num_to_idx(compat_long_t num)
1395 return (abs(num) - 1) >> 1;
1398 static int compat_ptrace_hbp_get_resource_info(u32 *kdata)
1400 u8 num_brps, num_wrps, debug_arch, wp_len;
1403 num_brps = hw_breakpoint_slots(TYPE_INST);
1404 num_wrps = hw_breakpoint_slots(TYPE_DATA);
1406 debug_arch = debug_monitors_arch();
1420 static int compat_ptrace_hbp_get(unsigned int note_type,
1421 struct task_struct *tsk,
1428 int err, idx = compat_ptrace_hbp_num_to_idx(num);
1431 err = ptrace_hbp_get_addr(note_type, tsk, idx, &addr);
1434 err = ptrace_hbp_get_ctrl(note_type, tsk, idx, &ctrl);
1441 static int compat_ptrace_hbp_set(unsigned int note_type,
1442 struct task_struct *tsk,
1449 int err, idx = compat_ptrace_hbp_num_to_idx(num);
1453 err = ptrace_hbp_set_addr(note_type, tsk, idx, addr);
1456 err = ptrace_hbp_set_ctrl(note_type, tsk, idx, ctrl);
1462 static int compat_ptrace_gethbpregs(struct task_struct *tsk, compat_long_t num,
1463 compat_ulong_t __user *data)
1470 ret = compat_ptrace_hbp_get(NT_ARM_HW_WATCH, tsk, num, &kdata);
1472 } else if (num == 0) {
1473 ret = compat_ptrace_hbp_get_resource_info(&kdata);
1476 ret = compat_ptrace_hbp_get(NT_ARM_HW_BREAK, tsk, num, &kdata);
1480 ret = put_user(kdata, data);
1485 static int compat_ptrace_sethbpregs(struct task_struct *tsk, compat_long_t num,
1486 compat_ulong_t __user *data)
1494 ret = get_user(kdata, data);
1499 ret = compat_ptrace_hbp_set(NT_ARM_HW_WATCH, tsk, num, &kdata);
1501 ret = compat_ptrace_hbp_set(NT_ARM_HW_BREAK, tsk, num, &kdata);
1505 #endif /* CONFIG_HAVE_HW_BREAKPOINT */
1507 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1508 compat_ulong_t caddr, compat_ulong_t cdata)
1510 unsigned long addr = caddr;
1511 unsigned long data = cdata;
1512 void __user *datap = compat_ptr(data);
1516 case PTRACE_PEEKUSR:
1517 ret = compat_ptrace_read_user(child, addr, datap);
1520 case PTRACE_POKEUSR:
1521 ret = compat_ptrace_write_user(child, addr, data);
1524 case COMPAT_PTRACE_GETREGS:
1525 ret = copy_regset_to_user(child,
1528 0, sizeof(compat_elf_gregset_t),
1532 case COMPAT_PTRACE_SETREGS:
1533 ret = copy_regset_from_user(child,
1536 0, sizeof(compat_elf_gregset_t),
1540 case COMPAT_PTRACE_GET_THREAD_AREA:
1541 ret = put_user((compat_ulong_t)child->thread.uw.tp_value,
1542 (compat_ulong_t __user *)datap);
1545 case COMPAT_PTRACE_SET_SYSCALL:
1546 task_pt_regs(child)->syscallno = data;
1550 case COMPAT_PTRACE_GETVFPREGS:
1551 ret = copy_regset_to_user(child,
1558 case COMPAT_PTRACE_SETVFPREGS:
1559 ret = copy_regset_from_user(child,
1566 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1567 case COMPAT_PTRACE_GETHBPREGS:
1568 ret = compat_ptrace_gethbpregs(child, addr, datap);
1571 case COMPAT_PTRACE_SETHBPREGS:
1572 ret = compat_ptrace_sethbpregs(child, addr, datap);
1577 ret = compat_ptrace_request(child, request, addr,
1584 #endif /* CONFIG_COMPAT */
1586 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1588 #ifdef CONFIG_COMPAT
1590 * Core dumping of 32-bit tasks or compat ptrace requests must use the
1591 * user_aarch32_view compatible with arm32. Native ptrace requests on
1592 * 32-bit children use an extended user_aarch32_ptrace_view to allow
1593 * access to the TLS register.
1595 if (is_compat_task())
1596 return &user_aarch32_view;
1597 else if (is_compat_thread(task_thread_info(task)))
1598 return &user_aarch32_ptrace_view;
1600 return &user_aarch64_view;
1603 long arch_ptrace(struct task_struct *child, long request,
1604 unsigned long addr, unsigned long data)
1606 return ptrace_request(child, request, addr, data);
1609 enum ptrace_syscall_dir {
1610 PTRACE_SYSCALL_ENTER = 0,
1611 PTRACE_SYSCALL_EXIT,
1614 static void tracehook_report_syscall(struct pt_regs *regs,
1615 enum ptrace_syscall_dir dir)
1618 unsigned long saved_reg;
1621 * A scratch register (ip(r12) on AArch32, x7 on AArch64) is
1622 * used to denote syscall entry/exit:
1624 regno = (is_compat_task() ? 12 : 7);
1625 saved_reg = regs->regs[regno];
1626 regs->regs[regno] = dir;
1628 if (dir == PTRACE_SYSCALL_EXIT)
1629 tracehook_report_syscall_exit(regs, 0);
1630 else if (tracehook_report_syscall_entry(regs))
1631 forget_syscall(regs);
1633 regs->regs[regno] = saved_reg;
1636 asmlinkage int syscall_trace_enter(struct pt_regs *regs)
1638 if (test_thread_flag(TIF_SYSCALL_TRACE))
1639 tracehook_report_syscall(regs, PTRACE_SYSCALL_ENTER);
1641 /* Do the secure computing after ptrace; failures should be fast. */
1642 if (secure_computing(NULL) == -1)
1645 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
1646 trace_sys_enter(regs, regs->syscallno);
1648 audit_syscall_entry(regs->syscallno, regs->orig_x0, regs->regs[1],
1649 regs->regs[2], regs->regs[3]);
1651 return regs->syscallno;
1654 asmlinkage void syscall_trace_exit(struct pt_regs *regs)
1656 audit_syscall_exit(regs);
1658 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
1659 trace_sys_exit(regs, regs_return_value(regs));
1661 if (test_thread_flag(TIF_SYSCALL_TRACE))
1662 tracehook_report_syscall(regs, PTRACE_SYSCALL_EXIT);
1666 * Bits which are always architecturally RES0 per ARM DDI 0487A.h
1667 * Userspace cannot use these until they have an architectural meaning.
1668 * We also reserve IL for the kernel; SS is handled dynamically.
1670 #define SPSR_EL1_AARCH64_RES0_BITS \
1671 (GENMASK_ULL(63,32) | GENMASK_ULL(27, 22) | GENMASK_ULL(20, 10) | \
1673 #define SPSR_EL1_AARCH32_RES0_BITS \
1674 (GENMASK_ULL(63,32) | GENMASK_ULL(24, 22) | GENMASK_ULL(20,20))
1676 static int valid_compat_regs(struct user_pt_regs *regs)
1678 regs->pstate &= ~SPSR_EL1_AARCH32_RES0_BITS;
1680 if (!system_supports_mixed_endian_el0()) {
1681 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
1682 regs->pstate |= COMPAT_PSR_E_BIT;
1684 regs->pstate &= ~COMPAT_PSR_E_BIT;
1687 if (user_mode(regs) && (regs->pstate & PSR_MODE32_BIT) &&
1688 (regs->pstate & COMPAT_PSR_A_BIT) == 0 &&
1689 (regs->pstate & COMPAT_PSR_I_BIT) == 0 &&
1690 (regs->pstate & COMPAT_PSR_F_BIT) == 0) {
1695 * Force PSR to a valid 32-bit EL0t, preserving the same bits as
1698 regs->pstate &= COMPAT_PSR_N_BIT | COMPAT_PSR_Z_BIT |
1699 COMPAT_PSR_C_BIT | COMPAT_PSR_V_BIT |
1700 COMPAT_PSR_Q_BIT | COMPAT_PSR_IT_MASK |
1701 COMPAT_PSR_GE_MASK | COMPAT_PSR_E_BIT |
1703 regs->pstate |= PSR_MODE32_BIT;
1708 static int valid_native_regs(struct user_pt_regs *regs)
1710 regs->pstate &= ~SPSR_EL1_AARCH64_RES0_BITS;
1712 if (user_mode(regs) && !(regs->pstate & PSR_MODE32_BIT) &&
1713 (regs->pstate & PSR_D_BIT) == 0 &&
1714 (regs->pstate & PSR_A_BIT) == 0 &&
1715 (regs->pstate & PSR_I_BIT) == 0 &&
1716 (regs->pstate & PSR_F_BIT) == 0) {
1720 /* Force PSR to a valid 64-bit EL0t */
1721 regs->pstate &= PSR_N_BIT | PSR_Z_BIT | PSR_C_BIT | PSR_V_BIT;
1727 * Are the current registers suitable for user mode? (used to maintain
1728 * security in signal handlers)
1730 int valid_user_regs(struct user_pt_regs *regs, struct task_struct *task)
1732 if (!test_tsk_thread_flag(task, TIF_SINGLESTEP))
1733 regs->pstate &= ~DBG_SPSR_SS;
1735 if (is_compat_thread(task_thread_info(task)))
1736 return valid_compat_regs(regs);
1738 return valid_native_regs(regs);