Upload
hoshi
View
44
Download
0
Tags:
Embed Size (px)
DESCRIPTION
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. - PowerPoint PPT Presentation
Citation preview
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)
8 bit 4 bit 8 bit 12 bit
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
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.
Case 2
6
Figure 1
Case 2
page0
page1
page2
page3runarun
.text
.data
page4
page5
libc.so
.text
.data
7
• 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”
Case 2
8
47
0
Physical Memory
14
15
12
13
10
11
8
9
6
7
4
5
2
3
0
1
16
BSS
Heap
Stack
Process A
vm_next
➃vm_startvm_end
vm_next
➂vm_startvm_end
vm_next
➁vm_startvm_end
vm_next
➀vm_startvm_end
vm_next
➄vm_startvm_end
Code(.text, .rodata)
Data(.data)
SHLIB Code
SHLIB Data
vm_next
⑥vm_startvm_end
vm_next
⑦vm_startvm_end
63
r/w
r/w
r
r/w
r/w
r/w
r
9
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.
Case 2
10
• 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: ________
A sample from exam papers
text data
bss heap stack
11
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
A sample from exam papers
12
47
0
Physical Memory
14
15
12
13
10
11
8
9
6
7
4
5
2
3
0
1
16
BSS
Heap
Stack
Process A
vm_next
➃vm_startvm_end
vm_next
➂vm_startvm_end
vm_next
➁vm_startvm_end
vm_next
➀vm_startvm_end
vm_next
➄vm_startvm_end
Code(.text, .rodata)
Data(.data)
SHLIB Code
SHLIB Data
vm_next
⑥vm_startvm_end
vm_next
⑦vm_startvm_end
63
13
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
Case 2
14
• 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;}
Case 2
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.
15
Parent Process
0
16
47
Child Process
0
16
47
Parent:
Page4
Page5
Stack
Page0
Page2
Bss
Heap
Fork
PPN:0PPN:1
PPN:3
PPN:4PPN:6PPN:7PPN:8
PPN:0PPN:1
PPN:3
PPN:4PPN:6PPN:7PPN:8
page0page1page2page3bssheap
page4page5
stack
page0page1page2page3bssheap
page4page5
stack
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.
16
Parent Process
0
16
47
Child Process
0
16
47
Parent:
Page4
Page5
Stack
Page0
Page2
Bss
Heap
Fork
PPN:0PPN:1
PPN:3
PPN:4PPN:6PPN:7PPN:8
PPN:0PPN:1
PPN:10
PPN:4PPN:6PPN:7PPN:8
Child:
Stack
page0page1page2page3bssheap
page4page5
stack
page0page1page2page3bssheap
page4page5
stack
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.
17
Parent Process
0
16
47
Child Process
0
16
47
Parent:
Page4
Page5
Stack
Page0
Page2
Bss
Heap
Fork
PPN:0PPN:1
PPN:10
PPN:4PPN:6PPN:7PPN:8
Child:
Stack
Page1
PPN:11
PPN:0PPN:1
PPN:3
PPN:4PPN:6PPN:7PPN:8
page0page1page2page3bssheap
page4page5
stack
page0page1page2page3bssheap
page4page5
stack
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.
18
Parent Process
0
16
47
Child Process
0
16
47
Parent:
Page4
Page5
Stack
Page0
Page2
Bss
Heap
Fork
PPN:0PPN:1
PPN:10
PPN:4PPN:6PPN:7PPN:8
Child:
Stack
Page1
Page3
PPN:11PPN:12
PPN:0PPN:1
PPN:3
PPN:4PPN:6PPN:7PPN:8
page0page1page2page3bssheap
page4page5
stack
page0page1page2page3bssheap
page4page5
stack
PPN:8
PPN:12
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.
19
Parent Process
0
16
47
Child Process
0
16
47
Parent:
Page4
Page5
Stack
Page0
Page2
Bss
Heap
Fork
PPN:0PPN:1
PPN:10
PPN:4PPN:6PPN:13
Child:
Stack
Page1
Page3
Bss
PPN:11
PPN:0PPN:1
PPN:3
PPN:4PPN:6PPN:7PPN:8
page0page1page2page3bssheap
page4page5
stack
page0page1page2page3bssheap
page4page5
stack
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.
20
Parent Process
0
16
47
Child Process
0
16
47
Parent:
Page4
Page5
Stack
Page0
Page2
Bss
Heap
Fork
PPN:0PPN:1
PPN:10
PPN:4PPN:6PPN:13PPN:8
Child:
Stack
Page1
Page3
Bss
Page0
PPN:11PPN:12
PPN:0PPN:1
PPN:3
PPN:4PPN:6PPN:7PPN:8
page0page1page2page3bssheap
page4page5
stack
page0page1page2page3bssheap
page4page5
stack
PPN:11PPN:12
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.
21
Parent Process
0
16
47
Child Process
0
16
47
Parent:
Page4
Page5
Stack
Page0
Page2
Bss
Heap
Fork
PPN:0PPN:1
PPN:10
PPN:4PPN:14PPN:13PPN:8
Child:
Stack
Page1
Page3
Bss
Page0
Page2
PPN:0PPN:1
PPN:3
PPN:4PPN:6PPN:7PPN:8
page0page1page2page3bssheap
page4page5
stack
page0page1page2page3bssheap
page4page5
stack
PPN:11PPN:12
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.
22
Parent Process
0
16
47
Child Process
0
16
47
Parent:
Page4
Page5
Stack
Page0
Page2
Bss
Heap
Fork
PPN:0PPN:1
PPN:10
PPN:4PPN:14PPN:13PPN:15
Child:
Stack
Page1
Page3
Bss
Page0
Page2
Heap
PPN:0PPN:1
PPN:3
PPN:4PPN:6PPN:7PPN:8
page0page1page2page3bssheap
page4page5
stack
page0page1page2page3bssheap
page4page5
stack
PPN:11PPN:12
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.
23
Parent Process
0
16
47
Child Process
0
16
47
Parent:
Page4
Page5
Stack
Page0
Page2
Bss
Heap
Fork
Page1
PPN:0PPN:1
PPN:10
PPN:4PPN:14PPN:13PPN:15
Child:
Stack
Page1
Page3
Bss
Page0
Page2
Heap
PPN:0PPN:1
PPN:3
PPN:4PPN:6PPN:7PPN:8
page0page1page2page3bssheap
page4page5
stack
page0page1page2page3bssheap
page4page5
stack
PPN:11
24
do_page_faultdo_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
Case 3
25
/* 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 tablealloc_zero_page();return success;
}/* unknown type */return fail;
}
Case 3
26
find_vmafind_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;
}
Case 3
27
• The page fault handler is buggy. • If the stack grows down one page, the
page fault handler is likely to fail. • Why?
Case 3
28
• 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; }
Case 4
29
• 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
Case 4
30
Case 4
0x2FE
0x020
0x88
0x7C
0x480x49
PDE
PTE
PTE
Start0xb7bdf000L1: 2DE L2: 3DF
+128K
End0xb7c5f000L1: 2DFL2: 05F
31
0x2FE
0x20
0x88
0x7C
0x480x49
0x2DF0x2DE
3DF
3FF
0x0
0x5E
PTE
PTE
PTE
PTEPDE