Linux Kernel Source Analysis--memory management (first, paging mechanism) __linux

Source: Internet
Author: User
Tags readable

Linux system is divided into several major modules: process scheduling, memory management, process communications, file systems, network modules; Each module has a certain connection, like a spider web, so this is why the Linux kernel is so difficult to understand, because do not know where to start to learn. Many people will follow the system to start bios-->bootsect-->setup-->head-->main--> To learn, but in the end you'll find that when you look at main, you have to know about the other modules, or you don't know why you're doing it (maybe you can read the C code and the assembly, but you really don't know what it means). Note: The following blog refers to the operating system is the choice of 0.11 version of the Linux system;


Preface

Fortunately, I started with memory management, and memory management and other modules are relatively small (not read other modules, but the feeling and other modules are not very large), only page break and process that two modules have some relationship. OK, now start to introduce the Memory management module (in fact, it is also to comb my knowledge points).

The first place is in the head assembly, and if you look at the three compilations in the boot, remember (those three compilations are more important). The first is the paging mechanism, in the CR0 of the 31st (PG bit) set 1 to open the paging mechanism, incidentally also introduced under several other control registers: CR1 reserved, useless; CR2 used to record page anomalies when the linear address (do not understand, the following will be introduced); CR3 The address of the page catalog table used by the current CPU (there is more than just one page catalog table in this visible system. But at some point the valid Page directory table has only one); Of course, the premise of the page operation is that the 31st bit of CR0 must be opened, that is, it must be in the paging mechanism when the several control registers are valid.

The most basic of the paging mechanism is to divide the memory space into multiple pages in 4KB units.


Total system memory distribution

All memory distributions in Linux systems are:


In the Setup assembly, the system kernel code has been moved from the 0x100000 to the 1MB address starting at 0, and the memory in the main function is set to no more than 16MB, so take 16MB memory as an example; the entire memory distribution is: Kernel code and system data use 0~1MB- ----Cache uses 1~4MB------virtual memory 4mb~xxx (if there is virtual memory)------The main memory area XXX~16MB, the specific settings in the main function, you can view it yourself. If it is greater than 16MB memory, it will limit the use of only low 16MB address memory, more than 16MB of memory will be discarded, if you want to use greater than 16MB, then in the main function and the head assembly to be modified (the specific modifications will be done on their own)


Introduction to the mechanism of paging

Page Catalog table: consists of 1024 directory entries, each of which consists of 4 bytes, the contents of which are the first 20 bits of the page table structure (because the page table structure is 2^12, so that the lower 12 bits can be ignored) and the properties of the page table;

Page table: Page table and page catalog table and similar, are made up of 1024 table entries, each of which consists of 4 bytes, and the contents of the page table entries are the first 20 bits of the starting address of the physical page (because the physical page size is 2^12, so that the lower 12 bits can be ignored) and the attributes of the physical page are composed;

Physical pages: Generally in the main memory area in the 4KB multiples as the starting address, the size of 4kb of contiguous memory address (here assumes no virtual memory);

Table entries: Table items are divided into page catalog table items and page table entries, with the same formatting. The first 20-bit page box addresses, and the last 12 bits represent the properties for the page; The table entry structure is as follows:

