Virtual Memory: Systems CSCI 380: Operating Systems

Slides:



Advertisements
Similar presentations
Chris Riesbeck, Fall 2007 Dynamic Memory Allocation Today Dynamic memory allocation – mechanisms & policies Memory bugs.
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.
The ‘mmap()’ method Adding the ‘mmap()’ capability to our ‘vram.c’ device-driver.
– 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.
Memory Mapping Sarah Diesburg COP5641.
Memory-Mapped Files & Unified VM System Vivek Pai.
1 Virtual Memory: Systems Level Andrew Case Slides adapted from jinyang Li, Randy Bryant and Dave O’Hallaron.
Virtual & Dynamic Memory Management Summer 2014 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University.
The Structure of Processes. What is a Process? an instance of running program Program vs process(task) Program : just a passive collection of instructions.
1 Virtual Memory. 2 Outline Pentium/Linux Memory System Core i7 Suggested reading: 9.6, 9.7.
The Structure of Processes (Chap 6 in the book “The Design of the UNIX Operating System”)
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
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Virtual Memory Implementation.
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.
What is a Process ? A program in execution.
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,
Why Kernel Works? kernel function gets dynamic memory in a fairly straightforward manner _get_free_pages( ) or alloc_pages( ) The simple approaches work.
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.
Lecture 24 – Paging implementation
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
Virtual Memory: Concepts CENG331 - Computer Organization
Memory Management and Virtual Memory
Virtual Memory (Review)
Virtual Memory: Systems /18-213/14-513/15-513: Introduction to Computer Systems 18th Lecture, October 25, 2018.
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
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.
Pentium III / Linux Memory System April 4, 2000
Virtual Memory.
Instructor: Phil Gibbons
Operating System Chapter 7. Memory Management
CS 105 “Tour of the Black Holes of Computing!”
CSCI 380: Operating Systems William Killian
CS 105 “Tour of the Black Holes of Computing!”
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.
Processes David Ferry, Chris Gill, Brian Kocoloski
User-level Memory Chris Gill, David Ferry, Brian Kocoloski
CS 105 “Tour of the Black Holes of Computing!”
Dirty COW Race Condition Attack
Instructor: Phil Gibbons
P6 (PentiumPro,II,III,Celeron) memory system
Presentation transcript:

Virtual Memory: Systems CSCI 380: Operating Systems Instructor: William Killian

Virtual Address Space of a Linux Process Process-specific data structs (ptables, task and mm structs, kernel stack) Different for each process Kernel virtual memory Physical memory Identical for each process Kernel code and data User stack %rsp Memory mapped region for shared libraries Process virtual memory brk Runtime heap (malloc) Uninitialized data (.bss) Initialized data (.data) Program text (.text) 0x00400000

Linux Organizes VM as 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 vm_end pgd: Page global directory address Points to L1 page table vm_prot: Read/write permissions for this area vm_flags Pages shared with other processes or private to this process vm_start Data vm_prot vm_flags Text vm_next vm_end vm_start vm_prot vm_flags vm_next

Linux Page Fault Handling Process virtual memory vm_area_struct vm_end vm_start vm_prot vm_flags shared libraries vm_next read 1 Segmentation fault: accessing a non-existing page vm_end vm_start read 3 data vm_prot Normal page fault vm_flags text vm_next write 2 Protection exception: e.g., violating permission by writing to a read-only page (Linux reports as Segmentation fault) vm_end vm_start vm_prot vm_flags vm_next

Memory Mapping VM areas initialized by associating them with disk objects. Process is known as memory mapping. 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 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 Dirty pages are copied back and forth between memory and a special swap file.

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

Sharing Revisited: Private Copy-on-write (COW) Objects Process 1 virtual memory Physical memory Process 2 virtual memory Two processes mapping a private copy-on-write (COW) object. Area flagged as private copy-on-write PTEs in private areas are flagged as read-only Private copy-on-write area Private copy-on-write object

Sharing Revisited: Private Copy-on-write (COW) Objects Process 1 virtual memory Physical memory Process 2 virtual memory Instruction writing to private page triggers protection fault. Handler creates new R/W page. Instruction restarts upon handler return. Copying deferred as long as possible! Copy-on-write Write to private copy-on-write page Private copy-on-write object

The fork Function Revisited VM and memory mapping explain how fork provides private address space for each process. To create virtual address for new new process Create exact copies of current mm_struct, vm_area_struct, and page tables. Flag each page in both processes as read-only Flag each vm_area_struct in both processes as private COW On return, each process has exact copy of virtual memory Subsequent writes create new pages using COW mechanism.

The execve Function Revisited To load and run a new program a.out in the current process using execve: Free vm_area_struct’s and page tables for old areas Create vm_area_struct’s and page tables for new areas Programs and initialized data backed by object files. .bss and stack backed by anonymous files . Set PC to entry point in .text Linux will fault in code and data pages as needed. User stack Private, demand-zero libc.so Memory mapped region for shared libraries .data Shared, file-backed .text Runtime heap (via malloc) Private, demand-zero Uninitialized data (.bss) Private, demand-zero a.out Initialized data (.data) .data Private, file-backed .text Program text (.text)

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 start: may be 0 for “pick an address” prot: PROT_READ, PROT_WRITE, ... flags: MAP_ANON, MAP_PRIVATE, MAP_SHARED, ... Return a pointer to start of mapped area (may not be start)

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) Disk file specified by file descriptor fd Process virtual memory

Example: Using mmap to Copy Files Copying a file to stdout without transferring data to user space . #include "csapp.h" void mmapcopy(int fd, int size) { /* Ptr to memory mapped area */ char *bufp; bufp = Mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); Write(1, bufp, size); return; } /* mmapcopy driver */ int main(int argc, char **argv) { struct stat stat; int fd; /* Check for required cmd line arg */ if (argc != 2) { printf("usage: %s <filename>\n", argv[0]); exit(0); } /* Copy input file to stdout */ fd = Open(argv[1], O_RDONLY, 0); Fstat(fd, &stat); mmapcopy(fd, stat.st_size); mmapcopy.c mmapcopy.c