blob: 51bc08bd046657ecb01530fb659f3a95596fe5f1 [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/*
Anil S Keshavamurthy1674eaf2005-06-23 00:09:33 -0700123 * In this function we check to see if the instruction
124 * (qp) cmpx.crel.ctype p1,p2=r2,r3
125 * on which we are inserting kprobe is cmp instruction
126 * with ctype as unc.
127 */
128static uint is_cmp_ctype_unc_inst(uint template, uint slot, uint major_opcode,
129unsigned long kprobe_inst)
130{
131 cmp_inst_t cmp_inst;
132 uint ctype_unc = 0;
133
134 if (!((bundle_encoding[template][slot] == I) ||
135 (bundle_encoding[template][slot] == M)))
136 goto out;
137
138 if (!((major_opcode == 0xC) || (major_opcode == 0xD) ||
139 (major_opcode == 0xE)))
140 goto out;
141
142 cmp_inst.l = kprobe_inst;
143 if ((cmp_inst.f.x2 == 0) || (cmp_inst.f.x2 == 1)) {
144 /* Integere compare - Register Register (A6 type)*/
145 if ((cmp_inst.f.tb == 0) && (cmp_inst.f.ta == 0)
146 &&(cmp_inst.f.c == 1))
147 ctype_unc = 1;
148 } else if ((cmp_inst.f.x2 == 2)||(cmp_inst.f.x2 == 3)) {
149 /* Integere compare - Immediate Register (A8 type)*/
150 if ((cmp_inst.f.ta == 0) &&(cmp_inst.f.c == 1))
151 ctype_unc = 1;
152 }
153out:
154 return ctype_unc;
155}
156
157/*
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700158 * In this function we override the bundle with
159 * the break instruction at the given slot.
160 */
161static void prepare_break_inst(uint template, uint slot, uint major_opcode,
162 unsigned long kprobe_inst, struct kprobe *p)
163{
164 unsigned long break_inst = BREAK_INST;
165 bundle_t *bundle = &p->ainsn.insn.bundle;
166
167 /*
168 * Copy the original kprobe_inst qualifying predicate(qp)
Anil S Keshavamurthy1674eaf2005-06-23 00:09:33 -0700169 * to the break instruction iff !is_cmp_ctype_unc_inst
170 * because for cmp instruction with ctype equal to unc,
171 * which is a special instruction always needs to be
172 * executed regradless of qp
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700173 */
Anil S Keshavamurthy1674eaf2005-06-23 00:09:33 -0700174 if (!is_cmp_ctype_unc_inst(template, slot, major_opcode, kprobe_inst))
175 break_inst |= (0x3f & kprobe_inst);
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700176
177 switch (slot) {
178 case 0:
179 bundle->quad0.slot0 = break_inst;
180 break;
181 case 1:
182 bundle->quad0.slot1_p0 = break_inst;
183 bundle->quad1.slot1_p1 = break_inst >> (64-46);
184 break;
185 case 2:
186 bundle->quad1.slot2 = break_inst;
187 break;
188 }
189
190 /*
191 * Update the instruction flag, so that we can
192 * emulate the instruction properly after we
193 * single step on original instruction
194 */
195 update_kprobe_inst_flag(template, slot, major_opcode, kprobe_inst, p);
196}
197
198static inline void get_kprobe_inst(bundle_t *bundle, uint slot,
199 unsigned long *kprobe_inst, uint *major_opcode)
200{
201 unsigned long kprobe_inst_p0, kprobe_inst_p1;
202 unsigned int template;
203
204 template = bundle->quad0.template;
205
206 switch (slot) {
207 case 0:
208 *major_opcode = (bundle->quad0.slot0 >> SLOT0_OPCODE_SHIFT);
209 *kprobe_inst = bundle->quad0.slot0;
210 break;
211 case 1:
212 *major_opcode = (bundle->quad1.slot1_p1 >> SLOT1_p1_OPCODE_SHIFT);
213 kprobe_inst_p0 = bundle->quad0.slot1_p0;
214 kprobe_inst_p1 = bundle->quad1.slot1_p1;
215 *kprobe_inst = kprobe_inst_p0 | (kprobe_inst_p1 << (64-46));
216 break;
217 case 2:
218 *major_opcode = (bundle->quad1.slot2 >> SLOT2_OPCODE_SHIFT);
219 *kprobe_inst = bundle->quad1.slot2;
220 break;
221 }
222}
223
224static int valid_kprobe_addr(int template, int slot, unsigned long addr)
225{
226 if ((slot > 2) || ((bundle_encoding[template][1] == L) && slot > 1)) {
Rusty Lynch8bc76772005-06-23 00:09:30 -0700227 printk(KERN_WARNING "Attempting to insert unaligned kprobe at 0x%lx\n",
228 addr);
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700229 return -EINVAL;
230 }
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700231 return 0;
232}
Rusty Lynch8bc76772005-06-23 00:09:30 -0700233
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700234int arch_prepare_kprobe(struct kprobe *p)
235{
236 unsigned long addr = (unsigned long) p->addr;
237 unsigned long *kprobe_addr = (unsigned long *)(addr & ~0xFULL);
238 unsigned long kprobe_inst=0;
239 unsigned int slot = addr & 0xf, template, major_opcode = 0;
240 bundle_t *bundle = &p->ainsn.insn.bundle;
Rusty Lynch8bc76772005-06-23 00:09:30 -0700241
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700242 memcpy(&p->opcode.bundle, kprobe_addr, sizeof(bundle_t));
243 memcpy(&p->ainsn.insn.bundle, kprobe_addr, sizeof(bundle_t));
Rusty Lynch8bc76772005-06-23 00:09:30 -0700244
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700245 template = bundle->quad0.template;
246
247 if(valid_kprobe_addr(template, slot, addr))
248 return -EINVAL;
249
250 /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
251 if (slot == 1 && bundle_encoding[template][1] == L)
252 slot++;
253
254 /* Get kprobe_inst and major_opcode from the bundle */
255 get_kprobe_inst(bundle, slot, &kprobe_inst, &major_opcode);
256
257 prepare_break_inst(template, slot, major_opcode, kprobe_inst, p);
Rusty Lynch8bc76772005-06-23 00:09:30 -0700258
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700259 return 0;
260}
261
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700262void arch_arm_kprobe(struct kprobe *p)
263{
264 unsigned long addr = (unsigned long)p->addr;
265 unsigned long arm_addr = addr & ~0xFULL;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700266
Rusty Lynch8bc76772005-06-23 00:09:30 -0700267 memcpy((char *)arm_addr, &p->ainsn.insn.bundle, sizeof(bundle_t));
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700268 flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t));
269}
270
271void arch_disarm_kprobe(struct kprobe *p)
272{
273 unsigned long addr = (unsigned long)p->addr;
274 unsigned long arm_addr = addr & ~0xFULL;
275
276 /* p->opcode contains the original unaltered bundle */
277 memcpy((char *) arm_addr, (char *) &p->opcode.bundle, sizeof(bundle_t));
278 flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t));
279}
280
281void arch_remove_kprobe(struct kprobe *p)
282{
283}
284
285/*
286 * We are resuming execution after a single step fault, so the pt_regs
287 * structure reflects the register state after we executed the instruction
288 * located in the kprobe (p->ainsn.insn.bundle). We still need to adjust
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700289 * the ip to point back to the original stack address. To set the IP address
290 * to original stack address, handle the case where we need to fixup the
291 * relative IP address and/or fixup branch register.
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700292 */
293static void resume_execution(struct kprobe *p, struct pt_regs *regs)
294{
Rusty Lynch8bc76772005-06-23 00:09:30 -0700295 unsigned long bundle_addr = ((unsigned long) (&p->opcode.bundle)) & ~0xFULL;
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700296 unsigned long resume_addr = (unsigned long)p->addr & ~0xFULL;
297 unsigned long template;
298 int slot = ((unsigned long)p->addr & 0xf);
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700299
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700300 template = p->opcode.bundle.quad0.template;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700301
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700302 if (slot == 1 && bundle_encoding[template][1] == L)
303 slot = 2;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700304
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700305 if (p->ainsn.inst_flag) {
306
307 if (p->ainsn.inst_flag & INST_FLAG_FIX_RELATIVE_IP_ADDR) {
308 /* Fix relative IP address */
309 regs->cr_iip = (regs->cr_iip - bundle_addr) + resume_addr;
310 }
311
312 if (p->ainsn.inst_flag & INST_FLAG_FIX_BRANCH_REG) {
313 /*
314 * Fix target branch register, software convention is
315 * to use either b0 or b6 or b7, so just checking
316 * only those registers
317 */
318 switch (p->ainsn.target_br_reg) {
319 case 0:
320 if ((regs->b0 == bundle_addr) ||
321 (regs->b0 == bundle_addr + 0x10)) {
322 regs->b0 = (regs->b0 - bundle_addr) +
323 resume_addr;
324 }
325 break;
326 case 6:
327 if ((regs->b6 == bundle_addr) ||
328 (regs->b6 == bundle_addr + 0x10)) {
329 regs->b6 = (regs->b6 - bundle_addr) +
330 resume_addr;
331 }
332 break;
333 case 7:
334 if ((regs->b7 == bundle_addr) ||
335 (regs->b7 == bundle_addr + 0x10)) {
336 regs->b7 = (regs->b7 - bundle_addr) +
337 resume_addr;
338 }
339 break;
340 } /* end switch */
341 }
342 goto turn_ss_off;
343 }
344
345 if (slot == 2) {
346 if (regs->cr_iip == bundle_addr + 0x10) {
347 regs->cr_iip = resume_addr + 0x10;
348 }
349 } else {
350 if (regs->cr_iip == bundle_addr) {
351 regs->cr_iip = resume_addr;
352 }
Anil S Keshavamurthya5403182005-06-23 00:09:32 -0700353 }
Anil S Keshavamurthycd2675b2005-06-23 00:09:29 -0700354
355turn_ss_off:
356 /* Turn off Single Step bit */
357 ia64_psr(regs)->ss = 0;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700358}
359
360static void prepare_ss(struct kprobe *p, struct pt_regs *regs)
361{
Rusty Lynch8bc76772005-06-23 00:09:30 -0700362 unsigned long bundle_addr = (unsigned long) &p->opcode.bundle;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700363 unsigned long slot = (unsigned long)p->addr & 0xf;
364
365 /* Update instruction pointer (IIP) and slot number (IPSR.ri) */
366 regs->cr_iip = bundle_addr & ~0xFULL;
367
368 if (slot > 2)
369 slot = 0;
370
371 ia64_psr(regs)->ri = slot;
372
373 /* turn on single stepping */
374 ia64_psr(regs)->ss = 1;
375}
376
377static int pre_kprobes_handler(struct pt_regs *regs)
378{
379 struct kprobe *p;
380 int ret = 0;
381 kprobe_opcode_t *addr = (kprobe_opcode_t *)instruction_pointer(regs);
382
383 preempt_disable();
384
385 /* Handle recursion cases */
386 if (kprobe_running()) {
387 p = get_kprobe(addr);
388 if (p) {
389 if (kprobe_status == KPROBE_HIT_SS) {
390 unlock_kprobes();
391 goto no_kprobe;
392 }
393 arch_disarm_kprobe(p);
394 ret = 1;
395 } else {
396 /*
397 * jprobe instrumented function just completed
398 */
399 p = current_kprobe;
400 if (p->break_handler && p->break_handler(p, regs)) {
401 goto ss_probe;
402 }
403 }
404 }
405
406 lock_kprobes();
407 p = get_kprobe(addr);
408 if (!p) {
409 unlock_kprobes();
410 goto no_kprobe;
411 }
412
413 kprobe_status = KPROBE_HIT_ACTIVE;
414 current_kprobe = p;
415
416 if (p->pre_handler && p->pre_handler(p, regs))
417 /*
418 * Our pre-handler is specifically requesting that we just
419 * do a return. This is handling the case where the
420 * pre-handler is really our special jprobe pre-handler.
421 */
422 return 1;
423
424ss_probe:
425 prepare_ss(p, regs);
426 kprobe_status = KPROBE_HIT_SS;
427 return 1;
428
429no_kprobe:
430 preempt_enable_no_resched();
431 return ret;
432}
433
434static int post_kprobes_handler(struct pt_regs *regs)
435{
436 if (!kprobe_running())
437 return 0;
438
439 if (current_kprobe->post_handler)
440 current_kprobe->post_handler(current_kprobe, regs, 0);
441
442 resume_execution(current_kprobe, regs);
443
444 unlock_kprobes();
445 preempt_enable_no_resched();
446 return 1;
447}
448
449static int kprobes_fault_handler(struct pt_regs *regs, int trapnr)
450{
451 if (!kprobe_running())
452 return 0;
453
454 if (current_kprobe->fault_handler &&
455 current_kprobe->fault_handler(current_kprobe, regs, trapnr))
456 return 1;
457
458 if (kprobe_status & KPROBE_HIT_SS) {
459 resume_execution(current_kprobe, regs);
460 unlock_kprobes();
461 preempt_enable_no_resched();
462 }
463
464 return 0;
465}
466
467int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
468 void *data)
469{
470 struct die_args *args = (struct die_args *)data;
471 switch(val) {
472 case DIE_BREAK:
473 if (pre_kprobes_handler(args->regs))
474 return NOTIFY_STOP;
475 break;
476 case DIE_SS:
477 if (post_kprobes_handler(args->regs))
478 return NOTIFY_STOP;
479 break;
480 case DIE_PAGE_FAULT:
481 if (kprobes_fault_handler(args->regs, args->trapnr))
482 return NOTIFY_STOP;
483 default:
484 break;
485 }
486 return NOTIFY_DONE;
487}
488
489int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
490{
Anil S Keshavamurthyb2761dc2005-06-23 00:09:28 -0700491 struct jprobe *jp = container_of(p, struct jprobe, kp);
492 unsigned long addr = ((struct fnptr *)(jp->entry))->ip;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700493
Anil S Keshavamurthyb2761dc2005-06-23 00:09:28 -0700494 /* save architectural state */
495 jprobe_saved_regs = *regs;
496
497 /* after rfi, execute the jprobe instrumented function */
498 regs->cr_iip = addr & ~0xFULL;
499 ia64_psr(regs)->ri = addr & 0xf;
500 regs->r1 = ((struct fnptr *)(jp->entry))->gp;
501
502 /*
503 * fix the return address to our jprobe_inst_return() function
504 * in the jprobes.S file
505 */
506 regs->b0 = ((struct fnptr *)(jprobe_inst_return))->ip;
507
508 return 1;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700509}
510
511int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
512{
Anil S Keshavamurthyb2761dc2005-06-23 00:09:28 -0700513 *regs = jprobe_saved_regs;
514 return 1;
Anil S Keshavamurthyfd7b2312005-06-23 00:09:28 -0700515}