Remap and Program the 8259 PIC in Your Hobby Kernel
Remap the two 8259 PICs away from the CPU exception vectors, mask and unmask IRQ lines, send the end-of-interrupt and detect spurious IRQ 7 and IRQ 15.

The first hardware interrupt a hobby kernel takes is usually the timer, and it usually arrives as a general protection fault. The cause is the 8259 Programmable Interrupt Controller: at power-on the BIOS sets it up so that device interrupts land on vectors 8 to 15, and in protected mode those vectors already belong to the CPU. Vector 8 is the double fault, 13 is the general protection fault, 14 is the page fault. A timer tick that arrives as vector 8 looks like a double fault, and the kernel dies with a confusing message. The fix is to reprogram the controllers so that their interrupts start at vector 32, which is the first number Intel leaves free. This post covers the 8259 pair on a PC in full: the cascade wiring, the initialization sequence word by word, masking, the end-of-interrupt, detection of spurious interrupts and how to turn the whole thing off for the APIC. The code is C for a 64-bit or 32-bit kernel, and the surrounding structure comes from the guide to interrupts, memory and paging, with the exception handling built in setting up an IDT.
The two chips
A PC has two 8259A controllers wired in a cascade. The master handles IRQs 0 to 7 and reports to the CPU directly. The slave handles IRQs 8 to 15 and is connected to the master's input line IRQ 2, so a slave interrupt reaches the CPU by raising IRQ 2 on the master. Each chip has two I/O ports, a command port and a data port:
| Chip | Command port | Data port | IRQs |
|---|---|---|---|
| Master | 0x20 | 0x21 | 0 to 7 |
| Slave | 0xA0 | 0xA1 | 8 to 15 |
The standard assignments, which are conventions of the PC architecture rather than anything the chip enforces:
| IRQ | Device |
|---|---|
| 0 | Programmable interval timer |
| 1 | PS/2 keyboard |
| 2 | Cascade from the slave, never raised by a device |
| 3 | Serial port COM2 |
| 4 | Serial port COM1 |
| 5 | Parallel port 2 or a sound card |
| 6 | Floppy controller |
| 7 | Parallel port 1, also the spurious interrupt of the master |
| 8 | Real-time clock |
| 9 | Free, often ACPI |
| 10, 11 | Free, often PCI devices |
| 12 | PS/2 mouse |
| 13 | Math coprocessor |
| 14 | Primary ATA channel |
| 15 | Secondary ATA channel, also the slave's spurious interrupt |
The initialization sequence
An 8259 is configured by writing four initialization command words, ICW1 to ICW4, in order, after which it is ready for ordinary operation. The first goes to the command port and the other three to the data port. The Intel 8259A datasheet defines each bit; in the PC arrangement the values are fixed.
ICW1, 0x11, written to the command port, starts initialization: bit 4 is the marker that says this is ICW1, and bit 0 says an ICW4 will follow. The chip resets its internal state and treats the next three writes to its data port as ICW2, ICW3 and ICW4.
ICW2 is the vector offset: the vector the chip will use for its IRQ 0, with the others following in sequence. The master gets 0x20 (32), so its IRQs 0 to 7 become vectors 32 to 39; the slave gets 0x28 (40), so IRQs 8 to 15 become 40 to 47. The low three bits must be zero.
ICW3 describes the cascade, and means different things on the two chips. On the master it is a bit mask of the input lines that have a slave attached, 0x04, bit 2, for IRQ 2. On the slave it is the cascade identity, the number of the master's line it is attached to, 0x02.
ICW4 sets the mode. 0x01 selects 8086 mode, as opposed to the 8080 mode of the original; nothing else is needed for a normal PC.
After the sequence, the chip's mask register has been reset, so you restore the masks that were in force, or set your own. The complete routine, with I/O ports declared as inline assembly in the same style as the earlier posts:
#include <stdint.h>
static inline void outb(uint16_t port, uint8_t value)
{
__asm__ volatile ("outb %0, %1" : : "a"(value), "Nd"(port));
}
static inline uint8_t inb(uint16_t port)
{
uint8_t value;
__asm__ volatile ("inb %1, %0" : "=a"(value) : "Nd"(port));
return value;
}
#define PIC1_CMD 0x20
#define PIC1_DATA 0x21
#define PIC2_CMD 0xA0
#define PIC2_DATA 0xA1
#define PIC_EOI 0x20
static inline void io_wait(void)
{
outb(0x80, 0); /* port 0x80 is unused; the write takes about a microsecond */
}
void pic_remap(uint8_t offset1, uint8_t offset2)
{
uint8_t mask1 = inb(PIC1_DATA);
uint8_t mask2 = inb(PIC2_DATA);
outb(PIC1_CMD, 0x11); io_wait(); /* ICW1: start init, ICW4 needed */
outb(PIC2_CMD, 0x11); io_wait();
outb(PIC1_DATA, offset1); io_wait(); /* ICW2: vector offsets */
outb(PIC2_DATA, offset2); io_wait();
outb(PIC1_DATA, 0x04); io_wait(); /* ICW3: slave on IRQ 2 */
outb(PIC2_DATA, 0x02); io_wait(); /* ICW3: cascade identity 2 */
outb(PIC1_DATA, 0x01); io_wait(); /* ICW4: 8086 mode */
outb(PIC2_DATA, 0x01); io_wait();
outb(PIC1_DATA, mask1); /* restore the saved masks */
outb(PIC2_DATA, mask2);
}The io_wait calls matter on old hardware, where the 8259 was slow to accept consecutive writes and could miss one. A write to port 0x80, which the BIOS uses for POST codes and which nothing reads, takes roughly one bus cycle's worth of microseconds and serves as a short delay. On modern machines and in emulators the delay is not needed, and it does no harm. Call the function once, early, with the offsets 32 and 40: pic_remap(32, 40);.
Masking and unmasking
Each chip has an interrupt mask register, reached by reading and writing the data port in normal operation. A set bit disables the corresponding IRQ line. After the remap, a kernel normally masks everything, and unmasks a line only when the driver that handles it is ready:
void irq_mask(uint8_t irq)
{
uint16_t port = irq < 8 ? PIC1_DATA : PIC2_DATA;
uint8_t bit = irq < 8 ? irq : irq - 8;
outb(port, inb(port) | (1u << bit));
}
void irq_unmask(uint8_t irq)
{
uint16_t port = irq < 8 ? PIC1_DATA : PIC2_DATA;
uint8_t bit = irq < 8 ? irq : irq - 8;
outb(port, inb(port) & (uint8_t)~(1u << bit));
if (irq >= 8)
irq_unmask(2); /* the slave's output comes through master IRQ 2 */
}The detail in irq_unmask is easy to miss. The slave raises the master's IRQ 2 line, so IRQ 2 must be unmasked on the master for any IRQ from 8 to 15 to reach the CPU. A kernel that unmasks IRQ 14 for the disk and gets nothing has forgotten that.
The end of interrupt
An 8259 delivers an interrupt and then waits. It tracks which IRQs are in service in its In-Service Register, and it will not deliver another interrupt of equal or lower priority until the handler says it has finished, by writing the end-of-interrupt command, 0x20, to the command port. Forgetting it is the classic bug: the first timer tick arrives, and no second one ever does.
void pic_send_eoi(uint8_t irq)
{
if (irq >= 8)
outb(PIC2_CMD, PIC_EOI); /* the slave, then always the master too */
outb(PIC1_CMD, PIC_EOI);
}For an IRQ handled by the slave, both chips must be told, in that order, because the master also marked IRQ 2 as in service. The command is sent at the end of the handler, or at its start if the handler is long and the kernel wants other interrupts to be able to nest. It is never sent from code that is not responding to an interrupt, since an extra EOI can cancel an interrupt of higher priority that is still in service.
Dispatching to handlers
The vector arrives in the interrupt stub described in the earlier posts, so the C dispatcher subtracts the offset to get the IRQ number and calls a registered function:
typedef void (*irq_handler_t)(void);
static irq_handler_t irq_handlers[16];
void irq_register(uint8_t irq, irq_handler_t h)
{
irq_handlers[irq] = h;
}
static int irq_is_spurious(uint8_t irq); /* defined below */
void irq_dispatch(uint64_t vector)
{
uint8_t irq = (uint8_t)(vector - 32);
if (irq_is_spurious(irq))
return; /* no EOI for a spurious interrupt on the master */
if (irq_handlers[irq])
irq_handlers[irq]();
pic_send_eoi(irq);
}A timer that counts ticks is the first user:
static volatile uint64_t ticks;
static void timer_tick(void) { ticks++; }
/* in kmain, after the IDT is loaded */
pic_remap(32, 40);
irq_register(0, timer_tick);
irq_unmask(0);
__asm__ volatile ("sti");With the PIT programmed to 100 Hz, as in the post on the PIT and APIC timer, ticks increases by 100 each second, and a message printed every 100 ticks proves the whole path: remap, unmask, vector, stub, handler, EOI.
Spurious interrupts
An 8259 can report an interrupt that is no longer there. If an IRQ line goes high and low again, a glitch, between the moment the controller signals the CPU and the moment the CPU acknowledges, the controller has nothing to deliver, and by design it sends its lowest-priority vector instead: IRQ 7 for the master, IRQ 15 for the slave. The handler can tell a real IRQ 7 from a spurious one by reading the In-Service Register. A spurious interrupt has no bit set there.
The register is read by writing an OCW3 command to the command port, 0x0B to select the In-Service Register, then reading the same port:
static uint16_t pic_read_isr(void)
{
outb(PIC1_CMD, 0x0B);
outb(PIC2_CMD, 0x0B);
return (uint16_t)(inb(PIC2_CMD) << 8) | inb(PIC1_CMD);
}
static int irq_is_spurious(uint8_t irq)
{
uint16_t isr = pic_read_isr();
if (irq == 7 && !(isr & (1u << 7)))
return 1; /* master spurious: send nothing */
if (irq == 15 && !(isr & (1u << 15))) {
outb(PIC1_CMD, PIC_EOI); /* the master did see IRQ 2, so acknowledge that */
return 1;
}
return 0;
}The rule for acknowledging is the subtle part. A spurious IRQ 7 from the master gets no EOI at all, because nothing is in service. A spurious IRQ 15 from the slave gets no EOI to the slave, but does need one to the master, since the master really did take an interrupt on its IRQ 2 line to forward it. Handling these correctly keeps a stray glitch from wedging the interrupt path or from calling a driver for a device that did not interrupt. The command 0x0A instead selects the Interrupt Request Register, which shows which lines are asserted right now and is useful for debugging a device that does not seem to interrupt.
Turning the PIC off
Multiprocessor kernels and kernels that use the I/O APIC do not use the 8259. The right way to disable it is to remap it as above, so that any spurious interrupt it emits lands on a harmless vector, and then mask every line:
void pic_disable(void)
{
pic_remap(32, 40);
outb(PIC1_DATA, 0xFF);
outb(PIC2_DATA, 0xFF);
}On some chipsets the PIC's output is also gated by the Interrupt Mode Control Register, IMCR, at ports 0x22 and 0x23, to select between the PIC and the APIC path when running on a multiprocessor machine. The ACPI MADT table's flags say whether the IMCR is present, and the procedure is in programming the local APIC and I/O APIC. Do not skip the remap and the mask: masking alone leaves the controller at the default offsets, where a glitch lands on the page fault vector.
Testing and debugging
QEMU emulates the 8259 pair faithfully, and its monitor prints the controller state with info pic:
(qemu) info pic
pic0: irr=00 imr=fe isr=00 hprio=0 irq_base=20 rr_sel=0 elcr=00 fnm=0
pic1: irr=00 imr=ff isr=00 hprio=0 irq_base=28 rr_sel=0 elcr=00 fnm=0The values show a good configuration after pic_remap(32, 40) and unmasking only the timer: irq_base=20 and 28 are hexadecimal, 32 and 40; the master's imr of fe has only bit 0 clear, so only IRQ 0 is enabled; the slave's imr of ff masks everything; and isr=00 on both says no interrupt is currently in service, which means the handler is sending its EOI. If isr stays nonzero after the first tick, the handler never acknowledges. If the machine resets as soon as sti runs, the controller still has the BIOS vector offsets, or the IDT has no gate for vector 32.
Where this does not fit
The 8259 is a legacy device. It supports only 15 usable lines and a single CPU, and it cannot deliver an interrupt to a chosen processor. Every multicore system uses the APIC, and recent machines emulate the PIC only for compatibility, so a kernel meant for real hardware should plan to use the APIC and keep this code for emulators and early boot. Message-signaled interrupts, used by PCIe devices, bypass both controllers. The 8259 is also edge-triggered by default, and PCI devices that share an IRQ line need level-triggered mode, which the Edge/Level Control Registers at 0x4D0 and 0x4D1 configure on chipsets that have them.


