From patchwork Sun Oct 20 19:47:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anup Patel X-Patchwork-Id: 1999662 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=Xo/0tIpi; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=ventanamicro.com header.i=@ventanamicro.com header.a=rsa-sha256 header.s=google header.b=miDqsvxz; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=kvm-riscv-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XWpsK1YwFz1xwK for ; Mon, 21 Oct 2024 06:48:37 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=e7/mmyqzMcM+XsVJs7uemkPOMPQ4gZuONSjh9DHUv3w=; b=Xo/0tIpiBubUwg 3KNKSOSqdKq2GT19ZPDbYKuZA8gXPhM3+EA4EvD4sodkoi2i7tP/R/e4/dXhI7nxaJTrAiWDwcrYN HcHu3GRWW4Cs09o3SM5YDS05dGaojGsBS6j2ALXtiB2yuMp3iejNPEkv9iKisFSWMMeU6wDlUhq+g yZJNlOYCtYr0qHQTAH4dejmj75HplVFspmdSlvrvEvxsPvCXM3yy+lMM18gCHOqqN098lvQ82xOPr VXOY+fqfPVbWbEEDCJ2cSmdSvuaMNn+JuJOlsZ6Wau83VrNSkhYflOQR9szsmMO4LXlzXa5pPDlxF 0CKhPU5/6gsVuBgwpmSw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t2bul-00000005NI2-1kqK; Sun, 20 Oct 2024 19:48:35 +0000 Received: from mail-pj1-x1035.google.com ([2607:f8b0:4864:20::1035]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t2buJ-00000005MuX-3T1U for kvm-riscv@lists.infradead.org; Sun, 20 Oct 2024 19:48:11 +0000 Received: by mail-pj1-x1035.google.com with SMTP id 98e67ed59e1d1-2e2e88cb0bbso2724047a91.3 for ; Sun, 20 Oct 2024 12:48:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1729453687; x=1730058487; darn=lists.infradead.org; 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=Yr2xZkoHB0nVswj42oFFwW3NpuvwZ9N4MbvDwoyulOQ=; b=miDqsvxzE94SWGpC5ftuo+1hiPwrRyMVVg3W6JO6Zg4Wa35BxQnmg/NcZZBtUVgKAn HFT5/vsPpg623ZFAka6ZN9sufni+U9VVpoh6esp3UWm5KJVyVjrYwz7nHhlR4Vck+SkU 7677z1FoUUG/PLvc972G8t9KGIp/doc/WY2wl5GuCj1itcg3cWS5PbWVo942RVg/zm/4 ar/VR/mHgYIFVD12aMfoArQCL7JJSOuVLBkpnL4YeT8U8kvcO2MOfp0s+Ao7+ksJWVNG A2pX/hMIXZ6IcUd8rhsjCcATvBhQdhtNvp2GRhnBNc5W5e27N9t5mdu+R8VuQuUK9WBz YyFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729453687; x=1730058487; 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=Yr2xZkoHB0nVswj42oFFwW3NpuvwZ9N4MbvDwoyulOQ=; b=tjm60SFh+3d8YNSdYSfzMolrlt/7mc24ZjeEP6cZH+S4uxP8okH3ddRa16s4noG6kW H0+WJgFUihFyR3p6D/8FVk4GxCzr6f9ZQ3C3ik6gAGrGYaL7Q+46yH7Ib/kCFoh6czNW 7XtIpl264mrMaLq2ogsv+uGos5ov9dhysr8holjtFuh1tRrCb3+5XfsgLsJiq0UTlNb1 c8LhAh9tMaoU2jVfB7p1X3rEO3tTm13zQnZfo8NCHYJjVxntfSUG9NUw4V9nlc/IAdAA kVSmhBhHs1NxnPI9OcRRGCLkpIJW5sB3Y7/4GPcQ8GKqYd39/VwyW0p+q4rAtCLNy6x0 XCQQ== X-Forwarded-Encrypted: i=1; AJvYcCXqi3yJ2w72b3gR8Tjt9aS/1JSbyuGtDKtNFPt1Dvci49NFKn1ofmoJL+PTAeSHMeVlBb4S4Y7yAcQ=@lists.infradead.org X-Gm-Message-State: AOJu0Yzd+wiJMLMT9sAKk3VrkP7xSuzpTm0imQLq0+J7p23tCET+gKZD 37qaOXzGL9x58SLMxx5yPxga2hk+r216Dhx4yGAxGNPED5XHAmbTZuHhsPAfNGE= X-Google-Smtp-Source: AGHT+IGOwsMImEEUFZ7X9qPpWtzyIIzNSFMp0fMdMzDbnnBwV6JmeAnM7Csv5DyXHIZDDlryM2Wx1g== X-Received: by 2002:a17:90a:5147:b0:2e2:c6a6:84da with SMTP id 98e67ed59e1d1-2e5617269c6mr10776191a91.34.1729453686738; Sun, 20 Oct 2024 12:48:06 -0700 (PDT) Received: from anup-ubuntu-vm.localdomain ([50.238.223.131]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e5ad365d4dsm1933188a91.14.2024.10.20.12.48.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 20 Oct 2024 12:48:06 -0700 (PDT) From: Anup Patel To: Palmer Dabbelt , Paul Walmsley Cc: Atish Patra , 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 , Atish Patra Subject: [PATCH v2 12/13] RISC-V: KVM: Save trap CSRs in kvm_riscv_vcpu_enter_exit() Date: Mon, 21 Oct 2024 01:17:33 +0530 Message-ID: <20241020194734.58686-13-apatel@ventanamicro.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241020194734.58686-1-apatel@ventanamicro.com> References: <20241020194734.58686-1-apatel@ventanamicro.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241020_124808_275872_EC800E72 X-CRM114-Status: GOOD ( 11.87 ) X-Spam-Score: -2.1 (--) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: Save trap CSRs in the kvm_riscv_vcpu_enter_exit() function instead of the kvm_arch_vcpu_ioctl_run() function so that HTVAL and HTINST CSRs are accessed in more optimized manner while running under som [...] Content analysis details: (-2.1 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:1035 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] X-BeenThere: kvm-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "kvm-riscv" Errors-To: kvm-riscv-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org Save trap CSRs in the kvm_riscv_vcpu_enter_exit() function instead of the kvm_arch_vcpu_ioctl_run() function so that HTVAL and HTINST CSRs are accessed in more optimized manner while running under some other hypervisor. Signed-off-by: Anup Patel Reviewed-by: Atish Patra --- arch/riscv/kvm/vcpu.c | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index e191e6eae0c0..e048dcc6e65e 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -768,12 +768,21 @@ static __always_inline void kvm_riscv_vcpu_swap_in_host_state(struct kvm_vcpu *v * This must be noinstr as instrumentation may make use of RCU, and this is not * safe during the EQS. */ -static void noinstr kvm_riscv_vcpu_enter_exit(struct kvm_vcpu *vcpu) +static void noinstr kvm_riscv_vcpu_enter_exit(struct kvm_vcpu *vcpu, + struct kvm_cpu_trap *trap) { void *nsh; struct kvm_cpu_context *gcntx = &vcpu->arch.guest_context; struct kvm_cpu_context *hcntx = &vcpu->arch.host_context; + /* + * We save trap CSRs (such as SEPC, SCAUSE, STVAL, HTVAL, and + * HTINST) here because we do local_irq_enable() after this + * function in kvm_arch_vcpu_ioctl_run() which can result in + * an interrupt immediately after local_irq_enable() and can + * potentially change trap CSRs. + */ + kvm_riscv_vcpu_swap_in_guest_state(vcpu); guest_state_enter_irqoff(); @@ -816,14 +825,24 @@ static void noinstr kvm_riscv_vcpu_enter_exit(struct kvm_vcpu *vcpu) } else { gcntx->hstatus = csr_swap(CSR_HSTATUS, hcntx->hstatus); } + + trap->htval = nacl_csr_read(nsh, CSR_HTVAL); + trap->htinst = nacl_csr_read(nsh, CSR_HTINST); } else { hcntx->hstatus = csr_swap(CSR_HSTATUS, gcntx->hstatus); __kvm_riscv_switch_to(&vcpu->arch); gcntx->hstatus = csr_swap(CSR_HSTATUS, hcntx->hstatus); + + trap->htval = csr_read(CSR_HTVAL); + trap->htinst = csr_read(CSR_HTINST); } + trap->sepc = gcntx->sepc; + trap->scause = csr_read(CSR_SCAUSE); + trap->stval = csr_read(CSR_STVAL); + vcpu->arch.last_exit_cpu = vcpu->cpu; guest_state_exit_irqoff(); kvm_riscv_vcpu_swap_in_host_state(vcpu); @@ -940,22 +959,11 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) guest_timing_enter_irqoff(); - kvm_riscv_vcpu_enter_exit(vcpu); + kvm_riscv_vcpu_enter_exit(vcpu, &trap); vcpu->mode = OUTSIDE_GUEST_MODE; vcpu->stat.exits++; - /* - * Save SCAUSE, STVAL, HTVAL, and HTINST because we might - * get an interrupt between __kvm_riscv_switch_to() and - * local_irq_enable() which can potentially change CSRs. - */ - trap.sepc = vcpu->arch.guest_context.sepc; - trap.scause = csr_read(CSR_SCAUSE); - trap.stval = csr_read(CSR_STVAL); - trap.htval = ncsr_read(CSR_HTVAL); - trap.htinst = ncsr_read(CSR_HTINST); - /* Syncup interrupts state with HW */ kvm_riscv_vcpu_sync_interrupts(vcpu);