In a physically indexed cache, the cached location is looked up using the physical
address of the memory location, and as such one physical memory address will
always be found through the same index and no aliasing can occur.
In contrast, a virtually indexed cache will use the virtual address as seen by
the running program as the cache lookup index. This has some obvious benefits
for CPU design: the physical address need not even be computed before the cache
indexing can take place, and cache lookups can be made faster and can happen in
parallel with the virtual to physical address translation.
However, the use of virtual caches directly lead to aliasing problems as different
virtual addresses can point to the same physical memory location. As a result, the
different virtual addresses can result in different cache locations being used to cache
the same memory, resulting in the above-mentioned aliasing problem [IKWS92].
It should be noted that virtual indexing and the resulting aliasing is not a prob-
lem for instruction caches: while the aliasing may result in slightly lower utilization
of the cache, this is often balanced by the simpler lookup, and because instruc-
tion caches do not modify their contents the worst effects of the aliasing problem
never materialize. In fact, virtual instruction caches can often make the issue of
instruction cache invalidation (see 4.3.1) simpler for the operating system.
However, in the case of data caches, the aliasing issues either require extra anti-
aliasing hardware or it has to be handled by proper support by the operating system.
As the reason for the virtual indexing in the first place was simplicity of hardware,
it is generally left to the operating system to clean up the mess.
The most trivial example of a dual virtual mapping is the case when a page
is available both in kernel space and in user space. As the kernel address space
generally contains all physical memory, this happens with any user page. Happily,
only in a few cases do these mappings actually conflict in the cache.
The reason that the double kernel-user mapping normally does not lead to alias-
ing problems is that in most cases a user process gets full control of any page it uses
and the kernel never reads or writes to that page through the kernel address. So
while the mapping exists both in user space and kernel space, this aliasing is not a
problem. Care must be taken that the kernel always uses the user virtual address
for any data copies, but as that is the normal mode of operation anyway the issue
is moot (see later about memory management issues in Chapter 6).
Even though most common memory mappings do not result in aliasing problems,
there are a few cases where this is not true, notably with memory mapped files.
When a user process asks to map a file into user memory, the user-accessible page
35