xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/amd64-netbsd-tdep.c (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
1*6881a400Schristos /* Target-dependent code for NetBSD/amd64.
2*6881a400Schristos 
3*6881a400Schristos    Copyright (C) 2003-2023 Free Software Foundation, Inc.
4*6881a400Schristos 
5*6881a400Schristos    This file is part of GDB.
6*6881a400Schristos 
7*6881a400Schristos    This program is free software; you can redistribute it and/or modify
8*6881a400Schristos    it under the terms of the GNU General Public License as published by
9*6881a400Schristos    the Free Software Foundation; either version 3 of the License, or
10*6881a400Schristos    (at your option) any later version.
11*6881a400Schristos 
12*6881a400Schristos    This program is distributed in the hope that it will be useful,
13*6881a400Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
14*6881a400Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*6881a400Schristos    GNU General Public License for more details.
16*6881a400Schristos 
17*6881a400Schristos    You should have received a copy of the GNU General Public License
18*6881a400Schristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19*6881a400Schristos 
20*6881a400Schristos #include "defs.h"
21*6881a400Schristos #include "arch-utils.h"
22*6881a400Schristos #include "frame.h"
23*6881a400Schristos #include "gdbcore.h"
24*6881a400Schristos #include "osabi.h"
25*6881a400Schristos #include "symtab.h"
26*6881a400Schristos 
27*6881a400Schristos #include "amd64-tdep.h"
28*6881a400Schristos #include "gdbsupport/x86-xstate.h"
29*6881a400Schristos #include "netbsd-tdep.h"
30*6881a400Schristos #include "solib-svr4.h"
31*6881a400Schristos #include "trad-frame.h"
32*6881a400Schristos #include "frame-unwind.h"
33*6881a400Schristos 
34*6881a400Schristos /* Support for signal handlers.  */
35*6881a400Schristos 
36*6881a400Schristos /* Return whether THIS_FRAME corresponds to a NetBSD sigtramp
37*6881a400Schristos    routine.  */
38*6881a400Schristos 
39*6881a400Schristos static int
40*6881a400Schristos amd64nbsd_sigtramp_p (frame_info_ptr this_frame)
41*6881a400Schristos {
42*6881a400Schristos   CORE_ADDR pc = get_frame_pc (this_frame);
43*6881a400Schristos   const char *name;
44*6881a400Schristos 
45*6881a400Schristos   find_pc_partial_function (pc, &name, NULL, NULL);
46*6881a400Schristos   return nbsd_pc_in_sigtramp (pc, name);
47*6881a400Schristos }
48*6881a400Schristos 
49*6881a400Schristos /* Assuming THIS_FRAME corresponds to a NetBSD sigtramp routine,
50*6881a400Schristos    return the address of the associated mcontext structure.  */
51*6881a400Schristos 
52*6881a400Schristos static CORE_ADDR
53*6881a400Schristos amd64nbsd_mcontext_addr (frame_info_ptr this_frame)
54*6881a400Schristos {
55*6881a400Schristos   CORE_ADDR addr;
56*6881a400Schristos 
57*6881a400Schristos   /* The register %r15 points at `struct ucontext' upon entry of a
58*6881a400Schristos      signal trampoline.  */
59*6881a400Schristos   addr = get_frame_register_unsigned (this_frame, AMD64_R15_REGNUM);
60*6881a400Schristos 
61*6881a400Schristos   /* The mcontext structure lives as offset 56 in `struct ucontext'.  */
62*6881a400Schristos   return addr + 56;
63*6881a400Schristos }
64*6881a400Schristos 
65*6881a400Schristos /* NetBSD 2.0 or later.  */
66*6881a400Schristos 
67*6881a400Schristos /* Mapping between the general-purpose registers in `struct reg'
68*6881a400Schristos    format and GDB's register cache layout.  */
69*6881a400Schristos 
70*6881a400Schristos /* From <machine/reg.h>.  */
71*6881a400Schristos int amd64nbsd_r_reg_offset[] =
72*6881a400Schristos {
73*6881a400Schristos   14 * 8,			/* %rax */
74*6881a400Schristos   13 * 8,			/* %rbx */
75*6881a400Schristos   3 * 8,			/* %rcx */
76*6881a400Schristos   2 * 8,			/* %rdx */
77*6881a400Schristos   1 * 8,			/* %rsi */
78*6881a400Schristos   0 * 8,			/* %rdi */
79*6881a400Schristos   12 * 8,			/* %rbp */
80*6881a400Schristos   24 * 8,			/* %rsp */
81*6881a400Schristos   4 * 8,			/* %r8 ..  */
82*6881a400Schristos   5 * 8,
83*6881a400Schristos   6 * 8,
84*6881a400Schristos   7 * 8,
85*6881a400Schristos   8 * 8,
86*6881a400Schristos   9 * 8,
87*6881a400Schristos   10 * 8,
88*6881a400Schristos   11 * 8,			/* ... %r15 */
89*6881a400Schristos   21 * 8,			/* %rip */
90*6881a400Schristos   23 * 8,			/* %eflags */
91*6881a400Schristos   22 * 8,			/* %cs */
92*6881a400Schristos   25 * 8,			/* %ss */
93*6881a400Schristos   18 * 8,			/* %ds */
94*6881a400Schristos   17 * 8,			/* %es */
95*6881a400Schristos   16 * 8,			/* %fs */
96*6881a400Schristos   15 * 8			/* %gs */
97*6881a400Schristos };
98*6881a400Schristos 
99*6881a400Schristos /* Kernel debugging support */
100*6881a400Schristos static const int amd64nbsd_tf_reg_offset[] =
101*6881a400Schristos {
102*6881a400Schristos   18 * 8,			/* %rax */
103*6881a400Schristos   17 * 8,			/* %rbx */
104*6881a400Schristos   10 * 8,			/* %rcx */
105*6881a400Schristos   2 * 8,			/* %rdx */
106*6881a400Schristos   1 * 8,			/* %rsi */
107*6881a400Schristos   0 * 8,			/* %rdi */
108*6881a400Schristos   16 * 8,			/* %rbp */
109*6881a400Schristos   28 * 8,			/* %rsp */
110*6881a400Schristos   4 * 8,			/* %r8 .. */
111*6881a400Schristos   5 * 8,
112*6881a400Schristos   3 * 8,
113*6881a400Schristos   11 * 8,
114*6881a400Schristos   12 * 8,
115*6881a400Schristos   13 * 8,
116*6881a400Schristos   14 * 8,
117*6881a400Schristos   15 * 8,			/* ... %r15 */
118*6881a400Schristos   25 * 8,			/* %rip */
119*6881a400Schristos   27 * 8,			/* %eflags */
120*6881a400Schristos   26 * 8,			/* %cs */
121*6881a400Schristos   29 * 8,			/* %ss */
122*6881a400Schristos   22 * 8,			/* %ds */
123*6881a400Schristos   21 * 8,			/* %es */
124*6881a400Schristos   20 * 8,			/* %fs */
125*6881a400Schristos   19 * 8,			/* %gs */
126*6881a400Schristos };
127*6881a400Schristos 
128*6881a400Schristos static struct trad_frame_cache *
129*6881a400Schristos amd64nbsd_trapframe_cache(frame_info_ptr this_frame, void **this_cache)
130*6881a400Schristos {
131*6881a400Schristos   struct trad_frame_cache *cache;
132*6881a400Schristos   CORE_ADDR func, sp, addr;
133*6881a400Schristos   ULONGEST cs = 0, rip = 0;
134*6881a400Schristos   const char *name;
135*6881a400Schristos   int i;
136*6881a400Schristos   struct gdbarch *gdbarch = get_frame_arch (this_frame);
137*6881a400Schristos   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
138*6881a400Schristos 
139*6881a400Schristos   if (*this_cache)
140*6881a400Schristos     return (struct trad_frame_cache *)*this_cache;
141*6881a400Schristos 
142*6881a400Schristos   cache = trad_frame_cache_zalloc (this_frame);
143*6881a400Schristos   *this_cache = cache;
144*6881a400Schristos 
145*6881a400Schristos   func = get_frame_func (this_frame);
146*6881a400Schristos   sp = get_frame_register_unsigned (this_frame, AMD64_RSP_REGNUM);
147*6881a400Schristos 
148*6881a400Schristos   find_pc_partial_function (func, &name, NULL, NULL);
149*6881a400Schristos 
150*6881a400Schristos   /* There is an extra 'call' in the interrupt sequence - ignore the extra
151*6881a400Schristos    * return address */
152*6881a400Schristos 
153*6881a400Schristos   addr = sp;
154*6881a400Schristos   if (name) {
155*6881a400Schristos 	if (strncmp (name, "Xintr", 5) == 0
156*6881a400Schristos 	 || strncmp (name, "Xhandle", 7) == 0) {
157*6881a400Schristos 		addr += 8;		/* It's an interrupt frame.  */
158*6881a400Schristos 	}
159*6881a400Schristos   }
160*6881a400Schristos 
161*6881a400Schristos #ifdef DEBUG_TRAPFRAME
162*6881a400Schristos   for (i = 0; i < 50; i++) {
163*6881a400Schristos     cs = read_memory_unsigned_integer (addr + i * 8, 8, byte_order);
164*6881a400Schristos     printf("%s i=%d r=%#jx\n", name, i, (intmax_t)cs);
165*6881a400Schristos   }
166*6881a400Schristos #endif
167*6881a400Schristos 
168*6881a400Schristos   for (i = 0; i < ARRAY_SIZE (amd64nbsd_tf_reg_offset); i++)
169*6881a400Schristos     if (amd64nbsd_tf_reg_offset[i] != -1)
170*6881a400Schristos       trad_frame_set_reg_addr (cache, i, addr + amd64nbsd_tf_reg_offset[i]);
171*6881a400Schristos 
172*6881a400Schristos   /* Read %cs and %rip when we have the addresses to hand */
173*6881a400Schristos   cs = read_memory_unsigned_integer (addr
174*6881a400Schristos     + amd64nbsd_tf_reg_offset[AMD64_CS_REGNUM], 8, byte_order);
175*6881a400Schristos   rip = read_memory_unsigned_integer (addr
176*6881a400Schristos     + amd64nbsd_tf_reg_offset[AMD64_RIP_REGNUM], 8, byte_order);
177*6881a400Schristos 
178*6881a400Schristos #ifdef DEBUG_TRAPFRAME
179*6881a400Schristos   printf("%s cs=%#jx rip=%#jx\n", name, (intmax_t)cs, (intmax_t)rip);
180*6881a400Schristos #endif
181*6881a400Schristos 
182*6881a400Schristos   /* The trap frame layout was changed lf the %rip value is less than 2^16 it
183*6881a400Schristos    * is almost certainly the %ss of the old format. */
184*6881a400Schristos   if (rip < (1 << 16))
185*6881a400Schristos     {
186*6881a400Schristos 
187*6881a400Schristos       for (i = 0; i < ARRAY_SIZE (amd64nbsd_tf_reg_offset); i++)
188*6881a400Schristos         {
189*6881a400Schristos 
190*6881a400Schristos           if (amd64nbsd_tf_reg_offset[i] == -1)
191*6881a400Schristos             continue;
192*6881a400Schristos 
193*6881a400Schristos           trad_frame_set_reg_addr (cache, i, addr + amd64nbsd_r_reg_offset[i]);
194*6881a400Schristos 
195*6881a400Schristos           /* Read %cs when we have the address to hand */
196*6881a400Schristos           if (i == AMD64_CS_REGNUM)
197*6881a400Schristos 	    cs = read_memory_unsigned_integer (addr + amd64nbsd_r_reg_offset[i],
198*6881a400Schristos 	    8, byte_order);
199*6881a400Schristos         }
200*6881a400Schristos     }
201*6881a400Schristos 
202*6881a400Schristos   if ((cs & I386_SEL_RPL) == I386_SEL_UPL ||
203*6881a400Schristos 	(name && strncmp(name, "Xsoft", 5) == 0))
204*6881a400Schristos     {
205*6881a400Schristos       /* Trap from user space or soft interrupt; terminate backtrace.  */
206*6881a400Schristos       trad_frame_set_id (cache, outer_frame_id);
207*6881a400Schristos     }
208*6881a400Schristos   else
209*6881a400Schristos     {
210*6881a400Schristos       /* Construct the frame ID using the function start.  */
211*6881a400Schristos       trad_frame_set_id (cache, frame_id_build (sp + 16, func));
212*6881a400Schristos     }
213*6881a400Schristos 
214*6881a400Schristos   return cache;
215*6881a400Schristos }
216*6881a400Schristos 
217*6881a400Schristos static void
218*6881a400Schristos amd64nbsd_trapframe_this_id (frame_info_ptr this_frame,
219*6881a400Schristos 			     void **this_cache,
220*6881a400Schristos 			     struct frame_id *this_id)
221*6881a400Schristos {
222*6881a400Schristos   struct trad_frame_cache *cache =
223*6881a400Schristos     amd64nbsd_trapframe_cache (this_frame, this_cache);
224*6881a400Schristos 
225*6881a400Schristos   trad_frame_get_id (cache, this_id);
226*6881a400Schristos }
227*6881a400Schristos 
228*6881a400Schristos static struct value *
229*6881a400Schristos amd64nbsd_trapframe_prev_register (frame_info_ptr this_frame,
230*6881a400Schristos 				   void **this_cache, int regnum)
231*6881a400Schristos {
232*6881a400Schristos   struct trad_frame_cache *cache =
233*6881a400Schristos     amd64nbsd_trapframe_cache (this_frame, this_cache);
234*6881a400Schristos 
235*6881a400Schristos   return trad_frame_get_register (cache, this_frame, regnum);
236*6881a400Schristos }
237*6881a400Schristos 
238*6881a400Schristos static int
239*6881a400Schristos amd64nbsd_trapframe_sniffer (const struct frame_unwind *self,
240*6881a400Schristos 			     frame_info_ptr this_frame,
241*6881a400Schristos 			     void **this_prologue_cache)
242*6881a400Schristos {
243*6881a400Schristos   ULONGEST cs = 0;
244*6881a400Schristos   const char *name;
245*6881a400Schristos 
246*6881a400Schristos   try
247*6881a400Schristos     {
248*6881a400Schristos       cs = get_frame_register_unsigned (this_frame, AMD64_CS_REGNUM);
249*6881a400Schristos     }
250*6881a400Schristos   catch (const gdb_exception &except)
251*6881a400Schristos     {
252*6881a400Schristos       if (except.reason < 0 && except.error != NOT_AVAILABLE_ERROR)
253*6881a400Schristos 	throw;
254*6881a400Schristos     }
255*6881a400Schristos   if ((cs & I386_SEL_RPL) == I386_SEL_UPL)
256*6881a400Schristos     return 0;
257*6881a400Schristos 
258*6881a400Schristos   find_pc_partial_function (get_frame_pc (this_frame), &name, NULL, NULL);
259*6881a400Schristos   return (name && ((strcmp (name, "alltraps") == 0)
260*6881a400Schristos 	        || (strcmp (name, "calltrap") == 0)
261*6881a400Schristos 	        || (strcmp (name, "handle_syscall") == 0)
262*6881a400Schristos 		|| (strcmp (name, "Xdoreti") == 0)
263*6881a400Schristos 		|| (strcmp (name, "Xspllower") == 0)
264*6881a400Schristos 		|| (strncmp (name, "Xhandle", 7) == 0)
265*6881a400Schristos 		|| (strncmp (name, "Xintr", 5) == 0)
266*6881a400Schristos 		|| (strncmp (name, "Xpreempt", 8) == 0)
267*6881a400Schristos 		|| (strncmp (name, "Xrecurse", 8) == 0)
268*6881a400Schristos 		|| (strncmp (name, "Xresume", 7) == 0)
269*6881a400Schristos 		|| (strncmp (name, "Xsoft", 5) == 0)
270*6881a400Schristos 		|| (strncmp (name, "Xstray", 6) == 0)
271*6881a400Schristos 		|| (strncmp (name, "Xsyscall", 8) == 0)
272*6881a400Schristos 		|| (strncmp (name, "Xtrap", 5) == 0)
273*6881a400Schristos 	    ));
274*6881a400Schristos }
275*6881a400Schristos 
276*6881a400Schristos static const struct frame_unwind amd64nbsd_trapframe_unwind = {
277*6881a400Schristos   /* FIXME: kettenis/20051219: This really is more like an interrupt
278*6881a400Schristos      frame, but SIGTRAMP_FRAME would print <signal handler called>,
279*6881a400Schristos      which really is not what we want here.  */
280*6881a400Schristos   "amd64 trapframe",
281*6881a400Schristos   NORMAL_FRAME,
282*6881a400Schristos   default_frame_unwind_stop_reason,
283*6881a400Schristos   amd64nbsd_trapframe_this_id,
284*6881a400Schristos   amd64nbsd_trapframe_prev_register,
285*6881a400Schristos   NULL,
286*6881a400Schristos   amd64nbsd_trapframe_sniffer
287*6881a400Schristos };
288*6881a400Schristos 
289*6881a400Schristos static void
290*6881a400Schristos amd64nbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
291*6881a400Schristos {
292*6881a400Schristos   i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
293*6881a400Schristos 
294*6881a400Schristos   /* Initialize general-purpose register set details first.  */
295*6881a400Schristos   tdep->gregset_reg_offset = amd64nbsd_r_reg_offset;
296*6881a400Schristos   tdep->gregset_num_regs = ARRAY_SIZE (amd64nbsd_r_reg_offset);
297*6881a400Schristos   tdep->sizeof_gregset = 26 * 8;
298*6881a400Schristos 
299*6881a400Schristos   amd64_init_abi (info, gdbarch,
300*6881a400Schristos 		  amd64_target_description (X86_XSTATE_SSE_MASK, true));
301*6881a400Schristos   nbsd_init_abi (info, gdbarch);
302*6881a400Schristos 
303*6881a400Schristos   tdep->jb_pc_offset = 7 * 8;
304*6881a400Schristos 
305*6881a400Schristos   /* NetBSD has its own convention for signal trampolines.  */
306*6881a400Schristos   tdep->sigtramp_p = amd64nbsd_sigtramp_p;
307*6881a400Schristos   tdep->sigcontext_addr = amd64nbsd_mcontext_addr;
308*6881a400Schristos   tdep->sc_reg_offset = amd64nbsd_r_reg_offset;
309*6881a400Schristos   tdep->sc_num_regs = ARRAY_SIZE (amd64nbsd_r_reg_offset);
310*6881a400Schristos 
311*6881a400Schristos   /* NetBSD uses SVR4-style shared libraries.  */
312*6881a400Schristos   set_solib_svr4_fetch_link_map_offsets
313*6881a400Schristos     (gdbarch, svr4_lp64_fetch_link_map_offsets);
314*6881a400Schristos   /* Unwind kernel trap frames correctly.  */
315*6881a400Schristos   frame_unwind_prepend_unwinder (gdbarch, &amd64nbsd_trapframe_unwind);
316*6881a400Schristos }
317*6881a400Schristos 
318*6881a400Schristos void _initialize_amd64nbsd_tdep ();
319*6881a400Schristos void
320*6881a400Schristos _initialize_amd64nbsd_tdep ()
321*6881a400Schristos {
322*6881a400Schristos   /* The NetBSD/amd64 native dependent code makes this assumption.  */
323*6881a400Schristos   gdb_assert (ARRAY_SIZE (amd64nbsd_r_reg_offset) == AMD64_NUM_GREGS);
324*6881a400Schristos 
325*6881a400Schristos   gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64,
326*6881a400Schristos 			  GDB_OSABI_NETBSD, amd64nbsd_init_abi);
327*6881a400Schristos }
328