4 Address Spaces Translation from logical to physical addresses 2N Process 1Process 3Process 2OS2N2NAddress space 32NAddress space 2Virtual/Logical Address spacesPhysical Address space
5 Hardware Support Two operating modes Segmentation (Logical addressing) Privileged (protected, kernel) mode: OS contextResult of OS invocation (system call, interrupt, exception)Allows execution of privileged instructionsAllows access to all of memory (sort of)User Mode: Process contextOnly access resources (memory) in its context (address space)Segmentation (Logical addressing)Base register: Start location for address spaceLimit register: Size of segment making up address space
6 Implementation Translation on every memory access in user process Compare logical address to limit registerIf logical address is greater, ERRORPhysical Address = base register + logical address
7 Managing Processes w/ Base and Limits Context SwitchAdd base and limit registers to process contextContext Switch stepsChange to privileged modeSave base and limit registers of old processLoad base and limit registers of new processChange to user mode and jump to new processProtection RequirementUser process can not change base and limitUser process can not run in privileged modeWhat if base and limit registers don’t change during context switch?
8 Pros and Cons of Segmentation AdvantagesSupports dynamic relocation of address spacesSupports protection across multiple address spacesCheap: Few registers and little logicFast: Add and Compare is easyDisadvantagesEach process must be allocated contiguously in real memoryFragmentation: Cannot allocate a new processMust allocate memory that may not be usedNo Sharing: Cannot share limited memory regions
9 Using Segments Divide address space into logical segments Each logical segment can be in separate part of physical memorySeparate base and limit for each segment (+ protection bits)Read and write bits for each segmentHow to designate segment?Use part of logical addressTop bits of logical address select segmentLow bits of logical address select offset within segmentImplicitly by type of memory referenceCode vs. Data segmentsSpecial registers
10 Segment TableSegment Table: Base and limit for every segment in processTranslation: Indirection -> Table lookup before Add and CompareSegmentBaseLimitR/W0x40000x06FF1 010x00000x04FF1 120x30000x0FFF0x3800Seg 20x3000Where is:0x02400x11080x265c0x30020x2a00Seg 10x20000x1000Seg 0Caveat:Assume segments are selected via the logical address.NOT A REAL SYSTEM0x0000LogicalPhysical
11 Pros and Cons of Segmentation AdvantagesDifferent protection for different segmentsE.g Code segment is read onlyEnables sharing of selected segmentsEasier to relocate segments than entire address spaceEnables sparse allocation of address spaceDisadvantagesStill expensive/difficult to allocate contiguous memory to segmentsFragmentation: Wasted memoryNext approach: PagingAllocation is easierReduces fragmentation
13 x86 Segments CS = Code Segment DS = Data Segment SS = Stack Segment ES, FS, GS = Auxiliary segmentsExplicit or implicitly specified by instructionsAccessed via special registers16 bit “Selectors”Identify the segment to the hardware MMUFunctionality depends on CPU operating mode
14 Memory Map Early PC’s depended on BIOS for hardware interactions Standard libraryImplemented as Real Mode code(16 bit instructions)Hardwired directly into memoryAll x86 CPUs start execution at 0xffff0Where is that?1MB of available memoryOn a 16 bit architecture?
15 Real Mode (16 bits) Segment registers act as base address Translation: Segment size = 64K (216)Translation:Physical Addr = (seg addr << 4) + logical addrx86 init values:CS: 0xf000IP: 0xfff0Goal when in Real Mode:Get Out of Real ModeFirst thing OS does is transition to Protected (32 bit) mode
16 32 bit Memory Map 32 bit addresses BIOS is still there Up to 4GB (232) Top of memory used by hardware again“Who would ever need more than 3GB of memory?”BIOS is still thereWhy?Is it still useful?
17 Protected Mode (32 bits) Segment information now stored as a table GDT (Global Descriptor Table)Where is the GDT?Array of segment descriptions (base, limit, flags)Segment registers now indicate array indexSegment registers select segment descriptorCS points to Code Segment descriptor in GDTStill 16 bitsHow does Linux use segments?Check architecture manuals
21 Paging Memory divided into fixed-sized pages Typical page size: k bytesFree PageAddress space 1Address space 2Address space 3Virtual MemoryPhysical Memory
22 Page Translation 4K Pages How are virtual addresses translated to physical addressesUpper bits of address designate page numberPage NumberPage OffsetPage Base AddressPage TableVirtualAddressPhysical20 Bits12 Bits4K PagesNo comparison or addition: Table lookup and bit substitution1 page table per process: One entry per page in address spaceBase address of each page in physical memoryRead/Write protection bitsHow many entries in page table?
23 Page Table Example Mapping of virtual addresses to physical memory Free PageAddress space 1Physical Memory0x30000x20004KB Pages0x10000x0000Page Table for process 1Base AddressProtection11 041 1610
24 Advantages of Paging Fast to allocate and free Alloc: Keep free list of pages and grab first page in listNo searching by first-fit, best-fitFree: Add page to free listNo inserting by address or sizeEasy to swap-out memory to diskPage size matches disk block sizeCan swap-out only necessary pagesEasy to swap-in pages back from disk
25 Disadvantages of Paging Additional memory reference -> InefficientPage table too large to store as registers in MMUPage tables kept in main memoryMMU stores only base address of page tableStorage for page tables may be substantialSimple page table -> Require entry for all pages in address spaceEven if actual pages are not allocatedSolution: Hierarchical page tablesIncrease granularity of page table entriesInternal fragmentation: Page size does not match allocation sizeHow much memory is wasted (on average) per process?Wasted memory grows with larger pages
26 Paging with Large Address Spaces Mapping of logical addresses to physical memoryPage table for processBase AddressProtection1 0141 1… skipped entries..0 0610Free PageFree PagePhysical MemoryHow are entries skipped?
27 Combine paging and segmentation StructureSegments correspond to logical units: code, data, stackSegments very in size and are often largeEach segment contains one or more (fixed-size) pagesBut no longer needs to be contiguousMultiple ways to combine them:System 370: Each segment got own page tablesSeg #(4 bits)Page #(8 bits)Page offset(12 bits)Why 12 Bits?x86: First calculate segment offset then do page table lookuplogical address -> linear address -> physical address
28 Segments + Pages Advantages Advantages of SegmentsSupports large memory regionsSingle entry can cover all memoryTranslation is fast and cheapAdvantages of PagingMemory does not have to exist (on demand)Can remap memory without copyingAdvantages of bothCan use protection of segments without preallocating memoryOther advantages?
29 Protected Mode + Paging Segmentation -> Paging -> Physical addressEvery address in a page table points to a physical addressVirtual addresses are only an INDEX into page tablesPage size: 4KBData and page table pagesPage table page?1024 entries per page table page2 Level Page TablesPage tables set via CR3 (What is this?)Top Level: Entire 4GB of virtual address space2nd level: 4MB of virtual address spaceLarge PagesContiguous mappings of virtual addresses to physical addresses
32 Long Mode (64 bits) Segments no longer used Addresses now 64 bits Present but must be set to a flat modelAddresses now 64 bitsBut pages are still 4KBPage table hierarchy now has 4 levelsCheck architecture manualsPage table pages now only include 512 entriesLast level page table only covers 2MB of addresses
33 Early Memory (un)management A history of the x86 Simple layout with a single segment per processEarly batch monitorsPersonal computersDisadvantagesOnly one process can run at a timeProcess can destroy OS2NOSOS resides inHigh memoryUserProcessProcess has memory0 to OS break
34 Goals for Multiprogramming SharingSeveral processes coexist in main memoryTransparencyProcesses not aware memory is sharedRun regardless of number and/or locations of processesProtectionCannot corrupt OS or other processesPrivacy: Cannot read data of other processesEfficiency should not be severely degradedPurpose of sharing is to increase efficiencyCPU and memory resources not wasted
35 Static Relocation Transparency == Relocation Advantages Processes can run anywhere in memoryCan’t predict in advanceModify addresses statically (ala linking)when process is loadedAdvantagesAllow multiple processes to runRequires no hardware supportOSProcess 3Process 2Process 12N
36 Disadvantages of Static Relocation Process allocation must be contiguousFragmentation: May not be able to allocate new processWhat Kind?Processes may not be able to increase address spaceCan’t move process after it has been placedNo Protection:Destroy other processes and/or OSOSProcess 3Process 2Process 12N
37 Dynamic Relocation Address space: View of memory for each process Translate address dynamically at every referenceCPUMMUMemoryPhysicalAddressesLogicalProgram-generated address translated to hardware addressProgram addresses: Logical or virtual addressesHardware addresses: Physical or real addressesAddress space: View of memory for each process
38 Managing Processes with Segments Process CreationFind contiguous space for each segmentFill in each base and limit value in segment tableAdditional memory allocation when no contiguous spaceCompact memory (move all segments, update bases)Swap one or more segments to diskContext SwitchInclude segment table in process contextProcess ExitFree segments