deal with interrupt shadow state for emulated instruction
PATCH 1/1 deal with interrupt shadow state for emulated instruction
we currently unblock shadow interrupt state when we skip an instruction, but failing to do so when we actually emulate one. This blocks interrupts in key instruction blocks, in particular sti; hlt; sequences
If the instruction emulated is an sti, we have to block shadow interrupts. The same goes for mov ss. pop ss also needs it, but we don't currently emulate it. For sequences of two or more instructions of the same type among those instructions, only the first one has this effect.
Without this patch, I cannot boot gpxe option roms at vmx machines. This is described at https://bugzilla.redhat.com/show_bug.cgi?id=494469
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/include/asm/kvm_x86_emulate.h | 7 +++++ arch/x86/kvm/svm.c | 19 +++++++++++++- arch/x86/kvm/vmx.c | 42 ++++++++++++++++++++++++------- arch/x86/kvm/x86.c | 6 ++++- arch/x86/kvm/x86_emulate.c | 14 ++++++++++ 6 files changed, 76 insertions(+), 13 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -513,6 +513,7 @@ struct kvm_x86_ops {void (*run)(struct kvm_vcpu *vcpu,struct kvm_run *run);int (*handle_exit)(struct kvm_run *run,struct kvm_vcpu *vcpu);void (*skip_emulated_instruction)(struct kvm_vcpu *vcpu); +void (*interrupt_shadow_mask)(struct kvm_vcpu *vcpu,int mask);void (*patch_hypercall)(struct kvm_vcpu *vcpu,unsigned char *hypercall_addr);int (*get_irq)(struct kvm_vcpu *vcpu);
arch/x86/include/asm/kvm_x86_emulate.h +7 -0
@@ -143,6 +143,9 @@ struct decode_cache {struct fetch_cache fetch; }; +#define X86_SHADOW_INT_MOV_SS 1 +#define X86_SHADOW_INT_STI 2 +struct x86_emulate_ctxt {/* Register state before/after emulation. */ struct kvm_vcpu *vcpu;
@@ -152,6 +155,10 @@ struct x86_emulate_ctxt {int mode;u32 cs_base; +/* interruptibility state, as a result of execution of STI or MOV SS */ +int interruptibility; +int movss_int_flag, movss_int_flag_old; +/* decode cache */ struct decode_cache decode; };
arch/x86/kvm/svm.c +17 -2
@@ -210,6 +210,21 @@ static int is_external_interrupt(u32 info)return info == (SVM_EVTINJ_VALID |SVM_EVTINJ_TYPE_INTR ); } +static void svm_interrupt_shadow_mask (struct kvm_vcpu *vcpu,int mask) +{ +struct vcpu_svm *svm =to_svm (vcpu); + +if (mask ==0 ) + svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK ; +else + svm->vmcb->control.int_state |=SVM_INTERRUPT_SHADOW_MASK ; + + svm->vcpu.arch.interrupt_window_open = + (!(svm->vmcb->control.int_state &SVM_INTERRUPT_SHADOW_MASK ) && + (svm->vmcb->save.rflags &X86_EFLAGS_IF ) && + (svm->vcpu.arch.hflags &HF_GIF_MASK )); +} +static void skip_emulated_instruction (struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu);
@@ -223,9 +238,8 @@ static void skip_emulated_instruction(struct kvm_vcpu *vcpu) __func__, kvm_rip_read(vcpu), svm->next_rip);kvm_rip_write (vcpu, svm->next_rip); - svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK ; - vcpu->arch.interrupt_window_open = (svm->vcpu.arch.hflags &HF_GIF_MASK ); +svm_interrupt_shadow_mask (vcpu,0 ); }static int has_svm (void )
@@ -2660,6 +2674,7 @@ static struct kvm_x86_ops svm_x86_ops = { .run = svm_vcpu_run, .handle_exit = handle_exit, .skip_emulated_instruction = skip_emulated_instruction, + .interrupt_shadow_mask = svm_interrupt_shadow_mask, .patch_hypercall = svm_patch_hypercall, .get_irq = svm_get_irq, .set_irq = svm_set_irq,
arch/x86/kvm/vmx.c +32 -10
64 lines not on this page · raw message
arch/x86/kvm/x86.c +5 -1
@@ -2364,7 +2364,7 @@ int emulate_instruction(struct kvm_vcpu *vcpu,u16 error_code, int emulation_type) { - int r; + int r, shadow_mask;struct decode_cache *c;kvm_clear_exception_queue (vcpu);
@@ -2412,8 +2412,12 @@ int emulate_instruction(struct kvm_vcpu *vcpu, } } + vcpu->arch.emulate_ctxt.interruptibility =0 ; r =x86_emulate_insn (&vcpu->arch.emulate_ctxt, &emulate_ops); + shadow_mask = vcpu->arch.emulate_ctxt.interruptibility; + kvm_x86_ops->interrupt_shadow_mask (vcpu, shadow_mask); +if (vcpu->arch.pio.string)return EMULATE_DO_MMIO ;
arch/x86/kvm/x86_emulate.c +14 -0
@@ -1360,6 +1360,10 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)int io_dir_in;int rc =0 ; + ctxt->movss_int_flag_old = ctxt->movss_int_flag; + + ctxt->movss_int_flag =0 ; +/* Shadow copy of register state. Committed on successful emulation. * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't * modify them.
@@ -1610,6 +1614,14 @@ special_insn: sel = c->src.val;if (c->modrm_reg <=5 ) { +if (c->modrm_reg ==VCPU_SREG_SS ) { +if (ctxt->movss_int_flag_old) + ctxt->interruptibility |= +X86_SHADOW_INT_MOV_SS ; +else + ctxt->movss_int_flag =1 ; + } + type_bits = (c->modrm_reg ==1 ) ?9 :1 ; err =kvm_load_segment_descriptor (ctxt->vcpu, sel, type_bits, c->modrm_reg);
@@ -1864,6 +1876,8 @@ special_insn: c->dst.type =OP_NONE ;/* Disable writeback. */ break ;case 0xfb :/* sti */ +if (!(ctxt->eflags &X86_EFLAGS_IF )) + ctxt->interruptibility |=X86_SHADOW_INT_STI ; ctxt->eflags |=X86_EFLAGS_IF ; c->dst.type =OP_NONE ;/* Disable writeback. */ break ;
Signature · 1 line
1.5.6.6
arch/x86/include/asm/kvm_host.h struct kvm_x86_ops { · line 516
Quoted 9 lines · Glauber Costa
arch/x86/include/asm/kvm_host.h +1
-0@@ -513,6 +513,7 @@ struct kvm_x86_ops {void (*run)(struct kvm_vcpu *vcpu,struct kvm_run *run);int (*handle_exit)(struct kvm_run *run,struct kvm_vcpu *vcpu);void (*skip_emulated_instruction)(struct kvm_vcpu *vcpu); +void (*interrupt_shadow_mask)(struct kvm_vcpu *vcpu,int mask);
Note that this conflicts with my "Move interrupt injection logic to x86.c" patch. It adds drop_interrupt_shadow callback. Your callback is more general so it should replace mine when/if my patchset will go in.
--
Gleb.
Quoted 4 lines · Glauber Costa
we currently unblock shadow interrupt state when we skip an instruction, but failing to do so when we actually emulate one. This blocks interrupts in key instruction blocks, in particular sti; hlt; sequences
Quoted 9 lines
If the instruction emulated is an sti, we have to block shadow interrupts. The same goes for mov ss. pop ss also needs it, but we don't currently emulate it. For sequences of two or more instructions of the same type among those instructions, only the first one has this effect.
Without this patch, I cannot boot gpxe option roms at vmx machines. This is described at https://bugzilla.redhat.com/show_bug.cgi?id=494469
We'll defer this until after Gleb's patchset, since that's much bigger.
Quoted 7 lines
arch/x86/include/asm/kvm_x86_emulate.h +3
-0+#define X86_SHADOW_INT_MOV_SS 1 +#define X86_SHADOW_INT_STI 2 +struct x86_emulate_ctxt {/* Register state before/after emulation. */ struct kvm_vcpu *vcpu;
arch/x86/include/asm/kvm_x86_emulate.h struct x86_emulate_ctxt { · line 162
Quoted 8 lines
arch/x86/include/asm/kvm_x86_emulate.h +4
-0@@ -152,6 +155,10 @@ struct x86_emulate_ctxt {int mode;u32 cs_base; +/* interruptibility state, as a result of execution of STI or MOV SS */ +int interruptibility; +int movss_int_flag, movss_int_flag_old; +
bit masks are traditionally unsigned.
Quoted 14 lines
arch/x86/kvm/x86.c +1
-1@@ -2364,7 +2364,7 @@ int emulate_instruction(struct kvm_vcpu *vcpu,u16 error_code, int emulation_type) { - int r; + int r, shadow_mask;struct decode_cache *c;kvm_clear_exception_queue (vcpu);
arch/x86/kvm/x86.c int emulate_instruction(struct kvm_vcpu *vcpu, · line 2421
Quoted 10 lines
arch/x86/kvm/x86.c +4
-0@@ -2412,8 +2412,12 @@ int emulate_instruction(struct kvm_vcpu *vcpu, } } + vcpu->arch.emulate_ctxt.interruptibility =0 ; r =x86_emulate_insn (&vcpu->arch.emulate_ctxt, &emulate_ops); + shadow_mask = vcpu->arch.emulate_ctxt.interruptibility; + kvm_x86_ops->interrupt_shadow_mask (vcpu, shadow_mask); +
Emulation may have failed, in which case you don't want to update the interrupt shadow mask.
Quoted 8 lines
arch/x86/kvm/x86.c +0
-0if (vcpu->arch.pio.string)return EMULATE_DO_MMIO ;arch/x86/kvm/x86_emulate.c +0
-0
arch/x86/kvm/x86_emulate.c x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) · line 1366
Quoted 7 lines
arch/x86/kvm/x86_emulate.c +3
-0@@ -1360,6 +1360,10 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)int io_dir_in;int rc =0 ; + ctxt->movss_int_flag_old = ctxt->movss_int_flag; + + ctxt->movss_int_flag =0 ;
This seem to be internal to the emulator. However, instructions may be executed outside the emulator, invalidating movss_int_flag. But see below.
arch/x86/kvm/x86_emulate.c special_insn: · line 1624
Quoted 12 lines
arch/x86/kvm/x86_emulate.c +7
-0@@ -1610,6 +1614,14 @@ special_insn: sel = c->src.val;if (c->modrm_reg <=5 ) { +if (c->modrm_reg ==VCPU_SREG_SS ) { +if (ctxt->movss_int_flag_old) + ctxt->interruptibility |= +X86_SHADOW_INT_MOV_SS ; +else + ctxt->movss_int_flag =1 ; + }
The comment about repeating 'mov ss' in the manual has that wonderful word in it, May. That means we're perfectly allowed to ignore it and just set the flag unconditionally.
I doubt we'll ever see a repeated 'mov ss', once is more than enough.
Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this signature is too narrow to contain.
H. Peter Anvin
reviewerQuoted 5 lines · Avi Kivity
The comment about repeating 'mov ss' in the manual has that wonderful word in it, May. That means we're perfectly allowed to ignore it and just set the flag unconditionally.
Realistically, though, this should only be done for a limited number of sequential instructions.
Quoted 1 line
I doubt we'll ever see a repeated 'mov ss', once is more than enough.
True enough, except maliciously.
-hpa
Signature · 2 lines
H. Peter Anvin, Intel Open Source Technology Center I work for Intel. I don't speak on their behalf.
Quoted 12 lines · H. Peter Anvin
Quoted 5 lines · Avi Kivity
The comment about repeating 'mov ss' in the manual has that wonderful word in it, May. That means we're perfectly allowed to ignore it and just set the flag unconditionally.
Realistically, though, this should only be done for a limited number of sequential instructions.
Why? Do you see a guest filling all of memory with 'mov ss' and expecting to break out of it via an interrupt?
Quoted 5 lines
Quoted 2 lines
I doubt we'll ever see a repeated 'mov ss', once is more than enough.
True enough, except maliciously.
Why do we care? The guest can only harm itself, and if it wants to disable interrupts, it would be a lot easier for it to run a plain 'cli'.
I guess it would be a problem if we emulated 'mov ss' for ordinary userspace or vm86 mode, but we don't.
Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this signature is too narrow to contain.
H. Peter Anvin
reviewerQuoted 7 lines · Avi Kivity
Why do we care? The guest can only harm itself, and if it wants to disable interrupts, it would be a lot easier for it to run a plain 'cli'.
I guess it would be a problem if we emulated 'mov ss' for ordinary userspace or vm86 mode, but we don't.
Well, the answer is that mov ss is an unprivileged instruction.
-hpa
Signature · 2 lines
H. Peter Anvin, Intel Open Source Technology Center I work for Intel. I don't speak on their behalf.
Quoted 2 lines
Why? Do you see a guest filling all of memory with 'mov ss' and expecting to break out of it via an interrupt?
Well I did try mapping a page of move ss all through memory on real hardware long ago and seeing what happened on a 386 in real mode with DOSEMU. I was disappointed ;)
H. Peter Anvin
reviewerQuoted 6 lines · Alan Cox
Quoted 1 line
Why? Do you see a guest filling all of memory with 'mov ss' and expecting to break out of it via an interrupt?
Well I did try mapping a page of move ss all through memory on real hardware long ago and seeing what happened on a 386 in real mode with DOSEMU. I was disappointed ;)
Heheheheh....
-hpa
Quoted 5 lines · H. Peter Anvin
Quoted 3 lines · Avi Kivity
Why do we care? The guest can only harm itself, and if it wants to disable interrupts, it would be a lot easier for it to run a plain 'cli'.
Quoted 8 lines
Quoted 4 lines
I guess it would be a problem if we emulated 'mov ss' for ordinary userspace or vm86 mode, but we don't.
Well, the answer is that mov ss is an unprivileged instruction.
We don't emulate guest user mode.
Well, if guest userspace can convince its kernel to give it access to some memory mapped I/O register, I guess it can execute repeated 'mov ss, mmio' and starve the guest kernel.
Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
H. Peter Anvin
reviewerQuoted 5 lines · Avi Kivity
We don't emulate guest user mode.
Well, if guest userspace can convince its kernel to give it access to some memory mapped I/O register, I guess it can execute repeated 'mov ss, mmio' and starve the guest kernel.
It doesn't need a MMIO register to do that, even.
-hpa
Quoted 9 lines · H. Peter Anvin
Quoted 5 lines · Avi Kivity
We don't emulate guest user mode.
Well, if guest userspace can convince its kernel to give it access to some memory mapped I/O register, I guess it can execute repeated 'mov ss, mmio' and starve the guest kernel.
It doesn't need a MMIO register to do that, even.
Can you explain how?
Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.