1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Test for x86 KVM_CAP_SYNC_REGS 4 * 5 * Copyright (C) 2018, Google LLC. 6 * 7 * Verifies expected behavior of x86 KVM_CAP_SYNC_REGS functionality, 8 * including requesting an invalid register set, updates to/from values 9 * in kvm_run.s.regs when kvm_valid_regs and kvm_dirty_regs are toggled. 10 */ 11 12 #define _GNU_SOURCE /* for program_invocation_short_name */ 13 #include <fcntl.h> 14 #include <stdio.h> 15 #include <stdlib.h> 16 #include <string.h> 17 #include <sys/ioctl.h> 18 19 #include "test_util.h" 20 #include "kvm_util.h" 21 #include "processor.h" 22 23 #define UCALL_PIO_PORT ((uint16_t)0x1000) 24 25 struct ucall uc_none = { 26 .cmd = UCALL_NONE, 27 }; 28 29 /* 30 * ucall is embedded here to protect against compiler reshuffling registers 31 * before calling a function. In this test we only need to get KVM_EXIT_IO 32 * vmexit and preserve RBX, no additional information is needed. 33 */ 34 void guest_code(void) 35 { 36 asm volatile("1: in %[port], %%al\n" 37 "add $0x1, %%rbx\n" 38 "jmp 1b" 39 : : [port] "d" (UCALL_PIO_PORT), "D" (&uc_none) 40 : "rax", "rbx"); 41 } 42 43 static void compare_regs(struct kvm_regs *left, struct kvm_regs *right) 44 { 45 #define REG_COMPARE(reg) \ 46 TEST_ASSERT(left->reg == right->reg, \ 47 "Register " #reg \ 48 " values did not match: 0x%llx, 0x%llx\n", \ 49 left->reg, right->reg) 50 REG_COMPARE(rax); 51 REG_COMPARE(rbx); 52 REG_COMPARE(rcx); 53 REG_COMPARE(rdx); 54 REG_COMPARE(rsi); 55 REG_COMPARE(rdi); 56 REG_COMPARE(rsp); 57 REG_COMPARE(rbp); 58 REG_COMPARE(r8); 59 REG_COMPARE(r9); 60 REG_COMPARE(r10); 61 REG_COMPARE(r11); 62 REG_COMPARE(r12); 63 REG_COMPARE(r13); 64 REG_COMPARE(r14); 65 REG_COMPARE(r15); 66 REG_COMPARE(rip); 67 REG_COMPARE(rflags); 68 #undef REG_COMPARE 69 } 70 71 static void compare_sregs(struct kvm_sregs *left, struct kvm_sregs *right) 72 { 73 } 74 75 static void compare_vcpu_events(struct kvm_vcpu_events *left, 76 struct kvm_vcpu_events *right) 77 { 78 } 79 80 #define TEST_SYNC_FIELDS (KVM_SYNC_X86_REGS|KVM_SYNC_X86_SREGS|KVM_SYNC_X86_EVENTS) 81 #define INVALID_SYNC_FIELD 0x80000000 82 83 int main(int argc, char *argv[]) 84 { 85 struct kvm_vcpu *vcpu; 86 struct kvm_vm *vm; 87 struct kvm_run *run; 88 struct kvm_regs regs; 89 struct kvm_sregs sregs; 90 struct kvm_vcpu_events events; 91 int rv, cap; 92 93 cap = kvm_check_cap(KVM_CAP_SYNC_REGS); 94 TEST_REQUIRE((cap & TEST_SYNC_FIELDS) == TEST_SYNC_FIELDS); 95 TEST_REQUIRE(!(cap & INVALID_SYNC_FIELD)); 96 97 vm = vm_create_with_one_vcpu(&vcpu, guest_code); 98 99 run = vcpu->run; 100 101 /* Request reading invalid register set from VCPU. */ 102 run->kvm_valid_regs = INVALID_SYNC_FIELD; 103 rv = _vcpu_run(vcpu); 104 TEST_ASSERT(rv < 0 && errno == EINVAL, 105 "Invalid kvm_valid_regs did not cause expected KVM_RUN error: %d\n", 106 rv); 107 run->kvm_valid_regs = 0; 108 109 run->kvm_valid_regs = INVALID_SYNC_FIELD | TEST_SYNC_FIELDS; 110 rv = _vcpu_run(vcpu); 111 TEST_ASSERT(rv < 0 && errno == EINVAL, 112 "Invalid kvm_valid_regs did not cause expected KVM_RUN error: %d\n", 113 rv); 114 run->kvm_valid_regs = 0; 115 116 /* Request setting invalid register set into VCPU. */ 117 run->kvm_dirty_regs = INVALID_SYNC_FIELD; 118 rv = _vcpu_run(vcpu); 119 TEST_ASSERT(rv < 0 && errno == EINVAL, 120 "Invalid kvm_dirty_regs did not cause expected KVM_RUN error: %d\n", 121 rv); 122 run->kvm_dirty_regs = 0; 123 124 run->kvm_dirty_regs = INVALID_SYNC_FIELD | TEST_SYNC_FIELDS; 125 rv = _vcpu_run(vcpu); 126 TEST_ASSERT(rv < 0 && errno == EINVAL, 127 "Invalid kvm_dirty_regs did not cause expected KVM_RUN error: %d\n", 128 rv); 129 run->kvm_dirty_regs = 0; 130 131 /* Request and verify all valid register sets. */ 132 /* TODO: BUILD TIME CHECK: TEST_ASSERT(KVM_SYNC_X86_NUM_FIELDS != 3); */ 133 run->kvm_valid_regs = TEST_SYNC_FIELDS; 134 rv = _vcpu_run(vcpu); 135 TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO); 136 137 vcpu_regs_get(vcpu, ®s); 138 compare_regs(®s, &run->s.regs.regs); 139 140 vcpu_sregs_get(vcpu, &sregs); 141 compare_sregs(&sregs, &run->s.regs.sregs); 142 143 vcpu_events_get(vcpu, &events); 144 compare_vcpu_events(&events, &run->s.regs.events); 145 146 /* Set and verify various register values. */ 147 run->s.regs.regs.rbx = 0xBAD1DEA; 148 run->s.regs.sregs.apic_base = 1 << 11; 149 /* TODO run->s.regs.events.XYZ = ABC; */ 150 151 run->kvm_valid_regs = TEST_SYNC_FIELDS; 152 run->kvm_dirty_regs = KVM_SYNC_X86_REGS | KVM_SYNC_X86_SREGS; 153 rv = _vcpu_run(vcpu); 154 TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO); 155 TEST_ASSERT(run->s.regs.regs.rbx == 0xBAD1DEA + 1, 156 "rbx sync regs value incorrect 0x%llx.", 157 run->s.regs.regs.rbx); 158 TEST_ASSERT(run->s.regs.sregs.apic_base == 1 << 11, 159 "apic_base sync regs value incorrect 0x%llx.", 160 run->s.regs.sregs.apic_base); 161 162 vcpu_regs_get(vcpu, ®s); 163 compare_regs(®s, &run->s.regs.regs); 164 165 vcpu_sregs_get(vcpu, &sregs); 166 compare_sregs(&sregs, &run->s.regs.sregs); 167 168 vcpu_events_get(vcpu, &events); 169 compare_vcpu_events(&events, &run->s.regs.events); 170 171 /* Clear kvm_dirty_regs bits, verify new s.regs values are 172 * overwritten with existing guest values. 173 */ 174 run->kvm_valid_regs = TEST_SYNC_FIELDS; 175 run->kvm_dirty_regs = 0; 176 run->s.regs.regs.rbx = 0xDEADBEEF; 177 rv = _vcpu_run(vcpu); 178 TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO); 179 TEST_ASSERT(run->s.regs.regs.rbx != 0xDEADBEEF, 180 "rbx sync regs value incorrect 0x%llx.", 181 run->s.regs.regs.rbx); 182 183 /* Clear kvm_valid_regs bits and kvm_dirty_bits. 184 * Verify s.regs values are not overwritten with existing guest values 185 * and that guest values are not overwritten with kvm_sync_regs values. 186 */ 187 run->kvm_valid_regs = 0; 188 run->kvm_dirty_regs = 0; 189 run->s.regs.regs.rbx = 0xAAAA; 190 regs.rbx = 0xBAC0; 191 vcpu_regs_set(vcpu, ®s); 192 rv = _vcpu_run(vcpu); 193 TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO); 194 TEST_ASSERT(run->s.regs.regs.rbx == 0xAAAA, 195 "rbx sync regs value incorrect 0x%llx.", 196 run->s.regs.regs.rbx); 197 vcpu_regs_get(vcpu, ®s); 198 TEST_ASSERT(regs.rbx == 0xBAC0 + 1, 199 "rbx guest value incorrect 0x%llx.", 200 regs.rbx); 201 202 /* Clear kvm_valid_regs bits. Verify s.regs values are not overwritten 203 * with existing guest values but that guest values are overwritten 204 * with kvm_sync_regs values. 205 */ 206 run->kvm_valid_regs = 0; 207 run->kvm_dirty_regs = TEST_SYNC_FIELDS; 208 run->s.regs.regs.rbx = 0xBBBB; 209 rv = _vcpu_run(vcpu); 210 TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO); 211 TEST_ASSERT(run->s.regs.regs.rbx == 0xBBBB, 212 "rbx sync regs value incorrect 0x%llx.", 213 run->s.regs.regs.rbx); 214 vcpu_regs_get(vcpu, ®s); 215 TEST_ASSERT(regs.rbx == 0xBBBB + 1, 216 "rbx guest value incorrect 0x%llx.", 217 regs.rbx); 218 219 kvm_vm_free(vm); 220 221 return 0; 222 } 223