1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2015, Cyril Bur, IBM Corp.
5 * This test attempts to see if the VSX registers change across preemption.
6 * There is no way to be sure preemption happened so this test just
7 * uses many threads and a long wait. As such, a successful test
8 * doesn't mean much but a failure is bad.
14 #include <sys/syscall.h>
16 #include <sys/types.h>
23 /* Time to wait for workers to get preempted (seconds) */
24 #define PREEMPT_TIME 20
26 * Factor by which to multiply number of online CPUs for total number of
29 #define THREAD_FACTOR 8
32 * Ensure there is twice the number of non-volatile VMX regs!
33 * check_vmx() is going to use the other half as space to put the live
34 * registers before calling vsx_memcmp()
36 __thread vector int varray[24] = {
37 {1, 2, 3, 4 }, {5, 6, 7, 8 }, {9, 10,11,12},
38 {13,14,15,16}, {17,18,19,20}, {21,22,23,24},
39 {25,26,27,28}, {29,30,31,32}, {33,34,35,36},
40 {37,38,39,40}, {41,42,43,44}, {45,46,47,48}
46 extern long preempt_vsx(vector int *varray, int *threads_starting, int *running);
48 long vsx_memcmp(vector int *a) {
49 vector int zero = {0, 0, 0, 0};
54 for(i = 0; i < 12; i++) {
55 if (memcmp(&a[i + 12], &zero, sizeof(vector int)) == 0) {
56 fprintf(stderr, "Detected zero from the VSX reg %d\n", i + 12);
61 if (memcmp(a, &a[12], 12 * sizeof(vector int))) {
63 fprintf(stderr, "VSX mismatch\n");
64 for (i = 0; i < 24; i=i+2)
65 fprintf(stderr, "%d: 0x%08lx%08lx | 0x%08lx%08lx\n",
66 i/2 + i%2 + 20, p[i], p[i + 1], p[i + 24], p[i + 25]);
72 void *preempt_vsx_c(void *p)
76 srand(pthread_self());
77 for (i = 0; i < 12; i++)
78 for (j = 0; j < 4; j++) {
79 varray[i][j] = rand();
80 /* Don't want zero because it hides kernel problems */
81 if (varray[i][j] == 0)
84 rc = preempt_vsx(varray, &threads_starting, &running);
86 fprintf(stderr, "Caught zeros in VSX compares\n");
90 int test_preempt_vsx(void)
95 threads = sysconf(_SC_NPROCESSORS_ONLN) * THREAD_FACTOR;
96 tids = malloc(threads * sizeof(pthread_t));
100 threads_starting = threads;
101 for (i = 0; i < threads; i++) {
102 rc = pthread_create(&tids[i], NULL, preempt_vsx_c, NULL);
106 setbuf(stdout, NULL);
107 /* Not really nessesary but nice to wait for every thread to start */
108 printf("\tWaiting for %d workers to start...", threads_starting);
109 while(threads_starting)
110 asm volatile("": : :"memory");
113 printf("\tWaiting for %d seconds to let some workers get preempted...", PREEMPT_TIME);
117 printf("\tStopping workers...");
119 * Working are checking this value every loop. In preempt_vsx 'cmpwi r5,0; bne 2b'.
120 * r5 will have loaded the value of running.
123 for (i = 0; i < threads; i++) {
125 pthread_join(tids[i], &rc_p);
128 * Harness will say the fail was here, look at why preempt_vsx
133 FAIL_IF((long) rc_p);
140 int main(int argc, char *argv[])
142 return test_harness(test_preempt_vsx, "vsx_preempt");