]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Kernel Probes (KProbes) | |
1da177e4 LT |
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. | |
d6be29b8 MH |
25 | * 2004-Oct Jim Keniston <jkenisto@us.ibm.com> and Prasanna S Panchamukhi |
26 | * <prasanna@in.ibm.com> adapted for x86_64 from i386. | |
1da177e4 LT |
27 | * 2005-Mar Roland McGrath <roland@redhat.com> |
28 | * Fixed to handle %rip-relative addressing mode correctly. | |
d6be29b8 MH |
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. | |
da07ab03 MH |
36 | * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com> added kprobe-booster |
37 | * and kretprobe-booster for x86-64 | |
d6be29b8 MH |
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. | |
1da177e4 LT |
41 | */ |
42 | ||
1da177e4 LT |
43 | #include <linux/kprobes.h> |
44 | #include <linux/ptrace.h> | |
1da177e4 LT |
45 | #include <linux/string.h> |
46 | #include <linux/slab.h> | |
47 | #include <linux/preempt.h> | |
c28f8966 | 48 | #include <linux/module.h> |
1eeb66a1 | 49 | #include <linux/kdebug.h> |
9ec4b1f3 | 50 | |
8533bbe9 MH |
51 | #include <asm/cacheflush.h> |
52 | #include <asm/desc.h> | |
1da177e4 | 53 | #include <asm/pgtable.h> |
c28f8966 | 54 | #include <asm/uaccess.h> |
19d36ccd | 55 | #include <asm/alternative.h> |
1da177e4 | 56 | |
1da177e4 LT |
57 | void jprobe_return_end(void); |
58 | ||
e7a510f9 AM |
59 | DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL; |
60 | DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); | |
1da177e4 | 61 | |
d6be29b8 | 62 | #ifdef CONFIG_X86_64 |
8533bbe9 | 63 | #define stack_addr(regs) ((unsigned long *)regs->sp) |
d6be29b8 MH |
64 | #else |
65 | /* | |
66 | * "®s->sp" looks wrong, but it's correct for x86_32. x86_32 CPUs | |
67 | * don't save the ss and esp registers if the CPU is already in kernel | |
68 | * mode when it traps. So for kprobes, regs->sp and regs->ss are not | |
69 | * the [nonexistent] saved stack pointer and ss register, but rather | |
70 | * the top 8 bytes of the pre-int3 stack. So ®s->sp happens to | |
71 | * point to the top of the pre-int3 stack. | |
72 | */ | |
73 | #define stack_addr(regs) ((unsigned long *)®s->sp) | |
74 | #endif | |
8533bbe9 MH |
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 % 32)) | |
82 | /* | |
83 | * Undefined/reserved opcodes, conditional jump, Opcode Extension | |
84 | * Groups, and some special opcodes can not boost. | |
85 | */ | |
86 | static const u32 twobyte_is_boostable[256 / 32] = { | |
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 | static const u32 onebyte_has_modrm[256 / 32] = { | |
109 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ | |
110 | /* ----------------------------------------------- */ | |
111 | W(0x00, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* 00 */ | |
112 | W(0x10, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) , /* 10 */ | |
113 | W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* 20 */ | |
114 | W(0x30, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) , /* 30 */ | |
115 | W(0x40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 40 */ | |
116 | W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */ | |
117 | W(0x60, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0) | /* 60 */ | |
118 | W(0x70, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 70 */ | |
119 | W(0x80, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 80 */ | |
120 | W(0x90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 90 */ | |
121 | W(0xa0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* a0 */ | |
122 | W(0xb0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* b0 */ | |
123 | W(0xc0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0) | /* c0 */ | |
124 | W(0xd0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) , /* d0 */ | |
125 | W(0xe0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* e0 */ | |
126 | W(0xf0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) /* f0 */ | |
127 | /* ----------------------------------------------- */ | |
128 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ | |
129 | }; | |
130 | static const u32 twobyte_has_modrm[256 / 32] = { | |
131 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ | |
132 | /* ----------------------------------------------- */ | |
133 | W(0x00, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1) | /* 0f */ | |
134 | W(0x10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0) , /* 1f */ | |
135 | W(0x20, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* 2f */ | |
136 | W(0x30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 3f */ | |
137 | W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 4f */ | |
138 | W(0x50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 5f */ | |
139 | W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 6f */ | |
140 | W(0x70, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1) , /* 7f */ | |
141 | W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 8f */ | |
142 | W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 9f */ | |
143 | W(0xa0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) | /* af */ | |
144 | W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1) , /* bf */ | |
145 | W(0xc0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0) | /* cf */ | |
146 | W(0xd0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* df */ | |
147 | W(0xe0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* ef */ | |
148 | W(0xf0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0) /* ff */ | |
149 | /* ----------------------------------------------- */ | |
150 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ | |
151 | }; | |
152 | #undef W | |
153 | ||
f438d914 MH |
154 | struct kretprobe_blackpoint kretprobe_blacklist[] = { |
155 | {"__switch_to", }, /* This function switches only current task, but | |
156 | doesn't switch kernel stack.*/ | |
157 | {NULL, NULL} /* Terminator */ | |
158 | }; | |
159 | const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist); | |
160 | ||
aa470140 | 161 | /* Insert a jump instruction at address 'from', which jumps to address 'to'.*/ |
e7b5e11e | 162 | static void __kprobes set_jmp_op(void *from, void *to) |
aa470140 MH |
163 | { |
164 | struct __arch_jmp_op { | |
165 | char op; | |
166 | s32 raddr; | |
167 | } __attribute__((packed)) * jop; | |
168 | jop = (struct __arch_jmp_op *)from; | |
169 | jop->raddr = (s32)((long)(to) - ((long)(from) + 5)); | |
170 | jop->op = RELATIVEJUMP_INSTRUCTION; | |
171 | } | |
172 | ||
9930927f HH |
173 | /* |
174 | * Check for the REX prefix which can only exist on X86_64 | |
175 | * X86_32 always returns 0 | |
176 | */ | |
177 | static int __kprobes is_REX_prefix(kprobe_opcode_t *insn) | |
178 | { | |
179 | #ifdef CONFIG_X86_64 | |
180 | if ((*insn & 0xf0) == 0x40) | |
181 | return 1; | |
182 | #endif | |
183 | return 0; | |
184 | } | |
185 | ||
aa470140 | 186 | /* |
d6be29b8 MH |
187 | * Returns non-zero if opcode is boostable. |
188 | * RIP relative instructions are adjusted at copying time in 64 bits mode | |
aa470140 | 189 | */ |
e7b5e11e | 190 | static int __kprobes can_boost(kprobe_opcode_t *opcodes) |
aa470140 | 191 | { |
aa470140 MH |
192 | kprobe_opcode_t opcode; |
193 | kprobe_opcode_t *orig_opcodes = opcodes; | |
194 | ||
195 | retry: | |
196 | if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1) | |
197 | return 0; | |
198 | opcode = *(opcodes++); | |
199 | ||
200 | /* 2nd-byte opcode */ | |
201 | if (opcode == 0x0f) { | |
202 | if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1) | |
203 | return 0; | |
8533bbe9 MH |
204 | return test_bit(*opcodes, |
205 | (unsigned long *)twobyte_is_boostable); | |
aa470140 MH |
206 | } |
207 | ||
208 | switch (opcode & 0xf0) { | |
d6be29b8 | 209 | #ifdef CONFIG_X86_64 |
aa470140 MH |
210 | case 0x40: |
211 | goto retry; /* REX prefix is boostable */ | |
d6be29b8 | 212 | #endif |
aa470140 MH |
213 | case 0x60: |
214 | if (0x63 < opcode && opcode < 0x67) | |
215 | goto retry; /* prefixes */ | |
216 | /* can't boost Address-size override and bound */ | |
217 | return (opcode != 0x62 && opcode != 0x67); | |
218 | case 0x70: | |
219 | return 0; /* can't boost conditional jump */ | |
220 | case 0xc0: | |
221 | /* can't boost software-interruptions */ | |
222 | return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf; | |
223 | case 0xd0: | |
224 | /* can boost AA* and XLAT */ | |
225 | return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7); | |
226 | case 0xe0: | |
227 | /* can boost in/out and absolute jmps */ | |
228 | return ((opcode & 0x04) || opcode == 0xea); | |
229 | case 0xf0: | |
230 | if ((opcode & 0x0c) == 0 && opcode != 0xf1) | |
231 | goto retry; /* lock/rep(ne) prefix */ | |
232 | /* clear and set flags are boostable */ | |
233 | return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe)); | |
234 | default: | |
235 | /* segment override prefixes are boostable */ | |
236 | if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e) | |
237 | goto retry; /* prefixes */ | |
238 | /* CS override prefix and call are not boostable */ | |
239 | return (opcode != 0x2e && opcode != 0x9a); | |
240 | } | |
241 | } | |
242 | ||
1da177e4 | 243 | /* |
d6be29b8 | 244 | * Returns non-zero if opcode modifies the interrupt flag. |
1da177e4 | 245 | */ |
8645419c | 246 | static int __kprobes is_IF_modifier(kprobe_opcode_t *insn) |
1da177e4 LT |
247 | { |
248 | switch (*insn) { | |
249 | case 0xfa: /* cli */ | |
250 | case 0xfb: /* sti */ | |
251 | case 0xcf: /* iret/iretd */ | |
252 | case 0x9d: /* popf/popfd */ | |
253 | return 1; | |
254 | } | |
9930927f | 255 | |
8533bbe9 | 256 | /* |
9930927f | 257 | * on X86_64, 0x40-0x4f are REX prefixes so we need to look |
8533bbe9 MH |
258 | * at the next byte instead.. but of course not recurse infinitely |
259 | */ | |
9930927f | 260 | if (is_REX_prefix(insn)) |
8533bbe9 | 261 | return is_IF_modifier(++insn); |
9930927f | 262 | |
1da177e4 LT |
263 | return 0; |
264 | } | |
265 | ||
266 | /* | |
8533bbe9 MH |
267 | * Adjust the displacement if the instruction uses the %rip-relative |
268 | * addressing mode. | |
aa470140 | 269 | * If it does, Return the address of the 32-bit displacement word. |
1da177e4 | 270 | * If not, return null. |
31f80e45 | 271 | * Only applicable to 64-bit x86. |
1da177e4 | 272 | */ |
8533bbe9 | 273 | static void __kprobes fix_riprel(struct kprobe *p) |
1da177e4 | 274 | { |
31f80e45 | 275 | #ifdef CONFIG_X86_64 |
8533bbe9 MH |
276 | u8 *insn = p->ainsn.insn; |
277 | s64 disp; | |
1da177e4 LT |
278 | int need_modrm; |
279 | ||
280 | /* Skip legacy instruction prefixes. */ | |
281 | while (1) { | |
282 | switch (*insn) { | |
283 | case 0x66: | |
284 | case 0x67: | |
285 | case 0x2e: | |
286 | case 0x3e: | |
287 | case 0x26: | |
288 | case 0x64: | |
289 | case 0x65: | |
290 | case 0x36: | |
291 | case 0xf0: | |
292 | case 0xf3: | |
293 | case 0xf2: | |
294 | ++insn; | |
295 | continue; | |
296 | } | |
297 | break; | |
298 | } | |
299 | ||
300 | /* Skip REX instruction prefix. */ | |
9930927f | 301 | if (is_REX_prefix(insn)) |
1da177e4 LT |
302 | ++insn; |
303 | ||
d6be29b8 MH |
304 | if (*insn == 0x0f) { |
305 | /* Two-byte opcode. */ | |
1da177e4 | 306 | ++insn; |
8533bbe9 MH |
307 | need_modrm = test_bit(*insn, |
308 | (unsigned long *)twobyte_has_modrm); | |
d6be29b8 MH |
309 | } else |
310 | /* One-byte opcode. */ | |
8533bbe9 MH |
311 | need_modrm = test_bit(*insn, |
312 | (unsigned long *)onebyte_has_modrm); | |
1da177e4 LT |
313 | |
314 | if (need_modrm) { | |
315 | u8 modrm = *++insn; | |
d6be29b8 MH |
316 | if ((modrm & 0xc7) == 0x05) { |
317 | /* %rip+disp32 addressing mode */ | |
1da177e4 | 318 | /* Displacement follows ModRM byte. */ |
8533bbe9 MH |
319 | ++insn; |
320 | /* | |
321 | * The copied instruction uses the %rip-relative | |
322 | * addressing mode. Adjust the displacement for the | |
323 | * difference between the original location of this | |
324 | * instruction and the location of the copy that will | |
325 | * actually be run. The tricky bit here is making sure | |
326 | * that the sign extension happens correctly in this | |
327 | * calculation, since we need a signed 32-bit result to | |
328 | * be sign-extended to 64 bits when it's added to the | |
329 | * %rip value and yield the same 64-bit result that the | |
330 | * sign-extension of the original signed 32-bit | |
331 | * displacement would have given. | |
332 | */ | |
333 | disp = (u8 *) p->addr + *((s32 *) insn) - | |
334 | (u8 *) p->ainsn.insn; | |
335 | BUG_ON((s64) (s32) disp != disp); /* Sanity check. */ | |
336 | *(s32 *)insn = (s32) disp; | |
1da177e4 LT |
337 | } |
338 | } | |
d6be29b8 | 339 | #endif |
31f80e45 | 340 | } |
1da177e4 | 341 | |
f709b122 | 342 | static void __kprobes arch_copy_kprobe(struct kprobe *p) |
1da177e4 | 343 | { |
8533bbe9 | 344 | memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t)); |
31f80e45 | 345 | |
8533bbe9 | 346 | fix_riprel(p); |
31f80e45 | 347 | |
8533bbe9 | 348 | if (can_boost(p->addr)) |
aa470140 | 349 | p->ainsn.boostable = 0; |
8533bbe9 | 350 | else |
aa470140 | 351 | p->ainsn.boostable = -1; |
8533bbe9 | 352 | |
7e1048b1 | 353 | p->opcode = *p->addr; |
1da177e4 LT |
354 | } |
355 | ||
8533bbe9 MH |
356 | int __kprobes arch_prepare_kprobe(struct kprobe *p) |
357 | { | |
358 | /* insn: must be on special executable page on x86. */ | |
359 | p->ainsn.insn = get_insn_slot(); | |
360 | if (!p->ainsn.insn) | |
361 | return -ENOMEM; | |
362 | arch_copy_kprobe(p); | |
363 | return 0; | |
364 | } | |
365 | ||
0f2fbdcb | 366 | void __kprobes arch_arm_kprobe(struct kprobe *p) |
1da177e4 | 367 | { |
19d36ccd | 368 | text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1); |
1da177e4 LT |
369 | } |
370 | ||
0f2fbdcb | 371 | void __kprobes arch_disarm_kprobe(struct kprobe *p) |
1da177e4 | 372 | { |
19d36ccd | 373 | text_poke(p->addr, &p->opcode, 1); |
7e1048b1 RL |
374 | } |
375 | ||
0498b635 | 376 | void __kprobes arch_remove_kprobe(struct kprobe *p) |
7e1048b1 | 377 | { |
7a7d1cf9 | 378 | mutex_lock(&kprobe_mutex); |
aa470140 | 379 | free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1)); |
7a7d1cf9 | 380 | mutex_unlock(&kprobe_mutex); |
1da177e4 LT |
381 | } |
382 | ||
3b60211c | 383 | static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb) |
aa3d7e3d | 384 | { |
e7a510f9 AM |
385 | kcb->prev_kprobe.kp = kprobe_running(); |
386 | kcb->prev_kprobe.status = kcb->kprobe_status; | |
8533bbe9 MH |
387 | kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags; |
388 | kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags; | |
aa3d7e3d PP |
389 | } |
390 | ||
3b60211c | 391 | static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb) |
aa3d7e3d | 392 | { |
e7a510f9 AM |
393 | __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp; |
394 | kcb->kprobe_status = kcb->prev_kprobe.status; | |
8533bbe9 MH |
395 | kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags; |
396 | kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags; | |
aa3d7e3d PP |
397 | } |
398 | ||
3b60211c | 399 | static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs, |
e7a510f9 | 400 | struct kprobe_ctlblk *kcb) |
aa3d7e3d | 401 | { |
e7a510f9 | 402 | __get_cpu_var(current_kprobe) = p; |
8533bbe9 | 403 | kcb->kprobe_saved_flags = kcb->kprobe_old_flags |
053de044 | 404 | = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF)); |
aa3d7e3d | 405 | if (is_IF_modifier(p->ainsn.insn)) |
053de044 | 406 | kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF; |
aa3d7e3d PP |
407 | } |
408 | ||
e7b5e11e | 409 | static void __kprobes clear_btf(void) |
1ecc798c RM |
410 | { |
411 | if (test_thread_flag(TIF_DEBUGCTLMSR)) | |
d6be29b8 | 412 | wrmsr(MSR_IA32_DEBUGCTLMSR, 0, 0); |
1ecc798c RM |
413 | } |
414 | ||
e7b5e11e | 415 | static void __kprobes restore_btf(void) |
1ecc798c RM |
416 | { |
417 | if (test_thread_flag(TIF_DEBUGCTLMSR)) | |
d6be29b8 | 418 | wrmsr(MSR_IA32_DEBUGCTLMSR, current->thread.debugctlmsr, 0); |
1ecc798c RM |
419 | } |
420 | ||
0f2fbdcb | 421 | static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs) |
1da177e4 | 422 | { |
1ecc798c | 423 | clear_btf(); |
053de044 GOC |
424 | regs->flags |= X86_EFLAGS_TF; |
425 | regs->flags &= ~X86_EFLAGS_IF; | |
e7b5e11e | 426 | /* single step inline if the instruction is an int3 */ |
1da177e4 | 427 | if (p->opcode == BREAKPOINT_INSTRUCTION) |
65ea5b03 | 428 | regs->ip = (unsigned long)p->addr; |
1da177e4 | 429 | else |
65ea5b03 | 430 | regs->ip = (unsigned long)p->ainsn.insn; |
1da177e4 LT |
431 | } |
432 | ||
991a51d8 | 433 | /* Called with kretprobe_lock held */ |
4c4308cb | 434 | void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri, |
0f2fbdcb | 435 | struct pt_regs *regs) |
73649dab | 436 | { |
8533bbe9 | 437 | unsigned long *sara = stack_addr(regs); |
ba8af12f | 438 | |
4c4308cb | 439 | ri->ret_addr = (kprobe_opcode_t *) *sara; |
8533bbe9 | 440 | |
4c4308cb CH |
441 | /* Replace the return addr with trampoline addr */ |
442 | *sara = (unsigned long) &kretprobe_trampoline; | |
73649dab | 443 | } |
40102d4a HH |
444 | /* |
445 | * We have reentered the kprobe_handler(), since another probe was hit while | |
446 | * within the handler. We save the original kprobes variables and just single | |
447 | * step on the instruction of the new probe without calling any user handlers. | |
448 | */ | |
59e87cdc MH |
449 | static int __kprobes reenter_kprobe(struct kprobe *p, struct pt_regs *regs, |
450 | struct kprobe_ctlblk *kcb) | |
40102d4a | 451 | { |
59e87cdc MH |
452 | if (kcb->kprobe_status == KPROBE_HIT_SS && |
453 | *p->ainsn.insn == BREAKPOINT_INSTRUCTION) { | |
454 | regs->flags &= ~X86_EFLAGS_TF; | |
455 | regs->flags |= kcb->kprobe_saved_flags; | |
456 | return 0; | |
457 | #ifdef CONFIG_X86_64 | |
458 | } else if (kcb->kprobe_status == KPROBE_HIT_SSDONE) { | |
459 | /* TODO: Provide re-entrancy from post_kprobes_handler() and | |
460 | * avoid exception stack corruption while single-stepping on | |
461 | * the instruction of the new probe. | |
462 | */ | |
463 | arch_disarm_kprobe(p); | |
464 | regs->ip = (unsigned long)p->addr; | |
465 | reset_current_kprobe(); | |
466 | return 1; | |
467 | #endif | |
468 | } | |
40102d4a HH |
469 | save_previous_kprobe(kcb); |
470 | set_current_kprobe(p, regs, kcb); | |
471 | kprobes_inc_nmissed_count(p); | |
472 | prepare_singlestep(p, regs); | |
473 | kcb->kprobe_status = KPROBE_REENTER; | |
59e87cdc | 474 | return 1; |
40102d4a | 475 | } |
73649dab | 476 | |
8533bbe9 MH |
477 | /* |
478 | * Interrupts are disabled on entry as trap3 is an interrupt gate and they | |
479 | * remain disabled thorough out this function. | |
480 | */ | |
481 | static int __kprobes kprobe_handler(struct pt_regs *regs) | |
1da177e4 LT |
482 | { |
483 | struct kprobe *p; | |
484 | int ret = 0; | |
8533bbe9 | 485 | kprobe_opcode_t *addr; |
d217d545 AM |
486 | struct kprobe_ctlblk *kcb; |
487 | ||
8533bbe9 MH |
488 | addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t)); |
489 | ||
d217d545 AM |
490 | /* |
491 | * We don't want to be preempted for the entire | |
492 | * duration of kprobe processing | |
493 | */ | |
494 | preempt_disable(); | |
495 | kcb = get_kprobe_ctlblk(); | |
1da177e4 | 496 | |
1da177e4 LT |
497 | /* Check we're not actually recursing */ |
498 | if (kprobe_running()) { | |
1da177e4 LT |
499 | p = get_kprobe(addr); |
500 | if (p) { | |
59e87cdc MH |
501 | ret = reenter_kprobe(p, regs, kcb); |
502 | if (kcb->kprobe_status == KPROBE_REENTER) | |
503 | return 1; | |
1da177e4 | 504 | } else { |
eb3a7292 KA |
505 | if (*addr != BREAKPOINT_INSTRUCTION) { |
506 | /* The breakpoint instruction was removed by | |
507 | * another cpu right after we hit, no further | |
508 | * handling of this interrupt is appropriate | |
509 | */ | |
65ea5b03 | 510 | regs->ip = (unsigned long)addr; |
eb3a7292 KA |
511 | ret = 1; |
512 | goto no_kprobe; | |
513 | } | |
e7a510f9 | 514 | p = __get_cpu_var(current_kprobe); |
d6be29b8 | 515 | if (p->break_handler && p->break_handler(p, regs)) |
1da177e4 | 516 | goto ss_probe; |
1da177e4 | 517 | } |
1da177e4 LT |
518 | goto no_kprobe; |
519 | } | |
520 | ||
1da177e4 LT |
521 | p = get_kprobe(addr); |
522 | if (!p) { | |
1da177e4 LT |
523 | if (*addr != BREAKPOINT_INSTRUCTION) { |
524 | /* | |
525 | * The breakpoint instruction was removed right | |
526 | * after we hit it. Another cpu has removed | |
527 | * either a probepoint or a debugger breakpoint | |
528 | * at this address. In either case, no further | |
529 | * handling of this interrupt is appropriate. | |
bce06494 JK |
530 | * Back up over the (now missing) int3 and run |
531 | * the original instruction. | |
1da177e4 | 532 | */ |
65ea5b03 | 533 | regs->ip = (unsigned long)addr; |
1da177e4 LT |
534 | ret = 1; |
535 | } | |
536 | /* Not one of ours: let kernel handle it */ | |
537 | goto no_kprobe; | |
538 | } | |
539 | ||
e7a510f9 AM |
540 | set_current_kprobe(p, regs, kcb); |
541 | kcb->kprobe_status = KPROBE_HIT_ACTIVE; | |
1da177e4 LT |
542 | |
543 | if (p->pre_handler && p->pre_handler(p, regs)) | |
544 | /* handler has already set things up, so skip ss setup */ | |
545 | return 1; | |
546 | ||
547 | ss_probe: | |
aa470140 MH |
548 | #if !defined(CONFIG_PREEMPT) || defined(CONFIG_PM) |
549 | if (p->ainsn.boostable == 1 && !p->post_handler) { | |
550 | /* Boost up -- we can execute copied instructions directly */ | |
551 | reset_current_kprobe(); | |
552 | regs->ip = (unsigned long)p->ainsn.insn; | |
553 | preempt_enable_no_resched(); | |
554 | return 1; | |
555 | } | |
556 | #endif | |
1da177e4 | 557 | prepare_singlestep(p, regs); |
e7a510f9 | 558 | kcb->kprobe_status = KPROBE_HIT_SS; |
1da177e4 LT |
559 | return 1; |
560 | ||
561 | no_kprobe: | |
d217d545 | 562 | preempt_enable_no_resched(); |
1da177e4 LT |
563 | return ret; |
564 | } | |
565 | ||
73649dab | 566 | /* |
da07ab03 MH |
567 | * When a retprobed function returns, this code saves registers and |
568 | * calls trampoline_handler() runs, which calls the kretprobe's handler. | |
73649dab | 569 | */ |
da07ab03 | 570 | void __kprobes kretprobe_trampoline_holder(void) |
73649dab | 571 | { |
d6be29b8 MH |
572 | asm volatile ( |
573 | ".global kretprobe_trampoline\n" | |
da07ab03 | 574 | "kretprobe_trampoline: \n" |
d6be29b8 | 575 | #ifdef CONFIG_X86_64 |
da07ab03 MH |
576 | /* We don't bother saving the ss register */ |
577 | " pushq %rsp\n" | |
578 | " pushfq\n" | |
579 | /* | |
580 | * Skip cs, ip, orig_ax. | |
581 | * trampoline_handler() will plug in these values | |
582 | */ | |
583 | " subq $24, %rsp\n" | |
584 | " pushq %rdi\n" | |
585 | " pushq %rsi\n" | |
586 | " pushq %rdx\n" | |
587 | " pushq %rcx\n" | |
588 | " pushq %rax\n" | |
589 | " pushq %r8\n" | |
590 | " pushq %r9\n" | |
591 | " pushq %r10\n" | |
592 | " pushq %r11\n" | |
593 | " pushq %rbx\n" | |
594 | " pushq %rbp\n" | |
595 | " pushq %r12\n" | |
596 | " pushq %r13\n" | |
597 | " pushq %r14\n" | |
598 | " pushq %r15\n" | |
599 | " movq %rsp, %rdi\n" | |
600 | " call trampoline_handler\n" | |
601 | /* Replace saved sp with true return address. */ | |
602 | " movq %rax, 152(%rsp)\n" | |
603 | " popq %r15\n" | |
604 | " popq %r14\n" | |
605 | " popq %r13\n" | |
606 | " popq %r12\n" | |
607 | " popq %rbp\n" | |
608 | " popq %rbx\n" | |
609 | " popq %r11\n" | |
610 | " popq %r10\n" | |
611 | " popq %r9\n" | |
612 | " popq %r8\n" | |
613 | " popq %rax\n" | |
614 | " popq %rcx\n" | |
615 | " popq %rdx\n" | |
616 | " popq %rsi\n" | |
617 | " popq %rdi\n" | |
618 | /* Skip orig_ax, ip, cs */ | |
619 | " addq $24, %rsp\n" | |
620 | " popfq\n" | |
d6be29b8 MH |
621 | #else |
622 | " pushf\n" | |
623 | /* | |
624 | * Skip cs, ip, orig_ax. | |
625 | * trampoline_handler() will plug in these values | |
626 | */ | |
627 | " subl $12, %esp\n" | |
628 | " pushl %fs\n" | |
629 | " pushl %ds\n" | |
630 | " pushl %es\n" | |
631 | " pushl %eax\n" | |
632 | " pushl %ebp\n" | |
633 | " pushl %edi\n" | |
634 | " pushl %esi\n" | |
635 | " pushl %edx\n" | |
636 | " pushl %ecx\n" | |
637 | " pushl %ebx\n" | |
638 | " movl %esp, %eax\n" | |
639 | " call trampoline_handler\n" | |
640 | /* Move flags to cs */ | |
641 | " movl 52(%esp), %edx\n" | |
642 | " movl %edx, 48(%esp)\n" | |
643 | /* Replace saved flags with true return address. */ | |
644 | " movl %eax, 52(%esp)\n" | |
645 | " popl %ebx\n" | |
646 | " popl %ecx\n" | |
647 | " popl %edx\n" | |
648 | " popl %esi\n" | |
649 | " popl %edi\n" | |
650 | " popl %ebp\n" | |
651 | " popl %eax\n" | |
652 | /* Skip ip, orig_ax, es, ds, fs */ | |
653 | " addl $20, %esp\n" | |
654 | " popf\n" | |
655 | #endif | |
da07ab03 | 656 | " ret\n"); |
73649dab RL |
657 | } |
658 | ||
659 | /* | |
da07ab03 | 660 | * Called from kretprobe_trampoline |
73649dab | 661 | */ |
d6be29b8 | 662 | void * __kprobes trampoline_handler(struct pt_regs *regs) |
73649dab | 663 | { |
62c27be0 | 664 | struct kretprobe_instance *ri = NULL; |
99219a3f | 665 | struct hlist_head *head, empty_rp; |
62c27be0 | 666 | struct hlist_node *node, *tmp; |
991a51d8 | 667 | unsigned long flags, orig_ret_address = 0; |
d6be29b8 | 668 | unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline; |
73649dab | 669 | |
99219a3f | 670 | INIT_HLIST_HEAD(&empty_rp); |
991a51d8 | 671 | spin_lock_irqsave(&kretprobe_lock, flags); |
62c27be0 | 672 | head = kretprobe_inst_table_head(current); |
8533bbe9 | 673 | /* fixup registers */ |
d6be29b8 | 674 | #ifdef CONFIG_X86_64 |
da07ab03 | 675 | regs->cs = __KERNEL_CS; |
d6be29b8 MH |
676 | #else |
677 | regs->cs = __KERNEL_CS | get_kernel_rpl(); | |
678 | #endif | |
da07ab03 | 679 | regs->ip = trampoline_address; |
8533bbe9 | 680 | regs->orig_ax = ~0UL; |
73649dab | 681 | |
ba8af12f RL |
682 | /* |
683 | * It is possible to have multiple instances associated with a given | |
8533bbe9 MH |
684 | * task either because multiple functions in the call path have |
685 | * return probes installed on them, and/or more then one | |
ba8af12f RL |
686 | * return probe was registered for a target function. |
687 | * | |
688 | * We can handle this because: | |
8533bbe9 | 689 | * - instances are always pushed into the head of the list |
ba8af12f | 690 | * - when multiple return probes are registered for the same |
8533bbe9 MH |
691 | * function, the (chronologically) first instance's ret_addr |
692 | * will be the real return address, and all the rest will | |
693 | * point to kretprobe_trampoline. | |
ba8af12f RL |
694 | */ |
695 | hlist_for_each_entry_safe(ri, node, tmp, head, hlist) { | |
62c27be0 | 696 | if (ri->task != current) |
ba8af12f | 697 | /* another task is sharing our hash bucket */ |
62c27be0 | 698 | continue; |
ba8af12f | 699 | |
da07ab03 MH |
700 | if (ri->rp && ri->rp->handler) { |
701 | __get_cpu_var(current_kprobe) = &ri->rp->kp; | |
702 | get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE; | |
ba8af12f | 703 | ri->rp->handler(ri, regs); |
da07ab03 MH |
704 | __get_cpu_var(current_kprobe) = NULL; |
705 | } | |
ba8af12f RL |
706 | |
707 | orig_ret_address = (unsigned long)ri->ret_addr; | |
99219a3f | 708 | recycle_rp_inst(ri, &empty_rp); |
ba8af12f RL |
709 | |
710 | if (orig_ret_address != trampoline_address) | |
711 | /* | |
712 | * This is the real return address. Any other | |
713 | * instances associated with this task are for | |
714 | * other calls deeper on the call stack | |
715 | */ | |
716 | break; | |
73649dab | 717 | } |
ba8af12f | 718 | |
0f95b7fc | 719 | kretprobe_assert(ri, orig_ret_address, trampoline_address); |
ba8af12f | 720 | |
991a51d8 | 721 | spin_unlock_irqrestore(&kretprobe_lock, flags); |
ba8af12f | 722 | |
99219a3f | 723 | hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) { |
724 | hlist_del(&ri->hlist); | |
725 | kfree(ri); | |
726 | } | |
da07ab03 | 727 | return (void *)orig_ret_address; |
73649dab RL |
728 | } |
729 | ||
1da177e4 LT |
730 | /* |
731 | * Called after single-stepping. p->addr is the address of the | |
732 | * instruction whose first byte has been replaced by the "int 3" | |
733 | * instruction. To avoid the SMP problems that can occur when we | |
734 | * temporarily put back the original opcode to single-step, we | |
735 | * single-stepped a copy of the instruction. The address of this | |
736 | * copy is p->ainsn.insn. | |
737 | * | |
738 | * This function prepares to return from the post-single-step | |
739 | * interrupt. We have to fix up the stack as follows: | |
740 | * | |
741 | * 0) Except in the case of absolute or indirect jump or call instructions, | |
65ea5b03 | 742 | * the new ip is relative to the copied instruction. We need to make |
1da177e4 LT |
743 | * it relative to the original instruction. |
744 | * | |
745 | * 1) If the single-stepped instruction was pushfl, then the TF and IF | |
65ea5b03 | 746 | * flags are set in the just-pushed flags, and may need to be cleared. |
1da177e4 LT |
747 | * |
748 | * 2) If the single-stepped instruction was a call, the return address | |
749 | * that is atop the stack is the address following the copied instruction. | |
750 | * We need to make it the address following the original instruction. | |
aa470140 MH |
751 | * |
752 | * If this is the first time we've single-stepped the instruction at | |
753 | * this probepoint, and the instruction is boostable, boost it: add a | |
754 | * jump instruction after the copied instruction, that jumps to the next | |
755 | * instruction after the probepoint. | |
1da177e4 | 756 | */ |
e7a510f9 AM |
757 | static void __kprobes resume_execution(struct kprobe *p, |
758 | struct pt_regs *regs, struct kprobe_ctlblk *kcb) | |
1da177e4 | 759 | { |
8533bbe9 MH |
760 | unsigned long *tos = stack_addr(regs); |
761 | unsigned long copy_ip = (unsigned long)p->ainsn.insn; | |
762 | unsigned long orig_ip = (unsigned long)p->addr; | |
1da177e4 LT |
763 | kprobe_opcode_t *insn = p->ainsn.insn; |
764 | ||
765 | /*skip the REX prefix*/ | |
9930927f | 766 | if (is_REX_prefix(insn)) |
1da177e4 LT |
767 | insn++; |
768 | ||
053de044 | 769 | regs->flags &= ~X86_EFLAGS_TF; |
1da177e4 | 770 | switch (*insn) { |
0b0122fa | 771 | case 0x9c: /* pushfl */ |
053de044 | 772 | *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF); |
8533bbe9 | 773 | *tos |= kcb->kprobe_old_flags; |
1da177e4 | 774 | break; |
0b0122fa MH |
775 | case 0xc2: /* iret/ret/lret */ |
776 | case 0xc3: | |
0b9e2cac | 777 | case 0xca: |
0b0122fa MH |
778 | case 0xcb: |
779 | case 0xcf: | |
780 | case 0xea: /* jmp absolute -- ip is correct */ | |
781 | /* ip is already adjusted, no more changes required */ | |
aa470140 | 782 | p->ainsn.boostable = 1; |
0b0122fa MH |
783 | goto no_change; |
784 | case 0xe8: /* call relative - Fix return addr */ | |
8533bbe9 | 785 | *tos = orig_ip + (*tos - copy_ip); |
1da177e4 | 786 | break; |
e7b5e11e | 787 | #ifdef CONFIG_X86_32 |
d6be29b8 MH |
788 | case 0x9a: /* call absolute -- same as call absolute, indirect */ |
789 | *tos = orig_ip + (*tos - copy_ip); | |
790 | goto no_change; | |
791 | #endif | |
1da177e4 | 792 | case 0xff: |
dc49e344 | 793 | if ((insn[1] & 0x30) == 0x10) { |
8533bbe9 MH |
794 | /* |
795 | * call absolute, indirect | |
796 | * Fix return addr; ip is correct. | |
797 | * But this is not boostable | |
798 | */ | |
799 | *tos = orig_ip + (*tos - copy_ip); | |
0b0122fa | 800 | goto no_change; |
8533bbe9 MH |
801 | } else if (((insn[1] & 0x31) == 0x20) || |
802 | ((insn[1] & 0x31) == 0x21)) { | |
803 | /* | |
804 | * jmp near and far, absolute indirect | |
805 | * ip is correct. And this is boostable | |
806 | */ | |
aa470140 | 807 | p->ainsn.boostable = 1; |
0b0122fa | 808 | goto no_change; |
1da177e4 | 809 | } |
1da177e4 LT |
810 | default: |
811 | break; | |
812 | } | |
813 | ||
aa470140 | 814 | if (p->ainsn.boostable == 0) { |
8533bbe9 MH |
815 | if ((regs->ip > copy_ip) && |
816 | (regs->ip - copy_ip) + 5 < MAX_INSN_SIZE) { | |
aa470140 MH |
817 | /* |
818 | * These instructions can be executed directly if it | |
819 | * jumps back to correct address. | |
820 | */ | |
821 | set_jmp_op((void *)regs->ip, | |
8533bbe9 | 822 | (void *)orig_ip + (regs->ip - copy_ip)); |
aa470140 MH |
823 | p->ainsn.boostable = 1; |
824 | } else { | |
825 | p->ainsn.boostable = -1; | |
826 | } | |
827 | } | |
828 | ||
8533bbe9 | 829 | regs->ip += orig_ip - copy_ip; |
65ea5b03 | 830 | |
0b0122fa | 831 | no_change: |
1ecc798c | 832 | restore_btf(); |
1da177e4 LT |
833 | } |
834 | ||
8533bbe9 MH |
835 | /* |
836 | * Interrupts are disabled on entry as trap1 is an interrupt gate and they | |
837 | * remain disabled thoroughout this function. | |
838 | */ | |
839 | static int __kprobes post_kprobe_handler(struct pt_regs *regs) | |
1da177e4 | 840 | { |
e7a510f9 AM |
841 | struct kprobe *cur = kprobe_running(); |
842 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); | |
843 | ||
844 | if (!cur) | |
1da177e4 LT |
845 | return 0; |
846 | ||
e7a510f9 AM |
847 | if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) { |
848 | kcb->kprobe_status = KPROBE_HIT_SSDONE; | |
849 | cur->post_handler(cur, regs, 0); | |
aa3d7e3d | 850 | } |
1da177e4 | 851 | |
e7a510f9 | 852 | resume_execution(cur, regs, kcb); |
8533bbe9 | 853 | regs->flags |= kcb->kprobe_saved_flags; |
65ea5b03 | 854 | trace_hardirqs_fixup_flags(regs->flags); |
1da177e4 | 855 | |
8533bbe9 | 856 | /* Restore back the original saved kprobes variables and continue. */ |
e7a510f9 AM |
857 | if (kcb->kprobe_status == KPROBE_REENTER) { |
858 | restore_previous_kprobe(kcb); | |
aa3d7e3d | 859 | goto out; |
aa3d7e3d | 860 | } |
e7a510f9 | 861 | reset_current_kprobe(); |
aa3d7e3d | 862 | out: |
1da177e4 LT |
863 | preempt_enable_no_resched(); |
864 | ||
865 | /* | |
65ea5b03 | 866 | * if somebody else is singlestepping across a probe point, flags |
1da177e4 LT |
867 | * will have TF set, in which case, continue the remaining processing |
868 | * of do_debug, as if this is not a probe hit. | |
869 | */ | |
053de044 | 870 | if (regs->flags & X86_EFLAGS_TF) |
1da177e4 LT |
871 | return 0; |
872 | ||
873 | return 1; | |
874 | } | |
875 | ||
0f2fbdcb | 876 | int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr) |
1da177e4 | 877 | { |
e7a510f9 AM |
878 | struct kprobe *cur = kprobe_running(); |
879 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); | |
880 | ||
d6be29b8 | 881 | switch (kcb->kprobe_status) { |
c28f8966 PP |
882 | case KPROBE_HIT_SS: |
883 | case KPROBE_REENTER: | |
884 | /* | |
885 | * We are here because the instruction being single | |
886 | * stepped caused a page fault. We reset the current | |
65ea5b03 | 887 | * kprobe and the ip points back to the probe address |
c28f8966 PP |
888 | * and allow the page fault handler to continue as a |
889 | * normal page fault. | |
890 | */ | |
65ea5b03 | 891 | regs->ip = (unsigned long)cur->addr; |
8533bbe9 | 892 | regs->flags |= kcb->kprobe_old_flags; |
c28f8966 PP |
893 | if (kcb->kprobe_status == KPROBE_REENTER) |
894 | restore_previous_kprobe(kcb); | |
895 | else | |
896 | reset_current_kprobe(); | |
1da177e4 | 897 | preempt_enable_no_resched(); |
c28f8966 PP |
898 | break; |
899 | case KPROBE_HIT_ACTIVE: | |
900 | case KPROBE_HIT_SSDONE: | |
901 | /* | |
902 | * We increment the nmissed count for accounting, | |
8533bbe9 | 903 | * we can also use npre/npostfault count for accounting |
c28f8966 PP |
904 | * these specific fault cases. |
905 | */ | |
906 | kprobes_inc_nmissed_count(cur); | |
907 | ||
908 | /* | |
909 | * We come here because instructions in the pre/post | |
910 | * handler caused the page_fault, this could happen | |
911 | * if handler tries to access user space by | |
912 | * copy_from_user(), get_user() etc. Let the | |
913 | * user-specified handler try to fix it first. | |
914 | */ | |
915 | if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr)) | |
916 | return 1; | |
917 | ||
918 | /* | |
919 | * In case the user-specified fault handler returned | |
920 | * zero, try to fix up. | |
921 | */ | |
d6be29b8 MH |
922 | if (fixup_exception(regs)) |
923 | return 1; | |
6d48583b | 924 | |
c28f8966 | 925 | /* |
8533bbe9 | 926 | * fixup routine could not handle it, |
c28f8966 PP |
927 | * Let do_page_fault() fix it. |
928 | */ | |
929 | break; | |
930 | default: | |
931 | break; | |
1da177e4 LT |
932 | } |
933 | return 0; | |
934 | } | |
935 | ||
936 | /* | |
937 | * Wrapper routine for handling exceptions. | |
938 | */ | |
0f2fbdcb PP |
939 | int __kprobes kprobe_exceptions_notify(struct notifier_block *self, |
940 | unsigned long val, void *data) | |
1da177e4 LT |
941 | { |
942 | struct die_args *args = (struct die_args *)data; | |
66ff2d06 AM |
943 | int ret = NOTIFY_DONE; |
944 | ||
8533bbe9 | 945 | if (args->regs && user_mode_vm(args->regs)) |
2326c770 | 946 | return ret; |
947 | ||
1da177e4 LT |
948 | switch (val) { |
949 | case DIE_INT3: | |
950 | if (kprobe_handler(args->regs)) | |
66ff2d06 | 951 | ret = NOTIFY_STOP; |
1da177e4 LT |
952 | break; |
953 | case DIE_DEBUG: | |
954 | if (post_kprobe_handler(args->regs)) | |
66ff2d06 | 955 | ret = NOTIFY_STOP; |
1da177e4 LT |
956 | break; |
957 | case DIE_GPF: | |
d217d545 AM |
958 | /* kprobe_running() needs smp_processor_id() */ |
959 | preempt_disable(); | |
1da177e4 LT |
960 | if (kprobe_running() && |
961 | kprobe_fault_handler(args->regs, args->trapnr)) | |
66ff2d06 | 962 | ret = NOTIFY_STOP; |
d217d545 | 963 | preempt_enable(); |
1da177e4 LT |
964 | break; |
965 | default: | |
966 | break; | |
967 | } | |
66ff2d06 | 968 | return ret; |
1da177e4 LT |
969 | } |
970 | ||
0f2fbdcb | 971 | int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs) |
1da177e4 LT |
972 | { |
973 | struct jprobe *jp = container_of(p, struct jprobe, kp); | |
974 | unsigned long addr; | |
e7a510f9 | 975 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
1da177e4 | 976 | |
e7a510f9 | 977 | kcb->jprobe_saved_regs = *regs; |
8533bbe9 MH |
978 | kcb->jprobe_saved_sp = stack_addr(regs); |
979 | addr = (unsigned long)(kcb->jprobe_saved_sp); | |
980 | ||
1da177e4 LT |
981 | /* |
982 | * As Linus pointed out, gcc assumes that the callee | |
983 | * owns the argument space and could overwrite it, e.g. | |
984 | * tailcall optimization. So, to be absolutely safe | |
985 | * we also save and restore enough stack bytes to cover | |
986 | * the argument area. | |
987 | */ | |
e7a510f9 | 988 | memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr, |
d6be29b8 | 989 | MIN_STACK_SIZE(addr)); |
053de044 | 990 | regs->flags &= ~X86_EFLAGS_IF; |
58dfe883 | 991 | trace_hardirqs_off(); |
65ea5b03 | 992 | regs->ip = (unsigned long)(jp->entry); |
1da177e4 LT |
993 | return 1; |
994 | } | |
995 | ||
0f2fbdcb | 996 | void __kprobes jprobe_return(void) |
1da177e4 | 997 | { |
e7a510f9 AM |
998 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
999 | ||
d6be29b8 MH |
1000 | asm volatile ( |
1001 | #ifdef CONFIG_X86_64 | |
1002 | " xchg %%rbx,%%rsp \n" | |
1003 | #else | |
1004 | " xchgl %%ebx,%%esp \n" | |
1005 | #endif | |
1006 | " int3 \n" | |
1007 | " .globl jprobe_return_end\n" | |
1008 | " jprobe_return_end: \n" | |
1009 | " nop \n"::"b" | |
1010 | (kcb->jprobe_saved_sp):"memory"); | |
1da177e4 LT |
1011 | } |
1012 | ||
0f2fbdcb | 1013 | int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs) |
1da177e4 | 1014 | { |
e7a510f9 | 1015 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
65ea5b03 | 1016 | u8 *addr = (u8 *) (regs->ip - 1); |
1da177e4 LT |
1017 | struct jprobe *jp = container_of(p, struct jprobe, kp); |
1018 | ||
d6be29b8 MH |
1019 | if ((addr > (u8 *) jprobe_return) && |
1020 | (addr < (u8 *) jprobe_return_end)) { | |
8533bbe9 | 1021 | if (stack_addr(regs) != kcb->jprobe_saved_sp) { |
29b6cd79 | 1022 | struct pt_regs *saved_regs = &kcb->jprobe_saved_regs; |
d6be29b8 MH |
1023 | printk(KERN_ERR |
1024 | "current sp %p does not match saved sp %p\n", | |
8533bbe9 | 1025 | stack_addr(regs), kcb->jprobe_saved_sp); |
d6be29b8 | 1026 | printk(KERN_ERR "Saved registers for jprobe %p\n", jp); |
1da177e4 | 1027 | show_registers(saved_regs); |
d6be29b8 | 1028 | printk(KERN_ERR "Current registers\n"); |
1da177e4 LT |
1029 | show_registers(regs); |
1030 | BUG(); | |
1031 | } | |
e7a510f9 | 1032 | *regs = kcb->jprobe_saved_regs; |
8533bbe9 MH |
1033 | memcpy((kprobe_opcode_t *)(kcb->jprobe_saved_sp), |
1034 | kcb->jprobes_stack, | |
1035 | MIN_STACK_SIZE(kcb->jprobe_saved_sp)); | |
d217d545 | 1036 | preempt_enable_no_resched(); |
1da177e4 LT |
1037 | return 1; |
1038 | } | |
1039 | return 0; | |
1040 | } | |
ba8af12f | 1041 | |
6772926b | 1042 | int __init arch_init_kprobes(void) |
ba8af12f | 1043 | { |
da07ab03 | 1044 | return 0; |
ba8af12f | 1045 | } |
bf8f6e5b AM |
1046 | |
1047 | int __kprobes arch_trampoline_kprobe(struct kprobe *p) | |
1048 | { | |
bf8f6e5b AM |
1049 | return 0; |
1050 | } |