ptrace-tm-vsx.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Ptrace test for VMX/VSX registers in the TM context
  3. *
  4. * Copyright (C) 2015 Anshuman Khandual, IBM Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include "ptrace.h"
  12. #include "tm.h"
  13. #include "ptrace-vsx.h"
  14. int shm_id;
  15. unsigned long *cptr, *pptr;
  16. unsigned long fp_load[VEC_MAX];
  17. unsigned long fp_store[VEC_MAX];
  18. unsigned long fp_load_ckpt[VEC_MAX];
  19. unsigned long fp_load_ckpt_new[VEC_MAX];
  20. __attribute__((used)) void load_vsx(void)
  21. {
  22. loadvsx(fp_load, 0);
  23. }
  24. __attribute__((used)) void load_vsx_ckpt(void)
  25. {
  26. loadvsx(fp_load_ckpt, 0);
  27. }
  28. void tm_vsx(void)
  29. {
  30. unsigned long result, texasr;
  31. int ret;
  32. cptr = (unsigned long *)shmat(shm_id, NULL, 0);
  33. trans:
  34. cptr[1] = 0;
  35. asm __volatile__(
  36. "bl load_vsx_ckpt;"
  37. "1: ;"
  38. "tbegin.;"
  39. "beq 2f;"
  40. "bl load_vsx;"
  41. "tsuspend.;"
  42. "li 7, 1;"
  43. "stw 7, 0(%[cptr1]);"
  44. "tresume.;"
  45. "b .;"
  46. "tend.;"
  47. "li 0, 0;"
  48. "ori %[res], 0, 0;"
  49. "b 3f;"
  50. "2: ;"
  51. "li 0, 1;"
  52. "ori %[res], 0, 0;"
  53. "mfspr %[texasr], %[sprn_texasr];"
  54. "3: ;"
  55. : [res] "=r" (result), [texasr] "=r" (texasr)
  56. : [sprn_texasr] "i" (SPRN_TEXASR), [cptr1] "b" (&cptr[1])
  57. : "memory", "r0", "r1", "r3", "r4",
  58. "r7", "r8", "r9", "r10", "r11"
  59. );
  60. if (result) {
  61. if (!cptr[0])
  62. goto trans;
  63. shmdt((void *)cptr);
  64. storevsx(fp_store, 0);
  65. ret = compare_vsx_vmx(fp_store, fp_load_ckpt_new);
  66. if (ret)
  67. exit(1);
  68. exit(0);
  69. }
  70. shmdt((void *)cptr);
  71. exit(1);
  72. }
  73. int trace_tm_vsx(pid_t child)
  74. {
  75. unsigned long vsx[VSX_MAX];
  76. unsigned long vmx[VMX_MAX + 2][2];
  77. FAIL_IF(start_trace(child));
  78. FAIL_IF(show_vsx(child, vsx));
  79. FAIL_IF(validate_vsx(vsx, fp_load));
  80. FAIL_IF(show_vmx(child, vmx));
  81. FAIL_IF(validate_vmx(vmx, fp_load));
  82. FAIL_IF(show_vsx_ckpt(child, vsx));
  83. FAIL_IF(validate_vsx(vsx, fp_load_ckpt));
  84. FAIL_IF(show_vmx_ckpt(child, vmx));
  85. FAIL_IF(validate_vmx(vmx, fp_load_ckpt));
  86. memset(vsx, 0, sizeof(vsx));
  87. memset(vmx, 0, sizeof(vmx));
  88. load_vsx_vmx(fp_load_ckpt_new, vsx, vmx);
  89. FAIL_IF(write_vsx_ckpt(child, vsx));
  90. FAIL_IF(write_vmx_ckpt(child, vmx));
  91. pptr[0] = 1;
  92. FAIL_IF(stop_trace(child));
  93. return TEST_PASS;
  94. }
  95. int ptrace_tm_vsx(void)
  96. {
  97. pid_t pid;
  98. int ret, status, i;
  99. SKIP_IF(!have_htm());
  100. shm_id = shmget(IPC_PRIVATE, sizeof(int) * 2, 0777|IPC_CREAT);
  101. for (i = 0; i < 128; i++) {
  102. fp_load[i] = 1 + rand();
  103. fp_load_ckpt[i] = 1 + 2 * rand();
  104. fp_load_ckpt_new[i] = 1 + 3 * rand();
  105. }
  106. pid = fork();
  107. if (pid < 0) {
  108. perror("fork() failed");
  109. return TEST_FAIL;
  110. }
  111. if (pid == 0)
  112. tm_vsx();
  113. if (pid) {
  114. pptr = (unsigned long *)shmat(shm_id, NULL, 0);
  115. while (!pptr[1])
  116. asm volatile("" : : : "memory");
  117. ret = trace_tm_vsx(pid);
  118. if (ret) {
  119. kill(pid, SIGKILL);
  120. shmdt((void *)pptr);
  121. shmctl(shm_id, IPC_RMID, NULL);
  122. return TEST_FAIL;
  123. }
  124. shmdt((void *)pptr);
  125. ret = wait(&status);
  126. shmctl(shm_id, IPC_RMID, NULL);
  127. if (ret != pid) {
  128. printf("Child's exit status not captured\n");
  129. return TEST_FAIL;
  130. }
  131. return (WIFEXITED(status) && WEXITSTATUS(status)) ? TEST_FAIL :
  132. TEST_PASS;
  133. }
  134. return TEST_PASS;
  135. }
  136. int main(int argc, char *argv[])
  137. {
  138. return test_harness(ptrace_tm_vsx, "ptrace_tm_vsx");
  139. }