Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Kernel Probes (KProbes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | * |
| 18 | * Copyright (C) IBM Corporation, 2002, 2004 |
| 19 | * |
| 20 | * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel |
| 21 | * Probes initial implementation ( includes contributions from |
| 22 | * Rusty Russell). |
| 23 | * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes |
| 24 | * interface to access function arguments. |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 25 | * 2004-Oct Jim Keniston <jkenisto@us.ibm.com> and Prasanna S Panchamukhi |
| 26 | * <prasanna@in.ibm.com> adapted for x86_64 from i386. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | * 2005-Mar Roland McGrath <roland@redhat.com> |
| 28 | * Fixed to handle %rip-relative addressing mode correctly. |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 29 | * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston |
| 30 | * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi |
| 31 | * <prasanna@in.ibm.com> added function-return probes. |
| 32 | * 2005-May Rusty Lynch <rusty.lynch@intel.com> |
Masami Hiramatsu | 3f33ab1 | 2012-03-05 22:32:22 +0900 | [diff] [blame] | 33 | * Added function return probes functionality |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 34 | * 2006-Feb Masami Hiramatsu <hiramatu@sdl.hitachi.co.jp> added |
Masami Hiramatsu | 3f33ab1 | 2012-03-05 22:32:22 +0900 | [diff] [blame] | 35 | * kprobe-booster and kretprobe-booster for i386. |
Masami Hiramatsu | da07ab0 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 36 | * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com> added kprobe-booster |
Masami Hiramatsu | 3f33ab1 | 2012-03-05 22:32:22 +0900 | [diff] [blame] | 37 | * and kretprobe-booster for x86-64 |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 38 | * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com>, Arjan van de Ven |
Masami Hiramatsu | 3f33ab1 | 2012-03-05 22:32:22 +0900 | [diff] [blame] | 39 | * <arjan@infradead.org> and Jim Keniston <jkenisto@us.ibm.com> |
| 40 | * unified x86 kprobes code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <linux/kprobes.h> |
| 43 | #include <linux/ptrace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <linux/string.h> |
| 45 | #include <linux/slab.h> |
Quentin Barnes | b506a9d | 2008-01-30 13:32:32 +0100 | [diff] [blame] | 46 | #include <linux/hardirq.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include <linux/preempt.h> |
Prasanna S Panchamukhi | c28f896 | 2006-03-26 01:38:23 -0800 | [diff] [blame] | 48 | #include <linux/module.h> |
Christoph Hellwig | 1eeb66a | 2007-05-08 00:27:03 -0700 | [diff] [blame] | 49 | #include <linux/kdebug.h> |
Masami Hiramatsu | b46b3d7 | 2009-08-13 16:34:28 -0400 | [diff] [blame] | 50 | #include <linux/kallsyms.h> |
Masami Hiramatsu | c0f7ac3 | 2010-02-25 08:34:46 -0500 | [diff] [blame] | 51 | #include <linux/ftrace.h> |
Josh Poimboeuf | 87aaff2 | 2016-02-28 22:22:40 -0600 | [diff] [blame] | 52 | #include <linux/frame.h> |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 53 | |
Andy Lutomirski | 35de5b0 | 2016-04-26 12:23:24 -0700 | [diff] [blame^] | 54 | #include <asm/text-patching.h> |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 55 | #include <asm/cacheflush.h> |
| 56 | #include <asm/desc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | #include <asm/pgtable.h> |
Prasanna S Panchamukhi | c28f896 | 2006-03-26 01:38:23 -0800 | [diff] [blame] | 58 | #include <asm/uaccess.h> |
Andi Kleen | 19d36cc | 2007-07-22 11:12:31 +0200 | [diff] [blame] | 59 | #include <asm/alternative.h> |
Masami Hiramatsu | b46b3d7 | 2009-08-13 16:34:28 -0400 | [diff] [blame] | 60 | #include <asm/insn.h> |
K.Prasad | 62edab9 | 2009-06-01 23:47:06 +0530 | [diff] [blame] | 61 | #include <asm/debugreg.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Masami Hiramatsu | f684199 | 2012-09-28 17:15:22 +0900 | [diff] [blame] | 63 | #include "common.h" |
Masami Hiramatsu | 3f33ab1 | 2012-03-05 22:32:22 +0900 | [diff] [blame] | 64 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | void jprobe_return_end(void); |
| 66 | |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 67 | DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL; |
| 68 | DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
H. Peter Anvin | 98272ed | 2009-10-12 14:14:10 -0700 | [diff] [blame] | 70 | #define stack_addr(regs) ((unsigned long *)kernel_stack_pointer(regs)) |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 71 | |
| 72 | #define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\ |
| 73 | (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \ |
| 74 | (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \ |
| 75 | (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \ |
| 76 | (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \ |
| 77 | << (row % 32)) |
| 78 | /* |
| 79 | * Undefined/reserved opcodes, conditional jump, Opcode Extension |
| 80 | * Groups, and some special opcodes can not boost. |
Linus Torvalds | 7115e3f | 2011-10-26 17:03:38 +0200 | [diff] [blame] | 81 | * This is non-const and volatile to keep gcc from statically |
| 82 | * optimizing it out, as variable_test_bit makes gcc think only |
Masami Hiramatsu | f684199 | 2012-09-28 17:15:22 +0900 | [diff] [blame] | 83 | * *(unsigned long*) is used. |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 84 | */ |
Linus Torvalds | 7115e3f | 2011-10-26 17:03:38 +0200 | [diff] [blame] | 85 | static volatile u32 twobyte_is_boostable[256 / 32] = { |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 86 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ |
| 87 | /* ---------------------------------------------- */ |
| 88 | W(0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0) | /* 00 */ |
Wang Nan | b7e3756 | 2015-02-10 09:34:05 +0800 | [diff] [blame] | 89 | W(0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1) , /* 10 */ |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 90 | W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 20 */ |
| 91 | W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */ |
| 92 | W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */ |
| 93 | W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */ |
| 94 | W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1) | /* 60 */ |
| 95 | W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */ |
| 96 | W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 80 */ |
| 97 | W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */ |
| 98 | W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* a0 */ |
| 99 | W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) , /* b0 */ |
| 100 | W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */ |
| 101 | W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) , /* d0 */ |
| 102 | W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* e0 */ |
| 103 | W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0) /* f0 */ |
| 104 | /* ----------------------------------------------- */ |
| 105 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ |
| 106 | }; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 107 | #undef W |
| 108 | |
Masami Hiramatsu | f438d91 | 2007-10-16 01:27:49 -0700 | [diff] [blame] | 109 | struct kretprobe_blackpoint kretprobe_blacklist[] = { |
| 110 | {"__switch_to", }, /* This function switches only current task, but |
| 111 | doesn't switch kernel stack.*/ |
| 112 | {NULL, NULL} /* Terminator */ |
| 113 | }; |
Masami Hiramatsu | 3f33ab1 | 2012-03-05 22:32:22 +0900 | [diff] [blame] | 114 | |
Masami Hiramatsu | f438d91 | 2007-10-16 01:27:49 -0700 | [diff] [blame] | 115 | const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist); |
| 116 | |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 117 | static nokprobe_inline void |
| 118 | __synthesize_relative_insn(void *from, void *to, u8 op) |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 119 | { |
Masami Hiramatsu | c0f7ac3 | 2010-02-25 08:34:46 -0500 | [diff] [blame] | 120 | struct __arch_relative_insn { |
| 121 | u8 op; |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 122 | s32 raddr; |
Masami Hiramatsu | f684199 | 2012-09-28 17:15:22 +0900 | [diff] [blame] | 123 | } __packed *insn; |
Masami Hiramatsu | c0f7ac3 | 2010-02-25 08:34:46 -0500 | [diff] [blame] | 124 | |
| 125 | insn = (struct __arch_relative_insn *)from; |
| 126 | insn->raddr = (s32)((long)(to) - ((long)(from) + 5)); |
| 127 | insn->op = op; |
| 128 | } |
| 129 | |
| 130 | /* Insert a jump instruction at address 'from', which jumps to address 'to'.*/ |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 131 | void synthesize_reljump(void *from, void *to) |
Masami Hiramatsu | c0f7ac3 | 2010-02-25 08:34:46 -0500 | [diff] [blame] | 132 | { |
| 133 | __synthesize_relative_insn(from, to, RELATIVEJUMP_OPCODE); |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 134 | } |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 135 | NOKPROBE_SYMBOL(synthesize_reljump); |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 136 | |
Masami Hiramatsu | 3f33ab1 | 2012-03-05 22:32:22 +0900 | [diff] [blame] | 137 | /* Insert a call instruction at address 'from', which calls address 'to'.*/ |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 138 | void synthesize_relcall(void *from, void *to) |
Masami Hiramatsu | 3f33ab1 | 2012-03-05 22:32:22 +0900 | [diff] [blame] | 139 | { |
| 140 | __synthesize_relative_insn(from, to, RELATIVECALL_OPCODE); |
| 141 | } |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 142 | NOKPROBE_SYMBOL(synthesize_relcall); |
Masami Hiramatsu | 3f33ab1 | 2012-03-05 22:32:22 +0900 | [diff] [blame] | 143 | |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 144 | /* |
Masami Hiramatsu | 567a9fd | 2010-06-29 14:53:50 +0900 | [diff] [blame] | 145 | * Skip the prefixes of the instruction. |
Harvey Harrison | 9930927 | 2008-01-30 13:32:14 +0100 | [diff] [blame] | 146 | */ |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 147 | static kprobe_opcode_t *skip_prefixes(kprobe_opcode_t *insn) |
Harvey Harrison | 9930927 | 2008-01-30 13:32:14 +0100 | [diff] [blame] | 148 | { |
Masami Hiramatsu | 567a9fd | 2010-06-29 14:53:50 +0900 | [diff] [blame] | 149 | insn_attr_t attr; |
| 150 | |
| 151 | attr = inat_get_opcode_attribute((insn_byte_t)*insn); |
| 152 | while (inat_is_legacy_prefix(attr)) { |
| 153 | insn++; |
| 154 | attr = inat_get_opcode_attribute((insn_byte_t)*insn); |
| 155 | } |
Harvey Harrison | 9930927 | 2008-01-30 13:32:14 +0100 | [diff] [blame] | 156 | #ifdef CONFIG_X86_64 |
Masami Hiramatsu | 567a9fd | 2010-06-29 14:53:50 +0900 | [diff] [blame] | 157 | if (inat_is_rex_prefix(attr)) |
| 158 | insn++; |
Harvey Harrison | 9930927 | 2008-01-30 13:32:14 +0100 | [diff] [blame] | 159 | #endif |
Masami Hiramatsu | 567a9fd | 2010-06-29 14:53:50 +0900 | [diff] [blame] | 160 | return insn; |
Harvey Harrison | 9930927 | 2008-01-30 13:32:14 +0100 | [diff] [blame] | 161 | } |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 162 | NOKPROBE_SYMBOL(skip_prefixes); |
Harvey Harrison | 9930927 | 2008-01-30 13:32:14 +0100 | [diff] [blame] | 163 | |
| 164 | /* |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 165 | * Returns non-zero if opcode is boostable. |
| 166 | * RIP relative instructions are adjusted at copying time in 64 bits mode |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 167 | */ |
Masami Hiramatsu | 7ec8a97 | 2014-04-17 17:17:47 +0900 | [diff] [blame] | 168 | int can_boost(kprobe_opcode_t *opcodes) |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 169 | { |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 170 | kprobe_opcode_t opcode; |
| 171 | kprobe_opcode_t *orig_opcodes = opcodes; |
| 172 | |
Jaswinder Singh Rajput | cde5edb | 2009-03-18 17:37:45 +0530 | [diff] [blame] | 173 | if (search_exception_tables((unsigned long)opcodes)) |
Masami Hiramatsu | 3039088 | 2009-03-16 18:57:22 -0400 | [diff] [blame] | 174 | return 0; /* Page fault may occur on this address. */ |
| 175 | |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 176 | retry: |
| 177 | if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1) |
| 178 | return 0; |
| 179 | opcode = *(opcodes++); |
| 180 | |
| 181 | /* 2nd-byte opcode */ |
| 182 | if (opcode == 0x0f) { |
| 183 | if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1) |
| 184 | return 0; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 185 | return test_bit(*opcodes, |
| 186 | (unsigned long *)twobyte_is_boostable); |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | switch (opcode & 0xf0) { |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 190 | #ifdef CONFIG_X86_64 |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 191 | case 0x40: |
| 192 | goto retry; /* REX prefix is boostable */ |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 193 | #endif |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 194 | case 0x60: |
| 195 | if (0x63 < opcode && opcode < 0x67) |
| 196 | goto retry; /* prefixes */ |
| 197 | /* can't boost Address-size override and bound */ |
| 198 | return (opcode != 0x62 && opcode != 0x67); |
| 199 | case 0x70: |
| 200 | return 0; /* can't boost conditional jump */ |
| 201 | case 0xc0: |
| 202 | /* can't boost software-interruptions */ |
| 203 | return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf; |
| 204 | case 0xd0: |
| 205 | /* can boost AA* and XLAT */ |
| 206 | return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7); |
| 207 | case 0xe0: |
| 208 | /* can boost in/out and absolute jmps */ |
| 209 | return ((opcode & 0x04) || opcode == 0xea); |
| 210 | case 0xf0: |
| 211 | if ((opcode & 0x0c) == 0 && opcode != 0xf1) |
| 212 | goto retry; /* lock/rep(ne) prefix */ |
| 213 | /* clear and set flags are boostable */ |
| 214 | return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe)); |
| 215 | default: |
| 216 | /* segment override prefixes are boostable */ |
| 217 | if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e) |
| 218 | goto retry; /* prefixes */ |
| 219 | /* CS override prefix and call are not boostable */ |
| 220 | return (opcode != 0x2e && opcode != 0x9a); |
| 221 | } |
| 222 | } |
| 223 | |
Masami Hiramatsu | 3f33ab1 | 2012-03-05 22:32:22 +0900 | [diff] [blame] | 224 | static unsigned long |
| 225 | __recover_probed_insn(kprobe_opcode_t *buf, unsigned long addr) |
Masami Hiramatsu | b46b3d7 | 2009-08-13 16:34:28 -0400 | [diff] [blame] | 226 | { |
| 227 | struct kprobe *kp; |
Petr Mladek | 650b7b2 | 2015-02-20 15:07:29 +0100 | [diff] [blame] | 228 | unsigned long faddr; |
Masami Hiramatsu | 86b4ce3 | 2012-03-05 22:32:09 +0900 | [diff] [blame] | 229 | |
Masami Hiramatsu | b46b3d7 | 2009-08-13 16:34:28 -0400 | [diff] [blame] | 230 | kp = get_kprobe((void *)addr); |
Petr Mladek | 650b7b2 | 2015-02-20 15:07:29 +0100 | [diff] [blame] | 231 | faddr = ftrace_location(addr); |
| 232 | /* |
Petr Mladek | 2a6730c | 2015-02-20 15:07:30 +0100 | [diff] [blame] | 233 | * Addresses inside the ftrace location are refused by |
| 234 | * arch_check_ftrace_location(). Something went terribly wrong |
| 235 | * if such an address is checked here. |
| 236 | */ |
| 237 | if (WARN_ON(faddr && faddr != addr)) |
| 238 | return 0UL; |
| 239 | /* |
Petr Mladek | 650b7b2 | 2015-02-20 15:07:29 +0100 | [diff] [blame] | 240 | * Use the current code if it is not modified by Kprobe |
| 241 | * and it cannot be modified by ftrace. |
| 242 | */ |
| 243 | if (!kp && !faddr) |
Masami Hiramatsu | 86b4ce3 | 2012-03-05 22:32:09 +0900 | [diff] [blame] | 244 | return addr; |
Masami Hiramatsu | b46b3d7 | 2009-08-13 16:34:28 -0400 | [diff] [blame] | 245 | |
| 246 | /* |
Petr Mladek | 650b7b2 | 2015-02-20 15:07:29 +0100 | [diff] [blame] | 247 | * Basically, kp->ainsn.insn has an original instruction. |
| 248 | * However, RIP-relative instruction can not do single-stepping |
| 249 | * at different place, __copy_instruction() tweaks the displacement of |
| 250 | * that instruction. In that case, we can't recover the instruction |
| 251 | * from the kp->ainsn.insn. |
Masami Hiramatsu | b46b3d7 | 2009-08-13 16:34:28 -0400 | [diff] [blame] | 252 | * |
Petr Mladek | 650b7b2 | 2015-02-20 15:07:29 +0100 | [diff] [blame] | 253 | * On the other hand, in case on normal Kprobe, kp->opcode has a copy |
| 254 | * of the first byte of the probed instruction, which is overwritten |
| 255 | * by int3. And the instruction at kp->addr is not modified by kprobes |
| 256 | * except for the first byte, we can recover the original instruction |
| 257 | * from it and kp->opcode. |
| 258 | * |
| 259 | * In case of Kprobes using ftrace, we do not have a copy of |
| 260 | * the original instruction. In fact, the ftrace location might |
| 261 | * be modified at anytime and even could be in an inconsistent state. |
| 262 | * Fortunately, we know that the original code is the ideal 5-byte |
| 263 | * long NOP. |
Masami Hiramatsu | b46b3d7 | 2009-08-13 16:34:28 -0400 | [diff] [blame] | 264 | */ |
Petr Mladek | 650b7b2 | 2015-02-20 15:07:29 +0100 | [diff] [blame] | 265 | memcpy(buf, (void *)addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t)); |
| 266 | if (faddr) |
| 267 | memcpy(buf, ideal_nops[NOP_ATOMIC5], 5); |
| 268 | else |
| 269 | buf[0] = kp->opcode; |
Masami Hiramatsu | 86b4ce3 | 2012-03-05 22:32:09 +0900 | [diff] [blame] | 270 | return (unsigned long)buf; |
| 271 | } |
| 272 | |
Masami Hiramatsu | 86b4ce3 | 2012-03-05 22:32:09 +0900 | [diff] [blame] | 273 | /* |
| 274 | * Recover the probed instruction at addr for further analysis. |
| 275 | * Caller must lock kprobes by kprobe_mutex, or disable preemption |
| 276 | * for preventing to release referencing kprobes. |
Petr Mladek | 2a6730c | 2015-02-20 15:07:30 +0100 | [diff] [blame] | 277 | * Returns zero if the instruction can not get recovered. |
Masami Hiramatsu | 86b4ce3 | 2012-03-05 22:32:09 +0900 | [diff] [blame] | 278 | */ |
Masami Hiramatsu | 3f33ab1 | 2012-03-05 22:32:22 +0900 | [diff] [blame] | 279 | unsigned long recover_probed_instruction(kprobe_opcode_t *buf, unsigned long addr) |
Masami Hiramatsu | 86b4ce3 | 2012-03-05 22:32:09 +0900 | [diff] [blame] | 280 | { |
| 281 | unsigned long __addr; |
| 282 | |
| 283 | __addr = __recover_optprobed_insn(buf, addr); |
| 284 | if (__addr != addr) |
| 285 | return __addr; |
| 286 | |
| 287 | return __recover_probed_insn(buf, addr); |
Masami Hiramatsu | b46b3d7 | 2009-08-13 16:34:28 -0400 | [diff] [blame] | 288 | } |
| 289 | |
Masami Hiramatsu | b46b3d7 | 2009-08-13 16:34:28 -0400 | [diff] [blame] | 290 | /* Check if paddr is at an instruction boundary */ |
Masami Hiramatsu | 7ec8a97 | 2014-04-17 17:17:47 +0900 | [diff] [blame] | 291 | static int can_probe(unsigned long paddr) |
Masami Hiramatsu | b46b3d7 | 2009-08-13 16:34:28 -0400 | [diff] [blame] | 292 | { |
Masami Hiramatsu | 86b4ce3 | 2012-03-05 22:32:09 +0900 | [diff] [blame] | 293 | unsigned long addr, __addr, offset = 0; |
Masami Hiramatsu | b46b3d7 | 2009-08-13 16:34:28 -0400 | [diff] [blame] | 294 | struct insn insn; |
| 295 | kprobe_opcode_t buf[MAX_INSN_SIZE]; |
| 296 | |
Namhyung Kim | 6abded7 | 2010-09-15 10:04:29 +0900 | [diff] [blame] | 297 | if (!kallsyms_lookup_size_offset(paddr, NULL, &offset)) |
Masami Hiramatsu | b46b3d7 | 2009-08-13 16:34:28 -0400 | [diff] [blame] | 298 | return 0; |
| 299 | |
| 300 | /* Decode instructions */ |
| 301 | addr = paddr - offset; |
| 302 | while (addr < paddr) { |
Masami Hiramatsu | b46b3d7 | 2009-08-13 16:34:28 -0400 | [diff] [blame] | 303 | /* |
| 304 | * Check if the instruction has been modified by another |
| 305 | * kprobe, in which case we replace the breakpoint by the |
| 306 | * original instruction in our buffer. |
Masami Hiramatsu | 86b4ce3 | 2012-03-05 22:32:09 +0900 | [diff] [blame] | 307 | * Also, jump optimization will change the breakpoint to |
| 308 | * relative-jump. Since the relative-jump itself is |
| 309 | * normally used, we just go through if there is no kprobe. |
Masami Hiramatsu | b46b3d7 | 2009-08-13 16:34:28 -0400 | [diff] [blame] | 310 | */ |
Masami Hiramatsu | 86b4ce3 | 2012-03-05 22:32:09 +0900 | [diff] [blame] | 311 | __addr = recover_probed_instruction(buf, addr); |
Petr Mladek | 2a6730c | 2015-02-20 15:07:30 +0100 | [diff] [blame] | 312 | if (!__addr) |
| 313 | return 0; |
Dave Hansen | 6ba48ff | 2014-11-14 07:39:57 -0800 | [diff] [blame] | 314 | kernel_insn_init(&insn, (void *)__addr, MAX_INSN_SIZE); |
Masami Hiramatsu | b46b3d7 | 2009-08-13 16:34:28 -0400 | [diff] [blame] | 315 | insn_get_length(&insn); |
Masami Hiramatsu | 86b4ce3 | 2012-03-05 22:32:09 +0900 | [diff] [blame] | 316 | |
| 317 | /* |
| 318 | * Another debugging subsystem might insert this breakpoint. |
| 319 | * In that case, we can't recover it. |
| 320 | */ |
| 321 | if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION) |
| 322 | return 0; |
Masami Hiramatsu | b46b3d7 | 2009-08-13 16:34:28 -0400 | [diff] [blame] | 323 | addr += insn.length; |
| 324 | } |
| 325 | |
| 326 | return (addr == paddr); |
| 327 | } |
| 328 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | /* |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 330 | * Returns non-zero if opcode modifies the interrupt flag. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | */ |
Masami Hiramatsu | 7ec8a97 | 2014-04-17 17:17:47 +0900 | [diff] [blame] | 332 | static int is_IF_modifier(kprobe_opcode_t *insn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | { |
Masami Hiramatsu | 567a9fd | 2010-06-29 14:53:50 +0900 | [diff] [blame] | 334 | /* Skip prefixes */ |
| 335 | insn = skip_prefixes(insn); |
| 336 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | switch (*insn) { |
| 338 | case 0xfa: /* cli */ |
| 339 | case 0xfb: /* sti */ |
| 340 | case 0xcf: /* iret/iretd */ |
| 341 | case 0x9d: /* popf/popfd */ |
| 342 | return 1; |
| 343 | } |
Harvey Harrison | 9930927 | 2008-01-30 13:32:14 +0100 | [diff] [blame] | 344 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | /* |
Masami Hiramatsu | c0f7ac3 | 2010-02-25 08:34:46 -0500 | [diff] [blame] | 349 | * Copy an instruction and adjust the displacement if the instruction |
| 350 | * uses the %rip-relative addressing mode. |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 351 | * If it does, Return the address of the 32-bit displacement word. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | * If not, return null. |
Harvey Harrison | 31f80e4 | 2008-01-30 13:32:16 +0100 | [diff] [blame] | 353 | * Only applicable to 64-bit x86. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | */ |
Masami Hiramatsu | 7ec8a97 | 2014-04-17 17:17:47 +0900 | [diff] [blame] | 355 | int __copy_instruction(u8 *dest, u8 *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | { |
Masami Hiramatsu | 89ae465 | 2009-08-13 16:34:36 -0400 | [diff] [blame] | 357 | struct insn insn; |
Masami Hiramatsu | c0f7ac3 | 2010-02-25 08:34:46 -0500 | [diff] [blame] | 358 | kprobe_opcode_t buf[MAX_INSN_SIZE]; |
Eugene Shatokhin | c80e5c0 | 2015-03-17 19:09:18 +0900 | [diff] [blame] | 359 | int length; |
Dave Hansen | 6ba48ff | 2014-11-14 07:39:57 -0800 | [diff] [blame] | 360 | unsigned long recovered_insn = |
| 361 | recover_probed_instruction(buf, (unsigned long)src); |
Masami Hiramatsu | 86b4ce3 | 2012-03-05 22:32:09 +0900 | [diff] [blame] | 362 | |
Petr Mladek | 2a6730c | 2015-02-20 15:07:30 +0100 | [diff] [blame] | 363 | if (!recovered_insn) |
| 364 | return 0; |
Dave Hansen | 6ba48ff | 2014-11-14 07:39:57 -0800 | [diff] [blame] | 365 | kernel_insn_init(&insn, (void *)recovered_insn, MAX_INSN_SIZE); |
Masami Hiramatsu | c0f7ac3 | 2010-02-25 08:34:46 -0500 | [diff] [blame] | 366 | insn_get_length(&insn); |
Eugene Shatokhin | c80e5c0 | 2015-03-17 19:09:18 +0900 | [diff] [blame] | 367 | length = insn.length; |
| 368 | |
Masami Hiramatsu | 86b4ce3 | 2012-03-05 22:32:09 +0900 | [diff] [blame] | 369 | /* Another subsystem puts a breakpoint, failed to recover */ |
Masami Hiramatsu | 4648468 | 2012-03-05 22:32:16 +0900 | [diff] [blame] | 370 | if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION) |
Masami Hiramatsu | 86b4ce3 | 2012-03-05 22:32:09 +0900 | [diff] [blame] | 371 | return 0; |
Eugene Shatokhin | c80e5c0 | 2015-03-17 19:09:18 +0900 | [diff] [blame] | 372 | memcpy(dest, insn.kaddr, length); |
Masami Hiramatsu | c0f7ac3 | 2010-02-25 08:34:46 -0500 | [diff] [blame] | 373 | |
| 374 | #ifdef CONFIG_X86_64 |
Masami Hiramatsu | 89ae465 | 2009-08-13 16:34:36 -0400 | [diff] [blame] | 375 | if (insn_rip_relative(&insn)) { |
| 376 | s64 newdisp; |
| 377 | u8 *disp; |
Eugene Shatokhin | c80e5c0 | 2015-03-17 19:09:18 +0900 | [diff] [blame] | 378 | kernel_insn_init(&insn, dest, length); |
Masami Hiramatsu | 89ae465 | 2009-08-13 16:34:36 -0400 | [diff] [blame] | 379 | insn_get_displacement(&insn); |
| 380 | /* |
| 381 | * The copied instruction uses the %rip-relative addressing |
| 382 | * mode. Adjust the displacement for the difference between |
| 383 | * the original location of this instruction and the location |
| 384 | * of the copy that will actually be run. The tricky bit here |
| 385 | * is making sure that the sign extension happens correctly in |
| 386 | * this calculation, since we need a signed 32-bit result to |
| 387 | * be sign-extended to 64 bits when it's added to the %rip |
| 388 | * value and yield the same 64-bit result that the sign- |
| 389 | * extension of the original signed 32-bit displacement would |
| 390 | * have given. |
| 391 | */ |
Masami Hiramatsu | 4648468 | 2012-03-05 22:32:16 +0900 | [diff] [blame] | 392 | newdisp = (u8 *) src + (s64) insn.displacement.value - (u8 *) dest; |
Masami Hiramatsu | 8101376 | 2013-04-04 19:42:30 +0900 | [diff] [blame] | 393 | if ((s64) (s32) newdisp != newdisp) { |
| 394 | pr_err("Kprobes error: new displacement does not fit into s32 (%llx)\n", newdisp); |
| 395 | pr_err("\tSrc: %p, Dest: %p, old disp: %x\n", src, dest, insn.displacement.value); |
| 396 | return 0; |
| 397 | } |
Masami Hiramatsu | c0f7ac3 | 2010-02-25 08:34:46 -0500 | [diff] [blame] | 398 | disp = (u8 *) dest + insn_offset_displacement(&insn); |
Masami Hiramatsu | 89ae465 | 2009-08-13 16:34:36 -0400 | [diff] [blame] | 399 | *(s32 *) disp = (s32) newdisp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | } |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 401 | #endif |
Eugene Shatokhin | c80e5c0 | 2015-03-17 19:09:18 +0900 | [diff] [blame] | 402 | return length; |
Harvey Harrison | 31f80e4 | 2008-01-30 13:32:16 +0100 | [diff] [blame] | 403 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | |
Masami Hiramatsu | 7ec8a97 | 2014-04-17 17:17:47 +0900 | [diff] [blame] | 405 | static int arch_copy_kprobe(struct kprobe *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | { |
Masami Hiramatsu | 003002e | 2013-06-05 12:12:16 +0900 | [diff] [blame] | 407 | int ret; |
| 408 | |
Masami Hiramatsu | 4648468 | 2012-03-05 22:32:16 +0900 | [diff] [blame] | 409 | /* Copy an instruction with recovering if other optprobe modifies it.*/ |
Masami Hiramatsu | 003002e | 2013-06-05 12:12:16 +0900 | [diff] [blame] | 410 | ret = __copy_instruction(p->ainsn.insn, p->addr); |
| 411 | if (!ret) |
| 412 | return -EINVAL; |
Harvey Harrison | 31f80e4 | 2008-01-30 13:32:16 +0100 | [diff] [blame] | 413 | |
Masami Hiramatsu | 4648468 | 2012-03-05 22:32:16 +0900 | [diff] [blame] | 414 | /* |
| 415 | * __copy_instruction can modify the displacement of the instruction, |
| 416 | * but it doesn't affect boostable check. |
| 417 | */ |
| 418 | if (can_boost(p->ainsn.insn)) |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 419 | p->ainsn.boostable = 0; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 420 | else |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 421 | p->ainsn.boostable = -1; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 422 | |
Masami Hiramatsu | 9a556ab | 2013-03-14 20:52:43 +0900 | [diff] [blame] | 423 | /* Check whether the instruction modifies Interrupt Flag or not */ |
| 424 | p->ainsn.if_modifier = is_IF_modifier(p->ainsn.insn); |
| 425 | |
Masami Hiramatsu | 4648468 | 2012-03-05 22:32:16 +0900 | [diff] [blame] | 426 | /* Also, displacement change doesn't affect the first byte */ |
| 427 | p->opcode = p->ainsn.insn[0]; |
Masami Hiramatsu | 003002e | 2013-06-05 12:12:16 +0900 | [diff] [blame] | 428 | |
| 429 | return 0; |
Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Masami Hiramatsu | 7ec8a97 | 2014-04-17 17:17:47 +0900 | [diff] [blame] | 432 | int arch_prepare_kprobe(struct kprobe *p) |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 433 | { |
Masami Hiramatsu | 4554dbc | 2010-02-02 16:49:18 -0500 | [diff] [blame] | 434 | if (alternatives_text_reserved(p->addr, p->addr)) |
| 435 | return -EINVAL; |
| 436 | |
Masami Hiramatsu | b46b3d7 | 2009-08-13 16:34:28 -0400 | [diff] [blame] | 437 | if (!can_probe((unsigned long)p->addr)) |
| 438 | return -EILSEQ; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 439 | /* insn: must be on special executable page on x86. */ |
| 440 | p->ainsn.insn = get_insn_slot(); |
| 441 | if (!p->ainsn.insn) |
| 442 | return -ENOMEM; |
Masami Hiramatsu | 003002e | 2013-06-05 12:12:16 +0900 | [diff] [blame] | 443 | |
| 444 | return arch_copy_kprobe(p); |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 445 | } |
| 446 | |
Masami Hiramatsu | 7ec8a97 | 2014-04-17 17:17:47 +0900 | [diff] [blame] | 447 | void arch_arm_kprobe(struct kprobe *p) |
Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame] | 448 | { |
Andi Kleen | 19d36cc | 2007-07-22 11:12:31 +0200 | [diff] [blame] | 449 | text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1); |
Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Masami Hiramatsu | 7ec8a97 | 2014-04-17 17:17:47 +0900 | [diff] [blame] | 452 | void arch_disarm_kprobe(struct kprobe *p) |
Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame] | 453 | { |
Andi Kleen | 19d36cc | 2007-07-22 11:12:31 +0200 | [diff] [blame] | 454 | text_poke(p->addr, &p->opcode, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Masami Hiramatsu | 7ec8a97 | 2014-04-17 17:17:47 +0900 | [diff] [blame] | 457 | void arch_remove_kprobe(struct kprobe *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | { |
Masami Hiramatsu | 1294156 | 2009-01-06 14:41:50 -0800 | [diff] [blame] | 459 | if (p->ainsn.insn) { |
| 460 | free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1)); |
| 461 | p->ainsn.insn = NULL; |
| 462 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 465 | static nokprobe_inline void |
| 466 | save_previous_kprobe(struct kprobe_ctlblk *kcb) |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 467 | { |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 468 | kcb->prev_kprobe.kp = kprobe_running(); |
| 469 | kcb->prev_kprobe.status = kcb->kprobe_status; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 470 | kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags; |
| 471 | kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags; |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 474 | static nokprobe_inline void |
| 475 | restore_previous_kprobe(struct kprobe_ctlblk *kcb) |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 476 | { |
Christoph Lameter | b76834b | 2010-12-06 11:16:25 -0600 | [diff] [blame] | 477 | __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp); |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 478 | kcb->kprobe_status = kcb->prev_kprobe.status; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 479 | kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags; |
| 480 | kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags; |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 483 | static nokprobe_inline void |
| 484 | set_current_kprobe(struct kprobe *p, struct pt_regs *regs, |
| 485 | struct kprobe_ctlblk *kcb) |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 486 | { |
Christoph Lameter | b76834b | 2010-12-06 11:16:25 -0600 | [diff] [blame] | 487 | __this_cpu_write(current_kprobe, p); |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 488 | kcb->kprobe_saved_flags = kcb->kprobe_old_flags |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 489 | = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF)); |
Masami Hiramatsu | 9a556ab | 2013-03-14 20:52:43 +0900 | [diff] [blame] | 490 | if (p->ainsn.if_modifier) |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 491 | kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF; |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 494 | static nokprobe_inline void clear_btf(void) |
Roland McGrath | 1ecc798 | 2008-01-30 13:30:54 +0100 | [diff] [blame] | 495 | { |
Peter Zijlstra | ea8e61b | 2010-03-25 14:51:51 +0100 | [diff] [blame] | 496 | if (test_thread_flag(TIF_BLOCKSTEP)) { |
| 497 | unsigned long debugctl = get_debugctlmsr(); |
| 498 | |
| 499 | debugctl &= ~DEBUGCTLMSR_BTF; |
| 500 | update_debugctlmsr(debugctl); |
| 501 | } |
Roland McGrath | 1ecc798 | 2008-01-30 13:30:54 +0100 | [diff] [blame] | 502 | } |
| 503 | |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 504 | static nokprobe_inline void restore_btf(void) |
Roland McGrath | 1ecc798 | 2008-01-30 13:30:54 +0100 | [diff] [blame] | 505 | { |
Peter Zijlstra | ea8e61b | 2010-03-25 14:51:51 +0100 | [diff] [blame] | 506 | if (test_thread_flag(TIF_BLOCKSTEP)) { |
| 507 | unsigned long debugctl = get_debugctlmsr(); |
| 508 | |
| 509 | debugctl |= DEBUGCTLMSR_BTF; |
| 510 | update_debugctlmsr(debugctl); |
| 511 | } |
Roland McGrath | 1ecc798 | 2008-01-30 13:30:54 +0100 | [diff] [blame] | 512 | } |
| 513 | |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 514 | void arch_prepare_kretprobe(struct kretprobe_instance *ri, struct pt_regs *regs) |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 515 | { |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 516 | unsigned long *sara = stack_addr(regs); |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 517 | |
Christoph Hellwig | 4c4308c | 2007-05-08 00:34:14 -0700 | [diff] [blame] | 518 | ri->ret_addr = (kprobe_opcode_t *) *sara; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 519 | |
Christoph Hellwig | 4c4308c | 2007-05-08 00:34:14 -0700 | [diff] [blame] | 520 | /* Replace the return addr with trampoline addr */ |
| 521 | *sara = (unsigned long) &kretprobe_trampoline; |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 522 | } |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 523 | NOKPROBE_SYMBOL(arch_prepare_kretprobe); |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 524 | |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 525 | static void setup_singlestep(struct kprobe *p, struct pt_regs *regs, |
| 526 | struct kprobe_ctlblk *kcb, int reenter) |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 527 | { |
Masami Hiramatsu | c0f7ac3 | 2010-02-25 08:34:46 -0500 | [diff] [blame] | 528 | if (setup_detour_execution(p, regs, reenter)) |
| 529 | return; |
| 530 | |
Masami Hiramatsu | 615d0eb | 2010-02-02 16:49:04 -0500 | [diff] [blame] | 531 | #if !defined(CONFIG_PREEMPT) |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 532 | if (p->ainsn.boostable == 1 && !p->post_handler) { |
| 533 | /* Boost up -- we can execute copied instructions directly */ |
Masami Hiramatsu | 0f94eb6 | 2010-02-25 08:34:23 -0500 | [diff] [blame] | 534 | if (!reenter) |
| 535 | reset_current_kprobe(); |
| 536 | /* |
| 537 | * Reentering boosted probe doesn't reset current_kprobe, |
| 538 | * nor set current_kprobe, because it doesn't use single |
| 539 | * stepping. |
| 540 | */ |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 541 | regs->ip = (unsigned long)p->ainsn.insn; |
| 542 | preempt_enable_no_resched(); |
| 543 | return; |
| 544 | } |
| 545 | #endif |
Masami Hiramatsu | 0f94eb6 | 2010-02-25 08:34:23 -0500 | [diff] [blame] | 546 | if (reenter) { |
| 547 | save_previous_kprobe(kcb); |
| 548 | set_current_kprobe(p, regs, kcb); |
| 549 | kcb->kprobe_status = KPROBE_REENTER; |
| 550 | } else |
| 551 | kcb->kprobe_status = KPROBE_HIT_SS; |
| 552 | /* Prepare real single stepping */ |
| 553 | clear_btf(); |
| 554 | regs->flags |= X86_EFLAGS_TF; |
| 555 | regs->flags &= ~X86_EFLAGS_IF; |
| 556 | /* single step inline if the instruction is an int3 */ |
| 557 | if (p->opcode == BREAKPOINT_INSTRUCTION) |
| 558 | regs->ip = (unsigned long)p->addr; |
| 559 | else |
| 560 | regs->ip = (unsigned long)p->ainsn.insn; |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 561 | } |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 562 | NOKPROBE_SYMBOL(setup_singlestep); |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 563 | |
Harvey Harrison | 40102d4 | 2008-01-30 13:32:02 +0100 | [diff] [blame] | 564 | /* |
| 565 | * We have reentered the kprobe_handler(), since another probe was hit while |
| 566 | * within the handler. We save the original kprobes variables and just single |
| 567 | * step on the instruction of the new probe without calling any user handlers. |
| 568 | */ |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 569 | static int reenter_kprobe(struct kprobe *p, struct pt_regs *regs, |
| 570 | struct kprobe_ctlblk *kcb) |
Harvey Harrison | 40102d4 | 2008-01-30 13:32:02 +0100 | [diff] [blame] | 571 | { |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 572 | switch (kcb->kprobe_status) { |
| 573 | case KPROBE_HIT_SSDONE: |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 574 | case KPROBE_HIT_ACTIVE: |
Masami Hiramatsu | 6a5022a | 2014-04-17 17:16:51 +0900 | [diff] [blame] | 575 | case KPROBE_HIT_SS: |
Abhishek Sagar | fb8830e | 2008-01-30 13:33:13 +0100 | [diff] [blame] | 576 | kprobes_inc_nmissed_count(p); |
Masami Hiramatsu | 0f94eb6 | 2010-02-25 08:34:23 -0500 | [diff] [blame] | 577 | setup_singlestep(p, regs, kcb, 1); |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 578 | break; |
Masami Hiramatsu | 6a5022a | 2014-04-17 17:16:51 +0900 | [diff] [blame] | 579 | case KPROBE_REENTER: |
Masami Hiramatsu | e9afe9e | 2009-08-27 13:22:58 -0400 | [diff] [blame] | 580 | /* A probe has been hit in the codepath leading up to, or just |
| 581 | * after, single-stepping of a probed instruction. This entire |
| 582 | * codepath should strictly reside in .kprobes.text section. |
| 583 | * Raise a BUG or we'll continue in an endless reentering loop |
| 584 | * and eventually a stack overflow. |
| 585 | */ |
| 586 | printk(KERN_WARNING "Unrecoverable kprobe detected at %p.\n", |
| 587 | p->addr); |
| 588 | dump_kprobe(p); |
| 589 | BUG(); |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 590 | default: |
| 591 | /* impossible cases */ |
| 592 | WARN_ON(1); |
Abhishek Sagar | fb8830e | 2008-01-30 13:33:13 +0100 | [diff] [blame] | 593 | return 0; |
Masami Hiramatsu | 59e87cd | 2008-01-30 13:32:02 +0100 | [diff] [blame] | 594 | } |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 595 | |
Masami Hiramatsu | 59e87cd | 2008-01-30 13:32:02 +0100 | [diff] [blame] | 596 | return 1; |
Harvey Harrison | 40102d4 | 2008-01-30 13:32:02 +0100 | [diff] [blame] | 597 | } |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 598 | NOKPROBE_SYMBOL(reenter_kprobe); |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 599 | |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 600 | /* |
| 601 | * Interrupts are disabled on entry as trap3 is an interrupt gate and they |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 602 | * remain disabled throughout this function. |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 603 | */ |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 604 | int kprobe_int3_handler(struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | { |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 606 | kprobe_opcode_t *addr; |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 607 | struct kprobe *p; |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 608 | struct kprobe_ctlblk *kcb; |
| 609 | |
Andy Lutomirski | f39b6f0 | 2015-03-18 18:33:33 -0700 | [diff] [blame] | 610 | if (user_mode(regs)) |
Andy Lutomirski | 0cdd192 | 2014-07-11 10:27:01 -0700 | [diff] [blame] | 611 | return 0; |
| 612 | |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 613 | addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t)); |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 614 | /* |
| 615 | * We don't want to be preempted for the entire |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 616 | * duration of kprobe processing. We conditionally |
| 617 | * re-enable preemption at the end of this function, |
| 618 | * and also in reenter_kprobe() and setup_singlestep(). |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 619 | */ |
| 620 | preempt_disable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 622 | kcb = get_kprobe_ctlblk(); |
Harvey Harrison | b976015 | 2008-01-30 13:32:19 +0100 | [diff] [blame] | 623 | p = get_kprobe(addr); |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 624 | |
Harvey Harrison | b976015 | 2008-01-30 13:32:19 +0100 | [diff] [blame] | 625 | if (p) { |
Harvey Harrison | b976015 | 2008-01-30 13:32:19 +0100 | [diff] [blame] | 626 | if (kprobe_running()) { |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 627 | if (reenter_kprobe(p, regs, kcb)) |
| 628 | return 1; |
Harvey Harrison | b976015 | 2008-01-30 13:32:19 +0100 | [diff] [blame] | 629 | } else { |
| 630 | set_current_kprobe(p, regs, kcb); |
| 631 | kcb->kprobe_status = KPROBE_HIT_ACTIVE; |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 632 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | /* |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 634 | * If we have no pre-handler or it returned 0, we |
| 635 | * continue with normal processing. If we have a |
| 636 | * pre-handler and it returned non-zero, it prepped |
| 637 | * for calling the break_handler below on re-entry |
| 638 | * for jprobe processing, so get out doing nothing |
| 639 | * more here. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | */ |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 641 | if (!p->pre_handler || !p->pre_handler(p, regs)) |
Masami Hiramatsu | 0f94eb6 | 2010-02-25 08:34:23 -0500 | [diff] [blame] | 642 | setup_singlestep(p, regs, kcb, 0); |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 643 | return 1; |
Harvey Harrison | b976015 | 2008-01-30 13:32:19 +0100 | [diff] [blame] | 644 | } |
Masami Hiramatsu | 829e924 | 2010-04-27 18:33:49 -0400 | [diff] [blame] | 645 | } else if (*addr != BREAKPOINT_INSTRUCTION) { |
| 646 | /* |
| 647 | * The breakpoint instruction was removed right |
| 648 | * after we hit it. Another cpu has removed |
| 649 | * either a probepoint or a debugger breakpoint |
| 650 | * at this address. In either case, no further |
| 651 | * handling of this interrupt is appropriate. |
| 652 | * Back up over the (now missing) int3 and run |
| 653 | * the original instruction. |
| 654 | */ |
| 655 | regs->ip = (unsigned long)addr; |
| 656 | preempt_enable_no_resched(); |
| 657 | return 1; |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 658 | } else if (kprobe_running()) { |
Christoph Lameter | b76834b | 2010-12-06 11:16:25 -0600 | [diff] [blame] | 659 | p = __this_cpu_read(current_kprobe); |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 660 | if (p->break_handler && p->break_handler(p, regs)) { |
Masami Hiramatsu | e7dbfe3 | 2012-09-28 17:15:20 +0900 | [diff] [blame] | 661 | if (!skip_singlestep(p, regs, kcb)) |
| 662 | setup_singlestep(p, regs, kcb, 0); |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 663 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | } |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 665 | } /* else: not a kprobe fault; let the kernel handle it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 667 | preempt_enable_no_resched(); |
Abhishek Sagar | f315dec | 2008-01-30 13:32:50 +0100 | [diff] [blame] | 668 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | } |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 670 | NOKPROBE_SYMBOL(kprobe_int3_handler); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | |
| 672 | /* |
Masami Hiramatsu | da07ab0 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 673 | * When a retprobed function returns, this code saves registers and |
| 674 | * calls trampoline_handler() runs, which calls the kretprobe's handler. |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 675 | */ |
Josh Poimboeuf | c1c355c | 2016-01-21 16:49:28 -0600 | [diff] [blame] | 676 | asm( |
| 677 | ".global kretprobe_trampoline\n" |
| 678 | ".type kretprobe_trampoline, @function\n" |
| 679 | "kretprobe_trampoline:\n" |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 680 | #ifdef CONFIG_X86_64 |
Josh Poimboeuf | c1c355c | 2016-01-21 16:49:28 -0600 | [diff] [blame] | 681 | /* We don't bother saving the ss register */ |
| 682 | " pushq %rsp\n" |
| 683 | " pushfq\n" |
| 684 | SAVE_REGS_STRING |
| 685 | " movq %rsp, %rdi\n" |
| 686 | " call trampoline_handler\n" |
| 687 | /* Replace saved sp with true return address. */ |
| 688 | " movq %rax, 152(%rsp)\n" |
| 689 | RESTORE_REGS_STRING |
| 690 | " popfq\n" |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 691 | #else |
Josh Poimboeuf | c1c355c | 2016-01-21 16:49:28 -0600 | [diff] [blame] | 692 | " pushf\n" |
| 693 | SAVE_REGS_STRING |
| 694 | " movl %esp, %eax\n" |
| 695 | " call trampoline_handler\n" |
| 696 | /* Move flags to cs */ |
| 697 | " movl 56(%esp), %edx\n" |
| 698 | " movl %edx, 52(%esp)\n" |
| 699 | /* Replace saved flags with true return address. */ |
| 700 | " movl %eax, 56(%esp)\n" |
| 701 | RESTORE_REGS_STRING |
| 702 | " popf\n" |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 703 | #endif |
Josh Poimboeuf | c1c355c | 2016-01-21 16:49:28 -0600 | [diff] [blame] | 704 | " ret\n" |
| 705 | ".size kretprobe_trampoline, .-kretprobe_trampoline\n" |
| 706 | ); |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 707 | NOKPROBE_SYMBOL(kretprobe_trampoline); |
Josh Poimboeuf | 87aaff2 | 2016-02-28 22:22:40 -0600 | [diff] [blame] | 708 | STACK_FRAME_NON_STANDARD(kretprobe_trampoline); |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 709 | |
| 710 | /* |
Masami Hiramatsu | da07ab0 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 711 | * Called from kretprobe_trampoline |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 712 | */ |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 713 | __visible __used void *trampoline_handler(struct pt_regs *regs) |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 714 | { |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 715 | struct kretprobe_instance *ri = NULL; |
bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 716 | struct hlist_head *head, empty_rp; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 717 | struct hlist_node *tmp; |
Ananth N Mavinakayanahalli | 991a51d | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 718 | unsigned long flags, orig_ret_address = 0; |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 719 | unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline; |
KUMANO Syuhei | 737480a | 2010-08-15 15:18:04 +0900 | [diff] [blame] | 720 | kprobe_opcode_t *correct_ret_addr = NULL; |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 721 | |
bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 722 | INIT_HLIST_HEAD(&empty_rp); |
Srinivasa D S | ef53d9c | 2008-07-25 01:46:04 -0700 | [diff] [blame] | 723 | kretprobe_hash_lock(current, &head, &flags); |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 724 | /* fixup registers */ |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 725 | #ifdef CONFIG_X86_64 |
Masami Hiramatsu | da07ab0 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 726 | regs->cs = __KERNEL_CS; |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 727 | #else |
| 728 | regs->cs = __KERNEL_CS | get_kernel_rpl(); |
Masami Hiramatsu | fee039a | 2009-03-23 10:14:52 -0400 | [diff] [blame] | 729 | regs->gs = 0; |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 730 | #endif |
Masami Hiramatsu | da07ab0 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 731 | regs->ip = trampoline_address; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 732 | regs->orig_ax = ~0UL; |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 733 | |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 734 | /* |
| 735 | * It is possible to have multiple instances associated with a given |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 736 | * task either because multiple functions in the call path have |
Frederik Schwarzer | 025dfda | 2008-10-16 19:02:37 +0200 | [diff] [blame] | 737 | * return probes installed on them, and/or more than one |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 738 | * return probe was registered for a target function. |
| 739 | * |
| 740 | * We can handle this because: |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 741 | * - instances are always pushed into the head of the list |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 742 | * - when multiple return probes are registered for the same |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 743 | * function, the (chronologically) first instance's ret_addr |
| 744 | * will be the real return address, and all the rest will |
| 745 | * point to kretprobe_trampoline. |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 746 | */ |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 747 | hlist_for_each_entry_safe(ri, tmp, head, hlist) { |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 748 | if (ri->task != current) |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 749 | /* another task is sharing our hash bucket */ |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 750 | continue; |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 751 | |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 752 | orig_ret_address = (unsigned long)ri->ret_addr; |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 753 | |
| 754 | if (orig_ret_address != trampoline_address) |
| 755 | /* |
| 756 | * This is the real return address. Any other |
| 757 | * instances associated with this task are for |
| 758 | * other calls deeper on the call stack |
| 759 | */ |
| 760 | break; |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 761 | } |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 762 | |
Ananth N Mavinakayanahalli | 0f95b7f | 2007-05-08 00:28:27 -0700 | [diff] [blame] | 763 | kretprobe_assert(ri, orig_ret_address, trampoline_address); |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 764 | |
KUMANO Syuhei | 737480a | 2010-08-15 15:18:04 +0900 | [diff] [blame] | 765 | correct_ret_addr = ri->ret_addr; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 766 | hlist_for_each_entry_safe(ri, tmp, head, hlist) { |
KUMANO Syuhei | 737480a | 2010-08-15 15:18:04 +0900 | [diff] [blame] | 767 | if (ri->task != current) |
| 768 | /* another task is sharing our hash bucket */ |
| 769 | continue; |
| 770 | |
| 771 | orig_ret_address = (unsigned long)ri->ret_addr; |
| 772 | if (ri->rp && ri->rp->handler) { |
Christoph Lameter | b76834b | 2010-12-06 11:16:25 -0600 | [diff] [blame] | 773 | __this_cpu_write(current_kprobe, &ri->rp->kp); |
KUMANO Syuhei | 737480a | 2010-08-15 15:18:04 +0900 | [diff] [blame] | 774 | get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE; |
| 775 | ri->ret_addr = correct_ret_addr; |
| 776 | ri->rp->handler(ri, regs); |
Christoph Lameter | b76834b | 2010-12-06 11:16:25 -0600 | [diff] [blame] | 777 | __this_cpu_write(current_kprobe, NULL); |
KUMANO Syuhei | 737480a | 2010-08-15 15:18:04 +0900 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | recycle_rp_inst(ri, &empty_rp); |
| 781 | |
| 782 | if (orig_ret_address != trampoline_address) |
| 783 | /* |
| 784 | * This is the real return address. Any other |
| 785 | * instances associated with this task are for |
| 786 | * other calls deeper on the call stack |
| 787 | */ |
| 788 | break; |
| 789 | } |
| 790 | |
Srinivasa D S | ef53d9c | 2008-07-25 01:46:04 -0700 | [diff] [blame] | 791 | kretprobe_hash_unlock(current, &flags); |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 792 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 793 | hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) { |
bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 794 | hlist_del(&ri->hlist); |
| 795 | kfree(ri); |
| 796 | } |
Masami Hiramatsu | da07ab0 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 797 | return (void *)orig_ret_address; |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 798 | } |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 799 | NOKPROBE_SYMBOL(trampoline_handler); |
Rusty Lynch | 73649da | 2005-06-23 00:09:23 -0700 | [diff] [blame] | 800 | |
| 801 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | * Called after single-stepping. p->addr is the address of the |
| 803 | * instruction whose first byte has been replaced by the "int 3" |
| 804 | * instruction. To avoid the SMP problems that can occur when we |
| 805 | * temporarily put back the original opcode to single-step, we |
| 806 | * single-stepped a copy of the instruction. The address of this |
| 807 | * copy is p->ainsn.insn. |
| 808 | * |
| 809 | * This function prepares to return from the post-single-step |
| 810 | * interrupt. We have to fix up the stack as follows: |
| 811 | * |
| 812 | * 0) Except in the case of absolute or indirect jump or call instructions, |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 813 | * the new ip is relative to the copied instruction. We need to make |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | * it relative to the original instruction. |
| 815 | * |
| 816 | * 1) If the single-stepped instruction was pushfl, then the TF and IF |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 817 | * flags are set in the just-pushed flags, and may need to be cleared. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | * |
| 819 | * 2) If the single-stepped instruction was a call, the return address |
| 820 | * that is atop the stack is the address following the copied instruction. |
| 821 | * We need to make it the address following the original instruction. |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 822 | * |
| 823 | * If this is the first time we've single-stepped the instruction at |
| 824 | * this probepoint, and the instruction is boostable, boost it: add a |
| 825 | * jump instruction after the copied instruction, that jumps to the next |
| 826 | * instruction after the probepoint. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | */ |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 828 | static void resume_execution(struct kprobe *p, struct pt_regs *regs, |
| 829 | struct kprobe_ctlblk *kcb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | { |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 831 | unsigned long *tos = stack_addr(regs); |
| 832 | unsigned long copy_ip = (unsigned long)p->ainsn.insn; |
| 833 | unsigned long orig_ip = (unsigned long)p->addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | kprobe_opcode_t *insn = p->ainsn.insn; |
| 835 | |
Masami Hiramatsu | 567a9fd | 2010-06-29 14:53:50 +0900 | [diff] [blame] | 836 | /* Skip prefixes */ |
| 837 | insn = skip_prefixes(insn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 839 | regs->flags &= ~X86_EFLAGS_TF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | switch (*insn) { |
Masami Hiramatsu | 0b0122f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 841 | case 0x9c: /* pushfl */ |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 842 | *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF); |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 843 | *tos |= kcb->kprobe_old_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | break; |
Masami Hiramatsu | 0b0122f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 845 | case 0xc2: /* iret/ret/lret */ |
| 846 | case 0xc3: |
Prasanna S Panchamukhi | 0b9e2ca | 2005-05-05 16:15:40 -0700 | [diff] [blame] | 847 | case 0xca: |
Masami Hiramatsu | 0b0122f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 848 | case 0xcb: |
| 849 | case 0xcf: |
| 850 | case 0xea: /* jmp absolute -- ip is correct */ |
| 851 | /* ip is already adjusted, no more changes required */ |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 852 | p->ainsn.boostable = 1; |
Masami Hiramatsu | 0b0122f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 853 | goto no_change; |
| 854 | case 0xe8: /* call relative - Fix return addr */ |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 855 | *tos = orig_ip + (*tos - copy_ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | break; |
Harvey Harrison | e7b5e11 | 2008-01-30 13:31:43 +0100 | [diff] [blame] | 857 | #ifdef CONFIG_X86_32 |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 858 | case 0x9a: /* call absolute -- same as call absolute, indirect */ |
| 859 | *tos = orig_ip + (*tos - copy_ip); |
| 860 | goto no_change; |
| 861 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | case 0xff: |
Satoshi Oshima | dc49e34 | 2006-05-20 15:00:21 -0700 | [diff] [blame] | 863 | if ((insn[1] & 0x30) == 0x10) { |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 864 | /* |
| 865 | * call absolute, indirect |
| 866 | * Fix return addr; ip is correct. |
| 867 | * But this is not boostable |
| 868 | */ |
| 869 | *tos = orig_ip + (*tos - copy_ip); |
Masami Hiramatsu | 0b0122f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 870 | goto no_change; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 871 | } else if (((insn[1] & 0x31) == 0x20) || |
| 872 | ((insn[1] & 0x31) == 0x21)) { |
| 873 | /* |
| 874 | * jmp near and far, absolute indirect |
| 875 | * ip is correct. And this is boostable |
| 876 | */ |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 877 | p->ainsn.boostable = 1; |
Masami Hiramatsu | 0b0122f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 878 | goto no_change; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | default: |
| 881 | break; |
| 882 | } |
| 883 | |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 884 | if (p->ainsn.boostable == 0) { |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 885 | if ((regs->ip > copy_ip) && |
| 886 | (regs->ip - copy_ip) + 5 < MAX_INSN_SIZE) { |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 887 | /* |
| 888 | * These instructions can be executed directly if it |
| 889 | * jumps back to correct address. |
| 890 | */ |
Masami Hiramatsu | c0f7ac3 | 2010-02-25 08:34:46 -0500 | [diff] [blame] | 891 | synthesize_reljump((void *)regs->ip, |
| 892 | (void *)orig_ip + (regs->ip - copy_ip)); |
Masami Hiramatsu | aa47014 | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 893 | p->ainsn.boostable = 1; |
| 894 | } else { |
| 895 | p->ainsn.boostable = -1; |
| 896 | } |
| 897 | } |
| 898 | |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 899 | regs->ip += orig_ip - copy_ip; |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 900 | |
Masami Hiramatsu | 0b0122f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 901 | no_change: |
Roland McGrath | 1ecc798 | 2008-01-30 13:30:54 +0100 | [diff] [blame] | 902 | restore_btf(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | } |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 904 | NOKPROBE_SYMBOL(resume_execution); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 906 | /* |
| 907 | * Interrupts are disabled on entry as trap1 is an interrupt gate and they |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 908 | * remain disabled throughout this function. |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 909 | */ |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 910 | int kprobe_debug_handler(struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | { |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 912 | struct kprobe *cur = kprobe_running(); |
| 913 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
| 914 | |
| 915 | if (!cur) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | return 0; |
| 917 | |
Yakov Lerner | acb5b8a | 2008-03-16 03:21:21 -0500 | [diff] [blame] | 918 | resume_execution(cur, regs, kcb); |
| 919 | regs->flags |= kcb->kprobe_saved_flags; |
Yakov Lerner | acb5b8a | 2008-03-16 03:21:21 -0500 | [diff] [blame] | 920 | |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 921 | if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) { |
| 922 | kcb->kprobe_status = KPROBE_HIT_SSDONE; |
| 923 | cur->post_handler(cur, regs, 0); |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 924 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 926 | /* Restore back the original saved kprobes variables and continue. */ |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 927 | if (kcb->kprobe_status == KPROBE_REENTER) { |
| 928 | restore_previous_kprobe(kcb); |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 929 | goto out; |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 930 | } |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 931 | reset_current_kprobe(); |
Prasanna S Panchamukhi | aa3d7e3 | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 932 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | preempt_enable_no_resched(); |
| 934 | |
| 935 | /* |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 936 | * if somebody else is singlestepping across a probe point, flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | * will have TF set, in which case, continue the remaining processing |
| 938 | * of do_debug, as if this is not a probe hit. |
| 939 | */ |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 940 | if (regs->flags & X86_EFLAGS_TF) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | return 0; |
| 942 | |
| 943 | return 1; |
| 944 | } |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 945 | NOKPROBE_SYMBOL(kprobe_debug_handler); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 947 | int kprobe_fault_handler(struct pt_regs *regs, int trapnr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | { |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 949 | struct kprobe *cur = kprobe_running(); |
| 950 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
| 951 | |
Masami Hiramatsu | 6381c24 | 2014-04-17 17:16:44 +0900 | [diff] [blame] | 952 | if (unlikely(regs->ip == (unsigned long)cur->ainsn.insn)) { |
| 953 | /* This must happen on single-stepping */ |
| 954 | WARN_ON(kcb->kprobe_status != KPROBE_HIT_SS && |
| 955 | kcb->kprobe_status != KPROBE_REENTER); |
Prasanna S Panchamukhi | c28f896 | 2006-03-26 01:38:23 -0800 | [diff] [blame] | 956 | /* |
| 957 | * We are here because the instruction being single |
| 958 | * stepped caused a page fault. We reset the current |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 959 | * kprobe and the ip points back to the probe address |
Prasanna S Panchamukhi | c28f896 | 2006-03-26 01:38:23 -0800 | [diff] [blame] | 960 | * and allow the page fault handler to continue as a |
| 961 | * normal page fault. |
| 962 | */ |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 963 | regs->ip = (unsigned long)cur->addr; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 964 | regs->flags |= kcb->kprobe_old_flags; |
Prasanna S Panchamukhi | c28f896 | 2006-03-26 01:38:23 -0800 | [diff] [blame] | 965 | if (kcb->kprobe_status == KPROBE_REENTER) |
| 966 | restore_previous_kprobe(kcb); |
| 967 | else |
| 968 | reset_current_kprobe(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | preempt_enable_no_resched(); |
Masami Hiramatsu | 6381c24 | 2014-04-17 17:16:44 +0900 | [diff] [blame] | 970 | } else if (kcb->kprobe_status == KPROBE_HIT_ACTIVE || |
| 971 | kcb->kprobe_status == KPROBE_HIT_SSDONE) { |
Prasanna S Panchamukhi | c28f896 | 2006-03-26 01:38:23 -0800 | [diff] [blame] | 972 | /* |
| 973 | * We increment the nmissed count for accounting, |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 974 | * we can also use npre/npostfault count for accounting |
Prasanna S Panchamukhi | c28f896 | 2006-03-26 01:38:23 -0800 | [diff] [blame] | 975 | * these specific fault cases. |
| 976 | */ |
| 977 | kprobes_inc_nmissed_count(cur); |
| 978 | |
| 979 | /* |
| 980 | * We come here because instructions in the pre/post |
| 981 | * handler caused the page_fault, this could happen |
| 982 | * if handler tries to access user space by |
| 983 | * copy_from_user(), get_user() etc. Let the |
| 984 | * user-specified handler try to fix it first. |
| 985 | */ |
| 986 | if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr)) |
| 987 | return 1; |
| 988 | |
| 989 | /* |
| 990 | * In case the user-specified fault handler returned |
| 991 | * zero, try to fix up. |
| 992 | */ |
Tony Luck | 548acf1 | 2016-02-17 10:20:12 -0800 | [diff] [blame] | 993 | if (fixup_exception(regs, trapnr)) |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 994 | return 1; |
Harvey Harrison | 6d48583 | 2008-01-30 13:31:41 +0100 | [diff] [blame] | 995 | |
Prasanna S Panchamukhi | c28f896 | 2006-03-26 01:38:23 -0800 | [diff] [blame] | 996 | /* |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 997 | * fixup routine could not handle it, |
Prasanna S Panchamukhi | c28f896 | 2006-03-26 01:38:23 -0800 | [diff] [blame] | 998 | * Let do_page_fault() fix it. |
| 999 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | } |
Masami Hiramatsu | 6381c24 | 2014-04-17 17:16:44 +0900 | [diff] [blame] | 1001 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | return 0; |
| 1003 | } |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 1004 | NOKPROBE_SYMBOL(kprobe_fault_handler); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | |
| 1006 | /* |
| 1007 | * Wrapper routine for handling exceptions. |
| 1008 | */ |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 1009 | int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val, |
| 1010 | void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | { |
Jan Engelhardt | ade1af7 | 2008-01-30 13:33:23 +0100 | [diff] [blame] | 1012 | struct die_args *args = data; |
Ananth N Mavinakayanahalli | 66ff2d0 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 1013 | int ret = NOTIFY_DONE; |
| 1014 | |
Andy Lutomirski | f39b6f0 | 2015-03-18 18:33:33 -0700 | [diff] [blame] | 1015 | if (args->regs && user_mode(args->regs)) |
bibo,mao | 2326c77 | 2006-03-26 01:38:21 -0800 | [diff] [blame] | 1016 | return ret; |
| 1017 | |
Masami Hiramatsu | 6f6343f | 2014-04-17 17:17:33 +0900 | [diff] [blame] | 1018 | if (val == DIE_GPF) { |
Quentin Barnes | b506a9d | 2008-01-30 13:32:32 +0100 | [diff] [blame] | 1019 | /* |
| 1020 | * To be potentially processing a kprobe fault and to |
| 1021 | * trust the result from kprobe_running(), we have |
| 1022 | * be non-preemptible. |
| 1023 | */ |
| 1024 | if (!preemptible() && kprobe_running() && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | kprobe_fault_handler(args->regs, args->trapnr)) |
Ananth N Mavinakayanahalli | 66ff2d0 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 1026 | ret = NOTIFY_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | } |
Ananth N Mavinakayanahalli | 66ff2d0 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 1028 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | } |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 1030 | NOKPROBE_SYMBOL(kprobe_exceptions_notify); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 1032 | int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | { |
| 1034 | struct jprobe *jp = container_of(p, struct jprobe, kp); |
| 1035 | unsigned long addr; |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 1036 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 1038 | kcb->jprobe_saved_regs = *regs; |
Masami Hiramatsu | 8533bbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 1039 | kcb->jprobe_saved_sp = stack_addr(regs); |
| 1040 | addr = (unsigned long)(kcb->jprobe_saved_sp); |
| 1041 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | /* |
| 1043 | * As Linus pointed out, gcc assumes that the callee |
| 1044 | * owns the argument space and could overwrite it, e.g. |
| 1045 | * tailcall optimization. So, to be absolutely safe |
| 1046 | * we also save and restore enough stack bytes to cover |
| 1047 | * the argument area. |
| 1048 | */ |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 1049 | memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr, |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 1050 | MIN_STACK_SIZE(addr)); |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 1051 | regs->flags &= ~X86_EFLAGS_IF; |
Peter Zijlstra | 58dfe88 | 2007-10-11 22:25:25 +0200 | [diff] [blame] | 1052 | trace_hardirqs_off(); |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 1053 | regs->ip = (unsigned long)(jp->entry); |
Steven Rostedt (Red Hat) | 237d28d | 2015-01-12 12:12:03 -0500 | [diff] [blame] | 1054 | |
| 1055 | /* |
| 1056 | * jprobes use jprobe_return() which skips the normal return |
| 1057 | * path of the function, and this messes up the accounting of the |
| 1058 | * function graph tracer to get messed up. |
| 1059 | * |
| 1060 | * Pause function graph tracing while performing the jprobe function. |
| 1061 | */ |
| 1062 | pause_graph_tracing(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | return 1; |
| 1064 | } |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 1065 | NOKPROBE_SYMBOL(setjmp_pre_handler); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 1067 | void jprobe_return(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | { |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 1069 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
| 1070 | |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 1071 | asm volatile ( |
| 1072 | #ifdef CONFIG_X86_64 |
| 1073 | " xchg %%rbx,%%rsp \n" |
| 1074 | #else |
| 1075 | " xchgl %%ebx,%%esp \n" |
| 1076 | #endif |
| 1077 | " int3 \n" |
| 1078 | " .globl jprobe_return_end\n" |
| 1079 | " jprobe_return_end: \n" |
| 1080 | " nop \n"::"b" |
| 1081 | (kcb->jprobe_saved_sp):"memory"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | } |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 1083 | NOKPROBE_SYMBOL(jprobe_return); |
| 1084 | NOKPROBE_SYMBOL(jprobe_return_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 1086 | int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | { |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 1088 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 1089 | u8 *addr = (u8 *) (regs->ip - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | struct jprobe *jp = container_of(p, struct jprobe, kp); |
Steven Rostedt (Red Hat) | 237d28d | 2015-01-12 12:12:03 -0500 | [diff] [blame] | 1091 | void *saved_sp = kcb->jprobe_saved_sp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 1093 | if ((addr > (u8 *) jprobe_return) && |
| 1094 | (addr < (u8 *) jprobe_return_end)) { |
Steven Rostedt (Red Hat) | 237d28d | 2015-01-12 12:12:03 -0500 | [diff] [blame] | 1095 | if (stack_addr(regs) != saved_sp) { |
Masami Hiramatsu | 29b6cd7 | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 1096 | struct pt_regs *saved_regs = &kcb->jprobe_saved_regs; |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 1097 | printk(KERN_ERR |
| 1098 | "current sp %p does not match saved sp %p\n", |
Steven Rostedt (Red Hat) | 237d28d | 2015-01-12 12:12:03 -0500 | [diff] [blame] | 1099 | stack_addr(regs), saved_sp); |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 1100 | printk(KERN_ERR "Saved registers for jprobe %p\n", jp); |
Jan Beulich | 57da8b9 | 2012-05-09 08:47:37 +0100 | [diff] [blame] | 1101 | show_regs(saved_regs); |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 1102 | printk(KERN_ERR "Current registers\n"); |
Jan Beulich | 57da8b9 | 2012-05-09 08:47:37 +0100 | [diff] [blame] | 1103 | show_regs(regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | BUG(); |
| 1105 | } |
Steven Rostedt (Red Hat) | 237d28d | 2015-01-12 12:12:03 -0500 | [diff] [blame] | 1106 | /* It's OK to start function graph tracing again */ |
| 1107 | unpause_graph_tracing(); |
Ananth N Mavinakayanahalli | e7a510f | 2005-11-07 01:00:12 -0800 | [diff] [blame] | 1108 | *regs = kcb->jprobe_saved_regs; |
Steven Rostedt (Red Hat) | 237d28d | 2015-01-12 12:12:03 -0500 | [diff] [blame] | 1109 | memcpy(saved_sp, kcb->jprobes_stack, MIN_STACK_SIZE(saved_sp)); |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 1110 | preempt_enable_no_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | return 1; |
| 1112 | } |
| 1113 | return 0; |
| 1114 | } |
Masami Hiramatsu | 9326638 | 2014-04-17 17:18:14 +0900 | [diff] [blame] | 1115 | NOKPROBE_SYMBOL(longjmp_break_handler); |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 1116 | |
Masami Hiramatsu | be8f274 | 2014-04-17 17:16:58 +0900 | [diff] [blame] | 1117 | bool arch_within_kprobe_blacklist(unsigned long addr) |
| 1118 | { |
| 1119 | return (addr >= (unsigned long)__kprobes_text_start && |
| 1120 | addr < (unsigned long)__kprobes_text_end) || |
| 1121 | (addr >= (unsigned long)__entry_text_start && |
| 1122 | addr < (unsigned long)__entry_text_end); |
| 1123 | } |
| 1124 | |
Rusty Lynch | 6772926 | 2005-07-05 18:54:50 -0700 | [diff] [blame] | 1125 | int __init arch_init_kprobes(void) |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 1126 | { |
Masami Hiramatsu | a7b0133 | 2013-07-18 20:47:50 +0900 | [diff] [blame] | 1127 | return 0; |
Rusty Lynch | ba8af12 | 2005-06-27 15:17:10 -0700 | [diff] [blame] | 1128 | } |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 1129 | |
Masami Hiramatsu | 7ec8a97 | 2014-04-17 17:17:47 +0900 | [diff] [blame] | 1130 | int arch_trampoline_kprobe(struct kprobe *p) |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 1131 | { |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 1132 | return 0; |
| 1133 | } |