vmx_signal.c 3.9 KB

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