KVM Archive mirror
 help / color / mirror / Atom feed
From: Marc Zyngier <maz@kernel.org>
To: Will Deacon <will@kernel.org>
Cc: kvmarm@lists.linux.dev, kvm@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org,
	James Morse <james.morse@arm.com>,
	Suzuki K Poulose <suzuki.poulose@arm.com>,
	Oliver Upton <oliver.upton@linux.dev>,
	Zenghui Yu <yuzenghui@huawei.com>
Subject: Re: [PATCH] KVM: arm64: Correct BTYPE/SS in host SMC emulation
Date: Wed, 08 May 2024 07:04:34 +0100	[thread overview]
Message-ID: <87ikzozvnx.wl-maz@kernel.org> (raw)
In-Reply-To: <20240507145733.GB22453@willie-the-truck>

On Tue, 07 May 2024 15:57:34 +0100,
Will Deacon <will@kernel.org> wrote:
> 
> On Thu, May 02, 2024 at 07:00:20PM +0100, Marc Zyngier wrote:
> > When taking a trap for an SMC instruction on the host, we must
> > stau true to the letter of the architecture and perform all the
> 
> typo: stay
> 
> > actions that the CPU would otherwise do. Among those are clearing
> > the BTYPE and SS bits.
> > 
> > Just do that.
> > 
> > Fixes: a805e1fb3099 ("KVM: arm64: Add SMC handler in nVHE EL2")
> > Signed-off-by: Marc Zyngier <maz@kernel.org>
> > ---
> >  arch/arm64/kvm/hyp/include/hyp/adjust_pc.h | 6 ++++++
> >  1 file changed, 6 insertions(+)
> > 
> > diff --git a/arch/arm64/kvm/hyp/include/hyp/adjust_pc.h b/arch/arm64/kvm/hyp/include/hyp/adjust_pc.h
> > index 4fdfeabefeb4..b1afb7b59a31 100644
> > --- a/arch/arm64/kvm/hyp/include/hyp/adjust_pc.h
> > +++ b/arch/arm64/kvm/hyp/include/hyp/adjust_pc.h
> > @@ -47,7 +47,13 @@ static inline void __kvm_skip_instr(struct kvm_vcpu *vcpu)
> >   */
> >  static inline void kvm_skip_host_instr(void)
> >  {
> > +	u64 spsr = read_sysreg_el2(SYS_SPSR);
> > +
> >  	write_sysreg_el2(read_sysreg_el2(SYS_ELR) + 4, SYS_ELR);
> > +
> > +	spsr &= ~(PSR_BTYPE_MASK | DBG_SPSR_SS);
> > +
> > +	write_sysreg_el2(spsr, SYS_SPSR);
> 
> The handling of SS looks correct to me, but I think the BTYPE
> manipulation could do with a little more commentary as it looks quite
> subtle when the SMC is in a guarded page. Am I right in thinking:
> 
>    * If the SMC is in a guarded page, the Branch Target exception is
>      higher priority (12) than the trap to EL2 and so the host will
>      handle it.
> 
>    * Therefore if a trapping SMC is in a guarded page, BTYPE must be
>      zero and we don't have to worry about injecting a Branch Target
>      exception.
> 
>    * Otherwise, if the SMC is in a non-guarded page, we should clear it
>      to 0 per the architecture (R_YWFHD).
> 
> ?

This is all correct. If we get to emulate the SMC by trapping to EL2,
it is that the instruction already satisfied the more basic execution
requirements such as having an acceptable BTYPE at that PC.

If that's OK with you, I'll nick that write-up and stick it into the
next revision of the patch.

> Having said that, I can't actually find the priority of an SMC trapped
> to EL2 by HCR_EL2.TSC in the Arm ARM. Trapped HVCs are priority 15 and
> SMCs trapped to EL3 are priority 23.

My understanding is that this falls into the catch-all priority 22 of
R_ZFGJP ("Other than an exception defined by priorities 4-21
inclusive, any exception that is the result of a configurable access
to instructions, where the exception is taken to EL2.").

Thanks,

	M.

-- 
Without deviation from the norm, progress is not possible.

      reply	other threads:[~2024-05-08  6:04 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-02 18:00 [PATCH] KVM: arm64: Correct BTYPE/SS in host SMC emulation Marc Zyngier
2024-05-07 14:57 ` Will Deacon
2024-05-08  6:04   ` Marc Zyngier [this message]

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=87ikzozvnx.wl-maz@kernel.org \
    --to=maz@kernel.org \
    --cc=james.morse@arm.com \
    --cc=kvm@vger.kernel.org \
    --cc=kvmarm@lists.linux.dev \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=oliver.upton@linux.dev \
    --cc=suzuki.poulose@arm.com \
    --cc=will@kernel.org \
    --cc=yuzenghui@huawei.com \
    /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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).