Skip to main content

Lesson 8 of 26 · 12 min read

Base-and-bounds relocation and segmentation

Trace hardware base-and-bounds registers, generalized three-segment translation, negative-growing stack math, and why variable segments fragment RAM.

Four stages of hardware relocation: Base + Bounds, 3-segment bits, Negative stack, and External fragmentation.
Base/bounds pairs relocate code, heap, and stack independently before external fragmentation forces a shift to paging.
Course syllabus · lesson 8 of 26
In this lesson

A process issues virtual addresses for instruction fetches, loads, and stores. The operating system chooses where the corresponding bytes live, and a memory management unit (MMU) translates the addresses and checks permissions. Asking the kernel to translate each access would make ordinary execution prohibitively expensive. Base-and-bounds relocation puts the check and addition into hardware while keeping placement decisions in the kernel.

Top 2 bits of virtual address pick segment 01 (Heap), bounds register validates offset against segment size, base register shifts offset to physical address, while stack uses negative offsets and variable sizes cause external fragmentation.
Open full-size diagram
Segmentation gives each logical region its own base and bounds pair, eliminating dead gap RAM while exposing external fragmentation.

Translate and check a segment offset

Virtual code +24, Segment code, Physical —, Bounds —Virtualcode +24SegmentcodePhysical—Bounds—

Step 1 of 4

Decode the address

The MMU splits the reference into a segment selector and an offset. Here a code reference selects offset 24.

Relocate one contiguous address space

In the simplest model, two privileged registers describe the current address space. Base is its starting physical byte address. Bounds is its length in bytes. With an unsigned virtual address and a single-byte access, the translation is valid exactly when VA < Bounds, and the physical address is PA = Base + VA. The kernel must install a pair whose complete physical interval lies in RAM. With a valid pair, the last permitted byte is Base + Bounds - 1.

Take Base = 0x8000 and Bounds = 0x4000. Virtual byte 0x0120 maps to 0x8120. The upper boundary is exclusive: 0x3FFF maps to 0xBFFF, while 0x4000 traps. The value 0x4000 uses a wider processor address field in this base-and-bounds example, so hardware can observe and reject it. A check against an exclusive length also works when the allocation is empty. A bound of zero permits no byte accesses.

A multiple-byte access must fit entirely within the range. For a positive width n, the equivalent test is n <= Bounds followed by VA <= Bounds - n. Subtraction after the first check avoids unsigned wraparound when implementing the range test. In this example, a four-byte access starting at 0x3FFE crosses the upper limit even though its first byte lies inside. The architecture may also enforce alignment, so bounds alone describe only one part of the decision.

The placement is dynamic because the processor adds the base when it uses the address. Suppose a stopped process moves from physical [0x8000, 0xC000) to [0xC000, 0x10000). The kernel copies the bytes and sets the new base to 0xC000. An existing pointer with value 0x0120 now accesses 0xC120. Pointer values stored inside the copied bytes need no rewriting because the references still denote positions in the same virtual address space.

Kernel responsibilities and context switches

The kernel reserves a contiguous physical region when it creates a process, initializes that region, and records the register values with the process state. On a context switch it saves the outgoing translation state and restores the incoming one before resuming user mode. On exit the region goes back to the kernel's allocator. Distinct processes can use the same virtual address while isolated physical intervals keep their ordinary writes separate.

Privilege is essential. A process that could set its own base could point it into another process's region. A process that could increase its own bound might reach beyond its reservation. The hardware allows the kernel to change the registers and to receive traps for rejected accesses. The kernel's exception handler can inspect the saved address and type of access, then take an appropriate action such as terminating the process for an illegal reference. The exact exception and signal names depend on the system.

Base and bounds treat the entire allowed interval alike in this simple model. If the program uses the first 2 KiB and last 2 KiB of a 16 KiB virtual span, the kernel still has to keep a contiguous 16 KiB physical region for it. The middle 12 KiB is unused by the program yet lies inside the allowed interval. This is waste within one reservation. External fragmentation, by contrast, describes holes between reservations that cannot jointly satisfy one contiguous request.

Give each segment its own mapping

Segmentation splits one big reservation into independently mapped logical regions. In a coarse-grained design, code, heap, and stack have separate base and bounds values. An entry also supplies access permissions and a growth direction. The kernel installs entries for a process and can give each region a different physical placement. The addresses in the virtual gaps fail the segment's bounds check or select an unused entry.

A segment entry serves two purposes. Its base and bound describe where the accessible bytes sit, while its permissions determine which operations user mode may perform there. A code entry can permit reading and execution. Heap and stack entries typically permit reading and writing. An instruction fetch from a heap address can therefore fail even when a load of that address would succeed. Two processes can share an immutable code segment by mapping the same range of physical bytes through entries that deny writes. The kernel tracks ownership of shared storage so one process exiting does not release storage that another still uses.

In this lesson's explicit scheme, each 14-bit virtual address has a two-bit selector followed by a 12-bit unsigned offset. The top two bits choose the entry, with 00 for code, 01 for heap, and 11 for stack. Selector 10 has no entry. A different architecture could use an instruction's context or designated segment registers to choose a segment. The formulas below use the explicit address split.

Code exampleTrace
Selector = VA >> 12
Offset   = VA & 0x0FFF
MaxSegmentSize = 1 << 12 = 4096 bytes

