1/* Motorola m68k native support for GNU/Linux. 2 3 Copyright (C) 1996, 1998, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 4 Free Software Foundation, Inc. 5 6 This file is part of GDB. 7 8 This program is free software; you can redistribute it and/or modify 9 it under the terms of the GNU General Public License as published by 10 the Free Software Foundation; either version 3 of the License, or 11 (at your option) any later version. 12 13 This program is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 20 21#include "defs.h" 22#include "frame.h" 23#include "inferior.h" 24#include "language.h" 25#include "gdbcore.h" 26#include "gdb_string.h" 27#include "regcache.h" 28#include "target.h" 29#include "linux-nat.h" 30 31#include "m68k-tdep.h" 32 33#include <sys/param.h> 34#include <sys/dir.h> 35#include <signal.h> 36#include <sys/ptrace.h> 37#include <sys/user.h> 38#include <sys/ioctl.h> 39#include <fcntl.h> 40#include <sys/procfs.h> 41 42#ifdef HAVE_SYS_REG_H 43#include <sys/reg.h> 44#endif 45 46#include <sys/file.h> 47#include "gdb_stat.h" 48 49#include "floatformat.h" 50 51#include "target.h" 52 53/* Prototypes for supply_gregset etc. */ 54#include "gregset.h" 55 56/* This table must line up with gdbarch_register_name in "m68k-tdep.c". */ 57static const int regmap[] = 58{ 59 PT_D0, PT_D1, PT_D2, PT_D3, PT_D4, PT_D5, PT_D6, PT_D7, 60 PT_A0, PT_A1, PT_A2, PT_A3, PT_A4, PT_A5, PT_A6, PT_USP, 61 PT_SR, PT_PC, 62 /* PT_FP0, ..., PT_FP7 */ 63 21, 24, 27, 30, 33, 36, 39, 42, 64 /* PT_FPCR, PT_FPSR, PT_FPIAR */ 65 45, 46, 47 66}; 67 68/* Which ptrace request retrieves which registers? 69 These apply to the corresponding SET requests as well. */ 70#define NUM_GREGS (18) 71#define MAX_NUM_REGS (NUM_GREGS + 11) 72 73int 74getregs_supplies (int regno) 75{ 76 return 0 <= regno && regno < NUM_GREGS; 77} 78 79int 80getfpregs_supplies (int regno) 81{ 82 return gdbarch_fp0_regnum (current_gdbarch) <= regno 83 && regno <= M68K_FPI_REGNUM; 84} 85 86/* Does the current host support the GETREGS request? */ 87int have_ptrace_getregs = 88#ifdef HAVE_PTRACE_GETREGS 89 1 90#else 91 0 92#endif 93; 94 95 96 97/* Fetching registers directly from the U area, one at a time. */ 98 99/* FIXME: This duplicates code from `inptrace.c'. The problem is that we 100 define FETCH_INFERIOR_REGISTERS since we want to use our own versions 101 of {fetch,store}_inferior_registers that use the GETREGS request. This 102 means that the code in `infptrace.c' is #ifdef'd out. But we need to 103 fall back on that code when GDB is running on top of a kernel that 104 doesn't support the GETREGS request. */ 105 106#ifndef PT_READ_U 107#define PT_READ_U PTRACE_PEEKUSR 108#endif 109#ifndef PT_WRITE_U 110#define PT_WRITE_U PTRACE_POKEUSR 111#endif 112 113/* Fetch one register. */ 114 115static void 116fetch_register (struct regcache *regcache, int regno) 117{ 118 /* This isn't really an address. But ptrace thinks of it as one. */ 119 CORE_ADDR regaddr; 120 char mess[128]; /* For messages */ 121 int i; 122 char buf[MAX_REGISTER_SIZE]; 123 int tid; 124 125 if (gdbarch_cannot_fetch_register (current_gdbarch, regno)) 126 { 127 memset (buf, '\0', register_size (current_gdbarch, regno)); /* Supply zeroes */ 128 regcache_raw_supply (regcache, regno, buf); 129 return; 130 } 131 132 /* Overload thread id onto process id */ 133 tid = TIDGET (inferior_ptid); 134 if (tid == 0) 135 tid = PIDGET (inferior_ptid); /* no thread id, just use process id */ 136 137 regaddr = 4 * regmap[regno]; 138 for (i = 0; i < register_size (current_gdbarch, regno); 139 i += sizeof (PTRACE_TYPE_RET)) 140 { 141 errno = 0; 142 *(PTRACE_TYPE_RET *) &buf[i] = ptrace (PT_READ_U, tid, 143 (PTRACE_TYPE_ARG3) regaddr, 0); 144 regaddr += sizeof (PTRACE_TYPE_RET); 145 if (errno != 0) 146 { 147 sprintf (mess, "reading register %s (#%d)", 148 gdbarch_register_name (current_gdbarch, regno), regno); 149 perror_with_name (mess); 150 } 151 } 152 regcache_raw_supply (regcache, regno, buf); 153} 154 155/* Fetch register values from the inferior. 156 If REGNO is negative, do this for all registers. 157 Otherwise, REGNO specifies which register (so we can save time). */ 158 159static void 160old_fetch_inferior_registers (struct regcache *regcache, int regno) 161{ 162 if (regno >= 0) 163 { 164 fetch_register (regcache, regno); 165 } 166 else 167 { 168 for (regno = 0; regno < gdbarch_num_regs (current_gdbarch); regno++) 169 { 170 fetch_register (regcache, regno); 171 } 172 } 173} 174 175/* Store one register. */ 176 177static void 178store_register (const struct regcache *regcache, int regno) 179{ 180 /* This isn't really an address. But ptrace thinks of it as one. */ 181 CORE_ADDR regaddr; 182 char mess[128]; /* For messages */ 183 int i; 184 int tid; 185 char buf[MAX_REGISTER_SIZE]; 186 187 if (gdbarch_cannot_store_register (current_gdbarch, regno)) 188 return; 189 190 /* Overload thread id onto process id */ 191 tid = TIDGET (inferior_ptid); 192 if (tid == 0) 193 tid = PIDGET (inferior_ptid); /* no thread id, just use process id */ 194 195 regaddr = 4 * regmap[regno]; 196 197 /* Put the contents of regno into a local buffer */ 198 regcache_raw_collect (regcache, regno, buf); 199 200 /* Store the local buffer into the inferior a chunk at the time. */ 201 for (i = 0; i < register_size (current_gdbarch, regno); 202 i += sizeof (PTRACE_TYPE_RET)) 203 { 204 errno = 0; 205 ptrace (PT_WRITE_U, tid, (PTRACE_TYPE_ARG3) regaddr, 206 *(PTRACE_TYPE_RET *) (buf + i)); 207 regaddr += sizeof (PTRACE_TYPE_RET); 208 if (errno != 0) 209 { 210 sprintf (mess, "writing register %s (#%d)", 211 gdbarch_register_name (current_gdbarch, regno), regno); 212 perror_with_name (mess); 213 } 214 } 215} 216 217/* Store our register values back into the inferior. 218 If REGNO is negative, do this for all registers. 219 Otherwise, REGNO specifies which register (so we can save time). */ 220 221static void 222old_store_inferior_registers (const struct regcache *regcache, int regno) 223{ 224 if (regno >= 0) 225 { 226 store_register (regcache, regno); 227 } 228 else 229 { 230 for (regno = 0; regno < gdbarch_num_regs (current_gdbarch); regno++) 231 { 232 store_register (regcache, regno); 233 } 234 } 235} 236 237/* Given a pointer to a general register set in /proc format 238 (elf_gregset_t *), unpack the register contents and supply 239 them as gdb's idea of the current register values. */ 240 241void 242supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp) 243{ 244 const elf_greg_t *regp = (const elf_greg_t *) gregsetp; 245 int regi; 246 247 for (regi = M68K_D0_REGNUM; 248 regi <= gdbarch_sp_regnum (current_gdbarch); 249 regi++) 250 regcache_raw_supply (regcache, regi, ®p[regmap[regi]]); 251 regcache_raw_supply (regcache, gdbarch_ps_regnum (current_gdbarch), 252 ®p[PT_SR]); 253 regcache_raw_supply (regcache, 254 gdbarch_pc_regnum (current_gdbarch), ®p[PT_PC]); 255} 256 257/* Fill register REGNO (if it is a general-purpose register) in 258 *GREGSETPS with the value in GDB's register array. If REGNO is -1, 259 do this for all registers. */ 260void 261fill_gregset (const struct regcache *regcache, 262 elf_gregset_t *gregsetp, int regno) 263{ 264 elf_greg_t *regp = (elf_greg_t *) gregsetp; 265 int i; 266 267 for (i = 0; i < NUM_GREGS; i++) 268 if (regno == -1 || regno == i) 269 regcache_raw_collect (regcache, i, regp + regmap[i]); 270} 271 272#ifdef HAVE_PTRACE_GETREGS 273 274/* Fetch all general-purpose registers from process/thread TID and 275 store their values in GDB's register array. */ 276 277static void 278fetch_regs (struct regcache *regcache, int tid) 279{ 280 elf_gregset_t regs; 281 282 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0) 283 { 284 if (errno == EIO) 285 { 286 /* The kernel we're running on doesn't support the GETREGS 287 request. Reset `have_ptrace_getregs'. */ 288 have_ptrace_getregs = 0; 289 return; 290 } 291 292 perror_with_name (_("Couldn't get registers")); 293 } 294 295 supply_gregset (regcache, (const elf_gregset_t *) ®s); 296} 297 298/* Store all valid general-purpose registers in GDB's register array 299 into the process/thread specified by TID. */ 300 301static void 302store_regs (const struct regcache *regcache, int tid, int regno) 303{ 304 elf_gregset_t regs; 305 306 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0) 307 perror_with_name (_("Couldn't get registers")); 308 309 fill_gregset (regcache, ®s, regno); 310 311 if (ptrace (PTRACE_SETREGS, tid, 0, (int) ®s) < 0) 312 perror_with_name (_("Couldn't write registers")); 313} 314 315#else 316 317static void fetch_regs (struct regcache *regcache, int tid) {} 318static void store_regs (const struct regcache *regcache, int tid, int regno) {} 319 320#endif 321 322 323/* Transfering floating-point registers between GDB, inferiors and cores. */ 324 325/* What is the address of fpN within the floating-point register set F? */ 326#define FPREG_ADDR(f, n) (&(f)->fpregs[(n) * 3]) 327 328/* Fill GDB's register array with the floating-point register values in 329 *FPREGSETP. */ 330 331void 332supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp) 333{ 334 int regi; 335 336 for (regi = gdbarch_fp0_regnum (current_gdbarch); 337 regi < gdbarch_fp0_regnum (current_gdbarch) + 8; regi++) 338 regcache_raw_supply (regcache, regi, 339 FPREG_ADDR (fpregsetp, 340 regi - gdbarch_fp0_regnum 341 (current_gdbarch))); 342 regcache_raw_supply (regcache, M68K_FPC_REGNUM, &fpregsetp->fpcntl[0]); 343 regcache_raw_supply (regcache, M68K_FPS_REGNUM, &fpregsetp->fpcntl[1]); 344 regcache_raw_supply (regcache, M68K_FPI_REGNUM, &fpregsetp->fpcntl[2]); 345} 346 347/* Fill register REGNO (if it is a floating-point register) in 348 *FPREGSETP with the value in GDB's register array. If REGNO is -1, 349 do this for all registers. */ 350 351void 352fill_fpregset (const struct regcache *regcache, 353 elf_fpregset_t *fpregsetp, int regno) 354{ 355 int i; 356 357 /* Fill in the floating-point registers. */ 358 for (i = gdbarch_fp0_regnum (current_gdbarch); 359 i < gdbarch_fp0_regnum (current_gdbarch) + 8; i++) 360 if (regno == -1 || regno == i) 361 regcache_raw_collect (regcache, i, 362 FPREG_ADDR (fpregsetp, 363 i - gdbarch_fp0_regnum 364 (current_gdbarch))); 365 366 /* Fill in the floating-point control registers. */ 367 for (i = M68K_FPC_REGNUM; i <= M68K_FPI_REGNUM; i++) 368 if (regno == -1 || regno == i) 369 regcache_raw_collect (regcache, i, 370 &fpregsetp->fpcntl[i - M68K_FPC_REGNUM]); 371} 372 373#ifdef HAVE_PTRACE_GETREGS 374 375/* Fetch all floating-point registers from process/thread TID and store 376 thier values in GDB's register array. */ 377 378static void 379fetch_fpregs (struct regcache *regcache, int tid) 380{ 381 elf_fpregset_t fpregs; 382 383 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0) 384 perror_with_name (_("Couldn't get floating point status")); 385 386 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs); 387} 388 389/* Store all valid floating-point registers in GDB's register array 390 into the process/thread specified by TID. */ 391 392static void 393store_fpregs (const struct regcache *regcache, int tid, int regno) 394{ 395 elf_fpregset_t fpregs; 396 397 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0) 398 perror_with_name (_("Couldn't get floating point status")); 399 400 fill_fpregset (regcache, &fpregs, regno); 401 402 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0) 403 perror_with_name (_("Couldn't write floating point status")); 404} 405 406#else 407 408static void fetch_fpregs (struct regcache *regcache, int tid) {} 409static void store_fpregs (const struct regcache *regcache, int tid, int regno) {} 410 411#endif 412 413/* Transferring arbitrary registers between GDB and inferior. */ 414 415/* Fetch register REGNO from the child process. If REGNO is -1, do 416 this for all registers (including the floating point and SSE 417 registers). */ 418 419static void 420m68k_linux_fetch_inferior_registers (struct regcache *regcache, int regno) 421{ 422 int tid; 423 424 /* Use the old method of peeking around in `struct user' if the 425 GETREGS request isn't available. */ 426 if (! have_ptrace_getregs) 427 { 428 old_fetch_inferior_registers (regcache, regno); 429 return; 430 } 431 432 /* GNU/Linux LWP ID's are process ID's. */ 433 tid = TIDGET (inferior_ptid); 434 if (tid == 0) 435 tid = PIDGET (inferior_ptid); /* Not a threaded program. */ 436 437 /* Use the PTRACE_GETFPXREGS request whenever possible, since it 438 transfers more registers in one system call, and we'll cache the 439 results. But remember that fetch_fpxregs can fail, and return 440 zero. */ 441 if (regno == -1) 442 { 443 fetch_regs (regcache, tid); 444 445 /* The call above might reset `have_ptrace_getregs'. */ 446 if (! have_ptrace_getregs) 447 { 448 old_fetch_inferior_registers (regcache, -1); 449 return; 450 } 451 452 fetch_fpregs (regcache, tid); 453 return; 454 } 455 456 if (getregs_supplies (regno)) 457 { 458 fetch_regs (regcache, tid); 459 return; 460 } 461 462 if (getfpregs_supplies (regno)) 463 { 464 fetch_fpregs (regcache, tid); 465 return; 466 } 467 468 internal_error (__FILE__, __LINE__, 469 _("Got request for bad register number %d."), regno); 470} 471 472/* Store register REGNO back into the child process. If REGNO is -1, 473 do this for all registers (including the floating point and SSE 474 registers). */ 475static void 476m68k_linux_store_inferior_registers (struct regcache *regcache, int regno) 477{ 478 int tid; 479 480 /* Use the old method of poking around in `struct user' if the 481 SETREGS request isn't available. */ 482 if (! have_ptrace_getregs) 483 { 484 old_store_inferior_registers (regcache, regno); 485 return; 486 } 487 488 /* GNU/Linux LWP ID's are process ID's. */ 489 tid = TIDGET (inferior_ptid); 490 if (tid == 0) 491 tid = PIDGET (inferior_ptid); /* Not a threaded program. */ 492 493 /* Use the PTRACE_SETFPREGS requests whenever possible, since it 494 transfers more registers in one system call. But remember that 495 store_fpregs can fail, and return zero. */ 496 if (regno == -1) 497 { 498 store_regs (regcache, tid, regno); 499 store_fpregs (regcache, tid, regno); 500 return; 501 } 502 503 if (getregs_supplies (regno)) 504 { 505 store_regs (regcache, tid, regno); 506 return; 507 } 508 509 if (getfpregs_supplies (regno)) 510 { 511 store_fpregs (regcache, tid, regno); 512 return; 513 } 514 515 internal_error (__FILE__, __LINE__, 516 _("Got request to store bad register number %d."), regno); 517} 518 519/* Interpreting register set info found in core files. */ 520 521/* Provide registers to GDB from a core file. 522 523 (We can't use the generic version of this function in 524 core-regset.c, because we need to use elf_gregset_t instead of 525 gregset_t.) 526 527 CORE_REG_SECT points to an array of bytes, which are the contents 528 of a `note' from a core file which BFD thinks might contain 529 register contents. CORE_REG_SIZE is its size. 530 531 WHICH says which register set corelow suspects this is: 532 0 --- the general-purpose register set, in elf_gregset_t format 533 2 --- the floating-point register set, in elf_fpregset_t format 534 535 REG_ADDR isn't used on GNU/Linux. */ 536 537static void 538fetch_core_registers (struct regcache *regcache, 539 char *core_reg_sect, unsigned core_reg_size, 540 int which, CORE_ADDR reg_addr) 541{ 542 elf_gregset_t gregset; 543 elf_fpregset_t fpregset; 544 545 switch (which) 546 { 547 case 0: 548 if (core_reg_size != sizeof (gregset)) 549 warning (_("Wrong size gregset in core file.")); 550 else 551 { 552 memcpy (&gregset, core_reg_sect, sizeof (gregset)); 553 supply_gregset (regcache, (const elf_gregset_t *) &gregset); 554 } 555 break; 556 557 case 2: 558 if (core_reg_size != sizeof (fpregset)) 559 warning (_("Wrong size fpregset in core file.")); 560 else 561 { 562 memcpy (&fpregset, core_reg_sect, sizeof (fpregset)); 563 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregset); 564 } 565 break; 566 567 default: 568 /* We've covered all the kinds of registers we know about here, 569 so this must be something we wouldn't know what to do with 570 anyway. Just ignore it. */ 571 break; 572 } 573} 574 575 576/* Register that we are able to handle GNU/Linux ELF core file 577 formats. */ 578 579static struct core_fns linux_elf_core_fns = 580{ 581 bfd_target_elf_flavour, /* core_flavour */ 582 default_check_format, /* check_format */ 583 default_core_sniffer, /* core_sniffer */ 584 fetch_core_registers, /* core_read_registers */ 585 NULL /* next */ 586}; 587 588void _initialize_m68k_linux_nat (void); 589 590void 591_initialize_m68k_linux_nat (void) 592{ 593 struct target_ops *t; 594 595 /* Fill in the generic GNU/Linux methods. */ 596 t = linux_target (); 597 598 /* Add our register access methods. */ 599 t->to_fetch_registers = m68k_linux_fetch_inferior_registers; 600 t->to_store_registers = m68k_linux_store_inferior_registers; 601 602 /* Register the target. */ 603 linux_nat_add_target (t); 604 605 deprecated_add_core_fns (&linux_elf_core_fns); 606} 607