Merge tag 'soc-dt-6.4' of git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc
[linux-block.git] / tools / testing / selftests / kvm / s390x / sync_regs_test.c
CommitLineData
ee1563f4
TH
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"
efaa83a3 23#include "diag318_test_handler.h"
17e48d8a 24#include "kselftest.h"
ee1563f4 25
ee1563f4
TH
26static void guest_code(void)
27{
efec8d21
CB
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 );
ee1563f4
TH
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
6a46fcf9
CB
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
ee1563f4
TH
52static 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
60static void compare_sregs(struct kvm_sregs *left, struct kvm_sync_regs *right)
61{
62 int i;
63
64 for (i = 0; i < 16; i++)
6a46fcf9 65 REG_COMPARE32(acrs[i]);
ee1563f4
TH
66
67 for (i = 0; i < 16; i++)
68 REG_COMPARE(crs[i]);
69}
70
71#undef REG_COMPARE
72
efaa83a3 73#define TEST_SYNC_FIELDS (KVM_SYNC_GPRS|KVM_SYNC_ACRS|KVM_SYNC_CRS|KVM_SYNC_DIAG318)
ee1563f4
TH
74#define INVALID_SYNC_FIELD 0x80000000
75
e5b77cde 76void test_read_invalid(struct kvm_vcpu *vcpu)
ee1563f4 77{
e5b77cde 78 struct kvm_run *run = vcpu->run;
17e48d8a 79 int rv;
ee1563f4 80
81cb736c
TH
81 /* Request reading invalid register set from VCPU. */
82 run->kvm_valid_regs = INVALID_SYNC_FIELD;
768e9a61 83 rv = _vcpu_run(vcpu);
81cb736c
TH
84 TEST_ASSERT(rv < 0 && errno == EINVAL,
85 "Invalid kvm_valid_regs did not cause expected KVM_RUN error: %d\n",
86 rv);
e5b77cde 87 run->kvm_valid_regs = 0;
81cb736c
TH
88
89 run->kvm_valid_regs = INVALID_SYNC_FIELD | TEST_SYNC_FIELDS;
768e9a61 90 rv = _vcpu_run(vcpu);
81cb736c
TH
91 TEST_ASSERT(rv < 0 && errno == EINVAL,
92 "Invalid kvm_valid_regs did not cause expected KVM_RUN error: %d\n",
93 rv);
e5b77cde 94 run->kvm_valid_regs = 0;
17e48d8a
TH
95}
96
e5b77cde 97void test_set_invalid(struct kvm_vcpu *vcpu)
17e48d8a 98{
e5b77cde 99 struct kvm_run *run = vcpu->run;
17e48d8a 100 int rv;
81cb736c
TH
101
102 /* Request setting invalid register set into VCPU. */
103 run->kvm_dirty_regs = INVALID_SYNC_FIELD;
768e9a61 104 rv = _vcpu_run(vcpu);
81cb736c
TH
105 TEST_ASSERT(rv < 0 && errno == EINVAL,
106 "Invalid kvm_dirty_regs did not cause expected KVM_RUN error: %d\n",
107 rv);
e5b77cde 108 run->kvm_dirty_regs = 0;
81cb736c
TH
109
110 run->kvm_dirty_regs = INVALID_SYNC_FIELD | TEST_SYNC_FIELDS;
768e9a61 111 rv = _vcpu_run(vcpu);
81cb736c
TH
112 TEST_ASSERT(rv < 0 && errno == EINVAL,
113 "Invalid kvm_dirty_regs did not cause expected KVM_RUN error: %d\n",
114 rv);
e5b77cde 115 run->kvm_dirty_regs = 0;
17e48d8a
TH
116}
117
e5b77cde 118void test_req_and_verify_all_valid_regs(struct kvm_vcpu *vcpu)
17e48d8a 119{
e5b77cde 120 struct kvm_run *run = vcpu->run;
17e48d8a
TH
121 struct kvm_sregs sregs;
122 struct kvm_regs regs;
123 int rv;
81cb736c 124
ee1563f4
TH
125 /* Request and verify all valid register sets. */
126 run->kvm_valid_regs = TEST_SYNC_FIELDS;
768e9a61 127 rv = _vcpu_run(vcpu);
ee1563f4 128 TEST_ASSERT(rv == 0, "vcpu_run failed: %d\n", rv);
c96f57b0 129 TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_S390_SIEIC);
ee1563f4
TH
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
768e9a61 137 vcpu_regs_get(vcpu, &regs);
ee1563f4
TH
138 compare_regs(&regs, &run->s.regs);
139
768e9a61 140 vcpu_sregs_get(vcpu, &sregs);
ee1563f4 141 compare_sregs(&sregs, &run->s.regs);
17e48d8a
TH
142}
143
e5b77cde 144void test_set_and_verify_various_reg_values(struct kvm_vcpu *vcpu)
17e48d8a 145{
e5b77cde 146 struct kvm_run *run = vcpu->run;
17e48d8a
TH
147 struct kvm_sregs sregs;
148 struct kvm_regs regs;
149 int rv;
ee1563f4
TH
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;
efaa83a3
CW
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
768e9a61 163 rv = _vcpu_run(vcpu);
ee1563f4 164 TEST_ASSERT(rv == 0, "vcpu_run failed: %d\n", rv);
c96f57b0 165 TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_S390_SIEIC);
ee1563f4
TH
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,
6a46fcf9 170 "acr0 sync regs value incorrect 0x%x.",
ee1563f4 171 run->s.regs.acrs[0]);
efaa83a3
CW
172 TEST_ASSERT(run->s.regs.diag318 == get_diag318_info(),
173 "diag318 sync regs value incorrect 0x%llx.",
174 run->s.regs.diag318);
ee1563f4 175
768e9a61 176 vcpu_regs_get(vcpu, &regs);
ee1563f4
TH
177 compare_regs(&regs, &run->s.regs);
178
768e9a61 179 vcpu_sregs_get(vcpu, &sregs);
ee1563f4 180 compare_sregs(&sregs, &run->s.regs);
17e48d8a
TH
181}
182
e5b77cde 183void test_clear_kvm_dirty_regs_bits(struct kvm_vcpu *vcpu)
17e48d8a 184{
e5b77cde 185 struct kvm_run *run = vcpu->run;
17e48d8a 186 int rv;
ee1563f4
TH
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;
efaa83a3 194 run->s.regs.diag318 = 0x4B1D;
768e9a61 195 rv = _vcpu_run(vcpu);
ee1563f4 196 TEST_ASSERT(rv == 0, "vcpu_run failed: %d\n", rv);
c96f57b0 197 TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_S390_SIEIC);
ee1563f4
TH
198 TEST_ASSERT(run->s.regs.gprs[11] != 0xDEADBEEF,
199 "r11 sync regs value incorrect 0x%llx.",
200 run->s.regs.gprs[11]);
efaa83a3
CW
201 TEST_ASSERT(run->s.regs.diag318 != 0x4B1D,
202 "diag318 sync regs value incorrect 0x%llx.",
203 run->s.regs.diag318);
17e48d8a
TH
204}
205
206struct testdef {
207 const char *name;
e5b77cde 208 void (*test)(struct kvm_vcpu *vcpu);
17e48d8a
TH
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
217int main(int argc, char *argv[])
218{
e5b77cde
SC
219 struct kvm_vcpu *vcpu;
220 struct kvm_vm *vm;
17e48d8a
TH
221 int idx;
222
9393cb13 223 TEST_REQUIRE(kvm_has_cap(KVM_CAP_SYNC_REGS));
7ed397d1 224
17e48d8a
TH
225 ksft_print_header();
226
17e48d8a
TH
227 ksft_set_plan(ARRAY_SIZE(testlist));
228
229 /* Create VM */
e5b77cde 230 vm = vm_create_with_one_vcpu(&vcpu, guest_code);
17e48d8a
TH
231
232 for (idx = 0; idx < ARRAY_SIZE(testlist); idx++) {
e5b77cde 233 testlist[idx].test(vcpu);
17e48d8a
TH
234 ksft_test_result_pass("%s\n", testlist[idx].name);
235 }
ee1563f4
TH
236
237 kvm_vm_free(vm);
238
17e48d8a 239 ksft_finished(); /* Print results and exit() accordingly */
ee1563f4 240}