blob: 50ae8c7d453d5075641a191dcfdf5bf0d4087ece [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>
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -070029#include <linux/string.h>
30#include <linux/slab.h>
31#include <linux/preempt.h>
32#include <linux/moduleloader.h>
33
34#include <asm/pgtable.h>
35#include <asm/kdebug.h>
Keshavamurthy Anil Sc7b645f2005-06-27 15:17:16 -070036#include <asm/sections.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
93 * Bits 37:40 Major opcode to be zero
94 * 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;
107 p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
108 break;
109 case IP_RELATIVE_PREDICT_OPCODE:
110 case IP_RELATIVE_BRANCH_OPCODE:
111 p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;
112 break;
113 case IP_RELATIVE_CALL_OPCODE:
114 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;
118 }
119 } else if (bundle_encoding[template][slot] == X) {
120 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,
139 struct kprobe *p)
Anil S Keshavamurthy708de8f2005-06-23 00:09:34 -0700140{
141 unsigned long addr = (unsigned long)p->addr;
142
143 if (bundle_encoding[template][slot] == I) {
144 switch (major_opcode) {
145 case 0x0: //I_UNIT_MISC_OPCODE:
146 /*
147 * Check for Integer speculation instruction
148 * - Bit 33-35 to be equal to 0x1
149 */
150 if (((kprobe_inst >> 33) & 0x7) == 1) {
151 printk(KERN_WARNING
152 "Kprobes on speculation inst at <0x%lx> not supported\n",
153 addr);
154 return -EINVAL;
155 }
156
157 /*
158 * IP relative mov instruction
159 * - Bit 27-35 to be equal to 0x30
160 */
161 if (((kprobe_inst >> 27) & 0x1FF) == 0x30) {
162 printk(KERN_WARNING
163 "Kprobes on \"mov r1=ip\" at <0x%lx> not supported\n",
164 addr);
165 return -EINVAL;
166
167 }
168 }
169 }
170 return 0;
171}
172
173
174/*
175 * In this function we check to see if the instruction
Anil S Keshavamurthy1674eaf2005-06-23 00:09:33 -0700176 * (qp) cmpx.crel.ctype p1,p2=r2,r3
177 * on which we are inserting kprobe is cmp instruction
178 * with ctype as unc.
179 */
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700180static uint __kprobes is_cmp_ctype_unc_inst(uint template, uint slot,
181 uint major_opcode,
182 unsigned long kprobe_inst)
Anil S Keshavamurthy1674eaf2005-06-23 00:09:33 -0700183{
184 cmp_inst_t cmp_inst;
185 uint ctype_unc = 0;
186
187 if (!((bundle_encoding[template][slot] == I) ||
188 (bundle_encoding[template][slot] == M)))
189 goto out;
190
191 if (!((major_opcode == 0xC) || (major_opcode == 0xD) ||
192 (major_opcode == 0xE)))
193 goto out;
194
195 cmp_inst.l = kprobe_inst;
196 if ((cmp_inst.f.x2 == 0) || (cmp_inst.f.x2 == 1)) {
197 /* Integere compare - Register Register (A6 type)*/
198 if ((cmp_inst.f.tb == 0) && (cmp_inst.f.ta == 0)
199 &&(cmp_inst.f.c == 1))
200 ctype_unc = 1;
201 } else if ((cmp_inst.f.x2 == 2)||(cmp_inst.f.x2 == 3)) {
202 /* Integere compare - Immediate Register (A8 type)*/
203 if ((cmp_inst.f.ta == 0) &&(cmp_inst.f.c == 1))
204 ctype_unc = 1;
205 }
206out:
207 return ctype_unc;
208}
209
210/*
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700211 * In this function we override the bundle with
212 * the break instruction at the given slot.
213 */
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700214static void __kprobes prepare_break_inst(uint template, uint slot,
215 uint major_opcode,
216 unsigned long kprobe_inst,
217 struct kprobe *p)
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700218{
219 unsigned long break_inst = BREAK_INST;
220 bundle_t *bundle = &p->ainsn.insn.bundle;
221
222 /*
223 * Copy the original kprobe_inst qualifying predicate(qp)
Anil S Keshavamurthy1674eaf2005-06-23 00:09:33 -0700224 * to the break instruction iff !is_cmp_ctype_unc_inst
225 * because for cmp instruction with ctype equal to unc,
226 * which is a special instruction always needs to be
227 * executed regradless of qp
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700228 */
Anil S Keshavamurthy1674eaf2005-06-23 00:09:33 -0700229 if (!is_cmp_ctype_unc_inst(template, slot, major_opcode, kprobe_inst))
230 break_inst |= (0x3f & kprobe_inst);
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700231
232 switch (slot) {
233 case 0:
234 bundle->quad0.slot0 = break_inst;
235 break;
236 case 1:
237 bundle->quad0.slot1_p0 = break_inst;
238 bundle->quad1.slot1_p1 = break_inst >> (64-46);
239 break;
240 case 2:
241 bundle->quad1.slot2 = break_inst;
242 break;
243 }
244
245 /*
246 * Update the instruction flag, so that we can
247 * emulate the instruction properly after we
248 * single step on original instruction
249 */
250 update_kprobe_inst_flag(template, slot, major_opcode, kprobe_inst, p);
251}
252
253static inline void get_kprobe_inst(bundle_t *bundle, uint slot,
254 unsigned long *kprobe_inst, uint *major_opcode)
255{
256 unsigned long kprobe_inst_p0, kprobe_inst_p1;
257 unsigned int template;
258
259 template = bundle->quad0.template;
260
261 switch (slot) {
262 case 0:
263 *major_opcode = (bundle->quad0.slot0 >> SLOT0_OPCODE_SHIFT);
264 *kprobe_inst = bundle->quad0.slot0;
265 break;
266 case 1:
267 *major_opcode = (bundle->quad1.slot1_p1 >> SLOT1_p1_OPCODE_SHIFT);
268 kprobe_inst_p0 = bundle->quad0.slot1_p0;
269 kprobe_inst_p1 = bundle->quad1.slot1_p1;
270 *kprobe_inst = kprobe_inst_p0 | (kprobe_inst_p1 << (64-46));
271 break;
272 case 2:
273 *major_opcode = (bundle->quad1.slot2 >> SLOT2_OPCODE_SHIFT);
274 *kprobe_inst = bundle->quad1.slot2;
275 break;
276 }
277}
278
Keshavamurthy Anil Sc7b645f2005-06-27 15:17:16 -0700279/* Returns non-zero if the addr is in the Interrupt Vector Table */
280static inline int in_ivt_functions(unsigned long addr)
281{
282 return (addr >= (unsigned long)__start_ivt_text
283 && addr < (unsigned long)__end_ivt_text);
284}
285
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700286static int __kprobes valid_kprobe_addr(int template, int slot,
287 unsigned long addr)
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700288{
289 if ((slot > 2) || ((bundle_encoding[template][1] == L) && slot > 1)) {
Keshavamurthy Anil Sc7b645f2005-06-27 15:17:16 -0700290 printk(KERN_WARNING "Attempting to insert unaligned kprobe "
291 "at 0x%lx\n", addr);
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700292 return -EINVAL;
293 }
Rusty Lyncha528e212005-06-27 15:17:15 -0700294
Keshavamurthy Anil Sc7b645f2005-06-27 15:17:16 -0700295 if (in_ivt_functions(addr)) {
296 printk(KERN_WARNING "Kprobes can't be inserted inside "
297 "IVT functions at 0x%lx\n", addr);
298 return -EINVAL;
299 }
300
Rusty Lyncha528e212005-06-27 15:17:15 -0700301 if (slot == 1 && bundle_encoding[template][1] != L) {
302 printk(KERN_WARNING "Inserting kprobes on slot #1 "
303 "is not supported\n");
304 return -EINVAL;
305 }
306
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700307 return 0;
308}
Rusty Lynch8bc76772005-06-23 00:09:30 -0700309
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800310static inline void save_previous_kprobe(struct kprobe_ctlblk *kcb)
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700311{
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800312 kcb->prev_kprobe.kp = kprobe_running();
313 kcb->prev_kprobe.status = kcb->kprobe_status;
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700314}
315
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800316static inline void restore_previous_kprobe(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) = kcb->prev_kprobe.kp;
319 kcb->kprobe_status = kcb->prev_kprobe.status;
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700320}
321
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800322static inline void set_current_kprobe(struct kprobe *p,
323 struct kprobe_ctlblk *kcb)
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700324{
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800325 __get_cpu_var(current_kprobe) = p;
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700326}
327
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700328static void kretprobe_trampoline(void)
329{
330}
331
332/*
333 * At this point the target function has been tricked into
334 * returning into our trampoline. Lookup the associated instance
335 * and then:
336 * - call the handler function
337 * - cleanup by marking the instance as unused
338 * - long jump back to the original return address
339 */
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700340int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700341{
342 struct kretprobe_instance *ri = NULL;
343 struct hlist_head *head;
344 struct hlist_node *node, *tmp;
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800345 unsigned long flags, orig_ret_address = 0;
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700346 unsigned long trampoline_address =
347 ((struct fnptr *)kretprobe_trampoline)->ip;
348
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800349 spin_lock_irqsave(&kretprobe_lock, flags);
Keith Owens9138d582005-11-07 11:27:13 -0800350 head = kretprobe_inst_table_head(current);
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700351
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) {
Keith Owens9138d582005-11-07 11:27:13 -0800366 if (ri->task != current)
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700367 /* another task is sharing our hash bucket */
Keith Owens9138d582005-11-07 11:27:13 -0800368 continue;
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700369
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();
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800389 spin_unlock_irqrestore(&kretprobe_lock, flags);
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700390 preempt_enable_no_resched();
391
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800392 /*
393 * By returning a non-zero value, we are telling
394 * kprobe_handler() that we don't want the post_handler
395 * to run (and have re-enabled preemption)
396 */
Keith Owens9138d582005-11-07 11:27:13 -0800397 return 1;
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700398}
399
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800400/* Called with kretprobe_lock held */
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700401void __kprobes arch_prepare_kretprobe(struct kretprobe *rp,
402 struct pt_regs *regs)
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700403{
404 struct kretprobe_instance *ri;
405
406 if ((ri = get_free_rp_inst(rp)) != NULL) {
407 ri->rp = rp;
408 ri->task = current;
409 ri->ret_addr = (kprobe_opcode_t *)regs->b0;
410
411 /* Replace the return addr with trampoline addr */
412 regs->b0 = ((struct fnptr *)kretprobe_trampoline)->ip;
413
414 add_rp_inst(ri);
415 } else {
416 rp->nmissed++;
417 }
418}
419
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700420int __kprobes arch_prepare_kprobe(struct kprobe *p)
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700421{
422 unsigned long addr = (unsigned long) p->addr;
423 unsigned long *kprobe_addr = (unsigned long *)(addr & ~0xFULL);
424 unsigned long kprobe_inst=0;
425 unsigned int slot = addr & 0xf, template, major_opcode = 0;
426 bundle_t *bundle = &p->ainsn.insn.bundle;
Rusty Lynch8bc76772005-06-23 00:09:30 -0700427
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700428 memcpy(&p->opcode.bundle, kprobe_addr, sizeof(bundle_t));
429 memcpy(&p->ainsn.insn.bundle, kprobe_addr, sizeof(bundle_t));
Rusty Lynch8bc76772005-06-23 00:09:30 -0700430
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700431 template = bundle->quad0.template;
432
433 if(valid_kprobe_addr(template, slot, addr))
434 return -EINVAL;
435
436 /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
437 if (slot == 1 && bundle_encoding[template][1] == L)
438 slot++;
439
440 /* Get kprobe_inst and major_opcode from the bundle */
441 get_kprobe_inst(bundle, slot, &kprobe_inst, &major_opcode);
442
Anil S Keshavamurthy708de8f2005-06-23 00:09:34 -0700443 if (unsupported_inst(template, slot, major_opcode, kprobe_inst, p))
444 return -EINVAL;
445
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700446 prepare_break_inst(template, slot, major_opcode, kprobe_inst, p);
Rusty Lynch8bc76772005-06-23 00:09:30 -0700447
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700448 return 0;
449}
450
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700451void __kprobes arch_arm_kprobe(struct kprobe *p)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700452{
453 unsigned long addr = (unsigned long)p->addr;
454 unsigned long arm_addr = addr & ~0xFULL;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700455
Rusty Lynch8bc76772005-06-23 00:09:30 -0700456 memcpy((char *)arm_addr, &p->ainsn.insn.bundle, sizeof(bundle_t));
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700457 flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t));
458}
459
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700460void __kprobes arch_disarm_kprobe(struct kprobe *p)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700461{
462 unsigned long addr = (unsigned long)p->addr;
463 unsigned long arm_addr = addr & ~0xFULL;
464
465 /* p->opcode contains the original unaltered bundle */
466 memcpy((char *) arm_addr, (char *) &p->opcode.bundle, sizeof(bundle_t));
467 flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t));
468}
469
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700470/*
471 * We are resuming execution after a single step fault, so the pt_regs
472 * structure reflects the register state after we executed the instruction
473 * located in the kprobe (p->ainsn.insn.bundle). We still need to adjust
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700474 * the ip to point back to the original stack address. To set the IP address
475 * to original stack address, handle the case where we need to fixup the
476 * relative IP address and/or fixup branch register.
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700477 */
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700478static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700479{
Rusty Lynch8bc76772005-06-23 00:09:30 -0700480 unsigned long bundle_addr = ((unsigned long) (&p->opcode.bundle)) & ~0xFULL;
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700481 unsigned long resume_addr = (unsigned long)p->addr & ~0xFULL;
482 unsigned long template;
483 int slot = ((unsigned long)p->addr & 0xf);
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700484
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700485 template = p->opcode.bundle.quad0.template;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700486
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700487 if (slot == 1 && bundle_encoding[template][1] == L)
488 slot = 2;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700489
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700490 if (p->ainsn.inst_flag) {
491
492 if (p->ainsn.inst_flag & INST_FLAG_FIX_RELATIVE_IP_ADDR) {
493 /* Fix relative IP address */
494 regs->cr_iip = (regs->cr_iip - bundle_addr) + resume_addr;
495 }
496
497 if (p->ainsn.inst_flag & INST_FLAG_FIX_BRANCH_REG) {
498 /*
499 * Fix target branch register, software convention is
500 * to use either b0 or b6 or b7, so just checking
501 * only those registers
502 */
503 switch (p->ainsn.target_br_reg) {
504 case 0:
505 if ((regs->b0 == bundle_addr) ||
506 (regs->b0 == bundle_addr + 0x10)) {
507 regs->b0 = (regs->b0 - bundle_addr) +
508 resume_addr;
509 }
510 break;
511 case 6:
512 if ((regs->b6 == bundle_addr) ||
513 (regs->b6 == bundle_addr + 0x10)) {
514 regs->b6 = (regs->b6 - bundle_addr) +
515 resume_addr;
516 }
517 break;
518 case 7:
519 if ((regs->b7 == bundle_addr) ||
520 (regs->b7 == bundle_addr + 0x10)) {
521 regs->b7 = (regs->b7 - bundle_addr) +
522 resume_addr;
523 }
524 break;
525 } /* end switch */
526 }
527 goto turn_ss_off;
528 }
529
530 if (slot == 2) {
531 if (regs->cr_iip == bundle_addr + 0x10) {
532 regs->cr_iip = resume_addr + 0x10;
533 }
534 } else {
535 if (regs->cr_iip == bundle_addr) {
536 regs->cr_iip = resume_addr;
537 }
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700538 }
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700539
540turn_ss_off:
541 /* Turn off Single Step bit */
542 ia64_psr(regs)->ss = 0;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700543}
544
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700545static void __kprobes prepare_ss(struct kprobe *p, struct pt_regs *regs)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700546{
Rusty Lynch8bc76772005-06-23 00:09:30 -0700547 unsigned long bundle_addr = (unsigned long) &p->opcode.bundle;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700548 unsigned long slot = (unsigned long)p->addr & 0xf;
549
Keshavamurthy Anil Sdeac66a2005-09-06 15:19:35 -0700550 /* single step inline if break instruction */
551 if (p->ainsn.inst_flag == INST_FLAG_BREAK_INST)
552 regs->cr_iip = (unsigned long)p->addr & ~0xFULL;
553 else
554 regs->cr_iip = bundle_addr & ~0xFULL;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700555
556 if (slot > 2)
557 slot = 0;
558
559 ia64_psr(regs)->ri = slot;
560
561 /* turn on single stepping */
562 ia64_psr(regs)->ss = 1;
563}
564
Keshavamurthy Anil S661e5a32005-09-06 15:19:32 -0700565static int __kprobes is_ia64_break_inst(struct pt_regs *regs)
566{
567 unsigned int slot = ia64_psr(regs)->ri;
568 unsigned int template, major_opcode;
569 unsigned long kprobe_inst;
570 unsigned long *kprobe_addr = (unsigned long *)regs->cr_iip;
571 bundle_t bundle;
572
573 memcpy(&bundle, kprobe_addr, sizeof(bundle_t));
574 template = bundle.quad0.template;
575
576 /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
577 if (slot == 1 && bundle_encoding[template][1] == L)
578 slot++;
579
580 /* Get Kprobe probe instruction at given slot*/
581 get_kprobe_inst(&bundle, slot, &kprobe_inst, &major_opcode);
582
583 /* For break instruction,
584 * Bits 37:40 Major opcode to be zero
585 * Bits 27:32 X6 to be zero
586 * Bits 32:35 X3 to be zero
587 */
588 if (major_opcode || ((kprobe_inst >> 27) & 0x1FF) ) {
589 /* Not a break instruction */
590 return 0;
591 }
592
593 /* Is a break instruction */
594 return 1;
595}
596
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700597static int __kprobes pre_kprobes_handler(struct die_args *args)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700598{
599 struct kprobe *p;
600 int ret = 0;
Keshavamurthy Anil S89cb14c2005-06-23 00:09:35 -0700601 struct pt_regs *regs = args->regs;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700602 kprobe_opcode_t *addr = (kprobe_opcode_t *)instruction_pointer(regs);
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800603 struct kprobe_ctlblk *kcb;
604
605 /*
606 * We don't want to be preempted for the entire
607 * duration of kprobe processing
608 */
609 preempt_disable();
610 kcb = get_kprobe_ctlblk();
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700611
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700612 /* Handle recursion cases */
613 if (kprobe_running()) {
614 p = get_kprobe(addr);
615 if (p) {
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800616 if ((kcb->kprobe_status == KPROBE_HIT_SS) &&
Keshavamurthy Anil Sdeac66a2005-09-06 15:19:35 -0700617 (p->ainsn.inst_flag == INST_FLAG_BREAK_INST)) {
618 ia64_psr(regs)->ss = 0;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700619 goto no_kprobe;
620 }
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700621 /* We have reentered the pre_kprobe_handler(), since
622 * another probe was hit while within the handler.
623 * We here save the original kprobes variables and
624 * just single step on the instruction of the new probe
625 * without calling any user handlers.
626 */
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800627 save_previous_kprobe(kcb);
628 set_current_kprobe(p, kcb);
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -0800629 kprobes_inc_nmissed_count(p);
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700630 prepare_ss(p, regs);
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800631 kcb->kprobe_status = KPROBE_REENTER;
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700632 return 1;
Keshavamurthy Anil S89cb14c2005-06-23 00:09:35 -0700633 } else if (args->err == __IA64_BREAK_JPROBE) {
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700634 /*
635 * jprobe instrumented function just completed
636 */
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800637 p = __get_cpu_var(current_kprobe);
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700638 if (p->break_handler && p->break_handler(p, regs)) {
639 goto ss_probe;
640 }
Keshavamurthy Anil Seb3a7292006-01-11 12:17:42 -0800641 } else if (!is_ia64_break_inst(regs)) {
642 /* The breakpoint instruction was removed by
643 * another cpu right after we hit, no further
644 * handling of this interrupt is appropriate
645 */
646 ret = 1;
647 goto no_kprobe;
Keshavamurthy Anil S89cb14c2005-06-23 00:09:35 -0700648 } else {
649 /* Not our break */
650 goto no_kprobe;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700651 }
652 }
653
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700654 p = get_kprobe(addr);
655 if (!p) {
Keshavamurthy Anil S661e5a32005-09-06 15:19:32 -0700656 if (!is_ia64_break_inst(regs)) {
657 /*
658 * The breakpoint instruction was removed right
659 * after we hit it. Another cpu has removed
660 * either a probepoint or a debugger breakpoint
661 * at this address. In either case, no further
662 * handling of this interrupt is appropriate.
663 */
664 ret = 1;
665
666 }
667
668 /* Not one of our break, let kernel handle it */
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700669 goto no_kprobe;
670 }
671
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800672 set_current_kprobe(p, kcb);
673 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700674
675 if (p->pre_handler && p->pre_handler(p, regs))
676 /*
677 * Our pre-handler is specifically requesting that we just
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700678 * do a return. This is used for both the jprobe pre-handler
679 * and the kretprobe trampoline
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700680 */
681 return 1;
682
683ss_probe:
684 prepare_ss(p, regs);
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800685 kcb->kprobe_status = KPROBE_HIT_SS;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700686 return 1;
687
688no_kprobe:
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800689 preempt_enable_no_resched();
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700690 return ret;
691}
692
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700693static int __kprobes post_kprobes_handler(struct pt_regs *regs)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700694{
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800695 struct kprobe *cur = kprobe_running();
696 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
697
698 if (!cur)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700699 return 0;
700
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800701 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
702 kcb->kprobe_status = KPROBE_HIT_SSDONE;
703 cur->post_handler(cur, regs, 0);
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700704 }
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700705
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800706 resume_execution(cur, regs);
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700707
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700708 /*Restore back the original saved kprobes variables and continue. */
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800709 if (kcb->kprobe_status == KPROBE_REENTER) {
710 restore_previous_kprobe(kcb);
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700711 goto out;
712 }
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800713 reset_current_kprobe();
Anil S Keshavamurthy852cacc2005-06-23 00:09:40 -0700714
715out:
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700716 preempt_enable_no_resched();
717 return 1;
718}
719
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700720static int __kprobes kprobes_fault_handler(struct pt_regs *regs, int trapnr)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700721{
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800722 struct kprobe *cur = kprobe_running();
723 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
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 preempt_enable_no_resched();
732 }
733
734 return 0;
735}
736
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700737int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
738 unsigned long val, void *data)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700739{
740 struct die_args *args = (struct die_args *)data;
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800741 int ret = NOTIFY_DONE;
742
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700743 switch(val) {
744 case DIE_BREAK:
Keith Owens9138d582005-11-07 11:27:13 -0800745 /* err is break number from ia64_bad_break() */
Keshavamurthy Anil S5a94bcf2005-11-22 14:15:49 -0800746 if (args->err == 0x80200 || args->err == 0x80300 || args->err == 0)
Keith Owens9138d582005-11-07 11:27:13 -0800747 if (pre_kprobes_handler(args))
748 ret = NOTIFY_STOP;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700749 break;
Keith Owens9138d582005-11-07 11:27:13 -0800750 case DIE_FAULT:
751 /* err is vector number from ia64_fault() */
752 if (args->err == 36)
753 if (post_kprobes_handler(args->regs))
754 ret = NOTIFY_STOP;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700755 break;
756 case DIE_PAGE_FAULT:
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800757 /* kprobe_running() needs smp_processor_id() */
758 preempt_disable();
759 if (kprobe_running() &&
760 kprobes_fault_handler(args->regs, args->trapnr))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800761 ret = NOTIFY_STOP;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800762 preempt_enable();
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700763 default:
764 break;
765 }
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800766 return ret;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700767}
768
Zhang Yanmind3ef1f52006-01-13 14:45:21 -0800769struct param_bsp_cfm {
770 unsigned long ip;
771 unsigned long *bsp;
772 unsigned long cfm;
773};
774
775static void ia64_get_bsp_cfm(struct unw_frame_info *info, void *arg)
776{
777 unsigned long ip;
778 struct param_bsp_cfm *lp = arg;
779
780 do {
781 unw_get_ip(info, &ip);
782 if (ip == 0)
783 break;
784 if (ip == lp->ip) {
785 unw_get_bsp(info, (unsigned long*)&lp->bsp);
786 unw_get_cfm(info, (unsigned long*)&lp->cfm);
787 return;
788 }
789 } while (unw_unwind(info) >= 0);
790 lp->bsp = 0;
791 lp->cfm = 0;
792 return;
793}
794
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700795int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700796{
Anil S Keshavamurthyb2761dc2005-06-23 00:09:28 -0700797 struct jprobe *jp = container_of(p, struct jprobe, kp);
798 unsigned long addr = ((struct fnptr *)(jp->entry))->ip;
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800799 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Zhang Yanmind3ef1f52006-01-13 14:45:21 -0800800 struct param_bsp_cfm pa;
801 int bytes;
802
803 /*
804 * Callee owns the argument space and could overwrite it, eg
805 * tail call optimization. So to be absolutely safe
806 * we save the argument space before transfering the control
807 * to instrumented jprobe function which runs in
808 * the process context
809 */
810 pa.ip = regs->cr_iip;
811 unw_init_running(ia64_get_bsp_cfm, &pa);
812 bytes = (char *)ia64_rse_skip_regs(pa.bsp, pa.cfm & 0x3f)
813 - (char *)pa.bsp;
814 memcpy( kcb->jprobes_saved_stacked_regs,
815 pa.bsp,
816 bytes );
817 kcb->bsp = pa.bsp;
818 kcb->cfm = pa.cfm;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700819
Anil S Keshavamurthyb2761dc2005-06-23 00:09:28 -0700820 /* save architectural state */
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800821 kcb->jprobe_saved_regs = *regs;
Anil S Keshavamurthyb2761dc2005-06-23 00:09:28 -0700822
823 /* after rfi, execute the jprobe instrumented function */
824 regs->cr_iip = addr & ~0xFULL;
825 ia64_psr(regs)->ri = addr & 0xf;
826 regs->r1 = ((struct fnptr *)(jp->entry))->gp;
827
828 /*
829 * fix the return address to our jprobe_inst_return() function
830 * in the jprobes.S file
831 */
832 regs->b0 = ((struct fnptr *)(jprobe_inst_return))->ip;
833
834 return 1;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700835}
836
Prasanna S Panchamukhi1f7ad572005-09-06 15:19:30 -0700837int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700838{
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800839 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Zhang Yanmind3ef1f52006-01-13 14:45:21 -0800840 int bytes;
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800841
Zhang Yanmind3ef1f52006-01-13 14:45:21 -0800842 /* restoring architectural state */
Ananth N Mavinakayanahalli8a5c4dc2005-11-07 01:00:09 -0800843 *regs = kcb->jprobe_saved_regs;
Zhang Yanmind3ef1f52006-01-13 14:45:21 -0800844
845 /* restoring the original argument space */
846 flush_register_stack();
847 bytes = (char *)ia64_rse_skip_regs(kcb->bsp, kcb->cfm & 0x3f)
848 - (char *)kcb->bsp;
849 memcpy( kcb->bsp,
850 kcb->jprobes_saved_stacked_regs,
851 bytes );
852 invalidate_stacked_regs();
853
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800854 preempt_enable_no_resched();
Anil S Keshavamurthyb2761dc2005-06-23 00:09:28 -0700855 return 1;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700856}
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700857
858static struct kprobe trampoline_p = {
859 .pre_handler = trampoline_probe_handler
860};
861
Rusty Lynch67729262005-07-05 18:54:50 -0700862int __init arch_init_kprobes(void)
Rusty Lynch9508dbf2005-06-27 15:17:12 -0700863{
864 trampoline_p.addr =
865 (kprobe_opcode_t *)((struct fnptr *)kretprobe_trampoline)->ip;
866 return register_kprobe(&trampoline_p);
867}