[PATCH] Kprobes/IA64: check jprobe break before handling
[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/config.h>
27 #include <linux/kprobes.h>
28 #include <linux/ptrace.h>
29 #include <linux/spinlock.h>
30 #include <linux/string.h>
31 #include <linux/slab.h>
32 #include <linux/preempt.h>
33 #include <linux/moduleloader.h>
34
35 #include <asm/pgtable.h>
36 #include <asm/kdebug.h>
37
38 extern void jprobe_inst_return(void);
39
40 /* kprobe_status settings */
41 #define KPROBE_HIT_ACTIVE       0x00000001
42 #define KPROBE_HIT_SS           0x00000002
43
44 static struct kprobe *current_kprobe;
45 static unsigned long kprobe_status;
46 static struct pt_regs jprobe_saved_regs;
47
48 enum instruction_type {A, I, M, F, B, L, X, u};
49 static enum instruction_type bundle_encoding[32][3] = {
50   { M, I, I },                          /* 00 */
51   { M, I, I },                          /* 01 */
52   { M, I, I },                          /* 02 */
53   { M, I, I },                          /* 03 */
54   { M, L, X },                          /* 04 */
55   { M, L, X },                          /* 05 */
56   { u, u, u },                          /* 06 */
57   { u, u, u },                          /* 07 */
58   { M, M, I },                          /* 08 */
59   { M, M, I },                          /* 09 */
60   { M, M, I },                          /* 0A */
61   { M, M, I },                          /* 0B */
62   { M, F, I },                          /* 0C */
63   { M, F, I },                          /* 0D */
64   { M, M, F },                          /* 0E */
65   { M, M, F },                          /* 0F */
66   { M, I, B },                          /* 10 */
67   { M, I, B },                          /* 11 */
68   { M, B, B },                          /* 12 */
69   { M, B, B },                          /* 13 */
70   { u, u, u },                          /* 14 */
71   { u, u, u },                          /* 15 */
72   { B, B, B },                          /* 16 */
73   { B, B, B },                          /* 17 */
74   { M, M, B },                          /* 18 */
75   { M, M, B },                          /* 19 */
76   { u, u, u },                          /* 1A */
77   { u, u, u },                          /* 1B */
78   { M, F, B },                          /* 1C */
79   { M, F, B },                          /* 1D */
80   { u, u, u },                          /* 1E */
81   { u, u, u },                          /* 1F */
82 };
83
84 /*
85  * In this function we check to see if the instruction
86  * is IP relative instruction and update the kprobe
87  * inst flag accordingly
88  */
89 static void update_kprobe_inst_flag(uint template, uint  slot, uint major_opcode,
90         unsigned long kprobe_inst, struct kprobe *p)
91 {
92         p->ainsn.inst_flag = 0;
93         p->ainsn.target_br_reg = 0;
94
95         if (bundle_encoding[template][slot] == B) {
96                 switch (major_opcode) {
97                   case INDIRECT_CALL_OPCODE:
98                         p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
99                         p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
100                         break;
101                   case IP_RELATIVE_PREDICT_OPCODE:
102                   case IP_RELATIVE_BRANCH_OPCODE:
103                         p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;
104                         break;
105                   case IP_RELATIVE_CALL_OPCODE:
106                         p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;
107                         p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
108                         p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
109                         break;
110                 }
111         } else if (bundle_encoding[template][slot] == X) {
112                 switch (major_opcode) {
113                   case LONG_CALL_OPCODE:
114                         p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
115                         p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
116                   break;
117                 }
118         }
119         return;
120 }
121
122 /*
123  * In this function we check to see if the instruction
124  * on which we are inserting kprobe is supported.
125  * Returns 0 if supported
126  * Returns -EINVAL if unsupported
127  */
128 static int unsupported_inst(uint template, uint  slot, uint major_opcode,
129         unsigned long kprobe_inst, struct kprobe *p)
130 {
131         unsigned long addr = (unsigned long)p->addr;
132
133         if (bundle_encoding[template][slot] == I) {
134                 switch (major_opcode) {
135                         case 0x0: //I_UNIT_MISC_OPCODE:
136                         /*
137                          * Check for Integer speculation instruction
138                          * - Bit 33-35 to be equal to 0x1
139                          */
140                         if (((kprobe_inst >> 33) & 0x7) == 1) {
141                                 printk(KERN_WARNING
142                                         "Kprobes on speculation inst at <0x%lx> not supported\n",
143                                         addr);
144                                 return -EINVAL;
145                         }
146
147                         /*
148                          * IP relative mov instruction
149                          *  - Bit 27-35 to be equal to 0x30
150                          */
151                         if (((kprobe_inst >> 27) & 0x1FF) == 0x30) {
152                                 printk(KERN_WARNING
153                                         "Kprobes on \"mov r1=ip\" at <0x%lx> not supported\n",
154                                         addr);
155                                 return -EINVAL;
156
157                         }
158                 }
159         }
160         return 0;
161 }
162
163
164 /*
165  * In this function we check to see if the instruction
166  * (qp) cmpx.crel.ctype p1,p2=r2,r3
167  * on which we are inserting kprobe is cmp instruction
168  * with ctype as unc.
169  */
170 static uint is_cmp_ctype_unc_inst(uint template, uint slot, uint major_opcode,
171 unsigned long kprobe_inst)
172 {
173         cmp_inst_t cmp_inst;
174         uint ctype_unc = 0;
175
176         if (!((bundle_encoding[template][slot] == I) ||
177                 (bundle_encoding[template][slot] == M)))
178                 goto out;
179
180         if (!((major_opcode == 0xC) || (major_opcode == 0xD) ||
181                 (major_opcode == 0xE)))
182                 goto out;
183
184         cmp_inst.l = kprobe_inst;
185         if ((cmp_inst.f.x2 == 0) || (cmp_inst.f.x2 == 1)) {
186                 /* Integere compare - Register Register (A6 type)*/
187                 if ((cmp_inst.f.tb == 0) && (cmp_inst.f.ta == 0)
188                                 &&(cmp_inst.f.c == 1))
189                         ctype_unc = 1;
190         } else if ((cmp_inst.f.x2 == 2)||(cmp_inst.f.x2 == 3)) {
191                 /* Integere compare - Immediate Register (A8 type)*/
192                 if ((cmp_inst.f.ta == 0) &&(cmp_inst.f.c == 1))
193                         ctype_unc = 1;
194         }
195 out:
196         return ctype_unc;
197 }
198
199 /*
200  * In this function we override the bundle with
201  * the break instruction at the given slot.
202  */
203 static void prepare_break_inst(uint template, uint  slot, uint major_opcode,
204         unsigned long kprobe_inst, struct kprobe *p)
205 {
206         unsigned long break_inst = BREAK_INST;
207         bundle_t *bundle = &p->ainsn.insn.bundle;
208
209         /*
210          * Copy the original kprobe_inst qualifying predicate(qp)
211          * to the break instruction iff !is_cmp_ctype_unc_inst
212          * because for cmp instruction with ctype equal to unc,
213          * which is a special instruction always needs to be
214          * executed regradless of qp
215          */
216         if (!is_cmp_ctype_unc_inst(template, slot, major_opcode, kprobe_inst))
217                 break_inst |= (0x3f & kprobe_inst);
218
219         switch (slot) {
220           case 0:
221                 bundle->quad0.slot0 = break_inst;
222                 break;
223           case 1:
224                 bundle->quad0.slot1_p0 = break_inst;
225                 bundle->quad1.slot1_p1 = break_inst >> (64-46);
226                 break;
227           case 2:
228                 bundle->quad1.slot2 = break_inst;
229                 break;
230         }
231
232         /*
233          * Update the instruction flag, so that we can
234          * emulate the instruction properly after we
235          * single step on original instruction
236          */
237         update_kprobe_inst_flag(template, slot, major_opcode, kprobe_inst, p);
238 }
239
240 static inline void get_kprobe_inst(bundle_t *bundle, uint slot,
241                 unsigned long *kprobe_inst, uint *major_opcode)
242 {
243         unsigned long kprobe_inst_p0, kprobe_inst_p1;
244         unsigned int template;
245
246         template = bundle->quad0.template;
247
248         switch (slot) {
249           case 0:
250                 *major_opcode = (bundle->quad0.slot0 >> SLOT0_OPCODE_SHIFT);
251                 *kprobe_inst = bundle->quad0.slot0;
252                 break;
253           case 1:
254                 *major_opcode = (bundle->quad1.slot1_p1 >> SLOT1_p1_OPCODE_SHIFT);
255                 kprobe_inst_p0 = bundle->quad0.slot1_p0;
256                 kprobe_inst_p1 = bundle->quad1.slot1_p1;
257                 *kprobe_inst = kprobe_inst_p0 | (kprobe_inst_p1 << (64-46));
258                 break;
259           case 2:
260                 *major_opcode = (bundle->quad1.slot2 >> SLOT2_OPCODE_SHIFT);
261                 *kprobe_inst = bundle->quad1.slot2;
262                 break;
263         }
264 }
265
266 static int valid_kprobe_addr(int template, int slot, unsigned long addr)
267 {
268         if ((slot > 2) || ((bundle_encoding[template][1] == L) && slot > 1)) {
269                 printk(KERN_WARNING "Attempting to insert unaligned kprobe at 0x%lx\n",
270                                 addr);
271                 return -EINVAL;
272         }
273         return 0;
274 }
275
276 int arch_prepare_kprobe(struct kprobe *p)
277 {
278         unsigned long addr = (unsigned long) p->addr;
279         unsigned long *kprobe_addr = (unsigned long *)(addr & ~0xFULL);
280         unsigned long kprobe_inst=0;
281         unsigned int slot = addr & 0xf, template, major_opcode = 0;
282         bundle_t *bundle = &p->ainsn.insn.bundle;
283
284         memcpy(&p->opcode.bundle, kprobe_addr, sizeof(bundle_t));
285         memcpy(&p->ainsn.insn.bundle, kprobe_addr, sizeof(bundle_t));
286
287         template = bundle->quad0.template;
288
289         if(valid_kprobe_addr(template, slot, addr))
290                 return -EINVAL;
291
292         /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
293         if (slot == 1 && bundle_encoding[template][1] == L)
294                 slot++;
295
296         /* Get kprobe_inst and major_opcode from the bundle */
297         get_kprobe_inst(bundle, slot, &kprobe_inst, &major_opcode);
298
299         if (unsupported_inst(template, slot, major_opcode, kprobe_inst, p))
300                         return -EINVAL;
301
302         prepare_break_inst(template, slot, major_opcode, kprobe_inst, p);
303
304         return 0;
305 }
306
307 void arch_arm_kprobe(struct kprobe *p)
308 {
309         unsigned long addr = (unsigned long)p->addr;
310         unsigned long arm_addr = addr & ~0xFULL;
311
312         memcpy((char *)arm_addr, &p->ainsn.insn.bundle, sizeof(bundle_t));
313         flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t));
314 }
315
316 void arch_disarm_kprobe(struct kprobe *p)
317 {
318         unsigned long addr = (unsigned long)p->addr;
319         unsigned long arm_addr = addr & ~0xFULL;
320
321         /* p->opcode contains the original unaltered bundle */
322         memcpy((char *) arm_addr, (char *) &p->opcode.bundle, sizeof(bundle_t));
323         flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t));
324 }
325
326 void arch_remove_kprobe(struct kprobe *p)
327 {
328 }
329
330 /*
331  * We are resuming execution after a single step fault, so the pt_regs
332  * structure reflects the register state after we executed the instruction
333  * located in the kprobe (p->ainsn.insn.bundle).  We still need to adjust
334  * the ip to point back to the original stack address. To set the IP address
335  * to original stack address, handle the case where we need to fixup the
336  * relative IP address and/or fixup branch register.
337  */
338 static void resume_execution(struct kprobe *p, struct pt_regs *regs)
339 {
340         unsigned long bundle_addr = ((unsigned long) (&p->opcode.bundle)) & ~0xFULL;
341         unsigned long resume_addr = (unsigned long)p->addr & ~0xFULL;
342         unsigned long template;
343         int slot = ((unsigned long)p->addr & 0xf);
344
345         template = p->opcode.bundle.quad0.template;
346
347         if (slot == 1 && bundle_encoding[template][1] == L)
348                 slot = 2;
349
350         if (p->ainsn.inst_flag) {
351
352                 if (p->ainsn.inst_flag & INST_FLAG_FIX_RELATIVE_IP_ADDR) {
353                         /* Fix relative IP address */
354                         regs->cr_iip = (regs->cr_iip - bundle_addr) + resume_addr;
355                 }
356
357                 if (p->ainsn.inst_flag & INST_FLAG_FIX_BRANCH_REG) {
358                 /*
359                  * Fix target branch register, software convention is
360                  * to use either b0 or b6 or b7, so just checking
361                  * only those registers
362                  */
363                         switch (p->ainsn.target_br_reg) {
364                         case 0:
365                                 if ((regs->b0 == bundle_addr) ||
366                                         (regs->b0 == bundle_addr + 0x10)) {
367                                         regs->b0 = (regs->b0 - bundle_addr) +
368                                                 resume_addr;
369                                 }
370                                 break;
371                         case 6:
372                                 if ((regs->b6 == bundle_addr) ||
373                                         (regs->b6 == bundle_addr + 0x10)) {
374                                         regs->b6 = (regs->b6 - bundle_addr) +
375                                                 resume_addr;
376                                 }
377                                 break;
378                         case 7:
379                                 if ((regs->b7 == bundle_addr) ||
380                                         (regs->b7 == bundle_addr + 0x10)) {
381                                         regs->b7 = (regs->b7 - bundle_addr) +
382                                                 resume_addr;
383                                 }
384                                 break;
385                         } /* end switch */
386                 }
387                 goto turn_ss_off;
388         }
389
390         if (slot == 2) {
391                 if (regs->cr_iip == bundle_addr + 0x10) {
392                         regs->cr_iip = resume_addr + 0x10;
393                 }
394         } else {
395                 if (regs->cr_iip == bundle_addr) {
396                         regs->cr_iip = resume_addr;
397                 }
398         }
399
400 turn_ss_off:
401         /* Turn off Single Step bit */
402         ia64_psr(regs)->ss = 0;
403 }
404
405 static void prepare_ss(struct kprobe *p, struct pt_regs *regs)
406 {
407         unsigned long bundle_addr = (unsigned long) &p->opcode.bundle;
408         unsigned long slot = (unsigned long)p->addr & 0xf;
409
410         /* Update instruction pointer (IIP) and slot number (IPSR.ri) */
411         regs->cr_iip = bundle_addr & ~0xFULL;
412
413         if (slot > 2)
414                 slot = 0;
415
416         ia64_psr(regs)->ri = slot;
417
418         /* turn on single stepping */
419         ia64_psr(regs)->ss = 1;
420 }
421
422 static int pre_kprobes_handler(struct die_args *args)
423 {
424         struct kprobe *p;
425         int ret = 0;
426         struct pt_regs *regs = args->regs;
427         kprobe_opcode_t *addr = (kprobe_opcode_t *)instruction_pointer(regs);
428
429         preempt_disable();
430
431         /* Handle recursion cases */
432         if (kprobe_running()) {
433                 p = get_kprobe(addr);
434                 if (p) {
435                         if (kprobe_status == KPROBE_HIT_SS) {
436                                 unlock_kprobes();
437                                 goto no_kprobe;
438                         }
439                         arch_disarm_kprobe(p);
440                         ret = 1;
441                 } else if (args->err == __IA64_BREAK_JPROBE) {
442                         /*
443                          * jprobe instrumented function just completed
444                          */
445                         p = current_kprobe;
446                         if (p->break_handler && p->break_handler(p, regs)) {
447                                 goto ss_probe;
448                         }
449                 } else {
450                         /* Not our break */
451                         goto no_kprobe;
452                 }
453         }
454
455         lock_kprobes();
456         p = get_kprobe(addr);
457         if (!p) {
458                 unlock_kprobes();
459                 goto no_kprobe;
460         }
461
462         kprobe_status = KPROBE_HIT_ACTIVE;
463         current_kprobe = p;
464
465         if (p->pre_handler && p->pre_handler(p, regs))
466                 /*
467                  * Our pre-handler is specifically requesting that we just
468                  * do a return.  This is handling the case where the
469                  * pre-handler is really our special jprobe pre-handler.
470                  */
471                 return 1;
472
473 ss_probe:
474         prepare_ss(p, regs);
475         kprobe_status = KPROBE_HIT_SS;
476         return 1;
477
478 no_kprobe:
479         preempt_enable_no_resched();
480         return ret;
481 }
482
483 static int post_kprobes_handler(struct pt_regs *regs)
484 {
485         if (!kprobe_running())
486                 return 0;
487
488         if (current_kprobe->post_handler)
489                 current_kprobe->post_handler(current_kprobe, regs, 0);
490
491         resume_execution(current_kprobe, regs);
492
493         unlock_kprobes();
494         preempt_enable_no_resched();
495         return 1;
496 }
497
498 static int kprobes_fault_handler(struct pt_regs *regs, int trapnr)
499 {
500         if (!kprobe_running())
501                 return 0;
502
503         if (current_kprobe->fault_handler &&
504             current_kprobe->fault_handler(current_kprobe, regs, trapnr))
505                 return 1;
506
507         if (kprobe_status & KPROBE_HIT_SS) {
508                 resume_execution(current_kprobe, regs);
509                 unlock_kprobes();
510                 preempt_enable_no_resched();
511         }
512
513         return 0;
514 }
515
516 int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
517                              void *data)
518 {
519         struct die_args *args = (struct die_args *)data;
520         switch(val) {
521         case DIE_BREAK:
522                 if (pre_kprobes_handler(args))
523                         return NOTIFY_STOP;
524                 break;
525         case DIE_SS:
526                 if (post_kprobes_handler(args->regs))
527                         return NOTIFY_STOP;
528                 break;
529         case DIE_PAGE_FAULT:
530                 if (kprobes_fault_handler(args->regs, args->trapnr))
531                         return NOTIFY_STOP;
532         default:
533                 break;
534         }
535         return NOTIFY_DONE;
536 }
537
538 int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
539 {
540         struct jprobe *jp = container_of(p, struct jprobe, kp);
541         unsigned long addr = ((struct fnptr *)(jp->entry))->ip;
542
543         /* save architectural state */
544         jprobe_saved_regs = *regs;
545
546         /* after rfi, execute the jprobe instrumented function */
547         regs->cr_iip = addr & ~0xFULL;
548         ia64_psr(regs)->ri = addr & 0xf;
549         regs->r1 = ((struct fnptr *)(jp->entry))->gp;
550
551         /*
552          * fix the return address to our jprobe_inst_return() function
553          * in the jprobes.S file
554          */
555         regs->b0 = ((struct fnptr *)(jprobe_inst_return))->ip;
556
557         return 1;
558 }
559
560 int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
561 {
562         *regs = jprobe_saved_regs;
563         return 1;
564 }