Kprobes: The ON/OFF knob thru debugfs
[deliverable/linux.git] / arch / ia64 / kernel / kprobes.c
CommitLineData
fd7b231f
AK
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
fd7b231f
AK
26#include <linux/kprobes.h>
27#include <linux/ptrace.h>
fd7b231f
AK
28#include <linux/string.h>
29#include <linux/slab.h>
30#include <linux/preempt.h>
31#include <linux/moduleloader.h>
1eeb66a1 32#include <linux/kdebug.h>
fd7b231f
AK
33
34#include <asm/pgtable.h>
c7b645f9 35#include <asm/sections.h>
c04c1c81 36#include <asm/uaccess.h>
fd7b231f 37
b2761dc2
AK
38extern void jprobe_inst_return(void);
39
8a5c4dc5
AM
40DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
41DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
fd7b231f
AK
42
43enum instruction_type {A, I, M, F, B, L, X, u};
44static 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
a5403183
AK
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 */
1f7ad57b
PP
84static void __kprobes update_kprobe_inst_flag(uint template, uint slot,
85 uint major_opcode,
86 unsigned long kprobe_inst,
87 struct kprobe *p)
fd7b231f 88{
8bc76772
RL
89 p->ainsn.inst_flag = 0;
90 p->ainsn.target_br_reg = 0;
08ed38b6 91 p->ainsn.slot = slot;
fd7b231f 92
deac66ae 93 /* Check for Break instruction
62c27be0 94 * Bits 37:40 Major opcode to be zero
deac66ae
KA
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
a5403183
AK
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;
62c27be0 108 p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
109 break;
a5403183
AK
110 case IP_RELATIVE_PREDICT_OPCODE:
111 case IP_RELATIVE_BRANCH_OPCODE:
112 p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;
62c27be0 113 break;
a5403183 114 case IP_RELATIVE_CALL_OPCODE:
62c27be0 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;
a5403183 119 }
62c27be0 120 } else if (bundle_encoding[template][slot] == X) {
a5403183
AK
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}
fd7b231f 130
1674eafc
AK
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 */
1f7ad57b
PP
137static uint __kprobes is_cmp_ctype_unc_inst(uint template, uint slot,
138 uint major_opcode,
139 unsigned long kprobe_inst)
1674eafc
AK
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 }
163out:
164 return ctype_unc;
165}
166
df3e0d1c 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 */
173static 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
a5403183
AK
280/*
281 * In this function we override the bundle with
282 * the break instruction at the given slot.
283 */
1f7ad57b
PP
284static void __kprobes prepare_break_inst(uint template, uint slot,
285 uint major_opcode,
286 unsigned long kprobe_inst,
df3e0d1c 287 struct kprobe *p,
288 int qp)
a5403183
AK
289{
290 unsigned long break_inst = BREAK_INST;
214ddde2 291 bundle_t *bundle = &p->opcode.bundle;
a5403183
AK
292
293 /*
294 * Copy the original kprobe_inst qualifying predicate(qp)
df3e0d1c 295 * to the break instruction
a5403183 296 */
df3e0d1c 297 break_inst |= qp;
a5403183
AK
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;
8bc76772 310 }
cd2675bf 311
a5403183
AK
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
3ca269d8 320static void __kprobes get_kprobe_inst(bundle_t *bundle, uint slot,
a5403183
AK
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;
fd7b231f 327
fd7b231f 328 switch (slot) {
a5403183 329 case 0:
62c27be0 330 *major_opcode = (bundle->quad0.slot0 >> SLOT0_OPCODE_SHIFT);
331 *kprobe_inst = bundle->quad0.slot0;
332 break;
a5403183 333 case 1:
62c27be0 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));
fd7b231f 338 break;
a5403183 339 case 2:
62c27be0 340 *major_opcode = (bundle->quad1.slot2 >> SLOT2_OPCODE_SHIFT);
341 *kprobe_inst = bundle->quad1.slot2;
fd7b231f
AK
342 break;
343 }
a5403183 344}
fd7b231f 345
c7b645f9 346/* Returns non-zero if the addr is in the Interrupt Vector Table */
3ca269d8 347static int __kprobes in_ivt_functions(unsigned long addr)
c7b645f9
KA
348{
349 return (addr >= (unsigned long)__start_ivt_text
350 && addr < (unsigned long)__end_ivt_text);
351}
352
1f7ad57b
PP
353static int __kprobes valid_kprobe_addr(int template, int slot,
354 unsigned long addr)
a5403183
AK
355{
356 if ((slot > 2) || ((bundle_encoding[template][1] == L) && slot > 1)) {
c7b645f9
KA
357 printk(KERN_WARNING "Attempting to insert unaligned kprobe "
358 "at 0x%lx\n", addr);
a5403183 359 return -EINVAL;
8bc76772 360 }
a528e21c 361
62c27be0 362 if (in_ivt_functions(addr)) {
363 printk(KERN_WARNING "Kprobes can't be inserted inside "
c7b645f9 364 "IVT functions at 0x%lx\n", addr);
62c27be0 365 return -EINVAL;
366 }
c7b645f9 367
a5403183
AK
368 return 0;
369}
370
3ca269d8 371static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
852caccc 372{
8a5c4dc5
AM
373 kcb->prev_kprobe.kp = kprobe_running();
374 kcb->prev_kprobe.status = kcb->kprobe_status;
852caccc
AK
375}
376
3ca269d8 377static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
852caccc 378{
8a5c4dc5
AM
379 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
380 kcb->kprobe_status = kcb->prev_kprobe.status;
852caccc
AK
381}
382
3ca269d8 383static void __kprobes set_current_kprobe(struct kprobe *p,
8a5c4dc5 384 struct kprobe_ctlblk *kcb)
852caccc 385{
8a5c4dc5 386 __get_cpu_var(current_kprobe) = p;
852caccc
AK
387}
388
9508dbfe
RL
389static 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 */
1f7ad57b 401int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
9508dbfe
RL
402{
403 struct kretprobe_instance *ri = NULL;
99219a3f 404 struct hlist_head *head, empty_rp;
9508dbfe 405 struct hlist_node *node, *tmp;
991a51d8 406 unsigned long flags, orig_ret_address = 0;
9508dbfe
RL
407 unsigned long trampoline_address =
408 ((struct fnptr *)kretprobe_trampoline)->ip;
409
99219a3f 410 INIT_HLIST_HEAD(&empty_rp);
991a51d8 411 spin_lock_irqsave(&kretprobe_lock, flags);
9138d581 412 head = kretprobe_inst_table_head(current);
9508dbfe
RL
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) {
9138d581 428 if (ri->task != current)
9508dbfe 429 /* another task is sharing our hash bucket */
9138d581 430 continue;
9508dbfe
RL
431
432 if (ri->rp && ri->rp->handler)
433 ri->rp->handler(ri, regs);
434
435 orig_ret_address = (unsigned long)ri->ret_addr;
99219a3f 436 recycle_rp_inst(ri, &empty_rp);
9508dbfe
RL
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
0f95b7fc
AM
447 kretprobe_assert(ri, orig_ret_address, trampoline_address);
448
9508dbfe
RL
449 regs->cr_iip = orig_ret_address;
450
8a5c4dc5 451 reset_current_kprobe();
991a51d8 452 spin_unlock_irqrestore(&kretprobe_lock, flags);
9508dbfe
RL
453 preempt_enable_no_resched();
454
99219a3f 455 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
456 hlist_del(&ri->hlist);
457 kfree(ri);
458 }
d217d545
AM
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 */
9138d581 464 return 1;
9508dbfe
RL
465}
466
991a51d8 467/* Called with kretprobe_lock held */
4c4308cb 468void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
1f7ad57b 469 struct pt_regs *regs)
9508dbfe 470{
4c4308cb 471 ri->ret_addr = (kprobe_opcode_t *)regs->b0;
9508dbfe 472
4c4308cb
CH
473 /* Replace the return addr with trampoline addr */
474 regs->b0 = ((struct fnptr *)kretprobe_trampoline)->ip;
9508dbfe
RL
475}
476
1f7ad57b 477int __kprobes arch_prepare_kprobe(struct kprobe *p)
a5403183
AK
478{
479 unsigned long addr = (unsigned long) p->addr;
480 unsigned long *kprobe_addr = (unsigned long *)(addr & ~0xFULL);
481 unsigned long kprobe_inst=0;
482 unsigned int slot = addr & 0xf, template, major_opcode = 0;
214ddde2 483 bundle_t *bundle;
df3e0d1c 484 int qp;
a5403183 485
214ddde2 486 bundle = &((kprobe_opcode_t *)kprobe_addr)->bundle;
62c27be0 487 template = bundle->quad0.template;
a5403183
AK
488
489 if(valid_kprobe_addr(template, slot, addr))
490 return -EINVAL;
491
492 /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
62c27be0 493 if (slot == 1 && bundle_encoding[template][1] == L)
494 slot++;
a5403183
AK
495
496 /* Get kprobe_inst and major_opcode from the bundle */
497 get_kprobe_inst(bundle, slot, &kprobe_inst, &major_opcode);
498
df3e0d1c 499 qp = unsupported_inst(template, slot, major_opcode, kprobe_inst, addr);
500 if (qp < 0)
501 return -EINVAL;
8bc76772 502
214ddde2 503 p->ainsn.insn = get_insn_slot();
504 if (!p->ainsn.insn)
505 return -ENOMEM;
506 memcpy(&p->opcode, kprobe_addr, sizeof(kprobe_opcode_t));
507 memcpy(p->ainsn.insn, kprobe_addr, sizeof(kprobe_opcode_t));
8bc76772 508
df3e0d1c 509 prepare_break_inst(template, slot, major_opcode, kprobe_inst, p, qp);
a9ad965e 510
214ddde2 511 return 0;
a9ad965e 512}
513
1f7ad57b 514void __kprobes arch_arm_kprobe(struct kprobe *p)
8bc76772 515{
08ed38b6
TL
516 unsigned long arm_addr;
517 bundle_t *src, *dest;
518
519 arm_addr = ((unsigned long)p->addr) & ~0xFUL;
520 dest = &((kprobe_opcode_t *)arm_addr)->bundle;
521 src = &p->opcode.bundle;
8bc76772 522
214ddde2 523 flush_icache_range((unsigned long)p->ainsn.insn,
524 (unsigned long)p->ainsn.insn + sizeof(kprobe_opcode_t));
08ed38b6
TL
525 switch (p->ainsn.slot) {
526 case 0:
527 dest->quad0.slot0 = src->quad0.slot0;
528 break;
529 case 1:
530 dest->quad1.slot1_p1 = src->quad1.slot1_p1;
531 break;
532 case 2:
533 dest->quad1.slot2 = src->quad1.slot2;
534 break;
535 }
214ddde2 536 flush_icache_range(arm_addr, arm_addr + sizeof(kprobe_opcode_t));
fd7b231f
AK
537}
538
1f7ad57b 539void __kprobes arch_disarm_kprobe(struct kprobe *p)
fd7b231f 540{
08ed38b6
TL
541 unsigned long arm_addr;
542 bundle_t *src, *dest;
fd7b231f 543
08ed38b6
TL
544 arm_addr = ((unsigned long)p->addr) & ~0xFUL;
545 dest = &((kprobe_opcode_t *)arm_addr)->bundle;
214ddde2 546 /* p->ainsn.insn contains the original unaltered kprobe_opcode_t */
08ed38b6
TL
547 src = &p->ainsn.insn->bundle;
548 switch (p->ainsn.slot) {
549 case 0:
550 dest->quad0.slot0 = src->quad0.slot0;
551 break;
552 case 1:
553 dest->quad1.slot1_p1 = src->quad1.slot1_p1;
554 break;
555 case 2:
556 dest->quad1.slot2 = src->quad1.slot2;
557 break;
558 }
214ddde2 559 flush_icache_range(arm_addr, arm_addr + sizeof(kprobe_opcode_t));
fd7b231f
AK
560}
561
214ddde2 562void __kprobes arch_remove_kprobe(struct kprobe *p)
563{
564 mutex_lock(&kprobe_mutex);
b4c6c34a 565 free_insn_slot(p->ainsn.insn, 0);
214ddde2 566 mutex_unlock(&kprobe_mutex);
567}
fd7b231f
AK
568/*
569 * We are resuming execution after a single step fault, so the pt_regs
570 * structure reflects the register state after we executed the instruction
571 * located in the kprobe (p->ainsn.insn.bundle). We still need to adjust
cd2675bf
AK
572 * the ip to point back to the original stack address. To set the IP address
573 * to original stack address, handle the case where we need to fixup the
574 * relative IP address and/or fixup branch register.
fd7b231f 575 */
1f7ad57b 576static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
fd7b231f 577{
62c27be0 578 unsigned long bundle_addr = (unsigned long) (&p->ainsn.insn->bundle);
579 unsigned long resume_addr = (unsigned long)p->addr & ~0xFULL;
580 unsigned long template;
581 int slot = ((unsigned long)p->addr & 0xf);
fd7b231f 582
214ddde2 583 template = p->ainsn.insn->bundle.quad0.template;
cd2675bf 584
62c27be0 585 if (slot == 1 && bundle_encoding[template][1] == L)
586 slot = 2;
cd2675bf
AK
587
588 if (p->ainsn.inst_flag) {
589
590 if (p->ainsn.inst_flag & INST_FLAG_FIX_RELATIVE_IP_ADDR) {
591 /* Fix relative IP address */
62c27be0 592 regs->cr_iip = (regs->cr_iip - bundle_addr) +
593 resume_addr;
cd2675bf
AK
594 }
595
596 if (p->ainsn.inst_flag & INST_FLAG_FIX_BRANCH_REG) {
597 /*
598 * Fix target branch register, software convention is
599 * to use either b0 or b6 or b7, so just checking
600 * only those registers
601 */
602 switch (p->ainsn.target_br_reg) {
603 case 0:
604 if ((regs->b0 == bundle_addr) ||
605 (regs->b0 == bundle_addr + 0x10)) {
606 regs->b0 = (regs->b0 - bundle_addr) +
607 resume_addr;
608 }
609 break;
610 case 6:
611 if ((regs->b6 == bundle_addr) ||
612 (regs->b6 == bundle_addr + 0x10)) {
613 regs->b6 = (regs->b6 - bundle_addr) +
614 resume_addr;
615 }
616 break;
617 case 7:
618 if ((regs->b7 == bundle_addr) ||
619 (regs->b7 == bundle_addr + 0x10)) {
620 regs->b7 = (regs->b7 - bundle_addr) +
621 resume_addr;
622 }
623 break;
624 } /* end switch */
625 }
626 goto turn_ss_off;
627 }
fd7b231f 628
cd2675bf 629 if (slot == 2) {
62c27be0 630 if (regs->cr_iip == bundle_addr + 0x10) {
631 regs->cr_iip = resume_addr + 0x10;
632 }
633 } else {
634 if (regs->cr_iip == bundle_addr) {
635 regs->cr_iip = resume_addr;
636 }
a5403183 637 }
fd7b231f 638
cd2675bf 639turn_ss_off:
62c27be0 640 /* Turn off Single Step bit */
641 ia64_psr(regs)->ss = 0;
fd7b231f
AK
642}
643
1f7ad57b 644static void __kprobes prepare_ss(struct kprobe *p, struct pt_regs *regs)
fd7b231f 645{
214ddde2 646 unsigned long bundle_addr = (unsigned long) &p->ainsn.insn->bundle;
fd7b231f
AK
647 unsigned long slot = (unsigned long)p->addr & 0xf;
648
deac66ae
KA
649 /* single step inline if break instruction */
650 if (p->ainsn.inst_flag == INST_FLAG_BREAK_INST)
651 regs->cr_iip = (unsigned long)p->addr & ~0xFULL;
652 else
653 regs->cr_iip = bundle_addr & ~0xFULL;
fd7b231f
AK
654
655 if (slot > 2)
656 slot = 0;
657
658 ia64_psr(regs)->ri = slot;
659
660 /* turn on single stepping */
661 ia64_psr(regs)->ss = 1;
662}
663
661e5a3d
KA
664static int __kprobes is_ia64_break_inst(struct pt_regs *regs)
665{
666 unsigned int slot = ia64_psr(regs)->ri;
667 unsigned int template, major_opcode;
668 unsigned long kprobe_inst;
669 unsigned long *kprobe_addr = (unsigned long *)regs->cr_iip;
670 bundle_t bundle;
671
672 memcpy(&bundle, kprobe_addr, sizeof(bundle_t));
673 template = bundle.quad0.template;
674
675 /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
676 if (slot == 1 && bundle_encoding[template][1] == L)
62c27be0 677 slot++;
661e5a3d
KA
678
679 /* Get Kprobe probe instruction at given slot*/
680 get_kprobe_inst(&bundle, slot, &kprobe_inst, &major_opcode);
681
682 /* For break instruction,
683 * Bits 37:40 Major opcode to be zero
684 * Bits 27:32 X6 to be zero
685 * Bits 32:35 X3 to be zero
686 */
687 if (major_opcode || ((kprobe_inst >> 27) & 0x1FF) ) {
688 /* Not a break instruction */
689 return 0;
690 }
691
692 /* Is a break instruction */
693 return 1;
694}
695
1f7ad57b 696static int __kprobes pre_kprobes_handler(struct die_args *args)
fd7b231f
AK
697{
698 struct kprobe *p;
699 int ret = 0;
89cb14c0 700 struct pt_regs *regs = args->regs;
fd7b231f 701 kprobe_opcode_t *addr = (kprobe_opcode_t *)instruction_pointer(regs);
d217d545
AM
702 struct kprobe_ctlblk *kcb;
703
704 /*
705 * We don't want to be preempted for the entire
706 * duration of kprobe processing
707 */
708 preempt_disable();
709 kcb = get_kprobe_ctlblk();
fd7b231f 710
fd7b231f
AK
711 /* Handle recursion cases */
712 if (kprobe_running()) {
713 p = get_kprobe(addr);
714 if (p) {
8a5c4dc5 715 if ((kcb->kprobe_status == KPROBE_HIT_SS) &&
deac66ae 716 (p->ainsn.inst_flag == INST_FLAG_BREAK_INST)) {
62c27be0 717 ia64_psr(regs)->ss = 0;
fd7b231f
AK
718 goto no_kprobe;
719 }
852caccc
AK
720 /* We have reentered the pre_kprobe_handler(), since
721 * another probe was hit while within the handler.
722 * We here save the original kprobes variables and
723 * just single step on the instruction of the new probe
724 * without calling any user handlers.
725 */
8a5c4dc5
AM
726 save_previous_kprobe(kcb);
727 set_current_kprobe(p, kcb);
bf8d5c52 728 kprobes_inc_nmissed_count(p);
852caccc 729 prepare_ss(p, regs);
8a5c4dc5 730 kcb->kprobe_status = KPROBE_REENTER;
852caccc 731 return 1;
89cb14c0 732 } else if (args->err == __IA64_BREAK_JPROBE) {
fd7b231f
AK
733 /*
734 * jprobe instrumented function just completed
735 */
8a5c4dc5 736 p = __get_cpu_var(current_kprobe);
fd7b231f
AK
737 if (p->break_handler && p->break_handler(p, regs)) {
738 goto ss_probe;
739 }
eb3a7292
KA
740 } else if (!is_ia64_break_inst(regs)) {
741 /* The breakpoint instruction was removed by
742 * another cpu right after we hit, no further
743 * handling of this interrupt is appropriate
744 */
745 ret = 1;
746 goto no_kprobe;
89cb14c0
KA
747 } else {
748 /* Not our break */
749 goto no_kprobe;
fd7b231f
AK
750 }
751 }
752
fd7b231f
AK
753 p = get_kprobe(addr);
754 if (!p) {
661e5a3d
KA
755 if (!is_ia64_break_inst(regs)) {
756 /*
757 * The breakpoint instruction was removed right
758 * after we hit it. Another cpu has removed
759 * either a probepoint or a debugger breakpoint
760 * at this address. In either case, no further
761 * handling of this interrupt is appropriate.
762 */
763 ret = 1;
764
765 }
766
767 /* Not one of our break, let kernel handle it */
fd7b231f
AK
768 goto no_kprobe;
769 }
770
8a5c4dc5
AM
771 set_current_kprobe(p, kcb);
772 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
fd7b231f
AK
773
774 if (p->pre_handler && p->pre_handler(p, regs))
775 /*
776 * Our pre-handler is specifically requesting that we just
9508dbfe
RL
777 * do a return. This is used for both the jprobe pre-handler
778 * and the kretprobe trampoline
fd7b231f
AK
779 */
780 return 1;
781
782ss_probe:
783 prepare_ss(p, regs);
8a5c4dc5 784 kcb->kprobe_status = KPROBE_HIT_SS;
fd7b231f
AK
785 return 1;
786
787no_kprobe:
d217d545 788 preempt_enable_no_resched();
fd7b231f
AK
789 return ret;
790}
791
1f7ad57b 792static int __kprobes post_kprobes_handler(struct pt_regs *regs)
fd7b231f 793{
8a5c4dc5
AM
794 struct kprobe *cur = kprobe_running();
795 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
796
797 if (!cur)
fd7b231f
AK
798 return 0;
799
8a5c4dc5
AM
800 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
801 kcb->kprobe_status = KPROBE_HIT_SSDONE;
802 cur->post_handler(cur, regs, 0);
852caccc 803 }
fd7b231f 804
8a5c4dc5 805 resume_execution(cur, regs);
fd7b231f 806
852caccc 807 /*Restore back the original saved kprobes variables and continue. */
8a5c4dc5
AM
808 if (kcb->kprobe_status == KPROBE_REENTER) {
809 restore_previous_kprobe(kcb);
852caccc
AK
810 goto out;
811 }
8a5c4dc5 812 reset_current_kprobe();
852caccc
AK
813
814out:
fd7b231f
AK
815 preempt_enable_no_resched();
816 return 1;
817}
818
1f7ad57b 819static int __kprobes kprobes_fault_handler(struct pt_regs *regs, int trapnr)
fd7b231f 820{
8a5c4dc5
AM
821 struct kprobe *cur = kprobe_running();
822 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
823
fd7b231f 824
c04c1c81
PP
825 switch(kcb->kprobe_status) {
826 case KPROBE_HIT_SS:
827 case KPROBE_REENTER:
828 /*
829 * We are here because the instruction being single
830 * stepped caused a page fault. We reset the current
831 * kprobe and the instruction pointer points back to
832 * the probe address and allow the page fault handler
833 * to continue as a normal page fault.
834 */
835 regs->cr_iip = ((unsigned long)cur->addr) & ~0xFULL;
836 ia64_psr(regs)->ri = ((unsigned long)cur->addr) & 0xf;
837 if (kcb->kprobe_status == KPROBE_REENTER)
838 restore_previous_kprobe(kcb);
839 else
840 reset_current_kprobe();
fd7b231f 841 preempt_enable_no_resched();
c04c1c81
PP
842 break;
843 case KPROBE_HIT_ACTIVE:
844 case KPROBE_HIT_SSDONE:
845 /*
846 * We increment the nmissed count for accounting,
847 * we can also use npre/npostfault count for accouting
848 * these specific fault cases.
849 */
850 kprobes_inc_nmissed_count(cur);
851
852 /*
853 * We come here because instructions in the pre/post
854 * handler caused the page_fault, this could happen
855 * if handler tries to access user space by
856 * copy_from_user(), get_user() etc. Let the
857 * user-specified handler try to fix it first.
858 */
859 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
860 return 1;
fd32cb3a
KA
861 /*
862 * In case the user-specified fault handler returned
863 * zero, try to fix up.
864 */
865 if (ia64_done_with_exception(regs))
866 return 1;
c04c1c81
PP
867
868 /*
869 * Let ia64_do_page_fault() fix it.
870 */
871 break;
872 default:
873 break;
fd7b231f
AK
874 }
875
876 return 0;
877}
878
1f7ad57b
PP
879int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
880 unsigned long val, void *data)
fd7b231f
AK
881{
882 struct die_args *args = (struct die_args *)data;
66ff2d06
AM
883 int ret = NOTIFY_DONE;
884
2326c770 885 if (args->regs && user_mode(args->regs))
886 return ret;
887
fd7b231f
AK
888 switch(val) {
889 case DIE_BREAK:
9138d581 890 /* err is break number from ia64_bad_break() */
08ed38b6
TL
891 if ((args->err >> 12) == (__IA64_BREAK_KPROBE >> 12)
892 || args->err == __IA64_BREAK_JPROBE
893 || args->err == 0)
9138d581
KO
894 if (pre_kprobes_handler(args))
895 ret = NOTIFY_STOP;
fd7b231f 896 break;
9138d581
KO
897 case DIE_FAULT:
898 /* err is vector number from ia64_fault() */
899 if (args->err == 36)
900 if (post_kprobes_handler(args->regs))
901 ret = NOTIFY_STOP;
fd7b231f
AK
902 break;
903 case DIE_PAGE_FAULT:
d217d545
AM
904 /* kprobe_running() needs smp_processor_id() */
905 preempt_disable();
906 if (kprobe_running() &&
907 kprobes_fault_handler(args->regs, args->trapnr))
66ff2d06 908 ret = NOTIFY_STOP;
d217d545 909 preempt_enable();
fd7b231f
AK
910 default:
911 break;
912 }
66ff2d06 913 return ret;
fd7b231f
AK
914}
915
d3ef1f5a
ZY
916struct param_bsp_cfm {
917 unsigned long ip;
918 unsigned long *bsp;
919 unsigned long cfm;
920};
921
922static void ia64_get_bsp_cfm(struct unw_frame_info *info, void *arg)
923{
924 unsigned long ip;
925 struct param_bsp_cfm *lp = arg;
926
927 do {
928 unw_get_ip(info, &ip);
929 if (ip == 0)
930 break;
931 if (ip == lp->ip) {
932 unw_get_bsp(info, (unsigned long*)&lp->bsp);
933 unw_get_cfm(info, (unsigned long*)&lp->cfm);
934 return;
935 }
936 } while (unw_unwind(info) >= 0);
d61b49c1 937 lp->bsp = NULL;
d3ef1f5a
ZY
938 lp->cfm = 0;
939 return;
940}
941
1f7ad57b 942int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
fd7b231f 943{
b2761dc2
AK
944 struct jprobe *jp = container_of(p, struct jprobe, kp);
945 unsigned long addr = ((struct fnptr *)(jp->entry))->ip;
8a5c4dc5 946 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
d3ef1f5a
ZY
947 struct param_bsp_cfm pa;
948 int bytes;
949
950 /*
951 * Callee owns the argument space and could overwrite it, eg
952 * tail call optimization. So to be absolutely safe
953 * we save the argument space before transfering the control
954 * to instrumented jprobe function which runs in
955 * the process context
956 */
957 pa.ip = regs->cr_iip;
958 unw_init_running(ia64_get_bsp_cfm, &pa);
959 bytes = (char *)ia64_rse_skip_regs(pa.bsp, pa.cfm & 0x3f)
960 - (char *)pa.bsp;
961 memcpy( kcb->jprobes_saved_stacked_regs,
962 pa.bsp,
963 bytes );
964 kcb->bsp = pa.bsp;
965 kcb->cfm = pa.cfm;
fd7b231f 966
b2761dc2 967 /* save architectural state */
8a5c4dc5 968 kcb->jprobe_saved_regs = *regs;
b2761dc2
AK
969
970 /* after rfi, execute the jprobe instrumented function */
971 regs->cr_iip = addr & ~0xFULL;
972 ia64_psr(regs)->ri = addr & 0xf;
973 regs->r1 = ((struct fnptr *)(jp->entry))->gp;
974
975 /*
976 * fix the return address to our jprobe_inst_return() function
977 * in the jprobes.S file
978 */
62c27be0 979 regs->b0 = ((struct fnptr *)(jprobe_inst_return))->ip;
b2761dc2
AK
980
981 return 1;
fd7b231f
AK
982}
983
1f7ad57b 984int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
fd7b231f 985{
8a5c4dc5 986 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
d3ef1f5a 987 int bytes;
8a5c4dc5 988
d3ef1f5a 989 /* restoring architectural state */
8a5c4dc5 990 *regs = kcb->jprobe_saved_regs;
d3ef1f5a
ZY
991
992 /* restoring the original argument space */
993 flush_register_stack();
994 bytes = (char *)ia64_rse_skip_regs(kcb->bsp, kcb->cfm & 0x3f)
995 - (char *)kcb->bsp;
996 memcpy( kcb->bsp,
997 kcb->jprobes_saved_stacked_regs,
998 bytes );
999 invalidate_stacked_regs();
1000
d217d545 1001 preempt_enable_no_resched();
b2761dc2 1002 return 1;
fd7b231f 1003}
9508dbfe
RL
1004
1005static struct kprobe trampoline_p = {
1006 .pre_handler = trampoline_probe_handler
1007};
1008
6772926b 1009int __init arch_init_kprobes(void)
9508dbfe
RL
1010{
1011 trampoline_p.addr =
1012 (kprobe_opcode_t *)((struct fnptr *)kretprobe_trampoline)->ip;
1013 return register_kprobe(&trampoline_p);
1014}
bf8f6e5b
AM
1015
1016int __kprobes arch_trampoline_kprobe(struct kprobe *p)
1017{
1018 if (p->addr ==
1019 (kprobe_opcode_t *)((struct fnptr *)kretprobe_trampoline)->ip)
1020 return 1;
1021
1022 return 0;
1023}
This page took 0.273289 seconds and 5 git commands to generate.