From patchwork Tue Jun 22 14:54:41 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nadav Har'El X-Patchwork-Id: 107400 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter.kernel.org (8.14.4/8.14.3) with ESMTP id o5MEstCA015895 for ; Tue, 22 Jun 2010 14:54:55 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757048Ab0FVOyx (ORCPT ); Tue, 22 Jun 2010 10:54:53 -0400 Received: from mailgw11.technion.ac.il ([132.68.225.11]:43176 "EHLO mailgw11.technion.ac.il" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756476Ab0FVOyw (ORCPT ); Tue, 22 Jun 2010 10:54:52 -0400 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AvsEAKJrIEyERHMG/2dsb2JhbACfF3HBRQKFGQSDUiY X-IronPort-AV: E=Sophos;i="4.53,460,1272834000"; d="scan'208";a="8471752" Received: from fermat.math.technion.ac.il ([132.68.115.6]) by mailgw11.technion.ac.il with ESMTP; 22 Jun 2010 17:54:48 +0300 Received: from fermat.math.technion.ac.il (localhost [127.0.0.1]) by fermat.math.technion.ac.il (8.12.10/8.12.10) with ESMTP id o5MEshcQ027304; Tue, 22 Jun 2010 17:54:43 +0300 (IDT) Received: (from nyh@localhost) by fermat.math.technion.ac.il (8.12.10/8.12.10/Submit) id o5MEsf4p027303; Tue, 22 Jun 2010 17:54:41 +0300 (IDT) X-Authentication-Warning: fermat.math.technion.ac.il: nyh set sender to nyh@math.technion.ac.il using -f Date: Tue, 22 Jun 2010 17:54:41 +0300 From: "Nadav Har'El" To: Avi Kivity Cc: kvm@vger.kernel.org Subject: Re: [PATCH 5/24] Introduce vmcs12: a VMCS structure for L1 Message-ID: <20100622145441.GA23496@fermat.math.technion.ac.il> References: <1276431753-nyh@il.ibm.com> <201006131225.o5DCP79H012922@rice.haifa.ibm.com> <4C15E95D.9000300@redhat.com> Mime-Version: 1.0 Content-Disposition: inline In-Reply-To: <4C15E95D.9000300@redhat.com> User-Agent: Mutt/1.4.2.2i Hebrew-Date: 10 Tammuz 5770 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter.kernel.org [140.211.167.41]); Tue, 22 Jun 2010 14:54:55 +0000 (UTC) --- .before/arch/x86/kvm/vmx.c 2010-06-22 15:57:45.000000000 +0300 +++ .after/arch/x86/kvm/vmx.c 2010-06-22 15:57:45.000000000 +0300 @@ -126,6 +126,34 @@ struct shared_msr_entry { }; /* + * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a + * single nested guest (L2), hence the name vmcs12. Any VMX implementation has + * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is + * stored in guest memory specified by VMPTRLD, but is opaque to the guest, + * which must access it using VMREAD/VMWRITE/VMCLEAR instructions. More + * than one of these structures may exist, if L1 runs multiple L2 guests. + * nested_vmx_run() will use the data here to build a VMCS for the underlying + * hardware which will be used to run L2. + * This structure is packed in order to preserve the binary content after live + * migration. If there are changes in the content or layout, VMCS12_REVISION + * must be changed. + */ +struct __packed vmcs12 { + /* According to the Intel spec, a VMCS region must start with the + * following two fields. Then follow implementation-specific data. + */ + u32 revision_id; + u32 abort; +}; + +/* + * VMCS12_REVISION is an arbitrary id that should be changed if the content or + * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and + * VMPTRLD verifies that the VMCS region that L1 is loading contains this id. + */ +#define VMCS12_REVISION 0x11e57ed0 + +/* * The nested_vmx structure is part of vcpu_vmx, and holds information we need * for correct emulation of VMX (i.e., nested VMX) on this vcpu. For example, * the current VMCS set by L1, a list of the VMCSs used to run the active @@ -134,6 +162,12 @@ struct shared_msr_entry { struct nested_vmx { /* Has the level1 guest done vmxon? */ bool vmxon; + + /* The guest-physical address of the current VMCS L1 keeps for L2 */ + gpa_t current_vmptr; + /* The host-usable pointer to the above */ + struct page *current_vmcs12_page; + struct vmcs12 *current_vmcs12; }; struct vcpu_vmx { @@ -197,6 +231,21 @@ static inline struct vcpu_vmx *to_vmx(st return container_of(vcpu, struct vcpu_vmx, vcpu); } +static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr) +{ + struct page *page = gfn_to_page(vcpu->kvm, addr >> PAGE_SHIFT); + if (is_error_page(page)) { + kvm_release_page_clean(page); + return NULL; + } + return page; +} + +static void nested_release_page(struct page *page) +{ + kvm_release_page_dirty(page); +} + static int init_rmode(struct kvm *kvm); static u64 construct_eptp(unsigned long root_hpa); static void kvm_cpu_vmxon(u64 addr); @@ -3464,6 +3513,11 @@ static int handle_vmoff(struct kvm_vcpu to_vmx(vcpu)->nested.vmxon = false; + if(to_vmx(vcpu)->nested.current_vmptr != -1ull){ + kunmap(to_vmx(vcpu)->nested.current_vmcs12_page); + nested_release_page(to_vmx(vcpu)->nested.current_vmcs12_page); + } + skip_emulated_instruction(vcpu); return 1; } @@ -4136,6 +4190,10 @@ static void vmx_free_vcpu(struct kvm_vcp struct vcpu_vmx *vmx = to_vmx(vcpu); free_vpid(vmx); + if (vmx->nested.vmxon && to_vmx(vcpu)->nested.current_vmptr != -1ull){ + kunmap(to_vmx(vcpu)->nested.current_vmcs12_page); + nested_release_page(to_vmx(vcpu)->nested.current_vmcs12_page); + } vmx_free_vmcs(vcpu); kfree(vmx->guest_msrs); kvm_vcpu_uninit(vcpu); @@ -4201,6 +4259,9 @@ static struct kvm_vcpu *vmx_create_vcpu( goto free_vmcs; } + vmx->nested.current_vmptr = -1ull; + vmx->nested.current_vmcs12 = NULL; + return &vmx->vcpu; free_vmcs: