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