Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp1231515ybl; Thu, 22 Aug 2019 11:18:25 -0700 (PDT) X-Google-Smtp-Source: APXvYqyQSZhv2sHVgztZsVScUat1PhfGMtP0X4SYDZkKqjXn86mt4s3bZkjmg2X89JTDdenJmZnF X-Received: by 2002:a17:902:b193:: with SMTP id s19mr170938plr.16.1566497905059; Thu, 22 Aug 2019 11:18:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566497905; cv=none; d=google.com; s=arc-20160816; b=ZRqGjm6k7LMpjDyB3fJTDnEYwdJsXgyYHONJY18BX21BTflxvp1sD4W/bJlaDIjF/s /PbXKQO5rK2kIYXA0/ofCeg3iZt2yHqt47bO3Vm71zSQWqMVjMIpCkOqQnvLrGgRp7P1 9VUQrTaWCd7zHFiHwXoFd4Lr0+XRfqJos1kVAl/1Fhb3pZq5xmTd0WtkPVN5VIXEQ1Dm 68Ae/6H0EXTfd4K/PwySk/OZMkHTB/LuReWjGFvaE4NfalFojVIVfJYiE3suGeaBwO+P 6BzcBKgBe+i2DktTBE1u6lj8KHzE45v1hS6W/xqnxG+/HbGD5jLKyXyW0NfMJyShUaCO +70w== 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=ROB4gx4OGB1CpDcKjvU71ET9FrwtUSFzAVqfUnfvjig=; b=HanseyXguoUlU6SF4LkbFFhN3DZtJ7VB+DSANavvPpjxc8oXUjWafJvjAsN4U7yBYj g3mUstzsltsbqPec2RktWGn3b98ThYO28G34RNyBuDmYFE9vpJLZwf31+Ev6QEFPJtxk sqPAXelCgZ7mf2lYO/okLmaURUIyPFC0iEBT3rdjr76bc0x8B/Kj82rHsByM1oJ9HVRn khpXCEbFt1b2d7udI8ZOB5BS4/o5khF3uMWZmN7ueJre4n8x7zllE5JhwDDdLipRQuC4 fpZOx8WpV9Qnr7389JXh8ubWLnELhyMsQVycIXFXZtwXaaJLCtFd0wthB1aXGfXPGtJS zvjg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@brainfault-org.20150623.gappssmtp.com header.s=20150623 header.b=Tjz6zp1l; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i8si326274pjx.0.2019.08.22.11.18.06; Thu, 22 Aug 2019 11:18:25 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@brainfault-org.20150623.gappssmtp.com header.s=20150623 header.b=Tjz6zp1l; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388095AbfHVMuQ (ORCPT + 99 others); Thu, 22 Aug 2019 08:50:16 -0400 Received: from mail-wr1-f68.google.com ([209.85.221.68]:46618 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731604AbfHVMuQ (ORCPT ); Thu, 22 Aug 2019 08:50:16 -0400 Received: by mail-wr1-f68.google.com with SMTP id z1so5284817wru.13 for ; Thu, 22 Aug 2019 05:50:13 -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=ROB4gx4OGB1CpDcKjvU71ET9FrwtUSFzAVqfUnfvjig=; b=Tjz6zp1luUYbfqgLHs8u1XU3r21rRiuHFjEEE445sTZjPTG1K1TbEO655UT3JDH3uL sC9vFssy7r4wfHsKKF/jvniljfpEfBFwOrQchEZDTPX3FFboKQyRluqtt46Kzkw0x8XG yKutR8H6zkflJqqRKMFoQNCPwDuLl5c3EUDnFRNxl1fIpcQ6/JjEJYLu2fsFzhRNkdkC T6RMZHoZAvxRZ25l7eXoVO73Em9ogqkTFCN2UVljQt/8eRLrTATgzUwrfpw6Zd6lqKRY g9WxpB32AOgs0olEMDJsyZyRztxQnxlRzFtFxz+8PJQyQWvuhTE7hN6yS2Wz9XivJaJq D+sQ== 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=ROB4gx4OGB1CpDcKjvU71ET9FrwtUSFzAVqfUnfvjig=; b=Q7D1p0L9QS8mRtFh0QNbYoMflsb4xDGf/aUzRtgO/uNrRP9Vwyf/kITtLrNI9DEwFo uJxhfEKNz1az5MKubJylaU9ZBpRTpb1vI2jsWw361XiDu5Bdo55vm+wKW/SMR+UBHhAg oGogE1u1bWMKEfeN8SFdAHNKsczxBrCgpvt63ImEb7wkvy38wouRrPAKdG6dJcZbaP5X qC8rEz0uOfLrEH4fRlaEuWkNVtDJX7Xg/A7kRck9cJR0Ywsihboas5PskkDIeqJ72A9y NarJQ0BWmlGkFvSBcRwpNfq5bG7CXzrMs6f59dgSlyhDwC8YxFCunuJ6T4sylE6HN5SV sAcA== X-Gm-Message-State: APjAAAUq42Y7ZizPmSqPoqlgoU0m+ariPpZxLK9USQc6Vod/UZbmX9Kc uIquTpd9jQVVpC9CMAOgxy8V4suev3iCASTPu5fzSQ== X-Received: by 2002:a05:6000:104c:: with SMTP id c12mr42632777wrx.328.1566478213090; Thu, 22 Aug 2019 05:50:13 -0700 (PDT) MIME-Version: 1.0 References: <20190822084131.114764-1-anup.patel@wdc.com> <20190822084131.114764-12-anup.patel@wdc.com> <29b8f7c6-4b9d-91fc-61e7-82ecfd26ff88@amazon.com> In-Reply-To: <29b8f7c6-4b9d-91fc-61e7-82ecfd26ff88@amazon.com> From: Anup Patel Date: Thu, 22 Aug 2019 18:20:01 +0530 Message-ID: Subject: Re: [PATCH v5 11/20] RISC-V: KVM: Handle WFI exits for VCPU To: Alexander Graf Cc: Anup Patel , Palmer Dabbelt , Paul Walmsley , Paolo Bonzini , Radim K , Daniel Lezcano , Thomas Gleixner , Atish Patra , Alistair Francis , Damien Le Moal , Christoph Hellwig , "kvm@vger.kernel.org" , "linux-riscv@lists.infradead.org" , "linux-kernel@vger.kernel.org" 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 Thu, Aug 22, 2019 at 5:49 PM Alexander Graf wrote: > > On 22.08.19 10:45, Anup Patel wrote: > > We get illegal instruction trap whenever Guest/VM executes WFI > > instruction. > > > > This patch handles WFI trap by blocking the trapped VCPU using > > kvm_vcpu_block() API. The blocked VCPU will be automatically > > resumed whenever a VCPU interrupt is injected from user-space > > or from in-kernel IRQCHIP emulation. > > > > Signed-off-by: Anup Patel > > Acked-by: Paolo Bonzini > > Reviewed-by: Paolo Bonzini > > --- > > arch/riscv/kvm/vcpu_exit.c | 88 ++++++++++++++++++++++++++++++++++++++ > > 1 file changed, 88 insertions(+) > > > > diff --git a/arch/riscv/kvm/vcpu_exit.c b/arch/riscv/kvm/vcpu_exit.c > > index efc06198c259..fbc04fe335ad 100644 > > --- a/arch/riscv/kvm/vcpu_exit.c > > +++ b/arch/riscv/kvm/vcpu_exit.c > > @@ -12,6 +12,9 @@ > > #include > > #include > > > > +#define INSN_MASK_WFI 0xffffff00 > > +#define INSN_MATCH_WFI 0x10500000 > > + > > #define INSN_MATCH_LB 0x3 > > #define INSN_MASK_LB 0x707f > > #define INSN_MATCH_LH 0x1003 > > @@ -179,6 +182,87 @@ static ulong get_insn(struct kvm_vcpu *vcpu) > > return val; > > } > > > > +typedef int (*illegal_insn_func)(struct kvm_vcpu *vcpu, > > + struct kvm_run *run, > > + ulong insn); > > + > > +static int truly_illegal_insn(struct kvm_vcpu *vcpu, > > + struct kvm_run *run, > > + ulong insn) > > +{ > > + /* TODO: Redirect trap to Guest VCPU */ > > + return -ENOTSUPP; > > +} > > + > > +static int system_opcode_insn(struct kvm_vcpu *vcpu, > > + struct kvm_run *run, > > + ulong insn) > > +{ > > + if ((insn & INSN_MASK_WFI) == INSN_MATCH_WFI) { > > + vcpu->stat.wfi_exit_stat++; > > + if (!kvm_arch_vcpu_runnable(vcpu)) { > > + srcu_read_unlock(&vcpu->kvm->srcu, vcpu->arch.srcu_idx); > > + kvm_vcpu_block(vcpu); > > + vcpu->arch.srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); > > + kvm_clear_request(KVM_REQ_UNHALT, vcpu); > > + } > > + vcpu->arch.guest_context.sepc += INSN_LEN(insn); > > + return 1; > > + } > > + > > + return truly_illegal_insn(vcpu, run, insn); > > +} > > + > > +static illegal_insn_func illegal_insn_table[32] = { > > Every time I did experiments on PowerPC with indirect tables like this > over switch() in C, the switch() code won. CPUs are pretty good at > predicting branches. Predicting indirect jumps however, they are > terrible at. > > So unless you consider the jump table more readable / maintainable, I > would suggest to use a simple switch() statement. It will be faster and > smaller. Yes, readability was the reason why we choose jump table but I see your point. Most of the entries in jump table point to truly_illegal_insn() so I guess switch case will be quite simple here. I will update this in next revision. Regards, Anup > > > Alex > > > > + truly_illegal_insn, /* 0 */ > > + truly_illegal_insn, /* 1 */ > > + truly_illegal_insn, /* 2 */ > > + truly_illegal_insn, /* 3 */ > > + truly_illegal_insn, /* 4 */ > > + truly_illegal_insn, /* 5 */ > > + truly_illegal_insn, /* 6 */ > > + truly_illegal_insn, /* 7 */ > > + truly_illegal_insn, /* 8 */ > > + truly_illegal_insn, /* 9 */ > > + truly_illegal_insn, /* 10 */ > > + truly_illegal_insn, /* 11 */ > > + truly_illegal_insn, /* 12 */ > > + truly_illegal_insn, /* 13 */ > > + truly_illegal_insn, /* 14 */ > > + truly_illegal_insn, /* 15 */ > > + truly_illegal_insn, /* 16 */ > > + truly_illegal_insn, /* 17 */ > > + truly_illegal_insn, /* 18 */ > > + truly_illegal_insn, /* 19 */ > > + truly_illegal_insn, /* 20 */ > > + truly_illegal_insn, /* 21 */ > > + truly_illegal_insn, /* 22 */ > > + truly_illegal_insn, /* 23 */ > > + truly_illegal_insn, /* 24 */ > > + truly_illegal_insn, /* 25 */ > > + truly_illegal_insn, /* 26 */ > > + truly_illegal_insn, /* 27 */ > > + system_opcode_insn, /* 28 */ > > + truly_illegal_insn, /* 29 */ > > + truly_illegal_insn, /* 30 */ > > + truly_illegal_insn /* 31 */ > > +}; > > + > > +static int illegal_inst_fault(struct kvm_vcpu *vcpu, struct kvm_run *run, > > + unsigned long stval) > > +{ > > + ulong insn = stval; > > + > > + if (unlikely((insn & 3) != 3)) { > > + if (insn == 0) > > + insn = get_insn(vcpu); > > + if ((insn & 3) != 3) > > + return truly_illegal_insn(vcpu, run, insn); > > + } > > + > > + return illegal_insn_table[(insn & 0x7c) >> 2](vcpu, run, insn); > > +} > > + > > static int emulate_load(struct kvm_vcpu *vcpu, struct kvm_run *run, > > unsigned long fault_addr) > > { > > @@ -439,6 +523,10 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run, > > ret = -EFAULT; > > run->exit_reason = KVM_EXIT_UNKNOWN; > > switch (scause) { > > + case EXC_INST_ILLEGAL: > > + if (vcpu->arch.guest_context.hstatus & HSTATUS_SPV) > > + ret = illegal_inst_fault(vcpu, run, stval); > > + break; > > case EXC_INST_PAGE_FAULT: > > case EXC_LOAD_PAGE_FAULT: > > case EXC_STORE_PAGE_FAULT: > > >