From 5d63c3e460eb868885822eb7281681643597ff8b Mon Sep 17 00:00:00 2001 From: Theodore Karatapanis Date: Tue, 6 May 2025 21:47:02 +0300 Subject: [PATCH] arch/arm64: Allow non MMU builds. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit MMU initialization is redundant for bootloaders(BL2) that load TF‑A(BL31). TF-A will ignore and overwrite any MMU configuration performed by BL2. Additionally the MMU comes with a large memory footprint (~50 KB) which could be precious for such setups. When the MMU is disabled all the memory is treated as device memory and all alignment faults generate exceptions (as opposed to normal memory when MMU is enabled). For that reason we need to add -fno-builtin when compiling with CONFIG_ARCH_USE_MMU != y to prevent alignment faults. - Guard MMU initialization with CONFIG_ARCH_USE_MMU - If CONFIG_ARCH_USE_MMU is not set add -fno-builtin which prevents alignment errors. - Deliberately didn't try to replace CONFIG_ARCH_HAVE_MMU with CONFIG_ARCH_USE_MMU occurrences, because this will break a lot of build targets that assume MMU without guarding the MMU initialization. Signed-off-by: Theodore Karatapanis --- arch/arm64/src/Toolchain.defs | 7 +++++++ arch/arm64/src/cmake/Toolchain.cmake | 4 ++++ arch/arm64/src/imx9/imx9_boot.c | 2 ++ 3 files changed, 13 insertions(+) diff --git a/arch/arm64/src/Toolchain.defs b/arch/arm64/src/Toolchain.defs index 3de82652a5f..40862575419 100644 --- a/arch/arm64/src/Toolchain.defs +++ b/arch/arm64/src/Toolchain.defs @@ -100,6 +100,13 @@ ifeq ($(CONFIG_MM_KASAN_DISABLE_WRITES_CHECK),y) ARCHOPTIMIZATION += --param asan-instrument-writes=0 endif +# If we don't use MMU, all memory is treated as device memory and can't handle +# alignment faults. Some compiler builtins like memset() can introduce +# alignment faults. We need to disable them in that case. +ifneq ($(CONFIG_ARCH_USE_MMU),y) + ARCHOPTIMIZATION += -fno-builtin +endif + # Instrumentation options ifeq ($(CONFIG_ARCH_INSTRUMENT_ALL),y) diff --git a/arch/arm64/src/cmake/Toolchain.cmake b/arch/arm64/src/cmake/Toolchain.cmake index f86506374e9..973f8c88973 100644 --- a/arch/arm64/src/cmake/Toolchain.cmake +++ b/arch/arm64/src/cmake/Toolchain.cmake @@ -195,6 +195,10 @@ if(CONFIG_DEBUG_SYMBOLS) add_compile_options(${CONFIG_DEBUG_SYMBOLS_LEVEL}) endif() +if(NOT CONFIG_ARCH_USE_MMU) + add_compile_options(-fno-builtin) +endif() + if(CONFIG_ARCH_TOOLCHAIN_GNU AND NOT CONFIG_ARCH_TOOLCHAIN_CLANG) if(NOT GCCVER) execute_process(COMMAND ${CMAKE_C_COMPILER} --version diff --git a/arch/arm64/src/imx9/imx9_boot.c b/arch/arm64/src/imx9/imx9_boot.c index 259c3cbb239..f0ed552d8c9 100644 --- a/arch/arm64/src/imx9/imx9_boot.c +++ b/arch/arm64/src/imx9/imx9_boot.c @@ -142,9 +142,11 @@ void arm64_chip_boot(void) #endif #endif +#ifdef CONFIG_ARCH_USE_MMU /* MAP IO and DRAM, enable MMU. */ arm64_mmu_init(true); +#endif /* Do UART early initialization & pin muxing */