Computer Organization and Design
Unit 12: Virtual Memory
Slides developed by , , & C.J. Taylor at the University of Pennsylvania
with sources that included University of Wisconsin slides
Copyright By PowCoder代写 加微信 powcoder
by , , , and .
CIS 371: Comp. Org. & Design | Dr. | Virtual Memory 1
This Unit: Virtual Memory
System software
• The operating system (OS) • A super-application
• Hardware support for an OS
• Virtual memory
• Page tables and address translation • TLBs and memory hierarchy issues
CIS 371: Comp. Org. & Design | Dr. | Virtual Memory 2
• Virtual Memory: 5.4
CIS 371: Comp. Org. & Design | Dr. | Virtual Memory 3
A Computer System: Hardware
• CPUs and memories
• Connected by memory bus
• I/Operipherals:storage,input,display,network,…
• With separate or built-in DMA
• Connected by system bus (which is connected to memory bus)
Memory bus
System (I/O) bus DMA DMA
CIS 371: Comp. Org. & Design | Dr. | Virtual Memory
A Computer System: + App Software
• Applicationprocess:theworkwewanttodo • everything else is in support of this
Application process
Memory bus
System (I/O) bus
CIS 371: Comp. Org. & Design | Dr. | Virtual Memory 5
A Computer System: + OS
Application process
Memory bus
System (I/O) bus
CIS 371: Comp. Org. & Design | Dr. | Virtual Memory 6
A Computer System: + OS
Memory bus
System (I/O) bus
CIS 371: Comp. Org. & Design | Dr. | Virtual Memory 7
A Computer System: + OS
• OperatingSystem(OS):virtualizeshardwareforprocesses • Abstraction:providesservices(e.g.,threads,files,etc.)
+ Simplifies application programming model, raw hardware is nasty • Isolation:giveseachprocessillusionofprivateCPU,memory,I/O
+ Simplifies application programming model + Increases hardware resource utilization
Memory bus
System (I/O) bus
CIS 371: Comp. Org. & Design | Dr. | Virtual Memory 8
“Virtualizing” a resource
• To virtualize a resource is to make a finite amount of a resource act like a very large/infinite amount.
• Easier to write programs with a virtualized interface
• Resources we can virtualize:
• processors (via multitasking)
• DRAM (via virtual memory)
• entire machine+OS (via virtual machines)
• Key question: how do you manage state?
CIS 371: Comp. Org. & Design | Dr. | Virtual Memory 9
Multitasking: virtualizing a processor
• When multiple applications are being run on the same core, the OS shares the computer between them
• Multitasking dates back to the early days of computers when systems were expensive and we needed to be able to support multiple users concurrently.
• The OS needs to be able to switch between different processes. In most cases it does this so quickly that users/processes don’t realize the machine is being shared.
• The act of switching between processes is referred to as a
context switch
• what state is involved?
CIS 371: Comp. Org. & Design | Dr. | Virtual Memory 10
When do we perform a context switch?
• Hardware timer ensures each process gets fair access to the CPU
• When do we switch?
• when the timer goes off (e.g., every 2ms)
• At system calls because they are usually slow
• we want to do something else useful in the meantime, so switch to another process and run it instead
CIS 371: Comp. Org. & Design | Dr. | Virtual Memory 11
Virtualizing Memory
CIS 371: Comp. Org. & Design | Dr. | Virtual Memory 12
Virtualizing Main Memory
• How do multiple procs (and the OS) share main memory? • Goal:eachapplicationthinksithasallofthememory
• One process may want more memory than is in the system • Process insn/data footprint may be larger than main memory
• Requiresmainmemorytoactlikeacache
• With disk as next level in memory hierarchy (slow)
• Write-back, write-allocate, large blocks or “pages”
• No notion of “program not fitting” in registers or caches (why?)
• Solution:
• Part #1: treat memory as a “cache”
• Store the overflowed blocks in “swap” space on disk • Part #2: add a level of indirection (address translation)
CIS 371: Comp. Org. & Design | Dr. | Virtual Memory 13
Virtual Memory (VM)
• VirtualMemory(VM):
• Level of indirection
• Application generated addresses are virtual addresses (VAs)
• Each process thinks it has its own 2N bytes of address space
• Memory accessed using physical addresses (PAs)
• VAs translated to PAs at coarse (page) granularity
• OS controls VA to PA mapping for itself and all other processes
• Logically: translation performed before every insn fetch, load, store • Physically: hardware acceleration removes translation overhead
OS controlled VA®PA mappings PAs (physical memory)
CIS 371: Comp. Org. & Design | Dr. | Virtual Memory 14
Virtual Memory (VM)
• Programs use virtual addresses (VA)
• VA size (N) aka pointer size (these days, 64 bits)
• Memory uses physical addresses (PA) • PA size (M) typically M
• Update page tables, flush TLBs, retry memory access
CIS 371: Comp. Org. & Design | Dr. | Virtual Memory 37
The life of a virtual memory access
access L1$ miss
TLB hit? no yes
MMU$, PT walk
CIS 371: Comp. Org. & Design | Dr. | Virtual Memory
physical address
access memory
access L2$
fill line to L1$
What happens on a Context Switch
• Operating system responsible for handling context switching • Hardware support is just a timer interrupt
• Each process has an associated data structure which is used to record relevant state such as:
• Architected state
• PC, registers, Page table pointer
• Saved to, restored from physical memory • Memory state? Use virtual memory
• Non-architectedstate:caches,predictortables,etc.
• Ignore or flush
• ignoring is key aspect of Spectre/Meltdown vulnerabilities!
• OS swaps out values for old process, swaps in values for new process, and lets new process run
CIS 371: Comp. Org. & Design | Dr. | Virtual Memory 39
Virtual Machines
• Share physical machine among many “virtual machines”
• each has an OS which thinks it totally controls the hardware
• control actually resides w/ hypervisor or VMM (Virtual Machine Manager)
Hypervisor
Memory bus
System (I/O) bus
CIS 371: Comp. Org. & Design | Dr. | Virtual Memory 40
Virtual Machines
• Need to “context switch” between OSes
• need to virtualize virtual memory using nested page tables
• add a level of indirection
• from VA, PA to guest VA, guest PA, host PA
• an n-level PT traversal is now n2
• a big reason why VMs can be slow
using hypervisor’s page table
CIS 371: Comp. Org. & Design | Dr. | Virtual Memory
• OS virtualizes memory and I/O devices
• Virtual memory
• “infinite” memory, isolation, protection, inter-process communication • Page tables
• Translation buffers
• Parallel vs serial access, interaction with caching • Page faults
CIS 371: Comp. Org. & Design | Dr. | Virtual Memory 42
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com