Está en la página 1de 7

/usr/lib/libsora.

so

Interrupt and Exceptions


2014 2 w

2014/10/17 By @if1live
#linux #system programming #interrupt #exception #study
w d
. : ; d
.

Interrupt and Exceptions


Interrupt vs Exception

Interrupt

Asynchronous Interrupt
-> CPU
: keyboard, etc
CPU : processors interrupt pin8
CPU //

Exception

Synchronous Interrupt
CPU
ex: 08
Interrupt Signal

Interrupt Signal 8
CPU x interrupt handler jump
program context kernel stack (eip, cs registers)
PC(program counter)
Interrupt handling Process switching
Interrupt handler w
w
w
interrupt handling mode switching
w x /
Interrupt Handling

2(kernel Effectiveness)
Top half : . : '
Bottom half : defer,
/ : 8 / . <
I/O
;
Nested
(kernel synchronization)
critical region .
:

Interrupt/Exception Handling

H/W interrupt

PIC
CPU Interrupt PIN8
do_IRQ() (x )
x
x handle_IRQ_event() .
ret_from_intr()
x
IA32 Architecture: Registers
Segment registers :
cs : code segment register
ds : data segment register
ss : stack segemnt register
es, fs, gs : general segment register
Control registers
CR : 0~3, 4
Debugging registers
DR : 0~7, 8
EFLAGS : Maskable Interrupt
System registers
GDTR : global descriptor table register
IDTR : interrupt descriptor table register
LDTR : local descriptor table register
etc
IRQs and Interrupts

Hardware device controller


Interrupt request output8

: Interrupt controller
Interrupt Controller
IRQ line;
;=
signal; vector
Interrupt Controller I/O port vector
CPU
INTR PIN; => Interrupt

CPU
INTR
Interrupts

Maskable Interrupts

I/O
IRQ
INTR pin8
CPU status register eflags enable/disable d
Nonmaskable Interrupts

(ex:
hardware failure)
NMI pin8 CPU
NMI = NonMaskable Interrupts
Exceptions

Processor-detected exceptions

CPU

Fault
:

: fault
, ~
page-fault ()
protection faults ( )
etc
Traps

=
breakpoint
Aborts
:

w~
(parity error, )
etc
Programmed exceptions(software interrupts)

w 8
int, int3, etc..
; (int 0x80, sysenter)
Interrupt Handling : Hardware part(IA32)

IDT(Interrupt Descriptor Table) : 256 entry x


CPU
PIC Interrupt vector i n
i IDT entry b, interrupt handler entry point d
(user mode ) kernel mode mode switch
kernel stack
interrupt handler entry point
Types of Interrupt Descriptor

x86

Task gate :
Interrupt Gate : (segment
selector + offset)
Trap Gate : exception (segment
selector + offset)
Linux

Interrupt Gate
DPL field = 0 (user mode b )
x86 interrupt gate
interrupt handler
System Gate
DPL field = 3 (user mode b )
x86 trap gate
vector = {3|4|5|128}, (int3, into, bound int 0x80)
Trap Gate
DPL field = 0
x86 trap gate
exception handler(3, 4, 5, 128 f)
Hardware Handling of Interrupt

CPU f

vector i
idtr IDT i (
=)
gdtr
GDT n IDT
GDT .
d

. CPL(current privilege level) DPL(Descriptor
privilege level)
privilege level; d
ss, esp d
ss, esp
fault cs, eip (fault fault a
)

eflags, cs, eip
8 ;
cs, eip IDT i |
d. a

iret interrupt handler
cs, eip, eflags
CPL
ss, esp
b fp ds, es, fs, gs
Exception Handling

;) (
exception handling(c )
ret_from_exception()8
Interrupt Handling

Exception handling vs Interrupt handling

Exception handling
UNIX Signal;
f signal; ; . :
Interrupt handling
UNIX Signal
w w .
Interrupt Context

Interrupt handler , ,
interrupt handler
user-space b
scheduler
w 8
syscall: (b )
w ( w ; w )

I/O Interrupts

~
I/O interrupt
Timer Interrupt
Interprocessor interrupts
I/O Interrupts
' .
block (ex: IO disk operation)
w TASK_RUNNING; /p
Interrupt Classes

Critical : Maskable interrupts disable



Noncritical : Interrupt enabled
Noncritical deferrable
7
; w 8
softirq
tasklet
work queue
Interrupt Handler

(kernel mode =; )
IRQ ;) (
PIC ACK .
IRQ ISR;
ret_from_intr(); ~
Interrupt Vector

interrupt/exception: /

0~255 (8 bit)

vector
IRQ Data Structure (IRQ Descriptor)

irq_desc_t[NR_IRQs]
struct hw_interrupt_type
struct irqaction : ISR, linked list
Interrupt Handler d

do_IRQ() : ISR
handle_IRQ_event()
do_softirq()
handle_IRQ_event()
linked list ISR
Nested Executions

Why?
PIC
( 8 ; )

' interrupt model
Nested v
interrupt context switch
Interrupt context
interupt handler blocking
kernel stack
Returning from Interupts and Exceptions

Nested
=1
: /
process switch
: w ;.
TIF_NEED_RESCHED 1
w f
8


w : , .
a
ret_from_intr()
ret_from_exception()
Deferred invocation

interrupt handler process context


.
interrupt handler / . .
subtask(function call);

;
(Noncritical deferrable d)

interrupt handler
;
interrupt handler return
=;
Linux b

softirq : interrupt context


tasklet : interrupt context, softirq
worker queue : process context
softirq, tasklet, work queue user mode
Tasklet

Really Lightweight Task


Def : a kernel runtine/function with an argument, which can be scheduled to run
later in the interrupt context

1
, Nesting (strictly serialized)
tasklet: CPU
softirq

Software IRQ
Def : Used to run the schduled tasklet
2.6.11 6 x
HI_SOFTIRQ
TIMER_SOFTIRQ
NET_TX_SOFTIRQ
NET_RX_SOFTIRQ
SCSI_SOFTIRQ
TASKLET_SOFTIRQ
tasklet8 HI_SOFTIRQ, TASKLET_SOFTIRQ . dn8.
do_softirq()

10 ;
: x wakeup_softirqd() ksoftirqd .
MAX_SOFTIRQ_RESTART = 10, loop
ksoftirqd Kernel Thread

why?
: softirq ; user-mode . ;
;
; / w
softirq responsiveness vs / latency
sol 1 : do_softirq() softirq . < .
sol 2 : softirq . high load do_softirq() .
user mode :
ksoftirqd


softirq . softirq 8 ksoftirqd =
ksoftirqd ' . user mode w
kernel thread / w .
work queue

worker thread ;
process context .
sleep, block
work queue cpu work thread
worker thread : 8 , 8 sleep
f
event work-queue
worker thread .

Interrupt Control


critical region ?
interrupt handler a ; 8p

local_irq_disable()
local_irq_enable()
local_irq_save() : save + disable
local_irq_restore() : restore
disabl/enable 8 :
CPU d IRQ x
Linux Process Scheduling
2014 2 w C++ Python with statment
Hugo | @if1live

También podría gustarte