linux+v2.6.24/arch/x86/boot/header.S注释
/*
* header.S
*
* Copyright (C) 1991, 1992 Linus Torvalds
*
* Based on bootsect.S and setup.S
* modified by more people than can be counted
*
* Rewritten as a common file by H. Peter Anvin (Apr 2007)
*
* BIG FAT NOTE: We're in real mode using 64k segments. Therefore segment
* addresses must be multiplied by 16 to obtain their respective linear
* addresses. To avoid confusion, linear addresses are written using leading
* hex while segment addresses are written as segment:offset.
*
*/
#include
#include
#include
#include
#include
#include
#include "boot.h"
SETUPSECTS = 4 /* default nr of setup-sectors */
BOOTSEG = 0x07C0 /* original address of boot-sector */
SYSSEG = DEF_SYSSEG /* system loaded at 0x10000 (65536) */
SYSSIZE = DEF_SYSSIZE /* system size: # of 16-byte clicks */
/* to be loaded */
ROOT_DEV = 0 /* ROOT_DEV is now written by "build" */
SWAP_DEV = 0 /* SWAP_DEV is now written by "build" */
#ifndef SVGA_MODE
#define SVGA_MODE ASK_VGA
#endif
#ifndef RAMDISK
#define RAMDISK 0
#endif
#ifndef ROOT_RDONLY
#define ROOT_RDONLY 1
#endif
.code16
.section ".bstext", "ax"
.global bootsect_start
bootsect_start:
# Normalize the start address
ljmp $BOOTSEG, $start2
start2:
movw %cs, %ax
movw %ax, %ds
movw %ax, %es
movw %ax, %ss
xorw %sp, %sp
sti
cld
movw $bugger_off_msg, %si
msg_loop:
lodsb
andb %al, %al
jz bs_die
movb $0xe, %ah
movw $7, %bx
int $0x10
jmp msg_loop
bs_die:
# Allow the user to press a key, then reboot
xorw %ax, %ax
int $0x16
int $0x19
# int 0x19 should never return. In case it does anyway,
# invoke the BIOS reset code...
ljmp $0xf000,$0xfff0
.section ".bsdata", "a"
bugger_off_msg:
.ascii "Direct booting from floppy is no longer supported.\r\n"
.ascii "Please use a boot loader program instead.\r\n"
.ascii "\n"
.ascii "Remove disk and press any key to reboot . . .\r\n"
.byte 0
# Kernel attributes; used by setup. This is part 1 of the
# header, from the old boot sector.
.section ".header", "a"
.globl hdr
hdr:
setup_sects: .byte SETUPSECTS
root_flags: .word ROOT_RDONLY
syssize: .long SYSSIZE
ram_size: .word RAMDISK
vid_mode: .word SVGA_MODE
root_dev: .word ROOT_DEV
boot_flag: .word 0xAA55
# offset 512, entry point
.globl _start
_start:
# Explicitly enter this as bytes, or the assembler
# tries to generate a 3-byte jump here, which causes
# everything else to push off to the wrong offset.
# <@mrhopehub,注意三点>
#
# 1. 为什么采用硬编码,如上的英文注释,如果采用汇编语言jump,
# 则汇编后得到是三字节的跳转指令,这使得后续的字段都被“推移”到错误的偏移处
#
# 2. 注意最后的一个f(注:start_of_setup-1f中),表示向前(forward)的意思,
# 向后是back
#
# 3. 为什么跳转指令不写符号地址start_of_setup,如果是汇编语言的跳转指令可以写
# start_of_setup,汇编器计算得到的还是start_of_setup-1f这个相对偏移量
#
# @mrhopehub,注意三点>
.byte 0xeb # short (2-byte) jump
.byte start_of_setup-1f
1:
# Part 2 of the header, from the old setup.S
.ascii "HdrS" # header signature
.word 0x0207 # header version number (>= 0x0105)
# or else old loadlin-1.5 will fail)
.globl realmode_swtch
realmode_swtch: .word 0, 0 # default_switch, SETUPSEG
start_sys_seg: .word SYSSEG
.word kernel_version-512 # pointing to kernel version string
# above section of header is compatible
# with loadlin-1.5 (header v1.5). Don't
# change it.
type_of_loader: .byte 0 # = 0, old one (LILO, Loadlin,
# Bootlin, SYSLX, bootsect...)
# See Documentation/i386/boot.txt for
# assigned ids
# flags, unused bits must be zero (RFU) bit within loadflags
loadflags:
LOADED_HIGH = 1 # If set, the kernel is loaded high
CAN_USE_HEAP = 0x80 # If set, the loader also has set
# heap_end_ptr to tell how much
# space behind setup.S can be used for
# heap purposes.
# Only the loader knows what is free
#ifndef __BIG_KERNEL__
.byte 0
#else
.byte LOADED_HIGH
#endif
setup_move_size: .word 0x8000 # size to move, when setup is not
# loaded at 0x90000. We will move setup
# to 0x90000 then just before jumping
# into the kernel. However, only the
# loader knows how much data behind
# us also needs to be loaded.
code32_start: # here loaders can put a different
# start address for 32-bit code.
#ifndef __BIG_KERNEL__
.long 0x1000 # 0x1000 = default for zImage
#else
.long 0x100000 # 0x100000 = default for big kernel
#endif
ramdisk_image: .long 0 # address of loaded ramdisk image
# Here the loader puts the 32-bit
# address where it loaded the image.
# This only will be read by the kernel.
ramdisk_size: .long 0 # its size in bytes
bootsect_kludge:
.long 0 # obsolete
heap_end_ptr: .word _end+STACK_SIZE-512
# (Header version 0x0201 or later)
# space from here (exclusive) down to
# end of setup code can be used by setup
# for local heap purposes.
pad1: .word 0
cmd_line_ptr: .long 0 # (Header version 0x0202 or later)
# If nonzero, a 32-bit pointer
# to the kernel command line.
# The command line should be
# located between the start of
# setup and the end of low
# memory (0xa0000), or it may
# get overwritten before it
# gets read. If this field is
# used, there is no longer
# anything magical about the
# 0x90000 segment; the setup
# can be located anywhere in
# low memory 0x10000 or higher.
ramdisk_max: .long (-__PAGE_OFFSET-(512 << 20)-1) & 0x7fffffff
# (Header version 0x0203 or later)
# The highest safe address for
# the contents of an initrd
kernel_alignment: .long CONFIG_PHYSICAL_ALIGN #physical addr alignment
#required for protected mode
#kernel
#ifdef CONFIG_RELOCATABLE
relocatable_kernel: .byte 1
#else
relocatable_kernel: .byte 0
#endif
pad2: .byte 0
pad3: .word 0
cmdline_size: .long COMMAND_LINE_SIZE-1 #length of the command line,
#added with boot protocol
#version 2.06
hardware_subarch: .long 0 # subarchitecture, added with 2.07
# default to 0 for normal x86 PC
hardware_subarch_data: .quad 0
# End of setup header #####################################################
.section ".inittext", "ax"
start_of_setup:
#ifdef SAFE_RESET_DISK_CONTROLLER
# Reset the disk controller.
movw $0x0000, %ax # Reset disk controller
movb $0x80, %dl # All disks
int $0x13
#endif
# Force %es = %ds
# <@mrhopehub>
#
# bootloader加载内核把控制权交给内核时,ds/es/ss寄存器应该指向实模式内核代码的开始处。
# 所以需要kernel setup进行检查、纠正,
# 包括此处的Force %es = %ds、还有后面对
# %ds == %ss?的检查
# %cs的纠正
#
# @mrhopehub>
movw %ds, %ax
movw %ax, %es
cld
# Apparently some ancient versions of LILO invoked the kernel with %ss != %ds,
# which happened to work by accident for the old code. Recalculate the stack
# pointer if %ss is invalid. Otherwise leave it alone, LOADLIN sets up the
# stack behind its own code, so we can't blindly put it directly past the heap.
# <@mrhopehub,堆栈设置>
#
# 从259行开始到2:处完成sp(放在dx中)的设置,2:到movzwl %dx, %esp
# 完成ss、sp的装载
#
# @mrhopehub,堆栈设置>
movw %ss, %dx
# <@mrhopehub>
# 255行的结果为:%ax == %ds,270行的结果为:%dx == %ss,
# 所以%ds == %ss?可以通过cmpw %ax, %dx实现
# @mrhopehub>
cmpw %ax, %dx # %ds == %ss?
# <@mrhopehub>
#
# 跳转到2f之前,需要把栈顶指针放入%dx,
# 参见2: # Now %dx should point to the end of our stack space
# 此处假设bootloader已经正确设置sp,只需要直接把%sp转移到%dx
#
# @mrhopehub>
movw %sp, %dx
je 2f # -> assume %sp is reasonably set
# Invalid %ss, make up a new stack
# <@mrhopehub,没有跳转,堆栈无效,需要重新设置堆栈>
#
# 堆栈的设置:栈最小为512,_end为BSS节结尾处,(STACK_SIZE-512)为堆大小,
# _end+(STACK_SIZE-512)表示堆结尾处。
# 1. 只是用栈、不适用堆:sp(实际存放在dx中)指向_end+STACK_SIZE的位置
#
# 2. 同时使用堆、栈:
# sp(实际存放在dx中)指向_end+(STACK_SIZE-512)+STACK_SIZE的位置
#
# 3. 源码中并不使用堆,有文章写到bootloader会设置CAN_USE_HEAP位
# 但是需要注意boot.h中#define STACK_SIZE 512,也就是默认情况下
# 即使使用堆,跟不使用堆的内存布局是一样的
#
# @mrhopehub,没有跳转,堆栈无效,需要重新设置堆栈>
movw $_end, %dx
# <@mrhopehub>
# CAN_USE_HEAP不是bootparam.h中的#define CAN_USE_HEAP (1<<7)
# 而是152行CAN_USE_HEAP = 0x80定义的汇编常量
# @mrhopehub>
testb $CAN_USE_HEAP, loadflags
# <@mrhopehub>
# 不使用堆,设置%dx为_end,1: addw $STACK_SIZE, %dx完成sp的计算
# @mrhopehub>
jz 1f
# <@mrhopehub>
# 使用堆,设置%dx为_end+(STACK_SIZE-512)(堆结尾处),
# 1: addw $STACK_SIZE, %dx完成sp的计算
# @mrhopehub>
movw heap_end_ptr, %dx
1: addw $STACK_SIZE, %dx
# <@mrhopehub>
# 如果发生进位,设置dx=0
# @mrhopehub>
jnc 2f
xorw %dx, %dx # Prevent wraparound
2: # Now %dx should point to the end of our stack space
# <@mrhopehub>
# 4字节对齐,使得加载数据的效率更高
# @mrhopehub>
andw $~3, %dx # dword align (might as well...)
jnz 3f
movw $0xfffc, %dx # Make sure we're not zero
# <@mrhopehub>
# 不管前面的ss是否等于ds,这里都重新装载ss(%ax=%ds)
# @mrhopehub>
3: movw %ax, %ss
movzwl %dx, %esp # Clear upper half of %esp
# <@mrhopehub,开启中断>
# bootloader禁止中断,此处开启中断,
# 注意开启中断的时机,堆栈建立好之后立即开启中断
# @mrhopehub,开启中断>
sti # Now we should have a working stack
# We will have entered with %cs = %ds+0x20, normalize %cs so
# it is on par with the other segments.
# <@mrhopehub,纠正CS>
#
# 因为GRUB将内核装载至从地址0x9 0000开始的物理内存后,
# 执行一条长跳转指令jmp_far(seg+0x20, 0)跳过了512字节大小的bootsect,
# 该指令将cs寄存器的值设置为0x9020,而其他的一系列段寄存器
# ds/es/ss/fs/gs的值均指向起始地址0x9000处,
# 因此在执行上述三条指令后将所有的段寄存器的值均设置为0x9000
#
# @mrhopehub>
pushw %ds
pushw $6f
lretw
6:
# Check signature at end of setup
cmpl $0x5a5aaa55, setup_sig
jne setup_bad
# Zero the bss
movw $__bss_start, %di
movw $_end+3, %cx
xorl %eax, %eax
subw %di, %cx
shrw $2, %cx
# <@mrhopehub>
# REP可以是任何字符传指令(CMPS, LODS, MOVS, SCAS, STOS)的前缀.
# REP能够引发其后的字符串指令被重复, 只要ecx的值不为0, 重复就会继续.
# 每一次字符串指令执行后, ecx的值都会减小.
# 如果设置了direction flag, 那么edi会在该指令执行后减小,
# 如果没有设置direction flag, 那么edi的值会增加.
# @mrhopehub>
rep; stosl
# Jump to C code (should not return)
calll main
# Setup corrupt somehow...
setup_bad:
movl $setup_corrupt, %eax
calll puts
# Fall through...
.globl die
.type die, @function
die:
hlt
jmp die
.size die, .-die
.section ".initdata", "a"
setup_corrupt:
.byte 7
.string "No setup signature found...\n"
完全可以把Header.S文件看做是_main函数的实现。
标号1和标号start_of_setup之间有一大串变量,这些变量是用于内核的加载以及初始化过程的,它们的值有的由编译内核过程中build标号1和标号start_of_setup之间有一大串变量,这些变量是用于内核的加载以及初始化过程的,它们的值有的由编译内核过程中build.c工具程序写入,有的由boot loader在加载内核时写入,这些就是前面说的boot loader与内核之间沟通的协议。我们忽略这些值的初始化过程,并默认内核被成功加载后这些值都是可用的。