提交 6e56862a 编写于 作者: R rain

0.540s-kernel: write in C.

上级 a6fd5994
......@@ -20,3 +20,5 @@
2021-2-25 0.619: 619 lines, 6 files, rain
2021-2-25 0.540: 540 lines, 6 files, rain
......@@ -2,11 +2,17 @@ all:
touch boot/x86/bootloader.bin
make -C boot/x86/
make -C kernel/
make cunix.img
clear:
rm boot/x86/*.bin kernel/*.bin cunix.img
rm *.bin boot/x86/*.bin kernel/*.o kernel/*.bin kernel/init/*.o cunix.img -rf
lines:
wc boot/x86/boot.asm boot/x86/loader.asm kernel/kernel.asm include/arch/x86/* -l
wc boot/x86/boot.asm boot/x86/loader.asm kernel/init/*.c include/arch/x86/* -l
cunix.img: boot/x86/bootloader.bin kernel/kernel.bin
dd if=/dev/zero of=cunix.img bs=1024 count=1440
cat boot/x86/bootloader.bin kernel/kernel.bin > boot.bin
dd if=boot.bin of=cunix.img conv=notrunc
......@@ -49,3 +49,5 @@ Cunix kernel
`0.619s-boot`:
&nbsp;&nbsp;&nbsp;&nbsp;load 180KB of disk, not 4KB <br/>
`0.540s-kernel`:
&nbsp;&nbsp;&nbsp;&nbsp;write in C. <br/>
......@@ -28,37 +28,18 @@ jmp _start
bits 16
_start:
mov si, message
call print
call set_vga_mode
; init segment register fs
.init_fs:
; enable A20 address 20 line
.open_a20:
in al, 0x92
or al, 0x02
out 0x92, al
.to_protect_mode:
cli
lgdt [gdt32_descriptor]
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
mov eax, cr0
; set CR0.bit 1 (Protect-mode Enable)
......@@ -84,6 +65,13 @@ print:
.do_ret:
ret
set_vga_mode:
mov al, 0x13
mov ah, 0x00
int 0x10
ret
bits 32
init_protect_mode:
......@@ -176,9 +164,6 @@ init_long_mode:
mov rsp, 0x0000000000007c00
jmp 0x8200
message: db "Cunix is loading...", 0x0d, 0x0a, 0x00
gdt32:
dd 0x00000000, 0x00000000
......
ASM_FLAGS = -I ../include
default:
make ../cunix.img
make kernel.bin
init/main.o: init/main.c
make -C init/
kernel.bin: kernel.asm
nasm kernel.asm -o kernel.bin $(ASM_FLAGS)
kernel.o: init/main.o
ld -b elf64-x86-64 -o kernel.o init/main.o -T kernel.lds
kernel.bin: kernel.o
objcopy -I elf64-x86-64 -S -R ".eh_frame" -R ".comment" -O binary kernel.o kernel.bin
../cunix.img: kernel.bin
cat ../boot/x86/bootloader.bin kernel.bin >> boot.bin
dd if=boot.bin of=../cunix.img
C_FLAGS = -mcmodel=large -fno-builtin -m64
all: main.o
%.o: %.c
gcc -c $*.c $(C_FLAGS)
void init(void) {
unsigned long i;
for (i = 0xa0000; i < 0xaffff; i++) {
*((char *) i) = 0x0f;
}
for (;;);
}
; 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/>.
org 0x8200
bits 64
_start:
mov byte [0xb8000], 'K'
call setup_idt
lidt [idt64_descriptor]
sti
jmp $
setup_idt:
lea rdx, ignore_int
mov rax, (0x08 << 16)
mov ax, dx
mov rcx, (0x8e00 << 32)
add rax, rcx
mov ecx, edx
shr ecx, 16
shl rcx, 48
add rax, rcx
shr rdx, 32
; we save IDT at 0x0000000000000000 like BIOS
lea rdi, 0x0000000000000000
mov rcx, 256
.setup_loop:
mov [rdi], rax
mov [rdi + 8], rdx
add rdi, 0x10
dec rcx
jne .setup_loop
ret
ignore_int:
add byte [0xb8000], 1
; CPU will cli when it call ignore_int
hlt
idt64_length equ 0xff * 16
idt64_descriptor:
dw idt64_length
dd 0x0000000000000000
OUTPUT_FORMAT("elf64-x86-64","elf64-x86-64", "elf64-x86-64")
OUTPUT_ARCH(i386:x86-64)
ENTRY(init)
SECTIONS {
. = 0x0000 + 0x8200;
.text : {
_text = .;
*(.text)
_etext = .;
}
. = ALIGN(8);
.data : {
_data = .;
*(.data);
_edata = .;
}
.rodata : {
_rodata = .;
*(.rodata)
.erodata = .;
}
.bss : {
_bss = .;
*(.bss)
_ebss = .;
}
_end = .;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册