Address spaces, binding and protection

Follow one address from a program to a RAM byte, move the process without changing the program, and watch a limit check stop an offset that is too large. Memory management foundations · Operating systems.

Two programs share one machine, and each one reads its own address 217. Neither disturbs the other, and each gets its own data. How can two different bytes both be called 217, and what keeps a program from reaching past its own memory?

Why a program counts its own bytes

When a program is compiled, it usually is not known which part of RAM it will get. RAM is shared, and the free stretch is different every time the program starts. So a program counts its own bytes from 0, as if it owned a private strip of memory. An address counted that way is a logical address, the address as the program sees it (the paging lesson calls it a virtual address; with one strip per process the two names mean the same thing). The position of the byte in RAM is its physical address. In this lesson every physical address is a position in RAM.

Something must turn one into the other on every single access. The simplest rule keeps two numbers for each process, and while the process runs they sit in two CPU registers. The base is the physical address where the process's strip begins. The limit is the length of the strip, the number of bytes the process may use. A logical address is then an offset, the distance from the start of the strip.

Take a process P with base 4000 and limit 500. Its offsets run from 0 to 499, and they sit at RAM bytes 4000 to 4499. When P reads offset 217, the hardware adds the base: 4000 + 217 = 4217. The hardware that does this on every access is the memory-management unit (MMU).

Moving a process

Suppose the OS wants the bytes at 4000 for a larger program, and RAM bytes 9000 to 9499 are free. Can P move there? That depends on binding, the moment when a program's addresses are tied to physical locations. There are three such moments.

  • Compile time. The compiler writes real RAM addresses into the code, so P's read of its byte 217 says 4217. The program can run only at 4000. Copied to 9000, it would still read 4217, which may now belong to another program.
  • Load time. The compiler writes offsets, and the loader adds the starting address when it places the program. Loaded at 4000 the code says 4217, and loaded at 9000 it says 9217. After that the addresses are fixed, so moving the running program later requires finding and adjusting its physical addresses, including any held in its state.
  • Run time. The code keeps its offsets for its whole life, and the hardware adds the base on every access. Once the OS has paused the process and found a free range of the same size, moving it is a copy and one register change.

Base and limit registers give us run-time binding. The OS copies P's 500 bytes to RAM bytes 9000 to 9499 and sets the base to 9000. The program is not touched. Its offset 217 now reaches 9000 + 217 = 9217.

An offset, then a move

Read the steps as text

  1. The CPU's registers hold P's pair: base 4000 and limit 500. P's addresses 0 to 499 sit at RAM bytes 4000 to 4499.
  2. P reads offset 217. The limit check comes first: 217 < 500, so it passes. Then the base is added: 4000 + 217 = 4217.
  3. The OS copies P to RAM bytes 9000 to 9499 and sets P's base to 9000. Its limit stays 500 and its addresses still run from 0 to 499.
  4. P reads offset 217. The limit check comes first: 217 < 500, so it passes. Then the base is added: 9000 + 217 = 9217. Earlier, P's offset 217 landed at 4217; now P's offset 217 lands at 9217.
  5. Finished with 2 accesses completed, 0 traps and 1 move.

The program never noticed the move. Offset 217 is the same number before and after, and only where it lands changed. Moving a process this way is called relocation.

Why does the move need run-time binding? The table follows the same copy of P from 4000 to 9000 under each of the three binding times.

Three binding times, one copy to 9000

Read the steps as text

  1. Watch compile time first: the code holds the RAM address 4217, so after the copy it still reads 4217, the old spot.
  2. Watch load time: it wrote 4217 when the program was placed. Nothing updates it during the copy, so it still reads 4217 until the addresses are patched.
  3. Watch run time: it keeps the offset 217 in the code and the hardware adds the new base: 9000 + 217 = 9217. Only this row follows the process.
  4. Watch the three rows side by side. Compile time and load time read the old spot; run time reads 9217 with the program untouched.

Compile time and load time leave the code pointing at 4217; only run time reaches the moved bytes at 9217.

The limit check

For this new example, P is back at base 4000. Now P writes to offset 640. Its limit is 500, so it has no byte 640. Adding the base blindly would give 4000 + 640 = 4640, an ordinary RAM address that probably belongs to someone else. So the hardware checks first: the offset must be below the limit. Offset 480 passes, because 480 < 500, and it reaches 4000 + 480 = 4480. Offset 640 fails, because 640 is not below 500.

