ptrace.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Copyright (C) 2001 PPC64 Team, IBM Corp
  4. *
  5. * This struct defines the way the registers are stored on the
  6. * kernel stack during a system call or other kernel entry.
  7. *
  8. * this should only contain volatile regs
  9. * since we can keep non-volatile in the thread_struct
  10. * should set this up when only volatiles are saved
  11. * by intr code.
  12. *
  13. * Since this is going on the stack, *CARE MUST BE TAKEN* to insure
  14. * that the overall structure is a multiple of 16 bytes in length.
  15. *
  16. * Note that the offsets of the fields in this struct correspond with
  17. * the PT_* values below. This simplifies arch/powerpc/kernel/ptrace.c.
  18. */
  19. #ifndef _ASM_POWERPC_PTRACE_H
  20. #define _ASM_POWERPC_PTRACE_H
  21. #include <linux/err.h>
  22. #include <uapi/asm/ptrace.h>
  23. #include <asm/asm-const.h>
  24. #include <asm/reg.h>
  25. #ifndef __ASSEMBLY__
  26. struct pt_regs
  27. {
  28. union {
  29. struct user_pt_regs user_regs;
  30. struct {
  31. unsigned long gpr[32];
  32. unsigned long nip;
  33. unsigned long msr;
  34. unsigned long orig_gpr3;
  35. unsigned long ctr;
  36. unsigned long link;
  37. unsigned long xer;
  38. unsigned long ccr;
  39. #ifdef CONFIG_PPC64
  40. unsigned long softe;
  41. #else
  42. unsigned long mq;
  43. #endif
  44. unsigned long trap;
  45. union {
  46. unsigned long dar;
  47. unsigned long dear;
  48. };
  49. union {
  50. unsigned long dsisr;
  51. unsigned long esr;
  52. };
  53. unsigned long result;
  54. };
  55. };
  56. #if defined(CONFIG_PPC64) || defined(CONFIG_PPC_KUAP)
  57. union {
  58. struct {
  59. #ifdef CONFIG_PPC64
  60. unsigned long ppr;
  61. unsigned long exit_result;
  62. #endif
  63. union {
  64. #ifdef CONFIG_PPC_KUAP
  65. unsigned long kuap;
  66. #endif
  67. #ifdef CONFIG_PPC_PKEY
  68. unsigned long amr;
  69. #endif
  70. };
  71. #ifdef CONFIG_PPC_PKEY
  72. unsigned long iamr;
  73. #endif
  74. };
  75. unsigned long __pad[4]; /* Maintain 16 byte interrupt stack alignment */
  76. };
  77. #endif
  78. #if defined(CONFIG_PPC32) && defined(CONFIG_BOOKE)
  79. struct { /* Must be a multiple of 16 bytes */
  80. unsigned long mas0;
  81. unsigned long mas1;
  82. unsigned long mas2;
  83. unsigned long mas3;
  84. unsigned long mas6;
  85. unsigned long mas7;
  86. unsigned long srr0;
  87. unsigned long srr1;
  88. unsigned long csrr0;
  89. unsigned long csrr1;
  90. unsigned long dsrr0;
  91. unsigned long dsrr1;
  92. };
  93. #endif
  94. };
  95. #endif
  96. #define STACK_FRAME_WITH_PT_REGS (STACK_FRAME_OVERHEAD + sizeof(struct pt_regs))
  97. // Always displays as "REGS" in memory dumps
  98. #ifdef CONFIG_CPU_BIG_ENDIAN
  99. #define STACK_FRAME_REGS_MARKER ASM_CONST(0x52454753)
  100. #else
  101. #define STACK_FRAME_REGS_MARKER ASM_CONST(0x53474552)
  102. #endif
  103. #ifdef __powerpc64__
  104. /*
  105. * Size of redzone that userspace is allowed to use below the stack
  106. * pointer. This is 288 in the 64-bit big-endian ELF ABI, and 512 in
  107. * the new ELFv2 little-endian ABI, so we allow the larger amount.
  108. *
  109. * For kernel code we allow a 288-byte redzone, in order to conserve
  110. * kernel stack space; gcc currently only uses 288 bytes, and will
  111. * hopefully allow explicit control of the redzone size in future.
  112. */
  113. #define USER_REDZONE_SIZE 512
  114. #define KERNEL_REDZONE_SIZE 288
  115. #define STACK_FRAME_OVERHEAD 112 /* size of minimum stack frame */
  116. #define STACK_FRAME_LR_SAVE 2 /* Location of LR in stack frame */
  117. #define STACK_INT_FRAME_SIZE (sizeof(struct pt_regs) + \
  118. STACK_FRAME_OVERHEAD + KERNEL_REDZONE_SIZE)
  119. #define STACK_FRAME_MARKER 12
  120. #ifdef CONFIG_PPC64_ELF_ABI_V2
  121. #define STACK_FRAME_MIN_SIZE 32
  122. #else
  123. #define STACK_FRAME_MIN_SIZE STACK_FRAME_OVERHEAD
  124. #endif
  125. /* Size of dummy stack frame allocated when calling signal handler. */
  126. #define __SIGNAL_FRAMESIZE 128
  127. #define __SIGNAL_FRAMESIZE32 64
  128. #else /* __powerpc64__ */
  129. #define USER_REDZONE_SIZE 0
  130. #define KERNEL_REDZONE_SIZE 0
  131. #define STACK_FRAME_OVERHEAD 16 /* size of minimum stack frame */
  132. #define STACK_FRAME_LR_SAVE 1 /* Location of LR in stack frame */
  133. #define STACK_INT_FRAME_SIZE (sizeof(struct pt_regs) + STACK_FRAME_OVERHEAD)
  134. #define STACK_FRAME_MARKER 2
  135. #define STACK_FRAME_MIN_SIZE STACK_FRAME_OVERHEAD
  136. /* Size of stack frame allocated when calling signal handler. */
  137. #define __SIGNAL_FRAMESIZE 64
  138. #endif /* __powerpc64__ */
  139. #ifndef __ASSEMBLY__
  140. #include <asm/paca.h>
  141. #ifdef CONFIG_SMP
  142. extern unsigned long profile_pc(struct pt_regs *regs);
  143. #else
  144. #define profile_pc(regs) instruction_pointer(regs)
  145. #endif
  146. long do_syscall_trace_enter(struct pt_regs *regs);
  147. void do_syscall_trace_leave(struct pt_regs *regs);
  148. static inline void set_return_regs_changed(void)
  149. {
  150. #ifdef CONFIG_PPC_BOOK3S_64
  151. local_paca->hsrr_valid = 0;
  152. local_paca->srr_valid = 0;
  153. #endif
  154. }
  155. static inline void regs_set_return_ip(struct pt_regs *regs, unsigned long ip)
  156. {
  157. regs->nip = ip;
  158. set_return_regs_changed();
  159. }
  160. static inline void regs_set_return_msr(struct pt_regs *regs, unsigned long msr)
  161. {
  162. regs->msr = msr;
  163. set_return_regs_changed();
  164. }
  165. static inline void regs_add_return_ip(struct pt_regs *regs, long offset)
  166. {
  167. regs_set_return_ip(regs, regs->nip + offset);
  168. }
  169. static inline unsigned long instruction_pointer(struct pt_regs *regs)
  170. {
  171. return regs->nip;
  172. }
  173. static inline void instruction_pointer_set(struct pt_regs *regs,
  174. unsigned long val)
  175. {
  176. regs_set_return_ip(regs, val);
  177. }
  178. static inline unsigned long user_stack_pointer(struct pt_regs *regs)
  179. {
  180. return regs->gpr[1];
  181. }
  182. static inline unsigned long frame_pointer(struct pt_regs *regs)
  183. {
  184. return 0;
  185. }
  186. #define user_mode(regs) (((regs)->msr & MSR_PR) != 0)
  187. #define force_successful_syscall_return() \
  188. do { \
  189. set_thread_flag(TIF_NOERROR); \
  190. } while(0)
  191. #define current_pt_regs() \
  192. ((struct pt_regs *)((unsigned long)task_stack_page(current) + THREAD_SIZE) - 1)
  193. /*
  194. * The 4 low bits (0xf) are available as flags to overload the trap word,
  195. * because interrupt vectors have minimum alignment of 0x10. TRAP_FLAGS_MASK
  196. * must cover the bits used as flags, including bit 0 which is used as the
  197. * "norestart" bit.
  198. */
  199. #ifdef __powerpc64__
  200. #define TRAP_FLAGS_MASK 0x1
  201. #else
  202. /*
  203. * On 4xx we use bit 1 in the trap word to indicate whether the exception
  204. * is a critical exception (1 means it is).
  205. */
  206. #define TRAP_FLAGS_MASK 0xf
  207. #define IS_CRITICAL_EXC(regs) (((regs)->trap & 2) != 0)
  208. #define IS_MCHECK_EXC(regs) (((regs)->trap & 4) != 0)
  209. #define IS_DEBUG_EXC(regs) (((regs)->trap & 8) != 0)
  210. #endif /* __powerpc64__ */
  211. #define TRAP(regs) ((regs)->trap & ~TRAP_FLAGS_MASK)
  212. static __always_inline void set_trap(struct pt_regs *regs, unsigned long val)
  213. {
  214. regs->trap = (regs->trap & TRAP_FLAGS_MASK) | (val & ~TRAP_FLAGS_MASK);
  215. }
  216. static inline bool trap_is_scv(struct pt_regs *regs)
  217. {
  218. return (IS_ENABLED(CONFIG_PPC_BOOK3S_64) && TRAP(regs) == 0x3000);
  219. }
  220. static inline bool trap_is_unsupported_scv(struct pt_regs *regs)
  221. {
  222. return IS_ENABLED(CONFIG_PPC_BOOK3S_64) && TRAP(regs) == 0x7ff0;
  223. }
  224. static inline bool trap_is_syscall(struct pt_regs *regs)
  225. {
  226. return (trap_is_scv(regs) || TRAP(regs) == 0xc00);
  227. }
  228. static inline bool trap_norestart(struct pt_regs *regs)
  229. {
  230. return regs->trap & 0x1;
  231. }
  232. static __always_inline void set_trap_norestart(struct pt_regs *regs)
  233. {
  234. regs->trap |= 0x1;
  235. }
  236. #define kernel_stack_pointer(regs) ((regs)->gpr[1])
  237. static inline int is_syscall_success(struct pt_regs *regs)
  238. {
  239. if (trap_is_scv(regs))
  240. return !IS_ERR_VALUE((unsigned long)regs->gpr[3]);
  241. else
  242. return !(regs->ccr & 0x10000000);
  243. }
  244. static inline long regs_return_value(struct pt_regs *regs)
  245. {
  246. if (trap_is_scv(regs))
  247. return regs->gpr[3];
  248. if (is_syscall_success(regs))
  249. return regs->gpr[3];
  250. else
  251. return -regs->gpr[3];
  252. }
  253. static inline void regs_set_return_value(struct pt_regs *regs, unsigned long rc)
  254. {
  255. regs->gpr[3] = rc;
  256. }
  257. static inline bool cpu_has_msr_ri(void)
  258. {
  259. return !IS_ENABLED(CONFIG_BOOKE_OR_40x);
  260. }
  261. static inline bool regs_is_unrecoverable(struct pt_regs *regs)
  262. {
  263. return unlikely(cpu_has_msr_ri() && !(regs->msr & MSR_RI));
  264. }
  265. static inline void regs_set_recoverable(struct pt_regs *regs)
  266. {
  267. if (cpu_has_msr_ri())
  268. regs_set_return_msr(regs, regs->msr | MSR_RI);
  269. }
  270. static inline void regs_set_unrecoverable(struct pt_regs *regs)
  271. {
  272. if (cpu_has_msr_ri())
  273. regs_set_return_msr(regs, regs->msr & ~MSR_RI);
  274. }
  275. #define arch_has_single_step() (1)
  276. #define arch_has_block_step() (true)
  277. #define ARCH_HAS_USER_SINGLE_STEP_REPORT
  278. /*
  279. * kprobe-based event tracer support
  280. */
  281. #include <linux/stddef.h>
  282. #include <linux/thread_info.h>
  283. extern int regs_query_register_offset(const char *name);
  284. extern const char *regs_query_register_name(unsigned int offset);
  285. #define MAX_REG_OFFSET (offsetof(struct pt_regs, dsisr))
  286. /**
  287. * regs_get_register() - get register value from its offset
  288. * @regs: pt_regs from which register value is gotten
  289. * @offset: offset number of the register.
  290. *
  291. * regs_get_register returns the value of a register whose offset from @regs.
  292. * The @offset is the offset of the register in struct pt_regs.
  293. * If @offset is bigger than MAX_REG_OFFSET, this returns 0.
  294. */
  295. static inline unsigned long regs_get_register(struct pt_regs *regs,
  296. unsigned int offset)
  297. {
  298. if (unlikely(offset > MAX_REG_OFFSET))
  299. return 0;
  300. return *(unsigned long *)((unsigned long)regs + offset);
  301. }
  302. /**
  303. * regs_within_kernel_stack() - check the address in the stack
  304. * @regs: pt_regs which contains kernel stack pointer.
  305. * @addr: address which is checked.
  306. *
  307. * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
  308. * If @addr is within the kernel stack, it returns true. If not, returns false.
  309. */
  310. static inline bool regs_within_kernel_stack(struct pt_regs *regs,
  311. unsigned long addr)
  312. {
  313. return ((addr & ~(THREAD_SIZE - 1)) ==
  314. (kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1)));
  315. }
  316. /**
  317. * regs_get_kernel_stack_nth() - get Nth entry of the stack
  318. * @regs: pt_regs which contains kernel stack pointer.
  319. * @n: stack entry number.
  320. *
  321. * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
  322. * is specified by @regs. If the @n th entry is NOT in the kernel stack,
  323. * this returns 0.
  324. */
  325. static inline unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs,
  326. unsigned int n)
  327. {
  328. unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
  329. addr += n;
  330. if (regs_within_kernel_stack(regs, (unsigned long)addr))
  331. return *addr;
  332. else
  333. return 0;
  334. }
  335. #endif /* __ASSEMBLY__ */
  336. #ifndef __powerpc64__
  337. /* We need PT_SOFTE defined at all time to avoid #ifdefs */
  338. #define PT_SOFTE PT_MQ
  339. #else /* __powerpc64__ */
  340. #define PT_FPSCR32 (PT_FPR0 + 2*32 + 1) /* each FP reg occupies 2 32-bit userspace slots */
  341. #define PT_VR0_32 164 /* each Vector reg occupies 4 slots in 32-bit */
  342. #define PT_VSCR_32 (PT_VR0 + 32*4 + 3)
  343. #define PT_VRSAVE_32 (PT_VR0 + 33*4)
  344. #define PT_VSR0_32 300 /* each VSR reg occupies 4 slots in 32-bit */
  345. #endif /* __powerpc64__ */
  346. #endif /* _ASM_POWERPC_PTRACE_H */