1 / 31

Virtual Memory

Virtual Memory. Case 1. In a 32-bit machine we subdivide the virtual address into 4 pieces as follows: We use a 3-level page table, such that the first 7 bits are for the first level and so on. Physical addresses are 44 bits and there are 4 protection bits per page. (Byte Alignment). Case 1.

hoshi
Download Presentation

Virtual Memory

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Virtual Memory

  2. Case 1 • In a 32-bit machine we subdivide the virtual address into 4 pieces as follows: • We use a 3-level page table, such that the first 7 bits are for the first level and so on. • Physical addresses are 44 bits and there are 4 protection bits per page. (Byte Alignment)

  3. Case 1 • What is the page size in such system? • 4K (212) • How much memory is consumed by the page table and wasted by internal fragmentation for a process that has 128K of memory starting at address 0? • #L3-entry: 28 = 256, entry size: 5byte (44-12+4bits) • #L2-entry: 24 = 16, entry size: 5byte (44-8+4bits) • #L1-entry: 28 = 256 , entry size: 6byte (44-4+4bits)

  4. Case 1 • How much memory is consumed by the page table and wasted by internal fragmentation for a process that has 128K of memory starting at address 0? • 128K = 4K * 32  1*L1 + 1*L2 + 1*L3 • L3: 256 * 5 = 1280 Bytes • L2: 16 * 5 = 80 Bytes • L1: 256 * 6 = 1536 Bytes • Memory: 1280 + 80 + 1536 = 2896 Bytes • Internal: 1*(5*8-36) + 1*(5*8-40) + 32*(6*8-44) = 132bits

  5. Case 2 Part 1 • Assume program “runarun” contains • 2 pages of code and • 2 pages of data, and • libc library contains 1 page of code and 1 page of data, • as shown in figure 1. • Assume we have a machine with • 16 pages of physical memory, and • 64 pages of virtual memory. • User space takes 48 pages • kernel space takes 16 pages.

  6. Case 2 Figure 1

  7. Case 2 • The figure 2 shows • the runtime virtual memory layout of the program • the virtual memory areas • Each shadowed rectangle represents one page. • Fill in the flags of virtual memory areas with “read-only” or “read/write”

  8. r/w r/w r r/w r/w r/w r

  9. Case 2 Part 2 • There are two types of page fault: • major and minor faults. • major page faults occur • when data has to be read from disk • which is an expensive operation; • minor page fault concerns • no disk operation, • the fault could be handled by • just memory read/write/copy operations.

  10. A sample from exam papers • During the lifetime of the program “runarun”, • every of the code and data pages are visited. • The process takes • one bss page, • one heap page • one stack page • During the lifetime of the program, how many pages faults occurred? • Number of major page faults: ________ • Number of minor page fault: ________ text data bss heap stack

  11. A sample from exam papers Part 3 • Assume the system performs no page swapping. • Before the program starts, physical memory looks like the table in Figure 2. • Blacked boxes (PPN:2,9) are already allocated. • Parent process blocks itself and waits after forking, and continue after child exit

  12. Case 2 Part 3 • Program binary and shared library are touched in the sequence of: • Parent process: <page4, page5, stack, page0, page2, bss, heap, fork, page1 > • Child Process: <stack, page1, page3, bss, page0, page2, heap> • Operations for data/bss/stack/heap are write • Don’t consider reap

  13. Case 2 • A simple first-hit memory allocation policy is used. slot_t *find_a_mem_slot() { for(i = 0; i < 16; i++) if (slot_is_free) return slot[i]; if (i == 16) return fail; }

  14. Case 2 • Please fill in the flat page tables with PPN below for both the parent process and the child process just before they finished executing. Parent: Page4 Page5 Stack Page0 Page2 Bss Heap Fork PPN:3 stack PPN:3 stack PPN:1 PPN:1 page5 page5 page4 page4 PPN:0 PPN:0 PPN:8 PPN:8 heap heap PPN:7 PPN:7 bss bss page3 page3 PPN:6 PPN:6 page2 page2 page1 page1 PPN:4 PPN:4 page0 page0

  15. Case 2 • Please fill in the flat page tables with PPN below for both the parent process and the child process just before they finished executing. Parent: Page4 Page5 Stack Page0 Page2 Bss Heap Fork Child: Stack PPN:3 stack PPN:10 stack PPN:1 PPN:1 page5 page5 page4 page4 PPN:0 PPN:0 PPN:8 PPN:8 heap heap PPN:7 PPN:7 bss bss page3 page3 PPN:6 PPN:6 page2 page2 page1 page1 PPN:4 PPN:4 page0 page0

  16. Case 2 • Please fill in the flat page tables with PPN below for both the parent process and the child process just before they finished executing. Parent: Page4 Page5 Stack Page0 Page2 Bss Heap Fork Child: Stack Page1 PPN:3 stack PPN:10 stack PPN:1 PPN:1 page5 page5 page4 page4 PPN:0 PPN:0 PPN:8 PPN:8 heap heap PPN:7 PPN:7 bss bss page3 page3 PPN:6 PPN:6 page2 page2 PPN:11 page1 page1 PPN:4 PPN:4 page0 page0

  17. Case 2 • Please fill in the flat page tables with PPN below for both the parent process and the child process just before they finished executing. Parent: Page4 Page5 Stack Page0 Page2 Bss Heap Fork Child: Stack Page1 Page3 PPN:3 stack PPN:10 stack PPN:1 PPN:1 page5 page5 page4 page4 PPN:0 PPN:0 PPN:8 PPN:8 heap heap PPN:7 PPN:7 bss bss PPN:12 page3 page3 PPN:6 PPN:6 page2 page2 PPN:11 page1 page1 PPN:4 PPN:4 page0 page0

  18. Case 2 • Please fill in the flat page tables with PPN below for both the parent process and the child process just before they finished executing. Parent: Page4 Page5 Stack Page0 Page2 Bss Heap Fork Child: Stack Page1 Page3 Bss PPN:3 stack PPN:10 stack PPN:1 PPN:1 page5 page5 page4 page4 PPN:0 PPN:0 PPN:8 PPN:8 heap heap PPN:7 PPN:13 bss bss PPN:12 page3 page3 PPN:6 PPN:6 page2 page2 PPN:11 page1 page1 PPN:4 PPN:4 page0 page0

  19. Case 2 • Please fill in the flat page tables with PPN below for both the parent process and the child process just before they finished executing. Parent: Page4 Page5 Stack Page0 Page2 Bss Heap Fork Child: Stack Page1 Page3 Bss Page0 PPN:3 stack PPN:10 stack PPN:1 PPN:1 page5 page5 page4 page4 PPN:0 PPN:0 PPN:8 PPN:8 heap heap PPN:7 PPN:13 bss bss PPN:12 page3 page3 PPN:6 PPN:6 page2 page2 PPN:11 page1 page1 PPN:4 PPN:4 page0 page0

  20. Case 2 • Please fill in the flat page tables with PPN below for both the parent process and the child process just before they finished executing. Parent: Page4 Page5 Stack Page0 Page2 Bss Heap Fork Child: Stack Page1 Page3 Bss Page0 Page2 PPN:3 stack PPN:10 stack PPN:1 PPN:1 page5 page5 page4 page4 PPN:0 PPN:0 PPN:8 PPN:8 heap heap PPN:7 PPN:13 bss bss PPN:12 page3 page3 PPN:6 PPN:14 page2 page2 PPN:11 page1 page1 PPN:4 PPN:4 page0 page0

  21. Case 2 • Please fill in the flat page tables with PPN below for both the parent process and the child process just before they finished executing. Parent: Page4 Page5 Stack Page0 Page2 Bss Heap Fork Child: Stack Page1 Page3 Bss Page0 Page2 Heap PPN:3 stack PPN:10 stack PPN:1 PPN:1 page5 page5 page4 page4 PPN:0 PPN:0 PPN:8 PPN:15 heap heap PPN:7 PPN:13 bss bss PPN:12 page3 page3 PPN:6 PPN:14 page2 page2 PPN:11 page1 page1 PPN:4 PPN:4 page0 page0

  22. Case 2 • Please fill in the flat page tables with PPN below for both the parent process and the child process just before they finished executing. Parent: Page4 Page5 Stack Page0 Page2 Bss Heap Fork Page1 Child: Stack Page1 Page3 Bss Page0 Page2 Heap PPN:3 stack PPN:10 stack PPN:1 PPN:1 page5 page5 page4 page4 PPN:0 PPN:0 PPN:8 PPN:15 heap heap PPN:7 PPN:13 bss bss PPN:12 page3 page3 PPN:6 PPN:14 page2 page2 PPN:11 PPN:11 page1 page1 PPN:4 PPN:4 page0 page0

  23. Case 3 do_page_fault() { //page fault virtual address address = get_page_fault_addr(); //the reason of the page fault type = get_page_fault_type(); //find the vm area which the address falls in vma = find_vma(address); if (!vma) return fail; if (type == WRITE_PROTECT_FAULT) do_copy_on_write(); //not detailed here

  24. Case 3 /* page not present */ if (vma->type == FILE_PAGE) { //file-backed vm area //load file page into memory and //update the page table load_file(); return success; } else if (vma->type == ANONYMOUS_PAGE) {// anonymous vm area //allocate a zero page and update the page table alloc_zero_page(); return success; } /* unknown type */ return fail; }

  25. Case 3 find_vma(address) { //get the list head of the vm area vma = get_head_vma();   while(vma) { if (address > vma->vm_end) return NULL;  if (address >= vma->vm_start && address <= vma->vm_end) return vma;   vma = vma->vm_next; }  return NULL; }

  26. Case 3 • The page fault handler is buggy. • If the stack grows down one page, the page fault handler is likely to fail. • Why?

  27. Case 4 • The following program executes on Pentium/Linux #define PAGE_SIZE 4 * 1024 #define BUF_SIZE 128*PAGE_SIZE int main(void) { char *p = NULL; int i; A: p = mmap(0, 128 * PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, 0, 0); printf("buffer start: %p\n", p); for(i = 0; i < BUF_SIZE; i += PAGE_SIZE) p[i] = 1; B: munmap(p, BUF_SIZE); return; }

  28. Case 4 • When the program arrives at label A, the page table is like below. The number within block is the offset of PDE/PTE. The output of the function printf is “buffer start: 0xb7bdf000”. • Please write the page table like above, when the program arrives at label B? • NOTE: the white block without number means empty PDE/PTE, the white block with number means a filled PDE/PTE, the black block means multiple filled PDE/PTEs, and the grey block means a page with some data/code

  29. Case 4 Start 0xb7bdf000 L1: 2DE L2: 3DF +128K End 0xb7c5f000 L1: 2DF L2: 05F

More Related