Skip to content

Commit f48b471

Browse files
Liran Alonbonzini
authored andcommitted
KVM: VMX: Update shared MSRs to be saved/restored on MSR_EFER.LMA changes
When guest transitions from/to long-mode by modifying MSR_EFER.LMA, the list of shared MSRs to be saved/restored on guest<->host transitions is updated (See vmx_set_efer() call to setup_msrs()). On every entry to guest, vcpu_enter_guest() calls vmx_prepare_switch_to_guest(). This function should also take care of setting the shared MSRs to be saved/restored. However, the function does nothing in case we are already running with loaded guest state (vmx->loaded_cpu_state != NULL). This means that even when guest modifies MSR_EFER.LMA which results in updating the list of shared MSRs, it isn't being taken into account by vmx_prepare_switch_to_guest() because it happens while we are running with loaded guest state. To fix above mentioned issue, add a flag to mark that the list of shared MSRs has been updated and modify vmx_prepare_switch_to_guest() to set shared MSRs when running with host state *OR* list of shared MSRs has been updated. Note that this issue was mistakenly introduced by commit 678e315 ("KVM: vmx: add dedicated utility to access guest's kernel_gs_base") because previously vmx_set_efer() always called vmx_load_host_state() which resulted in vmx_prepare_switch_to_guest() to set shared MSRs. Fixes: 678e315 ("KVM: vmx: add dedicated utility to access guest's kernel_gs_base") Reported-by: Eyal Moscovici <[email protected]> Reviewed-by: Mihai Carabas <[email protected]> Reviewed-by: Liam Merwick <[email protected]> Reviewed-by: Jim Mattson <[email protected]> Signed-off-by: Liran Alon <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent bcbfbd8 commit f48b471

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

arch/x86/kvm/vmx.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,7 @@ struct vcpu_vmx {
985985
struct shared_msr_entry *guest_msrs;
986986
int nmsrs;
987987
int save_nmsrs;
988+
bool guest_msrs_dirty;
988989
unsigned long host_idt_base;
989990
#ifdef CONFIG_X86_64
990991
u64 msr_host_kernel_gs_base;
@@ -2898,6 +2899,20 @@ static void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
28982899

28992900
vmx->req_immediate_exit = false;
29002901

2902+
/*
2903+
* Note that guest MSRs to be saved/restored can also be changed
2904+
* when guest state is loaded. This happens when guest transitions
2905+
* to/from long-mode by setting MSR_EFER.LMA.
2906+
*/
2907+
if (!vmx->loaded_cpu_state || vmx->guest_msrs_dirty) {
2908+
vmx->guest_msrs_dirty = false;
2909+
for (i = 0; i < vmx->save_nmsrs; ++i)
2910+
kvm_set_shared_msr(vmx->guest_msrs[i].index,
2911+
vmx->guest_msrs[i].data,
2912+
vmx->guest_msrs[i].mask);
2913+
2914+
}
2915+
29012916
if (vmx->loaded_cpu_state)
29022917
return;
29032918

@@ -2958,11 +2973,6 @@ static void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
29582973
vmcs_writel(HOST_GS_BASE, gs_base);
29592974
host_state->gs_base = gs_base;
29602975
}
2961-
2962-
for (i = 0; i < vmx->save_nmsrs; ++i)
2963-
kvm_set_shared_msr(vmx->guest_msrs[i].index,
2964-
vmx->guest_msrs[i].data,
2965-
vmx->guest_msrs[i].mask);
29662976
}
29672977

29682978
static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
@@ -3437,6 +3447,7 @@ static void setup_msrs(struct vcpu_vmx *vmx)
34373447
move_msr_up(vmx, index, save_nmsrs++);
34383448

34393449
vmx->save_nmsrs = save_nmsrs;
3450+
vmx->guest_msrs_dirty = true;
34403451

34413452
if (cpu_has_vmx_msr_bitmap())
34423453
vmx_update_msr_bitmap(&vmx->vcpu);

0 commit comments

Comments
 (0)