Gos-freebsd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /* libunwind - a platform-independent unwind library
  2. Copyright (C) 2010 Konstantin Belousov <kib@freebsd.org>
  3. This file is part of libunwind.
  4. Permission is hereby granted, free of charge, to any person obtaining
  5. a copy of this software and associated documentation files (the
  6. "Software"), to deal in the Software without restriction, including
  7. without limitation the rights to use, copy, modify, merge, publish,
  8. distribute, sublicense, and/or sell copies of the Software, and to
  9. permit persons to whom the Software is furnished to do so, subject to
  10. the following conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include <sys/types.h>
  24. #include <signal.h>
  25. #include <stddef.h>
  26. #include <ucontext.h>
  27. #include <machine/sigframe.h>
  28. #include "unwind_i.h"
  29. #include "offsets.h"
  30. PROTECTED int
  31. unw_is_signal_frame (unw_cursor_t *cursor)
  32. {
  33. struct cursor *c = (struct cursor *) cursor;
  34. unw_word_t w0, w1, w2, w3, w4, w5, ip;
  35. unw_addr_space_t as;
  36. unw_accessors_t *a;
  37. void *arg;
  38. int ret;
  39. as = c->dwarf.as;
  40. a = unw_get_accessors (as);
  41. arg = c->dwarf.as_arg;
  42. /* Check if EIP points at sigreturn() sequence. It can be:
  43. sigcode+4: from amd64 freebsd32 environment
  44. 8d 44 24 20 lea 0x20(%esp),%eax
  45. 50 push %eax
  46. b8 a1 01 00 00 mov $0x1a1,%eax
  47. 50 push %eax
  48. cd 80 int $0x80
  49. sigcode+4: from real i386
  50. 8d 44 24 20 lea 0x20(%esp),%eax
  51. 50 push %eax
  52. f7 40 54 00 02 00 testl $0x20000,0x54(%eax)
  53. 75 03 jne sigcode+21
  54. 8e 68 14 mov 0x14(%eax),%gs
  55. b8 a1 01 00 00 mov $0x1a1,%eax
  56. 50 push %eax
  57. cd 80 int $0x80
  58. freebsd4_sigcode+4:
  59. XXX
  60. osigcode:
  61. XXX
  62. */
  63. ip = c->dwarf.ip;
  64. ret = X86_SCF_NONE;
  65. c->sigcontext_format = ret;
  66. if ((*a->access_mem) (as, ip, &w0, 0, arg) < 0 ||
  67. (*a->access_mem) (as, ip + 4, &w1, 0, arg) < 0 ||
  68. (*a->access_mem) (as, ip + 8, &w2, 0, arg) < 0 ||
  69. (*a->access_mem) (as, ip + 12, &w3, 0, arg) < 0)
  70. return ret;
  71. if (w0 == 0x2024448d && w1 == 0x01a1b850 && w2 == 0xcd500000 &&
  72. (w3 & 0xff) == 0x80)
  73. ret = X86_SCF_FREEBSD_SIGFRAME;
  74. else {
  75. if ((*a->access_mem) (as, ip + 16, &w4, 0, arg) < 0 ||
  76. (*a->access_mem) (as, ip + 20, &w5, 0, arg) < 0)
  77. return ret;
  78. if (w0 == 0x2024448d && w1 == 0x5440f750 && w2 == 0x75000200 &&
  79. w3 == 0x14688e03 && w4 == 0x0001a1b8 && w5 == 0x80cd5000)
  80. ret = X86_SCF_FREEBSD_SIGFRAME;
  81. }
  82. Debug (16, "returning %d\n", ret);
  83. c->sigcontext_format = ret;
  84. return (ret);
  85. }
  86. PROTECTED int
  87. unw_handle_signal_frame (unw_cursor_t *cursor)
  88. {
  89. struct cursor *c = (struct cursor *) cursor;
  90. int ret;
  91. if (c->sigcontext_format == X86_SCF_FREEBSD_SIGFRAME) {
  92. struct sigframe *sf;
  93. uintptr_t uc_addr;
  94. struct dwarf_loc esp_loc;
  95. sf = (struct sigframe *)c->dwarf.cfa;
  96. uc_addr = (uintptr_t)&(sf->sf_uc);
  97. c->sigcontext_addr = c->dwarf.cfa;
  98. esp_loc = DWARF_LOC (uc_addr + FREEBSD_UC_MCONTEXT_ESP_OFF, 0);
  99. ret = dwarf_get (&c->dwarf, esp_loc, &c->dwarf.cfa);
  100. if (ret < 0)
  101. {
  102. Debug (2, "returning 0\n");
  103. return 0;
  104. }
  105. c->dwarf.loc[EIP] = DWARF_LOC (uc_addr + FREEBSD_UC_MCONTEXT_EIP_OFF, 0);
  106. c->dwarf.loc[ESP] = DWARF_LOC (uc_addr + FREEBSD_UC_MCONTEXT_ESP_OFF, 0);
  107. c->dwarf.loc[EAX] = DWARF_LOC (uc_addr + FREEBSD_UC_MCONTEXT_EAX_OFF, 0);
  108. c->dwarf.loc[ECX] = DWARF_LOC (uc_addr + FREEBSD_UC_MCONTEXT_ECX_OFF, 0);
  109. c->dwarf.loc[EDX] = DWARF_LOC (uc_addr + FREEBSD_UC_MCONTEXT_EDX_OFF, 0);
  110. c->dwarf.loc[EBX] = DWARF_LOC (uc_addr + FREEBSD_UC_MCONTEXT_EBX_OFF, 0);
  111. c->dwarf.loc[EBP] = DWARF_LOC (uc_addr + FREEBSD_UC_MCONTEXT_EBP_OFF, 0);
  112. c->dwarf.loc[ESI] = DWARF_LOC (uc_addr + FREEBSD_UC_MCONTEXT_ESI_OFF, 0);
  113. c->dwarf.loc[EDI] = DWARF_LOC (uc_addr + FREEBSD_UC_MCONTEXT_EDI_OFF, 0);
  114. c->dwarf.loc[EFLAGS] = DWARF_LOC (uc_addr + FREEBSD_UC_MCONTEXT_EFLAGS_OFF, 0);
  115. c->dwarf.loc[TRAPNO] = DWARF_LOC (uc_addr + FREEBSD_UC_MCONTEXT_TRAPNO_OFF, 0);
  116. c->dwarf.loc[ST0] = DWARF_NULL_LOC;
  117. } else {
  118. Debug (8, "Gstep: not handling frame format %d\n", c->sigcontext_format);
  119. abort();
  120. }
  121. return 0;
  122. }
  123. HIDDEN dwarf_loc_t
  124. x86_get_scratch_loc (struct cursor *c, unw_regnum_t reg)
  125. {
  126. unw_word_t addr = c->sigcontext_addr, off, xmm_off;
  127. unw_word_t fpstate, fpformat;
  128. int ret, is_fpstate = 0, is_xmmstate = 0;
  129. switch (c->sigcontext_format)
  130. {
  131. case X86_SCF_NONE:
  132. return DWARF_REG_LOC (&c->dwarf, reg);
  133. case X86_SCF_FREEBSD_SIGFRAME:
  134. addr += offsetof(struct sigframe, sf_uc) + FREEBSD_UC_MCONTEXT_OFF;
  135. break;
  136. case X86_SCF_FREEBSD_SIGFRAME4:
  137. abort();
  138. break;
  139. case X86_SCF_FREEBSD_OSIGFRAME:
  140. /* XXXKIB */
  141. abort();
  142. break;
  143. case X86_SCF_FREEBSD_SYSCALL:
  144. /* XXXKIB */
  145. abort();
  146. break;
  147. default:
  148. /* XXXKIB */
  149. abort();
  150. break;
  151. }
  152. off = 0; /* shut gcc warning */
  153. switch (reg)
  154. {
  155. case UNW_X86_GS: off = FREEBSD_UC_MCONTEXT_GS_OFF; break;
  156. case UNW_X86_FS: off = FREEBSD_UC_MCONTEXT_FS_OFF; break;
  157. case UNW_X86_ES: off = FREEBSD_UC_MCONTEXT_ES_OFF; break;
  158. case UNW_X86_DS: off = FREEBSD_UC_MCONTEXT_SS_OFF; break;
  159. case UNW_X86_EDI: off = FREEBSD_UC_MCONTEXT_EDI_OFF; break;
  160. case UNW_X86_ESI: off = FREEBSD_UC_MCONTEXT_ESI_OFF; break;
  161. case UNW_X86_EBP: off = FREEBSD_UC_MCONTEXT_EBP_OFF; break;
  162. case UNW_X86_ESP: off = FREEBSD_UC_MCONTEXT_ESP_OFF; break;
  163. case UNW_X86_EBX: off = FREEBSD_UC_MCONTEXT_EBX_OFF; break;
  164. case UNW_X86_EDX: off = FREEBSD_UC_MCONTEXT_EDX_OFF; break;
  165. case UNW_X86_ECX: off = FREEBSD_UC_MCONTEXT_ECX_OFF; break;
  166. case UNW_X86_EAX: off = FREEBSD_UC_MCONTEXT_EAX_OFF; break;
  167. case UNW_X86_TRAPNO: off = FREEBSD_UC_MCONTEXT_TRAPNO_OFF; break;
  168. case UNW_X86_EIP: off = FREEBSD_UC_MCONTEXT_EIP_OFF; break;
  169. case UNW_X86_CS: off = FREEBSD_UC_MCONTEXT_CS_OFF; break;
  170. case UNW_X86_EFLAGS: off = FREEBSD_UC_MCONTEXT_EFLAGS_OFF; break;
  171. case UNW_X86_SS: off = FREEBSD_UC_MCONTEXT_SS_OFF; break;
  172. case UNW_X86_FCW:
  173. is_fpstate = 1;
  174. off = FREEBSD_UC_MCONTEXT_CW_OFF;
  175. xmm_off = FREEBSD_UC_MCONTEXT_CW_XMM_OFF;
  176. break;
  177. case UNW_X86_FSW:
  178. is_fpstate = 1;
  179. off = FREEBSD_UC_MCONTEXT_SW_OFF;
  180. xmm_off = FREEBSD_UC_MCONTEXT_SW_XMM_OFF;
  181. break;
  182. case UNW_X86_FTW:
  183. is_fpstate = 1;
  184. xmm_off = FREEBSD_UC_MCONTEXT_TAG_XMM_OFF;
  185. off = FREEBSD_UC_MCONTEXT_TAG_OFF;
  186. break;
  187. case UNW_X86_FCS:
  188. is_fpstate = 1;
  189. off = FREEBSD_UC_MCONTEXT_CSSEL_OFF;
  190. xmm_off = FREEBSD_UC_MCONTEXT_CSSEL_XMM_OFF;
  191. break;
  192. case UNW_X86_FIP:
  193. is_fpstate = 1;
  194. off = FREEBSD_UC_MCONTEXT_IPOFF_OFF;
  195. xmm_off = FREEBSD_UC_MCONTEXT_IPOFF_XMM_OFF;
  196. break;
  197. case UNW_X86_FEA:
  198. is_fpstate = 1;
  199. off = FREEBSD_UC_MCONTEXT_DATAOFF_OFF;
  200. xmm_off = FREEBSD_UC_MCONTEXT_DATAOFF_XMM_OFF;
  201. break;
  202. case UNW_X86_FDS:
  203. is_fpstate = 1;
  204. off = FREEBSD_US_MCONTEXT_DATASEL_OFF;
  205. xmm_off = FREEBSD_US_MCONTEXT_DATASEL_XMM_OFF;
  206. break;
  207. case UNW_X86_MXCSR:
  208. is_fpstate = 1;
  209. is_xmmstate = 1;
  210. xmm_off = FREEBSD_UC_MCONTEXT_MXCSR_XMM_OFF;
  211. break;
  212. /* stacked fp registers */
  213. case UNW_X86_ST0: case UNW_X86_ST1: case UNW_X86_ST2: case UNW_X86_ST3:
  214. case UNW_X86_ST4: case UNW_X86_ST5: case UNW_X86_ST6: case UNW_X86_ST7:
  215. is_fpstate = 1;
  216. off = FREEBSD_UC_MCONTEXT_ST0_OFF + 10*(reg - UNW_X86_ST0);
  217. xmm_off = FREEBSD_UC_MCONTEXT_ST0_XMM_OFF + 10*(reg - UNW_X86_ST0);
  218. break;
  219. /* SSE fp registers */
  220. case UNW_X86_XMM0_lo: case UNW_X86_XMM0_hi:
  221. case UNW_X86_XMM1_lo: case UNW_X86_XMM1_hi:
  222. case UNW_X86_XMM2_lo: case UNW_X86_XMM2_hi:
  223. case UNW_X86_XMM3_lo: case UNW_X86_XMM3_hi:
  224. case UNW_X86_XMM4_lo: case UNW_X86_XMM4_hi:
  225. case UNW_X86_XMM5_lo: case UNW_X86_XMM5_hi:
  226. case UNW_X86_XMM6_lo: case UNW_X86_XMM6_hi:
  227. case UNW_X86_XMM7_lo: case UNW_X86_XMM7_hi:
  228. is_fpstate = 1;
  229. is_xmmstate = 1;
  230. xmm_off = FREEBSD_UC_MCONTEXT_XMM0_OFF + 8*(reg - UNW_X86_XMM0_lo);
  231. break;
  232. case UNW_X86_XMM0:
  233. case UNW_X86_XMM1:
  234. case UNW_X86_XMM2:
  235. case UNW_X86_XMM3:
  236. case UNW_X86_XMM4:
  237. case UNW_X86_XMM5:
  238. case UNW_X86_XMM6:
  239. case UNW_X86_XMM7:
  240. is_fpstate = 1;
  241. is_xmmstate = 1;
  242. xmm_off = FREEBSD_UC_MCONTEXT_XMM0_OFF + 16*(reg - UNW_X86_XMM0);
  243. break;
  244. case UNW_X86_FOP:
  245. case UNW_X86_TSS:
  246. case UNW_X86_LDT:
  247. default:
  248. return DWARF_REG_LOC (&c->dwarf, reg);
  249. }
  250. if (is_fpstate)
  251. {
  252. if ((ret = dwarf_get (&c->dwarf,
  253. DWARF_MEM_LOC (&c->dwarf, addr + FREEBSD_UC_MCONTEXT_FPSTATE_OFF),
  254. &fpstate)) < 0)
  255. return DWARF_NULL_LOC;
  256. if (fpstate == FREEBSD_UC_MCONTEXT_FPOWNED_NONE)
  257. return DWARF_NULL_LOC;
  258. if ((ret = dwarf_get (&c->dwarf,
  259. DWARF_MEM_LOC (&c->dwarf, addr + FREEBSD_UC_MCONTEXT_FPFORMAT_OFF),
  260. &fpformat)) < 0)
  261. return DWARF_NULL_LOC;
  262. if (fpformat == FREEBSD_UC_MCONTEXT_FPFMT_NODEV ||
  263. (is_xmmstate && fpformat != FREEBSD_UC_MCONTEXT_FPFMT_XMM))
  264. return DWARF_NULL_LOC;
  265. if (is_xmmstate)
  266. off = xmm_off;
  267. }
  268. return DWARF_MEM_LOC (c, addr + off);
  269. }
  270. #ifndef UNW_REMOTE_ONLY
  271. HIDDEN void *
  272. x86_r_uc_addr (ucontext_t *uc, int reg)
  273. {
  274. void *addr;
  275. switch (reg)
  276. {
  277. case UNW_X86_GS: addr = &uc->uc_mcontext.mc_gs; break;
  278. case UNW_X86_FS: addr = &uc->uc_mcontext.mc_fs; break;
  279. case UNW_X86_ES: addr = &uc->uc_mcontext.mc_es; break;
  280. case UNW_X86_DS: addr = &uc->uc_mcontext.mc_ds; break;
  281. case UNW_X86_EAX: addr = &uc->uc_mcontext.mc_eax; break;
  282. case UNW_X86_EBX: addr = &uc->uc_mcontext.mc_ebx; break;
  283. case UNW_X86_ECX: addr = &uc->uc_mcontext.mc_ecx; break;
  284. case UNW_X86_EDX: addr = &uc->uc_mcontext.mc_edx; break;
  285. case UNW_X86_ESI: addr = &uc->uc_mcontext.mc_esi; break;
  286. case UNW_X86_EDI: addr = &uc->uc_mcontext.mc_edi; break;
  287. case UNW_X86_EBP: addr = &uc->uc_mcontext.mc_ebp; break;
  288. case UNW_X86_EIP: addr = &uc->uc_mcontext.mc_eip; break;
  289. case UNW_X86_ESP: addr = &uc->uc_mcontext.mc_esp; break;
  290. case UNW_X86_TRAPNO: addr = &uc->uc_mcontext.mc_trapno; break;
  291. case UNW_X86_CS: addr = &uc->uc_mcontext.mc_cs; break;
  292. case UNW_X86_EFLAGS: addr = &uc->uc_mcontext.mc_eflags; break;
  293. case UNW_X86_SS: addr = &uc->uc_mcontext.mc_ss; break;
  294. default:
  295. addr = NULL;
  296. }
  297. return addr;
  298. }
  299. HIDDEN int
  300. x86_local_resume (unw_addr_space_t as, unw_cursor_t *cursor, void *arg)
  301. {
  302. struct cursor *c = (struct cursor *) cursor;
  303. ucontext_t *uc = c->uc;
  304. /* Ensure c->pi is up-to-date. On x86, it's relatively common to be
  305. missing DWARF unwind info. We don't want to fail in that case,
  306. because the frame-chain still would let us do a backtrace at
  307. least. */
  308. dwarf_make_proc_info (&c->dwarf);
  309. if (c->sigcontext_format == X86_SCF_NONE) {
  310. Debug (8, "resuming at ip=%x via setcontext()\n", c->dwarf.ip);
  311. setcontext (uc);
  312. abort();
  313. } else if (c->sigcontext_format == X86_SCF_FREEBSD_SIGFRAME) {
  314. struct sigcontext *sc = (struct sigcontext *) c->sigcontext_addr;
  315. Debug (8, "resuming at ip=%x via sigreturn(%p)\n", c->dwarf.ip, sc);
  316. sigreturn((ucontext_t *)((const char *)sc + FREEBSD_SC_UCONTEXT_OFF));
  317. abort();
  318. } else {
  319. Debug (8, "resuming at ip=%x for sigcontext format %d not implemented\n",
  320. c->dwarf.ip, c->sigcontext_format);
  321. abort();
  322. }
  323. return -UNW_EINVAL;
  324. }
  325. #endif