loongarch-linux-tdep.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* Target-dependent code for GNU/Linux on LoongArch processors.
  2. Copyright (C) 2022 Free Software Foundation, Inc.
  3. Contributed by Loongson Ltd.
  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 "glibc-tdep.h"
  17. #include "inferior.h"
  18. #include "linux-tdep.h"
  19. #include "loongarch-tdep.h"
  20. #include "solib-svr4.h"
  21. #include "target-descriptions.h"
  22. #include "trad-frame.h"
  23. #include "tramp-frame.h"
  24. /* Unpack an elf_gregset_t into GDB's register cache. */
  25. static void
  26. loongarch_supply_gregset (const struct regset *r,
  27. struct regcache *regcache, int regno,
  28. const void *gprs, size_t len)
  29. {
  30. loongarch_gdbarch_tdep *tdep
  31. = (loongarch_gdbarch_tdep *) gdbarch_tdep (regcache->arch ());
  32. auto regs = tdep->regs;
  33. int regsize = register_size (regcache->arch (), regs.r);
  34. const gdb_byte *buf = nullptr;
  35. if (regno == -1)
  36. {
  37. /* Set $r0 = 0. */
  38. regcache->raw_supply_zeroed (regs.r);
  39. for (int i = 1; i < 32; i++)
  40. {
  41. buf = (const gdb_byte*) gprs + regsize * i;
  42. regcache->raw_supply (regs.r + i, (const void *) buf);
  43. }
  44. /* Size base (pc) = regsize * regs.pc. */
  45. buf = (const gdb_byte*) gprs + regsize * regs.pc;
  46. regcache->raw_supply (regs.pc, (const void *) buf);
  47. /* Size base (badv) = regsize * regs.badv. */
  48. buf = (const gdb_byte*) gprs + regsize * regs.badv;
  49. regcache->raw_supply (regs.badv, (const void *) buf);
  50. }
  51. else if (regs.r == regno)
  52. regcache->raw_supply_zeroed (regno);
  53. else if ((regs.r < regno && regno < regs.r + 32)
  54. || regs.pc == regno || regs.badv == regno)
  55. {
  56. /* Offset offset (regno) = regsize * (regno - regs.r). */
  57. buf = (const gdb_byte*) gprs + regsize * (regno - regs.r);
  58. regcache->raw_supply (regno, (const void *) buf);
  59. }
  60. }
  61. /* Pack the GDB's register cache value into an elf_gregset_t. */
  62. static void
  63. loongarch_fill_gregset (const struct regset *r,
  64. const struct regcache *regcache, int regno,
  65. void *gprs, size_t len)
  66. {
  67. loongarch_gdbarch_tdep *tdep
  68. = (loongarch_gdbarch_tdep *) gdbarch_tdep (regcache->arch ());
  69. auto regs = tdep->regs;
  70. int regsize = register_size (regcache->arch (), regs.r);
  71. gdb_byte *buf = nullptr;
  72. if (regno == -1)
  73. {
  74. for (int i = 0; i < 32; i++)
  75. {
  76. buf = (gdb_byte *) gprs + regsize * i;
  77. regcache->raw_collect (regs.r + i, (void *) buf);
  78. }
  79. /* Size base (pc) = regsize * regs.pc. */
  80. buf = (gdb_byte *) gprs + regsize * regs.pc;
  81. regcache->raw_collect (regs.pc, (void *) buf);
  82. /* Size base (badv) = regsize * regs.badv. */
  83. buf = (gdb_byte *) gprs + regsize * regs.badv;
  84. regcache->raw_collect (regs.badv, (void *) buf);
  85. }
  86. else if ((regs.r <= regno && regno < regs.r + 32)
  87. || regs.pc == regno || regs.badv == regno)
  88. {
  89. /* Offset offset (regno) = regsize * (regno - regs.r). */
  90. buf = (gdb_byte *) gprs + regsize * (regno - regs.r);
  91. regcache->raw_collect (regno, (void *) buf);
  92. }
  93. }
  94. /* Register set definitions. */
  95. const struct regset loongarch_gregset =
  96. {
  97. nullptr,
  98. loongarch_supply_gregset,
  99. loongarch_fill_gregset,
  100. };
  101. /* Implement the "init" method of struct tramp_frame. */
  102. #define LOONGARCH_RT_SIGFRAME_UCONTEXT_OFFSET 128
  103. #define LOONGARCH_UCONTEXT_SIGCONTEXT_OFFSET 176
  104. static void
  105. loongarch_linux_rt_sigframe_init (const struct tramp_frame *self,
  106. struct frame_info *this_frame,
  107. struct trad_frame_cache *this_cache,
  108. CORE_ADDR func)
  109. {
  110. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  111. loongarch_gdbarch_tdep *tdep = (loongarch_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  112. auto regs = tdep->regs;
  113. CORE_ADDR frame_sp = get_frame_sp (this_frame);
  114. CORE_ADDR sigcontext_base = (frame_sp + LOONGARCH_RT_SIGFRAME_UCONTEXT_OFFSET
  115. + LOONGARCH_UCONTEXT_SIGCONTEXT_OFFSET);
  116. trad_frame_set_reg_addr (this_cache, regs.pc, sigcontext_base);
  117. for (int i = 0; i < 32; i++)
  118. trad_frame_set_reg_addr (this_cache, regs.r + i, sigcontext_base + 8 + i * 8);
  119. trad_frame_set_id (this_cache, frame_id_build (frame_sp, func));
  120. }
  121. /* li.w a7, __NR_rt_sigreturn */
  122. #define LOONGARCH_INST_LIW_A7_RT_SIGRETURN 0x03822c0b
  123. /* syscall 0 */
  124. #define LOONGARCH_INST_SYSCALL 0x002b0000
  125. static const struct tramp_frame loongarch_linux_rt_sigframe =
  126. {
  127. SIGTRAMP_FRAME,
  128. 4,
  129. {
  130. { LOONGARCH_INST_LIW_A7_RT_SIGRETURN, ULONGEST_MAX },
  131. { LOONGARCH_INST_SYSCALL, ULONGEST_MAX },
  132. { TRAMP_SENTINEL_INSN, ULONGEST_MAX }
  133. },
  134. loongarch_linux_rt_sigframe_init,
  135. NULL
  136. };
  137. /* Initialize LoongArch Linux ABI info. */
  138. static void
  139. loongarch_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  140. {
  141. linux_init_abi (info, gdbarch, 0);
  142. set_solib_svr4_fetch_link_map_offsets (gdbarch,
  143. info.bfd_arch_info->bits_per_address == 32
  144. ? linux_ilp32_fetch_link_map_offsets
  145. : linux_lp64_fetch_link_map_offsets);
  146. /* GNU/Linux uses SVR4-style shared libraries. */
  147. set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
  148. /* GNU/Linux uses the dynamic linker included in the GNU C Library. */
  149. set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
  150. /* Enable TLS support. */
  151. set_gdbarch_fetch_tls_load_module_address (gdbarch, svr4_fetch_objfile_link_map);
  152. /* Prepend tramp frame unwinder for signal. */
  153. tramp_frame_prepend_unwinder (gdbarch, &loongarch_linux_rt_sigframe);
  154. }
  155. /* Initialize LoongArch Linux target support. */
  156. void _initialize_loongarch_linux_tdep ();
  157. void
  158. _initialize_loongarch_linux_tdep ()
  159. {
  160. gdbarch_register_osabi (bfd_arch_loongarch, bfd_mach_loongarch32,
  161. GDB_OSABI_LINUX, loongarch_linux_init_abi);
  162. gdbarch_register_osabi (bfd_arch_loongarch, bfd_mach_loongarch64,
  163. GDB_OSABI_LINUX, loongarch_linux_init_abi);
  164. }