From nobody Tue Jul 23 09:52:10 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4WSsrC1M3bz5RdxR; Tue, 23 Jul 2024 09:52:11 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4WSsrB6lCxz4thy; Tue, 23 Jul 2024 09:52:10 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1721728330; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=6dLhwMH+lhkR4x8uHCIPpBLixwaKWL/e91jm6uQ/I5s=; b=n3IngwWA6Ui+DUeFkIyXL1q6ftUFRgqpBuYLWEfxkG/5smzCDM0o8gsayCsn1JRIX90WR8 RrUXeromIB14rlv587KMQ1FrBOa5c5lUTBsjnICVA1Nl/Ry+suy94n/oZUmXW5TaJ7M+J5 LcOtSIkluishFrtSFPVNN+YoxOcglnWx360J0BckEU82t/p5a3L9l2loAg3evmGOK0uPLx iLjSV3u5JB98a/ve5x/NXgmZ6/t41Z9NYJ5CkFuDfJm25lRc6md6j2nGzfrw0BBAEL0kZF nmF8SekdS4cXftkaVWDTLnjlkDBl75mG7lgzFNN1gZeZzNOyHSRlC2MLPiiC/Q== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1721728330; a=rsa-sha256; cv=none; b=b5vw+1J5YL3dpLNRHSTurL1SbRqinUW2KLo/+We+nmU1VXrS7eJFSmhzmjGLQ0ZKyb9jdg wft9HRlPNEtFLzY5j8KEAM5tn/WhIoQXqDRS7ovnbt1xtJRNaFUQL1VjiMznhidA6+it0j AXOAd/aIgs80i+W4SlIwSIIXPoh37iZjI6OOrl38Jb7+hoQd+rzVTYolnIxhFxuI6SynRn 51fQY8N5NPN88qJ5LcgAih7HokdV6qFp9zrNPUMe/xjqbcrRTGsN6ueib2TTlZBjX0LF9X cTODvIFEw+Ps0SWJIRaiRhquwc1cl039q01kLLzkMeGGGDSk4iuy86l/2tmbLw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1721728330; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=6dLhwMH+lhkR4x8uHCIPpBLixwaKWL/e91jm6uQ/I5s=; b=mXFl4IZavFaQTx4KreFOf2bWtYiNwTbti8vJWTRyvOPZXviFsX3JkJDTPgZHCARTZo7n+C SX0mP5uEEvh4wT6RjbGzzt00fppx2EmHkW3zT17rApIqZ28SjGuqAW0xrHKVGKnjk6diB+ W9gx5zRzWiCljgp7CaoJGoD5y96l3PlMjEMRNJks1wIuP58/uamhG/GJ17lKBz1b1FRDCq 4irxzQGAxHY+qhKihc45Fn0ICI+p/mdODyHwlfH+whtllrNDYIMh7PWb6Wig8oSgvnwhan 4TuVIwSBAZ9l827jG4CzHKxQY+0CN+QibPlq/dDSkt4z5zh76IlsVIajL7jAxw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4WSsrB68rbzPFV; Tue, 23 Jul 2024 09:52:10 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 46N9qAH0061147; Tue, 23 Jul 2024 09:52:10 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 46N9qAZm061144; Tue, 23 Jul 2024 09:52:10 GMT (envelope-from git) Date: Tue, 23 Jul 2024 09:52:10 GMT Message-Id: <202407230952.46N9qAZm061144@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Andrew Turner Subject: git: 034c83fd7d85 - main - arm64: Ensure sctlr and pstate are in known states List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: andrew X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 034c83fd7d85f57193850a73cc0ac957a211f725 Auto-Submitted: auto-generated The branch main has been updated by andrew: URL: https://cgit.FreeBSD.org/src/commit/?id=034c83fd7d85f57193850a73cc0ac957a211f725 commit 034c83fd7d85f57193850a73cc0ac957a211f725 Author: Andrew Turner AuthorDate: 2024-07-23 09:18:24 +0000 Commit: Andrew Turner CommitDate: 2024-07-23 09:25:05 +0000 arm64: Ensure sctlr and pstate are in known states Before entering the kernel exception level ensure sctlr_el2 and sctlr_el1 are in a known state. The EOS flag needs to be set to ensure an eret instruction is a context synchronization event. Set spcr_el1 when entering the kernel from EL1 and use an eret instruction to return to the caller. This ensures the CPU pstate is consistent with the value in spcr_el1 as it is the only way to set it directly. Sponsored by: Arm Ltd Differential Revision: https://reviews.freebsd.org/D45528 --- sys/arm64/arm64/locore.S | 52 ++++++++++++++++++++++-------------------- sys/arm64/include/hypervisor.h | 4 ++++ 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/sys/arm64/arm64/locore.S b/sys/arm64/arm64/locore.S index 3dcb01fd2a6e..4252ea3f59f8 100644 --- a/sys/arm64/arm64/locore.S +++ b/sys/arm64/arm64/locore.S @@ -67,19 +67,6 @@ ENTRY(_start) /* Enter the kernel exception level */ bl enter_kernel_el - /* - * Disable the MMU. We may have entered the kernel with it on and - * will need to update the tables later. If this has been set up - * with anything other than a VA == PA map then this will fail, - * but in this case the code to find where we are running from - * would have also failed. - */ - dsb sy - mrs x2, sctlr_el1 - bic x2, x2, SCTLR_M - msr sctlr_el1, x2 - isb - /* Set the context id */ msr contextidr_el1, xzr @@ -314,19 +301,37 @@ LEND(mpentry_common) * registers and drop to EL1. */ LENTRY(enter_kernel_el) +#define INIT_SCTLR_EL1 (SCTLR_LSMAOE | SCTLR_nTLSMD | SCTLR_EIS | \ + SCTLR_TSCXT | SCTLR_EOS) mrs x23, CurrentEL and x23, x23, #(CURRENTEL_EL_MASK) cmp x23, #(CURRENTEL_EL_EL2) b.eq 1f - ret + + ldr x2, =INIT_SCTLR_EL1 + msr sctlr_el1, x2 + /* SCTLR_EOS is set so eret is a context synchronizing event so we + * need an isb here to ensure it's observed by later instructions, + * but don't need it in the eret below. + */ + isb + + /* Ensure SPSR_EL1 and pstate are in sync. The only wat to set the + * latter is to set the former and return from an exception with eret. + */ + mov x2, #(PSR_DAIF | PSR_M_EL1h) + msr spsr_el1, x2 + msr elr_el1, lr + eret + 1: + dsb sy /* - * Disable the MMU. If the HCR_EL2.E2H field is set we will clear it - * which may break address translation. + * Set just the reserved bits in sctlr_el2. This will disable the + * MMU which may have broken the kernel if we enter the kernel in + * EL2, e.g. when using VHE. */ - dsb sy - mrs x2, sctlr_el2 - bic x2, x2, SCTLR_M + ldr x2, =(SCTLR_EL2_RES1 | SCTLR_EL2_EIS | SCTLR_EL2_EOS) msr sctlr_el2, x2 isb @@ -346,8 +351,8 @@ LENTRY(enter_kernel_el) mrs x2, mpidr_el1 msr vmpidr_el2, x2 - /* Set the bits that need to be 1 in sctlr_el1 */ - ldr x2, .Lsctlr_res1 + /* Set the initial sctlr_el1 */ + ldr x2, =INIT_SCTLR_EL1 msr sctlr_el1, x2 /* @@ -403,10 +408,7 @@ LENTRY(enter_kernel_el) isb eret - - .align 3 -.Lsctlr_res1: - .quad SCTLR_RES1 +#undef INIT_SCTLR_EL1 LEND(enter_kernel_el) /* diff --git a/sys/arm64/include/hypervisor.h b/sys/arm64/include/hypervisor.h index 418047cb22f2..011f86e83fdf 100644 --- a/sys/arm64/include/hypervisor.h +++ b/sys/arm64/include/hypervisor.h @@ -148,10 +148,14 @@ #define SCTLR_EL2_C (0x1UL << SCTLR_EL2_C_SHIFT) #define SCTLR_EL2_SA_SHIFT 3 #define SCTLR_EL2_SA (0x1UL << SCTLR_EL2_SA_SHIFT) +#define SCTLR_EL2_EOS_SHIFT 11 +#define SCTLR_EL2_EOS (0x1UL << SCTLR_EL2_EOS_SHIFT) #define SCTLR_EL2_I_SHIFT 12 #define SCTLR_EL2_I (0x1UL << SCTLR_EL2_I_SHIFT) #define SCTLR_EL2_WXN_SHIFT 19 #define SCTLR_EL2_WXN (0x1UL << SCTLR_EL2_WXN_SHIFT) +#define SCTLR_EL2_EIS_SHIFT 22 +#define SCTLR_EL2_EIS (0x1UL << SCTLR_EL2_EIS_SHIFT) #define SCTLR_EL2_EE_SHIFT 25 #define SCTLR_EL2_EE (0x1UL << SCTLR_EL2_EE_SHIFT)