Port-mapped I/O

Also known as: isolated I/O

On this page 6

Port-mapped I/O, also called isolated I/O, is a method of communicating with hardware in which device registers live in a separate I/O address space that the processor reaches only through dedicated input and output instructions, rather than through ordinary memory loads and stores. In operating systems, and specifically in the area of drivers & devices, it is the counterpart of memory-mapped I/O and remains a defining feature of the x86 architecture.

A device driver using port-mapped I/O reads a status register or writes a command by naming a numeric port address in an instruction such as in or out. The approach dates back to early microprocessors, and although modern high-speed devices rely mainly on memory-mapped registers, many legacy and platform devices on PCs still expose port interfaces.

How port-mapped I/O reaches device registers

Port-mapped I/O works by giving the processor two distinct address spaces: one for memory and one for I/O ports. On x86, the I/O space holds 65,536 eight-bit ports, addressed from 0x0000 to 0xFFFF, and adjacent ports can be combined for 16-bit or 32-bit transfers. When the CPU executes a port instruction, it signals on the bus or interconnect that the transaction targets I/O space rather than memory, so the same numeric address can mean different things in each space.

The instruction set provides a small family of operations for this purpose. The in and out instructions move a byte, word or doubleword between a register and a port, while the string variants ins and outs, often combined with a repeat prefix, move blocks of data between memory and a single port. The port number is supplied either as an immediate value for ports 0 to 255 or in the DX register for the full range.

The following table summarizes the core x86 port instructions.

InstructionDirectionData sizeTypical use
`in`Port to register8, 16 or 32 bitsRead a status or data register
`out`Register to port8, 16 or 32 bitsWrite a command or configuration value
`ins`Port to memory buffer8, 16 or 32 bits per stepRead a block from a legacy disk controller
`outs`Memory buffer to port8, 16 or 32 bits per stepWrite a block to a legacy device
Core x86 port instructions

Port accesses are strongly ordered and uncached by design. The processor does not combine, reorder or cache them as it may with normal memory, which makes them predictable for device control but also comparatively slow, often costing hundreds of processor cycles or more for each access.

Port-mapped I/O compared with memory-mapped I/O

The essential difference is where device registers appear. With memory-mapped I/O, registers occupy a range of the physical memory address space and are reached with ordinary load and store instructions; with port-mapped I/O, they sit in a dedicated, much smaller address space reached only by special instructions.

PropertyPort-mapped I/OMemory-mapped I/O
Address spaceSeparate I/O space, 64 KB on x86Shared with physical memory
InstructionsDedicated in and out familyAny load or store instruction
Register window sizeSmall, a few to a few dozen ports per deviceLarge, up to many megabytes
PerformanceSlow per access, strictly serializedFaster, supports write combining where allowed
Architecture supportMainly x86 and some older designsUniversal, including ARM and RISC-V
Access controlI/O privilege level and permission bitmapPage tables and the IOMMU
Port-mapped and memory-mapped I/O side by side

Most architectures other than x86, such as ARM and RISC-V, have no separate I/O space at all and use memory-mapped I/O exclusively. Even on x86, PCI and PCI Express devices may expose an I/O BAR, but modern devices typically provide memory-mapped BARs and transfer bulk data with direct memory access, using registers only for control.

Devices that still use I/O ports

On current PCs, port-mapped I/O survives mainly for legacy and platform devices whose port addresses became de facto standards in the original IBM PC architecture. Firmware, bootloaders and operating system drivers still use these interfaces during early boot, in virtual machines and on embedded x86 boards.

The table below lists well-known port ranges and the devices behind them.

Port rangeDeviceRole today
0x20 to 0x21, 0xA0 to 0xA1Programmable interrupt controllersUsually masked in favor of the APIC
0x40 to 0x43Programmable interval timerCalibration and fallback timing
0x60 and 0x64PS/2 keyboard controllerLegacy input and emulation
0x70 to 0x71CMOS and real-time clockReading the hardware clock
0x3F8 to 0x3FFFirst serial portKernel debugging and server consoles
0xCF8 and 0xCFCPCI configuration mechanismLegacy access to PCI configuration space
Well-known legacy port ranges on x86 PCs

Port-mapped I/O also remains important in virtualization. Hypervisors trap guest port accesses and emulate the corresponding device, and some paravirtual devices deliberately use a single port write as a lightweight notification to the hypervisor, because trapping a port access is simple and well defined.

Privilege, protection and driver practice

Because a stray port write can reprogram a controller or disable interrupts, operating systems restrict port access to privileged code. On x86, the I/O privilege level field in the flags register sets which privilege ring may execute port instructions, and the I/O permission bitmap in the task state segment can grant individual ports to less privileged code. A user-mode program that executes in or out without permission receives a general protection fault.

In practice, drivers access ports through kernel helper functions rather than raw instructions, which keeps code portable and allows the kernel to track resource ownership. Common guidance for driver developers includes:

  • Reserve the port range through the kernel resource manager before using it, so that two drivers never claim the same ports.
  • Use the platform accessor functions for 8, 16 and 32-bit port access instead of inline assembly.
  • Avoid tight polling loops on slow status ports, because each read is expensive; prefer interrupt-driven I/O where the device supports it.
  • Respect required delays between consecutive writes on older controllers that cannot accept back-to-back accesses.

A common misconception is that port-mapped I/O is obsolete. It is no longer the main path for high-bandwidth data, but it remains part of the x86 architecture, of firmware interfaces and of virtual hardware, so operating system developers still need to understand it.

The short version

A computer needs a way to talk to its keyboard, clock, serial port and other hardware. Port-mapped I/O gives these devices their own set of numbered mailboxes, separate from the main memory where programs keep their data. To send a device an instruction, the processor drops a value into the right mailbox with a special command; to hear back, it reads from a mailbox.

Most modern hardware instead uses addresses inside the main memory map, which is faster and simpler. The separate mailboxes are still found on PC-compatible computers for older and basic devices, and only trusted system software is allowed to use them.

Where it makes a difference

A systems engineer is diagnosing a server that reboots during startup before any graphics or network drivers load. The only reliable output channel at that stage is the first serial port at I/O base 0x3F8. The engineer enables the kernel’s early serial console, which writes each character with a single `out` instruction to the transmit register and polls the line status register at port 0x3FD before the next byte.

Because this path needs no memory-mapped device setup, no interrupts and no driver stack, it works from the first moments of kernel execution. A laptop connected through a serial cable captures the log at 115,200 baud and shows a fault in early memory initialization.

The engineer identifies an incompatible firmware setting, corrects it and the server boots normally. The simplicity of port-mapped I/O made the decisive diagnosis possible at a point where no other output mechanism was available.