Reputation: 5570
Theory:
There are multiple types of CPU cache implementations, depending on the method of accessing cache locations: Physically Indexed Physically Tagged (PIPT), Virtually Indexed Virtually Tagged (VIPT), etc.
In the case of having a VIPT cache, in order to read a cache location, the physical address is needed along side with the virtual address. For an user process this is straight forward: the process uses virtual addresses in its code, that are translated into physical addresses (hopefully by finding them in a TLB cache) and with the 2 addresses the CPU can compute the cache location. The question is about kernel code.
Questions:
When kernel code runs does it use physical or virtual addresses? The kernel is the one to translate from VA to PA (or at least manage the translation). I'm also knowing that a process cannot use the entire VA space because a big chunk of it is reserved for the kernel. So is it possible that the kernel uses VA but only in the 0x7FFFF... to 0xFFFFFF space?
If kernel code uses only physical addresses, how can code or data be cached in a VIPT cache? Does the CPU detect that it's running at highest privilege level and does the cache indexing based on the physical address?
If the kernel code or data gets cached in the end, would it be real beneficial? I'm thinking that most of code running is user space one so any code or data belonging to the kernel would be flushed away rapidly.
Upvotes: 0
Views: 936
Reputation: 171178
The CPU does not know what a kernel is. It just applies the translations as it always does. The kernel is (almost) not concerned with CPU caches either.
CPU caches cache by physical location. All of that is transparent to any software that runs (except if the software explicitly breaks the abstraction by asking the hardware some question about caches and such).
The kernel uses virtual addresses.
Caching kernel code and data is very important. In some BIOSes you can disable the CPU cache. When you do that booting takes hours. Even if the kernel just takes 1% of the CPU time that time would be magnified to be 99%.
Upvotes: 3