Bochs搭建GeekOS平臺(轉)

轉自 http://blog.csdn.net/huangwei1024/archive/2007/02/21/1512176.aspx

一、前言

最近接觸的OS比較多,外加自己想多學點OS的知識,就在網上狂搜教學用的OS。除了Minix用VMware搭建外,看到還有個叫GeekOS的學習用OS,就拿來試試。

GeekOS  

What is it?

GeekOS is a tiny operating system kernel for x86 PCs.  Its main purpose is to serve as a simple but realistic example of an OS kernel running on real hardware .  (Actually, most of the development is done on the Bochs emulator .)

The goal of GeekOS is to be a tool for learning about operating system kernels.  As of version 0.2.0, it comes with a set of projects suitable for use in an undergraduate operating systems course, or for self-directed learning.  GeekOS has been used in courses at a number of colleges and universities.

看了下說明,心動了~~呵呵(a tool for learning about operating system kernels)

還得下Bochs來虛擬機。順便提下Bochs, Virtual PC 和 VMware的區別

摘自《自己動手寫操作系統》Bochs vs. Virtual PC vs. VMware

不知道開源給你的感覺是什麼,對筆者而言,它總是意味着親切、清新,身上沒有討厭的商業氣息。也許你也有同感,如果是的話,可能你已經對本書到現在才提到 Bochs 感到不滿。實際上筆者對 Bochs 其實是充滿好感的,尤其是它可以對操作系統進行調試,這樣方便的特性不能不讓我們對它青睞有加。理論上講, Bochs 可以完全取代 Virtual PC VMware ,但以筆者個人的體會,從一個操作系統開發者的角度來看, Bochs 大致有這麼幾個缺點:

  Virtual PC 有共享文件夾功能,可以方便地讀取 host 上的文件,而 Bochs 不可以。當我們學習保護模式時,免不了要用到 DOS ,用 Virtual PC 可以在 Windows 下編寫代碼,通過共享文件夾的方式在 DOS 下執行,而 Bochs 就沒這麼方便。

Virtual PC VMware 比起來, Bochs 的速度要慢得多,筆者曾經試圖在 Bochs 上安裝 Red Hat 9 ,但隨後就發現這是個愚蠢的決定,太慢了,不久筆者就把它中止掉了。

  Virtual PC 的圖形化界面要比 Bochs 好用。

讀者可能注意到了上文中“從一個操作系統開發者的角度來看”這一句,之所以這樣說,是因爲我們這樣武斷地將 Bochs 拿來跟 Virtual PC Vmware 相比是不公平的。雖然它們模擬同一個操作系統時的輸出可能差不多,但從實現機制上講,它們有巨大的不同。

Bochs 是一個模擬器,它完全模擬 x86 的硬件以及一些外圍設備。而 VMware 除了模擬一些特定的 I/O 之外,還可以用它的 x86 運行時引擎來完成其餘內容的執行。意思是說,當客戶 PC 試圖執行一些動作時, VMware 並不是利用自己重造的機制來解釋它,而是將它傳遞給實際的硬件。在這裏, VMware 的工作更應稱爲“虛擬( virtualize )”而不是“模擬( emulate )”。 Virtual PC 的原理介於兩者之間,它的一些部分是模擬出來的,另一部分則是虛擬出來的。 Virtual PC 能夠實現共享文件夾也是因爲同樣的原因。

由於 Bochs 在模擬一臺真正的機器的硬件,所以使得它比 VMware Virtual PC 都慢得多。

筆者曾經將 Tinix 分別用 Bochs 2.1.1 Virtual PC 5.0 VMware 3.2.0 和真實的機器進行比較測試,發現在 Virtual PC 上運行的效果跟真實的機器居然差不多,而 Bochs VMware 則慢得多,這可能跟它們實現機制的差異以及 Tinix 自身的特點有關。不過, Virtual PC 的確因爲速度、操作方便性等特點具備很強的吸引力,以至於本書最終將它而不是開源免費的 Bochs 作爲首選。

二、主要開發工具

 Bochs 2.3 http://bochs.sourceforge.net/

geekos-0.3.0 http://geekos.sourceforge.net/

cygwin http://cygwin.com/

cygwin的安裝全面已經介紹過了,就不再重複了,只是要注意需要有一下軟件:

