1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Kernel Probes (KProbes)
4 * arch/ia64/kernel/kprobes.c
5 *
6 * Copyright (C) IBM Corporation, 2002, 2004
7 * Copyright (C) Intel Corporation, 2005
8 *
9 * 2005-Apr Rusty Lynch <rusty.lynch@intel.com> and Anil S Keshavamurthy
10 * <anil.s.keshavamurthy@intel.com> adapted from i386
11 */
12
13 #include <linux/kprobes.h>
14 #include <linux/ptrace.h>
15 #include <linux/string.h>
16 #include <linux/slab.h>
17 #include <linux/preempt.h>
18 #include <linux/extable.h>
19 #include <linux/kdebug.h>
20 #include <linux/pgtable.h>
21
22 #include <asm/sections.h>
23 #include <asm/exception.h>
24
25 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
26 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
27
28 struct kretprobe_blackpoint kretprobe_blacklist[] = {{NULL, NULL}};
29
30 enum instruction_type {A, I, M, F, B, L, X, u};
31 static enum instruction_type bundle_encoding[32][3] = {
32 { M, I, I }, /* 00 */
33 { M, I, I }, /* 01 */
34 { M, I, I }, /* 02 */
35 { M, I, I }, /* 03 */
36 { M, L, X }, /* 04 */
37 { M, L, X }, /* 05 */
38 { u, u, u }, /* 06 */
39 { u, u, u }, /* 07 */
40 { M, M, I }, /* 08 */
41 { M, M, I }, /* 09 */
42 { M, M, I }, /* 0A */
43 { M, M, I }, /* 0B */
44 { M, F, I }, /* 0C */
45 { M, F, I }, /* 0D */
46 { M, M, F }, /* 0E */
47 { M, M, F }, /* 0F */
48 { M, I, B }, /* 10 */
49 { M, I, B }, /* 11 */
50 { M, B, B }, /* 12 */
51 { M, B, B }, /* 13 */
52 { u, u, u }, /* 14 */
53 { u, u, u }, /* 15 */
54 { B, B, B }, /* 16 */
55 { B, B, B }, /* 17 */
56 { M, M, B }, /* 18 */
57 { M, M, B }, /* 19 */
58 { u, u, u }, /* 1A */
59 { u, u, u }, /* 1B */
60 { M, F, B }, /* 1C */
61 { M, F, B }, /* 1D */
62 { u, u, u }, /* 1E */
63 { u, u, u }, /* 1F */
64 };
65
66 /* Insert a long branch code */
set_brl_inst(void * from,void * to)67 static void __kprobes set_brl_inst(void *from, void *to)
68 {
69 s64 rel = ((s64) to - (s64) from) >> 4;
70 bundle_t *brl;
71 brl = (bundle_t *) ((u64) from & ~0xf);
72 brl->quad0.template = 0x05; /* [MLX](stop) */
73 brl->quad0.slot0 = NOP_M_INST; /* nop.m 0x0 */
74 brl->quad0.slot1_p0 = ((rel >> 20) & 0x7fffffffff) << 2;
75 brl->quad1.slot1_p1 = (((rel >> 20) & 0x7fffffffff) << 2) >> (64 - 46);
76 /* brl.cond.sptk.many.clr rel<<4 (qp=0) */
77 brl->quad1.slot2 = BRL_INST(rel >> 59, rel & 0xfffff);
78 }
79
80 /*
81 * In this function we check to see if the instruction
82 * is IP relative instruction and update the kprobe
83 * inst flag accordingly
84 */
update_kprobe_inst_flag(uint template,uint slot,uint major_opcode,unsigned long kprobe_inst,struct kprobe * p)85 static void __kprobes update_kprobe_inst_flag(uint template, uint slot,
86 uint major_opcode,
87 unsigned long kprobe_inst,
88 struct kprobe *p)
89 {
90 p->ainsn.inst_flag = 0;
91 p->ainsn.target_br_reg = 0;
92 p->ainsn.slot = slot;
93
94 /* Check for Break instruction
95 * Bits 37:40 Major opcode to be zero
96 * Bits 27:32 X6 to be zero
97 * Bits 32:35 X3 to be zero
98 */
99 if ((!major_opcode) && (!((kprobe_inst >> 27) & 0x1FF)) ) {
100 /* is a break instruction */
101 p->ainsn.inst_flag |= INST_FLAG_BREAK_INST;
102 return;
103 }
104
105 if (bundle_encoding[template][slot] == B) {
106 switch (major_opcode) {
107 case INDIRECT_CALL_OPCODE:
108 p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
109 p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
110 break;
111 case IP_RELATIVE_PREDICT_OPCODE:
112 case IP_RELATIVE_BRANCH_OPCODE:
113 p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;
114 break;
115 case IP_RELATIVE_CALL_OPCODE:
116 p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;
117 p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
118 p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
119 break;
120 }
121 } else if (bundle_encoding[template][slot] == X) {
122 switch (major_opcode) {
123 case LONG_CALL_OPCODE:
124 p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
125 p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
126 break;
127 }
128 }
129 return;
130 }
131
132 /*
133 * In this function we check to see if the instruction
134 * (qp) cmpx.crel.ctype p1,p2=r2,r3
135 * on which we are inserting kprobe is cmp instruction
136 * with ctype as unc.
137 */
is_cmp_ctype_unc_inst(uint template,uint slot,uint major_opcode,unsigned long kprobe_inst)138 static uint __kprobes is_cmp_ctype_unc_inst(uint template, uint slot,
139 uint major_opcode,
140 unsigned long kprobe_inst)
141 {
142 cmp_inst_t cmp_inst;
143 uint ctype_unc = 0;
144
145 if (!((bundle_encoding[template][slot] == I) ||
146 (bundle_encoding[template][slot] == M)))
147 goto out;
148
149 if (!((major_opcode == 0xC) || (major_opcode == 0xD) ||
150 (major_opcode == 0xE)))
151 goto out;
152
153 cmp_inst.l = kprobe_inst;
154 if ((cmp_inst.f.x2 == 0) || (cmp_inst.f.x2 == 1)) {
155 /* Integer compare - Register Register (A6 type)*/
156 if ((cmp_inst.f.tb == 0) && (cmp_inst.f.ta == 0)
157 &&(cmp_inst.f.c == 1))
158 ctype_unc = 1;
159 } else if ((cmp_inst.f.x2 == 2)||(cmp_inst.f.x2 == 3)) {
160 /* Integer compare - Immediate Register (A8 type)*/
161 if ((cmp_inst.f.ta == 0) &&(cmp_inst.f.c == 1))
162 ctype_unc = 1;
163 }
164 out:
165 return ctype_unc;
166 }
167
168 /*
169 * In this function we check to see if the instruction
170 * on which we are inserting kprobe is supported.
171 * Returns qp value if supported
172 * Returns -EINVAL if unsupported
173 */
unsupported_inst(uint template,uint slot,uint major_opcode,unsigned long kprobe_inst,unsigned long addr)174 static int __kprobes unsupported_inst(uint template, uint slot,
175 uint major_opcode,
176 unsigned long kprobe_inst,
177 unsigned long addr)
178 {
179 int qp;
180
181 qp = kprobe_inst & 0x3f;
182 if (is_cmp_ctype_unc_inst(template, slot, major_opcode, kprobe_inst)) {
183 if (slot == 1 && qp) {
184 printk(KERN_WARNING "Kprobes on cmp unc "
185 "instruction on slot 1 at <0x%lx> "
186 "is not supported\n", addr);
187 return -EINVAL;
188
189 }
190 qp = 0;
191 }
192 else if (bundle_encoding[template][slot] == I) {
193 if (major_opcode == 0) {
194 /*
195 * Check for Integer speculation instruction
196 * - Bit 33-35 to be equal to 0x1
197 */
198 if (((kprobe_inst >> 33) & 0x7) == 1) {
199 printk(KERN_WARNING
200 "Kprobes on speculation inst at <0x%lx> not supported\n",
201 addr);
202 return -EINVAL;
203 }
204 /*
205 * IP relative mov instruction
206 * - Bit 27-35 to be equal to 0x30
207 */
208 if (((kprobe_inst >> 27) & 0x1FF) == 0x30) {
209 printk(KERN_WARNING
210 "Kprobes on \"mov r1=ip\" at <0x%lx> not supported\n",
211 addr);
212 return -EINVAL;
213
214 }
215 }
216 else if ((major_opcode == 5) && !(kprobe_inst & (0xFUl << 33)) &&
217 (kprobe_inst & (0x1UL << 12))) {
218 /* test bit instructions, tbit,tnat,tf
219 * bit 33-36 to be equal to 0
220 * bit 12 to be equal to 1
221 */
222 if (slot == 1 && qp) {
223 printk(KERN_WARNING "Kprobes on test bit "
224 "instruction on slot at <0x%lx> "
225 "is not supported\n", addr);
226 return -EINVAL;
227 }
228 qp = 0;
229 }
230 }
231 else if (bundle_encoding[template][slot] == B) {
232 if (major_opcode == 7) {
233 /* IP-Relative Predict major code is 7 */
234 printk(KERN_WARNING "Kprobes on IP-Relative"
235 "Predict is not supported\n");
236 return -EINVAL;
237 }
238 else if (major_opcode == 2) {
239 /* Indirect Predict, major code is 2
240 * bit 27-32 to be equal to 10 or 11
241 */
242 int x6=(kprobe_inst >> 27) & 0x3F;
243 if ((x6 == 0x10) || (x6 == 0x11)) {
244 printk(KERN_WARNING "Kprobes on "
245 "Indirect Predict is not supported\n");
246 return -EINVAL;
247 }
248 }
249 }
250 /* kernel does not use float instruction, here for safety kprobe
251 * will judge whether it is fcmp/flass/float approximation instruction
252 */
253 else if (unlikely(bundle_encoding[template][slot] == F)) {
254 if ((major_opcode == 4 || major_opcode == 5) &&
255 (kprobe_inst & (0x1 << 12))) {
256 /* fcmp/fclass unc instruction */
257 if (slot == 1 && qp) {
258 printk(KERN_WARNING "Kprobes on fcmp/fclass "
259 "instruction on slot at <0x%lx> "
260 "is not supported\n", addr);
261 return -EINVAL;
262
263 }
264 qp = 0;
265 }
266 if ((major_opcode == 0 || major_opcode == 1) &&
267 (kprobe_inst & (0x1UL << 33))) {
268 /* float Approximation instruction */
269 if (slot == 1 && qp) {
270 printk(KERN_WARNING "Kprobes on float Approx "
271 "instr at <0x%lx> is not supported\n",
272 addr);
273 return -EINVAL;
274 }
275 qp = 0;
276 }
277 }
278 return qp;
279 }
280
281 /*
282 * In this function we override the bundle with
283 * the break instruction at the given slot.
284 */
prepare_break_inst(uint template,uint slot,uint major_opcode,unsigned long kprobe_inst,struct kprobe * p,int qp)285 static void __kprobes prepare_break_inst(uint template, uint slot,
286 uint major_opcode,
287 unsigned long kprobe_inst,
288 struct kprobe *p,
289 int qp)
290 {
291 unsigned long break_inst = BREAK_INST;
292 bundle_t *bundle = &p->opcode.bundle;
293
294 /*
295 * Copy the original kprobe_inst qualifying predicate(qp)
296 * to the break instruction
297 */
298 break_inst |= qp;
299
300 switch (slot) {
301 case 0:
302 bundle->quad0.slot0 = break_inst;
303 break;
304 case 1:
305 bundle->quad0.slot1_p0 = break_inst;
306 bundle->quad1.slot1_p1 = break_inst >> (64-46);
307 break;
308 case 2:
309 bundle->quad1.slot2 = break_inst;
310 break;
311 }
312
313 /*
314 * Update the instruction flag, so that we can
315 * emulate the instruction properly after we
316 * single step on original instruction
317 */
318 update_kprobe_inst_flag(template, slot, major_opcode, kprobe_inst, p);
319 }
320
get_kprobe_inst(bundle_t * bundle,uint slot,unsigned long * kprobe_inst,uint * major_opcode)321 static void __kprobes get_kprobe_inst(bundle_t *bundle, uint slot,
322 unsigned long *kprobe_inst, uint *major_opcode)
323 {
324 unsigned long kprobe_inst_p0, kprobe_inst_p1;
325 unsigned int template;
326
327 template = bundle->quad0.template;
328
329 switch (slot) {
330 case 0:
331 *major_opcode = (bundle->quad0.slot0 >> SLOT0_OPCODE_SHIFT);
332 *kprobe_inst = bundle->quad0.slot0;
333 break;
334 case 1:
335 *major_opcode = (bundle->quad1.slot1_p1 >> SLOT1_p1_OPCODE_SHIFT);
336 kprobe_inst_p0 = bundle->quad0.slot1_p0;
337 kprobe_inst_p1 = bundle->quad1.slot1_p1;
338 *kprobe_inst = kprobe_inst_p0 | (kprobe_inst_p1 << (64-46));
339 break;
340 case 2:
341 *major_opcode = (bundle->quad1.slot2 >> SLOT2_OPCODE_SHIFT);
342 *kprobe_inst = bundle->quad1.slot2;
343 break;
344 }
345 }
346
347 /* Returns non-zero if the addr is in the Interrupt Vector Table */
in_ivt_functions(unsigned long addr)348 static int __kprobes in_ivt_functions(unsigned long addr)
349 {
350 return (addr >= (unsigned long)__start_ivt_text
351 && addr < (unsigned long)__end_ivt_text);
352 }
353
valid_kprobe_addr(int template,int slot,unsigned long addr)354 static int __kprobes valid_kprobe_addr(int template, int slot,
355 unsigned long addr)
356 {
357 if ((slot > 2) || ((bundle_encoding[template][1] == L) && slot > 1)) {
358 printk(KERN_WARNING "Attempting to insert unaligned kprobe "
359 "at 0x%lx\n", addr);
360 return -EINVAL;
361 }
362
363 if (in_ivt_functions(addr)) {
364 printk(KERN_WARNING "Kprobes can't be inserted inside "
365 "IVT functions at 0x%lx\n", addr);
366 return -EINVAL;
367 }
368
369 return 0;
370 }
371
save_previous_kprobe(struct kprobe_ctlblk * kcb)372 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
373 {
374 unsigned int i;
375 i = atomic_add_return(1, &kcb->prev_kprobe_index);
376 kcb->prev_kprobe[i-1].kp = kprobe_running();
377 kcb->prev_kprobe[i-1].status = kcb->kprobe_status;
378 }
379
restore_previous_kprobe(struct kprobe_ctlblk * kcb)380 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
381 {
382 unsigned int i;
383 i = atomic_read(&kcb->prev_kprobe_index);
384 __this_cpu_write(current_kprobe, kcb->prev_kprobe[i-1].kp);
385 kcb->kprobe_status = kcb->prev_kprobe[i-1].status;
386 atomic_sub(1, &kcb->prev_kprobe_index);
387 }
388
set_current_kprobe(struct kprobe * p,struct kprobe_ctlblk * kcb)389 static void __kprobes set_current_kprobe(struct kprobe *p,
390 struct kprobe_ctlblk *kcb)
391 {
392 __this_cpu_write(current_kprobe, p);
393 }
394
kretprobe_trampoline(void)395 static void kretprobe_trampoline(void)
396 {
397 }
398
trampoline_probe_handler(struct kprobe * p,struct pt_regs * regs)399 int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
400 {
401 regs->cr_iip = __kretprobe_trampoline_handler(regs,
402 dereference_function_descriptor(kretprobe_trampoline), NULL);
403 /*
404 * By returning a non-zero value, we are telling
405 * kprobe_handler() that we don't want the post_handler
406 * to run (and have re-enabled preemption)
407 */
408 return 1;
409 }
410
arch_prepare_kretprobe(struct kretprobe_instance * ri,struct pt_regs * regs)411 void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
412 struct pt_regs *regs)
413 {
414 ri->ret_addr = (kprobe_opcode_t *)regs->b0;
415 ri->fp = NULL;
416
417 /* Replace the return addr with trampoline addr */
418 regs->b0 = (unsigned long)dereference_function_descriptor(kretprobe_trampoline);
419 }
420
421 /* Check the instruction in the slot is break */
__is_ia64_break_inst(bundle_t * bundle,uint slot)422 static int __kprobes __is_ia64_break_inst(bundle_t *bundle, uint slot)
423 {
424 unsigned int major_opcode;
425 unsigned int template = bundle->quad0.template;
426 unsigned long kprobe_inst;
427
428 /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
429 if (slot == 1 && bundle_encoding[template][1] == L)
430 slot++;
431
432 /* Get Kprobe probe instruction at given slot*/
433 get_kprobe_inst(bundle, slot, &kprobe_inst, &major_opcode);
434
435 /* For break instruction,
436 * Bits 37:40 Major opcode to be zero
437 * Bits 27:32 X6 to be zero
438 * Bits 32:35 X3 to be zero
439 */
440 if (major_opcode || ((kprobe_inst >> 27) & 0x1FF)) {
441 /* Not a break instruction */
442 return 0;
443 }
444
445 /* Is a break instruction */
446 return 1;
447 }
448
449 /*
450 * In this function, we check whether the target bundle modifies IP or
451 * it triggers an exception. If so, it cannot be boostable.
452 */
can_boost(bundle_t * bundle,uint slot,unsigned long bundle_addr)453 static int __kprobes can_boost(bundle_t *bundle, uint slot,
454 unsigned long bundle_addr)
455 {
456 unsigned int template = bundle->quad0.template;
457
458 do {
459 if (search_exception_tables(bundle_addr + slot) ||
460 __is_ia64_break_inst(bundle, slot))
461 return 0; /* exception may occur in this bundle*/
462 } while ((++slot) < 3);
463 template &= 0x1e;
464 if (template >= 0x10 /* including B unit */ ||
465 template == 0x04 /* including X unit */ ||
466 template == 0x06) /* undefined */
467 return 0;
468
469 return 1;
470 }
471
472 /* Prepare long jump bundle and disables other boosters if need */
prepare_booster(struct kprobe * p)473 static void __kprobes prepare_booster(struct kprobe *p)
474 {
475 unsigned long addr = (unsigned long)p->addr & ~0xFULL;
476 unsigned int slot = (unsigned long)p->addr & 0xf;
477 struct kprobe *other_kp;
478
479 if (can_boost(&p->ainsn.insn[0].bundle, slot, addr)) {
480 set_brl_inst(&p->ainsn.insn[1].bundle, (bundle_t *)addr + 1);
481 p->ainsn.inst_flag |= INST_FLAG_BOOSTABLE;
482 }
483
484 /* disables boosters in previous slots */
485 for (; addr < (unsigned long)p->addr; addr++) {
486 other_kp = get_kprobe((void *)addr);
487 if (other_kp)
488 other_kp->ainsn.inst_flag &= ~INST_FLAG_BOOSTABLE;
489 }
490 }
491
arch_prepare_kprobe(struct kprobe * p)492 int __kprobes arch_prepare_kprobe(struct kprobe *p)
493 {
494 unsigned long addr = (unsigned long) p->addr;
495 unsigned long *kprobe_addr = (unsigned long *)(addr & ~0xFULL);
496 unsigned long kprobe_inst=0;
497 unsigned int slot = addr & 0xf, template, major_opcode = 0;
498 bundle_t *bundle;
499 int qp;
500
501 bundle = &((kprobe_opcode_t *)kprobe_addr)->bundle;
502 template = bundle->quad0.template;
503
504 if(valid_kprobe_addr(template, slot, addr))
505 return -EINVAL;
506
507 /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
508 if (slot == 1 && bundle_encoding[template][1] == L)
509 slot++;
510
511 /* Get kprobe_inst and major_opcode from the bundle */
512 get_kprobe_inst(bundle, slot, &kprobe_inst, &major_opcode);
513
514 qp = unsupported_inst(template, slot, major_opcode, kprobe_inst, addr);
515 if (qp < 0)
516 return -EINVAL;
517
518 p->ainsn.insn = get_insn_slot();
519 if (!p->ainsn.insn)
520 return -ENOMEM;
521 memcpy(&p->opcode, kprobe_addr, sizeof(kprobe_opcode_t));
522 memcpy(p->ainsn.insn, kprobe_addr, sizeof(kprobe_opcode_t));
523
524 prepare_break_inst(template, slot, major_opcode, kprobe_inst, p, qp);
525
526 prepare_booster(p);
527
528 return 0;
529 }
530
arch_arm_kprobe(struct kprobe * p)531 void __kprobes arch_arm_kprobe(struct kprobe *p)
532 {
533 unsigned long arm_addr;
534 bundle_t *src, *dest;
535
536 arm_addr = ((unsigned long)p->addr) & ~0xFUL;
537 dest = &((kprobe_opcode_t *)arm_addr)->bundle;
538 src = &p->opcode.bundle;
539
540 flush_icache_range((unsigned long)p->ainsn.insn,
541 (unsigned long)p->ainsn.insn +
542 sizeof(kprobe_opcode_t) * MAX_INSN_SIZE);
543
544 switch (p->ainsn.slot) {
545 case 0:
546 dest->quad0.slot0 = src->quad0.slot0;
547 break;
548 case 1:
549 dest->quad1.slot1_p1 = src->quad1.slot1_p1;
550 break;
551 case 2:
552 dest->quad1.slot2 = src->quad1.slot2;
553 break;
554 }
555 flush_icache_range(arm_addr, arm_addr + sizeof(kprobe_opcode_t));
556 }
557
arch_disarm_kprobe(struct kprobe * p)558 void __kprobes arch_disarm_kprobe(struct kprobe *p)
559 {
560 unsigned long arm_addr;
561 bundle_t *src, *dest;
562
563 arm_addr = ((unsigned long)p->addr) & ~0xFUL;
564 dest = &((kprobe_opcode_t *)arm_addr)->bundle;
565 /* p->ainsn.insn contains the original unaltered kprobe_opcode_t */
566 src = &p->ainsn.insn->bundle;
567 switch (p->ainsn.slot) {
568 case 0:
569 dest->quad0.slot0 = src->quad0.slot0;
570 break;
571 case 1:
572 dest->quad1.slot1_p1 = src->quad1.slot1_p1;
573 break;
574 case 2:
575 dest->quad1.slot2 = src->quad1.slot2;
576 break;
577 }
578 flush_icache_range(arm_addr, arm_addr + sizeof(kprobe_opcode_t));
579 }
580
arch_remove_kprobe(struct kprobe * p)581 void __kprobes arch_remove_kprobe(struct kprobe *p)
582 {
583 if (p->ainsn.insn) {
584 free_insn_slot(p->ainsn.insn,
585 p->ainsn.inst_flag & INST_FLAG_BOOSTABLE);
586 p->ainsn.insn = NULL;
587 }
588 }
589 /*
590 * We are resuming execution after a single step fault, so the pt_regs
591 * structure reflects the register state after we executed the instruction
592 * located in the kprobe (p->ainsn.insn->bundle). We still need to adjust
593 * the ip to point back to the original stack address. To set the IP address
594 * to original stack address, handle the case where we need to fixup the
595 * relative IP address and/or fixup branch register.
596 */
resume_execution(struct kprobe * p,struct pt_regs * regs)597 static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
598 {
599 unsigned long bundle_addr = (unsigned long) (&p->ainsn.insn->bundle);
600 unsigned long resume_addr = (unsigned long)p->addr & ~0xFULL;
601 unsigned long template;
602 int slot = ((unsigned long)p->addr & 0xf);
603
604 template = p->ainsn.insn->bundle.quad0.template;
605
606 if (slot == 1 && bundle_encoding[template][1] == L)
607 slot = 2;
608
609 if (p->ainsn.inst_flag & ~INST_FLAG_BOOSTABLE) {
610
611 if (p->ainsn.inst_flag & INST_FLAG_FIX_RELATIVE_IP_ADDR) {
612 /* Fix relative IP address */
613 regs->cr_iip = (regs->cr_iip - bundle_addr) +
614 resume_addr;
615 }
616
617 if (p->ainsn.inst_flag & INST_FLAG_FIX_BRANCH_REG) {
618 /*
619 * Fix target branch register, software convention is
620 * to use either b0 or b6 or b7, so just checking
621 * only those registers
622 */
623 switch (p->ainsn.target_br_reg) {
624 case 0:
625 if ((regs->b0 == bundle_addr) ||
626 (regs->b0 == bundle_addr + 0x10)) {
627 regs->b0 = (regs->b0 - bundle_addr) +
628 resume_addr;
629 }
630 break;
631 case 6:
632 if ((regs->b6 == bundle_addr) ||
633 (regs->b6 == bundle_addr + 0x10)) {
634 regs->b6 = (regs->b6 - bundle_addr) +
635 resume_addr;
636 }
637 break;
638 case 7:
639 if ((regs->b7 == bundle_addr) ||
640 (regs->b7 == bundle_addr + 0x10)) {
641 regs->b7 = (regs->b7 - bundle_addr) +
642 resume_addr;
643 }
644 break;
645 } /* end switch */
646 }
647 goto turn_ss_off;
648 }
649
650 if (slot == 2) {
651 if (regs->cr_iip == bundle_addr + 0x10) {
652 regs->cr_iip = resume_addr + 0x10;
653 }
654 } else {
655 if (regs->cr_iip == bundle_addr) {
656 regs->cr_iip = resume_addr;
657 }
658 }
659
660 turn_ss_off:
661 /* Turn off Single Step bit */
662 ia64_psr(regs)->ss = 0;
663 }
664
prepare_ss(struct kprobe * p,struct pt_regs * regs)665 static void __kprobes prepare_ss(struct kprobe *p, struct pt_regs *regs)
666 {
667 unsigned long bundle_addr = (unsigned long) &p->ainsn.insn->bundle;
668 unsigned long slot = (unsigned long)p->addr & 0xf;
669
670 /* single step inline if break instruction */
671 if (p->ainsn.inst_flag == INST_FLAG_BREAK_INST)
672 regs->cr_iip = (unsigned long)p->addr & ~0xFULL;
673 else
674 regs->cr_iip = bundle_addr & ~0xFULL;
675
676 if (slot > 2)
677 slot = 0;
678
679 ia64_psr(regs)->ri = slot;
680
681 /* turn on single stepping */
682 ia64_psr(regs)->ss = 1;
683 }
684
is_ia64_break_inst(struct pt_regs * regs)685 static int __kprobes is_ia64_break_inst(struct pt_regs *regs)
686 {
687 unsigned int slot = ia64_psr(regs)->ri;
688 unsigned long *kprobe_addr = (unsigned long *)regs->cr_iip;
689 bundle_t bundle;
690
691 memcpy(&bundle, kprobe_addr, sizeof(bundle_t));
692
693 return __is_ia64_break_inst(&bundle, slot);
694 }
695
pre_kprobes_handler(struct die_args * args)696 static int __kprobes pre_kprobes_handler(struct die_args *args)
697 {
698 struct kprobe *p;
699 int ret = 0;
700 struct pt_regs *regs = args->regs;
701 kprobe_opcode_t *addr = (kprobe_opcode_t *)instruction_pointer(regs);
702 struct kprobe_ctlblk *kcb;
703
704 /*
705 * We don't want to be preempted for the entire
706 * duration of kprobe processing
707 */
708 preempt_disable();
709 kcb = get_kprobe_ctlblk();
710
711 /* Handle recursion cases */
712 if (kprobe_running()) {
713 p = get_kprobe(addr);
714 if (p) {
715 if ((kcb->kprobe_status == KPROBE_HIT_SS) &&
716 (p->ainsn.inst_flag == INST_FLAG_BREAK_INST)) {
717 ia64_psr(regs)->ss = 0;
718 goto no_kprobe;
719 }
720 /* We have reentered the pre_kprobe_handler(), since
721 * another probe was hit while within the handler.
722 * We here save the original kprobes variables and
723 * just single step on the instruction of the new probe
724 * without calling any user handlers.
725 */
726 save_previous_kprobe(kcb);
727 set_current_kprobe(p, kcb);
728 kprobes_inc_nmissed_count(p);
729 prepare_ss(p, regs);
730 kcb->kprobe_status = KPROBE_REENTER;
731 return 1;
732 } else if (!is_ia64_break_inst(regs)) {
733 /* The breakpoint instruction was removed by
734 * another cpu right after we hit, no further
735 * handling of this interrupt is appropriate
736 */
737 ret = 1;
738 goto no_kprobe;
739 } else {
740 /* Not our break */
741 goto no_kprobe;
742 }
743 }
744
745 p = get_kprobe(addr);
746 if (!p) {
747 if (!is_ia64_break_inst(regs)) {
748 /*
749 * The breakpoint instruction was removed right
750 * after we hit it. Another cpu has removed
751 * either a probepoint or a debugger breakpoint
752 * at this address. In either case, no further
753 * handling of this interrupt is appropriate.
754 */
755 ret = 1;
756
757 }
758
759 /* Not one of our break, let kernel handle it */
760 goto no_kprobe;
761 }
762
763 set_current_kprobe(p, kcb);
764 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
765
766 if (p->pre_handler && p->pre_handler(p, regs)) {
767 reset_current_kprobe();
768 preempt_enable_no_resched();
769 return 1;
770 }
771
772 #if !defined(CONFIG_PREEMPTION)
773 if (p->ainsn.inst_flag == INST_FLAG_BOOSTABLE && !p->post_handler) {
774 /* Boost up -- we can execute copied instructions directly */
775 ia64_psr(regs)->ri = p->ainsn.slot;
776 regs->cr_iip = (unsigned long)&p->ainsn.insn->bundle & ~0xFULL;
777 /* turn single stepping off */
778 ia64_psr(regs)->ss = 0;
779
780 reset_current_kprobe();
781 preempt_enable_no_resched();
782 return 1;
783 }
784 #endif
785 prepare_ss(p, regs);
786 kcb->kprobe_status = KPROBE_HIT_SS;
787 return 1;
788
789 no_kprobe:
790 preempt_enable_no_resched();
791 return ret;
792 }
793
post_kprobes_handler(struct pt_regs * regs)794 static int __kprobes post_kprobes_handler(struct pt_regs *regs)
795 {
796 struct kprobe *cur = kprobe_running();
797 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
798
799 if (!cur)
800 return 0;
801
802 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
803 kcb->kprobe_status = KPROBE_HIT_SSDONE;
804 cur->post_handler(cur, regs, 0);
805 }
806
807 resume_execution(cur, regs);
808
809 /*Restore back the original saved kprobes variables and continue. */
810 if (kcb->kprobe_status == KPROBE_REENTER) {
811 restore_previous_kprobe(kcb);
812 goto out;
813 }
814 reset_current_kprobe();
815
816 out:
817 preempt_enable_no_resched();
818 return 1;
819 }
820
kprobe_fault_handler(struct pt_regs * regs,int trapnr)821 int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
822 {
823 struct kprobe *cur = kprobe_running();
824 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
825
826
827 switch(kcb->kprobe_status) {
828 case KPROBE_HIT_SS:
829 case KPROBE_REENTER:
830 /*
831 * We are here because the instruction being single
832 * stepped caused a page fault. We reset the current
833 * kprobe and the instruction pointer points back to
834 * the probe address and allow the page fault handler
835 * to continue as a normal page fault.
836 */
837 regs->cr_iip = ((unsigned long)cur->addr) & ~0xFULL;
838 ia64_psr(regs)->ri = ((unsigned long)cur->addr) & 0xf;
839 if (kcb->kprobe_status == KPROBE_REENTER)
840 restore_previous_kprobe(kcb);
841 else
842 reset_current_kprobe();
843 preempt_enable_no_resched();
844 break;
845 case KPROBE_HIT_ACTIVE:
846 case KPROBE_HIT_SSDONE:
847 /*
848 * We increment the nmissed count for accounting,
849 * we can also use npre/npostfault count for accounting
850 * these specific fault cases.
851 */
852 kprobes_inc_nmissed_count(cur);
853
854 /*
855 * We come here because instructions in the pre/post
856 * handler caused the page_fault, this could happen
857 * if handler tries to access user space by
858 * copy_from_user(), get_user() etc. Let the
859 * user-specified handler try to fix it first.
860 */
861 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
862 return 1;
863 /*
864 * In case the user-specified fault handler returned
865 * zero, try to fix up.
866 */
867 if (ia64_done_with_exception(regs))
868 return 1;
869
870 /*
871 * Let ia64_do_page_fault() fix it.
872 */
873 break;
874 default:
875 break;
876 }
877
878 return 0;
879 }
880
kprobe_exceptions_notify(struct notifier_block * self,unsigned long val,void * data)881 int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
882 unsigned long val, void *data)
883 {
884 struct die_args *args = (struct die_args *)data;
885 int ret = NOTIFY_DONE;
886
887 if (args->regs && user_mode(args->regs))
888 return ret;
889
890 switch(val) {
891 case DIE_BREAK:
892 /* err is break number from ia64_bad_break() */
893 if ((args->err >> 12) == (__IA64_BREAK_KPROBE >> 12)
894 || args->err == 0)
895 if (pre_kprobes_handler(args))
896 ret = NOTIFY_STOP;
897 break;
898 case DIE_FAULT:
899 /* err is vector number from ia64_fault() */
900 if (args->err == 36)
901 if (post_kprobes_handler(args->regs))
902 ret = NOTIFY_STOP;
903 break;
904 default:
905 break;
906 }
907 return ret;
908 }
909
arch_deref_entry_point(void * entry)910 unsigned long arch_deref_entry_point(void *entry)
911 {
912 return ((struct fnptr *)entry)->ip;
913 }
914
915 static struct kprobe trampoline_p = {
916 .pre_handler = trampoline_probe_handler
917 };
918
arch_init_kprobes(void)919 int __init arch_init_kprobes(void)
920 {
921 trampoline_p.addr =
922 dereference_function_descriptor(kretprobe_trampoline);
923 return register_kprobe(&trampoline_p);
924 }
925
arch_trampoline_kprobe(struct kprobe * p)926 int __kprobes arch_trampoline_kprobe(struct kprobe *p)
927 {
928 if (p->addr ==
929 dereference_function_descriptor(kretprobe_trampoline))
930 return 1;
931
932 return 0;
933 }
934