Recap: Memory Management

Similar documents
CS 153 Design of Operating Systems Winter 2016

CSE 120 Principles of Operating Systems

CSE 120 Principles of Operating Systems Spring 2017

CS 318 Principles of Operating Systems

Paging. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Virtual Memory I. Jo, Heeseung

Paging. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

CS 153 Design of Operating Systems Winter 2016

VIRTUAL MEMORY. Operating Systems 2015 Spring by Euiseong Seo

Address Translation. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Recall: Address Space Map. 13: Memory Management. Let s be reasonable. Processes Address Space. Send it to disk. Freeing up System Memory

VIRTUAL MEMORY II. Jo, Heeseung

CSE 451: Operating Systems Winter Page Table Management, TLBs and Other Pragmatics. Gary Kimura

Address spaces and memory management

Paging. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS153: Memory Management 1

Memory Management Topics. CS 537 Lecture 11 Memory. Virtualizing Resources

Lecture 12: Demand Paging

Opera&ng Systems ECE344

Paging. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Memory Management. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Lecture 8: Memory Management

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

Lecture 21: Virtual Memory. Spring 2018 Jason Tang

Virtual Memory. Patterson & Hennessey Chapter 5 ELEC 5200/6200 1

CIS Operating Systems Memory Management Cache. Professor Qiang Zeng Fall 2017

Operating Systems. Memory Management. Lecture 9 Michael O Boyle

CIS Operating Systems Memory Management Cache and Demand Paging. Professor Qiang Zeng Spring 2018

MEMORY MANAGEMENT/1 CS 409, FALL 2013

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

Virtual Memory 1. Virtual Memory

Virtual Memory 1. Virtual Memory

The memory of a program. Paging and Virtual Memory. Swapping. Paging. Physical to logical address mapping. Memory management: Review

CS5460: Operating Systems Lecture 14: Memory Management (Chapter 8)

Multi-level Translation. CS 537 Lecture 9 Paging. Example two-level page table. Multi-level Translation Analysis

Memory Management. Goals of Memory Management. Mechanism. Policies

Memory Management. To improve CPU utilization in a multiprogramming environment we need multiple programs in main memory at the same time.

CSE 120. Translation Lookaside Buffer (TLB) Implemented in Hardware. July 18, Day 5 Memory. Instructor: Neil Rhodes. Software TLB Management

Memory Allocation. Copyright : University of Illinois CS 241 Staff 1

Virtual Memory: From Address Translation to Demand Paging

16 Sharing Main Memory Segmentation and Paging

Basic Memory Management

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

CIS Operating Systems Memory Management Cache. Professor Qiang Zeng Fall 2015

Caching and Demand-Paged Virtual Memory

CS399 New Beginnings. Jonathan Walpole

ECE 7650 Scalable and Secure Internet Services and Architecture ---- A Systems Perspective. Part I: Operating system overview: Memory Management

Virtual Memory: From Address Translation to Demand Paging

CPS 104 Computer Organization and Programming Lecture 20: Virtual Memory

Basic Memory Management. Basic Memory Management. Address Binding. Running a user program. Operating Systems 10/14/2018 CSC 256/456 1

How to create a process? What does process look like?

CSE 153 Design of Operating Systems

Virtual Memory Virtual memory first used to relive programmers from the burden of managing overlays.

15 Sharing Main Memory Segmentation and Paging

CPS104 Computer Organization and Programming Lecture 16: Virtual Memory. Robert Wagner

The Virtual Memory Abstraction. Memory Management. Address spaces: Physical and Virtual. Address Translation

Multi-Process Systems: Memory (2) Memory & paging structures: free frames. Memory & paging structures. Physical memory

Goals of memory management

Operating Systems. Operating Systems Sina Meraji U of T

CHAPTER 4 MEMORY HIERARCHIES TYPICAL MEMORY HIERARCHY TYPICAL MEMORY HIERARCHY: THE PYRAMID CACHE PERFORMANCE MEMORY HIERARCHIES CACHE DESIGN

Main Memory: Address Translation

CIS Operating Systems Memory Management Address Translation for Paging. Professor Qiang Zeng Spring 2018

Another View of the Memory Hierarchy. Lecture #25 Virtual Memory I Memory Hierarchy Requirements. Memory Hierarchy Requirements

ECE331: Hardware Organization and Design

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2015 Lecture 23

Chapter 8. Virtual Memory

Lecture 14: Cache & Virtual Memory

CS252 S05. Main memory management. Memory hardware. The scale of things. Memory hardware (cont.) Bottleneck

Operating Systems. Paging... Memory Management 2 Overview. Lecture 6 Memory management 2. Paging (contd.)

