Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * KVM_GET/SET_* tests
0004  *
0005  * Copyright (C) 2018, Red Hat, Inc.
0006  *
0007  * Tests for vCPU state save/restore, including nested guest state.
0008  */
0009 #define _GNU_SOURCE /* for program_invocation_short_name */
0010 #include <fcntl.h>
0011 #include <stdio.h>
0012 #include <stdlib.h>
0013 #include <string.h>
0014 #include <sys/ioctl.h>
0015 
0016 #include "test_util.h"
0017 
0018 #include "kvm_util.h"
0019 #include "processor.h"
0020 #include "vmx.h"
0021 #include "svm_util.h"
0022 
0023 #define L2_GUEST_STACK_SIZE 256
0024 
0025 void svm_l2_guest_code(void)
0026 {
0027     GUEST_SYNC(4);
0028     /* Exit to L1 */
0029     vmcall();
0030     GUEST_SYNC(6);
0031     /* Done, exit to L1 and never come back.  */
0032     vmcall();
0033 }
0034 
0035 static void svm_l1_guest_code(struct svm_test_data *svm)
0036 {
0037     unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
0038     struct vmcb *vmcb = svm->vmcb;
0039 
0040     GUEST_ASSERT(svm->vmcb_gpa);
0041     /* Prepare for L2 execution. */
0042     generic_svm_setup(svm, svm_l2_guest_code,
0043               &l2_guest_stack[L2_GUEST_STACK_SIZE]);
0044 
0045     GUEST_SYNC(3);
0046     run_guest(vmcb, svm->vmcb_gpa);
0047     GUEST_ASSERT(vmcb->control.exit_code == SVM_EXIT_VMMCALL);
0048     GUEST_SYNC(5);
0049     vmcb->save.rip += 3;
0050     run_guest(vmcb, svm->vmcb_gpa);
0051     GUEST_ASSERT(vmcb->control.exit_code == SVM_EXIT_VMMCALL);
0052     GUEST_SYNC(7);
0053 }
0054 
0055 void vmx_l2_guest_code(void)
0056 {
0057     GUEST_SYNC(6);
0058 
0059     /* Exit to L1 */
0060     vmcall();
0061 
0062     /* L1 has now set up a shadow VMCS for us.  */
0063     GUEST_ASSERT(vmreadz(GUEST_RIP) == 0xc0ffee);
0064     GUEST_SYNC(10);
0065     GUEST_ASSERT(vmreadz(GUEST_RIP) == 0xc0ffee);
0066     GUEST_ASSERT(!vmwrite(GUEST_RIP, 0xc0fffee));
0067     GUEST_SYNC(11);
0068     GUEST_ASSERT(vmreadz(GUEST_RIP) == 0xc0fffee);
0069     GUEST_ASSERT(!vmwrite(GUEST_RIP, 0xc0ffffee));
0070     GUEST_SYNC(12);
0071 
0072     /* Done, exit to L1 and never come back.  */
0073     vmcall();
0074 }
0075 
0076 static void vmx_l1_guest_code(struct vmx_pages *vmx_pages)
0077 {
0078     unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
0079 
0080     GUEST_ASSERT(vmx_pages->vmcs_gpa);
0081     GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages));
0082     GUEST_SYNC(3);
0083     GUEST_ASSERT(load_vmcs(vmx_pages));
0084     GUEST_ASSERT(vmptrstz() == vmx_pages->vmcs_gpa);
0085 
0086     GUEST_SYNC(4);
0087     GUEST_ASSERT(vmptrstz() == vmx_pages->vmcs_gpa);
0088 
0089     prepare_vmcs(vmx_pages, vmx_l2_guest_code,
0090              &l2_guest_stack[L2_GUEST_STACK_SIZE]);
0091 
0092     GUEST_SYNC(5);
0093     GUEST_ASSERT(vmptrstz() == vmx_pages->vmcs_gpa);
0094     GUEST_ASSERT(!vmlaunch());
0095     GUEST_ASSERT(vmptrstz() == vmx_pages->vmcs_gpa);
0096     GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
0097 
0098     /* Check that the launched state is preserved.  */
0099     GUEST_ASSERT(vmlaunch());
0100 
0101     GUEST_ASSERT(!vmresume());
0102     GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
0103 
0104     GUEST_SYNC(7);
0105     GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
0106 
0107     GUEST_ASSERT(!vmresume());
0108     GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
0109 
0110     vmwrite(GUEST_RIP, vmreadz(GUEST_RIP) + 3);
0111 
0112     vmwrite(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
0113     vmwrite(VMCS_LINK_POINTER, vmx_pages->shadow_vmcs_gpa);
0114 
0115     GUEST_ASSERT(!vmptrld(vmx_pages->shadow_vmcs_gpa));
0116     GUEST_ASSERT(vmlaunch());
0117     GUEST_SYNC(8);
0118     GUEST_ASSERT(vmlaunch());
0119     GUEST_ASSERT(vmresume());
0120 
0121     vmwrite(GUEST_RIP, 0xc0ffee);
0122     GUEST_SYNC(9);
0123     GUEST_ASSERT(vmreadz(GUEST_RIP) == 0xc0ffee);
0124 
0125     GUEST_ASSERT(!vmptrld(vmx_pages->vmcs_gpa));
0126     GUEST_ASSERT(!vmresume());
0127     GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
0128 
0129     GUEST_ASSERT(!vmptrld(vmx_pages->shadow_vmcs_gpa));
0130     GUEST_ASSERT(vmreadz(GUEST_RIP) == 0xc0ffffee);
0131     GUEST_ASSERT(vmlaunch());
0132     GUEST_ASSERT(vmresume());
0133     GUEST_SYNC(13);
0134     GUEST_ASSERT(vmreadz(GUEST_RIP) == 0xc0ffffee);
0135     GUEST_ASSERT(vmlaunch());
0136     GUEST_ASSERT(vmresume());
0137 }
0138 
0139 static void __attribute__((__flatten__)) guest_code(void *arg)
0140 {
0141     GUEST_SYNC(1);
0142     GUEST_SYNC(2);
0143 
0144     if (arg) {
0145         if (this_cpu_has(X86_FEATURE_SVM))
0146             svm_l1_guest_code(arg);
0147         else
0148             vmx_l1_guest_code(arg);
0149     }
0150 
0151     GUEST_DONE();
0152 }
0153 
0154 int main(int argc, char *argv[])
0155 {
0156     vm_vaddr_t nested_gva = 0;
0157 
0158     struct kvm_regs regs1, regs2;
0159     struct kvm_vcpu *vcpu;
0160     struct kvm_vm *vm;
0161     struct kvm_run *run;
0162     struct kvm_x86_state *state;
0163     struct ucall uc;
0164     int stage;
0165 
0166     /* Create VM */
0167     vm = vm_create_with_one_vcpu(&vcpu, guest_code);
0168     run = vcpu->run;
0169 
0170     vcpu_regs_get(vcpu, &regs1);
0171 
0172     if (kvm_has_cap(KVM_CAP_NESTED_STATE)) {
0173         if (kvm_cpu_has(X86_FEATURE_SVM))
0174             vcpu_alloc_svm(vm, &nested_gva);
0175         else if (kvm_cpu_has(X86_FEATURE_VMX))
0176             vcpu_alloc_vmx(vm, &nested_gva);
0177     }
0178 
0179     if (!nested_gva)
0180         pr_info("will skip nested state checks\n");
0181 
0182     vcpu_args_set(vcpu, 1, nested_gva);
0183 
0184     for (stage = 1;; stage++) {
0185         vcpu_run(vcpu);
0186         TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
0187                 "Stage %d: unexpected exit reason: %u (%s),\n",
0188                 stage, run->exit_reason,
0189                 exit_reason_str(run->exit_reason));
0190 
0191         switch (get_ucall(vcpu, &uc)) {
0192         case UCALL_ABORT:
0193             REPORT_GUEST_ASSERT(uc);
0194             /* NOT REACHED */
0195         case UCALL_SYNC:
0196             break;
0197         case UCALL_DONE:
0198             goto done;
0199         default:
0200             TEST_FAIL("Unknown ucall %lu", uc.cmd);
0201         }
0202 
0203         /* UCALL_SYNC is handled here.  */
0204         TEST_ASSERT(!strcmp((const char *)uc.args[0], "hello") &&
0205                 uc.args[1] == stage, "Stage %d: Unexpected register values vmexit, got %lx",
0206                 stage, (ulong)uc.args[1]);
0207 
0208         state = vcpu_save_state(vcpu);
0209         memset(&regs1, 0, sizeof(regs1));
0210         vcpu_regs_get(vcpu, &regs1);
0211 
0212         kvm_vm_release(vm);
0213 
0214         /* Restore state in a new VM.  */
0215         vcpu = vm_recreate_with_one_vcpu(vm);
0216         vcpu_load_state(vcpu, state);
0217         run = vcpu->run;
0218         kvm_x86_state_cleanup(state);
0219 
0220         memset(&regs2, 0, sizeof(regs2));
0221         vcpu_regs_get(vcpu, &regs2);
0222         TEST_ASSERT(!memcmp(&regs1, &regs2, sizeof(regs2)),
0223                 "Unexpected register values after vcpu_load_state; rdi: %lx rsi: %lx",
0224                 (ulong) regs2.rdi, (ulong) regs2.rsi);
0225     }
0226 
0227 done:
0228     kvm_vm_free(vm);
0229 }