Beruflich Dokumente
Kultur Dokumente
so
2014/10/17 By @if1live
#linux #system programming #interrupt #exception #study
w d
. : ; d
.
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
: 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)
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
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
(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
;
interrupt handler return
=;
Linux b
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