annotate cos/kernel/goto64.asm @ 271:cf7d5fb7d9c8

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