From 4e660ed36755a2a7e0e2bbe682aa6a4333320aeb Mon Sep 17 00:00:00 2001 From: Jeff Queisser Date: Mon, 23 Nov 2026 01:43:00 -0800 Subject: [PATCH 1/3] kboot: keep an FDT node off without needing space, so a boot loader step can refuse a coprocessor even in a full tree Disabling a node rewrites its status from "okay" to "disabled", which grows the tree. When a step must refuse a coprocessor because the tree is out of space for what it would add, that edit fails for the same reason, and the step can only stop the boot. Add fdt_disable_node(): it sets the status to "disabled"; if that cannot grow the tree, it re-reads status and overwrites an "okay" status in place with "fail" (the same length; the devicetree specification's not-operational status, which Linux neither probes nor treats as available); otherwise it turns the whole node (including its status, phandle and children) into NOPs with fdt_nop_node(), the same zero-space fallback kboot_gpu_m4.c already uses, so no driver probes it and no phandle consumer sees it as available. Neither fallback needs space. tests/run-fdt-disable.sh checks it on the host and runs in CI: with room, and with no free space at all on an "okay", an "ok" and a status-less node with phandles and a child node. Signed-off-by: Jeff Queisser --- .github/workflows/test.yml | 4 ++ Makefile | 1 + src/fdt_disable.c | 27 +++++++ src/fdt_disable.h | 8 +++ tests/fdt_disable_host.c | 141 +++++++++++++++++++++++++++++++++++++ tests/run-fdt-disable.sh | 16 +++++ 6 files changed, 197 insertions(+) create mode 100644 src/fdt_disable.c create mode 100644 src/fdt_disable.h create mode 100644 tests/fdt_disable_host.c create mode 100755 tests/run-fdt-disable.sh diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3f8bf7980..dee6544ce 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -69,6 +69,10 @@ jobs: - name: Test PMP boot data fixups run: sh tests/run-pmp-boot-data.sh + - name: Test keeping an FDT node off with no free space + run: sh tests/run-fdt-disable.sh + + - name: Run pytest with clang if: matrix.os == 'ubuntu-latest' && matrix.compiler == 'llvm' env: diff --git a/Makefile b/Makefile index 69124e40f..e593107d7 100644 --- a/Makefile +++ b/Makefile @@ -257,6 +257,7 @@ OBJECTS := \ dockchannel_uart.o \ exception.o exception_asm.o \ fb.o font.o font_retina.o \ + fdt_disable.o \ firmware.o \ gxf.o gxf_asm.o \ heapblock.o \ diff --git a/src/fdt_disable.c b/src/fdt_disable.c new file mode 100644 index 000000000..60e87bf30 --- /dev/null +++ b/src/fdt_disable.c @@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: MIT */ + +#include "fdt_disable.h" +#include "string.h" + +#include "libfdt/libfdt.h" + +/* + * Keep @node from being probed or seen as available by phandle consumers, whatever space the tree + * has left: set its status to "disabled"; if that cannot grow the tree, re-read status and + * overwrite "okay" in place with "fail" (the same length: not operational); otherwise turn the + * whole node (including its status, phandle and children) into NOPs, as kboot_gpu_m4.c already + * does. Neither fallback needs space. Returns 0, or a negative libfdt error with the tree + * unchanged. + */ +int fdt_disable_node(void *fdt, int node) +{ + int len; + const char *status; + + if (!fdt_setprop_string(fdt, node, "status", "disabled")) + return 0; + status = fdt_getprop(fdt, node, "status", &len); + if (status && len == sizeof("okay") && !memcmp(status, "okay", len)) + return fdt_setprop_inplace(fdt, node, "status", "fail", sizeof("fail")); + return fdt_nop_node(fdt, node); +} diff --git a/src/fdt_disable.h b/src/fdt_disable.h new file mode 100644 index 000000000..61bd3368b --- /dev/null +++ b/src/fdt_disable.h @@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: MIT */ + +#ifndef __FDT_DISABLE_H__ +#define __FDT_DISABLE_H__ + +int fdt_disable_node(void *fdt, int node); + +#endif diff --git a/tests/fdt_disable_host.c b/tests/fdt_disable_host.c new file mode 100644 index 000000000..bfc35865f --- /dev/null +++ b/tests/fdt_disable_host.c @@ -0,0 +1,141 @@ +/* SPDX-License-Identifier: MIT */ +/* + * Host check of fdt_disable_node() (src/fdt_disable.c) with libfdt: one tree with room and one + * packed with no free space at all are built with libfdt's sequential writer, and each case runs on + * a fresh copy of one. + * + * src/fdt_disable.c is built with -Dfdt_setprop=fdt_setprop_test (tests/run-fdt-disable.sh), so its + * status edit can be made to fail on a tree that has room. + */ +#include +#include + +#include "fdt_disable.h" + +#include "libfdt/libfdt.h" + +#define TREE_SIZE 4096 + +static int fail_status; + +int fdt_setprop_test(void *fdt, int node, const char *name, const void *val, int len) +{ + if (fail_status) + return -FDT_ERR_NOSPACE; + return fdt_setprop(fdt, node, name, val, len); +} + +static void node(void *fdt, const char *name, const char *status, uint32_t phandle) +{ + fdt_begin_node(fdt, name); + fdt_property_string(fdt, "compatible", "test,node"); + fdt_property_u32(fdt, "phandle", phandle); + if (status) + fdt_property_string(fdt, "status", status); + fdt_property_u32(fdt, "reg", 1); + fdt_begin_node(fdt, "audio"); + fdt_property_u32(fdt, "phandle", phandle + 100); + fdt_end_node(fdt); + fdt_end_node(fdt); +} + +/* Three enabled AOPs, whose status is "okay", "ok" or absent; packed: no free space */ +static int build(void *fdt) +{ + fdt_create(fdt, TREE_SIZE); + fdt_finish_reservemap(fdt); + fdt_begin_node(fdt, ""); + node(fdt, "okay@1", "okay", 11); + node(fdt, "ok@2", "ok", 22); + node(fdt, "none@3", NULL, 33); + fdt_end_node(fdt); + fdt_finish(fdt); + return fdt_totalsize(fdt); +} + +static int failures; + +static void check(int ok, const char *what) +{ + printf("%s %s\n", ok ? "ok" : "FAIL", what); + failures += !ok; +} + +static const char *prop(const void *fdt, const char *path, const char *name) +{ + int off = fdt_path_offset(fdt, path); + if (off < 0) + return ""; + const char *s = fdt_getprop(fdt, off, name, NULL); + return s ? s : "-"; +} + +int main(void) +{ + static char packed[TREE_SIZE], roomy[TREE_SIZE], fdt[TREE_SIZE]; + int size = build(packed), ret; + + memcpy(roomy, packed, TREE_SIZE); + fdt_open_into(roomy, roomy, TREE_SIZE); + check(!fdt_check_full(packed, size) && !fdt_check_full(roomy, TREE_SIZE), + "the test trees are valid"); + + static const char *const nodes[] = {"/okay@1", "/ok@2", "/none@3"}; + for (int i = 0; i < 3; i++) { + char what[96]; + memcpy(fdt, roomy, TREE_SIZE); + ret = fdt_disable_node(fdt, fdt_path_offset(fdt, nodes[i])); + snprintf(what, sizeof(what), "with room, %s: status \"disabled\", compatible kept", + nodes[i]); + check(!ret && !strcmp(prop(fdt, nodes[i], "status"), "disabled") && + !strcmp(prop(fdt, nodes[i], "compatible"), "test,node") && + !fdt_check_full(fdt, TREE_SIZE), + what); + } + + memcpy(fdt, packed, TREE_SIZE); + ret = fdt_disable_node(fdt, fdt_path_offset(fdt, "/okay@1")); + check(!ret && !strcmp(prop(fdt, "/okay@1", "status"), "fail") && + fdt_size_dt_struct(fdt) == fdt_size_dt_struct(packed) && !fdt_check_full(fdt, size), + "no free space, \"okay\": \"fail\" in place, the tree the same size"); + + memcpy(fdt, packed, TREE_SIZE); + ret = fdt_disable_node(fdt, fdt_path_offset(fdt, "/ok@2")); + check(!ret && fdt_path_offset(fdt, "/ok@2") < 0 && fdt_path_offset(fdt, "/ok@2/audio") < 0 && + fdt_node_offset_by_phandle(fdt, 22) < 0 && fdt_node_offset_by_phandle(fdt, 122) < 0 && + fdt_path_offset(fdt, "/okay@1") >= 0 && fdt_path_offset(fdt, "/none@3") >= 0 && + fdt_size_dt_struct(fdt) == fdt_size_dt_struct(packed) && !fdt_check_full(fdt, size), + "no free space, \"ok\": whole node, child and phandles NOP'd, siblings kept"); + + memcpy(fdt, packed, TREE_SIZE); + ret = fdt_disable_node(fdt, fdt_path_offset(fdt, "/none@3")); + check(!ret && fdt_path_offset(fdt, "/none@3") < 0 && + fdt_path_offset(fdt, "/none@3/audio") < 0 && + fdt_node_offset_by_phandle(fdt, 33) < 0 && fdt_node_offset_by_phandle(fdt, 133) < 0 && + fdt_path_offset(fdt, "/okay@1") >= 0 && fdt_path_offset(fdt, "/ok@2") >= 0 && + fdt_size_dt_struct(fdt) == fdt_size_dt_struct(packed) && !fdt_check_full(fdt, size), + "no free space, no status: whole node, child and phandles NOP'd, siblings kept"); + + memcpy(fdt, roomy, TREE_SIZE); + fail_status = 1; + ret = fdt_disable_node(fdt, fdt_path_offset(fdt, "/okay@1")); + fail_status = 0; + check(!ret && !strcmp(prop(fdt, "/okay@1", "status"), "fail") && + !fdt_check_full(fdt, TREE_SIZE), + "the status edit fails on \"okay\": \"fail\" in place"); + + memcpy(fdt, roomy, TREE_SIZE); + fail_status = 1; + ret = fdt_disable_node(fdt, fdt_path_offset(fdt, "/ok@2")); + fail_status = 0; + check(!ret && fdt_path_offset(fdt, "/ok@2") < 0 && fdt_node_offset_by_phandle(fdt, 22) < 0 && + fdt_path_offset(fdt, "/okay@1") >= 0 && !fdt_check_full(fdt, TREE_SIZE), + "the status edit fails on \"ok\": whole node NOP'd"); + + memcpy(fdt, packed, TREE_SIZE); + ret = fdt_disable_node(fdt, -FDT_ERR_NOTFOUND); + check(ret < 0 && !memcmp(fdt, packed, size), "no node: an error, the tree unchanged"); + + printf(failures ? "fdt disable: %d FAILED\n" : "fdt disable: all cases pass\n", failures); + return !!failures; +} diff --git a/tests/run-fdt-disable.sh b/tests/run-fdt-disable.sh new file mode 100755 index 000000000..875920cb3 --- /dev/null +++ b/tests/run-fdt-disable.sh @@ -0,0 +1,16 @@ +#!/bin/sh +# Host check of fdt_disable_node() (src/fdt_disable.c): keeps a node from being probed, with room in +# the tree and with no free space at all. +# +# sh tests/run-fdt-disable.sh +set -eu +cd "$(dirname "$0")/.." +tmp=$(mktemp -d) +trap 'rm -rf "$tmp"' EXIT HUP INT TERM +cflags="-std=gnu11 -O1 -g -Wall -Wextra -fsanitize=address,undefined -fno-omit-frame-pointer + -fno-sanitize-recover=undefined -Isrc" +${CC:-cc} $cflags -Dfdt_setprop=fdt_setprop_test -c src/fdt_disable.c -o "$tmp/fdt_disable.o" +${CC:-cc} $cflags tests/fdt_disable_host.c "$tmp/fdt_disable.o" src/libfdt/fdt.c src/libfdt/fdt_ro.c \ + src/libfdt/fdt_rw.c src/libfdt/fdt_wip.c src/libfdt/fdt_sw.c src/libfdt/fdt_strerror.c \ + src/libfdt/fdt_check.c -o "$tmp/fdt-disable-host" +ASAN_OPTIONS=${ASAN_OPTIONS:-detect_leaks=0} "$tmp/fdt-disable-host" From b21dcdadd937998442643d85a3ec275ab44d1095 Mon Sep 17 00:00:00 2001 From: Jeff Queisser Date: Mon, 23 Nov 2026 01:43:00 -0800 Subject: [PATCH 2/3] kboot: keep an AOP's firmware pages in the OS's memory reserved when its DART is locked, so the OS cannot reuse the log the firmware writes Since e41005db the AOP firmware-segment path adds nothing for a locked AOP DART: Linux keeps the firmware's own mappings there, and adding the segments' device addresses would make its default domain fail with EBUSY. That also leaves the RAM under any segment inside the OS's memory unreserved. On the Mac Studio (J775D) the AOP's log segment lies there, at an address that changes from boot to boot, and the firmware writes it once the OS starts the AOP. On a locked DART, reserve each segment that lies in the OS's memory as RAM only (no-map, no device address, not in the AOP's memory-region), under the node names the segment reservation uses. Each segment is checked for u64 wrap before rounding out to 16 KiB pages (as the T8152 display path does) and checked against the ADT's DRAM bounds; a segment list longer than 64 entries is refused. Reservations go through a non-bailing helper, dt_try_reserve_ram_mem(), instead of dt_get_or_add_reserved_mem() (which calls bail() on FDT errors and overwrites same-name nodes): an existing no-map reservation that already covers the range is kept untouched, any conflicting or non-no-map overlap fails closed, and a mid-creation out-of-space error removes the partial subnode with fdt_nop_node(). If reservation fails (including when the tree is out of space), a halted AOP is kept off with fdt_disable_node(), which needs no space: the OS then never starts it, and its firmware never writes those pages, so the boot goes on. A running AOP writes them whatever the tree says, and a disabled node would not stop it, so then the boot stops rather than hand the OS pages the firmware is writing. The pages come from the ADT's phys field: on the J775D the locked tables do not map the log at handoff, so no translation describes it. On the J775D the AOP waits halted at handoff, which is the case fdt_disable_node() covers. tests/run-aop-os-ram.sh checks the production reservation and failure paths from src/kboot.c against src/fdt_disable.c and libfdt on the host and runs in CI (aligned and unaligned 16 KiB rounding, wrap guards, count cap, DRAM bounds, existing no-map coverage vs overlap conflicts, and packed/tight FDT handling for halted and running AOPs). Nothing changes for an unlocked DART or for a segment outside the OS's memory. The locked-DART path runs wherever the AOP's DART is locked at handoff: the J700 (T8140), whose AOP DART probe failed with EBUSY before e41005db, and the T6050 boards that boot their AOP. Signed-off-by: Jeff Queisser --- .github/workflows/test.yml | 2 + src/kboot.c | 181 +++++++++++++++- tests/aop_os_ram_host.c | 409 +++++++++++++++++++++++++++++++++++++ tests/run-aop-os-ram.sh | 24 +++ 4 files changed, 611 insertions(+), 5 deletions(-) create mode 100644 tests/aop_os_ram_host.c create mode 100755 tests/run-aop-os-ram.sh diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index dee6544ce..3d7d7c0f4 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -72,6 +72,8 @@ jobs: - name: Test keeping an FDT node off with no free space run: sh tests/run-fdt-disable.sh + - name: Test locked-DART AOP OS-RAM reservation and failure handling + run: sh tests/run-aop-os-ram.sh - name: Run pytest with clang if: matrix.os == 'ubuntu-latest' && matrix.compiler == 'llvm' diff --git a/src/kboot.c b/src/kboot.c index b8a412948..d46719c23 100644 --- a/src/kboot.c +++ b/src/kboot.c @@ -17,6 +17,7 @@ #include "display.h" #include "dockchannel_uart.h" #include "exception.h" +#include "fdt_disable.h" #include "firmware.h" #include "heapblock.h" #include "iodev.h" @@ -10081,6 +10082,172 @@ static int dt_find_aop(char *path, size_t size) return -1; } +/* + * Reserve [paddr, paddr + size) as a no-map RAM-only node under /reserved-memory without calling + * bail() on error, so the caller can decide whether a failure disables a halted coprocessor or + * stops the boot. Returns 1 when an existing no-map reservation already covers the range (leaving + * it untouched), 0 when a new node was added, or -1 on a conflicting/overlapping reservation or an + * FDT error (removing any half-built node with fdt_nop_node()). + */ +static int dt_try_reserve_ram_mem(const char *node_name, const char *compat, u64 paddr, u64 size) +{ + int resv_node = fdt_path_offset(dt, "/reserved-memory"); + if (resv_node < 0 || !size || paddr > UINT64_MAX - size) + return -1; + + u64 end = paddr + size; + bool covered = false; + int sub; + fdt_for_each_subnode(sub, dt, resv_node) + { + const char *sname = fdt_get_name(dt, sub, NULL); + bool same_name = sname && !strcmp(sname, node_name); + int rlen = 0; + const fdt64_t *reg = fdt_getprop(dt, sub, "reg", &rlen); + + if (!reg || rlen != 16) { + if (same_name) { + printf("FDT: /reserved-memory/%s already exists without a valid reg\n", node_name); + return -1; + } + continue; + } + u64 base = fdt64_ld(®[0]), count = fdt64_ld(®[1]); + if (!count || base > UINT64_MAX - count) { + if (same_name) { + printf("FDT: /reserved-memory/%s has an invalid reg\n", node_name); + return -1; + } + continue; + } + u64 send = base + count; + if (paddr >= send || end <= base) { + if (same_name) { + printf("FDT: /reserved-memory/%s already exists for [0x%lx, +0x%lx)\n", node_name, + base, count); + return -1; + } + continue; + } + if (fdt_getprop(dt, sub, "no-map", NULL) && paddr >= base && end <= send) { + covered = true; + continue; + } + printf("FDT: [0x%lx, +0x%lx) conflicts with /reserved-memory/%s [0x%lx, +0x%lx)\n", paddr, + size, sname ? sname : "?", base, count); + return -1; + } + if (covered) + return 1; + + int node = fdt_add_subnode(dt, resv_node, node_name); + if (node < 0) + return -1; + + uint32_t phandle; + u64 reg[2] = {cpu_to_fdt64(paddr), cpu_to_fdt64(size)}; + if (fdt_generate_phandle(dt, &phandle) || fdt_setprop_u32(dt, node, "phandle", phandle) || + fdt_setprop(dt, node, "reg", reg, sizeof(reg)) || + fdt_setprop_string(dt, node, "compatible", compat) || + fdt_setprop_empty(dt, node, "no-map")) { + fdt_nop_node(dt, node); + return -1; + } + return 0; +} + +/* + * Reserve, RAM only, every firmware segment of the ADT AOP @anode that lies in the OS's memory. + * On a locked DART the OS keeps the firmware's own mappings, so the segments get no IOVA, but the + * pages under them must not be reused while the AOP uses them (its log, which iBoot places in the + * OS's memory at a new address every boot, and which the locked tables do not map at handoff, so + * the ADT's phys field is its only description). The nodes are named as the segment reservation + * names them. Segments are rounded out to 16K pages; a segment list longer than 64 entries, a + * wrapping address/size, a segment in the OS's memory outside the ADT's DRAM, a conflicting + * /reserved-memory node, or a full tree fails the reservation. + */ +static int dt_reserve_aop_os_ram(int anode, const char *path) +{ + u64 lo = kboot_ram_base(), ram_size = kboot_ram_size(), dram, dram_size; + int chosen = adt_path_offset(adt, "/chosen"); + u32 len = 0; + const struct adt_segment_ranges *seg = adt_getprop(adt, anode, "segment-ranges", &len); + + if (!seg || !len) + return 0; + if (!ram_size || lo > UINT64_MAX - ram_size || len % sizeof(*seg) || len / sizeof(*seg) > 64 || + chosen < 0 || ADT_GETPROP(adt, chosen, "dram-base", &dram) != sizeof(dram) || + ADT_GETPROP(adt, chosen, "dram-size", &dram_size) != sizeof(dram_size) || !dram_size || + dram > UINT64_MAX - dram_size) { + printf("FDT: %s: the ADT's segment list or DRAM bounds are not usable\n", path); + return -1; + } + u64 hi = lo + ram_size, dram_end = dram + dram_size; + + for (u32 i = 0; i < len / sizeof(*seg); i++) { + struct adt_segment_ranges r; + memcpy(&r, &seg[i], sizeof(r)); + if (!r.size) + continue; + if (r.phys > UINT64_MAX - (u64)r.size) { + printf("FDT: %s: firmware segment [0x%lx, +0x%x) wraps\n", path, r.phys, r.size); + return -1; + } + u64 raw_end = r.phys + (u64)r.size; + if (raw_end > UINT64_MAX - (SZ_16K - 1)) { + printf("FDT: %s: firmware segment [0x%lx, +0x%x) wraps on 16K rounding\n", path, r.phys, + r.size); + return -1; + } + u64 start = ALIGN_DOWN(r.phys, SZ_16K); + u64 end = ALIGN_UP(raw_end, SZ_16K); + u64 size = end - start; + if (start >= hi || end <= lo) + continue; + if (start < dram || end > dram_end) { + printf("FDT: %s: firmware segment [0x%lx, +0x%lx) is outside DRAM\n", path, start, + size); + return -1; + } + + char name[64]; + snprintf(name, sizeof(name), "asc-firmware@%lx", start); + int ret = dt_try_reserve_ram_mem(name, "apple,asc-mem", start, size); + if (ret < 0) + return -1; + if (ret > 0) + printf("FDT: %s: firmware segment [0x%lx, +0x%lx) is already reserved no-map\n", path, + start, size); + else + printf("FDT: %s: firmware segment [0x%lx, +0x%lx) is in the OS's memory: reserved, RAM " + "only\n", + path, start, size); + } + return 0; +} + +static int dt_aop_locked_dart_handoff(int anode, const char *path) +{ + printf("FDT: %s: preserving firmware mappings in locked DART\n", path); + if (!dt_reserve_aop_os_ram(anode, path)) + return 0; + + asc_dev_t *asc = asc_init("/arm-io/aop"); + bool running = !asc || asc_cpu_running(asc); + if (asc) + asc_free(asc); + if (running) + bail("FDT: %s: the AOP is running and its firmware RAM cannot be reserved\n", path); + + char found_path[64]; + int node = dt_find_aop(found_path, sizeof(found_path)); + /* fdt_disable_node() needs no space: this cannot fail on the node just found */ + if (node < 0 || fdt_disable_node(dt, node)) + bail("FDT: %s: cannot keep the AOP off\n", path); + printf("FDT: %s: its firmware RAM could not be reserved: AOP kept off\n", path); + return 0; +} + /* * The AOP's accelerometer and gyroscope services wait for their calibration, which the ADT * carries on every AOP. iBoot starts the AOP firmware and leaves the part that does not run @@ -10118,13 +10285,17 @@ static int dt_set_aop_firmware(void) return 0; } - /* Linux keeps the firmware roots when this DART's TTBR/TCR lock is set. */ + /* + * Linux keeps the firmware roots when this DART's TTBR/TCR lock is set. The RAM under any + * segment in the OS's memory is still reserved; without that the firmware would write into + * pages the OS reuses. If the reservation fails, a halted AOP is kept off, which needs no + * space (fdt_disable_node()): the OS then never starts it, and its firmware never writes + * those pages. A running AOP writes them whatever the tree says, so then no boot is safe. + */ u64 dart_base, dart_size; if (!dt_device_dart_mmio(node, anode, &dart_base, &dart_size) && - dart_t8110_is_locked(dart_base, dart_size)) { - printf("FDT: %s: preserving firmware mappings in locked DART\n", path); - return 0; - } + dart_t8110_is_locked(dart_base, dart_size)) + return dt_aop_locked_dart_handoff(anode, path); if (dt_reserve_asc_segments("/arm-io/aop", NULL, path, true, 0, true)) printf("FDT: %s: reserving the AOP firmware segments failed\n", path); diff --git a/tests/aop_os_ram_host.c b/tests/aop_os_ram_host.c new file mode 100644 index 000000000..9e6ce0780 --- /dev/null +++ b/tests/aop_os_ram_host.c @@ -0,0 +1,409 @@ +/* SPDX-License-Identifier: MIT */ +/* + * Host check of the locked-DART AOP OS-RAM reservation and failure handling extracted directly from + * src/kboot.c (dt_find_aop, dt_try_reserve_ram_mem, dt_reserve_aop_os_ram and + * dt_aop_locked_dart_handoff) against real src/fdt_disable.c and libfdt. + */ +#include +#include +#include +#include +#include + +#include "fdt_disable.h" + +#include "libfdt/libfdt.h" + +typedef uint8_t u8; +typedef uint32_t u32; +typedef unsigned long u64; + +#define SZ_16K 0x4000UL +#define ALIGN_DOWN(x, a) ((x) & ~((a) - 1)) +#define ALIGN_UP(x, a) (((x) + ((a) - 1)) & ~((a) - 1)) +#define PACKED __attribute__((packed)) + +struct adt_segment_ranges { + u64 phys; + u64 iova; + u64 remap; + u32 size; + u32 unk; +} PACKED; + +typedef struct asc_dev { + int dummy; +} asc_dev_t; + +static int bailed; +#define bail(...) \ + do { \ + bailed++; \ + printf("BAIL: " __VA_ARGS__); \ + return -1; \ + } while (0) + +static void *dt; +static void *adt = (void *)1; + +static u64 test_ram_base, test_ram_size; +static u64 test_dram_base, test_dram_size; +static bool test_chosen_ok; +static const void *test_segs; +static u32 test_segs_len; + +static asc_dev_t test_asc; +static bool test_asc_ok; +static bool test_asc_running; + +static u64 kboot_ram_base(void) +{ + return test_ram_base; +} + +static u64 kboot_ram_size(void) +{ + return test_ram_size; +} + +static int adt_path_offset(const void *tree, const char *path) +{ + (void)tree; + if (!strcmp(path, "/chosen")) + return test_chosen_ok ? 1 : -1; + return -1; +} + +static const void *adt_getprop(const void *tree, int node, const char *name, u32 *len) +{ + (void)tree; + if (node == 2 && !strcmp(name, "segment-ranges")) { + if (len) + *len = test_segs_len; + return test_segs; + } + if (len) + *len = 0; + return NULL; +} + +static int adt_getprop_copy(const void *tree, int node, const char *name, void *out, size_t len) +{ + (void)tree; + if (node == 1 && !strcmp(name, "dram-base") && len == sizeof(u64)) { + memcpy(out, &test_dram_base, sizeof(u64)); + return (int)sizeof(u64); + } + if (node == 1 && !strcmp(name, "dram-size") && len == sizeof(u64)) { + memcpy(out, &test_dram_size, sizeof(u64)); + return (int)sizeof(u64); + } + return -1; +} + +#define ADT_GETPROP(tree, node, name, val) adt_getprop_copy(tree, node, name, (val), sizeof(*(val))) + +static asc_dev_t *asc_init(const char *path) +{ + assert(!strcmp(path, "/arm-io/aop")); + return test_asc_ok ? &test_asc : NULL; +} + +static bool asc_cpu_running(asc_dev_t *asc) +{ + assert(asc == &test_asc); + return test_asc_running; +} + +static void asc_free(asc_dev_t *asc) +{ + assert(asc == &test_asc); +} + +/* SOURCE FUNCTIONS */ + +#define TREE_SIZE 8192 + +static int failures; + +static void check(int ok, const char *what) +{ + printf("%s %s\n", ok ? "ok" : "FAIL", what); + failures += !ok; +} + +static int build_tree(void *buf, size_t cap, const char *aop_status, bool pack_tree) +{ + assert(!fdt_create(buf, (int)cap)); + assert(!fdt_finish_reservemap(buf)); + assert(!fdt_begin_node(buf, "")); + assert(!fdt_property_u32(buf, "#address-cells", 2)); + assert(!fdt_property_u32(buf, "#size-cells", 2)); + + assert(!fdt_begin_node(buf, "reserved-memory")); + assert(!fdt_property_u32(buf, "#address-cells", 2)); + assert(!fdt_property_u32(buf, "#size-cells", 2)); + assert(!fdt_property(buf, "ranges", NULL, 0)); + assert(!fdt_end_node(buf)); + + assert(!fdt_begin_node(buf, "soc")); + assert(!fdt_begin_node(buf, "aop@290c00000")); + assert(!fdt_property_string(buf, "compatible", "apple,t6050-aop")); + assert(!fdt_property_u32(buf, "phandle", 42)); + if (aop_status) + assert(!fdt_property_string(buf, "status", aop_status)); + assert(!fdt_begin_node(buf, "audio")); + assert(!fdt_property_u32(buf, "phandle", 43)); + assert(!fdt_end_node(buf)); + assert(!fdt_end_node(buf)); + assert(!fdt_end_node(buf)); + + assert(!fdt_end_node(buf)); + assert(!fdt_finish(buf)); + + int packed_size = fdt_totalsize(buf); + if (!pack_tree) + assert(!fdt_open_into(buf, buf, (int)cap)); + dt = buf; + return packed_size; +} + +static void reset_env(const void *segs, u32 len) +{ + bailed = 0; + test_dram_base = 0x10000000000ULL; + test_dram_size = 0x01800000000ULL; /* 96 GiB */ + test_ram_base = 0x10002000000ULL; + test_ram_size = 0x017fe000000ULL; + test_chosen_ok = true; + test_segs = segs; + test_segs_len = len; + test_asc_ok = true; + test_asc_running = false; +} + +static void add_resv_node(const char *name, u64 base, u64 size, bool nomap, bool with_reg) +{ + int resv = fdt_path_offset(dt, "/reserved-memory"); + assert(resv >= 0); + int n = fdt_add_subnode(dt, resv, name); + assert(n >= 0); + if (with_reg) { + u64 reg[2] = {cpu_to_fdt64(base), cpu_to_fdt64(size)}; + assert(!fdt_setprop(dt, n, "reg", reg, sizeof(reg))); + } + assert(!fdt_setprop_string(dt, n, "compatible", "apple,other-mem")); + if (nomap) + assert(!fdt_setprop_empty(dt, n, "no-map")); +} + +static bool has_resv(const char *name, u64 want_base, u64 want_size) +{ + int resv = fdt_path_offset(dt, "/reserved-memory"); + if (resv < 0) + return false; + int n = fdt_subnode_offset(dt, resv, name); + if (n < 0) + return false; + int rlen = 0; + const fdt64_t *reg = fdt_getprop(dt, n, "reg", &rlen); + if (!reg || rlen != 16 || !fdt_getprop(dt, n, "no-map", NULL)) + return false; + return fdt64_ld(®[0]) == want_base && fdt64_ld(®[1]) == want_size; +} + +int main(void) +{ + static char tree[TREE_SIZE]; + + /* 1. Normal aligned OS-RAM log reserved; non-OS-RAM segments skipped */ + { + struct adt_segment_ranges segs[] = { + {.phys = 0x10000ecc000ULL, .size = 0x154000}, + {.phys = 0x1000ff18000ULL, .size = 0x2c000}, + }; + build_tree(tree, TREE_SIZE, "okay", false); + reset_env(segs, sizeof(segs)); + int ret = dt_aop_locked_dart_handoff(2, "/soc/aop@290c00000"); + check(ret == 0 && bailed == 0 && + has_resv("asc-firmware@1000ff18000", 0x1000ff18000ULL, 0x2c000) && + fdt_subnode_offset(dt, fdt_path_offset(dt, "/reserved-memory"), + "asc-firmware@10000ecc000") < 0 && + !fdt_check_full(dt, TREE_SIZE), + "aligned OS-RAM segment reserved no-map, non-OS-RAM skipped"); + } + + /* 2. Unaligned r.phys spanning a 16K boundary rounds down start and rounds up end */ + { + struct adt_segment_ranges segs[] = { + {.phys = 0x1000ff19000ULL, .size = 0x5000}, /* spans 0x1000ff18000..0x1000ff20000 */ + }; + build_tree(tree, TREE_SIZE, "okay", false); + reset_env(segs, sizeof(segs)); + int ret = dt_reserve_aop_os_ram(2, "/soc/aop@290c00000"); + check(ret == 0 && has_resv("asc-firmware@1000ff18000", 0x1000ff18000ULL, 0x8000) && + !fdt_check_full(dt, TREE_SIZE), + "unaligned r.phys spanning 16K boundary reserves both pages"); + } + + /* 3. u64 wrap on r.phys + r.size is refused (never silently skipped) */ + { + struct adt_segment_ranges segs[] = { + {.phys = UINT64_MAX - 0x7fffULL, .size = 0x10000}, + }; + build_tree(tree, TREE_SIZE, "okay", false); + reset_env(segs, sizeof(segs)); + int ret = dt_reserve_aop_os_ram(2, "/soc/aop@290c00000"); + check(ret < 0, "u64 wrap on r.phys + size fails closed"); + } + + /* 4. u64 wrap on 16K ALIGN_UP of end is refused */ + { + struct adt_segment_ranges segs[] = { + {.phys = UINT64_MAX - 0xfffULL, .size = 0xfff}, + }; + build_tree(tree, TREE_SIZE, "okay", false); + reset_env(segs, sizeof(segs)); + int ret = dt_reserve_aop_os_ram(2, "/soc/aop@290c00000"); + check(ret < 0, "u64 wrap on 16K rounding of end fails closed"); + } + + /* 5. Segment count > 64 is refused */ + { + struct adt_segment_ranges segs[65]; + memset(segs, 0, sizeof(segs)); + build_tree(tree, TREE_SIZE, "okay", false); + reset_env(segs, sizeof(segs)); + int ret = dt_reserve_aop_os_ram(2, "/soc/aop@290c00000"); + check(ret < 0, "segment list > 64 entries refused"); + } + + /* 6. Segment in OS RAM outside ADT DRAM is refused */ + { + struct adt_segment_ranges segs[] = { + {.phys = 0x11800000000ULL, .size = 0x4000}, + }; + build_tree(tree, TREE_SIZE, "okay", false); + reset_env(segs, sizeof(segs)); + test_ram_size = 0x01900000000ULL; + int ret = dt_reserve_aop_os_ram(2, "/soc/aop@290c00000"); + check(ret < 0, "OS-RAM segment outside ADT DRAM refused"); + } + + /* 7. Already covered by a larger existing no-map node: succeeds without clobbering */ + { + struct adt_segment_ranges segs[] = { + {.phys = 0x1000ff18000ULL, .size = 0x2c000}, + }; + build_tree(tree, TREE_SIZE, "okay", false); + reset_env(segs, sizeof(segs)); + add_resv_node("other-carveout@1000ff00000", 0x1000ff00000ULL, 0x100000, true, true); + int ret = dt_reserve_aop_os_ram(2, "/soc/aop@290c00000"); + check(ret == 0 && has_resv("other-carveout@1000ff00000", 0x1000ff00000ULL, 0x100000) && + fdt_subnode_offset(dt, fdt_path_offset(dt, "/reserved-memory"), + "asc-firmware@1000ff18000") < 0, + "already covered by existing no-map reservation: untouched, no duplicate"); + } + + /* 8. Partial overlap with existing node: refused and existing node untouched */ + { + struct adt_segment_ranges segs[] = { + {.phys = 0x1000ff18000ULL, .size = 0x2c000}, + }; + build_tree(tree, TREE_SIZE, "okay", false); + reset_env(segs, sizeof(segs)); + add_resv_node("asc-firmware@1000ff18000", 0x1000ff18000ULL, 0x10000, true, true); + int ret = dt_reserve_aop_os_ram(2, "/soc/aop@290c00000"); + check(ret < 0 && has_resv("asc-firmware@1000ff18000", 0x1000ff18000ULL, 0x10000), + "partial overlap / smaller same-name node refused without clobbering"); + } + + /* 9. Overlap with existing non-no-map node: refused and existing node untouched */ + { + struct adt_segment_ranges segs[] = { + {.phys = 0x1000ff18000ULL, .size = 0x2c000}, + }; + build_tree(tree, TREE_SIZE, "okay", false); + reset_env(segs, sizeof(segs)); + add_resv_node("dma-pool@1000ff00000", 0x1000ff00000ULL, 0x100000, false, true); + int ret = dt_reserve_aop_os_ram(2, "/soc/aop@290c00000"); + int n = + fdt_subnode_offset(dt, fdt_path_offset(dt, "/reserved-memory"), "dma-pool@1000ff00000"); + check(ret < 0 && n >= 0 && !fdt_getprop(dt, n, "no-map", NULL), + "overlap with non-no-map reservation refused without clobbering"); + } + + /* 10. Packed FDT + halted AOP ("okay"): fails reservation, sets "fail" in place, no bail */ + { + struct adt_segment_ranges segs[] = { + {.phys = 0x1000ff18000ULL, .size = 0x2c000}, + }; + int sz = build_tree(tree, TREE_SIZE, "okay", true); + reset_env(segs, sizeof(segs)); + int ret = dt_aop_locked_dart_handoff(2, "/soc/aop@290c00000"); + int aop = fdt_path_offset(dt, "/soc/aop@290c00000"); + const char *st = aop >= 0 ? fdt_getprop(dt, aop, "status", NULL) : NULL; + check(ret == 0 && bailed == 0 && st && !strcmp(st, "fail") && !fdt_check_full(dt, sz), + "packed FDT + halted AOP (okay): disabled in place as fail, no bail"); + } + + /* 11. Packed FDT + halted AOP ("ok" / absent status): whole node + child + phandles NOP'd */ + { + struct adt_segment_ranges segs[] = { + {.phys = 0x1000ff18000ULL, .size = 0x2c000}, + }; + int sz = build_tree(tree, TREE_SIZE, "ok", true); + reset_env(segs, sizeof(segs)); + int ret = dt_aop_locked_dart_handoff(2, "/soc/aop@290c00000"); + check(ret == 0 && bailed == 0 && fdt_path_offset(dt, "/soc/aop@290c00000") < 0 && + fdt_node_offset_by_phandle(dt, 42) < 0 && + fdt_node_offset_by_phandle(dt, 43) < 0 && !fdt_check_full(dt, sz), + "packed FDT + halted AOP (ok): node, child and phandles NOP'd, no bail"); + } + + /* 12. Packed FDT + running AOP: calls bail() */ + { + struct adt_segment_ranges segs[] = { + {.phys = 0x1000ff18000ULL, .size = 0x2c000}, + }; + build_tree(tree, TREE_SIZE, "okay", true); + reset_env(segs, sizeof(segs)); + test_asc_running = true; + int ret = dt_aop_locked_dart_handoff(2, "/soc/aop@290c00000"); + check(ret < 0 && bailed == 1, "packed FDT + running AOP: bails"); + } + + /* 13. Packed FDT + asc_init() fails: conservatively treated as running, calls bail() */ + { + struct adt_segment_ranges segs[] = { + {.phys = 0x1000ff18000ULL, .size = 0x2c000}, + }; + build_tree(tree, TREE_SIZE, "okay", true); + reset_env(segs, sizeof(segs)); + test_asc_ok = false; + int ret = dt_aop_locked_dart_handoff(2, "/soc/aop@290c00000"); + check(ret < 0 && bailed == 1, "packed FDT + asc_init failure: conservatively bails"); + } + + /* 14. Tight FDT where fdt_add_subnode fits but properties run out of space: rolled back */ + { + struct adt_segment_ranges segs[] = { + {.phys = 0x1000ff18000ULL, .size = 0x2c000}, + }; + int sz = build_tree(tree, TREE_SIZE, "okay", true); + int tight = sz + 64; /* enough for subnode header, not enough for all properties */ + assert(!fdt_open_into(tree, tree, tight)); + reset_env(segs, sizeof(segs)); + int ret = dt_aop_locked_dart_handoff(2, "/soc/aop@290c00000"); + int resv = fdt_path_offset(dt, "/reserved-memory"); + int half = fdt_subnode_offset(dt, resv, "asc-firmware@1000ff18000"); + int aop = fdt_path_offset(dt, "/soc/aop@290c00000"); + const char *st = aop >= 0 ? fdt_getprop(dt, aop, "status", NULL) : NULL; + check(ret == 0 && bailed == 0 && half < 0 && st && + (!strcmp(st, "disabled") || !strcmp(st, "fail")) && !fdt_check_full(dt, tight), + "tight FDT mid-creation NOSPACE: partial subnode rolled back, halted AOP disabled"); + } + + printf(failures ? "aop os ram: %d FAILED\n" : "aop os ram: all cases pass\n", failures); + return !!failures; +} diff --git a/tests/run-aop-os-ram.sh b/tests/run-aop-os-ram.sh new file mode 100755 index 000000000..16c1c2416 --- /dev/null +++ b/tests/run-aop-os-ram.sh @@ -0,0 +1,24 @@ +#!/bin/sh +# SPDX-License-Identifier: MIT +# Host check of locked-DART AOP OS-RAM reservation and failure handling (src/kboot.c + +# src/fdt_disable.c) with real libfdt. +set -eu +cd "$(dirname "$0")/.." +tmp=$(mktemp -d) +trap 'rm -rf "$tmp"' EXIT HUP INT TERM +python3 - "$tmp/aop_os_ram.c" <<'PY' +from pathlib import Path +src = Path("src/kboot.c").read_text() +start = src.index("static int dt_find_aop(") +end = src.index("\n/*\n * The AOP's accelerometer", start) +blocks = src[start:end] +Path(__import__("sys").argv[1]).write_text( + Path("tests/aop_os_ram_host.c").read_text().replace("/* SOURCE FUNCTIONS */", blocks) +) +PY +cflags="-std=gnu11 -O1 -g -Wall -Wextra -Werror -fsanitize=address,undefined + -fno-omit-frame-pointer -fno-sanitize-recover=undefined -Isrc" +${CC:-cc} $cflags "$tmp/aop_os_ram.c" src/fdt_disable.c src/libfdt/fdt.c src/libfdt/fdt_ro.c \ + src/libfdt/fdt_rw.c src/libfdt/fdt_wip.c src/libfdt/fdt_sw.c src/libfdt/fdt_strerror.c \ + src/libfdt/fdt_check.c -o "$tmp/aop-os-ram-host" +ASAN_OPTIONS=${ASAN_OPTIONS:-detect_leaks=0} "$tmp/aop-os-ram-host" From 0d66afc774eabb4455f9e3ca7b4f09a9fc013b4f Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Tue, 24 Nov 2026 07:55:04 +1100 Subject: [PATCH 3/3] tests: pass a valid pointer for the empty AOP fixture ranges property The fixture writes an empty ranges property. libfdt still calls memcpy for a zero-length property, so use an empty string to satisfy the nonnull contract and keep the property length at zero. Signed-off-by: Ryan Murray --- tests/aop_os_ram_host.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/aop_os_ram_host.c b/tests/aop_os_ram_host.c index 9e6ce0780..f8dba131c 100644 --- a/tests/aop_os_ram_host.c +++ b/tests/aop_os_ram_host.c @@ -143,7 +143,7 @@ static int build_tree(void *buf, size_t cap, const char *aop_status, bool pack_t assert(!fdt_begin_node(buf, "reserved-memory")); assert(!fdt_property_u32(buf, "#address-cells", 2)); assert(!fdt_property_u32(buf, "#size-cells", 2)); - assert(!fdt_property(buf, "ranges", NULL, 0)); + assert(!fdt_property(buf, "ranges", "", 0)); assert(!fdt_end_node(buf)); assert(!fdt_begin_node(buf, "soc"));