Repository navigation
Expand file tree
/
Copy pathkernel.asm
More file actions
1407 lines (1277 loc) · 36 KB
/
Copy pathkernel.asm
File metadata and controls
1407 lines (1277 loc) · 36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; ============================================================================
; os/kernel.asm — asm-llm OS v2.0 Bare-Metal Kernel, Boot Banner & REPL
;
; Boots directly on x86-64 hardware / QEMU KVM via Multiboot 1 (no OS, no libc):
; - Initializes COM1 Serial + 80x25 Color VGA + PIT/RDTSC Timer + SMP APIC
; - Parses Multiboot 1 cmdline & binds .asmbin Q8_0 model zero-copy from RAM
; - Displays color system & model boot banner
; - Runs batch cmdline prompt (with optional clean QEMU ACPI shutdown) or
; interactive bare-metal REPL console accepting both Serial and PS/2 input
; ============================================================================
bits 64
default rel
%include "constants.inc"
%include "os_constants.inc"
extern g_ctx
extern g_mb_magic
extern g_mb_info
extern g_has_avx2_fma
extern g_tsc_mhz
extern g_total_ram_mb
extern g_arena_used_mb
extern g_smp_total_cpus
extern g_smp_online_cpus
extern serial_init
extern serial_putc
extern vga_clear
extern vga_set_color
extern console_write
extern console_write_cstr
extern console_readline
extern timer_init
extern timer_ns
extern qemu_shutdown
extern mem_init
extern smp_init
extern tp_shutdown
extern model_init
extern transformer_forward
extern tok_build_chat_prompt
extern tok_write_stdout
extern argmax_f32
extern sample_top_p
extern str_len
extern str_eq
extern parse_u64
extern parse_f32
extern sys_write_all
extern write_cstr
extern write_u64
extern write_f64_2dp
extern write_hex64_raw
section .text
global kernel_main
; ============================================================================
; void kernel_main(void)
; ============================================================================
kernel_main:
push rbx
push rbp
push r12
push r13
push r14
push r15
; 1. Early hardware init: COM1 Serial + Color VGA + PIT/RDTSC Timer
call serial_init
mov dil, VGA_ATTR_GREY
call vga_clear
; Check AVX2 + FMA before executing any AVX instructions
cmp dword [rel g_has_avx2_fma], 0
je .err_no_avx2
call timer_init
call read_cpu_brand
; 2. Validate Multiboot 1 magic & extract module + cmdline
cmp dword [rel g_mb_magic], MB_BOOTLOADER_MAGIC
jne .err_multiboot
mov r12d, [rel g_mb_info] ; r12 = Multiboot Info struct (phys addr)
; Initialize default kernel options
mov byte [rel opt_prompt_buf], 0
mov dword [rel opt_has_prompt], 0
mov qword [rel opt_max_tok], 128
mov qword [rel opt_ctx_len], 2048
mov qword [rel opt_req_threads], 0 ; 0 = all online SMP cores
mov dword [rel opt_think], 0
mov dword [rel opt_raw], 0
mov dword [rel opt_single_turn], 0
mov dword [rel opt_shutdown], 0
mov dword [rel opt_print_toks], 0
mov qword [rel g_chat_pos], 0
mov dword [rel opt_temp], 0 ; 0.0f = greedy argmax
mov eax, 0x3F733333 ; 0.95f
mov [rel opt_topp], eax
mov qword [rel opt_rng], 42
; Copy & parse Multiboot cmdline if present (flags bit 2 = 0x04)
mov eax, [r12 + MBI_FLAGS]
test eax, 0x04
jz .no_cmdline
mov esi, [r12 + MBI_CMDLINE]
test esi, esi
jz .no_cmdline
lea rdi, [rel g_cmdline_buf]
mov ecx, CMDLINE_BUF_SIZE - 1
.copy_cmd:
mov al, [rsi]
mov [rdi], al
test al, al
jz .cmd_copied
inc rsi
inc rdi
dec ecx
jnz .copy_cmd
mov byte [rdi], 0
.cmd_copied:
call parse_cmdline
.no_cmdline:
; Locate .asmbin Multiboot module (flags bit 3 = 0x08, mods_count >= 1)
mov eax, [r12 + MBI_FLAGS]
test eax, 0x08
jz .err_no_module
mov eax, [r12 + MBI_MODS_COUNT]
test eax, eax
jz .err_no_module
mov eax, [r12 + MBI_MODS_ADDR] ; ptr to first mb_module_t
mov r13d, [rax + MB_MOD_START] ; r13 = mod_start
mov r14d, [rax + MB_MOD_END] ; r14 = mod_end
cmp r14, r13
jbe .err_no_module
mov [rel g_mod_start], r13
mov [rel g_mod_end], r14
mov r15, r14
sub r15, r13 ; r15 = mod_size
mov [rel g_mod_size], r15
; Verify ASML magic in physical RAM before anything else
cmp dword [r13 + HDR_MAGIC], ASMBIN_MAGIC
jne .err_bad_magic
; 3. Initialize bare-metal physical memory bump allocator
mov rdi, r12
mov rsi, r13
mov rdx, r14
call mem_init
test rax, rax
js .err_oom
; 4. Boot all SMP AP cores via Local APIC INIT-SIPI-SIPI + 16-bit trampoline
mov rdi, [rel opt_req_threads]
call smp_init
mov [rel g_active_cores], rax
; 5. Bind Qwen3-0.6B model zero-copy & allocate KV-cache / scratch arena
mov rdi, r13 ; mod_start
mov rsi, r15 ; mod_size
mov rdx, [rel opt_ctx_len] ; max_seq_override
call model_init
test rax, rax
js .err_model
; Capture boot-ready timestamp (from _start to model+SMP ready)
call timer_ns
mov [rel tm_boot_ready], rax
; 6. Print colored Bare-Metal OS Boot Banner (80-column clean)
call print_boot_banner
; 7. Batch mode (if prompt= was supplied via kernel cmdline)
cmp dword [rel opt_has_prompt], 0
je .enter_repl
mov dil, VGA_ATTR_YELLOW
call vga_set_color
mov edi, 1
lea rsi, [rel msg_prompt_lbl]
call write_cstr
mov dil, VGA_ATTR_WHITE
call vga_set_color
mov edi, 1
lea rsi, [rel opt_prompt_buf]
call write_cstr
mov edi, 1
lea rsi, [rel msg_nl]
call write_cstr
lea rdi, [rel opt_prompt_buf]
mov esi, 1 ; show boot-to-1st-token timing
call run_inference
cmp dword [rel opt_shutdown], 0
je .enter_repl
call tp_shutdown
call qemu_shutdown
; 8. Interactive Bare-Metal REPL Console (COM1 Serial + PS/2 Keyboard)
.enter_repl:
mov dil, VGA_ATTR_GREY
call vga_set_color
mov edi, 1
lea rsi, [rel msg_repl_help]
call write_cstr
.repl_loop:
mov dil, VGA_ATTR_GREEN
call vga_set_color
mov edi, 1
lea rsi, [rel msg_repl_prompt]
call write_cstr
mov dil, VGA_ATTR_WHITE
call vga_set_color
lea rdi, [rel g_line_buf]
mov rsi, LINE_BUF_SIZE
call console_readline
test rax, rax
js .do_halt ; Ctrl-D EOF
; Trim leading spaces
lea r12, [rel g_line_buf]
.trim_lead:
mov al, [r12]
cmp al, ' '
je .adv_lead
cmp al, 9
je .adv_lead
jmp .trim_tail
.adv_lead:
inc r12
jmp .trim_lead
.trim_tail:
mov rdi, r12
call str_len
test rax, rax
jz .repl_loop ; empty line
lea rcx, [r12 + rax - 1]
.tt_lp:
cmp rcx, r12
jb .chk_cmd
mov al, [rcx]
cmp al, ' '
je .tt_chop
cmp al, 9
je .tt_chop
jmp .chk_cmd
.tt_chop:
mov byte [rcx], 0
dec rcx
jmp .tt_lp
.chk_cmd:
; /halt, /quit, exit
mov rdi, r12
lea rsi, [rel cmd_halt]
call str_eq
test eax, eax
jnz .do_halt
mov rdi, r12
lea rsi, [rel cmd_quit]
call str_eq
test eax, eax
jnz .do_halt
mov rdi, r12
lea rsi, [rel cmd_exit]
call str_eq
test eax, eax
jnz .do_halt
; /clear, /new, /reset — reset multi-turn KV-cache history
mov rdi, r12
lea rsi, [rel cmd_clear]
call str_eq
test eax, eax
jnz .do_clear
mov rdi, r12
lea rsi, [rel cmd_new]
call str_eq
test eax, eax
jnz .do_clear
mov rdi, r12
lea rsi, [rel cmd_reset]
call str_eq
test eax, eax
jnz .do_clear
; /think
mov rdi, r12
lea rsi, [rel cmd_think]
call str_eq
test eax, eax
jz .chk_nothink
mov dword [rel opt_think], 1
mov dil, VGA_ATTR_CYAN
call vga_set_color
mov edi, 1
lea rsi, [rel msg_think_on]
call write_cstr
jmp .repl_loop
.do_clear:
mov qword [rel g_chat_pos], 0
mov dil, VGA_ATTR_CYAN
call vga_set_color
mov edi, 1
lea rsi, [rel msg_chat_cleared]
call write_cstr
lea rax, [rel g_ctx]
mov edi, 1
mov rsi, [rax + CTX_MAX_SEQ]
call write_u64
mov edi, 1
lea rsi, [rel msg_close_nl]
call write_cstr
jmp .repl_loop
.chk_nothink:
mov rdi, r12
lea rsi, [rel cmd_nothink]
call str_eq
test eax, eax
jz .chk_bench
mov dword [rel opt_think], 0
mov dil, VGA_ATTR_CYAN
call vga_set_color
mov edi, 1
lea rsi, [rel msg_think_off]
call write_cstr
jmp .repl_loop
.chk_bench:
mov rdi, r12
lea rsi, [rel cmd_bench]
call str_eq
test eax, eax
jz .chk_help
mov r13, [rel opt_max_tok]
mov r14d, [rel opt_single_turn]
mov qword [rel opt_max_tok], 64
mov dword [rel opt_single_turn], 1
mov qword [rel g_chat_pos], 0
lea rdi, [rel bench_prompt]
xor esi, esi
call run_inference
mov [rel opt_max_tok], r13
mov [rel opt_single_turn], r14d
mov qword [rel g_chat_pos], 0
jmp .repl_loop
.chk_help:
mov rdi, r12
lea rsi, [rel cmd_help]
call str_eq
test eax, eax
jz .run_user_prompt
mov dil, VGA_ATTR_GREY
call vga_set_color
mov edi, 1
lea rsi, [rel msg_repl_help]
call write_cstr
jmp .repl_loop
.run_user_prompt:
mov rdi, r12
xor esi, esi
call run_inference
jmp .repl_loop
.do_halt:
mov dil, VGA_ATTR_YELLOW
call vga_set_color
mov edi, 1
lea rsi, [rel msg_poweroff]
call write_cstr
call tp_shutdown
call qemu_shutdown
; Error handlers
.err_no_avx2:
lea rdi, [rel msg_err_avx2]
jmp .fatal
.err_multiboot:
lea rdi, [rel msg_err_mb]
jmp .fatal
.err_no_module:
lea rdi, [rel msg_err_mod]
jmp .fatal
.err_bad_magic:
lea rdi, [rel msg_err_magic]
jmp .fatal
.err_oom:
lea rdi, [rel msg_err_oom]
jmp .fatal
.err_model:
lea rdi, [rel msg_err_model]
.fatal:
push rdi
mov dil, VGA_ATTR_RED
call vga_set_color
pop rdi
call console_write_cstr
call tp_shutdown
call qemu_shutdown
; ============================================================================
; void run_inference(const char *prompt in rdi, int show_boot_ttft in esi)
; Exact same Qwen3-0.6B BPE + prefill + streaming decode loop as Phase 1!
; ============================================================================
run_inference:
push rbx
push rbp
push r12
push r13
push r14
push r15
sub rsp, 8 ; 6 pushes + 8 + ret(8) = 64 -> 16B aligned
mov [rel cur_show_ttft], esi
lea rbp, [rel g_ctx]
; Build prompt tokens via pure-asm BPE + Qwen3 chat template
mov rsi, [rbp + CTX_BUF_PROMPT]
mov rdx, MAX_PROMPT_TOKENS
mov ecx, [rel opt_raw]
mov r8d, [rel opt_think]
call tok_build_chat_prompt
mov r12, rax ; r12 = n_prompt
test r12, r12
jz .done
; Multi-turn KV-cache management
cmp dword [rel opt_single_turn], 0
je .chk_ctx_room
mov qword [rel g_chat_pos], 0
jmp .ctx_room_ok
.chk_ctx_room:
mov rax, [rel g_chat_pos]
test rax, rax
jz .ctx_room_ok
lea rcx, [rax + r12 + 64]
cmp rcx, [rbp + CTX_MAX_SEQ]
jb .ctx_room_ok
mov qword [rel g_chat_pos], 0
mov dil, VGA_ATTR_YELLOW
call vga_set_color
mov edi, 1
lea rsi, [rel msg_ctx_full]
call write_cstr
.ctx_room_ok:
mov rax, [rbp + CTX_MAX_SEQ]
cmp r12, rax
jbe .prompt_len_ok
mov r12, rax
.prompt_len_ok:
; Optional PROMPT_TOKENS output for automated exact verification
cmp dword [rel opt_print_toks], 0
je .do_prefill
mov edi, 1
lea rsi, [rel msg_ptoks]
call write_cstr
mov rdi, [rbp + CTX_BUF_PROMPT]
mov rsi, r12
call write_tok_csv
; --- PREFILL (incremental from pos = g_chat_pos, preserving KV-cache!) ---
.do_prefill:
call timer_ns
mov [rel tm_prefill_start], rax
mov r13, [rel g_chat_pos] ; pos = g_chat_pos
xor ebx, ebx ; i = 0
mov r14, [rbp + CTX_BUF_PROMPT]
.prefill_lp:
cmp rbx, r12
jae .prefill_done
mov edi, [r14 + rbx*4]
mov rsi, r13
lea rax, [rbx + 1]
xor edx, edx
cmp rax, r12
sete dl ; compute_logits = (i == n_prompt - 1)
call transformer_forward
inc rbx
inc r13
jmp .prefill_lp
.prefill_done:
call timer_ns
mov [rel tm_prefill_end], rax
; Sample first generated token
call pick_next_token
mov r14, rax ; r14 = next_tok
; Capture exact boot-to-first-token timestamp
call timer_ns
mov [rel tm_first_tok], rax
; --- DECODE (streaming directly to COM1 UTF-8 + VGA bright white!) ---
mov dil, VGA_ATTR_WHITE
call vga_set_color
call timer_ns
mov [rel tm_decode_start], rax
mov dword [rsp], 0 ; last_tok_unfed = 0
xor r15d, r15d ; n_gen = 0
mov rbx, [rel opt_max_tok]
.decode_lp:
cmp r15, rbx
jae .decode_done
cmp r14, [rbp + CTX_EOS_1]
je .decode_done
cmp r14, [rbp + CTX_EOS_2]
je .decode_done
mov rax, [rbp + CTX_BUF_GEN]
mov [rax + r15*4], r14d
inc r15
mov rdi, r14
call tok_write_stdout
cmp r15, rbx
jae .decode_done_unfed
cmp r13, [rbp + CTX_MAX_SEQ]
jae .decode_done
mov rdi, r14
mov rsi, r13
mov edx, 1
call transformer_forward
inc r13
call pick_next_token
mov r14, rax
jmp .decode_lp
.decode_done_unfed:
mov dword [rsp], 1 ; r14 was emitted on max_tok hit, not yet forwarded
.decode_done:
call timer_ns
mov [rel tm_decode_end], rax
mov edi, 1
lea rsi, [rel msg_nl]
call write_cstr
; Close assistant turn in KV-cache for seamless Qwen3 multi-turn continuity
cmp dword [rel opt_single_turn], 0
jne .reset_chat_after
; If decode stopped at max_tokens, feed the last emitted token into KV-cache
cmp dword [rsp], 0
je .close_eot
cmp r13, [rbp + CTX_MAX_SEQ]
jae .save_chat_pos
mov rdi, r14
mov rsi, r13
xor edx, edx ; compute_logits = 0
call transformer_forward
inc r13
.close_eot:
; Feed <|im_end|> (151645) and \n (198) so next turn's <|im_start|>user\n aligns exact
cmp dword [rel opt_raw], 0
jne .save_chat_pos
lea rax, [r13 + 2]
cmp rax, [rbp + CTX_MAX_SEQ]
jae .save_chat_pos
mov edi, TOK_IM_END
mov rsi, r13
xor edx, edx
call transformer_forward
inc r13
mov edi, TOK_NL
mov rsi, r13
xor edx, edx
call transformer_forward
inc r13
.save_chat_pos:
mov [rel g_chat_pos], r13
jmp .after_kv_close
.reset_chat_after:
mov qword [rel g_chat_pos], 0
.after_kv_close:
; Optional GEN_TOKENS output for automated exact verification
cmp dword [rel opt_print_toks], 0
je .print_stats
mov edi, 1
lea rsi, [rel msg_gtoks]
call write_cstr
mov rdi, [rbp + CTX_BUF_GEN]
mov rsi, r15
call write_tok_csv
; --- PERFORMANCE SUMMARY (green on VGA) ---
.print_stats:
mov dil, VGA_ATTR_GREEN
call vga_set_color
; Prefill line
mov edi, 1
lea rsi, [rel msg_prefill]
call write_cstr
mov edi, 1
mov rsi, r12
call write_u64
mov edi, 1
lea rsi, [rel msg_in]
call write_cstr
mov rax, [rel tm_prefill_end]
sub rax, [rel tm_prefill_start]
test rax, rax
jnz .pf_nz
mov rax, 1
.pf_nz:
vcvtsi2sd xmm0, xmm0, rax
vmovsd [rel tm_tmp], xmm0
vdivsd xmm0, xmm0, [rel d_1e6]
mov edi, 1
call write_f64_2dp
mov edi, 1
lea rsi, [rel msg_ms_open]
call write_cstr
vcvtsi2sd xmm0, xmm0, r12
vmulsd xmm0, xmm0, [rel d_1e9]
vdivsd xmm0, xmm0, [rel tm_tmp]
mov edi, 1
call write_f64_2dp
mov edi, 1
lea rsi, [rel msg_tps_end]
call write_cstr
; Decode line
mov edi, 1
lea rsi, [rel msg_decode]
call write_cstr
mov edi, 1
mov rsi, r15
call write_u64
mov edi, 1
lea rsi, [rel msg_in]
call write_cstr
mov rax, [rel tm_decode_end]
sub rax, [rel tm_decode_start]
test rax, rax
jnz .dec_nz
mov rax, 1
.dec_nz:
vcvtsi2sd xmm0, xmm0, rax
vmovsd [rel tm_tmp], xmm0
vdivsd xmm0, xmm0, [rel d_1e6]
mov edi, 1
call write_f64_2dp
mov edi, 1
lea rsi, [rel msg_ms_open]
call write_cstr
mov rax, r15
cmp rax, 1
jbe .dec_cnt_ok
dec rax ; exact number of decode forward passes
.dec_cnt_ok:
vcvtsi2sd xmm0, xmm0, rax
vmulsd xmm0, xmm0, [rel d_1e9]
vdivsd xmm0, xmm0, [rel tm_tmp]
mov edi, 1
call write_f64_2dp
mov edi, 1
lea rsi, [rel msg_tps_cores]
call write_cstr
mov edi, 1
mov rsi, [rel g_active_cores]
call write_u64
mov edi, 1
lea rsi, [rel msg_ctx_lbl]
call write_cstr
mov edi, 1
mov rsi, r13
call write_u64
mov edi, 1
lea rsi, [rel msg_slash]
call write_cstr
mov edi, 1
mov rsi, [rbp + CTX_MAX_SEQ]
call write_u64
mov edi, 1
lea rsi, [rel msg_nl]
call write_cstr
; Optional boot-to-first-token line (batch mode)
cmp dword [rel cur_show_ttft], 0
je .done
mov dil, VGA_ATTR_CYAN
call vga_set_color
mov edi, 1
lea rsi, [rel msg_boot_ttft]
call write_cstr
mov rax, [rel tm_boot_ready]
vcvtsi2sd xmm0, xmm0, rax
vdivsd xmm0, xmm0, [rel d_1e6]
mov edi, 1
call write_f64_2dp
mov edi, 1
lea rsi, [rel msg_ttft_mid]
call write_cstr
mov rax, [rel tm_first_tok]
vcvtsi2sd xmm0, xmm0, rax
vdivsd xmm0, xmm0, [rel d_1e6]
mov edi, 1
call write_f64_2dp
mov edi, 1
lea rsi, [rel msg_ms_nl]
call write_cstr
.done:
add rsp, 8
pop r15
pop r14
pop r13
pop r12
pop rbp
pop rbx
ret
; ----------------------------------------------------------------------------
; uint64 pick_next_token(void) -> rax
; ----------------------------------------------------------------------------
pick_next_token:
lea rax, [rel g_ctx]
vmovss xmm0, [rel opt_temp]
vxorps xmm1, xmm1, xmm1
vucomiss xmm0, xmm1
ja .sample
mov rdi, [rax + CTX_BUF_LOGITS]
mov rsi, [rax + CTX_VOCAB_SIZE]
jmp argmax_f32
.sample:
mov rdi, [rax + CTX_BUF_LOGITS]
mov rsi, [rax + CTX_BUF_PROBS]
mov rdx, [rax + CTX_VOCAB_SIZE]
vmovss xmm1, [rel opt_topp]
lea rcx, [rel opt_rng]
jmp sample_top_p
; ----------------------------------------------------------------------------
; void write_tok_csv(const uint32 *toks in rdi, uint64 count in rsi)
; ----------------------------------------------------------------------------
write_tok_csv:
push rbx
push r12
push r13
mov r12, rdi
mov r13, rsi
xor ebx, ebx
.lp:
cmp rbx, r13
jae .done
test rbx, rbx
jz .num
mov edi, 1
lea rsi, [rel msg_comma]
mov edx, 1
call sys_write_all
.num:
mov edi, 1
mov esi, [r12 + rbx*4]
call write_u64
inc rbx
jmp .lp
.done:
mov edi, 1
lea rsi, [rel msg_nl]
call write_cstr
pop r13
pop r12
pop rbx
ret
; ============================================================================
; Boot Banner Printer (80-column clean, colored on VGA + crisp text on COM1)
; ============================================================================
print_boot_banner:
push rbx
; Start banner on a clean line on COM1 serial (keep VGA at row 0!)
mov al, 10
call serial_putc
mov dil, VGA_ATTR_CYAN
call vga_set_color
mov edi, 1
lea rsi, [rel ban_line]
call write_cstr
mov dil, VGA_ATTR_WHITE
call vga_set_color
mov edi, 1
lea rsi, [rel ban_title]
call write_cstr
mov dil, VGA_ATTR_CYAN
call vga_set_color
mov edi, 1
lea rsi, [rel ban_line]
call write_cstr
; CPU line
mov dil, VGA_ATTR_YELLOW
call vga_set_color
mov edi, 1
lea rsi, [rel ban_cpu]
call write_cstr
mov dil, VGA_ATTR_WHITE
call vga_set_color
mov edi, 1
lea rsi, [rel g_cpu_brand]
call write_cstr
mov dil, VGA_ATTR_GREY
call vga_set_color
mov edi, 1
lea rsi, [rel ban_avx_tsc]
call write_cstr
mov edi, 1
mov rsi, [rel g_tsc_mhz]
call write_u64
mov edi, 1
lea rsi, [rel ban_mhz_nl]
call write_cstr
; SMP line
mov dil, VGA_ATTR_YELLOW
call vga_set_color
mov edi, 1
lea rsi, [rel ban_smp]
call write_cstr
mov dil, VGA_ATTR_GREEN
call vga_set_color
mov edi, 1
mov rsi, [rel g_active_cores]
call write_u64
mov edi, 1
lea rsi, [rel msg_slash]
call write_cstr
mov edi, 1
mov esi, [rel g_smp_total_cpus]
call write_u64
mov dil, VGA_ATTR_GREY
call vga_set_color
mov edi, 1
lea rsi, [rel ban_smp_tail]
call write_cstr
; RAM + Boot line
mov dil, VGA_ATTR_YELLOW
call vga_set_color
mov edi, 1
lea rsi, [rel ban_ram]
call write_cstr
mov dil, VGA_ATTR_WHITE
call vga_set_color
mov edi, 1
mov rsi, [rel g_total_ram_mb]
call write_u64
mov dil, VGA_ATTR_GREY
call vga_set_color
mov edi, 1
lea rsi, [rel ban_ram_mid]
call write_cstr
mov edi, 1
mov rsi, [rel g_arena_used_mb]
call write_u64
mov edi, 1
lea rsi, [rel ban_boot_lbl]
call write_cstr
mov rax, [rel tm_boot_ready]
vcvtsi2sd xmm0, xmm0, rax
vdivsd xmm0, xmm0, [rel d_1e6]
mov edi, 1
call write_f64_2dp
mov edi, 1
lea rsi, [rel msg_ms_nl]
call write_cstr
; Model line
mov dil, VGA_ATTR_YELLOW
call vga_set_color
mov edi, 1
lea rsi, [rel ban_model]
call write_cstr
mov dil, VGA_ATTR_WHITE
call vga_set_color
mov edi, 1
lea rsi, [rel ban_qwen]
call write_cstr
mov rax, [rel g_mod_size]
add rax, 524288
shr rax, 20 ; MiB
mov edi, 1
mov rsi, rax
call write_u64
mov edi, 1
lea rsi, [rel ban_mod_at]
call write_cstr
mov rdi, [rel g_mod_start]
call write_hex64_raw
mov dil, VGA_ATTR_GREY
call vga_set_color
mov edi, 1
lea rsi, [rel ban_mod_ctx]
call write_cstr
lea rax, [rel g_ctx]
mov edi, 1
mov rsi, [rax + CTX_MAX_SEQ]
call write_u64
mov edi, 1
lea rsi, [rel msg_close_nl]
call write_cstr
mov dil, VGA_ATTR_CYAN
call vga_set_color
mov edi, 1
lea rsi, [rel ban_line]
call write_cstr
mov dil, VGA_ATTR_GREY
call vga_set_color
pop rbx
ret
; ============================================================================
; CPUID Brand String Reader (leaves 0x80000002..0x80000004 -> g_cpu_brand)
; ============================================================================
read_cpu_brand:
push rbx
push r12
mov eax, 0x80000000
cpuid
cmp eax, 0x80000004
jb .fallback
lea r12, [rel cpu_brand_raw]
mov eax, 0x80000002
cpuid
mov [r12 + 0], eax
mov [r12 + 4], ebx
mov [r12 + 8], ecx
mov [r12 + 12], edx
mov eax, 0x80000003
cpuid
mov [r12 + 16], eax
mov [r12 + 20], ebx
mov [r12 + 24], ecx
mov [r12 + 28], edx
mov eax, 0x80000004
cpuid
mov [r12 + 32], eax
mov [r12 + 36], ebx
mov [r12 + 40], ecx
mov [r12 + 44], edx
mov byte [r12 + 48], 0
; Trim leading & trailing spaces into g_cpu_brand (max 38 chars for 80-col VGA)
mov rsi, r12
.skip_sp:
cmp byte [rsi], ' '
jne .copy
inc rsi
jmp .skip_sp
.copy:
lea rdi, [rel g_cpu_brand]
xor ecx, ecx
.cpy_lp:
mov al, [rsi + rcx]
test al, al