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. |
| 25 | * 2004-Oct Jim Keniston <kenistoj@us.ibm.com> and Prasanna S Panchamukhi |
| 26 | * <prasanna@in.ibm.com> adapted for x86_64 |
| 27 | * 2005-Mar Roland McGrath <roland@redhat.com> |
| 28 | * Fixed to handle %rip-relative addressing mode correctly. |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 29 | * 2005-May Rusty Lynch <rusty.lynch@intel.com> |
| 30 | * Added function return probes functionality |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | */ |
| 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/kprobes.h> |
| 34 | #include <linux/ptrace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/string.h> |
| 36 | #include <linux/slab.h> |
| 37 | #include <linux/preempt.h> |
Prasanna S Panchamukhi | c28f896 | 2006-03-26 01:38:23 -0800 | [diff] [blame] | 38 | #include <linux/module.h> |
Christoph Hellwig | 1eeb66a | 2007-05-08 00:27:03 -0700 | [diff] [blame] | 39 | #include <linux/kdebug.h> |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <asm/pgtable.h> |
Prasanna S Panchamukhi | c28f896 | 2006-03-26 01:38:23 -0800 | [diff] [blame] | 42 | #include <asm/uaccess.h> |
Andi Kleen | 19d36cc | 2007-07-22 11:12:31 +0200 | [diff] [blame] | 43 | #include <asm/alternative.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | void jprobe_return_end(void); |
Keshavamurthy Anil S | f709b12 | 2006-01-09 20:52:44 -0800 | [diff] [blame] | 46 | static void __kprobes arch_copy_kprobe(struct kprobe *p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 48 | DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL; |
| 49 | DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Masami Hiramatsu | f438d91 | 2007-10-16 01:27:49 -0700 | [diff] [blame] | 51 | struct kretprobe_blackpoint kretprobe_blacklist[] = { |
| 52 | {"__switch_to", }, /* This function switches only current task, but |
| 53 | doesn't switch kernel stack.*/ |
| 54 | {NULL, NULL} /* Terminator */ |
| 55 | }; |
| 56 | const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist); |
| 57 | |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame^] | 58 | /* Insert a jump instruction at address 'from', which jumps to address 'to'.*/ |
| 59 | static __always_inline void set_jmp_op(void *from, void *to) |
| 60 | { |
| 61 | struct __arch_jmp_op { |
| 62 | char op; |
| 63 | s32 raddr; |
| 64 | } __attribute__((packed)) * jop; |
| 65 | jop = (struct __arch_jmp_op *)from; |
| 66 | jop->raddr = (s32)((long)(to) - ((long)(from) + 5)); |
| 67 | jop->op = RELATIVEJUMP_INSTRUCTION; |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * returns non-zero if opcode is boostable |
| 72 | * RIP relative instructions are adjusted at copying time |
| 73 | */ |
| 74 | static __always_inline int can_boost(kprobe_opcode_t *opcodes) |
| 75 | { |
| 76 | #define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\ |
| 77 | (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \ |
| 78 | (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \ |
| 79 | (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \ |
| 80 | (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \ |
| 81 | << (row % 64)) |
| 82 | /* |
| 83 | * Undefined/reserved opcodes, conditional jump, Opcode Extension |
| 84 | * Groups, and some special opcodes can not boost. |
| 85 | */ |
| 86 | static const unsigned long twobyte_is_boostable[256 / 64] = { |
| 87 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ |
| 88 | /* ---------------------------------------------- */ |
| 89 | W(0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0)|/* 00 */ |
| 90 | W(0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)|/* 10 */ |
| 91 | W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)|/* 20 */ |
| 92 | W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),/* 30 */ |
| 93 | W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)|/* 40 */ |
| 94 | W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)|/* 50 */ |
| 95 | W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1)|/* 60 */ |
| 96 | W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1),/* 70 */ |
| 97 | W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)|/* 80 */ |
| 98 | W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)|/* 90 */ |
| 99 | W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1)|/* a0 */ |
| 100 | W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1),/* b0 */ |
| 101 | W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1)|/* c0 */ |
| 102 | W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1)|/* d0 */ |
| 103 | W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1)|/* e0 */ |
| 104 | W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0) /* f0 */ |
| 105 | /* ----------------------------------------------- */ |
| 106 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ |
| 107 | }; |
| 108 | #undef W |
| 109 | kprobe_opcode_t opcode; |
| 110 | kprobe_opcode_t *orig_opcodes = opcodes; |
| 111 | |
| 112 | retry: |
| 113 | if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1) |
| 114 | return 0; |
| 115 | opcode = *(opcodes++); |
| 116 | |
| 117 | /* 2nd-byte opcode */ |
| 118 | if (opcode == 0x0f) { |
| 119 | if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1) |
| 120 | return 0; |
| 121 | return test_bit(*opcodes, twobyte_is_boostable); |
| 122 | } |
| 123 | |
| 124 | switch (opcode & 0xf0) { |
| 125 | case 0x40: |
| 126 | goto retry; /* REX prefix is boostable */ |
| 127 | case 0x60: |
| 128 | if (0x63 < opcode && opcode < 0x67) |
| 129 | goto retry; /* prefixes */ |
| 130 | /* can't boost Address-size override and bound */ |
| 131 | return (opcode != 0x62 && opcode != 0x67); |
| 132 | case 0x70: |
| 133 | return 0; /* can't boost conditional jump */ |
| 134 | case 0xc0: |
| 135 | /* can't boost software-interruptions */ |
| 136 | return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf; |
| 137 | case 0xd0: |
| 138 | /* can boost AA* and XLAT */ |
| 139 | return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7); |
| 140 | case 0xe0: |
| 141 | /* can boost in/out and absolute jmps */ |
| 142 | return ((opcode & 0x04) || opcode == 0xea); |
| 143 | case 0xf0: |
| 144 | if ((opcode & 0x0c) == 0 && opcode != 0xf1) |
| 145 | goto retry; /* lock/rep(ne) prefix */ |
| 146 | /* clear and set flags are boostable */ |
| 147 | return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe)); |
| 148 | default: |
| 149 | /* segment override prefixes are boostable */ |
| 150 | if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e) |
| 151 | goto retry; /* prefixes */ |
| 152 | /* CS override prefix and call are not boostable */ |
| 153 | return (opcode != 0x2e && opcode != 0x9a); |
| 154 | } |
| 155 | } |
| 156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | /* |
| 158 | * returns non-zero if opcode modifies the interrupt flag. |
| 159 | */ |
Andrew Morton | 8645419 | 2007-11-26 20:42:19 +0100 | [diff] [blame] | 160 | static int __kprobes is_IF_modifier(kprobe_opcode_t *insn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | { |
| 162 | switch (*insn) { |
| 163 | case 0xfa: /* cli */ |
| 164 | case 0xfb: /* sti */ |
| 165 | case 0xcf: /* iret/iretd */ |
| 166 | case 0x9d: /* popf/popfd */ |
| 167 | return 1; |
| 168 | } |
| 169 | |
| 170 | if (*insn >= 0x40 && *insn <= 0x4f && *++insn == 0xcf) |
| 171 | return 1; |
| 172 | return 0; |
| 173 | } |
| 174 | |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 175 | int __kprobes arch_prepare_kprobe(struct kprobe *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | { |
| 177 | /* insn: must be on special executable page on x86_64. */ |
Zhang, Yanmin | 2dd960d | 2005-09-30 11:59:20 -0700 | [diff] [blame] | 178 | p->ainsn.insn = get_insn_slot(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | if (!p->ainsn.insn) { |
| 180 | return -ENOMEM; |
| 181 | } |
Anil S Keshavamurthy | 49a2a1b | 2006-01-09 20:52:43 -0800 | [diff] [blame] | 182 | arch_copy_kprobe(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * Determine if the instruction uses the %rip-relative addressing mode. |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame^] | 188 | * If it does, Return the address of the 32-bit displacement word. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | * If not, return null. |
| 190 | */ |
Prasanna S Panchamukhi | 3b60211 | 2006-04-18 22:22:00 -0700 | [diff] [blame] | 191 | static s32 __kprobes *is_riprel(u8 *insn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | { |
| 193 | #define W(row,b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,ba,bb,bc,bd,be,bf) \ |
| 194 | (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \ |
| 195 | (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \ |
| 196 | (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \ |
| 197 | (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \ |
| 198 | << (row % 64)) |
| 199 | static const u64 onebyte_has_modrm[256 / 64] = { |
| 200 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ |
| 201 | /* ------------------------------- */ |
| 202 | W(0x00, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0)| /* 00 */ |
| 203 | W(0x10, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0)| /* 10 */ |
| 204 | W(0x20, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0)| /* 20 */ |
| 205 | W(0x30, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0), /* 30 */ |
| 206 | W(0x40, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 40 */ |
| 207 | W(0x50, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 50 */ |
| 208 | W(0x60, 0,0,1,1,0,0,0,0,0,1,0,1,0,0,0,0)| /* 60 */ |
| 209 | W(0x70, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* 70 */ |
| 210 | W(0x80, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 80 */ |
| 211 | W(0x90, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 90 */ |
| 212 | W(0xa0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* a0 */ |
| 213 | W(0xb0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* b0 */ |
| 214 | W(0xc0, 1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0)| /* c0 */ |
| 215 | W(0xd0, 1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1)| /* d0 */ |
| 216 | W(0xe0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* e0 */ |
| 217 | W(0xf0, 0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1) /* f0 */ |
| 218 | /* ------------------------------- */ |
| 219 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ |
| 220 | }; |
| 221 | static const u64 twobyte_has_modrm[256 / 64] = { |
| 222 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ |
| 223 | /* ------------------------------- */ |
| 224 | W(0x00, 1,1,1,1,0,0,0,0,0,0,0,0,0,1,0,1)| /* 0f */ |
| 225 | W(0x10, 1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0)| /* 1f */ |
| 226 | W(0x20, 1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1)| /* 2f */ |
| 227 | W(0x30, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* 3f */ |
| 228 | W(0x40, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 4f */ |
| 229 | W(0x50, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 5f */ |
| 230 | W(0x60, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 6f */ |
| 231 | W(0x70, 1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1), /* 7f */ |
| 232 | W(0x80, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 8f */ |
| 233 | W(0x90, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 9f */ |
| 234 | W(0xa0, 0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1)| /* af */ |
| 235 | W(0xb0, 1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1), /* bf */ |
| 236 | W(0xc0, 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0)| /* cf */ |
| 237 | W(0xd0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* df */ |
| 238 | W(0xe0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* ef */ |
| 239 | W(0xf0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0) /* ff */ |
| 240 | /* ------------------------------- */ |
| 241 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ |
| 242 | }; |
| 243 | #undef W |
| 244 | int need_modrm; |
| 245 | |
| 246 | /* Skip legacy instruction prefixes. */ |
| 247 | while (1) { |
| 248 | switch (*insn) { |
| 249 | case 0x66: |
| 250 | case 0x67: |
| 251 | case 0x2e: |
| 252 | case 0x3e: |
| 253 | case 0x26: |
| 254 | case 0x64: |
| 255 | case 0x65: |
| 256 | case 0x36: |
| 257 | case 0xf0: |
| 258 | case 0xf3: |
| 259 | case 0xf2: |
| 260 | ++insn; |
| 261 | continue; |
| 262 | } |
| 263 | break; |
| 264 | } |
| 265 | |
| 266 | /* Skip REX instruction prefix. */ |
| 267 | if ((*insn & 0xf0) == 0x40) |
| 268 | ++insn; |
| 269 | |
| 270 | if (*insn == 0x0f) { /* Two-byte opcode. */ |
| 271 | ++insn; |
| 272 | need_modrm = test_bit(*insn, twobyte_has_modrm); |
| 273 | } else { /* One-byte opcode. */ |
| 274 | need_modrm = test_bit(*insn, onebyte_has_modrm); |
| 275 | } |
| 276 | |
| 277 | if (need_modrm) { |
| 278 | u8 modrm = *++insn; |
| 279 | if ((modrm & 0xc7) == 0x05) { /* %rip+disp32 addressing mode */ |
| 280 | /* Displacement follows ModRM byte. */ |
| 281 | return (s32 *) ++insn; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | /* No %rip-relative addressing mode here. */ |
| 286 | return NULL; |
| 287 | } |
| 288 | |
Keshavamurthy Anil S | f709b12 | 2006-01-09 20:52:44 -0800 | [diff] [blame] | 289 | static void __kprobes arch_copy_kprobe(struct kprobe *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | { |
| 291 | s32 *ripdisp; |
| 292 | memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE); |
| 293 | ripdisp = is_riprel(p->ainsn.insn); |
| 294 | if (ripdisp) { |
| 295 | /* |
| 296 | * The copied instruction uses the %rip-relative |
| 297 | * addressing mode. Adjust the displacement for the |
| 298 | * difference between the original location of this |
| 299 | * instruction and the location of the copy that will |
| 300 | * actually be run. The tricky bit here is making sure |
| 301 | * that the sign extension happens correctly in this |
| 302 | * calculation, since we need a signed 32-bit result to |
| 303 | * be sign-extended to 64 bits when it's added to the |
| 304 | * %rip value and yield the same 64-bit result that the |
| 305 | * sign-extension of the original signed 32-bit |
| 306 | * displacement would have given. |
| 307 | */ |
| 308 | s64 disp = (u8 *) p->addr + *ripdisp - (u8 *) p->ainsn.insn; |
| 309 | BUG_ON((s64) (s32) disp != disp); /* Sanity check. */ |
| 310 | *ripdisp = disp; |
| 311 | } |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame^] | 312 | if (can_boost(p->addr)) { |
| 313 | p->ainsn.boostable = 0; |
| 314 | } else { |
| 315 | p->ainsn.boostable = -1; |
| 316 | } |
Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame] | 317 | p->opcode = *p->addr; |
| 318 | } |
| 319 | |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 320 | void __kprobes arch_arm_kprobe(struct kprobe *p) |
Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame] | 321 | { |
Andi Kleen | 19d36cc | 2007-07-22 11:12:31 +0200 | [diff] [blame] | 322 | text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1); |
Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 325 | void __kprobes arch_disarm_kprobe(struct kprobe *p) |
Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame] | 326 | { |
Andi Kleen | 19d36cc | 2007-07-22 11:12:31 +0200 | [diff] [blame] | 327 | text_poke(p->addr, &p->opcode, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Ananth N Mavinakayanahalli | 0498b63 | 2006-01-09 20:52:46 -0800 | [diff] [blame] | 330 | void __kprobes arch_remove_kprobe(struct kprobe *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | { |
Ingo Molnar | 7a7d1cf | 2006-03-23 03:00:35 -0800 | [diff] [blame] | 332 | mutex_lock(&kprobe_mutex); |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame^] | 333 | free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1)); |
Ingo Molnar | 7a7d1cf | 2006-03-23 03:00:35 -0800 | [diff] [blame] | 334 | mutex_unlock(&kprobe_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Prasanna S Panchamukhi | 3b60211 | 2006-04-18 22:22:00 -0700 | [diff] [blame] | 337 | static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb) |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 338 | { |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 339 | kcb->prev_kprobe.kp = kprobe_running(); |
| 340 | kcb->prev_kprobe.status = kcb->kprobe_status; |
| 341 | kcb->prev_kprobe.old_rflags = kcb->kprobe_old_rflags; |
| 342 | kcb->prev_kprobe.saved_rflags = kcb->kprobe_saved_rflags; |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Prasanna S Panchamukhi | 3b60211 | 2006-04-18 22:22:00 -0700 | [diff] [blame] | 345 | static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb) |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 346 | { |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 347 | __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp; |
| 348 | kcb->kprobe_status = kcb->prev_kprobe.status; |
| 349 | kcb->kprobe_old_rflags = kcb->prev_kprobe.old_rflags; |
| 350 | kcb->kprobe_saved_rflags = kcb->prev_kprobe.saved_rflags; |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Prasanna S Panchamukhi | 3b60211 | 2006-04-18 22:22:00 -0700 | [diff] [blame] | 353 | 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] | 354 | struct kprobe_ctlblk *kcb) |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 355 | { |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 356 | __get_cpu_var(current_kprobe) = p; |
| 357 | kcb->kprobe_saved_rflags = kcb->kprobe_old_rflags |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 358 | = (regs->flags & (TF_MASK | IF_MASK)); |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 359 | if (is_IF_modifier(p->ainsn.insn)) |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 360 | kcb->kprobe_saved_rflags &= ~IF_MASK; |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Roland McGrath | 1ecc798 | 2008-01-30 13:30:54 +0100 | [diff] [blame] | 363 | static __always_inline void clear_btf(void) |
| 364 | { |
| 365 | if (test_thread_flag(TIF_DEBUGCTLMSR)) |
| 366 | wrmsrl(MSR_IA32_DEBUGCTLMSR, 0); |
| 367 | } |
| 368 | |
| 369 | static __always_inline void restore_btf(void) |
| 370 | { |
| 371 | if (test_thread_flag(TIF_DEBUGCTLMSR)) |
| 372 | wrmsrl(MSR_IA32_DEBUGCTLMSR, current->thread.debugctlmsr); |
| 373 | } |
| 374 | |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 375 | static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | { |
Roland McGrath | 1ecc798 | 2008-01-30 13:30:54 +0100 | [diff] [blame] | 377 | clear_btf(); |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 378 | regs->flags |= TF_MASK; |
| 379 | regs->flags &= ~IF_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | /*single step inline if the instruction is an int3*/ |
| 381 | if (p->opcode == BREAKPOINT_INSTRUCTION) |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 382 | regs->ip = (unsigned long)p->addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | else |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 384 | regs->ip = (unsigned long)p->ainsn.insn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Ananth N Mavinakayanahalli | 991a51d | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 387 | /* Called with kretprobe_lock held */ |
Christoph Hellwig | 4c4308c | 2007-05-08 00:34:14 -0700 | [diff] [blame] | 388 | void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri, |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 389 | struct pt_regs *regs) |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 390 | { |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 391 | unsigned long *sara = (unsigned long *)regs->sp; |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 392 | |
Christoph Hellwig | 4c4308c | 2007-05-08 00:34:14 -0700 | [diff] [blame] | 393 | ri->ret_addr = (kprobe_opcode_t *) *sara; |
| 394 | /* Replace the return addr with trampoline addr */ |
| 395 | *sara = (unsigned long) &kretprobe_trampoline; |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 398 | int __kprobes kprobe_handler(struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | { |
| 400 | struct kprobe *p; |
| 401 | int ret = 0; |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 402 | kprobe_opcode_t *addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t)); |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 403 | struct kprobe_ctlblk *kcb; |
| 404 | |
| 405 | /* |
| 406 | * We don't want to be preempted for the entire |
| 407 | * duration of kprobe processing |
| 408 | */ |
| 409 | preempt_disable(); |
| 410 | kcb = get_kprobe_ctlblk(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | /* Check we're not actually recursing */ |
| 413 | if (kprobe_running()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | p = get_kprobe(addr); |
| 415 | if (p) { |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 416 | if (kcb->kprobe_status == KPROBE_HIT_SS && |
Keshavamurthy Anil S | deac66a | 2005-09-06 15:19:35 -0700 | [diff] [blame] | 417 | *p->ainsn.insn == BREAKPOINT_INSTRUCTION) { |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 418 | regs->flags &= ~TF_MASK; |
| 419 | regs->flags |= kcb->kprobe_saved_rflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | goto no_kprobe; |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 421 | } else if (kcb->kprobe_status == KPROBE_HIT_SSDONE) { |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 422 | /* TODO: Provide re-entrancy from |
| 423 | * post_kprobes_handler() and avoid exception |
| 424 | * stack corruption while single-stepping on |
| 425 | * the instruction of the new probe. |
| 426 | */ |
| 427 | arch_disarm_kprobe(p); |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 428 | regs->ip = (unsigned long)p->addr; |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 429 | reset_current_kprobe(); |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 430 | ret = 1; |
| 431 | } else { |
| 432 | /* We have reentered the kprobe_handler(), since |
| 433 | * another probe was hit while within the |
| 434 | * handler. We here save the original kprobe |
| 435 | * variables and just single step on instruction |
| 436 | * of the new probe without calling any user |
| 437 | * handlers. |
| 438 | */ |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 439 | save_previous_kprobe(kcb); |
| 440 | set_current_kprobe(p, regs, kcb); |
Keshavamurthy Anil S | bf8d5c5 | 2005-12-12 00:37:34 -0800 | [diff] [blame] | 441 | kprobes_inc_nmissed_count(p); |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 442 | prepare_singlestep(p, regs); |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 443 | kcb->kprobe_status = KPROBE_REENTER; |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 444 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | } else { |
Keshavamurthy Anil S | eb3a729 | 2006-01-11 12:17:42 -0800 | [diff] [blame] | 447 | if (*addr != BREAKPOINT_INSTRUCTION) { |
| 448 | /* The breakpoint instruction was removed by |
| 449 | * another cpu right after we hit, no further |
| 450 | * handling of this interrupt is appropriate |
| 451 | */ |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 452 | regs->ip = (unsigned long)addr; |
Keshavamurthy Anil S | eb3a729 | 2006-01-11 12:17:42 -0800 | [diff] [blame] | 453 | ret = 1; |
| 454 | goto no_kprobe; |
| 455 | } |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 456 | p = __get_cpu_var(current_kprobe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | if (p->break_handler && p->break_handler(p, regs)) { |
| 458 | goto ss_probe; |
| 459 | } |
| 460 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | goto no_kprobe; |
| 462 | } |
| 463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | p = get_kprobe(addr); |
| 465 | if (!p) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | if (*addr != BREAKPOINT_INSTRUCTION) { |
| 467 | /* |
| 468 | * The breakpoint instruction was removed right |
| 469 | * after we hit it. Another cpu has removed |
| 470 | * either a probepoint or a debugger breakpoint |
| 471 | * at this address. In either case, no further |
| 472 | * handling of this interrupt is appropriate. |
Jim Keniston | bce0649 | 2005-09-06 15:19:34 -0700 | [diff] [blame] | 473 | * Back up over the (now missing) int3 and run |
| 474 | * the original instruction. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | */ |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 476 | regs->ip = (unsigned long)addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | ret = 1; |
| 478 | } |
| 479 | /* Not one of ours: let kernel handle it */ |
| 480 | goto no_kprobe; |
| 481 | } |
| 482 | |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 483 | set_current_kprobe(p, regs, kcb); |
| 484 | kcb->kprobe_status = KPROBE_HIT_ACTIVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | |
| 486 | if (p->pre_handler && p->pre_handler(p, regs)) |
| 487 | /* handler has already set things up, so skip ss setup */ |
| 488 | return 1; |
| 489 | |
| 490 | ss_probe: |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame^] | 491 | #if !defined(CONFIG_PREEMPT) || defined(CONFIG_PM) |
| 492 | if (p->ainsn.boostable == 1 && !p->post_handler) { |
| 493 | /* Boost up -- we can execute copied instructions directly */ |
| 494 | reset_current_kprobe(); |
| 495 | regs->ip = (unsigned long)p->ainsn.insn; |
| 496 | preempt_enable_no_resched(); |
| 497 | return 1; |
| 498 | } |
| 499 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | prepare_singlestep(p, regs); |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 501 | kcb->kprobe_status = KPROBE_HIT_SS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | return 1; |
| 503 | |
| 504 | no_kprobe: |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 505 | preempt_enable_no_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | return ret; |
| 507 | } |
| 508 | |
| 509 | /* |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 510 | * For function-return probes, init_kprobes() establishes a probepoint |
| 511 | * here. When a retprobed function returns, this probe is hit and |
| 512 | * trampoline_probe_handler() runs, calling the kretprobe's handler. |
| 513 | */ |
| 514 | void kretprobe_trampoline_holder(void) |
| 515 | { |
| 516 | asm volatile ( ".global kretprobe_trampoline\n" |
| 517 | "kretprobe_trampoline: \n" |
| 518 | "nop\n"); |
| 519 | } |
| 520 | |
| 521 | /* |
| 522 | * Called when we hit the probe point at kretprobe_trampoline |
| 523 | */ |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 524 | int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs) |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 525 | { |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 526 | struct kretprobe_instance *ri = NULL; |
bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 527 | struct hlist_head *head, empty_rp; |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 528 | struct hlist_node *node, *tmp; |
Ananth N Mavinakayanahalli | 991a51d | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 529 | unsigned long flags, orig_ret_address = 0; |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 530 | unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline; |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 531 | |
bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 532 | INIT_HLIST_HEAD(&empty_rp); |
Ananth N Mavinakayanahalli | 991a51d | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 533 | spin_lock_irqsave(&kretprobe_lock, flags); |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 534 | head = kretprobe_inst_table_head(current); |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 535 | |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 536 | /* |
| 537 | * It is possible to have multiple instances associated with a given |
| 538 | * task either because an multiple functions in the call path |
| 539 | * have a return probe installed on them, and/or more then one return |
| 540 | * return probe was registered for a target function. |
| 541 | * |
| 542 | * We can handle this because: |
| 543 | * - instances are always inserted at the head of the list |
| 544 | * - when multiple return probes are registered for the same |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 545 | * function, the first instance's ret_addr will point to the |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 546 | * real return address, and all the rest will point to |
| 547 | * kretprobe_trampoline |
| 548 | */ |
| 549 | hlist_for_each_entry_safe(ri, node, tmp, head, hlist) { |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 550 | if (ri->task != current) |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 551 | /* another task is sharing our hash bucket */ |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 552 | continue; |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 553 | |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 554 | if (ri->rp && ri->rp->handler) |
| 555 | ri->rp->handler(ri, regs); |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 556 | |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 557 | orig_ret_address = (unsigned long)ri->ret_addr; |
bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 558 | recycle_rp_inst(ri, &empty_rp); |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 559 | |
| 560 | if (orig_ret_address != trampoline_address) |
| 561 | /* |
| 562 | * This is the real return address. Any other |
| 563 | * instances associated with this task are for |
| 564 | * other calls deeper on the call stack |
| 565 | */ |
| 566 | break; |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 567 | } |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 568 | |
Ananth N Mavinakayanahalli | 0f95b7f | 2007-05-08 00:28:27 -0700 | [diff] [blame] | 569 | kretprobe_assert(ri, orig_ret_address, trampoline_address); |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 570 | regs->ip = orig_ret_address; |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 571 | |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 572 | reset_current_kprobe(); |
Ananth N Mavinakayanahalli | 991a51d | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 573 | spin_unlock_irqrestore(&kretprobe_lock, flags); |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 574 | preempt_enable_no_resched(); |
| 575 | |
bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 576 | hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) { |
| 577 | hlist_del(&ri->hlist); |
| 578 | kfree(ri); |
| 579 | } |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 580 | /* |
| 581 | * By returning a non-zero value, we are telling |
| 582 | * kprobe_handler() that we don't want the post_handler |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 583 | * to run (and have re-enabled preemption) |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 584 | */ |
| 585 | return 1; |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | * Called after single-stepping. p->addr is the address of the |
| 590 | * instruction whose first byte has been replaced by the "int 3" |
| 591 | * instruction. To avoid the SMP problems that can occur when we |
| 592 | * temporarily put back the original opcode to single-step, we |
| 593 | * single-stepped a copy of the instruction. The address of this |
| 594 | * copy is p->ainsn.insn. |
| 595 | * |
| 596 | * This function prepares to return from the post-single-step |
| 597 | * interrupt. We have to fix up the stack as follows: |
| 598 | * |
| 599 | * 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] | 600 | * 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] | 601 | * it relative to the original instruction. |
| 602 | * |
| 603 | * 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] | 604 | * 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] | 605 | * |
| 606 | * 2) If the single-stepped instruction was a call, the return address |
| 607 | * that is atop the stack is the address following the copied instruction. |
| 608 | * We need to make it the address following the original instruction. |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame^] | 609 | * |
| 610 | * If this is the first time we've single-stepped the instruction at |
| 611 | * this probepoint, and the instruction is boostable, boost it: add a |
| 612 | * jump instruction after the copied instruction, that jumps to the next |
| 613 | * instruction after the probepoint. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | */ |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 615 | static void __kprobes resume_execution(struct kprobe *p, |
| 616 | struct pt_regs *regs, struct kprobe_ctlblk *kcb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | { |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 618 | unsigned long *tos = (unsigned long *)regs->sp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | unsigned long copy_rip = (unsigned long)p->ainsn.insn; |
| 620 | unsigned long orig_rip = (unsigned long)p->addr; |
| 621 | kprobe_opcode_t *insn = p->ainsn.insn; |
| 622 | |
| 623 | /*skip the REX prefix*/ |
| 624 | if (*insn >= 0x40 && *insn <= 0x4f) |
| 625 | insn++; |
| 626 | |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 627 | regs->flags &= ~TF_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | switch (*insn) { |
Masami Hiramatsu | 0b0122f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 629 | case 0x9c: /* pushfl */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | *tos &= ~(TF_MASK | IF_MASK); |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 631 | *tos |= kcb->kprobe_old_rflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | break; |
Masami Hiramatsu | 0b0122f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 633 | case 0xc2: /* iret/ret/lret */ |
| 634 | case 0xc3: |
Prasanna S Panchamukhi | 0b9e2ca | 2005-05-05 16:15:40 -0700 | [diff] [blame] | 635 | case 0xca: |
Masami Hiramatsu | 0b0122f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 636 | case 0xcb: |
| 637 | case 0xcf: |
| 638 | case 0xea: /* jmp absolute -- ip is correct */ |
| 639 | /* ip is already adjusted, no more changes required */ |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame^] | 640 | p->ainsn.boostable = 1; |
Masami Hiramatsu | 0b0122f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 641 | goto no_change; |
| 642 | case 0xe8: /* call relative - Fix return addr */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | *tos = orig_rip + (*tos - copy_rip); |
| 644 | break; |
| 645 | case 0xff: |
Satoshi Oshima | dc49e34 | 2006-05-20 15:00:21 -0700 | [diff] [blame] | 646 | if ((insn[1] & 0x30) == 0x10) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | /* call absolute, indirect */ |
Masami Hiramatsu | 0b0122f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 648 | /* Fix return addr; ip is correct. */ |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame^] | 649 | /* not boostable */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | *tos = orig_rip + (*tos - copy_rip); |
Masami Hiramatsu | 0b0122f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 651 | goto no_change; |
Satoshi Oshima | dc49e34 | 2006-05-20 15:00:21 -0700 | [diff] [blame] | 652 | } else if (((insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */ |
| 653 | ((insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */ |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame^] | 654 | /* ip is correct. And this is boostable */ |
| 655 | p->ainsn.boostable = 1; |
Masami Hiramatsu | 0b0122f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 656 | goto no_change; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | default: |
| 659 | break; |
| 660 | } |
| 661 | |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame^] | 662 | if (p->ainsn.boostable == 0) { |
| 663 | if ((regs->ip > copy_rip) && |
| 664 | (regs->ip - copy_rip) + 5 < MAX_INSN_SIZE) { |
| 665 | /* |
| 666 | * These instructions can be executed directly if it |
| 667 | * jumps back to correct address. |
| 668 | */ |
| 669 | set_jmp_op((void *)regs->ip, |
| 670 | (void *)orig_rip + (regs->ip - copy_rip)); |
| 671 | p->ainsn.boostable = 1; |
| 672 | } else { |
| 673 | p->ainsn.boostable = -1; |
| 674 | } |
| 675 | } |
| 676 | |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 677 | regs->ip = orig_rip + (regs->ip - copy_rip); |
| 678 | |
Masami Hiramatsu | 0b0122f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 679 | no_change: |
Roland McGrath | 1ecc798 | 2008-01-30 13:30:54 +0100 | [diff] [blame] | 680 | restore_btf(); |
Masami Hiramatsu | 0b0122f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 681 | |
| 682 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | } |
| 684 | |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 685 | int __kprobes post_kprobe_handler(struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | { |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 687 | struct kprobe *cur = kprobe_running(); |
| 688 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
| 689 | |
| 690 | if (!cur) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | return 0; |
| 692 | |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 693 | if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) { |
| 694 | kcb->kprobe_status = KPROBE_HIT_SSDONE; |
| 695 | cur->post_handler(cur, regs, 0); |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 696 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 698 | resume_execution(cur, regs, kcb); |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 699 | regs->flags |= kcb->kprobe_saved_rflags; |
| 700 | trace_hardirqs_fixup_flags(regs->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 702 | /* Restore the original saved kprobes variables and continue. */ |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 703 | if (kcb->kprobe_status == KPROBE_REENTER) { |
| 704 | restore_previous_kprobe(kcb); |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 705 | goto out; |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 706 | } |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 707 | reset_current_kprobe(); |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 708 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | preempt_enable_no_resched(); |
| 710 | |
| 711 | /* |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 712 | * if somebody else is singlestepping across a probe point, flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | * will have TF set, in which case, continue the remaining processing |
| 714 | * of do_debug, as if this is not a probe hit. |
| 715 | */ |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 716 | if (regs->flags & TF_MASK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | return 0; |
| 718 | |
| 719 | return 1; |
| 720 | } |
| 721 | |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 722 | int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | { |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 724 | struct kprobe *cur = kprobe_running(); |
| 725 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
Prasanna S Panchamukhi | c28f896 | 2006-03-26 01:38:23 -0800 | [diff] [blame] | 726 | const struct exception_table_entry *fixup; |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 727 | |
Prasanna S Panchamukhi | c28f896 | 2006-03-26 01:38:23 -0800 | [diff] [blame] | 728 | switch(kcb->kprobe_status) { |
| 729 | case KPROBE_HIT_SS: |
| 730 | case KPROBE_REENTER: |
| 731 | /* |
| 732 | * We are here because the instruction being single |
| 733 | * stepped caused a page fault. We reset the current |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 734 | * kprobe and the ip points back to the probe address |
Prasanna S Panchamukhi | c28f896 | 2006-03-26 01:38:23 -0800 | [diff] [blame] | 735 | * and allow the page fault handler to continue as a |
| 736 | * normal page fault. |
| 737 | */ |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 738 | regs->ip = (unsigned long)cur->addr; |
| 739 | regs->flags |= kcb->kprobe_old_rflags; |
Prasanna S Panchamukhi | c28f896 | 2006-03-26 01:38:23 -0800 | [diff] [blame] | 740 | if (kcb->kprobe_status == KPROBE_REENTER) |
| 741 | restore_previous_kprobe(kcb); |
| 742 | else |
| 743 | reset_current_kprobe(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | preempt_enable_no_resched(); |
Prasanna S Panchamukhi | c28f896 | 2006-03-26 01:38:23 -0800 | [diff] [blame] | 745 | break; |
| 746 | case KPROBE_HIT_ACTIVE: |
| 747 | case KPROBE_HIT_SSDONE: |
| 748 | /* |
| 749 | * We increment the nmissed count for accounting, |
| 750 | * we can also use npre/npostfault count for accouting |
| 751 | * these specific fault cases. |
| 752 | */ |
| 753 | kprobes_inc_nmissed_count(cur); |
| 754 | |
| 755 | /* |
| 756 | * We come here because instructions in the pre/post |
| 757 | * handler caused the page_fault, this could happen |
| 758 | * if handler tries to access user space by |
| 759 | * copy_from_user(), get_user() etc. Let the |
| 760 | * user-specified handler try to fix it first. |
| 761 | */ |
| 762 | if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr)) |
| 763 | return 1; |
| 764 | |
| 765 | /* |
| 766 | * In case the user-specified fault handler returned |
| 767 | * zero, try to fix up. |
| 768 | */ |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 769 | fixup = search_exception_tables(regs->ip); |
Prasanna S Panchamukhi | c28f896 | 2006-03-26 01:38:23 -0800 | [diff] [blame] | 770 | if (fixup) { |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 771 | regs->ip = fixup->fixup; |
Prasanna S Panchamukhi | c28f896 | 2006-03-26 01:38:23 -0800 | [diff] [blame] | 772 | return 1; |
| 773 | } |
| 774 | |
| 775 | /* |
| 776 | * fixup() could not handle it, |
| 777 | * Let do_page_fault() fix it. |
| 778 | */ |
| 779 | break; |
| 780 | default: |
| 781 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | } |
| 783 | return 0; |
| 784 | } |
| 785 | |
| 786 | /* |
| 787 | * Wrapper routine for handling exceptions. |
| 788 | */ |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 789 | int __kprobes kprobe_exceptions_notify(struct notifier_block *self, |
| 790 | unsigned long val, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | { |
| 792 | struct die_args *args = (struct die_args *)data; |
Ananth N Mavinakayanahalli | 66ff2d0 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 793 | int ret = NOTIFY_DONE; |
| 794 | |
bibo,mao | 2326c77 | 2006-03-26 01:38:21 -0800 | [diff] [blame] | 795 | if (args->regs && user_mode(args->regs)) |
| 796 | return ret; |
| 797 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | switch (val) { |
| 799 | case DIE_INT3: |
| 800 | if (kprobe_handler(args->regs)) |
Ananth N Mavinakayanahalli | 66ff2d0 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 801 | ret = NOTIFY_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | break; |
| 803 | case DIE_DEBUG: |
| 804 | if (post_kprobe_handler(args->regs)) |
Ananth N Mavinakayanahalli | 66ff2d0 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 805 | ret = NOTIFY_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | break; |
| 807 | case DIE_GPF: |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 808 | /* kprobe_running() needs smp_processor_id() */ |
| 809 | preempt_disable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | if (kprobe_running() && |
| 811 | kprobe_fault_handler(args->regs, args->trapnr)) |
Ananth N Mavinakayanahalli | 66ff2d0 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 812 | ret = NOTIFY_STOP; |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 813 | preempt_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | break; |
| 815 | default: |
| 816 | break; |
| 817 | } |
Ananth N Mavinakayanahalli | 66ff2d0 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 818 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | } |
| 820 | |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 821 | int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | { |
| 823 | struct jprobe *jp = container_of(p, struct jprobe, kp); |
| 824 | unsigned long addr; |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 825 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 827 | kcb->jprobe_saved_regs = *regs; |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 828 | kcb->jprobe_saved_rsp = (long *) regs->sp; |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 829 | addr = (unsigned long)(kcb->jprobe_saved_rsp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | /* |
| 831 | * As Linus pointed out, gcc assumes that the callee |
| 832 | * owns the argument space and could overwrite it, e.g. |
| 833 | * tailcall optimization. So, to be absolutely safe |
| 834 | * we also save and restore enough stack bytes to cover |
| 835 | * the argument area. |
| 836 | */ |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 837 | memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr, |
| 838 | MIN_STACK_SIZE(addr)); |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 839 | regs->flags &= ~IF_MASK; |
Peter Zijlstra | 58dfe88 | 2007-10-11 22:25:25 +0200 | [diff] [blame] | 840 | trace_hardirqs_off(); |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 841 | regs->ip = (unsigned long)(jp->entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | return 1; |
| 843 | } |
| 844 | |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 845 | void __kprobes jprobe_return(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | { |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 847 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
| 848 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | asm volatile (" xchg %%rbx,%%rsp \n" |
| 850 | " int3 \n" |
| 851 | " .globl jprobe_return_end \n" |
| 852 | " jprobe_return_end: \n" |
| 853 | " nop \n"::"b" |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 854 | (kcb->jprobe_saved_rsp):"memory"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | } |
| 856 | |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 857 | int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | { |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 859 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 860 | u8 *addr = (u8 *) (regs->ip - 1); |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 861 | unsigned long stack_addr = (unsigned long)(kcb->jprobe_saved_rsp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | struct jprobe *jp = container_of(p, struct jprobe, kp); |
| 863 | |
| 864 | if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) { |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 865 | if ((unsigned long *)regs->sp != kcb->jprobe_saved_rsp) { |
Masami Hiramatsu | 29b6cd7 | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 866 | struct pt_regs *saved_regs = &kcb->jprobe_saved_regs; |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 867 | printk("current sp %p does not match saved sp %p\n", |
| 868 | (long *)regs->sp, kcb->jprobe_saved_rsp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | printk("Saved registers for jprobe %p\n", jp); |
| 870 | show_registers(saved_regs); |
| 871 | printk("Current registers\n"); |
| 872 | show_registers(regs); |
| 873 | BUG(); |
| 874 | } |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 875 | *regs = kcb->jprobe_saved_regs; |
| 876 | memcpy((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | MIN_STACK_SIZE(stack_addr)); |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 878 | preempt_enable_no_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | return 1; |
| 880 | } |
| 881 | return 0; |
| 882 | } |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 883 | |
| 884 | static struct kprobe trampoline_p = { |
| 885 | .addr = (kprobe_opcode_t *) &kretprobe_trampoline, |
| 886 | .pre_handler = trampoline_probe_handler |
| 887 | }; |
| 888 | |
Rusty Lynch | 6772926 | 2005-07-05 18:54:50 -0700 | [diff] [blame] | 889 | int __init arch_init_kprobes(void) |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 890 | { |
| 891 | return register_kprobe(&trampoline_p); |
| 892 | } |
Ananth N Mavinakayanahalli | bf8f6e5 | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 893 | |
| 894 | int __kprobes arch_trampoline_kprobe(struct kprobe *p) |
| 895 | { |
| 896 | if (p->addr == (kprobe_opcode_t *)&kretprobe_trampoline) |
| 897 | return 1; |
| 898 | |
| 899 | return 0; |
| 900 | } |