Kernel/x86_64 - Fixed Kernel not using COW, cleanup
[tpg/acess2.git] / 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 long/compatability mode
66         mov ecx, 0xC0000080
67         rdmsr
68         or ax, 0x100
69         wrmsr
70
71         ; Enable paging
72         mov eax, cr0
73         or eax, 0x80010000      ; PG & WP
74         mov cr0, eax
75
76         ; Load GDT
77         lgdt [gGDTPtr - KERNEL_BASE]
78         jmp 0x08:start64 - KERNEL_BASE
79
80 .not64bitCapable:
81         mov ah, 0x0F
82         mov edi, 0xB8000
83         mov esi, csNot64BitCapable - KERNEL_BASE
84
85 .loop:
86         lodsb
87         test al, al
88         jz .hlt
89         stosw
90         jmp .loop
91         
92 .hlt:
93         jmp .hlt
94
95 [section .data]
96 [global gGDT]
97 [global gGDTPtr]
98 gGDT:
99         dd      0,0
100         dd      0x00000000, 0x00209A00  ; 0x08: 64-bit Code
101         dd      0x00000000, 0x00009200  ; 0x10: 64-bit Data
102         dd      0x00000000, 0x0040FA00  ; 0x18: 32-bit User Code
103         dd      0x00000000, 0x0040F200  ; 0x20: User Data
104         dd      0x00000000, 0x0020FA00  ; 0x28: 64-bit User Code
105         dd      0x00000000, 0x0000F200  ; 0x30: User Data (64 version)
106         times MAX_CPUS  dd      0, 0x00008900, 0, 0     ; 0x38+16*n: TSS 0
107 gGDTPtr:
108         dw      $-gGDT-1
109         dd      gGDT-KERNEL_BASE
110         dd      0
111 [global gMultibootPtr]
112 [global gMultibootMagic]
113 gMultibootMagic:
114         dd      0
115 gMultibootPtr:
116         dd      0
117
118 [section .padata]
119 [global gInitialPML4]
120 gInitialPML4:   ; Covers 256 TiB (Full 48-bit Virtual Address Space)
121         dd      gInitialPDP - KERNEL_BASE + 3, 0        ; Identity Map Low 4Mb
122         times 0xA0*2-1  dq      0
123         dd      gStackPDP - KERNEL_BASE + 3, 0
124         times 512-4-($-gInitialPML4)/8  dq      0
125         dd      gInitialPML4 - KERNEL_BASE + 3, 0       ; Fractal Mapping
126         dq      0
127         dq      0
128         dd      gHighPDP - KERNEL_BASE + 3, 0   ; Map Low 4Mb to kernel base
129
130 gInitialPDP:    ; Covers 512 GiB
131         dd      gInitialPD - KERNEL_BASE + 3, 0
132         times 511       dq      0
133
134 gStackPDP:
135         dd      gStackPD - KERNEL_BASE + 3, 0
136         times 511       dq      0
137
138 gHighPDP:       ; Covers 512 GiB
139         times 510       dq      0
140         ;dq     0 + 0x143       ; 1 GiB Page from zero
141         dd      gInitialPD - KERNEL_BASE + 3, 0
142         dq      0
143
144 gInitialPD:     ; Covers 1 GiB
145 ;       dq      0 + 0x143       ; 1 GiB Page from zero
146         dd      gInitialPT1 - KERNEL_BASE + 3, 0
147         dd      gInitialPT2 - KERNEL_BASE + 3, 0
148         times 510       dq      0
149
150 gStackPD:
151         dd      gKStackPT - KERNEL_BASE + 3, 0
152         times 511       dq      0
153
154 gKStackPT:      ; Covers 2 MiB
155         ; Initial stack - 64KiB
156         dq      0
157         %assign i 0
158         %rep INITIAL_KSTACK_SIZE-1
159         dd      gInitialKernelStack - KERNEL_BASE + i*0x1000 + 0x103, 0
160         %assign i i+1
161         %endrep
162         times 512-INITIAL_KSTACK_SIZE   dq 0
163 gInitialPT1:    ; 2 MiB
164         %assign i 0
165         %rep 512
166         dq      i*4096+0x103
167         %assign i i+1
168         %endrep
169 gInitialPT2:    ; 2 MiB
170         %assign i 512
171         %rep 512
172         dq      i*4096+0x103
173         %assign i i+1
174         %endrep
175
176 [section .padata]
177 [global gInitialKernelStack]
178 gInitialKernelStack:
179         times 0x1000*(INITIAL_KSTACK_SIZE-1)    db 0    ; 8 Pages
180
181 [section .rodata]
182 csNot64BitCapable:
183         db "Not 64-bit Capable",0
184
185 ; vim: ft=nasm

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