Thefollowingis a complete list of software needed to compile GeekOS.
* gcc<http://gcc.gnu.org/>,version2.95.2orlater,targetinganyi386/ELFplatform,ortargeting
PECOFFunder Cygwin
* Any ANSI-compliant C compiler for the host platform (the computer you are planning to compile
GeekOSon);unlessyouarecross-compiling,thiscanbethesamecompileryouusetocompileGeekOS
* GNUbinutils<http://www.gnu.org/software/binutils/>,targetinganyi386/ELFplatform ,
or PECOFF under Cygwin; you probably alreadyhave this if you have gcc
* GNUMake<http://www.gnu.org/software/make/>;theGeekOSmakefilewillnotworkwith
other versions of make
* Perl<http://www.perl.org/ >, version5 or later
* egrep<http://www.gnu.org/software/grep/grep.html >
* AWK<http://www.gnu.org/software/gawk/ >
* diff3<http://www.gnu.org/software/diffutils/ >
* NASM <http://nasm.sourceforge.net/ >; on Linux or FreeBSD systems, this is probably the
only required software not already installed

在把GeekOS的壓縮包解壓在 /home 文件夾下,這樣它的路徑是 /home/geekos-0.3.0。

可以發現/home/geekos-0.3.0/src 有很多project文件夾,呵呵,不錯~~

這些是學習OS內核功能的各個版本,我們先來最簡單的project0。

三、開始試驗

Project 0 (Chapter 5, “Project 0: Getting Started”) serves as an introduction to modifying, building, and
running GeekOS.You will add akernel thread to read keys from the keyboard and echo them to the screen.

project0實現的功能就是讀鍵盤輸入並回顯。

運行cygwin,打入命令:

$ cd /home/geekos-0.3.0/src/project0/build

$ make depend

$ make

結束後,build文件夾下多了fd.img文件和.bochsrc文件,一個是引導盤的鏡像,一個是Bochs的配置文件。

把他們複製到Bochs的安裝目錄下。

但是這個配置文件好像不是Bochs2.3版本的,不需要newharddrivesupport參數。

幸好Bochs自帶了個sample文件,文件名是bochsrc-sample.txt,把名字改成.bochsrc。

在用自帶的bximage.exe作個硬盤鏡像,都按Enter就行了

打開編輯.bochsrc

floppya: 1_44=fd.img, status=inserted

ata0-master: type=disk, mode=flat, path="c.img", cylinders=0 # autodetect

boot: a

romimage: file=BIOS-bochs-latest, address=0xf0000

vgaromimage: file=VGABIOS-lgpl-latest

其實不用自己寫了的,用 # 就是註釋掉,選擇其他選項就好了。

 

運行Bochs,因爲已經配置好了,所以默認選項是[6]Begin Simulation,直接進入。

仔細看的話,會看見以軟盤啓動了。

Welcome to GeekOS 歡迎詞也出現了。第一個project就這樣搭建成功了,簡單吧。

四、小結

正因爲簡單,所以可以拿代碼來仔細研究下了。源代碼在 geekos-0.3.0/src/project0/src 下,呵呵。

一共有7個project,從官方的解釋,除了project0,其他project的學習目的是:

For Project 1 (Chapter 6, “Project 1: Loading Executable Files”), you become familiar with the structure
of an executable file. You are provided with code for loading and running executable files, but you need to
first become familiar with the ELF file format, then write code to parse the provided file and pass it to the
loader.

In Project 2 (Chapter 7, “Project 2: Adding Processes”), you will add support for user mode processes.
Rather than using virtual memory to provide separate user address spaces, this project uses segmentation,
which issimpler to understand.

Project 3 (Chapter 8, “Project 3: Scheduling”) improves the GeekOS scheduler and adds support for
semaphores to coordinate multiple processes.

Project4(Chapter9,“Project4: VirtualMemory”)replaces the segmentation based user memory protec-
tion added in Project 1 with paged virtual memory. Pages of memory can be stored on disk in order to free
up RAMwhen the demand for memory exceeds the amountavailable.

Project 5(Chapter10, “Project 5: AFilesystem”) adds a hierarchical read/writefile system to GeekOS.

Project 6 (Chapter 11, “Project 6: ACLs and Inter-process Communication”) adds access control lists
(ACLs)tothefilesystem,and adds interprocess communication using an onymoushalf-duplexpipes. Upon
the completion of this project, GeekOS will resemble a very simple versionof Unix.

以前從來沒試過用源代碼來編譯OS內核,還做成引導盤來引導,看來要好好學羅~~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章