Commit 716d51ab authored by Gleb Natapov's avatar Gleb Natapov Committed by Avi Kivity

KVM: Provide userspace IO exit completion callback

Current code assumes that IO exit was due to instruction emulation
and handles execution back to emulator directly. This patch adds new
userspace IO exit completion callback that can be set by any other code
that caused IO exit to userspace.
Signed-off-by: default avatarGleb Natapov <gleb@redhat.com>
Signed-off-by: default avatarAvi Kivity <avi@redhat.com>
parent 3b4dc3a0
...@@ -414,6 +414,7 @@ struct kvm_vcpu_arch { ...@@ -414,6 +414,7 @@ struct kvm_vcpu_arch {
struct x86_emulate_ctxt emulate_ctxt; struct x86_emulate_ctxt emulate_ctxt;
bool emulate_regs_need_sync_to_vcpu; bool emulate_regs_need_sync_to_vcpu;
bool emulate_regs_need_sync_from_vcpu; bool emulate_regs_need_sync_from_vcpu;
int (*complete_userspace_io)(struct kvm_vcpu *vcpu);
gpa_t time; gpa_t time;
struct pvclock_vcpu_time_info hv_clock; struct pvclock_vcpu_time_info hv_clock;
......
...@@ -4544,6 +4544,9 @@ static bool retry_instruction(struct x86_emulate_ctxt *ctxt, ...@@ -4544,6 +4544,9 @@ static bool retry_instruction(struct x86_emulate_ctxt *ctxt,
return true; return true;
} }
static int complete_emulated_mmio(struct kvm_vcpu *vcpu);
static int complete_emulated_pio(struct kvm_vcpu *vcpu);
int x86_emulate_instruction(struct kvm_vcpu *vcpu, int x86_emulate_instruction(struct kvm_vcpu *vcpu,
unsigned long cr2, unsigned long cr2,
int emulation_type, int emulation_type,
...@@ -4614,13 +4617,16 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, ...@@ -4614,13 +4617,16 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu,
} else if (vcpu->arch.pio.count) { } 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;
else else {
writeback = false; writeback = false;
vcpu->arch.complete_userspace_io = complete_emulated_pio;
}
r = EMULATE_DO_MMIO; r = EMULATE_DO_MMIO;
} else if (vcpu->mmio_needed) { } else if (vcpu->mmio_needed) {
if (!vcpu->mmio_is_write) if (!vcpu->mmio_is_write)
writeback = false; writeback = false;
r = EMULATE_DO_MMIO; r = EMULATE_DO_MMIO;
vcpu->arch.complete_userspace_io = complete_emulated_mmio;
} else if (r == EMULATION_RESTART) } else if (r == EMULATION_RESTART)
goto restart; goto restart;
else else
...@@ -5476,6 +5482,24 @@ static int __vcpu_run(struct kvm_vcpu *vcpu) ...@@ -5476,6 +5482,24 @@ static int __vcpu_run(struct kvm_vcpu *vcpu)
return r; return r;
} }
static inline int complete_emulated_io(struct kvm_vcpu *vcpu)
{
int r;
vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
r = emulate_instruction(vcpu, EMULTYPE_NO_DECODE);
srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
if (r != EMULATE_DONE)
return 0;
return 1;
}
static int complete_emulated_pio(struct kvm_vcpu *vcpu)
{
BUG_ON(!vcpu->arch.pio.count);
return complete_emulated_io(vcpu);
}
/* /*
* Implements the following, as a state machine: * Implements the following, as a state machine:
* *
...@@ -5492,47 +5516,37 @@ static int __vcpu_run(struct kvm_vcpu *vcpu) ...@@ -5492,47 +5516,37 @@ static int __vcpu_run(struct kvm_vcpu *vcpu)
* copy data * copy data
* exit * exit
*/ */
static int complete_mmio(struct kvm_vcpu *vcpu) static int complete_emulated_mmio(struct kvm_vcpu *vcpu)
{ {
struct kvm_run *run = vcpu->run; struct kvm_run *run = vcpu->run;
struct kvm_mmio_fragment *frag; struct kvm_mmio_fragment *frag;
int r;
if (!(vcpu->arch.pio.count || vcpu->mmio_needed)) BUG_ON(!vcpu->mmio_needed);
return 1;
if (vcpu->mmio_needed) { /* Complete previous fragment */
/* Complete previous fragment */ frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment++];
frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment++]; if (!vcpu->mmio_is_write)
if (!vcpu->mmio_is_write) memcpy(frag->data, run->mmio.data, frag->len);
memcpy(frag->data, run->mmio.data, frag->len); if (vcpu->mmio_cur_fragment == vcpu->mmio_nr_fragments) {
if (vcpu->mmio_cur_fragment == vcpu->mmio_nr_fragments) { vcpu->mmio_needed = 0;
vcpu->mmio_needed = 0;
if (vcpu->mmio_is_write)
return 1;
vcpu->mmio_read_completed = 1;
goto done;
}
/* Initiate next fragment */
++frag;
run->exit_reason = KVM_EXIT_MMIO;
run->mmio.phys_addr = frag->gpa;
if (vcpu->mmio_is_write) if (vcpu->mmio_is_write)
memcpy(run->mmio.data, frag->data, frag->len); return 1;
run->mmio.len = frag->len; vcpu->mmio_read_completed = 1;
run->mmio.is_write = vcpu->mmio_is_write; return complete_emulated_io(vcpu);
return 0; }
/* Initiate next fragment */
} ++frag;
done: run->exit_reason = KVM_EXIT_MMIO;
vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); run->mmio.phys_addr = frag->gpa;
r = emulate_instruction(vcpu, EMULTYPE_NO_DECODE); if (vcpu->mmio_is_write)
srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); memcpy(run->mmio.data, frag->data, frag->len);
if (r != EMULATE_DONE) run->mmio.len = frag->len;
return 0; run->mmio.is_write = vcpu->mmio_is_write;
return 1; vcpu->arch.complete_userspace_io = complete_emulated_mmio;
return 0;
} }
int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
{ {
int r; int r;
...@@ -5559,9 +5573,14 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) ...@@ -5559,9 +5573,14 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
} }
} }
r = complete_mmio(vcpu); if (unlikely(vcpu->arch.complete_userspace_io)) {
if (r <= 0) int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io;
goto out; vcpu->arch.complete_userspace_io = NULL;
r = cui(vcpu);
if (r <= 0)
goto out;
} else
WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed);
r = __vcpu_run(vcpu); r = __vcpu_run(vcpu);
......
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