From a0161286a6f76056f366bfe89db7bb278815a675 Mon Sep 17 00:00:00 2001 From: Ian Seyler Date: Tue, 17 Mar 2026 10:07:36 -0400 Subject: [PATCH 1/4] Add debug flag for build. --- src/pure64.asm | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/pure64.asm b/src/pure64.asm index a55bce7..1249a58 100644 --- a/src/pure64.asm +++ b/src/pure64.asm @@ -268,10 +268,11 @@ start64: ; Configure serial port @ 0x03F8 as 115200 8N1 call init_serial - + +%ifdef DEBUG mov rsi, msg_pure64 ; Output "[ Pure64 ]" call debug_msg - + ; Output boot method mov rsi, msg_boot call debug_msg @@ -284,6 +285,7 @@ boot_uefi: mov rsi, msg_uefi call debug_msg msg_boot_done: +%endif ; Clear out the first 20KiB of memory. This will store the 64-bit IDT, GDT, PML4, PDP Low, and PDP High mov ecx, 5120 @@ -745,11 +747,15 @@ pde_end: call debug_block %endif +%ifdef DEBUG mov rsi, msg_acpi call debug_msg +%endif call init_acpi ; Find and process the ACPI tables +%ifdef DEBUG mov rsi, msg_ok call debug_msg +%endif %ifndef NOVIDEO ; Visual Debug (4/8) @@ -757,11 +763,15 @@ pde_end: call debug_block %endif +%ifdef DEBUG mov rsi, msg_bsp call debug_msg +%endif call init_cpu ; Configure the BSP CPU +%ifdef DEBUG mov rsi, msg_ok call debug_msg +%endif %ifndef NOVIDEO ; Visual Debug (5/8) @@ -769,12 +779,16 @@ pde_end: call debug_block %endif +%ifdef DEBUG ; Configure system timer mov rsi, msg_timer call debug_msg +%endif call init_timer ; Configure the timer +%ifdef DEBUG mov rsi, msg_ok call debug_msg +%endif %ifndef NOVIDEO ; Visual Debug (6/8) @@ -782,11 +796,15 @@ pde_end: call debug_block %endif +%ifdef DEBUG mov rsi, msg_smp call debug_msg +%endif call init_smp ; Init of SMP, deactivate interrupts +%ifdef DEBUG mov rsi, msg_ok call debug_msg +%endif ; Reset the stack to the proper location (was set to 0x8000 previously) mov rsi, [p_LocalAPICAddress] ; We would call p_smp_get_id here but the stack is not ... @@ -945,8 +963,10 @@ lfb_wc_end: call debug_block %endif +%ifdef DEBUG mov rsi, msg_kernel call debug_msg +%endif %ifdef FLOPPY cmp byte [p_BootDisk], 'F' ; Check if sys is booted from floppy? @@ -1073,6 +1093,7 @@ debug_progressbar: %endif +%ifdef DEBUG ; ----------------------------------------------------------------------------- ; debug_msg_char - Send a single char via the serial port ; IN: AL = Byte to send @@ -1171,6 +1192,7 @@ debug_dump_al_l: pop rax ; Restore RAX ret ; ----------------------------------------------------------------------------- +%endif EOF: From 74f8dacfe68e9873fd76594576608a8b86a81dbe Mon Sep 17 00:00:00 2001 From: Ian Seyler Date: Tue, 17 Mar 2026 10:20:14 -0400 Subject: [PATCH 2/4] Exclude strings on non-debug build --- src/sysvar.asm | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/sysvar.asm b/src/sysvar.asm index 3ad1ae7..af8779a 100644 --- a/src/sysvar.asm +++ b/src/sysvar.asm @@ -6,6 +6,7 @@ ; ============================================================================= +%ifdef DEBUG ; Messages msg_pure64: db 13, 10, 13, 10, '[ Pure64 ]', 0 msg_ok: db 'ok', 0 @@ -20,6 +21,7 @@ msg_bsp: db 13, 10, 'bsp ', 0 msg_timer: db 13, 10, 'timer ', 0 msg_smp: db 13, 10, 'smp ', 0 msg_kernel: db 13, 10, 'kernel start', 13, 10, 0 +%endif ;CONFIG cfg_smpinit: db 1 ; By default SMP is enabled. Set to 0 to disable. From ed1f5c7b5b76c7cd24e2fcbf57ad53b212f56d90 Mon Sep 17 00:00:00 2001 From: Ian Seyler Date: Tue, 17 Mar 2026 10:37:23 -0400 Subject: [PATCH 3/4] Binary size reductions - Use 32-bit registers where possible - Rework exception gate setup into a loop (saved ~160 bytes) --- src/interrupt.asm | 1 + src/pure64.asm | 51 +++++++++++++++++++---------------------------- 2 files changed, 21 insertions(+), 31 deletions(-) diff --git a/src/interrupt.asm b/src/interrupt.asm index 62ea9cc..0897f80 100644 --- a/src/interrupt.asm +++ b/src/interrupt.asm @@ -55,6 +55,7 @@ spurious: ; handler for spurious interrupts ; ----------------------------------------------------------------------------- ; CPU Exception Gates +align 16 exception_gate_00: mov al, 0x00 jmp exception_gate_main diff --git a/src/pure64.asm b/src/pure64.asm index 1249a58..8e211bb 100644 --- a/src/pure64.asm +++ b/src/pure64.asm @@ -387,7 +387,7 @@ skip1GB: lgdt [GDTR64] ; Point cr3 at PML4 - mov rax, 0x00002008 ; Write-thru enabled (Bit 3) + mov eax, 0x00002008 ; Write-thru enabled (Bit 3) mov cr3, rax xor eax, eax ; aka r0 @@ -431,9 +431,9 @@ clearcs64: ; Build the IDT xor edi, edi ; create the 64-bit IDT (at linear address 0x0000000000000000) - mov rcx, 32 + mov ecx, 32 make_exception_gates: ; make gates for exception handlers - mov rax, exception_gate + mov eax, exception_gate push rax ; save the exception gate to the stack for later use stosw ; store the low word (15:0) of the address mov ax, SYS64_CODE_SEL @@ -445,14 +445,14 @@ make_exception_gates: ; make gates for exception handlers stosw ; store the high word (31:16) of the address shr rax, 16 stosd ; store the extra high dword (63:32) of the address. - xor rax, rax + xor eax, eax stosd ; reserved - dec rcx + dec ecx jnz make_exception_gates - mov rcx, 256-32 + mov ecx, 256-32 make_interrupt_gates: ; make gates for the other interrupts - mov rax, interrupt_gate + mov eax, interrupt_gate push rax ; save the interrupt gate to the stack for later use stosw ; store the low word (15:0) of the address mov ax, SYS64_CODE_SEL @@ -466,33 +466,22 @@ make_interrupt_gates: ; make gates for the other interrupts stosd ; store the extra high dword (63:32) of the address. xor eax, eax stosd ; reserved - dec rcx + dec ecx jnz make_interrupt_gates ; Set up the exception gates for all of the CPU exceptions - ; The following code depends on exception gates being below 16MB - mov word [0x00*16], exception_gate_00 ; #DE - mov word [0x01*16], exception_gate_01 ; #DB - mov word [0x02*16], exception_gate_02 - mov word [0x03*16], exception_gate_03 ; #BP - mov word [0x04*16], exception_gate_04 ; #OF - mov word [0x05*16], exception_gate_05 ; #BR - mov word [0x06*16], exception_gate_06 ; #UD - mov word [0x07*16], exception_gate_07 ; #NM - mov word [0x08*16], exception_gate_08 ; #DF - mov word [0x09*16], exception_gate_09 ; #MF - mov word [0x0A*16], exception_gate_10 ; #TS - mov word [0x0B*16], exception_gate_11 ; #NP - mov word [0x0C*16], exception_gate_12 ; #SS - mov word [0x0D*16], exception_gate_13 ; #GP - mov word [0x0E*16], exception_gate_14 ; #PF - mov word [0x0F*16], exception_gate_15 - mov word [0x10*16], exception_gate_16 ; #MF - mov word [0x11*16], exception_gate_17 ; #AC - mov word [0x12*16], exception_gate_18 ; #MC - mov word [0x13*16], exception_gate_19 ; #XM - mov word [0x14*16], exception_gate_20 ; #VE - mov word [0x15*16], exception_gate_21 ; #CP + ; The following code depends on: + ; - Exception gates being below 16MB + ; - Each exception_gate_XX being exactly 4 bytes apart + mov eax, exception_gate_00 ; Address of first handler + xor edi, edi ; Clear EDI as IDT starts at 0x0000 + mov cl, 22 ; 22 exception gates (0x00-0x15) +set_exception_gate: + mov [rdi], ax ; Patch low word of handler address in IDT entry + add edi, 16 ; Advance to next IDT entry (16 bytes each) + add eax, 4 ; Advance to next gate handler (4 bytes each) + dec cl + jnz set_exception_gate lidt [IDTR64] ; load IDT register From eb4761d5d2b2c0177276d1898e90fb36853ac9f5 Mon Sep 17 00:00:00 2001 From: Ian Seyler Date: Tue, 17 Mar 2026 11:27:58 -0400 Subject: [PATCH 4/4] Build NOVIDEO as 4KiB binary --- src/pure64.asm | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/pure64.asm b/src/pure64.asm index 8e211bb..91d61ec 100644 --- a/src/pure64.asm +++ b/src/pure64.asm @@ -20,7 +20,11 @@ BITS 64 ORG 0x00008000 DEFAULT ABS +%ifdef NOVIDEO +PURE64SIZE equ 4096 ; Pad Pure64 to this length +%else PURE64SIZE equ 6144 ; Pad Pure64 to this length +%endif start: jmp bootmode ; This command will be overwritten with 'NOP's before the AP's are started