Skip to content

Commit

Permalink
KVM: x86 emulator: get rid of "restart" in emulation context.
Browse files Browse the repository at this point in the history
x86_emulate_insn() will return 1 if instruction can be restarted
without re-entering a guest.

Signed-off-by: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
  • Loading branch information
Gleb Natapov authored and Avi Kivity committed Oct 24, 2010
1 parent 3e2f65d commit d2ddd1c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 33 deletions.
4 changes: 3 additions & 1 deletion arch/x86/include/asm/kvm_emulate.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ struct x86_emulate_ctxt {
/* interruptibility state, as a result of execution of STI or MOV SS */
int interruptibility;

bool restart; /* restart string instruction after writeback */
bool perm_ok; /* do not check permissions if true */

int exception; /* exception that happens during emulation or -1 */
Expand Down Expand Up @@ -255,6 +254,9 @@ struct x86_emulate_ctxt {
#endif

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 emulator_task_switch(struct x86_emulate_ctxt *ctxt,
u16 tss_selector, int reason,
Expand Down
43 changes: 19 additions & 24 deletions arch/x86/kvm/emulate.c
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,6 @@ static void emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
ctxt->exception = vec;
ctxt->error_code = error;
ctxt->error_code_valid = valid;
ctxt->restart = false;
}

static void emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Expand Down Expand Up @@ -2633,9 +2632,6 @@ x86_decode_insn(struct x86_emulate_ctxt *ctxt)
struct opcode opcode, *g_mod012, *g_mod3;
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->fetch.start = c->fetch.end = c->eip;
ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS);
Expand Down Expand Up @@ -2985,10 +2981,8 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
}

if (c->rep_prefix && (c->d & String)) {
ctxt->restart = true;
/* All REP prefixes have the same first termination condition */
if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
ctxt->restart = false;
ctxt->eip = c->eip;
goto done;
}
Expand Down Expand Up @@ -3446,28 +3440,29 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
struct read_cache *r = &ctxt->decode.io_read;
register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);

if (string_insn_completed(ctxt))
ctxt->restart = false;
/*
* Re-enter guest when pio read ahead buffer is empty or,
* if it is not used, after each 1024 iteration.
*/
else if ((r->end == 0 && !(c->regs[VCPU_REGS_RCX] & 0x3ff)) ||
(r->end != 0 && r->end == r->pos)) {
ctxt->restart = false;
c->eip = ctxt->eip;
if (!string_insn_completed(ctxt)) {
/*
* Re-enter guest when pio read ahead buffer is empty
* or, if it is not used, after each 1024 iteration.
*/
if ((r->end != 0 || c->regs[VCPU_REGS_RCX] & 0x3ff) &&
(r->end == 0 || r->end != r->pos)) {
/*
* Reset read cache. Usually happens before
* decode, but since instruction is restarted
* we have to do it here.
*/
ctxt->decode.mem_read.end = 0;
return EMULATION_RESTART;
}
goto done; /* skip rip writeback */
}
}
/*
* reset read cache here in case string instruction is restared
* without decoding
*/
ctxt->decode.mem_read.end = 0;
if (!ctxt->restart)
ctxt->eip = c->eip;

ctxt->eip = c->eip;

done:
return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;

twobyte_insn:
switch (c->b) {
Expand Down
16 changes: 8 additions & 8 deletions arch/x86/kvm/x86.c
Original file line number Diff line number Diff line change
Expand Up @@ -4181,27 +4181,28 @@ int emulate_instruction(struct kvm_vcpu *vcpu,
restart:
r = x86_emulate_insn(&vcpu->arch.emulate_ctxt);

if (r) { /* emulation failed */
if (r == EMULATION_FAILED) {
if (reexecute_instruction(vcpu, cr2))
return EMULATE_DONE;

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);
else if (vcpu->arch.pio.count) {
r = EMULATE_DONE;
} else if (vcpu->arch.pio.count) {
if (!vcpu->arch.pio.in)
vcpu->arch.pio.count = 0;
r = EMULATE_DO_MMIO;
} else if (vcpu->mmio_needed) {
if (vcpu->mmio_is_write)
vcpu->mmio_needed = 0;
r = EMULATE_DO_MMIO;
} else if (vcpu->arch.emulate_ctxt.restart)
} else if (r == EMULATION_RESTART)
goto restart;
else
r = EMULATE_DONE;

toggle_interruptibility(vcpu, vcpu->arch.emulate_ctxt.interruptibility);
kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
Expand Down Expand Up @@ -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))
kvm_set_cr8(vcpu, kvm_run->cr8);

if (vcpu->arch.pio.count || vcpu->mmio_needed ||
vcpu->arch.emulate_ctxt.restart) {
if (vcpu->arch.pio.count || vcpu->mmio_needed) {
if (vcpu->mmio_needed) {
memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
vcpu->mmio_read_completed = 1;
Expand Down

0 comments on commit d2ddd1c

Please sign in to comment.