Virtual Memory.

Slides:



Advertisements
Similar presentations
Carnegie Mellon 1 Virtual Memory: Concepts : Introduction to Computer Systems 15 th Lecture, Oct. 14, 2010 Instructors: Randy Bryant and Dave O’Hallaron.
Advertisements

Instructors: Randy Bryant and Dave O’Hallaron
Virtual Memory Topics Motivations for VM Address translation
P6/Linux Memory System Oct. 31, 2002
Memory System Case Studies Oct. 28, 2004 Topics P6 address translation x86-64 extensions Linux memory management Linux page fault handling Memory mapping.
– 1 – P6 (PentiumPro,II,III,Celeron) memory system bus interface unit DRAM Memory bus instruction fetch unit L1 i-cache L2 cache cache bus L1 d-cache inst.
1 Virtual Memory: Systems Level Andrew Case Slides adapted from jinyang Li, Randy Bryant and Dave O’Hallaron.
CS 153 Design of Operating Systems Spring 2015 Lecture 17: Paging.
1 Virtual Memory. 2 Outline Pentium/Linux Memory System Core i7 Suggested reading: 9.6, 9.7.
Carnegie Mellon Introduction to Computer Systems /18-243, spring th Lecture, Mar. 24 th Instructors: Gregory Kesden and Markus Püschel.
Memory System Case Studies Oct. 31, 2007 Topics P6 address translation x86-64 extensions Linux memory management Linux page fault handling Memory mapping.
University of Amsterdam Computer Systems – virtual memory Arnoud Visser 1 Computer Systems Virtual Memory.
Instructors: Majd Sakr and Khaled Harras
Processes and Virtual Memory
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Virtual Memory: Concepts Slides adapted from Bryant.
CS 105 “Tour of the Black Holes of Computing!”
Carnegie Mellon 1 Virtual Memory: Systems / : Introduction to Computer Systems 17 th Lecture, Oct. 27, 2011 Instructors: Dave O’Hallaron,
1 Virtual Memory. 2 Outline Multilevel page tables Different points of view Pentium/Linux Memory System Memory Mapping Suggested reading: 10.6, 10.3,
P6/Linux Memory System Topics P6 address translation Linux memory management Linux page fault handling memory mapping vm2.ppt CS 105 “Tour of the Black.
1 Virtual Memory. 2 Outline Multilevel page tables Different points of view Pentium/Linux Memory System Memory Mapping Suggested reading: 10.6, 10.3,
Memory System Case Studies Mar. 20, 2008 Topics P6 address translation x86-64 extensions Linux memory management Linux page fault handling Memory mapping.
Virtual Memory 1 Computer Organization II © McQuain Virtual Memory Use main memory as a “cache” for secondary (disk) storage – Managed jointly.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Virtual Memory: Systems CENG331 - Computer Organization.
Carnegie Mellon 1 Virtual Memory: Systems / : Introduction to Computer Systems 17 th Lecture, Mar. 19, 2015 Instructors: Seth Copen Goldstein,
Memory Management 백 일 우
Virtual Memory: Systems
Alan L. Cox Virtual Memory Alan L. Cox Some slides adapted from CMU slides.
1 Virtual Memory. 2 Outline Case analysis –Pentium/Linux Memory System –Core i7 Suggested reading: 9.7.
Memory System Case Studies Mar. 20, 2008 Topics P6 address translation x86-64 extensions Linux memory management Linux page fault handling Memory mapping.
Virtual Memory Alan L. Cox Some slides adapted from CMU slides.
CS 105 “Tour of the Black Holes of Computing!”
CS 105 “Tour of the Black Holes of Computing!”
Memory Management and Virtual Memory
Virtual Memory: Systems
Today How was the midterm review? Lab4 due today.
Virtual Memory: Concepts CENG331 - Computer Organization
Virtual Memory.
CS 105 “Tour of the Black Holes of Computing!”
Memory Management and Virtual Memory
CSE 153 Design of Operating Systems Winter 2018
Virtual Memory (Review)
Virtual Memory: Systems /18-213/14-513/15-513: Introduction to Computer Systems 18th Lecture, October 25, 2018.
CS 105 “Tour of the Black Holes of Computing!”
CSE 153 Design of Operating Systems Winter 2018
Virtual Memory: Concepts /18-213/14-513/15-513: Introduction to Computer Systems 17th Lecture, October 23, 2018.
Virtual Memory: Systems
Virtual Memory: Systems
Virtual Memory S04, Recitation, Section A
Making Virtual Memory Real: The Linux-x86-64 way
Memory System Case Studies Oct. 13, 2008
Pentium/Linux Memory System
P6 (PentiumPro,II,III,Celeron) memory system
Virtual Memory CSCI 380: Operating Systems Lecture #7 -- Review and Lab Suggestions William Killian.
Instructors: Majd Sakr and Khaled Harras
Pentium III / Linux Memory System April 4, 2000
Virtual Memory Nov 27, 2007 Slide Source:
Instructor: Phil Gibbons
Virtual Memory: Systems CSCI 380: Operating Systems
Virtual Memory II CSE 351 Winter 2018
CS 105 “Tour of the Black Holes of Computing!”
CSCI 380: Operating Systems William Killian
CS 105 “Tour of the Black Holes of Computing!”
CS 105 “Tour of the Black Holes of Computing!”
CSE 153 Design of Operating Systems Winter 2019
CSE 153 Design of Operating Systems Winter 2019
Virtual Memory Use main memory as a “cache” for secondary (disk) storage Managed jointly by CPU hardware and the operating system (OS) Programs share main.
CS 105 “Tour of the Black Holes of Computing!”
Virtual Memory and Paging
Instructor: Phil Gibbons
P6 (PentiumPro,II,III,Celeron) memory system
Presentation transcript:

