Bochs源码安装及运行调试

最近在读《Orange's一个操作系统的实现》,开始部分介绍了虚拟机Bochs的安装及调试,本文把折腾过程做下记录。
操作系统:Ubuntu10.04(最新的版本老是有问题)
软件版本:bochs 2.3.5(和书中保持一致)

1 更新Ubuntu

由于ubuntu 10.04是很老的版本,早在2016年已停止了更新支持,直接sudo apt-get update会失败,好在ubuntu提供了old-release的方案,通过编辑sources.list更新软件源,打开/etc/apt/sources.list,把里面内容全部删除(如果不放心,可以先将原文件备份),然后换上如下地址:

deb http://old-releases.ubuntu.com/ubuntu lucid main restricted universe multiverse   
deb http://old-releases.ubuntu.com/ubuntu lucid-security main restricted universe multiverse   
deb http://old-releases.ubuntu.com/ubuntu lucid-updates main restricted universe multiverse   
deb http://old-releases.ubuntu.com/ubuntu lucid-proposed main restricted universe multiverse   
deb http://old-releases.ubuntu.com/ubuntu lucid-backports main restricted universe multiverse   
deb-src http://old-releases.ubuntu.com/ubuntu lucid main restricted universe multiverse   
deb-src http://old-releases.ubuntu.com/ubuntu lucid-security main restricted universe multiverse   
deb-src http://old-releases.ubuntu.com/ubuntu lucid-updates main restricted universe multiverse   
deb-src http://old-releases.ubuntu.com/ubuntu lucid-proposed main restricted universe multiverse   
deb-src http://old-releases.ubuntu.com/ubuntu lucid-backports main restricted universe multiverse  

之后,依次执行如下命令,更新并安装必要软件
sudo apt-get update
sudo apt-get install build-essential
sudo apt-get install xorg-dev
sudo apt-get install libgtk2.0-dev
sudo apt-get install nasm

2 软件编译

1 解压软件包
tar -zxf bochs-2.3.5.tar.gz
2 进入bochs解压目录
cd bochs-2.3.5
3 配置,使能调试和反汇编功能
./configure --enable-debugger --enable-disasm
4 编译
make
5 安装(该步骤可不进行)
sudo make install
编译过程中可能有一个如下错误
symbols.cc:137: error: ISO C++ forbids declaration of ‘hash_map’ with no type
需要对./bx_debug/symbols.cc进行修改

using namespace std;
#ifdef __GNUC__  // 新加的,注意前后都是两个下划线 
using namespace __gnu_cxx; // 新加的,注意前面是两个下划线 
#endif  // 新加的
struct symbol_entry_t;

3 运行实例

1 编辑boot.asm

org	07c00h			; 告诉编译器程序加载到7c00处
	mov	ax, cs
	mov	ds, ax
	mov	es, ax
	call	DispStr			; 调用显示字符串例程
	jmp	$			; 无限循环
DispStr:
	mov	ax, BootMessage
	mov	bp, ax			; ES:BP = 串地址
	mov	cx, 16			; CX = 串长度
	mov	ax, 01301h		; AH = 13,  AL = 01h
	mov	bx, 000ch		; 页号为0(BH = 0) 黑底红字(BL = 0Ch,高亮)
	mov	dl, 0
	int	10h			; 10h 号中断
	ret
BootMessage:		db	"Hello, OS world!"
times 	510-($-$$)	db	0	; 填充剩下的空间,使生成的二进制代码恰好为512字节
dw 	0xaa55				; 结束标志

2 生成引导文件boot.bin

nasm boot.asm -o boot.bin

3 制作虚拟硬盘

dd if=boot.bin of=a.img bs=512 count=1

4 编辑配置文件bochsrc

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

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

# filename of ROM images
romimage: file=./bios/BIOS-bochs-latest
vgaromimage: file=./bios/VGABIOS-lgpl-latest

# what disk images will be used
floppya: 1_44=a.img, status=inserted

# choose the boot disk.
boot: floppy

# where do we send log messages?
# log: bochsout.txt

# disable the mouse
mouse: enabled=0

# enable key mapping, using US layout as default.
keyboard_mapping: enabled=1, map=./gui/keymaps/x11-pc-us.map

注意配置文件中的相对路径

5 运行实例

./bochs -f bochsrc

输入6回车

输入c回车

可见虚拟机已按照程序输出"Hello, OS world!",Ctrl+C后输入exit退出运行。

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