Linux Interrupts: The Basic Concepts
Mika J. Järvenpää University of Helsinki Department of Computer Science [email protected].fi
ABSTRACT The idea of this document is to shed some light to the con- Table 1: Signals sent by the exception handlers cepts and main ideas of implementation of interrupts in # Exception Handler Signal Linux kernel. Main focus is in Linux kernel version 2.4.18- 0 Divide error divide error() SIGFPE 10. 1 Debug debug() SIGTRAP 2 NMI nmi() None 3 Breakpoint int3() SIGTRAP 1. LINUX INTERRUPTS 4 Overflow overflow() SIGSEGV 5 Bounds check bounds() SIGSEGV At any time one CPU in a Linux system can be: 6 Invalid opcode invalid op() SIGILL - serving a hardware interrupt in kernel mode 7 Device not avail. device not avail.() SIGSEGV - serving a softirq, tasklet or bottom half in kernel mode - running a process in user mode. 8 Double fault double fault() SIGSEGV There is a strict order between these. Other than the last 9 Coproc.seg.ovr. coproc. seg. ovr.() SIGFPE category (user mode) can only be preempted by those above. 10 Invalid TSS invalid tss() SIGSEGV For example, while a softirq is running on a CPU, no other 11 Seg. not present seg. not present() SIGBUS softirqs will preempt it, but a hardware interrupt can. 12 Stack seg. fault stack segment() SIGBUS In the next chapters different interrupt and exception types, 13 General protect. general prot.() SIGSEGV initialization, hardware handling and software handling of 14 Page fault page fault() SIGSEGV interrupts, interrupt data structures and terms like IDT, 15 Intel reserved None None bottom half, softirq and tasklet are explained in more detail. 16 FP error coprocessor error() SIGFPE 17 Alignment check alignment check() SIGSEGV 18 Machine check machine check() None 2. INTERRUPTS AND EXCEPTIONS 19 SIMD coproc.err simd coproc. error Depends An interrupt is an asynchronous event that is typically generated by an I/O device not synchronized to processor instruction execution. 2.1 Exception and Interrupt Vectors An exception is a synchronous event that is generated when Intel architecture identifies different interrupts and excep- the processor detects one or more predefined conditions tions by a number ranging from 0 to 255 ([5], p. 4-11). while executing an instruction. This number is called a vector. Linux uses vectors 0 to Interrupts can be divided to maskable interrupts and 31, which are for exceptions and non-maskable interrupts, non-maskable interrupts. vectors 32 to 47, which are for maskable interrupts ie. in- Also exceptions can be divided to processor-detected terrupts caused by interrupt requests (IRQs) and only one exceptions ie. faults, traps, aborts and programmed vector (128) from the remaining vectors ranging from 48 to exceptions. Example of fault is Page fault. Traps are used 255, which are meant for software interrupts. mainly for debugging purposes. Aborts inform about hard- Linux uses this vector 128 to implement a system call ie. ware failures and invalid system tables and and abort han- when an int 0x80 opcode ([6]) is executed by a process dler has no choice but to force affected process to terminate. running in user mode the CPU switches into kernel mode [1], [3], p. 4-9, 4-10. and starts executing kernel function system call(). 2.2 Hardware Interrupts Hardware devices capable of issuing IRQs are connected to Interrupt Controller. The Intel 8259A Programmable Inter- rupt Controller (PIC) handles up to eight vectored prior- ity interrupts for the CPU and PICs can be cascaded ([2]). Typical configuration for 15 IRQs is cascade of two PICs. PIC can remember one IRQ while IRQ is masked. Vector of masked IRQ is sent to CPU after unmasking. Processing of previous interrupt is finished by writing End Of Interrupt (EOI) to PIC or both PICs in cascade, if source was the Task Gate Descriptor slave PIC. Since the number of available IRQ lines is lim- 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 ited the same IRQ line can be shared between several I/O RESERVED P DPL 0 0 1 0 1 RESERVED devices. This is possible, if interrupt handlers poll every TSS SEGMENT SELECTOR RESERVED I/O device connected to same IRQ line to determine which 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 I/O device should be serviced. Intel processors having local Interrupt Gate Descriptor APIC (more about APICs in section: APIC System) could 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 receive external interrupts through pins on the processor or OFFSET (16-31) P DPL 0 1 1 1 0 0 0 0 RESERVED through the local APIC serial bus. All maskable hardware SEGMENT SELECTOR OFFSET (0-15) interrupts ie. interrupts delivered to CPU by means of INTR 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 signal (0-255) or through local APIC (16-255) can be masked as a group by using IF flag in the EFLAGS register. Trap