From patchwork Sat Jan 28 08:28:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anup Patel X-Patchwork-Id: 49820 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1247409wrn; Sat, 28 Jan 2023 00:32:08 -0800 (PST) X-Google-Smtp-Source: AMrXdXuO50Hh50FYFc+asJxQGZe+KngXXrVB64vS80HbFfOZ4neFD4IC4mp4VnL5LMAL35IzAFkT X-Received: by 2002:a17:90a:5b0c:b0:229:2458:4b2d with SMTP id o12-20020a17090a5b0c00b0022924584b2dmr44965197pji.12.1674894728179; Sat, 28 Jan 2023 00:32:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674894728; cv=none; d=google.com; s=arc-20160816; b=gdMm1NVkeOGSEsmdxyLA9ASKyVBmTS1YE9KaSAR/+PDP2V75LnZJSzd3vEfkAB9oDa f1NbMiEN+O/Pv5BkbQ6KR4XKQkeP0njeSt2OYtiBoy3FJmH/rBs5+pR60NBxOqDeke9e kD7UIYp47MYbrMeBr0g9zXUq+4fe8QeSzCtPu+z+rWpEKp6r1cyyFT8I1JLS6VXntWGb Gu3H1D6wBJMqQ3dofeWtRokU34+ZPdBg4sWFvTtLvn+ggyzNqCnL7omnC0x9l9T7i2NT zCRZ8r2N9x7mas1ZR5yrRt7TzT5RsqlpB1hQ5bMVY8jId324S7vG7wzS0zRrlsUPVtX8 LFmw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=vEMGTnRapKJbFGDqzXqULLCIpjEAFBsGqGounWes3mM=; b=bajQTPr96VxDc1LlyUbeRCfdVnmlJbJ6zp+3GJvzVlxsgrSjYGafiPj7dEg5o0J1qB uSCgXW50yHnSJy7E8nASmgCcRPJ1zZryktSjtuIEOr7EhUfKrbDgWJFgBldyM53qs44X lTcIdI2hdp6Y25NSma4KSFBfpf4ZJHnz1+FwqOO+TZ76b7aiHkGudekC8tSWcNURATmi gyp14Xw7tBt8DYj5ZNrjISeHX3HU9zJZSgeFnN18OTZVRxa8eNCWgqL2L7o2XsZdTMmR EfUcHSRGrZCs7ylOi3kOKshdjXQY1wxycyruYVXzjcnc9Cr8TyD3RTs4qhUuFW/jej44 vhmQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ventanamicro.com header.s=google header.b=CiUkIqEV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w1-20020a63b741000000b004e0d688fb83si3466428pgt.824.2023.01.28.00.31.54; Sat, 28 Jan 2023 00:32:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@ventanamicro.com header.s=google header.b=CiUkIqEV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233831AbjA1I3B (ORCPT + 99 others); Sat, 28 Jan 2023 03:29:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57538 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230175AbjA1I3A (ORCPT ); Sat, 28 Jan 2023 03:29:00 -0500 Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 136041A95D for ; Sat, 28 Jan 2023 00:28:58 -0800 (PST) Received: by mail-pj1-x1031.google.com with SMTP id e8-20020a17090a9a8800b0022c387f0f93so5801366pjp.3 for ; Sat, 28 Jan 2023 00:28:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=vEMGTnRapKJbFGDqzXqULLCIpjEAFBsGqGounWes3mM=; b=CiUkIqEVNKU4zMoYEnIth2esN6DjwehosPZnvfdBNZaBVx+QIsqSnb3XvpVSzMgsdA 7c72VuAIcEoOuRPCWuZ8j7yx+vYMh7cSh5FrQ2AbmPttlPlVBV2vedTUlTvi0RkA9j7Q 046kTqEXm82CcyvN6KGlyjfQ7uWP8JzCIk57lJnYQ0Ac3BwUxo2xChEt9biru1mBy+Wx R4Otzlvp3tFec2XLKM6Rt2JfqIgBcBjKwAltFg60PoB2xeKyGxn5dvZFu79dcIlz68lR mufhx/r1w29hnGKk0bzPQn02jwh3RO794lXFoSOU8IkkOLHqexgFW8bn4TrYlykkmfII KvKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=vEMGTnRapKJbFGDqzXqULLCIpjEAFBsGqGounWes3mM=; b=1nItOIAPRF2fqTRDA47j7fIlbY1ko7t8sapJW6eLWl1pJyglVnxPtJogI1VrOUEDxs qcy/QoY5+SrBHIcs5M3Bnxoa5CvraatDJe/eDe82vBb7qDQ5UW6Orfd/D2MflK6+O3WE U9PYaQs5faPXkMbfa9E0CxZp1cfE2x9m74m3EX9/gqkfYQAy2eQcCH0D4JA+wxcaeMAi cYf0EPrXjp0fQXDeVdVjMWpEKVBBDrgXfazZ4RBRrpEW+OpW/SMrNCp8r5vGO8B0P0O9 CWo7wZYDXMa1FVO20ET6UZBPVvllC/7PVkpQGXGw9jHvXb8Eu2NpeH08HwnnwXP1D6Qb sWeQ== X-Gm-Message-State: AFqh2kpybisu+56mt5uDXbYd8mpHZOYmLuDCWDd5xV4FPtS35BXH3fRE m5sjTaeTz/fZGX635DjQEDNlgA== X-Received: by 2002:a05:6a21:339a:b0:b6:4270:6dfc with SMTP id yy26-20020a056a21339a00b000b642706dfcmr56156319pzb.6.1674894537513; Sat, 28 Jan 2023 00:28:57 -0800 (PST) Received: from anup-ubuntu-vm.localdomain ([103.97.165.210]) by smtp.gmail.com with ESMTPSA id i67-20020a639d46000000b004cc95c9bd97sm3477807pgd.35.2023.01.28.00.28.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Jan 2023 00:28:57 -0800 (PST) From: Anup Patel To: Paolo Bonzini , Atish Patra Cc: Palmer Dabbelt , Paul Walmsley , Andrew Jones , Anup Patel , kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Anup Patel Subject: [PATCH v3 1/2] RISC-V: KVM: Fix privilege mode setting in kvm_riscv_vcpu_trap_redirect() Date: Sat, 28 Jan 2023 13:58:46 +0530 Message-Id: <20230128082847.3055316-1-apatel@ventanamicro.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756254414239211318?= X-GMAIL-MSGID: =?utf-8?q?1756254414239211318?= The kvm_riscv_vcpu_trap_redirect() should set guest privilege mode to supervisor mode because guest traps/interrupts are always handled in virtual supervisor mode. Fixes: 9f7013265112 ("RISC-V: KVM: Handle MMIO exits for VCPU") Signed-off-by: Anup Patel Reviewed-by: Andrew Jones Reviewed-by: Atish Patra --- arch/riscv/kvm/vcpu_exit.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/arch/riscv/kvm/vcpu_exit.c b/arch/riscv/kvm/vcpu_exit.c index c9f741ab26f5..af7c4bc07929 100644 --- a/arch/riscv/kvm/vcpu_exit.c +++ b/arch/riscv/kvm/vcpu_exit.c @@ -160,6 +160,9 @@ void kvm_riscv_vcpu_trap_redirect(struct kvm_vcpu *vcpu, /* Set Guest PC to Guest exception vector */ vcpu->arch.guest_context.sepc = csr_read(CSR_VSTVEC); + + /* Set Guest privilege mode to supervisor */ + vcpu->arch.guest_context.sstatus |= SR_SPP; } /* From patchwork Sat Jan 28 08:28:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anup Patel X-Patchwork-Id: 49821 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1247517wrn; Sat, 28 Jan 2023 00:32:28 -0800 (PST) X-Google-Smtp-Source: AMrXdXsRnZ+NYIkuMxCQDt5iSQ1MjXvYtV66DFOe2HlWqsiLlOvW6InslPO4ycG+suzNX080G8nX X-Received: by 2002:a05:6a20:9399:b0:9d:efbf:7864 with SMTP id x25-20020a056a20939900b0009defbf7864mr51448899pzh.25.1674894747881; Sat, 28 Jan 2023 00:32:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674894747; cv=none; d=google.com; s=arc-20160816; b=OcUB+F/EtfokVk5BG9k20zWbzUeSYQ5sKHUjyHH8ebbEpCrE7RwrgkJUVUNC+GWjPP NymtbNdLFP/ON6770hzm+qYFu7xYWa0c46gy5H+YYrRnxsYGo3GrLTSFuwcd71Ahcip2 KTF6VM/1Y+oeXnwrBEp6uQzTSgrl7MY9C4iK2tYa6xKU3nq67OEvXyPlGDJTpWARvh7u +ohFTAkDzo/jB26WWNMWYx8JqmcA8FUp/S/iGXT1oOEUUyVD2gUH4Pvrtc79OO1Ivkxt naahLMIKkcaM2mQDU2Uxj7Q0fS8SWRIGLJxE9RT1p/5OJqcQTwYCtTVWmNPTZ8AyHDH+ 848g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=FljnRzw2zIs2SaTw4PAfD3E9tIV3gHkmBf8hApNO6fw=; b=ecD7TwzohoDrsxfxOyeG6YU3lLJFueWtnO53Vu6PIOH3jCHoq8aJqdgFhpHdi4bsgm wmgBMzY+APu7yaPJZx2GvirjzVVsR7bKCtsuKeQo2qlXkIIbZmhFKp5LLashXlXKpf8P qi/TYSIicIHjj/xCGGEeaddXAjeZ+V2pLanZKgXs24eHyfg0zJMjsncn4ZqBF0pbpcKI UuhVZpxCnkgLzvk3UUk25MElMNH8iWZUV48zIevG8xy8rOgy/x8n8BuqOdjixQVOB/Us CoQAuqrhiFxko+fDeFyDDSvyy0eKf2kDk9kVVuKCmnctB/sF/CRbPrxMVQ6N85ZdNCdl PZnQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ventanamicro.com header.s=google header.b=OQizLREN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bs5-20020a632805000000b004da9e2633a5si6443909pgb.502.2023.01.28.00.32.16; Sat, 28 Jan 2023 00:32:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@ventanamicro.com header.s=google header.b=OQizLREN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233938AbjA1I3I (ORCPT + 99 others); Sat, 28 Jan 2023 03:29:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57578 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233796AbjA1I3E (ORCPT ); Sat, 28 Jan 2023 03:29:04 -0500 Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 158A77D6E4 for ; Sat, 28 Jan 2023 00:29:03 -0800 (PST) Received: by mail-pj1-x1033.google.com with SMTP id lp10so6760313pjb.4 for ; Sat, 28 Jan 2023 00:29:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=FljnRzw2zIs2SaTw4PAfD3E9tIV3gHkmBf8hApNO6fw=; b=OQizLRENPAg09ciAbZoN5itK16Pz2bFe45sopfZppnH0Ud5XsJVtEotBWwPAg2/UWT P/udJB7pRNtPQqdy3cdj0M+2GNCv5rS5XLbQrM11WQ2U1/Hb2+U99G2rsJHywjGkF8hp lNNYYta2mdtUotMek4oyyiqqICB9JexQFicaRCgMT4X1r1cLj5EiEnIh/b65TLt76ui4 f3n9nqSwQ0kz0qHhn542mTas5YO2S+dA6/LraWs+hCe/8PQANnV7VWEtktCRUVU9EJTI v4xZ/s0AevZHVyQ8Y4CcEBtoMRTEWOtEDLTPfTddIzBCl9CbwsMO74mP/0c2bEb2L32j 1OPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=FljnRzw2zIs2SaTw4PAfD3E9tIV3gHkmBf8hApNO6fw=; b=LdrX2yH6oNuRGZJR7qdQe0knzdx/d+BKn9hgb4hGt8SGDJUesktKNcei/EmOUX9i3n CAZ5B8TQkC7FQ9iGR3dl5NDXlZpNblN/F8mNqArCGQDKZEzfcFJNoeMoB2BvtAUznhcq FNc0XQwGn7aNmYUB+HAFkzmepAPHwixafglkd30gzFQKGGpyOBqrQCJnmPf7fXcLn3Zk PPls542b5oFS9OwDtly3FA+E3jjgQ5H1ydUzqKGFyJAjgtTqf5DJQBvMv5Z44NWsEZyY brzADKo5gfl8PiBK568dHJNIpcNK7KsL+nb8ucOodPZiWZfJQVMEEs/YrTsApR9W2JM4 ucDA== X-Gm-Message-State: AFqh2konALyz39z640hqxVPu+f3ebHszzJbuFLj8P++CE+FcJwTHlTs+ 2RPQuloNMup80HGJ2AsgGIFu8y1mumKCzcr5 X-Received: by 2002:a05:6a20:4c16:b0:b8:6371:9abf with SMTP id fm22-20020a056a204c1600b000b863719abfmr41015379pzb.27.1674894542483; Sat, 28 Jan 2023 00:29:02 -0800 (PST) Received: from anup-ubuntu-vm.localdomain ([103.97.165.210]) by smtp.gmail.com with ESMTPSA id i67-20020a639d46000000b004cc95c9bd97sm3477807pgd.35.2023.01.28.00.28.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Jan 2023 00:29:01 -0800 (PST) From: Anup Patel To: Paolo Bonzini , Atish Patra Cc: Palmer Dabbelt , Paul Walmsley , Andrew Jones , Anup Patel , kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Andy Chiu , Anup Patel Subject: [PATCH v3 2/2] RISC-V: KVM: Redirect illegal instruction traps to guest Date: Sat, 28 Jan 2023 13:58:47 +0530 Message-Id: <20230128082847.3055316-2-apatel@ventanamicro.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230128082847.3055316-1-apatel@ventanamicro.com> References: <20230128082847.3055316-1-apatel@ventanamicro.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756254435325890920?= X-GMAIL-MSGID: =?utf-8?q?1756254435325890920?= From: Andy Chiu The M-mode redirects an unhandled illegal instruction trap back to S-mode. However, KVM running in HS-mode terminates the VS-mode software when it receives illegal instruction trap. Instead, KVM should redirect the illegal instruction trap back to VS-mode, and let VS-mode trap handler decide the next step. This futher allows guest kernel to implement on-demand enabling of vector extension for a guest user space process upon first-use. Signed-off-by: Andy Chiu Signed-off-by: Anup Patel Reviewed-by: Andrew Jones Reviewed-by: Atish Patra --- arch/riscv/kvm/vcpu_exit.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/arch/riscv/kvm/vcpu_exit.c b/arch/riscv/kvm/vcpu_exit.c index af7c4bc07929..4ea101a73d8b 100644 --- a/arch/riscv/kvm/vcpu_exit.c +++ b/arch/riscv/kvm/vcpu_exit.c @@ -182,6 +182,12 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run, ret = -EFAULT; run->exit_reason = KVM_EXIT_UNKNOWN; switch (trap->scause) { + case EXC_INST_ILLEGAL: + if (vcpu->arch.guest_context.hstatus & HSTATUS_SPV) { + kvm_riscv_vcpu_trap_redirect(vcpu, trap); + ret = 1; + } + break; case EXC_VIRTUAL_INST_FAULT: if (vcpu->arch.guest_context.hstatus & HSTATUS_SPV) ret = kvm_riscv_vcpu_virtual_insn(vcpu, run, trap);