LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Avi Kivity <avi@qumranet.com>
To: kvm-devel@lists.sourceforge.net
Cc: linux-kernel@vger.kernel.org, Avi Kivity <avi@qumranet.com>
Subject: [PATCH 07/41] KVM: Do not communicate to userspace through cpu registers during PIO
Date: Sun,  1 Apr 2007 17:35:04 +0300	[thread overview]
Message-ID: <11754381381597-git-send-email-avi@qumranet.com> (raw)
In-Reply-To: <11754381383144-git-send-email-avi@qumranet.com>

Currently when passing the a PIO emulation request to userspace, we
rely on userspace updating %rax (on 'in' instructions) and %rsi/%rdi/%rcx
(on string instructions).  This (a) requires two extra ioctls for getting
and setting the registers and (b) is unfriendly to non-x86 archs, when
they get kvm ports.

So fix by doing the register fixups in the kernel and passing to userspace
only an abstract description of the PIO to be done.

Signed-off-by: Avi Kivity <avi@qumranet.com>
---
 drivers/kvm/kvm.h      |    1 +
 drivers/kvm/kvm_main.c |   48 +++++++++++++++++++++++++++++++++++++++++++++---
 drivers/kvm/svm.c      |    2 ++
 drivers/kvm/vmx.c      |    2 ++
 include/linux/kvm.h    |    6 +++---
 5 files changed, 53 insertions(+), 6 deletions(-)
 mode change 100755 => 100644 drivers/kvm/kvm_main.c

diff --git a/drivers/kvm/kvm.h b/drivers/kvm/kvm.h
index 901b8d9..59cbc5b 100644
--- a/drivers/kvm/kvm.h
+++ b/drivers/kvm/kvm.h
@@ -274,6 +274,7 @@ struct kvm_vcpu {
 	int mmio_size;
 	unsigned char mmio_data[8];
 	gpa_t mmio_phys_addr;
+	int pio_pending;
 
 	struct {
 		int active;
diff --git a/drivers/kvm/kvm_main.c b/drivers/kvm/kvm_main.c
old mode 100755
new mode 100644
index 42be8a8..ff8bcfe
--- a/drivers/kvm/kvm_main.c
+++ b/drivers/kvm/kvm_main.c
@@ -1504,6 +1504,44 @@ void save_msrs(struct vmx_msr_entry *e, int n)
 }
 EXPORT_SYMBOL_GPL(save_msrs);
 
+static void complete_pio(struct kvm_vcpu *vcpu)
+{
+	struct kvm_io *io = &vcpu->run->io;
+	long delta;
+
+	kvm_arch_ops->cache_regs(vcpu);
+
+	if (!io->string) {
+		if (io->direction == KVM_EXIT_IO_IN)
+			memcpy(&vcpu->regs[VCPU_REGS_RAX], &io->value,
+			       io->size);
+	} else {
+		delta = 1;
+		if (io->rep) {
+			delta *= io->count;
+			/*
+			 * The size of the register should really depend on
+			 * current address size.
+			 */
+			vcpu->regs[VCPU_REGS_RCX] -= delta;
+		}
+		if (io->string_down)
+			delta = -delta;
+		delta *= io->size;
+		if (io->direction == KVM_EXIT_IO_IN)
+			vcpu->regs[VCPU_REGS_RDI] += delta;
+		else
+			vcpu->regs[VCPU_REGS_RSI] += delta;
+	}
+
+	vcpu->pio_pending = 0;
+	vcpu->run->io_completed = 0;
+
+	kvm_arch_ops->decache_regs(vcpu);
+
+	kvm_arch_ops->skip_emulated_instruction(vcpu);
+}
+
 static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
 {
 	int r;
@@ -1518,9 +1556,13 @@ static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
 		kvm_run->emulated = 0;
 	}
 
-	if (kvm_run->mmio_completed) {
-		memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
-		vcpu->mmio_read_completed = 1;
+	if (kvm_run->io_completed) {
+		if (vcpu->pio_pending)
+			complete_pio(vcpu);
+		else {
+			memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
+			vcpu->mmio_read_completed = 1;
+		}
 	}
 
 	vcpu->mmio_needed = 0;
diff --git a/drivers/kvm/svm.c b/drivers/kvm/svm.c
index 6787f11..c35b8c8 100644
--- a/drivers/kvm/svm.c
+++ b/drivers/kvm/svm.c
@@ -1037,6 +1037,7 @@ static int io_interception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
 	kvm_run->io.size = ((io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT);
 	kvm_run->io.string = (io_info & SVM_IOIO_STR_MASK) != 0;
 	kvm_run->io.rep = (io_info & SVM_IOIO_REP_MASK) != 0;
+	kvm_run->io.count = 1;
 
 	if (kvm_run->io.string) {
 		unsigned addr_mask;
@@ -1056,6 +1057,7 @@ static int io_interception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
 		}
 	} else
 		kvm_run->io.value = vcpu->svm->vmcb->save.rax;
+	vcpu->pio_pending = 1;
 	return 0;
 }
 
diff --git a/drivers/kvm/vmx.c b/drivers/kvm/vmx.c
index a721b60..4d5f40f 100644
--- a/drivers/kvm/vmx.c
+++ b/drivers/kvm/vmx.c
@@ -1459,12 +1459,14 @@ static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
 		= (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_DF) != 0;
 	kvm_run->io.rep = (exit_qualification & 32) != 0;
 	kvm_run->io.port = exit_qualification >> 16;
+	kvm_run->io.count = 1;
 	if (kvm_run->io.string) {
 		if (!get_io_count(vcpu, &kvm_run->io.count))
 			return 1;
 		kvm_run->io.address = vmcs_readl(GUEST_LINEAR_ADDRESS);
 	} else
 		kvm_run->io.value = vcpu->regs[VCPU_REGS_RAX]; /* rax */
+	vcpu->pio_pending = 1;
 	return 0;
 }
 
