Linux Kernel Startup Process Analysis
1. Linux kernel startup Protocol
Read Documents \ linux-2.6.35 \ Documentation \ x86 \ boot.txt
Traditionally, it supports the memory layout for starting and loading the Image and zImage kernels (the layout is as follows for Kernel loading earlier than 2.4 ):
|
0A0000 + ------------------------ +
| Reserved for BIOS | Do not use. Reserved for bios ebda.
09A000 + ------------------------ +
| Command line |
| Stack/heap | For use by the kernel real-mode code.
098000 + ------------------------ +
| Kernel setup | The kernel real-mode code.
090200 + ------------------------ +
| Kernel boot sector | The kernel legacy boot sector.
090000 + ------------------------ +
| Protected-mode kernel | The bulk of the kernel image.
010000 + ------------------------ +
| Boot loader | <-Boot sector entry point 0000: 7C00
001000 + ------------------------ +
| Reserved for MBR/BIOS |
000800 + ------------------------ +
| Typically used by MBR |
000600 + ------------------------ +
| BIOS use only |
000000 + ------------------------ +
When bzImage is used, the kernel in protection mode will be relocated to 0x1000000 (high-end memory), and the code in the internal validation mode (boot sector, setup and stack/heap) it will be compiled into any address that can be relocated between 0x100000 and the bottom of the low-end memory. Unfortunately, in Boot protocols 2.00 and 2.01, 0x90000 + memory is still used inside the kernel. The 2.02 Boot Protocol solves this problem. Boot loader should interrupt the 12 h BIOS call to check how much memory is available in the low-end memory.
People want to "memory ceiling", that is, the pointer at the highest position in the low-end memory that boot loader touches, as low as possible, because some new BIOS began to allocate some fairly large memory, the so-called extended BIOS data domain is almost close to the top of the low-end memory.
Unfortunately, if the BIOS 12 h interrupt report says the memory size is too small, the boot loader will not do anything except report an error. Therefore, boot loader should be designed to occupy as little low-end memory as possible. For zImage and the former bzImage, this requires that the data can be written to the x090000 segment. boot loader should ensure that the memory above 0x9A000 pointer is not used; many BIOS will terminate above this pointer.
For a modern bzImage kernel with a boot protocol> = 2.02, its memory layout uses the following format:
| Protected-mode kernel |
100000 + ------------------------ +
| I/O memory hole |
0A0000 + ------------------------ +
| Reserved for BIOS | Leave as much as possible unused
~ ~
| Command line | (Can also be below the X + 10000 mark)
X + 10000 + ------------------------ +
| Stack/heap | For use by the kernel real-mode code.
X + 08000 + ------------------------ +
| Kernel setup | The kernel real-mode code.
| Kernel boot sector | The kernel legacy boot sector.
X + ------------------------ +
| Boot loader | <-Boot sector entry point 0000: 7C00
001000 + ------------------------ +
| Reserved for MBR/BIOS |
000800 + ------------------------ +
| Typically used by MBR |
000600 + ------------------------ +
| BIOS use only |
000000 + ------------------------ +
The program segment address is determined by the grub size. Address X should be as low as possible within the range allowed by bootloader.
2. bios post process
Traditionally, since the CPU is powered on, the CPU can only access data in the ROM or RAM, but there is no computer operating system at this time, therefore, a program is required to load the operating system stored in non-volatile storage media (such as hard disks) to RAM. This program is stored in ROM, and BIOS is one of these programs. BIOS is mainly manufactured by two manufacturers and reside in the ROM of the motherboard. With BIOS, hardware manufacturers only need to focus on hardware, rather than software. The BIOS service program is implemented by calling the interrupt service program. When the BIOS loads the bootloader program, the Bootloader can also obtain system information from the BIOS through the interruption provided by the BIOS. The process is as follows:
(1) The power supply starts the clock generator and generates a # POWERGOOD interrupt on the bus.
(2) cpu reset interruption (the CPU is in the 8086 working mode ).
(3) enter the bios post code: % ds = % es = % fs = % gs = % ss = 0, % cs = 0xFFFF0000, % eip = 0x0000FFF0 (rom bios post code, instruction pointer eip, data segment register ds, code segment register cs ).
(4) perform all POST checks when the interrupt is invalid.
(5) initialize the interrupt vector table IVT at address 0.
(6) 0x19 interrupt: the BIOS boot loader is called with the Startup Device number as the parameter. This program starts to load data from the 0 sector and 1 Sector of the boot device (hard disk) to the memory physical address 0x7C00. This 0 sector 1 sector is called Boot sector (Boot sector), a total of 512 bytes, also known as MBR.
That is to say, the CPU executes the BIOS assembly program at the BIOS entry point (CS: IP = FFFF: 0000). The BIOS program functions to detect system hardware and provides interrupt access interfaces to access hardware. Then, the BIOS program calls the bootloader program on the disk MBR by interrupting 0x19, loads the bootloader program to ox7c00, and jumps to 0x7c00. In this way, the bootloader program located at 0x7c00, you can run the command.
Starting from executing the bootloader program in MBR in BIOS, it is what the linux code is doing.
3. Bootloader Process
The bootloader program loads the operating system of a computer. Boot is short for bootstrap, and bootstrap is the meaning of bootstrap commands. The bootloader program is usually located on the hard disk and called by the BIOS to load the kernel. On PC, common bootloaders include grub, lilo, and syslinux.
GRUB (GRand uniied Bootloader) is the default boot program for many linux releases. In embedded systems, the most common bootloader is U-BOOT. Such bootloader is generally located at the front of MBR. In linux, bootloader can also be written to the partition where the file system is located. For example, the grub program is very powerful. After Gurb runs, it initializes the environment required for Kernel running. Then load the kernel image.
Grub disk boot process:
(1) stage1: grub reads the first 512 bytes (0 sectors, 0 sectors, 0 sectors, 1 sectors, MBR (Master Boot Record), also known as bootsect) of the disk ). MBR consists of three parts: Bootstrap code, partition table, and magic number.
(2) stage00005: identifies different file system formats. This allows grub to recognize the file system.
(3) stage2: load the system boot menu (/boot/grub/menu. lst or grub. lst) and the kernel vmlinuz and RAM disk initrd.
4. kernel Startup Process
Kernel image file vmlinuz: an executable file that contains a static link to the Linux kernel. Traditionally, vmlinux is called a bootable kernel image. Vmlinuz is a compressed file of vmlinux. The structure is as follows:
(1) first 512 bytes (previously in arch/i386/boot/bootsect. S );
(2) second, a piece of code, several segments of no more than 512 bytes (previously in arch/i386/boot/setup. S );
(3) kernel code in protection mode (in arch/x86/boot/main. c ).
BzImage file: Use the make bzImage command to compile the kernel source code to obtain the zImage file compressed using the zlib algorithm, that is, the big zImage file. The old zImage decompress the kernel to the low-end memory, while the bzImage decompress the kernel to the high-end memory (1 M (0x100000) or above), which is executed in protection mode. BzImage files include vmlinuz, bootsect. o, setup. o, decompression program misc. o, and other related files (such as piggy. o ). Note: In the Linux 2.6 kernel, bootsect. S and setup. S are integrated into header. S.
Initramfs (or initrd) file: initrd indicates initialized ram disk. It is mainly used to load hardware driver modules, assist in kernel startup, and mount a real root file system.
For example, the grub startup items on my computer are as follows (in/boot/grub. lst ):
Title Fedora (2.6.35.10-74. fc14.i686)
Root (hd0, 0)
Kernel/vmlinuz-2.6.35.10-74.fc14.i686 ro root =/dev/mapper/VolGroup-lv_root rd_LVM_LV = VolGroup/lv_root volume = VolGroup/lv_swap rd_NO_LUKS rd_NO_MD rd_NO_DM LANG = zh_CN.UTF-8 KEYBOARDTYPE = pc KEYTABLE = us rhquiet
Initrd/initramfs-2.6.35.10-74.fc14.i686.img
Kernel execution parameters can control kernel behavior. For example, ro parameters tell the kernel to mount the root partition in read-only mode, while quiet tells the kernel not to print any information during startup. These parameters not only affect kernel execution, but are also used by most distributions to control subsequent actions after startup. These parameters can be obtained from the/proc/cmdline file at any time. Now grub finds the kernel (hd0, 0)/boot/vmlinuz-2.6.35.10-74.fc14.i686, which gives control of the entire computer to the program, the kernel starts various initialization actions, you can remove the quiet parameter to check what has been done by the kernel. You can also run the dmesg command to check what has been printed when the kernel is started.
The startup process is related to the architecture. For the 2.6 kernel and x86 architecture, when the CPU is initialized upon power-on, the command register CS: EIP is always initialized as a fixed value, this is the address of the first command after CPU reset. For a 32-bit address bus system, the 4 GB physical space is divided into at least two parts, one is the memory address space, and the other is the address space used to address the BIOS chip storage unit. After x86 is reset, it works in real mode. In this mode, the addressing space of the CPU is 1 MB. CS: the IP address reset value is FFFF: 0000, and the physical address is FFFF0. The motherboard designer must ensure that the physical address is mapped to the BIOS chip, rather than the RAM.
The first step to load the Linux kernel is to load the real-mode code (boot sector and setup code), and then check the parameter values in the header (header) at the offset 0x01f1. The real-mode code has a total of 32 K, but the boot loader can choose to load only the first two sectors (1 K), and then check the size of the bootup sector.
The format of each field in the header is as follows:
Offset/Size Proto Name Meaning
01F1/1 ALL (1 setup_sects The size of the setup in sectors
01F2/2 ALL root_flags If set, the root is mounted readonly
01F4/4 2.04 + syssize The size of the 32-bit code in 16-byte paras
01F8/2 ALL ram_size do not use-for bootsect. S use only
01FA/2 ALL vid_mode Video mode control
01FC/2 ALL root_dev Default root device number
01FE/2 ALL boot_flag 0xAA55 magic number
0200/2 2.00 + jump Jump instruction
0202/4 2.00 + header Magic signature "HdrS"
0206/2 2.00 + version Boot protocol version supported
0208/4 2.00 + realmode_swtch Boot loader hook (see below)
020C/2 2.00 + start_sys_seg The load-low segment (0x1000) (obsolete)
020E/2 2.00 + kernel_version Pointer to kernel version string
0210/1 2.00 + type_of_loader Boot loader identifier
0211/1 2.00 + loadflags Boot protocol option flags
0212/2 2.00 + setup_move_size Move to high memory size (used with hooks)
0214/4 2.00 + code32_start Boot loader hook (see below)
0218/4 2.00 + ramdisk_image initrd load address (set by boot loader)
021C/4 2.00 + ramdisk_size initrd size (set by boot loader)
0220/4 2.00 + bootsect_kludge do not use-for bootsect. S use only
0224/2 2.01 + heap_end_ptr Free memory after setup end
0226/1 2.02 + ext_loader_ver Extended boot loader version
0227/1 2.02 + ext_loader_type Extended boot loader ID
0228/4 2.02 + pai_line_ptr 32-bit pointer to the kernel command line
022C/4 2.03 + ramdisk_max Highest legal initrd address
0230/4 2.05 + kernel_alignment Physical addr alignment required for kernel
0234/1 2.05 + relocatable_kernel Whether kernel is relocatable or not
0235/1 2.10 + min_alignment Minimum alignment, as a power of two
0236/2 N/A pad3 & nbsp; Unused
0238/4 2.06 + bytes line_size Maximum size of the kernel command line
023C/4 2.07 + hardware_subarch Hardware subarchitecture
0240/8 2.07 + hardware_subarch_data Subarchitecture-specific data
0248/4 2.08 + payload_offset Offset of kernel payload
024C/4 2.08 + payload_length Length of kernel payload
0250/8 2.09 + setup_data 64-bit physical pointer to linked list of struct setup_data
0258/8 2.10 + pref_address Preferred loading address
0260/4 2.10 + init_size Linear memory required during initialization
For more details, please continue to read the highlights on the next page: