86c90a571d99f21834e3b762a4cbf275ce3f31f8
[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 gGDT:
93         dd      0,0
94         dd      0x00000000, 0x00209A00  ; 0x08: 64-bit Code
95         dd      0x00000000, 0x00009200  ; 0x10: 64-bit Data
96         dd      0x00000000, 0x0020FA00  ; 0x18: 64-bit User Code
97         dd      0x00000000, 0x0000F200  ; 0x20: 64-bit User Data
98         dd      0x00000000, 0x0040FA00  ; 0x38: 32-bit User Code
99         dd      0x00000000, 0x0040F200  ; 0x30: 32-bit User Data
100         times MAX_CPUS  dd      0, 0x00008900, 0, 0     ; 0x38+16*n: TSS 0
101 gGDTPtr:
102         dw      $-gGDT-1
103         dd      gGDT-KERNEL_BASE
104         dd      0
105 [global gMultibootPtr]
106 [global gMultibootMagic]
107 gMultibootMagic:
108         dd      0
109 gMultibootPtr:
110         dd      0
111
112 [section .padata]
113 [global gInitialPML4]
114 gInitialPML4:   ; Covers 256 TiB (Full 48-bit Virtual Address Space)
115         dd      gInitialPDP - KERNEL_BASE + 3, 0        ; Identity Map Low 4Mb
116         times 512-4-1 dq        0
117         dd      gInitialPML4 - KERNEL_BASE + 3, 0       ; Fractal Mapping
118         dq      0
119         dq      0
120         dd      gHighPDP - KERNEL_BASE + 3, 0   ; Map Low 4Mb to kernel base
121
122 gInitialPDP:    ; Covers 512 GiB
123         dd      gInitialPD - KERNEL_BASE + 3, 0
124         times 511       dq      0
125
126 gHighPDP:       ; Covers 512 GiB
127         times 510       dq      0
128         ;dq     0 + 0x143       ; 1 GiB Page from zero
129         dd      gInitialPD - KERNEL_BASE + 3, 0
130         dq      0
131
132 gInitialPD:     ; Covers 1 GiB
133         dd      gInitialPT1 - KERNEL_BASE + 3, 0
134         dd      gInitialPT2 - KERNEL_BASE + 3, 0
135         times 510       dq      0
136
137 gInitialPT1:    ; Covers 2 MiB
138         %assign i 0
139         %rep 512
140         dq      i*4096+0x103
141         %assign i i+1
142         %endrep
143 gInitialPT2:    ; 2 MiB
144         %assign i 512
145         %rep 512
146         dq      i*4096+0x103
147         %assign i i+1
148         %endrep
149
150
151 [section .rodata]
152 csNot64BitCapable:
153         db "Not 64-bit Capable",0

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