From nobody Fri Sep 19 10:30:10 2025 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 4cSpfq02lDz67nZT; Fri, 19 Sep 2025 10:30: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 "R12" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4cSpfp5sVPz3Lgv; Fri, 19 Sep 2025 10:30:10 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1758277810; 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=1egHfxrGKJOG0LXAuwWwrGmLBizoy3zyhGWOFFZF+1I=; b=h1G/2oJPuJfqFjyoK0wVzSzwIeROpw6T9ZZfZKxfpP4bQ9i3WbCI9O9JzfOHkeON/huyoI /GsYHh1CaTco9psSxeNCRWvQ42Xb2hhm2NpjQbtppxtmZ7w4DoXZKvBkfG+IQzGCiRr4/m rV6Bc2yWAgzEwJcJwPWufzirQqVQOaU8FRQ8+yGoJXQtQ1p/0Kd7tt4xgqabxtTcM5yoTD IArglEHeDDysFn9wjasSWpo/b9fQIqs4hRZtG+huBTW1hcvv5MQaSY7laWpyexqhOJ5ZsB cbLjAZR1+rQ8rMVMxiHInad3Vmx2MNL7woStrXscujcGTRbYMlUU12GDBWNC3w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1758277810; 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=1egHfxrGKJOG0LXAuwWwrGmLBizoy3zyhGWOFFZF+1I=; b=FlRhETzBSeoEGC9lStI3HHrvrcTy0reegeT5HB+m9E95SiDeGv/dBkovImv+Sr09kUbLf1 MHWOxMjksMesqWVm+VAEbt6iAK9hWFSIYitTCsgwuAjwnpKpD36V4IKNLYwauZQdAOHTp1 J6Yukr+K9Anlu6mjHbYTht9Y5sEv7g5SbQXwvORgF4EqVSNLJsDgSqkP9Cq9d7LDaCxcW2 ZDfpN4moHl2DGslxsxKj2IlRbQf3ftINQOzHDxOnjGYjLOyQqnl4P/O5l2IfuFlExuvSSF /BKxk9OBw3g+1SOCwJTUNkRVbedcuTvGkviiK+mHEMuN2kmU23nbgvhaWHL91g== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1758277810; a=rsa-sha256; cv=none; b=w4KEaoILhoWO+4/+veQlp5BiZgeoylHEZyiGYPgfF7GtmPTnvxZVIoU4QG3qgly23pjKf6 qzsNmI1U7BuTf1kj3U9j7RsWuWc6GMibmprRuXdmjGnyf0w7iUZh2HJ+5fB8ewLtASdwRY AMgv4ir3nMlMlveO/gO4795qALAtVS33vyk4xaArvSUGGJPZ16qIWJljD9s+H+Za5xXGTO D/LTLda4ibZgPHWlr60LiOHu2xjGzPRItH14KbcfN9sXZed1fdEh39GSm14KENaN1rsTbL MtbRTOmKkzeqFgmwSVhaQT61ZppnEoGGRRHVfnlfZfnlXhppKcpZAYYiJ5S7RQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none 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 4cSpfp5MPcztKR; Fri, 19 Sep 2025 10:30: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 58JAUAr6005596; Fri, 19 Sep 2025 10:30:10 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 58JAUACK005592; Fri, 19 Sep 2025 10:30:10 GMT (envelope-from git) Date: Fri, 19 Sep 2025 10:30:10 GMT Message-Id: <202509191030.58JAUACK005592@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: 0706d3464f4e - main - arm64: Set the endian without a memory access 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: 0706d3464f4ef375fc31ecc7fa0733a13eca9d19 Auto-Submitted: auto-generated The branch main has been updated by andrew: URL: https://cgit.FreeBSD.org/src/commit/?id=0706d3464f4ef375fc31ecc7fa0733a13eca9d19 commit 0706d3464f4ef375fc31ecc7fa0733a13eca9d19 Author: Andrew Turner AuthorDate: 2025-09-19 10:05:47 +0000 Commit: Andrew Turner CommitDate: 2025-09-19 10:05:47 +0000 arm64: Set the endian without a memory access Early in the kernel we set the endian through the sctlr_el1 and sctlr_el2 registers. To get the value to put into these registers we load them from memory. As this will depend on the endian to get the fields in the correct order then it will fail if the endian is not what the kernel expects. Add a macro to load a 64-bit value into a register without a memory access and use this to set the register. As instructions are not affected by the endian set in sctlr this is safe. It is unlikely this will be hit as UEFI requires the processor to be in little endian mode, however when booting using the Linux ABI the kernel may start in big-endian, and secondary CPUs could be big-endian. Reviewed by: emaste Sponsored by: Arm Ltd Differential Revision: https://reviews.freebsd.org/D51012 --- sys/arm64/arm64/locore.S | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/sys/arm64/arm64/locore.S b/sys/arm64/arm64/locore.S index 4a10a2b4f2d3..50a3eda846da 100644 --- a/sys/arm64/arm64/locore.S +++ b/sys/arm64/arm64/locore.S @@ -39,6 +39,23 @@ #define VIRT_BITS 48 +/* + * Loads a 64-bit value into reg using 1 to 4 mov/movk instructions. + * This can be used early on when we don't know the CPUs endianness. + */ +.macro mov_q reg, val + mov \reg, :abs_g0_nc:\val +.if (\val >> 16) & 0xffff != 0 + movk \reg, :abs_g1_nc:\val +.endif +.if (\val >> 32) & 0xffff != 0 + movk \reg, :abs_g2_nc:\val +.endif +.if (\val >> 48) & 0xffff != 0 + movk \reg, :abs_g3:\val +.endif +.endm + #if PAGE_SIZE == PAGE_SIZE_16K /* * The number of level 3 tables to create. 32 will allow for 1G of address @@ -324,15 +341,23 @@ LENTRY(enter_kernel_el) cmp x23, #(CURRENTEL_EL_EL2) b.eq 1f - ldr x2, =SCTLR_MMU_OFF + /* + * Ensure there are no memory operations here. If the boot loader + * enters the kernel in big-endian mode then loading sctlr will + * be incorrect. As instructions are the same in both endians it is + * safe to use mov instructions. + */ + mov_q x2, SCTLR_MMU_OFF msr sctlr_el1, x2 - /* SCTLR_EOS is set so eret is a context synchronizing event so we + /* + * SCTLR_EOS is set to make eret a context synchronizing event. 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 + /* + * Ensure SPSR_EL1 and pstate are in sync. The only way to set the * latter is to set the former and return from an exception with eret. */ mov x2, #(PSR_DAIF | PSR_M_EL1h) @@ -346,11 +371,19 @@ LENTRY(enter_kernel_el) * 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. + * + * As with sctlr_el1 above use mov instructions to ensure there are + * no memory operations. */ - ldr x2, =(SCTLR_EL2_RES1 | SCTLR_EL2_EIS | SCTLR_EL2_EOS) + mov_q x2, (SCTLR_EL2_RES1 | SCTLR_EL2_EIS | SCTLR_EL2_EOS) msr sctlr_el2, x2 isb + /* + * The hardware is now in little-endian mode so memory operations + * are safe. + */ + /* Configure the Hypervisor */ ldr x2, =(HCR_RW | HCR_APK | HCR_API) msr hcr_el2, x2