Received: by 2002:a25:ca44:0:0:0:0:0 with SMTP id a65csp327904ybg; Sun, 26 Jul 2020 05:58:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwbLlGp00WbEdv8Df8ikxSnmlMH2jVYjnPRifZSTzIZgUCHj7Evq6dZj4tcWGJEy5pSYJXt X-Received: by 2002:a17:906:7e57:: with SMTP id z23mr2281451ejr.294.1595768314503; Sun, 26 Jul 2020 05:58:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595768314; cv=none; d=google.com; s=arc-20160816; b=R/EfFrK/PnmYky5kyK6Biwl+AgjEXZcqeJuMWl1JSPlsZNkVtcdMlTDKRLymNKWkdq 120L8p6CKIaE8eGQkpb3bqBbL005hZtydip/ucw4JLCo2LTHzpi6gd48wGYdsJ25u+U9 qyiJWe/BidY43UZciwrCRSe5e2NUpA2DM0qGqezeaWLtm5SpCwyjnt67sQI0t5Mmr/fi 7XQ2cX3CwcMqKdYxXteYBnvRlsyIWd8POCN1P0L19O2Fiw7mg16PMBa7koECQ+Ddstpc Xmqws6lV2cDD8Lx8MqA/NRv3Mir7tVFcQKnVciDRzOnuMRaPrM8x8drQxVRcq093oORD mDmA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=cj1AA+QajmRenSwYh/8g5RjvZ+1Kfi4azWkbk5rVJMY=; b=hvXUQzZBk5wi4TZteLPLzAXHjikOshKSPw1MutVE+pf1XnRDkSclnnaD5o05IC8ntv uFvAZkGVwdg9VVeVfP3t3hs3L1euLKTBr+d9fAVSYLZ4tStb46BbJ3MHGqwyfBY26DiO DAuKfijbfRIWKs0ZdgXNu5GABrhuHUSYK89baCXyyaHmMThjW1akLO+eVS4sgmyKsW67 /veyYv4WDNb9lfMT5wO81sYcx8AY8QmT5Ky9UH3tskuCwA8paAYZkafOGvy/kwF1Uajd zcHE1wtbJKRjGL9DXPAM3hZMRyQaCpmGmXxnSJM5fLsOfsVK/P+enn7QcFtIQtbZNbKa GP2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@brainfault-org.20150623.gappssmtp.com header.s=20150623 header.b=l4ouuUm+; 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 d9si3738823edv.44.2020.07.26.05.58.10; Sun, 26 Jul 2020 05:58:34 -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; dkim=pass header.i=@brainfault-org.20150623.gappssmtp.com header.s=20150623 header.b=l4ouuUm+; 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 S1727071AbgGZM5a (ORCPT + 99 others); Sun, 26 Jul 2020 08:57:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726704AbgGZM5a (ORCPT ); Sun, 26 Jul 2020 08:57:30 -0400 Received: from mail-wm1-x344.google.com (mail-wm1-x344.google.com [IPv6:2a00:1450:4864:20::344]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DD645C0619D2 for ; Sun, 26 Jul 2020 05:57:29 -0700 (PDT) Received: by mail-wm1-x344.google.com with SMTP id t142so5779673wmt.4 for ; Sun, 26 Jul 2020 05:57:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brainfault-org.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=cj1AA+QajmRenSwYh/8g5RjvZ+1Kfi4azWkbk5rVJMY=; b=l4ouuUm+RZKjkDPNsYC7yAU3y2YJaYqK7fRoN7KnpfYrRUJXIdaNeqJraLDSLj+Hsq DoCjcYHNOPx4wQ8IoFKBY8oXq+gP6wlJ5S/+4ZJRUZe7F+op3/uY4m7z1/B2VnkOgD5m t2uIGFEk+6v+lJhjcexw3MSjbYwv9XzgPyuSk2nG7+0gRoCIhPcpMUQxBQ0CXJeMW7Bq Pj3YcVraJh5zWRzVM0CLCI1eEwZtk2hlU89S/EKMlfBzmwOSdLrkBcQe9JB7QgVZ03Q+ zeks5A6Odx9k1ILsGYWcElYbzhHbp57Sf/ChsiYlNzHYRcSM26Z/AA8OZ33WJhBEDIgd p68A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=cj1AA+QajmRenSwYh/8g5RjvZ+1Kfi4azWkbk5rVJMY=; b=LpqwlB1ty8EWHY8BIX4RxdLDfhmXeWPr8R3axdftd0wIBlP1HEKadVdz5iA8mTIQNg JR79f6BoJFmOMtjDGzfIaj7Drv6PBHufrHfs556wkTCKFRMVleuuMJAClTHGbe+HJKHQ 588sA9Jcc2OW8pETMwjZy8/oHMYoRP/WQqVp9pUtIl4EBnomRC7EAhh41OijXesL0uVe ZVtwYOLzyBUtgxiJv+Lc4WbjLeuZcbc38avUlNgUtVrfZGWksOfJEw9q0mJ8VqcInsiZ Gl9a/DFddsX7OTYMt4mQjZ0+gKT+FV2qmLWs7GiXU44KhSk01hWvVO5pZodD6Qarb0CA eAJg== X-Gm-Message-State: AOAM530oqgKfQLEYrdhsXWut9U92l2uF+ukb5VenCrwhHeYyuW3ibI4W w2fQ1BhJtvOT0dAq6BKrfAsA9mk7SGQ0QLoyuJ7Szg== X-Received: by 2002:a1c:5f41:: with SMTP id t62mr17178624wmb.134.1595768248343; Sun, 26 Jul 2020 05:57:28 -0700 (PDT) MIME-Version: 1.0 References: <20200724085441.1514-1-jiangyifei@huawei.com> <20200724085441.1514-3-jiangyifei@huawei.com> In-Reply-To: <20200724085441.1514-3-jiangyifei@huawei.com> From: Anup Patel Date: Sun, 26 Jul 2020 18:27:16 +0530 Message-ID: Subject: Re: [RFC 2/2] RISC-V: KVM: read\write kernel mmio device support To: Yifei Jiang Cc: Paolo Bonzini , Paul Walmsley , Palmer Dabbelt , Albert Ou , Anup Patel , Atish Patra , KVM General , kvm-riscv@lists.infradead.org, linux-riscv , "linux-kernel@vger.kernel.org List" , "Zhangxiaofeng (F)" , wu.wubin@huawei.com, Zhanghailiang , "dengkai (A)" , limingwang@huawei.com Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jul 24, 2020 at 2:25 PM Yifei Jiang wrote: > > 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); > + Move "ret = kvm_io_bus_read()" just before "if (!ret)". > 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); > + Same as above. Move "ret = kvm_io_bus_write()" just before "if (!ret) {" > 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 > > Regards, Anup