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