ホーム 研究内容 FreeBSD HP電卓 今日のgoo Links

--ホーム--
研究内容
FreeBSD
HP電卓
--Links--
BSD
Security
Hardware
NetHack
HP電卓

locore.sの解剖

「はじめにlocore.sありき」という格言(?)にしたがって,locore.sの解説を 行ないます.

目次

エントリーポイント
スタックフレームの準備
エプソンの286マシンのメモリ容量の取得
CPUタイプの識別
BSSの初期化
APMの初期化
ページングの準備
ブートストラップスタックの準備
CPUの初期化
非インテルCPUの初期化
メイン関数の呼びだし

btext: カーネル・エントリーポイント

ブートブロックからカーネルファイル(/kernel)に制御が移るとき,eipは btextを示しています.そこには,

	NON_GPROF_ENTRY(btext)
	#ifdef PC98
		jmp	1f
		.globl	_pc98_system_parameter
		.org	0x400
	_pc98_system_parameter:
		.space	0x240		/* skip over warm boot shit */
	1:
		/* save SYSTEM PARAMETER for resume (NS/T or other) */
		movl	$0xa1000,%esi
		movl	$0x100000,%edi
		movl	$0x0630,%ecx
		cld
		rep
		movsb
	#else	/* IBM-PC */
		....
	#endif	/* PC98 */
となっています.始めの0x640は,リアルモード時の割り込みベクタテーブル とBIOSワークエリアのために確保しています.BIOSワークエリアには,
pc98_system_parameter
というシンボルが定義してあります.カーネルはまず始めに第2キャラクター 画面用の領域にコピーしてあった割り込みベクタおよびBIOSワークエリアの内 容を上記の領域にコピーします.

スタックフレームの準備


/* Set up a real frame in case the double return in newboot is executed. */
	pushl	%ebp
	movl	%esp, %ebp

/* Don't trust what the BIOS gives for eflags. */
	pushl	$PSL_KERNEL
	popfl

/*
 * Don't trust what the BIOS gives for %fs and %gs.  Trust the bootstrap
 * to set %cs, %ds, %es and %ss.
 */
	mov	%ds, %ax
	mov	%ax, %fs
	mov	%ax, %gs

	call	recover_bootinfo
この部分に到達した段階で,fsおよびgsやeflagsがどのようになっているのかを 一意に決定することができません.そこでこれらの値を明示的に代入します. fsとgsはdsのコピーになります.eflagsは,0x2になります.そして,ブートブロックからの情報を取得します.

/* Get onto a stack that we can trust. */
/*
 * XXX this step is delayed in case recover_bootinfo needs to return via
 * the old stack, but it need not be, since recover_bootinfo actually
 * returns via the old frame.
 */
	movl	$R(tmpstk),%esp
ブートブロックからの情報を取得したら,スッタクをtmpstkからの 0x2000バイトの領域に切替えます.

ブートブロックからの情報の取得


recover_bootinfo:
	/*
	 * This code is called in different ways depending on what loaded
	 * and started the kernel.  This is used to detect how we get the
	 * arguments from the other code and what we do with them.
	 *
	 * Old disk boot blocks:
	 *	(*btext)(howto, bootdev, cyloffset, esym);
	 *	[return address == 0, and can NOT be returned to]
	 *	[cyloffset was not supported by the FreeBSD boot code
	 *	 and always passed in as 0]
	 *	[esym is also known as total in the boot code, and
	 *	 was never properly supported by the FreeBSD boot code]
	 *
	 * Old diskless netboot code:
	 *	(*btext)(0,0,0,0,&nfsdiskless,0,0,0);
	 *	[return address != 0, and can NOT be returned to]
	 *	If we are being booted by this code it will NOT work,
	 *	so we are just going to halt if we find this case.
	 *
	 * New uniform boot code:
	 *	(*btext)(howto, bootdev, 0, 0, 0, &bootinfo)
	 *	[return address != 0, and can be returned to]
	 *
	 * There may seem to be a lot of wasted arguments in here, but
	 * that is so the newer boot code can still load very old kernels
	 * and old boot code can load new kernels.
	 */

	/*
	 * The old style disk boot blocks fake a frame on the stack and
	 * did an lret to get here.  The frame on the stack has a return
	 * address of 0.
	 */
	cmpl	$0,4(%ebp)
	je	olddiskboot

	/*
	 * We have some form of return address, so this is either the
	 * old diskless netboot code, or the new uniform code.  That can
	 * be detected by looking at the 5th argument, if it is 0
	 * we are being booted by the new uniform boot code.
	 */
	cmpl	$0,24(%ebp)
	je	newboot

	/*
	 * Seems we have been loaded by the old diskless boot code, we
	 * don't stand a chance of running as the diskless structure
	 * changed considerably between the two, so just halt.
	 */
	 hlt

	/*
	 * We have been loaded by the new uniform boot code.
	 * Let's check the bootinfo version, and if we do not understand
	 * it we return to the loader with a status of 1 to indicate this error
	 */
