linux kernel head.S文件解析

#include <linux/threads.h>
#include <linux/linkage.h>
#include <asm/segment.h>
#include <asm/page.h>
#include <asm/pgtable.h>
#include <asm/desc.h>
#include <asm/cache.h>
#include <asm/thread_info.h>
#include <asm/asm-offsets.h>
#include <asm/setup.h>

#define X86  new_cpu_data+CPUINFO_x86
#define X86_VENDOR new_cpu_data+CPUINFO_x86_vendor
#define X86_MODEL new_cpu_data+CPUINFO_x86_model
#define X86_MASK new_cpu_data+CPUINFO_x86_mask
#define X86_HARD_MATH new_cpu_data+CPUINFO_hard_math
#define X86_CPUID new_cpu_data+CPUINFO_cpuid_level
#define X86_CAPABILITY new_cpu_data+CPUINFO_x86_capability
#define X86_VENDOR_ID new_cpu_data+CPUINFO_x86_vendor_id

 * This is how much memory *in addition to the memory covered up to
 * and including _end* we need mapped initially.  We need one bit for
 * each possible page, but only in low memory, which means
 * 2^32/4096/8 = 128K worst case (4G/4G split.)
 * Modulo rounding, each megabyte assigned here requires a kilobyte of
 * memory, which is currently unreclaimed.
 * This should be a multiple of a page.
#define INIT_MAP_BEYOND_END (128*1024) //内存映射表大小


 * Set segments to known values.
 lgdt boot_gdt_descr - __PAGE_OFFSET   //加载全局段描述符表,这里减__PAGE_OFFSET,是为了得到物理地址
 movl $(__BOOT_DS),%eax   
 movl %eax,%ds
 movl %eax,%es
 movl %eax,%fs
 movl %eax,%gs

 * Clear BSS first so that there are no surprises...
 * No need to cld as DF is already clear from cld above...
 xorl %eax,%eax
 movl $__bss_start - __PAGE_OFFSET,%edi
 movl $__bss_stop - __PAGE_OFFSET,%ecx
 subl %edi,%ecx
 shrl $2,%ecx
 rep ; stosl
 * Copy bootup parameters out of the way.
 * Note: %esi still has the pointer to the real-mode data.
 * With the kexec as boot loader, parameter segment might be loaded beyond
 * kernel image and might not even be addressable by early boot page tables.
 * (kexec on panic case). Hence copy out the parameters before initializing
 * page tables.

 movl $(boot_params - __PAGE_OFFSET),%edi
 movl $(PARAM_SIZE/4),%ecx
 movl boot_params - __PAGE_OFFSET + NEW_CL_POINTER,%esi
 andl %esi,%esi
 jnz 2f   # New command line protocol
 jne 1f
 movzwl OLD_CL_OFFSET,%esi
 addl $(OLD_CL_BASE_ADDR),%esi
 movl $(saved_command_line - __PAGE_OFFSET),%edi
 movl $(COMMAND_LINE_SIZE/4),%ecx

 * Initialize page tables.  This creates a PDE and a set of page
 * tables, which are located immediately beyond _end.  The variable
 * init_pg_tables_end is set up to point to the first "safe" location.
 * Mappings are created both at virtual address 0 (identity mapping)
 * and PAGE_OFFSET for up to _end+sizeof(page tables)+INIT_MAP_BEYOND_END.
 * Warning: don't use %esi or the stack in this code.  However, %esp
 * can be used as a GPR if you really need it...
// |内核|pg0 (pg1)页表|内存映射表| 内存布局

page_pde_offset = (__PAGE_OFFSET >> 20); //换算出该页表在页表项表中的索引号

 movl $(pg0 - __PAGE_OFFSET), %edi      
 movl $(swapper_pg_dir - __PAGE_OFFSET), %edx
 movl $0x007, %eax   /* 0x007 = PRESENT+RW+USER */
 leal 0x007(%edi),%ecx   /* Create PDE entry */
 movl %ecx,(%edx)   /* Store identity PDE entry */ //使页表项表的第0项指向pg0
 movl %ecx,page_pde_offset(%edx)  /* Store kernel PDE entry */ //使页表项表的内核开始处的项也指向pg0
 addl $4,%edx               //页表项表的下一项
 movl $1024, %ecx          
 addl $0x1000,%eax
 loop 11b
 /* End condition: we must map up to and including INIT_MAP_BEYOND_END */
 /* bytes beyond the end of our own page tables; the +0x007 is the attribute bits */
 leal (INIT_MAP_BEYOND_END+0x007)(%edi),%ebp  //得到内存映射表末端的物理地址
 cmpl %ebp,%eax       //比较页表影射的物理地址是否已经覆盖了该内存映射表的地址
 jb 10b  //如果没有,初始化下一个页表
 movl %edi,(init_pg_tables_end - __PAGE_OFFSET) //页表结束地址放入该变量中


 * Enable paging
 //1 把页表地址装入CR3
 //2 设置CR0寄存器开启分页

 movl $swapper_pg_dir-__PAGE_OFFSET,%eax
 movl %eax,%cr3  /* set the page table pointer.. */
 movl %cr0,%eax  
 orl $0x80000000,%eax
 movl %eax,%cr0  /* ..and set paging (PG) bit */
 ljmp $__BOOT_CS,$1f /* Clear prefetch and normalize %eip */
 lss stack_start,%esp


 pushl $0

 call setup_idt

is386: movl $2,%ecx  # set MP
2: movl %cr0,%eax
 andl $0x80000011,%eax # Save PG,PE,ET
 orl %ecx,%eax
 movl %eax,%cr0

 call check_x87
 lgdt cpu_gdt_descr //重新加载GDT寄存器
 lidt idt_descr     //加载IDT寄存器
 ljmp $(__KERNEL_CS),$1f
