# Does MMU contain and manage CPU caches?

I read about Virtual Memory in various CS references, however I'm still not sure whether CPU caches such as TLB, L1 and L2 are integrated within MMU. Also I don't know if the hit/miss operations are managed by hardware (i.e MMU) or by software (i.e OS).

Does anyone have any idea?

First the term MMU refers to many different thing (more on later below). For now let's defined MMU as "virtual to physical address translation".

The caches L1 and L2 are always managed in hardware, however note that for proper operation the OS still has to interact with the cache on some occasions (notably when using DMA). An important point to be aware of is that depending on whether the caches are virtually or physically indexed, they have a different relationship with the MMU. Indeed a virtually indexed cache (typically L1) will usually be accessed by the CPU without going through the MMU and it will not only contain the data but also the security bits associated with the MMU (privilege bit, read/write, cacheability, etc). On the other hand, a physically indexed cache must come after the address translation and does not care about the MMU at all. This also applies to higher levels (L3 and L4) which are always physically tagged (and sometimes outside the CPU die).

Thus a typical CPU will have the core with a memory bus connected to the L1 (which is typically split between data and instruction, but this is not important here). On cache miss, the L1 will issue read/write on a bus that goes through the MMU then L2, L3, L4 and finally DRAM.

The MMU itself may consist of different things. The simplest forms of MMU are just segmentation and PMU (protection memory unit). More advanced forms of MMU include a TLB so as to support paging. The TLB itself is always a hardware component (essentially just another cache). However there are two very different ways of managing the TLB:

• in hardware: the MMU has a "page table walker" that can issue read to the memory to load TLB entries on a miss (this includes x86, ARM, etc)
• in software: the MMU triggers a CPU exception on a TLB miss, and then the OS does the page table walk in software, fills the TLB with the entry and return from the exception (this includes MIPS)
• I didn't understand what do you mean by "virtually or physically indexed". – user6039980 Jan 9 '18 at 22:37
• That means whether the cache works on virtual addresses or physical addresses. The other answer by @TEMLIB gives more details. – Amaury Pouly Jan 9 '18 at 23:02
• Alright, understood. – user6039980 Jan 10 '18 at 1:32

It depends. Low level caches and MMUs are usually quite interdependant, because they are directly in the paths of data and instruction busses.

Depending on applications, some CPUs can have a cache and no MMU (small microcontrollers) or a MMU and no cache (historical : i386, MC68030)

The MMUs deal mostly with addresses (from virtual to physical), whereas caches associate addresses ranges with data. Whether the cache is placed before, after or in between the MMU gives for cache tags and indexes either virtual or physical addresses. Virtual caches can have lower latency but generate coherency problems, whereas physical caches (placed dowstream from the MMU) have higher latency but simpler management.

I AM SIMPLIFYING A LOT

• TLBs are inside the MMU. They are sometimes called "address translation caches", but are not part of the caches which stores data.
• For the caches, hit/miss is managed mostly by hardware, but some special software assistance is sometimes needed for optimisation (prefetching) or to manage coherency (for example a peripheral doing direct memory accesses in a system where the cache is not automatically made coherent through hardware mechanism)

• For the MMU, some CPU families (ISA="instruction set architecture") use hardware management of TLBs, which are automatically fetched from global tables in RAM, whereas some other families uses software loading.

• Hardware : x86, ARM, MC68K
• Software : MIPS, Sparc64
• Some have a bit of both : PowerPC.
• "For the caches, hit/miss is managed mostly by hardware" this applies to TLB as well? – user6039980 Jan 9 '18 at 22:33
• The MMUs deal mostly with addresses (from virtual to physical), whereas caches associate addresses ranges with data: I don't understand what you mean ranges with data? – Kindred Dec 9 '18 at 22:51