Chapter 5: Memory Management

Similar documents
Signal types (some of them) Networks and Operating Systems ( ) Chapter 5: Memory Management. Where do signals come from?

ADRIAN PERRIG & TORSTEN HOEFLER Networks and Operating Systems ( ) Chapter 6: Demand Paging

Chapter 6: Demand Paging

8.1 Background. Part Four - Memory Management. Chapter 8: Memory-Management Management Strategies. Chapter 8: Memory Management

Memory Management. Memory Management

Chapter 9: Memory Management. Background

Chapter 8: Main Memory

Chapter 8: Memory Management

Chapter 8: Main Memory

Chapter 8 Memory Management

Pentium/Linux Memory System March 17, 2005

Chapter 4: Synchronization

Chapter 8: Memory Management Strategies

Memory System Case Studies Oct. 13, 2008

Chapter 7: Main Memory. Operating System Concepts Essentials 8 th Edition

Chapter 8: Memory-Management Strategies

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 24

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

Chapter 8: Memory- Manage g me m nt n S tra r t a e t gie i s

Part Three - Memory Management. Chapter 8: Memory-Management Strategies

I.-C. Lin, Assistant Professor. Textbook: Operating System Principles 7ed CHAPTER 8: MEMORY

I.-C. Lin, Assistant Professor. Textbook: Operating System Concepts 8ed CHAPTER 8: MEMORY

CHAPTER 8 - MEMORY MANAGEMENT STRATEGIES

CS 3733 Operating Systems:

File Systems. OS Overview I/O. Swap. Management. Operations CPU. Hard Drive. Management. Memory. Hard Drive. CSI3131 Topics. Structure.

P6/Linux Memory System Nov 11, 2009"

Administrivia. - If you didn t get Second test of class mailing list, contact cs240c-staff. Clarification on double counting policy

Goals of Memory Management

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

Memory management. Last modified: Adaptation of Silberschatz, Galvin, Gagne slides for the textbook Applied Operating Systems Concepts

Memory Management. Memory

virtual memory. March 23, Levels in Memory Hierarchy. DRAM vs. SRAM as a Cache. Page 1. Motivation #1: DRAM a Cache for Disk

CS307: Operating Systems

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2015 Lecture 23

CS3600 SYSTEMS AND NETWORKS

Module 8: Memory Management

Systems Programming and Computer Architecture ( ) Timothy Roscoe

Memory Management. Contents: Memory Management. How to generate code? Background

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

Processes and Virtual Memory Concepts

Chapter 8: Main Memory. Operating System Concepts 9 th Edition

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

Chapter 8: Memory- Management Strategies. Operating System Concepts 9 th Edition

Logical versus Physical Address Space

Chapter 8: Main Memory

Virtual memory - Paging

The process. one problem

Last time: Scheduling. Goals today. Recap: Hardware support for synchronization. Disabling interrupts. Disabling interrupts

CS307 Operating Systems Main Memory

Module 9: Memory Management. Background. Binding of Instructions and Data to Memory

CHAPTER 8: MEMORY MANAGEMENT. By I-Chen Lin Textbook: Operating System Concepts 9th Ed.

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

Lecture 8 Memory Management Strategies (chapter 8)

Chapter 8 Main Memory

Example: Linux o(1) scheduler. Networks and Operating Systems ( ) Chapter 4: Synchronization. Problems with UNIX Scheduling

Chapter 9 Memory Management

Multiprogramming on physical memory

Chapter 8: Main Memory

Main Memory. CISC3595, Spring 2015 X. Zhang Fordham University

Learning Outcomes. An understanding of page-based virtual memory in depth. Including the R3000 s support for virtual memory.

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

Address Translation. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Learning Outcomes. An understanding of page-based virtual memory in depth. Including the R3000 s support for virtual memory.

Virtual Memory II CSE 351 Spring

Virtual Memory. Samira Khan Apr 27, 2017

Chapter 8: Memory- Management Strategies. Operating System Concepts 9 th Edition

Chapter 8: Memory- Management Strategies

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 23

CS 537: Introduction to Operating Systems Fall 2015: Midterm Exam #1

Chapter 8 & Chapter 9 Main Memory & Virtual Memory

Recap: Memory Management

Computer Systems. Virtual Memory. Han, Hwansoo

CSE 153 Design of Operating Systems

