1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Test for s390x KVM_CAP_SYNC_REGS
4  *
5  * Based on the same test for x86:
6  * Copyright (C) 2018, Google LLC.
7  *
8  * Adaptions for s390x:
9  * Copyright (C) 2019, Red Hat, Inc.
10  *
11  * Test expected behavior of the KVM_CAP_SYNC_REGS functionality.
12  */
13 
14 #define _GNU_SOURCE /* for program_invocation_short_name */
15 #include <fcntl.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <sys/ioctl.h>
20 
21 #include "test_util.h"
22 #include "kvm_util.h"
23 #include "diag318_test_handler.h"
24 #include "kselftest.h"
25 
guest_code(void)26 static void guest_code(void)
27 {
28 	/*
29 	 * We embed diag 501 here instead of doing a ucall to avoid that
30 	 * the compiler has messed with r11 at the time of the ucall.
31 	 */
32 	asm volatile (
33 		"0:	diag 0,0,0x501\n"
34 		"	ahi 11,1\n"
35 		"	j 0b\n"
36 	);
37 }
38 
39 #define REG_COMPARE(reg) \
40 	TEST_ASSERT(left->reg == right->reg, \
41 		    "Register " #reg \
42 		    " values did not match: 0x%llx, 0x%llx\n", \
43 		    left->reg, right->reg)
44 
45 #define REG_COMPARE32(reg) \
46 	TEST_ASSERT(left->reg == right->reg, \
47 		    "Register " #reg \
48 		    " values did not match: 0x%x, 0x%x\n", \
49 		    left->reg, right->reg)
50 
51 
compare_regs(struct kvm_regs * left,struct kvm_sync_regs * right)52 static void compare_regs(struct kvm_regs *left, struct kvm_sync_regs *right)
53 {
54 	int i;
55 
56 	for (i = 0; i < 16; i++)
57 		REG_COMPARE(gprs[i]);
58 }
59 
compare_sregs(struct kvm_sregs * left,struct kvm_sync_regs * right)60 static void compare_sregs(struct kvm_sregs *left, struct kvm_sync_regs *right)
61 {
62 	int i;
63 
64 	for (i = 0; i < 16; i++)
65 		REG_COMPARE32(acrs[i]);
66 
67 	for (i = 0; i < 16; i++)
68 		REG_COMPARE(crs[i]);
69 }
70 
71 #undef REG_COMPARE
72 
73 #define TEST_SYNC_FIELDS   (KVM_SYNC_GPRS|KVM_SYNC_ACRS|KVM_SYNC_CRS|KVM_SYNC_DIAG318)
74 #define INVALID_SYNC_FIELD 0x80000000
75 
test_read_invalid(struct kvm_vcpu * vcpu)76 void test_read_invalid(struct kvm_vcpu *vcpu)
77 {
78 	struct kvm_run *run = vcpu->run;
79 	int rv;
80 
81 	/* Request reading invalid register set from VCPU. */
82 	run->kvm_valid_regs = INVALID_SYNC_FIELD;
83 	rv = _vcpu_run(vcpu);
84 	TEST_ASSERT(rv < 0 && errno == EINVAL,
85 		    "Invalid kvm_valid_regs did not cause expected KVM_RUN error: %d\n",
86 		    rv);
87 	run->kvm_valid_regs = 0;
88 
89 	run->kvm_valid_regs = INVALID_SYNC_FIELD | TEST_SYNC_FIELDS;
90 	rv = _vcpu_run(vcpu);
91 	TEST_ASSERT(rv < 0 && errno == EINVAL,
92 		    "Invalid kvm_valid_regs did not cause expected KVM_RUN error: %d\n",
93 		    rv);
94 	run->kvm_valid_regs = 0;
95 }
96 
test_set_invalid(struct kvm_vcpu * vcpu)97 void test_set_invalid(struct kvm_vcpu *vcpu)
98 {
99 	struct kvm_run *run = vcpu->run;
100 	int rv;
101 
102 	/* Request setting invalid register set into VCPU. */
103 	run->kvm_dirty_regs = INVALID_SYNC_FIELD;
104 	rv = _vcpu_run(vcpu);
105 	TEST_ASSERT(rv < 0 && errno == EINVAL,
106 		    "Invalid kvm_dirty_regs did not cause expected KVM_RUN error: %d\n",
107 		    rv);
108 	run->kvm_dirty_regs = 0;
109 
110 	run->kvm_dirty_regs = INVALID_SYNC_FIELD | TEST_SYNC_FIELDS;
111 	rv = _vcpu_run(vcpu);
112 	TEST_ASSERT(rv < 0 && errno == EINVAL,
113 		    "Invalid kvm_dirty_regs did not cause expected KVM_RUN error: %d\n",
114 		    rv);
115 	run->kvm_dirty_regs = 0;
116 }
117 
test_req_and_verify_all_valid_regs(struct kvm_vcpu * vcpu)118 void test_req_and_verify_all_valid_regs(struct kvm_vcpu *vcpu)
119 {
120 	struct kvm_run *run = vcpu->run;
121 	struct kvm_sregs sregs;
122 	struct kvm_regs regs;
123 	int rv;
124 
125 	/* Request and verify all valid register sets. */
126 	run->kvm_valid_regs = TEST_SYNC_FIELDS;
127 	rv = _vcpu_run(vcpu);
128 	TEST_ASSERT(rv == 0, "vcpu_run failed: %d\n", rv);
129 	TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_S390_SIEIC);
130 	TEST_ASSERT(run->s390_sieic.icptcode == 4 &&
131 		    (run->s390_sieic.ipa >> 8) == 0x83 &&
132 		    (run->s390_sieic.ipb >> 16) == 0x501,
133 		    "Unexpected interception code: ic=%u, ipa=0x%x, ipb=0x%x\n",
134 		    run->s390_sieic.icptcode, run->s390_sieic.ipa,
135 		    run->s390_sieic.ipb);
136 
137 	vcpu_regs_get(vcpu, &regs);
138 	compare_regs(&regs, &run->s.regs);
139 
140 	vcpu_sregs_get(vcpu, &sregs);
141 	compare_sregs(&sregs, &run->s.regs);
142 }
143 
test_set_and_verify_various_reg_values(struct kvm_vcpu * vcpu)144 void test_set_and_verify_various_reg_values(struct kvm_vcpu *vcpu)
145 {
146 	struct kvm_run *run = vcpu->run;
147 	struct kvm_sregs sregs;
148 	struct kvm_regs regs;
149 	int rv;
150 
151 	/* Set and verify various register values */
152 	run->s.regs.gprs[11] = 0xBAD1DEA;
153 	run->s.regs.acrs[0] = 1 << 11;
154 
155 	run->kvm_valid_regs = TEST_SYNC_FIELDS;
156 	run->kvm_dirty_regs = KVM_SYNC_GPRS | KVM_SYNC_ACRS;
157 
158 	if (get_diag318_info() > 0) {
159 		run->s.regs.diag318 = get_diag318_info();
160 		run->kvm_dirty_regs |= KVM_SYNC_DIAG318;
161 	}
162 
163 	rv = _vcpu_run(vcpu);
164 	TEST_ASSERT(rv == 0, "vcpu_run failed: %d\n", rv);
165 	TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_S390_SIEIC);
166 	TEST_ASSERT(run->s.regs.gprs[11] == 0xBAD1DEA + 1,
167 		    "r11 sync regs value incorrect 0x%llx.",
168 		    run->s.regs.gprs[11]);
169 	TEST_ASSERT(run->s.regs.acrs[0]  == 1 << 11,
170 		    "acr0 sync regs value incorrect 0x%x.",
171 		    run->s.regs.acrs[0]);
172 	TEST_ASSERT(run->s.regs.diag318 == get_diag318_info(),
173 		    "diag318 sync regs value incorrect 0x%llx.",
174 		    run->s.regs.diag318);
175 
176 	vcpu_regs_get(vcpu, &regs);
177 	compare_regs(&regs, &run->s.regs);
178 
179 	vcpu_sregs_get(vcpu, &sregs);
180 	compare_sregs(&sregs, &run->s.regs);
181 }
182 
test_clear_kvm_dirty_regs_bits(struct kvm_vcpu * vcpu)183 void test_clear_kvm_dirty_regs_bits(struct kvm_vcpu *vcpu)
184 {
185 	struct kvm_run *run = vcpu->run;
186 	int rv;
187 
188 	/* Clear kvm_dirty_regs bits, verify new s.regs values are
189 	 * overwritten with existing guest values.
190 	 */
191 	run->kvm_valid_regs = TEST_SYNC_FIELDS;
192 	run->kvm_dirty_regs = 0;
193 	run->s.regs.gprs[11] = 0xDEADBEEF;
194 	run->s.regs.diag318 = 0x4B1D;
195 	rv = _vcpu_run(vcpu);
196 	TEST_ASSERT(rv == 0, "vcpu_run failed: %d\n", rv);
197 	TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_S390_SIEIC);
198 	TEST_ASSERT(run->s.regs.gprs[11] != 0xDEADBEEF,
199 		    "r11 sync regs value incorrect 0x%llx.",
200 		    run->s.regs.gprs[11]);
201 	TEST_ASSERT(run->s.regs.diag318 != 0x4B1D,
202 		    "diag318 sync regs value incorrect 0x%llx.",
203 		    run->s.regs.diag318);
204 }
205 
206 struct testdef {
207 	const char *name;
208 	void (*test)(struct kvm_vcpu *vcpu);
209 } testlist[] = {
210 	{ "read invalid", test_read_invalid },
211 	{ "set invalid", test_set_invalid },
212 	{ "request+verify all valid regs", test_req_and_verify_all_valid_regs },
213 	{ "set+verify various regs", test_set_and_verify_various_reg_values },
214 	{ "clear kvm_dirty_regs bits", test_clear_kvm_dirty_regs_bits },
215 };
216 
main(int argc,char * argv[])217 int main(int argc, char *argv[])
218 {
219 	struct kvm_vcpu *vcpu;
220 	struct kvm_vm *vm;
221 	int idx;
222 
223 	TEST_REQUIRE(kvm_has_cap(KVM_CAP_SYNC_REGS));
224 
225 	ksft_print_header();
226 
227 	ksft_set_plan(ARRAY_SIZE(testlist));
228 
229 	/* Create VM */
230 	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
231 
232 	for (idx = 0; idx < ARRAY_SIZE(testlist); idx++) {
233 		testlist[idx].test(vcpu);
234 		ksft_test_result_pass("%s\n", testlist[idx].name);
235 	}
236 
237 	kvm_vm_free(vm);
238 
239 	ksft_finished();	/* Print results and exit() accordingly */
240 }
241