350 likes | 363 Views
Explore interrupt types, handling, PIC, IRQs, top & bottom halves, enabling/disabling, SoftIRQs, tasklets, work queues, interrupt controllers, vectors, IRQ examples, and handling procedures.
E N D
CSC 660: Advanced OS Interrupts CSC 660: Advanced Operating Systems
Topics • Types of Interrupts • PIC and IRQs • Interrupt Handlers • Top Halves and Bottom Halves • Enabling/Disabling Interrupts • SoftIRQs • Tasklets • Work Queues • Timer Interrupts CSC 660: Advanced Operating Systems
How can hardware communicate with CPU? Busy Wait Issue hardware request. Wait in tight loop until receives answer. Polling Issue hardware request. Periodically check hardware status. Interrupts Issue hardware request. Hardware signals CPU when answer ready. CSC 660: Advanced Operating Systems
Types of Interrupts Synchronous Produced by CPU while executing instructions. Issues only after finishing execution of an instr. Often called exceptions. Ex: page faults, system calls, divide by zero Asynchronous Generated by other hardware devices. Occur at arbitrary times, including while CPU is busy executing an instruction. Ex: I/O, timer interrupts CSC 660: Advanced Operating Systems
Programmable Interrupt Controller PIC connects Hardware devices that issue IRQs. CPU: INTR pin and data bus. PIC features 15 IRQ lines Sharing and dynamic assignment of IRQs. Masking (disabling) of selected IRQs. CPU masking of all maskable interrupts: cli, sti. APIC: Advanced PIC Handles multiprocessor systems. CSC 660: Advanced Operating Systems
Interrupt Vectors CSC 660: Advanced Operating Systems
IRQ Example CSC 660: Advanced Operating Systems
IRQ Handling • Monitor IRQ lines for raised signals. If multiple IRQs raised, select lowest # IRQ. • If raised signal detected • Converts raised signal into vector (0-255). • Stores vector in I/O port, allowing CPU to read. • Sends raised signal to CPU INTR pin. • Waits for CPU to acknowledge interrupt. • Kernel runs do_IRQ(). • Clears INTR line. • Goto step 1. CSC 660: Advanced Operating Systems
do_IRQ • Kernel jumps to entry point in entry.S. • Entry point saves registers, calls do_IRQ(). • Finds IRQ number in saved %EAX register. • Looks up IRQ descriptor using IRQ #. • Acknowledges receipt of interrupt. • Disables interrupt delivery on line. • Calls handle_IRQ_event() to run handlers. • Cleans up and returns. • Jumps to ret_from_intr(). CSC 660: Advanced Operating Systems
handle_IRQ_event() fastcall int handle_IRQ_event(unsigned int irq, struct pt_regs *regs, struct irqaction *action) { int ret, retval = 0, status = 0; if (!(action->flags & SA_INTERRUPT)) local_irq_enable(); do { ret = action->handler(irq, action->dev_id, regs); if (ret == IRQ_HANDLED) status |= action->flags; retval |= ret; action = action->next; } while (action); if (status & SA_SAMPLE_RANDOM) add_interrupt_randomness(irq); local_irq_disable(); return retval; } CSC 660: Advanced Operating Systems
Interrupt Handlers Function kernel runs in response to interrupt. More than one handler can exist per IRQ. Must run quickly. Resume execution of interrupted code. How to deal with high work interrupts? Ex: network, hard disk CSC 660: Advanced Operating Systems
Top and Bottom Halves Top Half The interrupt handler. Current interrupt disabled, possibly all disabled. Runs in interrupt context, not process context. Can’t sleep. Acknowledges receipt of interrupt. Schedules bottom half to run later. Bottom Half Runs in process context with interrupts enabled. Performs most work required. Can sleep. Ex: copies network data to memory buffers. CSC 660: Advanced Operating Systems
Interrupt Context Not associated with a process. Cannot sleep: no task to reschedule. current macro points to interrupted process. Shares kernel stack of interrupted process. Be very frugal in stack usage. CSC 660: Advanced Operating Systems
Registering a Handler request_irq() Register an interrupt handler on a given line. free_irq() Unregister a given interrupt handler. Disable interrupt line if all handlers unregistered. CSC 660: Advanced Operating Systems
Registering a Handler int request_irq(unsigned int irq, irqreturn_t (*handler)(int, void *, struct pt_regs *), unsigned long irqflags, const char * devname, void *dev_id) irqflaqs = SA_INTERRUPT | SA_SAMPLE_RANDOM | SA_SHIRQ CSC 660: Advanced Operating Systems
Writing an Interrupt Handler irqreturn_t ih(int irq,void *devid,struct pt_regs *r) Differentiating between devices Pre-2.0: irq Current: dev_id Registers Pointer to registers before interrupt occurred. Return Values IRQ_NONE: Interrupt not for handler. IRQ_HANDLED: Interrupted handled. CSC 660: Advanced Operating Systems
RTC Handler irqreturn_t rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs) { spin_lock (&rtc_lock); rtc_irq_data += 0x100; rtc_irq_data &= ~0xff; if (rtc_status & RTC_TIMER_ON) mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100); spin_unlock (&rtc_lock); /* Now do the rest of the actions */ spin_lock(&rtc_task_lock); if (rtc_callback) rtc_callback->func(rtc_callback->private_data); spin_unlock(&rtc_task_lock); wake_up_interruptible(&rtc_wait); kill_fasync (&rtc_async_queue, SIGIO, POLL_IN); return IRQ_HANDLED; } CSC 660: Advanced Operating Systems
Interrupt Control Disable/Enable Local Interrupts local_irq_disable(); /* interrupts are disabled */ local_irq_enable(); Saving and Restoring IRQ state Useful when don’t know prior IRQ state. unsigned long flags; local_irq_save(flags); /* interrupts are disabled */ local_irq_restore(flags); /* interrupts in original state */ CSC 660: Advanced Operating Systems
Interrupt Control Disabling Specific Interrupts For legacy hardware, avoid for shared IRQ lines. disable_irq(irq) enable_irq(irq) What about other processors? Disable local interrupts + spin lock. We’ll talk about spin locks next time… CSC 660: Advanced Operating Systems
Bottom Halves Perform most work required by interrupt. Run in process context with interrupts enabled. Three forms of deferring work SoftIRQs Tasklets Work Queues CSC 660: Advanced Operating Systems
SoftIRQs Statically allocated at compile time. Only 32 softIRQs can exist (only 6 currently used.) struct softirq_action { void (*action)(struct softirq_action *); void *data; }; static struct softirq_action softirq_vec[32]; Tasklets built on SoftIRQs. All tasklets use one SoftIRQ. Dynamically allocated. CSC 660: Advanced Operating Systems
SoftIRQ Handlers Prototype void softirq_handler(struct softirq_action *) Calling my_softirq->action(my_softirq); Pre-emption SoftIRQs don’t pre-empt other softIRQs. Interrupt handlers can pre-empt softIRQs. Another softIRQ can run on other CPUs. CSC 660: Advanced Operating Systems
Executing SoftIRQs Interrupt handler marks softIRQ. Called raising the softirq. SoftIRQs checked for execution: In return from hardware interrupt code. In ksoftirq kernel thread. In any code that explicitly checks for softIRQs. do_softirq() Loops over all softIRQs. CSC 660: Advanced Operating Systems
Current SoftIRQs CSC 660: Advanced Operating Systems
Tasklets • Implemented as softIRQs. • Linked list of tasklet_struct objects. • Two priorities of tasklets: • HI: tasklet_hi_schedule() • TASKLET: tasklet_schedule() • Scheduled tasklets run via do_softirq() • HI action: tasklet_action() • TASKLET action: tasklet_hi_action() CSC 660: Advanced Operating Systems
ksoftirqd SoftIRQs may occur at high frequencies. SoftIRQs may re-raise themselves. Kernel will not handle re-raised softIRQs immediately in do_softirq(). Kernel thread ksoftirq solves problem. One thread per processor. Runs at lowest priority (nice +19). CSC 660: Advanced Operating Systems
Work Queues Defer work into a kernel thread. Execute in process context. One thread per processor: events/n. Processes can create own threads if needed. struct workqueue_struct { struct cpu_workqueue_struct cpu_wq[NR_CPUS]; const char *name; struct list_head list; /* Empty if single thread */ }; CSC 660: Advanced Operating Systems
Work Queue Data Structures worker thread cpu_workqueue_struct 1/CPU 1/thread type workqueue_struct work_struct work_struct work_struct 1/deferrable function CSC 660: Advanced Operating Systems
Worker Thread Each thread runs worker_thread() • Marks self as sleeping. • Adds self to wait queue. • If linked list of work empty, schedule(). • Else, marks self as running, removes from queue. • Calls run_workqueue() to perform work. CSC 660: Advanced Operating Systems
run_workqueue() • Loops through list of work_structs struct work_struct { unsigned long pending; struct list_head entry; void (*func)(void *); void *data; void *wq_data; struct timer_list timer; }; • Retrieves function, func, and arg, data • Removes entry from list, clears pending • Invokes function CSC 660: Advanced Operating Systems
Which Bottom Half to Use? • If needs to sleep, use work queue. • If doesn’t need to sleep, use tasklet. • What about serialization needs? CSC 660: Advanced Operating Systems
Timer Interrupt Executed HZ times a second. #define HZ 1000 /* <asm/param.h> */ Called the tick rate. Time between two interrupts is a tick. Driven by Programmable Interrupt Timer (PIT). Interrupt handler responsibilities Updating uptime, system time, kernel stats. Rescheduling if current has exhausted time slice. Balancing scheduler runqueues. Running dynamic timers. CSC 660: Advanced Operating Systems
Jiffies Jiffies = number of ticks since boot. extern unsigned long volatile jiffies; Incremented each timer interrupt. Uptime = jiffies/HZ seconds. Convert for user space: jiffies_to_clock_t() Comparing jiffies, while avoiding overflow. time_after(a, b): a > b time_before(a,b) a < b time_after_eq(a,b): a >= b time_before_eq(a,b): a <= b CSC 660: Advanced Operating Systems
Timer Interrupt Handler • Increments jiffies. • Update resource usages (sys + user time.) • Run dynamic timers. • Execute scheduler_tick(). • Update wall time. • Calculate load average. CSC 660: Advanced Operating Systems
References • Daniel P. Bovet and Marco Cesati, Understanding the Linux Kernel, 3rd edition, O’Reilly, 2005. • Johnathan Corbet et. al., Linux Device Drivers, 3rd edition, O’Reilly, 2005. • Robert Love, Linux Kernel Development, 2nd edition, Prentice-Hall, 2005. • Claudia Rodriguez et al, The Linux Kernel Primer, Prentice-Hall, 2005. • Peter Salzman et. al., Linux Kernel Module Programming Guide, version 2.6.1, 2005. • Andrew S. Tanenbaum, Modern Operating Systems, 3rd edition, Prentice-Hall, 2005. CSC 660: Advanced Operating Systems