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/elf.h>
31 #include <linux/rseq.h>
33 #include <asm/compat.h>
34 #include <asm/cpufeature.h>
35 #include <asm/debug-monitors.h>
36 #include <asm/fpsimd.h>
39 #include <asm/pointer_auth.h>
40 #include <asm/stacktrace.h>
41 #include <asm/syscall.h>
42 #include <asm/traps.h>
43 #include <asm/system_misc.h>
45 #define CREATE_TRACE_POINTS
46 #include <trace/events/syscalls.h>
48 struct pt_regs_offset
{
53 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
54 #define REG_OFFSET_END {.name = NULL, .offset = 0}
55 #define GPR_OFFSET_NAME(r) \
56 {.name = "x" #r, .offset = offsetof(struct pt_regs, regs[r])}
58 static const struct pt_regs_offset regoffset_table
[] = {
90 {.name
= "lr", .offset
= offsetof(struct pt_regs
, regs
[30])},
93 REG_OFFSET_NAME(pstate
),
98 * regs_query_register_offset() - query register offset from its name
99 * @name: the name of a register
101 * regs_query_register_offset() returns the offset of a register in struct
102 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
104 int regs_query_register_offset(const char *name
)
106 const struct pt_regs_offset
*roff
;
108 for (roff
= regoffset_table
; roff
->name
!= NULL
; roff
++)
109 if (!strcmp(roff
->name
, name
))
115 * regs_within_kernel_stack() - check the address in the stack
116 * @regs: pt_regs which contains kernel stack pointer.
117 * @addr: address which is checked.
119 * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
120 * If @addr is within the kernel stack, it returns true. If not, returns false.
122 static bool regs_within_kernel_stack(struct pt_regs
*regs
, unsigned long addr
)
124 return ((addr
& ~(THREAD_SIZE
- 1)) ==
125 (kernel_stack_pointer(regs
) & ~(THREAD_SIZE
- 1))) ||
126 on_irq_stack(addr
, sizeof(unsigned long));
130 * regs_get_kernel_stack_nth() - get Nth entry of the stack
131 * @regs: pt_regs which contains kernel stack pointer.
132 * @n: stack entry number.
134 * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
135 * is specified by @regs. If the @n th entry is NOT in the kernel stack,
138 unsigned long regs_get_kernel_stack_nth(struct pt_regs
*regs
, unsigned int n
)
140 unsigned long *addr
= (unsigned long *)kernel_stack_pointer(regs
);
143 if (regs_within_kernel_stack(regs
, (unsigned long)addr
))
150 * TODO: does not yet catch signals sent when the child dies.
151 * in exit.c or in signal.c.
155 * Called by kernel/ptrace.c when detaching..
157 void ptrace_disable(struct task_struct
*child
)
160 * This would be better off in core code, but PTRACE_DETACH has
161 * grown its fair share of arch-specific worts and changing it
162 * is likely to cause regressions on obscure architectures.
164 user_disable_single_step(child
);
167 #ifdef CONFIG_HAVE_HW_BREAKPOINT
169 * Handle hitting a HW-breakpoint.
171 static void ptrace_hbptriggered(struct perf_event
*bp
,
172 struct perf_sample_data
*data
,
173 struct pt_regs
*regs
)
175 struct arch_hw_breakpoint
*bkpt
= counter_arch_bp(bp
);
176 const char *desc
= "Hardware breakpoint trap (ptrace)";
178 if (is_compat_task()) {
182 for (i
= 0; i
< ARM_MAX_BRP
; ++i
) {
183 if (current
->thread
.debug
.hbp_break
[i
] == bp
) {
184 si_errno
= (i
<< 1) + 1;
189 for (i
= 0; i
< ARM_MAX_WRP
; ++i
) {
190 if (current
->thread
.debug
.hbp_watch
[i
] == bp
) {
191 si_errno
= -((i
<< 1) + 1);
195 arm64_force_sig_ptrace_errno_trap(si_errno
, bkpt
->trigger
,
200 arm64_force_sig_fault(SIGTRAP
, TRAP_HWBKPT
, bkpt
->trigger
, desc
);
204 * Unregister breakpoints from this task and reset the pointers in
207 void flush_ptrace_hw_breakpoint(struct task_struct
*tsk
)
210 struct thread_struct
*t
= &tsk
->thread
;
212 for (i
= 0; i
< ARM_MAX_BRP
; i
++) {
213 if (t
->debug
.hbp_break
[i
]) {
214 unregister_hw_breakpoint(t
->debug
.hbp_break
[i
]);
215 t
->debug
.hbp_break
[i
] = NULL
;
219 for (i
= 0; i
< ARM_MAX_WRP
; i
++) {
220 if (t
->debug
.hbp_watch
[i
]) {
221 unregister_hw_breakpoint(t
->debug
.hbp_watch
[i
]);
222 t
->debug
.hbp_watch
[i
] = NULL
;
227 void ptrace_hw_copy_thread(struct task_struct
*tsk
)
229 memset(&tsk
->thread
.debug
, 0, sizeof(struct debug_info
));
232 static struct perf_event
*ptrace_hbp_get_event(unsigned int note_type
,
233 struct task_struct
*tsk
,
236 struct perf_event
*bp
= ERR_PTR(-EINVAL
);
239 case NT_ARM_HW_BREAK
:
240 if (idx
>= ARM_MAX_BRP
)
242 idx
= array_index_nospec(idx
, ARM_MAX_BRP
);
243 bp
= tsk
->thread
.debug
.hbp_break
[idx
];
245 case NT_ARM_HW_WATCH
:
246 if (idx
>= ARM_MAX_WRP
)
248 idx
= array_index_nospec(idx
, ARM_MAX_WRP
);
249 bp
= tsk
->thread
.debug
.hbp_watch
[idx
];
257 static int ptrace_hbp_set_event(unsigned int note_type
,
258 struct task_struct
*tsk
,
260 struct perf_event
*bp
)
265 case NT_ARM_HW_BREAK
:
266 if (idx
>= ARM_MAX_BRP
)
268 idx
= array_index_nospec(idx
, ARM_MAX_BRP
);
269 tsk
->thread
.debug
.hbp_break
[idx
] = bp
;
272 case NT_ARM_HW_WATCH
:
273 if (idx
>= ARM_MAX_WRP
)
275 idx
= array_index_nospec(idx
, ARM_MAX_WRP
);
276 tsk
->thread
.debug
.hbp_watch
[idx
] = bp
;
285 static struct perf_event
*ptrace_hbp_create(unsigned int note_type
,
286 struct task_struct
*tsk
,
289 struct perf_event
*bp
;
290 struct perf_event_attr attr
;
294 case NT_ARM_HW_BREAK
:
295 type
= HW_BREAKPOINT_X
;
297 case NT_ARM_HW_WATCH
:
298 type
= HW_BREAKPOINT_RW
;
301 return ERR_PTR(-EINVAL
);
304 ptrace_breakpoint_init(&attr
);
307 * Initialise fields to sane defaults
308 * (i.e. values that will pass validation).
311 attr
.bp_len
= HW_BREAKPOINT_LEN_4
;
315 bp
= register_user_hw_breakpoint(&attr
, ptrace_hbptriggered
, NULL
, tsk
);
319 err
= ptrace_hbp_set_event(note_type
, tsk
, idx
, bp
);
326 static int ptrace_hbp_fill_attr_ctrl(unsigned int note_type
,
327 struct arch_hw_breakpoint_ctrl ctrl
,
328 struct perf_event_attr
*attr
)
330 int err
, len
, type
, offset
, disabled
= !ctrl
.enabled
;
332 attr
->disabled
= disabled
;
336 err
= arch_bp_generic_fields(ctrl
, &len
, &type
, &offset
);
341 case NT_ARM_HW_BREAK
:
342 if ((type
& HW_BREAKPOINT_X
) != type
)
345 case NT_ARM_HW_WATCH
:
346 if ((type
& HW_BREAKPOINT_RW
) != type
)
354 attr
->bp_type
= type
;
355 attr
->bp_addr
+= offset
;
360 static int ptrace_hbp_get_resource_info(unsigned int note_type
, u32
*info
)
366 case NT_ARM_HW_BREAK
:
367 num
= hw_breakpoint_slots(TYPE_INST
);
369 case NT_ARM_HW_WATCH
:
370 num
= hw_breakpoint_slots(TYPE_DATA
);
376 reg
|= debug_monitors_arch();
384 static int ptrace_hbp_get_ctrl(unsigned int note_type
,
385 struct task_struct
*tsk
,
389 struct perf_event
*bp
= ptrace_hbp_get_event(note_type
, tsk
, idx
);
394 *ctrl
= bp
? encode_ctrl_reg(counter_arch_bp(bp
)->ctrl
) : 0;
398 static int ptrace_hbp_get_addr(unsigned int note_type
,
399 struct task_struct
*tsk
,
403 struct perf_event
*bp
= ptrace_hbp_get_event(note_type
, tsk
, idx
);
408 *addr
= bp
? counter_arch_bp(bp
)->address
: 0;
412 static struct perf_event
*ptrace_hbp_get_initialised_bp(unsigned int note_type
,
413 struct task_struct
*tsk
,
416 struct perf_event
*bp
= ptrace_hbp_get_event(note_type
, tsk
, idx
);
419 bp
= ptrace_hbp_create(note_type
, tsk
, idx
);
424 static int ptrace_hbp_set_ctrl(unsigned int note_type
,
425 struct task_struct
*tsk
,
430 struct perf_event
*bp
;
431 struct perf_event_attr attr
;
432 struct arch_hw_breakpoint_ctrl ctrl
;
434 bp
= ptrace_hbp_get_initialised_bp(note_type
, tsk
, idx
);
441 decode_ctrl_reg(uctrl
, &ctrl
);
442 err
= ptrace_hbp_fill_attr_ctrl(note_type
, ctrl
, &attr
);
446 return modify_user_hw_breakpoint(bp
, &attr
);
449 static int ptrace_hbp_set_addr(unsigned int note_type
,
450 struct task_struct
*tsk
,
455 struct perf_event
*bp
;
456 struct perf_event_attr attr
;
458 bp
= ptrace_hbp_get_initialised_bp(note_type
, tsk
, idx
);
466 err
= modify_user_hw_breakpoint(bp
, &attr
);
470 #define PTRACE_HBP_ADDR_SZ sizeof(u64)
471 #define PTRACE_HBP_CTRL_SZ sizeof(u32)
472 #define PTRACE_HBP_PAD_SZ sizeof(u32)
474 static int hw_break_get(struct task_struct
*target
,
475 const struct user_regset
*regset
,
478 unsigned int note_type
= regset
->core_note_type
;
484 ret
= ptrace_hbp_get_resource_info(note_type
, &info
);
488 membuf_write(&to
, &info
, sizeof(info
));
489 membuf_zero(&to
, sizeof(u32
));
490 /* (address, ctrl) registers */
492 ret
= ptrace_hbp_get_addr(note_type
, target
, idx
, &addr
);
495 ret
= ptrace_hbp_get_ctrl(note_type
, target
, idx
, &ctrl
);
498 membuf_store(&to
, addr
);
499 membuf_store(&to
, ctrl
);
500 membuf_zero(&to
, sizeof(u32
));
506 static int hw_break_set(struct task_struct
*target
,
507 const struct user_regset
*regset
,
508 unsigned int pos
, unsigned int count
,
509 const void *kbuf
, const void __user
*ubuf
)
511 unsigned int note_type
= regset
->core_note_type
;
512 int ret
, idx
= 0, offset
, limit
;
516 /* Resource info and pad */
517 offset
= offsetof(struct user_hwdebug_state
, dbg_regs
);
518 user_regset_copyin_ignore(&pos
, &count
, &kbuf
, &ubuf
, 0, offset
);
520 /* (address, ctrl) registers */
521 limit
= regset
->n
* regset
->size
;
522 while (count
&& offset
< limit
) {
523 if (count
< PTRACE_HBP_ADDR_SZ
)
525 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
, &addr
,
526 offset
, offset
+ PTRACE_HBP_ADDR_SZ
);
529 ret
= ptrace_hbp_set_addr(note_type
, target
, idx
, addr
);
532 offset
+= PTRACE_HBP_ADDR_SZ
;
536 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
, &ctrl
,
537 offset
, offset
+ PTRACE_HBP_CTRL_SZ
);
540 ret
= ptrace_hbp_set_ctrl(note_type
, target
, idx
, ctrl
);
543 offset
+= PTRACE_HBP_CTRL_SZ
;
545 user_regset_copyin_ignore(&pos
, &count
, &kbuf
, &ubuf
,
546 offset
, offset
+ PTRACE_HBP_PAD_SZ
);
547 offset
+= PTRACE_HBP_PAD_SZ
;
553 #endif /* CONFIG_HAVE_HW_BREAKPOINT */
555 static int gpr_get(struct task_struct
*target
,
556 const struct user_regset
*regset
,
559 struct user_pt_regs
*uregs
= &task_pt_regs(target
)->user_regs
;
560 return membuf_write(&to
, uregs
, sizeof(*uregs
));
563 static int gpr_set(struct task_struct
*target
, const struct user_regset
*regset
,
564 unsigned int pos
, unsigned int count
,
565 const void *kbuf
, const void __user
*ubuf
)
568 struct user_pt_regs newregs
= task_pt_regs(target
)->user_regs
;
570 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
, &newregs
, 0, -1);
574 if (!valid_user_regs(&newregs
, target
))
577 task_pt_regs(target
)->user_regs
= newregs
;
581 static int fpr_active(struct task_struct
*target
, const struct user_regset
*regset
)
583 if (!system_supports_fpsimd())
589 * TODO: update fp accessors for lazy context switching (sync/flush hwstate)
591 static int __fpr_get(struct task_struct
*target
,
592 const struct user_regset
*regset
,
595 struct user_fpsimd_state
*uregs
;
597 sve_sync_to_fpsimd(target
);
599 uregs
= &target
->thread
.uw
.fpsimd_state
;
601 return membuf_write(&to
, uregs
, sizeof(*uregs
));
604 static int fpr_get(struct task_struct
*target
, const struct user_regset
*regset
,
607 if (!system_supports_fpsimd())
610 if (target
== current
)
611 fpsimd_preserve_current_state();
613 return __fpr_get(target
, regset
, to
);
616 static int __fpr_set(struct task_struct
*target
,
617 const struct user_regset
*regset
,
618 unsigned int pos
, unsigned int count
,
619 const void *kbuf
, const void __user
*ubuf
,
620 unsigned int start_pos
)
623 struct user_fpsimd_state newstate
;
626 * Ensure target->thread.uw.fpsimd_state is up to date, so that a
627 * short copyin can't resurrect stale data.
629 sve_sync_to_fpsimd(target
);
631 newstate
= target
->thread
.uw
.fpsimd_state
;
633 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
, &newstate
,
634 start_pos
, start_pos
+ sizeof(newstate
));
638 target
->thread
.uw
.fpsimd_state
= newstate
;
643 static int fpr_set(struct task_struct
*target
, const struct user_regset
*regset
,
644 unsigned int pos
, unsigned int count
,
645 const void *kbuf
, const void __user
*ubuf
)
649 if (!system_supports_fpsimd())
652 ret
= __fpr_set(target
, regset
, pos
, count
, kbuf
, ubuf
, 0);
656 sve_sync_from_fpsimd_zeropad(target
);
657 fpsimd_flush_task_state(target
);
662 static int tls_get(struct task_struct
*target
, const struct user_regset
*regset
,
667 if (target
== current
)
668 tls_preserve_current_state();
670 ret
= membuf_store(&to
, target
->thread
.uw
.tp_value
);
671 if (system_supports_tpidr2())
672 ret
= membuf_store(&to
, target
->thread
.tpidr2_el0
);
674 ret
= membuf_zero(&to
, sizeof(u64
));
679 static int tls_set(struct task_struct
*target
, const struct user_regset
*regset
,
680 unsigned int pos
, unsigned int count
,
681 const void *kbuf
, const void __user
*ubuf
)
684 unsigned long tls
[2];
686 tls
[0] = target
->thread
.uw
.tp_value
;
687 if (system_supports_tpidr2())
688 tls
[1] = target
->thread
.tpidr2_el0
;
690 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
, tls
, 0, count
);
694 target
->thread
.uw
.tp_value
= tls
[0];
695 if (system_supports_tpidr2())
696 target
->thread
.tpidr2_el0
= tls
[1];
701 static int fpmr_get(struct task_struct
*target
, const struct user_regset
*regset
,
704 if (!system_supports_fpmr())
707 if (target
== current
)
708 fpsimd_preserve_current_state();
710 return membuf_store(&to
, target
->thread
.uw
.fpmr
);
713 static int fpmr_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
)
720 if (!system_supports_fpmr())
723 fpmr
= target
->thread
.uw
.fpmr
;
725 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
, &fpmr
, 0, count
);
729 target
->thread
.uw
.fpmr
= fpmr
;
731 fpsimd_flush_task_state(target
);
736 static int system_call_get(struct task_struct
*target
,
737 const struct user_regset
*regset
,
740 return membuf_store(&to
, task_pt_regs(target
)->syscallno
);
743 static int system_call_set(struct task_struct
*target
,
744 const struct user_regset
*regset
,
745 unsigned int pos
, unsigned int count
,
746 const void *kbuf
, const void __user
*ubuf
)
748 int syscallno
= task_pt_regs(target
)->syscallno
;
751 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
, &syscallno
, 0, -1);
755 task_pt_regs(target
)->syscallno
= syscallno
;
759 #ifdef CONFIG_ARM64_SVE
761 static void sve_init_header_from_task(struct user_sve_header
*header
,
762 struct task_struct
*target
,
767 enum vec_type task_type
;
769 memset(header
, 0, sizeof(*header
));
771 /* Check if the requested registers are active for the task */
772 if (thread_sm_enabled(&target
->thread
))
773 task_type
= ARM64_VEC_SME
;
775 task_type
= ARM64_VEC_SVE
;
776 active
= (task_type
== type
);
780 if (test_tsk_thread_flag(target
, TIF_SVE_VL_INHERIT
))
781 header
->flags
|= SVE_PT_VL_INHERIT
;
784 if (test_tsk_thread_flag(target
, TIF_SME_VL_INHERIT
))
785 header
->flags
|= SVE_PT_VL_INHERIT
;
793 if (target
->thread
.fp_type
== FP_STATE_FPSIMD
) {
794 header
->flags
|= SVE_PT_REGS_FPSIMD
;
796 header
->flags
|= SVE_PT_REGS_SVE
;
800 header
->vl
= task_get_vl(target
, type
);
801 vq
= sve_vq_from_vl(header
->vl
);
803 header
->max_vl
= vec_max_vl(type
);
804 header
->size
= SVE_PT_SIZE(vq
, header
->flags
);
805 header
->max_size
= SVE_PT_SIZE(sve_vq_from_vl(header
->max_vl
),
809 static unsigned int sve_size_from_header(struct user_sve_header
const *header
)
811 return ALIGN(header
->size
, SVE_VQ_BYTES
);
814 static int sve_get_common(struct task_struct
*target
,
815 const struct user_regset
*regset
,
819 struct user_sve_header header
;
821 unsigned long start
, end
;
824 sve_init_header_from_task(&header
, target
, type
);
825 vq
= sve_vq_from_vl(header
.vl
);
827 membuf_write(&to
, &header
, sizeof(header
));
829 if (target
== current
)
830 fpsimd_preserve_current_state();
832 BUILD_BUG_ON(SVE_PT_FPSIMD_OFFSET
!= sizeof(header
));
833 BUILD_BUG_ON(SVE_PT_SVE_OFFSET
!= sizeof(header
));
835 switch ((header
.flags
& SVE_PT_REGS_MASK
)) {
836 case SVE_PT_REGS_FPSIMD
:
837 return __fpr_get(target
, regset
, to
);
839 case SVE_PT_REGS_SVE
:
840 start
= SVE_PT_SVE_OFFSET
;
841 end
= SVE_PT_SVE_FFR_OFFSET(vq
) + SVE_PT_SVE_FFR_SIZE(vq
);
842 membuf_write(&to
, target
->thread
.sve_state
, end
- start
);
845 end
= SVE_PT_SVE_FPSR_OFFSET(vq
);
846 membuf_zero(&to
, end
- start
);
849 * Copy fpsr, and fpcr which must follow contiguously in
850 * struct fpsimd_state:
853 end
= SVE_PT_SVE_FPCR_OFFSET(vq
) + SVE_PT_SVE_FPCR_SIZE
;
854 membuf_write(&to
, &target
->thread
.uw
.fpsimd_state
.fpsr
,
858 end
= sve_size_from_header(&header
);
859 return membuf_zero(&to
, end
- start
);
866 static int sve_get(struct task_struct
*target
,
867 const struct user_regset
*regset
,
870 if (!system_supports_sve())
873 return sve_get_common(target
, regset
, to
, ARM64_VEC_SVE
);
876 static int sve_set_common(struct task_struct
*target
,
877 const struct user_regset
*regset
,
878 unsigned int pos
, unsigned int count
,
879 const void *kbuf
, const void __user
*ubuf
,
883 struct user_sve_header header
;
885 unsigned long start
, end
;
888 if (count
< sizeof(header
))
890 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
, &header
,
896 * Apart from SVE_PT_REGS_MASK, all SVE_PT_* flags are consumed by
897 * vec_set_vector_length(), which will also validate them for us:
899 ret
= vec_set_vector_length(target
, type
, header
.vl
,
900 ((unsigned long)header
.flags
& ~SVE_PT_REGS_MASK
) << 16);
905 * Actual VL set may be different from what the user asked
906 * for, or we may have configured the _ONEXEC VL not the
909 vq
= sve_vq_from_vl(task_get_vl(target
, type
));
911 /* Enter/exit streaming mode */
912 if (system_supports_sme()) {
913 u64 old_svcr
= target
->thread
.svcr
;
917 target
->thread
.svcr
&= ~SVCR_SM_MASK
;
920 target
->thread
.svcr
|= SVCR_SM_MASK
;
923 * Disable traps and ensure there is SME storage but
924 * preserve any currently set values in ZA/ZT.
926 sme_alloc(target
, false);
927 set_tsk_thread_flag(target
, TIF_SME
);
936 * If we switched then invalidate any existing SVE
937 * state and ensure there's storage.
939 if (target
->thread
.svcr
!= old_svcr
)
940 sve_alloc(target
, true);
943 /* Registers: FPSIMD-only case */
945 BUILD_BUG_ON(SVE_PT_FPSIMD_OFFSET
!= sizeof(header
));
946 if ((header
.flags
& SVE_PT_REGS_MASK
) == SVE_PT_REGS_FPSIMD
) {
947 ret
= __fpr_set(target
, regset
, pos
, count
, kbuf
, ubuf
,
948 SVE_PT_FPSIMD_OFFSET
);
949 clear_tsk_thread_flag(target
, TIF_SVE
);
950 target
->thread
.fp_type
= FP_STATE_FPSIMD
;
955 * Otherwise: no registers or full SVE case. For backwards
956 * compatibility reasons we treat empty flags as SVE registers.
960 * If setting a different VL from the requested VL and there is
961 * register data, the data layout will be wrong: don't even
962 * try to set the registers in this case.
964 if (count
&& vq
!= sve_vq_from_vl(header
.vl
)) {
969 sve_alloc(target
, true);
970 if (!target
->thread
.sve_state
) {
972 clear_tsk_thread_flag(target
, TIF_SVE
);
973 target
->thread
.fp_type
= FP_STATE_FPSIMD
;
978 * Ensure target->thread.sve_state is up to date with target's
979 * FPSIMD regs, so that a short copyin leaves trailing
980 * registers unmodified. Only enable SVE if we are
981 * configuring normal SVE, a system with streaming SVE may not
984 fpsimd_sync_to_sve(target
);
985 if (type
== ARM64_VEC_SVE
)
986 set_tsk_thread_flag(target
, TIF_SVE
);
987 target
->thread
.fp_type
= FP_STATE_SVE
;
989 BUILD_BUG_ON(SVE_PT_SVE_OFFSET
!= sizeof(header
));
990 start
= SVE_PT_SVE_OFFSET
;
991 end
= SVE_PT_SVE_FFR_OFFSET(vq
) + SVE_PT_SVE_FFR_SIZE(vq
);
992 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
,
993 target
->thread
.sve_state
,
999 end
= SVE_PT_SVE_FPSR_OFFSET(vq
);
1000 user_regset_copyin_ignore(&pos
, &count
, &kbuf
, &ubuf
, start
, end
);
1003 * Copy fpsr, and fpcr which must follow contiguously in
1004 * struct fpsimd_state:
1007 end
= SVE_PT_SVE_FPCR_OFFSET(vq
) + SVE_PT_SVE_FPCR_SIZE
;
1008 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
,
1009 &target
->thread
.uw
.fpsimd_state
.fpsr
,
1013 fpsimd_flush_task_state(target
);
1017 static int sve_set(struct task_struct
*target
,
1018 const struct user_regset
*regset
,
1019 unsigned int pos
, unsigned int count
,
1020 const void *kbuf
, const void __user
*ubuf
)
1022 if (!system_supports_sve())
1025 return sve_set_common(target
, regset
, pos
, count
, kbuf
, ubuf
,
1029 #endif /* CONFIG_ARM64_SVE */
1031 #ifdef CONFIG_ARM64_SME
1033 static int ssve_get(struct task_struct
*target
,
1034 const struct user_regset
*regset
,
1037 if (!system_supports_sme())
1040 return sve_get_common(target
, regset
, to
, ARM64_VEC_SME
);
1043 static int ssve_set(struct task_struct
*target
,
1044 const struct user_regset
*regset
,
1045 unsigned int pos
, unsigned int count
,
1046 const void *kbuf
, const void __user
*ubuf
)
1048 if (!system_supports_sme())
1051 return sve_set_common(target
, regset
, pos
, count
, kbuf
, ubuf
,
1055 static int za_get(struct task_struct
*target
,
1056 const struct user_regset
*regset
,
1059 struct user_za_header header
;
1061 unsigned long start
, end
;
1063 if (!system_supports_sme())
1067 memset(&header
, 0, sizeof(header
));
1069 if (test_tsk_thread_flag(target
, TIF_SME_VL_INHERIT
))
1070 header
.flags
|= ZA_PT_VL_INHERIT
;
1072 header
.vl
= task_get_sme_vl(target
);
1073 vq
= sve_vq_from_vl(header
.vl
);
1074 header
.max_vl
= sme_max_vl();
1075 header
.max_size
= ZA_PT_SIZE(vq
);
1077 /* If ZA is not active there is only the header */
1078 if (thread_za_enabled(&target
->thread
))
1079 header
.size
= ZA_PT_SIZE(vq
);
1081 header
.size
= ZA_PT_ZA_OFFSET
;
1083 membuf_write(&to
, &header
, sizeof(header
));
1085 BUILD_BUG_ON(ZA_PT_ZA_OFFSET
!= sizeof(header
));
1086 end
= ZA_PT_ZA_OFFSET
;
1088 if (target
== current
)
1089 fpsimd_preserve_current_state();
1091 /* Any register data to include? */
1092 if (thread_za_enabled(&target
->thread
)) {
1094 end
= ZA_PT_SIZE(vq
);
1095 membuf_write(&to
, target
->thread
.sme_state
, end
- start
);
1098 /* Zero any trailing padding */
1100 end
= ALIGN(header
.size
, SVE_VQ_BYTES
);
1101 return membuf_zero(&to
, end
- start
);
1104 static int za_set(struct task_struct
*target
,
1105 const struct user_regset
*regset
,
1106 unsigned int pos
, unsigned int count
,
1107 const void *kbuf
, const void __user
*ubuf
)
1110 struct user_za_header header
;
1112 unsigned long start
, end
;
1114 if (!system_supports_sme())
1118 if (count
< sizeof(header
))
1120 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
, &header
,
1126 * All current ZA_PT_* flags are consumed by
1127 * vec_set_vector_length(), which will also validate them for
1130 ret
= vec_set_vector_length(target
, ARM64_VEC_SME
, header
.vl
,
1131 ((unsigned long)header
.flags
) << 16);
1136 * Actual VL set may be different from what the user asked
1137 * for, or we may have configured the _ONEXEC rather than
1140 vq
= sve_vq_from_vl(task_get_sme_vl(target
));
1142 /* Ensure there is some SVE storage for streaming mode */
1143 if (!target
->thread
.sve_state
) {
1144 sve_alloc(target
, false);
1145 if (!target
->thread
.sve_state
) {
1152 * Only flush the storage if PSTATE.ZA was not already set,
1153 * otherwise preserve any existing data.
1155 sme_alloc(target
, !thread_za_enabled(&target
->thread
));
1156 if (!target
->thread
.sme_state
)
1159 /* If there is no data then disable ZA */
1161 target
->thread
.svcr
&= ~SVCR_ZA_MASK
;
1166 * If setting a different VL from the requested VL and there is
1167 * register data, the data layout will be wrong: don't even
1168 * try to set the registers in this case.
1170 if (vq
!= sve_vq_from_vl(header
.vl
)) {
1175 BUILD_BUG_ON(ZA_PT_ZA_OFFSET
!= sizeof(header
));
1176 start
= ZA_PT_ZA_OFFSET
;
1177 end
= ZA_PT_SIZE(vq
);
1178 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
,
1179 target
->thread
.sme_state
,
1184 /* Mark ZA as active and let userspace use it */
1185 set_tsk_thread_flag(target
, TIF_SME
);
1186 target
->thread
.svcr
|= SVCR_ZA_MASK
;
1189 fpsimd_flush_task_state(target
);
1193 static int zt_get(struct task_struct
*target
,
1194 const struct user_regset
*regset
,
1197 if (!system_supports_sme2())
1201 * If PSTATE.ZA is not set then ZT will be zeroed when it is
1202 * enabled so report the current register value as zero.
1204 if (thread_za_enabled(&target
->thread
))
1205 membuf_write(&to
, thread_zt_state(&target
->thread
),
1208 membuf_zero(&to
, ZT_SIG_REG_BYTES
);
1213 static int zt_set(struct task_struct
*target
,
1214 const struct user_regset
*regset
,
1215 unsigned int pos
, unsigned int count
,
1216 const void *kbuf
, const void __user
*ubuf
)
1220 if (!system_supports_sme2())
1223 /* Ensure SVE storage in case this is first use of SME */
1224 sve_alloc(target
, false);
1225 if (!target
->thread
.sve_state
)
1228 if (!thread_za_enabled(&target
->thread
)) {
1229 sme_alloc(target
, true);
1230 if (!target
->thread
.sme_state
)
1234 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
,
1235 thread_zt_state(&target
->thread
),
1236 0, ZT_SIG_REG_BYTES
);
1238 target
->thread
.svcr
|= SVCR_ZA_MASK
;
1239 set_tsk_thread_flag(target
, TIF_SME
);
1242 fpsimd_flush_task_state(target
);
1247 #endif /* CONFIG_ARM64_SME */
1249 #ifdef CONFIG_ARM64_PTR_AUTH
1250 static int pac_mask_get(struct task_struct
*target
,
1251 const struct user_regset
*regset
,
1255 * The PAC bits can differ across data and instruction pointers
1256 * depending on TCR_EL1.TBID*, which we may make use of in future, so
1257 * we expose separate masks.
1259 unsigned long mask
= ptrauth_user_pac_mask();
1260 struct user_pac_mask uregs
= {
1265 if (!system_supports_address_auth())
1268 return membuf_write(&to
, &uregs
, sizeof(uregs
));
1271 static int pac_enabled_keys_get(struct task_struct
*target
,
1272 const struct user_regset
*regset
,
1275 long enabled_keys
= ptrauth_get_enabled_keys(target
);
1277 if (IS_ERR_VALUE(enabled_keys
))
1278 return enabled_keys
;
1280 return membuf_write(&to
, &enabled_keys
, sizeof(enabled_keys
));
1283 static int pac_enabled_keys_set(struct task_struct
*target
,
1284 const struct user_regset
*regset
,
1285 unsigned int pos
, unsigned int count
,
1286 const void *kbuf
, const void __user
*ubuf
)
1289 long enabled_keys
= ptrauth_get_enabled_keys(target
);
1291 if (IS_ERR_VALUE(enabled_keys
))
1292 return enabled_keys
;
1294 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
, &enabled_keys
, 0,
1299 return ptrauth_set_enabled_keys(target
, PR_PAC_ENABLED_KEYS_MASK
,
1303 #ifdef CONFIG_CHECKPOINT_RESTORE
1304 static __uint128_t
pac_key_to_user(const struct ptrauth_key
*key
)
1306 return (__uint128_t
)key
->hi
<< 64 | key
->lo
;
1309 static struct ptrauth_key
pac_key_from_user(__uint128_t ukey
)
1311 struct ptrauth_key key
= {
1312 .lo
= (unsigned long)ukey
,
1313 .hi
= (unsigned long)(ukey
>> 64),
1319 static void pac_address_keys_to_user(struct user_pac_address_keys
*ukeys
,
1320 const struct ptrauth_keys_user
*keys
)
1322 ukeys
->apiakey
= pac_key_to_user(&keys
->apia
);
1323 ukeys
->apibkey
= pac_key_to_user(&keys
->apib
);
1324 ukeys
->apdakey
= pac_key_to_user(&keys
->apda
);
1325 ukeys
->apdbkey
= pac_key_to_user(&keys
->apdb
);
1328 static void pac_address_keys_from_user(struct ptrauth_keys_user
*keys
,
1329 const struct user_pac_address_keys
*ukeys
)
1331 keys
->apia
= pac_key_from_user(ukeys
->apiakey
);
1332 keys
->apib
= pac_key_from_user(ukeys
->apibkey
);
1333 keys
->apda
= pac_key_from_user(ukeys
->apdakey
);
1334 keys
->apdb
= pac_key_from_user(ukeys
->apdbkey
);
1337 static int pac_address_keys_get(struct task_struct
*target
,
1338 const struct user_regset
*regset
,
1341 struct ptrauth_keys_user
*keys
= &target
->thread
.keys_user
;
1342 struct user_pac_address_keys user_keys
;
1344 if (!system_supports_address_auth())
1347 pac_address_keys_to_user(&user_keys
, keys
);
1349 return membuf_write(&to
, &user_keys
, sizeof(user_keys
));
1352 static int pac_address_keys_set(struct task_struct
*target
,
1353 const struct user_regset
*regset
,
1354 unsigned int pos
, unsigned int count
,
1355 const void *kbuf
, const void __user
*ubuf
)
1357 struct ptrauth_keys_user
*keys
= &target
->thread
.keys_user
;
1358 struct user_pac_address_keys user_keys
;
1361 if (!system_supports_address_auth())
1364 pac_address_keys_to_user(&user_keys
, keys
);
1365 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
,
1369 pac_address_keys_from_user(keys
, &user_keys
);
1374 static void pac_generic_keys_to_user(struct user_pac_generic_keys
*ukeys
,
1375 const struct ptrauth_keys_user
*keys
)
1377 ukeys
->apgakey
= pac_key_to_user(&keys
->apga
);
1380 static void pac_generic_keys_from_user(struct ptrauth_keys_user
*keys
,
1381 const struct user_pac_generic_keys
*ukeys
)
1383 keys
->apga
= pac_key_from_user(ukeys
->apgakey
);
1386 static int pac_generic_keys_get(struct task_struct
*target
,
1387 const struct user_regset
*regset
,
1390 struct ptrauth_keys_user
*keys
= &target
->thread
.keys_user
;
1391 struct user_pac_generic_keys user_keys
;
1393 if (!system_supports_generic_auth())
1396 pac_generic_keys_to_user(&user_keys
, keys
);
1398 return membuf_write(&to
, &user_keys
, sizeof(user_keys
));
1401 static int pac_generic_keys_set(struct task_struct
*target
,
1402 const struct user_regset
*regset
,
1403 unsigned int pos
, unsigned int count
,
1404 const void *kbuf
, const void __user
*ubuf
)
1406 struct ptrauth_keys_user
*keys
= &target
->thread
.keys_user
;
1407 struct user_pac_generic_keys user_keys
;
1410 if (!system_supports_generic_auth())
1413 pac_generic_keys_to_user(&user_keys
, keys
);
1414 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
,
1418 pac_generic_keys_from_user(keys
, &user_keys
);
1422 #endif /* CONFIG_CHECKPOINT_RESTORE */
1423 #endif /* CONFIG_ARM64_PTR_AUTH */
1425 #ifdef CONFIG_ARM64_TAGGED_ADDR_ABI
1426 static int tagged_addr_ctrl_get(struct task_struct
*target
,
1427 const struct user_regset
*regset
,
1430 long ctrl
= get_tagged_addr_ctrl(target
);
1432 if (WARN_ON_ONCE(IS_ERR_VALUE(ctrl
)))
1435 return membuf_write(&to
, &ctrl
, sizeof(ctrl
));
1438 static int tagged_addr_ctrl_set(struct task_struct
*target
, const struct
1439 user_regset
*regset
, unsigned int pos
,
1440 unsigned int count
, const void *kbuf
, const
1446 ctrl
= get_tagged_addr_ctrl(target
);
1447 if (WARN_ON_ONCE(IS_ERR_VALUE(ctrl
)))
1450 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
, &ctrl
, 0, -1);
1454 return set_tagged_addr_ctrl(target
, ctrl
);
1458 #ifdef CONFIG_ARM64_POE
1459 static int poe_get(struct task_struct
*target
,
1460 const struct user_regset
*regset
,
1463 if (!system_supports_poe())
1466 return membuf_write(&to
, &target
->thread
.por_el0
,
1467 sizeof(target
->thread
.por_el0
));
1470 static int poe_set(struct task_struct
*target
, const struct
1471 user_regset
*regset
, unsigned int pos
,
1472 unsigned int count
, const void *kbuf
, const
1478 if (!system_supports_poe())
1481 ctrl
= target
->thread
.por_el0
;
1483 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
, &ctrl
, 0, -1);
1487 target
->thread
.por_el0
= ctrl
;
1493 #ifdef CONFIG_ARM64_GCS
1494 static void task_gcs_to_user(struct user_gcs
*user_gcs
,
1495 const struct task_struct
*target
)
1497 user_gcs
->features_enabled
= target
->thread
.gcs_el0_mode
;
1498 user_gcs
->features_locked
= target
->thread
.gcs_el0_locked
;
1499 user_gcs
->gcspr_el0
= target
->thread
.gcspr_el0
;
1502 static void task_gcs_from_user(struct task_struct
*target
,
1503 const struct user_gcs
*user_gcs
)
1505 target
->thread
.gcs_el0_mode
= user_gcs
->features_enabled
;
1506 target
->thread
.gcs_el0_locked
= user_gcs
->features_locked
;
1507 target
->thread
.gcspr_el0
= user_gcs
->gcspr_el0
;
1510 static int gcs_get(struct task_struct
*target
,
1511 const struct user_regset
*regset
,
1514 struct user_gcs user_gcs
;
1516 if (!system_supports_gcs())
1519 if (target
== current
)
1520 gcs_preserve_current_state();
1522 task_gcs_to_user(&user_gcs
, target
);
1524 return membuf_write(&to
, &user_gcs
, sizeof(user_gcs
));
1527 static int gcs_set(struct task_struct
*target
, const struct
1528 user_regset
*regset
, unsigned int pos
,
1529 unsigned int count
, const void *kbuf
, const
1533 struct user_gcs user_gcs
;
1535 if (!system_supports_gcs())
1538 task_gcs_to_user(&user_gcs
, target
);
1540 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
, &user_gcs
, 0, -1);
1544 if (user_gcs
.features_enabled
& ~PR_SHADOW_STACK_SUPPORTED_STATUS_MASK
)
1547 task_gcs_from_user(target
, &user_gcs
);
1553 enum aarch64_regset
{
1557 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1563 #ifdef CONFIG_ARM64_SVE
1566 #ifdef CONFIG_ARM64_SME
1571 #ifdef CONFIG_ARM64_PTR_AUTH
1573 REGSET_PAC_ENABLED_KEYS
,
1574 #ifdef CONFIG_CHECKPOINT_RESTORE
1579 #ifdef CONFIG_ARM64_TAGGED_ADDR_ABI
1580 REGSET_TAGGED_ADDR_CTRL
,
1582 #ifdef CONFIG_ARM64_POE
1585 #ifdef CONFIG_ARM64_GCS
1590 static const struct user_regset aarch64_regsets
[] = {
1592 .core_note_type
= NT_PRSTATUS
,
1593 .n
= sizeof(struct user_pt_regs
) / sizeof(u64
),
1594 .size
= sizeof(u64
),
1595 .align
= sizeof(u64
),
1596 .regset_get
= gpr_get
,
1600 .core_note_type
= NT_PRFPREG
,
1601 .n
= sizeof(struct user_fpsimd_state
) / sizeof(u32
),
1603 * We pretend we have 32-bit registers because the fpsr and
1604 * fpcr are 32-bits wide.
1606 .size
= sizeof(u32
),
1607 .align
= sizeof(u32
),
1608 .active
= fpr_active
,
1609 .regset_get
= fpr_get
,
1613 .core_note_type
= NT_ARM_TLS
,
1615 .size
= sizeof(void *),
1616 .align
= sizeof(void *),
1617 .regset_get
= tls_get
,
1620 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1621 [REGSET_HW_BREAK
] = {
1622 .core_note_type
= NT_ARM_HW_BREAK
,
1623 .n
= sizeof(struct user_hwdebug_state
) / sizeof(u32
),
1624 .size
= sizeof(u32
),
1625 .align
= sizeof(u32
),
1626 .regset_get
= hw_break_get
,
1627 .set
= hw_break_set
,
1629 [REGSET_HW_WATCH
] = {
1630 .core_note_type
= NT_ARM_HW_WATCH
,
1631 .n
= sizeof(struct user_hwdebug_state
) / sizeof(u32
),
1632 .size
= sizeof(u32
),
1633 .align
= sizeof(u32
),
1634 .regset_get
= hw_break_get
,
1635 .set
= hw_break_set
,
1638 [REGSET_SYSTEM_CALL
] = {
1639 .core_note_type
= NT_ARM_SYSTEM_CALL
,
1641 .size
= sizeof(int),
1642 .align
= sizeof(int),
1643 .regset_get
= system_call_get
,
1644 .set
= system_call_set
,
1647 .core_note_type
= NT_ARM_FPMR
,
1649 .size
= sizeof(u64
),
1650 .align
= sizeof(u64
),
1651 .regset_get
= fpmr_get
,
1654 #ifdef CONFIG_ARM64_SVE
1655 [REGSET_SVE
] = { /* Scalable Vector Extension */
1656 .core_note_type
= NT_ARM_SVE
,
1657 .n
= DIV_ROUND_UP(SVE_PT_SIZE(ARCH_SVE_VQ_MAX
,
1660 .size
= SVE_VQ_BYTES
,
1661 .align
= SVE_VQ_BYTES
,
1662 .regset_get
= sve_get
,
1666 #ifdef CONFIG_ARM64_SME
1667 [REGSET_SSVE
] = { /* Streaming mode SVE */
1668 .core_note_type
= NT_ARM_SSVE
,
1669 .n
= DIV_ROUND_UP(SVE_PT_SIZE(SME_VQ_MAX
, SVE_PT_REGS_SVE
),
1671 .size
= SVE_VQ_BYTES
,
1672 .align
= SVE_VQ_BYTES
,
1673 .regset_get
= ssve_get
,
1676 [REGSET_ZA
] = { /* SME ZA */
1677 .core_note_type
= NT_ARM_ZA
,
1679 * ZA is a single register but it's variably sized and
1680 * the ptrace core requires that the size of any data
1681 * be an exact multiple of the configured register
1682 * size so report as though we had SVE_VQ_BYTES
1683 * registers. These values aren't exposed to
1686 .n
= DIV_ROUND_UP(ZA_PT_SIZE(SME_VQ_MAX
), SVE_VQ_BYTES
),
1687 .size
= SVE_VQ_BYTES
,
1688 .align
= SVE_VQ_BYTES
,
1689 .regset_get
= za_get
,
1692 [REGSET_ZT
] = { /* SME ZT */
1693 .core_note_type
= NT_ARM_ZT
,
1695 .size
= ZT_SIG_REG_BYTES
,
1696 .align
= sizeof(u64
),
1697 .regset_get
= zt_get
,
1701 #ifdef CONFIG_ARM64_PTR_AUTH
1702 [REGSET_PAC_MASK
] = {
1703 .core_note_type
= NT_ARM_PAC_MASK
,
1704 .n
= sizeof(struct user_pac_mask
) / sizeof(u64
),
1705 .size
= sizeof(u64
),
1706 .align
= sizeof(u64
),
1707 .regset_get
= pac_mask_get
,
1708 /* this cannot be set dynamically */
1710 [REGSET_PAC_ENABLED_KEYS
] = {
1711 .core_note_type
= NT_ARM_PAC_ENABLED_KEYS
,
1713 .size
= sizeof(long),
1714 .align
= sizeof(long),
1715 .regset_get
= pac_enabled_keys_get
,
1716 .set
= pac_enabled_keys_set
,
1718 #ifdef CONFIG_CHECKPOINT_RESTORE
1719 [REGSET_PACA_KEYS
] = {
1720 .core_note_type
= NT_ARM_PACA_KEYS
,
1721 .n
= sizeof(struct user_pac_address_keys
) / sizeof(__uint128_t
),
1722 .size
= sizeof(__uint128_t
),
1723 .align
= sizeof(__uint128_t
),
1724 .regset_get
= pac_address_keys_get
,
1725 .set
= pac_address_keys_set
,
1727 [REGSET_PACG_KEYS
] = {
1728 .core_note_type
= NT_ARM_PACG_KEYS
,
1729 .n
= sizeof(struct user_pac_generic_keys
) / sizeof(__uint128_t
),
1730 .size
= sizeof(__uint128_t
),
1731 .align
= sizeof(__uint128_t
),
1732 .regset_get
= pac_generic_keys_get
,
1733 .set
= pac_generic_keys_set
,
1737 #ifdef CONFIG_ARM64_TAGGED_ADDR_ABI
1738 [REGSET_TAGGED_ADDR_CTRL
] = {
1739 .core_note_type
= NT_ARM_TAGGED_ADDR_CTRL
,
1741 .size
= sizeof(long),
1742 .align
= sizeof(long),
1743 .regset_get
= tagged_addr_ctrl_get
,
1744 .set
= tagged_addr_ctrl_set
,
1747 #ifdef CONFIG_ARM64_POE
1749 .core_note_type
= NT_ARM_POE
,
1751 .size
= sizeof(long),
1752 .align
= sizeof(long),
1753 .regset_get
= poe_get
,
1757 #ifdef CONFIG_ARM64_GCS
1759 .core_note_type
= NT_ARM_GCS
,
1760 .n
= sizeof(struct user_gcs
) / sizeof(u64
),
1761 .size
= sizeof(u64
),
1762 .align
= sizeof(u64
),
1763 .regset_get
= gcs_get
,
1769 static const struct user_regset_view user_aarch64_view
= {
1770 .name
= "aarch64", .e_machine
= EM_AARCH64
,
1771 .regsets
= aarch64_regsets
, .n
= ARRAY_SIZE(aarch64_regsets
)
1774 enum compat_regset
{
1779 static inline compat_ulong_t
compat_get_user_reg(struct task_struct
*task
, int idx
)
1781 struct pt_regs
*regs
= task_pt_regs(task
);
1787 return pstate_to_compat_psr(regs
->pstate
);
1789 return regs
->orig_x0
;
1791 return regs
->regs
[idx
];
1795 static int compat_gpr_get(struct task_struct
*target
,
1796 const struct user_regset
*regset
,
1802 membuf_store(&to
, compat_get_user_reg(target
, i
++));
1806 static int compat_gpr_set(struct task_struct
*target
,
1807 const struct user_regset
*regset
,
1808 unsigned int pos
, unsigned int count
,
1809 const void *kbuf
, const void __user
*ubuf
)
1811 struct pt_regs newregs
;
1813 unsigned int i
, start
, num_regs
;
1815 /* Calculate the number of AArch32 registers contained in count */
1816 num_regs
= count
/ regset
->size
;
1818 /* Convert pos into an register number */
1819 start
= pos
/ regset
->size
;
1821 if (start
+ num_regs
> regset
->n
)
1824 newregs
= *task_pt_regs(target
);
1826 for (i
= 0; i
< num_regs
; ++i
) {
1827 unsigned int idx
= start
+ i
;
1831 memcpy(®
, kbuf
, sizeof(reg
));
1832 kbuf
+= sizeof(reg
);
1834 ret
= copy_from_user(®
, ubuf
, sizeof(reg
));
1840 ubuf
+= sizeof(reg
);
1848 reg
= compat_psr_to_pstate(reg
);
1849 newregs
.pstate
= reg
;
1852 newregs
.orig_x0
= reg
;
1855 newregs
.regs
[idx
] = reg
;
1860 if (valid_user_regs(&newregs
.user_regs
, target
))
1861 *task_pt_regs(target
) = newregs
;
1868 static int compat_vfp_get(struct task_struct
*target
,
1869 const struct user_regset
*regset
,
1872 struct user_fpsimd_state
*uregs
;
1873 compat_ulong_t fpscr
;
1875 if (!system_supports_fpsimd())
1878 uregs
= &target
->thread
.uw
.fpsimd_state
;
1880 if (target
== current
)
1881 fpsimd_preserve_current_state();
1884 * The VFP registers are packed into the fpsimd_state, so they all sit
1885 * nicely together for us. We just need to create the fpscr separately.
1887 membuf_write(&to
, uregs
, VFP_STATE_SIZE
- sizeof(compat_ulong_t
));
1888 fpscr
= (uregs
->fpsr
& VFP_FPSCR_STAT_MASK
) |
1889 (uregs
->fpcr
& VFP_FPSCR_CTRL_MASK
);
1890 return membuf_store(&to
, fpscr
);
1893 static int compat_vfp_set(struct task_struct
*target
,
1894 const struct user_regset
*regset
,
1895 unsigned int pos
, unsigned int count
,
1896 const void *kbuf
, const void __user
*ubuf
)
1898 struct user_fpsimd_state
*uregs
;
1899 compat_ulong_t fpscr
;
1900 int ret
, vregs_end_pos
;
1902 if (!system_supports_fpsimd())
1905 uregs
= &target
->thread
.uw
.fpsimd_state
;
1907 vregs_end_pos
= VFP_STATE_SIZE
- sizeof(compat_ulong_t
);
1908 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
, uregs
, 0,
1911 if (count
&& !ret
) {
1912 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
, &fpscr
,
1913 vregs_end_pos
, VFP_STATE_SIZE
);
1915 uregs
->fpsr
= fpscr
& VFP_FPSCR_STAT_MASK
;
1916 uregs
->fpcr
= fpscr
& VFP_FPSCR_CTRL_MASK
;
1920 fpsimd_flush_task_state(target
);
1924 static int compat_tls_get(struct task_struct
*target
,
1925 const struct user_regset
*regset
,
1928 return membuf_store(&to
, (compat_ulong_t
)target
->thread
.uw
.tp_value
);
1931 static int compat_tls_set(struct task_struct
*target
,
1932 const struct user_regset
*regset
, unsigned int pos
,
1933 unsigned int count
, const void *kbuf
,
1934 const void __user
*ubuf
)
1937 compat_ulong_t tls
= target
->thread
.uw
.tp_value
;
1939 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
, &tls
, 0, -1);
1943 target
->thread
.uw
.tp_value
= tls
;
1947 static const struct user_regset aarch32_regsets
[] = {
1948 [REGSET_COMPAT_GPR
] = {
1949 .core_note_type
= NT_PRSTATUS
,
1950 .n
= COMPAT_ELF_NGREG
,
1951 .size
= sizeof(compat_elf_greg_t
),
1952 .align
= sizeof(compat_elf_greg_t
),
1953 .regset_get
= compat_gpr_get
,
1954 .set
= compat_gpr_set
1956 [REGSET_COMPAT_VFP
] = {
1957 .core_note_type
= NT_ARM_VFP
,
1958 .n
= VFP_STATE_SIZE
/ sizeof(compat_ulong_t
),
1959 .size
= sizeof(compat_ulong_t
),
1960 .align
= sizeof(compat_ulong_t
),
1961 .active
= fpr_active
,
1962 .regset_get
= compat_vfp_get
,
1963 .set
= compat_vfp_set
1967 static const struct user_regset_view user_aarch32_view
= {
1968 .name
= "aarch32", .e_machine
= EM_ARM
,
1969 .regsets
= aarch32_regsets
, .n
= ARRAY_SIZE(aarch32_regsets
)
1972 static const struct user_regset aarch32_ptrace_regsets
[] = {
1974 .core_note_type
= NT_PRSTATUS
,
1975 .n
= COMPAT_ELF_NGREG
,
1976 .size
= sizeof(compat_elf_greg_t
),
1977 .align
= sizeof(compat_elf_greg_t
),
1978 .regset_get
= compat_gpr_get
,
1979 .set
= compat_gpr_set
1982 .core_note_type
= NT_ARM_VFP
,
1983 .n
= VFP_STATE_SIZE
/ sizeof(compat_ulong_t
),
1984 .size
= sizeof(compat_ulong_t
),
1985 .align
= sizeof(compat_ulong_t
),
1986 .regset_get
= compat_vfp_get
,
1987 .set
= compat_vfp_set
1990 .core_note_type
= NT_ARM_TLS
,
1992 .size
= sizeof(compat_ulong_t
),
1993 .align
= sizeof(compat_ulong_t
),
1994 .regset_get
= compat_tls_get
,
1995 .set
= compat_tls_set
,
1997 #ifdef CONFIG_HAVE_HW_BREAKPOINT
1998 [REGSET_HW_BREAK
] = {
1999 .core_note_type
= NT_ARM_HW_BREAK
,
2000 .n
= sizeof(struct user_hwdebug_state
) / sizeof(u32
),
2001 .size
= sizeof(u32
),
2002 .align
= sizeof(u32
),
2003 .regset_get
= hw_break_get
,
2004 .set
= hw_break_set
,
2006 [REGSET_HW_WATCH
] = {
2007 .core_note_type
= NT_ARM_HW_WATCH
,
2008 .n
= sizeof(struct user_hwdebug_state
) / sizeof(u32
),
2009 .size
= sizeof(u32
),
2010 .align
= sizeof(u32
),
2011 .regset_get
= hw_break_get
,
2012 .set
= hw_break_set
,
2015 [REGSET_SYSTEM_CALL
] = {
2016 .core_note_type
= NT_ARM_SYSTEM_CALL
,
2018 .size
= sizeof(int),
2019 .align
= sizeof(int),
2020 .regset_get
= system_call_get
,
2021 .set
= system_call_set
,
2025 static const struct user_regset_view user_aarch32_ptrace_view
= {
2026 .name
= "aarch32", .e_machine
= EM_ARM
,
2027 .regsets
= aarch32_ptrace_regsets
, .n
= ARRAY_SIZE(aarch32_ptrace_regsets
)
2030 #ifdef CONFIG_COMPAT
2031 static int compat_ptrace_read_user(struct task_struct
*tsk
, compat_ulong_t off
,
2032 compat_ulong_t __user
*ret
)
2039 if (off
== COMPAT_PT_TEXT_ADDR
)
2040 tmp
= tsk
->mm
->start_code
;
2041 else if (off
== COMPAT_PT_DATA_ADDR
)
2042 tmp
= tsk
->mm
->start_data
;
2043 else if (off
== COMPAT_PT_TEXT_END_ADDR
)
2044 tmp
= tsk
->mm
->end_code
;
2045 else if (off
< sizeof(compat_elf_gregset_t
))
2046 tmp
= compat_get_user_reg(tsk
, off
>> 2);
2047 else if (off
>= COMPAT_USER_SZ
)
2052 return put_user(tmp
, ret
);
2055 static int compat_ptrace_write_user(struct task_struct
*tsk
, compat_ulong_t off
,
2058 struct pt_regs newregs
= *task_pt_regs(tsk
);
2059 unsigned int idx
= off
/ 4;
2061 if (off
& 3 || off
>= COMPAT_USER_SZ
)
2064 if (off
>= sizeof(compat_elf_gregset_t
))
2072 newregs
.pstate
= compat_psr_to_pstate(val
);
2075 newregs
.orig_x0
= val
;
2078 newregs
.regs
[idx
] = val
;
2081 if (!valid_user_regs(&newregs
.user_regs
, tsk
))
2084 *task_pt_regs(tsk
) = newregs
;
2088 #ifdef CONFIG_HAVE_HW_BREAKPOINT
2091 * Convert a virtual register number into an index for a thread_info
2092 * breakpoint array. Breakpoints are identified using positive numbers
2093 * whilst watchpoints are negative. The registers are laid out as pairs
2094 * of (address, control), each pair mapping to a unique hw_breakpoint struct.
2095 * Register 0 is reserved for describing resource information.
2097 static int compat_ptrace_hbp_num_to_idx(compat_long_t num
)
2099 return (abs(num
) - 1) >> 1;
2102 static int compat_ptrace_hbp_get_resource_info(u32
*kdata
)
2104 u8 num_brps
, num_wrps
, debug_arch
, wp_len
;
2107 num_brps
= hw_breakpoint_slots(TYPE_INST
);
2108 num_wrps
= hw_breakpoint_slots(TYPE_DATA
);
2110 debug_arch
= debug_monitors_arch();
2124 static int compat_ptrace_hbp_get(unsigned int note_type
,
2125 struct task_struct
*tsk
,
2132 int err
, idx
= compat_ptrace_hbp_num_to_idx(num
);
2135 err
= ptrace_hbp_get_addr(note_type
, tsk
, idx
, &addr
);
2138 err
= ptrace_hbp_get_ctrl(note_type
, tsk
, idx
, &ctrl
);
2145 static int compat_ptrace_hbp_set(unsigned int note_type
,
2146 struct task_struct
*tsk
,
2153 int err
, idx
= compat_ptrace_hbp_num_to_idx(num
);
2157 err
= ptrace_hbp_set_addr(note_type
, tsk
, idx
, addr
);
2160 err
= ptrace_hbp_set_ctrl(note_type
, tsk
, idx
, ctrl
);
2166 static int compat_ptrace_gethbpregs(struct task_struct
*tsk
, compat_long_t num
,
2167 compat_ulong_t __user
*data
)
2174 ret
= compat_ptrace_hbp_get(NT_ARM_HW_WATCH
, tsk
, num
, &kdata
);
2176 } else if (num
== 0) {
2177 ret
= compat_ptrace_hbp_get_resource_info(&kdata
);
2180 ret
= compat_ptrace_hbp_get(NT_ARM_HW_BREAK
, tsk
, num
, &kdata
);
2184 ret
= put_user(kdata
, data
);
2189 static int compat_ptrace_sethbpregs(struct task_struct
*tsk
, compat_long_t num
,
2190 compat_ulong_t __user
*data
)
2198 ret
= get_user(kdata
, data
);
2203 ret
= compat_ptrace_hbp_set(NT_ARM_HW_WATCH
, tsk
, num
, &kdata
);
2205 ret
= compat_ptrace_hbp_set(NT_ARM_HW_BREAK
, tsk
, num
, &kdata
);
2209 #endif /* CONFIG_HAVE_HW_BREAKPOINT */
2211 long compat_arch_ptrace(struct task_struct
*child
, compat_long_t request
,
2212 compat_ulong_t caddr
, compat_ulong_t cdata
)
2214 unsigned long addr
= caddr
;
2215 unsigned long data
= cdata
;
2216 void __user
*datap
= compat_ptr(data
);
2220 case PTRACE_PEEKUSR
:
2221 ret
= compat_ptrace_read_user(child
, addr
, datap
);
2224 case PTRACE_POKEUSR
:
2225 ret
= compat_ptrace_write_user(child
, addr
, data
);
2228 case COMPAT_PTRACE_GETREGS
:
2229 ret
= copy_regset_to_user(child
,
2232 0, sizeof(compat_elf_gregset_t
),
2236 case COMPAT_PTRACE_SETREGS
:
2237 ret
= copy_regset_from_user(child
,
2240 0, sizeof(compat_elf_gregset_t
),
2244 case COMPAT_PTRACE_GET_THREAD_AREA
:
2245 ret
= put_user((compat_ulong_t
)child
->thread
.uw
.tp_value
,
2246 (compat_ulong_t __user
*)datap
);
2249 case COMPAT_PTRACE_SET_SYSCALL
:
2250 task_pt_regs(child
)->syscallno
= data
;
2254 case COMPAT_PTRACE_GETVFPREGS
:
2255 ret
= copy_regset_to_user(child
,
2262 case COMPAT_PTRACE_SETVFPREGS
:
2263 ret
= copy_regset_from_user(child
,
2270 #ifdef CONFIG_HAVE_HW_BREAKPOINT
2271 case COMPAT_PTRACE_GETHBPREGS
:
2272 ret
= compat_ptrace_gethbpregs(child
, addr
, datap
);
2275 case COMPAT_PTRACE_SETHBPREGS
:
2276 ret
= compat_ptrace_sethbpregs(child
, addr
, datap
);
2281 ret
= compat_ptrace_request(child
, request
, addr
,
2288 #endif /* CONFIG_COMPAT */
2290 const struct user_regset_view
*task_user_regset_view(struct task_struct
*task
)
2293 * Core dumping of 32-bit tasks or compat ptrace requests must use the
2294 * user_aarch32_view compatible with arm32. Native ptrace requests on
2295 * 32-bit children use an extended user_aarch32_ptrace_view to allow
2296 * access to the TLS register.
2298 if (is_compat_task())
2299 return &user_aarch32_view
;
2300 else if (is_compat_thread(task_thread_info(task
)))
2301 return &user_aarch32_ptrace_view
;
2303 return &user_aarch64_view
;
2306 long arch_ptrace(struct task_struct
*child
, long request
,
2307 unsigned long addr
, unsigned long data
)
2310 case PTRACE_PEEKMTETAGS
:
2311 case PTRACE_POKEMTETAGS
:
2312 return mte_ptrace_copy_tags(child
, request
, addr
, data
);
2315 return ptrace_request(child
, request
, addr
, data
);
2318 enum ptrace_syscall_dir
{
2319 PTRACE_SYSCALL_ENTER
= 0,
2320 PTRACE_SYSCALL_EXIT
,
2323 static void report_syscall(struct pt_regs
*regs
, enum ptrace_syscall_dir dir
)
2326 unsigned long saved_reg
;
2329 * We have some ABI weirdness here in the way that we handle syscall
2330 * exit stops because we indicate whether or not the stop has been
2331 * signalled from syscall entry or syscall exit by clobbering a general
2332 * purpose register (ip/r12 for AArch32, x7 for AArch64) in the tracee
2333 * and restoring its old value after the stop. This means that:
2335 * - Any writes by the tracer to this register during the stop are
2336 * ignored/discarded.
2338 * - The actual value of the register is not available during the stop,
2339 * so the tracer cannot save it and restore it later.
2341 * - Syscall stops behave differently to seccomp and pseudo-step traps
2342 * (the latter do not nobble any registers).
2344 regno
= (is_compat_task() ? 12 : 7);
2345 saved_reg
= regs
->regs
[regno
];
2346 regs
->regs
[regno
] = dir
;
2348 if (dir
== PTRACE_SYSCALL_ENTER
) {
2349 if (ptrace_report_syscall_entry(regs
))
2350 forget_syscall(regs
);
2351 regs
->regs
[regno
] = saved_reg
;
2352 } else if (!test_thread_flag(TIF_SINGLESTEP
)) {
2353 ptrace_report_syscall_exit(regs
, 0);
2354 regs
->regs
[regno
] = saved_reg
;
2356 regs
->regs
[regno
] = saved_reg
;
2359 * Signal a pseudo-step exception since we are stepping but
2360 * tracer modifications to the registers may have rewound the
2363 ptrace_report_syscall_exit(regs
, 1);
2367 int syscall_trace_enter(struct pt_regs
*regs
)
2369 unsigned long flags
= read_thread_flags();
2371 if (flags
& (_TIF_SYSCALL_EMU
| _TIF_SYSCALL_TRACE
)) {
2372 report_syscall(regs
, PTRACE_SYSCALL_ENTER
);
2373 if (flags
& _TIF_SYSCALL_EMU
)
2377 /* Do the secure computing after ptrace; failures should be fast. */
2378 if (secure_computing() == -1)
2381 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT
))
2382 trace_sys_enter(regs
, regs
->syscallno
);
2384 audit_syscall_entry(regs
->syscallno
, regs
->orig_x0
, regs
->regs
[1],
2385 regs
->regs
[2], regs
->regs
[3]);
2387 return regs
->syscallno
;
2390 void syscall_trace_exit(struct pt_regs
*regs
)
2392 unsigned long flags
= read_thread_flags();
2394 audit_syscall_exit(regs
);
2396 if (flags
& _TIF_SYSCALL_TRACEPOINT
)
2397 trace_sys_exit(regs
, syscall_get_return_value(current
, regs
));
2399 if (flags
& (_TIF_SYSCALL_TRACE
| _TIF_SINGLESTEP
))
2400 report_syscall(regs
, PTRACE_SYSCALL_EXIT
);
2406 * SPSR_ELx bits which are always architecturally RES0 per ARM DDI 0487D.a.
2407 * We permit userspace to set SSBS (AArch64 bit 12, AArch32 bit 23) which is
2408 * not described in ARM DDI 0487D.a.
2409 * We treat PAN and UAO as RES0 bits, as they are meaningless at EL0, and may
2410 * be allocated an EL0 meaning in future.
2411 * Userspace cannot use these until they have an architectural meaning.
2412 * Note that this follows the SPSR_ELx format, not the AArch32 PSR format.
2413 * We also reserve IL for the kernel; SS is handled dynamically.
2415 #define SPSR_EL1_AARCH64_RES0_BITS \
2416 (GENMASK_ULL(63, 32) | GENMASK_ULL(27, 26) | GENMASK_ULL(23, 22) | \
2417 GENMASK_ULL(20, 13) | GENMASK_ULL(5, 5))
2418 #define SPSR_EL1_AARCH32_RES0_BITS \
2419 (GENMASK_ULL(63, 32) | GENMASK_ULL(22, 22) | GENMASK_ULL(20, 20))
2421 static int valid_compat_regs(struct user_pt_regs
*regs
)
2423 regs
->pstate
&= ~SPSR_EL1_AARCH32_RES0_BITS
;
2425 if (!system_supports_mixed_endian_el0()) {
2426 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN
))
2427 regs
->pstate
|= PSR_AA32_E_BIT
;
2429 regs
->pstate
&= ~PSR_AA32_E_BIT
;
2432 if (user_mode(regs
) && (regs
->pstate
& PSR_MODE32_BIT
) &&
2433 (regs
->pstate
& PSR_AA32_A_BIT
) == 0 &&
2434 (regs
->pstate
& PSR_AA32_I_BIT
) == 0 &&
2435 (regs
->pstate
& PSR_AA32_F_BIT
) == 0) {
2440 * Force PSR to a valid 32-bit EL0t, preserving the same bits as
2443 regs
->pstate
&= PSR_AA32_N_BIT
| PSR_AA32_Z_BIT
|
2444 PSR_AA32_C_BIT
| PSR_AA32_V_BIT
|
2445 PSR_AA32_Q_BIT
| PSR_AA32_IT_MASK
|
2446 PSR_AA32_GE_MASK
| PSR_AA32_E_BIT
|
2448 regs
->pstate
|= PSR_MODE32_BIT
;
2453 static int valid_native_regs(struct user_pt_regs
*regs
)
2455 regs
->pstate
&= ~SPSR_EL1_AARCH64_RES0_BITS
;
2457 if (user_mode(regs
) && !(regs
->pstate
& PSR_MODE32_BIT
) &&
2458 (regs
->pstate
& PSR_D_BIT
) == 0 &&
2459 (regs
->pstate
& PSR_A_BIT
) == 0 &&
2460 (regs
->pstate
& PSR_I_BIT
) == 0 &&
2461 (regs
->pstate
& PSR_F_BIT
) == 0) {
2465 /* Force PSR to a valid 64-bit EL0t */
2466 regs
->pstate
&= PSR_N_BIT
| PSR_Z_BIT
| PSR_C_BIT
| PSR_V_BIT
;
2472 * Are the current registers suitable for user mode? (used to maintain
2473 * security in signal handlers)
2475 int valid_user_regs(struct user_pt_regs
*regs
, struct task_struct
*task
)
2477 /* https://lore.kernel.org/lkml/20191118131525.GA4180@willie-the-truck */
2478 user_regs_reset_single_step(regs
, task
);
2480 if (is_compat_thread(task_thread_info(task
)))
2481 return valid_compat_regs(regs
);
2483 return valid_native_regs(regs
);