Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
362a6d4
mm/execmem: confine executable allocations to a fixed physical window
DjDeveloperr Nov 5, 2026
5a4f741
arm64: confine kernel executable code to a CTXR physical window
DjDeveloperr Nov 5, 2026
0a325c1
arm64: ctxr-execmem: keep reserved memory out of the pool, fit the wi…
DjDeveloperr Nov 5, 2026
f7aeef2
arm64: ctxr-execmem: default to a 128 MiB window
DjDeveloperr Nov 5, 2026
3b76807
Merge integration/aurora into feat/ctxr-execmem
DjDeveloperr Nov 6, 2026
72880a4
arm64: stay at EL2 on entry when a CTXR window is armed
DjDeveloperr Nov 5, 2026
73b99c1
arm64: keep Apple cores at EL2 on every entry under CTXR
DjDeveloperr Nov 5, 2026
5863e24
arm64: configs: asahi: add what an installed system needs
DjDeveloperr Nov 5, 2026
207bc4d
arm64: vdso: keep the vDSO out of the CTXR window
DjDeveloperr Nov 5, 2026
4b630f7
mm/execmem: keep window pages out of the page allocator
DjDeveloperr Nov 5, 2026
fe8741d
arm64: keep M1 to M3 on the usual entry path under CTXR
DjDeveloperr Nov 6, 2026
0412b23
arm64: vdso: copy the vDSO only when a window is active
DjDeveloperr Nov 6, 2026
d9f915b
arm64: ctxr-execmem: turn EFI runtime services off if their code can'…
DjDeveloperr Nov 6, 2026
a64c54e
arm64: mm: kill a task whose fetch the CTXR window refuses
DjDeveloperr Nov 6, 2026
0ab88ef
arm64: kexec: refuse kexec and crash kernels under an enforcing CTXR …
DjDeveloperr Nov 6, 2026
2e6e700
arm64: ctxr-execmem: name the bootloader and describe the entry path
DjDeveloperr Nov 6, 2026
d9de654
arm64: ctxr-execmem: parse the command line with early_param()
DjDeveloperr Nov 6, 2026
d637de6
mm/execmem: keep CMA and failed reserved-memory out of the window
DjDeveloperr Nov 6, 2026
b3a9578
mm/execmem: tell KASAN and kmemleak about confined allocations
DjDeveloperr Nov 6, 2026
524f911
dt-bindings: chosen: add the CTXR executable window
DjDeveloperr Nov 6, 2026
c4b5fab
arm64: ctxr-execmem: document the options and what a window gives up
DjDeveloperr Nov 6, 2026
1c4ce70
arm64: ctxr-execmem: treat an active window as confining, enforcing o…
RyanTheTide Nov 6, 2026
20519af
arm64: mm: keep the zero page out of an active CTXR window
RyanTheTide Nov 6, 2026
a572073
arm64: head: take the direct VHE entry only when VHE is implemented
RyanTheTide Nov 6, 2026
e82a851
mm/execmem: flush before a confined chunk goes back to the pool
RyanTheTide Nov 6, 2026
46c5ae7
arm64: ctxr-execmem: keep the boot CPU on the usual entry path
DjDeveloperr Nov 6, 2026
91c85e7
arm64: ctxr-execmem: keep secondaries offline if the window can't be …
DjDeveloperr Nov 6, 2026
6ebbafe
arm64: errata: apply the Apple vGIC (broken SEIS) workaround to T8140
DjDeveloperr Nov 6, 2026
ddb50b4
cpuidle: apple: offer CPU PD only on M1 to M3
DjDeveloperr Nov 6, 2026
131e02f
Merge integration/aurora into feat/ctxr-execmem
DjDeveloperr Nov 6, 2026
5225232
arm64: ctxr-execmem: check the window for pool space before SMP
DjDeveloperr Nov 6, 2026
a304357
selftests: apple-dcp: replay the cpuidle probe against the M1 to M3 a…
DjDeveloperr Nov 6, 2026
51301b0
Documentation: arm64: ctxr-execmem: drop a wrong reason for no CTXR_D…
DjDeveloperr Nov 6, 2026
65da347
arm64: head: drop the VH check from the CTXR direct entry
DjDeveloperr Nov 6, 2026
c89843a
arm64: ctxr-execmem: drop aurora,ctxr-exec-enforce and windows withou…
DjDeveloperr Nov 6, 2026
d1eba7c
mm/execmem: drop the uncalled confinement accessors
DjDeveloperr Nov 6, 2026
9657b6e
mm/execmem: drop the confinement selftests and the debugfs sweep
DjDeveloperr Nov 6, 2026
0941e6f
arm64: configs: asahi: keep only the CTXR option
DjDeveloperr Nov 6, 2026
a1a2ac0
Merge integration/aurora into feat/ctxr-execmem
DjDeveloperr Nov 6, 2026
22420ee
arm64: ctxr-execmem: never read the entry flag on the boot CPU
DjDeveloperr Nov 6, 2026
13c7553
arm64: ctxr-execmem: run a command-line window through the same checks
DjDeveloperr Nov 6, 2026
59a218c
arm64: mm: check PFN-mapped window pages in the EL0 fetch backstop
DjDeveloperr Nov 6, 2026
7f54ed9
arm64: ctxr-execmem: re-reserve the window before the crash kernel
DjDeveloperr Nov 6, 2026
c2f9d0c
mm/cma: check the window overlap before allocating the bitmap
DjDeveloperr Nov 7, 2026
344bd8f
mm/execmem: back a confined allocation one window page at a time
DjDeveloperr Nov 7, 2026
8eb1ce3
arm64: vdso: free the copied pages if __vdso_init() fails
DjDeveloperr Nov 7, 2026
9478b36
arm64: ctxr-execmem: correct who places the image and what the pool h…
DjDeveloperr Nov 7, 2026
f6b1cbf
cpuidle: apple: reuse the M1-to-M3 list in the load check
DjDeveloperr Nov 7, 2026
6315aea
arm64: bpf: size the JIT limit from the CTXR pool
RyanTheTide Nov 7, 2026
273b9e8
mm/execmem: keep a quarter of the confined pool for module text
RyanTheTide Nov 7, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Documentation/admin-guide/kernel-parameters.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,24 @@ Kernel parameters
option depends on the CSD_LOCK_WAIT_DEBUG_DEFAULT
Kconfig option.

