comparison cos/kernel/goto64.asm @ 18:6129643f5c34

Fixed interrupt issue, ds, es, ss, fs and gs were not initialized to 0
author windel
date Sun, 20 Nov 2011 20:35:51 +0100
parents fcdae30b2782
children b1fed2171e1a
comparison
equal deleted inserted replaced
17:f3e3e0e9c4bc 18:6129643f5c34
31 dd loader ; entry_addr: jump to here 31 dd loader ; entry_addr: jump to here
32 32
33 ; GDT, three entries: one for code, one for data 33 ; GDT, three entries: one for code, one for data
34 GDT64: 34 GDT64:
35 .Null: equ $ - GDT64 35 .Null: equ $ - GDT64
36 dw 0 36 dq 0
37 dw 0
38 db 0
39 db 0
40 db 0
41 db 0
42 .Code: equ $ - GDT64 37 .Code: equ $ - GDT64
43 dw 0 38 dw 0 ; Segment limit 15-0
44 dw 0 39 dw 0 ; Base 15 - 0
45 db 0 40 db 0 ; Base 23 - 16
46 db 10011000b ; access 41 db 10011000b ; access 0x98 (P=1 => Present)
47 db 00100000b ; granularity 42 db 00100000b ; granularity 0x20 (L=1 => long mode)
48 db 0 43 db 0
49 .Data: equ $ - GDT64 44 .Data: equ $ - GDT64
50 dw 0 45 dw 0
51 dw 0 46 dw 0
52 db 0 47 db 0
53 db 10010000b ; access 48 db 10010000b ; access ; 0x90
54 db 00000000b ; granularity 49 db 00000000b ; granularity 0x00
55 db 0 50 db 0
56 .Pointer: ; GDT pointer 51 .Pointer: ; GDT pointer
57 dw $ - GDT64 - 1 ; Limit 52 dw $ - GDT64 - 1 ; Limit
58 dq GDT64 ; Base 53 dq GDT64 ; Base
59 54
60 ; Start of loader code: 55 ; Start of loader code:
61 global loader 56 global loader
62 loader: 57 loader:
58
59 ; Check that the CPU supports long mode:
60 mov eax, 80000000h
61 cpuid
62 cmp eax, 80000000h
63 jbe no_long_mode
64 mov eax, 80000001h
65 cpuid
66 bt edx, 29
67 jnc no_long_mode
68 jmp long_mode
69
70 no_long_mode:
71 hlt
72
73 long_mode:
63 74
64 ; Prepare paging: 75 ; Prepare paging:
65 ; PML4T - 0x1000 76 ; PML4T - 0x1000
66 ; PDPT - 0x2000 77 ; PDPT - 0x2000
67 ; PDT - 0x3000 78 ; PDT - 0x3000
95 ; Enable paging: 106 ; Enable paging:
96 mov eax, cr4 107 mov eax, cr4
97 or eax, 1 << 5 ; PAE-bit is bit 5 108 or eax, 1 << 5 ; PAE-bit is bit 5
98 mov cr4, eax 109 mov cr4, eax
99 110
111 ; Load the GDT:
112 lgdt [GDT64.Pointer]
113
100 ; Set LM-bit (Long Mode bit): 114 ; Set LM-bit (Long Mode bit):
101 mov ecx, 0xC0000080 115 mov ecx, 0xC0000080
102 rdmsr 116 rdmsr
103 or eax, 0x100 ; Set bit 8 (LM-bit) 117 or eax, 0x100 ; Set bit 8 (LM-bit)
104 wrmsr 118 wrmsr
106 ; Enable paging: 120 ; Enable paging:
107 mov eax, cr0 121 mov eax, cr0
108 or eax, 0x80000000 ; Set bit 31 (PG-bit) 122 or eax, 0x80000000 ; Set bit 31 (PG-bit)
109 mov cr0, eax 123 mov cr0, eax
110 124
111 ; Load the GDT:
112 lgdt [GDT64.Pointer]
113 125
114 ; Jump to 64 bits kernel: 126 ; Jump to 64 bits kernel:
115 jmp GDT64.Code:Realm64 127 jmp GDT64.Code:Realm64
116 128
117 bits 64 129 bits 64
118 130
119 ; realm64 131 ; realm64
120 Realm64: 132 Realm64:
121 133
122 cli 134 ; Clear segment registers:
123 mov ax, GDT64.Data 135 xor ax, ax
124 mov ds, ax 136 mov ds, ax
125 mov es, ax 137 mov es, ax
138 mov ss, ax
126 mov fs, ax 139 mov fs, ax
127 mov gs, ax 140 mov gs, ax
141
142 ; Reoad the GDT:
143 lgdt [GDT64.Pointer]
128 144
129 ; Done! 145 ; Done!
130 146
131 ; Setup stack pointer: 147 ; Setup stack pointer:
132 mov rsp, stackEnd 148 mov rsp, stackEnd