From nobody Sat Nov 30 10:30:57 2024 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9CC5418FDA5; Tue, 10 Sep 2024 12:01:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=114.242.206.163 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725969704; cv=none; b=NHOFV2ubaXF6IhU5WX+KeSW3nrkpBMfsOU8MB5F4ZwrWDSaO6RAJf8xMOW9kXqEUvXZVLpWihTv6jJrFBmGtNfv2aPTQgOv48HTJToxFDBRpdIZTw/A5q4/VoNBLd6oAsjXIj5SUHuo1sCDET8ROPHrGzzgT+YuFVic9ZPoSvl0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725969704; c=relaxed/simple; bh=7Z6oj8tec7G+l6Fi7f4VQ2bYnwgurw5JBRAmZfQoYHI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=IRMw34ovDdzqsXH/Mj9pcFZ+ztgaEVS3SFbI9q+PohFRjCGsb4zLIi39/aZ4DgjP9xAynz47FnT1lv2ScobtVQu5PLaGA8gXXUa5xV2coZn11rcW0YRhwCyyLV0k3cWaT12giYXyCE1vJcDcyqgSEamH6e5PxsHt7P9SVCgW3S4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn; spf=pass smtp.mailfrom=loongson.cn; arc=none smtp.client-ip=114.242.206.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=loongson.cn Received: from loongson.cn (unknown [10.2.5.185]) by gateway (Coremail) with SMTP id _____8CxxuglNeBmEa8DAA--.7542S3; Tue, 10 Sep 2024 20:01:41 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by front2 (Coremail) with SMTP id qciowMCxrsceNeBmyGIDAA--.15904S3; Tue, 10 Sep 2024 20:01:40 +0800 (CST) From: Xianglai Li To: linux-kernel@vger.kernel.org Cc: Tianrui Zhao , Bibo Mao , Huacai Chen , kvm@vger.kernel.org, loongarch@lists.linux.dev, Paolo Bonzini , WANG Xuerui , Xianglai li Subject: [PATCH V3 01/11] LoongArch: KVM: Add iocsr and mmio bus simulation in kernel Date: Tue, 10 Sep 2024 19:43:50 +0800 Message-Id: <20240910114400.4062433-2-lixianglai@loongson.cn> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20240910114400.4062433-1-lixianglai@loongson.cn> References: <20240910114400.4062433-1-lixianglai@loongson.cn> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: qciowMCxrsceNeBmyGIDAA--.15904S3 X-CM-SenderInfo: 5ol0xt5qjotxo6or00hjvr0hdfq/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== Content-Type: text/plain; charset="utf-8" Add iocsr and mmio memory read and write simulation to the kernel. When the VM accesses the device address space through iocsr instructions or mmio, it does not need to return to the qemu user mode but directly completes the access in the kernel mode. Signed-off-by: Tianrui Zhao Signed-off-by: Xianglai Li --- Cc: Bibo Mao =20 Cc: Huacai Chen =20 Cc: kvm@vger.kernel.org=20 Cc: loongarch@lists.linux.dev=20 Cc: Paolo Bonzini =20 Cc: Tianrui Zhao =20 Cc: WANG Xuerui =20 Cc: Xianglai li =20 arch/loongarch/kvm/exit.c | 86 +++++++++++++++++++++++++++----------- include/linux/kvm_host.h | 1 + include/trace/events/kvm.h | 35 ++++++++++++++++ 3 files changed, 97 insertions(+), 25 deletions(-) diff --git a/arch/loongarch/kvm/exit.c b/arch/loongarch/kvm/exit.c index ea73f9dc2cc6..6b15117106f9 100644 --- a/arch/loongarch/kvm/exit.c +++ b/arch/loongarch/kvm/exit.c @@ -151,7 +151,7 @@ static int kvm_handle_csr(struct kvm_vcpu *vcpu, larch_= inst inst) int kvm_emu_iocsr(larch_inst inst, struct kvm_run *run, struct kvm_vcpu *v= cpu) { int ret; - unsigned long val; + unsigned long *val; u32 addr, rd, rj, opcode; =20 /* @@ -164,6 +164,7 @@ int kvm_emu_iocsr(larch_inst inst, struct kvm_run *run,= struct kvm_vcpu *vcpu) ret =3D EMULATE_DO_IOCSR; run->iocsr_io.phys_addr =3D addr; run->iocsr_io.is_write =3D 0; + val =3D &vcpu->arch.gprs[rd]; =20 /* LoongArch is Little endian */ switch (opcode) { @@ -196,18 +197,30 @@ int kvm_emu_iocsr(larch_inst inst, struct kvm_run *ru= n, struct kvm_vcpu *vcpu) run->iocsr_io.is_write =3D 1; break; default: - ret =3D EMULATE_FAIL; - break; + return EMULATE_FAIL; } =20 - if (ret =3D=3D EMULATE_DO_IOCSR) { - if (run->iocsr_io.is_write) { - val =3D vcpu->arch.gprs[rd]; - memcpy(run->iocsr_io.data, &val, run->iocsr_io.len); - } - vcpu->arch.io_gpr =3D rd; + if (run->iocsr_io.is_write) { + if (!kvm_io_bus_write(vcpu, + KVM_IOCSR_BUS, addr, run->iocsr_io.len, val)) + ret =3D EMULATE_DONE; + else + /* Save data and let user space to write it */ + memcpy(run->iocsr_io.data, val, run->iocsr_io.len); + trace_kvm_iocsr(KVM_TRACE_IOCSR_WRITE, + run->iocsr_io.len, + addr, val); + } else { + if (!kvm_io_bus_read(vcpu, + KVM_IOCSR_BUS, addr, run->iocsr_io.len, val)) + ret =3D EMULATE_DONE; + else + /* Save register id for iocsr read completion */ + vcpu->arch.io_gpr =3D rd; + trace_kvm_iocsr(KVM_TRACE_IOCSR_READ, + run->iocsr_io.len, + addr, NULL); } - return ret; } =20 @@ -441,19 +454,32 @@ int kvm_emu_mmio_read(struct kvm_vcpu *vcpu, larch_in= st inst) } =20 if (ret =3D=3D EMULATE_DO_MMIO) { + trace_kvm_mmio(KVM_TRACE_MMIO_READ, run->mmio.len, + run->mmio.phys_addr, NULL); + /* + * if mmio device such as pch pic is emulated in KVM, + * it need not return to user space to handle the mmio + * exception. + */ + ret =3D kvm_io_bus_read(vcpu, KVM_MMIO_BUS, vcpu->arch.badv, + run->mmio.len, &vcpu->arch.gprs[rd]); + if (!ret) { + update_pc(&vcpu->arch); + vcpu->mmio_needed =3D 0; + return EMULATE_DONE; + } + /* Set for kvm_complete_mmio_read() use */ vcpu->arch.io_gpr =3D rd; run->mmio.is_write =3D 0; vcpu->mmio_is_write =3D 0; - trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, run->mmio.len, - run->mmio.phys_addr, NULL); - } else { - kvm_err("Read not supported Inst=3D0x%08x @%lx BadVaddr:%#lx\n", - inst.word, vcpu->arch.pc, vcpu->arch.badv); - kvm_arch_vcpu_dump_regs(vcpu); - vcpu->mmio_needed =3D 0; + return EMULATE_DO_MMIO; } =20 + kvm_err("Read not supported Inst=3D0x%08x @%lx BadVaddr:%#lx\n", + inst.word, vcpu->arch.pc, vcpu->arch.badv); + kvm_arch_vcpu_dump_regs(vcpu); + vcpu->mmio_needed =3D 0; return ret; } =20 @@ -594,19 +620,29 @@ int kvm_emu_mmio_write(struct kvm_vcpu *vcpu, larch_i= nst inst) } =20 if (ret =3D=3D EMULATE_DO_MMIO) { + trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, run->mmio.len, + run->mmio.phys_addr, data); + /* + * if mmio device such as pch pic is emulated in KVM, + * it need not return to user space to handle the mmio + * exception. + */ + ret =3D kvm_io_bus_write(vcpu, KVM_MMIO_BUS, vcpu->arch.badv, + run->mmio.len, data); + if (!ret) + return EMULATE_DONE; + run->mmio.is_write =3D 1; vcpu->mmio_needed =3D 1; vcpu->mmio_is_write =3D 1; - trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, run->mmio.len, - run->mmio.phys_addr, data); - } else { - vcpu->arch.pc =3D curr_pc; - kvm_err("Write not supported Inst=3D0x%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 EMULATE_DO_MMIO; } =20 + vcpu->arch.pc =3D curr_pc; + kvm_err("Write not supported Inst=3D0x%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; } =20 diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 0d5125a3e31a..1149cc6a9dde 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -219,6 +219,7 @@ enum kvm_bus { KVM_PIO_BUS, KVM_VIRTIO_CCW_NOTIFY_BUS, KVM_FAST_MMIO_BUS, + KVM_IOCSR_BUS, KVM_NR_BUSES }; =20 diff --git a/include/trace/events/kvm.h b/include/trace/events/kvm.h index 74e40d5d4af4..2391cb257636 100644 --- a/include/trace/events/kvm.h +++ b/include/trace/events/kvm.h @@ -236,6 +236,41 @@ TRACE_EVENT(kvm_mmio, __entry->len, __entry->gpa, __entry->val) ); =20 +#define KVM_TRACE_IOCSR_READ_UNSATISFIED 0 +#define KVM_TRACE_IOCSR_READ 1 +#define KVM_TRACE_IOCSR_WRITE 2 + +#define kvm_trace_symbol_iocsr \ + ({ KVM_TRACE_IOCSR_READ_UNSATISFIED, "unsatisfied-read" }, \ + { KVM_TRACE_IOCSR_READ, "read" }, \ + { KVM_TRACE_IOCSR_WRITE, "write" }) + +TRACE_EVENT(kvm_iocsr, + TP_PROTO(int type, int len, u64 gpa, void *val), + TP_ARGS(type, len, gpa, val), + + TP_STRUCT__entry( + __field(u32, type) + __field(u32, len) + __field(u64, gpa) + __field(u64, val) + ), + + TP_fast_assign( + __entry->type =3D type; + __entry->len =3D len; + __entry->gpa =3D gpa; + __entry->val =3D 0; + if (val) + memcpy(&__entry->val, val, + min_t(u32, sizeof(__entry->val), len)); + ), + + TP_printk("iocsr %s len %u gpa 0x%llx val 0x%llx", + __print_symbolic(__entry->type, kvm_trace_symbol_iocsr), + __entry->len, __entry->gpa, __entry->val) +); + #define kvm_fpu_load_symbol \ {0, "unload"}, \ {1, "load"} --=20 2.39.1