Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 | /* SPDX-License-Identifier: GPL-2.0-or-later */ /* * Ptrace interface test helper functions * * Copyright (C) 2015 Anshuman Khandual, IBM Corporation. */ #define __SANE_USERSPACE_TYPES__ #include <inttypes.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #include <malloc.h> #include <errno.h> #include <time.h> #include <sys/ptrace.h> #include <sys/ioctl.h> #include <sys/uio.h> #include <sys/types.h> #include <sys/wait.h> #include <sys/signal.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/user.h> #include <sys/syscall.h> #include <linux/elf.h> #include <linux/types.h> #include <linux/auxvec.h> #include "reg.h" #include "utils.h" #define TEST_PASS 0 #define TEST_FAIL 1 struct fpr_regs { __u64 fpr[32]; __u64 fpscr; }; struct tm_spr_regs { unsigned long tm_tfhar; unsigned long tm_texasr; unsigned long tm_tfiar; }; #ifndef NT_PPC_TAR #define NT_PPC_TAR 0x103 #define NT_PPC_PPR 0x104 #define NT_PPC_DSCR 0x105 #define NT_PPC_EBB 0x106 #define NT_PPC_PMU 0x107 #define NT_PPC_TM_CGPR 0x108 #define NT_PPC_TM_CFPR 0x109 #define NT_PPC_TM_CVMX 0x10a #define NT_PPC_TM_CVSX 0x10b #define NT_PPC_TM_SPR 0x10c #define NT_PPC_TM_CTAR 0x10d #define NT_PPC_TM_CPPR 0x10e #define NT_PPC_TM_CDSCR 0x10f #endif /* Basic ptrace operations */ int start_trace(pid_t child) { int ret; ret = ptrace(PTRACE_ATTACH, child, NULL, NULL); if (ret) { perror("ptrace(PTRACE_ATTACH) failed"); return TEST_FAIL; } ret = waitpid(child, NULL, 0); if (ret != child) { perror("waitpid() failed"); return TEST_FAIL; } return TEST_PASS; } int stop_trace(pid_t child) { int ret; ret = ptrace(PTRACE_DETACH, child, NULL, NULL); if (ret) { perror("ptrace(PTRACE_DETACH) failed"); return TEST_FAIL; } return TEST_PASS; } int cont_trace(pid_t child) { int ret; ret = ptrace(PTRACE_CONT, child, NULL, NULL); if (ret) { perror("ptrace(PTRACE_CONT) failed"); return TEST_FAIL; } return TEST_PASS; } int ptrace_read_regs(pid_t child, unsigned long type, unsigned long regs[], int n) { struct iovec iov; long ret; FAIL_IF(start_trace(child)); iov.iov_base = regs; iov.iov_len = n * sizeof(unsigned long); ret = ptrace(PTRACE_GETREGSET, child, type, &iov); if (ret) return ret; FAIL_IF(stop_trace(child)); return TEST_PASS; } long ptrace_write_regs(pid_t child, unsigned long type, unsigned long regs[], int n) { struct iovec iov; long ret; FAIL_IF(start_trace(child)); iov.iov_base = regs; iov.iov_len = n * sizeof(unsigned long); ret = ptrace(PTRACE_SETREGSET, child, type, &iov); FAIL_IF(stop_trace(child)); return ret; } /* TAR, PPR, DSCR */ int show_tar_registers(pid_t child, unsigned long *out) { struct iovec iov; unsigned long *reg; int ret; reg = malloc(sizeof(unsigned long)); if (!reg) { perror("malloc() failed"); return TEST_FAIL; } iov.iov_base = (u64 *) reg; iov.iov_len = sizeof(unsigned long); ret = ptrace(PTRACE_GETREGSET, child, NT_PPC_TAR, &iov); if (ret) { perror("ptrace(PTRACE_GETREGSET) failed"); goto fail; } if (out) out[0] = *reg; ret = ptrace(PTRACE_GETREGSET, child, NT_PPC_PPR, &iov); if (ret) { perror("ptrace(PTRACE_GETREGSET) failed"); goto fail; } if (out) out[1] = *reg; ret = ptrace(PTRACE_GETREGSET, child, NT_PPC_DSCR, &iov); if (ret) { perror("ptrace(PTRACE_GETREGSET) failed"); goto fail; } if (out) out[2] = *reg; free(reg); return TEST_PASS; fail: free(reg); return TEST_FAIL; } int write_tar_registers(pid_t child, unsigned long tar, unsigned long ppr, unsigned long dscr) { struct iovec iov; unsigned long *reg; int ret; reg = malloc(sizeof(unsigned long)); if (!reg) { perror("malloc() failed"); return TEST_FAIL; } iov.iov_base = (u64 *) reg; iov.iov_len = sizeof(unsigned long); *reg = tar; ret = ptrace(PTRACE_SETREGSET, child, NT_PPC_TAR, &iov); if (ret) { perror("ptrace(PTRACE_SETREGSET) failed"); goto fail; } *reg = ppr; ret = ptrace(PTRACE_SETREGSET, child, NT_PPC_PPR, &iov); if (ret) { perror("ptrace(PTRACE_SETREGSET) failed"); goto fail; } *reg = dscr; ret = ptrace(PTRACE_SETREGSET, child, NT_PPC_DSCR, &iov); if (ret) { perror("ptrace(PTRACE_SETREGSET) failed"); goto fail; } free(reg); return TEST_PASS; fail: free(reg); return TEST_FAIL; } int show_tm_checkpointed_state(pid_t child, unsigned long *out) { struct iovec iov; unsigned long *reg; int ret; reg = malloc(sizeof(unsigned long)); if (!reg) { perror("malloc() failed"); return TEST_FAIL; } iov.iov_base = (u64 *) reg; iov.iov_len = sizeof(unsigned long); ret = ptrace(PTRACE_GETREGSET, child, NT_PPC_TM_CTAR, &iov); if (ret) { perror("ptrace(PTRACE_GETREGSET) failed"); goto fail; } if (out) out[0] = *reg; ret = ptrace(PTRACE_GETREGSET, child, NT_PPC_TM_CPPR, &iov); if (ret) { perror("ptrace(PTRACE_GETREGSET) failed"); goto fail; } if (out) out[1] = *reg; ret = ptrace(PTRACE_GETREGSET, child, NT_PPC_TM_CDSCR, &iov); if (ret) { perror("ptrace(PTRACE_GETREGSET) failed"); goto fail; } if (out) out[2] = *reg; free(reg); return TEST_PASS; fail: free(reg); return TEST_FAIL; } int write_ckpt_tar_registers(pid_t child, unsigned long tar, unsigned long ppr, unsigned long dscr) { struct iovec iov; unsigned long *reg; int ret; reg = malloc(sizeof(unsigned long)); if (!reg) { perror("malloc() failed"); return TEST_FAIL; } iov.iov_base = (u64 *) reg; iov.iov_len = sizeof(unsigned long); *reg = tar; ret = ptrace(PTRACE_SETREGSET, child, NT_PPC_TM_CTAR, &iov); if (ret) { perror("ptrace(PTRACE_GETREGSET) failed"); goto fail; } *reg = ppr; ret = ptrace(PTRACE_SETREGSET, child, NT_PPC_TM_CPPR, &iov); if (ret) { perror("ptrace(PTRACE_GETREGSET) failed"); goto fail; } *reg = dscr; ret = ptrace(PTRACE_SETREGSET, child, NT_PPC_TM_CDSCR, &iov); if (ret) { perror("ptrace(PTRACE_GETREGSET) failed"); goto fail; } free(reg); return TEST_PASS; fail: free(reg); return TEST_FAIL; } /* FPR */ int show_fpr(pid_t child, __u64 *fpr) { struct fpr_regs *regs; int ret, i; regs = (struct fpr_regs *) malloc(sizeof(struct fpr_regs)); ret = ptrace(PTRACE_GETFPREGS, child, NULL, regs); if (ret) { perror("ptrace(PTRACE_GETREGSET) failed"); return TEST_FAIL; } if (fpr) { for (i = 0; i < 32; i++) fpr[i] = regs->fpr[i]; } return TEST_PASS; } int write_fpr(pid_t child, __u64 val) { struct fpr_regs *regs; int ret, i; regs = (struct fpr_regs *) malloc(sizeof(struct fpr_regs)); ret = ptrace(PTRACE_GETFPREGS, child, NULL, regs); if (ret) { perror("ptrace(PTRACE_GETREGSET) failed"); return TEST_FAIL; } for (i = 0; i < 32; i++) regs->fpr[i] = val; ret = ptrace(PTRACE_SETFPREGS, child, NULL, regs); if (ret) { perror("ptrace(PTRACE_GETREGSET) failed"); return TEST_FAIL; } return TEST_PASS; } int show_ckpt_fpr(pid_t child, __u64 *fpr) { struct fpr_regs *regs; struct iovec iov; int ret, i; regs = (struct fpr_regs *) malloc(sizeof(struct fpr_regs)); iov.iov_base = regs; iov.iov_len = sizeof(struct fpr_regs); ret = ptrace(PTRACE_GETREGSET, child, NT_PPC_TM_CFPR, &iov); if (ret) { perror("ptrace(PTRACE_GETREGSET) failed"); return TEST_FAIL; } if (fpr) { for (i = 0; i < 32; i++) fpr[i] = regs->fpr[i]; } return TEST_PASS; } int write_ckpt_fpr(pid_t child, unsigned long val) { struct fpr_regs *regs; struct iovec iov; int ret, i; regs = (struct fpr_regs *) malloc(sizeof(struct fpr_regs)); iov.iov_base = regs; iov.iov_len = sizeof(struct fpr_regs); ret = ptrace(PTRACE_GETREGSET, child, NT_PPC_TM_CFPR, &iov); if (ret) { perror("ptrace(PTRACE_GETREGSET) failed"); return TEST_FAIL; } for (i = 0; i < 32; i++) regs->fpr[i] = val; ret = ptrace(PTRACE_SETREGSET, child, NT_PPC_TM_CFPR, &iov); if (ret) { perror("ptrace(PTRACE_GETREGSET) failed"); return TEST_FAIL; } return TEST_PASS; } /* GPR */ int show_gpr(pid_t child, unsigned long *gpr) { struct pt_regs *regs; int ret, i; regs = (struct pt_regs *) malloc(sizeof(struct pt_regs)); if (!regs) { perror("malloc() failed"); return TEST_FAIL; } ret = ptrace(PTRACE_GETREGS, child, NULL, regs); if (ret) { perror("ptrace(PTRACE_GETREGSET) failed"); return TEST_FAIL; } if (gpr) { for (i = 14; i < 32; i++) gpr[i-14] = regs->gpr[i]; } return TEST_PASS; } long sys_ptrace(enum __ptrace_request request, pid_t pid, unsigned long addr, unsigned long data) { return syscall(__NR_ptrace, request, pid, (void *)addr, data); } // 33 because of FPSCR #define PT_NUM_FPRS (33 * (sizeof(__u64) / sizeof(unsigned long))) __u64 *peek_fprs(pid_t child) { unsigned long *fprs, *p, addr; long ret; int i; fprs = malloc(sizeof(unsigned long) * PT_NUM_FPRS); if (!fprs) { perror("malloc() failed"); return NULL; } for (i = 0, p = fprs; i < PT_NUM_FPRS; i++, p++) { addr = sizeof(unsigned long) * (PT_FPR0 + i); ret = sys_ptrace(PTRACE_PEEKUSER, child, addr, (unsigned long)p); if (ret) { perror("ptrace(PTRACE_PEEKUSR) failed"); return NULL; } } addr = sizeof(unsigned long) * (PT_FPR0 + i); ret = sys_ptrace(PTRACE_PEEKUSER, child, addr, (unsigned long)&addr); if (!ret) { printf("ptrace(PTRACE_PEEKUSR) succeeded unexpectedly!\n"); return NULL; } return (__u64 *)fprs; } int poke_fprs(pid_t child, unsigned long *fprs) { unsigned long *p, addr; long ret; int i; for (i = 0, p = fprs; i < PT_NUM_FPRS; i++, p++) { addr = sizeof(unsigned long) * (PT_FPR0 + i); ret = sys_ptrace(PTRACE_POKEUSER, child, addr, *p); if (ret) { perror("ptrace(PTRACE_POKEUSR) failed"); return -1; } } addr = sizeof(unsigned long) * (PT_FPR0 + i); ret = sys_ptrace(PTRACE_POKEUSER, child, addr, addr); if (!ret) { printf("ptrace(PTRACE_POKEUSR) succeeded unexpectedly!\n"); return -1; } return 0; } int write_gpr(pid_t child, unsigned long val) { struct pt_regs *regs; int i, ret; regs = (struct pt_regs *) malloc(sizeof(struct pt_regs)); if (!regs) { perror("malloc() failed"); return TEST_FAIL; } ret = ptrace(PTRACE_GETREGS, child, NULL, regs); if (ret) { perror("ptrace(PTRACE_GETREGSET) failed"); return TEST_FAIL; } for (i = 14; i < 32; i++) regs->gpr[i] = val; ret = ptrace(PTRACE_SETREGS, child, NULL, regs); if (ret) { perror("ptrace(PTRACE_GETREGSET) failed"); return TEST_FAIL; } return TEST_PASS; } int show_ckpt_gpr(pid_t child, unsigned long *gpr) { struct pt_regs *regs; struct iovec iov; int ret, i; regs = (struct pt_regs *) malloc(sizeof(struct pt_regs)); if (!regs) { perror("malloc() failed"); return TEST_FAIL; } iov.iov_base = (u64 *) regs; iov.iov_len = sizeof(struct pt_regs); ret = ptrace(PTRACE_GETREGSET, child, NT_PPC_TM_CGPR, &iov); if (ret) { perror("ptrace(PTRACE_GETREGSET) failed"); return TEST_FAIL; } if (gpr) { for (i = 14; i < 32; i++) gpr[i-14] = regs->gpr[i]; } return TEST_PASS; } int write_ckpt_gpr(pid_t child, unsigned long val) { struct pt_regs *regs; struct iovec iov; int ret, i; regs = (struct pt_regs *) malloc(sizeof(struct pt_regs)); if (!regs) { perror("malloc() failed\n"); return TEST_FAIL; } iov.iov_base = (u64 *) regs; iov.iov_len = sizeof(struct pt_regs); ret = ptrace(PTRACE_GETREGSET, child, NT_PPC_TM_CGPR, &iov); if (ret) { perror("ptrace(PTRACE_GETREGSET) failed"); return TEST_FAIL; } for (i = 14; i < 32; i++) regs->gpr[i] = val; ret = ptrace(PTRACE_SETREGSET, child, NT_PPC_TM_CGPR, &iov); if (ret) { perror("ptrace(PTRACE_GETREGSET) failed"); return TEST_FAIL; } return TEST_PASS; } /* VMX */ int show_vmx(pid_t child, unsigned long vmx[][2]) { int ret; ret = ptrace(PTRACE_GETVRREGS, child, 0, vmx); if (ret) { perror("ptrace(PTRACE_GETVRREGS) failed"); return TEST_FAIL; } return TEST_PASS; } int show_vmx_ckpt(pid_t child, unsigned long vmx[][2]) { unsigned long regs[34][2]; struct iovec iov; int ret; iov.iov_base = (u64 *) regs; iov.iov_len = sizeof(regs); ret = ptrace(PTRACE_GETREGSET, child, NT_PPC_TM_CVMX, &iov); if (ret) { perror("ptrace(PTRACE_GETREGSET, NT_PPC_TM_CVMX) failed"); return TEST_FAIL; } memcpy(vmx, regs, sizeof(regs)); return TEST_PASS; } int write_vmx(pid_t child, unsigned long vmx[][2]) { int ret; ret = ptrace(PTRACE_SETVRREGS, child, 0, vmx); if (ret) { perror("ptrace(PTRACE_SETVRREGS) failed"); return TEST_FAIL; } return TEST_PASS; } int write_vmx_ckpt(pid_t child, unsigned long vmx[][2]) { unsigned long regs[34][2]; struct iovec iov; int ret; memcpy(regs, vmx, sizeof(regs)); iov.iov_base = (u64 *) regs; iov.iov_len = sizeof(regs); ret = ptrace(PTRACE_SETREGSET, child, NT_PPC_TM_CVMX, &iov); if (ret) { perror("ptrace(PTRACE_SETREGSET, NT_PPC_TM_CVMX) failed"); return TEST_FAIL; } return TEST_PASS; } /* VSX */ int show_vsx(pid_t child, unsigned long *vsx) { int ret; ret = ptrace(PTRACE_GETVSRREGS, child, 0, vsx); if (ret) { perror("ptrace(PTRACE_GETVSRREGS) failed"); return TEST_FAIL; } return TEST_PASS; } int show_vsx_ckpt(pid_t child, unsigned long *vsx) { unsigned long regs[32]; struct iovec iov; int ret; iov.iov_base = (u64 *) regs; iov.iov_len = sizeof(regs); ret = ptrace(PTRACE_GETREGSET, child, NT_PPC_TM_CVSX, &iov); if (ret) { perror("ptrace(PTRACE_GETREGSET, NT_PPC_TM_CVSX) failed"); return TEST_FAIL; } memcpy(vsx, regs, sizeof(regs)); return TEST_PASS; } int write_vsx(pid_t child, unsigned long *vsx) { int ret; ret = ptrace(PTRACE_SETVSRREGS, child, 0, vsx); if (ret) { perror("ptrace(PTRACE_SETVSRREGS) failed"); return TEST_FAIL; } return TEST_PASS; } int write_vsx_ckpt(pid_t child, unsigned long *vsx) { unsigned long regs[32]; struct iovec iov; int ret; memcpy(regs, vsx, sizeof(regs)); iov.iov_base = (u64 *) regs; iov.iov_len = sizeof(regs); ret = ptrace(PTRACE_SETREGSET, child, NT_PPC_TM_CVSX, &iov); if (ret) { perror("ptrace(PTRACE_SETREGSET, NT_PPC_TM_CVSX) failed"); return TEST_FAIL; } return TEST_PASS; } /* TM SPR */ int show_tm_spr(pid_t child, struct tm_spr_regs *out) { struct tm_spr_regs *regs; struct iovec iov; int ret; regs = (struct tm_spr_regs *) malloc(sizeof(struct tm_spr_regs)); if (!regs) { perror("malloc() failed"); return TEST_FAIL; } iov.iov_base = (u64 *) regs; iov.iov_len = sizeof(struct tm_spr_regs); ret = ptrace(PTRACE_GETREGSET, child, NT_PPC_TM_SPR, &iov); if (ret) { perror("ptrace(PTRACE_GETREGSET) failed"); return TEST_FAIL; } if (out) memcpy(out, regs, sizeof(struct tm_spr_regs)); return TEST_PASS; } /* Analyse TEXASR after TM failure */ inline unsigned long get_tfiar(void) { return mfspr(SPRN_TFIAR); } void analyse_texasr(unsigned long texasr) { printf("TEXASR: %16lx\t", texasr); if (texasr & TEXASR_FP) printf("TEXASR_FP "); if (texasr & TEXASR_DA) printf("TEXASR_DA "); if (texasr & TEXASR_NO) printf("TEXASR_NO "); if (texasr & TEXASR_FO) printf("TEXASR_FO "); if (texasr & TEXASR_SIC) printf("TEXASR_SIC "); if (texasr & TEXASR_NTC) printf("TEXASR_NTC "); if (texasr & TEXASR_TC) printf("TEXASR_TC "); if (texasr & TEXASR_TIC) printf("TEXASR_TIC "); if (texasr & TEXASR_IC) printf("TEXASR_IC "); if (texasr & TEXASR_IFC) printf("TEXASR_IFC "); if (texasr & TEXASR_ABT) printf("TEXASR_ABT "); if (texasr & TEXASR_SPD) printf("TEXASR_SPD "); if (texasr & TEXASR_HV) printf("TEXASR_HV "); if (texasr & TEXASR_PR) printf("TEXASR_PR "); if (texasr & TEXASR_FS) printf("TEXASR_FS "); if (texasr & TEXASR_TE) printf("TEXASR_TE "); if (texasr & TEXASR_ROT) printf("TEXASR_ROT "); printf("TFIAR :%lx\n", get_tfiar()); } void store_gpr(unsigned long *addr); |