blob: f0153714ddac6b2305645ef5c0e35fd3a6c9fb2c [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>
Ingo Molnarb17b0152017-02-08 18:51:35 +010048#include <linux/sched/debug.h>
Paul Gortmaker744c1932016-09-19 17:04:18 -040049#include <linux/extable.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070050#include <linux/kdebug.h>
Masami Hiramatsub46b3d72009-08-13 16:34:28 -040051#include <linux/kallsyms.h>
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -050052#include <linux/ftrace.h>
Josh Poimboeuf87aaff22016-02-28 22:22:40 -060053#include <linux/frame.h>
Dmitry Vyukov9f7d4162016-10-14 16:07:23 +020054#include <linux/kasan.h>
Masami Hiramatsuc93f5cf2017-05-25 19:38:17 +090055#include <linux/moduleloader.h>
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070056
Andy Lutomirski35de5b02016-04-26 12:23:24 -070057#include <asm/text-patching.h>
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +010058#include <asm/cacheflush.h>
59#include <asm/desc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <asm/pgtable.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080061#include <linux/uaccess.h>
Andi Kleen19d36cc2007-07-22 11:12:31 +020062#include <asm/alternative.h>
Masami Hiramatsub46b3d72009-08-13 16:34:28 -040063#include <asm/insn.h>
K.Prasad62edab92009-06-01 23:47:06 +053064#include <asm/debugreg.h>
Laura Abbotte6ccbff2017-05-08 15:58:47 -070065#include <asm/set_memory.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Masami Hiramatsuf6841992012-09-28 17:15:22 +090067#include "common.h"
Masami Hiramatsu3f33ab12012-03-05 22:32:22 +090068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069void jprobe_return_end(void);
70
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -080071DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
72DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
H. Peter Anvin98272ed2009-10-12 14:14:10 -070074#define stack_addr(regs) ((unsigned long *)kernel_stack_pointer(regs))
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +010075
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.
Linus Torvalds7115e3f2011-10-26 17:03:38 +020085 * This is non-const and volatile to keep gcc from statically
86 * optimizing it out, as variable_test_bit makes gcc think only
Masami Hiramatsuf6841992012-09-28 17:15:22 +090087 * *(unsigned long*) is used.
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +010088 */
Linus Torvalds7115e3f2011-10-26 17:03:38 +020089static volatile u32 twobyte_is_boostable[256 / 32] = {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +010090 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
91 /* ---------------------------------------------- */
92 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 +080093 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 +010094 W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 20 */
95 W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */
96 W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
97 W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */
98 W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1) | /* 60 */
99 W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */
100 W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 80 */
101 W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
102 W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* a0 */
103 W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) , /* b0 */
104 W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */
105 W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) , /* d0 */
106 W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* e0 */
107 W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0) /* f0 */
108 /* ----------------------------------------------- */
109 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
110};
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100111#undef W
112
Masami Hiramatsuf438d912007-10-16 01:27:49 -0700113struct kretprobe_blackpoint kretprobe_blacklist[] = {
114 {"__switch_to", }, /* This function switches only current task, but
115 doesn't switch kernel stack.*/
116 {NULL, NULL} /* Terminator */
117};
Masami Hiramatsu3f33ab12012-03-05 22:32:22 +0900118
Masami Hiramatsuf438d912007-10-16 01:27:49 -0700119const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist);
120
Masami Hiramatsu93266382014-04-17 17:18:14 +0900121static nokprobe_inline void
122__synthesize_relative_insn(void *from, void *to, u8 op)
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100123{
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500124 struct __arch_relative_insn {
125 u8 op;
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100126 s32 raddr;
Masami Hiramatsuf6841992012-09-28 17:15:22 +0900127 } __packed *insn;
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500128
129 insn = (struct __arch_relative_insn *)from;
130 insn->raddr = (s32)((long)(to) - ((long)(from) + 5));
131 insn->op = op;
132}
133
134/* Insert a jump instruction at address 'from', which jumps to address 'to'.*/
Masami Hiramatsu93266382014-04-17 17:18:14 +0900135void synthesize_reljump(void *from, void *to)
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500136{
137 __synthesize_relative_insn(from, to, RELATIVEJUMP_OPCODE);
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100138}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900139NOKPROBE_SYMBOL(synthesize_reljump);
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100140
Masami Hiramatsu3f33ab12012-03-05 22:32:22 +0900141/* Insert a call instruction at address 'from', which calls address 'to'.*/
Masami Hiramatsu93266382014-04-17 17:18:14 +0900142void synthesize_relcall(void *from, void *to)
Masami Hiramatsu3f33ab12012-03-05 22:32:22 +0900143{
144 __synthesize_relative_insn(from, to, RELATIVECALL_OPCODE);
145}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900146NOKPROBE_SYMBOL(synthesize_relcall);
Masami Hiramatsu3f33ab12012-03-05 22:32:22 +0900147
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100148/*
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900149 * Skip the prefixes of the instruction.
Harvey Harrison99309272008-01-30 13:32:14 +0100150 */
Masami Hiramatsu93266382014-04-17 17:18:14 +0900151static kprobe_opcode_t *skip_prefixes(kprobe_opcode_t *insn)
Harvey Harrison99309272008-01-30 13:32:14 +0100152{
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900153 insn_attr_t attr;
154
155 attr = inat_get_opcode_attribute((insn_byte_t)*insn);
156 while (inat_is_legacy_prefix(attr)) {
157 insn++;
158 attr = inat_get_opcode_attribute((insn_byte_t)*insn);
159 }
Harvey Harrison99309272008-01-30 13:32:14 +0100160#ifdef CONFIG_X86_64
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900161 if (inat_is_rex_prefix(attr))
162 insn++;
Harvey Harrison99309272008-01-30 13:32:14 +0100163#endif
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900164 return insn;
Harvey Harrison99309272008-01-30 13:32:14 +0100165}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900166NOKPROBE_SYMBOL(skip_prefixes);
Harvey Harrison99309272008-01-30 13:32:14 +0100167
168/*
Masami Hiramatsua8d11cd2017-03-29 14:05:06 +0900169 * Returns non-zero if INSN is boostable.
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100170 * RIP relative instructions are adjusted at copying time in 64 bits mode
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100171 */
Masami Hiramatsua8d11cd2017-03-29 14:05:06 +0900172int can_boost(struct insn *insn, void *addr)
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100173{
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100174 kprobe_opcode_t opcode;
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100175
Masami Hiramatsu75013fb2017-03-01 01:23:24 +0900176 if (search_exception_tables((unsigned long)addr))
Masami Hiramatsu30390882009-03-16 18:57:22 -0400177 return 0; /* Page fault may occur on this address. */
178
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100179 /* 2nd-byte opcode */
Masami Hiramatsua8d11cd2017-03-29 14:05:06 +0900180 if (insn->opcode.nbytes == 2)
181 return test_bit(insn->opcode.bytes[1],
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100182 (unsigned long *)twobyte_is_boostable);
Masami Hiramatsu17880e42017-03-29 13:59:15 +0900183
Masami Hiramatsua8d11cd2017-03-29 14:05:06 +0900184 if (insn->opcode.nbytes != 1)
Masami Hiramatsu17880e42017-03-29 13:59:15 +0900185 return 0;
186
187 /* Can't boost Address-size override prefix */
Masami Hiramatsua8d11cd2017-03-29 14:05:06 +0900188 if (unlikely(inat_is_address_size_prefix(insn->attr)))
Masami Hiramatsu17880e42017-03-29 13:59:15 +0900189 return 0;
190
Masami Hiramatsua8d11cd2017-03-29 14:05:06 +0900191 opcode = insn->opcode.bytes[0];
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100192
193 switch (opcode & 0xf0) {
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100194 case 0x60:
Masami Hiramatsu17880e42017-03-29 13:59:15 +0900195 /* can't boost "bound" */
196 return (opcode != 0x62);
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100197 case 0x70:
198 return 0; /* can't boost conditional jump */
Masami Hiramatsubd0b9062017-03-29 13:56:56 +0900199 case 0x90:
200 return opcode != 0x9a; /* can't boost call far */
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100201 case 0xc0:
202 /* can't boost software-interruptions */
203 return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
204 case 0xd0:
205 /* can boost AA* and XLAT */
206 return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
207 case 0xe0:
208 /* can boost in/out and absolute jmps */
209 return ((opcode & 0x04) || opcode == 0xea);
210 case 0xf0:
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100211 /* clear and set flags are boostable */
212 return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
213 default:
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100214 /* CS override prefix and call are not boostable */
215 return (opcode != 0x2e && opcode != 0x9a);
216 }
217}
218
Masami Hiramatsu3f33ab12012-03-05 22:32:22 +0900219static unsigned long
220__recover_probed_insn(kprobe_opcode_t *buf, unsigned long addr)
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400221{
222 struct kprobe *kp;
Petr Mladek650b7b22015-02-20 15:07:29 +0100223 unsigned long faddr;
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900224
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400225 kp = get_kprobe((void *)addr);
Petr Mladek650b7b22015-02-20 15:07:29 +0100226 faddr = ftrace_location(addr);
227 /*
Petr Mladek2a6730c2015-02-20 15:07:30 +0100228 * Addresses inside the ftrace location are refused by
229 * arch_check_ftrace_location(). Something went terribly wrong
230 * if such an address is checked here.
231 */
232 if (WARN_ON(faddr && faddr != addr))
233 return 0UL;
234 /*
Petr Mladek650b7b22015-02-20 15:07:29 +0100235 * Use the current code if it is not modified by Kprobe
236 * and it cannot be modified by ftrace.
237 */
238 if (!kp && !faddr)
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900239 return addr;
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400240
241 /*
Petr Mladek650b7b22015-02-20 15:07:29 +0100242 * Basically, kp->ainsn.insn has an original instruction.
243 * However, RIP-relative instruction can not do single-stepping
244 * at different place, __copy_instruction() tweaks the displacement of
245 * that instruction. In that case, we can't recover the instruction
246 * from the kp->ainsn.insn.
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400247 *
Petr Mladek650b7b22015-02-20 15:07:29 +0100248 * On the other hand, in case on normal Kprobe, kp->opcode has a copy
249 * of the first byte of the probed instruction, which is overwritten
250 * by int3. And the instruction at kp->addr is not modified by kprobes
251 * except for the first byte, we can recover the original instruction
252 * from it and kp->opcode.
253 *
254 * In case of Kprobes using ftrace, we do not have a copy of
255 * the original instruction. In fact, the ftrace location might
256 * be modified at anytime and even could be in an inconsistent state.
257 * Fortunately, we know that the original code is the ideal 5-byte
258 * long NOP.
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400259 */
Masami Hiramatsuea1e34f2017-03-29 14:03:56 +0900260 if (probe_kernel_read(buf, (void *)addr,
261 MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
262 return 0UL;
263
Petr Mladek650b7b22015-02-20 15:07:29 +0100264 if (faddr)
265 memcpy(buf, ideal_nops[NOP_ATOMIC5], 5);
266 else
267 buf[0] = kp->opcode;
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900268 return (unsigned long)buf;
269}
270
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900271/*
272 * Recover the probed instruction at addr for further analysis.
273 * Caller must lock kprobes by kprobe_mutex, or disable preemption
274 * for preventing to release referencing kprobes.
Masami Hiramatsuea1e34f2017-03-29 14:03:56 +0900275 * Returns zero if the instruction can not get recovered (or access failed).
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900276 */
Masami Hiramatsu3f33ab12012-03-05 22:32:22 +0900277unsigned long recover_probed_instruction(kprobe_opcode_t *buf, unsigned long addr)
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900278{
279 unsigned long __addr;
280
281 __addr = __recover_optprobed_insn(buf, addr);
282 if (__addr != addr)
283 return __addr;
284
285 return __recover_probed_insn(buf, addr);
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400286}
287
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400288/* Check if paddr is at an instruction boundary */
Masami Hiramatsu7ec8a972014-04-17 17:17:47 +0900289static int can_probe(unsigned long paddr)
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400290{
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900291 unsigned long addr, __addr, offset = 0;
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400292 struct insn insn;
293 kprobe_opcode_t buf[MAX_INSN_SIZE];
294
Namhyung Kim6abded72010-09-15 10:04:29 +0900295 if (!kallsyms_lookup_size_offset(paddr, NULL, &offset))
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400296 return 0;
297
298 /* Decode instructions */
299 addr = paddr - offset;
300 while (addr < paddr) {
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400301 /*
302 * Check if the instruction has been modified by another
303 * kprobe, in which case we replace the breakpoint by the
304 * original instruction in our buffer.
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900305 * Also, jump optimization will change the breakpoint to
306 * relative-jump. Since the relative-jump itself is
307 * normally used, we just go through if there is no kprobe.
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400308 */
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900309 __addr = recover_probed_instruction(buf, addr);
Petr Mladek2a6730c2015-02-20 15:07:30 +0100310 if (!__addr)
311 return 0;
Dave Hansen6ba48ff2014-11-14 07:39:57 -0800312 kernel_insn_init(&insn, (void *)__addr, MAX_INSN_SIZE);
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400313 insn_get_length(&insn);
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900314
315 /*
316 * Another debugging subsystem might insert this breakpoint.
317 * In that case, we can't recover it.
318 */
319 if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION)
320 return 0;
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400321 addr += insn.length;
322 }
323
324 return (addr == paddr);
325}
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327/*
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100328 * Returns non-zero if opcode modifies the interrupt flag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 */
Masami Hiramatsu7ec8a972014-04-17 17:17:47 +0900330static int is_IF_modifier(kprobe_opcode_t *insn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900332 /* Skip prefixes */
333 insn = skip_prefixes(insn);
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 switch (*insn) {
336 case 0xfa: /* cli */
337 case 0xfb: /* sti */
338 case 0xcf: /* iret/iretd */
339 case 0x9d: /* popf/popfd */
340 return 1;
341 }
Harvey Harrison99309272008-01-30 13:32:14 +0100342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return 0;
344}
345
346/*
Masami Hiramatsu129d17e2017-03-29 13:58:06 +0900347 * Copy an instruction with recovering modified instruction by kprobes
348 * and adjust the displacement if the instruction uses the %rip-relative
349 * addressing mode.
350 * This returns the length of copied instruction, or 0 if it has an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 */
Masami Hiramatsua8d11cd2017-03-29 14:05:06 +0900352int __copy_instruction(u8 *dest, u8 *src, struct insn *insn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500354 kprobe_opcode_t buf[MAX_INSN_SIZE];
Dave Hansen6ba48ff2014-11-14 07:39:57 -0800355 unsigned long recovered_insn =
356 recover_probed_instruction(buf, (unsigned long)src);
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900357
Masami Hiramatsua8d11cd2017-03-29 14:05:06 +0900358 if (!recovered_insn || !insn)
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900359 return 0;
Masami Hiramatsuea1e34f2017-03-29 14:03:56 +0900360
361 /* This can access kernel text if given address is not recovered */
Masami Hiramatsua8d11cd2017-03-29 14:05:06 +0900362 if (probe_kernel_read(dest, (void *)recovered_insn, MAX_INSN_SIZE))
363 return 0;
364
365 kernel_insn_init(insn, dest, MAX_INSN_SIZE);
366 insn_get_length(insn);
367
368 /* Another subsystem puts a breakpoint, failed to recover */
369 if (insn->opcode.bytes[0] == BREAKPOINT_INSTRUCTION)
Masami Hiramatsuea1e34f2017-03-29 14:03:56 +0900370 return 0;
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500371
372#ifdef CONFIG_X86_64
Masami Hiramatsu129d17e2017-03-29 13:58:06 +0900373 /* Only x86_64 has RIP relative instructions */
Masami Hiramatsua8d11cd2017-03-29 14:05:06 +0900374 if (insn_rip_relative(insn)) {
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400375 s64 newdisp;
376 u8 *disp;
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400377 /*
378 * The copied instruction uses the %rip-relative addressing
379 * mode. Adjust the displacement for the difference between
380 * the original location of this instruction and the location
381 * of the copy that will actually be run. The tricky bit here
382 * is making sure that the sign extension happens correctly in
383 * this calculation, since we need a signed 32-bit result to
384 * be sign-extended to 64 bits when it's added to the %rip
385 * value and yield the same 64-bit result that the sign-
386 * extension of the original signed 32-bit displacement would
387 * have given.
388 */
Masami Hiramatsua8d11cd2017-03-29 14:05:06 +0900389 newdisp = (u8 *) src + (s64) insn->displacement.value
390 - (u8 *) dest;
Masami Hiramatsu81013762013-04-04 19:42:30 +0900391 if ((s64) (s32) newdisp != newdisp) {
392 pr_err("Kprobes error: new displacement does not fit into s32 (%llx)\n", newdisp);
Masami Hiramatsua8d11cd2017-03-29 14:05:06 +0900393 pr_err("\tSrc: %p, Dest: %p, old disp: %x\n",
394 src, dest, insn->displacement.value);
Masami Hiramatsu81013762013-04-04 19:42:30 +0900395 return 0;
396 }
Masami Hiramatsua8d11cd2017-03-29 14:05:06 +0900397 disp = (u8 *) dest + insn_offset_displacement(insn);
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400398 *(s32 *) disp = (s32) newdisp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100400#endif
Masami Hiramatsua8d11cd2017-03-29 14:05:06 +0900401 return insn->length;
Harvey Harrison31f80e42008-01-30 13:32:16 +0100402}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Masami Hiramatsu804dec52017-03-29 14:00:25 +0900404/* Prepare reljump right after instruction to boost */
Masami Hiramatsua8d11cd2017-03-29 14:05:06 +0900405static void prepare_boost(struct kprobe *p, struct insn *insn)
Masami Hiramatsu804dec52017-03-29 14:00:25 +0900406{
Masami Hiramatsua8d11cd2017-03-29 14:05:06 +0900407 if (can_boost(insn, p->addr) &&
408 MAX_INSN_SIZE - insn->length >= RELATIVEJUMP_SIZE) {
Masami Hiramatsu804dec52017-03-29 14:00:25 +0900409 /*
410 * These instructions can be executed directly if it
411 * jumps back to correct address.
412 */
Masami Hiramatsua8d11cd2017-03-29 14:05:06 +0900413 synthesize_reljump(p->ainsn.insn + insn->length,
414 p->addr + insn->length);
Masami Hiramatsu490154b2017-03-29 14:01:35 +0900415 p->ainsn.boostable = true;
Masami Hiramatsu804dec52017-03-29 14:00:25 +0900416 } else {
Masami Hiramatsu490154b2017-03-29 14:01:35 +0900417 p->ainsn.boostable = false;
Masami Hiramatsu804dec52017-03-29 14:00:25 +0900418 }
419}
420
Masami Hiramatsuc93f5cf2017-05-25 19:38:17 +0900421/* Recover page to RW mode before releasing it */
422void free_insn_page(void *page)
423{
424 set_memory_nx((unsigned long)page & PAGE_MASK, 1);
425 set_memory_rw((unsigned long)page & PAGE_MASK, 1);
426 module_memfree(page);
427}
428
Masami Hiramatsu7ec8a972014-04-17 17:17:47 +0900429static int arch_copy_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Masami Hiramatsua8d11cd2017-03-29 14:05:06 +0900431 struct insn insn;
Masami Hiramatsu804dec52017-03-29 14:00:25 +0900432 int len;
Masami Hiramatsu003002e2013-06-05 12:12:16 +0900433
Masami Hiramatsud0381c82017-03-29 14:02:46 +0900434 set_memory_rw((unsigned long)p->ainsn.insn & PAGE_MASK, 1);
435
Masami Hiramatsu46484682012-03-05 22:32:16 +0900436 /* Copy an instruction with recovering if other optprobe modifies it.*/
Masami Hiramatsua8d11cd2017-03-29 14:05:06 +0900437 len = __copy_instruction(p->ainsn.insn, p->addr, &insn);
Masami Hiramatsu804dec52017-03-29 14:00:25 +0900438 if (!len)
Masami Hiramatsu003002e2013-06-05 12:12:16 +0900439 return -EINVAL;
Harvey Harrison31f80e42008-01-30 13:32:16 +0100440
Masami Hiramatsu46484682012-03-05 22:32:16 +0900441 /*
442 * __copy_instruction can modify the displacement of the instruction,
443 * but it doesn't affect boostable check.
444 */
Masami Hiramatsua8d11cd2017-03-29 14:05:06 +0900445 prepare_boost(p, &insn);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100446
Masami Hiramatsud0381c82017-03-29 14:02:46 +0900447 set_memory_ro((unsigned long)p->ainsn.insn & PAGE_MASK, 1);
448
Masami Hiramatsu9a556ab2013-03-14 20:52:43 +0900449 /* Check whether the instruction modifies Interrupt Flag or not */
450 p->ainsn.if_modifier = is_IF_modifier(p->ainsn.insn);
451
Masami Hiramatsu46484682012-03-05 22:32:16 +0900452 /* Also, displacement change doesn't affect the first byte */
453 p->opcode = p->ainsn.insn[0];
Masami Hiramatsu003002e2013-06-05 12:12:16 +0900454
455 return 0;
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700456}
457
Masami Hiramatsu7ec8a972014-04-17 17:17:47 +0900458int arch_prepare_kprobe(struct kprobe *p)
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100459{
Masami Hiramatsu38115f22017-07-21 23:45:52 +0900460 int ret;
461
Masami Hiramatsu4554dbc2010-02-02 16:49:18 -0500462 if (alternatives_text_reserved(p->addr, p->addr))
463 return -EINVAL;
464
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400465 if (!can_probe((unsigned long)p->addr))
466 return -EILSEQ;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100467 /* insn: must be on special executable page on x86. */
468 p->ainsn.insn = get_insn_slot();
469 if (!p->ainsn.insn)
470 return -ENOMEM;
Masami Hiramatsu003002e2013-06-05 12:12:16 +0900471
Masami Hiramatsu38115f22017-07-21 23:45:52 +0900472 ret = arch_copy_kprobe(p);
473 if (ret) {
474 free_insn_slot(p->ainsn.insn, 0);
475 p->ainsn.insn = NULL;
476 }
477
478 return ret;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100479}
480
Masami Hiramatsu7ec8a972014-04-17 17:17:47 +0900481void arch_arm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700482{
Andi Kleen19d36cc2007-07-22 11:12:31 +0200483 text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1);
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700484}
485
Masami Hiramatsu7ec8a972014-04-17 17:17:47 +0900486void arch_disarm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700487{
Andi Kleen19d36cc2007-07-22 11:12:31 +0200488 text_poke(p->addr, &p->opcode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
Masami Hiramatsu7ec8a972014-04-17 17:17:47 +0900491void arch_remove_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Masami Hiramatsu12941562009-01-06 14:41:50 -0800493 if (p->ainsn.insn) {
Masami Hiramatsu490154b2017-03-29 14:01:35 +0900494 free_insn_slot(p->ainsn.insn, p->ainsn.boostable);
Masami Hiramatsu12941562009-01-06 14:41:50 -0800495 p->ainsn.insn = NULL;
496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
Masami Hiramatsu93266382014-04-17 17:18:14 +0900499static nokprobe_inline void
500save_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700501{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800502 kcb->prev_kprobe.kp = kprobe_running();
503 kcb->prev_kprobe.status = kcb->kprobe_status;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100504 kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags;
505 kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700506}
507
Masami Hiramatsu93266382014-04-17 17:18:14 +0900508static nokprobe_inline void
509restore_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700510{
Christoph Lameterb76834b2010-12-06 11:16:25 -0600511 __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800512 kcb->kprobe_status = kcb->prev_kprobe.status;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100513 kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags;
514 kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700515}
516
Masami Hiramatsu93266382014-04-17 17:18:14 +0900517static nokprobe_inline void
518set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
519 struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700520{
Christoph Lameterb76834b2010-12-06 11:16:25 -0600521 __this_cpu_write(current_kprobe, p);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100522 kcb->kprobe_saved_flags = kcb->kprobe_old_flags
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100523 = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF));
Masami Hiramatsu9a556ab2013-03-14 20:52:43 +0900524 if (p->ainsn.if_modifier)
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100525 kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700526}
527
Masami Hiramatsu93266382014-04-17 17:18:14 +0900528static nokprobe_inline void clear_btf(void)
Roland McGrath1ecc7982008-01-30 13:30:54 +0100529{
Peter Zijlstraea8e61b2010-03-25 14:51:51 +0100530 if (test_thread_flag(TIF_BLOCKSTEP)) {
531 unsigned long debugctl = get_debugctlmsr();
532
533 debugctl &= ~DEBUGCTLMSR_BTF;
534 update_debugctlmsr(debugctl);
535 }
Roland McGrath1ecc7982008-01-30 13:30:54 +0100536}
537
Masami Hiramatsu93266382014-04-17 17:18:14 +0900538static nokprobe_inline void restore_btf(void)
Roland McGrath1ecc7982008-01-30 13:30:54 +0100539{
Peter Zijlstraea8e61b2010-03-25 14:51:51 +0100540 if (test_thread_flag(TIF_BLOCKSTEP)) {
541 unsigned long debugctl = get_debugctlmsr();
542
543 debugctl |= DEBUGCTLMSR_BTF;
544 update_debugctlmsr(debugctl);
545 }
Roland McGrath1ecc7982008-01-30 13:30:54 +0100546}
547
Masami Hiramatsu93266382014-04-17 17:18:14 +0900548void arch_prepare_kretprobe(struct kretprobe_instance *ri, struct pt_regs *regs)
Rusty Lynch73649da2005-06-23 00:09:23 -0700549{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100550 unsigned long *sara = stack_addr(regs);
Rusty Lynch73649da2005-06-23 00:09:23 -0700551
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700552 ri->ret_addr = (kprobe_opcode_t *) *sara;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100553
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700554 /* Replace the return addr with trampoline addr */
555 *sara = (unsigned long) &kretprobe_trampoline;
Rusty Lynch73649da2005-06-23 00:09:23 -0700556}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900557NOKPROBE_SYMBOL(arch_prepare_kretprobe);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100558
Masami Hiramatsu93266382014-04-17 17:18:14 +0900559static void setup_singlestep(struct kprobe *p, struct pt_regs *regs,
560 struct kprobe_ctlblk *kcb, int reenter)
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100561{
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500562 if (setup_detour_execution(p, regs, reenter))
563 return;
564
Masami Hiramatsu615d0eb2010-02-02 16:49:04 -0500565#if !defined(CONFIG_PREEMPT)
Masami Hiramatsu490154b2017-03-29 14:01:35 +0900566 if (p->ainsn.boostable && !p->post_handler) {
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100567 /* Boost up -- we can execute copied instructions directly */
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500568 if (!reenter)
569 reset_current_kprobe();
570 /*
571 * Reentering boosted probe doesn't reset current_kprobe,
572 * nor set current_kprobe, because it doesn't use single
573 * stepping.
574 */
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100575 regs->ip = (unsigned long)p->ainsn.insn;
576 preempt_enable_no_resched();
577 return;
578 }
579#endif
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500580 if (reenter) {
581 save_previous_kprobe(kcb);
582 set_current_kprobe(p, regs, kcb);
583 kcb->kprobe_status = KPROBE_REENTER;
584 } else
585 kcb->kprobe_status = KPROBE_HIT_SS;
586 /* Prepare real single stepping */
587 clear_btf();
588 regs->flags |= X86_EFLAGS_TF;
589 regs->flags &= ~X86_EFLAGS_IF;
590 /* single step inline if the instruction is an int3 */
591 if (p->opcode == BREAKPOINT_INSTRUCTION)
592 regs->ip = (unsigned long)p->addr;
593 else
594 regs->ip = (unsigned long)p->ainsn.insn;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100595}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900596NOKPROBE_SYMBOL(setup_singlestep);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100597
Harvey Harrison40102d42008-01-30 13:32:02 +0100598/*
599 * We have reentered the kprobe_handler(), since another probe was hit while
600 * within the handler. We save the original kprobes variables and just single
601 * step on the instruction of the new probe without calling any user handlers.
602 */
Masami Hiramatsu93266382014-04-17 17:18:14 +0900603static int reenter_kprobe(struct kprobe *p, struct pt_regs *regs,
604 struct kprobe_ctlblk *kcb)
Harvey Harrison40102d42008-01-30 13:32:02 +0100605{
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100606 switch (kcb->kprobe_status) {
607 case KPROBE_HIT_SSDONE:
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100608 case KPROBE_HIT_ACTIVE:
Masami Hiramatsu6a5022a2014-04-17 17:16:51 +0900609 case KPROBE_HIT_SS:
Abhishek Sagarfb8830e2008-01-30 13:33:13 +0100610 kprobes_inc_nmissed_count(p);
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500611 setup_singlestep(p, regs, kcb, 1);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100612 break;
Masami Hiramatsu6a5022a2014-04-17 17:16:51 +0900613 case KPROBE_REENTER:
Masami Hiramatsue9afe9e2009-08-27 13:22:58 -0400614 /* A probe has been hit in the codepath leading up to, or just
615 * after, single-stepping of a probed instruction. This entire
616 * codepath should strictly reside in .kprobes.text section.
617 * Raise a BUG or we'll continue in an endless reentering loop
618 * and eventually a stack overflow.
619 */
620 printk(KERN_WARNING "Unrecoverable kprobe detected at %p.\n",
621 p->addr);
622 dump_kprobe(p);
623 BUG();
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100624 default:
625 /* impossible cases */
626 WARN_ON(1);
Abhishek Sagarfb8830e2008-01-30 13:33:13 +0100627 return 0;
Masami Hiramatsu59e87cd2008-01-30 13:32:02 +0100628 }
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100629
Masami Hiramatsu59e87cd2008-01-30 13:32:02 +0100630 return 1;
Harvey Harrison40102d42008-01-30 13:32:02 +0100631}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900632NOKPROBE_SYMBOL(reenter_kprobe);
Rusty Lynch73649da2005-06-23 00:09:23 -0700633
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100634/*
635 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200636 * remain disabled throughout this function.
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100637 */
Masami Hiramatsu93266382014-04-17 17:18:14 +0900638int kprobe_int3_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100640 kprobe_opcode_t *addr;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100641 struct kprobe *p;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800642 struct kprobe_ctlblk *kcb;
643
Andy Lutomirskif39b6f02015-03-18 18:33:33 -0700644 if (user_mode(regs))
Andy Lutomirski0cdd1922014-07-11 10:27:01 -0700645 return 0;
646
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100647 addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800648 /*
649 * We don't want to be preempted for the entire
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100650 * duration of kprobe processing. We conditionally
651 * re-enable preemption at the end of this function,
652 * and also in reenter_kprobe() and setup_singlestep().
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800653 */
654 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100656 kcb = get_kprobe_ctlblk();
Harvey Harrisonb9760152008-01-30 13:32:19 +0100657 p = get_kprobe(addr);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100658
Harvey Harrisonb9760152008-01-30 13:32:19 +0100659 if (p) {
Harvey Harrisonb9760152008-01-30 13:32:19 +0100660 if (kprobe_running()) {
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100661 if (reenter_kprobe(p, regs, kcb))
662 return 1;
Harvey Harrisonb9760152008-01-30 13:32:19 +0100663 } else {
664 set_current_kprobe(p, regs, kcb);
665 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 /*
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100668 * If we have no pre-handler or it returned 0, we
669 * continue with normal processing. If we have a
670 * pre-handler and it returned non-zero, it prepped
671 * for calling the break_handler below on re-entry
672 * for jprobe processing, so get out doing nothing
673 * more here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 */
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100675 if (!p->pre_handler || !p->pre_handler(p, regs))
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500676 setup_singlestep(p, regs, kcb, 0);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100677 return 1;
Harvey Harrisonb9760152008-01-30 13:32:19 +0100678 }
Masami Hiramatsu829e9242010-04-27 18:33:49 -0400679 } else if (*addr != BREAKPOINT_INSTRUCTION) {
680 /*
681 * The breakpoint instruction was removed right
682 * after we hit it. Another cpu has removed
683 * either a probepoint or a debugger breakpoint
684 * at this address. In either case, no further
685 * handling of this interrupt is appropriate.
686 * Back up over the (now missing) int3 and run
687 * the original instruction.
688 */
689 regs->ip = (unsigned long)addr;
690 preempt_enable_no_resched();
691 return 1;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100692 } else if (kprobe_running()) {
Christoph Lameterb76834b2010-12-06 11:16:25 -0600693 p = __this_cpu_read(current_kprobe);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100694 if (p->break_handler && p->break_handler(p, regs)) {
Masami Hiramatsue7dbfe32012-09-28 17:15:20 +0900695 if (!skip_singlestep(p, regs, kcb))
696 setup_singlestep(p, regs, kcb, 0);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100697 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100699 } /* else: not a kprobe fault; let the kernel handle it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800701 preempt_enable_no_resched();
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100702 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900704NOKPROBE_SYMBOL(kprobe_int3_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706/*
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100707 * When a retprobed function returns, this code saves registers and
708 * calls trampoline_handler() runs, which calls the kretprobe's handler.
Rusty Lynch73649da2005-06-23 00:09:23 -0700709 */
Josh Poimboeufc1c355c2016-01-21 16:49:28 -0600710asm(
711 ".global kretprobe_trampoline\n"
712 ".type kretprobe_trampoline, @function\n"
713 "kretprobe_trampoline:\n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100714#ifdef CONFIG_X86_64
Josh Poimboeufc1c355c2016-01-21 16:49:28 -0600715 /* We don't bother saving the ss register */
716 " pushq %rsp\n"
717 " pushfq\n"
718 SAVE_REGS_STRING
719 " movq %rsp, %rdi\n"
720 " call trampoline_handler\n"
721 /* Replace saved sp with true return address. */
722 " movq %rax, 152(%rsp)\n"
723 RESTORE_REGS_STRING
724 " popfq\n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100725#else
Josh Poimboeufc1c355c2016-01-21 16:49:28 -0600726 " pushf\n"
727 SAVE_REGS_STRING
728 " movl %esp, %eax\n"
729 " call trampoline_handler\n"
730 /* Move flags to cs */
731 " movl 56(%esp), %edx\n"
732 " movl %edx, 52(%esp)\n"
733 /* Replace saved flags with true return address. */
734 " movl %eax, 56(%esp)\n"
735 RESTORE_REGS_STRING
736 " popf\n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100737#endif
Josh Poimboeufc1c355c2016-01-21 16:49:28 -0600738 " ret\n"
739 ".size kretprobe_trampoline, .-kretprobe_trampoline\n"
740);
Masami Hiramatsu93266382014-04-17 17:18:14 +0900741NOKPROBE_SYMBOL(kretprobe_trampoline);
Josh Poimboeuf87aaff22016-02-28 22:22:40 -0600742STACK_FRAME_NON_STANDARD(kretprobe_trampoline);
Rusty Lynch73649da2005-06-23 00:09:23 -0700743
744/*
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100745 * Called from kretprobe_trampoline
Rusty Lynch73649da2005-06-23 00:09:23 -0700746 */
Masami Hiramatsu93266382014-04-17 17:18:14 +0900747__visible __used void *trampoline_handler(struct pt_regs *regs)
Rusty Lynch73649da2005-06-23 00:09:23 -0700748{
bibo,mao62c27be2006-10-02 02:17:33 -0700749 struct kretprobe_instance *ri = NULL;
bibo,mao99219a32006-10-02 02:17:35 -0700750 struct hlist_head *head, empty_rp;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800751 struct hlist_node *tmp;
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800752 unsigned long flags, orig_ret_address = 0;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100753 unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
KUMANO Syuhei737480a2010-08-15 15:18:04 +0900754 kprobe_opcode_t *correct_ret_addr = NULL;
Rusty Lynch73649da2005-06-23 00:09:23 -0700755
bibo,mao99219a32006-10-02 02:17:35 -0700756 INIT_HLIST_HEAD(&empty_rp);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700757 kretprobe_hash_lock(current, &head, &flags);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100758 /* fixup registers */
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100759#ifdef CONFIG_X86_64
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100760 regs->cs = __KERNEL_CS;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100761#else
762 regs->cs = __KERNEL_CS | get_kernel_rpl();
Masami Hiramatsufee039a2009-03-23 10:14:52 -0400763 regs->gs = 0;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100764#endif
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100765 regs->ip = trampoline_address;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100766 regs->orig_ax = ~0UL;
Rusty Lynch73649da2005-06-23 00:09:23 -0700767
Rusty Lynchba8af122005-06-27 15:17:10 -0700768 /*
769 * It is possible to have multiple instances associated with a given
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100770 * task either because multiple functions in the call path have
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200771 * return probes installed on them, and/or more than one
Rusty Lynchba8af122005-06-27 15:17:10 -0700772 * return probe was registered for a target function.
773 *
774 * We can handle this because:
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100775 * - instances are always pushed into the head of the list
Rusty Lynchba8af122005-06-27 15:17:10 -0700776 * - when multiple return probes are registered for the same
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100777 * function, the (chronologically) first instance's ret_addr
778 * will be the real return address, and all the rest will
779 * point to kretprobe_trampoline.
Rusty Lynchba8af122005-06-27 15:17:10 -0700780 */
Masami Hiramatsub6263172017-02-06 18:55:43 +0900781 hlist_for_each_entry(ri, head, hlist) {
bibo,mao62c27be2006-10-02 02:17:33 -0700782 if (ri->task != current)
Rusty Lynchba8af122005-06-27 15:17:10 -0700783 /* another task is sharing our hash bucket */
bibo,mao62c27be2006-10-02 02:17:33 -0700784 continue;
Rusty Lynch73649da2005-06-23 00:09:23 -0700785
Rusty Lynchba8af122005-06-27 15:17:10 -0700786 orig_ret_address = (unsigned long)ri->ret_addr;
Rusty Lynchba8af122005-06-27 15:17:10 -0700787
788 if (orig_ret_address != trampoline_address)
789 /*
790 * This is the real return address. Any other
791 * instances associated with this task are for
792 * other calls deeper on the call stack
793 */
794 break;
Rusty Lynch73649da2005-06-23 00:09:23 -0700795 }
Rusty Lynchba8af122005-06-27 15:17:10 -0700796
Ananth N Mavinakayanahalli0f95b7f2007-05-08 00:28:27 -0700797 kretprobe_assert(ri, orig_ret_address, trampoline_address);
Rusty Lynchba8af122005-06-27 15:17:10 -0700798
KUMANO Syuhei737480a2010-08-15 15:18:04 +0900799 correct_ret_addr = ri->ret_addr;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800800 hlist_for_each_entry_safe(ri, tmp, head, hlist) {
KUMANO Syuhei737480a2010-08-15 15:18:04 +0900801 if (ri->task != current)
802 /* another task is sharing our hash bucket */
803 continue;
804
805 orig_ret_address = (unsigned long)ri->ret_addr;
806 if (ri->rp && ri->rp->handler) {
Christoph Lameterb76834b2010-12-06 11:16:25 -0600807 __this_cpu_write(current_kprobe, &ri->rp->kp);
KUMANO Syuhei737480a2010-08-15 15:18:04 +0900808 get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
809 ri->ret_addr = correct_ret_addr;
810 ri->rp->handler(ri, regs);
Christoph Lameterb76834b2010-12-06 11:16:25 -0600811 __this_cpu_write(current_kprobe, NULL);
KUMANO Syuhei737480a2010-08-15 15:18:04 +0900812 }
813
814 recycle_rp_inst(ri, &empty_rp);
815
816 if (orig_ret_address != trampoline_address)
817 /*
818 * This is the real return address. Any other
819 * instances associated with this task are for
820 * other calls deeper on the call stack
821 */
822 break;
823 }
824
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700825 kretprobe_hash_unlock(current, &flags);
Rusty Lynchba8af122005-06-27 15:17:10 -0700826
Sasha Levinb67bfe02013-02-27 17:06:00 -0800827 hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
bibo,mao99219a32006-10-02 02:17:35 -0700828 hlist_del(&ri->hlist);
829 kfree(ri);
830 }
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100831 return (void *)orig_ret_address;
Rusty Lynch73649da2005-06-23 00:09:23 -0700832}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900833NOKPROBE_SYMBOL(trampoline_handler);
Rusty Lynch73649da2005-06-23 00:09:23 -0700834
835/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 * Called after single-stepping. p->addr is the address of the
837 * instruction whose first byte has been replaced by the "int 3"
838 * instruction. To avoid the SMP problems that can occur when we
839 * temporarily put back the original opcode to single-step, we
840 * single-stepped a copy of the instruction. The address of this
841 * copy is p->ainsn.insn.
842 *
843 * This function prepares to return from the post-single-step
844 * interrupt. We have to fix up the stack as follows:
845 *
846 * 0) Except in the case of absolute or indirect jump or call instructions,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100847 * the new ip is relative to the copied instruction. We need to make
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 * it relative to the original instruction.
849 *
850 * 1) If the single-stepped instruction was pushfl, then the TF and IF
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100851 * flags are set in the just-pushed flags, and may need to be cleared.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 *
853 * 2) If the single-stepped instruction was a call, the return address
854 * that is atop the stack is the address following the copied instruction.
855 * We need to make it the address following the original instruction.
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100856 *
857 * If this is the first time we've single-stepped the instruction at
858 * this probepoint, and the instruction is boostable, boost it: add a
859 * jump instruction after the copied instruction, that jumps to the next
860 * instruction after the probepoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 */
Masami Hiramatsu93266382014-04-17 17:18:14 +0900862static void resume_execution(struct kprobe *p, struct pt_regs *regs,
863 struct kprobe_ctlblk *kcb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100865 unsigned long *tos = stack_addr(regs);
866 unsigned long copy_ip = (unsigned long)p->ainsn.insn;
867 unsigned long orig_ip = (unsigned long)p->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 kprobe_opcode_t *insn = p->ainsn.insn;
869
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900870 /* Skip prefixes */
871 insn = skip_prefixes(insn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100873 regs->flags &= ~X86_EFLAGS_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 switch (*insn) {
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100875 case 0x9c: /* pushfl */
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100876 *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100877 *tos |= kcb->kprobe_old_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 break;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100879 case 0xc2: /* iret/ret/lret */
880 case 0xc3:
Prasanna S Panchamukhi0b9e2ca2005-05-05 16:15:40 -0700881 case 0xca:
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100882 case 0xcb:
883 case 0xcf:
884 case 0xea: /* jmp absolute -- ip is correct */
885 /* ip is already adjusted, no more changes required */
Masami Hiramatsu490154b2017-03-29 14:01:35 +0900886 p->ainsn.boostable = true;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100887 goto no_change;
888 case 0xe8: /* call relative - Fix return addr */
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100889 *tos = orig_ip + (*tos - copy_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 break;
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100891#ifdef CONFIG_X86_32
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100892 case 0x9a: /* call absolute -- same as call absolute, indirect */
893 *tos = orig_ip + (*tos - copy_ip);
894 goto no_change;
895#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 case 0xff:
Satoshi Oshimadc49e342006-05-20 15:00:21 -0700897 if ((insn[1] & 0x30) == 0x10) {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100898 /*
899 * call absolute, indirect
900 * Fix return addr; ip is correct.
901 * But this is not boostable
902 */
903 *tos = orig_ip + (*tos - copy_ip);
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100904 goto no_change;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100905 } else if (((insn[1] & 0x31) == 0x20) ||
906 ((insn[1] & 0x31) == 0x21)) {
907 /*
908 * jmp near and far, absolute indirect
909 * ip is correct. And this is boostable
910 */
Masami Hiramatsu490154b2017-03-29 14:01:35 +0900911 p->ainsn.boostable = true;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100912 goto no_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 default:
915 break;
916 }
917
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100918 regs->ip += orig_ip - copy_ip;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100919
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100920no_change:
Roland McGrath1ecc7982008-01-30 13:30:54 +0100921 restore_btf();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900923NOKPROBE_SYMBOL(resume_execution);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100925/*
926 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200927 * remain disabled throughout this function.
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100928 */
Masami Hiramatsu93266382014-04-17 17:18:14 +0900929int kprobe_debug_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800931 struct kprobe *cur = kprobe_running();
932 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
933
934 if (!cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return 0;
936
Yakov Lerneracb5b8a2008-03-16 03:21:21 -0500937 resume_execution(cur, regs, kcb);
938 regs->flags |= kcb->kprobe_saved_flags;
Yakov Lerneracb5b8a2008-03-16 03:21:21 -0500939
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800940 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
941 kcb->kprobe_status = KPROBE_HIT_SSDONE;
942 cur->post_handler(cur, regs, 0);
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100945 /* Restore back the original saved kprobes variables and continue. */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800946 if (kcb->kprobe_status == KPROBE_REENTER) {
947 restore_previous_kprobe(kcb);
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700948 goto out;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700949 }
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800950 reset_current_kprobe();
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700951out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 preempt_enable_no_resched();
953
954 /*
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100955 * if somebody else is singlestepping across a probe point, flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 * will have TF set, in which case, continue the remaining processing
957 * of do_debug, as if this is not a probe hit.
958 */
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100959 if (regs->flags & X86_EFLAGS_TF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return 0;
961
962 return 1;
963}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900964NOKPROBE_SYMBOL(kprobe_debug_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Masami Hiramatsu93266382014-04-17 17:18:14 +0900966int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800968 struct kprobe *cur = kprobe_running();
969 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
970
Masami Hiramatsu6381c242014-04-17 17:16:44 +0900971 if (unlikely(regs->ip == (unsigned long)cur->ainsn.insn)) {
972 /* This must happen on single-stepping */
973 WARN_ON(kcb->kprobe_status != KPROBE_HIT_SS &&
974 kcb->kprobe_status != KPROBE_REENTER);
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800975 /*
976 * We are here because the instruction being single
977 * stepped caused a page fault. We reset the current
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100978 * kprobe and the ip points back to the probe address
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800979 * and allow the page fault handler to continue as a
980 * normal page fault.
981 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100982 regs->ip = (unsigned long)cur->addr;
Masami Hiramatsudcfc4722016-06-11 23:06:53 +0900983 /*
984 * Trap flag (TF) has been set here because this fault
985 * happened where the single stepping will be done.
986 * So clear it by resetting the current kprobe:
987 */
988 regs->flags &= ~X86_EFLAGS_TF;
989
990 /*
991 * If the TF flag was set before the kprobe hit,
992 * don't touch it:
993 */
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100994 regs->flags |= kcb->kprobe_old_flags;
Masami Hiramatsudcfc4722016-06-11 23:06:53 +0900995
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800996 if (kcb->kprobe_status == KPROBE_REENTER)
997 restore_previous_kprobe(kcb);
998 else
999 reset_current_kprobe();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 preempt_enable_no_resched();
Masami Hiramatsu6381c242014-04-17 17:16:44 +09001001 } else if (kcb->kprobe_status == KPROBE_HIT_ACTIVE ||
1002 kcb->kprobe_status == KPROBE_HIT_SSDONE) {
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -08001003 /*
1004 * We increment the nmissed count for accounting,
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001005 * we can also use npre/npostfault count for accounting
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -08001006 * these specific fault cases.
1007 */
1008 kprobes_inc_nmissed_count(cur);
1009
1010 /*
1011 * We come here because instructions in the pre/post
1012 * handler caused the page_fault, this could happen
1013 * if handler tries to access user space by
1014 * copy_from_user(), get_user() etc. Let the
1015 * user-specified handler try to fix it first.
1016 */
1017 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
1018 return 1;
1019
1020 /*
1021 * In case the user-specified fault handler returned
1022 * zero, try to fix up.
1023 */
Tony Luck548acf12016-02-17 10:20:12 -08001024 if (fixup_exception(regs, trapnr))
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001025 return 1;
Harvey Harrison6d485832008-01-30 13:31:41 +01001026
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -08001027 /*
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001028 * fixup routine could not handle it,
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -08001029 * Let do_page_fault() fix it.
1030 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
Masami Hiramatsu6381c242014-04-17 17:16:44 +09001032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 return 0;
1034}
Masami Hiramatsu93266382014-04-17 17:18:14 +09001035NOKPROBE_SYMBOL(kprobe_fault_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037/*
1038 * Wrapper routine for handling exceptions.
1039 */
Masami Hiramatsu93266382014-04-17 17:18:14 +09001040int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
1041 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
Jan Engelhardtade1af72008-01-30 13:33:23 +01001043 struct die_args *args = data;
Ananth N Mavinakayanahalli66ff2d062005-11-07 01:00:07 -08001044 int ret = NOTIFY_DONE;
1045
Andy Lutomirskif39b6f02015-03-18 18:33:33 -07001046 if (args->regs && user_mode(args->regs))
bibo,mao2326c772006-03-26 01:38:21 -08001047 return ret;
1048
Masami Hiramatsu6f6343f2014-04-17 17:17:33 +09001049 if (val == DIE_GPF) {
Quentin Barnesb506a9d2008-01-30 13:32:32 +01001050 /*
1051 * To be potentially processing a kprobe fault and to
1052 * trust the result from kprobe_running(), we have
1053 * be non-preemptible.
1054 */
1055 if (!preemptible() && kprobe_running() &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 kprobe_fault_handler(args->regs, args->trapnr))
Ananth N Mavinakayanahalli66ff2d062005-11-07 01:00:07 -08001057 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
Ananth N Mavinakayanahalli66ff2d062005-11-07 01:00:07 -08001059 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
Masami Hiramatsu93266382014-04-17 17:18:14 +09001061NOKPROBE_SYMBOL(kprobe_exceptions_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Masami Hiramatsu93266382014-04-17 17:18:14 +09001063int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
1065 struct jprobe *jp = container_of(p, struct jprobe, kp);
1066 unsigned long addr;
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001067 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001069 kcb->jprobe_saved_regs = *regs;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001070 kcb->jprobe_saved_sp = stack_addr(regs);
1071 addr = (unsigned long)(kcb->jprobe_saved_sp);
1072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 /*
1074 * As Linus pointed out, gcc assumes that the callee
1075 * owns the argument space and could overwrite it, e.g.
1076 * tailcall optimization. So, to be absolutely safe
1077 * we also save and restore enough stack bytes to cover
1078 * the argument area.
Dmitry Vyukov92541392016-10-11 14:13:38 +02001079 * Use __memcpy() to avoid KASAN stack out-of-bounds reports as we copy
1080 * raw stack chunk with redzones:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 */
Dmitry Vyukov92541392016-10-11 14:13:38 +02001082 __memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr, MIN_STACK_SIZE(addr));
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001083 regs->flags &= ~X86_EFLAGS_IF;
Peter Zijlstra58dfe882007-10-11 22:25:25 +02001084 trace_hardirqs_off();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001085 regs->ip = (unsigned long)(jp->entry);
Steven Rostedt (Red Hat)237d28d2015-01-12 12:12:03 -05001086
1087 /*
1088 * jprobes use jprobe_return() which skips the normal return
1089 * path of the function, and this messes up the accounting of the
1090 * function graph tracer to get messed up.
1091 *
1092 * Pause function graph tracing while performing the jprobe function.
1093 */
1094 pause_graph_tracing();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 return 1;
1096}
Masami Hiramatsu93266382014-04-17 17:18:14 +09001097NOKPROBE_SYMBOL(setjmp_pre_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Masami Hiramatsu93266382014-04-17 17:18:14 +09001099void jprobe_return(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001101 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1102
Dmitry Vyukov9f7d4162016-10-14 16:07:23 +02001103 /* Unpoison stack redzones in the frames we are going to jump over. */
1104 kasan_unpoison_stack_above_sp_to(kcb->jprobe_saved_sp);
1105
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001106 asm volatile (
1107#ifdef CONFIG_X86_64
1108 " xchg %%rbx,%%rsp \n"
1109#else
1110 " xchgl %%ebx,%%esp \n"
1111#endif
1112 " int3 \n"
1113 " .globl jprobe_return_end\n"
1114 " jprobe_return_end: \n"
1115 " nop \n"::"b"
1116 (kcb->jprobe_saved_sp):"memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117}
Masami Hiramatsu93266382014-04-17 17:18:14 +09001118NOKPROBE_SYMBOL(jprobe_return);
1119NOKPROBE_SYMBOL(jprobe_return_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Masami Hiramatsu93266382014-04-17 17:18:14 +09001121int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001123 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001124 u8 *addr = (u8 *) (regs->ip - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 struct jprobe *jp = container_of(p, struct jprobe, kp);
Steven Rostedt (Red Hat)237d28d2015-01-12 12:12:03 -05001126 void *saved_sp = kcb->jprobe_saved_sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001128 if ((addr > (u8 *) jprobe_return) &&
1129 (addr < (u8 *) jprobe_return_end)) {
Steven Rostedt (Red Hat)237d28d2015-01-12 12:12:03 -05001130 if (stack_addr(regs) != saved_sp) {
Masami Hiramatsu29b6cd72007-12-18 18:05:58 +01001131 struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001132 printk(KERN_ERR
1133 "current sp %p does not match saved sp %p\n",
Steven Rostedt (Red Hat)237d28d2015-01-12 12:12:03 -05001134 stack_addr(regs), saved_sp);
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001135 printk(KERN_ERR "Saved registers for jprobe %p\n", jp);
Jan Beulich57da8b92012-05-09 08:47:37 +01001136 show_regs(saved_regs);
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001137 printk(KERN_ERR "Current registers\n");
Jan Beulich57da8b92012-05-09 08:47:37 +01001138 show_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 BUG();
1140 }
Steven Rostedt (Red Hat)237d28d2015-01-12 12:12:03 -05001141 /* It's OK to start function graph tracing again */
1142 unpause_graph_tracing();
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001143 *regs = kcb->jprobe_saved_regs;
Dmitry Vyukov92541392016-10-11 14:13:38 +02001144 __memcpy(saved_sp, kcb->jprobes_stack, MIN_STACK_SIZE(saved_sp));
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -08001145 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 return 1;
1147 }
1148 return 0;
1149}
Masami Hiramatsu93266382014-04-17 17:18:14 +09001150NOKPROBE_SYMBOL(longjmp_break_handler);
Rusty Lynchba8af122005-06-27 15:17:10 -07001151
Masami Hiramatsube8f2742014-04-17 17:16:58 +09001152bool arch_within_kprobe_blacklist(unsigned long addr)
1153{
1154 return (addr >= (unsigned long)__kprobes_text_start &&
1155 addr < (unsigned long)__kprobes_text_end) ||
1156 (addr >= (unsigned long)__entry_text_start &&
1157 addr < (unsigned long)__entry_text_end);
1158}
1159
Rusty Lynch67729262005-07-05 18:54:50 -07001160int __init arch_init_kprobes(void)
Rusty Lynchba8af122005-06-27 15:17:10 -07001161{
Masami Hiramatsua7b01332013-07-18 20:47:50 +09001162 return 0;
Rusty Lynchba8af122005-06-27 15:17:10 -07001163}
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001164
Masami Hiramatsu7ec8a972014-04-17 17:17:47 +09001165int arch_trampoline_kprobe(struct kprobe *p)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001166{
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001167 return 0;
1168}