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