Skip to content

Commit a9d30f3

Browse files
Nadav Har'Elavikivity
authored andcommitted
KVM: nVMX: Introduce vmcs12: a VMCS structure for L1
An implementation of VMX needs to define a VMCS structure. This structure is kept in guest memory, but is opaque to the guest (who can only read or write it with VMX instructions). This patch starts to define the VMCS structure which our nested VMX implementation will present to L1. We call it "vmcs12", as it is the VMCS that L1 keeps for its L2 guest. We will add more content to this structure in later patches. This patch also adds the notion (as required by the VMX spec) of L1's "current VMCS", and finally includes utility functions for mapping the guest-allocated VMCSs in host memory. Signed-off-by: Nadav Har'El <nyh@il.ibm.com> Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
1 parent 5e1746d commit a9d30f3

1 file changed

Lines changed: 75 additions & 0 deletions

File tree

‎arch/x86/kvm/vmx.c‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,54 @@ struct shared_msr_entry {
144144
u64 mask;
145145
};
146146

147+
/*
148+
* struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
149+
* single nested guest (L2), hence the name vmcs12. Any VMX implementation has
150+
* a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
151+
* stored in guest memory specified by VMPTRLD, but is opaque to the guest,
152+
* which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
153+
* More than one of these structures may exist, if L1 runs multiple L2 guests.
154+
* nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the
155+
* underlying hardware which will be used to run L2.
156+
* This structure is packed to ensure that its layout is identical across
157+
* machines (necessary for live migration).
158+
* If there are changes in this struct, VMCS12_REVISION must be changed.
159+
*/
160+
struct __packed vmcs12 {
161+
/* According to the Intel spec, a VMCS region must start with the
162+
* following two fields. Then follow implementation-specific data.
163+
*/
164+
u32 revision_id;
165+
u32 abort;
166+
};
167+
168+
/*
169+
* VMCS12_REVISION is an arbitrary id that should be changed if the content or
170+
* layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
171+
* VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
172+
*/
173+
#define VMCS12_REVISION 0x11e57ed0
174+
175+
/*
176+
* VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
177+
* and any VMCS region. Although only sizeof(struct vmcs12) are used by the
178+
* current implementation, 4K are reserved to avoid future complications.
179+
*/
180+
#define VMCS12_SIZE 0x1000
181+
147182
/*
148183
* The nested_vmx structure is part of vcpu_vmx, and holds information we need
149184
* for correct emulation of VMX (i.e., nested VMX) on this vcpu.
150185
*/
151186
struct nested_vmx {
152187
/* Has the level1 guest done vmxon? */
153188
bool vmxon;
189+
190+
/* The guest-physical address of the current VMCS L1 keeps for L2 */
191+
gpa_t current_vmptr;
192+
/* The host-usable pointer to the above */
193+
struct page *current_vmcs12_page;
194+
struct vmcs12 *current_vmcs12;
154195
};
155196

156197
struct vcpu_vmx {
@@ -231,6 +272,31 @@ static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
231272
return container_of(vcpu, struct vcpu_vmx, vcpu);
232273
}
233274

275+
static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
276+
{
277+
return to_vmx(vcpu)->nested.current_vmcs12;
278+
}
279+
280+
static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
281+
{
282+
struct page *page = gfn_to_page(vcpu->kvm, addr >> PAGE_SHIFT);
283+
if (is_error_page(page)) {
284+
kvm_release_page_clean(page);
285+
return NULL;
286+
}
287+
return page;
288+
}
289+
290+
static void nested_release_page(struct page *page)
291+
{
292+
kvm_release_page_dirty(page);
293+
}
294+
295+
static void nested_release_page_clean(struct page *page)
296+
{
297+
kvm_release_page_clean(page);
298+
}
299+
234300
static u64 construct_eptp(unsigned long root_hpa);
235301
static void kvm_cpu_vmxon(u64 addr);
236302
static void kvm_cpu_vmxoff(void);
@@ -4039,6 +4105,12 @@ static void free_nested(struct vcpu_vmx *vmx)
40394105
if (!vmx->nested.vmxon)
40404106
return;
40414107
vmx->nested.vmxon = false;
4108+
if (vmx->nested.current_vmptr != -1ull) {
4109+
kunmap(vmx->nested.current_vmcs12_page);
4110+
nested_release_page(vmx->nested.current_vmcs12_page);
4111+
vmx->nested.current_vmptr = -1ull;
4112+
vmx->nested.current_vmcs12 = NULL;
4113+
}
40424114
}
40434115

40444116
/* Emulate the VMXOFF instruction */
@@ -4543,6 +4615,9 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
45434615
goto free_vmcs;
45444616
}
45454617

4618+
vmx->nested.current_vmptr = -1ull;
4619+
vmx->nested.current_vmcs12 = NULL;
4620+
45464621
return &vmx->vcpu;
45474622

45484623
free_vmcs:

0 commit comments

Comments
 (0)