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

275 lines · 14431 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_console_rx.c -- the service lifecycle's operator input on the host:
18 * SPCR -> console UART selection (the DGX Spark's exact SPCR bytes), the
19 * POLLED 16550 and PL011 receive decode (core/uart_rx.h) on a simulated
20 * register file (byte, empty, overrun, parity, framing, break, not ready,
21 * nothing answering), the line assembler (core/line_rx.c: assembly, erase,
22 * overlong and damaged lines refused whole), and cancel arriving in the
23 * middle of a multi-step run (ck_line_rx_cancel_poll, the function
24 * core/lifecycle.c calls between generation steps). */
25#include <string.h>
26#include "ck_test.h"
27#include "acpi.h"
28#include "uart_rx.h"
29#include "line_rx.h"
30
31/* /sys/firmware/acpi/tables/SPCR read on the DGX Spark, 2026-10-12 (80
32 * bytes, revision 2, ALASKA/AMI): type 0x01 at byte 36, GAS memory/32-bit/
33 * dword at 0x16A00000. */
34static const uint8_t spark_spcr[80] = {
35 0x53, 0x50, 0x43, 0x52, 0x50, 0x00, 0x00, 0x00, 0x02, 0x53, 0x41, 0x4c, 0x41, 0x53, 0x4b, 0x41,
36 0x41, 0x20, 0x4d, 0x20, 0x49, 0x20, 0x00, 0x00, 0x21, 0x02, 0x13, 0x20, 0x41, 0x4d, 0x49, 0x20,
37 0x99, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x03, 0x00, 0x00, 0xa0, 0x16,
38 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x6e, 0x03, 0x00, 0x00, 0x07, 0x00, 0x01, 0x00, 0x02, 0x00,
39 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
40};
41
42/* A 16550 with dword-spaced registers (stride 4), and one with byte registers. */
43static uint32_t u4[8];
44static uint8_t u1[8];
45#define U4 ((volatile uint8_t *)u4)
46#define U1 ((volatile uint8_t *)u1)
47/* A PL011 (UARTDR 0x00, UARTFR 0x18, UARTCR 0x30). */
48static uint32_t pl[0x40 / 4];
49#define PL ((volatile uint8_t *)pl)
50
51static enum ck_line_ev feed_s(struct ck_line_rx *l, const char *s)
52{
53 enum ck_line_ev ev = CK_LINE_NONE;
54 for (; *s; s++) ev = ck_line_rx_feed(l, *s == '#' ? CK_LINE_RX_ERR : (uint8_t)*s);
55 return ev;
56}
57
58/* Bytes arriving through the simulated 16550: each byte is placed in RBR
59 * with its LSR flags, then taken by the real decode. */
60struct wire { uint8_t lsr; uint8_t byte; };
61static const struct wire *g_wire;
62static size_t g_wire_n, g_wire_pos;
63static struct ck_uart_rx_count g_wire_count;
64static int wire_poll(void)
65{
66 if (g_wire_pos == g_wire_n) { u4[CK_16550_LSR] = 0x60; return ck_uart_rx_take(CK_UART_16550, U4, 4, &g_wire_count); }
67 u4[CK_16550_LSR] = g_wire[g_wire_pos].lsr;
68 u4[CK_16550_RBR] = g_wire[g_wire_pos].byte;
69 g_wire_pos++;
70 return ck_uart_rx_take(CK_UART_16550, U4, 4, &g_wire_count);
71}
72
73/* A multi-step run (stands in for ck_infer_prompt's generation loop): at
74 * step i the bytes script[i] become readable ('#' = a byte the UART lost),
75 * then the cancel poll runs, as lifecycle.c's cancel_poll does between steps. */
76static const char *const *g_script;
77static size_t g_pos;
78static int g_step;
79static unsigned g_polls;
80static int script_poll(void)
81{
82 g_polls++;
83 const char *s = g_script[g_step];
84 if (!s || !s[g_pos]) return CK_LINE_RX_EMPTY;
85 char c = s[g_pos++];
86 return c == '#' ? CK_LINE_RX_ERR : (uint8_t)c;
87}
88static unsigned g_refused, g_refused_invalid;
89static enum ck_lc_verb g_last_refused;
90static void refuse(enum ck_lc_verb v, const char *reason)
91{
92 (void)reason;
93 g_refused++;
94 if (v == CK_LC_V_INVALID) g_refused_invalid++;
95 g_last_refused = v;
96}
97/* Runs up to `steps` steps; returns the step at which cancel stopped it, or 0. */
98static int run(const char *const *script, int steps, struct ck_line_rx *l)
99{
100 g_script = script;
101 g_refused = g_refused_invalid = 0;
102 g_last_refused = CK_LC_V_UNKNOWN;
103 ck_line_rx_init(l);
104 for (g_step = 0; g_step < steps; g_step++) {
105 g_pos = 0;
106 if (ck_line_rx_cancel_poll(l, script_poll, refuse)) return g_step + 1;
107 }
108 return 0;
109}
110static int g_always_a(void) { g_polls++; return 'a'; }
111
112int main(void)
113{
114 int kind = -9;
115 unsigned stride = 0;
116
117 /* ---- SPCR -> console UART (the same table the transmit side uses) ---- */
118 struct ck_spcr s;
119 CHECK(ck_spcr_parse(spark_spcr, &s) == 0);
120 CHECK(s.interface_type == 0x01 && s.space == 0 && s.bit_width == 32 && s.access_size == 3 && s.base == 0x16A00000ull);
121 CHECK(ck_uart_from_spcr(s.interface_type, s.access_size, &kind, &stride) == 0 && kind == CK_UART_16550 && stride == 4);
122 CHECK(ck_uart_from_spcr(0x03, 3, &kind, &stride) == 0 && kind == CK_UART_PL011 && stride == 4);
123 CHECK(ck_uart_from_spcr(0x0e, 3, &kind, &stride) == 0 && kind == CK_UART_PL011);
124 CHECK(ck_uart_from_spcr(0x00, 1, &kind, &stride) == 0 && kind == CK_UART_16550 && stride == 1);
125 CHECK(ck_uart_from_spcr(0x12, 4, &kind, &stride) == 0 && kind == CK_UART_16550 && stride == 8);
126 kind = -9;
127 CHECK(ck_uart_from_spcr(0x05, 3, &kind, &stride) == -1 && kind == -9); /* NVIDIA 16550 variant: not claimed */
128 CHECK(ck_uart_from_spcr(0x0f, 3, &kind, &stride) == -1); /* Arm DCC */
129 CHECK(ck_uart_from_spcr(0x02, 3, &kind, &stride) == -1); /* MAX311x SPI */
130
131 /* ---- 16550 readiness (stride 4: dword registers, low byte meaningful) ---- */
132 uint32_t regs = 0;
133 u4[CK_16550_LCR] = 0x03; /* 8N1, DLAB clear */
134 u4[CK_16550_LSR] = 0x60; /* THRE + TEMT, nothing received */
135 CHECK(ck_uart_rx_ready(CK_UART_16550, U4, 4, &regs) == 1 && regs == 0x0360);
136 u4[CK_16550_LCR] = 0x83; /* DLAB set: offset 0 is the divisor latch */
137 CHECK(ck_uart_rx_ready(CK_UART_16550, U4, 4, &regs) == 0 && regs == 0x8360);
138 u4[CK_16550_LCR] = 0x03;
139 u4[CK_16550_LSR] = 0xffffffffu; /* nothing answering at the address */
140 CHECK(ck_uart_rx_ready(CK_UART_16550, U4, 4, &regs) == 0);
141 CHECK(ck_uart_rx_ready(CK_UART_16550, 0, 4, &regs) == 0);
142 CHECK(ck_uart_rx_ready(CK_UART_NONE, U4, 4, &regs) == 0);
143
144 /* ---- 16550 receive decode ---- */
145 struct ck_uart_rx_count c = {0, 0};
146 u4[CK_16550_LSR] = 0x60;
147 CHECK(ck_uart_rx_take(CK_UART_16550, U4, 4, &c) == CK_UART_RX_EMPTY);
148 u4[CK_16550_LSR] = 0x61; u4[CK_16550_RBR] = 0xabcd0041u; /* only the low byte is data */
149 CHECK(ck_uart_rx_take(CK_UART_16550, U4, 4, &c) == 'A' && c.errors == 0);
150 u4[CK_16550_LSR] = 0x63; /* DR + OE: an earlier byte was lost */
151 CHECK(ck_uart_rx_take(CK_UART_16550, U4, 4, &c) == CK_UART_RX_ERROR && c.errors == 1 && c.overruns == 1);
152 u4[CK_16550_LSR] = 0x62; /* OE with nothing waiting */
153 CHECK(ck_uart_rx_take(CK_UART_16550, U4, 4, &c) == CK_UART_RX_ERROR && c.errors == 2 && c.overruns == 2);
154 u4[CK_16550_LSR] = 0x65; /* parity */
155 CHECK(ck_uart_rx_take(CK_UART_16550, U4, 4, &c) == CK_UART_RX_ERROR && c.errors == 3 && c.overruns == 2);
156 u4[CK_16550_LSR] = 0x69; /* framing */
157 CHECK(ck_uart_rx_take(CK_UART_16550, U4, 4, &c) == CK_UART_RX_ERROR && c.errors == 4);
158 u4[CK_16550_LSR] = 0x71; /* break */
159 CHECK(ck_uart_rx_take(CK_UART_16550, U4, 4, &c) == CK_UART_RX_ERROR && c.errors == 5 && c.overruns == 2);
160 u4[CK_16550_LSR] = 0xffffffffu; /* floating bus: never a stream of 0xff bytes */
161 CHECK(ck_uart_rx_take(CK_UART_16550, U4, 4, &c) == CK_UART_RX_EMPTY && c.errors == 5);
162 /* byte-spaced 16550 (stride 1) */
163 u1[CK_16550_LCR] = 0x03; u1[CK_16550_LSR] = 0x61; u1[CK_16550_RBR] = 'z';
164 CHECK(ck_uart_rx_ready(CK_UART_16550, U1, 1, &regs) == 1 && regs == 0x0361);
165 CHECK(ck_uart_rx_take(CK_UART_16550, U1, 1, &c) == 'z');
166 u1[CK_16550_LSR] = 0x60;
167 CHECK(ck_uart_rx_take(CK_UART_16550, U1, 1, &c) == CK_UART_RX_EMPTY);
168
169 /* ---- PL011 (QEMU virt) readiness and decode ---- */
170 pl[CK_PL011_CR / 4] = 0x301; /* UARTEN + TXE + RXE */
171 CHECK(ck_uart_rx_ready(CK_UART_PL011, PL, 4, &regs) == 1 && regs == 0x301);
172 pl[CK_PL011_CR / 4] = 0x101; /* receiver off */
173 CHECK(ck_uart_rx_ready(CK_UART_PL011, PL, 4, &regs) == 0);
174 struct ck_uart_rx_count pc = {0, 0};
175 pl[CK_PL011_FR / 4] = CK_PL011_FR_RXFE;
176 CHECK(ck_uart_rx_take(CK_UART_PL011, PL, 4, &pc) == CK_UART_RX_EMPTY);
177 pl[CK_PL011_FR / 4] = 0; pl[CK_PL011_DR / 4] = 0x41;
178 CHECK(ck_uart_rx_take(CK_UART_PL011, PL, 4, &pc) == 'A');
179 pl[CK_PL011_DR / 4] = 0x841; /* OE */
180 CHECK(ck_uart_rx_take(CK_UART_PL011, PL, 4, &pc) == CK_UART_RX_ERROR && pc.errors == 1 && pc.overruns == 1);
181 pl[CK_PL011_DR / 4] = 0x141; /* FE */
182 CHECK(ck_uart_rx_take(CK_UART_PL011, PL, 4, &pc) == CK_UART_RX_ERROR && pc.errors == 2 && pc.overruns == 1);
183 pl[CK_PL011_DR / 4] = 0x441; /* BE */
184 CHECK(ck_uart_rx_take(CK_UART_PL011, PL, 4, &pc) == CK_UART_RX_ERROR && pc.errors == 3);
185 CHECK(ck_uart_rx_take(CK_UART_NONE, PL, 4, &pc) == CK_UART_RX_EMPTY);
186
187 /* ---- line assembly ---- */
188 static struct ck_line_rx l;
189 ck_line_rx_init(&l);
190 CHECK(ck_line_rx_feed(&l, 's') == CK_LINE_ECHO && l.len == 1 && l.buf[0] == 's');
191 CHECK(feed_s(&l, "tatus\r") == CK_LINE_DONE && l.len == 6 && strcmp(l.buf, "status") == 0);
192 CHECK(ck_line_rx_feed(&l, '\n') == CK_LINE_DONE && l.len == 0); /* CR LF: one line, one empty line */
193 CHECK(feed_s(&l, "sxy") == CK_LINE_ECHO && ck_line_rx_feed(&l, 0x7f) == CK_LINE_ERASE && ck_line_rx_feed(&l, 0x08) == CK_LINE_ERASE);
194 CHECK(feed_s(&l, "tatus\n") == CK_LINE_DONE && strcmp(l.buf, "status") == 0);
195 CHECK(ck_line_rx_feed(&l, 0x7f) == CK_LINE_NONE && l.len == 0); /* erase on an empty line */
196 CHECK(ck_line_rx_feed(&l, 0x01) == CK_LINE_NONE && ck_line_rx_feed(&l, 0x1b) == CK_LINE_NONE && ck_line_rx_feed(&l, 0x80) == CK_LINE_NONE && l.dropped == 3 && l.len == 0);
197 CHECK(feed_s(&l, "help\r") == CK_LINE_DONE && strcmp(l.buf, "help") == 0);
198 {
199 char line[CK_LC_LINE_MAX + 2];
200 memset(line, 'a', sizeof line);
201 line[CK_LC_LINE_MAX] = '\r'; line[CK_LC_LINE_MAX + 1] = 0; /* exactly the limit */
202 CHECK(feed_s(&l, line) == CK_LINE_DONE && l.len == CK_LC_LINE_MAX && l.overlong == 0);
203 line[CK_LC_LINE_MAX] = 'a'; line[CK_LC_LINE_MAX + 1] = 0; /* limit + 1 */
204 CHECK(feed_s(&l, line) == CK_LINE_NONE && l.overflow == 1);
205 CHECK(ck_line_rx_feed(&l, 0x7f) == CK_LINE_NONE); /* erasing cannot rescue it */
206 CHECK(ck_line_rx_feed(&l, '\r') == CK_LINE_OVERLONG && l.overlong == 1 && l.len == 0);
207 CHECK(feed_s(&l, "status\r") == CK_LINE_DONE && strcmp(l.buf, "status") == 0); /* next line is clean */
208 }
209 /* damaged: a lost byte anywhere refuses the whole line */
210 CHECK(feed_s(&l, "shut#down\r") == CK_LINE_DAMAGED && l.damaged_lines == 1 && l.len == 0);
211 CHECK(feed_s(&l, "status\r") == CK_LINE_DONE && strcmp(l.buf, "status") == 0);
212 /* the lost byte was the Enter: the next line is joined and refused with it */
213 CHECK(feed_s(&l, "statu#shutdown\r") == CK_LINE_DAMAGED && l.damaged_lines == 2);
214 CHECK(feed_s(&l, "#\r") == CK_LINE_DAMAGED && l.damaged_lines == 3);
215
216 /* ---- the same through the simulated 16550: an overrun mid-line ---- */
217 {
218 static const struct wire w[] = {
219 { 0x61, 's' }, { 0x61, 't' }, { 0x63, 'o' } /* OE: a byte lost before 'o' */, { 0x61, 'p' }, { 0x61, '\r' },
220 { 0x61, 'h' }, { 0x61, 'e' }, { 0x61, 'l' }, { 0x61, 'p' }, { 0x61, '\r' },
221 };
222 g_wire = w; g_wire_n = sizeof w / sizeof w[0]; g_wire_pos = 0;
223 g_wire_count.errors = g_wire_count.overruns = 0;
224 ck_line_rx_init(&l);
225 enum ck_line_ev ev[16];
226 int n = 0, b;
227 while ((b = wire_poll()) != CK_UART_RX_EMPTY && n < 16) ev[n++] = ck_line_rx_feed(&l, b);
228 CHECK(n == 10 && ev[4] == CK_LINE_DAMAGED && ev[9] == CK_LINE_DONE && strcmp(l.buf, "help") == 0);
229 CHECK(g_wire_count.errors == 1 && g_wire_count.overruns == 1 && l.damaged_lines == 1);
230 }
231
232 /* ---- cancel during a multi-step run ---- */
233 static struct ck_line_rx g;
234 {
235 /* "cancel" split over steps 3 and 5: the run stops at step 5, not earlier, not later */
236 static const char *const sc[9] = { "", "", "can", "", "cel\r", "", "", "", 0 };
237 CHECK(run(sc, 8, &g) == 5 && g_refused == 0);
238 }
239 {
240 /* another verb during the run is refused, never run, and the run goes on */
241 static const char *const sc[9] = { "sta", "tus\r", "", "shutdown\r", "", "", "", "", 0 };
242 CHECK(run(sc, 8, &g) == 0 && g_refused == 2 && g_last_refused == CK_LC_V_SHUTDOWN && g_refused_invalid == 0);
243 }
244 {
245 /* a damaged "cancel" is refused (type it again); the retyped one cancels */
246 static const char *const sc[9] = { "can#cel\r", "", "cancel\r", "", "", "", "", "", 0 };
247 CHECK(run(sc, 8, &g) == 3 && g_refused == 1 && g_refused_invalid == 1 && g.damaged_lines == 1);
248 }
249 {
250 /* an overlong line that starts with "cancel" is refused whole, not truncated into a cancel */
251 static char longc[CK_LC_LINE_MAX + 16];
252 memset(longc, 'x', sizeof longc);
253 memcpy(longc, "cancel ", 7);
254 longc[CK_LC_LINE_MAX + 14] = '\r'; longc[CK_LC_LINE_MAX + 15] = 0;
255 static const char *sc[9] = { 0, "", "", "", "", "", "", "", 0 };
256 sc[0] = longc;
257 CHECK(run(sc, 8, &g) == 0 && g_refused == 1 && g_refused_invalid == 1 && g.overlong == 1);
258 }
259 {
260 /* empty lines and a CR LF pair are ignored; " cancel " with spaces cancels at once */
261 static const char *const sc[9] = { "\r\n\r\n", " cancel \r\n", "", "", "", "", "", "", 0 };
262 CHECK(run(sc, 8, &g) == 2 && g_refused == 0);
263 }
264 {
265 /* nothing typed: every step runs */
266 static const char *const sc[9] = { "", "", "", "", "", "", "", "", 0 };
267 CHECK(run(sc, 8, &g) == 0 && g_refused == 0);
268 }
269 /* bounded: a UART that never runs dry cannot hold one poll call */
270 ck_line_rx_init(&g);
271 g_polls = 0;
272 CHECK(ck_line_rx_cancel_poll(&g, g_always_a, refuse) == 0 && g_polls == CK_LINE_RX_BURST && g.overflow == 1);
273
274 return ck_t_verdict("CK_CONSOLE_RX");
275}