1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2015, Cyril Bur, IBM Corp.
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.
13 #include <sys/syscall.h>
15 #include <sys/types.h>
22 /* Number of times each thread should receive the signal */
25 * Factor by which to multiply number of online CPUs for total number of
28 #define THREAD_FACTOR 8
30 __thread double darray[] = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0,
31 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0,
38 extern long preempt_fpu(double *darray, int *threads_starting, int *running);
40 void signal_fpu_sig(int sig, siginfo_t *info, void *context)
43 ucontext_t *uc = context;
44 mcontext_t *mc = &uc->uc_mcontext;
46 /* Only the non volatiles were loaded up */
47 for (i = 14; i < 32; i++) {
48 if (mc->fp_regs[i] != darray[i - 14]) {
55 void *signal_fpu_c(void *p)
60 act.sa_sigaction = signal_fpu_sig;
61 act.sa_flags = SA_SIGINFO;
62 rc = sigaction(SIGUSR1, &act, NULL);
66 srand(pthread_self());
67 for (i = 0; i < 21; i++)
70 rc = preempt_fpu(darray, &threads_starting, &running);
75 int test_signal_fpu(void)
77 int i, j, rc, threads;
81 threads = sysconf(_SC_NPROCESSORS_ONLN) * THREAD_FACTOR;
82 tids = malloc(threads * sizeof(pthread_t));
86 threads_starting = threads;
87 for (i = 0; i < threads; i++) {
88 rc = pthread_create(&tids[i], NULL, signal_fpu_c, NULL);
93 printf("\tWaiting for all workers to start...");
94 while (threads_starting)
95 asm volatile("": : :"memory");
98 printf("\tSending signals to all threads %d times...", ITERATIONS);
99 for (i = 0; i < ITERATIONS; i++) {
100 for (j = 0; j < threads; j++) {
101 pthread_kill(tids[j], SIGUSR1);
107 printf("\tStopping workers...");
109 for (i = 0; i < threads; i++) {
110 pthread_join(tids[i], &rc_p);
113 * Harness will say the fail was here, look at why signal_fpu
116 if ((long) rc_p || bad_context)
119 fprintf(stderr, "\t!! bad_context is true\n");
120 FAIL_IF((long) rc_p || bad_context);
128 int main(int argc, char *argv[])
130 return test_harness(test_signal_fpu, "fpu_signal");