Initial commit: SUB-OS 64-Bit Production Monolithic Operating System with LazyBox userland suite

This commit is contained in:
subhobhai943 committed 2026-08-16 21:23:52 +05:30
commit cd9c786d14
88 files changed
+7132

No files matched your search

+98
View File
@@ -0,0 +1,98 @@
BITS 16
ORG 0x7C00
start:
; Set up segments and stack
cli
xor ax, ax
mov ds, ax
mov es, ax
mov ss, ax
mov sp, 0x7C00
sti
; Save boot drive number from BIOS (DL)
mov [BOOT_DRIVE], dl
; Print greeting
mov si, MSG_BOOT
call print_string
; Check for LBA extensions (INT 13h, AH=41h)
mov ah, 0x41
mov bx, 0x55AA
mov dl, [BOOT_DRIVE]
int 0x13
jc load_chs
cmp bx, 0xAA55
jne load_chs
load_lba:
; Load stage 2 using LBA (INT 13h, AH=42h)
; Read 15 sectors starting from LBA 1 to 0x0000:0x7E00
mov ah, 0x42
mov dl, [BOOT_DRIVE]
mov si, dap
int 0x13
jc disk_error
jmp jump_stage2
load_chs:
; Fallback: Load stage 2 using CHS (INT 13h, AH=02h)
mov ah, 0x02
mov al, 15 ; 15 sectors (sectors 2-16)
mov ch, 0 ; Cylinder 0
mov dh, 0 ; Head 0
mov cl, 2 ; Sector 2
xor bx, bx
mov es, bx
mov bx, 0x7E00 ; ES:BX = 0x0000:0x7E00
mov dl, [BOOT_DRIVE]
int 0x13
jc disk_error
jump_stage2:
; Pass boot drive in DL to Stage 2
mov dl, [BOOT_DRIVE]
jmp 0x0000:0x7E00
disk_error:
mov si, MSG_DISK_ERR
call print_string
.halt:
cli
hlt
jmp .halt
; BIOS Teletype string print (SI = null-terminated string)
print_string:
pusha
mov ah, 0x0E
.loop:
lodsb
test al, al
jz .done
int 0x10
jmp .loop
.done:
popa
ret
; Data
BOOT_DRIVE db 0
MSG_BOOT db "SUB-OS Bootloader...", 13, 10, 0
MSG_DISK_ERR db "Boot Disk Error!", 13, 10, 0
; Disk Address Packet for LBA
align 4
dap:
db 0x10 ; DAP size (16 bytes)
db 0 ; Reserved
dw 15 ; Read 15 sectors
dw 0x7E00 ; Offset
dw 0x0000 ; Segment
dq 1 ; Starting LBA (Sector 1)
; Boot Sector Signature
times 510 - ($ - $$) db 0
dw 0xAA55
+43
View File
@@ -0,0 +1,43 @@
; Global Descriptor Table Definition
align 16
gdt_start:
; Null descriptor (0x00)
dq 0
; 32-bit Code Segment (0x08)
dw 0xFFFF ; Limit (low 16 bits)
dw 0x0000 ; Base (low 16 bits)
db 0x00 ; Base (next 8 bits)
db 10011010b ; Access byte (Present, Ring 0, Code/Data, Executable, Read/Write)
db 11001111b ; Flags (32-bit, 4KB granularity) and Limit (high 4 bits)
db 0x00 ; Base (high 8 bits)
; 32-bit Data Segment (0x10)
dw 0xFFFF ; Limit
dw 0x0000 ; Base
db 0x00 ; Base
db 10010010b ; Access byte (Present, Ring 0, Code/Data, Read/Write)
db 11001111b ; Flags and Limit
db 0x00 ; Base
; 64-bit Code Segment (0x18)
dw 0x0000 ; Limit (ignored)
dw 0x0000 ; Base
db 0x00 ; Base
db 10011010b ; Access byte (Present, Ring 0, Code/Data, Executable, Read/Write)
db 00100000b ; Flags (64-bit code segment, Long Mode) and Limit
db 0x00 ; Base
; 64-bit Data Segment (0x20)
dw 0x0000 ; Limit (ignored)
dw 0x0000 ; Base
db 0x00 ; Base
db 10010010b ; Access byte (Present, Ring 0, Code/Data, Read/Write)
db 00000000b ; Flags and Limit
db 0x00 ; Base
gdt_end:
gdt_descriptor:
dw gdt_end - gdt_start - 1 ; Size of GDT minus 1
dd gdt_start ; Base address of GDT
+272
View File
@@ -0,0 +1,272 @@
BITS 16
ORG 0x7E00
start2:
cli
; Ensure segment registers are zero
xor ax, ax
mov ds, ax
mov es, ax
mov ss, ax
mov sp, 0x7C00
sti
; Save boot drive number passed from stage 1 in DL
mov [boot_drive], dl
; Print Stage 2 startup message
mov si, msg_stage2_start
call print_string_16
; -------------------------------------------------------------
; Step 1: Enable A20 Line
; -------------------------------------------------------------
mov si, msg_a20
call print_string_16
in al, 0x92
test al, 2
jnz .a20_done
or al, 2
and al, 0xFE
out 0x92, al
.a20_done:
; -------------------------------------------------------------
; Step 2: Detect Memory via INT 15h, EAX=0xE820
; Destination: 0x9000 (count at 0x9000, entries at 0x9008)
; -------------------------------------------------------------
mov si, msg_mmap
call print_string_16
; Zero out count at 0x9000
mov dword [0x9000], 0
mov dword [0x9004], 0
mov di, 0x9008 ; Memory map entries destination
xor ebx, ebx ; Continuation value
xor bp, bp ; Entry counter
mov edx, 0x534D4150 ; 'SMAP' signature
mov eax, 0xE820
mov ecx, 24 ; Buffer size
int 0x15
jc .mmap_error
.mmap_loop:
inc bp
add di, 24
test ebx, ebx
jz .mmap_finished
mov edx, 0x534D4150
mov eax, 0xE820
mov ecx, 24
int 0x15
jc .mmap_finished
jmp .mmap_loop
.mmap_error:
; If E820 failed, create 1 fallback entry (128MB usable)
mov dword [0x9000], 1
mov dword [0x9004], 0
mov dword [0x9008], 0x100000 ; base = 1MB
mov dword [0x900C], 0
mov dword [0x9010], 0x07F00000 ; length = 127MB
mov dword [0x9014], 0
mov dword [0x9018], 1 ; type = 1 (USABLE)
mov dword [0x901C], 1
jmp .mmap_store_done
.mmap_finished:
mov word [0x9000], bp
mov word [0x9002], 0
mov dword [0x9004], 0
.mmap_store_done:
; -------------------------------------------------------------
; Step 3: Load Kernel from Disk to 0x10000 (Conventional RAM)
; 4 chunks x 64 sectors = 256 sectors (128 KB)
; -------------------------------------------------------------
mov si, msg_kernel_load
call print_string_16
mov word [kernel_dap_off], 0x0000
mov word [kernel_dap_seg], 0x1000
mov dword [kernel_dap_lba], 16
mov dword [kernel_dap_lba + 4], 0
mov cx, 4 ; 4 chunks * 64 sectors = 256 sectors (128 KB)
.kernel_chunk_loop:
push cx
mov ah, 0x42
mov dl, [boot_drive]
mov si, kernel_dap
int 0x13
jc .kernel_disk_error
; Advance LBA by 64 sectors
add dword [kernel_dap_lba], 64
; Advance Segment by 0x800 (64 sectors * 512 bytes = 32768 bytes = 0x800 paragraphs)
add word [kernel_dap_seg], 0x0800
pop cx
loop .kernel_chunk_loop
.kernel_loaded_ok:
; -------------------------------------------------------------
; Step 4: Set up Identity Paging for Long Mode (All 4 GB)
; -------------------------------------------------------------
; Clear 24 KB for 6 page tables (0x1000 - 0x6FFF)
mov ax, 0x0000
mov es, ax
mov di, 0x1000
mov cx, 6144
xor eax, eax
rep stosd
; PML4[0] -> PDPT at 0x2000
mov dword [0x1000], 0x2003
mov dword [0x1004], 0
; PDPT -> 4 PDs
mov dword [0x2000], 0x3003
mov dword [0x2004], 0
mov dword [0x2008], 0x4003
mov dword [0x200C], 0
mov dword [0x2010], 0x5003
mov dword [0x2014], 0
mov dword [0x2018], 0x6003
mov dword [0x201C], 0
; Map 4GB (2048 x 2MB pages)
mov di, 0x3000
mov eax, 0x00000083
mov cx, 2048
.map_4gb_loop:
mov [di], eax
mov dword [di + 4], 0
add eax, 0x200000
add di, 8
loop .map_4gb_loop
; -------------------------------------------------------------
; Step 5: Switch to 32-bit Protected Mode
; -------------------------------------------------------------
mov si, msg_entering_pm
call print_string_16
cli
lgdt [gdt_descriptor]
mov eax, cr0
or eax, 1
mov cr0, eax
jmp 0x08:pm_entry
.kernel_disk_error:
mov si, msg_kernel_err
call print_string_16
.halt16:
cli
hlt
jmp .halt16
print_string_16:
pusha
mov ah, 0x0E
.p_loop:
lodsb
test al, al
jz .p_done
int 0x10
jmp .p_loop
.p_done:
popa
ret
; -----------------------------------------------------------------
; 32-Bit Protected Mode Entry Point
; -----------------------------------------------------------------
BITS 32
pm_entry:
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
mov esp, 0x7C00
; Copy kernel from 0x10000 to 0x100000 (1MB mark) - 256 KB (512 sectors)
mov esi, 0x10000
mov edi, 0x100000
mov ecx, (512 * 512) / 4
rep movsd
; Enable PAE in CR4
mov eax, cr4
or eax, (1 << 5)
mov cr4, eax
; Load CR3
mov eax, 0x1000
mov cr3, eax
; Enable Long Mode in EFER MSR
mov ecx, 0xC0000080
rdmsr
or eax, (1 << 8)
wrmsr
; Enable Paging and Protected Mode
mov eax, cr0
or eax, (1 << 31) | (1 << 0)
mov cr0, eax
jmp 0x18:lm_entry
; -----------------------------------------------------------------
; 64-Bit Long Mode Entry Point
; -----------------------------------------------------------------
BITS 64
lm_entry:
mov ax, 0x20
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
; Arguments: RDI = 0x9008 (entries), RSI = count at 0x9000
mov rdi, 0x9008
mov rsi, [0x9000]
mov rax, 0x100000
jmp rax
; -----------------------------------------------------------------
; Data Section
; -----------------------------------------------------------------
BITS 16
align 4
boot_drive: db 0
msg_stage2_start: db "Stage 2: Loaded", 13, 10, 0
msg_a20: db "Stage 2: A20 enabled", 13, 10, 0
msg_mmap: db "Stage 2: Memory map detected", 13, 10, 0
msg_kernel_load: db "Stage 2: Loading kernel...", 13, 10, 0
msg_entering_pm: db "Stage 2: Jumping to 64-bit mode...", 13, 10, 0
msg_kernel_err: db "Stage 2: Kernel Disk Read Error!", 13, 10, 0
align 4
kernel_dap:
db 0x10
db 0
dw 64 ; 64 sectors per chunk (32 KB)
kernel_dap_off:
dw 0x0000
kernel_dap_seg:
dw 0x1000
kernel_dap_lba:
dq 16
%include "gdt.asm"