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