]> bbs.cooldavid.org Git - net-next-2.6.git/blame - arch/x86_64/kernel/kprobes.c
[PATCH] x86_64 specific function return probes
[net-next-2.6.git] / arch / x86_64 / kernel / kprobes.c
CommitLineData
1da177e4
LT
1/*
2 * Kernel Probes (KProbes)
3 * arch/x86_64/kernel/kprobes.c
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 * Copyright (C) IBM Corporation, 2002, 2004
20 *
21 * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
22 * Probes initial implementation ( includes contributions from
23 * Rusty Russell).
24 * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
25 * interface to access function arguments.
26 * 2004-Oct Jim Keniston <kenistoj@us.ibm.com> and Prasanna S Panchamukhi
27 * <prasanna@in.ibm.com> adapted for x86_64
28 * 2005-Mar Roland McGrath <roland@redhat.com>
29 * Fixed to handle %rip-relative addressing mode correctly.
73649dab
RL
30 * 2005-May Rusty Lynch <rusty.lynch@intel.com>
31 * Added function return probes functionality
1da177e4
LT
32 */
33
34#include <linux/config.h>
35#include <linux/kprobes.h>
36#include <linux/ptrace.h>
37#include <linux/spinlock.h>
38#include <linux/string.h>
39#include <linux/slab.h>
40#include <linux/preempt.h>
41#include <linux/moduleloader.h>
42
43#include <asm/pgtable.h>
44#include <asm/kdebug.h>
45
46static DECLARE_MUTEX(kprobe_mutex);
47
48/* kprobe_status settings */
49#define KPROBE_HIT_ACTIVE 0x00000001
50#define KPROBE_HIT_SS 0x00000002
51
52static struct kprobe *current_kprobe;
53static unsigned long kprobe_status, kprobe_old_rflags, kprobe_saved_rflags;
54static struct pt_regs jprobe_saved_regs;
55static long *jprobe_saved_rsp;
56static kprobe_opcode_t *get_insn_slot(void);
57static void free_insn_slot(kprobe_opcode_t *slot);
58void jprobe_return_end(void);
59
60/* copy of the kernel stack at the probe fire time */
61static kprobe_opcode_t jprobes_stack[MAX_STACK_SIZE];
62
63/*
64 * returns non-zero if opcode modifies the interrupt flag.
65 */
66static inline int is_IF_modifier(kprobe_opcode_t *insn)
67{
68 switch (*insn) {
69 case 0xfa: /* cli */
70 case 0xfb: /* sti */
71 case 0xcf: /* iret/iretd */
72 case 0x9d: /* popf/popfd */
73 return 1;
74 }
75
76 if (*insn >= 0x40 && *insn <= 0x4f && *++insn == 0xcf)
77 return 1;
78 return 0;
79}
80
81int arch_prepare_kprobe(struct kprobe *p)
82{
83 /* insn: must be on special executable page on x86_64. */
84 up(&kprobe_mutex);
85 p->ainsn.insn = get_insn_slot();
86 down(&kprobe_mutex);
87 if (!p->ainsn.insn) {
88 return -ENOMEM;
89 }
90 return 0;
91}
92
93/*
94 * Determine if the instruction uses the %rip-relative addressing mode.
95 * If it does, return the address of the 32-bit displacement word.
96 * If not, return null.
97 */
98static inline s32 *is_riprel(u8 *insn)
99{
100#define W(row,b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,ba,bb,bc,bd,be,bf) \
101 (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
102 (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
103 (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
104 (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
105 << (row % 64))
106 static const u64 onebyte_has_modrm[256 / 64] = {
107 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
108 /* ------------------------------- */
109 W(0x00, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0)| /* 00 */
110 W(0x10, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0)| /* 10 */
111 W(0x20, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0)| /* 20 */
112 W(0x30, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0), /* 30 */
113 W(0x40, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 40 */
114 W(0x50, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 50 */
115 W(0x60, 0,0,1,1,0,0,0,0,0,1,0,1,0,0,0,0)| /* 60 */
116 W(0x70, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* 70 */
117 W(0x80, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 80 */
118 W(0x90, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 90 */
119 W(0xa0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* a0 */
120 W(0xb0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* b0 */
121 W(0xc0, 1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0)| /* c0 */
122 W(0xd0, 1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1)| /* d0 */
123 W(0xe0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* e0 */
124 W(0xf0, 0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1) /* f0 */
125 /* ------------------------------- */
126 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
127 };
128 static const u64 twobyte_has_modrm[256 / 64] = {
129 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
130 /* ------------------------------- */
131 W(0x00, 1,1,1,1,0,0,0,0,0,0,0,0,0,1,0,1)| /* 0f */
132 W(0x10, 1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0)| /* 1f */
133 W(0x20, 1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1)| /* 2f */
134 W(0x30, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* 3f */
135 W(0x40, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 4f */
136 W(0x50, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 5f */
137 W(0x60, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 6f */
138 W(0x70, 1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1), /* 7f */
139 W(0x80, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 8f */
140 W(0x90, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 9f */
141 W(0xa0, 0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1)| /* af */
142 W(0xb0, 1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1), /* bf */
143 W(0xc0, 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0)| /* cf */
144 W(0xd0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* df */
145 W(0xe0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* ef */
146 W(0xf0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0) /* ff */
147 /* ------------------------------- */
148 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
149 };
150#undef W
151 int need_modrm;
152
153 /* Skip legacy instruction prefixes. */
154 while (1) {
155 switch (*insn) {
156 case 0x66:
157 case 0x67:
158 case 0x2e:
159 case 0x3e:
160 case 0x26:
161 case 0x64:
162 case 0x65:
163 case 0x36:
164 case 0xf0:
165 case 0xf3:
166 case 0xf2:
167 ++insn;
168 continue;
169 }
170 break;
171 }
172
173 /* Skip REX instruction prefix. */
174 if ((*insn & 0xf0) == 0x40)
175 ++insn;
176
177 if (*insn == 0x0f) { /* Two-byte opcode. */
178 ++insn;
179 need_modrm = test_bit(*insn, twobyte_has_modrm);
180 } else { /* One-byte opcode. */
181 need_modrm = test_bit(*insn, onebyte_has_modrm);
182 }
183
184 if (need_modrm) {
185 u8 modrm = *++insn;
186 if ((modrm & 0xc7) == 0x05) { /* %rip+disp32 addressing mode */
187 /* Displacement follows ModRM byte. */
188 return (s32 *) ++insn;
189 }
190 }
191
192 /* No %rip-relative addressing mode here. */
193 return NULL;
194}
195
196void arch_copy_kprobe(struct kprobe *p)
197{
198 s32 *ripdisp;
199 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE);
200 ripdisp = is_riprel(p->ainsn.insn);
201 if (ripdisp) {
202 /*
203 * The copied instruction uses the %rip-relative
204 * addressing mode. Adjust the displacement for the
205 * difference between the original location of this
206 * instruction and the location of the copy that will
207 * actually be run. The tricky bit here is making sure
208 * that the sign extension happens correctly in this
209 * calculation, since we need a signed 32-bit result to
210 * be sign-extended to 64 bits when it's added to the
211 * %rip value and yield the same 64-bit result that the
212 * sign-extension of the original signed 32-bit
213 * displacement would have given.
214 */
215 s64 disp = (u8 *) p->addr + *ripdisp - (u8 *) p->ainsn.insn;
216 BUG_ON((s64) (s32) disp != disp); /* Sanity check. */
217 *ripdisp = disp;
218 }
219}
220
221void arch_remove_kprobe(struct kprobe *p)
222{
223 up(&kprobe_mutex);
224 free_insn_slot(p->ainsn.insn);
225 down(&kprobe_mutex);
226}
227
228static inline void disarm_kprobe(struct kprobe *p, struct pt_regs *regs)
229{
230 *p->addr = p->opcode;
231 regs->rip = (unsigned long)p->addr;
232}
233
234static void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
235{
236 regs->eflags |= TF_MASK;
237 regs->eflags &= ~IF_MASK;
238 /*single step inline if the instruction is an int3*/
239 if (p->opcode == BREAKPOINT_INSTRUCTION)
240 regs->rip = (unsigned long)p->addr;
241 else
242 regs->rip = (unsigned long)p->ainsn.insn;
243}
244
73649dab
RL
245struct task_struct *arch_get_kprobe_task(void *ptr)
246{
247 return ((struct thread_info *) (((unsigned long) ptr) &
248 (~(THREAD_SIZE -1))))->task;
249}
250
251void arch_prepare_kretprobe(struct kretprobe *rp, struct pt_regs *regs)
252{
253 unsigned long *sara = (unsigned long *)regs->rsp;
254 struct kretprobe_instance *ri;
255 static void *orig_ret_addr;
256
257 /*
258 * Save the return address when the return probe hits
259 * the first time, and use it to populate the (krprobe
260 * instance)->ret_addr for subsequent return probes at
261 * the same addrress since stack address would have
262 * the kretprobe_trampoline by then.
263 */
264 if (((void*) *sara) != kretprobe_trampoline)
265 orig_ret_addr = (void*) *sara;
266
267 if ((ri = get_free_rp_inst(rp)) != NULL) {
268 ri->rp = rp;
269 ri->stack_addr = sara;
270 ri->ret_addr = orig_ret_addr;
271 add_rp_inst(ri);
272 /* Replace the return addr with trampoline addr */
273 *sara = (unsigned long) &kretprobe_trampoline;
274 } else {
275 rp->nmissed++;
276 }
277}
278
279void arch_kprobe_flush_task(struct task_struct *tk)
280{
281 struct kretprobe_instance *ri;
282 while ((ri = get_rp_inst_tsk(tk)) != NULL) {
283 *((unsigned long *)(ri->stack_addr)) =
284 (unsigned long) ri->ret_addr;
285 recycle_rp_inst(ri);
286 }
287}
288
1da177e4
LT
289/*
290 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
291 * remain disabled thorough out this function.
292 */
293int kprobe_handler(struct pt_regs *regs)
294{
295 struct kprobe *p;
296 int ret = 0;
297 kprobe_opcode_t *addr = (kprobe_opcode_t *)(regs->rip - sizeof(kprobe_opcode_t));
298
299 /* We're in an interrupt, but this is clear and BUG()-safe. */
300 preempt_disable();
301
302 /* Check we're not actually recursing */
303 if (kprobe_running()) {
304 /* We *are* holding lock here, so this is safe.
305 Disarm the probe we just hit, and ignore it. */
306 p = get_kprobe(addr);
307 if (p) {
308 if (kprobe_status == KPROBE_HIT_SS) {
309 regs->eflags &= ~TF_MASK;
310 regs->eflags |= kprobe_saved_rflags;
311 unlock_kprobes();
312 goto no_kprobe;
313 }
314 disarm_kprobe(p, regs);
315 ret = 1;
316 } else {
317 p = current_kprobe;
318 if (p->break_handler && p->break_handler(p, regs)) {
319 goto ss_probe;
320 }
321 }
322 /* If it's not ours, can't be delete race, (we hold lock). */
323 goto no_kprobe;
324 }
325
326 lock_kprobes();
327 p = get_kprobe(addr);
328 if (!p) {
329 unlock_kprobes();
330 if (*addr != BREAKPOINT_INSTRUCTION) {
331 /*
332 * The breakpoint instruction was removed right
333 * after we hit it. Another cpu has removed
334 * either a probepoint or a debugger breakpoint
335 * at this address. In either case, no further
336 * handling of this interrupt is appropriate.
337 */
338 ret = 1;
339 }
340 /* Not one of ours: let kernel handle it */
341 goto no_kprobe;
342 }
343
344 kprobe_status = KPROBE_HIT_ACTIVE;
345 current_kprobe = p;
346 kprobe_saved_rflags = kprobe_old_rflags
347 = (regs->eflags & (TF_MASK | IF_MASK));
348 if (is_IF_modifier(p->ainsn.insn))
349 kprobe_saved_rflags &= ~IF_MASK;
350
351 if (p->pre_handler && p->pre_handler(p, regs))
352 /* handler has already set things up, so skip ss setup */
353 return 1;
354
355ss_probe:
356 prepare_singlestep(p, regs);
357 kprobe_status = KPROBE_HIT_SS;
358 return 1;
359
360no_kprobe:
361 preempt_enable_no_resched();
362 return ret;
363}
364
73649dab
RL
365/*
366 * For function-return probes, init_kprobes() establishes a probepoint
367 * here. When a retprobed function returns, this probe is hit and
368 * trampoline_probe_handler() runs, calling the kretprobe's handler.
369 */
370 void kretprobe_trampoline_holder(void)
371 {
372 asm volatile ( ".global kretprobe_trampoline\n"
373 "kretprobe_trampoline: \n"
374 "nop\n");
375 }
376
377/*
378 * Called when we hit the probe point at kretprobe_trampoline
379 */
380int trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
381{
382 struct task_struct *tsk;
383 struct kretprobe_instance *ri;
384 struct hlist_head *head;
385 struct hlist_node *node;
386 unsigned long *sara = (unsigned long *)regs->rsp - 1;
387
388 tsk = arch_get_kprobe_task(sara);
389 head = kretprobe_inst_table_head(tsk);
390
391 hlist_for_each_entry(ri, node, head, hlist) {
392 if (ri->stack_addr == sara && ri->rp) {
393 if (ri->rp->handler)
394 ri->rp->handler(ri, regs);
395 }
396 }
397 return 0;
398}
399
400void trampoline_post_handler(struct kprobe *p, struct pt_regs *regs,
401 unsigned long flags)
402{
403 struct kretprobe_instance *ri;
404 /* RA already popped */
405 unsigned long *sara = ((unsigned long *)regs->rsp) - 1;
406
407 while ((ri = get_rp_inst(sara))) {
408 regs->rip = (unsigned long)ri->ret_addr;
409 recycle_rp_inst(ri);
410 }
411 regs->eflags &= ~TF_MASK;
412}
413
1da177e4
LT
414/*
415 * Called after single-stepping. p->addr is the address of the
416 * instruction whose first byte has been replaced by the "int 3"
417 * instruction. To avoid the SMP problems that can occur when we
418 * temporarily put back the original opcode to single-step, we
419 * single-stepped a copy of the instruction. The address of this
420 * copy is p->ainsn.insn.
421 *
422 * This function prepares to return from the post-single-step
423 * interrupt. We have to fix up the stack as follows:
424 *
425 * 0) Except in the case of absolute or indirect jump or call instructions,
426 * the new rip is relative to the copied instruction. We need to make
427 * it relative to the original instruction.
428 *
429 * 1) If the single-stepped instruction was pushfl, then the TF and IF
430 * flags are set in the just-pushed eflags, and may need to be cleared.
431 *
432 * 2) If the single-stepped instruction was a call, the return address
433 * that is atop the stack is the address following the copied instruction.
434 * We need to make it the address following the original instruction.
435 */
436static void resume_execution(struct kprobe *p, struct pt_regs *regs)
437{
438 unsigned long *tos = (unsigned long *)regs->rsp;
439 unsigned long next_rip = 0;
440 unsigned long copy_rip = (unsigned long)p->ainsn.insn;
441 unsigned long orig_rip = (unsigned long)p->addr;
442 kprobe_opcode_t *insn = p->ainsn.insn;
443
444 /*skip the REX prefix*/
445 if (*insn >= 0x40 && *insn <= 0x4f)
446 insn++;
447
448 switch (*insn) {
449 case 0x9c: /* pushfl */
450 *tos &= ~(TF_MASK | IF_MASK);
451 *tos |= kprobe_old_rflags;
452 break;
0b9e2cac
PP
453 case 0xc3: /* ret/lret */
454 case 0xcb:
455 case 0xc2:
456 case 0xca:
457 regs->eflags &= ~TF_MASK;
458 /* rip is already adjusted, no more changes required*/
459 return;
1da177e4
LT
460 case 0xe8: /* call relative - Fix return addr */
461 *tos = orig_rip + (*tos - copy_rip);
462 break;
463 case 0xff:
464 if ((*insn & 0x30) == 0x10) {
465 /* call absolute, indirect */
466 /* Fix return addr; rip is correct. */
467 next_rip = regs->rip;
468 *tos = orig_rip + (*tos - copy_rip);
469 } else if (((*insn & 0x31) == 0x20) || /* jmp near, absolute indirect */
470 ((*insn & 0x31) == 0x21)) { /* jmp far, absolute indirect */
471 /* rip is correct. */
472 next_rip = regs->rip;
473 }
474 break;
475 case 0xea: /* jmp absolute -- rip is correct */
476 next_rip = regs->rip;
477 break;
478 default:
479 break;
480 }
481
482 regs->eflags &= ~TF_MASK;
483 if (next_rip) {
484 regs->rip = next_rip;
485 } else {
486 regs->rip = orig_rip + (regs->rip - copy_rip);
487 }
488}
489
490/*
491 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
492 * remain disabled thoroughout this function. And we hold kprobe lock.
493 */
494int post_kprobe_handler(struct pt_regs *regs)
495{
496 if (!kprobe_running())
497 return 0;
498
499 if (current_kprobe->post_handler)
500 current_kprobe->post_handler(current_kprobe, regs, 0);
501
73649dab
RL
502 if (current_kprobe->post_handler != trampoline_post_handler)
503 resume_execution(current_kprobe, regs);
1da177e4
LT
504 regs->eflags |= kprobe_saved_rflags;
505
506 unlock_kprobes();
507 preempt_enable_no_resched();
508
509 /*
510 * if somebody else is singlestepping across a probe point, eflags
511 * will have TF set, in which case, continue the remaining processing
512 * of do_debug, as if this is not a probe hit.
513 */
514 if (regs->eflags & TF_MASK)
515 return 0;
516
517 return 1;
518}
519
520/* Interrupts disabled, kprobe_lock held. */
521int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
522{
523 if (current_kprobe->fault_handler
524 && current_kprobe->fault_handler(current_kprobe, regs, trapnr))
525 return 1;
526
527 if (kprobe_status & KPROBE_HIT_SS) {
528 resume_execution(current_kprobe, regs);
529 regs->eflags |= kprobe_old_rflags;
530
531 unlock_kprobes();
532 preempt_enable_no_resched();
533 }
534 return 0;
535}
536
537/*
538 * Wrapper routine for handling exceptions.
539 */
540int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
541 void *data)
542{
543 struct die_args *args = (struct die_args *)data;
544 switch (val) {
545 case DIE_INT3:
546 if (kprobe_handler(args->regs))
547 return NOTIFY_STOP;
548 break;
549 case DIE_DEBUG:
550 if (post_kprobe_handler(args->regs))
551 return NOTIFY_STOP;
552 break;
553 case DIE_GPF:
554 if (kprobe_running() &&
555 kprobe_fault_handler(args->regs, args->trapnr))
556 return NOTIFY_STOP;
557 break;
558 case DIE_PAGE_FAULT:
559 if (kprobe_running() &&
560 kprobe_fault_handler(args->regs, args->trapnr))
561 return NOTIFY_STOP;
562 break;
563 default:
564 break;
565 }
566 return NOTIFY_DONE;
567}
568
569int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
570{
571 struct jprobe *jp = container_of(p, struct jprobe, kp);
572 unsigned long addr;
573
574 jprobe_saved_regs = *regs;
575 jprobe_saved_rsp = (long *) regs->rsp;
576 addr = (unsigned long)jprobe_saved_rsp;
577 /*
578 * As Linus pointed out, gcc assumes that the callee
579 * owns the argument space and could overwrite it, e.g.
580 * tailcall optimization. So, to be absolutely safe
581 * we also save and restore enough stack bytes to cover
582 * the argument area.
583 */
584 memcpy(jprobes_stack, (kprobe_opcode_t *) addr, MIN_STACK_SIZE(addr));
585 regs->eflags &= ~IF_MASK;
586 regs->rip = (unsigned long)(jp->entry);
587 return 1;
588}
589
590void jprobe_return(void)
591{
592 preempt_enable_no_resched();
593 asm volatile (" xchg %%rbx,%%rsp \n"
594 " int3 \n"
595 " .globl jprobe_return_end \n"
596 " jprobe_return_end: \n"
597 " nop \n"::"b"
598 (jprobe_saved_rsp):"memory");
599}
600
601int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
602{
603 u8 *addr = (u8 *) (regs->rip - 1);
604 unsigned long stack_addr = (unsigned long)jprobe_saved_rsp;
605 struct jprobe *jp = container_of(p, struct jprobe, kp);
606
607 if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
608 if ((long *)regs->rsp != jprobe_saved_rsp) {
609 struct pt_regs *saved_regs =
610 container_of(jprobe_saved_rsp, struct pt_regs, rsp);
611 printk("current rsp %p does not match saved rsp %p\n",
612 (long *)regs->rsp, jprobe_saved_rsp);
613 printk("Saved registers for jprobe %p\n", jp);
614 show_registers(saved_regs);
615 printk("Current registers\n");
616 show_registers(regs);
617 BUG();
618 }
619 *regs = jprobe_saved_regs;
620 memcpy((kprobe_opcode_t *) stack_addr, jprobes_stack,
621 MIN_STACK_SIZE(stack_addr));
622 return 1;
623 }
624 return 0;
625}
626
627/*
628 * kprobe->ainsn.insn points to the copy of the instruction to be single-stepped.
629 * By default on x86_64, pages we get from kmalloc or vmalloc are not
630 * executable. Single-stepping an instruction on such a page yields an
631 * oops. So instead of storing the instruction copies in their respective
632 * kprobe objects, we allocate a page, map it executable, and store all the
633 * instruction copies there. (We can allocate additional pages if somebody
634 * inserts a huge number of probes.) Each page can hold up to INSNS_PER_PAGE
635 * instruction slots, each of which is MAX_INSN_SIZE*sizeof(kprobe_opcode_t)
636 * bytes.
637 */
638#define INSNS_PER_PAGE (PAGE_SIZE/(MAX_INSN_SIZE*sizeof(kprobe_opcode_t)))
639struct kprobe_insn_page {
640 struct hlist_node hlist;
641 kprobe_opcode_t *insns; /* page of instruction slots */
642 char slot_used[INSNS_PER_PAGE];
643 int nused;
644};
645
646static struct hlist_head kprobe_insn_pages;
647
648/**
649 * get_insn_slot() - Find a slot on an executable page for an instruction.
650 * We allocate an executable page if there's no room on existing ones.
651 */
652static kprobe_opcode_t *get_insn_slot(void)
653{
654 struct kprobe_insn_page *kip;
655 struct hlist_node *pos;
656
657 hlist_for_each(pos, &kprobe_insn_pages) {
658 kip = hlist_entry(pos, struct kprobe_insn_page, hlist);
659 if (kip->nused < INSNS_PER_PAGE) {
660 int i;
661 for (i = 0; i < INSNS_PER_PAGE; i++) {
662 if (!kip->slot_used[i]) {
663 kip->slot_used[i] = 1;
664 kip->nused++;
665 return kip->insns + (i*MAX_INSN_SIZE);
666 }
667 }
668 /* Surprise! No unused slots. Fix kip->nused. */
669 kip->nused = INSNS_PER_PAGE;
670 }
671 }
672
673 /* All out of space. Need to allocate a new page. Use slot 0.*/
674 kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_KERNEL);
675 if (!kip) {
676 return NULL;
677 }
678
679 /*
680 * For the %rip-relative displacement fixups to be doable, we
681 * need our instruction copy to be within +/- 2GB of any data it
682 * might access via %rip. That is, within 2GB of where the
683 * kernel image and loaded module images reside. So we allocate
684 * a page in the module loading area.
685 */
686 kip->insns = module_alloc(PAGE_SIZE);
687 if (!kip->insns) {
688 kfree(kip);
689 return NULL;
690 }
691 INIT_HLIST_NODE(&kip->hlist);
692 hlist_add_head(&kip->hlist, &kprobe_insn_pages);
693 memset(kip->slot_used, 0, INSNS_PER_PAGE);
694 kip->slot_used[0] = 1;
695 kip->nused = 1;
696 return kip->insns;
697}
698
699/**
700 * free_insn_slot() - Free instruction slot obtained from get_insn_slot().
701 */
702static void free_insn_slot(kprobe_opcode_t *slot)
703{
704 struct kprobe_insn_page *kip;
705 struct hlist_node *pos;
706
707 hlist_for_each(pos, &kprobe_insn_pages) {
708 kip = hlist_entry(pos, struct kprobe_insn_page, hlist);
709 if (kip->insns <= slot
710 && slot < kip->insns+(INSNS_PER_PAGE*MAX_INSN_SIZE)) {
711 int i = (slot - kip->insns) / MAX_INSN_SIZE;
712 kip->slot_used[i] = 0;
713 kip->nused--;
714 if (kip->nused == 0) {
715 /*
716 * Page is no longer in use. Free it unless
717 * it's the last one. We keep the last one
718 * so as not to have to set it up again the
719 * next time somebody inserts a probe.
720 */
721 hlist_del(&kip->hlist);
722 if (hlist_empty(&kprobe_insn_pages)) {
723 INIT_HLIST_NODE(&kip->hlist);
724 hlist_add_head(&kip->hlist,
725 &kprobe_insn_pages);
726 } else {
727 module_free(NULL, kip->insns);
728 kfree(kip);
729 }
730 }
731 return;
732 }
733 }
734}