5 %include "arch/x86_64/include/common.inc.asm"
9 ;KERNEL_BASE equ 0xFFFF800000000000
10 KERNEL_BASE equ 0xFFFFFFFF80000000
14 ; Multiboot macros to make a few lines later more readable
15 MULTIBOOT_PAGE_ALIGN equ 1<<0
16 MULTIBOOT_MEMORY_INFO equ 1<<1
17 MULTIBOOT_AOUT_KLUDGE equ 1<<16
18 MULTIBOOT_HEADER_MAGIC equ 0x1BADB002
19 MULTIBOOT_HEADER_FLAGS equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO; | MULTIBOOT_AOUT_KLUDGE
20 MULTIBOOT_CHECKSUM equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)
22 ; This is the GRUB Multiboot header. A boot signature
23 dd MULTIBOOT_HEADER_MAGIC
24 dd MULTIBOOT_HEADER_FLAGS
30 dd mboot ; Location of Multiboot Header
31 dd __load_addr ; Load address
32 dd __bss_start - KERNEL_BASE ; End of .data
33 dd gKernelEnd - KERNEL_BASE ; End of .bss (and kernel)
34 dd start - KERNEL_BASE ; Entrypoint
41 mov [gMultibootMagic - KERNEL_BASE], eax
42 mov [gMultibootPtr - KERNEL_BASE], ebx
44 ; Check for Long Mode support
47 cmp eax, 0x80000001 ; Compare the A-register with 0x80000001.
54 ; Enable PGE (Page Global Enable)
55 ; + PAE (Physical Address Extension)
56 ; + PSE (Page Size Extensions)
58 or eax, 0x80|0x20|0x10
62 mov eax, gInitialPML4 - KERNEL_BASE
66 ; (Also enables SYSCALL and NX)
69 or eax, (1 << 11)|(1 << 8)|(1 << 0) ; NXE, LME, SCE
74 or eax, 0x80010000 ; PG & WP
78 lgdt [gGDTPtr - KERNEL_BASE]
79 jmp 0x08:start64 - KERNEL_BASE
84 mov esi, csNot64BitCapable - KERNEL_BASE
103 dd 0x00000000, 0x00209A00 ; 0x08: 64-bit Code
104 dd 0x00000000, 0x00009200 ; 0x10: 64-bit Data
105 dd 0x00000000, 0x0040FA00 ; 0x18: 32-bit User Code
106 dd 0x00000000, 0x0040F200 ; 0x20: User Data
107 dd 0x00000000, 0x0020FA00 ; 0x28: 64-bit User Code
108 dd 0x00000000, 0x0000F200 ; 0x30: User Data (64 version)
109 times MAX_CPUS dd 0, 0x00008900, 0, 0 ; 0x38+16*n: TSS 0
114 [global gMultibootPtr]
115 [global gMultibootMagic]
122 [global gInitialPML4]
123 gInitialPML4: ; Covers 256 TiB (Full 48-bit Virtual Address Space)
124 dd gInitialPDP - KERNEL_BASE + 3, 0 ; Identity Map Low 4Mb
126 dd gStackPDP - KERNEL_BASE + 3, 0
127 times 512-4-($-gInitialPML4)/8 dq 0
128 dd gInitialPML4 - KERNEL_BASE + 3, 0 ; Fractal Mapping
131 dd gHighPDP - KERNEL_BASE + 3, 0 ; Map Low 4Mb to kernel base
133 gInitialPDP: ; Covers 512 GiB
134 dd gInitialPD - KERNEL_BASE + 3, 0
138 dd gStackPD - KERNEL_BASE + 3, 0
141 gHighPDP: ; Covers 512 GiB
143 ;dq 0 + 0x143 ; 1 GiB Page from zero
144 dd gInitialPD - KERNEL_BASE + 3, 0
147 gInitialPD: ; Covers 1 GiB
148 ; dq 0 + 0x143 ; 1 GiB Page from zero
149 dd gInitialPT1 - KERNEL_BASE + 3, 0
150 dd gInitialPT2 - KERNEL_BASE + 3, 0
154 dd gKStackPT - KERNEL_BASE + 3, 0
157 gKStackPT: ; Covers 2 MiB
158 ; Initial stack - 64KiB
161 %rep INITIAL_KSTACK_SIZE-1
162 dd gInitialKernelStack - KERNEL_BASE + i*0x1000 + 0x103, 0
165 times 512-INITIAL_KSTACK_SIZE dq 0
180 [global gInitialKernelStack]
182 times 0x1000*(INITIAL_KSTACK_SIZE-1) db 0 ; 8 Pages
186 db "Not 64-bit Capable",0