Fact-checked by Grok 2 weeks ago

Protected mode

Protected mode is an operational mode of x86-compatible central processing units (CPUs) introduced by with the 80286 in 1982, serving as the native state of the processor and enabling advanced , , and multitasking through segmentation and paging mechanisms. In protected mode, the processor supports a segmented memory model where is divided into segments defined by descriptors in the (GDT) or Local Descriptor Table (LDT), each specifying base addresses, limits, and access rights to enforce isolation between tasks. Paging provides an additional layer of virtual-to-physical address translation, allowing for efficient implementation and further against unauthorized access. This mode expanded significantly with the Intel 80386 in 1985, introducing 32-bit addressing for up to 4 gigabytes of linear , compared to the 1-megabyte limit of . A core feature of protected mode is its hierarchical protection system, utilizing four privilege levels—known as rings 0 through 3—to control access to resources, with ring 0 reserved for the most privileged operations (typically the operating system kernel) and ring 3 for user applications. Multitasking is facilitated through Task-State Segments (TSS) and task gates, which manage context switches and maintain separate address spaces per task, ensuring hardware-enforced isolation. Entry into protected mode occurs by setting the Protection Enable (PE) bit in the machine status word (MSW) on the 80286 or in the CR0 control register on the 80386 and later processors, a process that requires careful initialization of segment descriptors and often a far jump to flush the processor pipeline. Unlike , which operates in a 16-bit flat without protection mechanisms and supports only single-tasking, protected mode provides robust against faults and malicious code, forming the foundation for like Windows, , and macOS. Later enhancements, such as (PAE) on processors from the era onward, further increased physical memory addressing to 64 gigabytes while maintaining compatibility.

Background

Real Mode Basics

Real mode, also known as real-address mode, is the default operational state of x86 processors upon power-up or reset, providing backward compatibility with the original and 8088 architectures. In this mode, the processor employs a 20-bit space, restricting accessible to a maximum of 1 MB (2^20 bytes). Addressing operates through a segment:offset scheme, where locations are specified by combining a 16-bit value from a segment register with a 16-bit . The x86 architecture includes four primary 16-bit segment registers in real mode: CS (code segment), which points to the current code segment; DS (data segment), used for data access; SS (stack segment), for stack operations; and ES (extra segment), for additional data. To form a physical address, the segment value is shifted left by 4 bits (effectively multiplied by 16) to establish the segment base, and the offset is then added to this base. For example, the physical address is calculated as \text{Physical address} = (\text{segment} \times 16) + \text{offset}. This segmented approach limits each individual segment to a maximum size of 64 KB (2^16 bytes), as the is only 16 bits wide. Furthermore, provides no hardware-enforced , allowing programs unrestricted access to the entire addressable memory space without checks for bounds or privileges. These characteristics establish the foundational model from which protected mode evolved to address growing demands for larger memory and security.

Limitations and Motivations

Real mode, the default operating mode of early x86 processors like the , imposed significant constraints that hindered the evolution of personal computing systems. Primarily, it provided no inherent mechanisms, allowing errant or malicious code to access and corrupt any part of the system's 1 , which frequently led to crashes or vulnerabilities in multi-program environments. This 1 ceiling stemmed from the 20-bit physical addressing scheme, where segmented addressing—combining a 16-bit segment register shifted left by 4 bits with a 16-bit —effectively capped usable at 2^20 bytes, restricting scalability as capacities grew beyond this limit in the early 1980s. Furthermore, real mode lacked support for native multitasking, forcing operating systems to rely on cumbersome software techniques like time-slicing or to simulate concurrent program execution, which were inefficient and prone to errors. These limitations motivated the development of protected mode as a foundational shift toward more robust, secure, and scalable architectures. The primary drivers included the need to support multitasking operating systems such as Unix variants, which required to prevent one application from interfering with others, thereby enabling safer multiuser environments on emerging . Protection against malicious software was another key impetus, as growing software complexity in the amplified risks from faulty or intentionally harmful code, necessitating hardware-enforced boundaries to enhance system reliability. Additionally, the demand for larger memory addressing arose from hardware advancements allowing RAM beyond 1 MB, compelling designs that could leverage up to 16 MB of physical memory while maintaining compatibility with existing 8086 software. Historically, protected mode drew inspiration from operating systems that emphasized and resource sharing, influencing 's response to pressures from developers seeking to port advanced OSes to x86 platforms. 's development of the 80286 was informed by six months of into customer requirements for enhanced memory addressing and protection mechanisms. Developers, including those at who ported (a derivative) to the 80286, sought features for advanced OS implementations, as real mode's constraints made reliable systems impractical on PCs. The 80286, designed starting in 1978 and released in 1982, specifically aimed to bridge 16-bit real-mode compatibility with advanced computing paradigms, incorporating protected mode to address these demands while supporting high-level languages and for scientific, engineering, and business applications.

Historical Development

Intel 80286 Implementation

The , released in 1982, introduced protected mode as the first implementation of this feature in the x86 architecture, extending the 16-bit iAPX 86/88 family with advanced capabilities. This processor supported 24-bit physical addressing, enabling access to up to 16 MB of physical memory, a significant expansion from the 1 MB limit of prior real-mode systems that relied on 20-bit addressing. In protected mode, the 80286 provided a of up to 1 GB per task through segmentation, allowing for more efficient multitasking and isolation without hardware paging support. This mode built on real mode's segment:offset addressing model for compatibility but enforced stricter boundaries to prevent unauthorized access. Key innovations in the 80286's protected mode included the Global Descriptor Table (GDT) and Local Descriptor Table (LDT), which defined segment boundaries and attributes for all processes. The GDT, a system-wide table accessed via the GDTR register, held shared segment descriptors, while the LDT, loaded via the LDTR register, provided task-specific segments for private address spaces. Protection was further enhanced by a four-level privilege hierarchy, known as rings 0 through 3, where ring 0 represented the highest privilege for kernel operations and ring 3 the lowest for user applications. These levels were enforced through the Descriptor Privilege Level (DPL) in segment descriptors and the Current Privilege Level (CPL) in the code segment register, preventing lower-privilege code from accessing sensitive resources. Segment descriptors in the GDT and LDT followed an 8-byte , specifying the (starting location of the ), (up to 64 KB in size), and access rights. Access rights included the present bit (P) to indicate validity, type fields for code, , or , and the DPL for checking. This structure allowed the to validate references dynamically, generating exceptions for violations such as invalid descriptors or breaches. Despite these advances, the 80286's protected mode had notable limitations, including restriction to 16-bit operations without support for 32-bit extensions, absence of for running real-mode applications natively, and no built-in paging hardware for demand-paged . Despite these innovations, the 80286's protected mode saw limited adoption in practice, as it lacked support for running real-mode applications without rebooting and was incompatible with the dominant environment, leading most software to run in . Transitioning to protected mode required software initialization: loading the GDT into the GDTR using the LGDT , followed by setting the Protection Enable (PE) bit in the Machine Status Word (MSW) via the LMSW at privilege level 0, and finally executing an intra-segment jump to flush the prefetch queue. This process ensured a clean switch but demanded careful setup to avoid faults during the mode change.

