* linux/boot/head.S
*
* Copyright (C) 1991, 1992 Linus Torvalds
*/
/*
* head.S contains the 32-bit startup code.
*/
.text
.globl _idt,_gdt,
.globl _swapper_pg_dir,_pg0
.globl _empty_bad_page
.globl _empty_bad_page_table
.globl _empty_zero_page
.globl _tmp_floppy_area,_floppy_track_buffer
#include <linux/tasks.h>
#include <linux/segment.h>
#define CL_MAGIC_ADDR 0x90020
#define CL_MAGIC 0xA33F
#define CL_BASE_ADDR 0x90000
#define CL_OFFSET 0x90022
/*
* swapper_pg_dir is the main page directory, address 0x00001000 (or at
* address 0x00101000 for a compressed boot).
*/
startup_32:
cld
movl $(KERNEL_DS),%eax
mov %ax,%ds
mov %ax,%es
mov %ax,%fs
mov %ax,%gs
lss _stack_start,%esp
/*
* Clear BSS first so that there are no surprises...
*/
xorl %eax,%eax
movl $__edata,%edi
movl $__end,%ecx
subl %edi,%ecx
cld
rep
stosb
/*
* start system 32-bit setup. We need to re-do some of the things done
* in 16-bit mode for the "real" operations.
*/
call setup_idt
xorl %eax,%eax
//eax初值为0,然后加1后为1.将eax赋值给0x000000的内存地址,即为内存首地址0。
//然后再将内存0x100000(刚好超过2^20=1M)的值和eax比较,如果A20没打开,则对于访问高于1M地址的内容,会自动环绕,即对1M取模。
//0x100000取模后即为0,故访问0x100000地址就是访问地址0.所以,如果是A20没打开,eax的值与0x100000处的值应该是相等的。
1: incl %eax # check that A20 really IS enabled
movl %eax,0x000000 # loop forever if it isn't
cmpl %eax,0x100000
je 1b
/*
* Initialize eflags. Some BIOS's leave bits like NT set. This would
* confuse the debugger if this code is traced.
* XXX - best to initialize before switching to protected mode.
*/
pushl $0
popfl
/*
* Copy bootup parameters out of the way. First 2kB of
* _empty_zero_page is for boot parameters, second 2kB
* is for the command line.
*/
movl $0x90000,%esi
movl $_empty_zero_page,%edi
movl $512,%ecx
cld
rep
movsl
xorl %eax,%eax
movl $512,%ecx
rep
stosl
cmpw $(CL_MAGIC),CL_MAGIC_ADDR
jne 1f
movl $_empty_zero_page+2048,%edi
movzwl CL_OFFSET,%esi
addl $(CL_BASE_ADDR),%esi
movl $2048,%ecx
rep
movsb
1:
/* check if it is 486 or 386. */
/*
* XXX - this does a lot of unnecessary setup. Alignment checks don't
* apply at our cpl of 0 and the stack ought to be aligned already, and
* we don't need to preserve eflags.
*/
movl %esp,%edi # save stack pointer
andl $0xfffffffc,%esp # align stack to avoid AC fault
movl $3,_x86
pushfl # push EFLAGS
popl %eax # get EFLAGS
movl %eax,%ecx # save original EFLAGS
xorl $0x40000,%eax # flip AC bit in EFLAGS
pushl %eax # copy to EFLAGS
popfl # set EFLAGS
pushfl # get new EFLAGS
popl %eax # put it in eax
xorl %ecx,%eax # change in flags
andl $0x40000,%eax # check if AC bit changed
je is386
movl $4,_x86
movl %ecx,%eax
xorl $0x200000,%eax # check ID flag
pushl %eax
popfl # if we are on a straight 486DX, SX, or
pushfl # 487SX we can't change it
popl %eax
xorl %ecx,%eax
andl $0x200000,%eax
je is486
isnew: pushl %ecx # restore original EFLAGS
popfl
movl $1, %eax # Use the CPUID instruction to
.byte 0x0f, 0xa2 # check the processor type
andl $0xf00, %eax # Set _x86 with the family
shrl $8, %eax # returned.
movl %eax, _x86
movl %edi,%esp # restore esp
movl %cr0,%eax # 486+
andl $0x80000011,%eax # Save PG,PE,ET
orl $0x50022,%eax # set AM, WP, NE and MP
jmp 2f
is486: pushl %ecx # restore original EFLAGS
popfl
movl %edi,%esp # restore esp
movl %cr0,%eax # 486
andl $0x80000011,%eax # Save PG,PE,ET
orl $0x50022,%eax # set AM, WP, NE and MP
jmp 2f
is386: pushl %ecx # restore original EFLAGS
popfl
movl %edi,%esp # restore esp
movl %cr0,%eax # 386
andl $0x80000011,%eax # Save PG,PE,ET
orl $2,%eax # set MP
2: movl %eax,%cr0
call check_x87
call setup_paging
lgdt gdt_descr
lidt idt_descr
//从Makefile可知道,tools/zSystem编译时用了编译参数-Ttext 100000,即表示tools/zSystem是从0x100000(即1M)处开始编址的,即boot/head.S的偏移地址为0x100000。
//那么$1f = 0x100000 + 符号1相对于system(即物理地址0x100000)的偏移量。
//根据KERNEL_CS所对应的内核代码段描述符可知,要jmp的虚拟地址 = 基地址0xC0000000 + $1f = 0xC0100000 + 符号1相对于system(即物理地址0x100000)的偏移量
//然后根据内核分页将jmp的虚拟地址转化为物理地址,从虚拟地址的高10位可知,它对应页目录表的768个表项,即指向pg0页表。而根据分页机制可知,虚拟地址是对等映射到0~4M的物理内存的,所以,偏移地址0x100***,如0x100002等,都是对等映射到相同的物理地址。故,jmp能跳到正确的物理地址,即接下来的标号1处接着执行。
ljmp $(KERNEL_CS),$1f ----顺便重新加载了CS寄存器
mov %ax,%ds # after changing gdt.
mov %ax,%es
mov %ax,%fs
mov %ax,%gs
lss _stack_start,%esp
xorl %eax,%eax
lldt %ax
pushl %eax # These are the parameters to main :-)
pushl %eax
pushl %eax
cld # gcc2 wants the direction flag cleared at all times
call _start_kernel
L6:
jmp L6 # main should never return here, but
# just in case, we know what happens.
/*
* We depend on ET to be correct. This checks for 287/387.
*/
check_x87:
movl $0,_hard_math
clts
fninit
fstsw %ax
cmpb $0,%al
je 1f
movl %cr0,%eax /* no coprocessor: have to set bits */
xorl $4,%eax /* set EM */
movl %eax,%cr0
ret
.align 2
1: movl $1,_hard_math
.byte 0xDB,0xE4 /* fsetpm for 287, ignored by 387 */
ret
/*
* setup_idt
*
* sets up a idt with 256 entries pointing to
* ignore_int, interrupt gates. It doesn't actually load
* idt - that can be done only after paging has been enabled
* and the kernel moved to 0xC0000000. Interrupts
* are enabled elsewhere, when we can be relatively
* sure everything is ok.
*/
//中断门的描述符内容为: 中断处理程序ignore_int地址的高16位 + 8E00 + 0010 + 中断处理程序ignore_int地址的低16位
//其中0x0010为中断处理程序ignore_int所在段的描述符,0x8E00为中断门的属性信息。
setup_idt:
lea ignore_int,%edx
movl $(KERNEL_CS << 16),%eax
movw %dx,%ax /* selector = 0x0010 = cs */
movw $0x8E00,%dx /* interrupt gate - dpl=0, present */
lea _idt,%edi ---中断表的起始地址
mov $256,%ecx ---循环256次,统一用默认的中断处理程序ignore_int来初始化256个中断。
rp_sidt:
movl %eax,(%edi)
movl %edx,4(%edi)
addl $8,%edi
dec %ecx
jne rp_sidt
ret
/*
* Setup_paging
*
* This routine sets up paging by setting the page bit
* in cr0. The page tables are set up, identity-mapping
* the first 4MB. The rest are initialized later.
*
* (ref: added support for up to 32mb, 17Apr92) -- Rik Faith
* (ref: update, 25Sept92) -- croutons@crunchy.uucp
* (ref: 92.10.11 - Linus Torvalds. Corrected 16M limit - no upper memory limit)
*/
.align 2
setup_paging:
//对页目录表和_pg0页内存清零(一共两页大小)
movl $1024*2,%ecx /* 2 pages - swapper_pg_dir+1 page table */
xorl %eax,%eax
movl $_swapper_pg_dir,%edi /* swapper_pg_dir is at 0x1000 */
cld;rep;stosl //循环清零,cld表示EDI自动每次加4
/* Identity-map the kernel in low 4MB memory for ease of transition */
//填写页目录表,分别填写了index为0和768的页表项。从gdt中可知道,kernel和user的段的基址分别为0xC0000000和0x00000000,高10位分别为1100 0000 00b和0000 0000 00b,即表示页目录表中的第768个页表项和第0个页表项。这样kernel和user的线性地址可以根据_pg0页表来转换为物理地址。
//"$_pg0+7"表示:0x00001007(保护模式下,偏移量为32位),是页目录表中的第1 项。则第1 个页表所在的地址 = 0x00001007 & 0xfffff000 = 0x1000;
//第1 个页表的属性标志 = 0x00001007 & 0x00000fff = 0x07,表示该页存在、用户可读写。
movl $_pg0+7,_swapper_pg_dir /* set present bit/user r/w */
/* But the real place is at 0xC0000000 */
//同时将该页表项也赋给swapper_pg_dir的第768个页表项(3072/4),表示虚拟地址0xc0000000也指向pg0(因为它高10位是1100 0000 00b,即为十进制768,故0xc0000000对应于页目录表的第768个表项,即指向pg0)。
movl $_pg0+7,_swapper_pg_dir+3072 /* set present bit/user r/w */
//从页的最后一项开始填写每一个页表项。
//一页有1024个项,每个项大小为4bytes,故最后一个项为页的第4093到4096字节,即$_pg0+4092开始的四个字节。
//分页只映射4M大小,故对应于最后一个项的物理页的起始地址为4MB - 4096,再加上属性值7,即为4Mb - 4096 + 7,表示该页存在、用户可读写。
movl $_pg0+4092,%edi
movl $0x03ff007,%eax /* 4Mb - 4096 + 7 (r/w user,p) */
std //表示EDI自动减4
//从后往前,开始填写每一个页表项。一个页表对应于4M的物理内存(2^10 * 2^12).
//4M的物理内存,即为物理地址0X0000 0000 到 0X003F F000
1: stosl /* fill the page backwards - more efficient :-) */
subl $0x1000,%eax //每填完一个页表项后,一个页表项对应一物理页,故对应的物理内存的地址就减少0x1000,即4kb。
jge 1b //如果小于0,则表明所有的页表项已经填完了
cld
movl $_swapper_pg_dir,%eax
movl %eax,%cr3 /* cr3 - page directory start */ //cr3,32位控制寄存器,高20位存页目录表基址
movl %cr0,%eax
orl $0x80000000,%eax //启动使用分页机制(cr0 的PG 标志,位31)
movl %eax,%cr0 /* set paging (PG) bit */
ret /* this also flushes the prefetch-queue */
/*
* page 0 is made non-existent, so that kernel NULL pointer references get
* caught. Thus the swapper page directory has been moved to 0x1000
*
* XXX Actually, the swapper page directory is at 0x1000 plus 1 megabyte,
* with the introduction of the compressed boot code. Theoretically,
* the original design of overlaying the startup code with the swapper
* page directory is still possible --- it would reduce the size of the kernel
* by 2-3k. This would be a good thing to do at some point.....
*/
.org 0x1000 //定义页目录表从偏移0x1000处开始
_swapper_pg_dir:
/*
* The page tables are initialized to only 4MB here - the final page
* tables are set up later depending on memory size.
*/
.org 0x2000 //定义第一个页表从偏移0x2000处开始
_pg0:
.org 0x3000
_empty_bad_page:
.org 0x4000
_empty_bad_page_table:
.org 0x5000
_empty_zero_page:
.org 0x6000
/*
* tmp_floppy_area is used by the floppy-driver when DMA cannot
* reach to a buffer-block. It needs to be aligned, so that it isn't
* on a 64kB border.
*/
_tmp_floppy_area:
.fill 1024,1,0
/*
* floppy_track_buffer is used to buffer one track of floppy data: it
* has to be separate from the tmp_floppy area, as otherwise a single-
* sector read/write can mess it up. It can contain one full track of
* data (18*2*512 bytes).
*/
_floppy_track_buffer:
.fill 512*2*18,1,0
/* This is the default interrupt "handler" :-) */
int_msg:
.asciz "Unknown interrupt\n"
.align 2
ignore_int:
cld
pushl %eax
pushl %ecx
pushl %edx
push %ds
push %es
push %fs
movl $(KERNEL_DS),%eax
mov %ax,%ds
mov %ax,%es
mov %ax,%fs
pushl $int_msg
call _printk
popl %eax
pop %fs
pop %es
pop %ds
popl %edx
popl %ecx
popl %eax
iret
/*
* The interrupt descriptor table has room for 256 idt's
*/
.align 4
.word 0
//idt和gdt表的描述都是长6个字节,第一第二字节表示描述符表的长度值,第三个字节到第六个字节表示的是32位的线性基地址(即表中第一个描述符在内存的中的起始地址)
idt_descr:
.word 256*8-1 # idt contains 256 entries
.long 0xc0000000+_idt
.align 4
_idt:
.fill 256,8,0 # idt is uninitialized //从当前地址开始连续填充256个size大小为8且value为0的数值
.align 4
.word 0
gdt_descr:
.word (8+2*NR_TASKS)*8-1
.long 0xc0000000+_gdt
/*
在Linux中,每个用户进程都可以访问4GB的线性地址空间。其中0x0~0xBFFFFFFF的3GB空间为用户态空间,用户态进程可以直接访问。从0xC0000000~0x3FFFFFFF的1GB空间为内核态空间,存放内核访问的代码和数据,用户态进程不能直接访问。当用户进程通过中断或系统调用访问内核态空间时,会触发X86的特权级转换(从特权级3切换到特权级0),即从用户态切换到内核态。
*/
//从0xC0000000~0x3FFFFFFF的1GB空间为内核态空间,存放内核访问的代码和数据。所以,kernel的代码段和数据段的基地址都是以0xC0000000开始的。
* This gdt setup gives the kernel a 1GB address space at virtual
* address 0xC0000000 - space enough for expansion, I hope.
*/
.align 4 //向后移动位置计数器至4的倍数
//伪指令.quad将其后面的参数都汇编成为8字节的整数
.quad 0x0000000000000000 /* NULL descriptor */
.quad 0x0000000000000000 /* not used */
.quad 0xc0c39a000000ffff /* 0x10 kernel 1GB code at 0xC0000000 */ -----0x10的选择子,0001 0000b,可知选择子指定gdt表中的index为2段描述符,即此段描述符,基地址为0xC0000000。
.quad 0xc0c392000000ffff /* 0x18 kernel 1GB data at 0xC0000000 */ -----0x18的选择子,0x18就是指定gdt表中的index为3段描述符,即此段描述符,此段的基地址为0xC0000000。
.quad 0x00cbfa000000ffff /* 0x23 user 3GB code at 0x00000000 */ ------0x23的选择子,0x23就是指定gdt表中的index为4段描述符,即此段描述符,此段的基地址为0x00000000。
.quad 0x00cbf2000000ffff /* 0x2b user 3GB data at 0x00000000 */ ------index为5的段
.quad 0x0000000000000000 /* not used */
.quad 0x0000000000000000 /* not used */
.fill 2*NR_TASKS,8,0 /* space for LDT's and TSS's etc */
不错的链接:
http://www.verydemo.com/demo_c167_i69772.html