Linux Kernel code initialization temporary page table of the kernel

Source: Internet
Author: User

The long and dark Prehistoric Age finally came to setup. In the setup Assembly function, Linux completes the transformation from prehistoric civilization to modern civilization by setting the PE bit of the Cr0 register (switching from the real mode to the protection mode. During the setup period, Linux has learned that the available memory resources in the world are far more than 1 MB. At this time, Linux's desire to expand, the first step to satisfy its desire, it began to seize the first 8 MB of memory resources.

Initializing the temporary kernel page table is completed in the startup_32 assembly language function. In the ulk description, assuming that the inner nuclear energy can accommodate the first 8 MB of Ram space, and then perform constant ing on the first 8 MB of RAM (for example, the user address 0x00003000 maps the physical address 0x00003000, 0xc0003000 maps to the physical address 0x00003000) to initialize the global directory swapper_pg_dir of the temporary page and the corresponding page table. To map 8 Mb, you only need to fill in 0th items, 1 items, 768 Items and 769 Items in swapper_pg_dir. The first two items are linear address ing for the user, and the last two items are linear address ing for the kernel. The reason why two items in the global page directory can be mapped to 8 Mb is 2 × 1024 (1024 items in the page table) × 4 K (the size of one page) = 8 m. In fact, initializing the kernel page table is not a hard rule to map the first 8 MB of RAM. This depends on the configuration of your kernel (I think it is 8 Mb ing in most cases ). In startup_32, we can see that the number of memory ing is dynamically determined by pg0.

Linux/ARCH/i386/kernel/head. s

Page_pde_offset = (_ page_offset> 20 );

/* _ Page_offset is 0xc0000000, the starting address of the Kernel linear space.

Page_pde_offset = 0xc00 (decimal 3072 )*/

Movl $ (pg0-_ page_offset), % EDI

The linear address of/* pg0 can be found in the/boot/system. Map File. On my ubuntu8.04 machine, it is 0xc04f4000.

0xc0000000 is the physical address of pg0 (004f4000), which is placed in edi. */

Movl $ (swapper_pg_dir-_ PAGE_OFFSET), % edx

/* The linear address of swapper_pg_dir can also be found in the/boot/System. map File. It is 0xc047d000 on my machine.

Subtract 0xc0000000, which is the physical address of swapper_pg_dir (0047d000) and put it in edx. */

Movl $0x007, % eax/* 0x007 = PRESENT + RW + USER */

/* The low 12 bits of the page Directory items and page table items are flags, and the flags 0x007 are placed in eax. */

10:

Leal 0x007 (% edi), % ecx/* Create PVDF entry */

/* Add 0x007 to the physical address of pg0 pointed by edi in the first cycle to ecx.

In the second cycle, place the physical address 0x4f5000 pointed to by edi with 0x007 into ecx. */

Movl % ecx, (% edx)/* Store identity PVDF entry */

/* Put the content of ecx in swapper_pg_dir In the first loop.

In the second loop, put the content in ecx into the 1st items in swapper_pg_dir. */

Movl % ecx, page_pde_offset (% edx)/* Store kernel PVDF entry */

/* Put the content of ecx in swapper_pg_dir In the first loop. Because the previously calculated value of page_pde_offset is 3072, while that of swapper_pg_dir is 4 bytes, 3072/4 = 768.

In the second loop, put the content in ECx into the 769th items in swapper_pg_dir. */

Addl $4, % edX

/* In the first cycle, EDX points to the 1st items of swapper_pg_dir.

In the second cycle, EDX points to the 2nd items of swapper_pg_dir. */

Movl $1024, % ECx

/* Set the count for the 1024 page table items during initialization */

11:

Stosl

/* Put the content in eax into the physical address pointed by EDI, and then EDI + 4. */

Addl $0x1000, % eax

Loop 11b

/* Jump to the above 11 cycles.

After 1024 executions for the first time, the physical address 0x007,0x1007,0x2007 is stored from the pg0 physical address (0x4f4000 ,..., 0x3ff007, that is, the physical address can be mapped from 0x000 to 0x3fffff. The value in EDI is 0x4f5000.

After the second execution, the physical address (0x4f5000) is 0x400007,0x401007,0x1024 ,..., 7ff007, that is, it can be mapped to the physical address 0x000 to 7fffff, Which is exactly 8 Mb. The value in EDI is 0x4f6000. */

Leal (init_map_beyond_end + 0x007) (% EDI), % EBP

/* The value of init_map_beyond_end is 128 K, which is defined as a macro in this file. Add the physical address pointed to by EDI plus K and 0x007 to EDP. */

CMPL % EBP, % eax

/* In the first loop, the value of EBP is 0x515007, and the value of eax is 0x400007 less than 0x515007. The maximum physical address currently mapped to is 0x3fffff, which does not contain 0x515007. Therefore, the ing is not completed.

In the second loop, the value of EBP is 0x516007, and the value of eax is 0x800007 greater than 0x516007. The maximum physical address mapped to is 0x7fffff, which contains 0x516007. Therefore, 8 Mb physical address ing is complete. */

JB 10b

/* When the first loop is completed, jump to the top 10 to continue the loop.

The second loop jumps out of the loop. */

Movl % EDI, (init_pg_tables_end-_ page_offset)

/* Put 0x4f6000 into the physical address indicated by init_pg_tables_end. Also in/boot/system. Map. */

At this time, the appetite for Linux is getting bigger and bigger, and 8 MB of resources cannot satisfy its appetite. Its black hand began to extend slowly to memory smaller than MB.

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.