linux-unwind.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /* DWARF2 EH unwinding support for PowerPC and PowerPC64 Linux.
  2. Copyright (C) 2004-2022 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  11. License for more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #define R_LR 65
  20. #define R_CR2 70
  21. #define R_CR3 71
  22. #define R_CR4 72
  23. #define R_VR0 77
  24. #define R_VRSAVE 109
  25. #ifdef __powerpc64__
  26. #if _CALL_ELF == 2
  27. #define TOC_SAVE_SLOT 24
  28. #else
  29. #define TOC_SAVE_SLOT 40
  30. #endif
  31. #endif
  32. struct gcc_vregs
  33. {
  34. __attribute__ ((vector_size (16))) int vr[32];
  35. #ifdef __powerpc64__
  36. unsigned int pad1[3];
  37. unsigned int vscr;
  38. unsigned int vsave;
  39. unsigned int pad2[3];
  40. #else
  41. unsigned int vsave;
  42. unsigned int pad[2];
  43. unsigned int vscr;
  44. #endif
  45. };
  46. struct gcc_regs
  47. {
  48. unsigned long gpr[32];
  49. unsigned long nip;
  50. unsigned long msr;
  51. unsigned long orig_gpr3;
  52. unsigned long ctr;
  53. unsigned long link;
  54. unsigned long xer;
  55. unsigned long ccr;
  56. unsigned long softe;
  57. unsigned long trap;
  58. unsigned long dar;
  59. unsigned long dsisr;
  60. unsigned long result;
  61. unsigned long pad1[4];
  62. double fpr[32];
  63. unsigned int pad2;
  64. unsigned int fpscr;
  65. #ifdef __powerpc64__
  66. struct gcc_vregs *vp;
  67. #else
  68. unsigned int pad3[2];
  69. #endif
  70. struct gcc_vregs vregs;
  71. };
  72. struct gcc_ucontext
  73. {
  74. #ifdef __powerpc64__
  75. unsigned long pad[28];
  76. #else
  77. unsigned long pad[12];
  78. #endif
  79. struct gcc_regs *regs;
  80. struct gcc_regs rsave;
  81. };
  82. #ifdef __powerpc64__
  83. enum { SIGNAL_FRAMESIZE = 128 };
  84. struct rt_sigframe {
  85. char gap[SIGNAL_FRAMESIZE];
  86. struct gcc_ucontext uc;
  87. unsigned long pad[2];
  88. int tramp[6];
  89. void *pinfo;
  90. struct gcc_ucontext *puc;
  91. };
  92. /* If PC is at a sigreturn trampoline, return a pointer to the
  93. regs. Otherwise return NULL. */
  94. static struct gcc_regs *
  95. get_regs (struct _Unwind_Context *context)
  96. {
  97. const unsigned int *pc = context->ra;
  98. /* addi r1, r1, 128; li r0, 0x0077; sc (sigreturn) */
  99. /* addi r1, r1, 128; li r0, 0x00AC; sc (rt_sigreturn) */
  100. if (pc[0] != 0x38210000 + SIGNAL_FRAMESIZE || pc[2] != 0x44000002)
  101. return NULL;
  102. if (pc[1] == 0x38000077)
  103. {
  104. struct sigframe {
  105. char gap[SIGNAL_FRAMESIZE];
  106. unsigned long pad[7];
  107. struct gcc_regs *regs;
  108. } *frame = (struct sigframe *) context->cfa;
  109. return frame->regs;
  110. }
  111. else if (pc[1] == 0x380000AC)
  112. {
  113. #if _CALL_ELF != 2
  114. /* These old kernel versions never supported ELFv2. */
  115. /* This works for 2.4 kernels, but not for 2.6 kernels with vdso
  116. because pc isn't pointing into the stack. Can be removed when
  117. no one is running 2.4.19 or 2.4.20, the first two ppc64
  118. kernels released. */
  119. const struct rt_sigframe_24 {
  120. int tramp[6];
  121. void *pinfo;
  122. struct gcc_ucontext *puc;
  123. } *frame24 = (const struct rt_sigframe_24 *) context->ra;
  124. /* Test for magic value in *puc of vdso. */
  125. if ((long) frame24->puc != -21 * 8)
  126. return frame24->puc->regs;
  127. else
  128. #endif
  129. {
  130. /* This works for 2.4.21 and later kernels. */
  131. struct rt_sigframe *frame = (struct rt_sigframe *) context->cfa;
  132. return frame->uc.regs;
  133. }
  134. }
  135. return NULL;
  136. }
  137. #else /* !__powerpc64__ */
  138. enum { SIGNAL_FRAMESIZE = 64 };
  139. struct rt_sigframe {
  140. char gap[SIGNAL_FRAMESIZE + 16];
  141. char siginfo[128];
  142. struct gcc_ucontext uc;
  143. };
  144. static struct gcc_regs *
  145. get_regs (struct _Unwind_Context *context)
  146. {
  147. const unsigned int *pc = context->ra;
  148. /* li r0, 0x7777; sc (sigreturn old) */
  149. /* li r0, 0x0077; sc (sigreturn new) */
  150. /* li r0, 0x6666; sc (rt_sigreturn old) */
  151. /* li r0, 0x00AC; sc (rt_sigreturn new) */
  152. if (pc[1] != 0x44000002)
  153. return NULL;
  154. if (pc[0] == 0x38007777 || pc[0] == 0x38000077)
  155. {
  156. struct sigframe {
  157. char gap[SIGNAL_FRAMESIZE];
  158. unsigned long pad[7];
  159. struct gcc_regs *regs;
  160. } *frame = (struct sigframe *) context->cfa;
  161. return frame->regs;
  162. }
  163. else if (pc[0] == 0x38006666 || pc[0] == 0x380000AC)
  164. {
  165. struct rt_sigframe *frame = (struct rt_sigframe *) context->cfa;
  166. return frame->uc.regs;
  167. }
  168. return NULL;
  169. }
  170. #endif
  171. /* Do code reading to identify a signal frame, and set the frame
  172. state data appropriately. See unwind-dw2.c for the structs. */
  173. #define MD_FALLBACK_FRAME_STATE_FOR ppc_fallback_frame_state
  174. static _Unwind_Reason_Code
  175. ppc_fallback_frame_state (struct _Unwind_Context *context,
  176. _Unwind_FrameState *fs)
  177. {
  178. struct gcc_regs *regs = get_regs (context);
  179. struct gcc_vregs *vregs;
  180. long cr_offset;
  181. long new_cfa;
  182. int i;
  183. if (regs == NULL)
  184. return _URC_NORMAL_STOP;
  185. new_cfa = regs->gpr[__LIBGCC_STACK_POINTER_REGNUM__];
  186. fs->regs.cfa_how = CFA_REG_OFFSET;
  187. fs->regs.cfa_reg = __LIBGCC_STACK_POINTER_REGNUM__;
  188. fs->regs.cfa_offset = new_cfa - (long) context->cfa;
  189. #ifdef __powerpc64__
  190. fs->regs.reg[2].how = REG_SAVED_OFFSET;
  191. fs->regs.reg[2].loc.offset = (long) &regs->gpr[2] - new_cfa;
  192. #endif
  193. for (i = 14; i < 32; i++)
  194. {
  195. fs->regs.reg[i].how = REG_SAVED_OFFSET;
  196. fs->regs.reg[i].loc.offset = (long) &regs->gpr[i] - new_cfa;
  197. }
  198. /* The CR is saved in the low 32 bits of regs->ccr. */
  199. cr_offset = (long) &regs->ccr - new_cfa;
  200. #ifndef __LITTLE_ENDIAN__
  201. cr_offset += sizeof (long) - 4;
  202. #endif
  203. /* In the ELFv1 ABI, CR2 stands in for the whole CR. */
  204. fs->regs.reg[R_CR2].how = REG_SAVED_OFFSET;
  205. fs->regs.reg[R_CR2].loc.offset = cr_offset;
  206. #if _CALL_ELF == 2
  207. /* In the ELFv2 ABI, every CR field has a separate CFI entry. */
  208. fs->regs.reg[R_CR3].how = REG_SAVED_OFFSET;
  209. fs->regs.reg[R_CR3].loc.offset = cr_offset;
  210. fs->regs.reg[R_CR4].how = REG_SAVED_OFFSET;
  211. fs->regs.reg[R_CR4].loc.offset = cr_offset;
  212. #endif
  213. fs->regs.reg[R_LR].how = REG_SAVED_OFFSET;
  214. fs->regs.reg[R_LR].loc.offset = (long) &regs->link - new_cfa;
  215. fs->regs.reg[ARG_POINTER_REGNUM].how = REG_SAVED_OFFSET;
  216. fs->regs.reg[ARG_POINTER_REGNUM].loc.offset = (long) &regs->nip - new_cfa;
  217. fs->retaddr_column = ARG_POINTER_REGNUM;
  218. fs->signal_frame = 1;
  219. /* If we have a FPU... */
  220. for (i = 14; i < 32; i++)
  221. {
  222. fs->regs.reg[i + 32].how = REG_SAVED_OFFSET;
  223. fs->regs.reg[i + 32].loc.offset = (long) &regs->fpr[i] - new_cfa;
  224. }
  225. /* If we have a VMX unit... */
  226. #ifdef __powerpc64__
  227. vregs = regs->vp;
  228. #else
  229. vregs = &regs->vregs;
  230. #endif
  231. if (regs->msr & (1 << 25))
  232. {
  233. for (i = 20; i < 32; i++)
  234. {
  235. fs->regs.reg[i + R_VR0].how = REG_SAVED_OFFSET;
  236. fs->regs.reg[i + R_VR0].loc.offset = (long) &vregs->vr[i] - new_cfa;
  237. }
  238. }
  239. fs->regs.reg[R_VRSAVE].how = REG_SAVED_OFFSET;
  240. fs->regs.reg[R_VRSAVE].loc.offset = (long) &vregs->vsave - new_cfa;
  241. /* If we have SPE register high-parts... we check at compile-time to
  242. avoid expanding the code for all other PowerPC. */
  243. #ifdef __SPE__
  244. for (i = 14; i < 32; i++)
  245. {
  246. fs->regs.reg[i + FIRST_SPE_HIGH_REGNO - 4].how = REG_SAVED_OFFSET;
  247. fs->regs.reg[i + FIRST_SPE_HIGH_REGNO - 4].loc.offset
  248. = (long) &regs->vregs - new_cfa + 4 * i;
  249. }
  250. #endif
  251. return _URC_NO_REASON;
  252. }
  253. #define MD_FROB_UPDATE_CONTEXT frob_update_context
  254. static void
  255. frob_update_context (struct _Unwind_Context *context, _Unwind_FrameState *fs ATTRIBUTE_UNUSED)
  256. {
  257. const unsigned int *pc = (const unsigned int *) context->ra;
  258. /* Fix up for 2.6.12 - 2.6.16 Linux kernels that have vDSO, but don't
  259. have S flag in it. */
  260. #ifdef __powerpc64__
  261. /* addi r1, r1, 128; li r0, 0x0077; sc (sigreturn) */
  262. /* addi r1, r1, 128; li r0, 0x00AC; sc (rt_sigreturn) */
  263. if (pc[0] == 0x38210000 + SIGNAL_FRAMESIZE
  264. && (pc[1] == 0x38000077 || pc[1] == 0x380000AC)
  265. && pc[2] == 0x44000002)
  266. _Unwind_SetSignalFrame (context, 1);
  267. #else
  268. /* li r0, 0x7777; sc (sigreturn old) */
  269. /* li r0, 0x0077; sc (sigreturn new) */
  270. /* li r0, 0x6666; sc (rt_sigreturn old) */
  271. /* li r0, 0x00AC; sc (rt_sigreturn new) */
  272. if ((pc[0] == 0x38007777 || pc[0] == 0x38000077
  273. || pc[0] == 0x38006666 || pc[0] == 0x380000AC)
  274. && pc[1] == 0x44000002)
  275. _Unwind_SetSignalFrame (context, 1);
  276. #endif
  277. #ifdef __powerpc64__
  278. if (fs->regs.reg[2].how == REG_UNSAVED)
  279. {
  280. /* If the current unwind info (FS) does not contain explicit info
  281. saving R2, then we have to do a minor amount of code reading to
  282. figure out if it was saved. The big problem here is that the
  283. code that does the save/restore is generated by the linker, so
  284. we have no good way to determine at compile time what to do. */
  285. if (pc[0] == 0xF8410000 + TOC_SAVE_SLOT
  286. #if _CALL_ELF != 2
  287. /* The ELFv2 linker never generates the old PLT stub form. */
  288. || ((pc[0] & 0xFFFF0000) == 0x3D820000
  289. && pc[1] == 0xF8410000 + TOC_SAVE_SLOT)
  290. #endif
  291. )
  292. {
  293. /* We are in a plt call stub or r2 adjusting long branch stub,
  294. before r2 has been saved. Keep REG_UNSAVED. */
  295. }
  296. else
  297. {
  298. unsigned int *insn
  299. = (unsigned int *) _Unwind_GetGR (context, R_LR);
  300. if (insn && *insn == 0xE8410000 + TOC_SAVE_SLOT)
  301. _Unwind_SetGRPtr (context, 2, context->cfa + TOC_SAVE_SLOT);
  302. #if _CALL_ELF != 2
  303. /* ELFv2 does not use this function pointer call sequence. */
  304. else if (pc[0] == 0x4E800421
  305. && pc[1] == 0xE8410000 + TOC_SAVE_SLOT)
  306. {
  307. /* We are at the bctrl instruction in a call via function
  308. pointer. gcc always emits the load of the new R2 just
  309. before the bctrl so this is the first and only place
  310. we need to use the stored R2. */
  311. _Unwind_Word sp = _Unwind_GetGR (context, 1);
  312. _Unwind_SetGRPtr (context, 2, (void *)(sp + TOC_SAVE_SLOT));
  313. }
  314. #endif
  315. }
  316. }
  317. #endif
  318. }
  319. #define MD_BACKCHAIN_FALLBACK ppc_backchain_fallback
  320. struct trace_arg
  321. {
  322. /* Stores the list of addresses. */
  323. void **array;
  324. struct unwind_link *unwind_link;
  325. _Unwind_Word cfa;
  326. /* Number of addresses currently stored. */
  327. int count;
  328. /* Maximum number of addresses. */
  329. int size;
  330. };
  331. /* This is the stack layout we see with every stack frame.
  332. Note that every routine is required by the ABI to lay out the stack
  333. like this.
  334. +----------------+ +-----------------+
  335. %r1 -> | previous frame--------> | previous frame--->... --> NULL
  336. | | | |
  337. | cr save | | cr save |
  338. | | | |
  339. | (unused) | | lr save |
  340. +----------------+ +-----------------+
  341. The CR save is only present on 64-bit ABIs.
  342. */
  343. struct frame_layout
  344. {
  345. struct frame_layout *backchain;
  346. #ifdef __powerpc64__
  347. long int cr_save;
  348. #endif
  349. void *lr_save;
  350. };
  351. static void
  352. ppc_backchain_fallback (struct _Unwind_Context *context, void *a)
  353. {
  354. struct frame_layout *current;
  355. struct trace_arg *arg = a;
  356. int count;
  357. /* Get the last address computed. */
  358. current = context->cfa;
  359. /* If the trace CFA is not the context CFA the backtrace is done. */
  360. if (arg == NULL || arg->cfa != current)
  361. return;
  362. /* Start with next address. */
  363. current = current->backchain;
  364. for (count = arg->count; current != NULL; current = current->backchain)
  365. {
  366. arg->array[count] = current->lr_save;
  367. /* Check if the symbol is the signal trampoline and get the interrupted
  368. symbol address from the trampoline saved area. */
  369. context->ra = current->lr_save;
  370. if (current->lr_save && get_regs (context))
  371. {
  372. struct rt_sigframe *sigframe = (struct rt_sigframe *) current;
  373. if (count + 1 == arg->size)
  374. break;
  375. arg->array[++count] = (void *) sigframe->uc.rsave.nip;
  376. current = (void *) sigframe->uc.rsave.gpr[1];
  377. }
  378. if (count++ >= arg->size)
  379. break;
  380. }
  381. arg->count = count-1;
  382. }