From patchwork Sat Jan 11 00:46:58 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 2033063 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=kQ9gLnwU; dkim=fail reason="signature verification failed" (2048-bit key; secure) header.d=infradead.org header.i=@infradead.org header.a=rsa-sha256 header.s=desiato.20200630 header.b=dhUYzGpJ; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=sifive.com header.i=@sifive.com header.a=rsa-sha256 header.s=google header.b=h3hUsaI+; 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 4YVM3g6VdNz1yPc for ; Sat, 11 Jan 2025 12:52:45 +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=WsUQ6IbZyyeWr2HFgY4WWX2AR9YFUXdwXQXUcH2gZDM=; b=kQ9gLnwU/D8Kfg 4sBfeZPH8nsH0X8mUza/sUqBPZl+0TC31okLVlfgTlJpPqT5HKFq+apRYJqRiA2EZHR/ijQe2sVuC xmVB//wcDoDWzmCvEmfiicL+TvGhWcihMU1J/jgAkRXDcbfR91i7JaK+tsXDUNk5q1wW5FUTLVxEe UKIPyE2Qw3HqCy6lZ0iZu2moWJh8yu/ii/99I1pGjR8theVezPZ5AnArNkGHvGcq4dohJT+eNFJB6 UzBIW5kRdP2Hd65SxXIUMEzzVdvjBpLJ5cy0FhD56ABWSmXg8IGclX2Ed92vKdZVKZV0ytWxqgw4+ O5xftSKPEZ105itXMYug==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tWQg9-0000000HX5G-07zd; Sat, 11 Jan 2025 01:52:45 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tWPei-0000000HPvA-0c1U for kvm-riscv@bombadil.infradead.org; Sat, 11 Jan 2025 00:47:12 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Transfer-Encoding:MIME-Version :References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=mG/7mpvt2lUZ0JpSGT/sSZlaG6n67EyEdFyeasyHfcQ=; b=dhUYzGpJLw/2hQhOoqisF1zGML lSPmiTBVPiSj+M/NYVtHRVpkgQ55xNO+iU4DYdJMFfi8eR7P1AyAac+SgTUuVF67rSalcAx1At2Hj MvyxYdmwQxII/hvw6jFwaZlWDNSMeLz1A99/F7qxPM/FL1NJt0UhJIcs0u5BMwrNVLKdC08ar+LWe COcVERtCgK3X5PgNFiu/y55vEU3lRa5EIy+rNQQ+iFXB5eMbqhSDPHTK5JGHDZvaHfwWvNQmq7V0O iJeW8fsB11KAGCPobFLjqYix6Utq0y8RBxbLxHIOZc2hV+yXtHahgbB0tnLt3DWjjwK8guwwVFPqi HmLQrf0w==; Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]) by desiato.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tWPee-00000009y9M-0z6t for kvm-riscv@lists.infradead.org; Sat, 11 Jan 2025 00:47:10 +0000 Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-21631789fcdso48605845ad.1 for ; Fri, 10 Jan 2025 16:47:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1736556425; x=1737161225; 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=mG/7mpvt2lUZ0JpSGT/sSZlaG6n67EyEdFyeasyHfcQ=; b=h3hUsaI+BkMcTXKUK0maHm1d8OigIeZwQy01k144MpwSWZpzMfY3+miCge44R2udjm bhO/a2JU8+bghAs9xet4UYY3sPxlsV2w107wKIWIT5jjtT+NIQO0csrccf7zx2/x0jx0 GegB563gsZnwNA30gjtaPnPf+EcdVGO2mQx4D96wZAkIbe+LVjIGMLqoyCIPddrJYFoq eljo5YnWXpJzOgWternGL82pENoS+2tD7opGNkeAyea5wcKk94OefftLbdmdENiNFsUf 5kLpwYvoodyMRFC5L95LV9RFtPpffkCZObTbpxLEIB1podRF2magmukbpthJkdQ22QYx REqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736556425; x=1737161225; 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=mG/7mpvt2lUZ0JpSGT/sSZlaG6n67EyEdFyeasyHfcQ=; b=IgHJlA45pZK+nuQ/pJm7H7hXK8ZKVE/P6un2cWuuFUghIIYIZ67jckA9s9SPYD+WsR ZvFaT+CGd6Z4MGvTJpK58Mn6rqQ/wzGPAUh2irQ6vnHYJ/pmg7cBk2smAuT6dm8zVphK FF7VH43Y9eTZAuuJhchLA+26zbF4f+tOK11xuSxULWm6zikH6CwYJXahx2ZTt1+itYNi Tm7GPe/9xs3u8oQRUIYmhpK6UKF7V7KrzsPmoc+fYv8zr5kW77aSBECQyRB0rGw6L4XW QevDg5k5SVxbJwyTJf6tEaW5HtCytrc14/MegwH8Q1kt7MZHff8Y1XdWUtG4fSE00TjU Wutg== X-Forwarded-Encrypted: i=1; AJvYcCXKtqMtZcwOcFb5xr44/YvRIZP9ZGUAsNYgD+4W6/uwRFkwdM+2Vb7n9brLhXADd1c3uV1xUil9Zn0=@lists.infradead.org X-Gm-Message-State: AOJu0YzpRSiIcUipNAhHBDotIRh0MtvHR9hYRsjjn3UypIvrWRpJqGTn tt+ETZg+Dnl4ZxKOXPE6eqf3rOiy7aFLaugsshzA0SuDh0MDzFsxtU867R8KoI0= X-Gm-Gg: ASbGncsY+Wxv/fCf8AHB4klc1k1r5L0SPj0qqCa+kQaipU+s1up1585afyyfZq/SCxl azvBmMZT7dwm2auz9C9pdOOHwlet9AFE3MkQd5kN95Ny5k5iVsJEixghOQnvpEdDfW9y22uY09y RWk9yfNl2sH8VawiqC+RDmAbyedaSDcYyhVgPiD8FTvpezKVAZ4spQkBXzCgNEXplSVhOosQ7E6 u44K855Qc3f+folXIQjCBp8gT8AqWxAhkLci5eEY5IiaWG7hYYmwho948K6277ApXI2hyi+OD20 p+U= X-Google-Smtp-Source: AGHT+IESLBI5YsUnw0OyUQC27t17HSuv96bLkPw7hOOSo/oEqHzGTMFX68XVgxfF9DQh8PbXSB/mhQ== X-Received: by 2002:a17:90a:c106:b0:2ee:7e53:bfae with SMTP id 98e67ed59e1d1-2f55836e87amr12065549a91.10.1736556425468; Fri, 10 Jan 2025 16:47:05 -0800 (PST) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2f54a28723esm6064295a91.19.2025.01.10.16.47.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Jan 2025 16:47:05 -0800 (PST) From: Samuel Holland To: Anup Patel , Atish Patra , kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org Cc: Samuel Holland , Albert Ou , Palmer Dabbelt , Paul Walmsley , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 1/2] RISC-V: KVM: Fix inclusion of Smnpm in the guest ISA bitmap Date: Fri, 10 Jan 2025 16:46:58 -0800 Message-ID: <20250111004702.2813013-2-samuel.holland@sifive.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250111004702.2813013-1-samuel.holland@sifive.com> References: <20250111004702.2813013-1-samuel.holland@sifive.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250111_004708_841649_F8C346CB X-CRM114-Status: GOOD ( 19.77 ) X-Spam-Score: -0.2 (/) X-Spam-Report: Spam detection software, running on the system "desiato.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: The Smnpm extension requires special handling because the guest ISA extension maps to a different extension (Ssnpm) on the host side. commit 1851e7836212 ("RISC-V: KVM: Allow Smnpm and Ssnpm extension [...] Content analysis details: (-0.2 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:62e 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_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 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 The Smnpm extension requires special handling because the guest ISA extension maps to a different extension (Ssnpm) on the host side. commit 1851e7836212 ("RISC-V: KVM: Allow Smnpm and Ssnpm extensions for guests") missed that the vcpu->arch.isa bit is based only on the host extension, so currently both KVM_RISCV_ISA_EXT_{SMNPM,SSNPM} map to vcpu->arch.isa[RISCV_ISA_EXT_SSNPM]. This does not cause any problems for the guest, because both extensions are force-enabled anyway when the host supports Ssnpm, but prevents checking for (guest) Smnpm in the SBI FWFT logic. Redefine kvm_isa_ext_arr to look up the guest extension, since only the guest -> host mapping is unambiguous. Factor out the logic for checking for host support of an extension, so this special case only needs to be handled in one place, and be explicit about which variables hold a host vs a guest ISA extension. Fixes: 1851e7836212 ("RISC-V: KVM: Allow Smnpm and Ssnpm extensions for guests") Signed-off-by: Samuel Holland --- arch/riscv/kvm/vcpu_onereg.c | 83 +++++++++++++++++++++++------------- 1 file changed, 53 insertions(+), 30 deletions(-) diff --git a/arch/riscv/kvm/vcpu_onereg.c b/arch/riscv/kvm/vcpu_onereg.c index 753f66c8b70a..93115abca3b8 100644 --- a/arch/riscv/kvm/vcpu_onereg.c +++ b/arch/riscv/kvm/vcpu_onereg.c @@ -23,7 +23,7 @@ #define KVM_ISA_EXT_ARR(ext) \ [KVM_RISCV_ISA_EXT_##ext] = RISCV_ISA_EXT_##ext -/* Mapping between KVM ISA Extension ID & Host ISA extension ID */ +/* Mapping between KVM ISA Extension ID & guest ISA extension ID */ static const unsigned long kvm_isa_ext_arr[] = { /* Single letter extensions (alphabetically sorted) */ [KVM_RISCV_ISA_EXT_A] = RISCV_ISA_EXT_a, @@ -35,7 +35,7 @@ static const unsigned long kvm_isa_ext_arr[] = { [KVM_RISCV_ISA_EXT_M] = RISCV_ISA_EXT_m, [KVM_RISCV_ISA_EXT_V] = RISCV_ISA_EXT_v, /* Multi letter extensions (alphabetically sorted) */ - [KVM_RISCV_ISA_EXT_SMNPM] = RISCV_ISA_EXT_SSNPM, + KVM_ISA_EXT_ARR(SMNPM), KVM_ISA_EXT_ARR(SMSTATEEN), KVM_ISA_EXT_ARR(SSAIA), KVM_ISA_EXT_ARR(SSCOFPMF), @@ -107,6 +107,36 @@ static unsigned long kvm_riscv_vcpu_base2isa_ext(unsigned long base_ext) return KVM_RISCV_ISA_EXT_MAX; } +static int kvm_riscv_vcpu_isa_check_host(unsigned long kvm_ext, unsigned long *guest_ext) +{ + unsigned long host_ext; + + if (kvm_ext >= KVM_RISCV_ISA_EXT_MAX || + kvm_ext >= ARRAY_SIZE(kvm_isa_ext_arr)) + return -ENOENT; + + *guest_ext = kvm_isa_ext_arr[kvm_ext]; + switch (*guest_ext) { + case RISCV_ISA_EXT_SMNPM: + /* + * Pointer masking effective in (H)S-mode is provided by the + * Smnpm extension, so that extension is reported to the guest, + * even though the CSR bits for configuring VS-mode pointer + * masking on the host side are part of the Ssnpm extension. + */ + host_ext = RISCV_ISA_EXT_SSNPM; + break; + default: + host_ext = *guest_ext; + break; + } + + if (!__riscv_isa_extension_available(NULL, host_ext)) + return -ENOENT; + + return 0; +} + static bool kvm_riscv_vcpu_isa_enable_allowed(unsigned long ext) { switch (ext) { @@ -209,13 +239,13 @@ static bool kvm_riscv_vcpu_isa_disable_allowed(unsigned long ext) void kvm_riscv_vcpu_setup_isa(struct kvm_vcpu *vcpu) { - unsigned long host_isa, i; + unsigned long guest_ext, i; for (i = 0; i < ARRAY_SIZE(kvm_isa_ext_arr); i++) { - host_isa = kvm_isa_ext_arr[i]; - if (__riscv_isa_extension_available(NULL, host_isa) && - kvm_riscv_vcpu_isa_enable_allowed(i)) - set_bit(host_isa, vcpu->arch.isa); + if (kvm_riscv_vcpu_isa_check_host(i, &guest_ext)) + continue; + if (kvm_riscv_vcpu_isa_enable_allowed(i)) + set_bit(guest_ext, vcpu->arch.isa); } } @@ -597,18 +627,15 @@ static int riscv_vcpu_get_isa_ext_single(struct kvm_vcpu *vcpu, unsigned long reg_num, unsigned long *reg_val) { - unsigned long host_isa_ext; - - if (reg_num >= KVM_RISCV_ISA_EXT_MAX || - reg_num >= ARRAY_SIZE(kvm_isa_ext_arr)) - return -ENOENT; + unsigned long guest_ext; + int ret; - host_isa_ext = kvm_isa_ext_arr[reg_num]; - if (!__riscv_isa_extension_available(NULL, host_isa_ext)) - return -ENOENT; + ret = kvm_riscv_vcpu_isa_check_host(reg_num, &guest_ext); + if (ret) + return ret; *reg_val = 0; - if (__riscv_isa_extension_available(vcpu->arch.isa, host_isa_ext)) + if (__riscv_isa_extension_available(vcpu->arch.isa, guest_ext)) *reg_val = 1; /* Mark the given extension as available */ return 0; @@ -618,17 +645,14 @@ static int riscv_vcpu_set_isa_ext_single(struct kvm_vcpu *vcpu, unsigned long reg_num, unsigned long reg_val) { - unsigned long host_isa_ext; - - if (reg_num >= KVM_RISCV_ISA_EXT_MAX || - reg_num >= ARRAY_SIZE(kvm_isa_ext_arr)) - return -ENOENT; + unsigned long guest_ext; + int ret; - host_isa_ext = kvm_isa_ext_arr[reg_num]; - if (!__riscv_isa_extension_available(NULL, host_isa_ext)) - return -ENOENT; + ret = kvm_riscv_vcpu_isa_check_host(reg_num, &guest_ext); + if (ret) + return ret; - if (reg_val == test_bit(host_isa_ext, vcpu->arch.isa)) + if (reg_val == test_bit(guest_ext, vcpu->arch.isa)) return 0; if (!vcpu->arch.ran_atleast_once) { @@ -638,10 +662,10 @@ static int riscv_vcpu_set_isa_ext_single(struct kvm_vcpu *vcpu, */ if (reg_val == 1 && kvm_riscv_vcpu_isa_enable_allowed(reg_num)) - set_bit(host_isa_ext, vcpu->arch.isa); + set_bit(guest_ext, vcpu->arch.isa); else if (!reg_val && kvm_riscv_vcpu_isa_disable_allowed(reg_num)) - clear_bit(host_isa_ext, vcpu->arch.isa); + clear_bit(guest_ext, vcpu->arch.isa); else return -EINVAL; kvm_riscv_vcpu_fp_reset(vcpu); @@ -999,16 +1023,15 @@ static int copy_fp_d_reg_indices(const struct kvm_vcpu *vcpu, static int copy_isa_ext_reg_indices(const struct kvm_vcpu *vcpu, u64 __user *uindices) { + unsigned long guest_ext; unsigned int n = 0; - unsigned long isa_ext; for (int i = 0; i < KVM_RISCV_ISA_EXT_MAX; i++) { u64 size = IS_ENABLED(CONFIG_32BIT) ? KVM_REG_SIZE_U32 : KVM_REG_SIZE_U64; u64 reg = KVM_REG_RISCV | size | KVM_REG_RISCV_ISA_EXT | i; - isa_ext = kvm_isa_ext_arr[i]; - if (!__riscv_isa_extension_available(NULL, isa_ext)) + if (kvm_riscv_vcpu_isa_check_host(i, &guest_ext)) continue; if (uindices) { From patchwork Sat Jan 11 00:46:59 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 2033047 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=MYwsuauC; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=sifive.com header.i=@sifive.com header.a=rsa-sha256 header.s=google header.b=RqkqA6Yy; 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 4YVKc05H8Wz1yPD for ; Sat, 11 Jan 2025 11:47:10 +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=tr3vYtCQeWF0JaOMcEx+S9PksfFR0AeGPQG+fiYSMRY=; b=MYwsuauCzjVVoD 6NiZBjnb9v2pJV18RGg2WtSjnSaeP7bqfdKVMisanXZciXwVwf1SCNGKTv9fiajuSXDYkuykjV/3M 5jXYiKj72TSjhcci81xUbrYxTBl1Vn4Y5p5/L0LLNKPLYb0mBlJMC0d9f2QSwjy8agqkAOI5zH6P0 acwpOgJ2flkMuFGF2j4wwjwGzpSJiAtklGQn7lGI23jCjtCqoetIb2ElTl2LDAVKjjBl80Z1jeEGb zXQztVTwwrfG5PFT1NogxO+KO6ufLnIXf4A7ZrTtyZ8QorvMNhlWOIfjJoVuKwzNHPByqmkGtx0Ut ig/mkxzSdbQ0vuGA3qzg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tWPef-0000000HPta-2bGo; Sat, 11 Jan 2025 00:47:09 +0000 Received: from mail-pj1-x1036.google.com ([2607:f8b0:4864:20::1036]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tWPed-0000000HPsF-11UF for kvm-riscv@lists.infradead.org; Sat, 11 Jan 2025 00:47:08 +0000 Received: by mail-pj1-x1036.google.com with SMTP id 98e67ed59e1d1-2f4448bf96fso3461420a91.0 for ; Fri, 10 Jan 2025 16:47:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1736556427; x=1737161227; 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=dhKWMGnM57WsMY43qomywj90difyS8W0XzJpu0qHE5Y=; b=RqkqA6Yykt6/2mIcyAuPXLyY7OOYdxsofFofeXt1y60bAFNtPKJ47nUO/9iG1o/egw lNi9ARV6FKFWJH8P76TI3MFK3xzzzrKytdHDO+RAvRkuC0KQKpNDbRDcYyf5Xlri4x75 AsIgC1COujDh+Y56e7OQlIbsSCiI5qSMh+ujP5nrFch4DbvbHwedwBmMx0TkVmojevng +nbMADPvND8rtGQ0YaAjo7t8f00GVPDO3XwZH5a+LEp3oEmkI0KhCGTWTmB1QYJ6Ix8k dFzFtVMjhIDlxOO20BlCiQ4RbfTpjlcengRrYOTJMquYWVHc19RXVdyrl4RSivAweQsB l5Sg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736556427; x=1737161227; 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=dhKWMGnM57WsMY43qomywj90difyS8W0XzJpu0qHE5Y=; b=ib5d1+N31w20kmHvzEsCzEIHogYUxsE1Q9I56tDB3NmyIBie4EPPM8ytiViCDMJ834 cZSUmhybHrTRUpKxgZT86SiaI8uLWs4DP0wKC/ecu1X/xwyXVdCJr602zxYOZd/gOR58 HkF4q14h85gjNpqa59xY9PqkQRl2WoWVBkJ2u9bNmVyoaYrQmLW79Wpt7VY7cMF2GIKD IQnBJ/aqkJ+w/aSgUTdHCnheYNoGzFNfK182n8ZE5RCZvsjAw9UH6wcC6ztetMtyBvLf kr++r7BTfBV7T/deYTPEVK/VOxca3JR6iBTlwFtRoC6tXU9TsbLxAxcB+nLFYCs6ozx1 jloQ== X-Forwarded-Encrypted: i=1; AJvYcCU8vTfE29kOzUezmEY6v8cUmAdBdVYR4+f/craqdBfqG/q6QFDcqGsG7x1Z3aqBqgr3bWigspbVErk=@lists.infradead.org X-Gm-Message-State: AOJu0YxaERoEuzcQG21XYiDMsDUdpYGUs1C0ydgncoVUNU2WP6c+jNkz fxHBNdY2y7ymB/Q60AlXs5jOL9HH0/nZppHydpGcQaod5uMoSZgeyXviPzzTEpk= X-Gm-Gg: ASbGncvIrWqkyw/1/0orQ+UM+Jpm4y2vuZsGivqk6XeCWhPrZw8+UIr6zbVQlg3fQKj BikzmVSckG1Jtuo/x0W6i40ounUf2w2PwmmPYkm2myE1AKZQuCSo01VkC5qzTNI6nZ2W7gRq+yv oiDrLSGPazfjSGhIB3UqChouJA5gqB71xIFF4czfpQJBsH/NDY1VBuU7rscjo3yTH1naPad4odL IWva68xs23qmXrBUXfiFx3PwUBPbJPoex9lysiljLrDZN2JS9KZfE+SaVd8Z43pgPv0rZ12ClD2 qxY= X-Google-Smtp-Source: AGHT+IEzAQsFVVUnarlp48IX+/exhmsTTctl6ss+iMEnk5dre6TgWISy267dYaDXt8LAjE4L45Tvqw== X-Received: by 2002:a17:90b:3bcf:b0:2ee:b6c5:1de7 with SMTP id 98e67ed59e1d1-2f548e9872emr19653709a91.2.1736556426732; Fri, 10 Jan 2025 16:47:06 -0800 (PST) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2f54a28723esm6064295a91.19.2025.01.10.16.47.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Jan 2025 16:47:06 -0800 (PST) From: Samuel Holland To: Anup Patel , Atish Patra , kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org Cc: Samuel Holland , Albert Ou , Palmer Dabbelt , Paul Walmsley , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 2/2] RISC-V: KVM: Add support for SBI_FWFT_POINTER_MASKING_PMLEN Date: Fri, 10 Jan 2025 16:46:59 -0800 Message-ID: <20250111004702.2813013-3-samuel.holland@sifive.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250111004702.2813013-1-samuel.holland@sifive.com> References: <20250111004702.2813013-1-samuel.holland@sifive.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250110_164707_272727_B2171AFA X-CRM114-Status: GOOD ( 18.30 ) 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: Pointer masking is controlled through a WARL field in henvcfg. Expose the feature only if at least one PMLEN value is supported for VS-mode. Allow the VMM to block access to the feature by disabling t [...] 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:1036 listed in] [list.dnswl.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -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 -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 Pointer masking is controlled through a WARL field in henvcfg. Expose the feature only if at least one PMLEN value is supported for VS-mode. Allow the VMM to block access to the feature by disabling the Smnpm ISA extension in the guest. Signed-off-by: Samuel Holland --- arch/riscv/include/asm/kvm_vcpu_sbi_fwft.h | 2 + arch/riscv/kvm/vcpu_onereg.c | 1 - arch/riscv/kvm/vcpu_sbi_fwft.c | 70 +++++++++++++++++++++- 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/arch/riscv/include/asm/kvm_vcpu_sbi_fwft.h b/arch/riscv/include/asm/kvm_vcpu_sbi_fwft.h index 5782517f6e08..5176344d9162 100644 --- a/arch/riscv/include/asm/kvm_vcpu_sbi_fwft.h +++ b/arch/riscv/include/asm/kvm_vcpu_sbi_fwft.h @@ -30,6 +30,8 @@ struct kvm_sbi_fwft_config { /* FWFT data structure per vcpu */ struct kvm_sbi_fwft { struct kvm_sbi_fwft_config *configs; + bool have_vs_pmlen_7; + bool have_vs_pmlen_16; }; #define vcpu_to_fwft(vcpu) (&(vcpu)->arch.fwft_context) diff --git a/arch/riscv/kvm/vcpu_onereg.c b/arch/riscv/kvm/vcpu_onereg.c index 93115abca3b8..1d2033b33e6d 100644 --- a/arch/riscv/kvm/vcpu_onereg.c +++ b/arch/riscv/kvm/vcpu_onereg.c @@ -168,7 +168,6 @@ static bool kvm_riscv_vcpu_isa_disable_allowed(unsigned long ext) case KVM_RISCV_ISA_EXT_C: case KVM_RISCV_ISA_EXT_I: case KVM_RISCV_ISA_EXT_M: - case KVM_RISCV_ISA_EXT_SMNPM: /* There is not architectural config bit to disable sscofpmf completely */ case KVM_RISCV_ISA_EXT_SSCOFPMF: case KVM_RISCV_ISA_EXT_SSNPM: diff --git a/arch/riscv/kvm/vcpu_sbi_fwft.c b/arch/riscv/kvm/vcpu_sbi_fwft.c index 1e85ff6666af..6e8f818fd6f5 100644 --- a/arch/riscv/kvm/vcpu_sbi_fwft.c +++ b/arch/riscv/kvm/vcpu_sbi_fwft.c @@ -68,13 +68,81 @@ static int kvm_sbi_fwft_get_misaligned_delegation(struct kvm_vcpu *vcpu, return SBI_SUCCESS; } +static bool try_to_set_pmm(unsigned long value) +{ + csr_set(CSR_HENVCFG, value); + return (csr_read_clear(CSR_HENVCFG, ENVCFG_PMM) & ENVCFG_PMM) == value; +} + +static bool kvm_sbi_fwft_pointer_masking_pmlen_supported(struct kvm_vcpu *vcpu) +{ + struct kvm_sbi_fwft *fwft = vcpu_to_fwft(vcpu); + + if (!riscv_isa_extension_available(vcpu->arch.isa, SMNPM)) + return false; + + fwft->have_vs_pmlen_7 = try_to_set_pmm(ENVCFG_PMM_PMLEN_7); + fwft->have_vs_pmlen_16 = try_to_set_pmm(ENVCFG_PMM_PMLEN_16); + + return fwft->have_vs_pmlen_7 || fwft->have_vs_pmlen_16; +} + +static int kvm_sbi_fwft_set_pointer_masking_pmlen(struct kvm_vcpu *vcpu, + struct kvm_sbi_fwft_config *conf, + unsigned long value) +{ + struct kvm_sbi_fwft *fwft = vcpu_to_fwft(vcpu); + unsigned long pmm; + + if (value == 0) + pmm = ENVCFG_PMM_PMLEN_0; + else if (value <= 7 && fwft->have_vs_pmlen_7) + pmm = ENVCFG_PMM_PMLEN_7; + else if (value <= 16 && fwft->have_vs_pmlen_16) + pmm = ENVCFG_PMM_PMLEN_16; + else + return SBI_ERR_INVALID_PARAM; + + vcpu->arch.cfg.henvcfg &= ~ENVCFG_PMM; + vcpu->arch.cfg.henvcfg |= pmm; + + return SBI_SUCCESS; +} + +static int kvm_sbi_fwft_get_pointer_masking_pmlen(struct kvm_vcpu *vcpu, + struct kvm_sbi_fwft_config *conf, + unsigned long *value) +{ + switch (vcpu->arch.cfg.henvcfg & ENVCFG_PMM) { + case ENVCFG_PMM_PMLEN_0: + *value = 0; + break; + case ENVCFG_PMM_PMLEN_7: + *value = 7; + break; + case ENVCFG_PMM_PMLEN_16: + *value = 16; + break; + default: + return SBI_ERR_FAILURE; + } + + return SBI_SUCCESS; +} + static const struct kvm_sbi_fwft_feature features[] = { { .id = SBI_FWFT_MISALIGNED_EXC_DELEG, .supported = kvm_sbi_fwft_misaligned_delegation_supported, .set = kvm_sbi_fwft_set_misaligned_delegation, .get = kvm_sbi_fwft_get_misaligned_delegation, - } + }, + { + .id = SBI_FWFT_POINTER_MASKING_PMLEN, + .supported = kvm_sbi_fwft_pointer_masking_pmlen_supported, + .set = kvm_sbi_fwft_set_pointer_masking_pmlen, + .get = kvm_sbi_fwft_get_pointer_masking_pmlen, + }, }; static struct kvm_sbi_fwft_config *