diff --git a/include/linux/kvm.h b/include/linux/kvm.h
index d88e750..19aeb33 100644
--- a/include/linux/kvm.h
+++ b/include/linux/kvm.h
@@ -11,7 +11,7 @@
 #include <asm/types.h>
 #include <linux/ioctl.h>
 
-#define KVM_API_VERSION 5
+#define KVM_API_VERSION 6
 
 /*
  * Architectural interrupt line count, and the size of the bitmap needed
@@ -53,7 +53,7 @@ enum kvm_exit_reason {
 struct kvm_run {
 	/* in */
 	__u32 emulated;  /* skip current instruction */
-	__u32 mmio_completed; /* mmio request completed */
+	__u32 io_completed; /* mmio/pio request completed */
 	__u8 request_interrupt_window;
 	__u8 padding1[7];
 
@@ -80,7 +80,7 @@ struct kvm_run {
 			__u32 error_code;
 		} ex;
 		/* KVM_EXIT_IO */
-		struct {
+		struct kvm_io {
 #define KVM_EXIT_IO_IN  0
 #define KVM_EXIT_IO_OUT 1
 			__u8 direction;
-- 
1.5.0.5


  reply	other threads:[~2007-04-01 14:49 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-04-01 14:34 [PATCH 00/41] kvm updates for 2.6.22 Avi Kivity
2007-04-01 14:34 ` [PATCH 01/41] KVM: Fix guest register corruption on paravirt hypercall Avi Kivity
2007-04-01 14:34   ` [PATCH 02/41] KVM: Use the generic skip_emulated_instruction() in hypercall code Avi Kivity
2007-04-01 14:35     ` [PATCH 03/41] KVM: Use own minor number Avi Kivity
2007-04-01 14:35       ` [PATCH 04/41] KVM: Export <linux/kvm.h> Avi Kivity
2007-04-01 14:35         ` [PATCH 05/41] KVM: Fix bogus sign extension in mmu mapping audit Avi Kivity
2007-04-01 14:35           ` [PATCH 06/41] KVM: Use a shared page for kernel/user communication when runing a vcpu Avi Kivity
2007-04-01 14:35             ` Avi Kivity [this message]
2007-04-01 14:35               ` [PATCH 08/41] KVM: Handle cpuid in the kernel instead of punting to userspace Avi Kivity
2007-04-01 14:35                 ` [PATCH 09/41] KVM: Remove the 'emulated' field from the userspace interface Avi Kivity
2007-04-01 14:35                   ` [PATCH 10/41] KVM: Remove minor wart from KVM_CREATE_VCPU ioctl Avi Kivity
2007-04-01 14:35                     ` [PATCH 11/41] KVM: Renumber ioctls Avi Kivity
2007-04-01 14:35                       ` [PATCH 12/41] KVM: Add method to check for backwards-compatible API extensions Avi Kivity
2007-04-01 14:35                         ` [PATCH 13/41] KVM: Allow userspace to process hypercalls which have no kernel handler Avi Kivity
2007-04-01 14:35                           ` [PATCH 14/41] KVM: Fold kvm_run::exit_type into kvm_run::exit_reason Avi Kivity
2007-04-01 14:35                             ` [PATCH 15/41] KVM: Add a special exit reason when exiting due to an interrupt Avi Kivity
2007-04-01 14:35                               ` [PATCH 16/41] KVM: Initialize the apic_base msr on svm too Avi Kivity
2007-04-01 14:35                                 ` [PATCH 17/41] KVM: Add guest mode signal mask Avi Kivity
2007-04-01 14:35                                   ` [PATCH 18/41] KVM: Allow kernel to select size of mmap() buffer Avi Kivity
2007-04-01 14:35                                     ` [PATCH 19/41] KVM: Future-proof argument-less ioctls Avi Kivity
2007-04-01 14:35                                       ` [PATCH 20/41] KVM: Avoid guest virtual addresses in string pio userspace interface Avi Kivity
2007-04-01 14:35                                         ` [PATCH 21/41] KVM: MMU: Remove unnecessary check for pdptr access Avi Kivity
2007-04-01 14:35                                           ` [PATCH 22/41] KVM: MMU: Remove global pte tracking Avi Kivity
2007-04-01 14:35                                             ` [PATCH 23/41] KVM: Workaround vmx inability to virtualize the reset state Avi Kivity
2007-04-01 14:35                                               ` [PATCH 24/41] KVM: Remove set_cr0_no_modeswitch() arch op Avi Kivity
2007-04-01 14:35                                                 ` [PATCH 25/41] KVM: Modify guest segments after potentially switching modes Avi Kivity
2007-04-01 14:35                                                   ` [PATCH 26/41] KVM: Hack real-mode segments on vmx from KVM_SET_SREGS Avi Kivity
2007-04-01 14:35                                                     ` [PATCH 27/41] KVM: Don't allow the guest to turn off the cpu cache Avi Kivity
2007-04-01 14:35                                                       ` [PATCH 28/41] KVM: Remove unused and write-only variables Avi Kivity
2007-04-01 14:35                                                         ` [PATCH 29/41] KVM: Handle writes to MCG_STATUS msr Avi Kivity
2007-04-01 14:35                                                           ` [PATCH 30/41] KVM: SVM: forbid guest to execute monitor/mwait Avi Kivity
2007-04-01 14:35                                                             ` [PATCH 31/41] KVM: MMU: Fix hugepage pdes mapping same physical address with different access Avi Kivity
2007-04-01 14:35                                                               ` [PATCH 32/41] KVM: SVM: Ensure timestamp counter monotonicity Avi Kivity
2007-04-01 14:35                                                                 ` [PATCH 33/41] KVM: Remove unused function Avi Kivity
2007-04-01 14:35                                                                   ` [PATCH 34/41] KVM: Use list_move() Avi Kivity
2007-04-01 14:35                                                                     ` [PATCH 35/41] KVM: Remove debug message Avi Kivity
2007-04-01 14:35                                                                       ` [PATCH 36/41] KVM: x86 emulator: fix bit string operations operand size Avi Kivity
2007-04-01 14:35                                                                         ` [PATCH 37/41] KVM: Add mmu cache clear function Avi Kivity
2007-04-01 14:35                                                                           ` [PATCH 38/41] KVM: Simply gfn_to_page() Avi Kivity
2007-04-01 14:35                                                                             ` [PATCH 39/41] KVM: Add physical memory aliasing feature Avi Kivity
2007-04-01 14:35                                                                               ` [PATCH 40/41] KVM: Add fpu get/set operations Avi Kivity
2007-04-01 14:35                                                                                 ` [PATCH 41/41] KVM: SVM: enable LBRV virtualization if available Avi Kivity

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=11754381381597-git-send-email-avi@qumranet.com \
    --to=avi@qumranet.com \
    --cc=kvm-devel@lists.sourceforge.net \
    --cc=linux-kernel@vger.kernel.org \
    --subject='Re: [PATCH 07/41] KVM: Do not communicate to userspace through cpu registers during PIO' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).