Include gdb_assert.h in common-defs.h
[platform/upstream/binutils.git] / gdb / amd64obsd-tdep.c
1 /* Target-dependent code for OpenBSD/amd64.
2
3    Copyright (C) 2003-2014 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 "frame.h"
22 #include "frame-unwind.h"
23 #include "gdbcore.h"
24 #include "symtab.h"
25 #include "objfiles.h"
26 #include "osabi.h"
27 #include "regcache.h"
28 #include "regset.h"
29 #include "target.h"
30 #include "trad-frame.h"
31
32 #include <string.h>
33
34 #include "obsd-tdep.h"
35 #include "amd64-tdep.h"
36 #include "i387-tdep.h"
37 #include "solib-svr4.h"
38 #include "bsd-uthread.h"
39
40 /* Support for core dumps.  */
41
42 static void
43 amd64obsd_supply_regset (const struct regset *regset,
44                          struct regcache *regcache, int regnum,
45                          const void *regs, size_t len)
46 {
47   struct gdbarch *gdbarch = get_regcache_arch (regcache);
48   const struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
49
50   gdb_assert (len >= tdep->sizeof_gregset + I387_SIZEOF_FXSAVE);
51
52   i386_supply_gregset (regset, regcache, regnum, regs, tdep->sizeof_gregset);
53   amd64_supply_fxsave (regcache, regnum,
54                        ((const gdb_byte *)regs) + tdep->sizeof_gregset);
55 }
56
57 static const struct regset amd64obsd_combined_regset =
58   {
59     NULL, amd64obsd_supply_regset, NULL
60   };
61
62 static const struct regset *
63 amd64obsd_regset_from_core_section (struct gdbarch *gdbarch,
64                                     const char *sect_name, size_t sect_size)
65 {
66   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
67
68   /* OpenBSD core dumps don't use seperate register sets for the
69      general-purpose and floating-point registers.  */
70
71   if (strcmp (sect_name, ".reg") == 0
72       && sect_size >= tdep->sizeof_gregset + I387_SIZEOF_FXSAVE)
73     return &amd64obsd_combined_regset;
74
75   return NULL;
76 }
77 \f
78
79 /* Support for signal handlers.  */
80
81 /* Default page size.  */
82 static const int amd64obsd_page_size = 4096;
83
84 /* Return whether THIS_FRAME corresponds to an OpenBSD sigtramp
85    routine.  */
86
87 static int
88 amd64obsd_sigtramp_p (struct frame_info *this_frame)
89 {
90   CORE_ADDR pc = get_frame_pc (this_frame);
91   CORE_ADDR start_pc = (pc & ~(amd64obsd_page_size - 1));
92   const gdb_byte osigreturn[] =
93   {
94     0x48, 0xc7, 0xc0,
95     0x67, 0x00, 0x00, 0x00,     /* movq $SYS_sigreturn, %rax */
96     0xcd, 0x80                  /* int $0x80 */
97   };
98   const gdb_byte sigreturn[] =
99   {
100     0x48, 0xc7, 0xc0,
101     0x67, 0x00, 0x00, 0x00,     /* movq $SYS_sigreturn, %rax */
102     0x0f, 0x05                  /* syscall */
103   };
104   size_t buflen = (sizeof sigreturn) + 1;
105   gdb_byte *buf;
106   const char *name;
107
108   /* If the function has a valid symbol name, it isn't a
109      trampoline.  */
110   find_pc_partial_function (pc, &name, NULL, NULL);
111   if (name != NULL)
112     return 0;
113
114   /* If the function lives in a valid section (even without a starting
115      point) it isn't a trampoline.  */
116   if (find_pc_section (pc) != NULL)
117     return 0;
118
119   /* If we can't read the instructions at START_PC, return zero.  */
120   buf = alloca ((sizeof sigreturn) + 1);
121   if (!safe_frame_unwind_memory (this_frame, start_pc + 6, buf, buflen))
122     return 0;
123
124   /* Check for sigreturn(2).  Depending on how the assembler encoded
125      the `movq %rsp, %rdi' instruction, the code starts at offset 6 or
126      7.  OpenBSD 5.0 and later use the `syscall' instruction.  Older
127      versions use `int $0x80'.  Check for both.  */
128   if (memcmp (buf, sigreturn, sizeof sigreturn)
129       && memcmp (buf + 1, sigreturn, sizeof sigreturn)
130       && memcmp (buf, osigreturn, sizeof osigreturn)
131       && memcmp (buf + 1, osigreturn, sizeof osigreturn))
132     return 0;
133
134   return 1;
135 }
136
137 /* Assuming THIS_FRAME is for a BSD sigtramp routine, return the
138    address of the associated sigcontext structure.  */
139
140 static CORE_ADDR
141 amd64obsd_sigcontext_addr (struct frame_info *this_frame)
142 {
143   CORE_ADDR pc = get_frame_pc (this_frame);
144   ULONGEST offset = (pc & (amd64obsd_page_size - 1));
145
146   /* The %rsp register points at `struct sigcontext' upon entry of a
147      signal trampoline.  The relevant part of the trampoline is
148
149         call    *%rax
150         movq    %rsp, %rdi
151         pushq   %rdi
152         movq    $SYS_sigreturn,%rax
153         int     $0x80
154
155      (see /usr/src/sys/arch/amd64/amd64/locore.S).  The `pushq'
156      instruction clobbers %rsp, but its value is saved in `%rdi'.  */
157
158   if (offset > 5)
159     return get_frame_register_unsigned (this_frame, AMD64_RDI_REGNUM);
160   else
161     return get_frame_register_unsigned (this_frame, AMD64_RSP_REGNUM);
162 }
163 \f
164 /* OpenBSD 3.5 or later.  */
165
166 /* Mapping between the general-purpose registers in `struct reg'
167    format and GDB's register cache layout.  */
168
169 /* From <machine/reg.h>.  */
170 int amd64obsd_r_reg_offset[] =
171 {
172   14 * 8,                       /* %rax */
173   13 * 8,                       /* %rbx */
174   3 * 8,                        /* %rcx */
175   2 * 8,                        /* %rdx */
176   1 * 8,                        /* %rsi */
177   0 * 8,                        /* %rdi */
178   12 * 8,                       /* %rbp */
179   15 * 8,                       /* %rsp */
180   4 * 8,                        /* %r8 ..  */
181   5 * 8,
182   6 * 8,
183   7 * 8,
184   8 * 8,
185   9 * 8,
186   10 * 8,
187   11 * 8,                       /* ... %r15 */
188   16 * 8,                       /* %rip */
189   17 * 8,                       /* %eflags */
190   18 * 8,                       /* %cs */
191   19 * 8,                       /* %ss */
192   20 * 8,                       /* %ds */
193   21 * 8,                       /* %es */
194   22 * 8,                       /* %fs */
195   23 * 8                        /* %gs */
196 };
197
198 /* From <machine/signal.h>.  */
199 static int amd64obsd_sc_reg_offset[] =
200 {
201   14 * 8,                       /* %rax */
202   13 * 8,                       /* %rbx */
203   3 * 8,                        /* %rcx */
204   2 * 8,                        /* %rdx */
205   1 * 8,                        /* %rsi */
206   0 * 8,                        /* %rdi */
207   12 * 8,                       /* %rbp */
208   24 * 8,                       /* %rsp */
209   4 * 8,                        /* %r8 ...  */
210   5 * 8,
211   6 * 8,
212   7 * 8,
213   8 * 8,
214   9 * 8,
215   10 * 8,
216   11 * 8,                       /* ... %r15 */
217   21 * 8,                       /* %rip */
218   23 * 8,                       /* %eflags */
219   22 * 8,                       /* %cs */
220   25 * 8,                       /* %ss */
221   18 * 8,                       /* %ds */
222   17 * 8,                       /* %es */
223   16 * 8,                       /* %fs */
224   15 * 8                        /* %gs */
225 };
226
227 /* From /usr/src/lib/libpthread/arch/amd64/uthread_machdep.c.  */
228 static int amd64obsd_uthread_reg_offset[] =
229 {
230   19 * 8,                       /* %rax */
231   16 * 8,                       /* %rbx */
232   18 * 8,                       /* %rcx */
233   17 * 8,                       /* %rdx */
234   14 * 8,                       /* %rsi */
235   13 * 8,                       /* %rdi */
236   15 * 8,                       /* %rbp */
237   -1,                           /* %rsp */
238   12 * 8,                       /* %r8 ...  */
239   11 * 8,
240   10 * 8,
241   9 * 8,
242   8 * 8,
243   7 * 8,
244   6 * 8,
245   5 * 8,                        /* ... %r15 */
246   20 * 8,                       /* %rip */
247   4 * 8,                        /* %eflags */
248   21 * 8,                       /* %cs */
249   -1,                           /* %ss */
250   3 * 8,                        /* %ds */
251   2 * 8,                        /* %es */
252   1 * 8,                        /* %fs */
253   0 * 8                         /* %gs */
254 };
255
256 /* Offset within the thread structure where we can find the saved
257    stack pointer (%esp).  */
258 #define AMD64OBSD_UTHREAD_RSP_OFFSET    400
259
260 static void
261 amd64obsd_supply_uthread (struct regcache *regcache,
262                           int regnum, CORE_ADDR addr)
263 {
264   struct gdbarch *gdbarch = get_regcache_arch (regcache);
265   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
266   CORE_ADDR sp_addr = addr + AMD64OBSD_UTHREAD_RSP_OFFSET;
267   CORE_ADDR sp = 0;
268   gdb_byte buf[8];
269   int i;
270
271   gdb_assert (regnum >= -1);
272
273   if (regnum == -1 || regnum == AMD64_RSP_REGNUM)
274     {
275       int offset;
276
277       /* Fetch stack pointer from thread structure.  */
278       sp = read_memory_unsigned_integer (sp_addr, 8, byte_order);
279
280       /* Adjust the stack pointer such that it looks as if we just
281          returned from _thread_machdep_switch.  */
282       offset = amd64obsd_uthread_reg_offset[AMD64_RIP_REGNUM] + 8;
283       store_unsigned_integer (buf, 8, byte_order, sp + offset);
284       regcache_raw_supply (regcache, AMD64_RSP_REGNUM, buf);
285     }
286
287   for (i = 0; i < ARRAY_SIZE (amd64obsd_uthread_reg_offset); i++)
288     {
289       if (amd64obsd_uthread_reg_offset[i] != -1
290           && (regnum == -1 || regnum == i))
291         {
292           /* Fetch stack pointer from thread structure (if we didn't
293              do so already).  */
294           if (sp == 0)
295             sp = read_memory_unsigned_integer (sp_addr, 8, byte_order);
296
297           /* Read the saved register from the stack frame.  */
298           read_memory (sp + amd64obsd_uthread_reg_offset[i], buf, 8);
299           regcache_raw_supply (regcache, i, buf);
300         }
301     }
302 }
303
304 static void
305 amd64obsd_collect_uthread (const struct regcache *regcache,
306                            int regnum, CORE_ADDR addr)
307 {
308   struct gdbarch *gdbarch = get_regcache_arch (regcache);
309   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
310   CORE_ADDR sp_addr = addr + AMD64OBSD_UTHREAD_RSP_OFFSET;
311   CORE_ADDR sp = 0;
312   gdb_byte buf[8];
313   int i;
314
315   gdb_assert (regnum >= -1);
316
317   if (regnum == -1 || regnum == AMD64_RSP_REGNUM)
318     {
319       int offset;
320
321       /* Calculate the stack pointer (frame pointer) that will be
322          stored into the thread structure.  */
323       offset = amd64obsd_uthread_reg_offset[AMD64_RIP_REGNUM] + 8;
324       regcache_raw_collect (regcache, AMD64_RSP_REGNUM, buf);
325       sp = extract_unsigned_integer (buf, 8, byte_order) - offset;
326
327       /* Store the stack pointer.  */
328       write_memory_unsigned_integer (sp_addr, 8, byte_order, sp);
329
330       /* The stack pointer was (potentially) modified.  Make sure we
331          build a proper stack frame.  */
332       regnum = -1;
333     }
334
335   for (i = 0; i < ARRAY_SIZE (amd64obsd_uthread_reg_offset); i++)
336     {
337       if (amd64obsd_uthread_reg_offset[i] != -1
338           && (regnum == -1 || regnum == i))
339         {
340           /* Fetch stack pointer from thread structure (if we didn't
341              calculate it already).  */
342           if (sp == 0)
343             sp = read_memory_unsigned_integer (sp_addr, 8, byte_order);
344
345           /* Write the register into the stack frame.  */
346           regcache_raw_collect (regcache, i, buf);
347           write_memory (sp + amd64obsd_uthread_reg_offset[i], buf, 8);
348         }
349     }
350 }
351 /* Kernel debugging support.  */
352
353 /* From <machine/frame.h>.  Easy since `struct trapframe' matches
354    `struct sigcontext'.  */
355 #define amd64obsd_tf_reg_offset amd64obsd_sc_reg_offset
356
357 static struct trad_frame_cache *
358 amd64obsd_trapframe_cache (struct frame_info *this_frame, void **this_cache)
359 {
360   struct gdbarch *gdbarch = get_frame_arch (this_frame);
361   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
362   struct trad_frame_cache *cache;
363   CORE_ADDR func, sp, addr;
364   ULONGEST cs;
365   const char *name;
366   int i;
367
368   if (*this_cache)
369     return *this_cache;
370
371   cache = trad_frame_cache_zalloc (this_frame);
372   *this_cache = cache;
373
374   func = get_frame_func (this_frame);
375   sp = get_frame_register_unsigned (this_frame, AMD64_RSP_REGNUM);
376
377   find_pc_partial_function (func, &name, NULL, NULL);
378   if (name && strncmp (name, "Xintr", 5) == 0)
379     addr = sp + 8;              /* It's an interrupt frame.  */
380   else
381     addr = sp;
382
383   for (i = 0; i < ARRAY_SIZE (amd64obsd_tf_reg_offset); i++)
384     if (amd64obsd_tf_reg_offset[i] != -1)
385       trad_frame_set_reg_addr (cache, i, addr + amd64obsd_tf_reg_offset[i]);
386
387   /* Read %cs from trap frame.  */
388   addr += amd64obsd_tf_reg_offset[AMD64_CS_REGNUM];
389   cs = read_memory_unsigned_integer (addr, 8, byte_order);
390   if ((cs & I386_SEL_RPL) == I386_SEL_UPL)
391     {
392       /* Trap from user space; terminate backtrace.  */
393       trad_frame_set_id (cache, outer_frame_id);
394     }
395   else
396     {
397       /* Construct the frame ID using the function start.  */
398       trad_frame_set_id (cache, frame_id_build (sp + 16, func));
399     }
400
401   return cache;
402 }
403
404 static void
405 amd64obsd_trapframe_this_id (struct frame_info *this_frame,
406                              void **this_cache, struct frame_id *this_id)
407 {
408   struct trad_frame_cache *cache =
409     amd64obsd_trapframe_cache (this_frame, this_cache);
410   
411   trad_frame_get_id (cache, this_id);
412 }
413
414 static struct value *
415 amd64obsd_trapframe_prev_register (struct frame_info *this_frame,
416                                    void **this_cache, int regnum)
417 {
418   struct trad_frame_cache *cache =
419     amd64obsd_trapframe_cache (this_frame, this_cache);
420
421   return trad_frame_get_register (cache, this_frame, regnum);
422 }
423
424 static int
425 amd64obsd_trapframe_sniffer (const struct frame_unwind *self,
426                              struct frame_info *this_frame,
427                              void **this_prologue_cache)
428 {
429   ULONGEST cs;
430   const char *name;
431
432   /* Check Current Privilege Level and bail out if we're not executing
433      in kernel space.  */
434   cs = get_frame_register_unsigned (this_frame, AMD64_CS_REGNUM);
435   if ((cs & I386_SEL_RPL) == I386_SEL_UPL)
436     return 0;
437
438   find_pc_partial_function (get_frame_pc (this_frame), &name, NULL, NULL);
439   return (name && ((strcmp (name, "calltrap") == 0)
440                    || (strcmp (name, "osyscall1") == 0)
441                    || (strcmp (name, "Xsyscall") == 0)
442                    || (strncmp (name, "Xintr", 5) == 0)));
443 }
444
445 static const struct frame_unwind amd64obsd_trapframe_unwind = {
446   /* FIXME: kettenis/20051219: This really is more like an interrupt
447      frame, but SIGTRAMP_FRAME would print <signal handler called>,
448      which really is not what we want here.  */
449   NORMAL_FRAME,
450   default_frame_unwind_stop_reason,
451   amd64obsd_trapframe_this_id,
452   amd64obsd_trapframe_prev_register,
453   NULL,
454   amd64obsd_trapframe_sniffer
455 };
456 \f
457
458 static void
459 amd64obsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
460 {
461   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
462
463   amd64_init_abi (info, gdbarch);
464   obsd_init_abi (info, gdbarch);
465
466   /* Initialize general-purpose register set details.  */
467   tdep->gregset_reg_offset = amd64obsd_r_reg_offset;
468   tdep->gregset_num_regs = ARRAY_SIZE (amd64obsd_r_reg_offset);
469   tdep->sizeof_gregset = 24 * 8;
470
471   tdep->jb_pc_offset = 7 * 8;
472
473   tdep->sigtramp_p = amd64obsd_sigtramp_p;
474   tdep->sigcontext_addr = amd64obsd_sigcontext_addr;
475   tdep->sc_reg_offset = amd64obsd_sc_reg_offset;
476   tdep->sc_num_regs = ARRAY_SIZE (amd64obsd_sc_reg_offset);
477
478   /* OpenBSD provides a user-level threads implementation.  */
479   bsd_uthread_set_supply_uthread (gdbarch, amd64obsd_supply_uthread);
480   bsd_uthread_set_collect_uthread (gdbarch, amd64obsd_collect_uthread);
481
482   /* OpenBSD uses SVR4-style shared libraries.  */
483   set_solib_svr4_fetch_link_map_offsets
484     (gdbarch, svr4_lp64_fetch_link_map_offsets);
485
486   /* Unwind kernel trap frames correctly.  */
487   frame_unwind_prepend_unwinder (gdbarch, &amd64obsd_trapframe_unwind);
488 }
489
490 /* Traditional (a.out) NetBSD-style core dumps.  */
491
492 static void
493 amd64obsd_core_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
494 {
495   amd64obsd_init_abi (info, gdbarch);
496
497   set_gdbarch_regset_from_core_section
498     (gdbarch, amd64obsd_regset_from_core_section);
499 }
500 \f
501
502 /* Provide a prototype to silence -Wmissing-prototypes.  */
503 void _initialize_amd64obsd_tdep (void);
504
505 void
506 _initialize_amd64obsd_tdep (void)
507 {
508   /* The OpenBSD/amd64 native dependent code makes this assumption.  */
509   gdb_assert (ARRAY_SIZE (amd64obsd_r_reg_offset) == AMD64_NUM_GREGS);
510
511   gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64,
512                           GDB_OSABI_OPENBSD_ELF, amd64obsd_init_abi);
513
514   /* OpenBSD uses traditional (a.out) NetBSD-style core dumps.  */
515   gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64,
516                           GDB_OSABI_NETBSD_AOUT, amd64obsd_core_init_abi);
517 }