1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _ASM_X86_PTRACE_H 3 #define _ASM_X86_PTRACE_H 4 5 #include <asm/segment.h> 6 #include <asm/page_types.h> 7 #include <uapi/asm/ptrace.h> 8 9 #ifndef __ASSEMBLY__ 10 #ifdef __i386__ 11 12 struct pt_regs { 13 /* 14 * NB: 32-bit x86 CPUs are inconsistent as what happens in the 15 * following cases (where %seg represents a segment register): 16 * 17 * - pushl %seg: some do a 16-bit write and leave the high 18 * bits alone 19 * - movl %seg, [mem]: some do a 16-bit write despite the movl 20 * - IDT entry: some (e.g. 486) will leave the high bits of CS 21 * and (if applicable) SS undefined. 22 * 23 * Fortunately, x86-32 doesn't read the high bits on POP or IRET, 24 * so we can just treat all of the segment registers as 16-bit 25 * values. 26 */ 27 unsigned long bx; 28 unsigned long cx; 29 unsigned long dx; 30 unsigned long si; 31 unsigned long di; 32 unsigned long bp; 33 unsigned long ax; 34 unsigned short ds; 35 unsigned short __dsh; 36 unsigned short es; 37 unsigned short __esh; 38 unsigned short fs; 39 unsigned short __fsh; 40 /* On interrupt, gs and __gsh store the vector number. */ 41 unsigned short gs; 42 unsigned short __gsh; 43 /* On interrupt, this is the error code. */ 44 unsigned long orig_ax; 45 unsigned long ip; 46 unsigned short cs; 47 unsigned short __csh; 48 unsigned long flags; 49 unsigned long sp; 50 unsigned short ss; 51 unsigned short __ssh; 52 }; 53 54 #else /* __i386__ */ 55 56 struct pt_regs { 57 /* 58 * C ABI says these regs are callee-preserved. They aren't saved on kernel entry 59 * unless syscall needs a complete, fully filled "struct pt_regs". 60 */ 61 unsigned long r15; 62 unsigned long r14; 63 unsigned long r13; 64 unsigned long r12; 65 unsigned long bp; 66 unsigned long bx; 67 /* These regs are callee-clobbered. Always saved on kernel entry. */ 68 unsigned long r11; 69 unsigned long r10; 70 unsigned long r9; 71 unsigned long r8; 72 unsigned long ax; 73 unsigned long cx; 74 unsigned long dx; 75 unsigned long si; 76 unsigned long di; 77 /* 78 * On syscall entry, this is syscall#. On CPU exception, this is error code. 79 * On hw interrupt, it's IRQ number: 80 */ 81 unsigned long orig_ax; 82 /* Return frame for iretq */ 83 unsigned long ip; 84 unsigned long cs; 85 unsigned long flags; 86 unsigned long sp; 87 unsigned long ss; 88 /* top of stack page */ 89 }; 90 91 #endif /* !__i386__ */ 92 93 #ifdef CONFIG_PARAVIRT 94 #include <asm/paravirt_types.h> 95 #endif 96 97 struct cpuinfo_x86; 98 struct task_struct; 99 100 extern unsigned long profile_pc(struct pt_regs *regs); 101 #define profile_pc profile_pc 102 103 extern unsigned long 104 convert_ip_to_linear(struct task_struct *child, struct pt_regs *regs); 105 extern void send_sigtrap(struct pt_regs *regs, int error_code, int si_code); 106 107 108 static inline unsigned long regs_return_value(struct pt_regs *regs) 109 { 110 return regs->ax; 111 } 112 113 static inline void regs_set_return_value(struct pt_regs *regs, unsigned long rc) 114 { 115 regs->ax = rc; 116 } 117 118 /* 119 * user_mode(regs) determines whether a register set came from user 120 * mode. On x86_32, this is true if V8086 mode was enabled OR if the 121 * register set was from protected mode with RPL-3 CS value. This 122 * tricky test checks that with one comparison. 123 * 124 * On x86_64, vm86 mode is mercifully nonexistent, and we don't need 125 * the extra check. 126 */ 127 static inline int user_mode(struct pt_regs *regs) 128 { 129 #ifdef CONFIG_X86_32 130 return ((regs->cs & SEGMENT_RPL_MASK) | (regs->flags & X86_VM_MASK)) >= USER_RPL; 131 #else 132 return !!(regs->cs & 3); 133 #endif 134 } 135 136 static inline int v8086_mode(struct pt_regs *regs) 137 { 138 #ifdef CONFIG_X86_32 139 return (regs->flags & X86_VM_MASK); 140 #else 141 return 0; /* No V86 mode support in long mode */ 142 #endif 143 } 144 145 static inline bool user_64bit_mode(struct pt_regs *regs) 146 { 147 #ifdef CONFIG_X86_64 148 #ifndef CONFIG_PARAVIRT_XXL 149 /* 150 * On non-paravirt systems, this is the only long mode CPL 3 151 * selector. We do not allow long mode selectors in the LDT. 152 */ 153 return regs->cs == __USER_CS; 154 #else 155 /* Headers are too twisted for this to go in paravirt.h. */ 156 return regs->cs == __USER_CS || regs->cs == pv_info.extra_user_64bit_cs; 157 #endif 158 #else /* !CONFIG_X86_64 */ 159 return false; 160 #endif 161 } 162 163 #ifdef CONFIG_X86_64 164 #define current_user_stack_pointer() current_pt_regs()->sp 165 #define compat_user_stack_pointer() current_pt_regs()->sp 166 #endif 167 168 #ifdef CONFIG_X86_32 169 extern unsigned long kernel_stack_pointer(struct pt_regs *regs); 170 #else 171 static inline unsigned long kernel_stack_pointer(struct pt_regs *regs) 172 { 173 return regs->sp; 174 } 175 #endif 176 177 #define GET_IP(regs) ((regs)->ip) 178 #define GET_FP(regs) ((regs)->bp) 179 #define GET_USP(regs) ((regs)->sp) 180 181 #include <asm-generic/ptrace.h> 182 183 /* Query offset/name of register from its name/offset */ 184 extern int regs_query_register_offset(const char *name); 185 extern const char *regs_query_register_name(unsigned int offset); 186 #define MAX_REG_OFFSET (offsetof(struct pt_regs, ss)) 187 188 /** 189 * regs_get_register() - get register value from its offset 190 * @regs: pt_regs from which register value is gotten. 191 * @offset: offset number of the register. 192 * 193 * regs_get_register returns the value of a register. The @offset is the 194 * offset of the register in struct pt_regs address which specified by @regs. 195 * If @offset is bigger than MAX_REG_OFFSET, this returns 0. 196 */ 197 static inline unsigned long regs_get_register(struct pt_regs *regs, 198 unsigned int offset) 199 { 200 if (unlikely(offset > MAX_REG_OFFSET)) 201 return 0; 202 #ifdef CONFIG_X86_32 203 /* 204 * Traps from the kernel do not save sp and ss. 205 * Use the helper function to retrieve sp. 206 */ 207 if (offset == offsetof(struct pt_regs, sp) && 208 regs->cs == __KERNEL_CS) 209 return kernel_stack_pointer(regs); 210 211 /* The selector fields are 16-bit. */ 212 if (offset == offsetof(struct pt_regs, cs) || 213 offset == offsetof(struct pt_regs, ss) || 214 offset == offsetof(struct pt_regs, ds) || 215 offset == offsetof(struct pt_regs, es) || 216 offset == offsetof(struct pt_regs, fs) || 217 offset == offsetof(struct pt_regs, gs)) { 218 return *(u16 *)((unsigned long)regs + offset); 219 220 } 221 #endif 222 return *(unsigned long *)((unsigned long)regs + offset); 223 } 224 225 /** 226 * regs_within_kernel_stack() - check the address in the stack 227 * @regs: pt_regs which contains kernel stack pointer. 228 * @addr: address which is checked. 229 * 230 * regs_within_kernel_stack() checks @addr is within the kernel stack page(s). 231 * If @addr is within the kernel stack, it returns true. If not, returns false. 232 */ 233 static inline int regs_within_kernel_stack(struct pt_regs *regs, 234 unsigned long addr) 235 { 236 return ((addr & ~(THREAD_SIZE - 1)) == 237 (kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1))); 238 } 239 240 /** 241 * regs_get_kernel_stack_nth_addr() - get the address of the Nth entry on stack 242 * @regs: pt_regs which contains kernel stack pointer. 243 * @n: stack entry number. 244 * 245 * regs_get_kernel_stack_nth() returns the address of the @n th entry of the 246 * kernel stack which is specified by @regs. If the @n th entry is NOT in 247 * the kernel stack, this returns NULL. 248 */ 249 static inline unsigned long *regs_get_kernel_stack_nth_addr(struct pt_regs *regs, unsigned int n) 250 { 251 unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs); 252 253 addr += n; 254 if (regs_within_kernel_stack(regs, (unsigned long)addr)) 255 return addr; 256 else 257 return NULL; 258 } 259 260 /* To avoid include hell, we can't include uaccess.h */ 261 extern long probe_kernel_read(void *dst, const void *src, size_t size); 262 263 /** 264 * regs_get_kernel_stack_nth() - get Nth entry of the stack 265 * @regs: pt_regs which contains kernel stack pointer. 266 * @n: stack entry number. 267 * 268 * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which 269 * is specified by @regs. If the @n th entry is NOT in the kernel stack 270 * this returns 0. 271 */ 272 static inline unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, 273 unsigned int n) 274 { 275 unsigned long *addr; 276 unsigned long val; 277 long ret; 278 279 addr = regs_get_kernel_stack_nth_addr(regs, n); 280 if (addr) { 281 ret = probe_kernel_read(&val, addr, sizeof(val)); 282 if (!ret) 283 return val; 284 } 285 return 0; 286 } 287 288 /** 289 * regs_get_kernel_argument() - get Nth function argument in kernel 290 * @regs: pt_regs of that context 291 * @n: function argument number (start from 0) 292 * 293 * regs_get_argument() returns @n th argument of the function call. 294 * Note that this chooses most probably assignment, in some case 295 * it can be incorrect. 296 * This is expected to be called from kprobes or ftrace with regs 297 * where the top of stack is the return address. 298 */ 299 static inline unsigned long regs_get_kernel_argument(struct pt_regs *regs, 300 unsigned int n) 301 { 302 static const unsigned int argument_offs[] = { 303 #ifdef __i386__ 304 offsetof(struct pt_regs, ax), 305 offsetof(struct pt_regs, cx), 306 offsetof(struct pt_regs, dx), 307 #define NR_REG_ARGUMENTS 3 308 #else 309 offsetof(struct pt_regs, di), 310 offsetof(struct pt_regs, si), 311 offsetof(struct pt_regs, dx), 312 offsetof(struct pt_regs, cx), 313 offsetof(struct pt_regs, r8), 314 offsetof(struct pt_regs, r9), 315 #define NR_REG_ARGUMENTS 6 316 #endif 317 }; 318 319 if (n >= NR_REG_ARGUMENTS) { 320 n -= NR_REG_ARGUMENTS - 1; 321 return regs_get_kernel_stack_nth(regs, n); 322 } else 323 return regs_get_register(regs, argument_offs[n]); 324 } 325 326 #define arch_has_single_step() (1) 327 #ifdef CONFIG_X86_DEBUGCTLMSR 328 #define arch_has_block_step() (1) 329 #else 330 #define arch_has_block_step() (boot_cpu_data.x86 >= 6) 331 #endif 332 333 #define ARCH_HAS_USER_SINGLE_STEP_REPORT 334 335 /* 336 * When hitting ptrace_stop(), we cannot return using SYSRET because 337 * that does not restore the full CPU state, only a minimal set. The 338 * ptracer can change arbitrary register values, which is usually okay 339 * because the usual ptrace stops run off the signal delivery path which 340 * forces IRET; however, ptrace_event() stops happen in arbitrary places 341 * in the kernel and don't force IRET path. 342 * 343 * So force IRET path after a ptrace stop. 344 */ 345 #define arch_ptrace_stop_needed(code, info) \ 346 ({ \ 347 force_iret(); \ 348 false; \ 349 }) 350 351 struct user_desc; 352 extern int do_get_thread_area(struct task_struct *p, int idx, 353 struct user_desc __user *info); 354 extern int do_set_thread_area(struct task_struct *p, int idx, 355 struct user_desc __user *info, int can_allocate); 356 357 #endif /* !__ASSEMBLY__ */ 358 #endif /* _ASM_X86_PTRACE_H */ 359