Skip to content
CurriculumLesson 23 ยท I/O
Intermediate~12 min

I/O Ports & Memory-Mapped I/O

The CPU needs to talk to device registers. It can use a separate port address space or map those registers into normal memory. The choice changes the instructions and the protection model.

In plain English

I/O ports are special mailboxes. Memory-mapped I/O gives devices regular addresses in the same neighborhood as memory.

By the end you will be able to

  • Explain port-mapped I/O and memory-mapped I/O.
  • Know why device registers are not ordinary variables.
  • Describe the protection and caching concerns.
  • Choose the right model for a given architecture.
01

I/O ports

A device exposes registers for control, status, and data. In port-mapped I/O, those registers live in a separate address space accessed by special instructions such as in and out. The CPU distinguishes a port address from a memory address, so ordinary loads and stores cannot reach the device.

lesson-23.osOS
/* conceptual port-mapped I/O */
outb(PORT_STATUS, command);
status = inb(PORT_STATUS);
02

Memory-mapped I/O

In memory-mapped I/O, device registers are assigned normal physical addresses. The CPU uses ordinary load and store instructions. The page table can mark those addresses as device memory, uncached, and privileged. This simplifies the instruction set and lets the same addressing modes reach devices.

lesson-23.osOS
/* conceptual memory-mapped I/O */
volatile uint32_t *status = (uint32_t *)0xFEE00000;
*status = command;
uint32_t value = *status;
03

Protection and caching

Device registers can change at any time, so they must not be cached. The compiler must not reorder or eliminate accesses, which is why drivers use volatile and memory barriers. The kernel must prevent user programs from writing device registers directly. Memory-mapped registers are protected by page-table permissions; port I/O is protected by privilege level.

Key takeaways

  • Port-mapped I/O uses a separate address space and special instructions.
  • Memory-mapped I/O uses normal addresses and ordinary loads/stores.
  • Device registers must be uncached and accessed carefully.
  • The kernel protects device access from user programs.
  • Memory-mapped I/O is common on modern systems.

Common pitfalls

  • Treating a device register as ordinary cached memory.
  • Forgetting volatile and memory barriers in driver code.
  • Assuming the CPU can access a device address from user mode.

Kernel notes

  • Compare the instruction sets used by each model.
  • Explain why device memory is uncached.
  • Know who is allowed to access device registers.

Quick check

0 / 2 answered

1. Which model uses ordinary load and store instructions for device registers?

2. Why must device registers usually be uncached?