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 FPU registers are correctly reported in a
10 * signal context. Each worker just spins checking its FPU 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
15 #include <stdio.h>
16 #include <unistd.h>
17 #include <sys/syscall.h>
18 #include <sys/time.h>
19 #include <sys/types.h>
20 #include <sys/wait.h>
21 #include <stdlib.h>
22 #include <pthread.h>
23
24 #include "utils.h"
25
26 /* Number of times each thread should receive the signal */
27 #define ITERATIONS 10
28 /*
29 * Factor by which to multiply number of online CPUs for total number of
30 * worker threads
31 */
32 #define THREAD_FACTOR 8
33
34 __thread double darray[] = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0,
35 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0,
36 2.1};
37
38 bool bad_context;
39 int threads_starting;
40 int running;
41
42 extern long preempt_fpu(double *darray, int *threads_starting, int *running);
43
signal_fpu_sig(int sig,siginfo_t * info,void * context)44 void signal_fpu_sig(int sig, siginfo_t *info, void *context)
45 {
46 int i;
47 ucontext_t *uc = context;
48 mcontext_t *mc = &uc->uc_mcontext;
49
50 /* Only the non volatiles were loaded up */
51 for (i = 14; i < 32; i++) {
52 if (mc->fp_regs[i] != darray[i - 14]) {
53 bad_context = true;
54 break;
55 }
56 }
57 }
58
signal_fpu_c(void * p)59 void *signal_fpu_c(void *p)
60 {
61 int i;
62 long rc;
63 struct sigaction act;
64 act.sa_sigaction = signal_fpu_sig;
65 act.sa_flags = SA_SIGINFO;
66 rc = sigaction(SIGUSR1, &act, NULL);
67 if (rc)
68 return p;
69
70 srand(pthread_self());
71 for (i = 0; i < 21; i++)
72 darray[i] = rand();
73
74 rc = preempt_fpu(darray, &threads_starting, &running);
75
76 return (void *) rc;
77 }
78
test_signal_fpu(void)79 int test_signal_fpu(void)
80 {
81 int i, j, rc, threads;
82 void *rc_p;
83 pthread_t *tids;
84
85 threads = sysconf(_SC_NPROCESSORS_ONLN) * THREAD_FACTOR;
86 tids = malloc(threads * sizeof(pthread_t));
87 FAIL_IF(!tids);
88
89 running = true;
90 threads_starting = threads;
91 for (i = 0; i < threads; i++) {
92 rc = pthread_create(&tids[i], NULL, signal_fpu_c, NULL);
93 FAIL_IF(rc);
94 }
95
96 setbuf(stdout, NULL);
97 printf("\tWaiting for all workers to start...");
98 while (threads_starting)
99 asm volatile("": : :"memory");
100 printf("done\n");
101
102 printf("\tSending signals to all threads %d times...", ITERATIONS);
103 for (i = 0; i < ITERATIONS; i++) {
104 for (j = 0; j < threads; j++) {
105 pthread_kill(tids[j], SIGUSR1);
106 }
107 sleep(1);
108 }
109 printf("done\n");
110
111 printf("\tStopping workers...");
112 running = 0;
113 for (i = 0; i < threads; i++) {
114 pthread_join(tids[i], &rc_p);
115
116 /*
117 * Harness will say the fail was here, look at why signal_fpu
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
128 free(tids);
129 return 0;
130 }
131
main(int argc,char * argv[])132 int main(int argc, char *argv[])
133 {
134 return test_harness(test_signal_fpu, "fpu_signal");
135 }
136