frv-linux-tdep.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /* Target-dependent code for GNU/Linux running on the Fujitsu FR-V,
  2. for GDB.
  3. Copyright (C) 2004-2022 Free Software Foundation, Inc.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "defs.h"
  16. #include "gdbcore.h"
  17. #include "target.h"
  18. #include "frame.h"
  19. #include "osabi.h"
  20. #include "regcache.h"
  21. #include "elf-bfd.h"
  22. #include "elf/frv.h"
  23. #include "frv-tdep.h"
  24. #include "trad-frame.h"
  25. #include "frame-unwind.h"
  26. #include "regset.h"
  27. #include "linux-tdep.h"
  28. #include "gdbarch.h"
  29. /* Define the size (in bytes) of an FR-V instruction. */
  30. static const int frv_instr_size = 4;
  31. enum {
  32. NORMAL_SIGTRAMP = 1,
  33. RT_SIGTRAMP = 2
  34. };
  35. static int
  36. frv_linux_pc_in_sigtramp (struct gdbarch *gdbarch, CORE_ADDR pc,
  37. const char *name)
  38. {
  39. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  40. gdb_byte buf[frv_instr_size];
  41. LONGEST instr;
  42. int retval = 0;
  43. if (target_read_memory (pc, buf, sizeof buf) != 0)
  44. return 0;
  45. instr = extract_unsigned_integer (buf, sizeof buf, byte_order);
  46. if (instr == 0x8efc0077) /* setlos #__NR_sigreturn, gr7 */
  47. retval = NORMAL_SIGTRAMP;
  48. else if (instr == 0x8efc00ad) /* setlos #__NR_rt_sigreturn, gr7 */
  49. retval = RT_SIGTRAMP;
  50. else
  51. return 0;
  52. if (target_read_memory (pc + frv_instr_size, buf, sizeof buf) != 0)
  53. return 0;
  54. instr = extract_unsigned_integer (buf, sizeof buf, byte_order);
  55. if (instr != 0xc0700000) /* tira gr0, 0 */
  56. return 0;
  57. /* If we get this far, we'll return a non-zero value, either
  58. NORMAL_SIGTRAMP (1) or RT_SIGTRAMP (2). */
  59. return retval;
  60. }
  61. /* Given NEXT_FRAME, the "callee" frame of the sigtramp frame that we
  62. wish to decode, and REGNO, one of the frv register numbers defined
  63. in frv-tdep.h, return the address of the saved register (corresponding
  64. to REGNO) in the sigtramp frame. Return -1 if the register is not
  65. found in the sigtramp frame. The magic numbers in the code below
  66. were computed by examining the following kernel structs:
  67. From arch/frv/kernel/signal.c:
  68. struct sigframe
  69. {
  70. void (*pretcode)(void);
  71. int sig;
  72. struct sigcontext sc;
  73. unsigned long extramask[_NSIG_WORDS-1];
  74. uint32_t retcode[2];
  75. };
  76. struct rt_sigframe
  77. {
  78. void (*pretcode)(void);
  79. int sig;
  80. struct siginfo *pinfo;
  81. void *puc;
  82. struct siginfo info;
  83. struct ucontext uc;
  84. uint32_t retcode[2];
  85. };
  86. From include/asm-frv/ucontext.h:
  87. struct ucontext {
  88. unsigned long uc_flags;
  89. struct ucontext *uc_link;
  90. stack_t uc_stack;
  91. struct sigcontext uc_mcontext;
  92. sigset_t uc_sigmask;
  93. };
  94. From include/asm-frv/signal.h:
  95. typedef struct sigaltstack {
  96. void *ss_sp;
  97. int ss_flags;
  98. size_t ss_size;
  99. } stack_t;
  100. From include/asm-frv/sigcontext.h:
  101. struct sigcontext {
  102. struct user_context sc_context;
  103. unsigned long sc_oldmask;
  104. } __attribute__((aligned(8)));
  105. From include/asm-frv/registers.h:
  106. struct user_int_regs
  107. {
  108. unsigned long psr;
  109. unsigned long isr;
  110. unsigned long ccr;
  111. unsigned long cccr;
  112. unsigned long lr;
  113. unsigned long lcr;
  114. unsigned long pc;
  115. unsigned long __status;
  116. unsigned long syscallno;
  117. unsigned long orig_gr8;
  118. unsigned long gner[2];
  119. unsigned long long iacc[1];
  120. union {
  121. unsigned long tbr;
  122. unsigned long gr[64];
  123. };
  124. };
  125. struct user_fpmedia_regs
  126. {
  127. unsigned long fr[64];
  128. unsigned long fner[2];
  129. unsigned long msr[2];
  130. unsigned long acc[8];
  131. unsigned char accg[8];
  132. unsigned long fsr[1];
  133. };
  134. struct user_context
  135. {
  136. struct user_int_regs i;
  137. struct user_fpmedia_regs f;
  138. void *extension;
  139. } __attribute__((aligned(8))); */
  140. static LONGEST
  141. frv_linux_sigcontext_reg_addr (struct frame_info *this_frame, int regno,
  142. CORE_ADDR *sc_addr_cache_ptr)
  143. {
  144. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  145. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  146. CORE_ADDR sc_addr;
  147. if (sc_addr_cache_ptr && *sc_addr_cache_ptr)
  148. {
  149. sc_addr = *sc_addr_cache_ptr;
  150. }
  151. else
  152. {
  153. CORE_ADDR pc, sp;
  154. gdb_byte buf[4];
  155. int tramp_type;
  156. pc = get_frame_pc (this_frame);
  157. tramp_type = frv_linux_pc_in_sigtramp (gdbarch, pc, 0);
  158. get_frame_register (this_frame, sp_regnum, buf);
  159. sp = extract_unsigned_integer (buf, sizeof buf, byte_order);
  160. if (tramp_type == NORMAL_SIGTRAMP)
  161. {
  162. /* For a normal sigtramp frame, the sigcontext struct starts
  163. at SP + 8. */
  164. sc_addr = sp + 8;
  165. }
  166. else if (tramp_type == RT_SIGTRAMP)
  167. {
  168. /* For a realtime sigtramp frame, SP + 12 contains a pointer
  169. to a ucontext struct. The ucontext struct contains a
  170. sigcontext struct starting 24 bytes in. (The offset of
  171. uc_mcontext within struct ucontext is derived as follows:
  172. stack_t is a 12-byte struct and struct sigcontext is
  173. 8-byte aligned. This gives an offset of 8 + 12 + 4 (for
  174. padding) = 24.) */
  175. if (target_read_memory (sp + 12, buf, sizeof buf) != 0)
  176. {
  177. warning (_("Can't read realtime sigtramp frame."));
  178. return 0;
  179. }
  180. sc_addr = extract_unsigned_integer (buf, sizeof buf, byte_order);
  181. sc_addr += 24;
  182. }
  183. else
  184. internal_error (__FILE__, __LINE__, _("not a signal trampoline"));
  185. if (sc_addr_cache_ptr)
  186. *sc_addr_cache_ptr = sc_addr;
  187. }
  188. switch (regno)
  189. {
  190. case psr_regnum :
  191. return sc_addr + 0;
  192. /* sc_addr + 4 has "isr", the Integer Status Register. */
  193. case ccr_regnum :
  194. return sc_addr + 8;
  195. case cccr_regnum :
  196. return sc_addr + 12;
  197. case lr_regnum :
  198. return sc_addr + 16;
  199. case lcr_regnum :
  200. return sc_addr + 20;
  201. case pc_regnum :
  202. return sc_addr + 24;
  203. /* sc_addr + 28 is __status, the exception status.
  204. sc_addr + 32 is syscallno, the syscall number or -1.
  205. sc_addr + 36 is orig_gr8, the original syscall arg #1.
  206. sc_addr + 40 is gner[0].
  207. sc_addr + 44 is gner[1]. */
  208. case iacc0h_regnum :
  209. return sc_addr + 48;
  210. case iacc0l_regnum :
  211. return sc_addr + 52;
  212. default :
  213. if (first_gpr_regnum <= regno && regno <= last_gpr_regnum)
  214. return sc_addr + 56 + 4 * (regno - first_gpr_regnum);
  215. else if (first_fpr_regnum <= regno && regno <= last_fpr_regnum)
  216. return sc_addr + 312 + 4 * (regno - first_fpr_regnum);
  217. else
  218. return -1; /* not saved. */
  219. }
  220. }
  221. /* Signal trampolines. */
  222. static struct trad_frame_cache *
  223. frv_linux_sigtramp_frame_cache (struct frame_info *this_frame,
  224. void **this_cache)
  225. {
  226. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  227. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  228. struct trad_frame_cache *cache;
  229. CORE_ADDR addr;
  230. gdb_byte buf[4];
  231. int regnum;
  232. CORE_ADDR sc_addr_cache_val = 0;
  233. struct frame_id this_id;
  234. if (*this_cache)
  235. return (struct trad_frame_cache *) *this_cache;
  236. cache = trad_frame_cache_zalloc (this_frame);
  237. /* FIXME: cagney/2004-05-01: This is is long standing broken code.
  238. The frame ID's code address should be the start-address of the
  239. signal trampoline and not the current PC within that
  240. trampoline. */
  241. get_frame_register (this_frame, sp_regnum, buf);
  242. addr = extract_unsigned_integer (buf, sizeof buf, byte_order);
  243. this_id = frame_id_build (addr, get_frame_pc (this_frame));
  244. trad_frame_set_id (cache, this_id);
  245. for (regnum = 0; regnum < frv_num_regs; regnum++)
  246. {
  247. LONGEST reg_addr = frv_linux_sigcontext_reg_addr (this_frame, regnum,
  248. &sc_addr_cache_val);
  249. if (reg_addr != -1)
  250. trad_frame_set_reg_addr (cache, regnum, reg_addr);
  251. }
  252. *this_cache = cache;
  253. return cache;
  254. }
  255. static void
  256. frv_linux_sigtramp_frame_this_id (struct frame_info *this_frame,
  257. void **this_cache,
  258. struct frame_id *this_id)
  259. {
  260. struct trad_frame_cache *cache
  261. = frv_linux_sigtramp_frame_cache (this_frame, this_cache);
  262. trad_frame_get_id (cache, this_id);
  263. }
  264. static struct value *
  265. frv_linux_sigtramp_frame_prev_register (struct frame_info *this_frame,
  266. void **this_cache, int regnum)
  267. {
  268. /* Make sure we've initialized the cache. */
  269. struct trad_frame_cache *cache
  270. = frv_linux_sigtramp_frame_cache (this_frame, this_cache);
  271. return trad_frame_get_register (cache, this_frame, regnum);
  272. }
  273. static int
  274. frv_linux_sigtramp_frame_sniffer (const struct frame_unwind *self,
  275. struct frame_info *this_frame,
  276. void **this_cache)
  277. {
  278. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  279. CORE_ADDR pc = get_frame_pc (this_frame);
  280. const char *name;
  281. find_pc_partial_function (pc, &name, NULL, NULL);
  282. if (frv_linux_pc_in_sigtramp (gdbarch, pc, name))
  283. return 1;
  284. return 0;
  285. }
  286. static const struct frame_unwind frv_linux_sigtramp_frame_unwind =
  287. {
  288. "frv linux sigtramp",
  289. SIGTRAMP_FRAME,
  290. default_frame_unwind_stop_reason,
  291. frv_linux_sigtramp_frame_this_id,
  292. frv_linux_sigtramp_frame_prev_register,
  293. NULL,
  294. frv_linux_sigtramp_frame_sniffer
  295. };
  296. /* The FRV kernel defines ELF_NGREG as 46. We add 2 in order to include
  297. the loadmap addresses in the register set. (See below for more info.) */
  298. #define FRV_ELF_NGREG (46 + 2)
  299. typedef unsigned char frv_elf_greg_t[4];
  300. typedef struct { frv_elf_greg_t reg[FRV_ELF_NGREG]; } frv_elf_gregset_t;
  301. typedef unsigned char frv_elf_fpreg_t[4];
  302. typedef struct
  303. {
  304. frv_elf_fpreg_t fr[64];
  305. frv_elf_fpreg_t fner[2];
  306. frv_elf_fpreg_t msr[2];
  307. frv_elf_fpreg_t acc[8];
  308. unsigned char accg[8];
  309. frv_elf_fpreg_t fsr[1];
  310. } frv_elf_fpregset_t;
  311. /* Register maps. */
  312. static const struct regcache_map_entry frv_linux_gregmap[] =
  313. {
  314. { 1, psr_regnum, 4 },
  315. { 1, REGCACHE_MAP_SKIP, 4 }, /* isr */
  316. { 1, ccr_regnum, 4 },
  317. { 1, cccr_regnum, 4 },
  318. { 1, lr_regnum, 4 },
  319. { 1, lcr_regnum, 4 },
  320. { 1, pc_regnum, 4 },
  321. { 1, REGCACHE_MAP_SKIP, 4 }, /* __status */
  322. { 1, REGCACHE_MAP_SKIP, 4 }, /* syscallno */
  323. { 1, REGCACHE_MAP_SKIP, 4 }, /* orig_gr8 */
  324. { 1, gner0_regnum, 4 },
  325. { 1, gner1_regnum, 4 },
  326. { 1, REGCACHE_MAP_SKIP, 8 }, /* iacc0 */
  327. { 1, tbr_regnum, 4 },
  328. { 31, first_gpr_regnum + 1, 4 }, /* gr1 ... gr31 */
  329. /* Technically, the loadmap addresses are not part of `pr_reg' as
  330. found in the elf_prstatus struct. The fields which communicate
  331. the loadmap address appear (by design) immediately after
  332. `pr_reg' though, and the BFD function elf32_frv_grok_prstatus()
  333. has been implemented to include these fields in the register
  334. section that it extracts from the core file. So, for our
  335. purposes, they may be viewed as registers. */
  336. { 1, fdpic_loadmap_exec_regnum, 4 },
  337. { 1, fdpic_loadmap_interp_regnum, 4 },
  338. { 0 }
  339. };
  340. static const struct regcache_map_entry frv_linux_fpregmap[] =
  341. {
  342. { 64, first_fpr_regnum, 4 }, /* fr0 ... fr63 */
  343. { 1, fner0_regnum, 4 },
  344. { 1, fner1_regnum, 4 },
  345. { 1, msr0_regnum, 4 },
  346. { 1, msr1_regnum, 4 },
  347. { 8, acc0_regnum, 4 }, /* acc0 ... acc7 */
  348. { 1, accg0123_regnum, 4 },
  349. { 1, accg4567_regnum, 4 },
  350. { 1, fsr0_regnum, 4 },
  351. { 0 }
  352. };
  353. /* Unpack an frv_elf_gregset_t into GDB's register cache. */
  354. static void
  355. frv_linux_supply_gregset (const struct regset *regset,
  356. struct regcache *regcache,
  357. int regnum, const void *gregs, size_t len)
  358. {
  359. int regi;
  360. /* gr0 always contains 0. Also, the kernel passes the TBR value in
  361. this slot. */
  362. regcache->raw_supply_zeroed (first_gpr_regnum);
  363. /* Fill gr32, ..., gr63 with zeros. */
  364. for (regi = first_gpr_regnum + 32; regi <= last_gpr_regnum; regi++)
  365. regcache->raw_supply_zeroed (regi);
  366. regcache_supply_regset (regset, regcache, regnum, gregs, len);
  367. }
  368. /* FRV Linux kernel register sets. */
  369. static const struct regset frv_linux_gregset =
  370. {
  371. frv_linux_gregmap,
  372. frv_linux_supply_gregset, regcache_collect_regset
  373. };
  374. static const struct regset frv_linux_fpregset =
  375. {
  376. frv_linux_fpregmap,
  377. regcache_supply_regset, regcache_collect_regset
  378. };
  379. static void
  380. frv_linux_iterate_over_regset_sections (struct gdbarch *gdbarch,
  381. iterate_over_regset_sections_cb *cb,
  382. void *cb_data,
  383. const struct regcache *regcache)
  384. {
  385. cb (".reg", sizeof (frv_elf_gregset_t), sizeof (frv_elf_gregset_t),
  386. &frv_linux_gregset, NULL, cb_data);
  387. cb (".reg2", sizeof (frv_elf_fpregset_t), sizeof (frv_elf_fpregset_t),
  388. &frv_linux_fpregset, NULL, cb_data);
  389. }
  390. static void
  391. frv_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  392. {
  393. linux_init_abi (info, gdbarch, 0);
  394. /* Set the sigtramp frame sniffer. */
  395. frame_unwind_append_unwinder (gdbarch, &frv_linux_sigtramp_frame_unwind);
  396. set_gdbarch_iterate_over_regset_sections
  397. (gdbarch, frv_linux_iterate_over_regset_sections);
  398. }
  399. static enum gdb_osabi
  400. frv_linux_elf_osabi_sniffer (bfd *abfd)
  401. {
  402. int elf_flags;
  403. elf_flags = elf_elfheader (abfd)->e_flags;
  404. /* Assume GNU/Linux if using the FDPIC ABI. If/when another OS shows
  405. up that uses this ABI, we'll need to start using .note sections
  406. or some such. */
  407. if (elf_flags & EF_FRV_FDPIC)
  408. return GDB_OSABI_LINUX;
  409. else
  410. return GDB_OSABI_UNKNOWN;
  411. }
  412. void _initialize_frv_linux_tdep ();
  413. void
  414. _initialize_frv_linux_tdep ()
  415. {
  416. gdbarch_register_osabi (bfd_arch_frv, 0, GDB_OSABI_LINUX,
  417. frv_linux_init_abi);
  418. gdbarch_register_osabi_sniffer (bfd_arch_frv,
  419. bfd_target_elf_flavour,
  420. frv_linux_elf_osabi_sniffer);
  421. }