This is an automated email from the ASF dual-hosted git repository. acassis pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 0a20a469cd4e06ee09b7069951a37a840bd84c30 Author: Marco Casaroli <[email protected]> AuthorDate: Tue Nov 10 10:23:25 2026 +0100 arch: Add up_addrenv_select_kernel() to select the kernel page tables. up_addrenv_select_kernel() selects the page tables of the kernel alone on the calling CPU. After the call the CPU uses no address environment of a process, so the address environment that it used before can be destroyed. Each architecture that supports kernel builds has it: - arm64 (MMU) and RISC-V write the root of the kernel page tables (g_kernel_pgt_pbase) to TTBR0_EL1 or satp. - x86_64 writes the boot PML4 to CR3. The boot PML4 becomes global as g_pml4_low, like g_pdpt_low. - ARMv7-A writes the boot L1 page table of the CPU to TTBR0. - ESP32-S3 invalidates the cache MMU entries of the user .text, .data and heap windows. Each of them makes the TLB or the instruction cache coherent, as its up_addrenv_select() does. The function exists only in kernel builds: flat and protected builds do not change. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]> --- Documentation/os/memory/addrenv.rst | 13 +++++++++++ arch/arm/src/armv7-a/arm_addrenv.c | 27 ++++++++++++++++++++++ arch/arm64/src/common/arm64_addrenv_mmu.c | 24 ++++++++++++++++++++ arch/risc-v/src/common/riscv_addrenv.c | 24 ++++++++++++++++++++ arch/x86_64/include/intel64/arch.h | 1 + arch/x86_64/src/common/x86_64_addrenv.c | 26 ++++++++++++++++++++++ arch/x86_64/src/intel64/intel64_head.S | 5 +++-- arch/xtensa/src/esp32s3/esp32s3_addrenv.c | 37 +++++++++++++++++++++++++++++++ include/nuttx/arch.h | 21 ++++++++++++++++++ 9 files changed, 176 insertions(+), 2 deletions(-) diff --git a/Documentation/os/memory/addrenv.rst b/Documentation/os/memory/addrenv.rst index cd23a3d091b..ecef7b44a12 100644 --- a/Documentation/os/memory/addrenv.rst +++ b/Documentation/os/memory/addrenv.rst @@ -34,6 +34,8 @@ The CPU-specific logic must provide two categories in interfaces: - :c:func:`up_addrenv_vdata()`: Returns the virtual base address of the ``.bss``/``.data`` address environment. - :c:func:`up_addrenv_heapsize()`: Return the initial heap size. - :c:func:`up_addrenv_select()`: Instantiate an address environment. + - :c:func:`up_addrenv_select_kernel()`: Select the page tables of the + kernel alone (``CONFIG_BUILD_KERNEL``). - :c:func:`up_addrenv_clone()`: Copy an address environment from one location to another. #. **Tasking Support**. Other interfaces must be provided to @@ -174,6 +176,17 @@ The CPU-specific logic must provide two categories in interfaces: :return: Zero (OK) on success; a negated errno value on failure. +.. c:function:: int up_addrenv_select_kernel(void) + + Select the page tables of the kernel alone on this CPU: after the call + the CPU uses no address environment of a process. The kernel calls it + when a CPU goes on with a kernel thread and no task owns the address + environment of the CPU any more (its process has ended). The address + environment can then be destroyed although the CPU ran it last. Each + architecture that supports ``CONFIG_BUILD_KERNEL`` must provide it. + + :return: Zero (OK) on success; a negated errno value on failure. + .. c:function:: int up_addrenv_clone(FAR const task_group_s *src, FAR struct task_group_s *dest) Duplicate an address environment. This does not copy the underlying diff --git a/arch/arm/src/armv7-a/arm_addrenv.c b/arch/arm/src/armv7-a/arm_addrenv.c index fa31d50e7e3..7c98207250d 100644 --- a/arch/arm/src/armv7-a/arm_addrenv.c +++ b/arch/arm/src/armv7-a/arm_addrenv.c @@ -578,6 +578,33 @@ int up_addrenv_select(const arch_addrenv_t *addrenv) return OK; } +/**************************************************************************** + * Name: up_addrenv_select_kernel + * + * Description: + * Select the page tables of the kernel alone on this CPU: after the call + * the CPU uses no address environment of a process, and the address + * environment that it used before can be destroyed. + * + * Input Parameters: + * None + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. + * + ****************************************************************************/ + +#ifdef CONFIG_BUILD_KERNEL +int up_addrenv_select_kernel(void) +{ + /* Each CPU has its own copy of the boot L1 page table */ + + mmu_l1_setpgtable((uintptr_t *)(PGTABLE_BASE_PADDR + + PGTABLE_SIZE * this_cpu())); + return OK; +} +#endif + /**************************************************************************** * Name: up_addrenv_coherent * diff --git a/arch/arm64/src/common/arm64_addrenv_mmu.c b/arch/arm64/src/common/arm64_addrenv_mmu.c index db847d34758..98f38663aad 100644 --- a/arch/arm64/src/common/arm64_addrenv_mmu.c +++ b/arch/arm64/src/common/arm64_addrenv_mmu.c @@ -944,6 +944,30 @@ int up_addrenv_select(const arch_addrenv_t *addrenv) return OK; } +/**************************************************************************** + * Name: up_addrenv_select_kernel + * + * Description: + * Select the page tables of the kernel alone on this CPU: after the call + * the CPU uses no address environment of a process, and the address + * environment that it used before can be destroyed. + * + * Input Parameters: + * None + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. + * + ****************************************************************************/ + +#ifdef CONFIG_BUILD_KERNEL +int up_addrenv_select_kernel(void) +{ + mmu_write_ttbr0(mmu_ttbr_reg(g_kernel_pgt_pbase, 0)); + return OK; +} +#endif + /**************************************************************************** * Name: up_addrenv_coherent * diff --git a/arch/risc-v/src/common/riscv_addrenv.c b/arch/risc-v/src/common/riscv_addrenv.c index 463dd8bf4ba..e42504aa984 100644 --- a/arch/risc-v/src/common/riscv_addrenv.c +++ b/arch/risc-v/src/common/riscv_addrenv.c @@ -946,6 +946,30 @@ int up_addrenv_select(const arch_addrenv_t *addrenv) return OK; } +/**************************************************************************** + * Name: up_addrenv_select_kernel + * + * Description: + * Select the page tables of the kernel alone on this CPU: after the call + * the CPU uses no address environment of a process, and the address + * environment that it used before can be destroyed. + * + * Input Parameters: + * None + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. + * + ****************************************************************************/ + +#ifdef CONFIG_BUILD_KERNEL +int up_addrenv_select_kernel(void) +{ + mmu_write_satp(mmu_satp_reg(g_kernel_pgt_pbase, 0)); + return OK; +} +#endif + /**************************************************************************** * Name: up_addrenv_coherent * diff --git a/arch/x86_64/include/intel64/arch.h b/arch/x86_64/include/intel64/arch.h index efb81dbdcc7..c001a748945 100644 --- a/arch/x86_64/include/intel64/arch.h +++ b/arch/x86_64/include/intel64/arch.h @@ -553,6 +553,7 @@ begin_packed_struct struct tss_s /* These are defined in intel64_head.S */ +extern volatile uint8_t g_pml4_low; extern volatile uint8_t g_pdpt_low; extern volatile uint8_t g_pd_low; extern volatile uint8_t g_pt_low; diff --git a/arch/x86_64/src/common/x86_64_addrenv.c b/arch/x86_64/src/common/x86_64_addrenv.c index 63f6d9b76e0..8f1b278b6ff 100644 --- a/arch/x86_64/src/common/x86_64_addrenv.c +++ b/arch/x86_64/src/common/x86_64_addrenv.c @@ -917,6 +917,32 @@ int up_addrenv_select(const arch_addrenv_t *addrenv) return OK; } +/**************************************************************************** + * Name: up_addrenv_select_kernel + * + * Description: + * Select the page tables of the kernel alone on this CPU: after the call + * the CPU uses no address environment of a process, and the address + * environment that it used before can be destroyed. + * + * Input Parameters: + * None + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. + * + ****************************************************************************/ + +#ifdef CONFIG_BUILD_KERNEL +int up_addrenv_select_kernel(void) +{ + /* The PML4 of the kernel: the processes have copies of it */ + + set_cr3(mmu_cr3_reg((uintptr_t)&g_pml4_low, 0)); + return OK; +} +#endif + /**************************************************************************** * Name: up_addrenv_coherent * diff --git a/arch/x86_64/src/intel64/intel64_head.S b/arch/x86_64/src/intel64/intel64_head.S index d3cef4b99d0..d7bfad3a4e7 100644 --- a/arch/x86_64/src/intel64/intel64_head.S +++ b/arch/x86_64/src/intel64/intel64_head.S @@ -91,6 +91,7 @@ .global g_cpu_count /* These are the page tables */ + .global g_pml4_low .global g_pdpt_low .global g_pd_low .global g_pt_low @@ -336,7 +337,7 @@ start64_init: * 4KiB page table is used. * Kernel mapped to 1GB HiMem */ - lea pml4, %eax + lea g_pml4_low, %eax mov %eax, %cr3 movl $MSR_MTRR_DEF_TYPE, %ecx @@ -758,7 +759,7 @@ mxcsr_mem: .long 0x00001f80 .align(PAGE_SIZE) -pml4: +g_pml4_low: .quad g_pdpt_low + X86_PAGE_PRESENT + X86_PAGE_WR .align(PAGE_SIZE) diff --git a/arch/xtensa/src/esp32s3/esp32s3_addrenv.c b/arch/xtensa/src/esp32s3/esp32s3_addrenv.c index 32c3390bb02..3d39ff82b39 100644 --- a/arch/xtensa/src/esp32s3/esp32s3_addrenv.c +++ b/arch/xtensa/src/esp32s3/esp32s3_addrenv.c @@ -419,6 +419,43 @@ int up_addrenv_select(const arch_addrenv_t *addrenv) return OK; } +/**************************************************************************** + * Name: up_addrenv_select_kernel + * + * Description: + * Leave the address environment of this CPU: invalidate the user windows, + * so that the address environment that was resident can be destroyed. + * + * Input Parameters: + * None + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. + * + ****************************************************************************/ + +int up_addrenv_select_kernel(void) +{ + irqstate_t flags; + uint32_t cache_state; + + flags = enter_critical_section(); + + cache_state = esp32s3_dcache_suspend(false); + + esp32s3_mmu_unmap(ESP32S3_TEXT_VBASE, CONFIG_ARCH_TEXT_NPAGES); + esp32s3_mmu_unmap(ESP32S3_DATA_VBASE, CONFIG_ARCH_DATA_NPAGES); + esp32s3_mmu_unmap(ESP32S3_HEAP_VBASE, CONFIG_ARCH_HEAP_NPAGES); + + esp32s3_icache_invalidate_all(); + esp32s3_dcache_resume(cache_state); + + g_current_addrenv = NULL; + + leave_critical_section(flags); + return OK; +} + /**************************************************************************** * Name: esp32s3_addrenv_coherent * diff --git a/include/nuttx/arch.h b/include/nuttx/arch.h index ff8eb9c2a03..6cc023dcdae 100644 --- a/include/nuttx/arch.h +++ b/include/nuttx/arch.h @@ -1087,6 +1087,7 @@ int up_copy_section(FAR void *dest, FAR const void *src, size_t n); * address environment * up_addrenv_heapsize - Returns the size of the initial heap allocation. * up_addrenv_select - Instantiate an address environment + * up_addrenv_select_kernel - Select the page tables of the kernel alone * up_addrenv_clone - Copy an address environment from one location to * another. * @@ -1313,6 +1314,26 @@ ssize_t up_addrenv_heapsize(FAR const arch_addrenv_t *addrenv); int up_addrenv_select(FAR const arch_addrenv_t *addrenv); #endif +/**************************************************************************** + * Name: up_addrenv_select_kernel + * + * Description: + * Select the page tables of the kernel alone on this CPU, so that the + * address environment that the CPU used before can be destroyed. Each + * architecture that supports CONFIG_BUILD_KERNEL must provide it. + * + * Input Parameters: + * None + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. + * + ****************************************************************************/ + +#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL) +int up_addrenv_select_kernel(void); +#endif + /**************************************************************************** * Name: up_addrenv_coherent *
