博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
异常 中断 实现
阅读量:7164 次
发布时间:2019-06-29

本文共 9897 字,大约阅读时间需要 32 分钟。

 

这里主要说说异常(cpu查出来的) 中断(狭义的指硬盘 键盘...)

 

1.异常的发生

2.异常的处理过程

3.异常的设置

===============================

1.异常发生

异常通常是cpu侦测出来的

还有疑问,用户可不可以用就不知?int n 可以么?

 

 

==================================

2.异常的处理 //先看异常的设置最下边

//set_intr_gate(n,addr)   在8295a设置对应的中断向量号 0x20-0x2f

//set_trap_gate(n,addr)

//set_system_gate(n,addr)

//lin/kernel/asm.s 分成有错误码和无错误码 两种,linux中做了统一对于没有错误码的也压入一个0x0000

/* *  linux/kernel/asm.s * *  (C) 1991  Linus Torvalds *//* * asm.s contains the low-level code for most hardware faults. * page_exception is handled by the mm, so that isn't here. This * file also handles (hopefully) fpu-exceptions due to TS-bit, as * the fpu must be properly saved/resored. This hasn't been tested. */.globl _divide_error,_debug,_nmi,_int3,_overflow,_bounds,_invalid_op.globl _double_fault,_coprocessor_segment_overrun.globl _invalid_TSS,_segment_not_present,_stack_segment.globl _general_protection,_coprocessor_error,_irq13,_reserved_divide_error:        pushl $_do_divide_errorno_error_code:        xchgl %eax,(%esp)        pushl %ebx        pushl %ecx        pushl %edx        pushl %edi        pushl %esi        pushl %ebp        push %ds        push %es        push %fs        pushl $0                # "error code"        lea 44(%esp),%edx        pushl %edx        movl $0x10,%edx        mov %dx,%ds        mov %dx,%es        mov %dx,%fs        call *%eax        addl $8,%esp        pop %fs        pop %es        pop %ds        popl %ebp        popl %esi        popl %edi        popl %edx        popl %ecx        popl %ebx        popl %eax        iret_debug:        pushl $_do_int3         # _do_debug        jmp no_error_code_nmi:        pushl $_do_nmi        jmp no_error_code_int3:        pushl $_do_int3        jmp no_error_code_overflow:        pushl $_do_overflow        jmp no_error_code_bounds:        pushl $_do_bounds        jmp no_error_code_invalid_op:        pushl $_do_invalid_op        jmp no_error_code_coprocessor_segment_overrun:        pushl $_do_coprocessor_segment_overrun        jmp no_error_code_reserved:        pushl $_do_reserved        jmp no_error_code_irq13:        pushl %eax        xorb %al,%al        outb %al,$0xF0        movb $0x20,%al        outb %al,$0x20        jmp 1f1:      jmp 1f1:      outb %al,$0xA0        popl %eax        jmp _coprocessor_error_double_fault:        pushl $_do_double_faulterror_code:        xchgl %eax,4(%esp)              # error code <-> %eax        xchgl %ebx,(%esp)               # &function <-> %ebx        pushl %ecx        pushl %edx        pushl %edi        pushl %esi        pushl %ebp        push %ds        push %es        push %fs        pushl %eax                      # error code        lea 44(%esp),%eax               # offset        pushl %eax        movl $0x10,%eax        mov %ax,%ds        mov %ax,%es        mov %ax,%fs        call *%ebx        addl $8,%esp        pop %fs        pop %es        pop %ds        popl %ebp        popl %esi        popl %edi        popl %edx        popl %ecx        popl %ebx        popl %eax        iret_invalid_TSS:        pushl $_do_invalid_TSS        jmp error_code_segment_not_present:        pushl $_do_segment_not_present        jmp error_code_stack_segment:        pushl $_do_stack_segment        jmp error_code_general_protection:        pushl $_do_general_protection        jmp error_code

 

//lin/kernel/trap.c 这里主要是异常 ,硬中断是硬件也分布在各个文件中

