1. 程式人生 > >自己動手寫作業系統之1:bochs初步使用

自己動手寫作業系統之1:bochs初步使用

0x00 前言

自己動手寫作業系統,自然需要一個承載程式的執行環境,這一章記錄我使用bochs遇到的坑,凌晨十二點半才填完坑。

這次記錄的是使用bochs完整實現一個最簡單的作業系統(其實根本不算作業系統,只是通過硬碟啟動個mbr程式)。

0x01 bochs

1.安裝

安裝比較簡單了,我沒有使用原始碼安裝,不想再折騰那個了,不然估計我就沒戲能開始了。

yum install bochs bochs-x -y

需要裝兩個東東,後面一個如果不過,會報錯,如果報錯可以看我後面的錯誤記錄。

2.配置檔案

bochs需要一個配置檔案,來描述硬體資訊。檔案裡面有詳細的註釋。

配置檔案就叫bochs.disk,名字可以隨意,字尾是什麼也無所謂。

###############################################################
# Configuration file for Bochs
###############################################################

# how much memory the emulated machine will have
megs: 32

# 對應真實機器的BIOS和VGA BIOS
romimage: file=/usr/share/bochs/BIOS-bochs-latest
vgaromimage
: file=/usr/share/bochs/VGABIOS-lgpl-latest # 設定bochs使用的磁碟,軟盤使用關鍵字floppya,硬碟使用disk # 若有多個軟盤,可寫floppya,floppyb #floppya: 1_44=a.img, status=inserted # choose the boot disk. # 預設是軟盤,註釋掉,改為disk #boot: floppy boot: disk # where do we send log messages? log: /tmp/bochsout.txt # disable the mouse mouse: enabled=0
# enable key mapping, using US layout as default. keyboard_mapping: enabled=1, map=/usr/share/bochs/keymaps/x11-pc-us.map ata0: enabled=1, ioaddr1=0x1f0, ioaddr2=0x3f0, irq=14 #這一句是根據bximage生成的,後面會解釋。 ata0-master: type=disk, path="/home/dante/workspace/os/hd60M.img", mode=flat, cylinders=121, heads=16, spt=63

3.製作虛擬硬碟

這裡使用bochs配備的bximage。

root@dante-pc:/home/dante/workspace/os# bximage -hd -mode='flat' -size=60 -q hd60M.img

========================================================================
                                bximage
                  Disk Image Creation Tool for Bochs
          $Id: bximage.c 11315 2012-08-05 18:13:38Z vruppert $
========================================================================

I will create a 'flat' hard disk image with
  cyl=121
  heads=16
  sectors per track=63
  total sectors=121968
  total size=59.55 megabytes

Writing: [] Done.

I wrote 62447616 bytes to hd60M.img.

The following line should appear in your bochsrc:
  ata0-master: type=disk, path="hd60M.img", mode=flat, cylinders=121, heads=16, spt=63

看到輸出的最後一句,指示我們把這句寫入bochs的配置檔案中。

4.程式寫入硬碟

此處的mbr.bin是通過彙編編譯後生成的檔案,下一篇部落格專門記錄這次實現的彙編程式碼。

程式碼實現的內容就是在螢幕上列印幾個字元。

××注意:×如無必要,後面的引數就不要改了,如果想知道每個引數改了×改後在啟動的時候會出錯。不過也可以看一下錯誤資訊多瞭解點知識。

root@dante-pc:/home/dante/workspace/os# dd if=mbr.bin of=hd60M.img bs=512 count=1 conv=notrunc
記錄了0+1 的讀入
記錄了0+1 的寫出
502位元組(502 B)已複製,0.000140041 秒,3.6 MB/秒

5.啟動

bochs -f bochs.disk

這樣就能看到在介面上列印的字元了。

0x02 錯誤列表

錯誤1

錯誤資訊

Event type: PANIC
Device: [     ]
Message: dlopen failed for module 'x': file not found

A PANIC has occurred.  Do you want to:
  cont       - continue execution
  alwayscont - continue execution, and don't ask again.
               This affects only PANIC events from device [     ]
  die        - stop execution now
  abort      - dump core 
  debug      - continue and return to bochs debugger
Choose one of the actions above: [die] 

分析和解決

這是由於在安裝的時候,少安裝了個bochs-x包,安裝即可。

sudo apt-get install bochs-x

錯誤2

錯誤資訊

# bochs -f bochs.disk 
========================================================================
                        Bochs x86 Emulator 2.6
            Built from SVN snapshot on September 2nd, 2012
                  Compiled on Aug 10 2015 at 10:27:14
========================================================================
00000000000i[     ] LTDL_LIBRARY_PATH not set. using compile time default '/usr/lib/bochs/plugins'
00000000000i[     ] BXSHARE not set. using compile time default '/usr/share/bochs'
00000000000i[     ] lt_dlhandle is 0x3ae4fe0
00000000000i[PLGIN] loaded plugin libbx_unmapped.so
00000000000i[     ] lt_dlhandle is 0x3ae6ba0
00000000000i[PLGIN] loaded plugin libbx_biosdev.so
00000000000i[     ] lt_dlhandle is 0x3ae74e0
00000000000i[PLGIN] loaded plugin libbx_speaker.so
00000000000i[     ] lt_dlhandle is 0x3ae7d50
00000000000i[PLGIN] loaded plugin libbx_extfpuirq.so
00000000000i[     ] lt_dlhandle is 0x3ae8590
00000000000i[PLGIN] loaded plugin libbx_parallel.so
00000000000i[     ] lt_dlhandle is 0x3aea240
00000000000i[PLGIN] loaded plugin libbx_serial.so
00000000000i[     ] lt_dlhandle is 0x3aede20
00000000000i[PLGIN] loaded plugin libbx_gameport.so
00000000000i[     ] lt_dlhandle is 0x3aee8c0
00000000000i[PLGIN] loaded plugin libbx_iodebug.so
00000000000i[     ] reading configuration from bochs.disk
00000000000p[     ] >>PANIC<< reading from bochs.disk failed
00000000000e[CTRL ] notify called, but no bxevent_callback function is registered
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

分析和解決

將boch.disk檔案中的vgaromimage: /usr/share/vgabios/vgabios.bin改為vgaromimage: file=/usr/share/vgabios/vgabios.bin即可,出現這種錯誤的,很多是因為直接使用了orange‘s的程式碼。

錯誤3

錯誤資訊

啟動bochs後沒有反應,一直是黑屏。

......
00000000000e[     ] bochs.disk:28: 'keyboard_mapping' will be replacedby new 'keyboard' option.
00000000000i[     ] lt_dlhandle is 0x36ac110
00000000000i[PLGIN] loaded plugin libbx_x.so
00000000000i[     ] installing x module as the Bochs GUI
00000000000i[     ] using log file /tmp/bochsout.txt
Next at t=0
(0) [0x00000000fffffff0] f000:fff0 (unk. ctxt): jmp far f000:e05b         ; ea5be000f0
<bochs:1>

分析和解決

因為網路不好,也沒有翻牆,官網進不去,在一個論壇裡面看到,直接輸入c接可以了。

錯誤4

錯誤資訊

啟動後出現無啟動裝置的錯誤。

Boot failed: not a bootable device. 
FATAL:No bootable device.

分析和解決

這個問題我也忘了具體是怎麼改的了,不過根據回憶,應該是和程式相關,我中間改過一次程式,沖洗編譯並使用dd寫入後就沒問題了。

我和網上其它人遇到的錯誤情形還不太一樣。

2016-04-07 00:56:00 hnds