linux-riscv.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: Yifei Jiang <jiangyifei@huawei.com>
To: <pbonzini@redhat.com>, <paul.walmsley@sifive.com>,
	<palmer@dabbelt.com>, <aou@eecs.berkeley.edu>
Cc: victor.zhangxiaofeng@huawei.com, zhang.zhanghailiang@huawei.com,
	kvm@vger.kernel.org, anup.patel@wdc.com,
	linux-kernel@vger.kernel.org, atish.patra@wdc.com,
	Yifei Jiang <jiangyifei@huawei.com>,
	kvm-riscv@lists.infradead.org, limingwang@huawei.com,
	linux-riscv@lists.infradead.org, wu.wubin@huawei.com,
	dengkai1@huawei.com
Subject: [RFC 2/2] RISC-V: KVM: read\write kernel mmio device support
Date: Fri, 24 Jul 2020 16:54:41 +0800	[thread overview]
Message-ID: <20200724085441.1514-3-jiangyifei@huawei.com> (raw)
In-Reply-To: <20200724085441.1514-1-jiangyifei@huawei.com>

Signed-off-by: Yifei Jiang <jiangyifei@huawei.com>
Signed-off-by: Mingwang Li <limingwang@huawei.com>
---
 arch/riscv/kvm/vcpu_exit.c | 38 ++++++++++++++++++++++++++++++++------
 1 file changed, 32 insertions(+), 6 deletions(-)

diff --git a/arch/riscv/kvm/vcpu_exit.c b/arch/riscv/kvm/vcpu_exit.c
index e97ba96cb0ae..448f11179fa8 100644
--- a/arch/riscv/kvm/vcpu_exit.c
+++ b/arch/riscv/kvm/vcpu_exit.c
@@ -191,6 +191,8 @@ static int virtual_inst_fault(struct kvm_vcpu *vcpu, struct kvm_run *run,
 static int emulate_load(struct kvm_vcpu *vcpu, struct kvm_run *run,
 			unsigned long fault_addr, unsigned long htinst)
 {
+	int ret;
+	u8 data_buf[8];
 	unsigned long insn;
 	int shift = 0, len = 0;
 	struct kvm_cpu_trap utrap = { 0 };
@@ -272,19 +274,32 @@ static int emulate_load(struct kvm_vcpu *vcpu, struct kvm_run *run,
 	vcpu->arch.mmio_decode.len = len;
 	vcpu->arch.mmio_decode.return_handled = 0;
 
-	/* Exit to userspace for MMIO emulation */
-	vcpu->stat.mmio_exit_user++;
-	run->exit_reason = KVM_EXIT_MMIO;
+	ret = kvm_io_bus_read(vcpu, KVM_MMIO_BUS, fault_addr, len,
+						  data_buf);
+
 	run->mmio.is_write = false;
 	run->mmio.phys_addr = fault_addr;
 	run->mmio.len = len;
 
+	if (!ret) {
+		/* We handled the access successfully in the kernel. */
+		memcpy(run->mmio.data, data_buf, len);
+		vcpu->stat.mmio_exit_kernel++;
+		kvm_riscv_vcpu_mmio_return(vcpu, run);
+		return 1;
+	}
+
+	/* Exit to userspace for MMIO emulation */
+	vcpu->stat.mmio_exit_user++;
+	run->exit_reason = KVM_EXIT_MMIO;
+
 	return 0;
 }
 
 static int emulate_store(struct kvm_vcpu *vcpu, struct kvm_run *run,
 			 unsigned long fault_addr, unsigned long htinst)
 {
+	int ret;
 	u8 data8;
 	u16 data16;
 	u32 data32;
@@ -378,13 +393,24 @@ static int emulate_store(struct kvm_vcpu *vcpu, struct kvm_run *run,
 		return -ENOTSUPP;
 	};
 
-	/* Exit to userspace for MMIO emulation */
-	vcpu->stat.mmio_exit_user++;
-	run->exit_reason = KVM_EXIT_MMIO;
+	ret = kvm_io_bus_write(vcpu, KVM_MMIO_BUS, fault_addr, len,
+						   run->mmio.data);
+
 	run->mmio.is_write = true;
 	run->mmio.phys_addr = fault_addr;
 	run->mmio.len = len;
 
+	if (!ret) {
+		/* We handled the access successfully in the kernel. */
+		vcpu->stat.mmio_exit_kernel++;
+		kvm_riscv_vcpu_mmio_return(vcpu, run);
+		return 1;
+	}
+
+	/* Exit to userspace for MMIO emulation */
+	vcpu->stat.mmio_exit_user++;
+	run->exit_reason = KVM_EXIT_MMIO;
+
 	return 0;
 }
 
-- 
2.19.1



_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

  parent reply	other threads:[~2020-07-24  8:56 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-24  8:54 [RFC 0/2] Add risc-v vhost-net support Yifei Jiang
2020-07-24  8:54 ` [RFC 1/2] RISC-V: KVM: enable ioeventfd capability and compile for risc-v Yifei Jiang
2020-07-26 12:48   ` Anup Patel
2020-07-24  8:54 ` Yifei Jiang [this message]
2020-07-26 12:57   ` [RFC 2/2] RISC-V: KVM: read\write kernel mmio device support Anup Patel
2020-07-26 12:58   ` Anup Patel
2020-08-28  4:57 ` [RFC 0/2] Add risc-v vhost-net support Anup Patel

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=20200724085441.1514-3-jiangyifei@huawei.com \
    --to=jiangyifei@huawei.com \
    --cc=anup.patel@wdc.com \
    --cc=aou@eecs.berkeley.edu \
    --cc=atish.patra@wdc.com \
    --cc=dengkai1@huawei.com \
    --cc=kvm-riscv@lists.infradead.org \
    --cc=kvm@vger.kernel.org \
    --cc=limingwang@huawei.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=palmer@dabbelt.com \
    --cc=paul.walmsley@sifive.com \
    --cc=pbonzini@redhat.com \
    --cc=victor.zhangxiaofeng@huawei.com \
    --cc=wu.wubin@huawei.com \
    --cc=zhang.zhanghailiang@huawei.com \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).