Search code examples
cachingmemoryvirtual-memorytlb

Does all the data in Cache have to be in Primary Memory?


I was studying Virtual Memory, TLB and their relationship with Cache:

Source

And I don't understand the reason behind the last sentence ("Impossible – data not allowed in cache if page is not in memory"), would appreciate if someone could explain me.


Solution

  • When the operating system swaps out a virtual memory page, all cache entries belonging to the physical page allocated to the virtual page must be flushed. If this was not so, the process for which the physical page is allocated next could get cache hits when it shouldn't - and read data from another process' memory! This is an obvious security hazard, and therefore it is indeed an impossible situation to have data belonging to a swapped-out page in the cache.

    However, even if the data was still in the cache, it would be impossible to get a cache hit in such a situation. This is because cached data is generally tagged by its physical address, and, in the absence of a valid virtual-to-physical memory address translation, you would be unable to access the correct cache entry.