newboot:
	movl	28(%ebp),%ebx		/* &bootinfo.version */
	movl	BI_VERSION(%ebx),%eax
	cmpl	$1,%eax			/* We only understand version 1 */
	je	1f
	movl	$1,%eax			/* Return status */
	leave
	/*
	 * XXX this returns to our caller's caller (as is required) since
	 * we didn't set up a frame and our caller did.
	 */
	ret

1:
	/*
	 * If we have a kernelname copy it in
	 */
	movl	BI_KERNELNAME(%ebx),%esi
	cmpl	$0,%esi
	je	2f			/* No kernelname */
	movl	$MAXPATHLEN,%ecx	/* Brute force!!! */
	movl	$R(_kernelname),%edi
	cmpb	$'/',(%esi)		/* Make sure it starts with a slash */
	je	1f
	movb	$'/',(%edi)
	incl	%edi
	decl	%ecx
1:
	cld
	rep
	movsb

2:
	/*
	 * Determine the size of the boot loader's copy of the bootinfo
	 * struct.  This is impossible to do properly because old versions
	 * of the struct don't contain a size field and there are 2 old
	 * versions with the same version number.
	 */
	movl	$BI_ENDCOMMON,%ecx	/* prepare for sizeless version */
	testl	$RB_BOOTINFO,8(%ebp)	/* bi_size (and bootinfo) valid? */
	je	got_bi_size		/* no, sizeless version */
	movl	BI_SIZE(%ebx),%ecx
got_bi_size:

	/*
	 * Copy the common part of the bootinfo struct
	 */
	movl	%ebx,%esi
	movl	$R(_bootinfo),%edi
	cmpl	$BOOTINFO_SIZE,%ecx
	jbe	got_common_bi_size
	movl	$BOOTINFO_SIZE,%ecx
got_common_bi_size:
	cld
	rep
	movsb

#ifdef NFS
	/*
	 * If we have a nfs_diskless structure copy it in
	 */
	movl	BI_NFS_DISKLESS(%ebx),%esi
	cmpl	$0,%esi
	je	olddiskboot
	movl	$R(_nfs_diskless),%edi
	movl	$NFSDISKLESS_SIZE,%ecx
	cld
	rep
	movsb
	movl	$R(_nfs_diskless_valid),%edi
	movl	$1,(%edi)
#endif

	/*
	 * The old style disk boot.
	 *	(*btext)(howto, bootdev, cyloffset, esym);
	 * Note that the newer boot code just falls into here to pick
	 * up howto and bootdev, cyloffset and esym are no longer used
	 */
olddiskboot:
	movl	8(%ebp),%eax
	movl	%eax,R(_boothowto)
	movl	12(%ebp),%eax
	movl	%eax,R(_bootdev)

#if defined(USERCONFIG_BOOT) && defined(USERCONFIG)
#ifdef PC98
	movl	$0x90200, %esi
#else
	movl	$0x10200, %esi
#endif
	movl	$R(_userconfig_from_boot),%edi
	movl	$512,%ecx
	cld
	rep
	movsb
#endif /* USERCONFIG_BOOT */

	ret

エプソンの286マシンのメモリ容量の取得

一部のEPSON PC-286シリーズのBIOSは,1MB以上のメモリ(プロテクトメモリあ るいはextended memory)の容量をワークエリアにセットしません.そこで,と りあえず16MB以下の空間にどれだけのメモリが存在するのか検査します.

