Memory Management 2005. 5. 백 일 우

Slides:



Advertisements
Similar presentations
1/1/ / faculty of Electrical Engineering eindhoven university of technology Memory Management and Protection Part 3:Virtual memory, mode switching,
Advertisements

Part IV: Memory Management
16.317: Microprocessor System Design I
Introduction to Memory Management 黃偉修 Linux Kernel.
OS Memory Addressing.
Instructors: Randy Bryant and Dave O’Hallaron
Memory Management (II)
Linux Vs. Windows NT Memory Management Hitesh Kumar
Chapter 3.2 : Virtual Memory
Memory Management 1 CS502 Spring 2006 Memory Management CS-502 Spring 2006.
CS-3013 & CS-502, Summer 2006 Memory Management1 CS-3013 & CS-502 Summer 2006.
Memory Management Chapter 5.
Operating System Chapter 7. Memory Management Lynn Choi School of Electrical Engineering.
Linux Virtual Memory for Intel Processor
1 Chapter 3.2 : Virtual Memory What is virtual memory? What is virtual memory? Virtual memory management schemes Virtual memory management schemes Paging.
Subject: Operating System.
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.
Topic 2d High-Level languages and Systems Software
Virtual Memory 1 Chapter 13. Virtual Memory Introduction Demand Paging Hardware Requirements 4.3 BSD Virtual Memory 4.3 BSD Memory Management Operations.
Virtual Memory 1 1.
University of Amsterdam Computer Systems – virtual memory Arnoud Visser 1 Computer Systems Virtual Memory.
Paging (continued) & Caching CS-3013 A-term Paging (continued) & Caching CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.
Demand Paging Reference Reference on UNIX memory management
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Virtual Memory Implementation.
What is a Process ? A program in execution.
OS Memory Addressing. Architecture CPU – Processing units – Caches – Interrupt controllers – MMU Memory Interconnect North bridge South bridge PCI, etc.
Chapter 7 Memory Management Eighth Edition William Stallings Operating Systems: Internals and Design Principles.
Memory Management. 2 How to create a process? On Unix systems, executable read by loader Compiler: generates one object file per source file Linker: combines.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Virtual Memory: Systems CENG331 - Computer Organization.
CS203 – Advanced Computer Architecture Virtual Memory.
Virtual Memory: Systems
CS161 – Design and Architecture of Computer
CS 105 “Tour of the Black Holes of Computing!”
ECE232: Hardware Organization and Design
CS161 – Design and Architecture of Computer
Memory Management and Virtual Memory
Virtual Memory: Systems
Modeling Page Replacement Algorithms
Program Execution in Linux
Memory Management and Virtual Memory
Virtual Memory Chapter 8.
Topic 2e High-Level languages and Systems Software
Virtual Memory: Systems /18-213/14-513/15-513: Introduction to Computer Systems 18th Lecture, October 25, 2018.
Chapter 8: Main Memory.
Operating System Concepts
Virtual Memory: Systems
Virtual Memory: Systems
Memory System Case Studies Oct. 13, 2008
Pentium/Linux Memory System
Main Memory Background Swapping Contiguous Allocation Paging
Modeling Page Replacement Algorithms
Introduction to the Intel x86’s support for “virtual” memory
Pentium III / Linux Memory System April 4, 2000
Virtual Memory.
Instructor: Phil Gibbons
CSE 451: Operating Systems Autumn 2005 Memory Management
Operating System Chapter 7. Memory Management
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
Virtual Memory: Systems CSCI 380: Operating Systems
Program Execution in Linux
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
CS703 - Advanced Operating Systems
Virtual Memory Lecture notes from MKP and S. Yalamanchili.
Operating Systems: Internals and Design Principles, 6/E
CSE 542: Operating Systems
Virtual Memory 1 1.
Page Main Memory.
Presentation transcript:

Memory Management 백 일 우

2 Contents Virtual Memory Physical Memory Virtual memory into physical memory in INTEL processor Linux kernel data structure for memory management

