3e5b5663d24492638f8ab9c3f310ef9b4697fb7c
[platform/kernel/linux-rpi.git] / tools / testing / selftests / powerpc / math / fpu_preempt.c
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 change across preemption.
6  * Two things should be noted here a) The check_fpu function in asm only checks
7  * the non volatile registers as it is reused from the syscall test b) There is
8  * no way to be sure preemption happened so this test just uses many threads
9  * and a long wait. As such, a successful test doesn't mean much but a failure
10  * is bad.
11  */
12
13 #include <stdio.h>
14 #include <unistd.h>
15 #include <sys/syscall.h>
16 #include <sys/time.h>
17 #include <sys/types.h>
18 #include <sys/wait.h>
19 #include <stdlib.h>
20 #include <pthread.h>
21
22 #include "utils.h"
23
24 /* Time to wait for workers to get preempted (seconds) */
25 #define PREEMPT_TIME 20
26 /*
27  * Factor by which to multiply number of online CPUs for total number of
28  * worker threads
29  */
30 #define THREAD_FACTOR 8
31
32
33 __thread double darray[] = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0,
34                      1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0,
35                      2.1};
36
37 int threads_starting;
38 int running;
39
40 extern int preempt_fpu(double *darray, int *threads_starting, int *running);
41
42 void *preempt_fpu_c(void *p)
43 {
44         long rc;
45         int i;
46
47         srand(pthread_self());
48         for (i = 0; i < 21; i++)
49                 darray[i] = rand();
50
51         rc = preempt_fpu(darray, &threads_starting, &running);
52
53         return (void *)rc;
54 }
55
56 int test_preempt_fpu(void)
57 {
58         int i, rc, threads;
59         pthread_t *tids;
60
61         threads = sysconf(_SC_NPROCESSORS_ONLN) * THREAD_FACTOR;
62         tids = malloc((threads) * sizeof(pthread_t));
63         FAIL_IF(!tids);
64
65         running = true;
66         threads_starting = threads;
67         for (i = 0; i < threads; i++) {
68                 rc = pthread_create(&tids[i], NULL, preempt_fpu_c, NULL);
69                 FAIL_IF(rc);
70         }
71
72         setbuf(stdout, NULL);
73         /* Not really necessary but nice to wait for every thread to start */
74         printf("\tWaiting for all workers to start...");
75         while(threads_starting)
76                 asm volatile("": : :"memory");
77         printf("done\n");
78
79         printf("\tWaiting for %d seconds to let some workers get preempted...", PREEMPT_TIME);
80         sleep(PREEMPT_TIME);
81         printf("done\n");
82
83         printf("\tStopping workers...");
84         /*
85          * Working are checking this value every loop. In preempt_fpu 'cmpwi r5,0; bne 2b'.
86          * r5 will have loaded the value of running.
87          */
88         running = 0;
89         for (i = 0; i < threads; i++) {
90                 void *rc_p;
91                 pthread_join(tids[i], &rc_p);
92
93                 /*
94                  * Harness will say the fail was here, look at why preempt_fpu
95                  * returned
96                  */
97                 if ((long) rc_p)
98                         printf("oops\n");
99                 FAIL_IF((long) rc_p);
100         }
101         printf("done\n");
102
103         free(tids);
104         return 0;
105 }
106
107 int main(int argc, char *argv[])
108 {
109         return test_harness(test_preempt_fpu, "fpu_preempt");
110 }