1 // SPDX-License-Identifier: GPL-2.0-only
3 * Test for s390x KVM_CAP_SYNC_REGS
5 * Based on the same test for x86:
6 * Copyright (C) 2018, Google LLC.
9 * Copyright (C) 2019, Red Hat, Inc.
11 * Test expected behavior of the KVM_CAP_SYNC_REGS functionality.
14 #define _GNU_SOURCE /* for program_invocation_short_name */
19 #include <sys/ioctl.h>
21 #include "test_util.h"
23 #include "diag318_test_handler.h"
27 static void guest_code(void)
30 * We embed diag 501 here instead of doing a ucall to avoid that
31 * the compiler has messed with r11 at the time of the ucall.
40 #define REG_COMPARE(reg) \
41 TEST_ASSERT(left->reg == right->reg, \
43 " values did not match: 0x%llx, 0x%llx\n", \
44 left->reg, right->reg)
46 #define REG_COMPARE32(reg) \
47 TEST_ASSERT(left->reg == right->reg, \
49 " values did not match: 0x%x, 0x%x\n", \
50 left->reg, right->reg)
53 static void compare_regs(struct kvm_regs
*left
, struct kvm_sync_regs
*right
)
57 for (i
= 0; i
< 16; i
++)
61 static void compare_sregs(struct kvm_sregs
*left
, struct kvm_sync_regs
*right
)
65 for (i
= 0; i
< 16; i
++)
66 REG_COMPARE32(acrs
[i
]);
68 for (i
= 0; i
< 16; i
++)
74 #define TEST_SYNC_FIELDS (KVM_SYNC_GPRS|KVM_SYNC_ACRS|KVM_SYNC_CRS|KVM_SYNC_DIAG318)
75 #define INVALID_SYNC_FIELD 0x80000000
77 int main(int argc
, char *argv
[])
82 struct kvm_sregs sregs
;
85 /* Tell stdout not to buffer its content */
88 cap
= kvm_check_cap(KVM_CAP_SYNC_REGS
);
90 print_skip("CAP_SYNC_REGS not supported");
95 vm
= vm_create_default(VCPU_ID
, 0, guest_code
);
97 run
= vcpu_state(vm
, VCPU_ID
);
99 /* Request reading invalid register set from VCPU. */
100 run
->kvm_valid_regs
= INVALID_SYNC_FIELD
;
101 rv
= _vcpu_run(vm
, VCPU_ID
);
102 TEST_ASSERT(rv
< 0 && errno
== EINVAL
,
103 "Invalid kvm_valid_regs did not cause expected KVM_RUN error: %d\n",
105 vcpu_state(vm
, VCPU_ID
)->kvm_valid_regs
= 0;
107 run
->kvm_valid_regs
= INVALID_SYNC_FIELD
| TEST_SYNC_FIELDS
;
108 rv
= _vcpu_run(vm
, VCPU_ID
);
109 TEST_ASSERT(rv
< 0 && errno
== EINVAL
,
110 "Invalid kvm_valid_regs did not cause expected KVM_RUN error: %d\n",
112 vcpu_state(vm
, VCPU_ID
)->kvm_valid_regs
= 0;
114 /* Request setting invalid register set into VCPU. */
115 run
->kvm_dirty_regs
= INVALID_SYNC_FIELD
;
116 rv
= _vcpu_run(vm
, VCPU_ID
);
117 TEST_ASSERT(rv
< 0 && errno
== EINVAL
,
118 "Invalid kvm_dirty_regs did not cause expected KVM_RUN error: %d\n",
120 vcpu_state(vm
, VCPU_ID
)->kvm_dirty_regs
= 0;
122 run
->kvm_dirty_regs
= INVALID_SYNC_FIELD
| TEST_SYNC_FIELDS
;
123 rv
= _vcpu_run(vm
, VCPU_ID
);
124 TEST_ASSERT(rv
< 0 && errno
== EINVAL
,
125 "Invalid kvm_dirty_regs did not cause expected KVM_RUN error: %d\n",
127 vcpu_state(vm
, VCPU_ID
)->kvm_dirty_regs
= 0;
129 /* Request and verify all valid register sets. */
130 run
->kvm_valid_regs
= TEST_SYNC_FIELDS
;
131 rv
= _vcpu_run(vm
, VCPU_ID
);
132 TEST_ASSERT(rv
== 0, "vcpu_run failed: %d\n", rv
);
133 TEST_ASSERT(run
->exit_reason
== KVM_EXIT_S390_SIEIC
,
134 "Unexpected exit reason: %u (%s)\n",
136 exit_reason_str(run
->exit_reason
));
137 TEST_ASSERT(run
->s390_sieic
.icptcode
== 4 &&
138 (run
->s390_sieic
.ipa
>> 8) == 0x83 &&
139 (run
->s390_sieic
.ipb
>> 16) == 0x501,
140 "Unexpected interception code: ic=%u, ipa=0x%x, ipb=0x%x\n",
141 run
->s390_sieic
.icptcode
, run
->s390_sieic
.ipa
,
142 run
->s390_sieic
.ipb
);
144 vcpu_regs_get(vm
, VCPU_ID
, ®s
);
145 compare_regs(®s
, &run
->s
.regs
);
147 vcpu_sregs_get(vm
, VCPU_ID
, &sregs
);
148 compare_sregs(&sregs
, &run
->s
.regs
);
150 /* Set and verify various register values */
151 run
->s
.regs
.gprs
[11] = 0xBAD1DEA;
152 run
->s
.regs
.acrs
[0] = 1 << 11;
154 run
->kvm_valid_regs
= TEST_SYNC_FIELDS
;
155 run
->kvm_dirty_regs
= KVM_SYNC_GPRS
| KVM_SYNC_ACRS
;
157 if (get_diag318_info() > 0) {
158 run
->s
.regs
.diag318
= get_diag318_info();
159 run
->kvm_dirty_regs
|= KVM_SYNC_DIAG318
;
162 rv
= _vcpu_run(vm
, VCPU_ID
);
163 TEST_ASSERT(rv
== 0, "vcpu_run failed: %d\n", rv
);
164 TEST_ASSERT(run
->exit_reason
== KVM_EXIT_S390_SIEIC
,
165 "Unexpected exit reason: %u (%s)\n",
167 exit_reason_str(run
->exit_reason
));
168 TEST_ASSERT(run
->s
.regs
.gprs
[11] == 0xBAD1DEA + 1,
169 "r11 sync regs value incorrect 0x%llx.",
170 run
->s
.regs
.gprs
[11]);
171 TEST_ASSERT(run
->s
.regs
.acrs
[0] == 1 << 11,
172 "acr0 sync regs value incorrect 0x%x.",
173 run
->s
.regs
.acrs
[0]);
174 TEST_ASSERT(run
->s
.regs
.diag318
== get_diag318_info(),
175 "diag318 sync regs value incorrect 0x%llx.",
176 run
->s
.regs
.diag318
);
178 vcpu_regs_get(vm
, VCPU_ID
, ®s
);
179 compare_regs(®s
, &run
->s
.regs
);
181 vcpu_sregs_get(vm
, VCPU_ID
, &sregs
);
182 compare_sregs(&sregs
, &run
->s
.regs
);
184 /* Clear kvm_dirty_regs bits, verify new s.regs values are
185 * overwritten with existing guest values.
187 run
->kvm_valid_regs
= TEST_SYNC_FIELDS
;
188 run
->kvm_dirty_regs
= 0;
189 run
->s
.regs
.gprs
[11] = 0xDEADBEEF;
190 run
->s
.regs
.diag318
= 0x4B1D;
191 rv
= _vcpu_run(vm
, VCPU_ID
);
192 TEST_ASSERT(rv
== 0, "vcpu_run failed: %d\n", rv
);
193 TEST_ASSERT(run
->exit_reason
== KVM_EXIT_S390_SIEIC
,
194 "Unexpected exit reason: %u (%s)\n",
196 exit_reason_str(run
->exit_reason
));
197 TEST_ASSERT(run
->s
.regs
.gprs
[11] != 0xDEADBEEF,
198 "r11 sync regs value incorrect 0x%llx.",
199 run
->s
.regs
.gprs
[11]);
200 TEST_ASSERT(run
->s
.regs
.diag318
!= 0x4B1D,
201 "diag318 sync regs value incorrect 0x%llx.",
202 run
->s
.regs
.diag318
);