Skip to content

Instantly share code, notes, and snippets.

@superaxander
Last active July 3, 2021 19:44
Show Gist options
  • Save superaxander/a9cbd35a9833009ee5c14e5611322d85 to your computer and use it in GitHub Desktop.
Save superaxander/a9cbd35a9833009ee5c14e5611322d85 to your computer and use it in GitHub Desktop.
; Time to enter long mode
xchg bx, bx
; First disable paging
mov eax, cr0
and eax, 01111111111111111111111111111111b ; Clear the PG-bit, which is bit 31.
mov cr0, eax
; Clear page tables
mov edi, 0x1000 ; Set the destination index to 0x1000.
mov cr3, edi ; Set control register 3 to the destination index.
xor eax, eax ; Nullify the A-register.
mov ecx, 4096 ; Set the C-register to 4096.
rep stosd ; Clear the memory.
mov edi, cr3 ; Set the destination index to control register 3.
mov DWORD [edi], 0x2003 ; Set the uint32_t at the destination index to 0x2003.
add edi, 0x1000 ; Add 0x1000 to the destination index.
mov DWORD [edi], 0x3003 ; Set the uint32_t at the destination index to 0x3003.
add edi, 0x1000 ; Add 0x1000 to the destination index.
mov DWORD [edi], 0x83 ; 2 MiB RW @ 0x0
add edi, 8 ; Move over 8 bytes
mov DWORD [edi], 0x200083 ; 2 MiB RW @ 0x200000
; Identity map the first 4 MiB
; Switch to PSE and only map a single page?
; mov ebx, 0b00000011 ; Set the B-register to 0b10000011.
; mov ecx, 2 ; Set the C-register to 2.
;.SetEntry:
; mov DWORD [edi], ebx ; Set the uint32_t at the destination index to the B-register.
; add ebx, 0x200000 ; Add 0x200000 to the B-register.
; add edi, 8 ; Add eight to the destination index.
; loop .SetEntry ; Set the next entry.
; Enable PAE paging
mov eax, cr4 ; Set the A-register to control register 4.
or eax, 1 << 5 ; Set the PAE-bit, which is the 6th bit (bit 5).
; PSE is on by default for PAE
or eax, 1 << 4 ; Set the PSE-bit, which is the 2nd bit (bit 1).
mov cr4, eax ; Set control register 4 to the A-register.
; Set LongMode bit
mov ecx, 0xC0000080 ; Set the C-register to 0xC0000080, which is the EFER MSR.
rdmsr ; Read from the model-specific register.
or eax, 1 << 8 ; Set the LM-bit which is the 9th bit (bit 8).
wrmsr ; Write to the model-specific register.
; Enable paging
mov eax, cr0 ; Set the A-register to control register 0.
or eax, 1 << 31 ; Set the PG-bit, which is the 32nd bit (bit 31).
mov cr0, eax ; Set control register 0 to the A-register.
; Load 64-bit gdt
lgdt [gdt64.Pointer] ; Load the 64-bit global descriptor table.
jmp gdt64.Code:entry64 ; Set the code segment and enter 64-bit long mode.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment