Skip to content

Commit 6ed071f

Browse files
ladiprobonzini
authored andcommitted
KVM: x86: fix emulation of RSM and IRET instructions
On AMD, the effect of set_nmi_mask called by emulate_iret_real and em_rsm on hflags is reverted later on in x86_emulate_instruction where hflags are overwritten with ctxt->emul_flags (the kvm_set_hflags call). This manifests as a hang when rebooting Windows VMs with QEMU, OVMF, and >1 vcpu. Instead of trying to merge ctxt->emul_flags into vcpu->arch.hflags after an instruction is emulated, this commit deletes emul_flags altogether and makes the emulator access vcpu->arch.hflags using two new accessors. This way all changes, on the emulator side as well as in functions called from the emulator and accessing vcpu state with emul_to_vcpu, are preserved. More details on the bug and its manifestation with Windows and OVMF: It's a KVM bug in the interaction between SMI/SMM and NMI, specific to AMD. I believe that the SMM part explains why we started seeing this only with OVMF. KVM masks and unmasks NMI when entering and leaving SMM. When KVM emulates the RSM instruction in em_rsm, the set_nmi_mask call doesn't stick because later on in x86_emulate_instruction we overwrite arch.hflags with ctxt->emul_flags, effectively reverting the effect of the set_nmi_mask call. The AMD-specific hflag of interest here is HF_NMI_MASK. When rebooting the system, Windows sends an NMI IPI to all but the current cpu to shut them down. Only after all of them are parked in HLT will the initiating cpu finish the restart. If NMI is masked, other cpus never get the memo and the initiating cpu spins forever, waiting for hal!HalpInterruptProcessorsStarted to drop. That's the symptom we observe. Fixes: a584539 ("KVM: x86: pass the whole hflags field to emulator and back") Signed-off-by: Ladi Prosek <lprosek@redhat.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 7a97cec commit 6ed071f

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

arch/x86/include/asm/kvm_emulate.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ struct x86_emulate_ops {
221221
void (*get_cpuid)(struct x86_emulate_ctxt *ctxt,
222222
u32 *eax, u32 *ebx, u32 *ecx, u32 *edx);
223223
void (*set_nmi_mask)(struct x86_emulate_ctxt *ctxt, bool masked);
224+
225+
unsigned (*get_hflags)(struct x86_emulate_ctxt *ctxt);
226+
void (*set_hflags)(struct x86_emulate_ctxt *ctxt, unsigned hflags);
224227
};
225228

