2014-12-18:18:30-21:00
# 这段程序实际上是出于内存的绝对地址0开始处。首先是加载各个数据段寄存器。
# 重新设置全局描述符表gdt --> 检测a20地址线是否真的开启,没有开启,loop
# 掉了 --> 检测pc是否含有数学协处理器 --> 设置管理内存分页的处理机制 -->
# 将页目录放置在内存地址0开始处。所以这段程序将被覆盖掉。 --> 最后利用ret
# 指令弹出预先压入的/init/main.c程序的入口地址,去运行main.c程序。
#之上这一段化借用出处:http://www.cnblogs.com/xuqiang/archive/2010/02/16/1953757.html
/*
* linux/boot/head.s
*
* (C) 1991 Linus Torvalds
*/
/*
* head.s contains the 32-bit startup code.
*
* NOTE!!! Startup happens at absolute address 0x00000000, which is also where
* the page directory will exist. The startup code will be overwritten by
* the page directory.
*/
! #head.s属于32位的启动代码。
!#注意!!!启动代码执行在绝对地址0x0000-0000,页目录也将存在这里。
!#这个statup代码将被页目录覆盖写。
.text
.globl _idt,_gdt,_pg_dir,_tmp_floppy_area
_pg_dir: # 页目录将会存放在这里。
startup_32:
!#对于 GNU 汇编来说,每个直接数要以'$'开始,否则是表示地址。
!#每个寄存器名都要以'%'开头,eax 表示是 32 位的 ax 寄存器。
!#代码中的赋值方向是从左到右。
##############################
# 设置段寄存器
# 再次注意,现在程序已经运行在32模式,因此这里
# 的0x10并不是把地址0x10装入各个段寄存器,这里$0x10 的含义是请求
# 特权级 0(位 0-1=0)、选择全局描述符表(位 2=0)、选择表中第 1 项(位 3-15=1)。
# 这里的0x10正好指向表中的数据段描述符项
movl $0x10,%eax
mov %ax,%ds
mov %ax,%es
mov %ax,%fs
mov %ax,%gs
# 下面代码的含义是,置ds,es,fs,gs中的选择符
# 为setup.s中构造的数据段,并将堆栈放置在数据段
# _stack_start数组内,然后使用新的中断描述符表
# 和全局描述符表,新的全局段描述符表中初始化内
# 容和setup.s中完全相同。
lss _stack_start,%esp
# 加载堆栈指针寄指令
# long user_stack [ PAGE_SIZE>>2 ] ;
#
# struct {
# long * a;
# short b;
# } stack_start = { & user_stack [PAGE_SIZE>>2] , 0x10 };
#
call setup_idt
call setup_gdt
################################################
!#改变了gdt后,重新加载所有的段寄存器
!#CS已经重新加载在setup_gdt
movl $0x10,%eax # reload all the segment registers
mov %ax,%ds # after changing gdt. CS was already
mov %ax,%es # reloaded in 'setup_gdt'
mov %ax,%fs
mov %ax,%gs
lss _stack_start,%esp
#############################################
# 下面检测是否开启a20地址线采用的方法是向0x100000
# 处写入一个数值,然后看内存地址0x000000处是否也是
# 这个数值。如果不相同的话,loop死掉。
#“地址访问超出回倦”。
xorl %eax,%eax
1: incl %eax # check that A20 really IS enabled
movl %eax,0x000000 # loop forever if it isn't
cmpl %eax,0x100000
je 1b # '1b'表示向后(backward)跳转到标号 1 去(34行)。
# 若是'5f'则表示向前(forward)跳转到标号5 去。
/*
* NOTE! 486 should set bit 16, to check for write-protect in supervisor
* mode. Then it would be unnecessary with the "verify_area()"-calls.
* 486 users probably want to set the NE (#5) bit also, so as to use
* int 16 for math errors.
*/
#####################################################
# 检查数学协处理器是否存在。采用的方法是先假设协处理器存在,
# 执行一个协处理器指令,出错,表明不存在协处理器。
movl %cr0,%eax # check math chip
andl $0x80000011,%eax # Save PG,PE,ET
/* "orl $0x10020,%eax" here for 486 might be good */
orl $2,%eax # set MP
movl %eax,%cr0
call check_x87
jmp after_page_tables
/*
* We depend on ET to be correct. This checks for 287/387.
*/
check_x87:
fninit
fstsw %ax
cmpb $0,%al
je 1f /* no coprocessor: have to set bits */
movl %cr0,%eax
xorl $6,%eax /* reset MP, set EM */
movl %eax,%cr0
ret
.align 2
1: .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 then loads
* idt. Everything that wants to install itself
* in the idt-table may do so themselves. Interrupts
* are enabled elsewhere, when we can be relatively
* sure everything is ok. This routine will be over-
* written by the page tables.
*/
#################################################
#设置idt有256个实体,都指向ignore_int门,只是一句打印,什么也没有做,中断此时也没有打开,留有以后设置,然后装载idt。。
#以后他们会自行安装到idt-table中。
#中断在别处使能,当我们认为相关的确定的事都OK。这个例程将被页表覆盖。
#中断描述表中的项8个字节。它的0-1,6-7字节是偏移量,2-3字节
#是选择符,4-5是一些标志。
setup_idt:
lea ignore_int,%edx #ignore_int移动到edx中。
movl $0x00080000,%eax
movw %dx,%ax /* selector = 0x0008 = cs */
movw $0x8E00,%dx /* interrupt gate - dpl=0, present */
lea _idt,%edi #将_idt值加载到edi中。
mov $256,%ecx
rp_sidt:
movl %eax,(%edi)
movl %edx,4(%edi) #中断描述符表中的表项全指向ignore_int,即打印一句话。此时还没有开中断。
addl $8,%edi
dec %ecx
jne rp_sidt #一直加载,知道256项加载完成,才执行后面的代码。
lidt idt_descr #加载中断描述符表寄存器,其中idt_descr在下面
ret
/*
* setup_gdt
*
* This routines sets up a new gdt and loads it.
* Only two entries are currently built, the same
* ones that were built in init.s. The routine
* is VERY complicated at two whole lines, so this
* rather long comment is certainly needed :-).
* This routine will beoverwritten by the page tables.
*/
setup_gdt:
lgdt gdt_descr
ret
/*
* I put the kernel page tables right after the page directory,
* using 4 of them to span 16 Mb of physical memory. People with
* more than 16MB will have to expand this.
*/
############################################
#我放内核页表在页目录后面,使用4个页表寻址16MB的物理内存。
#有人使用多余16MB将不得不扩展页表。
.org 0x1000
pg0:
.org 0x2000
pg1:
.org 0x3000
pg2:
.org 0x4000
pg3:
.org 0x5000
/*
* 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
# 下面这几个入栈操作(pushl)用于为调用/init/main.c 程序和返回作准备。
# 前面 3 个入栈指令不知道作什么用的,也许是 Linus 用于在调试时能看清机器码用的☺。
# 139 行的入栈操作是模拟调用 main.c 程序时首先将返回地址入栈的操作,所以如果
# main.c 程序真的退出时,就会返回到这里的标号 L6 处继续执行下去,也即死循环。
# 140 行将 main.c 的地址压入堆栈,这样,在设置分页处理( setup_paging)结束后
# 执行'ret'返回指令时就会将 main.c 程序的地址弹出堆栈,并去执行 main.c 程序去了
after_page_tables:
pushl $0 # These are the parameters to main :-)
pushl $0
pushl $0
pushl $L6 # return address for main, if it decides to.
pushl $_main #压栈_main函数的入口地址。当setup_paging返回时,_main函数弹出栈,***进入main函数。
jmp setup_paging
L6:
jmp L6 # main should never return here, but
# just in case, we know what happens.
/* This is the default interrupt "handler" :-) */
#这是默认的中断处理函数。
int_msg:
.asciz "Unknown interrupt\n\r"
.align 2
ignore_int:
pushl %eax
pushl %ecx
pushl %edx
push %ds
push %es
push %fs
movl $0x10,%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
/*
* Setup_paging
*
* This routine sets up paging by setting the page bit
* in cr0. The page tables are set up, identity-mapping
* the first 16MB. The pager assumes that no illegal
* addresses are produced (ie >4Mb on a 4Mb machine).
*
* NOTE! Although all physical memory should be identity
* mapped by this routine, only the kernel page functions
* use the >1Mb addresses directly. All "normal" functions
* use just the lower 1Mb, or the local data space, which
* will be mapped to some other place - mm keeps track of
* that.
*
* For those with more memory than 16 Mb - tough luck. I've
* not got it, why should you :-) The source is here. Change
* it. (Seriously - it shouldn't be too difficult. Mostly
* change some constants etc. I left it at 16Mb, as my machine
* even cannot be extended past that (ok, but it was cheap :-)
* I've tried to show which constants to change by having
* some kind of marker at them (search for "16Mb"), but I
* won't guarantee that's all :-( )
*/
.align 2 ## 按 4 字节方式对齐内存地址边界。
setup_paging:
movl $1024*5,%ecx /* 5 pages - pg_dir+4 page tables */ #5页,pg_dir(页目录)+4个页表.
xorl %eax,%eax
xorl %edi,%edi /* pg_dir is at 0x000 */
cld;rep;stosl
movl $pg0+7,_pg_dir /* set present bit/user r/w */ # "$pg0+7"表示:0x00001007,是页目录表中的第 1 项。
movl $pg1+7,_pg_dir+4 /* --------- " " --------- */
movl $pg2+7,_pg_dir+8 /* --------- " " --------- */
movl $pg3+7,_pg_dir+12 /* --------- " " --------- */
#下面 6 行填写 4 个页表中所有项的内容,共有:4(页表)*1024(项/页表)=4096 项(0 - 0xfff),
# 也即能映射物理内存 4096*4Kb = 16Mb。
movl $pg3+4092,%edi
movl $0xfff007,%eax /* 16Mb - 4096 + 7 (r/w user,p) */
std
1: stosl /* fill pages backwards - more efficient :-) */
subl $0x1000,%eax
jge 1b
# 设置页目录基址寄存器 cr3 的值,指向页目录表。
xorl %eax,%eax /* pg_dir is at 0x0000 */
movl %eax,%cr3 /* cr3 - page directory start */
# 设置启动使用分页处理(cr0 的 PG 标志,位 31)
movl %cr0,%eax
orl $0x80000000,%eax
movl %eax,%cr0 /* set paging (PG) bit */
ret /* this also flushes prefetch-queue */
# 在改变分页处理标志后要求使用转移指令刷新预取指令队列,这里用的是返回指令 ret。
# 该返回指令的另一个作用是将堆栈中的 main 程序的地址弹出,并开始运行/init/main.c 程序。
# 本程序到此真正结束了.
.align 2
.word 0
idt_descr:
# 下面两行是 lgdt 指令的 6 字节操作数:长度,基址
.word 256*8-1 # idt contains 256 entries
.long _idt
.align 2
.word 0
gdt_descr:
.word 256*8-1 # so does gdt (not that that's any
.long _gdt # magic number, but it works for me :^)
.align 3
_idt: .fill 256,8,0 # idt is uninitialized #占用内存256项,每一项8个字节,每一个字节用0填充。
_gdt: .quad 0x0000000000000000 /* NULL descriptor */
.quad 0x00c09a0000000fff /* 16Mb */
.quad 0x00c0920000000fff /* 16Mb */
.quad 0x0000000000000000 /* TEMPORARY - don't use */
.fill 252,8,0 /* space for LDT's and TSS's etc */