Mercurial > lcfOS
annotate cos/kernel/goto64.asm @ 34:8012221dd740
Fixes for uninitialized data. This causes problems on real machines
author | windel |
---|---|
date | Mon, 16 Jan 2012 13:46:06 +0100 |
parents | d8185ddb6c7b |
children | bcb3b68c8147 |
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 | |
20 | 21 bits 32 ; Start in 32 bits mode, as loaded by GRUB |
22 | |
9 | 23 ; Multiboot header: |
24 ; Settings for multiboot header | |
25 PAGE_ALIGN equ 1 << 0 | |
26 MEM_INFO equ 1 << 1 | |
27 KLUDGE equ 1 << 16 | |
28 MAGIC equ 0x1BADB002 | |
29 FLAGS equ PAGE_ALIGN | MEM_INFO | KLUDGE ; align and provide memory map | |
30 CHECKSUM equ -(MAGIC+FLAGS) | |
31 | |
32 ; actual multiboot header: | |
33 align 4 | |
34 MultiBootHeader: | |
35 dd MAGIC | |
36 dd FLAGS | |
37 dd CHECKSUM | |
38 ; item below are present if bit 16 is set in flags | |
39 dd MultiBootHeader ; physical address in file of header (will be 0x100000 if put at start) | |
40 dd 0x100000 ; load_addr: load address, the address to start loading | |
41 dd 0x0 ; load_end_addr: zero indicates to load whole file | |
42 dd 0x0 ; bss_end_addr: zero indicates no bss segment present | |
43 dd loader ; entry_addr: jump to here | |
44 | |
20 | 45 ; 32 bits temporary GDT: |
46 align 16 | |
47 gdt32: | |
48 dw 0x0000, 0x0000, 0x0000, 0x0000 ; Null desciptor | |
49 dw 0xFFFF, 0x0000, 0x9A00, 0x00CF ; 32-bit code desciptor | |
50 dw 0xFFFF, 0x0000, 0x9200, 0x008F ; 32-bit data desciptor | |
51 gdt32_end: | |
52 | |
53 ; 32 bits gdt pointer: | |
54 align 16 | |
55 gdt32pointer: ; Global Descriptors Table Register | |
56 dw gdt32_end - gdt32 - 1 ; limit of GDT (size minus one) | |
57 dq gdt32 ; linear address of GDT | |
58 | |
9 | 59 ; GDT, three entries: one for code, one for data |
20 | 60 align 16 |
61 gdt64: | |
62 .Null: equ $ - gdt64 | |
18
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
63 dq 0 |
20 | 64 .Code: equ $ - gdt64 |
18
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
65 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
|
66 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
|
67 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
|
68 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
|
69 db 00100000b ; granularity 0x20 (L=1 => long mode) |
9 | 70 db 0 |
20 | 71 .Data: equ $ - gdt64 |
9 | 72 dw 0 |
73 dw 0 | |
74 db 0 | |
18
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
75 db 10010000b ; access ; 0x90 |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
76 db 00000000b ; granularity 0x00 |
9 | 77 db 0 |
20 | 78 gdt64end: |
79 | |
80 ;SYS64_NULL_SEL equ $-gdt64 ; Null Segment | |
81 ; dq 0x0000000000000000 | |
82 ; SYS64_CODE_SEL equ $-gdt64 ; Code segment, read/execute, nonconforming | |
83 ; dq 0x0020980000000000 ; 0x00209A0000000000 | |
84 ; SYS64_DATA_SEL equ $-gdt64 ; Data segment, read/write, expand down | |
85 ; dq 0x0000900000000000 ; 0x0020920000000000 | |
86 ; gdt64_end: | |
87 | |
88 gdt64pointer: ; GDT pointer | |
89 dw gdt64end - gdt64 - 1 ; Limit (size) | |
90 dq gdt64 ; Base | |
9 | 91 |
23 | 92 hltmessage: |
93 db "Long mode not supported", 0x0 | |
94 | |
9 | 95 ; Start of loader code: |
96 loader: | |
32
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
24
diff
changeset
|
97 ; here ebx contains the pointer to the multiboot header, store is for later use. |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
24
diff
changeset
|
98 extern multiboot_info |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
24
diff
changeset
|
99 mov [multiboot_info], ebx |
34
8012221dd740
Fixes for uninitialized data. This causes problems on real machines
windel
parents:
33
diff
changeset
|
100 xor ebx, ebx |
8012221dd740
Fixes for uninitialized data. This causes problems on real machines
windel
parents:
33
diff
changeset
|
101 mov [multiboot_info + 4], ebx ; zero the rest of 64 bits address. |
9 | 102 |
18
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
103 ; 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
|
104 mov eax, 80000000h |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
105 cpuid |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
106 cmp eax, 80000000h |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
107 jbe no_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, 80000001h |
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 bt edx, 29 |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
111 jnc no_long_mode |
20 | 112 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
|
113 |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
114 no_long_mode: |
23 | 115 ; Print long mode not supported |
116 mov edi, 0xb8000 | |
117 mov esi, hltmessage | |
24 | 118 xor eax, eax |
23 | 119 loop1: |
120 lodsb | |
121 mov dl, al | |
122 stosb | |
123 mov al, 0x1f | |
124 stosb | |
125 cmp dl, 0 | |
126 jne loop1 | |
18
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
127 hlt |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
128 |
20 | 129 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
|
130 |
20 | 131 lgdt [gdt32pointer] ; Reload a valid temporary 32 bits GDT, overload GRUB gdt. |
132 mov ax, 0x10 | |
133 mov ds, ax | |
134 mov es, ax | |
135 mov fs, ax | |
136 mov gs, ax | |
137 mov ss, ax | |
138 jmp 8:start32 ; make sure CS is loaded. | |
139 start32: | |
9 | 140 |
20 | 141 cld ; clear direction? |
142 | |
143 ; Clear the paging tables 0x1000, 0x2000, 0x3000 and 0x4000: | |
9 | 144 mov edi, 0x1000 |
145 xor eax, eax | |
24 | 146 mov ecx, 0x1000 |
9 | 147 rep stosd |
148 | |
20 | 149 ; Create PML4 table: |
150 mov edi, 0x1000 | |
151 mov eax, 0x2003 | |
152 stosd | |
153 | |
154 ; Create PDP (page directory pointer) table: | |
155 mov edi, 0x2000 | |
156 mov eax, 0x3003 ; PDPT entry, present and read/write | |
157 stosd | |
9 | 158 |
20 | 159 ; Create PD (page directory) table |
160 mov edi, 0x3000 | |
161 ; First entry: | |
162 mov eax, 0x8F ; PD entry, present (bit 0), read write (bit 1) and bit 7, page size=2MB | |
163 stosd | |
164 xor eax, eax | |
165 stosd | |
9 | 166 |
20 | 167 ; Second entry: |
168 mov eax, 0x20008f | |
169 stosd | |
170 xor eax, eax | |
171 stosd | |
172 | |
173 ; Third entry: | |
174 mov eax, 0x40008f | |
175 stosd | |
176 xor eax, eax | |
177 stosd | |
178 ; 6 MB mapped in total now. | |
179 | |
180 mov edi, 0x1000 ; Set load address | |
181 mov cr3, edi ; CR3 is the PML4 base address! | |
182 | |
183 ; Enable address extension: | |
9 | 184 mov eax, cr4 |
185 or eax, 1 << 5 ; PAE-bit is bit 5 | |
186 mov cr4, eax | |
187 | |
24 | 188 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
|
189 |
9 | 190 ; Set LM-bit (Long Mode bit): |
191 mov ecx, 0xC0000080 | |
192 rdmsr | |
193 or eax, 0x100 ; Set bit 8 (LM-bit) | |
194 wrmsr | |
195 | |
196 ; Enable paging: | |
197 mov eax, cr0 | |
198 or eax, 0x80000000 ; Set bit 31 (PG-bit) | |
199 mov cr0, eax | |
200 | |
201 ; Jump to 64 bits kernel: | |
20 | 202 jmp gdt64.Code:Realm64 |
9 | 203 |
204 bits 64 | |
20 | 205 align 16 |
9 | 206 |
207 Realm64: | |
208 | |
18
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
209 ; Clear segment registers: |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
210 xor ax, ax |
9 | 211 mov ds, ax |
212 mov es, ax | |
18
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
213 mov ss, ax |
9 | 214 mov fs, ax |
215 mov gs, ax | |
216 | |
20 | 217 lgdt [gdt64pointer] ; Reload GDT in 64 bits mode |
9 | 218 |
33 | 219 ; Test: |
220 mov ax, 0x10 | |
221 mov ds, ax | |
222 ; End of test | |
223 | |
32
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
24
diff
changeset
|
224 ; TODO: determine a good place for the kernel stack. |
20 | 225 mov rsp, 0xA000 ; Setup stack pointer. |
9 | 226 |
227 extern kmain | |
20 | 228 call kmain ; Call kernel in C-code |
229 |