Linux-0.11 Kernel Source Analysis series: Memory Management Up_wp_page () and do_wp_page () function analysis

Source: Internet
Author: User

Tag:linux    kernel     memory management    

/* * the Up_wp_page () function is used to remove the shared state of the physical page while providing a page of a new physical page to the process where the copy occurs, and the new physical page is the same copy of the previously shared page's data. * Table_entry is a pointer to the address of the shared physical page, i.e. the actual address of the page table + the offset address in the table */void un_wp_page (unsigned long * table_entry) {unsigned long old_page,new_ Page;old_page = 0xfffff000 & *table_entry;    Get shared physical page actual address if (old_page >= low_mem && mem_map[map_nr (old_page)]==1) {*table_entry |= 2;         If the page is in the main memory area and is not a shared page, set to writable invalidate ();               Refresh page transform cache return; Exit}if directly (!) (                    New_page=get_free_page ()))//If it is a shared page, request a new physical page of Oom ();    If 0 = Get_free_page (), Memory overflow, Dieif (old_page >= low_mem)//If the shared page is in the main memory area MEM_MAP[MAP_NR (old_page)]--; Share count -1*table_entry = New_page |            7;                           mount a new page to the page table of the copy process on write invalidate ();           Refresh page Transform cache copy_page (old_page,new_page); Copy old_page content to new_page} 1/************************************************************************* 2 > File NAME:TABLE.C 3 > author:linpeng1577 4 > mail:[email protected] 5 &Gt Created Time:mon 06:05:54 PM PST 6 ************************************************************************ /7 8 #include <stdio.h> 9 static in T page_dir[1023] = {0}; One static int page_table[1023] = {0}; A static int phy_addr_table[1023] = {0}; static int new_phy_addr_table[1023] = {0};  + int main (void) + {*phy_page int      = NULL; Phy_page = phy_addr_table; PAGE_TABLE[114] = (int) phy_page; PAGE_DIR[83] = (int) (&page_table); printf ("phy_addr_table = 0x%x\n", (int) phy_addr_table); printf ("Phy_page = 0x%x\n", (int) phy_page); printf ("page_table = 0x%x\n", (int) page_table); -printf ("Page_dir = 0x%x\n", (int) page_dir);                 27               printf ("* (&page_dir[83]) = 0x%x\n", * (&page_dir[83])); printf ("* (&page_table[114]) = 0x%x\n", * (&page_table[114]));                                      * (&page_table[114]) = (int) new_phy_addr_table; printf ("new_phy_addr_table = 0x%x\n", (int) new_phy_addr_table); printf ("* (&page_table[114]) = 0x%x\n", * (&page_table[114]));                                                                                          34} ~ ~ </lin/tests/linux/test_ptr /table.c [Format=unix] [type=c] [pos=31,30][91%] 24/11/14-21:27 "table.c" 34L, 1162c[email protected]:/home/ iubuntu/lin/tests/linux/test_ptr#./table phy_addr_table = 0x804c040//Shared physical page address Phy_page = 0x804c040 page_table = 0x804b040//Page table Address Page_dir = 0x804a040//Page Directory Address * (&page_dir[83]) = 0x804b040 * (&page_table[114]) = 0x804c040//old_page = 0xfffff000 & *table_entry;new_phy_addr_table = 0x804d040/ /New Physical page address * (&page_table[114]) = 0x804d040//*table_entry = New_page | 7; 1. Assume that there is an actual physical address Phy_page = phy_addr_table;2. This address is managed by the 114th Item of page table Page_table, page_table[114] = phy_page;3. The address of this page table is represented by the page catalog table Page_ Dir's 83rd management, page_dir[83] = page_table;4. Now know the page Catalog table page_dir, page table page_table, page table by the 83rd page Catalog item management, physical page by page table 114th Item management, now to fetch physical page address Phy_ Page;5. The address of the page table page_table can be obtained by * (&page_dir[83]); 6. The physical page address phy_addr;7 can be obtained through * (&page_table[114]). in void Un_wp_ Page (unsigned long * table_entry), Table_entry plays a similar role (&page_table[114]).  /* * For copy-on-write * by assigning a new page to a process that writes a shared page, and a previous shared page share count minus 1 * * If the page belongs to a code snippet, error */* * This routine handles present pages, when users try To write * to a shared page. It is do by copying the page to a new address * and decrementing the shared-page counter for the old page. * * If it ' s in code space we exit with a segment error. */void do_wp_page (unsigned long error_code,unsigned long address) {#if 0The code that is blocked by Linus */* We cannot do the Yet:the Estdio Library writes to code space *//* stupid, stupid. I really want the LIBC.A from GNU */if (code_space) do_exit (SIGSEGV); #endifun_wp_page ((Unsigned long *) (((        ADDRESS&GT;&GT;10) & 0XFFC) + (0xfffff000 &* ((unsigned long *) ((address>>20) &AMP;0XFFC)))); /* address is linear (unsigned long *) (((address>>10) & 0XFFC) + (0xfffff000 &amp        ; * ((Unsigned long *) ((address>>20) &AMP;0XFFC))) The premise here is Pg_dir = 0; The above address can be divided into [1] ((address>>10) & 0XFFC) to calculate the offset index of the physical page in the page table here is the physical page address, each page table manages 1024 page table entries, each page Table entries account for 4 bytes each page table item can manage a physical page so here is ((address>>12) & 0x3ff) <<2 [2] (0xfffff000 & * ((unsigned long *) ((address>>20) &AMP;0XFFC)) Here is the page Table Address value page table saved in the page catalog entry 1 pages Catalog table has 1024 page catalog items 1 pages Catalog Item 4 Bytes 1 Page Catalog Item Management 1 page Table 1 page Table management 1024 Item Page table Item 1 page table Item Management 1 physical pages 1 physical pages accounted for 4K byte = 1024*4 is 1 page table item management 4K contiguous memory page directory entries (1024x768) x page table (1024x768) x 4K = 4096M = 4G ((address>>22) &0x3ff        ) <<2 This is the offset address of the page catalog entry in the page catalog table (unsigned long *) ((address>>22) &0x3ff)) <<2 is the physical address of the page table that is saved in the directory entry  So the physical page saved address is [2] page table physical Address +[1] Page table offset address so the address of the physical page is (unsigned long *) (((address>>12) & 0x3ff) <<2) + (Unsigned long *) ((address>>22) &0x3ff)) <<2 pointer refers to page table offset Address Page table physical Address} */

Linux-0.11 Kernel Source Analysis series: Memory Management Up_wp_page () and do_wp_page () function analysis

Related Article

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.