Skip to content

Apple's open-source XNU kernel on Android phones: its own loader and drivers, Metal on the Adreno. Runs from RAM; a reboot gives the phone back to Android.

C

Memory: compressor on, jetsam thresholds from usable memory, a memory report

vcvkk80512aa227d6
diff --git a/README.md b/README.md
index 1dadb03..067b76b 100644
--- a/README.md
+++ b/README.md
@@ -93,7 +93,8 @@ XL_SECS=900 XL_CMDLINE="touchscreen=1 cpufreq=1 bootcpu=7" ./xl run # 15 м
```
`./xl run` ждёт конца сеанса и кладёт журнал в `logs/`; запускать можно сколько угодно раз, образ системы в ОЗУ
-остаётся. `./xl` без аргументов печатает все команды.
+остаётся. `./xl` без аргументов печатает все команды. Пока идёт сеанс, другие команды `xl` не выполняются
+(«busy»): загрузчик отвечает одному собеседнику.
Управление как на iPhone X: свайп вверх от нижнего края — разблокировка и «домой», погасший экран будится
касанием. **Закончить сеанс раньше времени — приложить к экрану пять пальцев на полторы секунды**: ядро
@@ -222,7 +223,10 @@ Userland не изменён ни в одном байте: всё, чего е
## Отладка
- Журнал сеанса: stdout QEMU или `logs/ЧЧММСС-run.log` с телефона. Драйвер экрана раз в 5 с пишет частоту кадров,
- драйвер сенсора раз в минуту — сводку касаний.
+ драйвер сенсора раз в минуту — сводку касаний, ядро раз в минуту — строку `memory:` (куда ушла память и при
+ каком остатке оно начнёт убивать процессы).
+- `RC_JOBS=rc.jobs.tour REST=1 userland/build.sh` — образ со сценарием: сам разблокирует экран и по очереди
+ открывает все приложения первой страницы (проверка стабильности и памяти, сеанс ~9 минут).
- `userabort:` в журнале — стек процесса, вызвавшего `abort()`; `usertrap:` (с `systrace=1`) — аварии.
- Стеки зависшего процесса на лету (QEMU): `qemu/touch.py stacks ИМЯ`, затем `qemu/symstack.py ЖУРНАЛ ИМЯ`
(нужен [`ipsw`](https://github.com/blacktop/ipsw)).
diff --git a/prebuilt/SHA256SUMS b/prebuilt/SHA256SUMS
index 83186bb..50441ac 100644
--- a/prebuilt/SHA256SUMS
+++ b/prebuilt/SHA256SUMS
@@ -1,7 +1,7 @@
-Built 2026-10-12 14:59 from this tree.
+Built 2026-10-12 15:21 from this tree.
-5035f00ea98878cb09834b70f2ef9b4a3c3b0bb749f3770e9506d6db059ec78a prebuilt/mach.development.bcm2837
-4b703f1bce73fef0cdb803ef4773668d28a4fb956f5e16dcbfb895455ab2e091 prebuilt/loader.efi
+2129b264b396598956e07d1050a04aeef4e64dd0df0fe5eb99a2ff1acbc0cecb prebuilt/mach.development.bcm2837
+4a875f2943b3c2ead63ee9e4c6141251c1a62c24639954114651e37157f4dab8 prebuilt/loader.efi
d2a35a4a1d6658ffb3696914f727661b52c9333de8a3af8a31642b8023ce03ac prebuilt/userland/child
db564073effc31e7ab1679a7ffe0631ec22c3030ad347c927028f3e53c8583c3 prebuilt/userland/hello
a8baa9ab38333280369b0824aafaf92c7aa6a85851ff0b2551a4c711dbe59cfd prebuilt/userland/hello_dyn
diff --git a/prebuilt/loader.efi b/prebuilt/loader.efi
index ddb5ee1..1789028 100755
Binary files a/prebuilt/loader.efi and b/prebuilt/loader.efi differ
diff --git a/prebuilt/mach.development.bcm2837 b/prebuilt/mach.development.bcm2837
index 2bedd3f..d52dc36 100755
Binary files a/prebuilt/mach.development.bcm2837 and b/prebuilt/mach.development.bcm2837 differ
diff --git a/userland/rc.jobs.tour b/userland/rc.jobs.tour
new file mode 100644
index 0000000..7d62cbc
--- /dev/null
+++ b/userland/rc.jobs.tour
@@ -0,0 +1,11 @@
+# Commands run by the launchd job com.redwood.demo (see com.redwood.demo.plist).
+# A tour for exercising memory and stability (RC_JOBS=rc.jobs.tour, a run of about 9 minutes): unlock,
+# then every application of the first home-screen page and of the dock for 20 seconds each, and a
+# look at who is still alive at the end. The kernel's "memory:" lines tell the rest.
+/redwood/bin/rwtouch wait 30 swipe 0.5 0.985 0.5 0.3 200 wait 6 tap 0.15 0.36 wait 20 button home wait 5 tap 0.385 0.36 wait 20 button home wait 5 tap 0.615 0.36 wait 20 button home wait 5 tap 0.845 0.36 wait 20 button home wait 5
+/redwood/bin/rwtouch tap 0.15 0.475 wait 20 button home wait 5 tap 0.385 0.475 wait 20 button home wait 5 tap 0.615 0.475 wait 20 button home wait 5 tap 0.845 0.475 wait 20 button home wait 5
+/redwood/bin/rwtouch tap 0.15 0.59 wait 20 button home wait 5 tap 0.385 0.59 wait 20 button home wait 5 tap 0.615 0.59 wait 20 button home wait 5 tap 0.845 0.59 wait 20 button home wait 5
+/redwood/bin/rwtouch tap 0.15 0.71 wait 20 button home wait 5 tap 0.385 0.71 wait 20 button home wait 5 tap 0.615 0.71 wait 20 button home wait 5
+/redwood/bin/rwtouch tap 0.15 0.925 wait 20 button home wait 5 tap 0.615 0.925 wait 20 button home wait 5 tap 0.845 0.925 wait 20 button home wait 5
+/bin/ps -A
+/usr/bin/vm_stat
diff --git a/xl b/xl
index b17206c..5629a8c 100755
--- a/xl
+++ b/xl
@@ -160,6 +160,14 @@ with open(out, 'wb') as f:
PY
}
+# One command at a time: the loader answers one host at a time, and a second command sent while a
+# run is waiting for its result ("xl state" in another terminal is enough) can cost that run its log.
+case "$1" in
+"") ;;
+*) exec 9> logs/.xl.lock
+ flock -n 9 || { echo "xl: busy: another xl command (a run?) is talking to the phone" >&2; exit 1; } ;;
+esac
+
case "$1" in
enter) enter && echo "xl: loader is up" ;;
screenshot) screenshot "$2" ;;
diff --git a/xnu/files/bsd/miscfs/ramfs/ramfs.c b/xnu/files/bsd/miscfs/ramfs/ramfs.c
index 2ef2300..b167fb5 100644
--- a/xnu/files/bsd/miscfs/ramfs/ramfs.c
+++ b/xnu/files/bsd/miscfs/ramfs/ramfs.c
@@ -111,6 +111,9 @@ struct ramfs_mount {
uint64_t rm_max_pages;
};
+/* file pages in use on all the mounts together (each mount has its own lock): for the kernel's memory report */
+_Atomic uint64_t ramfs_pages_in_use;
+
#define RAMFS_MP(mp) ((struct ramfs_mount *)vfs_fsprivate(mp))
#define RAMFS_NODE(vp) ((struct ramfs_node *)vnode_fsnode(vp))
@@ -183,6 +186,7 @@ ramfs_free_pages(struct ramfs_node *node, uint64_t from)
node->rn_pages[i] = NULL;
node->rn_nalloc--;
node->rn_mount->rm_pages--;
+ os_atomic_dec(&ramfs_pages_in_use, relaxed);
}
}
}
@@ -401,6 +405,7 @@ ramfs_store(struct ramfs_node *node, uint64_t off, const void *src, size_t len,
*spare = NULL;
node->rn_nalloc++;
rm->rm_pages++;
+ os_atomic_inc(&ramfs_pages_in_use, relaxed);
}
memcpy((char *)node->rn_pages[index] + (off & PAGE_MASK), src, len);
if (off + len > node->rn_size) {
diff --git a/xnu/files/iokit/Kernel/MiniIOSurface.cpp b/xnu/files/iokit/Kernel/MiniIOSurface.cpp
index 0899cdb..d5ae49c 100644
--- a/xnu/files/iokit/Kernel/MiniIOSurface.cpp
+++ b/xnu/files/iokit/Kernel/MiniIOSurface.cpp
@@ -476,6 +476,14 @@ MiniIOSurfaceCopy(uint32_t sid)
return s;
}
+/* How many surfaces there are and how much (wired) memory they hold: for the kernel's memory report. */
+extern "C" void
+MiniIOSurfaceUsage(uint32_t * count, uint64_t * bytes)
+{
+ *count = gSurfaceCount;
+ *bytes = gSurfaceBytes;
+}
+
void
MiniIOSurfaceDrop(MiniIOSurface * s)
{
diff --git a/xnu/files/osfmk/arm64/loader_return.c b/xnu/files/osfmk/arm64/loader_return.c
index 92aeaba..ae8fe58 100644
--- a/xnu/files/osfmk/arm64/loader_return.c
+++ b/xnu/files/osfmk/arm64/loader_return.c
@@ -19,6 +19,8 @@
#include <arm64/proc_reg.h>
#include <pexpert/pexpert.h>
#include <pexpert/arm64/board_config.h>
+#include <vm/vm_page.h>
+#include <vm/vm_compressor.h>
/*
* The console log handed back to the loader. A long session writes more than fits, and both
@@ -332,6 +334,39 @@ STARTUP(EARLY_BOOT, STARTUP_RANK_LAST, loader_return_timer_init);
static thread_call_t hv_wdt_call;
static uint32_t hv_wdt_enabled;
+#define MEMORY_REPORT_BEATS 6 /* a line about memory every so many heartbeats */
+
+extern unsigned int memorystatus_available_pages, memorystatus_available_pages_critical,
+ memorystatus_available_pages_critical_base;
+extern uint32_t c_segment_pages_compressed;
+extern _Atomic uint64_t ramfs_pages_in_use;
+extern void MiniIOSurfaceUsage(uint32_t *count, uint64_t *bytes);
+
+/*
+ * Where the memory is, in megabytes: for reading a session's log afterwards. "available" is what
+ * the kernel's own count says (free and file-backed): below the first threshold it kills idle
+ * daemons, and once none is left and it is below the second, whatever is next in line -- the
+ * window server included.
+ */
+static void
+memory_report(void)
+{
+#define MB(pages) ((unsigned int)(((uint64_t)(pages) * PAGE_SIZE) >> 20))
+ uint32_t surfaces = 0;
+ uint64_t surface_bytes = 0;
+
+ MiniIOSurfaceUsage(&surfaces, &surface_bytes);
+ printf("memory: free %u, files %u, programs %u, wired %u (%u surfaces %u, ramfs %u), compressor %u (holding %u) MB; "
+ "available %u, kills below %u (the idle) and %u (anybody)\n",
+ MB(vm_page_free_count), MB(vm_page_pageable_external_count), MB(vm_page_pageable_internal_count),
+ MB(vm_page_wire_count), surfaces, (unsigned int)(surface_bytes >> 20),
+ MB(os_atomic_load(&ramfs_pages_in_use, relaxed)),
+ MB(VM_PAGE_COMPRESSOR_COUNT), MB(c_segment_pages_compressed),
+ MB(memorystatus_available_pages),
+ MB(memorystatus_available_pages_critical), MB(memorystatus_available_pages_critical_base));
+#undef MB
+}
+
/* Every HV_WDT_PET_SECONDS: feed the watchdog if there is one, and show any watching loader a sign of life. */
static void
hv_wdt_pet(__unused thread_call_param_t p0, __unused thread_call_param_t p1)
@@ -347,7 +382,9 @@ hv_wdt_pet(__unused thread_call_param_t p0, __unused thread_call_param_t p1)
__asm__ volatile ("smc #0" : "+r" (x0), "+r" (x1), "+r" (x2), "+r" (x3) : :
"x4", "x5", "x6", "x7", "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15", "x16", "x17");
}
- loader_live.heartbeat++;
+ if (++loader_live.heartbeat % MEMORY_REPORT_BEATS == 0) {
+ memory_report();
+ }
clock_interval_to_deadline(HV_WDT_PET_SECONDS, NSEC_PER_SEC, &deadline);
thread_call_enter_delayed(hv_wdt_call, deadline);
diff --git a/xnu/redwood.patch b/xnu/redwood.patch
index 1158b22..b47b8cb 100644
--- a/xnu/redwood.patch
+++ b/xnu/redwood.patch
@@ -483,6 +483,68 @@ index 1d71c66629fb..57cd16eed941 100644
if (0 == error) {
/*
* Update the new process's signature-dependent process state.
+diff --git a/bsd/kern/kern_memorystatus.c b/bsd/kern/kern_memorystatus.c
+index 1750d3a52d3c..9947cb2421da 100644
+--- a/bsd/kern/kern_memorystatus.c
++++ b/bsd/kern/kern_memorystatus.c
+@@ -222,6 +222,15 @@ MACRO_END
+
+ /* General tunables */
+
++/*
++ * redwood: the memory there is to manage. max_mem is the whole of the kernel's memory window, and
++ * here more than half of that window is the system image -- a RAM disk the kernel boots from, which
++ * is nobody's to use or to give back. Thresholds that are per cent of max_mem had idle daemons
++ * killed with a quarter of the usable memory still available, and the window server not long after.
++ * sane_size leaves the image (and the kernel itself) out.
++ */
++#define memstat_memory_size sane_size
++
+ unsigned long delta_percentage = 5;
+ unsigned long critical_threshold_percentage = 5;
+ // On embedded devices with more than 3GB of memory we lower the critical percentage.
+@@ -1365,7 +1374,7 @@ memorystatus_init(void)
+ if (delta_percentage == 0) {
+ delta_percentage = 5;
+ }
+- if (max_mem > config_jetsam_large_memory_cutoff) {
++ if (memstat_memory_size > config_jetsam_large_memory_cutoff) {
+ critical_threshold_percentage = critical_threshold_percentage_larger_devices;
+ delta_percentage = delta_percentage_larger_devices;
+ }
+@@ -1400,11 +1409,11 @@ memorystatus_init(void)
+ PE_get_default("kern.jetsam_idle_snapshot", &memorystatus_idle_snapshot, sizeof(memorystatus_idle_snapshot));
+ }
+
+- memorystatus_delta = (unsigned int) (delta_percentage * atop_64(max_mem) / 100);
+- memorystatus_available_pages_critical_idle_offset = (unsigned int) (idle_offset_percentage * atop_64(max_mem) / 100);
++ memorystatus_delta = (unsigned int) (delta_percentage * atop_64(memstat_memory_size) / 100);
++ memorystatus_available_pages_critical_idle_offset = (unsigned int) (idle_offset_percentage * atop_64(memstat_memory_size) / 100);
+ memorystatus_available_pages_critical_base = (unsigned int) ((critical_threshold_percentage / delta_percentage) * memorystatus_delta);
+ memorystatus_policy_more_free_offset_pages = (unsigned int) ((policy_more_free_offset_percentage / delta_percentage) * memorystatus_delta);
+- memorystatus_sysproc_aging_aggr_pages = (unsigned int) (sysproc_aging_aggr_threshold_percentage * atop_64(max_mem) / 100);
++ memorystatus_sysproc_aging_aggr_pages = (unsigned int) (sysproc_aging_aggr_threshold_percentage * atop_64(memstat_memory_size) / 100);
+
+ /* Set the swapin trigger in pages based on the maximum size allocated for each c_seg */
+ memorystatus_swapin_trigger_pages = (unsigned int) atop_64(memorystatus_swapin_trigger_segments * c_seg_allocsize);
+@@ -5103,7 +5112,7 @@ memorystatus_pages_update(unsigned int pages_avail)
+ if (critical || delta) {
+ unsigned int total_pages;
+
+- total_pages = (unsigned int) atop_64(max_mem);
++ total_pages = (unsigned int) atop_64(memstat_memory_size);
+ #if CONFIG_SECLUDED_MEMORY
+ total_pages -= vm_page_secluded_count;
+ #endif /* CONFIG_SECLUDED_MEMORY */
+@@ -6609,7 +6618,7 @@ memorystatus_update_levels_locked(boolean_t critical_only)
+ }
+
+ #if VM_PRESSURE_EVENTS
+- memorystatus_available_pages_pressure = (int32_t)(pressure_threshold_percentage * (atop_64(max_mem) / 100));
++ memorystatus_available_pages_pressure = (int32_t)(pressure_threshold_percentage * (atop_64(memstat_memory_size) / 100));
+ #endif
+ }
+
diff --git a/bsd/kern/kern_sig.c b/bsd/kern/kern_sig.c
index ae4a5f33df1e..986b517dfd18 100644
--- a/bsd/kern/kern_sig.c
diff --git a/xnuloader/loader.c b/xnuloader/loader.c
index 86cec63..ed68bc2 100644
--- a/xnuloader/loader.c
+++ b/xnuloader/loader.c
@@ -1641,7 +1641,13 @@ run_kernel(int have_checkpoint, uint64_t checkpoint)
dt_end(&w);
dt_end(&w);
+ /*
+ * What iBoot's device tree tells the kernel to do unless told otherwise. The compressor: an
+ * embedded kernel left to itself keeps it for the freezer alone, and with nothing being
+ * compressed the memory is gone twice as fast (2 = VM_PAGER_COMPRESSOR_NO_SWAP, as on a phone).
+ */
dt_begin(&w, "defaults");
+ dt_prop_u32(&w, "kern.vm_compressor", 2);
dt_end(&w);
/* the product: screen class, artwork scale, name ... as on the real phone */