提交 f4201d76 编写于 作者: R rain

0.326s-boot: enable long-mode but no kernel

上级
此差异已折叠。
2021-2-8 0.326: 326 lines, 2 files
remake:
echo "*** If there isn't 'bootloader.bin', use 'make first' ***"
rm bootloader.bin
make cunix.img
first:
make cunix.img
boot.bin: boot.asm
nasm boot.asm -o boot.bin
loader.bin: loader.asm
nasm loader.asm -o loader.bin
bootloader.bin: boot.bin loader.bin
cat boot.bin loader.bin >> bootloader.bin
cunix.img: bootloader.bin
dd if=/dev/zero of=cunix.img bs=1024 count=1440
dd if=bootloader.bin of=cunix.img conv=notrunc
clear:
rm *.bin cunix.img
; Copyright (C) 2021 Rain
; This file is part of Cunix.
; Cunix is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) and 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 received a copy of the GNU General Public License
; along with Cunix. If not, see <https://www.gnu.org/licenses/>.
BI_BOOTDEV equ 0x7e00
org 0x7c00
xor ax, ax
mov ds, ax
mov es, ax
mov ss, ax
mov sp, 0x7c00
; display message: `Cunix is booting...`
; boot device driver number is in DL (by BIOS-19)
mov [BI_BOOTDEV], dl
mov si, message
call print
call load_loader
jmp 0x0000:0x8000
message: db "Cunix is booting...", 0x0d, 0x0a, 0x00
diskpanic_message: db "read disk failed", 0x0d, 0x0a, 0x00
panic_message: db "panic: ", 0x00
print:
; SI: message address
mov al, [si]
add si, 1
cmp al, 0x00
je .ret
mov ah, 0x0e
mov bx, 0x0f
int 0x10
jmp print
.ret:
ret
load_loader:
mov ax, 0x0201
mov dx, 0x0000
mov dl, [BI_BOOTDEV]
mov cx, 0x0002
mov bx, 0x8000
int 0x13
mov di, diskpanic_message
jc .try_lba
ret
.try_lba:
mov ax, 0x0002
mov cl, 0x0001
mov bx, 0x8000
call read_lba
jc panic
ret
read_lba:
push dword 0x00000000
push dword eax
push word es
push word bx
push word cx
push word 0x0010
mov ah, 0x42
mov dl, 0x00
mov si, sp
int 0x13
add sp, 0x10
ret
panic:
mov si, panic_message
call print
mov si, di
call print
hlt
jmp $
times 510 - ($ - $$) db 0x00
db 0x55, 0xaa
; Copyright (C) 2021 Rain
; This file is part of Cunix.
; Cunix is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public Licene and published by
; 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
bits 16
_start:
mov si, message
call print
.to_protect_mode:
cli
lgdt [gdt32_descriptor]
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
mov dword [0x70000], 0x71007
mov dword [0x70004], 0x00000
mov dword [0x70800], 0x70007
mov dword [0x70804], 0x00000
; ...and, page directory pointer table
mov dword [0x71000], 0x72007
mov dword [0x71008], 0x00000
; then, page directory entry (2 MB size)
mov dword [0x72000], 0x000083
mov dword [0x72004], 0x000000
mov dword [0x72008], 0x200083
mov dword [0x7200c], 0x000000
mov dword [0x72010], 0x400083
mov dword [0x72014], 0x000000
mov dword [0x72018], 0x600083
mov dword [0x7201c], 0x000000
mov dword [0x72020], 0x800083
mov dword [0x72024], 0x000000
mov dword [0x72028], 0xa00083
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
t32_message: db "changing to protect mode...", 0x00
ok_message: db "OK", 0x0d, 0x0a, 0x00
fail_message: db "FAILED", 0x0d, 0x0a, 0x00
display_point: dw 0x0000
cur_position: db 0x00
line_position: db 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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册