tags : Operating Systems, Linux

Interrupt What?

  • Interrupt might sound like something bad/annoying
  • When you type, your keyboard is sending interrupts to the cpu; It’s something that needs immediate attention.
cat /proc/interrupts
irqtop

How an interrupt happens

  • Device raises an interrupt on the corresponding IRQ pin
  • PIC raises an interrupt on CPU INTR pin
    • PIC waits for CPU to acknowledge before issuing new stuff
  • If the interrupt is accept by the CPU
    • The processor suspends its current activities
    • Saves its state
    • Looks up an ISR entry for that specific interrupt from Interrupt Descriptor Table (IDT)
    • Executes a function called an ISR / interrupt handler.

Interrupt controller

IRQs need a Interrupt controller, maintains the index of IRQs.

  • Previously Programmable interrupt controller (PIC)
  • now APIC

Interrupt handler/ Interrupt service routine/ ISR

  • Interrupt handlers are expected to set interrupt flags
  • This is provided by OS
  • In Linux this is done in 3 phases: critical, immediate and deferred.

Interrupt Context

  • Interrupt Context = ISR return time - ISR start time (That time window)
  • Code is said to be running in Interrupt Context.
  • NO WAY TO DO CONTEXT SWITCH (no sleep, user memory access etc)
  • To keep the interrupt context short, it allows to have defered actions.
  • Tasklets (Defer Action)

    • Runs in interrupt context
    • Dynamically allocated
    • For most purposes, tasklets are much easier to write cuz no need to be reentrant.
  • Soft IRQs (Defer Action)

    • Runs in interrupt context
    • Statically allocated
    • Run concurrently on several CPUs, even if they are of the same type.
    • Reentrant functions, and must protect stuff with spin locks.
    $ irqtop -S
    HI       high priority tasklet softirq
    SCHED    schedule softirq
    TIMER    timer softirq
    RCU      RCU softirq
    NET_RX   network receive softirq
    BLOCK    block device softirq
    TASKLET  normal priority tasklet softirq
    HRTIMER  high resolution timer softirq
    NET_TX   network transmit softirq
    IRQ_POLL IO poll softirq
  • Workqueues (Defer Action)

    • Deferred work that runs in process context
    • Implemented on top of kernel threads.

Interrupt Descriptor Table (IDT)

  • Used as a jump table by the CPU when a given vector is triggered
  • May reside anywhere in physical memory
  • It stores pointers to ISR that will be automatically invoked by CPU on interrupt receive.

Interrupt request/ IRQ

Processors can enable/disable IRQs by setting interrupt masks. Certain IRQs that cannot be disabled.

Hardware interrupts (async / external execution/ HW to CPU)

  • hw interrupt request is an electronic signal from hw device external to the processor
    • Communicate that the device needs attention from the operating system (OS).
    • Each device is associated with a particular IRQ signal.

Exception interrupts (sync / internal execution/ CPU to Kernel)

Generated internally by the CPU and used to alert the running kernel of an event or situation which requires its attention.

Faults (Recoverable)

  • This is reported before the execution of the instruction
  • So can be usually corrected.
  • Eg. Page Fault

Traps (Recoverable)

  • Reported after the instruction executed in which exception was detected
  • Eg. debug trap, integer overflow

Aborts (Non Recoverable)

  • Imprecise exceptions.
  • Double fault, second fault while handling first
  • Typically do not allow reliable program restart.