Commit d2ddd1c4 authored by Gleb Natapov's avatar Gleb Natapov Committed by Avi Kivity

KVM: x86 emulator: get rid of "restart" in emulation context.

x86_emulate_insn() will return 1 if instruction can be restarted
without re-entering a guest.
Signed-off-by: default avatarGleb Natapov <gleb@redhat.com>
Signed-off-by: default avatarMarcelo Tosatti <mtosatti@redhat.com>
parent 3e2f65d5
...@@ -224,7 +224,6 @@ struct x86_emulate_ctxt { ...@@ -224,7 +224,6 @@ struct x86_emulate_ctxt {
/* interruptibility state, as a result of execution of STI or MOV SS */ /* interruptibility state, as a result of execution of STI or MOV SS */
int interruptibility; int interruptibility;
bool restart; /* restart string instruction after writeback */
bool perm_ok; /* do not check permissions if true */ bool perm_ok; /* do not check permissions if true */
int exception; /* exception that happens during emulation or -1 */ int exception; /* exception that happens during emulation or -1 */
...@@ -255,6 +254,9 @@ struct x86_emulate_ctxt { ...@@ -255,6 +254,9 @@ struct x86_emulate_ctxt {
#endif #endif
int x86_decode_insn(struct x86_emulate_ctxt *ctxt); int x86_decode_insn(struct x86_emulate_ctxt *ctxt);
#define EMULATION_FAILED -1
#define EMULATION_OK 0
#define EMULATION_RESTART 1
int x86_emulate_insn(struct x86_emulate_ctxt *ctxt); int x86_emulate_insn(struct x86_emulate_ctxt *ctxt);
int emulator_task_switch(struct x86_emulate_ctxt *ctxt, int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
u16 tss_selector, int reason, u16 tss_selector, int reason,
......
...@@ -437,7 +437,6 @@ static void emulate_exception(struct x86_emulate_ctxt *ctxt, int vec, ...@@ -437,7 +437,6 @@ static void emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
ctxt->exception = vec; ctxt->exception = vec;
ctxt->error_code = error; ctxt->error_code = error;
ctxt->error_code_valid = valid; ctxt->error_code_valid = valid;
ctxt->restart = false;
} }
static void emulate_gp(struct x86_emulate_ctxt *ctxt, int err) static void emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
...@@ -2633,9 +2632,6 @@ x86_decode_insn(struct x86_emulate_ctxt *ctxt) ...@@ -2633,9 +2632,6 @@ x86_decode_insn(struct x86_emulate_ctxt *ctxt)
struct opcode opcode, *g_mod012, *g_mod3; struct opcode opcode, *g_mod012, *g_mod3;
struct operand memop = { .type = OP_NONE }; struct operand memop = { .type = OP_NONE };
/* we cannot decode insn before we complete previous rep insn */
WARN_ON(ctxt->restart);
c->eip = ctxt->eip; c->eip = ctxt->eip;
c->fetch.start = c->fetch.end = c->eip; c->fetch.start = c->fetch.end = c->eip;
ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS); ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS);
...@@ -2985,10 +2981,8 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt) ...@@ -2985,10 +2981,8 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
} }
if (c->rep_prefix && (c->d & String)) { if (c->rep_prefix && (c->d & String)) {
ctxt->restart = true;
/* All REP prefixes have the same first termination condition */ /* All REP prefixes have the same first termination condition */
if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) { if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
ctxt->restart = false;
ctxt->eip = c->eip; ctxt->eip = c->eip;
goto done; goto done;
} }
...@@ -3446,28 +3440,29 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt) ...@@ -3446,28 +3440,29 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
struct read_cache *r = &ctxt->decode.io_read; struct read_cache *r = &ctxt->decode.io_read;
register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1); register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
if (string_insn_completed(ctxt)) if (!string_insn_completed(ctxt)) {
ctxt->restart = false;
/* /*
* Re-enter guest when pio read ahead buffer is empty or, * Re-enter guest when pio read ahead buffer is empty
* if it is not used, after each 1024 iteration. * or, if it is not used, after each 1024 iteration.
*/ */
else if ((r->end == 0 && !(c->regs[VCPU_REGS_RCX] & 0x3ff)) || if ((r->end != 0 || c->regs[VCPU_REGS_RCX] & 0x3ff) &&
(r->end != 0 && r->end == r->pos)) { (r->end == 0 || r->end != r->pos)) {
ctxt->restart = false;
c->eip = ctxt->eip;
}
}
/* /*
* reset read cache here in case string instruction is restared * Reset read cache. Usually happens before
* without decoding * decode, but since instruction is restarted
* we have to do it here.
*/ */
ctxt->decode.mem_read.end = 0; ctxt->decode.mem_read.end = 0;
if (!ctxt->restart) return EMULATION_RESTART;
}
goto done; /* skip rip writeback */
}
}
ctxt->eip = c->eip; ctxt->eip = c->eip;
done: done:
return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0; return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
twobyte_insn: twobyte_insn:
switch (c->b) { switch (c->b) {
......
...@@ -4181,18 +4181,17 @@ int emulate_instruction(struct kvm_vcpu *vcpu, ...@@ -4181,18 +4181,17 @@ int emulate_instruction(struct kvm_vcpu *vcpu,
restart: restart:
r = x86_emulate_insn(&vcpu->arch.emulate_ctxt); r = x86_emulate_insn(&vcpu->arch.emulate_ctxt);
if (r) { /* emulation failed */ if (r == EMULATION_FAILED) {
if (reexecute_instruction(vcpu, cr2)) if (reexecute_instruction(vcpu, cr2))
return EMULATE_DONE; return EMULATE_DONE;
return handle_emulation_failure(vcpu); return handle_emulation_failure(vcpu);
} }
r = EMULATE_DONE; if (vcpu->arch.emulate_ctxt.exception >= 0) {
if (vcpu->arch.emulate_ctxt.exception >= 0)
inject_emulated_exception(vcpu); inject_emulated_exception(vcpu);
else if (vcpu->arch.pio.count) { r = EMULATE_DONE;
} else if (vcpu->arch.pio.count) {
if (!vcpu->arch.pio.in) if (!vcpu->arch.pio.in)
vcpu->arch.pio.count = 0; vcpu->arch.pio.count = 0;
r = EMULATE_DO_MMIO; r = EMULATE_DO_MMIO;
...@@ -4200,8 +4199,10 @@ int emulate_instruction(struct kvm_vcpu *vcpu, ...@@ -4200,8 +4199,10 @@ int emulate_instruction(struct kvm_vcpu *vcpu,
if (vcpu->mmio_is_write) if (vcpu->mmio_is_write)
vcpu->mmio_needed = 0; vcpu->mmio_needed = 0;
r = EMULATE_DO_MMIO; r = EMULATE_DO_MMIO;
} else if (vcpu->arch.emulate_ctxt.restart) } else if (r == EMULATION_RESTART)
goto restart; goto restart;
else
r = EMULATE_DONE;
toggle_interruptibility(vcpu, vcpu->arch.emulate_ctxt.interruptibility); toggle_interruptibility(vcpu, vcpu->arch.emulate_ctxt.interruptibility);
kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags); kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
...@@ -5100,8 +5101,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) ...@@ -5100,8 +5101,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
if (!irqchip_in_kernel(vcpu->kvm)) if (!irqchip_in_kernel(vcpu->kvm))
kvm_set_cr8(vcpu, kvm_run->cr8); kvm_set_cr8(vcpu, kvm_run->cr8);
if (vcpu->arch.pio.count || vcpu->mmio_needed || if (vcpu->arch.pio.count || vcpu->mmio_needed) {
vcpu->arch.emulate_ctxt.restart) {
if (vcpu->mmio_needed) { if (vcpu->mmio_needed) {
memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8); memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
vcpu->mmio_read_completed = 1; vcpu->mmio_read_completed = 1;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment