x86_64 starts without erroring, reaches high mem and full 64-bit
[tpg/acess2.git] / Kernel / arch / x86_64 / start32.asm
1
2 [BITS 32]
3
4 KERNEL_BASE     equ     0xFFFF800000000000
5
6 [section .multiboot]
7 mboot:
8         ; Multiboot macros to make a few lines later more readable
9         MULTIBOOT_PAGE_ALIGN    equ 1<<0
10         MULTIBOOT_MEMORY_INFO   equ 1<<1
11         MULTIBOOT_AOUT_KLUDGE   equ 1<<16
12         MULTIBOOT_HEADER_MAGIC  equ 0x1BADB002
13         MULTIBOOT_HEADER_FLAGS  equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO; | MULTIBOOT_AOUT_KLUDGE
14         MULTIBOOT_CHECKSUM      equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)
15         
16         ; This is the GRUB Multiboot header. A boot signature
17         dd MULTIBOOT_HEADER_MAGIC
18         dd MULTIBOOT_HEADER_FLAGS
19         dd MULTIBOOT_CHECKSUM
20         [extern __load_addr]
21         [extern __bss_start]
22         [extern gKernelEnd]
23         ; a.out kludge
24         dd mboot        ; Location of Multiboot Header
25         dd __load_addr  ; Load address
26         dd __bss_start - KERNEL_BASE    ; End of .data
27         dd gKernelEnd - KERNEL_BASE     ; End of .bss (and kernel)
28         dd start - KERNEL_BASE  ; Entrypoint
29
30 [extern start64]
31
32 [section .text]
33 [global start]
34 start:
35         ; Check for Long Mode support
36         mov eax, 0x80000000
37         cpuid
38         cmp eax, 0x80000001     ; Compare the A-register with 0x80000001.
39         mov eax, 0x80000001
40         cpuid
41     jb .not64bitCapable
42         test edx, 1<<29
43         jz .not64bitCapable
44
45         ; Enable PAE
46         mov eax, cr4
47         or eax, 0x80|0x20
48         mov cr4, eax
49
50         ; Load PDP4
51         mov eax, gInitialPML4 - KERNEL_BASE
52         mov cr3, eax
53
54         ; Enable long/compatability mode
55         mov ecx, 0xC0000080
56         rdmsr
57         or ax, 0x100
58         wrmsr
59
60         ; Enable paging
61         mov eax, cr0
62         or eax, 0x80000000
63         mov cr0, eax
64
65         ; Load GDT
66         lgdt [gGDTPtr - KERNEL_BASE]
67         jmp 0x08:start64 - KERNEL_BASE
68
69 .not64bitCapable:
70         mov ah, 0x0F
71         mov edi, 0xB8000
72         mov esi, csNot64BitCapable - KERNEL_BASE
73
74 .loop:
75         lodsb
76         test al, al
77         jz .hlt
78         stosw
79         jmp .loop
80         
81 .hlt:
82         jmp .hlt
83
84 [section .data]
85 [global gGDT]
86 gGDT:
87         dd      0,0
88         dd      0x00000000, 0x00209800  ; 0x08: 64-bit Code
89         dd      0x00000000, 0x00009000  ; 0x10: 64-bit Data
90         dd      0x00000000, 0x00209800  ; 0x18: 64-bit User Code
91         dd      0x00000000, 0x00209000  ; 0x20: 64-bit User Data
92         dd      0x00000000, 0x00209800  ; 0x38: 32-bit User Code
93         dd      0x00000000, 0x00209000  ; 0x30: 32-bit User Data
94         times MAX_CPUS  dd      0, 0, 0, 0      ; 0x38+16*n: TSS 0
95 gGDTPtr:
96         dw      $-gGDT-1
97         dd      gGDT-KERNEL_BASE
98         dd      0
99
100 [section .padata]
101 [global gInitialPML4]
102 gInitialPML4:   ; Covers 256 TiB (Full 48-bit Virtual Address Space)
103         dd      gInitialPDP - KERNEL_BASE + 3, 0        ; Identity Map Low 4Mb
104         times 256-1 dq  0
105         dd      gInitialPDP - KERNEL_BASE + 3, 0        ; Map Low 4Mb to kernel base
106         times 256-1-2 dq 0
107         dd      gInitialPML4 - KERNEL_BASE + 3, 0       ; Fractal Mapping
108         dq      0
109
110 gInitialPDP:    ; Covers 512 GiB
111         dd      gInitialPD - KERNEL_BASE + 3, 0
112         times 511       dq      0
113
114 gInitialPD:     ; Covers 1 GiB
115         dd      gInitialPT1 - KERNEL_BASE + 3, 0
116         dd      gInitialPT2 - KERNEL_BASE + 3, 0
117         times 510       dq      0
118
119 gInitialPT1:    ; Covers 2 MiB
120         %assign i 0
121         %rep 512
122         dq      i*4096+3
123         %assign i i+1
124         %endrep
125 gInitialPT2:    ; 2 MiB
126         %assign i 512
127         %rep 512
128         dq      i*4096+3
129         %assign i i+1
130         %endrep
131
132
133 [section .rodata]
134 csNot64BitCapable:
135         db "Not 64-bit Capable",0

UCC git Repository :: git.ucc.asn.au