How does the Linux Startup code enter the main function in C?

Source: Internet
Author: User
Article Title: How does the Linux Startup code enter the main function in C ?. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

Note:

1. If you want to use the call command, you must create a stack, that is, assign a value to the ss and sp.

2. push the main function to the stack in the assembly code. If you want to run the main function, ret is enough, because ret will find the first IP address from the stack, then pop the ip address to the instruction register, and the CPU starts to execute the main function;

3. directly jump to the absolute physical address of the memory in the Assembly to start execution. This requires that the ld must specify the address of the main function in the memory during connection;

For example, after entering the protection mode, if we want to start the main function execution, we can jump through the command:

Jmp

08 h: 01000 h; Jump

To section 08 h (code), offset 01000 h

The above 08h is a hexadecimal number of segment delimiters, And the binary value is 1 0.

1st in gdt. According to the gdt configuration, we know that it is a code segment. Here, the BASEADDRESS of our code segment is 0, so our jmp command jumps to the memory address 0x1000.

If cr0 31 (PG--PAGE), 0 (PE--PROTECT ENABLE) bit are enabled, this address also through the page transformation to become a physical address.

The command for loading gdt is lgdt. The required operand is 6 bytes. The first two bytes are the size of the gdt table, and the last four bytes are the addresses of the gdt table. The gdt table is loaded into the gdtr register.

Therefore, the article "the OS develop from boot begin" in my BLOG uses the above 3 methods to enter the main function;

The Linux kernel enters the main function through the above 2 methods.

Related Article

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.