Series directory

  • introductory
  • The preparatory work
  • BIOS starts in real mode
  • GDT and Protected Mode
  • Exploration of Virtual Memory
  • Load and enter the kernel
  • Display and print
  • The global descriptor table GDT
  • Interrupt handling
  • Virtual memory improvement
  • Implement heap and malloc
  • The first kernel thread
  • Multithreading switch
  • The lock is synchronized with multithreading
  • Implementation of a process
  • Enter user mode
  • A simple file system
  • Load the executable program
  • Implementation of system calls
  • The keyboard driver
  • To run a shell

Install the Bochs

Bochs is a hardware emulator on which the kernel we write will run and on which all subsequent development and debugging will take place, so the first step is to install it. My development environment is Linux Mint, and you can install it directly with Ubuntu:

sudo apt install bochs

If this method does not work, you can go directly to the official website to download the source code package compilation and installation, this method I have tried is OK. However, in the installation process will encounter some third party dependent on the library is not complete, then find a way to meet the water bridge, the dependence on the installation on the line, here I will not detail, you need to think about ways to do it yourself, not very difficult, there are some tutorials online can refer to.

I haven’t tried any of the other systems, but compiling the source package installation certainly works.

Run the Bochs

First you need a configuration file for Bochs to run, bochsrc.txt, which I used:

# RAM size
megs: 32
# Change to your Bochs installation path
romimage: file=/usr/share/bochs/BIOS-bochs-latest
vgaromimage: file=/usr/share/bochs/VGABIOS-lgpl-latest

# Disk
boot: disk
ata0: enabled=1, ioaddr1=0x01f0, ioaddr2=0x03f0, irq=14
ata0-master: type=disk, path="scroll.img", mode=flat, cylinders=6, heads=16, spt=63

log: bochsout.txt

mouse: enabled=0
keyboard_mapping: enabled=1, map=/usr/share/bochs/keymaps/x11-pc-us.map
clock: sync=realtime
cpu: ips=1000000

Here is the most important part of the Disk configuration, we use a Disk, this is the boot loader and the kernel image Disk, the system is to use this Disk to boot. Of course it’s not a real disk. It’s a mirrored file.

Where path=”scroll. Img “is the mirror file, it is in the same path as the configuration file bochsrc.txt, so it is easy. Name it whatever you want, mine is Scroll, and we’ll talk about why later. Cylinders, heads, SPT are some of the parameters related to hard disk hardware, don’t go into details about what cylinders, cylinders and the like.

Of course, we don’t have any kernel images yet, but Bochs comes with a command line tool called bximage that will create an image file full of zeros for you and help you determine the above parameters. You just need to tell it how big your image file needs to be, in MB:

>> 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] hd What kind of image should I create? Please type flat, sparse or growing. [flat] flat Enter the hard disk size in megabytes, between 1 and 8257535 [10] 1 I will create a 'flat' hard disk image with cyl=2 heads=16 sectors per track=63 total Sectors =2016 total size=0.98 megabytes What should I name the image? Sectors =2016 total size=0.98 megabytes What should I name the image? [c.img] test Writing: [] Done. I wrote 1032192 bytes to test. The following line should appear in your bochsrc: ata0-master: type=disk, path="test", mode=flat, cylinders=2, heads=16, spt=63

Everything is ready to run Bochs:

bochs -f bochsrc.txt

If you’re lucky, you’ll see this screen, indicating that the startup was successful:

Establishing the project

So far we haven’t built a project yet, so go to Git and build one. I’ll give you my Git project address first, you can refer to it first, you can also download it and run the./start.sh script inside, see the operation effect.

The name I gave this project is scroll, which means scroll in Chinese, and that’s what it is:

A scroll is a piece of paper, a picture scroll. In our case, it’s memory, disk, starting with a blank space, and that brush is CPU. The whole kernel starts, runs, essentially is a dialogue between CPU and memory, the CPU jumps back and forth on the memory, flips and flips, sketches out a huge complex engineering system, this is OS.

Starting with the next post, we will officially enter OS boot: BIOS boot into real mode.