From patchwork Sat Nov 22 04:24:31 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wincy Van X-Patchwork-Id: 5358731 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id B0BDEC11AC for ; Sat, 22 Nov 2014 04:25:22 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 9CAC420158 for ; Sat, 22 Nov 2014 04:25:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 7274A20154 for ; Sat, 22 Nov 2014 04:25:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752902AbaKVEY4 (ORCPT ); Fri, 21 Nov 2014 23:24:56 -0500 Received: from mail-lb0-f171.google.com ([209.85.217.171]:47046 "EHLO mail-lb0-f171.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752811AbaKVEYz (ORCPT ); Fri, 21 Nov 2014 23:24:55 -0500 Received: by mail-lb0-f171.google.com with SMTP id b6so5170305lbj.16 for ; Fri, 21 Nov 2014 20:24:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to:cc:content-type; bh=JfM98UJPbf6gKEvn7Jv7meqHDYlUaEXhL9s5sEpoydk=; b=XAidY6MzXKy3ZlGmWiP7nqS9JCPSCDdrVR2KCCG8z4qboFB+H6oVk3xtu3Pc2xmV/K JJWsqNdFCDKcoak4rYIWwIzJRhWgHo8+aSSo+5ylyjUYT8FRQ+3hYIYivP7ljrXkOMoa ih4b+9H2L7HBj38QLcX18z5j6h8ZXlxT3rHMzo57WRd2XHJBBLNWzbCF+bb9/dMBQKWg 7u8xVFgcB4HxtUtQVhyFqD7UCI/s6ujIZstsCYaEPdMB6z2v1lSA+YqDYhrGlEXspWUv C0rWByc2VQjv6ZnkVDg24ETt6GSeM0KDm1aZKcC8HF3w/kiaSkKo7k/BoyMyZIKEbIfm qoiw== X-Received: by 10.112.130.132 with SMTP id oe4mr8818909lbb.82.1416630292126; Fri, 21 Nov 2014 20:24:52 -0800 (PST) MIME-Version: 1.0 Received: by 10.25.161.203 with HTTP; Fri, 21 Nov 2014 20:24:31 -0800 (PST) From: Wincy Van Date: Sat, 22 Nov 2014 12:24:31 +0800 Message-ID: Subject: [PATCH] KVM: nVMX: nested MSR auto load/restore emulation. To: Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, =?UTF-8?B?6IyD5paH5LiA?= Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Some hypervisors need MSR auto load/restore feature. We read MSRs from vm-entry MSR load area which specified by L1, and load them via kvm_set_msr in the nested entry. When nested exit occurs, we get MSRs via kvm_get_msr, writting them to L1`s MSR store area. After this, we read MSRs from vm-exit MSR load area, and load them via kvm_set_msr. VirtualBox will work fine with this patch. Signed-off-by: Wincy Van gpa_t gpa, unsigned long len) --- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/arch/x86/include/uapi/asm/vmx.h b/arch/x86/include/uapi/asm/vmx.h index 990a2fe..986af3f 100644 --- a/arch/x86/include/uapi/asm/vmx.h +++ b/arch/x86/include/uapi/asm/vmx.h @@ -56,6 +56,7 @@ #define EXIT_REASON_MSR_READ 31 #define EXIT_REASON_MSR_WRITE 32 #define EXIT_REASON_INVALID_STATE 33 +#define EXIT_REASON_MSR_LOAD_FAIL 34 #define EXIT_REASON_MWAIT_INSTRUCTION 36 #define EXIT_REASON_MONITOR_INSTRUCTION 39 #define EXIT_REASON_PAUSE_INSTRUCTION 40 @@ -114,8 +115,12 @@ { EXIT_REASON_APIC_WRITE, "APIC_WRITE" }, \ { EXIT_REASON_EOI_INDUCED, "EOI_INDUCED" }, \ { EXIT_REASON_INVALID_STATE, "INVALID_STATE" }, \ + { EXIT_REASON_MSR_LOAD_FAIL, "MSR_LOAD_FAIL" }, \ { EXIT_REASON_INVD, "INVD" }, \ { EXIT_REASON_INVVPID, "INVVPID" }, \ { EXIT_REASON_INVPCID, "INVPCID" } +#define VMX_ABORT_SAVE_GUEST_MSR_FAIL 1 +#define VMX_ABORT_LOAD_HOST_MSR_FAIL 4 + #endif /* _UAPIVMX_H */ diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index 6a951d8..377e405 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -6088,6 +6088,13 @@ static void nested_vmx_failValid(struct kvm_vcpu *vcpu, */ } +static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator) +{ + /* TODO: not to simply reset guest here. */ + kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); + printk(KERN_WARNING"kvm: nested vmx abort, indicator %d\n", indicator); +} + static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer) { struct vcpu_vmx *vmx = @@ -8215,6 +8222,88 @@ static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu) ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL); } +static inline int nested_msr_check_common(struct vmx_msr_entry *e) +{ + if (e->index >> 8 == 0x8 || e->reserved != 0) + return -EINVAL; + return 0; +} + +static inline int nested_load_msr_check(struct vmx_msr_entry *e) +{ + if (e->index == MSR_FS_BASE || + e->index == MSR_GS_BASE || + nested_msr_check_common(e)) + return -EINVAL; + return 0; +} + +/* load guest msr at nested entry. + * return 0 for success, entry index for failed. + */ +static u32 nested_entry_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) +{ + u32 i = 0; + struct vmx_msr_entry e; + struct msr_data msr; + + msr.host_initiated = false; + while (i < count) { + kvm_read_guest(vcpu->kvm, gpa + i * sizeof(struct vmx_msr_entry), + &e, sizeof(struct vmx_msr_entry)); + if (nested_load_msr_check(&e)) + goto fail; + msr.index = e.index; + msr.data = e.value; + if (kvm_set_msr(vcpu, &msr)) + goto fail; + ++i; + } + return 0; +fail: + return i + 1; +} + +static int nested_exit_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) +{ + u32 i = 0; + struct vmx_msr_entry e; + + while (i < count) { + kvm_read_guest(vcpu->kvm, gpa + i * sizeof(struct vmx_msr_entry), + &e, sizeof(struct vmx_msr_entry)); + if (nested_msr_check_common(&e)) + return -EINVAL; + if (kvm_get_msr(vcpu, e.index, &e.value)) + return -EINVAL; + kvm_write_guest(vcpu->kvm, gpa + i * sizeof(struct vmx_msr_entry), + &e, sizeof(struct vmx_msr_entry)); + ++i; + } + return 0; +} + +static int nested_exit_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) +{ + u32 i = 0; + struct vmx_msr_entry e; + struct msr_data msr; + + msr.host_initiated = false; + while (i < count) { + kvm_read_guest(vcpu->kvm, gpa + i * sizeof(struct vmx_msr_entry), + &e, sizeof(struct vmx_msr_entry)); + if (nested_load_msr_check(&e)) + return -EINVAL; + msr.index = e.index; + msr.data = e.value; + if (kvm_set_msr(vcpu, &msr)) + return -EINVAL; + ++i; + } + return 0; +} + /* * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it @@ -8509,6 +8598,7 @@ static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) int cpu; struct loaded_vmcs *vmcs02; bool ia32e; + u32 msr_entry_idx; if (!nested_vmx_check_permission(vcpu) || !nested_vmx_check_vmcs12(vcpu)) @@ -8556,11 +8646,12 @@ static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) return 1; } - if (vmcs12->vm_entry_msr_load_count > 0 || - vmcs12->vm_exit_msr_load_count > 0 || - vmcs12->vm_exit_msr_store_count > 0) { - pr_warn_ratelimited("%s: VMCS MSR_{LOAD,STORE} unsupported\n", - __func__); + if ((vmcs12->vm_entry_msr_load_count > 0 && + !IS_ALIGNED(vmcs12->vm_entry_msr_load_addr, 16)) || + (vmcs12->vm_exit_msr_load_count > 0 && + !IS_ALIGNED(vmcs12->vm_exit_msr_load_addr, 16)) || + (vmcs12->vm_exit_msr_store_count > 0 && + !IS_ALIGNED(vmcs12->vm_exit_msr_store_addr, 16))) { nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); return 1; } @@ -8666,10 +8757,20 @@ static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) vmx_segment_cache_clear(vmx); - vmcs12->launch_state = 1; - prepare_vmcs02(vcpu, vmcs12); + msr_entry_idx = nested_entry_load_msr(vcpu, vmcs12->vm_entry_msr_load_addr, + vmcs12->vm_entry_msr_load_count); + if (msr_entry_idx) { + leave_guest_mode(vcpu); + vmx_load_vmcs01(vcpu); + nested_vmx_entry_failure(vcpu, vmcs12, + EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx); + return 1; + } + + vmcs12->launch_state = 1; + if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) return kvm_emulate_halt(vcpu); @@ -9097,6 +9198,10 @@ static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, kvm_set_dr(vcpu, 7, 0x400); vmcs_write64(GUEST_IA32_DEBUGCTL, 0); + + if (nested_exit_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr, + vmcs12->vm_exit_msr_load_count)) + nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); } /* @@ -9118,6 +9223,10 @@ static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info, exit_qualification); + if (nested_exit_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr, + vmcs12->vm_exit_msr_store_count)) + nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL); + vmx_load_vmcs01(vcpu); if ((exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index a8f53a6..1c7eecc 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -2323,6 +2323,7 @@ int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) { return kvm_x86_ops->get_msr(vcpu, msr_index, pdata); } +EXPORT_SYMBOL_GPL(kvm_get_msr); static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) { diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 751ece6..be02556 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1585,6 +1585,7 @@ int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data, } return 0; } +EXPORT_SYMBOL_GPL(kvm_write_guest); int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,