From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.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 21329344D9D for ; Fri, 19 Jun 2026 13:29:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.54 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781875767; cv=none; b=jwOUStTVpFljTklVUB2Kqydu0vHWg1AqCgVBLR7qQS149hR/h7V1m+s5IccmNrKvtM5dU2TqjxpbxszbSKN4A1Q7O7bbzHpSe99rYd+xF91aMB0kVcxqwWNDzQfN7ANsidx+HHyw9vVQ7dIwVS/eT8Kd71jIqBqYNv4l2w+hwRI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781875767; c=relaxed/simple; bh=qNP/Ei+oEPWRuZHcFwFEMNs87Uh1QhIHpyjK1WqcACM=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=L3FstefnuP4qrgD+TcEhCCiPIEB7n9JxnxLA50clWW6UNofx8FwtU9p3DyttCjuDlm9dLYqdZohZCJQFgmJp5E4PuzfGcuMG3l416KuJwkfGTY270ltoFn3pxDt4z/0+tvtIhTp8U4m3O3/kh652w0Qxm4vI7IMACte3w6YN3k0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=AySqkQ4t; arc=none smtp.client-ip=209.85.128.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="AySqkQ4t" Received: by mail-wm1-f54.google.com with SMTP id 5b1f17b1804b1-490b64c8311so19844565e9.3 for ; Fri, 19 Jun 2026 06:29:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1781875764; x=1782480564; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=DFtqEUJwr/FxLj+oB4yh/uuwM4uyDihUAVSs/6dx5t0=; b=AySqkQ4tk7bxpGfgrNjAPkxOBUVaBax9ng/B7UiVxD66q4pIUr5Swl7RplhInmglTc DUEOj6HKsZ2dQKgb0vMaXse2e5uwzzJrlVGV2IwlwhclGOv3UaSzjWWYf6CP0Qttv42L vYU45JStAvZTu24TBWimeuBwPQ1B6qosTRrBlvMqdOvWwBkHqQIEvVsFDpoBxN+/fqUC Sv5NHD6KgWrnhCnzjJ3YSyAF9dwVmr5cVYRjOFSJqjOX1v+v8PBr4EJDRdtz2cU/johs oLz1SgENIYRw+kPs4JzGpwSIdbmnw2IvMRBb/QJX05iG/g1l18xC3uMdcfMTPQAg+tfe PnwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781875764; x=1782480564; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DFtqEUJwr/FxLj+oB4yh/uuwM4uyDihUAVSs/6dx5t0=; b=o0C4LMLRzTCeS5lmPHZcrrZC297P4gtAlOmRy6Yq/qrVGszhfTm4RvaZjVT2yobdU9 8sb9adblW+i0zFTH2Vdv0FqXkLA7q9pHCwbq50j0odusq2EgTuHc4zQ6z8Al+G9JLMJG HvLlj7gnDwNSV7vPgQrJQ9T94QflLf8GrKjCwuZtDd3UrCCEQkTGZetwEF8s1i3uDdr2 EbHaj/hUqXr0UlVrop88tUL3YjX/kP7hW2isKdXfaOj08uqviiMGd/NvAGeixSfsUenm aEWGDrxF44lyTGEt4PCxYK8nwkjtudpIORa+jh07v6Oc8k3pQ0n2jGxuX6KSXCTbqsxb u0Kg== X-Forwarded-Encrypted: i=1; AFNElJ+sjnsgKLl5pMMc8RDNEUKsC+zEO7FgCVyVSAJts23tc0DKEuVm7hOIPn/zP6UV3RbK6WlJaNccQNcrPB8=@vger.kernel.org X-Gm-Message-State: AOJu0YwAUzhEOcFQAu9+JQ/NJdDxFmqXoYgBoXVzBrpB6bGUEunlPm6d 7/D3RtfK4j1JdpEfMKlT5z0oks/ehlHL0bYLFtOklSN5AXOGhPasDz132ue+37cu3g== X-Gm-Gg: AfdE7cl+wMTMihayBmZNGqLOZOKb/UYej7T5YZBiFz/2rLgER2UvQhXsUP7b6XJTqdf +2rzH9PBY1uBVQAIxlLWGvHhY1T06KRm4MyDbQCw7vQBeUzcXY6GlKeB6j9T/9fnJ0qFa86Ucvh 9Gx+gKp5qECIqSM/CDhYKTyIx/m6SoCp8O/dlVy+DGd2pPlrvdvoFsEVAIRmqDnwum+U1qb7V2U vW0JnR0g+78b4mg/eNm48pxyMz7hNY8yidJdjLzwDIVJQquAJH8k2isTV1UtRZHibZFEIAZItiR kXTdZi38jjpWaYUXkZgg1OYKBXIudCjGvEn+4jk2LzaGCGKoOzj/w3Oi+pgRwxtVTU6oQxEzvDG Z1RMg/3NpdieyvJshmuS9el6C1rYlvygZzThTaWBLC3XVMwXyES/qHgjjz2aHZOVylD9Dje+z85 tVxZPygIxF5iaPY49Fr3MDtvy6i0EO0/DSVjxCSsQCmSTQN+Cy+NnGoX1H X-Received: by 2002:a05:600c:203:b0:490:c7dd:7cc2 with SMTP id 5b1f17b1804b1-49240e61101mr42977915e9.24.1781875763891; Fri, 19 Jun 2026 06:29:23 -0700 (PDT) Received: from google.com (135.91.155.104.bc.googleusercontent.com. [104.155.91.135]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4650c11693dsm8318152f8f.37.2026.06.19.06.29.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jun 2026 06:29:22 -0700 (PDT) Date: Fri, 19 Jun 2026 14:29:18 +0100 From: Vincent Donnefort To: Fuad Tabba Cc: Marc Zyngier , Oliver Upton , kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Catalin Marinas , Will Deacon , Joey Gouly , Steffen Eiden , Suzuki K Poulose , Zenghui Yu , Quentin Perret , Sebastian Ene , Hyunwoo Kim Subject: Re: [PATCH v2 3/8] KVM: arm64: Factor out reusable vCPU reset helpers Message-ID: References: <20260619070719.812227-1-tabba@google.com> <20260619070719.812227-4-tabba@google.com> 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=us-ascii Content-Disposition: inline In-Reply-To: <20260619070719.812227-4-tabba@google.com> On Fri, Jun 19, 2026 at 08:07:14AM +0100, Fuad Tabba wrote: > Pull the reusable pieces out of kvm_reset_vcpu(): expose the reset > PSTATE values in kvm_arm.h, and split the core register reset and the > PSCI-driven reset into kvm_reset_vcpu_core() and kvm_reset_vcpu_psci(). > A follow-up series reuses these to reset protected vCPUs at EL2. > > No functional change intended. > > Signed-off-by: Fuad Tabba Reviewed-by: Vincent Donnefort > --- > arch/arm64/include/asm/kvm_arm.h | 12 ++++++ > arch/arm64/include/asm/kvm_emulate.h | 57 ++++++++++++++++++++++++++ > arch/arm64/kvm/reset.c | 60 ++-------------------------- > 3 files changed, 72 insertions(+), 57 deletions(-) > > diff --git a/arch/arm64/include/asm/kvm_arm.h b/arch/arm64/include/asm/kvm_arm.h > index 3f9233b5a130..aba4ec09acd2 100644 > --- a/arch/arm64/include/asm/kvm_arm.h > +++ b/arch/arm64/include/asm/kvm_arm.h > @@ -348,4 +348,16 @@ > { PSR_AA32_MODE_UND, "32-bit UND" }, \ > { PSR_AA32_MODE_SYS, "32-bit SYS" } > > +/* > + * ARMv8 Reset Values > + */ > +#define VCPU_RESET_PSTATE_EL1 (PSR_MODE_EL1h | PSR_A_BIT | PSR_I_BIT | \ > + PSR_F_BIT | PSR_D_BIT) > + > +#define VCPU_RESET_PSTATE_EL2 (PSR_MODE_EL2h | PSR_A_BIT | PSR_I_BIT | \ > + PSR_F_BIT | PSR_D_BIT) > + > +#define VCPU_RESET_PSTATE_SVC (PSR_AA32_MODE_SVC | PSR_AA32_A_BIT | \ > + PSR_AA32_I_BIT | PSR_AA32_F_BIT) > + > #endif /* __ARM64_KVM_ARM_H__ */ > diff --git a/arch/arm64/include/asm/kvm_emulate.h b/arch/arm64/include/asm/kvm_emulate.h > index 80b30fead3d1..2385d8855fcf 100644 > --- a/arch/arm64/include/asm/kvm_emulate.h > +++ b/arch/arm64/include/asm/kvm_emulate.h > @@ -704,4 +704,61 @@ static inline void vcpu_set_hcrx(struct kvm_vcpu *vcpu) > vcpu->arch.hcrx_el2 |= HCRX_EL2_EnASR; > } > } > + > +/* Reset a vcpu's core registers. */ > +static inline void kvm_reset_vcpu_core(struct kvm_vcpu *vcpu) > +{ > + u32 pstate; > + > + if (vcpu_el1_is_32bit(vcpu)) > + pstate = VCPU_RESET_PSTATE_SVC; > + else if (vcpu_has_nv(vcpu)) > + pstate = VCPU_RESET_PSTATE_EL2; > + else > + pstate = VCPU_RESET_PSTATE_EL1; > + > + /* Reset core registers */ > + memset(vcpu_gp_regs(vcpu), 0, sizeof(*vcpu_gp_regs(vcpu))); > + memset(&vcpu->arch.ctxt.fp_regs, 0, sizeof(vcpu->arch.ctxt.fp_regs)); > + vcpu->arch.ctxt.spsr_abt = 0; > + vcpu->arch.ctxt.spsr_und = 0; > + vcpu->arch.ctxt.spsr_irq = 0; > + vcpu->arch.ctxt.spsr_fiq = 0; > + vcpu_gp_regs(vcpu)->pstate = pstate; > +} > + > +/* PSCI reset handling for a vcpu. */ > +static inline void kvm_reset_vcpu_psci(struct kvm_vcpu *vcpu, > + struct vcpu_reset_state *reset_state) > +{ > + unsigned long target_pc = reset_state->pc; > + > + /* Gracefully handle Thumb2 entry point */ > + if (vcpu_mode_is_32bit(vcpu) && (target_pc & 1)) { > + target_pc &= ~1UL; > + vcpu_set_thumb(vcpu); > + } > + > + /* Propagate caller endianness */ > + if (reset_state->be) > + kvm_vcpu_set_be(vcpu); > + > + *vcpu_pc(vcpu) = target_pc; > + > + /* > + * We may come from a state where either a PC update was > + * pending (SMC call resulting in PC being increpented to > + * skip the SMC) or a pending exception. Make sure we get > + * rid of all that, as this cannot be valid out of reset. > + * > + * Note that clearing the exception mask also clears PC > + * updates, but that's an implementation detail, and we > + * really want to make it explicit. > + */ > + vcpu_clear_flag(vcpu, PENDING_EXCEPTION); > + vcpu_clear_flag(vcpu, EXCEPT_MASK); > + vcpu_clear_flag(vcpu, INCREMENT_PC); > + vcpu_set_reg(vcpu, 0, reset_state->r0); > +} > + > #endif /* __ARM64_KVM_EMULATE_H__ */ > diff --git a/arch/arm64/kvm/reset.c b/arch/arm64/kvm/reset.c > index b963fd975aac..10eb7249aa9e 100644 > --- a/arch/arm64/kvm/reset.c > +++ b/arch/arm64/kvm/reset.c > @@ -34,18 +34,6 @@ > static u32 __ro_after_init kvm_ipa_limit; > unsigned int __ro_after_init kvm_host_sve_max_vl; > > -/* > - * ARMv8 Reset Values > - */ > -#define VCPU_RESET_PSTATE_EL1 (PSR_MODE_EL1h | PSR_A_BIT | PSR_I_BIT | \ > - PSR_F_BIT | PSR_D_BIT) > - > -#define VCPU_RESET_PSTATE_EL2 (PSR_MODE_EL2h | PSR_A_BIT | PSR_I_BIT | \ > - PSR_F_BIT | PSR_D_BIT) > - > -#define VCPU_RESET_PSTATE_SVC (PSR_AA32_MODE_SVC | PSR_AA32_A_BIT | \ > - PSR_AA32_I_BIT | PSR_AA32_F_BIT) > - > unsigned int __ro_after_init kvm_sve_max_vl; > > int __init kvm_arm_init_sve(void) > @@ -191,7 +179,6 @@ void kvm_reset_vcpu(struct kvm_vcpu *vcpu) > { > struct vcpu_reset_state reset_state; > bool loaded; > - u32 pstate; > > spin_lock(&vcpu->arch.mp_state_lock); > reset_state = vcpu->arch.reset_state; > @@ -210,21 +197,8 @@ void kvm_reset_vcpu(struct kvm_vcpu *vcpu) > kvm_vcpu_reset_sve(vcpu); > } > > - if (vcpu_el1_is_32bit(vcpu)) > - pstate = VCPU_RESET_PSTATE_SVC; > - else if (vcpu_has_nv(vcpu)) > - pstate = VCPU_RESET_PSTATE_EL2; > - else > - pstate = VCPU_RESET_PSTATE_EL1; > - > /* Reset core registers */ > - memset(vcpu_gp_regs(vcpu), 0, sizeof(*vcpu_gp_regs(vcpu))); > - memset(&vcpu->arch.ctxt.fp_regs, 0, sizeof(vcpu->arch.ctxt.fp_regs)); > - vcpu->arch.ctxt.spsr_abt = 0; > - vcpu->arch.ctxt.spsr_und = 0; > - vcpu->arch.ctxt.spsr_irq = 0; > - vcpu->arch.ctxt.spsr_fiq = 0; > - vcpu_gp_regs(vcpu)->pstate = pstate; > + kvm_reset_vcpu_core(vcpu); > > /* Reset system registers */ > kvm_reset_sys_regs(vcpu); > @@ -233,36 +207,8 @@ void kvm_reset_vcpu(struct kvm_vcpu *vcpu) > * Additional reset state handling that PSCI may have imposed on us. > * Must be done after all the sys_reg reset. > */ > - if (reset_state.reset) { > - unsigned long target_pc = reset_state.pc; > - > - /* Gracefully handle Thumb2 entry point */ > - if (vcpu_mode_is_32bit(vcpu) && (target_pc & 1)) { > - target_pc &= ~1UL; > - vcpu_set_thumb(vcpu); > - } > - > - /* Propagate caller endianness */ > - if (reset_state.be) > - kvm_vcpu_set_be(vcpu); > - > - *vcpu_pc(vcpu) = target_pc; > - > - /* > - * We may come from a state where either a PC update was > - * pending (SMC call resulting in PC being increpented to > - * skip the SMC) or a pending exception. Make sure we get > - * rid of all that, as this cannot be valid out of reset. > - * > - * Note that clearing the exception mask also clears PC > - * updates, but that's an implementation detail, and we > - * really want to make it explicit. > - */ > - vcpu_clear_flag(vcpu, PENDING_EXCEPTION); > - vcpu_clear_flag(vcpu, EXCEPT_MASK); > - vcpu_clear_flag(vcpu, INCREMENT_PC); > - vcpu_set_reg(vcpu, 0, reset_state.r0); > - } > + if (reset_state.reset) > + kvm_reset_vcpu_psci(vcpu, &reset_state); > > /* Reset timer */ > kvm_timer_vcpu_reset(vcpu); > -- > 2.55.0.rc0.738.g0c8ab3ebcc-goog >