blob: dcd6df5943d60ae5e89110a0fea48ba767c12aaa [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>
Masami Hiramatsufd2f0702017-05-25 19:38:17 +090054#include <linux/moduleloader.h>
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070055
Andy Lutomirski35de5b02016-04-26 12:23:24 -070056#include <asm/text-patching.h>
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +010057#include <asm/cacheflush.h>
58#include <asm/desc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <asm/pgtable.h>
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -080060#include <asm/uaccess.h>
Andi Kleen19d36cc2007-07-22 11:12:31 +020061#include <asm/alternative.h>
Masami Hiramatsub46b3d72009-08-13 16:34:28 -040062#include <asm/insn.h>
K.Prasad62edab92009-06-01 23:47:06 +053063#include <asm/debugreg.h>
Nicolai Stange8574df12018-07-29 12:15:33 +020064#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Masami Hiramatsuf6841992012-09-28 17:15:22 +090066#include "common.h"
Masami Hiramatsu3f33ab12012-03-05 22:32:22 +090067
Linus Torvalds1da177e2005-04-16 15:20:36 -070068void jprobe_return_end(void);
69
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -080070DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
71DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
H. Peter Anvin98272ed2009-10-12 14:14:10 -070073#define stack_addr(regs) ((unsigned long *)kernel_stack_pointer(regs))
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +010074
75#define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\
76 (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
77 (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
78 (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
79 (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
80 << (row % 32))
81 /*
82 * Undefined/reserved opcodes, conditional jump, Opcode Extension
83 * Groups, and some special opcodes can not boost.
Linus Torvalds7115e3f2011-10-26 17:03:38 +020084 * This is non-const and volatile to keep gcc from statically
85 * optimizing it out, as variable_test_bit makes gcc think only
Masami Hiramatsuf6841992012-09-28 17:15:22 +090086 * *(unsigned long*) is used.
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +010087 */
Linus Torvalds7115e3f2011-10-26 17:03:38 +020088static volatile u32 twobyte_is_boostable[256 / 32] = {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +010089 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
90 /* ---------------------------------------------- */
91 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 +080092 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 +010093 W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 20 */
94 W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */
95 W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
96 W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */
97 W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1) | /* 60 */
98 W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */
99 W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 80 */
100 W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
101 W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* a0 */
102 W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) , /* b0 */
103 W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */
104 W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) , /* d0 */
105 W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* e0 */
106 W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0) /* f0 */
107 /* ----------------------------------------------- */
108 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
109};
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100110#undef W
111
Masami Hiramatsuf438d912007-10-16 01:27:49 -0700112struct kretprobe_blackpoint kretprobe_blacklist[] = {
113 {"__switch_to", }, /* This function switches only current task, but
114 doesn't switch kernel stack.*/
115 {NULL, NULL} /* Terminator */
116};
Masami Hiramatsu3f33ab12012-03-05 22:32:22 +0900117
Masami Hiramatsuf438d912007-10-16 01:27:49 -0700118const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist);
119
Masami Hiramatsu93266382014-04-17 17:18:14 +0900120static nokprobe_inline void
121__synthesize_relative_insn(void *from, void *to, u8 op)
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100122{
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500123 struct __arch_relative_insn {
124 u8 op;
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100125 s32 raddr;
Masami Hiramatsuf6841992012-09-28 17:15:22 +0900126 } __packed *insn;
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500127
128 insn = (struct __arch_relative_insn *)from;
129 insn->raddr = (s32)((long)(to) - ((long)(from) + 5));
130 insn->op = op;
131}
132
133/* Insert a jump instruction at address 'from', which jumps to address 'to'.*/
Masami Hiramatsu93266382014-04-17 17:18:14 +0900134void synthesize_reljump(void *from, void *to)
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500135{
136 __synthesize_relative_insn(from, to, RELATIVEJUMP_OPCODE);
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100137}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900138NOKPROBE_SYMBOL(synthesize_reljump);
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100139
Masami Hiramatsu3f33ab12012-03-05 22:32:22 +0900140/* Insert a call instruction at address 'from', which calls address 'to'.*/
Masami Hiramatsu93266382014-04-17 17:18:14 +0900141void synthesize_relcall(void *from, void *to)
Masami Hiramatsu3f33ab12012-03-05 22:32:22 +0900142{
143 __synthesize_relative_insn(from, to, RELATIVECALL_OPCODE);
144}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900145NOKPROBE_SYMBOL(synthesize_relcall);
Masami Hiramatsu3f33ab12012-03-05 22:32:22 +0900146
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100147/*
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900148 * Skip the prefixes of the instruction.
Harvey Harrison99309272008-01-30 13:32:14 +0100149 */
Masami Hiramatsu93266382014-04-17 17:18:14 +0900150static kprobe_opcode_t *skip_prefixes(kprobe_opcode_t *insn)
Harvey Harrison99309272008-01-30 13:32:14 +0100151{
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900152 insn_attr_t attr;
153
154 attr = inat_get_opcode_attribute((insn_byte_t)*insn);
155 while (inat_is_legacy_prefix(attr)) {
156 insn++;
157 attr = inat_get_opcode_attribute((insn_byte_t)*insn);
158 }
Harvey Harrison99309272008-01-30 13:32:14 +0100159#ifdef CONFIG_X86_64
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900160 if (inat_is_rex_prefix(attr))
161 insn++;
Harvey Harrison99309272008-01-30 13:32:14 +0100162#endif
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900163 return insn;
Harvey Harrison99309272008-01-30 13:32:14 +0100164}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900165NOKPROBE_SYMBOL(skip_prefixes);
Harvey Harrison99309272008-01-30 13:32:14 +0100166
167/*
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100168 * Returns non-zero if opcode is boostable.
169 * RIP relative instructions are adjusted at copying time in 64 bits mode
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100170 */
Masami Hiramatsu1eae95d2017-03-01 01:23:24 +0900171int can_boost(kprobe_opcode_t *opcodes, void *addr)
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100172{
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100173 kprobe_opcode_t opcode;
174 kprobe_opcode_t *orig_opcodes = opcodes;
175
Masami Hiramatsu1eae95d2017-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 +0100179retry:
180 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
181 return 0;
182 opcode = *(opcodes++);
183
184 /* 2nd-byte opcode */
185 if (opcode == 0x0f) {
186 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
187 return 0;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100188 return test_bit(*opcodes,
189 (unsigned long *)twobyte_is_boostable);
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100190 }
191
192 switch (opcode & 0xf0) {
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100193#ifdef CONFIG_X86_64
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100194 case 0x40:
195 goto retry; /* REX prefix is boostable */
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100196#endif
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100197 case 0x60:
198 if (0x63 < opcode && opcode < 0x67)
199 goto retry; /* prefixes */
200 /* can't boost Address-size override and bound */
201 return (opcode != 0x62 && opcode != 0x67);
202 case 0x70:
203 return 0; /* can't boost conditional jump */
Masami Hiramatsu96d0c4c2017-03-29 13:56:56 +0900204 case 0x90:
205 return opcode != 0x9a; /* can't boost call far */
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100206 case 0xc0:
207 /* can't boost software-interruptions */
208 return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
209 case 0xd0:
210 /* can boost AA* and XLAT */
211 return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
212 case 0xe0:
213 /* can boost in/out and absolute jmps */
214 return ((opcode & 0x04) || opcode == 0xea);
215 case 0xf0:
216 if ((opcode & 0x0c) == 0 && opcode != 0xf1)
217 goto retry; /* lock/rep(ne) prefix */
218 /* clear and set flags are boostable */
219 return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
220 default:
221 /* segment override prefixes are boostable */
222 if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e)
223 goto retry; /* prefixes */
224 /* CS override prefix and call are not boostable */
225 return (opcode != 0x2e && opcode != 0x9a);
226 }
227}
228
Masami Hiramatsu3f33ab12012-03-05 22:32:22 +0900229static unsigned long
230__recover_probed_insn(kprobe_opcode_t *buf, unsigned long addr)
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400231{
232 struct kprobe *kp;
Petr Mladek650b7b22015-02-20 15:07:29 +0100233 unsigned long faddr;
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900234
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400235 kp = get_kprobe((void *)addr);
Petr Mladek650b7b22015-02-20 15:07:29 +0100236 faddr = ftrace_location(addr);
237 /*
Petr Mladek2a6730c2015-02-20 15:07:30 +0100238 * Addresses inside the ftrace location are refused by
239 * arch_check_ftrace_location(). Something went terribly wrong
240 * if such an address is checked here.
241 */
242 if (WARN_ON(faddr && faddr != addr))
243 return 0UL;
244 /*
Petr Mladek650b7b22015-02-20 15:07:29 +0100245 * Use the current code if it is not modified by Kprobe
246 * and it cannot be modified by ftrace.
247 */
248 if (!kp && !faddr)
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900249 return addr;
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400250
251 /*
Petr Mladek650b7b22015-02-20 15:07:29 +0100252 * Basically, kp->ainsn.insn has an original instruction.
253 * However, RIP-relative instruction can not do single-stepping
254 * at different place, __copy_instruction() tweaks the displacement of
255 * that instruction. In that case, we can't recover the instruction
256 * from the kp->ainsn.insn.
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400257 *
Petr Mladek650b7b22015-02-20 15:07:29 +0100258 * On the other hand, in case on normal Kprobe, kp->opcode has a copy
259 * of the first byte of the probed instruction, which is overwritten
260 * by int3. And the instruction at kp->addr is not modified by kprobes
261 * except for the first byte, we can recover the original instruction
262 * from it and kp->opcode.
263 *
264 * In case of Kprobes using ftrace, we do not have a copy of
265 * the original instruction. In fact, the ftrace location might
266 * be modified at anytime and even could be in an inconsistent state.
267 * Fortunately, we know that the original code is the ideal 5-byte
268 * long NOP.
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400269 */
Petr Mladek650b7b22015-02-20 15:07:29 +0100270 memcpy(buf, (void *)addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
271 if (faddr)
272 memcpy(buf, ideal_nops[NOP_ATOMIC5], 5);
273 else
274 buf[0] = kp->opcode;
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900275 return (unsigned long)buf;
276}
277
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900278/*
279 * Recover the probed instruction at addr for further analysis.
280 * Caller must lock kprobes by kprobe_mutex, or disable preemption
281 * for preventing to release referencing kprobes.
Petr Mladek2a6730c2015-02-20 15:07:30 +0100282 * Returns zero if the instruction can not get recovered.
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900283 */
Masami Hiramatsu3f33ab12012-03-05 22:32:22 +0900284unsigned long recover_probed_instruction(kprobe_opcode_t *buf, unsigned long addr)
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900285{
286 unsigned long __addr;
287
288 __addr = __recover_optprobed_insn(buf, addr);
289 if (__addr != addr)
290 return __addr;
291
292 return __recover_probed_insn(buf, addr);
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400293}
294
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400295/* Check if paddr is at an instruction boundary */
Masami Hiramatsu7ec8a972014-04-17 17:17:47 +0900296static int can_probe(unsigned long paddr)
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400297{
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900298 unsigned long addr, __addr, offset = 0;
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400299 struct insn insn;
300 kprobe_opcode_t buf[MAX_INSN_SIZE];
301
Namhyung Kim6abded72010-09-15 10:04:29 +0900302 if (!kallsyms_lookup_size_offset(paddr, NULL, &offset))
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400303 return 0;
304
305 /* Decode instructions */
306 addr = paddr - offset;
307 while (addr < paddr) {
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400308 /*
309 * Check if the instruction has been modified by another
310 * kprobe, in which case we replace the breakpoint by the
311 * original instruction in our buffer.
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900312 * Also, jump optimization will change the breakpoint to
313 * relative-jump. Since the relative-jump itself is
314 * normally used, we just go through if there is no kprobe.
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400315 */
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900316 __addr = recover_probed_instruction(buf, addr);
Petr Mladek2a6730c2015-02-20 15:07:30 +0100317 if (!__addr)
318 return 0;
Dave Hansen6ba48ff2014-11-14 07:39:57 -0800319 kernel_insn_init(&insn, (void *)__addr, MAX_INSN_SIZE);
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400320 insn_get_length(&insn);
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900321
322 /*
323 * Another debugging subsystem might insert this breakpoint.
324 * In that case, we can't recover it.
325 */
326 if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION)
327 return 0;
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400328 addr += insn.length;
329 }
330
331 return (addr == paddr);
332}
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334/*
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100335 * Returns non-zero if opcode modifies the interrupt flag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 */
Masami Hiramatsu7ec8a972014-04-17 17:17:47 +0900337static int is_IF_modifier(kprobe_opcode_t *insn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900339 /* Skip prefixes */
340 insn = skip_prefixes(insn);
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 switch (*insn) {
343 case 0xfa: /* cli */
344 case 0xfb: /* sti */
345 case 0xcf: /* iret/iretd */
346 case 0x9d: /* popf/popfd */
347 return 1;
348 }
Harvey Harrison99309272008-01-30 13:32:14 +0100349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return 0;
351}
352
353/*
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500354 * Copy an instruction and adjust the displacement if the instruction
355 * uses the %rip-relative addressing mode.
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100356 * If it does, Return the address of the 32-bit displacement word.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 * If not, return null.
Harvey Harrison31f80e42008-01-30 13:32:16 +0100358 * Only applicable to 64-bit x86.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 */
Masami Hiramatsu7ec8a972014-04-17 17:17:47 +0900360int __copy_instruction(u8 *dest, u8 *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400362 struct insn insn;
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500363 kprobe_opcode_t buf[MAX_INSN_SIZE];
Eugene Shatokhinc80e5c02015-03-17 19:09:18 +0900364 int length;
Dave Hansen6ba48ff2014-11-14 07:39:57 -0800365 unsigned long recovered_insn =
366 recover_probed_instruction(buf, (unsigned long)src);
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900367
Petr Mladek2a6730c2015-02-20 15:07:30 +0100368 if (!recovered_insn)
369 return 0;
Dave Hansen6ba48ff2014-11-14 07:39:57 -0800370 kernel_insn_init(&insn, (void *)recovered_insn, MAX_INSN_SIZE);
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500371 insn_get_length(&insn);
Eugene Shatokhinc80e5c02015-03-17 19:09:18 +0900372 length = insn.length;
373
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900374 /* Another subsystem puts a breakpoint, failed to recover */
Masami Hiramatsu46484682012-03-05 22:32:16 +0900375 if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION)
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900376 return 0;
Eugene Shatokhinc80e5c02015-03-17 19:09:18 +0900377 memcpy(dest, insn.kaddr, length);
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500378
Masami Hiramatsu01ac2122018-05-09 21:58:15 +0900379 /* We should not singlestep on the exception masking instructions */
380 if (insn_masking_exception(&insn))
381 return 0;
382
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500383#ifdef CONFIG_X86_64
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400384 if (insn_rip_relative(&insn)) {
385 s64 newdisp;
386 u8 *disp;
Eugene Shatokhinc80e5c02015-03-17 19:09:18 +0900387 kernel_insn_init(&insn, dest, length);
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400388 insn_get_displacement(&insn);
389 /*
390 * The copied instruction uses the %rip-relative addressing
391 * mode. Adjust the displacement for the difference between
392 * the original location of this instruction and the location
393 * of the copy that will actually be run. The tricky bit here
394 * is making sure that the sign extension happens correctly in
395 * this calculation, since we need a signed 32-bit result to
396 * be sign-extended to 64 bits when it's added to the %rip
397 * value and yield the same 64-bit result that the sign-
398 * extension of the original signed 32-bit displacement would
399 * have given.
400 */
Masami Hiramatsu46484682012-03-05 22:32:16 +0900401 newdisp = (u8 *) src + (s64) insn.displacement.value - (u8 *) dest;
Masami Hiramatsu81013762013-04-04 19:42:30 +0900402 if ((s64) (s32) newdisp != newdisp) {
403 pr_err("Kprobes error: new displacement does not fit into s32 (%llx)\n", newdisp);
Masami Hiramatsu81013762013-04-04 19:42:30 +0900404 return 0;
405 }
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500406 disp = (u8 *) dest + insn_offset_displacement(&insn);
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400407 *(s32 *) disp = (s32) newdisp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100409#endif
Eugene Shatokhinc80e5c02015-03-17 19:09:18 +0900410 return length;
Harvey Harrison31f80e42008-01-30 13:32:16 +0100411}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Masami Hiramatsufd2f0702017-05-25 19:38:17 +0900413/* Recover page to RW mode before releasing it */
414void free_insn_page(void *page)
415{
416 set_memory_nx((unsigned long)page & PAGE_MASK, 1);
417 set_memory_rw((unsigned long)page & PAGE_MASK, 1);
418 module_memfree(page);
419}
420
Masami Hiramatsu8391d382017-03-29 14:00:25 +0900421/* Prepare reljump right after instruction to boost */
422static void prepare_boost(struct kprobe *p, int length)
423{
424 if (can_boost(p->ainsn.insn, p->addr) &&
425 MAX_INSN_SIZE - length >= RELATIVEJUMP_SIZE) {
426 /*
427 * These instructions can be executed directly if it
428 * jumps back to correct address.
429 */
430 synthesize_reljump(p->ainsn.insn + length, p->addr + length);
431 p->ainsn.boostable = 1;
432 } else {
433 p->ainsn.boostable = -1;
434 }
435}
436
Masami Hiramatsu7ec8a972014-04-17 17:17:47 +0900437static int arch_copy_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Masami Hiramatsu8391d382017-03-29 14:00:25 +0900439 int len;
Masami Hiramatsu003002e2013-06-05 12:12:16 +0900440
Masami Hiramatsud0cab8c2017-03-29 14:02:46 +0900441 set_memory_rw((unsigned long)p->ainsn.insn & PAGE_MASK, 1);
442
Masami Hiramatsu46484682012-03-05 22:32:16 +0900443 /* Copy an instruction with recovering if other optprobe modifies it.*/
Masami Hiramatsu8391d382017-03-29 14:00:25 +0900444 len = __copy_instruction(p->ainsn.insn, p->addr);
445 if (!len)
Masami Hiramatsu003002e2013-06-05 12:12:16 +0900446 return -EINVAL;
Harvey Harrison31f80e42008-01-30 13:32:16 +0100447
Masami Hiramatsu46484682012-03-05 22:32:16 +0900448 /*
449 * __copy_instruction can modify the displacement of the instruction,
450 * but it doesn't affect boostable check.
451 */
Masami Hiramatsu8391d382017-03-29 14:00:25 +0900452 prepare_boost(p, len);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100453
Masami Hiramatsud0cab8c2017-03-29 14:02:46 +0900454 set_memory_ro((unsigned long)p->ainsn.insn & PAGE_MASK, 1);
455
Masami Hiramatsu9a556ab2013-03-14 20:52:43 +0900456 /* Check whether the instruction modifies Interrupt Flag or not */
457 p->ainsn.if_modifier = is_IF_modifier(p->ainsn.insn);
458
Masami Hiramatsu46484682012-03-05 22:32:16 +0900459 /* Also, displacement change doesn't affect the first byte */
460 p->opcode = p->ainsn.insn[0];
Masami Hiramatsu003002e2013-06-05 12:12:16 +0900461
462 return 0;
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700463}
464
Masami Hiramatsu7ec8a972014-04-17 17:17:47 +0900465int arch_prepare_kprobe(struct kprobe *p)
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100466{
Masami Hiramatsu4554dbc2010-02-02 16:49:18 -0500467 if (alternatives_text_reserved(p->addr, p->addr))
468 return -EINVAL;
469
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400470 if (!can_probe((unsigned long)p->addr))
471 return -EILSEQ;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100472 /* insn: must be on special executable page on x86. */
473 p->ainsn.insn = get_insn_slot();
474 if (!p->ainsn.insn)
475 return -ENOMEM;
Masami Hiramatsu003002e2013-06-05 12:12:16 +0900476
477 return arch_copy_kprobe(p);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100478}
479
Masami Hiramatsu7ec8a972014-04-17 17:17:47 +0900480void arch_arm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700481{
Andi Kleen19d36cc2007-07-22 11:12:31 +0200482 text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1);
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700483}
484
Masami Hiramatsu7ec8a972014-04-17 17:17:47 +0900485void arch_disarm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700486{
Andi Kleen19d36cc2007-07-22 11:12:31 +0200487 text_poke(p->addr, &p->opcode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
Masami Hiramatsu7ec8a972014-04-17 17:17:47 +0900490void arch_remove_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Masami Hiramatsu12941562009-01-06 14:41:50 -0800492 if (p->ainsn.insn) {
493 free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1));
494 p->ainsn.insn = NULL;
495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
Masami Hiramatsu93266382014-04-17 17:18:14 +0900498static nokprobe_inline void
499save_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700500{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800501 kcb->prev_kprobe.kp = kprobe_running();
502 kcb->prev_kprobe.status = kcb->kprobe_status;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100503 kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags;
504 kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700505}
506
Masami Hiramatsu93266382014-04-17 17:18:14 +0900507static nokprobe_inline void
508restore_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700509{
Christoph Lameterb76834b2010-12-06 11:16:25 -0600510 __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800511 kcb->kprobe_status = kcb->prev_kprobe.status;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100512 kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags;
513 kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700514}
515
Masami Hiramatsu93266382014-04-17 17:18:14 +0900516static nokprobe_inline void
517set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
518 struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700519{
Christoph Lameterb76834b2010-12-06 11:16:25 -0600520 __this_cpu_write(current_kprobe, p);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100521 kcb->kprobe_saved_flags = kcb->kprobe_old_flags
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100522 = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF));
Masami Hiramatsu9a556ab2013-03-14 20:52:43 +0900523 if (p->ainsn.if_modifier)
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100524 kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700525}
526
Masami Hiramatsu93266382014-04-17 17:18:14 +0900527static nokprobe_inline void clear_btf(void)
Roland McGrath1ecc7982008-01-30 13:30:54 +0100528{
Peter Zijlstraea8e61b2010-03-25 14:51:51 +0100529 if (test_thread_flag(TIF_BLOCKSTEP)) {
530 unsigned long debugctl = get_debugctlmsr();
531
532 debugctl &= ~DEBUGCTLMSR_BTF;
533 update_debugctlmsr(debugctl);
534 }
Roland McGrath1ecc7982008-01-30 13:30:54 +0100535}
536
Masami Hiramatsu93266382014-04-17 17:18:14 +0900537static nokprobe_inline void restore_btf(void)
Roland McGrath1ecc7982008-01-30 13:30:54 +0100538{
Peter Zijlstraea8e61b2010-03-25 14:51:51 +0100539 if (test_thread_flag(TIF_BLOCKSTEP)) {
540 unsigned long debugctl = get_debugctlmsr();
541
542 debugctl |= DEBUGCTLMSR_BTF;
543 update_debugctlmsr(debugctl);
544 }
Roland McGrath1ecc7982008-01-30 13:30:54 +0100545}
546
Masami Hiramatsu93266382014-04-17 17:18:14 +0900547void arch_prepare_kretprobe(struct kretprobe_instance *ri, struct pt_regs *regs)
Rusty Lynch73649da2005-06-23 00:09:23 -0700548{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100549 unsigned long *sara = stack_addr(regs);
Rusty Lynch73649da2005-06-23 00:09:23 -0700550
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700551 ri->ret_addr = (kprobe_opcode_t *) *sara;
Masami Hiramatsuc9c83bb2019-02-24 01:49:52 +0900552 ri->fp = 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)
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100566 if (p->ainsn.boostable == 1 && !p->post_handler) {
567 /* 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 */
Masami Hiramatsua92daabd2018-04-28 21:37:03 +0900620 pr_err("Unrecoverable kprobe detected.\n");
Masami Hiramatsue9afe9e2009-08-27 13:22:58 -0400621 dump_kprobe(p);
622 BUG();
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100623 default:
624 /* impossible cases */
625 WARN_ON(1);
Abhishek Sagarfb8830e2008-01-30 13:33:13 +0100626 return 0;
Masami Hiramatsu59e87cd2008-01-30 13:32:02 +0100627 }
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100628
Masami Hiramatsu59e87cd2008-01-30 13:32:02 +0100629 return 1;
Harvey Harrison40102d42008-01-30 13:32:02 +0100630}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900631NOKPROBE_SYMBOL(reenter_kprobe);
Rusty Lynch73649da2005-06-23 00:09:23 -0700632
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100633/*
634 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200635 * remain disabled throughout this function.
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100636 */
Masami Hiramatsu93266382014-04-17 17:18:14 +0900637int kprobe_int3_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100639 kprobe_opcode_t *addr;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100640 struct kprobe *p;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800641 struct kprobe_ctlblk *kcb;
642
Andy Lutomirskif39b6f02015-03-18 18:33:33 -0700643 if (user_mode(regs))
Andy Lutomirski0cdd1922014-07-11 10:27:01 -0700644 return 0;
645
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100646 addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800647 /*
648 * We don't want to be preempted for the entire
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100649 * duration of kprobe processing. We conditionally
650 * re-enable preemption at the end of this function,
651 * and also in reenter_kprobe() and setup_singlestep().
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800652 */
653 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100655 kcb = get_kprobe_ctlblk();
Harvey Harrisonb9760152008-01-30 13:32:19 +0100656 p = get_kprobe(addr);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100657
Harvey Harrisonb9760152008-01-30 13:32:19 +0100658 if (p) {
Harvey Harrisonb9760152008-01-30 13:32:19 +0100659 if (kprobe_running()) {
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100660 if (reenter_kprobe(p, regs, kcb))
661 return 1;
Harvey Harrisonb9760152008-01-30 13:32:19 +0100662 } else {
663 set_current_kprobe(p, regs, kcb);
664 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 /*
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100667 * If we have no pre-handler or it returned 0, we
668 * continue with normal processing. If we have a
669 * pre-handler and it returned non-zero, it prepped
670 * for calling the break_handler below on re-entry
671 * for jprobe processing, so get out doing nothing
672 * more here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 */
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100674 if (!p->pre_handler || !p->pre_handler(p, regs))
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500675 setup_singlestep(p, regs, kcb, 0);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100676 return 1;
Harvey Harrisonb9760152008-01-30 13:32:19 +0100677 }
Masami Hiramatsu829e9242010-04-27 18:33:49 -0400678 } else if (*addr != BREAKPOINT_INSTRUCTION) {
679 /*
680 * The breakpoint instruction was removed right
681 * after we hit it. Another cpu has removed
682 * either a probepoint or a debugger breakpoint
683 * at this address. In either case, no further
684 * handling of this interrupt is appropriate.
685 * Back up over the (now missing) int3 and run
686 * the original instruction.
687 */
688 regs->ip = (unsigned long)addr;
689 preempt_enable_no_resched();
690 return 1;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100691 } else if (kprobe_running()) {
Christoph Lameterb76834b2010-12-06 11:16:25 -0600692 p = __this_cpu_read(current_kprobe);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100693 if (p->break_handler && p->break_handler(p, regs)) {
Masami Hiramatsue7dbfe32012-09-28 17:15:20 +0900694 if (!skip_singlestep(p, regs, kcb))
695 setup_singlestep(p, regs, kcb, 0);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100696 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100698 } /* else: not a kprobe fault; let the kernel handle it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800700 preempt_enable_no_resched();
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100701 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900703NOKPROBE_SYMBOL(kprobe_int3_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705/*
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100706 * When a retprobed function returns, this code saves registers and
707 * calls trampoline_handler() runs, which calls the kretprobe's handler.
Rusty Lynch73649da2005-06-23 00:09:23 -0700708 */
Josh Poimboeufc1c355c2016-01-21 16:49:28 -0600709asm(
710 ".global kretprobe_trampoline\n"
711 ".type kretprobe_trampoline, @function\n"
712 "kretprobe_trampoline:\n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100713#ifdef CONFIG_X86_64
Josh Poimboeufc1c355c2016-01-21 16:49:28 -0600714 /* We don't bother saving the ss register */
715 " pushq %rsp\n"
716 " pushfq\n"
717 SAVE_REGS_STRING
718 " movq %rsp, %rdi\n"
719 " call trampoline_handler\n"
720 /* Replace saved sp with true return address. */
721 " movq %rax, 152(%rsp)\n"
722 RESTORE_REGS_STRING
723 " popfq\n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100724#else
Josh Poimboeufc1c355c2016-01-21 16:49:28 -0600725 " pushf\n"
726 SAVE_REGS_STRING
727 " movl %esp, %eax\n"
728 " call trampoline_handler\n"
729 /* Move flags to cs */
730 " movl 56(%esp), %edx\n"
731 " movl %edx, 52(%esp)\n"
732 /* Replace saved flags with true return address. */
733 " movl %eax, 56(%esp)\n"
734 RESTORE_REGS_STRING
735 " popf\n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100736#endif
Josh Poimboeufc1c355c2016-01-21 16:49:28 -0600737 " ret\n"
738 ".size kretprobe_trampoline, .-kretprobe_trampoline\n"
739);
Masami Hiramatsu93266382014-04-17 17:18:14 +0900740NOKPROBE_SYMBOL(kretprobe_trampoline);
Josh Poimboeuf87aaff22016-02-28 22:22:40 -0600741STACK_FRAME_NON_STANDARD(kretprobe_trampoline);
Rusty Lynch73649da2005-06-23 00:09:23 -0700742
743/*
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100744 * Called from kretprobe_trampoline
Rusty Lynch73649da2005-06-23 00:09:23 -0700745 */
Masami Hiramatsu93266382014-04-17 17:18:14 +0900746__visible __used void *trampoline_handler(struct pt_regs *regs)
Rusty Lynch73649da2005-06-23 00:09:23 -0700747{
bibo,mao62c27be2006-10-02 02:17:33 -0700748 struct kretprobe_instance *ri = NULL;
bibo,mao99219a32006-10-02 02:17:35 -0700749 struct hlist_head *head, empty_rp;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800750 struct hlist_node *tmp;
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800751 unsigned long flags, orig_ret_address = 0;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100752 unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
KUMANO Syuhei737480a2010-08-15 15:18:04 +0900753 kprobe_opcode_t *correct_ret_addr = NULL;
Masami Hiramatsuc9c83bb2019-02-24 01:49:52 +0900754 void *frame_pointer;
755 bool skipped = false;
Rusty Lynch73649da2005-06-23 00:09:23 -0700756
bibo,mao99219a32006-10-02 02:17:35 -0700757 INIT_HLIST_HEAD(&empty_rp);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700758 kretprobe_hash_lock(current, &head, &flags);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100759 /* fixup registers */
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100760#ifdef CONFIG_X86_64
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100761 regs->cs = __KERNEL_CS;
Masami Hiramatsuc9c83bb2019-02-24 01:49:52 +0900762 /* On x86-64, we use pt_regs->sp for return address holder. */
763 frame_pointer = &regs->sp;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100764#else
765 regs->cs = __KERNEL_CS | get_kernel_rpl();
Masami Hiramatsufee039a2009-03-23 10:14:52 -0400766 regs->gs = 0;
Masami Hiramatsuc9c83bb2019-02-24 01:49:52 +0900767 /* On x86-32, we use pt_regs->flags for return address holder. */
768 frame_pointer = &regs->flags;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100769#endif
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100770 regs->ip = trampoline_address;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100771 regs->orig_ax = ~0UL;
Rusty Lynch73649da2005-06-23 00:09:23 -0700772
Rusty Lynchba8af122005-06-27 15:17:10 -0700773 /*
774 * It is possible to have multiple instances associated with a given
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100775 * task either because multiple functions in the call path have
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200776 * return probes installed on them, and/or more than one
Rusty Lynchba8af122005-06-27 15:17:10 -0700777 * return probe was registered for a target function.
778 *
779 * We can handle this because:
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100780 * - instances are always pushed into the head of the list
Rusty Lynchba8af122005-06-27 15:17:10 -0700781 * - when multiple return probes are registered for the same
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100782 * function, the (chronologically) first instance's ret_addr
783 * will be the real return address, and all the rest will
784 * point to kretprobe_trampoline.
Rusty Lynchba8af122005-06-27 15:17:10 -0700785 */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800786 hlist_for_each_entry_safe(ri, tmp, head, hlist) {
bibo,mao62c27be2006-10-02 02:17:33 -0700787 if (ri->task != current)
Rusty Lynchba8af122005-06-27 15:17:10 -0700788 /* another task is sharing our hash bucket */
bibo,mao62c27be2006-10-02 02:17:33 -0700789 continue;
Masami Hiramatsuc9c83bb2019-02-24 01:49:52 +0900790 /*
791 * Return probes must be pushed on this hash list correct
792 * order (same as return order) so that it can be poped
793 * correctly. However, if we find it is pushed it incorrect
794 * order, this means we find a function which should not be
795 * probed, because the wrong order entry is pushed on the
796 * path of processing other kretprobe itself.
797 */
798 if (ri->fp != frame_pointer) {
799 if (!skipped)
800 pr_warn("kretprobe is stacked incorrectly. Trying to fixup.\n");
801 skipped = true;
802 continue;
803 }
Rusty Lynch73649da2005-06-23 00:09:23 -0700804
Rusty Lynchba8af122005-06-27 15:17:10 -0700805 orig_ret_address = (unsigned long)ri->ret_addr;
Masami Hiramatsuc9c83bb2019-02-24 01:49:52 +0900806 if (skipped)
807 pr_warn("%ps must be blacklisted because of incorrect kretprobe order\n",
808 ri->rp->kp.addr);
Rusty Lynchba8af122005-06-27 15:17:10 -0700809
810 if (orig_ret_address != trampoline_address)
811 /*
812 * This is the real return address. Any other
813 * instances associated with this task are for
814 * other calls deeper on the call stack
815 */
816 break;
Rusty Lynch73649da2005-06-23 00:09:23 -0700817 }
Rusty Lynchba8af122005-06-27 15:17:10 -0700818
Ananth N Mavinakayanahalli0f95b7f2007-05-08 00:28:27 -0700819 kretprobe_assert(ri, orig_ret_address, trampoline_address);
Rusty Lynchba8af122005-06-27 15:17:10 -0700820
KUMANO Syuhei737480a2010-08-15 15:18:04 +0900821 correct_ret_addr = ri->ret_addr;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800822 hlist_for_each_entry_safe(ri, tmp, head, hlist) {
KUMANO Syuhei737480a2010-08-15 15:18:04 +0900823 if (ri->task != current)
824 /* another task is sharing our hash bucket */
825 continue;
Masami Hiramatsuc9c83bb2019-02-24 01:49:52 +0900826 if (ri->fp != frame_pointer)
827 continue;
KUMANO Syuhei737480a2010-08-15 15:18:04 +0900828
829 orig_ret_address = (unsigned long)ri->ret_addr;
830 if (ri->rp && ri->rp->handler) {
Christoph Lameterb76834b2010-12-06 11:16:25 -0600831 __this_cpu_write(current_kprobe, &ri->rp->kp);
KUMANO Syuhei737480a2010-08-15 15:18:04 +0900832 get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
833 ri->ret_addr = correct_ret_addr;
834 ri->rp->handler(ri, regs);
Christoph Lameterb76834b2010-12-06 11:16:25 -0600835 __this_cpu_write(current_kprobe, NULL);
KUMANO Syuhei737480a2010-08-15 15:18:04 +0900836 }
837
838 recycle_rp_inst(ri, &empty_rp);
839
840 if (orig_ret_address != trampoline_address)
841 /*
842 * This is the real return address. Any other
843 * instances associated with this task are for
844 * other calls deeper on the call stack
845 */
846 break;
847 }
848
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700849 kretprobe_hash_unlock(current, &flags);
Rusty Lynchba8af122005-06-27 15:17:10 -0700850
Sasha Levinb67bfe02013-02-27 17:06:00 -0800851 hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
bibo,mao99219a32006-10-02 02:17:35 -0700852 hlist_del(&ri->hlist);
853 kfree(ri);
854 }
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100855 return (void *)orig_ret_address;
Rusty Lynch73649da2005-06-23 00:09:23 -0700856}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900857NOKPROBE_SYMBOL(trampoline_handler);
Rusty Lynch73649da2005-06-23 00:09:23 -0700858
859/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 * Called after single-stepping. p->addr is the address of the
861 * instruction whose first byte has been replaced by the "int 3"
862 * instruction. To avoid the SMP problems that can occur when we
863 * temporarily put back the original opcode to single-step, we
864 * single-stepped a copy of the instruction. The address of this
865 * copy is p->ainsn.insn.
866 *
867 * This function prepares to return from the post-single-step
868 * interrupt. We have to fix up the stack as follows:
869 *
870 * 0) Except in the case of absolute or indirect jump or call instructions,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100871 * the new ip is relative to the copied instruction. We need to make
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 * it relative to the original instruction.
873 *
874 * 1) If the single-stepped instruction was pushfl, then the TF and IF
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100875 * flags are set in the just-pushed flags, and may need to be cleared.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 *
877 * 2) If the single-stepped instruction was a call, the return address
878 * that is atop the stack is the address following the copied instruction.
879 * We need to make it the address following the original instruction.
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100880 *
881 * If this is the first time we've single-stepped the instruction at
882 * this probepoint, and the instruction is boostable, boost it: add a
883 * jump instruction after the copied instruction, that jumps to the next
884 * instruction after the probepoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 */
Masami Hiramatsu93266382014-04-17 17:18:14 +0900886static void resume_execution(struct kprobe *p, struct pt_regs *regs,
887 struct kprobe_ctlblk *kcb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100889 unsigned long *tos = stack_addr(regs);
890 unsigned long copy_ip = (unsigned long)p->ainsn.insn;
891 unsigned long orig_ip = (unsigned long)p->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 kprobe_opcode_t *insn = p->ainsn.insn;
893
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900894 /* Skip prefixes */
895 insn = skip_prefixes(insn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100897 regs->flags &= ~X86_EFLAGS_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 switch (*insn) {
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100899 case 0x9c: /* pushfl */
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100900 *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100901 *tos |= kcb->kprobe_old_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 break;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100903 case 0xc2: /* iret/ret/lret */
904 case 0xc3:
Prasanna S Panchamukhi0b9e2ca2005-05-05 16:15:40 -0700905 case 0xca:
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100906 case 0xcb:
907 case 0xcf:
908 case 0xea: /* jmp absolute -- ip is correct */
909 /* ip is already adjusted, no more changes required */
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100910 p->ainsn.boostable = 1;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100911 goto no_change;
912 case 0xe8: /* call relative - Fix return addr */
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100913 *tos = orig_ip + (*tos - copy_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 break;
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100915#ifdef CONFIG_X86_32
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100916 case 0x9a: /* call absolute -- same as call absolute, indirect */
917 *tos = orig_ip + (*tos - copy_ip);
918 goto no_change;
919#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 case 0xff:
Satoshi Oshimadc49e342006-05-20 15:00:21 -0700921 if ((insn[1] & 0x30) == 0x10) {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100922 /*
923 * call absolute, indirect
924 * Fix return addr; ip is correct.
925 * But this is not boostable
926 */
927 *tos = orig_ip + (*tos - copy_ip);
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100928 goto no_change;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100929 } else if (((insn[1] & 0x31) == 0x20) ||
930 ((insn[1] & 0x31) == 0x21)) {
931 /*
932 * jmp near and far, absolute indirect
933 * ip is correct. And this is boostable
934 */
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100935 p->ainsn.boostable = 1;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100936 goto no_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 default:
939 break;
940 }
941
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100942 regs->ip += orig_ip - copy_ip;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100943
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100944no_change:
Roland McGrath1ecc7982008-01-30 13:30:54 +0100945 restore_btf();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900947NOKPROBE_SYMBOL(resume_execution);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100949/*
950 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200951 * remain disabled throughout this function.
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100952 */
Masami Hiramatsu93266382014-04-17 17:18:14 +0900953int kprobe_debug_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800955 struct kprobe *cur = kprobe_running();
956 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
957
958 if (!cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 return 0;
960
Yakov Lerneracb5b8a2008-03-16 03:21:21 -0500961 resume_execution(cur, regs, kcb);
962 regs->flags |= kcb->kprobe_saved_flags;
Yakov Lerneracb5b8a2008-03-16 03:21:21 -0500963
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800964 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
965 kcb->kprobe_status = KPROBE_HIT_SSDONE;
966 cur->post_handler(cur, regs, 0);
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100969 /* Restore back the original saved kprobes variables and continue. */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800970 if (kcb->kprobe_status == KPROBE_REENTER) {
971 restore_previous_kprobe(kcb);
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700972 goto out;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700973 }
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800974 reset_current_kprobe();
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700975out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 preempt_enable_no_resched();
977
978 /*
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100979 * if somebody else is singlestepping across a probe point, flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 * will have TF set, in which case, continue the remaining processing
981 * of do_debug, as if this is not a probe hit.
982 */
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100983 if (regs->flags & X86_EFLAGS_TF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return 0;
985
986 return 1;
987}
Masami Hiramatsu93266382014-04-17 17:18:14 +0900988NOKPROBE_SYMBOL(kprobe_debug_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Masami Hiramatsu93266382014-04-17 17:18:14 +0900990int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800992 struct kprobe *cur = kprobe_running();
993 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
994
Masami Hiramatsu6381c242014-04-17 17:16:44 +0900995 if (unlikely(regs->ip == (unsigned long)cur->ainsn.insn)) {
996 /* This must happen on single-stepping */
997 WARN_ON(kcb->kprobe_status != KPROBE_HIT_SS &&
998 kcb->kprobe_status != KPROBE_REENTER);
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800999 /*
1000 * We are here because the instruction being single
1001 * stepped caused a page fault. We reset the current
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001002 * kprobe and the ip points back to the probe address
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -08001003 * and allow the page fault handler to continue as a
1004 * normal page fault.
1005 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001006 regs->ip = (unsigned long)cur->addr;
Masami Hiramatsudcfc4722016-06-11 23:06:53 +09001007 /*
1008 * Trap flag (TF) has been set here because this fault
1009 * happened where the single stepping will be done.
1010 * So clear it by resetting the current kprobe:
1011 */
1012 regs->flags &= ~X86_EFLAGS_TF;
1013
1014 /*
1015 * If the TF flag was set before the kprobe hit,
1016 * don't touch it:
1017 */
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001018 regs->flags |= kcb->kprobe_old_flags;
Masami Hiramatsudcfc4722016-06-11 23:06:53 +09001019
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -08001020 if (kcb->kprobe_status == KPROBE_REENTER)
1021 restore_previous_kprobe(kcb);
1022 else
1023 reset_current_kprobe();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 preempt_enable_no_resched();
Masami Hiramatsu6381c242014-04-17 17:16:44 +09001025 } else if (kcb->kprobe_status == KPROBE_HIT_ACTIVE ||
1026 kcb->kprobe_status == KPROBE_HIT_SSDONE) {
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -08001027 /*
1028 * We increment the nmissed count for accounting,
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001029 * we can also use npre/npostfault count for accounting
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -08001030 * these specific fault cases.
1031 */
1032 kprobes_inc_nmissed_count(cur);
1033
1034 /*
1035 * We come here because instructions in the pre/post
1036 * handler caused the page_fault, this could happen
1037 * if handler tries to access user space by
1038 * copy_from_user(), get_user() etc. Let the
1039 * user-specified handler try to fix it first.
1040 */
1041 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
1042 return 1;
1043
1044 /*
1045 * In case the user-specified fault handler returned
1046 * zero, try to fix up.
1047 */
Tony Luck548acf12016-02-17 10:20:12 -08001048 if (fixup_exception(regs, trapnr))
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001049 return 1;
Harvey Harrison6d485832008-01-30 13:31:41 +01001050
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -08001051 /*
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001052 * fixup routine could not handle it,
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -08001053 * Let do_page_fault() fix it.
1054 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
Masami Hiramatsu6381c242014-04-17 17:16:44 +09001056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 return 0;
1058}
Masami Hiramatsu93266382014-04-17 17:18:14 +09001059NOKPROBE_SYMBOL(kprobe_fault_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061/*
1062 * Wrapper routine for handling exceptions.
1063 */
Masami Hiramatsu93266382014-04-17 17:18:14 +09001064int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
1065 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
Jan Engelhardtade1af72008-01-30 13:33:23 +01001067 struct die_args *args = data;
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -08001068 int ret = NOTIFY_DONE;
1069
Andy Lutomirskif39b6f02015-03-18 18:33:33 -07001070 if (args->regs && user_mode(args->regs))
bibo,mao2326c772006-03-26 01:38:21 -08001071 return ret;
1072
Masami Hiramatsu6f6343f2014-04-17 17:17:33 +09001073 if (val == DIE_GPF) {
Quentin Barnesb506a9d2008-01-30 13:32:32 +01001074 /*
1075 * To be potentially processing a kprobe fault and to
1076 * trust the result from kprobe_running(), we have
1077 * be non-preemptible.
1078 */
1079 if (!preemptible() && kprobe_running() &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 kprobe_fault_handler(args->regs, args->trapnr))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -08001081 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -08001083 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084}
Masami Hiramatsu93266382014-04-17 17:18:14 +09001085NOKPROBE_SYMBOL(kprobe_exceptions_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Masami Hiramatsu93266382014-04-17 17:18:14 +09001087int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
1089 struct jprobe *jp = container_of(p, struct jprobe, kp);
1090 unsigned long addr;
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001091 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001093 kcb->jprobe_saved_regs = *regs;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001094 kcb->jprobe_saved_sp = stack_addr(regs);
1095 addr = (unsigned long)(kcb->jprobe_saved_sp);
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 /*
1098 * As Linus pointed out, gcc assumes that the callee
1099 * owns the argument space and could overwrite it, e.g.
1100 * tailcall optimization. So, to be absolutely safe
1101 * we also save and restore enough stack bytes to cover
1102 * the argument area.
Dmitry Vyukov92541392016-10-11 14:13:38 +02001103 * Use __memcpy() to avoid KASAN stack out-of-bounds reports as we copy
1104 * raw stack chunk with redzones:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 */
Dmitry Vyukov92541392016-10-11 14:13:38 +02001106 __memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr, MIN_STACK_SIZE(addr));
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001107 regs->flags &= ~X86_EFLAGS_IF;
Peter Zijlstra58dfe882007-10-11 22:25:25 +02001108 trace_hardirqs_off();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001109 regs->ip = (unsigned long)(jp->entry);
Steven Rostedt (Red Hat)237d28d2015-01-12 12:12:03 -05001110
1111 /*
1112 * jprobes use jprobe_return() which skips the normal return
1113 * path of the function, and this messes up the accounting of the
1114 * function graph tracer to get messed up.
1115 *
1116 * Pause function graph tracing while performing the jprobe function.
1117 */
1118 pause_graph_tracing();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return 1;
1120}
Masami Hiramatsu93266382014-04-17 17:18:14 +09001121NOKPROBE_SYMBOL(setjmp_pre_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Masami Hiramatsu93266382014-04-17 17:18:14 +09001123void jprobe_return(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001125 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1126
Dmitry Vyukov9f7d4162016-10-14 16:07:23 +02001127 /* Unpoison stack redzones in the frames we are going to jump over. */
1128 kasan_unpoison_stack_above_sp_to(kcb->jprobe_saved_sp);
1129
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001130 asm volatile (
1131#ifdef CONFIG_X86_64
1132 " xchg %%rbx,%%rsp \n"
1133#else
1134 " xchgl %%ebx,%%esp \n"
1135#endif
1136 " int3 \n"
1137 " .globl jprobe_return_end\n"
1138 " jprobe_return_end: \n"
1139 " nop \n"::"b"
1140 (kcb->jprobe_saved_sp):"memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}
Masami Hiramatsu93266382014-04-17 17:18:14 +09001142NOKPROBE_SYMBOL(jprobe_return);
1143NOKPROBE_SYMBOL(jprobe_return_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Masami Hiramatsu93266382014-04-17 17:18:14 +09001145int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001147 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001148 u8 *addr = (u8 *) (regs->ip - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 struct jprobe *jp = container_of(p, struct jprobe, kp);
Steven Rostedt (Red Hat)237d28d2015-01-12 12:12:03 -05001150 void *saved_sp = kcb->jprobe_saved_sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001152 if ((addr > (u8 *) jprobe_return) &&
1153 (addr < (u8 *) jprobe_return_end)) {
Steven Rostedt (Red Hat)237d28d2015-01-12 12:12:03 -05001154 if (stack_addr(regs) != saved_sp) {
Masami Hiramatsu29b6cd72007-12-18 18:05:58 +01001155 struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001156 printk(KERN_ERR
1157 "current sp %p does not match saved sp %p\n",
Steven Rostedt (Red Hat)237d28d2015-01-12 12:12:03 -05001158 stack_addr(regs), saved_sp);
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001159 printk(KERN_ERR "Saved registers for jprobe %p\n", jp);
Jan Beulich57da8b92012-05-09 08:47:37 +01001160 show_regs(saved_regs);
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001161 printk(KERN_ERR "Current registers\n");
Jan Beulich57da8b92012-05-09 08:47:37 +01001162 show_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 BUG();
1164 }
Steven Rostedt (Red Hat)237d28d2015-01-12 12:12:03 -05001165 /* It's OK to start function graph tracing again */
1166 unpause_graph_tracing();
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001167 *regs = kcb->jprobe_saved_regs;
Dmitry Vyukov92541392016-10-11 14:13:38 +02001168 __memcpy(saved_sp, kcb->jprobes_stack, MIN_STACK_SIZE(saved_sp));
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -08001169 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return 1;
1171 }
1172 return 0;
1173}
Masami Hiramatsu93266382014-04-17 17:18:14 +09001174NOKPROBE_SYMBOL(longjmp_break_handler);
Rusty Lynchba8af122005-06-27 15:17:10 -07001175
Masami Hiramatsube8f2742014-04-17 17:16:58 +09001176bool arch_within_kprobe_blacklist(unsigned long addr)
1177{
1178 return (addr >= (unsigned long)__kprobes_text_start &&
1179 addr < (unsigned long)__kprobes_text_end) ||
1180 (addr >= (unsigned long)__entry_text_start &&
1181 addr < (unsigned long)__entry_text_end);
1182}
1183
Rusty Lynch67729262005-07-05 18:54:50 -07001184int __init arch_init_kprobes(void)
Rusty Lynchba8af122005-06-27 15:17:10 -07001185{
Masami Hiramatsua7b01332013-07-18 20:47:50 +09001186 return 0;
Rusty Lynchba8af122005-06-27 15:17:10 -07001187}
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001188
Masami Hiramatsu7ec8a972014-04-17 17:17:47 +09001189int arch_trampoline_kprobe(struct kprobe *p)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001190{
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001191 return 0;
1192}