Skip to content

Commit a584539

Browse files
committed
KVM: x86: pass the whole hflags field to emulator and back
The hflags field will contain information about system management mode and will be useful for the emulator. Pass the entire field rather than just the guest-mode information. Reviewed-by: Radim Krčmář <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 609e36d commit a584539

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

arch/x86/include/asm/kvm_emulate.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,9 @@ enum x86emul_mode {
262262
X86EMUL_MODE_PROT64, /* 64-bit (long) mode. */
263263
};
264264

265+
/* These match some of the HF_* flags defined in kvm_host.h */
266+
#define X86EMUL_GUEST_MASK (1 << 5) /* VCPU is in guest-mode */
267+
265268
struct x86_emulate_ctxt {
266269
const struct x86_emulate_ops *ops;
267270

@@ -273,8 +276,8 @@ struct x86_emulate_ctxt {
273276

274277
/* interruptibility state, as a result of execution of STI or MOV SS */
275278
int interruptibility;
279+
int emul_flags;
276280

277-
bool guest_mode; /* guest running a nested guest */
278281
bool perm_ok; /* do not check permissions if true */
279282
bool ud; /* inject an #UD if host doesn't support insn */
280283

arch/x86/kvm/emulate.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4895,7 +4895,7 @@ int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
48954895
fetch_possible_mmx_operand(ctxt, &ctxt->dst);
48964896
}
48974897

4898-
if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
4898+
if (unlikely(ctxt->emul_flags & X86EMUL_GUEST_MASK) && ctxt->intercept) {
48994899
rc = emulator_check_intercept(ctxt, ctxt->intercept,
49004900
X86_ICPT_PRE_EXCEPT);
49014901
if (rc != X86EMUL_CONTINUE)
@@ -4924,7 +4924,7 @@ int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
49244924
goto done;
49254925
}
49264926

4927-
if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
4927+
if (unlikely(ctxt->emul_flags & X86EMUL_GUEST_MASK) && (ctxt->d & Intercept)) {
49284928
rc = emulator_check_intercept(ctxt, ctxt->intercept,
49294929
X86_ICPT_POST_EXCEPT);
49304930
if (rc != X86EMUL_CONTINUE)
@@ -4978,7 +4978,7 @@ int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
49784978

49794979
special_insn:
49804980

4981-
if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) {
4981+
if (unlikely(ctxt->emul_flags & X86EMUL_GUEST_MASK) && (ctxt->d & Intercept)) {
49824982
rc = emulator_check_intercept(ctxt, ctxt->intercept,
49834983
X86_ICPT_POST_MEMACCESS);
49844984
if (rc != X86EMUL_CONTINUE)

arch/x86/kvm/x86.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5240,7 +5240,8 @@ static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
52405240
(cs_l && is_long_mode(vcpu)) ? X86EMUL_MODE_PROT64 :
52415241
cs_db ? X86EMUL_MODE_PROT32 :
52425242
X86EMUL_MODE_PROT16;
5243-
ctxt->guest_mode = is_guest_mode(vcpu);
5243+
BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK);
5244+
ctxt->emul_flags = vcpu->arch.hflags;
52445245

52455246
init_decode_cache(ctxt);
52465247
vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
@@ -5409,6 +5410,11 @@ static bool retry_instruction(struct x86_emulate_ctxt *ctxt,
54095410
static int complete_emulated_mmio(struct kvm_vcpu *vcpu);
54105411
static int complete_emulated_pio(struct kvm_vcpu *vcpu);
54115412

5413+
void kvm_set_hflags(struct kvm_vcpu *vcpu, unsigned emul_flags)
5414+
{
5415+
vcpu->arch.hflags = emul_flags;
5416+
}
5417+
54125418
static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7,
54135419
unsigned long *db)
54145420
{
@@ -5608,6 +5614,8 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu,
56085614
unsigned long rflags = kvm_x86_ops->get_rflags(vcpu);
56095615
toggle_interruptibility(vcpu, ctxt->interruptibility);
56105616
vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
5617+
if (vcpu->arch.hflags != ctxt->emul_flags)
5618+
kvm_set_hflags(vcpu, ctxt->emul_flags);
56115619
kvm_rip_write(vcpu, ctxt->eip);
56125620
if (r == EMULATE_DONE)
56135621
kvm_vcpu_check_singlestep(vcpu, rflags, &r);

0 commit comments

Comments
 (0)