1b25d4cb4SJanosch Frank // SPDX-License-Identifier: GPL-2.0-or-later
2b25d4cb4SJanosch Frank /*
3b25d4cb4SJanosch Frank * Test for s390x CPU resets
4b25d4cb4SJanosch Frank *
5b25d4cb4SJanosch Frank * Copyright (C) 2020, IBM
6b25d4cb4SJanosch Frank */
7b25d4cb4SJanosch Frank
8b25d4cb4SJanosch Frank #include <stdio.h>
9b25d4cb4SJanosch Frank #include <stdlib.h>
10b25d4cb4SJanosch Frank #include <string.h>
11b25d4cb4SJanosch Frank #include <sys/ioctl.h>
12b25d4cb4SJanosch Frank
13b25d4cb4SJanosch Frank #include "test_util.h"
14b25d4cb4SJanosch Frank #include "kvm_util.h"
15b1edf7f1SThomas Huth #include "kselftest.h"
16b25d4cb4SJanosch Frank
17b2ff728bSPierre Morel #define LOCAL_IRQS 32
18b2ff728bSPierre Morel
19371dfb2eSSean Christopherson #define ARBITRARY_NON_ZERO_VCPU_ID 3
20b25d4cb4SJanosch Frank
21371dfb2eSSean Christopherson struct kvm_s390_irq buf[ARBITRARY_NON_ZERO_VCPU_ID + LOCAL_IRQS];
22371dfb2eSSean Christopherson
23b0435a12SChristian Borntraeger static uint8_t regs_null[512];
24b25d4cb4SJanosch Frank
guest_code_initial(void)25b25d4cb4SJanosch Frank static void guest_code_initial(void)
26b25d4cb4SJanosch Frank {
2741cbed5bSChristian Borntraeger /* set several CRs to "safe" value */
2841cbed5bSChristian Borntraeger unsigned long cr2_59 = 0x10; /* enable guarded storage */
2941cbed5bSChristian Borntraeger unsigned long cr8_63 = 0x1; /* monitor mask = 1 */
3041cbed5bSChristian Borntraeger unsigned long cr10 = 1; /* PER START */
3141cbed5bSChristian Borntraeger unsigned long cr11 = -1; /* PER END */
3241cbed5bSChristian Borntraeger
33b25d4cb4SJanosch Frank
34b25d4cb4SJanosch Frank /* Dirty registers */
35b25d4cb4SJanosch Frank asm volatile (
3641cbed5bSChristian Borntraeger " lghi 2,0x11\n" /* Round toward 0 */
3741cbed5bSChristian Borntraeger " sfpc 2\n" /* set fpc to !=0 */
3841cbed5bSChristian Borntraeger " lctlg 2,2,%0\n"
3941cbed5bSChristian Borntraeger " lctlg 8,8,%1\n"
4041cbed5bSChristian Borntraeger " lctlg 10,10,%2\n"
4141cbed5bSChristian Borntraeger " lctlg 11,11,%3\n"
423203a017SChristian Borntraeger /* now clobber some general purpose regs */
433203a017SChristian Borntraeger " llihh 0,0xffff\n"
443203a017SChristian Borntraeger " llihl 1,0x5555\n"
453203a017SChristian Borntraeger " llilh 2,0xaaaa\n"
463203a017SChristian Borntraeger " llill 3,0x0000\n"
473203a017SChristian Borntraeger /* now clobber a floating point reg */
483203a017SChristian Borntraeger " lghi 4,0x1\n"
493203a017SChristian Borntraeger " cdgbr 0,4\n"
503203a017SChristian Borntraeger /* now clobber an access reg */
513203a017SChristian Borntraeger " sar 9,4\n"
523203a017SChristian Borntraeger /* We embed diag 501 here to control register content */
533203a017SChristian Borntraeger " diag 0,0,0x501\n"
543203a017SChristian Borntraeger :
553203a017SChristian Borntraeger : "m" (cr2_59), "m" (cr8_63), "m" (cr10), "m" (cr11)
563203a017SChristian Borntraeger /* no clobber list as this should not return */
573203a017SChristian Borntraeger );
58b25d4cb4SJanosch Frank }
59b25d4cb4SJanosch Frank
test_one_reg(struct kvm_vcpu * vcpu,uint64_t id,uint64_t value)60371dfb2eSSean Christopherson static void test_one_reg(struct kvm_vcpu *vcpu, uint64_t id, uint64_t value)
61b25d4cb4SJanosch Frank {
62b25d4cb4SJanosch Frank uint64_t eval_reg;
63b25d4cb4SJanosch Frank
64768e9a61SSean Christopherson vcpu_get_reg(vcpu, id, &eval_reg);
6553362fe9SChristian Borntraeger TEST_ASSERT(eval_reg == value, "value == 0x%lx", value);
66b25d4cb4SJanosch Frank }
67b25d4cb4SJanosch Frank
assert_noirq(struct kvm_vcpu * vcpu)68371dfb2eSSean Christopherson static void assert_noirq(struct kvm_vcpu *vcpu)
69b2ff728bSPierre Morel {
70b2ff728bSPierre Morel struct kvm_s390_irq_state irq_state;
71b2ff728bSPierre Morel int irqs;
72b2ff728bSPierre Morel
73b2ff728bSPierre Morel irq_state.len = sizeof(buf);
74b2ff728bSPierre Morel irq_state.buf = (unsigned long)buf;
75768e9a61SSean Christopherson irqs = __vcpu_ioctl(vcpu, KVM_S390_GET_IRQ_STATE, &irq_state);
76b2ff728bSPierre Morel /*
77b2ff728bSPierre Morel * irqs contains the number of retrieved interrupts. Any interrupt
78b2ff728bSPierre Morel * (notably, the emergency call interrupt we have injected) should
79b2ff728bSPierre Morel * be cleared by the resets, so this should be 0.
80b2ff728bSPierre Morel */
81*a38125f1SAndrew Jones TEST_ASSERT(irqs >= 0, "Could not fetch IRQs: errno %d", errno);
82b2ff728bSPierre Morel TEST_ASSERT(!irqs, "IRQ pending");
83b2ff728bSPierre Morel }
84b2ff728bSPierre Morel
assert_clear(struct kvm_vcpu * vcpu)85371dfb2eSSean Christopherson static void assert_clear(struct kvm_vcpu *vcpu)
86b25d4cb4SJanosch Frank {
87371dfb2eSSean Christopherson struct kvm_sync_regs *sync_regs = &vcpu->run->s.regs;
88b25d4cb4SJanosch Frank struct kvm_sregs sregs;
89b25d4cb4SJanosch Frank struct kvm_regs regs;
90b25d4cb4SJanosch Frank struct kvm_fpu fpu;
91b25d4cb4SJanosch Frank
92768e9a61SSean Christopherson vcpu_regs_get(vcpu, ®s);
93b25d4cb4SJanosch Frank TEST_ASSERT(!memcmp(®s.gprs, regs_null, sizeof(regs.gprs)), "grs == 0");
94b25d4cb4SJanosch Frank
95768e9a61SSean Christopherson vcpu_sregs_get(vcpu, &sregs);
96b25d4cb4SJanosch Frank TEST_ASSERT(!memcmp(&sregs.acrs, regs_null, sizeof(sregs.acrs)), "acrs == 0");
97b25d4cb4SJanosch Frank
98768e9a61SSean Christopherson vcpu_fpu_get(vcpu, &fpu);
99b25d4cb4SJanosch Frank TEST_ASSERT(!memcmp(&fpu.fprs, regs_null, sizeof(fpu.fprs)), "fprs == 0");
100b0435a12SChristian Borntraeger
101b0435a12SChristian Borntraeger /* sync regs */
102b0435a12SChristian Borntraeger TEST_ASSERT(!memcmp(sync_regs->gprs, regs_null, sizeof(sync_regs->gprs)),
103b0435a12SChristian Borntraeger "gprs0-15 == 0 (sync_regs)");
104b0435a12SChristian Borntraeger
105b0435a12SChristian Borntraeger TEST_ASSERT(!memcmp(sync_regs->acrs, regs_null, sizeof(sync_regs->acrs)),
106b0435a12SChristian Borntraeger "acrs0-15 == 0 (sync_regs)");
107b0435a12SChristian Borntraeger
108b0435a12SChristian Borntraeger TEST_ASSERT(!memcmp(sync_regs->vrs, regs_null, sizeof(sync_regs->vrs)),
109b0435a12SChristian Borntraeger "vrs0-15 == 0 (sync_regs)");
110b25d4cb4SJanosch Frank }
111b25d4cb4SJanosch Frank
assert_initial_noclear(struct kvm_vcpu * vcpu)112371dfb2eSSean Christopherson static void assert_initial_noclear(struct kvm_vcpu *vcpu)
1133203a017SChristian Borntraeger {
114371dfb2eSSean Christopherson struct kvm_sync_regs *sync_regs = &vcpu->run->s.regs;
115371dfb2eSSean Christopherson
1163203a017SChristian Borntraeger TEST_ASSERT(sync_regs->gprs[0] == 0xffff000000000000UL,
1173203a017SChristian Borntraeger "gpr0 == 0xffff000000000000 (sync_regs)");
1183203a017SChristian Borntraeger TEST_ASSERT(sync_regs->gprs[1] == 0x0000555500000000UL,
1193203a017SChristian Borntraeger "gpr1 == 0x0000555500000000 (sync_regs)");
1203203a017SChristian Borntraeger TEST_ASSERT(sync_regs->gprs[2] == 0x00000000aaaa0000UL,
1213203a017SChristian Borntraeger "gpr2 == 0x00000000aaaa0000 (sync_regs)");
1223203a017SChristian Borntraeger TEST_ASSERT(sync_regs->gprs[3] == 0x0000000000000000UL,
1233203a017SChristian Borntraeger "gpr3 == 0x0000000000000000 (sync_regs)");
1243203a017SChristian Borntraeger TEST_ASSERT(sync_regs->fprs[0] == 0x3ff0000000000000UL,
1253203a017SChristian Borntraeger "fpr0 == 0f1 (sync_regs)");
1263203a017SChristian Borntraeger TEST_ASSERT(sync_regs->acrs[9] == 1, "ar9 == 1 (sync_regs)");
1273203a017SChristian Borntraeger }
1283203a017SChristian Borntraeger
assert_initial(struct kvm_vcpu * vcpu)129371dfb2eSSean Christopherson static void assert_initial(struct kvm_vcpu *vcpu)
130b25d4cb4SJanosch Frank {
131371dfb2eSSean Christopherson struct kvm_sync_regs *sync_regs = &vcpu->run->s.regs;
132b25d4cb4SJanosch Frank struct kvm_sregs sregs;
133b25d4cb4SJanosch Frank struct kvm_fpu fpu;
134b25d4cb4SJanosch Frank
135b0435a12SChristian Borntraeger /* KVM_GET_SREGS */
136768e9a61SSean Christopherson vcpu_sregs_get(vcpu, &sregs);
137b0435a12SChristian Borntraeger TEST_ASSERT(sregs.crs[0] == 0xE0UL, "cr0 == 0xE0 (KVM_GET_SREGS)");
138b0435a12SChristian Borntraeger TEST_ASSERT(sregs.crs[14] == 0xC2000000UL,
139b0435a12SChristian Borntraeger "cr14 == 0xC2000000 (KVM_GET_SREGS)");
140b25d4cb4SJanosch Frank TEST_ASSERT(!memcmp(&sregs.crs[1], regs_null, sizeof(sregs.crs[1]) * 12),
141b0435a12SChristian Borntraeger "cr1-13 == 0 (KVM_GET_SREGS)");
142b0435a12SChristian Borntraeger TEST_ASSERT(sregs.crs[15] == 0, "cr15 == 0 (KVM_GET_SREGS)");
143b0435a12SChristian Borntraeger
144b0435a12SChristian Borntraeger /* sync regs */
145b0435a12SChristian Borntraeger TEST_ASSERT(sync_regs->crs[0] == 0xE0UL, "cr0 == 0xE0 (sync_regs)");
146b0435a12SChristian Borntraeger TEST_ASSERT(sync_regs->crs[14] == 0xC2000000UL,
147b0435a12SChristian Borntraeger "cr14 == 0xC2000000 (sync_regs)");
148b0435a12SChristian Borntraeger TEST_ASSERT(!memcmp(&sync_regs->crs[1], regs_null, 8 * 12),
149b0435a12SChristian Borntraeger "cr1-13 == 0 (sync_regs)");
150b0435a12SChristian Borntraeger TEST_ASSERT(sync_regs->crs[15] == 0, "cr15 == 0 (sync_regs)");
151b0435a12SChristian Borntraeger TEST_ASSERT(sync_regs->fpc == 0, "fpc == 0 (sync_regs)");
152b0435a12SChristian Borntraeger TEST_ASSERT(sync_regs->todpr == 0, "todpr == 0 (sync_regs)");
153b0435a12SChristian Borntraeger TEST_ASSERT(sync_regs->cputm == 0, "cputm == 0 (sync_regs)");
154b0435a12SChristian Borntraeger TEST_ASSERT(sync_regs->ckc == 0, "ckc == 0 (sync_regs)");
155b0435a12SChristian Borntraeger TEST_ASSERT(sync_regs->pp == 0, "pp == 0 (sync_regs)");
156b0435a12SChristian Borntraeger TEST_ASSERT(sync_regs->gbea == 1, "gbea == 1 (sync_regs)");
157b0435a12SChristian Borntraeger
158b0435a12SChristian Borntraeger /* kvm_run */
159371dfb2eSSean Christopherson TEST_ASSERT(vcpu->run->psw_addr == 0, "psw_addr == 0 (kvm_run)");
160371dfb2eSSean Christopherson TEST_ASSERT(vcpu->run->psw_mask == 0, "psw_mask == 0 (kvm_run)");
161b25d4cb4SJanosch Frank
162768e9a61SSean Christopherson vcpu_fpu_get(vcpu, &fpu);
163b25d4cb4SJanosch Frank TEST_ASSERT(!fpu.fpc, "fpc == 0");
164b25d4cb4SJanosch Frank
165371dfb2eSSean Christopherson test_one_reg(vcpu, KVM_REG_S390_GBEA, 1);
166371dfb2eSSean Christopherson test_one_reg(vcpu, KVM_REG_S390_PP, 0);
167371dfb2eSSean Christopherson test_one_reg(vcpu, KVM_REG_S390_TODPR, 0);
168371dfb2eSSean Christopherson test_one_reg(vcpu, KVM_REG_S390_CPU_TIMER, 0);
169371dfb2eSSean Christopherson test_one_reg(vcpu, KVM_REG_S390_CLOCK_COMP, 0);
170b25d4cb4SJanosch Frank }
171b25d4cb4SJanosch Frank
assert_normal_noclear(struct kvm_vcpu * vcpu)172371dfb2eSSean Christopherson static void assert_normal_noclear(struct kvm_vcpu *vcpu)
1733203a017SChristian Borntraeger {
174371dfb2eSSean Christopherson struct kvm_sync_regs *sync_regs = &vcpu->run->s.regs;
175371dfb2eSSean Christopherson
1763203a017SChristian Borntraeger TEST_ASSERT(sync_regs->crs[2] == 0x10, "cr2 == 10 (sync_regs)");
1773203a017SChristian Borntraeger TEST_ASSERT(sync_regs->crs[8] == 1, "cr10 == 1 (sync_regs)");
1783203a017SChristian Borntraeger TEST_ASSERT(sync_regs->crs[10] == 1, "cr10 == 1 (sync_regs)");
1793203a017SChristian Borntraeger TEST_ASSERT(sync_regs->crs[11] == -1, "cr11 == -1 (sync_regs)");
1803203a017SChristian Borntraeger }
1813203a017SChristian Borntraeger
assert_normal(struct kvm_vcpu * vcpu)182371dfb2eSSean Christopherson static void assert_normal(struct kvm_vcpu *vcpu)
183b25d4cb4SJanosch Frank {
184371dfb2eSSean Christopherson test_one_reg(vcpu, KVM_REG_S390_PFTOKEN, KVM_S390_PFAULT_TOKEN_INVALID);
185371dfb2eSSean Christopherson TEST_ASSERT(vcpu->run->s.regs.pft == KVM_S390_PFAULT_TOKEN_INVALID,
186b0435a12SChristian Borntraeger "pft == 0xff..... (sync_regs)");
187371dfb2eSSean Christopherson assert_noirq(vcpu);
188b2ff728bSPierre Morel }
189b2ff728bSPierre Morel
inject_irq(struct kvm_vcpu * vcpu)190371dfb2eSSean Christopherson static void inject_irq(struct kvm_vcpu *vcpu)
191b2ff728bSPierre Morel {
192b2ff728bSPierre Morel struct kvm_s390_irq_state irq_state;
193b2ff728bSPierre Morel struct kvm_s390_irq *irq = &buf[0];
194b2ff728bSPierre Morel int irqs;
195b2ff728bSPierre Morel
196b2ff728bSPierre Morel /* Inject IRQ */
197b2ff728bSPierre Morel irq_state.len = sizeof(struct kvm_s390_irq);
198b2ff728bSPierre Morel irq_state.buf = (unsigned long)buf;
199b2ff728bSPierre Morel irq->type = KVM_S390_INT_EMERGENCY;
200371dfb2eSSean Christopherson irq->u.emerg.code = vcpu->id;
201768e9a61SSean Christopherson irqs = __vcpu_ioctl(vcpu, KVM_S390_SET_IRQ_STATE, &irq_state);
202*a38125f1SAndrew Jones TEST_ASSERT(irqs >= 0, "Error injecting EMERGENCY IRQ errno %d", errno);
203b25d4cb4SJanosch Frank }
204b25d4cb4SJanosch Frank
create_vm(struct kvm_vcpu ** vcpu)205371dfb2eSSean Christopherson static struct kvm_vm *create_vm(struct kvm_vcpu **vcpu)
206371dfb2eSSean Christopherson {
207371dfb2eSSean Christopherson struct kvm_vm *vm;
208371dfb2eSSean Christopherson
2096e1d13bfSSean Christopherson vm = vm_create(1);
210371dfb2eSSean Christopherson
211371dfb2eSSean Christopherson *vcpu = vm_vcpu_add(vm, ARBITRARY_NON_ZERO_VCPU_ID, guest_code_initial);
212371dfb2eSSean Christopherson
213371dfb2eSSean Christopherson return vm;
214371dfb2eSSean Christopherson }
215371dfb2eSSean Christopherson
test_normal(void)216b25d4cb4SJanosch Frank static void test_normal(void)
217b25d4cb4SJanosch Frank {
218371dfb2eSSean Christopherson struct kvm_vcpu *vcpu;
219371dfb2eSSean Christopherson struct kvm_vm *vm;
220371dfb2eSSean Christopherson
221b1edf7f1SThomas Huth ksft_print_msg("Testing normal reset\n");
222371dfb2eSSean Christopherson vm = create_vm(&vcpu);
223b25d4cb4SJanosch Frank
224768e9a61SSean Christopherson vcpu_run(vcpu);
225b25d4cb4SJanosch Frank
226371dfb2eSSean Christopherson inject_irq(vcpu);
227b2ff728bSPierre Morel
228fcba483eSSean Christopherson vcpu_ioctl(vcpu, KVM_S390_NORMAL_RESET, NULL);
2293203a017SChristian Borntraeger
2303203a017SChristian Borntraeger /* must clears */
231371dfb2eSSean Christopherson assert_normal(vcpu);
2323203a017SChristian Borntraeger /* must not clears */
233371dfb2eSSean Christopherson assert_normal_noclear(vcpu);
234371dfb2eSSean Christopherson assert_initial_noclear(vcpu);
2353203a017SChristian Borntraeger
236b25d4cb4SJanosch Frank kvm_vm_free(vm);
237b25d4cb4SJanosch Frank }
238b25d4cb4SJanosch Frank
test_initial(void)239b25d4cb4SJanosch Frank static void test_initial(void)
240b25d4cb4SJanosch Frank {
241371dfb2eSSean Christopherson struct kvm_vcpu *vcpu;
242371dfb2eSSean Christopherson struct kvm_vm *vm;
243371dfb2eSSean Christopherson
244b1edf7f1SThomas Huth ksft_print_msg("Testing initial reset\n");
245371dfb2eSSean Christopherson vm = create_vm(&vcpu);
246b25d4cb4SJanosch Frank
247768e9a61SSean Christopherson vcpu_run(vcpu);
248b25d4cb4SJanosch Frank
249371dfb2eSSean Christopherson inject_irq(vcpu);
250b2ff728bSPierre Morel
251fcba483eSSean Christopherson vcpu_ioctl(vcpu, KVM_S390_INITIAL_RESET, NULL);
2523203a017SChristian Borntraeger
2533203a017SChristian Borntraeger /* must clears */
254371dfb2eSSean Christopherson assert_normal(vcpu);
255371dfb2eSSean Christopherson assert_initial(vcpu);
2563203a017SChristian Borntraeger /* must not clears */
257371dfb2eSSean Christopherson assert_initial_noclear(vcpu);
2583203a017SChristian Borntraeger
259b25d4cb4SJanosch Frank kvm_vm_free(vm);
260b25d4cb4SJanosch Frank }
261b25d4cb4SJanosch Frank
test_clear(void)262b25d4cb4SJanosch Frank static void test_clear(void)
263b25d4cb4SJanosch Frank {
264371dfb2eSSean Christopherson struct kvm_vcpu *vcpu;
265371dfb2eSSean Christopherson struct kvm_vm *vm;
266371dfb2eSSean Christopherson
267b1edf7f1SThomas Huth ksft_print_msg("Testing clear reset\n");
268371dfb2eSSean Christopherson vm = create_vm(&vcpu);
269b25d4cb4SJanosch Frank
270768e9a61SSean Christopherson vcpu_run(vcpu);
271b25d4cb4SJanosch Frank
272371dfb2eSSean Christopherson inject_irq(vcpu);
273b2ff728bSPierre Morel
274fcba483eSSean Christopherson vcpu_ioctl(vcpu, KVM_S390_CLEAR_RESET, NULL);
2753203a017SChristian Borntraeger
2763203a017SChristian Borntraeger /* must clears */
277371dfb2eSSean Christopherson assert_normal(vcpu);
278371dfb2eSSean Christopherson assert_initial(vcpu);
279371dfb2eSSean Christopherson assert_clear(vcpu);
2803203a017SChristian Borntraeger
281b25d4cb4SJanosch Frank kvm_vm_free(vm);
282b25d4cb4SJanosch Frank }
283b25d4cb4SJanosch Frank
284b1edf7f1SThomas Huth struct testdef {
285b1edf7f1SThomas Huth const char *name;
286b1edf7f1SThomas Huth void (*test)(void);
287b1edf7f1SThomas Huth bool needs_cap;
288b1edf7f1SThomas Huth } testlist[] = {
289b1edf7f1SThomas Huth { "initial", test_initial, false },
290b1edf7f1SThomas Huth { "normal", test_normal, true },
291b1edf7f1SThomas Huth { "clear", test_clear, true },
292b1edf7f1SThomas Huth };
293b1edf7f1SThomas Huth
main(int argc,char * argv[])294b25d4cb4SJanosch Frank int main(int argc, char *argv[])
295b25d4cb4SJanosch Frank {
296b1edf7f1SThomas Huth bool has_s390_vcpu_resets = kvm_check_cap(KVM_CAP_S390_VCPU_RESETS);
297b1edf7f1SThomas Huth int idx;
298b1edf7f1SThomas Huth
299b1edf7f1SThomas Huth ksft_print_header();
300b1edf7f1SThomas Huth ksft_set_plan(ARRAY_SIZE(testlist));
301b1edf7f1SThomas Huth
302b1edf7f1SThomas Huth for (idx = 0; idx < ARRAY_SIZE(testlist); idx++) {
303b1edf7f1SThomas Huth if (!testlist[idx].needs_cap || has_s390_vcpu_resets) {
304b1edf7f1SThomas Huth testlist[idx].test();
305b1edf7f1SThomas Huth ksft_test_result_pass("%s\n", testlist[idx].name);
306b1edf7f1SThomas Huth } else {
307b1edf7f1SThomas Huth ksft_test_result_skip("%s - no VCPU_RESETS capability\n",
308b1edf7f1SThomas Huth testlist[idx].name);
309b25d4cb4SJanosch Frank }
310b1edf7f1SThomas Huth }
311b1edf7f1SThomas Huth
312b1edf7f1SThomas Huth ksft_finished(); /* Print results and exit() accordingly */
313b25d4cb4SJanosch Frank }
314