A failed check stops the access before any addition. The hardware raises a trap, which hands control to the kernel. No memory was read or written, and the OS normally ends the program.

An offset past the limit

Read the steps as text

  1. The CPU's registers hold P's pair: base 4000 and limit 500. P's addresses 0 to 499 sit at RAM bytes 4000 to 4499.
  2. P reads offset 480. The limit check comes first: 480 < 500, so it passes. Then the base is added: 4000 + 480 = 4480.
  3. P writes offset 640. The limit check comes first: 640 is not below 500, so the hardware stops the access before any addition and traps to the OS. No memory is touched. The OS rejects it. By default the OS ends P.
  4. Finished with 1 access completed, 1 trap and 0 moves.

Offset 499 passes and offset 500 traps, because the limit is a count of bytes and not the last offset.

The last offset that passes

Read the steps as text

  1. The CPU's registers hold P's pair: base 4000 and limit 500. P's addresses 0 to 499 sit at RAM bytes 4000 to 4499.
  2. P reads offset 499. The limit check comes first: 499 < 500, so it passes. Then the base is added: 4000 + 499 = 4499.
  3. P reads offset 500. The limit check comes first: 500 is not below 500, so the hardware stops the access before any addition and traps to the OS. No memory is touched. The OS rejects it. By default the OS ends P.
  4. Finished with 1 access completed, 1 trap and 0 moves.

Two processes, one address

Back to the opening question. A second process Q has base 7000 and limit 300, so its strip is RAM bytes 7000 to 7299. Each process has its own pair of numbers, and the OS loads the right pair into the registers whenever it switches to that process. Only the OS may write these registers. If a program could set its own limit, it could give itself more memory and the check would protect nothing.

While P runs, offset 217 reaches 4217, and offset 400 is below its limit of 500 and reaches 4000 + 400 = 4400. After the switch, Q's offset 217 reaches 7000 + 217 = 7217. Two bytes, both called 217, and neither process can touch the other's. The limit belongs to the process as well: offset 400 passed for P, but Q's limit is 300, so for Q it traps.

Two processes, one offset

Read the steps as text

  1. The CPU's registers hold P's pair: base 4000 and limit 500. P's addresses 0 to 499 sit at RAM bytes 4000 to 4499. The other process, Q, has its own pair.
  2. P reads offset 217. The limit check comes first: 217 < 500, so it passes. Then the base is added: 4000 + 217 = 4217.
  3. P reads offset 400. The limit check comes first: 400 < 500, so it passes. Then the base is added: 4000 + 400 = 4400.
  4. The OS switches to Q: the CPU's base and limit registers now hold Q's pair, base 7000 and limit 300.
  5. Q reads offset 217. The limit check comes first: 217 < 300, so it passes. Then the base is added: 7000 + 217 = 7217.
  6. Q reads offset 400. The limit check comes first: 400 is not below 300, so the hardware stops the access before any addition and traps to the OS. No memory is touched. The OS rejects it. By default the OS ends Q.
  7. Finished with 3 accesses completed, 1 trap and 0 moves.

Recap

  • A logical address is an offset from the start of a process's strip. The hardware adds the process's base to it, so two processes can both use 217 and reach different RAM bytes.
  • Compile-time and load-time binding fix physical addresses before the program runs. With run-time binding the OS can copy a process and change only its base, and the program's own addresses never change.
  • In this model the limit check comes before the addition. An offset that is not below the limit traps to the OS, and no memory is touched.
Go deeper

Real MMUs. Modern systems rarely give a process one base and one limit. Paging cuts the strip into equal pages that can sit anywhere in RAM, as in the paging lesson, and segmentation gives code, data and stack each their own base, limit and permissions, two lessons ahead. Segmentation keeps this order: the offset is checked against the segment's limit before the base is added. Paging checks validity and permissions on the page-table entry as part of the lookup.

Details this lesson leaves out. Some hardware states the limit as the largest legal offset instead of the length, which shifts the comparison by one but not the idea. Some designs compare the physical address with the base and with the base plus the limit instead of checking the offset first, and the outcome is the same: an access outside the range traps. A physical address can also name a device instead of RAM. Entering the kernel on a trap changes which instructions the CPU may run, but it does not move the process.

Next: Contiguous allocation and fragmentation.