linux-0.11之Kernel-traps.c

在linux內核的硬件中斷處理程序中,主要代碼集中在asm.s和traps.c中。asm.s用於實現大部分硬件異常所引起的中斷的彙編語言處理過程。而traps.c程序則實現了asm.s的中斷處理過程中調用的C函數。

一般的硬件異常所引起的中斷處理流程如下圖:

  

traps.c中初始化函數trap_init()是在main.c中被調用的,用於初始化硬件異常處理中斷向量(陷阱門),並設置允許中斷信號的到來。

traps.c程序主要包括一些在處理異常故障(硬件中斷)底層代碼asm.s文件中調用的相應C函數。用於顯示出錯位置和出錯調試信息。其中die()函數就是用於在中斷處理中顯示出錯信息。目前主要用於調試目的,以後將擴展用來殺死遭損壞的進程。主要源代碼如下注釋:

*
 *  linux/kernel/traps.c
 *
 *  (C) 1991  Linus Torvalds
 */


/*
 * 'Traps.c' handles hardware traps and faults after we have saved some
 * state in 'asm.s'. Currently mostly a debugging-aid, will be extended
 * to mainly kill the offending process (probably by giving it a signal,
 * but possibly by killing it outright if necessary).
 */
 /*在程序asm.s中保存了一些狀態後,本程序用來處理硬件陷阱和故障。主要用於調試*/
 
#include <string.h> /*字符串頭文件*/
#include <linux/head.h> /*定義了段描述符的簡單結構,和選擇符常量*/
#include <linux/sched.h> /*調度程序頭文件*/
#include <linux/kernel.h> /*內核頭文件*/
#include <asm/system.h> /*系統頭文件*/
#include <asm/segment.h> /*段操作頭文件*/
#include <asm/io.h> /*輸入、輸出頭文件*/


#define get_seg_byte(seg,addr) ({ \ /*宏定義:取段seg中地址addr處的一個字節*/
register char __res; \ /*定義一個寄存器變量*/
__asm__("push %%fs;mov %%ax,%%fs;movb %%fs:%2,%%al;pop %%fs" \
:"=a" (__res):"0" (seg),"m" (*(addr))); \ /*輸入:_res;輸出:seg 內存地址*/
__res;})


#define get_seg_long(seg,addr) ({ \ /*宏定義:取段seg中地址addr處的4個字節(long)*/
register unsigned long __res; \ /*定義一個寄存器變量*/
__asm__("push %%fs;mov %%ax,%%fs;movl %%fs:%2,%%eax;pop %%fs" \
:"=a" (__res):"0" (seg),"m" (*(addr))); \ /*輸入:_res;輸出:seg 內存地址*/
__res;})


#define _fs() ({ \ /*宏定義:取fs段寄存器的值*/
register unsigned short __res; \ /*定義一個寄存器變量*/
__asm__("mov %%fs,%%ax":"=a" (__res):); \ /*輸入:_res*/
__res;})


/*函數聲明*/
int do_exit(long code); /*程序退出處理函數*/


void page_exception(void); /*頁異常,實際是page_fault*/
/*中斷處理程序聲明*/
void divide_error(void); /*int0 中斷處理*/
void debug(void); /*int1 中斷處理*/
void nmi(void); /*int2 中斷處理*/
void int3(void); /*int3 中斷處理*/
void overflow(void); /*int4 中斷處理*/
void bounds(void); /*int5 中斷處理*/
void invalid_op(void); /*int6 中斷處理*/
void device_not_available(void); /*int7 中斷處理*/
void double_fault(void); /*int8 中斷處理*/
void coprocessor_segment_overrun(void); /*int9 中斷處理*/
void invalid_TSS(void); /*int10 中斷處理*/
void segment_not_present(void); /*int11 中斷處理*/
void stack_segment(void); /*int12 中斷處理*/
void general_protection(void); /*int13 中斷處理*/
void page_fault(void); /*int14 中斷處理*/
void coprocessor_error(void); /*int16 中斷處理*/
void reserved(void); /*int15 中斷處理*/
void parallel_interrupt(void); /*int39 中斷處理*/
void irq13(void); /*int45 中斷協助處理*/
/*該子程序用來打印出錯中斷的名稱、出錯號...*/
static void die(char * str,long esp_ptr,long nr)
{
long * esp = (long *) esp_ptr;
int i;


printk("%s: %04x\n\r",str,nr&0xffff); /*打印中斷和出錯號*/
printk("EIP:\t%04x:%p\nEFLAGS:\t%p\nESP:\t%04x:%p\n",
esp[1],esp[0],esp[2],esp[4],esp[3]); /*打印段選擇符、eip、eflegs、esp*/
printk("fs: %04x\n",_fs()); /*打印段寄存器的值*/
printk("base: %p, limit: %p\n",get_base(current->ldt[1]),get_limit(0x17));/*打印段寄存器的基址和長度*/
if (esp[4] == 0x17) { /*打印棧數據*/
printk("Stack: ");
for (i=0;i<4;i++)
printk("%p ",get_seg_long(0x17,i+(long *)esp[3]));
printk("\n");
}
str(i); /*獲取當前任務號*/
printk("Pid: %d, process nr: %d\n\r",current->pid,0xffff & i); /*打印進程pid*/
for(i=0;i<10;i++) /*打印10個指令碼*/
printk("%02x ",0xff & get_seg_byte(esp[1],(i+(char *)esp[0])));
printk("\n\r");
do_exit(11); /* play segment exception */
}
/*中斷處理調用函數*/
void do_double_fault(long esp, long error_code)
{
die("double fault",esp,error_code);
}