#ifdef PC98
	testb	$0x02,0x100620		/* pc98_machine_type & M_EPSON_PC98 */
	jz	3f
	cmpb	$0x0b,0x100624		/* epson_machine_id <= 0x0b */
	ja	3f

	/* count up memory */
	movl	$0x100000,%eax		/* next, talley remaining memory */
	movl	$(0xFFF-0x100),%ecx
1:	movl	0(%eax),%ebx		/* save location to check */
	movl	$0xa55a5aa5,0(%eax)	/* write test pattern */
	cmpl	$0xa55a5aa5,0(%eax)	/* does not check yet for rollover */
	jne	2f
	movl	%ebx,0(%eax)		/* restore memory */
	addl	$ PAGE_SIZE,%eax
	loop	1b
2:	subl	$0x100000,%eax
	shrl	$17,%eax
	movb	%al,0x100401
3:
#endif

CPUタイプの識別


	call	identify_cpu

identify_cpu:

	/* Try to toggle alignment check flag; does not exist on 386. */
	pushfl
	popl	%eax
	movl	%eax,%ecx
	orl	$PSL_AC,%eax
	pushl	%eax
	popfl
	pushfl
	popl	%eax
	xorl	%ecx,%eax
	andl	$PSL_AC,%eax
	pushl	%ecx
	popfl

	testl	%eax,%eax
	jnz	1f
	movl	$CPU_386,R(_cpu)
	jmp	3f
まず,eflagsのACビットが変更可能かどうか検査します.486以上のCPUでは変 更可能で,386では変更不可能です.

1:	/* Try to toggle identification flag; does not exist on early 486s. */
	pushfl
	popl	%eax
	movl	%eax,%ecx
	xorl	$PSL_ID,%eax
	pushl	%eax
	popfl
	pushfl
	popl	%eax
	xorl	%ecx,%eax
	andl	$PSL_ID,%eax
	pushl	%ecx
	popfl

	testl	%eax,%eax
	jnz	1f
	movl	$CPU_486,R(_cpu)
次に,cpuidをサポートしているかどうか検査しま す.もしサポートしていなければ,
  1. 古いIntel 486 CPU
  2. 古いAmd 486 CPU
  3. Cyrixの486,5x86および6x86
  4. IBM Blue Lightning CPU
のどれかです.これらのCPUである(cpuidをサポートしていない)場合, Intel/AMDのCPUかCyrixのCPUか検査します.

	/* check for Cyrix 486DLC -- based on check routine  */
	/* documented in "Cx486SLC/e SMM Programmer's Guide" */
	xorw	%dx,%dx
	cmpw	%dx,%dx			# set flags to known state
	pushfw
	popw	%cx			# store flags in ecx
	movw	$0xffff,%ax
	movw	$0x0004,%bx
	divw	%bx
	pushfw
	popw	%ax
	andw	$0x08d5,%ax		# mask off important bits
	andw	$0x08d5,%cx
	cmpw	%ax,%cx

	jnz	3f			# if flags changed, Intel chip
CyrixのCPUの場合は,変数をセットします.IBM-PCの場合はここでCPUキャッ シュの設定を行ないます(省略).

	movl	$CPU_486DLC,R(_cpu) # set CPU value for Cyrix
	movl	$0x69727943,R(_cpu_vendor)	# store vendor string
	movw	$0x0078,R(_cpu_vendor+4)

#ifndef CYRIX_CACHE_WORKS

		snipped...

#endif /* !CYRIX_CACHE_WORKS */
	jmp	3f
CPUがcpuidをサポートしている場合は,その情報を 用いてCPUを識別します.

1:	/* Use the `cpuid' instruction. */
	xorl	%eax,%eax
	.byte	0x0f,0xa2			# cpuid 0
	movl	%eax,R(_cpu_high)		# highest capability
	movl	%ebx,R(_cpu_vendor)		# store vendor string
	movl	%edx,R(_cpu_vendor+4)
	movl	%ecx,R(_cpu_vendor+8)
	movb	$0,R(_cpu_vendor+12)

	movl	$1,%eax
	.byte	0x0f,0xa2			# cpuid 1
	movl	%eax,R(_cpu_id)			# store cpu_id
	movl	%edx,R(_cpu_feature)		# store cpu_feature
	rorl	$8,%eax				# extract family type
	andl	$15,%eax
	cmpl	$5,%eax
	jae	1f

	/* less than Pentium; must be 486 */
	movl	$CPU_486,R(_cpu)
	jmp	3f
