xref: /kvm-unit-tests/s390x/smp.c (revision ea71612b8b179d3c38e6878faa5f010306168dc3)
1 /*
2  * Tests sigp emulation
3  *
4  * Copyright 2019 IBM Corp.
5  *
6  * Authors:
7  *    Janosch Frank <frankja@linux.ibm.com>
8  *
9  * This code is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License version 2.
11  */
12 #include <libcflat.h>
13 #include <asm/asm-offsets.h>
14 #include <asm/interrupt.h>
15 #include <asm/page.h>
16 #include <asm/facility.h>
17 #include <asm-generic/barrier.h>
18 #include <asm/sigp.h>
19 
20 #include <smp.h>
21 #include <alloc_page.h>
22 
23 static int testflag = 0;
24 
25 static void wait_for_flag(void)
26 {
27 	while (!testflag)
28 		mb();
29 }
30 
31 static void set_flag(int val)
32 {
33 	mb();
34 	testflag = val;
35 	mb();
36 }
37 
38 static void test_func(void)
39 {
40 	set_flag(1);
41 }
42 
43 static void test_start(void)
44 {
45 	struct psw psw;
46 	psw.mask = extract_psw_mask();
47 	psw.addr = (unsigned long)test_func;
48 
49 	set_flag(0);
50 	smp_cpu_start(1, psw);
51 	wait_for_flag();
52 	report(1, "start");
53 }
54 
55 /*
56  * Does only test restart when the target is running.
57  * The other tests do restarts when stopped multiple times already.
58  */
59 static void test_restart(void)
60 {
61 	struct cpu *cpu = smp_cpu_from_addr(1);
62 	struct lowcore *lc = cpu->lowcore;
63 
64 	lc->restart_new_psw.mask = extract_psw_mask();
65 	lc->restart_new_psw.addr = (unsigned long)test_func;
66 
67 	/* Make sure cpu is running */
68 	smp_cpu_stop(0);
69 	set_flag(0);
70 	smp_cpu_restart(1);
71 	wait_for_flag();
72 
73 	/*
74 	 * Wait until cpu 1 has set the flag because it executed the
75 	 * restart function.
76 	 */
77 	set_flag(0);
78 	smp_cpu_restart(1);
79 	wait_for_flag();
80 	report(1, "restart while running");
81 }
82 
83 static void test_stop(void)
84 {
85 	smp_cpu_stop(1);
86 	/*
87 	 * The smp library waits for the CPU to shut down, but let's
88 	 * also do it here, so we don't rely on the library
89 	 * implementation
90 	 */
91 	while (!smp_cpu_stopped(1)) {}
92 	report(1, "stop");
93 }
94 
95 static void test_stop_store_status(void)
96 {
97 	struct cpu *cpu = smp_cpu_from_addr(1);
98 	struct lowcore *lc = (void *)0x0;
99 
100 	report_prefix_push("stop store status");
101 	report_prefix_push("running");
102 	smp_cpu_restart(1);
103 	lc->prefix_sa = 0;
104 	lc->grs_sa[15] = 0;
105 	smp_cpu_stop_store_status(1);
106 	mb();
107 	report(lc->prefix_sa == (uint32_t)(uintptr_t)cpu->lowcore, "prefix");
108 	report(lc->grs_sa[15], "stack");
109 	report(smp_cpu_stopped(1), "cpu stopped");
110 	report_prefix_pop();
111 
112 	report_prefix_push("stopped");
113 	lc->prefix_sa = 0;
114 	lc->grs_sa[15] = 0;
115 	smp_cpu_stop_store_status(1);
116 	mb();
117 	report(lc->prefix_sa == (uint32_t)(uintptr_t)cpu->lowcore, "prefix");
118 	report(lc->grs_sa[15], "stack");
119 	report_prefix_pop();
120 
121 	report_prefix_pop();
122 }
123 
124 static void test_store_status(void)
125 {
126 	struct cpu_status *status = alloc_pages(1);
127 	uint32_t r;
128 
129 	report_prefix_push("store status at address");
130 	memset(status, 0, PAGE_SIZE * 2);
131 
132 	report_prefix_push("running");
133 	smp_cpu_restart(1);
134 	sigp(1, SIGP_STORE_STATUS_AT_ADDRESS, (uintptr_t)status, &r);
135 	report(r == SIGP_STATUS_INCORRECT_STATE, "incorrect state");
136 	report(!memcmp(status, (void *)status + PAGE_SIZE, PAGE_SIZE),
137 	       "status not written");
138 	report_prefix_pop();
139 
140 	memset(status, 0, PAGE_SIZE);
141 	report_prefix_push("stopped");
142 	smp_cpu_stop(1);
143 	sigp(1, SIGP_STORE_STATUS_AT_ADDRESS, (uintptr_t)status, NULL);
144 	while (!status->prefix) { mb(); }
145 	report(1, "status written");
146 	free_pages(status);
147 	report_prefix_pop();
148 	smp_cpu_stop(1);
149 
150 	report_prefix_pop();
151 }
152 
153 static void ecall(void)
154 {
155 	unsigned long mask;
156 	struct lowcore *lc = (void *)0x0;
157 
158 	expect_ext_int();
159 	ctl_set_bit(0, 13);
160 	mask = extract_psw_mask();
161 	mask |= PSW_MASK_EXT;
162 	load_psw_mask(mask);
163 	set_flag(1);
164 	while (lc->ext_int_code != 0x1202) { mb(); }
165 	report(1, "received");
166 	set_flag(1);
167 }
168 
169 static void test_ecall(void)
170 {
171 	struct psw psw;
172 	psw.mask = extract_psw_mask();
173 	psw.addr = (unsigned long)ecall;
174 
175 	report_prefix_push("ecall");
176 	set_flag(0);
177 
178 	smp_cpu_start(1, psw);
179 	wait_for_flag();
180 	set_flag(0);
181 	sigp(1, SIGP_EXTERNAL_CALL, 0, NULL);
182 	wait_for_flag();
183 	smp_cpu_stop(1);
184 	report_prefix_pop();
185 }
186 
187 static void emcall(void)
188 {
189 	unsigned long mask;
190 	struct lowcore *lc = (void *)0x0;
191 
192 	expect_ext_int();
193 	ctl_set_bit(0, 14);
194 	mask = extract_psw_mask();
195 	mask |= PSW_MASK_EXT;
196 	load_psw_mask(mask);
197 	set_flag(1);
198 	while (lc->ext_int_code != 0x1201) { mb(); }
199 	report(1, "received");
200 	set_flag(1);
201 }
202 
203 static void test_emcall(void)
204 {
205 	struct psw psw;
206 	psw.mask = extract_psw_mask();
207 	psw.addr = (unsigned long)emcall;
208 
209 	report_prefix_push("emcall");
210 	set_flag(0);
211 
212 	smp_cpu_start(1, psw);
213 	wait_for_flag();
214 	set_flag(0);
215 	sigp(1, SIGP_EMERGENCY_SIGNAL, 0, NULL);
216 	wait_for_flag();
217 	smp_cpu_stop(1);
218 	report_prefix_pop();
219 }
220 
221 static void test_sense_running(void)
222 {
223 	report_prefix_push("sense_running");
224 	/* we (CPU0) are running */
225 	report(smp_sense_running_status(0), "CPU0 sense claims running");
226 	/* stop the target CPU (CPU1) to speed up the not running case */
227 	smp_cpu_stop(1);
228 	/* Make sure to have at least one time with a not running indication */
229 	while(smp_sense_running_status(1));
230 	report(true, "CPU1 sense claims not running");
231 	report_prefix_pop();
232 }
233 
234 /* Used to dirty registers of cpu #1 before it is reset */
235 static void test_func_initial(void)
236 {
237 	asm volatile("sfpc %0" :: "d" (0x11));
238 	lctlg(1, 0x42000UL);
239 	lctlg(7, 0x43000UL);
240 	lctlg(13, 0x44000UL);
241 	set_flag(1);
242 }
243 
244 static void test_reset_initial(void)
245 {
246 	struct cpu_status *status = alloc_pages(0);
247 	struct psw psw;
248 	int i;
249 
250 	psw.mask = extract_psw_mask();
251 	psw.addr = (unsigned long)test_func_initial;
252 
253 	report_prefix_push("reset initial");
254 	set_flag(0);
255 	smp_cpu_start(1, psw);
256 	wait_for_flag();
257 
258 	sigp_retry(1, SIGP_INITIAL_CPU_RESET, 0, NULL);
259 	sigp(1, SIGP_STORE_STATUS_AT_ADDRESS, (uintptr_t)status, NULL);
260 
261 	report_prefix_push("clear");
262 	report(!status->psw.mask && !status->psw.addr, "psw");
263 	report(!status->prefix, "prefix");
264 	report(!status->fpc, "fpc");
265 	report(!status->cputm, "cpu timer");
266 	report(!status->todpr, "todpr");
267 	for (i = 1; i <= 13; i++) {
268 		report(status->crs[i] == 0, "cr%d == 0", i);
269 	}
270 	report(status->crs[15] == 0, "cr15 == 0");
271 	report_prefix_pop();
272 
273 	report_prefix_push("initialized");
274 	report(status->crs[0] == 0xE0UL, "cr0 == 0xE0");
275 	report(status->crs[14] == 0xC2000000UL, "cr14 == 0xC2000000");
276 	report_prefix_pop();
277 
278 	report(smp_cpu_stopped(1), "cpu stopped");
279 	free_pages(status);
280 	report_prefix_pop();
281 }
282 
283 static void test_local_ints(void)
284 {
285 	unsigned long mask;
286 
287 	/* Open masks for ecall and emcall */
288 	ctl_set_bit(0, 13);
289 	ctl_set_bit(0, 14);
290 	mask = extract_psw_mask();
291 	mask |= PSW_MASK_EXT;
292 	load_psw_mask(mask);
293 	set_flag(1);
294 }
295 
296 static void test_reset(void)
297 {
298 	struct psw psw;
299 
300 	psw.mask = extract_psw_mask();
301 	psw.addr = (unsigned long)test_func;
302 
303 	report_prefix_push("cpu reset");
304 	sigp(1, SIGP_EMERGENCY_SIGNAL, 0, NULL);
305 	sigp(1, SIGP_EXTERNAL_CALL, 0, NULL);
306 	smp_cpu_start(1, psw);
307 
308 	sigp_retry(1, SIGP_CPU_RESET, 0, NULL);
309 	report(smp_cpu_stopped(1), "cpu stopped");
310 
311 	set_flag(0);
312 	psw.addr = (unsigned long)test_local_ints;
313 	smp_cpu_start(1, psw);
314 	wait_for_flag();
315 	report(true, "local interrupts cleared");
316 	report_prefix_pop();
317 }
318 
319 int main(void)
320 {
321 	struct psw psw;
322 	report_prefix_push("smp");
323 
324 	if (smp_query_num_cpus() == 1) {
325 		report_skip("need at least 2 cpus for this test");
326 		goto done;
327 	}
328 
329 	/* Setting up the cpu to give it a stack and lowcore */
330 	psw.mask = extract_psw_mask();
331 	psw.addr = (unsigned long)test_func;
332 	smp_cpu_setup(1, psw);
333 	smp_cpu_stop(1);
334 
335 	test_start();
336 	test_restart();
337 	test_stop();
338 	test_stop_store_status();
339 	test_store_status();
340 	test_ecall();
341 	test_emcall();
342 	test_sense_running();
343 	test_reset();
344 	test_reset_initial();
345 	smp_cpu_destroy(1);
346 
347 done:
348 	report_prefix_pop();
349 	return report_summary();
350 }
351