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 / kernel / svc / tests / ck_host.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* ck_host.c -- host implementation of the ck.h services for the stage host |
| 18 | * tests (malloc + stdio). Hosted test code only; never in the image. |
| 19 | * Identity "mapping": ck_mmio_map returns the address it is given, so a |
| 20 | * test hands MCFG an aligned heap buffer as the fake ECAM. */ |
| 21 | #include <stdarg.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <time.h> |
| 26 | #include <sys/mman.h> |
| 27 | #include <unistd.h> |
| 28 | #include "ck.h" |
| 29 | #include "ck_compat.h" |
| 30 | #include "ck_host.h" |
| 31 | |
| 32 | const void *ck_host_mcfg; |
| 33 | int ck_host_quiet; |
| 34 | int ck_host_try_map_ok; |
| 35 | uint64_t ck_host_try_map_fail; |
| 36 | |
| 37 | /* Optional text capture so a test can check what a stage printed. */ |
| 38 | static char cap_buf[32768]; |
| 39 | static size_t cap_len; |
| 40 | static int cap_on; |
| 41 | void ck_host_capture_start(void) { cap_len = 0; cap_buf[0] = 0; cap_on = 1; } |
| 42 | const char *ck_host_capture_text(void) { return cap_buf; } |
| 43 | void ck_host_capture_stop(void) { cap_on = 0; } |
| 44 | static void cap_add(const char *s, size_t n) |
| 45 | { |
| 46 | if (!cap_on) return; |
| 47 | if (n > sizeof cap_buf - 1 - cap_len) n = sizeof cap_buf - 1 - cap_len; |
| 48 | memcpy(cap_buf + cap_len, s, n); |
| 49 | cap_len += n; |
| 50 | cap_buf[cap_len] = 0; |
| 51 | } |
| 52 | |
| 53 | void ck_puts(const char *s) |
| 54 | { |
| 55 | cap_add(s, strlen(s)); |
| 56 | if (!ck_host_quiet) fputs(s, stdout); |
| 57 | } |
| 58 | void ck_vprintf(const char *fmt, va_list ap) |
| 59 | { |
| 60 | if (cap_on) { |
| 61 | char tmp[512]; |
| 62 | va_list c; |
| 63 | va_copy(c, ap); |
| 64 | int n = vsnprintf(tmp, sizeof tmp, fmt, c); |
| 65 | va_end(c); |
| 66 | if (n > 0) cap_add(tmp, (size_t)n < sizeof tmp ? (size_t)n : sizeof tmp - 1); |
| 67 | } |
| 68 | if (!ck_host_quiet) vprintf(fmt, ap); |
| 69 | } |
| 70 | void ck_printf(const char *fmt, ...) |
| 71 | { |
| 72 | va_list ap; |
| 73 | va_start(ap, fmt); |
| 74 | ck_vprintf(fmt, ap); |
| 75 | va_end(ap); |
| 76 | } |
| 77 | void ck_panic(const char *fmt, ...) |
| 78 | { |
| 79 | va_list ap; |
| 80 | va_start(ap, fmt); |
| 81 | fputs("ck_panic: ", stdout); |
| 82 | vprintf(fmt, ap); |
| 83 | va_end(ap); |
| 84 | fflush(stdout); |
| 85 | abort(); |
| 86 | } |
| 87 | void *ck_alloc(size_t bytes) { return calloc(1, bytes ? bytes : 1); } |
| 88 | void ck_free(void *p) { free(p); } |
| 89 | /* Host: the kernel never frees DMA regions; the shim remembers them and frees at exit so leak checks stay useful. */ |
| 90 | static void *host_dma[32]; |
| 91 | static unsigned host_dma_n; |
| 92 | static void host_dma_free_all(void) |
| 93 | { |
| 94 | while (host_dma_n) free(host_dma[--host_dma_n]); |
| 95 | } |
| 96 | void *ck_dma_alloc(size_t bytes, size_t align, uint64_t *phys) |
| 97 | { |
| 98 | if (align < 16) align = 16; |
| 99 | size_t n = (bytes + align - 1) / align * align; |
| 100 | void *p = aligned_alloc(align, n); |
| 101 | if (!p) return NULL; |
| 102 | memset(p, 0, n); |
| 103 | if (host_dma_n < 32) { |
| 104 | if (host_dma_n == 0) atexit(host_dma_free_all); |
| 105 | host_dma[host_dma_n++] = p; |
| 106 | } |
| 107 | if (phys) *phys = (uint64_t)(uintptr_t)p; |
| 108 | return p; |
| 109 | } |
| 110 | /* Host: no platform MMIO by default (no DSDT unless ck_host_plat_set injects |
| 111 | * one). A test may turn on identity mapping (ck_host_try_map_ok) and make |
| 112 | * one exact address fail (ck_host_try_map_fail). |
| 113 | * |
| 114 | * The fake also keeps a tiny page map so exclusive ownership and unmap can be |
| 115 | * tested: try_map (shared, like the kernel's) and map_exclusive record the |
| 116 | * page range as mapped; map_exclusive refuses when any page is recorded; |
| 117 | * unmap_exclusive turns the range into PROT_NONE so a raw access really |
| 118 | * faults, and a later map_exclusive restores PROT_READ|PROT_WRITE. */ |
| 119 | #define HOST_PAGE 4096ull |
| 120 | #define HOST_MAPS 16 |
| 121 | static struct { uint64_t lo, hi; int unmapped, excl; } hmap[HOST_MAPS]; |
| 122 | static unsigned hmap_n; |
| 123 | int ck_host_unmap_calls; |
| 124 | |
| 125 | static void host_range(uint64_t phys, size_t len, uint64_t *lo, uint64_t *hi) |
| 126 | { |
| 127 | *lo = phys & ~(HOST_PAGE - 1); |
| 128 | *hi = (phys + len + HOST_PAGE - 1) & ~(HOST_PAGE - 1); |
| 129 | } |
| 130 | static int host_find(uint64_t lo, uint64_t hi, int unmapped) |
| 131 | { |
| 132 | for (unsigned i = 0; i < hmap_n; i++) |
| 133 | if (hmap[i].unmapped == unmapped && lo < hmap[i].hi && hmap[i].lo < hi) return (int)i; |
| 134 | return -1; |
| 135 | } |
| 136 | static void host_drop(int i) { hmap[i] = hmap[--hmap_n]; } |
| 137 | static void host_add(uint64_t lo, uint64_t hi, int excl) |
| 138 | { |
| 139 | if (hmap_n < HOST_MAPS) hmap[hmap_n++] = (typeof(hmap[0])){ lo, hi, 0, excl }; |
| 140 | } |
| 141 | /* Same rule as the kernel: a live exclusive range is never shared. */ |
| 142 | static int host_excl_live(uint64_t lo, uint64_t hi) |
| 143 | { |
| 144 | for (unsigned i = 0; i < hmap_n; i++) |
| 145 | if (hmap[i].excl && !hmap[i].unmapped && lo < hmap[i].hi && hmap[i].lo < hi) return 1; |
| 146 | return 0; |
| 147 | } |
| 148 | void ck_host_mmio_reset(void) { hmap_n = 0; ck_host_unmap_calls = 0; } |
| 149 | |
| 150 | volatile void *ck_mmio_try_map(uint64_t phys, size_t len) |
| 151 | { |
| 152 | if (!ck_host_try_map_ok || (ck_host_try_map_fail && phys == ck_host_try_map_fail)) return NULL; |
| 153 | uint64_t lo, hi; |
| 154 | host_range(phys, len, &lo, &hi); |
| 155 | if (host_excl_live(lo, hi)) return NULL; |
| 156 | for (int i; (i = host_find(lo, hi, 1)) >= 0;) { /* pages given back earlier: usable again */ |
| 157 | mprotect((void *)(uintptr_t)hmap[i].lo, hmap[i].hi - hmap[i].lo, PROT_READ | PROT_WRITE); |
| 158 | host_drop(i); |
| 159 | } |
| 160 | if (host_find(lo, hi, 0) < 0) host_add(lo, hi, 0); |
| 161 | return (volatile void *)(uintptr_t)phys; |
| 162 | } |
| 163 | volatile void *ck_mmio_map_exclusive(uint64_t phys, size_t len) |
| 164 | { |
| 165 | if (!len || !ck_host_try_map_ok || (ck_host_try_map_fail && phys == ck_host_try_map_fail)) return NULL; |
| 166 | uint64_t lo, hi; |
| 167 | host_range(phys, len, &lo, &hi); |
| 168 | if (hmap_n >= HOST_MAPS || host_find(lo, hi, 0) >= 0) return NULL; |
| 169 | for (int i; (i = host_find(lo, hi, 1)) >= 0;) { /* pages given back earlier: usable again */ |
| 170 | mprotect((void *)(uintptr_t)hmap[i].lo, hmap[i].hi - hmap[i].lo, PROT_READ | PROT_WRITE); |
| 171 | host_drop(i); |
| 172 | } |
| 173 | host_add(lo, hi, 1); |
| 174 | return (volatile void *)(uintptr_t)phys; |
| 175 | } |
| 176 | int ck_mmio_is_mapped(uint64_t phys, size_t len) |
| 177 | { |
| 178 | uint64_t lo, hi; |
| 179 | host_range(phys, len, &lo, &hi); |
| 180 | return len && host_find(lo, hi, 0) >= 0; |
| 181 | } |
| 182 | volatile void *ck_mmio_map(uint64_t phys, size_t len) |
| 183 | { |
| 184 | if (len && host_excl_live(phys & ~(HOST_PAGE - 1), (phys + len + HOST_PAGE - 1) & ~(HOST_PAGE - 1))) |
| 185 | ck_panic("ck_mmio_map: 0x%llx overlaps an exclusive MMIO window", (unsigned long long)phys); |
| 186 | return (volatile void *)(uintptr_t)phys; |
| 187 | } |
| 188 | int ck_mmio_unmap_exclusive(uint64_t phys, size_t len) |
| 189 | { |
| 190 | uint64_t lo, hi; |
| 191 | host_range(phys, len, &lo, &hi); |
| 192 | int i = len ? host_find(lo, hi, 0) : -1; |
| 193 | if (i < 0 || !hmap[i].excl || hmap[i].lo != lo || hmap[i].hi != hi) return -1; |
| 194 | ck_host_unmap_calls++; |
| 195 | hmap[i].unmapped = 1; |
| 196 | return mprotect((void *)(uintptr_t)lo, hi - lo, PROT_NONE) == 0 ? 0 : -1; |
| 197 | } |
| 198 | /* Host: no DSDT unless a test injects one platform device (ck_host_plat_set). */ |
| 199 | static struct ck_platform_dev host_plat; |
| 200 | static int host_plat_on; |
| 201 | void ck_host_plat_set(const char *name, const char *hid, uint64_t mmio_base, uint64_t mmio_len) |
| 202 | { |
| 203 | memset(&host_plat, 0, sizeof host_plat); |
| 204 | if (name) |
| 205 | for (size_t i = 0; i + 1 < sizeof host_plat.name && name[i]; i++) host_plat.name[i] = name[i]; |
| 206 | if (hid) |
| 207 | for (size_t i = 0; i + 1 < sizeof host_plat.hid && hid[i]; i++) host_plat.hid[i] = hid[i]; |
| 208 | host_plat.table[0] = 'D'; host_plat.table[1] = 'S'; host_plat.table[2] = 'D'; host_plat.table[3] = 'T'; |
| 209 | host_plat.mmio_base = mmio_base; |
| 210 | host_plat.mmio_len = mmio_len; |
| 211 | host_plat_on = 1; |
| 212 | } |
| 213 | void ck_host_plat_clear(void) { host_plat_on = 0; } |
| 214 | int ck_acpi_platform_devices(const char *const *ids, unsigned nids, struct ck_platform_dev *out, unsigned max, |
| 215 | struct ck_acpi_scan_info *info) |
| 216 | { |
| 217 | if (info) info->tables = info->refused = info->devices = 0, info->first_refusal = 0; |
| 218 | if (!host_plat_on) return -1; |
| 219 | if (info) info->tables = info->devices = 1; |
| 220 | int match = 0; |
| 221 | for (unsigned i = 0; i < nids && ids && ids[i]; i++) |
| 222 | if ((host_plat.hid[0] && strcmp(host_plat.hid, ids[i]) == 0) || |
| 223 | (host_plat.cid[0] && strcmp(host_plat.cid, ids[i]) == 0)) |
| 224 | match = 1; |
| 225 | if (!match) return 0; |
| 226 | if (out && max) *out = host_plat; |
| 227 | return 1; |
| 228 | } |
| 229 | void (*ck_host_mb_hook)(void); |
| 230 | void (*ck_host_udelay_hook)(void); |
| 231 | void ck_mb(void) |
| 232 | { |
| 233 | __sync_synchronize(); |
| 234 | if (ck_host_mb_hook) ck_host_mb_hook(); |
| 235 | } |
| 236 | const void *ck_acpi_find(const char sig[4]) |
| 237 | { |
| 238 | if (memcmp(sig, "MCFG", 4) == 0) return ck_host_mcfg; |
| 239 | return NULL; |
| 240 | } |
| 241 | uint64_t ck_time_us(void) |
| 242 | { |
| 243 | struct timespec ts; |
| 244 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 245 | return (uint64_t)ts.tv_sec * 1000000u + (uint64_t)ts.tv_nsec / 1000u; |
| 246 | } |
| 247 | void ck_udelay(uint32_t us) |
| 248 | { |
| 249 | (void)us; |
| 250 | if (ck_host_udelay_hook) ck_host_udelay_hook(); |
| 251 | } |
| 252 | const char *ck_commit(void) { return "host-test-commit"; } |
| 253 | unsigned ck_exception_level(void) { return 1; } |
| 254 | uint64_t ck_conventional_memory_kb(void) { return 42; } |
| 255 | uint32_t ck_boot_count_hint(void) { return 0; } |
| 256 | |
| 257 | /* Host: the capability library reads the real /dev/urandom. */ |
| 258 | int ck_compat_entropy_source(void) { return CK_ENTROPY_HOST; } |
| 259 | /* Host: the kernel RNDR service is not in play (libc /dev/urandom above). */ |
| 260 | int ck_entropy_status(void) { return 0; } |
| 261 | const char *ck_entropy_reason(void) { return "host-urandom"; } |
| 262 | /* Host: deterministic fill (a counter), so key-slot genesis tests are reproducible. |
| 263 | * ck_host_entropy_refuse = 1 models an RNDR refusal (fill returns -1, buffer zeroed). */ |
| 264 | int ck_host_entropy_refuse; |
| 265 | int ck_entropy_fill(void *buf, size_t len) |
| 266 | { |
| 267 | static uint32_t ctr = 0x1000; |
| 268 | uint8_t *b = buf; |
| 269 | if (ck_host_entropy_refuse) { |
| 270 | for (size_t i = 0; i < len; i++) b[i] = 0; |
| 271 | return -1; |
| 272 | } |
| 273 | for (size_t i = 0; i < len; i++) b[i] = (uint8_t)(0xA5u ^ (ctr++ * 2654435761u >> 24)); |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | /* Host: no IORT by default, so no SMMU (the NVMe gate stays fail-closed). A test may set |
| 278 | * ck_host_confine_rc = 0 to model an SMMU that confines; every call is recorded. |
| 279 | * ck_host_confine_named_rc is the ACPI-name path (platform xHCI). |
| 280 | * ck_host_unconfine_fail = 1 models an SMMU that refuses the abort (CK_SMMU_FAILED). |
| 281 | * ck_host_unconfine_allow lets that many aborts succeed first, then the refusal starts. */ |
| 282 | int ck_host_confine_rc = CK_SMMU_ABSENT; |
| 283 | int ck_host_confine_named_rc = CK_SMMU_ABSENT; |
| 284 | int ck_host_unconfine_fail; |
| 285 | unsigned ck_host_unconfine_allow; |
| 286 | unsigned ck_host_confine_calls, ck_host_unconfine_calls; |
| 287 | uint32_t ck_host_confine_segment, ck_host_confine_rid; |
| 288 | uint64_t ck_host_confine_phys, ck_host_confine_len; |
| 289 | int ck_dma_confine(uint32_t segment, uint32_t rid, uint64_t phys, uint64_t len, struct ck_dma_confinement *out) |
| 290 | { |
| 291 | ck_host_confine_calls++; |
| 292 | ck_host_confine_segment = segment; |
| 293 | ck_host_confine_rid = rid; |
| 294 | ck_host_confine_phys = phys; |
| 295 | ck_host_confine_len = len; |
| 296 | if (ck_host_confine_rc == 0 && out) { |
| 297 | out->smmu_base = 0x2b400000ull; |
| 298 | out->stream_id = 0x40u | rid; |
| 299 | out->iova = phys; |
| 300 | out->len = len; |
| 301 | } |
| 302 | return ck_host_confine_rc; |
| 303 | } |
| 304 | int ck_dma_confine_named(const char *acpi_name, uint64_t phys, uint64_t len, struct ck_dma_confinement *out) |
| 305 | { |
| 306 | (void)acpi_name; |
| 307 | ck_host_confine_calls++; |
| 308 | if (ck_host_confine_named_rc == 0 && out) { |
| 309 | out->smmu_base = 0x13800000ull; |
| 310 | out->stream_id = 0x5u; |
| 311 | out->iova = phys; |
| 312 | out->len = len; |
| 313 | } |
| 314 | return ck_host_confine_named_rc; |
| 315 | } |
| 316 | int ck_dma_unconfine(uint32_t stream_id) |
| 317 | { |
| 318 | (void)stream_id; |
| 319 | ck_host_unconfine_calls++; |
| 320 | if (ck_host_unconfine_fail) { |
| 321 | if (ck_host_unconfine_allow) { |
| 322 | ck_host_unconfine_allow--; |
| 323 | return 0; |
| 324 | } |
| 325 | return CK_SMMU_FAILED; |
| 326 | } |
| 327 | return ck_host_confine_rc == 0 ? 0 : CK_SMMU_EARG; |
| 328 | } |
| 329 | /* SMMU fault queue model. ck_host_faults_up = 0 (default): no SMMU is up, -1. |
| 330 | * Otherwise ck_host_fault_n events for ck_host_fault_sid are pending (the first |
| 331 | * is ck_host_fault_first); a call naming that stream drains them all. */ |
| 332 | int ck_host_faults_up; |
| 333 | int ck_host_fault_n; |
| 334 | uint32_t ck_host_fault_sid; |
| 335 | struct ck_dma_fault ck_host_fault_first; |
| 336 | unsigned ck_host_fault_calls; |
| 337 | int ck_dma_faults(uint32_t stream_id, struct ck_dma_fault *first) |
| 338 | { |
| 339 | if (!ck_host_faults_up) |
| 340 | return -1; |
| 341 | ck_host_fault_calls++; |
| 342 | if (stream_id != ck_host_fault_sid || ck_host_fault_n <= 0) |
| 343 | return 0; |
| 344 | int n = ck_host_fault_n; |
| 345 | ck_host_fault_n = 0; |
| 346 | if (first) |
| 347 | *first = ck_host_fault_first; |
| 348 | return n; |
| 349 | } |