W4118: segmentation and paging. Instructor: Junfeng Yang



Similar documents
W4118 Operating Systems. Instructor: Junfeng Yang

Memory Management Outline. Background Swapping Contiguous Memory Allocation Paging Segmentation Segmented Paging

COS 318: Operating Systems. Virtual Memory and Address Translation

CS 61C: Great Ideas in Computer Architecture Virtual Memory Cont.

Lecture 17: Virtual Memory II. Goals of virtual memory

Virtual vs Physical Addresses

CS5460: Operating Systems

Memory management basics (1) Requirements (1) Objectives. Operating Systems Part of E1.9 - Principles of Computers and Software Engineering

How To Write A Page Table

Virtual Memory. Virtual Memory. Paging. CSE 380 Computer Operating Systems. Paging (1)

Memories Are Made of This

Sistemi Operativi. Lezione 25: JOS processes (ENVS) Corso: Sistemi Operativi Danilo Bruschi A.A. 2015/2016

POSIX. RTOSes Part I. POSIX Versions. POSIX Versions (2)

Chapter 2. Processes. Address Spaces

This Unit: Virtual Memory. CIS 501 Computer Architecture. Readings. A Computer System: Hardware

Sistemi Operativi. Lez. 21/22 : Memoria Virtuale in JOS

The Deadlock Problem. Deadlocks. Deadlocks. Bridge Crossing Example

Operating System Overview. Otto J. Anshus

Virtual Memory. How is it possible for each process to have contiguous addresses and so many of them? A System Using Virtual Addressing

OPERATING SYSTEMS MEMORY MANAGEMENT

Virtual Machines. COMP 3361: Operating Systems I Winter

CHAPTER 6 TASK MANAGEMENT

VIRTUAL MEMORY IN CONTEMPORARY MICROPROCESSORS

Paging: Introduction A Simple Example And Overview

Operating Systems, 6 th ed. Test Bank Chapter 7

Embedded x86 Programming: Protected Mode

Board Notes on Virtual Memory

Computer Organization and Architecture

CS161: Operating Systems

Introduction to Virtual Machines

CS 695 Topics in Virtualization and Cloud Computing. More Introduction + Processor Virtualization

Chapter 6, The Operating System Machine Level

6.033 Computer System Engineering

Outline: Operating Systems

Intel s Virtualization Extensions (VT-x) So you want to build a hypervisor?

OPERATING SYSTEM - MEMORY MANAGEMENT

COS 318: Operating Systems. File Layout and Directories. Topics. File System Components. Steps to Open A File

Chapter 10: Virtual Memory. Lesson 03: Page tables and address translation process using page tables

Computer Architecture

Introduction to RISC Processor. ni logic Pvt. Ltd., Pune

Intel P6 Systemprogrammering 2007 Föreläsning 5 P6/Linux Memory System

Chapter 7 Memory Management

Page 1 of 5. IS 335: Information Technology in Business Lecture Outline Operating Systems

Virtualization in Linux KVM + QEMU

Hybrid Virtualization The Next Generation of XenLinux

Operating Systems. Virtual Memory

Understanding a Simple Operating System

Virtualization. Jia Rao Assistant Professor in CS

An Implementation Of Multiprocessor Linux

Phoenix Technologies Ltd.

Addendum Intel Architecture Software Developer s Manual

Memory management. Chapter 4: Memory Management. Memory hierarchy. In an ideal world. Basic memory management. Fixed partitions: multiple programs

18-548/ Associativity 9/16/98. 7 Associativity / Memory System Architecture Philip Koopman September 16, 1998

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Intel 8086 architecture

Virtualization. Explain how today s virtualization movement is actually a reinvention

Operating Systems. 05. Threads. Paul Krzyzanowski. Rutgers University. Spring 2015

Technical Properties. Mobile Operating Systems. Overview Concepts of Mobile. Functions Processes. Lecture 11. Memory Management.

CS5460: Operating Systems. Lecture: Virtualization 2. Anton Burtsev March, 2013

Lecture 25 Symbian OS

Physical Data Organization

matasano Hardware Virtualization Rootkits Dino A. Dai Zovi

Virtual machines and operating systems

COS 318: Operating Systems

361 Computer Architecture Lecture 14: Cache Memory

We r e going to play Final (exam) Jeopardy! "Answers:" "Questions:" - 1 -

Full and Para Virtualization

The Linux Kernel: Process Management. CS591 (Spring 2001)

IOMMU: A Detailed view

PikeOS: Multi-Core RTOS for IMA. Dr. Sergey Tverdyshev SYSGO AG , Moscow

& Data Processing 2. Exercise 3: Memory Management. Dipl.-Ing. Bogdan Marin. Universität Duisburg-Essen

Virtualization. Clothing the Wolf in Wool. Wednesday, April 17, 13

Secondary Storage. Any modern computer system will incorporate (at least) two levels of storage: magnetic disk/optical devices/tape systems

COS 318: Operating Systems. Virtual Machine Monitors

Virtual Memory. Chapter 4

Lecture 10: Dynamic Memory Allocation 1: Into the jaws of malloc()

