Commit | Line | Data |
---|---|---|
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 | ||
26 | #include <linux/config.h> | |
27 | #include <linux/kprobes.h> | |
28 | #include <linux/ptrace.h> | |
29 | #include <linux/spinlock.h> | |
30 | #include <linux/string.h> | |
31 | #include <linux/slab.h> | |
32 | #include <linux/preempt.h> | |
33 | #include <linux/moduleloader.h> | |
34 | ||
35 | #include <asm/pgtable.h> | |
36 | #include <asm/kdebug.h> | |
c7b645f9 | 37 | #include <asm/sections.h> |
fd7b231f | 38 | |
b2761dc2 AK |
39 | extern void jprobe_inst_return(void); |
40 | ||
fd7b231f AK |
41 | /* kprobe_status settings */ |
42 | #define KPROBE_HIT_ACTIVE 0x00000001 | |
43 | #define KPROBE_HIT_SS 0x00000002 | |
44 | ||
852caccc AK |
45 | static struct kprobe *current_kprobe, *kprobe_prev; |
46 | static unsigned long kprobe_status, kprobe_status_prev; | |
b2761dc2 | 47 | static struct pt_regs jprobe_saved_regs; |
fd7b231f AK |
48 | |
49 | enum instruction_type {A, I, M, F, B, L, X, u}; | |
50 | static enum instruction_type bundle_encoding[32][3] = { | |
51 | { M, I, I }, /* 00 */ | |
52 | { M, I, I }, /* 01 */ | |
53 | { M, I, I }, /* 02 */ | |
54 | { M, I, I }, /* 03 */ | |
55 | { M, L, X }, /* 04 */ | |
56 | { M, L, X }, /* 05 */ | |
57 | { u, u, u }, /* 06 */ | |
58 | { u, u, u }, /* 07 */ | |
59 | { M, M, I }, /* 08 */ | |
60 | { M, M, I }, /* 09 */ | |
61 | { M, M, I }, /* 0A */ | |
62 | { M, M, I }, /* 0B */ | |
63 | { M, F, I }, /* 0C */ | |
64 | { M, F, I }, /* 0D */ | |
65 | { M, M, F }, /* 0E */ | |
66 | { M, M, F }, /* 0F */ | |
67 | { M, I, B }, /* 10 */ | |
68 | { M, I, B }, /* 11 */ | |
69 | { M, B, B }, /* 12 */ | |
70 | { M, B, B }, /* 13 */ | |
71 | { u, u, u }, /* 14 */ | |
72 | { u, u, u }, /* 15 */ | |
73 | { B, B, B }, /* 16 */ | |
74 | { B, B, B }, /* 17 */ | |
75 | { M, M, B }, /* 18 */ | |
76 | { M, M, B }, /* 19 */ | |
77 | { u, u, u }, /* 1A */ | |
78 | { u, u, u }, /* 1B */ | |
79 | { M, F, B }, /* 1C */ | |
80 | { M, F, B }, /* 1D */ | |
81 | { u, u, u }, /* 1E */ | |
82 | { u, u, u }, /* 1F */ | |
83 | }; | |
84 | ||
a5403183 AK |
85 | /* |
86 | * In this function we check to see if the instruction | |
87 | * is IP relative instruction and update the kprobe | |
88 | * inst flag accordingly | |
89 | */ | |
1f7ad57b PP |
90 | static void __kprobes update_kprobe_inst_flag(uint template, uint slot, |
91 | uint major_opcode, | |
92 | unsigned long kprobe_inst, | |
93 | struct kprobe *p) | |
fd7b231f | 94 | { |
8bc76772 RL |
95 | p->ainsn.inst_flag = 0; |
96 | p->ainsn.target_br_reg = 0; | |
fd7b231f | 97 | |
deac66ae KA |
98 | /* Check for Break instruction |
99 | * Bits 37:40 Major opcode to be zero | |
100 | * Bits 27:32 X6 to be zero | |
101 | * Bits 32:35 X3 to be zero | |
102 | */ | |
103 | if ((!major_opcode) && (!((kprobe_inst >> 27) & 0x1FF)) ) { | |
104 | /* is a break instruction */ | |
105 | p->ainsn.inst_flag |= INST_FLAG_BREAK_INST; | |
106 | return; | |
107 | } | |
108 | ||
a5403183 AK |
109 | if (bundle_encoding[template][slot] == B) { |
110 | switch (major_opcode) { | |
111 | case INDIRECT_CALL_OPCODE: | |
112 | p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG; | |
113 | p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7); | |
114 | break; | |
115 | case IP_RELATIVE_PREDICT_OPCODE: | |
116 | case IP_RELATIVE_BRANCH_OPCODE: | |
117 | p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR; | |
118 | break; | |
119 | case IP_RELATIVE_CALL_OPCODE: | |
120 | p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR; | |
121 | p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG; | |
122 | p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7); | |
123 | break; | |
124 | } | |
125 | } else if (bundle_encoding[template][slot] == X) { | |
126 | switch (major_opcode) { | |
127 | case LONG_CALL_OPCODE: | |
128 | p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG; | |
129 | p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7); | |
130 | break; | |
131 | } | |
132 | } | |
133 | return; | |
134 | } | |
fd7b231f | 135 | |
708de8f1 AK |
136 | /* |
137 | * In this function we check to see if the instruction | |
138 | * on which we are inserting kprobe is supported. | |
139 | * Returns 0 if supported | |
140 | * Returns -EINVAL if unsupported | |
141 | */ | |
1f7ad57b PP |
142 | static int __kprobes unsupported_inst(uint template, uint slot, |
143 | uint major_opcode, | |
144 | unsigned long kprobe_inst, | |
145 | struct kprobe *p) | |
708de8f1 AK |
146 | { |
147 | unsigned long addr = (unsigned long)p->addr; | |
148 | ||
149 | if (bundle_encoding[template][slot] == I) { | |
150 | switch (major_opcode) { | |
151 | case 0x0: //I_UNIT_MISC_OPCODE: | |
152 | /* | |
153 | * Check for Integer speculation instruction | |
154 | * - Bit 33-35 to be equal to 0x1 | |
155 | */ | |
156 | if (((kprobe_inst >> 33) & 0x7) == 1) { | |
157 | printk(KERN_WARNING | |
158 | "Kprobes on speculation inst at <0x%lx> not supported\n", | |
159 | addr); | |
160 | return -EINVAL; | |
161 | } | |
162 | ||
163 | /* | |
164 | * IP relative mov instruction | |
165 | * - Bit 27-35 to be equal to 0x30 | |
166 | */ | |
167 | if (((kprobe_inst >> 27) & 0x1FF) == 0x30) { | |
168 | printk(KERN_WARNING | |
169 | "Kprobes on \"mov r1=ip\" at <0x%lx> not supported\n", | |
170 | addr); | |
171 | return -EINVAL; | |
172 | ||
173 | } | |
174 | } | |
175 | } | |
176 | return 0; | |
177 | } | |
178 | ||
179 | ||
1674eafc AK |
180 | /* |
181 | * In this function we check to see if the instruction | |
182 | * (qp) cmpx.crel.ctype p1,p2=r2,r3 | |
183 | * on which we are inserting kprobe is cmp instruction | |
184 | * with ctype as unc. | |
185 | */ | |
1f7ad57b PP |
186 | static uint __kprobes is_cmp_ctype_unc_inst(uint template, uint slot, |
187 | uint major_opcode, | |
188 | unsigned long kprobe_inst) | |
1674eafc AK |
189 | { |
190 | cmp_inst_t cmp_inst; | |
191 | uint ctype_unc = 0; | |
192 | ||
193 | if (!((bundle_encoding[template][slot] == I) || | |
194 | (bundle_encoding[template][slot] == M))) | |
195 | goto out; | |
196 | ||
197 | if (!((major_opcode == 0xC) || (major_opcode == 0xD) || | |
198 | (major_opcode == 0xE))) | |
199 | goto out; | |
200 | ||
201 | cmp_inst.l = kprobe_inst; | |
202 | if ((cmp_inst.f.x2 == 0) || (cmp_inst.f.x2 == 1)) { | |
203 | /* Integere compare - Register Register (A6 type)*/ | |
204 | if ((cmp_inst.f.tb == 0) && (cmp_inst.f.ta == 0) | |
205 | &&(cmp_inst.f.c == 1)) | |
206 | ctype_unc = 1; | |
207 | } else if ((cmp_inst.f.x2 == 2)||(cmp_inst.f.x2 == 3)) { | |
208 | /* Integere compare - Immediate Register (A8 type)*/ | |
209 | if ((cmp_inst.f.ta == 0) &&(cmp_inst.f.c == 1)) | |
210 | ctype_unc = 1; | |
211 | } | |
212 | out: | |
213 | return ctype_unc; | |
214 | } | |
215 | ||
a5403183 AK |
216 | /* |
217 | * In this function we override the bundle with | |
218 | * the break instruction at the given slot. | |
219 | */ | |
1f7ad57b PP |
220 | static void __kprobes prepare_break_inst(uint template, uint slot, |
221 | uint major_opcode, | |
222 | unsigned long kprobe_inst, | |
223 | struct kprobe *p) | |
a5403183 AK |
224 | { |
225 | unsigned long break_inst = BREAK_INST; | |
226 | bundle_t *bundle = &p->ainsn.insn.bundle; | |
227 | ||
228 | /* | |
229 | * Copy the original kprobe_inst qualifying predicate(qp) | |
1674eafc AK |
230 | * to the break instruction iff !is_cmp_ctype_unc_inst |
231 | * because for cmp instruction with ctype equal to unc, | |
232 | * which is a special instruction always needs to be | |
233 | * executed regradless of qp | |
a5403183 | 234 | */ |
1674eafc AK |
235 | if (!is_cmp_ctype_unc_inst(template, slot, major_opcode, kprobe_inst)) |
236 | break_inst |= (0x3f & kprobe_inst); | |
a5403183 AK |
237 | |
238 | switch (slot) { | |
239 | case 0: | |
240 | bundle->quad0.slot0 = break_inst; | |
241 | break; | |
242 | case 1: | |
243 | bundle->quad0.slot1_p0 = break_inst; | |
244 | bundle->quad1.slot1_p1 = break_inst >> (64-46); | |
245 | break; | |
246 | case 2: | |
247 | bundle->quad1.slot2 = break_inst; | |
248 | break; | |
8bc76772 | 249 | } |
cd2675bf | 250 | |
a5403183 AK |
251 | /* |
252 | * Update the instruction flag, so that we can | |
253 | * emulate the instruction properly after we | |
254 | * single step on original instruction | |
255 | */ | |
256 | update_kprobe_inst_flag(template, slot, major_opcode, kprobe_inst, p); | |
257 | } | |
258 | ||
259 | static inline void get_kprobe_inst(bundle_t *bundle, uint slot, | |
260 | unsigned long *kprobe_inst, uint *major_opcode) | |
261 | { | |
262 | unsigned long kprobe_inst_p0, kprobe_inst_p1; | |
263 | unsigned int template; | |
264 | ||
265 | template = bundle->quad0.template; | |
fd7b231f | 266 | |
fd7b231f | 267 | switch (slot) { |
a5403183 AK |
268 | case 0: |
269 | *major_opcode = (bundle->quad0.slot0 >> SLOT0_OPCODE_SHIFT); | |
270 | *kprobe_inst = bundle->quad0.slot0; | |
fd7b231f | 271 | break; |
a5403183 AK |
272 | case 1: |
273 | *major_opcode = (bundle->quad1.slot1_p1 >> SLOT1_p1_OPCODE_SHIFT); | |
274 | kprobe_inst_p0 = bundle->quad0.slot1_p0; | |
275 | kprobe_inst_p1 = bundle->quad1.slot1_p1; | |
276 | *kprobe_inst = kprobe_inst_p0 | (kprobe_inst_p1 << (64-46)); | |
fd7b231f | 277 | break; |
a5403183 AK |
278 | case 2: |
279 | *major_opcode = (bundle->quad1.slot2 >> SLOT2_OPCODE_SHIFT); | |
280 | *kprobe_inst = bundle->quad1.slot2; | |
fd7b231f AK |
281 | break; |
282 | } | |
a5403183 | 283 | } |
fd7b231f | 284 | |
c7b645f9 KA |
285 | /* Returns non-zero if the addr is in the Interrupt Vector Table */ |
286 | static inline int in_ivt_functions(unsigned long addr) | |
287 | { | |
288 | return (addr >= (unsigned long)__start_ivt_text | |
289 | && addr < (unsigned long)__end_ivt_text); | |
290 | } | |
291 | ||
1f7ad57b PP |
292 | static int __kprobes valid_kprobe_addr(int template, int slot, |
293 | unsigned long addr) | |
a5403183 AK |
294 | { |
295 | if ((slot > 2) || ((bundle_encoding[template][1] == L) && slot > 1)) { | |
c7b645f9 KA |
296 | printk(KERN_WARNING "Attempting to insert unaligned kprobe " |
297 | "at 0x%lx\n", addr); | |
a5403183 | 298 | return -EINVAL; |
8bc76772 | 299 | } |
a528e21c | 300 | |
c7b645f9 KA |
301 | if (in_ivt_functions(addr)) { |
302 | printk(KERN_WARNING "Kprobes can't be inserted inside " | |
303 | "IVT functions at 0x%lx\n", addr); | |
304 | return -EINVAL; | |
305 | } | |
306 | ||
a528e21c RL |
307 | if (slot == 1 && bundle_encoding[template][1] != L) { |
308 | printk(KERN_WARNING "Inserting kprobes on slot #1 " | |
309 | "is not supported\n"); | |
310 | return -EINVAL; | |
311 | } | |
312 | ||
a5403183 AK |
313 | return 0; |
314 | } | |
315 | ||
852caccc AK |
316 | static inline void save_previous_kprobe(void) |
317 | { | |
318 | kprobe_prev = current_kprobe; | |
319 | kprobe_status_prev = kprobe_status; | |
320 | } | |
321 | ||
322 | static inline void restore_previous_kprobe(void) | |
323 | { | |
324 | current_kprobe = kprobe_prev; | |
325 | kprobe_status = kprobe_status_prev; | |
326 | } | |
327 | ||
328 | static inline void set_current_kprobe(struct kprobe *p) | |
329 | { | |
330 | current_kprobe = p; | |
331 | } | |
332 | ||
9508dbfe RL |
333 | static void kretprobe_trampoline(void) |
334 | { | |
335 | } | |
336 | ||
337 | /* | |
338 | * At this point the target function has been tricked into | |
339 | * returning into our trampoline. Lookup the associated instance | |
340 | * and then: | |
341 | * - call the handler function | |
342 | * - cleanup by marking the instance as unused | |
343 | * - long jump back to the original return address | |
344 | */ | |
1f7ad57b | 345 | int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs) |
9508dbfe RL |
346 | { |
347 | struct kretprobe_instance *ri = NULL; | |
348 | struct hlist_head *head; | |
349 | struct hlist_node *node, *tmp; | |
350 | unsigned long orig_ret_address = 0; | |
351 | unsigned long trampoline_address = | |
352 | ((struct fnptr *)kretprobe_trampoline)->ip; | |
353 | ||
354 | head = kretprobe_inst_table_head(current); | |
355 | ||
356 | /* | |
357 | * It is possible to have multiple instances associated with a given | |
358 | * task either because an multiple functions in the call path | |
359 | * have a return probe installed on them, and/or more then one return | |
360 | * return probe was registered for a target function. | |
361 | * | |
362 | * We can handle this because: | |
363 | * - instances are always inserted at the head of the list | |
364 | * - when multiple return probes are registered for the same | |
365 | * function, the first instance's ret_addr will point to the | |
366 | * real return address, and all the rest will point to | |
367 | * kretprobe_trampoline | |
368 | */ | |
369 | hlist_for_each_entry_safe(ri, node, tmp, head, hlist) { | |
370 | if (ri->task != current) | |
371 | /* another task is sharing our hash bucket */ | |
372 | continue; | |
373 | ||
374 | if (ri->rp && ri->rp->handler) | |
375 | ri->rp->handler(ri, regs); | |
376 | ||
377 | orig_ret_address = (unsigned long)ri->ret_addr; | |
378 | recycle_rp_inst(ri); | |
379 | ||
380 | if (orig_ret_address != trampoline_address) | |
381 | /* | |
382 | * This is the real return address. Any other | |
383 | * instances associated with this task are for | |
384 | * other calls deeper on the call stack | |
385 | */ | |
386 | break; | |
387 | } | |
388 | ||
389 | BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address)); | |
390 | regs->cr_iip = orig_ret_address; | |
391 | ||
392 | unlock_kprobes(); | |
393 | preempt_enable_no_resched(); | |
394 | ||
395 | /* | |
396 | * By returning a non-zero value, we are telling | |
397 | * kprobe_handler() that we have handled unlocking | |
66ff2d06 | 398 | * and re-enabling preemption |
9508dbfe RL |
399 | */ |
400 | return 1; | |
401 | } | |
402 | ||
1f7ad57b PP |
403 | void __kprobes arch_prepare_kretprobe(struct kretprobe *rp, |
404 | struct pt_regs *regs) | |
9508dbfe RL |
405 | { |
406 | struct kretprobe_instance *ri; | |
407 | ||
408 | if ((ri = get_free_rp_inst(rp)) != NULL) { | |
409 | ri->rp = rp; | |
410 | ri->task = current; | |
411 | ri->ret_addr = (kprobe_opcode_t *)regs->b0; | |
412 | ||
413 | /* Replace the return addr with trampoline addr */ | |
414 | regs->b0 = ((struct fnptr *)kretprobe_trampoline)->ip; | |
415 | ||
416 | add_rp_inst(ri); | |
417 | } else { | |
418 | rp->nmissed++; | |
419 | } | |
420 | } | |
421 | ||
1f7ad57b | 422 | int __kprobes arch_prepare_kprobe(struct kprobe *p) |
a5403183 AK |
423 | { |
424 | unsigned long addr = (unsigned long) p->addr; | |
425 | unsigned long *kprobe_addr = (unsigned long *)(addr & ~0xFULL); | |
426 | unsigned long kprobe_inst=0; | |
427 | unsigned int slot = addr & 0xf, template, major_opcode = 0; | |
428 | bundle_t *bundle = &p->ainsn.insn.bundle; | |
429 | ||
430 | memcpy(&p->opcode.bundle, kprobe_addr, sizeof(bundle_t)); | |
431 | memcpy(&p->ainsn.insn.bundle, kprobe_addr, sizeof(bundle_t)); | |
432 | ||
433 | template = bundle->quad0.template; | |
434 | ||
435 | if(valid_kprobe_addr(template, slot, addr)) | |
436 | return -EINVAL; | |
437 | ||
438 | /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */ | |
439 | if (slot == 1 && bundle_encoding[template][1] == L) | |
440 | slot++; | |
441 | ||
442 | /* Get kprobe_inst and major_opcode from the bundle */ | |
443 | get_kprobe_inst(bundle, slot, &kprobe_inst, &major_opcode); | |
444 | ||
708de8f1 AK |
445 | if (unsupported_inst(template, slot, major_opcode, kprobe_inst, p)) |
446 | return -EINVAL; | |
447 | ||
a5403183 | 448 | prepare_break_inst(template, slot, major_opcode, kprobe_inst, p); |
8bc76772 RL |
449 | |
450 | return 0; | |
451 | } | |
452 | ||
1f7ad57b | 453 | void __kprobes arch_arm_kprobe(struct kprobe *p) |
8bc76772 RL |
454 | { |
455 | unsigned long addr = (unsigned long)p->addr; | |
456 | unsigned long arm_addr = addr & ~0xFULL; | |
457 | ||
458 | memcpy((char *)arm_addr, &p->ainsn.insn.bundle, sizeof(bundle_t)); | |
fd7b231f AK |
459 | flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t)); |
460 | } | |
461 | ||
1f7ad57b | 462 | void __kprobes arch_disarm_kprobe(struct kprobe *p) |
fd7b231f AK |
463 | { |
464 | unsigned long addr = (unsigned long)p->addr; | |
465 | unsigned long arm_addr = addr & ~0xFULL; | |
466 | ||
467 | /* p->opcode contains the original unaltered bundle */ | |
468 | memcpy((char *) arm_addr, (char *) &p->opcode.bundle, sizeof(bundle_t)); | |
469 | flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t)); | |
470 | } | |
471 | ||
1f7ad57b | 472 | void __kprobes arch_remove_kprobe(struct kprobe *p) |
fd7b231f AK |
473 | { |
474 | } | |
475 | ||
476 | /* | |
477 | * We are resuming execution after a single step fault, so the pt_regs | |
478 | * structure reflects the register state after we executed the instruction | |
479 | * located in the kprobe (p->ainsn.insn.bundle). We still need to adjust | |
cd2675bf AK |
480 | * the ip to point back to the original stack address. To set the IP address |
481 | * to original stack address, handle the case where we need to fixup the | |
482 | * relative IP address and/or fixup branch register. | |
fd7b231f | 483 | */ |
1f7ad57b | 484 | static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs) |
fd7b231f | 485 | { |
8bc76772 | 486 | unsigned long bundle_addr = ((unsigned long) (&p->opcode.bundle)) & ~0xFULL; |
cd2675bf AK |
487 | unsigned long resume_addr = (unsigned long)p->addr & ~0xFULL; |
488 | unsigned long template; | |
489 | int slot = ((unsigned long)p->addr & 0xf); | |
fd7b231f | 490 | |
cd2675bf AK |
491 | template = p->opcode.bundle.quad0.template; |
492 | ||
493 | if (slot == 1 && bundle_encoding[template][1] == L) | |
494 | slot = 2; | |
495 | ||
496 | if (p->ainsn.inst_flag) { | |
497 | ||
498 | if (p->ainsn.inst_flag & INST_FLAG_FIX_RELATIVE_IP_ADDR) { | |
499 | /* Fix relative IP address */ | |
500 | regs->cr_iip = (regs->cr_iip - bundle_addr) + resume_addr; | |
501 | } | |
502 | ||
503 | if (p->ainsn.inst_flag & INST_FLAG_FIX_BRANCH_REG) { | |
504 | /* | |
505 | * Fix target branch register, software convention is | |
506 | * to use either b0 or b6 or b7, so just checking | |
507 | * only those registers | |
508 | */ | |
509 | switch (p->ainsn.target_br_reg) { | |
510 | case 0: | |
511 | if ((regs->b0 == bundle_addr) || | |
512 | (regs->b0 == bundle_addr + 0x10)) { | |
513 | regs->b0 = (regs->b0 - bundle_addr) + | |
514 | resume_addr; | |
515 | } | |
516 | break; | |
517 | case 6: | |
518 | if ((regs->b6 == bundle_addr) || | |
519 | (regs->b6 == bundle_addr + 0x10)) { | |
520 | regs->b6 = (regs->b6 - bundle_addr) + | |
521 | resume_addr; | |
522 | } | |
523 | break; | |
524 | case 7: | |
525 | if ((regs->b7 == bundle_addr) || | |
526 | (regs->b7 == bundle_addr + 0x10)) { | |
527 | regs->b7 = (regs->b7 - bundle_addr) + | |
528 | resume_addr; | |
529 | } | |
530 | break; | |
531 | } /* end switch */ | |
532 | } | |
533 | goto turn_ss_off; | |
534 | } | |
fd7b231f | 535 | |
cd2675bf AK |
536 | if (slot == 2) { |
537 | if (regs->cr_iip == bundle_addr + 0x10) { | |
538 | regs->cr_iip = resume_addr + 0x10; | |
539 | } | |
540 | } else { | |
541 | if (regs->cr_iip == bundle_addr) { | |
542 | regs->cr_iip = resume_addr; | |
543 | } | |
a5403183 | 544 | } |
fd7b231f | 545 | |
cd2675bf AK |
546 | turn_ss_off: |
547 | /* Turn off Single Step bit */ | |
548 | ia64_psr(regs)->ss = 0; | |
fd7b231f AK |
549 | } |
550 | ||
1f7ad57b | 551 | static void __kprobes prepare_ss(struct kprobe *p, struct pt_regs *regs) |
fd7b231f | 552 | { |
8bc76772 | 553 | unsigned long bundle_addr = (unsigned long) &p->opcode.bundle; |
fd7b231f AK |
554 | unsigned long slot = (unsigned long)p->addr & 0xf; |
555 | ||
deac66ae KA |
556 | /* single step inline if break instruction */ |
557 | if (p->ainsn.inst_flag == INST_FLAG_BREAK_INST) | |
558 | regs->cr_iip = (unsigned long)p->addr & ~0xFULL; | |
559 | else | |
560 | regs->cr_iip = bundle_addr & ~0xFULL; | |
fd7b231f AK |
561 | |
562 | if (slot > 2) | |
563 | slot = 0; | |
564 | ||
565 | ia64_psr(regs)->ri = slot; | |
566 | ||
567 | /* turn on single stepping */ | |
568 | ia64_psr(regs)->ss = 1; | |
569 | } | |
570 | ||
661e5a3d KA |
571 | static int __kprobes is_ia64_break_inst(struct pt_regs *regs) |
572 | { | |
573 | unsigned int slot = ia64_psr(regs)->ri; | |
574 | unsigned int template, major_opcode; | |
575 | unsigned long kprobe_inst; | |
576 | unsigned long *kprobe_addr = (unsigned long *)regs->cr_iip; | |
577 | bundle_t bundle; | |
578 | ||
579 | memcpy(&bundle, kprobe_addr, sizeof(bundle_t)); | |
580 | template = bundle.quad0.template; | |
581 | ||
582 | /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */ | |
583 | if (slot == 1 && bundle_encoding[template][1] == L) | |
584 | slot++; | |
585 | ||
586 | /* Get Kprobe probe instruction at given slot*/ | |
587 | get_kprobe_inst(&bundle, slot, &kprobe_inst, &major_opcode); | |
588 | ||
589 | /* For break instruction, | |
590 | * Bits 37:40 Major opcode to be zero | |
591 | * Bits 27:32 X6 to be zero | |
592 | * Bits 32:35 X3 to be zero | |
593 | */ | |
594 | if (major_opcode || ((kprobe_inst >> 27) & 0x1FF) ) { | |
595 | /* Not a break instruction */ | |
596 | return 0; | |
597 | } | |
598 | ||
599 | /* Is a break instruction */ | |
600 | return 1; | |
601 | } | |
602 | ||
1f7ad57b | 603 | static int __kprobes pre_kprobes_handler(struct die_args *args) |
fd7b231f AK |
604 | { |
605 | struct kprobe *p; | |
606 | int ret = 0; | |
89cb14c0 | 607 | struct pt_regs *regs = args->regs; |
fd7b231f AK |
608 | kprobe_opcode_t *addr = (kprobe_opcode_t *)instruction_pointer(regs); |
609 | ||
fd7b231f AK |
610 | /* Handle recursion cases */ |
611 | if (kprobe_running()) { | |
612 | p = get_kprobe(addr); | |
613 | if (p) { | |
deac66ae KA |
614 | if ( (kprobe_status == KPROBE_HIT_SS) && |
615 | (p->ainsn.inst_flag == INST_FLAG_BREAK_INST)) { | |
616 | ia64_psr(regs)->ss = 0; | |
fd7b231f AK |
617 | unlock_kprobes(); |
618 | goto no_kprobe; | |
619 | } | |
852caccc AK |
620 | /* We have reentered the pre_kprobe_handler(), since |
621 | * another probe was hit while within the handler. | |
622 | * We here save the original kprobes variables and | |
623 | * just single step on the instruction of the new probe | |
624 | * without calling any user handlers. | |
625 | */ | |
626 | save_previous_kprobe(); | |
627 | set_current_kprobe(p); | |
628 | p->nmissed++; | |
629 | prepare_ss(p, regs); | |
630 | kprobe_status = KPROBE_REENTER; | |
631 | return 1; | |
89cb14c0 | 632 | } else if (args->err == __IA64_BREAK_JPROBE) { |
fd7b231f AK |
633 | /* |
634 | * jprobe instrumented function just completed | |
635 | */ | |
636 | p = current_kprobe; | |
637 | if (p->break_handler && p->break_handler(p, regs)) { | |
638 | goto ss_probe; | |
639 | } | |
89cb14c0 KA |
640 | } else { |
641 | /* Not our break */ | |
642 | goto no_kprobe; | |
fd7b231f AK |
643 | } |
644 | } | |
645 | ||
646 | lock_kprobes(); | |
647 | p = get_kprobe(addr); | |
648 | if (!p) { | |
649 | unlock_kprobes(); | |
661e5a3d KA |
650 | if (!is_ia64_break_inst(regs)) { |
651 | /* | |
652 | * The breakpoint instruction was removed right | |
653 | * after we hit it. Another cpu has removed | |
654 | * either a probepoint or a debugger breakpoint | |
655 | * at this address. In either case, no further | |
656 | * handling of this interrupt is appropriate. | |
657 | */ | |
658 | ret = 1; | |
659 | ||
660 | } | |
661 | ||
662 | /* Not one of our break, let kernel handle it */ | |
fd7b231f AK |
663 | goto no_kprobe; |
664 | } | |
665 | ||
66ff2d06 AM |
666 | /* |
667 | * This preempt_disable() matches the preempt_enable_no_resched() | |
668 | * in post_kprobes_handler() | |
669 | */ | |
670 | preempt_disable(); | |
fd7b231f | 671 | kprobe_status = KPROBE_HIT_ACTIVE; |
852caccc | 672 | set_current_kprobe(p); |
fd7b231f AK |
673 | |
674 | if (p->pre_handler && p->pre_handler(p, regs)) | |
675 | /* | |
676 | * Our pre-handler is specifically requesting that we just | |
9508dbfe RL |
677 | * do a return. This is used for both the jprobe pre-handler |
678 | * and the kretprobe trampoline | |
fd7b231f AK |
679 | */ |
680 | return 1; | |
681 | ||
682 | ss_probe: | |
683 | prepare_ss(p, regs); | |
684 | kprobe_status = KPROBE_HIT_SS; | |
685 | return 1; | |
686 | ||
687 | no_kprobe: | |
fd7b231f AK |
688 | return ret; |
689 | } | |
690 | ||
1f7ad57b | 691 | static int __kprobes post_kprobes_handler(struct pt_regs *regs) |
fd7b231f AK |
692 | { |
693 | if (!kprobe_running()) | |
694 | return 0; | |
695 | ||
852caccc AK |
696 | if ((kprobe_status != KPROBE_REENTER) && current_kprobe->post_handler) { |
697 | kprobe_status = KPROBE_HIT_SSDONE; | |
fd7b231f | 698 | current_kprobe->post_handler(current_kprobe, regs, 0); |
852caccc | 699 | } |
fd7b231f AK |
700 | |
701 | resume_execution(current_kprobe, regs); | |
702 | ||
852caccc AK |
703 | /*Restore back the original saved kprobes variables and continue. */ |
704 | if (kprobe_status == KPROBE_REENTER) { | |
705 | restore_previous_kprobe(); | |
706 | goto out; | |
707 | } | |
708 | ||
fd7b231f | 709 | unlock_kprobes(); |
852caccc AK |
710 | |
711 | out: | |
fd7b231f AK |
712 | preempt_enable_no_resched(); |
713 | return 1; | |
714 | } | |
715 | ||
1f7ad57b | 716 | static int __kprobes kprobes_fault_handler(struct pt_regs *regs, int trapnr) |
fd7b231f AK |
717 | { |
718 | if (!kprobe_running()) | |
719 | return 0; | |
720 | ||
721 | if (current_kprobe->fault_handler && | |
722 | current_kprobe->fault_handler(current_kprobe, regs, trapnr)) | |
723 | return 1; | |
724 | ||
725 | if (kprobe_status & KPROBE_HIT_SS) { | |
726 | resume_execution(current_kprobe, regs); | |
727 | unlock_kprobes(); | |
728 | preempt_enable_no_resched(); | |
729 | } | |
730 | ||
731 | return 0; | |
732 | } | |
733 | ||
1f7ad57b PP |
734 | int __kprobes kprobe_exceptions_notify(struct notifier_block *self, |
735 | unsigned long val, void *data) | |
fd7b231f AK |
736 | { |
737 | struct die_args *args = (struct die_args *)data; | |
66ff2d06 AM |
738 | int ret = NOTIFY_DONE; |
739 | ||
740 | preempt_disable(); | |
fd7b231f AK |
741 | switch(val) { |
742 | case DIE_BREAK: | |
89cb14c0 | 743 | if (pre_kprobes_handler(args)) |
66ff2d06 | 744 | ret = NOTIFY_STOP; |
fd7b231f AK |
745 | break; |
746 | case DIE_SS: | |
747 | if (post_kprobes_handler(args->regs)) | |
66ff2d06 | 748 | ret = NOTIFY_STOP; |
fd7b231f AK |
749 | break; |
750 | case DIE_PAGE_FAULT: | |
751 | if (kprobes_fault_handler(args->regs, args->trapnr)) | |
66ff2d06 | 752 | ret = NOTIFY_STOP; |
fd7b231f AK |
753 | default: |
754 | break; | |
755 | } | |
66ff2d06 AM |
756 | preempt_enable(); |
757 | return ret; | |
fd7b231f AK |
758 | } |
759 | ||
1f7ad57b | 760 | int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs) |
fd7b231f | 761 | { |
b2761dc2 AK |
762 | struct jprobe *jp = container_of(p, struct jprobe, kp); |
763 | unsigned long addr = ((struct fnptr *)(jp->entry))->ip; | |
fd7b231f | 764 | |
b2761dc2 AK |
765 | /* save architectural state */ |
766 | jprobe_saved_regs = *regs; | |
767 | ||
768 | /* after rfi, execute the jprobe instrumented function */ | |
769 | regs->cr_iip = addr & ~0xFULL; | |
770 | ia64_psr(regs)->ri = addr & 0xf; | |
771 | regs->r1 = ((struct fnptr *)(jp->entry))->gp; | |
772 | ||
773 | /* | |
774 | * fix the return address to our jprobe_inst_return() function | |
775 | * in the jprobes.S file | |
776 | */ | |
777 | regs->b0 = ((struct fnptr *)(jprobe_inst_return))->ip; | |
778 | ||
779 | return 1; | |
fd7b231f AK |
780 | } |
781 | ||
1f7ad57b | 782 | int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs) |
fd7b231f | 783 | { |
b2761dc2 AK |
784 | *regs = jprobe_saved_regs; |
785 | return 1; | |
fd7b231f | 786 | } |
9508dbfe RL |
787 | |
788 | static struct kprobe trampoline_p = { | |
789 | .pre_handler = trampoline_probe_handler | |
790 | }; | |
791 | ||
6772926b | 792 | int __init arch_init_kprobes(void) |
9508dbfe RL |
793 | { |
794 | trampoline_p.addr = | |
795 | (kprobe_opcode_t *)((struct fnptr *)kretprobe_trampoline)->ip; | |
796 | return register_kprobe(&trampoline_p); | |
797 | } |