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 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.
9  */
10 
11 #include <stdio.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <sys/syscall.h>
15 #include <sys/time.h>
16 #include <sys/types.h>
17 #include <sys/wait.h>
18 #include <stdlib.h>
19 #include <pthread.h>
20 
21 #include "utils.h"
22 
23 /* Time to wait for workers to get preempted (seconds) */
24 #define PREEMPT_TIME 20
25 /*
26  * Factor by which to multiply number of online CPUs for total number of
27  * worker threads
28  */
29 #define THREAD_FACTOR 8
30 
31 /*
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()
35  */
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}
41 };
42 
43 int threads_starting;
44 int running;
45 
46 extern long preempt_vsx(vector int *varray, int *threads_starting, int *running);
47 
48 long vsx_memcmp(vector int *a) {
49 	vector int zero = {0, 0, 0, 0};
50 	int i;
51 
52 	FAIL_IF(a != varray);
53 
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);
57 			return 2;
58 		}
59 	}
60 
61 	if (memcmp(a, &a[12], 12 * sizeof(vector int))) {
62 		long *p = (long *)a;
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]);
67 		return 1;
68 	}
69 	return 0;
70 }
71 
72 void *preempt_vsx_c(void *p)
73 {
74 	int i, j;
75 	long rc;
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)
82 				j--;
83 		}
84 	rc = preempt_vsx(varray, &threads_starting, &running);
85 	if (rc == 2)
86 		fprintf(stderr, "Caught zeros in VSX compares\n");
87 	return (void *)rc;
88 }
89 
90 int test_preempt_vsx(void)
91 {
92 	int i, rc, threads;
93 	pthread_t *tids;
94 
95 	SKIP_IF(!have_hwcap(PPC_FEATURE_HAS_VSX));
96 
97 	threads = sysconf(_SC_NPROCESSORS_ONLN) * THREAD_FACTOR;
98 	tids = malloc(threads * sizeof(pthread_t));
99 	FAIL_IF(!tids);
100 
101 	running = true;
102 	threads_starting = threads;
103 	for (i = 0; i < threads; i++) {
104 		rc = pthread_create(&tids[i], NULL, preempt_vsx_c, NULL);
105 		FAIL_IF(rc);
106 	}
107 
108 	setbuf(stdout, NULL);
109 	/* Not really nessesary but nice to wait for every thread to start */
110 	printf("\tWaiting for %d workers to start...", threads_starting);
111 	while(threads_starting)
112 		asm volatile("": : :"memory");
113 	printf("done\n");
114 
115 	printf("\tWaiting for %d seconds to let some workers get preempted...", PREEMPT_TIME);
116 	sleep(PREEMPT_TIME);
117 	printf("done\n");
118 
119 	printf("\tStopping workers...");
120 	/*
121 	 * Working are checking this value every loop. In preempt_vsx 'cmpwi r5,0; bne 2b'.
122 	 * r5 will have loaded the value of running.
123 	 */
124 	running = 0;
125 	for (i = 0; i < threads; i++) {
126 		void *rc_p;
127 		pthread_join(tids[i], &rc_p);
128 
129 		/*
130 		 * Harness will say the fail was here, look at why preempt_vsx
131 		 * returned
132 		 */
133 		if ((long) rc_p)
134 			printf("oops\n");
135 		FAIL_IF((long) rc_p);
136 	}
137 	printf("done\n");
138 
139 	return 0;
140 }
141 
142 int main(int argc, char *argv[])
143 {
144 	return test_harness(test_preempt_vsx, "vsx_preempt");
145 }
146