PageRenderTime 61ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/gdb-linaro-7.5-2012.12-1/gdb/sparc64obsd-tdep.c

https://bitbucket.org/codefirex/toolchain_gdb
C | 411 lines | 281 code | 76 blank | 54 comment | 55 complexity | 1bdfe1be269406fccc34131a87eabe63 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, LGPL-2.0, GPL-3.0, LGPL-2.1
  1. /* Target-dependent code for OpenBSD/sparc64.
  2. Copyright (C) 2004-2012 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "frame.h"
  16. #include "frame-unwind.h"
  17. #include "gdbcore.h"
  18. #include "osabi.h"
  19. #include "regcache.h"
  20. #include "regset.h"
  21. #include "symtab.h"
  22. #include "objfiles.h"
  23. #include "trad-frame.h"
  24. #include "gdb_assert.h"
  25. #include "obsd-tdep.h"
  26. #include "sparc64-tdep.h"
  27. #include "solib-svr4.h"
  28. #include "bsd-uthread.h"
  29. /* OpenBSD uses the traditional NetBSD core file format, even for
  30. ports that use ELF. The core files don't use multiple register
  31. sets. Instead, the general-purpose and floating-point registers
  32. are lumped together in a single section. Unlike on NetBSD, OpenBSD
  33. uses a different layout for its general-purpose registers than the
  34. layout used for ptrace(2). */
  35. /* From <machine/reg.h>. */
  36. const struct sparc_gregset sparc64obsd_core_gregset =
  37. {
  38. 0 * 8, /* "tstate" */
  39. 1 * 8, /* %pc */
  40. 2 * 8, /* %npc */
  41. 3 * 8, /* %y */
  42. -1, /* %fprs */
  43. -1,
  44. 7 * 8, /* %g1 */
  45. 22 * 8, /* %l0 */
  46. 4 /* sizeof (%y) */
  47. };
  48. static void
  49. sparc64obsd_supply_gregset (const struct regset *regset,
  50. struct regcache *regcache,
  51. int regnum, const void *gregs, size_t len)
  52. {
  53. const char *regs = gregs;
  54. sparc64_supply_gregset (&sparc64obsd_core_gregset, regcache, regnum, regs);
  55. sparc64_supply_fpregset (regcache, regnum, regs + 288);
  56. }
  57. /* Signal trampolines. */
  58. /* Since OpenBSD 3.2, the sigtramp routine is mapped at a random page
  59. in virtual memory. The randomness makes it somewhat tricky to
  60. detect it, but fortunately we can rely on the fact that the start
  61. of the sigtramp routine is page-aligned. We recognize the
  62. trampoline by looking for the code that invokes the sigreturn
  63. system call. The offset where we can find that code varies from
  64. release to release.
  65. By the way, the mapping mentioned above is read-only, so you cannot
  66. place a breakpoint in the signal trampoline. */
  67. /* Default page size. */
  68. static const int sparc64obsd_page_size = 8192;
  69. /* Offset for sigreturn(2). */
  70. static const int sparc64obsd_sigreturn_offset[] = {
  71. 0xf0, /* OpenBSD 3.8 */
  72. 0xec, /* OpenBSD 3.6 */
  73. 0xe8, /* OpenBSD 3.2 */
  74. -1
  75. };
  76. static int
  77. sparc64obsd_pc_in_sigtramp (CORE_ADDR pc, const char *name)
  78. {
  79. CORE_ADDR start_pc = (pc & ~(sparc64obsd_page_size - 1));
  80. unsigned long insn;
  81. const int *offset;
  82. if (name)
  83. return 0;
  84. for (offset = sparc64obsd_sigreturn_offset; *offset != -1; offset++)
  85. {
  86. /* Check for "restore %g0, SYS_sigreturn, %g1". */
  87. insn = sparc_fetch_instruction (start_pc + *offset);
  88. if (insn != 0x83e82067)
  89. continue;
  90. /* Check for "t ST_SYSCALL". */
  91. insn = sparc_fetch_instruction (start_pc + *offset + 8);
  92. if (insn != 0x91d02000)
  93. continue;
  94. return 1;
  95. }
  96. return 0;
  97. }
  98. static struct sparc_frame_cache *
  99. sparc64obsd_frame_cache (struct frame_info *this_frame, void **this_cache)
  100. {
  101. struct sparc_frame_cache *cache;
  102. CORE_ADDR addr;
  103. if (*this_cache)
  104. return *this_cache;
  105. cache = sparc_frame_cache (this_frame, this_cache);
  106. gdb_assert (cache == *this_cache);
  107. /* If we couldn't find the frame's function, we're probably dealing
  108. with an on-stack signal trampoline. */
  109. if (cache->pc == 0)
  110. {
  111. cache->pc = get_frame_pc (this_frame);
  112. cache->pc &= ~(sparc64obsd_page_size - 1);
  113. /* Since we couldn't find the frame's function, the cache was
  114. initialized under the assumption that we're frameless. */
  115. sparc_record_save_insn (cache);
  116. addr = get_frame_register_unsigned (this_frame, SPARC_FP_REGNUM);
  117. if (addr & 1)
  118. addr += BIAS;
  119. cache->base = addr;
  120. }
  121. /* We find the appropriate instance of `struct sigcontext' at a
  122. fixed offset in the signal frame. */
  123. addr = cache->base + 128 + 16;
  124. cache->saved_regs = sparc64nbsd_sigcontext_saved_regs (addr, this_frame);
  125. return cache;
  126. }
  127. static void
  128. sparc64obsd_frame_this_id (struct frame_info *this_frame, void **this_cache,
  129. struct frame_id *this_id)
  130. {
  131. struct sparc_frame_cache *cache =
  132. sparc64obsd_frame_cache (this_frame, this_cache);
  133. (*this_id) = frame_id_build (cache->base, cache->pc);
  134. }
  135. static struct value *
  136. sparc64obsd_frame_prev_register (struct frame_info *this_frame,
  137. void **this_cache, int regnum)
  138. {
  139. struct sparc_frame_cache *cache =
  140. sparc64obsd_frame_cache (this_frame, this_cache);
  141. return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
  142. }
  143. static int
  144. sparc64obsd_sigtramp_frame_sniffer (const struct frame_unwind *self,
  145. struct frame_info *this_frame,
  146. void **this_cache)
  147. {
  148. CORE_ADDR pc = get_frame_pc (this_frame);
  149. const char *name;
  150. find_pc_partial_function (pc, &name, NULL, NULL);
  151. if (sparc64obsd_pc_in_sigtramp (pc, name))
  152. return 1;
  153. return 0;
  154. }
  155. static const struct frame_unwind sparc64obsd_frame_unwind =
  156. {
  157. SIGTRAMP_FRAME,
  158. default_frame_unwind_stop_reason,
  159. sparc64obsd_frame_this_id,
  160. sparc64obsd_frame_prev_register,
  161. NULL,
  162. sparc64obsd_sigtramp_frame_sniffer
  163. };
  164. /* Kernel debugging support. */
  165. static struct sparc_frame_cache *
  166. sparc64obsd_trapframe_cache (struct frame_info *this_frame, void **this_cache)
  167. {
  168. struct sparc_frame_cache *cache;
  169. CORE_ADDR sp, trapframe_addr;
  170. int regnum;
  171. if (*this_cache)
  172. return *this_cache;
  173. cache = sparc_frame_cache (this_frame, this_cache);
  174. gdb_assert (cache == *this_cache);
  175. sp = get_frame_register_unsigned (this_frame, SPARC_SP_REGNUM);
  176. trapframe_addr = sp + BIAS + 176;
  177. cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
  178. cache->saved_regs[SPARC64_STATE_REGNUM].addr = trapframe_addr;
  179. cache->saved_regs[SPARC64_PC_REGNUM].addr = trapframe_addr + 8;
  180. cache->saved_regs[SPARC64_NPC_REGNUM].addr = trapframe_addr + 16;
  181. for (regnum = SPARC_G0_REGNUM; regnum <= SPARC_I7_REGNUM; regnum++)
  182. cache->saved_regs[regnum].addr =
  183. trapframe_addr + 48 + (regnum - SPARC_G0_REGNUM) * 8;
  184. return cache;
  185. }
  186. static void
  187. sparc64obsd_trapframe_this_id (struct frame_info *this_frame,
  188. void **this_cache, struct frame_id *this_id)
  189. {
  190. struct sparc_frame_cache *cache =
  191. sparc64obsd_trapframe_cache (this_frame, this_cache);
  192. (*this_id) = frame_id_build (cache->base, cache->pc);
  193. }
  194. static struct value *
  195. sparc64obsd_trapframe_prev_register (struct frame_info *this_frame,
  196. void **this_cache, int regnum)
  197. {
  198. struct sparc_frame_cache *cache =
  199. sparc64obsd_trapframe_cache (this_frame, this_cache);
  200. return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
  201. }
  202. static int
  203. sparc64obsd_trapframe_sniffer (const struct frame_unwind *self,
  204. struct frame_info *this_frame,
  205. void **this_cache)
  206. {
  207. CORE_ADDR pc;
  208. ULONGEST pstate;
  209. const char *name;
  210. /* Check whether we are in privileged mode, and bail out if we're not. */
  211. pstate = get_frame_register_unsigned (this_frame, SPARC64_PSTATE_REGNUM);
  212. if ((pstate & SPARC64_PSTATE_PRIV) == 0)
  213. return 0;
  214. pc = get_frame_address_in_block (this_frame);
  215. find_pc_partial_function (pc, &name, NULL, NULL);
  216. if (name && strcmp (name, "Lslowtrap_reenter") == 0)
  217. return 1;
  218. return 0;
  219. }
  220. static const struct frame_unwind sparc64obsd_trapframe_unwind =
  221. {
  222. NORMAL_FRAME,
  223. default_frame_unwind_stop_reason,
  224. sparc64obsd_trapframe_this_id,
  225. sparc64obsd_trapframe_prev_register,
  226. NULL,
  227. sparc64obsd_trapframe_sniffer
  228. };
  229. /* Threads support. */
  230. /* Offset wthin the thread structure where we can find %fp and %i7. */
  231. #define SPARC64OBSD_UTHREAD_FP_OFFSET 232
  232. #define SPARC64OBSD_UTHREAD_PC_OFFSET 240
  233. static void
  234. sparc64obsd_supply_uthread (struct regcache *regcache,
  235. int regnum, CORE_ADDR addr)
  236. {
  237. struct gdbarch *gdbarch = get_regcache_arch (regcache);
  238. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  239. CORE_ADDR fp, fp_addr = addr + SPARC64OBSD_UTHREAD_FP_OFFSET;
  240. gdb_byte buf[8];
  241. gdb_assert (regnum >= -1);
  242. fp = read_memory_unsigned_integer (fp_addr, 8, byte_order);
  243. if (regnum == SPARC_SP_REGNUM || regnum == -1)
  244. {
  245. store_unsigned_integer (buf, 8, byte_order, fp);
  246. regcache_raw_supply (regcache, SPARC_SP_REGNUM, buf);
  247. if (regnum == SPARC_SP_REGNUM)
  248. return;
  249. }
  250. if (regnum == SPARC64_PC_REGNUM || regnum == SPARC64_NPC_REGNUM
  251. || regnum == -1)
  252. {
  253. CORE_ADDR i7, i7_addr = addr + SPARC64OBSD_UTHREAD_PC_OFFSET;
  254. i7 = read_memory_unsigned_integer (i7_addr, 8, byte_order);
  255. if (regnum == SPARC64_PC_REGNUM || regnum == -1)
  256. {
  257. store_unsigned_integer (buf, 8, byte_order, i7 + 8);
  258. regcache_raw_supply (regcache, SPARC64_PC_REGNUM, buf);
  259. }
  260. if (regnum == SPARC64_NPC_REGNUM || regnum == -1)
  261. {
  262. store_unsigned_integer (buf, 8, byte_order, i7 + 12);
  263. regcache_raw_supply (regcache, SPARC64_NPC_REGNUM, buf);
  264. }
  265. if (regnum == SPARC64_PC_REGNUM || regnum == SPARC64_NPC_REGNUM)
  266. return;
  267. }
  268. sparc_supply_rwindow (regcache, fp, regnum);
  269. }
  270. static void
  271. sparc64obsd_collect_uthread(const struct regcache *regcache,
  272. int regnum, CORE_ADDR addr)
  273. {
  274. struct gdbarch *gdbarch = get_regcache_arch (regcache);
  275. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  276. CORE_ADDR sp;
  277. gdb_byte buf[8];
  278. gdb_assert (regnum >= -1);
  279. if (regnum == SPARC_SP_REGNUM || regnum == -1)
  280. {
  281. CORE_ADDR fp_addr = addr + SPARC64OBSD_UTHREAD_FP_OFFSET;
  282. regcache_raw_collect (regcache, SPARC_SP_REGNUM, buf);
  283. write_memory (fp_addr,buf, 8);
  284. }
  285. if (regnum == SPARC64_PC_REGNUM || regnum == -1)
  286. {
  287. CORE_ADDR i7, i7_addr = addr + SPARC64OBSD_UTHREAD_PC_OFFSET;
  288. regcache_raw_collect (regcache, SPARC64_PC_REGNUM, buf);
  289. i7 = extract_unsigned_integer (buf, 8, byte_order) - 8;
  290. write_memory_unsigned_integer (i7_addr, 8, byte_order, i7);
  291. if (regnum == SPARC64_PC_REGNUM)
  292. return;
  293. }
  294. regcache_raw_collect (regcache, SPARC_SP_REGNUM, buf);
  295. sp = extract_unsigned_integer (buf, 8, byte_order);
  296. sparc_collect_rwindow (regcache, sp, regnum);
  297. }
  298. static void
  299. sparc64obsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  300. {
  301. struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  302. tdep->gregset = regset_alloc (gdbarch, sparc64obsd_supply_gregset, NULL);
  303. tdep->sizeof_gregset = 832;
  304. /* Make sure we can single-step "new" syscalls. */
  305. tdep->step_trap = sparcnbsd_step_trap;
  306. frame_unwind_append_unwinder (gdbarch, &sparc64obsd_frame_unwind);
  307. frame_unwind_append_unwinder (gdbarch, &sparc64obsd_trapframe_unwind);
  308. sparc64_init_abi (info, gdbarch);
  309. /* OpenBSD/sparc64 has SVR4-style shared libraries. */
  310. set_solib_svr4_fetch_link_map_offsets
  311. (gdbarch, svr4_lp64_fetch_link_map_offsets);
  312. set_gdbarch_skip_solib_resolver (gdbarch, obsd_skip_solib_resolver);
  313. /* OpenBSD provides a user-level threads implementation. */
  314. bsd_uthread_set_supply_uthread (gdbarch, sparc64obsd_supply_uthread);
  315. bsd_uthread_set_collect_uthread (gdbarch, sparc64obsd_collect_uthread);
  316. }
  317. /* Provide a prototype to silence -Wmissing-prototypes. */
  318. void _initialize_sparc64obsd_tdep (void);
  319. void
  320. _initialize_sparc64obsd_tdep (void)
  321. {
  322. gdbarch_register_osabi (bfd_arch_sparc, bfd_mach_sparc_v9,
  323. GDB_OSABI_OPENBSD_ELF, sparc64obsd_init_abi);
  324. }