VIRTUAL MEMORY

33
VIRTUAL MEMORY PRESENTED BY KAMRAN ASHRAF 13-NTU-4009

Transcript of VIRTUAL MEMORY

Page 1: VIRTUAL MEMORY

VIRTUAL MEMORYPRESENTED BY KAMRAN ASHRAF13-NTU-4009

Page 2: VIRTUAL MEMORY

Outline Virtual Memory

Page faults

Swapping

Paging

Segmentation

Paging in Linux

Cache translation in hardware

Translation Lookaside buffer (TLB)

Page 3: VIRTUAL MEMORY

Goals of OS memory management

Allocate limited memory resources among competing

processes

Maximizing memory

utilization

Provide isolation between processes

Page 4: VIRTUAL MEMORY

Our main questions How is protection enforced?

How are processes relocated?

How is memory partitioned?

Page 5: VIRTUAL MEMORY

Why Virtual Memory? The basic abstraction that the OS provides for memory management is virtual

memory

◦ VM enables programs to execute without requiring their entire address space to be resident in physical memory

◦ program can also execute on machines with less RAM than it “needs”

virtual memory isolates processes from each other

◦ each process has its own isolated address space

Page 6: VIRTUAL MEMORY

Virtual addresses To make it easier to manage memory of multiple processes, virtual addresses are used

◦ virtual addresses are independent of location in physical memory (RAM)

◦ OS determines location in physical memory

virtual addresses are translated by hardware into physical addresses

Page 7: VIRTUAL MEMORY

Swapping

save a program’s entire state (including its

memory image) to disk

allows another program to run

first program can be swapped back in and re-started right where

it was

Page 8: VIRTUAL MEMORY
Page 9: VIRTUAL MEMORY

Fragmentation internal fragmentation:

◦ the available partition is larger than what was requested

external fragmentation:

◦ two small partitions left, but one big job

dealing with fragmentation:◦ Swap a program out

◦ Re-load it, adjacent to another

partition 0

partition 1

partition 2

partition 3

partition 4

partition 0

partition 1

partition 2partition 3

partition 4

Page 10: VIRTUAL MEMORY

Modern technique: Paging Solve the external fragmentation problem by using fixed sized units in both physical and

virtual memory

frame 0

frame 1

frame 2

frame Y

physical address space

page 0

page 1

page 2

page X

virtual address space

…page 3

Page 11: VIRTUAL MEMORY

Virtual address space Processes view memory as a contiguous address space from bytes 0 through N

◦ virtual address space (VAS)

In reality, virtual pages are scattered across physical memory frames

◦ virtual-to-physical mapping is invisible to the program

Protection is provided because a program cannot reference memory outside of its VAS

Page 12: VIRTUAL MEMORY

Address translation a virtual address has two parts: virtual page number & offset

◦ virtual page number (VPN) is index into a page table

◦ page table entry contains page frame number (PFN)

◦ physical address is PFN::offset

Page tables

◦ managed by the OS

◦ map virtual page number (VPN) to page frame number (PFN)

Page 13: VIRTUAL MEMORY

Examples:◦ workstations, servers, modern PCs, etc.

Address Translation: Hardware converts virtual addresses to physical addresses via OS-managed lookup table (page table)

CPU

0:1:

N-1:

Memory

0:1:

P-1:

Page Table

Disk

VirtualAddresses

PhysicalAddresses

Page 14: VIRTUAL MEMORY

pageframe 0

pageframe 1

pageframe 2

pageframe Y

pageframe 3

physical memory

offset

physical address

page frame #page frame #

page table

offset

virtual address

virtual page #

Page 15: VIRTUAL MEMORY

Page Table Entries (PTEs)

◦ the valid bit says whether or not the PTE can be used◦ says whether or not a virtual address is valid◦ it is checked each time a virtual address is used

◦ the referenced bit says whether the page has been accessed◦ it is set when a page has been read or written to

◦ the modified bit says whether or not the page is dirty◦ it is set when a write to the page has occurred

◦ the protection bits control which operations are allowed◦ read, write, execute

◦ the page frame number determines the physical page◦ physical page start address = PFN

page frame numberprotMRV

202111

Page 16: VIRTUAL MEMORY

Paging advantages Easy to allocate physical memory

◦ physical memory is allocated from free list of frames

◦ external fragmentation is not a problem!

Leads naturally to virtual memory

◦ entire program need not be memory resident

