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 / tests / test_dma_window.c

257 lines · 10296 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/* test_dma_window.c -- Pi 5 software-only DMA confinement (dev/dma_window.c,
18 * ADR 0020). Hosted: this proves the rule's logic, NOT the hardware. Run
19 * under ASan/UBSan and with each CK_DMAW_MUTANT_* (make dma-window-mutants),
20 * where every mutant must make this test FAIL. */
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include "ck_test.h"
25#include "dma_window.h"
26
27#define WSIZE 65536u
28#define WBASE 0x40000000ull
29
30static uint8_t *mem;
31
32static void fresh(struct ck_dmaw *w)
33{
34 memset(mem, 0xAA, WSIZE);
35 CHECK(ck_dmaw_init(w, mem, WBASE, WSIZE) == CK_DMAW_OK);
36}
37
38static void t_init(void)
39{
40 struct ck_dmaw w;
41 memset(mem, 0xAA, WSIZE);
42 CHECK(ck_dmaw_init(&w, mem, WBASE, WSIZE) == CK_DMAW_OK);
43 int zero = 1;
44 for (uint32_t i = 0; i < WSIZE; i++) zero &= mem[i] == 0;
45 CHECK(zero);
46 CHECK(!ck_dmaw_halted(&w));
47 CHECK(ck_dmaw_init(&w, mem, WBASE, WSIZE + 4096) == CK_DMAW_E_PARAM); /* not pow2 */
48 CHECK(ck_dmaw_halted(&w)); /* failed init stays halted */
49 CHECK(ck_dmaw_init(&w, mem, WBASE + 4096, WSIZE) == CK_DMAW_E_PARAM); /* misaligned base */
50 CHECK(ck_dmaw_init(&w, mem, WBASE, CK_DMAW_MAX_BYTES * 2) == CK_DMAW_E_PARAM);
51 CHECK(ck_dmaw_init(&w, mem, WBASE, 2048) == CK_DMAW_E_PARAM);
52 CHECK(ck_dmaw_init(&w, mem, 0 - (uint64_t)WSIZE, WSIZE) == CK_DMAW_E_PARAM); /* end wraps */
53 CHECK(ck_dmaw_init(&w, NULL, WBASE, WSIZE) == CK_DMAW_E_PARAM);
54 CHECK(ck_dmaw_init(&w, mem, WBASE, WSIZE) == CK_DMAW_OK);
55 CHECK(!ck_dmaw_halted(&w));
56}
57
58static void t_validate(void)
59{
60 struct ck_dmaw w;
61 fresh(&w);
62 CHECK(ck_dmaw_validate(&w, WBASE, 1) == CK_DMAW_OK);
63 CHECK(ck_dmaw_validate(&w, WBASE, WSIZE) == CK_DMAW_OK); /* whole window */
64 CHECK(ck_dmaw_validate(&w, WBASE + WSIZE - 1, 1) == CK_DMAW_OK); /* last byte */
65 CHECK(!ck_dmaw_halted(&w));
66
67 fresh(&w); /* below the window */
68 CHECK(ck_dmaw_validate(&w, WBASE - 1, 16) == CK_DMAW_E_RANGE);
69 CHECK(ck_dmaw_halted(&w));
70 CHECK(ck_dmaw_validate(&w, WBASE, 1) == CK_DMAW_E_LATCHED); /* valid call after latch refused */
71
72 fresh(&w); /* straddles the end */
73 CHECK(ck_dmaw_validate(&w, WBASE + WSIZE - 8, 16) == CK_DMAW_E_RANGE);
74 CHECK(ck_dmaw_halted(&w));
75
76 fresh(&w); /* starts at the end */
77 CHECK(ck_dmaw_validate(&w, WBASE + WSIZE, 1) == CK_DMAW_E_RANGE);
78
79 fresh(&w); /* entirely above */
80 CHECK(ck_dmaw_validate(&w, WBASE + WSIZE + 4096, 16) == CK_DMAW_E_RANGE);
81
82 fresh(&w); /* wrapped length: addr + len overflows 2^64 to a small number */
83 CHECK(ck_dmaw_validate(&w, 0 - 16ull, 64) == CK_DMAW_E_RANGE);
84 fresh(&w);
85 CHECK(ck_dmaw_validate(&w, WBASE + 16, 0 - 8ull) == CK_DMAW_E_RANGE); /* huge len wraps back inside */
86 fresh(&w);
87 CHECK(ck_dmaw_validate(&w, WBASE, UINT64_MAX) == CK_DMAW_E_RANGE);
88
89 fresh(&w); /* zero length */
90 CHECK(ck_dmaw_validate(&w, WBASE + 8, 0) == CK_DMAW_E_ZERO);
91 CHECK(ck_dmaw_halted(&w));
92
93 fresh(&w); /* misaligned */
94 CHECK(ck_dmaw_validate_aligned(&w, WBASE + 4, 16, 8) == CK_DMAW_E_ALIGN);
95 CHECK(ck_dmaw_halted(&w));
96 fresh(&w);
97 CHECK(ck_dmaw_validate_aligned(&w, WBASE + 8, 16, 8) == CK_DMAW_OK);
98 CHECK(ck_dmaw_validate_aligned(&w, WBASE + 8, 16, 3) == CK_DMAW_E_ALIGN); /* align not pow2 */
99
100 CHECK(ck_dmaw_init(&w, mem, WBASE, WSIZE) == CK_DMAW_OK); /* only init clears the latch */
101 CHECK(!ck_dmaw_halted(&w));
102}
103
104static void t_alloc(void)
105{
106 struct ck_dmaw w;
107 uint64_t a, b;
108 fresh(&w);
109 CHECK(ck_dmaw_alloc(&w, 100, 64, &a) == CK_DMAW_OK && a == WBASE);
110 CHECK(ck_dmaw_alloc(&w, 8, 64, &b) == CK_DMAW_OK && b == WBASE + 128 && ck_dmaw_validate(&w, b, 8) == CK_DMAW_OK);
111 CHECK(ck_dmaw_alloc(&w, WSIZE, 8, &a) == CK_DMAW_E_NOSPACE);
112 CHECK(!ck_dmaw_halted(&w)); /* running out of space is not a violation */
113 CHECK(ck_dmaw_alloc(&w, UINT64_MAX, 8, &a) == CK_DMAW_E_NOSPACE);
114 CHECK(ck_dmaw_alloc(&w, 8, 3, &a) == CK_DMAW_E_PARAM);
115 CHECK(ck_dmaw_alloc(&w, 0, 8, &a) == CK_DMAW_E_PARAM);
116}
117
118static void t_ring(void)
119{
120 struct ck_dmaw w;
121 struct ck_dmaw_ring r;
122 struct ck_dmaw_slot slots[4];
123 struct ck_dmaw_desc d;
124 uint32_t i, j;
125
126 fresh(&w);
127 CHECK(ck_dmaw_ring_init(&r, slots, 3) == CK_DMAW_E_PARAM);
128 CHECK(ck_dmaw_ring_init(&r, slots, 4) == CK_DMAW_OK);
129 CHECK(ck_dmaw_ring_post(&r, &w, WBASE + 64, 100, 7, &i) == CK_DMAW_OK && i == 0);
130 CHECK(ck_dmaw_ring_emit(&r, &w, i, &d) == CK_DMAW_OK && d.addr == WBASE + 64 && d.len == 100 && d.flags == 7);
131 CHECK(ck_dmaw_ring_complete(&r, &w, i, 100) == CK_DMAW_OK);
132 CHECK(!ck_dmaw_halted(&w));
133
134 /* stale: emit and complete of a slot no longer in flight */
135 CHECK(ck_dmaw_ring_emit(&r, &w, i, &d) == CK_DMAW_E_STALE);
136 CHECK(ck_dmaw_halted(&w));
137 fresh(&w);
138 ck_dmaw_ring_init(&r, slots, 4);
139 ck_dmaw_ring_post(&r, &w, WBASE, 64, 0, &i);
140 CHECK(ck_dmaw_ring_complete(&r, &w, i, 64) == CK_DMAW_OK);
141 CHECK(ck_dmaw_ring_complete(&r, &w, i, 64) == CK_DMAW_E_STALE); /* double completion */
142 CHECK(ck_dmaw_halted(&w));
143
144 /* device reports more bytes than were posted */
145 fresh(&w);
146 ck_dmaw_ring_init(&r, slots, 4);
147 ck_dmaw_ring_post(&r, &w, WBASE, 64, 0, &i);
148 CHECK(ck_dmaw_ring_complete(&r, &w, i, 65) == CK_DMAW_E_RANGE);
149 CHECK(ck_dmaw_halted(&w));
150
151 /* device-supplied index out of range */
152 fresh(&w);
153 ck_dmaw_ring_init(&r, slots, 4);
154 ck_dmaw_ring_post(&r, &w, WBASE, 64, 0, &i);
155 CHECK(ck_dmaw_ring_complete(&r, &w, 4, 1) == CK_DMAW_E_INDEX);
156 CHECK(ck_dmaw_halted(&w));
157 fresh(&w);
158 ck_dmaw_ring_init(&r, slots, 4);
159 ck_dmaw_ring_post(&r, &w, WBASE, 64, 0, &i);
160 CHECK(ck_dmaw_ring_emit(&r, &w, 99, &d) == CK_DMAW_E_INDEX);
161
162 /* post: out of window, wrapped, zero, misaligned, jumbo; each latches */
163 uint64_t bad_addr[] = {WBASE - 8, WBASE + WSIZE, WBASE + WSIZE - 8, 0 - 8ull};
164 uint32_t bad_len[] = {64, 64, 64, 64};
165 for (j = 0; j < 4; j++) {
166 fresh(&w);
167 ck_dmaw_ring_init(&r, slots, 4);
168 CHECK(ck_dmaw_ring_post(&r, &w, bad_addr[j], bad_len[j], 0, &i) == CK_DMAW_E_RANGE);
169 CHECK(ck_dmaw_halted(&w) && r.inflight == 0);
170 }
171 fresh(&w);
172 ck_dmaw_ring_init(&r, slots, 4);
173 CHECK(ck_dmaw_ring_post(&r, &w, WBASE, 0, 0, &i) == CK_DMAW_E_ZERO && ck_dmaw_halted(&w));
174 fresh(&w);
175 ck_dmaw_ring_init(&r, slots, 4);
176 CHECK(ck_dmaw_ring_post(&r, &w, WBASE + 4, 64, 0, &i) == CK_DMAW_E_ALIGN && ck_dmaw_halted(&w));
177 fresh(&w);
178 ck_dmaw_ring_init(&r, slots, 4);
179 CHECK(ck_dmaw_ring_post(&r, &w, WBASE, CK_DMAW_MAX_FRAME, 0, &i) == CK_DMAW_OK); /* max standard frame ok */
180 fresh(&w);
181 ck_dmaw_ring_init(&r, slots, 4);
182 CHECK(ck_dmaw_ring_post(&r, &w, WBASE, CK_DMAW_MAX_FRAME + 1, 0, &i) == CK_DMAW_E_BIG && ck_dmaw_halted(&w));
183
184 /* ring full is not a violation; a latched window refuses valid posts */
185 fresh(&w);
186 ck_dmaw_ring_init(&r, slots, 4);
187 for (j = 0; j < 4; j++) CHECK(ck_dmaw_ring_post(&r, &w, WBASE + 64 * j, 64, 0, &i) == CK_DMAW_OK);
188 CHECK(ck_dmaw_ring_post(&r, &w, WBASE, 64, 0, &i) == CK_DMAW_E_NOSPACE && !ck_dmaw_halted(&w));
189 ck_dmaw_validate(&w, WBASE - 1, 1);
190 CHECK(ck_dmaw_ring_post(&r, &w, WBASE, 64, 0, &i) == CK_DMAW_E_LATCHED);
191 CHECK(ck_dmaw_ring_emit(&r, &w, 0, &d) == CK_DMAW_E_LATCHED);
192
193 /* shadow, not ring memory: corrupting the shadow after post is caught at emit */
194 fresh(&w);
195 ck_dmaw_ring_init(&r, slots, 4);
196 ck_dmaw_ring_post(&r, &w, WBASE, 64, 0, &i);
197 slots[i].d.addr = WBASE + WSIZE + 8;
198 CHECK(ck_dmaw_ring_emit(&r, &w, i, &d) == CK_DMAW_E_RANGE && ck_dmaw_halted(&w));
199}
200
201/* Property test: for hostile (addr,len), validate agrees with a 128-bit
202 * reference, and every descriptor the ring emits is fully inside the window. */
203static uint64_t rs = 0x9e3779b97f4a7c15ull;
204static uint64_t rnd(void)
205{
206 rs ^= rs << 13; rs ^= rs >> 7; rs ^= rs << 17;
207 return rs;
208}
209
210static void t_property(void)
211{
212 struct ck_dmaw w;
213 struct ck_dmaw_ring r;
214 struct ck_dmaw_slot slots[8];
215 struct ck_dmaw_desc d;
216 int mismatch = 0, escaped = 0, emitted = 0;
217 for (int n = 0; n < 200000; n++) {
218 uint64_t addr, len;
219 switch (rnd() % 5) {
220 case 0: addr = rnd(); len = rnd(); break;
221 case 1: addr = WBASE + rnd() % (2 * WSIZE); len = rnd() % 4096; break;
222 case 2: addr = (WBASE - 64) + rnd() % 128; len = 1 + rnd() % 64; break;
223 case 3: addr = WBASE + WSIZE - 64 + rnd() % 128; len = rnd() % 128; break;
224 default: addr = 0 - (rnd() % 256); len = rnd() % 512; break;
225 }
226 fresh(&w);
227 int rc = ck_dmaw_validate(&w, addr, len);
228 unsigned __int128 end = (unsigned __int128)addr + len;
229 int inside = len != 0 && addr >= WBASE && end <= (unsigned __int128)WBASE + WSIZE;
230 if ((rc == CK_DMAW_OK) != inside) mismatch++;
231 ck_dmaw_ring_init(&r, slots, 8);
232 uint32_t idx;
233 if (ck_dmaw_ring_post(&r, &w, addr, (uint32_t)len, 0, &idx) == CK_DMAW_OK) {
234 if (ck_dmaw_ring_emit(&r, &w, idx, &d) == CK_DMAW_OK) {
235 emitted++;
236 unsigned __int128 e = (unsigned __int128)d.addr + d.len;
237 if (d.addr < WBASE || e > (unsigned __int128)WBASE + WSIZE || d.len == 0) escaped++;
238 }
239 }
240 }
241 CHECK(mismatch == 0);
242 CHECK(escaped == 0);
243 CHECK(emitted > 0);
244}
245
246int main(void)
247{
248 mem = malloc(WSIZE);
249 if (!mem) return 2;
250 t_init();
251 t_validate();
252 t_alloc();
253 t_ring();
254 t_property();
255 free(mem);
256 return ck_t_verdict("test_dma_window");
257}