由於想要寫一個簡單的操做系統,因此須要安裝虛擬機來模擬出硬件,VMware不適合這個場景,由於會使用硬件級別的虛擬化,而bochs這個開源虛擬機,是用軟件虛擬了全部的硬件,因此調試能夠作到很是細的粒度,好比每次cpu執行命令,咱們均可以暫停,看寄存器狀態,看內存狀態,這對於操做系統開發調試的幫助太大太大了。因此咱們使用bochs這個虛擬機來。git
個人當前環境是Mac 版本是10.15.3,記錄下安裝的過程和踩的坑。github
步驟:shell
brew install sdl
下載地址:https://sourceforge.net/projects/bochs/files/latest/downloadxcode
# 下載解壓 tar -xvf bochs-2.6.tar.gz # configure 所須要的參數 ./configure --enable-ne2000 \ --enable-all-optimizations \ --enable-cpu-level=6 \ --enable-x86-64 \ --enable-vmx=2 \ --enable-pci \ --enable-usb \ --enable-usb-ohci \ --enable-e1000 \ --enable-debugger \ --enable-disasm \ --disable-debugger-gui \ --with-sdl \ --prefix=$HOME/opt/bochs
這裏的 prefix 參數指定了安裝的位置,修改爲本身想要的地址。app
這裏出現了第一個問題:工具
報這個錯誤學習
cdrom_osx.cc:194:18: error: assigning to 'char ' from incompatible type 'const ch
因而在網上查了一下,這個報錯有個補丁[https://raw.githubusercontent.com/Homebrew/formula-patches/e9b520dd4c/bochs/xcode9.patch],咱們找到這個文件而且修改源碼,這個文件在 bochs-2.6/iodev/hdimage/cdrom_osx.cc
,咱們打開修改第 193 行ui
if ((devname = strrchr(devpath, '/')) != NULL) { 改成: if ((devname = (char *) strrchr(devpath, '/')) != NULL) {
問題解決,咱們接着編譯。spa
make && make install
這個時候報這個錯操作系統
config.cc:3261:55: error: ordered comparison between pointer and zero ('char *' and 'int') if (SIM->get_param_string("model", base)->getptr()>0) { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~ 1 error generated.
咱們打開config.cc,找到3621行,修改成
修改config.cc的3621行 if (SIM->get_param_string("model", base)->getptr()>0) { 爲 if (SIM->get_param_string("model", base)->getptr()>(char *)0) {
便可。
而後咱們再次編譯
make && make install
此時便可正常編譯成功。
咱們能夠將 bochs 加入環境變量以便使用:
export BXSHARE="$HOME/workplace/os/bochs/share/bochs" export PATH="$PATH:$HOME/workplace/os/bochs/bin"
bochs的安裝目錄下的bochs/share/doc/bochs/bochsrc-sample.txt
是配置文件的模板。咱們本身編寫一個簡單的配置文件來運行:
# 設置虛擬機內存爲32MB megs: 32 # 設置BIOS鏡像 romimage: file=$BXSHARE/BIOS-bochs-latest # 設置VGA BIOS鏡像 vgaromimage: file=$BXSHARE/VGABIOS-lgpl-latest # 設置從硬盤啓動 boot: disk # 設置日誌文件 log: bochsout.txt # 關閉鼠標 mouse: enabled=0 # 打開鍵盤 keyboard: type=mf, serial_delay=250 # 設置硬盤 ata0: enabled=1, ioaddr1=0x1f0, ioaddr2=0x3f0, irq=14 # 添加gdb遠程調試支持 gdbstub: enabled=1, port=1234, text_base=0, data_base=0, bss_base=0
保存爲 bochsrc 。在該目錄運行命令執行:
➜ my_src git:(master) ✗ bochs ======================================================================== Bochs x86 Emulator 2.6 Built from SVN snapshot on September 2nd, 2012 Compiled on Feb 11 2020 at 14:52:19 ======================================================================== 00000000000i[ ] reading configuration from bochsrc 00000000000p[ ] >>PANIC<< bochsrc:26: Bochs is not compiled with gdbstub support ======================================================================== Bochs is exiting with the following message: [ ] bochsrc:26: Bochs is not compiled with gdbstub support ======================================================================== 00000000000i[CPU0 ] CPU is in real mode (active) 00000000000i[CPU0 ] CS.mode = 16 bit 00000000000i[CPU0 ] SS.mode = 16 bit 00000000000i[CPU0 ] EFER = 0x00000000 00000000000i[CPU0 ] | EAX=00000000 EBX=00000000 ECX=00000000 EDX=00000000 00000000000i[CPU0 ] | ESP=00000000 EBP=00000000 ESI=00000000 EDI=00000000 00000000000i[CPU0 ] | IOPL=0 id vip vif ac vm rf nt of df if tf sf ZF af PF cf 00000000000i[CPU0 ] | SEG sltr(index|ti|rpl) base limit G D 00000000000i[CPU0 ] | CS:0000( 0000| 0| 0) 00000000 00000000 0 0 00000000000i[CPU0 ] | DS:0000( 0000| 0| 0) 00000000 00000000 0 0 00000000000i[CPU0 ] | SS:0000( 0000| 0| 0) 00000000 00000000 0 0 00000000000i[CPU0 ] | ES:0000( 0000| 0| 0) 00000000 00000000 0 0 00000000000i[CPU0 ] | FS:0000( 0000| 0| 0) 00000000 00000000 0 0 00000000000i[CPU0 ] | GS:0000( 0000| 0| 0) 00000000 00000000 0 0 00000000000i[CPU0 ] | EIP=00000000 (00000000) 00000000000i[CPU0 ] | CR0=0x00000000 CR2=0x00000000 00000000000i[CPU0 ] | CR3=0x00000000 CR4=0x00000000 bx_dbg_read_linear: physical memory read error (phy=0x0000000000000000, lin=0x0000000000000000) 00000000000i[CTRL ] quit_sim called with exit code 1
此時咱們便成功運行了一個虛擬機。
bochs提供了一個建立虛擬硬盤的工具 bximage,提供了交互的方式建立虛擬硬盤,咱們來作一下:
➜ my_src git:(master) ✗ bximage ======================================================================== bximage Disk Image Creation Tool for Bochs $Id: bximage.c 11315 2012-08-05 18:13:38Z vruppert $ ======================================================================== Do you want to create a floppy disk image or a hard disk image? Please type hd or fd. [hd] // 回車,新建硬盤 What kind of image should I create? Please type flat, sparse or growing. [flat] // 回車,flat硬盤 Enter the hard disk size in megabytes, between 1 and 8257535 [10] 10 // 10MB大小硬盤 I will create a 'flat' hard disk image with cyl=20 heads=16 sectors per track=63 total sectors=20160 total size=9.84 megabytes What should I name the image? [c.img] hd10m.img // 名稱 Writing: [] Done. I wrote 10321920 bytes to hd10m.img. The following line should appear in your bochsrc: ata0-master: type=disk, path="hd10m.img", mode=flat, cylinders=20, heads=16, spt=63 ➜ my_src git:(master) ✗ ls bochsrc hd10m.img
好了,環境配置到此結束,咱們以後將學習如何編寫啓動信息。
歡迎訪問個人博客和github(https://github.com/veeupup)!