Open source virtual machine Bochs installation and stepping on the pit

Because you want to write a simple operating system, you need to install a virtual machine to simulate the hardware. VMware is not suitable for this scenario, because you will use hardware level virtualization, and bochs, the open-source virtual machine, uses software to virtualize all the hardware, so debugging can be very fine-grained. For example, every time the cpu executes a command, we can pause and look at the register State, look at the memory state, which is too much help for operating system development and debugging. So we use bochs as a virtual machine.

mac install bochs

My current environment is Mac version 10.15.3. Record the installation process and the steps.

Steps:

  • Install sdl library, which will be used in subsequent compilation
brew install sdl
  • Download the source code and extract it

Download address: https://sourceforge.net/projects/bochs/files/latest/download

# Download decompression
tar -xvf bochs-2.6.tar.gz
# Parameters required for configure

./configure --enable-ne2000 \
            --enable-all-optimizations \
            --enable-cpu-level=6 \
            --enable-x86-64 \
            --enable-vmx=2 \
            --enable-pci \
            --enable-usb \
            --enable-usb-ohci \
            --enable-e1000 \
            --enable-debugger \
            --enable-disasm \
            --disable-debugger-gui \
            --with-sdl \
            --prefix=$HOME/opt/bochs

The prefix parameter here specifies the installation location and changes it to the desired address.

Here comes the first question:

Question 1

Report this error

cdrom_osx.cc:194:18: error: assigning to 'char ' from incompatible type 'const ch

So I checked it on the Internet. There is a patch for this error report[ https://raw.githubusercontent.com/Homebrew/formula-patches/e9b520dd4c/bochs/xcode9.patch ], we find this file and modify the source code. This file is in bochs-2.6/iodev/hdimage/cdrom_osx.cc. We open and modify line 193

if ((devname = strrchr(devpath, '/')) != NULL) {
//Changed to:
if ((devname = (char *) strrchr(devpath, '/')) != NULL) {

The problem is solved. Let's compile.

make && make install

Question two

Report this mistake at this time

config.cc:3261:55: error: ordered comparison between pointer and zero ('char *' and 'int')
    if (SIM->get_param_string("model", base)->getptr()>0) {
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
1 error generated.

We open config.cc, find line 3621, and change it to

modify config.cc The 3621 line
if (SIM->get_param_string("model", base)->getptr()>0) {
//by
if (SIM->get_param_string("model", base)->getptr()>(char *)0) {

Yes.

And then we compile it again

make && make install

At this time, the normal compilation is successful.

We can add bochs to the environment variable to use:

export BXSHARE="$HOME/workplace/os/bochs/share/bochs"
export PATH="$PATH:$HOME/workplace/os/bochs/bin"

Bochs use

bochs/share/doc/bochs/bochsrc-sample.txt in the installation directory of bochs is the template of the configuration file. We write a simple configuration file to run:

# Set virtual machine memory to 32MB
megs: 32

# Set BIOS image
romimage: file=$BXSHARE/BIOS-bochs-latest 

# Set VGA BIOS image
vgaromimage: file=$BXSHARE/VGABIOS-lgpl-latest

# Set boot from hard disk
boot: disk

# Set log file
log: bochsout.txt

# Close mouse
mouse: enabled=0

# Open keyboard
keyboard: type=mf, serial_delay=250

# Set up hard disk
ata0: enabled=1, ioaddr1=0x1f0, ioaddr2=0x3f0, irq=14

# Add gdb remote debugging support
gdbstub: enabled=1, port=1234, text_base=0, data_base=0, bss_base=0

Save as bochsrc. Run command execution in this directory:

➜  my_src git:(master) ✗ bochs
========================================================================
                        Bochs x86 Emulator 2.6
            Built from SVN snapshot on September 2nd, 2012
                  Compiled on Feb 11 2020 at 14:52:19
========================================================================
00000000000i[     ] reading configuration from bochsrc
00000000000p[     ] >>PANIC<< bochsrc:26: Bochs is not compiled with gdbstub support
========================================================================
Bochs is exiting with the following message:
[     ] bochsrc:26: Bochs is not compiled with gdbstub support
========================================================================
00000000000i[CPU0 ] CPU is in real mode (active)
00000000000i[CPU0 ] CS.mode = 16 bit
00000000000i[CPU0 ] SS.mode = 16 bit
00000000000i[CPU0 ] EFER   = 0x00000000
00000000000i[CPU0 ] | EAX=00000000  EBX=00000000  ECX=00000000  EDX=00000000
00000000000i[CPU0 ] | ESP=00000000  EBP=00000000  ESI=00000000  EDI=00000000
00000000000i[CPU0 ] | IOPL=0 id vip vif ac vm rf nt of df if tf sf ZF af PF cf
00000000000i[CPU0 ] | SEG sltr(index|ti|rpl)     base    limit G D
00000000000i[CPU0 ] |  CS:0000( 0000| 0|  0) 00000000 00000000 0 0
00000000000i[CPU0 ] |  DS:0000( 0000| 0|  0) 00000000 00000000 0 0
00000000000i[CPU0 ] |  SS:0000( 0000| 0|  0) 00000000 00000000 0 0
00000000000i[CPU0 ] |  ES:0000( 0000| 0|  0) 00000000 00000000 0 0
00000000000i[CPU0 ] |  FS:0000( 0000| 0|  0) 00000000 00000000 0 0
00000000000i[CPU0 ] |  GS:0000( 0000| 0|  0) 00000000 00000000 0 0
00000000000i[CPU0 ] | EIP=00000000 (00000000)
00000000000i[CPU0 ] | CR0=0x00000000 CR2=0x00000000
00000000000i[CPU0 ] | CR3=0x00000000 CR4=0x00000000
bx_dbg_read_linear: physical memory read error (phy=0x0000000000000000, lin=0x0000000000000000)
00000000000i[CTRL ] quit_sim called with exit code 1

At this point, we successfully run a virtual machine.

Analog hard disk

bochs provides a tool to create a virtual hard disk, bximage, which provides an interactive way to create a virtual hard disk. Let's do the following:

➜  my_src git:(master) ✗ bximage
========================================================================
                                bximage
                  Disk Image Creation Tool for Bochs
          $Id: bximage.c 11315 2012-08-05 18:13:38Z vruppert $
========================================================================

Do you want to create a floppy disk image or a hard disk image?
Please type hd or fd. [hd] // Enter, new hard disk

What kind of image should I create?
Please type flat, sparse or growing. [flat] // Enter, flat hard disk

Enter the hard disk size in megabytes, between 1 and 8257535
[10] 10 // 10MB hard disk

I will create a 'flat' hard disk image with
  cyl=20
  heads=16
  sectors per track=63
  total sectors=20160
  total size=9.84 megabytes

What should I name the image?
[c.img] hd10m.img   // Name

Writing: [] Done.

I wrote 10321920 bytes to hd10m.img.

The following line should appear in your bochsrc:
  ata0-master: type=disk, path="hd10m.img", mode=flat, cylinders=20, heads=16, spt=63
➜  my_src git:(master) ✗ ls
bochsrc   hd10m.img

Now, that's the end of the environment configuration, and we'll learn how to write the startup information later.

Welcome to my Blog And github( https://github.com/veeupup)!

Tags: C++ git Mac github Vmware

Posted on Tue, 11 Feb 2020 04:26:15 -0500 by racer x