How to Learn Linux kernel boot with cainiao

Source: Internet
Author: User

Note: Most of the content below is taken from the Linux kernel programming entry and the Linux kernel complete annotation.

 

I have a certain understanding of the entire Linux architecture and read the overall summary of the files in each directory.

You can start to check the source code of the kernel. The first step is to boot the kernel.


Information about tracks and sectors

Here we need to talk about the channels and sectors.

1. Slice: it is not a sector, that is, it is not the area between two radius, but a quadrilateral. The two radius are both sides, and the two concentric circles are used as the two outer sides.

2. Track: the track is not linear, but a region. It consists of two concentric circles.

3. cylinder: A column consisting of multiple tracks with the same number instead of a plane

4. Number of magnetic heads: because of the dual-sided storage of the disk, the number of magnetic heads is used for calculation instead of the number of disks.

It can be said that the sector is the smallest unit of Hard Disk segmentation.

Storage Capacity = number of cores * Number of tracks (cylindrical) * number of sectors * number of bytes per sector

_______________________________ | ----- Total number of bytes of the track -------- |

________________ | ------- Total number of bytes on a disk -------------- |

__________ | ----- Disk capacity (number of disk cores) ------------------ |

 

About register 8086

The Assembly Code is based on 8086, so it is better to first understand these registers and then look at the code.

1. Data registers are generally called General Register Groups.

8086 there are 8 8-bit data registers,

These 8-bit registers can form 16-bit registers respectively:

Ah & Al = AX: Accumulate register, which is often used in operations;

BH & BL = Bx: Base Address Register, often used for address index;

Ch & CL = Cx: The Count register, which is often used for counting;

DH & DL = DX: data register, often used for data transmission.

 

2. Address Register/segment address register

To use all the memory space, 8086 sets four segment registers to save the segment address:

CS (code segment): code segment register;

DS (Data Segment): Data Segment register;

SS (stack segment): Stack segment register;

ES (extra segment): additional segment register.

When a program is to be executed, it is necessary to determine the locations of the memory used by the program code, data, and stack. By setting the CS, DS, and SS segments, it points to these starting locations.

Generally, DS is fixed, and CS is modified as needed. Therefore, the program can be written as any size when the addressable space is smaller than 64 K. Therefore, the size of the program and its data combination is limited to 64 KB, which is why the COM file cannot exceed 64 KB.

 

3: registers with Special Functions

IP (Instruction Pointer): The instruction pointer register, used in conjunction with Cs, can track the program execution process;

SP (Stack pointer): A stack pointer that can be used with SS to point to the current stack position.

BP (base pointer): base address pointer register, which can be used as a relative base address location of SS;

Si (source index): the source address change register can be used to store the source address change pointer relative to the DS segment;

Di (destination index): the destination address change register, which can be used to store the destination address change pointer relative to the es segment.

There is also a flag register FR (flagregister) which has the following nine meaningful marks:

Of: overflow flag of indicates whether the result of the addition or subtraction operation of the number of symbols overflows. If the calculation result exceeds the range expressed by the current number of BITs, it is called overflow. The value of is set to 1. Otherwise, the value of is cleared to 0.

DF: The direction sign (DF) is used to determine the direction in which the pointer register is adjusted when the string operation command is executed.

If: indicates whether the if BIT is used to determine whether the CPU responds to the interrupt request that can be blocked outside the CPU. However, no matter what the value of this sign is, the CPU must respond to the interrupt requests from the outside of the CPU that cannot be blocked and the interrupt requests generated inside the CPU. The specific provisions are as follows:

(1) When if = 1, the CPU can respond to the interrupt requests externally blocked by the CPU;

(2) When if = 0, the CPU does not respond to the interrupt requests that can be blocked outside the CPU.

TF: the Status Control flag is used to control CPU operations. They must use special commands to make changes.

SF: Symbol sign SF is used to reflect the symbol bit of the calculation result. It is the same as the highest bit of the calculation result. In a microcomputer system, the signed number uses the complement representation. Therefore, SF also reflects the positive and negative signs of the calculation result. When the calculation result is a positive number, the value of SF is 0; otherwise, the value is 1.

ZF: the zero sign (ZF) indicates whether the calculation result is 0. If the calculation result is 0, the value is 1; otherwise, the value is 0. You can use this flag when determining whether the calculation result is 0.

AF: In the following cases, the value of the secondary carry flag AF is set to 1; otherwise, the value is 0:

(1) When the word operation occurs, the lower byte is carried to the high byte or the borrow digit;

(2) When the byte operation occurs, the lower 4 bits are carried to the upper 4 bits or the borrow bits.

PF: the parity mark PF is used to reflect the parity of the number of "1" in the calculation result. If the number of "1" is an even number, the PF value is 1; otherwise, the value is 0.

Cf: Carry mark CF is used to indicate whether the computation generates carry or borrow digits. If the highest bit of the calculation result produces a carry or borrow digit, the value is 1; otherwise, the value is 0 .)

 

BiOS Startup Process

1. Perform a series of tests on computer hardware to monitor what devices are currently in use and whether these devices are working properly. This phase is usually called post (power-on self-test, power-on self-test ). In this phase, some information, such as the BIOS version number, is displayed.

