Tag:linux kernel memory management
/* *author:davidlin *date:2014-11-22pm *email: [email protected] or [email protected] *wo Rld:the City of SZ, in China *ver:000.000.001 *history:editor time do * 1) Linpeng 2014-11-22 created this file! * 2) */<pre Name= "code" class= "plain" ><pre name= "code" class= "plain" >/* * This function puts a PA GE in memory at the wanted address. * It returns the physical address of the page gotten, 0 if * out of memory (either when trying to access page-table or * p Age.) */unsigned long put_page (unsigned long page,unsigned long address) {unsigned long tmp, *page_table;/* NOTE!!! This uses the fact that _pg_dir=0 * *//Page Table of contents starting from 0 address is a prerequisite if (Page < Low_mem | | page >= high_memory) printk ("Trying To put page%p at%p\n ", page,address); The physical address is the primary memory address? Otherwise warn if (mem_map[(PAGE-LOW_MEM) >>12]! = 1) printk ("Mem_map disagrees with%p at%p\n", page,address); Check the primary memory management array for the corresponding physical page index value//If it is not the physical address that has been applied for 1//, issue a warning page_table = (unsigned long *) ((address>> & 0XFFC); Gets the page directory entry//here page_table represents the page Catalog item//page table is saved in the page Catalog item//One page Catalog item Management one page table//One page table has 1024 page table item if (*page_t ABLE) &1)//Check if the page table is valid bit p in memory Page_table = (unsigned long *) (0xfffff000 & *page_table); Page Catalog item saved page table valid//Get page Table//Save page Table value to page directory entry, so 31-21 bit unclear 0//Here page_table represents page table else { Page Catalog item saved page table is invalid if (! ( Tmp=get_free_page ()))//Apply a free physical page to save the page table return 0; If it fails, return 0*page_table = tmp|7; Save page Table address to page directory entry Page_table = (unsigned long *) tmp; The page table pointer points to the physical page of the new request//here page_table only for Page table entries offset address calculation in the page table}//one page Table manage 1024 page table entries A page table entry corresponds to a 4K physical address page_table[(address>>) & 0x3ff] = page | 7; (addree>>12) & 0X3FF can get the offset address of the physical page (page table entry) in the page table//Because with 0X3FF, that is, 011,1111,1111b can clear the value of the page catalog entry saved in address//pa GE | 7 indicates user level, read/write, page lowest bit p valid/* No need for invalidate *//Because Just Update page table, no actual physical content update, no need to update cache return page; Returns the physical page address}//used to map the linear address to the actual physical page void Get_empty_page (unsigned long address) {unsigned long tmp;if (!) ( Tmp=get_free_page ()) | | !put_page (tmp,address)) {//If there is no free physical page, die//if the physical page of the request cannot be mounted to a linear address, diefree_page (TMP);/* 0 is ok-ignored */ Release the physical page Oom (); Memory overflow, error, panic}}
Linux-0.11 Kernel Source Analysis series: Memory Management Get_empty_page () and put_page () function analysis