Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interrupts and Exceptions 國立中正大學 資訊工程研究所 羅習五 老師. Interrupt & Exceptions Interrupts – maskable & nonmaskable interrupt Exceptions – Processor-detected.

Similar presentations


Presentation on theme: "Interrupts and Exceptions 國立中正大學 資訊工程研究所 羅習五 老師. Interrupt & Exceptions Interrupts – maskable & nonmaskable interrupt Exceptions – Processor-detected."— Presentation transcript:

1 Interrupts and Exceptions 國立中正大學 資訊工程研究所 羅習五 老師

2 Interrupt & Exceptions Interrupts – maskable & nonmaskable interrupt Exceptions – Processor-detected exceptions Faults Traps Aborts – Programmed exceptions software interrupt int, int3, into and bound sysenter, syscall, sysexit, sysreturn

3 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

4 Exceptions For some exceptions, CPU push an error code in the kernel mode stack. The kernel provides a dedicated handler for each exception type.

5 Interrupt descriptor table (IDT) Interrupt gate...... Trap gate idtr idtr: interrupt descriptor table register

6 Gate descriptors' format Interrupt gate...... Trap gate

7 The invocation of an interrupt handler

8 # cd /usr/src/kernels/2.6.25.4-shiwulo # ftp oss.sgi.com ftp> cd /www/projects/kdb/download/ ftp> cd v4.4 ftp> get kdb-v4.4-2.6.XX-common-Y.bz2 ftp> get kdb-v4.4-2.6.xx-i386-Y.bz2 ftp> bye # bzip2 -d *.bz2 # patch -p1 < kdb-v4.4-2.6.XX-common-Y # patch -p1 < kdb-v4.4-2.6.xx-i386-Y # cp /usr/src/kernels/2.6.25.fc8-x86/.config./.config # make menuconfige # cd /usr/src/kernels/2.6.25.4-shiwulo # ftp oss.sgi.com ftp> cd /www/projects/kdb/download/ ftp> cd v4.4 ftp> get kdb-v4.4-2.6.XX-common-Y.bz2 ftp> get kdb-v4.4-2.6.xx-i386-Y.bz2 ftp> bye # bzip2 -d *.bz2 # patch -p1 < kdb-v4.4-2.6.XX-common-Y # patch -p1 < kdb-v4.4-2.6.xx-i386-Y # cp /usr/src/kernels/2.6.25.fc8-x86/.config./.config # make menuconfige kdb usr src kernels 2.6.25.4-shiwulo 2.6.25.fc8-x86

9

10

11 # make -j8 # make modules # make modules_install # make install # vim /boot/grub/grub.conf default=0 timeout=5 splashimage=(hd0,0)/grub/splash.xpm.gz hiddenmenu title Fedora (2.6.24.4-shiwulo-04-22-a) root (hd0,0) kernel /vmlinuz-2.6.24.4-shiwulo-04-22-a ro root=LABEL=/ rhgb quiet vga=0x305 kdb console=ttyS0,38400 console=tty0 initrd /initrd-2.6.24.4-shiwulo-04-22-a.img title Fedora (2.6.24.4-64.fc8) root (hd0,0) kernel /vmlinuz-2.6.24.4-64.fc8 ro root=LABEL=/ rhgb quiet initrd /initrd-2.6.24.4-64.fc8.img title Fedora (2.6.23.1-42.fc8) root (hd0,0) kernel /vmlinuz-2.6.23.1-42.fc8 ro root=LABEL=/ rhgb quiet initrd /initrd-2.6.23.1-42.fc8.img # make -j8 # make modules # make modules_install # make install # vim /boot/grub/grub.conf default=0 timeout=5 splashimage=(hd0,0)/grub/splash.xpm.gz hiddenmenu title Fedora (2.6.24.4-shiwulo-04-22-a) root (hd0,0) kernel /vmlinuz-2.6.24.4-shiwulo-04-22-a ro root=LABEL=/ rhgb quiet vga=0x305 kdb console=ttyS0,38400 console=tty0 initrd /initrd-2.6.24.4-shiwulo-04-22-a.img title Fedora (2.6.24.4-64.fc8) root (hd0,0) kernel /vmlinuz-2.6.24.4-64.fc8 ro root=LABEL=/ rhgb quiet initrd /initrd-2.6.24.4-64.fc8.img title Fedora (2.6.23.1-42.fc8) root (hd0,0) kernel /vmlinuz-2.6.23.1-42.fc8 ro root=LABEL=/ rhgb quiet initrd /initrd-2.6.23.1-42.fc8.img make kernel

