Interrupt Exceptions Interrupts maskable nonmaskable interrupt Exceptions Processordetected
Interrupt & Exceptions • Interrupts – maskable & nonmaskable interrupt • Exceptions – Processor-detected exceptions • Faults • Traps • Aborts – Programmed exceptions • software interrupt • int, int 3, into and bound • sysenter, syscall, sysexit, sysreturn
IRQ & interrupts • PIC – I/O programmable interrupt controller – Each IRQ line can be selectively disabled. • APIC – I/O advanced programmable interrupt controller – designed for SMP (SMT/CMP) systems • static distribution • dynamic distribution
Exceptions • For some exceptions, CPU push an error code in the kernel mode stack. • The kernel provides a dedicated handler for each exception type.
Interrupt descriptor table (IDT) Interrupt gate. . . Trap gate idtr: interrupt descriptor table register
Gate descriptors' format Interrupt gate. . . Trap gate
The invocation of an interrupt handler
kdb # cd /usr/src/kernels/2. 6. 25. 4 -shiwulo # ftp oss. sgi. com ftp> cd /www/projects/kdb/download/ ftp> cd v 4. 4 ftp> get kdb-v 4. 4 -2. 6. XX-common-Y. bz 2 ftp> get kdb-v 4. 4 -2. 6. xx-i 386 -Y. bz 2 ftp> bye # bzip 2 -d *. bz 2 # patch -p 1 < kdb-v 4. 4 -2. 6. XX-common-Y # patch -p 1 < kdb-v 4. 4 -2. 6. xx-i 386 -Y # cp /usr/src/kernels/2. 6. 25. fc 8 -x 86/. config # make menuconfige usr src kernels 2. 6. 25. 4 -shiwulo 2. 6. 25. fc 8 -x 86
make kernel # make -j 8 # make modules_install # make install # vim /boot/grub. conf default=0 timeout=5 splashimage=(hd 0, 0)/grub/splash. xpm. gz hiddenmenu title Fedora (2. 6. 24. 4 -shiwulo-04 -22 -a) root (hd 0, 0) kernel /vmlinuz-2. 6. 24. 4 -shiwulo-04 -22 -a ro root=LABEL=/ rhgb quiet vga=0 x 305 kdb console=tty. S 0, 38400 console=tty 0 initrd /initrd-2. 6. 24. 4 -shiwulo-04 -22 -a. img title Fedora (2. 6. 24. 4 -64. fc 8) root (hd 0, 0) kernel /vmlinuz-2. 6. 24. 4 -64. fc 8 ro root=LABEL=/ rhgb quiet initrd /initrd-2. 6. 24. 4 -64. fc 8. img title Fedora (2. 6. 23. 1 -42. fc 8) root (hd 0, 0) kernel /vmlinuz-2. 6. 23. 1 -42. fc 8 ro root=LABEL=/ rhgb quiet initrd /initrd-2. 6. 23. 1 -42. fc 8. img
/** Pressing the PAUSE key on the keyboard **/ kdb Entering kdb (current=0 xffff 81011 bc 98000, pid 0) on processor 2 due to Keyboard Entry [2]kdb> help Command Usage Description -----------------------------md <vaddr> Display Memory Contents, also md. Wc. N, e. g. md 8 c 1 mdr <vaddr> <bytes> Display Raw Memory mdp <paddr> <bytes> Display Physical Memory idt <int> [<count>] Display IDT. . . [2]kdb> idt 0 0 x 0000 intgate segment=0 x 0010 off=0 xffff 8100 d 110 (divide_error) dpl=0 0 x 0001 intgate segment=0 x 0010 off=0 xffff 81285 d 50 (debug) dpl=0 ist=4 0 x 0002 intgate segment=0 x 0010 off=0 xffff 81285 ea 0 (nmi) dpl=0 ist=3 0 x 0003 intgate segment=0 x 0010 off=0 xffff 81285 f 30 (int 3) dpl=3 ist=4 0 x 0004 intgate segment=0 x 0010 off=0 xffff 8100 cf 70 (overflow) dpl=3 0 x 0005 intgate segment=0 x 0010 off=0 xffff 8100 cf 80 (bounds) dpl=0 0 x 0006 intgate segment=0 x 0010 off=0 xffff 8100 cf 90 (invalid_op) dpl=0 0 x 0007 intgate segment=0 x 0010 off=0 xffff 8100 cf 60 (device_not_available) dpl=0
/** Pressing the PAUSE key on the keyboard **/ kdb Entering kdb (current=0 xffff 81011 bc 98000, pid 0) on processor 2 due to Keyboard Entry [2]kdb> bp 0 xffff 8100 d 110 [2]kdb> go # less test. c #include <stdio. h> int i; void main() { int a=10; int b = 0; printf("%d", a/b); } # gcc test. c #. /a. out 0 xffff 8100 d 110 divide_error: int 3 Entering kdb (current=0 xffff 8100 b 918 a 000, pid 22706) on processor 1 due to Breakpoint @ 0 xffff 8100 d 110 [2] kdb>
Task State Segment
Stack layouts U K
Stack layouts n. When an interrupt handler is executed, the processor automatically stores the value of some registers in the stack of the current task. n. The difference between calling a handler through a trap gate or an interrupt gate is that in the last case interrupts are automatically disabled. @user_mode_stack
Nested execution of exception & interrupt handlers task 2 task 1 k-ξ scheduler ISR 1 ISR 2 ISR 3
Linux implementations • Interrupt gate – all Linux interrupt handlers – DPL = 0 • System gate – int 3, into, bound, int $0 x 80 – DPL = 3 • Trap gate – most Linux exception handlers – DPL = 0 • sysenter, sysexit, syscall, sysret
The Linux structure usr exception sys. call krnl process CTX interrupt H/W softirq/ tasklet process CTX workqueue
Exception handling 1. Save the contents of most registers in the Kernel Mode stack (this part is coded in assembly language). 2. Handle the exception by means of a high-level C function. 3. Exit from the handler by means of the ret_from_exception( ) function.
Exception handling
Interrupt handling • I/O interrupts • Timer interrupts • Inter-processor interrupts (IPI)
The Linux structure usr exception sys. call krnl process CTX interrupt H/W softirq/ tasklet Top-half process CTX workqueue
BSD user process deferrable functions bottom half of kernel Interrupt The top half of the kernel provides services to processes in response to system call or traps. This software can be thought of as a library of routines shared by all processes. The bottom half of the kernel comprises routines that are invoked to handle hardware interrupt.
top & bottom halves • the processing of interrupts is split into two parts, or halves. – The interrupt handler is the top half. – Work that can be performed later is deferred until the bottom half. • In this chapter, we look at the top half; in the next chapter, we study the bottom.
IRQ handler • Drivers can register an interrupt handler and enable a given interrupt line for handling with the function request_irq()
IRQ handler • “irq” specifies the interrupt number to allocate. • “handler” is a function pointer to the actual interrupt handler that services this interrupt • “name” is an ASCII text representation of the device associated with the interrupt. • “dev” is used as an ID for shared interrupt lines.
IRQ handler Flags • IRQF_DISABLED—When set, this flag instructs the kernel to disable all interrupts when executing this interrupt handler • IRQF_SAMPLE_RANDOM—This flag specifies that interrupts generated by this device should contribute to the kernel entropy pool
IRQ handler • IRQF_SHARED—This flag specifies that the interrupt line can be shared among multiple interrupt handlers.
Freeing an Interrupt Handler • When a driver unloads, we need to unregister the interrupt handler • If the interrupt line is not shared, this function removes the handler and disables the line. • If the interrupt line is shared, the handler identified via dev is removed only.
Reentrancy and Interrupt Handlers • Interrupt handlers in Linux need not be reentrant. – When a given interrupt handler is executing, the corresponding interrupt line is masked out on all processors
Shared Handlers • The interrupt handler must be capable of distinguishing whether its device actually generated an interrupt. • This requires both hardware support and associated logic in the interrupt handler.
Interrupt Context • When executing an interrupt handler, the kernel is in interrupt context. • Interrupt context, on the other hand, is not associated with a process. – The current macro is not relevant • Interrupt context is time-critical – Busy looping is possible, but discouraged.
The path that an interrupt takes
ret_from_intr() • Written in assembly • If a reschedule is pending, and the kernel is returning to user-space, schedule() is called • If the kernel is returning to kernel-space schedule() is called only if the preempt_count is zero. • After schedule() returns, or if there is no work pending, the kernel resumes whatever was interrupted.
Disabling and Enabling Interrupts • The reason to control the interrupt system is to provide synchronization. – provides protection against concurrent access from a possible interrupt handler.
Global IRQ disabling? • were deprecated during 2. 5 because the performance issues.
- Slides: 37