DEV Community

Jaipal001
Jaipal001

Posted on

64-bit bootloader from scratch in AT&T assembly, Part-3

.code32  # 32-bit mode
entry32:
    # Proper 32-bit segment initialization
    movw $(gdt_data_segment - gdt_start), %ax
    movw %ax, %ds
    movw %ax, %es
    movw %ax, %ss

    # 1. Setup Paging Structures
    mov $0x1000, %edi
    mov %edi, %cr3
    xor %eax, %eax
    mov $4096, %ecx
    rep stosl   # idk what this is

    mov %cr3, %edi
    movl $0x2003, (%edi)         # PML4 -> PDPT
    movl $0x3003, 0x1000(%edi)    # PDPT -> PD
    movl $0x00000083, 0x2000(%edi)# PD -> 2MB Huge Page (Identity map 0-2MB)

    # 2. Enable PAE (FIXED)
    mov %cr4, %eax
    or $(1 << 5), %eax
    mov %eax, %cr4               # Write back to register

    # 3. Enable Long Mode via EFER
    mov $0xC0000080, %ecx
    rdmsr
    or $(1 << 8), %eax           # LME Bit
    wrmsr

    # 4. Enable Paging
    mov %cr0, %eax
    or $(1 << 31), %eax          # PG Bit
    mov %eax, %cr0

    # 5. Jump to 64-bit
    lgdt gdt64_ptr
    ljmp $0x08, $entry64         # jump to 64-bit / long mode
Enter fullscreen mode Exit fullscreen mode

Top comments (0)