Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Kernel Probes (KProbes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | * |
| 18 | * Copyright (C) IBM Corporation, 2002, 2004 |
| 19 | * |
| 20 | * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel |
| 21 | * Probes initial implementation ( includes contributions from |
| 22 | * Rusty Russell). |
| 23 | * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes |
| 24 | * interface to access function arguments. |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 25 | * 2004-Oct Jim Keniston <jkenisto@us.ibm.com> and Prasanna S Panchamukhi |
| 26 | * <prasanna@in.ibm.com> adapted for x86_64 from i386. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | * 2005-Mar Roland McGrath <roland@redhat.com> |
| 28 | * Fixed to handle %rip-relative addressing mode correctly. |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 29 | * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston |
| 30 | * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi |
| 31 | * <prasanna@in.ibm.com> added function-return probes. |
| 32 | * 2005-May Rusty Lynch <rusty.lynch@intel.com> |
| 33 | * Added function return probes functionality |
| 34 | * 2006-Feb Masami Hiramatsu <hiramatu@sdl.hitachi.co.jp> added |
| 35 | * kprobe-booster and kretprobe-booster for i386. |
Masami Hiramatsu | da07ab0 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 36 | * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com> added kprobe-booster |
| 37 | * and kretprobe-booster for x86-64 |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 38 | * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com>, Arjan van de Ven |
| 39 | * <arjan@infradead.org> and Jim Keniston <jkenisto@us.ibm.com> |
| 40 | * unified x86 kprobes code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | */ |
| 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <linux/kprobes.h> |
| 44 | #include <linux/ptrace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <linux/string.h> |
| 46 | #include <linux/slab.h> |
Quentin Barnes | b506a9d | 2008-01-30 13:32:32 +0100 | [diff] [blame] | 47 | #include <linux/hardirq.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <linux/preempt.h> |
Prasanna S Panchamukhi | c28f896 | 2006-03-26 01:38:23 -0800 | [diff] [blame] | 49 | #include <linux/module.h> |
Christoph Hellwig | 1eeb66a | 2007-05-08 00:27:03 -0700 | [diff] [blame] | 50 | #include <linux/kdebug.h> |
Masami Hiramatsu | b46b3d7 | 2009-08-13 16:34:28 -0400 | [diff] [blame] | 51 | #include <linux/kallsyms.h> |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 52 | |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 53 | #include <asm/cacheflush.h> |
| 54 | #include <asm/desc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | #include <asm/pgtable.h> |
Prasanna S Panchamukhi | c28f896 | 2006-03-26 01:38:23 -0800 | [diff] [blame] | 56 | #include <asm/uaccess.h> |
Andi Kleen | 19d36cc | 2007-07-22 11:12:31 +0200 | [diff] [blame] | 57 | #include <asm/alternative.h> |
Masami Hiramatsu | b46b3d7 | 2009-08-13 16:34:28 -0400 | [diff] [blame] | 58 | #include <asm/insn.h> |
K.Prasad | 62edab9 | 2009-06-01 23:47:06 +0530 | [diff] [blame] | 59 | #include <asm/debugreg.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | void jprobe_return_end(void); |
| 62 | |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 63 | DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL; |
| 64 | DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
H. Peter Anvin | 98272ed | 2009-10-12 14:14:10 -0700 | [diff] [blame] | 66 | #define stack_addr(regs) ((unsigned long *)kernel_stack_pointer(regs)) |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 67 | |
| 68 | #define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\ |
| 69 | (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \ |
| 70 | (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \ |
| 71 | (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \ |
| 72 | (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \ |
| 73 | << (row % 32)) |
| 74 | /* |
| 75 | * Undefined/reserved opcodes, conditional jump, Opcode Extension |
| 76 | * Groups, and some special opcodes can not boost. |
| 77 | */ |
| 78 | static const u32 twobyte_is_boostable[256 / 32] = { |
| 79 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ |
| 80 | /* ---------------------------------------------- */ |
| 81 | W(0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0) | /* 00 */ |
| 82 | W(0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 10 */ |
| 83 | W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 20 */ |
| 84 | W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */ |
| 85 | W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */ |
| 86 | W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */ |
| 87 | W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1) | /* 60 */ |
| 88 | W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */ |
| 89 | W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 80 */ |
| 90 | W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */ |
| 91 | W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* a0 */ |
| 92 | W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) , /* b0 */ |
| 93 | W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */ |
| 94 | W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) , /* d0 */ |
| 95 | W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* e0 */ |
| 96 | W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0) /* f0 */ |
| 97 | /* ----------------------------------------------- */ |
| 98 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ |
| 99 | }; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 100 | #undef W |
| 101 | |
Masami Hiramatsu | f438d91 | 2007-10-16 01:27:49 -0700 | [diff] [blame] | 102 | struct kretprobe_blackpoint kretprobe_blacklist[] = { |
| 103 | {"__switch_to", }, /* This function switches only current task, but |
| 104 | doesn't switch kernel stack.*/ |
| 105 | {NULL, NULL} /* Terminator */ |
| 106 | }; |
| 107 | const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist); |
| 108 | |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 109 | /* Insert a jump instruction at address 'from', which jumps to address 'to'.*/ |
Harvey Harrison | e7b5e11 | 2008-01-30 13:31:43 +0100 | [diff] [blame] | 110 | static void __kprobes set_jmp_op(void *from, void *to) |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 111 | { |
| 112 | struct __arch_jmp_op { |
| 113 | char op; |
| 114 | s32 raddr; |
| 115 | } __attribute__((packed)) * jop; |
| 116 | jop = (struct __arch_jmp_op *)from; |
| 117 | jop->raddr = (s32)((long)(to) - ((long)(from) + 5)); |
| 118 | jop->op = RELATIVEJUMP_INSTRUCTION; |
| 119 | } |
| 120 | |
| 121 | /* |
Harvey Harrison | 9930927 | 2008-01-30 13:32:14 +0100 | [diff] [blame] | 122 | * Check for the REX prefix which can only exist on X86_64 |
| 123 | * X86_32 always returns 0 |
| 124 | */ |
| 125 | static int __kprobes is_REX_prefix(kprobe_opcode_t *insn) |
| 126 | { |
| 127 | #ifdef CONFIG_X86_64 |
| 128 | if ((*insn & 0xf0) == 0x40) |
| 129 | return 1; |
| 130 | #endif |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | /* |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 135 | * Returns non-zero if opcode is boostable. |
| 136 | * RIP relative instructions are adjusted at copying time in 64 bits mode |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 137 | */ |
Harvey Harrison | e7b5e11 | 2008-01-30 13:31:43 +0100 | [diff] [blame] | 138 | static int __kprobes can_boost(kprobe_opcode_t *opcodes) |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 139 | { |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 140 | kprobe_opcode_t opcode; |
| 141 | kprobe_opcode_t *orig_opcodes = opcodes; |
| 142 | |
Jaswinder Singh Rajput | cde5edb | 2009-03-18 17:37:45 +0530 | [diff] [blame] | 143 | if (search_exception_tables((unsigned long)opcodes)) |
Masami Hiramatsu | 3039088 | 2009-03-16 18:57:22 -0400 | [diff] [blame] | 144 | return 0; /* Page fault may occur on this address. */ |
| 145 | |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 146 | retry: |
| 147 | if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1) |
| 148 | return 0; |
| 149 | opcode = *(opcodes++); |
| 150 | |
| 151 | /* 2nd-byte opcode */ |
| 152 | if (opcode == 0x0f) { |
| 153 | if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1) |
| 154 | return 0; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 155 | return test_bit(*opcodes, |
| 156 | (unsigned long *)twobyte_is_boostable); |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | switch (opcode & 0xf0) { |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 160 | #ifdef CONFIG_X86_64 |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 161 | case 0x40: |
| 162 | goto retry; /* REX prefix is boostable */ |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 163 | #endif |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 164 | case 0x60: |
| 165 | if (0x63 < opcode && opcode < 0x67) |
| 166 | goto retry; /* prefixes */ |
| 167 | /* can't boost Address-size override and bound */ |
| 168 | return (opcode != 0x62 && opcode != 0x67); |
| 169 | case 0x70: |
| 170 | return 0; /* can't boost conditional jump */ |
| 171 | case 0xc0: |
| 172 | /* can't boost software-interruptions */ |
| 173 | return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf; |
| 174 | case 0xd0: |
| 175 | /* can boost AA* and XLAT */ |
| 176 | return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7); |
| 177 | case 0xe0: |
| 178 | /* can boost in/out and absolute jmps */ |
| 179 | return ((opcode & 0x04) || opcode == 0xea); |
| 180 | case 0xf0: |
| 181 | if ((opcode & 0x0c) == 0 && opcode != 0xf1) |
| 182 | goto retry; /* lock/rep(ne) prefix */ |
| 183 | /* clear and set flags are boostable */ |
| 184 | return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe)); |
| 185 | default: |
| 186 | /* segment override prefixes are boostable */ |
| 187 | if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e) |
| 188 | goto retry; /* prefixes */ |
| 189 | /* CS override prefix and call are not boostable */ |
| 190 | return (opcode != 0x2e && opcode != 0x9a); |
| 191 | } |
| 192 | } |
| 193 | |
Masami Hiramatsu | b46b3d7 | 2009-08-13 16:34:28 -0400 | [diff] [blame] | 194 | /* Recover the probed instruction at addr for further analysis. */ |
| 195 | static int recover_probed_instruction(kprobe_opcode_t *buf, unsigned long addr) |
| 196 | { |
| 197 | struct kprobe *kp; |
| 198 | kp = get_kprobe((void *)addr); |
| 199 | if (!kp) |
| 200 | return -EINVAL; |
| 201 | |
| 202 | /* |
| 203 | * Basically, kp->ainsn.insn has an original instruction. |
| 204 | * However, RIP-relative instruction can not do single-stepping |
| 205 | * at different place, fix_riprel() tweaks the displacement of |
| 206 | * that instruction. In that case, we can't recover the instruction |
| 207 | * from the kp->ainsn.insn. |
| 208 | * |
| 209 | * On the other hand, kp->opcode has a copy of the first byte of |
| 210 | * the probed instruction, which is overwritten by int3. And |
| 211 | * the instruction at kp->addr is not modified by kprobes except |
| 212 | * for the first byte, we can recover the original instruction |
| 213 | * from it and kp->opcode. |
| 214 | */ |
| 215 | memcpy(buf, kp->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t)); |
| 216 | buf[0] = kp->opcode; |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | /* Dummy buffers for kallsyms_lookup */ |
| 221 | static char __dummy_buf[KSYM_NAME_LEN]; |
| 222 | |
| 223 | /* Check if paddr is at an instruction boundary */ |
| 224 | static int __kprobes can_probe(unsigned long paddr) |
| 225 | { |
| 226 | int ret; |
| 227 | unsigned long addr, offset = 0; |
| 228 | struct insn insn; |
| 229 | kprobe_opcode_t buf[MAX_INSN_SIZE]; |
| 230 | |
| 231 | if (!kallsyms_lookup(paddr, NULL, &offset, NULL, __dummy_buf)) |
| 232 | return 0; |
| 233 | |
| 234 | /* Decode instructions */ |
| 235 | addr = paddr - offset; |
| 236 | while (addr < paddr) { |
| 237 | kernel_insn_init(&insn, (void *)addr); |
| 238 | insn_get_opcode(&insn); |
| 239 | |
| 240 | /* |
| 241 | * Check if the instruction has been modified by another |
| 242 | * kprobe, in which case we replace the breakpoint by the |
| 243 | * original instruction in our buffer. |
| 244 | */ |
| 245 | if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION) { |
| 246 | ret = recover_probed_instruction(buf, addr); |
| 247 | if (ret) |
| 248 | /* |
| 249 | * Another debugging subsystem might insert |
| 250 | * this breakpoint. In that case, we can't |
| 251 | * recover it. |
| 252 | */ |
| 253 | return 0; |
| 254 | kernel_insn_init(&insn, buf); |
| 255 | } |
| 256 | insn_get_length(&insn); |
| 257 | addr += insn.length; |
| 258 | } |
| 259 | |
| 260 | return (addr == paddr); |
| 261 | } |
| 262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | /* |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 264 | * Returns non-zero if opcode modifies the interrupt flag. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | */ |
Andrew Morton | 8645419 | 2007-11-26 20:42:19 +0100 | [diff] [blame] | 266 | static int __kprobes is_IF_modifier(kprobe_opcode_t *insn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | { |
| 268 | switch (*insn) { |
| 269 | case 0xfa: /* cli */ |
| 270 | case 0xfb: /* sti */ |
| 271 | case 0xcf: /* iret/iretd */ |
| 272 | case 0x9d: /* popf/popfd */ |
| 273 | return 1; |
| 274 | } |
Harvey Harrison | 9930927 | 2008-01-30 13:32:14 +0100 | [diff] [blame] | 275 | |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 276 | /* |
Harvey Harrison | 9930927 | 2008-01-30 13:32:14 +0100 | [diff] [blame] | 277 | * on X86_64, 0x40-0x4f are REX prefixes so we need to look |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 278 | * at the next byte instead.. but of course not recurse infinitely |
| 279 | */ |
Harvey Harrison | 9930927 | 2008-01-30 13:32:14 +0100 | [diff] [blame] | 280 | if (is_REX_prefix(insn)) |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 281 | return is_IF_modifier(++insn); |
Harvey Harrison | 9930927 | 2008-01-30 13:32:14 +0100 | [diff] [blame] | 282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | /* |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 287 | * Adjust the displacement if the instruction uses the %rip-relative |
| 288 | * addressing mode. |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 289 | * If it does, Return the address of the 32-bit displacement word. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | * If not, return null. |
Harvey Harrison | 31f80e4 | 2008-01-30 13:32:16 +0100 | [diff] [blame] | 291 | * Only applicable to 64-bit x86. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | */ |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 293 | static void __kprobes fix_riprel(struct kprobe *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | { |
Harvey Harrison | 31f80e4 | 2008-01-30 13:32:16 +0100 | [diff] [blame] | 295 | #ifdef CONFIG_X86_64 |
Masami Hiramatsu | 89ae465 | 2009-08-13 16:34:36 -0400 | [diff] [blame] | 296 | struct insn insn; |
| 297 | kernel_insn_init(&insn, p->ainsn.insn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | |
Masami Hiramatsu | 89ae465 | 2009-08-13 16:34:36 -0400 | [diff] [blame] | 299 | if (insn_rip_relative(&insn)) { |
| 300 | s64 newdisp; |
| 301 | u8 *disp; |
| 302 | insn_get_displacement(&insn); |
| 303 | /* |
| 304 | * The copied instruction uses the %rip-relative addressing |
| 305 | * mode. Adjust the displacement for the difference between |
| 306 | * the original location of this instruction and the location |
| 307 | * of the copy that will actually be run. The tricky bit here |
| 308 | * is making sure that the sign extension happens correctly in |
| 309 | * this calculation, since we need a signed 32-bit result to |
| 310 | * be sign-extended to 64 bits when it's added to the %rip |
| 311 | * value and yield the same 64-bit result that the sign- |
| 312 | * extension of the original signed 32-bit displacement would |
| 313 | * have given. |
| 314 | */ |
| 315 | newdisp = (u8 *) p->addr + (s64) insn.displacement.value - |
| 316 | (u8 *) p->ainsn.insn; |
| 317 | BUG_ON((s64) (s32) newdisp != newdisp); /* Sanity check. */ |
| 318 | disp = (u8 *) p->ainsn.insn + insn_offset_displacement(&insn); |
| 319 | *(s32 *) disp = (s32) newdisp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | } |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 321 | #endif |
Harvey Harrison | 31f80e4 | 2008-01-30 13:32:16 +0100 | [diff] [blame] | 322 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | |
Keshavamurthy Anil S | f709b12 | 2006-01-09 20:52:44 -0800 | [diff] [blame] | 324 | static void __kprobes arch_copy_kprobe(struct kprobe *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | { |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 326 | memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t)); |
Harvey Harrison | 31f80e4 | 2008-01-30 13:32:16 +0100 | [diff] [blame] | 327 | |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 328 | fix_riprel(p); |
Harvey Harrison | 31f80e4 | 2008-01-30 13:32:16 +0100 | [diff] [blame] | 329 | |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 330 | if (can_boost(p->addr)) |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 331 | p->ainsn.boostable = 0; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 332 | else |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 333 | p->ainsn.boostable = -1; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 334 | |
Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame] | 335 | p->opcode = *p->addr; |
| 336 | } |
| 337 | |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 338 | int __kprobes arch_prepare_kprobe(struct kprobe *p) |
| 339 | { |
Masami Hiramatsu | b46b3d7 | 2009-08-13 16:34:28 -0400 | [diff] [blame] | 340 | if (!can_probe((unsigned long)p->addr)) |
| 341 | return -EILSEQ; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 342 | /* insn: must be on special executable page on x86. */ |
| 343 | p->ainsn.insn = get_insn_slot(); |
| 344 | if (!p->ainsn.insn) |
| 345 | return -ENOMEM; |
| 346 | arch_copy_kprobe(p); |
| 347 | return 0; |
| 348 | } |
| 349 | |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 350 | void __kprobes arch_arm_kprobe(struct kprobe *p) |
Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame] | 351 | { |
Andi Kleen | 19d36cc | 2007-07-22 11:12:31 +0200 | [diff] [blame] | 352 | text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1); |
Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 355 | void __kprobes arch_disarm_kprobe(struct kprobe *p) |
Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame] | 356 | { |
Andi Kleen | 19d36cc | 2007-07-22 11:12:31 +0200 | [diff] [blame] | 357 | text_poke(p->addr, &p->opcode, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Ananth N Mavinakayanahalli | 0498b63 | 2006-01-09 20:52:46 -0800 | [diff] [blame] | 360 | void __kprobes arch_remove_kprobe(struct kprobe *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | { |
Masami Hiramatsu | 1294156 | 2009-01-06 14:41:50 -0800 | [diff] [blame] | 362 | if (p->ainsn.insn) { |
| 363 | free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1)); |
| 364 | p->ainsn.insn = NULL; |
| 365 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Prasanna S Panchamukhi | 3b60211 | 2006-04-18 22:22:00 -0700 | [diff] [blame] | 368 | static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb) |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 369 | { |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 370 | kcb->prev_kprobe.kp = kprobe_running(); |
| 371 | kcb->prev_kprobe.status = kcb->kprobe_status; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 372 | kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags; |
| 373 | kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags; |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Prasanna S Panchamukhi | 3b60211 | 2006-04-18 22:22:00 -0700 | [diff] [blame] | 376 | static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb) |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 377 | { |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 378 | __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp; |
| 379 | kcb->kprobe_status = kcb->prev_kprobe.status; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 380 | kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags; |
| 381 | kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags; |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Prasanna S Panchamukhi | 3b60211 | 2006-04-18 22:22:00 -0700 | [diff] [blame] | 384 | static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs, |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 385 | struct kprobe_ctlblk *kcb) |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 386 | { |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 387 | __get_cpu_var(current_kprobe) = p; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 388 | kcb->kprobe_saved_flags = kcb->kprobe_old_flags |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 389 | = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF)); |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 390 | if (is_IF_modifier(p->ainsn.insn)) |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 391 | kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF; |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Harvey Harrison | e7b5e11 | 2008-01-30 13:31:43 +0100 | [diff] [blame] | 394 | static void __kprobes clear_btf(void) |
Roland McGrath | 1ecc798 | 2008-01-30 13:30:54 +0100 | [diff] [blame] | 395 | { |
| 396 | if (test_thread_flag(TIF_DEBUGCTLMSR)) |
Jan Beulich | 5b0e508 | 2008-03-10 13:11:17 +0000 | [diff] [blame] | 397 | update_debugctlmsr(0); |
Roland McGrath | 1ecc798 | 2008-01-30 13:30:54 +0100 | [diff] [blame] | 398 | } |
| 399 | |
Harvey Harrison | e7b5e11 | 2008-01-30 13:31:43 +0100 | [diff] [blame] | 400 | static void __kprobes restore_btf(void) |
Roland McGrath | 1ecc798 | 2008-01-30 13:30:54 +0100 | [diff] [blame] | 401 | { |
| 402 | if (test_thread_flag(TIF_DEBUGCTLMSR)) |
Jan Beulich | 5b0e508 | 2008-03-10 13:11:17 +0000 | [diff] [blame] | 403 | update_debugctlmsr(current->thread.debugctlmsr); |
Roland McGrath | 1ecc798 | 2008-01-30 13:30:54 +0100 | [diff] [blame] | 404 | } |
| 405 | |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 406 | static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | { |
Roland McGrath | 1ecc798 | 2008-01-30 13:30:54 +0100 | [diff] [blame] | 408 | clear_btf(); |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 409 | regs->flags |= X86_EFLAGS_TF; |
| 410 | regs->flags &= ~X86_EFLAGS_IF; |
Harvey Harrison | e7b5e11 | 2008-01-30 13:31:43 +0100 | [diff] [blame] | 411 | /* single step inline if the instruction is an int3 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | if (p->opcode == BREAKPOINT_INSTRUCTION) |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 413 | regs->ip = (unsigned long)p->addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | else |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 415 | regs->ip = (unsigned long)p->ainsn.insn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Christoph Hellwig | 4c4308c | 2007-05-08 00:34:14 -0700 | [diff] [blame] | 418 | void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri, |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 419 | struct pt_regs *regs) |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 420 | { |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 421 | unsigned long *sara = stack_addr(regs); |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 422 | |
Christoph Hellwig | 4c4308c | 2007-05-08 00:34:14 -0700 | [diff] [blame] | 423 | ri->ret_addr = (kprobe_opcode_t *) *sara; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 424 | |
Christoph Hellwig | 4c4308c | 2007-05-08 00:34:14 -0700 | [diff] [blame] | 425 | /* Replace the return addr with trampoline addr */ |
| 426 | *sara = (unsigned long) &kretprobe_trampoline; |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 427 | } |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 428 | |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 429 | static void __kprobes setup_singlestep(struct kprobe *p, struct pt_regs *regs, |
| 430 | struct kprobe_ctlblk *kcb) |
| 431 | { |
Masami Hiramatsu | 5a4ccaf | 2009-01-06 21:15:32 +0100 | [diff] [blame] | 432 | #if !defined(CONFIG_PREEMPT) || defined(CONFIG_FREEZER) |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 433 | if (p->ainsn.boostable == 1 && !p->post_handler) { |
| 434 | /* Boost up -- we can execute copied instructions directly */ |
| 435 | reset_current_kprobe(); |
| 436 | regs->ip = (unsigned long)p->ainsn.insn; |
| 437 | preempt_enable_no_resched(); |
| 438 | return; |
| 439 | } |
| 440 | #endif |
| 441 | prepare_singlestep(p, regs); |
| 442 | kcb->kprobe_status = KPROBE_HIT_SS; |
| 443 | } |
| 444 | |
Harvey Harrison | 40102d4 | 2008-01-30 13:32:02 +0100 | [diff] [blame] | 445 | /* |
| 446 | * We have reentered the kprobe_handler(), since another probe was hit while |
| 447 | * within the handler. We save the original kprobes variables and just single |
| 448 | * step on the instruction of the new probe without calling any user handlers. |
| 449 | */ |
Masami Hiramatsu | 59e87cd | 2008-01-30 13:32:02 +0100 | [diff] [blame] | 450 | static int __kprobes reenter_kprobe(struct kprobe *p, struct pt_regs *regs, |
| 451 | struct kprobe_ctlblk *kcb) |
Harvey Harrison | 40102d4 | 2008-01-30 13:32:02 +0100 | [diff] [blame] | 452 | { |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 453 | switch (kcb->kprobe_status) { |
| 454 | case KPROBE_HIT_SSDONE: |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 455 | case KPROBE_HIT_ACTIVE: |
Abhishek Sagar | fb8830e | 2008-01-30 13:33:13 +0100 | [diff] [blame] | 456 | save_previous_kprobe(kcb); |
| 457 | set_current_kprobe(p, regs, kcb); |
| 458 | kprobes_inc_nmissed_count(p); |
| 459 | prepare_singlestep(p, regs); |
| 460 | kcb->kprobe_status = KPROBE_REENTER; |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 461 | break; |
| 462 | case KPROBE_HIT_SS: |
Masami Hiramatsu | e9afe9e | 2009-08-27 13:22:58 -0400 | [diff] [blame] | 463 | /* A probe has been hit in the codepath leading up to, or just |
| 464 | * after, single-stepping of a probed instruction. This entire |
| 465 | * codepath should strictly reside in .kprobes.text section. |
| 466 | * Raise a BUG or we'll continue in an endless reentering loop |
| 467 | * and eventually a stack overflow. |
| 468 | */ |
| 469 | printk(KERN_WARNING "Unrecoverable kprobe detected at %p.\n", |
| 470 | p->addr); |
| 471 | dump_kprobe(p); |
| 472 | BUG(); |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 473 | default: |
| 474 | /* impossible cases */ |
| 475 | WARN_ON(1); |
Abhishek Sagar | fb8830e | 2008-01-30 13:33:13 +0100 | [diff] [blame] | 476 | return 0; |
Masami Hiramatsu | 59e87cd | 2008-01-30 13:32:02 +0100 | [diff] [blame] | 477 | } |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 478 | |
Masami Hiramatsu | 59e87cd | 2008-01-30 13:32:02 +0100 | [diff] [blame] | 479 | return 1; |
Harvey Harrison | 40102d4 | 2008-01-30 13:32:02 +0100 | [diff] [blame] | 480 | } |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 481 | |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 482 | /* |
| 483 | * Interrupts are disabled on entry as trap3 is an interrupt gate and they |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 484 | * remain disabled throughout this function. |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 485 | */ |
| 486 | static int __kprobes kprobe_handler(struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | { |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 488 | kprobe_opcode_t *addr; |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 489 | struct kprobe *p; |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 490 | struct kprobe_ctlblk *kcb; |
| 491 | |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 492 | addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t)); |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 493 | if (*addr != BREAKPOINT_INSTRUCTION) { |
| 494 | /* |
| 495 | * The breakpoint instruction was removed right |
| 496 | * after we hit it. Another cpu has removed |
| 497 | * either a probepoint or a debugger breakpoint |
| 498 | * at this address. In either case, no further |
| 499 | * handling of this interrupt is appropriate. |
| 500 | * Back up over the (now missing) int3 and run |
| 501 | * the original instruction. |
| 502 | */ |
| 503 | regs->ip = (unsigned long)addr; |
| 504 | return 1; |
| 505 | } |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 506 | |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 507 | /* |
| 508 | * We don't want to be preempted for the entire |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 509 | * duration of kprobe processing. We conditionally |
| 510 | * re-enable preemption at the end of this function, |
| 511 | * and also in reenter_kprobe() and setup_singlestep(). |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 512 | */ |
| 513 | preempt_disable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 515 | kcb = get_kprobe_ctlblk(); |
Harvey Harrison | b976015 | 2008-01-30 13:32:19 +0100 | [diff] [blame] | 516 | p = get_kprobe(addr); |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 517 | |
Harvey Harrison | b976015 | 2008-01-30 13:32:19 +0100 | [diff] [blame] | 518 | if (p) { |
Harvey Harrison | b976015 | 2008-01-30 13:32:19 +0100 | [diff] [blame] | 519 | if (kprobe_running()) { |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 520 | if (reenter_kprobe(p, regs, kcb)) |
| 521 | return 1; |
Harvey Harrison | b976015 | 2008-01-30 13:32:19 +0100 | [diff] [blame] | 522 | } else { |
| 523 | set_current_kprobe(p, regs, kcb); |
| 524 | kcb->kprobe_status = KPROBE_HIT_ACTIVE; |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | /* |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 527 | * If we have no pre-handler or it returned 0, we |
| 528 | * continue with normal processing. If we have a |
| 529 | * pre-handler and it returned non-zero, it prepped |
| 530 | * for calling the break_handler below on re-entry |
| 531 | * for jprobe processing, so get out doing nothing |
| 532 | * more here. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | */ |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 534 | if (!p->pre_handler || !p->pre_handler(p, regs)) |
| 535 | setup_singlestep(p, regs, kcb); |
| 536 | return 1; |
Harvey Harrison | b976015 | 2008-01-30 13:32:19 +0100 | [diff] [blame] | 537 | } |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 538 | } else if (kprobe_running()) { |
| 539 | p = __get_cpu_var(current_kprobe); |
| 540 | if (p->break_handler && p->break_handler(p, regs)) { |
| 541 | setup_singlestep(p, regs, kcb); |
| 542 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | } |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 544 | } /* else: not a kprobe fault; let the kernel handle it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 546 | preempt_enable_no_resched(); |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 547 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | /* |
Masami Hiramatsu | da07ab0 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 551 | * When a retprobed function returns, this code saves registers and |
| 552 | * calls trampoline_handler() runs, which calls the kretprobe's handler. |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 553 | */ |
Harvey Harrison | f1452d4 | 2008-02-14 15:23:53 -0800 | [diff] [blame] | 554 | static void __used __kprobes kretprobe_trampoline_holder(void) |
Harvey Harrison | 1017579 | 2008-01-30 13:33:01 +0100 | [diff] [blame] | 555 | { |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 556 | asm volatile ( |
| 557 | ".global kretprobe_trampoline\n" |
Masami Hiramatsu | da07ab0 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 558 | "kretprobe_trampoline: \n" |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 559 | #ifdef CONFIG_X86_64 |
Masami Hiramatsu | da07ab0 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 560 | /* We don't bother saving the ss register */ |
| 561 | " pushq %rsp\n" |
| 562 | " pushfq\n" |
| 563 | /* |
| 564 | * Skip cs, ip, orig_ax. |
| 565 | * trampoline_handler() will plug in these values |
| 566 | */ |
| 567 | " subq $24, %rsp\n" |
| 568 | " pushq %rdi\n" |
| 569 | " pushq %rsi\n" |
| 570 | " pushq %rdx\n" |
| 571 | " pushq %rcx\n" |
| 572 | " pushq %rax\n" |
| 573 | " pushq %r8\n" |
| 574 | " pushq %r9\n" |
| 575 | " pushq %r10\n" |
| 576 | " pushq %r11\n" |
| 577 | " pushq %rbx\n" |
| 578 | " pushq %rbp\n" |
| 579 | " pushq %r12\n" |
| 580 | " pushq %r13\n" |
| 581 | " pushq %r14\n" |
| 582 | " pushq %r15\n" |
| 583 | " movq %rsp, %rdi\n" |
| 584 | " call trampoline_handler\n" |
| 585 | /* Replace saved sp with true return address. */ |
| 586 | " movq %rax, 152(%rsp)\n" |
| 587 | " popq %r15\n" |
| 588 | " popq %r14\n" |
| 589 | " popq %r13\n" |
| 590 | " popq %r12\n" |
| 591 | " popq %rbp\n" |
| 592 | " popq %rbx\n" |
| 593 | " popq %r11\n" |
| 594 | " popq %r10\n" |
| 595 | " popq %r9\n" |
| 596 | " popq %r8\n" |
| 597 | " popq %rax\n" |
| 598 | " popq %rcx\n" |
| 599 | " popq %rdx\n" |
| 600 | " popq %rsi\n" |
| 601 | " popq %rdi\n" |
| 602 | /* Skip orig_ax, ip, cs */ |
| 603 | " addq $24, %rsp\n" |
| 604 | " popfq\n" |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 605 | #else |
| 606 | " pushf\n" |
| 607 | /* |
Masami Hiramatsu | fee039a | 2009-03-23 10:14:52 -0400 | [diff] [blame] | 608 | * Skip cs, ip, orig_ax and gs. |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 609 | * trampoline_handler() will plug in these values |
| 610 | */ |
Masami Hiramatsu | fee039a | 2009-03-23 10:14:52 -0400 | [diff] [blame] | 611 | " subl $16, %esp\n" |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 612 | " pushl %fs\n" |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 613 | " pushl %es\n" |
Masami Hiramatsu | fee039a | 2009-03-23 10:14:52 -0400 | [diff] [blame] | 614 | " pushl %ds\n" |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 615 | " pushl %eax\n" |
| 616 | " pushl %ebp\n" |
| 617 | " pushl %edi\n" |
| 618 | " pushl %esi\n" |
| 619 | " pushl %edx\n" |
| 620 | " pushl %ecx\n" |
| 621 | " pushl %ebx\n" |
| 622 | " movl %esp, %eax\n" |
| 623 | " call trampoline_handler\n" |
| 624 | /* Move flags to cs */ |
Masami Hiramatsu | fee039a | 2009-03-23 10:14:52 -0400 | [diff] [blame] | 625 | " movl 56(%esp), %edx\n" |
| 626 | " movl %edx, 52(%esp)\n" |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 627 | /* Replace saved flags with true return address. */ |
Masami Hiramatsu | fee039a | 2009-03-23 10:14:52 -0400 | [diff] [blame] | 628 | " movl %eax, 56(%esp)\n" |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 629 | " popl %ebx\n" |
| 630 | " popl %ecx\n" |
| 631 | " popl %edx\n" |
| 632 | " popl %esi\n" |
| 633 | " popl %edi\n" |
| 634 | " popl %ebp\n" |
| 635 | " popl %eax\n" |
Masami Hiramatsu | fee039a | 2009-03-23 10:14:52 -0400 | [diff] [blame] | 636 | /* Skip ds, es, fs, gs, orig_ax and ip */ |
| 637 | " addl $24, %esp\n" |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 638 | " popf\n" |
| 639 | #endif |
Masami Hiramatsu | da07ab0 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 640 | " ret\n"); |
Harvey Harrison | 1017579 | 2008-01-30 13:33:01 +0100 | [diff] [blame] | 641 | } |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 642 | |
| 643 | /* |
Masami Hiramatsu | da07ab0 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 644 | * Called from kretprobe_trampoline |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 645 | */ |
Harvey Harrison | f1452d4 | 2008-02-14 15:23:53 -0800 | [diff] [blame] | 646 | static __used __kprobes void *trampoline_handler(struct pt_regs *regs) |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 647 | { |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 648 | struct kretprobe_instance *ri = NULL; |
bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 649 | struct hlist_head *head, empty_rp; |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 650 | struct hlist_node *node, *tmp; |
Ananth N Mavinakayanahalli | 991a51d | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 651 | unsigned long flags, orig_ret_address = 0; |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 652 | unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline; |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 653 | |
bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 654 | INIT_HLIST_HEAD(&empty_rp); |
Srinivasa D S | ef53d9c | 2008-07-25 01:46:04 -0700 | [diff] [blame] | 655 | kretprobe_hash_lock(current, &head, &flags); |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 656 | /* fixup registers */ |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 657 | #ifdef CONFIG_X86_64 |
Masami Hiramatsu | da07ab0 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 658 | regs->cs = __KERNEL_CS; |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 659 | #else |
| 660 | regs->cs = __KERNEL_CS | get_kernel_rpl(); |
Masami Hiramatsu | fee039a | 2009-03-23 10:14:52 -0400 | [diff] [blame] | 661 | regs->gs = 0; |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 662 | #endif |
Masami Hiramatsu | da07ab0 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 663 | regs->ip = trampoline_address; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 664 | regs->orig_ax = ~0UL; |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 665 | |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 666 | /* |
| 667 | * It is possible to have multiple instances associated with a given |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 668 | * task either because multiple functions in the call path have |
Frederik Schwarzer | 025dfda | 2008-10-16 19:02:37 +0200 | [diff] [blame] | 669 | * return probes installed on them, and/or more than one |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 670 | * return probe was registered for a target function. |
| 671 | * |
| 672 | * We can handle this because: |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 673 | * - instances are always pushed into the head of the list |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 674 | * - when multiple return probes are registered for the same |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 675 | * function, the (chronologically) first instance's ret_addr |
| 676 | * will be the real return address, and all the rest will |
| 677 | * point to kretprobe_trampoline. |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 678 | */ |
| 679 | hlist_for_each_entry_safe(ri, node, tmp, head, hlist) { |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 680 | if (ri->task != current) |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 681 | /* another task is sharing our hash bucket */ |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 682 | continue; |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 683 | |
Masami Hiramatsu | da07ab0 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 684 | if (ri->rp && ri->rp->handler) { |
| 685 | __get_cpu_var(current_kprobe) = &ri->rp->kp; |
| 686 | get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE; |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 687 | ri->rp->handler(ri, regs); |
Masami Hiramatsu | da07ab0 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 688 | __get_cpu_var(current_kprobe) = NULL; |
| 689 | } |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 690 | |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 691 | orig_ret_address = (unsigned long)ri->ret_addr; |
bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 692 | recycle_rp_inst(ri, &empty_rp); |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 693 | |
| 694 | if (orig_ret_address != trampoline_address) |
| 695 | /* |
| 696 | * This is the real return address. Any other |
| 697 | * instances associated with this task are for |
| 698 | * other calls deeper on the call stack |
| 699 | */ |
| 700 | break; |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 701 | } |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 702 | |
Ananth N Mavinakayanahalli | 0f95b7f | 2007-05-08 00:28:27 -0700 | [diff] [blame] | 703 | kretprobe_assert(ri, orig_ret_address, trampoline_address); |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 704 | |
Srinivasa D S | ef53d9c | 2008-07-25 01:46:04 -0700 | [diff] [blame] | 705 | kretprobe_hash_unlock(current, &flags); |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 706 | |
bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 707 | hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) { |
| 708 | hlist_del(&ri->hlist); |
| 709 | kfree(ri); |
| 710 | } |
Masami Hiramatsu | da07ab0 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 711 | return (void *)orig_ret_address; |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | * Called after single-stepping. p->addr is the address of the |
| 716 | * instruction whose first byte has been replaced by the "int 3" |
| 717 | * instruction. To avoid the SMP problems that can occur when we |
| 718 | * temporarily put back the original opcode to single-step, we |
| 719 | * single-stepped a copy of the instruction. The address of this |
| 720 | * copy is p->ainsn.insn. |
| 721 | * |
| 722 | * This function prepares to return from the post-single-step |
| 723 | * interrupt. We have to fix up the stack as follows: |
| 724 | * |
| 725 | * 0) Except in the case of absolute or indirect jump or call instructions, |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 726 | * the new ip is relative to the copied instruction. We need to make |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | * it relative to the original instruction. |
| 728 | * |
| 729 | * 1) If the single-stepped instruction was pushfl, then the TF and IF |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 730 | * flags are set in the just-pushed flags, and may need to be cleared. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | * |
| 732 | * 2) If the single-stepped instruction was a call, the return address |
| 733 | * that is atop the stack is the address following the copied instruction. |
| 734 | * We need to make it the address following the original instruction. |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 735 | * |
| 736 | * If this is the first time we've single-stepped the instruction at |
| 737 | * this probepoint, and the instruction is boostable, boost it: add a |
| 738 | * jump instruction after the copied instruction, that jumps to the next |
| 739 | * instruction after the probepoint. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | */ |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 741 | static void __kprobes resume_execution(struct kprobe *p, |
| 742 | struct pt_regs *regs, struct kprobe_ctlblk *kcb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | { |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 744 | unsigned long *tos = stack_addr(regs); |
| 745 | unsigned long copy_ip = (unsigned long)p->ainsn.insn; |
| 746 | unsigned long orig_ip = (unsigned long)p->addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | kprobe_opcode_t *insn = p->ainsn.insn; |
| 748 | |
| 749 | /*skip the REX prefix*/ |
Harvey Harrison | 9930927 | 2008-01-30 13:32:14 +0100 | [diff] [blame] | 750 | if (is_REX_prefix(insn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | insn++; |
| 752 | |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 753 | regs->flags &= ~X86_EFLAGS_TF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | switch (*insn) { |
Masami Hiramatsu | 0b0122f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 755 | case 0x9c: /* pushfl */ |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 756 | *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF); |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 757 | *tos |= kcb->kprobe_old_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | break; |
Masami Hiramatsu | 0b0122f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 759 | case 0xc2: /* iret/ret/lret */ |
| 760 | case 0xc3: |
Prasanna S Panchamukhi | 0b9e2ca | 2005-05-05 16:15:40 -0700 | [diff] [blame] | 761 | case 0xca: |
Masami Hiramatsu | 0b0122f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 762 | case 0xcb: |
| 763 | case 0xcf: |
| 764 | case 0xea: /* jmp absolute -- ip is correct */ |
| 765 | /* ip is already adjusted, no more changes required */ |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 766 | p->ainsn.boostable = 1; |
Masami Hiramatsu | 0b0122f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 767 | goto no_change; |
| 768 | case 0xe8: /* call relative - Fix return addr */ |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 769 | *tos = orig_ip + (*tos - copy_ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | break; |
Harvey Harrison | e7b5e11 | 2008-01-30 13:31:43 +0100 | [diff] [blame] | 771 | #ifdef CONFIG_X86_32 |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 772 | case 0x9a: /* call absolute -- same as call absolute, indirect */ |
| 773 | *tos = orig_ip + (*tos - copy_ip); |
| 774 | goto no_change; |
| 775 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | case 0xff: |
Satoshi Oshima | dc49e34 | 2006-05-20 15:00:21 -0700 | [diff] [blame] | 777 | if ((insn[1] & 0x30) == 0x10) { |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 778 | /* |
| 779 | * call absolute, indirect |
| 780 | * Fix return addr; ip is correct. |
| 781 | * But this is not boostable |
| 782 | */ |
| 783 | *tos = orig_ip + (*tos - copy_ip); |
Masami Hiramatsu | 0b0122f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 784 | goto no_change; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 785 | } else if (((insn[1] & 0x31) == 0x20) || |
| 786 | ((insn[1] & 0x31) == 0x21)) { |
| 787 | /* |
| 788 | * jmp near and far, absolute indirect |
| 789 | * ip is correct. And this is boostable |
| 790 | */ |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 791 | p->ainsn.boostable = 1; |
Masami Hiramatsu | 0b0122f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 792 | goto no_change; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | default: |
| 795 | break; |
| 796 | } |
| 797 | |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 798 | if (p->ainsn.boostable == 0) { |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 799 | if ((regs->ip > copy_ip) && |
| 800 | (regs->ip - copy_ip) + 5 < MAX_INSN_SIZE) { |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 801 | /* |
| 802 | * These instructions can be executed directly if it |
| 803 | * jumps back to correct address. |
| 804 | */ |
| 805 | set_jmp_op((void *)regs->ip, |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 806 | (void *)orig_ip + (regs->ip - copy_ip)); |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 807 | p->ainsn.boostable = 1; |
| 808 | } else { |
| 809 | p->ainsn.boostable = -1; |
| 810 | } |
| 811 | } |
| 812 | |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 813 | regs->ip += orig_ip - copy_ip; |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 814 | |
Masami Hiramatsu | 0b0122f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 815 | no_change: |
Roland McGrath | 1ecc798 | 2008-01-30 13:30:54 +0100 | [diff] [blame] | 816 | restore_btf(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | } |
| 818 | |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 819 | /* |
| 820 | * Interrupts are disabled on entry as trap1 is an interrupt gate and they |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 821 | * remain disabled throughout this function. |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 822 | */ |
| 823 | static int __kprobes post_kprobe_handler(struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | { |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 825 | struct kprobe *cur = kprobe_running(); |
| 826 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
| 827 | |
| 828 | if (!cur) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | return 0; |
| 830 | |
Yakov Lerner | acb5b8a | 2008-03-16 03:21:21 -0500 | [diff] [blame] | 831 | resume_execution(cur, regs, kcb); |
| 832 | regs->flags |= kcb->kprobe_saved_flags; |
Yakov Lerner | acb5b8a | 2008-03-16 03:21:21 -0500 | [diff] [blame] | 833 | |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 834 | if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) { |
| 835 | kcb->kprobe_status = KPROBE_HIT_SSDONE; |
| 836 | cur->post_handler(cur, regs, 0); |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 837 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 839 | /* Restore back the original saved kprobes variables and continue. */ |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 840 | if (kcb->kprobe_status == KPROBE_REENTER) { |
| 841 | restore_previous_kprobe(kcb); |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 842 | goto out; |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 843 | } |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 844 | reset_current_kprobe(); |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 845 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | preempt_enable_no_resched(); |
| 847 | |
| 848 | /* |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 849 | * if somebody else is singlestepping across a probe point, flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | * will have TF set, in which case, continue the remaining processing |
| 851 | * of do_debug, as if this is not a probe hit. |
| 852 | */ |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 853 | if (regs->flags & X86_EFLAGS_TF) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | return 0; |
| 855 | |
| 856 | return 1; |
| 857 | } |
| 858 | |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 859 | int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | { |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 861 | struct kprobe *cur = kprobe_running(); |
| 862 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
| 863 | |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 864 | switch (kcb->kprobe_status) { |
Prasanna S Panchamukhi | c28f896 | 2006-03-26 01:38:23 -0800 | [diff] [blame] | 865 | case KPROBE_HIT_SS: |
| 866 | case KPROBE_REENTER: |
| 867 | /* |
| 868 | * We are here because the instruction being single |
| 869 | * stepped caused a page fault. We reset the current |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 870 | * kprobe and the ip points back to the probe address |
Prasanna S Panchamukhi | c28f896 | 2006-03-26 01:38:23 -0800 | [diff] [blame] | 871 | * and allow the page fault handler to continue as a |
| 872 | * normal page fault. |
| 873 | */ |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 874 | regs->ip = (unsigned long)cur->addr; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 875 | regs->flags |= kcb->kprobe_old_flags; |
Prasanna S Panchamukhi | c28f896 | 2006-03-26 01:38:23 -0800 | [diff] [blame] | 876 | if (kcb->kprobe_status == KPROBE_REENTER) |
| 877 | restore_previous_kprobe(kcb); |
| 878 | else |
| 879 | reset_current_kprobe(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | preempt_enable_no_resched(); |
Prasanna S Panchamukhi | c28f896 | 2006-03-26 01:38:23 -0800 | [diff] [blame] | 881 | break; |
| 882 | case KPROBE_HIT_ACTIVE: |
| 883 | case KPROBE_HIT_SSDONE: |
| 884 | /* |
| 885 | * We increment the nmissed count for accounting, |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 886 | * we can also use npre/npostfault count for accounting |
Prasanna S Panchamukhi | c28f896 | 2006-03-26 01:38:23 -0800 | [diff] [blame] | 887 | * these specific fault cases. |
| 888 | */ |
| 889 | kprobes_inc_nmissed_count(cur); |
| 890 | |
| 891 | /* |
| 892 | * We come here because instructions in the pre/post |
| 893 | * handler caused the page_fault, this could happen |
| 894 | * if handler tries to access user space by |
| 895 | * copy_from_user(), get_user() etc. Let the |
| 896 | * user-specified handler try to fix it first. |
| 897 | */ |
| 898 | if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr)) |
| 899 | return 1; |
| 900 | |
| 901 | /* |
| 902 | * In case the user-specified fault handler returned |
| 903 | * zero, try to fix up. |
| 904 | */ |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 905 | if (fixup_exception(regs)) |
| 906 | return 1; |
Harvey Harrison | 6d48583 | 2008-01-30 13:31:41 +0100 | [diff] [blame] | 907 | |
Prasanna S Panchamukhi | c28f896 | 2006-03-26 01:38:23 -0800 | [diff] [blame] | 908 | /* |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 909 | * fixup routine could not handle it, |
Prasanna S Panchamukhi | c28f896 | 2006-03-26 01:38:23 -0800 | [diff] [blame] | 910 | * Let do_page_fault() fix it. |
| 911 | */ |
| 912 | break; |
| 913 | default: |
| 914 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | } |
| 916 | return 0; |
| 917 | } |
| 918 | |
| 919 | /* |
| 920 | * Wrapper routine for handling exceptions. |
| 921 | */ |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 922 | int __kprobes kprobe_exceptions_notify(struct notifier_block *self, |
| 923 | unsigned long val, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | { |
Jan Engelhardt | ade1af7 | 2008-01-30 13:33:23 +0100 | [diff] [blame] | 925 | struct die_args *args = data; |
Ananth N Mavinakayanahalli | 66ff2d06 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 926 | int ret = NOTIFY_DONE; |
| 927 | |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 928 | if (args->regs && user_mode_vm(args->regs)) |
bibo,mao | 2326c77 | 2006-03-26 01:38:21 -0800 | [diff] [blame] | 929 | return ret; |
| 930 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | switch (val) { |
| 932 | case DIE_INT3: |
| 933 | if (kprobe_handler(args->regs)) |
Ananth N Mavinakayanahalli | 66ff2d06 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 934 | ret = NOTIFY_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | break; |
| 936 | case DIE_DEBUG: |
K.Prasad | 62edab9 | 2009-06-01 23:47:06 +0530 | [diff] [blame] | 937 | if (post_kprobe_handler(args->regs)) { |
| 938 | /* |
| 939 | * Reset the BS bit in dr6 (pointed by args->err) to |
| 940 | * denote completion of processing |
| 941 | */ |
| 942 | (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP; |
Ananth N Mavinakayanahalli | 66ff2d06 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 943 | ret = NOTIFY_STOP; |
K.Prasad | 62edab9 | 2009-06-01 23:47:06 +0530 | [diff] [blame] | 944 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | break; |
| 946 | case DIE_GPF: |
Quentin Barnes | b506a9d | 2008-01-30 13:32:32 +0100 | [diff] [blame] | 947 | /* |
| 948 | * To be potentially processing a kprobe fault and to |
| 949 | * trust the result from kprobe_running(), we have |
| 950 | * be non-preemptible. |
| 951 | */ |
| 952 | if (!preemptible() && kprobe_running() && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | kprobe_fault_handler(args->regs, args->trapnr)) |
Ananth N Mavinakayanahalli | 66ff2d06 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 954 | ret = NOTIFY_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | break; |
| 956 | default: |
| 957 | break; |
| 958 | } |
Ananth N Mavinakayanahalli | 66ff2d06 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 959 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | } |
| 961 | |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 962 | int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | { |
| 964 | struct jprobe *jp = container_of(p, struct jprobe, kp); |
| 965 | unsigned long addr; |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 966 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 968 | kcb->jprobe_saved_regs = *regs; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 969 | kcb->jprobe_saved_sp = stack_addr(regs); |
| 970 | addr = (unsigned long)(kcb->jprobe_saved_sp); |
| 971 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | /* |
| 973 | * As Linus pointed out, gcc assumes that the callee |
| 974 | * owns the argument space and could overwrite it, e.g. |
| 975 | * tailcall optimization. So, to be absolutely safe |
| 976 | * we also save and restore enough stack bytes to cover |
| 977 | * the argument area. |
| 978 | */ |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 979 | memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr, |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 980 | MIN_STACK_SIZE(addr)); |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 981 | regs->flags &= ~X86_EFLAGS_IF; |
Peter Zijlstra | 58dfe88 | 2007-10-11 22:25:25 +0200 | [diff] [blame] | 982 | trace_hardirqs_off(); |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 983 | regs->ip = (unsigned long)(jp->entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | return 1; |
| 985 | } |
| 986 | |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 987 | void __kprobes jprobe_return(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | { |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 989 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
| 990 | |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 991 | asm volatile ( |
| 992 | #ifdef CONFIG_X86_64 |
| 993 | " xchg %%rbx,%%rsp \n" |
| 994 | #else |
| 995 | " xchgl %%ebx,%%esp \n" |
| 996 | #endif |
| 997 | " int3 \n" |
| 998 | " .globl jprobe_return_end\n" |
| 999 | " jprobe_return_end: \n" |
| 1000 | " nop \n"::"b" |
| 1001 | (kcb->jprobe_saved_sp):"memory"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 1004 | int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | { |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 1006 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 1007 | u8 *addr = (u8 *) (regs->ip - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | struct jprobe *jp = container_of(p, struct jprobe, kp); |
| 1009 | |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 1010 | if ((addr > (u8 *) jprobe_return) && |
| 1011 | (addr < (u8 *) jprobe_return_end)) { |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 1012 | if (stack_addr(regs) != kcb->jprobe_saved_sp) { |
Masami Hiramatsu | 29b6cd7 | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 1013 | struct pt_regs *saved_regs = &kcb->jprobe_saved_regs; |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 1014 | printk(KERN_ERR |
| 1015 | "current sp %p does not match saved sp %p\n", |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 1016 | stack_addr(regs), kcb->jprobe_saved_sp); |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 1017 | printk(KERN_ERR "Saved registers for jprobe %p\n", jp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | show_registers(saved_regs); |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 1019 | printk(KERN_ERR "Current registers\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | show_registers(regs); |
| 1021 | BUG(); |
| 1022 | } |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 1023 | *regs = kcb->jprobe_saved_regs; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 1024 | memcpy((kprobe_opcode_t *)(kcb->jprobe_saved_sp), |
| 1025 | kcb->jprobes_stack, |
| 1026 | MIN_STACK_SIZE(kcb->jprobe_saved_sp)); |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 1027 | preempt_enable_no_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | return 1; |
| 1029 | } |
| 1030 | return 0; |
| 1031 | } |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 1032 | |
Rusty Lynch | 6772926 | 2005-07-05 18:54:50 -0700 | [diff] [blame] | 1033 | int __init arch_init_kprobes(void) |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 1034 | { |
Masami Hiramatsu | da07ab0 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 1035 | return 0; |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 1036 | } |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 1037 | |
| 1038 | int __kprobes arch_trampoline_kprobe(struct kprobe *p) |
| 1039 | { |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 1040 | return 0; |
| 1041 | } |