Linux KVM/arm64 development list
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Fuad Tabba" <fuad.tabba@linux.dev>
Cc: "Oliver Upton" <oupton@kernel.org>,
	"Marc Zyngier" <maz@kernel.org>,
	kvmarm@lists.linux.dev
Subject: Re: [PATCH v7 2/8] KVM: arm64: Make vcpu_{read,write}_sys_reg available to HYP code
Date: Wed, 29 Jul 2026 13:34:03 +0000	[thread overview]
Message-ID: <20260729133403.C4DC51F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260729131823.2021516-3-fuad.tabba@linux.dev>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Pre-existing bug: `enter_exception64` reads stale `SCTLR_EL1` during early trap handling in nVHE/pKVM.
--

--- Patch [2]: [PATCH v7 2/8] KVM: arm64: Make vcpu_{read,write}_sys_reg available to HYP code ---
commit e62bbea08d3143161297d701776a47e619a76db1
Author: Fuad Tabba <fuad.tabba@linux.dev>

KVM: arm64: Make vcpu_{read,write}_sys_reg available to HYP code

This commit replaces the locally open-coded __vcpu_read_sys_reg and
__vcpu_write_sys_reg wrappers in exception.c with the canonical
vcpu_{read,write}_sys_reg accessors. For nVHE builds, it maps these
accessors directly to the raw in-memory context accessors, allowing HYP
code to reuse helpers without function duplication.

> diff --git a/arch/arm64/kvm/hyp/exception.c b/arch/arm64/kvm/hyp/exception.c
> index bef40ddb16dbc..754e2dc1df54a 100644
> --- a/arch/arm64/kvm/hyp/exception.c
> +++ b/arch/arm64/kvm/hyp/exception.c

[ ... ]

> @@ -101,14 +85,14 @@ static void enter_exception64(struct kvm_vcpu *vcpu, unsigned long target_mode,
>  
>  	switch (target_mode) {
>  	case PSR_MODE_EL1h:
> -		vbar = __vcpu_read_sys_reg(vcpu, VBAR_EL1);
> -		sctlr = __vcpu_read_sys_reg(vcpu, SCTLR_EL1);
> -		__vcpu_write_sys_reg(vcpu, *vcpu_pc(vcpu), ELR_EL1);
> +		vbar = vcpu_read_sys_reg(vcpu, VBAR_EL1);
> +		sctlr = vcpu_read_sys_reg(vcpu, SCTLR_EL1);

[Severity: High]
This is a pre-existing issue, but does enter_exception64() read a stale
SCTLR_EL1 value from the in-memory context here?

When a protected VM triggers an early EL2 trap, the hypervisor handles it
via the kvm_handle_pvm_sysreg() -> inject_undef64() -> inject_sync64() call
chain. At that point, the guest's system registers aren't fully saved to the
in-memory vcpu->arch.ctxt.

While inject_sync64() explicitly synchronizes VBAR_EL1 from hardware to memory:

arch/arm64/kvm/hyp/nvhe/sys_regs.c:inject_sync64() {
    ...
    __vcpu_assign_sys_reg(vcpu, VBAR_EL1, read_sysreg_el1(SYS_VBAR));
    ...
}

It doesn't appear to synchronize SCTLR_EL1 before triggering exception
injection.

Since nVHE builds map vcpu_read_sys_reg() directly to the memory context,
enter_exception64() seems to use a stale SCTLR_EL1 value to calculate the
PSR_PAN_BIT and PSR_SSBS_BIT for the exception's PSTATE.

This corrupted PSTATE is then written directly to the hardware SYS_SPSR_EL2
by inject_sync64(), potentially poisoning the guest's state upon ERET.

Should SCTLR_EL1 also be synchronized from hardware before injection?

> +		vcpu_write_sys_reg(vcpu, *vcpu_pc(vcpu), ELR_EL1);
>  		break;
>  	case PSR_MODE_EL2h:
> -		vbar = __vcpu_read_sys_reg(vcpu, VBAR_EL2);
> -		sctlr = __vcpu_read_sys_reg(vcpu, SCTLR_EL2);
> -		__vcpu_write_sys_reg(vcpu, *vcpu_pc(vcpu), ELR_EL2);
> +		vbar = vcpu_read_sys_reg(vcpu, VBAR_EL2);
> +		sctlr = vcpu_read_sys_reg(vcpu, SCTLR_EL2);
> +		vcpu_write_sys_reg(vcpu, *vcpu_pc(vcpu), ELR_EL2);
>  		break;
>  	default:

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260729131823.2021516-1-fuad.tabba@linux.dev?part=2

  reply	other threads:[~2026-07-29 13:34 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-29 13:18 [PATCH v7 0/8] KVM: arm64: pKVM vCPU state management at EL2 Fuad Tabba
2026-07-29 13:18 ` [PATCH v7 1/8] KVM: arm64: Extract MPIDR computation into a shared header Fuad Tabba
2026-07-29 13:18 ` [PATCH v7 2/8] KVM: arm64: Make vcpu_{read,write}_sys_reg available to HYP code Fuad Tabba
2026-07-29 13:34   ` sashiko-bot [this message]
2026-07-29 13:40     ` Fuad Tabba
2026-07-29 13:52   ` Joey Gouly
2026-07-29 14:51     ` Fuad Tabba
2026-07-29 13:18 ` [PATCH v7 3/8] KVM: arm64: Factor out reusable vCPU reset helpers Fuad Tabba
2026-07-29 13:18 ` [PATCH v7 4/8] KVM: arm64: Move PSCI helper functions to a shared header Fuad Tabba
2026-07-29 13:31   ` sashiko-bot
2026-07-29 13:33     ` Fuad Tabba
2026-07-29 13:18 ` [PATCH v7 5/8] KVM: arm64: Add host and hypervisor vCPU lookup primitives Fuad Tabba
2026-07-29 13:18 ` [PATCH v7 6/8] KVM: arm64: Minimise EL2's exposure of host VGIC state during world switch Fuad Tabba
2026-07-29 13:39   ` sashiko-bot
2026-07-29 13:44     ` Fuad Tabba
2026-07-29 13:18 ` [PATCH v7 7/8] KVM: arm64: Add primitives to flush/sync the VGIC state at EL2 Fuad Tabba
2026-07-29 13:18 ` [PATCH v7 8/8] KVM: arm64: Implement lazy vCPU state sync for non-protected guests Fuad Tabba
2026-07-29 13:36   ` sashiko-bot
2026-07-29 13:46     ` Fuad Tabba
2026-07-31 18:51 ` [PATCH v7 0/8] KVM: arm64: pKVM vCPU state management at EL2 Oliver Upton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260729133403.C4DC51F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=fuad.tabba@linux.dev \
    --cc=kvmarm@lists.linux.dev \
    --cc=maz@kernel.org \
    --cc=oupton@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox