ยง15.3โ€“15.4Dynamic Relocation โ€ฆ Hardware Support

Part I OSTEP pp. 145โ€“148 ยท ~4 min read

  • base register
  • bounds register
  • dynamic relocation
  • static relocation
  • mmu

The 1950s answer to transparent relocation โ€” born in the first time-sharing machines โ€” is base and bounds, also called dynamic relocation .

15.3 Dynamic (Hardware-Based) Relocation

Two hardware registers per CPU: the base and the bounds (or limit). Programs are compiled as if loaded at address zero; when one starts, the OS picks its physical home and sets the base. From then on, every reference is translated by the processor:

physicalย address=virtualย address+base\text{physical address} = \text{virtual address} + \text{base}

Trace the running example โ€” the process from Figure 15.1, relocated to 32KB:

The chapter's running example: base = 32KB, bounds = 16KB โ€” try the fetch at 128 and the load at 15KB (15360)

virtual address

128

โ†’

MMU (base & bounds)

1. bounds check:ย 0 โ‰ค 128 < 16384 โœ“

2. add base: 128 + 32768 = 32896

โ†’

physical address

32896

The fetch of instruction 128 becomes physical 32896; the load of x at 15KB (15360) becomes 47KB (48128). Try 16500 โ€” beyond the 16KB bounds โ€” and watch the exception. Then edit base to 48KB: same process, new home, zero recompilation. That is dynamic relocation.

The bounds register supplies the protection: before (or after โ€” both styles are equivalent) adding the base, the processor checks the virtual address is legal. Greater than bounds, or negative โ†’ the CPU raises an exception and the OSโ€™s handler runs (the process, most likely, dies). Note where this circuitry lives: the part of the CPU that helps translate addresses is the memory management unit (MMU) โ€” two registers and an adder today, TLBs and page-table walkers as the chapters roll on.

Aside: Software-based (static) relocation

Before hardware helped, a loader did a crude version in software โ€” static relocation : rewrite every address in the executable at load time (movl 1000 becomes movl 4000 when loading at offset 3000). Two fatal flaws: no protection โ€” a bad address still lands anywhere, and true protection wants hardware โ€” and once placed, an address space is painful to move. Dynamic relocation fixes both: translation happens at runtime, and the OS can even relocate a descheduled process by copying it and updating one saved register.

Now the bookโ€™s worked translations โ€” a tiny 4KB space at physical 16KB:

The book's example translations: a 4KB address space loaded at physical 16KB

virtual address

0

โ†’

MMU (base & bounds)

1. bounds check:ย 0 โ‰ค 0 < 4096 โœ“

2. add base: 0 + 16384 = 16384

โ†’

physical address

16 KB (16384)

0 โ†’ 16KB ยท 1KB โ†’ 17KB ยท 3000 โ†’ 19384 ยท 4400 โ†’ fault (out of bounds). A virtual address is rightly viewed as an OFFSET into the address space; the base slides that offset to wherever the space really lives.

15.4 Hardware Support: A Summary

Everything the hardware must supply for this simplest of MMUs:

Figure 15.3: Dynamic relocation โ€” hardware requirements (click rows for notes)
why it's needed
Privileged moderestrict user-mode processes
Base + bounds registersone pair per CPU, in the MMU
Translate + check circuitryadd base, compare against bounds
Privileged update instructionsonly the OS may set base/bounds
Privileged handler registrationOS tells hardware what to run on faults
Exception raisingstop the program, run the OS handler
Dotted-underlined cells have explanations โ€” click one.

Aside: Data structure โ€” the free list

The OS must track which physical memory is unused so it can place new address spaces. The simplest structure: a free list of unused ranges. (In our same-size-slots world itโ€™s trivial; chapter 17 is what happens when sizes vary and the free list becomes a genuine engineering problem.)

Hardware ready; but registers donโ€™t set themselves. What the OS must do โ€” at creation, termination, context switches, and faults โ€” completes the picture next.

Check yourself

1.Base = 32KB. The process fetches the instruction at virtual address 128. What does the memory system see?

2.With bounds = 4KB, the process references virtual address 4400. What happens, step by step?

3.Why must the instructions that modify base/bounds be privileged?

4.Static relocation (the loader rewrites addresses at load time) fails on two counts. Which two?

5.How can the OS move a process's entire address space to a new physical location under base/bounds?

5 questions