CS 333 Introduction to Operating Systems Class 11 – Virtual Memory (1)

Slides:



Advertisements
Similar presentations
1 Memory hierarchy and paging Electronic Computers M.
Advertisements

OS Fall’02 Virtual Memory Operating Systems Fall 2002.
16.317: Microprocessor System Design I
Computer Organization CS224 Fall 2012 Lesson 44. Virtual Memory  Use main memory as a “cache” for secondary (disk) storage l Managed jointly by CPU hardware.
CS 333 Introduction to Operating Systems Class 12 - Virtual Memory (2) Jonathan Walpole Computer Science Portland State University.
CS 333 Introduction to Operating Systems Class 12 - Virtual Memory (2) Jonathan Walpole Computer Science Portland State University.
CS 333 Introduction to Operating Systems Class 12 - Virtual Memory (2) Jonathan Walpole Computer Science Portland State University.
Memory Management (II)
CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Memory Management -3 CS 342 – Operating Systems Ibrahim Korpeoglu Bilkent.
Memory Management and Paging CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
CS 333 Introduction to Operating Systems Class 11 – Virtual Memory (1)
Paging and Virtual Memory. Memory management: Review  Fixed partitioning, dynamic partitioning  Problems Internal/external fragmentation A process can.
Chapter 3.2 : Virtual Memory
Translation Buffers (TLB’s)
CS 333 Introduction to Operating Systems Class 9 - Memory Management
Virtual Memory and Paging J. Nelson Amaral. Large Data Sets Size of address space: – 32-bit machines: 2 32 = 4 GB – 64-bit machines: 2 64 = a huge number.
©UCB CS 161 Ch 7: Memory Hierarchy LECTURE 24 Instructor: L.N. Bhuyan
EECE476: Computer Architecture Lecture 27: Virtual Memory, TLBs, and Caches Chapter 7 The University of British ColumbiaEECE 476© 2005 Guy Lemieux.
CS 333 Introduction to Operating Systems Class 12 - Virtual Memory (2) Jonathan Walpole Computer Science Portland State University.
CS 333 Introduction to Operating Systems Class 9 - Memory Management Jonathan Walpole Computer Science Portland State University.
Paging. Memory Partitioning Troubles Fragmentation Need for compaction/swapping A process size is limited by the available physical memory Dynamic growth.
CS333 Intro to Operating Systems Jonathan Walpole.
Operating Systems Chapter 8
CS333 Intro to Operating Systems Jonathan Walpole.
Computer Architecture Lecture 28 Fasih ur Rehman.
1 Chapter 3.2 : Virtual Memory What is virtual memory? What is virtual memory? Virtual memory management schemes Virtual memory management schemes Paging.
Lecture Topics: 11/17 Page tables TLBs Virtual memory flat page tables
Operating Systems COMP 4850/CISG 5550 Page Tables TLBs Inverted Page Tables Dr. James Money.
Chapter 8 – Main Memory (Pgs ). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui COMP 203 / NWEN 201 Computer Organisation / Computer Architectures Virtual.
CS399 New Beginnings Jonathan Walpole. Virtual Memory (1)
By Teacher Asma Aleisa Year 1433 H.   Goals of memory management  To provide a convenient abstraction for programming.  To allocate scarce memory.
Virtual Memory 1 1.
1 Some Real Problem  What if a program needs more memory than the machine has? —even if individual programs fit in memory, how can we run multiple programs?
1 Memory Management. 2 Fixed Partitions Legend Free Space 0k 4k 16k 64k 128k Internal fragmentation (cannot be reallocated) Divide memory into n (possible.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Virtual Memory Hardware.
4.3 Virtual Memory. Virtual memory  Want to run programs (code+stack+data) larger than available memory.  Overlays programmer divides program into pieces.
ICOM Noack Memory management Virtual memory Paging and segmentation Demand paging Memory management hardware.
CS2100 Computer Organisation Virtual Memory – Own reading only (AY2015/6) Semester 1.
Virtual Memory Review Goal: give illusion of a large memory Allow many processes to share single memory Strategy Break physical memory up into blocks (pages)
CS203 – Advanced Computer Architecture Virtual Memory.
Chapter 7: Main Memory CS 170, Fall Program Execution & Memory Management Program execution Swapping Contiguous Memory Allocation Paging Structure.
Memory Management memory hierarchy programs exhibit locality of reference - non-uniform reference patterns temporal locality - a program that references.
CS161 – Design and Architecture of Computer
Translation Lookaside Buffer
Lecture 11 Virtual Memory
ECE232: Hardware Organization and Design
Memory COMPUTER ARCHITECTURE
CS161 – Design and Architecture of Computer
Virtual Memory - Part II
Virtual Memory User memory model so far:
CS703 - Advanced Operating Systems
Page Table Implementation
Some Real Problem What if a program needs more memory than the machine has? even if individual programs fit in memory, how can we run multiple programs?
CS510 Operating System Foundations
Lecture 28: Virtual Memory-Address Translation
CSCI206 - Computer Organization & Programming
CS399 New Beginnings Jonathan Walpole.
Translation Lookaside Buffer
CSE 451: Operating Systems Autumn 2005 Memory Management
Translation Buffers (TLB’s)
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
Translation Buffers (TLB’s)
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
Translation Buffers (TLBs)
Review What are the advantages/disadvantages of pages versus segments?
4.3 Virtual Memory.
Virtual Memory 1 1.
Presentation transcript:

CS 333 Introduction to Operating Systems Class 11 – Virtual Memory (1) Jonathan Walpole Computer Science Portland State University

Virtual addresses Virtual memory addresses (what the process uses) Page number plus byte offset in page Low order n bits are the byte offset Remaining high order bits are the page number bit 31 bit n-1 bit 0 20 bits 12 bits page number offset Example: 32 bit virtual address Page size = 212 = 4KB Address space size = 232 bytes = 4GB

Physical addresses Physical memory addresses (what the CPU uses) Page “frame” number plus byte offset in page Low order n bits are the byte offset Remaining high order bits are the page frame number bit 24 bit n-1 bit 0 12 bits 12 bits Page frame number offset Example: 24 bit physical address Page frame size = 212 = 4KB Max physical memory size = 224 bytes = 16MB

Address translation Hardware maps page numbers to page frame numbers Memory management unit (MMU) has multiple registers for multiple pages Like a base register except its value is substituted for the page number rather than added to it Why don’t we need a limit register for each page?

Memory Management Unit (MMU)

Virtual address spaces Here is the virtual address space (as seen by the process) Lowest address Highest address Virtual Addr Space

Virtual address spaces The address space is divided into “pages” In BLITZ, the page size is 8K Page 0 1 2 3 4 5 6 7 N Page 1 A Page Page N Virtual Addr Space

Virtual address spaces In reality, only some of the pages are used 1 2 3 4 5 6 7 N Unused Virtual Addr Space

Physical memory Physical memory is divided into “page frames” (Page size = frame size) 1 2 3 4 5 6 7 N Virtual Addr Space Physical memory

Virtual and physical address spaces Some page frames are used to hold the pages of this process 1 2 3 4 5 6 7 N These frames are used for this process Virtual Addr Space Physical memory

Virtual and physical address spaces Some page frames are used for other processes 1 2 3 4 5 6 7 N Used by other processes Virtual Addr Space Physical memory

Virtual address spaces Address mappings say which frame has which page 1 2 3 4 5 6 7 N Virtual Addr Space Physical memory

Page tables Address mappings are stored in a page table in memory One page table entry per page... Is this page in memory? If so, which frame is it in? 1 2 3 4 5 6 7 N Virtual Addr Space Physical memory

Address mappings and translation Address mappings are stored in a page table in memory Typically one page table for each process Address translation is done by hardware (ie the MMU) How does the MMU get the address mappings? Either the MMU holds the entire page table (too expensive) Or the MMU holds a portion of the page table MMU caches page table entries called a translation look-aside buffer (TLB)

Address mappings and translation What if the TLB needs a mapping it doesn’t have? Software managed TLB it generates a TLB-miss fault which is handled by the operating system (like interrupt or trap handling) The operating system looks in the page tables, gets the mapping from the right entry, and puts it in the TLB Hardware managed TLB it looks in a pre-specified memory location for the appropriate entry in the page table The hardware architecture defines where page tables must be stored in memory

The BLITZ architecture Page size 8 Kbytes Virtual addresses (“logical addresses”) 24 bits --> 16 Mbyte virtual address space 211 Pages --> 11 bits for page number

The BLITZ architecture Page size 8 Kbytes Virtual addresses (“logical addresses”) 24 bits --> 16 Mbyte virtual address space 211 Pages --> 11 bits for page number An address: 23 13 12 11 bits 13 bits page number offset

The BLITZ architecture Physical addresses 32 bits --> 4 Gbyte installed memory (max) 219 Frames --> 19 bits for frame number

The BLITZ architecture Physical addresses 32 bits --> 4 Gbyte installed memory (max) 219 Frames --> 19 bits for frame number 31 13 12 19 bits 13 bits frame number offset

The BLITZ architecture The page table mapping: Page --> Frame Virtual Address: Physical Address: 23 13 12 11 bits 31 13 12 19 bits

The BLITZ page table An array of “page table entries” Kept in memory 211 pages in a virtual address space? ---> 2K entries in the table Each entry is 4 bytes long 19 bits The Frame Number 1 bit Valid Bit 1 bit Writable Bit 1 bit Dirty Bit 1 bit Referenced Bit 9 bits Unused (and available for OS algorithms)

The BLITZ page table Two page table related registers in the CPU Page Table Base Register Page Table Length Register These define the “current” page table. Must be saved and restored on process context switch Bits in the CPU “status register” “System Mode” “Interrupts Enabled” “Paging Enabled” 1 = Perform page table translation for every memory access 0 = Do not do translation

The BLITZ page table 31 13 12 frame number unused D R W V 19 bits frame number unused D R W V 19 bits dirty bit referenced bit writable bit valid bit

Indexed by the page number The BLITZ page table page table base register 31 13 12 frame number unused D R W V 1 frame number unused D R W V 2 frame number unused D R W V frame number unused D R W V 2K frame number unused D R W V Indexed by the page number

The BLITZ page table 23 13 12 page number offset virtual address page number offset virtual address page table base register 31 13 12 frame number unused D R W V 1 frame number unused D R W V 2 frame number unused D R W V frame number unused D R W V 2K frame number unused D R W V

The BLITZ page table 23 13 12 page number offset virtual address page number offset virtual address page table base register 31 13 12 frame number unused D R W V 1 frame number unused D R W V 2 frame number unused D R W V frame number unused D R W V 2K frame number unused D R W V 31 physical address

The BLITZ page table 23 13 12 page number offset virtual address page number offset virtual address page table base register 31 13 12 frame number unused D R W V 1 frame number unused D R W V 2 frame number unused D R W V frame number unused D R W V 2K frame number unused D R W V 31 13 12 offset physical address

The BLITZ page table 23 13 12 page number offset page number offset page table base register virtual address 31 13 12 frame number unused D R W V 1 frame number unused D R W V 2 frame number unused D R W V frame number unused D R W V 2K frame number unused D R W V 31 13 12 offset physical address

The BLITZ page table 23 13 12 page number offset page number offset page table base register virtual address 31 13 12 frame number unused D R W V 1 frame number unused D R W V 2 frame number unused D R W V frame number unused D R W V 2K frame number unused D R W V 31 13 12 frame number offset physical address

Page tables When and why do we access a page table? On every instruction to translate virtual to physical addresses?

Page tables When and why do we access a page table? On every instruction to translate virtual to physical addresses? NO! On TLB miss faults to refill the TLB During process creation and destruction When a process allocates or frees memory? …

Translation Lookaside Buffer (TLB) Problem: MMU must go to page table on every memory access!

Translation Lookaside Buffer (TLB) Problem: MMU can’t go to page table on every memory access! Solution: Cache the page table entries in a hardware cache Small number of entries (e.g., 64) Each entry contains Page number Other stuff from page table entry Associatively indexed on page number

Translation lookaside buffer p d CPU page # frame # TLB Hit Physical memory f d TLB Page Table

Hardware operation of TLB Key Page Number Frame Number Other 23 37 unused D R W V 17 50 unused D R W V 92 24 unused D R W V 5 19 unused D R W V 12 6 unused D R W V

Hardware operation of TLB virtual address 23 13 12 page number offset Key Page Number Frame Number Other 23 37 unused D R W V 17 50 unused D R W V 92 24 unused D R W V 5 19 unused D R W V 12 6 unused D R W V 31 13 12 frame number offset physical address

Hardware operation of TLB virtual address 23 13 12 page number offset Key Page Number Frame Number Other 23 37 unused D R W V 17 50 unused D R W V 92 24 unused D R W V 5 19 unused D R W V 12 6 unused D R W V 31 13 12 frame number offset physical address

Hardware operation of TLB virtual address 23 13 12 page number offset Key Page Number Frame Number Other 23 37 unused D R W V 17 50 unused D R W V 92 24 unused D R W V 5 19 unused D R W V 12 6 unused D R W V 31 13 12 frame number offset physical address

Hardware operation of TLB virtual address 23 13 12 page number offset Key Page Number Frame Number Other 23 37 unused D R W V 17 50 unused D R W V 92 24 unused D R W V 5 19 unused D R W V 12 6 unused D R W V 31 13 12 frame number offset physical address

Hardware operation of TLB virtual address 23 13 12 page number offset Key Page Number Frame Number Other 23 37 unused D R W V 17 50 unused D R W V 92 24 unused D R W V 5 19 unused D R W V 12 6 unused D R W V 31 13 12 frame number offset physical address

Software operation of TLB What if the entry is not in the TLB? Go to page table Find the right entry Move it into the TLB Which entry to replace?

Software operation of TLB Hardware TLB refill Page tables in specific location and format TLB hardware handles its own misses Software refill Hardware generates trap (TLB miss fault) Lets the OS deal with the problem Page tables become entirely a OS data structure!

Software operation of TLB What should we do with the TLB on a context switch? How can we prevent the next process from using the last process’s address mappings? Option 1: empty the TLB New process will generate faults until its pulls enough of its own entries into the TLB Option 2: just clear the “Valid Bit” Option 3: the hardware maintains a process id tag on each TLB entry Hardware compares this to a process id held in a specific register … on every translation

Page tables Do we access a page table when a process allocates or frees memory?

Page tables Do we access a page table when a process allocates or frees memory? Not necessarily Library routines (malloc) can service small requests from a pool of free memory already allocated within a process address space When these routines run out of space a new page must be allocated and its entry inserted into the page table This allocation is requested using a system call

Page tables We also access a page table during swapping/paging to disk We know the page frame we want to clear We need to find the right place on disk to store this process memory Hence, page table needs to be searchable using physical addresses Fastest approach, index page table using page frame numbers

Page tables In a well provisioned system, TLB miss faults will be the most frequently occurring event TLB miss fault Given a virtual page number we must find the right page table entry Fastest approach – index the page table using virtual page numbers …hmm, isn’t that the opposite of what we said last side?

Page table design issues Page table size depends on Page size Virtual address length Memory used for page tables is overhead! How can we save space? … and still find entries quickly? Three options Single-level page tables Multi-level page tables Inverted page tables

Single-level page tables A Virtual Address: 21-bits 11-bits page number offset frames in memory • Single-level page table

Single-level page tables 21-bits 11-bits page number offset frames in memory • Single-level page table

Single-level page tables 21-bits 11-bits page number offset frames in memory • Single-level page table Problem: requires one page table entry per virtual page!

Single-level page tables 21-bits 11-bits page number offset frames in memory • Single-level page table 32 bit addresses and 2KB pages means 221 page table entries per process

Multi-level page tables frames in memory • Top-level Page table 2nd-level tables

Multi-level page tables A Virtual Address: 10-bits 10-bits 12-bits PT1 PT2 offset frames in memory • Top-level Page table 2nd-level tables

Multi-level page tables A Virtual Address: 10-bits 10-bits 12-bits PT1 PT2 offset frames in memory • Top-level Page table 2nd-level tables

Multi-level page tables A Virtual Address: 10-bits 10-bits 12-bits PT1 PT2 offset frames in memory • Top-level Page table 2nd-level tables

Multi-level page tables A Virtual Address: 10-bits 10-bits 12-bits PT1 PT2 offset frames in memory • Top-level Page table 2nd-level tables

Multi-level page tables A Virtual Address: 10-bits 10-bits 12-bits PT1 PT2 offset frames in memory • Top-level Page table 2nd-level tables

Multi-level page tables A Virtual Address: 10-bits 10-bits 12-bits PT1 PT2 offset frames in memory • Top-level Page table 2nd-level tables

Multi-level page tables Ok, but how exactly does this save space?

Multi-level page tables Ok, but how exactly does this save space? Not all pages within a virtual address space are allocated Not only do they not have a page frame, but that range of virtual addresses is not being used So no need to maintain complete information about it Some intermediate page tables are empty and not needed We could also page the page table This saves space but slows access … a lot!

Other uses of a page table Swapping out a page to disk OS starts with frame number, so how can we look up which page is in this frame Another data structure could record contents of memory Write entry information to a separate per-process data structure (ie, an address map) Records location of page on disk, plus protection information, etc Allocating a page frame to a process OS starts with frame number and must modify the page table to insert a new mapping Allocating a new page to a process OS starts with a page number and must modify information about the process address space

Quiz … and VM puzzle What is the difference between a virtual and a physical address? What is address binding? Why are programs not usually written using physical addresses? Why is hardware support required for dynamic address translation? What is a page table used for? What is a TLB used for? How many address bits are used for the page offset in a system with 2KB page size?