2. initialize the hardware device. This phase is important in the modern PCI-based architecture because it ensures that all hardware device operations will not cause conflicts between the IRQ line and the I/O port. At the end of this phase, a list of all installed PCI devices is displayed.

3. Search for an operating system to start it. In fact, according to BIOS settings, this process may attempt to access the first sector (pilot sector) of the floppy disk, hard disk, and CD-ROM in the system ).

4. If a valid device is found, copy the content of the first slice to the location starting from the physical address 0x00007c00 in Ram, and then jump to the address, start executing the loaded code.

 

Next, let's take a look at BIOS interruptions. There are too many specific functions. You can check them when you call them.

00 H Division

01 H single step

02 H non-screen interruption (NMI)

03 h breakpoint

04 H Overflow

05 h Screen Printing

06 -- 07 h Reserved

________________________________________

08 h clock interruption

09 h keyboard interruption

0ah Reserved/8259a slave disk interrupted

0bh Serial Port 2 interrupted

0ch Serial Port 1 interrupted

0dh hard drive (XT)/Parallel Port 2 interrupt

0eh floppy disk interruption

0fh printer/Parallel Port 1 interrupted

________________________________________

10 h video display I/O

11 h Device Configuration Detection

12 h memory capacity Detection

13 H disk I/O

14 h serial communication I/O

15 h band (PC)/Multi-Function

16 h keyboard I/O

17 h printer I/O

ROM-BASIC 18 h

19 h self-lifting program

1ah clock I/O

1bh Ctrl-break aborted

1ch Clock Control

1dh video initialization parameter table

1 eh floppy disk parameter table

1 FH graphic display extended memory table

________________________________________

20 H program termination and exit

21 H system function call

22 h program end address

23 h Ctrl-C Processing

24 h severe error handling

25 h disk sector read

26 h disk sector write

27 H program termination resident

28 h idle keyboard processing

29 H fast character output

2ah Reserved

________________________________________

2B -- 2dh Reserved

2EH Command Execution

2fh multi-channel interruption and sharing

________________________________________

30 -- 31 H far to int 21h entry

32 -- 3fh retained

________________________________________

40 h floppy disk I/O

Base table of 41 h hard drive machine 1

42 -- 45 h Reserved

46 h Reserved

47 -- 5bh retained

Base table of 5ch hard drive machine 2

5D -- 6fh Reserved

________________________________________

70 h real-time clock interruption

71 h switch to 0ah

72 h reserved for irqa

73 h reserved for irqb

74 h reserved for irqc

75 h coprocessor interruption

76 H hard drive interruption

77 H reserved for irqf

________________________________________

78 -- retain 7fh

________________________________________

80 -- f0h basic zone

________________________________________

F1 -- FFH Reserved

 

About bootsect. s

The bootsect. s code is the disk boot block program that resides in the first sector of the disk (Boot Sector, 0 track (cylindrical), 0 head, 1st sectors ). After our computer is powered on .....

1. First, rom bios self-check. Boot Sector is loaded from BIOS to memory 0x7c00 (if it is an embedded device, kernel code is usually loaded to memory by bootloader after power-on );

2. Then, move yourself to the memory 0x90000. The main function of this program is to first load the setup module from the disk to the position (0x90200) following the memory and then the bootsect ).

3. Use BIOS interrupt 0x13 to get the current boot disk parameters in the disk parameter table, and then display the "loadingsystem..." string on the screen.

4. Load the system module from the disk to the place where the memory starts from 0x10000. Then determine the device Number of the root file system. If no device number is specified, then, the type and type of the disk are determined based on the number of each track sector of the mounted boot disk and saved to the device number root_dev (0x508 address of the boot block ).

5. Execute the setup program at the beginning of the setup program (0x90200.

During multi-system guidance, grub, which we often see, is similar to replacing the function mission of bootsect.

 

About setup. s

The setup program uses rom bios to interrupt the reading of machine system data and save the data to the starting position of 0x90000. The parameters read and retained are shown in the following table:


1. The setup program moves the entire block of the system module 0x10000 -- 0x8fffff down to the absolute memory address 0x00000.

2. Load the Interrupt Descriptor Table register (idtr) and Global description table register (GDTR ).

3. Enable A20 address line

4. Reset the two interrupt control chips 8259a and the hardware interrupt number 0x20-0x2f.

5. Set the CPU control register Cr0 (machine status word) to run in 32-bit protection mode.

6. Jump to the head. s program located at the beginning of the system module to continue running.

 

About head. s

After the head. s program is compiled, it is connected to the first part of the system module. From this point on, the kernel is completely running in protection mode. Functions:

1. Load various data segment registers, re-set the Interrupt Descriptor Table, a total of 256 Items, and make each table item point to a dumb interrupt program that only reports errors.

2. Reset the Global Descriptor Table.

3. Check whether the A20 address line is enabled by comparing the content at the beginning of physical address 0 and 1 MB.

4. test whether the PC contains a digital coprocessor chip and set the corresponding flag in the Controller Cr0.

5. Set the paging processing mechanism for memory management, place the page Directory table at the beginning of absolute physical address 0, followed by four page tables that can address 16 MB of memory, and set their table items respectively.

6. Use the return command to bring up the entry address of the/init/Main. C program preplaced in the stack and run the main () program.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.