1:
	/* a Pentium? */
	cmpl	$5,%eax
	jne	2f
	movl	$CPU_586,R(_cpu)
	jmp	3f
2:
	/* Greater than Pentium...call it a Pentium Pro */
	movl	$CPU_686,R(_cpu)
3:
	ret

BSSの初期化


	movl	$R(_end),%ecx
	movl	$R(_edata),%edi
	subl	%edi,%ecx
	xorl	%eax,%eax
	cld
	rep
	stosb

APMの初期化


#if NAPM > 0
/*
 * XXX it's not clear that APM can live in the current environonment.
 * Only pc-relative addressing works.
 */
	call	_apm_setup
#endif

ページング


	call	create_pagetables

create_pagetables:

	testl	$CPUID_PGE, R(_cpu_feature)
	jz	1f
	movl	%cr4, %eax
	orl	$CR4_PGE, %eax
	movl	%eax, %cr4
1:


/* Find end of kernel image (rounded up to a page boundary). */
	movl	$R(_end),%esi

		snipped...

	addl	$PAGE_MASK,%esi
	andl	$~PAGE_MASK,%esi
	movl	%esi,R(_KERNend)	/* save end of kernel */
	movl	%esi,R(physfree)	/* next free page is at end of kernel */
まず,ページ単位に切り上げたカーネルイメージの終了アドレスを, KERNendおよびphysfreeに格納します.

/* Allocate Kernel Page Tables */
	ALLOCPAGES(NKPT)
	movl	%esi,R(_KPTphys)
ALLOCPAGESマクロを用いて,NKPTページの大きさ のKernel Page Tableを確保します.先頭アドレスがKPTphysに格納 されます.

/* Allocate Page Table Directory */
	ALLOCPAGES(1)
	movl	%esi,R(_IdlePTD)
次に,Page Table Directoryを1ページ確保します.先頭アドレスが IdlePTDに格納されます.

/* Allocate UPAGES */
	ALLOCPAGES(UPAGES)
	movl	%esi,R(p0upa)
	addl	$KERNBASE, %esi
	movl	%esi, R(_proc0paddr)
そして,UPAGESページのUPAGEを確保します.

/* Allocate proc0's page table for the UPAGES. */
	ALLOCPAGES(1)
	movl	%esi,R(p0upt)

/* Map read-only from zero to the end of the kernel text section */
	xorl	%eax, %eax
	xorl	%edx,%edx
	testl	$CPUID_PGE, R(_cpu_feature)
	jz	2f
	orl	$PG_G,%edx
	
2:	movl	$R(_etext),%ecx
	addl	$PAGE_MASK,%ecx
	shrl	$PAGE_SHIFT,%ecx
	fillkptphys(%edx)
fillkptphysマクロを用いて,リニアアドレス0 番地からカーネルのtextセグメントを読み込み専用ページとして割り振りま す.

/* Map read-write, data, bss and symbols */
	movl	$R(_etext),%eax
	addl	$PAGE_MASK, %eax
	andl	$~PAGE_MASK, %eax
map_read_write:
	movl	$PG_RW,%edx
	testl	$CPUID_PGE, R(_cpu_feature)
	jz	1f
	orl	$PG_G,%edx
	
1:	movl	R(_KERNend),%ecx
	subl	%eax,%ecx
	shrl	$PAGE_SHIFT,%ecx
	fillkptphys(%edx)
続いて,dataおよびbssセグメントを書き込み可能ページとして割り振ります.

/* Map page directory. */
	movl	R(_IdlePTD), %eax
	movl	$1, %ecx
	fillkptphys($PG_RW)
続くリニアアドレスに,IdlePTDから1ページ分を割り振ります.

/* Map proc0's page table for the UPAGES. */
	movl	R(p0upt), %eax
	movl	$1, %ecx
	fillkptphys($PG_RW)
続くアドレスには,_p0uptから1ページ分を割り振ります.

