All the mail mirrored from lore.kernel.org
 help / color / mirror / Atom feed
From: "bibo, mao" <maobibo@loongson.cn>
To: Tianrui Zhao <zhaotianrui@loongson.cn>,
	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>,
	Xi Ruoyao <xry111@xry111.site>
Subject: Re: [PATCH v12 24/31] LoongArch: KVM: Implement handle mmio exception
Date: Tue, 6 Jun 2023 09:41:05 +0800	[thread overview]
Message-ID: <350aa194-de59-d128-c998-eef9d7424f4e@loongson.cn> (raw)
In-Reply-To: <20230530015223.147755-25-zhaotianrui@loongson.cn>

Reviewed-by: Bibo, Mao <maobibo@loongson.cn>

在 2023/5/30 09:52, Tianrui Zhao 写道:
> Implement handle mmio exception, setting the mmio info into vcpu_run and
> return to user space to handle it.
> 
> Signed-off-by: Tianrui Zhao <zhaotianrui@loongson.cn>
> ---
>  arch/loongarch/kvm/exit.c | 308 ++++++++++++++++++++++++++++++++++++++
>  1 file changed, 308 insertions(+)
> 
> diff --git a/arch/loongarch/kvm/exit.c b/arch/loongarch/kvm/exit.c
> index 26283a9d3c6d..1237ceb06d0c 100644
> --- a/arch/loongarch/kvm/exit.c
> +++ b/arch/loongarch/kvm/exit.c
> @@ -209,6 +209,265 @@ int _kvm_emu_idle(struct kvm_vcpu *vcpu)
>  	return EMULATE_DONE;
>  }
>  
> +int _kvm_emu_mmio_write(struct kvm_vcpu *vcpu, larch_inst inst)
> +{
> +	struct kvm_run *run = vcpu->run;
> +	unsigned int rd, op8, opcode;
> +	unsigned long rd_val = 0;
> +	void *data = run->mmio.data;
> +	unsigned long curr_pc;
> +	int ret;
> +
> +	/*
> +	 * Update PC and hold onto current PC in case there is
> +	 * an error and we want to rollback the PC
> +	 */
> +	curr_pc = vcpu->arch.pc;
> +	update_pc(&vcpu->arch);
> +
> +	op8 = (inst.word >> 24) & 0xff;
> +	run->mmio.phys_addr = vcpu->arch.badv;
> +	ret = EMULATE_DO_MMIO;
> +	if (op8 < 0x28) {
> +		/* stptrw/d process */
> +		rd = inst.reg2i14_format.rd;
> +		opcode = inst.reg2i14_format.opcode;
> +
> +		switch (opcode) {
> +		case stptrd_op:
> +			run->mmio.len = 8;
> +			*(unsigned long *)data = vcpu->arch.gprs[rd];
> +			break;
> +		case stptrw_op:
> +			run->mmio.len = 4;
> +			*(unsigned int *)data = vcpu->arch.gprs[rd];
> +			break;
> +		default:
> +			ret = EMULATE_FAIL;
> +			break;
> +		}
> +	} else if (op8 < 0x30) {
> +		/* st.b/h/w/d  process */
> +		rd = inst.reg2i12_format.rd;
> +		opcode = inst.reg2i12_format.opcode;
> +		rd_val = vcpu->arch.gprs[rd];
> +
> +		switch (opcode) {
> +		case std_op:
> +			run->mmio.len = 8;
> +			*(unsigned long *)data = rd_val;
> +			break;
> +		case stw_op:
> +			run->mmio.len = 4;
> +			*(unsigned int *)data = rd_val;
> +			break;
> +		case sth_op:
> +			run->mmio.len = 2;
> +			*(unsigned short *)data = rd_val;
> +			break;
> +		case stb_op:
> +			run->mmio.len = 1;
> +			*(unsigned char *)data = rd_val;
> +			break;
> +		default:
> +			ret = EMULATE_FAIL;
> +			break;
> +		}
> +	} else if (op8 == 0x38) {
> +		/* stxb/h/w/d process */
> +		rd = inst.reg3_format.rd;
> +		opcode = inst.reg3_format.opcode;
> +
> +		switch (opcode) {
> +		case stxb_op:
> +			run->mmio.len = 1;
> +			*(unsigned char *)data = vcpu->arch.gprs[rd];
> +			break;
> +		case stxh_op:
> +			run->mmio.len = 2;
> +			*(unsigned short *)data = vcpu->arch.gprs[rd];
> +			break;
> +		case stxw_op:
> +			run->mmio.len = 4;
> +			*(unsigned int *)data = vcpu->arch.gprs[rd];
> +			break;
> +		case stxd_op:
> +			run->mmio.len = 8;
> +			*(unsigned long *)data = vcpu->arch.gprs[rd];
> +			break;
> +		default:
> +			ret = EMULATE_FAIL;
> +			break;
> +		}
> +	} else
> +		ret = EMULATE_FAIL;
> +
> +	if (ret == EMULATE_DO_MMIO) {
> +		run->mmio.is_write = 1;
> +		vcpu->mmio_needed = 1;
> +		vcpu->mmio_is_write = 1;
> +	} else {
> +		vcpu->arch.pc = curr_pc;
> +		kvm_err("Write not supporded inst=0x%08x @%lx BadVaddr:%#lx\n",
> +			inst.word, vcpu->arch.pc, vcpu->arch.badv);
> +		kvm_arch_vcpu_dump_regs(vcpu);
> +		/* Rollback PC if emulation was unsuccessful */
> +	}
> +
> +	return ret;
> +}
> +
> +int _kvm_emu_mmio_read(struct kvm_vcpu *vcpu, larch_inst inst)
> +{
> +	unsigned int op8, opcode, rd;
> +	struct kvm_run *run = vcpu->run;
> +	int ret;
> +
> +	run->mmio.phys_addr = vcpu->arch.badv;
> +	vcpu->mmio_needed = 2;	/* signed */
> +	op8 = (inst.word >> 24) & 0xff;
> +	ret = EMULATE_DO_MMIO;
> +
> +	if (op8 < 0x28) {
> +		/* ldptr.w/d process */
> +		rd = inst.reg2i14_format.rd;
> +		opcode = inst.reg2i14_format.opcode;
> +
> +		switch (opcode) {
> +		case ldptrd_op:
> +			run->mmio.len = 8;
> +			break;
> +		case ldptrw_op:
> +			run->mmio.len = 4;
> +			break;
> +		default:
> +			break;
> +		}
> +	} else if (op8 < 0x2f) {
> +		/* ld.b/h/w/d, ld.bu/hu/wu process */
> +		rd = inst.reg2i12_format.rd;
> +		opcode = inst.reg2i12_format.opcode;
> +
> +		switch (opcode) {
> +		case ldd_op:
> +			run->mmio.len = 8;
> +			break;
> +		case ldwu_op:
> +			vcpu->mmio_needed = 1;	/* unsigned */
> +			run->mmio.len = 4;
> +			break;
> +		case ldw_op:
> +			run->mmio.len = 4;
> +			break;
> +		case ldhu_op:
> +			vcpu->mmio_needed = 1;	/* unsigned */
> +			run->mmio.len = 2;
> +			break;
> +		case ldh_op:
> +			run->mmio.len = 2;
> +			break;
> +		case ldbu_op:
> +			vcpu->mmio_needed = 1;	/* unsigned */
> +			run->mmio.len = 1;
> +			break;
> +		case ldb_op:
> +			run->mmio.len = 1;
> +			break;
> +		default:
> +			ret = EMULATE_FAIL;
> +			break;
> +		}
> +	} else if (op8 == 0x38) {
> +		/* ldxb/h/w/d, ldxb/h/wu, ldgtb/h/w/d, ldleb/h/w/d process */
> +		rd = inst.reg3_format.rd;
> +		opcode = inst.reg3_format.opcode;
> +
> +		switch (opcode) {
> +		case ldxb_op:
> +			run->mmio.len = 1;
> +			break;
> +		case ldxbu_op:
> +			run->mmio.len = 1;
> +			vcpu->mmio_needed = 1;	/* unsigned */
> +			break;
> +		case ldxh_op:
> +			run->mmio.len = 2;
> +			break;
> +		case ldxhu_op:
> +			run->mmio.len = 2;
> +			vcpu->mmio_needed = 1;	/* unsigned */
> +			break;
> +		case ldxw_op:
> +			run->mmio.len = 4;
> +			break;
> +		case ldxwu_op:
> +			run->mmio.len = 4;
> +			vcpu->mmio_needed = 1;	/* unsigned */
> +			break;
> +		case ldxd_op:
> +			run->mmio.len = 8;
> +			break;
> +		default:
> +			ret = EMULATE_FAIL;
> +			break;
> +		}
> +	} else
> +		ret = EMULATE_FAIL;
> +
> +	if (ret == EMULATE_DO_MMIO) {
> +		/* Set for _kvm_complete_mmio_read use */
> +		vcpu->arch.io_gpr = rd;
> +		run->mmio.is_write = 0;
> +		vcpu->mmio_is_write = 0;
> +	} else {
> +		kvm_err("Load not supporded inst=0x%08x @%lx BadVaddr:%#lx\n",
> +			inst.word, vcpu->arch.pc, vcpu->arch.badv);
> +		kvm_arch_vcpu_dump_regs(vcpu);
> +		vcpu->mmio_needed = 0;
> +	}
> +	return ret;
> +}
> +
> +int _kvm_complete_mmio_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;
> +
> +	/* update with new PC */
> +	update_pc(&vcpu->arch);
> +	switch (run->mmio.len) {
> +	case 8:
> +		*gpr = *(s64 *)run->mmio.data;
> +		break;
> +	case 4:
> +		if (vcpu->mmio_needed == 2)
> +			*gpr = *(int *)run->mmio.data;
> +		else
> +			*gpr = *(unsigned int *)run->mmio.data;
> +		break;
> +	case 2:
> +		if (vcpu->mmio_needed == 2)
> +			*gpr = *(short *) run->mmio.data;
> +		else
> +			*gpr = *(unsigned short *)run->mmio.data;
> +
> +		break;
> +	case 1:
> +		if (vcpu->mmio_needed == 2)
> +			*gpr = *(char *) run->mmio.data;
> +		else
> +			*gpr = *(unsigned char *) run->mmio.data;
> +		break;
> +	default:
> +		kvm_err("Bad MMIO length: %d,addr is 0x%lx",
> +				run->mmio.len, vcpu->arch.badv);
> +		er = EMULATE_FAIL;
> +		break;
> +	}
> +
> +	return er;
> +}
> +
>  static int _kvm_trap_handle_gspr(struct kvm_vcpu *vcpu)
>  {
>  	enum emulation_result er = EMULATE_DONE;
> @@ -323,3 +582,52 @@ static int _kvm_handle_gspr(struct kvm_vcpu *vcpu)
>  	}
>  	return ret;
>  }
> +
> +static int _kvm_handle_mmu_fault(struct kvm_vcpu *vcpu, bool write)
> +{
> +	struct kvm_run *run = vcpu->run;
> +	unsigned long badv = vcpu->arch.badv;
> +	larch_inst inst;
> +	enum emulation_result er = EMULATE_DONE;
> +	int ret;
> +
> +	ret = kvm_handle_mm_fault(vcpu, badv, write);
> +	if (ret) {
> +		/* Treat as MMIO */
> +		inst.word = vcpu->arch.badi;
> +		if (write) {
> +			er = _kvm_emu_mmio_write(vcpu, inst);
> +		} else {
> +			/* A code fetch fault doesn't count as an MMIO */
> +			if (kvm_is_ifetch_fault(&vcpu->arch)) {
> +				kvm_err("%s ifetch error addr:%lx\n", __func__, badv);
> +				run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
> +				return RESUME_HOST;
> +			}
> +
> +			er = _kvm_emu_mmio_read(vcpu, inst);
> +		}
> +	}
> +
> +	if (er == EMULATE_DONE) {
> +		ret = RESUME_GUEST;
> +	} else if (er == EMULATE_DO_MMIO) {
> +		run->exit_reason = KVM_EXIT_MMIO;
> +		ret = RESUME_HOST;
> +	} else {
> +		run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
> +		ret = RESUME_HOST;
> +	}
> +
> +	return ret;
> +}
> +
> +static int _kvm_handle_write_fault(struct kvm_vcpu *vcpu)
> +{
> +	return _kvm_handle_mmu_fault(vcpu, true);
> +}
> +
> +static int _kvm_handle_read_fault(struct kvm_vcpu *vcpu)
> +{
> +	return _kvm_handle_mmu_fault(vcpu, false);
> +}


  reply	other threads:[~2023-06-06  1:41 UTC|newest]