Chapter 9 Memory Management Main Memory Operating system concepts. Sixth Edition. Silberschatz, Galvin, and Gagne 8.1

Virtual Memory. control structures and hardware support

Memory Management - Demand Paging and Multi-level Page Tables

Memory Management. Dr. Yingwu Zhu

Memory Hierarchy Requirements. Three Advantages of Virtual Memory

Chapter 9 Memory Management

Virtual Memory. Lecture for CPSC 5155 Edward Bosworth, Ph.D. Computer Science Department Columbus State University

Virtual Memory. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

a process may be swapped in and out of main memory such that it occupies different regions

CSC 369 Operating Systems

Chapter 8 Memory Management

Memory Management. CSCI 315 Operating Systems Design Department of Computer Science

Topics: Memory Management (SGG, Chapter 08) 8.1, 8.2, 8.3, 8.5, 8.6 CS 3733 Operating Systems

CS 31: Intro to Systems Virtual Memory. Kevin Webb Swarthmore College November 15, 2018

Virtual Memory 1. To do. q Segmentation q Paging q A hybrid system

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 23

Memory Management Virtual Memory

Virtual Memory III. Jo, Heeseung

CPE300: Digital System Architecture and Design

Computer Systems. Virtual Memory. Han, Hwansoo

Virtual to physical address translation

Virtual Memory. Virtual Memory

Memory Management Cache Base and Limit Registers base limit Binding of Instructions and Data to Memory Compile time absolute code Load time

6 - Main Memory EECE 315 (101) ECE UBC 2013 W2

Page Tables. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Carnegie Mellon. 16 th Lecture, Mar. 20, Instructors: Todd C. Mowry & Anthony Rowe

Memory Management. Memory Management

Simple idea 1: load-time linking. Our main questions. Some terminology. Simple idea 2: base + bound register. Protection mechanics.

CS 3733 Operating Systems:

Outline. V Computer Systems Organization II (Honors) (Introductory Operating Systems) Advantages of Multi-level Page Tables

Chapter 8: Main Memory

Transcription:

, 4/13/2018 EE445M/EE360L.12 Embedded and Real-Time Systems/ Real-Time Operating Systems : Memory Protection, Virtual Memory, Paging References: T. Anderson, M. Dahlin, Operating Systems: Principles and Practice R. & A. Arpaci-Dusseau, Operating Systems: Three Easy Pieces, http://ostep.org 1 Recap: Memory Management Sharing Per-thread: stack Per-program/-process: heap, code, data Allocation Static, permanent Globals, OS code Dynamic, temporary, heap, process swapping Protection Access control Main Memory Process2 Process1 OS Disc Process Thread1Process R0 Thread1 R1R0 R2R1... R2 SP... PCSP PC Thread2 R0 Thread2 R1R0 R2R1... R2 SP... PCSP PC Heap Heap Data Data Code Code 2 1

, 4/13/2018 Recap: Processes OS manages processes CPU scheduling Code/data memory Independent programs Separately compiled Virtual address space Brought in/out of memory On load/exit, swapped in/out by OS Address translation max 0x0 Logical Process Heap Data Code Physical Memory Process2 Process1 OS 0xF..FF 0x0 3 Memory Protection Divide memory into regions Allocated heap blocks Thread/process data & code segments Define access control per region Read-only/read-write Execute/no-execute Enforce access control in hardware On every memory access (load/store) Permission fault exception 4 2

, 4/13/2018 TM4C123 Memory Protection Memory Protection Unit (MPU) 8 separate memory regions 5 Memory Region Attributes 6 3

, 4/13/2018 Access Privileges Memory management fault on violation Can be caught by OS in an interrupt handler 7 Thread-Protected Mode Only current thread has memory access Code Data/heap, stack OS kernel traps On context switch Re-assign MPU permissions Extra overhead Thread1 R0 R1 R2... SP PC Thread2 R0 R1 R2... SP PC Heap Data Code OS kernel MPU Read/Write, No execute Execute, read-only Privileged only 8 4

, 4/13/2018 μcos Thread Groups Group of threads protected jointly Called process in μcos-ii Group-local shared memory region Inter-group communication Thread1 R0 R1 R2... SP PC Thread2 R0 R1 R2... SP PC Through OS kernel Special shared memory areas Heap Data Code OS kernel MPU Read/Write Read-only Privileged only 9 Multi-Processing Process Whole program in execution Code, data, heap One or more threads Multi-processing Multiple processes/programs in main memory OS schedules processes & threads Process-level protection Thread1 R0 R1 R2... SP PC Thread2 R0 R1 R2... SP PC Process Heap Data Code OS kernel MPU 10 5

