linux-0.01 読み (5)

うにまがの 2002.8 な記事は bootsect 関連。

とりあえず boot.s

以下なコメントが冒頭にあります。

; boot.s is loaded at 0x7c00 by the bios-startup routines, and moves itself
; out of the way to address 0x90000, and jumps there.
;
; It then loads the system at 0x10000, using BIOS interrupts. Thereafter
; it disables all interrupts, moves the system down to 0x0000, changes
; to protected mode, and calls the start of system. System then must
; RE-initialize the protected mode in it's own tables, and enable
; interrupts as needed.

ええと、この記事は 2.4.17 なカーネルを基にしてる模様。ソースな記述は引用させて頂きます。

0035: BOOTSEG = 0x07C0
0036: INITSEG = DEF_INITSEG

0060: _start:

0064: movw $BOOTSEG %ax
0065: movw %ax, %ds
0066: movw $INITSEG, %ax
0067: movw %ax, %es
0068: movw $256, %cx
0069: subw %si, %si
0070: subw %di, %di
0071: cld
0072: rep
0073: movsw
0074: ljmp $INITSEG, $go

上記ですが、0.01 な boot/boot.s の以下に該当すると思われます。

BOOTSEG = 0x07c0
INITSEG = 0x9000
SYSSEG  = 0x1000			; system loaded at 0x10000 (65536).
ENDSEG	= SYSSEG + SYSSIZE

entry start
start:
	mov	ax,#BOOTSEG
	mov	ds,ax
	mov	ax,#INITSEG
	mov	es,ax
	mov	cx,#256
	sub	si,si
	sub	di,di
	rep
	movw
	jmpi	go,INITSEG

やっぱり src と dst の順が逆だなぁ、と思いつつ 2.6.18 な arch/i386/boot/bootsect.S の中身を見てみたのですが、先頭コメントが以下。

/*
 *	bootsect.S		Copyright (C) 1991, 1992 Linus Torvalds
 *
 *	modified by Drew Eckhardt
 *	modified by Bruce Evans (bde)
 *	modified by Chris Noe (May 1999) (as86 -> gas)
 *	gutted by H. Peter Anvin (Jan 2003)

as86 から gas に書き換えられたのが 1999 年な模様。確かに 0.01 なナニは as86 で云々って事で

# apt-cache search as86
bin86 - 16-bit x86 assembler and loader
#

を入れたのでした。as86 は

命令 dst src

という形なのか。

それは良いとして

中身を確認。うにまがによれば start: から movw までの一連の命令列が 0x07c0 から 0x9000 へのコピーになっているとの事。成程ッスねぇ。
で、最後の jmpi でコピー後の go なオフセットに jmp してるとの事。魔術だ。プログラムって所詮、どこまでメモリが見えてるかですわ。バイナリアン怖るべし。
と言いつつ go 以降をナニするリキがありませんので、今日はこれで終了。