1: movl $(__KERNEL_DS),%eax # reload all the segment registers
 movl %eax,%ss   # after changing gdt.

 movl $(__USER_DS),%eax  # DS/ES contains default USER segment
 movl %eax,%ds
 movl %eax,%es

 xorl %eax,%eax   # Clear FS/GS and LDT
 movl %eax,%fs
 movl %eax,%gs
 lldt %ax
 cld   # gcc2 wants the direction flag cleared at all times
 pushl %eax  # fake return address

 jmp start_kernel  //跳到start_kernel去初始化内核


 *  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 PAGE_OFFSET. Interrupts
 *  are enabled elsewhere, when we can be relatively
 *  sure everything is ok.
 *  Warning: %esi is live across this function.
 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_table,%edi
 mov $256,%ecx
 movl %eax,(%edi)
 movl %edx,4(%edi)
 addl $8,%edi
 dec %ecx
 jne rp_sidt


 * BSS section
.section ".bss.page_aligned","w"
ENTRY(swapper_pg_dir)  //临时页表项表,也是正式的页表项表
 .fill 1024,4,0
ENTRY(empty_zero_page) //空页
 .fill 4096,1,0

 * This starts the data section.
 .long init_thread_union+THREAD_SIZE
 .long __BOOT_DS

ready: .byte 0

 .asciz "Unknown interrupt or fault at EIP %p %p %p/n"

 * The IDT and GDT 'descriptors' are a strange 48-bit object
 * only used by the lidt and lgdt instructions. They are not
 * like usual segment descriptors - they consist of a 16-bit
 * segment size, and 32-bit linear address value:

.globl boot_gdt_descr
.globl idt_descr

# early boot GDT descriptor (must use 1:1 address mapping)
 .word 0    # 32 bit align gdt_desc.address
 .word __BOOT_DS+7
 .long boot_gdt_table - __PAGE_OFFSET

 .word 0 # 32-bit align idt_desc.address
 .word IDT_ENTRIES*8-1  # idt contains 256 entries
 .long idt_table

# boot GDT descriptor (later on used by CPU#0):
 .word 0 # 32 bit align gdt_desc.address
 .word GDT_ENTRIES*8-1
 .long cpu_gdt_table

 * The boot_gdt_table must mirror the equivalent in setup.S and is
 * used only for booting.
 .align L1_CACHE_BYTES
 .fill GDT_ENTRY_BOOT_CS,8,0
 .quad 0x00cf9a000000ffff /* kernel 4GB code at 0x00000000 */
 .quad 0x00cf92000000ffff /* kernel 4GB data at 0x00000000 */

 * The Global Descriptor Table contains 28 quadwords, per-CPU.
 .align L1_CACHE_BYTES
 .quad 0x0000000000000000 /* NULL descriptor */
 .quad 0x0000000000000000 /* 0x0b reserved */
 .quad 0x0000000000000000 /* 0x13 reserved */
 .quad 0x0000000000000000 /* 0x1b reserved */
 .quad 0x0000000000000000 /* 0x20 unused */
 .quad 0x0000000000000000 /* 0x28 unused */
 .quad 0x0000000000000000 /* 0x33 TLS entry 1 */
 .quad 0x0000000000000000 /* 0x3b TLS entry 2 */
 .quad 0x0000000000000000 /* 0x43 TLS entry 3 */
 .quad 0x0000000000000000 /* 0x4b reserved */
 .quad 0x0000000000000000 /* 0x53 reserved */
 .quad 0x0000000000000000 /* 0x5b reserved */

 .quad 0x00cf9a000000ffff /* 0x60 kernel 4GB code at 0x00000000 */
 .quad 0x00cf92000000ffff /* 0x68 kernel 4GB data at 0x00000000 */
 .quad 0x00cffa000000ffff /* 0x73 user 4GB code at 0x00000000 */
 .quad 0x00cff2000000ffff /* 0x7b user 4GB data at 0x00000000 */

 .quad 0x0000000000000000 /* 0x80 TSS descriptor */
 .quad 0x0000000000000000 /* 0x88 LDT descriptor */

  * Segments used for calling PnP BIOS have byte granularity.
  * They code segments and data segments have fixed 64k limits,
  * the transfer segment sizes are set at run time.
 .quad 0x00409a000000ffff /* 0x90 32-bit code */
 .quad 0x00009a000000ffff /* 0x98 16-bit code */
 .quad 0x000092000000ffff /* 0xa0 16-bit data */
 .quad 0x0000920000000000 /* 0xa8 16-bit data */
 .quad 0x0000920000000000 /* 0xb0 16-bit data */

  * The APM segments have byte granularity and their bases
  * are set at run time.  All have 64k limits.
 .quad 0x00409a000000ffff /* 0xb8 APM CS    code */
 .quad 0x00009a000000ffff /* 0xc0 APM CS 16 code (16 bit) */
 .quad 0x004092000000ffff /* 0xc8 APM DS    data */

 .quad 0x0000920000000000 /* 0xd0 - ESPFIX 16-bit SS */
 .quad 0x0000000000000000 /* 0xd8 - unused */
 .quad 0x0000000000000000 /* 0xe0 - unused */
 .quad 0x0000000000000000 /* 0xe8 - unused */
 .quad 0x0000000000000000 /* 0xf0 - unused */
 .quad 0x0000000000000000 /* 0xf8 - GDT entry 31: double-fault TSS */

个人分类: Linux Kernel
想对作者说点什么? 我来说一句