2 * Kernel Probes (KProbes)
3 * arch/ia64/kernel/kprobes.c
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.
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.
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.
19 * Copyright (C) IBM Corporation, 2002, 2004
20 * Copyright (C) Intel Corporation, 2005
22 * 2005-Apr Rusty Lynch <rusty.lynch@intel.com> and Anil S Keshavamurthy
23 * <anil.s.keshavamurthy@intel.com> adapted from i386
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>
35 #include <asm/pgtable.h>
36 #include <asm/kdebug.h>
38 extern void jprobe_inst_return(void);
40 /* kprobe_status settings */
41 #define KPROBE_HIT_ACTIVE 0x00000001
42 #define KPROBE_HIT_SS 0x00000002
44 static struct kprobe *current_kprobe;
45 static unsigned long kprobe_status;
46 static struct pt_regs jprobe_saved_regs;
48 enum instruction_type {A, I, M, F, B, L, X, u};
49 static enum instruction_type bundle_encoding[32][3] = {
85 * In this function we check to see if the instruction
86 * is IP relative instruction and update the kprobe
87 * inst flag accordingly
89 static void update_kprobe_inst_flag(uint template, uint slot, uint major_opcode,
90 unsigned long kprobe_inst, struct kprobe *p)
92 p->ainsn.inst_flag = 0;
93 p->ainsn.target_br_reg = 0;
95 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);
101 case IP_RELATIVE_PREDICT_OPCODE:
102 case IP_RELATIVE_BRANCH_OPCODE:
103 p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;
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);
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);
123 * 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
128 static uint is_cmp_ctype_unc_inst(uint template, uint slot, uint major_opcode,
129 unsigned long kprobe_inst)
134 if (!((bundle_encoding[template][slot] == I) ||
135 (bundle_encoding[template][slot] == M)))
138 if (!((major_opcode == 0xC) || (major_opcode == 0xD) ||
139 (major_opcode == 0xE)))
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))
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))
158 * In this function we override the bundle with
159 * the break instruction at the given slot.
161 static void prepare_break_inst(uint template, uint slot, uint major_opcode,
162 unsigned long kprobe_inst, struct kprobe *p)
164 unsigned long break_inst = BREAK_INST;
165 bundle_t *bundle = &p->ainsn.insn.bundle;
168 * Copy the original kprobe_inst qualifying predicate(qp)
169 * 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
174 if (!is_cmp_ctype_unc_inst(template, slot, major_opcode, kprobe_inst))
175 break_inst |= (0x3f & kprobe_inst);
179 bundle->quad0.slot0 = break_inst;
182 bundle->quad0.slot1_p0 = break_inst;
183 bundle->quad1.slot1_p1 = break_inst >> (64-46);
186 bundle->quad1.slot2 = break_inst;
191 * Update the instruction flag, so that we can
192 * emulate the instruction properly after we
193 * single step on original instruction
195 update_kprobe_inst_flag(template, slot, major_opcode, kprobe_inst, p);
198 static inline void get_kprobe_inst(bundle_t *bundle, uint slot,
199 unsigned long *kprobe_inst, uint *major_opcode)
201 unsigned long kprobe_inst_p0, kprobe_inst_p1;
202 unsigned int template;
204 template = bundle->quad0.template;
208 *major_opcode = (bundle->quad0.slot0 >> SLOT0_OPCODE_SHIFT);
209 *kprobe_inst = bundle->quad0.slot0;
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));
218 *major_opcode = (bundle->quad1.slot2 >> SLOT2_OPCODE_SHIFT);
219 *kprobe_inst = bundle->quad1.slot2;
224 static int valid_kprobe_addr(int template, int slot, unsigned long addr)
226 if ((slot > 2) || ((bundle_encoding[template][1] == L) && slot > 1)) {
227 printk(KERN_WARNING "Attempting to insert unaligned kprobe at 0x%lx\n",
234 int arch_prepare_kprobe(struct kprobe *p)
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;
242 memcpy(&p->opcode.bundle, kprobe_addr, sizeof(bundle_t));
243 memcpy(&p->ainsn.insn.bundle, kprobe_addr, sizeof(bundle_t));
245 template = bundle->quad0.template;
247 if(valid_kprobe_addr(template, slot, addr))
250 /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
251 if (slot == 1 && bundle_encoding[template][1] == L)
254 /* Get kprobe_inst and major_opcode from the bundle */
255 get_kprobe_inst(bundle, slot, &kprobe_inst, &major_opcode);
257 prepare_break_inst(template, slot, major_opcode, kprobe_inst, p);
262 void arch_arm_kprobe(struct kprobe *p)
264 unsigned long addr = (unsigned long)p->addr;
265 unsigned long arm_addr = addr & ~0xFULL;
267 memcpy((char *)arm_addr, &p->ainsn.insn.bundle, sizeof(bundle_t));
268 flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t));
271 void arch_disarm_kprobe(struct kprobe *p)
273 unsigned long addr = (unsigned long)p->addr;
274 unsigned long arm_addr = addr & ~0xFULL;
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));
281 void arch_remove_kprobe(struct kprobe *p)
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
289 * 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.
293 static void resume_execution(struct kprobe *p, struct pt_regs *regs)
295 unsigned long bundle_addr = ((unsigned long) (&p->opcode.bundle)) & ~0xFULL;
296 unsigned long resume_addr = (unsigned long)p->addr & ~0xFULL;
297 unsigned long template;
298 int slot = ((unsigned long)p->addr & 0xf);
300 template = p->opcode.bundle.quad0.template;
302 if (slot == 1 && bundle_encoding[template][1] == L)
305 if (p->ainsn.inst_flag) {
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;
312 if (p->ainsn.inst_flag & INST_FLAG_FIX_BRANCH_REG) {
314 * Fix target branch register, software convention is
315 * to use either b0 or b6 or b7, so just checking
316 * only those registers
318 switch (p->ainsn.target_br_reg) {
320 if ((regs->b0 == bundle_addr) ||
321 (regs->b0 == bundle_addr + 0x10)) {
322 regs->b0 = (regs->b0 - bundle_addr) +
327 if ((regs->b6 == bundle_addr) ||
328 (regs->b6 == bundle_addr + 0x10)) {
329 regs->b6 = (regs->b6 - bundle_addr) +
334 if ((regs->b7 == bundle_addr) ||
335 (regs->b7 == bundle_addr + 0x10)) {
336 regs->b7 = (regs->b7 - bundle_addr) +
346 if (regs->cr_iip == bundle_addr + 0x10) {
347 regs->cr_iip = resume_addr + 0x10;
350 if (regs->cr_iip == bundle_addr) {
351 regs->cr_iip = resume_addr;
356 /* Turn off Single Step bit */
357 ia64_psr(regs)->ss = 0;
360 static void prepare_ss(struct kprobe *p, struct pt_regs *regs)
362 unsigned long bundle_addr = (unsigned long) &p->opcode.bundle;
363 unsigned long slot = (unsigned long)p->addr & 0xf;
365 /* Update instruction pointer (IIP) and slot number (IPSR.ri) */
366 regs->cr_iip = bundle_addr & ~0xFULL;
371 ia64_psr(regs)->ri = slot;
373 /* turn on single stepping */
374 ia64_psr(regs)->ss = 1;
377 static int pre_kprobes_handler(struct pt_regs *regs)
381 kprobe_opcode_t *addr = (kprobe_opcode_t *)instruction_pointer(regs);
385 /* Handle recursion cases */
386 if (kprobe_running()) {
387 p = get_kprobe(addr);
389 if (kprobe_status == KPROBE_HIT_SS) {
393 arch_disarm_kprobe(p);
397 * jprobe instrumented function just completed
400 if (p->break_handler && p->break_handler(p, regs)) {
407 p = get_kprobe(addr);
413 kprobe_status = KPROBE_HIT_ACTIVE;
416 if (p->pre_handler && p->pre_handler(p, regs))
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.
426 kprobe_status = KPROBE_HIT_SS;
430 preempt_enable_no_resched();
434 static int post_kprobes_handler(struct pt_regs *regs)
436 if (!kprobe_running())
439 if (current_kprobe->post_handler)
440 current_kprobe->post_handler(current_kprobe, regs, 0);
442 resume_execution(current_kprobe, regs);
445 preempt_enable_no_resched();
449 static int kprobes_fault_handler(struct pt_regs *regs, int trapnr)
451 if (!kprobe_running())
454 if (current_kprobe->fault_handler &&
455 current_kprobe->fault_handler(current_kprobe, regs, trapnr))
458 if (kprobe_status & KPROBE_HIT_SS) {
459 resume_execution(current_kprobe, regs);
461 preempt_enable_no_resched();
467 int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
470 struct die_args *args = (struct die_args *)data;
473 if (pre_kprobes_handler(args->regs))
477 if (post_kprobes_handler(args->regs))
481 if (kprobes_fault_handler(args->regs, args->trapnr))
489 int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
491 struct jprobe *jp = container_of(p, struct jprobe, kp);
492 unsigned long addr = ((struct fnptr *)(jp->entry))->ip;
494 /* save architectural state */
495 jprobe_saved_regs = *regs;
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;
503 * fix the return address to our jprobe_inst_return() function
504 * in the jprobes.S file
506 regs->b0 = ((struct fnptr *)(jprobe_inst_return))->ip;
511 int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
513 *regs = jprobe_saved_regs;