loader.asm 3.4 KB
Newer Older
1 2 3 4 5
; Copyright (C) 2021 Rain

; This file is part of Cunix. 

; Cunix is free software: you can redistribute it and/or modify 
6
; it under the terms of the GNU General Public Licene and published by 
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
; the Free Software Foundation, either version 3 of the License, or 
; (at your option) any later version. 

; Cunix is distributed in the hope that it will be useful, 
; but WITHOUT ANY WARRANTY; without even the implied warranty of 
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
; GNU General Public License for more details. 

; You should have recceived a copy of the GNU General Public License 
; along with Cunix. If not, see <https://www.gnu.org/licenses/>. 




org 0x8000

23 24
jmp _start

25 26
%include "arch/x86/segment.inc"
%include "arch/x86/paging.inc"
R
rain 已提交
27

28 29 30 31 32 33
bits 16

_start:	
	mov si, message
	call print

34 35
; init segment register fs
.init_fs:
R
rain 已提交
36
	; enable A20 address 20 line
37
.open_a20:
R
rain 已提交
38
	in al, 0x92
39 40
	or al, 0x02
	out 0x92, al
R
rain 已提交
41

42 43
	cli
	lgdt [gdt32_descriptor]
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
	mov eax, cr0
	or eax, 0x01
	mov cr0, eax
	
	mov ax, data32
	mov fs, ax

	mov eax, cr0
	and eax, 11111111111111111111111111111110b
	mov cr0, eax

	; interrupt is disable
	
	; now, FS has addressing out of 1MB like protect-mode. 
	

.to_protect_mode:
	; GDT is already load by .init_fs
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

	mov eax, cr0
	; set CR0.bit 1 (Protect-mode Enable)
	or eax, 1
	mov cr0, eax

	jmp dword code32:init_protect_mode

print:
	mov al, [si]
	add si, 1
	
	cmp al, 0x00
	je .do_ret

	mov ah, 0x0e
	mov bx, 0x0f

	int 0x10

	jmp print

.do_ret:
	ret

bits 32

init_protect_mode:
	mov ax, 0x10
	mov ds, ax
	mov es, ax
	mov fs, ax
	mov gs, ax

	mov ss, ax
	mov esp, 0x00007c00

.to_long_mode:
	; page map at 0x7000
	; set pagemap level-4
R
rain 已提交
102
	mov dword [0x70000], 0x71000 | PAGE_USER
103
	mov dword [0x70004], 0x00000
R
rain 已提交
104
	mov dword [0x70800], 0x70000 | PAGE_USER
105 106 107
	mov dword [0x70804], 0x00000

	; ...and, page directory pointer table
R
rain 已提交
108
	mov dword [0x71000], 0x72000 | PAGE_USER
109 110 111
	mov dword [0x71008], 0x00000

	; then, page directory entry (2 MB size)
R
rain 已提交
112
	mov dword [0x72000], 0x000000 | PAGE_KERNEL
113 114
	mov dword [0x72004], 0x000000

R
rain 已提交
115
	mov dword [0x72008], 0x200000 | PAGE_KERNEL
116 117
	mov dword [0x7200c], 0x000000

R
rain 已提交
118
	mov dword [0x72010], 0x400000 | PAGE_KERNEL
119 120
	mov dword [0x72014], 0x000000

R
rain 已提交
121
	mov dword [0x72018], 0x600000 | PAGE_KERNEL
122 123
	mov dword [0x7201c], 0x000000
	
R
rain 已提交
124
	mov dword [0x72020], 0x800000 | PAGE_KERNEL
125 126
	mov dword [0x72024], 0x000000
	
R
rain 已提交
127
	mov dword [0x72028], 0xa00000 | PAGE_KERNEL
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
	mov dword [0x7202c], 0x000000
	
	

	lgdt [gdt64_descriptor]

	mov ax, 0x10
	mov ds, ax
	mov es, ax
	mov fs, ax
	mov gs, ax

	mov ss, ax
	mov esp, 0x00007c00

	mov eax, cr4
	bts eax, 5
	mov cr4, eax

	; page table
	mov eax, 0x70000
	mov cr3, eax

	; enable long-mode before paging
	mov ecx, 0xc0000080
	rdmsr

	bts eax, 8
	wrmsr

	mov eax, cr0
	or eax, 1
	bts eax, 31
	mov cr0, eax
	
	
	jmp code64:init_long_mode

bits 64

init_long_mode:
	mov ax, 0x10
	mov ds, ax
	mov es, ax
	mov fs, ax
	mov gs, ax

	mov ss, ax
	mov rsp, 0x0000000000007c00

e:
	hlt
	jmp e
	

message: db "Cunix is loading...", 0x0d, 0x0a, 0x00

gdt32:
	dd 0x00000000, 0x00000000

gdt32_code:
	dd 0x0000ffff, 0x00cf9a00

gdt32_data:
	dd 0x0000ffff, 0x00cf9200

gdt32_len equ $ - gdt32

gdt32_descriptor:
	dw gdt32_len - 1
	dd gdt32

code32 equ gdt32_code - gdt32
data32 equ gdt32_data - gdt32


gdt64:
	dq 0x0000000000000000

gdt64_code:
	dq 0x0020980000000000

gdt64_data:
	dq 0x0000920000000000

gdt64_len equ $ - gdt64

gdt64_descriptor:
	dw gdt64_len - 1
	dd gdt64

code64 equ gdt64_code - gdt64
data64 equ gdt64_data - gdt64