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 /* kprobe_status settings */
39 #define KPROBE_HIT_ACTIVE 0x00000001
40 #define KPROBE_HIT_SS 0x00000002
42 static struct kprobe
*current_kprobe
;
43 static unsigned long kprobe_status
;
45 enum instruction_type
{A
, I
, M
, F
, B
, L
, X
, u
};
46 static enum instruction_type bundle_encoding
[32][3] = {
81 int arch_prepare_kprobe(struct kprobe
*p
)
83 unsigned long addr
= (unsigned long) p
->addr
;
84 unsigned long bundle_addr
= addr
& ~0xFULL
;
85 unsigned long slot
= addr
& 0xf;
87 unsigned long template;
90 * TODO: Verify that a probe is not being inserted
91 * in sensitive regions of code
92 * TODO: Verify that the memory holding the probe is rwx
93 * TODO: verify this is a kernel address
95 memcpy(&bundle
, (unsigned long *)bundle_addr
, sizeof(bundle_t
));
96 template = bundle
.quad0
.template;
97 if (((bundle_encoding
[template][1] == L
) && slot
> 1) || (slot
> 2)) {
98 printk(KERN_WARNING
"Attempting to insert unaligned kprobe at 0x%lx\n", addr
);
104 void arch_copy_kprobe(struct kprobe
*p
)
106 unsigned long addr
= (unsigned long)p
->addr
;
107 unsigned long bundle_addr
= addr
& ~0xFULL
;
109 memcpy(&p
->ainsn
.insn
.bundle
, (unsigned long *)bundle_addr
,
111 memcpy(&p
->opcode
.bundle
, &p
->ainsn
.insn
.bundle
, sizeof(bundle_t
));
114 void arch_arm_kprobe(struct kprobe
*p
)
116 unsigned long addr
= (unsigned long)p
->addr
;
117 unsigned long arm_addr
= addr
& ~0xFULL
;
118 unsigned long slot
= addr
& 0xf;
119 unsigned long template;
122 memcpy(&bundle
, &p
->ainsn
.insn
.bundle
, sizeof(bundle_t
));
124 template = bundle
.quad0
.template;
125 if (slot
== 1 && bundle_encoding
[template][1] == L
)
129 bundle
.quad0
.slot0
= BREAK_INST
;
132 bundle
.quad0
.slot1_p0
= BREAK_INST
;
133 bundle
.quad1
.slot1_p1
= (BREAK_INST
>> (64-46));
136 bundle
.quad1
.slot2
= BREAK_INST
;
140 /* Flush icache for the instruction at the emulated address */
141 flush_icache_range((unsigned long)&p
->ainsn
.insn
.bundle
,
142 (unsigned long)&p
->ainsn
.insn
.bundle
+
145 * Patch the original instruction with the probe instruction
146 * and flush the instruction cache
148 memcpy((char *) arm_addr
, (char *) &bundle
, sizeof(bundle_t
));
149 flush_icache_range(arm_addr
, arm_addr
+ sizeof(bundle_t
));
152 void arch_disarm_kprobe(struct kprobe
*p
)
154 unsigned long addr
= (unsigned long)p
->addr
;
155 unsigned long arm_addr
= addr
& ~0xFULL
;
157 /* p->opcode contains the original unaltered bundle */
158 memcpy((char *) arm_addr
, (char *) &p
->opcode
.bundle
, sizeof(bundle_t
));
159 flush_icache_range(arm_addr
, arm_addr
+ sizeof(bundle_t
));
162 void arch_remove_kprobe(struct kprobe
*p
)
167 * We are resuming execution after a single step fault, so the pt_regs
168 * structure reflects the register state after we executed the instruction
169 * located in the kprobe (p->ainsn.insn.bundle). We still need to adjust
170 * the ip to point back to the original stack address, and if we see that
171 * the slot has incremented back to zero, then we need to point to the next
174 static void resume_execution(struct kprobe
*p
, struct pt_regs
*regs
)
176 unsigned long bundle
= (unsigned long)p
->addr
& ~0xFULL
;
179 * TODO: Handle cases where kprobe was inserted on a branch instruction
182 if (!ia64_psr(regs
)->ri
)
183 regs
->cr_iip
= bundle
+ 0x10;
185 regs
->cr_iip
= bundle
;
187 ia64_psr(regs
)->ss
= 0;
190 static void prepare_ss(struct kprobe
*p
, struct pt_regs
*regs
)
192 unsigned long bundle_addr
= (unsigned long) &p
->ainsn
.insn
.bundle
;
193 unsigned long slot
= (unsigned long)p
->addr
& 0xf;
195 /* Update instruction pointer (IIP) and slot number (IPSR.ri) */
196 regs
->cr_iip
= bundle_addr
& ~0xFULL
;
201 ia64_psr(regs
)->ri
= slot
;
203 /* turn on single stepping */
204 ia64_psr(regs
)->ss
= 1;
207 static int pre_kprobes_handler(struct pt_regs
*regs
)
211 kprobe_opcode_t
*addr
= (kprobe_opcode_t
*)instruction_pointer(regs
);
215 /* Handle recursion cases */
216 if (kprobe_running()) {
217 p
= get_kprobe(addr
);
219 if (kprobe_status
== KPROBE_HIT_SS
) {
223 arch_disarm_kprobe(p
);
227 * jprobe instrumented function just completed
230 if (p
->break_handler
&& p
->break_handler(p
, regs
)) {
237 p
= get_kprobe(addr
);
243 kprobe_status
= KPROBE_HIT_ACTIVE
;
246 if (p
->pre_handler
&& p
->pre_handler(p
, regs
))
248 * Our pre-handler is specifically requesting that we just
249 * do a return. This is handling the case where the
250 * pre-handler is really our special jprobe pre-handler.
256 kprobe_status
= KPROBE_HIT_SS
;
260 preempt_enable_no_resched();
264 static int post_kprobes_handler(struct pt_regs
*regs
)
266 if (!kprobe_running())
269 if (current_kprobe
->post_handler
)
270 current_kprobe
->post_handler(current_kprobe
, regs
, 0);
272 resume_execution(current_kprobe
, regs
);
275 preempt_enable_no_resched();
279 static int kprobes_fault_handler(struct pt_regs
*regs
, int trapnr
)
281 if (!kprobe_running())
284 if (current_kprobe
->fault_handler
&&
285 current_kprobe
->fault_handler(current_kprobe
, regs
, trapnr
))
288 if (kprobe_status
& KPROBE_HIT_SS
) {
289 resume_execution(current_kprobe
, regs
);
291 preempt_enable_no_resched();
297 int kprobe_exceptions_notify(struct notifier_block
*self
, unsigned long val
,
300 struct die_args
*args
= (struct die_args
*)data
;
303 if (pre_kprobes_handler(args
->regs
))
307 if (post_kprobes_handler(args
->regs
))
311 if (kprobes_fault_handler(args
->regs
, args
->trapnr
))
319 int setjmp_pre_handler(struct kprobe
*p
, struct pt_regs
*regs
)
321 printk(KERN_WARNING
"Jprobes is not supported\n");
325 void jprobe_return(void)
329 int longjmp_break_handler(struct kprobe
*p
, struct pt_regs
*regs
)