標籤:
一、異常初始化
中斷向量表的IDT的初始化
void __init trap_init(void){#ifdef CONFIG_EISAif (isa_readl(0x0FFFD9) == ‘E‘+(‘I‘<<8)+(‘S‘<<16)+(‘A‘<<24))EISA_bus = 1;#endifset_trap_gate(0,÷_error);set_trap_gate(1,&debug);......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,&spurious_interrupt_bug);set_trap_gate(16,&coprocessor_error);set_trap_gate(17,&alignment_check);set_trap_gate(18,&machine_check);set_trap_gate(19,&simd_coprocessor_error); ......}
對從0到19的異常,設定例外處理常式,set_trap_gate如下:
static void __init set_trap_gate(unsigned int n, void *addr){_set_gate(idt_table+n,15,0,addr);}
在IDT中設定了門描述符,如:
Selector為_KERNEL_CS。P為1;DPL為00;DT為0;TYPE為15,陷阱門。Offset就是異常處理函數的位移。
二、異常響應
異常一般發生在使用者態,在核心態能觸發的唯一異常就是缺頁異常。 我們以缺頁異常為例。
1、執行異常處理函數之前
如果異常發生在使用者態,則會形成如:
圖 異常處理和中斷處理系統堆棧對照圖
(1)、CPU根據具體的中斷向量(本例中為14),從中斷向量表IDT中找到相應的表項,而該表項應該是一個陷阱門。 首先把使用者態堆棧的SS,使用者堆棧的ESP,EFLAGS,使用者空間的CS,EIP存入到系統堆棧中(從TSS中擷取)。如果所發生的異常產生出錯代碼的話,就把這個出錯代碼也壓入堆棧。在中斷處理中,堆棧的這個位置存放的插斷要求號。
(2)、CPU根據陷阱門的設定到達了異常處理函數。
ENTRY(page_fault)pushl $ SYMBOL_NAME(do_page_fault)jmp error_code
把do_page_fault壓入堆棧中。在中斷處理中,堆棧的這個位置存放的ES。然後跳到error_code。
error_code:pushl %dspushl %eaxxorl %eax,%eaxpushl %ebppushl %edipushl %esipushl %edxdecl %eax//eax = -1pushl %ecxpushl %ebxcldmovl %es,%ecxmovl ORIG_EAX(%esp), %esi//擷取錯誤碼存放在esi中movl ES(%esp), %edi//獲得異常處理函數存放在edi中movl %eax, ORIG_EAX(%esp) //將-1存放在原來存放錯誤碼的位置movl %ecx, ES(%esp) //將es存放在原來存放異常函數的位置,這樣就和中斷一樣了movl %esp,%edxpushl %esi//把錯誤碼壓入堆棧,做為異常處理函數的參數pushl %edx//把堆棧的指標也壓入堆棧,做為異常處理函數的參數movl $(__KERNEL_DS),%edxmovl %edx,%dsmovl %edx,%esGET_CURRENT(%ebx)call *%edi //執行異常處理函數addl $8,%esp //跳過剛剛壓入堆棧做為參數的兩個值jmp ret_from_exception
經過了error_code,異常處理系統堆棧中,do_page_fault變成了es,出錯代碼變成了-1。異常處理和中斷處理的堆棧基本一樣。
2、執行異常處理函數,do_page_fault
asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long error_code)//剛剛壓入堆棧的參數{struct task_struct *tsk;struct mm_struct *mm;struct vm_area_struct * vma;unsigned long address;unsigned long page;unsigned long fixup;int write;siginfo_t info;/* get the address */__asm__("movl %%cr2,%0":"=r" (address));tsk = current;/* * We fault-in kernel-space virtual memory on-demand. The * ‘reference‘ page table is init_mm.pgd. * * NOTE! We MUST NOT take any locks for this case. We may * be in an interrupt or a critical region, and should * only copy the information from the master page table, * nothing more. */if (address >= TASK_SIZE)goto vmalloc_fault;mm = tsk->mm;info.si_code = SEGV_MAPERR;/* * If we‘re in an interrupt or have no user * context, we must not take the fault.. */if (in_interrupt() || !mm)goto no_context;down(&mm->mmap_sem);vma = find_vma(mm, address);if (!vma)goto bad_area;if (vma->vm_start <= address)goto good_area;if (!(vma->vm_flags & VM_GROWSDOWN))goto bad_area;if (error_code & 4) {/* * accessing the stack below %esp is always a bug. * The "+ 32" is there due to some instructions (like * pusha) doing post-decrement on the stack and that * doesn‘t show up until later.. */if (address + 32 < regs->esp)goto bad_area;}if (expand_stack(vma, address))goto bad_area;/* * Ok, we have a good vm_area for this memory access, so * we can handle it.. */good_area:info.si_code = SEGV_ACCERR;write = 0;switch (error_code & 3) {default:/* 3: write, present */#ifdef TEST_VERIFY_AREAif (regs->cs == KERNEL_CS)printk("WP fault at %08lx\n", regs->eip);#endif/* fall through */case 2:/* write, not present */if (!(vma->vm_flags & VM_WRITE))goto bad_area;write++;break;case 1:/* read, present */goto bad_area;case 0:/* read, not present */if (!(vma->vm_flags & (VM_READ | VM_EXEC)))goto bad_area;}/* * If for any reason at all we couldn‘t handle the fault, * make sure we exit gracefully rather than endlessly redo * the fault. */switch (handle_mm_fault(mm, vma, address, write)) {case 1:tsk->min_flt++;break;case 2:tsk->maj_flt++;break;case 0:goto do_sigbus;default:goto out_of_memory;}/* * Did it hit the DOS screen memory VA from vm86 mode? */if (regs->eflags & VM_MASK) {unsigned long bit = (address - 0xA0000) >> PAGE_SHIFT;if (bit < 32)tsk->thread.screen_bitmap |= 1 << bit;}up(&mm->mmap_sem);return; .......}
3、執行異常處理函數之後
error_code中最後jmp ret_from_exception。
ret_from_exception:#ifdef CONFIG_SMPGET_CURRENT(%ebx)movl processor(%ebx),%eaxshll $CONFIG_X86_L1_CACHE_SHIFT,%eaxmovl SYMBOL_NAME(irq_stat)(,%eax),%ecx# softirq_activetestl SYMBOL_NAME(irq_stat)+4(,%eax),%ecx# softirq_mask#elsemovl SYMBOL_NAME(irq_stat),%ecx# softirq_activetestl SYMBOL_NAME(irq_stat)+4,%ecx# softirq_mask#endifjne handle_softirq //如果有非強制中斷,先處理非強制中斷ENTRY(ret_from_intr) //這個函數我們在,執行中斷處理函數之後已經講過了GET_CURRENT(%ebx)movl EFLAGS(%esp),%eax# mix EFLAGS and CSmovb CS(%esp),%altestl $(VM_MASK | 3),%eax# return to VM86 mode or non-supervisor?jne ret_with_reschedulejmp restore_allALIGNhandle_softirq:call SYMBOL_NAME(do_softirq)jmp ret_from_intr
Linux核心原始碼情景分析-異常