blob: 5d53446815553892449e8d99bb8f249629996362 [file] [log] [blame]
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -07001/*
2 * Kernel Probes (KProbes)
3 * arch/ia64/kernel/kprobes.c
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 * Copyright (C) IBM Corporation, 2002, 2004
20 * Copyright (C) Intel Corporation, 2005
21 *
22 * 2005-Apr Rusty Lynch <rusty.lynch@intel.com> and Anil S Keshavamurthy
23 * <anil.s.keshavamurthy@intel.com> adapted from i386
24 */
25
26#include <linux/config.h>
27#include <linux/kprobes.h>
28#include <linux/ptrace.h>
29#include <linux/spinlock.h>
30#include <linux/string.h>
31#include <linux/slab.h>
32#include <linux/preempt.h>
33#include <linux/moduleloader.h>
34
35#include <asm/pgtable.h>
36#include <asm/kdebug.h>
37
Anil S Keshavamurthyb2761dc2005-06-23 00:09:28 -070038extern void jprobe_inst_return(void);
39
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -070040/* kprobe_status settings */
41#define KPROBE_HIT_ACTIVE 0x00000001
42#define KPROBE_HIT_SS 0x00000002
43
44static struct kprobe *current_kprobe;
45static unsigned long kprobe_status;
Anil S Keshavamurthyb2761dc2005-06-23 00:09:28 -070046static struct pt_regs jprobe_saved_regs;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -070047
48enum instruction_type {A, I, M, F, B, L, X, u};
49static enum instruction_type bundle_encoding[32][3] = {
50 { M, I, I }, /* 00 */
51 { M, I, I }, /* 01 */
52 { M, I, I }, /* 02 */
53 { M, I, I }, /* 03 */
54 { M, L, X }, /* 04 */
55 { M, L, X }, /* 05 */
56 { u, u, u }, /* 06 */
57 { u, u, u }, /* 07 */
58 { M, M, I }, /* 08 */
59 { M, M, I }, /* 09 */
60 { M, M, I }, /* 0A */
61 { M, M, I }, /* 0B */
62 { M, F, I }, /* 0C */
63 { M, F, I }, /* 0D */
64 { M, M, F }, /* 0E */
65 { M, M, F }, /* 0F */
66 { M, I, B }, /* 10 */
67 { M, I, B }, /* 11 */
68 { M, B, B }, /* 12 */
69 { M, B, B }, /* 13 */
70 { u, u, u }, /* 14 */
71 { u, u, u }, /* 15 */
72 { B, B, B }, /* 16 */
73 { B, B, B }, /* 17 */
74 { M, M, B }, /* 18 */
75 { M, M, B }, /* 19 */
76 { u, u, u }, /* 1A */
77 { u, u, u }, /* 1B */
78 { M, F, B }, /* 1C */
79 { M, F, B }, /* 1D */
80 { u, u, u }, /* 1E */
81 { u, u, u }, /* 1F */
82};
83
Anil S Keshavamurthya5403182005-06-23 00:09:32 -070084/*
85 * In this function we check to see if the instruction
86 * is IP relative instruction and update the kprobe
87 * inst flag accordingly
88 */
89static void update_kprobe_inst_flag(uint template, uint slot, uint major_opcode,
90 unsigned long kprobe_inst, struct kprobe *p)
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -070091{
Rusty Lynch8bc76772005-06-23 00:09:30 -070092 p->ainsn.inst_flag = 0;
93 p->ainsn.target_br_reg = 0;
94
Anil S Keshavamurthya5403182005-06-23 00:09:32 -070095 if (bundle_encoding[template][slot] == B) {
96 switch (major_opcode) {
97 case INDIRECT_CALL_OPCODE:
98 p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
99 p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
100 break;
101 case IP_RELATIVE_PREDICT_OPCODE:
102 case IP_RELATIVE_BRANCH_OPCODE:
103 p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;
104 break;
105 case IP_RELATIVE_CALL_OPCODE:
106 p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;
107 p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
108 p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
109 break;
110 }
111 } else if (bundle_encoding[template][slot] == X) {
112 switch (major_opcode) {
113 case LONG_CALL_OPCODE:
114 p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
115 p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
116 break;
117 }
118 }
119 return;
120}
Rusty Lynch8bc76772005-06-23 00:09:30 -0700121
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700122/*
123 * In this function we override the bundle with
124 * the break instruction at the given slot.
125 */
126static void prepare_break_inst(uint template, uint slot, uint major_opcode,
127 unsigned long kprobe_inst, struct kprobe *p)
128{
129 unsigned long break_inst = BREAK_INST;
130 bundle_t *bundle = &p->ainsn.insn.bundle;
131
132 /*
133 * Copy the original kprobe_inst qualifying predicate(qp)
134 * to the break instruction
135 */
136 break_inst |= (0x3f & kprobe_inst);
137
138 switch (slot) {
139 case 0:
140 bundle->quad0.slot0 = break_inst;
141 break;
142 case 1:
143 bundle->quad0.slot1_p0 = break_inst;
144 bundle->quad1.slot1_p1 = break_inst >> (64-46);
145 break;
146 case 2:
147 bundle->quad1.slot2 = break_inst;
148 break;
149 }
150
151 /*
152 * Update the instruction flag, so that we can
153 * emulate the instruction properly after we
154 * single step on original instruction
155 */
156 update_kprobe_inst_flag(template, slot, major_opcode, kprobe_inst, p);
157}
158
159static inline void get_kprobe_inst(bundle_t *bundle, uint slot,
160 unsigned long *kprobe_inst, uint *major_opcode)
161{
162 unsigned long kprobe_inst_p0, kprobe_inst_p1;
163 unsigned int template;
164
165 template = bundle->quad0.template;
166
167 switch (slot) {
168 case 0:
169 *major_opcode = (bundle->quad0.slot0 >> SLOT0_OPCODE_SHIFT);
170 *kprobe_inst = bundle->quad0.slot0;
171 break;
172 case 1:
173 *major_opcode = (bundle->quad1.slot1_p1 >> SLOT1_p1_OPCODE_SHIFT);
174 kprobe_inst_p0 = bundle->quad0.slot1_p0;
175 kprobe_inst_p1 = bundle->quad1.slot1_p1;
176 *kprobe_inst = kprobe_inst_p0 | (kprobe_inst_p1 << (64-46));
177 break;
178 case 2:
179 *major_opcode = (bundle->quad1.slot2 >> SLOT2_OPCODE_SHIFT);
180 *kprobe_inst = bundle->quad1.slot2;
181 break;
182 }
183}
184
185static int valid_kprobe_addr(int template, int slot, unsigned long addr)
186{
187 if ((slot > 2) || ((bundle_encoding[template][1] == L) && slot > 1)) {
Rusty Lynch8bc76772005-06-23 00:09:30 -0700188 printk(KERN_WARNING "Attempting to insert unaligned kprobe at 0x%lx\n",
189 addr);
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700190 return -EINVAL;
191 }
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700192 return 0;
193}
Rusty Lynch8bc76772005-06-23 00:09:30 -0700194
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700195int arch_prepare_kprobe(struct kprobe *p)
196{
197 unsigned long addr = (unsigned long) p->addr;
198 unsigned long *kprobe_addr = (unsigned long *)(addr & ~0xFULL);
199 unsigned long kprobe_inst=0;
200 unsigned int slot = addr & 0xf, template, major_opcode = 0;
201 bundle_t *bundle = &p->ainsn.insn.bundle;
Rusty Lynch8bc76772005-06-23 00:09:30 -0700202
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700203 memcpy(&p->opcode.bundle, kprobe_addr, sizeof(bundle_t));
204 memcpy(&p->ainsn.insn.bundle, kprobe_addr, sizeof(bundle_t));
Rusty Lynch8bc76772005-06-23 00:09:30 -0700205
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700206 template = bundle->quad0.template;
207
208 if(valid_kprobe_addr(template, slot, addr))
209 return -EINVAL;
210
211 /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
212 if (slot == 1 && bundle_encoding[template][1] == L)
213 slot++;
214
215 /* Get kprobe_inst and major_opcode from the bundle */
216 get_kprobe_inst(bundle, slot, &kprobe_inst, &major_opcode);
217
218 prepare_break_inst(template, slot, major_opcode, kprobe_inst, p);
Rusty Lynch8bc76772005-06-23 00:09:30 -0700219
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700220 return 0;
221}
222
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700223void arch_arm_kprobe(struct kprobe *p)
224{
225 unsigned long addr = (unsigned long)p->addr;
226 unsigned long arm_addr = addr & ~0xFULL;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700227
Rusty Lynch8bc76772005-06-23 00:09:30 -0700228 memcpy((char *)arm_addr, &p->ainsn.insn.bundle, sizeof(bundle_t));
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700229 flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t));
230}
231
232void arch_disarm_kprobe(struct kprobe *p)
233{
234 unsigned long addr = (unsigned long)p->addr;
235 unsigned long arm_addr = addr & ~0xFULL;
236
237 /* p->opcode contains the original unaltered bundle */
238 memcpy((char *) arm_addr, (char *) &p->opcode.bundle, sizeof(bundle_t));
239 flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t));
240}
241
242void arch_remove_kprobe(struct kprobe *p)
243{
244}
245
246/*
247 * We are resuming execution after a single step fault, so the pt_regs
248 * structure reflects the register state after we executed the instruction
249 * located in the kprobe (p->ainsn.insn.bundle). We still need to adjust
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700250 * the ip to point back to the original stack address. To set the IP address
251 * to original stack address, handle the case where we need to fixup the
252 * relative IP address and/or fixup branch register.
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700253 */
254static void resume_execution(struct kprobe *p, struct pt_regs *regs)
255{
Rusty Lynch8bc76772005-06-23 00:09:30 -0700256 unsigned long bundle_addr = ((unsigned long) (&p->opcode.bundle)) & ~0xFULL;
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700257 unsigned long resume_addr = (unsigned long)p->addr & ~0xFULL;
258 unsigned long template;
259 int slot = ((unsigned long)p->addr & 0xf);
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700260
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700261 template = p->opcode.bundle.quad0.template;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700262
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700263 if (slot == 1 && bundle_encoding[template][1] == L)
264 slot = 2;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700265
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700266 if (p->ainsn.inst_flag) {
267
268 if (p->ainsn.inst_flag & INST_FLAG_FIX_RELATIVE_IP_ADDR) {
269 /* Fix relative IP address */
270 regs->cr_iip = (regs->cr_iip - bundle_addr) + resume_addr;
271 }
272
273 if (p->ainsn.inst_flag & INST_FLAG_FIX_BRANCH_REG) {
274 /*
275 * Fix target branch register, software convention is
276 * to use either b0 or b6 or b7, so just checking
277 * only those registers
278 */
279 switch (p->ainsn.target_br_reg) {
280 case 0:
281 if ((regs->b0 == bundle_addr) ||
282 (regs->b0 == bundle_addr + 0x10)) {
283 regs->b0 = (regs->b0 - bundle_addr) +
284 resume_addr;
285 }
286 break;
287 case 6:
288 if ((regs->b6 == bundle_addr) ||
289 (regs->b6 == bundle_addr + 0x10)) {
290 regs->b6 = (regs->b6 - bundle_addr) +
291 resume_addr;
292 }
293 break;
294 case 7:
295 if ((regs->b7 == bundle_addr) ||
296 (regs->b7 == bundle_addr + 0x10)) {
297 regs->b7 = (regs->b7 - bundle_addr) +
298 resume_addr;
299 }
300 break;
301 } /* end switch */
302 }
303 goto turn_ss_off;
304 }
305
306 if (slot == 2) {
307 if (regs->cr_iip == bundle_addr + 0x10) {
308 regs->cr_iip = resume_addr + 0x10;
309 }
310 } else {
311 if (regs->cr_iip == bundle_addr) {
312 regs->cr_iip = resume_addr;
313 }
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700314 }
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700315
316turn_ss_off:
317 /* Turn off Single Step bit */
318 ia64_psr(regs)->ss = 0;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700319}
320
321static void prepare_ss(struct kprobe *p, struct pt_regs *regs)
322{
Rusty Lynch8bc76772005-06-23 00:09:30 -0700323 unsigned long bundle_addr = (unsigned long) &p->opcode.bundle;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700324 unsigned long slot = (unsigned long)p->addr & 0xf;
325
326 /* Update instruction pointer (IIP) and slot number (IPSR.ri) */
327 regs->cr_iip = bundle_addr & ~0xFULL;
328
329 if (slot > 2)
330 slot = 0;
331
332 ia64_psr(regs)->ri = slot;
333
334 /* turn on single stepping */
335 ia64_psr(regs)->ss = 1;
336}
337
338static int pre_kprobes_handler(struct pt_regs *regs)
339{
340 struct kprobe *p;
341 int ret = 0;
342 kprobe_opcode_t *addr = (kprobe_opcode_t *)instruction_pointer(regs);
343
344 preempt_disable();
345
346 /* Handle recursion cases */
347 if (kprobe_running()) {
348 p = get_kprobe(addr);
349 if (p) {
350 if (kprobe_status == KPROBE_HIT_SS) {
351 unlock_kprobes();
352 goto no_kprobe;
353 }
354 arch_disarm_kprobe(p);
355 ret = 1;
356 } else {
357 /*
358 * jprobe instrumented function just completed
359 */
360 p = current_kprobe;
361 if (p->break_handler && p->break_handler(p, regs)) {
362 goto ss_probe;
363 }
364 }
365 }
366
367 lock_kprobes();
368 p = get_kprobe(addr);
369 if (!p) {
370 unlock_kprobes();
371 goto no_kprobe;
372 }
373
374 kprobe_status = KPROBE_HIT_ACTIVE;
375 current_kprobe = p;
376
377 if (p->pre_handler && p->pre_handler(p, regs))
378 /*
379 * Our pre-handler is specifically requesting that we just
380 * do a return. This is handling the case where the
381 * pre-handler is really our special jprobe pre-handler.
382 */
383 return 1;
384
385ss_probe:
386 prepare_ss(p, regs);
387 kprobe_status = KPROBE_HIT_SS;
388 return 1;
389
390no_kprobe:
391 preempt_enable_no_resched();
392 return ret;
393}
394
395static int post_kprobes_handler(struct pt_regs *regs)
396{
397 if (!kprobe_running())
398 return 0;
399
400 if (current_kprobe->post_handler)
401 current_kprobe->post_handler(current_kprobe, regs, 0);
402
403 resume_execution(current_kprobe, regs);
404
405 unlock_kprobes();
406 preempt_enable_no_resched();
407 return 1;
408}
409
410static int kprobes_fault_handler(struct pt_regs *regs, int trapnr)
411{
412 if (!kprobe_running())
413 return 0;
414
415 if (current_kprobe->fault_handler &&
416 current_kprobe->fault_handler(current_kprobe, regs, trapnr))
417 return 1;
418
419 if (kprobe_status & KPROBE_HIT_SS) {
420 resume_execution(current_kprobe, regs);
421 unlock_kprobes();
422 preempt_enable_no_resched();
423 }
424
425 return 0;
426}
427
428int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
429 void *data)
430{
431 struct die_args *args = (struct die_args *)data;
432 switch(val) {
433 case DIE_BREAK:
434 if (pre_kprobes_handler(args->regs))
435 return NOTIFY_STOP;
436 break;
437 case DIE_SS:
438 if (post_kprobes_handler(args->regs))
439 return NOTIFY_STOP;
440 break;
441 case DIE_PAGE_FAULT:
442 if (kprobes_fault_handler(args->regs, args->trapnr))
443 return NOTIFY_STOP;
444 default:
445 break;
446 }
447 return NOTIFY_DONE;
448}
449
450int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
451{
Anil S Keshavamurthyb2761dc2005-06-23 00:09:28 -0700452 struct jprobe *jp = container_of(p, struct jprobe, kp);
453 unsigned long addr = ((struct fnptr *)(jp->entry))->ip;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700454
Anil S Keshavamurthyb2761dc2005-06-23 00:09:28 -0700455 /* save architectural state */
456 jprobe_saved_regs = *regs;
457
458 /* after rfi, execute the jprobe instrumented function */
459 regs->cr_iip = addr & ~0xFULL;
460 ia64_psr(regs)->ri = addr & 0xf;
461 regs->r1 = ((struct fnptr *)(jp->entry))->gp;
462
463 /*
464 * fix the return address to our jprobe_inst_return() function
465 * in the jprobes.S file
466 */
467 regs->b0 = ((struct fnptr *)(jprobe_inst_return))->ip;
468
469 return 1;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700470}
471
472int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
473{
Anil S Keshavamurthyb2761dc2005-06-23 00:09:28 -0700474 *regs = jprobe_saved_regs;
475 return 1;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700476}