CSC 660 Advanced OS Interrupts CSC 660 Advanced
![CSC 660: Advanced OS Interrupts CSC 660: Advanced Operating Systems 1 CSC 660: Advanced OS Interrupts CSC 660: Advanced Operating Systems 1](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-1.jpg)
![Topics 1. 2. 3. 4. 5. 6. 7. 8. 9. Types of Interrupts PIC Topics 1. 2. 3. 4. 5. 6. 7. 8. 9. Types of Interrupts PIC](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-2.jpg)
![How can hardware communicate with CPU? Busy Wait Issue hardware request. Wait in tight How can hardware communicate with CPU? Busy Wait Issue hardware request. Wait in tight](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-3.jpg)
![Types of Interrupts Synchronous Produced by CPU while executing instructions. Issues only after finishing Types of Interrupts Synchronous Produced by CPU while executing instructions. Issues only after finishing](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-4.jpg)
![Programmable Interrupt Controller PIC connects Hardware devices that issue IRQs. CPU: INTR pin and Programmable Interrupt Controller PIC connects Hardware devices that issue IRQs. CPU: INTR pin and](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-5.jpg)
![Interrupt Vectors Vector Range Use 0 -19 Nonmaskable interrupts and exceptions. 20 -31 Intel-reserved Interrupt Vectors Vector Range Use 0 -19 Nonmaskable interrupts and exceptions. 20 -31 Intel-reserved](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-6.jpg)
![IRQ Example IRQ INT Hardware Device 0 32 Timer 1 33 Keyboard 2 34 IRQ Example IRQ INT Hardware Device 0 32 Timer 1 33 Keyboard 2 34](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-7.jpg)
![IRQ Handling 1. Monitor IRQ lines for raised signals. If multiple IRQs raised, select IRQ Handling 1. Monitor IRQ lines for raised signals. If multiple IRQs raised, select](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-8.jpg)
![do_IRQ 1. 2. 3. 4. 5. 6. 7. 8. 9. Kernel jumps to entry do_IRQ 1. 2. 3. 4. 5. 6. 7. 8. 9. Kernel jumps to entry](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-9.jpg)
![handle_IRQ_event() fastcall int handle_IRQ_event(unsigned int irq, struct pt_regs *regs, struct irqaction *action) { int handle_IRQ_event() fastcall int handle_IRQ_event(unsigned int irq, struct pt_regs *regs, struct irqaction *action) { int](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-10.jpg)
![Interrupt Handlers Function kernel runs in response to interrupt. More than one handler can Interrupt Handlers Function kernel runs in response to interrupt. More than one handler can](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-11.jpg)
![Top and Bottom Halves Top Half The interrupt handler. Current interrupt disabled, possibly all Top and Bottom Halves Top Half The interrupt handler. Current interrupt disabled, possibly all](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-12.jpg)
![Interrupt Context Not associated with a process. Cannot sleep: no task to reschedule. current Interrupt Context Not associated with a process. Cannot sleep: no task to reschedule. current](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-13.jpg)
![Registering a Handler request_irq() Register an interrupt handler on a given line. free_irq() Unregister Registering a Handler request_irq() Register an interrupt handler on a given line. free_irq() Unregister](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-14.jpg)
![Registering a Handler int request_irq(unsigned int irq, irqreturn_t (*handler)(int, void *, struct pt_regs *), Registering a Handler int request_irq(unsigned int irq, irqreturn_t (*handler)(int, void *, struct pt_regs *),](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-15.jpg)
![Writing an Interrupt Handler irqreturn_t ih(int irq, void *devid, struct pt_regs *r) Differentiating between Writing an Interrupt Handler irqreturn_t ih(int irq, void *devid, struct pt_regs *r) Differentiating between](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-16.jpg)
![RTC Handler irqreturn_t rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs) { spin_lock (&rtc_lock); rtc_irq_data RTC Handler irqreturn_t rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs) { spin_lock (&rtc_lock); rtc_irq_data](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-17.jpg)
![Interrupt Control Disable/Enable Local Interrupts local_irq_disable(); /* interrupts are disabled */ local_irq_enable(); Saving and Interrupt Control Disable/Enable Local Interrupts local_irq_disable(); /* interrupts are disabled */ local_irq_enable(); Saving and](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-18.jpg)
![Interrupt Control Disabling Specific Interrupts For legacy hardware, avoid for shared IRQ lines. disable_irq(irq) Interrupt Control Disabling Specific Interrupts For legacy hardware, avoid for shared IRQ lines. disable_irq(irq)](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-19.jpg)
![Bottom Halves Perform most work required by interrupt. Run in process context with interrupts Bottom Halves Perform most work required by interrupt. Run in process context with interrupts](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-20.jpg)
![Soft. IRQs Statically allocated at compile time. Only 32 soft. IRQs can exist (only Soft. IRQs Statically allocated at compile time. Only 32 soft. IRQs can exist (only](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-21.jpg)
![Soft. IRQ Handlers Prototype void softirq_handler(struct softirq_action *) Calling my_softirq->action(my_softirq); Pre-emption Soft. IRQs don’t Soft. IRQ Handlers Prototype void softirq_handler(struct softirq_action *) Calling my_softirq->action(my_softirq); Pre-emption Soft. IRQs don’t](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-22.jpg)
![Executing Soft. IRQs Interrupt handler marks soft. IRQ. Called raising the softirq. Soft. IRQs Executing Soft. IRQs Interrupt handler marks soft. IRQ. Called raising the softirq. Soft. IRQs](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-23.jpg)
![Current Soft. IRQs Soft. IRQ Priority Description HI 0 High priority tasklets. TIMER 1 Current Soft. IRQs Soft. IRQ Priority Description HI 0 High priority tasklets. TIMER 1](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-24.jpg)
![Tasklets • Implemented as soft. IRQs. – Linked list of tasklet_struct objects. • Two Tasklets • Implemented as soft. IRQs. – Linked list of tasklet_struct objects. • Two](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-25.jpg)
![ksoftirqd Soft. IRQs may occur at high frequencies. Soft. IRQs may re-raise themselves. Kernel ksoftirqd Soft. IRQs may occur at high frequencies. Soft. IRQs may re-raise themselves. Kernel](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-26.jpg)
![Work Queues Defer work into a kernel thread. Execute in process context. One thread Work Queues Defer work into a kernel thread. Execute in process context. One thread](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-27.jpg)
![Work Queue Data Structures worker thread cpu_workqueue_struct 1/CPU workqueue_struct 1/thread type work_struct CSC 660: Work Queue Data Structures worker thread cpu_workqueue_struct 1/CPU workqueue_struct 1/thread type work_struct CSC 660:](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-28.jpg)
![Worker Thread Each thread runs worker_thread() 1. 2. 3. 4. 5. Marks self as Worker Thread Each thread runs worker_thread() 1. 2. 3. 4. 5. Marks self as](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-29.jpg)
![run_workqueue() 1. Loops through list of work_structs struct work_struct { unsigned long pending; struct run_workqueue() 1. Loops through list of work_structs struct work_struct { unsigned long pending; struct](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-30.jpg)
![Which Bottom Half to Use? 1. If needs to sleep, use work queue. 2. Which Bottom Half to Use? 1. If needs to sleep, use work queue. 2.](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-31.jpg)
![Timer Interrupt Executed HZ times a second. #define HZ 1000 /* <asm/param. h> */ Timer Interrupt Executed HZ times a second. #define HZ 1000 /* <asm/param. h> */](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-32.jpg)
![Jiffies = number of ticks since boot. extern unsigned long volatile jiffies; Incremented each Jiffies = number of ticks since boot. extern unsigned long volatile jiffies; Incremented each](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-33.jpg)
![Timer Interrupt Handler 1. Increments jiffies. 2. Update resource usages (sys + user time. Timer Interrupt Handler 1. Increments jiffies. 2. Update resource usages (sys + user time.](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-34.jpg)
![References 1. 2. 3. 4. 5. 6. Daniel P. Bovet and Marco Cesati, Understanding References 1. 2. 3. 4. 5. 6. Daniel P. Bovet and Marco Cesati, Understanding](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-35.jpg)
- Slides: 35
![CSC 660 Advanced OS Interrupts CSC 660 Advanced Operating Systems 1 CSC 660: Advanced OS Interrupts CSC 660: Advanced Operating Systems 1](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-1.jpg)
CSC 660: Advanced OS Interrupts CSC 660: Advanced Operating Systems 1
![Topics 1 2 3 4 5 6 7 8 9 Types of Interrupts PIC Topics 1. 2. 3. 4. 5. 6. 7. 8. 9. Types of Interrupts PIC](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-2.jpg)
Topics 1. 2. 3. 4. 5. 6. 7. 8. 9. Types of Interrupts PIC and IRQs Interrupt Handlers Top Halves and Bottom Halves Enabling/Disabling Interrupts Soft. IRQs Tasklets Work Queues Timer Interrupts CSC 660: Advanced Operating Systems 2
![How can hardware communicate with CPU Busy Wait Issue hardware request Wait in tight How can hardware communicate with CPU? Busy Wait Issue hardware request. Wait in tight](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-3.jpg)
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 3
![Types of Interrupts Synchronous Produced by CPU while executing instructions Issues only after finishing Types of Interrupts Synchronous Produced by CPU while executing instructions. Issues only after finishing](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-4.jpg)
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 4
![Programmable Interrupt Controller PIC connects Hardware devices that issue IRQs CPU INTR pin and Programmable Interrupt Controller PIC connects Hardware devices that issue IRQs. CPU: INTR pin and](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-5.jpg)
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 5
![Interrupt Vectors Vector Range Use 0 19 Nonmaskable interrupts and exceptions 20 31 Intelreserved Interrupt Vectors Vector Range Use 0 -19 Nonmaskable interrupts and exceptions. 20 -31 Intel-reserved](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-6.jpg)
Interrupt Vectors Vector Range Use 0 -19 Nonmaskable interrupts and exceptions. 20 -31 Intel-reserved 32 -127 External interrupts (IRQs) 128 System Call exception 129 -238 External interrupts (IRQs) 239 Local APIC timer interrupt 240 Local APIC thermal interrupt 241 -250 Reserved by Linux for future use 251 -253 Interprocessor interrupts 254 Local APIC error interrupt 255 Local APIC suprious interrupt CSC 660: Advanced Operating Systems 6
![IRQ Example IRQ INT Hardware Device 0 32 Timer 1 33 Keyboard 2 34 IRQ Example IRQ INT Hardware Device 0 32 Timer 1 33 Keyboard 2 34](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-7.jpg)
IRQ Example IRQ INT Hardware Device 0 32 Timer 1 33 Keyboard 2 34 PIC Cascading 3 35 Second serial port 4 36 First serial port 6 38 Floppy Disk 8 40 System Clock 10 42 Network Interface 11 43 USB port, sound card 12 44 PS/2 Mouse 13 45 Math Coprocessor 14 46 EIDE first controller 15 47 EIDE second controller CSC 660: Advanced Operating Systems 7
![IRQ Handling 1 Monitor IRQ lines for raised signals If multiple IRQs raised select IRQ Handling 1. Monitor IRQ lines for raised signals. If multiple IRQs raised, select](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-8.jpg)
IRQ Handling 1. Monitor IRQ lines for raised signals. If multiple IRQs raised, select lowest # IRQ. 2. If raised signal detected 1. 2. 3. 4. 5. 6. 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. 3. Goto step 1. CSC 660: Advanced Operating Systems 8
![doIRQ 1 2 3 4 5 6 7 8 9 Kernel jumps to entry do_IRQ 1. 2. 3. 4. 5. 6. 7. 8. 9. Kernel jumps to entry](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-9.jpg)
do_IRQ 1. 2. 3. 4. 5. 6. 7. 8. 9. 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 9
![handleIRQevent fastcall int handleIRQeventunsigned int irq struct ptregs regs struct irqaction action int handle_IRQ_event() fastcall int handle_IRQ_event(unsigned int irq, struct pt_regs *regs, struct irqaction *action) { int](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-10.jpg)
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 10
![Interrupt Handlers Function kernel runs in response to interrupt More than one handler can Interrupt Handlers Function kernel runs in response to interrupt. More than one handler can](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-11.jpg)
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 11
![Top and Bottom Halves Top Half The interrupt handler Current interrupt disabled possibly all Top and Bottom Halves Top Half The interrupt handler. Current interrupt disabled, possibly all](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-12.jpg)
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 12
![Interrupt Context Not associated with a process Cannot sleep no task to reschedule current Interrupt Context Not associated with a process. Cannot sleep: no task to reschedule. current](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-13.jpg)
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 13
![Registering a Handler requestirq Register an interrupt handler on a given line freeirq Unregister Registering a Handler request_irq() Register an interrupt handler on a given line. free_irq() Unregister](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-14.jpg)
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 14
![Registering a Handler int requestirqunsigned int irq irqreturnt handlerint void struct ptregs Registering a Handler int request_irq(unsigned int irq, irqreturn_t (*handler)(int, void *, struct pt_regs *),](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-15.jpg)
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 15
![Writing an Interrupt Handler irqreturnt ihint irq void devid struct ptregs r Differentiating between Writing an Interrupt Handler irqreturn_t ih(int irq, void *devid, struct pt_regs *r) Differentiating between](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-16.jpg)
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 16
![RTC Handler irqreturnt rtcinterruptint irq void devid struct ptregs regs spinlock rtclock rtcirqdata RTC Handler irqreturn_t rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs) { spin_lock (&rtc_lock); rtc_irq_data](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-17.jpg)
RTC Handler irqreturn_t rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs) { spin_lock (&rtc_lock); rtc_irq_data += 0 x 100; rtc_irq_data &= ~0 xff; 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 17
![Interrupt Control DisableEnable Local Interrupts localirqdisable interrupts are disabled localirqenable Saving and Interrupt Control Disable/Enable Local Interrupts local_irq_disable(); /* interrupts are disabled */ local_irq_enable(); Saving and](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-18.jpg)
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 18
![Interrupt Control Disabling Specific Interrupts For legacy hardware avoid for shared IRQ lines disableirqirq Interrupt Control Disabling Specific Interrupts For legacy hardware, avoid for shared IRQ lines. disable_irq(irq)](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-19.jpg)
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 19
![Bottom Halves Perform most work required by interrupt Run in process context with interrupts Bottom Halves Perform most work required by interrupt. Run in process context with interrupts](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-20.jpg)
Bottom Halves Perform most work required by interrupt. Run in process context with interrupts enabled. Three forms of deferring work Soft. IRQs Tasklets Work Queues CSC 660: Advanced Operating Systems 20
![Soft IRQs Statically allocated at compile time Only 32 soft IRQs can exist only Soft. IRQs Statically allocated at compile time. Only 32 soft. IRQs can exist (only](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-21.jpg)
Soft. IRQs Statically allocated at compile time. Only 32 soft. IRQs 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 Soft. IRQs. All tasklets use one Soft. IRQ. Dynamically allocated. CSC 660: Advanced Operating Systems 21
![Soft IRQ Handlers Prototype void softirqhandlerstruct softirqaction Calling mysoftirqactionmysoftirq Preemption Soft IRQs dont Soft. IRQ Handlers Prototype void softirq_handler(struct softirq_action *) Calling my_softirq->action(my_softirq); Pre-emption Soft. IRQs don’t](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-22.jpg)
Soft. IRQ Handlers Prototype void softirq_handler(struct softirq_action *) Calling my_softirq->action(my_softirq); Pre-emption Soft. IRQs don’t pre-empt other soft. IRQs. Interrupt handlers can pre-empt soft. IRQs. Another soft. IRQ can run on other CPUs. CSC 660: Advanced Operating Systems 22
![Executing Soft IRQs Interrupt handler marks soft IRQ Called raising the softirq Soft IRQs Executing Soft. IRQs Interrupt handler marks soft. IRQ. Called raising the softirq. Soft. IRQs](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-23.jpg)
Executing Soft. IRQs Interrupt handler marks soft. IRQ. Called raising the softirq. Soft. IRQs checked for execution: In return from hardware interrupt code. In ksoftirq kernel thread. In any code that explicitly checks for soft. IRQs. do_softirq() Loops over all soft. IRQs. CSC 660: Advanced Operating Systems 23
![Current Soft IRQs Soft IRQ Priority Description HI 0 High priority tasklets TIMER 1 Current Soft. IRQs Soft. IRQ Priority Description HI 0 High priority tasklets. TIMER 1](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-24.jpg)
Current Soft. IRQs Soft. IRQ Priority Description HI 0 High priority tasklets. TIMER 1 Timer bottom half. NET_TX 2 Send network packets. NET_RX 3 Receive network packets. SCSI 4 SCSI bottom half. TASKLET 5 Tasklets. CSC 660: Advanced Operating Systems 24
![Tasklets Implemented as soft IRQs Linked list of taskletstruct objects Two Tasklets • Implemented as soft. IRQs. – Linked list of tasklet_struct objects. • Two](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-25.jpg)
Tasklets • Implemented as soft. IRQs. – 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 25
![ksoftirqd Soft IRQs may occur at high frequencies Soft IRQs may reraise themselves Kernel ksoftirqd Soft. IRQs may occur at high frequencies. Soft. IRQs may re-raise themselves. Kernel](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-26.jpg)
ksoftirqd Soft. IRQs may occur at high frequencies. Soft. IRQs may re-raise themselves. Kernel will not handle re-raised soft. IRQs immediately in do_softirq(). Kernel thread ksoftirq solves problem. One thread per processor. Runs at lowest priority (nice +19). CSC 660: Advanced Operating Systems 26
![Work Queues Defer work into a kernel thread Execute in process context One thread Work Queues Defer work into a kernel thread. Execute in process context. One thread](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-27.jpg)
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 27
![Work Queue Data Structures worker thread cpuworkqueuestruct 1CPU workqueuestruct 1thread type workstruct CSC 660 Work Queue Data Structures worker thread cpu_workqueue_struct 1/CPU workqueue_struct 1/thread type work_struct CSC 660:](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-28.jpg)
Work Queue Data Structures worker thread cpu_workqueue_struct 1/CPU workqueue_struct 1/thread type work_struct CSC 660: Advanced Operating Systems 1/deferrable function 28
![Worker Thread Each thread runs workerthread 1 2 3 4 5 Marks self as Worker Thread Each thread runs worker_thread() 1. 2. 3. 4. 5. Marks self as](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-29.jpg)
Worker Thread Each thread runs worker_thread() 1. 2. 3. 4. 5. 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 29
![runworkqueue 1 Loops through list of workstructs struct workstruct unsigned long pending struct run_workqueue() 1. Loops through list of work_structs struct work_struct { unsigned long pending; struct](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-30.jpg)
run_workqueue() 1. 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; }; 2. Retrieves function, func, and arg, data 3. Removes entry from list, clears pending 4. Invokes function CSC 660: Advanced Operating Systems 30
![Which Bottom Half to Use 1 If needs to sleep use work queue 2 Which Bottom Half to Use? 1. If needs to sleep, use work queue. 2.](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-31.jpg)
Which Bottom Half to Use? 1. If needs to sleep, use work queue. 2. If doesn’t need to sleep, use tasklet. 3. What about serialization needs? Bottom Half Softirq Context Interrupt Serialization None Tasklet Interrupt Against same tasklet Work queues Process None CSC 660: Advanced Operating Systems 31
![Timer Interrupt Executed HZ times a second define HZ 1000 asmparam h Timer Interrupt Executed HZ times a second. #define HZ 1000 /* <asm/param. h> */](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-32.jpg)
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 32
![Jiffies number of ticks since boot extern unsigned long volatile jiffies Incremented each Jiffies = number of ticks since boot. extern unsigned long volatile jiffies; Incremented each](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-33.jpg)
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 33
![Timer Interrupt Handler 1 Increments jiffies 2 Update resource usages sys user time Timer Interrupt Handler 1. Increments jiffies. 2. Update resource usages (sys + user time.](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-34.jpg)
Timer Interrupt Handler 1. Increments jiffies. 2. Update resource usages (sys + user time. ) 3. Run dynamic timers. 4. Execute scheduler_tick(). 5. Update wall time. 6. Calculate load average. CSC 660: Advanced Operating Systems 34
![References 1 2 3 4 5 6 Daniel P Bovet and Marco Cesati Understanding References 1. 2. 3. 4. 5. 6. Daniel P. Bovet and Marco Cesati, Understanding](https://slidetodoc.com/presentation_image/1225281964502a1672b7684b40afa3aa/image-35.jpg)
References 1. 2. 3. 4. 5. 6. Daniel P. Bovet and Marco Cesati, Understanding the Linux Kernel, 3 rd edition, O’Reilly, 2005. Johnathan Corbet et. al. , Linux Device Drivers, 3 rd edition, O’Reilly, 2005. Robert Love, Linux Kernel Development, 2 nd 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, 3 rd edition, Prentice-Hall, 2005. CSC 660: Advanced Operating Systems 35
Os context switch
Csc 660
Csc 660
Csc 660
Csc 660
Os kernel
O'rta asrlarda jismoniy tarbiya
Art 660
93 000 in scientific notation
Healthscope benefits.com
Geoliner 660
What are hardware interrupts
What is an interrupt?
Ceg exception
Types of hardware interrupt
Classes of interrupts
What is interrupt
Sofia is angry that alisa interrupts her
Uart interrupts
5 types of software interrupts
Dos interrupts in 8086
Cara kerja polling adalah
Program flow of control without and with interrupts
Assembly interrupts
Time sequence of multiple interrupts
Cse307
Ie register in 8051
Program flow control with interrupts
Difference between dos and bios interrupts
Ivt table in 8086
Unexpected kernel mode trap
Programming serial communication interrupts in 8051
Interrupt in 8051 microcontroller
Dos interrupt in 8086
Interrupt vector table 8051
Interrupt in microprocessor