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
Sound: the audio DSP's firmware from the system image, beside GL=1
vcvkke45bc27a7ecb
diff --git a/README.md b/README.mdindex 3d6dc34..e9db848 100644--- a/README.md+++ b/README.md@@ -232,7 +232,7 @@ XL_SECS=900 XL_CMDLINE="touchscreen=1 cpufreq=1 bootcpu=7 cpus=4" ./xl run #| `wifi_file=0` | с `wifi=1`: сеть из `fw-wifi/network` при старте не подключается, радио ждёт, пока сеть выберут в Настройках (образ с `WIFID=1`) || `wifi_irq=0`, `wifi_ba=0`, `wifi_wide=0` | с `wifi=1`: без прерываний радио (его кольца опрашиваются раз в миллисекунду, как раньше), без блочных подтверждений (точка доступа шлёт кадры по одному — скорость впятеро ниже), без широкого канала (только 20 МГц) || `wifi_step=N` | то же по шагам, как это выяснялось: первые N шагов, и ядро ждёт, пока они пройдут. 1–5 только читают (общая память процессоров, почтовый ящик процессора питания, RPMh, шины питания, выводы), 6–11 кладут прошивку процессора Wi-Fi в его память, заводят в общей памяти нашу половину его сигнальных битов (SMP2P) и запускают его через TrustZone, 12–14 поднимают связь с ним (GLINK, канал IPCRTR) и QRTR поверх неё: процессор Wi-Fi объявляет свои службы, среди них службу прошивки Wi-Fi; 15 — разговор с этой службой, какой драйвер ведёт до того, как тронет радио (нужны описания сообщений из ath11k: `tools/mk-wlfw.sh`, затем пересобрать ядро): прошивка принимает сообщения ath11k, файл платы и базу регионов телефона, с `wifi_cal=1` калибруется и через 7 секунд говорит «готова»; с `wifi_mission=1` прошивка получает настройку и переходит в рабочий режим; 16 — чтение нескольких регистров радио (`wifi_radio_read=1`): в рабочем режиме они отвечают, вне его чтение кончается ошибкой шины, и шаг его не делает (`adb pull` файлов `wpss.mdt`, `wpss.b02`…`wpss.b07` из `/vendor/firmware_mnt/image` в каталог `fw-wifi/`; в git они не попадают, `userland/build.sh` кладёт их в малый образ). Каждый шаг пишет в журнал, что собирается тронуть и что прочёл. `wifi_keep=1` оставляет процессор Wi-Fi и связь с ним работать, пока идёт сеанс. С `wifi_lx=1` шаг 15 ведёт сам драйвер ath11k из Linux (см. «Драйвер Wi-Fi» ниже; ядро собирается после `tools/mk-ath11k.sh`): его `qmi.c` проходит весь разговор с прошивкой до рабочего режима, со своей настройкой и списком теневых регистров, а регистры радио не трогает. С `wifi_lx=2 wifi_smmu=2` потоку радио в SMMU даётся контекст с нашими таблицами страниц (`wifi_smmu=2` в одиночку — только это, с возвратом как было), `ce.c` и `hal.c` драйвера заводят в регистрах радио кольца его «копировальных машин», `htc.c` подключает по ним службу WMI, и прошивка по WMI рассказывает о радио (одно, две антенные цепи, 2,4 и 5 ГГц) и через четверть секунды после запуска шины сообщает, что готова к командам («unified ready»); собственного адреса у прошивки нет (нули). Прерываний при этом нет: кольца опрашиваются раз в миллисекунду. С `wifi_scan=1` драйвер после этого регистрирует радио, заводит интерфейс в режиме станции и просит прошивку просканировать эфир — только слушая, в эфир ничего не уходит: 26 каналов 2,4 и 5 ГГц за четыре секунды, и в журнале появляются точки доступа вокруг с каналами и уровнем сигнала (имена и адреса чужих сетей — только в журнале запуска на вашей машине). Адрес радио у прошивки свой не записан: он лежит одной строкой в `fw-wifi/mac` (в git не попадает), и `xl run` передаёт его ядру параметром `wifi_mac`. С `wifi_join=1` драйвер подключается к сети из `fw-wifi/network` (WPA2 с паролем): находит её точку доступа, проходит аутентификацию, ассоциацию и четырёхстороннее рукопожатие, отдаёт ключи прошивке и становится тем, что стоит за `en0`; с `wifi_keep=1` он остаётся работать, пока идёт сеанс, без него — сворачивается, когда шаги кончились (`wifi_hold=S` — на S секунд позже). Остановленный без спроса (ядро, которое до этого не дошло) второй раз без перезагрузки телефона не запускается — такой запуск сбрасывал телефон, и драйвер от него сам отказывается; шаги и остальные параметры описаны в начале `bsd/dev/redwood_wifi.c` |-| `adsp=1` | аудиопроцессор телефона (ADSP: звук, а у Xiaomi ещё и зарядка), шаг 1 звука: прошивка `adsp.mdt` и `adsp.b02`…`adsp.b36` с разделов телефона (`/vendor/firmware_mnt/image`, в каталог `fw-audio/`, в git не попадает; в малый образ её кладёт `ADSP=1 userland/build.sh`, и только без `GL=1` — вместе они не помещаются в 64 МБ, что берёт загрузчик) кладётся в его память, RPMh просят о его шинах питания LCX и LMX, пока он не попросит о них сам, TrustZone его запускает, через 41 мс он говорит «готов», поднимается GLINK (канал IPCRTR) и QRTR поверх него: процессор — узел 5 — объявляет 12 служб и открывает свои каналы, среди них `apr_audio_svc`, через который потом пойдёт звук. Рядом с загрузкой системы и рядом с `wifi=1`; в конце сеанса его просят остановиться и выключают. `adsp_step=N` — то же по шагам (первые N из 10, ядро ждёт), подробности в начале `bsd/dev/redwood_adsp.c` |+| `adsp=1` | аудиопроцессор телефона (ADSP: звук, а у Xiaomi ещё и зарядка), шаг 1 звука: прошивка `adsp.mdt` и `adsp.b02`…`adsp.b36` с разделов телефона (`/vendor/firmware_mnt/image`, в каталог `fw-audio/`, в git не попадает; `userland/build.sh` кладёт её в образ системы в `/fw`, откуда ядро читает её, когда корень смонтирован, — после её появления образ системы пересобирается с `SYSTEM=1`; для `adsp_step=N`, который идёт до монтирования, — в малый образ с `ADSP=1`, и только без `GL=1`: вместе они не помещаются в 64 МБ, что берёт загрузчик) кладётся в его память, RPMh просят о его шинах питания LCX и LMX, пока он не попросит о них сам, TrustZone его запускает, через 41 мс он говорит «готов», поднимается GLINK (канал IPCRTR) и QRTR поверх него: процессор — узел 5 — объявляет 12 служб и открывает свои каналы, среди них `apr_audio_svc`, через который потом пойдёт звук. Рядом с загрузкой системы и рядом с `wifi=1`; в конце сеанса его просят остановиться и выключают. `adsp_step=N` — то же по шагам (первые N из 10, ядро ждёт), подробности в начале `bsd/dev/redwood_adsp.c` |Несколько ядер на телефоне проверены (2, 4 и 7 ядер запускаются и чисто возвращаются в загрузчик). Лучшийвариант — **`cpus=4`**: четыре больших ядра (7, 6, 5, 4). На одном и том же сценарии: 929 кадров вместо 671,diff --git a/userland/build.sh b/userland/build.shindex 0e74514..660646f 100755--- a/userland/build.sh+++ b/userland/build.sh@@ -31,8 +31,9 @@# GL=test: only the library and bin/gl_test (a triangle, src/gl_test.c;# RC_JOBS=rc.jobs.gltest runs it); the window server stays as it is.# ADSP=1 with the audio DSP's firmware from ../fw-audio in fw/ (20 MB; for the-# kernel's adsp=1 / adsp_step=N, bsd/dev/redwood_adsp.c). Not beside GL=1:-# the two do not fit the 64 MB the loader takes with the kernel.+# kernel's adsp_step=N, bsd/dev/redwood_adsp.c, which runs before the root is+# mounted). Not beside GL=1: the two do not fit the 64 MB the loader takes with+# the kernel. adsp=1 needs none of it: the system image has the firmware in /fw.# system.packfs the system image, the root filesystem (second RAM disk, /dev/md1): the part of# iOS 16.5 chosen by ../ios165/select-system.py at its real paths, the dyld# shared cache, and src/stage0.c as /sbin/launchd (the real one is@@ -237,6 +238,12 @@ if [ -f $IOS/system.manifest ] && [ -f $CACHE/dyld_shared_cache_arm64 ] &&fimv sysroot/sbin/launchd sysroot/sbin/launchd.ioscp obj/stage0 sysroot/sbin/launchd+ # the audio DSP's firmware, the phone's own from ../fw-audio (never in the repository), where the kernel reads+ # it once the root is mounted (bsd/dev/redwood_adsp.c, adsp=1): 20 MB that do not fit beside GL=1 in the small image+ if [ -f ../fw-audio/adsp.mdt ]; then+ mkdir -p sysroot/fw+ cp ../fw-audio/adsp.mdt ../fw-audio/adsp.b0[2-9] ../fw-audio/adsp.b[1-9][0-9] sysroot/fw/+ fi# our audio device, where the audio server looks for such plug-ins (and for no others: Apple's are left out)mkdir -p sysroot/System/Library/Audio/Plug-Ins/HALln -s /redwood/lib/RedwoodAudio.driver sysroot/System/Library/Audio/Plug-Ins/HAL/RedwoodAudio.driverdiff --git a/xnu/files/bsd/dev/redwood_adsp.c b/xnu/files/bsd/dev/redwood_adsp.cindex b4e278a..5fa9048 100644--- a/xnu/files/bsd/dev/redwood_adsp.c+++ b/xnu/files/bsd/dev/redwood_adsp.c@@ -6,7 +6,8 @@* to the application processor. It is the audio DSP's -- a Hexagon of its own inside LPASS,* Android's "adsp" -- which stays in reset until somebody loads it, exactly as the Wi-Fi's does* (redwood_wifi.c): the phone's own signed image, adsp.mdt and adsp.b02 to adsp.b36 from- * /vendor/firmware_mnt/image of its Android, goes into the 64 MB reserved for it, TrustZone is+ * /vendor/firmware_mnt/image of its Android (20 MB: in the system image under /fw, or with ADSP=1 in+ * the small one, see image_file()), goes into the 64 MB reserved for it, TrustZone is* asked to authenticate and start it, and from there everything is conversation through the* shared memory: SMP2P for "up", "given up", "stopped"; GLINK for channels; QRTR on the channel* "IPCRTR" for the services the DSP offers. (Reading an LPASS clock register from here ends the@@ -80,8 +81,11 @@#include <miscfs/packfs/packfs.h>#include <pexpert/pexpert.h>#include <sys/errno.h>+#include <sys/kauth.h>#include <sys/stat.h>#include <sys/systm.h>+#include <sys/vnode.h>+#include <sys/vnode_internal.h>#include <vm/vm_kern.h>extern vm_offset_t ml_io_map(vm_offset_t phys_addr, vm_size_t size);@@ -974,7 +978,56 @@ image_entry(const uint8_t *image, const struct packfs_super *super, const structreturn NULL;}-/* A file of fw/ in the small image (userland/build.sh puts fw-audio/ there), or in the system image. */+/*+ * A file of /fw on the root file system, read whole into memory of ours: the system image, where+ * userland/build.sh puts fw-audio/ when it makes that image. It is compressed there, which the file+ * system undoes and the reading of the small image's memory below does not; and it is there only+ * once the root is mounted, so only adsp=1, which waits for that on its own thread, finds it so.+ */+static const uint8_t *+root_file(const char *name, uint64_t *size)+{+ char path[64];+ vfs_context_t context = vfs_context_kernel();+ vnode_t vnode = NULLVP;+ struct vnode_attr attributes;+ uint8_t *data;+ int left = 0, error;++ if (rootvnode == NULLVP) {+ return NULL;+ }+ snprintf(path, sizeof(path), "/fw/%s", name);+ if (vnode_lookup(path, 0, &vnode, context) != 0) {+ return NULL;+ }+ VATTR_INIT(&attributes);+ VATTR_WANTED(&attributes, va_data_size);+ if (vnode_getattr(vnode, &attributes, context) != 0 || attributes.va_data_size == 0 ||+ attributes.va_data_size > ADSP_MEMORY_SIZE) {+ vnode_put(vnode);+ return NULL;+ }+ data = kalloc_data(attributes.va_data_size, Z_WAITOK);+ if (data == NULL) {+ vnode_put(vnode);+ return NULL;+ }+ error = vn_rdwr(UIO_READ, vnode, (caddr_t)data, (int)attributes.va_data_size, 0, UIO_SYSSPACE, 0,+ vfs_context_ucred(context), &left, vfs_context_proc(context));+ vnode_put(vnode);+ if (error != 0 || left != 0) {+ printf("adsp: %u: %s: read error %d, %d bytes left\n", step, path, error, left);+ kfree_data(data, attributes.va_data_size);+ return NULL;+ }+ *size = attributes.va_data_size;+ return data;+}++static bool from_root; /* the image is in memory of ours (root_file()), to be given back once written */++/* A file of fw/ in the small image (ADSP=1 userland/build.sh puts fw-audio/ there), else of /fw on the root (above). */static const uint8_t *image_file(const char *name, uint64_t *size){@@ -1004,11 +1057,20 @@ image_file(const char *name, uint64_t *size)return (const uint8_t *)base + file->pd_data;}}- printf("adsp: %u: no fw/%s in the small image: put the phone's /vendor/firmware_mnt/image/%s into fw-audio/ and run "- "userland/build.sh\n", step, name, name);+ {+ const uint8_t *data = root_file(name, size);++ if (data != NULL) {+ from_root = true;+ return data;+ }+ }+ printf("adsp: %u: no fw/%s in the small image nor on the root: put the phone's /vendor/firmware_mnt/image/%s into "+ "fw-audio/ and run SYSTEM=1 userland/build.sh (or ADSP=1, for the small image)\n", step, name, name);return NULL;}+#define MDT_HASH(flags) (((flags) >> 24 & 7) == 2)#define MAX_SEGMENTS 48@@ -1028,6 +1090,29 @@ static struct {uint32_t size;} image;+/* The image's files given back, if they were read into memory of ours. */+static void+image_forget(void)+{+ if (!from_root) {+ return;+ }+ if (image.header != NULL) {+ void *data = (void *)(uintptr_t)image.header;++ kfree_data(data, image.header_size);+ }+ for (unsigned s = 0; s < image.segments; s++) {+ if (image.segment[s].file != NULL) {+ void *data = (void *)(uintptr_t)image.segment[s].file;++ kfree_data(data, image.segment[s].file_size);+ }+ }+ bzero(&image, sizeof(image));+ from_root = false;+}+static boolstep_firmware(void){@@ -1036,17 +1121,20 @@ step_firmware(void)uint64_t size = 0, files = 0;uint32_t count, hashes = 0, lowest = ~0u, highest = 0;- announce("find the audio DSP's image in the small image, and check it against its own header");+ announce("find the audio DSP's image in the small image or on the root, and check it against its own header");+ image_forget();image.segments = 0;header = image_file("adsp.mdt", &size);if (header == NULL) {return false;}+ image.header = header;+ image.header_size = size;count = size >= 52 + 2 * 32 ? header[44] | header[45] << 8 : 0;if (memcmp(header, "\177ELF\1", 5) != 0 || count < 3 || count > MAX_SEGMENTS || 52 + 32 * count > size || size > PAGE_SIZE) {printf("adsp: %u: fw/adsp.mdt is not such a header (%02x %02x %02x %02x, %u segments, %llu bytes)\n", step, header[0],header[1], header[2], header[3], count, size);- return false;+ goto fail;}for (uint32_t i = 0; i < count; i++) {char name[16];@@ -1062,7 +1150,7 @@ step_firmware(void)(uint64_t)segment.paddr + segment.memory_size > (uint64_t)ADSP_MEMORY + ADSP_MEMORY_SIZE) {printf("adsp: %u: segment %u (%#x, %#x bytes) is not within the DSP's memory\n", step, i, segment.paddr,segment.memory_size);- return false;+ goto fail;}image.segment[image.segments].at = segment.paddr - ADSP_MEMORY;image.segment[image.segments].file_size = segment.file_size;@@ -1074,11 +1162,12 @@ step_firmware(void)snprintf(name, sizeof(name), "adsp.b%02u", i);image.segment[image.segments].file = image_file(name, &file_size);if (image.segment[image.segments].file == NULL) {- return false;+ goto fail;}if (file_size != segment.file_size) {printf("adsp: %u: fw/%s has %llu bytes, the header says %u\n", step, name, file_size, segment.file_size);- return false;+ image.segments++; /* (for its file to be given back) */+ goto fail;}files += file_size;}@@ -1089,15 +1178,19 @@ step_firmware(void)if (image.segments == 0 || lowest != ADSP_MEMORY || 52 + 32 * count + (uint64_t)hashes != size) {printf("adsp: %u: not what TrustZone takes: %u segments to load, the lowest at %#x; the header file has %llu bytes, ""the header %u and the hashes %u\n", step, image.segments, lowest, size, 52 + 32 * count, hashes);- image.segments = 0;- return false;+ goto fail;}image.header = header;image.header_size = size;image.size = ((highest + 0xfff) & ~0xfffu) - ADSP_MEMORY;- printf("adsp: %u: the image is whole: a header of %llu bytes (%u of hashes), %u segments, %llu bytes of files, %#x bytes "- "of the DSP's %#x from %#x\n", step, size, hashes, image.segments, files, image.size, ADSP_MEMORY_SIZE, ADSP_MEMORY);+ printf("adsp: %u: the image is whole (%s): a header of %llu bytes (%u of hashes), %u segments, %llu bytes of files, %#x "+ "bytes of the DSP's %#x from %#x\n", step, from_root ? "from /fw on the root" : "from the small image", size, hashes,+ image.segments, files, image.size, ADSP_MEMORY_SIZE, ADSP_MEMORY);return true;+fail:+ image_forget();+ image.segments = 0;+ return false;}/* ---- step 3: the mailbox --------------------------------------------------------------------------- */@@ -1403,6 +1496,7 @@ step_image(void)settle();ml_io_unmap((vm_offset_t)dsp_memory, ADSP_MEMORY_SIZE);image_written = wrong == 0;+ image_forget(); /* (it is in the DSP's memory now: what was read of the root, given back) */return image_written;}@@ -2544,13 +2638,17 @@ whole_thread(__unused void *parameter, __unused wait_result_t result)uint64_t began = now_us();unsigned services = 0;+ /* (the firmware may be on the root file system, which is not mounted yet when this begins: a minute at most) */+ for (unsigned waited = 0; waited < 300 && rootvnode == NULLVP; waited++) {+ sleep_ms(200);+ }run_steps();for (unsigned i = 0; i < sizeof(qrtr_servers) / sizeof(qrtr_servers[0]); i++) {services += qrtr_servers[i].there;}if (step == STEPS && dsp_started) {- tell("adsp: THE AUDIO DSP IS UP AND LINKED, %llu ms after the kernel began on it: node %u, %u services; it stays on\n",- (now_us() - began) / 1000, qrtr_peer, services);+ tell("adsp: THE AUDIO DSP IS UP AND LINKED, %llu ms after the kernel began on it (the root's waited for included): node "+ "%u, %u services; it stays on\n", (now_us() - began) / 1000, qrtr_peer, services);} else {tell("adsp: THE AUDIO DSP DID NOT COME UP: step %u failed (%s); see the kernel's own log for the steps\n", step + 1,last_write);