Received: by 2002:a25:824b:0:0:0:0:0 with SMTP id d11csp704538ybn; Wed, 25 Sep 2019 06:32:24 -0700 (PDT) X-Google-Smtp-Source: APXvYqzvV6TbPHmOTMCsn/h3o0ok1QreyXg2HxfYhlpCJQ5CJJC8EclBc/+WkdVhxNVBYXmrT1E7 X-Received: by 2002:a50:fa83:: with SMTP id w3mr2870573edr.262.1569418344835; Wed, 25 Sep 2019 06:32:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1569418344; cv=none; d=google.com; s=arc-20160816; b=C90v0D0olQRvFYFlCDUGVIZI3lsmfFLbhN9dS4UsWnWkrDeFk9LPMTnnMfDDEEmu1M B2dqVuJr2vjzOblQg4cBsQ7hdNLEtht53/wH3Fo3pCxBsPSYKKu8Iawcqmlme0zXaaeX 3hONMJ67YExY6vVaFaaOQR/g/Z82K5nTEMsLyN2CFT5CiWMIZpXhMukaxJTDZtYpTxZ9 mG+DUt25gdGwTWKkXTETkzZkWFuTx3mDha4aHgPB6bYEmg9yGIS0r+i8Bc65cpdzehY6 yupvF1/CvCdqj34v12Df/Mka16ge+XPxy4FyClv72ghMG/M7BEzSGBnKfIAUaZqU7Bya vypQ== 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=AovsfvrkVC8KgehdY4+Z592P7dBIQY37LqoSTZ8Zeho=; b=M3vAvk1TV8+Ntr2/PtFf9/1fv8zJgieHMdh21R7RkaaAL92tbgUgC6r+UcRgDlMkkB sBPld4TVcUVOePFqOJELQfOQopJDfgb6br9aqcLm+e5D84ie1QjCGe/K3YP6OPiEzO8T M9nsv5LI4/cqk+aM1fqAPFqnNRddEt4K0Ez0MaUCJ4RsR5ExGy2/lkosgue2Yivif953 cTZcqlt4626eONlR5wZOuRozvscoyuvj83CyKvSKPyP//KnuN1S8swgb3V2CxcaZV0cJ 2Fg1BcT+1wAVIptMT11uh8pr0Rv3ob6QyrbIJ4CxxqOswTJBHTuZFTgL7EJsV/bFly+4 DxTg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@brainfault-org.20150623.gappssmtp.com header.s=20150623 header.b=HYeBM2Qp; 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 l12si3822899edk.444.2019.09.25.06.32.00; Wed, 25 Sep 2019 06:32:24 -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=HYeBM2Qp; 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 S2439703AbfIWMyR (ORCPT + 99 others); Mon, 23 Sep 2019 08:54:17 -0400 Received: from mail-wr1-f67.google.com ([209.85.221.67]:46286 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2439697AbfIWMyR (ORCPT ); Mon, 23 Sep 2019 08:54:17 -0400 Received: by mail-wr1-f67.google.com with SMTP id o18so13777042wrv.13 for ; Mon, 23 Sep 2019 05:54:16 -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=AovsfvrkVC8KgehdY4+Z592P7dBIQY37LqoSTZ8Zeho=; b=HYeBM2QpSXqbmldBl/VrqKVlCePEW918eFcjyJyHTWUNdbLCTZvsYKhdc8gAKYhMQ3 onS/RCKaAqam1IA/rnrzUrG+d7bZJr2jWsdDZkfh4si4j5R5w4HYXf0LAGhhfEQta0VH kOBETSMUfxF4BbeVChawSoHw5c/AqsVpt+ZYrtYpzU7nRnQBVA20F3pvTns/+ea10zfQ 3lBnqyOgUJHfGIr8j6LZs4YD/QrIBGZVH71lwS4eH1TSI15nbdNFNbGMZDcU0+pPHYDb fKiRaH3Q7y7oQ6BZr9K95rEDYWpXA3J7EJA9gsU8++gi2Mp8u0O7xXR4hJuREj8UxXok ifZg== 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=AovsfvrkVC8KgehdY4+Z592P7dBIQY37LqoSTZ8Zeho=; b=rfBvFtAcjlGMsFudFCjfKPMfdlJlOlCPP2vq2X6oXVOTlwhOrQduXH/lLqxvNUXzoF G5ZQty9mzwK6b9m4+R1CBx1EMn1P8z3K7awHeQpvUF1iXvMlqFS2WWgmg4IVDptKBMRM 3GCpLe48d1bro8Gh2sjDIds2rzflY7Dd/mCq4VfaDxg49DNvb7ZnTP0kAgC25AzCHJso FMvF4BHA8uDDFI9dtC/OSJYlh0y+e9AyF/dvaBu9DaAi9dZvCeDUXy4xrwW0OvJJ+dUa kww7Z5kd4PWLmwX/bRA/e7+g61NYICaTYaRzL+ZPdrjNJGKGkDA+YoPILYdWBtLyK2dW 0nMQ== X-Gm-Message-State: APjAAAUWnr/dyUtnEzGSHz6XzfxZbRo4c9TFvmj4CEOjmEPDESkgAoyk oTHJ+lzveq0MnmBXR/GhzVPEDRDDQcvBGRXUpPhgeNRJ X-Received: by 2002:a05:6000:2:: with SMTP id h2mr20587928wrx.309.1569243255021; Mon, 23 Sep 2019 05:54:15 -0700 (PDT) MIME-Version: 1.0 References: <20190904161245.111924-1-anup.patel@wdc.com> <20190904161245.111924-13-anup.patel@wdc.com> <3c149ec4-38df-9073-2880-b28148d3c059@amazon.com> In-Reply-To: <3c149ec4-38df-9073-2880-b28148d3c059@amazon.com> From: Anup Patel Date: Mon, 23 Sep 2019 18:24:02 +0530 Message-ID: Subject: Re: [PATCH v7 11/21] 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 Mon, Sep 23, 2019 at 12:24 PM Alexander Graf wrote: > > > > On 04.09.19 18:15, 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 | 72 ++++++++++++++++++++++++++++++++++++++ > > 1 file changed, 72 insertions(+) > > > > diff --git a/arch/riscv/kvm/vcpu_exit.c b/arch/riscv/kvm/vcpu_exit.c > > index d75a6c35b6c7..39469f67b241 100644 > > --- a/arch/riscv/kvm/vcpu_exit.c > > +++ b/arch/riscv/kvm/vcpu_exit.c > > @@ -12,6 +12,13 @@ > > #include > > #include > > > > +#define INSN_OPCODE_MASK 0x007c > > +#define INSN_OPCODE_SHIFT 2 > > +#define INSN_OPCODE_SYSTEM 28 > > + > > +#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 > > @@ -112,6 +119,67 @@ > > (s32)(((insn) >> 7) & 0x1f)) > > #define MASK_FUNCT3 0x7000 > > > > +static int truly_illegal_insn(struct kvm_vcpu *vcpu, > > + struct kvm_run *run, > > + ulong insn) > > +{ > > + /* Redirect trap to Guest VCPU */ > > + kvm_riscv_vcpu_trap_redirect(vcpu, EXC_INST_ILLEGAL, insn); > > + > > + return 1; > > +} > > + > > +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 int illegal_inst_fault(struct kvm_vcpu *vcpu, struct kvm_run *run, > > + unsigned long insn) > > +{ > > + unsigned long ut_scause = 0; > > + struct kvm_cpu_context *ct; > > + > > + if (unlikely((insn & 3) != 3)) { > > What do the low 2 bits mean here? Maybe you can use a define instead? These bits are for instruction length (16bit or 32bit). I will add appropriate defines for these bits. Regards, Anup > > > Alex > > > > > Amazon Development Center Germany GmbH > Krausenstr. 38 > 10117 Berlin > Geschaeftsfuehrung: Christian Schlaeger, Ralf Herbrich > Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B > Sitz: Berlin > Ust-ID: DE 289 237 879 > >