W4118 Operating Systems. Instructor: Junfeng Yang



Similar documents
W4118: segmentation and paging. Instructor: Junfeng Yang

VM Architecture. Jun-Chiao Wang, 15 Apr NCTU CIS Operating System lab Linux kernel trace seminar

CHAPTER 6 TASK MANAGEMENT

Lecture 17: Virtual Memory II. Goals of virtual memory

Virtual vs Physical Addresses

W4118 Operating Systems. Junfeng Yang

COS 318: Operating Systems. Virtual Memory and Address Translation

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

Carnegie Mellon Virtual Memory of a Linux Process Process-specific data structs (ptables, Different for

CS5460: Operating Systems

Computer Architecture

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

How To Write To A Linux Memory Map On A Microsoft Zseries (Amd64) On A Linux (Amd32) (

How To Write A Page Table

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

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

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

Phoenix Technologies Ltd.

Virtualization in Linux KVM + QEMU

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

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

Memory Management under Linux: Issues in Linux VM development

Intel Virtualization Technology Specification for the IA-32 Intel Architecture

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

Operating Systems, 6 th ed. Test Bank Chapter 7

Chapter 10 Case Study 1: LINUX

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

Embedded x86 Programming: Protected Mode

Intel Vanderpool Technology for IA-32 Processors (VT-x) Preliminary Specification

Virtual Memory. Chapter 4

OPERATING SYSTEM - VIRTUAL MEMORY

Bringing PowerPC Book E to Linux

Virtual Memory Behavior in Red Hat Linux Advanced Server 2.1

Operating Systems. Virtual Memory

Linux kernel support to exploit phase change memory

Where is the memory going? Memory usage in the 2.6 kernel

Computer Engineering and Systems Group Electrical and Computer Engineering SCMFS: A File System for Storage Class Memory

Understanding a Simple Operating System

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

Chapter 12 File Management

VIRTUAL MEMORY IN CONTEMPORARY MICROPROCESSORS

Paging: Introduction A Simple Example And Overview

Virtual Memory Paging

Physical Memory RAM. The kernel keeps track of how it is used

Hybrid Virtualization The Next Generation of XenLinux

Lecture 25 Symbian OS

Xen and the Art of. Virtualization. Ian Pratt

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

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

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

6.033 Computer System Engineering

matasano Hardware Virtualization Rootkits Dino A. Dai Zovi

Effective Handling of Low Memory Scenarios in Android

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

Full and Para Virtualization

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

Chapter 5 Cloud Resource Virtualization

The Linux Virtual Filesystem

Intel 8086 architecture

Chapter 2. Processes. Address Spaces

Hardware Assisted Virtualization Intel Virtualization Technology

Virtual Machines. COMP 3361: Operating Systems I Winter

Chapter 12. Paging an Virtual Memory Systems

IOMMU: A Detailed view

Virtual machines and operating systems

There s a kernel security researcher named Dan Rosenberg whose done a lot of linux kernel vulnerability research

Segmentation and Fragmentation

Addendum Intel Architecture Software Developer s Manual

Virtualization. ! Physical Hardware. ! Software. ! Isolation. ! Software Abstraction. ! Encapsulation. ! Virtualization Layer. !

Operating System Overview. Otto J. Anshus

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

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

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

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

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

Virtualization. Jia Rao Assistant Professor in CS

COS 318: Operating Systems

KVM & Memory Management Updates

OPERATING SYSTEM - MEMORY MANAGEMENT

Topics in Computer System Performance and Reliability: Storage Systems!

Chapter 7 Memory Management

5.14. EXCEPTION AND INTERRUPT REFERENCE

Recovery Principles in MySQL Cluster 5.1

Embedded X86 Programming: Protected Mode

Outline: Operating Systems

Operating Systems CSE 410, Spring File Management. Stephen Wagner Michigan State University

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

File Systems Management and Examples

CS 377: Operating Systems. Outline. A review of what you ve learned, and how it applies to a real operating system. Lecture 25 - Linux Case Study

Introduction to Virtual Machines

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

4.1 PAGING MODES AND CONTROL BITS

Virtualization Technology. Zhiming Shen

Memories Are Made of This

The Classical Architecture. Storage 1 / 36

General Purpose Operating System Support for Multiple Page Sizes

Betriebssysteme KU Security

Transcription:

W4118 Operating Systems Instructor: Junfeng Yang

Outline x86 segmentation and paging hardware Linux address space translation Copy-on-write Linux page replacement algorithm Linux dynamic memory allocation 1

x86 segmentation and paging Using Pentium as example 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 Paging units form equivalent of MMU 2

x86 segmentation hardware 3

Specifying segment selector virtual 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 $8049780 // implicitly use cs mov $8049780, %eax // implicitly use ds Through special registers (cs, ss, es, ds, fs, gs on x86) mov %ss:$8049780, %eax // explicitly use ss 4

x86 paging hardware 5

Outline x86 segmentation and paging hardware Linux address space translation Copy-on-write Linux page replacement algorithm Linux dynamic memory allocation 6

Linux address translation Linux uses paging to translate virtual addresses to physical addresses Linux does not use segmentation Advantages More portable since some RISC architectures don t support segmentation Hierarchical paging is flexible enough 7

Linux segmentation Since x86 segmentation hardware cannot be disabled, Linux just uses NULL mappings Linux defines four segments Set segment base to 0x00000000, limit to 0xffffffff segment offset == linear addresses User code (segment selector: USER_CS) User data (segment selector: USER_DS) Kernel code (segment selector: KERNEL_CS) Kernel data (segment selector: KERNEL_DATA) arch/i386/kernel/head.s 8

Segment protection Current Privilege level (CPL) specifies privileged mode or user mode Stored in current code segment descriptor User code segment: CPL = 3 Kernel code segment: CPL = 0 Descriptor Privilege Level (DPL) specifies protection Only accessible if CPL <= DPL Switch between user mode and kernel mode (e.g. system call and return) Hardware load the corresponding segment selector ( USER_CS or KERNEL_CS) into register cs 9

Paging Linux uses up to 4-level hierarchical paging A linear address is split into five parts, to seamlessly handle a range of different addressing modes Page Global Dir Page Upper Dir Page Middle Dir Page Table Page Offset Example: 32-bit address space, 4KB page without physical address extension (hardware mechanism to extend address range of physical memory) Page Global dir: 10 bits Page Upper dir and Page Middle dir are not used Page Table: 10 bits Page Offset: 12 bits 10

Paging in 64 bit Linux Platform Page Size Address Bits Used Paging Levels Address Splitting Alpha 8 KB 43 3 10+10+10+13 IA64 4 KB 39 3 9+9+9+12 PPC64 4 KB 41 3 10+10+9+12 sh64 4 KB 41 3 10+10+9+12 X86_64 4 KB 48 4 9+9+9+9+12 11

Page table operations Linux provides data structures and operations to create, delete, read and write page directories include/asm-i386/pgtable.h arch/i386/mm/hugetlbpage.c Naming convention pgd: Page Global Directory pmd: Page Middle Directory pud: Page Upper Directory pte: Page Table Entry Example: mk_pte(p, prot) 12

TLB operations x86 uses hardware TLB OS does not manage TLB Only operation: flush TLB entries include/asm-i386/tlbflush.h movl %0 cr3: flush all TLB entries invlpg addr: flush a single TLB entry More efficient than flushing all TLB entries 13

Outline x86 segmentation and paging hardware Linux address space translation Copy-on-write Linux page replacement algorithm Linux dynamic memory allocation 14

A cool trick: copy-on-write In fork(), parent and child often share significant amount of memory Expensive to copy all pages COW Idea: exploit VA to PA indirection Instead of copying all pages, share them If either process writes to shared pages, only then is the page copied How to detect page write? Mark pages as read-only in both parent and child address space On write, page fault occurs 15

Share pages copy_process() in kernel/fork.c copy_mm() dup_mmap() // copy page tables copy_page_range() in mm/memory.c copy_pud_range() copy_pmd_range() copy_pte_range() copy_one_pte() // mark readonly 16

Copy page on page fault set_intr_gate(14, &page_fault) in arch/i386/kernel/traps.c ENTRY(page_fault) calls do_page_fault in arch/i386/kernel/entry.s do_page_fault in arch/i386/mm/fault.c cr2 stores faulting virtual address handle_mm_fault in mm/memory.c handle_pte_fault in mm/memory.c if(write_access) do_wp_page() 17

Outline x86 segmentation and paging hardware Linux address space translation Copy-on-write Linux page replacement algorithm Linux dynamic memory allocation 18

Linux page replacement algorithm Two lists in struct zone active_list: hot pages inactive_list: cold pages Two bits in struct page PG_active: is page on active list? PG_referenced: has page been referenced recently? Approximate LRU algorithm Replace a page in inactive list Move from active to inactive under memory pressure Need two accesses to go from inactive to active 19

Functions for page replacement lru_cache_add*(): add to inactive or active list mark_page_accessed(): called twice to move a page from inactive to active page_referenced(): test if a page is referenced refill_inactive_zone(): move pages from active to inactive 20

How to swap out page free_more_memory() in fs/buffer.c called try_to_free_pages in mm/vmscan.c shrink_caches shrink_zone refill_inactive_zone shrink_cache shrink_list if(pagedirty(page)) pageout() 21

How to load page On page fault, cr2 stores faulting virtual address handle_mm_fault() in mm/memory.c handle_pte_fault() if(!pte_present(entry)) do_no_page() // anonymous page do_file_page() // file mapped page do_swap_page() // swapped out page 22

Outline x86 segmentation and paging hardware Linux address space translation Copy-on-write Linux page replacement algorithm Linux dynamic memory allocation 23

Dynamic memory allocation How to allocate pages? Data structures for page allocation Buddy algorithm for page allocation How to allocate objects? Slab allocation 24

Page descriptor Keep track of the status of each physical page struct page, include/linux/mm.h All stored in mem_map array Simple mapping between a page and its descriptor Nth page s descriptor is mem_map[n] virt_to_page page_to_pfn 25

Memory zone Keep track of pages in different zones struct zone, include/linux/mmzone.h ZONE_DMA: <16MB ZONE_NORMAL: 16MB-896MB ZONE_HIGHMEM: >896MB 26

Linux page allocator Linux use a buddy allocator for page allocation Fast, simple allocation for blocks that are 2^n bytes [Knuth 1968] Idea: a free list for each size of block users want to allocate page_alloc() in mm/page_alloc.c 27

Linux buddy allocator implementation Data structure 11 free lists of blocks of pages of size 2^0, 2^1,, 2^10 Allocation restrictions: 2^n pages, 0<= n <= 10 Allocation of 2^n pages: Search free lists (n, n+1, n+2, ) for appropriate size Free Recursively divide larger blocks until reach block of correct size Insert buddy blocks into free lists Recursively coalesce block with buddy if buddy free 28

Pros and cons of buddy allocator Advantages Fast and simple compared to general dynamic memory allocation Avoid external fragmentation by keeping free physical pages contiguous Disadvantages Internal fragmentation Allocation of block of k pages when k!= 2^n 29

Slab allocator For objects smaller than a page Implemented on top of page allocator Memory managed by slab allocator is called cache Two types of slab allocator Fixed-size slab allocator: cache contains objects of same size for frequently allocated objects General-purpose slab allocator: caches contain objects of size 2^n for less frequently allocated objects For allocation of object with size k, round to nearest 2^n _kmem_cache_create() and _kmalloc() in mm/slab.c 30

Pros and cons of slab allocator Advantages Reduce internal fragmentation: many objects in one page Fast Disadvantages Memory overhead for bookkeeping Internal fragmentation for general-purpose slab allocator 31