Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
Lecture 7: Introduction to virtualmemory
Lecture 7: Introduction to virtualmemory
Mythili VutukuruIIT Bombay
Why virtualize memory?• Because real view of
memory is messy!• Earlier, memory had only
code of one running process(and OS code)
• Now, multiple activeprocesses timeshare CPU– Memory of many processes
must be in memory– Non-contiguous too
• Need to hide this complexityfrom user
2
• Because real view ofmemory is messy!
• Earlier, memory had onlycode of one running process(and OS code)
• Now, multiple activeprocesses timeshare CPU– Memory of many processes
must be in memory– Non-contiguous too
• Need to hide this complexityfrom user
Abstraction: (Virtual) Address Space
• Virtual address space: everyprocess assumes it has accessto a large space of memoryfrom address 0 to a MAX
• Contains program code (andstatic data), heap (dynamicallocations), and stack (usedduring function calls)• Stack and heap grow
during runtime• CPU issues loads and stores
to virtual addresses3
• Virtual address space: everyprocess assumes it has accessto a large space of memoryfrom address 0 to a MAX
• Contains program code (andstatic data), heap (dynamicallocations), and stack (usedduring function calls)• Stack and heap grow
during runtime• CPU issues loads and stores
to virtual addresses
How is actual memory reached?• Address translation from
virtual addresses (VA) tophysical addresses (PA)– CPU issues loads/stores to VA
but memory hardwareaccesses PA
• OS allocates memory andtracks location of processes
• Translation done by memoryhardware called MemoryManagement Unit (MMU)– OS makes the necessary
information available
• Address translation fromvirtual addresses (VA) tophysical addresses (PA)– CPU issues loads/stores to VA
but memory hardwareaccesses PA
• OS allocates memory andtracks location of processes
• Translation done by memoryhardware called MemoryManagement Unit (MMU)– OS makes the necessary
information available4
Example: Paging
• OS divides virtual address spaceinto fixed size pages, physicalmemory into frames
• To allocate memory, a page ismapped to a free physical frame
• Page table stores mappings fromvirtual page number to physicalframe number for a process (e.g,page 0 to frame 3)
• MMU has access to page tables,and uses it to translate VA to PA
• OS divides virtual address spaceinto fixed size pages, physicalmemory into frames
• To allocate memory, a page ismapped to a free physical frame
• Page table stores mappings fromvirtual page number to physicalframe number for a process (e.g,page 0 to frame 3)
• MMU has access to page tables,and uses it to translate VA to PA
5
Goals of memory virtualization
• Transparency: user programs should not beaware of the messy details
• Efficiency: minimize overhead and wastage interms of memory space and access time
• Isolation and protection: a user processshould not be able to access anything outsideits address space
• Transparency: user programs should not beaware of the messy details
• Efficiency: minimize overhead and wastage interms of memory space and access time
• Isolation and protection: a user processshould not be able to access anything outsideits address space
6
How can a user allocate memory?
• OS allocates a set ofpages to the memoryimage of the process
• Within this image– Static/global variables
are allocated in theexecutable
– Local variables of afunction on stack
– Dynamic allocation withmalloc on the heap
• OS allocates a set ofpages to the memoryimage of the process
• Within this image– Static/global variables
are allocated in theexecutable
– Local variables of afunction on stack
– Dynamic allocation withmalloc on the heap
7
Memory allocation system calls• malloc implemented by C
library– Algorithms for efficient memory
allocation and free spacemanagement
• To grow heap, libc uses thebrk/sbrk system call
• A program can also allocate apage sized memory using themmap()system call– Gets “anonymous” page from OS
brk
• malloc implemented by Clibrary– Algorithms for efficient memory
allocation and free spacemanagement
• To grow heap, libc uses thebrk/sbrk system call
• A program can also allocate apage sized memory using themmap()system call– Gets “anonymous” page from OS
8
mmap
A subtle point: what is the addressspace of the OS?
• OS is not a separateprocess with its ownaddress space
• Instead, OS code is part ofthe address space of everyprocess
• A process sees OS as partof its code (e.g., library)
• Page tables map the OSaddresses to OS code
• OS is not a separateprocess with its ownaddress space
• Instead, OS code is part ofthe address space of everyprocess
• A process sees OS as partof its code (e.g., library)
• Page tables map the OSaddresses to OS code
9
OS
How does the OS allocate memory?
• OS needs memory for its data structures• For large allocations, OS allocates a page• For smaller allocations, OS uses various
memory allocation algorithms (more later)– Cannot use libc and malloc in kernel!
• OS needs memory for its data structures• For large allocations, OS allocates a page• For smaller allocations, OS uses various
memory allocation algorithms (more later)– Cannot use libc and malloc in kernel!
10