Virtual Memory

Outline Linux Memory Management Memory Mapping Suggested reading: 10.7, 10.8

Address Translation ... ... Virtual address (VA) L1 hit L1 miss CPU VPN VPO 36 12 4 32 Virtual address (VA) L1 TLB (16 sets, 4 entries/set) PPN PPO 40 TLBmiss TLB hit physical address (PA) result 32/64 CT 6 L2, L3, and main memory L1 d-cache (64 sets, 8 lines/set) L1 hit L1 miss TLBT TLBI ... 9 9 9 9 VPN1 VPN2 VPN3 VPN4 CI CO CR3 PTE PTE PTE PTE Page Tables

Linux Virtual Memory System Process-specific data structures (e.g. task and mm structs, page tables, kernel stack) Different for each process Kernel virtual memory Physical memory Identical for each process Kernel code and data User stack %esp Memory mapped region for shared libraries Linux Virtual Memory System Process virtual memory brk Run-time heap (via malloc) Uninitialized data (.bss) Initialized data (.data) Program text (.text) x08048000 (32) x40000000 (64)

Linux organizes VM as a collection of “areas” process virtual memory vm_area_struct task_struct mm_struct vm_end vm_start mm pgd vm_prot vm_flags mmap shared libraries vm_next 0x40000000 vm_end pgd: page directory address vm_prot: read/write permissions for this area vm_start data vm_prot vm_flags 0x0804a020 text vm_next vm_end vm_start 0x08048000 vm_prot vm_flags vm_next

Linux organizes VM as a collection of “areas” process virtual memory vm_area_struct task_struct mm_struct vm_end vm_start mm pgd vm_prot vm_flags mmap shared libraries vm_next 0x40000000 vm_end vm_flags shared with other processes or private to this process vm_start data vm_prot vm_flags 0x0804a020 text vm_next vm_end vm_start 0x08048000 vm_prot vm_flags vm_next

Linux page fault handling vm_area_struct vm_end r/o vm_next vm_start r/w process virtual memory text data shared libraries write read 1 2 3 Is the VA legal? i.e. is it in an area defined by a vm_area_struct? if not then signal segmentation violation (e.g. (1))

Linux page fault handling vm_area_struct vm_end r/o vm_next vm_start r/w process virtual memory text data shared libraries write read 1 2 3 Is the operation legal? i.e., can the process read/write this area? if not then signal protection violation (e.g., (2)) If OK, handle fault e.g., (3)

Creation of new VM area done via “memory mapping” create new vm_area_struct and page tables for area Area can be backed by (i.e., get its initial values from) : regular file on disk (e.g., an executable object file) initial page bytes come from a section of a file

Area can be backed by (i.e., get its initial values from) Memory mapping Area can be backed by (i.e., get its initial values from) anonymous file (e.g. nothing) First fault will allocate a physical page full of 0’s (demand-zero page) Once the page is written to (dirtied), it is like any other page

Memory mapping Dirty pages are copied back and forth between memory and a special swap file.

Demand Paging Key point: no virtual pages are copied into physical memory until they are referenced! known as “demand paging” crucial for time and space efficiency

Exec() revisited process-specific data structures (page tables, task and mm structs Kernal stack) To run a new program p in the current process using exec(): Free all vm_area_structs and page tables for old areas. same for each process physical memory kernel code/data kernel VM 0xc0000000 %esp stack demand-zero process VM Memory mapped region for shared libraries .data .text libc.so brk runtime heap (via malloc) demand-zero uninitialized data (.bss) initialized data (.data) .data program text (.text) .text forbidden p

Exec() revisited process-specific data structures (page tables, task and mm structs Kernal stack) To run a new program p in the current process using exec(): create new vm_area_structs and page tables for new areas. stack, bss, data, text, shared libs. same for each process physical memory kernel code/data kernel VM 0xc0000000 stack demand-zero %esp process VM Memory mapped region for shared libraries .data .text libc.so brk runtime heap (via malloc) demand-zero uninitialized data (.bss) initialized data (.data) .data program text (.text) .text forbidden p

