First, exception initialization
Initialization of IDT for interrupt vector table
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) ; ......}
For exceptions from 0 to 19, set the exception handler, set_trap_gate as follows:
static void __init set_trap_gate (unsigned int n, void *addr) {_set_gate (IDT_TABLE+N,15,0,ADDR);}
A door descriptor is set in IDT, such as:
Selector for _kernel_cs. P is 1;DPL for 00;DT for 0;type for 15, Trap gate. Offset is the deviation of the exception handling function.
Second, abnormal response
exceptions typically occur in the user state, The only exception that can be triggered in the kernel state is the fault of the pages. Let's take the example of a missing pages exception.
1. Before executing the exception handler function
If the exception occurs in the user state, it will form as follows:
Figure exception handling and interrupt processing system stack contrast chart
(1), the CPU according to the specific interrupt vector (in this case, 14), from the interrupt vector table IDT found the corresponding table entry, and the table entry should be a trap door. First, the SS of the user-state stack, the esp,eflags of the user stack, and the cs,eip of the user space are deposited into the system stack (obtained from TSS). If the exception that occurs produces an error code, the error code is also pressed onto the stack. in interrupt processing, this position of the stack holds the interrupt request number.
(2), the CPU according to trap gate settings to reach the exception handling function.
ENTRY (Page_fault) PUSHL $ symbol_name (do_page_fault) jmp Error_code
Press the Do_page_fault onto the stack. In interrupt processing,this position of the stack is stored in ES. Then jump to Error_code.
ERROR_CODE:PUSHL%dspushl%eaxxorl%eax,%eaxpushl%ebppushl%edipushl%esipushl%edxdecl%eax//eax = -1PUSHL%ECXPUSHL%eb XCLDMOVL%es,%ecxmovl Orig_eax (%ESP),%esi//get error code stored in ESI movl es (%ESP),%edi//get exception handling function stored in EDI movl%eax, Orig_eax (%ESP) //will 1 be stored in the original location of the error code MOVL%ecx, es (%ESP) //ES stored in the original location of the exception function, so that the same as the interrupt MOVL%ESP,%EDXPUSHL%esi//the error code into the stack, As the parameter of the exception handling function PUSHL%edx//the stack pointer to the stack, as an exception handler parameter MOVL $ (__kernel_ds),%edxmovl%edx,%dsmovl%edx,%esget_current (%EBX) Call *%edi //Execute exception handler Addl $8,%ESP //Skip the two values just pressed into the stack as parameters in JMP ret_from_exception
After the Error_code, the exception handling system stack, Do_page_fault becomes es, the error code becomes-1. The stack for exception handling and interrupt processing is basically the same.
2. Execute exception handling function, Do_page_fault
asmlinkage void Do_page_fault (struct pt_regs *regs, unsigned long error_code)//parameters just pressed into the stack {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 Me Mory On-demand. The * ' Reference ' page table is INIT_MM.PGD. * * note! We must not take any locks for the this case. We May * is in an interrupt or a critical region, and should * only copy the information from the master page table, * not Hing more. */if (address >= task_size) goto vmalloc_fault;mm = Tsk->mm;info.si_code = segv_maperr;/* * If we ' re in an interrupt Or there is 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 "+ +" are there due to Some instructions (like * Pusha) doing post-decrement on the stack and that * doesn ' t show up until later. */if (address + < 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_V Erify_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 P Resent */if (!) ( Vma->vm_flags & (Vm_read | vm_exec))) goto Bad_area;} /* * If for any reason @ all we couldn ' t handle the fault, * Make sure we exit gracefully rather than endlessly redo * th E 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;} /* Do it hits the DOS screen memory VA from vm86 mode? */if (Regs->eflags & vm_mask) {unsigned long bit = (address-0xa0000) >> page_shift;if (bit < +) tsk-> Thread.screen_bitmap |= 1 << bit;} Up (&mm->mmap_sem); return; .......}
3. After executing the exception handling function
Error_code in the last 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 //If there is a soft interrupt, first handle the soft interrupt entry (RET_FROM_INTR) //This function we have, after the execution of the interrupt handler function has been said 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 kernel Source-code scenario analysis-Exceptions