Mit OS Lab 2. Memory Management

Source: Internet
Author: User

Part 1:physical Page Management
1  for inch The order given). Boot_alloc () Mem_init ()(only up to the call to Check_page_free_list (1)) Page_Init () Page_alloc () Page_free () check_page_free_list () and Check_page_alloc () test your physical page allocator. You should boot JOS and see whether Check_page_alloc () reports success. Fix your code so it passes. You could find it helpful to add your own assert () s to verify that your assumptions is correct.

In Lab1, the memory layout is as follows:

Kernel is the 0xf0100000-end part, the remaining 4 K size is the page catalog table:

Needs to be filled by the function Boot_alloc.

This part of the address is a linear address, that is, line_addr:0xf0100000 ==> phy_addr:0x00100000

paddr: Linear address to Physical address, kva-kernbase

kaddr: Physical Address to linear address, pva+kernbase

page2pa: Page table entry to physical address, global variables pages represents the starting address of a page table entry, Pp-pages represents the K page, then (pp-pages) <<page_ Shift is the physical address of the page table entry pp.

1) Boot_alloc

1 Static void*2 Boot_alloc (uint32_t N)3 {4     Static Char*nextfree;//virtual address of next byte of free memory5     Char*result;6 7     //Initialize Nextfree If This is the first time.8     //' End ' is a magic symbol automatically generated by the linker,9     //which points to the end of the kernel ' s BSS segment:Ten     //The first virtual address, the linker did *not* assign One     //to any kernel code or global variables. A     if(!Nextfree) { -         extern Charend[]; -Nextfree = (Char*) ROUNDUP ((Char*) end, pgsize); the     } -  -     //Allocate a chunk large enough to hold ' n ' bytes and then update -     //Nextfree. Make sure Nextfree is kept aligned +     //To a multiple of pgsize. -     // +     //LAB 2:your code here.cprintf ("Boot_alloc Memory at Line_addr" [%08x,%08x +%08x]\n ", Nextfree, Nextfree, ROUNDUP (n, Pgsize)); 23 result = nextfree;24 Nextfree + = ROUNDUP (n, pgsize); -  -     returnresult; -}

Here's a tip, the local static variable nextfree default is 0 when uninitialized, 12-15 rows are executed for the first time, but not again.

2) Perfect Mem_init

Here you need to assign the page table entries, the memory layout is as follows:

In Mem_init, the Kern_pgdir = (pde_t *) Boot_alloc (pgsize) has been assigned, the page catalog table is allocated, and the allocation page table entries are fully pagesinfo:

1     //////////////////////////////////////////////////////////////////////2     //Allocate An array of npages ' struct PageInfo's and store it in ' pages '.3     //The kernel uses this array to keep track of physical pages:for4     //Each physical page, there are a corresponding struct PageInfo in this5     //Array.  ' Npages ' is the number of physical pages in memory. Use Memset6     //To initialize all fields of each struct PageInfo to 0.7     //Your code goes here:8Pages = (structpageinfo*) Boot_alloc (Npages *sizeof(structPageInfo));9memset (pages,0, Npages *sizeof(structPageInfo));Tencprintf ("npages:%d, npages_basemem:%d, pages_addr:%08x\n", Npages, Npages_basemem, pages);

3) Page_Init

With global variable page_free_list, all page table entries are mapped pageinfo and 4K size page one by one.

memory allocation [Page0][pgsize, Npages_basemem * pgsize) [Iophysmem, Extphysmem] [Extphysmem, ...]

PAGE0 is reserved for bios and IDT, PAGE1-NPAGES_BASEMEM can be allocated, iomem to Extmem for Io, then Extphysmem,

The starting part of Extphysmem to Nextfree is used as kernel, page directory, page table entry, etc., and should be assigned from Nextfree.

