CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Interrupts.

Slides:



Advertisements
Similar presentations
Tutorial 3 - Linux Interrupt Handling -
Advertisements

I/O Unit.
CS 6560 Operating System Design Lecture 7: Kernel Synchronization Kernel Time Management.
Interrupts (Hardware). Interrupt Descriptor Table Slide #2 IDT specified as a segment using the IDTR register.
The Process Control Block From: A Process Control Block (PCB, also called Task Control Block or Task Struct) is.
Interrupts What is an interrupt? What does an interrupt do to the “flow of control” Interrupts used to overlap computation & I/O – Examples would be console.
COMP3221: Microprocessors and Embedded Systems Lecture 15: Interrupts I Lecturer: Hui Wu Session 1, 2005.
Figure 2.8 Compiler phases Compiling. Figure 2.9 Object module Linking.
OS Spring’03 Introduction Operating Systems Spring 2003.
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
Hardware Support for Operating Systems Sunny Gleason Vivek Uppal COM S 414
Homework / Exam Return and Review Exam #1 Reading Machine Projects
Computer System Laboratory
I/O Tanenbaum, ch. 5 p. 329 – 427 Silberschatz, ch. 13 p
1 CS503: Operating Systems Part 1: OS Interface Dongyan Xu Department of Computer Science Purdue University.
Interrupt Handling Sarah Diesburg COP Interrupt Handling One big responsibility of an operating system is to handle hardware connected to the machine.
Interrupts. What Are Interrupts? Interrupts alter a program’s flow of control  Behavior is similar to a procedure call »Some significant differences.
1 Computer System Overview Chapter 1. 2 n An Operating System makes the computing power available to users by controlling the hardware n Let us review.
Recall: Three I/O Methods Synchronous: Wait for I/O operation to complete. Asynchronous: Post I/O request and switch to other work. DMA (Direct Memory.
1 Chapter 2: Computer-System Structures  Computer System Operation  I/O Structure  Storage Structure  Storage Hierarchy  Hardware Protection  General.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Tami Meredith, Ph.D. CSCI  Devices need CPU access  E.g., NIC has a full buffer it needs to empty  These device needs are often asynchronous.
I/O Interfacing A lot of handshaking is required between the CPU and most I/O devices. All I/O devices operate asynchronously with respect to the CPU.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Memory Addressing / Kernel Modules.
Multiprogramming. Readings r Silberschatz, Galvin, Gagne, “Operating System Concepts”, 8 th edition: Chapter 3.1, 3.2.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Synchronization.
Scheduling Lecture 6. What is Scheduling? An O/S often has many pending tasks. –Threads, async callbacks, device input. The order may matter. –Policy,
Fall 2013 SILICON VALLEY UNIVERSITY CONFIDENTIAL 1 Introduction to Embedded Systems Dr. Jerry Shiao, Silicon Valley University.
Accessing I/O Devices Processor Memory BUS I/O Device 1 I/O Device 2.
13-Nov-15 (1) CSC Computer Organization Lecture 7: Input/Output Organization.
CSNB374: Microprocessor Systems Chapter 5: Procedures and Interrupts.
Lab 13 Department of Computer Science and Information Engineering National Taiwan University Lab13 – Interrupt + Timer 2014/12/23 1 /16.
Sogang University Advanced Operating Systems (Enhanced Device Driver Operations) Advanced Operating Systems (Enhanced Device Driver Operations) Sang Gue.
Interrupt driven I/O. MIPS RISC Exception Mechanism The processor operates in The processor operates in user mode user mode kernel mode kernel mode Access.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Processes.
Preemptive Context Switching
Ch 7. Interrupts and Interrupt Handlers. Overview (1) A primary responsibility of the kernel is managing the hardware connected to the machine  The kernel.
CSNB334 Advanced Operating Systems 6. Device Management Lecturer: Asma Shakil.
7. IRQ and PIC ENGI 3655 Lab Sessions. Richard Khoury2 Textbook Readings  Interrupts ◦ Section
CSC 660: Advanced Operating Systems
Interrupt driven I/O Computer Organization and Assembly Language: Module 12.
Managing Processors Jeff Chase Duke University. The story so far: protected CPU mode user mode kernel mode kernel “top half” kernel “bottom half” (interrupt.
ICOM Noack Linux I/O structure Device special files Device switch tables and fops How the kernel finds a device Parts of a device driver or module.
Interrupt-Driven I/O There are different types of interrupts –Hardware Generated by the 8259 PIC – signals the CPU to suspend execution of the current.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Synchronization.
Ch 6. Interrupts and Interrupt Handlers. Overview (1) A primary responsibility of the kernel is managing the hardware connected to the machine  The kernel.
Interrupts and Exception Handling. Execution We are quite aware of the Fetch, Execute process of the control unit of the CPU –Fetch and instruction as.
Interrupts and Interrupt Handling David Ferry, Chris Gill CSE 522S - Advanced Operating Systems Washington University in St. Louis St. Louis, MO
INTERRUPTS. Topics to be discussed  8088/86 Hardware Interrupts pins 8088/86 Hardware Interrupts pins   Pin description Pin description.
Introduction to Exceptions 1 Introduction to Exceptions ARM Advanced RISC Machines.
Embedded Real-Time Systems Processing interrupts Lecturer Department University.
Lecture 7 Interrupt ,Trap and System Call
Interrupts and Exceptions 國立中正大學 資訊工程研究所 羅習五 老師. Interrupt & Exceptions Interrupts – maskable & nonmaskable interrupt Exceptions – Processor-detected.
Timers and Time Management Ok-Kyun Ha
Computer System Structures Interrupts
Linux Kernel Development - Robert Love
Interrupts and exceptions
Interrupts and signals
Operating Systems CMPSC 473
Interfacing with Hardware
Microprocessor Systems Design I
Anton Burtsev February, 2017
Homework Reading Labs S&S Extracts ,
Interrupts and Interrupt Handling
COMPUTER PERIPHERALS AND INTERFACES
Top Half / Bottom Half Processing
Linux Kernel Programming CIS 4930/COP 5641
COMP3221: Microprocessors and Embedded Systems
Interrupts and Interrupt Handling
Chapter 13: I/O Systems “The two main jobs of a computer are I/O and [CPU] processing. In many cases, the main job is I/O, and the [CPU] processing is.
Presentation transcript:

CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Interrupts

CSC 660: Advanced Operating SystemsSlide #2 Topics 1.Types of Interrupts 2.PIC and IRQs 3.Interrupt Handlers 4.Top Halves and Bottom Halves 5.Enabling/Disabling Interrupts 6.SoftIRQs 7.Tasklets 8.Work Queues 9.Timer Interrupts

CSC 660: Advanced Operating SystemsSlide #3 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 SystemsSlide #4 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 SystemsSlide #5 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 SystemsSlide #6 Interrupt Vectors Vector RangeUse 0-19Nonmaskable interrupts and exceptions Intel-reserved External interrupts (IRQs) 128System Call exception External interrupts (IRQs) 239Local APIC timer interrupt 240Local APIC thermal interrupt Reserved by Linux for future use Interprocessor interrupts 254Local APIC error interrupt 255Local APIC suprious interrupt

CSC 660: Advanced Operating SystemsSlide #7 IRQ Example IRQINTHardware Device 032Timer 133Keyboard 234PIC Cascading 335Second serial port 436First serial port 638Floppy Disk 840System Clock 1042Network Interface 1143USB port, sound card 1244PS/2 Mouse 1345Math Coprocessor 1446EIDE first controller 1547EIDE second controller

CSC 660: Advanced Operating SystemsSlide #8 IRQ Handling 1.Monitor IRQ lines for raised signals. If multiple IRQs raised, select lowest # IRQ. 2.If raised signal detected 1.Converts raised signal into vector (0-255). 2.Stores vector in I/O port, allowing CPU to read. 3.Sends raised signal to CPU INTR pin. 4.Waits for CPU to acknowledge interrupt. 5.Kernel runs do_IRQ(). 6.Clears INTR line. 3.Goto step 1.

CSC 660: Advanced Operating SystemsSlide #9 do_IRQ 1.Kernel jumps to entry point in entry.S. 2.Entry point saves registers, calls do_IRQ(). 3.Finds IRQ number in saved %EAX register. 4.Looks up IRQ descriptor using IRQ #. 5.Acknowledges receipt of interrupt. 6.Disables interrupt delivery on line. 7.Calls handle_IRQ_event() to run handlers. 8.Cleans up and returns. 9.Jumps to ret_from_intr().

CSC 660: Advanced Operating SystemsSlide #10 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 SystemsSlide #11 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 SystemsSlide #12 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 SystemsSlide #13 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 SystemsSlide #14 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 SystemsSlide #15 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 SystemsSlide #16 Writing an Interrupt Handler 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. irqreturn_t ih(int irq,void *devid,struct pt_regs *r)

CSC 660: Advanced Operating SystemsSlide #17 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 SystemsSlide #18 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 SystemsSlide #19 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 SystemsSlide #20 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 SystemsSlide #21 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 SystemsSlide #22 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 SystemsSlide #23 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 SystemsSlide #24 Current SoftIRQs SoftIRQPriorityDescription HI0High priority tasklets. TIMER1Timer bottom half. NET_TX2Send network packets. NET_RX3Receive network packets. SCSI4SCSI bottom half. TASKLET5Tasklets.

CSC 660: Advanced Operating SystemsSlide #25 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 SystemsSlide #26 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 SystemsSlide #27 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 SystemsSlide #28 Work Queue Data Structures worker thread work_struct cpu_workqueue_struct 1/CPU workqueue_struct 1/thread type work_struct 1/deferrable function

CSC 660: Advanced Operating SystemsSlide #29 Worker Thread Each thread runs worker_thread() 1.Marks self as sleeping. 2.Adds self to wait queue. 3.If linked list of work empty, schedule(). 4.Else, marks self as running, removes from queue. 5.Calls run_workqueue() to perform work.

CSC 660: Advanced Operating SystemsSlide #30 run_workqueue() 1.Loops through list of work_struct s 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 SystemsSlide #31 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 HalfContextSerialization SoftirqInterruptNone TaskletInterruptAgainst same tasklet Work queuesProcessNone

CSC 660: Advanced Operating SystemsSlide #32 Timer Interrupt Executed HZ times a second. #define HZ 1000 /* */ 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 SystemsSlide #33 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 SystemsSlide #34 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 SystemsSlide #35 References 1.Daniel P. Bovet and Marco Cesati, Understanding the Linux Kernel, 3 rd edition, O’Reilly, Johnathan Corbet et. al., Linux Device Drivers, 3 rd edition, O’Reilly, Robert Love, Linux Kernel Development, 2 nd edition, Prentice-Hall, Claudia Rodriguez et al, The Linux Kernel Primer, Prentice-Hall, Peter Salzman et. al., Linux Kernel Module Programming Guide, version 2.6.1, Andrew S. Tanenbaum, Modern Operating Systems, 3rd edition, Prentice-Hall, 2005.