10
Lecture 7: Introduction to virtual memory Mythili Vutukuru IIT Bombay

Lecture 7: Introduction to virtual memorymythili/os/anno_slides/lecture7.pdf · Lecture 7: Introduction to virtual memory Mythili Vutukuru IIT Bombay. Why virtualize memory? • Because

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture 7: Introduction to virtual memorymythili/os/anno_slides/lecture7.pdf · Lecture 7: Introduction to virtual memory Mythili Vutukuru IIT Bombay. Why virtualize memory? • Because

Lecture 7: Introduction to virtualmemory

Lecture 7: Introduction to virtualmemory

Mythili VutukuruIIT Bombay

Page 2: Lecture 7: Introduction to virtual memorymythili/os/anno_slides/lecture7.pdf · Lecture 7: Introduction to virtual memory Mythili Vutukuru IIT Bombay. Why virtualize memory? • Because

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

Page 3: Lecture 7: Introduction to virtual memorymythili/os/anno_slides/lecture7.pdf · Lecture 7: Introduction to virtual memory Mythili Vutukuru IIT Bombay. Why virtualize memory? • Because

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

Page 4: Lecture 7: Introduction to virtual memorymythili/os/anno_slides/lecture7.pdf · Lecture 7: Introduction to virtual memory Mythili Vutukuru IIT Bombay. Why virtualize memory? • Because

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

Page 5: Lecture 7: Introduction to virtual memorymythili/os/anno_slides/lecture7.pdf · Lecture 7: Introduction to virtual memory Mythili Vutukuru IIT Bombay. Why virtualize memory? • Because

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

Page 6: Lecture 7: Introduction to virtual memorymythili/os/anno_slides/lecture7.pdf · Lecture 7: Introduction to virtual memory Mythili Vutukuru IIT Bombay. Why virtualize memory? • Because

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

Page 7: Lecture 7: Introduction to virtual memorymythili/os/anno_slides/lecture7.pdf · Lecture 7: Introduction to virtual memory Mythili Vutukuru IIT Bombay. Why virtualize memory? • Because

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

Page 8: Lecture 7: Introduction to virtual memorymythili/os/anno_slides/lecture7.pdf · Lecture 7: Introduction to virtual memory Mythili Vutukuru IIT Bombay. Why virtualize memory? • Because

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

Page 9: Lecture 7: Introduction to virtual memorymythili/os/anno_slides/lecture7.pdf · Lecture 7: Introduction to virtual memory Mythili Vutukuru IIT Bombay. Why virtualize memory? • Because

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

Page 10: Lecture 7: Introduction to virtual memorymythili/os/anno_slides/lecture7.pdf · Lecture 7: Introduction to virtual memory Mythili Vutukuru IIT Bombay. Why virtualize memory? • Because

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