Mercurial > lcfOS
annotate cos/kernel/goto64.asm @ 24:d8627924d40d
Split up in more files and reboot command
author | windel |
---|---|
date | Fri, 02 Dec 2011 14:00:02 +0100 |
parents | 5dd47d6eebac |
children | 3a6a9b929db0 |
rev | line source |
---|---|
9 | 1 ;#!/usr/bin/nasm |
2 | |
3 ; | |
4 ; See http://wiki.osdev.org/User:Stephanvanschaik/Setting_Up_Long_Mode | |
5 ; Loader assembly to load the 64 bits kernel just after this file. | |
6 | |
7 ; Assume that we are loaded at 1M (0x100000) | |
20 | 8 |
9 ; This file sets up long mode and creates paging tables. | |
10 ; Use 2 mbyte pages. Is this more efficient? | |
9 | 11 |
20 | 12 ; Intended memory map (copied from pure64), at the end of this file: |
24 | 13 ; MOVED TO 0x5000!! 0x0 : IDT, 256 entries |
20 | 14 ; 0x1000 - 0x2000 : PML4 (Page map level 4) |
15 ; 0x2000 - 0x3000 : PDPT (page directory pointer table) | |
16 ; 0x3000 - 0x4000 : PDT (page directory table) | |
17 ; 0x4000 - 0x5000 : PT (page table) | |
24 | 18 ; 0x5000 - 0x6000 : IDT entries |
19 ; 0x6000 - 0xA000 : Stack | |
20 | |
21 ; panic macro to debug on real hardware | |
22 %macro DOPANIC 1 | |
23 mov al, %1 | |
24 mov edi, 0xb8000 | |
25 stosb | |
26 xchg bx,bx | |
27 hlt | |
28 %endmacro | |
20 | 29 |
30 bits 32 ; Start in 32 bits mode, as loaded by GRUB | |
31 | |
9 | 32 ; Multiboot header: |
33 ; Settings for multiboot header | |
34 PAGE_ALIGN equ 1 << 0 | |
35 MEM_INFO equ 1 << 1 | |
36 KLUDGE equ 1 << 16 | |
37 MAGIC equ 0x1BADB002 | |
38 FLAGS equ PAGE_ALIGN | MEM_INFO | KLUDGE ; align and provide memory map | |
39 CHECKSUM equ -(MAGIC+FLAGS) | |
40 | |
41 ; actual multiboot header: | |
42 align 4 | |
43 MultiBootHeader: | |
44 dd MAGIC | |
45 dd FLAGS | |
46 dd CHECKSUM | |
47 ; item below are present if bit 16 is set in flags | |
48 dd MultiBootHeader ; physical address in file of header (will be 0x100000 if put at start) | |
49 dd 0x100000 ; load_addr: load address, the address to start loading | |
50 dd 0x0 ; load_end_addr: zero indicates to load whole file | |
51 dd 0x0 ; bss_end_addr: zero indicates no bss segment present | |
52 dd loader ; entry_addr: jump to here | |
53 | |
20 | 54 ; 32 bits temporary GDT: |
55 align 16 | |
56 gdt32: | |
57 dw 0x0000, 0x0000, 0x0000, 0x0000 ; Null desciptor | |
58 dw 0xFFFF, 0x0000, 0x9A00, 0x00CF ; 32-bit code desciptor | |
59 dw 0xFFFF, 0x0000, 0x9200, 0x008F ; 32-bit data desciptor | |
60 gdt32_end: | |
61 | |
62 ; 32 bits gdt pointer: | |
63 align 16 | |
64 gdt32pointer: ; Global Descriptors Table Register | |
65 dw gdt32_end - gdt32 - 1 ; limit of GDT (size minus one) | |
66 dq gdt32 ; linear address of GDT | |
67 | |
9 | 68 ; GDT, three entries: one for code, one for data |
20 | 69 align 16 |
70 gdt64: | |
71 .Null: equ $ - gdt64 | |
18
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
72 dq 0 |
20 | 73 .Code: equ $ - gdt64 |
18
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
74 dw 0 ; Segment limit 15-0 |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
75 dw 0 ; Base 15 - 0 |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
76 db 0 ; Base 23 - 16 |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
77 db 10011000b ; access 0x98 (P=1 => Present) |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
78 db 00100000b ; granularity 0x20 (L=1 => long mode) |
9 | 79 db 0 |
20 | 80 .Data: equ $ - gdt64 |
9 | 81 dw 0 |
82 dw 0 | |
83 db 0 | |
18
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
84 db 10010000b ; access ; 0x90 |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
85 db 00000000b ; granularity 0x00 |
9 | 86 db 0 |
20 | 87 gdt64end: |
88 | |
89 ;SYS64_NULL_SEL equ $-gdt64 ; Null Segment | |
90 ; dq 0x0000000000000000 | |
91 ; SYS64_CODE_SEL equ $-gdt64 ; Code segment, read/execute, nonconforming | |
92 ; dq 0x0020980000000000 ; 0x00209A0000000000 | |
93 ; SYS64_DATA_SEL equ $-gdt64 ; Data segment, read/write, expand down | |
94 ; dq 0x0000900000000000 ; 0x0020920000000000 | |
95 ; gdt64_end: | |
96 | |
97 gdt64pointer: ; GDT pointer | |
98 dw gdt64end - gdt64 - 1 ; Limit (size) | |
99 dq gdt64 ; Base | |
9 | 100 |
23 | 101 hltmessage: |
102 db "Long mode not supported", 0x0 | |
103 | |
9 | 104 ; Start of loader code: |
105 loader: | |
106 | |
18
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
107 ; Check that the CPU supports long mode: |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
108 mov eax, 80000000h |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
109 cpuid |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
110 cmp eax, 80000000h |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
111 jbe no_long_mode |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
112 mov eax, 80000001h |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
113 cpuid |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
114 bt edx, 29 |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
115 jnc no_long_mode |
20 | 116 jmp cpu_has_long_mode |
18
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
117 |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
118 no_long_mode: |
23 | 119 ; Print long mode not supported |
120 mov edi, 0xb8000 | |
121 mov esi, hltmessage | |
24 | 122 xor eax, eax |
23 | 123 loop1: |
124 lodsb | |
125 mov dl, al | |
126 stosb | |
127 mov al, 0x1f | |
128 stosb | |
129 cmp dl, 0 | |
130 jne loop1 | |
18
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
131 hlt |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
132 |
20 | 133 cpu_has_long_mode: |
18
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
134 |
20 | 135 lgdt [gdt32pointer] ; Reload a valid temporary 32 bits GDT, overload GRUB gdt. |
136 mov ax, 0x10 | |
137 mov ds, ax | |
138 mov es, ax | |
139 mov fs, ax | |
140 mov gs, ax | |
141 mov ss, ax | |
142 jmp 8:start32 ; make sure CS is loaded. | |
143 start32: | |
9 | 144 |
20 | 145 cld ; clear direction? |
146 | |
147 ; Clear the paging tables 0x1000, 0x2000, 0x3000 and 0x4000: | |
9 | 148 mov edi, 0x1000 |
149 xor eax, eax | |
24 | 150 mov ecx, 0x1000 |
9 | 151 rep stosd |
152 | |
20 | 153 ; Create PML4 table: |
154 mov edi, 0x1000 | |
155 mov eax, 0x2003 | |
156 stosd | |
157 | |
158 ; Create PDP (page directory pointer) table: | |
159 mov edi, 0x2000 | |
160 mov eax, 0x3003 ; PDPT entry, present and read/write | |
161 stosd | |
9 | 162 |
20 | 163 ; Create PD (page directory) table |
164 mov edi, 0x3000 | |
165 ; First entry: | |
166 mov eax, 0x8F ; PD entry, present (bit 0), read write (bit 1) and bit 7, page size=2MB | |
167 stosd | |
168 xor eax, eax | |
169 stosd | |
9 | 170 |
20 | 171 ; Second entry: |
172 mov eax, 0x20008f | |
173 stosd | |
174 xor eax, eax | |
175 stosd | |
176 | |
177 ; Third entry: | |
178 mov eax, 0x40008f | |
179 stosd | |
180 xor eax, eax | |
181 stosd | |
182 ; 6 MB mapped in total now. | |
183 | |
184 mov edi, 0x1000 ; Set load address | |
185 mov cr3, edi ; CR3 is the PML4 base address! | |
186 | |
187 ; Enable address extension: | |
9 | 188 mov eax, cr4 |
189 or eax, 1 << 5 ; PAE-bit is bit 5 | |
190 mov cr4, eax | |
191 | |
24 | 192 lgdt [gdt64pointer] ; Load the GDT |
18
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
193 |
9 | 194 ; Set LM-bit (Long Mode bit): |
195 mov ecx, 0xC0000080 | |
196 rdmsr | |
197 or eax, 0x100 ; Set bit 8 (LM-bit) | |
198 wrmsr | |
199 | |
200 ; Enable paging: | |
201 mov eax, cr0 | |
202 or eax, 0x80000000 ; Set bit 31 (PG-bit) | |
203 mov cr0, eax | |
204 | |
205 ; Jump to 64 bits kernel: | |
20 | 206 jmp gdt64.Code:Realm64 |
9 | 207 |
208 bits 64 | |
20 | 209 align 16 |
9 | 210 |
211 Realm64: | |
212 | |
18
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
213 ; Clear segment registers: |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
214 xor ax, ax |
9 | 215 mov ds, ax |
216 mov es, ax | |
18
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
217 mov ss, ax |
9 | 218 mov fs, ax |
219 mov gs, ax | |
220 | |
20 | 221 lgdt [gdt64pointer] ; Reload GDT in 64 bits mode |
9 | 222 |
20 | 223 mov rsp, 0xA000 ; Setup stack pointer. |
9 | 224 |
225 extern kmain | |
20 | 226 call kmain ; Call kernel in C-code |
227 |