Skip to content

Commit 7172c75

Browse files
Binbin Wubonzini
authored andcommitted
KVM: VMX: Move common fields of struct vcpu_{vmx,tdx} to a struct
Move common fields of struct vcpu_vmx and struct vcpu_tdx to struct vcpu_vt, to share the code between VMX/TDX as much as possible and to make TDX exit handling more VMX like. No functional change intended. [Adrian: move code that depends on struct vcpu_vmx back to vmx.h] Suggested-by: Sean Christopherson <seanjc@google.com> Link: https://lore.kernel.org/r/Z1suNzg2Or743a7e@google.com Signed-off-by: Binbin Wu <binbin.wu@linux.intel.com> Signed-off-by: Adrian Hunter <adrian.hunter@intel.com> Message-ID: <20250129095902.16391-5-adrian.hunter@intel.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 69e23fa commit 7172c75

File tree

7 files changed

+176
-136
lines changed

7 files changed

+176
-136
lines changed

arch/x86/kvm/vmx/common.h

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,78 @@
33
#define __KVM_X86_VMX_COMMON_H
44

55
#include <linux/kvm_host.h>
6+
#include <asm/posted_intr.h>
67

78
#include "mmu.h"
89

10+
union vmx_exit_reason {
11+
struct {
12+
u32 basic : 16;
13+
u32 reserved16 : 1;
14+
u32 reserved17 : 1;
15+
u32 reserved18 : 1;
16+
u32 reserved19 : 1;
17+
u32 reserved20 : 1;
18+
u32 reserved21 : 1;
19+
u32 reserved22 : 1;
20+
u32 reserved23 : 1;
21+
u32 reserved24 : 1;
22+
u32 reserved25 : 1;
23+
u32 bus_lock_detected : 1;
24+
u32 enclave_mode : 1;
25+
u32 smi_pending_mtf : 1;
26+
u32 smi_from_vmx_root : 1;
27+
u32 reserved30 : 1;
28+
u32 failed_vmentry : 1;
29+
};
30+
u32 full;
31+
};
32+
33+
struct vcpu_vt {
34+
/* Posted interrupt descriptor */
35+
struct pi_desc pi_desc;
36+
37+
/* Used if this vCPU is waiting for PI notification wakeup. */
38+
struct list_head pi_wakeup_list;
39+
40+
union vmx_exit_reason exit_reason;
41+
42+
unsigned long exit_qualification;
43+
u32 exit_intr_info;
44+
45+
/*
46+
* If true, guest state has been loaded into hardware, and host state
47+
* saved into vcpu_{vt,vmx,tdx}. If false, host state is loaded into
48+
* hardware.
49+
*/
50+
bool guest_state_loaded;
51+
52+
#ifdef CONFIG_X86_64
53+
u64 msr_host_kernel_gs_base;
54+
#endif
55+
56+
unsigned long host_debugctlmsr;
57+
};
58+
59+
#ifdef CONFIG_KVM_INTEL_TDX
60+
61+
static __always_inline bool is_td(struct kvm *kvm)
62+
{
63+
return kvm->arch.vm_type == KVM_X86_TDX_VM;
64+
}
65+
66+
static __always_inline bool is_td_vcpu(struct kvm_vcpu *vcpu)
67+
{
68+
return is_td(vcpu->kvm);
69+
}
70+
71+
#else
72+
73+
static inline bool is_td(struct kvm *kvm) { return false; }
74+
static inline bool is_td_vcpu(struct kvm_vcpu *vcpu) { return false; }
75+
76+
#endif
77+
978
static inline bool vt_is_tdx_private_gpa(struct kvm *kvm, gpa_t gpa)
1079
{
1180
/* For TDX the direct mask is the shared mask. */

arch/x86/kvm/vmx/main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
#include "tdx.h"
1111
#include "tdx_arch.h"
1212

13+
#ifdef CONFIG_KVM_INTEL_TDX
14+
static_assert(offsetof(struct vcpu_vmx, vt) == offsetof(struct vcpu_tdx, vt));
15+
#endif
16+
1317
static void vt_disable_virtualization_cpu(void)
1418
{
1519
/* Note, TDX *and* VMX need to be disabled if TDX is enabled. */

arch/x86/kvm/vmx/nested.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ static void vmx_sync_vmcs_host_state(struct vcpu_vmx *vmx,
275275
{
276276
struct vmcs_host_state *dest, *src;
277277

278-
if (unlikely(!vmx->guest_state_loaded))
278+
if (unlikely(!vmx->vt.guest_state_loaded))
279279
return;
280280

281281
src = &prev->host_state;
@@ -425,7 +425,7 @@ static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
425425
* tables also changed, but KVM should not treat EPT Misconfig
426426
* VM-Exits as writes.
427427
*/
428-
WARN_ON_ONCE(vmx->exit_reason.basic != EXIT_REASON_EPT_VIOLATION);
428+
WARN_ON_ONCE(vmx->vt.exit_reason.basic != EXIT_REASON_EPT_VIOLATION);
429429

430430
/*
431431
* PML Full and EPT Violation VM-Exits both use bit 12 to report
@@ -4622,7 +4622,7 @@ static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
46224622
{
46234623
/* update exit information fields: */
46244624
vmcs12->vm_exit_reason = vm_exit_reason;
4625-
if (to_vmx(vcpu)->exit_reason.enclave_mode)
4625+
if (vmx_get_exit_reason(vcpu).enclave_mode)
46264626
vmcs12->vm_exit_reason |= VMX_EXIT_REASONS_SGX_ENCLAVE_MODE;
46274627
vmcs12->exit_qualification = exit_qualification;
46284628

@@ -6126,7 +6126,7 @@ static int handle_vmfunc(struct kvm_vcpu *vcpu)
61266126
* nested VM-Exit. Pass the original exit reason, i.e. don't hardcode
61276127
* EXIT_REASON_VMFUNC as the exit reason.
61286128
*/
6129-
nested_vmx_vmexit(vcpu, vmx->exit_reason.full,
6129+
nested_vmx_vmexit(vcpu, vmx->vt.exit_reason.full,
61306130
vmx_get_intr_info(vcpu),
61316131
vmx_get_exit_qual(vcpu));
61326132
return 1;
@@ -6571,7 +6571,7 @@ static bool nested_vmx_l1_wants_exit(struct kvm_vcpu *vcpu,
65716571
bool nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu)
65726572
{
65736573
struct vcpu_vmx *vmx = to_vmx(vcpu);
6574-
union vmx_exit_reason exit_reason = vmx->exit_reason;
6574+
union vmx_exit_reason exit_reason = vmx->vt.exit_reason;
65756575
unsigned long exit_qual;
65766576
u32 exit_intr_info;
65776577

arch/x86/kvm/vmx/posted_intr.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static DEFINE_PER_CPU(raw_spinlock_t, wakeup_vcpus_on_cpu_lock);
3333

3434
static inline struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
3535
{
36-
return &(to_vmx(vcpu)->pi_desc);
36+
return &(to_vt(vcpu)->pi_desc);
3737
}
3838

3939
static int pi_try_set_control(struct pi_desc *pi_desc, u64 *pold, u64 new)
@@ -53,7 +53,7 @@ static int pi_try_set_control(struct pi_desc *pi_desc, u64 *pold, u64 new)
5353
void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
5454
{
5555
struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
56-
struct vcpu_vmx *vmx = to_vmx(vcpu);
56+
struct vcpu_vt *vt = to_vt(vcpu);
5757
struct pi_desc old, new;
5858
unsigned long flags;
5959
unsigned int dest;
@@ -90,7 +90,7 @@ void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
9090
*/
9191
if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR) {
9292
raw_spin_lock(&per_cpu(wakeup_vcpus_on_cpu_lock, vcpu->cpu));
93-
list_del(&vmx->pi_wakeup_list);
93+
list_del(&vt->pi_wakeup_list);
9494
raw_spin_unlock(&per_cpu(wakeup_vcpus_on_cpu_lock, vcpu->cpu));
9595
}
9696

@@ -146,14 +146,14 @@ static bool vmx_can_use_vtd_pi(struct kvm *kvm)
146146
static void pi_enable_wakeup_handler(struct kvm_vcpu *vcpu)
147147
{
148148
struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
149-
struct vcpu_vmx *vmx = to_vmx(vcpu);
149+
struct vcpu_vt *vt = to_vt(vcpu);
150150
struct pi_desc old, new;
151151
unsigned long flags;
152152

153153
local_irq_save(flags);
154154

155155
raw_spin_lock(&per_cpu(wakeup_vcpus_on_cpu_lock, vcpu->cpu));
156-
list_add_tail(&vmx->pi_wakeup_list,
156+
list_add_tail(&vt->pi_wakeup_list,
157157
&per_cpu(wakeup_vcpus_on_cpu, vcpu->cpu));
158158
raw_spin_unlock(&per_cpu(wakeup_vcpus_on_cpu_lock, vcpu->cpu));
159159

@@ -220,13 +220,13 @@ void pi_wakeup_handler(void)
220220
int cpu = smp_processor_id();
221221
struct list_head *wakeup_list = &per_cpu(wakeup_vcpus_on_cpu, cpu);
222222
raw_spinlock_t *spinlock = &per_cpu(wakeup_vcpus_on_cpu_lock, cpu);
223-
struct vcpu_vmx *vmx;
223+
struct vcpu_vt *vt;
224224

225225
raw_spin_lock(spinlock);
226-
list_for_each_entry(vmx, wakeup_list, pi_wakeup_list) {
226+
list_for_each_entry(vt, wakeup_list, pi_wakeup_list) {
227227

228-
if (pi_test_on(&vmx->pi_desc))
229-
kvm_vcpu_wake_up(&vmx->vcpu);
228+
if (pi_test_on(&vt->pi_desc))
229+
kvm_vcpu_wake_up(vt_to_vcpu(vt));
230230
}
231231
raw_spin_unlock(spinlock);
232232
}

arch/x86/kvm/vmx/tdx.h

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include "tdx_errno.h"
77

88
#ifdef CONFIG_KVM_INTEL_TDX
9+
#include "common.h"
10+
911
int tdx_bringup(void);
1012
void tdx_cleanup(void);
1113

@@ -45,6 +47,7 @@ enum vcpu_tdx_state {
4547

4648
struct vcpu_tdx {
4749
struct kvm_vcpu vcpu;
50+
struct vcpu_vt vt;
4851

4952
struct tdx_vp vp;
5053

@@ -57,16 +60,6 @@ void tdh_vp_rd_failed(struct vcpu_tdx *tdx, char *uclass, u32 field, u64 err);
5760
void tdh_vp_wr_failed(struct vcpu_tdx *tdx, char *uclass, char *op, u32 field,
5861
u64 val, u64 err);
5962

60-
static inline bool is_td(struct kvm *kvm)
61-
{
62-
return kvm->arch.vm_type == KVM_X86_TDX_VM;
63-
}
64-
65-
static inline bool is_td_vcpu(struct kvm_vcpu *vcpu)
66-
{
67-
return is_td(vcpu->kvm);
68-
}
69-
7063
static __always_inline u64 td_tdcs_exec_read64(struct kvm_tdx *kvm_tdx, u32 field)
7164
{
7265
u64 err, data;
@@ -176,9 +169,6 @@ struct vcpu_tdx {
176169
struct kvm_vcpu vcpu;
177170
};
178171

179-
static inline bool is_td(struct kvm *kvm) { return false; }
180-
static inline bool is_td_vcpu(struct kvm_vcpu *vcpu) { return false; }
181-
182172
#endif
183173

184174
#endif

0 commit comments

Comments
 (0)