Skip to content
systemcoding.dev
Kernels

Segmentation vs Paging Explained for OS Developers

Segmentation and paging both translate x86 addresses. See what each does, why 64-bit mode keeps only FS and GS bases, and which to use for protection.

7 min read
Summarize in AI
Flat illustration of two options weighed side by side, with the Linux, Intel and GNU logos.

Anyone who starts an operating system on x86 meets both words in the first week and is told two things that sound contradictory: you must set up segmentation before the CPU will run your kernel, and you should ignore segmentation and use paging. Both are true, and the confusion comes from not knowing what each was invented to do. Segmentation and paging are two different mechanisms for translating the address a program uses into the address the memory hardware sees. The 8086 had a simple form of one, the 80286 and 80386 added protection to it and the 80386 added the other, and modern operating systems use one fully and keep the other as a formality. This post explains each in turn, shows the arithmetic with examples, and ends with a decision table for what to use for what. It is part of the guide to writing a hobby operating system, and the detailed setup of each is in the posts on the GDT and on paging.

What an address goes through

A program uses a logical address, such as the address held in a pointer. The CPU turns it into a linear address, and then into a physical address, which is what is put on the memory bus. Segmentation does the first step, and paging does the second:

  1. Segmentation: logical address (segment selector plus offset) becomes a linear address.

  2. Paging: linear address becomes a physical address, if paging is enabled.

If paging is off, the linear address is the physical address. If segmentation is configured flat, with every segment starting at 0, the logical offset is the linear address. A modern kernel makes the first step an identity, so that paging is the only translation that does anything.

Segmentation in real mode

In real mode, a segment is just a 16-bit register that is shifted left four bits and added to a 16-bit offset to make a 20-bit address. There is no table, no protection and no concept of a limit beyond the 64 KiB reach of the offset:

Text
segment:offset      calculation          address
0x1234:0x0010       0x12340 + 0x0010     0x12350
0xB800:0x0000       0xB8000 + 0x0000     0xB8000
0x0000:0x7C00       0x00000 + 0x7C00     0x07C00

Many pairs name the same byte; 0x07C0:0x0000 is the same physical address as 0x0000:0x7C00. The point of the scheme was to let a 16-bit processor address a megabyte. It protects nothing: any program can load any value into a segment register and write anywhere.

Segmentation in protected mode

Protected mode keeps the segment registers and changes what they hold. A segment register now contains a selector, which is an index into a descriptor table, the GDT or an LDT. Each 8-byte descriptor gives the segment's base address, its limit, whether it is code or data, and a privilege level from 0 to 3. The CPU computes linear = base + offset, checks that offset does not exceed the limit, and checks the privilege and type rules, raising a general protection fault on a violation.

This is real protection. Two programs can be given different segments, with different bases and limits, so one cannot reach the other's memory, and a user-mode segment at privilege level 3 cannot be used to access a kernel segment at level 0. It is also awkward. Segments are contiguous regions of variable size, so allocating them causes external fragmentation: free memory exists but not in one piece. Moving a segment to compact memory means copying it. And a program that wants to share a data structure with another must agree on segment arrangement, which language and compiler support was never good at.

Paging

Paging divides the linear address space into fixed-size pages, 4 KiB on x86, and physical memory into frames of the same size. A set of tables in memory maps each page to a frame, and the CPU walks them on every access that misses its translation cache, the TLB. A linear address is split into table indexes and an offset within the page. On 32-bit x86 with two-level tables, the split is 10 bits for the page directory index, 10 bits for the page table index and 12 bits for the offset:

c
#include <stdio.h>
#include <stdint.h>

int main(void)
{
    uint32_t vaddr = 0xC0100234;
    printf("dir=%u table=%u offset=0x%X\n",
           vaddr >> 22, (vaddr >> 12) & 0x3FF, vaddr & 0xFFF);
    return 0;
}
Text
dir=768 table=256 offset=0x234

