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