Bochs搭建GeekOS平台

來源:互聯網
上載者:User
一、前言

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

 

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核心,還做成引導盤來引導,看來要好好學羅~~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.