Tutorial 3 - Linux Interrupt Handling -

Slides:



Advertisements
Similar presentations
Computer System Organization Computer-system operation – One or more CPUs, device controllers connect through common bus providing access to shared memory.
Advertisements

Exceptional Control Flow Processes Today. Control Flow Processors do only one thing: From startup to shutdown, a CPU simply reads and executes (interprets)
Interrupts Chapter 8 – pp Chapter 10 – pp Appendix A – pp 537 &
1/1/ / faculty of Electrical Engineering eindhoven university of technology Introduction Part 3: Input/output and co-processors dr.ir. A.C. Verschueren.
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.
Architectural Support for OS March 29, 2000 Instructor: Gary Kimura Slides courtesy of Hank Levy.
Figure 2.8 Compiler phases Compiling. Figure 2.9 Object module Linking.
6-1 I/O Methods I/O – Transfer of data between memory of the system and the I/O device Most devices operate asynchronously from the CPU Most methods involve.
Architectural Support for Operating Systems. Announcements Most office hours are finalized Assignments up every Wednesday, due next week CS 415 section.
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
Introduction to Interrupts
Computer System Laboratory
1 Computer System Overview Chapter 1 Review of basic hardware concepts.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Input/Output. Input/Output Problems Wide variety of peripherals —Delivering different amounts of data —At different speeds —In different formats All slower.
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.
Input and Output Computer Organization and Assembly Language: Module 9.
Contact Information Office: 225 Neville Hall Office Hours: Monday and Wednesday 12:00-1:00 and by appointment.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 5 part 4 Exceptions.
The Functions of Operating Systems Interrupts. Learning Objectives Explain how interrupts are used to obtain processor time. Explain how processing of.
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.
© 2004, D. J. Foreman 1 Computer Organization. © 2004, D. J. Foreman 2 Basic Architecture Review  Von Neumann ■ Distinct single-ALU & single-Control.
Accessing I/O Devices Processor Memory BUS I/O Device 1 I/O Device 2.
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.
© 2004, D. J. Foreman 1 Computer Organization. © 2004, D. J. Foreman 2 Basic Architecture Review  Von Neumann ■ Distinct single-ALU & single-Control.
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.
1 CSE451 Architectural Supports for Operating Systems Autumn 2002 Gary Kimura Lecture #2 October 2, 2002.
Exceptional Control Flow Topics Exceptions except1.ppt CS 105 “Tour of the Black Holes of Computing”
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
Preemptive Context Switching
Processes & Threads Introduction to Operating Systems: Module 5.
1 Lecture 1: Computer System Structures We go over the aspects of computer architecture relevant to OS design  overview  input and output (I/O) organization.
Interrupt driven I/O Computer Organization and Assembly Language: Module 12.
OSes: 2. Structs 1 Operating Systems v Objective –to give a (selective) overview of computer system architectures Certificate Program in Software Development.
Managing Processors Jeff Chase Duke University. The story so far: protected CPU mode user mode kernel mode kernel “top half” kernel “bottom half” (interrupt.
Lecture 5 Rootkits Hoglund/Butler (Chapters 1-3).
Operating Systems Unit 2: – Process Context switch Interrupt Interprocess communication – Thread Thread models Operating Systems.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Synchronization.
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
Introduction to Exceptions 1 Introduction to Exceptions ARM Advanced RISC Machines.
Embedded Real-Time Systems Processing interrupts Lecturer Department University.
Introduction to Operating Systems Concepts
Computer System Structures Interrupts
Linux Kernel Development - Robert Love
Interrupts and signals
Interfacing with Hardware
Microprocessor Systems Design I
Anton Burtsev February, 2017
Day 08 Processes.
Day 09 Processes.
Computer System Overview
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Architectural Support for OS
Computer Organization
Top Half / Bottom Half Processing
CSE 451: Operating Systems Autumn 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 596 Allen Center 1.
CSE 451: Operating Systems Autumn 2001 Lecture 2 Architectural Support for Operating Systems Brian Bershad 310 Sieg Hall 1.
CSE 451: Operating Systems Winter 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 412 Sieg Hall 1.
Architectural Support for OS
COMP3221: Microprocessors and Embedded Systems
Interrupts and Interrupt Handling
Chapter 13: I/O Systems.
Presentation transcript:

Tutorial 3 - Linux Interrupt Handling - Bogdan Simion

Today’s tutorial Getting started with Linux programming Interrupt need and interrupt types Hardware support for interrupts Interrupt handling process Upper and bottom halves Concurrency considerations Implementing an interrupt handler

Getting started Linux Kernel Development, by Robert Love High-level, good starting point Understanding the Linux Kernel, by D.Bovet and M.Cesati More advanced, lots of details Linux Device Drivers, A.Rubini and J.Corbet Cross-reference Linux sources – with hyperlinks! http://lxr.linux.no Really useful to understand code and data structures

Interrupts An event external to the currently executing process that causes a change in the normal flow of instruction execution; usually generated by hardware devices external to the CPU Asynchronous w.r.t current process External & internal devices need CPU service CPU must detect devices that require attention!

Alternatives Polling: CPU checks each device periodically Too much overhead - CPU time wasted polling Efficient if events arrive fast, or if not urgent (slow polling at large intervals) Interrupts: Each device gets an “interrupt line” Device signals CPU when it needs attention, CPU handles request when it comes in No overhead / wasted cycles Good for events that are urgent, and/or infrequent

Interrupt types Hardware: An event/electronic signal from external device that needs CPU attention Mouse moved, keyboard pressed Printer ready, modem, etc Software: exceptions (traps) in the processor: divide by zero exception, page faults, etc. special software interrupt instructions (e.g., request disk reads/writes to disk controller)

Hardware support for interrupts Devices are connected to a shared message bus to the APIC Limited number of IRQ lines After every instruction (user-mode), CPU checks for hardware interrupt signals and, if present, calls an interrupt handler (kernel routine) Advanced Programmable Interrupt Controller

Load Interrupt Handler Interrupt handling Program instruction Interrupt pending YES Switch to kernel mode NO Save current PC Interrupt detected => Switch to kernel mode => Push PC onto stack => Load PC of interrupt handler from IVT (Interrupt Vector Table) The IVT is the list of handler addresses, and it’s mapped into kernel memory space so we can retrieve the interrupt handler fast. Load Interrupt Handler Execute IH Restore PC Program instruction

Interrupt Descriptor Table x86 implementation of IVT for fast interrupt handling Reserved chunk of RAM, used by the CPU to quickly branch to a specific interrupt handler Mapped to kernel space at 0x0000-0x03ff (256 4-byte pointers) on 8086 Later CPUs: flexible locations and different size First 32 entries (0x00-0x1F) - reserved for mapping handlers for CPU-specific exceptions (faults) Next entries – interrupt routines (e.g., keyboard) An Interrupt Vector Table (IVT) is a list of handler addresses that the CPU has to determine which interrupt handler to dispatch for each interrupt code. On x86 architectures, we have something called the IDT – provides special instr and data structures managed by the CPU itself so the interrupt handling can be as fast as possible. 32 and up -> Keyboard, video, IDE, etc

Interrupt handlers Fast/Hard/First-Level Interrupt Handler (FLIH) Quickly service an interrupt, minimal exec time Schedule SLIHs if needed Slow/Soft/Second-Level Interrupt Handler (SLIH) Long-lived interrupt processing tasks Lower priority - sit in a task runqueue Executed by a pool of kernel threads, when no FLIHs Linux: FLIHs = upper halves (UH) SLIHs = bottom halves (BH) Windows Deferred Procedure Calls (DPCs) FLIHs implement a minimum platform-specific interrupt handling – goal is to quickly service the interrupt, and schedule the execution of a SLI for further long-lived interrupt handling. SLIHs – take longer to execute, lower priority, executed whenever CPU is not executing a fast interrupt handler For instance, when a network interface reports the arrival of a new packet, the handler just retrieves the data and pushes it up to the protocol layer; actual processing of the packet is performed in a bottom half.

Bottom halves (BH) SoftIRQs and Tasklets Workqueues Deferred work runs in interrupt context Don’t run in process context Can’t sleep Tasklets somewhat easier to use Workqueues Run in kernel threads Schedulable Can sleep Also can’t block on a mutex.

Concurrency Hardware interrupts (IRQs) can arrive while a specific interrupt handler is in execution Fast interrupts must run atomically => Disable all interrupts and restore them when done As a result, fast interrupts must run fast, and defer long-lived work to bottom halves. Otherwise => interrupt storm => livelocks If FLIHs take too long, then other devices that require attention are waiting. Worse, same device might have more data to deliver to the OS before previous data is completely received. This can cause h/w failures, buffer overflows, dropped data, messages, etc. Inside the OS, this can lead to an interrupt storm: whenever there’s always an interrupt waiting whenever a fast int handler finishes its execution. This could occur either because the FLIH takes too long, or H/W has bugs. If the OS is continuously handling interrupts, it never runs any application code (livelock). So it never appears to respond to user inputs, the user sees the system as being frozen even though it’s running.

Interrupt enabling IE (Interrupt Enable) bit in the status register can be set or reset by the processor cli = clear interrupts sti = set interrupts Must be careful with semantics if using these directly cli disables interrupts on ALL processors If you are already handling an IRQ, cli only disables them on current CPU Basically CPU can decide to ignore the PIC’s requests and continue running, if IF (interrupt flag) is disabled in the status register Generally discouraged to use cli/sti directly.

Multiprocessors Linux kernel tries to divide interrupts evenly across processors to some extent Fast interrupts (SA_INTERRUPT) execute with all other interrupts disabled on the current processor Other processors can still handle interrupts, though not the same IRQ at the same time

Interrupt handling internals (x86) Each interrupt goes through do_IRQ A do_IRQ acquires spinlock on the irq#, preventing other CPUs from handling this IRQ Looks up handler If no handler, schedule bottom halves (if any) and return If handler, run handle_IRQ_event to invoke the handlers

Implementing an interrupt handler Use request_irq()to get interrupt handler irq (IRQ number) handler (func. pointer - interrupt handler) flags (SA_INTERRUPT, SA_SHIRQ, etc) dev_name (string used in /proc/interrupts) dev_id (used for shared interrupt lines) Fast handler - always with SA_INTERRUPT From within interrupt handler, schedule BH to run (tasklet_schedule, queue_work, etc.) Dev_name: – name of the device that registered this handler for the current interrupt “irq” (1st param)

Implementing an interrupt handler(2) A driver might need to disable/enable interrupt reporting for its own IRQ line only Kernel functions: disable_irq (int irq) disable_irq_nosync (int irq) enable_irq (int irq) Enable/disable IRQ - across ALL processors Nosync – doesn’t wait for currently executing IH’s to complete => faster, but leaves driver open to race conditions Disable_irq will not only disable that IRQ but will also wait for a currently executing interrupt handler, if any, to complete. Disable_irq will return immediately -> faster, but leaves your driver open to race conditions. Enable/disable this IRQ across all processors. IRQ lines are H/W lines on which devices can send interrupt signals to the processor (the APIC actually). Some lines are fixed for certain types of devices while some can be multiplexed between various devices. The APIC controller translates the IRQ number into the Interrupt Vector for the CPU’s table. Basically based on the IRQ line that signalled it, the APIC gives an INT (e.g., INT 0eh) to the CPU. When a CPU is finished handling an interrupt, it tells the APIC it’s ok to resume sending interrupts. If conflict – Plug and Play solves it. Also, interrupts have priorities.

Useful readings Linux Device Drivers, 3rd edition http://lwn.net/Kernel/LDD3/ Chapter 10 – Interrupt Handling The Linux Kernel Module Programming guide http://www.tdlp.org/LDP/lkmpg/2.6/html/ Chapter 12 – Interrupt Handlers Understanding the Linux Kernel Chapter 4 – Interrupts and Exceptions Consult LXR – Deep understanding of Linux source code and data structures involved