11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
22b144498SSrikar Dronamraju /*
37b2d81d4SIngo Molnar * User-space Probes (UProbes) for x86
42b144498SSrikar Dronamraju *
52b144498SSrikar Dronamraju * Copyright (C) IBM Corporation, 2008-2011
62b144498SSrikar Dronamraju * Authors:
72b144498SSrikar Dronamraju * Srikar Dronamraju
82b144498SSrikar Dronamraju * Jim Keniston
92b144498SSrikar Dronamraju */
102b144498SSrikar Dronamraju #include <linux/kernel.h>
112b144498SSrikar Dronamraju #include <linux/sched.h>
122b144498SSrikar Dronamraju #include <linux/ptrace.h>
132b144498SSrikar Dronamraju #include <linux/uprobes.h>
140326f5a9SSrikar Dronamraju #include <linux/uaccess.h>
152b144498SSrikar Dronamraju
162b144498SSrikar Dronamraju #include <linux/kdebug.h>
170326f5a9SSrikar Dronamraju #include <asm/processor.h>
182b144498SSrikar Dronamraju #include <asm/insn.h>
19b0e9b09bSDave Hansen #include <asm/mmu_context.h>
202b144498SSrikar Dronamraju
212b144498SSrikar Dronamraju /* Post-execution fixups. */
222b144498SSrikar Dronamraju
232b144498SSrikar Dronamraju /* Adjust IP back to vicinity of actual insn */
2478d9af4cSOleg Nesterov #define UPROBE_FIX_IP 0x01
250326f5a9SSrikar Dronamraju
262b144498SSrikar Dronamraju /* Adjust the return address of a call insn */
2778d9af4cSOleg Nesterov #define UPROBE_FIX_CALL 0x02
282b144498SSrikar Dronamraju
29bdc1e472SSebastian Andrzej Siewior /* Instruction will modify TF, don't change it */
3078d9af4cSOleg Nesterov #define UPROBE_FIX_SETF 0x04
31bdc1e472SSebastian Andrzej Siewior
321ea30fb6SDenys Vlasenko #define UPROBE_FIX_RIP_SI 0x08
331ea30fb6SDenys Vlasenko #define UPROBE_FIX_RIP_DI 0x10
341ea30fb6SDenys Vlasenko #define UPROBE_FIX_RIP_BX 0x20
351ea30fb6SDenys Vlasenko #define UPROBE_FIX_RIP_MASK \
361ea30fb6SDenys Vlasenko (UPROBE_FIX_RIP_SI | UPROBE_FIX_RIP_DI | UPROBE_FIX_RIP_BX)
372b144498SSrikar Dronamraju
380326f5a9SSrikar Dronamraju #define UPROBE_TRAP_NR UINT_MAX
390326f5a9SSrikar Dronamraju
402b144498SSrikar Dronamraju /* Adaptations for mhiramat x86 decoder v14. */
412b144498SSrikar Dronamraju #define OPCODE1(insn) ((insn)->opcode.bytes[0])
422b144498SSrikar Dronamraju #define OPCODE2(insn) ((insn)->opcode.bytes[1])
432b144498SSrikar Dronamraju #define OPCODE3(insn) ((insn)->opcode.bytes[2])
44ddb69f27SOleg Nesterov #define MODRM_REG(insn) X86_MODRM_REG((insn)->modrm.value)
452b144498SSrikar Dronamraju
462b144498SSrikar Dronamraju #define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\
472b144498SSrikar Dronamraju (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
482b144498SSrikar Dronamraju (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
492b144498SSrikar Dronamraju (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
502b144498SSrikar Dronamraju (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
512b144498SSrikar Dronamraju << (row % 32))
522b144498SSrikar Dronamraju
5304a3d984SSrikar Dronamraju /*
5404a3d984SSrikar Dronamraju * Good-instruction tables for 32-bit apps. This is non-const and volatile
5504a3d984SSrikar Dronamraju * to keep gcc from statically optimizing it out, as variable_test_bit makes
5604a3d984SSrikar Dronamraju * some versions of gcc to think only *(unsigned long*) is used.
57097f4e5eSDenys Vlasenko *
58097f4e5eSDenys Vlasenko * Opcodes we'll probably never support:
59097f4e5eSDenys Vlasenko * 6c-6f - ins,outs. SEGVs if used in userspace
60097f4e5eSDenys Vlasenko * e4-e7 - in,out imm. SEGVs if used in userspace
61097f4e5eSDenys Vlasenko * ec-ef - in,out acc. SEGVs if used in userspace
62097f4e5eSDenys Vlasenko * cc - int3. SIGTRAP if used in userspace
63097f4e5eSDenys Vlasenko * ce - into. Not used in userspace - no kernel support to make it useful. SEGVs
64097f4e5eSDenys Vlasenko * (why we support bound (62) then? it's similar, and similarly unused...)
65097f4e5eSDenys Vlasenko * f1 - int1. SIGTRAP if used in userspace
66097f4e5eSDenys Vlasenko * f4 - hlt. SEGVs if used in userspace
67097f4e5eSDenys Vlasenko * fa - cli. SEGVs if used in userspace
68097f4e5eSDenys Vlasenko * fb - sti. SEGVs if used in userspace
69097f4e5eSDenys Vlasenko *
70097f4e5eSDenys Vlasenko * Opcodes which need some work to be supported:
71097f4e5eSDenys Vlasenko * 07,17,1f - pop es/ss/ds
72097f4e5eSDenys Vlasenko * Normally not used in userspace, but would execute if used.
73097f4e5eSDenys Vlasenko * Can cause GP or stack exception if tries to load wrong segment descriptor.
74097f4e5eSDenys Vlasenko * We hesitate to run them under single step since kernel's handling
75097f4e5eSDenys Vlasenko * of userspace single-stepping (TF flag) is fragile.
76097f4e5eSDenys Vlasenko * We can easily refuse to support push es/cs/ss/ds (06/0e/16/1e)
77097f4e5eSDenys Vlasenko * on the same grounds that they are never used.
78097f4e5eSDenys Vlasenko * cd - int N.
79097f4e5eSDenys Vlasenko * Used by userspace for "int 80" syscall entry. (Other "int N"
80097f4e5eSDenys Vlasenko * cause GP -> SEGV since their IDT gates don't allow calls from CPL 3).
81097f4e5eSDenys Vlasenko * Not supported since kernel's handling of userspace single-stepping
82097f4e5eSDenys Vlasenko * (TF flag) is fragile.
83097f4e5eSDenys Vlasenko * cf - iret. Normally not used in userspace. Doesn't SEGV unless arguments are bad
8404a3d984SSrikar Dronamraju */
858dbacad9SOleg Nesterov #if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
8604a3d984SSrikar Dronamraju static volatile u32 good_insns_32[256 / 32] = {
872b144498SSrikar Dronamraju /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
882b144498SSrikar Dronamraju /* ---------------------------------------------- */
8967fc8092SDenys Vlasenko W(0x00, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* 00 */
902b144498SSrikar Dronamraju W(0x10, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0) , /* 10 */
9167fc8092SDenys Vlasenko W(0x20, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 20 */
9267fc8092SDenys Vlasenko W(0x30, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 30 */
932b144498SSrikar Dronamraju W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
942b144498SSrikar Dronamraju W(0x50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 50 */
9567fc8092SDenys Vlasenko W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0) | /* 60 */
962b144498SSrikar Dronamraju W(0x70, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 70 */
972b144498SSrikar Dronamraju W(0x80, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 80 */
982b144498SSrikar Dronamraju W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
992b144498SSrikar Dronamraju W(0xa0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* a0 */
1002b144498SSrikar Dronamraju W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* b0 */
1012b144498SSrikar Dronamraju W(0xc0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0) | /* c0 */
10267fc8092SDenys Vlasenko W(0xd0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* d0 */
1032b144498SSrikar Dronamraju W(0xe0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* e0 */
10467fc8092SDenys Vlasenko W(0xf0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1) /* f0 */
1052b144498SSrikar Dronamraju /* ---------------------------------------------- */
1062b144498SSrikar Dronamraju /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
1072b144498SSrikar Dronamraju };
1088dbacad9SOleg Nesterov #else
1098dbacad9SOleg Nesterov #define good_insns_32 NULL
1108dbacad9SOleg Nesterov #endif
1118dbacad9SOleg Nesterov
112097f4e5eSDenys Vlasenko /* Good-instruction tables for 64-bit apps.
113097f4e5eSDenys Vlasenko *
114097f4e5eSDenys Vlasenko * Genuinely invalid opcodes:
115097f4e5eSDenys Vlasenko * 06,07 - formerly push/pop es
116097f4e5eSDenys Vlasenko * 0e - formerly push cs
117097f4e5eSDenys Vlasenko * 16,17 - formerly push/pop ss
118097f4e5eSDenys Vlasenko * 1e,1f - formerly push/pop ds
119097f4e5eSDenys Vlasenko * 27,2f,37,3f - formerly daa/das/aaa/aas
120097f4e5eSDenys Vlasenko * 60,61 - formerly pusha/popa
12167fc8092SDenys Vlasenko * 62 - formerly bound. EVEX prefix for AVX512 (not yet supported)
122097f4e5eSDenys Vlasenko * 82 - formerly redundant encoding of Group1
12367fc8092SDenys Vlasenko * 9a - formerly call seg:ofs
124097f4e5eSDenys Vlasenko * ce - formerly into
125097f4e5eSDenys Vlasenko * d4,d5 - formerly aam/aad
126097f4e5eSDenys Vlasenko * d6 - formerly undocumented salc
12767fc8092SDenys Vlasenko * ea - formerly jmp seg:ofs
128097f4e5eSDenys Vlasenko *
129097f4e5eSDenys Vlasenko * Opcodes we'll probably never support:
130097f4e5eSDenys Vlasenko * 6c-6f - ins,outs. SEGVs if used in userspace
131097f4e5eSDenys Vlasenko * e4-e7 - in,out imm. SEGVs if used in userspace
132097f4e5eSDenys Vlasenko * ec-ef - in,out acc. SEGVs if used in userspace
133097f4e5eSDenys Vlasenko * cc - int3. SIGTRAP if used in userspace
134097f4e5eSDenys Vlasenko * f1 - int1. SIGTRAP if used in userspace
135097f4e5eSDenys Vlasenko * f4 - hlt. SEGVs if used in userspace
136097f4e5eSDenys Vlasenko * fa - cli. SEGVs if used in userspace
137097f4e5eSDenys Vlasenko * fb - sti. SEGVs if used in userspace
138097f4e5eSDenys Vlasenko *
139097f4e5eSDenys Vlasenko * Opcodes which need some work to be supported:
140097f4e5eSDenys Vlasenko * cd - int N.
141097f4e5eSDenys Vlasenko * Used by userspace for "int 80" syscall entry. (Other "int N"
142097f4e5eSDenys Vlasenko * cause GP -> SEGV since their IDT gates don't allow calls from CPL 3).
143097f4e5eSDenys Vlasenko * Not supported since kernel's handling of userspace single-stepping
144097f4e5eSDenys Vlasenko * (TF flag) is fragile.
145097f4e5eSDenys Vlasenko * cf - iret. Normally not used in userspace. Doesn't SEGV unless arguments are bad
146097f4e5eSDenys Vlasenko */
1478dbacad9SOleg Nesterov #if defined(CONFIG_X86_64)
1488dbacad9SOleg Nesterov static volatile u32 good_insns_64[256 / 32] = {
1498dbacad9SOleg Nesterov /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
1508dbacad9SOleg Nesterov /* ---------------------------------------------- */
15167fc8092SDenys Vlasenko W(0x00, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1) | /* 00 */
1528dbacad9SOleg Nesterov W(0x10, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0) , /* 10 */
15367fc8092SDenys Vlasenko W(0x20, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0) | /* 20 */
15467fc8092SDenys Vlasenko W(0x30, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0) , /* 30 */
15567fc8092SDenys Vlasenko W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
1568dbacad9SOleg Nesterov W(0x50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 50 */
15767fc8092SDenys Vlasenko W(0x60, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0) | /* 60 */
1588dbacad9SOleg Nesterov W(0x70, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 70 */
1598dbacad9SOleg Nesterov W(0x80, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 80 */
16067fc8092SDenys Vlasenko W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1) , /* 90 */
1618dbacad9SOleg Nesterov W(0xa0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* a0 */
1628dbacad9SOleg Nesterov W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* b0 */
16367fc8092SDenys Vlasenko W(0xc0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0) | /* c0 */
1648dbacad9SOleg Nesterov W(0xd0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* d0 */
16567fc8092SDenys Vlasenko W(0xe0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0) | /* e0 */
16667fc8092SDenys Vlasenko W(0xf0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1) /* f0 */
1678dbacad9SOleg Nesterov /* ---------------------------------------------- */
1688dbacad9SOleg Nesterov /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
1698dbacad9SOleg Nesterov };
1708dbacad9SOleg Nesterov #else
1718dbacad9SOleg Nesterov #define good_insns_64 NULL
1728dbacad9SOleg Nesterov #endif
1732b144498SSrikar Dronamraju
174097f4e5eSDenys Vlasenko /* Using this for both 64-bit and 32-bit apps.
175097f4e5eSDenys Vlasenko * Opcodes we don't support:
176097f4e5eSDenys Vlasenko * 0f 00 - SLDT/STR/LLDT/LTR/VERR/VERW/-/- group. System insns
177097f4e5eSDenys Vlasenko * 0f 01 - SGDT/SIDT/LGDT/LIDT/SMSW/-/LMSW/INVLPG group.
178097f4e5eSDenys Vlasenko * Also encodes tons of other system insns if mod=11.
179097f4e5eSDenys Vlasenko * Some are in fact non-system: xend, xtest, rdtscp, maybe more
180097f4e5eSDenys Vlasenko * 0f 05 - syscall
181097f4e5eSDenys Vlasenko * 0f 06 - clts (CPL0 insn)
182097f4e5eSDenys Vlasenko * 0f 07 - sysret
183097f4e5eSDenys Vlasenko * 0f 08 - invd (CPL0 insn)
184097f4e5eSDenys Vlasenko * 0f 09 - wbinvd (CPL0 insn)
185097f4e5eSDenys Vlasenko * 0f 0b - ud2
1865154d4f2SDenys Vlasenko * 0f 30 - wrmsr (CPL0 insn) (then why rdmsr is allowed, it's also CPL0 insn?)
187097f4e5eSDenys Vlasenko * 0f 34 - sysenter
188097f4e5eSDenys Vlasenko * 0f 35 - sysexit
189097f4e5eSDenys Vlasenko * 0f 37 - getsec
1905154d4f2SDenys Vlasenko * 0f 78 - vmread (Intel VMX. CPL0 insn)
1915154d4f2SDenys Vlasenko * 0f 79 - vmwrite (Intel VMX. CPL0 insn)
1925154d4f2SDenys Vlasenko * Note: with prefixes, these two opcodes are
1935154d4f2SDenys Vlasenko * extrq/insertq/AVX512 convert vector ops.
1945154d4f2SDenys Vlasenko * 0f ae - group15: [f]xsave,[f]xrstor,[v]{ld,st}mxcsr,clflush[opt],
1955154d4f2SDenys Vlasenko * {rd,wr}{fs,gs}base,{s,l,m}fence.
1965154d4f2SDenys Vlasenko * Why? They are all user-executable.
197097f4e5eSDenys Vlasenko */
19804a3d984SSrikar Dronamraju static volatile u32 good_2byte_insns[256 / 32] = {
1992b144498SSrikar Dronamraju /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
2002b144498SSrikar Dronamraju /* ---------------------------------------------- */
2015154d4f2SDenys Vlasenko W(0x00, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1) | /* 00 */
2025154d4f2SDenys Vlasenko W(0x10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 10 */
2035154d4f2SDenys Vlasenko W(0x20, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 20 */
2045154d4f2SDenys Vlasenko W(0x30, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1) , /* 30 */
2052b144498SSrikar Dronamraju W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
2062b144498SSrikar Dronamraju W(0x50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 50 */
2072b144498SSrikar Dronamraju W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 60 */
2085154d4f2SDenys Vlasenko W(0x70, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1) , /* 70 */
2092b144498SSrikar Dronamraju W(0x80, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 80 */
2102b144498SSrikar Dronamraju W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
2115154d4f2SDenys Vlasenko W(0xa0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1) | /* a0 */
2125154d4f2SDenys Vlasenko W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* b0 */
2132b144498SSrikar Dronamraju W(0xc0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */
2145154d4f2SDenys Vlasenko W(0xd0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* d0 */
2152b144498SSrikar Dronamraju W(0xe0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* e0 */
2165154d4f2SDenys Vlasenko W(0xf0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) /* f0 */
2172b144498SSrikar Dronamraju /* ---------------------------------------------- */
2182b144498SSrikar Dronamraju /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
2192b144498SSrikar Dronamraju };
2202b144498SSrikar Dronamraju #undef W
2212b144498SSrikar Dronamraju
2222b144498SSrikar Dronamraju /*
2232b144498SSrikar Dronamraju * opcodes we may need to refine support for:
2247b2d81d4SIngo Molnar *
2252b144498SSrikar Dronamraju * 0f - 2-byte instructions: For many of these instructions, the validity
2262b144498SSrikar Dronamraju * depends on the prefix and/or the reg field. On such instructions, we
2272b144498SSrikar Dronamraju * just consider the opcode combination valid if it corresponds to any
2282b144498SSrikar Dronamraju * valid instruction.
2297b2d81d4SIngo Molnar *
2302b144498SSrikar Dronamraju * 8f - Group 1 - only reg = 0 is OK
2312b144498SSrikar Dronamraju * c6-c7 - Group 11 - only reg = 0 is OK
2322b144498SSrikar Dronamraju * d9-df - fpu insns with some illegal encodings
2332b144498SSrikar Dronamraju * f2, f3 - repnz, repz prefixes. These are also the first byte for
2342b144498SSrikar Dronamraju * certain floating-point instructions, such as addsd.
2357b2d81d4SIngo Molnar *
2362b144498SSrikar Dronamraju * fe - Group 4 - only reg = 0 or 1 is OK
2372b144498SSrikar Dronamraju * ff - Group 5 - only reg = 0-6 is OK
2382b144498SSrikar Dronamraju *
2392b144498SSrikar Dronamraju * others -- Do we need to support these?
2407b2d81d4SIngo Molnar *
2412b144498SSrikar Dronamraju * 0f - (floating-point?) prefetch instructions
2422b144498SSrikar Dronamraju * 07, 17, 1f - pop es, pop ss, pop ds
2432b144498SSrikar Dronamraju * 26, 2e, 36, 3e - es:, cs:, ss:, ds: segment prefixes --
2442b144498SSrikar Dronamraju * but 64 and 65 (fs: and gs:) seem to be used, so we support them
2452b144498SSrikar Dronamraju * 67 - addr16 prefix
2462b144498SSrikar Dronamraju * ce - into
2472b144498SSrikar Dronamraju * f0 - lock prefix
2482b144498SSrikar Dronamraju */
2492b144498SSrikar Dronamraju
2502b144498SSrikar Dronamraju /*
2512b144498SSrikar Dronamraju * TODO:
2522b144498SSrikar Dronamraju * - Where necessary, examine the modrm byte and allow only valid instructions
2532b144498SSrikar Dronamraju * in the different Groups and fpu instructions.
2542b144498SSrikar Dronamraju */
2552b144498SSrikar Dronamraju
is_prefix_bad(struct insn * insn)2562b144498SSrikar Dronamraju static bool is_prefix_bad(struct insn *insn)
2572b144498SSrikar Dronamraju {
2584e9a5ae8SMasami Hiramatsu insn_byte_t p;
2592b144498SSrikar Dronamraju int i;
2602b144498SSrikar Dronamraju
2614e9a5ae8SMasami Hiramatsu for_each_insn_prefix(insn, i, p) {
262ed40a104SRicardo Neri insn_attr_t attr;
263ed40a104SRicardo Neri
2644e9a5ae8SMasami Hiramatsu attr = inat_get_opcode_attribute(p);
265ed40a104SRicardo Neri switch (attr) {
266ed40a104SRicardo Neri case INAT_MAKE_PREFIX(INAT_PFX_ES):
267ed40a104SRicardo Neri case INAT_MAKE_PREFIX(INAT_PFX_CS):
268ed40a104SRicardo Neri case INAT_MAKE_PREFIX(INAT_PFX_DS):
269ed40a104SRicardo Neri case INAT_MAKE_PREFIX(INAT_PFX_SS):
270ed40a104SRicardo Neri case INAT_MAKE_PREFIX(INAT_PFX_LOCK):
2712b144498SSrikar Dronamraju return true;
2722b144498SSrikar Dronamraju }
2732b144498SSrikar Dronamraju }
2742b144498SSrikar Dronamraju return false;
2752b144498SSrikar Dronamraju }
2762b144498SSrikar Dronamraju
uprobe_init_insn(struct arch_uprobe * auprobe,struct insn * insn,bool x86_64)27773175d0dSOleg Nesterov static int uprobe_init_insn(struct arch_uprobe *auprobe, struct insn *insn, bool x86_64)
2782b144498SSrikar Dronamraju {
27988afc239SBorislav Petkov enum insn_mode m = x86_64 ? INSN_MODE_64 : INSN_MODE_32;
28073175d0dSOleg Nesterov u32 volatile *good_insns;
28188afc239SBorislav Petkov int ret;
2822b144498SSrikar Dronamraju
28388afc239SBorislav Petkov ret = insn_decode(insn, auprobe->insn, sizeof(auprobe->insn), m);
28488afc239SBorislav Petkov if (ret < 0)
285ff261964SOleg Nesterov return -ENOEXEC;
28673175d0dSOleg Nesterov
2872b144498SSrikar Dronamraju if (is_prefix_bad(insn))
2882b144498SSrikar Dronamraju return -ENOTSUPP;
2897b2d81d4SIngo Molnar
29013ebe18cSMasami Hiramatsu /* We should not singlestep on the exception masking instructions */
29113ebe18cSMasami Hiramatsu if (insn_masking_exception(insn))
29213ebe18cSMasami Hiramatsu return -ENOTSUPP;
29313ebe18cSMasami Hiramatsu
29473175d0dSOleg Nesterov if (x86_64)
29573175d0dSOleg Nesterov good_insns = good_insns_64;
29673175d0dSOleg Nesterov else
29773175d0dSOleg Nesterov good_insns = good_insns_32;
29873175d0dSOleg Nesterov
29973175d0dSOleg Nesterov if (test_bit(OPCODE1(insn), (unsigned long *)good_insns))
3002b144498SSrikar Dronamraju return 0;
3017b2d81d4SIngo Molnar
3022b144498SSrikar Dronamraju if (insn->opcode.nbytes == 2) {
3032b144498SSrikar Dronamraju if (test_bit(OPCODE2(insn), (unsigned long *)good_2byte_insns))
3042b144498SSrikar Dronamraju return 0;
3052b144498SSrikar Dronamraju }
3067b2d81d4SIngo Molnar
3072b144498SSrikar Dronamraju return -ENOTSUPP;
3082b144498SSrikar Dronamraju }
3092b144498SSrikar Dronamraju
3102b144498SSrikar Dronamraju #ifdef CONFIG_X86_64
3112b144498SSrikar Dronamraju /*
3123ff54efdSSrikar Dronamraju * If arch_uprobe->insn doesn't use rip-relative addressing, return
3132b144498SSrikar Dronamraju * immediately. Otherwise, rewrite the instruction so that it accesses
3142b144498SSrikar Dronamraju * its memory operand indirectly through a scratch register. Set
3155cdb76d6SOleg Nesterov * defparam->fixups accordingly. (The contents of the scratch register
31650204c6fSDenys Vlasenko * will be saved before we single-step the modified instruction,
31750204c6fSDenys Vlasenko * and restored afterward).
3182b144498SSrikar Dronamraju *
3192b144498SSrikar Dronamraju * We do this because a rip-relative instruction can access only a
3202b144498SSrikar Dronamraju * relatively small area (+/- 2 GB from the instruction), and the XOL
3212b144498SSrikar Dronamraju * area typically lies beyond that area. At least for instructions
3222b144498SSrikar Dronamraju * that store to memory, we can't execute the original instruction
3232b144498SSrikar Dronamraju * and "fix things up" later, because the misdirected store could be
3242b144498SSrikar Dronamraju * disastrous.
3252b144498SSrikar Dronamraju *
3262b144498SSrikar Dronamraju * Some useful facts about rip-relative instructions:
3277b2d81d4SIngo Molnar *
32850204c6fSDenys Vlasenko * - There's always a modrm byte with bit layout "00 reg 101".
3292b144498SSrikar Dronamraju * - There's never a SIB byte.
3302b144498SSrikar Dronamraju * - The displacement is always 4 bytes.
33150204c6fSDenys Vlasenko * - REX.B=1 bit in REX prefix, which normally extends r/m field,
33250204c6fSDenys Vlasenko * has no effect on rip-relative mode. It doesn't make modrm byte
33350204c6fSDenys Vlasenko * with r/m=101 refer to register 1101 = R13.
3342b144498SSrikar Dronamraju */
riprel_analyze(struct arch_uprobe * auprobe,struct insn * insn)3351475ee7fSOleg Nesterov static void riprel_analyze(struct arch_uprobe *auprobe, struct insn *insn)
3362b144498SSrikar Dronamraju {
3372b144498SSrikar Dronamraju u8 *cursor;
3382b144498SSrikar Dronamraju u8 reg;
3391ea30fb6SDenys Vlasenko u8 reg2;
3402b144498SSrikar Dronamraju
3412b144498SSrikar Dronamraju if (!insn_rip_relative(insn))
3422b144498SSrikar Dronamraju return;
3432b144498SSrikar Dronamraju
3442b144498SSrikar Dronamraju /*
3451ea30fb6SDenys Vlasenko * insn_rip_relative() would have decoded rex_prefix, vex_prefix, modrm.
3462b144498SSrikar Dronamraju * Clear REX.b bit (extension of MODRM.rm field):
3471ea30fb6SDenys Vlasenko * we want to encode low numbered reg, not r8+.
3482b144498SSrikar Dronamraju */
3492b144498SSrikar Dronamraju if (insn->rex_prefix.nbytes) {
3503ff54efdSSrikar Dronamraju cursor = auprobe->insn + insn_offset_rex_prefix(insn);
3511ea30fb6SDenys Vlasenko /* REX byte has 0100wrxb layout, clearing REX.b bit */
3521ea30fb6SDenys Vlasenko *cursor &= 0xfe;
3531ea30fb6SDenys Vlasenko }
3541ea30fb6SDenys Vlasenko /*
35568187872SDenys Vlasenko * Similar treatment for VEX3/EVEX prefix.
35668187872SDenys Vlasenko * TODO: add XOP treatment when insn decoder supports them
3571ea30fb6SDenys Vlasenko */
35868187872SDenys Vlasenko if (insn->vex_prefix.nbytes >= 3) {
3591ea30fb6SDenys Vlasenko /*
3601ea30fb6SDenys Vlasenko * vex2: c5 rvvvvLpp (has no b bit)
3611ea30fb6SDenys Vlasenko * vex3/xop: c4/8f rxbmmmmm wvvvvLpp
3621ea30fb6SDenys Vlasenko * evex: 62 rxbR00mm wvvvv1pp zllBVaaa
36368187872SDenys Vlasenko * Setting VEX3.b (setting because it has inverted meaning).
36468187872SDenys Vlasenko * Setting EVEX.x since (in non-SIB encoding) EVEX.x
36568187872SDenys Vlasenko * is the 4th bit of MODRM.rm, and needs the same treatment.
36668187872SDenys Vlasenko * For VEX3-encoded insns, VEX3.x value has no effect in
36768187872SDenys Vlasenko * non-SIB encoding, the change is superfluous but harmless.
3681ea30fb6SDenys Vlasenko */
3691ea30fb6SDenys Vlasenko cursor = auprobe->insn + insn_offset_vex_prefix(insn) + 1;
37068187872SDenys Vlasenko *cursor |= 0x60;
3712b144498SSrikar Dronamraju }
3722b144498SSrikar Dronamraju
3732b144498SSrikar Dronamraju /*
3741ea30fb6SDenys Vlasenko * Convert from rip-relative addressing to register-relative addressing
3751ea30fb6SDenys Vlasenko * via a scratch register.
3761ea30fb6SDenys Vlasenko *
3771ea30fb6SDenys Vlasenko * This is tricky since there are insns with modrm byte
3781ea30fb6SDenys Vlasenko * which also use registers not encoded in modrm byte:
3791ea30fb6SDenys Vlasenko * [i]div/[i]mul: implicitly use dx:ax
3801ea30fb6SDenys Vlasenko * shift ops: implicitly use cx
3811ea30fb6SDenys Vlasenko * cmpxchg: implicitly uses ax
3821ea30fb6SDenys Vlasenko * cmpxchg8/16b: implicitly uses dx:ax and bx:cx
3831ea30fb6SDenys Vlasenko * Encoding: 0f c7/1 modrm
3841ea30fb6SDenys Vlasenko * The code below thinks that reg=1 (cx), chooses si as scratch.
3851ea30fb6SDenys Vlasenko * mulx: implicitly uses dx: mulx r/m,r1,r2 does r1:r2 = dx * r/m.
3861ea30fb6SDenys Vlasenko * First appeared in Haswell (BMI2 insn). It is vex-encoded.
3871ea30fb6SDenys Vlasenko * Example where none of bx,cx,dx can be used as scratch reg:
3881ea30fb6SDenys Vlasenko * c4 e2 63 f6 0d disp32 mulx disp32(%rip),%ebx,%ecx
3891ea30fb6SDenys Vlasenko * [v]pcmpistri: implicitly uses cx, xmm0
3901ea30fb6SDenys Vlasenko * [v]pcmpistrm: implicitly uses xmm0
3911ea30fb6SDenys Vlasenko * [v]pcmpestri: implicitly uses ax, dx, cx, xmm0
3921ea30fb6SDenys Vlasenko * [v]pcmpestrm: implicitly uses ax, dx, xmm0
3931ea30fb6SDenys Vlasenko * Evil SSE4.2 string comparison ops from hell.
3941ea30fb6SDenys Vlasenko * maskmovq/[v]maskmovdqu: implicitly uses (ds:rdi) as destination.
3951ea30fb6SDenys Vlasenko * Encoding: 0f f7 modrm, 66 0f f7 modrm, vex-encoded: c5 f9 f7 modrm.
3961ea30fb6SDenys Vlasenko * Store op1, byte-masked by op2 msb's in each byte, to (ds:rdi).
3971ea30fb6SDenys Vlasenko * AMD says it has no 3-operand form (vex.vvvv must be 1111)
3981ea30fb6SDenys Vlasenko * and that it can have only register operands, not mem
3991ea30fb6SDenys Vlasenko * (its modrm byte must have mode=11).
4001ea30fb6SDenys Vlasenko * If these restrictions will ever be lifted,
4011ea30fb6SDenys Vlasenko * we'll need code to prevent selection of di as scratch reg!
4021ea30fb6SDenys Vlasenko *
4031ea30fb6SDenys Vlasenko * Summary: I don't know any insns with modrm byte which
4041ea30fb6SDenys Vlasenko * use SI register implicitly. DI register is used only
4051ea30fb6SDenys Vlasenko * by one insn (maskmovq) and BX register is used
4061ea30fb6SDenys Vlasenko * only by one too (cmpxchg8b).
4071ea30fb6SDenys Vlasenko * BP is stack-segment based (may be a problem?).
4081ea30fb6SDenys Vlasenko * AX, DX, CX are off-limits (many implicit users).
4091ea30fb6SDenys Vlasenko * SP is unusable (it's stack pointer - think about "pop mem";
4101ea30fb6SDenys Vlasenko * also, rsp+disp32 needs sib encoding -> insn length change).
4111ea30fb6SDenys Vlasenko */
4121ea30fb6SDenys Vlasenko
4131ea30fb6SDenys Vlasenko reg = MODRM_REG(insn); /* Fetch modrm.reg */
4141ea30fb6SDenys Vlasenko reg2 = 0xff; /* Fetch vex.vvvv */
41568187872SDenys Vlasenko if (insn->vex_prefix.nbytes)
4161ea30fb6SDenys Vlasenko reg2 = insn->vex_prefix.bytes[2];
4171ea30fb6SDenys Vlasenko /*
41868187872SDenys Vlasenko * TODO: add XOP vvvv reading.
4191ea30fb6SDenys Vlasenko *
4201ea30fb6SDenys Vlasenko * vex.vvvv field is in bits 6-3, bits are inverted.
4211ea30fb6SDenys Vlasenko * But in 32-bit mode, high-order bit may be ignored.
4221ea30fb6SDenys Vlasenko * Therefore, let's consider only 3 low-order bits.
4231ea30fb6SDenys Vlasenko */
4241ea30fb6SDenys Vlasenko reg2 = ((reg2 >> 3) & 0x7) ^ 0x7;
4251ea30fb6SDenys Vlasenko /*
4261ea30fb6SDenys Vlasenko * Register numbering is ax,cx,dx,bx, sp,bp,si,di, r8..r15.
4271ea30fb6SDenys Vlasenko *
4281ea30fb6SDenys Vlasenko * Choose scratch reg. Order is important: must not select bx
4291ea30fb6SDenys Vlasenko * if we can use si (cmpxchg8b case!)
4301ea30fb6SDenys Vlasenko */
4311ea30fb6SDenys Vlasenko if (reg != 6 && reg2 != 6) {
4321ea30fb6SDenys Vlasenko reg2 = 6;
4335cdb76d6SOleg Nesterov auprobe->defparam.fixups |= UPROBE_FIX_RIP_SI;
4341ea30fb6SDenys Vlasenko } else if (reg != 7 && reg2 != 7) {
4351ea30fb6SDenys Vlasenko reg2 = 7;
4365cdb76d6SOleg Nesterov auprobe->defparam.fixups |= UPROBE_FIX_RIP_DI;
4371ea30fb6SDenys Vlasenko /* TODO (paranoia): force maskmovq to not use di */
4381ea30fb6SDenys Vlasenko } else {
4391ea30fb6SDenys Vlasenko reg2 = 3;
4405cdb76d6SOleg Nesterov auprobe->defparam.fixups |= UPROBE_FIX_RIP_BX;
4411ea30fb6SDenys Vlasenko }
4421ea30fb6SDenys Vlasenko /*
4432b144498SSrikar Dronamraju * Point cursor at the modrm byte. The next 4 bytes are the
4442b144498SSrikar Dronamraju * displacement. Beyond the displacement, for some instructions,
4452b144498SSrikar Dronamraju * is the immediate operand.
4462b144498SSrikar Dronamraju */
4473ff54efdSSrikar Dronamraju cursor = auprobe->insn + insn_offset_modrm(insn);
4482b144498SSrikar Dronamraju /*
4491ea30fb6SDenys Vlasenko * Change modrm from "00 reg 101" to "10 reg reg2". Example:
45050204c6fSDenys Vlasenko * 89 05 disp32 mov %eax,disp32(%rip) becomes
4511ea30fb6SDenys Vlasenko * 89 86 disp32 mov %eax,disp32(%rsi)
45250204c6fSDenys Vlasenko */
4531ea30fb6SDenys Vlasenko *cursor = 0x80 | (reg << 3) | reg2;
4542b144498SSrikar Dronamraju }
4552b144498SSrikar Dronamraju
456c90a6950SOleg Nesterov static inline unsigned long *
scratch_reg(struct arch_uprobe * auprobe,struct pt_regs * regs)457c90a6950SOleg Nesterov scratch_reg(struct arch_uprobe *auprobe, struct pt_regs *regs)
458c90a6950SOleg Nesterov {
4595cdb76d6SOleg Nesterov if (auprobe->defparam.fixups & UPROBE_FIX_RIP_SI)
4601ea30fb6SDenys Vlasenko return ®s->si;
4615cdb76d6SOleg Nesterov if (auprobe->defparam.fixups & UPROBE_FIX_RIP_DI)
4621ea30fb6SDenys Vlasenko return ®s->di;
4631ea30fb6SDenys Vlasenko return ®s->bx;
464c90a6950SOleg Nesterov }
465c90a6950SOleg Nesterov
466d20737c0SOleg Nesterov /*
467d20737c0SOleg Nesterov * If we're emulating a rip-relative instruction, save the contents
468d20737c0SOleg Nesterov * of the scratch register and store the target address in that register.
469d20737c0SOleg Nesterov */
riprel_pre_xol(struct arch_uprobe * auprobe,struct pt_regs * regs)4707f55e82bSOleg Nesterov static void riprel_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
471d20737c0SOleg Nesterov {
4725cdb76d6SOleg Nesterov if (auprobe->defparam.fixups & UPROBE_FIX_RIP_MASK) {
4737f55e82bSOleg Nesterov struct uprobe_task *utask = current->utask;
474c90a6950SOleg Nesterov unsigned long *sr = scratch_reg(auprobe, regs);
4757f55e82bSOleg Nesterov
476c90a6950SOleg Nesterov utask->autask.saved_scratch_register = *sr;
4775cdb76d6SOleg Nesterov *sr = utask->vaddr + auprobe->defparam.ilen;
478d20737c0SOleg Nesterov }
479d20737c0SOleg Nesterov }
480d20737c0SOleg Nesterov
riprel_post_xol(struct arch_uprobe * auprobe,struct pt_regs * regs)48150204c6fSDenys Vlasenko static void riprel_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
482d20737c0SOleg Nesterov {
4835cdb76d6SOleg Nesterov if (auprobe->defparam.fixups & UPROBE_FIX_RIP_MASK) {
484c90a6950SOleg Nesterov struct uprobe_task *utask = current->utask;
485c90a6950SOleg Nesterov unsigned long *sr = scratch_reg(auprobe, regs);
486d20737c0SOleg Nesterov
487c90a6950SOleg Nesterov *sr = utask->autask.saved_scratch_register;
488d20737c0SOleg Nesterov }
489d20737c0SOleg Nesterov }
4907b2d81d4SIngo Molnar #else /* 32-bit: */
491d20737c0SOleg Nesterov /*
492d20737c0SOleg Nesterov * No RIP-relative addressing on 32-bit
493d20737c0SOleg Nesterov */
riprel_analyze(struct arch_uprobe * auprobe,struct insn * insn)4941475ee7fSOleg Nesterov static void riprel_analyze(struct arch_uprobe *auprobe, struct insn *insn)
4952b144498SSrikar Dronamraju {
496d20737c0SOleg Nesterov }
riprel_pre_xol(struct arch_uprobe * auprobe,struct pt_regs * regs)4977f55e82bSOleg Nesterov static void riprel_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
498d20737c0SOleg Nesterov {
499d20737c0SOleg Nesterov }
riprel_post_xol(struct arch_uprobe * auprobe,struct pt_regs * regs)50050204c6fSDenys Vlasenko static void riprel_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
501d20737c0SOleg Nesterov {
5022b144498SSrikar Dronamraju }
5032b144498SSrikar Dronamraju #endif /* CONFIG_X86_64 */
5042b144498SSrikar Dronamraju
5058ad8e9d3SOleg Nesterov struct uprobe_xol_ops {
5068ad8e9d3SOleg Nesterov bool (*emulate)(struct arch_uprobe *, struct pt_regs *);
5078ad8e9d3SOleg Nesterov int (*pre_xol)(struct arch_uprobe *, struct pt_regs *);
5088ad8e9d3SOleg Nesterov int (*post_xol)(struct arch_uprobe *, struct pt_regs *);
509588fbd61SOleg Nesterov void (*abort)(struct arch_uprobe *, struct pt_regs *);
5108ad8e9d3SOleg Nesterov };
5118ad8e9d3SOleg Nesterov
sizeof_long(struct pt_regs * regs)5129212ec7dSSebastian Mayr static inline int sizeof_long(struct pt_regs *regs)
5138faaed1bSOleg Nesterov {
5149212ec7dSSebastian Mayr /*
5159212ec7dSSebastian Mayr * Check registers for mode as in_xxx_syscall() does not apply here.
5169212ec7dSSebastian Mayr */
5179212ec7dSSebastian Mayr return user_64bit_mode(regs) ? 8 : 4;
5188faaed1bSOleg Nesterov }
5198faaed1bSOleg Nesterov
default_pre_xol_op(struct arch_uprobe * auprobe,struct pt_regs * regs)5208ad8e9d3SOleg Nesterov static int default_pre_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
5218ad8e9d3SOleg Nesterov {
5227f55e82bSOleg Nesterov riprel_pre_xol(auprobe, regs);
5238ad8e9d3SOleg Nesterov return 0;
5248ad8e9d3SOleg Nesterov }
5258ad8e9d3SOleg Nesterov
emulate_push_stack(struct pt_regs * regs,unsigned long val)526e7ed9d9bSYonghong Song static int emulate_push_stack(struct pt_regs *regs, unsigned long val)
5272b82cadfSOleg Nesterov {
5289212ec7dSSebastian Mayr unsigned long new_sp = regs->sp - sizeof_long(regs);
5292b82cadfSOleg Nesterov
5309212ec7dSSebastian Mayr if (copy_to_user((void __user *)new_sp, &val, sizeof_long(regs)))
5312b82cadfSOleg Nesterov return -EFAULT;
5322b82cadfSOleg Nesterov
5332b82cadfSOleg Nesterov regs->sp = new_sp;
5342b82cadfSOleg Nesterov return 0;
5352b82cadfSOleg Nesterov }
5362b82cadfSOleg Nesterov
5371ea30fb6SDenys Vlasenko /*
5381ea30fb6SDenys Vlasenko * We have to fix things up as follows:
5391ea30fb6SDenys Vlasenko *
5401ea30fb6SDenys Vlasenko * Typically, the new ip is relative to the copied instruction. We need
5411ea30fb6SDenys Vlasenko * to make it relative to the original instruction (FIX_IP). Exceptions
5421ea30fb6SDenys Vlasenko * are return instructions and absolute or indirect jump or call instructions.
5431ea30fb6SDenys Vlasenko *
5441ea30fb6SDenys Vlasenko * If the single-stepped instruction was a call, the return address that
5451ea30fb6SDenys Vlasenko * is atop the stack is the address following the copied instruction. We
5461ea30fb6SDenys Vlasenko * need to make it the address following the original instruction (FIX_CALL).
5471ea30fb6SDenys Vlasenko *
5481ea30fb6SDenys Vlasenko * If the original instruction was a rip-relative instruction such as
5491ea30fb6SDenys Vlasenko * "movl %edx,0xnnnn(%rip)", we have instead executed an equivalent
5501ea30fb6SDenys Vlasenko * instruction using a scratch register -- e.g., "movl %edx,0xnnnn(%rsi)".
5511ea30fb6SDenys Vlasenko * We need to restore the contents of the scratch register
5521ea30fb6SDenys Vlasenko * (FIX_RIP_reg).
5531ea30fb6SDenys Vlasenko */
default_post_xol_op(struct arch_uprobe * auprobe,struct pt_regs * regs)5548ad8e9d3SOleg Nesterov static int default_post_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
5558ad8e9d3SOleg Nesterov {
5568ad8e9d3SOleg Nesterov struct uprobe_task *utask = current->utask;
5578ad8e9d3SOleg Nesterov
55850204c6fSDenys Vlasenko riprel_post_xol(auprobe, regs);
5595cdb76d6SOleg Nesterov if (auprobe->defparam.fixups & UPROBE_FIX_IP) {
56050204c6fSDenys Vlasenko long correction = utask->vaddr - utask->xol_vaddr;
5618ad8e9d3SOleg Nesterov regs->ip += correction;
5625cdb76d6SOleg Nesterov } else if (auprobe->defparam.fixups & UPROBE_FIX_CALL) {
5639212ec7dSSebastian Mayr regs->sp += sizeof_long(regs); /* Pop incorrect return address */
564e7ed9d9bSYonghong Song if (emulate_push_stack(regs, utask->vaddr + auprobe->defparam.ilen))
56575f9ef0bSOleg Nesterov return -ERESTART;
56675f9ef0bSOleg Nesterov }
567220ef8dcSOleg Nesterov /* popf; tell the caller to not touch TF */
5685cdb76d6SOleg Nesterov if (auprobe->defparam.fixups & UPROBE_FIX_SETF)
569220ef8dcSOleg Nesterov utask->autask.saved_tf = true;
5708ad8e9d3SOleg Nesterov
57175f9ef0bSOleg Nesterov return 0;
5728ad8e9d3SOleg Nesterov }
5738ad8e9d3SOleg Nesterov
default_abort_op(struct arch_uprobe * auprobe,struct pt_regs * regs)574588fbd61SOleg Nesterov static void default_abort_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
575588fbd61SOleg Nesterov {
57650204c6fSDenys Vlasenko riprel_post_xol(auprobe, regs);
577588fbd61SOleg Nesterov }
578588fbd61SOleg Nesterov
579dac42987SJulia Lawall static const struct uprobe_xol_ops default_xol_ops = {
5808ad8e9d3SOleg Nesterov .pre_xol = default_pre_xol_op,
5818ad8e9d3SOleg Nesterov .post_xol = default_post_xol_op,
582588fbd61SOleg Nesterov .abort = default_abort_op,
5838ad8e9d3SOleg Nesterov };
5848ad8e9d3SOleg Nesterov
branch_is_call(struct arch_uprobe * auprobe)5858e89c0beSOleg Nesterov static bool branch_is_call(struct arch_uprobe *auprobe)
5868e89c0beSOleg Nesterov {
5878e89c0beSOleg Nesterov return auprobe->branch.opc1 == 0xe8;
5888e89c0beSOleg Nesterov }
5898e89c0beSOleg Nesterov
5908f95505bSOleg Nesterov #define CASE_COND \
5918f95505bSOleg Nesterov COND(70, 71, XF(OF)) \
5928f95505bSOleg Nesterov COND(72, 73, XF(CF)) \
5938f95505bSOleg Nesterov COND(74, 75, XF(ZF)) \
5948f95505bSOleg Nesterov COND(78, 79, XF(SF)) \
5958f95505bSOleg Nesterov COND(7a, 7b, XF(PF)) \
5968f95505bSOleg Nesterov COND(76, 77, XF(CF) || XF(ZF)) \
5978f95505bSOleg Nesterov COND(7c, 7d, XF(SF) != XF(OF)) \
5988f95505bSOleg Nesterov COND(7e, 7f, XF(ZF) || XF(SF) != XF(OF))
5998f95505bSOleg Nesterov
6008f95505bSOleg Nesterov #define COND(op_y, op_n, expr) \
6018f95505bSOleg Nesterov case 0x ## op_y: DO((expr) != 0) \
6028f95505bSOleg Nesterov case 0x ## op_n: DO((expr) == 0)
6038f95505bSOleg Nesterov
6048f95505bSOleg Nesterov #define XF(xf) (!!(flags & X86_EFLAGS_ ## xf))
6058f95505bSOleg Nesterov
is_cond_jmp_opcode(u8 opcode)6068f95505bSOleg Nesterov static bool is_cond_jmp_opcode(u8 opcode)
6078f95505bSOleg Nesterov {
6088f95505bSOleg Nesterov switch (opcode) {
6098f95505bSOleg Nesterov #define DO(expr) \
6108f95505bSOleg Nesterov return true;
6118f95505bSOleg Nesterov CASE_COND
6128f95505bSOleg Nesterov #undef DO
6138f95505bSOleg Nesterov
6148f95505bSOleg Nesterov default:
6158f95505bSOleg Nesterov return false;
6168f95505bSOleg Nesterov }
6178f95505bSOleg Nesterov }
6188f95505bSOleg Nesterov
check_jmp_cond(struct arch_uprobe * auprobe,struct pt_regs * regs)6198f95505bSOleg Nesterov static bool check_jmp_cond(struct arch_uprobe *auprobe, struct pt_regs *regs)
6208f95505bSOleg Nesterov {
6218f95505bSOleg Nesterov unsigned long flags = regs->flags;
6228f95505bSOleg Nesterov
6238f95505bSOleg Nesterov switch (auprobe->branch.opc1) {
6248f95505bSOleg Nesterov #define DO(expr) \
6258f95505bSOleg Nesterov return expr;
6268f95505bSOleg Nesterov CASE_COND
6278f95505bSOleg Nesterov #undef DO
6288f95505bSOleg Nesterov
6298f95505bSOleg Nesterov default: /* not a conditional jmp */
6308f95505bSOleg Nesterov return true;
6318f95505bSOleg Nesterov }
6328f95505bSOleg Nesterov }
6338f95505bSOleg Nesterov
6348f95505bSOleg Nesterov #undef XF
6358f95505bSOleg Nesterov #undef COND
6368f95505bSOleg Nesterov #undef CASE_COND
6378f95505bSOleg Nesterov
branch_emulate_op(struct arch_uprobe * auprobe,struct pt_regs * regs)6387ba6db2dSOleg Nesterov static bool branch_emulate_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
6397ba6db2dSOleg Nesterov {
6408e89c0beSOleg Nesterov unsigned long new_ip = regs->ip += auprobe->branch.ilen;
6418f95505bSOleg Nesterov unsigned long offs = (long)auprobe->branch.offs;
6428e89c0beSOleg Nesterov
6438e89c0beSOleg Nesterov if (branch_is_call(auprobe)) {
6448e89c0beSOleg Nesterov /*
6458e89c0beSOleg Nesterov * If it fails we execute this (mangled, see the comment in
6468e89c0beSOleg Nesterov * branch_clear_offset) insn out-of-line. In the likely case
6478e89c0beSOleg Nesterov * this should trigger the trap, and the probed application
6488e89c0beSOleg Nesterov * should die or restart the same insn after it handles the
6498e89c0beSOleg Nesterov * signal, arch_uprobe_post_xol() won't be even called.
6508e89c0beSOleg Nesterov *
6518e89c0beSOleg Nesterov * But there is corner case, see the comment in ->post_xol().
6528e89c0beSOleg Nesterov */
653e7ed9d9bSYonghong Song if (emulate_push_stack(regs, new_ip))
6548e89c0beSOleg Nesterov return false;
6558f95505bSOleg Nesterov } else if (!check_jmp_cond(auprobe, regs)) {
6568f95505bSOleg Nesterov offs = 0;
6578e89c0beSOleg Nesterov }
6588e89c0beSOleg Nesterov
6598f95505bSOleg Nesterov regs->ip = new_ip + offs;
6607ba6db2dSOleg Nesterov return true;
6617ba6db2dSOleg Nesterov }
6627ba6db2dSOleg Nesterov
push_emulate_op(struct arch_uprobe * auprobe,struct pt_regs * regs)663e7ed9d9bSYonghong Song static bool push_emulate_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
664e7ed9d9bSYonghong Song {
665e7ed9d9bSYonghong Song unsigned long *src_ptr = (void *)regs + auprobe->push.reg_offset;
666e7ed9d9bSYonghong Song
667e7ed9d9bSYonghong Song if (emulate_push_stack(regs, *src_ptr))
668e7ed9d9bSYonghong Song return false;
669e7ed9d9bSYonghong Song regs->ip += auprobe->push.ilen;
670e7ed9d9bSYonghong Song return true;
671e7ed9d9bSYonghong Song }
672e7ed9d9bSYonghong Song
branch_post_xol_op(struct arch_uprobe * auprobe,struct pt_regs * regs)6738e89c0beSOleg Nesterov static int branch_post_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
6748e89c0beSOleg Nesterov {
6758e89c0beSOleg Nesterov BUG_ON(!branch_is_call(auprobe));
6768e89c0beSOleg Nesterov /*
6778e89c0beSOleg Nesterov * We can only get here if branch_emulate_op() failed to push the ret
6788e89c0beSOleg Nesterov * address _and_ another thread expanded our stack before the (mangled)
6798e89c0beSOleg Nesterov * "call" insn was executed out-of-line. Just restore ->sp and restart.
6808e89c0beSOleg Nesterov * We could also restore ->ip and try to call branch_emulate_op() again.
6818e89c0beSOleg Nesterov */
6829212ec7dSSebastian Mayr regs->sp += sizeof_long(regs);
6838e89c0beSOleg Nesterov return -ERESTART;
6848e89c0beSOleg Nesterov }
6858e89c0beSOleg Nesterov
branch_clear_offset(struct arch_uprobe * auprobe,struct insn * insn)6868e89c0beSOleg Nesterov static void branch_clear_offset(struct arch_uprobe *auprobe, struct insn *insn)
6878e89c0beSOleg Nesterov {
6888e89c0beSOleg Nesterov /*
6898e89c0beSOleg Nesterov * Turn this insn into "call 1f; 1:", this is what we will execute
6908e89c0beSOleg Nesterov * out-of-line if ->emulate() fails. We only need this to generate
6918e89c0beSOleg Nesterov * a trap, so that the probed task receives the correct signal with
6928e89c0beSOleg Nesterov * the properly filled siginfo.
6938e89c0beSOleg Nesterov *
6948e89c0beSOleg Nesterov * But see the comment in ->post_xol(), in the unlikely case it can
6958e89c0beSOleg Nesterov * succeed. So we need to ensure that the new ->ip can not fall into
6968e89c0beSOleg Nesterov * the non-canonical area and trigger #GP.
6978e89c0beSOleg Nesterov *
6988e89c0beSOleg Nesterov * We could turn it into (say) "pushf", but then we would need to
6998e89c0beSOleg Nesterov * divorce ->insn[] and ->ixol[]. We need to preserve the 1st byte
7008e89c0beSOleg Nesterov * of ->insn[] for set_orig_insn().
7018e89c0beSOleg Nesterov */
7028e89c0beSOleg Nesterov memset(auprobe->insn + insn_offset_immediate(insn),
7038e89c0beSOleg Nesterov 0, insn->immediate.nbytes);
7048e89c0beSOleg Nesterov }
7058e89c0beSOleg Nesterov
706dac42987SJulia Lawall static const struct uprobe_xol_ops branch_xol_ops = {
7077ba6db2dSOleg Nesterov .emulate = branch_emulate_op,
7088e89c0beSOleg Nesterov .post_xol = branch_post_xol_op,
7097ba6db2dSOleg Nesterov };
7107ba6db2dSOleg Nesterov
711e7ed9d9bSYonghong Song static const struct uprobe_xol_ops push_xol_ops = {
712e7ed9d9bSYonghong Song .emulate = push_emulate_op,
713e7ed9d9bSYonghong Song };
714e7ed9d9bSYonghong Song
7157ba6db2dSOleg Nesterov /* Returns -ENOSYS if branch_xol_ops doesn't handle this insn */
branch_setup_xol_ops(struct arch_uprobe * auprobe,struct insn * insn)7167ba6db2dSOleg Nesterov static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
7177ba6db2dSOleg Nesterov {
7188e89c0beSOleg Nesterov u8 opc1 = OPCODE1(insn);
7194e9a5ae8SMasami Hiramatsu insn_byte_t p;
720250bbd12SDenys Vlasenko int i;
7217ba6db2dSOleg Nesterov
7228e89c0beSOleg Nesterov switch (opc1) {
7238e89c0beSOleg Nesterov case 0xeb: /* jmp 8 */
7248e89c0beSOleg Nesterov case 0xe9: /* jmp 32 */
7258e89c0beSOleg Nesterov break;
726cefa7212SOleg Nesterov case 0x90: /* prefix* + nop; same as jmp with .offs = 0 */
727cefa7212SOleg Nesterov goto setup;
7288e89c0beSOleg Nesterov
7298e89c0beSOleg Nesterov case 0xe8: /* call relative */
7308e89c0beSOleg Nesterov branch_clear_offset(auprobe, insn);
7318e89c0beSOleg Nesterov break;
7328f95505bSOleg Nesterov
7336cc5e7ffSOleg Nesterov case 0x0f:
7346cc5e7ffSOleg Nesterov if (insn->opcode.nbytes != 2)
7356cc5e7ffSOleg Nesterov return -ENOSYS;
7366cc5e7ffSOleg Nesterov /*
7376cc5e7ffSOleg Nesterov * If it is a "near" conditional jmp, OPCODE2() - 0x10 matches
7386cc5e7ffSOleg Nesterov * OPCODE1() of the "short" jmp which checks the same condition.
7396cc5e7ffSOleg Nesterov */
7406cc5e7ffSOleg Nesterov opc1 = OPCODE2(insn) - 0x10;
741df561f66SGustavo A. R. Silva fallthrough;
7428e89c0beSOleg Nesterov default:
7438f95505bSOleg Nesterov if (!is_cond_jmp_opcode(opc1))
7448e89c0beSOleg Nesterov return -ENOSYS;
7458e89c0beSOleg Nesterov }
7468e89c0beSOleg Nesterov
747250bbd12SDenys Vlasenko /*
748250bbd12SDenys Vlasenko * 16-bit overrides such as CALLW (66 e8 nn nn) are not supported.
749250bbd12SDenys Vlasenko * Intel and AMD behavior differ in 64-bit mode: Intel ignores 66 prefix.
750250bbd12SDenys Vlasenko * No one uses these insns, reject any branch insns with such prefix.
751250bbd12SDenys Vlasenko */
7524e9a5ae8SMasami Hiramatsu for_each_insn_prefix(insn, i, p) {
7534e9a5ae8SMasami Hiramatsu if (p == 0x66)
754250bbd12SDenys Vlasenko return -ENOTSUPP;
755250bbd12SDenys Vlasenko }
756250bbd12SDenys Vlasenko
757cefa7212SOleg Nesterov setup:
7588e89c0beSOleg Nesterov auprobe->branch.opc1 = opc1;
7597ba6db2dSOleg Nesterov auprobe->branch.ilen = insn->length;
7607ba6db2dSOleg Nesterov auprobe->branch.offs = insn->immediate.value;
7617ba6db2dSOleg Nesterov
7627ba6db2dSOleg Nesterov auprobe->ops = &branch_xol_ops;
7637ba6db2dSOleg Nesterov return 0;
7647ba6db2dSOleg Nesterov }
7657ba6db2dSOleg Nesterov
766e7ed9d9bSYonghong Song /* Returns -ENOSYS if push_xol_ops doesn't handle this insn */
push_setup_xol_ops(struct arch_uprobe * auprobe,struct insn * insn)767e7ed9d9bSYonghong Song static int push_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
768e7ed9d9bSYonghong Song {
769e7ed9d9bSYonghong Song u8 opc1 = OPCODE1(insn), reg_offset = 0;
770e7ed9d9bSYonghong Song
771e7ed9d9bSYonghong Song if (opc1 < 0x50 || opc1 > 0x57)
772e7ed9d9bSYonghong Song return -ENOSYS;
773e7ed9d9bSYonghong Song
774e7ed9d9bSYonghong Song if (insn->length > 2)
775e7ed9d9bSYonghong Song return -ENOSYS;
776e7ed9d9bSYonghong Song if (insn->length == 2) {
777e7ed9d9bSYonghong Song /* only support rex_prefix 0x41 (x64 only) */
778e7ed9d9bSYonghong Song #ifdef CONFIG_X86_64
779e7ed9d9bSYonghong Song if (insn->rex_prefix.nbytes != 1 ||
780e7ed9d9bSYonghong Song insn->rex_prefix.bytes[0] != 0x41)
781e7ed9d9bSYonghong Song return -ENOSYS;
782e7ed9d9bSYonghong Song
783e7ed9d9bSYonghong Song switch (opc1) {
784e7ed9d9bSYonghong Song case 0x50:
785e7ed9d9bSYonghong Song reg_offset = offsetof(struct pt_regs, r8);
786e7ed9d9bSYonghong Song break;
787e7ed9d9bSYonghong Song case 0x51:
788e7ed9d9bSYonghong Song reg_offset = offsetof(struct pt_regs, r9);
789e7ed9d9bSYonghong Song break;
790e7ed9d9bSYonghong Song case 0x52:
791e7ed9d9bSYonghong Song reg_offset = offsetof(struct pt_regs, r10);
792e7ed9d9bSYonghong Song break;
793e7ed9d9bSYonghong Song case 0x53:
794e7ed9d9bSYonghong Song reg_offset = offsetof(struct pt_regs, r11);
795e7ed9d9bSYonghong Song break;
796e7ed9d9bSYonghong Song case 0x54:
797e7ed9d9bSYonghong Song reg_offset = offsetof(struct pt_regs, r12);
798e7ed9d9bSYonghong Song break;
799e7ed9d9bSYonghong Song case 0x55:
800e7ed9d9bSYonghong Song reg_offset = offsetof(struct pt_regs, r13);
801e7ed9d9bSYonghong Song break;
802e7ed9d9bSYonghong Song case 0x56:
803e7ed9d9bSYonghong Song reg_offset = offsetof(struct pt_regs, r14);
804e7ed9d9bSYonghong Song break;
805e7ed9d9bSYonghong Song case 0x57:
806e7ed9d9bSYonghong Song reg_offset = offsetof(struct pt_regs, r15);
807e7ed9d9bSYonghong Song break;
808e7ed9d9bSYonghong Song }
809e7ed9d9bSYonghong Song #else
810e7ed9d9bSYonghong Song return -ENOSYS;
811e7ed9d9bSYonghong Song #endif
812e7ed9d9bSYonghong Song } else {
813e7ed9d9bSYonghong Song switch (opc1) {
814e7ed9d9bSYonghong Song case 0x50:
815e7ed9d9bSYonghong Song reg_offset = offsetof(struct pt_regs, ax);
816e7ed9d9bSYonghong Song break;
817e7ed9d9bSYonghong Song case 0x51:
818e7ed9d9bSYonghong Song reg_offset = offsetof(struct pt_regs, cx);
819e7ed9d9bSYonghong Song break;
820e7ed9d9bSYonghong Song case 0x52:
821e7ed9d9bSYonghong Song reg_offset = offsetof(struct pt_regs, dx);
822e7ed9d9bSYonghong Song break;
823e7ed9d9bSYonghong Song case 0x53:
824e7ed9d9bSYonghong Song reg_offset = offsetof(struct pt_regs, bx);
825e7ed9d9bSYonghong Song break;
826e7ed9d9bSYonghong Song case 0x54:
827e7ed9d9bSYonghong Song reg_offset = offsetof(struct pt_regs, sp);
828e7ed9d9bSYonghong Song break;
829e7ed9d9bSYonghong Song case 0x55:
830e7ed9d9bSYonghong Song reg_offset = offsetof(struct pt_regs, bp);
831e7ed9d9bSYonghong Song break;
832e7ed9d9bSYonghong Song case 0x56:
833e7ed9d9bSYonghong Song reg_offset = offsetof(struct pt_regs, si);
834e7ed9d9bSYonghong Song break;
835e7ed9d9bSYonghong Song case 0x57:
836e7ed9d9bSYonghong Song reg_offset = offsetof(struct pt_regs, di);
837e7ed9d9bSYonghong Song break;
838e7ed9d9bSYonghong Song }
839e7ed9d9bSYonghong Song }
840e7ed9d9bSYonghong Song
841e7ed9d9bSYonghong Song auprobe->push.reg_offset = reg_offset;
842e7ed9d9bSYonghong Song auprobe->push.ilen = insn->length;
843e7ed9d9bSYonghong Song auprobe->ops = &push_xol_ops;
844e7ed9d9bSYonghong Song return 0;
845e7ed9d9bSYonghong Song }
846e7ed9d9bSYonghong Song
8472b144498SSrikar Dronamraju /**
8480326f5a9SSrikar Dronamraju * arch_uprobe_analyze_insn - instruction analysis including validity and fixups.
84944eb5a7eSYi Wang * @auprobe: the probepoint information.
8502b144498SSrikar Dronamraju * @mm: the probed address space.
8517eb9ba5eSAnanth N Mavinakayanahalli * @addr: virtual address at which to install the probepoint
8522b144498SSrikar Dronamraju * Return 0 on success or a -ve number on error.
8532b144498SSrikar Dronamraju */
arch_uprobe_analyze_insn(struct arch_uprobe * auprobe,struct mm_struct * mm,unsigned long addr)8547eb9ba5eSAnanth N Mavinakayanahalli int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long addr)
8552b144498SSrikar Dronamraju {
8562b144498SSrikar Dronamraju struct insn insn;
85783cd5914SOleg Nesterov u8 fix_ip_or_call = UPROBE_FIX_IP;
858ddb69f27SOleg Nesterov int ret;
8592b144498SSrikar Dronamraju
8602ae1f49aSOleg Nesterov ret = uprobe_init_insn(auprobe, &insn, is_64bit_mm(mm));
861ddb69f27SOleg Nesterov if (ret)
8622b144498SSrikar Dronamraju return ret;
8637b2d81d4SIngo Molnar
8647ba6db2dSOleg Nesterov ret = branch_setup_xol_ops(auprobe, &insn);
8657ba6db2dSOleg Nesterov if (ret != -ENOSYS)
8667ba6db2dSOleg Nesterov return ret;
8677ba6db2dSOleg Nesterov
868e7ed9d9bSYonghong Song ret = push_setup_xol_ops(auprobe, &insn);
869e7ed9d9bSYonghong Song if (ret != -ENOSYS)
870e7ed9d9bSYonghong Song return ret;
871e7ed9d9bSYonghong Song
872ddb69f27SOleg Nesterov /*
87397aa5cddSOleg Nesterov * Figure out which fixups default_post_xol_op() will need to perform,
8745cdb76d6SOleg Nesterov * and annotate defparam->fixups accordingly.
875ddb69f27SOleg Nesterov */
876ddb69f27SOleg Nesterov switch (OPCODE1(&insn)) {
877ddb69f27SOleg Nesterov case 0x9d: /* popf */
8785cdb76d6SOleg Nesterov auprobe->defparam.fixups |= UPROBE_FIX_SETF;
879ddb69f27SOleg Nesterov break;
880ddb69f27SOleg Nesterov case 0xc3: /* ret or lret -- ip is correct */
881ddb69f27SOleg Nesterov case 0xcb:
882ddb69f27SOleg Nesterov case 0xc2:
883ddb69f27SOleg Nesterov case 0xca:
88483cd5914SOleg Nesterov case 0xea: /* jmp absolute -- ip is correct */
88583cd5914SOleg Nesterov fix_ip_or_call = 0;
886ddb69f27SOleg Nesterov break;
887ddb69f27SOleg Nesterov case 0x9a: /* call absolute - Fix return addr, not ip */
88883cd5914SOleg Nesterov fix_ip_or_call = UPROBE_FIX_CALL;
889ddb69f27SOleg Nesterov break;
890ddb69f27SOleg Nesterov case 0xff:
891ddb69f27SOleg Nesterov switch (MODRM_REG(&insn)) {
892ddb69f27SOleg Nesterov case 2: case 3: /* call or lcall, indirect */
89383cd5914SOleg Nesterov fix_ip_or_call = UPROBE_FIX_CALL;
89483cd5914SOleg Nesterov break;
895ddb69f27SOleg Nesterov case 4: case 5: /* jmp or ljmp, indirect */
89683cd5914SOleg Nesterov fix_ip_or_call = 0;
89783cd5914SOleg Nesterov break;
898ddb69f27SOleg Nesterov }
899df561f66SGustavo A. R. Silva fallthrough;
900ddb69f27SOleg Nesterov default:
9011475ee7fSOleg Nesterov riprel_analyze(auprobe, &insn);
902ddb69f27SOleg Nesterov }
903ddb69f27SOleg Nesterov
9045cdb76d6SOleg Nesterov auprobe->defparam.ilen = insn.length;
9055cdb76d6SOleg Nesterov auprobe->defparam.fixups |= fix_ip_or_call;
9067b2d81d4SIngo Molnar
9078ad8e9d3SOleg Nesterov auprobe->ops = &default_xol_ops;
9082b144498SSrikar Dronamraju return 0;
9092b144498SSrikar Dronamraju }
9100326f5a9SSrikar Dronamraju
9110326f5a9SSrikar Dronamraju /*
9120326f5a9SSrikar Dronamraju * arch_uprobe_pre_xol - prepare to execute out of line.
9130326f5a9SSrikar Dronamraju * @auprobe: the probepoint information.
9140326f5a9SSrikar Dronamraju * @regs: reflects the saved user state of current task.
9150326f5a9SSrikar Dronamraju */
arch_uprobe_pre_xol(struct arch_uprobe * auprobe,struct pt_regs * regs)9160326f5a9SSrikar Dronamraju int arch_uprobe_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
9170326f5a9SSrikar Dronamraju {
91834e7317dSOleg Nesterov struct uprobe_task *utask = current->utask;
9190326f5a9SSrikar Dronamraju
920dd91016dSOleg Nesterov if (auprobe->ops->pre_xol) {
921dd91016dSOleg Nesterov int err = auprobe->ops->pre_xol(auprobe, regs);
922dd91016dSOleg Nesterov if (err)
923dd91016dSOleg Nesterov return err;
924dd91016dSOleg Nesterov }
925dd91016dSOleg Nesterov
92634e7317dSOleg Nesterov regs->ip = utask->xol_vaddr;
92734e7317dSOleg Nesterov utask->autask.saved_trap_nr = current->thread.trap_nr;
9280326f5a9SSrikar Dronamraju current->thread.trap_nr = UPROBE_TRAP_NR;
9290326f5a9SSrikar Dronamraju
93034e7317dSOleg Nesterov utask->autask.saved_tf = !!(regs->flags & X86_EFLAGS_TF);
9314dc316c6SOleg Nesterov regs->flags |= X86_EFLAGS_TF;
9324dc316c6SOleg Nesterov if (test_tsk_thread_flag(current, TIF_BLOCKSTEP))
9334dc316c6SOleg Nesterov set_task_blockstep(current, false);
9344dc316c6SOleg Nesterov
9350326f5a9SSrikar Dronamraju return 0;
9360326f5a9SSrikar Dronamraju }
9370326f5a9SSrikar Dronamraju
9380326f5a9SSrikar Dronamraju /*
9390326f5a9SSrikar Dronamraju * If xol insn itself traps and generates a signal(Say,
9400326f5a9SSrikar Dronamraju * SIGILL/SIGSEGV/etc), then detect the case where a singlestepped
9410326f5a9SSrikar Dronamraju * instruction jumps back to its own address. It is assumed that anything
9420326f5a9SSrikar Dronamraju * like do_page_fault/do_trap/etc sets thread.trap_nr != -1.
9430326f5a9SSrikar Dronamraju *
9440326f5a9SSrikar Dronamraju * arch_uprobe_pre_xol/arch_uprobe_post_xol save/restore thread.trap_nr,
9450326f5a9SSrikar Dronamraju * arch_uprobe_xol_was_trapped() simply checks that ->trap_nr is not equal to
9460326f5a9SSrikar Dronamraju * UPROBE_TRAP_NR == -1 set by arch_uprobe_pre_xol().
9470326f5a9SSrikar Dronamraju */
arch_uprobe_xol_was_trapped(struct task_struct * t)9480326f5a9SSrikar Dronamraju bool arch_uprobe_xol_was_trapped(struct task_struct *t)
9490326f5a9SSrikar Dronamraju {
9500326f5a9SSrikar Dronamraju if (t->thread.trap_nr != UPROBE_TRAP_NR)
9510326f5a9SSrikar Dronamraju return true;
9520326f5a9SSrikar Dronamraju
9530326f5a9SSrikar Dronamraju return false;
9540326f5a9SSrikar Dronamraju }
9550326f5a9SSrikar Dronamraju
9560326f5a9SSrikar Dronamraju /*
9570326f5a9SSrikar Dronamraju * Called after single-stepping. To avoid the SMP problems that can
9580326f5a9SSrikar Dronamraju * occur when we temporarily put back the original opcode to
9590326f5a9SSrikar Dronamraju * single-step, we single-stepped a copy of the instruction.
9600326f5a9SSrikar Dronamraju *
9610326f5a9SSrikar Dronamraju * This function prepares to resume execution after the single-step.
9620326f5a9SSrikar Dronamraju */
arch_uprobe_post_xol(struct arch_uprobe * auprobe,struct pt_regs * regs)9630326f5a9SSrikar Dronamraju int arch_uprobe_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
9640326f5a9SSrikar Dronamraju {
96534e7317dSOleg Nesterov struct uprobe_task *utask = current->utask;
966220ef8dcSOleg Nesterov bool send_sigtrap = utask->autask.saved_tf;
967220ef8dcSOleg Nesterov int err = 0;
9680326f5a9SSrikar Dronamraju
9690326f5a9SSrikar Dronamraju WARN_ON_ONCE(current->thread.trap_nr != UPROBE_TRAP_NR);
9706ded5f38SOleg Nesterov current->thread.trap_nr = utask->autask.saved_trap_nr;
971014940baSOleg Nesterov
972014940baSOleg Nesterov if (auprobe->ops->post_xol) {
973220ef8dcSOleg Nesterov err = auprobe->ops->post_xol(auprobe, regs);
974014940baSOleg Nesterov if (err) {
97575f9ef0bSOleg Nesterov /*
9766ded5f38SOleg Nesterov * Restore ->ip for restart or post mortem analysis.
9776ded5f38SOleg Nesterov * ->post_xol() must not return -ERESTART unless this
9786ded5f38SOleg Nesterov * is really possible.
97975f9ef0bSOleg Nesterov */
9806ded5f38SOleg Nesterov regs->ip = utask->vaddr;
98175f9ef0bSOleg Nesterov if (err == -ERESTART)
982220ef8dcSOleg Nesterov err = 0;
983220ef8dcSOleg Nesterov send_sigtrap = false;
984014940baSOleg Nesterov }
985014940baSOleg Nesterov }
9864dc316c6SOleg Nesterov /*
9874dc316c6SOleg Nesterov * arch_uprobe_pre_xol() doesn't save the state of TIF_BLOCKSTEP
9884dc316c6SOleg Nesterov * so we can get an extra SIGTRAP if we do not clear TF. We need
9894dc316c6SOleg Nesterov * to examine the opcode to make it right.
9904dc316c6SOleg Nesterov */
991220ef8dcSOleg Nesterov if (send_sigtrap)
9924dc316c6SOleg Nesterov send_sig(SIGTRAP, current, 0);
993220ef8dcSOleg Nesterov
994220ef8dcSOleg Nesterov if (!utask->autask.saved_tf)
9954dc316c6SOleg Nesterov regs->flags &= ~X86_EFLAGS_TF;
9964dc316c6SOleg Nesterov
997220ef8dcSOleg Nesterov return err;
9980326f5a9SSrikar Dronamraju }
9990326f5a9SSrikar Dronamraju
10000326f5a9SSrikar Dronamraju /* callback routine for handling exceptions. */
arch_uprobe_exception_notify(struct notifier_block * self,unsigned long val,void * data)10010326f5a9SSrikar Dronamraju int arch_uprobe_exception_notify(struct notifier_block *self, unsigned long val, void *data)
10020326f5a9SSrikar Dronamraju {
10030326f5a9SSrikar Dronamraju struct die_args *args = data;
10040326f5a9SSrikar Dronamraju struct pt_regs *regs = args->regs;
10050326f5a9SSrikar Dronamraju int ret = NOTIFY_DONE;
10060326f5a9SSrikar Dronamraju
10070326f5a9SSrikar Dronamraju /* We are only interested in userspace traps */
1008f39b6f0eSAndy Lutomirski if (regs && !user_mode(regs))
10090326f5a9SSrikar Dronamraju return NOTIFY_DONE;
10100326f5a9SSrikar Dronamraju
10110326f5a9SSrikar Dronamraju switch (val) {
10120326f5a9SSrikar Dronamraju case DIE_INT3:
10130326f5a9SSrikar Dronamraju if (uprobe_pre_sstep_notifier(regs))
10140326f5a9SSrikar Dronamraju ret = NOTIFY_STOP;
10150326f5a9SSrikar Dronamraju
10160326f5a9SSrikar Dronamraju break;
10170326f5a9SSrikar Dronamraju
10180326f5a9SSrikar Dronamraju case DIE_DEBUG:
10190326f5a9SSrikar Dronamraju if (uprobe_post_sstep_notifier(regs))
10200326f5a9SSrikar Dronamraju ret = NOTIFY_STOP;
10210326f5a9SSrikar Dronamraju
1022bd11952bSGustavo A. R. Silva break;
1023bd11952bSGustavo A. R. Silva
10240326f5a9SSrikar Dronamraju default:
10250326f5a9SSrikar Dronamraju break;
10260326f5a9SSrikar Dronamraju }
10270326f5a9SSrikar Dronamraju
10280326f5a9SSrikar Dronamraju return ret;
10290326f5a9SSrikar Dronamraju }
10300326f5a9SSrikar Dronamraju
10310326f5a9SSrikar Dronamraju /*
10320326f5a9SSrikar Dronamraju * This function gets called when XOL instruction either gets trapped or
10336ded5f38SOleg Nesterov * the thread has a fatal signal. Reset the instruction pointer to its
10346ded5f38SOleg Nesterov * probed address for the potential restart or for post mortem analysis.
10350326f5a9SSrikar Dronamraju */
arch_uprobe_abort_xol(struct arch_uprobe * auprobe,struct pt_regs * regs)10360326f5a9SSrikar Dronamraju void arch_uprobe_abort_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
10370326f5a9SSrikar Dronamraju {
10380326f5a9SSrikar Dronamraju struct uprobe_task *utask = current->utask;
10390326f5a9SSrikar Dronamraju
1040588fbd61SOleg Nesterov if (auprobe->ops->abort)
1041588fbd61SOleg Nesterov auprobe->ops->abort(auprobe, regs);
10424dc316c6SOleg Nesterov
1043588fbd61SOleg Nesterov current->thread.trap_nr = utask->autask.saved_trap_nr;
1044588fbd61SOleg Nesterov regs->ip = utask->vaddr;
10454dc316c6SOleg Nesterov /* clear TF if it was set by us in arch_uprobe_pre_xol() */
10464dc316c6SOleg Nesterov if (!utask->autask.saved_tf)
10474dc316c6SOleg Nesterov regs->flags &= ~X86_EFLAGS_TF;
10480326f5a9SSrikar Dronamraju }
10490326f5a9SSrikar Dronamraju
__skip_sstep(struct arch_uprobe * auprobe,struct pt_regs * regs)10503a4664aaSOleg Nesterov static bool __skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
10510326f5a9SSrikar Dronamraju {
10528ad8e9d3SOleg Nesterov if (auprobe->ops->emulate)
10538ad8e9d3SOleg Nesterov return auprobe->ops->emulate(auprobe, regs);
10540326f5a9SSrikar Dronamraju return false;
10550326f5a9SSrikar Dronamraju }
1056bdc1e472SSebastian Andrzej Siewior
arch_uprobe_skip_sstep(struct arch_uprobe * auprobe,struct pt_regs * regs)10573a4664aaSOleg Nesterov bool arch_uprobe_skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
10583a4664aaSOleg Nesterov {
10593a4664aaSOleg Nesterov bool ret = __skip_sstep(auprobe, regs);
10603a4664aaSOleg Nesterov if (ret && (regs->flags & X86_EFLAGS_TF))
10613a4664aaSOleg Nesterov send_sig(SIGTRAP, current, 0);
10623a4664aaSOleg Nesterov return ret;
10633a4664aaSOleg Nesterov }
1064791eca10SAnton Arapov
1065791eca10SAnton Arapov unsigned long
arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr,struct pt_regs * regs)1066791eca10SAnton Arapov arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, struct pt_regs *regs)
1067791eca10SAnton Arapov {
10689212ec7dSSebastian Mayr int rasize = sizeof_long(regs), nleft;
1069791eca10SAnton Arapov unsigned long orig_ret_vaddr = 0; /* clear high bits for 32-bit apps */
1070791eca10SAnton Arapov
10718faaed1bSOleg Nesterov if (copy_from_user(&orig_ret_vaddr, (void __user *)regs->sp, rasize))
1072791eca10SAnton Arapov return -1;
1073791eca10SAnton Arapov
1074791eca10SAnton Arapov /* check whether address has been already hijacked */
1075791eca10SAnton Arapov if (orig_ret_vaddr == trampoline_vaddr)
1076791eca10SAnton Arapov return orig_ret_vaddr;
1077791eca10SAnton Arapov
10788faaed1bSOleg Nesterov nleft = copy_to_user((void __user *)regs->sp, &trampoline_vaddr, rasize);
1079*27465601SJiri Olsa if (likely(!nleft)) {
1080*27465601SJiri Olsa if (shstk_update_last_frame(trampoline_vaddr)) {
1081*27465601SJiri Olsa force_sig(SIGSEGV);
1082*27465601SJiri Olsa return -1;
1083*27465601SJiri Olsa }
1084791eca10SAnton Arapov return orig_ret_vaddr;
1085*27465601SJiri Olsa }
1086791eca10SAnton Arapov
10878faaed1bSOleg Nesterov if (nleft != rasize) {
10881de392f5SJoe Perches pr_err("return address clobbered: pid=%d, %%sp=%#lx, %%ip=%#lx\n",
10891de392f5SJoe Perches current->pid, regs->sp, regs->ip);
1090791eca10SAnton Arapov
10913cf5d076SEric W. Biederman force_sig(SIGSEGV);
1092791eca10SAnton Arapov }
1093791eca10SAnton Arapov
1094791eca10SAnton Arapov return -1;
1095791eca10SAnton Arapov }
10967b868e48SOleg Nesterov
arch_uretprobe_is_alive(struct return_instance * ret,enum rp_check ctx,struct pt_regs * regs)109786dcb702SOleg Nesterov bool arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check ctx,
109886dcb702SOleg Nesterov struct pt_regs *regs)
10997b868e48SOleg Nesterov {
1100db087ef6SOleg Nesterov if (ctx == RP_CHECK_CALL) /* sp was just decremented by "call" insn */
1101db087ef6SOleg Nesterov return regs->sp < ret->stack;
1102db087ef6SOleg Nesterov else
11037b868e48SOleg Nesterov return regs->sp <= ret->stack;
11047b868e48SOleg Nesterov }
1105