Feat: Finnaly in 32-bits

This commit is contained in:
AfonsoCMSousa 2025-11-18 20:28:27 +00:00
parent 751c1f527d
commit af903b190a
5 changed files with 113 additions and 88 deletions

BIN
SoraOS

Binary file not shown.

181
boot.asm
View File

@ -1,101 +1,122 @@
; File: boot.asm ; File: boot.asm
global _start [org 0x7c00] ; BIOS loads us at 0x7C00
extern kmain [bits 16] ; We start in 16-bit Real Mode
section .multiboot start:
; Multiboot header mov bp, 0x7c00
dd 0x1BADB002 ; magic number mov sp, bp
dd 0x00000003 ; flags
dd -(0x1BADB002 + 0x00000003) ; checksum
call clear_screen
section .text mov si, real_msg
[bits 32] call print_string
_start:
mov esp, kernel_stack_top
cli ; Clear interrupts
; Load the gdt_ptr cli
lgdt [gdt_ptr]
mov eax, pml4_table lgdt [gdt_descriptor]
mov cr3, eax ; Load the PML4 table into CR3
mov eax, cr0
; Link PML4 -> PDPT or eax, 0x1
mov dword [pml4_table], pdpt_table + 0x3
mov dword [pml4_table + 4], 0x0
; Link PDPT -> PD
mov dword [pdpt_table], pd_table + 0x3
mov dword [pdpt_table + 4], 0x0
; Link PD -> 2MB Physical Page 0
mov dword [pd_table], 0x83
mov dword [pd_table + 4], 0x0
mov eax, cr4
or eax, 0x20 ; Enable PAE
mov cr4, eax
mov ecx, 0xC0000080 ; IA32_EFER MSR
rdmsr
or eax, 0x00000100 ; Set LME (Long Mode Enable) bit
wrmsr
; Enable long mode
mov eax, cr0
or eax, 0x80000000 ; Set the Long Mode Enable (LME) bit
mov cr0, eax mov cr0, eax
; Jump to 64-bit code Segment ; Far jump into 32-bit mode.
jmp 0x08:long_mode_start jmp 0x08:init_pm
section .bss ; --- Helper: Print String (BIOS INT 0x10) ---
; We define the stack size and labels in the uninitialized data section (.bss) print_string:
kernel_stack_bottom: pusha
; Reserve 16 KB for the stack mov ah, 0x0e ; INT 0x10 "Teletype Output" function
resb 16 * 1024 .loop:
kernel_stack_top: lodsb ; Load byte at [SI] into AL, increment SI
cmp al, 0 ; Check for null terminator
je .done
int 0x10 ; Call BIOS video interrupt
jmp .loop
.done:
popa
ret
; --- Helper: Clear Screen ---
clear_screen:
pusha
mov ah, 0x00 ; Set Video Mode function
mov al, 0x03 ; 80x25 Text Mode
int 0x10
popa
ret
pml4_table: real_msg: db '[REAL] - Loaded successfully.', 0x0D, 0x0A, 0
; Define a simple PML4 table here (identity mapping for simplicity)
; In a real kernel, you would set up proper paging structures
resb 4096 ; Reserve 4 KB for the PML4 table
pml4_end:
pdpt_table: gdt_start:
resb 4096 ; Reserve 4 KB for the PDPT table
pdpt_end:
pd_table: gdt_null: ; 8 bytes of zeros
resb 4096 ; Reserve 4 KB for the PD table dd 0x0
pd_end: dd 0x0
gdt_code: ; Code Segment (0x08)
; Base=0, Limit=0xFFFFF, Access=0x9A, Flags=0xC
dw 0xffff ; Limit (bits 0-15)
dw 0x0 ; Base (bits 0-15)
db 0x0 ; Base (bits 16-23)
db 10011010b ; Access Byte (0x9A)
db 11001111b ; Flags (0xC) + Limit (bits 16-19)
db 0x0 ; Base (bits 24-31)
gdt_data: ; Data Segment (0x10)
; Base=0, Limit=0xFFFFF, Access=0x92, Flags=0xC
dw 0xffff
dw 0x0
db 0x0
db 10010010b ; Access Byte (0x92)
db 11001111b ; Flags (0xC) + Limit
db 0x0
section .data
gdt_ptr:
dw (gdt_end - gdt_start - 1) ; Limit (set by linker/code later)
dq gdt_start ; Base Address (set by linker/code later)
gdt_start: ; Start of the actual GDT entries
; Entry 0: Null Descriptor
dq 0x0000000000000000
dq 0x00AF9A000000FFFF
dq 0x00AF92000000FFFF
gdt_end: gdt_end:
section .text gdt_descriptor:
[bits 64] ; Tell NASM to assemble in 64-bit mode dw gdt_end - gdt_start - 1 ; Size (Limit)
dd gdt_start ; Start Address
long_mode_start: ; ==================================================
; 1. Pass GRUB's Multiboot info (in ebx) as the first C argument (in rdi) ; 32-BIT PROTECTED MODE
; We must use the 64-bit register RDI, but the data is in the 32-bit EBX. ; ==================================================
; Moving ebx to rdi automatically clears the upper 32 bits of rdi. [bits 32]
mov rdi, rbx init_pm:
; 5. Update Segment Registers
; Now that we are in 32-bit, we must point all segment registers
; to our new Data Segment (0x10)
mov ax, 0x10
mov ds, ax
mov ss, ax
mov es, ax
mov fs, ax
mov gs, ax
; 2. Call the C kernel's main function mov ebp, 0x90000 ; Update stack to a safe 32-bit area
call kmain mov esp, ebp
; 3. If kmain ever returns, halt the CPU ; 6. Print "SoraOS Protected" directly to Video Memory
hlt ; We can't use BIOS interrupts anymore! We must write to 0xB8000.
mov ebx, 0xb8000
mov byte [ebx], 'S'
mov byte [ebx+1], 0x0f ; White on Black
mov byte [ebx+2], 'O'
mov byte [ebx+3], 0x0f
mov byte [ebx+4], 'R'
mov byte [ebx+5], 0x0f
mov byte [ebx+6], 'A'
mov byte [ebx+7], 0x0f
mov byte [ebx+8], ' '
mov byte [ebx+9], 0x0f
mov byte [ebx+10], 'P'
mov byte [ebx+11], 0x0f
mov byte [ebx+12], 'M'
mov byte [ebx+13], 0x0f
jmp $
; --- The Magic Boot Sector Footer ---
; Fill the rest of the 512 bytes with zeros
times 510-($-$$) db 0
; The BIOS signature (must be at the very end)
dw 0xAA55

