interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / dev / mmio_window.h

142 lines · 7917 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/* mmio_window.h -- AIENOS C kernel: capability-bound, READ-ONLY MMIO window
18 * for one discovered PCI function (aienos#286, cut B3).
19 *
20 * Freestanding. Reuses the existing capability code (core/ipc.h: rights,
21 * attenuating derive, generation-checked handles, cascading revoke); there is
22 * no second authority system here. A window is a kernel resource: one memory
23 * BAR of one pci_found, as firmware left it (pci_discover never sizes BARs and
24 * never writes). The capability is the only way to reach it:
25 *
26 * - rights are READ, DERIVE, REVOKE only; WRITE, MAP and GRANT are refused
27 * at creation, and derive cannot add a right the parent lacks;
28 * - there is NO write accessor for this object;
29 * - ck_mmio_read validates the handle (live, current generation, READ right,
30 * this window's resource) and then offset/width, all BEFORE the device is
31 * touched. After revoke every read through the old handle or any derived
32 * child is refused with CK_CAP_INVALID.
33 *
34 * Resource numbers: the registry never frees or reuses a window, so a number
35 * is never given to a second resource (aienos#285 / ADR 0019 trigger not hit).
36 *
37 * Pages (cut B3b): a window made with ck_mmio_window_open OWNS its pages. It
38 * maps them with ck_mmio_map_exclusive, which refuses (fail closed) when any
39 * page is already mapped, so a mapping some other driver holds (nvme_bind maps
40 * the same NVMe BAR0) can never be unmapped by a window. ck_mmio_revoke, once
41 * no live capability for the window is left, unmaps the pages and invalidates
42 * the TLB, so a raw access to the old pointer faults at the MMU; the accessor
43 * refusal stays as a second layer. Refuse-not-refcount was chosen on purpose:
44 * a shared page would make revoke depend on a stranger. Windows made with
45 * ck_mmio_window_create (borrowed mapping) still revoke the capability only.
46 * Exclusivity holds in both directions: while a window is live, the kernel's
47 * ck_mm_mmio_try_map refuses (NULL) and ck_mmio_map panics for any range that
48 * overlaps its pages, so a driver cannot share them later either.
49 *
50 * RULE: window capabilities must be revoked through ck_mmio_revoke. A bare
51 * ck_cap_revoke removes the capability and the accessor refuses, but the
52 * window is never told, so its pages stay mapped (tested in stage_test.c).
53 * That is the remaining exposure; there is no lazy cleanup, because a revoked
54 * handle no longer reaches the window.
55 *
56 * Limits (named): BAR size is not discoverable here, so the caller supplies
57 * it from firmware or a fixture; the mapping is Device RW even though the
58 * accessor is read-only (no read-only Device mapping is made); revoke must go
59 * through ck_mmio_revoke (see RULE above); the exclusive-range table holds 8
60 * ranges and a ninth window is refused with CK_MMIO_E_MAP; no interrupts, no DMA, no polling helper, no
61 * config-space or COMMAND changes. */
62#ifndef AIENOS_CK_MMIO_WINDOW_H
63#define AIENOS_CK_MMIO_WINDOW_H
64#include <stdint.h>
65#include "../core/ipc.h"
66#include "pci.h"
67
68#define CK_MMIO_RESOURCE_BASE 0x4d4d0000u /* "MM" */
69#define CK_MMIO_WINDOWS 4u
70/* The only rights a window capability may hold. */
71#define CK_MMIO_RIGHTS (CK_R_READ | CK_R_DERIVE | CK_R_REVOKE)
72
73enum {
74 CK_MMIO_OK = 0,
75 CK_MMIO_E_ARG = -501, /* null argument, zero size, bad BAR index */
76 CK_MMIO_E_RIGHTS = -502, /* creation asked for a right outside CK_MMIO_RIGHTS, or no READ */
77 CK_MMIO_E_BAR = -503, /* I/O BAR, reserved type, 64-bit pair out of range, or address 0 */
78 CK_MMIO_E_RANGE = -504, /* window wraps u64, or access outside the window */
79 CK_MMIO_E_ALIGN = -505, /* width not 1/2/4/8, or offset not width-aligned */
80 CK_MMIO_E_FULL = -506, /* registry full (windows are never reused) */
81 CK_MMIO_E_RESOURCE = -507, /* handle is live but names no MMIO window */
82 CK_MMIO_E_MAP = -508, /* exclusive map refused: a page is already mapped, overlaps RAM, or cannot be mapped */
83 CK_MMIO_E_UNMAP = -509, /* capability revoked but the page unmap failed */
84};
85
86struct ck_mmio_window {
87 uint8_t used;
88 uint32_t resource;
89 uint64_t bus_addr, size;
90 uint8_t owned; /* 1: window mapped these pages itself and unmaps them on revoke */
91 uint8_t mapped; /* owned pages still mapped */
92 const volatile uint8_t *base; /* caller's mapping of [bus_addr, +size) */
93};
94struct ck_mmio_registry {
95 struct ck_mmio_window w[CK_MMIO_WINDOWS];
96 uint32_t next; /* next unused index; never decreases */
97};
98
99void ck_mmio_registry_init(struct ck_mmio_registry *r);
100
101/* Address of memory BAR `bar` of `f` from bar_raw (type bits masked; a 64-bit
102 * BAR joins bar_raw[bar + 1]). CK_MMIO_E_BAR for I/O, reserved types, a
103 * 64-bit pair past the last register, or an address of 0. */
104int ck_mmio_bar_addr(const pci_found *f, unsigned bar, uint64_t *addr);
105
106/* Create the window and put its capability into `t`. `base` is the caller's
107 * mapping of the BAR (ck_mmio_try_map in the kernel, a fake on the host). */
108int ck_mmio_window_create(struct ck_mmio_registry *r, struct ck_cap_table *t, const pci_found *f, unsigned bar,
109 uint64_t size, const volatile void *base, unsigned rights, struct ck_handle *out);
110
111/* Like ck_mmio_window_create, but the window OWNS its pages: it maps the BAR
112 * itself with ck_mmio_map_exclusive (CK_MMIO_E_MAP, nothing created, if any
113 * page is already mapped by someone else) and ck_mmio_revoke takes the pages
114 * away again. */
115int ck_mmio_window_open(struct ck_mmio_registry *r, struct ck_cap_table *t, const pci_found *f, unsigned bar,
116 uint64_t size, unsigned rights, struct ck_handle *out);
117
118/* The revoke path for window capabilities: ck_cap_revoke(table_id, h, tables,
119 * ntables), and when that leaves no live capability for the window's resource
120 * in `tables` the owned pages are unmapped (MMU level; a raw access then
121 * faults) and the TLB is invalidated. Revoking a derived child while its
122 * parent is still live leaves the pages mapped. Returns the ck_cap code;
123 * CK_MMIO_E_UNMAP if the capability is gone but the unmap failed (the pages
124 * stay mapped and the accessor still refuses). `tables` must list every table
125 * that can hold a capability for the window. Borrowed windows (created with
126 * ck_mmio_window_create) revoke the capability only. Calling it again on a
127 * revoked handle is harmless (CK_CAP_INVALID, nothing touched). */
128int ck_mmio_revoke(struct ck_mmio_registry *r, uint32_t table_id, struct ck_handle h,
129 struct ck_cap_table *const *tables, unsigned ntables);
130
131/* Checked read of `width` (1, 2, 4 or 8) bytes at `offset`. Capability
132 * failures return the ck_cap code (CK_CAP_INVALID: revoked, stale or unknown
133 * handle; CK_CAP_MISSING_RIGHTS). *value is written only on CK_MMIO_OK. */
134int ck_mmio_read(const struct ck_mmio_registry *r, const struct ck_cap_table *t, struct ck_handle h,
135 uint64_t offset, unsigned width, uint64_t *value);
136
137/* Stage glue: report-only. Binds a window to the first discovered NVMe
138 * function's BAR0 (size from the NVMe register file minimum, 0x1000), reads
139 * VS through the capability, revokes, proves the next read is refused. */
140int ck_mmio_stage_report(void);
141
142#endif