xref: /openbmc/linux/arch/x86/kernel/kprobes/core.c (revision c7a806d9)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Kernel Probes (KProbes)
4  *
5  * Copyright (C) IBM Corporation, 2002, 2004
6  *
7  * 2002-Oct	Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
8  *		Probes initial implementation ( includes contributions from
9  *		Rusty Russell).
10  * 2004-July	Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
11  *		interface to access function arguments.
12  * 2004-Oct	Jim Keniston <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
13  *		<prasanna@in.ibm.com> adapted for x86_64 from i386.
14  * 2005-Mar	Roland McGrath <roland@redhat.com>
15  *		Fixed to handle %rip-relative addressing mode correctly.
16  * 2005-May	Hien Nguyen <hien@us.ibm.com>, Jim Keniston
17  *		<jkenisto@us.ibm.com> and Prasanna S Panchamukhi
18  *		<prasanna@in.ibm.com> added function-return probes.
19  * 2005-May	Rusty Lynch <rusty.lynch@intel.com>
20  *		Added function return probes functionality
21  * 2006-Feb	Masami Hiramatsu <hiramatu@sdl.hitachi.co.jp> added
22  *		kprobe-booster and kretprobe-booster for i386.
23  * 2007-Dec	Masami Hiramatsu <mhiramat@redhat.com> added kprobe-booster
24  *		and kretprobe-booster for x86-64
25  * 2007-Dec	Masami Hiramatsu <mhiramat@redhat.com>, Arjan van de Ven
26  *		<arjan@infradead.org> and Jim Keniston <jkenisto@us.ibm.com>
27  *		unified x86 kprobes code.
28  */
29 #include <linux/kprobes.h>
30 #include <linux/ptrace.h>
31 #include <linux/string.h>
32 #include <linux/slab.h>
33 #include <linux/hardirq.h>
34 #include <linux/preempt.h>
35 #include <linux/sched/debug.h>
36 #include <linux/perf_event.h>
37 #include <linux/extable.h>
38 #include <linux/kdebug.h>
39 #include <linux/kallsyms.h>
40 #include <linux/ftrace.h>
41 #include <linux/kasan.h>
42 #include <linux/moduleloader.h>
43 #include <linux/objtool.h>
44 #include <linux/vmalloc.h>
45 #include <linux/pgtable.h>
46 #include <linux/set_memory.h>
47 
48 #include <asm/text-patching.h>
49 #include <asm/cacheflush.h>
50 #include <asm/desc.h>
51 #include <linux/uaccess.h>
52 #include <asm/alternative.h>
53 #include <asm/insn.h>
54 #include <asm/debugreg.h>
55 #include <asm/ibt.h>
56 
57 #include "common.h"
58 
59 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
60 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
61 
62 #define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\
63 	(((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) |   \
64 	  (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) |   \
65 	  (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) |   \
66 	  (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf))    \
67 	 << (row % 32))
68 	/*
69 	 * Undefined/reserved opcodes, conditional jump, Opcode Extension
70 	 * Groups, and some special opcodes can not boost.
71 	 * This is non-const and volatile to keep gcc from statically
72 	 * optimizing it out, as variable_test_bit makes gcc think only
73 	 * *(unsigned long*) is used.
74 	 */
75 static volatile u32 twobyte_is_boostable[256 / 32] = {
76 	/*      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f          */
77 	/*      ----------------------------------------------          */
78 	W(0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0) | /* 00 */
79 	W(0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1) , /* 10 */
80 	W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 20 */
81 	W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */
82 	W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
83 	W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */
84 	W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1) | /* 60 */
85 	W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */
86 	W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 80 */
87 	W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
88 	W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* a0 */
89 	W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) , /* b0 */
90 	W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */
91 	W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) , /* d0 */
92 	W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* e0 */
93 	W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0)   /* f0 */
94 	/*      -----------------------------------------------         */
95 	/*      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f          */
96 };
97 #undef W
98 
99 struct kretprobe_blackpoint kretprobe_blacklist[] = {
100 	{"__switch_to", }, /* This function switches only current task, but
101 			      doesn't switch kernel stack.*/
102 	{NULL, NULL}	/* Terminator */
103 };
104 
105 const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist);
106 
107 static nokprobe_inline void
108 __synthesize_relative_insn(void *dest, void *from, void *to, u8 op)
109 {
110 	struct __arch_relative_insn {
111 		u8 op;
112 		s32 raddr;
113 	} __packed *insn;
114 
115 	insn = (struct __arch_relative_insn *)dest;
116 	insn->raddr = (s32)((long)(to) - ((long)(from) + 5));
117 	insn->op = op;
118 }
119 
120 /* Insert a jump instruction at address 'from', which jumps to address 'to'.*/
121 void synthesize_reljump(void *dest, void *from, void *to)
122 {
123 	__synthesize_relative_insn(dest, from, to, JMP32_INSN_OPCODE);
124 }
125 NOKPROBE_SYMBOL(synthesize_reljump);
126 
127 /* Insert a call instruction at address 'from', which calls address 'to'.*/
128 void synthesize_relcall(void *dest, void *from, void *to)
129 {
130 	__synthesize_relative_insn(dest, from, to, CALL_INSN_OPCODE);
131 }
132 NOKPROBE_SYMBOL(synthesize_relcall);
133 
134 /*
135  * Returns non-zero if INSN is boostable.
136  * RIP relative instructions are adjusted at copying time in 64 bits mode
137  */
138 int can_boost(struct insn *insn, void *addr)
139 {
140 	kprobe_opcode_t opcode;
141 	insn_byte_t prefix;
142 	int i;
143 
144 	if (search_exception_tables((unsigned long)addr))
145 		return 0;	/* Page fault may occur on this address. */
146 
147 	/* 2nd-byte opcode */
148 	if (insn->opcode.nbytes == 2)
149 		return test_bit(insn->opcode.bytes[1],
150 				(unsigned long *)twobyte_is_boostable);
151 
152 	if (insn->opcode.nbytes != 1)
153 		return 0;
154 
155 	for_each_insn_prefix(insn, i, prefix) {
156 		insn_attr_t attr;
157 
158 		attr = inat_get_opcode_attribute(prefix);
159 		/* Can't boost Address-size override prefix and CS override prefix */
160 		if (prefix == 0x2e || inat_is_address_size_prefix(attr))
161 			return 0;
162 	}
163 
164 	opcode = insn->opcode.bytes[0];
165 
166 	switch (opcode) {
167 	case 0x62:		/* bound */
168 	case 0x70 ... 0x7f:	/* Conditional jumps */
169 	case 0x9a:		/* Call far */
170 	case 0xc0 ... 0xc1:	/* Grp2 */
171 	case 0xcc ... 0xce:	/* software exceptions */
172 	case 0xd0 ... 0xd3:	/* Grp2 */
173 	case 0xd6:		/* (UD) */
174 	case 0xd8 ... 0xdf:	/* ESC */
175 	case 0xe0 ... 0xe3:	/* LOOP*, JCXZ */
176 	case 0xe8 ... 0xe9:	/* near Call, JMP */
177 	case 0xeb:		/* Short JMP */
178 	case 0xf0 ... 0xf4:	/* LOCK/REP, HLT */
179 	case 0xf6 ... 0xf7:	/* Grp3 */
180 	case 0xfe:		/* Grp4 */
181 		/* ... are not boostable */
182 		return 0;
183 	case 0xff:		/* Grp5 */
184 		/* Only indirect jmp is boostable */
185 		return X86_MODRM_REG(insn->modrm.bytes[0]) == 4;
186 	default:
187 		return 1;
188 	}
189 }
190 
191 static unsigned long
192 __recover_probed_insn(kprobe_opcode_t *buf, unsigned long addr)
193 {
194 	struct kprobe *kp;
195 	bool faddr;
196 
197 	kp = get_kprobe((void *)addr);
198 	faddr = ftrace_location(addr) == addr;
199 	/*
200 	 * Use the current code if it is not modified by Kprobe
201 	 * and it cannot be modified by ftrace.
202 	 */
203 	if (!kp && !faddr)
204 		return addr;
205 
206 	/*
207 	 * Basically, kp->ainsn.insn has an original instruction.
208 	 * However, RIP-relative instruction can not do single-stepping
209 	 * at different place, __copy_instruction() tweaks the displacement of
210 	 * that instruction. In that case, we can't recover the instruction
211 	 * from the kp->ainsn.insn.
212 	 *
213 	 * On the other hand, in case on normal Kprobe, kp->opcode has a copy
214 	 * of the first byte of the probed instruction, which is overwritten
215 	 * by int3. And the instruction at kp->addr is not modified by kprobes
216 	 * except for the first byte, we can recover the original instruction
217 	 * from it and kp->opcode.
218 	 *
219 	 * In case of Kprobes using ftrace, we do not have a copy of
220 	 * the original instruction. In fact, the ftrace location might
221 	 * be modified at anytime and even could be in an inconsistent state.
222 	 * Fortunately, we know that the original code is the ideal 5-byte
223 	 * long NOP.
224 	 */
225 	if (copy_from_kernel_nofault(buf, (void *)addr,
226 		MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
227 		return 0UL;
228 
229 	if (faddr)
230 		memcpy(buf, x86_nops[5], 5);
231 	else
232 		buf[0] = kp->opcode;
233 	return (unsigned long)buf;
234 }
235 
236 /*
237  * Recover the probed instruction at addr for further analysis.
238  * Caller must lock kprobes by kprobe_mutex, or disable preemption
239  * for preventing to release referencing kprobes.
240  * Returns zero if the instruction can not get recovered (or access failed).
241  */
242 unsigned long recover_probed_instruction(kprobe_opcode_t *buf, unsigned long addr)
243 {
244 	unsigned long __addr;
245 
246 	__addr = __recover_optprobed_insn(buf, addr);
247 	if (__addr != addr)
248 		return __addr;
249 
250 	return __recover_probed_insn(buf, addr);
251 }
252 
253 /* Check if paddr is at an instruction boundary */
254 static int can_probe(unsigned long paddr)
255 {
256 	unsigned long addr, __addr, offset = 0;
257 	struct insn insn;
258 	kprobe_opcode_t buf[MAX_INSN_SIZE];
259 
260 	if (!kallsyms_lookup_size_offset(paddr, NULL, &offset))
261 		return 0;
262 
263 	/* Decode instructions */
264 	addr = paddr - offset;
265 	while (addr < paddr) {
266 		int ret;
267 
268 		/*
269 		 * Check if the instruction has been modified by another
270 		 * kprobe, in which case we replace the breakpoint by the
271 		 * original instruction in our buffer.
272 		 * Also, jump optimization will change the breakpoint to
273 		 * relative-jump. Since the relative-jump itself is
274 		 * normally used, we just go through if there is no kprobe.
275 		 */
276 		__addr = recover_probed_instruction(buf, addr);
277 		if (!__addr)
278 			return 0;
279 
280 		ret = insn_decode_kernel(&insn, (void *)__addr);
281 		if (ret < 0)
282 			return 0;
283 
284 		/*
285 		 * Another debugging subsystem might insert this breakpoint.
286 		 * In that case, we can't recover it.
287 		 */
288 		if (insn.opcode.bytes[0] == INT3_INSN_OPCODE)
289 			return 0;
290 		addr += insn.length;
291 	}
292 
293 	return (addr == paddr);
294 }
295 
296 /* If x86 supports IBT (ENDBR) it must be skipped. */
297 kprobe_opcode_t *arch_adjust_kprobe_addr(unsigned long addr, unsigned long offset,
298 					 bool *on_func_entry)
299 {
300 	if (is_endbr(*(u32 *)addr)) {
301 		*on_func_entry = !offset || offset == 4;
302 		if (*on_func_entry)
303 			offset = 4;
304 
305 	} else {
306 		*on_func_entry = !offset;
307 	}
308 
309 	return (kprobe_opcode_t *)(addr + offset);
310 }
311 
312 /*
313  * Copy an instruction with recovering modified instruction by kprobes
314  * and adjust the displacement if the instruction uses the %rip-relative
315  * addressing mode. Note that since @real will be the final place of copied
316  * instruction, displacement must be adjust by @real, not @dest.
317  * This returns the length of copied instruction, or 0 if it has an error.
318  */
319 int __copy_instruction(u8 *dest, u8 *src, u8 *real, struct insn *insn)
320 {
321 	kprobe_opcode_t buf[MAX_INSN_SIZE];
322 	unsigned long recovered_insn = recover_probed_instruction(buf, (unsigned long)src);
323 	int ret;
324 
325 	if (!recovered_insn || !insn)
326 		return 0;
327 
328 	/* This can access kernel text if given address is not recovered */
329 	if (copy_from_kernel_nofault(dest, (void *)recovered_insn,
330 			MAX_INSN_SIZE))
331 		return 0;
332 
333 	ret = insn_decode_kernel(insn, dest);
334 	if (ret < 0)
335 		return 0;
336 
337 	/* We can not probe force emulate prefixed instruction */
338 	if (insn_has_emulate_prefix(insn))
339 		return 0;
340 
341 	/* Another subsystem puts a breakpoint, failed to recover */
342 	if (insn->opcode.bytes[0] == INT3_INSN_OPCODE)
343 		return 0;
344 
345 	/* We should not singlestep on the exception masking instructions */
346 	if (insn_masking_exception(insn))
347 		return 0;
348 
349 #ifdef CONFIG_X86_64
350 	/* Only x86_64 has RIP relative instructions */
351 	if (insn_rip_relative(insn)) {
352 		s64 newdisp;
353 		u8 *disp;
354 		/*
355 		 * The copied instruction uses the %rip-relative addressing
356 		 * mode.  Adjust the displacement for the difference between
357 		 * the original location of this instruction and the location
358 		 * of the copy that will actually be run.  The tricky bit here
359 		 * is making sure that the sign extension happens correctly in
360 		 * this calculation, since we need a signed 32-bit result to
361 		 * be sign-extended to 64 bits when it's added to the %rip
362 		 * value and yield the same 64-bit result that the sign-
363 		 * extension of the original signed 32-bit displacement would
364 		 * have given.
365 		 */
366 		newdisp = (u8 *) src + (s64) insn->displacement.value
367 			  - (u8 *) real;
368 		if ((s64) (s32) newdisp != newdisp) {
369 			pr_err("Kprobes error: new displacement does not fit into s32 (%llx)\n", newdisp);
370 			return 0;
371 		}
372 		disp = (u8 *) dest + insn_offset_displacement(insn);
373 		*(s32 *) disp = (s32) newdisp;
374 	}
375 #endif
376 	return insn->length;
377 }
378 
379 /* Prepare reljump or int3 right after instruction */
380 static int prepare_singlestep(kprobe_opcode_t *buf, struct kprobe *p,
381 			      struct insn *insn)
382 {
383 	int len = insn->length;
384 
385 	if (!IS_ENABLED(CONFIG_PREEMPTION) &&
386 	    !p->post_handler && can_boost(insn, p->addr) &&
387 	    MAX_INSN_SIZE - len >= JMP32_INSN_SIZE) {
388 		/*
389 		 * These instructions can be executed directly if it
390 		 * jumps back to correct address.
391 		 */
392 		synthesize_reljump(buf + len, p->ainsn.insn + len,
393 				   p->addr + insn->length);
394 		len += JMP32_INSN_SIZE;
395 		p->ainsn.boostable = 1;
396 	} else {
397 		/* Otherwise, put an int3 for trapping singlestep */
398 		if (MAX_INSN_SIZE - len < INT3_INSN_SIZE)
399 			return -ENOSPC;
400 
401 		buf[len] = INT3_INSN_OPCODE;
402 		len += INT3_INSN_SIZE;
403 	}
404 
405 	return len;
406 }
407 
408 /* Make page to RO mode when allocate it */
409 void *alloc_insn_page(void)
410 {
411 	void *page;
412 
413 	page = module_alloc(PAGE_SIZE);
414 	if (!page)
415 		return NULL;
416 
417 	/*
418 	 * TODO: Once additional kernel code protection mechanisms are set, ensure
419 	 * that the page was not maliciously altered and it is still zeroed.
420 	 */
421 	set_memory_rox((unsigned long)page, 1);
422 
423 	return page;
424 }
425 
426 /* Kprobe x86 instruction emulation - only regs->ip or IF flag modifiers */
427 
428 static void kprobe_emulate_ifmodifiers(struct kprobe *p, struct pt_regs *regs)
429 {
430 	switch (p->ainsn.opcode) {
431 	case 0xfa:	/* cli */
432 		regs->flags &= ~(X86_EFLAGS_IF);
433 		break;
434 	case 0xfb:	/* sti */
435 		regs->flags |= X86_EFLAGS_IF;
436 		break;
437 	case 0x9c:	/* pushf */
438 		int3_emulate_push(regs, regs->flags);
439 		break;
440 	case 0x9d:	/* popf */
441 		regs->flags = int3_emulate_pop(regs);
442 		break;
443 	}
444 	regs->ip = regs->ip - INT3_INSN_SIZE + p->ainsn.size;
445 }
446 NOKPROBE_SYMBOL(kprobe_emulate_ifmodifiers);
447 
448 static void kprobe_emulate_ret(struct kprobe *p, struct pt_regs *regs)
449 {
450 	int3_emulate_ret(regs);
451 }
452 NOKPROBE_SYMBOL(kprobe_emulate_ret);
453 
454 static void kprobe_emulate_call(struct kprobe *p, struct pt_regs *regs)
455 {
456 	unsigned long func = regs->ip - INT3_INSN_SIZE + p->ainsn.size;
457 
458 	func += p->ainsn.rel32;
459 	int3_emulate_call(regs, func);
460 }
461 NOKPROBE_SYMBOL(kprobe_emulate_call);
462 
463 static nokprobe_inline
464 void __kprobe_emulate_jmp(struct kprobe *p, struct pt_regs *regs, bool cond)
465 {
466 	unsigned long ip = regs->ip - INT3_INSN_SIZE + p->ainsn.size;
467 
468 	if (cond)
469 		ip += p->ainsn.rel32;
470 	int3_emulate_jmp(regs, ip);
471 }
472 
473 static void kprobe_emulate_jmp(struct kprobe *p, struct pt_regs *regs)
474 {
475 	__kprobe_emulate_jmp(p, regs, true);
476 }
477 NOKPROBE_SYMBOL(kprobe_emulate_jmp);
478 
479 static const unsigned long jcc_mask[6] = {
480 	[0] = X86_EFLAGS_OF,
481 	[1] = X86_EFLAGS_CF,
482 	[2] = X86_EFLAGS_ZF,
483 	[3] = X86_EFLAGS_CF | X86_EFLAGS_ZF,
484 	[4] = X86_EFLAGS_SF,
485 	[5] = X86_EFLAGS_PF,
486 };
487 
488 static void kprobe_emulate_jcc(struct kprobe *p, struct pt_regs *regs)
489 {
490 	bool invert = p->ainsn.jcc.type & 1;
491 	bool match;
492 
493 	if (p->ainsn.jcc.type < 0xc) {
494 		match = regs->flags & jcc_mask[p->ainsn.jcc.type >> 1];
495 	} else {
496 		match = ((regs->flags & X86_EFLAGS_SF) >> X86_EFLAGS_SF_BIT) ^
497 			((regs->flags & X86_EFLAGS_OF) >> X86_EFLAGS_OF_BIT);
498 		if (p->ainsn.jcc.type >= 0xe)
499 			match = match || (regs->flags & X86_EFLAGS_ZF);
500 	}
501 	__kprobe_emulate_jmp(p, regs, (match && !invert) || (!match && invert));
502 }
503 NOKPROBE_SYMBOL(kprobe_emulate_jcc);
504 
505 static void kprobe_emulate_loop(struct kprobe *p, struct pt_regs *regs)
506 {
507 	bool match;
508 
509 	if (p->ainsn.loop.type != 3) {	/* LOOP* */
510 		if (p->ainsn.loop.asize == 32)
511 			match = ((*(u32 *)&regs->cx)--) != 0;
512 #ifdef CONFIG_X86_64
513 		else if (p->ainsn.loop.asize == 64)
514 			match = ((*(u64 *)&regs->cx)--) != 0;
515 #endif
516 		else
517 			match = ((*(u16 *)&regs->cx)--) != 0;
518 	} else {			/* JCXZ */
519 		if (p->ainsn.loop.asize == 32)
520 			match = *(u32 *)(&regs->cx) == 0;
521 #ifdef CONFIG_X86_64
522 		else if (p->ainsn.loop.asize == 64)
523 			match = *(u64 *)(&regs->cx) == 0;
524 #endif
525 		else
526 			match = *(u16 *)(&regs->cx) == 0;
527 	}
528 
529 	if (p->ainsn.loop.type == 0)	/* LOOPNE */
530 		match = match && !(regs->flags & X86_EFLAGS_ZF);
531 	else if (p->ainsn.loop.type == 1)	/* LOOPE */
532 		match = match && (regs->flags & X86_EFLAGS_ZF);
533 
534 	__kprobe_emulate_jmp(p, regs, match);
535 }
536 NOKPROBE_SYMBOL(kprobe_emulate_loop);
537 
538 static const int addrmode_regoffs[] = {
539 	offsetof(struct pt_regs, ax),
540 	offsetof(struct pt_regs, cx),
541 	offsetof(struct pt_regs, dx),
542 	offsetof(struct pt_regs, bx),
543 	offsetof(struct pt_regs, sp),
544 	offsetof(struct pt_regs, bp),
545 	offsetof(struct pt_regs, si),
546 	offsetof(struct pt_regs, di),
547 #ifdef CONFIG_X86_64
548 	offsetof(struct pt_regs, r8),
549 	offsetof(struct pt_regs, r9),
550 	offsetof(struct pt_regs, r10),
551 	offsetof(struct pt_regs, r11),
552 	offsetof(struct pt_regs, r12),
553 	offsetof(struct pt_regs, r13),
554 	offsetof(struct pt_regs, r14),
555 	offsetof(struct pt_regs, r15),
556 #endif
557 };
558 
559 static void kprobe_emulate_call_indirect(struct kprobe *p, struct pt_regs *regs)
560 {
561 	unsigned long offs = addrmode_regoffs[p->ainsn.indirect.reg];
562 
563 	int3_emulate_call(regs, regs_get_register(regs, offs));
564 }
565 NOKPROBE_SYMBOL(kprobe_emulate_call_indirect);
566 
567 static void kprobe_emulate_jmp_indirect(struct kprobe *p, struct pt_regs *regs)
568 {
569 	unsigned long offs = addrmode_regoffs[p->ainsn.indirect.reg];
570 
571 	int3_emulate_jmp(regs, regs_get_register(regs, offs));
572 }
573 NOKPROBE_SYMBOL(kprobe_emulate_jmp_indirect);
574 
575 static int prepare_emulation(struct kprobe *p, struct insn *insn)
576 {
577 	insn_byte_t opcode = insn->opcode.bytes[0];
578 
579 	switch (opcode) {
580 	case 0xfa:		/* cli */
581 	case 0xfb:		/* sti */
582 	case 0x9c:		/* pushfl */
583 	case 0x9d:		/* popf/popfd */
584 		/*
585 		 * IF modifiers must be emulated since it will enable interrupt while
586 		 * int3 single stepping.
587 		 */
588 		p->ainsn.emulate_op = kprobe_emulate_ifmodifiers;
589 		p->ainsn.opcode = opcode;
590 		break;
591 	case 0xc2:	/* ret/lret */
592 	case 0xc3:
593 	case 0xca:
594 	case 0xcb:
595 		p->ainsn.emulate_op = kprobe_emulate_ret;
596 		break;
597 	case 0x9a:	/* far call absolute -- segment is not supported */
598 	case 0xea:	/* far jmp absolute -- segment is not supported */
599 	case 0xcc:	/* int3 */
600 	case 0xcf:	/* iret -- in-kernel IRET is not supported */
601 		return -EOPNOTSUPP;
602 		break;
603 	case 0xe8:	/* near call relative */
604 		p->ainsn.emulate_op = kprobe_emulate_call;
605 		if (insn->immediate.nbytes == 2)
606 			p->ainsn.rel32 = *(s16 *)&insn->immediate.value;
607 		else
608 			p->ainsn.rel32 = *(s32 *)&insn->immediate.value;
609 		break;
610 	case 0xeb:	/* short jump relative */
611 	case 0xe9:	/* near jump relative */
612 		p->ainsn.emulate_op = kprobe_emulate_jmp;
613 		if (insn->immediate.nbytes == 1)
614 			p->ainsn.rel32 = *(s8 *)&insn->immediate.value;
615 		else if (insn->immediate.nbytes == 2)
616 			p->ainsn.rel32 = *(s16 *)&insn->immediate.value;
617 		else
618 			p->ainsn.rel32 = *(s32 *)&insn->immediate.value;
619 		break;
620 	case 0x70 ... 0x7f:
621 		/* 1 byte conditional jump */
622 		p->ainsn.emulate_op = kprobe_emulate_jcc;
623 		p->ainsn.jcc.type = opcode & 0xf;
624 		p->ainsn.rel32 = *(char *)insn->immediate.bytes;
625 		break;
626 	case 0x0f:
627 		opcode = insn->opcode.bytes[1];
628 		if ((opcode & 0xf0) == 0x80) {
629 			/* 2 bytes Conditional Jump */
630 			p->ainsn.emulate_op = kprobe_emulate_jcc;
631 			p->ainsn.jcc.type = opcode & 0xf;
632 			if (insn->immediate.nbytes == 2)
633 				p->ainsn.rel32 = *(s16 *)&insn->immediate.value;
634 			else
635 				p->ainsn.rel32 = *(s32 *)&insn->immediate.value;
636 		} else if (opcode == 0x01 &&
637 			   X86_MODRM_REG(insn->modrm.bytes[0]) == 0 &&
638 			   X86_MODRM_MOD(insn->modrm.bytes[0]) == 3) {
639 			/* VM extensions - not supported */
640 			return -EOPNOTSUPP;
641 		}
642 		break;
643 	case 0xe0:	/* Loop NZ */
644 	case 0xe1:	/* Loop */
645 	case 0xe2:	/* Loop */
646 	case 0xe3:	/* J*CXZ */
647 		p->ainsn.emulate_op = kprobe_emulate_loop;
648 		p->ainsn.loop.type = opcode & 0x3;
649 		p->ainsn.loop.asize = insn->addr_bytes * 8;
650 		p->ainsn.rel32 = *(s8 *)&insn->immediate.value;
651 		break;
652 	case 0xff:
653 		/*
654 		 * Since the 0xff is an extended group opcode, the instruction
655 		 * is determined by the MOD/RM byte.
656 		 */
657 		opcode = insn->modrm.bytes[0];
658 		if ((opcode & 0x30) == 0x10) {
659 			if ((opcode & 0x8) == 0x8)
660 				return -EOPNOTSUPP;	/* far call */
661 			/* call absolute, indirect */
662 			p->ainsn.emulate_op = kprobe_emulate_call_indirect;
663 		} else if ((opcode & 0x30) == 0x20) {
664 			if ((opcode & 0x8) == 0x8)
665 				return -EOPNOTSUPP;	/* far jmp */
666 			/* jmp near absolute indirect */
667 			p->ainsn.emulate_op = kprobe_emulate_jmp_indirect;
668 		} else
669 			break;
670 
671 		if (insn->addr_bytes != sizeof(unsigned long))
672 			return -EOPNOTSUPP;	/* Don't support different size */
673 		if (X86_MODRM_MOD(opcode) != 3)
674 			return -EOPNOTSUPP;	/* TODO: support memory addressing */
675 
676 		p->ainsn.indirect.reg = X86_MODRM_RM(opcode);
677 #ifdef CONFIG_X86_64
678 		if (X86_REX_B(insn->rex_prefix.value))
679 			p->ainsn.indirect.reg += 8;
680 #endif
681 		break;
682 	default:
683 		break;
684 	}
685 	p->ainsn.size = insn->length;
686 
687 	return 0;
688 }
689 
690 static int arch_copy_kprobe(struct kprobe *p)
691 {
692 	struct insn insn;
693 	kprobe_opcode_t buf[MAX_INSN_SIZE];
694 	int ret, len;
695 
696 	/* Copy an instruction with recovering if other optprobe modifies it.*/
697 	len = __copy_instruction(buf, p->addr, p->ainsn.insn, &insn);
698 	if (!len)
699 		return -EINVAL;
700 
701 	/* Analyze the opcode and setup emulate functions */
702 	ret = prepare_emulation(p, &insn);
703 	if (ret < 0)
704 		return ret;
705 
706 	/* Add int3 for single-step or booster jmp */
707 	len = prepare_singlestep(buf, p, &insn);
708 	if (len < 0)
709 		return len;
710 
711 	/* Also, displacement change doesn't affect the first byte */
712 	p->opcode = buf[0];
713 
714 	p->ainsn.tp_len = len;
715 	perf_event_text_poke(p->ainsn.insn, NULL, 0, buf, len);
716 
717 	/* OK, write back the instruction(s) into ROX insn buffer */
718 	text_poke(p->ainsn.insn, buf, len);
719 
720 	return 0;
721 }
722 
723 int arch_prepare_kprobe(struct kprobe *p)
724 {
725 	int ret;
726 
727 	if (alternatives_text_reserved(p->addr, p->addr))
728 		return -EINVAL;
729 
730 	if (!can_probe((unsigned long)p->addr))
731 		return -EILSEQ;
732 
733 	memset(&p->ainsn, 0, sizeof(p->ainsn));
734 
735 	/* insn: must be on special executable page on x86. */
736 	p->ainsn.insn = get_insn_slot();
737 	if (!p->ainsn.insn)
738 		return -ENOMEM;
739 
740 	ret = arch_copy_kprobe(p);
741 	if (ret) {
742 		free_insn_slot(p->ainsn.insn, 0);
743 		p->ainsn.insn = NULL;
744 	}
745 
746 	return ret;
747 }
748 
749 void arch_arm_kprobe(struct kprobe *p)
750 {
751 	u8 int3 = INT3_INSN_OPCODE;
752 
753 	text_poke(p->addr, &int3, 1);
754 	text_poke_sync();
755 	perf_event_text_poke(p->addr, &p->opcode, 1, &int3, 1);
756 }
757 
758 void arch_disarm_kprobe(struct kprobe *p)
759 {
760 	u8 int3 = INT3_INSN_OPCODE;
761 
762 	perf_event_text_poke(p->addr, &int3, 1, &p->opcode, 1);
763 	text_poke(p->addr, &p->opcode, 1);
764 	text_poke_sync();
765 }
766 
767 void arch_remove_kprobe(struct kprobe *p)
768 {
769 	if (p->ainsn.insn) {
770 		/* Record the perf event before freeing the slot */
771 		perf_event_text_poke(p->ainsn.insn, p->ainsn.insn,
772 				     p->ainsn.tp_len, NULL, 0);
773 		free_insn_slot(p->ainsn.insn, p->ainsn.boostable);
774 		p->ainsn.insn = NULL;
775 	}
776 }
777 
778 static nokprobe_inline void
779 save_previous_kprobe(struct kprobe_ctlblk *kcb)
780 {
781 	kcb->prev_kprobe.kp = kprobe_running();
782 	kcb->prev_kprobe.status = kcb->kprobe_status;
783 	kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags;
784 	kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags;
785 }
786 
787 static nokprobe_inline void
788 restore_previous_kprobe(struct kprobe_ctlblk *kcb)
789 {
790 	__this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
791 	kcb->kprobe_status = kcb->prev_kprobe.status;
792 	kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags;
793 	kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags;
794 }
795 
796 static nokprobe_inline void
797 set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
798 		   struct kprobe_ctlblk *kcb)
799 {
800 	__this_cpu_write(current_kprobe, p);
801 	kcb->kprobe_saved_flags = kcb->kprobe_old_flags
802 		= (regs->flags & X86_EFLAGS_IF);
803 }
804 
805 static void kprobe_post_process(struct kprobe *cur, struct pt_regs *regs,
806 			       struct kprobe_ctlblk *kcb)
807 {
808 	/* Restore back the original saved kprobes variables and continue. */
809 	if (kcb->kprobe_status == KPROBE_REENTER) {
810 		/* This will restore both kcb and current_kprobe */
811 		restore_previous_kprobe(kcb);
812 	} else {
813 		/*
814 		 * Always update the kcb status because
815 		 * reset_curent_kprobe() doesn't update kcb.
816 		 */
817 		kcb->kprobe_status = KPROBE_HIT_SSDONE;
818 		if (cur->post_handler)
819 			cur->post_handler(cur, regs, 0);
820 		reset_current_kprobe();
821 	}
822 }
823 NOKPROBE_SYMBOL(kprobe_post_process);
824 
825 static void setup_singlestep(struct kprobe *p, struct pt_regs *regs,
826 			     struct kprobe_ctlblk *kcb, int reenter)
827 {
828 	if (setup_detour_execution(p, regs, reenter))
829 		return;
830 
831 #if !defined(CONFIG_PREEMPTION)
832 	if (p->ainsn.boostable) {
833 		/* Boost up -- we can execute copied instructions directly */
834 		if (!reenter)
835 			reset_current_kprobe();
836 		/*
837 		 * Reentering boosted probe doesn't reset current_kprobe,
838 		 * nor set current_kprobe, because it doesn't use single
839 		 * stepping.
840 		 */
841 		regs->ip = (unsigned long)p->ainsn.insn;
842 		return;
843 	}
844 #endif
845 	if (reenter) {
846 		save_previous_kprobe(kcb);
847 		set_current_kprobe(p, regs, kcb);
848 		kcb->kprobe_status = KPROBE_REENTER;
849 	} else
850 		kcb->kprobe_status = KPROBE_HIT_SS;
851 
852 	if (p->ainsn.emulate_op) {
853 		p->ainsn.emulate_op(p, regs);
854 		kprobe_post_process(p, regs, kcb);
855 		return;
856 	}
857 
858 	/* Disable interrupt, and set ip register on trampoline */
859 	regs->flags &= ~X86_EFLAGS_IF;
860 	regs->ip = (unsigned long)p->ainsn.insn;
861 }
862 NOKPROBE_SYMBOL(setup_singlestep);
863 
864 /*
865  * Called after single-stepping.  p->addr is the address of the
866  * instruction whose first byte has been replaced by the "int3"
867  * instruction.  To avoid the SMP problems that can occur when we
868  * temporarily put back the original opcode to single-step, we
869  * single-stepped a copy of the instruction.  The address of this
870  * copy is p->ainsn.insn. We also doesn't use trap, but "int3" again
871  * right after the copied instruction.
872  * Different from the trap single-step, "int3" single-step can not
873  * handle the instruction which changes the ip register, e.g. jmp,
874  * call, conditional jmp, and the instructions which changes the IF
875  * flags because interrupt must be disabled around the single-stepping.
876  * Such instructions are software emulated, but others are single-stepped
877  * using "int3".
878  *
879  * When the 2nd "int3" handled, the regs->ip and regs->flags needs to
880  * be adjusted, so that we can resume execution on correct code.
881  */
882 static void resume_singlestep(struct kprobe *p, struct pt_regs *regs,
883 			      struct kprobe_ctlblk *kcb)
884 {
885 	unsigned long copy_ip = (unsigned long)p->ainsn.insn;
886 	unsigned long orig_ip = (unsigned long)p->addr;
887 
888 	/* Restore saved interrupt flag and ip register */
889 	regs->flags |= kcb->kprobe_saved_flags;
890 	/* Note that regs->ip is executed int3 so must be a step back */
891 	regs->ip += (orig_ip - copy_ip) - INT3_INSN_SIZE;
892 }
893 NOKPROBE_SYMBOL(resume_singlestep);
894 
895 /*
896  * We have reentered the kprobe_handler(), since another probe was hit while
897  * within the handler. We save the original kprobes variables and just single
898  * step on the instruction of the new probe without calling any user handlers.
899  */
900 static int reenter_kprobe(struct kprobe *p, struct pt_regs *regs,
901 			  struct kprobe_ctlblk *kcb)
902 {
903 	switch (kcb->kprobe_status) {
904 	case KPROBE_HIT_SSDONE:
905 	case KPROBE_HIT_ACTIVE:
906 	case KPROBE_HIT_SS:
907 		kprobes_inc_nmissed_count(p);
908 		setup_singlestep(p, regs, kcb, 1);
909 		break;
910 	case KPROBE_REENTER:
911 		/* A probe has been hit in the codepath leading up to, or just
912 		 * after, single-stepping of a probed instruction. This entire
913 		 * codepath should strictly reside in .kprobes.text section.
914 		 * Raise a BUG or we'll continue in an endless reentering loop
915 		 * and eventually a stack overflow.
916 		 */
917 		pr_err("Unrecoverable kprobe detected.\n");
918 		dump_kprobe(p);
919 		BUG();
920 	default:
921 		/* impossible cases */
922 		WARN_ON(1);
923 		return 0;
924 	}
925 
926 	return 1;
927 }
928 NOKPROBE_SYMBOL(reenter_kprobe);
929 
930 static nokprobe_inline int kprobe_is_ss(struct kprobe_ctlblk *kcb)
931 {
932 	return (kcb->kprobe_status == KPROBE_HIT_SS ||
933 		kcb->kprobe_status == KPROBE_REENTER);
934 }
935 
936 /*
937  * Interrupts are disabled on entry as trap3 is an interrupt gate and they
938  * remain disabled throughout this function.
939  */
940 int kprobe_int3_handler(struct pt_regs *regs)
941 {
942 	kprobe_opcode_t *addr;
943 	struct kprobe *p;
944 	struct kprobe_ctlblk *kcb;
945 
946 	if (user_mode(regs))
947 		return 0;
948 
949 	addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
950 	/*
951 	 * We don't want to be preempted for the entire duration of kprobe
952 	 * processing. Since int3 and debug trap disables irqs and we clear
953 	 * IF while singlestepping, it must be no preemptible.
954 	 */
955 
956 	kcb = get_kprobe_ctlblk();
957 	p = get_kprobe(addr);
958 
959 	if (p) {
960 		if (kprobe_running()) {
961 			if (reenter_kprobe(p, regs, kcb))
962 				return 1;
963 		} else {
964 			set_current_kprobe(p, regs, kcb);
965 			kcb->kprobe_status = KPROBE_HIT_ACTIVE;
966 
967 			/*
968 			 * If we have no pre-handler or it returned 0, we
969 			 * continue with normal processing.  If we have a
970 			 * pre-handler and it returned non-zero, that means
971 			 * user handler setup registers to exit to another
972 			 * instruction, we must skip the single stepping.
973 			 */
974 			if (!p->pre_handler || !p->pre_handler(p, regs))
975 				setup_singlestep(p, regs, kcb, 0);
976 			else
977 				reset_current_kprobe();
978 			return 1;
979 		}
980 	} else if (kprobe_is_ss(kcb)) {
981 		p = kprobe_running();
982 		if ((unsigned long)p->ainsn.insn < regs->ip &&
983 		    (unsigned long)p->ainsn.insn + MAX_INSN_SIZE > regs->ip) {
984 			/* Most provably this is the second int3 for singlestep */
985 			resume_singlestep(p, regs, kcb);
986 			kprobe_post_process(p, regs, kcb);
987 			return 1;
988 		}
989 	}
990 
991 	if (*addr != INT3_INSN_OPCODE) {
992 		/*
993 		 * The breakpoint instruction was removed right
994 		 * after we hit it.  Another cpu has removed
995 		 * either a probepoint or a debugger breakpoint
996 		 * at this address.  In either case, no further
997 		 * handling of this interrupt is appropriate.
998 		 * Back up over the (now missing) int3 and run
999 		 * the original instruction.
1000 		 */
1001 		regs->ip = (unsigned long)addr;
1002 		return 1;
1003 	} /* else: not a kprobe fault; let the kernel handle it */
1004 
1005 	return 0;
1006 }
1007 NOKPROBE_SYMBOL(kprobe_int3_handler);
1008 
1009 int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
1010 {
1011 	struct kprobe *cur = kprobe_running();
1012 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1013 
1014 	if (unlikely(regs->ip == (unsigned long)cur->ainsn.insn)) {
1015 		/* This must happen on single-stepping */
1016 		WARN_ON(kcb->kprobe_status != KPROBE_HIT_SS &&
1017 			kcb->kprobe_status != KPROBE_REENTER);
1018 		/*
1019 		 * We are here because the instruction being single
1020 		 * stepped caused a page fault. We reset the current
1021 		 * kprobe and the ip points back to the probe address
1022 		 * and allow the page fault handler to continue as a
1023 		 * normal page fault.
1024 		 */
1025 		regs->ip = (unsigned long)cur->addr;
1026 
1027 		/*
1028 		 * If the IF flag was set before the kprobe hit,
1029 		 * don't touch it:
1030 		 */
1031 		regs->flags |= kcb->kprobe_old_flags;
1032 
1033 		if (kcb->kprobe_status == KPROBE_REENTER)
1034 			restore_previous_kprobe(kcb);
1035 		else
1036 			reset_current_kprobe();
1037 	}
1038 
1039 	return 0;
1040 }
1041 NOKPROBE_SYMBOL(kprobe_fault_handler);
1042 
1043 int __init arch_populate_kprobe_blacklist(void)
1044 {
1045 	return kprobe_add_area_blacklist((unsigned long)__entry_text_start,
1046 					 (unsigned long)__entry_text_end);
1047 }
1048 
1049 int __init arch_init_kprobes(void)
1050 {
1051 	return 0;
1052 }
1053 
1054 int arch_trampoline_kprobe(struct kprobe *p)
1055 {
1056 	return 0;
1057 }
1058