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