Before the Linux system kernel main function executes

Source: Internet
Author: User

1, Linux is an operating system after the machine power, need to from the hardware through a boot program to load OS kernel, then the main function of OS kernel before running, what happened?

(1) boot bios (stored in ROM chip, Rom: read-only memory, now generally flash) Start (Prepare interrupt vector table in real mode and interrupt service program)

Real mode: Intel80x86 series of a CPU operating mode, features, 20-bit address addressing (1MB), direct access to the BIOS and peripheral hardware, no hardware support real-time multi-tasking and paging mechanism.

BIOS task: Load the OS into RAM (random access memory, which is a common memory bar)

All CPUs are forced into the 16-bit real mode when power is on, some special values of the CPU at this time:

Cs:0xf0000,ip:0xfff0 so the starting address is Cs:ip = 0xffff0 is the address of the BIOS.

CS: Code Snippet Register

IP: Instruction pointer register (IP corresponds to 16 bits, EIP corresponds to 32 bits, rip corresponds to 64 bits)

Instruction address: Code snippet in fact address + instruction segment Offset address = CS + IP

Position of Interrupt vector table: 0x00000 ~ 0x003ff (altogether 1kb): altogether 256 interrupt vectors (CS:IP), each 4B (CS:2B;IP:2B)

BIOS Data area: 0x00400 ~ 0x004ff

Interrupt Service Program: 0x0e50b ~ 0x0fffe

interrupt int

(2) loading the first part of the kernel code (BOOTSECT)

BIOS received int 0x19, go to interrupt Service program 0X0E6F2 (Start Load Service Program entry address): Load the 512B code of the first sector into the memory development location (0X07C00)

(3) Loading the second part of the code:

First plan the memory and then copy itself from 0X07C00 to 0x90000

(ds/es/fs/gs/ss segment register, SP stack top pointer)

Then the int 0x13, which loads the SETUP.S part of the code into the memory 0x90200.

Bootsect and Setup.s are tightly connected at this time the value of SS:SP is 0x9ff00

(4) Loading system module

Bootsect uses the int 0x13 to load the system module, performs the load task of the main program Read_it, will be about 120KB content load value 0x10000 (SYSSEG) place

Then confirm the root device number, root file system device (root device): The system uses tools to make a file system and load it into the machine.

(5) via Jumpi 0,setupseg: Jump to Setup.s (0x90200), execute setup

Use the interrupt service program provided by the BIOS to read the machine system data from the device, read the hard disk Parameters table 1 and the hard disk parameters table 2 from 0x41 and 0x46, respectively, placed in

0x90080 and 0x90090 Place

System data is loaded into memory: 0X90000-0X901FD total:510b

Start real mode to protected mode change

(6) Shutdown interrupt (CPU if bit: 0)

Open interrupts and Guanzhong (CLI and STI)

Copy Kernel to memory 0x00000 (release BIOS interrupt vector table and interrupt service program, then move kernel)

Set interrupt descriptor and global descriptor (IDT and GDT),

Interrupt Descriptor Descriptor Register (IDTR)

The only array in the Global Descriptor Descriptor Register (GDTR) system that holds the contents of a segment register, with addressing in protected mode

************************************************************************************************

Local Descriptor Descriptor (LDT) address and Task status segment (TSS) address of the task to complete the addressing of each segment of the process, protection and restoration of the city site

GDTR marks the entrance to the GDT, typically LGDT the GDT base address to GDTR

IDT: The entry address of the interrupt service program.

IDTR: Save the start address of IDT. (Lidt command completed)

*************************************************************************************************

(7) Open the A20 for 32-bit addressing (otherwise, in 16-bit mode, the over-range addressing will cause the addressing rollback)

32-bit addressing: maximum addressing space 4GB

Executive Head.s

First meet the programmable Interrupt Controller: 8259A: Single-chip manageable 8-level vector-priority interrupt (cascade, up to 64-level vector interrupt management)

int 0x00 ~ int 0x1F Internal interrupts (non-blocking) and abnormal interrupts

Protected mode: irq0x00 ~ irq0x0f corresponding interrupt number: int 0x20 ~ int 0x2F

To set the protected mode code:

mov ax, #0x0001

LMSW AX

At this point the CR0 register No. 0 bit: PE 1

Jump to Head.s entrance to the end of Setup to start executing the head file.

Note: x87: math coprocessor, external optional (from 486 after general change to built-in).

The Head.s is compiled into the target code, and the C language kernel compiles into the target code, which is connected to the system module.

Where head:

25KB + 184B

Head in 0x000000 location create also catalog table, page table, buffer, Gdt,idt (occupies the code that covers the head already executed)

Kernel start position 0x000000 _pg_dir (page Table directory start location)

Rebuild a set of GDT because the source GDT memory location will be overwritten.

Check the math coprocessor

The final preparation:

L6 marking and main function entry address stack, L6 is the main function of abnormal exit of the program transfer point: Cheng jump to setup_paging to establish paging mechanism.

One page: 4KB

Main function Entry: 0x064b8

Global Descriptor Descriptor: 0X05CB8

Interrupt Descriptor Descriptor: 0X054B8

Remaining 184b:0x05400

Floppy disk buffer: 0x05000

Page Table 3 to page table 0 starting from: 0x01000 0x03000 0x02000 0x01000

Page Table directory: 0x00000

PG bit CR0 register 31st bit: Controls paging.

CR3 Register: Paging mechanism virtual address to physical address mapping.

Then start calling Main.

Before the Linux system kernel main function executes

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.