Skip to content

XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)

C

Host-file root disk: FKI fki_file_open/rw/close (I/O with Linux running, bounce buffer), xnu.ko hostdisk= (stages 995 size, 994 I/O), XNU memdev host mode (strategy and raw rw through the host) added as md0 when there is no RAM disk

Heyang Zhoub51780c7b6cb
diff --git a/fki-ko/fki.h b/fki-ko/fki.h
index d786e69..73b13ec 100644
--- a/fki-ko/fki.h
+++ b/fki-ko/fki.h
@@ -92,6 +92,15 @@ int fki_fault_register(unsigned long lo, unsigned long hi, int (*handler)(unsign
void fki_fault_unregister(void);
/* Read 8 bytes of RAM by physical address (debugging: walking a foreign page table). */
fki_u64 fki_phys_peek(fki_u64 pa);
+/*
+ * A host file as backing store (a foreign block device's image). fki_file_open is called from ordinary Linux context
+ * and returns an opaque handle (NULL on failure) and the file's size. fki_file_rw is called from inside fki_enter():
+ * it lets Linux run (IRQs and preemption on) for the I/O, copies through a Linux bounce buffer to or from buf
+ * (with IRQs off again), and returns the bytes moved or a negative errno.
+ */
+void *fki_file_open(const char *path, int writable, fki_u64 *size);
+long fki_file_rw(void *file, int write, void *buf, unsigned long len, fki_u64 off);
+void fki_file_close(void *file);
#ifdef __cplusplus
}
#endif
diff --git a/fki-ko/fki_mod.c b/fki-ko/fki_mod.c
index 50d9e9f..b49ea83 100644
--- a/fki-ko/fki_mod.c
+++ b/fki-ko/fki_mod.c
@@ -29,6 +29,7 @@
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/sched.h>
+#include <linux/fs.h>
int fki_version(void)
{
@@ -401,3 +402,71 @@ EXPORT_SYMBOL(fki_idle_until);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Foreign kernel interface v0 (out-of-tree form for stock kernels)");
+
+void *fki_file_open(const char *path, int writable, fki_u64 *size)
+{
+ struct file *f = filp_open(path, (writable ? O_RDWR : O_RDONLY) | O_LARGEFILE, 0);
+
+ if (IS_ERR(f))
+ return NULL;
+ if (size)
+ *size = i_size_read(file_inode(f));
+ return f;
+}
+EXPORT_SYMBOL(fki_file_open);
+
+#define FKI_FILE_CHUNK (256UL << 10)
+
+long fki_file_rw(void *file, int write, void *buf, unsigned long len, fki_u64 off)
+{
+ struct file *f = file;
+ unsigned long done = 0;
+ long ret = 0;
+ void *bounce;
+
+ if (!f)
+ return -EBADF;
+ local_irq_enable();
+ preempt_enable();
+ bounce = kvmalloc(min(len, FKI_FILE_CHUNK), GFP_KERNEL);
+ if (!bounce)
+ ret = -ENOMEM;
+ while (bounce && done < len) {
+ unsigned long n = min(len - done, FKI_FILE_CHUNK);
+ loff_t pos = off + done;
+ ssize_t r;
+
+ if (write) {
+ preempt_disable(); local_irq_disable();
+ memcpy(bounce, (char *)buf + done, n);
+ local_irq_enable(); preempt_enable();
+ r = kernel_write(f, bounce, n, &pos);
+ } else {
+ r = kernel_read(f, bounce, n, &pos);
+ if (r > 0) {
+ preempt_disable(); local_irq_disable();
+ memcpy((char *)buf + done, bounce, r);
+ local_irq_enable(); preempt_enable();
+ }
+ }
+ if (r < 0) {
+ ret = r;
+ break;
+ }
+ done += r;
+ if ((unsigned long)r < n)
+ break;
+ }
+ kvfree(bounce);
+ preempt_disable();
+ local_irq_disable();
+ return ret < 0 && !done ? ret : (long)done;
+}
+EXPORT_SYMBOL(fki_file_rw);
+
+void fki_file_close(void *file)
+{
+ if (file)
+ filp_close(file, NULL);
+}
+EXPORT_SYMBOL(fki_file_close);
diff --git a/linux/include/linux/fki.h b/linux/include/linux/fki.h
index d786e69..73b13ec 100644
--- a/linux/include/linux/fki.h
+++ b/linux/include/linux/fki.h
@@ -92,6 +92,15 @@ int fki_fault_register(unsigned long lo, unsigned long hi, int (*handler)(unsign
void fki_fault_unregister(void);
/* Read 8 bytes of RAM by physical address (debugging: walking a foreign page table). */
fki_u64 fki_phys_peek(fki_u64 pa);
+/*
+ * A host file as backing store (a foreign block device's image). fki_file_open is called from ordinary Linux context
+ * and returns an opaque handle (NULL on failure) and the file's size. fki_file_rw is called from inside fki_enter():
+ * it lets Linux run (IRQs and preemption on) for the I/O, copies through a Linux bounce buffer to or from buf
+ * (with IRQs off again), and returns the bytes moved or a negative errno.
+ */
+void *fki_file_open(const char *path, int writable, fki_u64 *size);
+long fki_file_rw(void *file, int write, void *buf, unsigned long len, fki_u64 off);
+void fki_file_close(void *file);
#ifdef __cplusplus
}
#endif
diff --git a/xnu-ko/xnu_main.c b/xnu-ko/xnu_main.c
index 06200dd..df2219d 100644
--- a/xnu-ko/xnu_main.c
+++ b/xnu-ko/xnu_main.c
@@ -46,6 +46,13 @@ static char *bootargs;
module_param(bootargs, charp, 0444);
module_param(ramdisk, charp, 0444);
static const void *rd_data; static unsigned long rd_size; static void *rd_blob;
+/* A root disk backed by a host file (an HFS+ image): XNU's memdev md0 in host mode, stages 995 (size) and 994 (I/O). */
+static char *hostdisk;
+static int hostdisk_rw;
+module_param(hostdisk, charp, 0444);
+module_param(hostdisk_rw, int, 0444);
+static void *hd_file; static fki_u64 hd_size;
+struct xnu_hostdisk_req { u64_t write, off, len; void *buf; };
struct xnu_world xnu_W;
static void logs(const char *s) { fki_log(s, x_strlen(s)); }
@@ -200,6 +207,14 @@ long xnu_hook(unsigned long stage, unsigned long a1, unsigned long a2)
static long xnu_hook_body(unsigned long stage, unsigned long a1, unsigned long a2)
{
+ if (stage == 995) /* host disk size in bytes, 0 when there is none */
+ return hd_file ? (long)hd_size : 0;
+ if (stage == 994) { /* host disk I/O: bytes moved or -errno */
+ const struct xnu_hostdisk_req *q = (const struct xnu_hostdisk_req *)a1;
+ if (!hd_file || !q) return -6;
+ if (q->write && !hostdisk_rw) return -30;
+ return fki_file_rw(hd_file, (int)q->write, q->buf, (unsigned long)q->len, q->off);
+ }
if (stage == 996) { /* XNU console bytes (/dev/console): line-buffered into the host log */
static char line[200]; static unsigned n;
const char *b = (const char *)a1; unsigned long i;
@@ -430,7 +445,7 @@ static struct boot_args_v2 *build_boot(unsigned char *base_va, u64_t phys, u64_t
ba->deviceTreeP = base_va + off_dt;
ba->deviceTreeLength = (unsigned)dt_len;
x_memcpy(ba->CommandLine, cmdline, x_strlen(cmdline) + 1);
- if (rd_size) {
+ if (rd_size || hd_file) {
const char *rd = " rd=md0";
x_memcpy(ba->CommandLine + x_strlen(ba->CommandLine), rd, x_strlen(rd) + 1);
}
@@ -634,6 +649,11 @@ static int __init xnu_init(void)
err = fki_blob_get(ramdisk, &rd_data, &rd_size, &rd_blob);
if (err) { logkv("xnu: cannot read the RAM disk, err", (u64_t)err); return err; }
}
+ if (hostdisk) {
+ hd_file = fki_file_open(hostdisk, hostdisk_rw, &hd_size);
+ if (!hd_file) { logs("xnu: cannot open the host disk"); return -2; }
+ logkv("xnu: host disk bytes", hd_size);
+ }
err = fki_blob_get(image, &data, &size, &blob);
if (err) { logkv("xnu: cannot read image, err", (u64_t)err); return err; }
err = macho_load(&img, data, size, mode == 3 ? ~0UL : mode == 2 ? (unsigned long)ram_mb << 20 : 0);
@@ -664,6 +684,7 @@ static void __exit xnu_exit(void)
} else if (img.region.va) fki_region_free(&img.region);
if (blob) fki_blob_put(blob);
if (rd_blob) fki_blob_put(rd_blob);
+ if (hd_file) fki_file_close(hd_file);
}
module_init(xnu_init);
diff --git a/xnu-patches/0001-linux64-stage-hook.patch b/xnu-patches/0001-linux64-stage-hook.patch
index 9397acd..5275fb4 100644
--- a/xnu-patches/0001-linux64-stage-hook.patch
+++ b/xnu-patches/0001-linux64-stage-hook.patch
@@ -1,3 +1,149 @@
+diff --git a/bsd/dev/memdev.c b/bsd/dev/memdev.c
+index d24535d..d4f1cc7 100644
+--- a/bsd/dev/memdev.c
++++ b/bsd/dev/memdev.c
+@@ -95,6 +95,7 @@
+ #include <vm/vm_pager_xnu.h>
+ #include <mach/memory_object_types.h>
+ #include <kern/debug.h>
++#include <kern/kalloc.h>
+
+ #include <miscfs/devfs/devfs.h>
+
+@@ -172,6 +173,7 @@ struct mdev {
+ #define mdInited 0x01 /* This device defined */
+ #define mdRO 0x02 /* This device is read-only */
+ #define mdPhys 0x04 /* This device is in physical memory */
++#define mdHost 0x08 /* LINUX64 (xnulinux): backed by a host file, I/O through the host */
+
+ int mdevBMajor = -1;
+ int mdevCMajor = -1;
+@@ -180,6 +182,37 @@ static int mdevioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct
+ dev_t mdevadd(int devid, uint64_t base, unsigned int size, int phys);
+ dev_t mdevlookup(int devid);
+ void mdevremoveall(void);
++dev_t mdevadd_host(void);
++
++/*
++ * LINUX64 (xnulinux): a hosted XNU's disk backed by a host file. Stage 995 returns its size in bytes (0: none),
++ * stage 994 moves bytes between it and a wired kernel buffer (returns the count or -errno).
++ */
++extern uintptr_t linux64_hook;
++struct mdev_host_req { uint64_t write, off, len; void *buf; };
++
++static long
++mdev_host_call(unsigned long stage, unsigned long arg)
++{
++ register unsigned long x0 __asm__("x0") = stage;
++ register unsigned long x1 __asm__("x1") = arg;
++ register unsigned long x2 __asm__("x2") = 0;
++ __asm__ volatile ("blr %3" : "+r"(x0), "+r"(x1), "+r"(x2) : "r"(linux64_hook)
++ : "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10", "x11", "x12", "x13",
++ "x14", "x15", "x16", "x17", "x30", "cc", "memory");
++ return (long)x0;
++}
++
++static int
++mdev_host_io(int write, uint64_t off, void *buf, uint64_t len)
++{
++ struct mdev_host_req q = { .write = (uint64_t)write, .off = off, .len = len, .buf = buf };
++ long r = mdev_host_call(994, (unsigned long)&q);
++ if (r < 0) {
++ return (int)-r;
++ }
++ return (uint64_t)r == len ? 0 : EIO;
++}
+ int mdevgetrange(int devid, uint64_t *base, uint64_t *size);
+
+ static int
+@@ -228,6 +261,32 @@ mdevrw(dev_t dev, struct uio *uio, __unused int ioflag)
+ if (uio_resid(uio) < 0) {
+ return EINVAL;
+ }
++ if (mdev[devid].mdFlags & mdHost) { /* LINUX64 (xnulinux): host-file disk, through a bounce buffer */
++ uint64_t end = (uint64_t)mdev[devid].mdSize << 12;
++ int write = (uio_rw(uio) == UIO_WRITE);
++ char *bounce = kalloc_data(65536, Z_WAITOK);
++ status = bounce ? 0 : ENOMEM;
++ while (!status && uio_resid(uio) > 0 && (uint64_t)uio->uio_offset < end) {
++ uint64_t off = (uint64_t)uio->uio_offset;
++ count = (int)MIN((uint64_t)uio_resid(uio), MIN(65536ULL, end - off));
++ if (write) {
++ status = uiomove(bounce, count, uio);
++ if (!status) {
++ status = mdev_host_io(1, off, bounce, count);
++ }
++ } else {
++ status = mdev_host_io(0, off, bounce, count);
++ if (!status) {
++ status = uiomove(bounce, count, uio);
++ }
++ }
++ }
++ if (bounce) {
++ kfree_data(bounce, 65536);
++ }
++ return status;
++ }
++
+ mdata = ((addr64_t)mdev[devid].mdBase << 12) + uio->uio_offset; /* Point to the area in "file" */
+
+ saveflag = uio->uio_segflg; /* Remember what the request is */
+@@ -299,6 +358,18 @@ mdevstrategy(struct buf *bp)
+ panic("ramstrategy: buf_map failed");
+ }
+
++ if (mdev[devid].mdFlags & mdHost) { /* LINUX64 (xnulinux): host-file disk */
++ int err = mdev_host_io(!(buf_flags(bp) & B_READ), blkoff, (void *)vaddr, buf_count(bp));
++ buf_unmap(bp);
++ if (err) {
++ buf_seterror(bp, err);
++ } else {
++ buf_setresid(bp, 0);
++ }
++ buf_biodone(bp);
++ return;
++ }
++
+ fvaddr = (mdev[devid].mdBase << 12) + blkoff; /* Point to offset into ram disk */
+
+ if (buf_flags(bp) & B_READ) { /* Is this a read? */
+@@ -708,6 +779,35 @@ mdevadd(int devid, uint64_t base, unsigned int size, int phys)
+ }
+
+
++/* LINUX64 (xnulinux): add the host-file disk (if the host has one) as the first free md unit. */
++dev_t
++mdevadd_host(void)
++{
++ long bytes;
++ dev_t dev;
++ int i;
++
++ if (!linux64_hook) {
++ return -1;
++ }
++ bytes = mdev_host_call(995, 0);
++ if (bytes < 4096) {
++ return -1;
++ }
++ /* a page-number range no RAM disk can overlap: the host disk has no memory behind it */
++ dev = mdevadd(-1, 1ULL << 40, (unsigned int)((uint64_t)bytes >> 12), 0);
++ if (dev == (dev_t)-1) {
++ return dev;
++ }
++ for (i = 0; i < NB_MAX_MDEVICES; i++) {
++ if ((mdev[i].mdFlags & mdInited) && mdev[i].mdBDev == dev) {
++ mdev[i].mdFlags |= mdHost;
++ }
++ }
++ printf("LINUX64: md%x is the host disk (%ld bytes)\n", minor(dev), bytes);
++ return dev;
++}
++
+ dev_t
+ mdevlookup(int devid)
+ {
diff --git a/bsd/kern/bsd_init.c b/bsd/kern/bsd_init.c
index 5362b32..cd43d8e 100644
--- a/bsd/kern/bsd_init.c
@@ -182,6 +328,28 @@ index 76555f6..3fad099 100644
panic("Unable to find driver for this platform: \"%s\".",
platform_name);
+diff --git a/iokit/bsddev/IOKitBSDInit.cpp b/iokit/bsddev/IOKitBSDInit.cpp
+index ae82fc8..3fd837c 100644
+--- a/iokit/bsddev/IOKitBSDInit.cpp
++++ b/iokit/bsddev/IOKitBSDInit.cpp
+@@ -65,6 +65,7 @@ extern "C" {
+
+ extern dev_t mdevadd(int devid, uint64_t base, unsigned int size, int phys);
+ extern dev_t mdevlookup(int devid);
++extern dev_t mdevadd_host(void);
+ extern void mdevremoveall(void);
+ extern int mdevgetrange(int devid, uint64_t *base, uint64_t *size);
+ extern void di_root_ramfile(IORegistryEntry * entry);
+@@ -859,6 +860,9 @@ IOFindBSDRoot( char * rootName, unsigned int rootNameSize,
+ }
+ regEntry->release(); /* Toss the entry */
+ }
++ if (mdevlookup(0) < 0) {
++ (void)mdevadd_host(); /* LINUX64 (xnulinux): no RAM disk, maybe a host-file disk */
++ }
+ }
+
+ //
diff --git a/libkern/crypto/corecrypto_rand.c b/libkern/crypto/corecrypto_rand.c
index d46815b..ccd2d84 100644
--- a/libkern/crypto/corecrypto_rand.c