Skip to content

Commit 15ad976

Browse files
Lai Jiangshanbonzini
authored andcommitted
KVM: VMX: Save HOST_CR3 in vmx_prepare_switch_to_guest()
The host CR3 in the vcpu thread can only be changed when scheduling. Moving the code in vmx_prepare_switch_to_guest() makes the code simpler. Signed-off-by: Lai Jiangshan <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 3ab4ac8 commit 15ad976

File tree

2 files changed

+11
-14
lines changed

2 files changed

+11
-14
lines changed

arch/x86/kvm/vmx/nested.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3033,7 +3033,7 @@ static int nested_vmx_check_guest_state(struct kvm_vcpu *vcpu,
30333033
static int nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu)
30343034
{
30353035
struct vcpu_vmx *vmx = to_vmx(vcpu);
3036-
unsigned long cr3, cr4;
3036+
unsigned long cr4;
30373037
bool vm_fail;
30383038

30393039
if (!nested_early_check)
@@ -3056,12 +3056,6 @@ static int nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu)
30563056
*/
30573057
vmcs_writel(GUEST_RFLAGS, 0);
30583058

3059-
cr3 = __get_current_cr3_fast();
3060-
if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
3061-
vmcs_writel(HOST_CR3, cr3);
3062-
vmx->loaded_vmcs->host_state.cr3 = cr3;
3063-
}
3064-
30653059
cr4 = cr4_read_shadow();
30663060
if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
30673061
vmcs_writel(HOST_CR4, cr4);

arch/x86/kvm/vmx/vmx.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,7 @@ void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
11031103
#ifdef CONFIG_X86_64
11041104
int cpu = raw_smp_processor_id();
11051105
#endif
1106+
unsigned long cr3;
11061107
unsigned long fs_base, gs_base;
11071108
u16 fs_sel, gs_sel;
11081109
int i;
@@ -1167,6 +1168,14 @@ void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
11671168
#endif
11681169

11691170
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+
}
1178+
11701179
vmx->guest_state_loaded = true;
11711180
}
11721181

@@ -6612,7 +6621,7 @@ static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu,
66126621
static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu)
66136622
{
66146623
struct vcpu_vmx *vmx = to_vmx(vcpu);
6615-
unsigned long cr3, cr4;
6624+
unsigned long cr4;
66166625

66176626
/* Record the guest's net vcpu time for enforced NMI injections. */
66186627
if (unlikely(!enable_vnmi &&
@@ -6657,12 +6666,6 @@ static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu)
66576666
vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
66586667
vcpu->arch.regs_dirty = 0;
66596668

6660-
cr3 = __get_current_cr3_fast();
6661-
if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
6662-
vmcs_writel(HOST_CR3, cr3);
6663-
vmx->loaded_vmcs->host_state.cr3 = cr3;
6664-
}
6665-
66666669
cr4 = cr4_read_shadow();
66676670
if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
66686671
vmcs_writel(HOST_CR4, cr4);

0 commit comments

Comments
 (0)