123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- #include <linux/mm.h>
- #include <linux/interrupt.h>
- #include <linux/extable.h>
- #include <linux/wait.h>
- #include <linux/uaccess.h>
- #include <arch/system.h>
- extern int find_fixup_code(struct pt_regs *);
- extern void die_if_kernel(const char *, struct pt_regs *, long);
- extern void show_registers(struct pt_regs *regs);
- #undef DEBUG
- #ifdef DEBUG
- #define D(x) x
- #else
- #define D(x)
- #endif
- #define DPG(x)
- DEFINE_PER_CPU(pgd_t *, current_pgd);
- unsigned long cris_signal_return_page;
- asmlinkage void
- do_page_fault(unsigned long address, struct pt_regs *regs,
- int protection, int writeaccess)
- {
- struct task_struct *tsk;
- struct mm_struct *mm;
- struct vm_area_struct * vma;
- siginfo_t info;
- int fault;
- unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
- D(printk(KERN_DEBUG
- "Page fault for %lX on %X at %lX, prot %d write %d\n",
- address, smp_processor_id(), instruction_pointer(regs),
- protection, writeaccess));
- tsk = current;
-
- if (address >= VMALLOC_START &&
- !protection &&
- !user_mode(regs))
- goto vmalloc_fault;
-
- if (cris_signal_return_page &&
- address == cris_signal_return_page &&
- !protection && user_mode(regs))
- goto vmalloc_fault;
-
- local_irq_enable();
- mm = tsk->mm;
- info.si_code = SEGV_MAPERR;
-
- if (faulthandler_disabled() || !mm)
- goto no_context;
- if (user_mode(regs))
- flags |= FAULT_FLAG_USER;
- retry:
- down_read(&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 (user_mode(regs)) {
-
- if (address + PAGE_SIZE < rdusp())
- goto bad_area;
- }
- if (expand_stack(vma, address))
- goto bad_area;
-
- good_area:
- info.si_code = SEGV_ACCERR;
-
- if (writeaccess == 2){
- if (!(vma->vm_flags & VM_EXEC))
- goto bad_area;
- } else if (writeaccess == 1) {
- if (!(vma->vm_flags & VM_WRITE))
- goto bad_area;
- flags |= FAULT_FLAG_WRITE;
- } else {
- if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
- goto bad_area;
- }
-
- fault = handle_mm_fault(vma, address, flags);
- if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
- return;
- if (unlikely(fault & VM_FAULT_ERROR)) {
- if (fault & VM_FAULT_OOM)
- goto out_of_memory;
- else if (fault & VM_FAULT_SIGSEGV)
- goto bad_area;
- else if (fault & VM_FAULT_SIGBUS)
- goto do_sigbus;
- BUG();
- }
- if (flags & FAULT_FLAG_ALLOW_RETRY) {
- if (fault & VM_FAULT_MAJOR)
- tsk->maj_flt++;
- else
- tsk->min_flt++;
- if (fault & VM_FAULT_RETRY) {
- flags &= ~FAULT_FLAG_ALLOW_RETRY;
- flags |= FAULT_FLAG_TRIED;
-
- goto retry;
- }
- }
- up_read(&mm->mmap_sem);
- return;
-
- bad_area:
- up_read(&mm->mmap_sem);
- bad_area_nosemaphore:
- DPG(show_registers(regs));
-
- if (user_mode(regs)) {
- #ifdef CONFIG_NO_SEGFAULT_TERMINATION
- DECLARE_WAIT_QUEUE_HEAD(wq);
- #endif
- printk(KERN_NOTICE "%s (pid %d) segfaults for page "
- "address %08lx at pc %08lx\n",
- tsk->comm, tsk->pid,
- address, instruction_pointer(regs));
-
- DPG(if (0))
- show_registers(regs);
- #ifdef CONFIG_NO_SEGFAULT_TERMINATION
- wait_event_interruptible(wq, 0 == 1);
- #else
- info.si_signo = SIGSEGV;
- info.si_errno = 0;
-
- info.si_addr = (void *)address;
- force_sig_info(SIGSEGV, &info, tsk);
- #endif
- return;
- }
- no_context:
-
- if (find_fixup_code(regs))
- return;
-
- if (!oops_in_progress) {
- oops_in_progress = 1;
- if ((unsigned long) (address) < PAGE_SIZE)
- printk(KERN_ALERT "Unable to handle kernel NULL "
- "pointer dereference");
- else
- printk(KERN_ALERT "Unable to handle kernel access"
- " at virtual address %08lx\n", address);
- die_if_kernel("Oops", regs, (writeaccess << 1) | protection);
- oops_in_progress = 0;
- }
- do_exit(SIGKILL);
-
- out_of_memory:
- up_read(&mm->mmap_sem);
- if (!user_mode(regs))
- goto no_context;
- pagefault_out_of_memory();
- return;
- do_sigbus:
- up_read(&mm->mmap_sem);
-
- info.si_signo = SIGBUS;
- info.si_errno = 0;
- info.si_code = BUS_ADRERR;
- info.si_addr = (void *)address;
- force_sig_info(SIGBUS, &info, tsk);
-
- if (!user_mode(regs))
- goto no_context;
- return;
- vmalloc_fault:
- {
-
- int offset = pgd_index(address);
- pgd_t *pgd, *pgd_k;
- pud_t *pud, *pud_k;
- pmd_t *pmd, *pmd_k;
- pte_t *pte_k;
- pgd = (pgd_t *)per_cpu(current_pgd, smp_processor_id()) + offset;
- pgd_k = init_mm.pgd + offset;
-
- pud = pud_offset(pgd, address);
- pud_k = pud_offset(pgd_k, address);
- if (!pud_present(*pud_k))
- goto no_context;
- pmd = pmd_offset(pud, address);
- pmd_k = pmd_offset(pud_k, address);
- if (!pmd_present(*pmd_k))
- goto bad_area_nosemaphore;
- set_pmd(pmd, *pmd_k);
-
- pte_k = pte_offset_kernel(pmd_k, address);
- if (!pte_present(*pte_k))
- goto no_context;
- return;
- }
- }
- int
- find_fixup_code(struct pt_regs *regs)
- {
- const struct exception_table_entry *fixup;
-
- unsigned long ip = (instruction_pointer(regs) & ~0x1);
- fixup = search_exception_tables(ip);
- if (fixup != 0) {
-
- instruction_pointer(regs) = fixup->fixup;
- arch_fixup(regs);
- return 1;
- }
- return 0;
- }
|