Each selector covers a fixed virtual window of 4 KiB. The current bound can be smaller and determines how much of that window is accessible. The code window starts at 0x0000, the heap at 0x1000, and the stack at 0x3000. The physical bases come from the segment entries and can be placed in a different order. An upward-growing segment admits offsets in [0, Bounds), so the address is Base + Offset. To grow that segment by 512 bytes, the kernel needs 512 adjacent physical bytes after its current interval, or it must find a larger contiguous region and move the segment.

Translate a downward-growing stack

The stack occupies the highest part of its virtual window. With a 2 KiB stack bound and a 4 KiB window, legal offsets run from 0x800 through 0xFFF. The stack base register denotes the exclusive high physical endpoint. Convert the unsigned offset to a signed displacement from that endpoint.

Code exampleTrace
Displacement = Offset - MaxSegmentSize
Access is in bounds when -Bounds <= Displacement and Displacement < 0
PA = Base + (Offset - MaxSegmentSize)

For a byte access, the offset 0xFFF yields displacement -1, which lands on the last physical byte. Offset 0x800 yields -2048 and lands on the first physical byte of the 2 KiB stack. Notice the inclusive lower check. Testing only whether the magnitude is strictly smaller than the bound would discard that first byte. If implementing the model in C, widen and convert before subtracting so that a negative displacement is represented as signed arithmetic. The unsigned offset alone has no direction.

Suppose the OS expands the stack by one additional KiB and keeps the base fixed. Its 3 KiB bound admits offsets starting at 0x400, while offsets formerly in bounds still map to the same physical addresses. The newly occupied physical bytes lie immediately below the old lower physical endpoint. Extending downward crosses a different physical boundary than an upward-growing heap, but both designs still need contiguous space on the relevant side.

A width check for a downward-growing segment still treats an access address as the lowest byte in the instruction's range. For a four-byte load, the displacement must be at least the negative bound and at most minus four. Displacement minus one identifies a legal byte, but it cannot start a legal four-byte load because three bytes would fall above the exclusive high endpoint. The phrase downward growing describes where new stack storage is added when the stack expands. It does not reverse the address order of bytes within a multi-byte object.

The kernel may also keep a maximum stack limit beyond the currently allocated bound. A fault just below the stack can trigger a growth attempt if the address meets the kernel's criteria and the physical allocation can expand or move. The kernel must reject references far from the stack or beyond the configured limit. This is a policy decision made after the hardware reports a failed check. The MMU translates only with the currently installed descriptor and leaves virtual addresses outside that descriptor inaccessible until the kernel changes it.

Account for holes between segments

Suppose physical memory has three separate free intervals of 4 KiB each. The total free capacity is 12 KiB, and the largest hole is 4 KiB. A request for a 6 KiB segment fails even though the total has room for two such requests in aggregate. An allocator needs both the requested size and a free interval large enough to hold it.

If two holes touch, the OS can coalesce them without moving any allocated bytes. If a live segment separates two holes, compaction could move that segment and consolidate the free space. That requires copying data, updating the translation state, and coordinating with processes using the moved memory. Some physical regions can be difficult or impossible to move while other devices or the kernel rely on their locations. Segmenting the address space solves the sparse-gap reservation problem and leaves a variable-size placement problem for physical memory.

Permission boundaries are coarse in this model. A process with three segment entries cannot give two disjoint portions of one heap two independent protections through those entries alone. Finer segmentation can express more boundaries, at the cost of more entries and more placement decisions. Paging takes a different approach by using equal-sized slots, which removes the requirement that an entire region fit into one physical hole.

Worked trace of three segment translations

Use these register values for one process. Code has base 0x8000, bound 0x0800, and read and execute permissions. Heap has base 0x8800, bound 0x0C00, and read and write permissions. Stack has exclusive high base 0x7000, bound 0x0800, and read and write permissions.

  1. Fetch at 0x0064. The split is 00 0000 0110 0100. Selector 00 chooses code and offset 0x064 passes the bounds check. An instruction fetch is allowed, so the physical address is 0x8000 + 0x64 = 0x8064. A store at the same virtual address would fail the permission check.
  2. Store at 0x1068. The split is 01 0000 0110 1000. Heap offset 0x068 is less than 0x0C00. The store reaches physical 0x8800 + 0x68 = 0x8868. At 0x1C00 the offset would equal the bound and fail before a physical store.
  3. Load at 0x3C00. The split is 11 1100 0000 0000. Offset 0xC00 gives displacement -0x400, inside [-0x800, 0). The load reaches 0x7000 - 0x400 = 0x6C00. The accessible stack interval in physical memory is [0x6800, 0x7000).
  4. Load at 0x2000. Selector 10 is unused. The offset is zero, but without a valid segment there is no physical address to compute.

Change one input. Keep the stack registers fixed and change the third load to 0x37FF. The selector is still 11, but the displacement becomes 0x7FF - 0x1000 = -0x801. That is one byte below the allowed interval, so the access traps. At the adjacent address 0x3800, displacement is exactly -0x800, and a single-byte load is permitted at 0x6800.

Next, use the same interval arithmetic to follow splitting and coalescing in free-space management and heap allocators.

Before you move on

Try it in your workspace

Translate virtual addresses under 16-bit segmentation (top 2 bits select Code, Heap, or negative-growing Stack). Check both the bounds comparison and the negative-offset stack calculation.

Keep a short note of what you tried, what passed, and what you still need to check.

Your practice record

0 of 3 checked.

Saved in this browser when storage is available. Uncheck any item to revisit it. This is your own record, not an assessment.