/* *  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). */#include 
#include
#include
#include
#include
#include
#include
#define get_seg_byte(seg,addr) ({ \register char __res; \__asm__("push %%fs;mov %%ax,%%fs;movb %%fs:%2,%%al;pop %%fs" \ :"=a" (__res):"" (seg),"m" (*(addr))); \__res;})#define get_seg_long(seg,addr) ({ \register unsigned long __res; \__asm__("push %%fs;mov %%ax,%%fs;movl %%fs:%2,%%eax;pop %%fs" \ :"=a" (__res):"" (seg),"m" (*(addr))); \__res;})#define _fs() ({ \register unsigned short __res; \__asm__("mov %%fs,%%ax":"=a" (__res):); \__res;})int do_exit(long code);void page_exception(void);void divide_error(void);void debug(void);void nmi(void);void int3(void);void overflow(void);void bounds(void);void invalid_op(void);void device_not_available(void);void double_fault(void);void coprocessor_segment_overrun(void);void invalid_TSS(void);void segment_not_present(void);void stack_segment(void);void general_protection(void);void page_fault(void);void coprocessor_error(void);void reserved(void);void parallel_interrupt(void);void irq13(void);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]); 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); for(i=0;i<10;i++) 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)); 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);}void trap_init(void){ int i; set_trap_gate(0,÷_error); set_trap_gate(1,&debug); set_trap_gate(2,&nmi); set_system_gate(3,&int3); /* int3-5 can be called from all */ 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); for (i=17;i<48;i++) set_trap_gate(i,&reserved); set_trap_gate(45,&irq13); outb_p(inb_p(0x21)&0xfb,0x21); outb(inb_p(0xA1)&0xdf,0xA1); set_trap_gate(39,&parallel_interrupt);}

 

2.异常的处理

当cpu侦测到异常后,会发出对应的中断号 ,这里已经设置好了

找到对应的处理过程处理异常

=============================

3.异常的设置

//这里可以看到每个异常对应一个编号,这个是intel,建议使用的或者说指定的

//再看第2处异常的处理   这里以overflow()为例来说

-------------------------------------------

在traps.c中有 2个很像

void overflow(void);   

  //这个声明实际   实现在asm.s中因为是汇编所以以_overflow:实现,其实还是调用了了do_overflow()函数

    _overflow:

    pushl $_do_overflow  //_overflow:函数在这里 ,压入的是_do_overflow
    jmp no_error_code  //通用过程的提炼部分

void do_overflow(long esp, long error_code)

  {die("overflow",esp,error_code);}  //overflow这个异常的实际处理过程因为最底层需要参数,所以交上层overfow()处理

overflow()函数实现在上边已经有说明

-------------------------------------------------------------

181 void trap_init(void)182 {183         int i;184 185         set_trap_gate(0,÷_error);186         set_trap_gate(1,&debug);187         set_trap_gate(2,&nmi);188         set_system_gate(3,&int3);       /* int3-5 can be called from all */189         set_system_gate(4,&overflow);190         set_system_gate(5,&bounds);191         set_trap_gate(6,&invalid_op);192         set_trap_gate(7,&device_not_available);193         set_trap_gate(8,&double_fault);194         set_trap_gate(9,&coprocessor_segment_overrun);195         set_trap_gate(10,&invalid_TSS);196         set_trap_gate(11,&segment_not_present);197         set_trap_gate(12,&stack_segment);198         set_trap_gate(13,&general_protection);199         set_trap_gate(14,&page_fault);200         set_trap_gate(15,&reserved);201         set_trap_gate(16,&coprocessor_error);202         for (i=17;i<48;i++)203                 set_trap_gate(i,&reserved);204         set_trap_gate(45,&irq13);205         outb_p(inb_p(0x21)&0xfb,0x21);206         outb(inb_p(0xA1)&0xdf,0xA1);207         set_trap_gate(39,&parallel_interrupt);208 }

 ===============================================

 

 

转载于:https://www.cnblogs.com/caesarxu/p/3261234.html

你可能感兴趣的文章