Thread overview: 73+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-30  1:51 [PATCH v12 00/31] Add KVM LoongArch support Tianrui Zhao
2023-05-30  1:51 ` [PATCH v12 01/31] LoongArch: KVM: Add kvm related header files Tianrui Zhao
2023-06-05  5:00   ` Huacai Chen
2023-06-05 10:18     ` Paolo Bonzini
2023-06-05 12:22   ` bibo, mao
2023-06-06  6:30   ` Youling Tang
2023-06-06 10:00     ` Tianrui Zhao
2023-05-30  1:51 ` [PATCH v12 02/31] LoongArch: KVM: Implement kvm module related interface Tianrui Zhao
2023-06-05 12:27   ` bibo, mao
2023-05-30  1:51 ` [PATCH v12 03/31] LoongArch: KVM: Implement kvm hardware enable, disable interface Tianrui Zhao
2023-05-30  1:51 ` [PATCH v12 04/31] LoongArch: KVM: Implement VM related functions Tianrui Zhao
2023-06-05 12:31   ` bibo, mao
2023-05-30  1:51 ` [PATCH v12 05/31] LoongArch: KVM: Add vcpu related header files Tianrui Zhao
2023-06-05 12:42   ` bibo, mao
2023-06-06  2:00     ` Tianrui Zhao
2023-05-30  1:51 ` [PATCH v12 06/31] LoongArch: KVM: Implement vcpu create and destroy interface Tianrui Zhao
2023-06-05 12:44   ` bibo, mao
2023-05-30  1:51 ` [PATCH v12 07/31] LoongArch: KVM: Implement vcpu run interface Tianrui Zhao
2023-06-05 12:53   ` bibo, mao
2023-06-06  2:02     ` Tianrui Zhao
2023-05-30  1:52 ` [PATCH v12 08/31] LoongArch: KVM: Implement vcpu handle exit interface Tianrui Zhao
2023-06-05 13:03   ` bibo, mao
2023-06-06  2:13     ` Tianrui Zhao
2023-05-30  1:52 ` [PATCH v12 09/31] LoongArch: KVM: Implement vcpu get, vcpu set registers Tianrui Zhao
2023-06-05 13:04   ` bibo, mao
2023-06-06  6:41   ` Youling Tang
2023-06-06  9:12     ` Tianrui Zhao
2023-05-30  1:52 ` [PATCH v12 10/31] LoongArch: KVM: Implement vcpu ENABLE_CAP ioctl interface Tianrui Zhao
2023-06-05 13:12   ` bibo, mao
2023-06-06  3:20     ` Tianrui Zhao
2023-05-30  1:52 ` [PATCH v12 11/31] LoongArch: KVM: Implement fpu related operations for vcpu Tianrui Zhao
2023-06-05 13:13   ` bibo, mao
2023-05-30  1:52 ` [PATCH v12 12/31] LoongArch: KVM: Implement vcpu interrupt operations Tianrui Zhao
2023-06-05 13:13   ` bibo, mao
2023-06-06  6:49   ` Youling Tang
2023-06-06  9:13     ` Tianrui Zhao
2023-05-30  1:52 ` [PATCH v12 13/31] LoongArch: KVM: Implement misc vcpu related interfaces Tianrui Zhao
2023-05-30  1:52 ` [PATCH v12 14/31] LoongArch: KVM: Implement vcpu load and vcpu put operations Tianrui Zhao
2023-05-30  1:52 ` [PATCH v12 15/31] LoongArch: KVM: Implement vcpu status description Tianrui Zhao
2023-06-05 13:04   ` bibo, mao
2023-05-30  1:52 ` [PATCH v12 16/31] LoongArch: KVM: Implement update VM id function Tianrui Zhao
2023-06-06  1:13   ` bibo, mao
2023-05-30  1:52 ` [PATCH v12 17/31] LoongArch: KVM: Implement virtual machine tlb operations Tianrui Zhao
2023-06-06  1:20   ` bibo, mao
2023-05-30  1:52 ` [PATCH v12 18/31] LoongArch: KVM: Implement vcpu timer operations Tianrui Zhao
2023-05-30  1:52 ` [PATCH v12 19/31] LoongArch: KVM: Implement kvm mmu operations Tianrui Zhao
2023-06-06  1:28   ` bibo, mao
2023-06-06  2:51     ` Tianrui Zhao
2023-05-30  1:52 ` [PATCH v12 20/31] LoongArch: KVM: Implement handle csr excption Tianrui Zhao
2023-06-06  1:35   ` bibo, mao
2023-06-06  2:59     ` Tianrui Zhao
2023-05-30  1:52 ` [PATCH v12 21/31] LoongArch: KVM: Implement handle iocsr exception Tianrui Zhao
2023-06-06  1:36   ` bibo, mao
2023-05-30  1:52 ` [PATCH v12 22/31] LoongArch: KVM: Implement handle idle exception Tianrui Zhao
2023-06-06  1:36   ` bibo, mao
2023-05-30  1:52 ` [PATCH v12 23/31] LoongArch: KVM: Implement handle gspr exception Tianrui Zhao
2023-06-06  1:38   ` bibo, mao
2023-05-30  1:52 ` [PATCH v12 24/31] LoongArch: KVM: Implement handle mmio exception Tianrui Zhao
2023-06-06  1:41   ` bibo, mao [this message]
2023-05-30  1:52 ` [PATCH v12 25/31] LoongArch: KVM: Implement handle fpu exception Tianrui Zhao
2023-06-06  1:42   ` bibo, mao
2023-05-30  1:52 ` [PATCH v12 26/31] LoongArch: KVM: Implement kvm exception vector Tianrui Zhao
2023-06-06  1:43   ` bibo, mao
2023-06-06  7:00   ` Youling Tang
2023-06-06  9:16     ` Tianrui Zhao
2023-06-08 11:44     ` 赵天瑞
2023-05-30  1:52 ` [PATCH v12 27/31] LoongArch: KVM: Implement vcpu world switch Tianrui Zhao
2023-06-06  2:27   ` bibo, mao
2023-05-30  1:52 ` [PATCH v12 28/31] LoongArch: KVM: Implement probe virtualization when LoongArch cpu init Tianrui Zhao
2023-05-30  1:52 ` [PATCH v12 29/31] LoongArch: KVM: Enable kvm config and add the makefile Tianrui Zhao
2023-06-06  2:37   ` bibo, mao
2023-05-30  1:52 ` [PATCH v12 30/31] LoongArch: KVM: Supplement kvm document about LoongArch-specific part Tianrui Zhao
2023-05-30  1:52 ` [PATCH v12 31/31] LoongArch: KVM: Add maintainers for LoongArch KVM Tianrui Zhao

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=350aa194-de59-d128-c998-eef9d7424f4e@loongson.cn \
    --to=maobibo@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=oliver.upton@linux.dev \
    --cc=pbonzini@redhat.com \
    --cc=xry111@xry111.site \
    --cc=zhaotianrui@loongson.cn \
    /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.