void do_general_protection(long esp, long error_code)
{
die("general protection",esp,error_code);
}


void do_divide_error(long esp, long error_code)
{
die("divide error",esp,error_code);
}


void do_int3(long * esp, long error_code,
long fs,long es,long ds,
long ebp,long esi,long edi,
long edx,long ecx,long ebx,long eax)
{
int tr;


__asm__("str %%ax":"=a" (tr):"0" (0));
printk("eax\t\tebx\t\tecx\t\tedx\n\r%8x\t%8x\t%8x\t%8x\n\r",
eax,ebx,ecx,edx);
printk("esi\t\tedi\t\tebp\t\tesp\n\r%8x\t%8x\t%8x\t%8x\n\r",
esi,edi,ebp,(long) esp);
printk("\n\rds\tes\tfs\ttr\n\r%4x\t%4x\t%4x\t%4x\n\r",
ds,es,fs,tr);
printk("EIP: %8x   CS: %4x  EFLAGS: %8x\n\r",esp[0],esp[1],esp[2]);
}


void do_nmi(long esp, long error_code)
{
die("nmi",esp,error_code);
}


void do_debug(long esp, long error_code)
{
die("debug",esp,error_code);
}


void do_overflow(long esp, long error_code)
{
die("overflow",esp,error_code);
}


void do_bounds(long esp, long error_code)
{
die("bounds",esp,error_code);
}


void do_invalid_op(long esp, long error_code)
{
die("invalid operand",esp,error_code);
}


void do_device_not_available(long esp, long error_code)
{
die("device not available",esp,error_code);
}


void do_coprocessor_segment_overrun(long esp, long error_code)
{
die("coprocessor segment overrun",esp,error_code);
}


void do_invalid_TSS(long esp,long error_code)
{
die("invalid TSS",esp,error_code);
}


void do_segment_not_present(long esp,long error_code)
{
die("segment not present",esp,error_code);
}


void do_stack_segment(long esp,long error_code)
{
die("stack segment",esp,error_code);
}


void do_coprocessor_error(long esp, long error_code)
{
if (last_task_used_math != current)
return;
die("coprocessor error",esp,error_code);
}


void do_reserved(long esp, long error_code)
{
die("reserved (15,17-47) error",esp,error_code);
}


/*中斷程序初始化子程序,設置中斷調用門,set_trap_gate()和set_system_gate()都使用了中斷描述
*符表IDT中的陷阱門(Trap Gate),前者設置的特權級爲0,後者爲3。兩者都是嵌入彙編宏程序。
*/
void trap_init(void)
{
int i;
/*設置各個中斷向量值,除操作出錯*/
set_trap_gate(0,&divide_error);
set_trap_gate(1,&debug);
set_trap_gate(2,&nmi);
/*中斷3-5可以被所有程序執行*/
set_system_gate(3,&int3);
set_system_gate(4,&overflow);
set_system_gate(5,&bounds);
set_trap_gate(6,&invalid_op);
set_trap_gate(7,&device_not_available);
set_trap_gate(8,&double_fault);
set_trap_gate(9,&coprocessor_segment_overrun);
set_trap_gate(10,&invalid_TSS);
set_trap_gate(11,&segment_not_present);
set_trap_gate(12,&stack_segment);
set_trap_gate(13,&general_protection);
set_trap_gate(14,&page_fault);
set_trap_gate(15,&reserved);
set_trap_gate(16,&coprocessor_error);
/*把int17-47的中斷先設置爲reserverd,後面初始化時會重新設置自己的中斷*/
for (i=17;i<48;i++)
set_trap_gate(i,&reserved);
/*設置協處理器中斷45,允許產生中斷請求,設置並行口中斷描述符*/
set_trap_gate(45,&irq13);
outb_p(inb_p(0x21)&0xfb,0x21); /*允許8259A主芯片的IRQ2中斷請求*/
outb(inb_p(0xA1)&0xdf,0xA1); /*允許8259A主芯片的IRQ3中斷請求*/
set_trap_gate(39,&parallel_interrupt); /*設置並行口1的中斷0x27陷阱門描述符*/
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章