vax-tdep.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /* Target-dependent code for the VAX.
  2. Copyright (C) 1986-2022 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 "arch-utils.h"
  16. #include "dis-asm.h"
  17. #include "frame.h"
  18. #include "frame-base.h"
  19. #include "frame-unwind.h"
  20. #include "gdbcore.h"
  21. #include "gdbtypes.h"
  22. #include "osabi.h"
  23. #include "regcache.h"
  24. #include "regset.h"
  25. #include "trad-frame.h"
  26. #include "value.h"
  27. #include "vax-tdep.h"
  28. /* Return the name of register REGNUM. */
  29. static const char *
  30. vax_register_name (struct gdbarch *gdbarch, int regnum)
  31. {
  32. static const char *register_names[] =
  33. {
  34. "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  35. "r8", "r9", "r10", "r11", "ap", "fp", "sp", "pc",
  36. "ps",
  37. };
  38. if (regnum >= 0 && regnum < ARRAY_SIZE (register_names))
  39. return register_names[regnum];
  40. return NULL;
  41. }
  42. /* Return the GDB type object for the "standard" data type of data in
  43. register REGNUM. */
  44. static struct type *
  45. vax_register_type (struct gdbarch *gdbarch, int regnum)
  46. {
  47. return builtin_type (gdbarch)->builtin_int;
  48. }
  49. /* Core file support. */
  50. /* Supply register REGNUM from the buffer specified by GREGS and LEN
  51. in the general-purpose register set REGSET to register cache
  52. REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
  53. static void
  54. vax_supply_gregset (const struct regset *regset, struct regcache *regcache,
  55. int regnum, const void *gregs, size_t len)
  56. {
  57. const gdb_byte *regs = (const gdb_byte *) gregs;
  58. int i;
  59. for (i = 0; i < VAX_NUM_REGS; i++)
  60. {
  61. if (regnum == i || regnum == -1)
  62. regcache->raw_supply (i, regs + i * 4);
  63. }
  64. }
  65. /* VAX register set. */
  66. static const struct regset vax_gregset =
  67. {
  68. NULL,
  69. vax_supply_gregset
  70. };
  71. /* Iterate over core file register note sections. */
  72. static void
  73. vax_iterate_over_regset_sections (struct gdbarch *gdbarch,
  74. iterate_over_regset_sections_cb *cb,
  75. void *cb_data,
  76. const struct regcache *regcache)
  77. {
  78. cb (".reg", VAX_NUM_REGS * 4, VAX_NUM_REGS * 4, &vax_gregset, NULL, cb_data);
  79. }
  80. /* The VAX UNIX calling convention uses R1 to pass a structure return
  81. value address instead of passing it as a first (hidden) argument as
  82. the VMS calling convention suggests. */
  83. static CORE_ADDR
  84. vax_store_arguments (struct regcache *regcache, int nargs,
  85. struct value **args, CORE_ADDR sp)
  86. {
  87. struct gdbarch *gdbarch = regcache->arch ();
  88. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  89. gdb_byte buf[4];
  90. int count = 0;
  91. int i;
  92. /* We create an argument list on the stack, and make the argument
  93. pointer to it. */
  94. /* Push arguments in reverse order. */
  95. for (i = nargs - 1; i >= 0; i--)
  96. {
  97. int len = TYPE_LENGTH (value_enclosing_type (args[i]));
  98. sp -= (len + 3) & ~3;
  99. count += (len + 3) / 4;
  100. write_memory (sp, value_contents_all (args[i]).data (), len);
  101. }
  102. /* Push argument count. */
  103. sp -= 4;
  104. store_unsigned_integer (buf, 4, byte_order, count);
  105. write_memory (sp, buf, 4);
  106. /* Update the argument pointer. */
  107. store_unsigned_integer (buf, 4, byte_order, sp);
  108. regcache->cooked_write (VAX_AP_REGNUM, buf);
  109. return sp;
  110. }
  111. static CORE_ADDR
  112. vax_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  113. struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
  114. struct value **args, CORE_ADDR sp,
  115. function_call_return_method return_method,
  116. CORE_ADDR struct_addr)
  117. {
  118. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  119. CORE_ADDR fp = sp;
  120. gdb_byte buf[4];
  121. /* Set up the function arguments. */
  122. sp = vax_store_arguments (regcache, nargs, args, sp);
  123. /* Store return value address. */
  124. if (return_method == return_method_struct)
  125. regcache_cooked_write_unsigned (regcache, VAX_R1_REGNUM, struct_addr);
  126. /* Store return address in the PC slot. */
  127. sp -= 4;
  128. store_unsigned_integer (buf, 4, byte_order, bp_addr);
  129. write_memory (sp, buf, 4);
  130. /* Store the (fake) frame pointer in the FP slot. */
  131. sp -= 4;
  132. store_unsigned_integer (buf, 4, byte_order, fp);
  133. write_memory (sp, buf, 4);
  134. /* Skip the AP slot. */
  135. sp -= 4;
  136. /* Store register save mask and control bits. */
  137. sp -= 4;
  138. store_unsigned_integer (buf, 4, byte_order, 0);
  139. write_memory (sp, buf, 4);
  140. /* Store condition handler. */
  141. sp -= 4;
  142. store_unsigned_integer (buf, 4, byte_order, 0);
  143. write_memory (sp, buf, 4);
  144. /* Update the stack pointer and frame pointer. */
  145. store_unsigned_integer (buf, 4, byte_order, sp);
  146. regcache->cooked_write (VAX_SP_REGNUM, buf);
  147. regcache->cooked_write (VAX_FP_REGNUM, buf);
  148. /* Return the saved (fake) frame pointer. */
  149. return fp;
  150. }
  151. static struct frame_id
  152. vax_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  153. {
  154. CORE_ADDR fp;
  155. fp = get_frame_register_unsigned (this_frame, VAX_FP_REGNUM);
  156. return frame_id_build (fp, get_frame_pc (this_frame));
  157. }
  158. static enum return_value_convention
  159. vax_return_value (struct gdbarch *gdbarch, struct value *function,
  160. struct type *type, struct regcache *regcache,
  161. gdb_byte *readbuf, const gdb_byte *writebuf)
  162. {
  163. int len = TYPE_LENGTH (type);
  164. gdb_byte buf[8];
  165. if (type->code () == TYPE_CODE_STRUCT
  166. || type->code () == TYPE_CODE_UNION
  167. || type->code () == TYPE_CODE_ARRAY)
  168. {
  169. /* The default on VAX is to return structures in static memory.
  170. Consequently a function must return the address where we can
  171. find the return value. */
  172. if (readbuf)
  173. {
  174. ULONGEST addr;
  175. regcache_raw_read_unsigned (regcache, VAX_R0_REGNUM, &addr);
  176. read_memory (addr, readbuf, len);
  177. }
  178. return RETURN_VALUE_ABI_RETURNS_ADDRESS;
  179. }
  180. if (readbuf)
  181. {
  182. /* Read the contents of R0 and (if necessary) R1. */
  183. regcache->cooked_read (VAX_R0_REGNUM, buf);
  184. if (len > 4)
  185. regcache->cooked_read (VAX_R1_REGNUM, buf + 4);
  186. memcpy (readbuf, buf, len);
  187. }
  188. if (writebuf)
  189. {
  190. /* Read the contents to R0 and (if necessary) R1. */
  191. memcpy (buf, writebuf, len);
  192. regcache->cooked_write (VAX_R0_REGNUM, buf);
  193. if (len > 4)
  194. regcache->cooked_write (VAX_R1_REGNUM, buf + 4);
  195. }
  196. return RETURN_VALUE_REGISTER_CONVENTION;
  197. }
  198. /* Use the program counter to determine the contents and size of a
  199. breakpoint instruction. Return a pointer to a string of bytes that
  200. encode a breakpoint instruction, store the length of the string in
  201. *LEN and optionally adjust *PC to point to the correct memory
  202. location for inserting the breakpoint. */
  203. constexpr gdb_byte vax_break_insn[] = { 3 };
  204. typedef BP_MANIPULATION (vax_break_insn) vax_breakpoint;
  205. /* Advance PC across any function entry prologue instructions
  206. to reach some "real" code. */
  207. static CORE_ADDR
  208. vax_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  209. {
  210. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  211. gdb_byte op = read_memory_unsigned_integer (pc, 1, byte_order);
  212. if (op == 0x11)
  213. pc += 2; /* skip brb */
  214. if (op == 0x31)
  215. pc += 3; /* skip brw */
  216. if (op == 0xC2
  217. && read_memory_unsigned_integer (pc + 2, 1, byte_order) == 0x5E)
  218. pc += 3; /* skip subl2 */
  219. if (op == 0x9E
  220. && read_memory_unsigned_integer (pc + 1, 1, byte_order) == 0xAE
  221. && read_memory_unsigned_integer (pc + 3, 1, byte_order) == 0x5E)
  222. pc += 4; /* skip movab */
  223. if (op == 0x9E
  224. && read_memory_unsigned_integer (pc + 1, 1, byte_order) == 0xCE
  225. && read_memory_unsigned_integer (pc + 4, 1, byte_order) == 0x5E)
  226. pc += 5; /* skip movab */
  227. if (op == 0x9E
  228. && read_memory_unsigned_integer (pc + 1, 1, byte_order) == 0xEE
  229. && read_memory_unsigned_integer (pc + 6, 1, byte_order) == 0x5E)
  230. pc += 7; /* skip movab */
  231. return pc;
  232. }
  233. /* Unwinding the stack is relatively easy since the VAX has a
  234. dedicated frame pointer, and frames are set up automatically as the
  235. result of a function call. Most of the relevant information can be
  236. inferred from the documentation of the Procedure Call Instructions
  237. in the VAX MACRO and Instruction Set Reference Manual. */
  238. struct vax_frame_cache
  239. {
  240. /* Base address. */
  241. CORE_ADDR base;
  242. /* Table of saved registers. */
  243. trad_frame_saved_reg *saved_regs;
  244. };
  245. static struct vax_frame_cache *
  246. vax_frame_cache (struct frame_info *this_frame, void **this_cache)
  247. {
  248. struct vax_frame_cache *cache;
  249. CORE_ADDR addr;
  250. ULONGEST mask;
  251. int regnum;
  252. if (*this_cache)
  253. return (struct vax_frame_cache *) *this_cache;
  254. /* Allocate a new cache. */
  255. cache = FRAME_OBSTACK_ZALLOC (struct vax_frame_cache);
  256. cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
  257. /* The frame pointer is used as the base for the frame. */
  258. cache->base = get_frame_register_unsigned (this_frame, VAX_FP_REGNUM);
  259. if (cache->base == 0)
  260. return cache;
  261. /* The register save mask and control bits determine the layout of
  262. the stack frame. */
  263. mask = get_frame_memory_unsigned (this_frame, cache->base + 4, 4) >> 16;
  264. /* These are always saved. */
  265. cache->saved_regs[VAX_PC_REGNUM].set_addr (cache->base + 16);
  266. cache->saved_regs[VAX_FP_REGNUM].set_addr (cache->base + 12);
  267. cache->saved_regs[VAX_AP_REGNUM].set_addr (cache->base + 8);
  268. cache->saved_regs[VAX_PS_REGNUM].set_addr (cache->base + 4);
  269. /* Scan the register save mask and record the location of the saved
  270. registers. */
  271. addr = cache->base + 20;
  272. for (regnum = 0; regnum < VAX_AP_REGNUM; regnum++)
  273. {
  274. if (mask & (1 << regnum))
  275. {
  276. cache->saved_regs[regnum].set_addr (addr);
  277. addr += 4;
  278. }
  279. }
  280. /* The CALLS/CALLG flag determines whether this frame has a General
  281. Argument List or a Stack Argument List. */
  282. if (mask & (1 << 13))
  283. {
  284. ULONGEST numarg;
  285. /* This is a procedure with Stack Argument List. Adjust the
  286. stack address for the arguments that were pushed onto the
  287. stack. The return instruction will automatically pop the
  288. arguments from the stack. */
  289. numarg = get_frame_memory_unsigned (this_frame, addr, 1);
  290. addr += 4 + numarg * 4;
  291. }
  292. /* Bits 1:0 of the stack pointer were saved in the control bits. */
  293. cache->saved_regs[VAX_SP_REGNUM].set_value (addr + (mask >> 14));
  294. return cache;
  295. }
  296. static void
  297. vax_frame_this_id (struct frame_info *this_frame, void **this_cache,
  298. struct frame_id *this_id)
  299. {
  300. struct vax_frame_cache *cache = vax_frame_cache (this_frame, this_cache);
  301. /* This marks the outermost frame. */
  302. if (cache->base == 0)
  303. return;
  304. (*this_id) = frame_id_build (cache->base, get_frame_func (this_frame));
  305. }
  306. static struct value *
  307. vax_frame_prev_register (struct frame_info *this_frame,
  308. void **this_cache, int regnum)
  309. {
  310. struct vax_frame_cache *cache = vax_frame_cache (this_frame, this_cache);
  311. return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
  312. }
  313. static const struct frame_unwind vax_frame_unwind =
  314. {
  315. "vax prologue",
  316. NORMAL_FRAME,
  317. default_frame_unwind_stop_reason,
  318. vax_frame_this_id,
  319. vax_frame_prev_register,
  320. NULL,
  321. default_frame_sniffer
  322. };
  323. static CORE_ADDR
  324. vax_frame_base_address (struct frame_info *this_frame, void **this_cache)
  325. {
  326. struct vax_frame_cache *cache = vax_frame_cache (this_frame, this_cache);
  327. return cache->base;
  328. }
  329. static CORE_ADDR
  330. vax_frame_args_address (struct frame_info *this_frame, void **this_cache)
  331. {
  332. return get_frame_register_unsigned (this_frame, VAX_AP_REGNUM);
  333. }
  334. static const struct frame_base vax_frame_base =
  335. {
  336. &vax_frame_unwind,
  337. vax_frame_base_address,
  338. vax_frame_base_address,
  339. vax_frame_args_address
  340. };
  341. /* Return number of arguments for FRAME. */
  342. static int
  343. vax_frame_num_args (struct frame_info *frame)
  344. {
  345. CORE_ADDR args;
  346. /* Assume that the argument pointer for the outermost frame is
  347. hosed, as is the case on NetBSD/vax ELF. */
  348. if (get_frame_base_address (frame) == 0)
  349. return 0;
  350. args = get_frame_register_unsigned (frame, VAX_AP_REGNUM);
  351. return get_frame_memory_unsigned (frame, args, 1);
  352. }
  353. /* Initialize the current architecture based on INFO. If possible, re-use an
  354. architecture from ARCHES, which is a list of architectures already created
  355. during this debugging session.
  356. Called e.g. at program startup, when reading a core file, and when reading
  357. a binary file. */
  358. static struct gdbarch *
  359. vax_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  360. {
  361. struct gdbarch *gdbarch;
  362. /* If there is already a candidate, use it. */
  363. arches = gdbarch_list_lookup_by_info (arches, &info);
  364. if (arches != NULL)
  365. return arches->gdbarch;
  366. gdbarch = gdbarch_alloc (&info, NULL);
  367. set_gdbarch_float_format (gdbarch, floatformats_vax_f);
  368. set_gdbarch_double_format (gdbarch, floatformats_vax_d);
  369. set_gdbarch_long_double_format (gdbarch, floatformats_vax_d);
  370. set_gdbarch_long_double_bit (gdbarch, 64);
  371. /* Register info */
  372. set_gdbarch_num_regs (gdbarch, VAX_NUM_REGS);
  373. set_gdbarch_register_name (gdbarch, vax_register_name);
  374. set_gdbarch_register_type (gdbarch, vax_register_type);
  375. set_gdbarch_sp_regnum (gdbarch, VAX_SP_REGNUM);
  376. set_gdbarch_pc_regnum (gdbarch, VAX_PC_REGNUM);
  377. set_gdbarch_ps_regnum (gdbarch, VAX_PS_REGNUM);
  378. set_gdbarch_iterate_over_regset_sections
  379. (gdbarch, vax_iterate_over_regset_sections);
  380. /* Frame and stack info */
  381. set_gdbarch_skip_prologue (gdbarch, vax_skip_prologue);
  382. set_gdbarch_frame_num_args (gdbarch, vax_frame_num_args);
  383. set_gdbarch_frame_args_skip (gdbarch, 4);
  384. /* Stack grows downward. */
  385. set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  386. /* Return value info */
  387. set_gdbarch_return_value (gdbarch, vax_return_value);
  388. /* Call dummy code. */
  389. set_gdbarch_push_dummy_call (gdbarch, vax_push_dummy_call);
  390. set_gdbarch_dummy_id (gdbarch, vax_dummy_id);
  391. /* Breakpoint info */
  392. set_gdbarch_breakpoint_kind_from_pc (gdbarch, vax_breakpoint::kind_from_pc);
  393. set_gdbarch_sw_breakpoint_from_kind (gdbarch, vax_breakpoint::bp_from_kind);
  394. /* Misc info */
  395. set_gdbarch_deprecated_function_start_offset (gdbarch, 2);
  396. set_gdbarch_believe_pcc_promotion (gdbarch, 1);
  397. frame_base_set_default (gdbarch, &vax_frame_base);
  398. /* Hook in ABI-specific overrides, if they have been registered. */
  399. gdbarch_init_osabi (info, gdbarch);
  400. frame_unwind_append_unwinder (gdbarch, &vax_frame_unwind);
  401. return (gdbarch);
  402. }
  403. void _initialize_vax_tdep ();
  404. void
  405. _initialize_vax_tdep ()
  406. {
  407. gdbarch_register (bfd_arch_vax, vax_gdbarch_init, NULL);
  408. }