Linux Kernel Development Robert Love 1 Contents Interrupts
Linux Kernel Development - Robert Love 설지훈 1
Contents © Interrupts and interrupt handlers © Bottom halves and deferring work 2
Introduction © Interrupts ² Interrupts are special electrical signals sent from hardware devices to the processor ² The operating system can service each interrupt with a unique handler ² These interrupt values are often called interrupt request (IRQ) lines Example, zero is timer, one is keyboard. 3
Interrupts and interrupt handlers © Interrupt handlers ² The function the kernel runs in response to a specific interrupt is called an interrupt handler or interrupt service routine (ISR) ² The interrupt handler for a device is part of the devices’s driver ² The interrupt handler execute in as short a period as possible 4
Registering an interrupt handler int request_irq (unsigned int irq, irqreturn_t (*handler)(int, void *, struct pt_regs *), unsigned long irqflags, const char * devname, void *dev_id) © Parameters ² ² ² © irq Specifies the interrupt number to allocate handler A pointer to the actual interrupt handler that services this interrupt irqflags SA_INTERRUPT, SA_SAMPLE_RANDOM, SA_SHIRQ devname An ASCII text representation of the device associated with the interrupt dev_id Used primarily for shared interrupt lines Note request_irq() might sleep and, therefore, cannot be called from interrupt context or other situations where code cannot block 5
Freeing an interrupt handler void free_irq (unsigned int irq, void *dev_id) © If the interrupt line is shared, the handler identified via dev_id is removed © A call to free_irq() must be made from process context 6
Writing an interrupt handler static irqreturn_t intr_handler (int irq, void *dev_id, struct pt_regs *regs) © Parameter ² regs Holds a pointer to a structure containing the processor registers and state prior to servicing the interrupt. They are rarely used, except for debugging © Return value of an interrupt handler ² IRQ_NONE Detects an interrupt for which its device was not the originator ² IRQ_HANDLED Correctly invoked, and its device did cause the interrupt 7
Shared handlers © A shared handler is registered and executed much like a non-shared handler. © Three main differences are ² The SA_SHIRQ flag must be set in the flags argument to request_irq() ² The dev_id argument must be unique to each registered handler. ² The interrupt handler must be capable of distinguishing whether its device actually generated an interrupt 8
Interrupt context © When executing an interrupt handler or bottom half, the kernel is in interrupt context ² ² Interrupt context handler is not associated with a process cannot sleep is time critical does not receive its own stack Instead, it shares the kernel stack of the process or idle task’s stack 9
Implementation of interrupt handling int handle_IRQ_event(unsigned int irq, struct pt_regs *regs, struct irqaction *action) { int status = 1; if (!(action->flags & SA_INTERRUPT)) local_irq_enable(); do { status |= action->flags; action ->handler(irq, action->dev_id, regs); action = action->next; } while (action); if (status & SA_SAMPLE_RANDOM) add_interrupt_randomness(irq); local_irq_disable(); return status; } 10
/proc/interrupts 11
Interrupt control © Reasons to control the interrupt system generally boil down to needing to provide synchronization ² Kernel code more generally needs to obtain some sort of lock to prevent access to shared data simultaneously from another processor ² These locks are often obtained in conjunction with disabling local interrupts 12
Disabling and Enabling interrupts © To disable interrupts locally for the current processor (and only the current processor) and later enable them: local_irq_disable(); /* interrupts are disabled */ local_irq_enabled(); 13
Bottom Halves and Deferring Work © Interrupt limitations include ² They need to run as quickly as possible ² Interrupt handlers are often very timing-critical because they deal with hardware ² Interrupt handlers do not run in process context, therefore, they cannot block © Managing interrupts is divided two parts ² Interrupt handlers (top halves) ² Interrupt-related work not performed by the interrupt handler (bottom halves) 14
Bottom halves © Why bottom halves? ² Interrupt handlers run with the current interrupt line disabled or all local interrupt disabled ² The bottom half runs later with all interrupts enabled Later is often simply not now © A world of bottom halves ² BH (bottom halves) Removed in 2. 5 ² Task queues Removed in 2. 5 ² Softirq Available since 2. 3 ² Tasklet Available since 2. 3 ² Work queues Available since 2. 5 15
Softirqs © Implementation of Softirqs ² Statically allocated at compile-time There can be a possible 32 softirqs (fixed) 16
- Slides: 16