1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2014-2016 Pratyush Anand <panand@redhat.com>
5 #include <linux/highmem.h>
6 #include <linux/ptrace.h>
7 #include <linux/uprobes.h>
8 #include <asm/cacheflush.h>
10 #include "decode-insn.h"
12 #define UPROBE_INV_FAULT_CODE UINT_MAX
14 void arch_uprobe_copy_ixol(struct page
*page
, unsigned long vaddr
,
15 void *src
, unsigned long len
)
17 void *xol_page_kaddr
= kmap_atomic(page
);
18 void *dst
= xol_page_kaddr
+ (vaddr
& ~PAGE_MASK
);
20 /* Initialize the slot */
21 memcpy(dst
, src
, len
);
23 /* flush caches (dcache/icache) */
24 sync_icache_aliases(dst
, len
);
26 kunmap_atomic(xol_page_kaddr
);
29 unsigned long uprobe_get_swbp_addr(struct pt_regs
*regs
)
31 return instruction_pointer(regs
);
34 int arch_uprobe_analyze_insn(struct arch_uprobe
*auprobe
, struct mm_struct
*mm
,
39 /* TODO: Currently we do not support AARCH32 instruction probing */
40 if (mm
->context
.flags
& MMCF_AARCH32
)
42 else if (!IS_ALIGNED(addr
, AARCH64_INSN_SIZE
))
45 insn
= *(probe_opcode_t
*)(&auprobe
->insn
[0]);
47 switch (arm_probe_decode_insn(insn
, &auprobe
->api
)) {
51 case INSN_GOOD_NO_SLOT
:
52 auprobe
->simulate
= true;
62 int arch_uprobe_pre_xol(struct arch_uprobe
*auprobe
, struct pt_regs
*regs
)
64 struct uprobe_task
*utask
= current
->utask
;
66 /* Initialize with an invalid fault code to detect if ol insn trapped */
67 current
->thread
.fault_code
= UPROBE_INV_FAULT_CODE
;
69 /* Instruction points to execute ol */
70 instruction_pointer_set(regs
, utask
->xol_vaddr
);
72 user_enable_single_step(current
);
77 int arch_uprobe_post_xol(struct arch_uprobe
*auprobe
, struct pt_regs
*regs
)
79 struct uprobe_task
*utask
= current
->utask
;
81 WARN_ON_ONCE(current
->thread
.fault_code
!= UPROBE_INV_FAULT_CODE
);
83 /* Instruction points to execute next to breakpoint address */
84 instruction_pointer_set(regs
, utask
->vaddr
+ 4);
86 user_disable_single_step(current
);
90 bool arch_uprobe_xol_was_trapped(struct task_struct
*t
)
93 * Between arch_uprobe_pre_xol and arch_uprobe_post_xol, if an xol
94 * insn itself is trapped, then detect the case with the help of
95 * invalid fault code which is being set in arch_uprobe_pre_xol
97 if (t
->thread
.fault_code
!= UPROBE_INV_FAULT_CODE
)
103 bool arch_uprobe_skip_sstep(struct arch_uprobe
*auprobe
, struct pt_regs
*regs
)
108 if (!auprobe
->simulate
)
111 insn
= *(probe_opcode_t
*)(&auprobe
->insn
[0]);
112 addr
= instruction_pointer(regs
);
114 if (auprobe
->api
.handler
)
115 auprobe
->api
.handler(insn
, addr
, regs
);
120 void arch_uprobe_abort_xol(struct arch_uprobe
*auprobe
, struct pt_regs
*regs
)
122 struct uprobe_task
*utask
= current
->utask
;
125 * Task has received a fatal signal, so reset back to probbed
128 instruction_pointer_set(regs
, utask
->vaddr
);
130 user_disable_single_step(current
);
133 bool arch_uretprobe_is_alive(struct return_instance
*ret
, enum rp_check ctx
,
134 struct pt_regs
*regs
)
137 * If a simple branch instruction (B) was called for retprobed
138 * assembly label then return true even when regs->sp and ret->stack
139 * are same. It will ensure that cleanup and reporting of return
140 * instances corresponding to callee label is done when
141 * handle_trampoline for called function is executed.
143 if (ctx
== RP_CHECK_CHAIN_CALL
)
144 return regs
->sp
<= ret
->stack
;
146 return regs
->sp
< ret
->stack
;
150 arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr
,
151 struct pt_regs
*regs
)
153 unsigned long orig_ret_vaddr
;
155 orig_ret_vaddr
= procedure_link_pointer(regs
);
156 /* Replace the return addr with trampoline addr */
157 procedure_link_pointer_set(regs
, trampoline_vaddr
);
159 return orig_ret_vaddr
;
162 int arch_uprobe_exception_notify(struct notifier_block
*self
,
163 unsigned long val
, void *data
)
168 static int uprobe_breakpoint_handler(struct pt_regs
*regs
,
171 if (uprobe_pre_sstep_notifier(regs
))
172 return DBG_HOOK_HANDLED
;
174 return DBG_HOOK_ERROR
;
177 static int uprobe_single_step_handler(struct pt_regs
*regs
,
180 struct uprobe_task
*utask
= current
->utask
;
182 WARN_ON(utask
&& (instruction_pointer(regs
) != utask
->xol_vaddr
+ 4));
183 if (uprobe_post_sstep_notifier(regs
))
184 return DBG_HOOK_HANDLED
;
186 return DBG_HOOK_ERROR
;
189 /* uprobe breakpoint handler hook */
190 static struct break_hook uprobes_break_hook
= {
191 .imm
= UPROBES_BRK_IMM
,
192 .fn
= uprobe_breakpoint_handler
,
195 /* uprobe single step handler hook */
196 static struct step_hook uprobes_step_hook
= {
197 .fn
= uprobe_single_step_handler
,
200 static int __init
arch_init_uprobes(void)
202 register_user_break_hook(&uprobes_break_hook
);
203 register_user_step_hook(&uprobes_step_hook
);
208 device_initcall(arch_init_uprobes
);