Skip to content

Commit 6ca00df

Browse files
Liran Alonbonzini
authored andcommitted
KVM: x86: Modify struct kvm_nested_state to have explicit fields for data
Improve the KVM_{GET,SET}_NESTED_STATE structs by detailing the format of VMX nested state data in a struct. In order to avoid changing the ioctl values of KVM_{GET,SET}_NESTED_STATE, there is a need to preserve sizeof(struct kvm_nested_state). This is done by defining the data struct as "data.vmx[0]". It was the most elegant way I found to preserve struct size while still keeping struct readable and easy to maintain. It does have a misfortunate side-effect that now it has to be accessed as "data.vmx[0]" rather than just "data.vmx". Because we are already modifying these structs, I also modified the following: * Define the "format" field values as macros. * Rename vmcs_pa to vmcs12_pa for better readability. Signed-off-by: Liran Alon <liran.alon@oracle.com> [Remove SVM stubs, add KVM_STATE_NESTED_VMX_VMCS12_SIZE. - Paolo] Reviewed-by: Liran Alon <liran.alon@oracle.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 76e3bcd commit 6ca00df

6 files changed

Lines changed: 122 additions & 85 deletions

File tree

‎Documentation/virtual/kvm/api.txt‎

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3857,43 +3857,59 @@ Type: vcpu ioctl
38573857
Parameters: struct kvm_nested_state (in/out)
38583858
Returns: 0 on success, -1 on error
38593859
Errors:
3860-
E2BIG: the total state size (including the fixed-size part of struct
3861-
kvm_nested_state) exceeds the value of 'size' specified by
3860+
E2BIG: the total state size exceeds the value of 'size' specified by
38623861
the user; the size required will be written into size.
38633862

38643863
struct kvm_nested_state {
38653864
__u16 flags;
38663865
__u16 format;
38673866
__u32 size;
3867+
38683868
union {
3869-
struct kvm_vmx_nested_state vmx;
3870-
struct kvm_svm_nested_state svm;
3869+
struct kvm_vmx_nested_state_hdr vmx;
3870+
struct kvm_svm_nested_state_hdr svm;
3871+
3872+
/* Pad the header to 128 bytes. */
38713873
__u8 pad[120];
3872-
};
3873-
__u8 data[0];
3874+
} hdr;
3875+
3876+
union {
3877+
struct kvm_vmx_nested_state_data vmx[0];
3878+
struct kvm_svm_nested_state_data svm[0];
3879+
} data;
38743880
};
38753881

38763882
#define KVM_STATE_NESTED_GUEST_MODE 0x00000001
38773883
#define KVM_STATE_NESTED_RUN_PENDING 0x00000002
3884+
#define KVM_STATE_NESTED_EVMCS 0x00000004
38783885

3879-
#define KVM_STATE_NESTED_SMM_GUEST_MODE 0x00000001
3880-
#define KVM_STATE_NESTED_SMM_VMXON 0x00000002
3886+
#define KVM_STATE_NESTED_FORMAT_VMX 0
3887+
#define KVM_STATE_NESTED_FORMAT_SVM 1
38813888

