head.s分析
//head.s采用的是AT&T的汇编格式
//使用GNU的gas和gld编译连接
//位置:物理地址0
//功能:1、加载各个数据段寄存器,设置256项中断描述符表idt,并使各个表项都指向错误的哑中断。
2、设置全局中断向量表gdt
3、比较物理地址0和1MB的内容比较判断是否开启了A20的地址线,没开启就进入死循环。
4、检查是否有协处理器,设置控制寄存器CR0。
5、开始设置内存的页处理机制,页目录表(4K)放在物理地址0开始的地方,4个页表放在后面的16k地址,共可以寻址16m空间。
6、利用返回指令将预先设置的init/main.c的入口地址弹出。
下面是详细的分析过程
/*
* 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.
*/
.text
.globl _idt,_gdt,_pg_dir,_tmp_floppy_area
_pg_dir: //页目录表的地址0x0000
startup_32:
movl $0x10,%eax //0x10=0b0000 0000 0000 1000 表示特权级=00,全局描述符表=0,局部描述符表=1,描述表项=(bit15~3)
mov %ax,%ds //设置ds,es,fs,gs
mov %ax,%es
mov %ax,%fs
mov %ax,%gs
lss _stack_start,%esp //_stack_start->ss:esp,_stack_start应是编译程序产生自动生成的存有堆栈信息的地方
call setup_idt //调用设置idt和gdt子程序
call 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
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
/*
* 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.
*/
setup_idt:
lea ignore_int,%edx //ignore-int偏移地址->edx
movl $0x00080000,%eax //0x0008->eax的高16位
movw %dx,%ax /* selector = 0x0008 = cs */
movw $0x8E00,%dx /* interrupt gate - dpl=0, present */
//此时edx含有门描述符的高4字节
lea _idt,%edi //_idt偏移地址->edi
mov $256,%ecx //ecx=256,循环设置256个中断描述符
rp_sidt:
movl %eax,(%edi) //哑中断描述符表存入表中,eax=0x00080000+ignore_int偏移地址
movl %edx,4(%edi)
addl $8,%edi //edi指向下一个地址
dec %ecx
jne rp_sidt
lidt 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.
*/
.org 0x1000 //4个页的偏移地址
pg0:
.org 0x2000
pg1:
.org 0x3000
pg2:
.org 0x4000
pg3:
.org 0x5000 //下面内存数据块从偏移地址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 //软盘缓冲区
after_page_tables: //这是调用main的参数
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函数的地址
jmp setup_paging
L6:
jmp L6 # main should never return here, but
# just in case, we know what happens.
//如果mian回到这里就进入死循环
/* 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 :-( )
*/ //设置页目录和4个页表,每个页表对应4m物理地址
.align 2
setup_paging:
movl $1024*5,%ecx /* 5 pages - pg_dir+4 page tables */
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 */ //依次把四个页表的所在地址和页属性标志放在四个页表目录项
movl $pg1+7,_pg_dir+4 //pg1为页地址,7为页属性标志 /* --------- " " --------- */
movl $pg2+7,_pg_dir+8 /* --------- " " --------- */
movl $pg3+7,_pg_dir+12 /* --------- " " --------- */
movl $pg3+4092,%edi //每个页表有1024项,每项四个字节
//从最后一页最后一项开始初始化
movl $0xfff007,%eax /* 16Mb - 4096 + 7 (r/w user,p) */ //最后一项的物理地址为16Mb - 4096
std
1: stosl /* fill pages backwards - more efficient :-) */
subl $0x1000,%eax
jge 1b
xorl %eax,%eax /* pg_dir is at 0x0000 */
movl %eax,%cr3 /* cr3 - page directory start */
movl %cr0,%eax
orl $0x80000000,%eax
movl %eax,%cr0 /* set paging (PG) bit */
ret /* this also flushes prefetch-queue */
//ret返回时弹出main的地址
.align 2
.word 0
idt_descr:
.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
_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 */
//head.s采用的是AT&T的汇编格式
//使用GNU的gas和gld编译连接
//位置:物理地址0
//功能:1、加载各个数据段寄存器,设置256项中断描述符表idt,并使各个表项都指向错误的哑中断。
2、设置全局中断向量表gdt
3、比较物理地址0和1MB的内容比较判断是否开启了A20的地址线,没开启就进入死循环。
4、检查是否有协处理器,设置控制寄存器CR0。
5、开始设置内存的页处理机制,页目录表(4K)放在物理地址0开始的地方,4个页表放在后面的16k地址,共可以寻址16m空间。
6、利用返回指令将预先设置的init/main.c的入口地址弹出。
下面是详细的分析过程
/*
* 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.
*/
.text
.globl _idt,_gdt,_pg_dir,_tmp_floppy_area
_pg_dir: //页目录表的地址0x0000
startup_32:
movl $0x10,%eax //0x10=0b0000 0000 0000 1000 表示特权级=00,全局描述符表=0,局部描述符表=1,描述表项=(bit15~3)
mov %ax,%ds //设置ds,es,fs,gs
mov %ax,%es
mov %ax,%fs
mov %ax,%gs
lss _stack_start,%esp //_stack_start->ss:esp,_stack_start应是编译程序产生自动生成的存有堆栈信息的地方
call setup_idt //调用设置idt和gdt子程序
call 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
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
/*
* 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.
*/
setup_idt:
lea ignore_int,%edx //ignore-int偏移地址->edx
movl $0x00080000,%eax //0x0008->eax的高16位
movw %dx,%ax /* selector = 0x0008 = cs */
movw $0x8E00,%dx /* interrupt gate - dpl=0, present */
//此时edx含有门描述符的高4字节
lea _idt,%edi //_idt偏移地址->edi
mov $256,%ecx //ecx=256,循环设置256个中断描述符
rp_sidt:
movl %eax,(%edi) //哑中断描述符表存入表中,eax=0x00080000+ignore_int偏移地址
movl %edx,4(%edi)
addl $8,%edi //edi指向下一个地址
dec %ecx
jne rp_sidt
lidt 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.
*/
.org 0x1000 //4个页的偏移地址
pg0:
.org 0x2000
pg1:
.org 0x3000
pg2:
.org 0x4000
pg3:
.org 0x5000 //下面内存数据块从偏移地址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 //软盘缓冲区
after_page_tables: //这是调用main的参数
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函数的地址
jmp setup_paging
L6:
jmp L6 # main should never return here, but
# just in case, we know what happens.
//如果mian回到这里就进入死循环
/* 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 :-( )
*/ //设置页目录和4个页表,每个页表对应4m物理地址
.align 2
setup_paging:
movl $1024*5,%ecx /* 5 pages - pg_dir+4 page tables */
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 */ //依次把四个页表的所在地址和页属性标志放在四个页表目录项
movl $pg1+7,_pg_dir+4 //pg1为页地址,7为页属性标志 /* --------- " " --------- */
movl $pg2+7,_pg_dir+8 /* --------- " " --------- */
movl $pg3+7,_pg_dir+12 /* --------- " " --------- */
movl $pg3+4092,%edi //每个页表有1024项,每项四个字节
//从最后一页最后一项开始初始化
movl $0xfff007,%eax /* 16Mb - 4096 + 7 (r/w user,p) */ //最后一项的物理地址为16Mb - 4096
std
1: stosl /* fill pages backwards - more efficient :-) */
subl $0x1000,%eax
jge 1b
xorl %eax,%eax /* pg_dir is at 0x0000 */
movl %eax,%cr3 /* cr3 - page directory start */
movl %cr0,%eax
orl $0x80000000,%eax
movl %eax,%cr0 /* set paging (PG) bit */
ret /* this also flushes prefetch-queue */
//ret返回时弹出main的地址
.align 2
.word 0
idt_descr:
.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
_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 */