Skip to content

Commit

Permalink
copy gdt to kernel
Browse files Browse the repository at this point in the history
  • Loading branch information
zeyu committed Jul 26, 2020
1 parent 2a516cb commit 263d802
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 54 deletions.
Empty file modified .gitignore
100644 → 100755
Empty file.
48 changes: 26 additions & 22 deletions Makefile
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
ENTRYPOINT = 0x10000

ASM = nasm
LD = ld
ASMBFLAGS = -I boot/include/
ASMKFLAGS = -I include/ -f elf
LDFLAGS = -m elf_i386 -s -Ttext $(ENTRYPOINT)
ORANGESBOOT = boot/boot.bin boot/loader.bin
ORANGESKERNEL = kernel.bin
OBJS = kernel/kernel.o
BOOT_BIN = boot/boot.bin
LDR_BIN = boot/loader.bin
KERNEL_BIN = kernel.bin

IMG:=boot.img
FLOPPY:=/mnt/floppy/
ENTRYPOINT = 0x10000

ASM = nasm
CC = gcc
CFLAGS = -m32 -g -I include/ -c -fno-builtin -fno-stack-protector
LD = ld
ASMBFLAGS = -I boot/include/
ASMKFLAGS = -I lib -f elf
LDFLAGS = -m elf_i386 -s -Ttext $(ENTRYPOINT)

ORANGESBOOT = boot/boot.bin boot/loader.bin
ORANGESKERNEL = kernel.bin
OBJS = kernel/kernel.o kernel/start.o lib/string.o
BOOT_BIN = boot/boot.bin
LDR_BIN = boot/loader.bin
KERNEL_BIN = kernel.bin

IMG = boot.img
FLOPPY = /mnt/floppy/

.PHONY : clean everything imgage all

everything: clean $(ORANGESBOOT) $(ORANGESKERNEL)
all: everything imgage
all: everything image
bochs: all bochstart

image : $(LDR_BIN) $(KERNEL_BIN)
# bximg
Expand All @@ -28,14 +32,17 @@ image : $(LDR_BIN) $(KERNEL_BIN)
sudo cp $(KERNEL_BIN) $(FLOPPY) -fv
sudo umount $(FLOPPY)

bochstart:
bochs -f bochsrc.bxrc

clean :
rm -f $(OBJS) $(ORANGESBOOT) $(ORANGESKERNEL)

$(BOOT_BIN) : boot/boot.asm boot/include/lib.asm boot/include/fat12hdr.asm
$(ASM) $(ASMBFLAGS) -o $@ $<

$(LDR_BIN) : boot/loader.asm boot/include/lib.asm \
boot/include/fat12hdr.asm boot/include/pm.asm
boot/include/fat12hdr.asm boot/include/pm.asm
$(ASM) $(ASMBFLAGS) -o $@ $<

$(ORANGESKERNEL) : $(OBJS)
Expand All @@ -44,11 +51,8 @@ $(ORANGESKERNEL) : $(OBJS)
kernel/kernel.o : kernel/kernel.asm
$(ASM) $(ASMKFLAGS) -o $@ $<

kernel/start.o : kernel/start.c include/type.h include/const.h include/protect.h
kernel/start.o : kernel/start.c kernel/start.h
$(CC) $(CFLAGS) -o $@ $<

lib/kliba.o : lib/kliba.asm
$(ASM) $(ASMKFLAGS) -o $@ $<

lib/string.o : lib/string.asm
$(ASM) $(ASMKFLAGS) -o $@ $<
Empty file modified README.md
100644 → 100755
Empty file.
Empty file modified bochsrc.bxrc
100644 → 100755
Empty file.
Binary file modified boot.img
100644 → 100755
Binary file not shown.
Empty file modified boot/Makefile
100644 → 100755
Empty file.
Empty file modified boot/boot.asm
100644 → 100755
Empty file.
Empty file modified boot/include/fat12hdr.asm
100644 → 100755
Empty file.
Empty file modified boot/include/lib.asm
100644 → 100755
Empty file.
Empty file modified boot/include/pm.asm
100644 → 100755
Empty file.
Empty file modified boot/loader.asm
100644 → 100755
Empty file.
54 changes: 22 additions & 32 deletions kernel/kernel.asm
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,37 +1,27 @@
[section .data]
randstr db "Welcome to kernel by techlog.cn", 0
SELECTOR_KERNEL_CS equ 8

; 导入函数与全局变量
extern copy_gdt
extern gdt_ptr

[SECTION .bss]
StackSpace resb 2 * 1024 * 1024
StackTop:

[section .text]
global _start

_start:
push dword randstr
call DispStr
add esp, 4
jmp $

DispStr:
push ebp
mov ebp, esp
push ebx
push esi
push edi

mov esi, [ebp + 8] ; pszInfo
mov edi, (80 * 7) * 2
mov ah, 0Fh
.1:
lodsb
test al, al
jz .2
.3:
mov [gs:edi], ax
add edi, 2
jmp .1

.2:
pop edi
pop esi
pop ebx
pop ebp
ret
mov esp, StackTop ; 堆栈在 bss 段中

sgdt [gdt_ptr] ; cstart() 中将会用到 gdt_ptr
call copy_gdt ; 在此函数中改变了gdt_ptr,让它指向新的GDT
lgdt [gdt_ptr] ; 使用新的GDT

jmp SELECTOR_KERNEL_CS:csinit
csinit: ; 长跳转,让 GDT 切换生效

push 0
popfd

hlt

0 comments on commit 263d802

Please sign in to comment.