View File

@ -1,8 +1,12 @@
echo ">>> Compiling bootloader" #!/bin/sh
nasm boot.asm -o boot.o -f elf32 export PATH=/usr/local/cross/bin:$PATH
echo ">>> Compiling kernel" # 1. Assemble raw binary
gcc kernel.c -o kernel.o -c -m32 -ffreestanding -nostdlib -no-pie echo ">>> Assembling boot.asm to boot.bin..."
nasm -f bin boot.asm -o boot.bin
echo ">>> Linking kernel" # 2. Run QEMU as a raw disk drive
gcc -m32 -nostdlib -no-pie -T linker.ld -o SoraOS boot.o kernel.o echo ">>> Running QEMU with boot.bin..."
qemu-system-x86_64 -drive format=raw,file=boot.bin
echo "Done!"

BIN
kernel.o

Binary file not shown.

View File

@ -1,4 +1,4 @@
OUTPUT_FORMAT(elf32-i386) OUTPUT_FORMAT(binary)
ENTRY(_start) ENTRY(_start)
SECTIONS SECTIONS
@ -21,7 +21,7 @@ SECTIONS
/* Define the .data section immediately after .text */ /* Define the .data section immediately after .text */
.data : .data :
{ {
/* Put all .data sections from all input files here */ /* Put all .data sections §from all input files here */
*(.data) *(.data)
} }