1 // SPDX-License-Identifier: GPL-2.0-only
3 * Based on arch/arm/kernel/ptrace.c
6 * edited by Linus Torvalds
7 * ARM modifications Copyright (C) 2000 Russell King
8 * Copyright (C) 2012 ARM Ltd.
11 #include <linux/audit.h>
12 #include <linux/compat.h>
13 #include <linux/kernel.h>
14 #include <linux/sched/signal.h>
15 #include <linux/sched/task_stack.h>
17 #include <linux/nospec.h>
18 #include <linux/smp.h>
19 #include <linux/ptrace.h>
20 #include <linux/user.h>
21 #include <linux/seccomp.h>
22 #include <linux/security.h>
23 #include <linux/init.h>
24 #include <linux/signal.h>
25 #include <linux/string.h>
26 #include <linux/uaccess.h>
27 #include <linux/perf_event.h>
28 #include <linux/hw_breakpoint.h>
29 #include <linux/regset.h>
30 #include <linux/tracehook.h>
31 #include <linux/elf.h>
33 #include <asm/compat.h>
34 #include <asm/cpufeature.h>
35 #include <asm/debug-monitors.h>
36 #include <asm/fpsimd.h>
37 #include <asm/pointer_auth.h>
38 #include <asm/stacktrace.h>
39 #include <asm/syscall.h>
40 #include <asm/traps.h>
41 #include <asm/system_misc.h>
43 #define CREATE_TRACE_POINTS
44 #include <trace/events/syscalls.h>
46 struct pt_regs_offset {
51 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
52 #define REG_OFFSET_END {.name = NULL, .offset = 0}
53 #define GPR_OFFSET_NAME(r) \
54 {.name = "x" #r, .offset = offsetof(struct pt_regs, regs[r])}
56 static const struct pt_regs_offset regoffset_table[] = {
88 {.name = "lr", .offset = offsetof(struct pt_regs, regs[30])},
91 REG_OFFSET_NAME(pstate),
96 * regs_query_register_offset() - query register offset from its name
97 * @name: the name of a register
99 * regs_query_register_offset() returns the offset of a register in struct
100 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
102 int regs_query_register_offset(const char *name)
104 const struct pt_regs_offset *roff;
106 for (roff = regoffset_table; roff->name != NULL; roff++)
107 if (!strcmp(roff->name, name))
113 * regs_within_kernel_stack() - check the address in the stack
114 * @regs: pt_regs which contains kernel stack pointer.
115 * @addr: address which is checked.
117 * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
118 * If @addr is within the kernel stack, it returns true. If not, returns false.
120 static bool regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
122 return ((addr & ~(THREAD_SIZE - 1)) ==
123 (kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1))) ||
124 on_irq_stack(addr, NULL);
128 * regs_get_kernel_stack_nth() - get Nth entry of the stack
129 * @regs: pt_regs which contains kernel stack pointer.
130 * @n: stack entry number.
132 * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
133 * is specified by @regs. If the @n th entry is NOT in the kernel stack,
136 unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
138 unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
141 if (regs_within_kernel_stack(regs, (unsigned long)addr))
148 * TODO: does not yet catch signals sent when the child dies.
149 * in exit.c or in signal.c.
153 * Called by kernel/ptrace.c when detaching..
155 void ptrace_disable(struct task_struct *child)
158 * This would be better off in core code, but PTRACE_DETACH has
159 * grown its fair share of arch-specific worts and changing it
160 * is likely to cause regressions on obscure architectures.
162 user_disable_single_step(child);
165 #ifdef CONFIG_HAVE_HW_BREAKPOINT
167 * Handle hitting a HW-breakpoint.
169 static void ptrace_hbptriggered(struct perf_event *bp,
170 struct perf_sample_data *data,
171 struct pt_regs *regs)
173 struct arch_hw_breakpoint *bkpt = counter_arch_bp(bp);
174 const char *desc = "Hardware breakpoint trap (ptrace)";
177 if (is_compat_task()) {
181 for (i = 0; i < ARM_MAX_BRP; ++i) {
182 if (current->thread.debug.hbp_break[i] == bp) {
183 si_errno = (i << 1) + 1;
188 for (i = 0; i < ARM_MAX_WRP; ++i) {
189 if (current->thread.debug.hbp_watch[i] == bp) {
190 si_errno = -((i << 1) + 1);
194 arm64_force_sig_ptrace_errno_trap(si_errno,
195 (void __user *)bkpt->trigger,
199 arm64_force_sig_fault(SIGTRAP, TRAP_HWBKPT,
200 (void __user *)(bkpt->trigger),
205 * Unregister breakpoints from this task and reset the pointers in
208 void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
211 struct thread_struct *t = &tsk->thread;
213 for (i = 0; i < ARM_MAX_BRP; i++) {
214 if (t->debug.hbp_break[i]) {
215 unregister_hw_breakpoint(t->debug.hbp_break[i]);
216 t->debug.hbp_break[i] = NULL;
220 for (i = 0; i < ARM_MAX_WRP; i++) {
221 if (t->debug.hbp_watch[i]) {
222 unregister_hw_breakpoint(t->debug.hbp_watch[i]);
223 t->debug.hbp_watch[i] = NULL;
228 void ptrace_hw_copy_thread(struct task_struct *tsk)
230 memset(&tsk->thread.debug, 0, sizeof(struct debug_info));
233 static struct perf_event *ptrace_hbp_get_event(unsigned int note_type,
234 struct task_struct *tsk,
237 struct perf_event *bp = ERR_PTR(-EINVAL);
240 case NT_ARM_HW_BREAK:
241 if (idx >= ARM_MAX_BRP)
243 idx = array_index_nospec(idx, ARM_MAX_BRP);
244 bp = tsk->thread.debug.hbp_break[idx];
246 case NT_ARM_HW_WATCH:
247 if (idx >= ARM_MAX_WRP)
249 idx = array_index_nospec(idx, ARM_MAX_WRP);
250 bp = tsk->thread.debug.hbp_watch[idx];
258 static int ptrace_hbp_set_event(unsigned int note_type,
259 struct task_struct *tsk,
261 struct perf_event *bp)
266 case NT_ARM_HW_BREAK:
267 if (idx >= ARM_MAX_BRP)
269 idx = array_index_nospec(idx, ARM_MAX_BRP);
270 tsk->thread.debug.hbp_break[idx] = bp;
273 case NT_ARM_HW_WATCH:
274 if (idx >= ARM_MAX_WRP)
276 idx = array_index_nospec(idx, ARM_MAX_WRP);
277 tsk->thread.debug.hbp_watch[idx] = bp;
286 static struct perf_event *ptrace_hbp_create(unsigned int note_type,
287 struct task_struct *tsk,
290 struct perf_event *bp;
291 struct perf_event_attr attr;
295 case NT_ARM_HW_BREAK:
296 type = HW_BREAKPOINT_X;
298 case NT_ARM_HW_WATCH:
299 type = HW_BREAKPOINT_RW;
302 return ERR_PTR(-EINVAL);
305 ptrace_breakpoint_init(&attr);
308 * Initialise fields to sane defaults
309 * (i.e. values that will pass validation).
312 attr.bp_len = HW_BREAKPOINT_LEN_4;
316 bp = register_user_hw_breakpoint(&attr, ptrace_hbptriggered, NULL, tsk);
320 err = ptrace_hbp_set_event(note_type, tsk, idx, bp);
327 static int ptrace_hbp_fill_attr_ctrl(unsigned int note_type,
328 struct arch_hw_breakpoint_ctrl ctrl,
329 struct perf_event_attr *attr)
331 int err, len, type, offset, disabled = !ctrl.enabled;
333 attr->disabled = disabled;
337 err = arch_bp_generic_fields(ctrl, &len, &type, &offset);
342 case NT_ARM_HW_BREAK:
343 if ((type & HW_BREAKPOINT_X) != type)
346 case NT_ARM_HW_WATCH:
347 if ((type & HW_BREAKPOINT_RW) != type)
355 attr->bp_type = type;
356 attr->bp_addr += offset;
361 static int ptrace_hbp_get_resource_info(unsigned int note_type, u32 *info)
367 case NT_ARM_HW_BREAK:
368 num = hw_breakpoint_slots(TYPE_INST);
370 case NT_ARM_HW_WATCH:
371 num = hw_breakpoint_slots(TYPE_DATA);
377 reg |= debug_monitors_arch();
385 static int ptrace_hbp_get_ctrl(unsigned int note_type,
386 struct task_struct *tsk,
390 struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
395 *ctrl = bp ? encode_ctrl_reg(counter_arch_bp(bp)->ctrl) : 0;
399 static int ptrace_hbp_get_addr(unsigned int note_type,
400 struct task_struct *tsk,
404 struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
409 *addr = bp ? counter_arch_bp(bp)->address : 0;
413 static struct perf_event *ptrace_hbp_get_initialised_bp(unsigned int note_type,
414 struct task_struct *tsk,
417 struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
420 bp = ptrace_hbp_create(note_type, tsk, idx);
425 static int ptrace_hbp_set_ctrl(unsigned int note_type,
426 struct task_struct *tsk,
431 struct perf_event *bp;
432 struct perf_event_attr attr;
433 struct arch_hw_breakpoint_ctrl ctrl;
435 bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
442 decode_ctrl_reg(uctrl, &ctrl);
443 err = ptrace_hbp_fill_attr_ctrl(note_type, ctrl, &attr);
447 return modify_user_hw_breakpoint(bp, &attr);
450 static int ptrace_hbp_set_addr(unsigned int note_type,
451 struct task_struct *tsk,
456 struct perf_event *bp;
457 struct perf_event_attr attr;
459 bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
467 err = modify_user_hw_breakpoint(bp, &attr);
471 #define PTRACE_HBP_ADDR_SZ sizeof(u64)
472 #define PTRACE_HBP_CTRL_SZ sizeof(u32)
473 #define PTRACE_HBP_PAD_SZ sizeof(u32)
475 static int hw_break_get(struct task_struct *target,
476 const struct user_regset *regset,
477 unsigned int pos, unsigned int count,
478 void *kbuf, void __user *ubuf)
480 unsigned int note_type = regset->core_note_type;
481 int ret, idx = 0, offset, limit;
486 ret = ptrace_hbp_get_resource_info(note_type, &info);
490 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &info, 0,
496 offset = offsetof(struct user_hwdebug_state, pad);
497 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf, offset,
498 offset + PTRACE_HBP_PAD_SZ);
502 /* (address, ctrl) registers */
503 offset = offsetof(struct user_hwdebug_state, dbg_regs);
504 limit = regset->n * regset->size;
505 while (count && offset < limit) {
506 ret = ptrace_hbp_get_addr(note_type, target, idx, &addr);
509 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &addr,
510 offset, offset + PTRACE_HBP_ADDR_SZ);
513 offset += PTRACE_HBP_ADDR_SZ;
515 ret = ptrace_hbp_get_ctrl(note_type, target, idx, &ctrl);
518 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &ctrl,
519 offset, offset + PTRACE_HBP_CTRL_SZ);
522 offset += PTRACE_HBP_CTRL_SZ;
524 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
526 offset + PTRACE_HBP_PAD_SZ);
529 offset += PTRACE_HBP_PAD_SZ;
536 static int hw_break_set(struct task_struct *target,
537 const struct user_regset *regset,
538 unsigned int pos, unsigned int count,
539 const void *kbuf, const void __user *ubuf)
541 unsigned int note_type = regset->core_note_type;
542 int ret, idx = 0, offset, limit;
546 /* Resource info and pad */
547 offset = offsetof(struct user_hwdebug_state, dbg_regs);
548 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, 0, offset);
552 /* (address, ctrl) registers */
553 limit = regset->n * regset->size;
554 while (count && offset < limit) {
555 if (count < PTRACE_HBP_ADDR_SZ)
557 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &addr,
558 offset, offset + PTRACE_HBP_ADDR_SZ);
561 ret = ptrace_hbp_set_addr(note_type, target, idx, addr);
564 offset += PTRACE_HBP_ADDR_SZ;
568 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ctrl,
569 offset, offset + PTRACE_HBP_CTRL_SZ);
572 ret = ptrace_hbp_set_ctrl(note_type, target, idx, ctrl);
575 offset += PTRACE_HBP_CTRL_SZ;
577 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
579 offset + PTRACE_HBP_PAD_SZ);
582 offset += PTRACE_HBP_PAD_SZ;
588 #endif /* CONFIG_HAVE_HW_BREAKPOINT */
590 static int gpr_get(struct task_struct *target,
591 const struct user_regset *regset,
592 unsigned int pos, unsigned int count,
593 void *kbuf, void __user *ubuf)
595 struct user_pt_regs *uregs = &task_pt_regs(target)->user_regs;
596 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0, -1);
599 static int gpr_set(struct task_struct *target, const struct user_regset *regset,
600 unsigned int pos, unsigned int count,
601 const void *kbuf, const void __user *ubuf)
604 struct user_pt_regs newregs = task_pt_regs(target)->user_regs;
606 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newregs, 0, -1);
610 if (!valid_user_regs(&newregs, target))
613 task_pt_regs(target)->user_regs = newregs;
617 static int fpr_active(struct task_struct *target, const struct user_regset *regset)
619 if (!system_supports_fpsimd())
625 * TODO: update fp accessors for lazy context switching (sync/flush hwstate)
627 static int __fpr_get(struct task_struct *target,
628 const struct user_regset *regset,
629 unsigned int pos, unsigned int count,
630 void *kbuf, void __user *ubuf, unsigned int start_pos)
632 struct user_fpsimd_state *uregs;
634 sve_sync_to_fpsimd(target);
636 uregs = &target->thread.uw.fpsimd_state;
638 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs,
639 start_pos, start_pos + sizeof(*uregs));
642 static int fpr_get(struct task_struct *target, const struct user_regset *regset,
643 unsigned int pos, unsigned int count,
644 void *kbuf, void __user *ubuf)
646 if (!system_supports_fpsimd())
649 if (target == current)
650 fpsimd_preserve_current_state();
652 return __fpr_get(target, regset, pos, count, kbuf, ubuf, 0);
655 static int __fpr_set(struct task_struct *target,
656 const struct user_regset *regset,
657 unsigned int pos, unsigned int count,
658 const void *kbuf, const void __user *ubuf,
659 unsigned int start_pos)
662 struct user_fpsimd_state newstate;
665 * Ensure target->thread.uw.fpsimd_state is up to date, so that a
666 * short copyin can't resurrect stale data.
668 sve_sync_to_fpsimd(target);
670 newstate = target->thread.uw.fpsimd_state;
672 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newstate,
673 start_pos, start_pos + sizeof(newstate));
677 target->thread.uw.fpsimd_state = newstate;
682 static int fpr_set(struct task_struct *target, const struct user_regset *regset,
683 unsigned int pos, unsigned int count,
684 const void *kbuf, const void __user *ubuf)
688 if (!system_supports_fpsimd())
691 ret = __fpr_set(target, regset, pos, count, kbuf, ubuf, 0);
695 sve_sync_from_fpsimd_zeropad(target);
696 fpsimd_flush_task_state(target);
701 static int tls_get(struct task_struct *target, const struct user_regset *regset,
702 unsigned int pos, unsigned int count,
703 void *kbuf, void __user *ubuf)
705 unsigned long *tls = &target->thread.uw.tp_value;
707 if (target == current)
708 tls_preserve_current_state();
710 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, tls, 0, -1);
713 static int tls_set(struct task_struct *target, const struct user_regset *regset,
714 unsigned int pos, unsigned int count,
715 const void *kbuf, const void __user *ubuf)
718 unsigned long tls = target->thread.uw.tp_value;
720 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
724 target->thread.uw.tp_value = tls;
728 static int system_call_get(struct task_struct *target,
729 const struct user_regset *regset,
730 unsigned int pos, unsigned int count,
731 void *kbuf, void __user *ubuf)
733 int syscallno = task_pt_regs(target)->syscallno;
735 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
739 static int system_call_set(struct task_struct *target,
740 const struct user_regset *regset,
741 unsigned int pos, unsigned int count,
742 const void *kbuf, const void __user *ubuf)
744 int syscallno = task_pt_regs(target)->syscallno;
747 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &syscallno, 0, -1);
751 task_pt_regs(target)->syscallno = syscallno;
755 #ifdef CONFIG_ARM64_SVE
757 static void sve_init_header_from_task(struct user_sve_header *header,
758 struct task_struct *target)
762 memset(header, 0, sizeof(*header));
764 header->flags = test_tsk_thread_flag(target, TIF_SVE) ?
765 SVE_PT_REGS_SVE : SVE_PT_REGS_FPSIMD;
766 if (test_tsk_thread_flag(target, TIF_SVE_VL_INHERIT))
767 header->flags |= SVE_PT_VL_INHERIT;
769 header->vl = target->thread.sve_vl;
770 vq = sve_vq_from_vl(header->vl);
772 header->max_vl = sve_max_vl;
773 header->size = SVE_PT_SIZE(vq, header->flags);
774 header->max_size = SVE_PT_SIZE(sve_vq_from_vl(header->max_vl),
778 static unsigned int sve_size_from_header(struct user_sve_header const *header)
780 return ALIGN(header->size, SVE_VQ_BYTES);
783 static unsigned int sve_get_size(struct task_struct *target,
784 const struct user_regset *regset)
786 struct user_sve_header header;
788 if (!system_supports_sve())
791 sve_init_header_from_task(&header, target);
792 return sve_size_from_header(&header);
795 static int sve_get(struct task_struct *target,
796 const struct user_regset *regset,
797 unsigned int pos, unsigned int count,
798 void *kbuf, void __user *ubuf)
801 struct user_sve_header header;
803 unsigned long start, end;
805 if (!system_supports_sve())
809 sve_init_header_from_task(&header, target);
810 vq = sve_vq_from_vl(header.vl);
812 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &header,
817 if (target == current)
818 fpsimd_preserve_current_state();
820 /* Registers: FPSIMD-only case */
822 BUILD_BUG_ON(SVE_PT_FPSIMD_OFFSET != sizeof(header));
823 if ((header.flags & SVE_PT_REGS_MASK) == SVE_PT_REGS_FPSIMD)
824 return __fpr_get(target, regset, pos, count, kbuf, ubuf,
825 SVE_PT_FPSIMD_OFFSET);
827 /* Otherwise: full SVE case */
829 BUILD_BUG_ON(SVE_PT_SVE_OFFSET != sizeof(header));
830 start = SVE_PT_SVE_OFFSET;
831 end = SVE_PT_SVE_FFR_OFFSET(vq) + SVE_PT_SVE_FFR_SIZE(vq);
832 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
833 target->thread.sve_state,
839 end = SVE_PT_SVE_FPSR_OFFSET(vq);
840 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
846 * Copy fpsr, and fpcr which must follow contiguously in
847 * struct fpsimd_state:
850 end = SVE_PT_SVE_FPCR_OFFSET(vq) + SVE_PT_SVE_FPCR_SIZE;
851 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
852 &target->thread.uw.fpsimd_state.fpsr,
858 end = sve_size_from_header(&header);
859 return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
863 static int sve_set(struct task_struct *target,
864 const struct user_regset *regset,
865 unsigned int pos, unsigned int count,
866 const void *kbuf, const void __user *ubuf)
869 struct user_sve_header header;
871 unsigned long start, end;
873 if (!system_supports_sve())
877 if (count < sizeof(header))
879 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &header,
885 * Apart from SVE_PT_REGS_MASK, all SVE_PT_* flags are consumed by
886 * sve_set_vector_length(), which will also validate them for us:
888 ret = sve_set_vector_length(target, header.vl,
889 ((unsigned long)header.flags & ~SVE_PT_REGS_MASK) << 16);
893 /* Actual VL set may be less than the user asked for: */
894 vq = sve_vq_from_vl(target->thread.sve_vl);
896 /* Registers: FPSIMD-only case */
898 BUILD_BUG_ON(SVE_PT_FPSIMD_OFFSET != sizeof(header));
899 if ((header.flags & SVE_PT_REGS_MASK) == SVE_PT_REGS_FPSIMD) {
900 ret = __fpr_set(target, regset, pos, count, kbuf, ubuf,
901 SVE_PT_FPSIMD_OFFSET);
902 clear_tsk_thread_flag(target, TIF_SVE);
906 /* Otherwise: full SVE case */
909 * If setting a different VL from the requested VL and there is
910 * register data, the data layout will be wrong: don't even
911 * try to set the registers in this case.
913 if (count && vq != sve_vq_from_vl(header.vl)) {
921 * Ensure target->thread.sve_state is up to date with target's
922 * FPSIMD regs, so that a short copyin leaves trailing registers
925 fpsimd_sync_to_sve(target);
926 set_tsk_thread_flag(target, TIF_SVE);
928 BUILD_BUG_ON(SVE_PT_SVE_OFFSET != sizeof(header));
929 start = SVE_PT_SVE_OFFSET;
930 end = SVE_PT_SVE_FFR_OFFSET(vq) + SVE_PT_SVE_FFR_SIZE(vq);
931 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
932 target->thread.sve_state,
938 end = SVE_PT_SVE_FPSR_OFFSET(vq);
939 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
945 * Copy fpsr, and fpcr which must follow contiguously in
946 * struct fpsimd_state:
949 end = SVE_PT_SVE_FPCR_OFFSET(vq) + SVE_PT_SVE_FPCR_SIZE;
950 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
951 &target->thread.uw.fpsimd_state.fpsr,
955 fpsimd_flush_task_state(target);
959 #endif /* CONFIG_ARM64_SVE */
961 #ifdef CONFIG_ARM64_PTR_AUTH
962 static int pac_mask_get(struct task_struct *target,
963 const struct user_regset *regset,
964 unsigned int pos, unsigned int count,
965 void *kbuf, void __user *ubuf)
968 * The PAC bits can differ across data and instruction pointers
969 * depending on TCR_EL1.TBID*, which we may make use of in future, so
970 * we expose separate masks.
972 unsigned long mask = ptrauth_user_pac_mask();
973 struct user_pac_mask uregs = {
978 if (!system_supports_address_auth())
981 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &uregs, 0, -1);
984 #ifdef CONFIG_CHECKPOINT_RESTORE
985 static __uint128_t pac_key_to_user(const struct ptrauth_key *key)
987 return (__uint128_t)key->hi << 64 | key->lo;
990 static struct ptrauth_key pac_key_from_user(__uint128_t ukey)
992 struct ptrauth_key key = {
993 .lo = (unsigned long)ukey,
994 .hi = (unsigned long)(ukey >> 64),
1000 static void pac_address_keys_to_user(struct user_pac_address_keys *ukeys,
1001 const struct ptrauth_keys_user *keys)
1003 ukeys->apiakey = pac_key_to_user(&keys->apia);
1004 ukeys->apibkey = pac_key_to_user(&keys->apib);
1005 ukeys->apdakey = pac_key_to_user(&keys->apda);
1006 ukeys->apdbkey = pac_key_to_user(&keys->apdb);
1009 static void pac_address_keys_from_user(struct ptrauth_keys_user *keys,
1010 const struct user_pac_address_keys *ukeys)
1012 keys->apia = pac_key_from_user(ukeys->apiakey);
1013 keys->apib = pac_key_from_user(ukeys->apibkey);
1014 keys->apda = pac_key_from_user(ukeys->apdakey);
1015 keys->apdb = pac_key_from_user(ukeys->apdbkey);
1018 static int pac_address_keys_get(struct task_struct *target,
1019 const struct user_regset *regset,
1020 unsigned int pos, unsigned int count,
1021 void *kbuf, void __user *ubuf)
1023 struct ptrauth_keys_user *keys = &target->thread.keys_user;
1024 struct user_pac_address_keys user_keys;
1026 if (!system_supports_address_auth())
1029 pac_address_keys_to_user(&user_keys, keys);
1031 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
1035 static int pac_address_keys_set(struct task_struct *target,
1036 const struct user_regset *regset,
1037 unsigned int pos, unsigned int count,
1038 const void *kbuf, const void __user *ubuf)
1040 struct ptrauth_keys_user *keys = &target->thread.keys_user;
1041 struct user_pac_address_keys user_keys;
1044 if (!system_supports_address_auth())
1047 pac_address_keys_to_user(&user_keys, keys);
1048 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
1052 pac_address_keys_from_user(keys, &user_keys);
1057 static void pac_generic_keys_to_user(struct user_pac_generic_keys *ukeys,
1058 const struct ptrauth_keys_user *keys)
1060 ukeys->apgakey = pac_key_to_user(&keys->apga);
1063 static void pac_generic_keys_from_user(struct ptrauth_keys_user *keys,
1064 const struct user_pac_generic_keys *ukeys)
1066 keys->apga = pac_key_from_user(ukeys->apgakey);
1069 static int pac_generic_keys_get(struct task_struct *target,
1070 const struct user_regset *regset,
1071 unsigned int pos, unsigned int count,
1072 void *kbuf, void __user *ubuf)
1074 struct ptrauth_keys_user *keys = &target->thread.keys_user;
1075 struct user_pac_generic_keys user_keys;
1077 if (!system_supports_generic_auth())
1080 pac_generic_keys_to_user(&user_keys, keys);
1082 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
1086 static int pac_generic_keys_set(struct task_struct *target,
1087 const struct user_regset *regset,
1088 unsigned int pos, unsigned int count,
1089 const void *kbuf, const void __user *ubuf)
1091 struct ptrauth_keys_user *keys = &target->thread.keys_user;
1092 struct user_pac_generic_keys user_keys;
1095 if (!system_supports_generic_auth())
1098 pac_generic_keys_to_user(&user_keys, keys);
1099 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
1103 pac_generic_keys_from_user(keys, &user_keys);
1107 #endif /* CONFIG_CHECKPOINT_RESTORE */
1108 #endif /* CONFIG_ARM64_PTR_AUTH */
1110 enum aarch64_regset {
1114 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1119 #ifdef CONFIG_ARM64_SVE
1122 #ifdef CONFIG_ARM64_PTR_AUTH
1124 #ifdef CONFIG_CHECKPOINT_RESTORE
1131 static const struct user_regset aarch64_regsets[] = {
1133 .core_note_type = NT_PRSTATUS,
1134 .n = sizeof(struct user_pt_regs) / sizeof(u64),
1135 .size = sizeof(u64),
1136 .align = sizeof(u64),
1141 .core_note_type = NT_PRFPREG,
1142 .n = sizeof(struct user_fpsimd_state) / sizeof(u32),
1144 * We pretend we have 32-bit registers because the fpsr and
1145 * fpcr are 32-bits wide.
1147 .size = sizeof(u32),
1148 .align = sizeof(u32),
1149 .active = fpr_active,
1154 .core_note_type = NT_ARM_TLS,
1156 .size = sizeof(void *),
1157 .align = sizeof(void *),
1161 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1162 [REGSET_HW_BREAK] = {
1163 .core_note_type = NT_ARM_HW_BREAK,
1164 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
1165 .size = sizeof(u32),
1166 .align = sizeof(u32),
1167 .get = hw_break_get,
1168 .set = hw_break_set,
1170 [REGSET_HW_WATCH] = {
1171 .core_note_type = NT_ARM_HW_WATCH,
1172 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
1173 .size = sizeof(u32),
1174 .align = sizeof(u32),
1175 .get = hw_break_get,
1176 .set = hw_break_set,
1179 [REGSET_SYSTEM_CALL] = {
1180 .core_note_type = NT_ARM_SYSTEM_CALL,
1182 .size = sizeof(int),
1183 .align = sizeof(int),
1184 .get = system_call_get,
1185 .set = system_call_set,
1187 #ifdef CONFIG_ARM64_SVE
1188 [REGSET_SVE] = { /* Scalable Vector Extension */
1189 .core_note_type = NT_ARM_SVE,
1190 .n = DIV_ROUND_UP(SVE_PT_SIZE(SVE_VQ_MAX, SVE_PT_REGS_SVE),
1192 .size = SVE_VQ_BYTES,
1193 .align = SVE_VQ_BYTES,
1196 .get_size = sve_get_size,
1199 #ifdef CONFIG_ARM64_PTR_AUTH
1200 [REGSET_PAC_MASK] = {
1201 .core_note_type = NT_ARM_PAC_MASK,
1202 .n = sizeof(struct user_pac_mask) / sizeof(u64),
1203 .size = sizeof(u64),
1204 .align = sizeof(u64),
1205 .get = pac_mask_get,
1206 /* this cannot be set dynamically */
1208 #ifdef CONFIG_CHECKPOINT_RESTORE
1209 [REGSET_PACA_KEYS] = {
1210 .core_note_type = NT_ARM_PACA_KEYS,
1211 .n = sizeof(struct user_pac_address_keys) / sizeof(__uint128_t),
1212 .size = sizeof(__uint128_t),
1213 .align = sizeof(__uint128_t),
1214 .get = pac_address_keys_get,
1215 .set = pac_address_keys_set,
1217 [REGSET_PACG_KEYS] = {
1218 .core_note_type = NT_ARM_PACG_KEYS,
1219 .n = sizeof(struct user_pac_generic_keys) / sizeof(__uint128_t),
1220 .size = sizeof(__uint128_t),
1221 .align = sizeof(__uint128_t),
1222 .get = pac_generic_keys_get,
1223 .set = pac_generic_keys_set,
1229 static const struct user_regset_view user_aarch64_view = {
1230 .name = "aarch64", .e_machine = EM_AARCH64,
1231 .regsets = aarch64_regsets, .n = ARRAY_SIZE(aarch64_regsets)
1234 #ifdef CONFIG_COMPAT
1235 enum compat_regset {
1240 static inline compat_ulong_t compat_get_user_reg(struct task_struct *task, int idx)
1242 struct pt_regs *regs = task_pt_regs(task);
1248 return pstate_to_compat_psr(regs->pstate);
1250 return regs->orig_x0;
1252 return regs->regs[idx];
1256 static int compat_gpr_get(struct task_struct *target,
1257 const struct user_regset *regset,
1258 unsigned int pos, unsigned int count,
1259 void *kbuf, void __user *ubuf)
1262 unsigned int i, start, num_regs;
1264 /* Calculate the number of AArch32 registers contained in count */
1265 num_regs = count / regset->size;
1267 /* Convert pos into an register number */
1268 start = pos / regset->size;
1270 if (start + num_regs > regset->n)
1273 for (i = 0; i < num_regs; ++i) {
1274 compat_ulong_t reg = compat_get_user_reg(target, start + i);
1277 memcpy(kbuf, ®, sizeof(reg));
1278 kbuf += sizeof(reg);
1280 ret = copy_to_user(ubuf, ®, sizeof(reg));
1286 ubuf += sizeof(reg);
1293 static int compat_gpr_set(struct task_struct *target,
1294 const struct user_regset *regset,
1295 unsigned int pos, unsigned int count,
1296 const void *kbuf, const void __user *ubuf)
1298 struct pt_regs newregs;
1300 unsigned int i, start, num_regs;
1302 /* Calculate the number of AArch32 registers contained in count */
1303 num_regs = count / regset->size;
1305 /* Convert pos into an register number */
1306 start = pos / regset->size;
1308 if (start + num_regs > regset->n)
1311 newregs = *task_pt_regs(target);
1313 for (i = 0; i < num_regs; ++i) {
1314 unsigned int idx = start + i;
1318 memcpy(®, kbuf, sizeof(reg));
1319 kbuf += sizeof(reg);
1321 ret = copy_from_user(®, ubuf, sizeof(reg));
1327 ubuf += sizeof(reg);
1335 reg = compat_psr_to_pstate(reg);
1336 newregs.pstate = reg;
1339 newregs.orig_x0 = reg;
1342 newregs.regs[idx] = reg;
1347 if (valid_user_regs(&newregs.user_regs, target))
1348 *task_pt_regs(target) = newregs;
1355 static int compat_vfp_get(struct task_struct *target,
1356 const struct user_regset *regset,
1357 unsigned int pos, unsigned int count,
1358 void *kbuf, void __user *ubuf)
1360 struct user_fpsimd_state *uregs;
1361 compat_ulong_t fpscr;
1362 int ret, vregs_end_pos;
1364 if (!system_supports_fpsimd())
1367 uregs = &target->thread.uw.fpsimd_state;
1369 if (target == current)
1370 fpsimd_preserve_current_state();
1373 * The VFP registers are packed into the fpsimd_state, so they all sit
1374 * nicely together for us. We just need to create the fpscr separately.
1376 vregs_end_pos = VFP_STATE_SIZE - sizeof(compat_ulong_t);
1377 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs,
1380 if (count && !ret) {
1381 fpscr = (uregs->fpsr & VFP_FPSCR_STAT_MASK) |
1382 (uregs->fpcr & VFP_FPSCR_CTRL_MASK);
1384 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &fpscr,
1385 vregs_end_pos, VFP_STATE_SIZE);
1391 static int compat_vfp_set(struct task_struct *target,
1392 const struct user_regset *regset,
1393 unsigned int pos, unsigned int count,
1394 const void *kbuf, const void __user *ubuf)
1396 struct user_fpsimd_state *uregs;
1397 compat_ulong_t fpscr;
1398 int ret, vregs_end_pos;
1400 if (!system_supports_fpsimd())
1403 uregs = &target->thread.uw.fpsimd_state;
1405 vregs_end_pos = VFP_STATE_SIZE - sizeof(compat_ulong_t);
1406 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, uregs, 0,
1409 if (count && !ret) {
1410 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &fpscr,
1411 vregs_end_pos, VFP_STATE_SIZE);
1413 uregs->fpsr = fpscr & VFP_FPSCR_STAT_MASK;
1414 uregs->fpcr = fpscr & VFP_FPSCR_CTRL_MASK;
1418 fpsimd_flush_task_state(target);
1422 static int compat_tls_get(struct task_struct *target,
1423 const struct user_regset *regset, unsigned int pos,
1424 unsigned int count, void *kbuf, void __user *ubuf)
1426 compat_ulong_t tls = (compat_ulong_t)target->thread.uw.tp_value;
1427 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
1430 static int compat_tls_set(struct task_struct *target,
1431 const struct user_regset *regset, unsigned int pos,
1432 unsigned int count, const void *kbuf,
1433 const void __user *ubuf)
1436 compat_ulong_t tls = target->thread.uw.tp_value;
1438 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
1442 target->thread.uw.tp_value = tls;
1446 static const struct user_regset aarch32_regsets[] = {
1447 [REGSET_COMPAT_GPR] = {
1448 .core_note_type = NT_PRSTATUS,
1449 .n = COMPAT_ELF_NGREG,
1450 .size = sizeof(compat_elf_greg_t),
1451 .align = sizeof(compat_elf_greg_t),
1452 .get = compat_gpr_get,
1453 .set = compat_gpr_set
1455 [REGSET_COMPAT_VFP] = {
1456 .core_note_type = NT_ARM_VFP,
1457 .n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
1458 .size = sizeof(compat_ulong_t),
1459 .align = sizeof(compat_ulong_t),
1460 .active = fpr_active,
1461 .get = compat_vfp_get,
1462 .set = compat_vfp_set
1466 static const struct user_regset_view user_aarch32_view = {
1467 .name = "aarch32", .e_machine = EM_ARM,
1468 .regsets = aarch32_regsets, .n = ARRAY_SIZE(aarch32_regsets)
1471 static const struct user_regset aarch32_ptrace_regsets[] = {
1473 .core_note_type = NT_PRSTATUS,
1474 .n = COMPAT_ELF_NGREG,
1475 .size = sizeof(compat_elf_greg_t),
1476 .align = sizeof(compat_elf_greg_t),
1477 .get = compat_gpr_get,
1478 .set = compat_gpr_set
1481 .core_note_type = NT_ARM_VFP,
1482 .n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
1483 .size = sizeof(compat_ulong_t),
1484 .align = sizeof(compat_ulong_t),
1485 .get = compat_vfp_get,
1486 .set = compat_vfp_set
1489 .core_note_type = NT_ARM_TLS,
1491 .size = sizeof(compat_ulong_t),
1492 .align = sizeof(compat_ulong_t),
1493 .get = compat_tls_get,
1494 .set = compat_tls_set,
1496 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1497 [REGSET_HW_BREAK] = {
1498 .core_note_type = NT_ARM_HW_BREAK,
1499 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
1500 .size = sizeof(u32),
1501 .align = sizeof(u32),
1502 .get = hw_break_get,
1503 .set = hw_break_set,
1505 [REGSET_HW_WATCH] = {
1506 .core_note_type = NT_ARM_HW_WATCH,
1507 .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
1508 .size = sizeof(u32),
1509 .align = sizeof(u32),
1510 .get = hw_break_get,
1511 .set = hw_break_set,
1514 [REGSET_SYSTEM_CALL] = {
1515 .core_note_type = NT_ARM_SYSTEM_CALL,
1517 .size = sizeof(int),
1518 .align = sizeof(int),
1519 .get = system_call_get,
1520 .set = system_call_set,
1524 static const struct user_regset_view user_aarch32_ptrace_view = {
1525 .name = "aarch32", .e_machine = EM_ARM,
1526 .regsets = aarch32_ptrace_regsets, .n = ARRAY_SIZE(aarch32_ptrace_regsets)
1529 static int compat_ptrace_read_user(struct task_struct *tsk, compat_ulong_t off,
1530 compat_ulong_t __user *ret)
1537 if (off == COMPAT_PT_TEXT_ADDR)
1538 tmp = tsk->mm->start_code;
1539 else if (off == COMPAT_PT_DATA_ADDR)
1540 tmp = tsk->mm->start_data;
1541 else if (off == COMPAT_PT_TEXT_END_ADDR)
1542 tmp = tsk->mm->end_code;
1543 else if (off < sizeof(compat_elf_gregset_t))
1544 tmp = compat_get_user_reg(tsk, off >> 2);
1545 else if (off >= COMPAT_USER_SZ)
1550 return put_user(tmp, ret);
1553 static int compat_ptrace_write_user(struct task_struct *tsk, compat_ulong_t off,
1557 mm_segment_t old_fs = get_fs();
1559 if (off & 3 || off >= COMPAT_USER_SZ)
1562 if (off >= sizeof(compat_elf_gregset_t))
1566 ret = copy_regset_from_user(tsk, &user_aarch32_view,
1567 REGSET_COMPAT_GPR, off,
1568 sizeof(compat_ulong_t),
1575 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1578 * Convert a virtual register number into an index for a thread_info
1579 * breakpoint array. Breakpoints are identified using positive numbers
1580 * whilst watchpoints are negative. The registers are laid out as pairs
1581 * of (address, control), each pair mapping to a unique hw_breakpoint struct.
1582 * Register 0 is reserved for describing resource information.
1584 static int compat_ptrace_hbp_num_to_idx(compat_long_t num)
1586 return (abs(num) - 1) >> 1;
1589 static int compat_ptrace_hbp_get_resource_info(u32 *kdata)
1591 u8 num_brps, num_wrps, debug_arch, wp_len;
1594 num_brps = hw_breakpoint_slots(TYPE_INST);
1595 num_wrps = hw_breakpoint_slots(TYPE_DATA);
1597 debug_arch = debug_monitors_arch();
1611 static int compat_ptrace_hbp_get(unsigned int note_type,
1612 struct task_struct *tsk,
1619 int err, idx = compat_ptrace_hbp_num_to_idx(num);
1622 err = ptrace_hbp_get_addr(note_type, tsk, idx, &addr);
1625 err = ptrace_hbp_get_ctrl(note_type, tsk, idx, &ctrl);
1632 static int compat_ptrace_hbp_set(unsigned int note_type,
1633 struct task_struct *tsk,
1640 int err, idx = compat_ptrace_hbp_num_to_idx(num);
1644 err = ptrace_hbp_set_addr(note_type, tsk, idx, addr);
1647 err = ptrace_hbp_set_ctrl(note_type, tsk, idx, ctrl);
1653 static int compat_ptrace_gethbpregs(struct task_struct *tsk, compat_long_t num,
1654 compat_ulong_t __user *data)
1661 ret = compat_ptrace_hbp_get(NT_ARM_HW_WATCH, tsk, num, &kdata);
1663 } else if (num == 0) {
1664 ret = compat_ptrace_hbp_get_resource_info(&kdata);
1667 ret = compat_ptrace_hbp_get(NT_ARM_HW_BREAK, tsk, num, &kdata);
1671 ret = put_user(kdata, data);
1676 static int compat_ptrace_sethbpregs(struct task_struct *tsk, compat_long_t num,
1677 compat_ulong_t __user *data)
1685 ret = get_user(kdata, data);
1690 ret = compat_ptrace_hbp_set(NT_ARM_HW_WATCH, tsk, num, &kdata);
1692 ret = compat_ptrace_hbp_set(NT_ARM_HW_BREAK, tsk, num, &kdata);
1696 #endif /* CONFIG_HAVE_HW_BREAKPOINT */
1698 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1699 compat_ulong_t caddr, compat_ulong_t cdata)
1701 unsigned long addr = caddr;
1702 unsigned long data = cdata;
1703 void __user *datap = compat_ptr(data);
1707 case PTRACE_PEEKUSR:
1708 ret = compat_ptrace_read_user(child, addr, datap);
1711 case PTRACE_POKEUSR:
1712 ret = compat_ptrace_write_user(child, addr, data);
1715 case COMPAT_PTRACE_GETREGS:
1716 ret = copy_regset_to_user(child,
1719 0, sizeof(compat_elf_gregset_t),
1723 case COMPAT_PTRACE_SETREGS:
1724 ret = copy_regset_from_user(child,
1727 0, sizeof(compat_elf_gregset_t),
1731 case COMPAT_PTRACE_GET_THREAD_AREA:
1732 ret = put_user((compat_ulong_t)child->thread.uw.tp_value,
1733 (compat_ulong_t __user *)datap);
1736 case COMPAT_PTRACE_SET_SYSCALL:
1737 task_pt_regs(child)->syscallno = data;
1741 case COMPAT_PTRACE_GETVFPREGS:
1742 ret = copy_regset_to_user(child,
1749 case COMPAT_PTRACE_SETVFPREGS:
1750 ret = copy_regset_from_user(child,
1757 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1758 case COMPAT_PTRACE_GETHBPREGS:
1759 ret = compat_ptrace_gethbpregs(child, addr, datap);
1762 case COMPAT_PTRACE_SETHBPREGS:
1763 ret = compat_ptrace_sethbpregs(child, addr, datap);
1768 ret = compat_ptrace_request(child, request, addr,
1775 #endif /* CONFIG_COMPAT */
1777 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1779 #ifdef CONFIG_COMPAT
1781 * Core dumping of 32-bit tasks or compat ptrace requests must use the
1782 * user_aarch32_view compatible with arm32. Native ptrace requests on
1783 * 32-bit children use an extended user_aarch32_ptrace_view to allow
1784 * access to the TLS register.
1786 if (is_compat_task())
1787 return &user_aarch32_view;
1788 else if (is_compat_thread(task_thread_info(task)))
1789 return &user_aarch32_ptrace_view;
1791 return &user_aarch64_view;
1794 long arch_ptrace(struct task_struct *child, long request,
1795 unsigned long addr, unsigned long data)
1797 return ptrace_request(child, request, addr, data);
1800 enum ptrace_syscall_dir {
1801 PTRACE_SYSCALL_ENTER = 0,
1802 PTRACE_SYSCALL_EXIT,
1805 static void tracehook_report_syscall(struct pt_regs *regs,
1806 enum ptrace_syscall_dir dir)
1809 unsigned long saved_reg;
1812 * A scratch register (ip(r12) on AArch32, x7 on AArch64) is
1813 * used to denote syscall entry/exit:
1815 regno = (is_compat_task() ? 12 : 7);
1816 saved_reg = regs->regs[regno];
1817 regs->regs[regno] = dir;
1819 if (dir == PTRACE_SYSCALL_EXIT)
1820 tracehook_report_syscall_exit(regs, 0);
1821 else if (tracehook_report_syscall_entry(regs))
1822 forget_syscall(regs);
1824 regs->regs[regno] = saved_reg;
1827 int syscall_trace_enter(struct pt_regs *regs)
1829 unsigned long flags = READ_ONCE(current_thread_info()->flags);
1831 if (flags & (_TIF_SYSCALL_EMU | _TIF_SYSCALL_TRACE)) {
1832 tracehook_report_syscall(regs, PTRACE_SYSCALL_ENTER);
1833 if (!in_syscall(regs) || (flags & _TIF_SYSCALL_EMU))
1837 /* Do the secure computing after ptrace; failures should be fast. */
1838 if (secure_computing() == -1)
1841 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
1842 trace_sys_enter(regs, regs->syscallno);
1844 audit_syscall_entry(regs->syscallno, regs->orig_x0, regs->regs[1],
1845 regs->regs[2], regs->regs[3]);
1847 return regs->syscallno;
1850 void syscall_trace_exit(struct pt_regs *regs)
1852 audit_syscall_exit(regs);
1854 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
1855 trace_sys_exit(regs, regs_return_value(regs));
1857 if (test_thread_flag(TIF_SYSCALL_TRACE))
1858 tracehook_report_syscall(regs, PTRACE_SYSCALL_EXIT);
1864 * SPSR_ELx bits which are always architecturally RES0 per ARM DDI 0487D.a.
1865 * We permit userspace to set SSBS (AArch64 bit 12, AArch32 bit 23) which is
1866 * not described in ARM DDI 0487D.a.
1867 * We treat PAN and UAO as RES0 bits, as they are meaningless at EL0, and may
1868 * be allocated an EL0 meaning in future.
1869 * Userspace cannot use these until they have an architectural meaning.
1870 * Note that this follows the SPSR_ELx format, not the AArch32 PSR format.
1871 * We also reserve IL for the kernel; SS is handled dynamically.
1873 #define SPSR_EL1_AARCH64_RES0_BITS \
1874 (GENMASK_ULL(63, 32) | GENMASK_ULL(27, 25) | GENMASK_ULL(23, 22) | \
1875 GENMASK_ULL(20, 13) | GENMASK_ULL(5, 5))
1876 #define SPSR_EL1_AARCH32_RES0_BITS \
1877 (GENMASK_ULL(63, 32) | GENMASK_ULL(22, 22) | GENMASK_ULL(20, 20))
1879 static int valid_compat_regs(struct user_pt_regs *regs)
1881 regs->pstate &= ~SPSR_EL1_AARCH32_RES0_BITS;
1883 if (!system_supports_mixed_endian_el0()) {
1884 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
1885 regs->pstate |= PSR_AA32_E_BIT;
1887 regs->pstate &= ~PSR_AA32_E_BIT;
1890 if (user_mode(regs) && (regs->pstate & PSR_MODE32_BIT) &&
1891 (regs->pstate & PSR_AA32_A_BIT) == 0 &&
1892 (regs->pstate & PSR_AA32_I_BIT) == 0 &&
1893 (regs->pstate & PSR_AA32_F_BIT) == 0) {
1898 * Force PSR to a valid 32-bit EL0t, preserving the same bits as
1901 regs->pstate &= PSR_AA32_N_BIT | PSR_AA32_Z_BIT |
1902 PSR_AA32_C_BIT | PSR_AA32_V_BIT |
1903 PSR_AA32_Q_BIT | PSR_AA32_IT_MASK |
1904 PSR_AA32_GE_MASK | PSR_AA32_E_BIT |
1906 regs->pstate |= PSR_MODE32_BIT;
1911 static int valid_native_regs(struct user_pt_regs *regs)
1913 regs->pstate &= ~SPSR_EL1_AARCH64_RES0_BITS;
1915 if (user_mode(regs) && !(regs->pstate & PSR_MODE32_BIT) &&
1916 (regs->pstate & PSR_D_BIT) == 0 &&
1917 (regs->pstate & PSR_A_BIT) == 0 &&
1918 (regs->pstate & PSR_I_BIT) == 0 &&
1919 (regs->pstate & PSR_F_BIT) == 0) {
1923 /* Force PSR to a valid 64-bit EL0t */
1924 regs->pstate &= PSR_N_BIT | PSR_Z_BIT | PSR_C_BIT | PSR_V_BIT;
1930 * Are the current registers suitable for user mode? (used to maintain
1931 * security in signal handlers)
1933 int valid_user_regs(struct user_pt_regs *regs, struct task_struct *task)
1935 if (!test_tsk_thread_flag(task, TIF_SINGLESTEP))
1936 regs->pstate &= ~DBG_SPSR_SS;
1938 if (is_compat_thread(task_thread_info(task)))
1939 return valid_compat_regs(regs);
1941 return valid_native_regs(regs);