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