1 / 22

Memory Management

Memory Management. 2005. 5. 백 일 우 steigensonne@hufs.ac.kr. Contents. Virtual Memory Physical Memory Virtual memory into physical memory in INTEL processor Linux kernel data structure for memory management. Virtual Memory . User always need more memory than physical one

annis
Télécharger la présentation

Memory Management

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Memory Management 2005. 5. 백 일 우 steigensonne@hufs.ac.kr

  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 4GB #include <stdio.h> 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 /* Local variable */ argc, argv, i, local_var Kernel space 3GB stack heap user space bss /* static variable */ a, b, glob, buf data Block Started by Symbol /* command */ a = i + 1; printf(“value of a = %d\n”, a); text 0 [Virtual memory structure in Program]

  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. ELF header phdr 1 phdr 2 …. phdr n Section 1 Section 2 …. Section n Virtual Memory • a.out • Format • Early UNIX use COFF(common object file format), but now ELF • ELF(Excutable and Linking Format) gcc p_flags p_offset p_vaddr p_filesz p_memsz p_types …. test.c 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. Page frame n Page frame n-1 …. Page frame 5 Page frame 4 Page frame 3 Page frame 2 Page frame 1 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 Physical Memory Task 1 Kernel Free_pageframe Task 2

  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. DISK a.out ELF header phdrs text data stack 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 Virtual Memory Kernel stack 4GB kernel 3GB stack 8KB Kernel data heap Kernel text 28KB S1 : 3GB-4KB S2 : 3GB-8KB data 16KB 12KB text 12KB 0

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

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

  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. Virtual address DIR PAGE offset 31 21 11 31 31 31 11 11 11 0 0 0 PFN PFN PFN 31 11 PFN offset Physical address Page table 1 Page directory CR3 Page table 2 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

  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. 31 11 6 5 2 1 0 PFN D R U W P 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

  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 0xffffffff Kernel Environment variables 0xc0000000 env_end arg_end Stack arg_start Argument variables (argc, argv) start_stack Shared memory bss other shared memory data text end_bss bss end_data data Shared C library end_code text End of the heap brk heap end_data bss program data end_code text start_code 0x0

  20. vm_area_struct vm_area_struct task_struct mm map_count vm_end vm_start pdg DATA vm_flags …… vm_file vm_offset mmap vm_ops vm_next vm_area_struct vm_end vm_start TEXT vm_flags …… vm_file vm_offset 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 vm_ops vm_next

  21. DIR PAGE offset 31 31 31 11 11 11 0 0 0 PFN PFN PFN PFN offset pdg task_struct map_count pdg : page directory when run, copy CR3 to variable pdg pdg mm mmap Virtual address 31 11 Physical address Page table 1 Page directory CR3

  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

More Related