blob: 9c9c8fcdfbdc6aa283d4769a37d4e12913e5bd55 [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
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -070026#include <linux/kprobes.h>
27#include <linux/ptrace.h>
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -070028#include <linux/string.h>
29#include <linux/slab.h>
30#include <linux/preempt.h>
31#include <linux/moduleloader.h>
32
33#include <asm/pgtable.h>
34#include <asm/kdebug.h>
Keshavamurthy Anil Sc7b645f2005-06-27 15:17:16 -070035#include <asm/sections.h>
Prasanna S Panchamukhic04c1c82006-03-26 01:38:25 -080036#include <asm/uaccess.h>
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -070037
Anil S Keshavamurthyb2761dc2005-06-23 00:09:28 -070038extern void jprobe_inst_return(void);
39
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -080040DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
41DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -070042
43enum instruction_type {A, I, M, F, B, L, X, u};
44static enum instruction_type bundle_encoding[32][3] = {
45 { M, I, I }, /* 00 */
46 { M, I, I }, /* 01 */
47 { M, I, I }, /* 02 */
48 { M, I, I }, /* 03 */
49 { M, L, X }, /* 04 */
50 { M, L, X }, /* 05 */
51 { u, u, u }, /* 06 */
52 { u, u, u }, /* 07 */
53 { M, M, I }, /* 08 */
54 { M, M, I }, /* 09 */
55 { M, M, I }, /* 0A */
56 { M, M, I }, /* 0B */
57 { M, F, I }, /* 0C */
58 { M, F, I }, /* 0D */
59 { M, M, F }, /* 0E */
60 { M, M, F }, /* 0F */
61 { M, I, B }, /* 10 */
62 { M, I, B }, /* 11 */
63 { M, B, B }, /* 12 */
64 { M, B, B }, /* 13 */
65 { u, u, u }, /* 14 */
66 { u, u, u }, /* 15 */
67 { B, B, B }, /* 16 */
68 { B, B, B }, /* 17 */
69 { M, M, B }, /* 18 */
70 { M, M, B }, /* 19 */
71 { u, u, u }, /* 1A */
72 { u, u, u }, /* 1B */
73 { M, F, B }, /* 1C */
74 { M, F, B }, /* 1D */
75 { u, u, u }, /* 1E */
76 { u, u, u }, /* 1F */
77};
78
Anil S Keshavamurthya5403182005-06-23 00:09:32 -070079/*
80 * In this function we check to see if the instruction
81 * is IP relative instruction and update the kprobe
82 * inst flag accordingly
83 */
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -070084static void __kprobes update_kprobe_inst_flag(uint template, uint slot,
85 uint major_opcode,
86 unsigned long kprobe_inst,
87 struct kprobe *p)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -070088{
Rusty Lynch8bc76772005-06-23 00:09:30 -070089 p->ainsn.inst_flag = 0;
90 p->ainsn.target_br_reg = 0;
91
Keshavamurthy Anil Sdeac66a2005-09-06 15:19:35 -070092 /* Check for Break instruction
bibo,mao62c27be2006-10-02 02:17:33 -070093 * Bits 37:40 Major opcode to be zero
Keshavamurthy Anil Sdeac66a2005-09-06 15:19:35 -070094 * Bits 27:32 X6 to be zero
95 * Bits 32:35 X3 to be zero
96 */
97 if ((!major_opcode) && (!((kprobe_inst >> 27) & 0x1FF)) ) {
98 /* is a break instruction */
99 p->ainsn.inst_flag |= INST_FLAG_BREAK_INST;
100 return;
101 }
102
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700103 if (bundle_encoding[template][slot] == B) {
104 switch (major_opcode) {
105 case INDIRECT_CALL_OPCODE:
106 p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
bibo,mao62c27be2006-10-02 02:17:33 -0700107 p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
108 break;
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700109 case IP_RELATIVE_PREDICT_OPCODE:
110 case IP_RELATIVE_BRANCH_OPCODE:
111 p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;
bibo,mao62c27be2006-10-02 02:17:33 -0700112 break;
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700113 case IP_RELATIVE_CALL_OPCODE:
bibo,mao62c27be2006-10-02 02:17:33 -0700114 p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;
115 p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
116 p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
117 break;
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700118 }
bibo,mao62c27be2006-10-02 02:17:33 -0700119 } else if (bundle_encoding[template][slot] == X) {
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700120 switch (major_opcode) {
121 case LONG_CALL_OPCODE:
122 p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
123 p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
124 break;
125 }
126 }
127 return;
128}
Rusty Lynch8bc76772005-06-23 00:09:30 -0700129
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700130/*
Anil S Keshavamurthy1674eaf2005-06-23 00:09:33 -0700131 * In this function we check to see if the instruction
Anil S Keshavamurthy708de8f2005-06-23 00:09:34 -0700132 * on which we are inserting kprobe is supported.
133 * Returns 0 if supported
134 * Returns -EINVAL if unsupported
135 */
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700136static int __kprobes unsupported_inst(uint template, uint slot,
137 uint major_opcode,
138 unsigned long kprobe_inst,
bibo mao214ddde2006-09-26 11:20:37 -0700139 unsigned long addr)
Anil S Keshavamurthy708de8f2005-06-23 00:09:34 -0700140{
Anil S Keshavamurthy708de8f2005-06-23 00:09:34 -0700141 if (bundle_encoding[template][slot] == I) {
142 switch (major_opcode) {
143 case 0x0: //I_UNIT_MISC_OPCODE:
144 /*
145 * Check for Integer speculation instruction
146 * - Bit 33-35 to be equal to 0x1
147 */
148 if (((kprobe_inst >> 33) & 0x7) == 1) {
149 printk(KERN_WARNING
150 "Kprobes on speculation inst at <0x%lx> not supported\n",
151 addr);
152 return -EINVAL;
153 }
154
155 /*
156 * IP relative mov instruction
157 * - Bit 27-35 to be equal to 0x30
158 */
159 if (((kprobe_inst >> 27) & 0x1FF) == 0x30) {
160 printk(KERN_WARNING
161 "Kprobes on \"mov r1=ip\" at <0x%lx> not supported\n",
162 addr);
163 return -EINVAL;
164
165 }
166 }
167 }
168 return 0;
169}
170
171
172/*
173 * In this function we check to see if the instruction
Anil S Keshavamurthy1674eaf2005-06-23 00:09:33 -0700174 * (qp) cmpx.crel.ctype p1,p2=r2,r3
175 * on which we are inserting kprobe is cmp instruction
176 * with ctype as unc.
177 */
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700178static uint __kprobes is_cmp_ctype_unc_inst(uint template, uint slot,
179 uint major_opcode,
180 unsigned long kprobe_inst)
Anil S Keshavamurthy1674eaf2005-06-23 00:09:33 -0700181{
182 cmp_inst_t cmp_inst;
183 uint ctype_unc = 0;
184
185 if (!((bundle_encoding[template][slot] == I) ||
186 (bundle_encoding[template][slot] == M)))
187 goto out;
188
189 if (!((major_opcode == 0xC) || (major_opcode == 0xD) ||
190 (major_opcode == 0xE)))
191 goto out;
192
193 cmp_inst.l = kprobe_inst;
194 if ((cmp_inst.f.x2 == 0) || (cmp_inst.f.x2 == 1)) {
195 /* Integere compare - Register Register (A6 type)*/
196 if ((cmp_inst.f.tb == 0) && (cmp_inst.f.ta == 0)
197 &&(cmp_inst.f.c == 1))
198 ctype_unc = 1;
199 } else if ((cmp_inst.f.x2 == 2)||(cmp_inst.f.x2 == 3)) {
200 /* Integere compare - Immediate Register (A8 type)*/
201 if ((cmp_inst.f.ta == 0) &&(cmp_inst.f.c == 1))
202 ctype_unc = 1;
203 }
204out:
205 return ctype_unc;
206}
207
208/*
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700209 * In this function we override the bundle with
210 * the break instruction at the given slot.
211 */
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700212static void __kprobes prepare_break_inst(uint template, uint slot,
213 uint major_opcode,
214 unsigned long kprobe_inst,
215 struct kprobe *p)
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700216{
217 unsigned long break_inst = BREAK_INST;
bibo mao214ddde2006-09-26 11:20:37 -0700218 bundle_t *bundle = &p->opcode.bundle;
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700219
220 /*
221 * Copy the original kprobe_inst qualifying predicate(qp)
Anil S Keshavamurthy1674eaf2005-06-23 00:09:33 -0700222 * to the break instruction iff !is_cmp_ctype_unc_inst
223 * because for cmp instruction with ctype equal to unc,
224 * which is a special instruction always needs to be
225 * executed regradless of qp
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700226 */
Anil S Keshavamurthy1674eaf2005-06-23 00:09:33 -0700227 if (!is_cmp_ctype_unc_inst(template, slot, major_opcode, kprobe_inst))
228 break_inst |= (0x3f & kprobe_inst);
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700229
230 switch (slot) {
231 case 0:
232 bundle->quad0.slot0 = break_inst;
233 break;
234 case 1:
235 bundle->quad0.slot1_p0 = break_inst;
236 bundle->quad1.slot1_p1 = break_inst >> (64-46);
237 break;
238 case 2:
239 bundle->quad1.slot2 = break_inst;
240 break;
241 }
242
243 /*
244 * Update the instruction flag, so that we can
245 * emulate the instruction properly after we
246 * single step on original instruction
247 */
248 update_kprobe_inst_flag(template, slot, major_opcode, kprobe_inst, p);
249}
250
Prasanna S Panchamukhi3ca269d2006-04-18 22:22:02 -0700251static void __kprobes get_kprobe_inst(bundle_t *bundle, uint slot,
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700252 unsigned long *kprobe_inst, uint *major_opcode)
253{
254 unsigned long kprobe_inst_p0, kprobe_inst_p1;
255 unsigned int template;
256
257 template = bundle->quad0.template;
258
259 switch (slot) {
260 case 0:
bibo,mao62c27be2006-10-02 02:17:33 -0700261 *major_opcode = (bundle->quad0.slot0 >> SLOT0_OPCODE_SHIFT);
262 *kprobe_inst = bundle->quad0.slot0;
263 break;
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700264 case 1:
bibo,mao62c27be2006-10-02 02:17:33 -0700265 *major_opcode = (bundle->quad1.slot1_p1 >> SLOT1_p1_OPCODE_SHIFT);
266 kprobe_inst_p0 = bundle->quad0.slot1_p0;
267 kprobe_inst_p1 = bundle->quad1.slot1_p1;
268 *kprobe_inst = kprobe_inst_p0 | (kprobe_inst_p1 << (64-46));
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700269 break;
270 case 2:
bibo,mao62c27be2006-10-02 02:17:33 -0700271 *major_opcode = (bundle->quad1.slot2 >> SLOT2_OPCODE_SHIFT);
272 *kprobe_inst = bundle->quad1.slot2;
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700273 break;
274 }
275}
276
Keshavamurthy Anil Sc7b645f2005-06-27 15:17:16 -0700277/* Returns non-zero if the addr is in the Interrupt Vector Table */
Prasanna S Panchamukhi3ca269d2006-04-18 22:22:02 -0700278static int __kprobes in_ivt_functions(unsigned long addr)
Keshavamurthy Anil Sc7b645f2005-06-27 15:17:16 -0700279{
280 return (addr >= (unsigned long)__start_ivt_text
281 && addr < (unsigned long)__end_ivt_text);
282}
283
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700284static int __kprobes valid_kprobe_addr(int template, int slot,
285 unsigned long addr)
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700286{
287 if ((slot > 2) || ((bundle_encoding[template][1] == L) && slot > 1)) {
Keshavamurthy Anil Sc7b645f2005-06-27 15:17:16 -0700288 printk(KERN_WARNING "Attempting to insert unaligned kprobe "
289 "at 0x%lx\n", addr);
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700290 return -EINVAL;
291 }
Rusty Lyncha528e212005-06-27 15:17:15 -0700292
bibo,mao62c27be2006-10-02 02:17:33 -0700293 if (in_ivt_functions(addr)) {
294 printk(KERN_WARNING "Kprobes can't be inserted inside "
Keshavamurthy Anil Sc7b645f2005-06-27 15:17:16 -0700295 "IVT functions at 0x%lx\n", addr);
bibo,mao62c27be2006-10-02 02:17:33 -0700296 return -EINVAL;
297 }
Keshavamurthy Anil Sc7b645f2005-06-27 15:17:16 -0700298
Rusty Lyncha528e212005-06-27 15:17:15 -0700299 if (slot == 1 && bundle_encoding[template][1] != L) {
300 printk(KERN_WARNING "Inserting kprobes on slot #1 "
301 "is not supported\n");
302 return -EINVAL;
303 }
304
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700305 return 0;
306}
Rusty Lynch8bc76772005-06-23 00:09:30 -0700307
Prasanna S Panchamukhi3ca269d2006-04-18 22:22:02 -0700308static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700309{
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800310 kcb->prev_kprobe.kp = kprobe_running();
311 kcb->prev_kprobe.status = kcb->kprobe_status;
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700312}
313
Prasanna S Panchamukhi3ca269d2006-04-18 22:22:02 -0700314static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700315{
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800316 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
317 kcb->kprobe_status = kcb->prev_kprobe.status;
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700318}
319
Prasanna S Panchamukhi3ca269d2006-04-18 22:22:02 -0700320static void __kprobes set_current_kprobe(struct kprobe *p,
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800321 struct kprobe_ctlblk *kcb)
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700322{
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800323 __get_cpu_var(current_kprobe) = p;
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700324}
325
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700326static void kretprobe_trampoline(void)
327{
328}
329
330/*
331 * At this point the target function has been tricked into
332 * returning into our trampoline. Lookup the associated instance
333 * and then:
334 * - call the handler function
335 * - cleanup by marking the instance as unused
336 * - long jump back to the original return address
337 */
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700338int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700339{
340 struct kretprobe_instance *ri = NULL;
341 struct hlist_head *head;
342 struct hlist_node *node, *tmp;
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800343 unsigned long flags, orig_ret_address = 0;
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700344 unsigned long trampoline_address =
345 ((struct fnptr *)kretprobe_trampoline)->ip;
346
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800347 spin_lock_irqsave(&kretprobe_lock, flags);
Keith Owens9138d582005-11-07 11:27:13 -0800348 head = kretprobe_inst_table_head(current);
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700349
350 /*
351 * It is possible to have multiple instances associated with a given
352 * task either because an multiple functions in the call path
353 * have a return probe installed on them, and/or more then one return
354 * return probe was registered for a target function.
355 *
356 * We can handle this because:
357 * - instances are always inserted at the head of the list
358 * - when multiple return probes are registered for the same
359 * function, the first instance's ret_addr will point to the
360 * real return address, and all the rest will point to
361 * kretprobe_trampoline
362 */
363 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
Keith Owens9138d582005-11-07 11:27:13 -0800364 if (ri->task != current)
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700365 /* another task is sharing our hash bucket */
Keith Owens9138d582005-11-07 11:27:13 -0800366 continue;
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700367
368 if (ri->rp && ri->rp->handler)
369 ri->rp->handler(ri, regs);
370
371 orig_ret_address = (unsigned long)ri->ret_addr;
372 recycle_rp_inst(ri);
373
374 if (orig_ret_address != trampoline_address)
375 /*
376 * This is the real return address. Any other
377 * instances associated with this task are for
378 * other calls deeper on the call stack
379 */
380 break;
381 }
382
383 BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
384 regs->cr_iip = orig_ret_address;
385
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800386 reset_current_kprobe();
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800387 spin_unlock_irqrestore(&kretprobe_lock, flags);
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700388 preempt_enable_no_resched();
389
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800390 /*
391 * By returning a non-zero value, we are telling
392 * kprobe_handler() that we don't want the post_handler
393 * to run (and have re-enabled preemption)
394 */
Keith Owens9138d582005-11-07 11:27:13 -0800395 return 1;
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700396}
397
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800398/* Called with kretprobe_lock held */
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700399void __kprobes arch_prepare_kretprobe(struct kretprobe *rp,
400 struct pt_regs *regs)
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700401{
402 struct kretprobe_instance *ri;
403
404 if ((ri = get_free_rp_inst(rp)) != NULL) {
405 ri->rp = rp;
406 ri->task = current;
407 ri->ret_addr = (kprobe_opcode_t *)regs->b0;
408
409 /* Replace the return addr with trampoline addr */
410 regs->b0 = ((struct fnptr *)kretprobe_trampoline)->ip;
411
412 add_rp_inst(ri);
413 } else {
414 rp->nmissed++;
415 }
416}
417
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700418int __kprobes arch_prepare_kprobe(struct kprobe *p)
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700419{
420 unsigned long addr = (unsigned long) p->addr;
421 unsigned long *kprobe_addr = (unsigned long *)(addr & ~0xFULL);
422 unsigned long kprobe_inst=0;
423 unsigned int slot = addr & 0xf, template, major_opcode = 0;
bibo mao214ddde2006-09-26 11:20:37 -0700424 bundle_t *bundle;
Rusty Lynch8bc76772005-06-23 00:09:30 -0700425
bibo mao214ddde2006-09-26 11:20:37 -0700426 bundle = &((kprobe_opcode_t *)kprobe_addr)->bundle;
bibo,mao62c27be2006-10-02 02:17:33 -0700427 template = bundle->quad0.template;
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700428
429 if(valid_kprobe_addr(template, slot, addr))
430 return -EINVAL;
431
432 /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
bibo,mao62c27be2006-10-02 02:17:33 -0700433 if (slot == 1 && bundle_encoding[template][1] == L)
434 slot++;
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700435
436 /* Get kprobe_inst and major_opcode from the bundle */
437 get_kprobe_inst(bundle, slot, &kprobe_inst, &major_opcode);
438
bibo mao214ddde2006-09-26 11:20:37 -0700439 if (unsupported_inst(template, slot, major_opcode, kprobe_inst, addr))
Anil S Keshavamurthy708de8f2005-06-23 00:09:34 -0700440 return -EINVAL;
441
bibo mao214ddde2006-09-26 11:20:37 -0700442
443 p->ainsn.insn = get_insn_slot();
444 if (!p->ainsn.insn)
445 return -ENOMEM;
446 memcpy(&p->opcode, kprobe_addr, sizeof(kprobe_opcode_t));
447 memcpy(p->ainsn.insn, kprobe_addr, sizeof(kprobe_opcode_t));
448
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700449 prepare_break_inst(template, slot, major_opcode, kprobe_inst, p);
Rusty Lynch8bc76772005-06-23 00:09:30 -0700450
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700451 return 0;
452}
453
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700454void __kprobes arch_arm_kprobe(struct kprobe *p)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700455{
456 unsigned long addr = (unsigned long)p->addr;
457 unsigned long arm_addr = addr & ~0xFULL;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700458
bibo mao214ddde2006-09-26 11:20:37 -0700459 flush_icache_range((unsigned long)p->ainsn.insn,
460 (unsigned long)p->ainsn.insn + sizeof(kprobe_opcode_t));
461 memcpy((char *)arm_addr, &p->opcode, sizeof(kprobe_opcode_t));
462 flush_icache_range(arm_addr, arm_addr + sizeof(kprobe_opcode_t));
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700463}
464
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700465void __kprobes arch_disarm_kprobe(struct kprobe *p)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700466{
467 unsigned long addr = (unsigned long)p->addr;
468 unsigned long arm_addr = addr & ~0xFULL;
469
bibo mao214ddde2006-09-26 11:20:37 -0700470 /* p->ainsn.insn contains the original unaltered kprobe_opcode_t */
471 memcpy((char *) arm_addr, (char *) p->ainsn.insn,
472 sizeof(kprobe_opcode_t));
473 flush_icache_range(arm_addr, arm_addr + sizeof(kprobe_opcode_t));
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700474}
475
bibo mao214ddde2006-09-26 11:20:37 -0700476void __kprobes arch_remove_kprobe(struct kprobe *p)
477{
478 mutex_lock(&kprobe_mutex);
479 free_insn_slot(p->ainsn.insn);
480 mutex_unlock(&kprobe_mutex);
481}
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700482/*
483 * We are resuming execution after a single step fault, so the pt_regs
484 * structure reflects the register state after we executed the instruction
485 * located in the kprobe (p->ainsn.insn.bundle). We still need to adjust
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700486 * the ip to point back to the original stack address. To set the IP address
487 * to original stack address, handle the case where we need to fixup the
488 * relative IP address and/or fixup branch register.
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700489 */
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700490static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700491{
bibo,mao62c27be2006-10-02 02:17:33 -0700492 unsigned long bundle_addr = (unsigned long) (&p->ainsn.insn->bundle);
493 unsigned long resume_addr = (unsigned long)p->addr & ~0xFULL;
494 unsigned long template;
495 int slot = ((unsigned long)p->addr & 0xf);
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700496
bibo mao214ddde2006-09-26 11:20:37 -0700497 template = p->ainsn.insn->bundle.quad0.template;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700498
bibo,mao62c27be2006-10-02 02:17:33 -0700499 if (slot == 1 && bundle_encoding[template][1] == L)
500 slot = 2;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700501
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700502 if (p->ainsn.inst_flag) {
503
504 if (p->ainsn.inst_flag & INST_FLAG_FIX_RELATIVE_IP_ADDR) {
505 /* Fix relative IP address */
bibo,mao62c27be2006-10-02 02:17:33 -0700506 regs->cr_iip = (regs->cr_iip - bundle_addr) +
507 resume_addr;
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700508 }
509
510 if (p->ainsn.inst_flag & INST_FLAG_FIX_BRANCH_REG) {
511 /*
512 * Fix target branch register, software convention is
513 * to use either b0 or b6 or b7, so just checking
514 * only those registers
515 */
516 switch (p->ainsn.target_br_reg) {
517 case 0:
518 if ((regs->b0 == bundle_addr) ||
519 (regs->b0 == bundle_addr + 0x10)) {
520 regs->b0 = (regs->b0 - bundle_addr) +
521 resume_addr;
522 }
523 break;
524 case 6:
525 if ((regs->b6 == bundle_addr) ||
526 (regs->b6 == bundle_addr + 0x10)) {
527 regs->b6 = (regs->b6 - bundle_addr) +
528 resume_addr;
529 }
530 break;
531 case 7:
532 if ((regs->b7 == bundle_addr) ||
533 (regs->b7 == bundle_addr + 0x10)) {
534 regs->b7 = (regs->b7 - bundle_addr) +
535 resume_addr;
536 }
537 break;
538 } /* end switch */
539 }
540 goto turn_ss_off;
541 }
542
543 if (slot == 2) {
bibo,mao62c27be2006-10-02 02:17:33 -0700544 if (regs->cr_iip == bundle_addr + 0x10) {
545 regs->cr_iip = resume_addr + 0x10;
546 }
547 } else {
548 if (regs->cr_iip == bundle_addr) {
549 regs->cr_iip = resume_addr;
550 }
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700551 }
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700552
553turn_ss_off:
bibo,mao62c27be2006-10-02 02:17:33 -0700554 /* Turn off Single Step bit */
555 ia64_psr(regs)->ss = 0;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700556}
557
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700558static void __kprobes prepare_ss(struct kprobe *p, struct pt_regs *regs)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700559{
bibo mao214ddde2006-09-26 11:20:37 -0700560 unsigned long bundle_addr = (unsigned long) &p->ainsn.insn->bundle;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700561 unsigned long slot = (unsigned long)p->addr & 0xf;
562
Keshavamurthy Anil Sdeac66a2005-09-06 15:19:35 -0700563 /* single step inline if break instruction */
564 if (p->ainsn.inst_flag == INST_FLAG_BREAK_INST)
565 regs->cr_iip = (unsigned long)p->addr & ~0xFULL;
566 else
567 regs->cr_iip = bundle_addr & ~0xFULL;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700568
569 if (slot > 2)
570 slot = 0;
571
572 ia64_psr(regs)->ri = slot;
573
574 /* turn on single stepping */
575 ia64_psr(regs)->ss = 1;
576}
577
Keshavamurthy Anil S661e5a32005-09-06 15:19:32 -0700578static int __kprobes is_ia64_break_inst(struct pt_regs *regs)
579{
580 unsigned int slot = ia64_psr(regs)->ri;
581 unsigned int template, major_opcode;
582 unsigned long kprobe_inst;
583 unsigned long *kprobe_addr = (unsigned long *)regs->cr_iip;
584 bundle_t bundle;
585
586 memcpy(&bundle, kprobe_addr, sizeof(bundle_t));
587 template = bundle.quad0.template;
588
589 /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
590 if (slot == 1 && bundle_encoding[template][1] == L)
bibo,mao62c27be2006-10-02 02:17:33 -0700591 slot++;
Keshavamurthy Anil S661e5a32005-09-06 15:19:32 -0700592
593 /* Get Kprobe probe instruction at given slot*/
594 get_kprobe_inst(&bundle, slot, &kprobe_inst, &major_opcode);
595
596 /* For break instruction,
597 * Bits 37:40 Major opcode to be zero
598 * Bits 27:32 X6 to be zero
599 * Bits 32:35 X3 to be zero
600 */
601 if (major_opcode || ((kprobe_inst >> 27) & 0x1FF) ) {
602 /* Not a break instruction */
603 return 0;
604 }
605
606 /* Is a break instruction */
607 return 1;
608}
609
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700610static int __kprobes pre_kprobes_handler(struct die_args *args)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700611{
612 struct kprobe *p;
613 int ret = 0;
Keshavamurthy Anil S89cb14c2005-06-23 00:09:35 -0700614 struct pt_regs *regs = args->regs;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700615 kprobe_opcode_t *addr = (kprobe_opcode_t *)instruction_pointer(regs);
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800616 struct kprobe_ctlblk *kcb;
617
618 /*
619 * We don't want to be preempted for the entire
620 * duration of kprobe processing
621 */
622 preempt_disable();
623 kcb = get_kprobe_ctlblk();
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700624
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700625 /* Handle recursion cases */
626 if (kprobe_running()) {
627 p = get_kprobe(addr);
628 if (p) {
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800629 if ((kcb->kprobe_status == KPROBE_HIT_SS) &&
Keshavamurthy Anil Sdeac66a2005-09-06 15:19:35 -0700630 (p->ainsn.inst_flag == INST_FLAG_BREAK_INST)) {
bibo,mao62c27be2006-10-02 02:17:33 -0700631 ia64_psr(regs)->ss = 0;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700632 goto no_kprobe;
633 }
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700634 /* We have reentered the pre_kprobe_handler(), since
635 * another probe was hit while within the handler.
636 * We here save the original kprobes variables and
637 * just single step on the instruction of the new probe
638 * without calling any user handlers.
639 */
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800640 save_previous_kprobe(kcb);
641 set_current_kprobe(p, kcb);
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -0800642 kprobes_inc_nmissed_count(p);
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700643 prepare_ss(p, regs);
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800644 kcb->kprobe_status = KPROBE_REENTER;
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700645 return 1;
Keshavamurthy Anil S89cb14c2005-06-23 00:09:35 -0700646 } else if (args->err == __IA64_BREAK_JPROBE) {
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700647 /*
648 * jprobe instrumented function just completed
649 */
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800650 p = __get_cpu_var(current_kprobe);
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700651 if (p->break_handler && p->break_handler(p, regs)) {
652 goto ss_probe;
653 }
Keshavamurthy Anil Seb3a7292006-01-11 12:17:42 -0800654 } else if (!is_ia64_break_inst(regs)) {
655 /* The breakpoint instruction was removed by
656 * another cpu right after we hit, no further
657 * handling of this interrupt is appropriate
658 */
659 ret = 1;
660 goto no_kprobe;
Keshavamurthy Anil S89cb14c2005-06-23 00:09:35 -0700661 } else {
662 /* Not our break */
663 goto no_kprobe;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700664 }
665 }
666
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700667 p = get_kprobe(addr);
668 if (!p) {
Keshavamurthy Anil S661e5a32005-09-06 15:19:32 -0700669 if (!is_ia64_break_inst(regs)) {
670 /*
671 * The breakpoint instruction was removed right
672 * after we hit it. Another cpu has removed
673 * either a probepoint or a debugger breakpoint
674 * at this address. In either case, no further
675 * handling of this interrupt is appropriate.
676 */
677 ret = 1;
678
679 }
680
681 /* Not one of our break, let kernel handle it */
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700682 goto no_kprobe;
683 }
684
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800685 set_current_kprobe(p, kcb);
686 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700687
688 if (p->pre_handler && p->pre_handler(p, regs))
689 /*
690 * Our pre-handler is specifically requesting that we just
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700691 * do a return. This is used for both the jprobe pre-handler
692 * and the kretprobe trampoline
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700693 */
694 return 1;
695
696ss_probe:
697 prepare_ss(p, regs);
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800698 kcb->kprobe_status = KPROBE_HIT_SS;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700699 return 1;
700
701no_kprobe:
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800702 preempt_enable_no_resched();
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700703 return ret;
704}
705
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700706static int __kprobes post_kprobes_handler(struct pt_regs *regs)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700707{
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800708 struct kprobe *cur = kprobe_running();
709 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
710
711 if (!cur)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700712 return 0;
713
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800714 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
715 kcb->kprobe_status = KPROBE_HIT_SSDONE;
716 cur->post_handler(cur, regs, 0);
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700717 }
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700718
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800719 resume_execution(cur, regs);
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700720
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700721 /*Restore back the original saved kprobes variables and continue. */
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800722 if (kcb->kprobe_status == KPROBE_REENTER) {
723 restore_previous_kprobe(kcb);
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700724 goto out;
725 }
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800726 reset_current_kprobe();
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700727
728out:
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700729 preempt_enable_no_resched();
730 return 1;
731}
732
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700733static int __kprobes kprobes_fault_handler(struct pt_regs *regs, int trapnr)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700734{
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800735 struct kprobe *cur = kprobe_running();
736 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
737
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700738
Prasanna S Panchamukhic04c1c82006-03-26 01:38:25 -0800739 switch(kcb->kprobe_status) {
740 case KPROBE_HIT_SS:
741 case KPROBE_REENTER:
742 /*
743 * We are here because the instruction being single
744 * stepped caused a page fault. We reset the current
745 * kprobe and the instruction pointer points back to
746 * the probe address and allow the page fault handler
747 * to continue as a normal page fault.
748 */
749 regs->cr_iip = ((unsigned long)cur->addr) & ~0xFULL;
750 ia64_psr(regs)->ri = ((unsigned long)cur->addr) & 0xf;
751 if (kcb->kprobe_status == KPROBE_REENTER)
752 restore_previous_kprobe(kcb);
753 else
754 reset_current_kprobe();
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700755 preempt_enable_no_resched();
Prasanna S Panchamukhic04c1c82006-03-26 01:38:25 -0800756 break;
757 case KPROBE_HIT_ACTIVE:
758 case KPROBE_HIT_SSDONE:
759 /*
760 * We increment the nmissed count for accounting,
761 * we can also use npre/npostfault count for accouting
762 * these specific fault cases.
763 */
764 kprobes_inc_nmissed_count(cur);
765
766 /*
767 * We come here because instructions in the pre/post
768 * handler caused the page_fault, this could happen
769 * if handler tries to access user space by
770 * copy_from_user(), get_user() etc. Let the
771 * user-specified handler try to fix it first.
772 */
773 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
774 return 1;
Keshavamurthy Anil Sfd32cb32006-09-25 16:32:20 -0700775 /*
776 * In case the user-specified fault handler returned
777 * zero, try to fix up.
778 */
779 if (ia64_done_with_exception(regs))
780 return 1;
Prasanna S Panchamukhic04c1c82006-03-26 01:38:25 -0800781
782 /*
783 * Let ia64_do_page_fault() fix it.
784 */
785 break;
786 default:
787 break;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700788 }
789
790 return 0;
791}
792
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700793int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
794 unsigned long val, void *data)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700795{
796 struct die_args *args = (struct die_args *)data;
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800797 int ret = NOTIFY_DONE;
798
bibo,mao2326c772006-03-26 01:38:21 -0800799 if (args->regs && user_mode(args->regs))
800 return ret;
801
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700802 switch(val) {
803 case DIE_BREAK:
Keith Owens9138d582005-11-07 11:27:13 -0800804 /* err is break number from ia64_bad_break() */
Keshavamurthy Anil S5a94bcf2005-11-22 14:15:49 -0800805 if (args->err == 0x80200 || args->err == 0x80300 || args->err == 0)
Keith Owens9138d582005-11-07 11:27:13 -0800806 if (pre_kprobes_handler(args))
807 ret = NOTIFY_STOP;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700808 break;
Keith Owens9138d582005-11-07 11:27:13 -0800809 case DIE_FAULT:
810 /* err is vector number from ia64_fault() */
811 if (args->err == 36)
812 if (post_kprobes_handler(args->regs))
813 ret = NOTIFY_STOP;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700814 break;
815 case DIE_PAGE_FAULT:
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800816 /* kprobe_running() needs smp_processor_id() */
817 preempt_disable();
818 if (kprobe_running() &&
819 kprobes_fault_handler(args->regs, args->trapnr))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800820 ret = NOTIFY_STOP;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800821 preempt_enable();
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700822 default:
823 break;
824 }
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800825 return ret;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700826}
827
Zhang Yanmind3ef1f52006-01-13 14:45:21 -0800828struct param_bsp_cfm {
829 unsigned long ip;
830 unsigned long *bsp;
831 unsigned long cfm;
832};
833
834static void ia64_get_bsp_cfm(struct unw_frame_info *info, void *arg)
835{
836 unsigned long ip;
837 struct param_bsp_cfm *lp = arg;
838
839 do {
840 unw_get_ip(info, &ip);
841 if (ip == 0)
842 break;
843 if (ip == lp->ip) {
844 unw_get_bsp(info, (unsigned long*)&lp->bsp);
845 unw_get_cfm(info, (unsigned long*)&lp->cfm);
846 return;
847 }
848 } while (unw_unwind(info) >= 0);
849 lp->bsp = 0;
850 lp->cfm = 0;
851 return;
852}
853
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700854int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700855{
Anil S Keshavamurthyb2761dc2005-06-23 00:09:28 -0700856 struct jprobe *jp = container_of(p, struct jprobe, kp);
857 unsigned long addr = ((struct fnptr *)(jp->entry))->ip;
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800858 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Zhang Yanmind3ef1f52006-01-13 14:45:21 -0800859 struct param_bsp_cfm pa;
860 int bytes;
861
862 /*
863 * Callee owns the argument space and could overwrite it, eg
864 * tail call optimization. So to be absolutely safe
865 * we save the argument space before transfering the control
866 * to instrumented jprobe function which runs in
867 * the process context
868 */
869 pa.ip = regs->cr_iip;
870 unw_init_running(ia64_get_bsp_cfm, &pa);
871 bytes = (char *)ia64_rse_skip_regs(pa.bsp, pa.cfm & 0x3f)
872 - (char *)pa.bsp;
873 memcpy( kcb->jprobes_saved_stacked_regs,
874 pa.bsp,
875 bytes );
876 kcb->bsp = pa.bsp;
877 kcb->cfm = pa.cfm;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700878
Anil S Keshavamurthyb2761dc2005-06-23 00:09:28 -0700879 /* save architectural state */
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800880 kcb->jprobe_saved_regs = *regs;
Anil S Keshavamurthyb2761dc2005-06-23 00:09:28 -0700881
882 /* after rfi, execute the jprobe instrumented function */
883 regs->cr_iip = addr & ~0xFULL;
884 ia64_psr(regs)->ri = addr & 0xf;
885 regs->r1 = ((struct fnptr *)(jp->entry))->gp;
886
887 /*
888 * fix the return address to our jprobe_inst_return() function
889 * in the jprobes.S file
890 */
bibo,mao62c27be2006-10-02 02:17:33 -0700891 regs->b0 = ((struct fnptr *)(jprobe_inst_return))->ip;
Anil S Keshavamurthyb2761dc2005-06-23 00:09:28 -0700892
893 return 1;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700894}
895
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700896int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700897{
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800898 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Zhang Yanmind3ef1f52006-01-13 14:45:21 -0800899 int bytes;
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800900
Zhang Yanmind3ef1f52006-01-13 14:45:21 -0800901 /* restoring architectural state */
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800902 *regs = kcb->jprobe_saved_regs;
Zhang Yanmind3ef1f52006-01-13 14:45:21 -0800903
904 /* restoring the original argument space */
905 flush_register_stack();
906 bytes = (char *)ia64_rse_skip_regs(kcb->bsp, kcb->cfm & 0x3f)
907 - (char *)kcb->bsp;
908 memcpy( kcb->bsp,
909 kcb->jprobes_saved_stacked_regs,
910 bytes );
911 invalidate_stacked_regs();
912
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800913 preempt_enable_no_resched();
Anil S Keshavamurthyb2761dc2005-06-23 00:09:28 -0700914 return 1;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700915}
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700916
917static struct kprobe trampoline_p = {
918 .pre_handler = trampoline_probe_handler
919};
920
Rusty Lynch67729262005-07-05 18:54:50 -0700921int __init arch_init_kprobes(void)
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700922{
923 trampoline_p.addr =
924 (kprobe_opcode_t *)((struct fnptr *)kretprobe_trampoline)->ip;
925 return register_kprobe(&trampoline_p);
926}