Emulator INTn and SCAS fixes
cover Emulator INTn and SCAS fixes
The following patchset makes INTn work and implements SCAS (used by vgabios). With the patchset, vgabios is able to display its splash screen but gets confused shortly afterwards.
Based on the non-atomic-injection branch.
Avi Kivity (4):
KVM: Initialize operand and address sizes before emulating interrupts
KVM: x86 emulator: fix INTn emulation not pushing EFLAGS and CS
KVM: x86 emulator: implement SCAS (opcodes AE, AF)
KVM: x86 emulator: fix REPZ/REPNZ termination condition
arch/x86/kvm/emulate.c | 60 +++++++++++++++++++++++++++++------------------ arch/x86/kvm/x86.c | 2 + 2 files changed, 39 insertions(+), 23 deletions(-)
PATCH 1/4 KVM: Initialize operand and address sizes before emulating interrupts
The emulator needs the operand and address sizes to be valid.
--- arch/x86/kvm/x86.c | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)
arch/x86/kvm/x86.c +2 -0
@@ -3965,6 +3965,8 @@ int kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq)init_emulate_ctxt (vcpu); + vcpu->arch.emulate_ctxt.decode.op_bytes =2 ; + vcpu->arch.emulate_ctxt.decode.ad_bytes =2 ; ret =emulate_int_real (&vcpu->arch.emulate_ctxt, &emulate_ops, irq);if (ret !=X86EMUL_CONTINUE )
Signature · 1 line
1.7.1
PATCH 2/4 KVM: x86 emulator: fix INTn emulation not pushing EFLAGS and CS
In reply to Avi Kivityemulate_push() only schedules a push; it doesn't actually push anything. Call writeback() to flush out the write.
--- arch/x86/kvm/emulate.c | 13 ++++++++++++- 1 files changed, 12 insertions(+), 1 deletions(-)
arch/x86/kvm/emulate.c +12 -1
@@ -1228,7 +1228,7 @@ int emulate_int_real(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops, int irq) {struct decode_cache *c = &ctxt->decode; -int rc =X86EMUL_CONTINUE ; +int rc;struct desc_ptr dt;gva_t cs_addr;gva_t eip_addr;
@@ -1238,14 +1238,25 @@ int emulate_int_real(struct x86_emulate_ctxt *ctxt,/* TODO: Add limit checks */ c->src.val = ctxt->eflags;emulate_push (ctxt, ops); + rc =writeback (ctxt, ops); +if (rc !=X86EMUL_CONTINUE ) +return rc; ctxt->eflags &= ~(EFLG_IF |EFLG_TF |EFLG_AC ); c->src.val = ops->get_segment_selector (VCPU_SREG_CS , ctxt->vcpu);emulate_push (ctxt, ops); + rc =writeback (ctxt, ops); +if (rc !=X86EMUL_CONTINUE ) +return rc; c->src.val = c->eip;emulate_push (ctxt, ops); + rc =writeback (ctxt, ops); +if (rc !=X86EMUL_CONTINUE ) +return rc; + + c->dst.type =OP_NONE ; ops->get_idt (&dt, ctxt->vcpu);
Signature · 1 line
1.7.1
PATCH 3/4 KVM: x86 emulator: implement SCAS (opcodes AE, AF)
In reply to Avi Kivity--- arch/x86/kvm/emulate.c | 5 ++--- 1 files changed, 2 insertions(+), 3 deletions(-)
arch/x86/kvm/emulate.c +2 -3
@@ -2308,7 +2308,7 @@ static struct opcode opcode_table[256] = {D (DstAcc | SrcImmByte | ByteOp),D (DstAcc | SrcImm),D (ByteOp | SrcAcc | DstDI | Mov | String),D (SrcAcc | DstDI | Mov | String),D (ByteOp | SrcSI | DstAcc | Mov | String),D (SrcSI | DstAcc | Mov | String), -D (ByteOp | DstDI | String),D (DstDI | String), +D (ByteOp | SrcAcc | DstDI | String),D (SrcAcc | DstDI | String),/* 0xB0 - 0xB7 */ X8(D(ByteOp | DstReg | SrcImm | Mov)),/* 0xB8 - 0xBF */
@@ -3068,8 +3068,7 @@ special_insn: case 0xac ... 0xad: /* lods */ goto mov; case 0xae ... 0xaf: /* scas */ -DPRINTF ("Urk! I don't handle SCAS.\n" ); - goto cannot_emulate; + goto cmp; case 0xb0 ... 0xbf: /* mov r, imm */ goto mov; case 0xc0 ... 0xc1:
Signature · 1 line
1.7.1
PATCH 4/4 KVM: x86 emulator: fix REPZ/REPNZ termination condition
In reply to Avi KivityEFLAGS.ZF needs to be checked after each iteration, not before.
--- arch/x86/kvm/emulate.c | 42 +++++++++++++++++++++++------------------- 1 files changed, 23 insertions(+), 19 deletions(-)
arch/x86/kvm/emulate.c +23 -19
@@ -2747,6 +2747,7 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt)int rc =X86EMUL_CONTINUE ;int saved_dst_type = c->dst.type;int irq;/* Used for int 3, int, and into */ +ulong old_eip; ctxt->decode.mem_read.pos =0 ;
@@ -2771,28 +2772,10 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt) ctxt->restart = true;/* All REP prefixes have the same first termination condition */ if (address_mask (c, c->regs[VCPU_REGS_RCX ]) ==0 ) { - string_done: ctxt->restart = false; ctxt->eip = c->eip; goto done; } -/* The second termination condition only applies for REPE -* and REPNE. Test if the repeat string operation prefix is -* REPE/REPZ or REPNE/REPNZ and if it's the case it tests the -* corresponding termination condition according to: -* - if REPE/REPZ and ZF = 0 then done -* - if REPNE/REPNZ and ZF = 1 then done -*/ -if ((c->b ==0xa6 ) || (c->b ==0xa7 ) || - (c->b ==0xae ) || (c->b ==0xaf )) { -if ((c->rep_prefix ==REPE_PREFIX ) && - ((ctxt->eflags &EFLG_ZF ) ==0 )) - goto string_done; -if ((c->rep_prefix ==REPNE_PREFIX ) && - ((ctxt->eflags &EFLG_ZF ) ==EFLG_ZF )) - goto string_done; - } - c->eip = ctxt->eip; }if (c->src.type ==OP_MEM ) {
@@ -3229,6 +3212,7 @@ special_insn: } writeback: + old_eip = c->eip; rc =writeback (ctxt, ops);if (rc !=X86EMUL_CONTINUE ) goto done;
@@ -3250,13 +3234,33 @@ writeback:if (c->rep_prefix && (c->d & String)) {struct read_cache *rc = &ctxt->decode.io_read;register_address_increment (c, &c->regs[VCPU_REGS_RCX ],-1 ); +/* The second termination condition only applies for REPE +* and REPNE. Test if the repeat string operation prefix is +* REPE/REPZ or REPNE/REPNZ and if it's the case it tests the +* corresponding termination condition according to: +* - if REPE/REPZ and ZF = 0 then done +* - if REPNE/REPNZ and ZF = 1 then done +*/ +if ((c->b ==0xa6 ) || (c->b ==0xa7 ) || + (c->b ==0xae ) || (c->b ==0xaf )) { +trace_printk ("c->eip %lx ctxt->eip %lx\n" , + c->eip, ctxt->eip); +if (((c->rep_prefix ==REPE_PREFIX ) && + ((ctxt->eflags &EFLG_ZF ) ==0 )) + || ((c->rep_prefix ==REPNE_PREFIX ) && + ((ctxt->eflags &EFLG_ZF ) ==EFLG_ZF ))) { + ctxt->restart = false; + } + }/* * Re-enter guest when pio read ahead buffer is empty or, * if it is not used, after each 1024 iteration. */ if ((rc->end ==0 && !(c->regs[VCPU_REGS_RCX ] &0x3ff )) || - (rc->end !=0 && rc->end == rc->pos)) + (rc->end !=0 && rc->end == rc->pos)) { ctxt->restart = false; + c->eip = ctxt->eip; + } }/* * reset read cache here in case string instruction is restared
Signature · 1 line
1.7.1
Re: [PATCH 4/4] KVM: x86 emulator: fix REPZ/REPNZ termination condition
Quoted 12 lines · Avi Kivity
EFLAGS.ZF needs to be checked after each iteration, not before.
Signed-off-by Avi Kivity <[email protected]>--- arch/x86/kvm/emulate.c | 42 +++++++++++++++++++++++------------------- 1 files changed, 23 insertions(+), 19 deletions(-)arch/x86/kvm/emulate.c +0
-0
arch/x86/kvm/emulate.c x86_emulate_insn(struct x86_emulate_ctxt *ctxt) · line 2750
Quoted 4 lines
arch/x86/kvm/emulate.c +1
-0@@ -2747,6 +2747,7 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt)int rc =X86EMUL_CONTINUE ;int saved_dst_type = c->dst.type;int irq;/* Used for int 3, int, and into */ +ulong old_eip;
Is never used.
Quoted 50 lines
arch/x86/kvm/emulate.c +7
-18ctxt->decode.mem_read.pos =0 ;@@ -2771,28 +2772,10 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt) ctxt->restart = true;/* All REP prefixes have the same first termination condition */ if (address_mask (c, c->regs[VCPU_REGS_RCX ]) ==0 ) { - string_done: ctxt->restart = false; ctxt->eip = c->eip; goto done; } -/* The second termination condition only applies for REPE -* and REPNE. Test if the repeat string operation prefix is -* REPE/REPZ or REPNE/REPNZ and if it's the case it tests the -* corresponding termination condition according to: -* - if REPE/REPZ and ZF = 0 then done -* - if REPNE/REPNZ and ZF = 1 then done -*/ -if ((c->b ==0xa6 ) || (c->b ==0xa7 ) || - (c->b ==0xae ) || (c->b ==0xaf )) { -if ((c->rep_prefix ==REPE_PREFIX ) && - ((ctxt->eflags &EFLG_ZF ) ==0 )) - goto string_done; -if ((c->rep_prefix ==REPNE_PREFIX ) && - ((ctxt->eflags &EFLG_ZF ) ==EFLG_ZF )) - goto string_done; - } - c->eip = ctxt->eip; }if (c->src.type ==OP_MEM ) {@@ -3229,6 +3212,7 @@ special_insn: } writeback: + old_eip = c->eip; rc =writeback (ctxt, ops);if (rc !=X86EMUL_CONTINUE ) goto done;@@ -3250,13 +3234,33 @@ writeback:if (c->rep_prefix && (c->d & String)) {struct read_cache *rc = &ctxt->decode.io_read;register_address_increment (c, &c->regs[VCPU_REGS_RCX ],-1 ); +/* The second termination condition only applies for REPE +* and REPNE. Test if the repeat string operation prefix is +* REPE/REPZ or REPNE/REPNZ and if it's the case it tests the +* corresponding termination condition according to: +* - if REPE/REPZ and ZF = 0 then done +* - if REPNE/REPNZ and ZF = 1 then done
arch/x86/kvm/emulate.c writeback: · line 3252
Quoted 10 lines
arch/x86/kvm/emulate.c +10
-0+*/ +if ((c->b ==0xa6 ) || (c->b ==0xa7 ) || + (c->b ==0xae ) || (c->b ==0xaf )) { +trace_printk ("c->eip %lx ctxt->eip %lx\n" , + c->eip, ctxt->eip); +if (((c->rep_prefix ==REPE_PREFIX ) && + ((ctxt->eflags &EFLG_ZF ) ==0 )) + || ((c->rep_prefix ==REPNE_PREFIX ) && + ((ctxt->eflags &EFLG_ZF ) ==EFLG_ZF ))) { + ctxt->restart = false;
Why not jump to string_done label here?
arch/x86/kvm/emulate.c writeback: · line 3262
Quoted 11 lines
arch/x86/kvm/emulate.c +4
-1+ } + }/* * Re-enter guest when pio read ahead buffer is empty or, * if it is not used, after each 1024 iteration. */ if ((rc->end ==0 && !(c->regs[VCPU_REGS_RCX ] &0x3ff )) || - (rc->end !=0 && rc->end == rc->pos)) + (rc->end !=0 && rc->end == rc->pos)) { ctxt->restart = false; + c->eip = ctxt->eip;
We can get here when instruction is completed by above "if", so same instruction will reexecute once again.
Quoted 11 lines
arch/x86/kvm/emulate.c +1
-0+ } }/* * reset read cache here in case string instruction is restared Signature · 6 lines
1.7.1 -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
--
Gleb.
Re: [PATCH 4/4] KVM: x86 emulator: fix REPZ/REPNZ termination condition
Quoted 8 lines · Gleb Natapov
Quoted 7 lines · Avi Kivity
EFLAGS.ZF needs to be checked after each iteration, not before.
Signed-off-by Avi Kivity<[email protected]>--- arch/x86/kvm/emulate.c | 42 +++++++++++++++++++++++------------------- 1 files changed, 23 insertions(+), 19 deletions(-)
Quoted 11 lines
arch/x86/kvm/emulate.c x86_emulate_insn(struct x86_emulate_ctxt *ctxt) · line 2750
Quoted 9 lines
arch/x86/kvm/emulate.c +1
-0@@ -2747,6 +2747,7 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt)int rc =X86EMUL_CONTINUE ;int saved_dst_type = c->dst.type;int irq;/* Used for int 3, int, and into */ +ulong old_eip;Is never used.
Whoops.
Quoted 13 lines
Quoted 13 lines
arch/x86/kvm/emulate.c +9
-0@@ -3250,13 +3234,33 @@ writeback:if (c->rep_prefix&& (c->d& String)) {struct read_cache *rc =&ctxt->decode.io_read;register_address_increment (c,&c->regs[VCPU_REGS_RCX ],-1 ); +/* The second termination condition only applies for REPE +* and REPNE. Test if the repeat string operation prefix is +* REPE/REPZ or REPNE/REPNZ and if it's the case it tests the +* corresponding termination condition according to: +* - if REPE/REPZ and ZF = 0 then done +* - if REPNE/REPNZ and ZF = 1 then done +*/ +if ((c->b ==0xa6 ) || (c->b ==0xa7 ) || + (c->b ==0xae ) || (c->b ==0xaf )) {
Quoted 8 lines
arch/x86/kvm/emulate.c writeback: · line 3252
Quoted 7 lines
arch/x86/kvm/emulate.c +7
-0+trace_printk ("c->eip %lx ctxt->eip %lx\n" , + c->eip, ctxt->eip); +if (((c->rep_prefix ==REPE_PREFIX )&& + ((ctxt->eflags&EFLG_ZF ) ==0 )) + || ((c->rep_prefix ==REPNE_PREFIX )&& + ((ctxt->eflags&EFLG_ZF ) ==EFLG_ZF ))) { + ctxt->restart = false;Why not jump to
string_donelabel here?
It does a 'goto done;' which skips a couple of things.
Quoted 5 lines
Quoted 5 lines
arch/x86/kvm/emulate.c +2
-0+ } + }/* * Re-enter guest when pio read ahead buffer is empty or, * if it is not used, after each 1024 iteration.
Quoted 8 lines
arch/x86/kvm/emulate.c writeback: · line 3262
Quoted 6 lines
arch/x86/kvm/emulate.c +2
-1*/ if ((rc->end ==0 && !(c->regs[VCPU_REGS_RCX ]&0x3ff )) || - (rc->end !=0 && rc->end == rc->pos)) + (rc->end !=0 && rc->end == rc->pos)) { ctxt->restart = false; + c->eip = ctxt->eip;We can get here when instruction is completed by above "if", so same instruction will reexecute once again.
Not good. Will redo (and write tests).
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 4/4] KVM: x86 emulator: fix REPZ/REPNZ termination condition
Quoted 45 lines · Avi Kivity
Quoted 8 lines · Gleb Natapov
Quoted 7 lines · Avi Kivity
EFLAGS.ZF needs to be checked after each iteration, not before.
Signed-off-by Avi Kivity<[email protected]>--- arch/x86/kvm/emulate.c | 42 +++++++++++++++++++++++------------------- 1 files changed, 23 insertions(+), 19 deletions(-)Quoted 11 lines
arch/x86/kvm/emulate.c x86_emulate_insn(struct x86_emulate_ctxt *ctxt) · line 2750
Quoted 9 lines
arch/x86/kvm/emulate.c +1
-0@@ -2747,6 +2747,7 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt)int rc =X86EMUL_CONTINUE ;int saved_dst_type = c->dst.type;int irq;/* Used for int 3, int, and into */ +ulong old_eip;Is never used.
Whoops.
Quoted 13 lines
Quoted 13 lines
arch/x86/kvm/emulate.c +9
-0@@ -3250,13 +3234,33 @@ writeback:if (c->rep_prefix&& (c->d& String)) {struct read_cache *rc =&ctxt->decode.io_read;register_address_increment (c,&c->regs[VCPU_REGS_RCX ],-1 ); +/* The second termination condition only applies for REPE +* and REPNE. Test if the repeat string operation prefix is +* REPE/REPZ or REPNE/REPNZ and if it's the case it tests the +* corresponding termination condition according to: +* - if REPE/REPZ and ZF = 0 then done +* - if REPNE/REPNZ and ZF = 1 then done +*/ +if ((c->b ==0xa6 ) || (c->b ==0xa7 ) || + (c->b ==0xae ) || (c->b ==0xaf )) {Quoted 8 lines
arch/x86/kvm/emulate.c writeback: · line 3252
Quoted 7 lines
arch/x86/kvm/emulate.c +7
-0+trace_printk ("c->eip %lx ctxt->eip %lx\n" , + c->eip, ctxt->eip); +if (((c->rep_prefix ==REPE_PREFIX )&& + ((ctxt->eflags&EFLG_ZF ) ==0 )) + || ((c->rep_prefix ==REPNE_PREFIX )&& + ((ctxt->eflags&EFLG_ZF ) ==EFLG_ZF ))) { + ctxt->restart = false;Why not jump to
string_donelabel here?
Quoted 2 lines
It does a 'goto done;' which skips a couple of things.
The only thing it skips is:ctxt->decode.mem_read.end = 0;
as far as I can see. And this is ok if instruction is completed.
Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
arch/x86/kvm/emulate.c +2
-0+ } + }/* * Re-enter guest when pio read ahead buffer is empty or, * if it is not used, after each 1024 iteration. Quoted 8 lines
arch/x86/kvm/emulate.c writeback: · line 3262
Quoted 6 lines
arch/x86/kvm/emulate.c +2
-1*/ if ((rc->end ==0 && !(c->regs[VCPU_REGS_RCX ]&0x3ff )) || - (rc->end !=0 && rc->end == rc->pos)) + (rc->end !=0 && rc->end == rc->pos)) { ctxt->restart = false; + c->eip = ctxt->eip;We can get here when instruction is completed by above "if", so same instruction will reexecute once again.
Quoted 5 lines
Not good. Will redo (and write tests).
Signature · 1 line
error compiling committee.c: too many arguments to function
--
Gleb.
Re: [PATCH 4/4] KVM: x86 emulator: fix REPZ/REPNZ termination condition
Quoted 44 lines · Gleb Natapov
Quoted 43 lines · Avi Kivity
Quoted 8 lines · Gleb Natapov
Quoted 7 lines · Avi Kivity
EFLAGS.ZF needs to be checked after each iteration, not before.
Signed-off-by Avi Kivity<[email protected]>--- arch/x86/kvm/emulate.c | 42 +++++++++++++++++++++++------------------- 1 files changed, 23 insertions(+), 19 deletions(-)Quoted 11 lines
arch/x86/kvm/emulate.c x86_emulate_insn(struct x86_emulate_ctxt *ctxt) · line 2750
Quoted 9 lines
arch/x86/kvm/emulate.c +1
-0@@ -2747,6 +2747,7 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt)int rc =X86EMUL_CONTINUE ;int saved_dst_type = c->dst.type;int irq;/* Used for int 3, int, and into */ +ulong old_eip;Is never used.
Whoops.
Quoted 21 lines
Quoted 10 lines
arch/x86/kvm/emulate.c +6
-0@@ -3250,13 +3234,33 @@ writeback:if (c->rep_prefix&& (c->d& String)) {struct read_cache *rc =&ctxt->decode.io_read;register_address_increment (c,&c->regs[VCPU_REGS_RCX ],-1 ); +/* The second termination condition only applies for REPE +* and REPNE. Test if the repeat string operation prefix is +* REPE/REPZ or REPNE/REPNZ and if it's the case it tests the +* corresponding termination condition according to: +* - if REPE/REPZ and ZF = 0 then done +* - if REPNE/REPNZ and ZF = 1 then done arch/x86/kvm/emulate.c writeback: · line 3252
Quoted 10 lines
arch/x86/kvm/emulate.c +10
-0+*/ +if ((c->b ==0xa6 ) || (c->b ==0xa7 ) || + (c->b ==0xae ) || (c->b ==0xaf )) { +trace_printk ("c->eip %lx ctxt->eip %lx\n" , + c->eip, ctxt->eip); +if (((c->rep_prefix ==REPE_PREFIX )&& + ((ctxt->eflags&EFLG_ZF ) ==0 )) + || ((c->rep_prefix ==REPNE_PREFIX )&& + ((ctxt->eflags&EFLG_ZF ) ==EFLG_ZF ))) { + ctxt->restart = false;Why not jump to
string_donelabel here?
Quoted 5 lines
Quoted 2 lines
It does a 'goto done;' which skips a couple of things.
The only thing it skips is:
ctxt->decode.mem_read.end= 0;
as far as I can see. And this is ok if instruction is completed.
I'd like to avoid this interdependency (and there are enough gotos in the emulator already).
Signature · 1 line
error compiling committee.c: too many arguments to function