blob: 17e70b1b8d79dfbaa739f6049933a9c7a6049f06 [file] [log] [blame]
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -07001/*
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>
Keshavamurthy Anil Sc7b645f2005-06-27 15:17:16 -070037#include <asm/sections.h>
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -070038
Anil S Keshavamurthyb2761dc2005-06-23 00:09:28 -070039extern void jprobe_inst_return(void);
40
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -080041DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
42DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -070043
44enum instruction_type {A, I, M, F, B, L, X, u};
45static enum instruction_type bundle_encoding[32][3] = {
46 { M, I, I }, /* 00 */
47 { M, I, I }, /* 01 */
48 { M, I, I }, /* 02 */
49 { M, I, I }, /* 03 */
50 { M, L, X }, /* 04 */
51 { M, L, X }, /* 05 */
52 { u, u, u }, /* 06 */
53 { u, u, u }, /* 07 */
54 { M, M, I }, /* 08 */
55 { M, M, I }, /* 09 */
56 { M, M, I }, /* 0A */
57 { M, M, I }, /* 0B */
58 { M, F, I }, /* 0C */
59 { M, F, I }, /* 0D */
60 { M, M, F }, /* 0E */
61 { M, M, F }, /* 0F */
62 { M, I, B }, /* 10 */
63 { M, I, B }, /* 11 */
64 { M, B, B }, /* 12 */
65 { M, B, B }, /* 13 */
66 { u, u, u }, /* 14 */
67 { u, u, u }, /* 15 */
68 { B, B, B }, /* 16 */
69 { B, B, B }, /* 17 */
70 { M, M, B }, /* 18 */
71 { M, M, B }, /* 19 */
72 { u, u, u }, /* 1A */
73 { u, u, u }, /* 1B */
74 { M, F, B }, /* 1C */
75 { M, F, B }, /* 1D */
76 { u, u, u }, /* 1E */
77 { u, u, u }, /* 1F */
78};
79
Anil S Keshavamurthya5403182005-06-23 00:09:32 -070080/*
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 */
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -070085static void __kprobes update_kprobe_inst_flag(uint template, uint slot,
86 uint major_opcode,
87 unsigned long kprobe_inst,
88 struct kprobe *p)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -070089{
Rusty Lynch8bc76772005-06-23 00:09:30 -070090 p->ainsn.inst_flag = 0;
91 p->ainsn.target_br_reg = 0;
92
Keshavamurthy Anil Sdeac66a2005-09-06 15:19:35 -070093 /* Check for Break instruction
94 * Bits 37:40 Major opcode to be zero
95 * Bits 27:32 X6 to be zero
96 * Bits 32:35 X3 to be zero
97 */
98 if ((!major_opcode) && (!((kprobe_inst >> 27) & 0x1FF)) ) {
99 /* is a break instruction */
100 p->ainsn.inst_flag |= INST_FLAG_BREAK_INST;
101 return;
102 }
103
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700104 if (bundle_encoding[template][slot] == B) {
105 switch (major_opcode) {
106 case INDIRECT_CALL_OPCODE:
107 p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
108 p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
109 break;
110 case IP_RELATIVE_PREDICT_OPCODE:
111 case IP_RELATIVE_BRANCH_OPCODE:
112 p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;
113 break;
114 case IP_RELATIVE_CALL_OPCODE:
115 p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;
116 p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
117 p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
118 break;
119 }
120 } else if (bundle_encoding[template][slot] == X) {
121 switch (major_opcode) {
122 case LONG_CALL_OPCODE:
123 p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
124 p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
125 break;
126 }
127 }
128 return;
129}
Rusty Lynch8bc76772005-06-23 00:09:30 -0700130
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700131/*
Anil S Keshavamurthy1674eaf2005-06-23 00:09:33 -0700132 * In this function we check to see if the instruction
Anil S Keshavamurthy708de8f2005-06-23 00:09:34 -0700133 * on which we are inserting kprobe is supported.
134 * Returns 0 if supported
135 * Returns -EINVAL if unsupported
136 */
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700137static int __kprobes unsupported_inst(uint template, uint slot,
138 uint major_opcode,
139 unsigned long kprobe_inst,
140 struct kprobe *p)
Anil S Keshavamurthy708de8f2005-06-23 00:09:34 -0700141{
142 unsigned long addr = (unsigned long)p->addr;
143
144 if (bundle_encoding[template][slot] == I) {
145 switch (major_opcode) {
146 case 0x0: //I_UNIT_MISC_OPCODE:
147 /*
148 * Check for Integer speculation instruction
149 * - Bit 33-35 to be equal to 0x1
150 */
151 if (((kprobe_inst >> 33) & 0x7) == 1) {
152 printk(KERN_WARNING
153 "Kprobes on speculation inst at <0x%lx> not supported\n",
154 addr);
155 return -EINVAL;
156 }
157
158 /*
159 * IP relative mov instruction
160 * - Bit 27-35 to be equal to 0x30
161 */
162 if (((kprobe_inst >> 27) & 0x1FF) == 0x30) {
163 printk(KERN_WARNING
164 "Kprobes on \"mov r1=ip\" at <0x%lx> not supported\n",
165 addr);
166 return -EINVAL;
167
168 }
169 }
170 }
171 return 0;
172}
173
174
175/*
176 * In this function we check to see if the instruction
Anil S Keshavamurthy1674eaf2005-06-23 00:09:33 -0700177 * (qp) cmpx.crel.ctype p1,p2=r2,r3
178 * on which we are inserting kprobe is cmp instruction
179 * with ctype as unc.
180 */
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700181static uint __kprobes is_cmp_ctype_unc_inst(uint template, uint slot,
182 uint major_opcode,
183 unsigned long kprobe_inst)
Anil S Keshavamurthy1674eaf2005-06-23 00:09:33 -0700184{
185 cmp_inst_t cmp_inst;
186 uint ctype_unc = 0;
187
188 if (!((bundle_encoding[template][slot] == I) ||
189 (bundle_encoding[template][slot] == M)))
190 goto out;
191
192 if (!((major_opcode == 0xC) || (major_opcode == 0xD) ||
193 (major_opcode == 0xE)))
194 goto out;
195
196 cmp_inst.l = kprobe_inst;
197 if ((cmp_inst.f.x2 == 0) || (cmp_inst.f.x2 == 1)) {
198 /* Integere compare - Register Register (A6 type)*/
199 if ((cmp_inst.f.tb == 0) && (cmp_inst.f.ta == 0)
200 &&(cmp_inst.f.c == 1))
201 ctype_unc = 1;
202 } else if ((cmp_inst.f.x2 == 2)||(cmp_inst.f.x2 == 3)) {
203 /* Integere compare - Immediate Register (A8 type)*/
204 if ((cmp_inst.f.ta == 0) &&(cmp_inst.f.c == 1))
205 ctype_unc = 1;
206 }
207out:
208 return ctype_unc;
209}
210
211/*
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700212 * In this function we override the bundle with
213 * the break instruction at the given slot.
214 */
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700215static void __kprobes prepare_break_inst(uint template, uint slot,
216 uint major_opcode,
217 unsigned long kprobe_inst,
218 struct kprobe *p)
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700219{
220 unsigned long break_inst = BREAK_INST;
221 bundle_t *bundle = &p->ainsn.insn.bundle;
222
223 /*
224 * Copy the original kprobe_inst qualifying predicate(qp)
Anil S Keshavamurthy1674eaf2005-06-23 00:09:33 -0700225 * to the break instruction iff !is_cmp_ctype_unc_inst
226 * because for cmp instruction with ctype equal to unc,
227 * which is a special instruction always needs to be
228 * executed regradless of qp
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700229 */
Anil S Keshavamurthy1674eaf2005-06-23 00:09:33 -0700230 if (!is_cmp_ctype_unc_inst(template, slot, major_opcode, kprobe_inst))
231 break_inst |= (0x3f & kprobe_inst);
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700232
233 switch (slot) {
234 case 0:
235 bundle->quad0.slot0 = break_inst;
236 break;
237 case 1:
238 bundle->quad0.slot1_p0 = break_inst;
239 bundle->quad1.slot1_p1 = break_inst >> (64-46);
240 break;
241 case 2:
242 bundle->quad1.slot2 = break_inst;
243 break;
244 }
245
246 /*
247 * Update the instruction flag, so that we can
248 * emulate the instruction properly after we
249 * single step on original instruction
250 */
251 update_kprobe_inst_flag(template, slot, major_opcode, kprobe_inst, p);
252}
253
254static inline void get_kprobe_inst(bundle_t *bundle, uint slot,
255 unsigned long *kprobe_inst, uint *major_opcode)
256{
257 unsigned long kprobe_inst_p0, kprobe_inst_p1;
258 unsigned int template;
259
260 template = bundle->quad0.template;
261
262 switch (slot) {
263 case 0:
264 *major_opcode = (bundle->quad0.slot0 >> SLOT0_OPCODE_SHIFT);
265 *kprobe_inst = bundle->quad0.slot0;
266 break;
267 case 1:
268 *major_opcode = (bundle->quad1.slot1_p1 >> SLOT1_p1_OPCODE_SHIFT);
269 kprobe_inst_p0 = bundle->quad0.slot1_p0;
270 kprobe_inst_p1 = bundle->quad1.slot1_p1;
271 *kprobe_inst = kprobe_inst_p0 | (kprobe_inst_p1 << (64-46));
272 break;
273 case 2:
274 *major_opcode = (bundle->quad1.slot2 >> SLOT2_OPCODE_SHIFT);
275 *kprobe_inst = bundle->quad1.slot2;
276 break;
277 }
278}
279
Keshavamurthy Anil Sc7b645f2005-06-27 15:17:16 -0700280/* Returns non-zero if the addr is in the Interrupt Vector Table */
281static inline int in_ivt_functions(unsigned long addr)
282{
283 return (addr >= (unsigned long)__start_ivt_text
284 && addr < (unsigned long)__end_ivt_text);
285}
286
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700287static int __kprobes valid_kprobe_addr(int template, int slot,
288 unsigned long addr)
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700289{
290 if ((slot > 2) || ((bundle_encoding[template][1] == L) && slot > 1)) {
Keshavamurthy Anil Sc7b645f2005-06-27 15:17:16 -0700291 printk(KERN_WARNING "Attempting to insert unaligned kprobe "
292 "at 0x%lx\n", addr);
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700293 return -EINVAL;
294 }
Rusty Lyncha528e212005-06-27 15:17:15 -0700295
Keshavamurthy Anil Sc7b645f2005-06-27 15:17:16 -0700296 if (in_ivt_functions(addr)) {
297 printk(KERN_WARNING "Kprobes can't be inserted inside "
298 "IVT functions at 0x%lx\n", addr);
299 return -EINVAL;
300 }
301
Rusty Lyncha528e212005-06-27 15:17:15 -0700302 if (slot == 1 && bundle_encoding[template][1] != L) {
303 printk(KERN_WARNING "Inserting kprobes on slot #1 "
304 "is not supported\n");
305 return -EINVAL;
306 }
307
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700308 return 0;
309}
Rusty Lynch8bc76772005-06-23 00:09:30 -0700310
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800311static inline void save_previous_kprobe(struct kprobe_ctlblk *kcb)
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700312{
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800313 kcb->prev_kprobe.kp = kprobe_running();
314 kcb->prev_kprobe.status = kcb->kprobe_status;
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700315}
316
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800317static inline void restore_previous_kprobe(struct kprobe_ctlblk *kcb)
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700318{
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800319 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
320 kcb->kprobe_status = kcb->prev_kprobe.status;
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700321}
322
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800323static inline void set_current_kprobe(struct kprobe *p,
324 struct kprobe_ctlblk *kcb)
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700325{
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800326 __get_cpu_var(current_kprobe) = p;
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700327}
328
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700329static void kretprobe_trampoline(void)
330{
331}
332
333/*
334 * At this point the target function has been tricked into
335 * returning into our trampoline. Lookup the associated instance
336 * and then:
337 * - call the handler function
338 * - cleanup by marking the instance as unused
339 * - long jump back to the original return address
340 */
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700341int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700342{
343 struct kretprobe_instance *ri = NULL;
344 struct hlist_head *head;
345 struct hlist_node *node, *tmp;
346 unsigned long orig_ret_address = 0;
347 unsigned long trampoline_address =
348 ((struct fnptr *)kretprobe_trampoline)->ip;
349
350 head = kretprobe_inst_table_head(current);
351
352 /*
353 * It is possible to have multiple instances associated with a given
354 * task either because an multiple functions in the call path
355 * have a return probe installed on them, and/or more then one return
356 * return probe was registered for a target function.
357 *
358 * We can handle this because:
359 * - instances are always inserted at the head of the list
360 * - when multiple return probes are registered for the same
361 * function, the first instance's ret_addr will point to the
362 * real return address, and all the rest will point to
363 * kretprobe_trampoline
364 */
365 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
366 if (ri->task != current)
367 /* another task is sharing our hash bucket */
368 continue;
369
370 if (ri->rp && ri->rp->handler)
371 ri->rp->handler(ri, regs);
372
373 orig_ret_address = (unsigned long)ri->ret_addr;
374 recycle_rp_inst(ri);
375
376 if (orig_ret_address != trampoline_address)
377 /*
378 * This is the real return address. Any other
379 * instances associated with this task are for
380 * other calls deeper on the call stack
381 */
382 break;
383 }
384
385 BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
386 regs->cr_iip = orig_ret_address;
387
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800388 reset_current_kprobe();
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700389 unlock_kprobes();
390 preempt_enable_no_resched();
391
392 /*
393 * By returning a non-zero value, we are telling
394 * kprobe_handler() that we have handled unlocking
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800395 * and re-enabling preemption
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700396 */
397 return 1;
398}
399
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700400void __kprobes arch_prepare_kretprobe(struct kretprobe *rp,
401 struct pt_regs *regs)
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700402{
403 struct kretprobe_instance *ri;
404
405 if ((ri = get_free_rp_inst(rp)) != NULL) {
406 ri->rp = rp;
407 ri->task = current;
408 ri->ret_addr = (kprobe_opcode_t *)regs->b0;
409
410 /* Replace the return addr with trampoline addr */
411 regs->b0 = ((struct fnptr *)kretprobe_trampoline)->ip;
412
413 add_rp_inst(ri);
414 } else {
415 rp->nmissed++;
416 }
417}
418
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700419int __kprobes arch_prepare_kprobe(struct kprobe *p)
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700420{
421 unsigned long addr = (unsigned long) p->addr;
422 unsigned long *kprobe_addr = (unsigned long *)(addr & ~0xFULL);
423 unsigned long kprobe_inst=0;
424 unsigned int slot = addr & 0xf, template, major_opcode = 0;
425 bundle_t *bundle = &p->ainsn.insn.bundle;
Rusty Lynch8bc76772005-06-23 00:09:30 -0700426
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700427 memcpy(&p->opcode.bundle, kprobe_addr, sizeof(bundle_t));
428 memcpy(&p->ainsn.insn.bundle, kprobe_addr, sizeof(bundle_t));
Rusty Lynch8bc76772005-06-23 00:09:30 -0700429
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700430 template = bundle->quad0.template;
431
432 if(valid_kprobe_addr(template, slot, addr))
433 return -EINVAL;
434
435 /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
436 if (slot == 1 && bundle_encoding[template][1] == L)
437 slot++;
438
439 /* Get kprobe_inst and major_opcode from the bundle */
440 get_kprobe_inst(bundle, slot, &kprobe_inst, &major_opcode);
441
Anil S Keshavamurthy708de8f2005-06-23 00:09:34 -0700442 if (unsupported_inst(template, slot, major_opcode, kprobe_inst, p))
443 return -EINVAL;
444
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700445 prepare_break_inst(template, slot, major_opcode, kprobe_inst, p);
Rusty Lynch8bc76772005-06-23 00:09:30 -0700446
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700447 return 0;
448}
449
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700450void __kprobes arch_arm_kprobe(struct kprobe *p)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700451{
452 unsigned long addr = (unsigned long)p->addr;
453 unsigned long arm_addr = addr & ~0xFULL;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700454
Rusty Lynch8bc76772005-06-23 00:09:30 -0700455 memcpy((char *)arm_addr, &p->ainsn.insn.bundle, sizeof(bundle_t));
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700456 flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t));
457}
458
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700459void __kprobes arch_disarm_kprobe(struct kprobe *p)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700460{
461 unsigned long addr = (unsigned long)p->addr;
462 unsigned long arm_addr = addr & ~0xFULL;
463
464 /* p->opcode contains the original unaltered bundle */
465 memcpy((char *) arm_addr, (char *) &p->opcode.bundle, sizeof(bundle_t));
466 flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t));
467}
468
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700469void __kprobes arch_remove_kprobe(struct kprobe *p)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700470{
471}
472
473/*
474 * We are resuming execution after a single step fault, so the pt_regs
475 * structure reflects the register state after we executed the instruction
476 * located in the kprobe (p->ainsn.insn.bundle). We still need to adjust
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700477 * the ip to point back to the original stack address. To set the IP address
478 * to original stack address, handle the case where we need to fixup the
479 * relative IP address and/or fixup branch register.
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700480 */
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700481static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700482{
Rusty Lynch8bc76772005-06-23 00:09:30 -0700483 unsigned long bundle_addr = ((unsigned long) (&p->opcode.bundle)) & ~0xFULL;
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700484 unsigned long resume_addr = (unsigned long)p->addr & ~0xFULL;
485 unsigned long template;
486 int slot = ((unsigned long)p->addr & 0xf);
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700487
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700488 template = p->opcode.bundle.quad0.template;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700489
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700490 if (slot == 1 && bundle_encoding[template][1] == L)
491 slot = 2;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700492
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700493 if (p->ainsn.inst_flag) {
494
495 if (p->ainsn.inst_flag & INST_FLAG_FIX_RELATIVE_IP_ADDR) {
496 /* Fix relative IP address */
497 regs->cr_iip = (regs->cr_iip - bundle_addr) + resume_addr;
498 }
499
500 if (p->ainsn.inst_flag & INST_FLAG_FIX_BRANCH_REG) {
501 /*
502 * Fix target branch register, software convention is
503 * to use either b0 or b6 or b7, so just checking
504 * only those registers
505 */
506 switch (p->ainsn.target_br_reg) {
507 case 0:
508 if ((regs->b0 == bundle_addr) ||
509 (regs->b0 == bundle_addr + 0x10)) {
510 regs->b0 = (regs->b0 - bundle_addr) +
511 resume_addr;
512 }
513 break;
514 case 6:
515 if ((regs->b6 == bundle_addr) ||
516 (regs->b6 == bundle_addr + 0x10)) {
517 regs->b6 = (regs->b6 - bundle_addr) +
518 resume_addr;
519 }
520 break;
521 case 7:
522 if ((regs->b7 == bundle_addr) ||
523 (regs->b7 == bundle_addr + 0x10)) {
524 regs->b7 = (regs->b7 - bundle_addr) +
525 resume_addr;
526 }
527 break;
528 } /* end switch */
529 }
530 goto turn_ss_off;
531 }
532
533 if (slot == 2) {
534 if (regs->cr_iip == bundle_addr + 0x10) {
535 regs->cr_iip = resume_addr + 0x10;
536 }
537 } else {
538 if (regs->cr_iip == bundle_addr) {
539 regs->cr_iip = resume_addr;
540 }
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700541 }
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700542
543turn_ss_off:
544 /* Turn off Single Step bit */
545 ia64_psr(regs)->ss = 0;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700546}
547
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700548static void __kprobes prepare_ss(struct kprobe *p, struct pt_regs *regs)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700549{
Rusty Lynch8bc76772005-06-23 00:09:30 -0700550 unsigned long bundle_addr = (unsigned long) &p->opcode.bundle;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700551 unsigned long slot = (unsigned long)p->addr & 0xf;
552
Keshavamurthy Anil Sdeac66a2005-09-06 15:19:35 -0700553 /* single step inline if break instruction */
554 if (p->ainsn.inst_flag == INST_FLAG_BREAK_INST)
555 regs->cr_iip = (unsigned long)p->addr & ~0xFULL;
556 else
557 regs->cr_iip = bundle_addr & ~0xFULL;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700558
559 if (slot > 2)
560 slot = 0;
561
562 ia64_psr(regs)->ri = slot;
563
564 /* turn on single stepping */
565 ia64_psr(regs)->ss = 1;
566}
567
Keshavamurthy Anil S661e5a32005-09-06 15:19:32 -0700568static int __kprobes is_ia64_break_inst(struct pt_regs *regs)
569{
570 unsigned int slot = ia64_psr(regs)->ri;
571 unsigned int template, major_opcode;
572 unsigned long kprobe_inst;
573 unsigned long *kprobe_addr = (unsigned long *)regs->cr_iip;
574 bundle_t bundle;
575
576 memcpy(&bundle, kprobe_addr, sizeof(bundle_t));
577 template = bundle.quad0.template;
578
579 /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
580 if (slot == 1 && bundle_encoding[template][1] == L)
581 slot++;
582
583 /* Get Kprobe probe instruction at given slot*/
584 get_kprobe_inst(&bundle, slot, &kprobe_inst, &major_opcode);
585
586 /* For break instruction,
587 * Bits 37:40 Major opcode to be zero
588 * Bits 27:32 X6 to be zero
589 * Bits 32:35 X3 to be zero
590 */
591 if (major_opcode || ((kprobe_inst >> 27) & 0x1FF) ) {
592 /* Not a break instruction */
593 return 0;
594 }
595
596 /* Is a break instruction */
597 return 1;
598}
599
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700600static int __kprobes pre_kprobes_handler(struct die_args *args)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700601{
602 struct kprobe *p;
603 int ret = 0;
Keshavamurthy Anil S89cb14c2005-06-23 00:09:35 -0700604 struct pt_regs *regs = args->regs;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700605 kprobe_opcode_t *addr = (kprobe_opcode_t *)instruction_pointer(regs);
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800606 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700607
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700608 /* Handle recursion cases */
609 if (kprobe_running()) {
610 p = get_kprobe(addr);
611 if (p) {
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800612 if ((kcb->kprobe_status == KPROBE_HIT_SS) &&
Keshavamurthy Anil Sdeac66a2005-09-06 15:19:35 -0700613 (p->ainsn.inst_flag == INST_FLAG_BREAK_INST)) {
614 ia64_psr(regs)->ss = 0;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700615 unlock_kprobes();
616 goto no_kprobe;
617 }
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700618 /* We have reentered the pre_kprobe_handler(), since
619 * another probe was hit while within the handler.
620 * We here save the original kprobes variables and
621 * just single step on the instruction of the new probe
622 * without calling any user handlers.
623 */
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800624 save_previous_kprobe(kcb);
625 set_current_kprobe(p, kcb);
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700626 p->nmissed++;
627 prepare_ss(p, regs);
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800628 kcb->kprobe_status = KPROBE_REENTER;
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700629 return 1;
Keshavamurthy Anil S89cb14c2005-06-23 00:09:35 -0700630 } else if (args->err == __IA64_BREAK_JPROBE) {
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700631 /*
632 * jprobe instrumented function just completed
633 */
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800634 p = __get_cpu_var(current_kprobe);
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700635 if (p->break_handler && p->break_handler(p, regs)) {
636 goto ss_probe;
637 }
Keshavamurthy Anil S89cb14c2005-06-23 00:09:35 -0700638 } else {
639 /* Not our break */
640 goto no_kprobe;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700641 }
642 }
643
644 lock_kprobes();
645 p = get_kprobe(addr);
646 if (!p) {
647 unlock_kprobes();
Keshavamurthy Anil S661e5a32005-09-06 15:19:32 -0700648 if (!is_ia64_break_inst(regs)) {
649 /*
650 * The breakpoint instruction was removed right
651 * after we hit it. Another cpu has removed
652 * either a probepoint or a debugger breakpoint
653 * at this address. In either case, no further
654 * handling of this interrupt is appropriate.
655 */
656 ret = 1;
657
658 }
659
660 /* Not one of our break, let kernel handle it */
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700661 goto no_kprobe;
662 }
663
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800664 /*
665 * This preempt_disable() matches the preempt_enable_no_resched()
666 * in post_kprobes_handler()
667 */
668 preempt_disable();
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800669 set_current_kprobe(p, kcb);
670 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700671
672 if (p->pre_handler && p->pre_handler(p, regs))
673 /*
674 * Our pre-handler is specifically requesting that we just
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700675 * do a return. This is used for both the jprobe pre-handler
676 * and the kretprobe trampoline
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700677 */
678 return 1;
679
680ss_probe:
681 prepare_ss(p, regs);
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800682 kcb->kprobe_status = KPROBE_HIT_SS;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700683 return 1;
684
685no_kprobe:
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700686 return ret;
687}
688
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700689static int __kprobes post_kprobes_handler(struct pt_regs *regs)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700690{
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800691 struct kprobe *cur = kprobe_running();
692 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
693
694 if (!cur)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700695 return 0;
696
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800697 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
698 kcb->kprobe_status = KPROBE_HIT_SSDONE;
699 cur->post_handler(cur, regs, 0);
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700700 }
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700701
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800702 resume_execution(cur, regs);
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700703
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700704 /*Restore back the original saved kprobes variables and continue. */
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800705 if (kcb->kprobe_status == KPROBE_REENTER) {
706 restore_previous_kprobe(kcb);
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700707 goto out;
708 }
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800709 reset_current_kprobe();
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700710 unlock_kprobes();
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700711
712out:
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700713 preempt_enable_no_resched();
714 return 1;
715}
716
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700717static int __kprobes kprobes_fault_handler(struct pt_regs *regs, int trapnr)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700718{
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800719 struct kprobe *cur = kprobe_running();
720 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
721
722 if (!cur)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700723 return 0;
724
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800725 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700726 return 1;
727
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800728 if (kcb->kprobe_status & KPROBE_HIT_SS) {
729 resume_execution(cur, regs);
730 reset_current_kprobe();
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700731 unlock_kprobes();
732 preempt_enable_no_resched();
733 }
734
735 return 0;
736}
737
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700738int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
739 unsigned long val, void *data)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700740{
741 struct die_args *args = (struct die_args *)data;
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800742 int ret = NOTIFY_DONE;
743
744 preempt_disable();
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700745 switch(val) {
746 case DIE_BREAK:
Keshavamurthy Anil S89cb14c2005-06-23 00:09:35 -0700747 if (pre_kprobes_handler(args))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800748 ret = NOTIFY_STOP;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700749 break;
750 case DIE_SS:
751 if (post_kprobes_handler(args->regs))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800752 ret = NOTIFY_STOP;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700753 break;
754 case DIE_PAGE_FAULT:
755 if (kprobes_fault_handler(args->regs, args->trapnr))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800756 ret = NOTIFY_STOP;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700757 default:
758 break;
759 }
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800760 preempt_enable();
761 return ret;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700762}
763
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700764int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700765{
Anil S Keshavamurthyb2761dc2005-06-23 00:09:28 -0700766 struct jprobe *jp = container_of(p, struct jprobe, kp);
767 unsigned long addr = ((struct fnptr *)(jp->entry))->ip;
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800768 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700769
Anil S Keshavamurthyb2761dc2005-06-23 00:09:28 -0700770 /* save architectural state */
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800771 kcb->jprobe_saved_regs = *regs;
Anil S Keshavamurthyb2761dc2005-06-23 00:09:28 -0700772
773 /* after rfi, execute the jprobe instrumented function */
774 regs->cr_iip = addr & ~0xFULL;
775 ia64_psr(regs)->ri = addr & 0xf;
776 regs->r1 = ((struct fnptr *)(jp->entry))->gp;
777
778 /*
779 * fix the return address to our jprobe_inst_return() function
780 * in the jprobes.S file
781 */
782 regs->b0 = ((struct fnptr *)(jprobe_inst_return))->ip;
783
784 return 1;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700785}
786
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700787int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700788{
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800789 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
790
791 *regs = kcb->jprobe_saved_regs;
Anil S Keshavamurthyb2761dc2005-06-23 00:09:28 -0700792 return 1;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700793}
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700794
795static struct kprobe trampoline_p = {
796 .pre_handler = trampoline_probe_handler
797};
798
Rusty Lynch67729262005-07-05 18:54:50 -0700799int __init arch_init_kprobes(void)
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700800{
801 trampoline_p.addr =
802 (kprobe_opcode_t *)((struct fnptr *)kretprobe_trampoline)->ip;
803 return register_kprobe(&trampoline_p);
804}