memory management

42
Memory Management Ken Birman

Upload: emma

Post on 26-Feb-2016

36 views

Category:

Documents


0 download

DESCRIPTION

Memory Management. Ken Birman. Compile time. runtime. Cache. loader. ld. How to create a process?. On Unix systems, executable read by loader Compiler: generates one object file per source file Linker: combines all object files into one executable Loader: loads executable in memory. - PowerPoint PPT Presentation

TRANSCRIPT

Memory Management

Memory ManagementKen BirmanHow to create a process?On Unix systems, executable read by loader

Compiler: generates one object file per source fileLinker: combines all object files into one executableLoader: loads executable in memory2ldloaderCache

Compile timeruntimeWhat does process look like?3heapstack codeinitialized dataaddress 2^n-1address >= 0Process divided into segments, each has:- Code, data, heap (dynamic data) and stack (procedure calls)Processes in MemoryWe want many of them to coexist

Issues:PowerPoint needs more memory than there is on machine?What is visual studio tries to access 0x0005?If PowerPoint is not using enough memory?If OS needs to expand?4OSPowerPointVisual Studio0x00000x11000x12100x2300IssuesProtection: Errors in process should not affect othersTransparency: Should run despite memory size/location5gccvirtual addressCPULoad Storelegal addr?Illegal?PhysicaladdressPhysicalmemoryfaultdataTranslation box (MMU)How to do this mapping?Scheme 1: Load-time LinkingLink as usual, but keep list of referencesAt load time: determine the new base addressAccordingly adjust all references (addition)

Issues: handling multiple segments, moving in memory60x1000static a.out0x30000x4000OSjump 0x2000jump 0x50000x6000jump 0x2000Scheme 2: Execution-time LinkingUse hardware (base + limit reg) to solve the problemDone for every memory accessRelocation: physical address = logical (virtual) address + baseProtection: is virtual address < limit?

When process runs, base register = 0x3000, bounds register = 0x2000. Jump addr = 0x2000 + 0x3000 = 0x500070x1000a.out0x30000x4000OS a.outjump 0x2000jump 0x20000x6000Base: 0x3000Limit: 0x2000MMUDynamic TranslationMemory Management Unit in hardwareEvery process has its own address space8CPUMMUlogical addrsmemoryPhysical addrsLogical and Physical Addresses9Logical AddressViewPhysical AddressView0000MMUOSScheme 2: DiscussionPro:cheap in terms of hardware: only two registerscheap in terms of cycles: do add and compare in parallelCon: only one segmentprob 1: growing processes. How to expand gcc?prob 2: how to share code and data?? how can Word copies share code? prob 3: how to separate code and data?A solution: multiple segmentssegmentation10gccp2Free spacep3gccWord2Word1SegmentationProcesses have multiple base + limit registersProcesses address space has multiple segmentsEach segment has its own base + limit registersAdd protection bits to every segment11gccText seg r/oStack seg r/w0x1000

0x3000

0x5000

0x6000Real memory0x2000

0x8000

0x6000Base&Limit?How to do the mapping?Types of SegmentsEach file of a program (or library) typically results inAn associated code segment (instructions)The initialized data segment for that codeThe zeroed part of the data segment (bss)Programs also haveOne heap segment grows upwardA stack segment for each active thread (grows down; all but first have a size limit)12Lots of SegmentsBottom line: a single process might have a whole lot of active segments!And it can add them while it runs, for example by linking to a DLL or forking a threadAddress space is a big range with chunks of data separated by gapsIt isnt uncommon for a process on a modern platform to have hundreds of segments in memory13Mapping SegmentsSegment TableAn entry for each segmentIs a tuple Each memory reference indicates segment and offset14Virtual addrSeg# offset 3 128Seg tableProt base len r 0x1000 512memseg128+0x1000?yesnofaultSegmentation ExampleIf first two bits are for segments, next 12 for offset

where is 0x0240?0x1108?0x265c?0x3002?0x1700?15Seg base bounds rw0 0x4000 0x6ff 101 0x0000 0x4ff 112 0x3000 0xfff 113 00 0x4000

0x3000

0x2000

0x1500

0x10000x0700

0x0000logicalphysical0x4700

0x4000

0x3000

0x500

0x0Segmentation: DiscussionAdvantages:Allows multiple segments per processEasy to allow sharing of codeDo not need to load entire process in memoryDisadvantages:Extra translation overhead: Memory & speedAn entire segment needs to reside contiguously in memory! Fragmentation16FragmentationFree memory is scattered into tiny, useless piecesExternal Fragmentation:Variable sized pieces many small holes over timeInternal Fragmentation:Fixed sized pieces internal waste if entire piece is not used17gccemacsdoomstackallocatedUnused (internal fragmentation)ExternalfragmentationWord??PagingDivide memory into fixed size piecesCalled frames or pagesPros: easy, no external fragmentation18gccemacsinternal fragPages typical: 4k-8kMapping PagesIf 2m virtual address space, 2n page size(m - n) bits to denote page number, n for offset within page

Translation done using a Page Table19Virtual addrVPN page offset 3 128 (12bits)page tableProt VPN PPN r 3 1memseg1280x1000((1