Part 3 of 6 · 3 min read
Base and limit
In plain words: Two kinds of address exist. The one the program uses, in its instructions, is the logical address, also called the virtual address. The one that goes onto the memory bus and picks a real location is the physical address. A piece of hardware between the processor and the memory, the memory-management unit (MMU), changes the first into the second on every access.
The simplest scheme uses two registers inside the MMU. The base register holds the physical address where the process's memory begins. The limit register holds the size of that memory. For each access, the hardware does two things:
- It compares the logical address with the limit. If the address is not below the limit, the hardware raises a trap (an error signal to the kernel) and does not touch the memory.
- Otherwise, it adds the base, and uses the sum as the physical address.
Here is a worked example with made-up numbers. Process A has base 40,000 and limit 8,000. Process B has base 60,000 and limit 15,000.
| Process | Logical address | Check against limit | Physical address |
|---|---|---|---|
| A | 100 | 100 < 8,000: ok | 40,000 + 100 = 40,100 |
| A | 7,999 | 7,999 < 8,000: ok | 40,000 + 7,999 = 47,999 |
| A | 8,000 | 8,000 is not below 8,000 | Trap, no access |
| B | 14,999 | 14,999 < 15,000: ok | 60,000 + 14,999 = 74,999 |
| B | 0 | 0 < 15,000: ok | 60,000 |
Process A can reach only the physical addresses 40,000 to 47,999. There is no logical address that A can write which lands in B's range, since the largest sum it can produce is 47,999. This is protection by construction, and it costs one addition and one comparison per access.
The base and limit registers are part of a process's state. At a context switch (Level 1) the kernel loads the registers of the next process. Only the kernel may change them: an instruction that writes them works only in kernel mode, and a user program that tries it causes a trap. Without that rule, a process could widen its own limit.
Relocation is a second benefit. A program can be loaded at any physical address, and only the base register changes. The program itself never changes, and a running process can even be moved: the kernel copies its memory elsewhere and sets a new base.
Base and limit registers
The base and limit registers are a pair of registers in the MMU that describe one contiguous region of physical memory: the base is where the region starts and the limit is its size, and each logical address is checked against the limit and then added to the base.
Example: with base 40,000 and limit 8,000, logical address 100 becomes physical address 40,100, and logical address 8,000 causes a trap.
Check yourself: 1. With base 60,000 and limit 15,000, what is the physical address for logical address 2,500? 2. What does the hardware do for logical address 15,000? 3. Why must the instruction that changes the base register be allowed only in kernel mode?