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