From nobody Tue Dec 16 07:42:14 2025 Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0F2CA2741B9 for ; Mon, 5 May 2025 21:39:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746481179; cv=none; b=DOOjFIA8dU1/oXBBELif4sKPw4cphBporQ8NecP4y9onkU+7ck/AJpgCBELGr5pddIv7JjAlsh/tDd6FM5Bg7RTFhxnldTOgjWM0O7qDpKzhsWIqcQNONZ6hO7Nu6YRmEEWZNz0ME22akG7WcI/cSMDwRMtwIEV1DqZwLh4B68M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746481179; c=relaxed/simple; bh=5PhR4Cke8yMwoA4uPKJBWa65xrmcQt00CtfyS9bPFCo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=p3SVSMk2Xk3Cz4vOvwxPuXE3ySH4y0zOOhhZSm5/YhlDnQWaUmVaQ0FOqCQhSKGv07wN6FeHuTdD5gZqs9DtIAKR4myX0+jwY+MVATUegYa7wqAmFBXLJUQMX9NMvWR+2a5EVvQ44IrDEnq7Mv2pn6UpU6NEWmL6Ux4KV043Gm4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=Ohzy+g+6; arc=none smtp.client-ip=209.85.214.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="Ohzy+g+6" Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-2241053582dso74327965ad.1 for ; Mon, 05 May 2025 14:39:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1746481177; x=1747085977; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=GuR/z1GpxuJ9rGb2JSweVgvKX4SVj6nkkUnTCjHYUz4=; b=Ohzy+g+6hZbJQLRJRHPr0TQ1O87bk+3sRgUZgcxEGSqaxMSjogksq+h3OWw88VRZVh 6Nsry3GiWf32vLrFmWfuuSh/HCHo6cr+QhyoNAT9SPhnM6Xbaj2hB4EAY2+zfeX3LDDC TrTFikHt0hzLHYFBwJ+yej6uVo/zPH6abIZqd40MIK13EzG3aPTaPCaVtGGfwEWBxIGM KXvx/YKEL91ea3kHPKRagtDf9f+ge5MFN95eQQhx3N9zrvTWx3eGgXy8lFnYteUBzicA EdK71MhRIsjs7ptcJ6p710nRUpWkJsabWNmsFOlHeJUR9wXoGQGuJ7CrSEL0X2Cz86v1 8BeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746481177; x=1747085977; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GuR/z1GpxuJ9rGb2JSweVgvKX4SVj6nkkUnTCjHYUz4=; b=kJ5DJA5iNHvuAUS5gsX0Ou/Yikdw4DdOsYCupNzh4wXc7mbkL0hTNItOZy52cXDR8a NJfE+NOOquXAmxAJwt9qnvrAd1v3oEBaHt7Tr3JtMMe3aj+BnMDyJcTslcfXZa4BvHHo ElAyXPqCZQVFXoTuwlAWSMoFcWPa63WjhNJxcCR02AdH4cQYiThjT9xEj2YhKg7aZQsk HC7fzOvnM7oeERSqG8IYucY2dwnkpmd6IqmLI3IR38vEzhaeaYG7fkaXdzRH8f3QcLe7 9m6Jnop2Qz/tH2k5okYoiWdm8/60y0BNe2U496c8RuwCRlOtyjPn2LI29uw4+lcM4vo6 qyIA== X-Forwarded-Encrypted: i=1; AJvYcCWfaW2P/jFgtEv5CAi9WhAGAzNOQUkEXT2TWJSwBuXznC3r56/80aheJmdSPJ9rn257hJVTx/O3S28Oz8I=@vger.kernel.org X-Gm-Message-State: AOJu0Yzc6ohxKIhE01Ro877bDg6+rdU+zfc3CCQ1OpQyw54t+/6QPEAw ba5YJIHXAaUYrD1vnlj6nJoxj56QvFebMtHOiC9QsrFUb9o0MYPWsCjp9xbZeiM= X-Gm-Gg: ASbGnctgoHFdSeIT5KjWVzreMwikj9kJn852rMv9eW9CMmyYb2L07yC8Pj0KikPaQQ/ uGuOS6ffev/wr3rJ6mwuymnp4GGO5RfqL6303kf9BkRiyj2/QcJ/k9WEDlrVH4UWpZiGwBSHWW5 I+dG2sH6bJDlTeVc+m/5FGdTxrOnc6URhmurf8UEBeRwuQmxf8krjF1ilkFDcKvcC77+CFiKAU9 YNyLYF7qWtwTZTjx6u8979uyfE2n+nEecpyeDlP/4ciGgcJv2tdk26yjugEW1od6rjvKl8mw98F 0rX8l8mth514lUHkk6mjO8ZR3tp/SGH2OLnKp2rENbjCcTo2li4faQ== X-Google-Smtp-Source: AGHT+IGhry+0GqEK0QENhdqzpXsxfdR/JgSEWuVPvlQK1RPcseXdT65lptZT2IHTz8U/93rQIvZwKw== X-Received: by 2002:a17:903:41cb:b0:220:e1e6:4472 with SMTP id d9443c01a7336-22e328cf92emr13304615ad.13.1746481177225; Mon, 05 May 2025 14:39:37 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-74058df12d8sm7388599b3a.78.2025.05.05.14.39.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 May 2025 14:39:36 -0700 (PDT) From: Atish Patra Date: Mon, 05 May 2025 14:39:26 -0700 Subject: [PATCH 1/5] RISC-V: KVM: Lazy enable hstateen IMSIC & ISEL bit Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250505-kvm_lazy_enable_stateen-v1-1-3bfc4008373c@rivosinc.com> References: <20250505-kvm_lazy_enable_stateen-v1-0-3bfc4008373c@rivosinc.com> In-Reply-To: <20250505-kvm_lazy_enable_stateen-v1-0-3bfc4008373c@rivosinc.com> To: Anup Patel , Atish Patra , Paul Walmsley , Palmer Dabbelt , Alexandre Ghiti Cc: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Atish Patra X-Mailer: b4 0.15-dev-42535 Currently, we enable the smstateen bit at vcpu configure time by only checking the presence of required ISA extensions. These bits are not required to be enabled if the guest never uses the corresponding architectural state. Enable the smstaeen bits at runtime lazily upon first access. Signed-off-by: Atish Patra --- arch/riscv/include/asm/kvm_aia.h | 1 + arch/riscv/kvm/aia.c | 43 ++++++++++++++++++++++++++++++++++++= ++++ arch/riscv/kvm/aia_imsic.c | 8 ++++++++ 3 files changed, 52 insertions(+) diff --git a/arch/riscv/include/asm/kvm_aia.h b/arch/riscv/include/asm/kvm_= aia.h index 1f37b600ca47..760a1aef09f7 100644 --- a/arch/riscv/include/asm/kvm_aia.h +++ b/arch/riscv/include/asm/kvm_aia.h @@ -112,6 +112,7 @@ int kvm_riscv_aia_aplic_has_attr(struct kvm *kvm, unsig= ned long type); int kvm_riscv_aia_aplic_inject(struct kvm *kvm, u32 source, bool level); int kvm_riscv_aia_aplic_init(struct kvm *kvm); void kvm_riscv_aia_aplic_cleanup(struct kvm *kvm); +bool kvm_riscv_aia_imsic_state_hw_backed(struct kvm_vcpu *vcpu); =20 #ifdef CONFIG_32BIT void kvm_riscv_vcpu_aia_flush_interrupts(struct kvm_vcpu *vcpu); diff --git a/arch/riscv/kvm/aia.c b/arch/riscv/kvm/aia.c index 19afd1f23537..1e0d2217ade7 100644 --- a/arch/riscv/kvm/aia.c +++ b/arch/riscv/kvm/aia.c @@ -241,6 +241,8 @@ int kvm_riscv_vcpu_aia_rmw_topei(struct kvm_vcpu *vcpu, unsigned long new_val, unsigned long wr_mask) { + bool vsfile_present =3D kvm_riscv_aia_imsic_state_hw_backed(vcpu); + /* If AIA not available then redirect trap */ if (!kvm_riscv_aia_available()) return KVM_INSN_ILLEGAL_TRAP; @@ -249,6 +251,26 @@ int kvm_riscv_vcpu_aia_rmw_topei(struct kvm_vcpu *vcpu, if (!kvm_riscv_aia_initialized(vcpu->kvm)) return KVM_INSN_EXIT_TO_USER_SPACE; =20 + /* Continue if smstaeen is not present */ + if (!riscv_has_extension_unlikely(RISCV_ISA_EXT_SMSTATEEN)) + goto skip_hstateen; + + /* Enable the bit in hstateen0 lazily upon first access */ + if (!(vcpu->arch.cfg.hstateen0 & SMSTATEEN0_AIA_IMSIC)) { + vcpu->arch.cfg.hstateen0 |=3D SMSTATEEN0_AIA_IMSIC; + if (IS_ENABLED(CONFIG_32BIT)) + csr_set(CSR_HSTATEEN0H, SMSTATEEN0_AIA_IMSIC >> 32); + else + csr_set(CSR_HSTATEEN0, SMSTATEEN0_AIA_IMSIC); + if (vsfile_present) + return KVM_INSN_CONTINUE_SAME_SEPC; + } else if (vsfile_present) { + pr_err("Unexpected trap for CSR [%x] with hstateen0 enabled and valid vs= file\n", + csr_num); + return KVM_INSN_EXIT_TO_USER_SPACE; + } + +skip_hstateen: return kvm_riscv_vcpu_aia_imsic_rmw(vcpu, KVM_RISCV_AIA_IMSIC_TOPEI, val, new_val, wr_mask); } @@ -400,11 +422,32 @@ int kvm_riscv_vcpu_aia_rmw_ireg(struct kvm_vcpu *vcpu= , unsigned int csr_num, unsigned long wr_mask) { unsigned int isel; + bool vsfile_present =3D kvm_riscv_aia_imsic_state_hw_backed(vcpu); =20 /* If AIA not available then redirect trap */ if (!kvm_riscv_aia_available()) return KVM_INSN_ILLEGAL_TRAP; =20 + /* Continue if smstaeen is not present */ + if (!riscv_has_extension_unlikely(RISCV_ISA_EXT_SMSTATEEN)) + goto skip_hstateen; + + /* Enable the bit in hstateen0 lazily upon first access */ + if (!(vcpu->arch.cfg.hstateen0 & SMSTATEEN0_AIA_ISEL)) { + vcpu->arch.cfg.hstateen0 |=3D SMSTATEEN0_AIA_ISEL; + if (IS_ENABLED(CONFIG_32BIT)) + csr_set(CSR_HSTATEEN0H, SMSTATEEN0_AIA_ISEL >> 32); + else + csr_set(CSR_HSTATEEN0, SMSTATEEN0_AIA_ISEL); + if (vsfile_present) + return KVM_INSN_CONTINUE_SAME_SEPC; + } else if (vsfile_present) { + pr_err("Unexpected trap for CSR [%x] with hstateen0 enabled and valid vs= file\n", + csr_num); + return KVM_INSN_EXIT_TO_USER_SPACE; + } + +skip_hstateen: /* First try to emulate in kernel space */ isel =3D ncsr_read(CSR_VSISELECT) & ISELECT_MASK; if (isel >=3D ISELECT_IPRIO0 && isel <=3D ISELECT_IPRIO15) diff --git a/arch/riscv/kvm/aia_imsic.c b/arch/riscv/kvm/aia_imsic.c index 29ef9c2133a9..d8e6f14850c0 100644 --- a/arch/riscv/kvm/aia_imsic.c +++ b/arch/riscv/kvm/aia_imsic.c @@ -361,6 +361,14 @@ static int imsic_mrif_rmw(struct imsic_mrif *mrif, u32= nr_eix, return 0; } =20 +bool kvm_riscv_aia_imsic_state_hw_backed(struct kvm_vcpu *vcpu) +{ + struct kvm_vcpu_aia *vaia =3D &vcpu->arch.aia_context; + struct imsic *imsic =3D vaia->imsic_state; + + return imsic && imsic->vsfile_cpu >=3D 0; +} + struct imsic_vsfile_read_data { int hgei; u32 nr_eix; --=20 2.43.0 From nobody Tue Dec 16 07:42:14 2025 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D6170274667 for ; Mon, 5 May 2025 21:39:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746481180; cv=none; b=TM7/Hpu/5VCB2iovds80IaQjjOHWuGXAaBrOTXbKstnleYGSgmDtQZwcES28ci2hHdY8mfDRe46ss5SyicVAjPg35mNuVt+W4cf8u1ahf3LvpnrGkb1gvuh7JIqzRAi8OYTcwvNmIYqxpYazByvlMjDPeLzY1ByPdgykWKSzaZM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746481180; c=relaxed/simple; bh=9BSvBt0OecV6YwUFtY5L/MBkp8S1ZbggBZys7UTbSWo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=L9Se+YTMDt4O+fPyQmN/w1qXczP2CUcYI/UX52P/yfM8RjMfbKZBKSyD9cGTICoG7NhjeCv9sBFn5jF7Ot9lG06y+SYGF014CV0iRTUE7cBXmQU2mBc1vWBSoRDxmdOTi2cog/wx6nZ+40uOtWrTqSzHKOw8EjO+l00Nq9ueW3A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=yMMoOl15; arc=none smtp.client-ip=209.85.214.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="yMMoOl15" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-2264aefc45dso78174095ad.0 for ; Mon, 05 May 2025 14:39:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1746481178; x=1747085978; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=hrOpef+mqFExNdYyOI2IIHYIr3RNpPTOkYkLVHjm6Us=; b=yMMoOl15EbGWPYjGib8ZBup107wwSxrLlpEyC/IwPe+c+1mWJyOoHM8PD+UZNw72FK SKBKVdXETHO64YxwYCrGgIi/o9KZP8JS86RBmgUdIqr+4ZZC7nfdd1yObNWX9QemajOc d2Qpcdoiljd9EQRjKP1j1eYTk6waMrN7dUIFnQxJe1yB7epIzZ/6cOr0GHlSVHK2BO1C KvlbHpqNallF9EPXwDdtmSEAWrn04n3zdzLKlH5PhsvtKOFAVknyPOCRHX70C5HcuZGC c/3MUTqb2x/cwzxcK0/iRjvxXs3cXm563GMqv7m2amUilQ81OVYpdJHWLK4YLTvSW9Au GO0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746481178; x=1747085978; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hrOpef+mqFExNdYyOI2IIHYIr3RNpPTOkYkLVHjm6Us=; b=Rky1ZwWgEtdxCvLfLbrzQMXcAyHQmFsRvaJbczhnQ22CwXuwKpmSg2PCpmFArdToET tADBVG9rfaD19dv38ZxmC7fY+02K5a1uIW50kzn6GeUBwUClAK3GGqOIGhCuGNrmNmym /iADdtOTSGUe/XHSgYI/puBmpDhp7gPLg4vvYJqHt/zRzXSX4UzmqFMEcaG+OBqRIMV2 8KWZnjflQ04PHVucyXRjoryhfMheI2K2RZo24o+v1FmSf5IW6sPzqb/DVtq/XHGIc2Wn 0+funDB2jOkGVNVQyNTes0DfopCaC7oc3464JtRJvKzJd4nsxOuzHdywXYCDttzT+WaE xKDw== X-Forwarded-Encrypted: i=1; AJvYcCXIG2vCV1tiOn6mPE1iPqqdzv0IbbiGQt4KV74AXZnSHxRo4DJnO9AjR2gYyA940rQ/uis3kFLwShgp09g=@vger.kernel.org X-Gm-Message-State: AOJu0YxhOcFqa0YADV2AiwcwxXMXGexORMsrXHLDrmpeS+gz16Bs+8ip SWyJ+H26vPyCCp0bvT6AKj4KAUHfC4mFiyPYebfQIab7tqks5Y+NZs2jjSQSYfo= X-Gm-Gg: ASbGncu2ibJ0Mt3EL9ozhrxOnemcnG4zYwa9LpoAB2LW+r2mdUpC+55hZQ0egC6WOdt Hh2hKOUILmGgWB6OB4soWYH1lfkYG4rbpUqQVUQy3DNOM1c4H7e7X7rZ9Y04TgutsVgyvwWGIeD TAMhG5B2mqrmpoG1LtLC79+f1g4kUSR1T06hrjSTW7FWLac5fRi59pHxEVJYb0SDQo21eN3Iism D70e2hY7tupr4Z8kZfr/OJ5mmTpHY4z3+jP8MSPuXIcW8Pz3vA0gw0MTXcYkRL4OcQwl9neb+CE rVSs71H6whg8eLjeHfaIfWsg2mUGPXIZnWyDLI4OhtcQlXGeiBHDyg== X-Google-Smtp-Source: AGHT+IFfWbZJOYWgohE6peHPOXmV8Bx9l1eOyThbAl9PHsbsWLwC5zSC5Wu3EefTf6bWH77K42NMRQ== X-Received: by 2002:a17:902:e80e:b0:224:13a4:d61e with SMTP id d9443c01a7336-22e1eb0a454mr122010005ad.51.1746481178148; Mon, 05 May 2025 14:39:38 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-74058df12d8sm7388599b3a.78.2025.05.05.14.39.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 May 2025 14:39:37 -0700 (PDT) From: Atish Patra Date: Mon, 05 May 2025 14:39:27 -0700 Subject: [PATCH 2/5] RISC-V: KVM: Add a hstateen lazy enabler helper function Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250505-kvm_lazy_enable_stateen-v1-2-3bfc4008373c@rivosinc.com> References: <20250505-kvm_lazy_enable_stateen-v1-0-3bfc4008373c@rivosinc.com> In-Reply-To: <20250505-kvm_lazy_enable_stateen-v1-0-3bfc4008373c@rivosinc.com> To: Anup Patel , Atish Patra , Paul Walmsley , Palmer Dabbelt , Alexandre Ghiti Cc: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Atish Patra X-Mailer: b4 0.15-dev-42535 Hstateen has different bits that can be enabled lazily at runtime. Most of them have similar functionality where the hstateen bit must be enabled if not enabled already. The correpsonding config bit in vcpu must be enabled as well so that hstateen CSR is updated correctly during the next vcpu load. In absesnce of Smstateen extension, exit to the userspace in the trap because CSR access control exists architecturally only if Smstateen extension is available. Add a common helper function to achieve the above said objective. Signed-off-by: Atish Patra --- arch/riscv/include/asm/kvm_vcpu_insn.h | 4 ++++ arch/riscv/kvm/vcpu_insn.c | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/arch/riscv/include/asm/kvm_vcpu_insn.h b/arch/riscv/include/as= m/kvm_vcpu_insn.h index 350011c83581..1125f3f1c8c4 100644 --- a/arch/riscv/include/asm/kvm_vcpu_insn.h +++ b/arch/riscv/include/asm/kvm_vcpu_insn.h @@ -6,6 +6,8 @@ #ifndef __KVM_VCPU_RISCV_INSN_H #define __KVM_VCPU_RISCV_INSN_H =20 +#include + struct kvm_vcpu; struct kvm_run; struct kvm_cpu_trap; @@ -44,5 +46,7 @@ int kvm_riscv_vcpu_mmio_store(struct kvm_vcpu *vcpu, stru= ct kvm_run *run, unsigned long fault_addr, unsigned long htinst); int kvm_riscv_vcpu_mmio_return(struct kvm_vcpu *vcpu, struct kvm_run *run); +int kvm_riscv_vcpu_hstateen_lazy_enable(struct kvm_vcpu *vcpu, unsigned in= t csr_num, + uint64_t hstateen_feature_bit_mask); =20 #endif diff --git a/arch/riscv/kvm/vcpu_insn.c b/arch/riscv/kvm/vcpu_insn.c index 97dec18e6989..3bc39572b79d 100644 --- a/arch/riscv/kvm/vcpu_insn.c +++ b/arch/riscv/kvm/vcpu_insn.c @@ -235,6 +235,27 @@ static int seed_csr_rmw(struct kvm_vcpu *vcpu, unsigne= d int csr_num, return KVM_INSN_EXIT_TO_USER_SPACE; } =20 +int kvm_riscv_vcpu_hstateen_lazy_enable(struct kvm_vcpu *vcpu, unsigned in= t csr_num, + uint64_t hstateen_feature_bit_mask) +{ + /* Access from VS shouldn't trap if smstaeen is not present */ + if (!riscv_has_extension_unlikely(RISCV_ISA_EXT_SMSTATEEN)) + return KVM_INSN_EXIT_TO_USER_SPACE; + + /* Enable the bit in hstateen0 lazily upon first access */ + if (!(vcpu->arch.cfg.hstateen0 & hstateen_feature_bit_mask)) { + vcpu->arch.cfg.hstateen0 |=3D hstateen_feature_bit_mask; + csr_set(CSR_HSTATEEN0, hstateen_feature_bit_mask); + if (IS_ENABLED(CONFIG_32BIT)) + csr_set(CSR_HSTATEEN0H, hstateen_feature_bit_mask >> 32); + } else { + return KVM_INSN_EXIT_TO_USER_SPACE; + } + + /* Let the guest retry the instruction read after hstateen0 is modified */ + return KVM_INSN_CONTINUE_SAME_SEPC; +} + static const struct csr_func csr_funcs[] =3D { KVM_RISCV_VCPU_AIA_CSR_FUNCS KVM_RISCV_VCPU_HPMCOUNTER_CSR_FUNCS --=20 2.43.0 From nobody Tue Dec 16 07:42:14 2025 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BEA60274676 for ; Mon, 5 May 2025 21:39:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746481181; cv=none; b=GwVKeThFgVMgyXu3tSVxfiaz4hORXyKox7ySVS7i8klDdIJhUtmDUgP8XiUJQmGHeIOjBpCOHnKLE4Gr2QTk+eprbMdsGDhCeD6eNXMWDwdP2SqmNg6Mik6JmO77HMBGPrrl0fUcPiYdAMybNdrazbTJ6z5N86e5pt0FS59D4MU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746481181; c=relaxed/simple; bh=jhp/61im/NHYvFzN/JEXw6pD0pJy0Hk/tmvWDBz/SFM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ER4mc+uqaQXBqFLzXg4fCwRDcxClsbLzhDLmQe7w6iFAK551+REKfkr99e1nxEMhWLKWMRRVs3qShzJ4A0/yYpfEh6ayHhPcKl4mwMEeh8oxBK90qjVPf2xeNdWQBNdBS+unway1Wag8QeUW2pZbGLQczuE83SipNulwCAqA9QQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=gFpycmdh; arc=none smtp.client-ip=209.85.214.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="gFpycmdh" Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-22d95f0dda4so71416025ad.2 for ; Mon, 05 May 2025 14:39:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1746481179; x=1747085979; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=BBkYMTmSl4PimBL1H4hWSadeldBIQV0hA1hHqFFVE6E=; b=gFpycmdh43qMf4a1ik6oAS3vDg61v0R8M/JeDgndL26gLENsgCoK3p747btYo/jxsg 9IqLoBGljp/2IBwFvjaqxBsP6AGJuSF+Y2vCVZvTFHGEqotcI7P68EB8VMcnPQsDTk+y bNRqUw2rXBB+LA4DXrXWXBIzg2AYoq6fvxN/ZKTNDqyVhiKPFX+CtTt0Ulnv8AlSjUsr gSb5oJ5wB8lza+wqkVGi4o89YjUx0Z+U8inLnKgqXhfrjHJVHjz6QVLtTIPL+YUd0sv1 Vxxf59N5CTgJowP9wiD1/sxy0hIyoK59bTaxwigpJSHjG5PatCmsH+XpycB32oj70S+w QvRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746481179; x=1747085979; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=BBkYMTmSl4PimBL1H4hWSadeldBIQV0hA1hHqFFVE6E=; b=mQ03SOciEdS36fSNewCNMePwCy9IHPq4TqhY7BXKxh9DSumf4xn5cWg/D6qaG+5EfM bhsEZvQo2BNW9Eh1MHEVY2F3QYozKnLGHD96Ks76/a9oZjaewb3fjZHnoXITr364Ke4E 1P3rhcvwYBuecps5vpqy7xaFUNhRyEOXZjU0kd+wT1tXhI7nZKPyBiHVtwK6DafXnQGJ XZwkcdCMPS763jH754/u/2CRdhX1OY+rrVe2SdIyz/cd4ygsYc8JHSJzvKPBY/uZs5T6 L5AU06NTiJE5UKjg648Tx9adMTdAXWisO1lLVxG8txH6lD+shoI5cUtA94RSXzzMl943 wNvw== X-Forwarded-Encrypted: i=1; AJvYcCUXkxHlSQ5CR/bYhh4DAHGeRIb/ScrravTnz88P8cI6SBuycY3fBiRU8Lp0Ovt0w/lDw3LKpuZFObx5Z2w=@vger.kernel.org X-Gm-Message-State: AOJu0YxL6DJb4l2RUjG2z+HdK0FEdoIVObs+17T9aCOFBtyUR4FoA7bn 1kfCcs28nTVLiVynCgK9iGHneXaJlI/9c4EYAq50YMdX8+4oktKbaMKSkZmXqTw= X-Gm-Gg: ASbGnct/UnqUcSolz6EqwVxIJ3rqD136ty5do1hZlzf8iGxbINl+D4uaa967up8xdPN GP9vslUqELs2URut7oiqQBIT0//3VlBIlCmu8dG5n3rd6P7/TKhx0zsM5d87Ih4fHJqiib7FtZ7 /myKEP8VMBTVN+xz5EcU3xKJuZ1mKaAsCcxIH7+2enIc4g5+VjTxOfo7fs1zDkLVrnuiWsm5z/S vddVP5rEZYBcawQU99qSVpcq+or9eTUskd9DspTVyPlhp5SoxqIuE7GArtf63pFB59NNfrrmMm4 8PWHLIZHKN6e/hyJPUNVZgUA5DYysug8ov0MWBFSxAClRQPh6oGqbg== X-Google-Smtp-Source: AGHT+IFSjl7ZZTm9sRdhzbga0ISTzjUyoYmypOpH2UHF3JUrUhm15fO1yXOvC7GyT9L7318UQ6VTkw== X-Received: by 2002:a17:902:ecc1:b0:224:c76:5e57 with SMTP id d9443c01a7336-22e3637e732mr7765845ad.39.1746481178982; Mon, 05 May 2025 14:39:38 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-74058df12d8sm7388599b3a.78.2025.05.05.14.39.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 May 2025 14:39:38 -0700 (PDT) From: Atish Patra Date: Mon, 05 May 2025 14:39:28 -0700 Subject: [PATCH 3/5] RISC-V: KVM: Support lazy enabling of siselect and aia bits Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250505-kvm_lazy_enable_stateen-v1-3-3bfc4008373c@rivosinc.com> References: <20250505-kvm_lazy_enable_stateen-v1-0-3bfc4008373c@rivosinc.com> In-Reply-To: <20250505-kvm_lazy_enable_stateen-v1-0-3bfc4008373c@rivosinc.com> To: Anup Patel , Atish Patra , Paul Walmsley , Palmer Dabbelt , Alexandre Ghiti Cc: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Atish Patra X-Mailer: b4 0.15-dev-42535 Smstateen extension controls the SISELECT and SIPH/SIEH register through hstateen.AIA bit (58). Add lazy enabling support for those bits. Signed-off-by: Atish Patra --- arch/riscv/include/asm/kvm_aia.h | 13 ++++++++++++- arch/riscv/kvm/aia.c | 34 ++++++++++++++++++++++++++++++++++ arch/riscv/kvm/vcpu_insn.c | 3 +++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/kvm_aia.h b/arch/riscv/include/asm/kvm_= aia.h index 760a1aef09f7..9e39b0e15169 100644 --- a/arch/riscv/include/asm/kvm_aia.h +++ b/arch/riscv/include/asm/kvm_aia.h @@ -142,12 +142,23 @@ int kvm_riscv_vcpu_aia_rmw_topei(struct kvm_vcpu *vcp= u, unsigned long *val, unsigned long new_val, unsigned long wr_mask); +int kvm_riscv_vcpu_aia_hstateen_enable(struct kvm_vcpu *vcpu, + unsigned int csr_num, unsigned long *val, + unsigned long new_val, unsigned long wr_mask); +int kvm_riscv_vcpu_aia_rmw_isel(struct kvm_vcpu *vcpu, unsigned int csr_nu= m, unsigned long *val, + unsigned long new_val, unsigned long wr_mask); int kvm_riscv_vcpu_aia_rmw_ireg(struct kvm_vcpu *vcpu, unsigned int csr_nu= m, unsigned long *val, unsigned long new_val, unsigned long wr_mask); #define KVM_RISCV_VCPU_AIA_CSR_FUNCS \ { .base =3D CSR_SIREG, .count =3D 1, .func =3D kvm_riscv_vcpu_aia_rmw= _ireg }, \ -{ .base =3D CSR_STOPEI, .count =3D 1, .func =3D kvm_riscv_vcpu_aia_rmw= _topei }, +{ .base =3D CSR_SISELECT, .count =3D 1, .func =3D kvm_riscv_vcpu_aia_rmw= _isel }, \ +{ .base =3D CSR_STOPEI, .count =3D 1, .func =3D kvm_riscv_vcpu_aia_rmw= _topei }, \ +{ .base =3D CSR_STOPI, .count =3D 1, .func =3D kvm_riscv_vcpu_aia_hst= ateen_enable }, \ + +#define KVM_RISCV_VCPU_AIA_CSR_32BIT_FUNCS \ +{ .base =3D CSR_SIPH, .count =3D 1, .func =3D kvm_riscv_vcpu_aia_hstatee= n_enable }, \ +{ .base =3D CSR_SIEH, .count =3D 1, .func =3D kvm_riscv_vcpu_aia_hstatee= n_enable }, \ =20 int kvm_riscv_vcpu_aia_update(struct kvm_vcpu *vcpu); void kvm_riscv_vcpu_aia_reset(struct kvm_vcpu *vcpu); diff --git a/arch/riscv/kvm/aia.c b/arch/riscv/kvm/aia.c index 1e0d2217ade7..3dfabf51a4d2 100644 --- a/arch/riscv/kvm/aia.c +++ b/arch/riscv/kvm/aia.c @@ -235,6 +235,40 @@ int kvm_riscv_vcpu_aia_set_csr(struct kvm_vcpu *vcpu, return 0; } =20 +int kvm_riscv_vcpu_aia_hstateen_enable(struct kvm_vcpu *vcpu, + unsigned int csr_num, + unsigned long *val, + unsigned long new_val, + unsigned long wr_mask) +{ + /* If AIA not available then redirect trap */ + if (!kvm_riscv_aia_available()) + return KVM_INSN_ILLEGAL_TRAP; + + /* If AIA not initialized then forward to user space */ + if (!kvm_riscv_aia_initialized(vcpu->kvm)) + return KVM_INSN_EXIT_TO_USER_SPACE; + + return kvm_riscv_vcpu_hstateen_lazy_enable(vcpu, csr_num, SMSTATEEN0_AIA); +} + +int kvm_riscv_vcpu_aia_rmw_isel(struct kvm_vcpu *vcpu, + unsigned int csr_num, + unsigned long *val, + unsigned long new_val, + unsigned long wr_mask) +{ + /* If AIA not available then redirect trap */ + if (!kvm_riscv_aia_available()) + return KVM_INSN_ILLEGAL_TRAP; + + /* If AIA not initialized then forward to user space */ + if (!kvm_riscv_aia_initialized(vcpu->kvm)) + return KVM_INSN_EXIT_TO_USER_SPACE; + + return kvm_riscv_vcpu_hstateen_lazy_enable(vcpu, csr_num, SMSTATEEN0_AIA_= ISEL); +} + int kvm_riscv_vcpu_aia_rmw_topei(struct kvm_vcpu *vcpu, unsigned int csr_num, unsigned long *val, diff --git a/arch/riscv/kvm/vcpu_insn.c b/arch/riscv/kvm/vcpu_insn.c index 3bc39572b79d..c46907bfe42f 100644 --- a/arch/riscv/kvm/vcpu_insn.c +++ b/arch/riscv/kvm/vcpu_insn.c @@ -260,6 +260,9 @@ static const struct csr_func csr_funcs[] =3D { KVM_RISCV_VCPU_AIA_CSR_FUNCS KVM_RISCV_VCPU_HPMCOUNTER_CSR_FUNCS { .base =3D CSR_SEED, .count =3D 1, .func =3D seed_csr_rmw }, +#ifdef CONFIG_32BIT + KVM_RISCV_VCPU_AIA_CSR_32BIT_FUNCS +#endif }; =20 /** --=20 2.43.0 From nobody Tue Dec 16 07:42:14 2025 Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9A53C2749EB for ; Mon, 5 May 2025 21:39:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746481182; cv=none; b=iMRIa4e8PJYVpK0VcraisdtaGti80V0xkeDlnYh5kzgHmTd0g6L6M8onCv78pHO6+1Zp3J0V+Vg3+P49eIQFacvCFgKZpewOVD4xrgquHzxp/+wPl7RQMw9vWmxnfX39fdO6ojqZw2c5fJAj6Ft7Mk5N6zrFDIdTTi26pLCQV0s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746481182; c=relaxed/simple; bh=hI14ytp30z40KhYvKEMBG0/A96oDveAf8+rhwv3xToI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=NMk85lexrZFFwdJk1KZRv/NSK2/838f8yCCaJW3Tu00cVslkX/3kFIlCF+K/QPEJyN9v5X0tMaPNbyLA4e7PeJl4cHAX4v/IwOpwgU5l0Z5qyI2KMIrD58iIzamWfjpZDAX/RI+46dluj2lKQsSJQFmjo9Ldft9uKwVwm/D5gt8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=qCUBoyOL; arc=none smtp.client-ip=209.85.210.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="qCUBoyOL" Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-74068f95d9fso2139011b3a.0 for ; Mon, 05 May 2025 14:39:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1746481180; x=1747085980; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=zfFMvCaGxkt3NeGE85EC76Mka/3yXyXq5JViUm/gzpM=; b=qCUBoyOLZy88zdxUcCZ78CMtAHNsj7EE2KXOKEns3QpFeJjCas9hkh1E7/+1++VC/5 j59HIUoplA2kD876mFsn5xnuEn/SsX0M/Onha2a+teCfMODM4EcYTnCaeDvAaiYlLMWh W2Pwoe5FQTzsADA2EFDjBUhQTKgJFpgnBG2Ed5qd15LvYhSpcblYPio9Go5K3OXhvPCa fJT6QqPDVEO+SViNaS9vHvaY4nCibC2G3IAiM6yALIt6UaDq6yknK8EWr88ZCrKkAdrN MnBxMo41PP7cwUCrlP48Ugx6kt0Q7378od8cP0kVQALsJI/TOTr1A84KvIpR9RiwJLwu IPCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746481180; x=1747085980; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zfFMvCaGxkt3NeGE85EC76Mka/3yXyXq5JViUm/gzpM=; b=Toxbphner0nm4yAMvUf7HUINERQZr2Kd0angZIXGTIhFAyVA8LC19qCX/u40f5WnDV T/HwZAkpMzhcTENyJaUbF/Sc6HPNAOuDxJPJGnuXU4PupgjK+L1i4J/0BbVyy2iQ+YqF bGOfLxQTOp3GSqn6wDGWfKoXs4a1s7H7GFeL3MJrjhIGvPRx3zQUu3dvSG3GXk4aTPsH J63qBDLV4t6nIzVdOLwM6np6NrYU69zyFsOESYP8NBG38gWrY6vIqAqtemnmGgFw7id8 9QBUlehcCef1w+ZrBUxWztG/eHObTVU2TCU5py/dGRToj38X7dYb+0C8zdV0hivZF41L horw== X-Forwarded-Encrypted: i=1; AJvYcCUSLQxxecT/AtJdLGNvboS89ZHFmymtSI/+Fbban5nNG0HEXHQXc5eB1K6PlMbr8BNaRbVhCpoHEG0gtxo=@vger.kernel.org X-Gm-Message-State: AOJu0YwaU/yrgS3Xl+LwGF/vn+sDfgkfrYq9WkMUlkhnsGxxbfYzDzIP G+g7tNuCG3412rBnbTTxRFZd5zCR4ZKzZ6io21DVOJt1Ite1+R35ne6iGZeLiS+BPI+gC1lIeGq Q X-Gm-Gg: ASbGnctfZGr/necqgxLjjdP3PtAoIuTunbgMZlR8QzD4Go2BS7Mbxs97lK/RSVQdLFC t6hhF3ouaW1ikxDbCoWB4KLLxCS8IVcAqtEYosxzrm6c7VWwGeo5teRxio5IngQzGkFqDhhC7zN nB5S4sxxvu55gltcrf8ZtAvHEuaXTA11WQrSdfFj65oK13j0qV1I02m99aB6MMPTBhR5i5mZajG mmAZASoTFqtjQwggOwEYwog3jpzj5E3UJy+dAzawviceYEw+oBbiezXi3m5lQWPLO7bzSQ2HL5i +zyOu9koWVY5im9pohpmNJbhWiBHFfwJCZ0z5Dfd8vuWu/amopSaLQ== X-Google-Smtp-Source: AGHT+IHaY1/wqoejX+c2irF8o4c25FVRdJ7OcAVhO20N1cFEts/lKwVCx4wh1gNDM2vBI71ScyuV8g== X-Received: by 2002:a05:6a00:2793:b0:73e:10ea:1196 with SMTP id d2e1a72fcca58-740919bcf33mr1106942b3a.8.1746481179845; Mon, 05 May 2025 14:39:39 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-74058df12d8sm7388599b3a.78.2025.05.05.14.39.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 May 2025 14:39:39 -0700 (PDT) From: Atish Patra Date: Mon, 05 May 2025 14:39:29 -0700 Subject: [PATCH 4/5] RISC-V: KVM: Enable envcfg and sstateen bits lazily Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250505-kvm_lazy_enable_stateen-v1-4-3bfc4008373c@rivosinc.com> References: <20250505-kvm_lazy_enable_stateen-v1-0-3bfc4008373c@rivosinc.com> In-Reply-To: <20250505-kvm_lazy_enable_stateen-v1-0-3bfc4008373c@rivosinc.com> To: Anup Patel , Atish Patra , Paul Walmsley , Palmer Dabbelt , Alexandre Ghiti Cc: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Atish Patra X-Mailer: b4 0.15-dev-42535 SENVCFG and SSTATEEN CSRs are controlled by HSENVCFG(62) and SSTATEEN0(63) bits in hstateen. Enable them lazily at runtime instead of bootime. Signed-off-by: Atish Patra --- arch/riscv/kvm/vcpu_insn.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/arch/riscv/kvm/vcpu_insn.c b/arch/riscv/kvm/vcpu_insn.c index c46907bfe42f..ed6302b1992b 100644 --- a/arch/riscv/kvm/vcpu_insn.c +++ b/arch/riscv/kvm/vcpu_insn.c @@ -256,9 +256,37 @@ int kvm_riscv_vcpu_hstateen_lazy_enable(struct kvm_vcp= u *vcpu, unsigned int csr_ return KVM_INSN_CONTINUE_SAME_SEPC; } =20 +static int kvm_riscv_vcpu_hstateen_enable_senvcfg(struct kvm_vcpu *vcpu, + unsigned int csr_num, + unsigned long *val, + unsigned long new_val, + unsigned long wr_mask) +{ + return kvm_riscv_vcpu_hstateen_lazy_enable(vcpu, csr_num, SMSTATEEN0_HSEN= VCFG); +} + +static int kvm_riscv_vcpu_hstateen_enable_stateen(struct kvm_vcpu *vcpu, + unsigned int csr_num, + unsigned long *val, + unsigned long new_val, + unsigned long wr_mask) +{ + const unsigned long *isa =3D vcpu->arch.isa; + + if (riscv_isa_extension_available(isa, SMSTATEEN)) + return kvm_riscv_vcpu_hstateen_lazy_enable(vcpu, csr_num, SMSTATEEN0_SST= ATEEN0); + else + return KVM_INSN_EXIT_TO_USER_SPACE; +} + +#define KVM_RISCV_VCPU_STATEEN_CSR_FUNCS \ +{ .base =3D CSR_SENVCFG, .count =3D 1, .func =3D kvm_riscv_vcpu_hstatee= n_enable_senvcfg }, \ +{ .base =3D CSR_SSTATEEN0, .count =3D 1, .func =3D kvm_riscv_vcpu_hstatee= n_enable_stateen },\ + static const struct csr_func csr_funcs[] =3D { KVM_RISCV_VCPU_AIA_CSR_FUNCS KVM_RISCV_VCPU_HPMCOUNTER_CSR_FUNCS + KVM_RISCV_VCPU_STATEEN_CSR_FUNCS { .base =3D CSR_SEED, .count =3D 1, .func =3D seed_csr_rmw }, #ifdef CONFIG_32BIT KVM_RISCV_VCPU_AIA_CSR_32BIT_FUNCS --=20 2.43.0 From nobody Tue Dec 16 07:42:14 2025 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5D79C2750EE for ; Mon, 5 May 2025 21:39:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746481183; cv=none; b=VgWAhun12RRNjZgnEhuSbA6H/dFFZewi2kAQ0fwUv6E77qImO0ijiebrcXbRPdMIpBUzOc/lQGOYiluOVCk0hJ2t5aJLjOppO0Kwjr0BS7Q2Wi7Siaz4AZ2G9o14PRpeiK1bwt3OJ7UMO9nEzBIKqc5/LwsXVuDP8rB18nSvBm4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746481183; c=relaxed/simple; bh=Pilds4nP9as2uF+L6ux+DctIi0+jR0fPhf8pONUM1gk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=neidJnctNsYvhdGeRxGFPg3dSVJho7LBnDZ76FGHpubNcIfVEzkYnsZhBLb9lAxBmUTBcBlyKUkSbjPpLPaOtBqzQGLTcbJzKs49xEEMJ3/N1mMx+0E/z4F82YwcCHPsFCJ18woKck5SEHai4/U4P1oet3JI6rjumRho3boY5cA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=J8gGG4nM; arc=none smtp.client-ip=209.85.214.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="J8gGG4nM" Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-22401f4d35aso58349605ad.2 for ; Mon, 05 May 2025 14:39:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1746481180; x=1747085980; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=AS5RZ2dErdCUnakbj1eEwsrgZNqL04KOmjC/MeeOuAI=; b=J8gGG4nMH/dUDO6KMjXr7gli7Q6hrv3mRaBQBIegxdPdDqEAtr72Lhy9QfTweVzjYg SqD8ErLRBdKwIe/zDIwogG7cUFAs0zLynT2TXvmsEHhsltIevC74kcq39xanMVGEydMD JqfbvTGtM0lIAMzZQ1eA4ZuLZxbg0d5ek29WzQGN1dpBx0i7SKi0oFdQgtblx+giwZtr Cskf5gexY3MTgFxPqOi5974BNsSHd4s+v0QGR3eREwYmdkkv9toKtC9TXVlRF7F1Qez8 XzuAI4tFBTNU694UV8wuiMLF/O8BP+haTBgqiE+0aLeT32RiHvh8Cti/BEKJlAKSbd9S e2fQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746481180; x=1747085980; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=AS5RZ2dErdCUnakbj1eEwsrgZNqL04KOmjC/MeeOuAI=; b=mlEKGyiohX+oGYHIOsD4n3CO2qtz3NDBo0Cd04pqulPQAk5SJsuNh7ZQ3rt49Lf11w 4u0qWSHyVMZxVzTCFHOIJ9A1M/gBA12OuvPrsPDZHCkQYUehqRs8tjMVLJ1sNOywJeGy oGDwRvcovrMMx6Jkx1CAupxNL6ZssZBGgSpQhNaE/bfLWiNZ63uWbz32nGJgnB0z3581 q80WU4jwlkA7yahMhHlxZQZO4Wm4qFxVaT+YUKWSnLwrROOHvis1TbR4pHSX9SBOEuNb zzg38geV+oXqf1BYEG7/frniUw+PPpTXXMT+vu5XuNdJ8D1Yhd05n94Yd7te5lwGCeJD XcuA== X-Forwarded-Encrypted: i=1; AJvYcCUfKtBssN8ddYvINaRwYRho79Cxd5OzkxoeZduqPpl/AR9LijEdHYPK+w01KvcCqL6nbwVmorRIzI5Ba64=@vger.kernel.org X-Gm-Message-State: AOJu0YyOKVaToYL62Sv1i6SRVgNdPoBwg+VY3bv0vsQLO1yNbtGN8okt ZgQtWZV0FxMdMrlhirTG3FCo9p+E+fZWDHZCJUAuLm3YdSU6ut5cojUhWnrgMdc= X-Gm-Gg: ASbGnctDA341xlg05HyRKqLcrM2aKpFgHCcGCV+TaL8M4G5VYUOBdfBxbCBYOn72AE1 IG570hOpB6OFLsKqqlYCW2w+gidaiBniw8NXHT9scY6MYRbmcN+LA5XMaa57XQ3wCnhaBVkpvXN Z8apzBR7Bd7TywRpTiA/vDvnZHy26/dUoXGa+OJkthlNaPXx8GrqvryBldNu0xoo+OUDih/V1RU MJcstnzfCO0BQB4sPwgQDS7hiZcxcy/ssRRrlc9JpS9vLyuMWQXRuxSUtZFm8fmlXtV2Z2c2YSE KzOrE7RpOGaccI7TXb1HBHagWELmkpris65eIF4Aczlon+LKHCGUCQ== X-Google-Smtp-Source: AGHT+IF0cdGfyegYWS2h3g8K1ylrRO9RH/TBuX+Z6nJcjOdwQsA79eqmv6Tj/3cv8UZxiuHtVwsgFA== X-Received: by 2002:a17:903:22c7:b0:225:abd2:5e39 with SMTP id d9443c01a7336-22e3633a6abmr7949715ad.30.1746481180682; Mon, 05 May 2025 14:39:40 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-74058df12d8sm7388599b3a.78.2025.05.05.14.39.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 May 2025 14:39:40 -0700 (PDT) From: Atish Patra Date: Mon, 05 May 2025 14:39:30 -0700 Subject: [PATCH 5/5] RISC-V: KVM: Remove the boot time enabling of hstateen bits Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250505-kvm_lazy_enable_stateen-v1-5-3bfc4008373c@rivosinc.com> References: <20250505-kvm_lazy_enable_stateen-v1-0-3bfc4008373c@rivosinc.com> In-Reply-To: <20250505-kvm_lazy_enable_stateen-v1-0-3bfc4008373c@rivosinc.com> To: Anup Patel , Atish Patra , Paul Walmsley , Palmer Dabbelt , Alexandre Ghiti Cc: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Atish Patra X-Mailer: b4 0.15-dev-42535 All the existing hstateen bits can be enabled at runtime upon first access now. Remove the default enabling at bootime now. Signed-off-by: Atish Patra --- arch/riscv/kvm/vcpu.c | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index 60d684c76c58..0aaa9e0e4a01 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -560,16 +560,6 @@ static void kvm_riscv_vcpu_setup_config(struct kvm_vcp= u *vcpu) !riscv_isa_extension_available(isa, SVADE)) cfg->henvcfg |=3D ENVCFG_ADUE; =20 - if (riscv_has_extension_unlikely(RISCV_ISA_EXT_SMSTATEEN)) { - cfg->hstateen0 |=3D SMSTATEEN0_HSENVCFG; - if (riscv_isa_extension_available(isa, SSAIA)) - cfg->hstateen0 |=3D SMSTATEEN0_AIA_IMSIC | - SMSTATEEN0_AIA | - SMSTATEEN0_AIA_ISEL; - if (riscv_isa_extension_available(isa, SMSTATEEN)) - cfg->hstateen0 |=3D SMSTATEEN0_SSTATEEN0; - } - cfg->hedeleg =3D KVM_HEDELEG_DEFAULT; if (vcpu->guest_debug) cfg->hedeleg &=3D ~BIT(EXC_BREAKPOINT); --=20 2.43.0