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