xref: /kvm-unit-tests/s390x/smp.c (revision 832e1c1596445f17d1542fc89aba81419154a4ca)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Tests sigp emulation
4  *
5  * Copyright 2019 IBM Corp.
6  *
7  * Authors:
8  *    Janosch Frank <frankja@linux.ibm.com>
9  */
10 #include <libcflat.h>
11 #include <asm/asm-offsets.h>
12 #include <asm/interrupt.h>
13 #include <asm/page.h>
14 #include <asm/facility.h>
15 #include <asm-generic/barrier.h>
16 #include <asm/sigp.h>
17 
18 #include <smp.h>
19 #include <uv.h>
20 #include <alloc_page.h>
21 
22 static int testflag = 0;
23 #define INVALID_CPU_ADDRESS -4711
24 #define INVALID_ORDER_CODE 0xFF
25 struct sigp_invalid_cases {
26 	int order;
27 	char message[100];
28 };
29 static const struct sigp_invalid_cases cases_invalid_cpu_addr[] = {
30 	{ SIGP_STOP,                  "stop with invalid CPU address" },
31 	{ SIGP_START,                 "start with invalid CPU address" },
32 	{ SIGP_CPU_RESET,             "reset with invalid CPU address" },
33 	{ INVALID_ORDER_CODE,         "invalid order code and CPU address" },
34 	{ SIGP_SENSE,                 "sense with invalid CPU address" },
35 	{ SIGP_STOP_AND_STORE_STATUS, "stop and store status with invalid CPU address" },
36 };
37 static const struct sigp_invalid_cases cases_valid_cpu_addr[] = {
38 	{ INVALID_ORDER_CODE,         "invalid order code" },
39 };
40 
41 static uint32_t cpu1_prefix;
42 
43 static void test_invalid(void)
44 {
45 	const struct sigp_invalid_cases *c;
46 	uint32_t status;
47 	int cc;
48 	int i;
49 
50 	report_prefix_push("invalid parameters");
51 
52 	for (i = 0; i < ARRAY_SIZE(cases_invalid_cpu_addr); i++) {
53 		c = &cases_invalid_cpu_addr[i];
54 		cc = sigp(INVALID_CPU_ADDRESS, c->order, 0, &status);
55 		report(cc == 3, "%s", c->message);
56 	}
57 
58 	for (i = 0; i < ARRAY_SIZE(cases_valid_cpu_addr); i++) {
59 		c = &cases_valid_cpu_addr[i];
60 		cc = smp_sigp(1, c->order, 0, &status);
61 		report(cc == 1, "%s", c->message);
62 	}
63 
64 	report_prefix_pop();
65 }
66 
67 static void wait_for_flag(void)
68 {
69 	while (!testflag)
70 		mb();
71 }
72 
73 static void set_flag(int val)
74 {
75 	mb();
76 	testflag = val;
77 	mb();
78 }
79 
80 static void test_func(void)
81 {
82 	set_flag(1);
83 }
84 
85 static void test_start(void)
86 {
87 	struct psw psw;
88 	psw.mask = extract_psw_mask();
89 	psw.addr = (unsigned long)test_func;
90 
91 	set_flag(0);
92 	smp_cpu_start(1, psw);
93 	wait_for_flag();
94 	report_pass("start");
95 }
96 
97 static void test_restart(void)
98 {
99 	struct cpu *cpu = smp_cpu_from_idx(1);
100 	struct lowcore *lc = cpu->lowcore;
101 	int rc;
102 
103 	report_prefix_push("restart");
104 	report_prefix_push("stopped");
105 
106 	lc->restart_new_psw.mask = extract_psw_mask();
107 	lc->restart_new_psw.addr = (unsigned long)test_func;
108 
109 	/* Make sure cpu is stopped */
110 	smp_cpu_stop(1);
111 	set_flag(0);
112 	rc = smp_cpu_restart_nowait(1);
113 	report(!rc, "return code");
114 	report(!smp_cpu_stopped(1), "cpu started");
115 	wait_for_flag();
116 	report_pass("test flag");
117 
118 	report_prefix_pop();
119 	report_prefix_push("running");
120 
121 	/*
122 	 * Wait until cpu 1 has set the flag because it executed the
123 	 * restart function.
124 	 */
125 	set_flag(0);
126 	rc = smp_cpu_restart_nowait(1);
127 	report(!rc, "return code");
128 	report(!smp_cpu_stopped(1), "cpu started");
129 	wait_for_flag();
130 	report_pass("test flag");
131 
132 	report_prefix_pop();
133 	report_prefix_pop();
134 }
135 
136 static void test_stop(void)
137 {
138 	int rc;
139 
140 	report_prefix_push("stop");
141 
142 	rc = smp_cpu_stop_nowait(1);
143 	report(!rc, "return code");
144 	report(smp_cpu_stopped(1), "cpu stopped");
145 
146 	report_prefix_push("stop stopped CPU");
147 	report(!smp_cpu_stop(1), "STOP succeeds");
148 	report(smp_cpu_stopped(1), "CPU is stopped");
149 	report_prefix_pop();
150 
151 	report_prefix_pop();
152 }
153 
154 static void test_stop_store_status(void)
155 {
156 	struct cpu *cpu = smp_cpu_from_idx(1);
157 	struct lowcore *lc = (void *)0x0;
158 
159 	report_prefix_push("stop store status");
160 	report_prefix_push("running");
161 	smp_cpu_restart(1);
162 	lc->prefix_sa = 0;
163 	lc->grs_sa[15] = 0;
164 	smp_cpu_stop_store_status(1);
165 	mb();
166 	report(smp_cpu_stopped(1), "cpu stopped");
167 	report(lc->prefix_sa == (uint32_t)(uintptr_t)cpu->lowcore, "prefix");
168 	report(lc->grs_sa[15], "stack");
169 	report_prefix_pop();
170 
171 	report_prefix_push("stopped");
172 	lc->prefix_sa = 0;
173 	lc->grs_sa[15] = 0;
174 	smp_cpu_stop_store_status(1);
175 	mb();
176 	report(smp_cpu_stopped(1), "cpu stopped");
177 	report(lc->prefix_sa == (uint32_t)(uintptr_t)cpu->lowcore, "prefix");
178 	report(lc->grs_sa[15], "stack");
179 	report_prefix_pop();
180 
181 	report_prefix_pop();
182 }
183 
184 static void test_store_status(void)
185 {
186 	struct cpu_status *status = alloc_pages_flags(1, AREA_DMA31);
187 	uint32_t r;
188 	int cc;
189 
190 	report_prefix_push("store status at address");
191 	memset(status, 0, PAGE_SIZE * 2);
192 
193 	report_prefix_push("invalid CPU address");
194 	cc = sigp(INVALID_CPU_ADDRESS, SIGP_STORE_STATUS_AT_ADDRESS, (uintptr_t)status, &r);
195 	report(cc == 3, "returned with CC = 3");
196 	report_prefix_pop();
197 
198 	report_prefix_push("running");
199 	smp_cpu_restart(1);
200 	smp_sigp(1, SIGP_STORE_STATUS_AT_ADDRESS, (uintptr_t)status, &r);
201 	report(r == SIGP_STATUS_INCORRECT_STATE, "incorrect state");
202 	report(!memcmp(status, (void *)status + PAGE_SIZE, PAGE_SIZE),
203 	       "status not written");
204 	report_prefix_pop();
205 
206 	memset(status, 0, PAGE_SIZE);
207 	report_prefix_push("stopped");
208 	smp_cpu_stop(1);
209 	smp_sigp(1, SIGP_STORE_STATUS_AT_ADDRESS, (uintptr_t)status, NULL);
210 	while (!status->prefix) { mb(); }
211 	report_pass("status written");
212 	free_pages(status);
213 	report_prefix_pop();
214 	smp_cpu_stop(1);
215 
216 	report_prefix_pop();
217 }
218 
219 static void loop(void)
220 {
221 	while (1)
222 		;
223 }
224 
225 static void stpx_and_set_flag(void)
226 {
227 	asm volatile (
228 		"	stpx %[prefix]\n"
229 		: [prefix] "=Q" (cpu1_prefix)
230 		:
231 		:
232 	);
233 
234 	set_flag(1);
235 }
236 
237 static void test_set_prefix(void)
238 {
239 	struct lowcore *new_lc = alloc_pages_flags(1, AREA_DMA31);
240 	struct cpu *cpu1 = smp_cpu_from_idx(1);
241 	uint32_t status = 0;
242 	struct psw new_psw;
243 	int cc;
244 
245 	report_prefix_push("set prefix");
246 
247 	assert(new_lc);
248 
249 	memcpy(new_lc, cpu1->lowcore, sizeof(struct lowcore));
250 	new_lc->restart_new_psw.addr = (unsigned long)loop;
251 
252 	report_prefix_push("running");
253 	set_flag(0);
254 	new_psw.addr = (unsigned long)stpx_and_set_flag;
255 	new_psw.mask = extract_psw_mask();
256 	smp_cpu_start(1, new_psw);
257 	wait_for_flag();
258 	cpu1_prefix = 0xFFFFFFFF;
259 
260 	cc = smp_sigp(1, SIGP_SET_PREFIX, (unsigned long)new_lc, &status);
261 	report(cc == 1, "CC = 1");
262 	report(status == SIGP_STATUS_INCORRECT_STATE, "status = INCORRECT_STATE");
263 
264 	/*
265 	 * If the prefix of the other CPU was changed it will enter an endless
266 	 * loop. Otherwise, it should eventually set the flag.
267 	 */
268 	smp_cpu_stop(1);
269 	set_flag(0);
270 	smp_cpu_restart(1);
271 	wait_for_flag();
272 	report(cpu1_prefix == (uint64_t)cpu1->lowcore, "prefix unchanged");
273 
274 	report_prefix_pop();
275 
276 	report_prefix_push("invalid CPU address");
277 
278 	cc = sigp(INVALID_CPU_ADDRESS, SIGP_SET_PREFIX, (unsigned long)new_lc, &status);
279 	report(cc == 3, "CC = 3");
280 
281 	report_prefix_pop();
282 
283 	free_pages(new_lc);
284 
285 	report_prefix_pop();
286 
287 }
288 
289 static void ecall(void)
290 {
291 	unsigned long mask;
292 	struct lowcore *lc = (void *)0x0;
293 
294 	expect_ext_int();
295 	ctl_set_bit(0, CTL0_EXTERNAL_CALL);
296 	mask = extract_psw_mask();
297 	mask |= PSW_MASK_EXT;
298 	load_psw_mask(mask);
299 	set_flag(1);
300 	while (lc->ext_int_code != 0x1202) { mb(); }
301 	report_pass("received");
302 	set_flag(1);
303 }
304 
305 static void test_ecall(void)
306 {
307 	struct psw psw;
308 	psw.mask = extract_psw_mask();
309 	psw.addr = (unsigned long)ecall;
310 
311 	report_prefix_push("ecall");
312 	set_flag(0);
313 
314 	smp_cpu_start(1, psw);
315 	wait_for_flag();
316 	set_flag(0);
317 	smp_sigp(1, SIGP_EXTERNAL_CALL, 0, NULL);
318 	wait_for_flag();
319 	smp_cpu_stop(1);
320 	report_prefix_pop();
321 }
322 
323 static void emcall(void)
324 {
325 	unsigned long mask;
326 	struct lowcore *lc = (void *)0x0;
327 
328 	expect_ext_int();
329 	ctl_set_bit(0, CTL0_EMERGENCY_SIGNAL);
330 	mask = extract_psw_mask();
331 	mask |= PSW_MASK_EXT;
332 	load_psw_mask(mask);
333 	set_flag(1);
334 	while (lc->ext_int_code != 0x1201) { mb(); }
335 	report_pass("received");
336 	set_flag(1);
337 }
338 
339 static void test_emcall(void)
340 {
341 	struct psw psw;
342 	int cc;
343 	psw.mask = extract_psw_mask();
344 	psw.addr = (unsigned long)emcall;
345 
346 	report_prefix_push("emcall");
347 	set_flag(0);
348 
349 	smp_cpu_start(1, psw);
350 	wait_for_flag();
351 	set_flag(0);
352 	smp_sigp(1, SIGP_EMERGENCY_SIGNAL, 0, NULL);
353 	wait_for_flag();
354 	smp_cpu_stop(1);
355 
356 	report_prefix_push("invalid CPU address");
357 
358 	cc = sigp(INVALID_CPU_ADDRESS, SIGP_EMERGENCY_SIGNAL, 0, NULL);
359 	report(cc == 3, "CC = 3");
360 
361 	report_prefix_pop();
362 
363 	report_prefix_pop();
364 }
365 
366 static void test_cond_emcall(void)
367 {
368 	uint32_t status = 0;
369 	struct psw psw;
370 	int cc;
371 	psw.mask = extract_psw_mask() & ~PSW_MASK_IO;
372 	psw.addr = (unsigned long)emcall;
373 
374 	report_prefix_push("conditional emergency call");
375 
376 	if (uv_os_is_guest()) {
377 		report_skip("unsupported under PV");
378 		goto out;
379 	}
380 
381 	report_prefix_push("invalid CPU address");
382 
383 	cc = sigp(INVALID_CPU_ADDRESS, SIGP_COND_EMERGENCY_SIGNAL, 0, NULL);
384 	report(cc == 3, "CC = 3");
385 
386 	report_prefix_pop();
387 
388 	report_prefix_push("success");
389 	set_flag(0);
390 
391 	smp_cpu_start(1, psw);
392 	wait_for_flag();
393 	set_flag(0);
394 	cc = smp_sigp(1, SIGP_COND_EMERGENCY_SIGNAL, 0, &status);
395 	report(!cc, "CC = 0");
396 
397 	wait_for_flag();
398 	smp_cpu_stop(1);
399 
400 	report_prefix_pop();
401 
402 out:
403 	report_prefix_pop();
404 
405 }
406 
407 static void test_sense_running(void)
408 {
409 	report_prefix_push("sense_running");
410 	/* we (CPU0) are running */
411 	report(smp_sense_running_status(0), "CPU0 sense claims running");
412 	/* stop the target CPU (CPU1) to speed up the not running case */
413 	smp_cpu_stop(1);
414 	/* Make sure to have at least one time with a not running indication */
415 	while(smp_sense_running_status(1));
416 	report_pass("CPU1 sense claims not running");
417 	report_prefix_pop();
418 }
419 
420 /* Used to dirty registers of cpu #1 before it is reset */
421 static void test_func_initial(void)
422 {
423 	asm volatile("sfpc %0" :: "d" (0x11));
424 	lctlg(1, 0x42000UL);
425 	lctlg(7, 0x43000UL);
426 	lctlg(13, 0x44000UL);
427 	set_flag(1);
428 }
429 
430 static void test_reset_initial(void)
431 {
432 	struct cpu_status *status = alloc_pages_flags(0, AREA_DMA31);
433 	struct psw psw;
434 	int i;
435 
436 	psw.mask = extract_psw_mask();
437 	psw.addr = (unsigned long)test_func_initial;
438 
439 	report_prefix_push("reset initial");
440 	set_flag(0);
441 	smp_cpu_start(1, psw);
442 	wait_for_flag();
443 
444 	smp_sigp(1, SIGP_INITIAL_CPU_RESET, 0, NULL);
445 	smp_sigp(1, SIGP_STORE_STATUS_AT_ADDRESS, (uintptr_t)status, NULL);
446 
447 	report_prefix_push("clear");
448 	report(!status->psw.mask && !status->psw.addr, "psw");
449 	report(!status->prefix, "prefix");
450 	report(!status->fpc, "fpc");
451 	report(!status->cputm, "cpu timer");
452 	report(!status->todpr, "todpr");
453 	for (i = 1; i <= 13; i++) {
454 		report(status->crs[i] == 0, "cr%d == 0", i);
455 	}
456 	report(status->crs[15] == 0, "cr15 == 0");
457 	report_prefix_pop();
458 
459 	report_prefix_push("initialized");
460 	report(status->crs[0] == 0xE0UL, "cr0 == 0xE0");
461 	report(status->crs[14] == 0xC2000000UL, "cr14 == 0xC2000000");
462 	report_prefix_pop();
463 
464 	report(smp_cpu_stopped(1), "cpu stopped");
465 	free_pages(status);
466 	report_prefix_pop();
467 }
468 
469 static void test_local_ints(void)
470 {
471 	unsigned long mask;
472 
473 	/* Open masks for ecall and emcall */
474 	ctl_set_bit(0, CTL0_EXTERNAL_CALL);
475 	ctl_set_bit(0, CTL0_EMERGENCY_SIGNAL);
476 	mask = extract_psw_mask();
477 	mask |= PSW_MASK_EXT;
478 	load_psw_mask(mask);
479 	set_flag(1);
480 }
481 
482 static void test_reset(void)
483 {
484 	struct psw psw;
485 
486 	psw.mask = extract_psw_mask();
487 	psw.addr = (unsigned long)test_func;
488 
489 	report_prefix_push("cpu reset");
490 	smp_sigp(1, SIGP_EMERGENCY_SIGNAL, 0, NULL);
491 	smp_sigp(1, SIGP_EXTERNAL_CALL, 0, NULL);
492 	smp_cpu_start(1, psw);
493 
494 	smp_sigp(1, SIGP_CPU_RESET, 0, NULL);
495 	report(smp_cpu_stopped(1), "cpu stopped");
496 
497 	set_flag(0);
498 	psw.addr = (unsigned long)test_local_ints;
499 	smp_cpu_start(1, psw);
500 	wait_for_flag();
501 	report_pass("local interrupts cleared");
502 	report_prefix_pop();
503 }
504 
505 int main(void)
506 {
507 	struct psw psw;
508 	report_prefix_push("smp");
509 
510 	if (smp_query_num_cpus() == 1) {
511 		report_skip("need at least 2 cpus for this test");
512 		goto done;
513 	}
514 
515 	/* Setting up the cpu to give it a stack and lowcore */
516 	psw.mask = extract_psw_mask();
517 	psw.addr = (unsigned long)test_func;
518 	smp_cpu_setup(1, psw);
519 	smp_cpu_stop(1);
520 
521 	test_start();
522 	test_invalid();
523 	test_restart();
524 	test_stop();
525 	test_stop_store_status();
526 	test_store_status();
527 	test_set_prefix();
528 	test_ecall();
529 	test_emcall();
530 	test_cond_emcall();
531 	test_sense_running();
532 	test_reset();
533 	test_reset_initial();
534 	smp_cpu_destroy(1);
535 
536 done:
537 	report_prefix_pop();
538 	return report_summary();
539 }
540