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 / svc / setup_inv.c

630 lines · 20782 bytes Raw Blame History
1/* setup_inv.c -- the machine inventory; see setup_inv.h. */
2#include "setup_inv.h"
3
4unsigned si_len(const char *s)
5{
6 unsigned n = 0;
7 while (s && s[n])
8 n++;
9 return n;
10}
11
12unsigned si_copy(char *d, const char *s, unsigned cap)
13{
14 unsigned n = 0;
15 while (s && s[n] && n + 1 < cap)
16 d[n] = s[n], n++;
17 d[n] = 0;
18 return n;
19}
20
21unsigned si_cat(char *d, unsigned at, const char *s, unsigned cap)
22{
23 while (s && *s && at + 1 < cap)
24 d[at++] = *s++;
25 d[at < cap ? at : cap - 1] = 0;
26 return at;
27}
28
29unsigned si_num(char *d, unsigned at, uint32_t v, unsigned cap)
30{
31 char t[12];
32 unsigned n = 0;
33 do
34 t[n++] = (char)('0' + v % 10);
35 while ((v /= 10) && n < sizeof t);
36 while (n && at + 1 < cap)
37 d[at++] = t[--n];
38 d[at < cap ? at : cap - 1] = 0;
39 return at;
40}
41
42static void zero(void *p, unsigned n)
43{
44 uint8_t *b = p;
45 while (n--)
46 *b++ = 0;
47}
48
49void si_init(si_inv *v) { zero(v, sizeof *v); }
50
51/* ---- the host description ---- */
52
53typedef struct {
54 const char *k, *v;
55 unsigned kn, vn;
56} kv;
57
58#define KV_MAX 12
59
60static int is_space(char c) { return c == ' ' || c == '\t' || c == '\r'; }
61
62static int eq(const char *s, unsigned n, const char *w)
63{
64 unsigned i = 0;
65 for (; i < n && w[i]; i++)
66 if (s[i] != w[i])
67 return 0;
68 return i == n && !w[i];
69}
70
71/* One line: its kind word and up to KV_MAX key=value pairs; 0 if malformed. */
72static int split(const char *s, unsigned n, const char **kind, unsigned *kindn, kv *out, unsigned *nkv,
73 unsigned *bad)
74{
75 unsigned i = 0, m = 0;
76 while (i < n && is_space(s[i]))
77 i++;
78 *kind = s + i;
79 while (i < n && !is_space(s[i]))
80 i++;
81 *kindn = (unsigned)(s + i - *kind);
82 for (;;) {
83 while (i < n && is_space(s[i]))
84 i++;
85 if (i >= n)
86 break;
87 unsigned k0 = i;
88 while (i < n && s[i] != '=' && !is_space(s[i]))
89 i++;
90 if (i >= n || s[i] != '=') { /* a word with no value: skipped */
91 (*bad)++;
92 while (i < n && !is_space(s[i]))
93 i++;
94 continue;
95 }
96 unsigned kn = i - k0, v0, vn;
97 i++;
98 if (i < n && s[i] == '"') {
99 v0 = ++i;
100 while (i < n && s[i] != '"')
101 i++;
102 if (i >= n)
103 return 0; /* an unclosed quote spoils the line */
104 vn = i - v0;
105 i++;
106 } else {
107 v0 = i;
108 while (i < n && !is_space(s[i]))
109 i++;
110 vn = i - v0;
111 }
112 if (m < KV_MAX)
113 out[m++] = (kv){s + k0, s + v0, kn, vn};
114 else
115 (*bad)++;
116 }
117 *nkv = m;
118 return *kindn > 0;
119}
120
121static void put(char *d, unsigned cap, const kv *p)
122{
123 unsigned n = p->vn < cap - 1 ? p->vn : cap - 1;
124 for (unsigned i = 0; i < n; i++)
125 d[i] = p->v[i] >= ' ' && p->v[i] < 127 ? p->v[i] : '?';
126 d[n] = 0;
127}
128
129static uint32_t num(const kv *p)
130{
131 uint64_t v = 0;
132 for (unsigned i = 0; i < p->vn && p->v[i] >= '0' && p->v[i] <= '9'; i++) {
133 v = v * 10 + (uint32_t)(p->v[i] - '0');
134 if (v > 0xffffffffu)
135 return 0xffffffffu;
136 }
137 return (uint32_t)v;
138}
139
140static uint16_t hex16(const kv *p)
141{
142 uint32_t v = 0;
143 for (unsigned i = 0; i < p->vn && i < 4; i++) {
144 char c = p->v[i];
145 unsigned d = c >= '0' && c <= '9' ? (unsigned)(c - '0')
146 : c >= 'a' && c <= 'f' ? (unsigned)(c - 'a' + 10)
147 : c >= 'A' && c <= 'F' ? (unsigned)(c - 'A' + 10)
148 : 16u;
149 if (d > 15)
150 break;
151 v = v << 4 | d;
152 }
153 return (uint16_t)v;
154}
155
156static uint16_t u16(uint32_t v) { return v > 0xffffu ? 0xffffu : (uint16_t)v; }
157
158static int item(si_inv *v, const char *kind, unsigned kn, const kv *p, unsigned m)
159{
160#define KEY(w) eq(p[i].k, p[i].kn, w)
161 if (eq(kind, kn, "gpu")) {
162 if (v->ngpu >= SI_GPUS)
163 return 0;
164 si_gpu *g = &v->gpu[v->ngpu];
165 zero(g, sizeof *g);
166 g->from = SI_FROM_HOST;
167 g->kind = SI_GPU_DISCRETE;
168 for (unsigned i = 0; i < m; i++) {
169 if (KEY("vendor"))
170 g->vendor = hex16(&p[i]);
171 else if (KEY("device"))
172 g->device = hex16(&p[i]);
173 else if (KEY("name"))
174 put(g->name, sizeof g->name, &p[i]);
175 else if (KEY("vram_mib"))
176 g->vram_mib = num(&p[i]);
177 else if (KEY("driver"))
178 put(g->driver, sizeof g->driver, &p[i]);
179 else if (KEY("kind"))
180 g->kind = eq(p[i].v, p[i].vn, "integrated") ? SI_GPU_INTEGRATED
181 : eq(p[i].v, p[i].vn, "unified") ? SI_GPU_UNIFIED
182 : eq(p[i].v, p[i].vn, "virtual") ? SI_GPU_VIRTUAL
183 : SI_GPU_DISCRETE;
184 else
185 v->skipped++;
186 }
187 v->ngpu++;
188 } else if (eq(kind, kn, "display")) {
189 if (v->ndisplay >= SI_DISPLAYS)
190 return 0;
191 si_display *d = &v->display[v->ndisplay];
192 zero(d, sizeof *d);
193 d->from = SI_FROM_HOST;
194 for (unsigned i = 0; i < m; i++) {
195 if (KEY("name"))
196 put(d->name, sizeof d->name, &p[i]);
197 else if (KEY("w"))
198 d->w = u16(num(&p[i]));
199 else if (KEY("h"))
200 d->h = u16(num(&p[i]));
201 else if (KEY("hz"))
202 d->hz = u16(num(&p[i]));
203 else if (KEY("max_w"))
204 d->max_w = u16(num(&p[i]));
205 else if (KEY("max_h"))
206 d->max_h = u16(num(&p[i]));
207 else if (KEY("max_hz"))
208 d->max_hz = u16(num(&p[i]));
209 else if (KEY("vrr"))
210 d->vrr = num(&p[i]) != 0;
211 else
212 v->skipped++;
213 }
214 v->ndisplay++;
215 } else if (eq(kind, kn, "cpu")) {
216 zero(&v->cpu, sizeof v->cpu);
217 v->cpu.from = SI_FROM_HOST;
218 for (unsigned i = 0; i < m; i++) {
219 if (KEY("name"))
220 put(v->cpu.name, sizeof v->cpu.name, &p[i]);
221 else if (KEY("fast"))
222 v->cpu.fast = u16(num(&p[i]));
223 else if (KEY("efficient"))
224 v->cpu.efficient = u16(num(&p[i]));
225 else if (KEY("threads"))
226 v->cpu.threads = u16(num(&p[i]));
227 else
228 v->skipped++;
229 }
230 } else if (eq(kind, kn, "ram")) {
231 for (unsigned i = 0; i < m; i++)
232 if (KEY("mib"))
233 v->ram_mib = num(&p[i]);
234 else
235 v->skipped++;
236 } else if (eq(kind, kn, "disk")) {
237 if (v->ndisk >= SI_DISKS)
238 return 0;
239 si_disk *d = &v->disk[v->ndisk];
240 zero(d, sizeof *d);
241 d->from = SI_FROM_HOST;
242 for (unsigned i = 0; i < m; i++) {
243 if (KEY("name"))
244 put(d->name, sizeof d->name, &p[i]);
245 else if (KEY("gib"))
246 d->gib = num(&p[i]);
247 else if (KEY("kind"))
248 d->kind = eq(p[i].v, p[i].vn, "nvme") ? SI_DISK_NVME
249 : eq(p[i].v, p[i].vn, "ssd") ? SI_DISK_SSD
250 : eq(p[i].v, p[i].vn, "hdd") ? SI_DISK_HDD
251 : SI_DISK_UNKNOWN;
252 else
253 v->skipped++;
254 }
255 v->ndisk++;
256 } else if (eq(kind, kn, "usb")) {
257 if (v->nusb >= SI_USB)
258 return 0;
259 for (unsigned i = 0; i < m; i++)
260 if (KEY("name"))
261 put(v->usb[v->nusb], SI_NAME, &p[i]);
262 else
263 v->skipped++;
264 v->nusb++;
265 } else if (eq(kind, kn, "acpi")) {
266 for (unsigned i = 0; i < m; i++)
267 if (KEY("tables"))
268 put(v->acpi, sizeof v->acpi, &p[i]);
269 else
270 v->skipped++;
271 } else if (eq(kind, kn, "gateway")) {
272 for (unsigned i = 0; i < m; i++)
273 if (KEY("addr"))
274 put(v->gateway, sizeof v->gateway, &p[i]);
275 else
276 v->skipped++;
277 } else if (eq(kind, kn, "peer")) {
278 if (v->npeer >= SI_PEERS)
279 return 0;
280 si_peer *q = &v->peer[v->npeer];
281 zero(q, sizeof *q);
282 q->kind = SI_PEER_OTHER;
283 for (unsigned i = 0; i < m; i++) {
284 if (KEY("name"))
285 put(q->name, sizeof q->name, &p[i]);
286 else if (KEY("addr"))
287 put(q->addr, sizeof q->addr, &p[i]);
288 else if (KEY("model"))
289 put(q->model, sizeof q->model, &p[i]);
290 else if (KEY("url"))
291 put(q->url, sizeof q->url, &p[i]);
292 else if (KEY("params_b10"))
293 q->params_b10 = u16(num(&p[i]));
294 else if (KEY("kind"))
295 q->kind = eq(p[i].v, p[i].vn, "aienos") ? SI_PEER_AIENOS
296 : eq(p[i].v, p[i].vn, "printer") ? SI_PEER_PRINTER
297 : eq(p[i].v, p[i].vn, "nas") ? SI_PEER_NAS
298 : SI_PEER_OTHER;
299 else
300 v->skipped++;
301 }
302 v->npeer++;
303 } else if (eq(kind, kn, "model") || eq(kind, kn, "server")) {
304 if (v->nmodel >= SI_MODELS)
305 return 0;
306 si_model *o = &v->model[v->nmodel];
307 zero(o, sizeof *o);
308 o->running = eq(kind, kn, "server");
309 o->src = o->running ? SI_SRC_SERVER : SI_SRC_LLAMACPP;
310 for (unsigned i = 0; i < m; i++) {
311 if (KEY("name"))
312 put(o->name, sizeof o->name, &p[i]);
313 else if (KEY("path") || KEY("url"))
314 put(o->where, sizeof o->where, &p[i]);
315 else if (KEY("params_b10"))
316 o->params_b10 = u16(num(&p[i]));
317 else if (KEY("bits10"))
318 o->bits10 = u16(num(&p[i]));
319 else if (KEY("mib"))
320 o->mib = num(&p[i]);
321 else if (KEY("source"))
322 o->src = eq(p[i].v, p[i].vn, "ollama") ? SI_SRC_OLLAMA
323 : eq(p[i].v, p[i].vn, "lmstudio") ? SI_SRC_LMSTUDIO
324 : eq(p[i].v, p[i].vn, "llamacpp") ? SI_SRC_LLAMACPP
325 : eq(p[i].v, p[i].vn, "pair") ? SI_SRC_PAIR
326 : o->src;
327 else
328 v->skipped++;
329 }
330 if (!o->name[0])
331 return 0;
332 v->nmodel++;
333 } else if (eq(kind, kn, "machine")) {
334 for (unsigned i = 0; i < m; i++)
335 if (KEY("name"))
336 put(v->machine, sizeof v->machine, &p[i]);
337 else if (!KEY("from"))
338 v->skipped++;
339 } else {
340 return 0;
341 }
342#undef KEY
343 return 1;
344}
345
346unsigned si_parse(si_inv *v, const char *text, unsigned n)
347{
348 unsigned taken = 0, i = 0;
349 while (i < n) {
350 unsigned s = i;
351 while (i < n && text[i] != '\n')
352 i++;
353 unsigned len = i - s;
354 i++;
355 const char *line = text + s;
356 unsigned j = 0;
357 while (j < len && is_space(line[j]))
358 j++;
359 if (j == len || line[j] == '#')
360 continue;
361 const char *kind;
362 unsigned kn, m = 0;
363 kv p[KV_MAX];
364 if (!split(line, len, &kind, &kn, p, &m, &v->skipped) || !item(v, kind, kn, p, m)) {
365 v->skipped++;
366 continue;
367 }
368 taken++;
369 }
370 if (taken)
371 v->host = 1;
372 return taken;
373}
374
375/* ---- what the kernel found ---- */
376
377const char *si_vendor(uint16_t vendor)
378{
379 switch (vendor) {
380 case 0x10de:
381 return "NVIDIA";
382 case 0x1002:
383 return "AMD";
384 case 0x8086:
385 return "Intel";
386 case 0x1af4:
387 return "virtio";
388 case 0x1234:
389 return "QEMU";
390 case 0x15ad:
391 return "VMware";
392 case 0x106b:
393 return "Apple";
394 default:
395 return "a";
396 }
397}
398
399void si_add_gpu(si_inv *v, uint16_t vendor, uint16_t device, uint32_t pci_class)
400{
401 if (v->ngpu >= SI_GPUS)
402 return;
403 si_gpu *g = &v->gpu[v->ngpu++];
404 zero(g, sizeof *g);
405 g->vendor = vendor, g->device = device, g->from = SI_FROM_HERE;
406 g->kind = vendor == 0x1234 || vendor == 0x1af4 || vendor == 0x15ad ? SI_GPU_VIRTUAL
407 : vendor == 0x8086 ? SI_GPU_INTEGRATED
408 : SI_GPU_DISCRETE;
409 unsigned n = si_copy(g->name, si_vendor(vendor), sizeof g->name);
410 n = si_cat(g->name, n, g->kind == SI_GPU_VIRTUAL ? " virtual display" : " graphics", sizeof g->name);
411 (void)pci_class;
412 (void)n;
413}
414
415void si_add_display(si_inv *v, uint16_t w, uint16_t h)
416{
417 if (v->ndisplay >= SI_DISPLAYS)
418 return;
419 si_display *d = &v->display[v->ndisplay++];
420 zero(d, sizeof *d);
421 si_copy(d->name, "this screen", sizeof d->name);
422 d->w = d->max_w = w, d->h = d->max_h = h;
423}
424
425void si_add_cpus(si_inv *v, unsigned count, const char *name)
426{
427 if (v->cpu.from == SI_FROM_HOST && v->cpu.fast)
428 return; /* the host's own processor says more */
429 zero(&v->cpu, sizeof v->cpu);
430 v->cpu.fast = u16(count), v->cpu.threads = u16(count);
431 si_copy(v->cpu.name, name, sizeof v->cpu.name);
432}
433
434void si_add_disk(si_inv *v, const char *name, uint64_t bytes, uint8_t kind)
435{
436 if (v->ndisk >= SI_DISKS)
437 return;
438 si_disk *d = &v->disk[v->ndisk++];
439 zero(d, sizeof *d);
440 si_copy(d->name, name, sizeof d->name);
441 d->gib = (uint32_t)(bytes >> 30);
442 d->kind = kind;
443}
444
445void si_add_ram(si_inv *v, uint64_t bytes)
446{
447 if (!v->ram_mib)
448 v->ram_mib = (uint32_t)(bytes >> 20);
449}
450
451int si_best_gpu(const si_inv *v)
452{
453 int best = -1;
454 for (unsigned i = 0; i < v->ngpu; i++)
455 if (v->gpu[i].kind != SI_GPU_VIRTUAL && (best < 0 || v->gpu[i].vram_mib > v->gpu[best].vram_mib))
456 best = (int)i;
457 return best;
458}
459
460/* ---- in plain words ---- */
461
462/* "24 GB" from MiB: whole gigabytes (1024 MiB each), rounded to nearest */
463static unsigned gb(char *d, unsigned at, uint32_t mib, unsigned cap)
464{
465 if (mib && mib < 1024) {
466 at = si_num(d, at, mib, cap);
467 return si_cat(d, at, " MB", cap);
468 }
469 at = si_num(d, at, (mib + 512) / 1024, cap);
470 return si_cat(d, at, " GB", cap);
471}
472
473static unsigned disk_size(char *d, unsigned at, uint32_t gib, unsigned cap)
474{
475 if (gib >= 1000) { /* 1.9 TB style: one decimal */
476 uint32_t t10 = (gib * 10 + 500) / 1000;
477 at = si_num(d, at, t10 / 10, cap);
478 if (t10 % 10) {
479 at = si_cat(d, at, ".", cap);
480 at = si_num(d, at, t10 % 10, cap);
481 }
482 return si_cat(d, at, " TB", cap);
483 }
484 at = si_num(d, at, gib, cap);
485 return si_cat(d, at, " GB", cap);
486}
487
488unsigned si_summary(const si_inv *v, char (*out)[96], unsigned max)
489{
490 unsigned n = 0;
491 const unsigned cap = 96;
492 /* in the order of FIRST_RUN_UX screen 5: screens, graphics, processor, memory, storage, nearby */
493 for (unsigned i = 0; i < v->ndisplay && n < max && i < 2; i++) {
494 const si_display *s = &v->display[i];
495 char *d = out[n++];
496 unsigned at = si_copy(d, "Screen: ", cap);
497 at = si_cat(d, at, s->name, cap);
498 at = si_cat(d, at, ", ", cap);
499 at = si_num(d, at, s->w, cap);
500 at = si_cat(d, at, " by ", cap);
501 at = si_num(d, at, s->h, cap);
502 if (s->hz) {
503 at = si_cat(d, at, ", ", cap);
504 at = si_num(d, at, s->hz, cap);
505 at = si_cat(d, at, " times a second", cap);
506 }
507 if (s->max_hz > s->hz && s->hz) {
508 at = si_cat(d, at, " (it can do ", cap);
509 at = si_num(d, at, s->max_hz, cap);
510 at = si_cat(d, at, ")", cap);
511 }
512 }
513 /* graphics: the best card, then how many more */
514 int g = si_best_gpu(v);
515 if (n < max) {
516 char *d = out[n++];
517 unsigned at = si_copy(d, "Graphics: ", cap);
518 if (g < 0) {
519 at = si_cat(d, at, v->ngpu ? "only a simple virtual display" : "none found", cap);
520 } else {
521 at = si_cat(d, at, v->gpu[g].name, cap);
522 if (v->gpu[g].vram_mib) {
523 at = si_cat(d, at, ", ", cap);
524 at = gb(d, at, v->gpu[g].vram_mib, cap);
525 at = si_cat(d, at, v->gpu[g].kind == SI_GPU_UNIFIED ? " of shared memory" : " of its own memory",
526 cap);
527 }
528 if (v->ngpu > 1) {
529 at = si_cat(d, at, ", and ", cap);
530 at = si_num(d, at, v->ngpu - 1, cap);
531 at = si_cat(d, at, " more", cap);
532 }
533 }
534 }
535 if (n < max && v->cpu.fast) {
536 char *d = out[n++];
537 unsigned at = si_copy(d, "Processor: ", cap);
538 unsigned cores = (unsigned)v->cpu.fast + v->cpu.efficient;
539 if (v->cpu.name[0] && v->cpu.from == SI_FROM_HOST) {
540 at = si_cat(d, at, v->cpu.name, cap);
541 at = si_cat(d, at, ", ", cap);
542 }
543 at = si_num(d, at, cores, cap);
544 at = si_cat(d, at, cores == 1 ? " core" : " cores", cap);
545 if (v->cpu.efficient) {
546 at = si_cat(d, at, " (", cap);
547 at = si_num(d, at, v->cpu.fast, cap);
548 at = si_cat(d, at, " fast, ", cap);
549 at = si_num(d, at, v->cpu.efficient, cap);
550 at = si_cat(d, at, " efficient)", cap);
551 }
552 }
553 if (n < max && v->ram_mib) {
554 char *d = out[n++];
555 unsigned at = si_copy(d, "Memory: ", cap);
556 gb(d, at, v->ram_mib, cap);
557 }
558 if (n < max && v->ndisk) {
559 char *d = out[n++];
560 unsigned at = si_copy(d, "Storage: ", cap);
561 for (unsigned i = 0; i < v->ndisk && i < 2; i++) {
562 if (i)
563 at = si_cat(d, at, "; ", cap);
564 at = disk_size(d, at, v->disk[i].gib, cap);
565 at = si_cat(d, at, v->disk[i].kind == SI_DISK_NVME ? " fast drive"
566 : v->disk[i].kind == SI_DISK_SSD ? " solid-state drive"
567 : v->disk[i].kind == SI_DISK_HDD ? " hard disk"
568 : " disk",
569 cap);
570 }
571 if (v->ndisk > 2) {
572 at = si_cat(d, at, " and ", cap);
573 at = si_num(d, at, v->ndisk - 2, cap);
574 at = si_cat(d, at, " more", cap);
575 }
576 }
577 if (n < max && (v->npeer || v->gateway[0])) {
578 char *d = out[n++];
579 unsigned at = si_copy(d, "Nearby: ", cap), aienos = 0;
580 for (unsigned i = 0; i < v->npeer; i++)
581 aienos += v->peer[i].kind == SI_PEER_AIENOS;
582 if (aienos) {
583 at = si_num(d, at, aienos, cap);
584 at = si_cat(d, at, aienos == 1 ? " other AienOS computer (" : " other AienOS computers (", cap);
585 unsigned shown = 0;
586 for (unsigned i = 0; i < v->npeer && shown < 2; i++)
587 if (v->peer[i].kind == SI_PEER_AIENOS) {
588 if (shown++)
589 at = si_cat(d, at, ", ", cap);
590 at = si_cat(d, at, v->peer[i].name, cap);
591 }
592 at = si_cat(d, at, ")", cap);
593 }
594 if (v->npeer > aienos) {
595 at = si_cat(d, at, aienos ? ", " : "", cap);
596 at = si_num(d, at, v->npeer - aienos, cap);
597 at = si_cat(d, at, v->npeer - aienos == 1 ? " other device" : " other devices", cap);
598 }
599 if (v->gateway[0])
600 at = si_cat(d, at, aienos || v->npeer ? ", and your router" : "your router", cap);
601 }
602 if (n < max && v->nmodel) {
603 char *d = out[n++];
604 unsigned at = si_copy(d, "Models already here: ", cap);
605 for (unsigned i = 0; i < v->nmodel && i < 2; i++) {
606 if (i)
607 at = si_cat(d, at, ", ", cap);
608 at = si_cat(d, at, v->model[i].name, cap);
609 }
610 if (v->nmodel > 2) {
611 at = si_cat(d, at, " and ", cap);
612 at = si_num(d, at, v->nmodel - 2, cap);
613 at = si_cat(d, at, " more", cap);
614 }
615 }
616 if (n < max && (v->nusb || v->npci)) {
617 char *d = out[n++];
618 unsigned at = si_copy(d, "Also: ", cap);
619 if (v->nusb) {
620 at = si_num(d, at, v->nusb, cap);
621 at = si_cat(d, at, v->nusb == 1 ? " USB device" : " USB devices", cap);
622 }
623 if (v->npci) {
624 at = si_cat(d, at, v->nusb ? ", " : "", cap);
625 at = si_num(d, at, v->npci, cap);
626 at = si_cat(d, at, " parts on the main board", cap);
627 }
628 }
629 return n;
630}