All the mail mirrored from lore.kernel.org
 help / color / mirror / Atom feed
From: Tianrui Zhao <zhaotianrui@loongson.cn>
To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	Huacai Chen <chenhuacai@kernel.org>,
	WANG Xuerui <kernel@xen0n.name>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	loongarch@lists.linux.dev, Jens Axboe <axboe@kernel.dk>,
	Mark Brown <broonie@kernel.org>,
	Alex Deucher <alexander.deucher@amd.com>,
	Oliver Upton <oliver.upton@linux.dev>,
	maobibo@loongson.cn, Xi Ruoyao <xry111@xry111.site>,
	zhaotianrui@loongson.cn
Subject: [PATCH v20 21/30] LoongArch: KVM: Implement handle iocsr exception
Date: Thu, 31 Aug 2023 16:30:11 +0800	[thread overview]
Message-ID: <20230831083020.2187109-22-zhaotianrui@loongson.cn> (raw)
In-Reply-To: <20230831083020.2187109-1-zhaotianrui@loongson.cn>

Implement kvm handle vcpu iocsr exception, setting the iocsr info into
vcpu_run and return to user space to handle it.

Reviewed-by: Bibo Mao <maobibo@loongson.cn>
Signed-off-by: Tianrui Zhao <zhaotianrui@loongson.cn>
---
 arch/loongarch/include/asm/inst.h | 16 ++++++
 arch/loongarch/kvm/exit.c         | 92 +++++++++++++++++++++++++++++++
 2 files changed, 108 insertions(+)

