Repository navigation
06 interrupt
Language: English | 中文
Goal: understand the interrupt mechanism and interrupt controllers; see how EwokOS responds to hardware events; focus on how the timer interrupt drives the whole system's "heartbeat" and process scheduling. This chapter is code set #2 on the hands-on main line: gather your courage and wade a little deeper. Terminology (IRQ/FIQ/GIC/EOI/Timer…) is in the glossary.
Suppose there were no interrupts: for the kernel to know "has a key been pressed", it could only keep querying the device's status register nonstop (polling). Polling wastes the CPU, and the later you check, the later you respond.
Interrupts let the hardware notify the CPU proactively:
The CPU is running process A
│
SD card finished reading ──► interrupt controller ──► raises an IRQ
│ (Interrupt Request) signal at the CPU
CPU hardware automatically: stops A, saves the scene, jumps into the
kernel's interrupt-handling code
│
kernel: takes the data, wakes the waiting process
│
restore the scene (possibly resuming with process B instead)
An interrupt is one kind of Ch. 04's "exceptions". The handling framework is
fully reused: vector-table entry → save context → handle in C → restore →
eret.
The Pi 3B has around 60+ interrupt sources, all aggregated into the Broadcom
interrupt controller's three status registers (IRQ_BASIC_PENDING,
IRQ_PENDING_1, IRQ_PENDING_2). The ones that matter for this tutorial:
| Source | Number | Handled by | Significance |
|---|---|---|---|
| Timer (ARM generic timer) | BASIC 5 | the kernel | heartbeat and scheduling (this chapter's star) |
| Mini UART | IRQ1 29 | userland uartd
|
serial TX/RX |
| AUX (SPI1/2 shared) | IRQ1 29 | userland drivers | peripherals |
| EMMC (SD card) | IRQ2 30 | userland sdd
|
storage |
| GPIO | IRQ1 49/50 | userland drivers | buttons etc. |
Getting familiar with interrupts = getting familiar with this table. Every time you take on a new device, the first thing is to look up its interrupt number, then follow the same routine: enable → wait for the interrupt → handle → clear the flag.
Whatever the interrupt, the handling method is a fixed four-step template:
① Enable: write the interrupt controller's enable register + open the
CPU's master switch (DAIF)
② Identify: an interrupt arrives; read the pending register to see which
device it came from
③ Handle: do the work (take data / keep accounts / wake processes);
the faster the better
④ Clear: write the device's "clear interrupt" register + tell the
controller "I'm done", or the same interrupt re-enters like mad
Compare with the real code in EwokOS's Pi BSP (irq_pix.c, excerpt):
// ② Identify: check the status registers one by one
uint32_t pend = get32(IRQ_PENDING_1);
if(pend & (1 << 29)) { /* Mini UART has an interrupt */ }
// ④ Clear: write the device's clear-interrupt bit (AUX as an example)
put32(AUX_MU_IIR, 0x4); // write 1 to clear the interrupt flagThe easiest bug among the four steps is forgetting step ④: with the interrupt flag never cleared, the CPU gets pulled right back by the same interrupt upon returning — the system appears "frozen". When a real machine mysteriously freezes after boot, suspect this first.
Modern ARM chips don't wire every device directly into the CPU; they wire them into a module called the GIC (Generic Interrupt Controller). Its jobs:
- Gather all devices' interrupt requests;
- Arbitrate by priority (when several arrive at once, who goes first);
- Deliver the chosen interrupt to the right CPU core;
- Provide registers for the kernel to enable/mask an interrupt, query which interrupt fired, and signal "handling complete".
GIC versions and registers differ across chips, so EwokOS puts this part in the platform layer: gic.c.
The Pi 3B, however, uses Broadcom's own interrupt controller (not a standard GIC), in irq_pix.c:
void irq_enable_pix(uint32_t irq) {
if(IRQ_TIMER0 == irq){
*((uint32_t*)ENABLE_BASIC_IRQS) = (1 << 5); // write the register to enable the timer interrupt
}
}This is exactly the routine from Ch. 03: consult the manual, find the
register, write the memory. Boards differ (Pi3/Pi4) only in register
addresses, which is why the Pi BSP splits into pix/ (Pi2/Pi3) and pi4/
(Pi4).
Until now, our kernel has been a "passive" program: if nobody calls it, it does nothing. The timer interrupt changes everything — from the moment the clock is switched on, the system has its own heartbeat, and with it, "life": it wakes up every ~1 millisecond and proactively checks whether a process should be switched, whether time accounts need bookkeeping, and whether any sleeping process has come due.
Among all interrupts, the most important is the timer interrupt — it fires at a fixed frequency and is the operating system's source of time.
The last step of kernel startup (kernel.c):
timer_set_interval(0, _kernel_config.timer_freq); // default 1024 times/second
__irq_enable(); // open the master interrupt switchtimer_freq comes from the config file /etc/kernel/kernel.conf (config
files are covered in Ch. 11) and defaults to 1024 — one timer interrupt
every ~0.98 ms. That is the kernel's heartbeat.
The Pi implementation (timer.c) writes ARM's generic timer registers directly:
inline void write_cntv_tval(uint32_t tval) {
__asm volatile("msr CNTV_TVAL_EL0, %0" : : "r" (tval) : "memory");
}
// Write a countdown value; when it counts down to 0, an interrupt firesThe generic interrupt entry point is in irq.c;
the timer branch (irq_do_timer0) boils down to:
// Timer interrupt handling (simplified):
renew_kernel_tic(usec_gap); // ① keep the time accounts
arch_irq_timer_ack(); // ② clear the flag / reload (an arch hook — on
// the Pi it lands in the board's timer code)
schedule(ctx); // ③ take the chance to schedule! switch to the next processThe key design: every timer interrupt is an opportunity for a "forced
switch". Process A is running, its time slice expires, the interrupt
breaks it, and before returning the kernel calls schedule() to switch to
process B. That way no process can hog the CPU — this is how preemptive
multitasking is realized (scheduler details in Ch. 07).
The kernel also keeps accounts inside the interrupt (accumulating system
uptime and the CPU time each process has burned) — that data is exactly
what the ps command displays.
EwokOS's interrupt code comes in four layers with clear duties:
① platform assembly layer interrupt.S save/restore all registers (Ch. 04)
│ kernel/platform/<arch>/arch/
② generic kernel layer kernel/kernel/src/ irq.c: dispatch, accounting,
│ irq.c / interrupt.c timer → schedule; other IRQs →
│ forward to the user process
▼ that registered them
③ arch C layer kernel/platform/ implement the arch_* hooks the
│ <arch>/arch/common/ generic layer calls: arch_irq_raw
│ src/irq.c (which IRQ fired), arch_irq_timer_ack,
│ irq_eoi_arch (EOI), …
④ board layer hardwares/raspix/bsp/ read the board's interrupt
irq_pix.c registers; clear the flag, EOI
The split between ② and ③ is the recent "kernel arch porting friendly"
refactor: the generic irq.c contains no
architecture #ifs anymore — everything arch-specific (how to read the raw
IRQ vector, how to acknowledge the timer, how to EOI, how to dump a faulting
context) is declared as an arch_* hook in
kernel/irq.h and implemented per
platform in kernel/platform/<arch>/arch/common/src/irq.c. Porting to a new
architecture means writing that one file, not patching shared code.
Layer ② has a microkernel specialty: hardware interrupts can be forwarded to userland driver processes (interrupt.c):
int32_t interrupt_setup(proc_t* cproc, uint32_t interrupt,
ewokos_addr_t entry, ewokos_addr_t data);
int32_t interrupt_send(context_t* ctx, uint32_t interrupt);The flow: a driver process (e.g. uartd) calls SYS_INTR_SETUP to tell the
kernel "IRQ xx belongs to me"; when the interrupt arrives, the kernel wakes
the driver process (as if it received an event); the driver handles it and
calls SYS_INTR_END so the kernel can tell the hardware "done". The code
handling hardware interrupts runs in user mode — this is the microkernel's
essence, and the reason "a driver crash doesn't take down the system".
When the kernel modifies shared data (like the ready queue), its worst fear is being interrupted mid-way and scheduled off — leaving the data in an inconsistent state. Hence this common pattern in kernel code:
__irq_disable(); // mask interrupts (write DAIF / CPSR)
... critical-section operations ...
__irq_enable(); // unmask interruptskprintf does the same when writing to the UART (to keep output from
multiple cores/processes from interleaving):
// kernel/lib/src/kprintf.c (illustrative)
__irq_disable();
mcore_lock(&_kout_spin); // SMP: also take the spinlock (multiple cores writing at once)
... write the UART ...
mcore_unlock(&_kout_spin);
__irq_enable();Interrupts must not stay masked for long! While they're masked, every event is frozen. That's why interrupt handlers should be as short as possible — heavy work is left for kernel threads or userland drivers to chew on slowly.
- Boot the system under
hardwares/raspix/system, runps, and observe each process'scputime statistics — they all come from the timer interrupt's bookkeeping; - Read the
IRQ_TIMER0branch in irq.c (irq_do_timer0) and the matchingarch_irq_timer_ack()/arch_irq_raw()implementations underkernel/platform/aarch64/arch/common/src/irq.c, and confirm the "timer interrupt →schedule()" path; - Food for thought: what happens if the timer frequency is set to 1 (once
per second)? (Hint: a process's time slice becomes 1 second, and
interactive programs get visibly laggy. Change
timer_freqin/etc/kernel/kernel.confand experience it for real.)
- Interrupts let hardware proactively break into the CPU, replacing inefficient polling;
- The four-step interrupt template: enable → identify → handle → clear (forgetting to clear = freeze);
- Interrupt controllers (GIC / Broadcom's controller) gather and dispatch interrupts; the kernel enables/masks them via registers;
- The timer interrupt beats at
timer_freq(default 1024) times per second, driving preemptive scheduling — the moment the system "comes alive"; - Interrupt handling has four layers: assembly saves the scene → the generic
kernel dispatches → the arch C layer implements the
arch_*hooks → the board layer talks to the actual interrupt controller; - A microkernel forwards hardware interrupts to userland driver processes;
- Critical sections are protected by masking interrupts / spinlocks — but keep them short.
Next chapter is the kernel's heart: processes and the scheduler.
EwokOS OS Tutorial / EwokOS 操作系统教学
Part 1: Getting Started / 第 1 部分:起步
Part 2: The Kernel / 第 2 部分:内核
Part 3: Userland / 第 3 部分:用户态
Part 4: The Full System / 第 4 部分:完整系统
Part 5: The ewokos Series (Libraries) / 第 5 部分:ewokos 系列(库生态)
Part 6: GPU & Compute / 第 6 部分:GPU 与计算
Appendix / 附录