Entering Long Mode Directly

From OSDev.wiki
Jump to navigation Jump to search

Demo Code

The following NASM code demonstrates how to boot into 64 bit mode without entering protected mode:

[ORG 0x00007C00]
[BITS 16]



boot_loader:
;Parameter from BIOS: dl = boot drive


;Set default state

cli
xor bx,bx
mov es,bx
mov fs,bx
mov gs,bx
mov ds,bx
mov ss,bx
mov sp,0x7C00
sti

jmp 0:.clear_cs
.clear_cs:


;Load kernel from floppy disk

mov ax,0x020D					;ah = Function 0x02 ;al = Number of sectors
mov bx,startLongMode				;es:bx = Destination
mov cx,0x0002					;cx = Cylinder and sector
xor dh,dh					;dx = Head and drive number
int 0x13					;Int 0x13 Function 0x02 (Load sectors)


;Enable A20 via port 92h

in al,92h
or al,02h
out 92h,al


;Build page tables
;The page tables will look like this:
;PML4:
;dq 0x000000000000b00f = 00000000 00000000 00000000 00000000 00000000 00000000 10010000 00001111
;times 511 dq 0x0000000000000000

;PDP:
;dq 0x000000000000c00f = 00000000 00000000 00000000 00000000 00000000 00000000 10100000 00001111
;times 511 dq 0x0000000000000000

;PD:
;dq 0x000000000000018f = 00000000 00000000 00000000 00000000 00000000 00000000 00000001 10001111
;times 511 dq 0x0000000000000000

;This defines one 2MB page at the start of memory, so we can access the first 2MBs as if paging was disabled

xor bx,bx
mov es,bx
cld
mov di,0xa000

mov ax,0xb00f
stosw

xor ax,ax
mov cx,0x07ff
rep stosw

mov ax,0xc00f
stosw

xor ax,ax
mov cx,0x07ff
rep stosw

mov ax,0x018f
stosw

xor ax,ax
mov cx,0x07ff
rep stosw


;Enter long mode

mov eax,10100000b				;Set PAE and PGE
mov cr4,eax

mov edx, 0x0000a000				;Point CR3 at PML4
mov cr3,edx

mov ecx,0xC0000080				;Specify EFER MSR

rdmsr						;Enable Long Mode
or eax,0x00000100
wrmsr

mov ebx,cr0					;Activate long mode
or ebx,0x80000001				;by enabling paging and protection simultaneously
mov cr0,ebx					;skipping protected mode entirely

lgdt [gdt.pointer]				;load 80-bit gdt.pointer below

jmp gdt.code:startLongMode			;Load CS with 64 bit segment and flush the instruction cache



;Global Descriptor Table
gdt:
dq 0x0000000000000000				;Null Descriptor

.code equ $ - gdt
dq 0x0020980000000000                   

.data equ $ - gdt
dq 0x0000900000000000                   

.pointer:
dw $-gdt-1					;16-bit Size (Limit)
dq gdt						;64-bit Base Address
						;Changed from "dd gdt"
						;Ref: Intel System Programming Manual V1 - 2.1.1.1


times 510-($-$$) db 0				;Fill boot sector
dw 0xAA55					;Boot loader signature


[BITS 64]

startLongMode:

cli						;Interupts are disabled because no IDT has been set up

mov edi,0x00b8000				;Display:"Put long mode kernel here.__"
mov rax,0x0720077407750750                      ;at the top left corner of screen
mov [edi],rax
mov rax,0x0767076e076f076c
mov [edi+8],rax
mov rax,0x0764076f076d0720
mov [edi+16],rax
mov rax,0x0765076b07200765
mov [edi+24],rax
mov rax,0x076c0765076e0772
mov [edi+32],rax
mov rax,0x0772076507680720
mov [edi+40],rax
mov rax,0x07200720072e0765
mov [edi+48],rax

jmp $						;Hang the system

Notes about the code

  1. This code should work fine if being run as MBR (with all segment registers set to zero)
  2. As noted in code comments, only 2MB is mapped
  3. The code doesn't check CPU type, so x86 CPU (ie .486 or before) won't fit to run

See Also

Related forum thread