]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/ia64/kernel/kprobes.c
Merge branch 'master' into gfs2
[linux-2.6-omap-h63xx.git] / arch / ia64 / kernel / kprobes.c
1 /*
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/kprobes.h>
27 #include <linux/ptrace.h>
28 #include <linux/string.h>
29 #include <linux/slab.h>
30 #include <linux/preempt.h>
31 #include <linux/moduleloader.h>
32
33 #include <asm/pgtable.h>
34 #include <asm/kdebug.h>
35 #include <asm/sections.h>
36 #include <asm/uaccess.h>
37
38 extern void jprobe_inst_return(void);
39
40 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
41 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
42
43 enum instruction_type {A, I, M, F, B, L, X, u};
44 static enum instruction_type bundle_encoding[32][3] = {
45   { M, I, I },                          /* 00 */
46   { M, I, I },                          /* 01 */
47   { M, I, I },                          /* 02 */
48   { M, I, I },                          /* 03 */
49   { M, L, X },                          /* 04 */
50   { M, L, X },                          /* 05 */
51   { u, u, u },                          /* 06 */
52   { u, u, u },                          /* 07 */
53   { M, M, I },                          /* 08 */
54   { M, M, I },                          /* 09 */
55   { M, M, I },                          /* 0A */
56   { M, M, I },                          /* 0B */
57   { M, F, I },                          /* 0C */
58   { M, F, I },                          /* 0D */
59   { M, M, F },                          /* 0E */
60   { M, M, F },                          /* 0F */
61   { M, I, B },                          /* 10 */
62   { M, I, B },                          /* 11 */
63   { M, B, B },                          /* 12 */
64   { M, B, B },                          /* 13 */
65   { u, u, u },                          /* 14 */
66   { u, u, u },                          /* 15 */
67   { B, B, B },                          /* 16 */
68   { B, B, B },                          /* 17 */
69   { M, M, B },                          /* 18 */
70   { M, M, B },                          /* 19 */
71   { u, u, u },                          /* 1A */
72   { u, u, u },                          /* 1B */
73   { M, F, B },                          /* 1C */
74   { M, F, B },                          /* 1D */
75   { u, u, u },                          /* 1E */
76   { u, u, u },                          /* 1F */
77 };
78
79 /*
80  * In this function we check to see if the instruction
81  * is IP relative instruction and update the kprobe
82  * inst flag accordingly
83  */
84 static void __kprobes update_kprobe_inst_flag(uint template, uint  slot,
85                                               uint major_opcode,
86                                               unsigned long kprobe_inst,
87                                               struct kprobe *p)
88 {
89         p->ainsn.inst_flag = 0;
90         p->ainsn.target_br_reg = 0;
91
92         /* Check for Break instruction
93          * Bits 37:40 Major opcode to be zero
94          * Bits 27:32 X6 to be zero
95          * Bits 32:35 X3 to be zero
96          */
97         if ((!major_opcode) && (!((kprobe_inst >> 27) & 0x1FF)) ) {
98                 /* is a break instruction */
99                 p->ainsn.inst_flag |= INST_FLAG_BREAK_INST;
100                 return;
101         }
102
103         if (bundle_encoding[template][slot] == B) {
104                 switch (major_opcode) {
105                   case INDIRECT_CALL_OPCODE:
106                         p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
107                         p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
108                         break;
109                   case IP_RELATIVE_PREDICT_OPCODE:
110                   case IP_RELATIVE_BRANCH_OPCODE:
111                         p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;
112                         break;
113                   case IP_RELATIVE_CALL_OPCODE:
114                         p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;
115                         p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
116                         p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
117                         break;
118                 }
119         } else if (bundle_encoding[template][slot] == X) {
120                 switch (major_opcode) {
121                   case LONG_CALL_OPCODE:
122                         p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
123                         p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
124                   break;
125                 }
126         }
127         return;
128 }
129
130 /*
131  * In this function we check to see if the instruction
132  * on which we are inserting kprobe is supported.
133  * Returns 0 if supported
134  * Returns -EINVAL if unsupported
135  */
136 static int __kprobes unsupported_inst(uint template, uint  slot,
137                                       uint major_opcode,
138                                       unsigned long kprobe_inst,
139                                       unsigned long addr)
140 {
141         if (bundle_encoding[template][slot] == I) {
142                 switch (major_opcode) {
143                         case 0x0: //I_UNIT_MISC_OPCODE:
144                         /*
145                          * Check for Integer speculation instruction
146                          * - Bit 33-35 to be equal to 0x1
147                          */
148                         if (((kprobe_inst >> 33) & 0x7) == 1) {
149                                 printk(KERN_WARNING
150                                         "Kprobes on speculation inst at <0x%lx> not supported\n",
151                                         addr);
152                                 return -EINVAL;
153                         }
154
155                         /*
156                          * IP relative mov instruction
157                          *  - Bit 27-35 to be equal to 0x30
158                          */
159                         if (((kprobe_inst >> 27) & 0x1FF) == 0x30) {
160                                 printk(KERN_WARNING
161                                         "Kprobes on \"mov r1=ip\" at <0x%lx> not supported\n",
162                                         addr);
163                                 return -EINVAL;
164
165                         }
166                 }
167         }
168         return 0;
169 }
170
171
172 /*
173  * In this function we check to see if the instruction
174  * (qp) cmpx.crel.ctype p1,p2=r2,r3
175  * on which we are inserting kprobe is cmp instruction
176  * with ctype as unc.
177  */
178 static uint __kprobes is_cmp_ctype_unc_inst(uint template, uint slot,
179                                             uint major_opcode,
180                                             unsigned long kprobe_inst)
181 {
182         cmp_inst_t cmp_inst;
183         uint ctype_unc = 0;
184
185         if (!((bundle_encoding[template][slot] == I) ||
186                 (bundle_encoding[template][slot] == M)))
187                 goto out;
188
189         if (!((major_opcode == 0xC) || (major_opcode == 0xD) ||
190                 (major_opcode == 0xE)))
191                 goto out;
192
193         cmp_inst.l = kprobe_inst;
194         if ((cmp_inst.f.x2 == 0) || (cmp_inst.f.x2 == 1)) {
195                 /* Integere compare - Register Register (A6 type)*/
196                 if ((cmp_inst.f.tb == 0) && (cmp_inst.f.ta == 0)
197                                 &&(cmp_inst.f.c == 1))
198                         ctype_unc = 1;
199         } else if ((cmp_inst.f.x2 == 2)||(cmp_inst.f.x2 == 3)) {
200                 /* Integere compare - Immediate Register (A8 type)*/
201                 if ((cmp_inst.f.ta == 0) &&(cmp_inst.f.c == 1))
202                         ctype_unc = 1;
203         }
204 out:
205         return ctype_unc;
206 }
207
208 /*
209  * In this function we override the bundle with
210  * the break instruction at the given slot.
211  */
212 static void __kprobes prepare_break_inst(uint template, uint  slot,
213                                          uint major_opcode,
214                                          unsigned long kprobe_inst,
215                                          struct kprobe *p)
216 {
217         unsigned long break_inst = BREAK_INST;
218         bundle_t *bundle = &p->opcode.bundle;
219
220         /*
221          * Copy the original kprobe_inst qualifying predicate(qp)
222          * to the break instruction iff !is_cmp_ctype_unc_inst
223          * because for cmp instruction with ctype equal to unc,
224          * which is a special instruction always needs to be
225          * executed regradless of qp
226          */
227         if (!is_cmp_ctype_unc_inst(template, slot, major_opcode, kprobe_inst))
228                 break_inst |= (0x3f & kprobe_inst);
229
230         switch (slot) {
231           case 0:
232                 bundle->quad0.slot0 = break_inst;
233                 break;
234           case 1:
235                 bundle->quad0.slot1_p0 = break_inst;
236                 bundle->quad1.slot1_p1 = break_inst >> (64-46);
237                 break;
238           case 2:
239                 bundle->quad1.slot2 = break_inst;
240                 break;
241         }
242
243         /*
244          * Update the instruction flag, so that we can
245          * emulate the instruction properly after we
246          * single step on original instruction
247          */
248         update_kprobe_inst_flag(template, slot, major_opcode, kprobe_inst, p);
249 }
250
251 static void __kprobes get_kprobe_inst(bundle_t *bundle, uint slot,
252                 unsigned long *kprobe_inst, uint *major_opcode)
253 {
254         unsigned long kprobe_inst_p0, kprobe_inst_p1;
255         unsigned int template;
256
257         template = bundle->quad0.template;
258
259         switch (slot) {
260           case 0:
261                 *major_opcode = (bundle->quad0.slot0 >> SLOT0_OPCODE_SHIFT);
262                 *kprobe_inst = bundle->quad0.slot0;
263                 break;
264           case 1:
265                 *major_opcode = (bundle->quad1.slot1_p1 >> SLOT1_p1_OPCODE_SHIFT);
266                 kprobe_inst_p0 = bundle->quad0.slot1_p0;
267                 kprobe_inst_p1 = bundle->quad1.slot1_p1;
268                 *kprobe_inst = kprobe_inst_p0 | (kprobe_inst_p1 << (64-46));
269                 break;
270           case 2:
271                 *major_opcode = (bundle->quad1.slot2 >> SLOT2_OPCODE_SHIFT);
272                 *kprobe_inst = bundle->quad1.slot2;
273                 break;
274         }
275 }
276
277 /* Returns non-zero if the addr is in the Interrupt Vector Table */
278 static int __kprobes in_ivt_functions(unsigned long addr)
279 {
280         return (addr >= (unsigned long)__start_ivt_text
281                 && addr < (unsigned long)__end_ivt_text);
282 }
283
284 static int __kprobes valid_kprobe_addr(int template, int slot,
285                                        unsigned long addr)
286 {
287         if ((slot > 2) || ((bundle_encoding[template][1] == L) && slot > 1)) {
288                 printk(KERN_WARNING "Attempting to insert unaligned kprobe "
289                                 "at 0x%lx\n", addr);
290                 return -EINVAL;
291         }
292
293         if (in_ivt_functions(addr)) {
294                 printk(KERN_WARNING "Kprobes can't be inserted inside "
295                                 "IVT functions at 0x%lx\n", addr);
296                 return -EINVAL;
297         }
298
299         if (slot == 1 && bundle_encoding[template][1] != L) {
300                 printk(KERN_WARNING "Inserting kprobes on slot #1 "
301                        "is not supported\n");
302                 return -EINVAL;
303         }
304
305         return 0;
306 }
307
308 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
309 {
310         kcb->prev_kprobe.kp = kprobe_running();
311         kcb->prev_kprobe.status = kcb->kprobe_status;
312 }
313
314 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
315 {
316         __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
317         kcb->kprobe_status = kcb->prev_kprobe.status;
318 }
319
320 static void __kprobes set_current_kprobe(struct kprobe *p,
321                         struct kprobe_ctlblk *kcb)
322 {
323         __get_cpu_var(current_kprobe) = p;
324 }
325
326 static void kretprobe_trampoline(void)
327 {
328 }
329
330 /*
331  * At this point the target function has been tricked into
332  * returning into our trampoline.  Lookup the associated instance
333  * and then:
334  *    - call the handler function
335  *    - cleanup by marking the instance as unused
336  *    - long jump back to the original return address
337  */
338 int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
339 {
340         struct kretprobe_instance *ri = NULL;
341         struct hlist_head *head;
342         struct hlist_node *node, *tmp;
343         unsigned long flags, orig_ret_address = 0;
344         unsigned long trampoline_address =
345                 ((struct fnptr *)kretprobe_trampoline)->ip;
346
347         spin_lock_irqsave(&kretprobe_lock, flags);
348         head = kretprobe_inst_table_head(current);
349
350         /*
351          * It is possible to have multiple instances associated with a given
352          * task either because an multiple functions in the call path
353          * have a return probe installed on them, and/or more then one return
354          * return probe was registered for a target function.
355          *
356          * We can handle this because:
357          *     - instances are always inserted at the head of the list
358          *     - when multiple return probes are registered for the same
359          *       function, the first instance's ret_addr will point to the
360          *       real return address, and all the rest will point to
361          *       kretprobe_trampoline
362          */
363         hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
364                 if (ri->task != current)
365                         /* another task is sharing our hash bucket */
366                         continue;
367
368                 if (ri->rp && ri->rp->handler)
369                         ri->rp->handler(ri, regs);
370
371                 orig_ret_address = (unsigned long)ri->ret_addr;
372                 recycle_rp_inst(ri);
373
374                 if (orig_ret_address != trampoline_address)
375                         /*
376                          * This is the real return address. Any other
377                          * instances associated with this task are for
378                          * other calls deeper on the call stack
379                          */
380                         break;
381         }
382
383         BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
384         regs->cr_iip = orig_ret_address;
385
386         reset_current_kprobe();
387         spin_unlock_irqrestore(&kretprobe_lock, flags);
388         preempt_enable_no_resched();
389
390         /*
391          * By returning a non-zero value, we are telling
392          * kprobe_handler() that we don't want the post_handler
393          * to run (and have re-enabled preemption)
394          */
395         return 1;
396 }
397
398 /* Called with kretprobe_lock held */
399 void __kprobes arch_prepare_kretprobe(struct kretprobe *rp,
400                                       struct pt_regs *regs)
401 {
402         struct kretprobe_instance *ri;
403
404         if ((ri = get_free_rp_inst(rp)) != NULL) {
405                 ri->rp = rp;
406                 ri->task = current;
407                 ri->ret_addr = (kprobe_opcode_t *)regs->b0;
408
409                 /* Replace the return addr with trampoline addr */
410                 regs->b0 = ((struct fnptr *)kretprobe_trampoline)->ip;
411
412                 add_rp_inst(ri);
413         } else {
414                 rp->nmissed++;
415         }
416 }
417
418 int __kprobes arch_prepare_kprobe(struct kprobe *p)
419 {
420         unsigned long addr = (unsigned long) p->addr;
421         unsigned long *kprobe_addr = (unsigned long *)(addr & ~0xFULL);
422         unsigned long kprobe_inst=0;
423         unsigned int slot = addr & 0xf, template, major_opcode = 0;
424         bundle_t *bundle;
425
426         bundle = &((kprobe_opcode_t *)kprobe_addr)->bundle;
427         template = bundle->quad0.template;
428
429         if(valid_kprobe_addr(template, slot, addr))
430                 return -EINVAL;
431
432         /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
433         if (slot == 1 && bundle_encoding[template][1] == L)
434                 slot++;
435
436         /* Get kprobe_inst and major_opcode from the bundle */
437         get_kprobe_inst(bundle, slot, &kprobe_inst, &major_opcode);
438
439         if (unsupported_inst(template, slot, major_opcode, kprobe_inst, addr))
440                         return -EINVAL;
441
442
443         p->ainsn.insn = get_insn_slot();
444         if (!p->ainsn.insn)
445                 return -ENOMEM;
446         memcpy(&p->opcode, kprobe_addr, sizeof(kprobe_opcode_t));
447         memcpy(p->ainsn.insn, kprobe_addr, sizeof(kprobe_opcode_t));
448
449         prepare_break_inst(template, slot, major_opcode, kprobe_inst, p);
450
451         return 0;
452 }
453
454 void __kprobes arch_arm_kprobe(struct kprobe *p)
455 {
456         unsigned long addr = (unsigned long)p->addr;
457         unsigned long arm_addr = addr & ~0xFULL;
458
459         flush_icache_range((unsigned long)p->ainsn.insn,
460                         (unsigned long)p->ainsn.insn + sizeof(kprobe_opcode_t));
461         memcpy((char *)arm_addr, &p->opcode, sizeof(kprobe_opcode_t));
462         flush_icache_range(arm_addr, arm_addr + sizeof(kprobe_opcode_t));
463 }
464
465 void __kprobes arch_disarm_kprobe(struct kprobe *p)
466 {
467         unsigned long addr = (unsigned long)p->addr;
468         unsigned long arm_addr = addr & ~0xFULL;
469
470         /* p->ainsn.insn contains the original unaltered kprobe_opcode_t */
471         memcpy((char *) arm_addr, (char *) p->ainsn.insn,
472                                          sizeof(kprobe_opcode_t));
473         flush_icache_range(arm_addr, arm_addr + sizeof(kprobe_opcode_t));
474 }
475
476 void __kprobes arch_remove_kprobe(struct kprobe *p)
477 {
478         mutex_lock(&kprobe_mutex);
479         free_insn_slot(p->ainsn.insn);
480         mutex_unlock(&kprobe_mutex);
481 }
482 /*
483  * We are resuming execution after a single step fault, so the pt_regs
484  * structure reflects the register state after we executed the instruction
485  * located in the kprobe (p->ainsn.insn.bundle).  We still need to adjust
486  * the ip to point back to the original stack address. To set the IP address
487  * to original stack address, handle the case where we need to fixup the
488  * relative IP address and/or fixup branch register.
489  */
490 static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
491 {
492         unsigned long bundle_addr = (unsigned long) (&p->ainsn.insn->bundle);
493         unsigned long resume_addr = (unsigned long)p->addr & ~0xFULL;
494         unsigned long template;
495         int slot = ((unsigned long)p->addr & 0xf);
496
497         template = p->ainsn.insn->bundle.quad0.template;
498
499         if (slot == 1 && bundle_encoding[template][1] == L)
500                 slot = 2;
501
502         if (p->ainsn.inst_flag) {
503
504                 if (p->ainsn.inst_flag & INST_FLAG_FIX_RELATIVE_IP_ADDR) {
505                         /* Fix relative IP address */
506                         regs->cr_iip = (regs->cr_iip - bundle_addr) + resume_addr;
507                 }
508
509                 if (p->ainsn.inst_flag & INST_FLAG_FIX_BRANCH_REG) {
510                 /*
511                  * Fix target branch register, software convention is
512                  * to use either b0 or b6 or b7, so just checking
513                  * only those registers
514                  */
515                         switch (p->ainsn.target_br_reg) {
516                         case 0:
517                                 if ((regs->b0 == bundle_addr) ||
518                                         (regs->b0 == bundle_addr + 0x10)) {
519                                         regs->b0 = (regs->b0 - bundle_addr) +
520                                                 resume_addr;
521                                 }
522                                 break;
523                         case 6:
524                                 if ((regs->b6 == bundle_addr) ||
525                                         (regs->b6 == bundle_addr + 0x10)) {
526                                         regs->b6 = (regs->b6 - bundle_addr) +
527                                                 resume_addr;
528                                 }
529                                 break;
530                         case 7:
531                                 if ((regs->b7 == bundle_addr) ||
532                                         (regs->b7 == bundle_addr + 0x10)) {
533                                         regs->b7 = (regs->b7 - bundle_addr) +
534                                                 resume_addr;
535                                 }
536                                 break;
537                         } /* end switch */
538                 }
539                 goto turn_ss_off;
540         }
541
542         if (slot == 2) {
543                 if (regs->cr_iip == bundle_addr + 0x10) {
544                         regs->cr_iip = resume_addr + 0x10;
545                 }
546         } else {
547                 if (regs->cr_iip == bundle_addr) {
548                         regs->cr_iip = resume_addr;
549                 }
550         }
551
552 turn_ss_off:
553         /* Turn off Single Step bit */
554         ia64_psr(regs)->ss = 0;
555 }
556
557 static void __kprobes prepare_ss(struct kprobe *p, struct pt_regs *regs)
558 {
559         unsigned long bundle_addr = (unsigned long) &p->ainsn.insn->bundle;
560         unsigned long slot = (unsigned long)p->addr & 0xf;
561
562         /* single step inline if break instruction */
563         if (p->ainsn.inst_flag == INST_FLAG_BREAK_INST)
564                 regs->cr_iip = (unsigned long)p->addr & ~0xFULL;
565         else
566                 regs->cr_iip = bundle_addr & ~0xFULL;
567
568         if (slot > 2)
569                 slot = 0;
570
571         ia64_psr(regs)->ri = slot;
572
573         /* turn on single stepping */
574         ia64_psr(regs)->ss = 1;
575 }
576
577 static int __kprobes is_ia64_break_inst(struct pt_regs *regs)
578 {
579         unsigned int slot = ia64_psr(regs)->ri;
580         unsigned int template, major_opcode;
581         unsigned long kprobe_inst;
582         unsigned long *kprobe_addr = (unsigned long *)regs->cr_iip;
583         bundle_t bundle;
584
585         memcpy(&bundle, kprobe_addr, sizeof(bundle_t));
586         template = bundle.quad0.template;
587
588         /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
589         if (slot == 1 && bundle_encoding[template][1] == L)
590                 slot++;
591
592         /* Get Kprobe probe instruction at given slot*/
593         get_kprobe_inst(&bundle, slot, &kprobe_inst, &major_opcode);
594
595         /* For break instruction,
596          * Bits 37:40 Major opcode to be zero
597          * Bits 27:32 X6 to be zero
598          * Bits 32:35 X3 to be zero
599          */
600         if (major_opcode || ((kprobe_inst >> 27) & 0x1FF) ) {
601                 /* Not a break instruction */
602                 return 0;
603         }
604
605         /* Is a break instruction */
606         return 1;
607 }
608
609 static int __kprobes pre_kprobes_handler(struct die_args *args)
610 {
611         struct kprobe *p;
612         int ret = 0;
613         struct pt_regs *regs = args->regs;
614         kprobe_opcode_t *addr = (kprobe_opcode_t *)instruction_pointer(regs);
615         struct kprobe_ctlblk *kcb;
616
617         /*
618          * We don't want to be preempted for the entire
619          * duration of kprobe processing
620          */
621         preempt_disable();
622         kcb = get_kprobe_ctlblk();
623
624         /* Handle recursion cases */
625         if (kprobe_running()) {
626                 p = get_kprobe(addr);
627                 if (p) {
628                         if ((kcb->kprobe_status == KPROBE_HIT_SS) &&
629                              (p->ainsn.inst_flag == INST_FLAG_BREAK_INST)) {
630                                 ia64_psr(regs)->ss = 0;
631                                 goto no_kprobe;
632                         }
633                         /* We have reentered the pre_kprobe_handler(), since
634                          * another probe was hit while within the handler.
635                          * We here save the original kprobes variables and
636                          * just single step on the instruction of the new probe
637                          * without calling any user handlers.
638                          */
639                         save_previous_kprobe(kcb);
640                         set_current_kprobe(p, kcb);
641                         kprobes_inc_nmissed_count(p);
642                         prepare_ss(p, regs);
643                         kcb->kprobe_status = KPROBE_REENTER;
644                         return 1;
645                 } else if (args->err == __IA64_BREAK_JPROBE) {
646                         /*
647                          * jprobe instrumented function just completed
648                          */
649                         p = __get_cpu_var(current_kprobe);
650                         if (p->break_handler && p->break_handler(p, regs)) {
651                                 goto ss_probe;
652                         }
653                 } else if (!is_ia64_break_inst(regs)) {
654                         /* The breakpoint instruction was removed by
655                          * another cpu right after we hit, no further
656                          * handling of this interrupt is appropriate
657                          */
658                         ret = 1;
659                         goto no_kprobe;
660                 } else {
661                         /* Not our break */
662                         goto no_kprobe;
663                 }
664         }
665
666         p = get_kprobe(addr);
667         if (!p) {
668                 if (!is_ia64_break_inst(regs)) {
669                         /*
670                          * The breakpoint instruction was removed right
671                          * after we hit it.  Another cpu has removed
672                          * either a probepoint or a debugger breakpoint
673                          * at this address.  In either case, no further
674                          * handling of this interrupt is appropriate.
675                          */
676                         ret = 1;
677
678                 }
679
680                 /* Not one of our break, let kernel handle it */
681                 goto no_kprobe;
682         }
683
684         set_current_kprobe(p, kcb);
685         kcb->kprobe_status = KPROBE_HIT_ACTIVE;
686
687         if (p->pre_handler && p->pre_handler(p, regs))
688                 /*
689                  * Our pre-handler is specifically requesting that we just
690                  * do a return.  This is used for both the jprobe pre-handler
691                  * and the kretprobe trampoline
692                  */
693                 return 1;
694
695 ss_probe:
696         prepare_ss(p, regs);
697         kcb->kprobe_status = KPROBE_HIT_SS;
698         return 1;
699
700 no_kprobe:
701         preempt_enable_no_resched();
702         return ret;
703 }
704
705 static int __kprobes post_kprobes_handler(struct pt_regs *regs)
706 {
707         struct kprobe *cur = kprobe_running();
708         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
709
710         if (!cur)
711                 return 0;
712
713         if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
714                 kcb->kprobe_status = KPROBE_HIT_SSDONE;
715                 cur->post_handler(cur, regs, 0);
716         }
717
718         resume_execution(cur, regs);
719
720         /*Restore back the original saved kprobes variables and continue. */
721         if (kcb->kprobe_status == KPROBE_REENTER) {
722                 restore_previous_kprobe(kcb);
723                 goto out;
724         }
725         reset_current_kprobe();
726
727 out:
728         preempt_enable_no_resched();
729         return 1;
730 }
731
732 static int __kprobes kprobes_fault_handler(struct pt_regs *regs, int trapnr)
733 {
734         struct kprobe *cur = kprobe_running();
735         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
736
737
738         switch(kcb->kprobe_status) {
739         case KPROBE_HIT_SS:
740         case KPROBE_REENTER:
741                 /*
742                  * We are here because the instruction being single
743                  * stepped caused a page fault. We reset the current
744                  * kprobe and the instruction pointer points back to
745                  * the probe address and allow the page fault handler
746                  * to continue as a normal page fault.
747                  */
748                 regs->cr_iip = ((unsigned long)cur->addr) & ~0xFULL;
749                 ia64_psr(regs)->ri = ((unsigned long)cur->addr) & 0xf;
750                 if (kcb->kprobe_status == KPROBE_REENTER)
751                         restore_previous_kprobe(kcb);
752                 else
753                         reset_current_kprobe();
754                 preempt_enable_no_resched();
755                 break;
756         case KPROBE_HIT_ACTIVE:
757         case KPROBE_HIT_SSDONE:
758                 /*
759                  * We increment the nmissed count for accounting,
760                  * we can also use npre/npostfault count for accouting
761                  * these specific fault cases.
762                  */
763                 kprobes_inc_nmissed_count(cur);
764
765                 /*
766                  * We come here because instructions in the pre/post
767                  * handler caused the page_fault, this could happen
768                  * if handler tries to access user space by
769                  * copy_from_user(), get_user() etc. Let the
770                  * user-specified handler try to fix it first.
771                  */
772                 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
773                         return 1;
774                 /*
775                  * In case the user-specified fault handler returned
776                  * zero, try to fix up.
777                  */
778                 if (ia64_done_with_exception(regs))
779                         return 1;
780
781                 /*
782                  * Let ia64_do_page_fault() fix it.
783                  */
784                 break;
785         default:
786                 break;
787         }
788
789         return 0;
790 }
791
792 int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
793                                        unsigned long val, void *data)
794 {
795         struct die_args *args = (struct die_args *)data;
796         int ret = NOTIFY_DONE;
797
798         if (args->regs && user_mode(args->regs))
799                 return ret;
800
801         switch(val) {
802         case DIE_BREAK:
803                 /* err is break number from ia64_bad_break() */
804                 if (args->err == 0x80200 || args->err == 0x80300 || args->err == 0)
805                         if (pre_kprobes_handler(args))
806                                 ret = NOTIFY_STOP;
807                 break;
808         case DIE_FAULT:
809                 /* err is vector number from ia64_fault() */
810                 if (args->err == 36)
811                         if (post_kprobes_handler(args->regs))
812                                 ret = NOTIFY_STOP;
813                 break;
814         case DIE_PAGE_FAULT:
815                 /* kprobe_running() needs smp_processor_id() */
816                 preempt_disable();
817                 if (kprobe_running() &&
818                         kprobes_fault_handler(args->regs, args->trapnr))
819                         ret = NOTIFY_STOP;
820                 preempt_enable();
821         default:
822                 break;
823         }
824         return ret;
825 }
826
827 struct param_bsp_cfm {
828         unsigned long ip;
829         unsigned long *bsp;
830         unsigned long cfm;
831 };
832
833 static void ia64_get_bsp_cfm(struct unw_frame_info *info, void *arg)
834 {
835         unsigned long ip;
836         struct param_bsp_cfm *lp = arg;
837
838         do {
839                 unw_get_ip(info, &ip);
840                 if (ip == 0)
841                         break;
842                 if (ip == lp->ip) {
843                         unw_get_bsp(info, (unsigned long*)&lp->bsp);
844                         unw_get_cfm(info, (unsigned long*)&lp->cfm);
845                         return;
846                 }
847         } while (unw_unwind(info) >= 0);
848         lp->bsp = 0;
849         lp->cfm = 0;
850         return;
851 }
852
853 int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
854 {
855         struct jprobe *jp = container_of(p, struct jprobe, kp);
856         unsigned long addr = ((struct fnptr *)(jp->entry))->ip;
857         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
858         struct param_bsp_cfm pa;
859         int bytes;
860
861         /*
862          * Callee owns the argument space and could overwrite it, eg
863          * tail call optimization. So to be absolutely safe
864          * we save the argument space before transfering the control
865          * to instrumented jprobe function which runs in
866          * the process context
867          */
868         pa.ip = regs->cr_iip;
869         unw_init_running(ia64_get_bsp_cfm, &pa);
870         bytes = (char *)ia64_rse_skip_regs(pa.bsp, pa.cfm & 0x3f)
871                                 - (char *)pa.bsp;
872         memcpy( kcb->jprobes_saved_stacked_regs,
873                 pa.bsp,
874                 bytes );
875         kcb->bsp = pa.bsp;
876         kcb->cfm = pa.cfm;
877
878         /* save architectural state */
879         kcb->jprobe_saved_regs = *regs;
880
881         /* after rfi, execute the jprobe instrumented function */
882         regs->cr_iip = addr & ~0xFULL;
883         ia64_psr(regs)->ri = addr & 0xf;
884         regs->r1 = ((struct fnptr *)(jp->entry))->gp;
885
886         /*
887          * fix the return address to our jprobe_inst_return() function
888          * in the jprobes.S file
889          */
890         regs->b0 = ((struct fnptr *)(jprobe_inst_return))->ip;
891
892         return 1;
893 }
894
895 int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
896 {
897         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
898         int bytes;
899
900         /* restoring architectural state */
901         *regs = kcb->jprobe_saved_regs;
902
903         /* restoring the original argument space */
904         flush_register_stack();
905         bytes = (char *)ia64_rse_skip_regs(kcb->bsp, kcb->cfm & 0x3f)
906                                 - (char *)kcb->bsp;
907         memcpy( kcb->bsp,
908                 kcb->jprobes_saved_stacked_regs,
909                 bytes );
910         invalidate_stacked_regs();
911
912         preempt_enable_no_resched();
913         return 1;
914 }
915
916 static struct kprobe trampoline_p = {
917         .pre_handler = trampoline_probe_handler
918 };
919
920 int __init arch_init_kprobes(void)
921 {
922         trampoline_p.addr =
923                 (kprobe_opcode_t *)((struct fnptr *)kretprobe_trampoline)->ip;
924         return register_kprobe(&trampoline_p);
925 }