xref: /kvm-unit-tests/s390x/smp.c (revision 6163f75d09a0a96a5c3db82dd768b13f79629c00)
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 cpu_loop(void)
26 {
27 	for (;;) {}
28 }
29 
30 static void test_func(void)
31 {
32 	testflag = 1;
33 	mb();
34 	cpu_loop();
35 }
36 
37 static void test_start(void)
38 {
39 	struct psw psw;
40 	psw.mask =  extract_psw_mask();
41 	psw.addr = (unsigned long)test_func;
42 
43 	smp_cpu_setup(1, psw);
44 	while (!testflag) {
45 		mb();
46 	}
47 	report("start", 1);
48 }
49 
50 static void test_stop(void)
51 {
52 	smp_cpu_stop(1);
53 	/*
54 	 * The smp library waits for the CPU to shut down, but let's
55 	 * also do it here, so we don't rely on the library
56 	 * implementation
57 	 */
58 	while (!smp_cpu_stopped(1)) {}
59 	report("stop", 1);
60 }
61 
62 static void test_stop_store_status(void)
63 {
64 	struct cpu *cpu = smp_cpu_from_addr(1);
65 	struct lowcore *lc = (void *)0x0;
66 
67 	report_prefix_push("stop store status");
68 	lc->prefix_sa = 0;
69 	lc->grs_sa[15] = 0;
70 	smp_cpu_stop_store_status(1);
71 	mb();
72 	report("prefix", lc->prefix_sa == (uint32_t)(uintptr_t)cpu->lowcore);
73 	report("stack", lc->grs_sa[15]);
74 	report_prefix_pop();
75 }
76 
77 static void test_store_status(void)
78 {
79 	struct cpu_status *status = alloc_pages(1);
80 	uint32_t r;
81 
82 	report_prefix_push("store status at address");
83 	memset(status, 0, PAGE_SIZE * 2);
84 
85 	report_prefix_push("running");
86 	smp_cpu_restart(1);
87 	sigp(1, SIGP_STORE_STATUS_AT_ADDRESS, (uintptr_t)status, &r);
88 	report("incorrect state", r == SIGP_STATUS_INCORRECT_STATE);
89 	report("status not written", !memcmp(status, (void*)status + PAGE_SIZE, PAGE_SIZE));
90 	report_prefix_pop();
91 
92 	memset(status, 0, PAGE_SIZE);
93 	report_prefix_push("stopped");
94 	smp_cpu_stop(1);
95 	sigp(1, SIGP_STORE_STATUS_AT_ADDRESS, (uintptr_t)status, NULL);
96 	while (!status->prefix) { mb(); }
97 	report("status written", 1);
98 	free_pages(status, PAGE_SIZE * 2);
99 	report_prefix_pop();
100 
101 	report_prefix_pop();
102 }
103 
104 static void ecall(void)
105 {
106 	unsigned long mask;
107 	struct lowcore *lc = (void *)0x0;
108 
109 	expect_ext_int();
110 	ctl_set_bit(0, 13);
111 	mask = extract_psw_mask();
112 	mask |= PSW_MASK_EXT;
113 	load_psw_mask(mask);
114 	testflag = 1;
115 	while (lc->ext_int_code != 0x1202) { mb(); }
116 	report("ecall", 1);
117 	testflag= 1;
118 }
119 
120 static void test_ecall(void)
121 {
122 	struct psw psw;
123 	psw.mask =  extract_psw_mask();
124 	psw.addr = (unsigned long)ecall;
125 
126 	report_prefix_push("ecall");
127 	testflag= 0;
128 	smp_cpu_destroy(1);
129 
130 	smp_cpu_setup(1, psw);
131 	while (!testflag) { mb(); }
132 	testflag= 0;
133 	sigp(1, SIGP_EXTERNAL_CALL, 0, NULL);
134 	while(!testflag) {mb();}
135 	smp_cpu_stop(1);
136 	report_prefix_pop();
137 }
138 
139 static void emcall(void)
140 {
141 	unsigned long mask;
142 	struct lowcore *lc = (void *)0x0;
143 
144 	expect_ext_int();
145 	ctl_set_bit(0, 14);
146 	mask = extract_psw_mask();
147 	mask |= PSW_MASK_EXT;
148 	load_psw_mask(mask);
149 	testflag= 1;
150 	while (lc->ext_int_code != 0x1201) { mb(); }
151 	report("ecall", 1);
152 	testflag = 1;
153 }
154 
155 static void test_emcall(void)
156 {
157 	struct psw psw;
158 	psw.mask =  extract_psw_mask();
159 	psw.addr = (unsigned long)emcall;
160 
161 	report_prefix_push("emcall");
162 	testflag= 0;
163 	smp_cpu_destroy(1);
164 
165 	smp_cpu_setup(1, psw);
166 	while (!testflag) { mb(); }
167 	testflag= 0;
168 	sigp(1, SIGP_EMERGENCY_SIGNAL, 0, NULL);
169 	while(!testflag) { mb(); }
170 	smp_cpu_stop(1);
171 	report_prefix_pop();
172 }
173 
174 static void test_reset_initial(void)
175 {
176 	struct cpu_status *status = alloc_pages(0);
177 	struct psw psw;
178 
179 	psw.mask =  extract_psw_mask();
180 	psw.addr = (unsigned long)test_func;
181 
182 	report_prefix_push("reset initial");
183 	smp_cpu_setup(1, psw);
184 
185 	sigp_retry(1, SIGP_INITIAL_CPU_RESET, 0, NULL);
186 	sigp(1, SIGP_STORE_STATUS_AT_ADDRESS, (uintptr_t)status, NULL);
187 
188 	report_prefix_push("clear");
189 	report("psw", !status->psw.mask && !status->psw.addr);
190 	report("prefix", !status->prefix);
191 	report("fpc", !status->fpc);
192 	report("cpu timer", !status->cputm);
193 	report("todpr", !status->todpr);
194 	report_prefix_pop();
195 
196 	report_prefix_push("initialized");
197 	report("cr0 == 0xE0", status->crs[0] == 0xE0UL);
198 	report("cr14 == 0xC2000000", status->crs[14] == 0xC2000000UL);
199 	report_prefix_pop();
200 
201 	report("cpu stopped", smp_cpu_stopped(1));
202 	free_pages(status, PAGE_SIZE);
203 	report_prefix_pop();
204 }
205 
206 static void test_reset(void)
207 {
208 	struct psw psw;
209 
210 	psw.mask =  extract_psw_mask();
211 	psw.addr = (unsigned long)test_func;
212 
213 	report_prefix_push("cpu reset");
214 	smp_cpu_setup(1, psw);
215 
216 	sigp_retry(1, SIGP_CPU_RESET, 0, NULL);
217 	report("cpu stopped", smp_cpu_stopped(1));
218 	report_prefix_pop();
219 }
220 
221 int main(void)
222 {
223 	report_prefix_push("smp");
224 
225 	if (smp_query_num_cpus() == 1) {
226 		report_skip("need at least 2 cpus for this test");
227 		goto done;
228 	}
229 
230 	test_start();
231 	test_stop();
232 	test_stop_store_status();
233 	test_store_status();
234 	test_ecall();
235 	test_emcall();
236 	test_reset();
237 	test_reset_initial();
238 
239 done:
240 	report_prefix_pop();
241 	return report_summary();
242 }
243