vmx_signal.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2015, Cyril Bur, IBM Corp.
  4. *
  5. * This test attempts to see if the VMX registers are correctly reported in a
  6. * signal context. Each worker just spins checking its VMX registers, at some
  7. * point a signal will interrupt it and C code will check the signal context
  8. * ensuring it is also the same.
  9. */
  10. #include <stdio.h>
  11. #include <unistd.h>
  12. #include <sys/syscall.h>
  13. #include <sys/time.h>
  14. #include <sys/types.h>
  15. #include <sys/wait.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <pthread.h>
  19. #include <altivec.h>
  20. #include "utils.h"
  21. /* Number of times each thread should receive the signal */
  22. #define ITERATIONS 10
  23. /*
  24. * Factor by which to multiply number of online CPUs for total number of
  25. * worker threads
  26. */
  27. #define THREAD_FACTOR 8
  28. __thread vector int varray[] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10,11,12},
  29. {13,14,15,16},{17,18,19,20},{21,22,23,24},
  30. {25,26,27,28},{29,30,31,32},{33,34,35,36},
  31. {37,38,39,40},{41,42,43,44},{45,46,47,48}};
  32. bool bad_context;
  33. int running;
  34. int threads_starting;
  35. extern int preempt_vmx(vector int *varray, int *threads_starting, int *sentinal);
  36. void signal_vmx_sig(int sig, siginfo_t *info, void *context)
  37. {
  38. int i;
  39. ucontext_t *uc = context;
  40. mcontext_t *mc = &uc->uc_mcontext;
  41. /* Only the non volatiles were loaded up */
  42. for (i = 20; i < 32; i++) {
  43. if (memcmp(mc->v_regs->vrregs[i], &varray[i - 20], 16)) {
  44. int j;
  45. /*
  46. * Shouldn't printf() in a signal handler, however, this is a
  47. * test and we've detected failure. Understanding what failed
  48. * is paramount. All that happens after this is tests exit with
  49. * failure.
  50. */
  51. printf("VMX mismatch at reg %d!\n", i);
  52. printf("Reg | Actual | Expected\n");
  53. for (j = 20; j < 32; j++) {
  54. printf("%d | 0x%04x%04x%04x%04x | 0x%04x%04x%04x%04x\n", j, mc->v_regs->vrregs[j][0],
  55. mc->v_regs->vrregs[j][1], mc->v_regs->vrregs[j][2], mc->v_regs->vrregs[j][3],
  56. varray[j - 20][0], varray[j - 20][1], varray[j - 20][2], varray[j - 20][3]);
  57. }
  58. bad_context = true;
  59. break;
  60. }
  61. }
  62. }
  63. void *signal_vmx_c(void *p)
  64. {
  65. int i, j;
  66. long rc;
  67. struct sigaction act;
  68. act.sa_sigaction = signal_vmx_sig;
  69. act.sa_flags = SA_SIGINFO;
  70. rc = sigaction(SIGUSR1, &act, NULL);
  71. if (rc)
  72. return p;
  73. srand(pthread_self());
  74. for (i = 0; i < 12; i++)
  75. for (j = 0; j < 4; j++)
  76. varray[i][j] = rand();
  77. rc = preempt_vmx(varray, &threads_starting, &running);
  78. return (void *) rc;
  79. }
  80. int test_signal_vmx(void)
  81. {
  82. int i, j, rc, threads;
  83. void *rc_p;
  84. pthread_t *tids;
  85. // vcmpequd used in vmx_asm.S is v2.07
  86. SKIP_IF(!have_hwcap2(PPC_FEATURE2_ARCH_2_07));
  87. threads = sysconf(_SC_NPROCESSORS_ONLN) * THREAD_FACTOR;
  88. tids = malloc(threads * sizeof(pthread_t));
  89. FAIL_IF(!tids);
  90. running = true;
  91. threads_starting = threads;
  92. for (i = 0; i < threads; i++) {
  93. rc = pthread_create(&tids[i], NULL, signal_vmx_c, NULL);
  94. FAIL_IF(rc);
  95. }
  96. setbuf(stdout, NULL);
  97. printf("\tWaiting for %d workers to start... %d", threads, threads_starting);
  98. while (threads_starting) {
  99. asm volatile("": : :"memory");
  100. usleep(1000);
  101. printf(", %d", threads_starting);
  102. }
  103. printf(" ...done\n");
  104. printf("\tSending signals to all threads %d times...", ITERATIONS);
  105. for (i = 0; i < ITERATIONS; i++) {
  106. for (j = 0; j < threads; j++) {
  107. pthread_kill(tids[j], SIGUSR1);
  108. }
  109. sleep(1);
  110. }
  111. printf("done\n");
  112. printf("\tKilling workers...");
  113. running = 0;
  114. for (i = 0; i < threads; i++) {
  115. pthread_join(tids[i], &rc_p);
  116. /*
  117. * Harness will say the fail was here, look at why signal_vmx
  118. * returned
  119. */
  120. if ((long) rc_p || bad_context)
  121. printf("oops\n");
  122. if (bad_context)
  123. fprintf(stderr, "\t!! bad_context is true\n");
  124. FAIL_IF((long) rc_p || bad_context);
  125. }
  126. printf("done\n");
  127. free(tids);
  128. return 0;
  129. }
  130. int main(int argc, char *argv[])
  131. {
  132. test_harness_set_timeout(360);
  133. return test_harness(test_signal_vmx, "vmx_signal");
  134. }