]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/x86/kernel/kprobes_64.c
x86: return probe-booster for x86-64
[mirror_ubuntu-artful-kernel.git] / arch / x86 / kernel / kprobes_64.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.
25 * 2004-Oct Jim Keniston <kenistoj@us.ibm.com> and Prasanna S Panchamukhi
26 * <prasanna@in.ibm.com> adapted for x86_64
27 * 2005-Mar Roland McGrath <roland@redhat.com>
28 * Fixed to handle %rip-relative addressing mode correctly.
73649dab
RL
29 * 2005-May Rusty Lynch <rusty.lynch@intel.com>
30 * Added function return probes functionality
da07ab03
MH
31 * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com> added kprobe-booster
32 * and kretprobe-booster for x86-64
1da177e4
LT
33 */
34
1da177e4
LT
35#include <linux/kprobes.h>
36#include <linux/ptrace.h>
1da177e4
LT
37#include <linux/string.h>
38#include <linux/slab.h>
39#include <linux/preempt.h>
c28f8966 40#include <linux/module.h>
1eeb66a1 41#include <linux/kdebug.h>
9ec4b1f3 42
1da177e4 43#include <asm/pgtable.h>
c28f8966 44#include <asm/uaccess.h>
19d36ccd 45#include <asm/alternative.h>
1da177e4 46
1da177e4 47void jprobe_return_end(void);
f709b122 48static void __kprobes arch_copy_kprobe(struct kprobe *p);
1da177e4 49
e7a510f9
AM
50DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
51DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
1da177e4 52
f438d914
MH
53struct kretprobe_blackpoint kretprobe_blacklist[] = {
54 {"__switch_to", }, /* This function switches only current task, but
55 doesn't switch kernel stack.*/
56 {NULL, NULL} /* Terminator */
57};
58const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist);
59
aa470140
MH
60/* Insert a jump instruction at address 'from', which jumps to address 'to'.*/
61static __always_inline void set_jmp_op(void *from, void *to)
62{
63 struct __arch_jmp_op {
64 char op;
65 s32 raddr;
66 } __attribute__((packed)) * jop;
67 jop = (struct __arch_jmp_op *)from;
68 jop->raddr = (s32)((long)(to) - ((long)(from) + 5));
69 jop->op = RELATIVEJUMP_INSTRUCTION;
70}
71
72/*
73 * returns non-zero if opcode is boostable
74 * RIP relative instructions are adjusted at copying time
75 */
76static __always_inline int can_boost(kprobe_opcode_t *opcodes)
77{
78#define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\
79 (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
80 (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
81 (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
82 (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
83 << (row % 64))
84 /*
85 * Undefined/reserved opcodes, conditional jump, Opcode Extension
86 * Groups, and some special opcodes can not boost.
87 */
88 static const unsigned long twobyte_is_boostable[256 / 64] = {
89 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
90 /* ---------------------------------------------- */
91 W(0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0)|/* 00 */
92 W(0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)|/* 10 */
93 W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)|/* 20 */
94 W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),/* 30 */
95 W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)|/* 40 */
96 W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)|/* 50 */
97 W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1)|/* 60 */
98 W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1),/* 70 */
99 W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)|/* 80 */
100 W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)|/* 90 */
101 W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1)|/* a0 */
102 W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1),/* b0 */
103 W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1)|/* c0 */
104 W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1)|/* d0 */
105 W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1)|/* e0 */
106 W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0) /* f0 */
107 /* ----------------------------------------------- */
108 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
109 };
110#undef W
111 kprobe_opcode_t opcode;
112 kprobe_opcode_t *orig_opcodes = opcodes;
113
114retry:
115 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
116 return 0;
117 opcode = *(opcodes++);
118
119 /* 2nd-byte opcode */
120 if (opcode == 0x0f) {
121 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
122 return 0;
123 return test_bit(*opcodes, twobyte_is_boostable);
124 }
125
126 switch (opcode & 0xf0) {
127 case 0x40:
128 goto retry; /* REX prefix is boostable */
129 case 0x60:
130 if (0x63 < opcode && opcode < 0x67)
131 goto retry; /* prefixes */
132 /* can't boost Address-size override and bound */
133 return (opcode != 0x62 && opcode != 0x67);
134 case 0x70:
135 return 0; /* can't boost conditional jump */
136 case 0xc0:
137 /* can't boost software-interruptions */
138 return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
139 case 0xd0:
140 /* can boost AA* and XLAT */
141 return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
142 case 0xe0:
143 /* can boost in/out and absolute jmps */
144 return ((opcode & 0x04) || opcode == 0xea);
145 case 0xf0:
146 if ((opcode & 0x0c) == 0 && opcode != 0xf1)
147 goto retry; /* lock/rep(ne) prefix */
148 /* clear and set flags are boostable */
149 return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
150 default:
151 /* segment override prefixes are boostable */
152 if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e)
153 goto retry; /* prefixes */
154 /* CS override prefix and call are not boostable */
155 return (opcode != 0x2e && opcode != 0x9a);
156 }
157}
158
1da177e4
LT
159/*
160 * returns non-zero if opcode modifies the interrupt flag.
161 */
8645419c 162static int __kprobes is_IF_modifier(kprobe_opcode_t *insn)
1da177e4
LT
163{
164 switch (*insn) {
165 case 0xfa: /* cli */
166 case 0xfb: /* sti */
167 case 0xcf: /* iret/iretd */
168 case 0x9d: /* popf/popfd */
169 return 1;
170 }
171
172 if (*insn >= 0x40 && *insn <= 0x4f && *++insn == 0xcf)
173 return 1;
174 return 0;
175}
176
0f2fbdcb 177int __kprobes arch_prepare_kprobe(struct kprobe *p)
1da177e4
LT
178{
179 /* insn: must be on special executable page on x86_64. */
2dd960d6 180 p->ainsn.insn = get_insn_slot();
1da177e4
LT
181 if (!p->ainsn.insn) {
182 return -ENOMEM;
183 }
49a2a1b8 184 arch_copy_kprobe(p);
1da177e4
LT
185 return 0;
186}
187
188/*
189 * Determine if the instruction uses the %rip-relative addressing mode.
aa470140 190 * If it does, Return the address of the 32-bit displacement word.
1da177e4
LT
191 * If not, return null.
192 */
3b60211c 193static s32 __kprobes *is_riprel(u8 *insn)
1da177e4
LT
194{
195#define W(row,b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,ba,bb,bc,bd,be,bf) \
196 (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
197 (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
198 (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
199 (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
200 << (row % 64))
201 static const u64 onebyte_has_modrm[256 / 64] = {
202 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
203 /* ------------------------------- */
204 W(0x00, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0)| /* 00 */
205 W(0x10, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0)| /* 10 */
206 W(0x20, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0)| /* 20 */
207 W(0x30, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0), /* 30 */
208 W(0x40, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 40 */
209 W(0x50, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 50 */
210 W(0x60, 0,0,1,1,0,0,0,0,0,1,0,1,0,0,0,0)| /* 60 */
211 W(0x70, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* 70 */
212 W(0x80, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 80 */
213 W(0x90, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 90 */
214 W(0xa0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* a0 */
215 W(0xb0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* b0 */
216 W(0xc0, 1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0)| /* c0 */
217 W(0xd0, 1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1)| /* d0 */
218 W(0xe0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* e0 */
219 W(0xf0, 0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1) /* f0 */
220 /* ------------------------------- */
221 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
222 };
223 static const u64 twobyte_has_modrm[256 / 64] = {
224 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
225 /* ------------------------------- */
226 W(0x00, 1,1,1,1,0,0,0,0,0,0,0,0,0,1,0,1)| /* 0f */
227 W(0x10, 1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0)| /* 1f */
228 W(0x20, 1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1)| /* 2f */
229 W(0x30, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* 3f */
230 W(0x40, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 4f */
231 W(0x50, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 5f */
232 W(0x60, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 6f */
233 W(0x70, 1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1), /* 7f */
234 W(0x80, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 8f */
235 W(0x90, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 9f */
236 W(0xa0, 0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1)| /* af */
237 W(0xb0, 1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1), /* bf */
238 W(0xc0, 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0)| /* cf */
239 W(0xd0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* df */
240 W(0xe0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* ef */
241 W(0xf0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0) /* ff */
242 /* ------------------------------- */
243 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
244 };
245#undef W
246 int need_modrm;
247
248 /* Skip legacy instruction prefixes. */
249 while (1) {
250 switch (*insn) {
251 case 0x66:
252 case 0x67:
253 case 0x2e:
254 case 0x3e:
255 case 0x26:
256 case 0x64:
257 case 0x65:
258 case 0x36:
259 case 0xf0:
260 case 0xf3:
261 case 0xf2:
262 ++insn;
263 continue;
264 }
265 break;
266 }
267
268 /* Skip REX instruction prefix. */
269 if ((*insn & 0xf0) == 0x40)
270 ++insn;
271
272 if (*insn == 0x0f) { /* Two-byte opcode. */
273 ++insn;
274 need_modrm = test_bit(*insn, twobyte_has_modrm);
275 } else { /* One-byte opcode. */
276 need_modrm = test_bit(*insn, onebyte_has_modrm);
277 }
278
279 if (need_modrm) {
280 u8 modrm = *++insn;
281 if ((modrm & 0xc7) == 0x05) { /* %rip+disp32 addressing mode */
282 /* Displacement follows ModRM byte. */
283 return (s32 *) ++insn;
284 }
285 }
286
287 /* No %rip-relative addressing mode here. */
288 return NULL;
289}
290
f709b122 291static void __kprobes arch_copy_kprobe(struct kprobe *p)
1da177e4
LT
292{
293 s32 *ripdisp;
294 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE);
295 ripdisp = is_riprel(p->ainsn.insn);
296 if (ripdisp) {
297 /*
298 * The copied instruction uses the %rip-relative
299 * addressing mode. Adjust the displacement for the
300 * difference between the original location of this
301 * instruction and the location of the copy that will
302 * actually be run. The tricky bit here is making sure
303 * that the sign extension happens correctly in this
304 * calculation, since we need a signed 32-bit result to
305 * be sign-extended to 64 bits when it's added to the
306 * %rip value and yield the same 64-bit result that the
307 * sign-extension of the original signed 32-bit
308 * displacement would have given.
309 */
310 s64 disp = (u8 *) p->addr + *ripdisp - (u8 *) p->ainsn.insn;
311 BUG_ON((s64) (s32) disp != disp); /* Sanity check. */
312 *ripdisp = disp;
313 }
aa470140
MH
314 if (can_boost(p->addr)) {
315 p->ainsn.boostable = 0;
316 } else {
317 p->ainsn.boostable = -1;
318 }
7e1048b1 319 p->opcode = *p->addr;
1da177e4
LT
320}
321
0f2fbdcb 322void __kprobes arch_arm_kprobe(struct kprobe *p)
1da177e4 323{
19d36ccd 324 text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1);
1da177e4
LT
325}
326
0f2fbdcb 327void __kprobes arch_disarm_kprobe(struct kprobe *p)
1da177e4 328{
19d36ccd 329 text_poke(p->addr, &p->opcode, 1);
7e1048b1
RL
330}
331
0498b635 332void __kprobes arch_remove_kprobe(struct kprobe *p)
7e1048b1 333{
7a7d1cf9 334 mutex_lock(&kprobe_mutex);
aa470140 335 free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1));
7a7d1cf9 336 mutex_unlock(&kprobe_mutex);
1da177e4
LT
337}
338
3b60211c 339static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
aa3d7e3d 340{
e7a510f9
AM
341 kcb->prev_kprobe.kp = kprobe_running();
342 kcb->prev_kprobe.status = kcb->kprobe_status;
343 kcb->prev_kprobe.old_rflags = kcb->kprobe_old_rflags;
344 kcb->prev_kprobe.saved_rflags = kcb->kprobe_saved_rflags;
aa3d7e3d
PP
345}
346
3b60211c 347static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
aa3d7e3d 348{
e7a510f9
AM
349 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
350 kcb->kprobe_status = kcb->prev_kprobe.status;
351 kcb->kprobe_old_rflags = kcb->prev_kprobe.old_rflags;
352 kcb->kprobe_saved_rflags = kcb->prev_kprobe.saved_rflags;
aa3d7e3d
PP
353}
354
3b60211c 355static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
e7a510f9 356 struct kprobe_ctlblk *kcb)
aa3d7e3d 357{
e7a510f9
AM
358 __get_cpu_var(current_kprobe) = p;
359 kcb->kprobe_saved_rflags = kcb->kprobe_old_rflags
65ea5b03 360 = (regs->flags & (TF_MASK | IF_MASK));
aa3d7e3d 361 if (is_IF_modifier(p->ainsn.insn))
e7a510f9 362 kcb->kprobe_saved_rflags &= ~IF_MASK;
aa3d7e3d
PP
363}
364
1ecc798c
RM
365static __always_inline void clear_btf(void)
366{
367 if (test_thread_flag(TIF_DEBUGCTLMSR))
368 wrmsrl(MSR_IA32_DEBUGCTLMSR, 0);
369}
370
371static __always_inline void restore_btf(void)
372{
373 if (test_thread_flag(TIF_DEBUGCTLMSR))
374 wrmsrl(MSR_IA32_DEBUGCTLMSR, current->thread.debugctlmsr);
375}
376
0f2fbdcb 377static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
1da177e4 378{
1ecc798c 379 clear_btf();
65ea5b03
PA
380 regs->flags |= TF_MASK;
381 regs->flags &= ~IF_MASK;
1da177e4
LT
382 /*single step inline if the instruction is an int3*/
383 if (p->opcode == BREAKPOINT_INSTRUCTION)
65ea5b03 384 regs->ip = (unsigned long)p->addr;
1da177e4 385 else
65ea5b03 386 regs->ip = (unsigned long)p->ainsn.insn;
1da177e4
LT
387}
388
991a51d8 389/* Called with kretprobe_lock held */
4c4308cb 390void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
0f2fbdcb 391 struct pt_regs *regs)
73649dab 392{
65ea5b03 393 unsigned long *sara = (unsigned long *)regs->sp;
ba8af12f 394
4c4308cb
CH
395 ri->ret_addr = (kprobe_opcode_t *) *sara;
396 /* Replace the return addr with trampoline addr */
397 *sara = (unsigned long) &kretprobe_trampoline;
73649dab
RL
398}
399
0f2fbdcb 400int __kprobes kprobe_handler(struct pt_regs *regs)
1da177e4
LT
401{
402 struct kprobe *p;
403 int ret = 0;
65ea5b03 404 kprobe_opcode_t *addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
d217d545
AM
405 struct kprobe_ctlblk *kcb;
406
407 /*
408 * We don't want to be preempted for the entire
409 * duration of kprobe processing
410 */
411 preempt_disable();
412 kcb = get_kprobe_ctlblk();
1da177e4 413
1da177e4
LT
414 /* Check we're not actually recursing */
415 if (kprobe_running()) {
1da177e4
LT
416 p = get_kprobe(addr);
417 if (p) {
e7a510f9 418 if (kcb->kprobe_status == KPROBE_HIT_SS &&
deac66ae 419 *p->ainsn.insn == BREAKPOINT_INSTRUCTION) {
65ea5b03
PA
420 regs->flags &= ~TF_MASK;
421 regs->flags |= kcb->kprobe_saved_rflags;
1da177e4 422 goto no_kprobe;
e7a510f9 423 } else if (kcb->kprobe_status == KPROBE_HIT_SSDONE) {
aa3d7e3d
PP
424 /* TODO: Provide re-entrancy from
425 * post_kprobes_handler() and avoid exception
426 * stack corruption while single-stepping on
427 * the instruction of the new probe.
428 */
429 arch_disarm_kprobe(p);
65ea5b03 430 regs->ip = (unsigned long)p->addr;
e7a510f9 431 reset_current_kprobe();
aa3d7e3d
PP
432 ret = 1;
433 } else {
434 /* We have reentered the kprobe_handler(), since
435 * another probe was hit while within the
436 * handler. We here save the original kprobe
437 * variables and just single step on instruction
438 * of the new probe without calling any user
439 * handlers.
440 */
e7a510f9
AM
441 save_previous_kprobe(kcb);
442 set_current_kprobe(p, regs, kcb);
bf8d5c52 443 kprobes_inc_nmissed_count(p);
aa3d7e3d 444 prepare_singlestep(p, regs);
e7a510f9 445 kcb->kprobe_status = KPROBE_REENTER;
aa3d7e3d 446 return 1;
1da177e4 447 }
1da177e4 448 } else {
eb3a7292
KA
449 if (*addr != BREAKPOINT_INSTRUCTION) {
450 /* The breakpoint instruction was removed by
451 * another cpu right after we hit, no further
452 * handling of this interrupt is appropriate
453 */
65ea5b03 454 regs->ip = (unsigned long)addr;
eb3a7292
KA
455 ret = 1;
456 goto no_kprobe;
457 }
e7a510f9 458 p = __get_cpu_var(current_kprobe);
1da177e4
LT
459 if (p->break_handler && p->break_handler(p, regs)) {
460 goto ss_probe;
461 }
462 }
1da177e4
LT
463 goto no_kprobe;
464 }
465
1da177e4
LT
466 p = get_kprobe(addr);
467 if (!p) {
1da177e4
LT
468 if (*addr != BREAKPOINT_INSTRUCTION) {
469 /*
470 * The breakpoint instruction was removed right
471 * after we hit it. Another cpu has removed
472 * either a probepoint or a debugger breakpoint
473 * at this address. In either case, no further
474 * handling of this interrupt is appropriate.
bce06494
JK
475 * Back up over the (now missing) int3 and run
476 * the original instruction.
1da177e4 477 */
65ea5b03 478 regs->ip = (unsigned long)addr;
1da177e4
LT
479 ret = 1;
480 }
481 /* Not one of ours: let kernel handle it */
482 goto no_kprobe;
483 }
484
e7a510f9
AM
485 set_current_kprobe(p, regs, kcb);
486 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
1da177e4
LT
487
488 if (p->pre_handler && p->pre_handler(p, regs))
489 /* handler has already set things up, so skip ss setup */
490 return 1;
491
492ss_probe:
aa470140
MH
493#if !defined(CONFIG_PREEMPT) || defined(CONFIG_PM)
494 if (p->ainsn.boostable == 1 && !p->post_handler) {
495 /* Boost up -- we can execute copied instructions directly */
496 reset_current_kprobe();
497 regs->ip = (unsigned long)p->ainsn.insn;
498 preempt_enable_no_resched();
499 return 1;
500 }
501#endif
1da177e4 502 prepare_singlestep(p, regs);
e7a510f9 503 kcb->kprobe_status = KPROBE_HIT_SS;
1da177e4
LT
504 return 1;
505
506no_kprobe:
d217d545 507 preempt_enable_no_resched();
1da177e4
LT
508 return ret;
509}
510
73649dab 511/*
da07ab03
MH
512 * When a retprobed function returns, this code saves registers and
513 * calls trampoline_handler() runs, which calls the kretprobe's handler.
73649dab 514 */
da07ab03 515 void __kprobes kretprobe_trampoline_holder(void)
73649dab
RL
516 {
517 asm volatile ( ".global kretprobe_trampoline\n"
da07ab03
MH
518 "kretprobe_trampoline: \n"
519 /* We don't bother saving the ss register */
520 " pushq %rsp\n"
521 " pushfq\n"
522 /*
523 * Skip cs, ip, orig_ax.
524 * trampoline_handler() will plug in these values
525 */
526 " subq $24, %rsp\n"
527 " pushq %rdi\n"
528 " pushq %rsi\n"
529 " pushq %rdx\n"
530 " pushq %rcx\n"
531 " pushq %rax\n"
532 " pushq %r8\n"
533 " pushq %r9\n"
534 " pushq %r10\n"
535 " pushq %r11\n"
536 " pushq %rbx\n"
537 " pushq %rbp\n"
538 " pushq %r12\n"
539 " pushq %r13\n"
540 " pushq %r14\n"
541 " pushq %r15\n"
542 " movq %rsp, %rdi\n"
543 " call trampoline_handler\n"
544 /* Replace saved sp with true return address. */
545 " movq %rax, 152(%rsp)\n"
546 " popq %r15\n"
547 " popq %r14\n"
548 " popq %r13\n"
549 " popq %r12\n"
550 " popq %rbp\n"
551 " popq %rbx\n"
552 " popq %r11\n"
553 " popq %r10\n"
554 " popq %r9\n"
555 " popq %r8\n"
556 " popq %rax\n"
557 " popq %rcx\n"
558 " popq %rdx\n"
559 " popq %rsi\n"
560 " popq %rdi\n"
561 /* Skip orig_ax, ip, cs */
562 " addq $24, %rsp\n"
563 " popfq\n"
564 " ret\n");
73649dab
RL
565 }
566
567/*
da07ab03 568 * Called from kretprobe_trampoline
73649dab 569 */
da07ab03 570fastcall void * __kprobes trampoline_handler(struct pt_regs *regs)
73649dab 571{
62c27be0 572 struct kretprobe_instance *ri = NULL;
99219a3f 573 struct hlist_head *head, empty_rp;
62c27be0 574 struct hlist_node *node, *tmp;
991a51d8 575 unsigned long flags, orig_ret_address = 0;
ba8af12f 576 unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
73649dab 577
99219a3f 578 INIT_HLIST_HEAD(&empty_rp);
991a51d8 579 spin_lock_irqsave(&kretprobe_lock, flags);
62c27be0 580 head = kretprobe_inst_table_head(current);
da07ab03
MH
581 /* fixup rt_regs */
582 regs->cs = __KERNEL_CS;
583 regs->ip = trampoline_address;
584 regs->orig_ax = 0xffffffffffffffff;
73649dab 585
ba8af12f
RL
586 /*
587 * It is possible to have multiple instances associated with a given
588 * task either because an multiple functions in the call path
589 * have a return probe installed on them, and/or more then one return
590 * return probe was registered for a target function.
591 *
592 * We can handle this because:
593 * - instances are always inserted at the head of the list
594 * - when multiple return probes are registered for the same
62c27be0 595 * function, the first instance's ret_addr will point to the
ba8af12f
RL
596 * real return address, and all the rest will point to
597 * kretprobe_trampoline
598 */
599 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
62c27be0 600 if (ri->task != current)
ba8af12f 601 /* another task is sharing our hash bucket */
62c27be0 602 continue;
ba8af12f 603
da07ab03
MH
604 if (ri->rp && ri->rp->handler) {
605 __get_cpu_var(current_kprobe) = &ri->rp->kp;
606 get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
ba8af12f 607 ri->rp->handler(ri, regs);
da07ab03
MH
608 __get_cpu_var(current_kprobe) = NULL;
609 }
ba8af12f
RL
610
611 orig_ret_address = (unsigned long)ri->ret_addr;
99219a3f 612 recycle_rp_inst(ri, &empty_rp);
ba8af12f
RL
613
614 if (orig_ret_address != trampoline_address)
615 /*
616 * This is the real return address. Any other
617 * instances associated with this task are for
618 * other calls deeper on the call stack
619 */
620 break;
73649dab 621 }
ba8af12f 622
0f95b7fc 623 kretprobe_assert(ri, orig_ret_address, trampoline_address);
ba8af12f 624
991a51d8 625 spin_unlock_irqrestore(&kretprobe_lock, flags);
ba8af12f 626
99219a3f 627 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
628 hlist_del(&ri->hlist);
629 kfree(ri);
630 }
da07ab03 631 return (void *)orig_ret_address;
73649dab
RL
632}
633
1da177e4
LT
634/*
635 * Called after single-stepping. p->addr is the address of the
636 * instruction whose first byte has been replaced by the "int 3"
637 * instruction. To avoid the SMP problems that can occur when we
638 * temporarily put back the original opcode to single-step, we
639 * single-stepped a copy of the instruction. The address of this
640 * copy is p->ainsn.insn.
641 *
642 * This function prepares to return from the post-single-step
643 * interrupt. We have to fix up the stack as follows:
644 *
645 * 0) Except in the case of absolute or indirect jump or call instructions,
65ea5b03 646 * the new ip is relative to the copied instruction. We need to make
1da177e4
LT
647 * it relative to the original instruction.
648 *
649 * 1) If the single-stepped instruction was pushfl, then the TF and IF
65ea5b03 650 * flags are set in the just-pushed flags, and may need to be cleared.
1da177e4
LT
651 *
652 * 2) If the single-stepped instruction was a call, the return address
653 * that is atop the stack is the address following the copied instruction.
654 * We need to make it the address following the original instruction.
aa470140
MH
655 *
656 * If this is the first time we've single-stepped the instruction at
657 * this probepoint, and the instruction is boostable, boost it: add a
658 * jump instruction after the copied instruction, that jumps to the next
659 * instruction after the probepoint.
1da177e4 660 */
e7a510f9
AM
661static void __kprobes resume_execution(struct kprobe *p,
662 struct pt_regs *regs, struct kprobe_ctlblk *kcb)
1da177e4 663{
65ea5b03 664 unsigned long *tos = (unsigned long *)regs->sp;
1da177e4
LT
665 unsigned long copy_rip = (unsigned long)p->ainsn.insn;
666 unsigned long orig_rip = (unsigned long)p->addr;
667 kprobe_opcode_t *insn = p->ainsn.insn;
668
669 /*skip the REX prefix*/
670 if (*insn >= 0x40 && *insn <= 0x4f)
671 insn++;
672
65ea5b03 673 regs->flags &= ~TF_MASK;
1da177e4 674 switch (*insn) {
0b0122fa 675 case 0x9c: /* pushfl */
1da177e4 676 *tos &= ~(TF_MASK | IF_MASK);
e7a510f9 677 *tos |= kcb->kprobe_old_rflags;
1da177e4 678 break;
0b0122fa
MH
679 case 0xc2: /* iret/ret/lret */
680 case 0xc3:
0b9e2cac 681 case 0xca:
0b0122fa
MH
682 case 0xcb:
683 case 0xcf:
684 case 0xea: /* jmp absolute -- ip is correct */
685 /* ip is already adjusted, no more changes required */
aa470140 686 p->ainsn.boostable = 1;
0b0122fa
MH
687 goto no_change;
688 case 0xe8: /* call relative - Fix return addr */
1da177e4
LT
689 *tos = orig_rip + (*tos - copy_rip);
690 break;
691 case 0xff:
dc49e344 692 if ((insn[1] & 0x30) == 0x10) {
1da177e4 693 /* call absolute, indirect */
0b0122fa 694 /* Fix return addr; ip is correct. */
aa470140 695 /* not boostable */
1da177e4 696 *tos = orig_rip + (*tos - copy_rip);
0b0122fa 697 goto no_change;
dc49e344
SO
698 } else if (((insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
699 ((insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */
aa470140
MH
700 /* ip is correct. And this is boostable */
701 p->ainsn.boostable = 1;
0b0122fa 702 goto no_change;
1da177e4 703 }
1da177e4
LT
704 default:
705 break;
706 }
707
aa470140
MH
708 if (p->ainsn.boostable == 0) {
709 if ((regs->ip > copy_rip) &&
710 (regs->ip - copy_rip) + 5 < MAX_INSN_SIZE) {
711 /*
712 * These instructions can be executed directly if it
713 * jumps back to correct address.
714 */
715 set_jmp_op((void *)regs->ip,
716 (void *)orig_rip + (regs->ip - copy_rip));
717 p->ainsn.boostable = 1;
718 } else {
719 p->ainsn.boostable = -1;
720 }
721 }
722
65ea5b03
PA
723 regs->ip = orig_rip + (regs->ip - copy_rip);
724
0b0122fa 725no_change:
1ecc798c 726 restore_btf();
0b0122fa
MH
727
728 return;
1da177e4
LT
729}
730
0f2fbdcb 731int __kprobes post_kprobe_handler(struct pt_regs *regs)
1da177e4 732{
e7a510f9
AM
733 struct kprobe *cur = kprobe_running();
734 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
735
736 if (!cur)
1da177e4
LT
737 return 0;
738
e7a510f9
AM
739 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
740 kcb->kprobe_status = KPROBE_HIT_SSDONE;
741 cur->post_handler(cur, regs, 0);
aa3d7e3d 742 }
1da177e4 743
e7a510f9 744 resume_execution(cur, regs, kcb);
65ea5b03
PA
745 regs->flags |= kcb->kprobe_saved_rflags;
746 trace_hardirqs_fixup_flags(regs->flags);
1da177e4 747
aa3d7e3d 748 /* Restore the original saved kprobes variables and continue. */
e7a510f9
AM
749 if (kcb->kprobe_status == KPROBE_REENTER) {
750 restore_previous_kprobe(kcb);
aa3d7e3d 751 goto out;
aa3d7e3d 752 }
e7a510f9 753 reset_current_kprobe();
aa3d7e3d 754out:
1da177e4
LT
755 preempt_enable_no_resched();
756
757 /*
65ea5b03 758 * if somebody else is singlestepping across a probe point, flags
1da177e4
LT
759 * will have TF set, in which case, continue the remaining processing
760 * of do_debug, as if this is not a probe hit.
761 */
65ea5b03 762 if (regs->flags & TF_MASK)
1da177e4
LT
763 return 0;
764
765 return 1;
766}
767
0f2fbdcb 768int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
1da177e4 769{
e7a510f9
AM
770 struct kprobe *cur = kprobe_running();
771 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
c28f8966 772 const struct exception_table_entry *fixup;
e7a510f9 773
c28f8966
PP
774 switch(kcb->kprobe_status) {
775 case KPROBE_HIT_SS:
776 case KPROBE_REENTER:
777 /*
778 * We are here because the instruction being single
779 * stepped caused a page fault. We reset the current
65ea5b03 780 * kprobe and the ip points back to the probe address
c28f8966
PP
781 * and allow the page fault handler to continue as a
782 * normal page fault.
783 */
65ea5b03
PA
784 regs->ip = (unsigned long)cur->addr;
785 regs->flags |= kcb->kprobe_old_rflags;
c28f8966
PP
786 if (kcb->kprobe_status == KPROBE_REENTER)
787 restore_previous_kprobe(kcb);
788 else
789 reset_current_kprobe();
1da177e4 790 preempt_enable_no_resched();
c28f8966
PP
791 break;
792 case KPROBE_HIT_ACTIVE:
793 case KPROBE_HIT_SSDONE:
794 /*
795 * We increment the nmissed count for accounting,
796 * we can also use npre/npostfault count for accouting
797 * these specific fault cases.
798 */
799 kprobes_inc_nmissed_count(cur);
800
801 /*
802 * We come here because instructions in the pre/post
803 * handler caused the page_fault, this could happen
804 * if handler tries to access user space by
805 * copy_from_user(), get_user() etc. Let the
806 * user-specified handler try to fix it first.
807 */
808 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
809 return 1;
810
811 /*
812 * In case the user-specified fault handler returned
813 * zero, try to fix up.
814 */
65ea5b03 815 fixup = search_exception_tables(regs->ip);
c28f8966 816 if (fixup) {
65ea5b03 817 regs->ip = fixup->fixup;
c28f8966
PP
818 return 1;
819 }
820
821 /*
822 * fixup() could not handle it,
823 * Let do_page_fault() fix it.
824 */
825 break;
826 default:
827 break;
1da177e4
LT
828 }
829 return 0;
830}
831
832/*
833 * Wrapper routine for handling exceptions.
834 */
0f2fbdcb
PP
835int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
836 unsigned long val, void *data)
1da177e4
LT
837{
838 struct die_args *args = (struct die_args *)data;
66ff2d06
AM
839 int ret = NOTIFY_DONE;
840
2326c770 841 if (args->regs && user_mode(args->regs))
842 return ret;
843
1da177e4
LT
844 switch (val) {
845 case DIE_INT3:
846 if (kprobe_handler(args->regs))
66ff2d06 847 ret = NOTIFY_STOP;
1da177e4
LT
848 break;
849 case DIE_DEBUG:
850 if (post_kprobe_handler(args->regs))
66ff2d06 851 ret = NOTIFY_STOP;
1da177e4
LT
852 break;
853 case DIE_GPF:
d217d545
AM
854 /* kprobe_running() needs smp_processor_id() */
855 preempt_disable();
1da177e4
LT
856 if (kprobe_running() &&
857 kprobe_fault_handler(args->regs, args->trapnr))
66ff2d06 858 ret = NOTIFY_STOP;
d217d545 859 preempt_enable();
1da177e4
LT
860 break;
861 default:
862 break;
863 }
66ff2d06 864 return ret;
1da177e4
LT
865}
866
0f2fbdcb 867int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
1da177e4
LT
868{
869 struct jprobe *jp = container_of(p, struct jprobe, kp);
870 unsigned long addr;
e7a510f9 871 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1da177e4 872
e7a510f9 873 kcb->jprobe_saved_regs = *regs;
65ea5b03 874 kcb->jprobe_saved_rsp = (long *) regs->sp;
e7a510f9 875 addr = (unsigned long)(kcb->jprobe_saved_rsp);
1da177e4
LT
876 /*
877 * As Linus pointed out, gcc assumes that the callee
878 * owns the argument space and could overwrite it, e.g.
879 * tailcall optimization. So, to be absolutely safe
880 * we also save and restore enough stack bytes to cover
881 * the argument area.
882 */
e7a510f9
AM
883 memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
884 MIN_STACK_SIZE(addr));
65ea5b03 885 regs->flags &= ~IF_MASK;
58dfe883 886 trace_hardirqs_off();
65ea5b03 887 regs->ip = (unsigned long)(jp->entry);
1da177e4
LT
888 return 1;
889}
890
0f2fbdcb 891void __kprobes jprobe_return(void)
1da177e4 892{
e7a510f9
AM
893 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
894
1da177e4
LT
895 asm volatile (" xchg %%rbx,%%rsp \n"
896 " int3 \n"
897 " .globl jprobe_return_end \n"
898 " jprobe_return_end: \n"
899 " nop \n"::"b"
e7a510f9 900 (kcb->jprobe_saved_rsp):"memory");
1da177e4
LT
901}
902
0f2fbdcb 903int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
1da177e4 904{
e7a510f9 905 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
65ea5b03 906 u8 *addr = (u8 *) (regs->ip - 1);
e7a510f9 907 unsigned long stack_addr = (unsigned long)(kcb->jprobe_saved_rsp);
1da177e4
LT
908 struct jprobe *jp = container_of(p, struct jprobe, kp);
909
910 if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
65ea5b03 911 if ((unsigned long *)regs->sp != kcb->jprobe_saved_rsp) {
29b6cd79 912 struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
65ea5b03
PA
913 printk("current sp %p does not match saved sp %p\n",
914 (long *)regs->sp, kcb->jprobe_saved_rsp);
1da177e4
LT
915 printk("Saved registers for jprobe %p\n", jp);
916 show_registers(saved_regs);
917 printk("Current registers\n");
918 show_registers(regs);
919 BUG();
920 }
e7a510f9
AM
921 *regs = kcb->jprobe_saved_regs;
922 memcpy((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack,
1da177e4 923 MIN_STACK_SIZE(stack_addr));
d217d545 924 preempt_enable_no_resched();
1da177e4
LT
925 return 1;
926 }
927 return 0;
928}
ba8af12f 929
6772926b 930int __init arch_init_kprobes(void)
ba8af12f 931{
da07ab03 932 return 0;
ba8af12f 933}
bf8f6e5b
AM
934
935int __kprobes arch_trampoline_kprobe(struct kprobe *p)
936{
bf8f6e5b
AM
937 return 0;
938}