Memory segmentation

On this page 7

Memory segmentation is a memory management scheme in which a program’s address space is divided into variable-sized logical units called segments, such as code, data and stack, each described by a base address, a limit and access rights. Every memory reference names a segment and an offset within it, and the hardware translates that pair into a linear or physical address while checking that the offset stays inside the limit.

In operating systems, segmentation was one of the first practical answers to relocation and memory protection. It survives mainly in the x86 architecture, where modern 64-bit systems have reduced it to a nearly flat model and rely on paging for most translation and protection work.

How segmented address translation works

A segmented address is a two-part logical address: a segment identifier and an offset. The processor uses the identifier to look up a segment descriptor in a table maintained by the operating system. The descriptor supplies the base, the limit and the permissions, and the hardware computes base plus offset to obtain the resulting address.

Before the addition, the hardware performs a bounds check. If the offset exceeds the limit, or if the access violates the descriptor’s rights, the processor raises a protection fault and the kernel usually terminates the program. This check gives every segment its own hardware-enforced boundary.

FieldPurposeEffect of a violation
Base addressStart of the segment in linear memoryNot applicable
LimitSize of the segmentFault when the offset is out of range
Type and rightsCode, data, read, write or execute permissionsFault on a forbidden access
Privilege levelRing required to use the segmentFault on an unprivileged access
Present bitWhether the segment is in memorySegment-not-present fault
Fields of a typical segment descriptor

Segmentation in the x86 architecture

The Intel 8086 introduced a simple form of segmentation with 16-bit segment registers named CS, DS, SS and ES. In this real mode, the processor shifts the segment value left by four bits and adds the offset, producing a 20-bit address that can reach 1 MB of memory. Segments overlap freely and offer no protection.

The 80286 and 80386 added protected mode, in which segment registers hold selectors that index the Global Descriptor Table or a Local Descriptor Table. Each descriptor carries a real base, a limit and a privilege level, which allowed operating systems to isolate programs and to separate the kernel in ring 0 from applications in ring 3.

Processor modeSegment register contentPractical role
Real modeParagraph address shifted by four bitsReach 1 MB with 16-bit registers
16-bit protected modeSelector into a descriptor tableProtection and relocation
32-bit protected modeSelector with 4 GB limitsMostly flat model with paging
64-bit long modeBase forced to zero except FS and GSThread-local storage and per-CPU data
Evolution of x86 segmentation

In 64-bit long mode, the processor ignores the base and limit of most segments. Only the FS and GS registers keep a usable base, which Linux, Windows and macOS use for thread-local storage and per-processor kernel data.

Segmentation compared with paging

Segmentation and paging solve the same problem in different ways. Segmentation follows the logical structure of a program, so a segment matches a meaningful unit such as a code module or a stack. Paging divides memory into fixed-size pages, typically 4 KB, which ignores program structure but makes allocation far simpler.

AspectSegmentationPaging
Unit sizeVariable, set per segmentFixed, commonly 4 KB
Visibility to programsVisible in the logical addressTransparent to programs
Fragmentation typeExternal fragmentationInternal fragmentation
Protection granularityPer logical unitPer page
SharingNatural for whole modulesShared pages mapped into several spaces
Modern useMinimal, mainly FS and GSPrimary mechanism in all major systems
Segmentation versus paging

Some systems combined both approaches as segmented paging, in which each segment has its own page table. Multics is the best-known example, and 32-bit x86 also translated segmented addresses to linear addresses first and then paged them through the memory management unit.

Strengths and limitations of segmentation

Segmentation’s main strength is that protection matches program structure. A code segment can be marked execute-only, a stack segment can grow within its limit, and a library segment can be shared among processes with a single descriptor. Bounds checks also catch many out-of-range accesses in hardware.

Its main weakness is external fragmentation. Because segments vary in size, free memory breaks into gaps that may be too small for the next request even when their total is sufficient. Solving this requires compaction or swapping whole segments, both of which are expensive. Segment registers also complicate compilers and pointers, which is why the industry adopted the flat memory model.

  • Advantage: protection and sharing align with logical program units.
  • Advantage: a small number of descriptors can describe a large program.
  • Limitation: variable sizes cause external fragmentation and costly compaction.
  • Limitation: segmented pointers make programming and porting harder.
  • Limitation: whole-segment swapping moves far more data than page-level swapping.

Segmentation and the segmentation fault

The common error name segmentation fault comes from this scheme, even though most modern systems no longer use segments for protection. Today a segmentation fault on Linux or macOS usually results from a page-level violation, such as touching an unmapped address or writing to a read-only page, which the kernel reports to the program as the SIGSEGV signal.

The table below separates what the name suggests from what actually happens on a current 64-bit system.

SituationHistorical causeModern cause
Dereferencing a null pointerOffset outside a segment limitAccess to an unmapped page
Writing to program codeWrite to an execute-only segmentWrite to a read-only page
Stack overflowOffset past the stack segment limitHit on a guard page
Historical and modern meaning of a segmentation fault

In simple terms

Memory segmentation is like storing a program in labeled boxes of different sizes: one box for the instructions, one for the data, one for temporary notes. Each box has a starting point and a size written on its label.

Whenever the program reaches into memory, the computer checks the label and refuses any reach beyond the edge of the box. This keeps programs from damaging each other. The downside is that boxes of many sizes leave awkward gaps, so modern computers mostly use equal-sized pages instead.

A practical example

A systems programmer maintains firmware for an industrial controller built on a 32-bit x86 processor running a small real-time operating system. The system defines separate segments for the kernel code, the kernel data and each of three control tasks, all described in the Global Descriptor Table.

During testing, one task writes past the end of a 2 KB buffer. Because the task’s data segment has a limit of exactly 64 KB, the stray write stays inside its own segment, but a later bug that computes a negative offset triggers a general protection fault. The kernel stops only that task and logs the faulting selector and offset.

The programmer reads the log, finds the faulty pointer arithmetic and fixes it. The segment limits contained the damage to one task, and the two other control loops kept running throughout the test.