1 void2Page_Init (void)3 {4     //The example code here marks all physical pages as free.5     //However this isn't truly the case . What's memory is free?6     //1) Mark physical page 0 as in use.7     //This is the preserve the Real-mode IDT and BIOS structures8     //In case we ever need them. (Currently we don ' t, but ...)9     //2) The rest of the base memory, [Pgsize, Npages_basemem * pgsize)Ten     //is free . One     //3) Then comes the IO hole [Iophysmem, Extphysmem), which must A     //never be allocated. -     //4) then extended memory [Extphysmem, ...). -     //Some of it is on use, Some are free. Where is the kernel the     //In physical memory? Which pages is already in use for -     //page tables and other data structures? -     // -     //Change the code to reflect. +     //Nb:do not actually touch the physical memory corresponding to -     //Free pages! + size_t i; Apages[0].pp_ref =1; atpages[0].pp_link =NULL; -  -uint32_t nextfree = (uint32_t) boot_alloc (0); -cprintf ("npages:%d npages_base_mem:%d\n", Npages, npages_basemem); -cprintf ("nextfree:%08x iophy:%08x EXT:%08x\n", Nextfree-kernbase, Iophysmem, extphysmem); -      for(i =1; i < npages; i++)  in     { -         if((I >= (iophysmem/pgsize)) && (I < ((nextfree-kernbase)/pgsize)))   to         { +Pages[i].pp_ref =1; -Pages[i].pp_link =NULL; the         } *         Else  $         {Panax NotoginsengPages[i].pp_ref =0; -Pages[i].pp_link =page_free_list; thePage_free_list = &Pages[i]; +         } A     } the}

4) Page_alloc

The implementation of the Page_alloc function. is to release one of the free pages in the current list, and then update page_free_list so that TA points to the next free page

If the flag is passed into the Alloc_zero, it is zeroed with memset.

1 structPageInfo *2Page_alloc (intalloc_flags)3 {4     //Fill This function in5     structpageinfo* Pginfo =NULL;6     if(!page_free_list)7     {8         returnNULL;9     }Ten  OnePginfo =page_free_list; APage_free_list = pginfo->Pp_link; -     if(Alloc_flags &Alloc_zero) -     { thememset (Page2kva (Pginfo),0, pgsize); -     } -  -     returnPginfo; +}

5) Page_free

The corresponding Page_free is to add the PP description page to the free list, making pp the newest page_free_list.

1 void2Page_free (structPageInfo *pp)3 {4     //Fill This function in5     //Hint:you want to panic if pp->pp_ref is nonzero or6     //Pp->pp_link is not NULL.7 8ASSERT (Pp->pp_ref = =0|| Pp->pp_link = =NULL);9 TenPp->pp_link =page_free_list; OnePage_free_list =pp; A}

Part 2:virtual Memory

Under Linux, each process has its own independent address space, 32bit system next 4GB. Therefore, the length of each address is four bytes , which is exactly the size of a pointer . After understanding the paging mechanism of Linux, you can see that a Virtual address is actually made up of the following 3 parts:

 //  a linear address ' la ' has A three-part Structure as follows:  // //  +--------------+--------------+-------------------+  //  |   Page Directory | Page Table | Offset within Page |  //       |      Index |                     Index | | //  +----------------+----------------+---------------------+  //  \---PDX (LA)--/\---PTX (LA)--/\----Pgoff (LA)----/ //  \----------Pgnum (LA)----------/ 

The page directory is actually a 1024-length shaped array, each of which is a pointer to each page table. Each page table is also an array of 1024-length shapes, and the elements inside are the values of the physical addresses .

However, the high 10 bits of a virtual address are the page directory index for that address, which is used to get the address of the page table in the page directory that points to that address.

Through the 10~20 bit, you can get the address in the Page Table entry index, and then you can get the address corresponding to the physical address , finally, the virtual address of the lower 12 bits plus the physical address of the base site. The conversion from the virtual address to the physical address is completed.

Mit OS Lab 2. Memory Management

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.