Intel 80386 Enhancements

The , released in October 1985, introduced 32-bit protected mode, featuring 32-bit general-purpose registers such as , EBX, ECX, and , along with a 32-bit address bus that supported a 4 GB physical address space. This marked a significant evolution from the 16-bit protected mode of the , extending the latter's four privilege levels while enabling full 32-bit operations for enhanced performance and scalability in multitasking environments. Key additions included Virtual 8086 (V86) mode, which allowed real-mode 8086 applications to execute within protected mode under multitasking supervision, and a built-in paging unit for management. The segment descriptor format was enhanced to support 32-bit addressing, incorporating a granularity bit that permitted segment limits up to 4 when set. For compatibility, the 80386 provided Big Real Mode, an unprotected extension enabling 32-bit addressing in to access the full 4 space, and enhanced task state segments (TSS) that facilitated efficient context switching for multitasking by storing complete 32-bit processor states. Exclusive to the 80386 were structures like the Page Directory and Page Tables, which formed the basis for demand-paged by mapping 4 pages in a hierarchical manner. Additionally, the I/O Privilege Level (IOPL) bits in the EFLAGS register (bits 12 and 13) introduced granular control over I/O instructions, restricting sensitive operations to higher privilege levels and enhancing in protected environments. These features collectively enabled the development of full 32-bit operating systems, such as Microsoft's and 2.0, which leveraged the 80386's capabilities for robust, protected multitasking on personal computers.

Mode Switching

Entering Protected Mode