◦ take page faults using “valid” bit

Page 17: VIRTUAL MEMORY

Paging disadvantages Can still have internal fragmentation

◦ process may not use memory in exact multiples of pages

Memory reference overhead

◦ 2 references per address lookup (page table, then memory)

◦ solution: use a hardware cache to absorb page table lookups

Page 18: VIRTUAL MEMORY

Page Faults What if an object is on disk rather than in memory?

◦ Page table entry indicates virtual address not in memory◦ OS exception handler invoked to move data from disk into memory

◦ current process suspends, others can resume◦ OS has full control over placement, etc.

CPU

Memory

Page Table

Disk

VirtualAddresses

PhysicalAddresses

CPU

Memory

Page Table

Disk

VirtualAddresses

PhysicalAddresses

Before After

Page 19: VIRTUAL MEMORY

Example: suppose page size is 4 bytes.

Page 20: VIRTUAL MEMORY

Addressing Page TablesWhere do we store Page Tables? (Which address space)

Physical Memory◦ Easy to address, no translation required◦ But allocated page tables consume memory for life time of VAS

Virtual Memory (OS Virtual address space)◦ Unused page table pages can be paged out to disk◦ But addressing required translation

Page 21: VIRTUAL MEMORY

Segmentation

partition an address space into logical unitsstack, code, data, subroutines, …

Page 22: VIRTUAL MEMORY

Segmentation Vs PagingPAGING

◦ Eases various memory allocation complexities (e.g., fragmentation)

◦ divide it into pages of equal size (e.g., 4KB)◦ use a page table to map virtual pages to

physical page frames◦ page (logical) => page frame (physical)

◦ Fixed size partition

SEGMENTATION

◦ partition an address space into logical units◦ stack, code, heap, subroutines, …

◦ a virtual address is <segment #, offset>◦ Variable size partition

Page 23: VIRTUAL MEMORY

Why Segmentation? More “logical”

◦ Linker takes a bunch of independent modules that call each other and organizes them

Facilitates sharing and reuse◦ a segment is a natural unit of sharing

A natural extension of variable-sized partitions◦ variable-sized partition = 1 segment/process◦ segmentation = many segments/process

Page 24: VIRTUAL MEMORY

Hardware support Segment table

◦ multiple base/limit pairs, one per segment

◦ segments named by segment #, used as index into table

◦ a virtual address is <segment #, offset>

Page 25: VIRTUAL MEMORY

Segment Lookups

segment 0

segment 1

segment 2

segment 3

segment 4

physical memory

segment #

+

virtual address

<?

raise protection fault

no

yes

offset

baselimit

segment table

Page 26: VIRTUAL MEMORY

Pros and Cons

+ efficient for sparse address spaces

+ easy to share whole segments (for example, code segment)

- complex memory allocation

- Still need first fit, best fit, etc., and re-shuffling to coalesce free fragments, if no single free space is big enough for a new segment.

Page 27: VIRTUAL MEMORY

Linux◦ 1 kernel code segment, 1 kernel data segment

◦ 1 user code segment, 1 user data segment

◦ N task state segments (stores registers on context switch)

◦ all of these segments are paged

Page 28: VIRTUAL MEMORY

Segmentation and Paging Can combine segmentation and Paging

Use segments to manage logically related units

Use pages to partition segments into fixed size chunks

Complex as two lookups per memory reference

Page 29: VIRTUAL MEMORY

Segmentation and Paging Translation

Page 30: VIRTUAL MEMORY

Efficient Translation Our original page table scheme already doubled the cost of doing memory lookups

◦ One lookup for page table, another to fetch the data

Now two level page tables triple the cost◦ Two lookups into the page tables, third to fetch the data

How can we use paging and have same lookup cost as fetching from memory?◦ Cache translation in hardware◦ Translation Lookaside buffer (TLB

Page 31: VIRTUAL MEMORY

Integrating Cache

CPUTrans-lation

Cache MainMemory

VA PA miss

hitdata

Page 32: VIRTUAL MEMORY

Speeding up Translation with a TLB

“Translation Lookaside Buffer” (TLB)◦ Small hardware cache in MMU◦ Maps virtual page numbers to physical page numbers◦ Contains complete page table entries for small number of pages

CPUTLBLookup

Cache MainMemory

VA PA miss

hit

data

Trans-lation

hit

miss

Page 33: VIRTUAL MEMORY

Thanks