From patchwork Thu Apr 3 11:25:20 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?UmFkaW0gS3LEjW3DocWZ?= X-Patchwork-Id: 14037287 Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) (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 EBB7B24BBF5 for ; Thu, 3 Apr 2025 11:32:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743679936; cv=none; b=Z61jyQhkFby3d+eiJmyjhsHXDVdQhgH41aKS3bfDmmR6lkC3/6uHI6JrVhFDHDrCarAXzil/EpISs66u4klI+IlwasYIKrkUfqsE9aR5QPe3kPffSTpZlG6sRHEhjpTPntC4drHe2Xz70Obh9+BNITIq4dCIhURsC+BxOHQPHig= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743679936; c=relaxed/simple; bh=jffCTzK2ifUcsRuyU60Vh5EyDsYilh1FFdL2XDp/iQo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=NaKULicDTqSIWJ6Ggakra7PhNZ/3mY3Pv1Tfp641f7Op1RDQa9V3dIPCaEnQdQB7NETxSXW8n2EKRm5jys22g/nmfaI2GF4om3WeNvCA94ciebQB4hrepCdiJN5EDHJxkC3E1Duz/W4hLcLSdzQX6QdvWJCenxaOWtrmVE2tseE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ventanamicro.com; spf=pass smtp.mailfrom=ventanamicro.com; dkim=pass (2048-bit key) header.d=ventanamicro.com header.i=@ventanamicro.com header.b=QkYw8+K8; arc=none smtp.client-ip=209.85.128.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ventanamicro.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ventanamicro.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ventanamicro.com header.i=@ventanamicro.com header.b="QkYw8+K8" Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-43d64e6c83eso608555e9.0 for ; Thu, 03 Apr 2025 04:32:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1743679932; x=1744284732; darn=vger.kernel.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=M+bBWXikjKTcV8ccgilNOkXVbdx4GamWUb2bvML3ssQ=; b=QkYw8+K8Y1aOHkQhVnDONrAN5/+jtuBP/vOoVOcOluUnlXSx2nXcK+dr0u9Q0YWrvW 67sdppl95BJWLI6Tw4gVIyx6EDhRyIgN4TKqCvRBlNcabHVpsjhZ5+Tk6NZUHTPTViiY fy92jQwedB2hJ1e292umhzjbdRWI5pM1MGaUfN2Zr55Isrs1BewFInpv69FId6Q9Y/GL L/rLTS7HCiUX5w3c5yvpM4FUEMOOwkIU4paO+QZDPh5q1XwTZiWSR5z6VM5ThxCY1UsD +LEKzvC/RYKFQPRm/JOUP8IWoNEdOq+yyMtubegsxExpxmXoTi4z3JgA/dahzbUV/KEv ehXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743679932; x=1744284732; 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=M+bBWXikjKTcV8ccgilNOkXVbdx4GamWUb2bvML3ssQ=; b=EJfF8nxbI4//tS9FvKqOnQaA2c6JLyYHFYdEPchlterfjXCub/iXBcPd6oPr+wQh8B 7sSE8Q3Be5I3MY5i7ldpqLNAkCCtVOpa4D2rEgXVy8Qc+zuya0e+ODyjOiMNcLxW7O5l MDoUb3cliwGAoTHp+tPVGBQ9i26ChqzUrHtHsJMh4LUWJTFj0ach9Wgy1gY8hTWeabWD diebqP8Z5iJ/5a5wO9hQy7VjYkN0SjnGAahPZ7jNw2klarIMQI0y1iqrbDIyLtR81h1f 7GZPzlXZKEkP2MZ5hlRzdhiM8gws6Vg9vAE44bSD+oS0gK94vqLc1S33xQ3egy/DbZ8I CIAQ== X-Gm-Message-State: AOJu0Yz3F7mBvPSMcwvB5gu3Ti5ku/fs1CKeRVbW8UHq+0q6nxSp1L7R ZBrkPfqOEZIQ9P3dTvx2GEAmhiqRVYWjax03EHVoPQ2kRmAtNt0x2TJUixWmDolAg+BX7AWSV4y N X-Gm-Gg: ASbGncvQxK1aZCdO4bwi0zTf9OsIZj3LdmN7GBJIFyOSl7KMxPotIG/SB3GcLw+Vbva D05DFt+pK+BEBMXVRYHdWrqMy6UGZJQs+WbJ+BedfJ6tkzKrman2aZP2FOsYL+XX0Aja8EfqJ7z Bmc/2yNuRZjv7DIsQ77uXfVRLSQME3MKle98Xva3yqWBzwQq8OdTSKecpYdIdLyQKVEY35sSieO mJ/7kIEe/hmt2xjnQK3J3P8yXNoVznzSG09I5H6FIPTD6f5RWB8stbklhOrLIepTaEdbXrdKhKE zBuu4xV3JCIDeG2aiXksTSxDMZbGj2K8sei6EBMjJaiB4LJILpbnZIT1Rk44j19+6ws7LI5WpQW EpQ== X-Google-Smtp-Source: AGHT+IFKuI/VQYzBaPbrBORZ5Fz1t5HIbnJ+TER1Sbfneisom4goMlCFQ2arAW6gwbRupN7/O0Rx8w== X-Received: by 2002:a05:6000:144a:b0:391:29ab:c9df with SMTP id ffacd0b85a97d-39c2470802cmr3357803f8f.4.1743679932289; Thu, 03 Apr 2025 04:32:12 -0700 (PDT) Received: from localhost (cst2-173-141.cust.vodafone.cz. [31.30.173.141]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43ec366a699sm15541695e9.38.2025.04.03.04.32.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Apr 2025 04:32:11 -0700 (PDT) From: =?utf-8?b?UmFkaW0gS3LEjW3DocWZ?= To: kvm-riscv@lists.infradead.org Cc: kvm@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Anup Patel , Atish Patra , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Andrew Jones , Mayuresh Chitale Subject: [PATCH 1/5] KVM: RISC-V: refactor vector state reset Date: Thu, 3 Apr 2025 13:25:20 +0200 Message-ID: <20250403112522.1566629-4-rkrcmar@ventanamicro.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250403112522.1566629-3-rkrcmar@ventanamicro.com> References: <20250403112522.1566629-3-rkrcmar@ventanamicro.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Do not depend on the reset structures. vector.datap is a kernel memory pointer that needs to be preserved as it is not a part of the guest vector data. Signed-off-by: Radim Krčmář --- arch/riscv/include/asm/kvm_vcpu_vector.h | 6 ++---- arch/riscv/kvm/vcpu.c | 5 ++++- arch/riscv/kvm/vcpu_vector.c | 13 +++++++------ 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/arch/riscv/include/asm/kvm_vcpu_vector.h b/arch/riscv/include/asm/kvm_vcpu_vector.h index 27f5bccdd8b0..57a798a4cb0d 100644 --- a/arch/riscv/include/asm/kvm_vcpu_vector.h +++ b/arch/riscv/include/asm/kvm_vcpu_vector.h @@ -33,8 +33,7 @@ void kvm_riscv_vcpu_guest_vector_restore(struct kvm_cpu_context *cntx, unsigned long *isa); void kvm_riscv_vcpu_host_vector_save(struct kvm_cpu_context *cntx); void kvm_riscv_vcpu_host_vector_restore(struct kvm_cpu_context *cntx); -int kvm_riscv_vcpu_alloc_vector_context(struct kvm_vcpu *vcpu, - struct kvm_cpu_context *cntx); +int kvm_riscv_vcpu_alloc_vector_context(struct kvm_vcpu *vcpu); void kvm_riscv_vcpu_free_vector_context(struct kvm_vcpu *vcpu); #else @@ -62,8 +61,7 @@ static inline void kvm_riscv_vcpu_host_vector_restore(struct kvm_cpu_context *cn { } -static inline int kvm_riscv_vcpu_alloc_vector_context(struct kvm_vcpu *vcpu, - struct kvm_cpu_context *cntx) +static inline int kvm_riscv_vcpu_alloc_vector_context(struct kvm_vcpu *vcpu) { return 0; } diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index 60d684c76c58..2fb75288ecfe 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -57,6 +57,7 @@ static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu) struct kvm_vcpu_csr *reset_csr = &vcpu->arch.guest_reset_csr; struct kvm_cpu_context *cntx = &vcpu->arch.guest_context; struct kvm_cpu_context *reset_cntx = &vcpu->arch.guest_reset_context; + void *vector_datap = cntx->vector.datap; bool loaded; /** @@ -79,6 +80,8 @@ static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu) kvm_riscv_vcpu_fp_reset(vcpu); + /* Restore datap as it's not a part of the guest context. */ + cntx->vector.datap = vector_datap; kvm_riscv_vcpu_vector_reset(vcpu); kvm_riscv_vcpu_timer_reset(vcpu); @@ -143,7 +146,7 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) cntx->hstatus |= HSTATUS_SPV; spin_unlock(&vcpu->arch.reset_cntx_lock); - if (kvm_riscv_vcpu_alloc_vector_context(vcpu, cntx)) + if (kvm_riscv_vcpu_alloc_vector_context(vcpu)) return -ENOMEM; /* By default, make CY, TM, and IR counters accessible in VU mode */ diff --git a/arch/riscv/kvm/vcpu_vector.c b/arch/riscv/kvm/vcpu_vector.c index d92d1348045c..a5f88cb717f3 100644 --- a/arch/riscv/kvm/vcpu_vector.c +++ b/arch/riscv/kvm/vcpu_vector.c @@ -22,6 +22,9 @@ void kvm_riscv_vcpu_vector_reset(struct kvm_vcpu *vcpu) struct kvm_cpu_context *cntx = &vcpu->arch.guest_context; cntx->sstatus &= ~SR_VS; + + cntx->vector.vlenb = riscv_v_vsize / 32; + if (riscv_isa_extension_available(isa, v)) { cntx->sstatus |= SR_VS_INITIAL; WARN_ON(!cntx->vector.datap); @@ -70,13 +73,11 @@ void kvm_riscv_vcpu_host_vector_restore(struct kvm_cpu_context *cntx) __kvm_riscv_vector_restore(cntx); } -int kvm_riscv_vcpu_alloc_vector_context(struct kvm_vcpu *vcpu, - struct kvm_cpu_context *cntx) +int kvm_riscv_vcpu_alloc_vector_context(struct kvm_vcpu *vcpu) { - cntx->vector.datap = kmalloc(riscv_v_vsize, GFP_KERNEL); - if (!cntx->vector.datap) + vcpu->arch.guest_context.vector.datap = kzalloc(riscv_v_vsize, GFP_KERNEL); + if (!vcpu->arch.guest_context.vector.datap) return -ENOMEM; - cntx->vector.vlenb = riscv_v_vsize / 32; vcpu->arch.host_context.vector.datap = kzalloc(riscv_v_vsize, GFP_KERNEL); if (!vcpu->arch.host_context.vector.datap) @@ -87,7 +88,7 @@ int kvm_riscv_vcpu_alloc_vector_context(struct kvm_vcpu *vcpu, void kvm_riscv_vcpu_free_vector_context(struct kvm_vcpu *vcpu) { - kfree(vcpu->arch.guest_reset_context.vector.datap); + kfree(vcpu->arch.guest_context.vector.datap); kfree(vcpu->arch.host_context.vector.datap); } #endif From patchwork Thu Apr 3 11:25:21 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?UmFkaW0gS3LEjW3DocWZ?= X-Patchwork-Id: 14037288 Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) (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 1A7AF5D738 for ; Thu, 3 Apr 2025 11:32:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743679936; cv=none; b=RixYhaxYwRPu6yKum5XtQ81R5eLXYI9899k8dKb8kzT2lmGGi4p6lVR32UIks1e2Rf7/HQJSM1kIPobvvxDi2vku2susNgumM8y//amn8IpNfarmT7HRseu3csJDS2smkOT6Zf413owiiScpaWMbnTvXKmebpDNmVXS+AFu0/fc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743679936; c=relaxed/simple; bh=+GFvOqrtYIALOn93rXdbiqNBHhDWTrxxaHkH9JUVZDM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Z/6SNzNNwlk4cXurUkihlwF2+xavKNqxdGQZblx7LypUgpJLliBu3NihWV3Qalz2IImUNQgONxRv9uE4/pP8DsCWyP4Ain4pwQ4l6i+z9dS18OU/heZdeIQ02l6m6i3LuzJLahDMVnaQm+ggeRs6eO41bassJt3wbfG+1hL/fVg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ventanamicro.com; spf=pass smtp.mailfrom=ventanamicro.com; dkim=pass (2048-bit key) header.d=ventanamicro.com header.i=@ventanamicro.com header.b=dFhVUUPX; arc=none smtp.client-ip=209.85.128.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ventanamicro.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ventanamicro.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ventanamicro.com header.i=@ventanamicro.com header.b="dFhVUUPX" Received: by mail-wm1-f46.google.com with SMTP id 5b1f17b1804b1-43d16a01deaso792705e9.2 for ; Thu, 03 Apr 2025 04:32:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1743679933; x=1744284733; darn=vger.kernel.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=WBJFdG3z/8VkLDYY4XL1oPtAwDow6ej1t0aXVg/kRKM=; b=dFhVUUPX7YI8pZe6UMH/jRIIvPZGFyyY4q/Pkk0tXrS2NI7sA3SFxZzVw/C4APXbKW m6PvLmNosQyT0GOSvq5s46NL+Q3992qxQsLcB2U0Nrewt6xUot9rw7X/BLMGjGNh//sB bL3vXttoaWwE+09Zc5VZpANxHZL8xJjkpTv5u7I4AL+Z/wcn88UwLpO7rts/J64o75UE cfGknoT5dgjVJjFUgWiozKFuWSqvut/vpFDPeKNSus/H4SSHeUWcgYQGTuSmbWHxj0MH jPBg8BQJWcFQWVAZ/yFc/iJb+egXEa7khKD9oP3FT68KMHd0uh0s8ClcTliu+EAmuwBI ZGXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743679933; x=1744284733; 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=WBJFdG3z/8VkLDYY4XL1oPtAwDow6ej1t0aXVg/kRKM=; b=LsZsmH5Gk8TsToX7j/YIar0QSwjQXXDeMIzO1EOaQW0icN8IidljmhEr52GPY1DLnM d5lgLlK9Cs+V0YUC1/1E3pc7aoY7uG+baDnJlUziEoWtuewsZ+VlkzEygp3r8KSFlkWf E/5Waw/iMFMGFjJyBdMfCv5BuLefoCi332kcK1gu/HML6I1s8KshHYZrNHnnLtMIBDwm uIn4naRydDunkIil+2FAgMzGN/htk5UHSngdTOzoNm08EUwQmcTVMLJaiwYDvn6FxNR9 658Q6QplFU6rHSeyM4gB8TxY6GO3k0ksDwqovUCxH0Tuw4GHUKuiZnC65lLQvq0qM4hr RNnw== X-Gm-Message-State: AOJu0YzoJY9c1E1OKgciJWgCDuBKz6wIlD9IT8sR7MnHIeFPrKPNSduE kzqob22EAVR9+9M4gigN/D077/9NXcEiJtQMsPemg8WcxCl8Fr9VN+4aZfyYy/g= X-Gm-Gg: ASbGncsDJZiwxmpSbVtrqg8eNOvC14YZNl9V4lnoAugVKEf6tCzitbhgZvAZo8X1ace /zk4fOpPj5JYH48eCc3hAvzB0yx/cQXHT0maCRswItHUagm3CZ4egZwgUrJ1ip2lpe6ZFkv/8g9 SsLX41d+nsf7QpqjH4V99E5/h/dcn3Q8fEIixATapgLVKqRhd94XpFl1E3aYtgPMH5lRmlLHX4j UBYg6qlaJngs59heQzC0t0a4gtaIjanFHdd6k3F+mXAfzBJdKH/sGFr4L3X6QdbuKd2vitft97H yBYT5B3+PJ94EENzsEUIoUCFjpalShXeTDTnrdRSPv4rqvTvQx3N/5yYGcq0rE53mDJQqCpxz7w mgQ== X-Google-Smtp-Source: AGHT+IEmT1nc0GRaKk4V5Lw6Y+h94knU+oKxuBtmIqWaoagUJp23E2laTFWN8HrLfDImIKVCRKrM7g== X-Received: by 2002:a05:600c:3510:b0:43b:c825:6cde with SMTP id 5b1f17b1804b1-43ea93540demr38321015e9.3.1743679933409; Thu, 03 Apr 2025 04:32:13 -0700 (PDT) Received: from localhost (cst2-173-141.cust.vodafone.cz. [31.30.173.141]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43ec3429f67sm16500145e9.7.2025.04.03.04.32.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Apr 2025 04:32:13 -0700 (PDT) From: =?utf-8?b?UmFkaW0gS3LEjW3DocWZ?= To: kvm-riscv@lists.infradead.org Cc: kvm@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Anup Patel , Atish Patra , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Andrew Jones , Mayuresh Chitale Subject: [PATCH 2/5] KVM: RISC-V: refactor sbi reset request Date: Thu, 3 Apr 2025 13:25:21 +0200 Message-ID: <20250403112522.1566629-5-rkrcmar@ventanamicro.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250403112522.1566629-3-rkrcmar@ventanamicro.com> References: <20250403112522.1566629-3-rkrcmar@ventanamicro.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The same code is used twice and SBI reset sets only two variables. Signed-off-by: Radim Krčmář --- arch/riscv/include/asm/kvm_vcpu_sbi.h | 2 ++ arch/riscv/kvm/vcpu_sbi.c | 12 ++++++++++++ arch/riscv/kvm/vcpu_sbi_hsm.c | 13 +------------ arch/riscv/kvm/vcpu_sbi_system.c | 10 +--------- 4 files changed, 16 insertions(+), 21 deletions(-) diff --git a/arch/riscv/include/asm/kvm_vcpu_sbi.h b/arch/riscv/include/asm/kvm_vcpu_sbi.h index 4ed6203cdd30..aaaa81355276 100644 --- a/arch/riscv/include/asm/kvm_vcpu_sbi.h +++ b/arch/riscv/include/asm/kvm_vcpu_sbi.h @@ -55,6 +55,8 @@ void kvm_riscv_vcpu_sbi_forward(struct kvm_vcpu *vcpu, struct kvm_run *run); void kvm_riscv_vcpu_sbi_system_reset(struct kvm_vcpu *vcpu, struct kvm_run *run, u32 type, u64 flags); +void kvm_riscv_vcpu_sbi_request_reset(struct kvm_vcpu *vcpu, + unsigned long pc, unsigned long a1); int kvm_riscv_vcpu_sbi_return(struct kvm_vcpu *vcpu, struct kvm_run *run); int kvm_riscv_vcpu_set_reg_sbi_ext(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg); diff --git a/arch/riscv/kvm/vcpu_sbi.c b/arch/riscv/kvm/vcpu_sbi.c index d1c83a77735e..f58368f7df1d 100644 --- a/arch/riscv/kvm/vcpu_sbi.c +++ b/arch/riscv/kvm/vcpu_sbi.c @@ -156,6 +156,18 @@ void kvm_riscv_vcpu_sbi_system_reset(struct kvm_vcpu *vcpu, run->exit_reason = KVM_EXIT_SYSTEM_EVENT; } +void kvm_riscv_vcpu_sbi_request_reset(struct kvm_vcpu *vcpu, + unsigned long pc, unsigned long a1) +{ + spin_lock(&vcpu->arch.reset_cntx_lock); + vcpu->arch.guest_reset_context.sepc = pc; + vcpu->arch.guest_reset_context.a0 = vcpu->vcpu_id; + vcpu->arch.guest_reset_context.a1 = a1; + spin_unlock(&vcpu->arch.reset_cntx_lock); + + kvm_make_request(KVM_REQ_VCPU_RESET, vcpu); +} + int kvm_riscv_vcpu_sbi_return(struct kvm_vcpu *vcpu, struct kvm_run *run) { struct kvm_cpu_context *cp = &vcpu->arch.guest_context; diff --git a/arch/riscv/kvm/vcpu_sbi_hsm.c b/arch/riscv/kvm/vcpu_sbi_hsm.c index 3070bb31745d..f26207f84bab 100644 --- a/arch/riscv/kvm/vcpu_sbi_hsm.c +++ b/arch/riscv/kvm/vcpu_sbi_hsm.c @@ -15,7 +15,6 @@ static int kvm_sbi_hsm_vcpu_start(struct kvm_vcpu *vcpu) { - struct kvm_cpu_context *reset_cntx; struct kvm_cpu_context *cp = &vcpu->arch.guest_context; struct kvm_vcpu *target_vcpu; unsigned long target_vcpuid = cp->a0; @@ -32,17 +31,7 @@ static int kvm_sbi_hsm_vcpu_start(struct kvm_vcpu *vcpu) goto out; } - spin_lock(&target_vcpu->arch.reset_cntx_lock); - reset_cntx = &target_vcpu->arch.guest_reset_context; - /* start address */ - reset_cntx->sepc = cp->a1; - /* target vcpu id to start */ - reset_cntx->a0 = target_vcpuid; - /* private data passed from kernel */ - reset_cntx->a1 = cp->a2; - spin_unlock(&target_vcpu->arch.reset_cntx_lock); - - kvm_make_request(KVM_REQ_VCPU_RESET, target_vcpu); + kvm_riscv_vcpu_sbi_request_reset(target_vcpu, cp->a1, cp->a2); __kvm_riscv_vcpu_power_on(target_vcpu); diff --git a/arch/riscv/kvm/vcpu_sbi_system.c b/arch/riscv/kvm/vcpu_sbi_system.c index bc0ebba89003..359be90b0fc5 100644 --- a/arch/riscv/kvm/vcpu_sbi_system.c +++ b/arch/riscv/kvm/vcpu_sbi_system.c @@ -13,7 +13,6 @@ static int kvm_sbi_ext_susp_handler(struct kvm_vcpu *vcpu, struct kvm_run *run, struct kvm_vcpu_sbi_return *retdata) { struct kvm_cpu_context *cp = &vcpu->arch.guest_context; - struct kvm_cpu_context *reset_cntx; unsigned long funcid = cp->a6; unsigned long hva, i; struct kvm_vcpu *tmp; @@ -45,14 +44,7 @@ static int kvm_sbi_ext_susp_handler(struct kvm_vcpu *vcpu, struct kvm_run *run, } } - spin_lock(&vcpu->arch.reset_cntx_lock); - reset_cntx = &vcpu->arch.guest_reset_context; - reset_cntx->sepc = cp->a1; - reset_cntx->a0 = vcpu->vcpu_id; - reset_cntx->a1 = cp->a2; - spin_unlock(&vcpu->arch.reset_cntx_lock); - - kvm_make_request(KVM_REQ_VCPU_RESET, vcpu); + kvm_riscv_vcpu_sbi_request_reset(vcpu, cp->a1, cp->a2); /* userspace provides the suspend implementation */ kvm_riscv_vcpu_sbi_forward(vcpu, run); From patchwork Thu Apr 3 11:25:22 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?UmFkaW0gS3LEjW3DocWZ?= X-Patchwork-Id: 14037289 Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) (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 27ABE24C09C for ; Thu, 3 Apr 2025 11:32:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743679939; cv=none; b=idmMucRqJ+z0ujbYgjO/jfZbyEn7mf4XuNqkZducC1goYV1G98zXnA0ceFlm0uUTO8CkJYSd6KqoBj3hhLzPsGxxL5lkkPZKa3pUH7Uytz9r5rmkpx6BJTHuzJwWiQ7xKG+z9RdubN40h7ouJFsoMG6BYGfWbLJ1+SFD6/8zn1k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743679939; c=relaxed/simple; bh=mPYFlhbqwUJghQgdiJTQpQAylQDiVF48Xvlg8qS+To8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=AzlxtQEfujfFrGO9r9JF+lo9P1YC2alfu9i7ZaD2UKokOmjZ7p1BS3IDInVUqdOD2wF00uNr8UCdmO9s3J6GUD510WvjRaq0xrI4lGR1fDvplW30A3Nl5WQq1wY37bJOn46nlJLxQwtUKDq8/oXZyveV68gyskpSK5+6DNtCyec= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ventanamicro.com; spf=pass smtp.mailfrom=ventanamicro.com; dkim=pass (2048-bit key) header.d=ventanamicro.com header.i=@ventanamicro.com header.b=MohUbOon; arc=none smtp.client-ip=209.85.221.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ventanamicro.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ventanamicro.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ventanamicro.com header.i=@ventanamicro.com header.b="MohUbOon" Received: by mail-wr1-f48.google.com with SMTP id ffacd0b85a97d-39c2688e2bbso49708f8f.1 for ; Thu, 03 Apr 2025 04:32:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1743679934; x=1744284734; darn=vger.kernel.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=oXWumzDe9cV0GiyZzQ4x/1N6jIE102NsKwOmEpuNc8U=; b=MohUbOonnRMhVtguRhvo6fGiyVM1wu0WKomRTg3SlY0rN+YN8S+LNN4ZaJPIyruPeY n0Yc+LfmHIUFtU+r3DqT2lc6dKEBkUijijUNZ43wa2sqxPRpKdkFlf1g75ihR9dqs3cL ZBoqn1HR9t5M1OrusqUz7MBYyKta8t8PGYS9p6yrHG4jRAMCnesr9UTPsRaIP73Ewr+e quTOE6TBVWmL9RqvirmYqJQsj68o7rFXgs1r0R6TvJUbb7S9lhHY0/E1F4HYXl2MuKqp 63kVCScGfSnqGFHYM0cqJoH4bmPk7MYFlRwNvuc2o/P+fY334ZzIosh4nFpUGSx7vMW3 7P9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743679934; x=1744284734; 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=oXWumzDe9cV0GiyZzQ4x/1N6jIE102NsKwOmEpuNc8U=; b=pri21stpstmIvLtEcqIufP2k5vO/W0kcCGkmePItREBh0YApRh9dZ+V7O/ckUX6F8N zvvudneqmXB0XAL2YYAiaITvQHhz3VNgkbel1/dqek0GRHe3oL+qIaQi4yYVKhEbFIdS 15QmS7suaJYxXJNPSmQUnh/gznXo0WdNRBBdIBZcwgzyxuEevGCgTZeWzfBegeVEJfFQ kUujXM30QGNeYSJsbxTzZ/OAJITLOCyt3qh5I6nnxmHj+0AGVvx769u+ScWwgOB7cC8+ fnoDDN2vp6wN1tNzASM9VySB8TKFmuitzE2zd8uKMtHireDQ2uHE78mSD9jA96LNjGCS 9aGw== X-Gm-Message-State: AOJu0YxYkPcubMf/jO9f9bQ+c+mLT3RJkM1phnityZmTKY1uL9N0VTjG NB9XssClDg8PEbvQw5YDnFcc5pjnMFZLj7AFsecxolol7xOxDkQ+uyEcjuLBwhY= X-Gm-Gg: ASbGncsSHX36Bz5y/cFW1JZTYkXx59ZLzYuOLCYWkdDSLpgpTb8fSxBn/iZBzndEuc6 YGpnwK4+3/jpnA8E6m1JCQzdnm8TXQ7/FlzPW9E6RsmYFmqLgqaUze56m+O6874ijesSYNh7LKS bdDSGQqZa1vNy4KPMGSnIvZ8m/a9YILSjfgAmsHBf4s/01SZ+gDFYLBd5+RYXW5aM5YzEesWVXG 2RfF0uC5bJfgsbIpYSVY9Q0LxySBOochqw72mVrqnobbVa3RFA6m3XgL8BdsZ/FW95mhhRBqODY ODxVxiB7EcNnEsxOSzgRqjLPRAChbmDesiMGfr4OaprF5n5vD19HECPeqgdZLlDN7pDt4rgSfc0 3xw== X-Google-Smtp-Source: AGHT+IGYZA79TpGdD3IyE6RBBx1HPlO32wDC9YCrigfm8LcUPmDgc+MGuI/dN03f02oGXug0YpJyDw== X-Received: by 2002:a5d:5f8d:0:b0:386:3a50:8c52 with SMTP id ffacd0b85a97d-39c2470a9a8mr3130677f8f.7.1743679934361; Thu, 03 Apr 2025 04:32:14 -0700 (PDT) Received: from localhost (cst2-173-141.cust.vodafone.cz. [31.30.173.141]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-39c301a7045sm1584106f8f.39.2025.04.03.04.32.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Apr 2025 04:32:14 -0700 (PDT) From: =?utf-8?b?UmFkaW0gS3LEjW3DocWZ?= To: kvm-riscv@lists.infradead.org Cc: kvm@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Anup Patel , Atish Patra , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Andrew Jones , Mayuresh Chitale Subject: [PATCH 3/5] KVM: RISC-V: remove unnecessary SBI reset state Date: Thu, 3 Apr 2025 13:25:22 +0200 Message-ID: <20250403112522.1566629-6-rkrcmar@ventanamicro.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250403112522.1566629-3-rkrcmar@ventanamicro.com> References: <20250403112522.1566629-3-rkrcmar@ventanamicro.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The SBI reset state has only two variables -- pc and a1. The rest is known, so keep only the necessary information. The reset structures make sense if we want userspace to control the reset state (which we do), but I'd still remove them now and reintroduce with the userspace interface later -- we could probably have just a single reset state per VM, instead of a reset state for each VCPU. Signed-off-by: Radim Krčmář --- arch/riscv/include/asm/kvm_aia.h | 3 -- arch/riscv/include/asm/kvm_host.h | 12 ++++--- arch/riscv/kvm/aia_device.c | 4 +-- arch/riscv/kvm/vcpu.c | 58 +++++++++++++++++-------------- arch/riscv/kvm/vcpu_sbi.c | 9 +++-- 5 files changed, 44 insertions(+), 42 deletions(-) diff --git a/arch/riscv/include/asm/kvm_aia.h b/arch/riscv/include/asm/kvm_aia.h index 1f37b600ca47..3b643b9efc07 100644 --- a/arch/riscv/include/asm/kvm_aia.h +++ b/arch/riscv/include/asm/kvm_aia.h @@ -63,9 +63,6 @@ struct kvm_vcpu_aia { /* CPU AIA CSR context of Guest VCPU */ struct kvm_vcpu_aia_csr guest_csr; - /* CPU AIA CSR context upon Guest VCPU reset */ - struct kvm_vcpu_aia_csr guest_reset_csr; - /* Guest physical address of IMSIC for this VCPU */ gpa_t imsic_addr; diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/kvm_host.h index 0e9c2fab6378..0c8c9c05af91 100644 --- a/arch/riscv/include/asm/kvm_host.h +++ b/arch/riscv/include/asm/kvm_host.h @@ -193,6 +193,12 @@ struct kvm_vcpu_smstateen_csr { unsigned long sstateen0; }; +struct kvm_vcpu_reset_state { + spinlock_t lock; + unsigned long pc; + unsigned long a1; +}; + struct kvm_vcpu_arch { /* VCPU ran at least once */ bool ran_atleast_once; @@ -227,12 +233,8 @@ struct kvm_vcpu_arch { /* CPU Smstateen CSR context of Guest VCPU */ struct kvm_vcpu_smstateen_csr smstateen_csr; - /* CPU context upon Guest VCPU reset */ - struct kvm_cpu_context guest_reset_context; - spinlock_t reset_cntx_lock; + struct kvm_vcpu_reset_state reset_state; - /* CPU CSR context upon Guest VCPU reset */ - struct kvm_vcpu_csr guest_reset_csr; /* * VCPU interrupts diff --git a/arch/riscv/kvm/aia_device.c b/arch/riscv/kvm/aia_device.c index 39cd26af5a69..43e472ff3e1a 100644 --- a/arch/riscv/kvm/aia_device.c +++ b/arch/riscv/kvm/aia_device.c @@ -526,12 +526,10 @@ int kvm_riscv_vcpu_aia_update(struct kvm_vcpu *vcpu) void kvm_riscv_vcpu_aia_reset(struct kvm_vcpu *vcpu) { struct kvm_vcpu_aia_csr *csr = &vcpu->arch.aia_context.guest_csr; - struct kvm_vcpu_aia_csr *reset_csr = - &vcpu->arch.aia_context.guest_reset_csr; if (!kvm_riscv_aia_available()) return; - memcpy(csr, reset_csr, sizeof(*csr)); + memset(csr, 0, sizeof(*csr)); /* Proceed only if AIA was initialized successfully */ if (!kvm_riscv_aia_initialized(vcpu->kvm)) diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index 2fb75288ecfe..b8485c1c1ce4 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -51,13 +51,40 @@ const struct kvm_stats_header kvm_vcpu_stats_header = { sizeof(kvm_vcpu_stats_desc), }; -static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu) +static void kvm_riscv_vcpu_context_reset(struct kvm_vcpu *vcpu) { struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr; - struct kvm_vcpu_csr *reset_csr = &vcpu->arch.guest_reset_csr; struct kvm_cpu_context *cntx = &vcpu->arch.guest_context; - struct kvm_cpu_context *reset_cntx = &vcpu->arch.guest_reset_context; + struct kvm_vcpu_reset_state *reset_state = &vcpu->arch.reset_state; void *vector_datap = cntx->vector.datap; + + memset(cntx, 0, sizeof(*cntx)); + memset(csr, 0, sizeof(*csr)); + + /* Restore datap as it's not a part of the guest context. */ + cntx->vector.datap = vector_datap; + + /* Load SBI reset values */ + cntx->a0 = vcpu->vcpu_id; + + spin_lock(&reset_state->lock); + cntx->sepc = reset_state->pc; + cntx->a1 = reset_state->a1; + spin_unlock(&reset_state->lock); + + /* Setup reset state of shadow SSTATUS and HSTATUS CSRs */ + cntx->sstatus = SR_SPP | SR_SPIE; + + cntx->hstatus |= HSTATUS_VTW; + cntx->hstatus |= HSTATUS_SPVP; + cntx->hstatus |= HSTATUS_SPV; + + /* By default, make CY, TM, and IR counters accessible in VU mode */ + csr->scounteren = 0x7; +} + +static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu) +{ bool loaded; /** @@ -72,16 +99,10 @@ static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu) vcpu->arch.last_exit_cpu = -1; - memcpy(csr, reset_csr, sizeof(*csr)); - - spin_lock(&vcpu->arch.reset_cntx_lock); - memcpy(cntx, reset_cntx, sizeof(*cntx)); - spin_unlock(&vcpu->arch.reset_cntx_lock); + kvm_riscv_vcpu_context_reset(vcpu); kvm_riscv_vcpu_fp_reset(vcpu); - /* Restore datap as it's not a part of the guest context. */ - cntx->vector.datap = vector_datap; kvm_riscv_vcpu_vector_reset(vcpu); kvm_riscv_vcpu_timer_reset(vcpu); @@ -113,8 +134,6 @@ int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) { int rc; - struct kvm_cpu_context *cntx; - struct kvm_vcpu_csr *reset_csr = &vcpu->arch.guest_reset_csr; spin_lock_init(&vcpu->arch.mp_state_lock); @@ -134,24 +153,11 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) /* Setup VCPU hfence queue */ spin_lock_init(&vcpu->arch.hfence_lock); - /* Setup reset state of shadow SSTATUS and HSTATUS CSRs */ - spin_lock_init(&vcpu->arch.reset_cntx_lock); - - spin_lock(&vcpu->arch.reset_cntx_lock); - cntx = &vcpu->arch.guest_reset_context; - cntx->sstatus = SR_SPP | SR_SPIE; - cntx->hstatus = 0; - cntx->hstatus |= HSTATUS_VTW; - cntx->hstatus |= HSTATUS_SPVP; - cntx->hstatus |= HSTATUS_SPV; - spin_unlock(&vcpu->arch.reset_cntx_lock); + spin_lock_init(&vcpu->arch.reset_state.lock); if (kvm_riscv_vcpu_alloc_vector_context(vcpu)) return -ENOMEM; - /* By default, make CY, TM, and IR counters accessible in VU mode */ - reset_csr->scounteren = 0x7; - /* Setup VCPU timer */ kvm_riscv_vcpu_timer_init(vcpu); diff --git a/arch/riscv/kvm/vcpu_sbi.c b/arch/riscv/kvm/vcpu_sbi.c index f58368f7df1d..3d7955e05cc3 100644 --- a/arch/riscv/kvm/vcpu_sbi.c +++ b/arch/riscv/kvm/vcpu_sbi.c @@ -159,11 +159,10 @@ void kvm_riscv_vcpu_sbi_system_reset(struct kvm_vcpu *vcpu, void kvm_riscv_vcpu_sbi_request_reset(struct kvm_vcpu *vcpu, unsigned long pc, unsigned long a1) { - spin_lock(&vcpu->arch.reset_cntx_lock); - vcpu->arch.guest_reset_context.sepc = pc; - vcpu->arch.guest_reset_context.a0 = vcpu->vcpu_id; - vcpu->arch.guest_reset_context.a1 = a1; - spin_unlock(&vcpu->arch.reset_cntx_lock); + spin_lock(&vcpu->arch.reset_state.lock); + vcpu->arch.reset_state.pc = pc; + vcpu->arch.reset_state.a1 = a1; + spin_unlock(&vcpu->arch.reset_state.lock); kvm_make_request(KVM_REQ_VCPU_RESET, vcpu); } From patchwork Thu Apr 3 11:25:23 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?UmFkaW0gS3LEjW3DocWZ?= X-Patchwork-Id: 14037291 Received: from mail-wr1-f54.google.com (mail-wr1-f54.google.com [209.85.221.54]) (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 8828524DFFA for ; Thu, 3 Apr 2025 11:32:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743679941; cv=none; b=KMtbarcHeeFD/ttB/suefv2meBv/0qH3FoOhMuxyI25pMwuvZ8Mjc/tnw1GdixVLWVCjJGIip3hupsWu+SR70G2fvkttO8R5yHMJ8qbV++mpcw/BTMKjhMXuuqt6ZNLkGJlWvUAN5VttldaaMcb4S3wikstO2wDPksVwXjXV4NE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743679941; c=relaxed/simple; bh=Azh59wZxXw0yOcy654yOp1KBuheFNdxSily/oCO2cqE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=gzaE9BacNXRnmvxU5QzXNlrVaMkF5Yj43vwFhaC+fGUVPg3aV8sRMVF/m4B3IqRGiH3ghklUx7fRpUtSSjsiefzcbXPuQ/F70TlKw7bqpXHMSTRF5sOD8bDEd4y22S5gDM8iBAwziicz6STXf84Yau/oARy0Tezhv/b7UnJKd4U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ventanamicro.com; spf=pass smtp.mailfrom=ventanamicro.com; dkim=pass (2048-bit key) header.d=ventanamicro.com header.i=@ventanamicro.com header.b=TcrRN4mY; arc=none smtp.client-ip=209.85.221.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ventanamicro.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ventanamicro.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ventanamicro.com header.i=@ventanamicro.com header.b="TcrRN4mY" Received: by mail-wr1-f54.google.com with SMTP id ffacd0b85a97d-3913290f754so61887f8f.1 for ; Thu, 03 Apr 2025 04:32:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1743679936; x=1744284736; darn=vger.kernel.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=v4I41IipITScJpjVRL6TprMAJ/XjTheyblDpzSa8c88=; b=TcrRN4mYeUgNsDn/+x9gXg2oR+Oz5B25LI6av3I7R6/E8fY28yeVrv3Xg9pjJZtaO8 FFQp6GdInoGSj4kqFMpdCXae9zqQuy+P+e5Ru8kGg+oFCq1pb30IpYYTD2L3DJsNvzO0 Y5Ph69Q7IGlu0dqmx4FZtWQUesRBJoGMDFeVXhVTWyeXGvooTvimCAmDY9vbLQEbBaKL mnb0WjJEN/ag9xoP4/jS8FH9SqIYXIC1A44D6mku0LA8EwHLTvDovvx6a4jR3FO5Q4IK 3iGvWaU5so2DofrVXZT186ByU9IrsI5GO+sstW/qjly4WdSOukdkh6+q1PEBmHPGT7J+ f7aQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743679936; x=1744284736; 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=v4I41IipITScJpjVRL6TprMAJ/XjTheyblDpzSa8c88=; b=gPOT9Jefm4ItP4jS/Y4OGxVGWx5PAR8vQIiVuKzm7Sbblct2+0+JStrG3Nitul+km2 nGi5f+8JLcdPkxiOJ9wP0Lw+X2W7CQlYSCFsYSsX8NrojDRK7fFVMquMjeF8jiqLKqNM pmBSxhCigasGg4kSeoySj2IkqMSdp0dbXCzLbCT87PNjYAb4WQSLSsa0dreuILg6bau6 UfSkdngYqI5QBlp+Zl2EVg2K6NRn5SgYhhKwaA8JoLWj7potM8mzx1TuOewOII+y8u6D VRuOQcIoWKA3T9pBCiV90WTGga2C1iaXQfytJRUpGt2eD5a2s3BK9KKjxa68aB7lflKJ OIxQ== X-Gm-Message-State: AOJu0YwWFl9SepRt5h7ehvY2Sijbp6DAhntZuLII1D5TBYakg3TgCzOz mU6U5ptBPI5s8zsTzbn4dxwvl/9otFEofbKpMbvmSnpUIl3TE9obz0Pd61gHjQA= X-Gm-Gg: ASbGncuAF0li4zSF6LMfcefDWxe1Yreh/vfoJ3jVLCsdluUFI3Kv7S4il4OVqyQ7edJ WeFLaTeL9l4njYz7f45Sa8dwEjL4Jz6C2zQ/glxFvNrWt1Iv26/zpiLAvf0aYW6MU/4U4D74zog hTfA7BwGxfiGjCCqo7jXJ4xfzkN3xsd/uLv9ggimoLPVRDnKolXB0Ot9BxkU3U8K8Fo4aLXQMNb za5epclPJHzs3/a/YiAVCjnQhcNE0qXh2J+2kPIvD3Yes8yzKeoE9o5jWWcDQN4GmD3FEXESD9M TwDRVDBDMym0v9jyH4h+I3C3qeZRUC7dqH4S1zFQ1H2GGSuF9jXFpk/4yLuDTlZqXL0oYYkDLZs vcQ== X-Google-Smtp-Source: AGHT+IG37Xf9B62VUcljGsX2nc8emFPUfOIiPgop6kaEA0XU1nktX/EDvfLlLGCqE6Qni7LwKqU1Ng== X-Received: by 2002:a5d:59ad:0:b0:39c:1258:17d5 with SMTP id ffacd0b85a97d-39c2483abcfmr3137958f8f.14.1743679935688; Thu, 03 Apr 2025 04:32:15 -0700 (PDT) Received: from localhost (cst2-173-141.cust.vodafone.cz. [31.30.173.141]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-39c3020d6b1sm1575928f8f.62.2025.04.03.04.32.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Apr 2025 04:32:14 -0700 (PDT) From: =?utf-8?b?UmFkaW0gS3LEjW3DocWZ?= To: kvm-riscv@lists.infradead.org Cc: kvm@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Anup Patel , Atish Patra , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Andrew Jones , Mayuresh Chitale Subject: [PATCH 4/5] KVM: RISC-V: reset VCPU state when becoming runnable Date: Thu, 3 Apr 2025 13:25:23 +0200 Message-ID: <20250403112522.1566629-7-rkrcmar@ventanamicro.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250403112522.1566629-3-rkrcmar@ventanamicro.com> References: <20250403112522.1566629-3-rkrcmar@ventanamicro.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Beware, this patch is "breaking" the userspace interface, because it fixes a KVM/QEMU bug where the boot VCPU is not being reset by KVM. The VCPU reset paths are inconsistent right now. KVM resets VCPUs that are brought up by KVM-accelerated SBI calls, but does nothing for VCPUs brought up through ioctls. We need to perform a KVM reset even when the VCPU is started through an ioctl. This patch is one of the ways we can achieve it. Assume that userspace has no business setting the post-reset state. KVM is de-facto the SBI implementation, as the SBI HSM acceleration cannot be disabled and userspace cannot control the reset state, so KVM should be in full control of the post-reset state. Do not reset the pc and a1 registers, because SBI reset is expected to provide them and KVM has no idea what these registers should be -- only the userspace knows where it put the data. An important consideration is resume. Userspace might want to start with non-reset state. Check ran_atleast_once to allow this, because KVM-SBI HSM creates some VCPUs as STOPPED. The drawback is that userspace can still start the boot VCPU with an incorrect reset state, because there is no way to distinguish a freshly reset new VCPU on the KVM side (userspace might set some values by mistake) from a restored VCPU (userspace must set all values). The advantage of this solution is that it fixes current QEMU and makes some sense with the assumption that KVM implements SBI HSM. I do not like it too much, so I'd be in favor of a different solution if we can still afford to drop support for current userspaces. For a cleaner solution, we should add interfaces to perform the KVM-SBI reset request on userspace demand. I think it would also be much better if userspace was in control of the post-reset state. Signed-off-by: Radim Krčmář --- arch/riscv/include/asm/kvm_host.h | 1 + arch/riscv/include/asm/kvm_vcpu_sbi.h | 3 +++ arch/riscv/kvm/vcpu.c | 9 +++++++++ arch/riscv/kvm/vcpu_sbi.c | 21 +++++++++++++++++++-- 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/kvm_host.h index 0c8c9c05af91..9bbf8c4a286b 100644 --- a/arch/riscv/include/asm/kvm_host.h +++ b/arch/riscv/include/asm/kvm_host.h @@ -195,6 +195,7 @@ struct kvm_vcpu_smstateen_csr { struct kvm_vcpu_reset_state { spinlock_t lock; + bool active; unsigned long pc; unsigned long a1; }; diff --git a/arch/riscv/include/asm/kvm_vcpu_sbi.h b/arch/riscv/include/asm/kvm_vcpu_sbi.h index aaaa81355276..2c334a87e02a 100644 --- a/arch/riscv/include/asm/kvm_vcpu_sbi.h +++ b/arch/riscv/include/asm/kvm_vcpu_sbi.h @@ -57,6 +57,9 @@ void kvm_riscv_vcpu_sbi_system_reset(struct kvm_vcpu *vcpu, u32 type, u64 flags); void kvm_riscv_vcpu_sbi_request_reset(struct kvm_vcpu *vcpu, unsigned long pc, unsigned long a1); +void __kvm_riscv_vcpu_set_reset_state(struct kvm_vcpu *vcpu, + unsigned long pc, unsigned long a1); +void kvm_riscv_vcpu_sbi_request_reset_from_userspace(struct kvm_vcpu *vcpu); int kvm_riscv_vcpu_sbi_return(struct kvm_vcpu *vcpu, struct kvm_run *run); int kvm_riscv_vcpu_set_reg_sbi_ext(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg); diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index b8485c1c1ce4..4578863a39e3 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -58,6 +58,11 @@ static void kvm_riscv_vcpu_context_reset(struct kvm_vcpu *vcpu) struct kvm_vcpu_reset_state *reset_state = &vcpu->arch.reset_state; void *vector_datap = cntx->vector.datap; + spin_lock(&reset_state->lock); + if (!reset_state->active) + __kvm_riscv_vcpu_set_reset_state(vcpu, cntx->sepc, cntx->a1); + spin_unlock(&reset_state->lock); + memset(cntx, 0, sizeof(*cntx)); memset(csr, 0, sizeof(*csr)); @@ -520,6 +525,10 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, switch (mp_state->mp_state) { case KVM_MP_STATE_RUNNABLE: + if (riscv_vcpu_supports_sbi_ext(vcpu, KVM_RISCV_SBI_EXT_HSM) && + vcpu->arch.ran_atleast_once && + kvm_riscv_vcpu_stopped(vcpu)) + kvm_riscv_vcpu_sbi_request_reset_from_userspace(vcpu); WRITE_ONCE(vcpu->arch.mp_state, *mp_state); break; case KVM_MP_STATE_STOPPED: diff --git a/arch/riscv/kvm/vcpu_sbi.c b/arch/riscv/kvm/vcpu_sbi.c index 3d7955e05cc3..77f9f0bd3842 100644 --- a/arch/riscv/kvm/vcpu_sbi.c +++ b/arch/riscv/kvm/vcpu_sbi.c @@ -156,12 +156,29 @@ void kvm_riscv_vcpu_sbi_system_reset(struct kvm_vcpu *vcpu, run->exit_reason = KVM_EXIT_SYSTEM_EVENT; } +/* must be called with held vcpu->arch.reset_state.lock */ +void __kvm_riscv_vcpu_set_reset_state(struct kvm_vcpu *vcpu, + unsigned long pc, unsigned long a1) +{ + vcpu->arch.reset_state.active = true; + vcpu->arch.reset_state.pc = pc; + vcpu->arch.reset_state.a1 = a1; +} + void kvm_riscv_vcpu_sbi_request_reset(struct kvm_vcpu *vcpu, unsigned long pc, unsigned long a1) { spin_lock(&vcpu->arch.reset_state.lock); - vcpu->arch.reset_state.pc = pc; - vcpu->arch.reset_state.a1 = a1; + __kvm_riscv_vcpu_set_reset_state(vcpu, pc, a1); + spin_unlock(&vcpu->arch.reset_state.lock); + + kvm_make_request(KVM_REQ_VCPU_RESET, vcpu); +} + +void kvm_riscv_vcpu_sbi_request_reset_from_userspace(struct kvm_vcpu *vcpu) +{ + spin_lock(&vcpu->arch.reset_state.lock); + vcpu->arch.reset_state.active = false; spin_unlock(&vcpu->arch.reset_state.lock); kvm_make_request(KVM_REQ_VCPU_RESET, vcpu); From patchwork Thu Apr 3 11:25:24 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?UmFkaW0gS3LEjW3DocWZ?= X-Patchwork-Id: 14037290 Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (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 9006F24E01B for ; Thu, 3 Apr 2025 11:32:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743679941; cv=none; b=G7WhYZebO60eMXY885PMxF4uia7o/8GKzveiK/4gkSZFIwcvOEvGcwtp//DQESWjypCfgEpOv6WTxjrN+1+09TJ5wt1Tc8uVyJlivJQYeLZELWhuzEBAzIlW+L7A/nOBrpixdKGk+uZIcTA6C5UFwxBnSqHCq4Xr8f71uOb1D40= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743679941; c=relaxed/simple; bh=WO1z1H1Mibkl/wzBNl7Xjb98G13d2MySStzMJVGKot4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=CnEq6C+AuNLsL0uBjm/Hlnr75FMTTZXS4IQrikJPb7OYGMQu1YSfHYmlLxS4jZDpzxdO9haJ68I1Gl+va1dm4X6wGE6vGQk2gwNbvmA8NPTVoC4NsrVOkMnVmggOGJRElV2dEYOu+AWeSkHVNq0u4Ckwi7ZXhx8dHqu7oVVzNV8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ventanamicro.com; spf=pass smtp.mailfrom=ventanamicro.com; dkim=pass (2048-bit key) header.d=ventanamicro.com header.i=@ventanamicro.com header.b=f/LkW38F; arc=none smtp.client-ip=209.85.128.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ventanamicro.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ventanamicro.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ventanamicro.com header.i=@ventanamicro.com header.b="f/LkW38F" Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-43cfe99f2a7so257185e9.2 for ; Thu, 03 Apr 2025 04:32:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1743679937; x=1744284737; darn=vger.kernel.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=oR6PqtGhCEjz92xi+hsij6mLCExpfkTVXIEuhpc1C/o=; b=f/LkW38FRYON0y8kpjoZFjil6pP8fLd8oniJFXr1bEkEpq6WdN5ZzbcoV1aXz9HSyY JrEF9Exkrk68qlkGIGDJr57o7qmz56uG3momgxS2Qi7l/OahGTE03F3Sjz53Q+rTogbv X96N1l4uYRUoDk7boqGB2s+iUkyG8Gh64Fty2fDMCZ9gravHeyPgzLikaNP1qlhkIP3B xsU9NCSEmMyoakNAIHfobWwT0x7pfDYrtuqDQwuqmLxW+Ve7TfmOor06AYuDTsTG7RJP OVH2d/sEvVztIsdsWaO42kobRsJV24OnzaZNGF1HWCJfFrZ4M1SbBSCoN2rvQnbPlTtV dfZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743679937; x=1744284737; 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=oR6PqtGhCEjz92xi+hsij6mLCExpfkTVXIEuhpc1C/o=; b=smYFM+JEI90X9MFEEJ0tzGa7AdX/+Q3e+KqU5V10blky+SaKRQNvKJS6ozB3mKfLZz HHTBzmDqKnt7aLJAEgB7dAYitERmeX33sB1jIu5eSOcprd6GmGk1pyUCRod2XlQJsrnw VRS2qDuComC2c+inkYIi8ZAJUuMNx5wo5pDIFdi2xLYm16lA1J2VO6wHIsBQwx+6iKfa 5mt/9gNlnB3O0MIeALEMYPNh8SIvNCzFOUSFQ5/0lnqsPk6p4FNhMHSNpZmOy4Wqrx1p augNkk7m5WafePiIl5z370iRdvJit1z/iz7GpB2JA9hpcKza8imb3MXnqksux1A+PaSG m3rg== X-Gm-Message-State: AOJu0Yw8f30dOXszoLDFBKnfpJEEAntw/dWHY1MedD/YT9MDIYTNKiHC yzPuX/oXW9FYnvHt9XJjbVWT38tqKuoJyLTrFLvx6e4xldVDPtVoDkSk8cyG+Is= X-Gm-Gg: ASbGncuHcihv27hbQsmeZ2CthBr40OaNaNvo1Ga1/1Yp1BEOaCTRlOCe4oXxsk9En8i /MdM2Lmi6dWuJGTS0CIqCGHgoiSnbO85rhe03CvBzrMt5gTLDJV9r/bpHZ+HySScOc0e8m36F6M +K0wgxczsR57z5+beMykpVT95yTeCF4zz8v3XYQnmci0UlAH7FkoXGPdYM+49dHd3b8sZqeAgTC KSNQAZK9LDTsUtaidsUyT03JYTwdKs+Fm2NqmsL2vnsA10T+0m9lu9ID+CKPZEsNyPacwMDIanA 5UM8mM66FnYt4VdmMoaB3Cw6y4eSDFANUNGClKO+z5C0a5z9mnOnXFucwpTFn50ig+/6LDuvMKJ BHw== X-Google-Smtp-Source: AGHT+IFLuPHIJtkY001V8NzGfGDvy4KWQWKKGAIqS8lFxrZ1d/GjyGP4Yf4pmkBcRsFqmKnj2gYoIw== X-Received: by 2002:a05:600c:1d12:b0:439:8c80:6aee with SMTP id 5b1f17b1804b1-43db62bc0femr69162255e9.4.1743679936757; Thu, 03 Apr 2025 04:32:16 -0700 (PDT) Received: from localhost (cst2-173-141.cust.vodafone.cz. [31.30.173.141]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43ec169b4e4sm19424775e9.20.2025.04.03.04.32.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Apr 2025 04:32:16 -0700 (PDT) From: =?utf-8?b?UmFkaW0gS3LEjW3DocWZ?= To: kvm-riscv@lists.infradead.org Cc: kvm@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Anup Patel , Atish Patra , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Andrew Jones , Mayuresh Chitale Subject: [PATCH 5/5] KVM: RISC-V: reset smstateen CSRs Date: Thu, 3 Apr 2025 13:25:24 +0200 Message-ID: <20250403112522.1566629-8-rkrcmar@ventanamicro.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250403112522.1566629-3-rkrcmar@ventanamicro.com> References: <20250403112522.1566629-3-rkrcmar@ventanamicro.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Not resetting smstateen is a potential security hole, because VU might be able to access state that VS does not properly context-switch. Fixes: 81f0f314fec9 ("RISCV: KVM: Add sstateen0 context save/restore") Signed-off-by: Radim Krčmář --- arch/riscv/kvm/vcpu.c | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index 4578863a39e3..ac0fa50bc489 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -65,6 +65,7 @@ static void kvm_riscv_vcpu_context_reset(struct kvm_vcpu *vcpu) memset(cntx, 0, sizeof(*cntx)); memset(csr, 0, sizeof(*csr)); + memset(&vcpu->arch.smstateen_csr, 0, sizeof(vcpu->arch.smstateen_csr)); /* Restore datap as it's not a part of the guest context. */ cntx->vector.datap = vector_datap;