1 / 44

Principles of Virtual Memory

Principles of Virtual Memory. Virtual Memory, Paging, Segmentation. Overview. Virtual Memory Paging Segmentation Combined Segmentation a nd Paging Virtual Memory a nd Linux. 1. Virtual Memory. 1.1 Why Virtual Memory (VM)? 1.2 What is VM ? 1.3 The Mapping Process

ulla
Download Presentation

Principles of Virtual Memory

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. Principles of Virtual Memory Virtual Memory, Paging, Segmentation

  2. Overview • Virtual Memory • Paging • Segmentation • Combined Segmentation and Paging • Virtual Memory and Linux

  3. 1. Virtual Memory • 1.1 Why Virtual Memory (VM)? • 1.2 What is VM ? • 1.3 The Mapping Process • 1.4 Terms & Definitions • 1.5 The Principle of Locality • 1.6 VM: Features • 1.7 VM: Advantages • 1.8 VM: Disadvantages • 1.9 VM: Implementation

  4. 1.1 Why Virtual Memory (VM)? • Shortage of memory • Efficient memory management needed Process 3 • Process may be too big for physical memory • More active processes than physical memory can hold • Requirements of multiprogramming • Efficient protection scheme • Simple way of sharing Process 2 Process 4 Process 1 OS Memory

  5. Table (one per Process) 0xA0F4 Mapping Unit (MMU) 0xC0F4 1.2 What is VM? • Program: • .... • Mov AX, 0xA0F4 • .... Virtual Address „Piece“ of Virtual Memory PhysicalMemory „Piece“ of Physical Memory Physical Address VirtualMemory Note: It does not matter at which physical address a „piece“ of VM is placed, since the corresponding addresses are mapped by the mapping unit.

  6. piece in physical memory? OS brings „piece“ in from HDD memory access fault yes OS adjusts mapping table physical address translate address MMU 1.3 The Mapping Process • Usually every process has its own mapping table  own virtual address space (assumed from now on) • Not every „piece“ of VM has to be present in PM • „Pieces“ may be loaded from HDD as they are referenced • Rarely used „pieces“ may be discarded or written out to disk ( swapping) check using mapping table virtual address

  7. 1.4 Terms & Notions • Virtual memory (VM) is • Not a physical device but an abstract concept • Comprised of the virtual address spaces (of all processes) • Virtual address space (VAS) (of one process) • Set of visible virtual addresses • (Some systems may use a single VAS for all processes) • Resident set • Pieces of a process currently in physical memory • Working set • Set of pieces a process is currently working on

  8. repeated references: initialization data initialization code finalization code code 1 code 2 data single jumps: working set: early phase of process lifetime final phase of process lifetime main phase of process lifetime 1.5 The Principle of Locality • Principle of Locality: • Memory references within a process tend to cluster • Working set should be part of the resident set to operate efficiently (else: frequent memory access faults) honor the principle of locality to achieve this

  9. „piece“ modified? find rarely used „piece" adjust mapping table no discard „piece“ yes save HDD location of „piece“ write „piece“ out to disk 1.6 VM: FeaturesSwapping lack of memory • Danger: Thrashing: • „Piece“ just swapped out is immediately requested again • System swaps in/out all the time, no real work is done • Thus: „piece“for swap out has to be chosen carefully • Keep track of „piece“ usage („age of piece“) • Hopefully „piece“ used frequently lately will be used again in near future (principle of locality!) no need to swap out complete process!!!

  10. 1.6 VM: FeaturesProtection • Each process has its own virtual address space • Processes invisible to each other • Process cannot access another processes memory • MMU checks protection bits on memory access (during address mapping) • „Pieces“ can be protected from being written to or being executed or even being read • System can distinguish different protection levels (user / kernel mode) • Write protection can be used to implement copy on write ( Sharing)

  11. Piece0 Piece0 Piece 0 Piece 1 Piece1 Piece1 Piece 2 Piece2 Piece2 Physical memory Virtual memory Process 1 Virtual memory Process 2 1.6 VM: FeaturesSharing • „Pieces“ of different processes mapped to one single „piece“ of physical memory • Allows sharing of code (saves memory), e.g. libraries • Copy on write: „piece“ may be used by several processes until one writes to it (then that process gets its own copy) • Simplifies interprocess-communication (IPC) shared memory

  12. 1.7 VM: Advantages (1) • VM supports • Swapping • Rarely used „pieces“ can be discarded or swapped out • „Piece“ can be swapped back in to any free piece of physical memory large enough, mapping unit translates addresses • Protection • Sharing • Common data or code may be shared to save memory • Process need not be in memory as a whole • No need for complicated overlay techniques (OS does job) • Process may even be larger than all of physical memory • Data / code can be read from disk as needed

  13. 1.7 VM: Advantages (2) • Code can be placed anywhere in physical memory without relocation (adresses are mapped!) • Increased cpu utilization • more processes can be held in memory (in part) more processes in ready state (consider: 80% HDD I/O wait time not uncommon)

  14. 1.8 VM: Disadvantages • Memory requirements (mapping tables) • Longer memory access times (mapping table lookup) • Can beimproved usingTLB

  15. 1.9 VM: Implementation • VM may be implemented using • Paging • Segmentation • Combination of both • Note:Everything said in the first chapter still holds for the following chapters!

  16. 2. Paging • 2.1 What is Paging? • 2.2 Paging: Implementation • 2.3 Paging: Features • 2.4 Paging: Advantages • 2.5 Paging: Disadvantages • 2.6 Summary: Conversion of a Virtual Address

  17. 0x00 Page 0 Page 0 Page 1 Page 1 0x00 Page 2 Page 2 Frame 1 v v Frame 0 Page 3 Page 3 Frame 1 Page 4 Page 4 Frame 0 v v Frame 2 Page 5 Page 5 Frame 3 Page 6 Page 6 Frame 3 v v Page 7 Page 7 Virtual memory (divided into equal size pages) Page Table (one per process, one entry per page maintained by OS) Physical memory (divided into equal size page frames) 2.1 What is Paging?

  18. Page Frame # r read s shared w write c caching disabled x execute su super-page v valid pid process id re referenced g (extended) guard m modified gd guard data r w x v r w x v re re m m s s c c su su pid pid g g gd gd other 2.2 Paging: ImplementationTypical Page Table Entry other

  19. Virtual address Page # Offset 0x2 0x14 Page Table Page Table Base Register (PTBR) 0x0 * L 0x1 * L 0x2 * L 0x8 L : size of entry 0x8 0x14 Frame # Offset ... Physical address 2.2 Paging: ImplementationSinglelevel Page Tables one entry per page one table per process Problem: Page tables can get very large, e.g. 32 bit address space, 4KB pages  2^20 entries per process  4MB at 4B per entry 64 bit  16777216 GB page table!!!!

  20. Page #1 Page #2 Page #3 Offset Page Frame # Offset Page Directory v=0 Page Middle Directory Page Table Frame # Offset Oversized Super-Page 2.2 Paging: ImplementationMultilevel Page Tables table size can be restricted to one page not all need be present saves memory

  21. Virtual address Page # Offset 0xA 0x14 Inverted Page Table 0x0 0x1 hash fkt. 0xA PID 0x2 0x14 Frame # Offset ... Physical address 2.2 Paging: ImplementationInverted Page Tables hash table one entry per frame one table for all processes Problem: Additional information about pages not presently in memory must still be kept somehow (e.g. normal page tables on HDD)

  22. Page #1 Page #2 Page #3 Offset Virtual address 0x2 0xA 0x5 0x14 no = page fault 0x3B2 0x14 yes Frame # Offset Physical address 0xA5 0x8 0x3B2 Page Directory (guarded) Page Middle Directory Page Table 2.2 Paging: ImplementationGuarded Page Tables (1) only one valid entry per table guard guard length Frame # or page table base table not needed, if guard in place

  23. 2.2 Paging: ImplementationGuarded Page Tables (2) • Guarded page tables especially interesting if hardware offers only TLB (zero-level paging, MIPS) • OS has total flexibility, may use • Different sizes of pages and page tables (all powers of 2 ok) and as many levels as desired • Guarded page tables • Inverted page tables • Optimization: guarded table entry will usually not contain guard and guard length but equivalent information • Note that handling of protection is to be modified

  24. referenced by process prepaged by OS VM 2.3 Paging: FeaturesPrepaging • Process requests consecutive pages (or just one) OS loads following pages into memory as well (expecting they will also be needed) • Saves time when large contiguous structures are used (e.g. huge arrays) • Wastes memory and time case pages not needed

  25. 2.3 Paging: FeaturesDemand Paging • On process startup only first page is loaded into physical memory • Pages are then loaded as referenced • Saves memory • But: may cause frequent page faults until process has its working set in physical memory. • OS may adjust its policy (demand / prepaging) dependent on • Available free physical memory • Process types and history

  26. PM PM Process started, requiring 6KB (4KB pages) Frame 0 Page 1 Frame 0 Page 1 Frame 1 Page 2 Frame 1 Page 2 Frame 2 Frame 2 Frame 3 Frame 3 Linked list of free frames Frame 4 Frame 4 Frame 5 Frame 5 6 1 4 Frame 6 Frame 6 4 Frame 7 Frame 7 2.3 Paging: FeaturesCheap Memory Allocation • No search for large enough a piece of PM necessary • Any requested amount of memory is divided into pages can be distributed over the available frames • OS keeps a list of free frames • If memory is requested the first frame is taken

  27. Paging VM system Non-Paging VM system Process requires memory 3 pages 1 „piece“ PM PM 2.3 Paging: FeaturesSimplified Swapping rarely used • Process requires 3 frames • swap out 3 most seldomly used pages Swapping out the 3 most seldomly used „pieces“ will not work  Swap algo must try to create free pieces as big as possible (costly!)

  28. 2.4 Paging: Advantages • Allocating memory is easy and cheap • Any free page is ok, OS can take first one out of list it keeps • Eliminates external fragmentation • Data (page frames) can be scattered all over PM pages are mapped appropriately anyway • Allows demand paging and prepaging • More efficient swapping • No need for considerations about fragmentation • Just swap out page least likely to be used

  29. 2.5 Paging: Disadvantages • Longer memory access times (page table lookup) • Can beimproved using • TLB • Guarded page tables • Inverted page tables • Memory requirements (one entry per VM page) • Improve using • Multilevel page tables and variable page sizes (super-pages) • Guarded page tables • Page Table Length Register (PTLR) to limit virtual memory size • Internal fragmentation • Yet only an average of about ½ page per contiguous address range

  30. bring in page from HDD! process into blocking state hit TLB exception to process yes memory full? miss swap out a page page table no no reference legal? yes page in mem? HDD I/O read req. no page fault yes HDD I/O complete: interrupt process into blocking state update TLB copy on write? yes access rights? no protection fault copy page update page table yes no Hard ware exception to process process into ready state OS 2.6 Summary: Conversion ofa Virtual Address Virtual address Physical address

  31. 3. Segmentation • 3.1 What is Segmentation? • 3.2 Segmentation: Advantages • 3.3 Segmentation: Disadvantages

  32. virtual address External fragmentation Segment # Offset Seg 1 (code) Seg 2 (data) Physical memory Seg 3 (stack) Seg 3 (stack) Virtual memory Seg 1 (code) Segment table MMU Base Limit Other offset < limit ? no STBR Seg 2 (data) yes STLR memory access fault Segment Base + Offset physical address 0x00 3.1 What is Segmentation? as in paging: valid, modified, protection, etc.

  33. 3.2 Segmentation: Advantages • As opposed to paging: • No internal fragmentation (but: external fragmentation) • May save memory if segments are very small and should not be combined into one page (e.g. for reasons of protection) • Segment tables: only one entry per actual segment as opposed to one per page in VM • Average segment size >> average page sizeless overhead (smaller tables)

  34. 3.3 Segmentation: Disadvantages • External fragmentation • Costly memory management algorithms • Segmentation: find free memory area big enough (search!) • Paging: keep list of free pages, any page is ok (take first!) • Segments of unequal size not suited as well for swapping

  35. 4. Combined Segmentationand Paging (CoSP) • 4.1 What is CoSP? • 4.2 CoSP: Advantages • 4.3 CoSP: Disadvantages

  36. Virtual Address Seg # Page #1 Page #2 Offset Page Frame # Offset limit base Physical Address Segment Table Page Directory Page Table 4.1 What is CoSP? size limited by segment number not all need be present size limited by segment limit

  37. 4.2 CoSP: Advantages • Reduces memory usage as opposed to pure paging • Page table size limited by segment size • Segment table has only one entry per actual segment • Simplifies handling protection and sharing of larger modules (define them as segments) • Most advantages of paging still hold • Simplifies memory allocation • Eliminates external fragmentation • Supports swapping, demand paging, prepaging etc.

  38. Process requests a 6KB address range (4KB pages) Page 1 Page 2 internal fragmentation 4.3 CoSP: Disadvantages • Internal fragmentation • Yet only an average of about ½ page per contiguous address range

  39. 5. Virtual memoryand Linux • 5.1 Why VM under Linux? • 5.2 The Linux VM System • 5.3 The Linux Protection Scheme • 5.4 The Linux Paging System

  40. 5.1 Why VM under Linux? • Linux is a multitasking, multiuser OS. It requires: • Protection • Ability to ensure pseudo-parallel execution (even if cumulated process size greater than physical memory) • Efficient IPC methods (sharing) • Good solution: virtual memory

  41. 5.2 The Linux VM System • Kernel runs in physical addressing mode, maintains VM system • Basically a paging system • Some remainders of a CoSP scheme present: • Process memory segmented into kernel/user memory • Process in user mode (5.3) may not access kernel memory • V 2.0 defined code and data segments for each kernel and user mode • V 2.2 still defines those segments but for complete virtual address space

  42. 5.3 The Linux Protection Scheme • Linux uses two modes: kernel and user mode • Makes no use of elaborate protection scheme x86 processors provide (only uses ring 0 (kernel) and ring 3 (user)) • Programs are all started in user mode • Program needs to use system resources  must make system call (per software interrupt)  kernel code is executed on behalf of process • Kernel processes permanently run in kernel mode

  43. 5.4 The Linux Paging System • Linux employs architecture independent memory management code • Linux uses 3-level paging system • Intel x86 system: only 2-level paging • Entry in page directory is treated as page middle directory with only one entry • 4 Mb pages on some Intel systems are used (e.g. for graphics memory) • Linux uses valid, protection, referenced, modified bits • Employs copy on write and demand paging

  44. The End

More Related