Linux kernel Source Learning kernel page table printing

Source: Internet
Author: User

This semester the Linux kernel experiment is the final Print kernel page table, linear address----physical Address

When I saw this experiment, I thought of this init function (because that's what I was talking about at the time), which is the ^_^ of the Linux kernel page table, which is the mapping of 896M linear addresses above 3G in 32-bit systems to the 0-896m of physical addresses. Other situations, such as fixed mappings, are also handled in the caller Paging_init function. That belongs to the high-end memory map that piece of content, and now we first look at how the kernel page table 3g~3g+896m the linear address corresponding to the physical address of the print out.

The source code is linux2.6.11 version, it should be the corresponding version of the white Paper

static void __initkernel_physical_mapping_init (pgd_t *pgd_base)

{

Unsignedlong PFN;

PGD_T*PGD;

PMD_T*PMD;

Pte_t*pte;

Intpgd_idx, Pmd_idx, Pte_ofs;

/* Because the kernel's linear address space starts with 0xc0000000, we only need to initialize the key for the kernel Global page directory starting with 0x300 */

pgd_idx= Pgd_index (Page_offset); /*768*/

pgd= pgd_base + pgd_idx; /*PGD points to the current directory entry */

pfn= 0; /* Physical page frame number that needs to be mapped, starting from physical address 0 */

/* Initializes a global catalog entry for each page starting at 768, PTRS_PER_PGD the total number of items 1024 */

for (; pgd_idx < PTRS_PER_PGD; pgd++, pgd_idx++) {

pmd= One_md_table_init (PGD);

if (PFN >= max_low_pfn)/*MAX_LOW_PFN represents the page box number of the last physical page box mapped directly by the kernel */

Continue

/* Initialize each page intermediate directory entry, in front of the 32-bit x86 system with physical Address extension enabled, using level three mapping,

The 32-bit system, which does not have physical Address extensions enabled, actually uses only two of them, although PMD still exists in the software architecture,

But actually it's just a decoration. Kernel by setting PTRS_PER_PMD to 1, and in One_md_table_init initializing PMD functions

The first term of the PMD is initialized directly to the PGD item itself that points to its address, and a "in situ" mapping is completed. Other words

Each page catalog entry at this time represents either a page intermediate directory descriptor, or a page table */

for (pmd_idx = 0; pmd_idx < PTRS_PER_PMD && PFN < MAX_LOW_PFN; pmd++,pmd_idx++) {

Unsignedint address = PFN * page_size + page_offset;

/*map with big pages if possible, otherwise create normal page tables. */

if (cpu_has_pse) {

Unsignedint Address2 = (PFN + ptrs_per_pte-1) * page_size + page_offset +page_size-1;

if (Is_kernel_text (address) | | | is_kernel_text (ADDRESS2))

SET_PMD (PMD,PFN_PMD (PFN, page_kernel_large_exec));

Else

SET_PMD (PMD,PFN_PMD (PFN, Page_kernel_large));

pfn+= Ptrs_per_pte;

}else {

Pte= One_page_table_init (PMD);

/* Finally initialize each page table entry, which is the descriptor for each physical page box. Note that the pfn++ indicates that the page box number is added 1 in turn,

and its initial value is 0, that is, the physical address from the 0-based page box, directly mapped to the kernel linear address 0xc0000000 in the beginning of the space

, the mapping is similar to the Temporary Kernel page table, except that the scope is larger.

for (pte_ofs = 0;

Pte_ofs < ptrs_per_pte && pfn< MAX_LOW_PFN;

pte++, pfn++, pte_ofs++, address +=page_size) {

if (Is_kernel_text (address))

Set_pte (Pte,pfn_pte (PFN, page_kernel_exec));

Else

Set_pte (Pte,pfn_pte (PFN, Page_kernel));

}

}

}

}

}

Note: The above note is for the PAE mode is not turned on, if the PAE is turned on, is 4--------the----that is, the above 768 becomes 3,PTRS_PER_PGD to PTRS_PER_PMD to 1,PTRS_PER_PTE for 512

After reading this function, let's think about how to print the kernel page table, my system is the default to open PAE, that is, in memory is 2M pages and 4KB pages coexist, page directory and page table organization is:

CR3--ÀPDPT--ÀPMD [--àpte]-àpage in the middle of the PTEs to add square brackets, indicating that this level is not necessarily, that is, if the corresponding 2M page, then this PMD is stored in the 2M page physical address and flag bit, the page size is 2M If there is this level, then the corresponding page is 4KB. This is well understood in terms of quantity, PMD a table item corresponds to a 2M linear address space, if the size of the page is 2M then PMD is equivalent to a page table, if the size of the page is 4KB, then there are 512 items, just need to add a level of PTEs (512) to represent, This is the structure of the above function corresponding to the PMD to determine whether there are 2M pages.

So the print function can be written like this:

for (; Pgd_idx < ptrs_per_pgd;pgd_idx++) {

Unsignedlong pgd_cur = Pgd_idx * pgdir_size;

Pmd= Pmd_offset ((pud_t *) (pgd_base + pgd_idx), pgd_cur);

for (pmd_idx = 0; pmd_idx < 448/*ptrs_per_pmd*/; pmd_idx++) {

Unsignedlong pmd_cur = pgd_cur + pmd_idx * pmd_size;

if (Pmd_present (Pmd[pmd_idx])) {

Pte= Pte_offset_kernel ((pmd_t *) (PMD + pmd_idx), pmd_cur);

Pte1= Pmd_val (* (pmd_t *) (PMD + pmd_idx));

if ((((Unsignedlong) pte1) >>7) &0x1)

{

Print 2M page

}

Else

{

for (pte_ofs = 0; pte_ofs < ptrs_per_pte; pte_ofs++) {

Unsignedlong pg_cur = pmd_cur + pte_ofs * page_size;

if (Pte_present (Pte[pte_ofs]))

{

Print 4KB page

}

}

}

}

}

}

Because there is a lot of print content, it is convenient to use SEQ file to implement.

Linux kernel Source Learning kernel page table printing

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.