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] = {
84 int arch_prepare_kprobe(struct kprobe *p)
86 unsigned long addr = (unsigned long) p->addr;
87 unsigned long bundle_addr = addr & ~0xFULL;
88 unsigned long slot = addr & 0xf;
90 unsigned long template;
93 * TODO: Verify that a probe is not being inserted
94 * in sensitive regions of code
95 * TODO: Verify that the memory holding the probe is rwx
96 * TODO: verify this is a kernel address
98 memcpy(&bundle, (unsigned long *)bundle_addr, sizeof(bundle_t));
99 template = bundle.quad0.template;
100 if (((bundle_encoding[template][1] == L) && slot > 1) || (slot > 2)) {
101 printk(KERN_WARNING "Attempting to insert unaligned kprobe at 0x%lx\n", addr);
107 void arch_copy_kprobe(struct kprobe *p)
109 unsigned long addr = (unsigned long)p->addr;
110 unsigned long bundle_addr = addr & ~0xFULL;
112 memcpy(&p->ainsn.insn.bundle, (unsigned long *)bundle_addr,
114 memcpy(&p->opcode.bundle, &p->ainsn.insn.bundle, sizeof(bundle_t));
117 void arch_arm_kprobe(struct kprobe *p)
119 unsigned long addr = (unsigned long)p->addr;
120 unsigned long arm_addr = addr & ~0xFULL;
121 unsigned long slot = addr & 0xf;
122 unsigned long template;
125 memcpy(&bundle, &p->ainsn.insn.bundle, sizeof(bundle_t));
127 template = bundle.quad0.template;
128 if (slot == 1 && bundle_encoding[template][1] == L)
132 bundle.quad0.slot0 = BREAK_INST;
135 bundle.quad0.slot1_p0 = BREAK_INST;
136 bundle.quad1.slot1_p1 = (BREAK_INST >> (64-46));
139 bundle.quad1.slot2 = BREAK_INST;
143 /* Flush icache for the instruction at the emulated address */
144 flush_icache_range((unsigned long)&p->ainsn.insn.bundle,
145 (unsigned long)&p->ainsn.insn.bundle +
148 * Patch the original instruction with the probe instruction
149 * and flush the instruction cache
151 memcpy((char *) arm_addr, (char *) &bundle, sizeof(bundle_t));
152 flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t));
155 void arch_disarm_kprobe(struct kprobe *p)
157 unsigned long addr = (unsigned long)p->addr;
158 unsigned long arm_addr = addr & ~0xFULL;
160 /* p->opcode contains the original unaltered bundle */
161 memcpy((char *) arm_addr, (char *) &p->opcode.bundle, sizeof(bundle_t));
162 flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t));
165 void arch_remove_kprobe(struct kprobe *p)
170 * We are resuming execution after a single step fault, so the pt_regs
171 * structure reflects the register state after we executed the instruction
172 * located in the kprobe (p->ainsn.insn.bundle). We still need to adjust
173 * the ip to point back to the original stack address, and if we see that
174 * the slot has incremented back to zero, then we need to point to the next
177 static void resume_execution(struct kprobe *p, struct pt_regs *regs)
179 unsigned long bundle = (unsigned long)p->addr & ~0xFULL;
182 * TODO: Handle cases where kprobe was inserted on a branch instruction
185 if (!ia64_psr(regs)->ri)
186 regs->cr_iip = bundle + 0x10;
188 regs->cr_iip = bundle;
190 ia64_psr(regs)->ss = 0;
193 static void prepare_ss(struct kprobe *p, struct pt_regs *regs)
195 unsigned long bundle_addr = (unsigned long) &p->ainsn.insn.bundle;
196 unsigned long slot = (unsigned long)p->addr & 0xf;
198 /* Update instruction pointer (IIP) and slot number (IPSR.ri) */
199 regs->cr_iip = bundle_addr & ~0xFULL;
204 ia64_psr(regs)->ri = slot;
206 /* turn on single stepping */
207 ia64_psr(regs)->ss = 1;
210 static int pre_kprobes_handler(struct pt_regs *regs)
214 kprobe_opcode_t *addr = (kprobe_opcode_t *)instruction_pointer(regs);
218 /* Handle recursion cases */
219 if (kprobe_running()) {
220 p = get_kprobe(addr);
222 if (kprobe_status == KPROBE_HIT_SS) {
226 arch_disarm_kprobe(p);
230 * jprobe instrumented function just completed
233 if (p->break_handler && p->break_handler(p, regs)) {
240 p = get_kprobe(addr);
246 kprobe_status = KPROBE_HIT_ACTIVE;
249 if (p->pre_handler && p->pre_handler(p, regs))
251 * Our pre-handler is specifically requesting that we just
252 * do a return. This is handling the case where the
253 * pre-handler is really our special jprobe pre-handler.
259 kprobe_status = KPROBE_HIT_SS;
263 preempt_enable_no_resched();
267 static int post_kprobes_handler(struct pt_regs *regs)
269 if (!kprobe_running())
272 if (current_kprobe->post_handler)
273 current_kprobe->post_handler(current_kprobe, regs, 0);
275 resume_execution(current_kprobe, regs);
278 preempt_enable_no_resched();
282 static int kprobes_fault_handler(struct pt_regs *regs, int trapnr)
284 if (!kprobe_running())
287 if (current_kprobe->fault_handler &&
288 current_kprobe->fault_handler(current_kprobe, regs, trapnr))
291 if (kprobe_status & KPROBE_HIT_SS) {
292 resume_execution(current_kprobe, regs);
294 preempt_enable_no_resched();
300 int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
303 struct die_args *args = (struct die_args *)data;
306 if (pre_kprobes_handler(args->regs))
310 if (post_kprobes_handler(args->regs))
314 if (kprobes_fault_handler(args->regs, args->trapnr))
322 int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
324 struct jprobe *jp = container_of(p, struct jprobe, kp);
325 unsigned long addr = ((struct fnptr *)(jp->entry))->ip;
327 /* save architectural state */
328 jprobe_saved_regs = *regs;
330 /* after rfi, execute the jprobe instrumented function */
331 regs->cr_iip = addr & ~0xFULL;
332 ia64_psr(regs)->ri = addr & 0xf;
333 regs->r1 = ((struct fnptr *)(jp->entry))->gp;
336 * fix the return address to our jprobe_inst_return() function
337 * in the jprobes.S file
339 regs->b0 = ((struct fnptr *)(jprobe_inst_return))->ip;
344 int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
346 *regs = jprobe_saved_regs;