What it actually does
AI analysis from the code graph — generated when you open this
loading…
README
CPU Internals
These notes are taken from Intel SDM. You can consider them as a short/resumed version of some parts of the manuals that I found worth looking at when learning about system programming, OS internals or virtualization.
System-level architecture consists of a set of registers, data structures, and instructions designed to support basic system-level operations such as memory management, interrupt and exception handling, task management, and control of multiple processors
Global and Local Descriptor Tables
When operating in protected mode, all memory accesses pass through either the global descriptor table (GDT) or an optional local descriptor table (LDT).
These tables contain entries called segment descriptors and can contain up to 8192 (2^13) 8-byte descriptors.
Segment descriptors (8 byte large) provide the base address of segments as well as access rights, type, and usage information.
Each segment descriptor has an associated segment selector.
A segment selector (16-bits) provides the software that uses it with an index into the GDT or LDT (the offset of its associated segment descriptor), a global/local flag (determines whether the selector points to the GDT or the LDT), and access rights information (CPL).
Global and Local Descriptor Tables in IA-32e Mode
GDTR and LDTR registers are expanded to 64-bits wide in both IA-32e sub-modes.
GDT/LDT are expanded in 64-bit mode to support 64-bit base addresses, (16-byte LDT descriptors hold a 64-bit base address and various attributes).
System Segments, Segment Descriptors, and Gates
The architecture defines two system segments: the task-state segment (TSS) and the LDT.
The architecture defines a set of special descriptors called gates: call gates, interrupt gates, trap gates, and task gates.
These gates provide protected gateways to system procedures and handlers that may operate at a different privilege level than application programs and most procedures.
For example, a CALL to a call gate can provide access to a procedure in a code segment that is at the same or a numerically lower privilege level (more privileged) than the current code segment.
Gates in IA-32e Mode
In IA-32e mode, the following descriptors are 16-byte descriptors (expanded to allow a 64-bit base): LDT descriptors, 64-bit TSSs, call gates, interrupt gates, and trap gates.
Call gates facilitate transitions between 64-bit mode and compatibility mode. Task gates are not supported in IA-32e mode.
On privilege level changes, stack segment selectors are not read from the TSS. Instead, they are set to
NULL.
Task-State Segments and Task Gates
The TSS defines the state of the execution environment for a task. It includes the state of:
GPR, segment registers, the EFLAGS register, the EIP register.
segment selectors with stack pointers for three stack segments (one stack for each privilege level).
segment selector for the LDT associated with the task and the base address of the paging-structure hierarchy.
All program execution in protected mode happens within the context of a task called the current task.
In switching tasks, the processor performs the following actions:
Stores the state of the current task in the current TSS.
Loads the task register with the segment selector for the new task.
Accesses the new TSS through a segment descriptor in the GDT.
Loads the state of the new task from the new TSS into the general-purpose registers, the segment registers, the LDTR, control register CR3, the EFLAGS register, and the EIP register.
Begins execution of the new task.
A task can also be accessed through a task gate. A task gate is similar to a call gate, except that it provides access (through a segment selector) to a TSS rather than a code segment
Task-State Segments in IA-32e Mode
Hardware task switches are not supported in IA-32e mode. However, TSSs continue to exist.
The base address of a TSS is specified by its descriptor.
A 64-bit TSS holds the following information that is important to 64-bit operation:
Stack pointer addresses for each privilege level
Pointer addresses for the interrupt stack table
Offset address of the IO-permission bitmap (from the TSS base)
The task register is expanded to hold 64-bit base addresses in IA-32e mode.
Interrupt and Exception Handling
External interrupts, software interrupts and exceptions are handled through the interrupt descriptor table (IDT).
The IDT stores a collection of gate descriptors that provide access to interrupt and exception handlers
The linear address for the base of the IDT is contained in the IDT register (IDTR)
Gate descriptors in the IDT can be interrupt, trap, or task gate descriptors.
To access an interrupt or exception handler, the processor first receives an interrupt vector from internal hardware, an external interrupt controller, or from software by means of an INT n, INTO, INT3, INT1, or BOUND instruction.
Memory Management
The base physical address of the paging-structure hierarchy is contained in control register CR3.
The entries in the paging structures determine the physical address of the base of a page frame, access rights and memory management information.
Memory Management in IA-32e Mode
Four levels of system data structures are used:
The page map level 4 (PML4) — An entry in a PML4 table contains the physical address of the base of a page directory pointer table, access rights, and memory management information. The base physical address of the PML4 is stored in CR3.
A set of page directory pointer tables — An entry in a page directory pointer table contains the physical
address of the base of a page directory table, access rights, and memory management information.
Sets of page directories — An entry in a page directory table contains the physical address of the base of a page table, access rights, and memory management information.
Sets of page tables — An entry in a page table contains the physical address of a page frame, access rights, and memory management information.
System Registers
The system flags and IOPL field in the EFLAGS register control task and mode switching, interrupt handling, instruction tracing, and access rights.
The control registers CR0, CR2, CR3, and CR4 contain a variety of flags and data fields for controlling system level operations.
The debug registers allow the setting of breakpoints for use in debugging programs and systems software.
The GDTR, LDTR, and IDTR registers contain the linear addresses and sizes (limits) of their respective tables.
The TR task register contains the linear address and size of the TSS for the current task.
Model-specific registers: group of registers available primarily to OS or executive procedures (that is code running at privilege level 0). These registers control items such as the debug extensions, the performance-monitoring counters, the machine- check architecture, and the memory type ranges (MTRRs)
System Registers in IA-32e Mode
system-descriptor-table registers (GDTR, IDTR, LDTR, and TR) are expanded in hardware to hold 64-bit base addresses.
EFLAGS becomes the 64-bit RFLAGS register.
CR0–CR4 are expanded to 64 bits.
CR8 becomes available. CR8 provides read-write access to the task priority register (TPR) so that the OS can control the priority classes of external interrupts.
In 64-bit mode, debug registers DR0–DR7 are 64 bits. In compatibility mode, address-matching in DR0–DR3 is
also done at 64-bit granularity.
On systems that support IA-32e mode, the extended feature enable register (IA32_EFER) is available. This MSR controls activation of IA-32e mode and other IA-32e mode operations.
In addition, there are several model-specific registers that govern IA-32e mode instructions:
IA32_KERNEL_GS_BASE — Used by SWAPGS instruction.
IA32_LSTAR — Used by SYSCALL instruction.
IA32_FMASK — Used by SYSCALL instruction.
IA32_STAR — Used by SYSCALL and SYSRET instruction
MODES OF OPERATION
The processor is placed in real-address mode following power-up or a reset.
The PE flag in control register CR0 then controls whether the processor is operating in real-address or protected mode.
The VM flag in the EFLAGS register determines whether the processor is operating in protected mode or virtual-8086 mode.
The LMA bit (IA32_EFER.LMA[bit 10]) determines whether the processor is operating in IA-32e mode.
When running in IA-32e mode, 64-bit or compatibility sub-mode operation is determined by CS.L bit of the code segment.
The processor switches to SMM whenever it receives an SMI while the processor is in real-address, protected, virtual-8086, or IA-32e modes. Upon execution of the RSM instruction, the processor always returns to the mode it was in when the SMI occurred.
Extended Feature Enable Register
SYSTEM FLAGS AND FIELDS IN THE EFLAGS REGISTER
The system flags and IOPL field of the EFLAGS register control I/O, maskable hardware interrupts, debugging, task switching, and the virtual-8086 mode.
Only privileged code (typically operating system or executive code) should be allowed to modify these bits.
TF: Set to enable single-step mode for debugging; clear to disable single-step mode.
IF: Set to respond to maskable hardware interrupt requests; cleared to inhibit maskable hardware interrupts.
IOPL: Indicates the I/O privilege level of the currently running program or task. The CPL of the currently running program or task must be less than or equal to the IOPL to access the I/O address space.
NT: Controls the chaining of interrupted and called tasks.
RF: Set to temporarily disables debug exceptions (##DB) from being generated for instruction breakpoints; cleared to generate debug exceptions.
VM: Set to enable virtual-8086 mode; clear to return to protected mode.
AC: An alignment-check exception is generated when reference is made to an unaligned operand, such as a word at an odd byte address or a doubleword at an address which is not an integral multiple of four.
VIP: Set by software to indicate that an interrupt is pending; cleared to indicate that no interrupt is pending.
VIF: Contains a virtual image of the IF flag.
ID: The ability of a program or procedure to set or clear this flag indicates support for the CPUID instruction.
System Flags and Fields in IA-32e Mode
In 64-bit mode, the RFLAGS register expands to 64 bits with the upper 32 bits reserved.
In IA-32e mode, the processor does not allow the VM bit to be set because virtual-8086 mode is not supported
MEMORY-MANAGEMENT REGISTERS
The processor provides four memory-management registers (GDTR, LDTR, IDTR, and TR) that specify the locations of the data structures which control segmented memory management
Global Descriptor Table Register (GDTR)
The GDTR register holds the base address (32 bits in protected mode; 64 bits in IA-32e mode) and the 16-bit table limit for the GDT
The base address specifies the linear address of byte 0 of the GDT; the table limit specifies the
number of bytes in the table.
The LGDT and SGDT instructions load and store the GDTR register, respectively.
Local Descriptor Table Register (LDTR)
The LDTR register holds the 16-bit segment selector, base address (32 bits in protected mode; 64 bits in IA-32e mode), segment limit, and descriptor attributes for the LDT.
The base address specifies the linear address of byte 0 of the LDT segment; the segment limit specifies the number of bytes in the segment.
The LLDT and SLDT instructions load and store the segment selector part of the LDTR register, respectively.
The segment that contains the LDT must have a segment descriptor in the GDT.
LDT is intended to used per process and switched when the kernel switch between process contexts. GDT is for use system wide.