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