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 / frames.c

198 lines · 6104 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/* frames.c -- see frames.h. */
18#include "frames.h"
19
20static uint64_t up(uint64_t v, uint64_t a) { return (v + a - 1) & ~(a - 1); }
21static uint64_t down(uint64_t v, uint64_t a) { return v & ~(a - 1); }
22
23void ck_frames_init(struct ck_frames *f)
24{
25 f->n = 0;
26 f->dropped = 0;
27}
28
29static int insert_at(struct ck_frames *f, unsigned i, uint64_t base, uint64_t end)
30{
31 if (f->n >= CK_FRAMES_MAX) {
32 f->dropped++;
33 return -1;
34 }
35 for (unsigned j = f->n; j > i; j--)
36 f->r[j] = f->r[j - 1];
37 f->r[i].base = base;
38 f->r[i].end = end;
39 f->n++;
40 return 0;
41}
42
43static void remove_at(struct ck_frames *f, unsigned i)
44{
45 for (unsigned j = i; j + 1 < f->n; j++)
46 f->r[j] = f->r[j + 1];
47 f->n--;
48}
49
50int ck_frames_add(struct ck_frames *f, uint64_t base, uint64_t end)
51{
52 base = up(base, CK_PAGE);
53 end = down(end, CK_PAGE);
54 if (end <= base)
55 return 0;
56 /* Drop any part already free (overlaps are a caller bug; stay sane). */
57 ck_frames_reserve(f, base, end);
58 unsigned i = 0;
59 while (i < f->n && f->r[i].base < base)
60 i++;
61 /* Merge with predecessor / successor when they touch. */
62 if (i > 0 && f->r[i - 1].end == base) {
63 f->r[i - 1].end = end;
64 if (i < f->n && f->r[i].base == end) {
65 f->r[i - 1].end = f->r[i].end;
66 remove_at(f, i);
67 }
68 return 0;
69 }
70 if (i < f->n && f->r[i].base == end) {
71 f->r[i].base = base;
72 return 0;
73 }
74 return insert_at(f, i, base, end);
75}
76
77void ck_frames_reserve(struct ck_frames *f, uint64_t base, uint64_t end)
78{
79 base = down(base, CK_PAGE);
80 end = up(end, CK_PAGE);
81 if (end <= base)
82 return;
83 for (unsigned i = 0; i < f->n;) {
84 struct ck_frange *r = &f->r[i];
85 if (r->end <= base || r->base >= end) {
86 i++;
87 continue;
88 }
89 if (r->base >= base && r->end <= end) {
90 remove_at(f, i);
91 continue;
92 }
93 if (r->base < base && r->end > end) {
94 uint64_t tail = r->end;
95 r->end = base;
96 if (insert_at(f, i + 1, end, tail))
97 return; /* table full: the tail is lost, never handed out */
98 i += 2;
99 continue;
100 }
101 if (r->base < base)
102 r->end = base;
103 else
104 r->base = end;
105 i++;
106 }
107}
108
109int ck_frames_from_efi(struct ck_frames *f, const void *map, uint64_t map_size, uint64_t desc_size)
110{
111 if (!map || desc_size < sizeof(struct ck_efi_desc))
112 return -1;
113 int added = 0;
114 for (uint64_t off = 0; off + desc_size <= map_size; off += desc_size) {
115 const struct ck_efi_desc *d = (const struct ck_efi_desc *)((const uint8_t *)map + off);
116 if (d->type != CK_EFI_CONVENTIONAL || !d->pages || d->pages > (1ull << 36))
117 continue;
118 uint64_t end = d->phys + d->pages * CK_PAGE;
119 if (end < d->phys)
120 continue;
121 if (ck_frames_add(f, d->phys, end) == 0)
122 added++;
123 }
124 return added;
125}
126
127int ck_frames_alloc(struct ck_frames *f, uint64_t npages, uint64_t align, uint64_t *out)
128{
129 if (!npages || align < CK_PAGE || (align & (align - 1)))
130 return -1;
131 uint64_t size = npages * CK_PAGE;
132 for (unsigned i = 0; i < f->n; i++) {
133 struct ck_frange *r = &f->r[i];
134 uint64_t a = up(r->base, align);
135 if (a < r->base || a >= r->end || r->end - a < size)
136 continue;
137 uint64_t head_base = r->base, tail_base = a + size, tail_end = r->end;
138 if (head_base == a && tail_base == tail_end) {
139 remove_at(f, i);
140 } else if (head_base == a) {
141 r->base = tail_base;
142 } else if (tail_base == tail_end) {
143 r->end = a;
144 } else {
145 if (f->n >= CK_FRAMES_MAX)
146 continue; /* cannot split here; try a later range */
147 r->end = a;
148 insert_at(f, i + 1, tail_base, tail_end);
149 }
150 *out = a;
151 return 0;
152 }
153 return -1;
154}
155
156int ck_frames_alloc_below(struct ck_frames *f, uint64_t npages, uint64_t align, uint64_t limit, uint64_t *out)
157{
158 if (!npages || align < CK_PAGE || (align & (align - 1)) || !limit)
159 return -1;
160 uint64_t size = npages * CK_PAGE;
161 for (unsigned i = 0; i < f->n; i++) {
162 struct ck_frange *r = &f->r[i];
163 uint64_t old_end = r->end;
164 uint64_t top = old_end < limit ? old_end : limit; /* usable end of this range */
165 uint64_t a = up(r->base, align);
166 if (a < r->base || a >= top || top - a < size)
167 continue;
168 uint64_t tail_base = a + size;
169 if (r->base == a && tail_base == old_end) {
170 remove_at(f, i);
171 } else if (r->base == a) {
172 r->base = tail_base;
173 } else if (tail_base == old_end) {
174 r->end = a;
175 } else {
176 if (f->n >= CK_FRAMES_MAX)
177 continue; /* cannot split here; try a later range */
178 r->end = a;
179 insert_at(f, i + 1, tail_base, old_end);
180 }
181 *out = a;
182 return 0;
183 }
184 return -1;
185}
186
187void ck_frames_free(struct ck_frames *f, uint64_t base, uint64_t npages)
188{
189 ck_frames_add(f, base, base + npages * CK_PAGE);
190}
191
192uint64_t ck_frames_free_bytes(const struct ck_frames *f)
193{
194 uint64_t s = 0;
195 for (unsigned i = 0; i < f->n; i++)
196 s += f->r[i].end - f->r[i].base;
197 return s;
198}