, 4/13/2018 Memory Management Unit (MMU) Virtual addresses Physical addresses Processor MMU Physical memory Fast & efficient address translation Hardware supported, at run-time Start with old, simple ways Progress to current techniques 11 Fixed Partitions Base Register P4 s Base P1 P2 Virtual Address Offset + P3 P4 P5 Allocation? Fragmentation? Overhead? 12 6

, 4/13/2018 Variable Partitions Base Register P3 s Base Limit Register P3 s Limit P1 P2 Virtual Address Offset < Yes? + P3 No? Protection Fault Allocation? Fragmentation? Overhead? 13 Segmentation Partition memory into logically related units Module, procedure, stack, data, file, etc. Virtual addresses become <segment #, offset> Units of memory from programmer s perspective Natural extension of variable-sized partitions Variable-sized partitions = 1 segment/process Segmentation = many segments/process Hardware support Multiple base/limit pairs, one per segment (segment table) Segments named by #, used to index into table 14 7

, 4/13/2018 Segmented Address Space 0xFFFF Process (Virtual Addresses) Base 3 Heap Heap Base 2 --rw-base 0x2000 ZI section RW section Second load region Base 1 ZI section RW section Static Data (Data Segment) --entry --ro-base 0x0000 RO section ELF Load view First load region Base 0 RO section Code (Text Segment) ELF Execution view 15 Segment Lookups Segment Table limit base Segment # Offset Virtual Address < Yes? + No? Protection Fault Allocation? Fragmentation? Overhead? 16 8

, 4/13/2018 Paging Paging solves the external fragmentation problem by using fixed sized units in both physical and virtual memory Virtual Memory Page 0 Page 1 Page 2 Page N-1 Allocation? Fragmentation? Overhead? 17 Virtual Memory Programmers (and processes) view memory as one contiguous address space From 0 through N Virtual address space (VAS) In reality, pages are scattered throughout physical storage The mapping is invisible to the program Protection is provided because a program cannot reference memory outside of its VAS The address 0x1000 maps to different physical addresses in different processes 18 9

, 4/13/2018 Demand Paging / Swapping Pages can be moved between memory and disk Use disk to provide more virtual than physical memory OS uses main memory as a page cache of all the data allocated by processes in the system Initially, pages are allocated from memory When memory fills up, allocating a page in memory requires some other page to be evicted from memory Why physical memory pages are called frames Evicted pages go to disk Where? The swap file/backing store The movement of pages between memory and disk is done by the OS, and is transparent to the application But: expensive! 19 Page Lookups 20-bit address, 4K pages Offset is 12 bits Virtual Address Page number Offset 0x7 468 Page Table Page frame Physical Address Page frame Offset 0x2 468 20 10

, 4/13/2018 Page Tables Page tables completely define the mapping between virtual pages and physical pages for an address space Each process has address space & page table Page tables are data structures maintained in the OS Accessible by MMU for hardware translation Page Table Entry (PTE) Page Table 3 0 Virtual Memory Page 0 Page 1 3 0 2 Page 2 2 Page N-1 Page N-1 N-1 21 Page Table Size How big does the page table need to be? 20-bit address 12 bit page offset (4 kibibyte page size) 8 bit page number 256 pages, each entry is 12 bits 32-bit address 14 bit page offset (16 kibibyte page size) 18 bit page number 2 18 pages, each entry is 14 bits 22 11

, 4/13/2018 Page Table Entries (PTEs) 1 1 1 2 20 M R V Prot Page Frame Number Page table entries control mapping The Modify bit says whether or not the page has been written It is set when a write to the page occurs The Reference bit says whether the page has been accessed It is set when a read or write to the page occurs The Valid bit says whether or not the PTE can be used It is checked each time the virtual address is used, set when page is in memory The Protection bits say what operations are allowed on page Read, write, execute The page frame number (PFN) determines physical page 23 Segmentation and Paging Can combine segmentation and paging The x86 supports segments and paging Use segments to manage logically related units Module, procedure, stack, file, data, etc. Segments vary in size, but usually large (>1 page) Pages to partition segments into fixed size chunks Segments easier to manage within physical memory Segments become pageable rather than moving segments into and out of memory, just move page portions of segment Need to allocate page table entries only for those pieces of the segments that have themselves been allocated Tends to be complex 24 12

, 4/13/2018 Paging Limitations Can still have internal fragmentation Process may not use memory in multiples of a page Memory reference overhead 2 references per address lookup (page table, then memory) Solution use a hardware cache of lookups (more later) Memory required to hold page table can be significant Need one PTE per page 32 bit address space w/ 4KB pages = 2 20 PTEs 4 bytes/pte = 4MB/page table 25 processes = 100MB just for page tables! How to reduce page size? How do we only map what is being used? Dynamically extending page table, but fragmentation 25 Two-Level Page Tables Virtual Address Master page number Secondary Offset Page table Physical Address Page frame Offset Master Page Table Page frame Secondary Page Table 26 13