Advanced Computer Architecture-CS501. Computer Systems Design and Architecture 2.1, 2.2, 3.2

COS 318: Operating Systems

I/O. Input/Output. Types of devices. Interface. Computer hardware

Outline. Outline. Why virtualization? Why not virtualize? Today s data center. Cloud computing. Virtual resource pool

Virtual Memory Paging

Chapter 12. Paging an Virtual Memory Systems

Exceptions in MIPS. know the exception mechanism in MIPS be able to write a simple exception handler for a MIPS machine

ELEC 377. Operating Systems. Week 1 Class 3

Clouds Under the Covers. Elgazzar - CISC Fall

OSes. Arvind Seshadri Mark Luk Ning Qu Adrian Perrig SOSP2007. CyLab of CMU. SecVisor: A Tiny Hypervisor to Provide

6.828 Operating System Engineering: Fall Quiz II Solutions THIS IS AN OPEN BOOK, OPEN NOTES QUIZ.

A Deduplication File System & Course Review

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

WHITE PAPER. AMD-V Nested Paging. AMD-V Nested Paging. Issue Date: July, 2008 Revision: 1.0. Advanced Micro Devices, Inc.

b. A program calls malloc to request more memory from the operating system. i.what system call would malloc use to request more memory of the OS?

COS 318: Operating Systems. Virtual Machine Monitors

SOC architecture and design

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

CHAPTER 17: File Management

COMPUTER HARDWARE. Input- Output and Communication Memory Systems

Overview. CISC Developments. RISC Designs. CISC Designs. VAX: Addressing Modes. Digital VAX

Two Parts. Filesystem Interface. Filesystem design. Interface the user sees. Implementing the interface

Transcription:

W4118: segmentation and paging Instructor: Junfeng Yang

Outline Memory management goals Segmentation Paging TLB 1

Uni- v.s. multi-programming Simple uniprogramming with a single segment per process Uniprogramming disadvantages Only one process can run a time Process can destroy OS Want multiprogramming! OS User Process 2

Multiple address spaces co-exist max PHYSTOP AS1 0 max 0 max AS2 AS3 0 Logical view 0 Physical view 3

Memory management wish-list Sharing multiple processes coexist in main memory Transparency Processes are not aware that memory is shared Run regardless of number/locations of other processes Protection Cannot access data of OS or other processes Efficiency: should have reasonable performance Purpose of sharing is to increase efficiency Do not waste CPU or memory resources (fragmentation) 4

Outline Memory management goals Segmentation Paging TLB 5

Memory Management Unit (MMU) Virtual Addresses CPU MMU MEMORY Map program-generated address (virtual address) to hardware address (physical address) dynamically at every reference Check range and permissions Programmed by OS Physical Addresses 6

x86 address translation CPU generates virtual address (seg, offset) Given to segmentation unit Which produces linear addresses Linear address given to paging unit Which generates physical address in main memory 7

Segmentation Divide virtual address space into separate logical segments; each is part of physical mem stack stack heap code data heap data code 8

Segmentation translation Virtual address: <segment-number, offset> Segment table maps segment number to segment information Base: starting address of the segment in physical memory Limit: length of the segment Addition metadata includes protection bits Limit & protection checked on each access 9

x86 segmentation hardware Logical address 15 selector 0 31 offset 0 Global descriptor table base limit perm base limit perm base limit perm 31 + Compute: base + offset Check: offset <= limit Check: permissions 0 Linear address 10

xv6 segments vm.c, seginit() Kernel code: readable + executable in kernel mode Kernel data: writable in kernel mode User code: readable + executable in user mode User data: writable in user mode These are all null mappings Map to [0, 0xFFFFFFFF) Linear address = Offset Kernel CPU: shortcuts to per-cpu data Base: &c->cpu Limit: 8 bytes 11

Pros and cons of segmentation Advantages Segment sharing Easier to relocate segment than entire program Avoids allocating unused memory Flexible protection Efficient translation Segment table small fit in MMU Disadvantages Segments have variable lengths how to fit? Segments can be large fragmentation 12

Outline Memory management goals Segmentation Paging TLB 13

Goal Paging overview Eliminate fragmentation due to large segments Don t allocate memory that will not be used Enable fine-grained sharing Paging: divide memory into fixed-sized pages For both virtual and physical memory Another terminology A virtual page: page A physical page: frame 14

Page translation Address bits = page number + page offset Translate virtual page number (vpn) to physical page number (ppn) using page table pa = page_table[va/pg_sz] + va%pg_sz CPU vpn off ppn off ppn vpn ppn Page table Memory 15

Page translation example Page 0 Page 1 Page 2 Page 3 Virtual Memory 0 1 1 4 2 3 3 7 Page table Page 0 Page 2 Page 1 Page 3 Physical Memory 16

Page translation exercise 8-bit virtual address, 10-bit physical address, and each page is 64 bytes How many virtual pages? How many physical pages? How many entries in page table? Given page table = [2, 5, 1, 8], what s the physical address for virtual address 241? m-bit virtual address, n-bit physical address, k-bit page size What are the answers to the above questions? 17

