日本免费全黄少妇一区二区三区-高清无码一区二区三区四区-欧美中文字幕日韩在线观看-国产福利诱惑在线网站-国产中文字幕一区在线-亚洲欧美精品日韩一区-久久国产精品国产精品国产-国产精久久久久久一区二区三区-欧美亚洲国产精品久久久久

FreeBSD 5 內(nèi)核源代碼分析之中斷處理

FreeBSD 5 內(nèi)核中斷處理的最大特點(diǎn)是將中斷處理程序在線程的上下文中運(yùn)行 。
為此,內(nèi)核為每個(gè)注冊的中斷源(即vector)準(zhǔn)備一個(gè)內(nèi)核線程,即中斷線程,
其任務(wù)就是等待中斷的發(fā)生,一旦發(fā)生,便運(yùn)行相應(yīng)的中斷處理程序 。

FreeBSD 5這樣做,有好處也有壞處 。好處是可以簡化線程和中斷的互斥關(guān)系,
并使得中斷處理可以阻塞 。
壞處是每次響應(yīng)中斷都要進(jìn)行線程調(diào)度,可能有兩次線程上下文的切換
(從用戶線程切到中斷線程再切回來) 。未來的想法是進(jìn)行l(wèi)azy scheduling,
即盡可能借用當(dāng)前線程的上下文,只有在中斷要阻塞時(shí)才進(jìn)行真正的調(diào)度 。

與中斷有關(guān)的源代碼主要在

sys/kern/kern_intr.c (與體系結(jié)構(gòu)無關(guān)的中斷代碼)
sys/i386/i386/intr_Machdep.c (與i386體系結(jié)構(gòu)相關(guān)的中斷代碼)
sys/i386/isa/atpic.c (與8259A相關(guān)的.c代碼)
sys/i386/isa/atpic_vector.s (與8259A相關(guān)的.s代碼)

Contents

1,登記IRQ中斷源
1.1 數(shù)據(jù)結(jié)構(gòu)與函數(shù)
1.2 8259A的登記過程
2,IRQ中斷的處理過程
3, 軟件中斷swi
3.1 軟件中斷的登記
3.2 軟件中斷的調(diào)度

-------------------------------
1,登記IRQ中斷源

1.1 數(shù)據(jù)結(jié)構(gòu)與函數(shù)

中斷向量表有多個(gè)vector,0-31為CPU用,32~32 15對應(yīng)IRQ0~IRQ15
一個(gè)vector對應(yīng)一個(gè)source,數(shù)據(jù)類型是struct intsrc
代碼:
/*
* An interrupt source. The upper-layer code uses the PIC methods to
* control a given source. The lower-layer PIC drivers can store additional
* private data in a given interrupt source such as an interrupt pin number
* or an I/O APIC pointer.
*/
struct intsrc {
struct pic *is_pic;
struct ithd *is_ithread;
u_long *is_count;
u_long *is_straycount;
u_int is_index;
};


其實(shí)在vector后面的是中斷控制器,如8259A,I/O APIC等,
事實(shí)上,對中斷源的控制實(shí)際上就是對中斷控制器的操作,
因此,在struct intsrc中有成員struct pic *is_pic,
即中斷控制器的操作函數(shù)表,通過這個(gè)表,可以為不同的中斷控制器
定義不同的操作,達(dá)到demultiplex的作用 。這里pic是
programmable interrupt controller的意思 。
代碼:
/*
* Methods that a PIC provides to mask/unmask a given interrupt source,
* "turn on" the interrupt on the CPU side by setting up an IDT entry, and
* return the vector associated with this source.
*/
struct pic {
void (*pic_enable_source)(struct intsrc *);
void (*pic_disable_source)(struct intsrc *);
void (*pic_eoi_source)(struct intsrc *);
void (*pic_enable_intr)(struct intsrc *);
int (*pic_vector)(struct intsrc *);
int (*pic_source_pending)(struct intsrc *);
void (*pic_suspend)(struct intsrc *);
void (*pic_resume)(struct intsrc *);
};

系統(tǒng)中所有的中斷源組成一個(gè)數(shù)組,由于當(dāng)采用I/O APIC作為中斷控制器時(shí),
可以有191個(gè)中斷號(hào)(IRQ),因此該數(shù)組大小定義為191 。
代碼:
static struct intsrc *interrupt_sources[NUM_IO_INTS];

/* With I/O APIC"s we can have up to 191 interrupts. */
#define NUM_IO_INTS 191

所謂登記中斷源,就是將實(shí)際的中斷控制器的對應(yīng)struct intsrc數(shù)據(jù)結(jié)構(gòu)
添加到該數(shù)組中去 。同時(shí),系統(tǒng)為每個(gè)登記的中斷源創(chuàng)建一個(gè)中斷線程,
中斷處理程序就在該線程的上下文中運(yùn)行,該線程的入口函數(shù)為ithread_loop(),

struct intsrc結(jié)構(gòu)成員is_ithread指向描述中斷線程的數(shù)據(jù)結(jié)構(gòu)struct ithd,
而struct ithd結(jié)構(gòu)成員it_td指向真正的線程結(jié)構(gòu)struct thread,從而將中斷
與系統(tǒng)的調(diào)度單元線程聯(lián)系起來 。

代碼:
/*
* Describe an interrupt thread. There is one of these per interrupt vector.
* Note that this actually describes an interrupt source. There may or may

推薦閱讀