198:231 Intro to Computer Organization. 198:231 Introduction to Computer Organization Lecture 14

Virtual memory Paging

14 May 2012 Virtual Memory. Definition: A process is an instance of a running program

A Few Problems with Physical Addressing. Virtual Memory Process Abstraction, Part 2: Private Address Space

Networks and Opera/ng Systems Chapter 14: Synchroniza/on

Administrivia. Lab 1 due Friday 12pm. We give will give short extensions to groups that run into trouble. But us:

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

Module 8: Memory Management

Virtual Memory Oct. 29, 2002

Background Swapping Contiguous Memory Allocation Paging Structure of the Page Table Segmentation

Virtual Memory II. CSE 351 Autumn Instructor: Justin Hsia

PROCESS VIRTUAL MEMORY PART 2. CS124 Operating Systems Winter , Lecture 19

Memory Management. An expensive way to run multiple processes: Swapping. CPSC 410/611 : Operating Systems. Memory Management: Paging / Segmentation 1

Virtual Memory. Today. Handling bigger address spaces Speeding translation

VIRTUAL MEMORY II. Jo, Heeseung

CS 537: Introduction to Operating Systems Fall 2015: Midterm Exam #1

Chapter 8. Virtual Memory

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

Chapter 8: Memory Management. Operating System Concepts with Java 8 th Edition

Virtual Memory. CS61, Lecture 15. Prof. Stephen Chong October 20, 2011

PROCESS VIRTUAL MEMORY. CS124 Operating Systems Winter , Lecture 18

Address Translation. Tore Larsen Material developed by: Kai Li, Princeton University

Outlook. Background Swapping Contiguous Memory Allocation Paging Structure of the Page Table Segmentation Example: The Intel Pentium

Main Points. Address Transla+on Concept. Flexible Address Transla+on. Efficient Address Transla+on

Virtual Memory II. CSE 351 Autumn Instructor: Justin Hsia

! What is main memory? ! What is static and dynamic allocation? ! What is segmentation? Maria Hybinette, UGA. High Address (0x7fffffff) !

Main Memory: Address Translation

Transcription:

ADRIAN PERRIG & TORSTEN HOEFLER ( 252-0062-00 ) Networks and Operating Systems Chapter 5: Memory Management http://support.apple.com/kb/ht562 Description: The ios kernel has checks to validate that the user-mode pointer and length passed to the copyin and copyout functions would not result in a user-mode process being able to directly access kernel memory. The checks were not being used if the length was smaller than one page. This issue was addressed through additional validation of the arguments to copyin and copyout. Oldskool: signal() void (*signal(int sig, void (*handler)(int))) (int); Unpacking this: A handler looks like void my_handler(int); Signal takes two arguments An integer (the signal type, e.g., SIGPIPE) A pointer to a handler function and returns a pointer to a handler function The previous handler, Special handler arguments: SIG_IGN (ignore), SIG_DFL (default), SIG_ERR (error code) 2 1

Unix signal handlers Signal handler can be called at any time! Executes on the current user stack If process is in kernel, may need to retry current system call Can also be set to run on a different (alternate) stack User process is in undefined state when signal delivered Implications There is very little you can safely do in a signal handler! Can t safely access program global or static variables Some system calls are re-entrant, and can be called Many C library calls cannot (including _r variants!) Can sometimes execute a longjmp if you are careful With signal, cannot safely change signal handlers What happens if another signal arrives? 2

Multiple signals If multiple signals of the same type are to be delivered, Unix will discard all but one. If signals of different types are to be delivered, Unix will deliver them in any order. Serious concurrency problem: How to make sense of this? 5 A better signal()posix sigaction() #include <signal.h> int sigaction(int signo, const struct sigaction *act, struct sigaction *oldact); Signal handler struct sigaction { void (*sa_handler)(int); sigset_t sa_mask; int sa_flags; }; New action for signal signo Signals to be blocked in this handler (cf., fd_set) void (*sa_sigaction)(int, siginfo_t *, void *); More sophisticated signal handler (depending on flags) Previous action is returned 6

