Memorymanagement of linux-0.11-general Linux technology-Linux programming and kernel information, which is described below. Today's whim, research the source code of Linux-0.11. Let's take a look at the memoy Management Section. The main content of this part is in memory. c.
Linux uses virtual memory management. Each page has 4 k. It uses the second-level page table structure. The first-level page table has a physical address space starting from 0. There are a total of 1024 table items, each of which has 4 bytes. That is to say, the first-level page table occupies one page. The structure of the second-level page table is the same as that of the first-level page table. The page table directory (that is, the first-level page table) Table items and the page table (this refers to the second-level page table) Table items each have 4 bytes, that is, 32-bit, their first 20 (31-12) is the base address of the corresponding page table or physical page, and the last 12 digits are other information, such as whether the current page is in medium memory. In Linux, each virtual address (32-bit) can be considered as composed of three parts: page table Directory Index (31-22), page table index (21-12 ), the offset of the physical page (11-0 ).
For management of all physical pages, a char array mem_map is used to record the number of times each physical page is referenced currently. Of course, if the physical page is free, the corresponding value in the mem_map is 0. if the value of mem_map is greater than 1, it indicates that the physical page is shared among multiple virtual pages (it can be a virtual page of different processes to achieve sharing between processes ). In memory processing, several frequently used operations are to find the page table directory and page table based on the virtual address. The formula used is:
Dir = (unsigned long *) (addr> 20) & 0 xffc;
Page_table = (unsigned long *) (0xfffff000 & * dir );
Physic_addr = (unsigned long *) (0xfffff000 & * page_table );
To understand the meanings of these formulas, you must first understand the structure of the virtual address in linux and the structure of the page table directory table items and page table items. Now let's analyze the meanings of the above three formulas:
1. dir = (addr> 20) & 0 xffc; equivalent to dir = (addr> 22) <2) & 0 xffc. According to the address structure, addr> 22 obtains the index of the page table directory, but each page table directory occupies four addresses and the start position of the page table directory in the memory is 0, so (addr> 22) <2 is the physical address of the page table directory table item corresponding to this address in the memory. Here, & 0xffc is used to ensure that the number of digits of the obtained address is 12 bits (10 bits are page table directory indexes, and each page table directory table item occupies 4 bytes, all the other two are equivalent to the indexes in the four bytes .).
2. page_table = (0xfffff000 & * dir); * dir get the table items in the page table directory. 0xfffff000 & * dir: obtain the first 20 digits in the table. The result is the address in the physical memory where the page table is located. We can use page_table ++ or page_table -- to move in the page table to obtain the physical addresses of different virtual pages.
3. physic_addr (0xfffff000 & * page_table); similar to the preceding formula, we only get the address of the physical page corresponding to the virtual page.
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