1 /* Native-dependent code for GNU/Linux i386. 2 3 Copyright (C) 1999-2015 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 "inferior.h" 22 #include "gdbcore.h" 23 #include "regcache.h" 24 #include "elf/common.h" 25 #include <sys/ptrace.h> 26 #include <sys/uio.h> 27 #include "gregset.h" 28 #include "gdb_proc_service.h" 29 30 #include "i386-linux-nat.h" 31 #include "i387-tdep.h" 32 #include "i386-tdep.h" 33 #include "i386-linux-tdep.h" 34 #include "x86-xstate.h" 35 36 #include "x86-linux-nat.h" 37 38 /* The register sets used in GNU/Linux ELF core-dumps are identical to 39 the register sets in `struct user' that is used for a.out 40 core-dumps, and is also used by `ptrace'. The corresponding types 41 are `elf_gregset_t' for the general-purpose registers (with 42 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t' 43 for the floating-point registers. 44 45 Those types used to be available under the names `gregset_t' and 46 `fpregset_t' too, and this file used those names in the past. But 47 those names are now used for the register sets used in the 48 `mcontext_t' type, and have a different size and layout. */ 49 50 /* Which ptrace request retrieves which registers? 51 These apply to the corresponding SET requests as well. */ 52 53 #define GETREGS_SUPPLIES(regno) \ 54 ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM) 55 56 #define GETFPXREGS_SUPPLIES(regno) \ 57 (I386_ST0_REGNUM <= (regno) && (regno) < I386_SSE_NUM_REGS) 58 59 #define GETXSTATEREGS_SUPPLIES(regno) \ 60 (I386_ST0_REGNUM <= (regno) && (regno) < I386_AVX512_NUM_REGS) 61 62 /* Does the current host support the GETREGS request? */ 63 int have_ptrace_getregs = 64 #ifdef HAVE_PTRACE_GETREGS 65 1 66 #else 67 0 68 #endif 69 ; 70 71 /* Does the current host support the GETFPXREGS request? The header 72 file may or may not define it, and even if it is defined, the 73 kernel will return EIO if it's running on a pre-SSE processor. 74 75 My instinct is to attach this to some architecture- or 76 target-specific data structure, but really, a particular GDB 77 process can only run on top of one kernel at a time. So it's okay 78 for this to be a simple variable. */ 79 int have_ptrace_getfpxregs = 80 #ifdef HAVE_PTRACE_GETFPXREGS 81 -1 82 #else 83 0 84 #endif 85 ; 86 87 88 /* Accessing registers through the U area, one at a time. */ 89 90 /* Fetch one register. */ 91 92 static void 93 fetch_register (struct regcache *regcache, int regno) 94 { 95 int tid; 96 int val; 97 98 gdb_assert (!have_ptrace_getregs); 99 if (i386_linux_gregset_reg_offset[regno] == -1) 100 { 101 regcache_raw_supply (regcache, regno, NULL); 102 return; 103 } 104 105 /* GNU/Linux LWP ID's are process ID's. */ 106 tid = ptid_get_lwp (inferior_ptid); 107 if (tid == 0) 108 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */ 109 110 errno = 0; 111 val = ptrace (PTRACE_PEEKUSER, tid, 112 i386_linux_gregset_reg_offset[regno], 0); 113 if (errno != 0) 114 error (_("Couldn't read register %s (#%d): %s."), 115 gdbarch_register_name (get_regcache_arch (regcache), regno), 116 regno, safe_strerror (errno)); 117 118 regcache_raw_supply (regcache, regno, &val); 119 } 120 121 /* Store one register. */ 122 123 static void 124 store_register (const struct regcache *regcache, int regno) 125 { 126 int tid; 127 int val; 128 129 gdb_assert (!have_ptrace_getregs); 130 if (i386_linux_gregset_reg_offset[regno] == -1) 131 return; 132 133 /* GNU/Linux LWP ID's are process ID's. */ 134 tid = ptid_get_lwp (inferior_ptid); 135 if (tid == 0) 136 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */ 137 138 errno = 0; 139 regcache_raw_collect (regcache, regno, &val); 140 ptrace (PTRACE_POKEUSER, tid, 141 i386_linux_gregset_reg_offset[regno], val); 142 if (errno != 0) 143 error (_("Couldn't write register %s (#%d): %s."), 144 gdbarch_register_name (get_regcache_arch (regcache), regno), 145 regno, safe_strerror (errno)); 146 } 147 148 149 /* Transfering the general-purpose registers between GDB, inferiors 150 and core files. */ 151 152 /* Fill GDB's register array with the general-purpose register values 153 in *GREGSETP. */ 154 155 void 156 supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp) 157 { 158 const gdb_byte *regp = (const gdb_byte *) gregsetp; 159 int i; 160 161 for (i = 0; i < I386_NUM_GREGS; i++) 162 regcache_raw_supply (regcache, i, 163 regp + i386_linux_gregset_reg_offset[i]); 164 165 if (I386_LINUX_ORIG_EAX_REGNUM 166 < gdbarch_num_regs (get_regcache_arch (regcache))) 167 regcache_raw_supply (regcache, I386_LINUX_ORIG_EAX_REGNUM, regp 168 + i386_linux_gregset_reg_offset[I386_LINUX_ORIG_EAX_REGNUM]); 169 } 170 171 /* Fill register REGNO (if it is a general-purpose register) in 172 *GREGSETPS with the value in GDB's register array. If REGNO is -1, 173 do this for all registers. */ 174 175 void 176 fill_gregset (const struct regcache *regcache, 177 elf_gregset_t *gregsetp, int regno) 178 { 179 gdb_byte *regp = (gdb_byte *) gregsetp; 180 int i; 181 182 for (i = 0; i < I386_NUM_GREGS; i++) 183 if (regno == -1 || regno == i) 184 regcache_raw_collect (regcache, i, 185 regp + i386_linux_gregset_reg_offset[i]); 186 187 if ((regno == -1 || regno == I386_LINUX_ORIG_EAX_REGNUM) 188 && I386_LINUX_ORIG_EAX_REGNUM 189 < gdbarch_num_regs (get_regcache_arch (regcache))) 190 regcache_raw_collect (regcache, I386_LINUX_ORIG_EAX_REGNUM, regp 191 + i386_linux_gregset_reg_offset[I386_LINUX_ORIG_EAX_REGNUM]); 192 } 193 194 #ifdef HAVE_PTRACE_GETREGS 195 196 /* Fetch all general-purpose registers from process/thread TID and 197 store their values in GDB's register array. */ 198 199 static void 200 fetch_regs (struct regcache *regcache, int tid) 201 { 202 elf_gregset_t regs; 203 elf_gregset_t *regs_p = ®s; 204 205 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0) 206 { 207 if (errno == EIO) 208 { 209 /* The kernel we're running on doesn't support the GETREGS 210 request. Reset `have_ptrace_getregs'. */ 211 have_ptrace_getregs = 0; 212 return; 213 } 214 215 perror_with_name (_("Couldn't get registers")); 216 } 217 218 supply_gregset (regcache, (const elf_gregset_t *) regs_p); 219 } 220 221 /* Store all valid general-purpose registers in GDB's register array 222 into the process/thread specified by TID. */ 223 224 static void 225 store_regs (const struct regcache *regcache, int tid, int regno) 226 { 227 elf_gregset_t regs; 228 229 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0) 230 perror_with_name (_("Couldn't get registers")); 231 232 fill_gregset (regcache, ®s, regno); 233 234 if (ptrace (PTRACE_SETREGS, tid, 0, (int) ®s) < 0) 235 perror_with_name (_("Couldn't write registers")); 236 } 237 238 #else 239 240 static void fetch_regs (struct regcache *regcache, int tid) {} 241 static void store_regs (const struct regcache *regcache, int tid, int regno) {} 242 243 #endif 244 245 246 /* Transfering floating-point registers between GDB, inferiors and cores. */ 247 248 /* Fill GDB's register array with the floating-point register values in 249 *FPREGSETP. */ 250 251 void 252 supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp) 253 { 254 i387_supply_fsave (regcache, -1, fpregsetp); 255 } 256 257 /* Fill register REGNO (if it is a floating-point register) in 258 *FPREGSETP with the value in GDB's register array. If REGNO is -1, 259 do this for all registers. */ 260 261 void 262 fill_fpregset (const struct regcache *regcache, 263 elf_fpregset_t *fpregsetp, int regno) 264 { 265 i387_collect_fsave (regcache, regno, fpregsetp); 266 } 267 268 #ifdef HAVE_PTRACE_GETREGS 269 270 /* Fetch all floating-point registers from process/thread TID and store 271 thier values in GDB's register array. */ 272 273 static void 274 fetch_fpregs (struct regcache *regcache, int tid) 275 { 276 elf_fpregset_t fpregs; 277 278 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0) 279 perror_with_name (_("Couldn't get floating point status")); 280 281 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs); 282 } 283 284 /* Store all valid floating-point registers in GDB's register array 285 into the process/thread specified by TID. */ 286 287 static void 288 store_fpregs (const struct regcache *regcache, int tid, int regno) 289 { 290 elf_fpregset_t fpregs; 291 292 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0) 293 perror_with_name (_("Couldn't get floating point status")); 294 295 fill_fpregset (regcache, &fpregs, regno); 296 297 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0) 298 perror_with_name (_("Couldn't write floating point status")); 299 } 300 301 #else 302 303 static void 304 fetch_fpregs (struct regcache *regcache, int tid) 305 { 306 } 307 308 static void 309 store_fpregs (const struct regcache *regcache, int tid, int regno) 310 { 311 } 312 313 #endif 314 315 316 /* Transfering floating-point and SSE registers to and from GDB. */ 317 318 /* Fetch all registers covered by the PTRACE_GETREGSET request from 319 process/thread TID and store their values in GDB's register array. 320 Return non-zero if successful, zero otherwise. */ 321 322 static int 323 fetch_xstateregs (struct regcache *regcache, int tid) 324 { 325 char xstateregs[X86_XSTATE_MAX_SIZE]; 326 struct iovec iov; 327 328 if (!have_ptrace_getregset) 329 return 0; 330 331 iov.iov_base = xstateregs; 332 iov.iov_len = sizeof(xstateregs); 333 if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE, 334 &iov) < 0) 335 perror_with_name (_("Couldn't read extended state status")); 336 337 i387_supply_xsave (regcache, -1, xstateregs); 338 return 1; 339 } 340 341 /* Store all valid registers in GDB's register array covered by the 342 PTRACE_SETREGSET request into the process/thread specified by TID. 343 Return non-zero if successful, zero otherwise. */ 344 345 static int 346 store_xstateregs (const struct regcache *regcache, int tid, int regno) 347 { 348 char xstateregs[X86_XSTATE_MAX_SIZE]; 349 struct iovec iov; 350 351 if (!have_ptrace_getregset) 352 return 0; 353 354 iov.iov_base = xstateregs; 355 iov.iov_len = sizeof(xstateregs); 356 if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE, 357 &iov) < 0) 358 perror_with_name (_("Couldn't read extended state status")); 359 360 i387_collect_xsave (regcache, regno, xstateregs, 0); 361 362 if (ptrace (PTRACE_SETREGSET, tid, (unsigned int) NT_X86_XSTATE, 363 (int) &iov) < 0) 364 perror_with_name (_("Couldn't write extended state status")); 365 366 return 1; 367 } 368 369 #ifdef HAVE_PTRACE_GETFPXREGS 370 371 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from 372 process/thread TID and store their values in GDB's register array. 373 Return non-zero if successful, zero otherwise. */ 374 375 static int 376 fetch_fpxregs (struct regcache *regcache, int tid) 377 { 378 elf_fpxregset_t fpxregs; 379 380 if (! have_ptrace_getfpxregs) 381 return 0; 382 383 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0) 384 { 385 if (errno == EIO) 386 { 387 have_ptrace_getfpxregs = 0; 388 return 0; 389 } 390 391 perror_with_name (_("Couldn't read floating-point and SSE registers")); 392 } 393 394 i387_supply_fxsave (regcache, -1, (const elf_fpxregset_t *) &fpxregs); 395 return 1; 396 } 397 398 /* Store all valid registers in GDB's register array covered by the 399 PTRACE_SETFPXREGS request into the process/thread specified by TID. 400 Return non-zero if successful, zero otherwise. */ 401 402 static int 403 store_fpxregs (const struct regcache *regcache, int tid, int regno) 404 { 405 elf_fpxregset_t fpxregs; 406 407 if (! have_ptrace_getfpxregs) 408 return 0; 409 410 if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1) 411 { 412 if (errno == EIO) 413 { 414 have_ptrace_getfpxregs = 0; 415 return 0; 416 } 417 418 perror_with_name (_("Couldn't read floating-point and SSE registers")); 419 } 420 421 i387_collect_fxsave (regcache, regno, &fpxregs); 422 423 if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1) 424 perror_with_name (_("Couldn't write floating-point and SSE registers")); 425 426 return 1; 427 } 428 429 #else 430 431 static int 432 fetch_fpxregs (struct regcache *regcache, int tid) 433 { 434 return 0; 435 } 436 437 static int 438 store_fpxregs (const struct regcache *regcache, int tid, int regno) 439 { 440 return 0; 441 } 442 443 #endif /* HAVE_PTRACE_GETFPXREGS */ 444 445 446 /* Transferring arbitrary registers between GDB and inferior. */ 447 448 /* Fetch register REGNO from the child process. If REGNO is -1, do 449 this for all registers (including the floating point and SSE 450 registers). */ 451 452 static void 453 i386_linux_fetch_inferior_registers (struct target_ops *ops, 454 struct regcache *regcache, int regno) 455 { 456 int tid; 457 458 /* Use the old method of peeking around in `struct user' if the 459 GETREGS request isn't available. */ 460 if (!have_ptrace_getregs) 461 { 462 int i; 463 464 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++) 465 if (regno == -1 || regno == i) 466 fetch_register (regcache, i); 467 468 return; 469 } 470 471 /* GNU/Linux LWP ID's are process ID's. */ 472 tid = ptid_get_lwp (inferior_ptid); 473 if (tid == 0) 474 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */ 475 476 /* Use the PTRACE_GETFPXREGS request whenever possible, since it 477 transfers more registers in one system call, and we'll cache the 478 results. But remember that fetch_fpxregs can fail, and return 479 zero. */ 480 if (regno == -1) 481 { 482 fetch_regs (regcache, tid); 483 484 /* The call above might reset `have_ptrace_getregs'. */ 485 if (!have_ptrace_getregs) 486 { 487 i386_linux_fetch_inferior_registers (ops, regcache, regno); 488 return; 489 } 490 491 if (fetch_xstateregs (regcache, tid)) 492 return; 493 if (fetch_fpxregs (regcache, tid)) 494 return; 495 fetch_fpregs (regcache, tid); 496 return; 497 } 498 499 if (GETREGS_SUPPLIES (regno)) 500 { 501 fetch_regs (regcache, tid); 502 return; 503 } 504 505 if (GETXSTATEREGS_SUPPLIES (regno)) 506 { 507 if (fetch_xstateregs (regcache, tid)) 508 return; 509 } 510 511 if (GETFPXREGS_SUPPLIES (regno)) 512 { 513 if (fetch_fpxregs (regcache, tid)) 514 return; 515 516 /* Either our processor or our kernel doesn't support the SSE 517 registers, so read the FP registers in the traditional way, 518 and fill the SSE registers with dummy values. It would be 519 more graceful to handle differences in the register set using 520 gdbarch. Until then, this will at least make things work 521 plausibly. */ 522 fetch_fpregs (regcache, tid); 523 return; 524 } 525 526 internal_error (__FILE__, __LINE__, 527 _("Got request for bad register number %d."), regno); 528 } 529 530 /* Store register REGNO back into the child process. If REGNO is -1, 531 do this for all registers (including the floating point and SSE 532 registers). */ 533 static void 534 i386_linux_store_inferior_registers (struct target_ops *ops, 535 struct regcache *regcache, int regno) 536 { 537 int tid; 538 539 /* Use the old method of poking around in `struct user' if the 540 SETREGS request isn't available. */ 541 if (!have_ptrace_getregs) 542 { 543 int i; 544 545 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++) 546 if (regno == -1 || regno == i) 547 store_register (regcache, i); 548 549 return; 550 } 551 552 /* GNU/Linux LWP ID's are process ID's. */ 553 tid = ptid_get_lwp (inferior_ptid); 554 if (tid == 0) 555 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */ 556 557 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it 558 transfers more registers in one system call. But remember that 559 store_fpxregs can fail, and return zero. */ 560 if (regno == -1) 561 { 562 store_regs (regcache, tid, regno); 563 if (store_xstateregs (regcache, tid, regno)) 564 return; 565 if (store_fpxregs (regcache, tid, regno)) 566 return; 567 store_fpregs (regcache, tid, regno); 568 return; 569 } 570 571 if (GETREGS_SUPPLIES (regno)) 572 { 573 store_regs (regcache, tid, regno); 574 return; 575 } 576 577 if (GETXSTATEREGS_SUPPLIES (regno)) 578 { 579 if (store_xstateregs (regcache, tid, regno)) 580 return; 581 } 582 583 if (GETFPXREGS_SUPPLIES (regno)) 584 { 585 if (store_fpxregs (regcache, tid, regno)) 586 return; 587 588 /* Either our processor or our kernel doesn't support the SSE 589 registers, so just write the FP registers in the traditional 590 way. */ 591 store_fpregs (regcache, tid, regno); 592 return; 593 } 594 595 internal_error (__FILE__, __LINE__, 596 _("Got request to store bad register number %d."), regno); 597 } 598 599 600 /* Called by libthread_db. Returns a pointer to the thread local 601 storage (or its descriptor). */ 602 603 ps_err_e 604 ps_get_thread_area (const struct ps_prochandle *ph, 605 lwpid_t lwpid, int idx, void **base) 606 { 607 unsigned int base_addr; 608 ps_err_e result; 609 610 result = x86_linux_get_thread_area (lwpid, (void *) idx, &base_addr); 611 612 if (result == PS_OK) 613 *(int *) base = base_addr; 614 615 return result; 616 } 617 618 619 /* The instruction for a GNU/Linux system call is: 620 int $0x80 621 or 0xcd 0x80. */ 622 623 static const unsigned char linux_syscall[] = { 0xcd, 0x80 }; 624 625 #define LINUX_SYSCALL_LEN (sizeof linux_syscall) 626 627 /* The system call number is stored in the %eax register. */ 628 #define LINUX_SYSCALL_REGNUM I386_EAX_REGNUM 629 630 /* We are specifically interested in the sigreturn and rt_sigreturn 631 system calls. */ 632 633 #ifndef SYS_sigreturn 634 #define SYS_sigreturn 0x77 635 #endif 636 #ifndef SYS_rt_sigreturn 637 #define SYS_rt_sigreturn 0xad 638 #endif 639 640 /* Offset to saved processor flags, from <asm/sigcontext.h>. */ 641 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64) 642 643 /* Resume execution of the inferior process. 644 If STEP is nonzero, single-step it. 645 If SIGNAL is nonzero, give it that signal. */ 646 647 static void 648 i386_linux_resume (struct target_ops *ops, 649 ptid_t ptid, int step, enum gdb_signal signal) 650 { 651 int pid = ptid_get_pid (ptid); 652 653 int request; 654 655 if (catch_syscall_enabled () > 0) 656 request = PTRACE_SYSCALL; 657 else 658 request = PTRACE_CONT; 659 660 if (step) 661 { 662 struct regcache *regcache = get_thread_regcache (pid_to_ptid (pid)); 663 struct gdbarch *gdbarch = get_regcache_arch (regcache); 664 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); 665 ULONGEST pc; 666 gdb_byte buf[LINUX_SYSCALL_LEN]; 667 668 request = PTRACE_SINGLESTEP; 669 670 regcache_cooked_read_unsigned (regcache, 671 gdbarch_pc_regnum (gdbarch), &pc); 672 673 /* Returning from a signal trampoline is done by calling a 674 special system call (sigreturn or rt_sigreturn, see 675 i386-linux-tdep.c for more information). This system call 676 restores the registers that were saved when the signal was 677 raised, including %eflags. That means that single-stepping 678 won't work. Instead, we'll have to modify the signal context 679 that's about to be restored, and set the trace flag there. */ 680 681 /* First check if PC is at a system call. */ 682 if (target_read_memory (pc, buf, LINUX_SYSCALL_LEN) == 0 683 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0) 684 { 685 ULONGEST syscall; 686 regcache_cooked_read_unsigned (regcache, 687 LINUX_SYSCALL_REGNUM, &syscall); 688 689 /* Then check the system call number. */ 690 if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn) 691 { 692 ULONGEST sp, addr; 693 unsigned long int eflags; 694 695 regcache_cooked_read_unsigned (regcache, I386_ESP_REGNUM, &sp); 696 if (syscall == SYS_rt_sigreturn) 697 addr = read_memory_unsigned_integer (sp + 8, 4, byte_order) 698 + 20; 699 else 700 addr = sp; 701 702 /* Set the trace flag in the context that's about to be 703 restored. */ 704 addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET; 705 read_memory (addr, (gdb_byte *) &eflags, 4); 706 eflags |= 0x0100; 707 write_memory (addr, (gdb_byte *) &eflags, 4); 708 } 709 } 710 } 711 712 if (ptrace (request, pid, 0, gdb_signal_to_host (signal)) == -1) 713 perror_with_name (("ptrace")); 714 } 715 716 717 /* -Wmissing-prototypes */ 718 extern initialize_file_ftype _initialize_i386_linux_nat; 719 720 void 721 _initialize_i386_linux_nat (void) 722 { 723 /* Create a generic x86 GNU/Linux target. */ 724 struct target_ops *t = x86_linux_create_target (); 725 726 /* Override the default ptrace resume method. */ 727 t->to_resume = i386_linux_resume; 728 729 /* Add our register access methods. */ 730 t->to_fetch_registers = i386_linux_fetch_inferior_registers; 731 t->to_store_registers = i386_linux_store_inferior_registers; 732 733 /* Add the target. */ 734 x86_linux_add_target (t); 735 } 736