Page protection Implemented by associating protection bits with each virtual page in page table Protection bits present bit: map to a valid physical page? read/write/execute bits: can read/write/execute? user bit: can access in user mode? x86: PTE_P, PTE_W, PTE_U Checked by MMU on each memory access 18

Page protection example Page 0 Page 1 Page 3 Virtual Memory pwu 0 1 2 3 1 4 3 7 101 110 000 111 Page table Page 0 Page 1 Page 3 Physical Memory 19

Page allocation Free page management E.g., can put page on a free list Allocation policy E.g., one page at a time, from head of free list xv6: kalloc.c free_page_list Page 0 Page 1 Page 3 2, 3, 6, 5, 0 20

Implementation of page table Page table is stored in memory Page table base register (PTBR) points to the base of page table x86: cr3 OS stores base in process control block (PCB) OS switches PTBR on each context switch Problem: each data/instruction access requires two memory accesses Extra memory access for page table 21

Given: Page table size issues A 32 bit address space (4 GB) 4 KB pages A page table entry of 4 bytes Implication: page table is 4 MB per process! Observation: address space are often sparse Few programs use all of 2^32 bytes Change page table structures to save memory Trade translation time for page table space 22

Hierarchical page table Break up virtual address space into multiple page tables at different levels 23

Address translation with hierarchical page table 24

x86 page translation with 4KB pages 32-bit address space, 4 KB page 4KB page 12 bits for page offset How many bits for 2 nd -level page table? Desirable to fit a 2 nd -level page table in one page 4KB/4B = 1024 10 bits for 2 nd -level page table Address bits for top-level page table: 32 10 12 = 10 page number page offset p i p 2 d 10 10 12 25

x86 paging architecture 26

x86 PC Physical Memory Layout Specific to each platform Different across architectures Different for machines with the same processor Firmware knows exact layout Passes to kernel at boot time (in Linux through atag_mem structures) 27

Linux Address Space Layout Same address layout for all processes Read: http://duartes.org/gustavo/blog/post/anatomy-of-a-program-in-memory 28

xv6 address space (memlayout.h) 29

xv6 address space implementation Split into kernel space and user space User: 0--KERNBASE Map to physical pages Kernel: KERNBASE (KERNBASE+PHYSTOP) Virtual address = physical address + KERNBASE Kernel: 0xFE000000--4GB Direct (virtual = physical) Kernel: vm.c, setupkvm() User: vm.c, inituvm() and exec.c, exec() 30

Outline Memory management goals Segmentation Paging TLB 31

Avoiding extra memory accesses Observation: locality Temporal: access locations accessed just now Spatial: access locations adjacent to locations accessed just now Process often needs only a small number of vpn ppn mappings at any moment! Fast-lookup hardware cache called associative memory or translation lookaside buffers (TLBs) Fast parallel search (CPU speed) Small VPN PPN 32

Paging hardware with TLB 33

Effective access time with TLB Assume memory cycle time is 1 unit time TLB Lookup time = TLB Hit ratio = Percentage of times that a vpn ppn mapping is found in TLB Effective Access Time (EAT) EAT = (1 + ) + (2 + )(1 ) = + + 2 + - - 2 = 2 + 34

TLB Miss Depending on the architecture, TLB misses are handled in either hardware or software Hardware (CISC: x86) Pros: hardware doesn t have to trust OS! Cons: complex hardware, inflexible Software (RISC: MIPS, SPARC) Pros: simple hardware, flexible Cons: code may have bug! Question: what can t a TLB miss handler do? 35

TLB and context switches What happens to TLB on context switches? Option 1: flush entire TLB x86 load cr3 (load page table base) flushes TLB x86 INVLPG addr invalidates one TLB entry Option 2: attach process ID to TLB entries ASID: Address Space Identifier MIPS, SPARC 36

Backup Slides 37

Motivation for page sharing Efficient communication. Processes communicate by write to shared pages Memory efficiency. One copy of read-only code/data shared among processes Example 1: multiple instances of the shell program Example 2: copy-on-write fork. Parent and child processes share pages right after fork; copy only when either writes to a page 38

Page sharing example 39

80x86 segment selector Logical address: segment selector + offset Segment selector stored in segment registers (16-bit) cs: code segment selector ss: stack segment selector ds: data segment selector es, fs, gs Segment register can be implicitly or explicitly specified Implicit by type of memory reference (jmp) mov $8049780, %eax // implicitly use ds Through special registers (cs, ss, es, ds, fs, gs on x86) mov %ss:$8049780, %eax // explicitly use ss 40

Three-level Paging in Linux 41

Page table structures Hierarchical paging Hashed page tables Inverted page tables 42

Hashed page table Common in address spaces > 32 bits Page table contains a chain of elements hashing to the same location On page translation Hash virtual page number into page table Search chain for a match on virtual page number 43

Hashed page table example 44

Inverted page table One entry for each real page of memory Entry consists of the virtual address of the page stored in that real memory location, with information about the process that owns that page Can use hash table to limit the search to one or at most a few page-table entries 45

Inverted page table example 46