blob: ec2ceade12b071d6e08e9318f7107466f83fde27 [file] [log] [blame]
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -07001/*
2 * Kernel Probes (KProbes)
3 * arch/ia64/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 * Copyright (C) Intel Corporation, 2005
21 *
22 * 2005-Apr Rusty Lynch <rusty.lynch@intel.com> and Anil S Keshavamurthy
23 * <anil.s.keshavamurthy@intel.com> adapted from i386
24 */
25
26#include <linux/config.h>
27#include <linux/kprobes.h>
28#include <linux/ptrace.h>
29#include <linux/spinlock.h>
30#include <linux/string.h>
31#include <linux/slab.h>
32#include <linux/preempt.h>
33#include <linux/moduleloader.h>
34
35#include <asm/pgtable.h>
36#include <asm/kdebug.h>
37
Anil S Keshavamurthyb2761dc2005-06-23 00:09:28 -070038extern void jprobe_inst_return(void);
39
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -070040/* kprobe_status settings */
41#define KPROBE_HIT_ACTIVE 0x00000001
42#define KPROBE_HIT_SS 0x00000002
43
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -070044static struct kprobe *current_kprobe, *kprobe_prev;
45static unsigned long kprobe_status, kprobe_status_prev;
Anil S Keshavamurthyb2761dc2005-06-23 00:09:28 -070046static struct pt_regs jprobe_saved_regs;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -070047
48enum instruction_type {A, I, M, F, B, L, X, u};
49static enum instruction_type bundle_encoding[32][3] = {
50 { M, I, I }, /* 00 */
51 { M, I, I }, /* 01 */
52 { M, I, I }, /* 02 */
53 { M, I, I }, /* 03 */
54 { M, L, X }, /* 04 */
55 { M, L, X }, /* 05 */
56 { u, u, u }, /* 06 */
57 { u, u, u }, /* 07 */
58 { M, M, I }, /* 08 */
59 { M, M, I }, /* 09 */
60 { M, M, I }, /* 0A */
61 { M, M, I }, /* 0B */
62 { M, F, I }, /* 0C */
63 { M, F, I }, /* 0D */
64 { M, M, F }, /* 0E */
65 { M, M, F }, /* 0F */
66 { M, I, B }, /* 10 */
67 { M, I, B }, /* 11 */
68 { M, B, B }, /* 12 */
69 { M, B, B }, /* 13 */
70 { u, u, u }, /* 14 */
71 { u, u, u }, /* 15 */
72 { B, B, B }, /* 16 */
73 { B, B, B }, /* 17 */
74 { M, M, B }, /* 18 */
75 { M, M, B }, /* 19 */
76 { u, u, u }, /* 1A */
77 { u, u, u }, /* 1B */
78 { M, F, B }, /* 1C */
79 { M, F, B }, /* 1D */
80 { u, u, u }, /* 1E */
81 { u, u, u }, /* 1F */
82};
83
Anil S Keshavamurthya5403182005-06-23 00:09:32 -070084/*
85 * In this function we check to see if the instruction
86 * is IP relative instruction and update the kprobe
87 * inst flag accordingly
88 */
89static void update_kprobe_inst_flag(uint template, uint slot, uint major_opcode,
90 unsigned long kprobe_inst, struct kprobe *p)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -070091{
Rusty Lynch8bc76772005-06-23 00:09:30 -070092 p->ainsn.inst_flag = 0;
93 p->ainsn.target_br_reg = 0;
94
Anil S Keshavamurthya5403182005-06-23 00:09:32 -070095 if (bundle_encoding[template][slot] == B) {
96 switch (major_opcode) {
97 case INDIRECT_CALL_OPCODE:
98 p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
99 p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
100 break;
101 case IP_RELATIVE_PREDICT_OPCODE:
102 case IP_RELATIVE_BRANCH_OPCODE:
103 p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;
104 break;
105 case IP_RELATIVE_CALL_OPCODE:
106 p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;
107 p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
108 p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
109 break;
110 }
111 } else if (bundle_encoding[template][slot] == X) {
112 switch (major_opcode) {
113 case LONG_CALL_OPCODE:
114 p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
115 p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
116 break;
117 }
118 }
119 return;
120}
Rusty Lynch8bc76772005-06-23 00:09:30 -0700121
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700122/*
Anil S Keshavamurthy1674eaf2005-06-23 00:09:33 -0700123 * In this function we check to see if the instruction
Anil S Keshavamurthy708de8f2005-06-23 00:09:34 -0700124 * on which we are inserting kprobe is supported.
125 * Returns 0 if supported
126 * Returns -EINVAL if unsupported
127 */
128static int unsupported_inst(uint template, uint slot, uint major_opcode,
129 unsigned long kprobe_inst, struct kprobe *p)
130{
131 unsigned long addr = (unsigned long)p->addr;
132
133 if (bundle_encoding[template][slot] == I) {
134 switch (major_opcode) {
135 case 0x0: //I_UNIT_MISC_OPCODE:
136 /*
137 * Check for Integer speculation instruction
138 * - Bit 33-35 to be equal to 0x1
139 */
140 if (((kprobe_inst >> 33) & 0x7) == 1) {
141 printk(KERN_WARNING
142 "Kprobes on speculation inst at <0x%lx> not supported\n",
143 addr);
144 return -EINVAL;
145 }
146
147 /*
148 * IP relative mov instruction
149 * - Bit 27-35 to be equal to 0x30
150 */
151 if (((kprobe_inst >> 27) & 0x1FF) == 0x30) {
152 printk(KERN_WARNING
153 "Kprobes on \"mov r1=ip\" at <0x%lx> not supported\n",
154 addr);
155 return -EINVAL;
156
157 }
158 }
159 }
160 return 0;
161}
162
163
164/*
165 * In this function we check to see if the instruction
Anil S Keshavamurthy1674eaf2005-06-23 00:09:33 -0700166 * (qp) cmpx.crel.ctype p1,p2=r2,r3
167 * on which we are inserting kprobe is cmp instruction
168 * with ctype as unc.
169 */
170static uint is_cmp_ctype_unc_inst(uint template, uint slot, uint major_opcode,
171unsigned long kprobe_inst)
172{
173 cmp_inst_t cmp_inst;
174 uint ctype_unc = 0;
175
176 if (!((bundle_encoding[template][slot] == I) ||
177 (bundle_encoding[template][slot] == M)))
178 goto out;
179
180 if (!((major_opcode == 0xC) || (major_opcode == 0xD) ||
181 (major_opcode == 0xE)))
182 goto out;
183
184 cmp_inst.l = kprobe_inst;
185 if ((cmp_inst.f.x2 == 0) || (cmp_inst.f.x2 == 1)) {
186 /* Integere compare - Register Register (A6 type)*/
187 if ((cmp_inst.f.tb == 0) && (cmp_inst.f.ta == 0)
188 &&(cmp_inst.f.c == 1))
189 ctype_unc = 1;
190 } else if ((cmp_inst.f.x2 == 2)||(cmp_inst.f.x2 == 3)) {
191 /* Integere compare - Immediate Register (A8 type)*/
192 if ((cmp_inst.f.ta == 0) &&(cmp_inst.f.c == 1))
193 ctype_unc = 1;
194 }
195out:
196 return ctype_unc;
197}
198
199/*
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700200 * In this function we override the bundle with
201 * the break instruction at the given slot.
202 */
203static void prepare_break_inst(uint template, uint slot, uint major_opcode,
204 unsigned long kprobe_inst, struct kprobe *p)
205{
206 unsigned long break_inst = BREAK_INST;
207 bundle_t *bundle = &p->ainsn.insn.bundle;
208
209 /*
210 * Copy the original kprobe_inst qualifying predicate(qp)
Anil S Keshavamurthy1674eaf2005-06-23 00:09:33 -0700211 * to the break instruction iff !is_cmp_ctype_unc_inst
212 * because for cmp instruction with ctype equal to unc,
213 * which is a special instruction always needs to be
214 * executed regradless of qp
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700215 */
Anil S Keshavamurthy1674eaf2005-06-23 00:09:33 -0700216 if (!is_cmp_ctype_unc_inst(template, slot, major_opcode, kprobe_inst))
217 break_inst |= (0x3f & kprobe_inst);
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700218
219 switch (slot) {
220 case 0:
221 bundle->quad0.slot0 = break_inst;
222 break;
223 case 1:
224 bundle->quad0.slot1_p0 = break_inst;
225 bundle->quad1.slot1_p1 = break_inst >> (64-46);
226 break;
227 case 2:
228 bundle->quad1.slot2 = break_inst;
229 break;
230 }
231
232 /*
233 * Update the instruction flag, so that we can
234 * emulate the instruction properly after we
235 * single step on original instruction
236 */
237 update_kprobe_inst_flag(template, slot, major_opcode, kprobe_inst, p);
238}
239
240static inline void get_kprobe_inst(bundle_t *bundle, uint slot,
241 unsigned long *kprobe_inst, uint *major_opcode)
242{
243 unsigned long kprobe_inst_p0, kprobe_inst_p1;
244 unsigned int template;
245
246 template = bundle->quad0.template;
247
248 switch (slot) {
249 case 0:
250 *major_opcode = (bundle->quad0.slot0 >> SLOT0_OPCODE_SHIFT);
251 *kprobe_inst = bundle->quad0.slot0;
252 break;
253 case 1:
254 *major_opcode = (bundle->quad1.slot1_p1 >> SLOT1_p1_OPCODE_SHIFT);
255 kprobe_inst_p0 = bundle->quad0.slot1_p0;
256 kprobe_inst_p1 = bundle->quad1.slot1_p1;
257 *kprobe_inst = kprobe_inst_p0 | (kprobe_inst_p1 << (64-46));
258 break;
259 case 2:
260 *major_opcode = (bundle->quad1.slot2 >> SLOT2_OPCODE_SHIFT);
261 *kprobe_inst = bundle->quad1.slot2;
262 break;
263 }
264}
265
266static int valid_kprobe_addr(int template, int slot, unsigned long addr)
267{
268 if ((slot > 2) || ((bundle_encoding[template][1] == L) && slot > 1)) {
Rusty Lynch8bc76772005-06-23 00:09:30 -0700269 printk(KERN_WARNING "Attempting to insert unaligned kprobe at 0x%lx\n",
270 addr);
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700271 return -EINVAL;
272 }
Rusty Lyncha528e212005-06-27 15:17:15 -0700273
274 if (slot == 1 && bundle_encoding[template][1] != L) {
275 printk(KERN_WARNING "Inserting kprobes on slot #1 "
276 "is not supported\n");
277 return -EINVAL;
278 }
279
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700280 return 0;
281}
Rusty Lynch8bc76772005-06-23 00:09:30 -0700282
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700283static inline void save_previous_kprobe(void)
284{
285 kprobe_prev = current_kprobe;
286 kprobe_status_prev = kprobe_status;
287}
288
289static inline void restore_previous_kprobe(void)
290{
291 current_kprobe = kprobe_prev;
292 kprobe_status = kprobe_status_prev;
293}
294
295static inline void set_current_kprobe(struct kprobe *p)
296{
297 current_kprobe = p;
298}
299
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700300static void kretprobe_trampoline(void)
301{
302}
303
304/*
305 * At this point the target function has been tricked into
306 * returning into our trampoline. Lookup the associated instance
307 * and then:
308 * - call the handler function
309 * - cleanup by marking the instance as unused
310 * - long jump back to the original return address
311 */
312int trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
313{
314 struct kretprobe_instance *ri = NULL;
315 struct hlist_head *head;
316 struct hlist_node *node, *tmp;
317 unsigned long orig_ret_address = 0;
318 unsigned long trampoline_address =
319 ((struct fnptr *)kretprobe_trampoline)->ip;
320
321 head = kretprobe_inst_table_head(current);
322
323 /*
324 * It is possible to have multiple instances associated with a given
325 * task either because an multiple functions in the call path
326 * have a return probe installed on them, and/or more then one return
327 * return probe was registered for a target function.
328 *
329 * We can handle this because:
330 * - instances are always inserted at the head of the list
331 * - when multiple return probes are registered for the same
332 * function, the first instance's ret_addr will point to the
333 * real return address, and all the rest will point to
334 * kretprobe_trampoline
335 */
336 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
337 if (ri->task != current)
338 /* another task is sharing our hash bucket */
339 continue;
340
341 if (ri->rp && ri->rp->handler)
342 ri->rp->handler(ri, regs);
343
344 orig_ret_address = (unsigned long)ri->ret_addr;
345 recycle_rp_inst(ri);
346
347 if (orig_ret_address != trampoline_address)
348 /*
349 * This is the real return address. Any other
350 * instances associated with this task are for
351 * other calls deeper on the call stack
352 */
353 break;
354 }
355
356 BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
357 regs->cr_iip = orig_ret_address;
358
359 unlock_kprobes();
360 preempt_enable_no_resched();
361
362 /*
363 * By returning a non-zero value, we are telling
364 * kprobe_handler() that we have handled unlocking
365 * and re-enabling preemption.
366 */
367 return 1;
368}
369
370void arch_prepare_kretprobe(struct kretprobe *rp, struct pt_regs *regs)
371{
372 struct kretprobe_instance *ri;
373
374 if ((ri = get_free_rp_inst(rp)) != NULL) {
375 ri->rp = rp;
376 ri->task = current;
377 ri->ret_addr = (kprobe_opcode_t *)regs->b0;
378
379 /* Replace the return addr with trampoline addr */
380 regs->b0 = ((struct fnptr *)kretprobe_trampoline)->ip;
381
382 add_rp_inst(ri);
383 } else {
384 rp->nmissed++;
385 }
386}
387
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700388int arch_prepare_kprobe(struct kprobe *p)
389{
390 unsigned long addr = (unsigned long) p->addr;
391 unsigned long *kprobe_addr = (unsigned long *)(addr & ~0xFULL);
392 unsigned long kprobe_inst=0;
393 unsigned int slot = addr & 0xf, template, major_opcode = 0;
394 bundle_t *bundle = &p->ainsn.insn.bundle;
Rusty Lynch8bc76772005-06-23 00:09:30 -0700395
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700396 memcpy(&p->opcode.bundle, kprobe_addr, sizeof(bundle_t));
397 memcpy(&p->ainsn.insn.bundle, kprobe_addr, sizeof(bundle_t));
Rusty Lynch8bc76772005-06-23 00:09:30 -0700398
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700399 template = bundle->quad0.template;
400
401 if(valid_kprobe_addr(template, slot, addr))
402 return -EINVAL;
403
404 /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
405 if (slot == 1 && bundle_encoding[template][1] == L)
406 slot++;
407
408 /* Get kprobe_inst and major_opcode from the bundle */
409 get_kprobe_inst(bundle, slot, &kprobe_inst, &major_opcode);
410
Anil S Keshavamurthy708de8f2005-06-23 00:09:34 -0700411 if (unsupported_inst(template, slot, major_opcode, kprobe_inst, p))
412 return -EINVAL;
413
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700414 prepare_break_inst(template, slot, major_opcode, kprobe_inst, p);
Rusty Lynch8bc76772005-06-23 00:09:30 -0700415
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700416 return 0;
417}
418
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700419void arch_arm_kprobe(struct kprobe *p)
420{
421 unsigned long addr = (unsigned long)p->addr;
422 unsigned long arm_addr = addr & ~0xFULL;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700423
Rusty Lynch8bc76772005-06-23 00:09:30 -0700424 memcpy((char *)arm_addr, &p->ainsn.insn.bundle, sizeof(bundle_t));
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700425 flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t));
426}
427
428void arch_disarm_kprobe(struct kprobe *p)
429{
430 unsigned long addr = (unsigned long)p->addr;
431 unsigned long arm_addr = addr & ~0xFULL;
432
433 /* p->opcode contains the original unaltered bundle */
434 memcpy((char *) arm_addr, (char *) &p->opcode.bundle, sizeof(bundle_t));
435 flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t));
436}
437
438void arch_remove_kprobe(struct kprobe *p)
439{
440}
441
442/*
443 * We are resuming execution after a single step fault, so the pt_regs
444 * structure reflects the register state after we executed the instruction
445 * located in the kprobe (p->ainsn.insn.bundle). We still need to adjust
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700446 * the ip to point back to the original stack address. To set the IP address
447 * to original stack address, handle the case where we need to fixup the
448 * relative IP address and/or fixup branch register.
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700449 */
450static void resume_execution(struct kprobe *p, struct pt_regs *regs)
451{
Rusty Lynch8bc76772005-06-23 00:09:30 -0700452 unsigned long bundle_addr = ((unsigned long) (&p->opcode.bundle)) & ~0xFULL;
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700453 unsigned long resume_addr = (unsigned long)p->addr & ~0xFULL;
454 unsigned long template;
455 int slot = ((unsigned long)p->addr & 0xf);
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700456
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700457 template = p->opcode.bundle.quad0.template;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700458
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700459 if (slot == 1 && bundle_encoding[template][1] == L)
460 slot = 2;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700461
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700462 if (p->ainsn.inst_flag) {
463
464 if (p->ainsn.inst_flag & INST_FLAG_FIX_RELATIVE_IP_ADDR) {
465 /* Fix relative IP address */
466 regs->cr_iip = (regs->cr_iip - bundle_addr) + resume_addr;
467 }
468
469 if (p->ainsn.inst_flag & INST_FLAG_FIX_BRANCH_REG) {
470 /*
471 * Fix target branch register, software convention is
472 * to use either b0 or b6 or b7, so just checking
473 * only those registers
474 */
475 switch (p->ainsn.target_br_reg) {
476 case 0:
477 if ((regs->b0 == bundle_addr) ||
478 (regs->b0 == bundle_addr + 0x10)) {
479 regs->b0 = (regs->b0 - bundle_addr) +
480 resume_addr;
481 }
482 break;
483 case 6:
484 if ((regs->b6 == bundle_addr) ||
485 (regs->b6 == bundle_addr + 0x10)) {
486 regs->b6 = (regs->b6 - bundle_addr) +
487 resume_addr;
488 }
489 break;
490 case 7:
491 if ((regs->b7 == bundle_addr) ||
492 (regs->b7 == bundle_addr + 0x10)) {
493 regs->b7 = (regs->b7 - bundle_addr) +
494 resume_addr;
495 }
496 break;
497 } /* end switch */
498 }
499 goto turn_ss_off;
500 }
501
502 if (slot == 2) {
503 if (regs->cr_iip == bundle_addr + 0x10) {
504 regs->cr_iip = resume_addr + 0x10;
505 }
506 } else {
507 if (regs->cr_iip == bundle_addr) {
508 regs->cr_iip = resume_addr;
509 }
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700510 }
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700511
512turn_ss_off:
513 /* Turn off Single Step bit */
514 ia64_psr(regs)->ss = 0;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700515}
516
517static void prepare_ss(struct kprobe *p, struct pt_regs *regs)
518{
Rusty Lynch8bc76772005-06-23 00:09:30 -0700519 unsigned long bundle_addr = (unsigned long) &p->opcode.bundle;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700520 unsigned long slot = (unsigned long)p->addr & 0xf;
521
522 /* Update instruction pointer (IIP) and slot number (IPSR.ri) */
523 regs->cr_iip = bundle_addr & ~0xFULL;
524
525 if (slot > 2)
526 slot = 0;
527
528 ia64_psr(regs)->ri = slot;
529
530 /* turn on single stepping */
531 ia64_psr(regs)->ss = 1;
532}
533
Keshavamurthy Anil S89cb14c2005-06-23 00:09:35 -0700534static int pre_kprobes_handler(struct die_args *args)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700535{
536 struct kprobe *p;
537 int ret = 0;
Keshavamurthy Anil S89cb14c2005-06-23 00:09:35 -0700538 struct pt_regs *regs = args->regs;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700539 kprobe_opcode_t *addr = (kprobe_opcode_t *)instruction_pointer(regs);
540
541 preempt_disable();
542
543 /* Handle recursion cases */
544 if (kprobe_running()) {
545 p = get_kprobe(addr);
546 if (p) {
547 if (kprobe_status == KPROBE_HIT_SS) {
548 unlock_kprobes();
549 goto no_kprobe;
550 }
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700551 /* We have reentered the pre_kprobe_handler(), since
552 * another probe was hit while within the handler.
553 * We here save the original kprobes variables and
554 * just single step on the instruction of the new probe
555 * without calling any user handlers.
556 */
557 save_previous_kprobe();
558 set_current_kprobe(p);
559 p->nmissed++;
560 prepare_ss(p, regs);
561 kprobe_status = KPROBE_REENTER;
562 return 1;
Keshavamurthy Anil S89cb14c2005-06-23 00:09:35 -0700563 } else if (args->err == __IA64_BREAK_JPROBE) {
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700564 /*
565 * jprobe instrumented function just completed
566 */
567 p = current_kprobe;
568 if (p->break_handler && p->break_handler(p, regs)) {
569 goto ss_probe;
570 }
Keshavamurthy Anil S89cb14c2005-06-23 00:09:35 -0700571 } else {
572 /* Not our break */
573 goto no_kprobe;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700574 }
575 }
576
577 lock_kprobes();
578 p = get_kprobe(addr);
579 if (!p) {
580 unlock_kprobes();
581 goto no_kprobe;
582 }
583
584 kprobe_status = KPROBE_HIT_ACTIVE;
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700585 set_current_kprobe(p);
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700586
587 if (p->pre_handler && p->pre_handler(p, regs))
588 /*
589 * Our pre-handler is specifically requesting that we just
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700590 * do a return. This is used for both the jprobe pre-handler
591 * and the kretprobe trampoline
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700592 */
593 return 1;
594
595ss_probe:
596 prepare_ss(p, regs);
597 kprobe_status = KPROBE_HIT_SS;
598 return 1;
599
600no_kprobe:
601 preempt_enable_no_resched();
602 return ret;
603}
604
605static int post_kprobes_handler(struct pt_regs *regs)
606{
607 if (!kprobe_running())
608 return 0;
609
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700610 if ((kprobe_status != KPROBE_REENTER) && current_kprobe->post_handler) {
611 kprobe_status = KPROBE_HIT_SSDONE;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700612 current_kprobe->post_handler(current_kprobe, regs, 0);
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700613 }
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700614
615 resume_execution(current_kprobe, regs);
616
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700617 /*Restore back the original saved kprobes variables and continue. */
618 if (kprobe_status == KPROBE_REENTER) {
619 restore_previous_kprobe();
620 goto out;
621 }
622
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700623 unlock_kprobes();
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700624
625out:
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700626 preempt_enable_no_resched();
627 return 1;
628}
629
630static int kprobes_fault_handler(struct pt_regs *regs, int trapnr)
631{
632 if (!kprobe_running())
633 return 0;
634
635 if (current_kprobe->fault_handler &&
636 current_kprobe->fault_handler(current_kprobe, regs, trapnr))
637 return 1;
638
639 if (kprobe_status & KPROBE_HIT_SS) {
640 resume_execution(current_kprobe, regs);
641 unlock_kprobes();
642 preempt_enable_no_resched();
643 }
644
645 return 0;
646}
647
648int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
649 void *data)
650{
651 struct die_args *args = (struct die_args *)data;
652 switch(val) {
653 case DIE_BREAK:
Keshavamurthy Anil S89cb14c2005-06-23 00:09:35 -0700654 if (pre_kprobes_handler(args))
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700655 return NOTIFY_STOP;
656 break;
657 case DIE_SS:
658 if (post_kprobes_handler(args->regs))
659 return NOTIFY_STOP;
660 break;
661 case DIE_PAGE_FAULT:
662 if (kprobes_fault_handler(args->regs, args->trapnr))
663 return NOTIFY_STOP;
664 default:
665 break;
666 }
667 return NOTIFY_DONE;
668}
669
670int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
671{
Anil S Keshavamurthyb2761dc2005-06-23 00:09:28 -0700672 struct jprobe *jp = container_of(p, struct jprobe, kp);
673 unsigned long addr = ((struct fnptr *)(jp->entry))->ip;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700674
Anil S Keshavamurthyb2761dc2005-06-23 00:09:28 -0700675 /* save architectural state */
676 jprobe_saved_regs = *regs;
677
678 /* after rfi, execute the jprobe instrumented function */
679 regs->cr_iip = addr & ~0xFULL;
680 ia64_psr(regs)->ri = addr & 0xf;
681 regs->r1 = ((struct fnptr *)(jp->entry))->gp;
682
683 /*
684 * fix the return address to our jprobe_inst_return() function
685 * in the jprobes.S file
686 */
687 regs->b0 = ((struct fnptr *)(jprobe_inst_return))->ip;
688
689 return 1;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700690}
691
692int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
693{
Anil S Keshavamurthyb2761dc2005-06-23 00:09:28 -0700694 *regs = jprobe_saved_regs;
695 return 1;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700696}
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700697
698static struct kprobe trampoline_p = {
699 .pre_handler = trampoline_probe_handler
700};
701
702int __init arch_init(void)
703{
704 trampoline_p.addr =
705 (kprobe_opcode_t *)((struct fnptr *)kretprobe_trampoline)->ip;
706 return register_kprobe(&trampoline_p);
707}