blob: 520b8dfe164026cf53b762f87b8f507401384969 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Kernel Probes (KProbes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * Copyright (C) IBM Corporation, 2002, 2004
19 *
20 * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
21 * Probes initial implementation ( includes contributions from
22 * Rusty Russell).
23 * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
24 * interface to access function arguments.
Masami Hiramatsud6be29b2008-01-30 13:31:21 +010025 * 2004-Oct Jim Keniston <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
26 * <prasanna@in.ibm.com> adapted for x86_64 from i386.
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 * 2005-Mar Roland McGrath <roland@redhat.com>
28 * Fixed to handle %rip-relative addressing mode correctly.
Masami Hiramatsud6be29b2008-01-30 13:31:21 +010029 * 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>
Masami Hiramatsu3f33ab12012-03-05 22:32:22 +090033 * Added function return probes functionality
Masami Hiramatsud6be29b2008-01-30 13:31:21 +010034 * 2006-Feb Masami Hiramatsu <hiramatu@sdl.hitachi.co.jp> added
Masami Hiramatsu3f33ab12012-03-05 22:32:22 +090035 * kprobe-booster and kretprobe-booster for i386.
Masami Hiramatsuda07ab02008-01-30 13:31:21 +010036 * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com> added kprobe-booster
Masami Hiramatsu3f33ab12012-03-05 22:32:22 +090037 * and kretprobe-booster for x86-64
Masami Hiramatsud6be29b2008-01-30 13:31:21 +010038 * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com>, Arjan van de Ven
Masami Hiramatsu3f33ab12012-03-05 22:32:22 +090039 * <arjan@infradead.org> and Jim Keniston <jkenisto@us.ibm.com>
40 * unified x86 kprobes code.
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/kprobes.h>
43#include <linux/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/string.h>
45#include <linux/slab.h>
Quentin Barnesb506a9d2008-01-30 13:32:32 +010046#include <linux/hardirq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/preempt.h>
Paul Gortmaker744c1932016-09-19 17:04:18 -040048#include <linux/extable.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070049#include <linux/kdebug.h>
Masami Hiramatsub46b3d72009-08-13 16:34:28 -040050#include <linux/kallsyms.h>
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -050051#include <linux/ftrace.h>
Josh Poimboeuf87aaff22016-02-28 22:22:40 -060052#include <linux/frame.h>
Dmitry Vyukov9f7d4162016-10-14 16:07:23 +020053#include <linux/kasan.h>
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070054
Andy Lutomirski35de5b02016-04-26 12:23:24 -070055#include <asm/text-patching.h>
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +010056#include <asm/cacheflush.h>
57#include <asm/desc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <asm/pgtable.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080059#include <linux/uaccess.h>
Andi Kleen19d36cc2007-07-22 11:12:31 +020060#include <asm/alternative.h>
Masami Hiramatsub46b3d72009-08-13 16:34:28 -040061#include <asm/insn.h>
K.Prasad62edab92009-06-01 23:47:06 +053062#include <asm/debugreg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Masami Hiramatsuf6841992012-09-28 17:15:22 +090064#include "common.h"
Masami Hiramatsu3f33ab12012-03-05 22:32:22 +090065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066void jprobe_return_end(void);
67
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -080068DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
69DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
H. Peter Anvin98272ed2009-10-12 14:14:10 -070071#define stack_addr(regs) ((unsigned long *)kernel_stack_pointer(regs))
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +010072
73#define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\
74 (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
75 (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
76 (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
77 (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
78 << (row % 32))
79 /*
80 * Undefined/reserved opcodes, conditional jump, Opcode Extension
81 * Groups, and some special opcodes can not boost.
Linus Torvalds7115e3f2011-10-26 17:03:38 +020082 * This is non-const and volatile to keep gcc from statically
83 * optimizing it out, as variable_test_bit makes gcc think only
Masami Hiramatsuf6841992012-09-28 17:15:22 +090084 * *(unsigned long*) is used.
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +010085 */
Linus Torvalds7115e3f2011-10-26 17:03:38 +020086static volatile u32 twobyte_is_boostable[256 / 32] = {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +010087 /* 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 */
Wang Nanb7e37562015-02-10 09:34:05 +080090 W(0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1) , /* 10 */
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +010091 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};
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100108#undef W
109
Masami Hiramatsuf438d912007-10-16 01:27:49 -0700110struct kretprobe_blackpoint kretprobe_blacklist[] = {
111 {"__switch_to", }, /* This function switches only current task, but
112 doesn't switch kernel stack.*/
113 {NULL, NULL} /* Terminator */
114};
Masami Hiramatsu3f33ab12012-03-05 22:32:22 +0900115
Masami Hiramatsuf438d912007-10-16 01:27:49 -0700116const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist);
117
Masami Hiramatsu93266382014-04-17 17:18:14 +0900118static nokprobe_inline void
119__synthesize_relative_insn(void *from, void *to, u8 op)
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100120{
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500121 struct __arch_relative_insn {
122 u8 op;
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100123 s32 raddr;
Masami Hiramatsuf6841992012-09-28 17:15:22 +0900124 } __packed *insn;
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500125
126 insn = (struct __arch_relative_insn *)from;
127 insn->raddr = (s32)((long)(to) - ((long)(from) + 5));
128 insn->op = op;
129}
130
131/* Insert a jump instruction at address 'from', which jumps to address 'to'.*/
Masami Hiramatsu93266382014-04-17 17:18:14 +0900132void synthesize_reljump(void *from, void *to)
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500133{
134 __synthesize_relative_insn(from, to, RELATIVEJUMP_OPCODE);
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100135}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900136NOKPROBE_SYMBOL(synthesize_reljump);
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100137
Masami Hiramatsu3f33ab12012-03-05 22:32:22 +0900138/* Insert a call instruction at address 'from', which calls address 'to'.*/
Masami Hiramatsu93266382014-04-17 17:18:14 +0900139void synthesize_relcall(void *from, void *to)
Masami Hiramatsu3f33ab12012-03-05 22:32:22 +0900140{
141 __synthesize_relative_insn(from, to, RELATIVECALL_OPCODE);
142}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900143NOKPROBE_SYMBOL(synthesize_relcall);
Masami Hiramatsu3f33ab12012-03-05 22:32:22 +0900144
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100145/*
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900146 * Skip the prefixes of the instruction.
Harvey Harrison99309272008-01-30 13:32:14 +0100147 */
Masami Hiramatsu93266382014-04-17 17:18:14 +0900148static kprobe_opcode_t *skip_prefixes(kprobe_opcode_t *insn)
Harvey Harrison99309272008-01-30 13:32:14 +0100149{
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900150 insn_attr_t attr;
151
152 attr = inat_get_opcode_attribute((insn_byte_t)*insn);
153 while (inat_is_legacy_prefix(attr)) {
154 insn++;
155 attr = inat_get_opcode_attribute((insn_byte_t)*insn);
156 }
Harvey Harrison99309272008-01-30 13:32:14 +0100157#ifdef CONFIG_X86_64
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900158 if (inat_is_rex_prefix(attr))
159 insn++;
Harvey Harrison99309272008-01-30 13:32:14 +0100160#endif
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900161 return insn;
Harvey Harrison99309272008-01-30 13:32:14 +0100162}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900163NOKPROBE_SYMBOL(skip_prefixes);
Harvey Harrison99309272008-01-30 13:32:14 +0100164
165/*
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100166 * Returns non-zero if opcode is boostable.
167 * RIP relative instructions are adjusted at copying time in 64 bits mode
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100168 */
Masami Hiramatsu7ec8a972014-04-17 17:17:47 +0900169int can_boost(kprobe_opcode_t *opcodes)
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100170{
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100171 kprobe_opcode_t opcode;
172 kprobe_opcode_t *orig_opcodes = opcodes;
173
Jaswinder Singh Rajputcde5edb2009-03-18 17:37:45 +0530174 if (search_exception_tables((unsigned long)opcodes))
Masami Hiramatsu30390882009-03-16 18:57:22 -0400175 return 0; /* Page fault may occur on this address. */
176
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100177retry:
178 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
179 return 0;
180 opcode = *(opcodes++);
181
182 /* 2nd-byte opcode */
183 if (opcode == 0x0f) {
184 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
185 return 0;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100186 return test_bit(*opcodes,
187 (unsigned long *)twobyte_is_boostable);
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100188 }
189
190 switch (opcode & 0xf0) {
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100191#ifdef CONFIG_X86_64
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100192 case 0x40:
193 goto retry; /* REX prefix is boostable */
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100194#endif
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100195 case 0x60:
196 if (0x63 < opcode && opcode < 0x67)
197 goto retry; /* prefixes */
198 /* can't boost Address-size override and bound */
199 return (opcode != 0x62 && opcode != 0x67);
200 case 0x70:
201 return 0; /* can't boost conditional jump */
202 case 0xc0:
203 /* can't boost software-interruptions */
204 return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
205 case 0xd0:
206 /* can boost AA* and XLAT */
207 return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
208 case 0xe0:
209 /* can boost in/out and absolute jmps */
210 return ((opcode & 0x04) || opcode == 0xea);
211 case 0xf0:
212 if ((opcode & 0x0c) == 0 && opcode != 0xf1)
213 goto retry; /* lock/rep(ne) prefix */
214 /* clear and set flags are boostable */
215 return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
216 default:
217 /* segment override prefixes are boostable */
218 if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e)
219 goto retry; /* prefixes */
220 /* CS override prefix and call are not boostable */
221 return (opcode != 0x2e && opcode != 0x9a);
222 }
223}
224
Masami Hiramatsu3f33ab12012-03-05 22:32:22 +0900225static unsigned long
226__recover_probed_insn(kprobe_opcode_t *buf, unsigned long addr)
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400227{
228 struct kprobe *kp;
Petr Mladek650b7b22015-02-20 15:07:29 +0100229 unsigned long faddr;
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900230
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400231 kp = get_kprobe((void *)addr);
Petr Mladek650b7b22015-02-20 15:07:29 +0100232 faddr = ftrace_location(addr);
233 /*
Petr Mladek2a6730c2015-02-20 15:07:30 +0100234 * Addresses inside the ftrace location are refused by
235 * arch_check_ftrace_location(). Something went terribly wrong
236 * if such an address is checked here.
237 */
238 if (WARN_ON(faddr && faddr != addr))
239 return 0UL;
240 /*
Petr Mladek650b7b22015-02-20 15:07:29 +0100241 * Use the current code if it is not modified by Kprobe
242 * and it cannot be modified by ftrace.
243 */
244 if (!kp && !faddr)
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900245 return addr;
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400246
247 /*
Petr Mladek650b7b22015-02-20 15:07:29 +0100248 * Basically, kp->ainsn.insn has an original instruction.
249 * However, RIP-relative instruction can not do single-stepping
250 * at different place, __copy_instruction() tweaks the displacement of
251 * that instruction. In that case, we can't recover the instruction
252 * from the kp->ainsn.insn.
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400253 *
Petr Mladek650b7b22015-02-20 15:07:29 +0100254 * On the other hand, in case on normal Kprobe, kp->opcode has a copy
255 * of the first byte of the probed instruction, which is overwritten
256 * by int3. And the instruction at kp->addr is not modified by kprobes
257 * except for the first byte, we can recover the original instruction
258 * from it and kp->opcode.
259 *
260 * In case of Kprobes using ftrace, we do not have a copy of
261 * the original instruction. In fact, the ftrace location might
262 * be modified at anytime and even could be in an inconsistent state.
263 * Fortunately, we know that the original code is the ideal 5-byte
264 * long NOP.
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400265 */
Petr Mladek650b7b22015-02-20 15:07:29 +0100266 memcpy(buf, (void *)addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
267 if (faddr)
268 memcpy(buf, ideal_nops[NOP_ATOMIC5], 5);
269 else
270 buf[0] = kp->opcode;
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900271 return (unsigned long)buf;
272}
273
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900274/*
275 * Recover the probed instruction at addr for further analysis.
276 * Caller must lock kprobes by kprobe_mutex, or disable preemption
277 * for preventing to release referencing kprobes.
Petr Mladek2a6730c2015-02-20 15:07:30 +0100278 * Returns zero if the instruction can not get recovered.
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900279 */
Masami Hiramatsu3f33ab12012-03-05 22:32:22 +0900280unsigned long recover_probed_instruction(kprobe_opcode_t *buf, unsigned long addr)
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900281{
282 unsigned long __addr;
283
284 __addr = __recover_optprobed_insn(buf, addr);
285 if (__addr != addr)
286 return __addr;
287
288 return __recover_probed_insn(buf, addr);
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400289}
290
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400291/* Check if paddr is at an instruction boundary */
Masami Hiramatsu7ec8a972014-04-17 17:17:47 +0900292static int can_probe(unsigned long paddr)
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400293{
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900294 unsigned long addr, __addr, offset = 0;
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400295 struct insn insn;
296 kprobe_opcode_t buf[MAX_INSN_SIZE];
297
Namhyung Kim6abded72010-09-15 10:04:29 +0900298 if (!kallsyms_lookup_size_offset(paddr, NULL, &offset))
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400299 return 0;
300
301 /* Decode instructions */
302 addr = paddr - offset;
303 while (addr < paddr) {
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400304 /*
305 * Check if the instruction has been modified by another
306 * kprobe, in which case we replace the breakpoint by the
307 * original instruction in our buffer.
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900308 * Also, jump optimization will change the breakpoint to
309 * relative-jump. Since the relative-jump itself is
310 * normally used, we just go through if there is no kprobe.
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400311 */
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900312 __addr = recover_probed_instruction(buf, addr);
Petr Mladek2a6730c2015-02-20 15:07:30 +0100313 if (!__addr)
314 return 0;
Dave Hansen6ba48ff2014-11-14 07:39:57 -0800315 kernel_insn_init(&insn, (void *)__addr, MAX_INSN_SIZE);
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400316 insn_get_length(&insn);
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900317
318 /*
319 * Another debugging subsystem might insert this breakpoint.
320 * In that case, we can't recover it.
321 */
322 if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION)
323 return 0;
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400324 addr += insn.length;
325 }
326
327 return (addr == paddr);
328}
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330/*
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100331 * Returns non-zero if opcode modifies the interrupt flag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 */
Masami Hiramatsu7ec8a972014-04-17 17:17:47 +0900333static int is_IF_modifier(kprobe_opcode_t *insn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900335 /* Skip prefixes */
336 insn = skip_prefixes(insn);
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 switch (*insn) {
339 case 0xfa: /* cli */
340 case 0xfb: /* sti */
341 case 0xcf: /* iret/iretd */
342 case 0x9d: /* popf/popfd */
343 return 1;
344 }
Harvey Harrison99309272008-01-30 13:32:14 +0100345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return 0;
347}
348
349/*
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500350 * Copy an instruction and adjust the displacement if the instruction
351 * uses the %rip-relative addressing mode.
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100352 * If it does, Return the address of the 32-bit displacement word.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 * If not, return null.
Harvey Harrison31f80e42008-01-30 13:32:16 +0100354 * Only applicable to 64-bit x86.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 */
Masami Hiramatsu7ec8a972014-04-17 17:17:47 +0900356int __copy_instruction(u8 *dest, u8 *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400358 struct insn insn;
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500359 kprobe_opcode_t buf[MAX_INSN_SIZE];
Eugene Shatokhinc80e5c02015-03-17 19:09:18 +0900360 int length;
Dave Hansen6ba48ff2014-11-14 07:39:57 -0800361 unsigned long recovered_insn =
362 recover_probed_instruction(buf, (unsigned long)src);
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900363
Petr Mladek2a6730c2015-02-20 15:07:30 +0100364 if (!recovered_insn)
365 return 0;
Dave Hansen6ba48ff2014-11-14 07:39:57 -0800366 kernel_insn_init(&insn, (void *)recovered_insn, MAX_INSN_SIZE);
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500367 insn_get_length(&insn);
Eugene Shatokhinc80e5c02015-03-17 19:09:18 +0900368 length = insn.length;
369
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900370 /* Another subsystem puts a breakpoint, failed to recover */
Masami Hiramatsu46484682012-03-05 22:32:16 +0900371 if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION)
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900372 return 0;
Eugene Shatokhinc80e5c02015-03-17 19:09:18 +0900373 memcpy(dest, insn.kaddr, length);
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500374
375#ifdef CONFIG_X86_64
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400376 if (insn_rip_relative(&insn)) {
377 s64 newdisp;
378 u8 *disp;
Eugene Shatokhinc80e5c02015-03-17 19:09:18 +0900379 kernel_insn_init(&insn, dest, length);
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400380 insn_get_displacement(&insn);
381 /*
382 * The copied instruction uses the %rip-relative addressing
383 * mode. Adjust the displacement for the difference between
384 * the original location of this instruction and the location
385 * of the copy that will actually be run. The tricky bit here
386 * is making sure that the sign extension happens correctly in
387 * this calculation, since we need a signed 32-bit result to
388 * be sign-extended to 64 bits when it's added to the %rip
389 * value and yield the same 64-bit result that the sign-
390 * extension of the original signed 32-bit displacement would
391 * have given.
392 */
Masami Hiramatsu46484682012-03-05 22:32:16 +0900393 newdisp = (u8 *) src + (s64) insn.displacement.value - (u8 *) dest;
Masami Hiramatsu81013762013-04-04 19:42:30 +0900394 if ((s64) (s32) newdisp != newdisp) {
395 pr_err("Kprobes error: new displacement does not fit into s32 (%llx)\n", newdisp);
396 pr_err("\tSrc: %p, Dest: %p, old disp: %x\n", src, dest, insn.displacement.value);
397 return 0;
398 }
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500399 disp = (u8 *) dest + insn_offset_displacement(&insn);
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400400 *(s32 *) disp = (s32) newdisp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100402#endif
Eugene Shatokhinc80e5c02015-03-17 19:09:18 +0900403 return length;
Harvey Harrison31f80e42008-01-30 13:32:16 +0100404}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Masami Hiramatsu7ec8a972014-04-17 17:17:47 +0900406static int arch_copy_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Masami Hiramatsu003002e2013-06-05 12:12:16 +0900408 int ret;
409
Masami Hiramatsu46484682012-03-05 22:32:16 +0900410 /* Copy an instruction with recovering if other optprobe modifies it.*/
Masami Hiramatsu003002e2013-06-05 12:12:16 +0900411 ret = __copy_instruction(p->ainsn.insn, p->addr);
412 if (!ret)
413 return -EINVAL;
Harvey Harrison31f80e42008-01-30 13:32:16 +0100414
Masami Hiramatsu46484682012-03-05 22:32:16 +0900415 /*
416 * __copy_instruction can modify the displacement of the instruction,
417 * but it doesn't affect boostable check.
418 */
419 if (can_boost(p->ainsn.insn))
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100420 p->ainsn.boostable = 0;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100421 else
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100422 p->ainsn.boostable = -1;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100423
Masami Hiramatsu9a556ab2013-03-14 20:52:43 +0900424 /* Check whether the instruction modifies Interrupt Flag or not */
425 p->ainsn.if_modifier = is_IF_modifier(p->ainsn.insn);
426
Masami Hiramatsu46484682012-03-05 22:32:16 +0900427 /* Also, displacement change doesn't affect the first byte */
428 p->opcode = p->ainsn.insn[0];
Masami Hiramatsu003002e2013-06-05 12:12:16 +0900429
430 return 0;
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700431}
432
Masami Hiramatsu7ec8a972014-04-17 17:17:47 +0900433int arch_prepare_kprobe(struct kprobe *p)
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100434{
Masami Hiramatsu4554dbc2010-02-02 16:49:18 -0500435 if (alternatives_text_reserved(p->addr, p->addr))
436 return -EINVAL;
437
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400438 if (!can_probe((unsigned long)p->addr))
439 return -EILSEQ;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100440 /* insn: must be on special executable page on x86. */
441 p->ainsn.insn = get_insn_slot();
442 if (!p->ainsn.insn)
443 return -ENOMEM;
Masami Hiramatsu003002e2013-06-05 12:12:16 +0900444
445 return arch_copy_kprobe(p);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100446}
447
Masami Hiramatsu7ec8a972014-04-17 17:17:47 +0900448void arch_arm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700449{
Andi Kleen19d36cc2007-07-22 11:12:31 +0200450 text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1);
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700451}
452
Masami Hiramatsu7ec8a972014-04-17 17:17:47 +0900453void arch_disarm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700454{
Andi Kleen19d36cc2007-07-22 11:12:31 +0200455 text_poke(p->addr, &p->opcode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
Masami Hiramatsu7ec8a972014-04-17 17:17:47 +0900458void arch_remove_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Masami Hiramatsu12941562009-01-06 14:41:50 -0800460 if (p->ainsn.insn) {
461 free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1));
462 p->ainsn.insn = NULL;
463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
Masami Hiramatsu93266382014-04-17 17:18:14 +0900466static nokprobe_inline void
467save_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700468{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800469 kcb->prev_kprobe.kp = kprobe_running();
470 kcb->prev_kprobe.status = kcb->kprobe_status;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100471 kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags;
472 kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700473}
474
Masami Hiramatsu93266382014-04-17 17:18:14 +0900475static nokprobe_inline void
476restore_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700477{
Christoph Lameterb76834b2010-12-06 11:16:25 -0600478 __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800479 kcb->kprobe_status = kcb->prev_kprobe.status;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100480 kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags;
481 kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700482}
483
Masami Hiramatsu93266382014-04-17 17:18:14 +0900484static nokprobe_inline void
485set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
486 struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700487{
Christoph Lameterb76834b2010-12-06 11:16:25 -0600488 __this_cpu_write(current_kprobe, p);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100489 kcb->kprobe_saved_flags = kcb->kprobe_old_flags
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100490 = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF));
Masami Hiramatsu9a556ab2013-03-14 20:52:43 +0900491 if (p->ainsn.if_modifier)
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100492 kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700493}
494
Masami Hiramatsu93266382014-04-17 17:18:14 +0900495static nokprobe_inline void clear_btf(void)
Roland McGrath1ecc7982008-01-30 13:30:54 +0100496{
Peter Zijlstraea8e61b2010-03-25 14:51:51 +0100497 if (test_thread_flag(TIF_BLOCKSTEP)) {
498 unsigned long debugctl = get_debugctlmsr();
499
500 debugctl &= ~DEBUGCTLMSR_BTF;
501 update_debugctlmsr(debugctl);
502 }
Roland McGrath1ecc7982008-01-30 13:30:54 +0100503}
504
Masami Hiramatsu93266382014-04-17 17:18:14 +0900505static nokprobe_inline void restore_btf(void)
Roland McGrath1ecc7982008-01-30 13:30:54 +0100506{
Peter Zijlstraea8e61b2010-03-25 14:51:51 +0100507 if (test_thread_flag(TIF_BLOCKSTEP)) {
508 unsigned long debugctl = get_debugctlmsr();
509
510 debugctl |= DEBUGCTLMSR_BTF;
511 update_debugctlmsr(debugctl);
512 }
Roland McGrath1ecc7982008-01-30 13:30:54 +0100513}
514
Masami Hiramatsu93266382014-04-17 17:18:14 +0900515void arch_prepare_kretprobe(struct kretprobe_instance *ri, struct pt_regs *regs)
Rusty Lynch73649da2005-06-23 00:09:23 -0700516{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100517 unsigned long *sara = stack_addr(regs);
Rusty Lynch73649da2005-06-23 00:09:23 -0700518
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700519 ri->ret_addr = (kprobe_opcode_t *) *sara;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100520
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700521 /* Replace the return addr with trampoline addr */
522 *sara = (unsigned long) &kretprobe_trampoline;
Rusty Lynch73649da2005-06-23 00:09:23 -0700523}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900524NOKPROBE_SYMBOL(arch_prepare_kretprobe);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100525
Masami Hiramatsu93266382014-04-17 17:18:14 +0900526static void setup_singlestep(struct kprobe *p, struct pt_regs *regs,
527 struct kprobe_ctlblk *kcb, int reenter)
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100528{
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500529 if (setup_detour_execution(p, regs, reenter))
530 return;
531
Masami Hiramatsu615d0eb2010-02-02 16:49:04 -0500532#if !defined(CONFIG_PREEMPT)
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100533 if (p->ainsn.boostable == 1 && !p->post_handler) {
534 /* Boost up -- we can execute copied instructions directly */
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500535 if (!reenter)
536 reset_current_kprobe();
537 /*
538 * Reentering boosted probe doesn't reset current_kprobe,
539 * nor set current_kprobe, because it doesn't use single
540 * stepping.
541 */
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100542 regs->ip = (unsigned long)p->ainsn.insn;
543 preempt_enable_no_resched();
544 return;
545 }
546#endif
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500547 if (reenter) {
548 save_previous_kprobe(kcb);
549 set_current_kprobe(p, regs, kcb);
550 kcb->kprobe_status = KPROBE_REENTER;
551 } else
552 kcb->kprobe_status = KPROBE_HIT_SS;
553 /* Prepare real single stepping */
554 clear_btf();
555 regs->flags |= X86_EFLAGS_TF;
556 regs->flags &= ~X86_EFLAGS_IF;
557 /* single step inline if the instruction is an int3 */
558 if (p->opcode == BREAKPOINT_INSTRUCTION)
559 regs->ip = (unsigned long)p->addr;
560 else
561 regs->ip = (unsigned long)p->ainsn.insn;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100562}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900563NOKPROBE_SYMBOL(setup_singlestep);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100564
Harvey Harrison40102d42008-01-30 13:32:02 +0100565/*
566 * We have reentered the kprobe_handler(), since another probe was hit while
567 * within the handler. We save the original kprobes variables and just single
568 * step on the instruction of the new probe without calling any user handlers.
569 */
Masami Hiramatsu93266382014-04-17 17:18:14 +0900570static int reenter_kprobe(struct kprobe *p, struct pt_regs *regs,
571 struct kprobe_ctlblk *kcb)
Harvey Harrison40102d42008-01-30 13:32:02 +0100572{
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100573 switch (kcb->kprobe_status) {
574 case KPROBE_HIT_SSDONE:
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100575 case KPROBE_HIT_ACTIVE:
Masami Hiramatsu6a5022a2014-04-17 17:16:51 +0900576 case KPROBE_HIT_SS:
Abhishek Sagarfb8830e2008-01-30 13:33:13 +0100577 kprobes_inc_nmissed_count(p);
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500578 setup_singlestep(p, regs, kcb, 1);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100579 break;
Masami Hiramatsu6a5022a2014-04-17 17:16:51 +0900580 case KPROBE_REENTER:
Masami Hiramatsue9afe9e2009-08-27 13:22:58 -0400581 /* A probe has been hit in the codepath leading up to, or just
582 * after, single-stepping of a probed instruction. This entire
583 * codepath should strictly reside in .kprobes.text section.
584 * Raise a BUG or we'll continue in an endless reentering loop
585 * and eventually a stack overflow.
586 */
587 printk(KERN_WARNING "Unrecoverable kprobe detected at %p.\n",
588 p->addr);
589 dump_kprobe(p);
590 BUG();
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100591 default:
592 /* impossible cases */
593 WARN_ON(1);
Abhishek Sagarfb8830e2008-01-30 13:33:13 +0100594 return 0;
Masami Hiramatsu59e87cd2008-01-30 13:32:02 +0100595 }
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100596
Masami Hiramatsu59e87cd2008-01-30 13:32:02 +0100597 return 1;
Harvey Harrison40102d42008-01-30 13:32:02 +0100598}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900599NOKPROBE_SYMBOL(reenter_kprobe);
Rusty Lynch73649da2005-06-23 00:09:23 -0700600
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100601/*
602 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200603 * remain disabled throughout this function.
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100604 */
Masami Hiramatsu93266382014-04-17 17:18:14 +0900605int kprobe_int3_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100607 kprobe_opcode_t *addr;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100608 struct kprobe *p;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800609 struct kprobe_ctlblk *kcb;
610
Andy Lutomirskif39b6f02015-03-18 18:33:33 -0700611 if (user_mode(regs))
Andy Lutomirski0cdd1922014-07-11 10:27:01 -0700612 return 0;
613
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100614 addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800615 /*
616 * We don't want to be preempted for the entire
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100617 * duration of kprobe processing. We conditionally
618 * re-enable preemption at the end of this function,
619 * and also in reenter_kprobe() and setup_singlestep().
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800620 */
621 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100623 kcb = get_kprobe_ctlblk();
Harvey Harrisonb9760152008-01-30 13:32:19 +0100624 p = get_kprobe(addr);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100625
Harvey Harrisonb9760152008-01-30 13:32:19 +0100626 if (p) {
Harvey Harrisonb9760152008-01-30 13:32:19 +0100627 if (kprobe_running()) {
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100628 if (reenter_kprobe(p, regs, kcb))
629 return 1;
Harvey Harrisonb9760152008-01-30 13:32:19 +0100630 } else {
631 set_current_kprobe(p, regs, kcb);
632 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 /*
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100635 * If we have no pre-handler or it returned 0, we
636 * continue with normal processing. If we have a
637 * pre-handler and it returned non-zero, it prepped
638 * for calling the break_handler below on re-entry
639 * for jprobe processing, so get out doing nothing
640 * more here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 */
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100642 if (!p->pre_handler || !p->pre_handler(p, regs))
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500643 setup_singlestep(p, regs, kcb, 0);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100644 return 1;
Harvey Harrisonb9760152008-01-30 13:32:19 +0100645 }
Masami Hiramatsu829e9242010-04-27 18:33:49 -0400646 } else if (*addr != BREAKPOINT_INSTRUCTION) {
647 /*
648 * The breakpoint instruction was removed right
649 * after we hit it. Another cpu has removed
650 * either a probepoint or a debugger breakpoint
651 * at this address. In either case, no further
652 * handling of this interrupt is appropriate.
653 * Back up over the (now missing) int3 and run
654 * the original instruction.
655 */
656 regs->ip = (unsigned long)addr;
657 preempt_enable_no_resched();
658 return 1;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100659 } else if (kprobe_running()) {
Christoph Lameterb76834b2010-12-06 11:16:25 -0600660 p = __this_cpu_read(current_kprobe);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100661 if (p->break_handler && p->break_handler(p, regs)) {
Masami Hiramatsue7dbfe32012-09-28 17:15:20 +0900662 if (!skip_singlestep(p, regs, kcb))
663 setup_singlestep(p, regs, kcb, 0);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100664 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100666 } /* else: not a kprobe fault; let the kernel handle it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800668 preempt_enable_no_resched();
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100669 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900671NOKPROBE_SYMBOL(kprobe_int3_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673/*
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100674 * When a retprobed function returns, this code saves registers and
675 * calls trampoline_handler() runs, which calls the kretprobe's handler.
Rusty Lynch73649da2005-06-23 00:09:23 -0700676 */
Josh Poimboeufc1c355c2016-01-21 16:49:28 -0600677asm(
678 ".global kretprobe_trampoline\n"
679 ".type kretprobe_trampoline, @function\n"
680 "kretprobe_trampoline:\n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100681#ifdef CONFIG_X86_64
Josh Poimboeufc1c355c2016-01-21 16:49:28 -0600682 /* We don't bother saving the ss register */
683 " pushq %rsp\n"
684 " pushfq\n"
685 SAVE_REGS_STRING
686 " movq %rsp, %rdi\n"
687 " call trampoline_handler\n"
688 /* Replace saved sp with true return address. */
689 " movq %rax, 152(%rsp)\n"
690 RESTORE_REGS_STRING
691 " popfq\n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100692#else
Josh Poimboeufc1c355c2016-01-21 16:49:28 -0600693 " pushf\n"
694 SAVE_REGS_STRING
695 " movl %esp, %eax\n"
696 " call trampoline_handler\n"
697 /* Move flags to cs */
698 " movl 56(%esp), %edx\n"
699 " movl %edx, 52(%esp)\n"
700 /* Replace saved flags with true return address. */
701 " movl %eax, 56(%esp)\n"
702 RESTORE_REGS_STRING
703 " popf\n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100704#endif
Josh Poimboeufc1c355c2016-01-21 16:49:28 -0600705 " ret\n"
706 ".size kretprobe_trampoline, .-kretprobe_trampoline\n"
707);
Masami Hiramatsu93266382014-04-17 17:18:14 +0900708NOKPROBE_SYMBOL(kretprobe_trampoline);
Josh Poimboeuf87aaff22016-02-28 22:22:40 -0600709STACK_FRAME_NON_STANDARD(kretprobe_trampoline);
Rusty Lynch73649da2005-06-23 00:09:23 -0700710
711/*
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100712 * Called from kretprobe_trampoline
Rusty Lynch73649da2005-06-23 00:09:23 -0700713 */
Masami Hiramatsu93266382014-04-17 17:18:14 +0900714__visible __used void *trampoline_handler(struct pt_regs *regs)
Rusty Lynch73649da2005-06-23 00:09:23 -0700715{
bibo,mao62c27be2006-10-02 02:17:33 -0700716 struct kretprobe_instance *ri = NULL;
bibo,mao99219a32006-10-02 02:17:35 -0700717 struct hlist_head *head, empty_rp;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800718 struct hlist_node *tmp;
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800719 unsigned long flags, orig_ret_address = 0;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100720 unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
KUMANO Syuhei737480a2010-08-15 15:18:04 +0900721 kprobe_opcode_t *correct_ret_addr = NULL;
Rusty Lynch73649da2005-06-23 00:09:23 -0700722
bibo,mao99219a32006-10-02 02:17:35 -0700723 INIT_HLIST_HEAD(&empty_rp);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700724 kretprobe_hash_lock(current, &head, &flags);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100725 /* fixup registers */
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100726#ifdef CONFIG_X86_64
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100727 regs->cs = __KERNEL_CS;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100728#else
729 regs->cs = __KERNEL_CS | get_kernel_rpl();
Masami Hiramatsufee039a2009-03-23 10:14:52 -0400730 regs->gs = 0;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100731#endif
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100732 regs->ip = trampoline_address;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100733 regs->orig_ax = ~0UL;
Rusty Lynch73649da2005-06-23 00:09:23 -0700734
Rusty Lynchba8af122005-06-27 15:17:10 -0700735 /*
736 * It is possible to have multiple instances associated with a given
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100737 * task either because multiple functions in the call path have
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200738 * return probes installed on them, and/or more than one
Rusty Lynchba8af122005-06-27 15:17:10 -0700739 * return probe was registered for a target function.
740 *
741 * We can handle this because:
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100742 * - instances are always pushed into the head of the list
Rusty Lynchba8af122005-06-27 15:17:10 -0700743 * - when multiple return probes are registered for the same
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100744 * function, the (chronologically) first instance's ret_addr
745 * will be the real return address, and all the rest will
746 * point to kretprobe_trampoline.
Rusty Lynchba8af122005-06-27 15:17:10 -0700747 */
Masami Hiramatsub6263172017-02-06 18:55:43 +0900748 hlist_for_each_entry(ri, head, hlist) {
bibo,mao62c27be2006-10-02 02:17:33 -0700749 if (ri->task != current)
Rusty Lynchba8af122005-06-27 15:17:10 -0700750 /* another task is sharing our hash bucket */
bibo,mao62c27be2006-10-02 02:17:33 -0700751 continue;
Rusty Lynch73649da2005-06-23 00:09:23 -0700752
Rusty Lynchba8af122005-06-27 15:17:10 -0700753 orig_ret_address = (unsigned long)ri->ret_addr;
Rusty Lynchba8af122005-06-27 15:17:10 -0700754
755 if (orig_ret_address != trampoline_address)
756 /*
757 * This is the real return address. Any other
758 * instances associated with this task are for
759 * other calls deeper on the call stack
760 */
761 break;
Rusty Lynch73649da2005-06-23 00:09:23 -0700762 }
Rusty Lynchba8af122005-06-27 15:17:10 -0700763
Ananth N Mavinakayanahalli0f95b7f2007-05-08 00:28:27 -0700764 kretprobe_assert(ri, orig_ret_address, trampoline_address);
Rusty Lynchba8af122005-06-27 15:17:10 -0700765
KUMANO Syuhei737480a2010-08-15 15:18:04 +0900766 correct_ret_addr = ri->ret_addr;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800767 hlist_for_each_entry_safe(ri, tmp, head, hlist) {
KUMANO Syuhei737480a2010-08-15 15:18:04 +0900768 if (ri->task != current)
769 /* another task is sharing our hash bucket */
770 continue;
771
772 orig_ret_address = (unsigned long)ri->ret_addr;
773 if (ri->rp && ri->rp->handler) {
Christoph Lameterb76834b2010-12-06 11:16:25 -0600774 __this_cpu_write(current_kprobe, &ri->rp->kp);
KUMANO Syuhei737480a2010-08-15 15:18:04 +0900775 get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
776 ri->ret_addr = correct_ret_addr;
777 ri->rp->handler(ri, regs);
Christoph Lameterb76834b2010-12-06 11:16:25 -0600778 __this_cpu_write(current_kprobe, NULL);
KUMANO Syuhei737480a2010-08-15 15:18:04 +0900779 }
780
781 recycle_rp_inst(ri, &empty_rp);
782
783 if (orig_ret_address != trampoline_address)
784 /*
785 * This is the real return address. Any other
786 * instances associated with this task are for
787 * other calls deeper on the call stack
788 */
789 break;
790 }
791
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700792 kretprobe_hash_unlock(current, &flags);
Rusty Lynchba8af122005-06-27 15:17:10 -0700793
Sasha Levinb67bfe02013-02-27 17:06:00 -0800794 hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
bibo,mao99219a32006-10-02 02:17:35 -0700795 hlist_del(&ri->hlist);
796 kfree(ri);
797 }
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100798 return (void *)orig_ret_address;
Rusty Lynch73649da2005-06-23 00:09:23 -0700799}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900800NOKPROBE_SYMBOL(trampoline_handler);
Rusty Lynch73649da2005-06-23 00:09:23 -0700801
802/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 * Called after single-stepping. p->addr is the address of the
804 * instruction whose first byte has been replaced by the "int 3"
805 * instruction. To avoid the SMP problems that can occur when we
806 * temporarily put back the original opcode to single-step, we
807 * single-stepped a copy of the instruction. The address of this
808 * copy is p->ainsn.insn.
809 *
810 * This function prepares to return from the post-single-step
811 * interrupt. We have to fix up the stack as follows:
812 *
813 * 0) Except in the case of absolute or indirect jump or call instructions,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100814 * the new ip is relative to the copied instruction. We need to make
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 * it relative to the original instruction.
816 *
817 * 1) If the single-stepped instruction was pushfl, then the TF and IF
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100818 * flags are set in the just-pushed flags, and may need to be cleared.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 *
820 * 2) If the single-stepped instruction was a call, the return address
821 * that is atop the stack is the address following the copied instruction.
822 * We need to make it the address following the original instruction.
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100823 *
824 * If this is the first time we've single-stepped the instruction at
825 * this probepoint, and the instruction is boostable, boost it: add a
826 * jump instruction after the copied instruction, that jumps to the next
827 * instruction after the probepoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 */
Masami Hiramatsu93266382014-04-17 17:18:14 +0900829static void resume_execution(struct kprobe *p, struct pt_regs *regs,
830 struct kprobe_ctlblk *kcb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100832 unsigned long *tos = stack_addr(regs);
833 unsigned long copy_ip = (unsigned long)p->ainsn.insn;
834 unsigned long orig_ip = (unsigned long)p->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 kprobe_opcode_t *insn = p->ainsn.insn;
836
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900837 /* Skip prefixes */
838 insn = skip_prefixes(insn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100840 regs->flags &= ~X86_EFLAGS_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 switch (*insn) {
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100842 case 0x9c: /* pushfl */
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100843 *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100844 *tos |= kcb->kprobe_old_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 break;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100846 case 0xc2: /* iret/ret/lret */
847 case 0xc3:
Prasanna S Panchamukhi0b9e2ca2005-05-05 16:15:40 -0700848 case 0xca:
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100849 case 0xcb:
850 case 0xcf:
851 case 0xea: /* jmp absolute -- ip is correct */
852 /* ip is already adjusted, no more changes required */
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100853 p->ainsn.boostable = 1;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100854 goto no_change;
855 case 0xe8: /* call relative - Fix return addr */
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100856 *tos = orig_ip + (*tos - copy_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 break;
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100858#ifdef CONFIG_X86_32
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100859 case 0x9a: /* call absolute -- same as call absolute, indirect */
860 *tos = orig_ip + (*tos - copy_ip);
861 goto no_change;
862#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 case 0xff:
Satoshi Oshimadc49e342006-05-20 15:00:21 -0700864 if ((insn[1] & 0x30) == 0x10) {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100865 /*
866 * call absolute, indirect
867 * Fix return addr; ip is correct.
868 * But this is not boostable
869 */
870 *tos = orig_ip + (*tos - copy_ip);
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100871 goto no_change;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100872 } else if (((insn[1] & 0x31) == 0x20) ||
873 ((insn[1] & 0x31) == 0x21)) {
874 /*
875 * jmp near and far, absolute indirect
876 * ip is correct. And this is boostable
877 */
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100878 p->ainsn.boostable = 1;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100879 goto no_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 default:
882 break;
883 }
884
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100885 if (p->ainsn.boostable == 0) {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100886 if ((regs->ip > copy_ip) &&
887 (regs->ip - copy_ip) + 5 < MAX_INSN_SIZE) {
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100888 /*
889 * These instructions can be executed directly if it
890 * jumps back to correct address.
891 */
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500892 synthesize_reljump((void *)regs->ip,
893 (void *)orig_ip + (regs->ip - copy_ip));
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100894 p->ainsn.boostable = 1;
895 } else {
896 p->ainsn.boostable = -1;
897 }
898 }
899
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100900 regs->ip += orig_ip - copy_ip;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100901
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100902no_change:
Roland McGrath1ecc7982008-01-30 13:30:54 +0100903 restore_btf();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900905NOKPROBE_SYMBOL(resume_execution);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100907/*
908 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200909 * remain disabled throughout this function.
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100910 */
Masami Hiramatsu93266382014-04-17 17:18:14 +0900911int kprobe_debug_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800913 struct kprobe *cur = kprobe_running();
914 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
915
916 if (!cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return 0;
918
Yakov Lerneracb5b8a2008-03-16 03:21:21 -0500919 resume_execution(cur, regs, kcb);
920 regs->flags |= kcb->kprobe_saved_flags;
Yakov Lerneracb5b8a2008-03-16 03:21:21 -0500921
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800922 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
923 kcb->kprobe_status = KPROBE_HIT_SSDONE;
924 cur->post_handler(cur, regs, 0);
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100927 /* Restore back the original saved kprobes variables and continue. */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800928 if (kcb->kprobe_status == KPROBE_REENTER) {
929 restore_previous_kprobe(kcb);
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700930 goto out;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700931 }
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800932 reset_current_kprobe();
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700933out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 preempt_enable_no_resched();
935
936 /*
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100937 * if somebody else is singlestepping across a probe point, flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 * will have TF set, in which case, continue the remaining processing
939 * of do_debug, as if this is not a probe hit.
940 */
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100941 if (regs->flags & X86_EFLAGS_TF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return 0;
943
944 return 1;
945}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900946NOKPROBE_SYMBOL(kprobe_debug_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Masami Hiramatsu93266382014-04-17 17:18:14 +0900948int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800950 struct kprobe *cur = kprobe_running();
951 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
952
Masami Hiramatsu6381c242014-04-17 17:16:44 +0900953 if (unlikely(regs->ip == (unsigned long)cur->ainsn.insn)) {
954 /* This must happen on single-stepping */
955 WARN_ON(kcb->kprobe_status != KPROBE_HIT_SS &&
956 kcb->kprobe_status != KPROBE_REENTER);
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800957 /*
958 * We are here because the instruction being single
959 * stepped caused a page fault. We reset the current
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100960 * kprobe and the ip points back to the probe address
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800961 * and allow the page fault handler to continue as a
962 * normal page fault.
963 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100964 regs->ip = (unsigned long)cur->addr;
Masami Hiramatsudcfc4722016-06-11 23:06:53 +0900965 /*
966 * Trap flag (TF) has been set here because this fault
967 * happened where the single stepping will be done.
968 * So clear it by resetting the current kprobe:
969 */
970 regs->flags &= ~X86_EFLAGS_TF;
971
972 /*
973 * If the TF flag was set before the kprobe hit,
974 * don't touch it:
975 */
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100976 regs->flags |= kcb->kprobe_old_flags;
Masami Hiramatsudcfc4722016-06-11 23:06:53 +0900977
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800978 if (kcb->kprobe_status == KPROBE_REENTER)
979 restore_previous_kprobe(kcb);
980 else
981 reset_current_kprobe();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 preempt_enable_no_resched();
Masami Hiramatsu6381c242014-04-17 17:16:44 +0900983 } else if (kcb->kprobe_status == KPROBE_HIT_ACTIVE ||
984 kcb->kprobe_status == KPROBE_HIT_SSDONE) {
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800985 /*
986 * We increment the nmissed count for accounting,
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100987 * we can also use npre/npostfault count for accounting
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800988 * these specific fault cases.
989 */
990 kprobes_inc_nmissed_count(cur);
991
992 /*
993 * We come here because instructions in the pre/post
994 * handler caused the page_fault, this could happen
995 * if handler tries to access user space by
996 * copy_from_user(), get_user() etc. Let the
997 * user-specified handler try to fix it first.
998 */
999 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
1000 return 1;
1001
1002 /*
1003 * In case the user-specified fault handler returned
1004 * zero, try to fix up.
1005 */
Tony Luck548acf12016-02-17 10:20:12 -08001006 if (fixup_exception(regs, trapnr))
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001007 return 1;
Harvey Harrison6d485832008-01-30 13:31:41 +01001008
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -08001009 /*
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001010 * fixup routine could not handle it,
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -08001011 * Let do_page_fault() fix it.
1012 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
Masami Hiramatsu6381c242014-04-17 17:16:44 +09001014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return 0;
1016}
Masami Hiramatsu93266382014-04-17 17:18:14 +09001017NOKPROBE_SYMBOL(kprobe_fault_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019/*
1020 * Wrapper routine for handling exceptions.
1021 */
Masami Hiramatsu93266382014-04-17 17:18:14 +09001022int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
1023 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Jan Engelhardtade1af72008-01-30 13:33:23 +01001025 struct die_args *args = data;
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -08001026 int ret = NOTIFY_DONE;
1027
Andy Lutomirskif39b6f02015-03-18 18:33:33 -07001028 if (args->regs && user_mode(args->regs))
bibo,mao2326c772006-03-26 01:38:21 -08001029 return ret;
1030
Masami Hiramatsu6f6343f2014-04-17 17:17:33 +09001031 if (val == DIE_GPF) {
Quentin Barnesb506a9d2008-01-30 13:32:32 +01001032 /*
1033 * To be potentially processing a kprobe fault and to
1034 * trust the result from kprobe_running(), we have
1035 * be non-preemptible.
1036 */
1037 if (!preemptible() && kprobe_running() &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 kprobe_fault_handler(args->regs, args->trapnr))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -08001039 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -08001041 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
Masami Hiramatsu93266382014-04-17 17:18:14 +09001043NOKPROBE_SYMBOL(kprobe_exceptions_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Masami Hiramatsu93266382014-04-17 17:18:14 +09001045int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
1047 struct jprobe *jp = container_of(p, struct jprobe, kp);
1048 unsigned long addr;
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001049 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001051 kcb->jprobe_saved_regs = *regs;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001052 kcb->jprobe_saved_sp = stack_addr(regs);
1053 addr = (unsigned long)(kcb->jprobe_saved_sp);
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 /*
1056 * As Linus pointed out, gcc assumes that the callee
1057 * owns the argument space and could overwrite it, e.g.
1058 * tailcall optimization. So, to be absolutely safe
1059 * we also save and restore enough stack bytes to cover
1060 * the argument area.
Dmitry Vyukov92541392016-10-11 14:13:38 +02001061 * Use __memcpy() to avoid KASAN stack out-of-bounds reports as we copy
1062 * raw stack chunk with redzones:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 */
Dmitry Vyukov92541392016-10-11 14:13:38 +02001064 __memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr, MIN_STACK_SIZE(addr));
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001065 regs->flags &= ~X86_EFLAGS_IF;
Peter Zijlstra58dfe882007-10-11 22:25:25 +02001066 trace_hardirqs_off();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001067 regs->ip = (unsigned long)(jp->entry);
Steven Rostedt (Red Hat)237d28d2015-01-12 12:12:03 -05001068
1069 /*
1070 * jprobes use jprobe_return() which skips the normal return
1071 * path of the function, and this messes up the accounting of the
1072 * function graph tracer to get messed up.
1073 *
1074 * Pause function graph tracing while performing the jprobe function.
1075 */
1076 pause_graph_tracing();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return 1;
1078}
Masami Hiramatsu93266382014-04-17 17:18:14 +09001079NOKPROBE_SYMBOL(setjmp_pre_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Masami Hiramatsu93266382014-04-17 17:18:14 +09001081void jprobe_return(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001083 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1084
Dmitry Vyukov9f7d4162016-10-14 16:07:23 +02001085 /* Unpoison stack redzones in the frames we are going to jump over. */
1086 kasan_unpoison_stack_above_sp_to(kcb->jprobe_saved_sp);
1087
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001088 asm volatile (
1089#ifdef CONFIG_X86_64
1090 " xchg %%rbx,%%rsp \n"
1091#else
1092 " xchgl %%ebx,%%esp \n"
1093#endif
1094 " int3 \n"
1095 " .globl jprobe_return_end\n"
1096 " jprobe_return_end: \n"
1097 " nop \n"::"b"
1098 (kcb->jprobe_saved_sp):"memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
Masami Hiramatsu93266382014-04-17 17:18:14 +09001100NOKPROBE_SYMBOL(jprobe_return);
1101NOKPROBE_SYMBOL(jprobe_return_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Masami Hiramatsu93266382014-04-17 17:18:14 +09001103int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001105 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001106 u8 *addr = (u8 *) (regs->ip - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 struct jprobe *jp = container_of(p, struct jprobe, kp);
Steven Rostedt (Red Hat)237d28d2015-01-12 12:12:03 -05001108 void *saved_sp = kcb->jprobe_saved_sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001110 if ((addr > (u8 *) jprobe_return) &&
1111 (addr < (u8 *) jprobe_return_end)) {
Steven Rostedt (Red Hat)237d28d2015-01-12 12:12:03 -05001112 if (stack_addr(regs) != saved_sp) {
Masami Hiramatsu29b6cd72007-12-18 18:05:58 +01001113 struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001114 printk(KERN_ERR
1115 "current sp %p does not match saved sp %p\n",
Steven Rostedt (Red Hat)237d28d2015-01-12 12:12:03 -05001116 stack_addr(regs), saved_sp);
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001117 printk(KERN_ERR "Saved registers for jprobe %p\n", jp);
Jan Beulich57da8b92012-05-09 08:47:37 +01001118 show_regs(saved_regs);
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001119 printk(KERN_ERR "Current registers\n");
Jan Beulich57da8b92012-05-09 08:47:37 +01001120 show_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 BUG();
1122 }
Steven Rostedt (Red Hat)237d28d2015-01-12 12:12:03 -05001123 /* It's OK to start function graph tracing again */
1124 unpause_graph_tracing();
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001125 *regs = kcb->jprobe_saved_regs;
Dmitry Vyukov92541392016-10-11 14:13:38 +02001126 __memcpy(saved_sp, kcb->jprobes_stack, MIN_STACK_SIZE(saved_sp));
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -08001127 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return 1;
1129 }
1130 return 0;
1131}
Masami Hiramatsu93266382014-04-17 17:18:14 +09001132NOKPROBE_SYMBOL(longjmp_break_handler);
Rusty Lynchba8af122005-06-27 15:17:10 -07001133
Masami Hiramatsube8f2742014-04-17 17:16:58 +09001134bool arch_within_kprobe_blacklist(unsigned long addr)
1135{
1136 return (addr >= (unsigned long)__kprobes_text_start &&
1137 addr < (unsigned long)__kprobes_text_end) ||
1138 (addr >= (unsigned long)__entry_text_start &&
1139 addr < (unsigned long)__entry_text_end);
1140}
1141
Rusty Lynch67729262005-07-05 18:54:50 -07001142int __init arch_init_kprobes(void)
Rusty Lynchba8af122005-06-27 15:17:10 -07001143{
Masami Hiramatsua7b01332013-07-18 20:47:50 +09001144 return 0;
Rusty Lynchba8af122005-06-27 15:17:10 -07001145}
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001146
Masami Hiramatsu7ec8a972014-04-17 17:17:47 +09001147int arch_trampoline_kprobe(struct kprobe *p)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001148{
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001149 return 0;
1150}