XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)
C
Host disk upcalls return 0 (a nonzero hook return ends the run): size and I/O results come back through the request
Heyang Zhou0f3d464db44a
diff --git a/xnu-ko/xnu_main.c b/xnu-ko/xnu_main.cindex df2219d..b039e17 100644--- a/xnu-ko/xnu_main.c+++ b/xnu-ko/xnu_main.c@@ -52,7 +52,8 @@ 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; };+/* hook returns must be 0 (nonzero ends the run): results come back through the request */+struct xnu_hostdisk_req { u64_t write, off, len; void *buf; long result; };struct xnu_world xnu_W;static void logs(const char *s) { fki_log(s, x_strlen(s)); }@@ -207,13 +208,17 @@ 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 == 995) { /* host disk size in bytes into *a1, 0 when there is none */+ if (a1) *(u64_t *)a1 = hd_file ? hd_size : 0;+ return 0;+ }+ if (stage == 994) { /* host disk I/O: q->result = bytes moved or -errno */+ struct xnu_hostdisk_req *q = (struct xnu_hostdisk_req *)a1;+ if (!q) return 0;+ if (!hd_file) q->result = -6;+ else if (q->write && !hostdisk_rw) q->result = -30;+ else q->result = fki_file_rw(hd_file, (int)q->write, q->buf, (unsigned long)q->len, q->off);+ return 0;}if (stage == 996) { /* XNU console bytes (/dev/console): line-buffered into the host log */static char line[200]; static unsigned n;diff --git a/xnu-patches/0001-linux64-stage-hook.patch b/xnu-patches/0001-linux64-stage-hook.patchindex 5275fb4..a7a8d0f 100644--- a/xnu-patches/0001-linux64-stage-hook.patch+++ b/xnu-patches/0001-linux64-stage-hook.patch@@ -1,5 +1,5 @@diff --git a/bsd/dev/memdev.c b/bsd/dev/memdev.c-index d24535d..d4f1cc7 100644+index d24535d..0b13601 100644--- a/bsd/dev/memdev.c+++ b/bsd/dev/memdev.c@@ -95,6 +95,7 @@@@ -18,7 +18,7 @@ index d24535d..d4f1cc7 100644int mdevBMajor = -1;int mdevCMajor = -1;-@@ -180,6 +182,37 @@ static int mdevioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct+@@ -180,6 +182,39 @@ static int mdevioctl(dev_t dev, u_long cmd, caddr_t data, int flag, structdev_t mdevadd(int devid, uint64_t base, unsigned int size, int phys);dev_t mdevlookup(int devid);void mdevremoveall(void);@@ -29,7 +29,7 @@ index d24535d..d4f1cc7 100644+ * 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; };++struct mdev_host_req { uint64_t write, off, len; void *buf; long result; }; /* the hook itself must return 0 */++static long+mdev_host_call(unsigned long stage, unsigned long arg)@@ -46,8 +46,10 @@ index d24535d..d4f1cc7 100644+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);++ struct mdev_host_req q = { .write = (uint64_t)write, .off = off, .len = len, .buf = buf, .result = -EIO };++ long r;++ mdev_host_call(994, (unsigned long)&q);++ r = q.result;+ if (r < 0) {+ return (int)-r;+ }@@ -56,7 +58,7 @@ index d24535d..d4f1cc7 100644int 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)+@@ -228,6 +263,32 @@ mdevrw(dev_t dev, struct uio *uio, __unused int ioflag)if (uio_resid(uio) < 0) {return EINVAL;}@@ -89,7 +91,7 @@ index d24535d..d4f1cc7 100644mdata = ((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)+@@ -299,6 +360,18 @@ mdevstrategy(struct buf *bp)panic("ramstrategy: buf_map failed");}@@ -108,7 +110,7 @@ index d24535d..d4f1cc7 100644fvaddr = (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)+@@ -708,6 +781,37 @@ mdevadd(int devid, uint64_t base, unsigned int size, int phys)}@@ -116,6 +118,7 @@ index d24535d..d4f1cc7 100644+dev_t+mdevadd_host(void)+{++ uint64_t size = 0;+ long bytes;+ dev_t dev;+ int i;@@ -123,7 +126,8 @@ index d24535d..d4f1cc7 100644+ if (!linux64_hook) {+ return -1;+ }-+ bytes = mdev_host_call(995, 0);++ mdev_host_call(995, (unsigned long)&size);++ bytes = (long)size;+ if (bytes < 4096) {+ return -1;+ }