]>
Commit | Line | Data |
---|---|---|
1a59d1b8 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 LT |
2 | /* |
3 | * Kernel Probes (KProbes) | |
1da177e4 | 4 | * |
1da177e4 LT |
5 | * Copyright (C) IBM Corporation, 2002, 2004 |
6 | * | |
7 | * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel | |
8 | * Probes initial implementation ( includes contributions from | |
9 | * Rusty Russell). | |
10 | * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes | |
11 | * interface to access function arguments. | |
d6be29b8 MH |
12 | * 2004-Oct Jim Keniston <jkenisto@us.ibm.com> and Prasanna S Panchamukhi |
13 | * <prasanna@in.ibm.com> adapted for x86_64 from i386. | |
1da177e4 LT |
14 | * 2005-Mar Roland McGrath <roland@redhat.com> |
15 | * Fixed to handle %rip-relative addressing mode correctly. | |
d6be29b8 MH |
16 | * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston |
17 | * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi | |
18 | * <prasanna@in.ibm.com> added function-return probes. | |
19 | * 2005-May Rusty Lynch <rusty.lynch@intel.com> | |
3f33ab1c | 20 | * Added function return probes functionality |
d6be29b8 | 21 | * 2006-Feb Masami Hiramatsu <hiramatu@sdl.hitachi.co.jp> added |
3f33ab1c | 22 | * kprobe-booster and kretprobe-booster for i386. |
da07ab03 | 23 | * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com> added kprobe-booster |
3f33ab1c | 24 | * and kretprobe-booster for x86-64 |
d6be29b8 | 25 | * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com>, Arjan van de Ven |
3f33ab1c MH |
26 | * <arjan@infradead.org> and Jim Keniston <jkenisto@us.ibm.com> |
27 | * unified x86 kprobes code. | |
1da177e4 | 28 | */ |
1da177e4 LT |
29 | #include <linux/kprobes.h> |
30 | #include <linux/ptrace.h> | |
1da177e4 LT |
31 | #include <linux/string.h> |
32 | #include <linux/slab.h> | |
b506a9d0 | 33 | #include <linux/hardirq.h> |
1da177e4 | 34 | #include <linux/preempt.h> |
b17b0153 | 35 | #include <linux/sched/debug.h> |
744c193e | 36 | #include <linux/extable.h> |
1eeb66a1 | 37 | #include <linux/kdebug.h> |
b46b3d70 | 38 | #include <linux/kallsyms.h> |
c0f7ac3a | 39 | #include <linux/ftrace.h> |
87aaff2a | 40 | #include <linux/frame.h> |
9f7d416c | 41 | #include <linux/kasan.h> |
c93f5cf5 | 42 | #include <linux/moduleloader.h> |
6315ec92 | 43 | #include <linux/vmalloc.h> |
65fddcfc | 44 | #include <linux/pgtable.h> |
9ec4b1f3 | 45 | |
35de5b06 | 46 | #include <asm/text-patching.h> |
8533bbe9 MH |
47 | #include <asm/cacheflush.h> |
48 | #include <asm/desc.h> | |
7c0f6ba6 | 49 | #include <linux/uaccess.h> |
19d36ccd | 50 | #include <asm/alternative.h> |
b46b3d70 | 51 | #include <asm/insn.h> |
62edab90 | 52 | #include <asm/debugreg.h> |
e6ccbff0 | 53 | #include <asm/set_memory.h> |
1da177e4 | 54 | |
f684199f | 55 | #include "common.h" |
3f33ab1c | 56 | |
e7a510f9 AM |
57 | DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL; |
58 | DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); | |
1da177e4 | 59 | |
3c88c692 | 60 | #define stack_addr(regs) ((unsigned long *)regs->sp) |
8533bbe9 MH |
61 | |
62 | #define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\ | |
63 | (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \ | |
64 | (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \ | |
65 | (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \ | |
66 | (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \ | |
67 | << (row % 32)) | |
68 | /* | |
69 | * Undefined/reserved opcodes, conditional jump, Opcode Extension | |
70 | * Groups, and some special opcodes can not boost. | |
7115e3fc LT |
71 | * This is non-const and volatile to keep gcc from statically |
72 | * optimizing it out, as variable_test_bit makes gcc think only | |
f684199f | 73 | * *(unsigned long*) is used. |
8533bbe9 | 74 | */ |
7115e3fc | 75 | static volatile u32 twobyte_is_boostable[256 / 32] = { |
8533bbe9 MH |
76 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ |
77 | /* ---------------------------------------------- */ | |
78 | W(0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0) | /* 00 */ | |
b7e37567 | 79 | W(0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1) , /* 10 */ |
8533bbe9 MH |
80 | W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 20 */ |
81 | W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */ | |
82 | W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */ | |
83 | W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */ | |
84 | W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1) | /* 60 */ | |
85 | W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */ | |
86 | W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 80 */ | |
87 | W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */ | |
88 | W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* a0 */ | |
89 | W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) , /* b0 */ | |
90 | W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */ | |
91 | W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) , /* d0 */ | |
92 | W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* e0 */ | |
93 | W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0) /* f0 */ | |
94 | /* ----------------------------------------------- */ | |
95 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ | |
96 | }; | |
8533bbe9 MH |
97 | #undef W |
98 | ||
f438d914 MH |
99 | struct kretprobe_blackpoint kretprobe_blacklist[] = { |
100 | {"__switch_to", }, /* This function switches only current task, but | |
101 | doesn't switch kernel stack.*/ | |
102 | {NULL, NULL} /* Terminator */ | |
103 | }; | |
3f33ab1c | 104 | |
f438d914 MH |
105 | const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist); |
106 | ||
9326638c | 107 | static nokprobe_inline void |
63fef14f | 108 | __synthesize_relative_insn(void *dest, void *from, void *to, u8 op) |
aa470140 | 109 | { |
c0f7ac3a MH |
110 | struct __arch_relative_insn { |
111 | u8 op; | |
aa470140 | 112 | s32 raddr; |
f684199f | 113 | } __packed *insn; |
c0f7ac3a | 114 | |
63fef14f | 115 | insn = (struct __arch_relative_insn *)dest; |
c0f7ac3a MH |
116 | insn->raddr = (s32)((long)(to) - ((long)(from) + 5)); |
117 | insn->op = op; | |
118 | } | |
119 | ||
120 | /* Insert a jump instruction at address 'from', which jumps to address 'to'.*/ | |
63fef14f | 121 | void synthesize_reljump(void *dest, void *from, void *to) |
c0f7ac3a | 122 | { |
ab09e95c | 123 | __synthesize_relative_insn(dest, from, to, JMP32_INSN_OPCODE); |
aa470140 | 124 | } |
9326638c | 125 | NOKPROBE_SYMBOL(synthesize_reljump); |
aa470140 | 126 | |
3f33ab1c | 127 | /* Insert a call instruction at address 'from', which calls address 'to'.*/ |
63fef14f | 128 | void synthesize_relcall(void *dest, void *from, void *to) |
3f33ab1c | 129 | { |
ab09e95c | 130 | __synthesize_relative_insn(dest, from, to, CALL_INSN_OPCODE); |
3f33ab1c | 131 | } |
9326638c | 132 | NOKPROBE_SYMBOL(synthesize_relcall); |
3f33ab1c | 133 | |
9930927f | 134 | /* |
567a9fd8 | 135 | * Skip the prefixes of the instruction. |
9930927f | 136 | */ |
9326638c | 137 | static kprobe_opcode_t *skip_prefixes(kprobe_opcode_t *insn) |
9930927f | 138 | { |
567a9fd8 MH |
139 | insn_attr_t attr; |
140 | ||
141 | attr = inat_get_opcode_attribute((insn_byte_t)*insn); | |
142 | while (inat_is_legacy_prefix(attr)) { | |
143 | insn++; | |
144 | attr = inat_get_opcode_attribute((insn_byte_t)*insn); | |
145 | } | |
9930927f | 146 | #ifdef CONFIG_X86_64 |
567a9fd8 MH |
147 | if (inat_is_rex_prefix(attr)) |
148 | insn++; | |
9930927f | 149 | #endif |
567a9fd8 | 150 | return insn; |
9930927f | 151 | } |
9326638c | 152 | NOKPROBE_SYMBOL(skip_prefixes); |
9930927f | 153 | |
aa470140 | 154 | /* |
a8d11cd0 | 155 | * Returns non-zero if INSN is boostable. |
d6be29b8 | 156 | * RIP relative instructions are adjusted at copying time in 64 bits mode |
aa470140 | 157 | */ |
a8d11cd0 | 158 | int can_boost(struct insn *insn, void *addr) |
aa470140 | 159 | { |
aa470140 | 160 | kprobe_opcode_t opcode; |
aa470140 | 161 | |
75013fb1 | 162 | if (search_exception_tables((unsigned long)addr)) |
30390880 MH |
163 | return 0; /* Page fault may occur on this address. */ |
164 | ||
aa470140 | 165 | /* 2nd-byte opcode */ |
a8d11cd0 MH |
166 | if (insn->opcode.nbytes == 2) |
167 | return test_bit(insn->opcode.bytes[1], | |
8533bbe9 | 168 | (unsigned long *)twobyte_is_boostable); |
17880e4d | 169 | |
a8d11cd0 | 170 | if (insn->opcode.nbytes != 1) |
17880e4d MH |
171 | return 0; |
172 | ||
173 | /* Can't boost Address-size override prefix */ | |
a8d11cd0 | 174 | if (unlikely(inat_is_address_size_prefix(insn->attr))) |
17880e4d MH |
175 | return 0; |
176 | ||
a8d11cd0 | 177 | opcode = insn->opcode.bytes[0]; |
aa470140 MH |
178 | |
179 | switch (opcode & 0xf0) { | |
aa470140 | 180 | case 0x60: |
17880e4d MH |
181 | /* can't boost "bound" */ |
182 | return (opcode != 0x62); | |
aa470140 MH |
183 | case 0x70: |
184 | return 0; /* can't boost conditional jump */ | |
bd0b9067 MH |
185 | case 0x90: |
186 | return opcode != 0x9a; /* can't boost call far */ | |
aa470140 MH |
187 | case 0xc0: |
188 | /* can't boost software-interruptions */ | |
189 | return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf; | |
190 | case 0xd0: | |
191 | /* can boost AA* and XLAT */ | |
192 | return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7); | |
193 | case 0xe0: | |
194 | /* can boost in/out and absolute jmps */ | |
195 | return ((opcode & 0x04) || opcode == 0xea); | |
196 | case 0xf0: | |
aa470140 MH |
197 | /* clear and set flags are boostable */ |
198 | return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe)); | |
199 | default: | |
aa470140 MH |
200 | /* CS override prefix and call are not boostable */ |
201 | return (opcode != 0x2e && opcode != 0x9a); | |
202 | } | |
203 | } | |
204 | ||
3f33ab1c MH |
205 | static unsigned long |
206 | __recover_probed_insn(kprobe_opcode_t *buf, unsigned long addr) | |
b46b3d70 MH |
207 | { |
208 | struct kprobe *kp; | |
650b7b23 | 209 | unsigned long faddr; |
86b4ce31 | 210 | |
b46b3d70 | 211 | kp = get_kprobe((void *)addr); |
650b7b23 | 212 | faddr = ftrace_location(addr); |
2a6730c8 PM |
213 | /* |
214 | * Addresses inside the ftrace location are refused by | |
215 | * arch_check_ftrace_location(). Something went terribly wrong | |
216 | * if such an address is checked here. | |
217 | */ | |
218 | if (WARN_ON(faddr && faddr != addr)) | |
219 | return 0UL; | |
650b7b23 PM |
220 | /* |
221 | * Use the current code if it is not modified by Kprobe | |
222 | * and it cannot be modified by ftrace. | |
223 | */ | |
224 | if (!kp && !faddr) | |
86b4ce31 | 225 | return addr; |
b46b3d70 MH |
226 | |
227 | /* | |
650b7b23 PM |
228 | * Basically, kp->ainsn.insn has an original instruction. |
229 | * However, RIP-relative instruction can not do single-stepping | |
230 | * at different place, __copy_instruction() tweaks the displacement of | |
231 | * that instruction. In that case, we can't recover the instruction | |
232 | * from the kp->ainsn.insn. | |
233 | * | |
234 | * On the other hand, in case on normal Kprobe, kp->opcode has a copy | |
235 | * of the first byte of the probed instruction, which is overwritten | |
236 | * by int3. And the instruction at kp->addr is not modified by kprobes | |
237 | * except for the first byte, we can recover the original instruction | |
238 | * from it and kp->opcode. | |
b46b3d70 | 239 | * |
650b7b23 PM |
240 | * In case of Kprobes using ftrace, we do not have a copy of |
241 | * the original instruction. In fact, the ftrace location might | |
242 | * be modified at anytime and even could be in an inconsistent state. | |
243 | * Fortunately, we know that the original code is the ideal 5-byte | |
244 | * long NOP. | |
b46b3d70 | 245 | */ |
ea1e34fc MH |
246 | if (probe_kernel_read(buf, (void *)addr, |
247 | MAX_INSN_SIZE * sizeof(kprobe_opcode_t))) | |
248 | return 0UL; | |
249 | ||
650b7b23 PM |
250 | if (faddr) |
251 | memcpy(buf, ideal_nops[NOP_ATOMIC5], 5); | |
252 | else | |
253 | buf[0] = kp->opcode; | |
86b4ce31 MH |
254 | return (unsigned long)buf; |
255 | } | |
256 | ||
86b4ce31 MH |
257 | /* |
258 | * Recover the probed instruction at addr for further analysis. | |
259 | * Caller must lock kprobes by kprobe_mutex, or disable preemption | |
260 | * for preventing to release referencing kprobes. | |
ea1e34fc | 261 | * Returns zero if the instruction can not get recovered (or access failed). |
86b4ce31 | 262 | */ |
3f33ab1c | 263 | unsigned long recover_probed_instruction(kprobe_opcode_t *buf, unsigned long addr) |
86b4ce31 MH |
264 | { |
265 | unsigned long __addr; | |
266 | ||
267 | __addr = __recover_optprobed_insn(buf, addr); | |
268 | if (__addr != addr) | |
269 | return __addr; | |
270 | ||
271 | return __recover_probed_insn(buf, addr); | |
b46b3d70 MH |
272 | } |
273 | ||
b46b3d70 | 274 | /* Check if paddr is at an instruction boundary */ |
7ec8a97a | 275 | static int can_probe(unsigned long paddr) |
b46b3d70 | 276 | { |
86b4ce31 | 277 | unsigned long addr, __addr, offset = 0; |
b46b3d70 MH |
278 | struct insn insn; |
279 | kprobe_opcode_t buf[MAX_INSN_SIZE]; | |
280 | ||
6abded71 | 281 | if (!kallsyms_lookup_size_offset(paddr, NULL, &offset)) |
b46b3d70 MH |
282 | return 0; |
283 | ||
284 | /* Decode instructions */ | |
285 | addr = paddr - offset; | |
286 | while (addr < paddr) { | |
b46b3d70 MH |
287 | /* |
288 | * Check if the instruction has been modified by another | |
289 | * kprobe, in which case we replace the breakpoint by the | |
290 | * original instruction in our buffer. | |
86b4ce31 MH |
291 | * Also, jump optimization will change the breakpoint to |
292 | * relative-jump. Since the relative-jump itself is | |
293 | * normally used, we just go through if there is no kprobe. | |
b46b3d70 | 294 | */ |
86b4ce31 | 295 | __addr = recover_probed_instruction(buf, addr); |
2a6730c8 PM |
296 | if (!__addr) |
297 | return 0; | |
6ba48ff4 | 298 | kernel_insn_init(&insn, (void *)__addr, MAX_INSN_SIZE); |
b46b3d70 | 299 | insn_get_length(&insn); |
86b4ce31 MH |
300 | |
301 | /* | |
302 | * Another debugging subsystem might insert this breakpoint. | |
303 | * In that case, we can't recover it. | |
304 | */ | |
ab09e95c | 305 | if (insn.opcode.bytes[0] == INT3_INSN_OPCODE) |
86b4ce31 | 306 | return 0; |
b46b3d70 MH |
307 | addr += insn.length; |
308 | } | |
309 | ||
310 | return (addr == paddr); | |
311 | } | |
312 | ||
1da177e4 | 313 | /* |
d6be29b8 | 314 | * Returns non-zero if opcode modifies the interrupt flag. |
1da177e4 | 315 | */ |
7ec8a97a | 316 | static int is_IF_modifier(kprobe_opcode_t *insn) |
1da177e4 | 317 | { |
567a9fd8 MH |
318 | /* Skip prefixes */ |
319 | insn = skip_prefixes(insn); | |
320 | ||
1da177e4 LT |
321 | switch (*insn) { |
322 | case 0xfa: /* cli */ | |
323 | case 0xfb: /* sti */ | |
324 | case 0xcf: /* iret/iretd */ | |
325 | case 0x9d: /* popf/popfd */ | |
326 | return 1; | |
327 | } | |
9930927f | 328 | |
1da177e4 LT |
329 | return 0; |
330 | } | |
331 | ||
332 | /* | |
129d17e8 MH |
333 | * Copy an instruction with recovering modified instruction by kprobes |
334 | * and adjust the displacement if the instruction uses the %rip-relative | |
63fef14f MH |
335 | * addressing mode. Note that since @real will be the final place of copied |
336 | * instruction, displacement must be adjust by @real, not @dest. | |
129d17e8 | 337 | * This returns the length of copied instruction, or 0 if it has an error. |
1da177e4 | 338 | */ |
63fef14f | 339 | int __copy_instruction(u8 *dest, u8 *src, u8 *real, struct insn *insn) |
1da177e4 | 340 | { |
c0f7ac3a | 341 | kprobe_opcode_t buf[MAX_INSN_SIZE]; |
6ba48ff4 DH |
342 | unsigned long recovered_insn = |
343 | recover_probed_instruction(buf, (unsigned long)src); | |
86b4ce31 | 344 | |
a8d11cd0 | 345 | if (!recovered_insn || !insn) |
2a6730c8 | 346 | return 0; |
c80e5c0c | 347 | |
a8d11cd0 MH |
348 | /* This can access kernel text if given address is not recovered */ |
349 | if (probe_kernel_read(dest, (void *)recovered_insn, MAX_INSN_SIZE)) | |
86b4ce31 | 350 | return 0; |
ea1e34fc | 351 | |
a8d11cd0 MH |
352 | kernel_insn_init(insn, dest, MAX_INSN_SIZE); |
353 | insn_get_length(insn); | |
354 | ||
004e8dce MH |
355 | /* We can not probe force emulate prefixed instruction */ |
356 | if (insn_has_emulate_prefix(insn)) | |
357 | return 0; | |
358 | ||
a8d11cd0 | 359 | /* Another subsystem puts a breakpoint, failed to recover */ |
ab09e95c | 360 | if (insn->opcode.bytes[0] == INT3_INSN_OPCODE) |
ea1e34fc | 361 | return 0; |
c0f7ac3a | 362 | |
ee6a7354 MH |
363 | /* We should not singlestep on the exception masking instructions */ |
364 | if (insn_masking_exception(insn)) | |
365 | return 0; | |
366 | ||
c0f7ac3a | 367 | #ifdef CONFIG_X86_64 |
129d17e8 | 368 | /* Only x86_64 has RIP relative instructions */ |
a8d11cd0 | 369 | if (insn_rip_relative(insn)) { |
89ae465b MH |
370 | s64 newdisp; |
371 | u8 *disp; | |
89ae465b MH |
372 | /* |
373 | * The copied instruction uses the %rip-relative addressing | |
374 | * mode. Adjust the displacement for the difference between | |
375 | * the original location of this instruction and the location | |
376 | * of the copy that will actually be run. The tricky bit here | |
377 | * is making sure that the sign extension happens correctly in | |
378 | * this calculation, since we need a signed 32-bit result to | |
379 | * be sign-extended to 64 bits when it's added to the %rip | |
380 | * value and yield the same 64-bit result that the sign- | |
381 | * extension of the original signed 32-bit displacement would | |
382 | * have given. | |
383 | */ | |
a8d11cd0 | 384 | newdisp = (u8 *) src + (s64) insn->displacement.value |
63fef14f | 385 | - (u8 *) real; |
8101376d MH |
386 | if ((s64) (s32) newdisp != newdisp) { |
387 | pr_err("Kprobes error: new displacement does not fit into s32 (%llx)\n", newdisp); | |
8101376d MH |
388 | return 0; |
389 | } | |
a8d11cd0 | 390 | disp = (u8 *) dest + insn_offset_displacement(insn); |
89ae465b | 391 | *(s32 *) disp = (s32) newdisp; |
1da177e4 | 392 | } |
d6be29b8 | 393 | #endif |
a8d11cd0 | 394 | return insn->length; |
31f80e45 | 395 | } |
1da177e4 | 396 | |
804dec5b | 397 | /* Prepare reljump right after instruction to boost */ |
63fef14f MH |
398 | static int prepare_boost(kprobe_opcode_t *buf, struct kprobe *p, |
399 | struct insn *insn) | |
804dec5b | 400 | { |
63fef14f MH |
401 | int len = insn->length; |
402 | ||
a8d11cd0 | 403 | if (can_boost(insn, p->addr) && |
ab09e95c | 404 | MAX_INSN_SIZE - len >= JMP32_INSN_SIZE) { |
804dec5b MH |
405 | /* |
406 | * These instructions can be executed directly if it | |
407 | * jumps back to correct address. | |
408 | */ | |
63fef14f | 409 | synthesize_reljump(buf + len, p->ainsn.insn + len, |
a8d11cd0 | 410 | p->addr + insn->length); |
ab09e95c | 411 | len += JMP32_INSN_SIZE; |
490154bc | 412 | p->ainsn.boostable = true; |
804dec5b | 413 | } else { |
490154bc | 414 | p->ainsn.boostable = false; |
804dec5b | 415 | } |
63fef14f MH |
416 | |
417 | return len; | |
418 | } | |
419 | ||
420 | /* Make page to RO mode when allocate it */ | |
421 | void *alloc_insn_page(void) | |
422 | { | |
423 | void *page; | |
424 | ||
425 | page = module_alloc(PAGE_SIZE); | |
7298e24f NA |
426 | if (!page) |
427 | return NULL; | |
428 | ||
241a1f22 | 429 | set_vm_flush_reset_perms(page); |
7298e24f NA |
430 | /* |
431 | * First make the page read-only, and only then make it executable to | |
432 | * prevent it from being W+X in between. | |
433 | */ | |
434 | set_memory_ro((unsigned long)page, 1); | |
435 | ||
436 | /* | |
437 | * TODO: Once additional kernel code protection mechanisms are set, ensure | |
438 | * that the page was not maliciously altered and it is still zeroed. | |
439 | */ | |
440 | set_memory_x((unsigned long)page, 1); | |
63fef14f MH |
441 | |
442 | return page; | |
804dec5b MH |
443 | } |
444 | ||
c93f5cf5 MH |
445 | /* Recover page to RW mode before releasing it */ |
446 | void free_insn_page(void *page) | |
447 | { | |
c93f5cf5 MH |
448 | module_memfree(page); |
449 | } | |
450 | ||
7ec8a97a | 451 | static int arch_copy_kprobe(struct kprobe *p) |
1da177e4 | 452 | { |
a8d11cd0 | 453 | struct insn insn; |
63fef14f | 454 | kprobe_opcode_t buf[MAX_INSN_SIZE]; |
804dec5b | 455 | int len; |
003002e0 | 456 | |
46484688 | 457 | /* Copy an instruction with recovering if other optprobe modifies it.*/ |
63fef14f | 458 | len = __copy_instruction(buf, p->addr, p->ainsn.insn, &insn); |
804dec5b | 459 | if (!len) |
003002e0 | 460 | return -EINVAL; |
46484688 | 461 | |
c0f7ac3a | 462 | /* |
46484688 MH |
463 | * __copy_instruction can modify the displacement of the instruction, |
464 | * but it doesn't affect boostable check. | |
c0f7ac3a | 465 | */ |
63fef14f | 466 | len = prepare_boost(buf, p, &insn); |
d0381c81 | 467 | |
9a556ab9 | 468 | /* Check whether the instruction modifies Interrupt Flag or not */ |
63fef14f | 469 | p->ainsn.if_modifier = is_IF_modifier(buf); |
9a556ab9 | 470 | |
46484688 | 471 | /* Also, displacement change doesn't affect the first byte */ |
63fef14f MH |
472 | p->opcode = buf[0]; |
473 | ||
474 | /* OK, write back the instruction(s) into ROX insn buffer */ | |
475 | text_poke(p->ainsn.insn, buf, len); | |
003002e0 MH |
476 | |
477 | return 0; | |
1da177e4 LT |
478 | } |
479 | ||
7ec8a97a | 480 | int arch_prepare_kprobe(struct kprobe *p) |
8533bbe9 | 481 | { |
38115f2f MH |
482 | int ret; |
483 | ||
4554dbcb MH |
484 | if (alternatives_text_reserved(p->addr, p->addr)) |
485 | return -EINVAL; | |
486 | ||
b46b3d70 MH |
487 | if (!can_probe((unsigned long)p->addr)) |
488 | return -EILSEQ; | |
8533bbe9 MH |
489 | /* insn: must be on special executable page on x86. */ |
490 | p->ainsn.insn = get_insn_slot(); | |
491 | if (!p->ainsn.insn) | |
492 | return -ENOMEM; | |
003002e0 | 493 | |
38115f2f MH |
494 | ret = arch_copy_kprobe(p); |
495 | if (ret) { | |
496 | free_insn_slot(p->ainsn.insn, 0); | |
497 | p->ainsn.insn = NULL; | |
498 | } | |
499 | ||
500 | return ret; | |
8533bbe9 MH |
501 | } |
502 | ||
7ec8a97a | 503 | void arch_arm_kprobe(struct kprobe *p) |
1da177e4 | 504 | { |
ab09e95c | 505 | text_poke(p->addr, ((unsigned char []){INT3_INSN_OPCODE}), 1); |
5c02ece8 | 506 | text_poke_sync(); |
1da177e4 LT |
507 | } |
508 | ||
7ec8a97a | 509 | void arch_disarm_kprobe(struct kprobe *p) |
1da177e4 | 510 | { |
19d36ccd | 511 | text_poke(p->addr, &p->opcode, 1); |
5c02ece8 | 512 | text_poke_sync(); |
7e1048b1 RL |
513 | } |
514 | ||
7ec8a97a | 515 | void arch_remove_kprobe(struct kprobe *p) |
7e1048b1 | 516 | { |
12941560 | 517 | if (p->ainsn.insn) { |
490154bc | 518 | free_insn_slot(p->ainsn.insn, p->ainsn.boostable); |
12941560 MH |
519 | p->ainsn.insn = NULL; |
520 | } | |
1da177e4 LT |
521 | } |
522 | ||
9326638c MH |
523 | static nokprobe_inline void |
524 | save_previous_kprobe(struct kprobe_ctlblk *kcb) | |
aa3d7e3d | 525 | { |
e7a510f9 AM |
526 | kcb->prev_kprobe.kp = kprobe_running(); |
527 | kcb->prev_kprobe.status = kcb->kprobe_status; | |
8533bbe9 MH |
528 | kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags; |
529 | kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags; | |
aa3d7e3d PP |
530 | } |
531 | ||
9326638c MH |
532 | static nokprobe_inline void |
533 | restore_previous_kprobe(struct kprobe_ctlblk *kcb) | |
aa3d7e3d | 534 | { |
b76834bc | 535 | __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp); |
e7a510f9 | 536 | kcb->kprobe_status = kcb->prev_kprobe.status; |
8533bbe9 MH |
537 | kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags; |
538 | kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags; | |
aa3d7e3d PP |
539 | } |
540 | ||
9326638c MH |
541 | static nokprobe_inline void |
542 | set_current_kprobe(struct kprobe *p, struct pt_regs *regs, | |
543 | struct kprobe_ctlblk *kcb) | |
aa3d7e3d | 544 | { |
b76834bc | 545 | __this_cpu_write(current_kprobe, p); |
8533bbe9 | 546 | kcb->kprobe_saved_flags = kcb->kprobe_old_flags |
053de044 | 547 | = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF)); |
9a556ab9 | 548 | if (p->ainsn.if_modifier) |
053de044 | 549 | kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF; |
aa3d7e3d PP |
550 | } |
551 | ||
9326638c | 552 | static nokprobe_inline void clear_btf(void) |
1ecc798c | 553 | { |
ea8e61b7 PZ |
554 | if (test_thread_flag(TIF_BLOCKSTEP)) { |
555 | unsigned long debugctl = get_debugctlmsr(); | |
556 | ||
557 | debugctl &= ~DEBUGCTLMSR_BTF; | |
558 | update_debugctlmsr(debugctl); | |
559 | } | |
1ecc798c RM |
560 | } |
561 | ||
9326638c | 562 | static nokprobe_inline void restore_btf(void) |
1ecc798c | 563 | { |
ea8e61b7 PZ |
564 | if (test_thread_flag(TIF_BLOCKSTEP)) { |
565 | unsigned long debugctl = get_debugctlmsr(); | |
566 | ||
567 | debugctl |= DEBUGCTLMSR_BTF; | |
568 | update_debugctlmsr(debugctl); | |
569 | } | |
1ecc798c RM |
570 | } |
571 | ||
9326638c | 572 | void arch_prepare_kretprobe(struct kretprobe_instance *ri, struct pt_regs *regs) |
73649dab | 573 | { |
8533bbe9 | 574 | unsigned long *sara = stack_addr(regs); |
ba8af12f | 575 | |
4c4308cb | 576 | ri->ret_addr = (kprobe_opcode_t *) *sara; |
3ff9c075 | 577 | ri->fp = sara; |
8533bbe9 | 578 | |
4c4308cb CH |
579 | /* Replace the return addr with trampoline addr */ |
580 | *sara = (unsigned long) &kretprobe_trampoline; | |
73649dab | 581 | } |
9326638c | 582 | NOKPROBE_SYMBOL(arch_prepare_kretprobe); |
f315decb | 583 | |
9326638c MH |
584 | static void setup_singlestep(struct kprobe *p, struct pt_regs *regs, |
585 | struct kprobe_ctlblk *kcb, int reenter) | |
f315decb | 586 | { |
c0f7ac3a MH |
587 | if (setup_detour_execution(p, regs, reenter)) |
588 | return; | |
589 | ||
48593975 | 590 | #if !defined(CONFIG_PREEMPTION) |
490154bc | 591 | if (p->ainsn.boostable && !p->post_handler) { |
f315decb | 592 | /* Boost up -- we can execute copied instructions directly */ |
0f94eb63 MH |
593 | if (!reenter) |
594 | reset_current_kprobe(); | |
595 | /* | |
596 | * Reentering boosted probe doesn't reset current_kprobe, | |
597 | * nor set current_kprobe, because it doesn't use single | |
598 | * stepping. | |
599 | */ | |
f315decb | 600 | regs->ip = (unsigned long)p->ainsn.insn; |
f315decb AS |
601 | return; |
602 | } | |
603 | #endif | |
0f94eb63 MH |
604 | if (reenter) { |
605 | save_previous_kprobe(kcb); | |
606 | set_current_kprobe(p, regs, kcb); | |
607 | kcb->kprobe_status = KPROBE_REENTER; | |
608 | } else | |
609 | kcb->kprobe_status = KPROBE_HIT_SS; | |
610 | /* Prepare real single stepping */ | |
611 | clear_btf(); | |
612 | regs->flags |= X86_EFLAGS_TF; | |
613 | regs->flags &= ~X86_EFLAGS_IF; | |
614 | /* single step inline if the instruction is an int3 */ | |
ab09e95c | 615 | if (p->opcode == INT3_INSN_OPCODE) |
0f94eb63 MH |
616 | regs->ip = (unsigned long)p->addr; |
617 | else | |
618 | regs->ip = (unsigned long)p->ainsn.insn; | |
f315decb | 619 | } |
9326638c | 620 | NOKPROBE_SYMBOL(setup_singlestep); |
f315decb | 621 | |
40102d4a HH |
622 | /* |
623 | * We have reentered the kprobe_handler(), since another probe was hit while | |
624 | * within the handler. We save the original kprobes variables and just single | |
625 | * step on the instruction of the new probe without calling any user handlers. | |
626 | */ | |
9326638c MH |
627 | static int reenter_kprobe(struct kprobe *p, struct pt_regs *regs, |
628 | struct kprobe_ctlblk *kcb) | |
40102d4a | 629 | { |
f315decb AS |
630 | switch (kcb->kprobe_status) { |
631 | case KPROBE_HIT_SSDONE: | |
f315decb | 632 | case KPROBE_HIT_ACTIVE: |
6a5022a5 | 633 | case KPROBE_HIT_SS: |
fb8830e7 | 634 | kprobes_inc_nmissed_count(p); |
0f94eb63 | 635 | setup_singlestep(p, regs, kcb, 1); |
f315decb | 636 | break; |
6a5022a5 | 637 | case KPROBE_REENTER: |
e9afe9e1 MH |
638 | /* A probe has been hit in the codepath leading up to, or just |
639 | * after, single-stepping of a probed instruction. This entire | |
640 | * codepath should strictly reside in .kprobes.text section. | |
641 | * Raise a BUG or we'll continue in an endless reentering loop | |
642 | * and eventually a stack overflow. | |
643 | */ | |
0ea06330 | 644 | pr_err("Unrecoverable kprobe detected.\n"); |
e9afe9e1 MH |
645 | dump_kprobe(p); |
646 | BUG(); | |
f315decb AS |
647 | default: |
648 | /* impossible cases */ | |
649 | WARN_ON(1); | |
fb8830e7 | 650 | return 0; |
59e87cdc | 651 | } |
f315decb | 652 | |
59e87cdc | 653 | return 1; |
40102d4a | 654 | } |
9326638c | 655 | NOKPROBE_SYMBOL(reenter_kprobe); |
73649dab | 656 | |
8533bbe9 MH |
657 | /* |
658 | * Interrupts are disabled on entry as trap3 is an interrupt gate and they | |
af901ca1 | 659 | * remain disabled throughout this function. |
8533bbe9 | 660 | */ |
9326638c | 661 | int kprobe_int3_handler(struct pt_regs *regs) |
1da177e4 | 662 | { |
8533bbe9 | 663 | kprobe_opcode_t *addr; |
f315decb | 664 | struct kprobe *p; |
d217d545 AM |
665 | struct kprobe_ctlblk *kcb; |
666 | ||
f39b6f0e | 667 | if (user_mode(regs)) |
0cdd192c AL |
668 | return 0; |
669 | ||
8533bbe9 | 670 | addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t)); |
d217d545 | 671 | /* |
2bbda764 MH |
672 | * We don't want to be preempted for the entire duration of kprobe |
673 | * processing. Since int3 and debug trap disables irqs and we clear | |
674 | * IF while singlestepping, it must be no preemptible. | |
d217d545 | 675 | */ |
1da177e4 | 676 | |
f315decb | 677 | kcb = get_kprobe_ctlblk(); |
b9760156 | 678 | p = get_kprobe(addr); |
f315decb | 679 | |
b9760156 | 680 | if (p) { |
b9760156 | 681 | if (kprobe_running()) { |
f315decb AS |
682 | if (reenter_kprobe(p, regs, kcb)) |
683 | return 1; | |
1da177e4 | 684 | } else { |
b9760156 HH |
685 | set_current_kprobe(p, regs, kcb); |
686 | kcb->kprobe_status = KPROBE_HIT_ACTIVE; | |
f315decb | 687 | |
1da177e4 | 688 | /* |
f315decb AS |
689 | * If we have no pre-handler or it returned 0, we |
690 | * continue with normal processing. If we have a | |
80006dbe MH |
691 | * pre-handler and it returned non-zero, that means |
692 | * user handler setup registers to exit to another | |
693 | * instruction, we must skip the single stepping. | |
1da177e4 | 694 | */ |
f315decb | 695 | if (!p->pre_handler || !p->pre_handler(p, regs)) |
0f94eb63 | 696 | setup_singlestep(p, regs, kcb, 0); |
2bbda764 | 697 | else |
cce188bd | 698 | reset_current_kprobe(); |
f315decb | 699 | return 1; |
b9760156 | 700 | } |
ab09e95c | 701 | } else if (*addr != INT3_INSN_OPCODE) { |
829e9245 MH |
702 | /* |
703 | * The breakpoint instruction was removed right | |
704 | * after we hit it. Another cpu has removed | |
705 | * either a probepoint or a debugger breakpoint | |
706 | * at this address. In either case, no further | |
707 | * handling of this interrupt is appropriate. | |
708 | * Back up over the (now missing) int3 and run | |
709 | * the original instruction. | |
710 | */ | |
711 | regs->ip = (unsigned long)addr; | |
829e9245 | 712 | return 1; |
f315decb | 713 | } /* else: not a kprobe fault; let the kernel handle it */ |
1da177e4 | 714 | |
f315decb | 715 | return 0; |
1da177e4 | 716 | } |
9326638c | 717 | NOKPROBE_SYMBOL(kprobe_int3_handler); |
1da177e4 | 718 | |
73649dab | 719 | /* |
da07ab03 MH |
720 | * When a retprobed function returns, this code saves registers and |
721 | * calls trampoline_handler() runs, which calls the kretprobe's handler. | |
73649dab | 722 | */ |
c1c355ce | 723 | asm( |
c03e2750 | 724 | ".text\n" |
c1c355ce JP |
725 | ".global kretprobe_trampoline\n" |
726 | ".type kretprobe_trampoline, @function\n" | |
727 | "kretprobe_trampoline:\n" | |
c1c355ce | 728 | /* We don't bother saving the ss register */ |
3c88c692 | 729 | #ifdef CONFIG_X86_64 |
c1c355ce JP |
730 | " pushq %rsp\n" |
731 | " pushfq\n" | |
732 | SAVE_REGS_STRING | |
733 | " movq %rsp, %rdi\n" | |
734 | " call trampoline_handler\n" | |
735 | /* Replace saved sp with true return address. */ | |
3c88c692 | 736 | " movq %rax, 19*8(%rsp)\n" |
c1c355ce JP |
737 | RESTORE_REGS_STRING |
738 | " popfq\n" | |
d6be29b8 | 739 | #else |
3c88c692 PZ |
740 | " pushl %esp\n" |
741 | " pushfl\n" | |
c1c355ce JP |
742 | SAVE_REGS_STRING |
743 | " movl %esp, %eax\n" | |
744 | " call trampoline_handler\n" | |
3c88c692 PZ |
745 | /* Replace saved sp with true return address. */ |
746 | " movl %eax, 15*4(%esp)\n" | |
c1c355ce | 747 | RESTORE_REGS_STRING |
3c88c692 | 748 | " popfl\n" |
d6be29b8 | 749 | #endif |
c1c355ce JP |
750 | " ret\n" |
751 | ".size kretprobe_trampoline, .-kretprobe_trampoline\n" | |
752 | ); | |
9326638c | 753 | NOKPROBE_SYMBOL(kretprobe_trampoline); |
87aaff2a | 754 | STACK_FRAME_NON_STANDARD(kretprobe_trampoline); |
73649dab | 755 | |
b191fa96 MH |
756 | static struct kprobe kretprobe_kprobe = { |
757 | .addr = (void *)kretprobe_trampoline, | |
758 | }; | |
759 | ||
73649dab | 760 | /* |
da07ab03 | 761 | * Called from kretprobe_trampoline |
73649dab | 762 | */ |
0e72499c | 763 | __used __visible void *trampoline_handler(struct pt_regs *regs) |
73649dab | 764 | { |
b191fa96 | 765 | struct kprobe_ctlblk *kcb; |
62c27be0 | 766 | struct kretprobe_instance *ri = NULL; |
99219a3f | 767 | struct hlist_head *head, empty_rp; |
b67bfe0d | 768 | struct hlist_node *tmp; |
991a51d8 | 769 | unsigned long flags, orig_ret_address = 0; |
d6be29b8 | 770 | unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline; |
737480a0 | 771 | kprobe_opcode_t *correct_ret_addr = NULL; |
3ff9c075 MH |
772 | void *frame_pointer; |
773 | bool skipped = false; | |
73649dab | 774 | |
b191fa96 MH |
775 | preempt_disable(); |
776 | ||
777 | /* | |
778 | * Set a dummy kprobe for avoiding kretprobe recursion. | |
779 | * Since kretprobe never run in kprobe handler, kprobe must not | |
780 | * be running at this point. | |
781 | */ | |
782 | kcb = get_kprobe_ctlblk(); | |
783 | __this_cpu_write(current_kprobe, &kretprobe_kprobe); | |
784 | kcb->kprobe_status = KPROBE_HIT_ACTIVE; | |
785 | ||
99219a3f | 786 | INIT_HLIST_HEAD(&empty_rp); |
ef53d9c5 | 787 | kretprobe_hash_lock(current, &head, &flags); |
8533bbe9 | 788 | /* fixup registers */ |
da07ab03 | 789 | regs->cs = __KERNEL_CS; |
3c88c692 PZ |
790 | #ifdef CONFIG_X86_32 |
791 | regs->cs |= get_kernel_rpl(); | |
fee039a1 | 792 | regs->gs = 0; |
d6be29b8 | 793 | #endif |
3c88c692 PZ |
794 | /* We use pt_regs->sp for return address holder. */ |
795 | frame_pointer = ®s->sp; | |
da07ab03 | 796 | regs->ip = trampoline_address; |
8533bbe9 | 797 | regs->orig_ax = ~0UL; |
73649dab | 798 | |
ba8af12f RL |
799 | /* |
800 | * It is possible to have multiple instances associated with a given | |
8533bbe9 | 801 | * task either because multiple functions in the call path have |
025dfdaf | 802 | * return probes installed on them, and/or more than one |
ba8af12f RL |
803 | * return probe was registered for a target function. |
804 | * | |
805 | * We can handle this because: | |
8533bbe9 | 806 | * - instances are always pushed into the head of the list |
ba8af12f | 807 | * - when multiple return probes are registered for the same |
8533bbe9 MH |
808 | * function, the (chronologically) first instance's ret_addr |
809 | * will be the real return address, and all the rest will | |
810 | * point to kretprobe_trampoline. | |
ba8af12f | 811 | */ |
b6263178 | 812 | hlist_for_each_entry(ri, head, hlist) { |
62c27be0 | 813 | if (ri->task != current) |
ba8af12f | 814 | /* another task is sharing our hash bucket */ |
62c27be0 | 815 | continue; |
3ff9c075 MH |
816 | /* |
817 | * Return probes must be pushed on this hash list correct | |
462e5a52 | 818 | * order (same as return order) so that it can be popped |
3ff9c075 MH |
819 | * correctly. However, if we find it is pushed it incorrect |
820 | * order, this means we find a function which should not be | |
821 | * probed, because the wrong order entry is pushed on the | |
822 | * path of processing other kretprobe itself. | |
823 | */ | |
824 | if (ri->fp != frame_pointer) { | |
825 | if (!skipped) | |
826 | pr_warn("kretprobe is stacked incorrectly. Trying to fixup.\n"); | |
827 | skipped = true; | |
828 | continue; | |
829 | } | |
ba8af12f | 830 | |
737480a0 | 831 | orig_ret_address = (unsigned long)ri->ret_addr; |
3ff9c075 MH |
832 | if (skipped) |
833 | pr_warn("%ps must be blacklisted because of incorrect kretprobe order\n", | |
834 | ri->rp->kp.addr); | |
737480a0 KS |
835 | |
836 | if (orig_ret_address != trampoline_address) | |
837 | /* | |
838 | * This is the real return address. Any other | |
839 | * instances associated with this task are for | |
840 | * other calls deeper on the call stack | |
841 | */ | |
842 | break; | |
843 | } | |
844 | ||
845 | kretprobe_assert(ri, orig_ret_address, trampoline_address); | |
846 | ||
847 | correct_ret_addr = ri->ret_addr; | |
b67bfe0d | 848 | hlist_for_each_entry_safe(ri, tmp, head, hlist) { |
737480a0 KS |
849 | if (ri->task != current) |
850 | /* another task is sharing our hash bucket */ | |
851 | continue; | |
3ff9c075 MH |
852 | if (ri->fp != frame_pointer) |
853 | continue; | |
737480a0 KS |
854 | |
855 | orig_ret_address = (unsigned long)ri->ret_addr; | |
da07ab03 | 856 | if (ri->rp && ri->rp->handler) { |
b76834bc | 857 | __this_cpu_write(current_kprobe, &ri->rp->kp); |
737480a0 | 858 | ri->ret_addr = correct_ret_addr; |
ba8af12f | 859 | ri->rp->handler(ri, regs); |
b191fa96 | 860 | __this_cpu_write(current_kprobe, &kretprobe_kprobe); |
da07ab03 | 861 | } |
ba8af12f | 862 | |
99219a3f | 863 | recycle_rp_inst(ri, &empty_rp); |
ba8af12f RL |
864 | |
865 | if (orig_ret_address != trampoline_address) | |
866 | /* | |
867 | * This is the real return address. Any other | |
868 | * instances associated with this task are for | |
869 | * other calls deeper on the call stack | |
870 | */ | |
871 | break; | |
73649dab | 872 | } |
ba8af12f | 873 | |
ef53d9c5 | 874 | kretprobe_hash_unlock(current, &flags); |
ba8af12f | 875 | |
b191fa96 MH |
876 | __this_cpu_write(current_kprobe, NULL); |
877 | preempt_enable(); | |
878 | ||
b67bfe0d | 879 | hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) { |
99219a3f | 880 | hlist_del(&ri->hlist); |
881 | kfree(ri); | |
882 | } | |
da07ab03 | 883 | return (void *)orig_ret_address; |
73649dab | 884 | } |
9326638c | 885 | NOKPROBE_SYMBOL(trampoline_handler); |
73649dab | 886 | |
1da177e4 LT |
887 | /* |
888 | * Called after single-stepping. p->addr is the address of the | |
889 | * instruction whose first byte has been replaced by the "int 3" | |
890 | * instruction. To avoid the SMP problems that can occur when we | |
891 | * temporarily put back the original opcode to single-step, we | |
892 | * single-stepped a copy of the instruction. The address of this | |
893 | * copy is p->ainsn.insn. | |
894 | * | |
895 | * This function prepares to return from the post-single-step | |
896 | * interrupt. We have to fix up the stack as follows: | |
897 | * | |
898 | * 0) Except in the case of absolute or indirect jump or call instructions, | |
65ea5b03 | 899 | * the new ip is relative to the copied instruction. We need to make |
1da177e4 LT |
900 | * it relative to the original instruction. |
901 | * | |
902 | * 1) If the single-stepped instruction was pushfl, then the TF and IF | |
65ea5b03 | 903 | * flags are set in the just-pushed flags, and may need to be cleared. |
1da177e4 LT |
904 | * |
905 | * 2) If the single-stepped instruction was a call, the return address | |
906 | * that is atop the stack is the address following the copied instruction. | |
907 | * We need to make it the address following the original instruction. | |
aa470140 MH |
908 | * |
909 | * If this is the first time we've single-stepped the instruction at | |
910 | * this probepoint, and the instruction is boostable, boost it: add a | |
911 | * jump instruction after the copied instruction, that jumps to the next | |
912 | * instruction after the probepoint. | |
1da177e4 | 913 | */ |
9326638c MH |
914 | static void resume_execution(struct kprobe *p, struct pt_regs *regs, |
915 | struct kprobe_ctlblk *kcb) | |
1da177e4 | 916 | { |
8533bbe9 MH |
917 | unsigned long *tos = stack_addr(regs); |
918 | unsigned long copy_ip = (unsigned long)p->ainsn.insn; | |
919 | unsigned long orig_ip = (unsigned long)p->addr; | |
1da177e4 LT |
920 | kprobe_opcode_t *insn = p->ainsn.insn; |
921 | ||
567a9fd8 MH |
922 | /* Skip prefixes */ |
923 | insn = skip_prefixes(insn); | |
1da177e4 | 924 | |
053de044 | 925 | regs->flags &= ~X86_EFLAGS_TF; |
1da177e4 | 926 | switch (*insn) { |
0b0122fa | 927 | case 0x9c: /* pushfl */ |
053de044 | 928 | *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF); |
8533bbe9 | 929 | *tos |= kcb->kprobe_old_flags; |
1da177e4 | 930 | break; |
0b0122fa MH |
931 | case 0xc2: /* iret/ret/lret */ |
932 | case 0xc3: | |
0b9e2cac | 933 | case 0xca: |
0b0122fa MH |
934 | case 0xcb: |
935 | case 0xcf: | |
936 | case 0xea: /* jmp absolute -- ip is correct */ | |
937 | /* ip is already adjusted, no more changes required */ | |
490154bc | 938 | p->ainsn.boostable = true; |
0b0122fa MH |
939 | goto no_change; |
940 | case 0xe8: /* call relative - Fix return addr */ | |
8533bbe9 | 941 | *tos = orig_ip + (*tos - copy_ip); |
1da177e4 | 942 | break; |
e7b5e11e | 943 | #ifdef CONFIG_X86_32 |
d6be29b8 MH |
944 | case 0x9a: /* call absolute -- same as call absolute, indirect */ |
945 | *tos = orig_ip + (*tos - copy_ip); | |
946 | goto no_change; | |
947 | #endif | |
1da177e4 | 948 | case 0xff: |
dc49e344 | 949 | if ((insn[1] & 0x30) == 0x10) { |
8533bbe9 MH |
950 | /* |
951 | * call absolute, indirect | |
952 | * Fix return addr; ip is correct. | |
953 | * But this is not boostable | |
954 | */ | |
955 | *tos = orig_ip + (*tos - copy_ip); | |
0b0122fa | 956 | goto no_change; |
8533bbe9 MH |
957 | } else if (((insn[1] & 0x31) == 0x20) || |
958 | ((insn[1] & 0x31) == 0x21)) { | |
959 | /* | |
960 | * jmp near and far, absolute indirect | |
961 | * ip is correct. And this is boostable | |
962 | */ | |
490154bc | 963 | p->ainsn.boostable = true; |
0b0122fa | 964 | goto no_change; |
1da177e4 | 965 | } |
1da177e4 LT |
966 | default: |
967 | break; | |
968 | } | |
969 | ||
8533bbe9 | 970 | regs->ip += orig_ip - copy_ip; |
65ea5b03 | 971 | |
0b0122fa | 972 | no_change: |
1ecc798c | 973 | restore_btf(); |
1da177e4 | 974 | } |
9326638c | 975 | NOKPROBE_SYMBOL(resume_execution); |
1da177e4 | 976 | |
8533bbe9 MH |
977 | /* |
978 | * Interrupts are disabled on entry as trap1 is an interrupt gate and they | |
af901ca1 | 979 | * remain disabled throughout this function. |
8533bbe9 | 980 | */ |
9326638c | 981 | int kprobe_debug_handler(struct pt_regs *regs) |
1da177e4 | 982 | { |
e7a510f9 AM |
983 | struct kprobe *cur = kprobe_running(); |
984 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); | |
985 | ||
986 | if (!cur) | |
1da177e4 LT |
987 | return 0; |
988 | ||
acb5b8a2 YL |
989 | resume_execution(cur, regs, kcb); |
990 | regs->flags |= kcb->kprobe_saved_flags; | |
acb5b8a2 | 991 | |
e7a510f9 AM |
992 | if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) { |
993 | kcb->kprobe_status = KPROBE_HIT_SSDONE; | |
994 | cur->post_handler(cur, regs, 0); | |
aa3d7e3d | 995 | } |
1da177e4 | 996 | |
8533bbe9 | 997 | /* Restore back the original saved kprobes variables and continue. */ |
e7a510f9 AM |
998 | if (kcb->kprobe_status == KPROBE_REENTER) { |
999 | restore_previous_kprobe(kcb); | |
aa3d7e3d | 1000 | goto out; |
aa3d7e3d | 1001 | } |
e7a510f9 | 1002 | reset_current_kprobe(); |
aa3d7e3d | 1003 | out: |
1da177e4 | 1004 | /* |
65ea5b03 | 1005 | * if somebody else is singlestepping across a probe point, flags |
1da177e4 LT |
1006 | * will have TF set, in which case, continue the remaining processing |
1007 | * of do_debug, as if this is not a probe hit. | |
1008 | */ | |
053de044 | 1009 | if (regs->flags & X86_EFLAGS_TF) |
1da177e4 LT |
1010 | return 0; |
1011 | ||
1012 | return 1; | |
1013 | } | |
9326638c | 1014 | NOKPROBE_SYMBOL(kprobe_debug_handler); |
1da177e4 | 1015 | |
9326638c | 1016 | int kprobe_fault_handler(struct pt_regs *regs, int trapnr) |
1da177e4 | 1017 | { |
e7a510f9 AM |
1018 | struct kprobe *cur = kprobe_running(); |
1019 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); | |
1020 | ||
6381c24c MH |
1021 | if (unlikely(regs->ip == (unsigned long)cur->ainsn.insn)) { |
1022 | /* This must happen on single-stepping */ | |
1023 | WARN_ON(kcb->kprobe_status != KPROBE_HIT_SS && | |
1024 | kcb->kprobe_status != KPROBE_REENTER); | |
c28f8966 PP |
1025 | /* |
1026 | * We are here because the instruction being single | |
1027 | * stepped caused a page fault. We reset the current | |
65ea5b03 | 1028 | * kprobe and the ip points back to the probe address |
c28f8966 PP |
1029 | * and allow the page fault handler to continue as a |
1030 | * normal page fault. | |
1031 | */ | |
65ea5b03 | 1032 | regs->ip = (unsigned long)cur->addr; |
dcfc4724 MH |
1033 | /* |
1034 | * Trap flag (TF) has been set here because this fault | |
1035 | * happened where the single stepping will be done. | |
1036 | * So clear it by resetting the current kprobe: | |
1037 | */ | |
1038 | regs->flags &= ~X86_EFLAGS_TF; | |
1039 | ||
1040 | /* | |
1041 | * If the TF flag was set before the kprobe hit, | |
1042 | * don't touch it: | |
1043 | */ | |
8533bbe9 | 1044 | regs->flags |= kcb->kprobe_old_flags; |
dcfc4724 | 1045 | |
c28f8966 PP |
1046 | if (kcb->kprobe_status == KPROBE_REENTER) |
1047 | restore_previous_kprobe(kcb); | |
1048 | else | |
1049 | reset_current_kprobe(); | |
6381c24c MH |
1050 | } else if (kcb->kprobe_status == KPROBE_HIT_ACTIVE || |
1051 | kcb->kprobe_status == KPROBE_HIT_SSDONE) { | |
c28f8966 PP |
1052 | /* |
1053 | * We increment the nmissed count for accounting, | |
8533bbe9 | 1054 | * we can also use npre/npostfault count for accounting |
c28f8966 PP |
1055 | * these specific fault cases. |
1056 | */ | |
1057 | kprobes_inc_nmissed_count(cur); | |
1058 | ||
1059 | /* | |
1060 | * We come here because instructions in the pre/post | |
1061 | * handler caused the page_fault, this could happen | |
1062 | * if handler tries to access user space by | |
1063 | * copy_from_user(), get_user() etc. Let the | |
1064 | * user-specified handler try to fix it first. | |
1065 | */ | |
1066 | if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr)) | |
1067 | return 1; | |
1da177e4 | 1068 | } |
6381c24c | 1069 | |
1da177e4 LT |
1070 | return 0; |
1071 | } | |
9326638c | 1072 | NOKPROBE_SYMBOL(kprobe_fault_handler); |
1da177e4 | 1073 | |
fe6e6561 MH |
1074 | int __init arch_populate_kprobe_blacklist(void) |
1075 | { | |
0eae81dc MH |
1076 | int ret; |
1077 | ||
1078 | ret = kprobe_add_area_blacklist((unsigned long)__irqentry_text_start, | |
1079 | (unsigned long)__irqentry_text_end); | |
1080 | if (ret) | |
1081 | return ret; | |
1082 | ||
fe6e6561 MH |
1083 | return kprobe_add_area_blacklist((unsigned long)__entry_text_start, |
1084 | (unsigned long)__entry_text_end); | |
1085 | } | |
1086 | ||
6772926b | 1087 | int __init arch_init_kprobes(void) |
ba8af12f | 1088 | { |
a7b0133e | 1089 | return 0; |
ba8af12f | 1090 | } |
bf8f6e5b | 1091 | |
7ec8a97a | 1092 | int arch_trampoline_kprobe(struct kprobe *p) |
bf8f6e5b | 1093 | { |
bf8f6e5b AM |
1094 | return 0; |
1095 | } |