Signals as upcalls Particularly specialized (and complex) form of Upcall Kernel RPC to user process Other OSes use upcalls much more heavily Including Barrelfish Scheduler Activations : dispatch every process using an upcall instead of return Very important structuring concept for systems! 7 Our Small Quiz True or false (raise hand) Mutual exclusion on a multicore can be achieved by disabling interrupts Test and set can be used to achieve mutual exclusion Test and set is more powerful than compare and swap The CPU retries load-linked/store conditional instructions after a conflict The best spinning time is 2x the context switch time Priority inheritance can prevent priority inversion The receiver never blocks in asynchronous IPC The sender blocks in synchronous IPC if the receiver is not ready A pipe file descriptor can be sent to a different process Pipes do not guarantee ordering Named pipes in Unix behave like files A process can catch all signals with handlers Signals always trigger actions at the signaled process One can implement a user-level tasking library using signals Signals of the same type are buffered in the kernel 8

Goals of Memory Management Allocate physical memory to applications Protect an application s memory from others Allow applications to share areas of memory Data, code, etc. Create illusion of a whole address space Virtualization of the physical address space Create illusion of more memory than you have Tomorrow demand paging Book: OSPP Chapter 8 9 In CASP last semester we saw: Assorted uses for virtual memory x86 paging Page table format Translation process Translation lookaside buffers (TLBs) Interaction with caches Performance implications For application code, e.g., matrix multiply 10 5

What s new this semester? Wider range of memory management hardware Base/limit, segmentation Inverted page tables, etc. How the OS uses the hardware Demand paging and swapping Page replacement algorithms Frame allocation policies 11 Terminology Physical address: address as seen by the memory unit Virtual or Logical address: address issued by the processor Loads Stores Instruction fetches Possible others (e.g., TLB fills) 12 6

Memory management 1. Allocating physical addresses to applications 2. Managing the name translation of virtual addresses to physical addresses. Performing access control on memory access Functions 2 & usually involve the hardware Memory Management Unit (MMU) 1 Simple scheme: partitioned memory 1 7

Base and Limit Registers A pair of base and limit registers define the logical address space 0x0000000 0x1000000 0x5600ba0 0x8ff0010 Operating System Process Process Process 0x5600ba0 base 0x9ef70 limit 0xB000000 0xfffffff 15 Issue: address binding Base address isn t known until load time Options: 1. Compiled code must be completely position-independent, or 2. Relocation Register maps compiled addresses dynamically to physical addresses 16 8

Dynamic relocation using a relocation register Relocation register CPU Logical address 6 1000 + Physical address 16 Memory MMU 17 Contiguous Allocation Main memory usually into two partitions: Resident OS, usually in low memory with interrupt vector User processes in high memory Relocation registers protect user processes from 1. each other 2. changing operating-system code and data Registers: Base register contains value of smallest physical address Limit register contains range of logical addresses each logical address must be less than the limit register MMU maps logical address dynamically 18 9

Hardware Support for Relocation and Limit Registers Limit register Relocation register CPU Logical address yes Physical address < + Memory no trap: addressing error 19 Base & Limit summary Simple to implement (addition & compare) Physical memory fragmentation Only a single contiguous address range How to share data between applications? How to share program text (code)? How to load code dynamically? Total logical address space physical memory 20 10

Segmentation 21 Segmentation Generalize base + limit: Physical memory divided into segments Logical address = (segment id, offset) Segment identifier supplied by: Explicit instruction reference Explicit processor segment register Implicit instruction or process state 22 11

User s View of a Program shared library stack symbol table heap main program logical address 2 Segmentation Reality 1 2 1 2 0 0 logical address physical memory 2 12

Segmentation Hardware limit base CPU s d segment table yes < + Physical address Memory no trap: addressing error 25 Segmentation Reality 1 2 0 1 2 limit 00 1000 00 00 1000 base 1500 5000 00 600 1800 6000 5000 600 800 00 1 2 0 segment table 2800 1800 1500 0 logical address physical memory 26 1

Segmentation Architecture Segment table each entry has: base starting physical address of segment limit length of the segment Segment-table base register (STBR) Current segment table location in memory Segment-table length register (STLR) Current size of segment table segment number s is legal if s < STLR 27 Segmentation Summary Fast context switch Simply reload STBR/STLR Fast translation 2 loads, 2 compares Segment table can be cached Segments can easily be shared Segments can appear in multiple segment tables Physical layout must still be contiguous (External) fragmentation still a problem 28 1

