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 / dev / mmio_window.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 | /* mmio_window.c -- see mmio_window.h. Freestanding. */ |
| 18 | #include "mmio_window.h" |
| 19 | #include "ck.h" |
| 20 | |
| 21 | void ck_mmio_registry_init(struct ck_mmio_registry *r) |
| 22 | { |
| 23 | for (unsigned i = 0; i < CK_MMIO_WINDOWS; i++) |
| 24 | r->w[i] = (struct ck_mmio_window){ 0 }; |
| 25 | r->next = 0; |
| 26 | } |
| 27 | |
| 28 | int ck_mmio_bar_addr(const pci_found *f, unsigned bar, uint64_t *addr) |
| 29 | { |
| 30 | if (!f || !addr || bar >= PCI_MAX_BARS) |
| 31 | return CK_MMIO_E_ARG; |
| 32 | uint32_t raw = f->bar_raw[bar]; |
| 33 | if (raw & 1u) /* I/O BAR */ |
| 34 | return CK_MMIO_E_BAR; |
| 35 | uint64_t a = raw & ~0xfu; |
| 36 | switch ((raw >> 1) & 3u) { |
| 37 | case 0: /* 32-bit */ |
| 38 | break; |
| 39 | case 2: /* 64-bit: the next register holds the upper half */ |
| 40 | if (bar + 1 >= PCI_MAX_BARS) |
| 41 | return CK_MMIO_E_BAR; |
| 42 | a |= (uint64_t)f->bar_raw[bar + 1] << 32; |
| 43 | break; |
| 44 | default: /* reserved (type 1 is the legacy below-1-MiB type) */ |
| 45 | return CK_MMIO_E_BAR; |
| 46 | } |
| 47 | if (a == 0) |
| 48 | return CK_MMIO_E_BAR; |
| 49 | *addr = a; |
| 50 | return CK_MMIO_OK; |
| 51 | } |
| 52 | |
| 53 | int ck_mmio_window_create(struct ck_mmio_registry *r, struct ck_cap_table *t, const pci_found *f, unsigned bar, |
| 54 | uint64_t size, const volatile void *base, unsigned rights, struct ck_handle *out) |
| 55 | { |
| 56 | if (!r || !t || !f || !out || !base || size == 0) |
| 57 | return CK_MMIO_E_ARG; |
| 58 | /* No write path exists, so a write (or map/grant) right is refused here |
| 59 | * instead of being granted and ignored. */ |
| 60 | if ((rights & ~CK_MMIO_RIGHTS) || !(rights & CK_R_READ)) |
| 61 | return CK_MMIO_E_RIGHTS; |
| 62 | uint64_t addr; |
| 63 | int rc = ck_mmio_bar_addr(f, bar, &addr); |
| 64 | if (rc) |
| 65 | return rc; |
| 66 | if (addr + size < addr) |
| 67 | return CK_MMIO_E_RANGE; |
| 68 | if (r->next >= CK_MMIO_WINDOWS) |
| 69 | return CK_MMIO_E_FULL; |
| 70 | uint32_t resource = CK_MMIO_RESOURCE_BASE + r->next; |
| 71 | rc = ck_cap_insert(t, resource, rights, out); |
| 72 | if (rc) |
| 73 | return rc; |
| 74 | r->w[r->next] = (struct ck_mmio_window){ .used = 1, .resource = resource, .bus_addr = addr, .size = size, |
| 75 | .base = (const volatile uint8_t *)base }; |
| 76 | r->next++; /* never decremented: a resource number is not reused */ |
| 77 | return CK_MMIO_OK; |
| 78 | } |
| 79 | |
| 80 | int ck_mmio_window_open(struct ck_mmio_registry *r, struct ck_cap_table *t, const pci_found *f, unsigned bar, |
| 81 | uint64_t size, unsigned rights, struct ck_handle *out) |
| 82 | { |
| 83 | if (!r || !t || !f || !out || size == 0) |
| 84 | return CK_MMIO_E_ARG; |
| 85 | if ((rights & ~CK_MMIO_RIGHTS) || !(rights & CK_R_READ)) |
| 86 | return CK_MMIO_E_RIGHTS; |
| 87 | uint64_t addr; |
| 88 | int rc = ck_mmio_bar_addr(f, bar, &addr); |
| 89 | if (rc) |
| 90 | return rc; |
| 91 | if (addr + size < addr) |
| 92 | return CK_MMIO_E_RANGE; |
| 93 | if (r->next >= CK_MMIO_WINDOWS) |
| 94 | return CK_MMIO_E_FULL; |
| 95 | /* Exclusive: a page some other driver already mapped is refused, so the |
| 96 | * unmap on revoke can never pull a mapping out from under it. */ |
| 97 | const volatile void *m = ck_mmio_map_exclusive(addr, (size_t)size); |
| 98 | if (!m) |
| 99 | return CK_MMIO_E_MAP; |
| 100 | uint32_t idx = r->next; |
| 101 | rc = ck_mmio_window_create(r, t, f, bar, size, m, rights, out); |
| 102 | if (rc) { |
| 103 | (void)ck_mmio_unmap_exclusive(addr, (size_t)size); |
| 104 | return rc; |
| 105 | } |
| 106 | r->w[idx].owned = r->w[idx].mapped = 1; |
| 107 | return CK_MMIO_OK; |
| 108 | } |
| 109 | |
| 110 | int ck_mmio_revoke(struct ck_mmio_registry *r, uint32_t table_id, struct ck_handle h, |
| 111 | struct ck_cap_table *const *tables, unsigned ntables) |
| 112 | { |
| 113 | if (!r || !tables) |
| 114 | return CK_MMIO_E_ARG; |
| 115 | uint32_t resource = 0; |
| 116 | int named = 0; |
| 117 | for (unsigned i = 0; i < ntables; i++) |
| 118 | if (tables[i]->id == table_id) |
| 119 | named = ck_cap_lookup(tables[i], h, 0, &resource) == CK_CAP_OK; |
| 120 | int rc = ck_cap_revoke(table_id, h, tables, ntables); |
| 121 | if (rc || !named) |
| 122 | return rc; /* refused or already revoked: nothing is unmapped here */ |
| 123 | uint32_t idx = resource - CK_MMIO_RESOURCE_BASE; |
| 124 | if (resource < CK_MMIO_RESOURCE_BASE || idx >= CK_MMIO_WINDOWS || !r->w[idx].used || |
| 125 | r->w[idx].resource != resource || !r->w[idx].owned || !r->w[idx].mapped) |
| 126 | return CK_CAP_OK; |
| 127 | for (unsigned i = 0; i < ntables; i++) |
| 128 | for (unsigned s = 0; s < tables[i]->n; s++) |
| 129 | if (tables[i]->slots[s].live && !tables[i]->slots[s].tombstone && tables[i]->slots[s].resource == resource) |
| 130 | return CK_CAP_OK; /* a parent or sibling still holds the window */ |
| 131 | struct ck_mmio_window *w = &r->w[idx]; |
| 132 | w->base = 0; /* the accessor has no pointer left to use, whatever the unmap does */ |
| 133 | if (ck_mmio_unmap_exclusive(w->bus_addr, (size_t)w->size)) |
| 134 | return CK_MMIO_E_UNMAP; |
| 135 | w->mapped = 0; |
| 136 | return CK_CAP_OK; |
| 137 | } |
| 138 | |
| 139 | int ck_mmio_read(const struct ck_mmio_registry *r, const struct ck_cap_table *t, struct ck_handle h, |
| 140 | uint64_t offset, unsigned width, uint64_t *value) |
| 141 | { |
| 142 | if (!r || !t || !value) |
| 143 | return CK_MMIO_E_ARG; |
| 144 | uint32_t resource; |
| 145 | int rc = ck_cap_lookup(t, h, CK_R_READ, &resource); |
| 146 | if (rc) |
| 147 | return rc; /* revoked, stale, unknown or no READ: refused before the window is looked at */ |
| 148 | uint32_t idx = resource - CK_MMIO_RESOURCE_BASE; |
| 149 | if (resource < CK_MMIO_RESOURCE_BASE || idx >= CK_MMIO_WINDOWS || !r->w[idx].used || |
| 150 | r->w[idx].resource != resource) |
| 151 | return CK_MMIO_E_RESOURCE; |
| 152 | const struct ck_mmio_window *w = &r->w[idx]; |
| 153 | if (!w->base) |
| 154 | return CK_CAP_INVALID; /* owned pages were given back */ |
| 155 | if (width != 1 && width != 2 && width != 4 && width != 8) |
| 156 | return CK_MMIO_E_ALIGN; |
| 157 | if (offset & (width - 1u)) |
| 158 | return CK_MMIO_E_ALIGN; |
| 159 | if (offset >= w->size || width > w->size - offset) |
| 160 | return CK_MMIO_E_RANGE; |
| 161 | const volatile uint8_t *p = w->base + offset; |
| 162 | switch (width) { |
| 163 | case 1: *value = *p; break; |
| 164 | case 2: *value = *(const volatile uint16_t *)p; break; |
| 165 | case 4: *value = *(const volatile uint32_t *)p; break; |
| 166 | default: *value = *(const volatile uint64_t *)p; break; |
| 167 | } |
| 168 | return CK_MMIO_OK; |
| 169 | } |
| 170 | |
| 171 | int ck_mmio_stage_report(void) |
| 172 | { |
| 173 | uint32_t n = 0; |
| 174 | const pci_found *found = pci_stage_disc_found(&n); |
| 175 | const pci_found *nv = 0; |
| 176 | for (uint32_t i = 0; i < n; i++) |
| 177 | if ((found[i].class_code >> 8) == 0x0108 && ck_mmio_bar_addr(&found[i], 0, &(uint64_t){ 0 }) == CK_MMIO_OK) { |
| 178 | nv = &found[i]; |
| 179 | break; |
| 180 | } |
| 181 | if (!nv) { |
| 182 | ck_printf("mmio_win: no discovered NVMe function with a firmware-assigned BAR0 (report-only)\n"); |
| 183 | return CK_MMIO_E_BAR; |
| 184 | } |
| 185 | if (!(nv->command & 0x2u)) { /* reading a BAR with memory decode off can abort on real hardware */ |
| 186 | ck_printf("mmio_win: %04x:%02x:%02x.%u memory decode off, not read (report-only)\n", nv->segment, nv->bus, nv->dev, |
| 187 | nv->fn); |
| 188 | return CK_MMIO_E_BAR; |
| 189 | } |
| 190 | uint64_t addr = 0; |
| 191 | (void)ck_mmio_bar_addr(nv, 0, &addr); |
| 192 | struct ck_mmio_registry reg; |
| 193 | struct ck_cap_table tab; |
| 194 | struct ck_cap_table *const tabs[1] = { &tab }; |
| 195 | struct ck_handle h; |
| 196 | ck_mmio_registry_init(®); |
| 197 | ck_cap_init(&tab, 1, CK_CAP_SLOTS); |
| 198 | /* 0x1000 covers the NVMe register file head (CAP, VS); the real BAR size is unknown here. The window maps |
| 199 | * the page exclusively: if the page is already mapped it is refused and nothing is touched. */ |
| 200 | int crc = ck_mmio_window_open(®, &tab, nv, 0, 0x1000, CK_MMIO_RIGHTS, &h); |
| 201 | if (crc == CK_MMIO_E_MAP) { |
| 202 | ck_printf("mmio_win: %04x:%02x:%02x.%u BAR0 map refused (report-only)\n", nv->segment, nv->bus, nv->dev, nv->fn); |
| 203 | return CK_MMIO_E_BAR; |
| 204 | } |
| 205 | uint64_t vs = 0, again = 0; |
| 206 | int rrc = crc ? crc : ck_mmio_read(®, &tab, h, 0x08, 4, &vs); /* NVMe VS register */ |
| 207 | int wrc = crc ? crc : ck_mmio_read(®, &tab, h, 0x1000, 4, &again); /* one past the window */ |
| 208 | int shrc = crc ? crc : ck_mmio_try_map(addr, 0x1000) == 0; /* 1: a second (shared) map of the live window is refused */ |
| 209 | int vrc = crc ? crc : ck_mmio_revoke(®, tab.id, h, tabs, 1); |
| 210 | int arc = crc ? crc : ck_mmio_read(®, &tab, h, 0x08, 4, &again); |
| 211 | ck_printf("mmio_win: %04x:%02x:%02x.%u bar0 addr=0x%llx size=0x1000 create=%d read(VS)=%d value=0x%llx " |
| 212 | "past-end=%d shared-map-refused=%d revoke=%d after-revoke=%d unmapped=%d (read-only, report-only)\n", |
| 213 | nv->segment, nv->bus, nv->dev, nv->fn, (unsigned long long)addr, crc, rrc, (unsigned long long)vs, wrc, shrc, vrc, arc, crc ? -1 : !ck_mmio_is_mapped(addr, 0x1000)); |
| 214 | return CK_MMIO_OK; |
| 215 | } |