bochs简介

bochs 是一个虚拟机(模拟器),能够完整的模拟一台计算机。详细来讲 bochs 是 X86 硬件平台的开源模拟器,完全可以通过软件来给我们提供各种所需的硬件资源。和 bochs 类似的虚拟机软件还有我们常用的 VMware、Virtuabox,但区别也是明显的。bochs 是完全依靠软件来模拟整个环境的:从启动到重启包括 PC 的外设键盘、鼠标、磁盘以及网卡等,全部都是由软件来模拟的,而其余软件则不然(部分依赖于硬件)。也就是说,bochs 可以从头到尾模拟整个硬件环境。它可以从PC机刚启动的那一刹那起就开始模拟。同时,bochs 带有强大的调试功,能够直接单步调试二进制文件,我们可以看到二进制代码在硬件上运行的每一步!后面咋们的操作系统都将由 bochs 运行

bochs安装与配置

本自制操作系统笔记系列都在 Ubuntu 下进行,关于 Bochs 在 Windows 下的使用,可参考 程序加载器 一文末尾。

  1. 前往 bochs官网 下载 2.7 版本:bochs-2.7.tar.gz 。必须为 2.7 版本,后面的 bochs 配置仅对此版本有效。

  2. 安装依赖:解压后进入 bochs-2.7 目录,在终端执行命令:

    1
    sudo apt-get install libx11-dev libc6-dev build-essential xorg-dev libgtk2.0-dev libreadline-dev
  3. 生成配置:

    1
    ./configure --with-x11 --with-x --enable-all-optimizations --enable-readline --enable-debugger-gui --enable-x86-debugger --enable-a20-pin --enable-fast-function-calls --enable-debugger
  4. 编译安装:

    1
    make -j4 && sudo make install

    安装完毕,但此时 bochs 还只是一台裸机,无法运行。

  5. bochsrc 配置:
    在 bochs 目录中创建 bochsrc,写入如下内容:

    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
    58
    59
    60
    61
    62
    63
    64
    65
    # configuration file generated by Bochs
    plugin_ctrl: unmapped=true, biosdev=true, speaker=true, extfpuirq=true, parallel=true, serial=true
    config_interface: textconfig

    # display_library: x
    # gdbstub: enabled=1, port=1234, text_base=0, data_base=0, bss_base=0

    # 这两个一起开启
    #魔术断点:
    magic_break: enabled=1
    #gui调试
    display_library: x, options="gui_debug"

    memory: host=32, guest=32
    romimage: file="/usr/local/share/bochs/BIOS-bochs-latest", address=0x00000000, options=none
    vgaromimage: file="/usr/local/share/bochs/VGABIOS-lgpl-latest"
    boot: disk
    floppy_bootsig_check: disabled=0
    # no floppya
    floppya: image="a.img", status=inserted
    # no floppyb
    ata0: enabled=true, ioaddr1=0x1f0, ioaddr2=0x3f0, irq=14
    ata0-master: type=disk, path="./build/hd.img", mode=flat
    ata0-slave: type=none
    ata1: enabled=true, ioaddr1=0x170, ioaddr2=0x370, irq=15
    ata1-master: type=none
    ata1-slave: type=none
    ata2: enabled=false
    ata3: enabled=false
    optromimage1: file=none
    optromimage2: file=none
    optromimage3: file=none
    optromimage4: file=none
    optramimage1: file=none
    optramimage2: file=none
    optramimage3: file=none
    optramimage4: file=none
    pci: enabled=1, chipset=i440fx
    vga: extension=vbe, update_freq=5, realtime=1
    cpu: count=1, ips=4000000, model=bx_generic, reset_on_triple_fault=1, cpuid_limit_winnt=0, ignore_bad_msrs=1, mwait_is_nop=0
    cpuid: level=6, stepping=3, model=3, family=6, vendor_string="GenuineIntel", brand_string=" Intel(R) Pentium(R) 4 CPU "
    cpuid: mmx=true, apic=xapic, simd=sse2, sse4a=false, misaligned_sse=false, sep=true
    cpuid: movbe=false, adx=false, aes=false, sha=false, xsave=false, xsaveopt=false, smep=false
    cpuid: smap=false, mwait=true
    print_timestamps: enabled=0
    # no gdb stub
    port_e9_hack: enabled=0
    private_colormap: enabled=0
    clock: sync=none, time0=local, rtc_sync=0
    # no cmosimage
    log: -
    logprefix: %t%e%d
    debug: action=ignore
    info: action=report
    error: action=report
    panic: action=ask
    keyboard: type=mf, serial_delay=250, paste_delay=100000, user_shortcut=none
    mouse: type=ps2, enabled=false, toggle=ctrl+mbutton
    speaker: enabled=true, mode=system
    parport1: enabled=true, file=none
    parport2: enabled=false
    com1: enabled=true, mode=null
    com2: enabled=false
    com3: enabled=false
    com4: enabled=false
  6. 接下来就需要创建硬盘,并写入 mbr 使其运行起来。详细内容参见:程序加载器文末。