TODO
[tpg/acess2.git] / KernelLand / Kernel / arch / x86_64 / start32.asm
1 ;
2 ; Acess2 x86_64 port
3 ;
4
5 %include "arch/x86_64/include/common.inc.asm"
6
7 [BITS 32]
8
9 ;KERNEL_BASE    equ     0xFFFF800000000000
10 KERNEL_BASE     equ     0xFFFFFFFF80000000
11
12 [section .multiboot]
13 mboot:
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)
21         
22         ; This is the GRUB Multiboot header. A boot signature
23         dd MULTIBOOT_HEADER_MAGIC
24         dd MULTIBOOT_HEADER_FLAGS
25         dd MULTIBOOT_CHECKSUM
26         [extern __load_addr]
27         [extern __bss_start]
28         [extern gKernelEnd]
29         ; a.out kludge
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
35
36 [extern start64]
37
38 [section .text]
39 [global start]
40 start:
41         mov [gMultibootMagic - KERNEL_BASE], eax
42         mov [gMultibootPtr - KERNEL_BASE], ebx
43
44         ; Check for Long Mode support
45         mov eax, 0x80000000
46         cpuid
47         cmp eax, 0x80000001     ; Compare the A-register with 0x80000001.
48         mov eax, 0x80000001
49         cpuid
50         jb .not64bitCapable
51         test edx, 1<<29
52         jz .not64bitCapable
53
54         ; Enable PGE (Page Global Enable)
55         ; + PAE (Physical Address Extension)
56         ; + PSE (Page Size Extensions)
57         mov eax, cr4
58         or eax, 0x80|0x20|0x10
59         mov cr4, eax
60
61         ; Load PDP4
62         mov eax, gInitialPML4 - KERNEL_BASE
63         mov cr3, eax
64
65         ; Enable IA-32e mode
66         ; (Also enables SYSCALL and NX)
67         mov ecx, 0xC0000080
68         rdmsr
69         or eax, (1 << 11)|(1 << 8)|(1 << 0)     ; NXE, LME, SCE
70         wrmsr
71
72         ; Enable paging
73         mov eax, cr0
74         or eax, 0x80010000      ; PG & WP
75         mov cr0, eax
76
77         ; Load GDT
78         lgdt [gGDTPtr - KERNEL_BASE]
79         jmp 0x08:start64 - KERNEL_BASE
80
81 .not64bitCapable:
82         mov ah, 0x0F
83         mov edi, 0xB8000
84         mov esi, csNot64BitCapable - KERNEL_BASE
85
86 .loop:
87         lodsb
88         test al, al
89         jz .hlt
90         stosw
91         jmp .loop
92         
93 .hlt:
94         cli
95         hlt
96         jmp .hlt
97
98 [section .data]
99 [global gGDT]
100 [global gGDTPtr]
101 gGDT:
102         dd      0,0
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
110 gGDTPtr:
111         dw      $-gGDT-1
112         dd      gGDT-KERNEL_BASE
113         dd      0
114 [global gMultibootPtr]
115 [global gMultibootMagic]
116 gMultibootMagic:
117         dd      0
118 gMultibootPtr:
119         dd      0
120
121 [section .padata]
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
125         times 0xA0*2-1  dq      0
126         dd      gStackPDP - KERNEL_BASE + 3, 0
127         times 512-4-($-gInitialPML4)/8  dq      0
128         dd      gInitialPML4 - KERNEL_BASE + 3, 0       ; Fractal Mapping
129         dq      0
130         dq      0
131         dd      gHighPDP - KERNEL_BASE + 3, 0   ; Map Low 4Mb to kernel base
132
133 gInitialPDP:    ; Covers 512 GiB
134         dd      gInitialPD - KERNEL_BASE + 3, 0
135         times 511       dq      0
136
137 gStackPDP:
138         dd      gStackPD - KERNEL_BASE + 3, 0
139         times 511       dq      0
140
141 gHighPDP:       ; Covers 512 GiB
142         times 510       dq      0
143         ;dq     0 + 0x143       ; 1 GiB Page from zero
144         dd      gInitialPD - KERNEL_BASE + 3, 0
145         dq      0
146
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
151         times 510       dq      0
152
153 gStackPD:
154         dd      gKStackPT - KERNEL_BASE + 3, 0
155         times 511       dq      0
156
157 gKStackPT:      ; Covers 2 MiB
158         ; Initial stack - 64KiB
159         dq      0
160         %assign i 0
161         %rep INITIAL_KSTACK_SIZE-1
162         dd      gInitialKernelStack - KERNEL_BASE + i*0x1000 + 0x103, 0
163         %assign i i+1
164         %endrep
165         times 512-INITIAL_KSTACK_SIZE   dq 0
166 gInitialPT1:    ; 2 MiB
167         %assign i 0
168         %rep 512
169         dq      i*4096+0x103
170         %assign i i+1
171         %endrep
172 gInitialPT2:    ; 2 MiB
173         %assign i 512
174         %rep 512
175         dq      i*4096+0x103
176         %assign i i+1
177         %endrep
178
179 [section .padata]
180 [global gInitialKernelStack]
181 gInitialKernelStack:
182         times 0x1000*(INITIAL_KSTACK_SIZE-1)    db 0    ; 8 Pages
183
184 [section .rodata]
185 csNot64BitCapable:
186         db "Not 64-bit Capable",0
187
188 ; vim: ft=nasm

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