Entering Long Mode Directly: Difference between revisions
Jump to navigation
Jump to search
[unchecked revision] | [unchecked revision] |
Content deleted Content added
No edit summary |
|||
Line 9:
cli
Line 29:
mov ax,0x020D ;ah = Function 0x02 ;al = Number of sectors
Line 92:
;Enter long mode
mov eax,10100000b
mov cr4,eax
mov edx, 0x0000a000
mov cr3,edx
mov ecx,0xC0000080
rdmsr
or eax,0x00000100
wrmsr
mov ebx,cr0
or ebx,0x80000001
mov cr0,ebx
lgdt [gdt.pointer]
jmp gdt.code:startLongMode
dq 0x0000000000000000 ;Null Descriptor
Line 133:
times 510-($-$$) db 0
dw 0xAA55
Line 146:
startLongMode:
cli
mov edi,0x00b8000
mov rax,0x0720077407750750
mov [edi],rax
|
Revision as of 19:50, 6 October 2009
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] 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" ;Reference to Intel System Programming Manual Part 1 - 2.1.1.1 ;Fill boot sector times 510-($-$$) db 0 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 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 $