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 / mm / pt.c

194 lines · 6517 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/* pt.c -- see pt.h. Descriptors: table/page = 0b11, block = 0b01. */
18#include "pt.h"
19
20#define DESC_VALID 1ull
21#define DESC_TABLE 2ull /* table at levels 0-2, page at level 3 */
22#define ATTR_MASK (~CK_PTE_ADDR & ~3ull)
23
24static uint64_t *tbl(uint64_t phys) { return (uint64_t *)(uintptr_t)phys; }
25static unsigned shift_of(int level) { return 39u - 9u * (unsigned)level; }
26
27static int new_table(struct ck_pt *pt, uint64_t *phys)
28{
29 if (pt->alloc(pt->ctx, phys) || (*phys & 0xfff) || (*phys & ~CK_PTE_ADDR))
30 return CK_PT_ENOMEM;
31 uint64_t *t = tbl(*phys);
32 for (int i = 0; i < 512; i++)
33 t[i] = 0;
34 pt->tables++;
35 return 0;
36}
37
38int ck_pt_init(struct ck_pt *pt, int (*alloc)(void *, uint64_t *), void *ctx)
39{
40 pt->alloc = alloc;
41 pt->ctx = ctx;
42 pt->live = 0;
43 pt->tables = 0;
44 return new_table(pt, &pt->root);
45}
46
47static int is_table(uint64_t e, int level) { return level < 3 && (e & 3) == 3; }
48
49/* Replace a block at `level` with a next-level table mapping the same range
50 * with the same attributes. */
51static int split(struct ck_pt *pt, uint64_t *slot, int level)
52{
53 uint64_t e = *slot, t;
54 if (pt->live)
55 return CK_PT_ELIVE;
56 int r = new_table(pt, &t);
57 if (r)
58 return r;
59 int child = level + 1;
60 uint64_t csize = 1ull << shift_of(child);
61 uint64_t base = e & CK_PTE_ADDR & ~((1ull << shift_of(level)) - 1);
62 uint64_t attrs = e & ATTR_MASK;
63 uint64_t kind = child == 3 ? 3ull : 1ull;
64 for (int i = 0; i < 512; i++)
65 tbl(t)[i] = (base + (uint64_t)i * csize) | attrs | kind;
66 *slot = t | 3ull;
67 return 0;
68}
69
70/* Does the valid leaf `e` at `level` already map va..va+n -> pa.. with attrs? */
71static int leaf_matches(uint64_t e, int level, uint64_t va, uint64_t pa, uint64_t attrs)
72{
73 uint64_t size = 1ull << shift_of(level);
74 uint64_t base = e & CK_PTE_ADDR & ~(size - 1);
75 return (e & ATTR_MASK) == attrs && base + (va & (size - 1)) == pa;
76}
77
78static int map_level(struct ck_pt *pt, uint64_t table, int level, uint64_t va, uint64_t pa,
79 uint64_t len, uint64_t attrs)
80{
81 unsigned sh = shift_of(level);
82 uint64_t size = 1ull << sh;
83 while (len) {
84 uint64_t *slot = &tbl(table)[(va >> sh) & 511];
85 uint64_t chunk_end = (va & ~(size - 1)) + size;
86 uint64_t n = chunk_end - va < len ? chunk_end - va : len;
87 uint64_t e = *slot;
88 if (level == 3) {
89 uint64_t d = pa | attrs | 3ull;
90 if (pt->live && (e & DESC_VALID) && e != d)
91 return CK_PT_ELIVE;
92 *slot = d;
93 } else if (level >= 1 && !(va & (size - 1)) && !(pa & (size - 1)) && n == size &&
94 !is_table(e, level)) {
95 uint64_t d = pa | attrs | 1ull;
96 if (pt->live && (e & DESC_VALID) && e != d)
97 return CK_PT_ELIVE;
98 *slot = d;
99 } else {
100 if (!(e & DESC_VALID)) {
101 uint64_t t;
102 int r = new_table(pt, &t);
103 if (r)
104 return r;
105 *slot = t | 3ull;
106 } else if (!is_table(e, level)) {
107 if (pt->live && leaf_matches(e, level, va, pa, attrs))
108 goto next; /* already mapped exactly so */
109 int r = split(pt, slot, level);
110 if (r)
111 return r;
112 }
113 int r = map_level(pt, *slot & CK_PTE_ADDR, level + 1, va, pa, n, attrs);
114 if (r)
115 return r;
116 }
117 next:
118 va += n;
119 pa += n;
120 len -= n;
121 }
122 return 0;
123}
124
125int ck_pt_map(struct ck_pt *pt, uint64_t va, uint64_t pa, uint64_t len, uint64_t attrs)
126{
127 if ((va | pa | len) & 0xfff || !len || (attrs & ~ATTR_MASK) || va + len - 1 > 0xffffffffffffull ||
128 pa + len - 1 > 0xffffffffffffull)
129 return CK_PT_EINVAL;
130 return map_level(pt, pt->root, 0, va, pa, len, attrs);
131}
132
133static int unmap_level(struct ck_pt *pt, uint64_t table, int level, uint64_t va, uint64_t len)
134{
135 unsigned sh = shift_of(level);
136 uint64_t size = 1ull << sh;
137 while (len) {
138 uint64_t *slot = &tbl(table)[(va >> sh) & 511];
139 uint64_t chunk_end = (va & ~(size - 1)) + size;
140 uint64_t n = chunk_end - va < len ? chunk_end - va : len;
141 uint64_t e = *slot;
142 if (e & DESC_VALID) {
143 if (level == 3 || (!is_table(e, level) && n == size)) {
144 *slot = 0;
145 } else {
146 if (!is_table(e, level)) {
147 int r = split(pt, slot, level);
148 if (r)
149 return r;
150 }
151 int r = unmap_level(pt, *slot & CK_PTE_ADDR, level + 1, va, n);
152 if (r)
153 return r;
154 }
155 }
156 va += n;
157 len -= n;
158 }
159 return 0;
160}
161
162int ck_pt_unmap(struct ck_pt *pt, uint64_t va, uint64_t len)
163{
164 if ((va | len) & 0xfff || !len)
165 return CK_PT_EINVAL;
166 return unmap_level(pt, pt->root, 0, va, len);
167}
168
169int ck_pt_lookup(const struct ck_pt *pt, uint64_t va, uint64_t *pa, uint64_t *attrs, int *level)
170{
171 uint64_t table = pt->root;
172 for (int l = 0; l <= 3; l++) {
173 uint64_t e = tbl(table)[(va >> shift_of(l)) & 511];
174 if (!(e & DESC_VALID))
175 return -1;
176 if (l == 0 && !is_table(e, 0))
177 return -1; /* no level-0 blocks with a 4 KiB granule */
178 if (is_table(e, l)) {
179 table = e & CK_PTE_ADDR;
180 continue;
181 }
182 if (l == 3 && (e & 3) != 3)
183 return -1; /* reserved encoding */
184 uint64_t size = 1ull << shift_of(l);
185 if (pa)
186 *pa = (e & CK_PTE_ADDR & ~(size - 1)) | (va & (size - 1));
187 if (attrs)
188 *attrs = e & ATTR_MASK;
189 if (level)
190 *level = l;
191 return 0;
192 }
193 return -1;
194}