3 Virtual Memory User always need more memory than physical one To overcome limit of physical memory Overlay, virtual memory, etc Virtual memory can offer virtual space independent of existing physical memory. If 32bit processor, 2 space(4GB) If 64bit processor, 2 space 32 64

4 Virtual Memory #include int a, b; int glob = 3; char buf[100]; main(int argc, char* argv[]) { int i = 1; int local_var; a = i + 1; printf(“value of a = %d\n”, a); } kernel stack heap text 4GB 3GB Kernel space user space /* Local variable */ argc, argv, i, local_var /* static variable */ a, b, glob, buf /* command */ a = i + 1; printf(“value of a = %d\n”, a); 0 bss data [Virtual memory structure in Program] Block Started by Symbol

5 Virtual Memory Text segment Taking up from lowest space, that is, starting from 0x0 Data segment Taking up from the end of the text segment Stack segment Taking up from the boundary between user and kernel space to downward Stack depends on program Segments construct virtual memory and divided by fixed page Some use only segments, not page or contrary Most processor including INTEL use both of them Difference between Segment and Page The former could be variable size, the latter fixed size

6 Virtual Memory a.out Format  Early UNIX use COFF(common object file format), but now ELF ELF(Excutable and Linking Format) ELF header phdr 1 phdr 2 …. phdr n Section 1 Section 2 …. Section n test.c gcc p_flags p_offset p_vaddr p_filesz p_memsz p_types …. e_ident e_type e_machine e_phnum e_flags …. a.out : (ELF format) /*include/linux/elf.h*/

7 ELF(Executable and Linking Format) ELF file format Consisted of n sections and each section correspond to each segment  If a.out has text, stack, and data segments, 3 Sections  If a.out is used like shared library(meaning NO stack), 2 Sections  If data segment could be divided and each has different approaches, could be 4 sections Contents of the entire Header of a.out  e_ident : magic number of file  e_type : file type (executable file or not, object file/shared library file)  e_machine : target system that execute the file  e_phnum : number of existing section Each section has its own header(phdr : program header)  p_flags : Approach type  If section is text, Only Read  If data or stack, Be able to Read/Write  p_vaddr : Starting virtual address  p_memsz : size of the section

8 Physical Memeory Size of physical memory depends on main memory Physical memory basic unit ‘Page frame’ Virtual memory basic unit called as Page Generally, the size of page frame is equal to page’s The size of page frame depends on system processor  If INTEL, page frame size is 4KB (Alpha’s is 8KB)  If Intel and 256MB, there are 2 page frames 16 Page frame n Page frame n-1 …. Page frame 5 Page frame 4 Page frame 3 Page frame 2 Page frame 1 Task 1 Task 2 Kernel Free_pageframe Physical Memory

9 Physical Memeory Linux kernel is resident program so, it always take up parts of page frame The other parts are FREE When some task created or ask for memory, kernel assign memory in Free pageframe  It will be returned when exit or return the memory If free is getting too smaller, some parts of existing task memory would be swapped out, returned its memory by compulsion and added to FREE  Kswapd Daemon do this What happen if some task request smaller page than unit size of page frame?  Linux use Buddy system

10 Relation between virtual and physical Suppose,…. Call sys_execve() with argument a.out  Load new program as argument (system_call) a.out has 3 section( text:12KB, data:16KB, stack:8KB ) Processor INTEL ELF header phdrs text data stack DISK a.out kernel stack heap text 4GB 3GB 0 data Virtual Memory Kernel stack Kernel data Kernel text 8KB 16KB 12KB 28KB 12KB S1 : 3GB-4KB S2 : 3GB-8KB

11 Relation between virtual and physical ELF header phdrs …. text data stack DISK a.out Text 2 Data1 Stack1 Text1 0 K 4 K 8 K 12 K 16 K 28 K 20 K 24 K 32 K N-1 K n K Memory Load a.out in physical memory

12 Relation between virtual and physical Why not text3, data2,etc loaded? Not all loaded, just when it needed Demand Paging  When approach not loaded page, ‘Page fault’  Trapped, and page fault handler is called by IDT table  Page fault handler get free page frame, read parts correspond to the page, and load them in page frame assigned newly Page table  Processor has to know how to translate virtual address into physical memory  That is, if processor want to use some commands in text1, it need the page table which let the processor know that text1 is located in 4KB page frame

