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

Kernel build from a fresh clone; resumable IPSW download

vcvkk01af9a282064
diff --git a/.gitignore b/.gitignore
index f835581..3c13307 100644
--- a/.gitignore
+++ b/.gitignore
@@ -72,4 +72,9 @@ __pycache__/
!/xnu-deps/build/pthread/
/xnu-deps/build/pthread/*
!/xnu-deps/build/pthread/build.sh
+!/xnu-deps/build/pthread/inc/
!/xnu-deps/build/lzbitmap/
+/xnu-deps/build/lzbitmap/*
+!/xnu-deps/build/lzbitmap/unzbm.c
+!/xnu-deps/build/lzbitmap/libzbitmap.c
+!/xnu-deps/build/lzbitmap/libzbitmap.h
diff --git a/README.md b/README.md
index cabbeb9..a37ad38 100644
--- a/README.md
+++ b/README.md
@@ -125,9 +125,16 @@ XL_SECS=900 XL_CMDLINE="touchscreen=1 cpufreq=1 bootcpu=7" ./xl run # 15 м
### Ядро
-См. `xnu/README.md`: исходники XNU берутся у Apple (`apple-oss-distributions/xnu`, тег `xnu-8796.121.2`), поверх
-накладываются наши правки (`xnu/redwood.patch` + новые файлы в `xnu/files/`), окружение для сборки на Linux
-стоковым clang готовит `xnu-deps/setup.sh`.
+Нужны [theos](https://theos.dev/docs/installation-linux) в `~/theos` (cctools и `iPhoneOS16.5.sdk`) и пакеты
+`base-devel git curl clang lld python perl xz`. Дальше три команды, вместе около двух минут:
+
+```sh
+xnu/apply.sh # исходники XNU (тег xnu-8796.121.2) с GitHub Apple + наши правки → xnu-src/
+xnu-deps/setup.sh # зависимости, инструменты и SDK для сборки на Linux стоковым clang
+./xnu-build.sh -j8 -k # → xnu-src/xnu-8796.121.2/BUILD/obj/DEVELOPMENT_ARM64_BCM2837/mach.development.bcm2837
+```
+
+Подробности, подводные камни и обзор того, что добавлено в ядро, — в `xnu/README.md`.
### Загрузчик
@@ -219,6 +226,7 @@ Userland не изменён ни в одном байте: всё, чего е
- Файлов iOS в репозитории нет: `ios165/fetch.sh` скачивает их из IPSW с CDN Apple на вашу машину.
- `xnu/` — производная работа от XNU, распространяется на условиях APSL 2.0, как и сам XNU; `prebuilt/mach.*` —
ядро, собранное из этих исходников.
-- `xnu-deps/build/lzbitmap/libzbitmap.*` — libzbitmap (Corellium, GPL-2.0+ OR MIT).
+- `xnu-deps/build/lzbitmap/libzbitmap.*` — libzbitmap (Corellium, GPL-2.0+ OR MIT);
+ `xnu-deps/hostinc/mach-o/{loader,nlist,fat}.h` — заголовки из XNU (APSL 2.0).
- `uefi/files/Binaries/redwood` и `prebuilt/uefi/SILICIUM_UEFI.fd` содержат драйверы UEFI из прошивки Xiaomi
(так же, как их распространяет Project Silicium для других устройств); `prebuilt/uefi/mkbootimg.py` — AOSP, Apache-2.0.
diff --git a/ios165/fetch-from-ipsw.py b/ios165/fetch-from-ipsw.py
index a1f4273..b5648f3 100755
--- a/ios165/fetch-from-ipsw.py
+++ b/ios165/fetch-from-ipsw.py
@@ -4,13 +4,21 @@
usage: fetch-from-ipsw.py MEMBER OUTPUT
The IPSW is a ZIP64 archive on Apple's CDN; its directory is read with HTTP range requests and
-the member is streamed (and inflated, if it is compressed) straight to OUTPUT.
+the member is streamed (and inflated, if it is compressed) straight to OUTPUT. A download that
+breaks off is taken up again: a member stored without compression (the big disk images are)
+continues where it stopped, also when OUTPUT is the partial file of an earlier run; a compressed
+one starts over.
"""
+import http.client
+import os
import struct
import sys
+import time
import urllib.request
import zlib
+ATTEMPTS = 20
+
URL = ('https://updates.cdn-apple.com/2023SpringFCS/fullrestores/032-85130/6AB0D10A-8AF9-4F6C-8CF5-A1B315CABE88/'
'iPhone10,3,iPhone10,6_16.5_20F66_Restore.ipsw')
SIZE = 6165698198
@@ -49,25 +57,49 @@ def directory():
p += 46 + nl + el + cl
+def download(data, method, csize, output):
+ """One attempt at the member whose bytes start at DATA in the archive; returns the size of OUTPUT."""
+ if method == 0:
+ have = os.path.getsize(output) if os.path.exists(output) else 0
+ if have > csize:
+ have = 0
+ if have < csize:
+ with fetch(data + have, data + csize - 1) as stream, open(output, 'ab' if have else 'wb') as out:
+ while chunk := stream.read(1 << 20):
+ out.write(chunk)
+ return os.path.getsize(output)
+ inflater = zlib.decompressobj(-15)
+ written = 0
+ with fetch(data, data + csize - 1) as stream, open(output, 'wb') as out:
+ while chunk := stream.read(1 << 20):
+ chunk = inflater.decompress(chunk)
+ out.write(chunk)
+ written += len(chunk)
+ tail = inflater.flush()
+ out.write(tail)
+ return written + len(tail)
+
+
def main():
member, output = sys.argv[1], sys.argv[2]
for name, method, csize, usize, offset in directory():
if name != member:
continue
+ if method not in (0, 8):
+ sys.exit(f'{member}: compression method {method} is not supported')
header = fetch(offset, offset + 29).read()
nl, el = struct.unpack('<HH', header[26:30])
data = offset + 30 + nl + el
- inflater = zlib.decompressobj(-15) if method == 8 else None
- written = 0
- with fetch(data, data + csize - 1) as stream, open(output, 'wb') as out:
- while chunk := stream.read(1 << 20):
- chunk = inflater.decompress(chunk) if inflater else chunk
- out.write(chunk)
- written += len(chunk)
- if inflater:
- tail = inflater.flush()
- out.write(tail)
- written += len(tail)
+ written = -1
+ for attempt in range(1, ATTEMPTS + 1):
+ try:
+ written = download(data, method, csize, output)
+ except (OSError, http.client.HTTPException, zlib.error) as error:
+ print(f'{member}: {error}', file=sys.stderr)
+ if written == usize:
+ break
+ print(f'{member}: the download broke off, trying again ({attempt} of {ATTEMPTS})', file=sys.stderr)
+ time.sleep(5)
print(f'{member}: {written} bytes (expected {usize})')
sys.exit(0 if written == usize else 1)
sys.exit(f'{member}: not in the archive')
diff --git a/xnu-deps/build/lzbitmap/compress.c b/xnu-deps/build/lzbitmap/compress.c
deleted file mode 100644
index 2e05e7a..0000000
--- a/xnu-deps/build/lzbitmap/compress.c
+++ /dev/null
@@ -1,478 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0-only
-/*
- * Copyright (C) 2020 Corellium LLC
- */
-
-#include <linux/slab.h>
-#include <linux/zlib.h>
-#include <linux/mutex.h>
-
-#include "apfs.h"
-#include "libzbitmap.h"
-#include "lzfse/lzfse.h"
-#include "lzfse/lzvn_decode_base.h"
-
-#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 12, 0)
-
-#include <linux/vmalloc.h>
-
-static inline void *kvmalloc(size_t size, gfp_t flags)
-{
- gfp_t kmalloc_flags = flags;
- void *ret;
-
- if ((flags & GFP_KERNEL) != GFP_KERNEL)
- return kmalloc(size, flags);
-
- if (size > PAGE_SIZE)
- kmalloc_flags |= __GFP_NOWARN | __GFP_NORETRY;
-
- ret = kmalloc(size, flags);
- if (ret || size < PAGE_SIZE)
- return ret;
-
- return vmalloc(size);
-}
-
-#endif /* LINUX_VERSION_CODE < KERNEL_VERSION(4, 12, 0) */
-
-struct apfs_compress_file_data {
- struct apfs_compress_hdr hdr;
- u8 *buf;
- ssize_t bufblk;
- size_t bufsize;
- struct mutex mtx;
- struct super_block *sb;
- struct apfs_compressed_data cdata;
-};
-
-static inline int apfs_compress_is_rsrc(u32 algo)
-{
- return (algo & 1) == 0;
-}
-
-static inline bool apfs_compress_is_supported(u32 algo)
-{
- switch (algo) {
- case APFS_COMPRESS_ZLIB_RSRC:
- case APFS_COMPRESS_ZLIB_ATTR:
- case APFS_COMPRESS_LZVN_RSRC:
- case APFS_COMPRESS_LZVN_ATTR:
- case APFS_COMPRESS_PLAIN_RSRC:
- case APFS_COMPRESS_PLAIN_ATTR:
- case APFS_COMPRESS_LZFSE_RSRC:
- case APFS_COMPRESS_LZFSE_ATTR:
- case APFS_COMPRESS_LZBITMAP_RSRC:
- case APFS_COMPRESS_LZBITMAP_ATTR:
- return true;
- default:
- /* Once will usually be enough, don't flood the console */
- pr_err_once("APFS: unsupported compression algorithm (%u)\n", algo);
- return false;
- }
-}
-
-static int apfs_compress_file_open(struct inode *inode, struct file *filp)
-{
- struct super_block *sb = inode->i_sb;
- struct apfs_nxsb_info *nxi = APFS_NXI(sb);
- struct apfs_compress_file_data *fd;
- ssize_t res;
- bool is_rsrc;
-
- /*
- * The official implementation seems to transparently decompress files
- * when you write to them. Doing that atomically inside the kernel is
- * probably a chore, so for now I'll just leave it to the user to make
- * an uncompressed copy themselves and replace the original. I might
- * fix this in the future, but only if people complain (TODO).
- */
- if (filp->f_mode & FMODE_WRITE) {
- apfs_warn(sb, "writes to compressed files are not supported");
- apfs_warn(sb, "you can work with a copy of the file instead");
- return -EOPNOTSUPP;
- }
-
- if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
- return -EOVERFLOW;
-
- fd = kzalloc(sizeof(*fd), GFP_KERNEL);
- if (!fd)
- return -ENOMEM;
- mutex_init(&fd->mtx);
- fd->sb = sb;
-
- down_read(&nxi->nx_big_sem);
-
- res = ____apfs_xattr_get(inode, APFS_XATTR_NAME_COMPRESSED, &fd->hdr, sizeof(fd->hdr), 0);
- if (res != sizeof(fd->hdr)) {
- apfs_err(sb, "decmpfs header read failed");
- goto fail;
- }
-
- if (!apfs_compress_is_supported(le32_to_cpu(fd->hdr.algo))) {
- res = -EOPNOTSUPP;
- goto fail;
- }
-
- fd->buf = kvmalloc(APFS_COMPRESS_BLOCK, GFP_KERNEL);
- if (!fd->buf) {
- res = -ENOMEM;
- goto fail;
- }
- fd->bufblk = -1;
-
- is_rsrc = apfs_compress_is_rsrc(le32_to_cpu(fd->hdr.algo));
- res = apfs_xattr_get_compressed_data(inode, is_rsrc ? APFS_XATTR_NAME_RSRC_FORK : APFS_XATTR_NAME_COMPRESSED, &fd->cdata);
- if (res) {
- apfs_err(sb, "failed to get compressed data");
- goto fail;
- }
-
- up_read(&nxi->nx_big_sem);
-
- filp->private_data = fd;
- return 0;
-
-fail:
- apfs_release_compressed_data(&fd->cdata);
- if (fd->buf)
- kvfree(fd->buf);
- up_read(&nxi->nx_big_sem);
- kfree(fd);
- if (res > 0)
- res = -EINVAL;
- return res;
-}
-
-static int apfs_compress_file_read_block(struct apfs_compress_file_data *fd, loff_t block)
-{
- struct super_block *sb = fd->sb;
- struct apfs_compressed_data *comp_data = &fd->cdata;
- u8 *cdata = NULL;
- u8 *tmp = fd->buf;
- u32 doffs = 0, coffs;
- size_t csize, bsize;
- int res = 0;
-
- if (apfs_compress_is_rsrc(le32_to_cpu(fd->hdr.algo)) &&
- le32_to_cpu(fd->hdr.algo) != APFS_COMPRESS_LZBITMAP_RSRC &&
- le32_to_cpu(fd->hdr.algo) != APFS_COMPRESS_LZVN_RSRC &&
- le32_to_cpu(fd->hdr.algo) != APFS_COMPRESS_LZFSE_RSRC) {
- struct apfs_compress_rsrc_hdr hdr = {0};
- struct apfs_compress_rsrc_data cd = {0};
- struct apfs_compress_rsrc_block blk = {0};
- u32 blk_off;
-
- res = apfs_compressed_data_read(comp_data, &hdr, sizeof(hdr), 0 /* offset */);
- if (res) {
- apfs_err(sb, "failed to read resource header");
- return res;
- }
-
- doffs = be32_to_cpu(hdr.data_offs);
- res = apfs_compressed_data_read(comp_data, &cd, sizeof(cd), doffs);
- if (res) {
- apfs_err(sb, "failed to read resource data header");
- return res;
- }
- if (block >= le32_to_cpu(cd.num))
- return 0;
-
- blk_off = doffs + sizeof(cd) + sizeof(blk) * block;
- res = apfs_compressed_data_read(comp_data, &blk, sizeof(blk), blk_off);
- if (res) {
- apfs_err(sb, "failed to read resource block metadata");
- return res;
- }
-
- bsize = le64_to_cpu(fd->hdr.size) - block * APFS_COMPRESS_BLOCK;
- if (bsize > APFS_COMPRESS_BLOCK)
- bsize = APFS_COMPRESS_BLOCK;
-
- csize = le32_to_cpu(blk.size);
- coffs = le32_to_cpu(blk.offs) + 4;
- } else if (apfs_compress_is_rsrc(le32_to_cpu(fd->hdr.algo))) {
- __le32 blks[2];
- u32 blk_off;
-
- blk_off = doffs + sizeof(__le32) * block;
- res = apfs_compressed_data_read(comp_data, blks, sizeof(blks), blk_off);
- if (res) {
- apfs_err(sb, "failed to read resource block metadata");
- return res;
- }
-
- bsize = le64_to_cpu(fd->hdr.size) - block * APFS_COMPRESS_BLOCK;
- if (bsize > APFS_COMPRESS_BLOCK)
- bsize = APFS_COMPRESS_BLOCK;
-
- coffs = le32_to_cpu(blks[0]);
- csize = le32_to_cpu(blks[1]) - coffs;
- } else {
- /*
- * I think attr compression is only for single-block files, in
- * fact none of these files ever seem to decompress to more than
- * 2048 bytes.
- */
- bsize = le64_to_cpu(fd->hdr.size);
- if (block != 0 || bsize > APFS_COMPRESS_BLOCK) {
- apfs_err(sb, "file too big for inline compression");
- return -EFSCORRUPTED;
- }
-
- /* The first few bytes are the decmpfs header */
- coffs = sizeof(struct apfs_compress_hdr);
- csize = comp_data->size - sizeof(struct apfs_compress_hdr);
- }
-
- cdata = kvmalloc(csize, GFP_KERNEL);
- if (!cdata)
- return -ENOMEM;
- res = apfs_compressed_data_read(comp_data, cdata, csize, doffs + coffs);
- if (res) {
- apfs_err(sb, "failed to read compressed block");
- goto fail;
- }
-
- switch (le32_to_cpu(fd->hdr.algo)) {
- case APFS_COMPRESS_ZLIB_RSRC:
- case APFS_COMPRESS_ZLIB_ATTR:
- if (cdata[0] == 0x78 && csize >= 2) {
- res = zlib_inflate_blob(tmp, bsize, cdata + 2, csize - 2);
- if (res <= 0) {
- apfs_err(sb, "zlib decompression failed");
- goto fail;
- }
- bsize = res;
- res = 0;
- } else if ((cdata[0] & 0x0F) == 0x0F) {
- memcpy(tmp, &cdata[1], csize - 1);
- bsize = csize - 1;
- } else {
- apfs_err(sb, "zlib decompression failed");
- res = -EINVAL;
- goto fail;
- }
- break;
- case APFS_COMPRESS_LZVN_RSRC:
- case APFS_COMPRESS_LZVN_ATTR:
- if (cdata[0] == 0x06) {
- memcpy(tmp, &cdata[1], csize - 1);
- bsize = csize - 1;
- } else {
- lzvn_decoder_state dstate = {0};
-
- dstate.src = cdata;
- dstate.src_end = dstate.src + csize;
- dstate.dst = dstate.dst_begin = tmp;
- dstate.dst_end = dstate.dst + bsize;
- lzvn_decode(&dstate);
- bsize = dstate.dst - tmp;
- }
- break;
- case APFS_COMPRESS_LZBITMAP_RSRC:
- case APFS_COMPRESS_LZBITMAP_ATTR:
- if (cdata[0] == 0x5a) {
- res = zbm_decompress(tmp, bsize, cdata, csize, &bsize);
- if (res < 0) {
- apfs_err(sb, "lzbitmap decompression failed");
- goto fail;
- }
- res = 0;
- } else if ((cdata[0] & 0x0F) == 0x0F) {
- memcpy(tmp, &cdata[1], csize - 1);
- bsize = csize - 1;
- } else {
- apfs_err(sb, "lzbitmap decompression failed");
- res = -EINVAL;
- goto fail;
- }
- break;
- case APFS_COMPRESS_LZFSE_RSRC:
- case APFS_COMPRESS_LZFSE_ATTR:
- if (cdata[0] == 0x62 && csize >= 2) {
- res = lzfse_decode_buffer(tmp, bsize, cdata, csize, NULL);
- if (res == 0) {
- apfs_err(sb, "lzfse decompression failed");
- /* Could be ENOMEM too... */
- res = -EINVAL;
- goto fail;
- }
- bsize = res;
- res = 0;
- } else {
- /* cdata[0] == 0xff, apparently */
- memcpy(tmp, &cdata[1], csize - 1);
- bsize = csize - 1;
- }
- break;
- case APFS_COMPRESS_PLAIN_RSRC:
- case APFS_COMPRESS_PLAIN_ATTR:
- memcpy(tmp, &cdata[1], csize - 1);
- bsize = csize - 1;
- break;
- default:
- res = -EINVAL;
- goto fail;
- }
- fd->bufblk = block;
- fd->bufsize = bsize;
-fail:
- kvfree(cdata);
- return res;
-}
-
-static int apfs_compress_file_release(struct inode *inode, struct file *filp)
-{
- struct apfs_compress_file_data *fd = filp->private_data;
-
- apfs_release_compressed_data(&fd->cdata);
- if (fd->buf)
- kvfree(fd->buf);
- kfree(fd);
- return 0;
-}
-
-static ssize_t apfs_compress_file_read_from_block(struct apfs_compress_file_data *fd, char *buf, size_t size, loff_t off)
-{
- struct super_block *sb = fd->sb;
- struct apfs_nxsb_info *nxi = APFS_NXI(sb);
- struct apfs_compressed_data cdata = fd->cdata;
- loff_t block;
- size_t bsize;
- ssize_t res;
-
- /*
- * Request reads of all blocks before actually working with any of them.
- * The compressed data is typically small enough that this is effective.
- * It would be much better to make an inode for the xattr dstream and
- * work with readahead as usual, but I'm not confident I can get that
- * right (TODO).
- */
- if (cdata.has_dstream && off == 0) {
- down_read(&nxi->nx_big_sem);
- apfs_nonsparse_dstream_preread(cdata.dstream);
- up_read(&nxi->nx_big_sem);
- }
-
- if (off >= le64_to_cpu(fd->hdr.size))
- return 0;
- if (size > le64_to_cpu(fd->hdr.size) - off)
- size = le64_to_cpu(fd->hdr.size) - off;
-
- block = off / APFS_COMPRESS_BLOCK;
- off -= block * APFS_COMPRESS_BLOCK;
- if (block != fd->bufblk) {
- down_read(&nxi->nx_big_sem);
- res = apfs_compress_file_read_block(fd, block);
- up_read(&nxi->nx_big_sem);
- if (res) {
- apfs_err(sb, "failed to read block into buffer");
- return res;
- }
- }
- bsize = fd->bufsize;
-
- if (bsize < off)
- return 0;
- bsize -= off;
- if (size > bsize)
- size = bsize;
- memcpy(buf, fd->buf + off, size);
- return size;
-}
-
-static ssize_t apfs_compress_file_read_page(struct file *filp, char *buf, loff_t off)
-{
- struct apfs_compress_file_data *fd = filp->private_data;
- loff_t step;
- ssize_t block, res;
- size_t size = PAGE_SIZE;
-
- step = 0;
- while (step < size) {
- block = APFS_COMPRESS_BLOCK - ((off + step) & (APFS_COMPRESS_BLOCK - 1));
- if (block > size - step)
- block = size - step;
- mutex_lock(&fd->mtx);
- res = apfs_compress_file_read_from_block(fd, buf + step, block, off + step);
- mutex_unlock(&fd->mtx);
- if (res < block) {
- if (res < 0 && !step)
- return res;
- step += res > 0 ? res : 0;
- break;
- }
- step += block;
- }
- return step;
-}
-
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 19, 0) || RHEL_VERSION_GE(9, 3)
-static int apfs_compress_read_folio(struct file *filp, struct folio *folio)
-{
- struct page *page = &folio->page;
-#else
-static int apfs_compress_readpage(struct file *filp, struct page *page)
-{
-#endif
- char *addr = NULL;
- ssize_t ret;
- loff_t off;
-
- /* Mostly copied from ext4_read_inline_page() */
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 16, 0)
- off = page_folio(page)->index << PAGE_SHIFT;
-#else
- off = page->index << PAGE_SHIFT;
-#endif
- addr = kmap(page);
- ret = apfs_compress_file_read_page(filp, addr, off);
- flush_dcache_page(page);
- kunmap(page);
- if (ret >= 0) {
- zero_user_segment(page, ret, PAGE_SIZE);
- SetPageUptodate(page);
- ret = 0;
- }
-
- unlock_page(page);
- return ret;
-}
-
-const struct address_space_operations apfs_compress_aops = {
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 19, 0) || RHEL_VERSION_GE(9, 3)
- .read_folio = apfs_compress_read_folio,
-#else
- .readpage = apfs_compress_readpage,
-#endif
-};
-
-/* TODO: these operations are all happening without proper locks */
-const struct file_operations apfs_compress_file_operations = {
- .open = apfs_compress_file_open,
- .llseek = generic_file_llseek,
- .read_iter = generic_file_read_iter,
- .release = apfs_compress_file_release,
- .mmap = apfs_file_mmap,
-};
-
-int apfs_compress_get_size(struct inode *inode, loff_t *size)
-{
- struct apfs_compress_hdr hdr;
- int res = ____apfs_xattr_get(inode, APFS_XATTR_NAME_COMPRESSED, &hdr, sizeof(hdr), 0);
-
- if (res < 0)
- return res;
- if (res != sizeof(hdr)) {
- apfs_err(inode->i_sb, "decmpfs header read failed");
- return 1;
- }
-
- if (!apfs_compress_is_supported(le32_to_cpu(hdr.algo)))
- return 1;
-
- *size = le64_to_cpu(hdr.size);
- return 0;
-}
diff --git a/xnu-deps/build/pthread/inc/TargetConditionals.h b/xnu-deps/build/pthread/inc/TargetConditionals.h
new file mode 100644
index 0000000..57375ed
--- /dev/null
+++ b/xnu-deps/build/pthread/inc/TargetConditionals.h
@@ -0,0 +1,12 @@
+#ifndef _TARGETCONDITIONALS_H_
+#define _TARGETCONDITIONALS_H_
+/* minimal stand-in for building the pthread kext sources outside Xcode */
+#define TARGET_OS_MAC 1
+#define TARGET_OS_OSX 0
+#define TARGET_OS_IPHONE 1
+#define TARGET_OS_IOS 1
+#define TARGET_OS_EMBEDDED 1
+#define TARGET_OS_SIMULATOR 0
+#define TARGET_OS_DRIVERKIT 0
+#define TARGET_CPU_ARM64 1
+#endif
diff --git a/xnu-deps/iig-impl/undefined-all.txt b/xnu-deps/iig-impl/undefined-all.txt
new file mode 100644
index 0000000..77fcb73
--- /dev/null
+++ b/xnu-deps/iig-impl/undefined-all.txt
@@ -0,0 +1,105 @@
+_CleanPoC_DcacheRegion_Force_nopreempt_nohid
+___cxa_atexit
+_kpc_arch_init
+_kpc_configurable_config_count
+_kpc_configurable_count
+_kpc_configurable_max
+_kpc_fixed_config_count
+_kpc_fixed_count
+_kpc_fixed_max
+_kpc_get_all_cpus_counters
+_kpc_get_classes
+_kpc_get_configurable_config
+_kpc_get_configurable_counters
+_kpc_get_fixed_config
+_kpc_get_fixed_counters
+_kpc_get_pmu_version
+_kpc_get_rawpmu_config
+_kpc_idle
+_kpc_idle_exit
+_kpc_is_running_configurable
+_kpc_is_running_fixed
+_kpc_pc_capture
+_kpc_rawpmu_config_count
+_kpc_set_config_arch
+_kpc_set_period_arch
+_kpc_set_running_arch
+_kpc_set_sw_inc
+___llvm_profile_get_size_for_buffer_internal
+___llvm_profile_write_buffer_internal
+_ml_enable_monitor
+_pmap_pending_preemption
+__ZN11IOEventLink16SetEventlinkPortEP8ipc_portPFiP15OSMetaClassBase5IORPCE
+__ZN11IOEventLink8DispatchE5IORPC
+__ZN11IOEventLink9MetaClass8DispatchE5IORPC
+__ZN11IOEventLink9metaClassE
+__ZN11IOMemoryMap8DispatchE5IORPC
+__ZN11IOMemoryMap9MetaClass8DispatchE5IORPC
+__ZN11IOWorkGroup16SetWorkGroupPortEP8ipc_portPFiP15OSMetaClassBase5IORPCE
+__ZN11IOWorkGroup8DispatchE5IORPC
+__ZN11IOWorkGroup9MetaClass8DispatchE5IORPC
+__ZN11IOWorkGroup9metaClassE
+__ZN12IODMACommand11CompleteDMAEyPFiP15OSMetaClassBase5IORPCE
+__ZN12IODMACommand8DispatchE5IORPC
+__ZN12IODMACommand9MetaClass8DispatchE5IORPC
+__ZN12IOUserClient15_ExternalMethodEyPKyjP6OSDataP18IOMemoryDescriptorPyPjyPS3_S5_P8OSActionPFiP15OSMetaClassBase5IORPCE
+__ZN12IOUserClient23CopyClientMemoryForTypeEyPyPP18IOMemoryDescriptorPFiP15OSMetaClassBase5IORPCE
+__ZN12IOUserClient28CreateActionKernelCompletionEmPP8OSAction
+__ZN12IOUserClient8DispatchE5IORPC
+__ZN12IOUserClient9MetaClass8DispatchE5IORPC
+__ZN12IOUserServer10LoadModuleEPKcPFiP15OSMetaClassBase5IORPCE
+__ZN12IOUserServer4ExitEPKcPFiP15OSMetaClassBase5IORPCE
+__ZN12IOUserServer8DispatchE5IORPC
+__ZN12IOUserServer9MetaClass8DispatchE5IORPC
+__ZN15IODispatchQueue8DispatchE5IORPC
+__ZN15IODispatchQueue9MetaClass8DispatchE5IORPC
+__ZN15IODispatchQueue9metaClassE
+__ZN16IODispatchSource23SetEnableWithCompletionEbU13block_pointerFvvEPFiP15OSMetaClassBase5IORPCE
+__ZN16IODispatchSource8DispatchE5IORPC
+__ZN16IODispatchSource9MetaClass8DispatchE5IORPC
+__ZN18IOMemoryDescriptor8DispatchE5IORPC
+__ZN18IOMemoryDescriptor9MetaClass8DispatchE5IORPC
+__ZN24IOBufferMemoryDescriptor8DispatchE5IORPC
+__ZN24IOBufferMemoryDescriptor9MetaClass8DispatchE5IORPC
+__ZN25IODataQueueDispatchSource12DataServicedEP8OSActionPFiP15OSMetaClassBase5IORPCE
+__ZN25IODataQueueDispatchSource13DataAvailableEP8OSActionPFiP15OSMetaClassBase5IORPCE
+__ZN25IODataQueueDispatchSource23CopyDataServicedHandlerEPP8OSActionPFiP15OSMetaClassBase5IORPCE
+__ZN25IODataQueueDispatchSource24CopyDataAvailableHandlerEPP8OSActionPFiP15OSMetaClassBase5IORPCE
+__ZN25IODataQueueDispatchSource6CreateEyP15IODispatchQueuePPS_
+__ZN25IODataQueueDispatchSource8DispatchE5IORPC
+__ZN25IODataQueueDispatchSource9MetaClass8DispatchE5IORPC
+__ZN25IODataQueueDispatchSource9metaClassE
+__ZN25IOInterruptDispatchSource17InterruptOccurredE5IORPCP8OSActionyyPFiP15OSMetaClassBaseS0_E
+__ZN25IOInterruptDispatchSource8DispatchE5IORPC
+__ZN25IOInterruptDispatchSource9MetaClass8DispatchE5IORPC
+__ZN25IOInterruptDispatchSource9metaClassE
+__ZN35IOServiceNotificationDispatchSource24ServiceNotificationReadyEP8OSActionPFiP15OSMetaClassBase5IORPCE
+__ZN35IOServiceNotificationDispatchSource8DispatchE5IORPC
+__ZN35IOServiceNotificationDispatchSource9MetaClass8DispatchE5IORPC
+__ZN35IOServiceNotificationDispatchSource9metaClassE
+__ZN40IOServiceStateNotificationDispatchSource22StateNotificationReadyEP8OSActionPFiP15OSMetaClassBase5IORPCE
+__ZN40IOServiceStateNotificationDispatchSource8DispatchE5IORPC
+__ZN40IOServiceStateNotificationDispatchSource9MetaClass8DispatchE5IORPC
+__ZN40IOServiceStateNotificationDispatchSource9metaClassE
+__ZN8OSAction7AbortedEPFiP15OSMetaClassBase5IORPCE
+__ZN8OSAction8DispatchE5IORPC
+__ZN8OSAction9MetaClass8DispatchE5IORPC
+__ZN8OSAction9metaClassE
+__ZN8OSObject17CopyDispatchQueueEPKcPP15IODispatchQueuePFiP15OSMetaClassBase5IORPCE
+__ZN8OSObject8DispatchE5IORPC
+__ZN8OSObject9MetaClass8DispatchE5IORPC
+__ZN9IOService10Stop_asyncEPS_PFiP15OSMetaClassBase5IORPCE
+__ZN9IOService12UpdateReportEP6OSDatajPjyyP18IOMemoryDescriptorPFiP15OSMetaClassBase5IORPCE
+__ZN9IOService13ClientCrashedEPS_yPFiP15OSMetaClassBase5IORPCE
+__ZN9IOService13SetPowerStateEjPFiP15OSMetaClassBase5IORPCE
+__ZN9IOService14_NewUserClientEjP12OSDictionaryPP12IOUserClientPFiP15OSMetaClassBase5IORPCE
+__ZN9IOService15ConfigureReportEP6OSDatajPjPFiP15OSMetaClassBase5IORPCE
+__ZN9IOService17UserSetPropertiesEP8OSObjectPFiP15OSMetaClassBase5IORPCE
+__ZN9IOService24StateNotificationItemSetEP8OSStringP12OSDictionaryPFiP15OSMetaClassBase5IORPCE
+__ZN9IOService25StateNotificationItemCopyEP8OSStringPP12OSDictionaryPFiP15OSMetaClassBase5IORPCE
+__ZN9IOService27StateNotificationItemCreateEP8OSStringP12OSDictionaryPFiP15OSMetaClassBase5IORPCE
+__ZN9IOService4StopEPS_PFiP15OSMetaClassBase5IORPCE
+__ZN9IOService5StartEPS_PFiP15OSMetaClassBase5IORPCE
+__ZN9IOService8DispatchE5IORPC
+__ZN9IOService9MetaClass8DispatchE5IORPC
+__ZTV16IODispatchSource
diff --git a/xnu-deps/setup.sh b/xnu-deps/setup.sh
index dbedc32..005dd06 100755
--- a/xnu-deps/setup.sh
+++ b/xnu-deps/setup.sh
@@ -36,7 +36,9 @@ die() { echo "setup: $*" >&2; exit 1; }
# fetch DIR URL COMMIT: one commit of a repository, without its history
fetch() {
- git -C "$1" rev-parse -q --verify HEAD >/dev/null 2>&1 && [ "$(git -C "$1" rev-parse --show-toplevel)" = "$XNUDEPS/$1" ] && return 0
+ # there already? (its own repository, not the one this directory is part of)
+ [ -d "$1" ] && git -C "$1" rev-parse -q --verify HEAD >/dev/null 2>&1 &&
+ [ "$(git -C "$1" rev-parse --show-toplevel)" = "$(cd "$1" && pwd -P)" ] && return 0
log "fetching $1"
rm -rf "$1"
git init -q "$1"
diff --git a/xnu/README.md b/xnu/README.md
new file mode 100644
index 0000000..da4fcf8
--- /dev/null
+++ b/xnu/README.md
@@ -0,0 +1,70 @@
+# Ядро
+
+Наши правки XNU `xnu-8796.121.2` (ядро iOS 16.5, Darwin 22.5.0):
+
+- `redwood.patch` — изменения в файлах Apple (56 файлов);
+- `files/` — новые файлы по их путям в дереве XNU (31 файл);
+- `apply.sh` — берёт исходники у Apple и накладывает всё это; `export.sh` — обратное действие.
+
+Собирается на Linux стоковым clang, без macOS и Xcode.
+
+## Сборка
+
+Нужно:
+
+- пакеты (Arch): `sudo pacman -S --needed base-devel git curl clang lld python perl xz`;
+- [theos](https://theos.dev/docs/installation-linux) в `~/theos` (или в `$THEOS`): из него берутся cctools
+ (`toolchain/linux/iphone/bin`: линкер ld64, nm, otool, strip…) и `sdks/iPhoneOS16.5.sdk`.
+
+```sh
+xnu/apply.sh # ~6 с: тег xnu-8796.121.2 из apple-oss-distributions/xnu + наши правки
+ # → xnu-src/xnu-8796.121.2, ветка redwood, правки не закоммичены
+xnu-deps/setup.sh # ~30 с: исходники зависимостей, mig/unifdef/tcsh, SDK-надстройка, две библиотеки
+./xnu-build.sh -j8 -k # ~45 с на 12 потоках
+```
+
+Результат: `xnu-src/xnu-8796.121.2/BUILD/obj/DEVELOPMENT_ARM64_BCM2837/mach.development.bcm2837`.
+`./xl run` и `qemu/run-ios.sh` берут ядро оттуда сами (а `prebuilt/` — только когда его нет).
+
+Проверено: в пустом каталоге, где лежало только содержимое репозитория, эти три команды дали ядро того же
+размера и с тем же набором символов (49 407), что и рабочее. Диск: ~600 МБ, не считая theos.
+
+Что нужно знать:
+
+- **`-k` обязателен.** Единственная ошибка сборки — установка lldbmacros (make завершается с кодом 2) — безвредна.
+- В пути к репозиторию не должно быть пробелов (makefile'ы XNU их не переживают).
+- Обёртка `xnu-deps/host/bin/xnu-clang` зовёт `/usr/bin/clang`. Проверено с clang 22.1.8.
+- После правки заголовков ядра, которые видят две внешние библиотеки (pthread kext, firehose):
+ `xnu-deps/setup.sh --rebuild-libs`.
+- DriverKit-генератор Apple (`iig`) закрыт. Вместо него — заглушки `xnu-deps/iig-impl/OSObject.iig.cpp`,
+ сгенерированные по списку неопределённых символов линкера. Если набор изменится:
+ `xnu-deps/gen-iig-stubs.py xnu-deps/iig-impl/undefined-all.txt xnu-deps/iig-impl qemu/kernel.syms`, затем `rm -rf BUILD`.
+- Для `qemu/sym.py` после пересборки: `nm -n …/mach.development.bcm2837.unstripped > qemu/kernel.syms`.
+
+## Работа с деревом
+
+Правки делаются прямо в `xnu-src/xnu-8796.121.2` (обычное git-дерево Apple на ветке `redwood`). Перед коммитом
+в этот репозиторий — `xnu/export.sh` (перегенерирует патч и `files/`), а чтобы обновить готовое ядро в
+`prebuilt/` — `tools/update-prebuilt.sh`.
+
+## Что добавлено в ядро
+
+| Файлы | Что |
+|---|---|
+| `osfmk/arm64/gic_v3_irq.c`, `generic_arm64_support.c`, `kpc.c`, `ppl/`, `tunables/` | платформа: GICv3, таймер, заглушки вырезанных Apple файлов |
+| `osfmk/arm64/loader_return.c` | возврат в загрузчик с журналом, «сердцебиение», сторожевой таймер гипервизора Qualcomm |
+| `osfmk/arm64/user_exception_trace.c` | отладка userland: `usertrap:`, `userabort:`, стеки потоков (`ustacks`) |
+| `osfmk/minicrypto/` | замена corecrypto: MD5/SHA-1/SHA-2, HMAC, DRBG, AES (исходники Apple corecrypto не использованы) |
+| `iokit/Kernel/GenericARMPE.cpp` | эксперт платформы и CPU |
+| `iokit/Kernel/MiniIOSurface.*`, `MiniIOMobileFramebuffer.cpp`, `MiniAppleKeyStore.cpp` | замены закрытых драйверов: поверхности, экран (с масштабированием в панель и записью экрана), хранилище ключей |
+| `bsd/miscfs/packfs/`, `bsd/miscfs/ramfs/` | свои ФС: сжатый образ только для чтения (корень) и записываемая ФС в памяти |
+| `bsd/kern/kern_minimac.c`, `libkern/amfi/` | заглушки AMFI и Sandbox (MAC-политики), права из подписи программы |
+| `bsd/dev/redwood_input.c`, `redwood_goodix.c`, `redwood_virtio_input.c` | ввод: `/dev/redwood-touch`, драйвер Goodix GT9916 (SPI бит-бангом по GPIO), virtio-input для QEMU |
+| `bsd/dev/redwood_cpufreq.c` | частота процессора (Qualcomm EPSS) |
+| `bsd/pthread/pthread_kext_static.c` | pthread kext, слинкованный в ядро |
+
+Патч, помимо врезок под всё это, чинит то, что мешает собрать и запустить открытый XNU вне Apple: makefile'ы под
+Linux, конфигурацию `BCM2837` (она взята за основу), ошибки в заброшенном коде (mockfs, не-PPL pmap, percpu на одном
+процессоре) и т. п.
+
+Лицензия: производная работа от XNU, APSL 2.0.
diff --git a/xnu/apply.sh b/xnu/apply.sh
index 55ce722..5e5b4f7 100755
--- a/xnu/apply.sh
+++ b/xnu/apply.sh
@@ -21,8 +21,8 @@ git init -q "$DIR"
trap 'echo "apply.sh: failed; removing the incomplete $DIR" >&2; rm -rf "$DIR"' ERR
cd "$DIR"
git remote add origin "$URL"
-git fetch -q --depth 1 origin "refs/tags/$TAG" # that one release, without the history
-git checkout -q -b redwood FETCH_HEAD
+git fetch -q --depth 1 origin "refs/tags/$TAG:refs/tags/$TAG" # that one release, without the history
+git checkout -q -b redwood "$TAG"
[ "$(git rev-parse HEAD)" = "$BASE" ] || { echo "apply.sh: $TAG is $(git rev-parse HEAD), expected $BASE" >&2; false; }
echo 'BUILD/' >> .git/info/exclude
git apply --whitespace=nowarn "$HERE/redwood.patch"
diff --git a/xnu/files/bsd/dev/redwood_input.c b/xnu/files/bsd/dev/redwood_input.c
index 325de10..642647e 100644
--- a/xnu/files/bsd/dev/redwood_input.c
+++ b/xnu/files/bsd/dev/redwood_input.c
@@ -3,6 +3,7 @@
*/
#include <kern/clock.h>
#include <kern/locks.h>
+#include <kern/thread_call.h>
#include <libkern/libkern.h>
#include <miscfs/devfs/devfs.h>
#include <sys/conf.h>
@@ -19,8 +20,52 @@ static LCK_MTX_DECLARE(touch_lock, &touch_lck_grp);
static struct redwood_touch touch_queue[TOUCH_QUEUE];
static uint32_t touch_head, touch_tail; /* read from head, add at tail */
-void
-redwood_touch_post(uint32_t type, uint32_t finger, uint32_t x, uint32_t y)
+/*
+ * The way out when nothing else is at hand: five fingers held on the screen for a second and a
+ * half end the run -- control goes back to the loader, as when loader_return_after expires.
+ * Only a driver's fingers count, not samples a program writes to the device.
+ */
+#define EXIT_FINGERS 5
+#define EXIT_HOLD_MS 1500
+
+extern void loader_return(void);
+
+static thread_call_t exit_call;
+static uint32_t fingers_down; /* one bit for each finger a driver says is down */
+
+static void
+exit_hold_elapsed(__unused thread_call_param_t p0, __unused thread_call_param_t p1)
+{
+ printf("\n--- %d fingers held on the screen for %d ms: back to the loader ---\n", EXIT_FINGERS, EXIT_HOLD_MS);
+ loader_return();
+}
+
+/* With touch_lock held. */
+static void
+count_fingers(uint32_t type, uint32_t finger)
+{
+ int before = __builtin_popcount(fingers_down), now;
+ uint64_t deadline;
+
+ if (finger >= 32 || exit_call == NULL || (type != REDWOOD_TOUCH_DOWN && type != REDWOOD_TOUCH_UP)) {
+ return;
+ }
+ if (type == REDWOOD_TOUCH_DOWN) {
+ fingers_down |= 1u << finger;
+ } else {
+ fingers_down &= ~(1u << finger);
+ }
+ now = __builtin_popcount(fingers_down);
+ if (now >= EXIT_FINGERS && before < EXIT_FINGERS) {
+ clock_interval_to_deadline(EXIT_HOLD_MS, NSEC_PER_MSEC, &deadline);
+ thread_call_enter_delayed(exit_call, deadline);
+ } else if (now < EXIT_FINGERS && before >= EXIT_FINGERS) {
+ thread_call_cancel(exit_call);
+ }
+}
+
+static void
+touch_post(uint32_t type, uint32_t finger, uint32_t x, uint32_t y, bool from_driver)
{
struct redwood_touch *t;
@@ -34,10 +79,19 @@ redwood_touch_post(uint32_t type, uint32_t finger, uint32_t x, uint32_t y)
t->finger = finger;
t->x = x > 65535 ? 65535 : x;
t->y = y > 65535 ? 65535 : y;
+ if (from_driver) {
+ count_fingers(type, finger);
+ }
lck_mtx_unlock(&touch_lock);
wakeup(&touch_queue);
}
+void
+redwood_touch_post(uint32_t type, uint32_t finger, uint32_t x, uint32_t y)
+{
+ touch_post(type, finger, x, y, true);
+}
+
static int
touch_open(__unused dev_t dev, __unused int flags, __unused int devtype, __unused struct proc *p)
{
@@ -92,7 +146,7 @@ touch_write(__unused dev_t dev, struct uio *uio, __unused int ioflag)
if (t.type < REDWOOD_TOUCH_DOWN || t.type > REDWOOD_BUTTON_UP) {
return EINVAL;
}
- redwood_touch_post(t.type, t.finger, t.x, t.y);
+ touch_post(t.type, t.finger, t.x, t.y, false);
}
return error;
}
@@ -111,6 +165,7 @@ redwood_input_init(void)
if (major < 0 || devfs_make_node(makedev(major, 0), DEVFS_CHAR, UID_ROOT, GID_WHEEL, 0666, "redwood-touch") == NULL) {
printf("redwood-touch: cannot create the device\n");
}
+ exit_call = thread_call_allocate(exit_hold_elapsed, NULL);
redwood_cpufreq_init();
redwood_virtio_input_init();
redwood_goodix_init();
diff --git a/xnu/files/bsd/dev/redwood_virtio_input.c b/xnu/files/bsd/dev/redwood_virtio_input.c
index 96cf03c..d8484e3 100644
--- a/xnu/files/bsd/dev/redwood_virtio_input.c
+++ b/xnu/files/bsd/dev/redwood_virtio_input.c
@@ -11,7 +11,8 @@
*
* The keyboard is also a way to tell the kernel something from the host while the system runs:
* F12, a process name, Enter prints where every thread of that process is (ustacks_now(); the
- * host side is "qemu/touch.py stacks NAME").
+ * host side is "qemu/touch.py stacks NAME"). F11 is five fingers on the screen for as long as it
+ * is held, which is how the way out in redwood_input.c is tried ("qemu/touch.py key fivefingers 2000").
*/
#include <kern/clock.h>
#include <kern/thread_call.h>
@@ -136,6 +137,7 @@ extern void ustacks_now(const char *name);
#define KEY_ENTER 28
#define KEY_LEFTSHIFT 42
#define KEY_RIGHTSHIFT 54
+#define KEY_F11 87
#define KEY_F12 88
/* F12 NAME Enter: the keys in between spell a process name. Returns true if the key was taken. */
@@ -223,6 +225,11 @@ input_event(struct input_device *d, const struct input_event *e)
d->pressed = e->value != 0;
} else if (command_key(e->code, e->value)) {
break;
+ } else if (e->code == KEY_F11 && e->value < 2) {
+ /* five more fingers, for as long as the key is held: the way out of redwood_input.c */
+ for (uint32_t i = 0; i < 5; i++) {
+ redwood_touch_post(e->value ? REDWOOD_TOUCH_DOWN : REDWOOD_TOUCH_UP, 5 + i, 13107 + 9830 * i, 32768);
+ }
} else if ((button = button_for_key(e->code)) != 0 && e->value < 2) { /* 2 = key repeat */
redwood_touch_post(e->value ? REDWOOD_BUTTON_DOWN : REDWOOD_BUTTON_UP, button, 0, 0);
}