Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 1 | /* |
Ingo Molnar | 7b2d81d | 2012-02-17 09:27:41 +0100 | [diff] [blame] | 2 | * User-space Probes (UProbes) for x86 |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [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, 2008-2011 |
| 19 | * Authors: |
| 20 | * Srikar Dronamraju |
| 21 | * Jim Keniston |
| 22 | */ |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 23 | #include <linux/kernel.h> |
| 24 | #include <linux/sched.h> |
| 25 | #include <linux/ptrace.h> |
| 26 | #include <linux/uprobes.h> |
Srikar Dronamraju | 0326f5a | 2012-03-13 23:30:11 +0530 | [diff] [blame] | 27 | #include <linux/uaccess.h> |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 28 | |
| 29 | #include <linux/kdebug.h> |
Srikar Dronamraju | 0326f5a | 2012-03-13 23:30:11 +0530 | [diff] [blame] | 30 | #include <asm/processor.h> |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 31 | #include <asm/insn.h> |
| 32 | |
| 33 | /* Post-execution fixups. */ |
| 34 | |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 35 | /* Adjust IP back to vicinity of actual insn */ |
Oleg Nesterov | 78d9af4 | 2014-04-24 18:52:37 +0200 | [diff] [blame] | 36 | #define UPROBE_FIX_IP 0x01 |
Srikar Dronamraju | 0326f5a | 2012-03-13 23:30:11 +0530 | [diff] [blame] | 37 | |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 38 | /* Adjust the return address of a call insn */ |
Oleg Nesterov | 78d9af4 | 2014-04-24 18:52:37 +0200 | [diff] [blame] | 39 | #define UPROBE_FIX_CALL 0x02 |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 40 | |
Sebastian Andrzej Siewior | bdc1e47 | 2012-08-20 12:47:34 +0200 | [diff] [blame] | 41 | /* Instruction will modify TF, don't change it */ |
Oleg Nesterov | 78d9af4 | 2014-04-24 18:52:37 +0200 | [diff] [blame] | 42 | #define UPROBE_FIX_SETF 0x04 |
Sebastian Andrzej Siewior | bdc1e47 | 2012-08-20 12:47:34 +0200 | [diff] [blame] | 43 | |
Denys Vlasenko | 1ea30fb | 2014-05-02 17:04:00 +0200 | [diff] [blame] | 44 | #define UPROBE_FIX_RIP_SI 0x08 |
| 45 | #define UPROBE_FIX_RIP_DI 0x10 |
| 46 | #define UPROBE_FIX_RIP_BX 0x20 |
| 47 | #define UPROBE_FIX_RIP_MASK \ |
| 48 | (UPROBE_FIX_RIP_SI | UPROBE_FIX_RIP_DI | UPROBE_FIX_RIP_BX) |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 49 | |
Srikar Dronamraju | 0326f5a | 2012-03-13 23:30:11 +0530 | [diff] [blame] | 50 | #define UPROBE_TRAP_NR UINT_MAX |
| 51 | |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 52 | /* Adaptations for mhiramat x86 decoder v14. */ |
Ingo Molnar | 7b2d81d | 2012-02-17 09:27:41 +0100 | [diff] [blame] | 53 | #define OPCODE1(insn) ((insn)->opcode.bytes[0]) |
| 54 | #define OPCODE2(insn) ((insn)->opcode.bytes[1]) |
| 55 | #define OPCODE3(insn) ((insn)->opcode.bytes[2]) |
Oleg Nesterov | ddb69f2 | 2014-03-31 15:16:22 +0200 | [diff] [blame] | 56 | #define MODRM_REG(insn) X86_MODRM_REG((insn)->modrm.value) |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 57 | |
| 58 | #define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\ |
| 59 | (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \ |
| 60 | (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \ |
| 61 | (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \ |
| 62 | (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \ |
| 63 | << (row % 32)) |
| 64 | |
Srikar Dronamraju | 04a3d98 | 2012-02-22 14:45:35 +0530 | [diff] [blame] | 65 | /* |
| 66 | * Good-instruction tables for 32-bit apps. This is non-const and volatile |
| 67 | * to keep gcc from statically optimizing it out, as variable_test_bit makes |
| 68 | * some versions of gcc to think only *(unsigned long*) is used. |
Denys Vlasenko | 097f4e5 | 2015-02-12 20:18:50 +0100 | [diff] [blame^] | 69 | * |
| 70 | * Prefixes. Most marked as "bad", but it doesn't matter, since insn decoder |
| 71 | * won't report *prefixes* as OPCODE1(insn). |
| 72 | * 0f - 2-byte opcode prefix |
| 73 | * 26,2e,36,3e - es:/cs:/ss:/ds: |
| 74 | * 64 - fs: (marked as "good", why?) |
| 75 | * 65 - gs: (marked as "good", why?) |
| 76 | * 66 - operand-size prefix |
| 77 | * 67 - address-size prefix |
| 78 | * f0 - lock prefix |
| 79 | * f2 - repnz (marked as "good", why?) |
| 80 | * f3 - rep/repz (marked as "good", why?) |
| 81 | * |
| 82 | * Opcodes we'll probably never support: |
| 83 | * 6c-6f - ins,outs. SEGVs if used in userspace |
| 84 | * e4-e7 - in,out imm. SEGVs if used in userspace |
| 85 | * ec-ef - in,out acc. SEGVs if used in userspace |
| 86 | * cc - int3. SIGTRAP if used in userspace |
| 87 | * ce - into. Not used in userspace - no kernel support to make it useful. SEGVs |
| 88 | * (why we support bound (62) then? it's similar, and similarly unused...) |
| 89 | * f1 - int1. SIGTRAP if used in userspace |
| 90 | * f4 - hlt. SEGVs if used in userspace |
| 91 | * fa - cli. SEGVs if used in userspace |
| 92 | * fb - sti. SEGVs if used in userspace |
| 93 | * |
| 94 | * Opcodes which need some work to be supported: |
| 95 | * 07,17,1f - pop es/ss/ds |
| 96 | * Normally not used in userspace, but would execute if used. |
| 97 | * Can cause GP or stack exception if tries to load wrong segment descriptor. |
| 98 | * We hesitate to run them under single step since kernel's handling |
| 99 | * of userspace single-stepping (TF flag) is fragile. |
| 100 | * We can easily refuse to support push es/cs/ss/ds (06/0e/16/1e) |
| 101 | * on the same grounds that they are never used. |
| 102 | * cd - int N. |
| 103 | * Used by userspace for "int 80" syscall entry. (Other "int N" |
| 104 | * cause GP -> SEGV since their IDT gates don't allow calls from CPL 3). |
| 105 | * Not supported since kernel's handling of userspace single-stepping |
| 106 | * (TF flag) is fragile. |
| 107 | * cf - iret. Normally not used in userspace. Doesn't SEGV unless arguments are bad |
| 108 | * |
| 109 | * Opcodes which can be enabled right away: |
| 110 | * 63 - arpl. This insn has no unusual exceptions (it's basically an arith op). |
| 111 | * d6 - salc. Undocumented "sign-extend carry flag to AL" insn |
Srikar Dronamraju | 04a3d98 | 2012-02-22 14:45:35 +0530 | [diff] [blame] | 112 | */ |
Oleg Nesterov | 8dbacad | 2014-04-19 16:07:15 +0200 | [diff] [blame] | 113 | #if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION) |
Srikar Dronamraju | 04a3d98 | 2012-02-22 14:45:35 +0530 | [diff] [blame] | 114 | static volatile u32 good_insns_32[256 / 32] = { |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 115 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ |
| 116 | /* ---------------------------------------------- */ |
| 117 | W(0x00, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0) | /* 00 */ |
| 118 | W(0x10, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0) , /* 10 */ |
| 119 | W(0x20, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1) | /* 20 */ |
| 120 | W(0x30, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1) , /* 30 */ |
| 121 | W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */ |
| 122 | W(0x50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 50 */ |
| 123 | W(0x60, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* 60 */ |
| 124 | W(0x70, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 70 */ |
| 125 | W(0x80, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 80 */ |
| 126 | W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */ |
| 127 | W(0xa0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* a0 */ |
| 128 | W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* b0 */ |
| 129 | W(0xc0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0) | /* c0 */ |
| 130 | W(0xd0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* d0 */ |
| 131 | W(0xe0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* e0 */ |
| 132 | W(0xf0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1) /* f0 */ |
| 133 | /* ---------------------------------------------- */ |
| 134 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ |
| 135 | }; |
Oleg Nesterov | 8dbacad | 2014-04-19 16:07:15 +0200 | [diff] [blame] | 136 | #else |
| 137 | #define good_insns_32 NULL |
| 138 | #endif |
| 139 | |
Denys Vlasenko | 097f4e5 | 2015-02-12 20:18:50 +0100 | [diff] [blame^] | 140 | /* Good-instruction tables for 64-bit apps. |
| 141 | * |
| 142 | * Prefixes. Most marked as "bad", but it doesn't matter, since insn decoder |
| 143 | * won't report *prefixes* as OPCODE1(insn). |
| 144 | * 0f - 2-byte opcode prefix |
| 145 | * 26,2e,36,3e - es:/cs:/ss:/ds: |
| 146 | * 40-4f - rex prefixes |
| 147 | * 64 - fs: (marked as "good", why?) |
| 148 | * 65 - gs: (marked as "good", why?) |
| 149 | * 66 - operand-size prefix |
| 150 | * 67 - address-size prefix |
| 151 | * f0 - lock prefix |
| 152 | * f2 - repnz (marked as "good", why?) |
| 153 | * f3 - rep/repz (marked as "good", why?) |
| 154 | * |
| 155 | * Genuinely invalid opcodes: |
| 156 | * 06,07 - formerly push/pop es |
| 157 | * 0e - formerly push cs |
| 158 | * 16,17 - formerly push/pop ss |
| 159 | * 1e,1f - formerly push/pop ds |
| 160 | * 27,2f,37,3f - formerly daa/das/aaa/aas |
| 161 | * 60,61 - formerly pusha/popa |
| 162 | * 62 - formerly bound. EVEX prefix for AVX512 |
| 163 | * 82 - formerly redundant encoding of Group1 |
| 164 | * 9a - formerly call seg:ofs (marked as "supported"???) |
| 165 | * c4,c5 - formerly les/lds. VEX prefixes for AVX |
| 166 | * ce - formerly into |
| 167 | * d4,d5 - formerly aam/aad |
| 168 | * d6 - formerly undocumented salc |
| 169 | * ea - formerly jmp seg:ofs (marked as "supported"???) |
| 170 | * |
| 171 | * Opcodes we'll probably never support: |
| 172 | * 6c-6f - ins,outs. SEGVs if used in userspace |
| 173 | * e4-e7 - in,out imm. SEGVs if used in userspace |
| 174 | * ec-ef - in,out acc. SEGVs if used in userspace |
| 175 | * cc - int3. SIGTRAP if used in userspace |
| 176 | * f1 - int1. SIGTRAP if used in userspace |
| 177 | * f4 - hlt. SEGVs if used in userspace |
| 178 | * fa - cli. SEGVs if used in userspace |
| 179 | * fb - sti. SEGVs if used in userspace |
| 180 | * |
| 181 | * Opcodes which need some work to be supported: |
| 182 | * cd - int N. |
| 183 | * Used by userspace for "int 80" syscall entry. (Other "int N" |
| 184 | * cause GP -> SEGV since their IDT gates don't allow calls from CPL 3). |
| 185 | * Not supported since kernel's handling of userspace single-stepping |
| 186 | * (TF flag) is fragile. |
| 187 | * cf - iret. Normally not used in userspace. Doesn't SEGV unless arguments are bad |
| 188 | */ |
Oleg Nesterov | 8dbacad | 2014-04-19 16:07:15 +0200 | [diff] [blame] | 189 | #if defined(CONFIG_X86_64) |
| 190 | static volatile u32 good_insns_64[256 / 32] = { |
| 191 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ |
| 192 | /* ---------------------------------------------- */ |
| 193 | W(0x00, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0) | /* 00 */ |
| 194 | W(0x10, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0) , /* 10 */ |
| 195 | W(0x20, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0) | /* 20 */ |
| 196 | W(0x30, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0) , /* 30 */ |
| 197 | W(0x40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 40 */ |
| 198 | W(0x50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 50 */ |
| 199 | W(0x60, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* 60 */ |
| 200 | W(0x70, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 70 */ |
| 201 | W(0x80, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 80 */ |
| 202 | W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */ |
| 203 | W(0xa0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* a0 */ |
| 204 | W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* b0 */ |
| 205 | W(0xc0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0) | /* c0 */ |
| 206 | W(0xd0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* d0 */ |
| 207 | W(0xe0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* e0 */ |
| 208 | W(0xf0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1) /* f0 */ |
| 209 | /* ---------------------------------------------- */ |
| 210 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ |
| 211 | }; |
| 212 | #else |
| 213 | #define good_insns_64 NULL |
| 214 | #endif |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 215 | |
Denys Vlasenko | 097f4e5 | 2015-02-12 20:18:50 +0100 | [diff] [blame^] | 216 | /* Using this for both 64-bit and 32-bit apps. |
| 217 | * Opcodes we don't support: |
| 218 | * 0f 00 - SLDT/STR/LLDT/LTR/VERR/VERW/-/- group. System insns |
| 219 | * 0f 01 - SGDT/SIDT/LGDT/LIDT/SMSW/-/LMSW/INVLPG group. |
| 220 | * Also encodes tons of other system insns if mod=11. |
| 221 | * Some are in fact non-system: xend, xtest, rdtscp, maybe more |
| 222 | * 0f 02 - lar (why? should be safe, it throws no exceptipons) |
| 223 | * 0f 03 - lsl (why? should be safe, it throws no exceptipons) |
| 224 | * 0f 04 - undefined |
| 225 | * 0f 05 - syscall |
| 226 | * 0f 06 - clts (CPL0 insn) |
| 227 | * 0f 07 - sysret |
| 228 | * 0f 08 - invd (CPL0 insn) |
| 229 | * 0f 09 - wbinvd (CPL0 insn) |
| 230 | * 0f 0a - undefined |
| 231 | * 0f 0b - ud2 |
| 232 | * 0f 0c - undefined |
| 233 | * 0f 0d - prefetchFOO (amd prefetch insns) |
| 234 | * 0f 18 - prefetchBAR (intel prefetch insns) |
| 235 | * 0f 24 - mov from test regs (perhaps entire 20-27 area can be disabled (special reg ops)) |
| 236 | * 0f 25 - undefined |
| 237 | * 0f 26 - mov to test regs |
| 238 | * 0f 27 - undefined |
| 239 | * 0f 30 - wrmsr (CPL0 insn) |
| 240 | * 0f 34 - sysenter |
| 241 | * 0f 35 - sysexit |
| 242 | * 0f 36 - undefined |
| 243 | * 0f 37 - getsec |
| 244 | * 0f 38-3f - 3-byte opcodes (why?? all look safe) |
| 245 | * 0f 78 - vmread |
| 246 | * 0f 79 - vmwrite |
| 247 | * 0f 7a - undefined |
| 248 | * 0f 7b - undefined |
| 249 | * 0f 7c - undefined |
| 250 | * 0f 7d - undefined |
| 251 | * 0f a6 - undefined |
| 252 | * 0f a7 - undefined |
| 253 | * 0f b8 - popcnt (why?? it's an ordinary ALU op) |
| 254 | * 0f d0 - undefined |
| 255 | * 0f f0 - lddqu (why?? it's an ordinary vector load op) |
| 256 | * 0f ff - undefined |
| 257 | */ |
Srikar Dronamraju | 04a3d98 | 2012-02-22 14:45:35 +0530 | [diff] [blame] | 258 | static volatile u32 good_2byte_insns[256 / 32] = { |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 259 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ |
| 260 | /* ---------------------------------------------- */ |
| 261 | W(0x00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1) | /* 00 */ |
| 262 | W(0x10, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1) , /* 10 */ |
| 263 | W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* 20 */ |
| 264 | W(0x30, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */ |
| 265 | W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */ |
| 266 | W(0x50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 50 */ |
| 267 | W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 60 */ |
| 268 | W(0x70, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */ |
| 269 | W(0x80, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 80 */ |
| 270 | W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */ |
| 271 | W(0xa0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1) | /* a0 */ |
| 272 | W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1) , /* b0 */ |
| 273 | W(0xc0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */ |
| 274 | W(0xd0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* d0 */ |
| 275 | W(0xe0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* e0 */ |
| 276 | W(0xf0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0) /* f0 */ |
| 277 | /* ---------------------------------------------- */ |
| 278 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ |
| 279 | }; |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 280 | #undef W |
| 281 | |
| 282 | /* |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 283 | * opcodes we may need to refine support for: |
Ingo Molnar | 7b2d81d | 2012-02-17 09:27:41 +0100 | [diff] [blame] | 284 | * |
| 285 | * 0f - 2-byte instructions: For many of these instructions, the validity |
| 286 | * depends on the prefix and/or the reg field. On such instructions, we |
| 287 | * just consider the opcode combination valid if it corresponds to any |
| 288 | * valid instruction. |
| 289 | * |
| 290 | * 8f - Group 1 - only reg = 0 is OK |
| 291 | * c6-c7 - Group 11 - only reg = 0 is OK |
| 292 | * d9-df - fpu insns with some illegal encodings |
| 293 | * f2, f3 - repnz, repz prefixes. These are also the first byte for |
| 294 | * certain floating-point instructions, such as addsd. |
| 295 | * |
| 296 | * fe - Group 4 - only reg = 0 or 1 is OK |
| 297 | * ff - Group 5 - only reg = 0-6 is OK |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 298 | * |
| 299 | * others -- Do we need to support these? |
Ingo Molnar | 7b2d81d | 2012-02-17 09:27:41 +0100 | [diff] [blame] | 300 | * |
| 301 | * 0f - (floating-point?) prefetch instructions |
| 302 | * 07, 17, 1f - pop es, pop ss, pop ds |
| 303 | * 26, 2e, 36, 3e - es:, cs:, ss:, ds: segment prefixes -- |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 304 | * but 64 and 65 (fs: and gs:) seem to be used, so we support them |
Ingo Molnar | 7b2d81d | 2012-02-17 09:27:41 +0100 | [diff] [blame] | 305 | * 67 - addr16 prefix |
| 306 | * ce - into |
| 307 | * f0 - lock prefix |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 308 | */ |
| 309 | |
| 310 | /* |
| 311 | * TODO: |
| 312 | * - Where necessary, examine the modrm byte and allow only valid instructions |
| 313 | * in the different Groups and fpu instructions. |
| 314 | */ |
| 315 | |
| 316 | static bool is_prefix_bad(struct insn *insn) |
| 317 | { |
| 318 | int i; |
| 319 | |
| 320 | for (i = 0; i < insn->prefixes.nbytes; i++) { |
| 321 | switch (insn->prefixes.bytes[i]) { |
Ingo Molnar | 7b2d81d | 2012-02-17 09:27:41 +0100 | [diff] [blame] | 322 | case 0x26: /* INAT_PFX_ES */ |
| 323 | case 0x2E: /* INAT_PFX_CS */ |
| 324 | case 0x36: /* INAT_PFX_DS */ |
| 325 | case 0x3E: /* INAT_PFX_SS */ |
| 326 | case 0xF0: /* INAT_PFX_LOCK */ |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 327 | return true; |
| 328 | } |
| 329 | } |
| 330 | return false; |
| 331 | } |
| 332 | |
Oleg Nesterov | 73175d0 | 2014-04-19 12:34:02 +0200 | [diff] [blame] | 333 | static int uprobe_init_insn(struct arch_uprobe *auprobe, struct insn *insn, bool x86_64) |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 334 | { |
Oleg Nesterov | 73175d0 | 2014-04-19 12:34:02 +0200 | [diff] [blame] | 335 | u32 volatile *good_insns; |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 336 | |
Dave Hansen | 6ba48ff | 2014-11-14 07:39:57 -0800 | [diff] [blame] | 337 | insn_init(insn, auprobe->insn, sizeof(auprobe->insn), x86_64); |
Oleg Nesterov | ff26196 | 2014-04-19 14:15:27 +0200 | [diff] [blame] | 338 | /* has the side-effect of processing the entire instruction */ |
| 339 | insn_get_length(insn); |
| 340 | if (WARN_ON_ONCE(!insn_complete(insn))) |
| 341 | return -ENOEXEC; |
Oleg Nesterov | 73175d0 | 2014-04-19 12:34:02 +0200 | [diff] [blame] | 342 | |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 343 | if (is_prefix_bad(insn)) |
| 344 | return -ENOTSUPP; |
Ingo Molnar | 7b2d81d | 2012-02-17 09:27:41 +0100 | [diff] [blame] | 345 | |
Oleg Nesterov | 73175d0 | 2014-04-19 12:34:02 +0200 | [diff] [blame] | 346 | if (x86_64) |
| 347 | good_insns = good_insns_64; |
| 348 | else |
| 349 | good_insns = good_insns_32; |
| 350 | |
| 351 | if (test_bit(OPCODE1(insn), (unsigned long *)good_insns)) |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 352 | return 0; |
Ingo Molnar | 7b2d81d | 2012-02-17 09:27:41 +0100 | [diff] [blame] | 353 | |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 354 | if (insn->opcode.nbytes == 2) { |
| 355 | if (test_bit(OPCODE2(insn), (unsigned long *)good_2byte_insns)) |
| 356 | return 0; |
| 357 | } |
Ingo Molnar | 7b2d81d | 2012-02-17 09:27:41 +0100 | [diff] [blame] | 358 | |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 359 | return -ENOTSUPP; |
| 360 | } |
| 361 | |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 362 | #ifdef CONFIG_X86_64 |
Oleg Nesterov | 2ae1f49 | 2014-04-19 14:03:05 +0200 | [diff] [blame] | 363 | static inline bool is_64bit_mm(struct mm_struct *mm) |
| 364 | { |
| 365 | return !config_enabled(CONFIG_IA32_EMULATION) || |
Oleg Nesterov | b24dc8d | 2014-04-19 18:10:09 +0200 | [diff] [blame] | 366 | !(mm->context.ia32_compat == TIF_IA32); |
Oleg Nesterov | 2ae1f49 | 2014-04-19 14:03:05 +0200 | [diff] [blame] | 367 | } |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 368 | /* |
Srikar Dronamraju | 3ff54ef | 2012-02-22 14:46:02 +0530 | [diff] [blame] | 369 | * If arch_uprobe->insn doesn't use rip-relative addressing, return |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 370 | * immediately. Otherwise, rewrite the instruction so that it accesses |
| 371 | * its memory operand indirectly through a scratch register. Set |
Oleg Nesterov | 5cdb76d | 2014-06-01 21:13:46 +0200 | [diff] [blame] | 372 | * defparam->fixups accordingly. (The contents of the scratch register |
Denys Vlasenko | 50204c6 | 2014-05-01 16:52:46 +0200 | [diff] [blame] | 373 | * will be saved before we single-step the modified instruction, |
| 374 | * and restored afterward). |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 375 | * |
| 376 | * We do this because a rip-relative instruction can access only a |
| 377 | * relatively small area (+/- 2 GB from the instruction), and the XOL |
| 378 | * area typically lies beyond that area. At least for instructions |
| 379 | * that store to memory, we can't execute the original instruction |
| 380 | * and "fix things up" later, because the misdirected store could be |
| 381 | * disastrous. |
| 382 | * |
| 383 | * Some useful facts about rip-relative instructions: |
Ingo Molnar | 7b2d81d | 2012-02-17 09:27:41 +0100 | [diff] [blame] | 384 | * |
Denys Vlasenko | 50204c6 | 2014-05-01 16:52:46 +0200 | [diff] [blame] | 385 | * - There's always a modrm byte with bit layout "00 reg 101". |
Ingo Molnar | 7b2d81d | 2012-02-17 09:27:41 +0100 | [diff] [blame] | 386 | * - There's never a SIB byte. |
| 387 | * - The displacement is always 4 bytes. |
Denys Vlasenko | 50204c6 | 2014-05-01 16:52:46 +0200 | [diff] [blame] | 388 | * - REX.B=1 bit in REX prefix, which normally extends r/m field, |
| 389 | * has no effect on rip-relative mode. It doesn't make modrm byte |
| 390 | * with r/m=101 refer to register 1101 = R13. |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 391 | */ |
Oleg Nesterov | 1475ee7 | 2014-04-27 16:31:59 +0200 | [diff] [blame] | 392 | static void riprel_analyze(struct arch_uprobe *auprobe, struct insn *insn) |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 393 | { |
| 394 | u8 *cursor; |
| 395 | u8 reg; |
Denys Vlasenko | 1ea30fb | 2014-05-02 17:04:00 +0200 | [diff] [blame] | 396 | u8 reg2; |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 397 | |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 398 | if (!insn_rip_relative(insn)) |
| 399 | return; |
| 400 | |
| 401 | /* |
Denys Vlasenko | 1ea30fb | 2014-05-02 17:04:00 +0200 | [diff] [blame] | 402 | * insn_rip_relative() would have decoded rex_prefix, vex_prefix, modrm. |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 403 | * Clear REX.b bit (extension of MODRM.rm field): |
Denys Vlasenko | 1ea30fb | 2014-05-02 17:04:00 +0200 | [diff] [blame] | 404 | * we want to encode low numbered reg, not r8+. |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 405 | */ |
| 406 | if (insn->rex_prefix.nbytes) { |
Srikar Dronamraju | 3ff54ef | 2012-02-22 14:46:02 +0530 | [diff] [blame] | 407 | cursor = auprobe->insn + insn_offset_rex_prefix(insn); |
Denys Vlasenko | 1ea30fb | 2014-05-02 17:04:00 +0200 | [diff] [blame] | 408 | /* REX byte has 0100wrxb layout, clearing REX.b bit */ |
| 409 | *cursor &= 0xfe; |
| 410 | } |
| 411 | /* |
| 412 | * Similar treatment for VEX3 prefix. |
| 413 | * TODO: add XOP/EVEX treatment when insn decoder supports them |
| 414 | */ |
| 415 | if (insn->vex_prefix.nbytes == 3) { |
| 416 | /* |
| 417 | * vex2: c5 rvvvvLpp (has no b bit) |
| 418 | * vex3/xop: c4/8f rxbmmmmm wvvvvLpp |
| 419 | * evex: 62 rxbR00mm wvvvv1pp zllBVaaa |
| 420 | * (evex will need setting of both b and x since |
| 421 | * in non-sib encoding evex.x is 4th bit of MODRM.rm) |
| 422 | * Setting VEX3.b (setting because it has inverted meaning): |
| 423 | */ |
| 424 | cursor = auprobe->insn + insn_offset_vex_prefix(insn) + 1; |
| 425 | *cursor |= 0x20; |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | /* |
Denys Vlasenko | 1ea30fb | 2014-05-02 17:04:00 +0200 | [diff] [blame] | 429 | * Convert from rip-relative addressing to register-relative addressing |
| 430 | * via a scratch register. |
| 431 | * |
| 432 | * This is tricky since there are insns with modrm byte |
| 433 | * which also use registers not encoded in modrm byte: |
| 434 | * [i]div/[i]mul: implicitly use dx:ax |
| 435 | * shift ops: implicitly use cx |
| 436 | * cmpxchg: implicitly uses ax |
| 437 | * cmpxchg8/16b: implicitly uses dx:ax and bx:cx |
| 438 | * Encoding: 0f c7/1 modrm |
| 439 | * The code below thinks that reg=1 (cx), chooses si as scratch. |
| 440 | * mulx: implicitly uses dx: mulx r/m,r1,r2 does r1:r2 = dx * r/m. |
| 441 | * First appeared in Haswell (BMI2 insn). It is vex-encoded. |
| 442 | * Example where none of bx,cx,dx can be used as scratch reg: |
| 443 | * c4 e2 63 f6 0d disp32 mulx disp32(%rip),%ebx,%ecx |
| 444 | * [v]pcmpistri: implicitly uses cx, xmm0 |
| 445 | * [v]pcmpistrm: implicitly uses xmm0 |
| 446 | * [v]pcmpestri: implicitly uses ax, dx, cx, xmm0 |
| 447 | * [v]pcmpestrm: implicitly uses ax, dx, xmm0 |
| 448 | * Evil SSE4.2 string comparison ops from hell. |
| 449 | * maskmovq/[v]maskmovdqu: implicitly uses (ds:rdi) as destination. |
| 450 | * Encoding: 0f f7 modrm, 66 0f f7 modrm, vex-encoded: c5 f9 f7 modrm. |
| 451 | * Store op1, byte-masked by op2 msb's in each byte, to (ds:rdi). |
| 452 | * AMD says it has no 3-operand form (vex.vvvv must be 1111) |
| 453 | * and that it can have only register operands, not mem |
| 454 | * (its modrm byte must have mode=11). |
| 455 | * If these restrictions will ever be lifted, |
| 456 | * we'll need code to prevent selection of di as scratch reg! |
| 457 | * |
| 458 | * Summary: I don't know any insns with modrm byte which |
| 459 | * use SI register implicitly. DI register is used only |
| 460 | * by one insn (maskmovq) and BX register is used |
| 461 | * only by one too (cmpxchg8b). |
| 462 | * BP is stack-segment based (may be a problem?). |
| 463 | * AX, DX, CX are off-limits (many implicit users). |
| 464 | * SP is unusable (it's stack pointer - think about "pop mem"; |
| 465 | * also, rsp+disp32 needs sib encoding -> insn length change). |
| 466 | */ |
| 467 | |
| 468 | reg = MODRM_REG(insn); /* Fetch modrm.reg */ |
| 469 | reg2 = 0xff; /* Fetch vex.vvvv */ |
| 470 | if (insn->vex_prefix.nbytes == 2) |
| 471 | reg2 = insn->vex_prefix.bytes[1]; |
| 472 | else if (insn->vex_prefix.nbytes == 3) |
| 473 | reg2 = insn->vex_prefix.bytes[2]; |
| 474 | /* |
| 475 | * TODO: add XOP, EXEV vvvv reading. |
| 476 | * |
| 477 | * vex.vvvv field is in bits 6-3, bits are inverted. |
| 478 | * But in 32-bit mode, high-order bit may be ignored. |
| 479 | * Therefore, let's consider only 3 low-order bits. |
| 480 | */ |
| 481 | reg2 = ((reg2 >> 3) & 0x7) ^ 0x7; |
| 482 | /* |
| 483 | * Register numbering is ax,cx,dx,bx, sp,bp,si,di, r8..r15. |
| 484 | * |
| 485 | * Choose scratch reg. Order is important: must not select bx |
| 486 | * if we can use si (cmpxchg8b case!) |
| 487 | */ |
| 488 | if (reg != 6 && reg2 != 6) { |
| 489 | reg2 = 6; |
Oleg Nesterov | 5cdb76d | 2014-06-01 21:13:46 +0200 | [diff] [blame] | 490 | auprobe->defparam.fixups |= UPROBE_FIX_RIP_SI; |
Denys Vlasenko | 1ea30fb | 2014-05-02 17:04:00 +0200 | [diff] [blame] | 491 | } else if (reg != 7 && reg2 != 7) { |
| 492 | reg2 = 7; |
Oleg Nesterov | 5cdb76d | 2014-06-01 21:13:46 +0200 | [diff] [blame] | 493 | auprobe->defparam.fixups |= UPROBE_FIX_RIP_DI; |
Denys Vlasenko | 1ea30fb | 2014-05-02 17:04:00 +0200 | [diff] [blame] | 494 | /* TODO (paranoia): force maskmovq to not use di */ |
| 495 | } else { |
| 496 | reg2 = 3; |
Oleg Nesterov | 5cdb76d | 2014-06-01 21:13:46 +0200 | [diff] [blame] | 497 | auprobe->defparam.fixups |= UPROBE_FIX_RIP_BX; |
Denys Vlasenko | 1ea30fb | 2014-05-02 17:04:00 +0200 | [diff] [blame] | 498 | } |
| 499 | /* |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 500 | * Point cursor at the modrm byte. The next 4 bytes are the |
| 501 | * displacement. Beyond the displacement, for some instructions, |
| 502 | * is the immediate operand. |
| 503 | */ |
Srikar Dronamraju | 3ff54ef | 2012-02-22 14:46:02 +0530 | [diff] [blame] | 504 | cursor = auprobe->insn + insn_offset_modrm(insn); |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 505 | /* |
Denys Vlasenko | 1ea30fb | 2014-05-02 17:04:00 +0200 | [diff] [blame] | 506 | * Change modrm from "00 reg 101" to "10 reg reg2". Example: |
| 507 | * 89 05 disp32 mov %eax,disp32(%rip) becomes |
| 508 | * 89 86 disp32 mov %eax,disp32(%rsi) |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 509 | */ |
Denys Vlasenko | 1ea30fb | 2014-05-02 17:04:00 +0200 | [diff] [blame] | 510 | *cursor = 0x80 | (reg << 3) | reg2; |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 511 | } |
| 512 | |
Oleg Nesterov | c90a695 | 2014-04-27 18:13:31 +0200 | [diff] [blame] | 513 | static inline unsigned long * |
| 514 | scratch_reg(struct arch_uprobe *auprobe, struct pt_regs *regs) |
| 515 | { |
Oleg Nesterov | 5cdb76d | 2014-06-01 21:13:46 +0200 | [diff] [blame] | 516 | if (auprobe->defparam.fixups & UPROBE_FIX_RIP_SI) |
Denys Vlasenko | 1ea30fb | 2014-05-02 17:04:00 +0200 | [diff] [blame] | 517 | return ®s->si; |
Oleg Nesterov | 5cdb76d | 2014-06-01 21:13:46 +0200 | [diff] [blame] | 518 | if (auprobe->defparam.fixups & UPROBE_FIX_RIP_DI) |
Denys Vlasenko | 1ea30fb | 2014-05-02 17:04:00 +0200 | [diff] [blame] | 519 | return ®s->di; |
| 520 | return ®s->bx; |
Oleg Nesterov | c90a695 | 2014-04-27 18:13:31 +0200 | [diff] [blame] | 521 | } |
| 522 | |
Oleg Nesterov | d20737c | 2014-03-31 18:35:09 +0200 | [diff] [blame] | 523 | /* |
| 524 | * If we're emulating a rip-relative instruction, save the contents |
| 525 | * of the scratch register and store the target address in that register. |
| 526 | */ |
Oleg Nesterov | 7f55e82 | 2014-04-27 17:00:46 +0200 | [diff] [blame] | 527 | static void riprel_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs) |
Oleg Nesterov | d20737c | 2014-03-31 18:35:09 +0200 | [diff] [blame] | 528 | { |
Oleg Nesterov | 5cdb76d | 2014-06-01 21:13:46 +0200 | [diff] [blame] | 529 | if (auprobe->defparam.fixups & UPROBE_FIX_RIP_MASK) { |
Oleg Nesterov | c90a695 | 2014-04-27 18:13:31 +0200 | [diff] [blame] | 530 | struct uprobe_task *utask = current->utask; |
| 531 | unsigned long *sr = scratch_reg(auprobe, regs); |
Oleg Nesterov | 7f55e82 | 2014-04-27 17:00:46 +0200 | [diff] [blame] | 532 | |
Oleg Nesterov | c90a695 | 2014-04-27 18:13:31 +0200 | [diff] [blame] | 533 | utask->autask.saved_scratch_register = *sr; |
Oleg Nesterov | 5cdb76d | 2014-06-01 21:13:46 +0200 | [diff] [blame] | 534 | *sr = utask->vaddr + auprobe->defparam.ilen; |
Oleg Nesterov | d20737c | 2014-03-31 18:35:09 +0200 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | |
Denys Vlasenko | 50204c6 | 2014-05-01 16:52:46 +0200 | [diff] [blame] | 538 | static void riprel_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs) |
Oleg Nesterov | d20737c | 2014-03-31 18:35:09 +0200 | [diff] [blame] | 539 | { |
Oleg Nesterov | 5cdb76d | 2014-06-01 21:13:46 +0200 | [diff] [blame] | 540 | if (auprobe->defparam.fixups & UPROBE_FIX_RIP_MASK) { |
Oleg Nesterov | c90a695 | 2014-04-27 18:13:31 +0200 | [diff] [blame] | 541 | struct uprobe_task *utask = current->utask; |
| 542 | unsigned long *sr = scratch_reg(auprobe, regs); |
Oleg Nesterov | d20737c | 2014-03-31 18:35:09 +0200 | [diff] [blame] | 543 | |
Oleg Nesterov | c90a695 | 2014-04-27 18:13:31 +0200 | [diff] [blame] | 544 | *sr = utask->autask.saved_scratch_register; |
Oleg Nesterov | d20737c | 2014-03-31 18:35:09 +0200 | [diff] [blame] | 545 | } |
| 546 | } |
Ingo Molnar | 7b2d81d | 2012-02-17 09:27:41 +0100 | [diff] [blame] | 547 | #else /* 32-bit: */ |
Oleg Nesterov | 2ae1f49 | 2014-04-19 14:03:05 +0200 | [diff] [blame] | 548 | static inline bool is_64bit_mm(struct mm_struct *mm) |
| 549 | { |
| 550 | return false; |
| 551 | } |
Oleg Nesterov | d20737c | 2014-03-31 18:35:09 +0200 | [diff] [blame] | 552 | /* |
| 553 | * No RIP-relative addressing on 32-bit |
| 554 | */ |
Oleg Nesterov | 1475ee7 | 2014-04-27 16:31:59 +0200 | [diff] [blame] | 555 | static void riprel_analyze(struct arch_uprobe *auprobe, struct insn *insn) |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 556 | { |
Oleg Nesterov | d20737c | 2014-03-31 18:35:09 +0200 | [diff] [blame] | 557 | } |
Oleg Nesterov | 7f55e82 | 2014-04-27 17:00:46 +0200 | [diff] [blame] | 558 | static void riprel_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs) |
Oleg Nesterov | d20737c | 2014-03-31 18:35:09 +0200 | [diff] [blame] | 559 | { |
| 560 | } |
Denys Vlasenko | 50204c6 | 2014-05-01 16:52:46 +0200 | [diff] [blame] | 561 | static void riprel_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs) |
Oleg Nesterov | d20737c | 2014-03-31 18:35:09 +0200 | [diff] [blame] | 562 | { |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 563 | } |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 564 | #endif /* CONFIG_X86_64 */ |
| 565 | |
Oleg Nesterov | 8ad8e9d | 2014-03-31 21:01:31 +0200 | [diff] [blame] | 566 | struct uprobe_xol_ops { |
| 567 | bool (*emulate)(struct arch_uprobe *, struct pt_regs *); |
| 568 | int (*pre_xol)(struct arch_uprobe *, struct pt_regs *); |
| 569 | int (*post_xol)(struct arch_uprobe *, struct pt_regs *); |
Oleg Nesterov | 588fbd6 | 2014-04-21 16:58:17 +0200 | [diff] [blame] | 570 | void (*abort)(struct arch_uprobe *, struct pt_regs *); |
Oleg Nesterov | 8ad8e9d | 2014-03-31 21:01:31 +0200 | [diff] [blame] | 571 | }; |
| 572 | |
Oleg Nesterov | 8faaed1 | 2014-04-06 17:16:10 +0200 | [diff] [blame] | 573 | static inline int sizeof_long(void) |
| 574 | { |
| 575 | return is_ia32_task() ? 4 : 8; |
| 576 | } |
| 577 | |
Oleg Nesterov | 8ad8e9d | 2014-03-31 21:01:31 +0200 | [diff] [blame] | 578 | static int default_pre_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs) |
| 579 | { |
Oleg Nesterov | 7f55e82 | 2014-04-27 17:00:46 +0200 | [diff] [blame] | 580 | riprel_pre_xol(auprobe, regs); |
Oleg Nesterov | 8ad8e9d | 2014-03-31 21:01:31 +0200 | [diff] [blame] | 581 | return 0; |
| 582 | } |
| 583 | |
Oleg Nesterov | 2b82cad | 2014-04-24 19:21:38 +0200 | [diff] [blame] | 584 | static int push_ret_address(struct pt_regs *regs, unsigned long ip) |
| 585 | { |
| 586 | unsigned long new_sp = regs->sp - sizeof_long(); |
| 587 | |
| 588 | if (copy_to_user((void __user *)new_sp, &ip, sizeof_long())) |
| 589 | return -EFAULT; |
| 590 | |
| 591 | regs->sp = new_sp; |
| 592 | return 0; |
| 593 | } |
| 594 | |
Denys Vlasenko | 1ea30fb | 2014-05-02 17:04:00 +0200 | [diff] [blame] | 595 | /* |
| 596 | * We have to fix things up as follows: |
| 597 | * |
| 598 | * Typically, the new ip is relative to the copied instruction. We need |
| 599 | * to make it relative to the original instruction (FIX_IP). Exceptions |
| 600 | * are return instructions and absolute or indirect jump or call instructions. |
| 601 | * |
| 602 | * If the single-stepped instruction was a call, the return address that |
| 603 | * is atop the stack is the address following the copied instruction. We |
| 604 | * need to make it the address following the original instruction (FIX_CALL). |
| 605 | * |
| 606 | * If the original instruction was a rip-relative instruction such as |
| 607 | * "movl %edx,0xnnnn(%rip)", we have instead executed an equivalent |
| 608 | * instruction using a scratch register -- e.g., "movl %edx,0xnnnn(%rsi)". |
| 609 | * We need to restore the contents of the scratch register |
| 610 | * (FIX_RIP_reg). |
| 611 | */ |
Oleg Nesterov | 8ad8e9d | 2014-03-31 21:01:31 +0200 | [diff] [blame] | 612 | static int default_post_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs) |
| 613 | { |
| 614 | struct uprobe_task *utask = current->utask; |
Oleg Nesterov | 8ad8e9d | 2014-03-31 21:01:31 +0200 | [diff] [blame] | 615 | |
Denys Vlasenko | 50204c6 | 2014-05-01 16:52:46 +0200 | [diff] [blame] | 616 | riprel_post_xol(auprobe, regs); |
Oleg Nesterov | 5cdb76d | 2014-06-01 21:13:46 +0200 | [diff] [blame] | 617 | if (auprobe->defparam.fixups & UPROBE_FIX_IP) { |
Denys Vlasenko | 50204c6 | 2014-05-01 16:52:46 +0200 | [diff] [blame] | 618 | long correction = utask->vaddr - utask->xol_vaddr; |
Oleg Nesterov | 8ad8e9d | 2014-03-31 21:01:31 +0200 | [diff] [blame] | 619 | regs->ip += correction; |
Oleg Nesterov | 5cdb76d | 2014-06-01 21:13:46 +0200 | [diff] [blame] | 620 | } else if (auprobe->defparam.fixups & UPROBE_FIX_CALL) { |
| 621 | regs->sp += sizeof_long(); /* Pop incorrect return address */ |
| 622 | if (push_ret_address(regs, utask->vaddr + auprobe->defparam.ilen)) |
Oleg Nesterov | 75f9ef0 | 2014-04-03 20:52:19 +0200 | [diff] [blame] | 623 | return -ERESTART; |
Oleg Nesterov | 75f9ef0 | 2014-04-03 20:52:19 +0200 | [diff] [blame] | 624 | } |
Oleg Nesterov | 220ef8d | 2014-04-21 20:39:56 +0200 | [diff] [blame] | 625 | /* popf; tell the caller to not touch TF */ |
Oleg Nesterov | 5cdb76d | 2014-06-01 21:13:46 +0200 | [diff] [blame] | 626 | if (auprobe->defparam.fixups & UPROBE_FIX_SETF) |
Oleg Nesterov | 220ef8d | 2014-04-21 20:39:56 +0200 | [diff] [blame] | 627 | utask->autask.saved_tf = true; |
Oleg Nesterov | 8ad8e9d | 2014-03-31 21:01:31 +0200 | [diff] [blame] | 628 | |
Oleg Nesterov | 75f9ef0 | 2014-04-03 20:52:19 +0200 | [diff] [blame] | 629 | return 0; |
Oleg Nesterov | 8ad8e9d | 2014-03-31 21:01:31 +0200 | [diff] [blame] | 630 | } |
| 631 | |
Oleg Nesterov | 588fbd6 | 2014-04-21 16:58:17 +0200 | [diff] [blame] | 632 | static void default_abort_op(struct arch_uprobe *auprobe, struct pt_regs *regs) |
| 633 | { |
Denys Vlasenko | 50204c6 | 2014-05-01 16:52:46 +0200 | [diff] [blame] | 634 | riprel_post_xol(auprobe, regs); |
Oleg Nesterov | 588fbd6 | 2014-04-21 16:58:17 +0200 | [diff] [blame] | 635 | } |
| 636 | |
Oleg Nesterov | 8ad8e9d | 2014-03-31 21:01:31 +0200 | [diff] [blame] | 637 | static struct uprobe_xol_ops default_xol_ops = { |
| 638 | .pre_xol = default_pre_xol_op, |
| 639 | .post_xol = default_post_xol_op, |
Oleg Nesterov | 588fbd6 | 2014-04-21 16:58:17 +0200 | [diff] [blame] | 640 | .abort = default_abort_op, |
Oleg Nesterov | 8ad8e9d | 2014-03-31 21:01:31 +0200 | [diff] [blame] | 641 | }; |
| 642 | |
Oleg Nesterov | 8e89c0b | 2014-04-06 18:11:02 +0200 | [diff] [blame] | 643 | static bool branch_is_call(struct arch_uprobe *auprobe) |
| 644 | { |
| 645 | return auprobe->branch.opc1 == 0xe8; |
| 646 | } |
| 647 | |
Oleg Nesterov | 8f95505 | 2014-04-06 21:53:47 +0200 | [diff] [blame] | 648 | #define CASE_COND \ |
| 649 | COND(70, 71, XF(OF)) \ |
| 650 | COND(72, 73, XF(CF)) \ |
| 651 | COND(74, 75, XF(ZF)) \ |
| 652 | COND(78, 79, XF(SF)) \ |
| 653 | COND(7a, 7b, XF(PF)) \ |
| 654 | COND(76, 77, XF(CF) || XF(ZF)) \ |
| 655 | COND(7c, 7d, XF(SF) != XF(OF)) \ |
| 656 | COND(7e, 7f, XF(ZF) || XF(SF) != XF(OF)) |
| 657 | |
| 658 | #define COND(op_y, op_n, expr) \ |
| 659 | case 0x ## op_y: DO((expr) != 0) \ |
| 660 | case 0x ## op_n: DO((expr) == 0) |
| 661 | |
| 662 | #define XF(xf) (!!(flags & X86_EFLAGS_ ## xf)) |
| 663 | |
| 664 | static bool is_cond_jmp_opcode(u8 opcode) |
| 665 | { |
| 666 | switch (opcode) { |
| 667 | #define DO(expr) \ |
| 668 | return true; |
| 669 | CASE_COND |
| 670 | #undef DO |
| 671 | |
| 672 | default: |
| 673 | return false; |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | static bool check_jmp_cond(struct arch_uprobe *auprobe, struct pt_regs *regs) |
| 678 | { |
| 679 | unsigned long flags = regs->flags; |
| 680 | |
| 681 | switch (auprobe->branch.opc1) { |
| 682 | #define DO(expr) \ |
| 683 | return expr; |
| 684 | CASE_COND |
| 685 | #undef DO |
| 686 | |
| 687 | default: /* not a conditional jmp */ |
| 688 | return true; |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | #undef XF |
| 693 | #undef COND |
| 694 | #undef CASE_COND |
| 695 | |
Oleg Nesterov | 7ba6db2 | 2014-04-05 20:05:02 +0200 | [diff] [blame] | 696 | static bool branch_emulate_op(struct arch_uprobe *auprobe, struct pt_regs *regs) |
| 697 | { |
Oleg Nesterov | 8e89c0b | 2014-04-06 18:11:02 +0200 | [diff] [blame] | 698 | unsigned long new_ip = regs->ip += auprobe->branch.ilen; |
Oleg Nesterov | 8f95505 | 2014-04-06 21:53:47 +0200 | [diff] [blame] | 699 | unsigned long offs = (long)auprobe->branch.offs; |
Oleg Nesterov | 8e89c0b | 2014-04-06 18:11:02 +0200 | [diff] [blame] | 700 | |
| 701 | if (branch_is_call(auprobe)) { |
Oleg Nesterov | 8e89c0b | 2014-04-06 18:11:02 +0200 | [diff] [blame] | 702 | /* |
| 703 | * If it fails we execute this (mangled, see the comment in |
| 704 | * branch_clear_offset) insn out-of-line. In the likely case |
| 705 | * this should trigger the trap, and the probed application |
| 706 | * should die or restart the same insn after it handles the |
| 707 | * signal, arch_uprobe_post_xol() won't be even called. |
| 708 | * |
| 709 | * But there is corner case, see the comment in ->post_xol(). |
| 710 | */ |
Oleg Nesterov | 2b82cad | 2014-04-24 19:21:38 +0200 | [diff] [blame] | 711 | if (push_ret_address(regs, new_ip)) |
Oleg Nesterov | 8e89c0b | 2014-04-06 18:11:02 +0200 | [diff] [blame] | 712 | return false; |
Oleg Nesterov | 8f95505 | 2014-04-06 21:53:47 +0200 | [diff] [blame] | 713 | } else if (!check_jmp_cond(auprobe, regs)) { |
| 714 | offs = 0; |
Oleg Nesterov | 8e89c0b | 2014-04-06 18:11:02 +0200 | [diff] [blame] | 715 | } |
| 716 | |
Oleg Nesterov | 8f95505 | 2014-04-06 21:53:47 +0200 | [diff] [blame] | 717 | regs->ip = new_ip + offs; |
Oleg Nesterov | 7ba6db2 | 2014-04-05 20:05:02 +0200 | [diff] [blame] | 718 | return true; |
| 719 | } |
| 720 | |
Oleg Nesterov | 8e89c0b | 2014-04-06 18:11:02 +0200 | [diff] [blame] | 721 | static int branch_post_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs) |
| 722 | { |
| 723 | BUG_ON(!branch_is_call(auprobe)); |
| 724 | /* |
| 725 | * We can only get here if branch_emulate_op() failed to push the ret |
| 726 | * address _and_ another thread expanded our stack before the (mangled) |
| 727 | * "call" insn was executed out-of-line. Just restore ->sp and restart. |
| 728 | * We could also restore ->ip and try to call branch_emulate_op() again. |
| 729 | */ |
| 730 | regs->sp += sizeof_long(); |
| 731 | return -ERESTART; |
| 732 | } |
| 733 | |
| 734 | static void branch_clear_offset(struct arch_uprobe *auprobe, struct insn *insn) |
| 735 | { |
| 736 | /* |
| 737 | * Turn this insn into "call 1f; 1:", this is what we will execute |
| 738 | * out-of-line if ->emulate() fails. We only need this to generate |
| 739 | * a trap, so that the probed task receives the correct signal with |
| 740 | * the properly filled siginfo. |
| 741 | * |
| 742 | * But see the comment in ->post_xol(), in the unlikely case it can |
| 743 | * succeed. So we need to ensure that the new ->ip can not fall into |
| 744 | * the non-canonical area and trigger #GP. |
| 745 | * |
| 746 | * We could turn it into (say) "pushf", but then we would need to |
| 747 | * divorce ->insn[] and ->ixol[]. We need to preserve the 1st byte |
| 748 | * of ->insn[] for set_orig_insn(). |
| 749 | */ |
| 750 | memset(auprobe->insn + insn_offset_immediate(insn), |
| 751 | 0, insn->immediate.nbytes); |
| 752 | } |
| 753 | |
Oleg Nesterov | 7ba6db2 | 2014-04-05 20:05:02 +0200 | [diff] [blame] | 754 | static struct uprobe_xol_ops branch_xol_ops = { |
| 755 | .emulate = branch_emulate_op, |
Oleg Nesterov | 8e89c0b | 2014-04-06 18:11:02 +0200 | [diff] [blame] | 756 | .post_xol = branch_post_xol_op, |
Oleg Nesterov | 7ba6db2 | 2014-04-05 20:05:02 +0200 | [diff] [blame] | 757 | }; |
| 758 | |
| 759 | /* Returns -ENOSYS if branch_xol_ops doesn't handle this insn */ |
| 760 | static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn) |
| 761 | { |
Oleg Nesterov | 8e89c0b | 2014-04-06 18:11:02 +0200 | [diff] [blame] | 762 | u8 opc1 = OPCODE1(insn); |
Denys Vlasenko | 250bbd1 | 2014-04-24 19:08:24 +0200 | [diff] [blame] | 763 | int i; |
Oleg Nesterov | 7ba6db2 | 2014-04-05 20:05:02 +0200 | [diff] [blame] | 764 | |
Oleg Nesterov | 8e89c0b | 2014-04-06 18:11:02 +0200 | [diff] [blame] | 765 | switch (opc1) { |
| 766 | case 0xeb: /* jmp 8 */ |
| 767 | case 0xe9: /* jmp 32 */ |
| 768 | case 0x90: /* prefix* + nop; same as jmp with .offs = 0 */ |
| 769 | break; |
| 770 | |
| 771 | case 0xe8: /* call relative */ |
| 772 | branch_clear_offset(auprobe, insn); |
| 773 | break; |
Oleg Nesterov | 8f95505 | 2014-04-06 21:53:47 +0200 | [diff] [blame] | 774 | |
Oleg Nesterov | 6cc5e7f | 2014-04-07 16:22:58 +0200 | [diff] [blame] | 775 | case 0x0f: |
| 776 | if (insn->opcode.nbytes != 2) |
| 777 | return -ENOSYS; |
| 778 | /* |
| 779 | * If it is a "near" conditional jmp, OPCODE2() - 0x10 matches |
| 780 | * OPCODE1() of the "short" jmp which checks the same condition. |
| 781 | */ |
| 782 | opc1 = OPCODE2(insn) - 0x10; |
Oleg Nesterov | 8e89c0b | 2014-04-06 18:11:02 +0200 | [diff] [blame] | 783 | default: |
Oleg Nesterov | 8f95505 | 2014-04-06 21:53:47 +0200 | [diff] [blame] | 784 | if (!is_cond_jmp_opcode(opc1)) |
| 785 | return -ENOSYS; |
Oleg Nesterov | 8e89c0b | 2014-04-06 18:11:02 +0200 | [diff] [blame] | 786 | } |
| 787 | |
Denys Vlasenko | 250bbd1 | 2014-04-24 19:08:24 +0200 | [diff] [blame] | 788 | /* |
| 789 | * 16-bit overrides such as CALLW (66 e8 nn nn) are not supported. |
| 790 | * Intel and AMD behavior differ in 64-bit mode: Intel ignores 66 prefix. |
| 791 | * No one uses these insns, reject any branch insns with such prefix. |
| 792 | */ |
| 793 | for (i = 0; i < insn->prefixes.nbytes; i++) { |
| 794 | if (insn->prefixes.bytes[i] == 0x66) |
| 795 | return -ENOTSUPP; |
| 796 | } |
| 797 | |
Oleg Nesterov | 8e89c0b | 2014-04-06 18:11:02 +0200 | [diff] [blame] | 798 | auprobe->branch.opc1 = opc1; |
Oleg Nesterov | 7ba6db2 | 2014-04-05 20:05:02 +0200 | [diff] [blame] | 799 | auprobe->branch.ilen = insn->length; |
| 800 | auprobe->branch.offs = insn->immediate.value; |
| 801 | |
| 802 | auprobe->ops = &branch_xol_ops; |
| 803 | return 0; |
| 804 | } |
| 805 | |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 806 | /** |
Srikar Dronamraju | 0326f5a | 2012-03-13 23:30:11 +0530 | [diff] [blame] | 807 | * arch_uprobe_analyze_insn - instruction analysis including validity and fixups. |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 808 | * @mm: the probed address space. |
Srikar Dronamraju | 3ff54ef | 2012-02-22 14:46:02 +0530 | [diff] [blame] | 809 | * @arch_uprobe: the probepoint information. |
Ananth N Mavinakayanahalli | 7eb9ba5 | 2012-06-08 15:02:57 +0530 | [diff] [blame] | 810 | * @addr: virtual address at which to install the probepoint |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 811 | * Return 0 on success or a -ve number on error. |
| 812 | */ |
Ananth N Mavinakayanahalli | 7eb9ba5 | 2012-06-08 15:02:57 +0530 | [diff] [blame] | 813 | int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long addr) |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 814 | { |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 815 | struct insn insn; |
Oleg Nesterov | 83cd591 | 2014-04-25 18:53:32 +0200 | [diff] [blame] | 816 | u8 fix_ip_or_call = UPROBE_FIX_IP; |
Oleg Nesterov | ddb69f2 | 2014-03-31 15:16:22 +0200 | [diff] [blame] | 817 | int ret; |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 818 | |
Oleg Nesterov | 2ae1f49 | 2014-04-19 14:03:05 +0200 | [diff] [blame] | 819 | ret = uprobe_init_insn(auprobe, &insn, is_64bit_mm(mm)); |
Oleg Nesterov | ddb69f2 | 2014-03-31 15:16:22 +0200 | [diff] [blame] | 820 | if (ret) |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 821 | return ret; |
Ingo Molnar | 7b2d81d | 2012-02-17 09:27:41 +0100 | [diff] [blame] | 822 | |
Oleg Nesterov | 7ba6db2 | 2014-04-05 20:05:02 +0200 | [diff] [blame] | 823 | ret = branch_setup_xol_ops(auprobe, &insn); |
| 824 | if (ret != -ENOSYS) |
| 825 | return ret; |
| 826 | |
Oleg Nesterov | ddb69f2 | 2014-03-31 15:16:22 +0200 | [diff] [blame] | 827 | /* |
Oleg Nesterov | 97aa5cd | 2014-04-22 16:20:55 +0200 | [diff] [blame] | 828 | * Figure out which fixups default_post_xol_op() will need to perform, |
Oleg Nesterov | 5cdb76d | 2014-06-01 21:13:46 +0200 | [diff] [blame] | 829 | * and annotate defparam->fixups accordingly. |
Oleg Nesterov | ddb69f2 | 2014-03-31 15:16:22 +0200 | [diff] [blame] | 830 | */ |
Oleg Nesterov | ddb69f2 | 2014-03-31 15:16:22 +0200 | [diff] [blame] | 831 | switch (OPCODE1(&insn)) { |
| 832 | case 0x9d: /* popf */ |
Oleg Nesterov | 5cdb76d | 2014-06-01 21:13:46 +0200 | [diff] [blame] | 833 | auprobe->defparam.fixups |= UPROBE_FIX_SETF; |
Oleg Nesterov | ddb69f2 | 2014-03-31 15:16:22 +0200 | [diff] [blame] | 834 | break; |
| 835 | case 0xc3: /* ret or lret -- ip is correct */ |
| 836 | case 0xcb: |
| 837 | case 0xc2: |
| 838 | case 0xca: |
Oleg Nesterov | 83cd591 | 2014-04-25 18:53:32 +0200 | [diff] [blame] | 839 | case 0xea: /* jmp absolute -- ip is correct */ |
| 840 | fix_ip_or_call = 0; |
Oleg Nesterov | ddb69f2 | 2014-03-31 15:16:22 +0200 | [diff] [blame] | 841 | break; |
Oleg Nesterov | ddb69f2 | 2014-03-31 15:16:22 +0200 | [diff] [blame] | 842 | case 0x9a: /* call absolute - Fix return addr, not ip */ |
Oleg Nesterov | 83cd591 | 2014-04-25 18:53:32 +0200 | [diff] [blame] | 843 | fix_ip_or_call = UPROBE_FIX_CALL; |
Oleg Nesterov | ddb69f2 | 2014-03-31 15:16:22 +0200 | [diff] [blame] | 844 | break; |
| 845 | case 0xff: |
Oleg Nesterov | ddb69f2 | 2014-03-31 15:16:22 +0200 | [diff] [blame] | 846 | switch (MODRM_REG(&insn)) { |
| 847 | case 2: case 3: /* call or lcall, indirect */ |
Oleg Nesterov | 83cd591 | 2014-04-25 18:53:32 +0200 | [diff] [blame] | 848 | fix_ip_or_call = UPROBE_FIX_CALL; |
| 849 | break; |
Oleg Nesterov | ddb69f2 | 2014-03-31 15:16:22 +0200 | [diff] [blame] | 850 | case 4: case 5: /* jmp or ljmp, indirect */ |
Oleg Nesterov | 83cd591 | 2014-04-25 18:53:32 +0200 | [diff] [blame] | 851 | fix_ip_or_call = 0; |
| 852 | break; |
Oleg Nesterov | ddb69f2 | 2014-03-31 15:16:22 +0200 | [diff] [blame] | 853 | } |
Oleg Nesterov | e55848a | 2014-03-31 17:24:14 +0200 | [diff] [blame] | 854 | /* fall through */ |
Oleg Nesterov | ddb69f2 | 2014-03-31 15:16:22 +0200 | [diff] [blame] | 855 | default: |
Oleg Nesterov | 1475ee7 | 2014-04-27 16:31:59 +0200 | [diff] [blame] | 856 | riprel_analyze(auprobe, &insn); |
Oleg Nesterov | ddb69f2 | 2014-03-31 15:16:22 +0200 | [diff] [blame] | 857 | } |
| 858 | |
Oleg Nesterov | 5cdb76d | 2014-06-01 21:13:46 +0200 | [diff] [blame] | 859 | auprobe->defparam.ilen = insn.length; |
| 860 | auprobe->defparam.fixups |= fix_ip_or_call; |
Ingo Molnar | 7b2d81d | 2012-02-17 09:27:41 +0100 | [diff] [blame] | 861 | |
Oleg Nesterov | 8ad8e9d | 2014-03-31 21:01:31 +0200 | [diff] [blame] | 862 | auprobe->ops = &default_xol_ops; |
Srikar Dronamraju | 2b14449 | 2012-02-09 14:56:42 +0530 | [diff] [blame] | 863 | return 0; |
| 864 | } |
Srikar Dronamraju | 0326f5a | 2012-03-13 23:30:11 +0530 | [diff] [blame] | 865 | |
Srikar Dronamraju | 0326f5a | 2012-03-13 23:30:11 +0530 | [diff] [blame] | 866 | /* |
| 867 | * arch_uprobe_pre_xol - prepare to execute out of line. |
| 868 | * @auprobe: the probepoint information. |
| 869 | * @regs: reflects the saved user state of current task. |
| 870 | */ |
| 871 | int arch_uprobe_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs) |
| 872 | { |
Oleg Nesterov | 34e7317 | 2014-03-31 19:38:09 +0200 | [diff] [blame] | 873 | struct uprobe_task *utask = current->utask; |
Srikar Dronamraju | 0326f5a | 2012-03-13 23:30:11 +0530 | [diff] [blame] | 874 | |
Oleg Nesterov | dd91016 | 2014-04-22 15:20:07 +0200 | [diff] [blame] | 875 | if (auprobe->ops->pre_xol) { |
| 876 | int err = auprobe->ops->pre_xol(auprobe, regs); |
| 877 | if (err) |
| 878 | return err; |
| 879 | } |
| 880 | |
Oleg Nesterov | 34e7317 | 2014-03-31 19:38:09 +0200 | [diff] [blame] | 881 | regs->ip = utask->xol_vaddr; |
| 882 | utask->autask.saved_trap_nr = current->thread.trap_nr; |
Srikar Dronamraju | 0326f5a | 2012-03-13 23:30:11 +0530 | [diff] [blame] | 883 | current->thread.trap_nr = UPROBE_TRAP_NR; |
Srikar Dronamraju | 0326f5a | 2012-03-13 23:30:11 +0530 | [diff] [blame] | 884 | |
Oleg Nesterov | 34e7317 | 2014-03-31 19:38:09 +0200 | [diff] [blame] | 885 | utask->autask.saved_tf = !!(regs->flags & X86_EFLAGS_TF); |
Oleg Nesterov | 4dc316c | 2012-10-28 17:57:30 +0100 | [diff] [blame] | 886 | regs->flags |= X86_EFLAGS_TF; |
| 887 | if (test_tsk_thread_flag(current, TIF_BLOCKSTEP)) |
| 888 | set_task_blockstep(current, false); |
| 889 | |
Srikar Dronamraju | 0326f5a | 2012-03-13 23:30:11 +0530 | [diff] [blame] | 890 | return 0; |
| 891 | } |
| 892 | |
Srikar Dronamraju | 0326f5a | 2012-03-13 23:30:11 +0530 | [diff] [blame] | 893 | /* |
| 894 | * If xol insn itself traps and generates a signal(Say, |
| 895 | * SIGILL/SIGSEGV/etc), then detect the case where a singlestepped |
| 896 | * instruction jumps back to its own address. It is assumed that anything |
| 897 | * like do_page_fault/do_trap/etc sets thread.trap_nr != -1. |
| 898 | * |
| 899 | * arch_uprobe_pre_xol/arch_uprobe_post_xol save/restore thread.trap_nr, |
| 900 | * arch_uprobe_xol_was_trapped() simply checks that ->trap_nr is not equal to |
| 901 | * UPROBE_TRAP_NR == -1 set by arch_uprobe_pre_xol(). |
| 902 | */ |
| 903 | bool arch_uprobe_xol_was_trapped(struct task_struct *t) |
| 904 | { |
| 905 | if (t->thread.trap_nr != UPROBE_TRAP_NR) |
| 906 | return true; |
| 907 | |
| 908 | return false; |
| 909 | } |
| 910 | |
| 911 | /* |
| 912 | * Called after single-stepping. To avoid the SMP problems that can |
| 913 | * occur when we temporarily put back the original opcode to |
| 914 | * single-step, we single-stepped a copy of the instruction. |
| 915 | * |
| 916 | * This function prepares to resume execution after the single-step. |
Srikar Dronamraju | 0326f5a | 2012-03-13 23:30:11 +0530 | [diff] [blame] | 917 | */ |
| 918 | int arch_uprobe_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs) |
| 919 | { |
Oleg Nesterov | 34e7317 | 2014-03-31 19:38:09 +0200 | [diff] [blame] | 920 | struct uprobe_task *utask = current->utask; |
Oleg Nesterov | 220ef8d | 2014-04-21 20:39:56 +0200 | [diff] [blame] | 921 | bool send_sigtrap = utask->autask.saved_tf; |
| 922 | int err = 0; |
Srikar Dronamraju | 0326f5a | 2012-03-13 23:30:11 +0530 | [diff] [blame] | 923 | |
| 924 | WARN_ON_ONCE(current->thread.trap_nr != UPROBE_TRAP_NR); |
Oleg Nesterov | 6ded5f3 | 2014-04-21 18:28:02 +0200 | [diff] [blame] | 925 | current->thread.trap_nr = utask->autask.saved_trap_nr; |
Oleg Nesterov | 014940b | 2014-04-03 20:20:10 +0200 | [diff] [blame] | 926 | |
| 927 | if (auprobe->ops->post_xol) { |
Oleg Nesterov | 220ef8d | 2014-04-21 20:39:56 +0200 | [diff] [blame] | 928 | err = auprobe->ops->post_xol(auprobe, regs); |
Oleg Nesterov | 014940b | 2014-04-03 20:20:10 +0200 | [diff] [blame] | 929 | if (err) { |
Oleg Nesterov | 75f9ef0 | 2014-04-03 20:52:19 +0200 | [diff] [blame] | 930 | /* |
Oleg Nesterov | 6ded5f3 | 2014-04-21 18:28:02 +0200 | [diff] [blame] | 931 | * Restore ->ip for restart or post mortem analysis. |
| 932 | * ->post_xol() must not return -ERESTART unless this |
| 933 | * is really possible. |
Oleg Nesterov | 75f9ef0 | 2014-04-03 20:52:19 +0200 | [diff] [blame] | 934 | */ |
Oleg Nesterov | 6ded5f3 | 2014-04-21 18:28:02 +0200 | [diff] [blame] | 935 | regs->ip = utask->vaddr; |
Oleg Nesterov | 75f9ef0 | 2014-04-03 20:52:19 +0200 | [diff] [blame] | 936 | if (err == -ERESTART) |
Oleg Nesterov | 220ef8d | 2014-04-21 20:39:56 +0200 | [diff] [blame] | 937 | err = 0; |
| 938 | send_sigtrap = false; |
Oleg Nesterov | 014940b | 2014-04-03 20:20:10 +0200 | [diff] [blame] | 939 | } |
| 940 | } |
Oleg Nesterov | 4dc316c | 2012-10-28 17:57:30 +0100 | [diff] [blame] | 941 | /* |
| 942 | * arch_uprobe_pre_xol() doesn't save the state of TIF_BLOCKSTEP |
| 943 | * so we can get an extra SIGTRAP if we do not clear TF. We need |
| 944 | * to examine the opcode to make it right. |
| 945 | */ |
Oleg Nesterov | 220ef8d | 2014-04-21 20:39:56 +0200 | [diff] [blame] | 946 | if (send_sigtrap) |
Oleg Nesterov | 4dc316c | 2012-10-28 17:57:30 +0100 | [diff] [blame] | 947 | send_sig(SIGTRAP, current, 0); |
Oleg Nesterov | 220ef8d | 2014-04-21 20:39:56 +0200 | [diff] [blame] | 948 | |
| 949 | if (!utask->autask.saved_tf) |
Oleg Nesterov | 4dc316c | 2012-10-28 17:57:30 +0100 | [diff] [blame] | 950 | regs->flags &= ~X86_EFLAGS_TF; |
| 951 | |
Oleg Nesterov | 220ef8d | 2014-04-21 20:39:56 +0200 | [diff] [blame] | 952 | return err; |
Srikar Dronamraju | 0326f5a | 2012-03-13 23:30:11 +0530 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | /* callback routine for handling exceptions. */ |
| 956 | int arch_uprobe_exception_notify(struct notifier_block *self, unsigned long val, void *data) |
| 957 | { |
| 958 | struct die_args *args = data; |
| 959 | struct pt_regs *regs = args->regs; |
| 960 | int ret = NOTIFY_DONE; |
| 961 | |
| 962 | /* We are only interested in userspace traps */ |
| 963 | if (regs && !user_mode_vm(regs)) |
| 964 | return NOTIFY_DONE; |
| 965 | |
| 966 | switch (val) { |
| 967 | case DIE_INT3: |
| 968 | if (uprobe_pre_sstep_notifier(regs)) |
| 969 | ret = NOTIFY_STOP; |
| 970 | |
| 971 | break; |
| 972 | |
| 973 | case DIE_DEBUG: |
| 974 | if (uprobe_post_sstep_notifier(regs)) |
| 975 | ret = NOTIFY_STOP; |
| 976 | |
| 977 | default: |
| 978 | break; |
| 979 | } |
| 980 | |
| 981 | return ret; |
| 982 | } |
| 983 | |
| 984 | /* |
| 985 | * This function gets called when XOL instruction either gets trapped or |
Oleg Nesterov | 6ded5f3 | 2014-04-21 18:28:02 +0200 | [diff] [blame] | 986 | * the thread has a fatal signal. Reset the instruction pointer to its |
| 987 | * probed address for the potential restart or for post mortem analysis. |
Srikar Dronamraju | 0326f5a | 2012-03-13 23:30:11 +0530 | [diff] [blame] | 988 | */ |
| 989 | void arch_uprobe_abort_xol(struct arch_uprobe *auprobe, struct pt_regs *regs) |
| 990 | { |
| 991 | struct uprobe_task *utask = current->utask; |
| 992 | |
Oleg Nesterov | 588fbd6 | 2014-04-21 16:58:17 +0200 | [diff] [blame] | 993 | if (auprobe->ops->abort) |
| 994 | auprobe->ops->abort(auprobe, regs); |
Oleg Nesterov | 4dc316c | 2012-10-28 17:57:30 +0100 | [diff] [blame] | 995 | |
Oleg Nesterov | 588fbd6 | 2014-04-21 16:58:17 +0200 | [diff] [blame] | 996 | current->thread.trap_nr = utask->autask.saved_trap_nr; |
| 997 | regs->ip = utask->vaddr; |
Oleg Nesterov | 4dc316c | 2012-10-28 17:57:30 +0100 | [diff] [blame] | 998 | /* clear TF if it was set by us in arch_uprobe_pre_xol() */ |
| 999 | if (!utask->autask.saved_tf) |
| 1000 | regs->flags &= ~X86_EFLAGS_TF; |
Srikar Dronamraju | 0326f5a | 2012-03-13 23:30:11 +0530 | [diff] [blame] | 1001 | } |
| 1002 | |
Oleg Nesterov | 3a4664a | 2012-09-03 16:05:10 +0200 | [diff] [blame] | 1003 | static bool __skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs) |
Srikar Dronamraju | 0326f5a | 2012-03-13 23:30:11 +0530 | [diff] [blame] | 1004 | { |
Oleg Nesterov | 8ad8e9d | 2014-03-31 21:01:31 +0200 | [diff] [blame] | 1005 | if (auprobe->ops->emulate) |
| 1006 | return auprobe->ops->emulate(auprobe, regs); |
Srikar Dronamraju | 0326f5a | 2012-03-13 23:30:11 +0530 | [diff] [blame] | 1007 | return false; |
| 1008 | } |
Sebastian Andrzej Siewior | bdc1e47 | 2012-08-20 12:47:34 +0200 | [diff] [blame] | 1009 | |
Oleg Nesterov | 3a4664a | 2012-09-03 16:05:10 +0200 | [diff] [blame] | 1010 | bool arch_uprobe_skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs) |
| 1011 | { |
| 1012 | bool ret = __skip_sstep(auprobe, regs); |
| 1013 | if (ret && (regs->flags & X86_EFLAGS_TF)) |
| 1014 | send_sig(SIGTRAP, current, 0); |
| 1015 | return ret; |
| 1016 | } |
Anton Arapov | 791eca1 | 2013-04-03 18:00:33 +0200 | [diff] [blame] | 1017 | |
| 1018 | unsigned long |
| 1019 | arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, struct pt_regs *regs) |
| 1020 | { |
Oleg Nesterov | 8faaed1 | 2014-04-06 17:16:10 +0200 | [diff] [blame] | 1021 | int rasize = sizeof_long(), nleft; |
Anton Arapov | 791eca1 | 2013-04-03 18:00:33 +0200 | [diff] [blame] | 1022 | unsigned long orig_ret_vaddr = 0; /* clear high bits for 32-bit apps */ |
| 1023 | |
Oleg Nesterov | 8faaed1 | 2014-04-06 17:16:10 +0200 | [diff] [blame] | 1024 | if (copy_from_user(&orig_ret_vaddr, (void __user *)regs->sp, rasize)) |
Anton Arapov | 791eca1 | 2013-04-03 18:00:33 +0200 | [diff] [blame] | 1025 | return -1; |
| 1026 | |
| 1027 | /* check whether address has been already hijacked */ |
| 1028 | if (orig_ret_vaddr == trampoline_vaddr) |
| 1029 | return orig_ret_vaddr; |
| 1030 | |
Oleg Nesterov | 8faaed1 | 2014-04-06 17:16:10 +0200 | [diff] [blame] | 1031 | nleft = copy_to_user((void __user *)regs->sp, &trampoline_vaddr, rasize); |
| 1032 | if (likely(!nleft)) |
Anton Arapov | 791eca1 | 2013-04-03 18:00:33 +0200 | [diff] [blame] | 1033 | return orig_ret_vaddr; |
| 1034 | |
Oleg Nesterov | 8faaed1 | 2014-04-06 17:16:10 +0200 | [diff] [blame] | 1035 | if (nleft != rasize) { |
Anton Arapov | 791eca1 | 2013-04-03 18:00:33 +0200 | [diff] [blame] | 1036 | pr_err("uprobe: return address clobbered: pid=%d, %%sp=%#lx, " |
| 1037 | "%%ip=%#lx\n", current->pid, regs->sp, regs->ip); |
| 1038 | |
| 1039 | force_sig_info(SIGSEGV, SEND_SIG_FORCED, current); |
| 1040 | } |
| 1041 | |
| 1042 | return -1; |
| 1043 | } |