420 likes | 590 Views
Virtual Caches. กรกฎ คชฤทธิ์ 4422005. Virtual Address. Virtual Address. Physical Address. MMU. Main Memory. Virtual Cache. CPU. Data. Data. Virtual Caches. Virtual Caches. Problems with Virtual Caches Ambiguities Aliases. Physical Address. 0 x0000. 1234. * *. 0 x5000.
E N D
Virtual Caches กรกฎ คชฤทธิ์ 4422005
Virtual Address Virtual Address Physical Address MMU Main Memory Virtual Cache CPU Data Data Virtual Caches
Virtual Caches • Problems with Virtual Caches • Ambiguities • Aliases
Physical Address 0x0000 1234 * * 0x5000 5678 * Ambiguities
Ambiguities Virtual Address 0x1000 Physical Address 0x5000 Maps to Virtual Cache Tag Data * 0x1000 5678 *
Ambiguities Virtual Address 0x1000 Physical Address 0x0000 Maps to Virtual Cache Tag Data * 0x1000 5678 *
Virtual Address Space Physical Address Space 0x0000 0x0000 0x1000 0x1000 0x2000 0x2000 0x3000 0x3000 0x4000 0x4000 0x5000 0x5000 Aliases
Managing a Virtual Cache • Context Switch • Process may use the same virtual address to refer to different physical address before and after context switch • Kernel must flush everything the old process had cached • Flushing a virtual cache can be a time-consuming operation
Managing a Virtual Cache • Fork • the child receive a complete copy of the parent’s address space • Flushing during each context switch eliminates most inconsistencies between parent’s and child’s data • But must consider about during the copy operations
Managing a Virtual Cache • Fork (without copy-on-write) Parent’s Virtual Address Space Data Copied to Child’s Pages Mapping to Parent’s Physical Pages * * * * * * * * * * * * Kernel Virtual Space Temporary Mapping to Child’s Physical Pages for Copy Operation
Managing a Virtual Cache • Fork • If write-through caching is used, No special flushing is needed during fork since at least one context switch must occur before the child executes • If write-back caching is used, Main memory must be validated before temporary mapping is removed
Managing a Virtual Cache • Fork • if fork is implemented with copy-on-write • if write-through, no flushing is needed at fork time • if write-back, main memory must be validated
Managing a Virtual Cache • Exec • Discards the process’s current address space and replaces it with a new one for the new program the process will run • Possible to occur ambiguities • Kernel must invalidated all user data from cache before execution begins
Managing a Virtual Cache • Exit • Address space of process is discarded • Last step of exit is to perform context switch that flushed the cache
Managing a Virtual Cache • Brk and Sbrk • use to grow and shrink the process’s bss segment • growing bss causes no cache problems • shrink bss, kernel must invalidates data from cache
Managing a Virtual Cache • Shared Memory and Mapped Files • no special cache management is needed since the cache is flushed at each context switch • if single process attaches shared memory segment at different address ?
Virtual Address Physical Address Virtual Address Physical Address MMU Main Memory Virtual Cache CPU I/O Data Data Data Managing a Virtual Cache • I/O
Managing a Virtual Cache • I/O • Some problem occurs when using DMA • when write to device, kernel must validate main memory with cache data before DMA starts • when read from device, cache must be flushed
Managing a Virtual Cache • User-Kernel Data Ambiguities • to prevent cached kernel data from user process, cache must be flushed • Apollo DN-4000, add bit indicate user or kernel mode
Virtual Cacheswith Keys กรกฎ คชฤทธิ์ 4422005
Current Process Key Virtual Address Virtual Address Physical Address MMU Main Memory Virtual Cache CPU Data Data Virtual Caches with Keys
Managing a Virtual Cache with Keys • Context Switch • Normally no need to flush the cache as long as enough keys that unique can be assigned to each processes • when reassigning key, all entries tagged with affected key must be flushed
Key Key Tag Tag Data Data 1 2 0x100000 0x100000 1011970 1011970 Managing a Virtual Cache with Keys • Fork • No need to Flush • Some problem
Key Tag Data 1 0x100000 1011970 - - - Key Tag Data 1 0x100000 1011970 2 0x100000 1011970 Managing a Virtual Cache with Keys • Fork • Some problem
Managing a Virtual Cache with Keys • Exec • A unique key must be chosen for new address space • if reassigning key, all entries tagged with affected key must be flushed
Managing a Virtual Cache with Keys • Exit • not necessary to invalidate cache at exit time • cache is flushed during context switch
Managing a Virtual Cache with Keys • Brk and Sbrk • Same as pure virtual cache
Key Key Mod Mod Tag Tag Data Data 2 1 - M 0x100000 0x100000 1011970 1011970 Managing a Virtual Cache with Keys • Shared Memory and Mapped Files • Can not share data within cache
Managing a Virtual Cache with Keys • I/O • Same as pure virtual cache
Managing a Virtual Cache with Keys • User-Kernel Data Ambiguities • Kernel process have special key • User process can not access kernel data • Kernel process can not share user data
Virtual Cache Usage in MMUs • Special-purpose cache, Translation Lookside Buffer (TLB) or Address translation Cache (ATC) • caching most recently used page mapping • speed up virtual-to-physical address translation process • must be flush on each context switch
Virtual Cacheswith Physical Address Tags กรกฎ คชฤทธิ์ 4422005
MMU Physical Address Virtual Address Main Memory Virtual Cache with Physical Tags CPU Data Data Virtual Caches with Physical Tags
Managing a Virtual Cache with Physical Tags • Context Switch • Normally no need to flush the cache • No keys to mange as virtual cache with keys
Managing a Virtual Cache with Physical Tags • Fork • No cache flushing is required for fork • Data can be shared in cache • No aliases can occur
Managing a Virtual Cache with Physical Tags • Exec • Address space not be use by any process • exec deallocates pages, must be ensure that stale data is removed prior to allocating page to new process
Managing a Virtual Cache with Physical Tags • Exit • Can be either flushed at system call or postponed until physical page are reuse
Managing a Virtual Cache with Physical Tags • Brk and Sbrk • Like exec and exit • Can be either flushed at system call or postponed until physical page are reuse
Managing a Virtual Cache with Physical Tags • Shared Memory and Mapped Files • Can share data in cache • Better hit ratio for shared memory
Managing a Virtual Cache with Physical Tags • I/O • Same as pure virtual cache
Managing a Virtual Cache with Physical Tags • User-Kernel Data Ambiguities • No ambiguities between user and kernel data • Kernel can reference cached user data directly