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