x86 memory segmentation

the implementation of memory segmentation on the x86 architecture

Uses your system voice; voice and rate in settings.

Q4413527Snapshot

≈ 11 min read

Names in other languages

  • eomemora segmenteco de x86-arkitekturo
  • EspañolSegmentación de memoria del x86
  • PortuguêsSegmentaçao de memoria do x86
  • Українськасегментація у процесорах x86
  • РусскийСегментная защита памяти
  • Englishx86 memory segmentation
  • 한국어x86 메모리 분할
  • 中文X86記憶體區段

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

x86 memory segmentation is a term for the kind of memory segmentation characteristic of the Intel x86 computer instruction set architecture. The x86 architecture has supported memory segmentation since the original Intel 8086 (1978), but x86 memory segmentation is a plainly descriptive retronym.

The introduction of memory segmentation mechanisms in this architecture reflects the legacy of earlier 80xx processors, which initially could only address 16 KB, or later 64 KB of memory (16,384 bytes or 65,536 bytes, respectively), and whose instructions and registers were optimised for the latter. Dealing with larger addresses and more memory was thus comparably slower, as that capability was somewhat grafted-on in the Intel 8086.

Memory segmentation could keep programs compatible, relocatable in memory, and by confining significant parts of a program's operation to 64 KB segments, the program could still run faster. In 1982, the Intel 80286 added support for virtual memory and memory protection; the original mode was renamed real mode, and the new version was named protected mode.

The x86-64 architecture, introduced in 2003, has largely dropped support for segmentation in 64-bit mode. In both real and protected modes, the system uses 16-bit segment registers to derive the actual memory address.…

x86 memory segmentation is a term for the kind of memory segmentation characteristic of the Intel x86 computer instruction set architecture. The x86 architecture has supported memory segmentation since the original Intel 8086 (1978), but x86 memory segmentation is a plainly descriptive retronym.

The introduction of memory segmentation mechanisms in this architecture reflects the legacy of earlier 80xx processors, which initially could only address 16 KB, or later 64 KB of memory (16,384 bytes or 65,536 bytes, respectively), and whose instructions and registers were optimised for the latter. Dealing with larger addresses and more memory was thus comparably slower, as that capability was somewhat grafted-on in the Intel 8086.

Memory segmentation could keep programs compatible, relocatable in memory, and by confining significant parts of a program's operation to 64 KB segments, the program could still run faster. In 1982, the Intel 80286 added support for virtual memory and memory protection; the original mode was renamed real mode, and the new version was named protected mode.

The x86-64 architecture, introduced in 2003, has largely dropped support for segmentation in 64-bit mode. In both real and protected modes, the system uses 16-bit segment registers to derive the actual memory address.

In real mode, the registers CS, DS, SS, and ES point to the currently used program code segment (CS), the current data segment (DS), the current stack segment (SS), and one extra segment determined by the system programmer (ES). The Intel 80386, introduced in 1985, adds two additional segment registers, FS and GS, with no specific uses defined by the hardware.

The way in which the segment registers are used differs between the two modes. The choice of segment is normally defaulted by the processor according to the function being executed. Instructions are always fetched from the code segment.

Any data reference to the stack, including any stack push or pop, uses the stack segment; data references indirected through the BP register typically refer to the stack and so they default to the stack segment. The extra segment is the mandatory destination for string operations (for example MOVS or CMPS); for this one purpose only, the automatically selected segment register cannot be overridden.

All other references to data use the data segment by default. The data segment is the default source for string operations, but it can be overridden. The instruction format allows an optional segment prefix byte which can be used to override the default segment for selected instructions if desired.

The 16-bit segment selector in the segment register is interpreted as the most significant 16 bits of a linear 20-bit address, called a segment address, of which the remaining four least significant bits are all zeros. The segment address is always added to a 16-bit offset in the instruction to yield a linear address, which is the same as physical address in this mode.

For instance, the segmented address 06EFh:9234h (here the suffix "h" means hexadecimal) has a segment selector of 06EFh, representing a segment address of 06EF0h, to which the offset is added, yielding the linear address 06EF0h + 9234h = 10124h. (The leading zeros of the linear address, segmented addresses, and the segment and offset fields are shown here for clarity. They are usually omitted.)

Because of the way the segment address and offset are added, a single linear address can be mapped to up to 2 12 = 4096 distinct segment:offset pairs. For example, the linear address 08124h can have the segmented addresses 06EFh:1234h, 0812h:0004h, 0000h:8124h, etc. This could be confusing to programmers accustomed to unique addressing schemes, but it can also be used to advantage, for example when addressing multiple nested data structures.

While real mode segments are technically always 64 KB long, the practical effect is only that no segment can be longer than 64 KB, rather than that every segment as actually used in a program must be treated as 64 KB long – dealing with effectively smaller segments is possible: usable sizes range from 16 through 65,536 bytes, in 16-byte steps. Because there is no protection or privilege limitation in real mode, it is still entirely up to the program to coordinate and keep within the bounds of any segments.

