# L3 cache mapping on Sandy Bridge CPUs

DevFeed: [L3 cache mapping on Sandy Bridge CPUs](<https://devfeed.tech/articles/l3-cache-mapping-on-sandy-bridge-cpus-21570.md>)

Original publisher: [Read original article](<http://lackingrhoticity.blogspot.com/2015/04/l3-cache-mapping-on-sandy-bridge-cpus.html>)

Author: Mark Seaborn (noreply@blogger.com)

Published: 2015-04-27T21:59:00Z

Content type: article

Language: en

Sources: [Mark Seaborn](<https://devfeed.tech/sources/mark-seaborn.md>)

Topics: [CPU Cache](<https://devfeed.tech/topics/cpu-cache.md>), [Cache](<https://devfeed.tech/topics/cache.md>), [Cache Coherence](<https://devfeed.tech/topics/cache-coherence.md>)

Tags: [2013](<https://devfeed.tech/tags/2013.md>), [aslr](<https://devfeed.tech/tags/aslr.md>), [attacks](<https://devfeed.tech/tags/attacks.md>), [bits](<https://devfeed.tech/tags/bits.md>), [bus](<https://devfeed.tech/tags/bus.md>), [cache](<https://devfeed.tech/tags/cache.md>), [cpus](<https://devfeed.tech/tags/cpus.md>), [distributed](<https://devfeed.tech/tags/distributed.md>), [dram](<https://devfeed.tech/tags/dram.md>), [intel](<https://devfeed.tech/tags/intel.md>), [kernel](<https://devfeed.tech/tags/kernel.md>), [l3-cache](<https://devfeed.tech/tags/l3-cache.md>), [map](<https://devfeed.tech/tags/map.md>), [mapping](<https://devfeed.tech/tags/mapping.md>), [memory](<https://devfeed.tech/tags/memory.md>), [ring](<https://devfeed.tech/tags/ring.md>), [rowhammer](<https://devfeed.tech/tags/rowhammer.md>), [side-channel](<https://devfeed.tech/tags/side-channel.md>), [side-channel-attacks](<https://devfeed.tech/tags/side-channel-attacks.md>), [size](<https://devfeed.tech/tags/size.md>), [slices](<https://devfeed.tech/tags/slices.md>)

## AI overview

On Sandy Bridge, physical address bits 6-16 select one of 2,048 L3 cache sets within a slice, bits 17-31 are used to hash the address to a slice, and bits 0-5 give the offset within a 64-byte cache line; bits 32 and above are unused. The cache has one slice per core, connected by a ring bus, and accessing another core's slice adds one or two hops. Slices are 1.5 MB with 12-way associativity on lower-end CPUs and 2 MB with 16-way associativity on higher-end CPUs. Hund, Willems, and Holz published the mapping for four-core CPUs; the source author reports deriving it for two-core CPUs. The excerpt gives the beginning of the four-core slice hash equations but cuts off before completing them.

## Source excerpt

In 2013, some researchers reverse-engineered how Intel Sandy Bridge CPUs map physical addresses to cache sets in the L3 cache (the last-level cache). They were interested in the cache mapping because it can be used to defeat kernel ASLR. I'm interested because the cache mapping can be used to test whether cached memory accesses can do row hammering (which can cause exploitable bit flips in some DRAM devices). The researchers published the details in the paper "Practical Timing Side Channel Attacks Against Kernel Space ASLR" (Ralf Hund, Carsten Willems and Thorsten Holz). They only published the mapping for 4-core CPUs, but I have figured out the mapping for 2-core CPUs as well. Some background: On Sandy Bridge CPUs, the L3 cache is divided into slices. Physical addresses are hashed to determine which slice of the L3 cache they will be stored in. The L3 cache is distributed and ring-based. There is one slice per core, but all the cores in a CPU can access all the cache slices via a ring bus which connects all the cores and their caches together. When a core accesses a memory location, the location will be slightly slower to access if it maps to a different core's cache slice, because it would take one or two hops around the ring bus to access it. The protocol used on the ring bus is based on QPI (Intel's QuickPath Interconnect). (QPI is a protocol used for connecting multiple CPUs together on high-end multi-socket systems.) Each cache slice contains 2048 cache sets. On lower-end CPUs, cache sets are 12-way associative, so a cache slice is 1.5MB in size (2048 sets * 12 ways * 64 bytes per cache line = 1.5MB). On higher-end CPUs, cache sets are 16-way associative, so a cache slice is 2MB in size (2048 sets * 16 ways * 64 bytes per cache line = 2MB). Cache mapping The researchers (Hund et al) figured out that the L3 cache uses the bits of a physical address as follows: Bits 0-5: These give the 6-bit byte offset within a 64-byte cache line. Bits 6-16: These give the 11-b