你可能需要google: kernel, ELF format, makefile
目標(biāo): 寫一個(gè)簡單的kernel,讓bootsect啟動(dòng)它
The kernel
我們用C語言寫的內(nèi)核只能做一點(diǎn)點(diǎn)事,就是在屏幕左上角打印一個(gè)'X'膜赃,打開這個(gè)kernel.c
些举。
你會(huì)發(fā)現(xiàn)第一個(gè)函數(shù)中什么都沒寫铐望,這個(gè)函數(shù)創(chuàng)建了指向main
函數(shù)的內(nèi)核入口扶平。
添加-g的原因是在調(diào)試中需要用到這些信息清女,最后鏈接器鏈接kernel.o 與kernel_entry.o時(shí)會(huì)用到這些信息生成.elf玩徊,而這個(gè).elf最終會(huì)用在gdb remote 到qemu中調(diào)試時(shí)用到的榛。
i386-elf-gcc -g -ffreestanding -c kernel.c -o kernel.o
調(diào)用main
的程序是 kernel_entry.asm
琼了。打開學(xué)習(xí)學(xué)習(xí)匯編中[extern]
聲明的用法。編譯這個(gè)文件的elf
格式生成kernel.o
,注意,這次的最終文件不是.bin
的機(jī)器碼文件雕薪。
nasm kernel_entry.asm -f elf -o kernel_entry.o
The linker
鏈接器是非常有用的工具昧诱,我們剛才只使用到它一點(diǎn)點(diǎn)能力。
要鏈接兩個(gè).obj文件所袁,并且解析標(biāo)簽盏档,運(yùn)行:
注意:這里的kernel_entry.o kernle.o兩個(gè)文件的順序不能顛倒,否則程序找不到入口燥爷!
i386-elf-ld -o kernel.bin -Ttext 0x1000 kernel_entry.o kernel.o --oformat binary
注意蜈亩,內(nèi)核代碼可不是被放在內(nèi)存的0x0
位置上,而是放在了0x1000
,(譯注:進(jìn)入保護(hù)模式后會(huì)跳轉(zhuǎn)到這個(gè)地址執(zhí)行內(nèi)核代碼前翎,指定地址后稚配,鏈接器會(huì)將所有跳轉(zhuǎn)(段內(nèi))指令的目標(biāo)地址加上0x1000
)。引導(dǎo)程序需要知道這個(gè)地址港华。這和lesson10中bootsect.asm
很像道川。
The bootsector
編譯它:
nasm bootsect.asm -f bin -o bootsect.bin
Putting it all together
現(xiàn)在我們分別由兩個(gè)文件,bootsecto
和 kernel
立宜。
可以直接link
它們嗎冒萄?哈哈,當(dāng)然橙数,把他們接在一起就可以:
cat bootsect.bin kernel.bin > os-image.bin
Run!
用qemu運(yùn)行宦言!
注意,如果你遇到硬盤載入錯(cuò)誤之類的情況商模,可能需要調(diào)整硬盤的編號或者qemu的參數(shù),我通常這么做:
qemu-system-i386 -fda os-image.bin
如果成功蜘澜,會(huì)看到如下信息:
- "Started in 16-bit Real Mode"
- "Loading kernel into memory"
- (Top left) "Landed in 32-bit Protected Mode"
- (Top left, overwriting previous message) "X"
Congratulations!
THE ORIGIN ARTICALE IN GITHUB:[^1]
Concepts you may want to Google beforehand: kernel, ELF format, makefile
Goal: Create a simple kernel and a bootsector capable of booting it
The kernel
Our C kernel will just print an 'X' on the top left corner of the screen. Go ahead and open kernel.c
.
You will notice a dummy function that does nothing. That function will force us to create a kernel entry routine which does not point to byte 0x0 in our kernel, but to an actual label which we know that launches it. In our case, function main()
.
i386-elf-gcc -ffreestanding -c kernel.c -o kernel.o
That routine is coded on kernel_entry.asm
. Read it and you will learn how to use [extern]
declarations in assembly. To compile this file, instead of generating a binary, we will generate an elf
format file which will be linked with kernel.o
nasm kernel_entry.asm -f elf -o kernel_entry.o
The linker
A linker is a very powerful tool and we only started to benefit from it.
To link both object files into a single binary kernel and resolve label references, run:
i386-elf-ld -o kernel.bin -Ttext 0x1000 kernel_entry.o kernel.o --oformat binary
Notice how our kernel will be placed not at 0x0
in memory, but at 0x1000
. The bootsector will need to know this address too.
The bootsector
It is very similar to the one in lesson 10. Open bootsect.asm
and examine the code. Actually, if you remove all the lines used to print messages on the screen, it accounts to a couple dozen lines.
Compile it with nasm bootsect.asm -f bin -o bootsect.bin
Putting it all together
Now what? We have two separate files for the bootsector and the kernel?
Can't we just "link" them together into a single file? Yes, we can, and it's easy, just concatenate them:
cat bootsect.bin kernel.bin > os-image.bin
Run!
You can now run os-image.bin
with qemu.
Remember that if you find disk load errors you may need to play with the disk numbers or qemu parameters (floppy = 0x0
, hdd = 0x80
). I usually use qemu-system-i386 -fda os-image.bin
You will see four messages:
- "Started in 16-bit Real Mode"
- "Loading kernel into memory"
- (Top left) "Landed in 32-bit Protected Mode"
- (Top left, overwriting previous message) "X"
Congratulations!
Makefile
As a last step, we will tidy up the compilation process with a Makefile. Open the Makefile
script and examine its contents. If you don't know what a Makefile is, now is a good time to Google and learn it, as this will save us a lot of time in the future.
參考資料:
[1]:https://github.com/cfenollosa/os-tutorial/blob/master/12-kernel-c
版權(quán)注明:本文所有涉及到:https://github.com/cfenollosa/os-tutorial/
git倉庫的內(nèi)容施流,全部對應(yīng)以下開源協(xié)議聲明:
BSD 3-Clause License
Copyright (c) 2018,
Carlos Fenollosa