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