From 362a6d473652d6ec6489f334b6fce78e09ef7ad2 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Thu, 5 Nov 2026 06:25:03 -0500 Subject: [PATCH 01/47] mm/execmem: confine executable allocations to a fixed physical window Add CONFIG_EXECMEM_CONFINE: an optional backing mode for execmem ranges flagged EXECMEM_CONFINE. Such ranges keep their virtual placement (so a module stays within branch range of the kernel) but draw their physical backing pages from a single fixed window registered by the architecture via execmem_confine_init(), rather than from arbitrary buddy pages. This lets a platform keep every run-time executable physical page (modules, BPF, kprobes, ftrace) inside a hardware-enforceable code region such as an Apple-silicon CTXR window, which is a prerequisite for running the kernel at that privilege level. A confined allocation reserves its virtual area with __get_vm_area_node() inside the range, pulls a physically-contiguous chunk from a gen_pool over the window, and maps it with vmap_pages_range(); the backing pages are published on the vm_struct so set_memory_rox() can mirror permission changes onto their linear alias. If the pool is exhausted the allocation fails, with no buddy fallback, mirroring a hardware execute-range fault. A verifier sweeps for any executable mapping that resolves outside the window (the page-table sweep itself is provided per-architecture); when the window is enforcing a violation is fatal. "ctxr_exec_selftest" adds a boot-time positive/negative self-check. Signed-off-by: Diljit Singh --- include/linux/execmem.h | 65 ++++++ mm/Kconfig | 13 ++ mm/Makefile | 1 + mm/execmem.c | 15 ++ mm/execmem_confine.c | 433 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 527 insertions(+) create mode 100644 mm/execmem_confine.c diff --git a/include/linux/execmem.h b/include/linux/execmem.h index 1bd34925d1aa9..b402c11622b7a 100644 --- a/include/linux/execmem.h +++ b/include/linux/execmem.h @@ -48,10 +48,16 @@ enum execmem_type { * enum execmem_range_flags - options for executable memory allocations * @EXECMEM_KASAN_SHADOW: allocate kasan shadow * @EXECMEM_ROX_CACHE: allocations should use ROX cache of huge pages + * @EXECMEM_CONFINE: back allocations for this range with physical + * pages drawn from the confined executable window + * (see CONFIG_EXECMEM_CONFINE). The virtual range + * is still taken from [start, end); only the + * physical backing is restricted. */ enum execmem_range_flags { EXECMEM_KASAN_SHADOW = (1 << 0), EXECMEM_ROX_CACHE = (1 << 1), + EXECMEM_CONFINE = (1 << 2), }; #ifdef CONFIG_ARCH_HAS_EXECMEM_ROX @@ -204,4 +210,63 @@ void execmem_init(void); static inline void execmem_init(void) {} #endif +#ifdef CONFIG_EXECMEM_CONFINE +struct gen_pool; +struct execmem_range; + +/** + * execmem_confine_init - register the confined executable physical window + * @base: physical base of the window + * @size: size of the window in bytes + * @pool: gen_pool that hands out page-aligned chunks of the window + * (entries added with linear-map virtual addresses) + * @enforce: if true, a confinement violation found by the verifier is + * fatal; if false it is only reported + * + * Must be called before the first confined allocation, i.e. before + * execmem_arch_setup() marks any range with EXECMEM_CONFINE. + * + * Return: 0 on success, negative errno otherwise. + */ +int execmem_confine_init(phys_addr_t base, phys_addr_t size, + struct gen_pool *pool, bool enforce); + +/* True once a window has been registered. */ +bool execmem_confine_active(void); + +/* True if confinement violations are fatal. */ +bool execmem_confine_enforced(void); + +/* Window bounds, valid only when execmem_confine_active(). */ +void execmem_confine_window(phys_addr_t *base, phys_addr_t *size); + +/* + * Internal backing allocator/free used by mm/execmem.c for EXECMEM_CONFINE + * ranges. Not for general use. + */ +void *execmem_confine_alloc(struct execmem_range *range, size_t size, + pgprot_t pgprot, unsigned long vm_flags); +bool execmem_confine_free(void *ptr); + +/* + * Verify that no EL1-executable mapping resolves to a physical page outside + * the window. Returns the number of violations; fills in the number of + * executable leaf mappings inspected when @nr_exec is non-NULL. Implemented + * generically for the confined allocations; the page-table sweep is provided + * per-architecture by execmem_confine_arch_verify(). + */ +unsigned long execmem_confine_verify(unsigned long *nr_exec); + +/* + * Architecture page-table sweep. Walk the kernel page tables and, for every + * privileged-executable leaf entry, add 1 to *@nr_exec and, if its output + * physical address lies outside [base, base + size), add 1 to the return + * value. A weak default returns 0 (no sweep available). + */ +unsigned long execmem_confine_arch_verify(phys_addr_t base, phys_addr_t size, + unsigned long *nr_exec); +#else +static inline bool execmem_confine_active(void) { return false; } +#endif /* CONFIG_EXECMEM_CONFINE */ + #endif /* _LINUX_EXECMEM_ALLOC_H */ diff --git a/mm/Kconfig b/mm/Kconfig index 587af2f594350..3dcecd4a28e62 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -1474,6 +1474,19 @@ config IOMMU_MM_DATA config EXECMEM bool +config EXECMEM_CONFINE + bool + depends on EXECMEM && MMU + select GENERIC_ALLOCATOR + help + Back executable-memory ranges flagged EXECMEM_CONFINE with physical + pages drawn from a single fixed window registered by the architecture, + rather than from arbitrary buddy pages. This keeps every run-time + executable physical page (modules, BPF, kprobes, ftrace) inside a + hardware-enforceable code region such as an Apple-silicon CTXR window. + + Selected by architectures that provide a window; no effect otherwise. + config NUMA_MEMBLKS bool diff --git a/mm/Makefile b/mm/Makefile index 9fa4e1fb7e1ea..188db871956e8 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -145,6 +145,7 @@ obj-$(CONFIG_PAGE_REPORTING) += page_reporting.o obj-$(CONFIG_GENERIC_IOREMAP) += ioremap.o obj-$(CONFIG_SHRINKER_DEBUG) += shrinker_debug.o obj-$(CONFIG_EXECMEM) += execmem.o +obj-$(CONFIG_EXECMEM_CONFINE) += execmem_confine.o obj-$(CONFIG_TMPFS_QUOTA) += shmem_quota.o obj-$(CONFIG_LAZY_MMU_MODE_KUNIT_TEST) += tests/lazy_mmu_mode_kunit.o obj-$(CONFIG_MEM_ALLOC_PROFILING) += alloc_tag.o diff --git a/mm/execmem.c b/mm/execmem.c index 74a178a87e758..cc68c2cc0f55d 100644 --- a/mm/execmem.c +++ b/mm/execmem.c @@ -36,6 +36,16 @@ static void *execmem_vmalloc(struct execmem_range *range, size_t size, unsigned long end = range->end; void *p; +#ifdef CONFIG_EXECMEM_CONFINE + /* + * Confined ranges draw their physical backing from a fixed window + * rather than from arbitrary buddy pages, so that every executable + * physical page stays inside a CTXR-enrolled region. + */ + if (range->flags & EXECMEM_CONFINE) + return execmem_confine_alloc(range, size, pgprot, vm_flags); +#endif + if (kasan) vm_flags |= VM_DEFER_KMEMLEAK; @@ -503,6 +513,11 @@ void execmem_free(void *ptr) */ WARN_ON(in_interrupt()); +#ifdef CONFIG_EXECMEM_CONFINE + if (execmem_confine_free(ptr)) + return; +#endif + if (!execmem_cache_free(ptr)) vfree(ptr); } diff --git a/mm/execmem_confine.c b/mm/execmem_confine.c new file mode 100644 index 0000000000000..fc92f6628d53f --- /dev/null +++ b/mm/execmem_confine.c @@ -0,0 +1,433 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Confined executable memory. + * + * Some platforms can only execute privileged code whose *physical* address + * lies inside a fixed, hardware-enforced window (for example an Apple-silicon + * CTXR "code text execute range" armed by the boot firmware). To run at such a + * privilege level the kernel must keep every executable physical page inside + * that window: the static image is placed there by the bootloader, and every + * run-time executable allocation (modules, BPF, kprobes, ftrace trampolines) + * must draw its backing pages from it too. + * + * execmem already funnels all run-time executable allocations through a small + * set of ranges. This file backs the ranges flagged EXECMEM_CONFINE with pages + * taken from a gen_pool carved over the window, mapped into the range's virtual + * area so that branch-reachability (the +/-2G module window) is preserved while + * the physical backing stays confined. + * + * The architecture registers the window and pool via execmem_confine_init() and + * sets EXECMEM_CONFINE on the relevant ranges in execmem_arch_setup(). + */ + +#define pr_fmt(fmt) "execmem-confine: " fmt + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "internal.h" +#include "vmalloc.h" + +struct confine_chunk { + struct vm_struct *area; + unsigned long pool_va; /* linear-map VA handed out by the pool */ + size_t size; + struct page **pages; +}; + +static phys_addr_t confine_base __ro_after_init; +static phys_addr_t confine_size __ro_after_init; +static struct gen_pool *confine_pool __ro_after_init; +static bool confine_enforce __ro_after_init; +static bool confine_ready __ro_after_init; + +/* keyed by mapped-VA PFN -> struct confine_chunk */ +static DEFINE_XARRAY(confine_chunks); +/* serialises pool (de)allocation and the xarray */ +static DEFINE_MUTEX(confine_lock); + +int __init execmem_confine_init(phys_addr_t base, phys_addr_t size, + struct gen_pool *pool, bool enforce) +{ + if (WARN_ON(confine_ready)) + return -EBUSY; + if (WARN_ON(!pool || !size)) + return -EINVAL; + + confine_base = base; + confine_size = size; + confine_pool = pool; + confine_enforce = enforce; + smp_wmb(); + confine_ready = true; + + { + phys_addr_t end = base + size; + + pr_info("window [%pa, %pa) (%llu MiB), %s\n", + &base, &end, (unsigned long long)(size >> 20), + enforce ? "enforcing" : "reporting"); + } + return 0; +} + +bool execmem_confine_active(void) +{ + return confine_ready; +} + +bool execmem_confine_enforced(void) +{ + return confine_ready && confine_enforce; +} + +void execmem_confine_window(phys_addr_t *base, phys_addr_t *size) +{ + if (base) + *base = confine_base; + if (size) + *size = confine_size; +} + +static bool phys_in_window(phys_addr_t pa) +{ + return pa >= confine_base && pa < confine_base + confine_size; +} + +void *execmem_confine_alloc(struct execmem_range *range, size_t size, + pgprot_t pgprot, unsigned long vm_flags) +{ + struct confine_chunk *ck; + struct vm_struct *area; + struct page **pages; + unsigned long pool_va, vaddr; + unsigned int i, nr; + int err; + + if (WARN_ON_ONCE(!confine_ready)) + return NULL; + + size = PAGE_ALIGN(size); + nr = size >> PAGE_SHIFT; + + /* + * Reserve the virtual area inside the range (then the fallback range) + * so module code keeps its +/-2G reachability to the kernel image; only + * the physical backing is confined. + */ + area = __get_vm_area_node(size, range->alignment, PAGE_SHIFT, + VM_ALLOC | vm_flags, range->start, range->end, + NUMA_NO_NODE, GFP_KERNEL, + __builtin_return_address(0)); + if (!area && range->fallback_start) + area = __get_vm_area_node(size, range->alignment, PAGE_SHIFT, + VM_ALLOC | vm_flags, + range->fallback_start, + range->fallback_end, NUMA_NO_NODE, + GFP_KERNEL, __builtin_return_address(0)); + if (!area) + return NULL; + + pages = kmalloc_array(nr, sizeof(*pages), GFP_KERNEL); + if (!pages) + goto err_area; + + ck = kzalloc(sizeof(*ck), GFP_KERNEL); + if (!ck) + goto err_pages; + + mutex_lock(&confine_lock); + pool_va = gen_pool_alloc(confine_pool, size); + mutex_unlock(&confine_lock); + if (!pool_va) { + pr_warn_ratelimited("window exhausted, cannot satisfy %zu bytes\n", + size); + goto err_ck; + } + + for (i = 0; i < nr; i++) + pages[i] = virt_to_page((void *)(pool_va + ((unsigned long)i << PAGE_SHIFT))); + + vaddr = (unsigned long)area->addr; + err = vmap_pages_range(vaddr, vaddr + size, pgprot, pages, PAGE_SHIFT); + if (err) + goto err_pool; + + /* + * Publish the backing pages on the vm_struct. arm64's set_memory_rox() + * (used by the module loader to make text executable) walks area->pages + * to mirror the permission change onto the pages' linear-map alias when + * rodata_full is on; without this it would dereference a NULL array. + */ + area->pages = pages; + area->nr_pages = nr; + + ck->area = area; + ck->pool_va = pool_va; + ck->size = size; + ck->pages = pages; + + mutex_lock(&confine_lock); + err = xa_err(xa_store(&confine_chunks, vaddr >> PAGE_SHIFT, ck, + GFP_KERNEL)); + mutex_unlock(&confine_lock); + if (err) + goto err_map; + + if ((range->flags & EXECMEM_KASAN_SHADOW) && + kasan_alloc_module_shadow(area->addr, size, GFP_KERNEL) < 0) + goto err_xa; + + return area->addr; + +err_xa: + mutex_lock(&confine_lock); + xa_erase(&confine_chunks, vaddr >> PAGE_SHIFT); + mutex_unlock(&confine_lock); +err_map: + free_vm_area(area); /* unmaps the range and frees bookkeeping */ + mutex_lock(&confine_lock); + gen_pool_free(confine_pool, pool_va, size); + mutex_unlock(&confine_lock); + kfree(ck); + kfree(pages); + return NULL; +err_pool: + mutex_lock(&confine_lock); + gen_pool_free(confine_pool, pool_va, size); + mutex_unlock(&confine_lock); +err_ck: + kfree(ck); +err_pages: + kfree(pages); +err_area: + free_vm_area(area); + return NULL; +} + +bool execmem_confine_free(void *ptr) +{ + unsigned long vaddr = (unsigned long)ptr; + struct confine_chunk *ck; + unsigned int nr; + + if (!confine_ready || !ptr) + return false; + + mutex_lock(&confine_lock); + ck = xa_load(&confine_chunks, vaddr >> PAGE_SHIFT); + if (ck) + xa_erase(&confine_chunks, vaddr >> PAGE_SHIFT); + mutex_unlock(&confine_lock); + if (!ck) + return false; + + nr = ck->size >> PAGE_SHIFT; + + /* + * We bypass vfree(), so VM_FLUSH_RESET_PERMS does not run for us: drop + * the mapping back to non-executable, writable before tearing it down, + * so the window never retains a stale executable alias. + */ + set_memory_nx(vaddr, nr); + set_memory_rw(vaddr, nr); + + free_vm_area(ck->area); + + mutex_lock(&confine_lock); + gen_pool_free(confine_pool, ck->pool_va, ck->size); + mutex_unlock(&confine_lock); + + kfree(ck->pages); + kfree(ck); + return true; +} + +/* Weak default: no architecture page-table sweep available. */ +unsigned long __weak execmem_confine_arch_verify(phys_addr_t base, + phys_addr_t size, + unsigned long *nr_exec) +{ + return 0; +} + +unsigned long execmem_confine_verify(unsigned long *nr_exec_out) +{ + unsigned long nr_exec = 0, violations = 0; + struct confine_chunk *ck; + unsigned long idx; + + if (!confine_ready) { + if (nr_exec_out) + *nr_exec_out = 0; + return 0; + } + + /* + * Registry sanity: every confined allocation is backed by pool pages, + * which are by construction inside the window. Re-check anyway so the + * self-test actually inspects the physical backing it relies on. + */ + mutex_lock(&confine_lock); + xa_for_each(&confine_chunks, idx, ck) { + phys_addr_t pa = virt_to_phys((void *)ck->pool_va); + phys_addr_t end = pa + ck->size - 1; + + if (!phys_in_window(pa) || !phys_in_window(end)) { + pr_err("confined chunk at %#lx backs out-of-window phys [%pa, %pa]\n", + (unsigned long)ck->area->addr, &pa, &end); + violations++; + } + } + mutex_unlock(&confine_lock); + + /* Architecture page-table sweep over all kernel executable mappings. */ + violations += execmem_confine_arch_verify(confine_base, confine_size, + &nr_exec); + + if (nr_exec_out) + *nr_exec_out = nr_exec; + return violations; +} + +#ifdef CONFIG_DEBUG_FS +static int confine_debug_show(struct seq_file *m, void *v) +{ + unsigned long nr_exec = 0, violations; + + seq_printf(m, "window-base: %pa\n", &confine_base); + seq_printf(m, "window-size: %llu MiB\n", + (unsigned long long)(confine_size >> 20)); + seq_printf(m, "enforce: %d\n", confine_enforce); + if (confine_pool) { + seq_printf(m, "pool-total: %zu KiB\n", + gen_pool_size(confine_pool) >> 10); + seq_printf(m, "pool-available: %zu KiB\n", + gen_pool_avail(confine_pool) >> 10); + } + + violations = execmem_confine_verify(&nr_exec); + seq_printf(m, "exec-mappings: %lu\n", nr_exec); + seq_printf(m, "violations: %lu\n", violations); + seq_printf(m, "result: %s\n", violations ? "FAIL" : "PASS"); + return 0; +} +DEFINE_SHOW_ATTRIBUTE(confine_debug); +#endif + +/* + * Boot self-test (enabled with "ctxr_exec_selftest" on the command line). + * + * In a VM there is no hardware CTXR to fault on an escaping fetch, so we prove + * two things in software: + * 1. a confined execmem allocation is physically inside the window, and + * 2. the page-table sweep actually catches an executable mapping placed + * outside the window (i.e. the verifier is not a no-op). + * The negative case only *maps* an out-of-window page and inspects the tables; + * nothing ever executes from it, and it is torn down immediately. + */ +static bool ctxr_selftest __initdata; + +static int __init execmem_confine_selftest_param(char *s) +{ + ctxr_selftest = true; + return 0; +} +early_param("ctxr_exec_selftest", execmem_confine_selftest_param); + +static void __init execmem_confine_selftest(void) +{ + unsigned long nr_exec = 0, before, after; + struct vm_struct *area; + struct page *page; + void *p; + bool ok; + + /* 1. positive: a confined allocation lands inside the window. */ + p = execmem_alloc(EXECMEM_MODULE_TEXT, PAGE_SIZE); + if (p) { + struct confine_chunk *ck; + + mutex_lock(&confine_lock); + ck = xa_load(&confine_chunks, (unsigned long)p >> PAGE_SHIFT); + ok = ck && phys_in_window(virt_to_phys((void *)ck->pool_va)); + mutex_unlock(&confine_lock); + pr_info("selftest 1 (confined alloc in window): %s\n", + ok ? "PASS" : "FAIL"); + execmem_free(p); + } else { + pr_info("selftest 1 (confined alloc in window): FAIL (no memory)\n"); + } + + /* 2. negative: an out-of-window executable mapping must be detected. */ + before = execmem_confine_arch_verify(confine_base, confine_size, &nr_exec); + + page = alloc_page(GFP_KERNEL); /* buddy never returns window pages */ + if (!page) { + pr_info("selftest 2 (detect escape): SKIP (no memory)\n"); + goto done; + } + area = __get_vm_area_node(PAGE_SIZE, PAGE_SIZE, PAGE_SHIFT, VM_ALLOC, + VMALLOC_START, VMALLOC_END, NUMA_NO_NODE, + GFP_KERNEL, __builtin_return_address(0)); + if (!area) { + __free_page(page); + pr_info("selftest 2 (detect escape): SKIP (no vm area)\n"); + goto done; + } + + if (vmap_pages_range((unsigned long)area->addr, + (unsigned long)area->addr + PAGE_SIZE, + PAGE_KERNEL_EXEC, &page, PAGE_SHIFT)) { + free_vm_area(area); + __free_page(page); + pr_info("selftest 2 (detect escape): SKIP (map failed)\n"); + goto done; + } + + after = execmem_confine_arch_verify(confine_base, confine_size, &nr_exec); + pr_info("selftest 2 (detect escape): %s (%lu -> %lu violations)\n", + after > before ? "PASS" : "FAIL", before, after); + + set_memory_nx((unsigned long)area->addr, 1); + free_vm_area(area); + __free_page(page); +done: + return; +} + +static int __init execmem_confine_late_init(void) +{ + unsigned long nr_exec = 0, violations; + + if (!confine_ready) + return 0; + +#ifdef CONFIG_DEBUG_FS + debugfs_create_file("execmem_confine", 0400, NULL, NULL, + &confine_debug_fops); +#endif + + if (ctxr_selftest) + execmem_confine_selftest(); + + violations = execmem_confine_verify(&nr_exec); + pr_info("boot verify: %lu executable mapping(s), %lu violation(s): %s\n", + nr_exec, violations, violations ? "FAIL" : "PASS"); + + if (violations && confine_enforce) + panic("execmem-confine: %lu executable mapping(s) outside the window\n", + violations); + + return 0; +} +late_initcall(execmem_confine_late_init); From 5a4f741f646504f0f2b8563f392af538c266b4d9 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Thu, 5 Nov 2026 06:25:03 -0500 Subject: [PATCH 02/47] arm64: confine kernel executable code to a CTXR physical window On T8140/M4-class Apple-silicon machines the boot firmware arms a per-CPU code text execute range (CTXR) on the secondary cores: at the privileged exception level an instruction may only be fetched from inside a fixed physical window. To run the kernel there rather than dropping it to EL1, every executable physical page must live inside one window. Add CONFIG_ARM64_CTXR_EXECMEM, the arm64 end of the EXECMEM_CONFINE contract: - ctxr_exec_reserve(), from arm64_memblock_init(), learns the window from /chosen/aurora,ctxr-exec-window (emitted by the EL2 monitor) or the ctxr_exec_window= command line (for VM bring-up), checks that the kernel image fits, and reserves it from memblock. - execmem_arch_setup() carves the non-image remainder into a gen_pool, registers it with the confinement core, and flags the executable ranges (module text, BPF, kprobes and, by inheritance, ftrace) with EXECMEM_CONFINE. Module data is left on ordinary vmalloc backing. - execmem_confine_arch_verify() provides the page-table sweep used by the verifier: every privileged-executable leaf outside the window is reported, standing in for the hardware fault when testing in a VM. The option is inert unless a window is supplied at boot, so it is enabled in asahi.config and is a no-op on machines that do not need it. Signed-off-by: Diljit Singh --- Documentation/arch/arm64/ctxr-execmem.rst | 72 +++++ Documentation/arch/arm64/index.rst | 1 + arch/arm64/Kconfig | 24 ++ arch/arm64/configs/asahi.config | 5 + arch/arm64/include/asm/ctxr_execmem.h | 31 +++ arch/arm64/mm/Makefile | 1 + arch/arm64/mm/ctxr_execmem.c | 311 ++++++++++++++++++++++ arch/arm64/mm/init.c | 25 ++ 8 files changed, 470 insertions(+) create mode 100644 Documentation/arch/arm64/ctxr-execmem.rst create mode 100644 arch/arm64/include/asm/ctxr_execmem.h create mode 100644 arch/arm64/mm/ctxr_execmem.c diff --git a/Documentation/arch/arm64/ctxr-execmem.rst b/Documentation/arch/arm64/ctxr-execmem.rst new file mode 100644 index 0000000000000..b0c5710a0b13f --- /dev/null +++ b/Documentation/arch/arm64/ctxr-execmem.rst @@ -0,0 +1,72 @@ +.. SPDX-License-Identifier: GPL-2.0 + +========================================== +Confining kernel executable code (CTXR) +========================================== + +Some Apple-silicon machines (T8140/M4-class and later) have boot firmware that +arms a per-CPU *code text execute range* (CTXR) on the secondary cores: at the +privileged exception level, an instruction may only be fetched from inside a +fixed physical window. Outside it, the fetch takes an instruction abort. + +To run the kernel at EL2 behind such a window (instead of dropping it to EL1), +**every** executable physical page must live inside one window: the static image +(placed there by the boot monitor) and every run-time executable allocation. + +``CONFIG_ARM64_CTXR_EXECMEM`` makes that possible. It is inert unless a window +is supplied at boot, so it is safe to leave enabled on machines that do not need +it. + +Supplying the window +==================== + +Device tree (production) + The EL2 monitor emits, under ``/chosen``:: + + aurora,ctxr-exec-window = ; + aurora,ctxr-exec-enforce = <1>; /* optional, default 1 */ + + A ``base`` of 0 tells the kernel to anchor the window on its own loaded + image (rounded down to 2 MiB). ``size`` is the window size in bytes. + +Command line (bring-up / VM) + :: + + ctxr_exec_window=[@] e.g. ctxr_exec_window=1G + ctxr_exec_enforce=0 (default: enforcing) + + The command line overrides the device tree. ``size`` accepts the usual + ``K``/``M``/``G`` suffixes; if ``@base`` is omitted it is derived from the + kernel image. + +How it works +============ + +* ``ctxr_exec_reserve()`` (from ``arm64_memblock_init()``) parses the window, + checks that ``[_text, _end)`` fits inside it, and reserves it from memblock. +* ``execmem_arch_setup()`` carves the non-image remainder of the window into a + ``gen_pool`` and registers it with the generic confinement core + (``CONFIG_EXECMEM_CONFINE``). The executable ranges (module text, BPF, + kprobes and, by inheritance, ftrace) are flagged ``EXECMEM_CONFINE``. +* A confined allocation reserves its virtual area in the normal module range — + preserving the +/-2 GiB branch reachability to the kernel — but backs it with + physical pages drawn from the window. Module *data* is not confined. +* If the window's pool is exhausted the allocation fails, exactly as a hardware + CTXR fault would prevent executing from outside the window; there is no buddy + fallback. + +Verifying confinement +===================== + +The feature carries its own verifier, which is what stands in for the hardware +fault when testing in a VM: + +* At ``late_initcall`` the kernel sweeps its page tables and reports the number + of EL1-executable leaf mappings and how many resolve outside the window. When + the window is enforcing, a non-zero count is fatal. +* ``/sys/kernel/debug/execmem_confine`` re-runs the sweep on read and reports + window bounds, pool usage and ``PASS``/``FAIL``. +* Booting with ``ctxr_exec_selftest`` runs two extra checks: that a confined + allocation is physically inside the window, and that the sweep actually + detects an executable mapping placed *outside* it (proving the verifier is + not a no-op). diff --git a/Documentation/arch/arm64/index.rst b/Documentation/arch/arm64/index.rst index d335b9e43fe99..cd82b573e7bfd 100644 --- a/Documentation/arch/arm64/index.rst +++ b/Documentation/arch/arm64/index.rst @@ -24,6 +24,7 @@ ARM64 Architecture booting cpu-feature-registers cpu-hotplug + ctxr-execmem elf_hwcaps gcs hugetlbpage diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index a108e83dbc8a4..ea591dc3d35e4 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -2535,6 +2535,30 @@ config RANDOMIZE_MODULE_REGION_FULL the region is exhausted. In this particular case of region exhaustion, modules might be able to fall back to a larger 2GB area. +config ARM64_CTXR_EXECMEM + bool "Confine kernel executable code to a fixed physical window (CTXR)" + depends on EXECMEM && MMU + select EXECMEM_CONFINE + select GENERIC_ALLOCATOR + select PTDUMP + help + On some Apple-silicon machines the boot firmware arms a CTXR (code + text execute range) on the secondary CPUs: privileged code can only + be fetched from inside a fixed physical window. To run the kernel at + EL2 behind such a window (rather than dropping it to EL1), every + executable physical page must live inside it. + + With this option the kernel reserves a single window — described by + the EL2 monitor in /chosen/aurora,ctxr-exec-window, or by the + "ctxr_exec_window=" command-line option for VM bring-up — places the + static image inside it, and backs every run-time executable allocation + (modules, BPF, kprobes, ftrace) with pages drawn from the remainder. + A verifier sweeps the kernel page tables and reports (or, when the + window is enforcing, panics on) any executable mapping that escapes. + + This has no effect unless a window is supplied at boot. If unsure, + say N. + config CC_HAVE_STACKPROTECTOR_SYSREG def_bool $(cc-option,-mstack-protector-guard=sysreg -mstack-protector-guard-reg=sp_el0 -mstack-protector-guard-offset=0) diff --git a/arch/arm64/configs/asahi.config b/arch/arm64/configs/asahi.config index 870ff1b6b4dda..b0577fbb2084d 100644 --- a/arch/arm64/configs/asahi.config +++ b/arch/arm64/configs/asahi.config @@ -144,3 +144,8 @@ CONFIG_DMABUF_HEAPS=y CONFIG_DMABUF_HEAPS_SYSTEM=y CONFIG_UDMABUF=y CONFIG_DETECT_HUNG_TASK=y +# Confine kernel executable code to a fixed physical CTXR window so the kernel +# can run at EL2 on chips whose firmware locks the code-execute range. Inert +# unless the EL2 monitor supplies a window (/chosen/aurora,ctxr-exec-window) or +# the ctxr_exec_window= command-line option is given. +CONFIG_ARM64_CTXR_EXECMEM=y diff --git a/arch/arm64/include/asm/ctxr_execmem.h b/arch/arm64/include/asm/ctxr_execmem.h new file mode 100644 index 0000000000000..c9f5088821e95 --- /dev/null +++ b/arch/arm64/include/asm/ctxr_execmem.h @@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Confine the kernel's executable code to a fixed physical window so it can + * run at EL2 behind an Apple-silicon CTXR (code text execute range). + */ +#ifndef __ASM_CTXR_EXECMEM_H +#define __ASM_CTXR_EXECMEM_H + +#include + +#ifdef CONFIG_ARM64_CTXR_EXECMEM +/* + * Parse the window (from the kernel command line or the device tree), validate + * that the kernel image fits inside it, and reserve it from memblock. Called + * once from arm64_memblock_init(), while memblock is still authoritative. + */ +void ctxr_exec_reserve(void); + +/* + * Build the gen_pool over the reserved window and register it with the execmem + * confinement core. Called from execmem_arch_setup(). Returns true when + * confinement is active and the caller should flag its executable ranges with + * EXECMEM_CONFINE. + */ +bool ctxr_exec_execmem_setup(void); +#else +static inline void ctxr_exec_reserve(void) {} +static inline bool ctxr_exec_execmem_setup(void) { return false; } +#endif + +#endif /* __ASM_CTXR_EXECMEM_H */ diff --git a/arch/arm64/mm/Makefile b/arch/arm64/mm/Makefile index 757f7eb07ccac..3c01c922582fb 100644 --- a/arch/arm64/mm/Makefile +++ b/arch/arm64/mm/Makefile @@ -6,6 +6,7 @@ obj-y := dma-mapping.o extable.o fault.o init.o \ obj-$(CONFIG_ARM64_CONTPTE) += contpte.o obj-$(CONFIG_HUGETLB_PAGE) += hugetlbpage.o obj-$(CONFIG_PTDUMP) += ptdump.o +obj-$(CONFIG_ARM64_CTXR_EXECMEM) += ctxr_execmem.o obj-$(CONFIG_PTDUMP_DEBUGFS) += ptdump_debugfs.o obj-$(CONFIG_TRANS_TABLE) += trans_pgd.o obj-$(CONFIG_TRANS_TABLE) += trans_pgd-asm.o diff --git a/arch/arm64/mm/ctxr_execmem.c b/arch/arm64/mm/ctxr_execmem.c new file mode 100644 index 0000000000000..7e1aa88e4c918 --- /dev/null +++ b/arch/arm64/mm/ctxr_execmem.c @@ -0,0 +1,311 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Confine the kernel's executable code to a fixed physical window. + * + * On Apple-silicon machines whose boot firmware arms a CTXR (code text execute + * range) on the secondary CPUs, privileged code may only be fetched from inside + * that hardware window. To run the kernel at EL2 rather than dropping it to EL1, + * every executable physical page must live inside one such window: the static + * image (placed there by the bootloader) and every run-time executable + * allocation (modules, BPF, kprobes, ftrace). + * + * This file owns the arm64 end of that contract: + * - it learns the window from the device tree (/chosen/aurora,ctxr-exec-window, + * emitted by the EL2 monitor) or the command line (ctxr_exec_window=, used + * for VM/simulated bring-up), + * - reserves it from memblock and checks the image fits, + * - carves the non-image remainder into a gen_pool and hands it to the generic + * execmem confinement core, and + * - provides the page-table sweep that the verifier uses to prove, in a VM + * without real CTXR hardware, that no EL1-executable mapping escapes. + */ + +#define pr_fmt(fmt) "ctxr-execmem: " fmt + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +/* Window granularity when the base is derived rather than given. */ +#define CTXR_WINDOW_ALIGN SZ_2M +/* Default size if only "ctxr_exec_window" with no value is given. */ +#define CTXR_WINDOW_DEFAULT SZ_1G + +static phys_addr_t ctxr_base __initdata; +static phys_addr_t ctxr_size __initdata; +static bool ctxr_enforce __initdata = true; +static bool ctxr_active __initdata; + +static bool __init ctxr_parse_cmdline(phys_addr_t *base, phys_addr_t *size, + bool *enforce) +{ + const char *p; + char *endp; + + p = strstr(boot_command_line, "ctxr_exec_window="); + if (!p) + return false; + p += strlen("ctxr_exec_window="); + + *size = memparse(p, &endp); + if (!*size) + *size = CTXR_WINDOW_DEFAULT; + if (*endp == '@') + *base = memparse(endp + 1, NULL); + else + *base = 0; /* derive from the image below */ + + p = strstr(boot_command_line, "ctxr_exec_enforce="); + if (p) + *enforce = (p[strlen("ctxr_exec_enforce=")] != '0'); + + return true; +} + +static bool __init ctxr_parse_dt(phys_addr_t *base, phys_addr_t *size, + bool *enforce) +{ + unsigned long chosen; + const __be32 *prop; + int len; + + chosen = of_get_flat_dt_subnode_by_name(of_get_flat_dt_root(), "chosen"); + if ((long)chosen < 0) + return false; + + /* */ + prop = of_get_flat_dt_prop(chosen, "aurora,ctxr-exec-window", &len); + if (!prop || len != 4 * sizeof(__be32)) + return false; + + *base = ((u64)be32_to_cpu(prop[0]) << 32) | be32_to_cpu(prop[1]); + *size = ((u64)be32_to_cpu(prop[2]) << 32) | be32_to_cpu(prop[3]); + + prop = of_get_flat_dt_prop(chosen, "aurora,ctxr-exec-enforce", &len); + if (prop && len == sizeof(__be32)) + *enforce = !!be32_to_cpu(prop[0]); + + return *size != 0; +} + +void __init ctxr_exec_reserve(void) +{ + phys_addr_t base = 0, size = 0, img_start, img_end; + bool enforce = true; + + if (!ctxr_parse_cmdline(&base, &size, &enforce) && + !ctxr_parse_dt(&base, &size, &enforce)) + return; + + if (!size) + return; + + img_start = __pa_symbol(_text); + img_end = __pa_symbol(_end); + + if (!base) + base = round_down(img_start, CTXR_WINDOW_ALIGN); + + if (img_start < base || img_end > base + size) { + pr_err("kernel image [%pa, %pa) does not fit window [%pa, +%llu MiB)\n", + &img_start, &img_end, &base, + (unsigned long long)(size >> 20)); + goto fail; + } + + if (base < memblock_start_of_DRAM() || + base + size > memblock_end_of_DRAM()) { + pr_err("window [%pa, +%llu MiB) is not within DRAM\n", + &base, (unsigned long long)(size >> 20)); + goto fail; + } + + if (memblock_reserve(base, size)) { + pr_err("failed to reserve window [%pa, +%llu MiB)\n", + &base, (unsigned long long)(size >> 20)); + goto fail; + } + + ctxr_base = base; + ctxr_size = size; + ctxr_enforce = enforce; + ctxr_active = true; + + pr_info("reserved exec window [%pa, +%llu MiB), %s\n", + &base, (unsigned long long)(size >> 20), + enforce ? "enforcing" : "reporting"); + return; + +fail: + if (enforce) + panic("ctxr-execmem: cannot establish the executable window\n"); + pr_warn("confinement disabled\n"); +} + +static void __init ctxr_pool_add_gap(struct gen_pool *pool, phys_addr_t from, + phys_addr_t to) +{ + from = PAGE_ALIGN(from); + to = round_down(to, PAGE_SIZE); + if (to <= from) + return; + + if (gen_pool_add_virt(pool, (unsigned long)__va(from), from, to - from, + NUMA_NO_NODE)) + pr_err("failed to add [%pa, %pa) to the pool\n", &from, &to); +} + +bool __init ctxr_exec_execmem_setup(void) +{ + struct gen_pool *pool; + phys_addr_t img_start, img_end; + + if (!ctxr_active) + return false; + + pool = gen_pool_create(PAGE_SHIFT, NUMA_NO_NODE); + if (!pool) { + pr_err("failed to create the window pool\n"); + goto fail; + } + + /* The image occupies part of the window; the rest becomes the pool. */ + img_start = __pa_symbol(_text); + img_end = __pa_symbol(_end); + ctxr_pool_add_gap(pool, ctxr_base, img_start); + ctxr_pool_add_gap(pool, img_end, ctxr_base + ctxr_size); + + if (!gen_pool_size(pool)) { + pr_err("window has no free space for executable allocations\n"); + goto fail_pool; + } + + if (execmem_confine_init(ctxr_base, ctxr_size, pool, ctxr_enforce)) { + pr_err("failed to register the window\n"); + goto fail_pool; + } + + pr_info("executable pool: %zu MiB usable in a %llu MiB window\n", + gen_pool_size(pool) >> 20, + (unsigned long long)(ctxr_size >> 20)); + return true; + +fail_pool: + gen_pool_destroy(pool); +fail: + if (ctxr_enforce) + panic("ctxr-execmem: cannot build the executable pool\n"); + return false; +} + +/* + * Page-table sweep used by the confinement verifier. For every privileged + * (PXN-clear) executable leaf in the kernel page tables, count it and flag any + * whose physical output lies outside the window. This is what stands in, in a + * VM, for the hardware CTXR fault. + */ +struct ctxr_verify { + struct ptdump_state ptdump; + phys_addr_t base; + phys_addr_t end; + unsigned long nr_exec; + unsigned long violations; +}; + +static void ctxr_check_leaf(struct ctxr_verify *v, unsigned long addr, + pteval_t val, phys_addr_t leaf_size) +{ + phys_addr_t pa, last; + + if (!(val & PTE_VALID)) + return; + if (val & PTE_PXN) /* not executable at EL1 */ + return; + + v->nr_exec++; + + pa = PFN_PHYS(pte_pfn(__pte(val))); + last = pa + leaf_size - 1; + if (pa < v->base || last >= v->end) { + v->violations++; + pr_warn("EL1-exec mapping va %#lx -> phys [%pa, %pa] outside window\n", + addr, &pa, &last); + } +} + +static void ctxr_note_pte(struct ptdump_state *st, unsigned long addr, pte_t pte) +{ + ctxr_check_leaf(container_of(st, struct ctxr_verify, ptdump), addr, + pte_val(pte), PAGE_SIZE); +} + +static void ctxr_note_pmd(struct ptdump_state *st, unsigned long addr, pmd_t pmd) +{ + ctxr_check_leaf(container_of(st, struct ctxr_verify, ptdump), addr, + pmd_val(pmd), PMD_SIZE); +} + +static void ctxr_note_pud(struct ptdump_state *st, unsigned long addr, pud_t pud) +{ + ctxr_check_leaf(container_of(st, struct ctxr_verify, ptdump), addr, + pud_val(pud), PUD_SIZE); +} + +/* + * p4d/pgd leaves never carry executable kernel mappings on arm64, but the + * page-table walker calls every level's note callback (including for holes and + * a final flush), so each must be non-NULL. + */ +static void ctxr_note_p4d(struct ptdump_state *st, unsigned long addr, + p4d_t p4d) +{ +} + +static void ctxr_note_pgd(struct ptdump_state *st, unsigned long addr, + pgd_t pgd) +{ +} + +static void ctxr_note_flush(struct ptdump_state *st) +{ +} + +unsigned long execmem_confine_arch_verify(phys_addr_t base, phys_addr_t size, + unsigned long *nr_exec) +{ + struct ctxr_verify v = { + .base = base, + .end = base + size, + .ptdump = { + .note_page_pte = ctxr_note_pte, + .note_page_pmd = ctxr_note_pmd, + .note_page_pud = ctxr_note_pud, + .note_page_p4d = ctxr_note_p4d, + .note_page_pgd = ctxr_note_pgd, + .note_page_flush = ctxr_note_flush, + .range = (struct ptdump_range[]) { + { _PAGE_OFFSET(vabits_actual), ~0UL }, + { 0, 0 } + }, + }, + }; + + ptdump_walk_pgd(&v.ptdump, &init_mm, NULL); + + if (nr_exec) + *nr_exec = v.nr_exec; + return v.violations; +} diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c index a3acc7c802955..ce0e1b21bbd52 100644 --- a/arch/arm64/mm/init.c +++ b/arch/arm64/mm/init.c @@ -36,6 +36,7 @@ #include #include +#include #include #include #include @@ -298,6 +299,8 @@ void __init arm64_memblock_init(void) } early_init_fdt_scan_reserved_mem(); + + ctxr_exec_reserve(); } void __init bootmem_init(void) @@ -516,9 +519,20 @@ struct execmem_info __init *execmem_arch_setup(void) { unsigned long fallback_start = 0, fallback_end = 0; unsigned long start = 0, end = 0; + enum execmem_range_flags cflags = 0; module_init_limits(); + /* + * When a CTXR executable window is active, back the executable ranges + * (text, BPF, kprobes and — by inheritance — ftrace) with pages drawn + * from the window so every executable physical page stays confined. + * Module *data* (EXECMEM_MODULE_DATA, set explicitly below) is never + * executed and is left on ordinary vmalloc backing. + */ + if (ctxr_exec_execmem_setup()) + cflags = EXECMEM_CONFINE; + /* * Where possible, prefer to allocate within direct branch range of the * kernel such that no PLTs are necessary. @@ -545,18 +559,29 @@ struct execmem_info __init *execmem_arch_setup(void) .alignment = 1, .fallback_start = fallback_start, .fallback_end = fallback_end, + .flags = cflags, + }, + [EXECMEM_MODULE_DATA] = { + .start = start, + .end = end, + .pgprot = PAGE_KERNEL, + .alignment = 1, + .fallback_start = fallback_start, + .fallback_end = fallback_end, }, [EXECMEM_KPROBES] = { .start = VMALLOC_START, .end = VMALLOC_END, .pgprot = PAGE_KERNEL_ROX, .alignment = 1, + .flags = cflags, }, [EXECMEM_BPF] = { .start = VMALLOC_START, .end = VMALLOC_END, .pgprot = PAGE_KERNEL, .alignment = 1, + .flags = cflags, }, }, }; From 0a325c1dc7cb93473f3eeeb2428dc9ea9f41d177 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Thu, 5 Nov 2026 12:00:50 -0500 Subject: [PATCH 03/47] arm64: ctxr-execmem: keep reserved memory out of the pool, fit the window Build the executable pool only from memory that was free before the window was reserved. Snapshot for_each_free_mem_range() over the window just before memblock_reserve(), so anything already reserved at that point -- the image, the device tree blob, an initrd, /reserved-memory and memreserve entries, EFI runtime regions and the UEFI memory map -- and any hole or no-map range stays out of the pool. Previously the pool was the window minus the image only, so a lowest-first executable allocation could overwrite a live DTB, an initrd or EFI runtime memory that a loader had placed inside the window. When no base is given and anchoring the window on the image would run past the end of DRAM (a loader placed the image within the window size of the top), slide the window down to end at DRAM's end, still covering the image, instead of panicking. List the pool's ranges in /sys/kernel/debug/execmem_confine. Signed-off-by: Diljit Singh --- arch/arm64/mm/ctxr_execmem.c | 81 ++++++++++++++++++++++++++++++------ mm/execmem_confine.c | 11 +++++ 2 files changed, 79 insertions(+), 13 deletions(-) diff --git a/arch/arm64/mm/ctxr_execmem.c b/arch/arm64/mm/ctxr_execmem.c index 7e1aa88e4c918..5c5bf8d7ab8a7 100644 --- a/arch/arm64/mm/ctxr_execmem.c +++ b/arch/arm64/mm/ctxr_execmem.c @@ -49,6 +49,20 @@ static phys_addr_t ctxr_size __initdata; static bool ctxr_enforce __initdata = true; static bool ctxr_active __initdata; +/* + * Free, linear-mapped RAM inside the window, snapshotted just before the + * window is reserved. Anything already reserved at that point (the kernel + * image, the device tree, an initrd, /reserved-memory and memreserve entries, + * firmware or loader regions) and any hole or no-map region is excluded, so + * the executable pool can never hand out memory something else owns. + */ +#define CTXR_MAX_FREE 64 +static struct { + phys_addr_t start; + phys_addr_t end; +} ctxr_free[CTXR_MAX_FREE] __initdata; +static int ctxr_nr_free __initdata; + static bool __init ctxr_parse_cmdline(phys_addr_t *base, phys_addr_t *size, bool *enforce) { @@ -101,9 +115,35 @@ static bool __init ctxr_parse_dt(phys_addr_t *base, phys_addr_t *size, return *size != 0; } +static void __init ctxr_snapshot_free(phys_addr_t base, phys_addr_t size) +{ + phys_addr_t start, end, wend = base + size; + u64 i; + + ctxr_nr_free = 0; + for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end, + NULL) { + if (start >= wend) + break; + start = max(start, base); + end = min(end, wend); + if (start >= end) + continue; + if (ctxr_nr_free == CTXR_MAX_FREE) { + pr_warn("more than %d free ranges in the window; ignoring the rest\n", + CTXR_MAX_FREE); + break; + } + ctxr_free[ctxr_nr_free].start = start; + ctxr_free[ctxr_nr_free].end = end; + ctxr_nr_free++; + } +} + void __init ctxr_exec_reserve(void) { phys_addr_t base = 0, size = 0, img_start, img_end; + phys_addr_t dram_start, dram_end; bool enforce = true; if (!ctxr_parse_cmdline(&base, &size, &enforce) && @@ -115,9 +155,20 @@ void __init ctxr_exec_reserve(void) img_start = __pa_symbol(_text); img_end = __pa_symbol(_end); - - if (!base) + dram_start = memblock_start_of_DRAM(); + dram_end = memblock_end_of_DRAM(); + + if (!base) { + /* + * No base given: anchor the window on the image. If that would run + * past the end of DRAM (the image is within @size of the top), slide + * it down so it ends at DRAM's end while still covering the image. + */ base = round_down(img_start, CTXR_WINDOW_ALIGN); + if (base + size > dram_end && dram_end - dram_start >= size) + base = max(round_down(dram_end - size, CTXR_WINDOW_ALIGN), + round_up(dram_start, CTXR_WINDOW_ALIGN)); + } if (img_start < base || img_end > base + size) { pr_err("kernel image [%pa, %pa) does not fit window [%pa, +%llu MiB)\n", @@ -126,13 +177,15 @@ void __init ctxr_exec_reserve(void) goto fail; } - if (base < memblock_start_of_DRAM() || - base + size > memblock_end_of_DRAM()) { + if (base < dram_start || base + size > dram_end) { pr_err("window [%pa, +%llu MiB) is not within DRAM\n", &base, (unsigned long long)(size >> 20)); goto fail; } + /* Must run before the window is reserved, or every range looks taken. */ + ctxr_snapshot_free(base, size); + if (memblock_reserve(base, size)) { pr_err("failed to reserve window [%pa, +%llu MiB)\n", &base, (unsigned long long)(size >> 20)); @@ -171,7 +224,7 @@ static void __init ctxr_pool_add_gap(struct gen_pool *pool, phys_addr_t from, bool __init ctxr_exec_execmem_setup(void) { struct gen_pool *pool; - phys_addr_t img_start, img_end; + int i; if (!ctxr_active) return false; @@ -182,11 +235,12 @@ bool __init ctxr_exec_execmem_setup(void) goto fail; } - /* The image occupies part of the window; the rest becomes the pool. */ - img_start = __pa_symbol(_text); - img_end = __pa_symbol(_end); - ctxr_pool_add_gap(pool, ctxr_base, img_start); - ctxr_pool_add_gap(pool, img_end, ctxr_base + ctxr_size); + /* + * Only memory that was free before the window was reserved joins the + * pool; the image and everything else already reserved stays out. + */ + for (i = 0; i < ctxr_nr_free; i++) + ctxr_pool_add_gap(pool, ctxr_free[i].start, ctxr_free[i].end); if (!gen_pool_size(pool)) { pr_err("window has no free space for executable allocations\n"); @@ -198,9 +252,10 @@ bool __init ctxr_exec_execmem_setup(void) goto fail_pool; } - pr_info("executable pool: %zu MiB usable in a %llu MiB window\n", - gen_pool_size(pool) >> 20, - (unsigned long long)(ctxr_size >> 20)); + pr_info("executable pool: %zu MiB usable in %d range(s) of a %llu MiB window (%llu MiB already reserved)\n", + gen_pool_size(pool) >> 20, ctxr_nr_free, + (unsigned long long)(ctxr_size >> 20), + (unsigned long long)((ctxr_size - gen_pool_size(pool)) >> 20)); return true; fail_pool: diff --git a/mm/execmem_confine.c b/mm/execmem_confine.c index fc92f6628d53f..09068d95e7d7f 100644 --- a/mm/execmem_confine.c +++ b/mm/execmem_confine.c @@ -300,6 +300,16 @@ unsigned long execmem_confine_verify(unsigned long *nr_exec_out) } #ifdef CONFIG_DEBUG_FS +static void confine_show_chunk(struct gen_pool *pool, + struct gen_pool_chunk *chunk, void *data) +{ + struct seq_file *m = data; + phys_addr_t start = chunk->phys_addr; + phys_addr_t end = start + (chunk->end_addr - chunk->start_addr + 1); + + seq_printf(m, "pool-range: [%pa, %pa)\n", &start, &end); +} + static int confine_debug_show(struct seq_file *m, void *v) { unsigned long nr_exec = 0, violations; @@ -313,6 +323,7 @@ static int confine_debug_show(struct seq_file *m, void *v) gen_pool_size(confine_pool) >> 10); seq_printf(m, "pool-available: %zu KiB\n", gen_pool_avail(confine_pool) >> 10); + gen_pool_for_each_chunk(confine_pool, confine_show_chunk, m); } violations = execmem_confine_verify(&nr_exec); From f7aeef20bd6e100440b2fdfdb69526c590cbe3a4 Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Thu, 5 Nov 2026 18:14:23 -0500 Subject: [PATCH 04/47] arm64: ctxr-execmem: default to a 128 MiB window The kernel image and the code of every module take about 66 MiB, and RAM in the window that the pool doesn't use is lost to the rest of the system. 128 MiB holds them with room to spare. Signed-off-by: Diljit Singh --- Documentation/arch/arm64/ctxr-execmem.rst | 2 +- arch/arm64/mm/ctxr_execmem.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Documentation/arch/arm64/ctxr-execmem.rst b/Documentation/arch/arm64/ctxr-execmem.rst index b0c5710a0b13f..ac1d1b9216dee 100644 --- a/Documentation/arch/arm64/ctxr-execmem.rst +++ b/Documentation/arch/arm64/ctxr-execmem.rst @@ -32,7 +32,7 @@ Device tree (production) Command line (bring-up / VM) :: - ctxr_exec_window=[@] e.g. ctxr_exec_window=1G + ctxr_exec_window=[@] e.g. ctxr_exec_window=128M ctxr_exec_enforce=0 (default: enforcing) The command line overrides the device tree. ``size`` accepts the usual diff --git a/arch/arm64/mm/ctxr_execmem.c b/arch/arm64/mm/ctxr_execmem.c index 5c5bf8d7ab8a7..13489ec764be7 100644 --- a/arch/arm64/mm/ctxr_execmem.c +++ b/arch/arm64/mm/ctxr_execmem.c @@ -42,7 +42,7 @@ /* Window granularity when the base is derived rather than given. */ #define CTXR_WINDOW_ALIGN SZ_2M /* Default size if only "ctxr_exec_window" with no value is given. */ -#define CTXR_WINDOW_DEFAULT SZ_1G +#define CTXR_WINDOW_DEFAULT SZ_128M static phys_addr_t ctxr_base __initdata; static phys_addr_t ctxr_size __initdata; From 72880a443655eef071feafad6ad843177ba23dfe Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Thu, 5 Nov 2026 15:03:36 -0500 Subject: [PATCH 05/47] arm64: stay at EL2 on entry when a CTXR window is armed Kernel text inside an armed CTXR window can't execute at EL1 on a core the window is enforced on, so the usual EL1 detour on entry faults. Once the boot CPU has found an armed window, cores entering after it switch to VHE in init_kernel_el and never run at EL1. Signed-off-by: Diljit Singh --- arch/arm64/kernel/head.S | 32 ++++++++++++++++++++++++++++++++ arch/arm64/mm/ctxr_execmem.c | 16 +++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kernel/head.S b/arch/arm64/kernel/head.S index 87a822e5c4ca8..bdb79eb18d8e1 100644 --- a/arch/arm64/kernel/head.S +++ b/arch/arm64/kernel/head.S @@ -307,6 +307,38 @@ SYM_INNER_LABEL(init_el2, SYM_L_LOCAL) msr vbar_el2, x0 isb +#ifdef CONFIG_ARM64_CTXR_EXECMEM + /* + * Kernel text inside an armed CTXR window can't execute at EL1, so a + * core entering after the window is known goes straight to VHE here + * instead of dropping to EL1 and coming back through finalise_el2. + */ + adr_l x0, ctxr_stay_el2 + ldr w0, [x0] + cbz w0, 1f + + finalise_el2_state + + mov_q x0, HCR_HOST_VHE_FLAGS + msr_hcr_el2 x0 + isb + + mrs x0, mdcr_el2 + bic x0, x0, #MDCR_EL2_E2PB_MASK + bic x0, x0, #MDCR_EL2_E2TB_MASK + msr mdcr_el2, x0 + + mov_q x0, INIT_SCTLR_EL1_MMU_OFF + pre_disable_mmu_workaround + msr sctlr_el1, x0 + isb + + mov w0, #BOOT_CPU_MODE_EL2 + orr x0, x0, #BOOT_CPU_FLAG_E2H + ret +1: +#endif + mov_q x1, INIT_SCTLR_EL1_MMU_OFF mrs x0, hcr_el2 diff --git a/arch/arm64/mm/ctxr_execmem.c b/arch/arm64/mm/ctxr_execmem.c index 13489ec764be7..1c5471d6f8342 100644 --- a/arch/arm64/mm/ctxr_execmem.c +++ b/arch/arm64/mm/ctxr_execmem.c @@ -33,7 +33,9 @@ #include #include +#include #include +#include #include #include #include @@ -140,11 +142,14 @@ static void __init ctxr_snapshot_free(phys_addr_t base, phys_addr_t size) } } +/* Read by cores entering with the MMU off: see init_kernel_el. */ +u32 ctxr_stay_el2 __section(".mmuoff.data.read"); + void __init ctxr_exec_reserve(void) { phys_addr_t base = 0, size = 0, img_start, img_end; phys_addr_t dram_start, dram_end; - bool enforce = true; + bool enforce = true, armed; if (!ctxr_parse_cmdline(&base, &size, &enforce) && !ctxr_parse_dt(&base, &size, &enforce)) @@ -153,6 +158,9 @@ void __init ctxr_exec_reserve(void) if (!size) return; + /* A base means firmware armed this window; none means software only. */ + armed = base; + img_start = __pa_symbol(_text); img_end = __pa_symbol(_end); dram_start = memblock_start_of_DRAM(); @@ -195,6 +203,12 @@ void __init ctxr_exec_reserve(void) ctxr_base = base; ctxr_size = size; ctxr_enforce = enforce; + + if (armed && is_kernel_in_hyp_mode()) { + ctxr_stay_el2 = 1; + dcache_clean_inval_poc((unsigned long)&ctxr_stay_el2, + (unsigned long)&ctxr_stay_el2 + sizeof(ctxr_stay_el2)); + } ctxr_active = true; pr_info("reserved exec window [%pa, +%llu MiB), %s\n", From 73b99c17e605927563e7d00da723292c9fc38c59 Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Thu, 5 Nov 2026 15:40:18 -0500 Subject: [PATCH 06/47] arm64: keep Apple cores at EL2 on every entry under CTXR Kernel text inside an armed CTXR window can't execute at EL1 on a core the window is enforced on, and the window isn't known when the boot CPU first enters. Apple cores are VHE only, so with CONFIG_ARM64_CTXR_EXECMEM they now switch to VHE in init_kernel_el on every entry, boot CPU included, and never run at EL1. This replaces the flag the boot CPU set for later cores. Signed-off-by: Diljit Singh --- arch/arm64/kernel/head.S | 21 +++++++++++++-------- arch/arm64/mm/ctxr_execmem.c | 16 +--------------- 2 files changed, 14 insertions(+), 23 deletions(-) diff --git a/arch/arm64/kernel/head.S b/arch/arm64/kernel/head.S index bdb79eb18d8e1..af7c9d7277e77 100644 --- a/arch/arm64/kernel/head.S +++ b/arch/arm64/kernel/head.S @@ -309,13 +309,14 @@ SYM_INNER_LABEL(init_el2, SYM_L_LOCAL) #ifdef CONFIG_ARM64_CTXR_EXECMEM /* - * Kernel text inside an armed CTXR window can't execute at EL1, so a - * core entering after the window is known goes straight to VHE here - * instead of dropping to EL1 and coming back through finalise_el2. + * Kernel text inside an armed CTXR window can't execute at EL1, and + * the window isn't known this early. Apple cores are VHE only, so they + * go straight to VHE here on every entry and never run at EL1. */ - adr_l x0, ctxr_stay_el2 - ldr w0, [x0] - cbz w0, 1f + mrs x0, midr_el1 + ubfx x0, x0, #MIDR_IMPLEMENTOR_SHIFT, #8 + cmp x0, #ARM_CPU_IMP_APPLE + b.ne .Lctxr_not_apple finalise_el2_state @@ -329,14 +330,18 @@ SYM_INNER_LABEL(init_el2, SYM_L_LOCAL) msr mdcr_el2, x0 mov_q x0, INIT_SCTLR_EL1_MMU_OFF + msr_s SYS_SCTLR_EL12, x0 pre_disable_mmu_workaround msr sctlr_el1, x0 isb + mov x0, #(PSR_D_BIT | PSR_A_BIT | PSR_I_BIT | PSR_F_BIT | PSR_MODE_EL2h) + msr spsr_el2, x0 + mov w0, #BOOT_CPU_MODE_EL2 orr x0, x0, #BOOT_CPU_FLAG_E2H - ret -1: + eret +.Lctxr_not_apple: #endif mov_q x1, INIT_SCTLR_EL1_MMU_OFF diff --git a/arch/arm64/mm/ctxr_execmem.c b/arch/arm64/mm/ctxr_execmem.c index 1c5471d6f8342..13489ec764be7 100644 --- a/arch/arm64/mm/ctxr_execmem.c +++ b/arch/arm64/mm/ctxr_execmem.c @@ -33,9 +33,7 @@ #include #include -#include #include -#include #include #include #include @@ -142,14 +140,11 @@ static void __init ctxr_snapshot_free(phys_addr_t base, phys_addr_t size) } } -/* Read by cores entering with the MMU off: see init_kernel_el. */ -u32 ctxr_stay_el2 __section(".mmuoff.data.read"); - void __init ctxr_exec_reserve(void) { phys_addr_t base = 0, size = 0, img_start, img_end; phys_addr_t dram_start, dram_end; - bool enforce = true, armed; + bool enforce = true; if (!ctxr_parse_cmdline(&base, &size, &enforce) && !ctxr_parse_dt(&base, &size, &enforce)) @@ -158,9 +153,6 @@ void __init ctxr_exec_reserve(void) if (!size) return; - /* A base means firmware armed this window; none means software only. */ - armed = base; - img_start = __pa_symbol(_text); img_end = __pa_symbol(_end); dram_start = memblock_start_of_DRAM(); @@ -203,12 +195,6 @@ void __init ctxr_exec_reserve(void) ctxr_base = base; ctxr_size = size; ctxr_enforce = enforce; - - if (armed && is_kernel_in_hyp_mode()) { - ctxr_stay_el2 = 1; - dcache_clean_inval_poc((unsigned long)&ctxr_stay_el2, - (unsigned long)&ctxr_stay_el2 + sizeof(ctxr_stay_el2)); - } ctxr_active = true; pr_info("reserved exec window [%pa, +%llu MiB), %s\n", From 5863e2492e8bd1a6eee48b0e8bd7ebe7434c5530 Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Thu, 5 Nov 2026 16:23:30 -0500 Subject: [PATCH 07/47] arm64: configs: asahi: add what an installed system needs The profile had no dm-crypt, so a kernel built from it can't unlock an encrypted root. Add it with the crypto interfaces LUKS uses, and the modules an Aurora install's services load. Signed-off-by: Diljit Singh --- arch/arm64/configs/asahi.config | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/arch/arm64/configs/asahi.config b/arch/arm64/configs/asahi.config index b0577fbb2084d..d7f6f216f557c 100644 --- a/arch/arm64/configs/asahi.config +++ b/arch/arm64/configs/asahi.config @@ -149,3 +149,25 @@ CONFIG_DETECT_HUNG_TASK=y # unless the EL2 monitor supplies a window (/chosen/aurora,ctxr-exec-window) or # the ctxr_exec_window= command-line option is given. CONFIG_ARM64_CTXR_EXECMEM=y + +# An installed system: encrypted root, and what its services load. +CONFIG_BLK_DEV_DM=m +CONFIG_DM_CRYPT=m +CONFIG_DM_INTEGRITY=m +CONFIG_DM_UEVENT=y +CONFIG_CRYPTO_ESSIV=m +CONFIG_CRYPTO_USER_API_HASH=m +CONFIG_CRYPTO_USER_API_SKCIPHER=m +CONFIG_CRYPTO_USER_API_AEAD=m +CONFIG_SND_SEQUENCER=m +CONFIG_SND_SEQ_DUMMY=m +CONFIG_SND_HRTIMER=m +CONFIG_BT_RFCOMM=m +CONFIG_BT_RFCOMM_TTY=y +CONFIG_BT_BNEP=m +CONFIG_BT_BNEP_MC_FILTER=y +CONFIG_BT_BNEP_PROTO_FILTER=y +CONFIG_INET_UDP_DIAG=m +CONFIG_INPUT_JOYDEV=m +CONFIG_INPUT_UINPUT=m +CONFIG_HIDRAW=y From 207bc4d748e3ca810c3d74c4d82a14f6b77f628b Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Thu, 5 Nov 2026 17:09:31 -0500 Subject: [PATCH 08/47] arm64: vdso: keep the vDSO out of the CTXR window The vDSO's code is part of the kernel image, which sits in the CTXR window, and EL0 can't execute there on a core the window is enforced on. A process calling into the vDSO on such a core takes a permission fault that the kernel finds nothing to fix, and retries it forever. Map a copy from ordinary memory into userspace instead. Signed-off-by: Diljit Singh --- arch/arm64/kernel/vdso.c | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/arch/arm64/kernel/vdso.c b/arch/arm64/kernel/vdso.c index 592dd8668de46..abdf8d572e14a 100644 --- a/arch/arm64/kernel/vdso.c +++ b/arch/arm64/kernel/vdso.c @@ -65,6 +65,24 @@ static int vdso_mremap(const struct vm_special_mapping *sm, return 0; } +/* + * The kernel image sits in the CTXR window, where EL0 can't execute on a core + * the window is enforced on, so EL0 gets a copy in ordinary memory. + */ +static struct page * __init vdso_code_page(unsigned long pfn) +{ + struct page *page; + + if (!IS_ENABLED(CONFIG_ARM64_CTXR_EXECMEM)) + return pfn_to_page(pfn); + + page = alloc_page(GFP_KERNEL); + if (page) + copy_page(page_address(page), pfn_to_kaddr(pfn)); + + return page; +} + static int __init __vdso_init(enum vdso_abi abi) { int i; @@ -88,8 +106,11 @@ static int __init __vdso_init(enum vdso_abi abi) /* Grab the vDSO code pages. */ pfn = sym_to_pfn(vdso_info[abi].vdso_code_start); - for (i = 0; i < vdso_info[abi].vdso_pages; i++) - vdso_pagelist[i] = pfn_to_page(pfn + i); + for (i = 0; i < vdso_info[abi].vdso_pages; i++) { + vdso_pagelist[i] = vdso_code_page(pfn + i); + if (!vdso_pagelist[i]) + return -ENOMEM; + } vdso_info[abi].cm->pages = vdso_pagelist; From 4b630f74a37e00f08b4236eeb0935eb0cbbc115e Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Thu, 5 Nov 2026 17:43:52 -0500 Subject: [PATCH 09/47] mm/execmem: keep window pages out of the page allocator A core that enforces the CTXR window doesn't let EL0 execute from it either. The fetch takes a permission fault that the page tables don't explain, so the kernel retries it forever. The window is reserved, but free_reserved_area() can still release parts of it: __init memory, which is part of the image, and an initrd placed in the window. Those pages reach the page allocator and can become user pages. Keep them reserved, and have the verifier count any window page that isn't reserved as a violation. Signed-off-by: Diljit Singh --- Documentation/arch/arm64/ctxr-execmem.rst | 20 +++++++++++++++-- include/linux/execmem.h | 16 ++++++++++---- mm/execmem_confine.c | 27 ++++++++++++++++++++++- mm/memblock.c | 4 ++++ 4 files changed, 60 insertions(+), 7 deletions(-) diff --git a/Documentation/arch/arm64/ctxr-execmem.rst b/Documentation/arch/arm64/ctxr-execmem.rst index ac1d1b9216dee..934258264d82d 100644 --- a/Documentation/arch/arm64/ctxr-execmem.rst +++ b/Documentation/arch/arm64/ctxr-execmem.rst @@ -55,6 +55,20 @@ How it works CTXR fault would prevent executing from outside the window; there is no buddy fallback. +Userspace +========= + +On a core that enforces the window, EL0 can't execute from it either. The fetch +takes a permission fault that the page tables don't explain, so the kernel +retries it forever. Anything EL0 can execute therefore lives outside the window: + +* The vDSO's code is part of the image, so userspace gets a copy in ordinary + memory. +* No window page reaches the page allocator. ``free_reserved_area()`` skips them, + so freed ``__init`` memory stays reserved, as does an initrd in the window. RAM + in the window that the pool doesn't use is not available to the rest of the + system, so a window should be no larger than the executable code needs. + Verifying confinement ===================== @@ -63,9 +77,11 @@ fault when testing in a VM: * At ``late_initcall`` the kernel sweeps its page tables and reports the number of EL1-executable leaf mappings and how many resolve outside the window. When - the window is enforcing, a non-zero count is fatal. + the window is enforcing, a non-zero count is fatal. It also counts window + pages that reached the page allocator as violations. * ``/sys/kernel/debug/execmem_confine`` re-runs the sweep on read and reports - window bounds, pool usage and ``PASS``/``FAIL``. + window bounds, pool usage and ``PASS``/``FAIL``. Init memory is freed after + ``late_initcall``, and an initrd can be, so this is the check that covers them. * Booting with ``ctxr_exec_selftest`` runs two extra checks: that a confined allocation is physically inside the window, and that the sweep actually detects an executable mapping placed *outside* it (proving the verifier is diff --git a/include/linux/execmem.h b/include/linux/execmem.h index b402c11622b7a..0837e0f813720 100644 --- a/include/linux/execmem.h +++ b/include/linux/execmem.h @@ -240,6 +240,12 @@ bool execmem_confine_enforced(void); /* Window bounds, valid only when execmem_confine_active(). */ void execmem_confine_window(phys_addr_t *base, phys_addr_t *size); +/* + * True if @pfn is in the window. Userspace can't execute from the window, so + * such a page must never reach the page allocator. + */ +bool execmem_confine_pfn_in_window(unsigned long pfn); + /* * Internal backing allocator/free used by mm/execmem.c for EXECMEM_CONFINE * ranges. Not for general use. @@ -250,10 +256,11 @@ bool execmem_confine_free(void *ptr); /* * Verify that no EL1-executable mapping resolves to a physical page outside - * the window. Returns the number of violations; fills in the number of - * executable leaf mappings inspected when @nr_exec is non-NULL. Implemented - * generically for the confined allocations; the page-table sweep is provided - * per-architecture by execmem_confine_arch_verify(). + * the window and that no window page reached the page allocator. Returns the + * number of violations; fills in the number of executable leaf mappings + * inspected when @nr_exec is non-NULL. Implemented generically for the confined + * allocations; the page-table sweep is provided per-architecture by + * execmem_confine_arch_verify(). */ unsigned long execmem_confine_verify(unsigned long *nr_exec); @@ -267,6 +274,7 @@ unsigned long execmem_confine_arch_verify(phys_addr_t base, phys_addr_t size, unsigned long *nr_exec); #else static inline bool execmem_confine_active(void) { return false; } +static inline bool execmem_confine_pfn_in_window(unsigned long pfn) { return false; } #endif /* CONFIG_EXECMEM_CONFINE */ #endif /* _LINUX_EXECMEM_ALLOC_H */ diff --git a/mm/execmem_confine.c b/mm/execmem_confine.c index 09068d95e7d7f..f54e1d7d51209 100644 --- a/mm/execmem_confine.c +++ b/mm/execmem_confine.c @@ -103,6 +103,25 @@ static bool phys_in_window(phys_addr_t pa) return pa >= confine_base && pa < confine_base + confine_size; } +bool execmem_confine_pfn_in_window(unsigned long pfn) +{ + return confine_ready && phys_in_window(PFN_PHYS(pfn)); +} + +/* Window pages stay reserved for good, so one that isn't went to the allocator. */ +static unsigned long confine_count_freed_pages(void) +{ + unsigned long pfn, nr = 0; + + for_each_valid_pfn(pfn, PFN_DOWN(confine_base), + PFN_DOWN(confine_base + confine_size)) { + if (!PageReserved(pfn_to_page(pfn))) + nr++; + } + + return nr; +} + void *execmem_confine_alloc(struct execmem_range *range, size_t size, pgprot_t pgprot, unsigned long vm_flags) { @@ -262,7 +281,7 @@ unsigned long __weak execmem_confine_arch_verify(phys_addr_t base, unsigned long execmem_confine_verify(unsigned long *nr_exec_out) { - unsigned long nr_exec = 0, violations = 0; + unsigned long nr_exec = 0, violations = 0, freed; struct confine_chunk *ck; unsigned long idx; @@ -272,6 +291,12 @@ unsigned long execmem_confine_verify(unsigned long *nr_exec_out) return 0; } + freed = confine_count_freed_pages(); + if (freed) { + pr_err("%lu window page(s) reached the page allocator\n", freed); + violations += freed; + } + /* * Registry sanity: every confined allocation is backed by pool pages, * which are by construction inside the window. Re-check anyway so the diff --git a/mm/memblock.c b/mm/memblock.c index e6fdf95a0534d..a2b11c143d396 100644 --- a/mm/memblock.c +++ b/mm/memblock.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -908,6 +909,9 @@ static unsigned long __free_reserved_area(phys_addr_t start, phys_addr_t end, struct page *page = pfn_to_page(pfn); void *direct_map_addr; + if (execmem_confine_pfn_in_window(pfn)) + continue; + /* * 'direct_map_addr' might be different from the kernel virtual * address because some architectures use aliases. From fe8741d4f6da8802000376e3ff6953eaabe783c2 Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Fri, 6 Nov 2026 01:16:38 -0500 Subject: [PATCH 10/47] arm64: keep M1 to M3 on the usual entry path under CTXR With CONFIG_ARM64_CTXR_EXECMEM every Apple core switched to VHE in init_kernel_el, whether or not it has CTXR and whether or not a window was supplied, so a kernel built with the option took a different entry path on M1 to M3. Only M4-generation and later cores have CTXR. Their part numbers start at 0x50, and every earlier core's is below that, so limit the direct switch to them. Earlier cores drop to EL1 and come back through finalise_el2 as before. Signed-off-by: Diljit Singh --- arch/arm64/kernel/head.S | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/arch/arm64/kernel/head.S b/arch/arm64/kernel/head.S index af7c9d7277e77..7b724c06353c9 100644 --- a/arch/arm64/kernel/head.S +++ b/arch/arm64/kernel/head.S @@ -310,13 +310,17 @@ SYM_INNER_LABEL(init_el2, SYM_L_LOCAL) #ifdef CONFIG_ARM64_CTXR_EXECMEM /* * Kernel text inside an armed CTXR window can't execute at EL1, and - * the window isn't known this early. Apple cores are VHE only, so they - * go straight to VHE here on every entry and never run at EL1. + * the window isn't known this early. Apple cores from the M4 generation + * on (part numbers from 0x50) have CTXR and are VHE only, so they go + * straight to VHE here on every entry and never run at EL1. */ mrs x0, midr_el1 - ubfx x0, x0, #MIDR_IMPLEMENTOR_SHIFT, #8 - cmp x0, #ARM_CPU_IMP_APPLE - b.ne .Lctxr_not_apple + ubfx x1, x0, #MIDR_IMPLEMENTOR_SHIFT, #8 + cmp x1, #ARM_CPU_IMP_APPLE + b.ne .Lctxr_no_direct_vhe + ubfx x0, x0, #MIDR_PARTNUM_SHIFT, #12 + cmp x0, #0x50 + b.lo .Lctxr_no_direct_vhe finalise_el2_state @@ -341,7 +345,7 @@ SYM_INNER_LABEL(init_el2, SYM_L_LOCAL) mov w0, #BOOT_CPU_MODE_EL2 orr x0, x0, #BOOT_CPU_FLAG_E2H eret -.Lctxr_not_apple: +.Lctxr_no_direct_vhe: #endif mov_q x1, INIT_SCTLR_EL1_MMU_OFF From 0412b237dc1fb059cf3597a30c3d3c6c09294e72 Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Fri, 6 Nov 2026 01:16:38 -0500 Subject: [PATCH 11/47] arm64: vdso: copy the vDSO only when a window is active The copy was made whenever CONFIG_ARM64_CTXR_EXECMEM was on. With no window there is nothing to keep the vDSO out of, so map the image's pages as before. Signed-off-by: Diljit Singh --- arch/arm64/kernel/vdso.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kernel/vdso.c b/arch/arm64/kernel/vdso.c index abdf8d572e14a..3aea59d87364f 100644 --- a/arch/arm64/kernel/vdso.c +++ b/arch/arm64/kernel/vdso.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -73,7 +74,7 @@ static struct page * __init vdso_code_page(unsigned long pfn) { struct page *page; - if (!IS_ENABLED(CONFIG_ARM64_CTXR_EXECMEM)) + if (!execmem_confine_active()) return pfn_to_page(pfn); page = alloc_page(GFP_KERNEL); From d9f915b119ddbd52a30d316b14467cbfe77e0286 Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Fri, 6 Nov 2026 01:16:38 -0500 Subject: [PATCH 12/47] arm64: ctxr-execmem: turn EFI runtime services off if their code can't run EFI runtime code runs at EL2 on whichever core efi_rts_wq is on. On a core that enforces the window, code outside it faults, and the fault handler dies with "execution of user memory" before the EFI fixup runs. So when a window is enforcing and any EFI_RUNTIME_SERVICES_CODE region is outside it, warn once and turn runtime services off for that boot. Misaligned code counts too: on a 16K kernel it makes the misaligned regions after it executable, wherever they are, and the verifier would then panic on them. The window is read from this file rather than through execmem_confine_*(), since execmem_init() is a later core_initcall than arm64_efi_rt_init(). Have the verifier sweep efi_mm as well, so its report covers the runtime mappings that stay on. Signed-off-by: Diljit Singh --- Documentation/arch/arm64/ctxr-execmem.rst | 19 ++++++++-- arch/arm64/include/asm/ctxr_execmem.h | 7 ++++ arch/arm64/kernel/efi.c | 6 +++ arch/arm64/mm/ctxr_execmem.c | 46 +++++++++++++++++++++-- 4 files changed, 71 insertions(+), 7 deletions(-) diff --git a/Documentation/arch/arm64/ctxr-execmem.rst b/Documentation/arch/arm64/ctxr-execmem.rst index 934258264d82d..73c7e9bd1a1b5 100644 --- a/Documentation/arch/arm64/ctxr-execmem.rst +++ b/Documentation/arch/arm64/ctxr-execmem.rst @@ -69,16 +69,27 @@ retries it forever. Anything EL0 can execute therefore lives outside the window: in the window that the pool doesn't use is not available to the rest of the system, so a window should be no larger than the executable code needs. +EFI runtime services +==================== + +EFI runtime code runs at EL2 on whichever core ``efi_rts_wq`` happens to be on, +so on a confined core it has to be inside the window too. When a window is +enforcing and any ``EFI_RUNTIME_SERVICES_CODE`` region is outside it, or isn't +aligned to the kernel's page size, the kernel warns once and turns runtime +services off for that boot, as ``efi=noruntime`` would. When all of it is inside, +runtime services stay on. A window in reporting mode leaves them on. + Verifying confinement ===================== The feature carries its own verifier, which is what stands in for the hardware fault when testing in a VM: -* At ``late_initcall`` the kernel sweeps its page tables and reports the number - of EL1-executable leaf mappings and how many resolve outside the window. When - the window is enforcing, a non-zero count is fatal. It also counts window - pages that reached the page allocator as violations. +* At ``late_initcall`` the kernel sweeps its page tables, and ``efi_mm`` when + runtime services are on, and reports the number of EL1-executable leaf mappings + and how many resolve outside the window. When the window is enforcing, a + non-zero count is fatal. It also counts window pages that reached the page + allocator as violations. * ``/sys/kernel/debug/execmem_confine`` re-runs the sweep on read and reports window bounds, pool usage and ``PASS``/``FAIL``. Init memory is freed after ``late_initcall``, and an initrd can be, so this is the check that covers them. diff --git a/arch/arm64/include/asm/ctxr_execmem.h b/arch/arm64/include/asm/ctxr_execmem.h index c9f5088821e95..06618c9f4dead 100644 --- a/arch/arm64/include/asm/ctxr_execmem.h +++ b/arch/arm64/include/asm/ctxr_execmem.h @@ -23,9 +23,16 @@ void ctxr_exec_reserve(void); * EXECMEM_CONFINE. */ bool ctxr_exec_execmem_setup(void); + +/* + * False if a window is enforcing and EFI runtime code can't run behind it. + * Warns once when it returns false. + */ +bool ctxr_exec_efi_runtime_ok(void); #else static inline void ctxr_exec_reserve(void) {} static inline bool ctxr_exec_execmem_setup(void) { return false; } +static inline bool ctxr_exec_efi_runtime_ok(void) { return true; } #endif #endif /* __ASM_CTXR_EXECMEM_H */ diff --git a/arch/arm64/kernel/efi.c b/arch/arm64/kernel/efi.c index 0ec90fd1754ed..0f080509f2dfe 100644 --- a/arch/arm64/kernel/efi.c +++ b/arch/arm64/kernel/efi.c @@ -14,6 +14,7 @@ #include #include +#include #include #include #include @@ -249,6 +250,11 @@ static int __init arm64_efi_rt_init(void) if (!efi_enabled(EFI_RUNTIME_SERVICES)) return 0; + if (!ctxr_exec_efi_runtime_ok()) { + clear_bit(EFI_RUNTIME_SERVICES, &efi.flags); + return 0; + } + p = arch_alloc_vmap_stack(THREAD_SIZE, NUMA_NO_NODE); if (!p) { pr_warn("Failed to allocate EFI runtime stack\n"); diff --git a/arch/arm64/mm/ctxr_execmem.c b/arch/arm64/mm/ctxr_execmem.c index 13489ec764be7..76b0f75bcc879 100644 --- a/arch/arm64/mm/ctxr_execmem.c +++ b/arch/arm64/mm/ctxr_execmem.c @@ -22,6 +22,7 @@ #define pr_fmt(fmt) "ctxr-execmem: " fmt +#include #include #include #include @@ -266,11 +267,41 @@ bool __init ctxr_exec_execmem_setup(void) return false; } +/* + * A core enforcing the window can't fetch firmware code outside it, and + * efi_rts_wq can run on any core. Misaligned code also makes the misaligned + * regions after it executable, wherever they are. + */ +bool __init ctxr_exec_efi_runtime_ok(void) +{ + efi_memory_desc_t *md; + + if (!IS_ENABLED(CONFIG_EFI) || !ctxr_active || !ctxr_enforce) + return true; + + for_each_efi_memory_desc(md) { + phys_addr_t start = md->phys_addr; + phys_addr_t end = start + (md->num_pages << EFI_PAGE_SHIFT); + + if (md->type != EFI_RUNTIME_SERVICES_CODE) + continue; + if (start >= ctxr_base && end <= ctxr_base + ctxr_size && + PAGE_ALIGNED(start) && PAGE_ALIGNED(end)) + continue; + + pr_warn("EFI runtime services off: firmware code [%pa, %pa) is outside the window or misaligned\n", + &start, &end); + return false; + } + + return true; +} + /* * Page-table sweep used by the confinement verifier. For every privileged - * (PXN-clear) executable leaf in the kernel page tables, count it and flag any - * whose physical output lies outside the window. This is what stands in, in a - * VM, for the hardware CTXR fault. + * (PXN-clear) executable leaf in the kernel and EFI page tables, count it and + * flag any whose physical output lies outside the window. This is what stands + * in, in a VM, for the hardware CTXR fault. */ struct ctxr_verify { struct ptdump_state ptdump; @@ -360,6 +391,15 @@ unsigned long execmem_confine_arch_verify(phys_addr_t base, phys_addr_t size, ptdump_walk_pgd(&v.ptdump, &init_mm, NULL); + /* EFI runtime code has its own page tables, at user addresses. */ + if (efi_enabled(EFI_RUNTIME_SERVICES)) { + v.ptdump.range = (struct ptdump_range[]) { + { 0, TASK_SIZE_64 }, + { 0, 0 } + }; + ptdump_walk_pgd(&v.ptdump, &efi_mm, NULL); + } + if (nr_exec) *nr_exec = v.nr_exec; return v.violations; From a64c54e49a628daeb24fdda78bde353f0d4d41e9 Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Fri, 6 Nov 2026 01:16:38 -0500 Subject: [PATCH 13/47] arm64: mm: kill a task whose fetch the CTXR window refuses On a core enforcing the window, an EL0 fetch from a window page takes a level 3 permission fault that the page tables don't explain. handle_mm_fault() finds nothing to fix, so the task retries forever: it stays runnable, with no panic and no hung-task report. The zero page stays in the window, so a program jumping into fresh executable memory before writing it hits this. When a window is enforcing and such a fault lands on a present, executable mapping of a window page, log the task and kill it. Every other fault takes the path it took before. Signed-off-by: Diljit Singh --- Documentation/arch/arm64/ctxr-execmem.rst | 10 ++++-- arch/arm64/mm/fault.c | 44 +++++++++++++++++++++++ include/linux/execmem.h | 1 + 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/Documentation/arch/arm64/ctxr-execmem.rst b/Documentation/arch/arm64/ctxr-execmem.rst index 73c7e9bd1a1b5..04b2a600824d1 100644 --- a/Documentation/arch/arm64/ctxr-execmem.rst +++ b/Documentation/arch/arm64/ctxr-execmem.rst @@ -59,8 +59,8 @@ Userspace ========= On a core that enforces the window, EL0 can't execute from it either. The fetch -takes a permission fault that the page tables don't explain, so the kernel -retries it forever. Anything EL0 can execute therefore lives outside the window: +takes a permission fault that the page tables don't explain, so nothing can fix +it up. Anything EL0 can execute therefore lives outside the window: * The vDSO's code is part of the image, so userspace gets a copy in ordinary memory. @@ -69,6 +69,12 @@ retries it forever. Anything EL0 can execute therefore lives outside the window: in the window that the pool doesn't use is not available to the rest of the system, so a window should be no larger than the executable code needs. +The zero page stays in the window, so a program that jumps into fresh executable +memory before writing it still gets there. When the window is enforcing, an EL0 +instruction permission fault on a present, executable mapping of a window page +logs the task and its address and PFN, then kills it with an uncatchable +``SIGSEGV`` rather than retrying. Any other fault takes the usual path. + EFI runtime services ==================== diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c index 2ea95d7490dca..a2ee7770a5738 100644 --- a/arch/arm64/mm/fault.c +++ b/arch/arm64/mm/fault.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -19,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -602,6 +604,43 @@ static bool is_invalid_gcs_access(struct vm_area_struct *vma, u64 esr) return false; } +/* + * A core enforcing the CTXR window refuses EL0 fetches from it with a permission + * fault that the page tables don't explain. Nothing can fix that up, so a retry + * would fault forever with the task still runnable. The PFN may have no usable + * struct page, so only compare it. + */ +static bool el0_fetch_refused_by_window(struct mm_struct *mm, unsigned long addr, + unsigned long esr, unsigned int mm_flags) +{ + struct vm_area_struct *vma; + struct folio_walk fw; + unsigned long pfn = 0; + + if (!is_el0_instruction_abort(esr) || !esr_fsc_is_permission_fault(esr) || + !execmem_confine_enforced()) + return false; + + if (mmap_read_lock_killable(mm)) + return false; + + vma = vma_lookup(mm, addr); + if (vma && (vma->vm_flags & VM_EXEC) && !fault_from_pkey(vma, mm_flags) && + folio_walk_start(&fw, vma, addr, FW_ZEROPAGE)) { + if (fw.level == FW_LEVEL_PTE && pte_user_exec(fw.pte)) + pfn = pte_pfn(fw.pte); + folio_walk_end(&fw, vma); + } + mmap_read_unlock(mm); + + if (!pfn || !execmem_confine_pfn_in_window(pfn)) + return false; + + pr_warn_ratelimited("%s[%d]: EL0 fetch at %#lx from pfn %#lx refused by the CTXR window, killing it\n", + current->comm, task_pid_nr(current), addr, pfn); + return true; +} + static int __kprobes do_page_fault(unsigned long far, unsigned long esr, struct pt_regs *regs) { @@ -679,6 +718,11 @@ static int __kprobes do_page_fault(unsigned long far, unsigned long esr, perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr); + if (el0_fetch_refused_by_window(mm, addr, esr, mm_flags)) { + force_fatal_sig(SIGSEGV); + return 0; + } + if (!(mm_flags & FAULT_FLAG_USER)) goto lock_mmap; diff --git a/include/linux/execmem.h b/include/linux/execmem.h index 0837e0f813720..e561b00dcaf88 100644 --- a/include/linux/execmem.h +++ b/include/linux/execmem.h @@ -274,6 +274,7 @@ unsigned long execmem_confine_arch_verify(phys_addr_t base, phys_addr_t size, unsigned long *nr_exec); #else static inline bool execmem_confine_active(void) { return false; } +static inline bool execmem_confine_enforced(void) { return false; } static inline bool execmem_confine_pfn_in_window(unsigned long pfn) { return false; } #endif /* CONFIG_EXECMEM_CONFINE */ From 0ab88ef01d7c926a9a59414773da18a7bf69e3f2 Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Fri, 6 Nov 2026 01:16:38 -0500 Subject: [PATCH 14/47] arm64: kexec: refuse kexec and crash kernels under an enforcing CTXR window The relocation code runs from an ordinary page, outside the window, and a crash dump can start on a core enforcing it, where it would hang during a panic. Fail the load cleanly instead, with one message, so nothing finds out only when it needs the second kernel. Signed-off-by: Diljit Singh --- Documentation/arch/arm64/ctxr-execmem.rst | 8 ++++++++ arch/arm64/kernel/machine_kexec.c | 10 ++++++++++ 2 files changed, 18 insertions(+) diff --git a/Documentation/arch/arm64/ctxr-execmem.rst b/Documentation/arch/arm64/ctxr-execmem.rst index 04b2a600824d1..bcdb1e0691dbb 100644 --- a/Documentation/arch/arm64/ctxr-execmem.rst +++ b/Documentation/arch/arm64/ctxr-execmem.rst @@ -85,6 +85,14 @@ aligned to the kernel's page size, the kernel warns once and turns runtime services off for that boot, as ``efi=noruntime`` would. When all of it is inside, runtime services stay on. A window in reporting mode leaves them on. +kexec and crash kernels +======================= + +The relocation code runs from an ordinary page, outside the window, and a crash +dump can start on a confined core, where it would hang during a panic. While a +window is enforcing, ``machine_kexec_prepare()`` therefore refuses both, so +``kexec_load()`` and ``kexec_file_load()`` fail with ``EOPNOTSUPP``. + Verifying confinement ===================== diff --git a/arch/arm64/kernel/machine_kexec.c b/arch/arm64/kernel/machine_kexec.c index 8f9bc2327dc85..ac8471c47229f 100644 --- a/arch/arm64/kernel/machine_kexec.c +++ b/arch/arm64/kernel/machine_kexec.c @@ -6,6 +6,7 @@ * Copyright (C) Huawei Futurewei Technologies. */ +#include #include #include #include @@ -54,6 +55,15 @@ void machine_kexec_cleanup(struct kimage *kimage) */ int machine_kexec_prepare(struct kimage *kimage) { + /* + * The relocation code runs from an ordinary page, outside the window, + * and a crash dump can start on a core enforcing it. + */ + if (execmem_confine_enforced()) { + pr_err("Can't kexec: the kernel's code is confined to a CTXR window.\n"); + return -EOPNOTSUPP; + } + if (kimage->type != KEXEC_TYPE_CRASH && cpus_are_stuck_in_kernel()) { pr_err("Can't kexec: CPUs are stuck in the kernel.\n"); return -EBUSY; From 2e6e70017cec00ad08ef2752cfb0d8e9d65f127c Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Fri, 6 Nov 2026 01:16:38 -0500 Subject: [PATCH 15/47] arm64: ctxr-execmem: name the bootloader and describe the entry path A kernel in a window runs at EL2 with no monitor below it, so the bootloader is what supplies the window. Say so, and document the entry path. Signed-off-by: Diljit Singh --- Documentation/arch/arm64/ctxr-execmem.rst | 8 ++++++-- arch/arm64/Kconfig | 2 +- arch/arm64/configs/asahi.config | 2 +- arch/arm64/mm/ctxr_execmem.c | 2 +- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Documentation/arch/arm64/ctxr-execmem.rst b/Documentation/arch/arm64/ctxr-execmem.rst index bcdb1e0691dbb..e628852a67689 100644 --- a/Documentation/arch/arm64/ctxr-execmem.rst +++ b/Documentation/arch/arm64/ctxr-execmem.rst @@ -11,7 +11,7 @@ fixed physical window. Outside it, the fetch takes an instruction abort. To run the kernel at EL2 behind such a window (instead of dropping it to EL1), **every** executable physical page must live inside one window: the static image -(placed there by the boot monitor) and every run-time executable allocation. +(placed there by the bootloader) and every run-time executable allocation. ``CONFIG_ARM64_CTXR_EXECMEM`` makes that possible. It is inert unless a window is supplied at boot, so it is safe to leave enabled on machines that do not need @@ -21,7 +21,7 @@ Supplying the window ==================== Device tree (production) - The EL2 monitor emits, under ``/chosen``:: + The bootloader emits, under ``/chosen``:: aurora,ctxr-exec-window = ; aurora,ctxr-exec-enforce = <1>; /* optional, default 1 */ @@ -54,6 +54,10 @@ How it works * If the window's pool is exhausted the allocation fails, exactly as a hardware CTXR fault would prevent executing from outside the window; there is no buddy fallback. +* Code in the window can't execute at EL1 on a confined core, and the window + isn't known at the first instruction, so cores from the M4 generation on switch + to VHE in ``init_kernel_el`` on every entry at EL2. Earlier cores don't have + CTXR and enter as they always have. Userspace ========= diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index ea591dc3d35e4..054969dc7bae2 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -2549,7 +2549,7 @@ config ARM64_CTXR_EXECMEM executable physical page must live inside it. With this option the kernel reserves a single window — described by - the EL2 monitor in /chosen/aurora,ctxr-exec-window, or by the + the bootloader in /chosen/aurora,ctxr-exec-window, or by the "ctxr_exec_window=" command-line option for VM bring-up — places the static image inside it, and backs every run-time executable allocation (modules, BPF, kprobes, ftrace) with pages drawn from the remainder. diff --git a/arch/arm64/configs/asahi.config b/arch/arm64/configs/asahi.config index d7f6f216f557c..d034153a2e64f 100644 --- a/arch/arm64/configs/asahi.config +++ b/arch/arm64/configs/asahi.config @@ -146,7 +146,7 @@ CONFIG_UDMABUF=y CONFIG_DETECT_HUNG_TASK=y # Confine kernel executable code to a fixed physical CTXR window so the kernel # can run at EL2 on chips whose firmware locks the code-execute range. Inert -# unless the EL2 monitor supplies a window (/chosen/aurora,ctxr-exec-window) or +# unless the bootloader supplies a window (/chosen/aurora,ctxr-exec-window) or # the ctxr_exec_window= command-line option is given. CONFIG_ARM64_CTXR_EXECMEM=y diff --git a/arch/arm64/mm/ctxr_execmem.c b/arch/arm64/mm/ctxr_execmem.c index 76b0f75bcc879..7100c3cdbe465 100644 --- a/arch/arm64/mm/ctxr_execmem.c +++ b/arch/arm64/mm/ctxr_execmem.c @@ -11,7 +11,7 @@ * * This file owns the arm64 end of that contract: * - it learns the window from the device tree (/chosen/aurora,ctxr-exec-window, - * emitted by the EL2 monitor) or the command line (ctxr_exec_window=, used + * emitted by the bootloader) or the command line (ctxr_exec_window=, used * for VM/simulated bring-up), * - reserves it from memblock and checks the image fits, * - carves the non-image remainder into a gen_pool and hands it to the generic From d9de654671e173598638924e6f9b5dfb34747615 Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Fri, 6 Nov 2026 03:14:43 -0500 Subject: [PATCH 16/47] arm64: ctxr-execmem: parse the command line with early_param() ctxr_exec_enforce= was only read along with ctxr_exec_window=, so it did nothing to a window from the device tree. strstr() matched inside other words and took the first use of an option. A value that didn't parse gave a default window, and a window too small for the image panicked a machine that would otherwise boot. Use early_param(), which runs before arm64_memblock_init(). The last use of an option wins, and ctxr_exec_enforce= applies to a window from either place. A value that doesn't parse is ignored with a warning. So is a window from the command line that can't work, which leaves the device tree's window if there is one. A window from the device tree that can't work is still fatal when enforcing, since the kernel can't run on the confined cores outside it. Signed-off-by: Diljit Singh --- arch/arm64/mm/ctxr_execmem.c | 110 ++++++++++++++++++++++------------- 1 file changed, 70 insertions(+), 40 deletions(-) diff --git a/arch/arm64/mm/ctxr_execmem.c b/arch/arm64/mm/ctxr_execmem.c index 7100c3cdbe465..337111deb4116 100644 --- a/arch/arm64/mm/ctxr_execmem.c +++ b/arch/arm64/mm/ctxr_execmem.c @@ -64,31 +64,52 @@ static struct { } ctxr_free[CTXR_MAX_FREE] __initdata; static int ctxr_nr_free __initdata; -static bool __init ctxr_parse_cmdline(phys_addr_t *base, phys_addr_t *size, - bool *enforce) +/* From the command line. A size of 0 means none was given. */ +static phys_addr_t ctxr_cmd_base __initdata; +static phys_addr_t ctxr_cmd_size __initdata; +static int ctxr_cmd_enforce __initdata = -1; + +/* ctxr_exec_window=[[@]]: with no size, the default one. */ +static int __init ctxr_exec_window_param(char *arg) { - const char *p; - char *endp; + phys_addr_t base = 0, size = CTXR_WINDOW_DEFAULT; + char *p = arg, *end; + + if (p && *p) { + size = memparse(p, &end); + if (end == p || !size) + goto bad; + if (*end == '@') { + p = end + 1; + base = memparse(p, &end); + if (end == p) + goto bad; + } + if (*end) + goto bad; + } - p = strstr(boot_command_line, "ctxr_exec_window="); - if (!p) - return false; - p += strlen("ctxr_exec_window="); + ctxr_cmd_base = base; + ctxr_cmd_size = size; + return 0; - *size = memparse(p, &endp); - if (!*size) - *size = CTXR_WINDOW_DEFAULT; - if (*endp == '@') - *base = memparse(endp + 1, NULL); - else - *base = 0; /* derive from the image below */ +bad: + pr_warn("ignoring ctxr_exec_window=%s\n", arg); + return 0; +} +early_param("ctxr_exec_window", ctxr_exec_window_param); - p = strstr(boot_command_line, "ctxr_exec_enforce="); - if (p) - *enforce = (p[strlen("ctxr_exec_enforce=")] != '0'); +static int __init ctxr_exec_enforce_param(char *arg) +{ + bool enforce; - return true; + if (kstrtobool(arg, &enforce)) + pr_warn("ignoring ctxr_exec_enforce=%s\n", arg ?: ""); + else + ctxr_cmd_enforce = enforce; + return 0; } +early_param("ctxr_exec_enforce", ctxr_exec_enforce_param); static bool __init ctxr_parse_dt(phys_addr_t *base, phys_addr_t *size, bool *enforce) @@ -141,23 +162,12 @@ static void __init ctxr_snapshot_free(phys_addr_t base, phys_addr_t size) } } -void __init ctxr_exec_reserve(void) +/* Reserve a window. False, with the reason logged, if it can't work. */ +static bool __init ctxr_establish(phys_addr_t base, phys_addr_t size, bool enforce) { - phys_addr_t base = 0, size = 0, img_start, img_end; - phys_addr_t dram_start, dram_end; - bool enforce = true; - - if (!ctxr_parse_cmdline(&base, &size, &enforce) && - !ctxr_parse_dt(&base, &size, &enforce)) - return; - - if (!size) - return; - - img_start = __pa_symbol(_text); - img_end = __pa_symbol(_end); - dram_start = memblock_start_of_DRAM(); - dram_end = memblock_end_of_DRAM(); + phys_addr_t img_start = __pa_symbol(_text), img_end = __pa_symbol(_end); + phys_addr_t dram_start = memblock_start_of_DRAM(); + phys_addr_t dram_end = memblock_end_of_DRAM(); if (!base) { /* @@ -175,13 +185,13 @@ void __init ctxr_exec_reserve(void) pr_err("kernel image [%pa, %pa) does not fit window [%pa, +%llu MiB)\n", &img_start, &img_end, &base, (unsigned long long)(size >> 20)); - goto fail; + return false; } if (base < dram_start || base + size > dram_end) { pr_err("window [%pa, +%llu MiB) is not within DRAM\n", &base, (unsigned long long)(size >> 20)); - goto fail; + return false; } /* Must run before the window is reserved, or every range looks taken. */ @@ -190,7 +200,7 @@ void __init ctxr_exec_reserve(void) if (memblock_reserve(base, size)) { pr_err("failed to reserve window [%pa, +%llu MiB)\n", &base, (unsigned long long)(size >> 20)); - goto fail; + return false; } ctxr_base = base; @@ -201,9 +211,29 @@ void __init ctxr_exec_reserve(void) pr_info("reserved exec window [%pa, +%llu MiB), %s\n", &base, (unsigned long long)(size >> 20), enforce ? "enforcing" : "reporting"); - return; + return true; +} -fail: +void __init ctxr_exec_reserve(void) +{ + phys_addr_t base = 0, size = 0; + bool enforce = true; + bool from_dt = ctxr_parse_dt(&base, &size, &enforce); + + if (ctxr_cmd_enforce >= 0) + enforce = ctxr_cmd_enforce; + + /* A bad override leaves the machine as its bootloader described it. */ + if (ctxr_cmd_size) { + if (ctxr_establish(ctxr_cmd_base, ctxr_cmd_size, enforce)) + return; + pr_warn("ignoring ctxr_exec_window=\n"); + } + + if (!from_dt || ctxr_establish(base, size, enforce)) + return; + + /* The bootloader armed this window, so the kernel can't run outside it. */ if (enforce) panic("ctxr-execmem: cannot establish the executable window\n"); pr_warn("confinement disabled\n"); From d637de6296f8023cabc10b547a947181da1386e7 Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Fri, 6 Nov 2026 03:14:44 -0500 Subject: [PATCH 17/47] mm/execmem: keep CMA and failed reserved-memory out of the window The pool leaves out whatever was reserved before the window, but two paths still took window pages to the page allocator, and the verifier then panicked at boot. A reusable CMA region inside the window goes to the page allocator from cma_activate_area(). Don't activate a CMA area that overlaps the window, and keep its pages reserved. A reserved-memory node with a "reg" whose init fails is freed back to memblock from unflatten_device_tree(), after the window was reserved. That leaves a hole for memblock_free_all() to release, so reserve the window again before that. Record the window when it's reserved rather than when the pool is set up, because execmem_init() and CMA activation are both core_initcalls. Signed-off-by: Diljit Singh --- arch/arm64/include/asm/ctxr_execmem.h | 4 ++++ arch/arm64/mm/ctxr_execmem.c | 14 +++++++++++++- arch/arm64/mm/init.c | 2 ++ include/linux/execmem.h | 13 +++++++++++-- mm/cma.c | 21 +++++++++++++++++++++ mm/execmem_confine.c | 25 +++++++++++++++++-------- 6 files changed, 68 insertions(+), 11 deletions(-) diff --git a/arch/arm64/include/asm/ctxr_execmem.h b/arch/arm64/include/asm/ctxr_execmem.h index 06618c9f4dead..348de9f71d754 100644 --- a/arch/arm64/include/asm/ctxr_execmem.h +++ b/arch/arm64/include/asm/ctxr_execmem.h @@ -16,6 +16,9 @@ */ void ctxr_exec_reserve(void); +/* Close any hole left in the window before memblock releases free memory. */ +void ctxr_exec_keep_reserved(void); + /* * Build the gen_pool over the reserved window and register it with the execmem * confinement core. Called from execmem_arch_setup(). Returns true when @@ -31,6 +34,7 @@ bool ctxr_exec_execmem_setup(void); bool ctxr_exec_efi_runtime_ok(void); #else static inline void ctxr_exec_reserve(void) {} +static inline void ctxr_exec_keep_reserved(void) {} static inline bool ctxr_exec_execmem_setup(void) { return false; } static inline bool ctxr_exec_efi_runtime_ok(void) { return true; } #endif diff --git a/arch/arm64/mm/ctxr_execmem.c b/arch/arm64/mm/ctxr_execmem.c index 337111deb4116..59e9d35440a61 100644 --- a/arch/arm64/mm/ctxr_execmem.c +++ b/arch/arm64/mm/ctxr_execmem.c @@ -203,6 +203,8 @@ static bool __init ctxr_establish(phys_addr_t base, phys_addr_t size, bool enfor return false; } + execmem_confine_set_window(base, size); + ctxr_base = base; ctxr_size = size; ctxr_enforce = enforce; @@ -214,6 +216,16 @@ static bool __init ctxr_establish(phys_addr_t base, phys_addr_t size, bool enfor return true; } +/* + * A reserved-memory node whose init fails goes back to memblock, which can leave a + * hole in the window for memblock_free_all() to release. + */ +void __init ctxr_exec_keep_reserved(void) +{ + if (ctxr_active) + memblock_reserve(ctxr_base, ctxr_size); +} + void __init ctxr_exec_reserve(void) { phys_addr_t base = 0, size = 0; @@ -278,7 +290,7 @@ bool __init ctxr_exec_execmem_setup(void) goto fail_pool; } - if (execmem_confine_init(ctxr_base, ctxr_size, pool, ctxr_enforce)) { + if (execmem_confine_init(pool, ctxr_enforce)) { pr_err("failed to register the window\n"); goto fail_pool; } diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c index ce0e1b21bbd52..98e53dc23eb61 100644 --- a/arch/arm64/mm/init.c +++ b/arch/arm64/mm/init.c @@ -343,6 +343,8 @@ void __init arch_mm_preinit(void) { unsigned int flags = SWIOTLB_VERBOSE; + ctxr_exec_keep_reserved(); + if (max_pfn <= PFN_DOWN(arm64_dma_phys_limit)) { /* * If no bouncing needed for ZONE_DMA, reduce the swiotlb diff --git a/include/linux/execmem.h b/include/linux/execmem.h index e561b00dcaf88..204d1be409ca1 100644 --- a/include/linux/execmem.h +++ b/include/linux/execmem.h @@ -228,8 +228,7 @@ struct execmem_range; * * Return: 0 on success, negative errno otherwise. */ -int execmem_confine_init(phys_addr_t base, phys_addr_t size, - struct gen_pool *pool, bool enforce); +int execmem_confine_init(struct gen_pool *pool, bool enforce); /* True once a window has been registered. */ bool execmem_confine_active(void); @@ -240,12 +239,21 @@ bool execmem_confine_enforced(void); /* Window bounds, valid only when execmem_confine_active(). */ void execmem_confine_window(phys_addr_t *base, phys_addr_t *size); +/* + * Record the window once it is reserved. This comes ahead of the pool, so that + * nothing can release window pages between the two. + */ +void execmem_confine_set_window(phys_addr_t base, phys_addr_t size); + /* * True if @pfn is in the window. Userspace can't execute from the window, so * such a page must never reach the page allocator. */ bool execmem_confine_pfn_in_window(unsigned long pfn); +/* True if any of [@start, @start + @size) is in the window. */ +bool execmem_confine_overlaps(phys_addr_t start, phys_addr_t size); + /* * Internal backing allocator/free used by mm/execmem.c for EXECMEM_CONFINE * ranges. Not for general use. @@ -276,6 +284,7 @@ unsigned long execmem_confine_arch_verify(phys_addr_t base, phys_addr_t size, static inline bool execmem_confine_active(void) { return false; } static inline bool execmem_confine_enforced(void) { return false; } static inline bool execmem_confine_pfn_in_window(unsigned long pfn) { return false; } +static inline bool execmem_confine_overlaps(phys_addr_t start, phys_addr_t size) { return false; } #endif /* CONFIG_EXECMEM_CONFINE */ #endif /* _LINUX_EXECMEM_ALLOC_H */ diff --git a/mm/cma.c b/mm/cma.c index a10ea37a261da..75dbb94d2c447 100644 --- a/mm/cma.c +++ b/mm/cma.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include @@ -138,6 +139,20 @@ bool cma_validate_zones(struct cma *cma) return true; } +/* CMA hands its pages to the page allocator, and window pages must stay out of it. */ +static bool __init cma_overlaps_exec_window(struct cma *cma) +{ + for (int r = 0; r < cma->nranges; r++) { + struct cma_memrange *cmr = &cma->ranges[r]; + + if (execmem_confine_overlaps(PFN_PHYS(cmr->base_pfn), + PFN_PHYS(cmr->count))) + return true; + } + + return false; +} + static void __init cma_activate_area(struct cma *cma) { unsigned long pfn, end_pfn, early_pfn[CMA_MAX_RANGES]; @@ -154,6 +169,12 @@ static void __init cma_activate_area(struct cma *cma) goto cleanup; } + if (cma_overlaps_exec_window(cma)) { + pr_err("CMA area %s overlaps the executable window\n", cma->name); + cma_reserve_pages_on_error(cma); + goto cleanup; + } + if (!cma_validate_zones(cma)) goto cleanup; diff --git a/mm/execmem_confine.c b/mm/execmem_confine.c index f54e1d7d51209..60e0169f07372 100644 --- a/mm/execmem_confine.c +++ b/mm/execmem_confine.c @@ -55,26 +55,30 @@ static DEFINE_XARRAY(confine_chunks); /* serialises pool (de)allocation and the xarray */ static DEFINE_MUTEX(confine_lock); -int __init execmem_confine_init(phys_addr_t base, phys_addr_t size, - struct gen_pool *pool, bool enforce) +void __init execmem_confine_set_window(phys_addr_t base, phys_addr_t size) +{ + confine_base = base; + confine_size = size; +} + +int __init execmem_confine_init(struct gen_pool *pool, bool enforce) { if (WARN_ON(confine_ready)) return -EBUSY; - if (WARN_ON(!pool || !size)) + if (WARN_ON(!pool || !confine_size)) return -EINVAL; - confine_base = base; - confine_size = size; confine_pool = pool; confine_enforce = enforce; smp_wmb(); confine_ready = true; { - phys_addr_t end = base + size; + phys_addr_t end = confine_base + confine_size; pr_info("window [%pa, %pa) (%llu MiB), %s\n", - &base, &end, (unsigned long long)(size >> 20), + &confine_base, &end, + (unsigned long long)(confine_size >> 20), enforce ? "enforcing" : "reporting"); } return 0; @@ -105,7 +109,12 @@ static bool phys_in_window(phys_addr_t pa) bool execmem_confine_pfn_in_window(unsigned long pfn) { - return confine_ready && phys_in_window(PFN_PHYS(pfn)); + return phys_in_window(PFN_PHYS(pfn)); +} + +bool __init execmem_confine_overlaps(phys_addr_t start, phys_addr_t size) +{ + return start < confine_base + confine_size && confine_base < start + size; } /* Window pages stay reserved for good, so one that isn't went to the allocator. */ From b3a9578538ffc23398a826c1613dce8480beed5d Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Fri, 6 Nov 2026 03:14:45 -0500 Subject: [PATCH 18/47] mm/execmem: tell KASAN and kmemleak about confined allocations __vmalloc_node_range() unpoisons a VM_ALLOC area once it's mapped and registers it with kmemleak, and vfree() unregisters it. A confined allocation goes around both, so on a debug kernel KASAN sees the area as poisoned and kmemleak doesn't know it. Do the same here. Signed-off-by: Diljit Singh --- mm/execmem_confine.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/mm/execmem_confine.c b/mm/execmem_confine.c index 60e0169f07372..572ee98a7fe5f 100644 --- a/mm/execmem_confine.c +++ b/mm/execmem_confine.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -215,6 +216,13 @@ void *execmem_confine_alloc(struct execmem_range *range, size_t size, kasan_alloc_module_shadow(area->addr, size, GFP_KERNEL) < 0) goto err_xa; + /* + * __vmalloc_node_range() does these two for vmalloc memory. KASAN doesn't + * tag executable memory, so the address stays as it is. + */ + area->addr = kasan_unpoison_vmalloc(area->addr, size, KASAN_VMALLOC_VM_ALLOC); + kmemleak_vmalloc(area, size, GFP_KERNEL); + return area->addr; err_xa: @@ -261,6 +269,8 @@ bool execmem_confine_free(void *ptr) nr = ck->size >> PAGE_SHIFT; + kmemleak_free(ptr); + /* * We bypass vfree(), so VM_FLUSH_RESET_PERMS does not run for us: drop * the mapping back to non-executable, writable before tearing it down, From 524f9114cf129c758b80d5d91ca845bf2f6fefaf Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Fri, 6 Nov 2026 03:15:01 -0500 Subject: [PATCH 19/47] dt-bindings: chosen: add the CTXR executable window Describe the /chosen properties a bootloader uses to give the kernel its executable window. Signed-off-by: Diljit Singh --- .../chosen/aurora,ctxr-exec-window.yaml | 50 +++++++++++++++++++ MAINTAINERS | 1 + 2 files changed, 51 insertions(+) create mode 100644 Documentation/devicetree/bindings/chosen/aurora,ctxr-exec-window.yaml diff --git a/Documentation/devicetree/bindings/chosen/aurora,ctxr-exec-window.yaml b/Documentation/devicetree/bindings/chosen/aurora,ctxr-exec-window.yaml new file mode 100644 index 0000000000000..bbffbf958ec9c --- /dev/null +++ b/Documentation/devicetree/bindings/chosen/aurora,ctxr-exec-window.yaml @@ -0,0 +1,50 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/chosen/aurora,ctxr-exec-window.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Executable window for a kernel running behind a CTXR + +maintainers: + - Diljit Singh + +description: + On Apple silicon from the M4 generation on, the boot firmware arms a code + text execute range on the secondary CPUs, so privileged code there can only + be fetched from one physical window. The bootloader tells the kernel where + that window is, and the kernel keeps all of its executable code inside it. + See Documentation/arch/arm64/ctxr-execmem.rst. + +select: + properties: + $nodename: + const: chosen + required: + - $nodename + +properties: + $nodename: + const: chosen + + aurora,ctxr-exec-window: + $ref: /schemas/types.yaml#/definitions/uint64-array + description: The physical window the kernel's executable code has to stay in. + items: + - description: + Physical base of the window. Zero tells the kernel to anchor the + window on its own image. + - description: Size of the window in bytes. + + aurora,ctxr-exec-enforce: + $ref: /schemas/types.yaml#/definitions/uint32 + enum: [0, 1] + description: + One if code outside the window is fatal, zero if the kernel only reports + it. A window with no such property is enforcing. + +dependentRequired: + aurora,ctxr-exec-enforce: [ aurora,ctxr-exec-window ] + +additionalProperties: true +... diff --git a/MAINTAINERS b/MAINTAINERS index e0a0070243ae6..1b47aea307e78 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -2090,6 +2090,7 @@ F: drivers/soc/apple/pmp.rs F: include/linux/mt7932.h F: include/linux/soc/apple/pmp-report.h F: Documentation/devicetree/bindings/chosen/apple,dcp-rtkit-quiesced.yaml +F: Documentation/devicetree/bindings/chosen/aurora,ctxr-exec-window.yaml F: Documentation/devicetree/bindings/display/apple,dcp.yaml F: Documentation/devicetree/bindings/display/apple,display-subsystem.yaml F: Documentation/PCI/controller/apple-t8140.rst From c4b5fab28dda86672ad705921dc2de42b9766f36 Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Fri, 6 Nov 2026 03:15:01 -0500 Subject: [PATCH 20/47] arm64: ctxr-execmem: document the options and what a window gives up Add the command-line options to kernel-parameters.txt and describe how they treat bad values. Say that a confined machine loses CPU hotplug and kexec, and that hibernate is untested under a window. aura doesn't emit aurora,ctxr-exec-enforce, so it's no longer shown as part of what the bootloader emits. The Kconfig help said to say N if unsure while asahi.config turns the option on, so say who wants it. Signed-off-by: Diljit Singh --- .../admin-guide/kernel-parameters.txt | 21 ++++++++ Documentation/arch/arm64/ctxr-execmem.rst | 53 +++++++++++++------ arch/arm64/Kconfig | 5 +- 3 files changed, 61 insertions(+), 18 deletions(-) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt index 18f2e943bfb29..9cd5da988c7bd 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -1138,6 +1138,27 @@ Kernel parameters option depends on the CSD_LOCK_WAIT_DEBUG_DEFAULT Kconfig option. + ctxr_exec_enforce= + [ARM64,EARLY] Whether code outside the CTXR executable + window is fatal. Applies to a window from the device + tree or from ctxr_exec_window=, and overrides + aurora,ctxr-exec-enforce in the device tree. + Format: + Default: 1 + + ctxr_exec_selftest + [ARM64,EARLY] Run two extra checks of the CTXR + executable window at boot. + + ctxr_exec_window= + [ARM64,EARLY] Confine kernel executable code to a + physical window, overriding the device tree. This is + for bring-up and VMs. A value that doesn't parse, or a + window that can't work, is ignored with a warning. + See Documentation/arch/arm64/ctxr-execmem.rst. + Format: [[KMG][@[KMG]]] + Default size: 128M. Default base: from the kernel image. + dasd= [HW,NET] See header of drivers/s390/block/dasd_devmap.c. diff --git a/Documentation/arch/arm64/ctxr-execmem.rst b/Documentation/arch/arm64/ctxr-execmem.rst index e628852a67689..438c904cbfdef 100644 --- a/Documentation/arch/arm64/ctxr-execmem.rst +++ b/Documentation/arch/arm64/ctxr-execmem.rst @@ -24,20 +24,29 @@ Device tree (production) The bootloader emits, under ``/chosen``:: aurora,ctxr-exec-window = ; - aurora,ctxr-exec-enforce = <1>; /* optional, default 1 */ A ``base`` of 0 tells the kernel to anchor the window on its own loaded - image (rounded down to 2 MiB). ``size`` is the window size in bytes. + image (rounded down to 2 MiB). ``size`` is the window size in bytes. The + window is enforcing unless ``aurora,ctxr-exec-enforce = <0>`` is also + there, which aura doesn't emit. See + ``Documentation/devicetree/bindings/chosen/aurora,ctxr-exec-window.yaml``. Command line (bring-up / VM) :: - ctxr_exec_window=[@] e.g. ctxr_exec_window=128M - ctxr_exec_enforce=0 (default: enforcing) + ctxr_exec_window=[[@]] e.g. ctxr_exec_window=128M + ctxr_exec_enforce= (default: enforcing) - The command line overrides the device tree. ``size`` accepts the usual - ``K``/``M``/``G`` suffixes; if ``@base`` is omitted it is derived from the - kernel image. + The command line overrides the device tree, and ``ctxr_exec_enforce=`` + applies to a window from either place. ``size`` accepts the usual + ``K``/``M``/``G`` suffixes and defaults to 128 MiB; if ``@base`` is omitted + it is derived from the kernel image. The last use of an option wins. + +A value that doesn't parse is ignored with a warning. So is a window from the +command line that can't work because the image doesn't fit it or it isn't in +DRAM, and the device tree's window then applies if there is one. A window from +the device tree that can't work is fatal when enforcing, because the kernel +can't run on the confined cores outside it. How it works ============ @@ -69,9 +78,11 @@ it up. Anything EL0 can execute therefore lives outside the window: * The vDSO's code is part of the image, so userspace gets a copy in ordinary memory. * No window page reaches the page allocator. ``free_reserved_area()`` skips them, - so freed ``__init`` memory stays reserved, as does an initrd in the window. RAM - in the window that the pool doesn't use is not available to the rest of the - system, so a window should be no larger than the executable code needs. + so freed ``__init`` memory stays reserved, as does an initrd in the window. A + CMA area that overlaps the window isn't activated, and a reserved-memory node + whose init fails stays reserved. RAM in the window that the pool doesn't use is + not available to the rest of the system, so a window should be no larger than + the executable code needs. The zero page stays in the window, so a program that jumps into fresh executable memory before writing it still gets there. When the window is enforcing, an EL0 @@ -89,13 +100,23 @@ aligned to the kernel's page size, the kernel warns once and turns runtime services off for that boot, as ``efi=noruntime`` would. When all of it is inside, runtime services stay on. A window in reporting mode leaves them on. -kexec and crash kernels -======================= +What a confined machine gives up +================================ -The relocation code runs from an ordinary page, outside the window, and a crash -dump can start on a confined core, where it would hang during a panic. While a -window is enforcing, ``machine_kexec_prepare()`` therefore refuses both, so -``kexec_load()`` and ``kexec_file_load()`` fail with ``EOPNOTSUPP``. +CPU hotplug + The kernel runs at EL2 with no monitor below it, so no PSCI, and CPUs start + through spin-table, which has no way to take a CPU down. The CPUs have no + ``online`` file in sysfs, and anything that needs them offline fails. + +kexec and crash kernels + The relocation code runs from an ordinary page, outside the window, and a + crash dump can start on a confined core, where it would hang during a panic. + While a window is enforcing, ``machine_kexec_prepare()`` refuses both, so + ``kexec_load()`` and ``kexec_file_load()`` fail with ``EOPNOTSUPP``. + +Hibernate + Resume runs from a ``trans_pgd`` page outside the window. This is unchanged + and untested under a window. Verifying confinement ===================== diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 054969dc7bae2..e7d7418b27660 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -2556,8 +2556,9 @@ config ARM64_CTXR_EXECMEM A verifier sweeps the kernel page tables and reports (or, when the window is enforcing, panics on) any executable mapping that escapes. - This has no effect unless a window is supplied at boot. If unsure, - say N. + This has no effect unless the bootloader or the command line supplies + a window, so a distribution kernel for Apple silicon can enable it. + Say Y for such a kernel, and N otherwise. config CC_HAVE_STACKPROTECTOR_SYSREG def_bool $(cc-option,-mstack-protector-guard=sysreg -mstack-protector-guard-reg=sp_el0 -mstack-protector-guard-offset=0) From 1c4ce70047bbe102ab70d3d8c236d394f2f66aad Mon Sep 17 00:00:00 2001 From: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> Date: Fri, 6 Nov 2026 18:25:37 +1100 Subject: [PATCH 21/47] arm64: ctxr-execmem: treat an active window as confining, enforcing or not Whether a core can fetch from outside the window is decided by the hardware, not by aurora,ctxr-exec-enforce or ctxr_exec_enforce=. Those only decide whether a verifier finding is fatal. EFI runtime services, kexec and the EL0 fetch check keyed on enforcement, so a confined machine booted with enforcement off, now possible from the command line, would run EFI runtime code from efi_rts_wq on a confined core, allow a kexec that hangs, and leave a task faulting forever on the zero page. Key all three on the window being active. Assisted-by: LLM Signed-off-by: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> --- Documentation/arch/arm64/ctxr-execmem.rst | 13 +++++++++---- arch/arm64/include/asm/ctxr_execmem.h | 2 +- arch/arm64/kernel/machine_kexec.c | 2 +- arch/arm64/mm/ctxr_execmem.c | 2 +- arch/arm64/mm/fault.c | 2 +- 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/Documentation/arch/arm64/ctxr-execmem.rst b/Documentation/arch/arm64/ctxr-execmem.rst index 438c904cbfdef..6b17d214b0f1c 100644 --- a/Documentation/arch/arm64/ctxr-execmem.rst +++ b/Documentation/arch/arm64/ctxr-execmem.rst @@ -48,6 +48,11 @@ DRAM, and the device tree's window then applies if there is one. A window from the device tree that can't work is fatal when enforcing, because the kernel can't run on the confined cores outside it. +``enforce`` only decides whether a confinement violation found by the verifier +(see below) is fatal. Whether the hardware confines a core does not depend on +it, so everything else in this document applies whenever a window is active, +enforcing or not. + How it works ============ @@ -85,7 +90,7 @@ it up. Anything EL0 can execute therefore lives outside the window: the executable code needs. The zero page stays in the window, so a program that jumps into fresh executable -memory before writing it still gets there. When the window is enforcing, an EL0 +memory before writing it still gets there. When a window is active, an EL0 instruction permission fault on a present, executable mapping of a window page logs the task and its address and PFN, then kills it with an uncatchable ``SIGSEGV`` rather than retrying. Any other fault takes the usual path. @@ -95,10 +100,10 @@ EFI runtime services EFI runtime code runs at EL2 on whichever core ``efi_rts_wq`` happens to be on, so on a confined core it has to be inside the window too. When a window is -enforcing and any ``EFI_RUNTIME_SERVICES_CODE`` region is outside it, or isn't +active and any ``EFI_RUNTIME_SERVICES_CODE`` region is outside it, or isn't aligned to the kernel's page size, the kernel warns once and turns runtime services off for that boot, as ``efi=noruntime`` would. When all of it is inside, -runtime services stay on. A window in reporting mode leaves them on. +runtime services stay on. What a confined machine gives up ================================ @@ -111,7 +116,7 @@ CPU hotplug kexec and crash kernels The relocation code runs from an ordinary page, outside the window, and a crash dump can start on a confined core, where it would hang during a panic. - While a window is enforcing, ``machine_kexec_prepare()`` refuses both, so + While a window is active, ``machine_kexec_prepare()`` refuses both, so ``kexec_load()`` and ``kexec_file_load()`` fail with ``EOPNOTSUPP``. Hibernate diff --git a/arch/arm64/include/asm/ctxr_execmem.h b/arch/arm64/include/asm/ctxr_execmem.h index 348de9f71d754..584def0e4c273 100644 --- a/arch/arm64/include/asm/ctxr_execmem.h +++ b/arch/arm64/include/asm/ctxr_execmem.h @@ -28,7 +28,7 @@ void ctxr_exec_keep_reserved(void); bool ctxr_exec_execmem_setup(void); /* - * False if a window is enforcing and EFI runtime code can't run behind it. + * False if a window is active and EFI runtime code can't run behind it. * Warns once when it returns false. */ bool ctxr_exec_efi_runtime_ok(void); diff --git a/arch/arm64/kernel/machine_kexec.c b/arch/arm64/kernel/machine_kexec.c index ac8471c47229f..9c4a407222ea1 100644 --- a/arch/arm64/kernel/machine_kexec.c +++ b/arch/arm64/kernel/machine_kexec.c @@ -59,7 +59,7 @@ int machine_kexec_prepare(struct kimage *kimage) * The relocation code runs from an ordinary page, outside the window, * and a crash dump can start on a core enforcing it. */ - if (execmem_confine_enforced()) { + if (execmem_confine_active()) { pr_err("Can't kexec: the kernel's code is confined to a CTXR window.\n"); return -EOPNOTSUPP; } diff --git a/arch/arm64/mm/ctxr_execmem.c b/arch/arm64/mm/ctxr_execmem.c index 59e9d35440a61..74828a0445ee4 100644 --- a/arch/arm64/mm/ctxr_execmem.c +++ b/arch/arm64/mm/ctxr_execmem.c @@ -318,7 +318,7 @@ bool __init ctxr_exec_efi_runtime_ok(void) { efi_memory_desc_t *md; - if (!IS_ENABLED(CONFIG_EFI) || !ctxr_active || !ctxr_enforce) + if (!IS_ENABLED(CONFIG_EFI) || !ctxr_active) return true; for_each_efi_memory_desc(md) { diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c index a2ee7770a5738..ac1f23f911138 100644 --- a/arch/arm64/mm/fault.c +++ b/arch/arm64/mm/fault.c @@ -618,7 +618,7 @@ static bool el0_fetch_refused_by_window(struct mm_struct *mm, unsigned long addr unsigned long pfn = 0; if (!is_el0_instruction_abort(esr) || !esr_fsc_is_permission_fault(esr) || - !execmem_confine_enforced()) + !execmem_confine_active()) return false; if (mmap_read_lock_killable(mm)) From 20519af6fc3dbe01843599a181615b9e96577b33 Mon Sep 17 00:00:00 2001 From: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> Date: Fri, 6 Nov 2026 18:25:38 +1100 Subject: [PATCH 22/47] arm64: mm: keep the zero page out of an active CTXR window empty_zero_page is in the image's .bss, so it is inside the window. Any anonymous mapping that is read before it is written maps it, including PROT_EXEC ones, userfaultfd's UFFDIO_ZEROPAGE and KSM's zero pages, and KVM can map it into a guest's stage 2 for untouched guest RAM. EL0 can't execute from the window on a confined core, so an unprivileged program that jumps into such memory is killed there and gets SIGILL anywhere else, and a guest fetching from it may fault to EL2 again and again. With a window reserved, allocate the zero pages from memblock, outside the window, and clear their MTE tags as cpu_enable_mte() does for empty_zero_page. The fault-path kill stays as a backstop. Assisted-by: LLM Signed-off-by: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> --- Documentation/arch/arm64/ctxr-execmem.rst | 14 +++++++---- arch/arm64/include/asm/ctxr_execmem.h | 7 ++++++ arch/arm64/mm/ctxr_execmem.c | 5 ++++ arch/arm64/mm/init.c | 30 ++++++++++++++++++++++- 4 files changed, 50 insertions(+), 6 deletions(-) diff --git a/Documentation/arch/arm64/ctxr-execmem.rst b/Documentation/arch/arm64/ctxr-execmem.rst index 6b17d214b0f1c..8dabf641af5c5 100644 --- a/Documentation/arch/arm64/ctxr-execmem.rst +++ b/Documentation/arch/arm64/ctxr-execmem.rst @@ -89,11 +89,15 @@ it up. Anything EL0 can execute therefore lives outside the window: not available to the rest of the system, so a window should be no larger than the executable code needs. -The zero page stays in the window, so a program that jumps into fresh executable -memory before writing it still gets there. When a window is active, an EL0 -instruction permission fault on a present, executable mapping of a window page -logs the task and its address and PFN, then kills it with an uncatchable -``SIGSEGV`` rather than retrying. Any other fault takes the usual path. +* The zero page is part of the image too, and every untouched anonymous page + that is read before it is written maps it, executable mappings included. With + a window active, ``arch_setup_zero_pages()`` allocates the zero pages from + memblock instead, outside the window. + +As a backstop, when a window is active, an EL0 instruction permission fault on a +present, executable mapping of a window page logs the task and its address and +PFN, then kills it with an uncatchable ``SIGSEGV`` rather than retrying. Any +other fault takes the usual path. EFI runtime services ==================== diff --git a/arch/arm64/include/asm/ctxr_execmem.h b/arch/arm64/include/asm/ctxr_execmem.h index 584def0e4c273..990cfe355dd5d 100644 --- a/arch/arm64/include/asm/ctxr_execmem.h +++ b/arch/arm64/include/asm/ctxr_execmem.h @@ -32,11 +32,18 @@ bool ctxr_exec_execmem_setup(void); * Warns once when it returns false. */ bool ctxr_exec_efi_runtime_ok(void); + +/* + * True once ctxr_exec_reserve() has reserved a window. Valid from + * arm64_memblock_init() until the end of init. + */ +bool ctxr_exec_window_reserved(void); #else static inline void ctxr_exec_reserve(void) {} static inline void ctxr_exec_keep_reserved(void) {} static inline bool ctxr_exec_execmem_setup(void) { return false; } static inline bool ctxr_exec_efi_runtime_ok(void) { return true; } +static inline bool ctxr_exec_window_reserved(void) { return false; } #endif #endif /* __ASM_CTXR_EXECMEM_H */ diff --git a/arch/arm64/mm/ctxr_execmem.c b/arch/arm64/mm/ctxr_execmem.c index 74828a0445ee4..1f39996f3ad3e 100644 --- a/arch/arm64/mm/ctxr_execmem.c +++ b/arch/arm64/mm/ctxr_execmem.c @@ -309,6 +309,11 @@ bool __init ctxr_exec_execmem_setup(void) return false; } +bool __init ctxr_exec_window_reserved(void) +{ + return ctxr_active; +} + /* * A core enforcing the window can't fetch firmware code outside it, and * efi_rts_wq can run on any core. Misaligned code also makes the misaligned diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c index 98e53dc23eb61..9ddd592c17cdf 100644 --- a/arch/arm64/mm/init.c +++ b/arch/arm64/mm/init.c @@ -43,6 +43,7 @@ #include #include #include +#include #include #include #include @@ -336,7 +337,34 @@ void __init bootmem_init(void) void __init arch_setup_zero_pages(void) { - __zero_page = phys_to_page(__pa_symbol(empty_zero_page)); + unsigned long off; + void *zero = NULL; + + /* + * Every untouched anonymous page that is read before it is written + * maps the zero page, executable mappings included, and EL0 can't + * execute from an active CTXR window, where empty_zero_page lies. + */ + if (ctxr_exec_window_reserved()) { + zero = memblock_alloc(ARCH_ZERO_PAGE_SIZE, ARCH_ZERO_PAGE_SIZE); + if (!zero) + pr_warn("ctxr-execmem: no memory for a zero page outside the window\n"); + } + + if (!zero) { + __zero_page = phys_to_page(__pa_symbol(empty_zero_page)); + return; + } + + /* + * As cpu_enable_mte() does for empty_zero_page: the zero page is + * always mapped pte_special(), so nothing else clears its tags. + */ + if (system_supports_mte()) + for (off = 0; off < ARCH_ZERO_PAGE_SIZE; off += PAGE_SIZE) + mte_clear_page_tags(zero + off); + + __zero_page = virt_to_page(zero); } void __init arch_mm_preinit(void) From a57207316f8b1bc18b5f3048a26475b31640c6f4 Mon Sep 17 00:00:00 2001 From: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> Date: Fri, 6 Nov 2026 18:32:02 +1100 Subject: [PATCH 23/47] arm64: head: take the direct VHE entry only when VHE is implemented The direct entry for Apple parts from 0x50 sets E2H without checking ID_AA64MMFR1_EL1.VH. A guest whose virtual EL2 has no VHE, such as a nested KVM guest on an M4 host that sees the host's MIDR, would hang in early boot. Fall back to the usual path when VH is 0. Assisted-by: LLM Signed-off-by: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> --- arch/arm64/kernel/head.S | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/arm64/kernel/head.S b/arch/arm64/kernel/head.S index 7b724c06353c9..50164fe15991c 100644 --- a/arch/arm64/kernel/head.S +++ b/arch/arm64/kernel/head.S @@ -321,6 +321,10 @@ SYM_INNER_LABEL(init_el2, SYM_L_LOCAL) ubfx x0, x0, #MIDR_PARTNUM_SHIFT, #12 cmp x0, #0x50 b.lo .Lctxr_no_direct_vhe + /* A virtual EL2 without VHE, as nested KVM may give a guest. */ + mrs x0, id_aa64mmfr1_el1 + ubfx x0, x0, #ID_AA64MMFR1_EL1_VH_SHIFT, #4 + cbz x0, .Lctxr_no_direct_vhe finalise_el2_state From e82a8514a6f300bbbde4774d325125b0eef842d8 Mon Sep 17 00:00:00 2001 From: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> Date: Fri, 6 Nov 2026 21:09:34 +1100 Subject: [PATCH 24/47] mm/execmem: flush before a confined chunk goes back to the pool execmem_confine_free() unmaps a chunk with free_vm_area(), which leaves the TLB flush to the lazy vmap purge, and then returns the pages to the pool at once. Until that purge, a CPU can still hold a writable entry for the old address while the same pages are mapped as another module's text. vfree() avoids this for VM_FLUSH_RESET_PERMS areas; this path bypasses vfree(). Flush the range before gen_pool_free(), on the free path and on the allocation error path. A vmap_pages_range() failure can also leave the area partly mapped, and its error path freed the pages without unmapping them. Send it through err_map, which unmaps and now flushes. Assisted-by: LLM Signed-off-by: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> --- mm/execmem_confine.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/mm/execmem_confine.c b/mm/execmem_confine.c index 572ee98a7fe5f..b7b817fff1abc 100644 --- a/mm/execmem_confine.c +++ b/mm/execmem_confine.c @@ -35,6 +35,8 @@ #include #include +#include + #include "internal.h" #include "vmalloc.h" @@ -189,7 +191,7 @@ void *execmem_confine_alloc(struct execmem_range *range, size_t size, vaddr = (unsigned long)area->addr; err = vmap_pages_range(vaddr, vaddr + size, pgprot, pages, PAGE_SHIFT); if (err) - goto err_pool; + goto err_map; /* may be partly mapped */ /* * Publish the backing pages on the vm_struct. arm64's set_memory_rox() @@ -231,16 +233,17 @@ void *execmem_confine_alloc(struct execmem_range *range, size_t size, mutex_unlock(&confine_lock); err_map: free_vm_area(area); /* unmaps the range and frees bookkeeping */ + /* + * free_vm_area() leaves the TLB flush to the lazy vmap purge: flush now, + * before the pages can be mapped again for someone else. + */ + flush_tlb_kernel_range(vaddr, vaddr + size); mutex_lock(&confine_lock); gen_pool_free(confine_pool, pool_va, size); mutex_unlock(&confine_lock); kfree(ck); kfree(pages); return NULL; -err_pool: - mutex_lock(&confine_lock); - gen_pool_free(confine_pool, pool_va, size); - mutex_unlock(&confine_lock); err_ck: kfree(ck); err_pages: @@ -281,6 +284,13 @@ bool execmem_confine_free(void *ptr) free_vm_area(ck->area); + /* + * free_vm_area() leaves the TLB flush to the lazy vmap purge. Flush + * before the pages go back to the pool, or a stale writable entry for + * this address could reach the next module's text. + */ + flush_tlb_kernel_range(vaddr, vaddr + ck->size); + mutex_lock(&confine_lock); gen_pool_free(confine_pool, ck->pool_va, ck->size); mutex_unlock(&confine_lock); From 46c5ae7cd01b4a0a0ea5dd820a2365dd2ca67e44 Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Fri, 6 Nov 2026 11:40:53 -0500 Subject: [PATCH 25/47] arm64: ctxr-execmem: keep the boot CPU on the usual entry path The direct VHE entry in init_el2 was keyed on the part number, so every M4-generation and later core took it on every entry, with or without a window, and the boot CPU took it before the feature overrides were parsed. A kernel with no window entered differently from one without CONFIG_ARM64_CTXR_EXECMEM, and kvm-arm.mode=protected got VHE KVM. Only a confined core has to skip the trip through EL1, and the boot CPU isn't confined. It now enters as it always has. Once it has reserved a window at EL2 it sets ctxr_stay_el2 and cleans it to the PoC, and later entries, which read it with the MMU off, take the direct path only when it is set. With no window, every core enters as it would without the option. The boot CPU's trip through EL1 inside the window is safe: its CTXR_D_CTL_EL1 reads 0 on the T8140, and this split (boot CPU through EL1, secondaries direct) brought up 6 CPUs at EL2 behind an enforcing window on the Neo. Signed-off-by: Diljit Singh --- Documentation/arch/arm64/ctxr-execmem.rst | 9 +++++---- arch/arm64/include/asm/ctxr_execmem.h | 3 +++ arch/arm64/kernel/head.S | 17 ++++++----------- arch/arm64/mm/ctxr_execmem.c | 11 +++++++++++ 4 files changed, 25 insertions(+), 15 deletions(-) diff --git a/Documentation/arch/arm64/ctxr-execmem.rst b/Documentation/arch/arm64/ctxr-execmem.rst index 8dabf641af5c5..19dcb0117834f 100644 --- a/Documentation/arch/arm64/ctxr-execmem.rst +++ b/Documentation/arch/arm64/ctxr-execmem.rst @@ -68,10 +68,11 @@ How it works * If the window's pool is exhausted the allocation fails, exactly as a hardware CTXR fault would prevent executing from outside the window; there is no buddy fallback. -* Code in the window can't execute at EL1 on a confined core, and the window - isn't known at the first instruction, so cores from the M4 generation on switch - to VHE in ``init_kernel_el`` on every entry at EL2. Earlier cores don't have - CTXR and enter as they always have. +* Code in the window can't execute at EL1 on a confined core. The boot CPU isn't + confined and enters as it always has, through EL1 and ``finalise_el2``. Once it + has reserved a window at EL2, it sets a flag that later entries read with the + MMU off, and those switch to VHE in ``init_kernel_el`` without running at EL1. + With no window, every core enters as it always has. Userspace ========= diff --git a/arch/arm64/include/asm/ctxr_execmem.h b/arch/arm64/include/asm/ctxr_execmem.h index 990cfe355dd5d..ecbd02884f6c0 100644 --- a/arch/arm64/include/asm/ctxr_execmem.h +++ b/arch/arm64/include/asm/ctxr_execmem.h @@ -9,6 +9,9 @@ #include #ifdef CONFIG_ARM64_CTXR_EXECMEM +/* Set once the boot CPU has a window at EL2; see init_kernel_el. */ +extern u32 ctxr_stay_el2; + /* * Parse the window (from the kernel command line or the device tree), validate * that the kernel image fits inside it, and reserve it from memblock. Called diff --git a/arch/arm64/kernel/head.S b/arch/arm64/kernel/head.S index 50164fe15991c..21b0fc15c2742 100644 --- a/arch/arm64/kernel/head.S +++ b/arch/arm64/kernel/head.S @@ -309,18 +309,13 @@ SYM_INNER_LABEL(init_el2, SYM_L_LOCAL) #ifdef CONFIG_ARM64_CTXR_EXECMEM /* - * Kernel text inside an armed CTXR window can't execute at EL1, and - * the window isn't known this early. Apple cores from the M4 generation - * on (part numbers from 0x50) have CTXR and are VHE only, so they go - * straight to VHE here on every entry and never run at EL1. + * A core confined to a CTXR window can't run the window's code at + * EL1. Once the boot CPU has a window at EL2, later entries switch + * to VHE here instead of going through EL1 and finalise_el2. */ - mrs x0, midr_el1 - ubfx x1, x0, #MIDR_IMPLEMENTOR_SHIFT, #8 - cmp x1, #ARM_CPU_IMP_APPLE - b.ne .Lctxr_no_direct_vhe - ubfx x0, x0, #MIDR_PARTNUM_SHIFT, #12 - cmp x0, #0x50 - b.lo .Lctxr_no_direct_vhe + adr_l x0, ctxr_stay_el2 + ldr w0, [x0] + cbz w0, .Lctxr_no_direct_vhe /* A virtual EL2 without VHE, as nested KVM may give a guest. */ mrs x0, id_aa64mmfr1_el1 ubfx x0, x0, #ID_AA64MMFR1_EL1_VH_SHIFT, #4 diff --git a/arch/arm64/mm/ctxr_execmem.c b/arch/arm64/mm/ctxr_execmem.c index 1f39996f3ad3e..947614c63c4c5 100644 --- a/arch/arm64/mm/ctxr_execmem.c +++ b/arch/arm64/mm/ctxr_execmem.c @@ -34,11 +34,13 @@ #include #include +#include #include #include #include #include #include +#include /* Window granularity when the base is derived rather than given. */ #define CTXR_WINDOW_ALIGN SZ_2M @@ -50,6 +52,9 @@ static phys_addr_t ctxr_size __initdata; static bool ctxr_enforce __initdata = true; static bool ctxr_active __initdata; +/* Read with the MMU off by cores entering init_kernel_el. */ +u32 ctxr_stay_el2 __section(".mmuoff.data.read"); + /* * Free, linear-mapped RAM inside the window, snapshotted just before the * window is reserved. Anything already reserved at that point (the kernel @@ -205,6 +210,12 @@ static bool __init ctxr_establish(phys_addr_t base, phys_addr_t size, bool enfor execmem_confine_set_window(base, size); + if (is_kernel_in_hyp_mode()) { + WRITE_ONCE(ctxr_stay_el2, 1); + dcache_clean_inval_poc((unsigned long)&ctxr_stay_el2, + (unsigned long)&ctxr_stay_el2 + sizeof(ctxr_stay_el2)); + } + ctxr_base = base; ctxr_size = size; ctxr_enforce = enforce; From 91c85e7bf4ff305399e415d7d74f0f8ad470de60 Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Fri, 6 Nov 2026 11:42:19 -0500 Subject: [PATCH 26/47] arm64: ctxr-execmem: keep secondaries offline if the window can't be used A window from the device tree that couldn't be used panicked when enforcing. With ctxr_exec_enforce=0 the kernel printed "confinement disabled" and released the secondaries anyway, although they are still confined and can't run the kernel outside the window. A malformed property was treated as no property at all, so the secondaries were released then too. The boot CPU isn't confined, so such a machine can still boot on it alone. When the device tree has a window and any of these holds, log why, leave the secondaries in the bootloader and carry on with one CPU: - the property isn't four cells, has a size of 0, isn't page aligned or wraps, - the kernel isn't at EL2, so it would run the secondaries at EL1, - the image doesn't fit the window, the window isn't in DRAM, or it can't be reserved, - the pool can't be built, for example because nothing in the window is free. smp_prepare_cpus() then doesn't prepare or mark present any secondary, so no release address is written and no CPU can be brought online later. The pool failure applies to a command-line window too. A bad command-line override still falls back to the device tree's window. Enforcement now only decides whether a verifier finding is fatal. Machines with no window property are unchanged. The window isn't cross-checked against CTXR_D. The boot CPU's copy doesn't describe the window, and a secondary can only read its own once it runs kernel code, which needs the image inside the window already. The registers also don't exist on every Apple core, and an undefined register read that early can't be recovered. The bootloader reads CTXR_D when it picks the window. The documentation says so. Signed-off-by: Diljit Singh --- Documentation/arch/arm64/ctxr-execmem.rst | 21 +++++++-- arch/arm64/include/asm/ctxr_execmem.h | 4 ++ arch/arm64/kernel/smp.c | 5 +++ arch/arm64/mm/ctxr_execmem.c | 52 ++++++++++++++++------- 4 files changed, 64 insertions(+), 18 deletions(-) diff --git a/Documentation/arch/arm64/ctxr-execmem.rst b/Documentation/arch/arm64/ctxr-execmem.rst index 19dcb0117834f..0cea8f9b996ad 100644 --- a/Documentation/arch/arm64/ctxr-execmem.rst +++ b/Documentation/arch/arm64/ctxr-execmem.rst @@ -44,9 +44,24 @@ Command line (bring-up / VM) A value that doesn't parse is ignored with a warning. So is a window from the command line that can't work because the image doesn't fit it or it isn't in -DRAM, and the device tree's window then applies if there is one. A window from -the device tree that can't work is fatal when enforcing, because the kernel -can't run on the confined cores outside it. +DRAM, and the device tree's window then applies if there is one. + +The secondary CPUs can only run code inside the device tree's window, and the +boot CPU isn't confined. So when that window can't be used, the kernel logs why, +leaves the secondaries in the bootloader and boots on the boot CPU alone, as with +``nosmp``. That happens when the property is malformed (not four cells, a size of +0, a base or size that isn't page aligned, or one that wraps), when the image +doesn't fit the window or the window isn't in DRAM, when it can't be reserved, +when no free memory is left in it for executable allocations, and when the kernel +isn't running at EL2 (``kvm-arm.mode=nvhe`` or ``protected``, for example). With +no such property nothing changes. + +The kernel doesn't check the device tree's window against ``CTXR_D``. The boot +CPU's copy doesn't describe the window, and a secondary can only read its own +once it is running kernel code, which it can't do unless the window already +holds the image. The registers also don't exist on every Apple core, and an +undefined system register read at that point can't be recovered. The bootloader +reads ``CTXR_D`` when it chooses the window, so the check belongs there. ``enforce`` only decides whether a confinement violation found by the verifier (see below) is fatal. Whether the hardware confines a core does not depend on diff --git a/arch/arm64/include/asm/ctxr_execmem.h b/arch/arm64/include/asm/ctxr_execmem.h index ecbd02884f6c0..d15a82ad831ca 100644 --- a/arch/arm64/include/asm/ctxr_execmem.h +++ b/arch/arm64/include/asm/ctxr_execmem.h @@ -41,12 +41,16 @@ bool ctxr_exec_efi_runtime_ok(void); * arm64_memblock_init() until the end of init. */ bool ctxr_exec_window_reserved(void); + +/* True if cores confined to the window can't run this kernel. */ +bool ctxr_exec_secondaries_held(void); #else static inline void ctxr_exec_reserve(void) {} static inline void ctxr_exec_keep_reserved(void) {} static inline bool ctxr_exec_execmem_setup(void) { return false; } static inline bool ctxr_exec_efi_runtime_ok(void) { return true; } static inline bool ctxr_exec_window_reserved(void) { return false; } +static inline bool ctxr_exec_secondaries_held(void) { return false; } #endif #endif /* __ASM_CTXR_EXECMEM_H */ diff --git a/arch/arm64/kernel/smp.c b/arch/arm64/kernel/smp.c index 1a30dcbaedb94..9cd9bc7e40812 100644 --- a/arch/arm64/kernel/smp.c +++ b/arch/arm64/kernel/smp.c @@ -44,6 +44,7 @@ #include #include #include +#include #include #include #include @@ -799,6 +800,10 @@ void __init smp_prepare_cpus(unsigned int max_cpus) if (max_cpus == 0) return; + /* Leave CPUs confined to an unusable CTXR window in the bootloader. */ + if (ctxr_exec_secondaries_held()) + return; + /* * Initialise the present map (which describes the set of CPUs * actually populated at the present time) and release the diff --git a/arch/arm64/mm/ctxr_execmem.c b/arch/arm64/mm/ctxr_execmem.c index 947614c63c4c5..63c0d7ef5f59f 100644 --- a/arch/arm64/mm/ctxr_execmem.c +++ b/arch/arm64/mm/ctxr_execmem.c @@ -51,6 +51,7 @@ static phys_addr_t ctxr_base __initdata; static phys_addr_t ctxr_size __initdata; static bool ctxr_enforce __initdata = true; static bool ctxr_active __initdata; +static bool ctxr_hold_cpus __initdata; /* Read with the MMU off by cores entering init_kernel_el. */ u32 ctxr_stay_el2 __section(".mmuoff.data.read"); @@ -116,8 +117,9 @@ static int __init ctxr_exec_enforce_param(char *arg) } early_param("ctxr_exec_enforce", ctxr_exec_enforce_param); -static bool __init ctxr_parse_dt(phys_addr_t *base, phys_addr_t *size, - bool *enforce) +/* 1 for a window, 0 for none, -EINVAL for one that can't be read. */ +static int __init ctxr_parse_dt(phys_addr_t *base, phys_addr_t *size, + bool *enforce) { unsigned long chosen; const __be32 *prop; @@ -125,21 +127,26 @@ static bool __init ctxr_parse_dt(phys_addr_t *base, phys_addr_t *size, chosen = of_get_flat_dt_subnode_by_name(of_get_flat_dt_root(), "chosen"); if ((long)chosen < 0) - return false; + return 0; /* */ prop = of_get_flat_dt_prop(chosen, "aurora,ctxr-exec-window", &len); - if (!prop || len != 4 * sizeof(__be32)) - return false; + if (!prop) + return 0; + if (len != 4 * sizeof(__be32)) + return -EINVAL; *base = ((u64)be32_to_cpu(prop[0]) << 32) | be32_to_cpu(prop[1]); *size = ((u64)be32_to_cpu(prop[2]) << 32) | be32_to_cpu(prop[3]); + if (!*size || !PAGE_ALIGNED(*base) || !PAGE_ALIGNED(*size) || + *base + *size < *base) + return -EINVAL; prop = of_get_flat_dt_prop(chosen, "aurora,ctxr-exec-enforce", &len); if (prop && len == sizeof(__be32)) *enforce = !!be32_to_cpu(prop[0]); - return *size != 0; + return 1; } static void __init ctxr_snapshot_free(phys_addr_t base, phys_addr_t size) @@ -237,11 +244,26 @@ void __init ctxr_exec_keep_reserved(void) memblock_reserve(ctxr_base, ctxr_size); } +/* + * The secondaries are confined to a window this kernel can't use, so they + * can't run it. The boot CPU isn't confined and carries on alone. + */ +static void __init ctxr_hold_secondaries(const char *why) +{ + ctxr_hold_cpus = true; + pr_err("%s: keeping the secondary CPUs offline\n", why); +} + +bool __init ctxr_exec_secondaries_held(void) +{ + return ctxr_hold_cpus; +} + void __init ctxr_exec_reserve(void) { phys_addr_t base = 0, size = 0; bool enforce = true; - bool from_dt = ctxr_parse_dt(&base, &size, &enforce); + int dt = ctxr_parse_dt(&base, &size, &enforce); if (ctxr_cmd_enforce >= 0) enforce = ctxr_cmd_enforce; @@ -253,13 +275,14 @@ void __init ctxr_exec_reserve(void) pr_warn("ignoring ctxr_exec_window=\n"); } - if (!from_dt || ctxr_establish(base, size, enforce)) + if (!dt) return; - - /* The bootloader armed this window, so the kernel can't run outside it. */ - if (enforce) - panic("ctxr-execmem: cannot establish the executable window\n"); - pr_warn("confinement disabled\n"); + if (dt < 0) + ctxr_hold_secondaries("malformed aurora,ctxr-exec-window"); + else if (!is_kernel_in_hyp_mode()) + ctxr_hold_secondaries("window given but the kernel is not at EL2"); + else if (!ctxr_establish(base, size, enforce)) + ctxr_hold_secondaries("unusable window"); } static void __init ctxr_pool_add_gap(struct gen_pool *pool, phys_addr_t from, @@ -315,8 +338,7 @@ bool __init ctxr_exec_execmem_setup(void) fail_pool: gen_pool_destroy(pool); fail: - if (ctxr_enforce) - panic("ctxr-execmem: cannot build the executable pool\n"); + ctxr_hold_secondaries("no executable pool"); return false; } From 6ebbafe217522a38edbf61ad9794a9bf54cb2c4c Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Fri, 6 Nov 2026 11:42:37 -0500 Subject: [PATCH 27/47] arm64: errata: apply the Apple vGIC (broken SEIS) workaround to T8140 With the kernel at EL2 on the T8140 (A18 Pro, Tahiti cores), KVM comes up with the vGIC placeholder: the Tahiti MIDRs aren't in apple_broken_seis_cpus, so kvm_patch_ich_vtr_el2() leaves vgic_ich_vtr() at 0 and KVM uses one list register. The boot log shows only "GICv3 sysreg trapping enabled ([C], ...)", the same signature as the T6040 before its entry was added. With one LR, two pending private interrupts while the guest masks IRQs make KVM set ICH_HCR_EL2.UIE, and the underflow maintenance interrupt fires on every guest entry. ICH_VTR_EL2 reads 0x90580007 on the T8140 (8 LRs, SEIS and TDS set), the same value as on the T6040. Add the Tahiti E (0x060) and P (0x061) cores to the broken-SEIS match. The match only applies with the kernel at EL2 and SEIS set, and the IMPDEF PMUv3 trap tied to apple_cpus stays off, since HACR_EL2 bit 56 hasn't been checked on these cores. The same entry, in a kernel run at virtual EL2 under aura's FEAT_NV2 monitor on the Neo, let a 4-vCPU guest boot and run stress-ng; without it SMP guests livelocked. It hasn't run with the kernel at real EL2. Signed-off-by: Diljit Singh --- arch/arm64/kernel/cpu_errata.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/arch/arm64/kernel/cpu_errata.c b/arch/arm64/kernel/cpu_errata.c index 6a12d12effacb..f8116630c743b 100644 --- a/arch/arm64/kernel/cpu_errata.c +++ b/arch/arm64/kernel/cpu_errata.c @@ -314,6 +314,8 @@ static bool has_impdef_pmuv3(const struct arm64_cpu_capabilities *entry, int sco static const struct midr_range apple_broken_seis_cpus[] = { MIDR_ALL_VERSIONS(MIDR_APPLE_T6040_E), MIDR_ALL_VERSIONS(MIDR_APPLE_T6040_P), + MIDR_ALL_VERSIONS(MIDR_APPLE_TAHITI_E), + MIDR_ALL_VERSIONS(MIDR_APPLE_TAHITI_P), {}, }; From ddb50b42408ee9ef0a1b957991fb5321b99ce686 Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Fri, 6 Nov 2026 11:43:14 -0500 Subject: [PATCH 28/47] cpuidle: apple: offer CPU PD only on M1 to M3 CPU PD sets bits in CYC_OVRD (s3_5_c15_c5_0). From the M4 generation on iBoot locks that register and writing it is an undefined instruction. The driver kept those SoCs off CPU PD with a deny list: an "apple,t6041-cpuidle" or "apple,t8132-cpuidle" node, or a machine compatible with "apple,t8140". Any other SoC that reached the probe got CPU PD, so a later SoC whose device tree reused one of the node compatibles, T6040 with its own node name or T8142 for instance, would fault on its first deep idle. Turn the test around: offer CPU PD only on the machines known to let the OS write CYC_OVRD, M1 to M3 (T8103, T600x, T8112, T602x, T8122, T603x), and run both entries through WFI everywhere else. These are the SoCs aura also programs CYC_OVRD on, through its apple_sysregs_unlocked feature; its M4-and-later feature set leaves the register alone. Every device tree in the tree gets the same states as before: M1 to M3 keep CPU PD, and T8132, T6040, T6041 and T8140 keep WFI for both. This is the only CYC_OVRD user in the kernel. Signed-off-by: Diljit Singh --- drivers/cpuidle/cpuidle-apple.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/drivers/cpuidle/cpuidle-apple.c b/drivers/cpuidle/cpuidle-apple.c index 896b92440ec79..e3751f23aef6d 100644 --- a/drivers/cpuidle/cpuidle-apple.c +++ b/drivers/cpuidle/cpuidle-apple.c @@ -122,21 +122,25 @@ static struct cpuidle_driver apple_idle_driver = { .state_count = STATE_COUNT, }; +/* SoCs whose CYC_OVRD the OS can write: M1 to M3. */ +static const char *const apple_cyc_ovrd_machines[] = { + "apple,t8103", "apple,t6000", "apple,t6001", "apple,t6002", + "apple,t8112", "apple,t6020", "apple,t6021", "apple,t6022", + "apple,t8122", "apple,t6030", "apple,t6031", "apple,t6032", + "apple,t6034", + NULL +}; + static int apple_cpuidle_probe(struct platform_device *pdev) { - struct device_node *np = pdev->dev.of_node; - bool wfi_only = of_device_is_compatible(np, "apple,t6041-cpuidle") || - of_device_is_compatible(np, "apple,t8132-cpuidle"); - /* - * CPU PD writes CYC_OVRD, which is an undefined instruction on - * T8140 and the M4 SoCs (T6041, T8132). Their WFI already lets - * the hardware power-gate idle cores, so both entries must use - * WFI. Keep index 1: the generic s2idle selector skips fallback - * index 0, otherwise tick_freeze() and timekeeping suspension are - * never reached. + * CPU PD writes CYC_OVRD, which only M1 to M3 let the OS write. From + * M4 on it is locked and the write is undefined, and WFI already + * power-gates an idle core, so both entries use WFI. Keep index 1: + * the generic s2idle selector skips fallback index 0, otherwise + * tick_freeze() and timekeeping suspension are never reached. */ - if (of_machine_is_compatible("apple,t8140") || wfi_only) + if (!of_machine_compatible_match(apple_cyc_ovrd_machines)) apple_idle_driver.states[STATE_PWRDOWN] = apple_idle_driver.states[STATE_WFI]; return cpuidle_register(&apple_idle_driver, NULL); From 52252329df0ba127f965a536d4ad7cd882d85fbf Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Fri, 6 Nov 2026 12:37:07 -0500 Subject: [PATCH 29/47] arm64: ctxr-execmem: check the window for pool space before SMP On arm64 execmem_arch_setup() runs at core_initcall (ARCH_WANTS_EXECMEM_LATE), after smp_init(), so keeping the secondaries offline when the pool couldn't be built came too late. Under QEMU, with a device-tree window exactly the size of the image, all three CPUs were up before "no executable pool" was logged. Count the whole free pages in the window when its free ranges are recorded, before it is reserved, and treat a window with none as unusable, like one the image doesn't fit. That keeps the secondaries in the bootloader in time. What can still fail later is allocating the pool itself, by which point confined CPUs are running and executable memory from outside the window would fault on them. That failure now panics whether or not the window is enforcing. It needs a failed allocation of a few hundred bytes at core_initcall, not a bad window. Signed-off-by: Diljit Singh --- arch/arm64/mm/ctxr_execmem.c | 40 ++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/arch/arm64/mm/ctxr_execmem.c b/arch/arm64/mm/ctxr_execmem.c index 63c0d7ef5f59f..021ca3c15651f 100644 --- a/arch/arm64/mm/ctxr_execmem.c +++ b/arch/arm64/mm/ctxr_execmem.c @@ -149,9 +149,10 @@ static int __init ctxr_parse_dt(phys_addr_t *base, phys_addr_t *size, return 1; } -static void __init ctxr_snapshot_free(phys_addr_t base, phys_addr_t size) +/* Returns how much the pool would get, in whole pages. */ +static phys_addr_t __init ctxr_snapshot_free(phys_addr_t base, phys_addr_t size) { - phys_addr_t start, end, wend = base + size; + phys_addr_t start, end, wend = base + size, usable = 0; u64 i; ctxr_nr_free = 0; @@ -171,7 +172,11 @@ static void __init ctxr_snapshot_free(phys_addr_t base, phys_addr_t size) ctxr_free[ctxr_nr_free].start = start; ctxr_free[ctxr_nr_free].end = end; ctxr_nr_free++; + if (round_down(end, PAGE_SIZE) > PAGE_ALIGN(start)) + usable += round_down(end, PAGE_SIZE) - PAGE_ALIGN(start); } + + return usable; } /* Reserve a window. False, with the reason logged, if it can't work. */ @@ -206,8 +211,14 @@ static bool __init ctxr_establish(phys_addr_t base, phys_addr_t size, bool enfor return false; } - /* Must run before the window is reserved, or every range looks taken. */ - ctxr_snapshot_free(base, size); + /* + * Must run before the window is reserved, or every range looks taken. + * The pool is built after the secondaries start, so check it now. + */ + if (!ctxr_snapshot_free(base, size)) { + pr_err("window has no free space for executable allocations\n"); + return false; + } if (memblock_reserve(base, size)) { pr_err("failed to reserve window [%pa, +%llu MiB)\n", @@ -307,10 +318,8 @@ bool __init ctxr_exec_execmem_setup(void) return false; pool = gen_pool_create(PAGE_SHIFT, NUMA_NO_NODE); - if (!pool) { - pr_err("failed to create the window pool\n"); + if (!pool) goto fail; - } /* * Only memory that was free before the window was reserved joins the @@ -319,15 +328,8 @@ bool __init ctxr_exec_execmem_setup(void) for (i = 0; i < ctxr_nr_free; i++) ctxr_pool_add_gap(pool, ctxr_free[i].start, ctxr_free[i].end); - if (!gen_pool_size(pool)) { - pr_err("window has no free space for executable allocations\n"); - goto fail_pool; - } - - if (execmem_confine_init(pool, ctxr_enforce)) { - pr_err("failed to register the window\n"); - goto fail_pool; - } + if (!gen_pool_size(pool) || execmem_confine_init(pool, ctxr_enforce)) + goto fail; pr_info("executable pool: %zu MiB usable in %d range(s) of a %llu MiB window (%llu MiB already reserved)\n", gen_pool_size(pool) >> 20, ctxr_nr_free, @@ -335,11 +337,9 @@ bool __init ctxr_exec_execmem_setup(void) (unsigned long long)((ctxr_size - gen_pool_size(pool)) >> 20)); return true; -fail_pool: - gen_pool_destroy(pool); fail: - ctxr_hold_secondaries("no executable pool"); - return false; + /* Only an allocation failure gets here, and confined CPUs are running. */ + panic("ctxr-execmem: cannot build the executable pool\n"); } bool __init ctxr_exec_window_reserved(void) From a3043575d91adb182fd57ff00c8611fff05a7faa Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Fri, 6 Nov 2026 13:31:02 -0500 Subject: [PATCH 30/47] selftests: apple-dcp: replay the cpuidle probe against the M1 to M3 allowlist "cpuidle: apple: offer CPU PD only on M1 to M3" made apple_cpuidle_probe() call of_machine_compatible_match() instead of of_machine_is_compatible() and of_device_is_compatible(), so the cpuidle-s2idle replay no longer built: the new call had no mock, and the old mocks were unused under -Werror. Mock of_machine_compatible_match() against one machine compatible and name the machine in each case. The T8140 cases are unchanged, the case that expects CPU PD now uses T8103, and the WFI-only case covers T6041 and T8142, which the old deny list only caught through a cpuidle node or not at all. Signed-off-by: Diljit Singh --- .../selftests/drivers/gpu/apple-dcp/idle.py | 52 ++++++++++--------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/idle.py b/tools/testing/selftests/drivers/gpu/apple-dcp/idle.py index 4f13a4f2105cc..c84e0ff9c53d6 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/idle.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/idle.py @@ -79,10 +79,8 @@ def function(text, name): static ktime_t ktime_us_delta(ktime_t a,ktime_t b){return (a-b)/1000;} static u64 div_u64(u64 a,u64 b){return a/b;} static u64 mul_u32_u32(unsigned a,unsigned b){return (u64)a*b;} -static bool t8140; -static bool of_machine_is_compatible(const char *s){return t8140&&!strcmp(s,"apple,t8140");} -static bool t6041; -static bool of_device_is_compatible(const struct device_node *np,const char *s){(void)np;return t6041&&!strcmp(s,"apple,t6041-cpuidle");} +static const char *machine; +static bool of_machine_compatible_match(const char *const *c){for(;machine&&*c;c++)if(!strcmp(*c,machine))return true;return false;} static int cpu_pm_enter(void){pm++;return pm_fail;} static void cpu_pm_exit(void){pm--;} static void apple_cpu_deep_wfi(void){deep++;} @@ -100,8 +98,8 @@ def function(text, name): if(faulty_irq)irq[current]=0; } static struct cpuidle_driver original; -static void reset(bool is_t8140){ - apple_idle_driver=original;t8140=is_t8140;t6041=false; current=0; +static void reset(const char *m){ + apple_idle_driver=original;machine=m; current=0; memset(cpuidle_dev,0,sizeof(cpuidle_dev)); memset(registered,0,sizeof(registered)); for(int i=0;i<6;i++){irq[i]=1;rcu[i]=stopped[i]=0;} wfi=deep=pm=pm_fail=warnings=unregistered=0;reg_fail=-1;drv_fail=0; @@ -110,7 +108,7 @@ def function(text, name): } int main(void){ original=apple_idle_driver; - reset(true);check(!apple_cpuidle_probe(&pdev)); + reset("apple,t8140");check(!apple_cpuidle_probe(&pdev)); check(apple_idle_driver.cpumask==cpu_possible_mask);check(!apple_idle_driver.bctimer); for(int i=0;i<6;i++)check(registered[i]&&cpuidle_dev[i].cpu==i); int expected=EXPECT_FIXED?1:0; @@ -126,34 +124,35 @@ def function(text, name): check(apple_idle_driver.states[1].exit_latency_ns==1000); check(apple_idle_driver.states[1].target_residency_ns==1000); check(apple_idle_driver.states[1].power_usage==apple_idle_driver.states[0].power_usage); - reset(true);apple_cpuidle_probe(&pdev);fanout=1; + reset("apple,t8140");apple_cpuidle_probe(&pdev);fanout=1; check(cpuidle_enter_s2idle(&apple_idle_driver,&cpuidle_dev[0],100000)==1); check(wfi==6&&deep==0&&pm==0);check(tk_suspend==1&&tk_resume==1); check(sched_suspend==1&&sched_resume==1&&local_suspend==5&&local_resume==5); check(tick_freeze_depth==0&&!frozen); for(int i=0;i<6;i++)check(cpuidle_dev[i].states_usage[1].s2idle_usage==1&&rcu[i]==0&&stopped[i]==0); - reset(true);apple_cpuidle_probe(&pdev);fanout=1;cpuidle_dev[5].states_usage[1].disable=1; + reset("apple,t8140");apple_cpuidle_probe(&pdev);fanout=1;cpuidle_dev[5].states_usage[1].disable=1; cpuidle_enter_s2idle(&apple_idle_driver,&cpuidle_dev[0],100000); check(wfi==5&&tk_suspend==0&&tk_resume==0&&tick_freeze_depth==0); - reset(true);apple_cpuidle_probe(&pdev);cpuidle_dev[0].states_usage[1].disable=1; + reset("apple,t8140");apple_cpuidle_probe(&pdev);cpuidle_dev[0].states_usage[1].disable=1; check(cpuidle_enter_s2idle(&apple_idle_driver,&cpuidle_dev[0],100000)==0&&wfi==0); cpuidle_dev[0].states_usage[1].disable=0;apple_idle_driver.states[1].enter_s2idle=NULL; check(cpuidle_enter_s2idle(&apple_idle_driver,&cpuidle_dev[0],100000)==0&&wfi==0); - reset(true);apple_cpuidle_probe(&pdev); + reset("apple,t8140");apple_cpuidle_probe(&pdev); check(cpuidle_enter_s2idle(&apple_idle_driver,&cpuidle_dev[0],999)==0); check(cpuidle_enter_s2idle(&apple_idle_driver,&cpuidle_dev[0],1000)==1); - reset(true);apple_cpuidle_probe(&pdev);apple_idle_driver.states[1].exit_latency_ns=0; + reset("apple,t8140");apple_cpuidle_probe(&pdev);apple_idle_driver.states[1].exit_latency_ns=0; check(find_deepest_state(&apple_idle_driver,&cpuidle_dev[0],100000,0,true)==0); - reset(true);apple_cpuidle_probe(&pdev);apple_idle_driver.states[1].flags=CPUIDLE_FLAG_TIMER_STOP; + reset("apple,t8140");apple_cpuidle_probe(&pdev);apple_idle_driver.states[1].flags=CPUIDLE_FLAG_TIMER_STOP; check(find_deepest_state(&apple_idle_driver,&cpuidle_dev[0],100000,CPUIDLE_FLAG_TIMER_STOP,false)==0); - reset(true);apple_cpuidle_probe(&pdev);faulty_irq=1; + reset("apple,t8140");apple_cpuidle_probe(&pdev);faulty_irq=1; check(cpuidle_enter_s2idle(&apple_idle_driver,&cpuidle_dev[0],100000)==1); check(warnings==1&&tick_freeze_depth==0&&rcu[0]==0&&stopped[0]==0); } - reset(true);apple_cpuidle_probe(&pdev); + reset("apple,t8140");apple_cpuidle_probe(&pdev); for(int i=0;i Date: Fri, 6 Nov 2026 13:38:13 -0500 Subject: [PATCH 31/47] Documentation: arm64: ctxr-execmem: drop a wrong reason for no CTXR_D check The boot CPU's CTXR_D bounds do describe the window: on the Neo, aura reads them on the boot CPU and gets the range a secondary enforces, ending at the boot object's load address plus kcrz. Only enforcement is off there. Keep the real reasons: the bootloader already checks, and the registers aren't on every Apple core. Signed-off-by: Diljit Singh --- Documentation/arch/arm64/ctxr-execmem.rst | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Documentation/arch/arm64/ctxr-execmem.rst b/Documentation/arch/arm64/ctxr-execmem.rst index 0cea8f9b996ad..de8ca3bfad3ea 100644 --- a/Documentation/arch/arm64/ctxr-execmem.rst +++ b/Documentation/arch/arm64/ctxr-execmem.rst @@ -56,12 +56,11 @@ when no free memory is left in it for executable allocations, and when the kerne isn't running at EL2 (``kvm-arm.mode=nvhe`` or ``protected``, for example). With no such property nothing changes. -The kernel doesn't check the device tree's window against ``CTXR_D``. The boot -CPU's copy doesn't describe the window, and a secondary can only read its own -once it is running kernel code, which it can't do unless the window already -holds the image. The registers also don't exist on every Apple core, and an -undefined system register read at that point can't be recovered. The bootloader -reads ``CTXR_D`` when it chooses the window, so the check belongs there. +The kernel doesn't check the device tree's window against ``CTXR_D``. The +bootloader reads ``CTXR_D`` when it chooses the window and reserves the rest of +the armed range, so the check belongs there. The registers also don't exist on +every Apple core, and an undefined system register read this early can't be +recovered. ``enforce`` only decides whether a confinement violation found by the verifier (see below) is fatal. Whether the hardware confines a core does not depend on From 65da3475d81e225c0f5f0a97e30e1e11c4a849b1 Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Fri, 6 Nov 2026 14:11:17 -0500 Subject: [PATCH 32/47] arm64: head: drop the VH check from the CTXR direct entry a57207316f8 made the direct VHE entry check ID_AA64MMFR1_EL1.VH, so that an Apple-MIDR guest whose virtual EL2 lacks VHE would not take it. The entry is now gated on ctxr_stay_el2, which only a boot CPU already running VHE at EL2 sets. In such a guest the boot CPU stays at EL1 and never sets it, so the check can no longer fail. Signed-off-by: Diljit Singh --- arch/arm64/kernel/head.S | 4 ---- 1 file changed, 4 deletions(-) diff --git a/arch/arm64/kernel/head.S b/arch/arm64/kernel/head.S index 21b0fc15c2742..dbb37dbf2522d 100644 --- a/arch/arm64/kernel/head.S +++ b/arch/arm64/kernel/head.S @@ -316,10 +316,6 @@ SYM_INNER_LABEL(init_el2, SYM_L_LOCAL) adr_l x0, ctxr_stay_el2 ldr w0, [x0] cbz w0, .Lctxr_no_direct_vhe - /* A virtual EL2 without VHE, as nested KVM may give a guest. */ - mrs x0, id_aa64mmfr1_el1 - ubfx x0, x0, #ID_AA64MMFR1_EL1_VH_SHIFT, #4 - cbz x0, .Lctxr_no_direct_vhe finalise_el2_state From c89843af86d888ebd03c2408d0340ddfec88cebb Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Fri, 6 Nov 2026 14:12:49 -0500 Subject: [PATCH 33/47] arm64: ctxr-execmem: drop aurora,ctxr-exec-enforce and windows without a base aura emits neither a base of 0 nor aurora,ctxr-exec-enforce. Drop both: - A window now always has an explicit base. A device-tree window with a base of 0 is malformed, like one with a size of 0, and keeps the secondaries offline. ctxr_exec_window= takes @, both required and checked the same way, so the default size and the code that anchored the window on the image and slid it below the end of DRAM go. - The device tree no longer says whether the window is enforcing. ctxr_exec_enforce= still does, for a window from either place, and only decides whether a boot-time verifier finding is fatal. The binding, the doc and kernel-parameters.txt follow. Signed-off-by: Diljit Singh --- .../admin-guide/kernel-parameters.txt | 11 +-- Documentation/arch/arm64/ctxr-execmem.rst | 28 +++--- .../chosen/aurora,ctxr-exec-window.yaml | 18 +--- arch/arm64/mm/ctxr_execmem.c | 93 ++++++------------- 4 files changed, 52 insertions(+), 98 deletions(-) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt index 9cd5da988c7bd..c9525511974bb 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -1139,10 +1139,10 @@ Kernel parameters Kconfig option. ctxr_exec_enforce= - [ARM64,EARLY] Whether code outside the CTXR executable - window is fatal. Applies to a window from the device - tree or from ctxr_exec_window=, and overrides - aurora,ctxr-exec-enforce in the device tree. + [ARM64,EARLY] Whether executable code that the boot-time + check finds outside the CTXR executable window is + fatal. Applies to a window from the device tree or + from ctxr_exec_window=. Format: Default: 1 @@ -1156,8 +1156,7 @@ Kernel parameters for bring-up and VMs. A value that doesn't parse, or a window that can't work, is ignored with a warning. See Documentation/arch/arm64/ctxr-execmem.rst. - Format: [[KMG][@[KMG]]] - Default size: 128M. Default base: from the kernel image. + Format: [KMG]@[KMG] dasd= [HW,NET] See header of drivers/s390/block/dasd_devmap.c. diff --git a/Documentation/arch/arm64/ctxr-execmem.rst b/Documentation/arch/arm64/ctxr-execmem.rst index de8ca3bfad3ea..04fc745faebda 100644 --- a/Documentation/arch/arm64/ctxr-execmem.rst +++ b/Documentation/arch/arm64/ctxr-execmem.rst @@ -25,22 +25,18 @@ Device tree (production) aurora,ctxr-exec-window = ; - A ``base`` of 0 tells the kernel to anchor the window on its own loaded - image (rounded down to 2 MiB). ``size`` is the window size in bytes. The - window is enforcing unless ``aurora,ctxr-exec-enforce = <0>`` is also - there, which aura doesn't emit. See + ``base`` and ``size`` are in bytes, page aligned and not 0. See ``Documentation/devicetree/bindings/chosen/aurora,ctxr-exec-window.yaml``. Command line (bring-up / VM) :: - ctxr_exec_window=[[@]] e.g. ctxr_exec_window=128M - ctxr_exec_enforce= (default: enforcing) + ctxr_exec_window=@ e.g. ctxr_exec_window=128M@0x40200000 + ctxr_exec_enforce= (default: enforcing) - The command line overrides the device tree, and ``ctxr_exec_enforce=`` - applies to a window from either place. ``size`` accepts the usual - ``K``/``M``/``G`` suffixes and defaults to 128 MiB; if ``@base`` is omitted - it is derived from the kernel image. The last use of an option wins. + The command line overrides the device tree's window. ``size`` and ``base`` + accept the usual ``K``/``M``/``G`` suffixes, and both are required. The last + use of an option wins. A value that doesn't parse is ignored with a warning. So is a window from the command line that can't work because the image doesn't fit it or it isn't in @@ -49,8 +45,8 @@ DRAM, and the device tree's window then applies if there is one. The secondary CPUs can only run code inside the device tree's window, and the boot CPU isn't confined. So when that window can't be used, the kernel logs why, leaves the secondaries in the bootloader and boots on the boot CPU alone, as with -``nosmp``. That happens when the property is malformed (not four cells, a size of -0, a base or size that isn't page aligned, or one that wraps), when the image +``nosmp``. That happens when the property is malformed (not four cells, a base or +size of 0 or not page aligned, or a window that wraps), when the image doesn't fit the window or the window isn't in DRAM, when it can't be reserved, when no free memory is left in it for executable allocations, and when the kernel isn't running at EL2 (``kvm-arm.mode=nvhe`` or ``protected``, for example). With @@ -62,10 +58,10 @@ the armed range, so the check belongs there. The registers also don't exist on every Apple core, and an undefined system register read this early can't be recovered. -``enforce`` only decides whether a confinement violation found by the verifier -(see below) is fatal. Whether the hardware confines a core does not depend on -it, so everything else in this document applies whenever a window is active, -enforcing or not. +``ctxr_exec_enforce=`` only decides whether a violation found by the boot-time +verifier (see below) is fatal, for a window from either place. Whether the +hardware confines a core does not depend on it, so everything else in this +document applies whenever a window is active, enforcing or not. How it works ============ diff --git a/Documentation/devicetree/bindings/chosen/aurora,ctxr-exec-window.yaml b/Documentation/devicetree/bindings/chosen/aurora,ctxr-exec-window.yaml index bbffbf958ec9c..c40ff69fa37aa 100644 --- a/Documentation/devicetree/bindings/chosen/aurora,ctxr-exec-window.yaml +++ b/Documentation/devicetree/bindings/chosen/aurora,ctxr-exec-window.yaml @@ -32,19 +32,11 @@ properties: description: The physical window the kernel's executable code has to stay in. items: - description: - Physical base of the window. Zero tells the kernel to anchor the - window on its own image. - - description: Size of the window in bytes. - - aurora,ctxr-exec-enforce: - $ref: /schemas/types.yaml#/definitions/uint32 - enum: [0, 1] - description: - One if code outside the window is fatal, zero if the kernel only reports - it. A window with no such property is enforcing. - -dependentRequired: - aurora,ctxr-exec-enforce: [ aurora,ctxr-exec-window ] + Physical base of the window, not zero and aligned to the kernel's + page size. + - description: + Size of the window in bytes, not zero and aligned to the kernel's + page size. additionalProperties: true ... diff --git a/arch/arm64/mm/ctxr_execmem.c b/arch/arm64/mm/ctxr_execmem.c index 021ca3c15651f..9f2a9227d3dde 100644 --- a/arch/arm64/mm/ctxr_execmem.c +++ b/arch/arm64/mm/ctxr_execmem.c @@ -42,11 +42,6 @@ #include #include -/* Window granularity when the base is derived rather than given. */ -#define CTXR_WINDOW_ALIGN SZ_2M -/* Default size if only "ctxr_exec_window" with no value is given. */ -#define CTXR_WINDOW_DEFAULT SZ_128M - static phys_addr_t ctxr_base __initdata; static phys_addr_t ctxr_size __initdata; static bool ctxr_enforce __initdata = true; @@ -73,53 +68,50 @@ static int ctxr_nr_free __initdata; /* From the command line. A size of 0 means none was given. */ static phys_addr_t ctxr_cmd_base __initdata; static phys_addr_t ctxr_cmd_size __initdata; -static int ctxr_cmd_enforce __initdata = -1; -/* ctxr_exec_window=[[@]]: with no size, the default one. */ +/* A page-aligned base and size, neither of them 0, that don't wrap. */ +static bool __init ctxr_window_valid(phys_addr_t base, phys_addr_t size) +{ + return base && size && PAGE_ALIGNED(base) && PAGE_ALIGNED(size) && + base + size > base; +} + +/* ctxr_exec_window=@ */ static int __init ctxr_exec_window_param(char *arg) { - phys_addr_t base = 0, size = CTXR_WINDOW_DEFAULT; - char *p = arg, *end; - - if (p && *p) { - size = memparse(p, &end); - if (end == p || !size) - goto bad; - if (*end == '@') { - p = end + 1; - base = memparse(p, &end); - if (end == p) - goto bad; - } - if (*end) - goto bad; - } + phys_addr_t base, size; + char *p, *end; + + if (!arg) + goto bad; + size = memparse(arg, &end); + if (end == arg || *end != '@') + goto bad; + p = end + 1; + base = memparse(p, &end); + if (end == p || *end || !ctxr_window_valid(base, size)) + goto bad; ctxr_cmd_base = base; ctxr_cmd_size = size; return 0; bad: - pr_warn("ignoring ctxr_exec_window=%s\n", arg); + pr_warn("ignoring ctxr_exec_window=%s\n", arg ?: ""); return 0; } early_param("ctxr_exec_window", ctxr_exec_window_param); static int __init ctxr_exec_enforce_param(char *arg) { - bool enforce; - - if (kstrtobool(arg, &enforce)) + if (kstrtobool(arg, &ctxr_enforce)) pr_warn("ignoring ctxr_exec_enforce=%s\n", arg ?: ""); - else - ctxr_cmd_enforce = enforce; return 0; } early_param("ctxr_exec_enforce", ctxr_exec_enforce_param); -/* 1 for a window, 0 for none, -EINVAL for one that can't be read. */ -static int __init ctxr_parse_dt(phys_addr_t *base, phys_addr_t *size, - bool *enforce) +/* 1 for a window, 0 for none, -EINVAL for one that can't be used as given. */ +static int __init ctxr_parse_dt(phys_addr_t *base, phys_addr_t *size) { unsigned long chosen; const __be32 *prop; @@ -138,15 +130,7 @@ static int __init ctxr_parse_dt(phys_addr_t *base, phys_addr_t *size, *base = ((u64)be32_to_cpu(prop[0]) << 32) | be32_to_cpu(prop[1]); *size = ((u64)be32_to_cpu(prop[2]) << 32) | be32_to_cpu(prop[3]); - if (!*size || !PAGE_ALIGNED(*base) || !PAGE_ALIGNED(*size) || - *base + *size < *base) - return -EINVAL; - - prop = of_get_flat_dt_prop(chosen, "aurora,ctxr-exec-enforce", &len); - if (prop && len == sizeof(__be32)) - *enforce = !!be32_to_cpu(prop[0]); - - return 1; + return ctxr_window_valid(*base, *size) ? 1 : -EINVAL; } /* Returns how much the pool would get, in whole pages. */ @@ -180,24 +164,12 @@ static phys_addr_t __init ctxr_snapshot_free(phys_addr_t base, phys_addr_t size) } /* Reserve a window. False, with the reason logged, if it can't work. */ -static bool __init ctxr_establish(phys_addr_t base, phys_addr_t size, bool enforce) +static bool __init ctxr_establish(phys_addr_t base, phys_addr_t size) { phys_addr_t img_start = __pa_symbol(_text), img_end = __pa_symbol(_end); phys_addr_t dram_start = memblock_start_of_DRAM(); phys_addr_t dram_end = memblock_end_of_DRAM(); - if (!base) { - /* - * No base given: anchor the window on the image. If that would run - * past the end of DRAM (the image is within @size of the top), slide - * it down so it ends at DRAM's end while still covering the image. - */ - base = round_down(img_start, CTXR_WINDOW_ALIGN); - if (base + size > dram_end && dram_end - dram_start >= size) - base = max(round_down(dram_end - size, CTXR_WINDOW_ALIGN), - round_up(dram_start, CTXR_WINDOW_ALIGN)); - } - if (img_start < base || img_end > base + size) { pr_err("kernel image [%pa, %pa) does not fit window [%pa, +%llu MiB)\n", &img_start, &img_end, &base, @@ -236,12 +208,11 @@ static bool __init ctxr_establish(phys_addr_t base, phys_addr_t size, bool enfor ctxr_base = base; ctxr_size = size; - ctxr_enforce = enforce; ctxr_active = true; pr_info("reserved exec window [%pa, +%llu MiB), %s\n", &base, (unsigned long long)(size >> 20), - enforce ? "enforcing" : "reporting"); + ctxr_enforce ? "enforcing" : "reporting"); return true; } @@ -273,15 +244,11 @@ bool __init ctxr_exec_secondaries_held(void) void __init ctxr_exec_reserve(void) { phys_addr_t base = 0, size = 0; - bool enforce = true; - int dt = ctxr_parse_dt(&base, &size, &enforce); - - if (ctxr_cmd_enforce >= 0) - enforce = ctxr_cmd_enforce; + int dt = ctxr_parse_dt(&base, &size); /* A bad override leaves the machine as its bootloader described it. */ if (ctxr_cmd_size) { - if (ctxr_establish(ctxr_cmd_base, ctxr_cmd_size, enforce)) + if (ctxr_establish(ctxr_cmd_base, ctxr_cmd_size)) return; pr_warn("ignoring ctxr_exec_window=\n"); } @@ -292,7 +259,7 @@ void __init ctxr_exec_reserve(void) ctxr_hold_secondaries("malformed aurora,ctxr-exec-window"); else if (!is_kernel_in_hyp_mode()) ctxr_hold_secondaries("window given but the kernel is not at EL2"); - else if (!ctxr_establish(base, size, enforce)) + else if (!ctxr_establish(base, size)) ctxr_hold_secondaries("unusable window"); } From d1eba7c3ee25b25cebceb8f6ff72d4b0a4793552 Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Fri, 6 Nov 2026 14:13:21 -0500 Subject: [PATCH 34/47] mm/execmem: drop the uncalled confinement accessors execmem_confine_window() never had a caller, and execmem_confine_enforced() lost its last ones when EFI runtime services, kexec and the EL0 fetch check moved to execmem_confine_active(). Remove both, and the two parameters execmem_confine_init()'s kernel-doc still described but the function doesn't take. Signed-off-by: Diljit Singh --- include/linux/execmem.h | 9 --------- mm/execmem_confine.c | 13 ------------- 2 files changed, 22 deletions(-) diff --git a/include/linux/execmem.h b/include/linux/execmem.h index 204d1be409ca1..d4004103c44ea 100644 --- a/include/linux/execmem.h +++ b/include/linux/execmem.h @@ -216,8 +216,6 @@ struct execmem_range; /** * execmem_confine_init - register the confined executable physical window - * @base: physical base of the window - * @size: size of the window in bytes * @pool: gen_pool that hands out page-aligned chunks of the window * (entries added with linear-map virtual addresses) * @enforce: if true, a confinement violation found by the verifier is @@ -233,12 +231,6 @@ int execmem_confine_init(struct gen_pool *pool, bool enforce); /* True once a window has been registered. */ bool execmem_confine_active(void); -/* True if confinement violations are fatal. */ -bool execmem_confine_enforced(void); - -/* Window bounds, valid only when execmem_confine_active(). */ -void execmem_confine_window(phys_addr_t *base, phys_addr_t *size); - /* * Record the window once it is reserved. This comes ahead of the pool, so that * nothing can release window pages between the two. @@ -282,7 +274,6 @@ unsigned long execmem_confine_arch_verify(phys_addr_t base, phys_addr_t size, unsigned long *nr_exec); #else static inline bool execmem_confine_active(void) { return false; } -static inline bool execmem_confine_enforced(void) { return false; } static inline bool execmem_confine_pfn_in_window(unsigned long pfn) { return false; } static inline bool execmem_confine_overlaps(phys_addr_t start, phys_addr_t size) { return false; } #endif /* CONFIG_EXECMEM_CONFINE */ diff --git a/mm/execmem_confine.c b/mm/execmem_confine.c index b7b817fff1abc..d548b96f7cf0f 100644 --- a/mm/execmem_confine.c +++ b/mm/execmem_confine.c @@ -92,19 +92,6 @@ bool execmem_confine_active(void) return confine_ready; } -bool execmem_confine_enforced(void) -{ - return confine_ready && confine_enforce; -} - -void execmem_confine_window(phys_addr_t *base, phys_addr_t *size) -{ - if (base) - *base = confine_base; - if (size) - *size = confine_size; -} - static bool phys_in_window(phys_addr_t pa) { return pa >= confine_base && pa < confine_base + confine_size; From 9657b6e15db9ce97b950732f0c42ad7a7b9775a9 Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Fri, 6 Nov 2026 14:14:17 -0500 Subject: [PATCH 35/47] mm/execmem: drop the confinement selftests and the debugfs sweep ctxr_exec_selftest checked, once per boot, that a confined allocation lands in the window and that the page-table sweep notices an executable mapping placed outside it. /sys/kernel/debug/execmem_confine reran the sweep and printed the pool. Both were test aids for bring-up, and the selftest mapped a page RWX to do its second check. Remove them, and make execmem_confine_verify() static, since the boot-time check is now its only caller. The boot-time verifier stays, and so does the EL0 fetch kill, neither of which depends on what goes here. ctxr_exec_enforce= now only decides whether a verifier finding at late_initcall panics. Signed-off-by: Diljit Singh --- .../admin-guide/kernel-parameters.txt | 4 - Documentation/arch/arm64/ctxr-execmem.rst | 23 +-- include/linux/execmem.h | 10 -- mm/execmem_confine.c | 141 +----------------- 4 files changed, 10 insertions(+), 168 deletions(-) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt index c9525511974bb..ee681cfca630b 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -1146,10 +1146,6 @@ Kernel parameters Format: Default: 1 - ctxr_exec_selftest - [ARM64,EARLY] Run two extra checks of the CTXR - executable window at boot. - ctxr_exec_window= [ARM64,EARLY] Confine kernel executable code to a physical window, overriding the device tree. This is diff --git a/Documentation/arch/arm64/ctxr-execmem.rst b/Documentation/arch/arm64/ctxr-execmem.rst index 04fc745faebda..a99638b7e06b2 100644 --- a/Documentation/arch/arm64/ctxr-execmem.rst +++ b/Documentation/arch/arm64/ctxr-execmem.rst @@ -141,18 +141,11 @@ Hibernate Verifying confinement ===================== -The feature carries its own verifier, which is what stands in for the hardware -fault when testing in a VM: - -* At ``late_initcall`` the kernel sweeps its page tables, and ``efi_mm`` when - runtime services are on, and reports the number of EL1-executable leaf mappings - and how many resolve outside the window. When the window is enforcing, a - non-zero count is fatal. It also counts window pages that reached the page - allocator as violations. -* ``/sys/kernel/debug/execmem_confine`` re-runs the sweep on read and reports - window bounds, pool usage and ``PASS``/``FAIL``. Init memory is freed after - ``late_initcall``, and an initrd can be, so this is the check that covers them. -* Booting with ``ctxr_exec_selftest`` runs two extra checks: that a confined - allocation is physically inside the window, and that the sweep actually - detects an executable mapping placed *outside* it (proving the verifier is - not a no-op). +At ``late_initcall`` the kernel sweeps its page tables, and ``efi_mm`` when +runtime services are on, and logs the number of EL1-executable leaf mappings and +how many resolve outside the window. It also counts window pages that reached +the page allocator, and confined allocations whose backing is outside the +window, as violations. Any violation is fatal unless ``ctxr_exec_enforce=0`` is +given. This is also what stands in for the hardware fault when testing in a VM. +The check runs once: memory freed after it, such as init memory, relies on +``free_reserved_area()`` skipping window pages. diff --git a/include/linux/execmem.h b/include/linux/execmem.h index d4004103c44ea..891ea71ea2045 100644 --- a/include/linux/execmem.h +++ b/include/linux/execmem.h @@ -254,16 +254,6 @@ void *execmem_confine_alloc(struct execmem_range *range, size_t size, pgprot_t pgprot, unsigned long vm_flags); bool execmem_confine_free(void *ptr); -/* - * Verify that no EL1-executable mapping resolves to a physical page outside - * the window and that no window page reached the page allocator. Returns the - * number of violations; fills in the number of executable leaf mappings - * inspected when @nr_exec is non-NULL. Implemented generically for the confined - * allocations; the page-table sweep is provided per-architecture by - * execmem_confine_arch_verify(). - */ -unsigned long execmem_confine_verify(unsigned long *nr_exec); - /* * Architecture page-table sweep. Walk the kernel page tables and, for every * privileged-executable leaf entry, add 1 to *@nr_exec and, if its output diff --git a/mm/execmem_confine.c b/mm/execmem_confine.c index d548b96f7cf0f..1985846007b8c 100644 --- a/mm/execmem_confine.c +++ b/mm/execmem_confine.c @@ -22,14 +22,12 @@ #define pr_fmt(fmt) "execmem-confine: " fmt -#include #include #include #include #include #include #include -#include #include #include #include @@ -295,29 +293,19 @@ unsigned long __weak execmem_confine_arch_verify(phys_addr_t base, return 0; } -unsigned long execmem_confine_verify(unsigned long *nr_exec_out) +static unsigned long __init execmem_confine_verify(unsigned long *nr_exec_out) { unsigned long nr_exec = 0, violations = 0, freed; struct confine_chunk *ck; unsigned long idx; - if (!confine_ready) { - if (nr_exec_out) - *nr_exec_out = 0; - return 0; - } - freed = confine_count_freed_pages(); if (freed) { pr_err("%lu window page(s) reached the page allocator\n", freed); violations += freed; } - /* - * Registry sanity: every confined allocation is backed by pool pages, - * which are by construction inside the window. Re-check anyway so the - * self-test actually inspects the physical backing it relies on. - */ + /* Every confined allocation is backed by pool pages, inside the window. */ mutex_lock(&confine_lock); xa_for_each(&confine_chunks, idx, ck) { phys_addr_t pa = virt_to_phys((void *)ck->pool_va); @@ -340,123 +328,6 @@ unsigned long execmem_confine_verify(unsigned long *nr_exec_out) return violations; } -#ifdef CONFIG_DEBUG_FS -static void confine_show_chunk(struct gen_pool *pool, - struct gen_pool_chunk *chunk, void *data) -{ - struct seq_file *m = data; - phys_addr_t start = chunk->phys_addr; - phys_addr_t end = start + (chunk->end_addr - chunk->start_addr + 1); - - seq_printf(m, "pool-range: [%pa, %pa)\n", &start, &end); -} - -static int confine_debug_show(struct seq_file *m, void *v) -{ - unsigned long nr_exec = 0, violations; - - seq_printf(m, "window-base: %pa\n", &confine_base); - seq_printf(m, "window-size: %llu MiB\n", - (unsigned long long)(confine_size >> 20)); - seq_printf(m, "enforce: %d\n", confine_enforce); - if (confine_pool) { - seq_printf(m, "pool-total: %zu KiB\n", - gen_pool_size(confine_pool) >> 10); - seq_printf(m, "pool-available: %zu KiB\n", - gen_pool_avail(confine_pool) >> 10); - gen_pool_for_each_chunk(confine_pool, confine_show_chunk, m); - } - - violations = execmem_confine_verify(&nr_exec); - seq_printf(m, "exec-mappings: %lu\n", nr_exec); - seq_printf(m, "violations: %lu\n", violations); - seq_printf(m, "result: %s\n", violations ? "FAIL" : "PASS"); - return 0; -} -DEFINE_SHOW_ATTRIBUTE(confine_debug); -#endif - -/* - * Boot self-test (enabled with "ctxr_exec_selftest" on the command line). - * - * In a VM there is no hardware CTXR to fault on an escaping fetch, so we prove - * two things in software: - * 1. a confined execmem allocation is physically inside the window, and - * 2. the page-table sweep actually catches an executable mapping placed - * outside the window (i.e. the verifier is not a no-op). - * The negative case only *maps* an out-of-window page and inspects the tables; - * nothing ever executes from it, and it is torn down immediately. - */ -static bool ctxr_selftest __initdata; - -static int __init execmem_confine_selftest_param(char *s) -{ - ctxr_selftest = true; - return 0; -} -early_param("ctxr_exec_selftest", execmem_confine_selftest_param); - -static void __init execmem_confine_selftest(void) -{ - unsigned long nr_exec = 0, before, after; - struct vm_struct *area; - struct page *page; - void *p; - bool ok; - - /* 1. positive: a confined allocation lands inside the window. */ - p = execmem_alloc(EXECMEM_MODULE_TEXT, PAGE_SIZE); - if (p) { - struct confine_chunk *ck; - - mutex_lock(&confine_lock); - ck = xa_load(&confine_chunks, (unsigned long)p >> PAGE_SHIFT); - ok = ck && phys_in_window(virt_to_phys((void *)ck->pool_va)); - mutex_unlock(&confine_lock); - pr_info("selftest 1 (confined alloc in window): %s\n", - ok ? "PASS" : "FAIL"); - execmem_free(p); - } else { - pr_info("selftest 1 (confined alloc in window): FAIL (no memory)\n"); - } - - /* 2. negative: an out-of-window executable mapping must be detected. */ - before = execmem_confine_arch_verify(confine_base, confine_size, &nr_exec); - - page = alloc_page(GFP_KERNEL); /* buddy never returns window pages */ - if (!page) { - pr_info("selftest 2 (detect escape): SKIP (no memory)\n"); - goto done; - } - area = __get_vm_area_node(PAGE_SIZE, PAGE_SIZE, PAGE_SHIFT, VM_ALLOC, - VMALLOC_START, VMALLOC_END, NUMA_NO_NODE, - GFP_KERNEL, __builtin_return_address(0)); - if (!area) { - __free_page(page); - pr_info("selftest 2 (detect escape): SKIP (no vm area)\n"); - goto done; - } - - if (vmap_pages_range((unsigned long)area->addr, - (unsigned long)area->addr + PAGE_SIZE, - PAGE_KERNEL_EXEC, &page, PAGE_SHIFT)) { - free_vm_area(area); - __free_page(page); - pr_info("selftest 2 (detect escape): SKIP (map failed)\n"); - goto done; - } - - after = execmem_confine_arch_verify(confine_base, confine_size, &nr_exec); - pr_info("selftest 2 (detect escape): %s (%lu -> %lu violations)\n", - after > before ? "PASS" : "FAIL", before, after); - - set_memory_nx((unsigned long)area->addr, 1); - free_vm_area(area); - __free_page(page); -done: - return; -} - static int __init execmem_confine_late_init(void) { unsigned long nr_exec = 0, violations; @@ -464,14 +335,6 @@ static int __init execmem_confine_late_init(void) if (!confine_ready) return 0; -#ifdef CONFIG_DEBUG_FS - debugfs_create_file("execmem_confine", 0400, NULL, NULL, - &confine_debug_fops); -#endif - - if (ctxr_selftest) - execmem_confine_selftest(); - violations = execmem_confine_verify(&nr_exec); pr_info("boot verify: %lu executable mapping(s), %lu violation(s): %s\n", nr_exec, violations, violations ? "FAIL" : "PASS"); From 0941e6f6831f7a72bc96a4e9eddc3dd2e07313ed Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Fri, 6 Nov 2026 14:14:53 -0500 Subject: [PATCH 36/47] arm64: configs: asahi: keep only the CTXR option 5863e2492e8 added dm-crypt, the crypto user APIs, the sound sequencer, Bluetooth RFCOMM and BNEP, INET_UDP_DIAG, joydev, uinput and HIDRAW to the asahi fragment. None of that is about the CTXR window. Drop it and keep CONFIG_ARM64_CTXR_EXECMEM=y with its comment. The fleet's Fedora Asahi base config already has every one of them, and four of these lines turned the base's built-in BLK_DEV_DM and CRYPTO_USER_API_{HASH,SKCIPHER,AEAD} into modules, so the fleet kernel gets those back as they were. Signed-off-by: Diljit Singh --- arch/arm64/configs/asahi.config | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/arch/arm64/configs/asahi.config b/arch/arm64/configs/asahi.config index 26784df5ce52c..7428c123b7cbe 100644 --- a/arch/arm64/configs/asahi.config +++ b/arch/arm64/configs/asahi.config @@ -151,25 +151,3 @@ CONFIG_DETECT_HUNG_TASK=y # unless the bootloader supplies a window (/chosen/aurora,ctxr-exec-window) or # the ctxr_exec_window= command-line option is given. CONFIG_ARM64_CTXR_EXECMEM=y - -# An installed system: encrypted root, and what its services load. -CONFIG_BLK_DEV_DM=m -CONFIG_DM_CRYPT=m -CONFIG_DM_INTEGRITY=m -CONFIG_DM_UEVENT=y -CONFIG_CRYPTO_ESSIV=m -CONFIG_CRYPTO_USER_API_HASH=m -CONFIG_CRYPTO_USER_API_SKCIPHER=m -CONFIG_CRYPTO_USER_API_AEAD=m -CONFIG_SND_SEQUENCER=m -CONFIG_SND_SEQ_DUMMY=m -CONFIG_SND_HRTIMER=m -CONFIG_BT_RFCOMM=m -CONFIG_BT_RFCOMM_TTY=y -CONFIG_BT_BNEP=m -CONFIG_BT_BNEP_MC_FILTER=y -CONFIG_BT_BNEP_PROTO_FILTER=y -CONFIG_INET_UDP_DIAG=m -CONFIG_INPUT_JOYDEV=m -CONFIG_INPUT_UINPUT=m -CONFIG_HIDRAW=y From 22420ee58a0ba98ffba693e613acf711e2b06f4c Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Fri, 6 Nov 2026 18:10:34 -0500 Subject: [PATCH 37/47] arm64: ctxr-execmem: never read the entry flag on the boot CPU init_el2 read ctxr_stay_el2 with the MMU off on every entry, the boot CPU's first one included. That entry runs before ctxr_exec_reserve() writes the flag, and nothing cleaned the .mmuoff.data.read line to the PoC: primary_entry cleans only the idmap text and init_el2 the HYP text, and the stub's relocation syncs code to the PoU. So on an M1-M3 machine with no window, stale DRAM from an earlier ctxr_exec_window= boot, or garbage, could send the boot CPU down the direct VHE path, running finalise_el2_state before init_feature_override() and ignoring kvm-arm.mode= and the VHE overrides. The secondaries then read the same uncleaned line. The boot CPU is never confined, so it now always takes the stock entry: init_kernel_el reads the flag only when x22 is 1, which just the secondary entry points and cpu_resume set; primary_entry sets it to 0. ctxr_exec_reserve() writes the flag on every boot, 0 when no window is active, and cleans it to the PoC before any secondary is released, so a confined secondary or a resuming CPU reads a defined value and no stale line survives. Signed-off-by: Diljit Singh --- arch/arm64/kernel/head.S | 19 ++++++++++++++++--- arch/arm64/kernel/sleep.S | 3 +++ arch/arm64/mm/ctxr_execmem.c | 30 ++++++++++++++++-------------- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/arch/arm64/kernel/head.S b/arch/arm64/kernel/head.S index dbb37dbf2522d..53e1c9af9fd22 100644 --- a/arch/arm64/kernel/head.S +++ b/arch/arm64/kernel/head.S @@ -117,6 +117,9 @@ SYM_CODE_START(primary_entry) blr x2 1: mov x0, x19 +#ifdef CONFIG_ARM64_CTXR_EXECMEM + mov x22, xzr // boot CPU: never read the CTXR flag +#endif bl init_kernel_el // w0=cpu_boot_mode mov x20, x0 @@ -309,10 +312,14 @@ SYM_INNER_LABEL(init_el2, SYM_L_LOCAL) #ifdef CONFIG_ARM64_CTXR_EXECMEM /* - * A core confined to a CTXR window can't run the window's code at - * EL1. Once the boot CPU has a window at EL2, later entries switch - * to VHE here instead of going through EL1 and finalise_el2. + * A core confined to a CTXR window can't run the window's code at EL1, + * so once a window is active it switches to VHE here instead of going + * through EL1 and finalise_el2. Only a secondary or a resuming CPU is + * ever confined (x22 == 1): the boot CPU's first entry (x22 == 0) is + * not, and it runs before ctxr_stay_el2 is set and cleaned to the PoC, + * so it must not read the flag at all. */ + cbz x22, .Lctxr_no_direct_vhe adr_l x0, ctxr_stay_el2 ldr w0, [x0] cbz w0, .Lctxr_no_direct_vhe @@ -372,6 +379,9 @@ SYM_FUNC_END(init_kernel_el) */ SYM_FUNC_START(secondary_holding_pen) mov x0, xzr +#ifdef CONFIG_ARM64_CTXR_EXECMEM + mov x22, #1 // a secondary may be confined +#endif bl init_kernel_el // w0=cpu_boot_mode mrs x2, mpidr_el1 mov_q x1, MPIDR_HWID_BITMASK @@ -390,6 +400,9 @@ SYM_FUNC_END(secondary_holding_pen) */ SYM_FUNC_START(secondary_entry) mov x0, xzr +#ifdef CONFIG_ARM64_CTXR_EXECMEM + mov x22, #1 // a secondary may be confined +#endif bl init_kernel_el // w0=cpu_boot_mode b secondary_startup SYM_FUNC_END(secondary_entry) diff --git a/arch/arm64/kernel/sleep.S b/arch/arm64/kernel/sleep.S index f093cdf71be11..11a08cb03009c 100644 --- a/arch/arm64/kernel/sleep.S +++ b/arch/arm64/kernel/sleep.S @@ -100,6 +100,9 @@ SYM_FUNC_END(__cpu_suspend_enter) .pushsection ".idmap.text", "a" SYM_CODE_START(cpu_resume) mov x0, xzr +#ifdef CONFIG_ARM64_CTXR_EXECMEM + mov x22, #1 // a resuming CPU may be confined +#endif bl init_kernel_el mov x19, x0 // preserve boot mode bl __cpu_setup diff --git a/arch/arm64/mm/ctxr_execmem.c b/arch/arm64/mm/ctxr_execmem.c index 9f2a9227d3dde..0466274ec3f6a 100644 --- a/arch/arm64/mm/ctxr_execmem.c +++ b/arch/arm64/mm/ctxr_execmem.c @@ -200,12 +200,6 @@ static bool __init ctxr_establish(phys_addr_t base, phys_addr_t size) execmem_confine_set_window(base, size); - if (is_kernel_in_hyp_mode()) { - WRITE_ONCE(ctxr_stay_el2, 1); - dcache_clean_inval_poc((unsigned long)&ctxr_stay_el2, - (unsigned long)&ctxr_stay_el2 + sizeof(ctxr_stay_el2)); - } - ctxr_base = base; ctxr_size = size; ctxr_active = true; @@ -247,20 +241,28 @@ void __init ctxr_exec_reserve(void) int dt = ctxr_parse_dt(&base, &size); /* A bad override leaves the machine as its bootloader described it. */ - if (ctxr_cmd_size) { - if (ctxr_establish(ctxr_cmd_base, ctxr_cmd_size)) - return; + if (ctxr_cmd_size && ctxr_establish(ctxr_cmd_base, ctxr_cmd_size)) + goto done; + if (ctxr_cmd_size) pr_warn("ignoring ctxr_exec_window=\n"); - } - if (!dt) - return; if (dt < 0) ctxr_hold_secondaries("malformed aurora,ctxr-exec-window"); - else if (!is_kernel_in_hyp_mode()) + else if (dt && !is_kernel_in_hyp_mode()) ctxr_hold_secondaries("window given but the kernel is not at EL2"); - else if (!ctxr_establish(base, size)) + else if (dt && !ctxr_establish(base, size)) ctxr_hold_secondaries("unusable window"); + +done: + /* + * Secondaries and resuming CPUs read ctxr_stay_el2 with the MMU off, so + * set it on every boot (0 when no window is active) and clean it to the + * PoC here, before any secondary is released. The boot CPU never reads + * it. See init_kernel_el. + */ + WRITE_ONCE(ctxr_stay_el2, ctxr_active); + dcache_clean_inval_poc((unsigned long)&ctxr_stay_el2, + (unsigned long)&ctxr_stay_el2 + sizeof(ctxr_stay_el2)); } static void __init ctxr_pool_add_gap(struct gen_pool *pool, phys_addr_t from, From 13c755306c5fb9cfb87cef1bf76a36f0b4a41a0d Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Fri, 6 Nov 2026 18:10:59 -0500 Subject: [PATCH 38/47] arm64: ctxr-execmem: run a command-line window through the same checks A successful ctxr_exec_window= returned before the device-tree checks, so it never saw the EL2 test or the hold path, and nothing compared it with the firmware's window. At EL1 it would confine the kernel's text and still release the secondaries; a window unrelated to the one the firmware armed would place code where a confined core can't fetch it. Route both the same way: a window from either place needs the kernel at EL2, and a command-line window is accepted only when it lies inside the device-tree window, if there is one. Anything else keeps the secondaries offline, as a bad device-tree window already did. A command-line window with no device-tree window (VM bring-up) is unchanged except that it now requires EL2. Update the binding doc and kernel-parameters.txt. Signed-off-by: Diljit Singh --- .../admin-guide/kernel-parameters.txt | 8 +++-- Documentation/arch/arm64/ctxr-execmem.rst | 32 ++++++++--------- arch/arm64/mm/ctxr_execmem.c | 35 +++++++++++++------ 3 files changed, 45 insertions(+), 30 deletions(-) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt index ee681cfca630b..9526a1b399fcd 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -1148,9 +1148,11 @@ Kernel parameters ctxr_exec_window= [ARM64,EARLY] Confine kernel executable code to a - physical window, overriding the device tree. This is - for bring-up and VMs. A value that doesn't parse, or a - window that can't work, is ignored with a warning. + physical window. This is for bring-up and VMs. A value + that doesn't parse is ignored with a warning. The + kernel must be at EL2, and when the device tree carries + a window the given one must lie inside it; otherwise the + secondary CPUs are kept offline. See Documentation/arch/arm64/ctxr-execmem.rst. Format: [KMG]@[KMG] diff --git a/Documentation/arch/arm64/ctxr-execmem.rst b/Documentation/arch/arm64/ctxr-execmem.rst index a99638b7e06b2..3dc3008ca9974 100644 --- a/Documentation/arch/arm64/ctxr-execmem.rst +++ b/Documentation/arch/arm64/ctxr-execmem.rst @@ -34,23 +34,23 @@ Command line (bring-up / VM) ctxr_exec_window=@ e.g. ctxr_exec_window=128M@0x40200000 ctxr_exec_enforce= (default: enforcing) - The command line overrides the device tree's window. ``size`` and ``base`` - accept the usual ``K``/``M``/``G`` suffixes, and both are required. The last - use of an option wins. - -A value that doesn't parse is ignored with a warning. So is a window from the -command line that can't work because the image doesn't fit it or it isn't in -DRAM, and the device tree's window then applies if there is one. - -The secondary CPUs can only run code inside the device tree's window, and the -boot CPU isn't confined. So when that window can't be used, the kernel logs why, + ``size`` and ``base`` accept the usual ``K``/``M``/``G`` suffixes, and both + are required. The last use of an option wins. The kernel must be at EL2, and + when the device tree also carries a window the command-line one must lie + inside it: the command line can only pick a window within what the firmware + armed, never a different region. A value that doesn't parse is ignored with + a warning. + +The secondary CPUs can only run code inside the window, and the boot CPU isn't +confined. So when a window is given but can't be used, the kernel logs why, leaves the secondaries in the bootloader and boots on the boot CPU alone, as with -``nosmp``. That happens when the property is malformed (not four cells, a base or -size of 0 or not page aligned, or a window that wraps), when the image -doesn't fit the window or the window isn't in DRAM, when it can't be reserved, -when no free memory is left in it for executable allocations, and when the kernel -isn't running at EL2 (``kvm-arm.mode=nvhe`` or ``protected``, for example). With -no such property nothing changes. +``nosmp``. That happens when the device-tree property is malformed (not four +cells, a base or size of 0 or not page aligned, or a window that wraps), when the +image doesn't fit the window or the window isn't in DRAM, when it can't be +reserved, when no free memory is left in it for executable allocations, when the +command-line window isn't inside the device-tree one, and when the kernel isn't +running at EL2 (``kvm-arm.mode=nvhe`` or ``protected``, for example). With no +window from either place, nothing changes. The kernel doesn't check the device tree's window against ``CTXR_D``. The bootloader reads ``CTXR_D`` when it chooses the window and reserves the rest of diff --git a/arch/arm64/mm/ctxr_execmem.c b/arch/arm64/mm/ctxr_execmem.c index 0466274ec3f6a..60ec0b9cbed21 100644 --- a/arch/arm64/mm/ctxr_execmem.c +++ b/arch/arm64/mm/ctxr_execmem.c @@ -235,25 +235,38 @@ bool __init ctxr_exec_secondaries_held(void) return ctxr_hold_cpus; } +/* Is [b, b + s) wholly inside [base, base + size)? */ +static bool __init ctxr_within(phys_addr_t b, phys_addr_t s, + phys_addr_t base, phys_addr_t size) +{ + return b >= base && b + s <= base + size; +} + void __init ctxr_exec_reserve(void) { phys_addr_t base = 0, size = 0; int dt = ctxr_parse_dt(&base, &size); - /* A bad override leaves the machine as its bootloader described it. */ - if (ctxr_cmd_size && ctxr_establish(ctxr_cmd_base, ctxr_cmd_size)) - goto done; - if (ctxr_cmd_size) - pr_warn("ignoring ctxr_exec_window=\n"); - + /* + * A command-line window and a device-tree window take the same EL2 and + * hold checks: a core confined to the firmware's window can't run this + * kernel from outside it, whichever supplied the bounds. The command + * line can only pick a window inside the firmware's one. + */ if (dt < 0) ctxr_hold_secondaries("malformed aurora,ctxr-exec-window"); - else if (dt && !is_kernel_in_hyp_mode()) - ctxr_hold_secondaries("window given but the kernel is not at EL2"); - else if (dt && !ctxr_establish(base, size)) - ctxr_hold_secondaries("unusable window"); + else if (dt || ctxr_cmd_size) { + if (!is_kernel_in_hyp_mode()) + ctxr_hold_secondaries("window given but the kernel is not at EL2"); + else if (ctxr_cmd_size && dt && + !ctxr_within(ctxr_cmd_base, ctxr_cmd_size, base, size)) + ctxr_hold_secondaries("ctxr_exec_window= is not inside the device-tree window"); + else if (ctxr_cmd_size && !ctxr_establish(ctxr_cmd_base, ctxr_cmd_size)) + ctxr_hold_secondaries("unusable ctxr_exec_window="); + else if (!ctxr_cmd_size && !ctxr_establish(base, size)) + ctxr_hold_secondaries("unusable window"); + } -done: /* * Secondaries and resuming CPUs read ctxr_stay_el2 with the MMU off, so * set it on every boot (0 when no window is active) and clean it to the From 59a218c59f7f96d273c77bcdab4316118c746465 Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Fri, 6 Nov 2026 18:16:14 -0500 Subject: [PATCH 39/47] arm64: mm: check PFN-mapped window pages in the EL0 fetch backstop The backstop only walked a folio, so a VM_PFNMAP or struct-page-less VM_MIXEDMAP mapping was never checked, although the comment said the PFN might have no struct page. With the zero page now outside the window, a driver's remap_pfn_range() of an image buffer is the likeliest way a window page reaches EL0, and that is exactly a PFNMAP with no folio. Fall back to follow_pfnmap_start() for such a VMA when the folio walk finds nothing, and compare that PFN against the window as before. The doc's claim is corrected to match. Signed-off-by: Diljit Singh --- Documentation/arch/arm64/ctxr-execmem.rst | 6 ++++-- arch/arm64/mm/fault.c | 25 +++++++++++++++-------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/Documentation/arch/arm64/ctxr-execmem.rst b/Documentation/arch/arm64/ctxr-execmem.rst index 3dc3008ca9974..fc91f2f46de16 100644 --- a/Documentation/arch/arm64/ctxr-execmem.rst +++ b/Documentation/arch/arm64/ctxr-execmem.rst @@ -107,8 +107,10 @@ it up. Anything EL0 can execute therefore lives outside the window: As a backstop, when a window is active, an EL0 instruction permission fault on a present, executable mapping of a window page logs the task and its address and -PFN, then kills it with an uncatchable ``SIGSEGV`` rather than retrying. Any -other fault takes the usual path. +PFN, then kills it with an uncatchable ``SIGSEGV`` rather than retrying. The +mapping is resolved to a PFN either through its folio or, for a ``VM_PFNMAP`` or +``VM_MIXEDMAP`` mapping with no struct page (a driver's ``remap_pfn_range()`` of +an image buffer), through a raw PFN walk. Any other fault takes the usual path. EFI runtime services ==================== diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c index ac1f23f911138..c95f3ca4c6700 100644 --- a/arch/arm64/mm/fault.c +++ b/arch/arm64/mm/fault.c @@ -607,14 +607,14 @@ static bool is_invalid_gcs_access(struct vm_area_struct *vma, u64 esr) /* * A core enforcing the CTXR window refuses EL0 fetches from it with a permission * fault that the page tables don't explain. Nothing can fix that up, so a retry - * would fault forever with the task still runnable. The PFN may have no usable - * struct page, so only compare it. + * would fault forever with the task still runnable. The window page may be + * mapped without a struct page (a driver's remap_pfn_range() of an image + * buffer), so fall back to a raw PFN walk and only ever compare the PFN. */ static bool el0_fetch_refused_by_window(struct mm_struct *mm, unsigned long addr, unsigned long esr, unsigned int mm_flags) { struct vm_area_struct *vma; - struct folio_walk fw; unsigned long pfn = 0; if (!is_el0_instruction_abort(esr) || !esr_fsc_is_permission_fault(esr) || @@ -625,11 +625,20 @@ static bool el0_fetch_refused_by_window(struct mm_struct *mm, unsigned long addr return false; vma = vma_lookup(mm, addr); - if (vma && (vma->vm_flags & VM_EXEC) && !fault_from_pkey(vma, mm_flags) && - folio_walk_start(&fw, vma, addr, FW_ZEROPAGE)) { - if (fw.level == FW_LEVEL_PTE && pte_user_exec(fw.pte)) - pfn = pte_pfn(fw.pte); - folio_walk_end(&fw, vma); + if (vma && (vma->vm_flags & VM_EXEC) && !fault_from_pkey(vma, mm_flags)) { + struct follow_pfnmap_args pfnmap = { .vma = vma, .address = addr, + .allow_subpage = true }; + struct folio_walk fw; + + if (folio_walk_start(&fw, vma, addr, FW_ZEROPAGE)) { + if (fw.level == FW_LEVEL_PTE && pte_user_exec(fw.pte)) + pfn = pte_pfn(fw.pte); + folio_walk_end(&fw, vma); + } else if ((vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP)) && + !follow_pfnmap_start(&pfnmap)) { + pfn = pfnmap.pfn; + follow_pfnmap_end(&pfnmap); + } } mmap_read_unlock(mm); From 7f54ed9a877507aeb4cec6bc745c1348f894a232 Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Fri, 6 Nov 2026 18:21:14 -0500 Subject: [PATCH 40/47] arm64: ctxr-execmem: re-reserve the window before the crash kernel unflatten_device_tree() runs fdt_scan_reserved_mem_late(), which frees a reserved-memory node whose late init fails and can punch a hole in the window. bootmem_init() then reserves the crash kernel, which could land in that hole and later be handed to the buddy as a window page through free_reserved_page(). Close the hole at the start of bootmem_init(), before arch_reserve_crashkernel(), instead of in arch_mm_preinit() after it. Signed-off-by: Diljit Singh --- arch/arm64/mm/init.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c index 9ddd592c17cdf..f937420e17997 100644 --- a/arch/arm64/mm/init.c +++ b/arch/arm64/mm/init.c @@ -308,6 +308,14 @@ void __init bootmem_init(void) { unsigned long min, max; + /* + * unflatten_device_tree() has run fdt_scan_reserved_mem_late(), which + * can free a failed reserved-memory node and punch a hole in the + * window. Close it before arch_reserve_crashkernel() can place the + * crash kernel there, only for it to be freed later as a window page. + */ + ctxr_exec_keep_reserved(); + min = PFN_UP(memblock_start_of_DRAM()); max = PFN_DOWN(memblock_end_of_DRAM()); @@ -371,8 +379,6 @@ void __init arch_mm_preinit(void) { unsigned int flags = SWIOTLB_VERBOSE; - ctxr_exec_keep_reserved(); - if (max_pfn <= PFN_DOWN(arm64_dma_phys_limit)) { /* * If no bouncing needed for ZONE_DMA, reduce the swiotlb From c2f9d0cc1ce6a4bb3b445d2c0ea616908871874c Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Fri, 6 Nov 2026 19:03:52 -0500 Subject: [PATCH 41/47] mm/cma: check the window overlap before allocating the bitmap cma_activate_area() allocated each range's bitmap first and only then checked whether the area overlaps the executable window. A bitmap allocation failure jumps to cleanup, which frees the area's pages to the buddy unless the reserve-on-error bit is set, so an overlapping area could leak window pages into the allocator on that path. Do the overlap check, which sets that bit, before the bitmap loop. Signed-off-by: Diljit Singh --- mm/cma.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/mm/cma.c b/mm/cma.c index 75dbb94d2c447..3894990f588d9 100644 --- a/mm/cma.c +++ b/mm/cma.c @@ -156,10 +156,21 @@ static bool __init cma_overlaps_exec_window(struct cma *cma) static void __init cma_activate_area(struct cma *cma) { unsigned long pfn, end_pfn, early_pfn[CMA_MAX_RANGES]; - int allocrange, r; + int allocrange = 0, r; struct cma_memrange *cmr; unsigned long bitmap_count, count; + /* + * Check this before allocating anything: a bitmap-allocation failure + * jumps to cleanup, which would free the pages to the buddy unless the + * reserve-on-error bit is already set. + */ + if (cma_overlaps_exec_window(cma)) { + pr_err("CMA area %s overlaps the executable window\n", cma->name); + cma_reserve_pages_on_error(cma); + goto cleanup; + } + for (allocrange = 0; allocrange < cma->nranges; allocrange++) { cmr = &cma->ranges[allocrange]; early_pfn[allocrange] = cmr->early_pfn; @@ -169,12 +180,6 @@ static void __init cma_activate_area(struct cma *cma) goto cleanup; } - if (cma_overlaps_exec_window(cma)) { - pr_err("CMA area %s overlaps the executable window\n", cma->name); - cma_reserve_pages_on_error(cma); - goto cleanup; - } - if (!cma_validate_zones(cma)) goto cleanup; From 344bd8f2bdb0d21805a7437c3852d1b90b9ec435 Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Fri, 6 Nov 2026 19:03:53 -0500 Subject: [PATCH 42/47] mm/execmem: back a confined allocation one window page at a time execmem_confine_alloc() drew the whole allocation from the window with a single gen_pool_alloc(), which needs that many physically contiguous bytes free. The pages are vmapped one by one, so contiguity isn't needed, and a fragmented window could fail a 2 MiB BPF pack or a large module's text with enough free pages to spare. Allocate a page at a time, record the pages, and return them to the pool individually on free and on every error path. While here, use kvmalloc_array() for the page array, which can be large, and warn if set_memory_nx()/set_memory_rw() fail on free rather than leaving a possible executable alias behind without a word. Signed-off-by: Diljit Singh --- mm/execmem_confine.c | 86 +++++++++++++++++++++++++++----------------- 1 file changed, 53 insertions(+), 33 deletions(-) diff --git a/mm/execmem_confine.c b/mm/execmem_confine.c index 1985846007b8c..1cb61fd2ca779 100644 --- a/mm/execmem_confine.c +++ b/mm/execmem_confine.c @@ -40,9 +40,8 @@ struct confine_chunk { struct vm_struct *area; - unsigned long pool_va; /* linear-map VA handed out by the pool */ size_t size; - struct page **pages; + struct page **pages; /* one window page each */ }; static phys_addr_t confine_base __ro_after_init; @@ -56,6 +55,18 @@ static DEFINE_XARRAY(confine_chunks); /* serialises pool (de)allocation and the xarray */ static DEFINE_MUTEX(confine_lock); +/* Return a chunk's pages to the window pool. The caller holds no lock. */ +static void confine_pool_free(struct page **pages, unsigned int nr) +{ + unsigned int i; + + mutex_lock(&confine_lock); + for (i = 0; i < nr; i++) + gen_pool_free(confine_pool, + (unsigned long)page_address(pages[i]), PAGE_SIZE); + mutex_unlock(&confine_lock); +} + void __init execmem_confine_set_window(phys_addr_t base, phys_addr_t size) { confine_base = base; @@ -125,7 +136,7 @@ void *execmem_confine_alloc(struct execmem_range *range, size_t size, struct confine_chunk *ck; struct vm_struct *area; struct page **pages; - unsigned long pool_va, vaddr; + unsigned long vaddr; unsigned int i, nr; int err; @@ -153,7 +164,7 @@ void *execmem_confine_alloc(struct execmem_range *range, size_t size, if (!area) return NULL; - pages = kmalloc_array(nr, sizeof(*pages), GFP_KERNEL); + pages = kvmalloc_array(nr, sizeof(*pages), GFP_KERNEL); if (!pages) goto err_area; @@ -161,18 +172,27 @@ void *execmem_confine_alloc(struct execmem_range *range, size_t size, if (!ck) goto err_pages; - mutex_lock(&confine_lock); - pool_va = gen_pool_alloc(confine_pool, size); - mutex_unlock(&confine_lock); - if (!pool_va) { - pr_warn_ratelimited("window exhausted, cannot satisfy %zu bytes\n", - size); - goto err_ck; + /* + * Draw the backing a page at a time. The pages are vmapped one by one, + * so they need not be physically contiguous; a single gen_pool_alloc() + * of the whole size could fail on a fragmented window with free pages + * to spare. + */ + for (i = 0; i < nr; i++) { + unsigned long page_va; + + mutex_lock(&confine_lock); + page_va = gen_pool_alloc(confine_pool, PAGE_SIZE); + mutex_unlock(&confine_lock); + if (!page_va) { + pr_warn_ratelimited("window exhausted, cannot satisfy %zu bytes\n", + size); + confine_pool_free(pages, i); + goto err_ck; + } + pages[i] = virt_to_page((void *)page_va); } - for (i = 0; i < nr; i++) - pages[i] = virt_to_page((void *)(pool_va + ((unsigned long)i << PAGE_SHIFT))); - vaddr = (unsigned long)area->addr; err = vmap_pages_range(vaddr, vaddr + size, pgprot, pages, PAGE_SHIFT); if (err) @@ -188,7 +208,6 @@ void *execmem_confine_alloc(struct execmem_range *range, size_t size, area->nr_pages = nr; ck->area = area; - ck->pool_va = pool_va; ck->size = size; ck->pages = pages; @@ -223,16 +242,14 @@ void *execmem_confine_alloc(struct execmem_range *range, size_t size, * before the pages can be mapped again for someone else. */ flush_tlb_kernel_range(vaddr, vaddr + size); - mutex_lock(&confine_lock); - gen_pool_free(confine_pool, pool_va, size); - mutex_unlock(&confine_lock); + confine_pool_free(pages, nr); kfree(ck); - kfree(pages); + kvfree(pages); return NULL; err_ck: kfree(ck); err_pages: - kfree(pages); + kvfree(pages); err_area: free_vm_area(area); return NULL; @@ -262,10 +279,12 @@ bool execmem_confine_free(void *ptr) /* * We bypass vfree(), so VM_FLUSH_RESET_PERMS does not run for us: drop * the mapping back to non-executable, writable before tearing it down, - * so the window never retains a stale executable alias. + * so the window never retains a stale executable alias. A failure here + * would leave an executable alias behind, so warn rather than ignore it. */ - set_memory_nx(vaddr, nr); - set_memory_rw(vaddr, nr); + if (set_memory_nx(vaddr, nr) || set_memory_rw(vaddr, nr)) + pr_warn_ratelimited("could not reset perms on %#lx before freeing\n", + vaddr); free_vm_area(ck->area); @@ -276,11 +295,9 @@ bool execmem_confine_free(void *ptr) */ flush_tlb_kernel_range(vaddr, vaddr + ck->size); - mutex_lock(&confine_lock); - gen_pool_free(confine_pool, ck->pool_va, ck->size); - mutex_unlock(&confine_lock); + confine_pool_free(ck->pages, nr); - kfree(ck->pages); + kvfree(ck->pages); kfree(ck); return true; } @@ -308,13 +325,16 @@ static unsigned long __init execmem_confine_verify(unsigned long *nr_exec_out) /* Every confined allocation is backed by pool pages, inside the window. */ mutex_lock(&confine_lock); xa_for_each(&confine_chunks, idx, ck) { - phys_addr_t pa = virt_to_phys((void *)ck->pool_va); - phys_addr_t end = pa + ck->size - 1; + unsigned int i, nr = ck->size >> PAGE_SHIFT; + + for (i = 0; i < nr; i++) { + phys_addr_t pa = page_to_phys(ck->pages[i]); - if (!phys_in_window(pa) || !phys_in_window(end)) { - pr_err("confined chunk at %#lx backs out-of-window phys [%pa, %pa]\n", - (unsigned long)ck->area->addr, &pa, &end); - violations++; + if (!phys_in_window(pa)) { + pr_err("confined chunk at %#lx backs out-of-window phys %pa\n", + (unsigned long)ck->area->addr, &pa); + violations++; + } } } mutex_unlock(&confine_lock); From 8eb1ce3ce9867b03f975023c05f200b198428b4f Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Fri, 6 Nov 2026 19:03:54 -0500 Subject: [PATCH 43/47] arm64: vdso: free the copied pages if __vdso_init() fails When a window is active __vdso_init() copies each vDSO code page into a fresh page. If one allocation failed it returned -ENOMEM leaving the page-list array and the copies already made behind. Free them on the error path, taking care not to free the image pages used when no window is active. Signed-off-by: Diljit Singh --- arch/arm64/kernel/vdso.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kernel/vdso.c b/arch/arm64/kernel/vdso.c index 3aea59d87364f..962a7501cbae3 100644 --- a/arch/arm64/kernel/vdso.c +++ b/arch/arm64/kernel/vdso.c @@ -110,12 +110,20 @@ static int __init __vdso_init(enum vdso_abi abi) for (i = 0; i < vdso_info[abi].vdso_pages; i++) { vdso_pagelist[i] = vdso_code_page(pfn + i); if (!vdso_pagelist[i]) - return -ENOMEM; + goto err_free; } vdso_info[abi].cm->pages = vdso_pagelist; return 0; + +err_free: + /* Only the confined copies were allocated; image pages must not go. */ + if (execmem_confine_active()) + while (i--) + __free_page(vdso_pagelist[i]); + kfree(vdso_pagelist); + return -ENOMEM; } static int __setup_additional_pages(enum vdso_abi abi, From 9478b364d2ef74303f3af745e223add3a29c3f9b Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Fri, 6 Nov 2026 19:03:54 -0500 Subject: [PATCH 44/47] arm64: ctxr-execmem: correct who places the image and what the pool holds The bootloader places the static image in the window; the kernel doesn't. And the pool is built from the window's free ranges snapshotted when it was reserved, not from a "non-image remainder". Fix the Kconfig help, the documentation and the file comment. Signed-off-by: Diljit Singh --- Documentation/arch/arm64/ctxr-execmem.rst | 6 ++++-- arch/arm64/Kconfig | 12 ++++++------ arch/arm64/mm/ctxr_execmem.c | 4 ++-- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/Documentation/arch/arm64/ctxr-execmem.rst b/Documentation/arch/arm64/ctxr-execmem.rst index fc91f2f46de16..2e131d2403839 100644 --- a/Documentation/arch/arm64/ctxr-execmem.rst +++ b/Documentation/arch/arm64/ctxr-execmem.rst @@ -68,8 +68,10 @@ How it works * ``ctxr_exec_reserve()`` (from ``arm64_memblock_init()``) parses the window, checks that ``[_text, _end)`` fits inside it, and reserves it from memblock. -* ``execmem_arch_setup()`` carves the non-image remainder of the window into a - ``gen_pool`` and registers it with the generic confinement core +* ``execmem_arch_setup()`` carves the window's free memory — a snapshot of the + ranges still free when the window was reserved, so the image and everything + else already reserved stay out — into a ``gen_pool`` and registers it with the + generic confinement core (``CONFIG_EXECMEM_CONFINE``). The executable ranges (module text, BPF, kprobes and, by inheritance, ftrace) are flagged ``EXECMEM_CONFINE``. * A confined allocation reserves its virtual area in the normal module range — diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index e7d7418b27660..81f92a6fb50f7 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -2549,12 +2549,12 @@ config ARM64_CTXR_EXECMEM executable physical page must live inside it. With this option the kernel reserves a single window — described by - the bootloader in /chosen/aurora,ctxr-exec-window, or by the - "ctxr_exec_window=" command-line option for VM bring-up — places the - static image inside it, and backs every run-time executable allocation - (modules, BPF, kprobes, ftrace) with pages drawn from the remainder. - A verifier sweeps the kernel page tables and reports (or, when the - window is enforcing, panics on) any executable mapping that escapes. + the bootloader in /chosen/aurora,ctxr-exec-window, where it has already + placed the static image, or by the "ctxr_exec_window=" command-line + option for VM bring-up — and backs every run-time executable allocation + (modules, BPF, kprobes, ftrace) with pages drawn from the window's free + memory. A verifier sweeps the kernel page tables and reports (or, when + the window is enforcing, panics on) any executable mapping that escapes. This has no effect unless the bootloader or the command line supplies a window, so a distribution kernel for Apple silicon can enable it. diff --git a/arch/arm64/mm/ctxr_execmem.c b/arch/arm64/mm/ctxr_execmem.c index 60ec0b9cbed21..da6671b10b4e6 100644 --- a/arch/arm64/mm/ctxr_execmem.c +++ b/arch/arm64/mm/ctxr_execmem.c @@ -14,8 +14,8 @@ * emitted by the bootloader) or the command line (ctxr_exec_window=, used * for VM/simulated bring-up), * - reserves it from memblock and checks the image fits, - * - carves the non-image remainder into a gen_pool and hands it to the generic - * execmem confinement core, and + * - carves the memory still free when it was reserved into a gen_pool and + * hands it to the generic execmem confinement core, and * - provides the page-table sweep that the verifier uses to prove, in a VM * without real CTXR hardware, that no EL1-executable mapping escapes. */ From f6b1cbf1bc17447f5024bef7de9208282702f4ec Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Fri, 6 Nov 2026 19:03:55 -0500 Subject: [PATCH 45/47] cpuidle: apple: reuse the M1-to-M3 list in the load check apple_cpuidle_init() open-coded the M1 to M3 compatibles that apple_cyc_ovrd_machines already lists. Match against that list and add only the M4-and-later parts that load the driver for its WFI state (T6050, T8140), so the two can't drift apart. Signed-off-by: Diljit Singh --- drivers/cpuidle/cpuidle-apple.c | 23 +++++++------------ .../selftests/drivers/gpu/apple-dcp/idle.py | 8 +++---- 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/drivers/cpuidle/cpuidle-apple.c b/drivers/cpuidle/cpuidle-apple.c index 831f9f5115eed..38e5de49a1e3b 100644 --- a/drivers/cpuidle/cpuidle-apple.c +++ b/drivers/cpuidle/cpuidle-apple.c @@ -181,21 +181,14 @@ static int __init apple_cpuidle_init(void) if (!of_machine_is_compatible("apple,arm-platform")) return 0; - if (!(of_machine_is_compatible("apple,t8103") || - of_machine_is_compatible("apple,t8112") || - of_machine_is_compatible("apple,t8122") || - of_machine_is_compatible("apple,t6000") || - of_machine_is_compatible("apple,t6001") || - of_machine_is_compatible("apple,t6002") || - of_machine_is_compatible("apple,t6020") || - of_machine_is_compatible("apple,t6021") || - of_machine_is_compatible("apple,t6022") || - of_machine_is_compatible("apple,t6030") || - of_machine_is_compatible("apple,t6031") || - of_machine_is_compatible("apple,t6032") || - of_machine_is_compatible("apple,t6034") || - of_machine_is_compatible("apple,t6050") || - of_machine_is_compatible("apple,t8140"))) + /* + * The M1 to M3 machines (CPU PD) plus the M4-and-later ones that load + * the driver for its WFI state. The device-tree parts above are matched + * by their cpuidle node instead. + */ + if (!of_machine_compatible_match(apple_cyc_ovrd_machines) && + !of_machine_is_compatible("apple,t6050") && + !of_machine_is_compatible("apple,t8140")) return 0; pdev = platform_device_register_simple("cpuidle-apple", -1, NULL, 0); diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/idle.py b/tools/testing/selftests/drivers/gpu/apple-dcp/idle.py index c84e0ff9c53d6..d699ab4c51f83 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/idle.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/idle.py @@ -160,10 +160,10 @@ def function(text, name): pm_fail=1;check(apple_enter_idle(&cpuidle_dev[0],&apple_idle_driver,1)==-1&&deep==1);pm=0; check(cpuidle_enter_s2idle(&apple_idle_driver,&cpuidle_dev[0],100000)==1); check(tick_freeze_depth==0&&cpuidle_dev[0].states_usage[1].s2idle_usage==1&&deep==1);pm=0; - /* Anything but M1 to M3, T6041 and T8142 included: the same WFI-only states as T8140. */ - static const char *const wfi_only[]={"apple,t6041","apple,t8142",NULL}; - for(const char *const *m=wfi_only;*m;m++){ - reset(*m);check(!apple_cpuidle_probe(&pdev)); + /* A WFI-only M4 part that still probes (it has a cpuidle node): same states as T8140. */ + { + const char *m="apple,t6041"; + reset(m);check(!apple_cpuidle_probe(&pdev)); check(apple_idle_driver.state_count==2&&apple_idle_driver.states[1].enter==apple_enter_wfi); check(apple_idle_driver.states[1].enter_s2idle==apple_enter_wfi&&apple_idle_driver.states[1].flags==0); check(find_deepest_state(&apple_idle_driver,&cpuidle_dev[0],100000,0,true)==1); From 6315aea76cc245331d4342419b729e450a8f76b9 Mon Sep 17 00:00:00 2001 From: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> Date: Sat, 7 Nov 2026 13:51:36 +1100 Subject: [PATCH 46/47] arm64: bpf: size the JIT limit from the CTXR pool bpf_jit_limit, the cap on unprivileged JIT memory, defaults to half of bpf_jit_alloc_exec_limit(), which arm64 reports as the vmalloc space (about 64 TiB). Under a CTXR window every JIT image comes from the window's executable pool instead, about 53 MiB on a J700. On that machine an unprivileged user (uid 65534, no capabilities) filled the pool with 1379 classic socket filters in 0.3 s, after which root could no longer load a 4 MiB module. Record how much of the window the pool gets when the window is reserved, which is before bpf_jit_charge_init() runs, and report that as the JIT allocation limit. bpf_jit_limit then defaults to half of the pool, and bpf_jit_limit_max to the pool. Without a window nothing changes. Signed-off-by: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> Assisted-by: LLM --- arch/arm64/include/asm/ctxr_execmem.h | 7 +++++++ arch/arm64/mm/ctxr_execmem.c | 12 +++++++++++- arch/arm64/net/bpf_jit_comp.c | 11 ++++++++++- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/arch/arm64/include/asm/ctxr_execmem.h b/arch/arm64/include/asm/ctxr_execmem.h index d15a82ad831ca..b7bf460ddfcfa 100644 --- a/arch/arm64/include/asm/ctxr_execmem.h +++ b/arch/arm64/include/asm/ctxr_execmem.h @@ -42,6 +42,12 @@ bool ctxr_exec_efi_runtime_ok(void); */ bool ctxr_exec_window_reserved(void); +/* + * Bytes of the window the executable pool gets, or 0 with no window. Valid as + * soon as ctxr_exec_reserve() has run, before the pool itself exists. + */ +u64 ctxr_exec_pool_bytes(void); + /* True if cores confined to the window can't run this kernel. */ bool ctxr_exec_secondaries_held(void); #else @@ -51,6 +57,7 @@ static inline bool ctxr_exec_execmem_setup(void) { return false; } static inline bool ctxr_exec_efi_runtime_ok(void) { return true; } static inline bool ctxr_exec_window_reserved(void) { return false; } static inline bool ctxr_exec_secondaries_held(void) { return false; } +static inline u64 ctxr_exec_pool_bytes(void) { return 0; } #endif #endif /* __ASM_CTXR_EXECMEM_H */ diff --git a/arch/arm64/mm/ctxr_execmem.c b/arch/arm64/mm/ctxr_execmem.c index da6671b10b4e6..dca84c0657b85 100644 --- a/arch/arm64/mm/ctxr_execmem.c +++ b/arch/arm64/mm/ctxr_execmem.c @@ -46,6 +46,8 @@ static phys_addr_t ctxr_base __initdata; static phys_addr_t ctxr_size __initdata; static bool ctxr_enforce __initdata = true; static bool ctxr_active __initdata; +/* What the executable pool will get, known as soon as the window is reserved. */ +static u64 ctxr_pool_bytes __ro_after_init; static bool ctxr_hold_cpus __initdata; /* Read with the MMU off by cores entering init_kernel_el. */ @@ -167,6 +169,7 @@ static phys_addr_t __init ctxr_snapshot_free(phys_addr_t base, phys_addr_t size) static bool __init ctxr_establish(phys_addr_t base, phys_addr_t size) { phys_addr_t img_start = __pa_symbol(_text), img_end = __pa_symbol(_end); + phys_addr_t usable; phys_addr_t dram_start = memblock_start_of_DRAM(); phys_addr_t dram_end = memblock_end_of_DRAM(); @@ -187,7 +190,8 @@ static bool __init ctxr_establish(phys_addr_t base, phys_addr_t size) * Must run before the window is reserved, or every range looks taken. * The pool is built after the secondaries start, so check it now. */ - if (!ctxr_snapshot_free(base, size)) { + usable = ctxr_snapshot_free(base, size); + if (!usable) { pr_err("window has no free space for executable allocations\n"); return false; } @@ -202,6 +206,7 @@ static bool __init ctxr_establish(phys_addr_t base, phys_addr_t size) ctxr_base = base; ctxr_size = size; + ctxr_pool_bytes = usable; ctxr_active = true; pr_info("reserved exec window [%pa, +%llu MiB), %s\n", @@ -324,6 +329,11 @@ bool __init ctxr_exec_execmem_setup(void) panic("ctxr-execmem: cannot build the executable pool\n"); } +u64 ctxr_exec_pool_bytes(void) +{ + return ctxr_pool_bytes; +} + bool __init ctxr_exec_window_reserved(void) { return ctxr_active; diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c index c1279fabce473..1cb77cbf298b4 100644 --- a/arch/arm64/net/bpf_jit_comp.c +++ b/arch/arm64/net/bpf_jit_comp.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -2409,7 +2410,15 @@ void *bpf_arch_text_copy(void *dst, void *src, size_t len) u64 bpf_jit_alloc_exec_limit(void) { - return VMALLOC_END - VMALLOC_START; + /* + * Under a CTXR window every JIT image comes from the window's pool, which + * is tens of MiB, not the vmalloc space. Sizing the limit from the pool + * keeps unprivileged programs (bpf_jit_limit defaults to half of this) from + * using up the memory that module text and privileged BPF need. + */ + u64 pool = ctxr_exec_pool_bytes(); + + return pool ?: VMALLOC_END - VMALLOC_START; } /* Indicate the JIT backend supports mixing bpf2bpf and tailcalls. */ From 273b9e8c84654d033336d9fc554ec6f2ea6945a5 Mon Sep 17 00:00:00 2001 From: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> Date: Sat, 7 Nov 2026 13:51:37 +1100 Subject: [PATCH 47/47] mm/execmem: keep a quarter of the confined pool for module text bpf_jit_limit only charges unprivileged programs. Privileged BPF (cgroup programs from the init system, tracing tools) can still fill the confined pool, and then no module loads. Add EXECMEM_CONFINE_BOUNDED: a confined range with it never takes the pool below a reserve, a quarter of the pool, kept for the other confined ranges. arm64 sets it on the BPF range. Hold confine_lock across the page loop so the check still holds when the last page is taken, and free the pages of a failed allocation under the same lock. Signed-off-by: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> Assisted-by: LLM --- arch/arm64/mm/init.c | 3 ++- include/linux/execmem.h | 4 ++++ mm/execmem_confine.c | 43 ++++++++++++++++++++++++++++++----------- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c index f937420e17997..f5d7f5d25f741 100644 --- a/arch/arm64/mm/init.c +++ b/arch/arm64/mm/init.c @@ -617,7 +617,8 @@ struct execmem_info __init *execmem_arch_setup(void) .end = VMALLOC_END, .pgprot = PAGE_KERNEL, .alignment = 1, - .flags = cflags, + /* JIT images can't use up what modules need. */ + .flags = cflags ? cflags | EXECMEM_CONFINE_BOUNDED : 0, }, }, }; diff --git a/include/linux/execmem.h b/include/linux/execmem.h index 891ea71ea2045..2ab917b2d4bd6 100644 --- a/include/linux/execmem.h +++ b/include/linux/execmem.h @@ -53,11 +53,15 @@ enum execmem_type { * (see CONFIG_EXECMEM_CONFINE). The virtual range * is still taken from [start, end); only the * physical backing is restricted. + * @EXECMEM_CONFINE_BOUNDED: with EXECMEM_CONFINE, allocations for this range + * never take the confined pool below the share + * kept for the other confined ranges (module text) */ enum execmem_range_flags { EXECMEM_KASAN_SHADOW = (1 << 0), EXECMEM_ROX_CACHE = (1 << 1), EXECMEM_CONFINE = (1 << 2), + EXECMEM_CONFINE_BOUNDED = (1 << 3), }; #ifdef CONFIG_ARCH_HAS_EXECMEM_ROX diff --git a/mm/execmem_confine.c b/mm/execmem_confine.c index 1cb61fd2ca779..c839a2e07537a 100644 --- a/mm/execmem_confine.c +++ b/mm/execmem_confine.c @@ -49,21 +49,33 @@ static phys_addr_t confine_size __ro_after_init; static struct gen_pool *confine_pool __ro_after_init; static bool confine_enforce __ro_after_init; static bool confine_ready __ro_after_init; +/* + * Pool bytes an EXECMEM_CONFINE_BOUNDED range (BPF) may never take, so that a + * pool filled with JIT images still loads modules. + */ +static size_t confine_reserve __ro_after_init; /* keyed by mapped-VA PFN -> struct confine_chunk */ static DEFINE_XARRAY(confine_chunks); /* serialises pool (de)allocation and the xarray */ static DEFINE_MUTEX(confine_lock); -/* Return a chunk's pages to the window pool. The caller holds no lock. */ -static void confine_pool_free(struct page **pages, unsigned int nr) +/* Return a chunk's pages to the window pool. The caller holds confine_lock. */ +static void __confine_pool_free(struct page **pages, unsigned int nr) { unsigned int i; - mutex_lock(&confine_lock); + lockdep_assert_held(&confine_lock); for (i = 0; i < nr; i++) gen_pool_free(confine_pool, (unsigned long)page_address(pages[i]), PAGE_SIZE); +} + +/* Return a chunk's pages to the window pool. The caller holds no lock. */ +static void confine_pool_free(struct page **pages, unsigned int nr) +{ + mutex_lock(&confine_lock); + __confine_pool_free(pages, nr); mutex_unlock(&confine_lock); } @@ -82,16 +94,17 @@ int __init execmem_confine_init(struct gen_pool *pool, bool enforce) confine_pool = pool; confine_enforce = enforce; + confine_reserve = PAGE_ALIGN(gen_pool_size(pool) / 4); smp_wmb(); confine_ready = true; { phys_addr_t end = confine_base + confine_size; - pr_info("window [%pa, %pa) (%llu MiB), %s\n", + pr_info("window [%pa, %pa) (%llu MiB), %s; %zu KiB kept for module text\n", &confine_base, &end, (unsigned long long)(confine_size >> 20), - enforce ? "enforcing" : "reporting"); + enforce ? "enforcing" : "reporting", confine_reserve >> 10); } return 0; } @@ -135,6 +148,7 @@ void *execmem_confine_alloc(struct execmem_range *range, size_t size, { struct confine_chunk *ck; struct vm_struct *area; + bool bounded = range->flags & EXECMEM_CONFINE_BOUNDED; struct page **pages; unsigned long vaddr; unsigned int i, nr; @@ -176,22 +190,29 @@ void *execmem_confine_alloc(struct execmem_range *range, size_t size, * Draw the backing a page at a time. The pages are vmapped one by one, * so they need not be physically contiguous; a single gen_pool_alloc() * of the whole size could fail on a fragmented window with free pages - * to spare. + * to spare. The lock is held across the loop so that the reserve check + * below still holds when the last page is taken. */ + mutex_lock(&confine_lock); + if (bounded && gen_pool_avail(confine_pool) < size + confine_reserve) { + mutex_unlock(&confine_lock); + pr_warn_ratelimited("%zu bytes refused: %zu KiB of the window is kept for module text\n", + size, confine_reserve >> 10); + goto err_ck; + } for (i = 0; i < nr; i++) { - unsigned long page_va; + unsigned long page_va = gen_pool_alloc(confine_pool, PAGE_SIZE); - mutex_lock(&confine_lock); - page_va = gen_pool_alloc(confine_pool, PAGE_SIZE); - mutex_unlock(&confine_lock); if (!page_va) { + __confine_pool_free(pages, i); + mutex_unlock(&confine_lock); pr_warn_ratelimited("window exhausted, cannot satisfy %zu bytes\n", size); - confine_pool_free(pages, i); goto err_ck; } pages[i] = virt_to_page((void *)page_va); } + mutex_unlock(&confine_lock); vaddr = (unsigned long)area->addr; err = vmap_pages_range(vaddr, vaddr + size, pgprot, pages, PAGE_SHIFT);