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