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 24 #define VCPU_ID 5 25 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 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 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) 74 #define INVALID_SYNC_FIELD 0x80000000 75 76 int main(int argc, char *argv[]) 77 { 78 struct kvm_vm *vm; 79 struct kvm_run *run; 80 struct kvm_regs regs; 81 struct kvm_sregs sregs; 82 int rv, cap; 83 84 /* Tell stdout not to buffer its content */ 85 setbuf(stdout, NULL); 86 87 cap = kvm_check_cap(KVM_CAP_SYNC_REGS); 88 if (!cap) { 89 print_skip("CAP_SYNC_REGS not supported"); 90 exit(KSFT_SKIP); 91 } 92 93 /* Create VM */ 94 vm = vm_create_default(VCPU_ID, 0, guest_code); 95 96 run = vcpu_state(vm, VCPU_ID); 97 98 /* Request reading invalid register set from VCPU. */ 99 run->kvm_valid_regs = INVALID_SYNC_FIELD; 100 rv = _vcpu_run(vm, VCPU_ID); 101 TEST_ASSERT(rv < 0 && errno == EINVAL, 102 "Invalid kvm_valid_regs did not cause expected KVM_RUN error: %d\n", 103 rv); 104 vcpu_state(vm, VCPU_ID)->kvm_valid_regs = 0; 105 106 run->kvm_valid_regs = INVALID_SYNC_FIELD | TEST_SYNC_FIELDS; 107 rv = _vcpu_run(vm, VCPU_ID); 108 TEST_ASSERT(rv < 0 && errno == EINVAL, 109 "Invalid kvm_valid_regs did not cause expected KVM_RUN error: %d\n", 110 rv); 111 vcpu_state(vm, VCPU_ID)->kvm_valid_regs = 0; 112 113 /* Request setting invalid register set into VCPU. */ 114 run->kvm_dirty_regs = INVALID_SYNC_FIELD; 115 rv = _vcpu_run(vm, VCPU_ID); 116 TEST_ASSERT(rv < 0 && errno == EINVAL, 117 "Invalid kvm_dirty_regs did not cause expected KVM_RUN error: %d\n", 118 rv); 119 vcpu_state(vm, VCPU_ID)->kvm_dirty_regs = 0; 120 121 run->kvm_dirty_regs = INVALID_SYNC_FIELD | TEST_SYNC_FIELDS; 122 rv = _vcpu_run(vm, VCPU_ID); 123 TEST_ASSERT(rv < 0 && errno == EINVAL, 124 "Invalid kvm_dirty_regs did not cause expected KVM_RUN error: %d\n", 125 rv); 126 vcpu_state(vm, VCPU_ID)->kvm_dirty_regs = 0; 127 128 /* Request and verify all valid register sets. */ 129 run->kvm_valid_regs = TEST_SYNC_FIELDS; 130 rv = _vcpu_run(vm, VCPU_ID); 131 TEST_ASSERT(rv == 0, "vcpu_run failed: %d\n", rv); 132 TEST_ASSERT(run->exit_reason == KVM_EXIT_S390_SIEIC, 133 "Unexpected exit reason: %u (%s)\n", 134 run->exit_reason, 135 exit_reason_str(run->exit_reason)); 136 TEST_ASSERT(run->s390_sieic.icptcode == 4 && 137 (run->s390_sieic.ipa >> 8) == 0x83 && 138 (run->s390_sieic.ipb >> 16) == 0x501, 139 "Unexpected interception code: ic=%u, ipa=0x%x, ipb=0x%x\n", 140 run->s390_sieic.icptcode, run->s390_sieic.ipa, 141 run->s390_sieic.ipb); 142 143 vcpu_regs_get(vm, VCPU_ID, ®s); 144 compare_regs(®s, &run->s.regs); 145 146 vcpu_sregs_get(vm, VCPU_ID, &sregs); 147 compare_sregs(&sregs, &run->s.regs); 148 149 /* Set and verify various register values */ 150 run->s.regs.gprs[11] = 0xBAD1DEA; 151 run->s.regs.acrs[0] = 1 << 11; 152 153 run->kvm_valid_regs = TEST_SYNC_FIELDS; 154 run->kvm_dirty_regs = KVM_SYNC_GPRS | KVM_SYNC_ACRS; 155 rv = _vcpu_run(vm, VCPU_ID); 156 TEST_ASSERT(rv == 0, "vcpu_run failed: %d\n", rv); 157 TEST_ASSERT(run->exit_reason == KVM_EXIT_S390_SIEIC, 158 "Unexpected exit reason: %u (%s)\n", 159 run->exit_reason, 160 exit_reason_str(run->exit_reason)); 161 TEST_ASSERT(run->s.regs.gprs[11] == 0xBAD1DEA + 1, 162 "r11 sync regs value incorrect 0x%llx.", 163 run->s.regs.gprs[11]); 164 TEST_ASSERT(run->s.regs.acrs[0] == 1 << 11, 165 "acr0 sync regs value incorrect 0x%x.", 166 run->s.regs.acrs[0]); 167 168 vcpu_regs_get(vm, VCPU_ID, ®s); 169 compare_regs(®s, &run->s.regs); 170 171 vcpu_sregs_get(vm, VCPU_ID, &sregs); 172 compare_sregs(&sregs, &run->s.regs); 173 174 /* Clear kvm_dirty_regs bits, verify new s.regs values are 175 * overwritten with existing guest values. 176 */ 177 run->kvm_valid_regs = TEST_SYNC_FIELDS; 178 run->kvm_dirty_regs = 0; 179 run->s.regs.gprs[11] = 0xDEADBEEF; 180 rv = _vcpu_run(vm, VCPU_ID); 181 TEST_ASSERT(rv == 0, "vcpu_run failed: %d\n", rv); 182 TEST_ASSERT(run->exit_reason == KVM_EXIT_S390_SIEIC, 183 "Unexpected exit reason: %u (%s)\n", 184 run->exit_reason, 185 exit_reason_str(run->exit_reason)); 186 TEST_ASSERT(run->s.regs.gprs[11] != 0xDEADBEEF, 187 "r11 sync regs value incorrect 0x%llx.", 188 run->s.regs.gprs[11]); 189 190 kvm_vm_free(vm); 191 192 return 0; 193 } 194