226229
typedef u32 __attribute__((vector_size(16))) sse128_t;
@@ -290,7 +293,6 @@ struct x86_emulate_ctxt {
290293

291294
/* interruptibility state, as a result of execution of STI or MOV SS */
292295
int interruptibility;
293-
int emul_flags;
294296

295297
bool perm_ok; /* do not check permissions if true */
296298
bool ud; /* inject an #UD if host doesn't support insn */

arch/x86/kvm/emulate.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2547,7 +2547,7 @@ static int em_rsm(struct x86_emulate_ctxt *ctxt)
25472547
u64 smbase;
25482548
int ret;
25492549

2550-
if ((ctxt->emul_flags & X86EMUL_SMM_MASK) == 0)
2550+
if ((ctxt->ops->get_hflags(ctxt) & X86EMUL_SMM_MASK) == 0)
25512551
return emulate_ud(ctxt);
25522552

25532553
/*
@@ -2596,11 +2596,11 @@ static int em_rsm(struct x86_emulate_ctxt *ctxt)
25962596
return X86EMUL_UNHANDLEABLE;
25972597
}
25982598

2599-
if ((ctxt->emul_flags & X86EMUL_SMM_INSIDE_NMI_MASK) == 0)
2599+
if ((ctxt->ops->get_hflags(ctxt) & X86EMUL_SMM_INSIDE_NMI_MASK) == 0)
26002600
ctxt->ops->set_nmi_mask(ctxt, false);
26012601

2602-
ctxt->emul_flags &= ~X86EMUL_SMM_INSIDE_NMI_MASK;
2603-
ctxt->emul_flags &= ~X86EMUL_SMM_MASK;
2602+
ctxt->ops->set_hflags(ctxt, ctxt->ops->get_hflags(ctxt) &
2603+
~(X86EMUL_SMM_INSIDE_NMI_MASK | X86EMUL_SMM_MASK));
26042604
return X86EMUL_CONTINUE;
26052605
}
26062606

@@ -5323,6 +5323,7 @@ int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
53235323
const struct x86_emulate_ops *ops = ctxt->ops;
53245324
int rc = X86EMUL_CONTINUE;
53255325
int saved_dst_type = ctxt->dst.type;
5326+
unsigned emul_flags;
53265327

53275328
ctxt->mem_read.pos = 0;
53285329

@@ -5337,6 +5338,7 @@ int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
53375338
goto done;
53385339
}
53395340

5341+
emul_flags = ctxt->ops->get_hflags(ctxt);
53405342
if (unlikely(ctxt->d &
53415343
(No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) {
53425344
if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
@@ -5370,7 +5372,7 @@ int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
53705372
fetch_possible_mmx_operand(ctxt, &ctxt->dst);
53715373
}
53725374

5373-
if (unlikely(ctxt->emul_flags & X86EMUL_GUEST_MASK) && ctxt->intercept) {
5375+
if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && ctxt->intercept) {
53745376
rc = emulator_check_intercept(ctxt, ctxt->intercept,
53755377
X86_ICPT_PRE_EXCEPT);
53765378
if (rc != X86EMUL_CONTINUE)
@@ -5399,7 +5401,7 @@ int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
53995401
goto done;
54005402
}
54015403

5402-
if (unlikely(ctxt->emul_flags & X86EMUL_GUEST_MASK) && (ctxt->d & Intercept)) {
5404+
if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && (ctxt->d & Intercept)) {
54035405
rc = emulator_check_intercept(ctxt, ctxt->intercept,
54045406
X86_ICPT_POST_EXCEPT);
54055407
if (rc != X86EMUL_CONTINUE)
@@ -5453,7 +5455,7 @@ int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
54535455

54545456
special_insn:
54555457

5456-
if (unlikely(ctxt->emul_flags & X86EMUL_GUEST_MASK) && (ctxt->d & Intercept)) {
5458+
if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && (ctxt->d & Intercept)) {
54575459
rc = emulator_check_intercept(ctxt, ctxt->intercept,
54585460
X86_ICPT_POST_MEMACCESS);
54595461
if (rc != X86EMUL_CONTINUE)

arch/x86/kvm/x86.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5207,6 +5207,16 @@ static void emulator_set_nmi_mask(struct x86_emulate_ctxt *ctxt, bool masked)
52075207
kvm_x86_ops->set_nmi_mask(emul_to_vcpu(ctxt), masked);
52085208
}
52095209

5210+
static unsigned emulator_get_hflags(struct x86_emulate_ctxt *ctxt)
5211+
{
5212+
return emul_to_vcpu(ctxt)->arch.hflags;
5213+
}
5214+
5215+
static void emulator_set_hflags(struct x86_emulate_ctxt *ctxt, unsigned emul_flags)
5216+
{
5217+
kvm_set_hflags(emul_to_vcpu(ctxt), emul_flags);
5218+
}
5219+
52105220
static const struct x86_emulate_ops emulate_ops = {
52115221
.read_gpr = emulator_read_gpr,
52125222
.write_gpr = emulator_write_gpr,
@@ -5246,6 +5256,8 @@ static const struct x86_emulate_ops emulate_ops = {
52465256
.intercept = emulator_intercept,
52475257
.get_cpuid = emulator_get_cpuid,
52485258
.set_nmi_mask = emulator_set_nmi_mask,
5259+
.get_hflags = emulator_get_hflags,
5260+
.set_hflags = emulator_set_hflags,
52495261
};
52505262

52515263
static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
@@ -5298,7 +5310,6 @@ static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
52985310
BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK);
52995311
BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK);
53005312
BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK);
5301-
ctxt->emul_flags = vcpu->arch.hflags;
53025313

53035314
init_decode_cache(ctxt);
53045315
vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
@@ -5702,8 +5713,6 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu,
57025713
unsigned long rflags = kvm_x86_ops->get_rflags(vcpu);
57035714
toggle_interruptibility(vcpu, ctxt->interruptibility);
57045715
vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
5705-
if (vcpu->arch.hflags != ctxt->emul_flags)
5706-
kvm_set_hflags(vcpu, ctxt->emul_flags);
57075716
kvm_rip_write(vcpu, ctxt->eip);
57085717
if (r == EMULATE_DONE)
57095718
kvm_vcpu_check_singlestep(vcpu, rflags, &r);

0 commit comments

Comments
 (0)