Mercurial > lcfOS
annotate cos/kernel/goto64.asm @ 19:f454e3c592dd
Cleanup of bochs config file
author | windel |
---|---|
date | Sun, 27 Nov 2011 21:38:38 +0100 |
parents | 6129643f5c34 |
children | b1fed2171e1a |
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) | |
8 ;org 0x100000 ; this only works with flat bin output, not with elf64 output. | |
9 | |
10 bits 32 ; Start in 32 bits mode. | |
11 ; Multiboot header: | |
12 ; Settings for multiboot header | |
13 PAGE_ALIGN equ 1 << 0 | |
14 MEM_INFO equ 1 << 1 | |
15 KLUDGE equ 1 << 16 | |
16 MAGIC equ 0x1BADB002 | |
17 FLAGS equ PAGE_ALIGN | MEM_INFO | KLUDGE ; align and provide memory map | |
18 CHECKSUM equ -(MAGIC+FLAGS) | |
19 | |
20 ; actual multiboot header: | |
21 align 4 | |
22 MultiBootHeader: | |
23 dd MAGIC | |
24 dd FLAGS | |
25 dd CHECKSUM | |
26 ; item below are present if bit 16 is set in flags | |
27 dd MultiBootHeader ; physical address in file of header (will be 0x100000 if put at start) | |
28 dd 0x100000 ; load_addr: load address, the address to start loading | |
29 dd 0x0 ; load_end_addr: zero indicates to load whole file | |
30 dd 0x0 ; bss_end_addr: zero indicates no bss segment present | |
31 dd loader ; entry_addr: jump to here | |
32 | |
33 ; GDT, three entries: one for code, one for data | |
34 GDT64: | |
35 .Null: equ $ - GDT64 | |
18
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
36 dq 0 |
9 | 37 .Code: equ $ - GDT64 |
18
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
38 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
|
39 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
|
40 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
|
41 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
|
42 db 00100000b ; granularity 0x20 (L=1 => long mode) |
9 | 43 db 0 |
44 .Data: equ $ - GDT64 | |
45 dw 0 | |
46 dw 0 | |
47 db 0 | |
18
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
48 db 10010000b ; access ; 0x90 |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
49 db 00000000b ; granularity 0x00 |
9 | 50 db 0 |
51 .Pointer: ; GDT pointer | |
52 dw $ - GDT64 - 1 ; Limit | |
53 dq GDT64 ; Base | |
54 | |
55 ; Start of loader code: | |
56 global loader | |
57 loader: | |
58 | |
18
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
59 ; 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
|
60 mov eax, 80000000h |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
61 cpuid |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
62 cmp eax, 80000000h |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
63 jbe no_long_mode |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
64 mov eax, 80000001h |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
65 cpuid |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
66 bt edx, 29 |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
67 jnc no_long_mode |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
68 jmp long_mode |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
69 |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
70 no_long_mode: |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
71 hlt |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
72 |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
73 long_mode: |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
74 |
9 | 75 ; Prepare paging: |
76 ; PML4T - 0x1000 | |
77 ; PDPT - 0x2000 | |
78 ; PDT - 0x3000 | |
79 ; PT - 0x4000 | |
80 | |
81 ; Clear the tables: | |
82 mov edi, 0x1000 | |
83 mov cr3, edi ; CR3 is the page table address! | |
84 xor eax, eax | |
85 mov ecx, 4096 | |
86 rep stosd | |
87 mov edi, cr3 ; restore edi | |
88 | |
89 mov DWORD [edi], 0x2003 ; present and readwrite, points to first PDPT | |
90 add edi, 0x1000 | |
91 mov DWORD [edi], 0x3003 ; present and readwrite, points to first PDT | |
92 add edi, 0x1000 | |
93 mov DWORD [edi], 0x4003 ; present and readwrite, points to first PT | |
94 add edi, 0x1000 | |
95 | |
96 ; identity map the first two megabytes: | |
97 mov ebx, 0x00000003 | |
98 mov ecx, 512 | |
99 ; Fill all PT entries at 0x4000 | |
100 SetEntry: | |
101 mov DWORD [edi], ebx | |
102 add ebx, 0x1000 | |
103 add edi, 8 | |
104 loop SetEntry | |
105 | |
106 ; Enable paging: | |
107 mov eax, cr4 | |
108 or eax, 1 << 5 ; PAE-bit is bit 5 | |
109 mov cr4, eax | |
110 | |
18
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
111 ; Load the GDT: |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
112 lgdt [GDT64.Pointer] |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
113 |
9 | 114 ; Set LM-bit (Long Mode bit): |
115 mov ecx, 0xC0000080 | |
116 rdmsr | |
117 or eax, 0x100 ; Set bit 8 (LM-bit) | |
118 wrmsr | |
119 | |
120 ; Enable paging: | |
121 mov eax, cr0 | |
122 or eax, 0x80000000 ; Set bit 31 (PG-bit) | |
123 mov cr0, eax | |
124 | |
125 | |
126 ; Jump to 64 bits kernel: | |
127 jmp GDT64.Code:Realm64 | |
128 | |
129 bits 64 | |
130 | |
131 ; realm64 | |
132 Realm64: | |
133 | |
18
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
134 ; Clear segment registers: |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
135 xor ax, ax |
9 | 136 mov ds, ax |
137 mov es, ax | |
18
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
138 mov ss, ax |
9 | 139 mov fs, ax |
140 mov gs, ax | |
141 | |
18
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
142 ; Reoad the GDT: |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
143 lgdt [GDT64.Pointer] |
6129643f5c34
Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
windel
parents:
12
diff
changeset
|
144 |
9 | 145 ; Done! |
146 | |
147 ; Setup stack pointer: | |
148 mov rsp, stackEnd | |
149 ; Put a B upper left corner | |
150 mov al, 66 ; 'B' | |
151 mov [0xb8000], al | |
152 | |
153 ; Jump to code that is glued after this file | |
154 jmp einde | |
155 | |
156 align 16 | |
157 dataEnd: | |
158 ; reserve bytes for stack: | |
159 stackBegin: | |
160 resb 1024 | |
161 stackEnd: | |
162 | |
163 einde: | |
12 | 164 # XCHG BX, BX ; bochs breakpoint |
9 | 165 |
166 # Call kernel: | |
167 extern kmain | |
168 call kmain | |
169 # Should we ever return, remain in endless loop: | |
170 cli | |
171 hang: | |
172 hlt | |
173 jmp hang | |
174 |