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_frames.c

116 lines · 5551 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_frames.c -- frame allocator over a crafted UEFI memory map: only
18 * EfiConventionalMemory is handed out, reserved ranges (image, tables,
19 * handoff) never are, alignment holds, exhaustion returns -1. */
20#include <stdint.h>
21#include <string.h>
22#include "ck_test.h"
23#include "frames.h"
24
25#define DSZ 48 /* firmware descriptor size larger than the struct, as UEFI allows */
26
27static uint8_t map[DSZ * 16];
28static int nd;
29
30static void add(uint32_t type, uint64_t phys, uint64_t pages)
31{
32 struct ck_efi_desc d = { type, 0, phys, 0, pages, 0x8 };
33 memcpy(map + nd * DSZ, &d, sizeof d);
34 nd++;
35}
36
37static int inside_free(const struct ck_frames *f, uint64_t a)
38{
39 for (unsigned i = 0; i < f->n; i++)
40 if (a >= f->r[i].base && a < f->r[i].end)
41 return 1;
42 return 0;
43}
44
45int main(void)
46{
47 struct ck_frames f;
48 add(7, 0x40000000, 0x100); /* conventional 1 MiB */
49 add(1, 0x40100000, 0x10); /* loader code (the image) */
50 add(7, 0x40110000, 0x10); /* conventional, adjacent after a hole */
51 add(9, 0x40120000, 0x4); /* ACPI reclaim */
52 add(7, 0x40124000, 0x1000); /* conventional 16 MiB, merges with nothing */
53 add(4, 0x41124000, 0x2); /* boot services data */
54 add(0, 0x50000000, 0x10); /* reserved */
55 add(7, 0x60000000, 0); /* empty descriptor */
56 ck_frames_init(&f);
57 CHECK(ck_frames_from_efi(&f, map, (uint64_t)nd * DSZ, DSZ) == 3);
58 CHECK(ck_frames_from_efi(&f, map, (uint64_t)nd * DSZ, 8) == -1);
59 CHECK(ck_frames_free_bytes(&f) == (0x100ull + 0x10 + 0x1000) * 4096);
60 CHECK(!inside_free(&f, 0x40100000) && !inside_free(&f, 0x40120000));
61 CHECK(!inside_free(&f, 0x50000000) && !inside_free(&f, 0x41124000));
62
63 /* Reserve an image-like range that straddles two free ranges, unaligned. */
64 ck_frames_reserve(&f, 0x400ff800, 0x40110800);
65 CHECK(!inside_free(&f, 0x400ff000) && !inside_free(&f, 0x40110000));
66 CHECK(inside_free(&f, 0x400fe000) && inside_free(&f, 0x40111000));
67 /* Split a range in the middle. */
68 ck_frames_reserve(&f, 0x40200000, 0x40201000);
69 CHECK(!inside_free(&f, 0x40200000) && inside_free(&f, 0x401ff000) && inside_free(&f, 0x40201000));
70
71 /* Allocations never land in reserved space and respect alignment. */
72 uint64_t a;
73 CHECK(ck_frames_alloc(&f, 1, 4096, &a) == 0 && a == 0x40000000);
74 CHECK(ck_frames_alloc(&f, 512, 2ull << 20, &a) == 0 && (a & ((2ull << 20) - 1)) == 0);
75 CHECK(a >= 0x40124000 && a + 512 * 4096 <= 0x41124000);
76 CHECK(ck_frames_alloc(&f, 3, 1000, &a) == -1); /* bad alignment */
77 CHECK(ck_frames_alloc(&f, 0, 4096, &a) == -1);
78
79 /* Exhaustion: drain everything one page at a time, then fail. */
80 uint64_t left = ck_frames_free_bytes(&f) / 4096, got = 0;
81 while (ck_frames_alloc(&f, 1, 4096, &a) == 0) {
82 CHECK(a != 0x40100000 && a != 0x40120000 && !(a >= 0x400ff000 && a < 0x40111000));
83 got++;
84 }
85 CHECK(got == left && ck_frames_free_bytes(&f) == 0);
86 CHECK(ck_frames_alloc(&f, 1, 4096, &a) == -1);
87 ck_frames_free(&f, 0x40000000, 2);
88 CHECK(ck_frames_free_bytes(&f) == 2 * 4096);
89 CHECK(ck_frames_alloc(&f, 2, 4096, &a) == 0 && a == 0x40000000);
90
91 /* Below a limit (exclusive): the block ends at or under it, frames above stay free. */
92 ck_frames_init(&f);
93 ck_frames_add(&f, 0x80000000ull, 0x100100000ull); /* 2 GiB .. 4 GiB + 1 MiB */
94 CHECK(ck_frames_alloc_below(&f, 1024, 4096, 1ull << 32, &a) == 0 && a == 0x80000000ull);
95 CHECK(ck_frames_alloc_below(&f, 0x7FC00, 4096, 1ull << 32, &a) == 0 && a == 0x80400000ull);
96 CHECK(a + 0x7FC00ull * 4096 == 1ull << 32 && ck_frames_free_bytes(&f) == 0x100000ull);
97 CHECK(ck_frames_alloc_below(&f, 1, 4096, 1ull << 32, &a) == -1); /* only frames above 4 GiB left */
98 CHECK(ck_frames_free_bytes(&f) == 0x100000ull);
99 CHECK(ck_frames_alloc(&f, 1, 4096, &a) == 0 && a == (1ull << 32)); /* unlimited alloc still sees them */
100
101 /* A block that starts past the first free frame and stops at the limit splits the range. */
102 ck_frames_init(&f);
103 ck_frames_add(&f, 0x10001000ull, 0x20000000ull);
104 CHECK(ck_frames_alloc_below(&f, 1, 0x400000, 0x10800000ull, &a) == 0 && a == 0x10400000ull);
105 CHECK(!inside_free(&f, 0x10400000ull) && inside_free(&f, 0x10401000ull) && inside_free(&f, 0x10001000ull));
106 CHECK(ck_frames_free_bytes(&f) == 0x20000000ull - 0x10001000ull - 4096);
107 CHECK(ck_frames_alloc_below(&f, 1, 4096, 0x10000000ull, &a) == -1); /* limit below the free start */
108 CHECK(ck_frames_alloc_below(&f, 1, 4096, 0, &a) == -1); /* no limit given */
109
110 /* Range table overflow is counted, not silent. */
111 ck_frames_init(&f);
112 for (unsigned i = 0; i < CK_FRAMES_MAX + 3; i++)
113 ck_frames_add(&f, 0x100000000ull + i * 0x2000ull, 0x100000000ull + i * 0x2000ull + 0x1000);
114 CHECK(f.n == CK_FRAMES_MAX && f.dropped == 3);
115 return ck_t_verdict("CK_FRAMES");
116}