H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 1 | #ifndef _ASM_X86_PTRACE_H |
| 2 | #define _ASM_X86_PTRACE_H |
Thomas Gleixner | 8fc37f2 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 3 | |
Vegard Nossum | 6330a30 | 2008-05-28 09:46:19 +0200 | [diff] [blame] | 4 | #include <asm/segment.h> |
Masami Hiramatsu | b1cf540 | 2009-08-13 16:34:44 -0400 | [diff] [blame] | 5 | #include <asm/page_types.h> |
David Howells | af170c5 | 2012-12-14 22:37:13 +0000 | [diff] [blame] | 6 | #include <uapi/asm/ptrace.h> |
Markus Metzger | eee3af4 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 7 | |
Thomas Gleixner | 8fc37f2 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 8 | #ifndef __ASSEMBLY__ |
Thomas Gleixner | 8fc37f2 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 9 | #ifdef __i386__ |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 10 | |
| 11 | struct pt_regs { |
Harvey Harrison | 92bc205 | 2008-02-08 12:09:56 -0800 | [diff] [blame] | 12 | unsigned long bx; |
| 13 | unsigned long cx; |
| 14 | unsigned long dx; |
| 15 | unsigned long si; |
| 16 | unsigned long di; |
| 17 | unsigned long bp; |
Harvey Harrison | 9902a70 | 2008-02-08 12:09:57 -0800 | [diff] [blame] | 18 | unsigned long ax; |
Harvey Harrison | 92bc205 | 2008-02-08 12:09:56 -0800 | [diff] [blame] | 19 | unsigned long ds; |
| 20 | unsigned long es; |
| 21 | unsigned long fs; |
Tejun Heo | ccbeed3 | 2009-02-09 22:17:40 +0900 | [diff] [blame] | 22 | unsigned long gs; |
Harvey Harrison | 9902a70 | 2008-02-08 12:09:57 -0800 | [diff] [blame] | 23 | unsigned long orig_ax; |
Harvey Harrison | 92bc205 | 2008-02-08 12:09:56 -0800 | [diff] [blame] | 24 | unsigned long ip; |
| 25 | unsigned long cs; |
| 26 | unsigned long flags; |
| 27 | unsigned long sp; |
| 28 | unsigned long ss; |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 29 | }; |
Thomas Gleixner | 8fc37f2 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 30 | |
Thomas Gleixner | 8fc37f2 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 31 | #else /* __i386__ */ |
| 32 | |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 33 | struct pt_regs { |
Denys Vlasenko | e90e147 | 2015-02-26 14:40:28 -0800 | [diff] [blame] | 34 | /* |
| 35 | * C ABI says these regs are callee-preserved. They aren't saved on kernel entry |
| 36 | * unless syscall needs a complete, fully filled "struct pt_regs". |
| 37 | */ |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 38 | unsigned long r15; |
| 39 | unsigned long r14; |
| 40 | unsigned long r13; |
| 41 | unsigned long r12; |
| 42 | unsigned long bp; |
| 43 | unsigned long bx; |
Denys Vlasenko | e90e147 | 2015-02-26 14:40:28 -0800 | [diff] [blame] | 44 | /* These regs are callee-clobbered. Always saved on kernel entry. */ |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 45 | unsigned long r11; |
| 46 | unsigned long r10; |
| 47 | unsigned long r9; |
| 48 | unsigned long r8; |
| 49 | unsigned long ax; |
| 50 | unsigned long cx; |
| 51 | unsigned long dx; |
| 52 | unsigned long si; |
| 53 | unsigned long di; |
Denys Vlasenko | e90e147 | 2015-02-26 14:40:28 -0800 | [diff] [blame] | 54 | /* |
| 55 | * On syscall entry, this is syscall#. On CPU exception, this is error code. |
| 56 | * On hw interrupt, it's IRQ number: |
| 57 | */ |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 58 | unsigned long orig_ax; |
Denys Vlasenko | e90e147 | 2015-02-26 14:40:28 -0800 | [diff] [blame] | 59 | /* Return frame for iretq */ |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 60 | unsigned long ip; |
| 61 | unsigned long cs; |
| 62 | unsigned long flags; |
| 63 | unsigned long sp; |
| 64 | unsigned long ss; |
| 65 | /* top of stack page */ |
| 66 | }; |
Thomas Gleixner | 8fc37f2 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 67 | |
Thomas Gleixner | 8fc37f2 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 68 | #endif /* !__i386__ */ |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame] | 69 | |
Andy Lutomirski | 318f5a2 | 2011-08-03 09:31:53 -0400 | [diff] [blame] | 70 | #ifdef CONFIG_PARAVIRT |
| 71 | #include <asm/paravirt_types.h> |
| 72 | #endif |
Harvey Harrison | dbe3533 | 2008-01-30 13:33:16 +0100 | [diff] [blame] | 73 | |
Markus Metzger | 93fa763 | 2008-04-08 11:01:58 +0200 | [diff] [blame] | 74 | struct cpuinfo_x86; |
Harvey Harrison | dbe3533 | 2008-01-30 13:33:16 +0100 | [diff] [blame] | 75 | struct task_struct; |
| 76 | |
Harvey Harrison | dbe3533 | 2008-01-30 13:33:16 +0100 | [diff] [blame] | 77 | extern unsigned long profile_pc(struct pt_regs *regs); |
Mike Frysinger | c46dd6b | 2011-05-26 16:25:43 -0700 | [diff] [blame] | 78 | #define profile_pc profile_pc |
Harvey Harrison | dbe3533 | 2008-01-30 13:33:16 +0100 | [diff] [blame] | 79 | |
| 80 | extern unsigned long |
| 81 | convert_ip_to_linear(struct task_struct *child, struct pt_regs *regs); |
Joe Perches | 72f74fa2 | 2008-03-23 01:03:17 -0700 | [diff] [blame] | 82 | extern void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, |
Srinivasa Ds | da654b7 | 2008-09-23 15:23:52 +0530 | [diff] [blame] | 83 | int error_code, int si_code); |
Hiroshi Shimamoto | 72fa50f | 2008-09-05 16:27:11 -0700 | [diff] [blame] | 84 | |
Andy Lutomirski | e0ffbaa | 2014-09-05 15:13:54 -0700 | [diff] [blame] | 85 | |
| 86 | extern unsigned long syscall_trace_enter_phase1(struct pt_regs *, u32 arch); |
| 87 | extern long syscall_trace_enter_phase2(struct pt_regs *, u32 arch, |
| 88 | unsigned long phase1_result); |
| 89 | |
Jaswinder Singh | c1686ae | 2008-07-21 22:35:38 +0530 | [diff] [blame] | 90 | extern long syscall_trace_enter(struct pt_regs *); |
| 91 | extern void syscall_trace_leave(struct pt_regs *); |
| 92 | |
Harvey Harrison | 9902a70 | 2008-02-08 12:09:57 -0800 | [diff] [blame] | 93 | static inline unsigned long regs_return_value(struct pt_regs *regs) |
| 94 | { |
| 95 | return regs->ax; |
| 96 | } |
Harvey Harrison | dbe3533 | 2008-01-30 13:33:16 +0100 | [diff] [blame] | 97 | |
Roland McGrath | 7f23234 | 2008-01-30 13:30:48 +0100 | [diff] [blame] | 98 | /* |
Andy Lutomirski | efa7045 | 2015-03-18 18:33:32 -0700 | [diff] [blame] | 99 | * user_mode(regs) determines whether a register set came from user |
| 100 | * mode. On x86_32, this is true if V8086 mode was enabled OR if the |
| 101 | * register set was from protected mode with RPL-3 CS value. This |
| 102 | * tricky test checks that with one comparison. |
| 103 | * |
| 104 | * On x86_64, vm86 mode is mercifully nonexistent, and we don't need |
| 105 | * the extra check. |
Harvey Harrison | 90d43d7 | 2008-01-30 13:33:16 +0100 | [diff] [blame] | 106 | */ |
| 107 | static inline int user_mode(struct pt_regs *regs) |
| 108 | { |
| 109 | #ifdef CONFIG_X86_32 |
| 110 | return (regs->cs & SEGMENT_RPL_MASK) == USER_RPL; |
| 111 | #else |
| 112 | return !!(regs->cs & 3); |
| 113 | #endif |
| 114 | } |
| 115 | |
Harvey Harrison | 90d43d7 | 2008-01-30 13:33:16 +0100 | [diff] [blame] | 116 | static inline int v8086_mode(struct pt_regs *regs) |
| 117 | { |
| 118 | #ifdef CONFIG_X86_32 |
gorcunov@gmail.com | 6b6891f | 2008-03-28 17:56:57 +0300 | [diff] [blame] | 119 | return (regs->flags & X86_VM_MASK); |
Harvey Harrison | 90d43d7 | 2008-01-30 13:33:16 +0100 | [diff] [blame] | 120 | #else |
| 121 | return 0; /* No V86 mode support in long mode */ |
| 122 | #endif |
| 123 | } |
| 124 | |
Andy Lutomirski | 318f5a2 | 2011-08-03 09:31:53 -0400 | [diff] [blame] | 125 | #ifdef CONFIG_X86_64 |
| 126 | static inline bool user_64bit_mode(struct pt_regs *regs) |
| 127 | { |
| 128 | #ifndef CONFIG_PARAVIRT |
| 129 | /* |
| 130 | * On non-paravirt systems, this is the only long mode CPL 3 |
| 131 | * selector. We do not allow long mode selectors in the LDT. |
| 132 | */ |
| 133 | return regs->cs == __USER_CS; |
| 134 | #else |
| 135 | /* Headers are too twisted for this to go in paravirt.h. */ |
| 136 | return regs->cs == __USER_CS || regs->cs == pv_info.extra_user_64bit_cs; |
| 137 | #endif |
| 138 | } |
Al Viro | 9b064fc | 2012-12-14 13:49:35 -0500 | [diff] [blame] | 139 | |
Denys Vlasenko | 263042e | 2015-03-09 19:39:23 +0100 | [diff] [blame] | 140 | #define current_user_stack_pointer() current_pt_regs()->sp |
| 141 | #define compat_user_stack_pointer() current_pt_regs()->sp |
Andy Lutomirski | 318f5a2 | 2011-08-03 09:31:53 -0400 | [diff] [blame] | 142 | #endif |
| 143 | |
Robert Richter | 1022623 | 2012-09-03 20:54:48 +0200 | [diff] [blame] | 144 | #ifdef CONFIG_X86_32 |
| 145 | extern unsigned long kernel_stack_pointer(struct pt_regs *regs); |
| 146 | #else |
Masami Hiramatsu | 7b6c6c7 | 2009-05-11 17:03:00 -0400 | [diff] [blame] | 147 | static inline unsigned long kernel_stack_pointer(struct pt_regs *regs) |
Harvey Harrison | 90d43d7 | 2008-01-30 13:33:16 +0100 | [diff] [blame] | 148 | { |
Harvey Harrison | 90d43d7 | 2008-01-30 13:33:16 +0100 | [diff] [blame] | 149 | return regs->sp; |
Harvey Harrison | 90d43d7 | 2008-01-30 13:33:16 +0100 | [diff] [blame] | 150 | } |
Robert Richter | 1022623 | 2012-09-03 20:54:48 +0200 | [diff] [blame] | 151 | #endif |
Harvey Harrison | 90d43d7 | 2008-01-30 13:33:16 +0100 | [diff] [blame] | 152 | |
Mike Frysinger | c46dd6b | 2011-05-26 16:25:43 -0700 | [diff] [blame] | 153 | #define GET_IP(regs) ((regs)->ip) |
| 154 | #define GET_FP(regs) ((regs)->bp) |
| 155 | #define GET_USP(regs) ((regs)->sp) |
Harvey Harrison | 90d43d7 | 2008-01-30 13:33:16 +0100 | [diff] [blame] | 156 | |
Mike Frysinger | c46dd6b | 2011-05-26 16:25:43 -0700 | [diff] [blame] | 157 | #include <asm-generic/ptrace.h> |
Roland McGrath | 68bd0f4 | 2008-04-18 17:08:44 -0700 | [diff] [blame] | 158 | |
Masami Hiramatsu | b1cf540 | 2009-08-13 16:34:44 -0400 | [diff] [blame] | 159 | /* Query offset/name of register from its name/offset */ |
| 160 | extern int regs_query_register_offset(const char *name); |
| 161 | extern const char *regs_query_register_name(unsigned int offset); |
| 162 | #define MAX_REG_OFFSET (offsetof(struct pt_regs, ss)) |
| 163 | |
| 164 | /** |
| 165 | * regs_get_register() - get register value from its offset |
| 166 | * @regs: pt_regs from which register value is gotten. |
| 167 | * @offset: offset number of the register. |
| 168 | * |
Masami Hiramatsu | e859cf8 | 2009-11-30 19:02:22 -0500 | [diff] [blame] | 169 | * regs_get_register returns the value of a register. The @offset is the |
| 170 | * offset of the register in struct pt_regs address which specified by @regs. |
Masami Hiramatsu | b1cf540 | 2009-08-13 16:34:44 -0400 | [diff] [blame] | 171 | * If @offset is bigger than MAX_REG_OFFSET, this returns 0. |
| 172 | */ |
| 173 | static inline unsigned long regs_get_register(struct pt_regs *regs, |
| 174 | unsigned int offset) |
| 175 | { |
| 176 | if (unlikely(offset > MAX_REG_OFFSET)) |
| 177 | return 0; |
Steven Rostedt | 6c8d8b3 | 2012-07-13 15:44:14 -0400 | [diff] [blame] | 178 | #ifdef CONFIG_X86_32 |
| 179 | /* |
| 180 | * Traps from the kernel do not save sp and ss. |
| 181 | * Use the helper function to retrieve sp. |
| 182 | */ |
| 183 | if (offset == offsetof(struct pt_regs, sp) && |
| 184 | regs->cs == __KERNEL_CS) |
| 185 | return kernel_stack_pointer(regs); |
| 186 | #endif |
Masami Hiramatsu | b1cf540 | 2009-08-13 16:34:44 -0400 | [diff] [blame] | 187 | return *(unsigned long *)((unsigned long)regs + offset); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * regs_within_kernel_stack() - check the address in the stack |
| 192 | * @regs: pt_regs which contains kernel stack pointer. |
| 193 | * @addr: address which is checked. |
| 194 | * |
Masami Hiramatsu | e859cf8 | 2009-11-30 19:02:22 -0500 | [diff] [blame] | 195 | * regs_within_kernel_stack() checks @addr is within the kernel stack page(s). |
Masami Hiramatsu | b1cf540 | 2009-08-13 16:34:44 -0400 | [diff] [blame] | 196 | * If @addr is within the kernel stack, it returns true. If not, returns false. |
| 197 | */ |
| 198 | static inline int regs_within_kernel_stack(struct pt_regs *regs, |
| 199 | unsigned long addr) |
| 200 | { |
| 201 | return ((addr & ~(THREAD_SIZE - 1)) == |
| 202 | (kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1))); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * regs_get_kernel_stack_nth() - get Nth entry of the stack |
| 207 | * @regs: pt_regs which contains kernel stack pointer. |
| 208 | * @n: stack entry number. |
| 209 | * |
| 210 | * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which |
Masami Hiramatsu | e859cf8 | 2009-11-30 19:02:22 -0500 | [diff] [blame] | 211 | * is specified by @regs. If the @n th entry is NOT in the kernel stack, |
Masami Hiramatsu | b1cf540 | 2009-08-13 16:34:44 -0400 | [diff] [blame] | 212 | * this returns 0. |
| 213 | */ |
| 214 | static inline unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, |
| 215 | unsigned int n) |
| 216 | { |
| 217 | unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs); |
| 218 | addr += n; |
| 219 | if (regs_within_kernel_stack(regs, (unsigned long)addr)) |
| 220 | return *addr; |
| 221 | else |
| 222 | return 0; |
| 223 | } |
| 224 | |
Roland McGrath | 7f23234 | 2008-01-30 13:30:48 +0100 | [diff] [blame] | 225 | #define arch_has_single_step() (1) |
Roland McGrath | 10faa81 | 2008-01-30 13:30:54 +0100 | [diff] [blame] | 226 | #ifdef CONFIG_X86_DEBUGCTLMSR |
| 227 | #define arch_has_block_step() (1) |
| 228 | #else |
| 229 | #define arch_has_block_step() (boot_cpu_data.x86 >= 6) |
| 230 | #endif |
| 231 | |
Oleg Nesterov | 7f38551 | 2009-12-15 16:47:20 -0800 | [diff] [blame] | 232 | #define ARCH_HAS_USER_SINGLE_STEP_INFO |
| 233 | |
Tejun Heo | b9cd18d | 2014-07-03 15:43:15 -0400 | [diff] [blame] | 234 | /* |
| 235 | * When hitting ptrace_stop(), we cannot return using SYSRET because |
| 236 | * that does not restore the full CPU state, only a minimal set. The |
| 237 | * ptracer can change arbitrary register values, which is usually okay |
| 238 | * because the usual ptrace stops run off the signal delivery path which |
| 239 | * forces IRET; however, ptrace_event() stops happen in arbitrary places |
| 240 | * in the kernel and don't force IRET path. |
| 241 | * |
| 242 | * So force IRET path after a ptrace stop. |
| 243 | */ |
| 244 | #define arch_ptrace_stop_needed(code, info) \ |
| 245 | ({ \ |
Brian Gerst | 1daeaa3 | 2015-03-21 18:54:21 -0400 | [diff] [blame] | 246 | force_iret(); \ |
Tejun Heo | b9cd18d | 2014-07-03 15:43:15 -0400 | [diff] [blame] | 247 | false; \ |
| 248 | }) |
| 249 | |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame] | 250 | struct user_desc; |
| 251 | extern int do_get_thread_area(struct task_struct *p, int idx, |
| 252 | struct user_desc __user *info); |
| 253 | extern int do_set_thread_area(struct task_struct *p, int idx, |
| 254 | struct user_desc __user *info, int can_allocate); |
| 255 | |
Thomas Gleixner | 8fc37f2 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 256 | #endif /* !__ASSEMBLY__ */ |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 257 | #endif /* _ASM_X86_PTRACE_H */ |