Exec() revisited process-specific data structures (page tables, task and mm structs Kernal stack) To run a new program p in the current process using exec(): create new vm_area_structs and page tables for new areas. text and data backed by ELF executable object file. bss and stack initialized to zero. same for each process physical memory kernel code/data kernel VM 0xc0000000 stack demand-zero %esp process VM Memory mapped region for shared libraries .data .text libc.so brk runtime heap (via malloc) demand-zero uninitialized data (.bss) initialized data (.data) .data program text (.text) .text forbidden p

Exec() revisited process-specific data structures (page tables, task and mm structs Kernal stack) To run a new program p in the current process using exec(): set PC to entry point in .text Linux will swap in code and data pages as needed. same for each process physical memory kernel code/data kernel VM 0xc0000000 stack demand-zero %esp process VM Memory mapped region for shared libraries .data .text libc.so brk runtime heap (via malloc) demand-zero uninitialized data (.bss) initialized data (.data) .data program text (.text) .text forbidden p

User-level memory mapping void *mmap(void *start, int len, int prot, int flags, int fd, int offset) Map len bytes starting at offset offset of the file specified by file description fd, preferably at address start (usually 0 for don’t care). prot: MAP_READ, MAP_WRITE flags: MAP_PRIVATE, MAP_SHARED, MAP_ANON Return a pointer to the mapped area

User-level memory mapping void *mmap(void *start, int len, int prot, int flags, int fd, int offset) len bytes start (or address chosen by kernel) len bytes offset (bytes) Process virtual memory Disk file specified by file descriptor fd

User-level memory mapping Example: fast file copy Useful for applications like Web servers that need to quickly copy files. mmap allows file transfers without copying into user space.

mmap() example: fast file copy /* * mmapcopy - uses mmap to copy file fd to stdout */ void mmapcopy(int fd, int size) { char *bufp; /* map the file to a new VM area */ bufp = mmap(0, size, PROT_READ, MAP_PRIVATE, fd, 0); /* write the VM area to stdout */ write(1, bufp, size); return ; }

mmap() example: fast file copy int main(int argc, char **argv) { struct stat stat; /* check for required command line argument */ if ( argc != 2 ) { printf(“usage: %s <filename>\n”, argv[0]); exit(0) ; } /* open the file and get its size*/ fd = open(argv[1], O_RDONLY, 0); fstat(fd, &stat); mmapcopy(fd, stat.st_size);

To create a new process using fork: Fork() revisted To create a new process using fork: make copies of the old process’s mm_struct, vm_area_struct, and page tables Two processes are sharing all of their pages (At this point) How to get separate spaces without copying all the virtual pages from one space to another? “Copy On Write” (COW) technique.

Shared Object Shared Object Shared Area An object which is mapped into an area of virtual memory of a process Any writes that the process makes to that area are visible to any other processes that have also mapped the shared object into their virtual memory The changes are also reflected in the original object on disk Shared Area A virtual memory area that a shared object is mapped

Private object Private Object Private Area As oppose to shared object Changes made to an area mapped to a private object are not visible to other processes Any writes that the process makes to the area are not reflected back to the object on disk. Private Area Similar to shared area

Sharing Revisited: Shared Objects Process 1 virtual memory Physical memory Process 2 virtual memory Process 1 maps the shared object Shared object

Sharing Revisited: Shared Objects Process 1 virtual memory Physical memory Process 2 virtual memory Process 2 maps the shared object Notice how the virtual addresses can be different Shared object

Copy-on-Write A private object begins life in exactly the same way as a shared object, with only one copy of the private object stored in physical memory.

To create a new process using fork Fork() revisted To create a new process using fork Copy-On-Write make pages of writeable areas read-only flag vm_area_struct for these areas as private “copy-on-write”. writes by either process to these pages will cause page faults fault handler recognizes copy-on-write, makes a copy of the page, and restores write permissions.

Sharing Revisited: Private COW Objects Process 1 virtual memory Physical memory Process 2 virtual memory Private copy-on-write area Private copy-on-write object

For each process that maps the private object Copy-on-Write For each process that maps the private object The page table entries for the corresponding private area are flagged as read-only The area struct is flagged as private copy-on-write So long as neither process attempts to write to its respective private area, they continue to share a single copy of the object in physical memory.

For each process that maps the private object Copy-on-Write For each process that maps the private object As soon as a process attempts to write to some page in the private area, the write triggers a protection fault The fault handler notices that the protection exception was caused by the process trying to write to a page in a private copy-on-write area

For each process that maps the private object Copy-on-Write For each process that maps the private object The fault handler Creates a new copy of the page in physical memory Updates the page table entry to point to the new copy Restores write permissions to the page

Sharing Revisited: Private COW Objects Process 1 virtual memory Physical memory Process 2 virtual memory Copy-on-write Write to private copy-on-write page Private copy-on-write object

To create a new process using fork: Fork() revisted To create a new process using fork: Net result: copies are deferred until absolutely necessary (i.e., when one of the processes tries to modify a shared page).