Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/kernel/x86/report_x86.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / x86 / report_x86.c` 308 lines ยท 12403 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 /* report_x86.c -- LAB-ONLY: sends the iMac kernel's diagnostic lines over 18 * Ethernet, so the owner does not have to photograph the screen. 19 * 20 * Built only into the opt-in lab image (make imac-kernel CK_X86_REPORT=1, 21 * which writes IMACKERN-LAB.EFI). The default image links none of this file 22 * nor the network library, and the Makefile fails a default image that has 23 * its symbols; resident and service builds must never add it. Any future 24 * service diagnostics go over the authenticated transport with an explicitly 25 * authorized peer, never this broadcast. 26 * 27 * What is sent: ck_x86_report_filter's copy of the console's RAM copy 28 * (console_x86.c, the first 64 KiB), which keeps only complete, printable, 29 * bounded lines that start with a fixed kernel diagnostic key and drops 30 * everything else whole. The raw console is never sent. The copy is cut into 31 * parts of CK_X86_REPORT_CHUNK bytes; each part is one UDP datagram from 32 * 0.0.0.0:PORT to 255.255.255.255:PORT in an Ethernet broadcast frame (no 33 * address to configure, no ARP, nothing to answer), with a one-line header the 34 * receiver checks: 35 * "AIENOS-REPORT v=2 auth=none run=<16 hex> part=/ bytes=\n" 36 * auth=none says what it is: anyone on the LAN can read or forge one, and the 37 * run id and checksum are not authenticity. A received report is never a 38 * receipt, evidence or a command. The host side is 39 * native/kernel/tools/ck_imac_report_listen.c. During the screen hold the whole 40 * report is sent again every 2 s while the link is up, so a receiver that 41 * missed a datagram gets it on the next round. 42 * 43 * ck_x86_report_filter and ck_x86_report_frame are pure functions of their 44 * arguments (no device, no clock) and are host-tested by 45 * scripts/ck_imac_report_test.sh. */ 46 #ifdef CK_X86_REPORT_HOST 47 #include 48 #else 49 #include "../core/ck_internal.h" 50 #include "x86.h" 51 #endif 52 #include "aienos_net.h" 53 #include "report_x86.h" 54 #include "console_log.h" 55 56 _Static_assert(CK_X86_REPORT_MAX >= CK_X86_LOG_BYTES, "the report must hold a full console copy"); 57 _Static_assert(CK_X86_REPORT_MAX / CK_X86_REPORT_CHUNK <= 64, "the listener rebuilds at most 64 parts"); 58 59 #define REPORT_HDR "AIENOS-REPORT v=2 auth=none " 60 61 /* The allowlist: every key the iMac kernel prints (the x86 sources, efi_gop.c, 62 * the disk report's ahci_/nvme_ _read and _gpt lines, and Allen's memory 63 * status "allen_memory:", which carries only counts and the disk layout, 64 * and "boot_disk:" / "boot_time:", only sizes, commit ids and milliseconds), 65 * minus "memory:", which describes the kernel's own memory layout, and 66 * minus what is said to Allen ("keyboard_line:" and "allen:"): the conversation never leaves the 67 * iMac; "screen: Allen answered" carries only lengths. A new key stays off the 68 * report until it is added here. */ 69 static const char *const allowed_keys[] = { 70 "acpi_rsdp", "ahci", "ahci_disk", "ahci_gpt", "ahci_part", "ahci_port", "ahci_read", "ahci_write", 71 "aienos_commit", "allen-code", "allen_memory", "apic", "bench", "boot_disk", "boot_time", "courier", "cpu", "descriptors", "dmar", "exit_boot_services", 72 "firmware_cpl", "firmware_revision", "firmware_vendor", "gop", "guard_page", "handoff", "image", "kernel", 73 "kernel_impl", "keyboard", "local_model", "local_tools", "madt", "net", "net_tg3", "note", "nvme", "nvme_disk", "nvme_gpt", "nvme_read", "paging", 74 "panic", "pci", "power", "ram", "report", "report_kind", "screen", "smp", "sound", "stack", "storage", "summary", 75 "timebase", "timer_irq", "timer_stats", "usb", "wizard", "xhci", 76 }; 77 static const char *const allowed_lines[] = { 78 "AIENOS-KERNEL-0", 79 "AIENOS-KERNEL-0 DONE", 80 "exiting firmware boot services", 81 }; 82 83 static int line_allowed(const char *s, size_t n) 84 { 85 if (n > CK_X86_REPORT_LINE_MAX) 86 return 0; 87 for (size_t i = 0; i < n; i++) 88 if ((unsigned char)s[i] < 0x20 || (unsigned char)s[i] > 0x7e) 89 return 0; 90 for (size_t k = 0; k < sizeof allowed_lines / sizeof allowed_lines[0]; k++) 91 if (strlen(allowed_lines[k]) == n && !memcmp(s, allowed_lines[k], n)) 92 return 1; 93 for (size_t k = 0; k < sizeof allowed_keys / sizeof allowed_keys[0]; k++) { 94 size_t kl = strlen(allowed_keys[k]); 95 if (n > kl && s[kl] == ':' && !memcmp(s, allowed_keys[k], kl)) 96 return 1; 97 } 98 return 0; 99 } 100 101 size_t ck_x86_report_filter(const char *log, size_t len, char *out, size_t cap) 102 { 103 size_t w = 0; 104 if (!log || !out) 105 return 0; 106 for (size_t i = 0; i < len;) { 107 size_t e = i; 108 while (e < len && log[e] != '\n') 109 e++; 110 if (e == len) /* unterminated last line: dropped */ 111 break; 112 size_t n = e - i; 113 if (line_allowed(log + i, n) && cap - w > n) { 114 memcpy(out + w, log + i, n); 115 out[w + n] = '\n'; 116 w += n + 1; 117 } 118 i = e + 1; 119 } 120 return w; 121 } 122 static void hex16(char *out, uint64_t v) 123 { 124 for (int i = 15; i >= 0; i--, v >>= 4) 125 out[i] = "`0123456789abcdef`"[v & 15]; 126 } 127 128 static size_t dec(char *out, unsigned v) 129 { 130 char t[10]; 131 size_t n = 0, k = 0; 132 do 133 t[n++] = (char)('0' + v % 10); 134 while ((v /= 10) && n < sizeof t); 135 while (n) 136 out[k++] = t[--n]; 137 return k; 138 } 139 140 size_t ck_x86_report_seal(char *out, size_t len, size_t cap, size_t dropped) 141 { 142 static const char head[] = "report: console_copy_full dropped_bytes="; 143 if (!out || !dropped || cap - len < sizeof head + 11) /* digits of a 32-bit count, '\n' */ 144 return len; 145 memcpy(out + len, head, sizeof head - 1); 146 len += sizeof head - 1; 147 len += dec(out + len, dropped > 0xffffffffu ? 0xffffffffu : (unsigned)dropped); 148 out[len++] = '\n'; 149 return len; 150 } 151 152 unsigned ck_x86_report_parts(size_t len) 153 { 154 if (!len || len > CK_X86_REPORT_MAX) 155 return 0; 156 return (unsigned)((len + CK_X86_REPORT_CHUNK - 1) / CK_X86_REPORT_CHUNK); 157 } 158 159 size_t ck_x86_report_frame(const uint8_t mac[6], uint64_t run, const char *report, size_t len, unsigned part, 160 uint8_t *out, size_t cap) 161 { 162 static uint8_t data[160 + CK_X86_REPORT_CHUNK], udp[8 + sizeof data], ip[20 + sizeof udp]; 163 unsigned parts = ck_x86_report_parts(len); 164 if (!mac || !report || !out || part >= parts) 165 return 0; 166 size_t off = (size_t)part * CK_X86_REPORT_CHUNK; 167 size_t n = len - off < CK_X86_REPORT_CHUNK ? len - off : CK_X86_REPORT_CHUNK; 168 size_t h = sizeof REPORT_HDR - 1; 169 memcpy(data, REPORT_HDR "run=", h + 4); 170 h += 4; 171 hex16((char *)data + h, run); 172 h += 16; 173 memcpy(data + h, " part=", 6); 174 h += 6; 175 h += dec((char *)data + h, part); 176 data[h++] = '/'; 177 h += dec((char *)data + h, parts); 178 memcpy(data + h, " bytes=", 7); 179 h += 7; 180 h += dec((char *)data + h, (unsigned)len); 181 data[h++] = '\n'; 182 memcpy(data + h, report + off, n); 183 184 const net_ipv4 any = {{0, 0, 0, 0}}, bcast_ip = {{255, 255, 255, 255}}; 185 net_udp_header uh = {CK_X86_REPORT_PORT, CK_X86_REPORT_PORT}; 186 size_t uw = 0, iw = 0, ew = 0; 187 if (net_udp_build(&uh, data, h + n, any, bcast_ip, udp, sizeof udp, &uw) != NET_OK) 188 return 0; 189 net_ipv4_header ih; 190 ih.dscp_ecn = 0; 191 ih.identification = (uint16_t)part; 192 ih.ttl = 1; /* this network only */ 193 ih.protocol = NET_IPPROTO_UDP; 194 ih.source = any; 195 ih.destination = bcast_ip; 196 if (net_ipv4_build(&ih, udp, uw, ip, sizeof ip, &iw) != NET_OK) 197 return 0; 198 net_eth_frame e; 199 memset(e.destination.b, 0xff, 6); 200 memcpy(e.source.b, mac, 6); 201 e.ethertype = NET_ETHERTYPE_IPV4; 202 e.payload = ip; 203 e.payload_len = iw; 204 if (net_eth_build(&e, out, cap, &ew) != NET_OK) 205 return 0; 206 return ew; 207 } 208 209 #ifndef CK_X86_REPORT_HOST 210 /* Lab build only. Holds for `seconds` (longer while ck_x86_hold_step moves the 211 * end out: typing, or Allen's window, which stays up through sleep), sending the allowlisted report every 212 * 2 s while the link is up. Without a working network it only waits. Returns 213 * the rounds sent. */ 214 unsigned ck_x86_report_hold(uint64_t tsc_hz, unsigned seconds) 215 { 216 static uint8_t frame[1600]; 217 static int busy; /* a fault while sending ends up here again: only wait */ 218 uint64_t t0 = ck_rdtsc(), end = t0 + (uint64_t)seconds * tsc_hz, next = t0; 219 if (busy++ || !ck_x86_tg3_ready()) { 220 while (ck_rdtsc() < end) { 221 ck_x86_hold_step(&end); 222 ck_pause(); 223 } 224 return 0; 225 } 226 ck_printf("report: LAB BUILD, sending allowlisted diagnostic lines to UDP port %u (broadcast, " 227 "unauthenticated, auth=none) every 2 s during the hold\n", 228 (unsigned)CK_X86_REPORT_PORT); 229 static char report[CK_X86_REPORT_MAX]; 230 size_t len = 0; 231 unsigned parts = 0, rounds = 0, failed = 0; 232 uint8_t mac[6]; 233 ck_x86_tg3_mac(mac); 234 uint64_t run = 0; 235 while (ck_rdtsc() < end) { 236 ck_x86_hold_step(&end); /* typing keeps the hold going */ 237 if (ck_rdtsc() < next) { 238 ck_pause(); 239 continue; 240 } 241 next += 2 * tsc_hz; 242 if (!ck_x86_tg3_link()) 243 continue; 244 /* A fresh filtered copy each round, so lines typed during the hold go 245 * out too; a new copy gets a new run id. Only the filtered copy leaves 246 * the machine; the raw console never does. */ 247 size_t raw_len; 248 const char *raw = ck_x86_console_log(&raw_len); 249 size_t n_len = ck_x86_report_filter(raw, raw_len, report, sizeof report - CK_X86_REPORT_NOTE_MAX); 250 n_len = ck_x86_report_seal(report, n_len, sizeof report, ck_x86_console_log_dropped()); 251 if (n_len != len || !run) { 252 len = n_len; 253 parts = ck_x86_report_parts(len); 254 run = ck_rdtsc() ^ (uint64_t)len << 48; 255 } 256 for (unsigned p = 0; p < parts; p++) { 257 size_t n = ck_x86_report_frame(mac, run, report, len, p, frame, sizeof frame); 258 if (!n || ck_x86_tg3_send(frame, n)) 259 failed++; 260 } 261 if (ck_x86_tg3_drain(100)) 262 failed++; 263 rounds++; 264 } 265 /* Printed after the last copy, so only the screen shows it. */ 266 ck_printf("report: rounds=%u parts=%u bytes=%u send_failures=%u\n", rounds, parts, (unsigned)len, failed); 267 return rounds; 268 } 269 #endif 270 271 #ifndef CK_X86_REPORT_HOST 272 /* Allen's courier over Ethernet (courier_udp.h) bound to the chip: lab image 273 * only, like everything else in this file. The far side's address is the 274 * broadcast address unless the lab image was built with another 275 * (make imac-kernel CK_X86_REPORT=1 CK_COURIER_PEER=192.168.1.108). */ 276 #include "courier_udp.h" 277 #ifndef CK_COURIER_PEER_STR 278 #define CK_COURIER_PEER_STR "255.255.255.255" 279 #endif 280 static int nio_send(void *c, const uint8_t *f, size_t n) { (void)c; return ck_x86_tg3_send(f, n); } 281 static size_t nio_recv(void *c, uint8_t *f, size_t cap) { (void)c; return ck_x86_tg3_recv(f, cap); } 282 static int nio_link(void *c) { (void)c; return ck_x86_tg3_link(); } 283 static uint64_t nio_now(void *c) { (void)c; return ck_rdtsc(); } 284 static const ck_netio nio = {0, nio_send, nio_recv, nio_link, nio_now}; 285 286 static void dotted(const char *s, uint8_t out[4]) 287 { 288 for (int i = 0; i < 4; i++) { 289 unsigned v = 0; 290 while (*s >= '0' && *s <= '9') 291 v = v * 10 + (unsigned)(*s++ - '0'); 292 out[i] = (uint8_t)v; 293 if (*s == '.') 294 s++; 295 } 296 } 297 298 const ck_courier *ck_x86_courier_udp(uint64_t tsc_hz) 299 { 300 if (!ck_x86_tg3_ready()) 301 return 0; 302 uint8_t mac[6], peer[4]; 303 static const uint8_t none[4] = {0, 0, 0, 0}; 304 ck_x86_tg3_mac(mac); 305 dotted(CK_COURIER_PEER_STR, peer); 306 return ck_courier_udp_init(&nio, mac, none, peer, CK_COURIER_PORT, tsc_hz); 307 } 308 #endif