Kernel/x86_64 - Working out bugs and unimplementeds
[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  ; 0x38: 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 512-4-1 dq        0
118         dd      gInitialPML4 - KERNEL_BASE + 3, 0       ; Fractal Mapping
119         dq      0
120         dq      0
121         dd      gHighPDP - KERNEL_BASE + 3, 0   ; Map Low 4Mb to kernel base
122
123 gInitialPDP:    ; Covers 512 GiB
124         dd      gInitialPD - KERNEL_BASE + 3, 0
125         times 511       dq      0
126
127 gHighPDP:       ; Covers 512 GiB
128         times 510       dq      0
129         ;dq     0 + 0x143       ; 1 GiB Page from zero
130         dd      gInitialPD - KERNEL_BASE + 3, 0
131         dq      0
132
133 gInitialPD:     ; Covers 1 GiB
134         dd      gInitialPT1 - KERNEL_BASE + 3, 0
135         dd      gInitialPT2 - KERNEL_BASE + 3, 0
136         times 510       dq      0
137
138 gInitialPT1:    ; Covers 2 MiB
139         %assign i 0
140         %rep 512
141         dq      i*4096+0x103
142         %assign i i+1
143         %endrep
144 gInitialPT2:    ; 2 MiB
145         %assign i 512
146         %rep 512
147         dq      i*4096+0x103
148         %assign i i+1
149         %endrep
150
151
152 [section .rodata]
153 csNot64BitCapable:
154         db "Not 64-bit Capable",0

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