Skip to content

Commit a9f2705

Browse files
Lai Jiangshanbonzini
authored andcommitted
KVM: VMX: Save HOST_CR3 in vmx_set_host_fs_gs()
The host CR3 in the vcpu thread can only be changed when scheduling, so commit 15ad976 ("KVM: VMX: Save HOST_CR3 in vmx_prepare_switch_to_guest()") changed vmx.c to only save it in vmx_prepare_switch_to_guest(). However, it also has to be synced in vmx_sync_vmcs_host_state() when switching VMCS. vmx_set_host_fs_gs() is called in both places, so rename it to vmx_set_vmcs_host_state() and make it update HOST_CR3. Fixes: 15ad976 ("KVM: VMX: Save HOST_CR3 in vmx_prepare_switch_to_guest()") Signed-off-by: Lai Jiangshan <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 46cbc04 commit a9f2705

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

arch/x86/kvm/vmx/nested.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,8 @@ static void vmx_sync_vmcs_host_state(struct vcpu_vmx *vmx,
245245
src = &prev->host_state;
246246
dest = &vmx->loaded_vmcs->host_state;
247247

248-
vmx_set_host_fs_gs(dest, src->fs_sel, src->gs_sel, src->fs_base, src->gs_base);
248+
vmx_set_vmcs_host_state(dest, src->cr3, src->fs_sel, src->gs_sel,
249+
src->fs_base, src->gs_base);
249250
dest->ldt_sel = src->ldt_sel;
250251
#ifdef CONFIG_X86_64
251252
dest->ds_sel = src->ds_sel;

arch/x86/kvm/vmx/vmx.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,9 +1069,14 @@ static void pt_guest_exit(struct vcpu_vmx *vmx)
10691069
wrmsrl(MSR_IA32_RTIT_CTL, vmx->pt_desc.host.ctl);
10701070
}
10711071

1072-
void vmx_set_host_fs_gs(struct vmcs_host_state *host, u16 fs_sel, u16 gs_sel,
1073-
unsigned long fs_base, unsigned long gs_base)
1072+
void vmx_set_vmcs_host_state(struct vmcs_host_state *host, unsigned long cr3,
1073+
u16 fs_sel, u16 gs_sel,
1074+
unsigned long fs_base, unsigned long gs_base)
10741075
{
1076+
if (unlikely(cr3 != host->cr3)) {
1077+
vmcs_writel(HOST_CR3, cr3);
1078+
host->cr3 = cr3;
1079+
}
10751080
if (unlikely(fs_sel != host->fs_sel)) {
10761081
if (!(fs_sel & 7))
10771082
vmcs_write16(HOST_FS_SELECTOR, fs_sel);
@@ -1103,7 +1108,6 @@ void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
11031108
#ifdef CONFIG_X86_64
11041109
int cpu = raw_smp_processor_id();
11051110
#endif
1106-
unsigned long cr3;
11071111
unsigned long fs_base, gs_base;
11081112
u16 fs_sel, gs_sel;
11091113
int i;
@@ -1167,14 +1171,8 @@ void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
11671171
gs_base = segment_base(gs_sel);
11681172
#endif
11691173

1170-
vmx_set_host_fs_gs(host_state, fs_sel, gs_sel, fs_base, gs_base);
1171-
1172-
/* Host CR3 including its PCID is stable when guest state is loaded. */
1173-
cr3 = __get_current_cr3_fast();
1174-
if (unlikely(cr3 != host_state->cr3)) {
1175-
vmcs_writel(HOST_CR3, cr3);
1176-
host_state->cr3 = cr3;
1177-
}
1174+
vmx_set_vmcs_host_state(host_state, __get_current_cr3_fast(),
1175+
fs_sel, gs_sel, fs_base, gs_base);
11781176

11791177
vmx->guest_state_loaded = true;
11801178
}

arch/x86/kvm/vmx/vmx.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,9 @@ int allocate_vpid(void);
371371
void free_vpid(int vpid);
372372
void vmx_set_constant_host_state(struct vcpu_vmx *vmx);
373373
void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu);
374-
void vmx_set_host_fs_gs(struct vmcs_host_state *host, u16 fs_sel, u16 gs_sel,
375-
unsigned long fs_base, unsigned long gs_base);
374+
void vmx_set_vmcs_host_state(struct vmcs_host_state *host, unsigned long cr3,
375+
u16 fs_sel, u16 gs_sel,
376+
unsigned long fs_base, unsigned long gs_base);
376377
int vmx_get_cpl(struct kvm_vcpu *vcpu);
377378
bool vmx_emulation_required(struct kvm_vcpu *vcpu);
378379
unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu);

0 commit comments

Comments
 (0)