Files
AirlysOS/arch/x86_64/boot.s
T

242 lines
6.0 KiB
ArmAsm

/*
* Airlys Kernel Entry Point & Long-Mode Trampoline
* Supports both Multiboot 1 and Multiboot 2 specifications
* Transitions x86 CPU from 32-bit Protected Mode into 64-bit Long Mode
* Architecture: x86_64 | Aira Group of Technology
*/
.set ALIGN, 1<<0
.set MEMINFO, 1<<1
.set MB1_FLAGS, ALIGN | MEMINFO
.set MB1_MAGIC, 0x1BADB002
.set MB1_CHECKSUM, -(MB1_MAGIC + MB1_FLAGS)
/* Multiboot 2 Constants */
.set MB2_MAGIC, 0xE85250D6
.set MB2_ARCH, 0
.set MB2_LENGTH, (multiboot2_header_end - multiboot2_header)
.set MB2_CHECKSUM, -(MB2_MAGIC + MB2_ARCH + MB2_LENGTH)
/* ---------------- Multiboot Headers ---------------- */
.section .multiboot, "a"
.align 8
multiboot2_header:
.long MB2_MAGIC
.long MB2_ARCH
.long MB2_LENGTH
.long MB2_CHECKSUM
/* Entry Address Tag (Type 3) */
.align 8
.short 3 /* type = 3 (Entry address) */
.short 0 /* flags = 0 */
.long 12 /* size = 12 */
.long _start /* entry_addr */
.long 0 /* padding to 8-byte boundary */
/* Framebuffer Request Tag (Type 5) - Requests UEFI GOP / VBE native @ 32bpp */
.align 8
.short 5 /* type = 5 (Framebuffer) */
.short 1 /* flags = 1 (Optional - graceful fallback if exact 1080p unavailable) */
.long 20 /* size = 20 bytes */
.long 1920 /* width = 1920 (FHD Native for Acer Nitro V 15) */
.long 1080 /* height = 1080 (FHD Native for Acer Nitro V 15) */
.long 32 /* depth = 32bpp */
/* End tag */
.align 8
.short 0
.short 0
.long 8
multiboot2_header_end:
.align 4
multiboot1_header:
.long MB1_MAGIC
.long MB1_FLAGS
.long MB1_CHECKSUM
/* ---------------- 32-Bit Entry Point ---------------- */
.section .boot, "ax"
.code32
.global _start
.type _start, @function
_start:
/* Disable interrupts */
cli
/* Initialize stack */
movl $stack_top, %esp
/* Save Multiboot magic (EAX) and info pointer (EBX) in non-volatile registers */
movl %eax, %ebp
movl %ebx, %esi
/* Zero out ONLY the PML4 and PDPT page tables (8KB = 2048 dwords) */
movl $pml4_table, %edi
movl $2048, %ecx
xorl %eax, %eax
rep stosl
/* Restore Multiboot magic to EDI */
movl %ebp, %edi
/* Check if CPU supports CPUID */
pushfl
popl %eax
movl %eax, %ecx
xorl $(1 << 21), %eax
pushl %eax
popfl
pushfl
popl %eax
pushl %ecx
popfl
cmpl %ecx, %eax
je no_cpuid
/* Check if Long Mode is available (EFER.LME) */
movl $0x80000000, %eax
cpuid
cmpl $0x80000001, %eax
jb no_long_mode
movl $0x80000001, %eax
cpuid
testl $(1 << 29), %edx
jz no_long_mode
/* Build identity page tables for 512GB using 2MB huge pages */
/* PML4[0] -> PDPT */
movl $pml4_table, %eax
movl $pdpt_table, %ebx
orl $0x03, %ebx /* Present + Writable */
movl %ebx, (%eax)
movl $0, 4(%eax) /* Upper 32 bits = 0 */
/* PDPT[0..511] -> 512 Page Directories (512 * 1GB = 512GB) */
movl $pdpt_table, %eax
movl $page_dir_table, %ebx
orl $0x03, %ebx
movl $0, %ecx
init_pdpt_loop:
movl %ebx, (%eax, %ecx, 8)
movl $0, 4(%eax, %ecx, 8)
addl $4096, %ebx
incl %ecx
cmpl $512, %ecx
jne init_pdpt_loop
/* Identity map 262144 entries of 2MB = 512GB (0x00000000 .. 0x7FFFFFFFFF) */
movl $0, %ecx
map_pd_loop:
movl $0x200000, %eax /* 2MB */
mull %ecx /* edx:eax = ecx * 2MB */
orl $0x83, %eax /* Present + Writable + Huge Page (bit 7) */
movl $page_dir_table, %ebx
movl %eax, (%ebx, %ecx, 8)
movl %edx, 4(%ebx, %ecx, 8)
incl %ecx
cmpl $262144, %ecx
jne map_pd_loop
/* Load CR3 with physical address of PML4 */
movl $pml4_table, %eax
movl %eax, %cr3
/* Enable PAE (Physical Address Extension) in CR4 */
movl %cr4, %eax
orl $(1 << 5), %eax
movl %eax, %cr4
/* Set Long Mode Enable bit in EFER MSR (0xC0000080) */
movl $0xC0000080, %ecx
rdmsr
orl $(1 << 8), %eax
wrmsr
/* Load 64-bit Global Descriptor Table BEFORE enabling paging */
lgdt (gdt64_pointer)
/* Enable Paging and Protected Mode in CR0 */
movl %cr0, %eax
orl $(1 << 31 | 1 << 0), %eax
movl %eax, %cr0
/* Far jump to 64-bit code segment */
ljmp $0x08, $long_mode_entry
no_cpuid:
no_long_mode:
/* Write error to VGA and halt */
movl $0x4F524F45, 0xB8000 /* 'E' 'R' with red background */
cli
halt_loop:
hlt
jmp halt_loop
/* ---------------- 64-Bit Long Mode Entry ---------------- */
.code64
.global long_mode_entry
.type long_mode_entry, @function
long_mode_entry:
/* Reload GDT in 64-bit mode to guarantee 64-bit canonical GDTR base */
lgdt (gdt64_pointer)
/* Reload data segment registers */
movw $0x10, %ax
movw %ax, %ds
movw %ax, %es
movw %ax, %fs
movw %ax, %gs
movw %ax, %ss
/* Setup 64-bit stack pointer */
movq $stack_top, %rsp
/* Zero out RFLAGS */
pushq $0
popfq
/* Call Kernel Main with System V ABI arguments:
rdi = magic (was in edi)
rsi = multiboot info addr (was in esi)
*/
movl %edi, %edi
movl %esi, %esi
call kernel_main
/* Hang if kernel_main ever returns */
cli
hang_64:
hlt
jmp hang_64
/* ---------------- Page Tables (4KB Aligned) ---------------- */
.section .bss
.align 4096
pml4_table:
.skip 4096
pdpt_table:
.skip 4096
page_dir_table:
.skip 2097152 /* 512 Page Directories * 4096 = 2MB = 512GB */
/* Kernel Stack (16KB) */
.align 16
stack_bottom:
.skip 16384
stack_top:
/* ---------------- 64-Bit GDT ---------------- */
.section .rodata
.align 8
gdt64:
.quad 0 /* Null descriptor */
.quad (1<<41) | (1<<43) | (1<<44) | (1<<47) | (1<<53) /* 64-bit Code: Readable, Executable, Code/Data, Present, Long Mode */
.quad (1<<41) | (1<<44) | (1<<47) /* 64-bit Data: Writable, Code/Data, Present */
gdt64_pointer:
.short . - gdt64 - 1
.quad gdt64