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 override the bundle with
124 * the break instruction at the given slot.
126 static void prepare_break_inst(uint template, uint slot, uint major_opcode,
127 unsigned long kprobe_inst, struct kprobe *p)
129 unsigned long break_inst = BREAK_INST;
130 bundle_t *bundle = &p->ainsn.insn.bundle;
133 * Copy the original kprobe_inst qualifying predicate(qp)
134 * to the break instruction
136 break_inst |= (0x3f & kprobe_inst);
140 bundle->quad0.slot0 = break_inst;
143 bundle->quad0.slot1_p0 = break_inst;
144 bundle->quad1.slot1_p1 = break_inst >> (64-46);
147 bundle->quad1.slot2 = break_inst;
152 * Update the instruction flag, so that we can
153 * emulate the instruction properly after we
154 * single step on original instruction
156 update_kprobe_inst_flag(template, slot, major_opcode, kprobe_inst, p);
159 static inline void get_kprobe_inst(bundle_t *bundle, uint slot,
160 unsigned long *kprobe_inst, uint *major_opcode)
162 unsigned long kprobe_inst_p0, kprobe_inst_p1;
163 unsigned int template;
165 template = bundle->quad0.template;
169 *major_opcode = (bundle->quad0.slot0 >> SLOT0_OPCODE_SHIFT);
170 *kprobe_inst = bundle->quad0.slot0;
173 *major_opcode = (bundle->quad1.slot1_p1 >> SLOT1_p1_OPCODE_SHIFT);
174 kprobe_inst_p0 = bundle->quad0.slot1_p0;
175 kprobe_inst_p1 = bundle->quad1.slot1_p1;
176 *kprobe_inst = kprobe_inst_p0 | (kprobe_inst_p1 << (64-46));
179 *major_opcode = (bundle->quad1.slot2 >> SLOT2_OPCODE_SHIFT);
180 *kprobe_inst = bundle->quad1.slot2;
185 static int valid_kprobe_addr(int template, int slot, unsigned long addr)
187 if ((slot > 2) || ((bundle_encoding[template][1] == L) && slot > 1)) {
188 printk(KERN_WARNING "Attempting to insert unaligned kprobe at 0x%lx\n",
195 int arch_prepare_kprobe(struct kprobe *p)
197 unsigned long addr = (unsigned long) p->addr;
198 unsigned long *kprobe_addr = (unsigned long *)(addr & ~0xFULL);
199 unsigned long kprobe_inst=0;
200 unsigned int slot = addr & 0xf, template, major_opcode = 0;
201 bundle_t *bundle = &p->ainsn.insn.bundle;
203 memcpy(&p->opcode.bundle, kprobe_addr, sizeof(bundle_t));
204 memcpy(&p->ainsn.insn.bundle, kprobe_addr, sizeof(bundle_t));
206 template = bundle->quad0.template;
208 if(valid_kprobe_addr(template, slot, addr))
211 /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
212 if (slot == 1 && bundle_encoding[template][1] == L)
215 /* Get kprobe_inst and major_opcode from the bundle */
216 get_kprobe_inst(bundle, slot, &kprobe_inst, &major_opcode);
218 prepare_break_inst(template, slot, major_opcode, kprobe_inst, p);
223 void arch_arm_kprobe(struct kprobe *p)
225 unsigned long addr = (unsigned long)p->addr;
226 unsigned long arm_addr = addr & ~0xFULL;
228 memcpy((char *)arm_addr, &p->ainsn.insn.bundle, sizeof(bundle_t));
229 flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t));
232 void arch_disarm_kprobe(struct kprobe *p)
234 unsigned long addr = (unsigned long)p->addr;
235 unsigned long arm_addr = addr & ~0xFULL;
237 /* p->opcode contains the original unaltered bundle */
238 memcpy((char *) arm_addr, (char *) &p->opcode.bundle, sizeof(bundle_t));
239 flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t));
242 void arch_remove_kprobe(struct kprobe *p)
247 * We are resuming execution after a single step fault, so the pt_regs
248 * structure reflects the register state after we executed the instruction
249 * located in the kprobe (p->ainsn.insn.bundle). We still need to adjust
250 * the ip to point back to the original stack address. To set the IP address
251 * to original stack address, handle the case where we need to fixup the
252 * relative IP address and/or fixup branch register.
254 static void resume_execution(struct kprobe *p, struct pt_regs *regs)
256 unsigned long bundle_addr = ((unsigned long) (&p->opcode.bundle)) & ~0xFULL;
257 unsigned long resume_addr = (unsigned long)p->addr & ~0xFULL;
258 unsigned long template;
259 int slot = ((unsigned long)p->addr & 0xf);
261 template = p->opcode.bundle.quad0.template;
263 if (slot == 1 && bundle_encoding[template][1] == L)
266 if (p->ainsn.inst_flag) {
268 if (p->ainsn.inst_flag & INST_FLAG_FIX_RELATIVE_IP_ADDR) {
269 /* Fix relative IP address */
270 regs->cr_iip = (regs->cr_iip - bundle_addr) + resume_addr;
273 if (p->ainsn.inst_flag & INST_FLAG_FIX_BRANCH_REG) {
275 * Fix target branch register, software convention is
276 * to use either b0 or b6 or b7, so just checking
277 * only those registers
279 switch (p->ainsn.target_br_reg) {
281 if ((regs->b0 == bundle_addr) ||
282 (regs->b0 == bundle_addr + 0x10)) {
283 regs->b0 = (regs->b0 - bundle_addr) +
288 if ((regs->b6 == bundle_addr) ||
289 (regs->b6 == bundle_addr + 0x10)) {
290 regs->b6 = (regs->b6 - bundle_addr) +
295 if ((regs->b7 == bundle_addr) ||
296 (regs->b7 == bundle_addr + 0x10)) {
297 regs->b7 = (regs->b7 - bundle_addr) +
307 if (regs->cr_iip == bundle_addr + 0x10) {
308 regs->cr_iip = resume_addr + 0x10;
311 if (regs->cr_iip == bundle_addr) {
312 regs->cr_iip = resume_addr;
317 /* Turn off Single Step bit */
318 ia64_psr(regs)->ss = 0;
321 static void prepare_ss(struct kprobe *p, struct pt_regs *regs)
323 unsigned long bundle_addr = (unsigned long) &p->opcode.bundle;
324 unsigned long slot = (unsigned long)p->addr & 0xf;
326 /* Update instruction pointer (IIP) and slot number (IPSR.ri) */
327 regs->cr_iip = bundle_addr & ~0xFULL;
332 ia64_psr(regs)->ri = slot;
334 /* turn on single stepping */
335 ia64_psr(regs)->ss = 1;
338 static int pre_kprobes_handler(struct pt_regs *regs)
342 kprobe_opcode_t *addr = (kprobe_opcode_t *)instruction_pointer(regs);
346 /* Handle recursion cases */
347 if (kprobe_running()) {
348 p = get_kprobe(addr);
350 if (kprobe_status == KPROBE_HIT_SS) {
354 arch_disarm_kprobe(p);
358 * jprobe instrumented function just completed
361 if (p->break_handler && p->break_handler(p, regs)) {
368 p = get_kprobe(addr);
374 kprobe_status = KPROBE_HIT_ACTIVE;
377 if (p->pre_handler && p->pre_handler(p, regs))
379 * Our pre-handler is specifically requesting that we just
380 * do a return. This is handling the case where the
381 * pre-handler is really our special jprobe pre-handler.
387 kprobe_status = KPROBE_HIT_SS;
391 preempt_enable_no_resched();
395 static int post_kprobes_handler(struct pt_regs *regs)
397 if (!kprobe_running())
400 if (current_kprobe->post_handler)
401 current_kprobe->post_handler(current_kprobe, regs, 0);
403 resume_execution(current_kprobe, regs);
406 preempt_enable_no_resched();
410 static int kprobes_fault_handler(struct pt_regs *regs, int trapnr)
412 if (!kprobe_running())
415 if (current_kprobe->fault_handler &&
416 current_kprobe->fault_handler(current_kprobe, regs, trapnr))
419 if (kprobe_status & KPROBE_HIT_SS) {
420 resume_execution(current_kprobe, regs);
422 preempt_enable_no_resched();
428 int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
431 struct die_args *args = (struct die_args *)data;
434 if (pre_kprobes_handler(args->regs))
438 if (post_kprobes_handler(args->regs))
442 if (kprobes_fault_handler(args->regs, args->trapnr))
450 int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
452 struct jprobe *jp = container_of(p, struct jprobe, kp);
453 unsigned long addr = ((struct fnptr *)(jp->entry))->ip;
455 /* save architectural state */
456 jprobe_saved_regs = *regs;
458 /* after rfi, execute the jprobe instrumented function */
459 regs->cr_iip = addr & ~0xFULL;
460 ia64_psr(regs)->ri = addr & 0xf;
461 regs->r1 = ((struct fnptr *)(jp->entry))->gp;
464 * fix the return address to our jprobe_inst_return() function
465 * in the jprobes.S file
467 regs->b0 = ((struct fnptr *)(jprobe_inst_return))->ip;
472 int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
474 *regs = jprobe_saved_regs;