annotate cos/kernel/goto64.asm @ 13:d07d4701a103

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