From 5a775e23b0a8794a5359d944dc00b841d136c7a2 Mon Sep 17 00:00:00 2001 From: marcobuschini Date: Thu, 3 Dec 2026 17:37:53 +0000 Subject: [PATCH] feat(user): add EL0 test_milo end-to-end verification command (#220) - Add user/test_milo.c static musl C binary (/bin/test_milo) with -q/--quiet and -h/--help flags, verifying uname, fork/waitpid, sigaction/kill/rt_sigreturn, clock_gettime/nanosleep, sysinfo/getrandom, mmap/mprotect/munmap, pipe2/dup3/fcntl/ppoll, AF_UNIX socketpair, rootfs (/init.txt), tmpfs, ext2 (/mnt/disk), devfs (/dev/console, /dev/null, /dev/zero, /dev/urandom), and /dev/fb0 - Wire user/test_milo.c into Makefile (SRCS_C, TEST_MILO_USER_BIN, and both busybox.cpio and bash.cpio initrd images) - Document /bin/test_milo in docs/08-testing-and-ci.md --- Makefile | 18 +- docs/08-testing-and-ci.md | 2 +- user/test_milo.c | 453 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 469 insertions(+), 4 deletions(-) create mode 100644 user/test_milo.c diff --git a/Makefile b/Makefile index 405eb07..59bb6c6 100644 --- a/Makefile +++ b/Makefile @@ -32,7 +32,8 @@ LDFLAGS := -nostdlib -T arch/$(ARCH)/linker.ld SRCS_CXX := SRCS_ASM := SRCS_C := user/hello_musl.c user/hello_dyn.c user/fb_test.c user/evdev_smoke.c \ - user/unix_echo_server.c user/unix_echo_client.c user/unix_sock_smoke.c + user/unix_echo_server.c user/unix_echo_client.c user/unix_sock_smoke.c \ + user/test_milo.c # Include architecture and core modules include arch/$(ARCH)/arch.mk @@ -212,6 +213,7 @@ EVDEV_USER_BIN := $(if $(HAS_MUSL_CC),$(BUILD_DIR)/user/evdev_smoke) UNIX_SRV_BIN := $(if $(HAS_MUSL_CC),$(BUILD_DIR)/user/unix_echo_server) UNIX_CLI_BIN := $(if $(HAS_MUSL_CC),$(BUILD_DIR)/user/unix_echo_client) UNIX_SMOKE_BIN := $(if $(HAS_MUSL_CC),$(BUILD_DIR)/user/unix_sock_smoke) +TEST_MILO_USER_BIN := $(if $(HAS_MUSL_CC),$(BUILD_DIR)/user/test_milo) MUSL_LDSO_LIB := $(wildcard $(MUSL_SYSROOT)/lib/libc.so) LINUX_VER := 6.6.63 @@ -260,6 +262,10 @@ $(BUILD_DIR)/user/unix_sock_smoke: user/unix_sock_smoke.c @mkdir -p $(dir $@) $(MUSL_CC) -static -O2 -Wall -Wextra $< -o $@ +$(BUILD_DIR)/user/test_milo: user/test_milo.c + @mkdir -p $(dir $@) + $(MUSL_CC) -static -O2 -Wall -Wextra $< -o $@ + # --- BusyBox Initrd (initrds/busybox.cpio) --- BUSYBOX_VER := 1.36.1 BUSYBOX_BIN := $(BUILD_DIR)/busybox-$(BUSYBOX_VER)/busybox @@ -275,7 +281,7 @@ $(BUSYBOX_BIN): sed -i 's/CONFIG_TC=y/# CONFIG_TC is not set/' .config && \ $(MAKE) -j$$(nproc) CROSS_COMPILE=aarch64-linux-musl- CC="$(MUSL_CC)" -$(BUSYBOX_CPIO): $(BUILD_DIR)/user/hello $(MUSL_USER_BIN) $(DYN_USER_BIN) $(FB_USER_BIN) $(EVDEV_USER_BIN) $(UNIX_SRV_BIN) $(UNIX_CLI_BIN) $(UNIX_SMOKE_BIN) $(wildcard $(BUSYBOX_BIN)) +$(BUSYBOX_CPIO): $(BUILD_DIR)/user/hello $(MUSL_USER_BIN) $(DYN_USER_BIN) $(FB_USER_BIN) $(EVDEV_USER_BIN) $(UNIX_SRV_BIN) $(UNIX_CLI_BIN) $(UNIX_SMOKE_BIN) $(TEST_MILO_USER_BIN) $(wildcard $(BUSYBOX_BIN)) @rm -rf $(BUSYBOX_ROOTFS_DIR) @mkdir -p $(INITRDS_DIR) $(BUSYBOX_ROOTFS_DIR)/bin $(BUSYBOX_ROOTFS_DIR)/lib $(BUSYBOX_ROOTFS_DIR)/dev $(BUSYBOX_ROOTFS_DIR)/proc $(BUSYBOX_ROOTFS_DIR)/tmp @echo "milo-initrd-v1" > $(BUSYBOX_ROOTFS_DIR)/init.txt @@ -301,6 +307,9 @@ $(BUSYBOX_CPIO): $(BUILD_DIR)/user/hello $(MUSL_USER_BIN) $(DYN_USER_BIN) $(FB_U @if [ -f "$(BUILD_DIR)/user/unix_sock_smoke" ]; then \ install -m 0755 $(BUILD_DIR)/user/unix_sock_smoke $(BUSYBOX_ROOTFS_DIR)/bin/unix_sock_smoke; \ fi + @if [ -f "$(BUILD_DIR)/user/test_milo" ]; then \ + install -m 0755 $(BUILD_DIR)/user/test_milo $(BUSYBOX_ROOTFS_DIR)/bin/test_milo; \ + fi @if [ -f "$(MUSL_SYSROOT)/lib/libc.so" ]; then \ install -m 0755 $(MUSL_SYSROOT)/lib/libc.so $(BUSYBOX_ROOTFS_DIR)/lib/libc.so && \ ln -sf libc.so $(BUSYBOX_ROOTFS_DIR)/lib/ld-musl-aarch64.so.1 && \ @@ -343,7 +352,7 @@ $(BASH_BIN): $(MAKE) -j$$(nproc) && \ aarch64-linux-musl-strip bash -$(BASH_CPIO): $(BUILD_DIR)/user/hello $(MUSL_USER_BIN) $(DYN_USER_BIN) $(FB_USER_BIN) $(EVDEV_USER_BIN) $(UNIX_SRV_BIN) $(UNIX_CLI_BIN) $(UNIX_SMOKE_BIN) $(BASH_BIN) +$(BASH_CPIO): $(BUILD_DIR)/user/hello $(MUSL_USER_BIN) $(DYN_USER_BIN) $(FB_USER_BIN) $(EVDEV_USER_BIN) $(UNIX_SRV_BIN) $(UNIX_CLI_BIN) $(UNIX_SMOKE_BIN) $(TEST_MILO_USER_BIN) $(BASH_BIN) @rm -rf $(BASH_ROOTFS_DIR) @mkdir -p $(INITRDS_DIR) $(BASH_ROOTFS_DIR)/bin $(BASH_ROOTFS_DIR)/lib $(BASH_ROOTFS_DIR)/dev $(BASH_ROOTFS_DIR)/proc $(BASH_ROOTFS_DIR)/tmp @echo "milo-initrd-v1" > $(BASH_ROOTFS_DIR)/init.txt @@ -369,6 +378,9 @@ $(BASH_CPIO): $(BUILD_DIR)/user/hello $(MUSL_USER_BIN) $(DYN_USER_BIN) $(FB_USER @if [ -f "$(BUILD_DIR)/user/unix_sock_smoke" ]; then \ install -m 0755 $(BUILD_DIR)/user/unix_sock_smoke $(BASH_ROOTFS_DIR)/bin/unix_sock_smoke; \ fi + @if [ -f "$(BUILD_DIR)/user/test_milo" ]; then \ + install -m 0755 $(BUILD_DIR)/user/test_milo $(BASH_ROOTFS_DIR)/bin/test_milo; \ + fi @if [ -f "$(MUSL_SYSROOT)/lib/libc.so" ]; then \ install -m 0755 $(MUSL_SYSROOT)/lib/libc.so $(BASH_ROOTFS_DIR)/lib/libc.so && \ ln -sf libc.so $(BASH_ROOTFS_DIR)/lib/ld-musl-aarch64.so.1 && \ diff --git a/docs/08-testing-and-ci.md b/docs/08-testing-and-ci.md index 13fe16f..53e6e88 100644 --- a/docs/08-testing-and-ci.md +++ b/docs/08-testing-and-ci.md @@ -49,7 +49,7 @@ Booting with `make run` starts `kmain` and enters `Kernel::Shell::run()`, which ### EL0 User-Space Programs & Initrd Images (`user/`, `Makefile`) -`Makefile` builds EL0 programs (`user/hello.s`, `user/hello_musl.c`, `user/hello_dyn.c`, `user/fb_test.c`, `user/evdev_smoke.c`, `user/unix_echo_server.c`, `user/unix_echo_client.c`, `user/unix_sock_smoke.c`) when `aarch64-linux-musl-gcc` is available and packages them into CPIO `newc` archives (`initrds/busybox.cpio` via `make setup-busybox`, or `initrds/bash.cpio` via `make setup-bash`). +`Makefile` builds EL0 programs (`user/hello.s`, `user/hello_musl.c`, `user/hello_dyn.c`, `user/fb_test.c`, `user/evdev_smoke.c`, `user/unix_echo_server.c`, `user/unix_echo_client.c`, `user/unix_sock_smoke.c`, `user/test_milo.c`) when `aarch64-linux-musl-gcc` is available and packages them into CPIO `newc` archives (`initrds/busybox.cpio` via `make setup-busybox`, or `initrds/bash.cpio` via `make setup-bash`). Inside `milo-sh` or `milo-bash`, `/bin/test_milo` runs non-interactive EL0 end-to-end checks (`-q`/`--quiet` for summary-only output). ## GitHub Actions Workflows (`.github/workflows/`) diff --git a/user/test_milo.c b/user/test_milo.c new file mode 100644 index 0000000..4233480 --- /dev/null +++ b/user/test_milo.c @@ -0,0 +1,453 @@ +// SPDX-License-Identifier: GPL-3.0-only +// Copyright (c) 2026 Marco Buschini + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MILO_FBIOGET_VSCREENINFO 0x4600UL +#define MILO_FBIOGET_FSCREENINFO 0x4602UL +#define MILO_FBIO_FLUSH 0x4620UL + +struct milo_fb_bitfield { + uint32_t offset; + uint32_t length; + uint32_t msb_right; +}; + +struct milo_fb_var_screeninfo { + uint32_t xres; + uint32_t yres; + uint32_t xres_virtual; + uint32_t yres_virtual; + uint32_t xoffset; + uint32_t yoffset; + uint32_t bits_per_pixel; + uint32_t grayscale; + struct milo_fb_bitfield red; + struct milo_fb_bitfield green; + struct milo_fb_bitfield blue; + struct milo_fb_bitfield transp; + uint32_t nonstd; + uint32_t activate; + uint32_t height; + uint32_t width; + uint32_t accel_flags; + uint32_t pixclock; + uint32_t left_margin; + uint32_t right_margin; + uint32_t upper_margin; + uint32_t lower_margin; + uint32_t hsync_len; + uint32_t vsync_len; + uint32_t sync; + uint32_t vmode; + uint32_t rotate; + uint32_t colorspace; + uint32_t reserved[4]; +}; + +struct milo_fb_fix_screeninfo { + char id[16]; + uint64_t smem_start; + uint32_t smem_len; + uint32_t type; + uint32_t type_aux; + uint32_t visual; + uint16_t xpanstep; + uint16_t ypanstep; + uint16_t ywrapstep; + uint16_t pad0; + uint32_t line_length; + uint32_t pad1; + uint64_t mmio_start; + uint32_t mmio_len; + uint32_t accel; + uint16_t capabilities; + uint16_t reserved[2]; + uint16_t pad2; +}; + +static int g_passed = 0; +static int g_skipped = 0; +static int g_failed = 0; +static int g_quiet = 0; + +static volatile sig_atomic_t g_sigusr1_seen = 0; + +static void handle_sigusr1(int sig) { + if (sig == SIGUSR1) { + g_sigusr1_seen = 1; + } +} + +static void record_pass(const char* name) { + ++g_passed; + if (!g_quiet) { + printf("[PASS] %s\n", name); + } +} + +static void record_skip(const char* name, const char* reason) { + ++g_skipped; + if (!g_quiet) { + printf("[SKIP] %s (%s)\n", name, reason); + } +} + +static void record_fail(const char* name, const char* detail) { + ++g_failed; + printf("[FAIL] %s (%s, errno=%d)\n", name, detail, errno); +} + +static void check_process_and_signals(void) { + struct utsname uts; + memset(&uts, 0, sizeof(uts)); + if (uname(&uts) == 0 && strcmp(uts.machine, "aarch64") == 0) { + record_pass("uname reports aarch64"); + } else { + record_fail("uname reports aarch64", "unexpected uname output"); + } + + const pid_t self_pid = getpid(); + const pid_t pid = fork(); + if (pid < 0) { + record_fail("fork/waitpid lifecycle", "fork failed"); + return; + } + if (pid == 0) { + if (getppid() != self_pid) { + _exit(2); + } + _exit(42); + } + + int status = 0; + const pid_t reaped = waitpid(pid, &status, 0); + if (reaped == pid && WIFEXITED(status) && WEXITSTATUS(status) == 42) { + record_pass("fork/waitpid lifecycle"); + } else { + record_fail("fork/waitpid lifecycle", "unexpected child exit status"); + } + + struct sigaction sa; + struct sigaction old_sa; + memset(&sa, 0, sizeof(sa)); + memset(&old_sa, 0, sizeof(old_sa)); + sa.sa_handler = handle_sigusr1; + sa.sa_flags = SA_RESTART; + if (sigaction(SIGUSR1, &sa, &old_sa) != 0) { + record_fail("sigaction/kill/rt_sigreturn", "sigaction failed"); + return; + } + + g_sigusr1_seen = 0; + if (kill(self_pid, SIGUSR1) == 0 && g_sigusr1_seen == 1) { + record_pass("sigaction/kill/rt_sigreturn"); + } else { + record_fail("sigaction/kill/rt_sigreturn", "handler did not run"); + } + (void)sigaction(SIGUSR1, &old_sa, NULL); +} + +static void check_time_and_memory(void) { + struct timespec ts0; + struct timespec ts1; + memset(&ts0, 0, sizeof(ts0)); + memset(&ts1, 0, sizeof(ts1)); + if (clock_gettime(CLOCK_MONOTONIC, &ts0) != 0) { + record_fail("clock_gettime/nanosleep", "clock_gettime failed"); + } else { + const struct timespec req = {.tv_sec = 0, .tv_nsec = 1000000L}; + if (nanosleep(&req, NULL) == 0 && clock_gettime(CLOCK_MONOTONIC, &ts1) == 0 && + (ts1.tv_sec > ts0.tv_sec || (ts1.tv_sec == ts0.tv_sec && ts1.tv_nsec >= ts0.tv_nsec))) { + record_pass("clock_gettime/nanosleep"); + } else { + record_fail("clock_gettime/nanosleep", "monotonic clock did not advance"); + } + } + + struct sysinfo info; + memset(&info, 0, sizeof(info)); + uint8_t rnd[16]; + memset(rnd, 0, sizeof(rnd)); + if (sysinfo(&info) == 0 && info.totalram > 0 && getrandom(rnd, sizeof(rnd), 0) == 16) { + record_pass("sysinfo/getrandom"); + } else { + record_fail("sysinfo/getrandom", "sysinfo or getrandom failed"); + } + + void* const p = mmap(NULL, 8192U, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (p == MAP_FAILED) { + record_fail("mmap/mprotect/munmap", "mmap failed"); + return; + } + volatile uint32_t* words = (volatile uint32_t*)p; + words[0] = 0xCAFEBABEU; + words[1024] = 0xDEADBEEFU; + if (mprotect(p, 8192U, PROT_READ) == 0 && words[0] == 0xCAFEBABEU && + words[1024] == 0xDEADBEEFU && munmap(p, 8192U) == 0) { + record_pass("mmap/mprotect/munmap"); + } else { + (void)munmap(p, 8192U); + record_fail("mmap/mprotect/munmap", "mprotect or munmap failed"); + } +} + +static void check_ipc_and_poll(void) { + int pfd[2] = {-1, -1}; + if (pipe2(pfd, O_CLOEXEC | O_NONBLOCK) != 0) { + record_fail("pipe2/dup3/fcntl/ppoll", "pipe2 failed"); + return; + } + + const int dup_wr = dup3(pfd[1], 16, O_CLOEXEC); + if (dup_wr < 0) { + close(pfd[0]); + close(pfd[1]); + record_fail("pipe2/dup3/fcntl/ppoll", "dup3 failed"); + return; + } + + const int fl = fcntl(pfd[0], F_GETFL, 0); + const char msg[] = "milo-pipe"; + const ssize_t wn = write(dup_wr, msg, sizeof(msg) - 1U); + struct pollfd p = {.fd = pfd[0], .events = POLLIN, .revents = 0}; + const int prc = poll(&p, 1U, 0); + char rbuf[16]; + memset(rbuf, 0, sizeof(rbuf)); + const ssize_t rn = read(pfd[0], rbuf, sizeof(rbuf)); + + close(dup_wr); + close(pfd[0]); + close(pfd[1]); + + if (fl >= 0 && (fl & O_NONBLOCK) != 0 && wn == (ssize_t)(sizeof(msg) - 1U) && prc == 1 && + (p.revents & POLLIN) != 0 && rn == wn && memcmp(rbuf, msg, (size_t)wn) == 0) { + record_pass("pipe2/dup3/fcntl/ppoll"); + } else { + record_fail("pipe2/dup3/fcntl/ppoll", "pipe/poll round-trip mismatch"); + } + + int sv[2] = {-1, -1}; + if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0, sv) != 0) { + record_fail("AF_UNIX socketpair", "socketpair failed"); + return; + } + const char smsg[] = "unix-ok"; + char sbuf[16]; + memset(sbuf, 0, sizeof(sbuf)); + const ssize_t sw = write(sv[0], smsg, sizeof(smsg) - 1U); + const ssize_t sr = read(sv[1], sbuf, sizeof(sbuf)); + close(sv[0]); + close(sv[1]); + + if (sw == (ssize_t)(sizeof(smsg) - 1U) && sr == sw && memcmp(sbuf, smsg, (size_t)sw) == 0) { + record_pass("AF_UNIX socketpair"); + } else { + record_fail("AF_UNIX socketpair", "socketpair echo mismatch"); + } +} + +static void check_filesystems_and_devices(void) { + // 1. Read-only rootfs (/init.txt) + const int init_fd = open("/init.txt", O_RDONLY); + if (init_fd >= 0) { + char ibuf[32]; + memset(ibuf, 0, sizeof(ibuf)); + const ssize_t n = read(init_fd, ibuf, sizeof(ibuf) - 1U); + close(init_fd); + const int ro_fd = open("/init.txt", O_WRONLY); + const int ro_err = (ro_fd < 0) ? errno : 0; + if (ro_fd >= 0) { + close(ro_fd); + } + if (n > 0 && ro_fd < 0 && ro_err == EROFS) { + record_pass("rootfs read-only /init.txt"); + } else { + record_fail("rootfs read-only /init.txt", "unexpected read or EROFS check"); + } + } else { + record_skip("rootfs read-only /init.txt", "/init.txt absent"); + } + + // 2. Writable tmpfs (/tmp) + char tmp_a[64]; + char tmp_b[64]; + snprintf(tmp_a, sizeof(tmp_a), "/tmp/test_milo_%d_a.txt", (int)getpid()); + snprintf(tmp_b, sizeof(tmp_b), "/tmp/test_milo_%d_b.txt", (int)getpid()); + unlink(tmp_a); + unlink(tmp_b); + + const int tfd = open(tmp_a, O_CREAT | O_EXCL | O_RDWR, 0644); + if (tfd >= 0) { + const char payload[] = "tmpfs-data"; + const ssize_t tw = write(tfd, payload, sizeof(payload) - 1U); + const off_t seeked = lseek(tfd, 0, SEEK_SET); + char tbuf[16]; + memset(tbuf, 0, sizeof(tbuf)); + const ssize_t tr = read(tfd, tbuf, sizeof(tbuf)); + struct stat st; + memset(&st, 0, sizeof(st)); + const int fstat_rc = fstat(tfd, &st); + close(tfd); + + const int ren_rc = rename(tmp_a, tmp_b); + const int unl_rc = unlink(tmp_b); + if (tw == (ssize_t)(sizeof(payload) - 1U) && seeked == 0 && tr == tw && + memcmp(tbuf, payload, (size_t)tw) == 0 && fstat_rc == 0 && st.st_size == tw && + ren_rc == 0 && unl_rc == 0) { + record_pass("tmpfs create/write/read/rename/unlink"); + } else { + unlink(tmp_a); + unlink(tmp_b); + record_fail("tmpfs create/write/read/rename/unlink", "tmpfs operation failed"); + } + } else { + record_fail("tmpfs create/write/read/rename/unlink", "open O_CREAT failed"); + } + + // 3. Optional ext2 mount (/mnt/disk) + char ext2_path[64]; + snprintf(ext2_path, sizeof(ext2_path), "/mnt/disk/test_milo_%d.txt", (int)getpid()); + const int efd = open(ext2_path, O_CREAT | O_RDWR | O_TRUNC, 0644); + if (efd >= 0) { + const char epayload[] = "ext2-ok"; + const ssize_t ew = write(efd, epayload, sizeof(epayload) - 1U); + const int sync_rc = fsync(efd); + lseek(efd, 0, SEEK_SET); + char ebuf[16]; + memset(ebuf, 0, sizeof(ebuf)); + const ssize_t er = read(efd, ebuf, sizeof(ebuf)); + close(efd); + const int eunl = unlink(ext2_path); + if (ew == (ssize_t)(sizeof(epayload) - 1U) && sync_rc == 0 && er == ew && + memcmp(ebuf, epayload, (size_t)ew) == 0 && eunl == 0) { + record_pass("ext2 /mnt/disk read/write/fsync/unlink"); + } else { + unlink(ext2_path); + record_fail("ext2 /mnt/disk read/write/fsync/unlink", "ext2 operation failed"); + } + } else { + record_skip("ext2 /mnt/disk read/write/fsync/unlink", "/mnt/disk not mounted"); + } + + // 4. Devfs nodes (/dev/console non-blocking ioctls, /dev/null, /dev/zero, /dev/urandom) + const int cfd = open("/dev/console", O_RDWR); + struct termios tio; + struct winsize ws; + memset(&tio, 0, sizeof(tio)); + memset(&ws, 0, sizeof(ws)); + if (cfd >= 0 && ioctl(cfd, TCGETS, &tio) == 0 && ioctl(cfd, TIOCGWINSZ, &ws) == 0 && + ws.ws_row > 0 && ws.ws_col > 0) { + close(cfd); + record_pass("devfs /dev/console TCGETS & TIOCGWINSZ"); + } else { + if (cfd >= 0) { + close(cfd); + } + record_fail("devfs /dev/console TCGETS & TIOCGWINSZ", "console ioctl failed"); + } + + const int nfd = open("/dev/null", O_RDWR); + const int zfd = open("/dev/zero", O_RDONLY); + const int ufd = open("/dev/urandom", O_RDONLY); + uint8_t zbuf[8]; + uint8_t ubuf[8]; + memset(zbuf, 0x5A, sizeof(zbuf)); + memset(ubuf, 0, sizeof(ubuf)); + const ssize_t nw = (nfd >= 0) ? write(nfd, "abc", 3U) : -1; + const ssize_t zr = (zfd >= 0) ? read(zfd, zbuf, sizeof(zbuf)) : -1; + const ssize_t ur = (ufd >= 0) ? read(ufd, ubuf, sizeof(ubuf)) : -1; + if (nfd >= 0) { + close(nfd); + } + if (zfd >= 0) { + close(zfd); + } + if (ufd >= 0) { + close(ufd); + } + int z_ok = (zr == (ssize_t)sizeof(zbuf)); + for (size_t i = 0U; i < sizeof(zbuf); ++i) { + if (zbuf[i] != 0U) { + z_ok = 0; + } + } + if (nw == 3 && z_ok && ur == (ssize_t)sizeof(ubuf)) { + record_pass("devfs /dev/{null,zero,urandom}"); + } else { + record_fail("devfs /dev/{null,zero,urandom}", "device node I/O failed"); + } + + // 5. Optional /dev/fb0 (MAP_SHARED + ioctl) + const int fbfd = open("/dev/fb0", O_RDWR); + if (fbfd < 0) { + record_skip("devfs /dev/fb0 ioctl & MAP_SHARED", "/dev/fb0 absent"); + return; + } + + struct milo_fb_var_screeninfo vinfo; + struct milo_fb_fix_screeninfo finfo; + memset(&vinfo, 0, sizeof(vinfo)); + memset(&finfo, 0, sizeof(finfo)); + if (ioctl(fbfd, MILO_FBIOGET_VSCREENINFO, &vinfo) != 0 || + ioctl(fbfd, MILO_FBIOGET_FSCREENINFO, &finfo) != 0 || finfo.smem_len == 0U) { + close(fbfd); + record_fail("devfs /dev/fb0 ioctl & MAP_SHARED", "fb0 ioctl failed"); + return; + } + + void* const fb_map = mmap(NULL, finfo.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0); + if (fb_map == MAP_FAILED) { + close(fbfd); + record_fail("devfs /dev/fb0 ioctl & MAP_SHARED", "fb0 mmap failed"); + return; + } + + const int flush_rc = ioctl(fbfd, MILO_FBIO_FLUSH, 0); + (void)munmap(fb_map, finfo.smem_len); + close(fbfd); + + if (flush_rc == 0) { + record_pass("devfs /dev/fb0 ioctl & MAP_SHARED"); + } else { + record_fail("devfs /dev/fb0 ioctl & MAP_SHARED", "FBIO_FLUSH failed"); + } +} + +int main(int argc, char** argv) { + for (int i = 1; i < argc; ++i) { + if (strcmp(argv[i], "-q") == 0 || strcmp(argv[i], "--quiet") == 0) { + g_quiet = 1; + } else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) { + printf("Usage: %s [-q|--quiet] [-h|--help]\n", argv[0]); + return 0; + } + } + + check_process_and_signals(); + check_time_and_memory(); + check_ipc_and_poll(); + check_filesystems_and_devices(); + + printf("test_milo summary: passed=%d skipped=%d failed=%d\n", g_passed, g_skipped, g_failed); + return (g_failed == 0) ? 0 : 1; +}