diff --git a/arch/loongarch/include/asm/inst.h b/arch/loongarch/include/asm/inst.h
index 71e1ed4165..008a88ead6 100644
--- a/arch/loongarch/include/asm/inst.h
+++ b/arch/loongarch/include/asm/inst.h
@@ -65,6 +65,14 @@ enum reg2_op {
 	revbd_op	= 0x0f,
 	revh2w_op	= 0x10,
 	revhd_op	= 0x11,
+	iocsrrdb_op     = 0x19200,
+	iocsrrdh_op     = 0x19201,
+	iocsrrdw_op     = 0x19202,
+	iocsrrdd_op     = 0x19203,
+	iocsrwrb_op     = 0x19204,
+	iocsrwrh_op     = 0x19205,
+	iocsrwrw_op     = 0x19206,
+	iocsrwrd_op     = 0x19207,
 };
 
 enum reg2i5_op {
@@ -318,6 +326,13 @@ struct reg2bstrd_format {
 	unsigned int opcode : 10;
 };
 
+struct reg2csr_format {
+	unsigned int rd : 5;
+	unsigned int rj : 5;
+	unsigned int csr : 14;
+	unsigned int opcode : 8;
+};
+
 struct reg3_format {
 	unsigned int rd : 5;
 	unsigned int rj : 5;
@@ -346,6 +361,7 @@ union loongarch_instruction {
 	struct reg2i14_format	reg2i14_format;
 	struct reg2i16_format	reg2i16_format;
 	struct reg2bstrd_format	reg2bstrd_format;
+	struct reg2csr_format   reg2csr_format;
 	struct reg3_format	reg3_format;
 	struct reg3sa2_format	reg3sa2_format;
 };
diff --git a/arch/loongarch/kvm/exit.c b/arch/loongarch/kvm/exit.c
index 18635333fc..32edd915eb 100644
--- a/arch/loongarch/kvm/exit.c
+++ b/arch/loongarch/kvm/exit.c
@@ -96,3 +96,95 @@ static int _kvm_handle_csr(struct kvm_vcpu *vcpu, larch_inst inst)
 
 	return EMULATE_DONE;
 }
+
+int _kvm_emu_iocsr(larch_inst inst, struct kvm_run *run, struct kvm_vcpu *vcpu)
+{
+	u32 rd, rj, opcode;
+	u32 addr;
+	unsigned long val;
+	int ret;
+
+	/*
+	 * Each IOCSR with different opcode
+	 */
+	rd = inst.reg2_format.rd;
+	rj = inst.reg2_format.rj;
+	opcode = inst.reg2_format.opcode;
+	addr = vcpu->arch.gprs[rj];
+	ret = EMULATE_DO_IOCSR;
+	run->iocsr_io.phys_addr = addr;
+	run->iocsr_io.is_write = 0;
+
+	/* LoongArch is Little endian */
+	switch (opcode) {
+	case iocsrrdb_op:
+		run->iocsr_io.len = 1;
+		break;
+	case iocsrrdh_op:
+		run->iocsr_io.len = 2;
+		break;
+	case iocsrrdw_op:
+		run->iocsr_io.len = 4;
+		break;
+	case iocsrrdd_op:
+		run->iocsr_io.len = 8;
+		break;
+	case iocsrwrb_op:
+		run->iocsr_io.len = 1;
+		run->iocsr_io.is_write = 1;
+		break;
+	case iocsrwrh_op:
+		run->iocsr_io.len = 2;
+		run->iocsr_io.is_write = 1;
+		break;
+	case iocsrwrw_op:
+		run->iocsr_io.len = 4;
+		run->iocsr_io.is_write = 1;
+		break;
+	case iocsrwrd_op:
+		run->iocsr_io.len = 8;
+		run->iocsr_io.is_write = 1;
+		break;
+	default:
+		ret = EMULATE_FAIL;
+		break;
+	}
+
+	if (ret == EMULATE_DO_IOCSR) {
+		if (run->iocsr_io.is_write) {
+			val = vcpu->arch.gprs[rd];
+			memcpy(run->iocsr_io.data, &val, run->iocsr_io.len);
+		}
+		vcpu->arch.io_gpr = rd;
+	}
+
+	return ret;
+}
+
+int _kvm_complete_iocsr_read(struct kvm_vcpu *vcpu, struct kvm_run *run)
+{
+	unsigned long *gpr = &vcpu->arch.gprs[vcpu->arch.io_gpr];
+	enum emulation_result er = EMULATE_DONE;
+
+	switch (run->iocsr_io.len) {
+	case 8:
+		*gpr = *(s64 *)run->iocsr_io.data;
+		break;
+	case 4:
+		*gpr = *(int *)run->iocsr_io.data;
+		break;
+	case 2:
+		*gpr = *(short *)run->iocsr_io.data;
+		break;
+	case 1:
+		*gpr = *(char *) run->iocsr_io.data;
+		break;
+	default:
+		kvm_err("Bad IOCSR length: %d,addr is 0x%lx",
+				run->iocsr_io.len, vcpu->arch.badv);
+		er = EMULATE_FAIL;
+		break;
+	}
+
+	return er;
+}
-- 
2.27.0


  parent reply	other threads:[~2023-08-31  8:41 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-31  8:29 [PATCH v20 00/30] Add KVM LoongArch support Tianrui Zhao
2023-08-31  8:29 ` [PATCH v20 01/30] LoongArch: KVM: Add kvm related header files Tianrui Zhao
2023-09-11  4:59   ` Huacai Chen
2023-09-11  9:41     ` zhaotianrui
2023-08-31  8:29 ` [PATCH v20 02/30] LoongArch: KVM: Implement kvm module related interface Tianrui Zhao
2023-08-31  8:29 ` [PATCH v20 03/30] LoongArch: KVM: Implement kvm hardware enable, disable interface Tianrui Zhao
2023-08-31  8:29 ` [PATCH v20 04/30] LoongArch: KVM: Implement VM related functions Tianrui Zhao
2023-08-31  8:29 ` [PATCH v20 05/30] LoongArch: KVM: Add vcpu related header files Tianrui Zhao
2023-09-11  8:07   ` Huacai Chen
2023-09-12  8:26     ` zhaotianrui
2023-08-31  8:29 ` [PATCH v20 06/30] LoongArch: KVM: Implement vcpu create and destroy interface Tianrui Zhao
2023-08-31  8:29 ` [PATCH v20 07/30] LoongArch: KVM: Implement vcpu run interface Tianrui Zhao
2023-08-31  8:29 ` [PATCH v20 08/30] LoongArch: KVM: Implement vcpu handle exit interface Tianrui Zhao
2023-08-31  8:29 ` [PATCH v20 09/30] LoongArch: KVM: Implement vcpu get, vcpu set registers Tianrui Zhao
2023-09-11  9:03   ` Huacai Chen
2023-09-11 10:03     ` zhaotianrui
2023-09-11 10:13       ` zhaotianrui
2023-09-11 11:49       ` Huacai Chen
2023-09-12  2:41         ` bibo mao
2023-08-31  8:30 ` [PATCH v20 10/30] LoongArch: KVM: Implement vcpu ENABLE_CAP ioctl interface Tianrui Zhao
2023-08-31  8:30 ` [PATCH v20 11/30] LoongArch: KVM: Implement fpu related operations for vcpu Tianrui Zhao
2023-08-31  8:30 ` [PATCH v20 12/30] LoongArch: KVM: Implement vcpu interrupt operations Tianrui Zhao
2023-08-31  8:30 ` [PATCH v20 13/30] LoongArch: KVM: Implement misc vcpu related interfaces Tianrui Zhao
2023-08-31  8:30 ` [PATCH v20 14/30] LoongArch: KVM: Implement vcpu load and vcpu put operations Tianrui Zhao
2023-08-31  8:30 ` [PATCH v20 15/30] LoongArch: KVM: Implement vcpu status description Tianrui Zhao
2023-08-31  8:30 ` [PATCH v20 16/30] LoongArch: KVM: Implement update VM id function Tianrui Zhao
2023-09-11 10:00   ` Huacai Chen
2023-09-11 10:23     ` bibo mao
2023-09-12  3:51       ` Huacai Chen
2023-08-31  8:30 ` [PATCH v20 17/30] LoongArch: KVM: Implement virtual machine tlb operations Tianrui Zhao
2023-08-31  8:30 ` [PATCH v20 18/30] LoongArch: KVM: Implement vcpu timer operations Tianrui Zhao
2023-08-31  8:30 ` [PATCH v20 19/30] LoongArch: KVM: Implement kvm mmu operations Tianrui Zhao
2023-09-07 19:57   ` WANG Xuerui
2023-09-12  9:42     ` zhaotianrui
2023-08-31  8:30 ` [PATCH v20 20/30] LoongArch: KVM: Implement handle csr excption Tianrui Zhao
2023-08-31  8:30 ` Tianrui Zhao [this message]
2023-08-31  8:30 ` [PATCH v20 22/30] LoongArch: KVM: Implement handle idle exception Tianrui Zhao
2023-08-31  8:30 ` [PATCH v20 23/30] LoongArch: KVM: Implement handle gspr exception Tianrui Zhao
2023-08-31  8:30 ` [PATCH v20 24/30] LoongArch: KVM: Implement handle mmio exception Tianrui Zhao
2023-08-31  8:30 ` [PATCH v20 25/30] LoongArch: KVM: Implement handle fpu exception Tianrui Zhao
2023-08-31  8:30 ` [PATCH v20 26/30] LoongArch: KVM: Implement kvm exception vector Tianrui Zhao
2023-08-31  8:30 ` [PATCH v20 27/30] LoongArch: KVM: Implement vcpu world switch Tianrui Zhao
2023-09-07 20:04   ` WANG Xuerui
2023-09-12  9:55     ` zhaotianrui
2023-08-31  8:30 ` [PATCH v20 28/30] LoongArch: KVM: Enable kvm config and add the makefile Tianrui Zhao
2023-09-07 20:10   ` WANG Xuerui
2023-09-08  1:40     ` Huacai Chen
2023-09-08  1:49       ` bibo mao
2023-09-08  1:54         ` Huacai Chen
2023-09-12  9:47     ` zhaotianrui
2023-09-11  7:30   ` WANG Xuerui
2023-09-12  1:57     ` zhaotianrui
2023-08-31  8:30 ` [PATCH v20 29/30] LoongArch: KVM: Supplement kvm document about LoongArch-specific part Tianrui Zhao
2023-08-31  8:30 ` [PATCH v20 30/30] LoongArch: KVM: Add maintainers for LoongArch KVM Tianrui Zhao
2023-09-11  4:02 ` [PATCH v20 00/30] Add KVM LoongArch support Huacai Chen
2023-09-11  9:34   ` zhaotianrui

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=20230831083020.2187109-22-zhaotianrui@loongson.cn \
    --to=zhaotianrui@loongson.cn \
    --cc=alexander.deucher@amd.com \
    --cc=axboe@kernel.dk \
    --cc=broonie@kernel.org \
    --cc=chenhuacai@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=kernel@xen0n.name \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=loongarch@lists.linux.dev \
    --cc=maobibo@loongson.cn \
    --cc=oliver.upton@linux.dev \
    --cc=pbonzini@redhat.com \
    --cc=xry111@xry111.site \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.