fpu_signal.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 FPU registers are correctly reported in a
  6. * signal context. Each worker just spins checking its FPU 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 <pthread.h>
  18. #include "utils.h"
  19. #include "fpu.h"
  20. /* Number of times each thread should receive the signal */
  21. #define ITERATIONS 10
  22. /*
  23. * Factor by which to multiply number of online CPUs for total number of
  24. * worker threads
  25. */
  26. #define THREAD_FACTOR 8
  27. __thread double darray[32];
  28. bool bad_context;
  29. int threads_starting;
  30. int running;
  31. extern long preempt_fpu(double *darray, int *threads_starting, int *running);
  32. void signal_fpu_sig(int sig, siginfo_t *info, void *context)
  33. {
  34. int i;
  35. ucontext_t *uc = context;
  36. mcontext_t *mc = &uc->uc_mcontext;
  37. // Don't check f30/f31, they're used as scratches in check_all_fprs()
  38. for (i = 0; i < 30; i++) {
  39. if (mc->fp_regs[i] != darray[i]) {
  40. bad_context = true;
  41. break;
  42. }
  43. }
  44. }
  45. void *signal_fpu_c(void *p)
  46. {
  47. long rc;
  48. struct sigaction act;
  49. act.sa_sigaction = signal_fpu_sig;
  50. act.sa_flags = SA_SIGINFO;
  51. rc = sigaction(SIGUSR1, &act, NULL);
  52. if (rc)
  53. return p;
  54. srand(pthread_self());
  55. randomise_darray(darray, ARRAY_SIZE(darray));
  56. rc = preempt_fpu(darray, &threads_starting, &running);
  57. return (void *) rc;
  58. }
  59. int test_signal_fpu(void)
  60. {
  61. int i, j, rc, threads;
  62. void *rc_p;
  63. pthread_t *tids;
  64. threads = sysconf(_SC_NPROCESSORS_ONLN) * THREAD_FACTOR;
  65. tids = malloc(threads * sizeof(pthread_t));
  66. FAIL_IF(!tids);
  67. running = true;
  68. threads_starting = threads;
  69. for (i = 0; i < threads; i++) {
  70. rc = pthread_create(&tids[i], NULL, signal_fpu_c, NULL);
  71. FAIL_IF(rc);
  72. }
  73. setbuf(stdout, NULL);
  74. printf("\tWaiting for all workers to start...");
  75. while (threads_starting)
  76. asm volatile("": : :"memory");
  77. printf("done\n");
  78. printf("\tSending signals to all threads %d times...", ITERATIONS);
  79. for (i = 0; i < ITERATIONS; i++) {
  80. for (j = 0; j < threads; j++) {
  81. pthread_kill(tids[j], SIGUSR1);
  82. }
  83. sleep(1);
  84. }
  85. printf("done\n");
  86. printf("\tStopping workers...");
  87. running = 0;
  88. for (i = 0; i < threads; i++) {
  89. pthread_join(tids[i], &rc_p);
  90. /*
  91. * Harness will say the fail was here, look at why signal_fpu
  92. * returned
  93. */
  94. if ((long) rc_p || bad_context)
  95. printf("oops\n");
  96. if (bad_context)
  97. fprintf(stderr, "\t!! bad_context is true\n");
  98. FAIL_IF((long) rc_p || bad_context);
  99. }
  100. printf("done\n");
  101. free(tids);
  102. return 0;
  103. }
  104. int main(int argc, char *argv[])
  105. {
  106. return test_harness(test_signal_fpu, "fpu_signal");
  107. }