To enter protected mode on an x86 processor, specific prerequisites must be met to ensure a stable transition from . The (GDT) must be loaded into memory with at least a null descriptor, a descriptor, and a descriptor; these descriptors define the initial memory segments for code execution and data access in protected mode. A must also be established within a to handle any immediate subroutine calls or interrupts post-switch. Interrupts, including non-maskable interrupts (NMIs), should be disabled prior to the switch to prevent interference during the transition. The step-by-step procedure to switch to protected mode involves initializing segment registers to a flat memory model, loading the GDT, enabling the protection enable (PE) bit, and reloading the code segment register (CS). First, initialize the segment registers (DS, ES, SS, FS, GS) to point to a flat data segment, typically using a selector value like 0x10 for a 32-bit flat descriptor in the GDT; this ensures data access remains valid during the switch. Next, load the GDTR with the base address and size of the GDT using the LGDT instruction, which accepts a 6-byte pseudo-descriptor containing the 32-bit base address (24-bit for 80286) and 16-bit limit. Then, set the PE bit (bit 0) in the CR0 register to 1 using a MOV to CR0 instruction; this action serializes the and activates protected mode semantics. Finally, execute a far jump (JMP FAR) to a protected-mode code segment selector (e.g., 0x08 for a 32-bit code descriptor) and offset, which flushes the prefetch queue, reloads with the new selector, and begins execution in protected mode. After the jump, reinitialize the remaining segment registers if needed and re-enable interrupts. The implementation differs from the 80386 in several key aspects during entry. On the 80286, segments are limited to 16-bit operations, and the GDT base is 24 bits wide, requiring a hardware to exit protected mode once entered. In contrast, the 80386 supports 32-bit code and data segments (via the D/B flag in descriptors), a full 32-bit GDT base address, and software-based mode switching without , allowing larger address spaces and enabling paging if desired post-entry. These enhancements make the 80386 procedure more flexible for . Invalid configurations during entry can trigger exceptions, primarily general protection faults (#GP). For instance, attempting to set the PE bit without a valid GDT or using an invalid segment selector in the far jump causes a #GP(0) exception, halting the processor and requiring error handling via an interrupt service routine if one is already set up. Similarly, loading a null selector into CS or SS post-jump results in a #GP fault. Proper validation of descriptors and selectors is essential to avoid these faults. A minimal example for entering protected mode on an 80386, assuming a pre-built GDT at 0x1000 with flat 32-bit (selector 0x08) and data (0x10) segments, illustrates the sequence:
    cli                     ; Disable interrupts
    lgdt [gdt_descriptor]   ; Load GDT (gdt_descriptor at 0x1000-0x1005)
    mov [eax](/page/EAX), cr0
    or [eax](/page/EAX), 1               ; Set [PE](/page/PE) bit
    mov cr0, [eax](/page/EAX)
    jmp 0x08:protected_mode ; Far jump to reload [CS](/page/CS)

protected_mode:
    mov ax, 0x10            ; Data segment selector
    mov [ds](/page/DS), ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    mov ss, ax
    [sti](/page/STI)                     ; Re-enable interrupts
This code performs the essential switch, transitioning to a flat 4 GB model.

Exiting Protected Mode

Exiting protected mode on x86 processors, particularly the 80386 and later, involves a careful sequence of operations to return the CPU to while ensuring compatibility with legacy services and avoiding system instability. This transition disables the protection enable () bit in the CR0 register, flushes the instruction pipeline, and reinitializes segment registers and interrupt handling to mimic the 20-bit addressing and flat memory model of . The procedure requires prior setup of the () with real-mode-compatible descriptors (64 KB limits, byte granularity) to prevent invalid memory accesses during the switch. The step-by-step procedure for exiting protected mode is as follows: First, disable all interrupts to prevent asynchronous disruptions, including maskable interrupts via the CLI instruction and non-maskable interrupts (NMIs) through external masking or by ensuring a valid real-mode vector for NMI (IVT offset 0x08). Second, if paging is enabled, disable it by clearing the PG bit (bit 31) in CR0, zeroing CR3 to invalidate the page directory, and ensuring the CPU is executing from an identity-mapped physical address to avoid translation errors. Third, clear the PE bit (bit 0) in CR0 using a MOV instruction to disable protected mode addressing and protection checks. Fourth, perform a far jump to a real-mode code segment, typically to offset 0xFFF0 in segment 0xF000 (the conventional BIOS entry point), to flush the prefetch queue and reload the code segment register (CS) with real-mode values. Finally, reload the data segment (DS), extra segment (ES), stack segment (SS), FS, and GS registers with real-mode values (usually 0 for flat addressing), load the interrupt descriptor table register (IDTR) via LIDT to point to the real-mode interrupt vector table (IVT) at physical address 0, and re-enable interrupts with STI. The order of segment register reloads is critical: SS should be updated early to ensure a valid stack for any subsequent operations, followed by DS and ES to avoid data access faults. On the Intel 80386, additional considerations apply for safe exit. Paging must be disabled prior to clearing the PE bit, as active paging with protected mode disabled can lead to unpredictable address translations. In multitasking environments using task state segments (TSS), any active task switch must be completed or aborted by popping the task register (TR) and ensuring no pending task gates in the IDT before the transition, to prevent corruption of the task state. Risks during the exit process include potential s if the GDT is not properly configured with valid descriptors before clearing , as invalid segment references post-transition can trigger unhandled exceptions leading to a double fault and subsequent , causing a CPU . Post-exit, reliance on calls requires the IVT to be correctly initialized, as improper handling can hang the or corrupt . This mode-switching capability has been historically utilized in bootloaders like for hybrid operations, where is entered for efficient loading but exited to for accessing disk and video services. A representative assembly example for a safe exit on the 80386, assuming paging is disabled and GDT is preset, emphasizes the segment reload sequence:
    cli                    ; Disable maskable interrupts
    ; Assume NMI masked externally
    mov eax, cr0
    and eax, 0x7FFFFFFE     ; Clear PE bit (and PG if needed)
    mov cr0, eax
    jmp far 0xF000:0xFFF0   ; Far jump to flush pipeline, CS=0xF000, IP=0xFFF0 (BIOS reset vector)
real_mode_cs:
    mov ax, 0               ; Real-mode segment value
    mov ds, ax              ; Reload DS
    mov es, ax              ; Reload ES
    mov ss, ax              ; Reload SS (critical for stack)
    mov fs, ax              ; Reload FS
    mov gs, ax              ; Reload GS
    lidt [real_idt]         ; Load IVT pointer (base 0, limit 0x3FF)
    sti                     ; Re-enable interrupts
This sequence ensures a clean transition, with the far jump establishing the real-mode CS before other registers are adjusted to prevent faults.

Protection Mechanisms

Privilege Levels

Protected mode in the x86 employs a hierarchical consisting of four rings, numbered 0 through 3, to enforce boundaries between different software components. Ring 0 represents the highest level, typically reserved for operating kernel code with unrestricted to and resources. Rings 1 and 2 serve as intermediate levels for less trusted services, such as device drivers or executive modules, while Ring 3 is the lowest level, designated for applications with restricted to prevent interference with critical operations. The privilege of executing code or accessing data is determined by two key fields: the Current Privilege Level (CPL), which indicates the privilege level of the currently running task and is stored in bits 0-1 of the (CS) register, and the Descriptor Privilege Level (DPL), a 2-bit field (bits 5-6 of the access rights byte) in segment descriptors that specifies the minimum privilege required to access the associated segment or gate. The DPL in segment descriptors acts as the primary enforcement mechanism for privilege checks. For nonconforming code segments, code executing at a given CPL can only load segments whose DPL equals the CPL, ensuring same-privilege direct execution. To invoke more privileged code (lower ring number), transitions must occur through controlled mechanisms like call gates, which validate the caller's CPL against the gate's DPL before allowing the switch. Data access follows similar rules, where a task at CPL can read or write data segments only if the CPL is less than or equal to the segment's DPL, and the requested privilege level (RPL) of the segment selector is also less than or equal to the DPL; violations result in a (#GP). For inter-ring calls to more privileged code using nonconforming segments, the processor performs a stack switch to a new segment at the target privilege level, loaded from the task state segment (TSS), and pushes the caller's pointer, flags, pointer, and parameters onto it to maintain and enable proper returns. Conforming code segments, marked in their descriptor, allow execution from any CPL greater than or equal to the DPL without a stack switch, facilitating code across privilege levels. Input/output (I/O) operations and handling further enforce privilege separation. I/O instructions from a non-zero ring require the current privilege level to be less than or equal to the I/O privilege level (IOPL) bits in the ; otherwise, a #GP fault occurs, with escalation typically to Ring 0 handlers. and exceptions always vector to Ring 0 via entries in the (IDT), using or trap gates that may disable interrupts to prevent nesting issues. In the implementation, I/O access is strictly limited to Ring 0 unless explicitly permitted via the task's I/O permission , with no dedicated IOPL field for finer Ring 3 control. The Intel 80386 enhances this by introducing IOPL bits that allow Ring 3 code to perform I/O operations directly if IOPL equals 3, providing more flexible control without always relying on bitmaps. For instance, a Ring 3 application attempting to access a Ring 0 without proper triggers a #GP fault, protecting memory from user-mode corruption.

Memory Protection Fundamentals

In protected mode, the x86 employs hardware-enforced checks to prevent unauthorized , ensuring between , data, and tasks. These checks occur automatically during operations and include verification of bounds, types, and segment or presence, as defined in descriptors. Bounds checking confirms that the effective falls within the defined limits of the region, triggering a fault if exceeded. Type checking validates whether the operation (read, write, or execute) aligns with the region's permissions, such as restricting writes to executable segments. The present/not-present bit in descriptors further ensures that only loaded or mapped regions are accessible, with absent regions causing an immediate fault. Violations of these protection rules generate specific exceptions to allow the operating system to handle errors gracefully. The general protection fault (#GP) arises from bounds violations, invalid types, or privilege mismatches, providing an error code identifying the offending selector or operation. The not-present fault (#NP) specifically occurs when a descriptor's present bit is clear, indicating the memory region is not loaded into physical memory. On processors supporting paging, such as the 80386 and later, the page fault (#PF) handles similar issues at the page level, including not-present pages or permission violations, with an error code detailing the cause like writability or user/supervisor access. These faults are fully restartable, enabling precise recovery without . These mechanisms deliver key isolation benefits by enforcing per-task address spaces, where each operates within its allocated regions without interfering with others. This prevents common exploits like buffer overflows from propagating to adjacent areas, enhancing system stability and . Protection integrates with privilege levels (rings 0-3) to add layered enforcement, where access requires matching current privilege level (CPL) against descriptor privilege level (DPL), further restricting sensitive operations to higher-privilege code. In the , the original implementation of protected mode, these checks relied solely on segmentation for coarse-grained protection without paging support, limiting finer-grained until the 80386's enhancements.

Memory Management

Segmentation

In protected mode, segmentation provides a mechanism for dividing the linear into variable-sized s, each defined by a segment descriptor stored in either the (GDT) or a Local Descriptor Table (LDT). A segment selector, a 16-bit value loaded into one of the segment registers (, DS, , FS, GS, or ), serves as an index into the GDT or LDT to retrieve the descriptor, which specifies the segment's , size , and access rights such as readability, writability, and executability. The , consisting of the selector and an , is translated to a linear address by adding the offset to the segment ; the hardware automatically performs bounds checking to ensure the offset does not exceed the segment , generating a general-protection exception if it does. This approach contrasts sharply with segmentation, where segments are fixed at 64 and addressed via a simple left-shift of the segment value by 4 bits to form a base address, lacking hardware-enforced bounds or controls. In protected mode, segments can vary in size without the 64 restriction, enabling more efficient utilization, and stack segments support expand-up (growing from low to high addresses) or expand-down (growing from high to low addresses) configurations to accommodate operations while respecting the limit. These features introduce robust protection against buffer overflows and unauthorized , fundamental to the security model of protected mode. The implemented protected mode segmentation with 16-bit offsets, restricting each segment to a maximum size of 64 , which aligned offsets and addresses within this limit for with earlier designs but limited for larger programs. The Intel 80386 enhanced this by introducing 32-bit offsets, allowing segments up to 4 in size, and added a bit in the descriptor that, when set, scales the limit to units of 4 , permitting segment sizes from 4 to 4 in 4 increments for finer control over memory allocation. These improvements made segmentation more practical for multitasking environments and larger address spaces. Segmentation in protected mode supports various segment types essential for program execution and system operation: code segments hold executable instructions with attributes controlling conformity and readability; data segments manage read-write data areas; and stack segments handle push and pop operations with directionality flags. System segments, such as the Task State Segment (TSS), facilitate by storing processor state for context switching in multitasking scenarios. This segmented model, while flexible, is often combined with other mechanisms in to address memory needs comprehensively.

Paging

The Intel 80386 introduced paging as a key enhancement to its protected mode , providing a mechanism for virtual-to-physical address translation that was absent in the 80286, thereby enabling true support for multitasking operating systems. This paging unit operates on 32-bit linear addresses produced by segmentation, dividing the into fixed-size pages of 4 each, with support for larger 4 pages through specific page directory entry flags. Paging employs a two-level hierarchical structure: a Page Directory, which holds up to entries and serves as the top-level table, points to Page Tables; each Page Table also contains up to entries that map to individual 4 KB physical pages. The base physical address of the Page Directory is stored in the CR3 , allowing dynamic switching of address spaces during task changes. To enable paging, the PG bit (bit 31) in the CR0 register must be set after protected mode is activated ( bit in CR0), which initiates for all subsequent accesses. A 32-bit linear address is split into three fields for translation: the upper 10 bits (31–22) index the Page Directory entry, the middle 10 bits (21–12) index the corresponding Page Table entry, and the lower 12 bits (11–0) provide the offset within the 4 KB page. The translation process begins by using CR3 to locate the Page Directory, then fetches the Page Table base address from the indexed directory entry (shifted left by 12 bits to align to a 4 KB boundary), followed by fetching the physical page frame base from the indexed table entry (also shifted left by 12 bits), and finally adding the offset to yield the physical address. For example, given a linear address of 0x402003, the directory index is 0x001 (bits 31–22), the table index is 0x002 (bits 21–12), and the offset is 0x003 (bits 11–0); the physical address is then computed as ((page frame base from table entry) << 12) + offset. Key features include demand paging, where pages are loaded into physical memory only upon access; this is controlled by the Present bit (P, bit 0) in page directory and table entries—if unset, a page fault exception (#PF) is generated to allow the operating system to handle paging in or out. Page-level protections are enforced via bits in the entries: the Read/Write bit (R/W, bit 1) distinguishes read-only from read-write access, and the User/Supervisor bit (U/S, bit 2) restricts access based on the current privilege level (CPL), with supervisor-mode (U/S=0) pages inaccessible from user mode (CPL>0). Effective permissions combine attributes from both the directory and table entries, providing granular control that complements segmentation. Performance is optimized by a Translation Lookaside Buffer (TLB), an on-chip four-way set-associative cache holding 32 recent translations (8 entries per set), which is flushed on CR3 reloads or task switches to ensure coherence. While the 80386 paging supports up to 4 GB of physical memory with 32-bit addresses, later processors introduced (PAE) starting with the to handle more than 4 GB by expanding to 36-bit physical addresses and adding a third level (page directory pointer table) to the hierarchy.

Segment Descriptors

Segment descriptors in the Intel 80386 protected mode are 8-byte entries stored in the (GDT) or local descriptor tables (LDT), defining the base address, size, and access attributes of memory segments. Each descriptor consists of a 32-bit base address, a 20-bit limit field, and various control bits for protection and usage. The structure is as follows:
ByteBitsField NameDescription
00-7Limit (bits 0-7)Lower 8 bits of the 20-bit segment limit; when the granularity bit (G) is 0, units are bytes (maximum 1 MB - 1); when G=1, units are 4 KB pages (maximum 4 GB - 1).
10-7Limit (bits 8-15)Upper 8 bits of the lower 16 bits of the segment limit.
20-7Base (bits 0-7)Lower 8 bits of the 32-bit base address, specifying the starting linear address of the segment.
30-7Base (bits 8-15)Next 8 bits of the 32-bit base address.
40-7Base (bits 16-23)Middle 8 bits of the 32-bit base address.
50-3Type4-bit type field defining the segment category and access rights (bits 40-43); combined with S bit for effective 5-bit type interpretation (detailed below).
4S (System)System flag: 0 for system segments, 1 for code/data segments (bit 44).
5-6DPL (Descriptor Privilege Level)Privilege level (0-3) for the descriptor, used in protection checks; 0 is the most privileged (bits 45-46).
7P (Present)Present bit: 1 indicates the segment is present in memory; 0 indicates it is not (bit 47).
60-3Limit (bits 16-19)Upper 4 bits of the 20-bit segment limit.
4AVL (Available)Available bit for use by system software (bit 52).
5ReservedMust be 0 (bit 53).
6D/B (Default operation size)Default size: 0 for 16-bit mode, 1 for 32-bit mode (bit 54).
7G (Granularity)Granularity bit: 0 for byte granularity, 1 for 4 KB granularity (bit 55).
70-7Base (bits 24-31)Upper 8 bits of the 32-bit base address.
The type field (combined with S for interpretation) distinguishes between code segments, data segments, and system segments. Code segments (type values 8h-Bh when S=1) support execution, with bit 1 indicating readability (e.g., type 0Ah for execute-only non-conforming, 0Bh for execute/read non-conforming). Data segments (type values 0h-7h when S=1) allow data access, with bit 1 for writability and bit 0 for expansion direction (e.g., type 0h for read-only expand-up, 2h for read/write expand-up, or 6h for read/write expand-down for stacks). System segments include types for task state segments (TSS, 9h available or 0Bh busy), local descriptor tables (LDT, 2h), and gates. Gate descriptors enable controlled inter-privilege-level transfers, such as call gates (type for 80386 32-bit). Their format replaces the base and with a 32-bit offset to the target , a 16-bit target segment selector, and bits 0-3 of byte 6 specifying the parameter count (0-15 doublewords) to copy from the caller's to the target's during privilege transitions. The P, DPL, and type fields in byte 5 ensure the gate is present and enforces the required for invocation. Segment descriptor tables are loaded using instructions like LGDT (load GDT register) or LLDT (load LDT register), which set the base and limit of the table in special registers (GDTR or LDTR). Individual descriptors are referenced via 16-bit selectors loaded into segment registers (, DS, , , GS, ) using instructions such as , far jumps, or calls, which cache the descriptor details for address translation. Compared to the Intel 80286, the 80386 segment descriptors expand the base address to 32 bits (from 24 bits, enabling 4 GB addressing versus 16 MB) and the limit to 20 bits with granularity support (up to 4 GB versus 1 MB fixed). The 80386 also introduces 32-bit offsets in gates and additional flags like D/B and G, absent in the 80286's 16-bit-oriented format.

Compatibility Features

Real Mode Application Support

Protected mode environments, designed primarily for advanced and , initially posed challenges for running legacy real-mode software, which operates under the simpler 8086 addressing model limited to 1 MB of memory. To bridge this gap, developers introduced DOS extenders, software mechanisms that enable 16-bit real-mode applications to leverage protected-mode features without full rewriting. These extenders temporarily switch the into protected mode for the application while maintaining compatibility with the underlying environment. A key standardization effort was the DOS Protected Mode Interface (DPMI), a specification released in 1989 that defines an API for 16-bit DOS programs to request protected-mode services, such as allocating extended memory beyond 1 MB and managing descriptors for larger address spaces. DPMI allows applications to execute in protected mode via function calls (interrupt 31h), providing access to up to 4 GB of memory on 80386 processors while intercepting and emulating real-mode DOS and BIOS services as needed. This interface became widely adopted for memory-intensive DOS applications, like games and utilities, by encapsulating protected-mode operations behind a real-mode-compatible facade. Another technique, known as big real mode or unreal mode, emerged as an unofficial but practical workaround on 80386 and later processors. By briefly entering protected mode to load segment descriptors with 32-bit base addresses and limits set to the maximum (0xFFFFFFFF), the system can then return to real mode while retaining these extended descriptors, effectively allowing 32-bit addressing and access to the full 4 GB address space without formal protection mechanisms. This mode lacks memory protection, making it vulnerable to crashes from invalid accesses, but it enabled early 32-bit DOS programs to run with minimal modifications under real-mode DOS. Intel's 1986 programmer's reference manual implicitly supported this behavior through descriptor manipulation, though it was not an intended operating mode. Historically, these techniques saw significant use in Windows 3.x, particularly in 386 mode, where DPMI served as the interface for running applications alongside Windows sessions. Windows 3.0 formalized DPMI support, allowing real-mode programs to request protected-mode execution for memory expansion, while the system managed mode switches to integrate them into the multitasking environment. However, limitations persisted, such as the absence of true multitasking for apps, which ran in isolated sessions without preemptive scheduling, and reliance on cooperative interactions with the host OS. Key challenges in supporting real-mode applications within protected environments include handling interrupts and BIOS calls, which are inherently real-mode constructs incompatible with protected-mode segmentation and privilege checks. DOS extenders address this by implementing mode-switching routines, such as the int86 function family, to temporarily revert to for BIOS interrupts (e.g., for video services) before returning, incurring performance overhead from context switches and descriptor reloads. Failure to manage these transitions properly could lead to system instability or inaccessible hardware resources. With the shift to 64-bit operating systems like and later, native support for real-mode applications has declined sharply, as () omits hardware features like essential for direct 16-bit emulation. 64-bit Windows versions explicitly do not support 16-bit processes or components, rendering traditional DOS extenders obsolete and requiring software emulation tools for legacy real-mode software. serves as an advanced hardware alternative in 32-bit protected mode for more seamless integration.

Virtual 8086 Mode

Virtual 8086 mode, introduced with the 80386 , enables the execution of real-mode 8086/8088/80186/80188 software within a protected-mode , providing a for while maintaining and multitasking capabilities. This mode is activated by setting the VM bit (bit 17) in the EFLAGS while the operates in protected mode (PE bit set in CR0), typically through methods such as an IRET instruction from ring 0 with VM=1 or a task switch to a Task State Segment (TSS) marked with VM=1. Once activated, each V86 task functions as a emulating the 8086 state, including 16-bit , segment-based addressing, and real-mode handling, including additional segment (FS and GS) and support for 80186 instructions such as BOUND, while restricted to the 8086 instruction set. Key features of Virtual 8086 mode include the virtualization of sensitive operations, where instructions for I/O (e.g., IN, OUT, , OUTS) and privileged actions (e.g., CLI, , HLT, n) generate exceptions that trap to a ring 0 Virtual 8086 monitor for or direct access, ensuring protected-mode supervision. Interrupts and exceptions in V86 mode vector through the protected-mode (IDT) to privilege level 0 handlers, which can reflect them back to the 8086 code or handle them natively, supporting seamless integration with the host operating system. Multiple V86 instances are supported on 80386 and later processors by enabling paging (PG bit in CR0), which allows distinct linear address spaces for each task, preventing interference while emulating the 8086's 20-bit addressing model (up to 1 ). This paging integration maps V86 linear addresses—formed by shifting 16-bit selectors left by 4 bits and adding offsets—into the protected-mode virtual address space, enabling address wrapping if needed to mimic 8086 behavior beyond 1 . In practice, Virtual 8086 mode facilitated the integration of legacy real-mode applications into protected-mode operating systems, such as in , where it powered "DOS boxes" by running programs in isolated virtual machines under the 80386's enhanced mode. However, the mode imposes limitations, including a per-task capped at 1 MB (with potential for 64 KB overflow in some configurations), reliance on paging for isolation which adds complexity, and performance overhead from frequent supervisor traps for I/O and interrupts, as all V86 tasks execute at current privilege level (CPL) 3 without direct access to hardware. Compatibility differences from true 8086 execution, such as altered instruction timings and exception behaviors (e.g., added page faults), may require OS-level adjustments. For instance, setting the I/O privilege level (IOPL) to 3 in the EFLAGS register permits V86 tasks running at ring 3 to perform I/O operations without trapping to the monitor, enabling user-level in scenarios like application-hosted environments, though lower IOPL values enforce stricter supervision.

Multitasking Support

Task Structures

In protected mode, multitasking is supported through hardware data structures that manage task states and facilitate switches between them. The primary structure is the Task State Segment (TSS), a system segment that holds the complete of a task, including registers and information, allowing the CPU to save and restore states efficiently during switches. Introduced with the , the TSS is a fixed-format segment with a minimum size of 44 bytes, containing fields such as the back link to the previous TSS, segment registers (, , , ), general-purpose registers (AX, BX, , , SI, , , ), the instruction pointer (), , and initial stack pointers and selectors for each privilege level (0 through 2). In the 80286, hardware task switching is initiated by a CALL or JMP instruction targeting a task selector, which references a TSS descriptor in the (GDT); this loads the new task's state from its TSS, saves the current task's state, and updates the processor's registers accordingly, enabling basic multitasking without software intervention for context management. The Intel 80386 enhanced the TSS to a minimum size of 104 bytes to accommodate 32-bit operations and additional features, incorporating expanded fields like 32-bit versions of the general-purpose registers (EAX, EBX, etc.), the extended instruction pointer (EIP), extended flags (EFLAGS), additional segment registers (FS, GS), the page directory base register (CR3) for per-task paging, the Local Descriptor Table (LDT) selector, and stack pointers and selectors per privilege ring (ESP0:ESP2, SS0:SS2) to support ring transitions during switches. An optional I/O permission bitmap at the end of the TSS allows fine-grained control over I/O port access for the task, with the bitmap's offset specified in the TSS itself. The Task Register (TR), a special 16-bit register, holds the selector for the current task's TSS descriptor in the GDT or LDT; it is loaded using the LTR instruction (restricted to privilege level 0) and automatically updated during task switches to point to the active TSS. The 80386 introduced the Nested Task (NT) flag in EFLAGS (bit 14), set during CALL-initiated switches to indicate nesting and enable return to the parent task via IRET, while JMP switches clear it for non-nested transitions; additionally, a busy bit in the TSS descriptor (changing type from 9 to B for available to busy) prevents reentrancy by triggering a if a busy TSS is referenced. For , the Descriptor Privilege Level (DPL) in the TSS descriptor (bits 5:6) specifies the minimum required to access the TSS, ensuring that task switches are only permitted if the current privilege level (CPL) is at or below the TSS DPL, thus enforcing inter-task via privilege levels.

Context Switching

In protected mode, context switching enables multitasking by saving the processor state of the current task and restoring that of another, leveraging task state segments (TSS) to hold the complete execution context including registers, segment selectors, and stack pointers. This mechanism supports both inter-task and nested execution flows on x86 processors. Hardware context switching is performed using JMP, CALL, or IRET instructions that target a TSS selector in the (GDT). Upon execution, the processor automatically saves the current task's state—such as general-purpose registers, EFLAGS, EIP, and segment registers—into the old TSS, then loads the new task's state from its TSS into the processor registers. The task register () is updated with the new TSS selector, and the busy flag in the TSS descriptor is atomically set using LOCK semantics to prevent concurrent access. This process ensures isolation between tasks without software intervention. Software context switching, in contrast, is managed entirely by the operating system without hardware TSS involvement, offering greater and efficiency. The explicitly saves the current task's registers and to a per-task using instructions like PUSH, POP, or , then restores the next task's similarly; floating-point and vector unit states are handled via FXSAVE/FXRSTOR or the XSAVE family of instructions. This method is typically invoked during scheduler decisions, such as interrupts for preemptive multitasking. Interrupt-driven context switching is triggered when an interrupt or exception vectors through a task gate in the (IDT), initiating a hardware task switch to a dedicated handler task. The processor saves the interrupted task's state to its TSS, loads the handler's state, and provides a fresh for execution, which enhances for event handling; interrupts must often be disabled during the switch to avoid reentrancy. This approach integrates seamlessly with the IDT for asynchronous operations. With the introduction of the Intel 386 processor, task switching gained support for nesting via a back pointer field in the TSS, which stores the selector of the previous task's TSS during switches initiated by CALL or interrupts. When the nested task flag () is set in EFLAGS, an IRET instruction uses this back pointer to return control to the prior task, automatically reloading its saved state and clearing the flag. This linkage facilitates chained task execution without manual stack management. Hardware task switching incurs higher compared to software methods due to TSS accesses, which often cause misses and TLB invalidations, making it less suitable for frequent switches in performance-critical systems. Consequently, favor software approaches for their speed and adaptability, reserving mechanisms for specific needs.

Operating Systems

Early Adopters

Earlier commercial operating systems to adopt protected mode included around 1984 and Coherent in the mid-1980s, which utilized the 80286's protected mode for environments on PC-compatible hardware. One of the earliest widespread commercial operating systems to adopt protected mode was 1.x, jointly developed by and and released in December 1987. It represented a significant implementation of the 80286's protected mode for multitasking, enabling pre-emptive scheduling, multi-threading, and through a segmented while requiring at least a 286 processor. This allowed to support up to 16 MB of RAM and , marking a significant shift from the limitations of real-mode . In 1990, released , which incorporated protected mode in its standard mode for 80286-based systems, utilizing segment descriptors to enforce attributes such as read-only access and prevent errant pointers from corrupting other programs. This segmented approach addressed the 1 MB real-mode barrier, supporting up to 16 MB of physical memory, though it retained 16-bit limitations compared to the enhanced mode on 80386 processors. 's protected mode features facilitated better multitasking and application isolation, contributing to its commercial success with millions of installations. Among Unix variants, emerged as an early adopter, with its project established in the summer of 1989 to port (BSD) to the 80386 platform. It leveraged the 80386's protected mode for 32-bit addressing in a flat memory model, incorporating two-level paging with 4 KB pages to manage efficiently and support a 4 GB per process. While initial releases minimized segmentation for BSD compatibility, future plans included support to enable real-mode application execution within protected environments. Early protected mode adoption encountered significant challenges, including the limited availability and high cost of 80286 and 80386 hardware in the late 1980s, exacerbated by a 1988 RAM price crisis that made even 3 MB systems expensive for typical users. To bridge the gap for DOS-based applications, tools like Phar Lap's 286/ emerged around 1989, allowing protected mode execution on 286 systems by proxying DOS calls and providing access to , though mode-switching overhead limited its use in performance-sensitive software. A pivotal development came in 1993 with Microsoft's release of , which mandated an 80386 processor to deliver full 32-bit protected mode functionality, including advanced paging and portability across architectures, as part of a strategic shift from 16-bit systems to enterprise-grade . This requirement reflected Microsoft's focus on leveraging the 80386's capabilities for robust multitasking and , distinguishing NT from consumer-oriented Windows versions.

Modern Implementations

In contemporary x86 architectures, all CPUs initialize in upon power-on for with legacy firmware, requiring the to transition the processor to protected mode before enabling for 64-bit operation. This sequence ensures that protected mode serves as the foundational execution environment, even in 64-bit systems, where extends its capabilities with a larger while retaining core protection mechanisms like privilege rings and memory isolation. Modern operating systems predominantly adopt a flat memory model in protected mode, configuring segment registers to span the entire with minimal segmentation, thereby simplifying and relying primarily on paging for and . This approach persists in 64-bit , where protected mode features remain essential for support, such as running 32-bit applications in submode, underscoring the enduring role of protected mode as the for x86 execution beyond . The , powering modern Windows versions, operates exclusively in protected mode with a flat segmentation model augmented by paging to enforce strict separation between -mode (ring 3) and -mode (ring 0) address spaces, preventing applications from directly accessing memory or . For 16-bit application , Windows employs the NT Virtual DOS Machine (NTVDM) subsystem, which leverages within protected mode to emulate a environment without compromising overall system isolation. In , the utilizes protected mode for managing 32-bit processes on 64-bit systems via long mode's compatibility submode, where the code segment selector (CS=0x08) designates 32-bit execution while maintaining protected mode's privilege levels and paging for . This setup allows seamless execution of legacy binaries under the 64-bit , building directly on protected mode foundations extended by long mode's 64-bit registers and addressing. macOS, based on the , minimizes reliance on protected mode segmentation in favor of paging for management, implementing a flat model where page tables handle user-kernel separation and protection across its hybrid Unix foundation. To enhance kernel security, macOS incorporates Intel's Supervisor Mode Execution Prevention (SMEP, introduced in 2012) and (SMAP, introduced in 2013), which prevent the kernel from executing or accessing user-mode memory pages, thereby mitigating common exploit techniques like and data leaks.

References

  1. [1]
    [PDF] Volume 3 (3A, 3B, 3C & 3D): System Programming Guide - Intel
    Intel technologies may require enabled hardware, software or service activation. No product or component can be absolutely secure.
  2. [2]
    [PDF] Intel® 64 and IA-32 Architectures Software Developer's Manual
    Jan 2, 2012 · Intel technologies features and benefits depend on system configuration and may require enabled hardware, software, or service activation. Learn.
  3. [3]
    [PDF] Intel 80286 Programmer's Reference Manual - Bitsavers.org
    Product line handbooks contain data sheets, application notes, article reprints and other design information. NAME. COMPLETE SET OF 9 HANDBOOKS. Save $50.00 off ...Missing: motivations | Show results with:motivations<|control11|><|separator|>
  4. [4]
    Kicking Off the 80286 - Explore Intel's history
    The 80286 was a massive undertaking, with complex design, that dramatically reconceptualized personal computing and had unheard-of capabilities.
  5. [5]
    There's More to the 286 XENIX Story | OS/2 Museum
    Mar 22, 2017 · This was a highly complex CPU for the time, with an intricate system of software privilege and security enforcement known as protected mode.Missing: motivations | Show results with:motivations
  6. [6]
    [PDF] iAPX 286 Programmer's Reference Manual
    ... Intel ... The iAPX 286 can be operated in either of two different modes: Real Address Mode or. Protected Virtual Address Mode (also referred to as Protected Mode) ...
  7. [7]
    Raising the Bar with the 386 - Explore Intel's history
    The 386, introduced in 1985, had 275,000 transistors, a 16-megahertz clock speed, and offered more than double the performance of the 286.
  8. [8]
    [PDF] 386 Programmer's Reference Manual - Bitsavers.org
    CHMOS and HMOS are patented processes of Intel Corp. Intel Corporation and Intel's FASTPATH are not affiliated with Kinetics, a division of Excelan, Inc. or its ...
  9. [9]
    The Intel 80386 and Windows NT, part 1: introduction - OSnews
    Jan 24, 2019 · Windows NT stopped supporting the Intel 80386 processor with Windows 4.0, which raised the minimum requirements to an Intel 80486.
  10. [10]
    OS/2 History
    This product would be the first true 32 bit operating system for personal computers. Designed to work on the Intel 80386 and its follow on processors which were ...
  11. [11]
    [PDF] Intel® 64 and IA-32 Architectures Software Developer's Manual
    Intel technologies features and benefits depend on system configuration and may require enabled hardware, software, or service activation. Learn more at intel.
  12. [12]
    [PDF] INTEL 80386 PROGRAMMER'S REFERENCE MANUAL 1986
    Virtual 8086 mode (also called V86 mode) is a dynamic mode in the sense that the processor can switch repeatedly and rapidly between V86 mode and protected mode ...
  13. [13]
    [PDF] 80386 Returning to Real Mode (January 8, 1986)
    Jan 8, 1986 · This memo describes the procedure necessary to return the 80386 from operation in protected mode, back to operating in real mode. protected.
  14. [14]
    GNU GRUB Manual 2.12
    This is the documentation of GNU GRUB, the GRand Unified Bootloader, a flexible and powerful boot loader program for a wide range of architectures.
  15. [15]
    [PDF] Intel® 64 and IA-32 Architectures Software Developer's Manual
    NOTE: The Intel 64 and IA-32 Architectures Software Developer's Manual consists of four volumes: Basic Architecture, Order Number 253665; Instruction Set ...
  16. [16]
    [PDF] Intel® 64 and IA-32 Architectures Software Developer's Manual
    This chapter describes the basics of virtual machine architecture and an overview of the virtual-machine extensions. (VMX) that support virtualization of ...
  17. [17]
    [PDF] DOS PROTECTED MODE INTERFACE (DPMI) SPECIFICATION
    Mar 12, 1991 · The DPMI Specification Version 1.0 is copyrighted 1989, 1990, 1991 by the DPMI Committee. Although this Specification is publicly available and ...
  18. [18]
    DPMI 1.0 Programming API Specfication - delorie software
    The DPMI Specification is Copyright The DPMI Committee, 1989-1991. Unsupported download (100K) if you want.Missing: source | Show results with:source
  19. [19]
    A Brief History of Unreal Mode | OS/2 Museum
    Jun 15, 2018 · 1986—The 1986 Intel 80386 Programmer's Reference Manual says that to switch from protected to real mode, the programmer must among other ...
  20. [20]
    The popularity of DOS/4GW made Windows 95 game compatibility a ...
    Aug 29, 2023 · Windows 3.0 introduced a new interface called the DOS Protected Mode Interface (DPMI) which let MS-DOS programs request that their code execute ...
  21. [21]
    Interrupts and Handlers Part 2 - delorie software
    - Software interrupts : Calling real-mode DOS or BIOS services from protected-mode programs requires a switch to real-mode, so the int86 family of functions ...Missing: challenges | Show results with:challenges
  22. [22]
    64-bit versions of Windows don't support 16-bit components, 16-bit ...
    May 29, 2025 · This article discusses the lack of support for 16-bit components, 16-bit processes, or 16-bit applications in x64-based versions Windows.
  23. [23]
    Under the Hood: Happy 10th Anniversary, Windows | Microsoft Learn
    Virtual 8086 mode enabled Windows to run MS-DOS-based programs in a processor simulation of an 8086 CPU. Ill-behaved MS-DOS-based programs were rarely able to ...
  24. [24]
    [PDF] Control Transfer in Operating System Kernels
    May 13, 1994 · The MS-DOS emulation uses the 80386's virtual-8086 mode. It implements privileged operations and MS-DOS (BIOS) system calls with a user ...
  25. [25]
    [PDF] Introduction to the 80386
    For the majority of instructions, the segment unit obtains the translation and protection data from the 80386's on-chip segment and descriptor registers ...
  26. [26]
    [PDF] Intel® 64 and IA-32 Architectures Software Developer's Manual
    NOTE: The Intel® 64 and IA-32 Architectures Software Developer's Manual consists of ten volumes: Basic Architecture, Order Number 253665; Instruction Set ...
  27. [27]
    OS/2 1.0
    Features. OS/2 1.0 was a 16-bit multi-tasking protected-mode operating system with pre-emptive scheduling, multi-threading, dynamic linking, and virtual memory.Missing: commercial | Show results with:commercial
  28. [28]
    Designing the software specification - 386BSD
    The 386BSD project was established in the summer of 1989 for the specific purpose of porting BSD to the Intel 80386 microprocessor platform so that the tools ...
  29. [29]
    Half an operating system: The triumph and tragedy of OS/2
    Nov 29, 2019 · If it wasn't in protected mode, it was in “real” mode, where it acted like a faster 8086 chip and was limited to only one megabyte of RAM (the ...
  30. [30]
    Phar Lap's 286/DOS-Extender: why nobody used it for games
    Jan 29, 2024 · A DOS extender is a special program that can load a protected mode program into memory on a 286 or better computer and run it.Missing: adoption challenges
  31. [31]
    The History of Windows NT 3.1 - by Bradford Morgan White
    Aug 20, 2023 · Windows NT 3.1 required a minimum of a 25 MHz 80386, 12 MBs of RAM, 75 MBs of hard disk space, and a VGA card. In its first year, NT 3.1 sold ...
  32. [32]
    Setting Up Long Mode - OSDev Wiki
    Long mode consists of two sub modes which are: the actual 64-bit mode, and compatibility mode (a 32-bit mode, usually referred to as IA32e in the AMD64 manuals) ...Missing: Linux IA- 0x08
  33. [33]
    Envisioning a Simplified Intel® Architecture
    This white paper details the architectural enhancements and modifications that Intel investigated for a 64-bit mode-only architecture referred to as X86S (for ...<|control11|><|separator|>
  34. [34]
    x86-64 - OSDev Wiki
    Long mode does not support hardware task switching or virtual 8086 tasks. In long mode the current CS determines if the code currently running is 64 bit code ( ...Missing: 0x08 | Show results with:0x08<|separator|>
  35. [35]
    NTVDM and 16-bit app support - Compatibility Cookbook
    Nov 17, 2021 · NTVDM is a Feature on Demand and only supported on the x86 version of Windows. It is not supported on x64 and ARM versions of Windows, which do ...
  36. [36]
    Linux i386 Boot Code HOWTO
    Jan 23, 2004 · Switch to Protected Mode. To ensure code compatibility with all 32-bit IA-32 processors, perform the following steps to switch to protected mode ...
  37. [37]
    Memory and Virtual Memory - Apple Developer
    Aug 8, 2013 · Memory and Virtual Memory. This chapter describes allocating memory and the low-level routines for modifying memory maps in the kernel.Missing: SMEP SMAP<|separator|>
  38. [38]
    Intel® Supervisor Mode Access Prevention (Intel® SMAP) - ID:842704
    Intel® Supervisor Mode Access Prevention (Intel® SMAP) is a mechanism that provides next level of system protection by blocking a malicious user from tricking ...