interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / gpu / driver / gsp / logdec.om

1059 lines · 20904 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note gsp/logdec.om: Pure-Omega (OM-1) LibosLog and CrashCat decoder for GB20B GSP bring-up.
3note Host-only decoder over caller-supplied memory images; never opens a device, maps MMIO, or performs DMA.
4note Verified GB20B arena layout reference: gpu/driver/gsp/arena.om (arlogs, arlogpt, arnames).
5note LibosMemoryRegionInitArgument: 32 bytes/entry (+0 id8 BE-ASCII, +8 pa, +16 size, +24 kind=1, +25 loc=1).
6note Verified 8 GB20B region IDs (arena.om:42,271-289):
7note LOGINIT=0x004c4f47494e4954 (64 KiB), LOGINTR=0x004c4f47494e5452 (64 KiB),
8note LOGRM=0x0000004c4f47524d (64 KiB), LOGMNOC=0x004c4f474d4e4f43 (64 KiB),
9note LOGDBG=0x00004c4f47444247 (64 KiB), LOGROOT=0x004c4f47524f4f54 (4 KiB),
10note LOGKRNL=0x004c4f474b524e4c (64 KiB), RMARGS=0x0000524d41524753 (4 KiB).
11note Verified paged log-buffer header (arena.om:260-270 arlogpt):
12note Page 0 (4096 bytes): +0 u64 LE put (monotonic byte offset), +8..+8+8*n page_table[0..n-1] (pa + j*4096).
13note Pages 1..n-1 (size - 4096 bytes): circular u64-slot ring data (61440 bytes = 7680 qwords for 64 KiB).
14note LOGROOT (4096 bytes) has 0 data bytes and is handled without division by zero.
15note CrashCat V1 wire format (NVIDIA open-gpu-kernel-modules 580.173.02 nv-crashcat.h):
16note NvCrashCatPacketHeader_V1 (u64 LE): [63:48] sig=0xCA70 (51824), [47:44] ver=1,
17note [43:40] type (0=REPORT,1=STACK_TRACE,2=REG32,3=REG64,4=IO32,5=IO64,6=RV64_CSR,7=RV64_GPR,8=RV64_TRACE),
18note [39:32] payload_qwords (0..255), [31:0] header_data.
19note Prefix lg (LibosLog/RMARGS/formatter) and cc (CrashCat). Library unit: no main.
20
21note ---- Region ID constants (verified via Python from ASCII big-endian packing in arena.om:274-278) ----
22const LGIDINIT ulong 21479265821149524
23const LGIDINTR ulong 21479265821152338
24const LGIDRM ulong 327747588685
25const LGIDMNOC ulong 21479265888259907
26const LGIDDBG ulong 83903381783111
27const LGIDROOT ulong 21479265972211540
28const LGIDKRNL ulong 21479265854967372
29const LGIDARGS ulong 90491761870675
30
31note ---- Buffer and limit constants ----
32const LGPAGE ulong 4096
33const LGBMAX ulong 65536
34const LGTMAX ulong 32
35const LGMAXA ulong 20
36const LGOCAP ulong 4096
37const LGU32 ulong 4294967295
38const LGFMTM ulong 281474976710655
39const LGWPRMAG ulong 15869187694674993331
40const LGWPRSZ ulong 256
41const LGRMASZ ulong 128
42const LGCCQPA ulong 224
43const LGCCQSZ ulong 232
44
45note ---- CrashCat V1 constants (0xCA70 = 51824) ----
46const CCSIG ulong 51824
47const CCVER1 ulong 1
48const CCTRPT ulong 0
49const CCTSTK ulong 1
50const CCTR32 ulong 2
51const CCTR64 ulong 3
52const CCTI32 ulong 4
53const CCTI64 ulong 5
54const CCTCSR ulong 6
55const CCTGPR ulong 7
56const CCTTRC ulong 8
57const CCMAXT ulong 8
58const CCMAXP ulong 256
59const CCRMIN ulong 5
60const CCGPRN ulong 31
61
62note ---- WPR meta (256 B) and RMARGS (128 B) inspection state ----
63global lgwpr uchar 256
64global lgccqpa ulong 1
65global lgccqsz ulong 1
66global lgrma uchar 128
67note lgrmout: 0 sharedPA, 1 ptCount, 2 cmdOff, 3 statOff, 4 gpuInst, 5 passthru, 6 heapPA, 7 heapBytes
68global lgrmout ulong 8
69
70note ---- LibosMemoryRegionInitArgument table state ----
71global lgtbuf uchar 4096
72global lgtcnt ulong 1
73global lgtid ulong 32
74global lgtpa ulong 32
75global lgtsz ulong 32
76global lgtkl ulong 32
77
78note ---- LibosLog ring buffer and decoded record state ----
79global lgbuf uchar 65536
80global lgsize ulong 1
81global lghdr ulong 1
82global lgdata ulong 1
83global lgput ulong 1
84global lgget ulong 1
85global lgdrop ulong 1
86global lgwrap ulong 1
87global lgrcnt ulong 1
88global lgfmt ulong 1
89global lgts ulong 1
90global lgargc ulong 1
91global lglvl ulong 1
92global lgflg ulong 1
93global lgargs ulong 20
94
95note ---- CrashCat linear buffer and decoded packet state ----
96global ccbuf uchar 65536
97global cclen ulong 1
98global ccpos ulong 1
99global ccver ulong 1
100global cctype ulong 1
101global ccplen ulong 1
102global cchdat ulong 1
103global ccpay ulong 256
104global ccrsrc ulong 1
105global ccrcse ulong 1
106global ccrext ulong 1
107global ccrts ulong 1
108global ccrver ulong 1
109
110note ---- Buffered diagnostic formatter state (stdout/caller fd) ----
111global lgob uchar 4096
112global lgon ulong 1
113global lgofd int 1
114global lgoerr ulong 1
115text LGHEX "0123456789abcdef"
116text LGREG0 "LOGINIT"
117text LGREG1 "LOGINTR"
118text LGREG2 "LOGRM"
119text LGREG3 "LOGMNOC"
120text LGREG4 "LOGDBG"
121text LGREG5 "LOGROOT"
122text LGREG6 "LOGKRNL"
123text LGREG7 "RMARGS"
124text LGREGU "UNKNOWN"
125text LGKREG "REGION "
126text LGKPA " pa=0x"
127text LGKSZ " size=0x"
128text LGKKD " kind="
129text LGKLC " loc="
130text LGKLOG "LOG ts=0x"
131text LGKLVL " lvl="
132text LGKFMT " fmt=0x"
133text LGKARC " argc="
134text LGKARG " args=["
135text LGKCC "CRASHCAT "
136text LGKVER " ver="
137text LGKHD " hdata=0x"
138text LGKSRC " src=0x"
139text LGKCSE " cause=0x"
140text LGKEXT " extra=0x"
141text LGKTS " ts=0x"
142text LGKRVR " rver=0x"
143text LGKCNT " count="
144text LGKOFF " off=0x"
145text LGKVAL " val=0x"
146text LGK0X "0x"
147text LGCT0 "REPORT"
148text LGCT1 "STACK_TRACE"
149text LGCT2 "REGISTER32"
150text LGCT3 "REGISTER64"
151text LGCT4 "IO32_STATE"
152text LGCT5 "IO64_STATE"
153text LGCT6 "RISCV64_CSR"
154text LGCT7 "RISCV64_GPR"
155text LGCT8 "RISCV64_TRACE"
156
157note ==== Low-level little-endian readers (bounds-checked by callers) ====
158
159fn lgu64t o ulong to ulong
160v = ulong 0
161for i ulong 8
162b = ulong get lgtbuf add o i
163v = or v shl b mul i 8
164end
165ret v
166end
167
168fn lgu64w o ulong to ulong
169v = ulong 0
170for i ulong 8
171b = ulong get lgwpr add o i
172v = or v shl b mul i 8
173end
174ret v
175end
176
177fn lgu64r o ulong to ulong
178v = ulong 0
179for i ulong 8
180b = ulong get lgrma add o i
181v = or v shl b mul i 8
182end
183ret v
184end
185
186fn lgu32r o ulong to ulong
187v = ulong 0
188for i ulong 4
189b = ulong get lgrma add o i
190v = or v shl b mul i 8
191end
192ret v
193end
194
195note ==== GspFwWprMeta CrashCat queue locator (gb20b-gsp-boot.md:17, arena.om:312-325) ====
196note Verifies lgwpr[0..256): +0 magic==0xdc3aae21371a60b3, +8 rev==1,
197note extracts +224 sysmemAddrOfCrashReportQueue and +232 sizeOfCrashReportQueue.
198note Returns:
199note 1 CrashCat queue present (lgccqpa, lgccqsz populated; page-aligned and non-zero)
200note 0 WPR meta valid, CrashCat queue absent (both +224 and +232 are 0)
201note -1 bad magic or revision
202note -2 malformed CrashCat queue descriptor (unaligned PA/size, size > LGBMAX, or one zero)
203
204fn lgwprparse to long
205put lgccqpa 0 0
206put lgccqsz 0 0
207if ne lgu64w 0 LGWPRMAG
208ret -1
209end
210if ne lgu64w 8 1
211ret -1
212end
213pa = lgu64w LGCCQPA
214sz = lgu64w LGCCQSZ
215if and eq pa 0 eq sz 0
216ret 0
217end
218if or eq pa 0 eq sz 0
219ret -2
220end
221if or ne and pa 4095 0 ne and sz 4095 0
222ret -2
223end
224if gt sz LGBMAX
225ret -2
226end
227put lgccqpa 0 pa
228put lgccqsz 0 sz
229ret 1
230end
231
232note ==== RMARGS (GSP_ARGUMENTS_CACHED) parser (arena.om:294-303) ====
233note Extracts sharedMemPhysAddr (+0), ptCount (+8 u32), cmdQueueOffset (+16),
234note statQueueOffset (+24), gpuInstance (+44 u32), bIsPassthru (+48 u8),
235note heapPA (+72), heapBytes (+80) from lgrma[0..128) into lgrmout[0..7].
236note Returns 0 on valid GB20B/arena.om shape, -1 on alignment/offset violation.
237
238fn lgrmargs to long
239for i ulong 8
240put lgrmout i 0
241end
242spa = lgu64r 0
243ptc = lgu32r 8
244cmdo = lgu64r 16
245stato = lgu64r 24
246if or eq spa 0 ne and spa 4095 0
247ret -1
248end
249if eq ptc 0
250ret -1
251end
252if or ne cmdo LGPAGE lt stato cmdo
253ret -1
254end
255hpa = lgu64r 72
256hsz = lgu64r 80
257if or ne and hpa 4095 0 ne and hsz 4095 0
258ret -1
259end
260put lgrmout 0 spa
261put lgrmout 1 ptc
262put lgrmout 2 cmdo
263put lgrmout 3 stato
264put lgrmout 4 lgu32r 44
265put lgrmout 5 ulong get lgrma 48
266put lgrmout 6 hpa
267put lgrmout 7 hsz
268ret 0
269end
270
271fn lgu64b o ulong to ulong
272v = ulong 0
273for i ulong 8
274b = ulong get lgbuf add o i
275v = or v shl b mul i 8
276end
277ret v
278end
279
280fn ccu64b o ulong to ulong
281v = ulong 0
282for i ulong 8
283b = ulong get ccbuf add o i
284v = or v shl b mul i 8
285end
286ret v
287end
288
289note ==== Region ID classification ====
290
291fn lgkind id ulong to long
292if eq id LGIDINIT
293ret 0
294end
295if eq id LGIDINTR
296ret 1
297end
298if eq id LGIDRM
299ret 2
300end
301if eq id LGIDMNOC
302ret 3
303end
304if eq id LGIDDBG
305ret 4
306end
307if eq id LGIDROOT
308ret 5
309end
310if eq id LGIDKRNL
311ret 6
312end
313if eq id LGIDARGS
314ret 7
315end
316ret -1
317end
318
319note ==== LibosMemoryRegionInitArgument table parser ====
320note Reads up to LGTMAX (32) 32-byte entries from lgtbuf[0..n).
321note Stops at the first zero id8 terminator or when n bytes are consumed.
322note Returns entry count (>=0) on success, or negative error:
323note -1 bad table length (not multiple of 32 or > 4096)
324note -2 non-zero reserved bytes (+26..+31)
325note -3 invalid kind/loc (must be kind=1, loc=1) or unaligned/zero pa/size
326note -4 exceeds LGTMAX entries without terminator
327
328fn lgtabparse n ulong to long
329put lgtcnt 0 0
330if or eq n 0 gt n LGPAGE
331ret -1
332end
333if ne rem n 32 0
334ret -1
335end
336maxe = div n 32
337cnt = ulong 0
338for i ulong maxe
339off = mul i 32
340id = lgu64t off
341if eq id 0
342break
343end
344if ge cnt LGTMAX
345ret -4
346end
347for r ulong 6
348if ne get lgtbuf add off add 26 r 0
349ret -2
350end
351end
352pa = lgu64t add off 8
353sz = lgu64t add off 16
354if ne ulong get lgtbuf add off 24 1
355ret -3
356end
357if ne ulong get lgtbuf add off 25 1
358ret -3
359end
360if eq sz 0
361ret -3
362end
363if or ne and pa 4095 0 ne and sz 4095 0
364ret -3
365end
366put lgtid cnt id
367put lgtpa cnt pa
368put lgtsz cnt sz
369put lgtkl cnt 257
370cnt = add cnt 1
371end
372put lgtcnt 0 cnt
373ret long cnt
374end
375
376note ==== Paged LibosLog page-table validator (matches arena.om:arlogpt) ====
377note Verifies lgbuf[0..total_size) page 0 page table entries at +8..+8+8*npages
378note equal base_pa + j*4096 for j in 0..npages-1.
379note Returns 0 on success, -1 invalid size/alignment, -2 page-table entry mismatch.
380
381fn lgptchk basepa ulong totsize ulong to long
382if or lt totsize LGPAGE gt totsize LGBMAX
383ret -1
384end
385if or ne and totsize 4095 0 ne and basepa 4095 0
386ret -1
387end
388np = div totsize LGPAGE
389if gt np 511
390ret -1
391end
392for j ulong np
393off = add 8 mul j 8
394want = add basepa mul j LGPAGE
395got = lgu64b off
396if ne got want
397ret -2
398end
399end
400ret 0
401end
402
403note ==== LibosLog ring buffer control and wrap-around reader ====
404note lgrinit configures a ring buffer in lgbuf[0..totsize) with data starting at hdrsize.
405note Standard arena.om paged buffers use hdrsize=4096 (data=61440 for 64 KiB, data=0 for LOGROOT 4 KiB).
406note Linear header-only buffers may use hdrsize=8.
407note Returns 0 on success, -1 invalid geometry.
408
409fn lgrinit totsize ulong hdrsize ulong to long
410put lgsize 0 0
411put lghdr 0 0
412put lgdata 0 0
413put lgput 0 0
414put lgget 0 0
415put lgdrop 0 0
416put lgwrap 0 0
417put lgrcnt 0 0
418if or lt totsize 8 gt totsize LGBMAX
419ret -1
420end
421if or lt hdrsize 8 gt hdrsize totsize
422ret -1
423end
424if ne and hdrsize 7 0
425ret -1
426end
427dat = sub totsize hdrsize
428if ne and dat 7 0
429ret -1
430end
431put lgsize 0 totsize
432put lghdr 0 hdrsize
433put lgdata 0 dat
434ret 0
435end
436
437note lgrsetget allows a caller to restore a saved host cursor (must be 8-byte aligned).
438fn lgrsetget g ulong to long
439if ne and g 7 0
440ret -3
441end
442put lgget 0 g
443ret 0
444end
445
446note lgrsync reads the firmware put pointer at lgbuf[0..8), validates it against lgget,
447note and handles ring wrap-around overflow by advancing lgget to (put - lgdata) and
448note accumulating dropped byte count in lgdrop and wrap count in lgwrap.
449note Returns available unread bytes (>=0) on success, or negative error:
450note -1 ring not initialized (lgsize < 8)
451note -2 put < get (cursor regression or reset)
452note -3 put not 8-byte aligned
453note -4 non-zero put in a zero-data buffer (e.g. 4 KiB LOGROOT)
454
455fn lgrsync to long
456sz = get lgsize 0
457if lt sz 8
458ret -1
459end
460p = lgu64b 0
461if ne and p 7 0
462ret -3
463end
464g = get lgget 0
465if lt p g
466ret -2
467end
468put lgput 0 p
469dat = get lgdata 0
470if eq dat 0
471if gt p 0
472ret -4
473end
474ret 0
475end
476avail = sub p g
477if gt avail dat
478dropped = sub avail dat
479put lgdrop 0 add get lgdrop 0 dropped
480put lgwrap 0 add get lgwrap 0 1
481g = sub p dat
482put lgget 0 g
483avail = dat
484end
485ret long avail
486end
487
488note lgrqword reads one 64-bit LE word at logical byte offset pos (8-byte aligned),
489note wrapping modulo lgdata inside lgbuf[lghdr .. lghdr + lgdata).
490note Caller must guarantee lgdata >= 8 and pos is 8-byte aligned.
491
492fn lgrqword pos ulong to ulong
493dat = get lgdata 0
494if lt dat 8
495ret 0
496end
497off = add get lghdr 0 rem pos dat
498if gt add off 8 get lgsize 0
499ret 0
500end
501ret lgu64b off
502end
503
504note lgpeek inspects the next u64 word (format ID / meta_va) without advancing lgget.
505note Returns 1 if a word is available (stored in lgfmt), 0 if empty, or negative error from lgrsync.
506
507fn lgpeek to long
508av = lgrsync
509if le av 0
510ret av
511end
512if lt av 8
513ret -6
514end
515put lgfmt 0 lgrqword get lgget 0
516ret 1
517end
518
519note lgnext decodes one self-describing framed LibosLog record from the ring buffer.
520note Slot 0 (meta word):
521note [47:0] format_id (48-bit metadata VA / format ID)
522note [55:48] argc (0..20)
523note [59:56] level (0..15)
524note [63:60] flags (bit 0 = has_timestamp)
525note Slot 1 (if flags & 1 != 0): 64-bit PTIMER timestamp (ns).
526note Next argc slots: u64 arguments stored in lgargs[0..argc-1].
527note Returns:
528note 1 record decoded (lgfmt, lgts, lgargc, lglvl, lgflg, lgargs updated; lgget advanced)
529note 0 empty (no unread bytes)
530note -1..-4 sync errors from lgrsync
531note -5 argc > LGMAXA (20)
532note -6 truncated record (not enough unread bytes; lgget unchanged)
533
534fn lgnext to long
535av = lgrsync
536if le av 0
537ret av
538end
539if lt av 8
540ret -6
541end
542pos = get lgget 0
543meta = lgrqword pos
544argc = and shr meta 48 255
545if gt argc LGMAXA
546ret -5
547end
548hasts = and shr meta 60 1
549needb = mul add 1 add hasts argc 8
550if lt ulong av needb
551ret -6
552end
553put lgfmt 0 and meta LGFMTM
554put lgargc 0 argc
555put lglvl 0 and shr meta 56 15
556put lgflg 0 and shr meta 60 15
557pos = add pos 8
558if eq hasts 1
559put lgts 0 lgrqword pos
560pos = add pos 8
561else
562put lgts 0 0
563end
564for i ulong argc
565put lgargs i lgrqword pos
566pos = add pos 8
567end
568put lgget 0 pos
569put lgrcnt 0 add get lgrcnt 0 1
570ret 1
571end
572
573note lgnextraw decodes one ELF-metadata-linked LibosLog record where the caller supplies
574note argc (0..20) and hasts (0 or 1) obtained from the firmware .logging section for lgfmt.
575note Slot 0 is stored verbatim as the full 64-bit meta_va in lgfmt.
576note Returns 1 decoded, 0 empty, -1..-4 sync errors, -5 bad argc, -6 truncated.
577
578fn lgnextraw argc ulong hasts ulong to long
579if gt argc LGMAXA
580ret -5
581end
582if gt hasts 1
583ret -5
584end
585av = lgrsync
586if le av 0
587ret av
588end
589needw = add 1 add hasts argc
590needb = mul needw 8
591if lt ulong av needb
592ret -6
593end
594pos = get lgget 0
595put lgfmt 0 lgrqword pos
596put lgargc 0 argc
597put lglvl 0 0
598put lgflg 0 hasts
599pos = add pos 8
600if eq hasts 1
601put lgts 0 lgrqword pos
602pos = add pos 8
603else
604put lgts 0 0
605end
606for i ulong argc
607put lgargs i lgrqword pos
608pos = add pos 8
609end
610put lgget 0 pos
611put lgrcnt 0 add get lgrcnt 0 1
612ret 1
613end
614
615note ==== CrashCat V1 packet and report decoder ====
616note ccinit initializes linear CrashCat decoding over ccbuf[0..n).
617
618fn ccinit n ulong to long
619put cclen 0 0
620put ccpos 0 0
621if gt n LGBMAX
622ret -1
623end
624put cclen 0 n
625ret 0
626end
627
628note ccchk validates a CrashCat V1 header word and stores parsed header fields.
629note Returns total packet byte length (>0) on success, or negative error:
630note -10 bad signature (not 0xCA70)
631note -11 unsupported format version (not 1)
632note -12 unknown packet type (> 8)
633note -14 invalid payload qword count for typed packet
634
635fn ccchk hdr ulong to long
636sig = and shr hdr 48 65535
637if ne sig CCSIG
638ret -10
639end
640ver = and shr hdr 44 15
641if ne ver CCVER1
642ret -11
643end
644pt = and shr hdr 40 15
645if gt pt CCMAXT
646ret -12
647end
648plen = and shr hdr 32 255
649if eq pt CCTRPT
650if lt plen CCRMIN
651ret -14
652end
653end
654if eq pt CCTGPR
655if ne plen CCGPRN
656ret -14
657end
658end
659if eq pt CCTCSR
660if eq plen 0
661ret -14
662end
663end
664if or eq pt CCTR64 eq pt CCTI64
665if ne and plen 1 0
666ret -14
667end
668end
669put ccver 0 ver
670put cctype 0 pt
671put ccplen 0 plen
672put cchdat 0 and hdr LGU32
673ret long mul add 1 plen 8
674end
675
676note ccfinal populates structured report fields when cctype == CCTRPT.
677fn ccfinal to void
678if eq get cctype 0 CCTRPT
679put ccrsrc 0 get ccpay 0
680put ccrcse 0 get ccpay 1
681put ccrext 0 get ccpay 2
682put ccrts 0 get ccpay 3
683put ccrver 0 get ccpay 4
684else
685put ccrsrc 0 0
686put ccrcse 0 0
687put ccrext 0 0
688put ccrts 0 0
689put ccrver 0 0
690end
691end
692
693note ccnext decodes one CrashCat V1 packet from linear ccbuf[ccpos..cclen).
694note Returns:
695note 1 packet decoded (ccver, cctype, ccplen, cchdat, ccpay, and ccr* if REPORT updated; ccpos advanced)
696note 0 end of buffer (ccpos == cclen)
697note -10..-12, -14 header/shape errors from ccchk
698note -13 truncated header or payload (ccpos unchanged)
699
700fn ccnext to long
701p = get ccpos 0
702n = get cclen 0
703if ge p n
704ret 0
705end
706av = sub n p
707if lt av 8
708ret -13
709end
710hdr = ccu64b p
711need = ccchk hdr
712if lt need 0
713ret need
714end
715if lt av ulong need
716ret -13
717end
718plen = get ccplen 0
719q = add p 8
720for i ulong plen
721put ccpay i ccu64b q
722q = add q 8
723end
724ccfinal
725put ccpos 0 add p ulong need
726ret 1
727end
728
729note ccnextring decodes one CrashCat V1 packet directly from the wrapped LibosLog ring buffer.
730note Advances lgget only after full validation and extraction.
731note Returns 1 decoded, 0 empty, -1..-4 ring sync error, -10..-14 CrashCat error.
732
733fn ccnextring to long
734av = lgrsync
735if le av 0
736ret av
737end
738if lt av 8
739ret -13
740end
741p = get lgget 0
742hdr = lgrqword p
743need = ccchk hdr
744if lt need 0
745ret need
746end
747if lt ulong av ulong need
748ret -13
749end
750plen = get ccplen 0
751q = add p 8
752for i ulong plen
753put ccpay i lgrqword q
754q = add q 8
755end
756ccfinal
757put lgget 0 add p ulong need
758ret 1
759end
760
761note Helper accessors for REGISTER32 / IO32_STATE packed pairs (low32=offset, high32=value).
762fn ccpair_off i ulong to ulong
763if ge i get ccplen 0
764ret 0
765end
766ret and get ccpay i LGU32
767end
768
769fn ccpair_val i ulong to ulong
770if ge i get ccplen 0
771ret 0
772end
773ret and shr get ccpay i 32 LGU32
774end
775
776note ==== Buffered diagnostic text formatter (stdout / caller fd) ====
777
778fn lgoinit fd int to void
779put lgon 0 0
780put lgofd 0 fd
781put lgoerr 0 0
782end
783
784fn lgoflush to long
785n = get lgon 0
786p = ulong 0
787while lt p n bound 4096
788r = write get lgofd 0 lgob p sub n p
789if le r 0
790put lgoerr 0 1
791put lgon 0 0
792ret -5
793end
794p = add p ulong r
795end
796put lgon 0 0
797ret 0
798end
799
800fn lgoc c uchar to void
801if ge get lgon 0 LGOCAP
802z = lgoflush
803end
804n = get lgon 0
805put lgob n c
806put lgon 0 add n 1
807end
808
809fn lgodec v ulong to void
810d = new uchar 24 0
811n = ulong 0
812x = v
813while lt n 24 bound 24
814put d n add uchar rem x 10 48
815n = add n 1
816x = div x 10
817if eq x 0
818break
819end
820end
821while gt n 0 bound 24
822n = sub n 1
823lgoc get d n
824end
825end
826
827fn lgohex v ulong to void
828d = new uchar 16 0
829n = ulong 0
830x = v
831while lt n 16 bound 16
832put d n get LGHEX and x 15
833n = add n 1
834x = shr x 4
835if eq x 0
836break
837end
838end
839while gt n 0 bound 16
840n = sub n 1
841lgoc get d n
842end
843end
844
845fn lgoregname k long to void
846if eq k 0
847for i ulong len LGREG0
848lgoc get LGREG0 i
849end
850ret
851end
852if eq k 1
853for i ulong len LGREG1
854lgoc get LGREG1 i
855end
856ret
857end
858if eq k 2
859for i ulong len LGREG2
860lgoc get LGREG2 i
861end
862ret
863end
864if eq k 3
865for i ulong len LGREG3
866lgoc get LGREG3 i
867end
868ret
869end
870if eq k 4
871for i ulong len LGREG4
872lgoc get LGREG4 i
873end
874ret
875end
876if eq k 5
877for i ulong len LGREG5
878lgoc get LGREG5 i
879end
880ret
881end
882if eq k 6
883for i ulong len LGREG6
884lgoc get LGREG6 i
885end
886ret
887end
888if eq k 7
889for i ulong len LGREG7
890lgoc get LGREG7 i
891end
892ret
893end
894for i ulong len LGREGU
895lgoc get LGREGU i
896end
897end
898
899fn lgcctname t ulong to void
900if eq t 0
901for i ulong len LGCT0
902lgoc get LGCT0 i
903end
904ret
905end
906if eq t 1
907for i ulong len LGCT1
908lgoc get LGCT1 i
909end
910ret
911end
912if eq t 2
913for i ulong len LGCT2
914lgoc get LGCT2 i
915end
916ret
917end
918if eq t 3
919for i ulong len LGCT3
920lgoc get LGCT3 i
921end
922ret
923end
924if eq t 4
925for i ulong len LGCT4
926lgoc get LGCT4 i
927end
928ret
929end
930if eq t 5
931for i ulong len LGCT5
932lgoc get LGCT5 i
933end
934ret
935end
936if eq t 6
937for i ulong len LGCT6
938lgoc get LGCT6 i
939end
940ret
941end
942if eq t 7
943for i ulong len LGCT7
944lgoc get LGCT7 i
945end
946ret
947end
948for i ulong len LGCT8
949lgoc get LGCT8 i
950end
951end
952
953fn lgfmtreg idx ulong to void
954if ge idx get lgtcnt 0
955ret
956end
957for i ulong len LGKREG
958lgoc get LGKREG i
959end
960lgoregname lgkind get lgtid idx
961for i ulong len LGKPA
962lgoc get LGKPA i
963end
964lgohex get lgtpa idx
965for i ulong len LGKSZ
966lgoc get LGKSZ i
967end
968lgohex get lgtsz idx
969kl = get lgtkl idx
970for i ulong len LGKKD
971lgoc get LGKKD i
972end
973lgodec and kl 255
974for i ulong len LGKLC
975lgoc get LGKLC i
976end
977lgodec and shr kl 8 255
978lgoc 10
979end
980
981fn lgfmtlog to void
982for i ulong len LGKLOG
983lgoc get LGKLOG i
984end
985lgohex get lgts 0
986for i ulong len LGKLVL
987lgoc get LGKLVL i
988end
989lgodec get lglvl 0
990for i ulong len LGKFMT
991lgoc get LGKFMT i
992end
993lgohex get lgfmt 0
994for i ulong len LGKARC
995lgoc get LGKARC i
996end
997lgodec get lgargc 0
998for i ulong len LGKARG
999lgoc get LGKARG i
1000end
1001ac = get lgargc 0
1002for i ulong ac
1003if gt i 0
1004lgoc 44
1005end
1006for j ulong len LGK0X
1007lgoc get LGK0X j
1008end
1009lgohex get lgargs i
1010end
1011lgoc 93
1012lgoc 10
1013end
1014
1015fn lgfmtcc to void
1016for i ulong len LGKCC
1017lgoc get LGKCC i
1018end
1019t = get cctype 0
1020lgcctname t
1021for i ulong len LGKVER
1022lgoc get LGKVER i
1023end
1024lgodec get ccver 0
1025for i ulong len LGKHD
1026lgoc get LGKHD i
1027end
1028lgohex get cchdat 0
1029if eq t CCTRPT
1030for i ulong len LGKSRC
1031lgoc get LGKSRC i
1032end
1033lgohex get ccrsrc 0
1034for i ulong len LGKCSE
1035lgoc get LGKCSE i
1036end
1037lgohex get ccrcse 0
1038for i ulong len LGKEXT
1039lgoc get LGKEXT i
1040end
1041lgohex get ccrext 0
1042for i ulong len LGKTS
1043lgoc get LGKTS i
1044end
1045lgohex get ccrts 0
1046for i ulong len LGKRVR
1047lgoc get LGKRVR i
1048end
1049lgohex get ccrver 0
1050lgoc 10
1051ret
1052end
1053for i ulong len LGKCNT
1054lgoc get LGKCNT i
1055end
1056pn = get ccplen 0
1057lgodec pn
1058lgoc 10
1059end