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

Source: Internet
Author: User

This article mainly explains the missing pages processing program, the gaze is specific enough, no longer explained.

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 folder items and page table entries. This function is called in the handling of the do_no_page of the pages exception function. Number of references: address--linear address; page--is a page pointer in the main memory area of the allocation 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 folder item pointer in the page folder table based on the linear address addresses specified by the number of parameters And get a Level two page table address from it. Assuming that the folder 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 a spare page is used for the page table, the corresponding flag is set in the corresponding folder item, 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;} The function that is called during the page exception handling is run on the pages. Called in the PAGE.S program. Function reference Error_code and address is the process when the page is visited by the CPU due to the error caused by the fault and self-generated. 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 loaded, or simply because the process dynamically requests the memory page to map just one page of physical memory pages. If the sharing operation is unsuccessful, only the missing data page can be read from the corresponding file to the specified linear address at 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 in the address is inferred from the content of the page table. If you are in the page and exit. The method is to first take the corresponding folder item content for the specified linear address. Assuming that the corresponding Level two page table exists, the address of the two-level page table in the folder item is taken out, plus the page table entry offset is the corresponding page pointer to the linear address, which gets the contents of the page table entry. If the page table entry 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>&GT;10) & 0xffctmp=* (unsigned long *) page;if (tmp &&!) ( 1 & tmp) {swap_in (unsigned long*) page); return;}} Otherwise, the address of the page is specified in the linear space and the offset length value of the specified linear address in the process space relative to the process base address is calculated, which is the corresponding logical addressing. This allows you to figure out the detailed starting block number of the page in the run file image or in the library file. Since the 1th piece of data that is stored on the device as a running file image is the program header structure, the first piece of data needs to be skipped when reading the file. Therefore, it is necessary to calculate the block number of the missing pages first. 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 divided by the size of the data block plus 1 can be missing the page in the run 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.