Paging 29 Paging Solves contiguous physical memory problem Process can always fit if there is available free memory Divide physical memory into frames Size is power of two, e.g., 096 bytes Divide logical memory into pages of the same size For a program of n pages in size: Find and allocate n frames Load program Set up page table to translate logical pages to physical frames 0 15

Page table jargon Page tables maps VPNs to PFNs Page table entry = PTE VPN = Virtual Page Number Upper bits of virtual or logical address PFN = Page Frame Number Upper bits of physical or logical address Same number of bits (usually). 1 Recall: P6 Page tables (2bit) Logical address: 20 VPN 12 VPO VPN1 VPN2 20 12 PPN PPO PDE p=1 PTE p=1 data PDBR Page directory Page table Data page Pages, page directories, page tables all kb 2 16

x86-6 Paging 9 VPN1 9 9 9 VPN2 VPN VPN 12 VPO Virtual address Page Map Table Page Directory Pointer Table Page Directory Table Page Table PMLE PDPE PDE PTE BR 0 PPN 12 PPO Physical address Problem: performance Every logical memory access needs more than two physical memory accesses Load page table entry PFN Load desired location Performance half as fast as with no translation Solution: cache page table entries 17

Translating with the P6 TLB CPU 1. Partition VPN into TLBT and TLBI. 20 12 virtual address VPN VPO TLB miss 16 TLBT TLBI par-al TLB hit 1 PDE... page table translaoon 2 PTE TLB hit 20 12 PPN PPO physical address 2. Is the PTE for VPN cached in set TLBI?. Yes: Check permissions, build physical address. No: Read PTE (and PDE if not cached) from memory and build physical address 5 In fact, x86 combines segmentation and paging Segments do (still) have uses Thread-local state Sandboxing (Google NativeClient, etc.) Virtual machine monitors (Xen, etc.) CPU logical address segmentation unit linear address paging unit physical address physical memory 6 18

Effective Access Time Associative Lookup = ε time units Assume memory cycle time is 1 time unit Hit ratio α = % time that a page number is found in the TLB; Depends on locality and TLB entries (coverage) Then Effective Access Time: EAT = (1 + ε) α + (2 + ε)(1 α) = 2 + ε α Assuming singlelevel page table. Exercise: work this out for the P6 2-level table 7 Page Protection 8 19

Memory Protection Associate protection info with each frame Actually no - with the PTE. Valid-invalid bit valid page mapping is legal invalid page is not part of address space, i.e., entry does not exist Requesting an invalid address fault A page fault, or. 9 Remember the P6 Page Table Entry (PTE) 1 12 11 9 8 7 6 5 2 1 0 Page physical base address Avail G 0 D A CD WT U/S R/W P=1 Page base address: 20 most significant bits of physical page address (forces pages to be KB aligned) Avail: available for system programmers G: global page (don t evict from TLB on task switch) D: dirty (set by MMU on writes) A: accessed (set by MMU on reads and writes) CD: cache disabled or enabled WT: write through or write back cache policy for this page U/S: user/supervisor R/W: read/write P: page is present in physical memory (1) or not (0) 0 20

P6 protection bits 1 12 11 9 8 7 6 5 2 1 0 Page physical base address Avail G 0 D A CD WT U/S R/W P=1 Page base address: 20 most significant bits of physical page address (forces pages to be KB aligned) Avail: available for system programmers G: global page (don t evict from TLB on task switch) D: dirty (set by MMU on writes) A: accessed (set by MMU on reads and writes) CD: cache disabled or enabled WT: write through or write back cache policy for this page U/S: user/supervisor R/W: read/write P: page is present in physical memory (1) or not (0) P bit can be used to trap on any access (read or write) 1 Protection information Protection information typically includes: Readable Writeable Executable (can fetch to i-cache) Reference bits used for demand paging Observe: same attributes can be (and are) associated with segments as well 2 21

Page sharing Shared Pages Example text 1 text 2 text data 1 Process P 1 6 1 page table for P 1 0 1 2 5 6 7 8 9 10 11 data 1 code 1 code 2 code 22

Shared Pages Example text 1 text 2 text data 1 Process P 1 6 1 page table for P 1 text 1 text 2 text data 2 Process P 2 6 7 page table for P 2 0 1 2 5 6 7 8 data 1 code 1 code 2 code data 2 9 10 11 5 Shared Pages Example text 1 text 2 text data 1 Process P 1 text 1 text 2 text data Process P 6 1 page table for P 1 6 2 page table for P text 1 text 2 text data 2 Process P 2 6 7 page table for P 2 0 1 2 5 6 7 8 9 10 11 data 1 data code 1 code 2 code data 2 6 2

