diff --git a/NOTICE b/NOTICE index 9644c815c..a7f1251f4 100644 --- a/NOTICE +++ b/NOTICE @@ -139,4 +139,8 @@ Fetched at build time, never committed: His patches under tools/mesa/panvk/ change Mesa (MIT) and those under tools/metal/m2v/ change metal2vulkan (GPL-2.0-or-later OR LGPL-3.0-or-later), each under the licence of the code it changes, as their headers say. - Nils (github.com/dwaris): his commits to this repository, from 12 November 2026 (drivers/audio/abox/hakios.exynos_abox.c, - drivers/audio/codec/hakios.max98512.c), under the licence of the files they change. + drivers/audio/codec/hakios.max98512.c; from 18 November also drivers/camera/exynos/hakios.exynos_camera_star2.c, + userland/src/camera_device.c and userland/src/inject_media.c), under the licence of the files they change. +- deLiseLINO (github.com/deLiseLINO): his commits to this repository, from 18 November 2026 (avf/shim/avfboot.c, + avf/shim/start.S, userland/src/eagl.c, drivers/virtio/hakios.virtio_net.c, avf/scion/avf.properties), under the licence + of the files they change. diff --git a/README.md b/README.md index 05c687a6f..3821c9ed0 100644 --- a/README.md +++ b/README.md @@ -225,7 +225,14 @@ from your own device. - **Nils** ([@dwaris](https://github.com/dwaris)), a Galaxy S9+ owner and one of the installer's testers. Two fixes for the speakers found on his own phone: the first sound no longer lost when the stream starts before the amplifiers' driver has registered, and the MAX98512's enable given a moment before it is read back, with a pause between a bus transfer's - retries. His commits are in the history under his name. Thank you, Nils. + retries. Then four for the S9+'s cameras: the S5K3M3 telephoto's two virtual channels told apart so its frames are + real, the telephoto's availability read again after a failure, and the capture graph's audio ended cleanly when the + camera is switched (end-of-stream markers sent, late samples and stale notifications dropped). His commits are in the + history under his name. Thank you, Nils. +- **deLiseLINO** ([@deLiseLINO](https://github.com/deLiseLINO)), who runs Scion in AVF on a Pixel 7: the boot hang there + found and fixed (FP/SIMD turned on before the jump to XNU), CoreAnimation's masks drawn on the Mali-G710 (GLSL ES 1.00 + extension lines dropped from ES 3.00 shaders), and the guest's network as a stream on the virtio socket device, served by + libslirp in the Scion app (`virtio_net_vsock=PORT`). His commits are in the history under his name. Thank you. ## Licenses and the legal side diff --git a/avf/scion/avf.properties b/avf/scion/avf.properties index 2e921a79f..0961a263d 100644 --- a/avf/scion/avf.properties +++ b/avf/scion/avf.properties @@ -13,9 +13,9 @@ disk.0=system.apfs # gl=1: CoreAnimation draws through OpenGL ES on the phone's GPU (userland/src/eagl.c over gfx, # gfxstream through /dev/vgpu3d), which needs a GPU below (gpu=scgpu or gpu=gfxstream). Without gl=1 it draws in # software. -# virtio_net_wifi=1: with Scion's "Network" setting on, AVF adds a virtio-net card (Android's VM -# tethering behind it) and iOS sees it as Wi-Fi, joined to the open network "Scion" -# (bsd/dev/hakios.virtio_net.c). Without the setting there is no card and it does nothing. +# virtio_net_wifi=1: with Scion's "Network" setting on, the app adds virtio_net_vsock=PORT and serves +# the guest's en0 over a virtio socket stream (libslirp in the app), and iOS sees it as Wi-Fi, joined +# to the open network "Scion" (bsd/dev/hakios.virtio_net.c). Without the setting there is no link. params=md0=rvblk0 cpus=8 gl=1 virtio_net_wifi=1 memory_mib=4096 cpus=host diff --git a/avf/shim/avfboot.c b/avf/shim/avfboot.c index 915cd2421..7e28775f3 100644 --- a/avf/shim/avfboot.c +++ b/avf/shim/avfboot.c @@ -1088,6 +1088,17 @@ void avfboot_main(uint64_t fdt_pa) uart_puts("\n\n"); stage = "xnu"; + /* + * FP/SIMD on before the kernel runs, on whichever core it starts (here or in avfboot_hop). + * XNU's start.s turns it on itself (CPACR_EL1.FPEN in common_start, MMU on), and its first FP + * instruction (cpu_data_init(), from arm_init()) should then run. On the Pixel 7 (pKVM) + * it took an "FP/SIMD access trapped" exception instead (ESR 0x1fe00000) with CPACR_EL1 + * reading 0x300000 in the vector. With no thread pointer yet, sleh_synchronous() faulted on it, + * and so on until the exception stack ran into the interrupt stack: the vCPU spun at 100%, + * nothing on the console. Half the boots at first, all of them later the same day. With FPEN + * set here, before the jump, the same kernel boots every time. + */ + __asm__ volatile("msr cpacr_el1, %0" : : "r"(3ULL << 20)); __asm__ volatile("dsb sy; ic iallu; dsb sy; isb" ::: "memory"); if (m.mpidr[0] != m.self) { /* the kernel starts on the big core (see "fdt: cpus"); this one switches itself off */ diff --git a/avf/shim/start.S b/avf/shim/start.S index 5f25c9fa9..266630946 100644 --- a/avf/shim/start.S +++ b/avf/shim/start.S @@ -71,11 +71,13 @@ _start: /* * A core started with PSCI CPU_ON (avfboot_main hands the kernel to the biggest core): EL1, * MMU and caches off like the boot core. Jumps to hop_entry with x0 = hop_arg, as the boot - * core would have. + * core would have, with FP/SIMD on (CPACR_EL1.FPEN: see the jump at the end of avfboot_main). */ .globl avfboot_hop avfboot_hop: msr daifset, #0xf + mov x1, #(3 << 20) /* CPACR_EL1.FPEN: no traps at EL1 or EL0 */ + msr cpacr_el1, x1 adrp x1, hop_entry ldr x1, [x1, :lo12:hop_entry] adrp x0, hop_arg diff --git a/configs/boot-args.lst b/configs/boot-args.lst index 7de0bb8ac..decd314d0 100644 --- a/configs/boot-args.lst +++ b/configs/boot-args.lst @@ -653,6 +653,7 @@ virtio_input k virtio_mmio k virtio_net k virtio_net_ssid k +virtio_net_vsock k virtio_net_wifi k virtio_poll k virtio_sound k diff --git a/configs/common_defconfig b/configs/common_defconfig index dfed81c3a..a614358ff 100644 --- a/configs/common_defconfig +++ b/configs/common_defconfig @@ -2,7 +2,7 @@ # HakiOS common defconfig: every setting there is, with what it does. # Made by tools/hakios-config.py listing common -o ; the values are the ones that file sets, and tests/CI check that it names every symbol. # -# 765 symbols in 20 groups; 54 are set, 45 of them drivers built in. +# 766 symbols in 20 groups; 54 are set, 45 of them drivers built in. # # @@ -181,7 +181,7 @@ CONFIG_HAKIOS_VIRTIO_SOUND=y # QEMU's virtio tablet and keyboard as the touch screen and the buttons (virtio_input=1). QEMU only [hakios.virtio_input.c] CONFIG_HAKIOS_VIRTIO_INPUT=y -# QEMU's virtio network device as the link behind en0 (virtio_net=1). QEMU only [hakios.virtio_net.c; requires CONFIG_HAKIOS_WIFI_STA] +# QEMU's virtio network device as the link behind en0 (virtio_net=1), or on AVF a stream to the Scion app on the ... [hakios.virtio_net.c; requires CONFIG_HAKIOS_WIFI_STA] CONFIG_HAKIOS_VIRTIO_NET=y # a disk image of the host as /dev/rvblk0 (virtio_blk=1). QEMU only [hakios.virtio_blk.c] @@ -1590,7 +1590,7 @@ CONFIG_MODEM_AGAIN=y # CONFIG_TMU_UNGATE is not set # -# Network (5) +# Network (6) # # a log line for each of the first N frames on the network interfaces @@ -1605,6 +1605,9 @@ CONFIG_MODEM_AGAIN=y # read by hakios.virtio_net.c (kernel) # CONFIG_VIRTIO_NET_SSID is not set +# read by hakios.virtio_net.c (kernel) +# CONFIG_VIRTIO_NET_VSOCK is not set + # iOS is told that en0 is a radio, the way the phone's is (hakios.net.c, hakios.wlan.cpp, hakios.apple80211.c), and a ... # CONFIG_VIRTIO_NET_WIFI is not set diff --git a/configs/symbols.tsv b/configs/symbols.tsv index 20fa06123..e9583f2d4 100644 --- a/configs/symbols.tsv +++ b/configs/symbols.tsv @@ -615,6 +615,7 @@ VIRTIO_CONSOLE arg virtio_console User space k hakios.virtio_console.c none. Wit VIRTIO_INPUT arg virtio_input Touch and buttons k hakios.virtio_input.c VIRTIO_MMIO arg virtio_mmio User space k hakios.virtio.c VIRTIO_NET_SSID arg virtio_net_ssid Network k hakios.virtio_net.c +VIRTIO_NET_VSOCK arg virtio_net_vsock Network k hakios.virtio_net.c VIRTIO_NET_WIFI arg virtio_net_wifi Network k hakios.virtio_net.c iOS is told that en0 is a radio, the way the phone's is (hakios.net.c, hakios.wlan.cpp, hakios.apple80211.c), and a station of this file's stands VIRTIO_POLL arg virtio_poll User space k hakios.virtio.c VIRTIO_SOUND arg virtio_sound Audio and battery k hakios.virtio_sound.c diff --git a/docs/boot-args.md b/docs/boot-args.md index f4029b64b..898b730dc 100644 --- a/docs/boot-args.md +++ b/docs/boot-args.md @@ -2,7 +2,7 @@ Made by `tools/boot-args.py --markdown`; CI fails if it is out of date with the source (`tools/boot-args.py --check`). -Every argument that the kernel, the loader or user space of this port reads, and which file reads it. **707 arguments**: 615 read by the kernel, 24 by the loader, 85 by user space (one is read in more than one place). +Every argument that the kernel, the loader or user space of this port reads, and which file reads it. **708 arguments**: 616 read by the kernel, 24 by the loader, 85 by user space (one is read in more than one place). This is a catalogue of what exists and who reads it, not a manual. What an argument means is in the head of the file that reads it. The stable switches have a symbol in the configuration (`tools/hakios-config.py help SYMBOL`, `docs/configs.md`); the rest are the knobs of bring-up (a driver's steps, pauses, traces, test modes) and can change with the driver. Any of them can be given in a configuration with `CONFIG_BOOTARGS="name=value ..."`, which checks that each name is in this list. What the arguments of the two Metal paths do: [Metal on the Adreno](reference/metal.md#boot-arguments). @@ -477,7 +477,7 @@ Layers: **K** the kernel, **L** the loader, **U** user space (through `sysctl ke | `virtio_blk` | K | `hakios.virtio_blk.c` | `CONFIG_VIRTIO_BLK` | | | `virtio_blk_rw` | K | `hakios.virtio_blk.c` | `CONFIG_VIRTIO_BLK_RW` | | -## Network (5) +## Network (6) | Argument | Layer | Read by | Configuration | Set by | |---|---|---|---|---| @@ -485,6 +485,7 @@ Layers: **K** the kernel, **L** the loader, **U** user space (through `sysctl ke | `net_trace` | K | `hakios.net.c` | `CONFIG_NET_TRACE` | | | `virtio_net` | K | `hakios.virtio_net.c` | `CONFIG_VIRTIO_NET` | | | `virtio_net_ssid` | K | `hakios.virtio_net.c` | `CONFIG_VIRTIO_NET_SSID` | | +| `virtio_net_vsock` | K | `hakios.virtio_net.c` | `CONFIG_VIRTIO_NET_VSOCK` | | | `virtio_net_wifi` | K | `hakios.virtio_net.c` | `CONFIG_VIRTIO_NET_WIFI` | | ## USB (10) diff --git a/docs/reference/drivers.md b/docs/reference/drivers.md index 80e5ec125..3af2d0d41 100644 --- a/docs/reference/drivers.md +++ b/docs/reference/drivers.md @@ -163,7 +163,7 @@ Every driver the kernel can be built with. `CONFIG_HAKIOS_=y` in a defconf |---|---|---|---|---|---|---|---|---|---|---| | **`HAKIOS_VIRTIO_SOUND`**
QEMU's virtio sound device as the player of /dev/hakios-audio (virtio_sound=1). QEMU only | `hakios.virtio_sound.c` | | `virtio_sound=` | | | | built | built | | | | | **`HAKIOS_VIRTIO_INPUT`**
QEMU's virtio tablet and keyboard as the touch screen and the buttons (virtio_input=1). QEMU only | `hakios.virtio_input.c` | | `virtio_input=` | | | | built | built | | | | -| **`HAKIOS_VIRTIO_NET`**
QEMU's virtio network device as the link behind en0 (virtio_net=1). QEMU only | `hakios.virtio_net.c` | `HAKIOS_WIFI_STA` | `virtio_net=`, `virtio_net_ssid=`, `virtio_net_wifi=` | | | | built | built | | | | +| **`HAKIOS_VIRTIO_NET`**
QEMU's virtio network device as the link behind en0 (virtio_net=1), or on AVF a stream to the Scion app on the virtio socket device (virtio_net_vsock=PORT) | `hakios.virtio_net.c` | `HAKIOS_WIFI_STA` | `virtio_net=`, `virtio_net_ssid=`, `virtio_net_vsock=`, `virtio_net_wifi=` | | | | built | built | | | | | **`HAKIOS_VIRTIO_BLK`**
a disk image of the host as /dev/rvblk0 (virtio_blk=1). QEMU only | `hakios.virtio_blk.c` | | `virtio_blk=`, `virtio_blk_rw=` | | | | built | built | | | | ## wifi diff --git a/drivers/camera/exynos/hakios.exynos_camera_star2.c b/drivers/camera/exynos/hakios.exynos_camera_star2.c index eca422095..6298b999e 100644 --- a/drivers/camera/exynos/hakios.exynos_camera_star2.c +++ b/drivers/camera/exynos/hakios.exynos_camera_star2.c @@ -1025,7 +1025,8 @@ paths_init(void) .name = "the telephoto camera (S5K3M3 on CSIS2)", .sensor = TELE_SENSOR, .bus = &tele_bus, .addr = &tele_addr, .csis_pa = TELE_CSIS_PA, .phy_pa = TELE_PHY_PA, .wdma_pa = TELE_WDMA_PA, .phy_size = 0x4000, .phy_off = 0x1400, .iso_off = TELE_ISO, .reset_bit = 1, .iso_shared = false, .bias = false, - .lanes = 4, .settle = TELE_SETTLE, .interleave = 0, .w = 2016, .h = 1512, .slot_bytes = 6u << 20, + /* CSI_MODE_VC_ONLY is 2: VC0 carries pixels and VC1 carries the PDAF tail. */ + .lanes = 4, .settle = TELE_SETTLE, .interleave = 2, .w = 2016, .h = 1512, .slot_bytes = 6u << 20, .mode_lines = S3M3_MODE_LINES, .margin = TELE_MARGIN, .line_ns = S3M3_MODE_LINE_NS, .bayer = 1, .turn = false, .exp = 1000, .gain = 0x80, }; diff --git a/drivers/virtio/conf b/drivers/virtio/conf index eca8b3961..3bc372734 100644 --- a/drivers/virtio/conf +++ b/drivers/virtio/conf @@ -21,7 +21,7 @@ driver VIRTIO_INPUT driver VIRTIO_NET file bsd/dev/hakios.virtio_net.c requires WIFI_STA - help QEMU's virtio network device as the link behind en0 (virtio_net=1). QEMU only + help QEMU's virtio network device as the link behind en0 (virtio_net=1), or on AVF a stream to the Scion app on the virtio socket device (virtio_net_vsock=PORT) driver VIRTIO_BLK file bsd/dev/hakios.virtio_blk.c diff --git a/drivers/virtio/hakios.virtio_net.c b/drivers/virtio/hakios.virtio_net.c index 0d20984cb..b69b7c7b2 100644 --- a/drivers/virtio/hakios.virtio_net.c +++ b/drivers/virtio/hakios.virtio_net.c @@ -15,9 +15,23 @@ * behind it (the station boundary, sys/hakios.sta.h). It hears one open network, "Scion" (or * virtio_net_ssid=NAME), joins it at the start and whenever iOS asks, and then lets frames * pass. So Settings shows Wi-Fi on and that network joined, configd configures the interface - * by itself (a "Wi-Fi" network service, DHCP), and the status bar has the Wi-Fi bars. Under - * AVF (Scion's "Network" setting) the cable behind it is Android's VM tethering. + * by itself (a "Wi-Fi" network service, DHCP), and the status bar has the Wi-Fi bars. + * + * Boot-arg virtio_net_vsock=PORT: the link is a stream on the virtio socket device instead (AVF + * gives every guest one; Scion's "Network" setting, since the phone's crosvm can't be given a + * network card). The guest listens on PORT; the Scion app connects (VirtualMachine.connectVsock) + * and runs a user-mode network stack on its end (libslirp: DHCP, DNS and NAT through the app's + * own sockets). One structure carries the frames, both ways: + * + * u32 length, big-endian (1..HAKIOS_NET_FRAME_MAX), then that many bytes: the Ethernet frame + * + * repeated for as long as the connection lasts (QEMU's "-netdev stream" framing). The stream + * cuts the bytes into packets as it likes, so the receiver puts frames back together; anything + * else (a length of 0 or over the maximum) ends the connection. A new connection replaces the + * old one: the app connects again whenever it has lost the stream. No interrupt either: the + * interface's thread asks (vsock_poll()). Virtio 1.2, section 5.10, for the socket device. */ +#include #include #include #include @@ -27,6 +41,7 @@ #include #include #include +#include #include #include #include @@ -180,6 +195,371 @@ static struct hakios_net_link virtio_link = { .poll = virtio_poll, }; +/* ---- virtio_net_vsock=PORT: the frames on a stream of the virtio socket device ---------------------------- */ + +#define VSOCK_QUEUE_RECEIVE 0 +#define VSOCK_QUEUE_TRANSMIT 1 +#define VSOCK_QUEUE_EVENT 2 +#define VSOCK_QUEUE_SIZE 64 /* descriptors, and buffers, per queue */ +#define VSOCK_BUFFER 4096 /* one packet: its header and up to 4052 bytes of the stream; a page */ +#define VSOCK_EVENTS 4 +#define VSOCK_EVENT_SIZE 8 +#define VSOCK_CFG_GUEST_CID 0x00 +#define VSOCK_TYPE_STREAM 1 +#define VSOCK_OP_REQUEST 1 +#define VSOCK_OP_RESPONSE 2 +#define VSOCK_OP_RST 3 +#define VSOCK_OP_SHUTDOWN 4 +#define VSOCK_OP_RW 5 +#define VSOCK_OP_CREDIT_UPDATE 6 +#define VSOCK_OP_CREDIT_REQUEST 7 +#define VSOCK_EVENT_TRANSPORT_RESET 0 +/* the room the peer is told the guest has: what arrives is handed up at once, so only a limit on what is in flight */ +#define VSOCK_BUF_ALLOC (256 * 1024) +#define VSOCK_PREFIX 4 /* the frame's length, before each frame */ + +/* before every packet, either way (little-endian, as the CPU) */ +struct vsock_header { + uint64_t src_cid, dst_cid; + uint32_t src_port, dst_port, len; + uint16_t type, op; + uint32_t flags, buf_alloc, fwd_cnt; +} __attribute__((packed)); + +struct vsock_queue { + vio_queue_t * vq; + uint8_t * buffers; /* VSOCK_QUEUE_SIZE of VSOCK_BUFFER */ + uint64_t buffers_pa; + uint16_t free[VSOCK_QUEUE_SIZE]; /* transmit: the buffers the device does not have */ + unsigned free_count; +}; + +static LCK_GRP_DECLARE(vsock_lck_grp, "hakios-vsock-net"); +/* the transmit queue and the connection: transmit (the stack's threads) and poll (the interface's) both send */ +static LCK_MTX_DECLARE(vsock_lock, &vsock_lck_grp); + +static struct { + vio_dev_t * dev; + struct vsock_queue rx, tx; + vio_queue_t * events; + uint8_t * event_buffers; + uint64_t event_pa; + uint64_t cid; /* the guest's */ + uint32_t port; /* the one listened on */ + bool connected; + uint64_t peer_cid; + uint32_t peer_port; + uint32_t peer_buf_alloc, peer_fwd_cnt; /* the peer's room, as it last said */ + uint32_t sent; /* stream bytes sent on this connection */ + uint32_t received; /* stream bytes received, all handed on at once */ + uint32_t received_said; /* "received" as the peer was last told it */ + bool credit_asked; + uint8_t frame[VSOCK_PREFIX + HAKIOS_NET_FRAME_MAX]; /* the frame being put together */ + uint32_t have; + uint64_t connections, no_room, no_app; +} vs; + +static void +vsock_give(struct vsock_queue *q, uint16_t id, uint32_t len, bool device_writes) +{ + struct vio_sg sg = { q->buffers_pa + (uint64_t)id * VSOCK_BUFFER, len, device_writes }; + + (void)vio_queue_add(q->vq, &sg, 1, COOKIE(id)); +} + +static void +vsock_give_event(uint16_t id) +{ + struct vio_sg sg = { vs.event_pa + (uint64_t)id * VSOCK_EVENT_SIZE, VSOCK_EVENT_SIZE, true }; + + (void)vio_queue_add(vs.events, &sg, 1, COOKIE(id)); +} + +static bool +vsock_queue_make(struct vsock_queue *q, uint16_t index, bool device_writes) +{ + int error; + + if ((error = vio_queue_setup(vs.dev, index, VSOCK_QUEUE_SIZE, &q->vq)) != 0) { + printf("net: virtio socket queue %u: %d\n", index, error); + return false; + } + if ((q->buffers = vio_dma_alloc(VSOCK_QUEUE_SIZE * VSOCK_BUFFER, &q->buffers_pa)) == NULL) { + return false; + } + for (uint16_t i = 0; i < vio_queue_size(q->vq); i++) { + if (device_writes) { + vsock_give(q, i, VSOCK_BUFFER, true); + } else { + q->free[q->free_count++] = i; + } + } + return true; +} + +/* + * One packet from port `from` to (cid, port), with vsock_lock held: `head_length` bytes of `head`, then `length` + * bytes of `packet` (if not NULL) after the header. False if no buffer was free. + */ +static bool +vsock_send(uint16_t op, uint32_t from, uint64_t cid, uint32_t port, const void *head, size_t head_length, + mbuf_t packet, size_t length) +{ + struct vsock_queue *q = &vs.tx; + struct vsock_header h = { + .src_cid = vs.cid, .dst_cid = cid, .src_port = from, .dst_port = port, + .len = (uint32_t)(head_length + length), .type = VSOCK_TYPE_STREAM, .op = op, + .buf_alloc = VSOCK_BUF_ALLOC, .fwd_cnt = vs.received, + }; + uint8_t *buffer; + uint16_t id; + void *done; + + while ((done = vio_queue_get(q->vq, NULL)) != NULL) { + q->free[q->free_count++] = BUFFER_OF(done); + } + if (q->free_count == 0 || sizeof(h) + head_length + length > VSOCK_BUFFER) { + return false; + } + id = q->free[--q->free_count]; + buffer = q->buffers + (size_t)id * VSOCK_BUFFER; + memcpy(buffer, &h, sizeof(h)); + if (head_length != 0) { + memcpy(buffer + sizeof(h), head, head_length); + } + if (packet != NULL && mbuf_copydata(packet, 0, length, buffer + sizeof(h) + head_length) != 0) { + q->free[q->free_count++] = id; + return false; + } + vs.received_said = vs.received; + vsock_give(q, id, (uint32_t)(sizeof(h) + head_length + length), false); + vio_queue_kick(q->vq); + return true; +} + +/* A control packet on the connection (vsock_lock held). */ +static void +vsock_tell(uint16_t op) +{ + (void)vsock_send(op, vs.port, vs.peer_cid, vs.peer_port, NULL, 0, NULL, 0); +} + +/* The connection is over (vsock_lock held). */ +static void +vsock_drop(const char *why) +{ + if (vs.connected) { + printf("net: vsock: the app's stream on port %u ended: %s\n", vs.port, why); + } + vs.connected = false; + vs.have = 0; +} + +/* A frame to send: its length, then the frame, as one packet. */ +static bool +vsock_transmit(mbuf_t packet, size_t length) +{ + uint8_t prefix[VSOCK_PREFIX] = { (uint8_t)(length >> 24), (uint8_t)(length >> 16), (uint8_t)(length >> 8), (uint8_t)length }; + bool sent = false; + + lck_mtx_lock(&vsock_lock); + if (!vs.connected) { + vs.no_app++; + } else if ((int64_t)vs.peer_buf_alloc - (int64_t)(uint32_t)(vs.sent - vs.peer_fwd_cnt) < (int64_t)(VSOCK_PREFIX + length)) { + vs.no_room++; /* the app has not read what it has; dropped (TCP sends again), and ask once */ + if (!vs.credit_asked) { + vsock_tell(VSOCK_OP_CREDIT_REQUEST); + vs.credit_asked = true; + } + } else if ((sent = vsock_send(VSOCK_OP_RW, vs.port, vs.peer_cid, vs.peer_port, prefix, sizeof(prefix), packet, length))) { + vs.sent += (uint32_t)(VSOCK_PREFIX + length); + } + lck_mtx_unlock(&vsock_lock); + return sent; +} + +/* Bytes of the stream, in order: the frames in them handed up. False if they are not frames. Poll's thread only. */ +static bool +vsock_stream(const uint8_t *p, uint32_t n) +{ + while (n > 0) { + uint32_t want = VSOCK_PREFIX, take; + + if (vs.have >= VSOCK_PREFIX) { + uint32_t length = (uint32_t)vs.frame[0] << 24 | (uint32_t)vs.frame[1] << 16 | (uint32_t)vs.frame[2] << 8 | vs.frame[3]; + + if (length == 0 || length > HAKIOS_NET_FRAME_MAX) { + return false; + } + want = VSOCK_PREFIX + length; + } + take = MIN(n, want - vs.have); + memcpy(vs.frame + vs.have, p, take); + vs.have += take; + p += take; + n -= take; + if (vs.have == want && want > VSOCK_PREFIX) { + hakios_net_input(vs.frame + VSOCK_PREFIX, want - VSOCK_PREFIX); + vs.have = 0; + } + } + return true; +} + +/* One packet from the device. Poll's thread only (the only one that changes the connection). */ +static void +vsock_packet(const struct vsock_header *h, const uint8_t *payload, uint32_t length) +{ + bool ours = vs.connected && h->src_cid == vs.peer_cid && h->src_port == vs.peer_port; + bool frames; + + lck_mtx_lock(&vsock_lock); + if (h->dst_port != vs.port || h->type != VSOCK_TYPE_STREAM || (!ours && h->op != VSOCK_OP_REQUEST)) { + if (h->op != VSOCK_OP_RST) { /* nobody listens there, or a connection that is gone */ + (void)vsock_send(VSOCK_OP_RST, h->dst_port, h->src_cid, h->src_port, NULL, 0, NULL, 0); + } + lck_mtx_unlock(&vsock_lock); + return; + } + if (h->op == VSOCK_OP_REQUEST) { + if (vs.connected) { + vsock_tell(VSOCK_OP_RST); + vsock_drop("the app connected again"); + } + vs.connected = true; + vs.peer_cid = h->src_cid; + vs.peer_port = h->src_port; + vs.sent = vs.received = vs.received_said = 0; + vs.have = 0; + vs.connections++; + } + vs.peer_buf_alloc = h->buf_alloc; + vs.peer_fwd_cnt = h->fwd_cnt; + vs.credit_asked = false; + switch (h->op) { + case VSOCK_OP_REQUEST: + vsock_tell(VSOCK_OP_RESPONSE); + printf("net: vsock: the app connected from cid %llu port %u; frames pass on port %u\n", h->src_cid, h->src_port, vs.port); + break; + case VSOCK_OP_RW: + vs.received += length; + lck_mtx_unlock(&vsock_lock); + frames = vsock_stream(payload, length); /* (the stack is not entered with the lock held) */ + lck_mtx_lock(&vsock_lock); + if (!frames && vs.connected) { + vsock_tell(VSOCK_OP_RST); + vsock_drop("what came was not frames"); + } + break; + case VSOCK_OP_CREDIT_REQUEST: + vsock_tell(VSOCK_OP_CREDIT_UPDATE); + break; + case VSOCK_OP_SHUTDOWN: + vsock_tell(VSOCK_OP_RST); + vsock_drop("the app closed it"); + break; + case VSOCK_OP_RST: + vsock_drop("the app reset it"); + break; + default: /* CREDIT_UPDATE, RESPONSE: only the room, taken above */ + break; + } + lck_mtx_unlock(&vsock_lock); +} + +/* What has arrived: packets of the stream, and the device's events. Returns how many packets that was. */ +static unsigned +vsock_poll(void) +{ + unsigned packets = 0; + uint32_t length; + void *done; + + while ((done = vio_queue_get(vs.rx.vq, &length)) != NULL) { + uint16_t id = BUFFER_OF(done); + const uint8_t *buffer = vs.rx.buffers + (size_t)id * VSOCK_BUFFER; + struct vsock_header h; + + if (length >= sizeof(h) && length <= VSOCK_BUFFER) { + memcpy(&h, buffer, sizeof(h)); + vsock_packet(&h, buffer + sizeof(h), MIN(h.len, length - (uint32_t)sizeof(h))); + } + vsock_give(&vs.rx, id, VSOCK_BUFFER, true); /* the buffer goes back */ + packets++; + } + if (packets) { + vio_queue_kick(vs.rx.vq); + } + while ((done = vio_queue_get(vs.events, NULL)) != NULL) { + uint16_t id = BUFFER_OF(done); + uint32_t event; + + memcpy(&event, vs.event_buffers + (size_t)id * VSOCK_EVENT_SIZE, sizeof(event)); + if (event == VSOCK_EVENT_TRANSPORT_RESET) { /* (the VM was moved or restored: connections are gone) */ + lck_mtx_lock(&vsock_lock); + vsock_drop("the transport was reset"); + vs.cid = vio_cfg_read64(vs.dev, VSOCK_CFG_GUEST_CID); + lck_mtx_unlock(&vsock_lock); + } + vsock_give_event(id); + vio_queue_kick(vs.events); + } + /* the peer learns of the room made with every packet sent; without traffic back, tell it now and then */ + lck_mtx_lock(&vsock_lock); + if (vs.connected && vs.received - vs.received_said >= VSOCK_BUF_ALLOC / 4) { + vsock_tell(VSOCK_OP_CREDIT_UPDATE); + } + lck_mtx_unlock(&vsock_lock); + return packets; +} + +static void +vsock_counts(char *line, size_t room) +{ + snprintf(line, room, "vsock port %u: %s, %llu connections; frames dropped: %llu with no room, %llu with no app", + vs.port, vs.connected ? "the app is connected" : "waiting for the app", vs.connections, vs.no_room, vs.no_app); +} + +static bool +vsock_start(uint32_t port) +{ + int error; + + if ((vs.dev = vio_find(VIRTIO_ID_VSOCK, 0)) == NULL) { + return false; + } + if ((error = vio_init(vs.dev, 0, NULL)) != 0) { + printf("net: %s: the virtio socket device refused (it offers %#llx)\n", vio_name(vs.dev), vio_device_features(vs.dev)); + return false; + } + if (!vsock_queue_make(&vs.rx, VSOCK_QUEUE_RECEIVE, true) || !vsock_queue_make(&vs.tx, VSOCK_QUEUE_TRANSMIT, false) || + vio_queue_setup(vs.dev, VSOCK_QUEUE_EVENT, VSOCK_EVENTS, &vs.events) != 0 || + (vs.event_buffers = vio_dma_alloc(PAGE_SIZE, &vs.event_pa)) == NULL) { + printf("net: %s: no queues for the virtio socket device\n", vio_name(vs.dev)); + vio_fail(vs.dev); + return false; + } + for (uint16_t i = 0; i < vio_queue_size(vs.events); i++) { + vsock_give_event(i); + } + vs.cid = vio_cfg_read64(vs.dev, VSOCK_CFG_GUEST_CID); + vs.port = port; + vio_driver_ok(vs.dev); + vio_queue_kick(vs.rx.vq); + vio_queue_kick(vs.events); + printf("net: virtio socket device on %s: guest cid %llu, the app's frames on port %u\n", vio_name(vs.dev), vs.cid, port); + return true; +} + +static struct hakios_net_link vsock_link = { + .name = "the Scion app's network (virtio socket)", + .address = { 0x02, 'r', 'e', 'd', 'w', 'd' }, + .up = true, + .transmit = vsock_transmit, + .poll = vsock_poll, + .counts = vsock_counts, +}; + /* ---- virtio_net_wifi=1: a station with one network, always in reach ------------------------------------- */ #define VSTA_FREQUENCY 2437 /* channel 6 */ @@ -343,23 +723,34 @@ vsta_start(const uint8_t address[ETHER_ADDR_LEN]) void hakios_virtio_net_init(void) { - uint32_t enabled = 1, wifi = 0; + uint32_t enabled = 1, wifi = 0, port = 0; + struct hakios_net_link *link; + bool started; if (PE_parse_boot_argn("virtio_net", &enabled, sizeof(enabled)) && !enabled) { return; } - if (vio_find(VIRTIO_ID_NET, 0) == NULL) { - return; /* quietly: most machines have none */ + PE_parse_boot_argn("virtio_net_vsock", &port, sizeof(port)); + if (port != 0 && vio_find(VIRTIO_ID_VSOCK, 0) != NULL) { + link = &vsock_link; + } else if (vio_find(VIRTIO_ID_NET, 0) != NULL) { + link = &virtio_link; + } else { + if (port != 0) { + printf("net: virtio_net_vsock=%u, but there is no virtio socket device\n", port); + } + return; /* quietly otherwise: most machines have neither */ } if (PE_parse_boot_argn("virtio_net_wifi", &wifi, sizeof(wifi)) && wifi) { - virtio_link.wireless = true; - virtio_link.up = false; /* until the station has joined its network */ + link->wireless = true; + link->up = false; /* until the station has joined its network */ } - if (!virtio_net_start(virtio_link.address) || !hakios_net_attach(&virtio_link)) { + started = link == &vsock_link ? vsock_start(port) : virtio_net_start(link->address); + if (!started || !hakios_net_attach(link)) { printf("net: no network interface\n"); return; } - if (virtio_link.wireless) { - vsta_start(virtio_link.address); + if (link->wireless) { + vsta_start(link->address); } } diff --git a/tools/arch-size.allow b/tools/arch-size.allow index 1f95e126f..1747b8fda 100644 --- a/tools/arch-size.allow +++ b/tools/arch-size.allow @@ -17,7 +17,7 @@ drivers/bluetooth/bcm4361/hakios.bcm4361_bt.c 2222 # +34: bt_vsc= t drivers/camera/exynos/hakios.ak737x.c 991 # 901 today, and the growth of open pull request #334 drivers/camera/exynos/hakios.exynos_camera.c 4045 # 3692 today, and the growth of open pull request #334 drivers/camera/exynos/hakios.exynos_camera_rear.c 1404 # 1395 today, and the growth of open pull request #334 -drivers/camera/exynos/hakios.exynos_camera_star2.c 4482 +drivers/camera/exynos/hakios.exynos_camera_star2.c 4483 drivers/camera/exynos/hakios.exynos_iris.c 1721 drivers/camera/hakios.camera.c 4754 drivers/display/dpu/hakios.exynos_dpu.c 1043 @@ -87,9 +87,9 @@ tools/qmi-test/test_qmi.c 1562 userland/src/HakiosMetal.m 1534 userland/src/HakiosMetalCA.m 8747 userland/src/audio_plugin.c 1100 -userland/src/camera_device.c 2043 +userland/src/camera_device.c 2044 userland/src/camera_isp.c 1230 -userland/src/eagl.c 4781 +userland/src/eagl.c 4827 userland/src/gfx/gfx_gl.c 1083 userland/src/gnssd.m 1936 userland/src/gnsstool.c 1104 @@ -97,7 +97,7 @@ userland/src/gps_app.m 1073 userland/src/hakiosmetal_air.m 6342 userland/src/init.c 1840 userland/src/inject.c 2048 -userland/src/inject_media.c 993 +userland/src/inject_media.c 1093 userland/src/inject_sb.c 3999 userland/src/metal_drv_test.m 7737 userland/src/metal_stubdev.m 877 diff --git a/userland/src/camera_device.c b/userland/src/camera_device.c index be63bca42..52a661c94 100644 --- a/userland/src/camera_device.c +++ b/userland/src/camera_device.c @@ -1434,6 +1434,8 @@ worker(void *context) while (s->running && failures < 5) { ssize_t n; + tele = tele_state(); /* refresh after a telephoto probe or stream failure */ + if (buffer == NULL) { n = read(device, &head, sizeof(head)); if (n != (ssize_t)sizeof(head)) { diff --git a/userland/src/eagl.c b/userland/src/eagl.c index c402ab352..2453e2951 100644 --- a/userland/src/eagl.c +++ b/userland/src/eagl.c @@ -4135,6 +4135,13 @@ my_glPopGroupMarkerEXT(void *context) * version 3.00 it can. So every shader is handed on as a 3.00 one: the words of the first * version that 3.00 dropped are defined back, and texture2DRect() divides by textureSize(). * (All of a program's shaders must be of one version, so all are treated alike.) + * + * The extensions of the first version whose words 3.00 has of its own are not 3.00 extensions: + * a compiler may refuse a shader that asks for one. CoreAnimation's rounded corners ask for + * GL_OES_standard_derivatives (fwidth()), and the Mali-G710's compiler (Pixel 7) says "Extension + * 'GL_OES_standard_derivatives' not supported": the program did not link, and every draw of it + * was left out (Control Center's modules). Those lines are dropped, and what the extension + * named differently is defined to its 3.00 name (core_in_300). */ /* * CoreAnimation's shaders say "lowp" and "mediump" for nearly everything, as shaders written for @@ -4192,6 +4199,36 @@ static const char vertex_start[] = "#define texture2DLod textureLod\n" "#line 1\n"; +/* Extensions of the first version that are part of 3.00: their line goes, these definitions come instead */ +static const struct { + const char *name, *defines; +} core_in_300[] = { + { "GL_OES_standard_derivatives", "" }, + { "GL_EXT_shader_texture_lod", + "#define texture2DLodEXT textureLod\n#define texture2DProjLodEXT textureProjLod\n" + "#define textureCubeLodEXT textureLod\n#define texture2DGradEXT textureGrad\n" + "#define texture2DProjGradEXT textureProjGrad\n#define textureCubeGradEXT textureGrad\n" }, + { "GL_EXT_frag_depth", "#define gl_FragDepthEXT gl_FragDepth\n" }, + { "GL_EXT_draw_buffers", "" }, +}; +#define CORE_IN_300_DEFINES 400 /* (room for all of their definitions at once) */ + +/* The entry of core_in_300 the "#extension" line at `line` names, or -1. */ +static int +core_in_300_of(const char *line) +{ + line += strlen("#extension"); + line += strspn(line, " \t"); + for (int i = 0; i < (int)(sizeof(core_in_300) / sizeof(core_in_300[0])); i++) { + size_t n = strlen(core_in_300[i].name); + + if (strncmp(line, core_in_300[i].name, n) == 0 && strchr(" \t:", line[n]) != NULL) { + return i; + } + } + return -1; +} + static GLuint my_glCreateShader(void *context, GLenum type) { @@ -4216,30 +4253,39 @@ my_glShaderSource(void *context, GLuint shader, GLsizei count, const GLchar *con bool fragment = e->fragment[shader % 256], fetch; const GLchar *parts[6] = { "#version 300 es\n", fragment ? fragment_start : vertex_start, "", NULL, "", full_precision ? all_highp : "" }; - size_t size = 1, extensions = 0; + size_t size = 1; + unsigned core_defined = 0; char *text, *line, *head; EAGL_ENTER(e, 0x12a0); for (GLsizei i = 0; i < count; i++) { size += lengths != NULL && lengths[i] >= 0 ? (size_t)lengths[i] : strlen(strings[i]); } - if ((text = calloc(1, 2 * size + 64)) == NULL) { + if ((text = calloc(1, 2 * size + 64 + CORE_IN_300_DEFINES)) == NULL) { glShaderSource(shader, count, strings, lengths); return; } for (GLsizei i = 0; i < count; i++) { strncat(text, strings[i], lengths != NULL && lengths[i] >= 0 ? (size_t)lengths[i] : strlen(strings[i])); } - /* "#extension" lines must stay ahead of everything that is not the preprocessor's: they move to the top */ + /* + * "#extension" lines must stay ahead of everything that is not the preprocessor's: they move to + * the top (those of core_in_300 become their definitions) + */ head = text + size; strcpy(head, parts[0]); for (line = text; (line = strstr(line, "#extension")) != NULL; line += 2) { size_t length = strcspn(line, "\n"); + int core = core_in_300_of(line); - strncat(head, line, length); - strcat(head, "\n"); + if (core < 0) { + strncat(head, line, length); + strcat(head, "\n"); + } else if (!(core_defined & (1u << core))) { + core_defined |= 1u << core; + strcat(head, core_in_300[core].defines); + } line[0] = line[1] = '/'; - extensions++; } parts[0] = head; parts[3] = text; diff --git a/userland/src/inject_media.c b/userland/src/inject_media.c index 4b5a23902..e3dc9173d 100644 --- a/userland/src/inject_media.c +++ b/userland/src/inject_media.c @@ -42,6 +42,7 @@ #include #include #include +#include #include #include #include @@ -340,11 +341,13 @@ video_stabilization_type(id self, SEL selector) */ #define ERROR_NO_SUCH_THING 1852797029 /* 'nope' */ -static IMP original_audio_source_start; +static IMP original_audio_source_start, original_audio_source_stop; +static char audio_source_stopped; static BOOL audio_source_start(id self, SEL selector, id *error) { + if (camera) objc_setAssociatedObject(self, &audio_source_stopped, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC); BOOL started = ((BOOL (*)(id, SEL, id *))original_audio_source_start)(self, selector, error); id made = error != NULL ? *error : NULL; static bool said; @@ -361,6 +364,36 @@ audio_source_start(id self, SEL selector, id *error) return true; } +static BOOL +audio_source_stop(id self, SEL selector, id *error) +{ + BOOL result = ((BOOL (*)(id, SEL, id *))original_audio_source_stop)(self, selector, error); + /* + * The kernel camera's audio output can remain live after a successful stop, even when the + * microphone started normally. BWIrisStagingNode then waits for audio after video has ended, + * and BWGraph stop traps after ten seconds waiting for its movie sink. Close any outputs + * the original stop left open. Share the output lock with emit_sample_buffer so an in-flight + * audio sample finishes before its end marker, and later samples cannot reach a freed consumer. + */ + if (result && camera) { + objc_setAssociatedObject(self, &audio_source_stopped, (id)kCFBooleanTrue, OBJC_ASSOCIATION_RETAIN_NONATOMIC); + id outputs = ((id (*)(id, SEL))objc_msgSend)(self, sel_registerName("outputs")); + unsigned long count = outputs ? ((unsigned long (*)(id, SEL))objc_msgSend)(outputs, sel_registerName("count")) : 0; + Ivar ended = class_getInstanceVariable(objc_getClass("BWNodeOutput"), "_receivedEOD"); + for (unsigned long i = 0; ended != NULL && i < count; i++) { + id output = ((id (*)(id, SEL, unsigned long))objc_msgSend)(outputs, sel_registerName("objectAtIndex:"), i); + if (output == nil) continue; + objc_sync_enter(output); + if (*(unsigned char *)((char *)output + ivar_getOffset(ended)) == 0) { + ((void (*)(id, SEL))objc_msgSend)(output, sel_registerName("markEndOfLiveOutput")); + say("hakios: camera: audio output %lu closed after stop\n", i); + } + objc_sync_exit(output); + } + } + return result; +} + /* * A photograph's quality prioritization, "speed" at the most. Asked for "balanced" (Camera.app's) or * "quality", the capture device takes a scene for a low-light one whenever a frame was exposed at @@ -472,10 +505,72 @@ emit_sample_buffer(id self, SEL selector, CMSampleBufferRef sample_buffer) } return; } + if (camera && is_node(((id (*)(id, SEL))objc_msgSend)(self, sel_registerName("node")), "BWAudioSourceNode")) { + /* The audio timer may still emit after stop has ended this output. */ + Ivar ended = class_getInstanceVariable(objc_getClass("BWNodeOutput"), "_receivedEOD"); + objc_sync_enter(self); + if (ended == NULL || *(unsigned char *)((char *)self + ivar_getOffset(ended)) == 0) { + ((void (*)(id, SEL, CMSampleBufferRef))original_emit_sample_buffer)(self, selector, sample_buffer); + } else { + static bool said_late; + if (!__atomic_exchange_n(&said_late, true, __ATOMIC_RELAXED)) { + say("hakios: camera: audio sample after end of output dropped\n"); + } + } + objc_sync_exit(self); + return; + } time_photo_encoder(self, sample_buffer); ((void (*)(id, SEL, CMSampleBufferRef))original_emit_sample_buffer)(self, selector, sample_buffer); } +/* makeOutputsLiveIfNeeded can leave a format notification queued past the source's stop. */ +static IMP original_make_format_live; + +static void +make_format_live(id self, SEL selector) +{ + id node = ((id (*)(id, SEL))objc_msgSend)(self, sel_registerName("node")); + if (camera && is_node(node, "BWAudioSourceNode")) { + objc_sync_enter(self); + if (objc_getAssociatedObject(node, &audio_source_stopped) == nil) { + ((void (*)(id, SEL))original_make_format_live)(self, selector); + } else { + static bool said; + if (!__atomic_exchange_n(&said, true, __ATOMIC_RELAXED)) { + say("hakios: camera: audio format notification after stop dropped\n"); + } + } + objc_sync_exit(self); + return; + } + ((void (*)(id, SEL))original_make_format_live)(self, selector); +} + +/* The native stop's queued marker may arrive after audio_source_stop closed the output. */ +static IMP original_mark_end; + +static void +mark_end(id self, SEL selector) +{ + id node = ((id (*)(id, SEL))objc_msgSend)(self, sel_registerName("node")); + if (camera && is_node(node, "BWAudioSourceNode")) { + Ivar ended = class_getInstanceVariable(objc_getClass("BWNodeOutput"), "_receivedEOD"); + objc_sync_enter(self); + if (ended == NULL || *(unsigned char *)((char *)self + ivar_getOffset(ended)) == 0) { + ((void (*)(id, SEL))original_mark_end)(self, selector); + } else { + static bool said; + if (!__atomic_exchange_n(&said, true, __ATOMIC_RELAXED)) { + say("hakios: camera: duplicate audio end marker dropped\n"); + } + } + objc_sync_exit(self); + return; + } + ((void (*)(id, SEL))original_mark_end)(self, selector); +} + /* * The photograph's encoder. Camera.app asks for HEIC -- HEVC inside a HEIF file, which CMPhoto makes * through VideoToolbox -- and VideoToolbox here has no HEVC encoder but the iPhone's hardware ones @@ -569,6 +664,7 @@ static struct replacement { { "BWFigCaptureDeviceVendor", "sharedCaptureDeviceVendor", 0x1a1ef0d04UL, (IMP)shared_vendor, &original_shared_vendor, false, false }, { "BWAudioSourceNode", "start:", 0x1a1eefa18UL, (IMP)audio_source_start, &original_audio_source_start, true, false }, + { "BWAudioSourceNode", "stop:", 0x1a1f4dc70UL, (IMP)audio_source_stop, &original_audio_source_stop, true, false }, { "FigVideoCaptureConnectionConfiguration", "videoStabilizationMethod", 0x1a1e99ef8UL, (IMP)video_stabilization_method, NULL, true, false }, { "FigVideoCaptureConnectionConfiguration", "videoStabilizationType", 0x1a20d8f48UL, @@ -576,6 +672,10 @@ static struct replacement { { "FigCaptureStillImageSettings", "qualityPrioritization", 0x1a1f533dcUL, (IMP)quality_prioritization, &original_quality_prioritization, true, false }, { "BWGNRNode", "renderSampleBuffer:forInput:", 0x1a1ec83b8UL, (IMP)gnr_render, NULL, true, false }, + { "BWNodeOutput", "markEndOfLiveOutput", 0x1a1f1b6ccUL, + (IMP)mark_end, &original_mark_end, true, false }, + { "BWNodeOutput", "makeConfiguredFormatLive", 0x1a1ef402cUL, + (IMP)make_format_live, &original_make_format_live, true, false }, { "BWNodeOutput", "emitSampleBuffer:", 0x1a1ea37e0UL, (IMP)emit_sample_buffer, &original_emit_sample_buffer, true, false }, };