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

188 lines · 8028 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_pt.c -- page-table builder: build real 4 KiB-granule tables in host
18 * memory, then walk them independently (not through ck_pt_lookup) and check
19 * output addresses, memory types, permissions, block sizes and guard holes. */
20#include <stdint.h>
21#include <stdlib.h>
22#include <string.h>
23#include "ck_test.h"
24#include "pt.h"
25
26static int allocs, alloc_limit = 1 << 30;
27static void *pool[4096];
28
29static int host_alloc(void *ctx, uint64_t *phys)
30{
31 (void)ctx;
32 if (allocs >= alloc_limit || allocs >= 4096)
33 return -1;
34 void *p = aligned_alloc(4096, 4096);
35 if (!p)
36 return -1;
37 memset(p, 0xee, 4096); /* the builder must clear what it uses */
38 pool[allocs++] = p;
39 *phys = (uint64_t)(uintptr_t)p;
40 return 0;
41}
42
43/* Independent walk: returns leaf descriptor and level, 0 if unmapped. */
44static uint64_t walk(uint64_t root, uint64_t va, int *level)
45{
46 uint64_t t = root;
47 for (int l = 0; l <= 3; l++) {
48 unsigned idx = (va >> (39 - 9 * l)) & 511;
49 uint64_t e = ((uint64_t *)(uintptr_t)t)[idx];
50 if (!(e & 1))
51 return 0;
52 if (l == 3) {
53 if (!(e & 2))
54 return 0; /* level-3 type must be "page" */
55 *level = 3;
56 return e;
57 }
58 if (!(e & 2)) { /* block */
59 if (l == 0)
60 return 0;
61 *level = l;
62 return e;
63 }
64 t = e & CK_PTE_ADDR;
65 }
66 return 0;
67}
68
69static uint64_t out_pa(uint64_t e, int level, uint64_t va)
70{
71 uint64_t size = level == 1 ? 1ull << 30 : level == 2 ? 1ull << 21 : 1ull << 12;
72 return (e & CK_PTE_ADDR & ~(size - 1)) | (va & (size - 1));
73}
74
75#define ATTR(e) ((e) & (CK_PTE_ATTRIDX(7) | CK_PTE_AP_RO | (1ull << 6) | CK_PTE_SH_INNER | CK_PTE_AF | CK_PTE_PXN | CK_PTE_UXN))
76
77static void expect(uint64_t root, uint64_t va, uint64_t pa, uint64_t attrs, int level)
78{
79 int l = -1;
80 uint64_t e = walk(root, va, &l);
81 CHECK(e != 0);
82 if (!e)
83 return;
84 CHECK(out_pa(e, l, va) == pa);
85 CHECK(ATTR(e) == attrs);
86 if (level)
87 CHECK(l == level);
88 if (out_pa(e, l, va) != pa || ATTR(e) != attrs || (level && l != level))
89 printf(" va=0x%llx got pa=0x%llx attr=0x%llx level=%d\n", (unsigned long long)va,
90 (unsigned long long)out_pa(e, l, va), (unsigned long long)ATTR(e), l);
91}
92
93static void expect_hole(uint64_t root, uint64_t va)
94{
95 int l;
96 CHECK(walk(root, va, &l) == 0);
97}
98
99int main(void)
100{
101 struct ck_pt pt;
102 CHECK(ck_pt_init(&pt, host_alloc, 0) == 0);
103 uint64_t G = 1ull << 30, M2 = 2ull << 20, K4 = 4096;
104
105 /* 3 GiB of RAM at 1 GiB: 1 GiB blocks. */
106 CHECK(ck_pt_map(&pt, G, G, 3 * G, CK_PT_NORMAL_RW) == 0);
107 expect(pt.root, G + 0x1234, G + 0x1234, CK_PT_NORMAL_RW, 1);
108 expect(pt.root, 4 * G - 8, 4 * G - 8, CK_PT_NORMAL_RW, 1);
109 expect_hole(pt.root, 4 * G);
110 expect_hole(pt.root, 0x08000000);
111
112 /* Image inside the RAM: splits the 1 GiB block down to pages. */
113 uint64_t img = G + 0x200000 + 0x3000;
114 CHECK(ck_pt_map(&pt, img, img, 0x5000, CK_PT_NORMAL_RX) == 0);
115 CHECK(ck_pt_map(&pt, img + 0x5000, img + 0x5000, 0x2000, CK_PT_NORMAL_RO) == 0);
116 expect(pt.root, img, img, CK_PT_NORMAL_RX, 3);
117 expect(pt.root, img + 0x5000, img + 0x5000, CK_PT_NORMAL_RO, 3);
118 expect(pt.root, img - K4, img - K4, CK_PT_NORMAL_RW, 3);
119 expect(pt.root, img + 0x7000, img + 0x7000, CK_PT_NORMAL_RW, 3);
120 expect(pt.root, G + 0x400000, G + 0x400000, CK_PT_NORMAL_RW, 2); /* neighbour 2 MiB block */
121 expect(pt.root, 2 * G, 2 * G, CK_PT_NORMAL_RW, 1); /* other GiB untouched */
122 /* Text is executable at EL1 only, everything else is never executable. */
123 { int l; uint64_t e = walk(pt.root, img, &l); CHECK(e && !(e & CK_PTE_PXN) && (e & CK_PTE_UXN) && (e & CK_PTE_AP_RO)); }
124 { int l; uint64_t e = walk(pt.root, img + 0x5000, &l); CHECK(e && (e & CK_PTE_PXN) && (e & CK_PTE_AP_RO)); }
125 { int l; uint64_t e = walk(pt.root, G, &l); CHECK(e && (e & CK_PTE_PXN) && !(e & CK_PTE_AP_RO)); }
126
127 /* Guard holes: stack guard below, heap guards on both sides. */
128 uint64_t stack = 2 * G + 0x10000;
129 CHECK(ck_pt_unmap(&pt, stack, K4) == 0);
130 expect_hole(pt.root, stack);
131 expect_hole(pt.root, stack + K4 - 1);
132 expect(pt.root, stack + K4, stack + K4, CK_PT_NORMAL_RW, 3);
133 expect(pt.root, stack - K4, stack - K4, CK_PT_NORMAL_RW, 3);
134 uint64_t heap = 3 * G, hsz = 16ull << 20;
135 CHECK(ck_pt_unmap(&pt, heap - K4, K4) == 0 && ck_pt_unmap(&pt, heap + hsz, K4) == 0);
136 expect_hole(pt.root, heap - K4);
137 expect_hole(pt.root, heap + hsz);
138 expect(pt.root, heap, heap, CK_PT_NORMAL_RW, 0);
139 expect(pt.root, heap + hsz - 8, heap + hsz - 8, CK_PT_NORMAL_RW, 0);
140 expect(pt.root, heap + M2, heap + M2, CK_PT_NORMAL_RW, 2);
141
142 /* DMA pool non-cacheable, MMIO device. */
143 uint64_t dma = G + 0x10000000;
144 CHECK(ck_pt_map(&pt, dma, dma, 8ull << 20, CK_PT_NORMAL_NC) == 0);
145 expect(pt.root, dma + 0x123456, dma + 0x123456, CK_PT_NORMAL_NC, 2);
146 CHECK(ck_pt_map(&pt, 0x09000000, 0x09000000, K4, CK_PT_DEVICE) == 0);
147 expect(pt.root, 0x09000010, 0x09000010, CK_PT_DEVICE, 3);
148 expect_hole(pt.root, 0x09001000);
149
150 /* Live tables: new mappings into empty space are fine, identical remaps
151 * are accepted, anything needing break-before-make is refused. */
152 pt.live = 1;
153 CHECK(ck_pt_map(&pt, 0x08000000, 0x08000000, 0x10000, CK_PT_DEVICE) == 0);
154 expect(pt.root, 0x0800fff8, 0x0800fff8, CK_PT_DEVICE, 3);
155 CHECK(ck_pt_map(&pt, 0x09000000, 0x09000000, K4, CK_PT_DEVICE) == 0);
156 CHECK(ck_pt_map(&pt, 0x09000000, 0x09000000, K4, CK_PT_NORMAL_RW) == CK_PT_ELIVE);
157 CHECK(ck_pt_map(&pt, 2 * G + 0x400000, 2 * G + 0x400000, K4, CK_PT_DEVICE) == CK_PT_ELIVE);
158 CHECK(ck_pt_unmap(&pt, 2 * G + 0x400000, K4) == CK_PT_ELIVE);
159 expect(pt.root, 2 * G + 0x400000, 2 * G + 0x400000, CK_PT_NORMAL_RW, 2); /* block split by the stack guard */
160 /* Live unmap of one level-3 page (MMIO window revoke): the page becomes a
161 * hole, neighbours are untouched, and the hole can be mapped again (the
162 * live rule only fills invalid entries). A block would need a split. */
163 CHECK(ck_pt_unmap(&pt, 0x08004000, K4) == 0);
164 expect_hole(pt.root, 0x08004000);
165 expect_hole(pt.root, 0x08004ff8);
166 expect(pt.root, 0x08003000, 0x08003000, CK_PT_DEVICE, 3);
167 expect(pt.root, 0x08005000, 0x08005000, CK_PT_DEVICE, 3);
168 CHECK(ck_pt_unmap(&pt, 0x08004000, K4) == 0); /* unmapping a hole is harmless */
169 CHECK(ck_pt_map(&pt, 0x08004000, 0x08004000, K4, CK_PT_DEVICE) == 0);
170 expect(pt.root, 0x08004000, 0x08004000, CK_PT_DEVICE, 3);
171 pt.live = 0;
172
173 /* ck_pt_lookup agrees with the independent walk. */
174 uint64_t pa, at;
175 int lv;
176 CHECK(ck_pt_lookup(&pt, img + 0x10, &pa, &at, &lv) == 0 && pa == img + 0x10 && lv == 3);
177 CHECK(ck_pt_lookup(&pt, stack, &pa, &at, &lv) == -1);
178
179 /* Bad arguments and table exhaustion. */
180 CHECK(ck_pt_map(&pt, 0x1001, 0x1001, K4, CK_PT_DEVICE) == CK_PT_EINVAL);
181 CHECK(ck_pt_map(&pt, 1ull << 48, 0, K4, CK_PT_DEVICE) == CK_PT_EINVAL);
182 alloc_limit = allocs;
183 CHECK(ck_pt_map(&pt, 0x200000000000ull, 0x10000000, K4, CK_PT_DEVICE) == CK_PT_ENOMEM);
184 CHECK(pt.tables == (unsigned)allocs);
185 for (int i = 0; i < allocs; i++)
186 free(pool[i]);
187 return ck_t_verdict("CK_PT");
188}