21e7c408bce8565dee492e1e6fe1b00369977ac2
[tpg/acess2.git] / Kernel / arch / x86_64 / start32.asm
1
2 [BITS 32]
3
4 KERNEL_BASE     equ     0xFFFF800000000000
5
6 [section .multiboot]
7 mboot:
8         ; Multiboot macros to make a few lines later more readable
9         MULTIBOOT_PAGE_ALIGN    equ 1<<0
10         MULTIBOOT_MEMORY_INFO   equ 1<<1
11         MULTIBOOT_AOUT_KLUDGE   equ 1<<16
12         MULTIBOOT_HEADER_MAGIC  equ 0x1BADB002
13         MULTIBOOT_HEADER_FLAGS  equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO; | MULTIBOOT_AOUT_KLUDGE
14         MULTIBOOT_CHECKSUM      equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)
15         
16         ; This is the GRUB Multiboot header. A boot signature
17         dd MULTIBOOT_HEADER_MAGIC
18         dd MULTIBOOT_HEADER_FLAGS
19         dd MULTIBOOT_CHECKSUM
20         [extern __load_addr]
21         [extern __bss_start]
22         [extern gKernelEnd]
23         ; a.out kludge
24         dd mboot        ; Location of Multiboot Header
25         dd __load_addr  ; Load address
26         dd __bss_start - KERNEL_BASE    ; End of .data
27         dd gKernelEnd - KERNEL_BASE     ; End of .bss (and kernel)
28         dd start - KERNEL_BASE  ; Entrypoint
29
30 [extern start64]
31
32 [section .text]
33 [global start]
34 start:
35         ; Check for Long Mode support
36         mov eax, 0x80000001
37         cpuid
38         test edx, 1<<29
39         jz .not64bitCapable
40
41         ; Enable PAE
42         mov eax, cr4
43         or eax, 0x80|0x20
44         mov cr4, eax
45
46         ; Load PDP4
47         mov eax, gInitialPML4 - KERNEL_BASE
48         mov cr3, eax
49
50         ; Enable long/compatability mode
51         mov ecx, 0xC0000080
52         rdmsr
53         or ax, 0x100
54         wrmsr
55
56         ; Enable paging
57         mov eax, cr0
58         or eax, 0x80000000
59         mov cr0, eax
60
61         ; Load GDT
62         lgdt [gGDTPtr - KERNEL_BASE]
63         mov ax, 0x10
64         mov ss, ax
65         mov ds, ax
66         mov es, ax
67         mov fs, ax
68         mov gs, ax
69
70         jmp 0x08:start64 - KERNEL_BASE
71
72 .not64bitCapable:
73         mov ah, 0x0F
74         mov edi, 0xB8000
75         mov esi, csNot64BitCapable - KERNEL_BASE
76
77 .loop:
78         lodsb
79         test al, al
80         jz .hlt
81         stosw
82         jmp .loop
83         
84 .hlt:
85         jmp .hlt
86
87 [section .data]
88 [global gGDT]
89 gGDT:
90         dd      0,0
91         dd      0x00000000, 0x00209800  ; 0x08: 64-bit Code
92         dd      0x00000000, 0x00009000  ; 0x10: 64-bit Data
93         dd      0x00000000, 0x00209800  ; 0x18: 64-bit User Code
94         dd      0x00000000, 0x00209000  ; 0x20: 64-bit User Data
95         dd      0x00000000, 0x00209800  ; 0x38: 32-bit User Code
96         dd      0x00000000, 0x00209000  ; 0x30: 32-bit User Data
97         times MAX_CPUS  dd      0, 0, 0, 0      ; 0x38+16*n: TSS 0
98 gGDTPtr:
99         dw      $-gGDT-1
100         dd      gGDT-KERNEL_BASE
101         dd      0
102
103 [section .padata]
104 [global gInitialPML4]
105 gInitialPML4:   ; Covers 256 TiB (Full 48-bit Virtual Address Space)
106         dd      gInitialPDP - KERNEL_BASE + 3, 0        ; Identity Map Low 4Mb
107         times 256-1 dq  0
108         dd      gInitialPDP - KERNEL_BASE + 3, 0        ; Map Low 4Mb to kernel base
109         times 256-1-2 dq 0
110         dd      gInitialPML4 - KERNEL_BASE + 3, 0       ; Fractal Mapping
111         dq      0
112
113 gInitialPDP:    ; Covers 512 GiB
114         dd      gInitialPD - KERNEL_BASE + 3, 0
115         times 511       dq      0
116
117 gInitialPD:     ; Covers 1 GiB
118         dd      gInitialPT1 - KERNEL_BASE + 3, 0
119         dd      gInitialPT2 - KERNEL_BASE + 3, 0
120         times 510       dq      0
121
122 gInitialPT1:    ; Covers 2 MiB
123         %assign i 0
124         %rep 512
125         dq      i*4096+3
126         %assign i i+1
127         %endrep
128 gInitialPT2:    ; 2 MiB
129         %assign i 512
130         %rep 512
131         dq      i*4096+3
132         %assign i i+1
133         %endrep
134
135
136 [section .rodata]
137 csNot64BitCapable:
138         db "Not 64-bit Capable",0

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