Virtual memory

Function calls

Previously we’ve discussed how function calls use RAM to save state between function calls.

Mostly we talked about using the stack to pass arguments but also the stack is (conceptually) where local variables live.

In reality a lot of state exists in CPU registers and those values also get copied to the stack as part of the function calling machinery.

Process switching

Switching from one processes (a.k.a. program) to another in order to give the illusion of two processes running at the same time also requires saving state so we can resume each process wherever it left off.

  • Registers and flags need to be saved when switching processes.
  • Caches do not need to be saved because the point of a cache is it is just a copy of actual memory.

A note about caches

“There are only two hard problems in Computer Science: cache invalidation and naming things.”

(And off-by-one errors.)

There is a lot of machinery devoted to making caches transparent. But that is mostly at the hardware level.

What about RAM?

The simplest way to deal with RAM is to just give every process a dedicated chunk of RAM and then whatever process is running will access its RAM and other processes’ RAM will sit around waiting for those processes to run again.

However, this means that if we want to run lots of programs simultaneously, we will only be able to give each program a small fraction of the RAM in the computer.

Swapping

One simple way to give every process the illusion of having access to all the RAM is to swap out a process’s memory by writing the full contents of RAM to secondary storage and reading in the previously stored contents of RAM of whatever process is about to start running.

However, as we know, disks are incredibly slow and the whole point of switching between processes is to give the illusion of them running simultaneously.

We need a better idea.

What if chopped a process’s memory up into smaller chunks and treated RAM as kind of cache on top of the disk, pulling in chunks as they’re needed and writing them back out to disk when we need the RAM for something else?

However, now we have a different problem.

Can you see it?

If we’re moving data from disk to RAM as needed, data doesn’t necessarily have a stable location in memory.

Virtual addresses

The solution to this problem is called virtual memory.

The CPU and the operating system conspire to create virtual addresses that programs actually deal with and maintaining a mapping between the virtual addresses for each process and where that data actually is, whether it’s in RAM or on disk.

At the level of CPU we’re dealing with virtual addresses but the memory management unit on the CPU knows how to use tables in memory maintained by the operating system to translate those virtual addresses to physical addresses.

A new important bit of process state

When we switch from one process to another, the CPU has to start using the virtual address mapping for the new process.

This is typically stored in a register that points to the root of the tables that hold the mapping.

TLBs

The CPU also maintains special on-chip caches called Translation Lookaside Buffers (TLB) that cache the translation between virtual and physical addresses so the CPU doesn’t have to always go out to RAM just to find out where some data lives.