12 /** Pressing the PAUSE key on the keyboard **/ Entering kdb (current=0xffff81011bc98000, pid 0) on processor 2 due to Keyboard Entry [2]kdb> help Command Usage Description ---------------------------------------------------------- md Display Memory Contents, also mdWcN, e.g. md8c1 mdr Display Raw Memory mdp Display Physical Memory idt [ ] Display IDT... [2]kdb> idt 0 0x0000 intgate segment=0x0010 off=0xffffffff8100d110 (divide_error) dpl=0 0x0001 intgate segment=0x0010 off=0xffffffff81285d50 (debug) dpl=0 ist=4 0x0002 intgate segment=0x0010 off=0xffffffff81285ea0 (nmi) dpl=0 ist=3 0x0003 intgate segment=0x0010 off=0xffffffff81285f30 (int3) dpl=3 ist=4 0x0004 intgate segment=0x0010 off=0xffffffff8100cf70 (overflow) dpl=3 0x0005 intgate segment=0x0010 off=0xffffffff8100cf80 (bounds) dpl=0 0x0006 intgate segment=0x0010 off=0xffffffff8100cf90 (invalid_op) dpl=0 0x0007 intgate segment=0x0010 off=0xffffffff8100cf60 (device_not_available) dpl=0 /** Pressing the PAUSE key on the keyboard **/ Entering kdb (current=0xffff81011bc98000, pid 0) on processor 2 due to Keyboard Entry [2]kdb> help Command Usage Description ---------------------------------------------------------- md Display Memory Contents, also mdWcN, e.g. md8c1 mdr Display Raw Memory mdp Display Physical Memory idt [ ] Display IDT... [2]kdb> idt 0 0x0000 intgate segment=0x0010 off=0xffffffff8100d110 (divide_error) dpl=0 0x0001 intgate segment=0x0010 off=0xffffffff81285d50 (debug) dpl=0 ist=4 0x0002 intgate segment=0x0010 off=0xffffffff81285ea0 (nmi) dpl=0 ist=3 0x0003 intgate segment=0x0010 off=0xffffffff81285f30 (int3) dpl=3 ist=4 0x0004 intgate segment=0x0010 off=0xffffffff8100cf70 (overflow) dpl=3 0x0005 intgate segment=0x0010 off=0xffffffff8100cf80 (bounds) dpl=0 0x0006 intgate segment=0x0010 off=0xffffffff8100cf90 (invalid_op) dpl=0 0x0007 intgate segment=0x0010 off=0xffffffff8100cf60 (device_not_available) dpl=0 kdb

13 /** Pressing the PAUSE key on the keyboard **/ Entering kdb (current=0xffff81011bc98000, pid 0) on processor 2 due to Keyboard Entry [2]kdb> bp 0xffffffff8100d110 [2]kdb> go # less test.c #include int i; void main() { int a=10; int b = 0; printf("%d", a/b); } # gcc test.c #./a.out 0xffffffff8100d110 divide_error: int3 Entering kdb (current=0xffff8100b918a000, pid 22706) on processor 1 due to Breakpoint @ 0xffffffff8100d110 [2] kdb> /** Pressing the PAUSE key on the keyboard **/ Entering kdb (current=0xffff81011bc98000, pid 0) on processor 2 due to Keyboard Entry [2]kdb> bp 0xffffffff8100d110 [2]kdb> go # less test.c #include int i; void main() { int a=10; int b = 0; printf("%d", a/b); } # gcc test.c #./a.out 0xffffffff8100d110 divide_error: int3 Entering kdb (current=0xffff8100b918a000, pid 22706) on processor 1 due to Breakpoint @ 0xffffffff8100d110 [2] kdb> kdb

14 Task State Segment

15 Stack layouts K U K U

16 When an interrupt handler is executed, the processor automatically stores the value of some registers in the stack of the current task. 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

17 Nested execution of exception & interrupt handlers ISR1 ISR2 ISR3 ISR2 ISR1 scheduler task1 task2 k-ξ

18 Linux implementations Interrupt gate – all Linux interrupt handlers – DPL = 0 System gate – int3, into, bound, int $0x80 – DPL = 3 Trap gate – most Linux exception handlers – DPL = 0 sysenter, sysexit, syscall, sysret

19 The Linux structure krnl usr H/W sys. call interrupt softirq/ tasklet work- queue interrupt CTX process CTX exception

20 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.

21 Exception handling

22 Interrupt handling I/O interrupts Timer interrupts Inter-processor interrupts (IPI)

23 The Linux structure krnl usr H/W sys. call interrupt softirq/ tasklet work- queue interrupt CTX process CTX exception Top-halfTop-half

24 BSD user process bottom half of kernel deferrable functions 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.

25 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.

26 IRQ handler Drivers can register an interrupt handler and enable a given interrupt line for handling with the function request_irq()

27 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.

28 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

29 IRQ handler IRQF_SHARED—This flag specifies that the interrupt line can be shared among multiple interrupt handlers.

30 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.

31 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

32 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.

33 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.

34 The path that an interrupt takes

35 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.

36 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.

37 Global IRQ disabling? were deprecated during 2.5 because the performance issues.


Download ppt "Interrupts and Exceptions 國立中正大學 資訊工程研究所 羅習五 老師. Interrupt & Exceptions Interrupts – maskable & nonmaskable interrupt Exceptions – Processor-detected."

Similar presentations


Ads by Google