aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / disk / Makefile
| 1 | # AIENOS native disk layer in C: disk_dev core (bounds, split, ordered queue), |
| 2 | # hosted file backend for tests, and a freestanding polled NVMe driver |
| 3 | # (disk_nvme.c) ported from the audited Rust path, and a polled PIO SD Host |
| 4 | # Controller driver (disk_sdhci.c) for the Pi 5 microSD. No outside library. |
| 5 | # Library objects (disk.c, disk_nvme.c, disk_sdhci.c) import no libc symbols at all; the |
| 6 | # file backend and the tests use hosted libc. |
| 7 | # |
| 8 | # make build out/libaienos_disk.a (+ out/disk_file.o, hosted) |
| 9 | # make test disk core + file backend + NVMe and SDHCI drivers vs software models |
| 10 | # make freestanding build library objects -ffreestanding; nm -u must be empty |
| 11 | # make sanitize the same tests under ASan+UBSan (Linux) |
| 12 | # make mutants delete each GUARD-tagged check in turn; tests must fail |
| 13 | # make clean |
| 14 | # |
| 15 | # Host tests and the NVMe and SDHCI models are not QEMU and not hardware. |
| 16 | |
| 17 | CC ?= cc |
| 18 | AR ?= ar |
| 19 | NM ?= nm |
| 20 | LD ?= ld |
| 21 | CFLAGS ?= -std=gnu11 -O2 -Wall -Wextra -Werror -fstack-protector-strong |
| 22 | FREEFLAGS = -std=gnu11 -O2 -Wall -Wextra -Werror -ffreestanding -nostdlib -fno-builtin \ |
| 23 | -fno-stack-protector -fno-tree-loop-distribute-patterns |
| 24 | # On AArch64 also build as the bare-metal payload does (MMU off: no unaligned |
| 25 | # access, no FP/SIMD registers), which catches struct copies lowered to memcpy. |
| 26 | ifeq ($(shell uname -m),aarch64) |
| 27 | FREEFLAGS += -mstrict-align -mgeneral-regs-only |
| 28 | endif |
| 29 | OUT ?= out |
| 30 | MUTANT_TIMEOUT ?= 60 |
| 31 | |
| 32 | SRCS = disk.c disk_nvme.c disk_sdhci.c |
| 33 | OBJS = $(SRCS:%.c=$(OUT)/%.o) |
| 34 | FILE_OBJ = $(OUT)/disk_file.o |
| 35 | LIB = $(OUT)/libaienos_disk.a |
| 36 | HDRS = disk.h disk_nvme.h disk_sdhci.h disk_file.h |
| 37 | DTEST = $(OUT)/disk_test |
| 38 | NTEST = $(OUT)/nvme_test |
| 39 | STEST = $(OUT)/sdhci_test |
| 40 | SANFLAGS = -fsanitize=address,undefined -fno-omit-frame-pointer -fno-sanitize-recover=all -g |
| 41 | |
| 42 | all: $(LIB) $(FILE_OBJ) |
| 43 | |
| 44 | $(OUT): |
| 45 | mkdir -p $(OUT) |
| 46 | |
| 47 | $(OUT)/%.o: %.c $(HDRS) | $(OUT) |
| 48 | $(CC) $(CFLAGS) -c -o $@ $< |
| 49 | |
| 50 | $(LIB): $(OBJS) |
| 51 | rm -f $@ |
| 52 | $(AR) rcs $@ $^ |
| 53 | |
| 54 | $(DTEST): tests/disk_test.c $(LIB) $(FILE_OBJ) $(HDRS) |
| 55 | $(CC) $(CFLAGS) -o $@ tests/disk_test.c $(FILE_OBJ) $(LIB) |
| 56 | |
| 57 | $(NTEST): tests/nvme_test.c $(LIB) $(HDRS) |
| 58 | $(CC) $(CFLAGS) -o $@ tests/nvme_test.c $(LIB) |
| 59 | |
| 60 | $(STEST): tests/sdhci_test.c $(LIB) $(HDRS) |
| 61 | $(CC) $(CFLAGS) -o $@ tests/sdhci_test.c $(LIB) |
| 62 | |
| 63 | test: $(DTEST) $(NTEST) $(STEST) lib-checks |
| 64 | @rm -rf $(OUT)/scratch && mkdir -p $(OUT)/scratch; \ |
| 65 | rc=0; ./$(DTEST) $(OUT)/scratch || rc=1; ./$(NTEST) || rc=1; ./$(STEST) > $(OUT)/sdhci_test.log 2>&1 && tail -1 $(OUT)/sdhci_test.log || { cat $(OUT)/sdhci_test.log; rc=1; }; \ |
| 66 | if [ $$rc -eq 0 ]; then echo "AIENOS_DISK_NATIVE: PASS"; else echo "AIENOS_DISK_NATIVE: FAIL"; exit 1; fi |
| 67 | |
| 68 | # The library objects must not allocate, read a clock, or do their own I/O. |
| 69 | lib-checks: $(OBJS) |
| 70 | @bad=$$($(NM) -u $(OBJS) | awk 'NF {print $$NF}' | sed 's/^_//' | grep -xE 'malloc|calloc|realloc|free|aligned_alloc|posix_memalign|mmap|clock_gettime|time|gettimeofday|open|openat|read|write|pread|pwrite|fopen|fread|fwrite|printf|fprintf|puts|getrandom|rand|random'); \ |
| 71 | if [ -n "$$bad" ]; then echo "lib-checks FAIL: library imports $$bad"; exit 1; fi |
| 72 | @echo "lib-checks PASS (no heap, clock or direct I/O in libaienos_disk objects)" |
| 73 | |
| 74 | # Freestanding: no libc at all, not even memcpy/memset. |
| 75 | freestanding: | $(OUT) |
| 76 | @mkdir -p $(OUT)/free |
| 77 | $(CC) $(FREEFLAGS) -c -o $(OUT)/free/disk.o disk.c |
| 78 | $(CC) $(FREEFLAGS) -c -o $(OUT)/free/disk_nvme.o disk_nvme.c |
| 79 | $(CC) $(FREEFLAGS) -c -o $(OUT)/free/disk_sdhci.o disk_sdhci.c |
| 80 | $(LD) -r -o $(OUT)/free/disk_all.o $(OUT)/free/disk.o $(OUT)/free/disk_nvme.o $(OUT)/free/disk_sdhci.o |
| 81 | @u=$$($(NM) -u $(OUT)/free/disk_all.o | awk 'NF==2 {print $$2}'); \ |
| 82 | if [ -n "$$u" ]; then echo "freestanding FAIL: undefined symbols: $$u"; echo "AIENOS_DISK_FREESTANDING: FAIL"; exit 1; fi; \ |
| 83 | echo "nm -u: (empty)"; echo "AIENOS_DISK_FREESTANDING: PASS" |
| 84 | |
| 85 | sanitize: | $(OUT) |
| 86 | ifeq ($(shell uname -s),Linux) |
| 87 | $(CC) $(CFLAGS) $(SANFLAGS) -o $(OUT)/disk_test_asan tests/disk_test.c disk_file.c $(SRCS) |
| 88 | $(CC) $(CFLAGS) $(SANFLAGS) -o $(OUT)/nvme_test_asan tests/nvme_test.c $(SRCS) |
| 89 | $(CC) $(CFLAGS) $(SANFLAGS) -o $(OUT)/sdhci_test_asan tests/sdhci_test.c $(SRCS) |
| 90 | @rm -rf $(OUT)/scratch_asan && mkdir -p $(OUT)/scratch_asan |
| 91 | ASAN_OPTIONS=detect_leaks=1 UBSAN_OPTIONS=print_stacktrace=1:halt_on_error=1 ./$(OUT)/disk_test_asan $(OUT)/scratch_asan |
| 92 | ASAN_OPTIONS=detect_leaks=1 UBSAN_OPTIONS=print_stacktrace=1:halt_on_error=1 ./$(OUT)/nvme_test_asan |
| 93 | ASAN_OPTIONS=detect_leaks=1 UBSAN_OPTIONS=print_stacktrace=1:halt_on_error=1 ./$(OUT)/sdhci_test_asan > $(OUT)/sdhci_asan.log 2>&1 || { cat $(OUT)/sdhci_asan.log; exit 1; } |
| 94 | @echo "AIENOS_DISK_SANITIZE: PASS" |
| 95 | else |
| 96 | @echo "sanitize: Linux only" |
| 97 | endif |
| 98 | |
| 99 | # Mutants: delete each GUARD-tagged line in turn. All three test programs run |
| 100 | # against the mutant (under a timeout: deleting a poll bound can hang) and |
| 101 | # at least one must fail, crash or time out. |
| 102 | GUARDS = disk-zero disk-lba disk-end queue-bound queue-failstop \ |
| 103 | nvme-vs nvme-mpsmin nvme-mqes-admin nvme-mqes-io nvme-css \ |
| 104 | nvme-cfs-ready nvme-ready-bound nvme-cfs-cmd nvme-cmd-bound nvme-cid nvme-sqid \ |
| 105 | nvme-sqes nvme-cqes nvme-nsid nvme-nsze nvme-flbas nvme-meta nvme-lbads \ |
| 106 | nvme-failstop nvme-xfer nvme-range \ |
| 107 | prp-len-wrap prp-addr-wrap prp-list-cap atomic-unit atomic-boundary \ |
| 108 | sdhci-err-int sdhci-inhibit sdhci-r1 sdhci-ver sdhci-reset sdhci-volt sdhci-clock0 \ |
| 109 | sdhci-nocard sdhci-cmd8 sdhci-acmd41-bound sdhci-csd-zero sdhci-ready sdhci-range \ |
| 110 | sdhci-tran sdhci-failstop |
| 111 | |
| 112 | DISK_MUT_TEST_OBJS = $(OUT)/t_disk_test.o $(OUT)/t_nvme_test.o $(OUT)/t_sdhci_test.o |
| 113 | $(OUT)/t_%_test.o: tests/%_test.c $(HDRS) | $(OUT) |
| 114 | $(CC) $(CFLAGS) -Wno-error -I. -c -o $@ $< |
| 115 | |
| 116 | mutants: $(OBJS) $(FILE_OBJ) $(DISK_MUT_TEST_OBJS) | $(OUT) |
| 117 | @ulimit -c 0; fail=0; n=0; k=0; \ |
| 118 | dups=$$(printf '%s\n' $(GUARDS) | sort | uniq -d); \ |
| 119 | [ -z "$$dups" ] || { echo "duplicate guard names (parallel runs would collide):" $$dups; echo "AIENOS_DISK_MUTANTS: FAIL"; exit 1; }; \ |
| 120 | for g in $(GUARDS); do \ |
| 121 | n=$$((n+1)); d=$(OUT)/mut_$$g; rm -rf $$d; mkdir -p $$d/scratch; \ |
| 122 | ( f=$$(grep -l "GUARD:$$g \*/" $(SRCS)); \ |
| 123 | if [ $$(echo "$$f" | wc -w) -ne 1 ]; then echo "mutant $$g: guard tag missing or duplicated" > $$d/msg; echo 1 > $$d/fail; exit 0; fi; \ |
| 124 | if [ $$(grep -c "GUARD:$$g \*/" $$f) -ne 1 ]; then echo "mutant $$g: guard tag duplicated" > $$d/msg; echo 1 > $$d/fail; exit 0; fi; \ |
| 125 | sed "/GUARD:$$g \*\//d" $$f > $$d/$$f; \ |
| 126 | if cmp -s $$f $$d/$$f; then echo "mutant $$g: sed changed nothing (FAIL)" > $$d/msg; echo 1 > $$d/fail; exit 0; fi; \ |
| 127 | others=""; for s in $(SRCS); do [ "$$s" = "$$f" ] || others="$$others $(OUT)/$${s%.c}.o"; done; \ |
| 128 | $(CC) $(CFLAGS) -Wno-error -I. -c -o $$d/m.o $$d/$$f 2> $$d/cc.log \ |
| 129 | && $(CC) $(CFLAGS) -Wno-error -o $$d/dt $(OUT)/t_disk_test.o $(FILE_OBJ) $$d/m.o $$others 2>> $$d/cc.log \ |
| 130 | && $(CC) $(CFLAGS) -Wno-error -o $$d/nt $(OUT)/t_nvme_test.o $$d/m.o $$others 2>> $$d/cc.log \ |
| 131 | && $(CC) $(CFLAGS) -Wno-error -o $$d/st $(OUT)/t_sdhci_test.o $$d/m.o $$others 2>> $$d/cc.log \ |
| 132 | || { echo "mutant $$g: does not compile (FAIL)" > $$d/msg; echo 1 > $$d/fail; exit 0; }; \ |
| 133 | case "$$f" in \ |
| 134 | disk.c) order="dt nt st" ;; \ |
| 135 | disk_nvme.c) order="nt dt st" ;; \ |
| 136 | *) order="st dt nt" ;; \ |
| 137 | esac; \ |
| 138 | pass=1; : > $$d/run.log; \ |
| 139 | for b in $$order; do \ |
| 140 | if [ "$$b" = "dt" ]; then timeout $(MUTANT_TIMEOUT) ./$$d/dt $$d/scratch >> $$d/run.log 2>&1 || { pass=0; break; }; \ |
| 141 | else timeout $(MUTANT_TIMEOUT) ./$$d/$$b >> $$d/run.log 2>&1 || { pass=0; break; }; fi; \ |
| 142 | done; \ |
| 143 | if [ $$pass -eq 1 ]; then echo "mutant $$g: SURVIVED (FAIL)" > $$d/msg; echo 1 > $$d/fail; \ |
| 144 | else echo "mutant $$g: killed" > $$d/msg; : > $$d/killed; fi \ |
| 145 | ) & \ |
| 146 | done; \ |
| 147 | wait; \ |
| 148 | for g in $(GUARDS); do \ |
| 149 | d=$(OUT)/mut_$$g; \ |
| 150 | if [ -f $$d/msg ]; then cat $$d/msg; else echo "mutant $$g: no result recorded (FAIL)"; fi; \ |
| 151 | if [ -f $$d/killed ] && [ ! -f $$d/fail ]; then k=$$((k+1)); else fail=1; fi; \ |
| 152 | done; \ |
| 153 | echo "mutants killed: $$k/$$n"; \ |
| 154 | if [ $$fail -eq 0 ] && [ $$n -ge 10 ]; then echo "AIENOS_DISK_MUTANTS: PASS"; else echo "AIENOS_DISK_MUTANTS: FAIL"; exit 1; fi |
| 155 | |
| 156 | clean: |
| 157 | rm -rf $(OUT) |
| 158 | |
| 159 | .PHONY: all test lib-checks freestanding sanitize mutants clean |