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