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 = (unsigned long *)(addr & ~0xFULL);
88 unsigned long slot = addr & 0xf;
89 unsigned long template;
90 unsigned long major_opcode = 0;
91 unsigned long lx_type_inst = 0;
92 unsigned long kprobe_inst = 0;
93 bundle_t *bundle = &p->ainsn.insn.bundle;
95 memcpy(&p->opcode.bundle, bundle_addr, sizeof(bundle_t));
96 memcpy(&p->ainsn.insn.bundle, bundle_addr, sizeof(bundle_t));
98 p->ainsn.inst_flag = 0;
99 p->ainsn.target_br_reg = 0;
101 template = bundle->quad0.template;
103 if (((bundle_encoding[template][1] == L) && slot > 1) || (slot > 2)) {
104 printk(KERN_WARNING "Attempting to insert unaligned kprobe at 0x%lx\n",
109 if (slot == 1 && bundle_encoding[template][1] == L) {
116 major_opcode = (bundle->quad0.slot0 >> SLOT0_OPCODE_SHIFT);
117 kprobe_inst = bundle->quad0.slot0;
118 bundle->quad0.slot0 = BREAK_INST | (0x3f & kprobe_inst);
121 major_opcode = (bundle->quad1.slot1_p1 >> SLOT1_p1_OPCODE_SHIFT);
122 kprobe_inst = (bundle->quad0.slot1_p0 |
123 (bundle->quad1.slot1_p1 << (64-46)));
124 bundle->quad0.slot1_p0 = BREAK_INST | (0x3f & kprobe_inst);
125 bundle->quad1.slot1_p1 = (BREAK_INST >> (64-46));
128 major_opcode = (bundle->quad1.slot2 >> SLOT2_OPCODE_SHIFT);
129 kprobe_inst = bundle->quad1.slot2;
130 bundle->quad1.slot2 = BREAK_INST | (0x3f & kprobe_inst);
135 * Look for IP relative Branches, IP relative call or
136 * IP relative predicate instructions
138 if (bundle_encoding[template][slot] == B) {
139 switch (major_opcode) {
140 case INDIRECT_CALL_OPCODE:
141 p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
142 p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
144 case IP_RELATIVE_PREDICT_OPCODE:
145 case IP_RELATIVE_BRANCH_OPCODE:
146 p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;
148 case IP_RELATIVE_CALL_OPCODE:
149 p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;
150 p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
151 p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
157 } else if (lx_type_inst) {
158 switch (major_opcode) {
159 case LONG_CALL_OPCODE:
160 p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
161 p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
172 void arch_arm_kprobe(struct kprobe *p)
174 unsigned long addr = (unsigned long)p->addr;
175 unsigned long arm_addr = addr & ~0xFULL;
177 memcpy((char *)arm_addr, &p->ainsn.insn.bundle, sizeof(bundle_t));
178 flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t));
181 void arch_disarm_kprobe(struct kprobe *p)
183 unsigned long addr = (unsigned long)p->addr;
184 unsigned long arm_addr = addr & ~0xFULL;
186 /* p->opcode contains the original unaltered bundle */
187 memcpy((char *) arm_addr, (char *) &p->opcode.bundle, sizeof(bundle_t));
188 flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t));
191 void arch_remove_kprobe(struct kprobe *p)
196 * We are resuming execution after a single step fault, so the pt_regs
197 * structure reflects the register state after we executed the instruction
198 * located in the kprobe (p->ainsn.insn.bundle). We still need to adjust
199 * the ip to point back to the original stack address. To set the IP address
200 * to original stack address, handle the case where we need to fixup the
201 * relative IP address and/or fixup branch register.
203 static void resume_execution(struct kprobe *p, struct pt_regs *regs)
205 unsigned long bundle_addr = ((unsigned long) (&p->opcode.bundle)) & ~0xFULL;
206 unsigned long resume_addr = (unsigned long)p->addr & ~0xFULL;
207 unsigned long template;
208 int slot = ((unsigned long)p->addr & 0xf);
210 template = p->opcode.bundle.quad0.template;
212 if (slot == 1 && bundle_encoding[template][1] == L)
215 if (p->ainsn.inst_flag) {
217 if (p->ainsn.inst_flag & INST_FLAG_FIX_RELATIVE_IP_ADDR) {
218 /* Fix relative IP address */
219 regs->cr_iip = (regs->cr_iip - bundle_addr) + resume_addr;
222 if (p->ainsn.inst_flag & INST_FLAG_FIX_BRANCH_REG) {
224 * Fix target branch register, software convention is
225 * to use either b0 or b6 or b7, so just checking
226 * only those registers
228 switch (p->ainsn.target_br_reg) {
230 if ((regs->b0 == bundle_addr) ||
231 (regs->b0 == bundle_addr + 0x10)) {
232 regs->b0 = (regs->b0 - bundle_addr) +
237 if ((regs->b6 == bundle_addr) ||
238 (regs->b6 == bundle_addr + 0x10)) {
239 regs->b6 = (regs->b6 - bundle_addr) +
244 if ((regs->b7 == bundle_addr) ||
245 (regs->b7 == bundle_addr + 0x10)) {
246 regs->b7 = (regs->b7 - bundle_addr) +
256 if (regs->cr_iip == bundle_addr + 0x10) {
257 regs->cr_iip = resume_addr + 0x10;
260 if (regs->cr_iip == bundle_addr) {
261 regs->cr_iip = resume_addr;
266 /* Turn off Single Step bit */
267 ia64_psr(regs)->ss = 0;
270 static void prepare_ss(struct kprobe *p, struct pt_regs *regs)
272 unsigned long bundle_addr = (unsigned long) &p->opcode.bundle;
273 unsigned long slot = (unsigned long)p->addr & 0xf;
275 /* Update instruction pointer (IIP) and slot number (IPSR.ri) */
276 regs->cr_iip = bundle_addr & ~0xFULL;
281 ia64_psr(regs)->ri = slot;
283 /* turn on single stepping */
284 ia64_psr(regs)->ss = 1;
287 static int pre_kprobes_handler(struct pt_regs *regs)
291 kprobe_opcode_t *addr = (kprobe_opcode_t *)instruction_pointer(regs);
295 /* Handle recursion cases */
296 if (kprobe_running()) {
297 p = get_kprobe(addr);
299 if (kprobe_status == KPROBE_HIT_SS) {
303 arch_disarm_kprobe(p);
307 * jprobe instrumented function just completed
310 if (p->break_handler && p->break_handler(p, regs)) {
317 p = get_kprobe(addr);
323 kprobe_status = KPROBE_HIT_ACTIVE;
326 if (p->pre_handler && p->pre_handler(p, regs))
328 * Our pre-handler is specifically requesting that we just
329 * do a return. This is handling the case where the
330 * pre-handler is really our special jprobe pre-handler.
336 kprobe_status = KPROBE_HIT_SS;
340 preempt_enable_no_resched();
344 static int post_kprobes_handler(struct pt_regs *regs)
346 if (!kprobe_running())
349 if (current_kprobe->post_handler)
350 current_kprobe->post_handler(current_kprobe, regs, 0);
352 resume_execution(current_kprobe, regs);
355 preempt_enable_no_resched();
359 static int kprobes_fault_handler(struct pt_regs *regs, int trapnr)
361 if (!kprobe_running())
364 if (current_kprobe->fault_handler &&
365 current_kprobe->fault_handler(current_kprobe, regs, trapnr))
368 if (kprobe_status & KPROBE_HIT_SS) {
369 resume_execution(current_kprobe, regs);
371 preempt_enable_no_resched();
377 int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
380 struct die_args *args = (struct die_args *)data;
383 if (pre_kprobes_handler(args->regs))
387 if (post_kprobes_handler(args->regs))
391 if (kprobes_fault_handler(args->regs, args->trapnr))
399 int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
401 struct jprobe *jp = container_of(p, struct jprobe, kp);
402 unsigned long addr = ((struct fnptr *)(jp->entry))->ip;
404 /* save architectural state */
405 jprobe_saved_regs = *regs;
407 /* after rfi, execute the jprobe instrumented function */
408 regs->cr_iip = addr & ~0xFULL;
409 ia64_psr(regs)->ri = addr & 0xf;
410 regs->r1 = ((struct fnptr *)(jp->entry))->gp;
413 * fix the return address to our jprobe_inst_return() function
414 * in the jprobes.S file
416 regs->b0 = ((struct fnptr *)(jprobe_inst_return))->ip;
421 int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
423 *regs = jprobe_saved_regs;