Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp2006922ybh; Fri, 24 Jul 2020 01:58:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxz5abNH4Y71cQRmi+UNB/dttxmptw02QmgPj0H7Gu6zADItwQRtDIC7ReuZc3e9WQZ0euL X-Received: by 2002:a17:906:434c:: with SMTP id z12mr7813236ejm.33.1595581100133; Fri, 24 Jul 2020 01:58:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595581100; cv=none; d=google.com; s=arc-20160816; b=yTw3XS14MK0u6USlC178dHiyYICBcbkFyB9Ovbg2RwqvJxT7aM0QG+IFvCILeuK7RS +51WoLd0N6a9LS0icQfrmpICmoAg58X7XKNO5bdzJgmWndNAuK3sip8+fiXF6HJxU94y 5WiRA4wUeRdk4GYg3RlNYa0kfLHTiJRXo8rfx6JZxDB/HK4vS4NPrDar5zjuTe+c87X2 FA7j7+3XsZ6IrRaTtSWUffzwgRsKGecHi11vWKeHEIuaF5UEihVd1eqlDmB01z1tDo1v VIQxnQezpD4Oe3bFs5Qjurqh/Ok1Ab4YBVjQCY8emXmkdppobB76zsCU2MKFGVkdHRJI GW3g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=/RKJgNlwklyRlpj0TTG9e4hxGSQKIPeT/MAu3jGvE9o=; b=W2oES424chvYEDKlwZUWfkHLh3JCduyovQz4k8jfyslNqsGFnKiqi4CAN+zD1h+qS0 653uVI+ZOfbPTtQA9rvG8p5q6QV2KBI5u3WxpMnMwC24OmGUZy6XWaNj8ILeUcqMppmH ImrCN4Fcs5UMnH7GKToZRGiaJ8Ct5NLA7Yqjy90lzELufvEhp3Z0RPzvbUJkr+T9YRac goldBSW2Ijb6MEFDyUpZ4KnY7jlbOjFTXbLEjtctTACPk2NQgfdIYir8zvQaIskO/etG QP9Yx+OBh0NYJsj/9Cxwl2yyhU/KmShG+Hh7AuwyESoOo6I5P1nsyEP2Asx/Y6lBW4lj YQNg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g6si94985edk.604.2020.07.24.01.57.57; Fri, 24 Jul 2020 01:58:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727819AbgGXIzl (ORCPT + 99 others); Fri, 24 Jul 2020 04:55:41 -0400 Received: from szxga06-in.huawei.com ([45.249.212.32]:55964 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727109AbgGXIzl (ORCPT ); Fri, 24 Jul 2020 04:55:41 -0400 Received: from DGGEMS401-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id E57FCE0BEBBE4295DAF2; Fri, 24 Jul 2020 16:55:38 +0800 (CST) Received: from huawei.com (10.174.187.31) by DGGEMS401-HUB.china.huawei.com (10.3.19.201) with Microsoft SMTP Server id 14.3.487.0; Fri, 24 Jul 2020 16:55:29 +0800 From: Yifei Jiang To: , , , CC: , , , , , , , , , , , Yifei Jiang Subject: [RFC 2/2] RISC-V: KVM: read\write kernel mmio device support Date: Fri, 24 Jul 2020 16:54:41 +0800 Message-ID: <20200724085441.1514-3-jiangyifei@huawei.com> X-Mailer: git-send-email 2.26.2.windows.1 In-Reply-To: <20200724085441.1514-1-jiangyifei@huawei.com> References: <20200724085441.1514-1-jiangyifei@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain X-Originating-IP: [10.174.187.31] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Signed-off-by: Yifei Jiang Signed-off-by: Mingwang Li --- 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