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> |
Masami Hiramatsu | c0f7ac3 | 2010-02-25 08:34:46 -0500 | [diff] [blame] | 52 | #include <linux/ftrace.h> |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 53 | |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 54 | #include <asm/cacheflush.h> |
| 55 | #include <asm/desc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | #include <asm/pgtable.h> |
Prasanna S Panchamukhi | c28f896 | 2006-03-26 01:38:23 -0800 | [diff] [blame] | 57 | #include <asm/uaccess.h> |
Andi Kleen | 19d36cc | 2007-07-22 11:12:31 +0200 | [diff] [blame] | 58 | #include <asm/alternative.h> |
Masami Hiramatsu | b46b3d7 | 2009-08-13 16:34:28 -0400 | [diff] [blame] | 59 | #include <asm/insn.h> |
K.Prasad | 62edab9 | 2009-06-01 23:47:06 +0530 | [diff] [blame] | 60 | #include <asm/debugreg.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | void jprobe_return_end(void); |
| 63 | |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 64 | DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL; |
| 65 | DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
H. Peter Anvin | 98272ed | 2009-10-12 14:14:10 -0700 | [diff] [blame] | 67 | #define stack_addr(regs) ((unsigned long *)kernel_stack_pointer(regs)) |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 68 | |
| 69 | #define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\ |
| 70 | (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \ |
| 71 | (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \ |
| 72 | (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \ |
| 73 | (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \ |
| 74 | << (row % 32)) |
| 75 | /* |
| 76 | * Undefined/reserved opcodes, conditional jump, Opcode Extension |
| 77 | * Groups, and some special opcodes can not boost. |
| 78 | */ |
| 79 | static const u32 twobyte_is_boostable[256 / 32] = { |
| 80 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ |
| 81 | /* ---------------------------------------------- */ |
| 82 | W(0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0) | /* 00 */ |
| 83 | W(0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 10 */ |
| 84 | W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 20 */ |
| 85 | W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */ |
| 86 | W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */ |
| 87 | W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */ |
| 88 | W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1) | /* 60 */ |
| 89 | W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */ |
| 90 | W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 80 */ |
| 91 | W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */ |
| 92 | W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* a0 */ |
| 93 | W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) , /* b0 */ |
| 94 | W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */ |
| 95 | W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) , /* d0 */ |
| 96 | W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* e0 */ |
| 97 | W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0) /* f0 */ |
| 98 | /* ----------------------------------------------- */ |
| 99 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ |
| 100 | }; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 101 | #undef W |
| 102 | |
Masami Hiramatsu | f438d91 | 2007-10-16 01:27:49 -0700 | [diff] [blame] | 103 | struct kretprobe_blackpoint kretprobe_blacklist[] = { |
| 104 | {"__switch_to", }, /* This function switches only current task, but |
| 105 | doesn't switch kernel stack.*/ |
| 106 | {NULL, NULL} /* Terminator */ |
| 107 | }; |
| 108 | const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist); |
| 109 | |
Masami Hiramatsu | c0f7ac3 | 2010-02-25 08:34:46 -0500 | [diff] [blame] | 110 | static void __kprobes __synthesize_relative_insn(void *from, void *to, u8 op) |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 111 | { |
Masami Hiramatsu | c0f7ac3 | 2010-02-25 08:34:46 -0500 | [diff] [blame] | 112 | struct __arch_relative_insn { |
| 113 | u8 op; |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 114 | s32 raddr; |
Masami Hiramatsu | c0f7ac3 | 2010-02-25 08:34:46 -0500 | [diff] [blame] | 115 | } __attribute__((packed)) *insn; |
| 116 | |
| 117 | insn = (struct __arch_relative_insn *)from; |
| 118 | insn->raddr = (s32)((long)(to) - ((long)(from) + 5)); |
| 119 | insn->op = op; |
| 120 | } |
| 121 | |
| 122 | /* Insert a jump instruction at address 'from', which jumps to address 'to'.*/ |
| 123 | static void __kprobes synthesize_reljump(void *from, void *to) |
| 124 | { |
| 125 | __synthesize_relative_insn(from, to, RELATIVEJUMP_OPCODE); |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /* |
Harvey Harrison | 9930927 | 2008-01-30 13:32:14 +0100 | [diff] [blame] | 129 | * Check for the REX prefix which can only exist on X86_64 |
| 130 | * X86_32 always returns 0 |
| 131 | */ |
| 132 | static int __kprobes is_REX_prefix(kprobe_opcode_t *insn) |
| 133 | { |
| 134 | #ifdef CONFIG_X86_64 |
| 135 | if ((*insn & 0xf0) == 0x40) |
| 136 | return 1; |
| 137 | #endif |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | /* |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 142 | * Returns non-zero if opcode is boostable. |
| 143 | * RIP relative instructions are adjusted at copying time in 64 bits mode |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 144 | */ |
Harvey Harrison | e7b5e11 | 2008-01-30 13:31:43 +0100 | [diff] [blame] | 145 | static int __kprobes can_boost(kprobe_opcode_t *opcodes) |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 146 | { |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 147 | kprobe_opcode_t opcode; |
| 148 | kprobe_opcode_t *orig_opcodes = opcodes; |
| 149 | |
Jaswinder Singh Rajput | cde5edb | 2009-03-18 17:37:45 +0530 | [diff] [blame] | 150 | if (search_exception_tables((unsigned long)opcodes)) |
Masami Hiramatsu | 3039088 | 2009-03-16 18:57:22 -0400 | [diff] [blame] | 151 | return 0; /* Page fault may occur on this address. */ |
| 152 | |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 153 | retry: |
| 154 | if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1) |
| 155 | return 0; |
| 156 | opcode = *(opcodes++); |
| 157 | |
| 158 | /* 2nd-byte opcode */ |
| 159 | if (opcode == 0x0f) { |
| 160 | if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1) |
| 161 | return 0; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 162 | return test_bit(*opcodes, |
| 163 | (unsigned long *)twobyte_is_boostable); |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | switch (opcode & 0xf0) { |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 167 | #ifdef CONFIG_X86_64 |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 168 | case 0x40: |
| 169 | goto retry; /* REX prefix is boostable */ |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 170 | #endif |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 171 | case 0x60: |
| 172 | if (0x63 < opcode && opcode < 0x67) |
| 173 | goto retry; /* prefixes */ |
| 174 | /* can't boost Address-size override and bound */ |
| 175 | return (opcode != 0x62 && opcode != 0x67); |
| 176 | case 0x70: |
| 177 | return 0; /* can't boost conditional jump */ |
| 178 | case 0xc0: |
| 179 | /* can't boost software-interruptions */ |
| 180 | return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf; |
| 181 | case 0xd0: |
| 182 | /* can boost AA* and XLAT */ |
| 183 | return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7); |
| 184 | case 0xe0: |
| 185 | /* can boost in/out and absolute jmps */ |
| 186 | return ((opcode & 0x04) || opcode == 0xea); |
| 187 | case 0xf0: |
| 188 | if ((opcode & 0x0c) == 0 && opcode != 0xf1) |
| 189 | goto retry; /* lock/rep(ne) prefix */ |
| 190 | /* clear and set flags are boostable */ |
| 191 | return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe)); |
| 192 | default: |
| 193 | /* segment override prefixes are boostable */ |
| 194 | if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e) |
| 195 | goto retry; /* prefixes */ |
| 196 | /* CS override prefix and call are not boostable */ |
| 197 | return (opcode != 0x2e && opcode != 0x9a); |
| 198 | } |
| 199 | } |
| 200 | |
Masami Hiramatsu | b46b3d7 | 2009-08-13 16:34:28 -0400 | [diff] [blame] | 201 | /* Recover the probed instruction at addr for further analysis. */ |
| 202 | static int recover_probed_instruction(kprobe_opcode_t *buf, unsigned long addr) |
| 203 | { |
| 204 | struct kprobe *kp; |
| 205 | kp = get_kprobe((void *)addr); |
| 206 | if (!kp) |
| 207 | return -EINVAL; |
| 208 | |
| 209 | /* |
| 210 | * Basically, kp->ainsn.insn has an original instruction. |
| 211 | * However, RIP-relative instruction can not do single-stepping |
Masami Hiramatsu | c0f7ac3 | 2010-02-25 08:34:46 -0500 | [diff] [blame] | 212 | * at different place, __copy_instruction() tweaks the displacement of |
Masami Hiramatsu | b46b3d7 | 2009-08-13 16:34:28 -0400 | [diff] [blame] | 213 | * that instruction. In that case, we can't recover the instruction |
| 214 | * from the kp->ainsn.insn. |
| 215 | * |
| 216 | * On the other hand, kp->opcode has a copy of the first byte of |
| 217 | * the probed instruction, which is overwritten by int3. And |
| 218 | * the instruction at kp->addr is not modified by kprobes except |
| 219 | * for the first byte, we can recover the original instruction |
| 220 | * from it and kp->opcode. |
| 221 | */ |
| 222 | memcpy(buf, kp->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t)); |
| 223 | buf[0] = kp->opcode; |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | /* Dummy buffers for kallsyms_lookup */ |
| 228 | static char __dummy_buf[KSYM_NAME_LEN]; |
| 229 | |
| 230 | /* Check if paddr is at an instruction boundary */ |
| 231 | static int __kprobes can_probe(unsigned long paddr) |
| 232 | { |
| 233 | int ret; |
| 234 | unsigned long addr, offset = 0; |
| 235 | struct insn insn; |
| 236 | kprobe_opcode_t buf[MAX_INSN_SIZE]; |
| 237 | |
| 238 | if (!kallsyms_lookup(paddr, NULL, &offset, NULL, __dummy_buf)) |
| 239 | return 0; |
| 240 | |
| 241 | /* Decode instructions */ |
| 242 | addr = paddr - offset; |
| 243 | while (addr < paddr) { |
| 244 | kernel_insn_init(&insn, (void *)addr); |
| 245 | insn_get_opcode(&insn); |
| 246 | |
| 247 | /* |
| 248 | * Check if the instruction has been modified by another |
| 249 | * kprobe, in which case we replace the breakpoint by the |
| 250 | * original instruction in our buffer. |
| 251 | */ |
| 252 | if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION) { |
| 253 | ret = recover_probed_instruction(buf, addr); |
| 254 | if (ret) |
| 255 | /* |
| 256 | * Another debugging subsystem might insert |
| 257 | * this breakpoint. In that case, we can't |
| 258 | * recover it. |
| 259 | */ |
| 260 | return 0; |
| 261 | kernel_insn_init(&insn, buf); |
| 262 | } |
| 263 | insn_get_length(&insn); |
| 264 | addr += insn.length; |
| 265 | } |
| 266 | |
| 267 | return (addr == paddr); |
| 268 | } |
| 269 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | /* |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 271 | * Returns non-zero if opcode modifies the interrupt flag. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | */ |
Andrew Morton | 8645419 | 2007-11-26 20:42:19 +0100 | [diff] [blame] | 273 | static int __kprobes is_IF_modifier(kprobe_opcode_t *insn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | { |
| 275 | switch (*insn) { |
| 276 | case 0xfa: /* cli */ |
| 277 | case 0xfb: /* sti */ |
| 278 | case 0xcf: /* iret/iretd */ |
| 279 | case 0x9d: /* popf/popfd */ |
| 280 | return 1; |
| 281 | } |
Harvey Harrison | 9930927 | 2008-01-30 13:32:14 +0100 | [diff] [blame] | 282 | |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 283 | /* |
Harvey Harrison | 9930927 | 2008-01-30 13:32:14 +0100 | [diff] [blame] | 284 | * 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] | 285 | * at the next byte instead.. but of course not recurse infinitely |
| 286 | */ |
Harvey Harrison | 9930927 | 2008-01-30 13:32:14 +0100 | [diff] [blame] | 287 | if (is_REX_prefix(insn)) |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 288 | return is_IF_modifier(++insn); |
Harvey Harrison | 9930927 | 2008-01-30 13:32:14 +0100 | [diff] [blame] | 289 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | /* |
Masami Hiramatsu | c0f7ac3 | 2010-02-25 08:34:46 -0500 | [diff] [blame] | 294 | * Copy an instruction and adjust the displacement if the instruction |
| 295 | * uses the %rip-relative addressing mode. |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 296 | * If it does, Return the address of the 32-bit displacement word. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | * If not, return null. |
Harvey Harrison | 31f80e4 | 2008-01-30 13:32:16 +0100 | [diff] [blame] | 298 | * Only applicable to 64-bit x86. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | */ |
Masami Hiramatsu | c0f7ac3 | 2010-02-25 08:34:46 -0500 | [diff] [blame] | 300 | static int __kprobes __copy_instruction(u8 *dest, u8 *src, int recover) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | { |
Masami Hiramatsu | 89ae465 | 2009-08-13 16:34:36 -0400 | [diff] [blame] | 302 | struct insn insn; |
Masami Hiramatsu | c0f7ac3 | 2010-02-25 08:34:46 -0500 | [diff] [blame] | 303 | int ret; |
| 304 | kprobe_opcode_t buf[MAX_INSN_SIZE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | |
Masami Hiramatsu | c0f7ac3 | 2010-02-25 08:34:46 -0500 | [diff] [blame] | 306 | kernel_insn_init(&insn, src); |
| 307 | if (recover) { |
| 308 | insn_get_opcode(&insn); |
| 309 | if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION) { |
| 310 | ret = recover_probed_instruction(buf, |
| 311 | (unsigned long)src); |
| 312 | if (ret) |
| 313 | return 0; |
| 314 | kernel_insn_init(&insn, buf); |
| 315 | } |
| 316 | } |
| 317 | insn_get_length(&insn); |
| 318 | memcpy(dest, insn.kaddr, insn.length); |
| 319 | |
| 320 | #ifdef CONFIG_X86_64 |
Masami Hiramatsu | 89ae465 | 2009-08-13 16:34:36 -0400 | [diff] [blame] | 321 | if (insn_rip_relative(&insn)) { |
| 322 | s64 newdisp; |
| 323 | u8 *disp; |
Masami Hiramatsu | c0f7ac3 | 2010-02-25 08:34:46 -0500 | [diff] [blame] | 324 | kernel_insn_init(&insn, dest); |
Masami Hiramatsu | 89ae465 | 2009-08-13 16:34:36 -0400 | [diff] [blame] | 325 | insn_get_displacement(&insn); |
| 326 | /* |
| 327 | * The copied instruction uses the %rip-relative addressing |
| 328 | * mode. Adjust the displacement for the difference between |
| 329 | * the original location of this instruction and the location |
| 330 | * of the copy that will actually be run. The tricky bit here |
| 331 | * is making sure that the sign extension happens correctly in |
| 332 | * this calculation, since we need a signed 32-bit result to |
| 333 | * be sign-extended to 64 bits when it's added to the %rip |
| 334 | * value and yield the same 64-bit result that the sign- |
| 335 | * extension of the original signed 32-bit displacement would |
| 336 | * have given. |
| 337 | */ |
Masami Hiramatsu | c0f7ac3 | 2010-02-25 08:34:46 -0500 | [diff] [blame] | 338 | newdisp = (u8 *) src + (s64) insn.displacement.value - |
| 339 | (u8 *) dest; |
Masami Hiramatsu | 89ae465 | 2009-08-13 16:34:36 -0400 | [diff] [blame] | 340 | BUG_ON((s64) (s32) newdisp != newdisp); /* Sanity check. */ |
Masami Hiramatsu | c0f7ac3 | 2010-02-25 08:34:46 -0500 | [diff] [blame] | 341 | disp = (u8 *) dest + insn_offset_displacement(&insn); |
Masami Hiramatsu | 89ae465 | 2009-08-13 16:34:36 -0400 | [diff] [blame] | 342 | *(s32 *) disp = (s32) newdisp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | } |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 344 | #endif |
Masami Hiramatsu | c0f7ac3 | 2010-02-25 08:34:46 -0500 | [diff] [blame] | 345 | return insn.length; |
Harvey Harrison | 31f80e4 | 2008-01-30 13:32:16 +0100 | [diff] [blame] | 346 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
Keshavamurthy Anil S | f709b12 | 2006-01-09 20:52:44 -0800 | [diff] [blame] | 348 | static void __kprobes arch_copy_kprobe(struct kprobe *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | { |
Masami Hiramatsu | c0f7ac3 | 2010-02-25 08:34:46 -0500 | [diff] [blame] | 350 | /* |
| 351 | * Copy an instruction without recovering int3, because it will be |
| 352 | * put by another subsystem. |
| 353 | */ |
| 354 | __copy_instruction(p->ainsn.insn, p->addr, 0); |
Harvey Harrison | 31f80e4 | 2008-01-30 13:32:16 +0100 | [diff] [blame] | 355 | |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 356 | if (can_boost(p->addr)) |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 357 | p->ainsn.boostable = 0; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 358 | else |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 359 | p->ainsn.boostable = -1; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 360 | |
Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame] | 361 | p->opcode = *p->addr; |
| 362 | } |
| 363 | |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 364 | int __kprobes arch_prepare_kprobe(struct kprobe *p) |
| 365 | { |
Masami Hiramatsu | 4554dbc | 2010-02-02 16:49:18 -0500 | [diff] [blame] | 366 | if (alternatives_text_reserved(p->addr, p->addr)) |
| 367 | return -EINVAL; |
| 368 | |
Masami Hiramatsu | b46b3d7 | 2009-08-13 16:34:28 -0400 | [diff] [blame] | 369 | if (!can_probe((unsigned long)p->addr)) |
| 370 | return -EILSEQ; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 371 | /* insn: must be on special executable page on x86. */ |
| 372 | p->ainsn.insn = get_insn_slot(); |
| 373 | if (!p->ainsn.insn) |
| 374 | return -ENOMEM; |
| 375 | arch_copy_kprobe(p); |
| 376 | return 0; |
| 377 | } |
| 378 | |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 379 | void __kprobes arch_arm_kprobe(struct kprobe *p) |
Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame] | 380 | { |
Andi Kleen | 19d36cc | 2007-07-22 11:12:31 +0200 | [diff] [blame] | 381 | text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1); |
Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 384 | void __kprobes arch_disarm_kprobe(struct kprobe *p) |
Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame] | 385 | { |
Andi Kleen | 19d36cc | 2007-07-22 11:12:31 +0200 | [diff] [blame] | 386 | text_poke(p->addr, &p->opcode, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Ananth N Mavinakayanahalli | 0498b63 | 2006-01-09 20:52:46 -0800 | [diff] [blame] | 389 | void __kprobes arch_remove_kprobe(struct kprobe *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | { |
Masami Hiramatsu | 1294156 | 2009-01-06 14:41:50 -0800 | [diff] [blame] | 391 | if (p->ainsn.insn) { |
| 392 | free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1)); |
| 393 | p->ainsn.insn = NULL; |
| 394 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Prasanna S Panchamukhi | 3b60211 | 2006-04-18 22:22:00 -0700 | [diff] [blame] | 397 | static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb) |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 398 | { |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 399 | kcb->prev_kprobe.kp = kprobe_running(); |
| 400 | kcb->prev_kprobe.status = kcb->kprobe_status; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 401 | kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags; |
| 402 | kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags; |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Prasanna S Panchamukhi | 3b60211 | 2006-04-18 22:22:00 -0700 | [diff] [blame] | 405 | static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb) |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 406 | { |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 407 | __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp; |
| 408 | kcb->kprobe_status = kcb->prev_kprobe.status; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 409 | kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags; |
| 410 | kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags; |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Prasanna S Panchamukhi | 3b60211 | 2006-04-18 22:22:00 -0700 | [diff] [blame] | 413 | 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] | 414 | struct kprobe_ctlblk *kcb) |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 415 | { |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 416 | __get_cpu_var(current_kprobe) = p; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 417 | kcb->kprobe_saved_flags = kcb->kprobe_old_flags |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 418 | = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF)); |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 419 | if (is_IF_modifier(p->ainsn.insn)) |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 420 | kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF; |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Harvey Harrison | e7b5e11 | 2008-01-30 13:31:43 +0100 | [diff] [blame] | 423 | static void __kprobes clear_btf(void) |
Roland McGrath | 1ecc798 | 2008-01-30 13:30:54 +0100 | [diff] [blame] | 424 | { |
| 425 | if (test_thread_flag(TIF_DEBUGCTLMSR)) |
Jan Beulich | 5b0e508 | 2008-03-10 13:11:17 +0000 | [diff] [blame] | 426 | update_debugctlmsr(0); |
Roland McGrath | 1ecc798 | 2008-01-30 13:30:54 +0100 | [diff] [blame] | 427 | } |
| 428 | |
Harvey Harrison | e7b5e11 | 2008-01-30 13:31:43 +0100 | [diff] [blame] | 429 | static void __kprobes restore_btf(void) |
Roland McGrath | 1ecc798 | 2008-01-30 13:30:54 +0100 | [diff] [blame] | 430 | { |
| 431 | if (test_thread_flag(TIF_DEBUGCTLMSR)) |
Jan Beulich | 5b0e508 | 2008-03-10 13:11:17 +0000 | [diff] [blame] | 432 | update_debugctlmsr(current->thread.debugctlmsr); |
Roland McGrath | 1ecc798 | 2008-01-30 13:30:54 +0100 | [diff] [blame] | 433 | } |
| 434 | |
Christoph Hellwig | 4c4308c | 2007-05-08 00:34:14 -0700 | [diff] [blame] | 435 | void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri, |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 436 | struct pt_regs *regs) |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 437 | { |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 438 | unsigned long *sara = stack_addr(regs); |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 439 | |
Christoph Hellwig | 4c4308c | 2007-05-08 00:34:14 -0700 | [diff] [blame] | 440 | ri->ret_addr = (kprobe_opcode_t *) *sara; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 441 | |
Christoph Hellwig | 4c4308c | 2007-05-08 00:34:14 -0700 | [diff] [blame] | 442 | /* Replace the return addr with trampoline addr */ |
| 443 | *sara = (unsigned long) &kretprobe_trampoline; |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 444 | } |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 445 | |
Masami Hiramatsu | c0f7ac3 | 2010-02-25 08:34:46 -0500 | [diff] [blame] | 446 | #ifdef CONFIG_OPTPROBES |
| 447 | static int __kprobes setup_detour_execution(struct kprobe *p, |
| 448 | struct pt_regs *regs, |
| 449 | int reenter); |
| 450 | #else |
| 451 | #define setup_detour_execution(p, regs, reenter) (0) |
| 452 | #endif |
| 453 | |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 454 | static void __kprobes setup_singlestep(struct kprobe *p, struct pt_regs *regs, |
Masami Hiramatsu | 0f94eb6 | 2010-02-25 08:34:23 -0500 | [diff] [blame] | 455 | struct kprobe_ctlblk *kcb, int reenter) |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 456 | { |
Masami Hiramatsu | c0f7ac3 | 2010-02-25 08:34:46 -0500 | [diff] [blame] | 457 | if (setup_detour_execution(p, regs, reenter)) |
| 458 | return; |
| 459 | |
Masami Hiramatsu | 615d0eb | 2010-02-02 16:49:04 -0500 | [diff] [blame] | 460 | #if !defined(CONFIG_PREEMPT) |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 461 | if (p->ainsn.boostable == 1 && !p->post_handler) { |
| 462 | /* Boost up -- we can execute copied instructions directly */ |
Masami Hiramatsu | 0f94eb6 | 2010-02-25 08:34:23 -0500 | [diff] [blame] | 463 | if (!reenter) |
| 464 | reset_current_kprobe(); |
| 465 | /* |
| 466 | * Reentering boosted probe doesn't reset current_kprobe, |
| 467 | * nor set current_kprobe, because it doesn't use single |
| 468 | * stepping. |
| 469 | */ |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 470 | regs->ip = (unsigned long)p->ainsn.insn; |
| 471 | preempt_enable_no_resched(); |
| 472 | return; |
| 473 | } |
| 474 | #endif |
Masami Hiramatsu | 0f94eb6 | 2010-02-25 08:34:23 -0500 | [diff] [blame] | 475 | if (reenter) { |
| 476 | save_previous_kprobe(kcb); |
| 477 | set_current_kprobe(p, regs, kcb); |
| 478 | kcb->kprobe_status = KPROBE_REENTER; |
| 479 | } else |
| 480 | kcb->kprobe_status = KPROBE_HIT_SS; |
| 481 | /* Prepare real single stepping */ |
| 482 | clear_btf(); |
| 483 | regs->flags |= X86_EFLAGS_TF; |
| 484 | regs->flags &= ~X86_EFLAGS_IF; |
| 485 | /* single step inline if the instruction is an int3 */ |
| 486 | if (p->opcode == BREAKPOINT_INSTRUCTION) |
| 487 | regs->ip = (unsigned long)p->addr; |
| 488 | else |
| 489 | regs->ip = (unsigned long)p->ainsn.insn; |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 490 | } |
| 491 | |
Harvey Harrison | 40102d4 | 2008-01-30 13:32:02 +0100 | [diff] [blame] | 492 | /* |
| 493 | * We have reentered the kprobe_handler(), since another probe was hit while |
| 494 | * within the handler. We save the original kprobes variables and just single |
| 495 | * step on the instruction of the new probe without calling any user handlers. |
| 496 | */ |
Masami Hiramatsu | 59e87cd | 2008-01-30 13:32:02 +0100 | [diff] [blame] | 497 | static int __kprobes reenter_kprobe(struct kprobe *p, struct pt_regs *regs, |
| 498 | struct kprobe_ctlblk *kcb) |
Harvey Harrison | 40102d4 | 2008-01-30 13:32:02 +0100 | [diff] [blame] | 499 | { |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 500 | switch (kcb->kprobe_status) { |
| 501 | case KPROBE_HIT_SSDONE: |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 502 | case KPROBE_HIT_ACTIVE: |
Abhishek Sagar | fb8830e | 2008-01-30 13:33:13 +0100 | [diff] [blame] | 503 | kprobes_inc_nmissed_count(p); |
Masami Hiramatsu | 0f94eb6 | 2010-02-25 08:34:23 -0500 | [diff] [blame] | 504 | setup_singlestep(p, regs, kcb, 1); |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 505 | break; |
| 506 | case KPROBE_HIT_SS: |
Masami Hiramatsu | e9afe9e | 2009-08-27 13:22:58 -0400 | [diff] [blame] | 507 | /* A probe has been hit in the codepath leading up to, or just |
| 508 | * after, single-stepping of a probed instruction. This entire |
| 509 | * codepath should strictly reside in .kprobes.text section. |
| 510 | * Raise a BUG or we'll continue in an endless reentering loop |
| 511 | * and eventually a stack overflow. |
| 512 | */ |
| 513 | printk(KERN_WARNING "Unrecoverable kprobe detected at %p.\n", |
| 514 | p->addr); |
| 515 | dump_kprobe(p); |
| 516 | BUG(); |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 517 | default: |
| 518 | /* impossible cases */ |
| 519 | WARN_ON(1); |
Abhishek Sagar | fb8830e | 2008-01-30 13:33:13 +0100 | [diff] [blame] | 520 | return 0; |
Masami Hiramatsu | 59e87cd | 2008-01-30 13:32:02 +0100 | [diff] [blame] | 521 | } |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 522 | |
Masami Hiramatsu | 59e87cd | 2008-01-30 13:32:02 +0100 | [diff] [blame] | 523 | return 1; |
Harvey Harrison | 40102d4 | 2008-01-30 13:32:02 +0100 | [diff] [blame] | 524 | } |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 525 | |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 526 | /* |
| 527 | * 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] | 528 | * remain disabled throughout this function. |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 529 | */ |
| 530 | static int __kprobes kprobe_handler(struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | { |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 532 | kprobe_opcode_t *addr; |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 533 | struct kprobe *p; |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 534 | struct kprobe_ctlblk *kcb; |
| 535 | |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 536 | addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t)); |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 537 | if (*addr != BREAKPOINT_INSTRUCTION) { |
| 538 | /* |
| 539 | * The breakpoint instruction was removed right |
| 540 | * after we hit it. Another cpu has removed |
| 541 | * either a probepoint or a debugger breakpoint |
| 542 | * at this address. In either case, no further |
| 543 | * handling of this interrupt is appropriate. |
| 544 | * Back up over the (now missing) int3 and run |
| 545 | * the original instruction. |
| 546 | */ |
| 547 | regs->ip = (unsigned long)addr; |
| 548 | return 1; |
| 549 | } |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 550 | |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 551 | /* |
| 552 | * We don't want to be preempted for the entire |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 553 | * duration of kprobe processing. We conditionally |
| 554 | * re-enable preemption at the end of this function, |
| 555 | * and also in reenter_kprobe() and setup_singlestep(). |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 556 | */ |
| 557 | preempt_disable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 559 | kcb = get_kprobe_ctlblk(); |
Harvey Harrison | b976015 | 2008-01-30 13:32:19 +0100 | [diff] [blame] | 560 | p = get_kprobe(addr); |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 561 | |
Harvey Harrison | b976015 | 2008-01-30 13:32:19 +0100 | [diff] [blame] | 562 | if (p) { |
Harvey Harrison | b976015 | 2008-01-30 13:32:19 +0100 | [diff] [blame] | 563 | if (kprobe_running()) { |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 564 | if (reenter_kprobe(p, regs, kcb)) |
| 565 | return 1; |
Harvey Harrison | b976015 | 2008-01-30 13:32:19 +0100 | [diff] [blame] | 566 | } else { |
| 567 | set_current_kprobe(p, regs, kcb); |
| 568 | kcb->kprobe_status = KPROBE_HIT_ACTIVE; |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 569 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | /* |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 571 | * If we have no pre-handler or it returned 0, we |
| 572 | * continue with normal processing. If we have a |
| 573 | * pre-handler and it returned non-zero, it prepped |
| 574 | * for calling the break_handler below on re-entry |
| 575 | * for jprobe processing, so get out doing nothing |
| 576 | * more here. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | */ |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 578 | if (!p->pre_handler || !p->pre_handler(p, regs)) |
Masami Hiramatsu | 0f94eb6 | 2010-02-25 08:34:23 -0500 | [diff] [blame] | 579 | setup_singlestep(p, regs, kcb, 0); |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 580 | return 1; |
Harvey Harrison | b976015 | 2008-01-30 13:32:19 +0100 | [diff] [blame] | 581 | } |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 582 | } else if (kprobe_running()) { |
| 583 | p = __get_cpu_var(current_kprobe); |
| 584 | if (p->break_handler && p->break_handler(p, regs)) { |
Masami Hiramatsu | 0f94eb6 | 2010-02-25 08:34:23 -0500 | [diff] [blame] | 585 | setup_singlestep(p, regs, kcb, 0); |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 586 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | } |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 588 | } /* else: not a kprobe fault; let the kernel handle it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 590 | preempt_enable_no_resched(); |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 591 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Masami Hiramatsu | f007ea2 | 2010-02-25 08:34:30 -0500 | [diff] [blame] | 594 | #ifdef CONFIG_X86_64 |
| 595 | #define SAVE_REGS_STRING \ |
| 596 | /* Skip cs, ip, orig_ax. */ \ |
| 597 | " subq $24, %rsp\n" \ |
| 598 | " pushq %rdi\n" \ |
| 599 | " pushq %rsi\n" \ |
| 600 | " pushq %rdx\n" \ |
| 601 | " pushq %rcx\n" \ |
| 602 | " pushq %rax\n" \ |
| 603 | " pushq %r8\n" \ |
| 604 | " pushq %r9\n" \ |
| 605 | " pushq %r10\n" \ |
| 606 | " pushq %r11\n" \ |
| 607 | " pushq %rbx\n" \ |
| 608 | " pushq %rbp\n" \ |
| 609 | " pushq %r12\n" \ |
| 610 | " pushq %r13\n" \ |
| 611 | " pushq %r14\n" \ |
| 612 | " pushq %r15\n" |
| 613 | #define RESTORE_REGS_STRING \ |
| 614 | " popq %r15\n" \ |
| 615 | " popq %r14\n" \ |
| 616 | " popq %r13\n" \ |
| 617 | " popq %r12\n" \ |
| 618 | " popq %rbp\n" \ |
| 619 | " popq %rbx\n" \ |
| 620 | " popq %r11\n" \ |
| 621 | " popq %r10\n" \ |
| 622 | " popq %r9\n" \ |
| 623 | " popq %r8\n" \ |
| 624 | " popq %rax\n" \ |
| 625 | " popq %rcx\n" \ |
| 626 | " popq %rdx\n" \ |
| 627 | " popq %rsi\n" \ |
| 628 | " popq %rdi\n" \ |
| 629 | /* Skip orig_ax, ip, cs */ \ |
| 630 | " addq $24, %rsp\n" |
| 631 | #else |
| 632 | #define SAVE_REGS_STRING \ |
| 633 | /* Skip cs, ip, orig_ax and gs. */ \ |
| 634 | " subl $16, %esp\n" \ |
| 635 | " pushl %fs\n" \ |
| 636 | " pushl %ds\n" \ |
| 637 | " pushl %es\n" \ |
| 638 | " pushl %eax\n" \ |
| 639 | " pushl %ebp\n" \ |
| 640 | " pushl %edi\n" \ |
| 641 | " pushl %esi\n" \ |
| 642 | " pushl %edx\n" \ |
| 643 | " pushl %ecx\n" \ |
| 644 | " pushl %ebx\n" |
| 645 | #define RESTORE_REGS_STRING \ |
| 646 | " popl %ebx\n" \ |
| 647 | " popl %ecx\n" \ |
| 648 | " popl %edx\n" \ |
| 649 | " popl %esi\n" \ |
| 650 | " popl %edi\n" \ |
| 651 | " popl %ebp\n" \ |
| 652 | " popl %eax\n" \ |
| 653 | /* Skip ds, es, fs, gs, orig_ax, and ip. Note: don't pop cs here*/\ |
| 654 | " addl $24, %esp\n" |
| 655 | #endif |
| 656 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | /* |
Masami Hiramatsu | da07ab0 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 658 | * When a retprobed function returns, this code saves registers and |
| 659 | * calls trampoline_handler() runs, which calls the kretprobe's handler. |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 660 | */ |
Harvey Harrison | f1452d4 | 2008-02-14 15:23:53 -0800 | [diff] [blame] | 661 | static void __used __kprobes kretprobe_trampoline_holder(void) |
Harvey Harrison | 1017579 | 2008-01-30 13:33:01 +0100 | [diff] [blame] | 662 | { |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 663 | asm volatile ( |
| 664 | ".global kretprobe_trampoline\n" |
Masami Hiramatsu | da07ab0 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 665 | "kretprobe_trampoline: \n" |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 666 | #ifdef CONFIG_X86_64 |
Masami Hiramatsu | da07ab0 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 667 | /* We don't bother saving the ss register */ |
| 668 | " pushq %rsp\n" |
| 669 | " pushfq\n" |
Masami Hiramatsu | f007ea2 | 2010-02-25 08:34:30 -0500 | [diff] [blame] | 670 | SAVE_REGS_STRING |
Masami Hiramatsu | da07ab0 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 671 | " movq %rsp, %rdi\n" |
| 672 | " call trampoline_handler\n" |
| 673 | /* Replace saved sp with true return address. */ |
| 674 | " movq %rax, 152(%rsp)\n" |
Masami Hiramatsu | f007ea2 | 2010-02-25 08:34:30 -0500 | [diff] [blame] | 675 | RESTORE_REGS_STRING |
Masami Hiramatsu | da07ab0 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 676 | " popfq\n" |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 677 | #else |
| 678 | " pushf\n" |
Masami Hiramatsu | f007ea2 | 2010-02-25 08:34:30 -0500 | [diff] [blame] | 679 | SAVE_REGS_STRING |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 680 | " movl %esp, %eax\n" |
| 681 | " call trampoline_handler\n" |
| 682 | /* Move flags to cs */ |
Masami Hiramatsu | fee039a | 2009-03-23 10:14:52 -0400 | [diff] [blame] | 683 | " movl 56(%esp), %edx\n" |
| 684 | " movl %edx, 52(%esp)\n" |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 685 | /* Replace saved flags with true return address. */ |
Masami Hiramatsu | fee039a | 2009-03-23 10:14:52 -0400 | [diff] [blame] | 686 | " movl %eax, 56(%esp)\n" |
Masami Hiramatsu | f007ea2 | 2010-02-25 08:34:30 -0500 | [diff] [blame] | 687 | RESTORE_REGS_STRING |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 688 | " popf\n" |
| 689 | #endif |
Masami Hiramatsu | da07ab0 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 690 | " ret\n"); |
Harvey Harrison | 1017579 | 2008-01-30 13:33:01 +0100 | [diff] [blame] | 691 | } |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 692 | |
| 693 | /* |
Masami Hiramatsu | da07ab0 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 694 | * Called from kretprobe_trampoline |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 695 | */ |
Harvey Harrison | f1452d4 | 2008-02-14 15:23:53 -0800 | [diff] [blame] | 696 | static __used __kprobes void *trampoline_handler(struct pt_regs *regs) |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 697 | { |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 698 | struct kretprobe_instance *ri = NULL; |
bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 699 | struct hlist_head *head, empty_rp; |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 700 | struct hlist_node *node, *tmp; |
Ananth N Mavinakayanahalli | 991a51d | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 701 | unsigned long flags, orig_ret_address = 0; |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 702 | unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline; |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 703 | |
bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 704 | INIT_HLIST_HEAD(&empty_rp); |
Srinivasa D S | ef53d9c | 2008-07-25 01:46:04 -0700 | [diff] [blame] | 705 | kretprobe_hash_lock(current, &head, &flags); |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 706 | /* fixup registers */ |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 707 | #ifdef CONFIG_X86_64 |
Masami Hiramatsu | da07ab0 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 708 | regs->cs = __KERNEL_CS; |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 709 | #else |
| 710 | regs->cs = __KERNEL_CS | get_kernel_rpl(); |
Masami Hiramatsu | fee039a | 2009-03-23 10:14:52 -0400 | [diff] [blame] | 711 | regs->gs = 0; |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 712 | #endif |
Masami Hiramatsu | da07ab0 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 713 | regs->ip = trampoline_address; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 714 | regs->orig_ax = ~0UL; |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 715 | |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 716 | /* |
| 717 | * It is possible to have multiple instances associated with a given |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 718 | * task either because multiple functions in the call path have |
Frederik Schwarzer | 025dfda | 2008-10-16 19:02:37 +0200 | [diff] [blame] | 719 | * return probes installed on them, and/or more than one |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 720 | * return probe was registered for a target function. |
| 721 | * |
| 722 | * We can handle this because: |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 723 | * - instances are always pushed into the head of the list |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 724 | * - when multiple return probes are registered for the same |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 725 | * function, the (chronologically) first instance's ret_addr |
| 726 | * will be the real return address, and all the rest will |
| 727 | * point to kretprobe_trampoline. |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 728 | */ |
| 729 | hlist_for_each_entry_safe(ri, node, tmp, head, hlist) { |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 730 | if (ri->task != current) |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 731 | /* another task is sharing our hash bucket */ |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 732 | continue; |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 733 | |
Masami Hiramatsu | da07ab0 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 734 | if (ri->rp && ri->rp->handler) { |
| 735 | __get_cpu_var(current_kprobe) = &ri->rp->kp; |
| 736 | get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE; |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 737 | ri->rp->handler(ri, regs); |
Masami Hiramatsu | da07ab0 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 738 | __get_cpu_var(current_kprobe) = NULL; |
| 739 | } |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 740 | |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 741 | orig_ret_address = (unsigned long)ri->ret_addr; |
bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 742 | recycle_rp_inst(ri, &empty_rp); |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 743 | |
| 744 | if (orig_ret_address != trampoline_address) |
| 745 | /* |
| 746 | * This is the real return address. Any other |
| 747 | * instances associated with this task are for |
| 748 | * other calls deeper on the call stack |
| 749 | */ |
| 750 | break; |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 751 | } |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 752 | |
Ananth N Mavinakayanahalli | 0f95b7f | 2007-05-08 00:28:27 -0700 | [diff] [blame] | 753 | kretprobe_assert(ri, orig_ret_address, trampoline_address); |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 754 | |
Srinivasa D S | ef53d9c | 2008-07-25 01:46:04 -0700 | [diff] [blame] | 755 | kretprobe_hash_unlock(current, &flags); |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 756 | |
bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 757 | hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) { |
| 758 | hlist_del(&ri->hlist); |
| 759 | kfree(ri); |
| 760 | } |
Masami Hiramatsu | da07ab0 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 761 | return (void *)orig_ret_address; |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | * Called after single-stepping. p->addr is the address of the |
| 766 | * instruction whose first byte has been replaced by the "int 3" |
| 767 | * instruction. To avoid the SMP problems that can occur when we |
| 768 | * temporarily put back the original opcode to single-step, we |
| 769 | * single-stepped a copy of the instruction. The address of this |
| 770 | * copy is p->ainsn.insn. |
| 771 | * |
| 772 | * This function prepares to return from the post-single-step |
| 773 | * interrupt. We have to fix up the stack as follows: |
| 774 | * |
| 775 | * 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] | 776 | * 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] | 777 | * it relative to the original instruction. |
| 778 | * |
| 779 | * 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] | 780 | * 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] | 781 | * |
| 782 | * 2) If the single-stepped instruction was a call, the return address |
| 783 | * that is atop the stack is the address following the copied instruction. |
| 784 | * We need to make it the address following the original instruction. |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 785 | * |
| 786 | * If this is the first time we've single-stepped the instruction at |
| 787 | * this probepoint, and the instruction is boostable, boost it: add a |
| 788 | * jump instruction after the copied instruction, that jumps to the next |
| 789 | * instruction after the probepoint. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | */ |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 791 | static void __kprobes resume_execution(struct kprobe *p, |
| 792 | struct pt_regs *regs, struct kprobe_ctlblk *kcb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | { |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 794 | unsigned long *tos = stack_addr(regs); |
| 795 | unsigned long copy_ip = (unsigned long)p->ainsn.insn; |
| 796 | unsigned long orig_ip = (unsigned long)p->addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | kprobe_opcode_t *insn = p->ainsn.insn; |
| 798 | |
| 799 | /*skip the REX prefix*/ |
Harvey Harrison | 9930927 | 2008-01-30 13:32:14 +0100 | [diff] [blame] | 800 | if (is_REX_prefix(insn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | insn++; |
| 802 | |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 803 | regs->flags &= ~X86_EFLAGS_TF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | switch (*insn) { |
Masami Hiramatsu | 0b0122f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 805 | case 0x9c: /* pushfl */ |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 806 | *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF); |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 807 | *tos |= kcb->kprobe_old_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | break; |
Masami Hiramatsu | 0b0122f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 809 | case 0xc2: /* iret/ret/lret */ |
| 810 | case 0xc3: |
Prasanna S Panchamukhi | 0b9e2ca | 2005-05-05 16:15:40 -0700 | [diff] [blame] | 811 | case 0xca: |
Masami Hiramatsu | 0b0122f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 812 | case 0xcb: |
| 813 | case 0xcf: |
| 814 | case 0xea: /* jmp absolute -- ip is correct */ |
| 815 | /* ip is already adjusted, no more changes required */ |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 816 | p->ainsn.boostable = 1; |
Masami Hiramatsu | 0b0122f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 817 | goto no_change; |
| 818 | case 0xe8: /* call relative - Fix return addr */ |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 819 | *tos = orig_ip + (*tos - copy_ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | break; |
Harvey Harrison | e7b5e11 | 2008-01-30 13:31:43 +0100 | [diff] [blame] | 821 | #ifdef CONFIG_X86_32 |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 822 | case 0x9a: /* call absolute -- same as call absolute, indirect */ |
| 823 | *tos = orig_ip + (*tos - copy_ip); |
| 824 | goto no_change; |
| 825 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | case 0xff: |
Satoshi Oshima | dc49e34 | 2006-05-20 15:00:21 -0700 | [diff] [blame] | 827 | if ((insn[1] & 0x30) == 0x10) { |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 828 | /* |
| 829 | * call absolute, indirect |
| 830 | * Fix return addr; ip is correct. |
| 831 | * But this is not boostable |
| 832 | */ |
| 833 | *tos = orig_ip + (*tos - copy_ip); |
Masami Hiramatsu | 0b0122f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 834 | goto no_change; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 835 | } else if (((insn[1] & 0x31) == 0x20) || |
| 836 | ((insn[1] & 0x31) == 0x21)) { |
| 837 | /* |
| 838 | * jmp near and far, absolute indirect |
| 839 | * ip is correct. And this is boostable |
| 840 | */ |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 841 | p->ainsn.boostable = 1; |
Masami Hiramatsu | 0b0122f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 842 | goto no_change; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | default: |
| 845 | break; |
| 846 | } |
| 847 | |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 848 | if (p->ainsn.boostable == 0) { |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 849 | if ((regs->ip > copy_ip) && |
| 850 | (regs->ip - copy_ip) + 5 < MAX_INSN_SIZE) { |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 851 | /* |
| 852 | * These instructions can be executed directly if it |
| 853 | * jumps back to correct address. |
| 854 | */ |
Masami Hiramatsu | c0f7ac3 | 2010-02-25 08:34:46 -0500 | [diff] [blame] | 855 | synthesize_reljump((void *)regs->ip, |
| 856 | (void *)orig_ip + (regs->ip - copy_ip)); |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 857 | p->ainsn.boostable = 1; |
| 858 | } else { |
| 859 | p->ainsn.boostable = -1; |
| 860 | } |
| 861 | } |
| 862 | |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 863 | regs->ip += orig_ip - copy_ip; |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 864 | |
Masami Hiramatsu | 0b0122f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 865 | no_change: |
Roland McGrath | 1ecc798 | 2008-01-30 13:30:54 +0100 | [diff] [blame] | 866 | restore_btf(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | } |
| 868 | |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 869 | /* |
| 870 | * 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] | 871 | * remain disabled throughout this function. |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 872 | */ |
| 873 | static int __kprobes post_kprobe_handler(struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | { |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 875 | struct kprobe *cur = kprobe_running(); |
| 876 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
| 877 | |
| 878 | if (!cur) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | return 0; |
| 880 | |
Yakov Lerner | acb5b8a | 2008-03-16 03:21:21 -0500 | [diff] [blame] | 881 | resume_execution(cur, regs, kcb); |
| 882 | regs->flags |= kcb->kprobe_saved_flags; |
Yakov Lerner | acb5b8a | 2008-03-16 03:21:21 -0500 | [diff] [blame] | 883 | |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 884 | if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) { |
| 885 | kcb->kprobe_status = KPROBE_HIT_SSDONE; |
| 886 | cur->post_handler(cur, regs, 0); |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 887 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 889 | /* Restore back the original saved kprobes variables and continue. */ |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 890 | if (kcb->kprobe_status == KPROBE_REENTER) { |
| 891 | restore_previous_kprobe(kcb); |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 892 | goto out; |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 893 | } |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 894 | reset_current_kprobe(); |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 895 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | preempt_enable_no_resched(); |
| 897 | |
| 898 | /* |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 899 | * if somebody else is singlestepping across a probe point, flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | * will have TF set, in which case, continue the remaining processing |
| 901 | * of do_debug, as if this is not a probe hit. |
| 902 | */ |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 903 | if (regs->flags & X86_EFLAGS_TF) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | return 0; |
| 905 | |
| 906 | return 1; |
| 907 | } |
| 908 | |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 909 | int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | { |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 911 | struct kprobe *cur = kprobe_running(); |
| 912 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
| 913 | |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 914 | switch (kcb->kprobe_status) { |
Prasanna S Panchamukhi | c28f896 | 2006-03-26 01:38:23 -0800 | [diff] [blame] | 915 | case KPROBE_HIT_SS: |
| 916 | case KPROBE_REENTER: |
| 917 | /* |
| 918 | * We are here because the instruction being single |
| 919 | * stepped caused a page fault. We reset the current |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 920 | * kprobe and the ip points back to the probe address |
Prasanna S Panchamukhi | c28f896 | 2006-03-26 01:38:23 -0800 | [diff] [blame] | 921 | * and allow the page fault handler to continue as a |
| 922 | * normal page fault. |
| 923 | */ |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 924 | regs->ip = (unsigned long)cur->addr; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 925 | regs->flags |= kcb->kprobe_old_flags; |
Prasanna S Panchamukhi | c28f896 | 2006-03-26 01:38:23 -0800 | [diff] [blame] | 926 | if (kcb->kprobe_status == KPROBE_REENTER) |
| 927 | restore_previous_kprobe(kcb); |
| 928 | else |
| 929 | reset_current_kprobe(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | preempt_enable_no_resched(); |
Prasanna S Panchamukhi | c28f896 | 2006-03-26 01:38:23 -0800 | [diff] [blame] | 931 | break; |
| 932 | case KPROBE_HIT_ACTIVE: |
| 933 | case KPROBE_HIT_SSDONE: |
| 934 | /* |
| 935 | * We increment the nmissed count for accounting, |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 936 | * we can also use npre/npostfault count for accounting |
Prasanna S Panchamukhi | c28f896 | 2006-03-26 01:38:23 -0800 | [diff] [blame] | 937 | * these specific fault cases. |
| 938 | */ |
| 939 | kprobes_inc_nmissed_count(cur); |
| 940 | |
| 941 | /* |
| 942 | * We come here because instructions in the pre/post |
| 943 | * handler caused the page_fault, this could happen |
| 944 | * if handler tries to access user space by |
| 945 | * copy_from_user(), get_user() etc. Let the |
| 946 | * user-specified handler try to fix it first. |
| 947 | */ |
| 948 | if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr)) |
| 949 | return 1; |
| 950 | |
| 951 | /* |
| 952 | * In case the user-specified fault handler returned |
| 953 | * zero, try to fix up. |
| 954 | */ |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 955 | if (fixup_exception(regs)) |
| 956 | return 1; |
Harvey Harrison | 6d48583 | 2008-01-30 13:31:41 +0100 | [diff] [blame] | 957 | |
Prasanna S Panchamukhi | c28f896 | 2006-03-26 01:38:23 -0800 | [diff] [blame] | 958 | /* |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 959 | * fixup routine could not handle it, |
Prasanna S Panchamukhi | c28f896 | 2006-03-26 01:38:23 -0800 | [diff] [blame] | 960 | * Let do_page_fault() fix it. |
| 961 | */ |
| 962 | break; |
| 963 | default: |
| 964 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | } |
| 966 | return 0; |
| 967 | } |
| 968 | |
| 969 | /* |
| 970 | * Wrapper routine for handling exceptions. |
| 971 | */ |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 972 | int __kprobes kprobe_exceptions_notify(struct notifier_block *self, |
| 973 | unsigned long val, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | { |
Jan Engelhardt | ade1af7 | 2008-01-30 13:33:23 +0100 | [diff] [blame] | 975 | struct die_args *args = data; |
Ananth N Mavinakayanahalli | 66ff2d0 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 976 | int ret = NOTIFY_DONE; |
| 977 | |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 978 | if (args->regs && user_mode_vm(args->regs)) |
bibo,mao | 2326c77 | 2006-03-26 01:38:21 -0800 | [diff] [blame] | 979 | return ret; |
| 980 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | switch (val) { |
| 982 | case DIE_INT3: |
| 983 | if (kprobe_handler(args->regs)) |
Ananth N Mavinakayanahalli | 66ff2d0 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 984 | ret = NOTIFY_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | break; |
| 986 | case DIE_DEBUG: |
K.Prasad | 62edab9 | 2009-06-01 23:47:06 +0530 | [diff] [blame] | 987 | if (post_kprobe_handler(args->regs)) { |
| 988 | /* |
| 989 | * Reset the BS bit in dr6 (pointed by args->err) to |
| 990 | * denote completion of processing |
| 991 | */ |
| 992 | (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP; |
Ananth N Mavinakayanahalli | 66ff2d0 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 993 | ret = NOTIFY_STOP; |
K.Prasad | 62edab9 | 2009-06-01 23:47:06 +0530 | [diff] [blame] | 994 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | break; |
| 996 | case DIE_GPF: |
Quentin Barnes | b506a9d | 2008-01-30 13:32:32 +0100 | [diff] [blame] | 997 | /* |
| 998 | * To be potentially processing a kprobe fault and to |
| 999 | * trust the result from kprobe_running(), we have |
| 1000 | * be non-preemptible. |
| 1001 | */ |
| 1002 | if (!preemptible() && kprobe_running() && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | kprobe_fault_handler(args->regs, args->trapnr)) |
Ananth N Mavinakayanahalli | 66ff2d0 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 1004 | ret = NOTIFY_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | break; |
| 1006 | default: |
| 1007 | break; |
| 1008 | } |
Ananth N Mavinakayanahalli | 66ff2d0 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 1009 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | } |
| 1011 | |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 1012 | int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | { |
| 1014 | struct jprobe *jp = container_of(p, struct jprobe, kp); |
| 1015 | unsigned long addr; |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 1016 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 1018 | kcb->jprobe_saved_regs = *regs; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 1019 | kcb->jprobe_saved_sp = stack_addr(regs); |
| 1020 | addr = (unsigned long)(kcb->jprobe_saved_sp); |
| 1021 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | /* |
| 1023 | * As Linus pointed out, gcc assumes that the callee |
| 1024 | * owns the argument space and could overwrite it, e.g. |
| 1025 | * tailcall optimization. So, to be absolutely safe |
| 1026 | * we also save and restore enough stack bytes to cover |
| 1027 | * the argument area. |
| 1028 | */ |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 1029 | memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr, |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 1030 | MIN_STACK_SIZE(addr)); |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 1031 | regs->flags &= ~X86_EFLAGS_IF; |
Peter Zijlstra | 58dfe88 | 2007-10-11 22:25:25 +0200 | [diff] [blame] | 1032 | trace_hardirqs_off(); |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 1033 | regs->ip = (unsigned long)(jp->entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | return 1; |
| 1035 | } |
| 1036 | |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 1037 | void __kprobes jprobe_return(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | { |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 1039 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
| 1040 | |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 1041 | asm volatile ( |
| 1042 | #ifdef CONFIG_X86_64 |
| 1043 | " xchg %%rbx,%%rsp \n" |
| 1044 | #else |
| 1045 | " xchgl %%ebx,%%esp \n" |
| 1046 | #endif |
| 1047 | " int3 \n" |
| 1048 | " .globl jprobe_return_end\n" |
| 1049 | " jprobe_return_end: \n" |
| 1050 | " nop \n"::"b" |
| 1051 | (kcb->jprobe_saved_sp):"memory"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | } |
| 1053 | |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 1054 | int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | { |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 1056 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 1057 | u8 *addr = (u8 *) (regs->ip - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | struct jprobe *jp = container_of(p, struct jprobe, kp); |
| 1059 | |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 1060 | if ((addr > (u8 *) jprobe_return) && |
| 1061 | (addr < (u8 *) jprobe_return_end)) { |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 1062 | if (stack_addr(regs) != kcb->jprobe_saved_sp) { |
Masami Hiramatsu | 29b6cd7 | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 1063 | struct pt_regs *saved_regs = &kcb->jprobe_saved_regs; |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 1064 | printk(KERN_ERR |
| 1065 | "current sp %p does not match saved sp %p\n", |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 1066 | stack_addr(regs), kcb->jprobe_saved_sp); |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 1067 | printk(KERN_ERR "Saved registers for jprobe %p\n", jp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | show_registers(saved_regs); |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 1069 | printk(KERN_ERR "Current registers\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | show_registers(regs); |
| 1071 | BUG(); |
| 1072 | } |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 1073 | *regs = kcb->jprobe_saved_regs; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 1074 | memcpy((kprobe_opcode_t *)(kcb->jprobe_saved_sp), |
| 1075 | kcb->jprobes_stack, |
| 1076 | MIN_STACK_SIZE(kcb->jprobe_saved_sp)); |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 1077 | preempt_enable_no_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | return 1; |
| 1079 | } |
| 1080 | return 0; |
| 1081 | } |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 1082 | |
Masami Hiramatsu | c0f7ac3 | 2010-02-25 08:34:46 -0500 | [diff] [blame] | 1083 | |
| 1084 | #ifdef CONFIG_OPTPROBES |
| 1085 | |
| 1086 | /* Insert a call instruction at address 'from', which calls address 'to'.*/ |
| 1087 | static void __kprobes synthesize_relcall(void *from, void *to) |
| 1088 | { |
| 1089 | __synthesize_relative_insn(from, to, RELATIVECALL_OPCODE); |
| 1090 | } |
| 1091 | |
| 1092 | /* Insert a move instruction which sets a pointer to eax/rdi (1st arg). */ |
| 1093 | static void __kprobes synthesize_set_arg1(kprobe_opcode_t *addr, |
| 1094 | unsigned long val) |
| 1095 | { |
| 1096 | #ifdef CONFIG_X86_64 |
| 1097 | *addr++ = 0x48; |
| 1098 | *addr++ = 0xbf; |
| 1099 | #else |
| 1100 | *addr++ = 0xb8; |
| 1101 | #endif |
| 1102 | *(unsigned long *)addr = val; |
| 1103 | } |
| 1104 | |
| 1105 | void __kprobes kprobes_optinsn_template_holder(void) |
| 1106 | { |
| 1107 | asm volatile ( |
| 1108 | ".global optprobe_template_entry\n" |
| 1109 | "optprobe_template_entry: \n" |
| 1110 | #ifdef CONFIG_X86_64 |
| 1111 | /* We don't bother saving the ss register */ |
| 1112 | " pushq %rsp\n" |
| 1113 | " pushfq\n" |
| 1114 | SAVE_REGS_STRING |
| 1115 | " movq %rsp, %rsi\n" |
| 1116 | ".global optprobe_template_val\n" |
| 1117 | "optprobe_template_val: \n" |
| 1118 | ASM_NOP5 |
| 1119 | ASM_NOP5 |
| 1120 | ".global optprobe_template_call\n" |
| 1121 | "optprobe_template_call: \n" |
| 1122 | ASM_NOP5 |
| 1123 | /* Move flags to rsp */ |
| 1124 | " movq 144(%rsp), %rdx\n" |
| 1125 | " movq %rdx, 152(%rsp)\n" |
| 1126 | RESTORE_REGS_STRING |
| 1127 | /* Skip flags entry */ |
| 1128 | " addq $8, %rsp\n" |
| 1129 | " popfq\n" |
| 1130 | #else /* CONFIG_X86_32 */ |
| 1131 | " pushf\n" |
| 1132 | SAVE_REGS_STRING |
| 1133 | " movl %esp, %edx\n" |
| 1134 | ".global optprobe_template_val\n" |
| 1135 | "optprobe_template_val: \n" |
| 1136 | ASM_NOP5 |
| 1137 | ".global optprobe_template_call\n" |
| 1138 | "optprobe_template_call: \n" |
| 1139 | ASM_NOP5 |
| 1140 | RESTORE_REGS_STRING |
| 1141 | " addl $4, %esp\n" /* skip cs */ |
| 1142 | " popf\n" |
| 1143 | #endif |
| 1144 | ".global optprobe_template_end\n" |
| 1145 | "optprobe_template_end: \n"); |
| 1146 | } |
| 1147 | |
| 1148 | #define TMPL_MOVE_IDX \ |
| 1149 | ((long)&optprobe_template_val - (long)&optprobe_template_entry) |
| 1150 | #define TMPL_CALL_IDX \ |
| 1151 | ((long)&optprobe_template_call - (long)&optprobe_template_entry) |
| 1152 | #define TMPL_END_IDX \ |
| 1153 | ((long)&optprobe_template_end - (long)&optprobe_template_entry) |
| 1154 | |
| 1155 | #define INT3_SIZE sizeof(kprobe_opcode_t) |
| 1156 | |
| 1157 | /* Optimized kprobe call back function: called from optinsn */ |
| 1158 | static void __kprobes optimized_callback(struct optimized_kprobe *op, |
| 1159 | struct pt_regs *regs) |
| 1160 | { |
| 1161 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
| 1162 | |
| 1163 | preempt_disable(); |
| 1164 | if (kprobe_running()) { |
| 1165 | kprobes_inc_nmissed_count(&op->kp); |
| 1166 | } else { |
| 1167 | /* Save skipped registers */ |
| 1168 | #ifdef CONFIG_X86_64 |
| 1169 | regs->cs = __KERNEL_CS; |
| 1170 | #else |
| 1171 | regs->cs = __KERNEL_CS | get_kernel_rpl(); |
| 1172 | regs->gs = 0; |
| 1173 | #endif |
| 1174 | regs->ip = (unsigned long)op->kp.addr + INT3_SIZE; |
| 1175 | regs->orig_ax = ~0UL; |
| 1176 | |
| 1177 | __get_cpu_var(current_kprobe) = &op->kp; |
| 1178 | kcb->kprobe_status = KPROBE_HIT_ACTIVE; |
| 1179 | opt_pre_handler(&op->kp, regs); |
| 1180 | __get_cpu_var(current_kprobe) = NULL; |
| 1181 | } |
| 1182 | preempt_enable_no_resched(); |
| 1183 | } |
| 1184 | |
| 1185 | static int __kprobes copy_optimized_instructions(u8 *dest, u8 *src) |
| 1186 | { |
| 1187 | int len = 0, ret; |
| 1188 | |
| 1189 | while (len < RELATIVEJUMP_SIZE) { |
| 1190 | ret = __copy_instruction(dest + len, src + len, 1); |
| 1191 | if (!ret || !can_boost(dest + len)) |
| 1192 | return -EINVAL; |
| 1193 | len += ret; |
| 1194 | } |
| 1195 | /* Check whether the address range is reserved */ |
| 1196 | if (ftrace_text_reserved(src, src + len - 1) || |
| 1197 | alternatives_text_reserved(src, src + len - 1)) |
| 1198 | return -EBUSY; |
| 1199 | |
| 1200 | return len; |
| 1201 | } |
| 1202 | |
| 1203 | /* Check whether insn is indirect jump */ |
| 1204 | static int __kprobes insn_is_indirect_jump(struct insn *insn) |
| 1205 | { |
| 1206 | return ((insn->opcode.bytes[0] == 0xff && |
| 1207 | (X86_MODRM_REG(insn->modrm.value) & 6) == 4) || /* Jump */ |
| 1208 | insn->opcode.bytes[0] == 0xea); /* Segment based jump */ |
| 1209 | } |
| 1210 | |
| 1211 | /* Check whether insn jumps into specified address range */ |
| 1212 | static int insn_jump_into_range(struct insn *insn, unsigned long start, int len) |
| 1213 | { |
| 1214 | unsigned long target = 0; |
| 1215 | |
| 1216 | switch (insn->opcode.bytes[0]) { |
| 1217 | case 0xe0: /* loopne */ |
| 1218 | case 0xe1: /* loope */ |
| 1219 | case 0xe2: /* loop */ |
| 1220 | case 0xe3: /* jcxz */ |
| 1221 | case 0xe9: /* near relative jump */ |
| 1222 | case 0xeb: /* short relative jump */ |
| 1223 | break; |
| 1224 | case 0x0f: |
| 1225 | if ((insn->opcode.bytes[1] & 0xf0) == 0x80) /* jcc near */ |
| 1226 | break; |
| 1227 | return 0; |
| 1228 | default: |
| 1229 | if ((insn->opcode.bytes[0] & 0xf0) == 0x70) /* jcc short */ |
| 1230 | break; |
| 1231 | return 0; |
| 1232 | } |
| 1233 | target = (unsigned long)insn->next_byte + insn->immediate.value; |
| 1234 | |
| 1235 | return (start <= target && target <= start + len); |
| 1236 | } |
| 1237 | |
| 1238 | /* Decode whole function to ensure any instructions don't jump into target */ |
| 1239 | static int __kprobes can_optimize(unsigned long paddr) |
| 1240 | { |
| 1241 | int ret; |
| 1242 | unsigned long addr, size = 0, offset = 0; |
| 1243 | struct insn insn; |
| 1244 | kprobe_opcode_t buf[MAX_INSN_SIZE]; |
| 1245 | /* Dummy buffers for lookup_symbol_attrs */ |
| 1246 | static char __dummy_buf[KSYM_NAME_LEN]; |
| 1247 | |
| 1248 | /* Lookup symbol including addr */ |
| 1249 | if (!kallsyms_lookup(paddr, &size, &offset, NULL, __dummy_buf)) |
| 1250 | return 0; |
| 1251 | |
| 1252 | /* Check there is enough space for a relative jump. */ |
| 1253 | if (size - offset < RELATIVEJUMP_SIZE) |
| 1254 | return 0; |
| 1255 | |
| 1256 | /* Decode instructions */ |
| 1257 | addr = paddr - offset; |
| 1258 | while (addr < paddr - offset + size) { /* Decode until function end */ |
| 1259 | if (search_exception_tables(addr)) |
| 1260 | /* |
| 1261 | * Since some fixup code will jumps into this function, |
| 1262 | * we can't optimize kprobe in this function. |
| 1263 | */ |
| 1264 | return 0; |
| 1265 | kernel_insn_init(&insn, (void *)addr); |
| 1266 | insn_get_opcode(&insn); |
| 1267 | if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION) { |
| 1268 | ret = recover_probed_instruction(buf, addr); |
| 1269 | if (ret) |
| 1270 | return 0; |
| 1271 | kernel_insn_init(&insn, buf); |
| 1272 | } |
| 1273 | insn_get_length(&insn); |
| 1274 | /* Recover address */ |
| 1275 | insn.kaddr = (void *)addr; |
| 1276 | insn.next_byte = (void *)(addr + insn.length); |
| 1277 | /* Check any instructions don't jump into target */ |
| 1278 | if (insn_is_indirect_jump(&insn) || |
| 1279 | insn_jump_into_range(&insn, paddr + INT3_SIZE, |
| 1280 | RELATIVE_ADDR_SIZE)) |
| 1281 | return 0; |
| 1282 | addr += insn.length; |
| 1283 | } |
| 1284 | |
| 1285 | return 1; |
| 1286 | } |
| 1287 | |
| 1288 | /* Check optimized_kprobe can actually be optimized. */ |
| 1289 | int __kprobes arch_check_optimized_kprobe(struct optimized_kprobe *op) |
| 1290 | { |
| 1291 | int i; |
| 1292 | struct kprobe *p; |
| 1293 | |
| 1294 | for (i = 1; i < op->optinsn.size; i++) { |
| 1295 | p = get_kprobe(op->kp.addr + i); |
| 1296 | if (p && !kprobe_disabled(p)) |
| 1297 | return -EEXIST; |
| 1298 | } |
| 1299 | |
| 1300 | return 0; |
| 1301 | } |
| 1302 | |
| 1303 | /* Check the addr is within the optimized instructions. */ |
| 1304 | int __kprobes arch_within_optimized_kprobe(struct optimized_kprobe *op, |
| 1305 | unsigned long addr) |
| 1306 | { |
| 1307 | return ((unsigned long)op->kp.addr <= addr && |
| 1308 | (unsigned long)op->kp.addr + op->optinsn.size > addr); |
| 1309 | } |
| 1310 | |
| 1311 | /* Free optimized instruction slot */ |
| 1312 | static __kprobes |
| 1313 | void __arch_remove_optimized_kprobe(struct optimized_kprobe *op, int dirty) |
| 1314 | { |
| 1315 | if (op->optinsn.insn) { |
| 1316 | free_optinsn_slot(op->optinsn.insn, dirty); |
| 1317 | op->optinsn.insn = NULL; |
| 1318 | op->optinsn.size = 0; |
| 1319 | } |
| 1320 | } |
| 1321 | |
| 1322 | void __kprobes arch_remove_optimized_kprobe(struct optimized_kprobe *op) |
| 1323 | { |
| 1324 | __arch_remove_optimized_kprobe(op, 1); |
| 1325 | } |
| 1326 | |
| 1327 | /* |
| 1328 | * Copy replacing target instructions |
| 1329 | * Target instructions MUST be relocatable (checked inside) |
| 1330 | */ |
| 1331 | int __kprobes arch_prepare_optimized_kprobe(struct optimized_kprobe *op) |
| 1332 | { |
| 1333 | u8 *buf; |
| 1334 | int ret; |
| 1335 | long rel; |
| 1336 | |
| 1337 | if (!can_optimize((unsigned long)op->kp.addr)) |
| 1338 | return -EILSEQ; |
| 1339 | |
| 1340 | op->optinsn.insn = get_optinsn_slot(); |
| 1341 | if (!op->optinsn.insn) |
| 1342 | return -ENOMEM; |
| 1343 | |
| 1344 | /* |
| 1345 | * Verify if the address gap is in 2GB range, because this uses |
| 1346 | * a relative jump. |
| 1347 | */ |
| 1348 | rel = (long)op->optinsn.insn - (long)op->kp.addr + RELATIVEJUMP_SIZE; |
| 1349 | if (abs(rel) > 0x7fffffff) |
| 1350 | return -ERANGE; |
| 1351 | |
| 1352 | buf = (u8 *)op->optinsn.insn; |
| 1353 | |
| 1354 | /* Copy instructions into the out-of-line buffer */ |
| 1355 | ret = copy_optimized_instructions(buf + TMPL_END_IDX, op->kp.addr); |
| 1356 | if (ret < 0) { |
| 1357 | __arch_remove_optimized_kprobe(op, 0); |
| 1358 | return ret; |
| 1359 | } |
| 1360 | op->optinsn.size = ret; |
| 1361 | |
| 1362 | /* Copy arch-dep-instance from template */ |
| 1363 | memcpy(buf, &optprobe_template_entry, TMPL_END_IDX); |
| 1364 | |
| 1365 | /* Set probe information */ |
| 1366 | synthesize_set_arg1(buf + TMPL_MOVE_IDX, (unsigned long)op); |
| 1367 | |
| 1368 | /* Set probe function call */ |
| 1369 | synthesize_relcall(buf + TMPL_CALL_IDX, optimized_callback); |
| 1370 | |
| 1371 | /* Set returning jmp instruction at the tail of out-of-line buffer */ |
| 1372 | synthesize_reljump(buf + TMPL_END_IDX + op->optinsn.size, |
| 1373 | (u8 *)op->kp.addr + op->optinsn.size); |
| 1374 | |
| 1375 | flush_icache_range((unsigned long) buf, |
| 1376 | (unsigned long) buf + TMPL_END_IDX + |
| 1377 | op->optinsn.size + RELATIVEJUMP_SIZE); |
| 1378 | return 0; |
| 1379 | } |
| 1380 | |
| 1381 | /* Replace a breakpoint (int3) with a relative jump. */ |
| 1382 | int __kprobes arch_optimize_kprobe(struct optimized_kprobe *op) |
| 1383 | { |
| 1384 | unsigned char jmp_code[RELATIVEJUMP_SIZE]; |
| 1385 | s32 rel = (s32)((long)op->optinsn.insn - |
| 1386 | ((long)op->kp.addr + RELATIVEJUMP_SIZE)); |
| 1387 | |
| 1388 | /* Backup instructions which will be replaced by jump address */ |
| 1389 | memcpy(op->optinsn.copied_insn, op->kp.addr + INT3_SIZE, |
| 1390 | RELATIVE_ADDR_SIZE); |
| 1391 | |
| 1392 | jmp_code[0] = RELATIVEJUMP_OPCODE; |
| 1393 | *(s32 *)(&jmp_code[1]) = rel; |
| 1394 | |
| 1395 | /* |
| 1396 | * text_poke_smp doesn't support NMI/MCE code modifying. |
| 1397 | * However, since kprobes itself also doesn't support NMI/MCE |
| 1398 | * code probing, it's not a problem. |
| 1399 | */ |
| 1400 | text_poke_smp(op->kp.addr, jmp_code, RELATIVEJUMP_SIZE); |
| 1401 | return 0; |
| 1402 | } |
| 1403 | |
| 1404 | /* Replace a relative jump with a breakpoint (int3). */ |
| 1405 | void __kprobes arch_unoptimize_kprobe(struct optimized_kprobe *op) |
| 1406 | { |
| 1407 | u8 buf[RELATIVEJUMP_SIZE]; |
| 1408 | |
| 1409 | /* Set int3 to first byte for kprobes */ |
| 1410 | buf[0] = BREAKPOINT_INSTRUCTION; |
| 1411 | memcpy(buf + 1, op->optinsn.copied_insn, RELATIVE_ADDR_SIZE); |
| 1412 | text_poke_smp(op->kp.addr, buf, RELATIVEJUMP_SIZE); |
| 1413 | } |
| 1414 | |
| 1415 | static int __kprobes setup_detour_execution(struct kprobe *p, |
| 1416 | struct pt_regs *regs, |
| 1417 | int reenter) |
| 1418 | { |
| 1419 | struct optimized_kprobe *op; |
| 1420 | |
| 1421 | if (p->flags & KPROBE_FLAG_OPTIMIZED) { |
| 1422 | /* This kprobe is really able to run optimized path. */ |
| 1423 | op = container_of(p, struct optimized_kprobe, kp); |
| 1424 | /* Detour through copied instructions */ |
| 1425 | regs->ip = (unsigned long)op->optinsn.insn + TMPL_END_IDX; |
| 1426 | if (!reenter) |
| 1427 | reset_current_kprobe(); |
| 1428 | preempt_enable_no_resched(); |
| 1429 | return 1; |
| 1430 | } |
| 1431 | return 0; |
| 1432 | } |
| 1433 | #endif |
| 1434 | |
Rusty Lynch | 6772926 | 2005-07-05 18:54:50 -0700 | [diff] [blame] | 1435 | int __init arch_init_kprobes(void) |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 1436 | { |
Masami Hiramatsu | da07ab0 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 1437 | return 0; |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 1438 | } |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 1439 | |
| 1440 | int __kprobes arch_trampoline_kprobe(struct kprobe *p) |
| 1441 | { |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 1442 | return 0; |
| 1443 | } |