Linux Kernel Interrupts, Exceptions and System Calls

Author: Harold Wang

http://blog.csdn.net/hero7935

Basic knowledge

In interrupt is usually defined as an event that alters the sequence of instructions executed by a processor, providing a way to divert the processor to code outside the normal flow of control.

Interrupt are often divided into synchronous and asynchronous interrupts:

--Synchronous interrupts(exceptions): produced by the CPU control unit while executing instructions and are called synchronous because the control unit issues them only after terminating the executing of an instruction
like… Processor-detected exceptions/Faults/Traps/Aborts/Programmed exceptions
Caused by…programming errors/anomalous conditions that must be handled by the kernel

--Asynchronous interrupts(interrupts): generated by other hardware devices at arbitrary times with respect to the CPU clock signals.
Caused by…timers/I/O devices/

--Interrupt vectorEach interrupt or exception is identified by a number ranging from 0 to 255 (8-bit unsigned number)
--The vectors of nonmaskable interrupts and exceptions are fixed
--The vectors of maskable interrupts can be altered by programming the Interrupt Controller

image

Relavent Instructions:
CALL procedure name
INT interrupt vector
IRET return
LIDT 48 bit interrupt description table

IDTR in CPU save the baseaddress of the IDT which is filled with gate descriptors(called interrupt vector N years ago)
----IDTR 48 bit , lower 16 bit indicate the size of the IDT, higher 32 bit indicate the line base address of the IDT.

image

interrupt gate and trap gate can not be accessed by user mode.
system gate (call gate) can be accessed by user mode. there are 4 linux exception program, their vector are 3,4,5,128. so user mode can publish int3,into,bound,int $0x80 asmble instructions. using set_system_gate() function to insert a system gate into the IDT.

Author: Harold Wang

http://blog.csdn.net/hero7935

image

Interrupt Handling

setup_idt()
--initialize IDT’s 256 items with ignore_int() function’s result. 
--initizlize items with meaningful trap and interrupt handlers, using start_kernel() function--->trap_init() and init_IRQ() functions.
image

image

image

image

image

NOTE: interrupt handler can not be blocked or go to sleep. So there are some limitations: interrupt handler MUST ASAP--->solutions: divide into two parts:
--Top half: simple and fast, dealing with time-critical hardware tasks. like: packets transmission and receiving
--Bottom half: deferring work to a later point where interrupt can be enabled. like: network protocols processing

Author: Harold Wang

http://blog.csdn.net/hero7935

Bottom Half

mechnisam of realization:
--softirq
--tasklet
--work queue

image

Bottom Half to Use?

NOTE: if needs to sleep, use work queue.
if doesn’t need to sleep, use tasklet.

Interrupt Applications

Timer application:

struct timer_list my_timer;
init_timer(&my_timer);
add_timer(&my_timer);
del_timer(&my_timer);
image

System call application:

kernel initialize: trap_init()—>set_system_gate(0x80,$system_call); more information can see arch/i386/kernel/traps.c

NOTE: eax reg is used for save syscall number! eax,ebx,ecx,edx,esi,edi save paramters.  by calling call* sys_call_table(%eax,4); return value saved in eax
see include/asm/unistd_32.h
kernel/syscall_table.S

Author: Harold Wang

http://blog.csdn.net/hero7935

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值