blob: 8406d24d516bc8a6d9813b6dff61b9c74d6e0a33 [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;
Tony Luck08ed38b2006-11-14 09:33:38 -080091 p->ainsn.slot = slot;
Rusty Lynch8bc76772005-06-23 00:09:30 -070092
Keshavamurthy Anil Sdeac66a2005-09-06 15:19:35 -070093 /* Check for Break instruction
bibo,mao62c27be2006-10-02 02:17:33 -070094 * Bits 37:40 Major opcode to be zero
Keshavamurthy Anil Sdeac66a2005-09-06 15:19:35 -070095 * 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;
bibo,mao62c27be2006-10-02 02:17:33 -0700108 p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
109 break;
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700110 case IP_RELATIVE_PREDICT_OPCODE:
111 case IP_RELATIVE_BRANCH_OPCODE:
112 p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;
bibo,mao62c27be2006-10-02 02:17:33 -0700113 break;
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700114 case IP_RELATIVE_CALL_OPCODE:
bibo,mao62c27be2006-10-02 02:17:33 -0700115 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;
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700119 }
bibo,mao62c27be2006-10-02 02:17:33 -0700120 } else if (bundle_encoding[template][slot] == X) {
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700121 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,
bibo mao214ddde2006-09-26 11:20:37 -0700140 unsigned long addr)
Anil S Keshavamurthy708de8f2005-06-23 00:09:34 -0700141{
Anil S Keshavamurthy708de8f2005-06-23 00:09:34 -0700142 if (bundle_encoding[template][slot] == I) {
143 switch (major_opcode) {
144 case 0x0: //I_UNIT_MISC_OPCODE:
145 /*
146 * Check for Integer speculation instruction
147 * - Bit 33-35 to be equal to 0x1
148 */
149 if (((kprobe_inst >> 33) & 0x7) == 1) {
150 printk(KERN_WARNING
151 "Kprobes on speculation inst at <0x%lx> not supported\n",
152 addr);
153 return -EINVAL;
154 }
155
156 /*
157 * IP relative mov instruction
158 * - Bit 27-35 to be equal to 0x30
159 */
160 if (((kprobe_inst >> 27) & 0x1FF) == 0x30) {
161 printk(KERN_WARNING
162 "Kprobes on \"mov r1=ip\" at <0x%lx> not supported\n",
163 addr);
164 return -EINVAL;
165
166 }
167 }
168 }
169 return 0;
170}
171
172
173/*
174 * In this function we check to see if the instruction
Anil S Keshavamurthy1674eaf2005-06-23 00:09:33 -0700175 * (qp) cmpx.crel.ctype p1,p2=r2,r3
176 * on which we are inserting kprobe is cmp instruction
177 * with ctype as unc.
178 */
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700179static uint __kprobes is_cmp_ctype_unc_inst(uint template, uint slot,
180 uint major_opcode,
181 unsigned long kprobe_inst)
Anil S Keshavamurthy1674eaf2005-06-23 00:09:33 -0700182{
183 cmp_inst_t cmp_inst;
184 uint ctype_unc = 0;
185
186 if (!((bundle_encoding[template][slot] == I) ||
187 (bundle_encoding[template][slot] == M)))
188 goto out;
189
190 if (!((major_opcode == 0xC) || (major_opcode == 0xD) ||
191 (major_opcode == 0xE)))
192 goto out;
193
194 cmp_inst.l = kprobe_inst;
195 if ((cmp_inst.f.x2 == 0) || (cmp_inst.f.x2 == 1)) {
196 /* Integere compare - Register Register (A6 type)*/
197 if ((cmp_inst.f.tb == 0) && (cmp_inst.f.ta == 0)
198 &&(cmp_inst.f.c == 1))
199 ctype_unc = 1;
200 } else if ((cmp_inst.f.x2 == 2)||(cmp_inst.f.x2 == 3)) {
201 /* Integere compare - Immediate Register (A8 type)*/
202 if ((cmp_inst.f.ta == 0) &&(cmp_inst.f.c == 1))
203 ctype_unc = 1;
204 }
205out:
206 return ctype_unc;
207}
208
209/*
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700210 * In this function we override the bundle with
211 * the break instruction at the given slot.
212 */
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700213static void __kprobes prepare_break_inst(uint template, uint slot,
214 uint major_opcode,
215 unsigned long kprobe_inst,
216 struct kprobe *p)
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700217{
218 unsigned long break_inst = BREAK_INST;
bibo mao214ddde2006-09-26 11:20:37 -0700219 bundle_t *bundle = &p->opcode.bundle;
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700220
221 /*
222 * Copy the original kprobe_inst qualifying predicate(qp)
Anil S Keshavamurthy1674eaf2005-06-23 00:09:33 -0700223 * to the break instruction iff !is_cmp_ctype_unc_inst
224 * because for cmp instruction with ctype equal to unc,
225 * which is a special instruction always needs to be
226 * executed regradless of qp
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700227 */
Anil S Keshavamurthy1674eaf2005-06-23 00:09:33 -0700228 if (!is_cmp_ctype_unc_inst(template, slot, major_opcode, kprobe_inst))
229 break_inst |= (0x3f & kprobe_inst);
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700230
231 switch (slot) {
232 case 0:
233 bundle->quad0.slot0 = break_inst;
234 break;
235 case 1:
236 bundle->quad0.slot1_p0 = break_inst;
237 bundle->quad1.slot1_p1 = break_inst >> (64-46);
238 break;
239 case 2:
240 bundle->quad1.slot2 = break_inst;
241 break;
242 }
243
244 /*
245 * Update the instruction flag, so that we can
246 * emulate the instruction properly after we
247 * single step on original instruction
248 */
249 update_kprobe_inst_flag(template, slot, major_opcode, kprobe_inst, p);
250}
251
Prasanna S Panchamukhi3ca269d2006-04-18 22:22:02 -0700252static void __kprobes get_kprobe_inst(bundle_t *bundle, uint slot,
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700253 unsigned long *kprobe_inst, uint *major_opcode)
254{
255 unsigned long kprobe_inst_p0, kprobe_inst_p1;
256 unsigned int template;
257
258 template = bundle->quad0.template;
259
260 switch (slot) {
261 case 0:
bibo,mao62c27be2006-10-02 02:17:33 -0700262 *major_opcode = (bundle->quad0.slot0 >> SLOT0_OPCODE_SHIFT);
263 *kprobe_inst = bundle->quad0.slot0;
264 break;
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700265 case 1:
bibo,mao62c27be2006-10-02 02:17:33 -0700266 *major_opcode = (bundle->quad1.slot1_p1 >> SLOT1_p1_OPCODE_SHIFT);
267 kprobe_inst_p0 = bundle->quad0.slot1_p0;
268 kprobe_inst_p1 = bundle->quad1.slot1_p1;
269 *kprobe_inst = kprobe_inst_p0 | (kprobe_inst_p1 << (64-46));
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700270 break;
271 case 2:
bibo,mao62c27be2006-10-02 02:17:33 -0700272 *major_opcode = (bundle->quad1.slot2 >> SLOT2_OPCODE_SHIFT);
273 *kprobe_inst = bundle->quad1.slot2;
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700274 break;
275 }
276}
277
Keshavamurthy Anil Sc7b645f2005-06-27 15:17:16 -0700278/* Returns non-zero if the addr is in the Interrupt Vector Table */
Prasanna S Panchamukhi3ca269d2006-04-18 22:22:02 -0700279static int __kprobes in_ivt_functions(unsigned long addr)
Keshavamurthy Anil Sc7b645f2005-06-27 15:17:16 -0700280{
281 return (addr >= (unsigned long)__start_ivt_text
282 && addr < (unsigned long)__end_ivt_text);
283}
284
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700285static int __kprobes valid_kprobe_addr(int template, int slot,
286 unsigned long addr)
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700287{
288 if ((slot > 2) || ((bundle_encoding[template][1] == L) && slot > 1)) {
Keshavamurthy Anil Sc7b645f2005-06-27 15:17:16 -0700289 printk(KERN_WARNING "Attempting to insert unaligned kprobe "
290 "at 0x%lx\n", addr);
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700291 return -EINVAL;
292 }
Rusty Lyncha528e212005-06-27 15:17:15 -0700293
bibo,mao62c27be2006-10-02 02:17:33 -0700294 if (in_ivt_functions(addr)) {
295 printk(KERN_WARNING "Kprobes can't be inserted inside "
Keshavamurthy Anil Sc7b645f2005-06-27 15:17:16 -0700296 "IVT functions at 0x%lx\n", addr);
bibo,mao62c27be2006-10-02 02:17:33 -0700297 return -EINVAL;
298 }
Keshavamurthy Anil Sc7b645f2005-06-27 15:17:16 -0700299
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700300 return 0;
301}
Rusty Lynch8bc76772005-06-23 00:09:30 -0700302
Prasanna S Panchamukhi3ca269d2006-04-18 22:22:02 -0700303static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700304{
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800305 kcb->prev_kprobe.kp = kprobe_running();
306 kcb->prev_kprobe.status = kcb->kprobe_status;
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700307}
308
Prasanna S Panchamukhi3ca269d2006-04-18 22:22:02 -0700309static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700310{
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800311 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
312 kcb->kprobe_status = kcb->prev_kprobe.status;
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700313}
314
Prasanna S Panchamukhi3ca269d2006-04-18 22:22:02 -0700315static void __kprobes set_current_kprobe(struct kprobe *p,
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800316 struct kprobe_ctlblk *kcb)
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700317{
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800318 __get_cpu_var(current_kprobe) = p;
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700319}
320
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700321static void kretprobe_trampoline(void)
322{
323}
324
325/*
326 * At this point the target function has been tricked into
327 * returning into our trampoline. Lookup the associated instance
328 * and then:
329 * - call the handler function
330 * - cleanup by marking the instance as unused
331 * - long jump back to the original return address
332 */
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700333int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700334{
335 struct kretprobe_instance *ri = NULL;
bibo,mao99219a32006-10-02 02:17:35 -0700336 struct hlist_head *head, empty_rp;
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700337 struct hlist_node *node, *tmp;
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800338 unsigned long flags, orig_ret_address = 0;
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700339 unsigned long trampoline_address =
340 ((struct fnptr *)kretprobe_trampoline)->ip;
341
bibo,mao99219a32006-10-02 02:17:35 -0700342 INIT_HLIST_HEAD(&empty_rp);
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800343 spin_lock_irqsave(&kretprobe_lock, flags);
Keith Owens9138d582005-11-07 11:27:13 -0800344 head = kretprobe_inst_table_head(current);
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700345
346 /*
347 * It is possible to have multiple instances associated with a given
348 * task either because an multiple functions in the call path
349 * have a return probe installed on them, and/or more then one return
350 * return probe was registered for a target function.
351 *
352 * We can handle this because:
353 * - instances are always inserted at the head of the list
354 * - when multiple return probes are registered for the same
355 * function, the first instance's ret_addr will point to the
356 * real return address, and all the rest will point to
357 * kretprobe_trampoline
358 */
359 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
Keith Owens9138d582005-11-07 11:27:13 -0800360 if (ri->task != current)
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700361 /* another task is sharing our hash bucket */
Keith Owens9138d582005-11-07 11:27:13 -0800362 continue;
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700363
364 if (ri->rp && ri->rp->handler)
365 ri->rp->handler(ri, regs);
366
367 orig_ret_address = (unsigned long)ri->ret_addr;
bibo,mao99219a32006-10-02 02:17:35 -0700368 recycle_rp_inst(ri, &empty_rp);
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700369
370 if (orig_ret_address != trampoline_address)
371 /*
372 * This is the real return address. Any other
373 * instances associated with this task are for
374 * other calls deeper on the call stack
375 */
376 break;
377 }
378
379 BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
380 regs->cr_iip = orig_ret_address;
381
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800382 reset_current_kprobe();
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800383 spin_unlock_irqrestore(&kretprobe_lock, flags);
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700384 preempt_enable_no_resched();
385
bibo,mao99219a32006-10-02 02:17:35 -0700386 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
387 hlist_del(&ri->hlist);
388 kfree(ri);
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{
Tony Luck08ed38b2006-11-14 09:33:38 -0800456 unsigned long arm_addr;
457 bundle_t *src, *dest;
458
459 arm_addr = ((unsigned long)p->addr) & ~0xFUL;
460 dest = &((kprobe_opcode_t *)arm_addr)->bundle;
461 src = &p->opcode.bundle;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700462
bibo mao214ddde2006-09-26 11:20:37 -0700463 flush_icache_range((unsigned long)p->ainsn.insn,
464 (unsigned long)p->ainsn.insn + sizeof(kprobe_opcode_t));
Tony Luck08ed38b2006-11-14 09:33:38 -0800465 switch (p->ainsn.slot) {
466 case 0:
467 dest->quad0.slot0 = src->quad0.slot0;
468 break;
469 case 1:
470 dest->quad1.slot1_p1 = src->quad1.slot1_p1;
471 break;
472 case 2:
473 dest->quad1.slot2 = src->quad1.slot2;
474 break;
475 }
bibo mao214ddde2006-09-26 11:20:37 -0700476 flush_icache_range(arm_addr, arm_addr + sizeof(kprobe_opcode_t));
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700477}
478
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700479void __kprobes arch_disarm_kprobe(struct kprobe *p)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700480{
Tony Luck08ed38b2006-11-14 09:33:38 -0800481 unsigned long arm_addr;
482 bundle_t *src, *dest;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700483
Tony Luck08ed38b2006-11-14 09:33:38 -0800484 arm_addr = ((unsigned long)p->addr) & ~0xFUL;
485 dest = &((kprobe_opcode_t *)arm_addr)->bundle;
bibo mao214ddde2006-09-26 11:20:37 -0700486 /* p->ainsn.insn contains the original unaltered kprobe_opcode_t */
Tony Luck08ed38b2006-11-14 09:33:38 -0800487 src = &p->ainsn.insn->bundle;
488 switch (p->ainsn.slot) {
489 case 0:
490 dest->quad0.slot0 = src->quad0.slot0;
491 break;
492 case 1:
493 dest->quad1.slot1_p1 = src->quad1.slot1_p1;
494 break;
495 case 2:
496 dest->quad1.slot2 = src->quad1.slot2;
497 break;
498 }
bibo mao214ddde2006-09-26 11:20:37 -0700499 flush_icache_range(arm_addr, arm_addr + sizeof(kprobe_opcode_t));
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700500}
501
bibo mao214ddde2006-09-26 11:20:37 -0700502void __kprobes arch_remove_kprobe(struct kprobe *p)
503{
504 mutex_lock(&kprobe_mutex);
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800505 free_insn_slot(p->ainsn.insn, 0);
bibo mao214ddde2006-09-26 11:20:37 -0700506 mutex_unlock(&kprobe_mutex);
507}
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700508/*
509 * We are resuming execution after a single step fault, so the pt_regs
510 * structure reflects the register state after we executed the instruction
511 * located in the kprobe (p->ainsn.insn.bundle). We still need to adjust
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700512 * the ip to point back to the original stack address. To set the IP address
513 * to original stack address, handle the case where we need to fixup the
514 * relative IP address and/or fixup branch register.
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700515 */
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700516static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700517{
bibo,mao62c27be2006-10-02 02:17:33 -0700518 unsigned long bundle_addr = (unsigned long) (&p->ainsn.insn->bundle);
519 unsigned long resume_addr = (unsigned long)p->addr & ~0xFULL;
520 unsigned long template;
521 int slot = ((unsigned long)p->addr & 0xf);
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700522
bibo mao214ddde2006-09-26 11:20:37 -0700523 template = p->ainsn.insn->bundle.quad0.template;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700524
bibo,mao62c27be2006-10-02 02:17:33 -0700525 if (slot == 1 && bundle_encoding[template][1] == L)
526 slot = 2;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700527
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700528 if (p->ainsn.inst_flag) {
529
530 if (p->ainsn.inst_flag & INST_FLAG_FIX_RELATIVE_IP_ADDR) {
531 /* Fix relative IP address */
bibo,mao62c27be2006-10-02 02:17:33 -0700532 regs->cr_iip = (regs->cr_iip - bundle_addr) +
533 resume_addr;
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700534 }
535
536 if (p->ainsn.inst_flag & INST_FLAG_FIX_BRANCH_REG) {
537 /*
538 * Fix target branch register, software convention is
539 * to use either b0 or b6 or b7, so just checking
540 * only those registers
541 */
542 switch (p->ainsn.target_br_reg) {
543 case 0:
544 if ((regs->b0 == bundle_addr) ||
545 (regs->b0 == bundle_addr + 0x10)) {
546 regs->b0 = (regs->b0 - bundle_addr) +
547 resume_addr;
548 }
549 break;
550 case 6:
551 if ((regs->b6 == bundle_addr) ||
552 (regs->b6 == bundle_addr + 0x10)) {
553 regs->b6 = (regs->b6 - bundle_addr) +
554 resume_addr;
555 }
556 break;
557 case 7:
558 if ((regs->b7 == bundle_addr) ||
559 (regs->b7 == bundle_addr + 0x10)) {
560 regs->b7 = (regs->b7 - bundle_addr) +
561 resume_addr;
562 }
563 break;
564 } /* end switch */
565 }
566 goto turn_ss_off;
567 }
568
569 if (slot == 2) {
bibo,mao62c27be2006-10-02 02:17:33 -0700570 if (regs->cr_iip == bundle_addr + 0x10) {
571 regs->cr_iip = resume_addr + 0x10;
572 }
573 } else {
574 if (regs->cr_iip == bundle_addr) {
575 regs->cr_iip = resume_addr;
576 }
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700577 }
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700578
579turn_ss_off:
bibo,mao62c27be2006-10-02 02:17:33 -0700580 /* Turn off Single Step bit */
581 ia64_psr(regs)->ss = 0;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700582}
583
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700584static void __kprobes prepare_ss(struct kprobe *p, struct pt_regs *regs)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700585{
bibo mao214ddde2006-09-26 11:20:37 -0700586 unsigned long bundle_addr = (unsigned long) &p->ainsn.insn->bundle;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700587 unsigned long slot = (unsigned long)p->addr & 0xf;
588
Keshavamurthy Anil Sdeac66a2005-09-06 15:19:35 -0700589 /* single step inline if break instruction */
590 if (p->ainsn.inst_flag == INST_FLAG_BREAK_INST)
591 regs->cr_iip = (unsigned long)p->addr & ~0xFULL;
592 else
593 regs->cr_iip = bundle_addr & ~0xFULL;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700594
595 if (slot > 2)
596 slot = 0;
597
598 ia64_psr(regs)->ri = slot;
599
600 /* turn on single stepping */
601 ia64_psr(regs)->ss = 1;
602}
603
Keshavamurthy Anil S661e5a32005-09-06 15:19:32 -0700604static int __kprobes is_ia64_break_inst(struct pt_regs *regs)
605{
606 unsigned int slot = ia64_psr(regs)->ri;
607 unsigned int template, major_opcode;
608 unsigned long kprobe_inst;
609 unsigned long *kprobe_addr = (unsigned long *)regs->cr_iip;
610 bundle_t bundle;
611
612 memcpy(&bundle, kprobe_addr, sizeof(bundle_t));
613 template = bundle.quad0.template;
614
615 /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
616 if (slot == 1 && bundle_encoding[template][1] == L)
bibo,mao62c27be2006-10-02 02:17:33 -0700617 slot++;
Keshavamurthy Anil S661e5a32005-09-06 15:19:32 -0700618
619 /* Get Kprobe probe instruction at given slot*/
620 get_kprobe_inst(&bundle, slot, &kprobe_inst, &major_opcode);
621
622 /* For break instruction,
623 * Bits 37:40 Major opcode to be zero
624 * Bits 27:32 X6 to be zero
625 * Bits 32:35 X3 to be zero
626 */
627 if (major_opcode || ((kprobe_inst >> 27) & 0x1FF) ) {
628 /* Not a break instruction */
629 return 0;
630 }
631
632 /* Is a break instruction */
633 return 1;
634}
635
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700636static int __kprobes pre_kprobes_handler(struct die_args *args)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700637{
638 struct kprobe *p;
639 int ret = 0;
Keshavamurthy Anil S89cb14c2005-06-23 00:09:35 -0700640 struct pt_regs *regs = args->regs;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700641 kprobe_opcode_t *addr = (kprobe_opcode_t *)instruction_pointer(regs);
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800642 struct kprobe_ctlblk *kcb;
643
644 /*
645 * We don't want to be preempted for the entire
646 * duration of kprobe processing
647 */
648 preempt_disable();
649 kcb = get_kprobe_ctlblk();
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700650
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700651 /* Handle recursion cases */
652 if (kprobe_running()) {
653 p = get_kprobe(addr);
654 if (p) {
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800655 if ((kcb->kprobe_status == KPROBE_HIT_SS) &&
Keshavamurthy Anil Sdeac66a2005-09-06 15:19:35 -0700656 (p->ainsn.inst_flag == INST_FLAG_BREAK_INST)) {
bibo,mao62c27be2006-10-02 02:17:33 -0700657 ia64_psr(regs)->ss = 0;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700658 goto no_kprobe;
659 }
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700660 /* We have reentered the pre_kprobe_handler(), since
661 * another probe was hit while within the handler.
662 * We here save the original kprobes variables and
663 * just single step on the instruction of the new probe
664 * without calling any user handlers.
665 */
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800666 save_previous_kprobe(kcb);
667 set_current_kprobe(p, kcb);
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -0800668 kprobes_inc_nmissed_count(p);
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700669 prepare_ss(p, regs);
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800670 kcb->kprobe_status = KPROBE_REENTER;
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700671 return 1;
Keshavamurthy Anil S89cb14c2005-06-23 00:09:35 -0700672 } else if (args->err == __IA64_BREAK_JPROBE) {
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700673 /*
674 * jprobe instrumented function just completed
675 */
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800676 p = __get_cpu_var(current_kprobe);
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700677 if (p->break_handler && p->break_handler(p, regs)) {
678 goto ss_probe;
679 }
Keshavamurthy Anil Seb3a7292006-01-11 12:17:42 -0800680 } else if (!is_ia64_break_inst(regs)) {
681 /* The breakpoint instruction was removed by
682 * another cpu right after we hit, no further
683 * handling of this interrupt is appropriate
684 */
685 ret = 1;
686 goto no_kprobe;
Keshavamurthy Anil S89cb14c2005-06-23 00:09:35 -0700687 } else {
688 /* Not our break */
689 goto no_kprobe;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700690 }
691 }
692
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700693 p = get_kprobe(addr);
694 if (!p) {
Keshavamurthy Anil S661e5a32005-09-06 15:19:32 -0700695 if (!is_ia64_break_inst(regs)) {
696 /*
697 * The breakpoint instruction was removed right
698 * after we hit it. Another cpu has removed
699 * either a probepoint or a debugger breakpoint
700 * at this address. In either case, no further
701 * handling of this interrupt is appropriate.
702 */
703 ret = 1;
704
705 }
706
707 /* Not one of our break, let kernel handle it */
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700708 goto no_kprobe;
709 }
710
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800711 set_current_kprobe(p, kcb);
712 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700713
714 if (p->pre_handler && p->pre_handler(p, regs))
715 /*
716 * Our pre-handler is specifically requesting that we just
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700717 * do a return. This is used for both the jprobe pre-handler
718 * and the kretprobe trampoline
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700719 */
720 return 1;
721
722ss_probe:
723 prepare_ss(p, regs);
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800724 kcb->kprobe_status = KPROBE_HIT_SS;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700725 return 1;
726
727no_kprobe:
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800728 preempt_enable_no_resched();
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700729 return ret;
730}
731
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700732static int __kprobes post_kprobes_handler(struct pt_regs *regs)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700733{
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800734 struct kprobe *cur = kprobe_running();
735 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
736
737 if (!cur)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700738 return 0;
739
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800740 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
741 kcb->kprobe_status = KPROBE_HIT_SSDONE;
742 cur->post_handler(cur, regs, 0);
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700743 }
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700744
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800745 resume_execution(cur, regs);
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700746
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700747 /*Restore back the original saved kprobes variables and continue. */
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800748 if (kcb->kprobe_status == KPROBE_REENTER) {
749 restore_previous_kprobe(kcb);
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700750 goto out;
751 }
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800752 reset_current_kprobe();
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700753
754out:
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700755 preempt_enable_no_resched();
756 return 1;
757}
758
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700759static int __kprobes kprobes_fault_handler(struct pt_regs *regs, int trapnr)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700760{
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800761 struct kprobe *cur = kprobe_running();
762 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
763
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700764
Prasanna S Panchamukhic04c1c82006-03-26 01:38:25 -0800765 switch(kcb->kprobe_status) {
766 case KPROBE_HIT_SS:
767 case KPROBE_REENTER:
768 /*
769 * We are here because the instruction being single
770 * stepped caused a page fault. We reset the current
771 * kprobe and the instruction pointer points back to
772 * the probe address and allow the page fault handler
773 * to continue as a normal page fault.
774 */
775 regs->cr_iip = ((unsigned long)cur->addr) & ~0xFULL;
776 ia64_psr(regs)->ri = ((unsigned long)cur->addr) & 0xf;
777 if (kcb->kprobe_status == KPROBE_REENTER)
778 restore_previous_kprobe(kcb);
779 else
780 reset_current_kprobe();
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700781 preempt_enable_no_resched();
Prasanna S Panchamukhic04c1c82006-03-26 01:38:25 -0800782 break;
783 case KPROBE_HIT_ACTIVE:
784 case KPROBE_HIT_SSDONE:
785 /*
786 * We increment the nmissed count for accounting,
787 * we can also use npre/npostfault count for accouting
788 * these specific fault cases.
789 */
790 kprobes_inc_nmissed_count(cur);
791
792 /*
793 * We come here because instructions in the pre/post
794 * handler caused the page_fault, this could happen
795 * if handler tries to access user space by
796 * copy_from_user(), get_user() etc. Let the
797 * user-specified handler try to fix it first.
798 */
799 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
800 return 1;
Keshavamurthy Anil Sfd32cb32006-09-25 16:32:20 -0700801 /*
802 * In case the user-specified fault handler returned
803 * zero, try to fix up.
804 */
805 if (ia64_done_with_exception(regs))
806 return 1;
Prasanna S Panchamukhic04c1c82006-03-26 01:38:25 -0800807
808 /*
809 * Let ia64_do_page_fault() fix it.
810 */
811 break;
812 default:
813 break;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700814 }
815
816 return 0;
817}
818
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700819int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
820 unsigned long val, void *data)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700821{
822 struct die_args *args = (struct die_args *)data;
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800823 int ret = NOTIFY_DONE;
824
bibo,mao2326c772006-03-26 01:38:21 -0800825 if (args->regs && user_mode(args->regs))
826 return ret;
827
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700828 switch(val) {
829 case DIE_BREAK:
Keith Owens9138d582005-11-07 11:27:13 -0800830 /* err is break number from ia64_bad_break() */
Tony Luck08ed38b2006-11-14 09:33:38 -0800831 if ((args->err >> 12) == (__IA64_BREAK_KPROBE >> 12)
832 || args->err == __IA64_BREAK_JPROBE
833 || args->err == 0)
Keith Owens9138d582005-11-07 11:27:13 -0800834 if (pre_kprobes_handler(args))
835 ret = NOTIFY_STOP;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700836 break;
Keith Owens9138d582005-11-07 11:27:13 -0800837 case DIE_FAULT:
838 /* err is vector number from ia64_fault() */
839 if (args->err == 36)
840 if (post_kprobes_handler(args->regs))
841 ret = NOTIFY_STOP;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700842 break;
843 case DIE_PAGE_FAULT:
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800844 /* kprobe_running() needs smp_processor_id() */
845 preempt_disable();
846 if (kprobe_running() &&
847 kprobes_fault_handler(args->regs, args->trapnr))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800848 ret = NOTIFY_STOP;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800849 preempt_enable();
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700850 default:
851 break;
852 }
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800853 return ret;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700854}
855
Zhang Yanmind3ef1f52006-01-13 14:45:21 -0800856struct param_bsp_cfm {
857 unsigned long ip;
858 unsigned long *bsp;
859 unsigned long cfm;
860};
861
862static void ia64_get_bsp_cfm(struct unw_frame_info *info, void *arg)
863{
864 unsigned long ip;
865 struct param_bsp_cfm *lp = arg;
866
867 do {
868 unw_get_ip(info, &ip);
869 if (ip == 0)
870 break;
871 if (ip == lp->ip) {
872 unw_get_bsp(info, (unsigned long*)&lp->bsp);
873 unw_get_cfm(info, (unsigned long*)&lp->cfm);
874 return;
875 }
876 } while (unw_unwind(info) >= 0);
Matthew Wilcoxd61b49c2006-10-26 12:22:32 -0600877 lp->bsp = NULL;
Zhang Yanmind3ef1f52006-01-13 14:45:21 -0800878 lp->cfm = 0;
879 return;
880}
881
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700882int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700883{
Anil S Keshavamurthyb2761dc2005-06-23 00:09:28 -0700884 struct jprobe *jp = container_of(p, struct jprobe, kp);
885 unsigned long addr = ((struct fnptr *)(jp->entry))->ip;
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800886 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Zhang Yanmind3ef1f52006-01-13 14:45:21 -0800887 struct param_bsp_cfm pa;
888 int bytes;
889
890 /*
891 * Callee owns the argument space and could overwrite it, eg
892 * tail call optimization. So to be absolutely safe
893 * we save the argument space before transfering the control
894 * to instrumented jprobe function which runs in
895 * the process context
896 */
897 pa.ip = regs->cr_iip;
898 unw_init_running(ia64_get_bsp_cfm, &pa);
899 bytes = (char *)ia64_rse_skip_regs(pa.bsp, pa.cfm & 0x3f)
900 - (char *)pa.bsp;
901 memcpy( kcb->jprobes_saved_stacked_regs,
902 pa.bsp,
903 bytes );
904 kcb->bsp = pa.bsp;
905 kcb->cfm = pa.cfm;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700906
Anil S Keshavamurthyb2761dc2005-06-23 00:09:28 -0700907 /* save architectural state */
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800908 kcb->jprobe_saved_regs = *regs;
Anil S Keshavamurthyb2761dc2005-06-23 00:09:28 -0700909
910 /* after rfi, execute the jprobe instrumented function */
911 regs->cr_iip = addr & ~0xFULL;
912 ia64_psr(regs)->ri = addr & 0xf;
913 regs->r1 = ((struct fnptr *)(jp->entry))->gp;
914
915 /*
916 * fix the return address to our jprobe_inst_return() function
917 * in the jprobes.S file
918 */
bibo,mao62c27be2006-10-02 02:17:33 -0700919 regs->b0 = ((struct fnptr *)(jprobe_inst_return))->ip;
Anil S Keshavamurthyb2761dc2005-06-23 00:09:28 -0700920
921 return 1;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700922}
923
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700924int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700925{
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800926 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Zhang Yanmind3ef1f52006-01-13 14:45:21 -0800927 int bytes;
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800928
Zhang Yanmind3ef1f52006-01-13 14:45:21 -0800929 /* restoring architectural state */
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800930 *regs = kcb->jprobe_saved_regs;
Zhang Yanmind3ef1f52006-01-13 14:45:21 -0800931
932 /* restoring the original argument space */
933 flush_register_stack();
934 bytes = (char *)ia64_rse_skip_regs(kcb->bsp, kcb->cfm & 0x3f)
935 - (char *)kcb->bsp;
936 memcpy( kcb->bsp,
937 kcb->jprobes_saved_stacked_regs,
938 bytes );
939 invalidate_stacked_regs();
940
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800941 preempt_enable_no_resched();
Anil S Keshavamurthyb2761dc2005-06-23 00:09:28 -0700942 return 1;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700943}
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700944
945static struct kprobe trampoline_p = {
946 .pre_handler = trampoline_probe_handler
947};
948
Rusty Lynch67729262005-07-05 18:54:50 -0700949int __init arch_init_kprobes(void)
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700950{
951 trampoline_p.addr =
952 (kprobe_opcode_t *)((struct fnptr *)kretprobe_trampoline)->ip;
953 return register_kprobe(&trampoline_p);
954}