一、为什么需要搭建虚拟开发环境
操作系统作为软件,是从硬件设备加载,执行的的二进制代码。因此,从做一个可以快速加载启动的简单小系统开始,毕竟有成就感。也是后续开发运行的基础。
选择虚拟环境,而不是直接进行硬件加载,首先是方便开发。另外可以节省资源。虚拟环境就是采用软件模拟了硬件环境。
因此各种虚拟机、模拟器都可以用来做这样的工作。网络上有很多采用虚拟机来进行硬盘加载开发的,也有采用模拟器进行的。
我们这里先介绍采用一个主流的x86模拟器bochs进行搭建入门。后续再介绍采用虚拟机,以及其中的原理。
二、搭建bochs开发环境
2.1 环境安装
1、在linux或者macs上直接采用apt-get或者brew进行安装
(当前我采用的,因为采用源码在mac一直未正常编译通过)
下面给出在mac上进行安装到命令
# install the bochs
brew install bochs
# 检查命令行输出,查看是否正常安装
bochs -h
####outPut########
00000000000i[ ] LTDL_LIBRARY_PATH not set. using compile time default '/usr/local/Cellar/bochs/2.7/lib/bochs/plugins'
========================================================================
Bochs x86 Emulator 2.7
Built from SVN snapshot on August 1, 2021
Timestamp: Sun Aug 1 10:07:00 CEST 2021
========================================================================
Usage: bochs [flags] [bochsrc options]
-n no configuration file
-f configfile specify configuration file
-q quick start (skip configuration interface)
-benchmark N run Bochs in benchmark mode for N millions of emulated ticks
-dumpstats N dump Bochs stats every N millions of emulated ticks
-r path restore the Bochs state from path
-log filename specify Bochs log file name
-unlock unlock Bochs images leftover from previous session
-rc filename execute debugger commands stored in file
-dbglog filename specify Bochs internal debugger log file name
--help display this help and exit
--help features display available features / devices and exit
--help cpu display supported CPU models and exit
For information on Bochs configuration file arguments, see the
bochsrc section in the user documentation or the man page of bochsrc.
00000000000i[SIM ] quit_sim called with exit code 0
# install the sdl2, 后续bochs运行时进行图形化模拟到工具,可以暂时不装,等需要时安装
brew install sdl2
# install nasm,进行系统文件汇编的工具,后续会用到
brew install nasm
# 检查nasm版本,查看是否正常安装
nasm -v
####outPut########
NASM version 2.15.05 compiled on Aug 29 2020
2、采用源码进行编译bochs安装
下载地址
https://bochs.sourceforge.io/
安装参考
编译安装bochs
mac编译过程有比较多的报错需要处理,网上可以找打相关的解决方式,但是环境不同造成的影响和问题太多。后续放弃了。但后面bochs启动需要配置文件,可以从源码中拿到。当然熟悉后可以直接自己动手编写。
2.2 准备配置文件
将上一节中从源码中得到的.bochsrc拷贝到某个目录下,也就是后续进行系统文件放置和启动bochs的目录。
## 拷贝到自己开发的目录下
cp .bochsrc /User/test/bochsrc.txt
该文件作为后续bochs启动的配置文件,决定虚拟机启动的情况,修改检查其中特定的内容。
# 暂时不了解其涵义,可以百度设置
romimage: file=$BXSHARE/BIOS-bochs-latest
vgaromimage: file=$BXSHARE/VGABIOS-lgpl-latest
# 1.44=磁盘镜像位置,后面将在本地目录下放置a.img文件
floppya: 1_44=a.img, status=inserted
# 从软盘启动,熟悉后可以设置硬盘,光驱等各种方式
boot: floppy
# 日志输出文件
log: bochsout.txt
# 置鼠标不可用
mouse: enabled=0
keyboard: keymap=#$BXSHARE/keymaps/x11-pc-us.map
megs: 32
## 找到以下代码,进行注释
sound: driver=default, waveout=/dev/dsp. wavein=, midiout=
ata0-master: type=disk, mode=flat, path="30M.sample"
## 交互界面
display_library: sdl2 # startup in fullscreen mode
2.3 编写系统文件
创建一个文件
touch boot.asm
进行填充代码
org 0x7c00
jmp entry
db 0x90
entry:
mov ax,0
mov ss,ax
mov sp,0x7c00
mov ds,ax
mov es,ax
mov si,msg
putloop:
mov al,[si]
add si,1
cmp al,0
je fin
mov ah,0x0e
mov bx,15
int 0x10
jmp putloop
fin:
hlt
jmp fin
msg:
db 0x0a,0x0a
db "hello world!"
db 0x0a
db 0
times 510 - ($-$$) db 0
dw 0xaa55
编译
nasm boot.asm -o boot.bin
这里使用软盘方式进行加载,将引导程序写进软盘镜像。对于其他的方式有其他的加载方法。
dd if=boot.bin of=a.img bs=512 count=1 conv=notrunc
2.4 启动服务
bochs -f 配置文件位置,即
bochs -f bochsrc.txt
输出如下
在另外的窗口会开启该图形化窗口
三、部署遇到的问题
1、cpu占用率过高的解决
clock: sync=slowddown, time0=local
cpu: count=1, ips=1000000
clock指的是,将内部时钟调到最慢,cpu选项中count指的是使用单核心
cpu中,ips则是指指令的运行速度。可以通过更改iaips值和colck来降低CPU占用率,注意设置后ips为下限,再低启动bochs会报错提示。
这是以牺牲性能为代价。当然开发并不需要特别高的运行速度。
2. bochs开启后闪退
## 交互界面
display_library: sdl2, options="fullscreen" # startup in fullscreen mode
改为
## 交互界面
display_library: sdl2 # startup in fullscreen mode
取消其全屏模式
3. 图形界面不显示
00000000000i[ ] lt_dlhandle is 0x7fe11940fa70
00000000000i[PLUGIN] loaded plugin libbx_term_gui.so
00000000000i[ ] installing term module as the Bochs GUI
00000000000i[ ] using log file bochsout.txt
Bochs connected to screen "/dev/ttys005"
注意设置bochs中的display项目
参考:
Mac安装bochs,编译helloword操作系统
linux下Bochs模拟占用CPU过高问题解决
Bochs启动不显示窗口