Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/kernel/mm/mmu.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / mm / mmu.c` 527 lines ยท 19650 bytes Raw Blame History 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 /* mmu.c -- the kernel's own address space. Built while the firmware's 18 * translation is still live (identity), switched on at EL1 by ck_enter_el1. 19 * 20 * RAM (every WB-capable UEFI RAM type) Normal WB, RW, XN 21 * kernel image text RX, rodata/relocs RO+XN, data/bss RW+XN 22 * kernel stack (256 KiB) RW, unmapped guard page below 23 * heap (64 MiB) RW, unmapped guard page on each side 24 * DMA pool (24 MiB) Normal Non-cacheable (ck_dma_alloc) 25 * MMIO Device-nGnRE, XN (ck_mmio_map) 26 * GOP framebuffer (CHANDOF3) Device-nGnRE, XN (ck_mmio_map), visible 27 * pitch x height x 4 bytes, only outside RAM 28 * ACPI tables outside WB RAM Normal RO, XN 29 * 30 * Frames for tables, stack, heap and the pool come from the allocator, which 31 * only ever holds EfiConventionalMemory minus the image and the handoff. */ 32 #include "arch.h" 33 #include "ck_internal.h" 34 #include "frames.h" 35 #include "heap.h" 36 #include "pt.h" 37 38 /* Stack: stage code needs >= 128 KiB (m5_envelope_open alone has a ~64 KiB 39 * frame); 256 KiB gives 2x margin. Heap: the Store workspace and state are 40 * each over 1 MiB and the stage probe ran in a 64 MiB bump heap; 64 MiB with 41 * free() is the same budget. Both report their high-water marks. */ 42 #define STACK_BYTES (256ull << 10) 43 #define HEAP_BYTES (64ull << 20) 44 #define STACK_PAINT 0x5354414b5354414bull /* "KATSKATS": never-used stack words */ 45 #define DMA_BYTES (24ull << 20) /* 8 MiB of rings and queues + the VM screen picture (boot sequence, up to 1920x1080) */ 46 #define RAM_MAX 256 47 48 extern char __image_base[], __text_start[], __text_end[], __ro_start[], __ro_end[], 49 __rw_start[], __image_end[]; 50 51 uint64_t ck_stack_floor; /* read by vectors.S; 0 until the EL1 stack is live */ 52 53 static struct ck_frames frames; 54 static struct ck_pt pt; 55 static struct ck_heap heap; 56 static int heap_ready; 57 static size_t heap_min_free; 58 static struct ck_mm_report rep; 59 static uint64_t dma_cur; 60 static struct { 61 uint64_t base, end; 62 } ram[RAM_MAX]; 63 static unsigned ram_n; 64 65 /* Live exclusive MMIO ranges (ck_mmio_map_exclusive). Fixed table: when full, a 66 * new exclusive map is refused (fail closed). ck_mm_mmio_try_map and 67 * ck_mmio_map refuse to share these pages, so an unmap can never pull a 68 * mapping out from under another driver. */ 69 #define EXCL_MAX 8 70 static struct { 71 uint64_t lo, hi; 72 } excl[EXCL_MAX]; 73 static unsigned excl_n; 74 75 static int pt_alloc(void *ctx, uint64_t *phys) 76 { 77 (void)ctx; 78 if (ck_frames_alloc(&frames, 1, CK_PAGE, phys)) 79 return -1; 80 memset((void *)(uintptr_t)*phys, 0, CK_PAGE); 81 return 0; 82 } 83 84 static int ram_type(uint32_t t) 85 { 86 return (t >= 1 && t <= 7) || t == 9 || t == 10 || t == 14; 87 } 88 89 static int overlaps_ram(uint64_t lo, uint64_t hi) 90 { 91 for (unsigned i = 0; i < ram_n; i++) 92 if (lo < ram[i].end && ram[i].base < hi) 93 return 1; 94 return 0; 95 } 96 97 static int overlaps_excl(uint64_t lo, uint64_t hi) 98 { 99 for (unsigned i = 0; i < excl_n; i++) 100 if (lo < excl[i].hi && excl[i].lo < hi) 101 return 1; 102 return 0; 103 } 104 105 static void must(int rc, const char *what) 106 { 107 if (rc) 108 ck_panic("mm: %s failed rc=%d", what, rc); 109 } 110 111 static uint64_t take(uint64_t bytes, uint64_t align, const char *what) 112 { 113 uint64_t pa; 114 if (ck_frames_alloc(&frames, bytes / CK_PAGE, align, &pa)) 115 ck_panic("mm: out of frames for %s (%llu bytes)", what, (unsigned long long)bytes); 116 return pa; 117 } 118 119 static void tlb_sync(void) 120 { 121 __asm__ volatile("dsb ishst\n tlbi vmalle1is\n dsb ish\n isb" ::: "memory"); 122 } 123 124 125 static void map_acpi_page(uint64_t pa, uint64_t len) 126 { 127 uint64_t lo = pa & ~(CK_PAGE - 1), hi = (pa + len + CK_PAGE - 1) & ~(CK_PAGE - 1); 128 for (uint64_t p = lo; p < hi; p += CK_PAGE) { 129 uint64_t a; 130 if (ck_pt_lookup(&pt, p, &a, 0, 0) != 0) 131 must(ck_pt_map(&pt, p, p, CK_PAGE, CK_PT_NORMAL_RO), "acpi map"); 132 } 133 } 134 135 static void acpi_reserve_cb(uint64_t lo, uint64_t hi, void *ctx) 136 { 137 ck_frames_reserve(ctx, lo, hi); 138 } 139 140 static void acpi_cb(uint64_t table, uint32_t len, void *ctx) 141 { 142 (void)ctx; 143 map_acpi_page(table, len); 144 } 145 146 uint64_t ck_mm_build(const struct ck_handoff *h) 147 { 148 ck_frames_init(&frames); 149 if (ck_frames_from_efi(&frames, (const void *)(uintptr_t)h->memory_map, h->map_size, 150 h->desc_size) <= 0) 151 ck_panic("mm: no usable memory in the UEFI map"); 152 /* The image (with the handoff and the map copy in its BSS) is LoaderCode 153 * on UEFI already; reserve it explicitly anyway. */ 154 ck_frames_reserve(&frames, h->image_base, h->image_end); 155 ck_frames_reserve(&frames, (uint64_t)(uintptr_t)h, (uint64_t)(uintptr_t)h + sizeof *h); 156 ck_frames_reserve(&frames, h->memory_map, h->memory_map + h->map_size); 157 /* CHANDOF2: the model the stub read from the boot disk (EfiLoaderData, 158 * never handed out anyway); whole pages, explicit like the image. */ 159 if (h->model_flags & CK_HANDOFF_MODEL_PRESENT) 160 ck_frames_reserve(&frames, h->model_base, 161 (h->model_base + h->model_len + CK_PAGE - 1) & ~(uint64_t)(CK_PAGE - 1)); 162 /* ACPI: the RSDP, the XSDT/RSDT and every table the root lists, whole 163 * pages, before the first allocation. UEFI keeps them out of 164 * EfiConventionalMemory already; this holds even if a firmware does not. */ 165 if (h->rsdp) 166 ck_acpi_spans(h->rsdp, acpi_reserve_cb, &frames); 167 168 must(ck_pt_init(&pt, pt_alloc, 0), "pt init"); 169 170 /* RAM, identity, Normal WB, non-executable. */ 171 for (uint64_t off = 0; off + h->desc_size <= h->map_size; off += h->desc_size) { 172 const struct ck_efi_desc *d = (const void *)(uintptr_t)(h->memory_map + off); 173 if (!ram_type(d->type) || !(d->attr & 0x8 /* EFI_MEMORY_WB */) || !d->pages) 174 continue; 175 uint64_t len = d->pages * CK_PAGE; 176 must(ck_pt_map(&pt, d->phys, d->phys, len, CK_PT_NORMAL_RW), "ram map"); 177 if (ram_n < RAM_MAX) { 178 ram[ram_n].base = d->phys; 179 ram[ram_n].end = d->phys + len; 180 ram_n++; 181 } else { 182 ck_panic("mm: more than %d RAM ranges", RAM_MAX); 183 } 184 } 185 186 /* Kernel image permissions. */ 187 uint64_t ib = (uint64_t)(uintptr_t)__image_base; 188 must(ck_pt_map(&pt, ib, ib, (uint64_t)(uintptr_t)__text_start - ib, CK_PT_NORMAL_RO), "image header"); 189 must(ck_pt_map(&pt, (uint64_t)(uintptr_t)__text_start, (uint64_t)(uintptr_t)__text_start, 190 (uint64_t)(__text_end - __text_start), CK_PT_NORMAL_RX), "image text"); 191 must(ck_pt_map(&pt, (uint64_t)(uintptr_t)__ro_start, (uint64_t)(uintptr_t)__ro_start, 192 (uint64_t)(__ro_end - __ro_start), CK_PT_NORMAL_RO), "image rodata"); 193 must(ck_pt_map(&pt, (uint64_t)(uintptr_t)__rw_start, (uint64_t)(uintptr_t)__rw_start, 194 (uint64_t)(__image_end - __rw_start), CK_PT_NORMAL_RW), "image data"); 195 196 /* Stack with a guard page below. */ 197 uint64_t s = take(STACK_BYTES + CK_PAGE, CK_PAGE, "stack"); 198 must(ck_pt_unmap(&pt, s, CK_PAGE), "stack guard"); 199 rep.stack_guard = s; 200 rep.stack_lo = s + CK_PAGE; 201 rep.stack_hi = rep.stack_lo + STACK_BYTES; 202 for (uint64_t *w = (uint64_t *)(uintptr_t)rep.stack_lo; w < (uint64_t *)(uintptr_t)rep.stack_hi; w++) 203 *w = STACK_PAINT; 204 205 /* Heap with guard pages on both sides. */ 206 uint64_t hp = take(HEAP_BYTES + 2 * CK_PAGE, CK_PAGE, "heap"); 207 must(ck_pt_unmap(&pt, hp, CK_PAGE), "heap guard low"); 208 must(ck_pt_unmap(&pt, hp + CK_PAGE + HEAP_BYTES, CK_PAGE), "heap guard high"); 209 rep.heap_guard_lo = hp; 210 rep.heap_lo = hp + CK_PAGE; 211 rep.heap_hi = rep.heap_lo + HEAP_BYTES; 212 rep.heap_guard_hi = rep.heap_hi; 213 214 /* DMA pool: Normal Non-cacheable. Zero it through the firmware's cached 215 * mapping, then clean+invalidate so no dirty or stale line survives the 216 * attribute change. */ 217 uint64_t dp = take(DMA_BYTES, 2ull << 20, "dma pool"); 218 memset((void *)(uintptr_t)dp, 0, DMA_BYTES); 219 uint64_t ctr = ck_rd(ctr_el0); 220 uint64_t line = 4ull << ((ctr >> 16) & 0xf); 221 for (uint64_t a = dp; a < dp + DMA_BYTES; a += line) 222 __asm__ volatile("dc civac, %0" ::"r"(a) : "memory"); 223 __asm__ volatile("dsb sy" ::: "memory"); 224 must(ck_pt_map(&pt, dp, dp, DMA_BYTES, CK_PT_NORMAL_NC), "dma pool"); 225 rep.dma_lo = dp; 226 rep.dma_hi = dp + DMA_BYTES; 227 dma_cur = dp; 228 229 /* ACPI tables (and the RSDP) wherever the firmware put them. */ 230 if (h->rsdp) { 231 map_acpi_page(h->rsdp, 36); 232 int is_x = 0; 233 uint64_t root = ck_acpi_root(h->rsdp, &is_x); 234 if (root) 235 map_acpi_page(root, *(const uint32_t *)(uintptr_t)(root + 4)); 236 ck_acpi_each(h->rsdp, acpi_cb, 0); 237 } 238 239 /* Console UART, so output keeps working after the switch. */ 240 uint64_t ub = ck_console_uart_base(); 241 if (ub) 242 ck_mmio_map(ub, CK_PAGE); 243 244 /* GOP framebuffer (CHANDOF3, validated by handoff_check.c): Device-nGnRE 245 * like any MMIO, the bytes the console touches (pitch x height x 4). 246 * UEFI 2.10 12.9 says nothing about the memory attributes of the frame 247 * buffer after ExitBootServices (DOCS SILENT); on QEMU ramfb and, per 248 * Linux /proc/iomem, on the Spark it lies in a firmware-reserved range 249 * this kernel does not map as RAM. If it overlaps a RAM range mapped 250 * write-back the kernel refuses it (no second, mismatched mapping) and 251 * reports on the UART only. */ 252 if (h->fb_status == CK_HANDOFF_FB_PRESENT) { 253 uint64_t fb_len = (uint64_t)h->fb_pitch * h->fb_height * 4u; 254 uint64_t lo = h->fb_base & ~(CK_PAGE - 1); 255 uint64_t hi = (h->fb_base + fb_len + CK_PAGE - 1) & ~(CK_PAGE - 1); 256 rep.fb_map_bytes = fb_len; 257 if (overlaps_ram(lo, hi)) { 258 rep.fb_map = CK_MM_FB_OVERLAPS_RAM; 259 } else { 260 ck_mmio_map(h->fb_base, fb_len); 261 rep.fb_map = CK_MM_FB_MAPPED; 262 } 263 } 264 265 /* Tables were written through the firmware's cacheable mapping and our 266 * walks are cacheable too; make them visible before the switch. */ 267 __asm__ volatile("dsb ish" ::: "memory"); 268 rep.root = pt.root; 269 rep.pt_tables = pt.tables; 270 rep.ram_ranges = ram_n; 271 rep.free_bytes = ck_frames_free_bytes(&frames); 272 return rep.stack_hi; 273 } 274 275 void ck_mm_el1_ready(void) 276 { 277 pt.live = 1; 278 ck_stack_floor = rep.stack_lo; 279 if (ck_heap_init(&heap, (void *)(uintptr_t)rep.heap_lo, HEAP_BYTES)) 280 ck_panic("mm: heap init failed"); 281 heap_ready = 1; 282 heap_min_free = ck_heap_free_bytes(&heap); 283 } 284 285 void ck_mm_usage(struct ck_mm_usage *u) 286 { 287 u->stack_bytes = STACK_BYTES; 288 const uint64_t *w = (const uint64_t *)(uintptr_t)rep.stack_lo; 289 while ((uint64_t)(uintptr_t)w < rep.stack_hi && *w == STACK_PAINT) 290 w++; 291 u->stack_used = rep.stack_hi - (uint64_t)(uintptr_t)w; 292 u->heap_bytes = HEAP_BYTES; 293 u->heap_free = heap_ready ? ck_heap_free_bytes(&heap) : 0; 294 u->heap_min_free = heap_min_free; 295 } 296 297 const struct ck_mm_report *ck_mm_report(void) { return &rep; } 298 uint64_t ck_mm_mair(void) { return CK_MAIR_VALUE; } 299 300 uint64_t ck_mm_tcr(void) 301 { 302 uint64_t ips = ck_rd(id_aa64mmfr0_el1) & 7; 303 if (ips > 5) 304 ips = 5; /* 48-bit output is all a 4-level, 48-bit VA walk needs */ 305 return 16ull | (1ull << 8) | (1ull << 10) | (3ull << 12) | (16ull << 16) | (1ull << 23) | 306 (2ull << 30) | (ips << 32); 307 } 308 309 uint64_t ck_mm_sctlr(void) 310 { 311 return 0x30d00800ull | (1ull << 0) | (1ull << 2) | (1ull << 12); 312 } 313 314 void *ck_alloc(size_t bytes) 315 { 316 if (!heap_ready) 317 return 0; 318 void *p = ck_heap_alloc(&heap, bytes); 319 if (p) { 320 size_t fb = ck_heap_free_bytes(&heap); 321 if (fb < heap_min_free) 322 heap_min_free = fb; 323 } 324 return p; 325 } 326 327 void ck_free(void *p) 328 { 329 if (!p) 330 return; 331 if (!heap_ready || ck_heap_free(&heap, p)) 332 ck_panic("ck_free: bad pointer %p", p); 333 } 334 335 void *ck_dma_alloc(size_t bytes, size_t align, uint64_t *phys) 336 { 337 if (!bytes || align < CK_PAGE || (align & (align - 1))) 338 return 0; 339 uint64_t a = (dma_cur + align - 1) & ~((uint64_t)align - 1); 340 uint64_t len = ((uint64_t)bytes + CK_PAGE - 1) & ~(CK_PAGE - 1); 341 if (!dma_cur || a < dma_cur || a + len > rep.dma_hi) 342 return 0; 343 dma_cur = a + len; 344 memset((void *)(uintptr_t)a, 0, len); 345 if (phys) 346 *phys = a; 347 return (void *)(uintptr_t)a; 348 } 349 350 volatile void *ck_mmio_map(uint64_t phys, size_t len) 351 { 352 if (!len) 353 ck_panic("ck_mmio_map: zero length at 0x%llx", (unsigned long long)phys); 354 uint64_t lo = phys & ~(CK_PAGE - 1); 355 uint64_t hi = (phys + len + CK_PAGE - 1) & ~(CK_PAGE - 1); 356 if (hi < lo || overlaps_ram(lo, hi)) 357 ck_panic("ck_mmio_map: 0x%llx+0x%llx overlaps RAM", (unsigned long long)phys, 358 (unsigned long long)len); 359 if (overlaps_excl(lo, hi)) 360 ck_panic("ck_mmio_map: 0x%llx+0x%llx overlaps an exclusive MMIO window", (unsigned long long)phys, 361 (unsigned long long)len); 362 int rc = ck_pt_map(&pt, lo, lo, hi - lo, CK_PT_DEVICE); 363 if (rc) 364 ck_panic("ck_mmio_map: 0x%llx+0x%llx rc=%d", (unsigned long long)phys, 365 (unsigned long long)len, rc); 366 if (pt.live) 367 tlb_sync(); 368 return (volatile void *)(uintptr_t)phys; 369 } 370 371 int ck_mm_guard_selftest(char *detail, size_t n) 372 { 373 const struct { 374 const char *name; 375 uint64_t addr; 376 } g[] = { 377 {"stack", rep.stack_guard}, 378 {"heap_lo", rep.heap_guard_lo}, 379 {"heap_hi", rep.heap_guard_hi}, 380 }; 381 /* Control: a mapped heap word must read without a fault. */ 382 if (ck_probe_read((const volatile void *)(uintptr_t)rep.heap_lo) != 0) { 383 ck_snprintf(detail, n, "control read of heap faulted"); 384 return -1; 385 } 386 for (unsigned i = 0; i < sizeof g / sizeof g[0]; i++) { 387 uint64_t a = g[i].addr + 8; 388 ck_probe_state.esr = ck_probe_state.far = 0; 389 if (ck_probe_read((const volatile void *)(uintptr_t)a) != 1) { 390 ck_snprintf(detail, n, "%s guard 0x%llx did not fault", g[i].name, (unsigned long long)a); 391 return -1; 392 } 393 uint64_t ec = (ck_probe_state.esr >> 26) & 0x3f; 394 uint64_t dfsc = ck_probe_state.esr & 0x3f; 395 if (ec != 0x25 || ck_probe_state.far != a || (dfsc & 0x3c) != 0x04) { 396 ck_snprintf(detail, n, "%s guard: unexpected esr=0x%llx far=0x%llx", g[i].name, 397 (unsigned long long)ck_probe_state.esr, 398 (unsigned long long)ck_probe_state.far); 399 return -1; 400 } 401 } 402 ck_snprintf(detail, n, "stack=0x%llx heap_lo=0x%llx heap_hi=0x%llx", 403 (unsigned long long)rep.stack_guard, (unsigned long long)rep.heap_guard_lo, 404 (unsigned long long)rep.heap_guard_hi); 405 return 0; 406 } 407 408 /* ---- artifact loader support (core/artifact_loader.c) ---- */ 409 410 int ck_mm_frames_alloc(uint64_t npages, uint64_t *pa) 411 { 412 return npages ? ck_frames_alloc(&frames, npages, CK_PAGE, pa) : -1; 413 } 414 415 int ck_mm_frames_free(uint64_t pa, uint64_t npages) 416 { 417 return ck_frames_add(&frames, pa, pa + npages * CK_PAGE); 418 } 419 420 uint64_t ck_mm_free_frames(void) { return ck_frames_free_bytes(&frames) / CK_PAGE; } 421 422 int ck_mm_mapped(uint64_t pa, uint64_t len) 423 { 424 uint64_t lo = pa & ~(CK_PAGE - 1), hi = pa + len; 425 if (hi < pa) 426 return 0; 427 for (uint64_t p = lo; p < hi; p += CK_PAGE) { 428 uint64_t a; 429 if (ck_pt_lookup(&pt, p, &a, 0, 0) != 0 || a != p) 430 return 0; 431 } 432 return 1; 433 } 434 435 volatile void *ck_mm_mmio_try_map(uint64_t phys, size_t len) 436 { 437 uint64_t lo = phys & ~(CK_PAGE - 1); 438 uint64_t hi = (phys + len + CK_PAGE - 1) & ~(CK_PAGE - 1); 439 if (!len || hi <= lo || overlaps_ram(lo, hi) || overlaps_excl(lo, hi)) 440 return 0; 441 for (uint64_t p = lo; p < hi; p += CK_PAGE) { 442 uint64_t a; 443 if (ck_pt_lookup(&pt, p, &a, 0, 0) == 0) { 444 if (a != p) 445 return 0; 446 continue; 447 } 448 if (ck_pt_map(&pt, p, p, CK_PAGE, CK_PT_DEVICE)) 449 return 0; 450 } 451 if (pt.live) 452 tlb_sync(); 453 return (volatile void *)(uintptr_t)phys; 454 } 455 456 volatile void *ck_mmio_try_map(uint64_t phys, size_t len) 457 { 458 return len ? ck_mm_mmio_try_map(phys, len) : 0; 459 } 460 461 /* Exclusive map for a caller that unmaps again (mmio_window.c). All or 462 * nothing: if any page is already mapped, by anyone, nothing is mapped. Pages 463 * are mapped one at a time at level 3 so the unmap never needs to split a 464 * block (a split is refused on live tables). */ 465 volatile void *ck_mmio_map_exclusive(uint64_t phys, size_t len) 466 { 467 uint64_t lo = phys & ~(CK_PAGE - 1); 468 uint64_t hi = (phys + len + CK_PAGE - 1) & ~(CK_PAGE - 1); 469 if (!len || hi <= lo || overlaps_ram(lo, hi) || overlaps_excl(lo, hi) || 470 excl_n >= EXCL_MAX) 471 return 0; 472 for (uint64_t p = lo; p < hi; p += CK_PAGE) 473 if (ck_pt_lookup(&pt, p, 0, 0, 0) == 0) 474 return 0; 475 for (uint64_t p = lo; p < hi; p += CK_PAGE) 476 if (ck_pt_map(&pt, p, p, CK_PAGE, CK_PT_DEVICE)) { 477 (void)ck_pt_unmap(&pt, lo, p - lo); /* undo: these pages were ours a moment ago */ 478 if (pt.live) 479 tlb_sync(); 480 return 0; 481 } 482 excl[excl_n].lo = lo; 483 excl[excl_n++].hi = hi; 484 if (pt.live) 485 tlb_sync(); 486 return (volatile void *)(uintptr_t)phys; 487 } 488 489 /* Break before make: the entries are made invalid first, then the TLB is 490 * invalidated and synchronised (tlb_sync) before this returns, so no stale 491 * translation outlives the call and a later map of the same pages starts from 492 * invalid entries. */ 493 int ck_mmio_unmap_exclusive(uint64_t phys, size_t len) 494 { 495 uint64_t lo = phys & ~(CK_PAGE - 1); 496 uint64_t hi = (phys + len + CK_PAGE - 1) & ~(CK_PAGE - 1); 497 if (!len || hi <= lo || overlaps_ram(lo, hi)) 498 return -1; 499 unsigned e = 0; 500 while (e < excl_n && !(excl[e].lo == lo && excl[e].hi == hi)) 501 e++; 502 if (e == excl_n) /* only a range handed out by ck_mmio_map_exclusive can be taken back */ 503 return -1; 504 for (uint64_t p = lo; p < hi; p += CK_PAGE) { 505 uint64_t a, at; 506 int lv; 507 /* Only a Device identity page of its own: never RAM, a block, or a page with other attributes. */ 508 if (ck_pt_lookup(&pt, p, &a, &at, &lv) != 0 || a != p || lv != 3 || at != CK_PT_DEVICE) 509 return -1; 510 } 511 if (ck_pt_unmap(&pt, lo, hi - lo)) 512 return -1; 513 excl[e] = excl[--excl_n]; /* the range is free for ordinary maps again */ 514 if (pt.live) 515 tlb_sync(); 516 return 0; 517 } 518 519 int ck_mmio_is_mapped(uint64_t phys, size_t len) 520 { 521 uint64_t lo = phys & ~(CK_PAGE - 1); 522 uint64_t hi = (phys + len + CK_PAGE - 1) & ~(CK_PAGE - 1); 523 for (uint64_t p = lo; len && p < hi; p += CK_PAGE) 524 if (ck_pt_lookup(&pt, p, 0, 0, 0) == 0) 525 return 1; 526 return 0; 527 }