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