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