-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a3e7e7d
Showing
235 changed files
with
21,438 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/*Link.ld*/ | ||
OUTPUT_FORMAT("pei-i386") | ||
|
||
ENTRY(greatstart) | ||
|
||
SECTIONS | ||
{ | ||
.text 0xc0100000: | ||
{ | ||
code = .; _code = .; __code = .; | ||
*(.text) | ||
. = ALIGN(4096); | ||
} | ||
|
||
.data : | ||
{ | ||
__CTOR_LIST__ = .; | ||
|
||
*(.ctors) | ||
LONG(0) | ||
__CTOR_END__ = .; | ||
|
||
__DTOR_LIST__ = .; | ||
/*LONG((__DTOR_END__ - __DTOR_LIST__) /4 - 2)*/ | ||
*(.dtors) | ||
LONG(0) | ||
__DTOR_END__ = .; | ||
|
||
data = .; _data = .; __data = .; | ||
*(.data) | ||
. = ALIGN(4096); | ||
} | ||
|
||
.bss : | ||
{ | ||
bss = .; _bss = .; __bss = .; | ||
*(.bss) | ||
. = ALIGN(4096); | ||
} | ||
|
||
end = .; _end =.; __end = .; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
include Makefile.inc | ||
|
||
.PHONY : all build deploy run $(DIRS) | ||
|
||
PROGRAMS = cat \ | ||
cp \ | ||
ls \ | ||
mkdir \ | ||
rm \ | ||
date \ | ||
perf \ | ||
rm | ||
|
||
all: build deploy | ||
|
||
$(DIRS) : | ||
$(MAKE) --directory=$@ | ||
|
||
build : $(DIRS) | ||
$(LD) $(LDFLAGS) $(OBJS) -o $(TARGET)\kernel.exe | ||
$(OBJCOPY) -O binary $(TARGET)\kernel.exe $(TARGET)\kernel.bin | ||
$(NM) $(TARGET)\kernel.exe > $(TARGET)\kernel.sym | ||
$(OBJDUMP) -d $(TARGET)\kernel.exe > $(TARGET)\kernel.asm | ||
|
||
deploy : | ||
|
||
copy $(TARGET)\boot.bin $(IMGTARGET)\boot.bin | ||
copy $(TARGET)\kernel.bin $(IMGTARGET)\kernel.bin | ||
copy $(TARGET)\kernel.sym $(IMGTARGET)\kernel.sym | ||
copy $(TARGET)\kernel.asm $(IMGTARGET)\kernel.asm | ||
|
||
cd $(IMGTARGET) && partcopy boot.bin 0 200 c.img 0 | ||
cd $(IMGTARGET) && partcopy kernel.bin 0 13000 c.img 200 | ||
|
||
copy $(TARGET)\boot.bin $(MakeImageDebug)\boot.bin | ||
copy $(TARGET)\kernel.bin $(MakeImageDebug)\kernel.bin | ||
copy $(IMGTARGET)\c.img $(MakeImageDebug)\c.img | ||
|
||
cd $(MakeImageDebug) && build.exe c.img boot.bin kernel.bin programs | ||
copy $(MakeImageDebug)\$(IMG) $(UNIXV6PPTARGET)\$(IMG) | ||
|
||
clean : | ||
del $(TARGET)\*.o | ||
del $(TARGET)\*.exe | ||
del $(TARGET)\*.bin | ||
del $(TARGET)\*.sym | ||
del $(TARGET)\*.asm | ||
|
||
del $(IMGTARGET)\*.bin | ||
del $(IMGTARGET)\*.sym | ||
del $(IMGTARGET)\*.asm | ||
|
||
del $(UNIXV6PPTARGET)\c.img |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#命令定义 | ||
CC = g++ | ||
LD = ld | ||
OBJCOPY = objcopy | ||
OBJDUMP = objdump | ||
ASM = nasm | ||
NM = nm | ||
MAKE = make | ||
|
||
#参数定义 | ||
#g++参数 | ||
CFLAGS = -Wall -O0 -g -nostartfiles -nostdlib -fno-builtin -fno-rtti -fno-exceptions -nostdinc | ||
#ld参数 | ||
LDFLAGS = -T $(LINKSCRIPT) | ||
|
||
#目录定义 | ||
OBJFILES = ..\objs | ||
TARGET = ..\targets\objs | ||
DIRS = boot kernel machine interrupt mm proc dev fs tty test pe lib program shell | ||
INCLUDE = ..\include | ||
IMGTARGET = ..\targets\img | ||
#"++"号是特殊符号,因此需要加引号 | ||
UNIXV6PPTARGET = "..\targets\UNIXV6++" | ||
MakeImageDebug = ..\tools\MakeImage\bin\Debug | ||
|
||
#文件定义 | ||
LINKSCRIPT = Link.ld | ||
SECTOR2_OBJ = sector2.bin | ||
CPP_OBJS = $(foreach OBJFILE, \ | ||
$(shell dir $(TARGET)\*.o /B), \ | ||
$(TARGET)\$(OBJFILE) \ | ||
) | ||
OBJS = $(TARGET)\$(SECTOR2_OBJ) $(CPP_OBJS) | ||
IMG = c.img | ||
|
||
#编译规则 | ||
.c.s: | ||
$(CC) $(CFLAGS) \ | ||
-S -o $*.s $< | ||
.s.o: | ||
$(AS) -o $*.o $< | ||
.c.o: | ||
$(CC) $(CFLAGS) \ | ||
-c -o $*.o $< |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
include ..\Makefile.inc | ||
|
||
TARGET = ..\..\targets\objs | ||
|
||
all : $(TARGET)\boot.bin $(TARGET)\sector2.bin $(TARGET)\support.o | ||
|
||
$(TARGET)\boot.bin : boot.s | ||
$(ASM) -f bin boot.s -o $(TARGET)\boot.bin | ||
|
||
$(TARGET)\sector2.bin : sector2.s | ||
$(ASM) -f elf sector2.s -o $(TARGET)\sector2.bin | ||
|
||
$(TARGET)\support.o : support.c | ||
$(CC) $(CFLAGS) -c $< -o $@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
org 0x7c00 | ||
|
||
;section .code16 | ||
[BITS 16] | ||
start: | ||
lgdt [gdtr] | ||
|
||
cli | ||
|
||
;打开a20 地址线 | ||
in al,92h | ||
or al,00000010b | ||
out 92h, al | ||
|
||
; start to load sector 1 to memory | ||
|
||
mov eax, cr0; | ||
or eax, 1; | ||
mov cr0, eax | ||
|
||
jmp dword 0x8:_startup ; | ||
|
||
|
||
;section .code32 | ||
[BITS 32] | ||
_startup: | ||
|
||
mov ax, 0x10 | ||
mov ds, ax | ||
mov es, ax | ||
mov ss, ax | ||
|
||
mov ecx, KERNEL_SIZE ;cx = 扇区数KERNEL_SIZE,作为loop的次数 | ||
mov eax, 1 ;LBA寻址模式下sector编号从0开始。 #0是引导扇区,#1扇区开始才是kernel的首扇区 | ||
mov ebx, 0x100000 ;目标存放地址从1M处开始,每次loop递增512 bytes | ||
_load_kernel: | ||
push eax | ||
inc eax | ||
|
||
push ebx | ||
add ebx, 512 | ||
call _load_sector | ||
loop _load_kernel | ||
|
||
;修改所有寄存器到高位地址 | ||
mov ax, 0x20 | ||
mov ds, ax | ||
mov es, ax | ||
mov ss, ax | ||
or esp, 0xc0000000 | ||
jmp 0x18:0xc0100000 | ||
|
||
_load_sector: | ||
push ebp | ||
mov ebp,esp | ||
|
||
push edx | ||
push ecx | ||
push edi | ||
push eax | ||
|
||
mov al,1 ;读1个扇区 | ||
mov dx,1f2h ;扇区数寄存器 0x1f2 | ||
out dx,al | ||
|
||
mov eax,[ebp+12] ;[ebp+12]对应上面mov eax, 1 push eax指令入栈的值,eax为要读入的扇区号 | ||
;LBA28(Linear Block Addressing)模式输入扇区号的Bits 7~0, 共28 Bits扇区号 | ||
inc dx ;扇区号寄存器 0x1f3 | ||
out dx,al | ||
|
||
shr eax,8 ;LBA28(Linear Block Addressing)模式输入扇区号的Bits 15~8 放入AL中, 共28 Bits扇区号 | ||
inc dx ;Port:DX = 0x1f3+1 = 0x1f4 | ||
out dx,al | ||
|
||
shr eax,8 ;LBA28(Linear Block Addressing)模式输入扇区号的Bits 23~16放入AL中, 共28 Bits扇区号 | ||
inc dx ;Port:DX = 0x1f4+1 = 0x1f5 | ||
out dx,al | ||
|
||
shr eax,8 | ||
and al,0x0f | ||
or al,11100000b ;Bit(7和5)为1表示是IDE接口,Bit(6)为1表示开启LBA28模式,Bit(4)为1表示主盘。 | ||
;Bit(3~0)为LBA28中的Bit27~24位 | ||
inc dx ;Port:DX = 0x1f5+1 = 0x1f6 | ||
out dx,al | ||
|
||
mov al,0x20 ;0x20表示读1个sector,0x30表示写1个sector | ||
inc dx ;Port:DX = 0x1f6+1 = 0x1f7 | ||
out dx,al | ||
|
||
.test: | ||
in al,dx | ||
test al,10000000b | ||
jnz .test | ||
|
||
test al,00001000b | ||
jz .load_error | ||
|
||
|
||
mov ecx,512/4 | ||
mov dx,0x1f0 | ||
mov edi,[ebp+8] ;取得call前入栈参数[ebp+8] = 0x100000 = 1MB | ||
rep insd | ||
xor ax,ax | ||
jmp .load_exit | ||
|
||
.load_error: | ||
mov dx,0x1f1 | ||
in al,dx | ||
xor ah,ah | ||
|
||
.load_exit: | ||
|
||
pop eax | ||
pop edi | ||
pop ecx | ||
pop edx | ||
leave ;Destory stack frame | ||
retn 8 | ||
|
||
;section .data | ||
KERNEL_SIZE equ 180 | ||
|
||
gdt: | ||
dw 0x0000 | ||
dw 0x0000 | ||
dw 0x0000 | ||
dw 0x0000 | ||
|
||
dw 0xFFFF | ||
dw 0x0000 | ||
dw 0x9A00 | ||
dw 0x00CF | ||
|
||
dw 0xFFFF | ||
dw 0x0000 | ||
dw 0x9200 | ||
dw 0x00CF | ||
|
||
dw 0xFFFF | ||
dw 0x0000 | ||
dw 0x9A00 | ||
dw 0x40CF | ||
|
||
dw 0xFFFF | ||
dw 0x0000 | ||
dw 0x9200 | ||
dw 0x40CF | ||
|
||
gdtr: | ||
dw $-gdt ;limit | ||
dd gdt ;offset | ||
|
||
times 510 - ($ - $$) db 0 | ||
dw 0xAA55 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[BITS 32] | ||
[extern _main0] | ||
|
||
[extern __main] ;"_main()"定义在support.c中 | ||
[extern __atexit] ; "_atexit()"定义在support.c中 | ||
|
||
global greatstart | ||
greatstart: | ||
mov eax,1 | ||
mov eax,2 | ||
mov eax,3 | ||
|
||
;Makefile中g++选项 -nostartfiles禁止了g++去链接startup code, | ||
;startup code即是在进入我们用C++编写的main0()函数之前,以及main0() | ||
;退出时执行的代码,其执行的工作是初始化(/销毁)global/static对象。 | ||
call __main ;call our own startup code | ||
jmp _main0 | ||
call __atexit ;call our own startup code |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
//Support.c | ||
|
||
/* | ||
函数名前需加上 extern “C”,虽然本文件名support.c是.c后缀的文件, | ||
文件内容也是pure C code,让是因为Makefile里面对support.c编译用的是 | ||
g++,而不是gcc,所以函数名前面还是要加上extern “C”,否则会报 | ||
“undefined reference to __main()”类似的错误。 | ||
*/ | ||
extern "C" void _main() | ||
{ | ||
extern void (*_CTOR_LIST__)(); | ||
/*Note: 此处使用_CTOR_LIST__,变量名prefix 一个 ’_‘ | ||
而link.ld中要prefix 两个 ’_‘*/ | ||
|
||
void (**constructor)() = &_CTOR_LIST__; | ||
|
||
//the first element is the number of constructors | ||
int total = *(int *)constructor; | ||
|
||
//constructor++; | ||
/* (可以先看一下链接脚本:Link.ld) | ||
Link script中修改过后,这里的total已经不是constructor的个数了, | ||
_CTOR_LIST__的第一个单元开始就是global/static对象的constructor, | ||
所以不用 constructor++; | ||
*/ | ||
|
||
while(total) //total不是constructor的数量,而是用于检测是否到了_CTOR_LIST__的末尾 | ||
{ | ||
(*constructor)(); | ||
//total--; | ||
constructor++; | ||
total = *(int *)constructor; | ||
} | ||
} | ||
|
||
extern "C" void _atexit() | ||
{ | ||
extern void (*_DTOR_LIST__)(); | ||
|
||
void (**deconstructor)() = &_DTOR_LIST__; | ||
|
||
int total = *(int *)deconstructor; | ||
|
||
deconstructor++; | ||
|
||
while(total) | ||
{ | ||
(*deconstructor)(); | ||
//total--; | ||
deconstructor++; | ||
total = *(int *)deconstructor; | ||
} | ||
} |
Oops, something went wrong.