0.11 kernel provided memory management functions most of them are in memery.c, and I divide the functions into three categories: memory allocation and release, page exception handling, and memory initialization. The first category mainly includes the operation of a single physical page and the operation of a page table, and the operation of a single physical page is the application, mapping, and release of a physical page; Page table operations are primarily the release and replication of multiple page tables. The second type is mainly dealing with the interrupt processing function of the fault page anomaly and write protection exception. The third category is a function that is responsible for the initialization of the memory.
I. Allocation and release of memory
1, Get_free_page (): Request a free physical page in the main memory area.
First scan mem_map[], looking forward from the end of the value of 0 elements, find its value into 1, and according to the index of this element in mem_map[] is worth to the address of the physical page, and then the physical page of the 4K byte clear Zero, and finally return the physical address.
2, Free_page (): Releases the physical page at the specified physical address.
After verifying the validity of this address, it translates the corresponding page address according to it: (addr-1m)/4k, and then uses this address value as the index to find the corresponding item in mem_map[], clearing it.
3, Free_page_tables (): Releases a number of page tables starting at the specified linear address and its corresponding physical page.
First, determine if the incoming linear address is valid, that is, on the 4MB boundary, then, even if it takes up the number of page directory entries and the address of the first directory entry, then starting with the first directory entry, the address of the page table is based on the directory entry for the available directory entry (i.e. P=1: there is a corresponding page table). This frees up all the recorded physical pages (free_page) in the page table and zeroes the page table entry, then releases the physical page of the page table itself and zeros out its corresponding page catalog entry. Until the last page directory entry is processed. Finally, the TLB is refreshed.
4, Copy_page_tables (): Copy the specified linear address to start a number of page tables to the specified destination linear address.
This function is used when fork a process, which lets the child process share the physical memory of the parent process. After verifying the boundary of the address, the starting address of the source directory entry and the starting address of the destination directory entry and the number of page directory entries to be copied are converted into the page Catalog Entry table. Then start the copy work: first apply for a page of memory to store the new page table, it then sets the corresponding page catalog entry, then loops through the page table entries in the source page table to the new page table (only the first 160 items are copied for the kernel process, because the kernel module is no more than 640K), and the individual page table entries are set to read-only. Finally, the value of the element corresponding to the page in mem_map[] is added by 1 (representing the reference count plus 1) based on the physical address of each page. It then loops through the replication work until all the page tables have been copied.
5, Put_page (): Map a physical page to the specified linear address space.
That is, the corresponding page table entry is found according to the linear address, and the value in the page table entry is set to the physical address of the physical page. This function is typically used after get_free_page (). After verifying the validity of the address, first the address of the page directory entry is converted according to the high 10 bits of the linear address, the page table address is obtained from the page catalog entry, and the page table entry address is obtained according to the middle 10 digits of the linear address. Finally, you set the physical page address and some page information that it maps to in this page table entry.
6, Get_empty_page (): The combination of get_free_page () and Put_page ().
Two, page exception processing correlation function
1, Up_wp_page (): Cancels the write protection of the physical page corresponding to the specified page table entry.
This function is to handle the case of page write protection. It first checks whether the page is used by only one process (the corresponding element in mem_map[] is 1), if you change it directly to writable, if it is used by more than one process, apply for a page and then overwrite the page table entry: Update the address of the new physical page, set to read-write, and then subtract the reference number of the original physical page by 1. Refreshes the TLB after copying the contents of the original physical page to the new physical page.
2, Do_wp_page (): Interrupt handler function: Handle write protection exception
Gets the page table entry address based on the linear address and calls it Up_wp_page () as a parameter.
3, Write_verify (): Check whether the page can be written, if not copied page.
According to the given linear address to get page table entries, according to the page table entries to determine whether the corresponding physical page is writable, if not writable, then call Un_wp_page (), complete the new page copy.
4. Try_to_share (): Lets the specified logical address of the current process share the physical page corresponding to the logical address of another process.
When a process corresponds to the same executable program as another process, you can have one process share the physical memory of another process. First, the page directory entries for the current process and target process are obtained based on the logical address. The page table entries are then obtained according to the page catalog entry of the target process, and then the page table entry is determined and the physical page corresponding to the page table entry is modified, and if it has not been modified, it can be shared. Next you get the page table entry for the current process and set the contents of this page table to the contents of the Destination Process page table entry that you just obtained, and finally make the two page table entries read-only.
5, Share_page (): Shared page
Locate the process that corresponds to the same executable program as the current process, and then call Try_to_share () to share the page. The way to find this process is to get the I node of the corresponding executable program for the current process, and then scan the entire task array to find the same process as the current process I node.
6, Do_no_page (): Interrupt processing Function: page fault processing
There are two kinds of missing pages, one is the dynamic application of memory caused by the page, a access to the process code or data when the page is missing. According to the linear address and the beginning of the current process to obtain a logical address, to determine whether the logical address is outside the code segment and the range of data, if the description is outside the scope of the heap, this is the dynamic application memory caused by the page fault, this time, with Get_empty_page () on can solve the problem. If within this range, the description is a rhetorical question of the code or data caused by the page, the whole time, you need to copy from the disk's executable program pages into memory: first to apply for a piece of physical memory page, and then according to the logical address of the corresponding logical block in the executable file (1KB per block size) number, The four consecutive logical blocks are then copied to the new application's physical page, and the mapping of the physical page to the linear address is completed after processing some useless information (less than 4KB of the actual useful information in the 4 logical blocks that might be read).
Iii. Initialization of Memory: Mem_init ()
This function is called when the main initialization, previously said that the Linux memory management is mainly through the mem_map[] completed, the memory of the first time, the main is to mem_map[] initialization: Is the 1M of memory space in the main memory part of the mem_map[ ] The corresponding element is set to 0, and the other parts occupy the state.
Attached: 0.11 kernel memory Management There is also a file, PAGE.S, this file is mainly caused by the interruption of page exception interrupt processing function, specifically, according to the type of exception to invoke different processing functions: Do_no_page (), Do_wp_page ().
So far, Linux0.11 kernel of memory management learning, I intend to put an ending, the following I plan to learn Linxu management, mainly set up a matching drive.