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