fault.c
来自「自己根据lkd和情境分析」· C语言 代码 · 共 414 行
C
414 行
/* * linux/arch/i386/mm/fault.c * * Copyright (C) 1995 Linus Torvalds */#include <linux/signal.h>#include <linux/sched.h>#include <linux/kernel.h>#include <linux/errno.h>#include <linux/string.h>#include <linux/types.h>#include <linux/ptrace.h>#include <linux/mman.h>#include <linux/mm.h>#include <linux/smp.h>#include <linux/smp_lock.h>#include <linux/interrupt.h>#include <linux/init.h>#include <linux/tty.h>#include <linux/vt_kern.h> /* For unblank_screen() */#include <asm/system.h>#include <asm/uaccess.h>#include <asm/pgalloc.h>#include <asm/hardirq.h>extern void die(const char *,struct pt_regs *,long);/* * Ugly, ugly, but the goto's result in better assembly.. */int __verify_write(const void * addr, unsigned long size){ struct vm_area_struct * vma; unsigned long start = (unsigned long) addr; if (!size) return 1; vma = find_vma(current->mm, start); if (!vma) goto bad_area; if (vma->vm_start > start) goto check_stack;good_area: if (!(vma->vm_flags & VM_WRITE)) goto bad_area; size--; size += start & ~PAGE_MASK; size >>= PAGE_SHIFT; start &= PAGE_MASK; for (;;) { survive: { int fault = handle_mm_fault(current->mm, vma, start, 1); if (!fault) goto bad_area; if (fault < 0) goto out_of_memory; } if (!size) break; size--; start += PAGE_SIZE; if (start < vma->vm_end) continue; vma = vma->vm_next; if (!vma || vma->vm_start != start) goto bad_area; if (!(vma->vm_flags & VM_WRITE)) goto bad_area;; } return 1;check_stack: if (!(vma->vm_flags & VM_GROWSDOWN)) goto bad_area; if (expand_stack(vma, start) == 0) goto good_area;bad_area: return 0;out_of_memory: if (current->pid == 1) { current->policy |= SCHED_YIELD; schedule(); goto survive; } goto bad_area;}extern spinlock_t timerlist_lock;/* * Unlock any spinlocks which will prevent us from getting the * message out (timerlist_lock is acquired through the * console unblank code) */void bust_spinlocks(int yes){ spin_lock_init(&timerlist_lock); if (yes) { oops_in_progress = 1;#ifdef CONFIG_SMP global_irq_lock = 0; /* Many serial drivers do __global_cli() */#endif } else { int loglevel_save = console_loglevel;#ifdef CONFIG_VT unblank_screen();#endif oops_in_progress = 0; /* * OK, the message is on the console. Now we call printk() * without oops_in_progress set so that printk will give klogd * a poke. Hold onto your hats... */ console_loglevel = 15; /* NMI oopser may have shut the console up */ printk(" "); console_loglevel = loglevel_save; }}void do_BUG(const char *file, int line){ bust_spinlocks(1); printk("kernel BUG at %s:%d!\n", file, line);}asmlinkage void do_invalid_op(struct pt_regs *, unsigned long);extern unsigned long idt;/* * This routine handles page faults. It determines the address, * and the problem, and then passes it off to one of the appropriate * routines. * * error_code: * bit 0 == 0 means no page found, 1 means protection fault * bit 1 == 0 means read, 1 means write * bit 2 == 0 means kernel, 1 means user-mode */ /*regs指向例外发生前夕CPU中各寄存器内容的一份副本这是由内核的中断响应机制保存下来的现场,error_code指明映射失败的具体原因*/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 CPU将导致映射失败的线性地址放在控制寄存器CR2中*/ __asm__("movl %%cr2,%0":"=r" (address)); /* It's safe to allow irq's after cr2 has been saved */ if (regs->eflags & X86_EFLAGS_IF) local_irq_enable(); 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. * * This verifies that the fault happens in kernel space * (error_code & 4) == 0, and that the fault was not a * protection error (error_code & 1) == 0. */ if (address >= TASK_SIZE && !(error_code & 5)) 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) /*若映射发生在某个中断服务程序则与当前进程毫无关系,或者当前进程的mm指针为0,也就说明该进程的映射尚未建立当然也与当前进程么的关系*/ goto no_context; down_read(&mm->mmap_sem); /*若find_vma找不到区间,那本次异常就必定是因越界访问引起,内核对用户空间的使用,堆栈在用户区的顶部,从上向下伸展,而进程的代码和数据都是自底向上分配空间,如果没有一个区间的结束地址高于给定的地址,那就说明这个地址是在堆栈之上,就是3G字节以上,要从用户空间访问属于系统的空间那就是越界了*/ vma = find_vma(mm, address); if (!vma) goto bad_area; if (vma->vm_start <= address) goto good_area; /*除去以上2种情况,剩下的就是给定地址正落在两个区间当中的空洞里,也就是该地址所在页面的映射尚未建立或已经撤销*/ if (!(vma->vm_flags & VM_GROWSDOWN)) /*在堆栈区下面有一个大空洞,它代表着动态分配(通过系统调用brk)而仍未分配除出去的空间,如果find_vma找到的区间是堆栈区间,那么它的vm_flags中应该有个标志位VM_GROWSDOWN,若VM_GROWSDOWN为0则说明这个空洞上方的区间并非堆栈区,说明这个空洞是因为一个映射区间被撤销而留下或者在建立映射时跳过了一块地址*/ 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_AREA if (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; } survive: /* * 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_read(&mm->mmap_sem); return;/* * Something tried to access memory that isn't in our memory map.. * Fix it, but check if it's kernel or user first.. */bad_area: up_read(&mm->mmap_sem); /* User mode accesses just cause a SIGSEGV */ if (error_code & 4) { tsk->thread.cr2 = address; tsk->thread.error_code = error_code; tsk->thread.trap_no = 14; info.si_signo = SIGSEGV; info.si_errno = 0; /* info.si_code has been set above */ info.si_addr = (void *)address; force_sig_info(SIGSEGV, &info, tsk); return; } /* * Pentium F0 0F C7 C8 bug workaround. */ if (boot_cpu_data.f00f_bug) { unsigned long nr; nr = (address - idt) >> 3; if (nr == 6) { do_invalid_op(regs, 0); return; } }no_context: /* Are we prepared to handle this kernel fault? */ if ((fixup = search_exception_table(regs->eip)) != 0) { regs->eip = fixup; return; }/* * Oops. The kernel tried to access some bad page. We'll have to * terminate things with extreme prejudice. */ bust_spinlocks(1); if (address < PAGE_SIZE) printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference"); else printk(KERN_ALERT "Unable to handle kernel paging request"); printk(" at virtual address %08lx\n",address); printk(" printing eip:\n"); printk("%08lx\n", regs->eip); asm("movl %%cr3,%0":"=r" (page)); page = ((unsigned long *) __va(page))[address >> 22]; printk(KERN_ALERT "*pde = %08lx\n", page); if (page & 1) { page &= PAGE_MASK; address &= 0x003ff000; page = ((unsigned long *) __va(page))[address >> PAGE_SHIFT]; printk(KERN_ALERT "*pte = %08lx\n", page); } die("Oops", regs, error_code); bust_spinlocks(0); do_exit(SIGKILL);/* * We ran out of memory, or some other thing happened to us that made * us unable to handle the page fault gracefully. */out_of_memory: up_read(&mm->mmap_sem); if (tsk->pid == 1) { tsk->policy |= SCHED_YIELD; schedule(); down_read(&mm->mmap_sem); goto survive; } printk("VM: killing process %s\n", tsk->comm); if (error_code & 4) do_exit(SIGKILL); goto no_context;do_sigbus: /*碰上坏指针而发生了页面异常,用户空间的地址访问失败*/ up_read(&mm->mmap_sem); /* * Send a sigbus, regardless of whether we were in kernel * or user mode. */ tsk->thread.cr2 = address; tsk->thread.error_code = error_code; tsk->thread.trap_no = 14; info.si_signo = SIGBUS; info.si_errno = 0; info.si_code = BUS_ADRERR; info.si_addr = (void *)address; force_sig_info(SIGBUS, &info, tsk); /* Kernel mode? Handle exceptions or die */ if (!(error_code & 4)) /*CPU的执行地址在系统空间?*/ goto no_context; return;vmalloc_fault: { /* * Synchronize this task's top level page-table * with the 'reference' page table. * * Do _not_ use "tsk" here. We might be inside * an interrupt in the middle of a task switch.. 若访问的内容所对应的目录项不在内存中,则跳转到no_context */ int offset = __pgd_offset(address); pgd_t *pgd, *pgd_k; pmd_t *pmd, *pmd_k; pte_t *pte_k; asm("movl %%cr3,%0":"=r" (pgd)); pgd = offset + (pgd_t *)__va(pgd); pgd_k = init_mm.pgd + offset; if (!pgd_present(*pgd_k)) goto no_context; set_pgd(pgd, *pgd_k); pmd = pmd_offset(pgd, address); pmd_k = pmd_offset(pgd_k, address); if (!pmd_present(*pmd_k)) goto no_context; set_pmd(pmd, *pmd_k); pte_k = pte_offset(pmd_k, address); if (!pte_present(*pte_k)) goto no_context; return; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?