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(run->exit_reason == KVM_EXIT_S390_SIEIC,
130 "Unexpected exit reason: %u (%s)\n",
131 run->exit_reason,
132 exit_reason_str(run->exit_reason));
133 TEST_ASSERT(run->s390_sieic.icptcode == 4 &&
134 (run->s390_sieic.ipa >> 8) == 0x83 &&
135 (run->s390_sieic.ipb >> 16) == 0x501,
136 "Unexpected interception code: ic=%u, ipa=0x%x, ipb=0x%x\n",
137 run->s390_sieic.icptcode, run->s390_sieic.ipa,
138 run->s390_sieic.ipb);
139
140 vcpu_regs_get(vcpu, ®s);
141 compare_regs(®s, &run->s.regs);
142
143 vcpu_sregs_get(vcpu, &sregs);
144 compare_sregs(&sregs, &run->s.regs);
145 }
146
test_set_and_verify_various_reg_values(struct kvm_vcpu * vcpu)147 void test_set_and_verify_various_reg_values(struct kvm_vcpu *vcpu)
148 {
149 struct kvm_run *run = vcpu->run;
150 struct kvm_sregs sregs;
151 struct kvm_regs regs;
152 int rv;
153
154 /* Set and verify various register values */
155 run->s.regs.gprs[11] = 0xBAD1DEA;
156 run->s.regs.acrs[0] = 1 << 11;
157
158 run->kvm_valid_regs = TEST_SYNC_FIELDS;
159 run->kvm_dirty_regs = KVM_SYNC_GPRS | KVM_SYNC_ACRS;
160
161 if (get_diag318_info() > 0) {
162 run->s.regs.diag318 = get_diag318_info();
163 run->kvm_dirty_regs |= KVM_SYNC_DIAG318;
164 }
165
166 rv = _vcpu_run(vcpu);
167 TEST_ASSERT(rv == 0, "vcpu_run failed: %d\n", rv);
168 TEST_ASSERT(run->exit_reason == KVM_EXIT_S390_SIEIC,
169 "Unexpected exit reason: %u (%s)\n",
170 run->exit_reason,
171 exit_reason_str(run->exit_reason));
172 TEST_ASSERT(run->s.regs.gprs[11] == 0xBAD1DEA + 1,
173 "r11 sync regs value incorrect 0x%llx.",
174 run->s.regs.gprs[11]);
175 TEST_ASSERT(run->s.regs.acrs[0] == 1 << 11,
176 "acr0 sync regs value incorrect 0x%x.",
177 run->s.regs.acrs[0]);
178 TEST_ASSERT(run->s.regs.diag318 == get_diag318_info(),
179 "diag318 sync regs value incorrect 0x%llx.",
180 run->s.regs.diag318);
181
182 vcpu_regs_get(vcpu, ®s);
183 compare_regs(®s, &run->s.regs);
184
185 vcpu_sregs_get(vcpu, &sregs);
186 compare_sregs(&sregs, &run->s.regs);
187 }
188
test_clear_kvm_dirty_regs_bits(struct kvm_vcpu * vcpu)189 void test_clear_kvm_dirty_regs_bits(struct kvm_vcpu *vcpu)
190 {
191 struct kvm_run *run = vcpu->run;
192 int rv;
193
194 /* Clear kvm_dirty_regs bits, verify new s.regs values are
195 * overwritten with existing guest values.
196 */
197 run->kvm_valid_regs = TEST_SYNC_FIELDS;
198 run->kvm_dirty_regs = 0;
199 run->s.regs.gprs[11] = 0xDEADBEEF;
200 run->s.regs.diag318 = 0x4B1D;
201 rv = _vcpu_run(vcpu);
202 TEST_ASSERT(rv == 0, "vcpu_run failed: %d\n", rv);
203 TEST_ASSERT(run->exit_reason == KVM_EXIT_S390_SIEIC,
204 "Unexpected exit reason: %u (%s)\n",
205 run->exit_reason,
206 exit_reason_str(run->exit_reason));
207 TEST_ASSERT(run->s.regs.gprs[11] != 0xDEADBEEF,
208 "r11 sync regs value incorrect 0x%llx.",
209 run->s.regs.gprs[11]);
210 TEST_ASSERT(run->s.regs.diag318 != 0x4B1D,
211 "diag318 sync regs value incorrect 0x%llx.",
212 run->s.regs.diag318);
213 }
214
215 struct testdef {
216 const char *name;
217 void (*test)(struct kvm_vcpu *vcpu);
218 } testlist[] = {
219 { "read invalid", test_read_invalid },
220 { "set invalid", test_set_invalid },
221 { "request+verify all valid regs", test_req_and_verify_all_valid_regs },
222 { "set+verify various regs", test_set_and_verify_various_reg_values },
223 { "clear kvm_dirty_regs bits", test_clear_kvm_dirty_regs_bits },
224 };
225
main(int argc,char * argv[])226 int main(int argc, char *argv[])
227 {
228 struct kvm_vcpu *vcpu;
229 struct kvm_vm *vm;
230 int idx;
231
232 TEST_REQUIRE(kvm_has_cap(KVM_CAP_SYNC_REGS));
233
234 ksft_print_header();
235
236 ksft_set_plan(ARRAY_SIZE(testlist));
237
238 /* Create VM */
239 vm = vm_create_with_one_vcpu(&vcpu, guest_code);
240
241 for (idx = 0; idx < ARRAY_SIZE(testlist); idx++) {
242 testlist[idx].test(vcpu);
243 ksft_test_result_pass("%s\n", testlist[idx].name);
244 }
245
246 kvm_vm_free(vm);
247
248 ksft_finished(); /* Print results and exit() accordingly */
249 }
250