從頭寫一個(gè)操作系統(tǒng) 12 (create an OS from scratch 12)

你可能需要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è)文件,bootsectokernel立宜。
可以直接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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市鄙信,隨后出現(xiàn)的幾起案子瞪醋,更是在濱河造成了極大的恐慌,老刑警劉巖装诡,帶你破解...
    沈念sama閱讀 219,188評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件银受,死亡現(xiàn)場離奇詭異,居然都是意外死亡鸦采,警方通過查閱死者的電腦和手機(jī)宾巍,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來渔伯,“玉大人顶霞,你說我怎么就攤上這事。” “怎么了选浑?”我有些...
    開封第一講書人閱讀 165,562評論 0 356
  • 文/不壞的土叔 我叫張陵蓝厌,是天一觀的道長。 經(jīng)常有香客問我古徒,道長拓提,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,893評論 1 295
  • 正文 為了忘掉前任隧膘,我火速辦了婚禮代态,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘舀寓。我一直安慰自己胆数,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,917評論 6 392
  • 文/花漫 我一把揭開白布互墓。 她就那樣靜靜地躺著必尼,像睡著了一般。 火紅的嫁衣襯著肌膚如雪篡撵。 梳的紋絲不亂的頭發(fā)上判莉,一...
    開封第一講書人閱讀 51,708評論 1 305
  • 那天,我揣著相機(jī)與錄音育谬,去河邊找鬼券盅。 笑死,一個(gè)胖子當(dāng)著我的面吹牛膛檀,可吹牛的內(nèi)容都是我干的锰镀。 我是一名探鬼主播,決...
    沈念sama閱讀 40,430評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼咖刃,長吁一口氣:“原來是場噩夢啊……” “哼泳炉!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起嚎杨,我...
    開封第一講書人閱讀 39,342評論 0 276
  • 序言:老撾萬榮一對情侶失蹤花鹅,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后枫浙,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,801評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡箩帚,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,976評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了锻狗。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,115評論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖轻纪,靈堂內(nèi)的尸體忽然破棺而出油额,到底是詐尸還是另有隱情刻帚,我是刑警寧澤潦嘶,帶...
    沈念sama閱讀 35,804評論 5 346
  • 正文 年R本政府宣布崇众,位于F島的核電站掂僵,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏顷歌。R本人自食惡果不足惜锰蓬,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,458評論 3 331
  • 文/蒙蒙 一眯漩、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧赦抖,春花似錦、人聲如沸队萤。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,008評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至赵辕,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間匆帚,已是汗流浹背旁钧。 一陣腳步聲響...
    開封第一講書人閱讀 33,135評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留嚎幸,地道東北人寄猩。 一個(gè)月前我還...
    沈念sama閱讀 48,365評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親替废。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,055評論 2 355

推薦閱讀更多精彩內(nèi)容