/* Map proc0's UPAGES in the physical way ... */
	movl	R(p0upa), %eax
	movl	$UPAGES, %ecx
	fillkptphys($PG_RW)
そして,UPAGEページ分を割り振ります.ここまでで,カーネルイメージおよ びページディレクトリのリニアアドレスが決まります.

/* Map ISA hole */
	movl	$ISA_HOLE_START, %eax
	movl	$ISA_HOLE_LENGTH>>PAGE_SHIFT, %ecx
	fillkptphys($PG_RW)
次に,リニアアドレス0xa0000番地から0xfffff番地までの``ISA hole''を割り 振ります.以上で,ページテーブルに物理アドレスを設定する作業が終ります. 続いて,ページディレクトリテーブルを作成します.

/* Map proc0s UPAGES in the special page table for this purpose ... */
	movl	R(p0upa), %eax
	movl	$KSTKPTEOFF, %ebx
	movl	$UPAGES, %ecx
	fillkpt(R(p0upt), $PG_RW)
次に,proc 0のユーザーページのページディレクトリ(p0upaを指す)を作成し ます.

/* ... and put the page table in the pde. */
	movl	R(p0upt), %eax
	movl	$KSTKPTDI, %ebx
	movl	$1, %ecx
	fillkpt(R(_IdlePTD), $PG_RW)
そして,proc 0のページテーブルをしめすページディレクトリを作成します.

/* install a pde for temporary double map of bottom of VA */
	movl	R(_KPTphys), %eax
	xorl	%ebx, %ebx
	movl	$1, %ecx
	fillkpt(R(_IdlePTD), $PG_RW)
続いて,物理アドレスでカーネルのページテーブルを示すページディレクトリ を作成します.

/* install pde's for pt's */
	movl	R(_KPTphys), %eax
	movl	$KPTDI, %ebx
	movl	$NKPT, %ecx
	fillkpt(R(_IdlePTD), $PG_RW)
さらに,リニアアドレスでカーネルのページテーブルを示すページディレクト リを作成します.

/* install a pde recursively mapping page directory as a page table */
	movl	R(_IdlePTD), %eax
	movl	$PTDPTDI, %ebx
	movl	$1,%ecx
	fillkpt(R(_IdlePTD), $PG_RW)

	ret
最後に,IdlePTD自身を示すページディレクトリを作成します.ここまでで,ペー ジングで必要となるテーブルの設定が終りました.

	movl	R(_IdlePTD), %eax
	movl	%eax,%cr3		/* load ptd addr into mmu */
ページディレクトリをcr3にセットし,

	movl	%cr0,%eax		/* get control word */
	orl	$CR0_PE|CR0_PG,%eax	/* enable paging */
	movl	%eax,%cr0		/* and let's page NOW! */
cr0のPEビットを1にしてページングを有効にし,

	pushl	$begin			/* jump to high virtualized address */
	ret

begin:
これで,ページングを有効にしたアドレス空間に移行します.

ブートストラップスタックの作成


	/* set up bootstrap stack */
	movl	$_kstack+UPAGES*PAGE_SIZE,%esp	/* bootstrap stack end location */
	xorl	%eax,%eax			/* mark end of frames */
	movl	%eax,%ebp
	movl	_proc0paddr,%eax
	movl	_IdlePTD, %esi
	movl	%esi,PCB_CR3(%eax)

CPUの初期化


	movl	physfree, %esi
	pushl	%esi				/* value of first for init386(first) */
	call	_init386			/* wire 386 chip for
unix operation */
machdep.cのinit386()を呼びだし,CPUに依存 した初期化を行ないます.そして,非インテルCPUの 初期化を行ないます.

	popl	%esi

非インテルCPUの初期化


