| 1 | /* Target-dependent code for OpenBSD/sparc64. |
| 2 | |
| 3 | Copyright (C) 2004, 2005, 2006, 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 "osabi.h" |
| 27 | #include "regcache.h" |
| 28 | #include "regset.h" |
| 29 | #include "symtab.h" |
| 30 | #include "objfiles.h" |
| 31 | #include "trad-frame.h" |
| 32 | |
| 33 | #include "gdb_assert.h" |
| 34 | |
| 35 | #include "obsd-tdep.h" |
| 36 | #include "sparc64-tdep.h" |
| 37 | #include "solib-svr4.h" |
| 38 | #include "bsd-uthread.h" |
| 39 | |
| 40 | /* OpenBSD uses the traditional NetBSD core file format, even for |
| 41 | ports that use ELF. The core files don't use multiple register |
| 42 | sets. Instead, the general-purpose and floating-point registers |
| 43 | are lumped together in a single section. Unlike on NetBSD, OpenBSD |
| 44 | uses a different layout for its general-purpose registers than the |
| 45 | layout used for ptrace(2). */ |
| 46 | |
| 47 | /* From <machine/reg.h>. */ |
| 48 | const struct sparc_gregset sparc64obsd_core_gregset = |
| 49 | { |
| 50 | 0 * 8, /* "tstate" */ |
| 51 | 1 * 8, /* %pc */ |
| 52 | 2 * 8, /* %npc */ |
| 53 | 3 * 8, /* %y */ |
| 54 | -1, /* %fprs */ |
| 55 | -1, |
| 56 | 7 * 8, /* %g1 */ |
| 57 | 22 * 8, /* %l0 */ |
| 58 | 4 /* sizeof (%y) */ |
| 59 | }; |
| 60 | |
| 61 | static void |
| 62 | sparc64obsd_supply_gregset (const struct regset *regset, |
| 63 | struct regcache *regcache, |
| 64 | int regnum, const void *gregs, size_t len) |
| 65 | { |
| 66 | const char *regs = gregs; |
| 67 | |
| 68 | sparc64_supply_gregset (&sparc64obsd_core_gregset, regcache, regnum, regs); |
| 69 | sparc64_supply_fpregset (regcache, regnum, regs + 288); |
| 70 | } |
| 71 | \f |
| 72 | |
| 73 | /* Signal trampolines. */ |
| 74 | |
| 75 | /* Since OpenBSD 3.2, the sigtramp routine is mapped at a random page |
| 76 | in virtual memory. The randomness makes it somewhat tricky to |
| 77 | detect it, but fortunately we can rely on the fact that the start |
| 78 | of the sigtramp routine is page-aligned. We recognize the |
| 79 | trampoline by looking for the code that invokes the sigreturn |
| 80 | system call. The offset where we can find that code varies from |
| 81 | release to release. |
| 82 | |
| 83 | By the way, the mapping mentioned above is read-only, so you cannot |
| 84 | place a breakpoint in the signal trampoline. */ |
| 85 | |
| 86 | /* Default page size. */ |
| 87 | static const int sparc64obsd_page_size = 8192; |
| 88 | |
| 89 | /* Offset for sigreturn(2). */ |
| 90 | static const int sparc64obsd_sigreturn_offset[] = { |
| 91 | 0xf0, /* OpenBSD 3.8 */ |
| 92 | 0xec, /* OpenBSD 3.6 */ |
| 93 | 0xe8, /* OpenBSD 3.2 */ |
| 94 | -1 |
| 95 | }; |
| 96 | |
| 97 | static int |
| 98 | sparc64obsd_pc_in_sigtramp (CORE_ADDR pc, char *name) |
| 99 | { |
| 100 | CORE_ADDR start_pc = (pc & ~(sparc64obsd_page_size - 1)); |
| 101 | unsigned long insn; |
| 102 | const int *offset; |
| 103 | |
| 104 | if (name) |
| 105 | return 0; |
| 106 | |
| 107 | for (offset = sparc64obsd_sigreturn_offset; *offset != -1; offset++) |
| 108 | { |
| 109 | /* Check for "restore %g0, SYS_sigreturn, %g1". */ |
| 110 | insn = sparc_fetch_instruction (start_pc + *offset); |
| 111 | if (insn != 0x83e82067) |
| 112 | continue; |
| 113 | |
| 114 | /* Check for "t ST_SYSCALL". */ |
| 115 | insn = sparc_fetch_instruction (start_pc + *offset + 8); |
| 116 | if (insn != 0x91d02000) |
| 117 | continue; |
| 118 | |
| 119 | return 1; |
| 120 | } |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static struct sparc_frame_cache * |
| 126 | sparc64obsd_frame_cache (struct frame_info *next_frame, void **this_cache) |
| 127 | { |
| 128 | struct sparc_frame_cache *cache; |
| 129 | CORE_ADDR addr; |
| 130 | |
| 131 | if (*this_cache) |
| 132 | return *this_cache; |
| 133 | |
| 134 | cache = sparc_frame_cache (next_frame, this_cache); |
| 135 | gdb_assert (cache == *this_cache); |
| 136 | |
| 137 | /* If we couldn't find the frame's function, we're probably dealing |
| 138 | with an on-stack signal trampoline. */ |
| 139 | if (cache->pc == 0) |
| 140 | { |
| 141 | cache->pc = frame_pc_unwind (next_frame); |
| 142 | cache->pc &= ~(sparc64obsd_page_size - 1); |
| 143 | |
| 144 | /* Since we couldn't find the frame's function, the cache was |
| 145 | initialized under the assumption that we're frameless. */ |
| 146 | cache->frameless_p = 0; |
| 147 | addr = frame_unwind_register_unsigned (next_frame, SPARC_FP_REGNUM); |
| 148 | if (addr & 1) |
| 149 | addr += BIAS; |
| 150 | cache->base = addr; |
| 151 | } |
| 152 | |
| 153 | /* We find the appropriate instance of `struct sigcontext' at a |
| 154 | fixed offset in the signal frame. */ |
| 155 | addr = cache->base + 128 + 16; |
| 156 | cache->saved_regs = sparc64nbsd_sigcontext_saved_regs (addr, next_frame); |
| 157 | |
| 158 | return cache; |
| 159 | } |
| 160 | |
| 161 | static void |
| 162 | sparc64obsd_frame_this_id (struct frame_info *next_frame, void **this_cache, |
| 163 | struct frame_id *this_id) |
| 164 | { |
| 165 | struct sparc_frame_cache *cache = |
| 166 | sparc64obsd_frame_cache (next_frame, this_cache); |
| 167 | |
| 168 | (*this_id) = frame_id_build (cache->base, cache->pc); |
| 169 | } |
| 170 | |
| 171 | static void |
| 172 | sparc64obsd_frame_prev_register (struct frame_info *next_frame, |
| 173 | void **this_cache, |
| 174 | int regnum, int *optimizedp, |
| 175 | enum lval_type *lvalp, CORE_ADDR *addrp, |
| 176 | int *realnump, gdb_byte *valuep) |
| 177 | { |
| 178 | struct sparc_frame_cache *cache = |
| 179 | sparc64obsd_frame_cache (next_frame, this_cache); |
| 180 | |
| 181 | trad_frame_get_prev_register (next_frame, cache->saved_regs, regnum, |
| 182 | optimizedp, lvalp, addrp, realnump, valuep); |
| 183 | } |
| 184 | |
| 185 | static const struct frame_unwind sparc64obsd_frame_unwind = |
| 186 | { |
| 187 | SIGTRAMP_FRAME, |
| 188 | sparc64obsd_frame_this_id, |
| 189 | sparc64obsd_frame_prev_register |
| 190 | }; |
| 191 | |
| 192 | static const struct frame_unwind * |
| 193 | sparc64obsd_sigtramp_frame_sniffer (struct frame_info *next_frame) |
| 194 | { |
| 195 | CORE_ADDR pc = frame_pc_unwind (next_frame); |
| 196 | char *name; |
| 197 | |
| 198 | find_pc_partial_function (pc, &name, NULL, NULL); |
| 199 | if (sparc64obsd_pc_in_sigtramp (pc, name)) |
| 200 | return &sparc64obsd_frame_unwind; |
| 201 | |
| 202 | return NULL; |
| 203 | } |
| 204 | \f |
| 205 | /* Kernel debugging support. */ |
| 206 | |
| 207 | static struct sparc_frame_cache * |
| 208 | sparc64obsd_trapframe_cache (struct frame_info *next_frame, void **this_cache) |
| 209 | { |
| 210 | struct sparc_frame_cache *cache; |
| 211 | CORE_ADDR sp, trapframe_addr; |
| 212 | int regnum; |
| 213 | |
| 214 | if (*this_cache) |
| 215 | return *this_cache; |
| 216 | |
| 217 | cache = sparc_frame_cache (next_frame, this_cache); |
| 218 | gdb_assert (cache == *this_cache); |
| 219 | |
| 220 | sp = frame_unwind_register_unsigned (next_frame, SPARC_SP_REGNUM); |
| 221 | trapframe_addr = sp + BIAS + 176; |
| 222 | |
| 223 | cache->saved_regs = trad_frame_alloc_saved_regs (next_frame); |
| 224 | |
| 225 | cache->saved_regs[SPARC64_STATE_REGNUM].addr = trapframe_addr; |
| 226 | cache->saved_regs[SPARC64_PC_REGNUM].addr = trapframe_addr + 8; |
| 227 | cache->saved_regs[SPARC64_NPC_REGNUM].addr = trapframe_addr + 16; |
| 228 | |
| 229 | for (regnum = SPARC_G0_REGNUM; regnum <= SPARC_I7_REGNUM; regnum++) |
| 230 | cache->saved_regs[regnum].addr = |
| 231 | trapframe_addr + 48 + (regnum - SPARC_G0_REGNUM) * 8; |
| 232 | |
| 233 | return cache; |
| 234 | } |
| 235 | |
| 236 | static void |
| 237 | sparc64obsd_trapframe_this_id (struct frame_info *next_frame, |
| 238 | void **this_cache, struct frame_id *this_id) |
| 239 | { |
| 240 | struct sparc_frame_cache *cache = |
| 241 | sparc64obsd_trapframe_cache (next_frame, this_cache); |
| 242 | |
| 243 | (*this_id) = frame_id_build (cache->base, cache->pc); |
| 244 | } |
| 245 | |
| 246 | static void |
| 247 | sparc64obsd_trapframe_prev_register (struct frame_info *next_frame, |
| 248 | void **this_cache, |
| 249 | int regnum, int *optimizedp, |
| 250 | enum lval_type *lvalp, CORE_ADDR *addrp, |
| 251 | int *realnump, gdb_byte *valuep) |
| 252 | { |
| 253 | struct sparc_frame_cache *cache = |
| 254 | sparc64obsd_trapframe_cache (next_frame, this_cache); |
| 255 | |
| 256 | trad_frame_get_prev_register (next_frame, cache->saved_regs, regnum, |
| 257 | optimizedp, lvalp, addrp, realnump, valuep); |
| 258 | } |
| 259 | |
| 260 | static const struct frame_unwind sparc64obsd_trapframe_unwind = |
| 261 | { |
| 262 | NORMAL_FRAME, |
| 263 | sparc64obsd_trapframe_this_id, |
| 264 | sparc64obsd_trapframe_prev_register |
| 265 | }; |
| 266 | |
| 267 | static const struct frame_unwind * |
| 268 | sparc64obsd_trapframe_sniffer (struct frame_info *next_frame) |
| 269 | { |
| 270 | CORE_ADDR pc; |
| 271 | ULONGEST pstate; |
| 272 | char *name; |
| 273 | |
| 274 | /* Check whether we are in privileged mode, and bail out if we're not. */ |
| 275 | pstate = frame_unwind_register_unsigned (next_frame, SPARC64_PSTATE_REGNUM); |
| 276 | if ((pstate & SPARC64_PSTATE_PRIV) == 0) |
| 277 | return NULL; |
| 278 | |
| 279 | pc = frame_unwind_address_in_block (next_frame, NORMAL_FRAME); |
| 280 | find_pc_partial_function (pc, &name, NULL, NULL); |
| 281 | if (name && strcmp (name, "Lslowtrap_reenter") == 0) |
| 282 | return &sparc64obsd_trapframe_unwind; |
| 283 | |
| 284 | return NULL; |
| 285 | } |
| 286 | \f |
| 287 | |
| 288 | /* Threads support. */ |
| 289 | |
| 290 | /* Offset wthin the thread structure where we can find %fp and %i7. */ |
| 291 | #define SPARC64OBSD_UTHREAD_FP_OFFSET 232 |
| 292 | #define SPARC64OBSD_UTHREAD_PC_OFFSET 240 |
| 293 | |
| 294 | static void |
| 295 | sparc64obsd_supply_uthread (struct regcache *regcache, |
| 296 | int regnum, CORE_ADDR addr) |
| 297 | { |
| 298 | CORE_ADDR fp, fp_addr = addr + SPARC64OBSD_UTHREAD_FP_OFFSET; |
| 299 | gdb_byte buf[8]; |
| 300 | |
| 301 | gdb_assert (regnum >= -1); |
| 302 | |
| 303 | fp = read_memory_unsigned_integer (fp_addr, 8); |
| 304 | if (regnum == SPARC_SP_REGNUM || regnum == -1) |
| 305 | { |
| 306 | store_unsigned_integer (buf, 8, fp); |
| 307 | regcache_raw_supply (regcache, SPARC_SP_REGNUM, buf); |
| 308 | |
| 309 | if (regnum == SPARC_SP_REGNUM) |
| 310 | return; |
| 311 | } |
| 312 | |
| 313 | if (regnum == SPARC64_PC_REGNUM || regnum == SPARC64_NPC_REGNUM |
| 314 | || regnum == -1) |
| 315 | { |
| 316 | CORE_ADDR i7, i7_addr = addr + SPARC64OBSD_UTHREAD_PC_OFFSET; |
| 317 | |
| 318 | i7 = read_memory_unsigned_integer (i7_addr, 8); |
| 319 | if (regnum == SPARC64_PC_REGNUM || regnum == -1) |
| 320 | { |
| 321 | store_unsigned_integer (buf, 8, i7 + 8); |
| 322 | regcache_raw_supply (regcache, SPARC64_PC_REGNUM, buf); |
| 323 | } |
| 324 | if (regnum == SPARC64_NPC_REGNUM || regnum == -1) |
| 325 | { |
| 326 | store_unsigned_integer (buf, 8, i7 + 12); |
| 327 | regcache_raw_supply (regcache, SPARC64_NPC_REGNUM, buf); |
| 328 | } |
| 329 | |
| 330 | if (regnum == SPARC64_PC_REGNUM || regnum == SPARC64_NPC_REGNUM) |
| 331 | return; |
| 332 | } |
| 333 | |
| 334 | sparc_supply_rwindow (regcache, fp, regnum); |
| 335 | } |
| 336 | |
| 337 | static void |
| 338 | sparc64obsd_collect_uthread(const struct regcache *regcache, |
| 339 | int regnum, CORE_ADDR addr) |
| 340 | { |
| 341 | CORE_ADDR sp; |
| 342 | gdb_byte buf[8]; |
| 343 | |
| 344 | gdb_assert (regnum >= -1); |
| 345 | |
| 346 | if (regnum == SPARC_SP_REGNUM || regnum == -1) |
| 347 | { |
| 348 | CORE_ADDR fp_addr = addr + SPARC64OBSD_UTHREAD_FP_OFFSET; |
| 349 | |
| 350 | regcache_raw_collect (regcache, SPARC_SP_REGNUM, buf); |
| 351 | write_memory (fp_addr,buf, 8); |
| 352 | } |
| 353 | |
| 354 | if (regnum == SPARC64_PC_REGNUM || regnum == -1) |
| 355 | { |
| 356 | CORE_ADDR i7, i7_addr = addr + SPARC64OBSD_UTHREAD_PC_OFFSET; |
| 357 | |
| 358 | regcache_raw_collect (regcache, SPARC64_PC_REGNUM, buf); |
| 359 | i7 = extract_unsigned_integer (buf, 8) - 8; |
| 360 | write_memory_unsigned_integer (i7_addr, 8, i7); |
| 361 | |
| 362 | if (regnum == SPARC64_PC_REGNUM) |
| 363 | return; |
| 364 | } |
| 365 | |
| 366 | regcache_raw_collect (regcache, SPARC_SP_REGNUM, buf); |
| 367 | sp = extract_unsigned_integer (buf, 8); |
| 368 | sparc_collect_rwindow (regcache, sp, regnum); |
| 369 | } |
| 370 | \f |
| 371 | |
| 372 | static void |
| 373 | sparc64obsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch) |
| 374 | { |
| 375 | struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); |
| 376 | |
| 377 | tdep->gregset = regset_alloc (gdbarch, sparc64obsd_supply_gregset, NULL); |
| 378 | tdep->sizeof_gregset = 832; |
| 379 | |
| 380 | /* Make sure we can single-step "new" syscalls. */ |
| 381 | tdep->step_trap = sparcnbsd_step_trap; |
| 382 | |
| 383 | frame_unwind_append_sniffer (gdbarch, sparc64obsd_sigtramp_frame_sniffer); |
| 384 | frame_unwind_append_sniffer (gdbarch, sparc64obsd_trapframe_sniffer); |
| 385 | |
| 386 | sparc64_init_abi (info, gdbarch); |
| 387 | |
| 388 | /* OpenBSD/sparc64 has SVR4-style shared libraries. */ |
| 389 | set_solib_svr4_fetch_link_map_offsets |
| 390 | (gdbarch, svr4_lp64_fetch_link_map_offsets); |
| 391 | set_gdbarch_skip_solib_resolver (gdbarch, obsd_skip_solib_resolver); |
| 392 | |
| 393 | /* OpenBSD provides a user-level threads implementation. */ |
| 394 | bsd_uthread_set_supply_uthread (gdbarch, sparc64obsd_supply_uthread); |
| 395 | bsd_uthread_set_collect_uthread (gdbarch, sparc64obsd_collect_uthread); |
| 396 | } |
| 397 | \f |
| 398 | |
| 399 | /* Provide a prototype to silence -Wmissing-prototypes. */ |
| 400 | void _initialize_sparc64obsd_tdep (void); |
| 401 | |
| 402 | void |
| 403 | _initialize_sparc64obsd_tdep (void) |
| 404 | { |
| 405 | gdbarch_register_osabi (bfd_arch_sparc, bfd_mach_sparc_v9, |
| 406 | GDB_OSABI_OPENBSD_ELF, sparc64obsd_init_abi); |
| 407 | } |