Memory management of the Linux kernel (4)--Missing pages handler

Source: Internet
Author: User

This article mainly explains the missing pages processing program, the comment is enough detailed, no longer explain.

The following function maps a page memory page to a specified linear address, which returns the physical address of the page//Maps a physical memory page to a linear address space designation, or maps a page at the address of a linear address space to the main memory area page. The main work is to set the information for the specified page in the related also catalog items and page table entries. This function is called in the handling of the do_no_page of the pages exception function. Parameters: address--linear address; page--is the allocation of a page pointer in the main memory area static unsigned long put_page (unsigned long page,unsigned long address) { unsigned long tmp,*page_table;//first determines the validity of a given page. If the page is in Low_mem or exceeds the system's high-end memory high_memory, a warning is issued. See if the page is already assigned. If no warning is given, determine whether the corresponding byte in the memory page map byte graph mem_map[] has been placed. if (Page<low_mem | | page>=high_memory) PRINTK ("Trying to put page @%p at%p", page,address); if (mem_map[(page-low_ MEM) >>12]!=1) printk ("Mem_map disagrees with%p at%p\n", page,address)//calculates its corresponding directory entry pointer in the page catalog table, based on the linear address addresses specified by the parameter, And get a Level two page table address from it. If the catalog entry is valid (P=1), that is, the specified page table is in memory, the specified page table address is placed in the page_table variable, otherwise an idle page is requested to be used by the page table, the corresponding flag is set in the corresponding directory entry, and the page table address is placed in the page_table variable Page_ table= (unsigned long *) ((address>>20) & 0XFFC) if ((*page_table) & 1) page_table= (unsigned long *) ( 0xfffff000 & *page_table); Else{if (!) ( Tmp=get_free_page ()) return 0;*page_table=tmp|7;page_table= (unsigned long *) tmp;} Page_table[(address>>12) &0x3ff]=page|7;return page;} Performs a page fault handling, a function that is called during an exception interrupt processing. Called in the PAGE.S program. The function parameters Error_code and address are automatically generated by the CPU due to a page fault when the process accesses the pages. Error_code indicates the type of error, and address is a linear location that generates a missing pages. The function first checks if the missing pages are in the switching device, or if they are swapped in. Otherwise, try to share the page with the same file that was already loaded, or simply map a page of physical memory pages just because the process dynamically requests the memory page. If the sharing operation is unsuccessful, then only the missing data page can be read from the corresponding file to the specified linear address void do_no_page (unsigned long error_code,unsigned long address) {int nr[4]; unsigned long tmp;unsigned long page;int block,i;struct m_inode *inode;if (address<task_size) printk ("\n\rbad! KERNEL page missing\n\r "); if (address-current->start_code>task_size) {PRINTK (" bad things happen:nonexistent Page error in no_page\n\r ");d o_exit (SIGSEGV);} Then the corresponding two-level page table item pointer is calculated based on the specified linear address, and the page table item content determines whether the pages in the address are in the switching device. If you are in the page and exit. The method is to first take the contents of the directory entry corresponding to the address of the specified linear addresses. If the corresponding Level two page table exists, then the address of the two-level page table in the directory entry is fetched, and the page table entry offset is the page pointer corresponding to the linear address, thus obtaining the contents of the page table entry. If the page table item content is not 0 and the page table entry exists as p=0, then the physical page specified by the page table entry should be in the swap device. The page=* (unsigned long *) ((address>>20) & 0XFFC) is exited after the specified page is transferred from the switching device, if (page & 1) {page &=0xfffff000;page + = (address>>10) & 0xffctmp=* (unsigned long *) page;if (tmp &&!) ( 1 & tmp) {swap_in (unsigned long*) page); return;}} Otherwise, the address of the address is specified in the linear space, and the offset length value of the specified linear address in process space relative to the process base address, TMP, which corresponds to the logical addresses, is calculated. This makes it possible to figure out the specific starting block number of the page that pages are performing in the file image or in the library file. Since the 1th piece of data in the executable image stored on the device is the program header structure, the first piece of data needs to be skipped when reading the file. Therefore, you need to first calculate the number of the data block where the pages are. Because each piece of data is block_size=1kb in length, a page of memory can hold 4 blocks of data. The process logical address TMP is divided by the chunk size plus 1 to get the missing page in the execution image file in the start block number. Address &=0xfffff000;tmp=address-current->start_code;if (Tmp>=library_offset) {inode=current-> library;block=1+ (Tmp-library_offset);} else if (tmp<current->end_data) {inode=current->executable;block=1+tmp/block_size;} else{inode=null;block=0}//is a dynamically requested data memory page if (!node) {get_empty_page (address); return;} Try sharing the physical page at TMP if (Share_page (inode,tmp)) return;if (!) ( Page=get_free_page ()) oom (); for (i=0;i<4;block++,i++) Nr[i]=bmap (Inode,block); Bread_page (Page,inode->i_dev , nr);.. if (Put_page (page,address)) return;free_page (page); Oom (); </span>

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.