Tools and bootstrap

git 

A short history of Git

$ git

$ man git

$ man git-add  # add a dash '-' between git and the sub-command

$ git clone https://github.com/bitslab/xv6-public.git

Useful sub-commands: diff, stash, reset, log, etc..

Three sections of a git project:

How do they change:

Everything is possible:

Tools and tips

Declare some shortcuts at ~/.gitconfig.

Github: Use public key authentication.

tig is a very handy text-based git helper.

Creating a hello-world user program in xv6

There is no glibc or stdio.h in xv6. Everything you can use must be provided in the xv6 codebase.

Try some tools

Creating aliases in your ~/.bashrc) (rc is short for "run commands")

Creating two aliases 'gk' and 'gu' will be helpful for grepping kernel-only or user-only symbols in xv6: https://github.com/wuxb45/rc/wiki/Extra-aliases-for-xv6

User-space resources:

Debugging a user-space program

add-auto-load-safe-path ./

Show source code in gdb: gdb> tui enable

gdb cheatsheet: https://cs.brown.edu/courses/cs033/docs/guides/gdb.pdf

BIOS

https://en.wikipedia.org/wiki/BIOS

BIOS is a library & executable: contains the first instruction to run when a PC boots up (at 0xFFFF0)

BIOS is firmware: comes with the motherboard. Contains hardware-specific subroutines for upper-level OS (DOS).

Why do we need a firmware?

Why BIOS has become less relevant today?

Inspecting the MBR on your x86 PC

MBR: Master Boot Record

The first (512-byte) sector on the disk. Loaded to address 0x7c00 by BIOS.

Code starts to runs in real mode (the 16-bit mode).

Look into your laptop/pc's boot sector: (your laptop may not have it)

Who wrote/generated those code?

git clone git://git.savannah.gnu.org/grub.git

vi grub/grub-core/boot/i386/pc/boot.S

vi grub/grub-core/boot/i386/qemu/boot.S

The MBR is too small. It does some essential initialization and then loads a larger program into the memory to execute (the real grub boot loader).

6 Stages of Linux Boot Process (see below)

Read bootasm.S in xv6

Games that fit in an MBR: Tetris, Pillman, Invader, ... (note that they are all written in Intel syntax).

*The init process has been replaced by systemd in most Linux distros.

Creating your own mbr

We want to show something on the screen (say, character 'D') using a BIOS interrupt: Int 10h, AH=0Eh (Teletype output).

mymbr.S

.code16  # 16-bit real mode

.text

.global start

start:

  cli    # BIOS enabled interrupts; disable

  movb $0x0, %bh

  movb $0xe, %ah

  movb $'D', %al

  int  $0x10

spin:

  jmp spin

$ qemu-system-x86_64 a.out

or

$ qemu-system-i386 a.out

Debugging the MBR starting from the first instruction

The default 64-bit qemu does support debugging of 16-bit MBR. We will use a 32-bit qemu vm for this experiment (and for debugging your code in homework 2).

add-auto-load-safe-path ./

set architecture i8086

target remote localhost:1234

tui enable

layout asm

b *0x7c00

c

You can also manually type-in the commands without using .gdbinit. In this case, check and remove unwanted commands in .gdbinit.

Beware that the .gdbinit will be overwritten if you use make qemu-gdb. We DON'T use "make qemu-gdb" for debugging the MBR.

To start the vm: $ qemu-system-i386 -S -s a.out. This will start a 32-bit qemu vm and listen on the default port 1234

THEN, from another terminal at the same working directory: $ gdb

(gdb will try to load the .gdbinit at the current working directory, if it exists.)

After a half-second pause, you can see the gdb prompt with a message like this: "Breakpoint 1, 0x00007c00 in ?? ()"

Now you can debug the 16-bit MBR.

use si to "step instruction".

To show disassembly at some address (show 10 instructions): x/10i 0x7c00

The disassembly of xv6's boot sector can be found in bootblock.asm after a successful build (see Makefile for the rules that generate the disassembly).

Warning: qemu does not report 32-bit target type to gdb. You may see wrong disassembly when debugging an MBR. But the execution will still be correct.

Debugging the 64-bit kernel will be much easier.