ctxr_exec_enforce=
[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: <bool>
Default: 1

ctxr_exec_window=
[ARM64,EARLY] Confine kernel executable code to a
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: <size>[KMG]@<base>[KMG]

dasd= [HW,NET]
See header of drivers/s390/block/dasd_devmap.c.

Expand Down
155 changes: 155 additions & 0 deletions Documentation/arch/arm64/ctxr-execmem.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
.. 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 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
it.

Supplying the window
====================

Device tree (production)
The bootloader emits, under ``/chosen``::

aurora,ctxr-exec-window = <base_hi base_lo size_hi size_lo>;

``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=<size>@<base> e.g. ctxr_exec_window=128M@0x40200000
ctxr_exec_enforce=<bool> (default: enforcing)

``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 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
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.

``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
============

* ``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 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 —
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.
* 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
=========

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 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.
* 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. 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 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. 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
====================

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
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.

What a confined machine gives up
================================

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 active, ``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
=====================

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.
1 change: 1 addition & 0 deletions Documentation/arch/arm64/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ ARM64 Architecture
booting
cpu-feature-registers
cpu-hotplug
ctxr-execmem
elf_hwcaps
gcs
hugetlbpage
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# 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 <hey@djdev.me>

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, 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
...
1 change: 1 addition & 0 deletions MAINTAINERS
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions arch/arm64/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2535,6 +2535,31 @@ 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 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.
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)

Expand Down
5 changes: 5 additions & 0 deletions arch/arm64/configs/asahi.config
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,8 @@ CONFIG_DMABUF_HEAPS_SYSTEM=y
CONFIG_UDMABUF=y
CONFIG_DETECT_HUNG_TASK=y
CONFIG_EDAC_APPLE_SOC=m
# 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 bootloader supplies a window (/chosen/aurora,ctxr-exec-window) or
# the ctxr_exec_window= command-line option is given.
CONFIG_ARM64_CTXR_EXECMEM=y
63 changes: 63 additions & 0 deletions arch/arm64/include/asm/ctxr_execmem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* 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 <linux/types.h>

#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
* once from arm64_memblock_init(), while memblock is still authoritative.
*/
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
* confinement is active and the caller should flag its executable ranges with
* EXECMEM_CONFINE.
*/
bool ctxr_exec_execmem_setup(void);

/*
* 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);

/*
* 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);

/*
* 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
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; }
static inline u64 ctxr_exec_pool_bytes(void) { return 0; }
#endif

#endif /* __ASM_CTXR_EXECMEM_H */
2 changes: 2 additions & 0 deletions arch/arm64/kernel/cpu_errata.c
Original file line number Diff line number Diff line change
Expand Up @@ -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),
{},
};

Expand Down
6 changes: 6 additions & 0 deletions arch/arm64/kernel/efi.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <linux/screen_info.h>
#include <linux/vmalloc.h>

#include <asm/ctxr_execmem.h>
#include <asm/efi.h>
#include <asm/stacktrace.h>
#include <asm/vmap_stack.h>
Expand Down Expand Up @@ -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");
Expand Down
Loading
Loading