13 IDT(Interrupt Descriptor Table) When U call system_call, finally ‘Trap’ happen Trap is software interrupt All kinds of interrupt through IDT are controlled at i386  System_call use 0x80 IDT struct

14 Page Table kernel 4GB 3GB Virtual Memory 12KB text data stack 0 K 4K 28K NP 20K NP ….. …. 12K NP Text 2 Data1 Stack1 Text1 0 K 4 K 8 K 12 K 16 K 28 K 20 K 24 K 32 K N-1 K n K Physical Memory Page Table

15 Approaching Memory If approach address number 1000,..(page size : 4KB) It means text 1(first page) in virtual memory 1000 / 4000 = 0(share), 1000(remainder)  Share(0) means entry index  Remain(1000) means offset Search first index page and location page table say, move as size of offset 1000 What about 10000? NP(not presented) Page fault Assign page fame newly, and load Write down page frame number Do address_Translation again Now possible to translate virtual address 10000

16 INTEL processor Works like,..(I) In previous example, if each task has its own page table,. Number of table entry is getting too many  220 (4GB/4KB) entries in page table are too many 2 stages in Intel processor  Alpha has 3 stages DIRPAGEoffset PFN CR3 PFN PFN PFNoffset Page directory Page table 1 Page table 2 Physical address Virtual address

17 INTEL processor Works like,..(II) Each entry in page directory has 4K bytes, each entry in page table has same size 1024 * 1024 * 4KB = 4GB virtual space size DIR  Directory number  It needs 10 bits to point 1024 entries PAGE  page table index  Also 10 bits needed like above PFN  Physical page frame number  Because page frame size is 4KB, offset is 12bits According to example, entry of page directory and page table need only 20 bits Page directory entry point to page frame address of page table, also page table entry point to page frame address of physical memory So, upper 20 bits are enough to point them  Size is 4KB,and they will located at as 4 multiples Lower 12bits are used for other control (in Intel)

18 Page Table Entry (INTEL) Page table entry P  Meaning that whether this page exits in physical memory or not  If 0, Not exist (might be ‘Page fault’) U : Meaning that content of page frame is kernel or user program W : able to write R : task referred this page frame D : referred this page and modified  Use R, D bit to trade with pages for shortage of page frame  Two-handed clock algorithm PFNDRUWP

19 Memory management in LINUX Task management : task_struct Memory management in task_struct : mm_struct  Segments management : vm_area_struct (linked by list)  pgd : starting address of page directory  Variables for virtual memory : start_code, start_data, start_stack, etc Kernel env_end arg_end arg_start bss data text bss data text heap bss data text start_stack end_bss end_data end_code brk end_data end_code start_code Shared memory other shared memory Shared C library program 0x0 0xc xffffffff Stack End of the heap Environment variables Argument variables (argc, argv)

20 vm_area_struct mm task_struct map_count pdg mmap vm_end vm_start vm_flags …… vm_file vm_offset vm_ops vm_next vm_area_struct vm_end vm_start vm_flags …… vm_file vm_offset vm_ops vm_next vm_area_struct DATA TEXT a.out pdg : page directory when run, copy CR3 to this vm_start : segment start vm_end : sengment end vm_flags : write/read(only) vm_file, vm_offset this segment of real location in a.out

21 pdg mm task_struct map_count pdg mmap DIRPAGEoffset PFN CR3 PFN PFNoffset Page directory Page table 1 Physical address Virtual address 3111 pdg : page directory when run, copy CR3 to variable pdg PFN 31110

22 Summary Can offer very big size of address space than real one No need to load all pages of program Demand-paging : when refer or need, and then load One more tasks want to share specific region, just point page frame using page table But, need to translate(transform) It can cause program performance lower Most system use hardware way for performance  HAT : hardware address translation  MMU : Memory management Unit TLB(translation lookup buffer)  Fast translation using page table cache