Skip to content

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

C

Hosted macOS networking: Ethernet en0 in XNU (bsd/net/if_linux64.c via the ifnet KPI) carried by fki's netdev xnu0 (fki_net_send/recv/pending, upcalls 815-817, idle-wakeup on receive); vm-net-up.sh gives xnu0 192.168.77.1/24 with NAT and dnsmasq; xnu.ko resumes XNU's UBSan soft traps (brk #0xff00) like XNU's own handler. From the hosted macOS: ping, DNS, curl https (HTTP 200), git clone from GitHub, 29 MB download at 47 MB/s

Heyang Zhouc68a54ef1d0c
diff --git a/fki-ko/fki.h b/fki-ko/fki.h
index 4d5ebe8..3c7fc25 100644
--- a/fki-ko/fki.h
+++ b/fki-ko/fki.h
@@ -76,6 +76,13 @@ void fki_idle_until(fki_u64 counter_deadline, fki_u64 max_ns);
void fki_yield(void);
/* The host's wall clock (UTC): seconds and nanoseconds since 1970. */
void fki_realtime(fki_u64 *sec, fki_u64 *nsec);
+/*
+ * A virtual Ethernet link to Linux (the netdev "xnu0"): send one frame (header included, no FCS); receive the next
+ * frame Linux sent (its length, 0 when none waits); how many frames wait.
+ */
+long fki_net_send(const void *frame, unsigned long len);
+long fki_net_recv(void *buf, unsigned long len);
+long fki_net_pending(void);
/* Keep the calling task on one CPU (the hosted kernel sees that CPU's MPIDR). */
int fki_bind_cpu(int cpu);
/*
diff --git a/fki-ko/fki_mod.c b/fki-ko/fki_mod.c
index 12f31bf..f9e37f0 100644
--- a/fki-ko/fki_mod.c
+++ b/fki-ko/fki_mod.c
@@ -32,6 +32,9 @@
#include <linux/sched.h>
#include <linux/fs.h>
#include <asm/stacktrace.h>
+#include <linux/netdevice.h>
+#include <linux/etherdevice.h>
+#include <linux/skbuff.h>
int fki_version(void)
{
@@ -511,6 +514,8 @@ static void fki_linux_runs_body(void (*fn)(void *), void *arg)
struct fki_idle_args { fki_u64 ns; };
+static struct task_struct *fki_idle_task; /* the task sleeping in fki_idle_until, for fki_net's receive wakeup */
+
static void fki_idle_body(void *p)
{
struct fki_idle_args *a = p;
@@ -519,7 +524,9 @@ static void fki_idle_body(void *p)
local_irq_enable();
preempt_enable();
set_current_state(TASK_UNINTERRUPTIBLE);
+ WRITE_ONCE(fki_idle_task, current);
schedule_hrtimeout_range(&t, 50000, HRTIMER_MODE_REL);
+ WRITE_ONCE(fki_idle_task, NULL);
preempt_disable();
local_irq_disable();
}
@@ -567,6 +574,151 @@ void fki_realtime(fki_u64 *sec, fki_u64 *nsec)
}
EXPORT_SYMBOL(fki_realtime);
+/*
+ * A virtual Ethernet link to the foreign kernel: the Linux netdev "xnu0". What Linux transmits on xnu0 is queued for
+ * the foreign kernel (fki_net_recv); what the foreign kernel sends (fki_net_send) arrives on xnu0 as received frames.
+ */
+#define FKI_NET_QLEN 1024
+static struct net_device *fki_netdev;
+static struct sk_buff_head fki_net_rxq; /* Linux -> foreign */
+
+static netdev_tx_t fki_net_xmit(struct sk_buff *skb, struct net_device *dev)
+{
+ struct task_struct *t;
+
+ if (skb_queue_len(&fki_net_rxq) >= FKI_NET_QLEN) {
+ dev->stats.tx_dropped++;
+ dev_kfree_skb_any(skb);
+ return NETDEV_TX_OK;
+ }
+ dev->stats.tx_packets++;
+ dev->stats.tx_bytes += skb->len;
+ skb_queue_tail(&fki_net_rxq, skb);
+ t = READ_ONCE(fki_idle_task);
+ if (t)
+ wake_up_process(t); /* the foreign kernel is idle: let it see the frame now */
+ return NETDEV_TX_OK;
+}
+
+static int fki_net_up(struct net_device *dev) { netif_start_queue(dev); return 0; }
+static int fki_net_down(struct net_device *dev) { netif_stop_queue(dev); return 0; }
+
+static const struct net_device_ops fki_net_ops = {
+ .ndo_open = fki_net_up,
+ .ndo_stop = fki_net_down,
+ .ndo_start_xmit = fki_net_xmit,
+ .ndo_set_mac_address = eth_mac_addr,
+ .ndo_validate_addr = eth_validate_addr,
+};
+
+struct fki_net_io { const void *in; void *out; unsigned long len; long result; };
+
+static void fki_net_send_body(void *p)
+{
+ struct fki_net_io *io = p;
+ struct sk_buff *skb;
+
+ io->result = -ENODEV;
+ if (!fki_netdev || io->len < ETH_HLEN || io->len > ETH_FRAME_LEN + 4)
+ return;
+ skb = netdev_alloc_skb(fki_netdev, io->len + NET_IP_ALIGN);
+ if (!skb) {
+ fki_netdev->stats.rx_dropped++;
+ io->result = -ENOMEM;
+ return;
+ }
+ skb_reserve(skb, NET_IP_ALIGN);
+ skb_put_data(skb, io->in, io->len);
+ skb->protocol = eth_type_trans(skb, fki_netdev);
+ fki_netdev->stats.rx_packets++;
+ fki_netdev->stats.rx_bytes += io->len;
+ local_irq_enable(); /* netif_rx re-enables bottom halves, which must not happen with interrupts off */
+ netif_rx(skb);
+ local_irq_disable();
+ io->result = io->len;
+}
+
+/* The foreign kernel sends one Ethernet frame (header included, no FCS) out of its link: it arrives on xnu0. */
+long fki_net_send(const void *frame, unsigned long len)
+{
+ struct fki_net_io io = { .in = frame, .len = len };
+
+ fki_linux_runs(fki_net_send_body, &io);
+ return io.result;
+}
+EXPORT_SYMBOL(fki_net_send);
+
+static void fki_net_recv_body(void *p)
+{
+ struct fki_net_io *io = p;
+ struct sk_buff *skb = skb_dequeue(&fki_net_rxq);
+
+ io->result = 0;
+ if (!skb)
+ return;
+ if (skb->len > io->len) {
+ io->result = -EMSGSIZE;
+ } else if (skb_copy_bits(skb, 0, io->out, skb->len) == 0) {
+ io->result = skb->len;
+ } else {
+ io->result = -EIO;
+ }
+ dev_consume_skb_any(skb);
+}
+
+/* The next frame Linux sent on xnu0 into buf (at most len bytes): its length, 0 when none is waiting. */
+long fki_net_recv(void *buf, unsigned long len)
+{
+ struct fki_net_io io = { .out = buf, .len = len };
+
+ fki_linux_runs(fki_net_recv_body, &io);
+ return io.result;
+}
+EXPORT_SYMBOL(fki_net_recv);
+
+/* Frames waiting for the foreign kernel. */
+long fki_net_pending(void)
+{
+ return skb_queue_len(&fki_net_rxq);
+}
+EXPORT_SYMBOL(fki_net_pending);
+
+static int __init fki_init(void)
+{
+ struct net_device *dev;
+ int err;
+
+ skb_queue_head_init(&fki_net_rxq);
+ dev = alloc_etherdev(0);
+ if (!dev)
+ return 0; /* no link; the rest of fki still works */
+ strscpy(dev->name, "xnu0", IFNAMSIZ);
+ dev->netdev_ops = &fki_net_ops;
+ eth_hw_addr_random(dev);
+ dev->min_mtu = 68;
+ dev->max_mtu = ETH_DATA_LEN;
+ err = register_netdev(dev);
+ if (err) {
+ free_netdev(dev);
+ pr_warn("fki: no xnu0 link (%d)\n", err);
+ return 0;
+ }
+ fki_netdev = dev;
+ return 0;
+}
+
+static void __exit fki_exit(void)
+{
+ if (fki_netdev) {
+ unregister_netdev(fki_netdev);
+ free_netdev(fki_netdev);
+ fki_netdev = NULL;
+ }
+ skb_queue_purge(&fki_net_rxq);
+}
+module_init(fki_init);
+module_exit(fki_exit);
+
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Foreign kernel interface v0 (out-of-tree form for stock kernels)");
diff --git a/linux/include/linux/fki.h b/linux/include/linux/fki.h
index 4d5ebe8..3c7fc25 100644
--- a/linux/include/linux/fki.h
+++ b/linux/include/linux/fki.h
@@ -76,6 +76,13 @@ void fki_idle_until(fki_u64 counter_deadline, fki_u64 max_ns);
void fki_yield(void);
/* The host's wall clock (UTC): seconds and nanoseconds since 1970. */
void fki_realtime(fki_u64 *sec, fki_u64 *nsec);
+/*
+ * A virtual Ethernet link to Linux (the netdev "xnu0"): send one frame (header included, no FCS); receive the next
+ * frame Linux sent (its length, 0 when none waits); how many frames wait.
+ */
+long fki_net_send(const void *frame, unsigned long len);
+long fki_net_recv(void *buf, unsigned long len);
+long fki_net_pending(void);
/* Keep the calling task on one CPU (the hosted kernel sees that CPU's MPIDR). */
int fki_bind_cpu(int cpu);
/*
diff --git a/scripts/vm-m2-kbisect.sh b/scripts/vm-m2-kbisect.sh
index d029ccd..e3f6ed7 100755
--- a/scripts/vm-m2-kbisect.sh
+++ b/scripts/vm-m2-kbisect.sh
@@ -6,6 +6,7 @@ make -s -C /lib/modules/$(uname -r)/build M=$PWD/xnu-ko clean >/dev/null 2>&1
make -s -C /lib/modules/$(uname -r)/build M=$PWD/xnu-ko KBUILD_EXTRA_SYMBOLS=$PWD/fki-ko/Module.symvers modules >/dev/null 2>&1
sudo dmesg -n ${CONLEVEL:-8}
sudo rmmod xnu 2>/dev/null; sudo rmmod fki 2>/dev/null; sudo insmod fki-ko/fki.ko
+bash scripts/vm-net-up.sh # the hosted macOS's en0: xnu0, NAT, DHCP/DNS
# a hosted XNU never unmounts its read-write root: check the host disk before each boot
if [ -n "$HOSTDISK" ] && [ "${HOSTDISK#/dev/}" = "$HOSTDISK" ]; then
for t in 1 2 3; do r=$(sudo fsck.hfsplus -fy "$HOSTDISK" 2>&1 | tail -1); echo "fsck: $r"; case "$r" in *"appears to be OK"*) break;; esac; done
diff --git a/scripts/vm-net-up.sh b/scripts/vm-net-up.sh
new file mode 100755
index 0000000..e945e8a
--- /dev/null
+++ b/scripts/vm-net-up.sh
@@ -0,0 +1,22 @@
+#!/bin/bash
+# In the VM, after fki.ko is loaded: the host side of the hosted macOS's en0. fki.ko's netdev xnu0 carries en0's
+# frames; Linux routes and NATs 192.168.77.0/24 out of its own uplink, and dnsmasq (when installed) answers DHCP and
+# DNS on xnu0. The hosted system: en0 192.168.77.2/24, router and DNS 192.168.77.1.
+set -u
+ip link show xnu0 >/dev/null 2>&1 || { echo "vm-net-up: no xnu0 (fki.ko without the link)"; exit 0; }
+sudo ip addr replace 192.168.77.1/24 dev xnu0
+sudo ip link set xnu0 up
+sudo sysctl -q -w net.ipv4.ip_forward=1
+up=$(ip route show default | awk '{print $5; exit}')
+sudo iptables -t nat -C POSTROUTING -s 192.168.77.0/24 -o "$up" -j MASQUERADE 2>/dev/null ||
+ sudo iptables -t nat -A POSTROUTING -s 192.168.77.0/24 -o "$up" -j MASQUERADE
+sudo iptables -C FORWARD -i xnu0 -j ACCEPT 2>/dev/null || sudo iptables -I FORWARD -i xnu0 -j ACCEPT
+sudo iptables -C FORWARD -o xnu0 -m state --state RELATED,ESTABLISHED -j ACCEPT 2>/dev/null ||
+ sudo iptables -I FORWARD -o xnu0 -m state --state RELATED,ESTABLISHED -j ACCEPT
+command -v dnsmasq >/dev/null || sudo DEBIAN_FRONTEND=noninteractive apt-get install -y -q dnsmasq-base >/dev/null 2>&1
+if command -v dnsmasq >/dev/null; then
+ sudo pkill -f 'dnsmasq.*xnu0' 2>/dev/null
+ sudo dnsmasq --interface=xnu0 --bind-interfaces --except-interface=lo --dhcp-range=192.168.77.2,192.168.77.50,12h \
+ --dhcp-option=3,192.168.77.1 --dhcp-option=6,192.168.77.1 --pid-file=/run/dnsmasq-xnu0.pid
+fi
+echo "vm-net-up: xnu0 192.168.77.1/24 up, NAT via $up, dnsmasq: $(command -v dnsmasq >/dev/null && echo yes || echo no)"
diff --git a/xnu-ko/xnu_main.c b/xnu-ko/xnu_main.c
index 29bfa1a..564eedf 100644
--- a/xnu-ko/xnu_main.c
+++ b/xnu-ko/xnu_main.c
@@ -74,6 +74,7 @@ static void *ci_file; static fki_u64 ci_off;
struct xnu_cons_req { void *buf; u64_t len; long result; };
/* 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_net_req { void *buf; u64_t len; long result; };
struct xnu_world xnu_W;
static void logs(const char *s) { fki_log(s, x_strlen(s)); }
@@ -247,6 +248,17 @@ static int xnu_fault(unsigned long far, unsigned long esr, void *regs)
if (pc < XNU_VA_LO || pc >= XNU_VA_HI)
return 1;
/* inside copyin/copyout: the routine's recovery path returns EFAULT to its caller, as XNU's own abort handler does */
+ /*
+ * brk #0xff00: XNU's UBSan minimal soft trap (signed overflow). XNU's own exception handler notes it
+ * (ubsan_handle_brk_trap) and resumes after the brk; do the same.
+ */
+ if ((esr >> 26) == 0x3c && (esr & 0xffff) == 0xff00) {
+ static unsigned long ubsan_traps;
+ if (ubsan_traps++ < 8)
+ logkv("xnu: UBSan soft trap (signed overflow), resuming; pc", pc);
+ fki_regs_set_pc(regs, pc + 4);
+ return 0;
+ }
if (xnu_kfault_recover) {
unsigned long rpc = xnu_kfault_recover(pc, far, esr);
if (rpc) {
@@ -297,6 +309,16 @@ static long xnu_hook_body(unsigned long stage, unsigned long a1, unsigned long a
}
return 0;
}
+ if (stage == 815) { /* frames Linux sent on xnu0 waiting for XNU, into *a1 */
+ if (a1) *(u64_t *)a1 = (u64_t)fki_net_pending();
+ return 0;
+ }
+ if (stage == 816 || stage == 817) { /* XNU's Ethernet link: 816 sends q->buf (q->len bytes), 817 receives into it */
+ struct xnu_net_req *q = (struct xnu_net_req *)a1;
+ if (!q) return 0;
+ q->result = stage == 816 ? fki_net_send(q->buf, (unsigned long)q->len) : fki_net_recv(q->buf, (unsigned long)q->len);
+ return 0;
+ }
if (stage == 812) { /* the host's wall clock for XNU's calendar: a1 -> u64[2] {seconds, nanoseconds} UTC */
if (a1) fki_realtime(&((fki_u64 *)a1)[0], &((fki_u64 *)a1)[1]);
return 0;
diff --git a/xnu-patches/0001-linux64-stage-hook.patch b/xnu-patches/0001-linux64-stage-hook.patch
index d0a9bb3..9cbf541 100644
--- a/xnu-patches/0001-linux64-stage-hook.patch
+++ b/xnu-patches/0001-linux64-stage-hook.patch
@@ -1,3 +1,15 @@
+diff --git a/bsd/conf/files b/bsd/conf/files
+index 7a2a873..27843d7 100644
+--- a/bsd/conf/files
++++ b/bsd/conf/files
+@@ -177,6 +177,7 @@ bsd/net/ether_if_module.c optional ether bound-checks
+ bsd/net/ether_inet_pr_module.c optional ether inet bound-checks
+ bsd/net/ether_inet6_pr_module.c optional ether inet bound-checks
+ bsd/net/if_loop.c optional loop bound-checks
++bsd/net/if_linux64.c optional ether
+ bsd/net/if_mib.c optional networking bound-checks
+ bsd/net/if_vlan.c optional vlan bound-checks
+ bsd/net/if_fake.c optional if_fake bound-checks
diff --git a/bsd/dev/arm/systemcalls.c b/bsd/dev/arm/systemcalls.c
index 5a7b717..5c12c8f 100644
--- a/bsd/dev/arm/systemcalls.c
@@ -242,7 +254,7 @@ index ec19ee7..6baf626 100644
}
}
diff --git a/bsd/kern/bsd_init.c b/bsd/kern/bsd_init.c
-index 5362b32..cd43d8e 100644
+index 5362b32..8028c48 100644
--- a/bsd/kern/bsd_init.c
+++ b/bsd/kern/bsd_init.c
@@ -585,7 +585,7 @@ bsd_init(void)
@@ -254,6 +266,18 @@ index 5362b32..cd43d8e 100644
#if DEBUG_BSDINIT
#define bsd_init_kprintf(x, ...) kprintf("bsd_init: " x, ## __VA_ARGS__)
+@@ -963,6 +963,11 @@ bsd_init(void)
+ /* Register the built-in dlil ethernet interface family */
+ bsd_init_kprintf("calling ether_family_init\n");
+ ether_family_init();
++ {
++ /* LINUX64: the hosted kernel's Ethernet interface en0, framed through the host's xnu0 (bsd/net/if_linux64.c) */
++ extern void linux64_net_attach(void);
++ linux64_net_attach();
++ }
+ #endif /* ETHER */
+
+ #if NETWORKING
diff --git a/bsd/kern/kern_descrip.c b/bsd/kern/kern_descrip.c
index fac307f..23e1d06 100644
--- a/bsd/kern/kern_descrip.c
@@ -630,6 +654,214 @@ index c08a74c..1e26f75 100644
353 AUE_GETAUID ALL { int getauid(au_id_t *auid); }
354 AUE_SETAUID ALL { int setauid(au_id_t *auid); }
355 AUE_NULL ALL { int nosys(void); } { old getaudit }
+diff --git a/bsd/net/if_linux64.c b/bsd/net/if_linux64.c
+new file mode 100644
+index 0000000..1f459b3
+--- /dev/null
++++ b/bsd/net/if_linux64.c
+@@ -0,0 +1,202 @@
++/*
++ * LINUX64 (xnulinux): the hosted kernel's Ethernet interface en0.
++ *
++ * Its frames travel through the host: upcall 816 hands an outgoing frame to Linux (it arrives on the Linux netdev
++ * "xnu0"), upcall 817 takes the next frame Linux sent on xnu0, upcall 815 says how many wait. Linux routes, NATs and
++ * answers DHCP on xnu0. A receive thread polls the host (every 2 ms, and at once when the idle loop sees a frame).
++ */
++#include <sys/param.h>
++#include <sys/systm.h>
++#include <sys/kernel.h>
++#include <sys/mbuf.h>
++#include <sys/socket.h>
++#include <sys/sockio.h>
++#include <sys/kpi_mbuf.h>
++#include <net/if.h>
++#include <net/if_types.h>
++#include <net/if_ether.h>
++#include <net/ethernet.h>
++#include <net/kpi_interface.h>
++#include <kern/thread.h>
++#include <kern/locks.h>
++#include <kern/sched_prim.h>
++
++extern uintptr_t linux64_hook;
++extern void linux64_stage_arg(unsigned long, unsigned long);
++
++struct linux64_net_req {
++ void *buf;
++ uint64_t len;
++ long result;
++};
++
++#define L64_FRAME_MAX (ETHER_MAX_LEN + 64)
++
++static ifnet_t l64_ifp;
++static uint8_t l64_txbuf[L64_FRAME_MAX];
++static uint8_t l64_rxbuf[L64_FRAME_MAX];
++static LCK_GRP_DECLARE(l64_lck_grp, "linux64_net");
++static LCK_MTX_DECLARE(l64_tx_lock, &l64_lck_grp);
++static int l64_rx_event;
++
++/* the MAC is locally administered: 02:58:4e:55:00:01 ("XNU") */
++static const uint8_t l64_mac[ETHER_ADDR_LEN] = { 0x02, 0x58, 0x4e, 0x55, 0x00, 0x01 };
++
++static errno_t
++l64_output(ifnet_t ifp, mbuf_t m)
++{
++ size_t len = mbuf_pkthdr_len(m);
++
++ if (len < ETHER_HDR_LEN || len > sizeof(l64_txbuf)) {
++ ifnet_stat_increment_out(ifp, 0, 0, 1);
++ mbuf_freem(m);
++ return 0;
++ }
++ lck_mtx_lock(&l64_tx_lock);
++ if (mbuf_copydata(m, 0, len, l64_txbuf) == 0) {
++ struct linux64_net_req q = { .buf = l64_txbuf, .len = len, .result = 0 };
++ linux64_stage_arg(816, (unsigned long)&q);
++ if (q.result > 0) {
++ ifnet_stat_increment_out(ifp, 1, (uint32_t)len, 0);
++ } else {
++ ifnet_stat_increment_out(ifp, 0, 0, 1);
++ }
++ }
++ lck_mtx_unlock(&l64_tx_lock);
++ mbuf_freem(m);
++ return 0;
++}
++
++static errno_t
++l64_ioctl(ifnet_t ifp, u_long cmd, void *data)
++{
++ switch (cmd) {
++ case SIOCSIFFLAGS:
++ /* the link to the host is always there: running whenever it is up */
++ ifnet_set_flags(ifp, (ifnet_flags(ifp) & IFF_UP) ? IFF_RUNNING : 0, IFF_RUNNING);
++ return 0;
++ case SIOCSIFADDR:
++ case SIOCAIFADDR:
++ case SIOCADDMULTI:
++ case SIOCDELMULTI:
++ return 0;
++ case SIOCSIFMTU: {
++ struct ifreq *ifr = (struct ifreq *)data;
++ if (ifr->ifr_mtu < 68 || ifr->ifr_mtu > ETHERMTU) {
++ return EINVAL;
++ }
++ return ifnet_set_mtu(ifp, (uint32_t)ifr->ifr_mtu);
++ }
++ default:
++ return EOPNOTSUPP;
++ }
++}
++
++static void
++l64_input_one(long len)
++{
++ mbuf_t m = NULL;
++ struct ifnet_stat_increment_param stats = {};
++
++ /* one packet-header mbuf with a 2 KB cluster holds any frame (ETHER_MAX_LEN 1518): no chain to walk */
++ if (len > MCLBYTES || mbuf_getpacket(MBUF_WAITOK, &m) != 0 || m == NULL) {
++ return;
++ }
++ memcpy(mbuf_data(m), l64_rxbuf, (size_t)len);
++ mbuf_setlen(m, (size_t)len);
++ mbuf_pkthdr_setlen(m, (size_t)len);
++ mbuf_pkthdr_setrcvif(m, l64_ifp);
++ mbuf_pkthdr_setheader(m, mbuf_data(m));
++ mbuf_setdata(m, (char *)mbuf_data(m) + ETHER_HDR_LEN, mbuf_len(m) - ETHER_HDR_LEN);
++ mbuf_pkthdr_adjustlen(m, -ETHER_HDR_LEN);
++ stats.packets_in = 1;
++ stats.bytes_in = (uint32_t)len;
++ ifnet_input(l64_ifp, m, &stats);
++}
++
++static void
++l64_rx_thread(__unused void *arg, __unused wait_result_t wr)
++{
++ for (;;) {
++ for (int n = 0; n < 256; n++) {
++ struct linux64_net_req q = { .buf = l64_rxbuf, .len = sizeof(l64_rxbuf), .result = 0 };
++ linux64_stage_arg(817, (unsigned long)&q);
++ if (q.result < ETHER_HDR_LEN) {
++ break;
++ }
++ l64_input_one(q.result);
++ }
++ assert_wait_timeout((event_t)&l64_rx_event, THREAD_UNINT, 2, NSEC_PER_MSEC);
++ thread_block(THREAD_CONTINUE_NULL);
++ }
++}
++
++/* From the hosted idle loop, after the host let it sleep: wake the receive thread when Linux queued frames. */
++void linux64_net_idle_poll(void);
++void
++linux64_net_idle_poll(void)
++{
++ uint64_t waiting = 0;
++
++ if (l64_ifp == NULL) {
++ return;
++ }
++ linux64_stage_arg(815, (unsigned long)&waiting);
++ if (waiting != 0) {
++ thread_wakeup((event_t)&l64_rx_event);
++ }
++}
++
++void linux64_net_attach(void);
++void
++linux64_net_attach(void)
++{
++ struct ifnet_init_eparams init = {};
++ ifnet_t ifp = NULL;
++ thread_t th = THREAD_NULL;
++ errno_t err;
++
++ if (linux64_hook == 0) {
++ return;
++ }
++ init.ver = IFNET_INIT_CURRENT_VERSION;
++ init.len = sizeof(init);
++ init.flags = IFNET_INIT_LEGACY | IFNET_INIT_NX_NOAUTO;
++ init.uniqueid = "linux64-en0";
++ init.uniqueid_len = sizeof("linux64-en0") - 1;
++ init.name = "en";
++ init.unit = 0;
++ init.family = IFNET_FAMILY_ETHERNET;
++ init.type = IFT_ETHER;
++ init.output = l64_output;
++ init.demux = ether_demux;
++ init.add_proto = ether_add_proto;
++ init.del_proto = ether_del_proto;
++ init.check_multi = ether_check_multi;
++ init.framer_extended = ether_frameout_extended;
++ init.ioctl = l64_ioctl;
++ init.broadcast_addr = etherbroadcastaddr;
++ init.broadcast_len = ETHER_ADDR_LEN;
++ err = ifnet_allocate_extended(&init, &ifp);
++ if (err != 0) {
++ printf("linux64: en0 allocate failed %d\n", err);
++ return;
++ }
++ ifnet_set_mtu(ifp, ETHERMTU);
++ ifnet_set_hdrlen(ifp, ETHER_HDR_LEN);
++ ifnet_set_flags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST | IFF_RUNNING, 0xffff);
++ ifnet_set_baudrate(ifp, 1000ULL * 1000 * 1000);
++ err = ifnet_attach(ifp, NULL);
++ if (err != 0) {
++ printf("linux64: en0 attach failed %d\n", err);
++ ifnet_release(ifp);
++ return;
++ }
++ ifnet_set_lladdr(ifp, l64_mac, sizeof(l64_mac));
++ l64_ifp = ifp;
++ if (kernel_thread_start(l64_rx_thread, NULL, &th) == KERN_SUCCESS) {
++ thread_deallocate(th);
++ }
++ printf("linux64: en0 attached (%02x:%02x:%02x:%02x:%02x:%02x), frames through the host's xnu0\n",
++ l64_mac[0], l64_mac[1], l64_mac[2], l64_mac[3], l64_mac[4], l64_mac[5]);
++}
diff --git a/bsd/pthread/pthread_shims.c b/bsd/pthread/pthread_shims.c
index e5e58b5..6e6596d 100644
--- a/bsd/pthread/pthread_shims.c
@@ -1858,7 +2090,7 @@ index 84deb3f..767b8f8 100644
.macro BRANCH_EXTERN
diff --git a/osfmk/arm64/cpu.c b/osfmk/arm64/cpu.c
-index d10d7a1..15e2fb4 100644
+index d10d7a1..7f195a2 100644
--- a/osfmk/arm64/cpu.c
+++ b/osfmk/arm64/cpu.c
@@ -100,6 +100,7 @@ extern void __attribute__((noreturn)) arm64_prepare_for_sleep(boolean_t deep_sle
@@ -1882,7 +2114,7 @@ index d10d7a1..15e2fb4 100644
if (unmask) {
/* Latch SW IRQ+FIQ counter prior to unmasking
* interrupts.
-@@ -506,6 +513,34 @@ cpu_idle(void)
+@@ -506,6 +513,35 @@ cpu_idle(void)
uint64_t new_idle_timeout_ticks = 0x0ULL, lastPop;
bool idle_disallowed = false;
@@ -1908,6 +2140,7 @@ index d10d7a1..15e2fb4 100644
+ }
+ }
+ linux64_stage_arg(810, deadline);
++ { extern void linux64_net_idle_poll(void); linux64_net_idle_poll(); } /* frames from the host: wake en0's receive thread */
+ cpu_data_ptr->cpu_decrementer = -1;
+ rtclock_intr(FALSE);
+ ClearIdlePop(TRUE);
@@ -1917,7 +2150,7 @@ index d10d7a1..15e2fb4 100644
if (__improbable((!idle_enable))) {
idle_disallowed = true;
os_atomic_inc(&virt_idle_disallowed[0], relaxed);
-@@ -570,7 +605,9 @@ cpu_idle(void)
+@@ -570,7 +606,9 @@ cpu_idle(void)
}
}
@@ -1928,7 +2161,7 @@ index d10d7a1..15e2fb4 100644
if (idle_wfe_poll && idle_proximate_timer_wfe == 1) {
/* Poll issuing WFEs until the expected
* timer FIQ arrives.
-@@ -614,6 +651,28 @@ cpu_idle(void)
+@@ -614,6 +652,29 @@ cpu_idle(void)
cpc_cpu_transition(CPC_CPU_IDLE, cpu_data_ptr);
#endif /* CONFIG_CPU_COUNTERS */
@@ -1947,6 +2180,7 @@ index d10d7a1..15e2fb4 100644
+ }
+ { extern unsigned long linux64_dbg[16]; linux64_dbg[3]++; } /* LINUX64 debug */
+ linux64_stage_arg(810, deadline);
++ { extern void linux64_net_idle_poll(void); linux64_net_idle_poll(); } /* frames from the host: wake en0's receive thread */
+ cpu_data_ptr->cpu_decrementer = -1;
+ rtclock_intr(FALSE);
+ ClearIdlePop(TRUE);
@@ -1957,7 +2191,7 @@ index d10d7a1..15e2fb4 100644
if (wfi) {
#if !defined(APPLE_ARM64_ARCH_FAMILY)
platform_cache_idle_enter();
-@@ -735,20 +794,27 @@ cpu_idle_exit(boolean_t from_reset)
+@@ -735,20 +796,27 @@ cpu_idle_exit(boolean_t from_reset)
void
cpu_init(void)
{
@@ -1985,7 +2219,7 @@ index d10d7a1..15e2fb4 100644
} else {
/*
* We initialize non-boot CPUs here; the boot CPU is
-@@ -758,9 +824,11 @@ cpu_init(void)
+@@ -758,9 +826,11 @@ cpu_init(void)
}
do_cacheid();
@@ -1997,7 +2231,7 @@ index d10d7a1..15e2fb4 100644
cpu_info_p = cpuid_info();
-@@ -780,6 +848,7 @@ cpu_init(void)
+@@ -780,6 +850,7 @@ cpu_init(void)
}
cdp->cpu_threadtype = CPU_THREADTYPE_NONE;