从头写一个操作系统 08
我们一直用qemu来装载.bin文件,模拟计算机启动的过程,qemu很好,后面我们会讲到如何用gdb调试qemu,只能用强大来形容。
喜欢折腾的人,永远都不会闲下来,就像用vmware将二进制文件作为启动盘,用qemu模拟树莓派的启动一样,对于学习来说是有意义的。
今天介绍另一个很好用的虚拟机:bochs。
bochs完全用软件模拟了整个X86计算机的硬件逻辑,然后在其虚拟的硬件上插入虚拟的软盘,砰,系统就启动了。这世界本身就是信息,无论是真实的,还是虚拟的,只要对外输出一样的数据,我们是无法感知它们的区别,就像阿里云,我们以为在使用一台独立的主机,但实际上是一个服务器中小小的vamwar虚拟机。
扯远了,bochs比vmware虚拟得彻底的多,vmware总是希望让能在cpu上跑的指令就在cpu上跑,bochs完全不依赖硬件,cpu在其上不过是一个不断轮询、等待信息的函数。bochs与真实机器几乎没有区别,整个模拟过程接近于现实,当然qemu也是同样的原理。
用bochs的原因依然是集思广益,由于bochs专业性,debug的选项与gdb调试qemu是有些区别的,到底哪个好现在没有客观的评价,所以最好都会用,遇到问题多一个分析角度。
下面是介绍bochs在ubuntu18上的安装步骤,fedora28也测试过,需要下载库的名字与ubuntu上稍有区别:
1.下载bochs:
https://sourceforge.net/projects/bochs/files/bochs/2.6.9/bochs-2.6.9.tar.gz/download
2.解压
tar vxzf bochs-2.6.9.tar.gz
3.运行configure脚本
简书上有个大神的帖子里有 建议[1] :
注意:其中--prefix是安装地址
cd bochs-2.6.9
./configure --prefix=/home/happy/Documents/bochs --enable-debugger --enable-disasm --enable-iodebug --enable-x86-debugger --with-x --with-x11
也可以简单的:
```bash
cd bochs-2.6.9
./configure --enable-debugger --enable-disasm
4.make
make
sudo make install
最后,建立软链接:
cd /bin
ln -s ~/bochs.2.6.9/bochs /bin/bochs
ln -s ~/bochs.2.6.9/bximage /bin/bximage
错误排查:
1.出现 找不到X11/Xlib.h 错误:x11/xlib.h no such file or directory
ubuntu:
sudo apt-get install libx11-dev
fedora:
sudo dnf install libX*
如果未解决,执行下面指令再试:
make dist-clean
./configure --enbale-debugger --enable-disasm
make
make install
使用bochs的方法:
1.创建硬盘镜像 1->hd -> enter->enter...
bximage
2.nasm编译bin文件
nasm boot.asm -o boot.bin
3.dd写入镜像文件
dd if=boot.bin of=c.img bs=512 count=1 conv=notrunc
4.修改bochsrc.txt[2](链接指向官方img范例,是个压缩包,其中的配置文件既是本bochsrc.txt的原版文件,本人略作了修改)。
bochsrc.txt其实只是bochs启动时默认查找的配置文件,官方有详细的文档 ,这里要谢谢@cn-stem的细心提醒,推荐大家都加入
cn-stem标签,能够让文章更加规范哦。
如果按照上面的步骤,使用下面的bochsrc.txt即可:
###############################################################
# bochsrc.txt file for DLX Linux disk image.
###############################################################
# how much memory the emulated machine will have
megs: 32
# filename of ROM images
romimage: file=$BXSHARE/BIOS-bochs-latest
vgaromimage: file=$BXSHARE/VGABIOS-lgpl-latest
# what disk images will be used
floppya: 1_44=floppya.img, status=inserted
floppyb: 1_44=floppyb.img, status=inserted
# hard disk
ata0: enabled=1, ioaddr1=0x1f0, ioaddr2=0x3f0, irq=14
#ata0-master: type=disk, path="c.img", cylinders=306, heads=4, spt=17
ata0-master: type=disk, path="c.img", mode=flat
# choose the boot disk.
boot: c
# default config interface is textconfig.
#config_interface: textconfig
#config_interface: wx
#display_library: x
# other choices: win32 sdl wx carbon amigaos beos macintosh nogui rfb term svga
# where do we send log messages?
log: bochsout.txt
# disable the mouse, since DLX is text only
mouse: enabled=0
# set up IPS value and clock sync
cpu: ips=15000000
clock: sync=both
# enable key mapping, using US layout as default.
#
# NOTE: In Bochs 1.4, keyboard mapping is only 100% implemented on X windows.
# However, the key mapping tables are used in the paste function, so
# in the DLX Linux example I'm enabling keyboard_mapping so that paste
# will work. Cut&Paste is currently implemented on win32 and X windows only.
keyboard: keymap=$BXSHARE/keymaps/x11-pc-us.map
#keyboard: keymap=$BXSHARE/keymaps/x11-pc-fr.map
#keyboard: keymap=$BXSHARE/keymaps/x11-pc-de.map
#keyboard: keymap=$BXSHARE/keymaps/x11-pc-es.map
5.启动bochs,如果c.img文件没有问题,弹出的bochs窗口中选择6,然后bochs停在黑屏状态,在shell中输入c即可运行系统
Leave 从头写一个操作系统 08 to:
Read more #esteem posts
Best Posts From geyu
We have not curated any of geyu's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.