#ifdef PC98
#if defined(CYRIX_486DLC) && defined(I486_CPU)
	/* Cyrix 486DLC/SLC/DLC2/SLC2 CPU */
	cmpl	$CPU_486DLC,_cpu
	jne	1f
	cli
	movl	%cr0,%eax
	orl	$0x40000000,%eax	/* disable cache */
	mov	%eax,%cr0
	.byte	0x0f,0x08	/* invd */

	movb	$0xc0,%al
	outb	%al,$0x22	/* Cyrix486[SD]LC cache controler index */
	movb	$0x02,%al	/* CCR0 = 0x02 (disable cache 640K-1M) */
	outb	%al,$0x23	/* window */
	movb	$0xc6,%al
	outb	%al,$0x22
	movb	$0x00,%al
	outb	%al,$0x23	/* NCR1: enable cache in all area */
	movb	$0x00,%al
	outb	%al,$0x22	/* dummy window */
	movb	$0x00,%al
	outb	%al,$0x23	/* dummy write */

	movl	%cr0,%eax
	andl	$0x9fffffff,%eax	/* enable cache !! */
	movl	%eax,%cr0
	sti
1:
#endif
#if defined(IBM_486SLC) && defined(I486_CPU)
/* optimization */
	cli
	movl	%cr0,%eax
	orl	$0x40000000,%eax  # disable cache
	mov	%eax,%cr0
	.byte	0x0f,0x08	  # invd

	movl	$0x00000000,%edx
	movl	$0x00009c92,%eax  # If using Intel-FPU,set "1c92" instead.
	movl	$0x00001000,%ecx
	.byte	0x0f,0x30	  # wrmsr

	movl	$0x000000d0,%edx  # 13MB(0x0d) cache.(over1MB-14MB region)
	movl	$0x000003ff,%eax  # 0-640KB cache. (64KB block * 10)
	movl	$0x00001001,%ecx
	.byte	0x0f,0x30	  # wrmsr

	movl	$0x00000000,%edx
	movl	$0x03000000,%eax  # "3" means double-clock-mode. or set all-zero.
	movl	$0x00001002,%ecx
	.byte	0x0f,0x30	  # wrmsr

	movl	%cr0,%eax
	andl	$0x9fffffff,%eax  # enable cache !!
	movl	%eax,%cr0
	sti
#endif
#ifdef CYRIX_5X86
	/* CYRIX 5x86 CPU */
	cli

	outb	%al,$0x50		# Reset NMI F/F
	mov	%cr0,%eax
	orl	$0x40000000,%eax	# disable cache
	movl	%eax,%cr0

	wbinvd				# flush buffer

	movb	$0xc3,%al
	outb	%al,$0x22
	inb	$0x23,%al

	movb	$0x0c1,%al		# CCR1
	outb	%al,$0x22
	movb	$0x00,%al		# No SMM support
	outb	%al,$0x23
	movb	$0x0c2,%al		# CCR2
	outb	%al,$0x22
#ifdef SUSP_HLT
	movb	$0x0a,%al		# USE_WBAK, SUSP_HLT
#else
	movb	$0x02,%al		# USE_WBAK
#endif
	outb	%al,$0x23
	movb	$0xc3,%al		# CCR3
	outb	%al,$0x22		# 
	movb	$0x10,%al		# MAPEN0 (to access CCR4)
	outb	%al,$0x23
	movb	$0x0e8,%al		# CCR4
	outb	%al,$0x22
	movb	$0x18, %al		# DTE_EN, MEM_BYP
#ifdef FASTER_5X86_FPU
	orb	$0x20, %al		# UNDOCUMENTED OPTION
#endif
#ifdef CX586_IO
	orb	$CX586_IO, %al
#endif
	outb	%al,$0x23
	movb	$0x020,%al		# PCR0
	outb	%al,$0x22
	xorb	%al, %al
#ifdef RSTK_EN
	orb	$0x01, %al		# Return Stack Enable
#endif
#ifdef BTB_EN
	orb	$0x02, %al		# Branch Target Buffer enable
#endif
#ifdef LOOP_EN
	orb	$0x04, %al		# Loop Enable
#endif
#ifndef DISABLE_5X86_LSSER
	orb	$0x80, %al		# Reorder
#endif
	outb	%al,$0x23
	movb	$0x0c3,%al		# CCR3
	outb	%al,$0x22
	movb	$0x00,%al
	outb	%al,$0x23
	movb	$0x80,%al		# dummy
	outb	%al,$0x22
	inb	$0x23,%al

	movl	%cr0,%ebx
	andl	$0x0bfffffff,%ebx	# enable cache
	orl	$0x020000000,%ebx	# write back mode
	movl	%ebx,%cr0		# go!

	outb	%al,$0x52		# Set NMI F/F
	sti