Shared Pages Shared code One copy of read-only code shared among processes Shared code appears in same location in the logical address space of all processes Data segment is not shared, different for each process But still mapped at same address (so code can find it) Private code and data Allows code to be relocated anywhere in address space 7 Per-process protection Protection bits are stored in page table Plenty of bits available in PTEs independent of frames themselves Different processes can share pages Each page can have different protection to different processes Many uses! E.g., debugging, communication, copy-on-write, etc. 8 2

Page Table Structures 9 Page table structures Problem: simple linear page table is too big Solutions: 1. Hierarchical page tables 2. Virtual memory page tables. Hashed page tables. Inverted page tables 50 25

Page table structures Problem: simple linear page table is too big Solutions: 1. Hierarchical page tables 2. Virtual memory page tables (VAX). Hashed page tables. Inverted page tables Saw these last Semester. 51 # Hashed Page Tables VPN is hashed into table Hash bucket has chain of logical->physical page mappings Hash chain is traversed to find match. Can be fast, but can be unpredicable Often used for Portability Software-loaded TLBs (e.g., MIPS) 52 26

Hashed Page Table logical address p d r d physical address hash function q s p r physical memory hash table 5 # Inverted Page Table One system-wide table now maps PFN -> VPN One entry for each real page of memory Contains VPN, and which process owns the page Bounds total size of all page information on machine Hashing used to locate an entry efficiently Examples: PowerPC, ia6, UltraSPARC 5 27

Inverted Page Table Architecture CPU logical address pid p d i d physical address Physical memory search i pid p page table 55 The need for more bookkeeping Most OSes keep their own translation info Per-process hierarchical page table (Linux) System wide inverted page table (Mach, MacOS) Why? Portability Tracking memory objects Software virtual physical translation Physical virtual translation 56 28

TLB shootdown 57 TLB management Recall: the TLB is a cache. Machines have many MMUs on many cores many TLBs Problem: TLBs should be coherent. Why? Security problem if mappings change E.g., when memory is reused 58 29

TLB management Process ID VPN PPN acces s Core 1 TLB: Core 2 TLB: Core TLB: 0 0x005 0x0 r/w 1 0x20f8 0x12 r/w 0 0x005 0x0 r/w 1 0x0001 0x05 read 0 0x20f8 0x12 r/w 1 0x0001 0x05 read 59 TLB management Core 1 TLB: Core 2 TLB: Core TLB: Process ID VPN PPN acces s 0 0x005 0x0 r/w 1 0x20f8 0x12 r/w 0 0x005 0x0 r/w 1 0x0001 0x05 read 0 0x20f8 0x12 r/w 1 0x0001 0x05 read Change to read only 60 0

TLB management Core 1 TLB: Core 2 TLB: Core TLB: Process ID VPN PPN acces s 0 0x005 0x0 r/w 1 0x20f8 0x12 r/w 0 0x005 0x0 r/w 1 0x0001 0x05 read 0 0x20f8 0x12 r/w 1 0x0001 0x05 read Change to read only 61 TLB management Core 1 TLB: Core 2 TLB: Core TLB: Process ID VPN PPN acces s 0 0x005 0x0 r/w 1 0x20f8 0x12 r/w 0 0x005 0x0 r/w 1 0x0001 0x05 read 0 0x20f8 0x12 r/w 1 0x0001 0x05 read Change to read only Process 0 on core 1 can only continue once shootdown is complete! 62 1

Keeping TLBs consistent 1. Hardware TLB coherence Integrate TLB mgmt with cache coherence Invalidate TLB entry when PTE memory changes Rarely implemented 2. Virtual caches Required cache flush / invalidate will take care of the TLB High context switch cost! Most processors use physical caches 5. Software TLB shootdown Most common OS on one core notifies all other cores - Typically an IPI Each core provides local invalidation 6. Hardware shootdown instructions Broadcast special address access on the bus Interpreted as TLB shootdown rather than cache coherence message E.g., PowerPC architecture 6 Tomorrow: demand paging 6 2