In the case of a page catalog entry: The first 20 digits in the page box address represent the first 20 digits in the physical starting address of the page table (there are several highlights here: 1, the Physical address of the page table, rather than the linear address, the two address after the analysis of the relationship, 2, is the starting address, because a page table is 4KB size, So a page table has 4KB addresses (one byte for an address), the starting address represents an address with an offset of 0, 3, and the first 20 digits, because pages in the paging mechanism (whether page directories or page tables or physical pages) are starting addresses in multiples of 4KB. That is, the page's starting address of the low 12-bit all 0,2^12 = 4KB);

If the page table entry: The first 20 digits in the page box address represent the first 20 digits of the physical page's starting address;

The lower 12 bits are used to indicate some attributes of the corresponding page: p = = whether there exists (1 exists; 0 does not exist = = page break); r/w = = whether readable or writable (default is readable, 1 for pages to write); u/s = = Whether it is Superuser (this is not yet how to use, 1 means superuser); Whether to access, D = = Whether to modify (these two bits are generally handled by the hardware);

The following is a macro diagram of the page Catalog table, page table, physical page:



The nature of paging mechanismBefore analyzing the nature of the paging mechanism, we should first understand several address concepts: logical address, linear address, virtual address, physical address; First, define an address: 0xb8000 (This is a special address, remember.) This is a graphics card map of the physical address, you need to display things on the screen from this address to write the desired content and font properties; now suppose (in real mode) ds = 0xb800 Ax = 0x00 = = ds*16 + Ax = = 0xb8000 + 0 = 0xb8000 Logical Address: Is the offset address, for the above, the address in AX (either in protected mode or in real mode); Linear address: The 32-bit address that is formed by the segment address plus the offset (also the logical address); If the paging mechanism is not turned on, then the linear address and the physical address To open the paging mechanism, then the linear address is a page directory entry number (high 10-bit address) + Page table entry number (address of 12~21 total 10 digits) + physical page offset (address of the lower 12 bits) together; Virtual address: This has not been carefully studied, each process has a 4G memory address, but this is not The real address, but has the system fictitious, therefore the virtual address designation is uses the virtual address in the process; Physical Address: This is the most fundamental address, the address on the hardware, the address used on the CPU address bus select、read; The essence of the paging mechanism is to translate the linear address into physical address: Face View address conversion diagram
        According to the conversion diagram, step-by-step explanation of the conversion: assume that the linear address is  0x00c0 F0EF (I admit that this address is designed in advance, but only for the convenience of calculation, does not affect the conversion work)   & nbsp     1, find the physical starting address of the page directory entry: Known linear address eax = 0x00c0 F0ef, then how to get the page directory entries.    eax >> 22 (Move the linear address 22-bit to the right) to get the page catalog entry number (be sure to remember that this is the page directory entry number to be separated from the page catalog entry starting address; Because there are only the first 20 addresses on the linear address and the page directory, So the maximum number of page entries in a linear address is  2^10=1024, and one page has 4KB (4096byte), and each page catalog item occupies 4 bytes, so 4KB/4 = 1KB (1024), just right. The General page catalog entry number starts with 0, then 1, 2, 3, 4 ...) the page catalog entry number is: 0x003, the physical starting address of the page directory entry is: 0x003 x 4 (4 bytes per item) = 0x00c; Then, based on the base address of the page directory table in CR3, You can find the physical starting address of the page directory entry, assuming that there is only one page catalog table, CR0 = 0x000, and the starting physical address of the page catalog entry is: 0X00C0 0000;          2, based on the page catalog entries found, Analyze page table physical Base address: From the previous step to the page catalog entry, according to the directory item structure can know, only the top 20 is the page box address, the latter 12 is the corresponding page property settings, page directory entry & 0xFFFF F000 (in fact, is to get the first 20-bit page box address) can get the page table physical base site;         3, getting the physical starting address of the page table entry: As in the first step, get to the page table entry number, through the linear address & 0x 3F F000 (in fact, to extract the middle and page table about the 10-bit address), will get 0x0000 F000 the page table entry number (also separate from the physical starting address of the page table), the physical starting address of the page table entry is: 0x0000 F000 * 4;         4, according to the page table entry, the physical starting address of the analysis physical page: The steps are the same as 2, Gets the first 20-bit page box address, which is the physical starting address of the physical page;         5, the last step of the physical page to the starting address plus linear address of the last 12 bits in the page offset value, you can accurately locate each byte;

Reprint please indicate the author and original source, original address: http://blog.csdn.net/yuzhihui_no1/article/details/43021405

If there is no correct place, I hope everyone corrected and study together. Thank you...

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.