#endif /* CYRIX_5X86 */
#endif	/* PC98 */

メイン関数の呼びだし


	.globl	__ucodesel,__udatasel

	pushl	$0				/* unused */
	pushl	__udatasel			/* ss */
	pushl	$0				/* esp - filled in by execve() */
	pushl	$PSL_USER			/* eflags (IOPL 0, int enab) */
	pushl	__ucodesel			/* cs */
	pushl	$0				/* eip - filled in by execve() */
	subl	$(12*4),%esp			/* space for rest of registers */

	pushl	%esp				/* call main with frame pointer */
	call	_main				/* autoconfiguration, mountroot etc */

	addl	$(13*4),%esp			/* back to a frame we can return with */

ユーザーモードへ移行

メイン関数から戻ってきたら,アプリケーションプログラムなら終了を意味す るのですが,カーネルの場合はちょっと違います.

	/*
	 * now we've run main() and determined what cpu-type we are, we can
	 * enable write protection and alignment checking on i486 cpus and
	 * above.
	 */
#if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
	cmpl    $CPUCLASS_386,_cpu_class
	je	1f
	movl	%cr0,%eax			/* get control word */
	orl	$CR0_WP|CR0_AM,%eax		/* enable i486 features */
	movl	%eax,%cr0			/* and do it */
1:
#endif

	/*
	 * on return from main(), we are process 1
	 * set up address space and stack so that we can 'return' to user mode
	 */
	movl	__ucodesel,%eax
	movl	__udatasel,%ecx

	movl	%cx,%ds
	movl	%cx,%es
	movl	%ax,%fs				/* double map cs to fs */
	movl	%cx,%gs				/* and ds to gs */
	iret					/* goto user! */

マクロ

ALLOCPAGES
physfreeから始まるfooページの領域を0で埋めます.その 次のアドレスをphysfreeに格納します.すなわち, physfreefooページ分増加させます.終了時にはesiにも ともとのphysfreeの値が格納されます.

#define ALLOCPAGES(foo) \
	movl	R(physfree), %esi ; \
	movl	$((foo)*PAGE_SIZE), %eax ; \
	addl	%esi, %eax ; \
	movl	%eax, R(physfree) ; \
	movl	%esi, %edi ; \
	movl	$((foo)*PAGE_SIZE),%ecx ; \
	xorl	%eax,%eax ; \
	cld ; \
	rep ; \
	stosb
fillkpt

/*
 * fillkpt
 *	eax = page frame address
 *	ebx = index into page table
 *	ecx = how many pages to map
 * 	base = base address of page dir/table
 *	prot = protection bits
 */
#define	fillkpt(base, prot)		  \
	shll	$2,%ebx			; \
	addl	base,%ebx		; \
	orl	$PG_V,%eax		; \
	orl	prot,%eax		; \
1:	movl	%eax,(%ebx)		; \
	addl	$PAGE_SIZE,%eax		; /* increment physical address */ \
	addl	$4,%ebx			; /* next pte */ \
	loop	1b
fillkptphys

/*
 * fillkptphys(prot)
 *	eax = physical address
 *	ecx = how many pages to map
 *	prot = protection bits
 */
#define	fillkptphys(prot)		  \
	movl	%eax, %ebx		; \
	shrl	$PAGE_SHIFT, %ebx	; \
	fillkpt(R(_KPTphys), prot)

/*
 * Signal trampoline, copied to top of user stack
 */
NON_GPROF_ENTRY(sigcode)
	call	SIGF_HANDLER(%esp)
	lea	SIGF_SC(%esp),%eax		/* scp (the call may have clobbered the */
						/* copy at 8(%esp)) */
	pushl	%eax
	pushl	%eax				/* junk to fake return address */
	movl	$SYS_sigreturn,%eax		/* sigreturn() */
	LCALL(0x7,0)				/* enter kernel with args on stack */
	hlt					/* never gets here */
	.align	2,0x90				/* long word text-align */
_esigcode:

	.data
	.globl	_szsigcode
_szsigcode:
	.long	_esigcode-_sigcode
	.text

リーガル Contact 名大岩鉱 名大地球 名大年測
Copyright KATO Takenori, 1997, 1998, 199, 2000. All rights reserved.