0% found this document useful (0 votes)
22 views

12 VirtualMemory

The document discusses virtual memory and how it is implemented. Virtual memory allows for a larger virtual address space than physical memory by mapping virtual addresses to physical addresses. This is done using hierarchical page tables which break the virtual address space into pages and map them to physical pages. The page tables need to be stored efficiently since representing the full mapping would require too much space. Translation is also optimized by caching mappings to allow fast translation without walking the page tables on every memory access.

Uploaded by

Manoj Kumar G
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

12 VirtualMemory

The document discusses virtual memory and how it is implemented. Virtual memory allows for a larger virtual address space than physical memory by mapping virtual addresses to physical addresses. This is done using hierarchical page tables which break the virtual address space into pages and map them to physical pages. The page tables need to be stored efficiently since representing the full mapping would require too much space. Translation is also optimized by caching mappings to allow fast translation without walking the page tables on every memory access.

Uploaded by

Manoj Kumar G
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Virtual Memory

Learning to Play Well With Others

(Physical) Memory 0x10000 (64KB) Stack

Heap 0x00000

Learning to Play Well With Others

malloc(0x20000)

(Physical) Memory 0x10000 (64KB) Stack

Heap 0x00000

Learning to Play Well With Others

malloc(0x20000)

(Physical) Memory 0x10000 (64KB) Stack

Heap 0x00000

Learning to Play Well With Others

(Physical) Memory 0x10000 (64KB) Stack

Heap 0x00000

Learning to Play Well With Others

(Physical) Memory 0x10000 (64KB) Stack

Heap 0x00000

Learning to Play Well With Others

(Physical) Memory 0x10000 (64KB) Stack

Heap 0x00000

Learning to Play Well With Others

(Physical) Memory 0x10000 (64KB) Stack

Heap 0x00000

Learning to Play Well With Others


Virtual Memory 0x10000 (64KB) Stack

Heap 0x00000 Virtual Memory 0x10000 (64KB) Stack

Heap 0x00000

Learning to Play Well With Others


Virtual Memory 0x10000 (64KB) Stack Physical Memory Heap 0x00000 Virtual Memory 0x10000 (64KB) Stack 0x00000 0x10000 (64KB)

Heap 0x00000

Learning to Play Well With Others


Virtual Memory 0x10000 (64KB) Stack Physical Memory Heap 0x00000 Virtual Memory 0x10000 (64KB) Stack 0x00000 0x10000 (64KB)

Heap 0x00000

Learning to Play Well With Others


Virtual Memory 0x400000 (4MB) Stack Physical Memory Heap 0x00000 Virtual Memory 0xF000000 (240MB) Stack 0x00000 Disk (GBs) Heap 0x00000 0x10000 (64KB)

Mapping
mapping Virtual-to-physicaladdress space Virtual --> virtual

We will break both address spaces up into pages


a page Use physical table to map between virtual pages and pages. The processor generates virtual addresses They are translated via address translation into

physical --> physical address space

Typically 4KB in size, although sometimes large

physical addresses.

Implementing Virtual Memory


232 -1 230 1 (or whatever)

Stack

We need to keep track of this mapping Heap 0 Virtual Address Space 0 Physical Address Space

The Mapping Process


Virtual address (32 bits) Virtual Page Number Page Offset (log(page size))

Virtual-to-physical map

Physical Page Number

Page Offset (log(page size))

Physical address (32 bits)

Two Problems With VM

How do we store the map compactly? How do we translation quickly?

How Big is the map?


bit address space: 324GB of virtual addresses

1MPages Each entry is 4 bytes (a 32 bit physical address) 4MB of map bit address 6416 exabytes of space address space virtual 4PetaPagesbytes is Entry of 8 64PB map
10

Shrinking the map


Only store the entries that matter (i.e.,. enough for your physical address space) on a 64GB pages,64bit machine 128MB of map 16M big. This is still pretty map is now hard because we Representing therepresentation. need a sparse How do you represent this sparse map?

The OS allocates stuff all over the place. For security, convenience, or caching optimizations For instance: The stack is at the top of memory. The heap is at the bottom
11

Hierarchical Page Tables


page number into several Break the virtual N bits, build an 2 -ary treepieces If each piece has of the tree that contain valid Only store the part pages the To do translation, walk down visit.tree using the pieces to select with child to
N

12

Hierarchical Page Table


Virtual Address
31 22 21 12 11 0

p1

p2

offset

10-bit 10-bit L1 index L2 index Root of the Current Page Table


p1 p2

offset

(Processor Register)

Level 1 Page Table Level 2 Page Tables

Parts of the map that exist Parts that dont


Data Pages

Making Translation Fast

Address translation has to happen for every memory access it squarely on the critical This potentially puts(which are already slow) for memory operation

14

Solution 1: Use the Page Table

We could walk the page table on every memory access load or store requires Result: everywalk the page table. an additional 3-4 loads to Unacceptable performance hit.
15

You might also like