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