Presentation is loading. Please wait.

Presentation is loading. Please wait.

Memory Management 2005. 5. 백 일 우

Similar presentations


Presentation on theme: "Memory Management 2005. 5. 백 일 우"— Presentation transcript:

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

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

3 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 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 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 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 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 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 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 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 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 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 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 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 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 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 31110 CR3 PFN 31110 PFN 31110 PFNoffset Page directory Page table 1 Page table 2 Physical address Virtual address 3111 312111

17 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 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 311165210

19 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 0xc0000000 0xffffffff Stack End of the heap Environment variables Argument variables (argc, argv)

20 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 21 pdg mm task_struct map_count pdg mmap DIRPAGEoffset PFN 31110 CR3 PFN 31110 PFNoffset Page directory Page table 1 Physical address Virtual address 3111 pdg : page directory when run, copy CR3 to variable pdg PFN 31110

22 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


Download ppt "Memory Management 2005. 5. 백 일 우"

Similar presentations


Ads by Google