-Makefile編- xv6 (mips) コードリーディング

QEMUで実行できる形に最適化されているので、実機(FPGA)で動作させる場合はコードの手直しが必要かもしれない。

xv6.img, fs.img

QEMUで実行する際に

qemu-nox: fs.img xv6.img
    $(QEMU) -nographic $(QEMUOPTS)

となるが、これは下記のようなコマンドとして実行される

qemu-system-mipsel -nographic -hdb fs.img -kernel kernel -smp 1 -m 256 -M mips

hdb   : block device file
kernel: Use bzImage as kernel image
smp   : the number of simulation processor
m     : RAM size (MB)
M     : Set the emulated machine type

original vs. mips

MIPS版では、bootblock(Master Boot Record)を使わない。実機で動かす場合はこの辺の手直しが必要?

original

xv6.img: bootblock kernel fs.img
    dd if=/dev/zero of=xv6.img count=10000
    dd if=bootblock of=xv6.img conv=notrunc
    dd if=kernel of=xv6.img seek=1 conv=notrunc

bootblock: bootasm.S bootmain.c
    $(CC) $(CFLAGS) -fno-pic -O -nostdinc -I. -c bootmain.c
    $(CC) $(CFLAGS) -fno-pic -nostdinc -I. -c bootasm.S
    $(LD) $(LDFLAGS) -N -e start -Ttext 0x7C00 -o bootblock.o bootasm.o bootmain.o
    $(OBJDUMP) -S bootblock.o > bootblock.asm
    $(OBJCOPY) -S -O binary -j .text bootblock.o bootblock
    ./sign.pl bootblock

mips

xv6.img: kernel fs.img
    dd if=/dev/zero of=xv6.img count=10000
    dd if=kernel of=xv6.img seek=1 conv=notrunc

じゃあどうやってxv6-mipsは動作しているのか?

結論から書くと、xv6-mipsQEMU上での動作しかサポートしていない。QEMUはあくまでエミュレータなので、BIOSにあたるもの(MBRセクタ)がなくても問題ないらしい。実機ではおそらく何かしら

There currently is no MIPS BIOS file for QEMU (see firmware). However if passed a -kernel argument qemu will not call the firmware at all, so this does no harm at all. Since QEMU 0.8.1 this workaround is obsolete, a missing BIOS file triggers only a warning message.

Firmware

The QEMU distribution does not contain any firmware for MIPS. This is only a minor problem as unlike on a real system QEMU's virtual hardware is mostly initialized after a reset. At least some existing firmware for MIPS Malta also works with QEMU, e.g. RedBoot. There is also a mmon port to QEMU.

https://www.linux-mips.org/wiki/QEMU#MIPS_BIOS_not_found_on_startup https://www.linux-mips.org/wiki/QEMU#Firmware

そもそもファームウェアとは?

x86でいうBIOSのように、ハードウェアとソフトウェアの間にあるもの。電源投入と同時に実行される。主な働きはハードウェアを初期化して記憶装置からブートローダーを呼び出すこと。 ハード的に見たら、ProgramCounterが0に初期化された後、すぐ実行される位置に格納されているProgram、という感じなのかな?実装がよくわからない。

Firmware is the system software residing in a non-volatile memory, typically a ROM, EPROM or today often a flash memory. What is called firmware for the MIPS systems is relatively close to what is called the BIOS for the PC sector. There is a small number of firmware implementations in use on MIPS only however in a large number of variants.

ARC
Common Firmware Environment (CFE)
Cobalt Firmware
CoLo
IDT/sim, IDT/boot
mmon tiny monitor program for MIPS VR4300
PMON
PMON 2000
RedBoot
U-Boot
YAMON

実機でのfirmware実装のヒント

UTのCPU実験、GAIA上で動作させたxv6がヒントになるかも。カーネルビルドの最後にヘッダ部分に4bytesのサイズを書き込んでる。この辺はプロセッサ回路がどう設計されてるかで、firmware実装も変わってきそうな気がする。

kernelmemfs: $(MEMFSASMS) initcode _min-rt fs.img
    ./tools/gen_binary_blobs 0x80002000 initcode _min-rt fs.img
    $(AS) $(ASFLAGS) -c -o _kernelmemfs -e 0x80002000 -start _start $(MEMFSASMS) _binary_blobs.s $(UCCLIBS) -f __UCC_HEAP_START
    ./tools/gen_binary_blobs `ruby -e "print open('_kernelmemfs').size + 0x80002000"` initcode _min-rt fs.img
    $(AS) $(ASFLAGS) -c -o _kernelmemfs -e 0x80002000 -start _start $(MEMFSASMS) _binary_blobs.s $(UCCLIBS) -f __UCC_HEAP_START
    cat _kernelmemfs initcode _min-rt fs.img > kernelmemfs
    rm _kernelmemfs
    ./tools/attach_boot_header kernelmemfs

xv6/Makefile at 94d02ecb2b9492365574f2166cf29ce5a170abf5 · nyuichi/xv6 · GitHub