1. bochs所需环境

安装sdl库

1
brew install sdl

2. 安装bochs

用brew默认安装最新版,我这里是Bochs x86 Emulator 2.6.9

1
brew install bochs

3. 终端测试

终端输入

1
bochs

若看到

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
========================================================================
Bochs x86 Emulator 2.6.9
Built from SVN snapshot on April 9, 2017
Compiled on Dec 23 2018 at 17:15:13
========================================================================
00000000000i[ ] LTDL_LIBRARY_PATH not set. using compile time default '/usr/local/Cellar/bochs/2.6.9_2/lib/bochs/plugins'
00000000000i[ ] BXSHARE not set. using compile time default '/usr/local/Cellar/bochs/2.6.9_2/share/bochs'
00000000000i[ ] lt_dlhandle is 0x7fd7cc600220
00000000000i[PLUGIN] loaded plugin libbx_usb_common.so
00000000000i[ ] lt_dlhandle is 0x7fd7cc40ee00
00000000000i[PLUGIN] loaded plugin libbx_unmapped.so
00000000000i[ ] lt_dlhandle is 0x7fd7cc529eb0
00000000000i[PLUGIN] loaded plugin libbx_biosdev.so
00000000000i[ ] lt_dlhandle is 0x7fd7cc6007e0
00000000000i[PLUGIN] loaded plugin libbx_speaker.so
00000000000i[ ] lt_dlhandle is 0x7fd7cc600e30
00000000000i[PLUGIN] loaded plugin libbx_extfpuirq.so
00000000000i[ ] lt_dlhandle is 0x7fd7cc6012c0
00000000000i[PLUGIN] loaded plugin libbx_parallel.so
00000000000i[ ] lt_dlhandle is 0x7fd7cc602810
00000000000i[PLUGIN] loaded plugin libbx_serial.so
00000000000i[ ] lt_dlhandle is 0x7fd7cc605840
00000000000i[PLUGIN] loaded plugin libbx_iodebug.so
------------------------------
Bochs Configuration: Main Menu
------------------------------

This is the Bochs Configuration Interface, where you can describe the
machine that you want to simulate. Bochs has already searched for a
configuration file (typically called bochsrc.txt) and loaded it if it
could be found. When you are satisfied with the configuration, go
ahead and start the simulation.

You can also start bochs with the -q option to skip these menus.

1. Restore factory default configuration
2. Read options from...
3. Edit options
4. Save options to...
5. Restore the Bochs state from...
6. Begin simulation
7. Quit now

Please choose one: [2]

则说明,安装成功。

4. 测试“Hello world”输出

4.1 构建bochsrc.txt配置文件

因为homebrew安装的软件路径都在

1
/usr/local/Cellar/

文件夹下;我们找到bochs中的bochsrc-sample.txt文件在

1
/usr/local/Cellar/bochs/2.6.9_2/share/doc/bochs/bochsrc-sample.txt

复制,粘贴到新建文件夹(用来做os lab)的地方。

4.2 修改bochsrc-sample.txt部分代码为bochsrc.txt

找到如下两行并注释掉:

1
2
3
sound: driver=default, waveout=/dev/dsp. wavein=, midiout=
...
ata0-master: type=disk, mode=flat, path="30M.sample"

检查如下几处并做改为(记得某一名称只能出现一次,其余注释):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
romimage: file=$BXSHARE/BIOS-bochs-latest 

vgaromimage: file=$BXSHARE/VGABIOS-lgpl-latest

# 1.44=磁盘镜像位置
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

4.3 新建镜像文件,用以模拟硬件

首先,bximage命令创建一个软盘镜像a.img(对应配置文件中的floppya):

1

即建立成功。

4.4 构建引导程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
    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

编译:

1
nasm boot.asm -o boot.bin

记得保证之前的a.imgboot.binboot.asmbochsrc.txt在一个文件夹下

将引导程序写进软盘镜像

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

4.5 测试

启动bochs

bochs -f 配置文件位置

1
bochs -f bochsrc.txt

也可直接运行bochs ,进入后键入2来更改配置文件路径,键入6运行虚拟机

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
 fzy@fuujiro-MacBook-Pro  ~/fuujiro/fuujiroCode/OS_lab  bochs -f bochsrc.txt

========================================================================
Bochs x86 Emulator 2.6.9
Built from SVN snapshot on April 9, 2017
Compiled on Dec 23 2018 at 17:15:13
========================================================================
00000000000i[ ] LTDL_LIBRARY_PATH not set. using compile time default '/usr/local/Cellar/bochs/2.6.9_2/lib/bochs/plugins'
00000000000i[ ] BXSHARE not set. using compile time default '/usr/local/Cellar/bochs/2.6.9_2/share/bochs'
00000000000i[ ] lt_dlhandle is 0x7fe5df520a70
00000000000i[PLUGIN] loaded plugin libbx_usb_common.so
00000000000i[ ] lt_dlhandle is 0x7fe5df700340
00000000000i[PLUGIN] loaded plugin libbx_unmapped.so
00000000000i[ ] lt_dlhandle is 0x7fe5df700720
00000000000i[PLUGIN] loaded plugin libbx_biosdev.so
00000000000i[ ] lt_dlhandle is 0x7fe5df700ab0
00000000000i[PLUGIN] loaded plugin libbx_speaker.so
00000000000i[ ] lt_dlhandle is 0x7fe5df418810
00000000000i[PLUGIN] loaded plugin libbx_extfpuirq.so
00000000000i[ ] lt_dlhandle is 0x7fe5df6003b0
00000000000i[PLUGIN] loaded plugin libbx_parallel.so
00000000000i[ ] lt_dlhandle is 0x7fe5df6017f0
00000000000i[PLUGIN] loaded plugin libbx_serial.so
00000000000i[ ] lt_dlhandle is 0x7fe5df604950
00000000000i[PLUGIN] loaded plugin libbx_iodebug.so
00000000000i[ ] reading configuration from bochsrc.txt
00000000000e[ ] bochsrc.txt:926: wrong value for parameter 'mode'
00000000000e[PCSPK ] bochsrc.txt:926: unknown parameter for speaker ignored.
------------------------------
Bochs Configuration: Main Menu
------------------------------

This is the Bochs Configuration Interface, where you can describe the
machine that you want to simulate. Bochs has already searched for a
configuration file (typically called bochsrc.txt) and loaded it if it
could be found. When you are satisfied with the configuration, go
ahead and start the simulation.

You can also start bochs with the -q option to skip these menus.

1. Restore factory default configuration
2. Read options from...
3. Edit options
4. Save options to...
5. Restore the Bochs state from...
6. Begin simulation
7. Quit now

Please choose one: [6] 6
00000000000i[ ] lt_dlhandle is 0x7fe5df700b20
00000000000i[PLUGIN] loaded plugin libbx_sdl2.so
00000000000i[ ] installing sdl2 module as the Bochs GUI
00000000000i[SDL2 ] maximum host resolution: x=2880 y=1800
00000000000i[ ] using log file bochsout.txt
Next at t=0
(0) [0x0000fffffff0] f000:fff0 (unk. ctxt): jmpf 0xf000:e05b ; ea5be000f0
<bochs:1> c

2

评论