Β§15.1–15.2Assumptions … An Example

Part I OSTEP pp. 141–144 Β· ~5 min read

  • address translation
  • interposition

CPU virtualization ran on limited direct execution: let the program run directly on the hardware, but interpose at key moments to keep control. Memory virtualization pursues the same bargain β€” efficiency (with hardware help, starting from just a few registers), control (no process touches memory that isn’t its own), plus one new demand: flexibility β€” programs should use their address spaces however they like.

The Crux: How To Efficiently And Flexibly Virtualize Memory

How can we build an efficient virtualization of memory? How do we provide the flexibility needed by applications? How do we maintain control over which memory locations an application can access? How do we do all of this efficiently?

The generic technique β€” the addition to LDE β€” is hardware-based address translation : on each and every memory reference (instruction fetch, load, or store), the hardware transforms the virtual address into the physical address where the data actually lives. Hardware provides the fast mechanism; the OS manages memory (tracking free and used) and steps in at the key moments. The goal, as ever: the beautiful illusion of private memory, hiding the ugly truth of many programs sharing it.

Tip: Interposition is powerful

Interposition β€” inserting yourself at a well-defined interface β€” is a broadly applicable systems technique. Here the hardware interposes on every memory access to translate it; in general, almost any interface can be interposed upon to add functionality, usually with the bonus of transparency: the client needn’t change at all.

15.1 Assumptions

First attempts will be simple β€” β€œlaughably so. Go ahead, laugh all you want; pretty soon it will be the OS laughing at you, when you try to understand the ins and outs of TLBs and multi-level page tables. That’s just how the OS rolls.”

THREE (TEMPORARY) ASSUMPTIONS

  1. Each address space is placed contiguously in physical memory (relaxed by segmentation, ch. 16)
  2. Every address space is smaller than physical memory (relaxed by swapping, ch. 21)
  3. All address spaces are exactly the same size (relaxed almost immediately, ch. 16–17)

15.2 An Example

Take the humblest line of C and watch what memory work it causes:

void func() {
    int x = 3000; // thanks, Perry.
    x = x + 3;    // line of code we are interested in
    ...

The compiler emits three x86 instructions (disassemble with objdump on Linux, otool on a Mac). The address of x waits in %ebx:

128: movl 0x0(%ebx), %eax    ;load 0+ebx into eax
132: addl $0x03, %eax        ;add 3 to eax register
135: movl %eax, 0x0(%ebx)    ;store eax back to mem

Here’s where everything lives in the process’s 16KB view:

Figure 15.1: a process and its address space β€” click the regions the example touches
Program Code β“˜0KBHeap β–Ύβ–Ό2KB(free)4KBStack β–΄ (x = 3000 at 15KB) β“˜β–²14KB16KB

click a region marked β“˜ for details

Running those three instructions produces six memory accesses, from the process’s perspective:

#accessvirtual addresswhy
1fetch128the movl instruction itself
2load15KBread x (3000)
3fetch132the addl instruction
β€”executeβ€”register-only: no memory reference
4fetch135the second movl
5store15KBwrite 3003 back to x

(Instruction fetches count! Chapter 2 warned that memory is touched on every fetch β€” here it’s 3 of the 5 accesses.)

Now the problem. The program believes its space runs 0–16KB, and every reference it generates will be in that range. But the OS wants to place it somewhere else β€” say at physical 32KB:

Figure 15.2: physical memory with a single relocated process
Operating System β“˜0KB(not in use) β“˜16KBRelocated Process (code, heap … stack) β“˜32KB(not in use) β“˜48KB64KB

click a region marked β“˜ for details

How can the process be relocated transparently β€” the illusion of an address space at 0, while the truth is 32KB? Every one of those six accesses must be quietly redirected, at full speed. The 1950s answer β€” two little registers β€” is next.

Check yourself

1.How is address translation the memory-side twin of limited direct execution?

2.Executing the three instructions of x = x + 3 causes how many memory accesses?

3.Which of these is one of the chapter's three (temporary) simplifying assumptions?

4.The process is placed at physical 32KB but generates addresses 0–16KB. State the core problem this chapter must solve.

4 questions