Kernel/x86_64 - Fucking about removing inline asm
[tpg/acess2.git] / Kernel / arch / x86_64 / start32.asm
1
2 [BITS 32]
3
4 ;KERNEL_BASE    equ     0xFFFF800000000000
5 KERNEL_BASE     equ     0xFFFFFFFF80000000
6
7 [section .multiboot]
8 mboot:
9         ; Multiboot macros to make a few lines later more readable
10         MULTIBOOT_PAGE_ALIGN    equ 1<<0
11         MULTIBOOT_MEMORY_INFO   equ 1<<1
12         MULTIBOOT_AOUT_KLUDGE   equ 1<<16
13         MULTIBOOT_HEADER_MAGIC  equ 0x1BADB002
14         MULTIBOOT_HEADER_FLAGS  equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO; | MULTIBOOT_AOUT_KLUDGE
15         MULTIBOOT_CHECKSUM      equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)
16         
17         ; This is the GRUB Multiboot header. A boot signature
18         dd MULTIBOOT_HEADER_MAGIC
19         dd MULTIBOOT_HEADER_FLAGS
20         dd MULTIBOOT_CHECKSUM
21         [extern __load_addr]
22         [extern __bss_start]
23         [extern gKernelEnd]
24         ; a.out kludge
25         dd mboot        ; Location of Multiboot Header
26         dd __load_addr  ; Load address
27         dd __bss_start - KERNEL_BASE    ; End of .data
28         dd gKernelEnd - KERNEL_BASE     ; End of .bss (and kernel)
29         dd start - KERNEL_BASE  ; Entrypoint
30
31 [extern start64]
32
33 [section .text]
34 [global start]
35 start:
36         mov [gMultibootMagic - KERNEL_BASE], eax
37         mov [gMultibootPtr - KERNEL_BASE], ebx
38
39         ; Check for Long Mode support
40         mov eax, 0x80000000
41         cpuid
42         cmp eax, 0x80000001     ; Compare the A-register with 0x80000001.
43         mov eax, 0x80000001
44         cpuid
45         jb .not64bitCapable
46         test edx, 1<<29
47         jz .not64bitCapable
48
49         ; Enable PGE (Page Global Enable)
50         ; + PAE (Physical Address Extension)
51         ; + PSE (Page Size Extensions)
52         mov eax, cr4
53         or eax, 0x80|0x20|0x10
54         mov cr4, eax
55
56         ; Load PDP4
57         mov eax, gInitialPML4 - KERNEL_BASE
58         mov cr3, eax
59
60         ; Enable long/compatability mode
61         mov ecx, 0xC0000080
62         rdmsr
63         or ax, 0x100
64         wrmsr
65
66         ; Enable paging
67         mov eax, cr0
68         or eax, 0x80000000
69         mov cr0, eax
70
71         ; Load GDT
72         lgdt [gGDTPtr - KERNEL_BASE]
73         jmp 0x08:start64 - KERNEL_BASE
74
75 .not64bitCapable:
76         mov ah, 0x0F
77         mov edi, 0xB8000
78         mov esi, csNot64BitCapable - KERNEL_BASE
79
80 .loop:
81         lodsb
82         test al, al
83         jz .hlt
84         stosw
85         jmp .loop
86         
87 .hlt:
88         jmp .hlt
89
90 [section .data]
91 [global gGDT]
92 [global gGDTPtr]
93 gGDT:
94         dd      0,0
95         dd      0x00000000, 0x00209A00  ; 0x08: 64-bit Code
96         dd      0x00000000, 0x00009200  ; 0x10: 64-bit Data
97         dd      0x00000000, 0x0020FA00  ; 0x18: 64-bit User Code
98         dd      0x00000000, 0x0000F200  ; 0x20: 64-bit User Data
99         dd      0x00000000, 0x0040FA00  ; 0x28: 32-bit User Code
100         dd      0x00000000, 0x0040F200  ; 0x30: 32-bit User Data
101         times MAX_CPUS  dd      0, 0x00008900, 0, 0     ; 0x38+16*n: TSS 0
102 gGDTPtr:
103         dw      $-gGDT-1
104         dd      gGDT-KERNEL_BASE
105         dd      0
106 [global gMultibootPtr]
107 [global gMultibootMagic]
108 gMultibootMagic:
109         dd      0
110 gMultibootPtr:
111         dd      0
112
113 [section .padata]
114 [global gInitialPML4]
115 gInitialPML4:   ; Covers 256 TiB (Full 48-bit Virtual Address Space)
116         dd      gInitialPDP - KERNEL_BASE + 3, 0        ; Identity Map Low 4Mb
117         times 0xA0*2-1  dq      0
118         dd      gStackPDP - KERNEL_BASE + 3, 0
119         times 512-4-($-gInitialPML4)/8  dq      0
120         dd      gInitialPML4 - KERNEL_BASE + 3, 0       ; Fractal Mapping
121         dq      0
122         dq      0
123         dd      gHighPDP - KERNEL_BASE + 3, 0   ; Map Low 4Mb to kernel base
124
125 gInitialPDP:    ; Covers 512 GiB
126         dd      gInitialPD - KERNEL_BASE + 3, 0
127         times 511       dq      0
128
129 gStackPDP:
130         dd      gStackPD - KERNEL_BASE + 3, 0
131         times 511       dq      0
132
133 gHighPDP:       ; Covers 512 GiB
134         times 510       dq      0
135         ;dq     0 + 0x143       ; 1 GiB Page from zero
136         dd      gInitialPD - KERNEL_BASE + 3, 0
137         dq      0
138
139 gInitialPD:     ; Covers 1 GiB
140 ;       dq      0 + 0x143       ; 1 GiB Page from zero
141         dd      gInitialPT1 - KERNEL_BASE + 3, 0
142         dd      gInitialPT2 - KERNEL_BASE + 3, 0
143         times 510       dq      0
144
145 gStackPD:
146         dd      gKStackPT - KERNEL_BASE + 3, 0
147         times 511       dq      0
148
149 gKStackPT:      ; Covers 2 MiB
150         ; Initial stack - 64KiB
151         dq      0
152         %assign i 0
153         %rep 16-1
154         dd      gInitialKernelStack - KERNEL_BASE + i*0x1000 + 0x103, 0
155         %assign i i+1
156         %endrep
157         times 512-16    dq 0
158 gInitialPT1:    ; 2 MiB
159         %assign i 0
160         %rep 512
161         dq      i*4096+0x103
162         %assign i i+1
163         %endrep
164 gInitialPT2:    ; 2 MiB
165         %assign i 512
166         %rep 512
167         dq      i*4096+0x103
168         %assign i i+1
169         %endrep
170
171 [section .padata]
172 [global gInitialKernelStack]
173 gInitialKernelStack:
174         times 0x1000*(16-1)     db 0    ; 16 Pages
175
176 [section .rodata]
177 csNot64BitCapable:
178         db "Not 64-bit Capable",0

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