3882-
struct kvm_vmx_nested_state {
3889+
#define KVM_STATE_NESTED_VMX_VMCS_SIZE 0x1000
3890+
3891+
#define KVM_STATE_NESTED_VMX_SMM_GUEST_MODE 0x00000001
3892+
#define KVM_STATE_NESTED_VMX_SMM_VMXON 0x00000002
3893+
3894+
struct kvm_vmx_nested_state_hdr {
38833895
__u64 vmxon_pa;
3884-
__u64 vmcs_pa;
3896+
__u64 vmcs12_pa;
38853897

38863898
struct {
38873899
__u16 flags;
38883900
} smm;
38893901
};
38903902

3903+
struct kvm_vmx_nested_state_data {
3904+
__u8 vmcs12[KVM_STATE_NESTED_VMX_VMCS_SIZE];
3905+
__u8 shadow_vmcs12[KVM_STATE_NESTED_VMX_VMCS_SIZE];
3906+
};
3907+
38913908
This ioctl copies the vcpu's nested virtualization state from the kernel to
38923909
userspace.
38933910

3894-
The maximum size of the state, including the fixed-size part of struct
3895-
kvm_nested_state, can be retrieved by passing KVM_CAP_NESTED_STATE to
3896-
the KVM_CHECK_EXTENSION ioctl().
3911+
The maximum size of the state can be retrieved by passing KVM_CAP_NESTED_STATE
3912+
to the KVM_CHECK_EXTENSION ioctl().
38973913

38983914
4.115 KVM_SET_NESTED_STATE
38993915

@@ -3903,8 +3919,8 @@ Type: vcpu ioctl
39033919
Parameters: struct kvm_nested_state (in)
39043920
Returns: 0 on success, -1 on error
39053921

3906-
This copies the vcpu's kvm_nested_state struct from userspace to the kernel. For
3907-
the definition of struct kvm_nested_state, see KVM_GET_NESTED_STATE.
3922+
This copies the vcpu's kvm_nested_state struct from userspace to the kernel.
3923+
For the definition of struct kvm_nested_state, see KVM_GET_NESTED_STATE.
39083924

39093925
4.116 KVM_(UN)REGISTER_COALESCED_MMIO
39103926

‎arch/x86/include/uapi/asm/kvm.h‎

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -383,16 +383,26 @@ struct kvm_sync_regs {
383383
#define KVM_X86_QUIRK_LAPIC_MMIO_HOLE (1 << 2)
384384
#define KVM_X86_QUIRK_OUT_7E_INC_RIP (1 << 3)
385385

386+
#define KVM_STATE_NESTED_FORMAT_VMX 0
387+
#define KVM_STATE_NESTED_FORMAT_SVM 1 /* unused */
388+
386389
#define KVM_STATE_NESTED_GUEST_MODE 0x00000001
387390
#define KVM_STATE_NESTED_RUN_PENDING 0x00000002
388391
#define KVM_STATE_NESTED_EVMCS 0x00000004
389392

390393
#define KVM_STATE_NESTED_SMM_GUEST_MODE 0x00000001
391394
#define KVM_STATE_NESTED_SMM_VMXON 0x00000002
392395

393-
struct kvm_vmx_nested_state {
396+
#define KVM_STATE_NESTED_VMX_VMCS_SIZE 0x1000
397+
398+
struct kvm_vmx_nested_state_data {
399+
__u8 vmcs12[KVM_STATE_NESTED_VMX_VMCS_SIZE];
400+
__u8 shadow_vmcs12[KVM_STATE_NESTED_VMX_VMCS_SIZE];
401+
};
402+
403+
struct kvm_vmx_nested_state_hdr {
394404
__u64 vmxon_pa;
395-
__u64 vmcs_pa;
405+
__u64 vmcs12_pa;
396406

397407
struct {
398408
__u16 flags;
@@ -401,24 +411,25 @@ struct kvm_vmx_nested_state {
401411

402412
/* for KVM_CAP_NESTED_STATE */
403413
struct kvm_nested_state {
404-
/* KVM_STATE_* flags */
405414
__u16 flags;
406-
407-
/* 0 for VMX, 1 for SVM. */
408415
__u16 format;
409-
410-
/* 128 for SVM, 128 + VMCS size for VMX. */
411416
__u32 size;
412417

413418
union {
414-
/* VMXON, VMCS */
415-
struct kvm_vmx_nested_state vmx;
419+
struct kvm_vmx_nested_state_hdr vmx;
416420

417421
/* Pad the header to 128 bytes. */
418422
__u8 pad[120];
419-
};
423+
} hdr;
420424

421-
__u8 data[0];
425+
/*
426+
* Define data region as 0 bytes to preserve backwards-compatability
427+
* to old definition of kvm_nested_state in order to avoid changing
428+
* KVM_{GET,PUT}_NESTED_STATE ioctl values.
429+
*/
430+
union {
431+
struct kvm_vmx_nested_state_data vmx[0];
432+
} data;
422433
};
423434

424435
#endif /* _ASM_X86_KVM_H */

‎arch/x86/kvm/vmx/nested.c‎

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5226,14 +5226,16 @@ static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
52265226
struct vmcs12 *vmcs12;
52275227
struct kvm_nested_state kvm_state = {
52285228
.flags = 0,
5229-
.format = 0,
5229+
.format = KVM_STATE_NESTED_FORMAT_VMX,
52305230
.size = sizeof(kvm_state),
5231-
.vmx.vmxon_pa = -1ull,
5232-
.vmx.vmcs_pa = -1ull,
5231+
.hdr.vmx.vmxon_pa = -1ull,
5232+
.hdr.vmx.vmcs12_pa = -1ull,
52335233
};
5234+
struct kvm_vmx_nested_state_data __user *user_vmx_nested_state =
5235+
&user_kvm_nested_state->data.vmx[0];
52345236

52355237
if (!vcpu)
5236-
return kvm_state.size + 2 * VMCS12_SIZE;
5238+
return kvm_state.size + sizeof(*user_vmx_nested_state);
52375239

52385240
vmx = to_vmx(vcpu);
52395241
vmcs12 = get_vmcs12(vcpu);
@@ -5243,23 +5245,23 @@ static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
52435245

52445246
if (nested_vmx_allowed(vcpu) &&
52455247
(vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
5246-
kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
5247-
kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
5248+
kvm_state.hdr.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
5249+
kvm_state.hdr.vmx.vmcs12_pa = vmx->nested.current_vmptr;
52485250

52495251
if (vmx_has_valid_vmcs12(vcpu)) {
5250-
kvm_state.size += VMCS12_SIZE;
5252+
kvm_state.size += sizeof(user_vmx_nested_state->vmcs12);
52515253

52525254
if (is_guest_mode(vcpu) &&
52535255
nested_cpu_has_shadow_vmcs(vmcs12) &&
52545256
vmcs12->vmcs_link_pointer != -1ull)
5255-
kvm_state.size += VMCS12_SIZE;
5257+
kvm_state.size += sizeof(user_vmx_nested_state->shadow_vmcs12);
52565258
}
52575259

52585260
if (vmx->nested.smm.vmxon)
5259-
kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
5261+
kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
52605262

52615263
if (vmx->nested.smm.guest_mode)
5262-
kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
5264+
kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
52635265

52645266
if (is_guest_mode(vcpu)) {
52655267
kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
@@ -5294,16 +5296,19 @@ static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
52945296
copy_shadow_to_vmcs12(vmx);
52955297
}
52965298

5299+
BUILD_BUG_ON(sizeof(user_vmx_nested_state->vmcs12) < VMCS12_SIZE);
5300+
BUILD_BUG_ON(sizeof(user_vmx_nested_state->shadow_vmcs12) < VMCS12_SIZE);
5301+
52975302
/*
52985303
* Copy over the full allocated size of vmcs12 rather than just the size
52995304
* of the struct.
53005305
*/
5301-
if (copy_to_user(user_kvm_nested_state->data, vmcs12, VMCS12_SIZE))
5306+
if (copy_to_user(user_vmx_nested_state->vmcs12, vmcs12, VMCS12_SIZE))
53025307
return -EFAULT;
53035308

53045309
if (nested_cpu_has_shadow_vmcs(vmcs12) &&
53055310
vmcs12->vmcs_link_pointer != -1ull) {
5306-
if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
5311+
if (copy_to_user(user_vmx_nested_state->shadow_vmcs12,
53075312
get_shadow_vmcs12(vcpu), VMCS12_SIZE))
53085313
return -EFAULT;
53095314
}
@@ -5331,33 +5336,35 @@ static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
53315336
struct vcpu_vmx *vmx = to_vmx(vcpu);
53325337
struct vmcs12 *vmcs12;
53335338
u32 exit_qual;
5339+
struct kvm_vmx_nested_state_data __user *user_vmx_nested_state =
5340+
&user_kvm_nested_state->data.vmx[0];
53345341
int ret;
53355342

5336-
if (kvm_state->format != 0)
5343+
if (kvm_state->format != KVM_STATE_NESTED_FORMAT_VMX)
53375344
return -EINVAL;
53385345

53395346
if (!nested_vmx_allowed(vcpu))
5340-
return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
5347+
return kvm_state->hdr.vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
53415348

5342-
if (kvm_state->vmx.vmxon_pa == -1ull) {
5343-
if (kvm_state->vmx.smm.flags)
5349+
if (kvm_state->hdr.vmx.vmxon_pa == -1ull) {
5350+
if (kvm_state->hdr.vmx.smm.flags)
53445351
return -EINVAL;
53455352

5346-
if (kvm_state->vmx.vmcs_pa != -1ull)
5353+
if (kvm_state->hdr.vmx.vmcs12_pa != -1ull)
53475354
return -EINVAL;
53485355

53495356
vmx_leave_nested(vcpu);
53505357
return 0;
53515358
}
53525359

5353-
if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
5360+
if (!page_address_valid(vcpu, kvm_state->hdr.vmx.vmxon_pa))
53545361
return -EINVAL;
53555362

5356-
if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
5363+
if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
53575364
(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
53585365
return -EINVAL;
53595366

5360-
if (kvm_state->vmx.smm.flags &
5367+
if (kvm_state->hdr.vmx.smm.flags &
53615368
~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
53625369
return -EINVAL;
53635370

@@ -5366,21 +5373,21 @@ static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
53665373
* nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
53675374
* must be zero.
53685375
*/
5369-
if (is_smm(vcpu) ? kvm_state->flags : kvm_state->vmx.smm.flags)
5376+
if (is_smm(vcpu) ? kvm_state->flags : kvm_state->hdr.vmx.smm.flags)
53705377
return -EINVAL;
53715378

5372-
if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
5373-
!(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
5379+
if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
5380+
!(kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
53745381
return -EINVAL;
53755382

53765383
vmx_leave_nested(vcpu);
5377-
if (kvm_state->vmx.vmxon_pa == -1ull)
5384+
if (kvm_state->hdr.vmx.vmxon_pa == -1ull)
53785385
return 0;
53795386

53805387
if (kvm_state->flags & KVM_STATE_NESTED_EVMCS)
53815388
nested_enable_evmcs(vcpu, NULL);
53825389

5383-
vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
5390+
vmx->nested.vmxon_ptr = kvm_state->hdr.vmx.vmxon_pa;
53845391
ret = enter_vmx_operation(vcpu);
53855392
if (ret)
53865393
return ret;
@@ -5389,12 +5396,12 @@ static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
53895396
if (kvm_state->size < sizeof(*kvm_state) + sizeof(*vmcs12))
53905397
return 0;
53915398

5392-
if (kvm_state->vmx.vmcs_pa != -1ull) {
5393-
if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
5394-
!page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
5399+
if (kvm_state->hdr.vmx.vmcs12_pa != -1ull) {
5400+
if (kvm_state->hdr.vmx.vmcs12_pa == kvm_state->hdr.vmx.vmxon_pa ||
5401+
!page_address_valid(vcpu, kvm_state->hdr.vmx.vmcs12_pa))
53955402
return -EINVAL;
53965403

5397-
set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
5404+
set_current_vmptr(vmx, kvm_state->hdr.vmx.vmcs12_pa);
53985405
} else if (kvm_state->flags & KVM_STATE_NESTED_EVMCS) {
53995406
/*
54005407
* Sync eVMCS upon entry as we may not have
@@ -5405,16 +5412,16 @@ static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
54055412
return -EINVAL;
54065413
}
54075414

5408-
if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
5415+
if (kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
54095416
vmx->nested.smm.vmxon = true;
54105417
vmx->nested.vmxon = false;
54115418

5412-
if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
5419+
if (kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
54135420
vmx->nested.smm.guest_mode = true;
54145421
}
54155422

54165423
vmcs12 = get_vmcs12(vcpu);
5417-
if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
5424+
if (copy_from_user(vmcs12, user_vmx_nested_state->vmcs12, sizeof(*vmcs12)))
54185425
return -EFAULT;
54195426

54205427
if (vmcs12->hdr.revision_id != VMCS12_REVISION)
@@ -5431,12 +5438,14 @@ static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
54315438
vmcs12->vmcs_link_pointer != -1ull) {
54325439
struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
54335440

5434-
if (kvm_state->size < sizeof(*kvm_state) + VMCS12_SIZE + sizeof(*vmcs12))
5441+
if (kvm_state->size <
5442+
sizeof(*kvm_state) +
5443+
sizeof(user_vmx_nested_state->vmcs12) + sizeof(*shadow_vmcs12))
54355444
goto error_guest_mode;
54365445

54375446
if (copy_from_user(shadow_vmcs12,
5438-
user_kvm_nested_state->data + VMCS12_SIZE,
5439-
sizeof(*vmcs12))) {
5447+
user_vmx_nested_state->shadow_vmcs12,
5448+
sizeof(*shadow_vmcs12))) {
54405449
ret = -EFAULT;
54415450
goto error_guest_mode;
54425451
}

‎arch/x86/kvm/vmx/vmcs12.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,10 @@ struct __packed vmcs12 {
201201
/*
202202
* VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
203203
* and any VMCS region. Although only sizeof(struct vmcs12) are used by the
204-
* current implementation, 4K are reserved to avoid future complications.
204+
* current implementation, 4K are reserved to avoid future complications and
205+
* to preserve userspace ABI.
205206
*/
206-
#define VMCS12_SIZE 0x1000
207+
#define VMCS12_SIZE KVM_STATE_NESTED_VMX_VMCS_SIZE
207208

208209
/*
209210
* VMCS12_MAX_FIELD_INDEX is the highest index value used in any

‎tools/arch/x86/include/uapi/asm/kvm.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ struct kvm_sync_regs {
392392

393393
struct kvm_vmx_nested_state {
394394
__u64 vmxon_pa;
395-
__u64 vmcs_pa;
395+
__u64 vmcs12_pa;
396396

397397
struct {
398398
__u16 flags;

0 commit comments

Comments
 (0)