The directory entry 768 points to a page table, entry 256 of that table holds the frame's physical address, and the low 12 bits 0x234 are added unchanged. Each table entry also carries permission bits: present, writable, and user-accessible, and with extra support a no-execute bit. Four-level paging on x86-64 uses the same idea with four 9-bit indexes and a 12-bit offset.

Paging solves the problems segmentation had. Pages are all the same size, so there is no external fragmentation: any free frame fits any page. Physical memory behind a contiguous virtual range need not be contiguous, so a process sees one clean address space while its pages are scattered in RAM. Pages can be marked not present and brought in from disk when touched, which is demand paging and swapping. Two processes can map the same frame at different addresses, which is shared memory, with different permissions. And each process can have its own set of tables, by switching CR3, giving every process the illusion of a private address space.

What 64-bit mode did to segmentation

In long mode, the base and limit of the code and data segments, CS, DS, ES and SS, are ignored. The CPU treats them as if the base were 0 and the limit unbounded. The segment registers still must hold valid selectors for code, as CS determines the privilege level and the mode through the descriptor's L bit, but the translation is flat by definition. The exceptions are FS and GS, whose bases are still applied, and are set through the model-specific registers IA32_FS_BASE and IA32_GS_BASE, or by the wrfsbase instructions. They exist so that a kernel and user code can reach per-CPU and per-thread data with one instruction, such as mov %gs:0x10, %rax. Linux uses GS for per-CPU data in the kernel and FS for thread-local storage in user space.

The consequence for an OS developer on x86-64: you still need a GDT, with a null entry, a 64-bit code segment, a data segment and a task state segment, because the CPU insists on them for privilege levels and for the syscall and interrupt machinery. But you do not use segmentation for protection or for address space layout. All of that is paging.

Why flat segmentation is the default answer

In protected mode on 32-bit x86, you could still do something clever with segments, and some early systems did. In practice, every mainstream operating system, from Linux to Windows to the BSDs, sets all segments to base 0, limit 4 GiB, and uses paging for everything else. The reasons are practical. Paging gives per-page permissions and demand loading, and segments cannot. Compilers and the C language assume a flat address space, with pointers that are plain integers. Other architectures, such as ARM and RISC-V, have no segmentation at all, so portable kernels use paging only. And the TLB makes translation cheap, while loading a segment register has a cost.

So the flat GDT you write is a formality that satisfies the hardware, and the real design work is in the page tables.

What to use for what

GoalUseWhy
Keep user code from reading kernel memoryPaging, user/supervisor bitPer page, works on all architectures
Give each process its own address spacePaging, one CR3 per processTables are swapped on a context switch
Make code read-only and data non-executablePaging, write and NX bitsSegmentation limits cannot express it at page grain
Swap or demand-load memoryPaging, present bit and page faultHandler brings the page in
Share memory between processesPaging, map one frame twiceDifferent addresses, different permissions
Set privilege level of running codeSegmentation, the CS descriptorRing 0 versus ring 3 comes from the selector
Per-CPU or per-thread data pointerFS or GS baseOne instruction, no register spent
Switch stacks on interrupts from user modeTSS, a segment-system structureThe CPU reads the kernel stack pointer from it
Run old 16-bit codeReal mode or virtual 8086 segmentsOnly segmentation understands that code

The summary is that segmentation survives for privilege levels, the TSS and FS and GS bases, and paging does everything about memory.

Where this does not fit

This is the x86 story. Other architectures never had the choice, so a kernel for ARM or RISC-V has only paging, and the vocabulary differs: a page table root register, address space identifiers and permission bits in entries. Systems that do use segments seriously, such as some embedded designs and the older IBM and Burroughs architectures, treat them as units of protection and sharing, and the arguments for them there are about different workloads. And a few legacy uses of x86 segmentation remain, such as 16-bit code under the virtual 8086 mode and 32-bit compatibility mode inside a 64-bit OS, where the base and limit of the 32-bit segments are honored again.

Also inAssembly
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.