This is true both when a segment is programmatically treated as smaller than, or the full 64 KB, but it is also true that any program can always access any memory by just changing segments, since it can arbitrarily set segment selectors to change segment addresses with absolutely no supervision. Therefore, while real mode can be thought of as allowing different segment lengths, and as allowing segments to be overlapping or non-overlapping as desired, none of this is restrictively enforced by the CPU.

The effective 20-bit address space of PC/XT-generation CPUs limits the addressable memory to 2 20  bytes, or 1,048,576 bytes (1 MB). This derived directly from the hardware design of the Intel 8086 (and, subsequently, the closely related 8088), which had exactly 20 address pins.

(Both were packaged in 40-pin DIP packages; even with only 20 address lines, the address and data buses were multiplexed to fit all the address and data lines within the limited pin count.) Each segment begins at a multiple of 16 bytes, called a paragraph, from the beginning of the linear (flat) address space. That is, at 16 byte intervals.

Since all segments are technically 64 KB long, this explains how overlap can occur between segments and why any location in the linear memory address space can be accessed with many segment:offset pairs. The actual location of the beginning of a segment in the linear address space can be calculated with segment × 16.

Such address translations are carried out by the segmentation unit of the CPU. The memory model concept derives from the setup of the segment registers. For example, in the tiny model CS=DS=ES=SS, that is the program's code, data, and stack are all contained within a single 64 KB segment.

In the small memory model DS=SS, so both data and stack reside in the same segment; CS points to a different code segment of up to 64 KB. When attempting address translation of a given logical address, the processor reads the 64-bit segment descriptor structure from either the Global Descriptor Table when TI=0 or the Local Descriptor Table when TI=1.

It then performs the privilege check: max(CPL, RPL) ≤ DPL where CPL is the current privilege level (found in the lower 2 bits of the CS register), RPL is the requested privilege level from the segment selector, and DPL is the descriptor privilege level of the segment (found in the descriptor). All privilege levels are integers in the range 0–3, where the lowest number corresponds to the highest privilege.

If the inequality is false, the processor generates a general protection (GP) fault. Otherwise, address translation continues. The processor then takes the 16-bit offset and compares it against the segment limit specified in the segment descriptor.

If it is larger, a GP fault is generated. Otherwise, the processor adds the 24-bit segment base, specified in descriptor, to the offset, creating a linear physical address.

The privilege check is done only when the segment register is loaded, because segment descriptors are cached in hidden parts of the segment registers. mov dword [fs:eax], 42 However, segment registers are usually used implicitly. All CPU instructions are implicitly fetched from the code segment specified by the segment selector held in the CS register.

Most memory references come from the data segment specified by the segment selector held in the DS register. These may also come from any segment specified by the segment selector held in the respective segment register, if a segment-override prefix precedes the instruction that makes the memory reference.

Most, but not all, instructions that use DS by default will accept any other override prefix. Processor stack references, either implicitly (e.g. push and pop instructions) or explicitly (memory accesses using the (E)SP or (E)BP registers) use the stack segment specified by the segment selector held in the SS register. For explicit references, the segment may be overridden.

String instructions (e.g. stos, movs, cmps) use the extra segment for the destination string specified by the segment selector held in the ES register. Segmentation cannot be turned off on x86-32 processors (this is true for 64-bit mode as well, but beyond the scope of discussion), so many 32-bit operating systems simulate a flat memory model by setting all segments' bases to 0 in order to make segmentation neutral to programs.

For instance, the Linux kernel sets up only 4 general purpose segments: Since the base is set to 0 in all cases and the limit 4 GiB, the segmentation unit does not affect the addresses the program issues before they arrive at the paging unit. (This, of course, refers to 80386 and later processors, as the earlier x86 processors do not have a paging unit.)

Current Linux also uses GS to point to thread-local storage. Segments can be defined to be either code, data, or system segments. Additional permission bits are present to make segments read only, read/write, execute, etc. In protected mode, code may always modify all segment registers except CS (the code segment selector).

This is because the current privilege level (CPL) of the processor is stored in the lower 2 bits of the CS register. The only ways to raise the processor privilege level (and reload CS) are through the lcall (far call) and int (interrupt) instructions.

Similarly, the only ways to lower the privilege level (and reload CS) are through lret (far return) and iret (interrupt return) instructions. In real mode, code may also modify the CS register by making a far jump (or using an undocumented POP CS instruction on the 8086 or 8088).

Of course, in real mode, there are no privilege levels; all programs have absolute unchecked access to all of memory and all CPU instructions. For more information about segmentation, see the IA-32 manuals freely available on the AMD or Intel websites.

See also

x86 memory models THE multiprogramming system Split octal

Home of the IA-32 Intel Architecture Software Developer's Manual The Segment:Offset Addressing Scheme de:Segmentierung (Speicherverwaltung) fr:Segment (mémoire) ja:セグメント方式

Text derived from https://en.wikipedia.org/w/index.php?oldid=1346824848 under CC BY-SA 4.0; modifications have been made.

Sources cited in this revision

3 named source(s) extracted from the cited revision. A source with no author is shown by its publication; nothing is invented.