, 4/13/2018 Two-Level Page Tables Hierarchical Page Table Virtual Address Space Secondary Page 0 Page 1 Master Not Needed Page 2 Unmapped Page N-1 27 Addressing Page Tables Where do we store page tables? Physical memory Easy to address, no translation required But, allocated tables consume memory for lifetime of VAS Virtual memory (OS virtual address space) Cold (unused) page table pages can be paged out to disk But, addressing page tables requires translation How do we stop recursion? Do not page the outer page table (called wiring) If we re going to page the page tables, might as well page the entire OS address space, too Need to wire special code and data (fault, int handlers) 28 14

, 4/13/2018 Efficient Translations Original page table scheme already doubled the cost of doing memory lookups Lookup into page table + fetch the data Two-level page tables triple the cost! 2x lookups into page tables, a third to fetch the data And this assumes the page table is in memory How can we use paging but also have lookups cost about the same as fetching from memory? Cache translations in hardware Translation Lookaside Buffer (TLB) TLB managed by Memory Management Unit (MMU) 29 Translation Lookaside Buffer (TLB) Cache of PTEs Typical Details: Fully associative Small (Just 32-128 PTEs) Separate Instruction and Data TLBs Two-level (256-512 combined I/D) Full Page Table in Memory CPU TLB (MMU) Main Memory Virtual Addresses Physical Addresses Context switch? 30 15

, 4/13/2018 Memory Access Example Process is executing on CPU, issues a read to an address What kind of address is it? Virtual or physical? The read goes to the TLB in the MMU 1. TLB does a lookup using the page number of the address 2. Common case is that the page number matches, returning a page table entry (PTE) for the mapping for this address 3. TLB validates that the PTE protection allows reads (in this case) 4. PTE specifies which physical frame holds the page 5. MMU combines physical frame and offset into a physical address 6. MMU then reads from that physical address, returns value to CPU Note: This is all done by the hardware 31 TLB Miss If the TLB does not have mapping: 1. MMU loads PTE from page table in memory Hardware managed TLB [x86] OS has already set up the page tables so that the hardware can access it directly, otherwise not involved 2. Trap to the OS Software/OS managed TLB [MIPS, Alpha, Sparc, PowerPC] OS does lookup in page table, loads PTE into TLB OS returns from exception, TLB continues Replace existing PTE in TLB Done in hardware, e.g. least recently used At this point, PTE for the address in the TLB 32 16

, 4/13/2018 Page Fault PTE can indicate a protection fault Read/write/execute operation not permitted Invalid virtual page not allocated/not in memory TLB traps to the OS (OS takes over) R/W/E violation OS sends fault back up to process, or intervenes Invalid Virtual page not allocated in address space OS sends fault to process (e.g., segmentation fault) Page not in physical memory OS allocates frame, reads from disk (swap space) Maps PTE to physical frame, update TLB 33 Page Replacement Which page to evict on invalid page fault? Page replacement policies Avoid thrashing (if possible) Exploit locality Temporal and spatial locality Working set (pages most recently referenced) FIFO, Least Recently Used (LRU), Dirty vs. clean pages (marked in PTE) Only write back dirty pages to disk 34 CSE 120 Lecture 11 Page Replacement 17

, 4/13/2018 Advanced Functionality Shared memory PTEs of two processes point to same page Copy on Write (fork() a process) Copy only page table to clone process Copy memory frame only on first write Mapped files Map pages from file on disk into memory 35 Shared Memory PTEs Point to Same Physical Page Virtual Address Space #1 Virtual Address Space #2 36 18

, 4/13/2018 Copy on Write: Before Fork Parent Virtual Address Space 37 Copy on Write: Fork Parent Virtual Address Space Read-Only Mappings Child Virtual Address Space 38 19

, 4/13/2018 Copy on Write: On A Write Now Read-Write & Private Parent Virtual Address Space Child Virtual Address Space 39 Mapped Files Virtual Address Space Mapped File PTEs initially invalid Read into memory on page fault Write back when dirty on eviction Similar approach used by OS when loading process/program from disk (copy to swap space, then map) Bind file to memory region Linux: mmap() File acts as backup store Instead of swap space 40 20

, 4/13/2018 Memory Management Summary Often not used in embedded devices Overhead Page table storage, Context switching Unpredictable timing TLB misses, Page faults Static memory management Static data allocation, no heap No MMU/paging Compile/load time relocation (optionally segmented) Hardware support for protection & translation Swapping under program control (overlays) 41 21