2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
7 #include <linux/sched.h>
8 #include <linux/hardirq.h>
9 #include <asm/current.h>
10 #include <asm/pgtable.h>
11 #include <asm/tlbflush.h>
13 #include "as-layout.h"
14 #include "kern_util.h"
19 * Note this is constrained to return 0, -EFAULT, -EACCESS, -ENOMEM by
22 int handle_page_fault(unsigned long address, unsigned long ip,
23 int is_write, int is_user, int *code_out)
25 struct mm_struct *mm = current->mm;
26 struct vm_area_struct *vma;
33 *code_out = SEGV_MAPERR;
36 * If the fault was during atomic operation, don't take the fault, just
42 down_read(&mm->mmap_sem);
43 vma = find_vma(mm, address);
46 else if (vma->vm_start <= address)
48 else if (!(vma->vm_flags & VM_GROWSDOWN))
50 else if (is_user && !ARCH_IS_STACKGROW(address))
52 else if (expand_stack(vma, address))
56 *code_out = SEGV_ACCERR;
57 if (is_write && !(vma->vm_flags & VM_WRITE))
60 /* Don't require VM_READ|VM_EXEC for write faults! */
61 if (!is_write && !(vma->vm_flags & (VM_READ | VM_EXEC)))
67 fault = handle_mm_fault(mm, vma, address, is_write ? FAULT_FLAG_WRITE : 0);
68 if (unlikely(fault & VM_FAULT_ERROR)) {
69 if (fault & VM_FAULT_OOM) {
71 } else if (fault & VM_FAULT_SIGBUS) {
77 if (fault & VM_FAULT_MAJOR)
82 pgd = pgd_offset(mm, address);
83 pud = pud_offset(pgd, address);
84 pmd = pmd_offset(pud, address);
85 pte = pte_offset_kernel(pmd, address);
86 } while (!pte_present(*pte));
89 * The below warning was added in place of
90 * pte_mkyoung(); if (is_write) pte_mkdirty();
91 * If it's triggered, we'd see normally a hang here (a clean pte is
92 * marked read-only to emulate the dirty bit).
93 * However, the generic code can mark a PTE writable but clean on a
94 * concurrent read fault, triggering this harmlessly. So comment it out.
97 WARN_ON(!pte_young(*pte) || (is_write && !pte_dirty(*pte)));
99 flush_tlb_page(vma, address);
101 up_read(&mm->mmap_sem);
107 * We ran out of memory, call the OOM killer, and return the userspace
108 * (which will retry the fault, or kill us if we got oom-killed).
110 up_read(&mm->mmap_sem);
111 pagefault_out_of_memory();
115 static void show_segv_info(struct uml_pt_regs *regs)
117 struct task_struct *tsk = current;
118 struct faultinfo *fi = UPT_FAULTINFO(regs);
120 if (!unhandled_signal(tsk, SIGSEGV))
123 if (!printk_ratelimit())
126 printk("%s%s[%d]: segfault at %lx ip %p sp %p error %x",
127 task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
128 tsk->comm, task_pid_nr(tsk), FAULT_ADDRESS(*fi),
129 (void *)UPT_IP(regs), (void *)UPT_SP(regs),
132 print_vma_addr(KERN_CONT " in ", UPT_IP(regs));
133 printk(KERN_CONT "\n");
136 static void bad_segv(struct faultinfo fi, unsigned long ip)
140 si.si_signo = SIGSEGV;
141 si.si_code = SEGV_ACCERR;
142 si.si_addr = (void __user *) FAULT_ADDRESS(fi);
143 current->thread.arch.faultinfo = fi;
144 force_sig_info(SIGSEGV, &si, current);
147 void fatal_sigsegv(void)
149 force_sigsegv(SIGSEGV, current);
152 * This is to tell gcc that we're not returning - do_signal
153 * can, in general, return, but in this case, it's not, since
154 * we just got a fatal SIGSEGV queued.
159 void segv_handler(int sig, struct uml_pt_regs *regs)
161 struct faultinfo * fi = UPT_FAULTINFO(regs);
163 if (UPT_IS_USER(regs) && !SEGV_IS_FIXABLE(fi)) {
164 show_segv_info(regs);
165 bad_segv(*fi, UPT_IP(regs));
168 segv(*fi, UPT_IP(regs), UPT_IS_USER(regs), regs);
172 * We give a *copy* of the faultinfo in the regs to segv.
173 * This must be done, since nesting SEGVs could overwrite
174 * the info in the regs. A pointer to the info then would
177 unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user,
178 struct uml_pt_regs *regs)
183 int is_write = FAULT_WRITE(fi);
184 unsigned long address = FAULT_ADDRESS(fi);
186 if (!is_user && (address >= start_vm) && (address < end_vm)) {
187 flush_tlb_kernel_vm();
190 else if (current->mm == NULL) {
191 show_regs(container_of(regs, struct pt_regs, regs));
192 panic("Segfault with no mm");
195 if (SEGV_IS_FIXABLE(&fi) || SEGV_MAYBE_FIXABLE(&fi))
196 err = handle_page_fault(address, ip, is_write, is_user,
201 * A thread accessed NULL, we get a fault, but CR2 is invalid.
202 * This code is used in __do_copy_from_user() of TT mode.
203 * XXX tt mode is gone, so maybe this isn't needed any more
208 catcher = current->thread.fault_catcher;
211 else if (catcher != NULL) {
212 current->thread.fault_addr = (void *) address;
213 UML_LONGJMP(catcher, 1);
215 else if (current->thread.fault_addr != NULL)
216 panic("fault_addr set but no fault catcher");
217 else if (!is_user && arch_fixup(ip, regs))
221 show_regs(container_of(regs, struct pt_regs, regs));
222 panic("Kernel mode fault at addr 0x%lx, ip 0x%lx",
226 show_segv_info(regs);
228 if (err == -EACCES) {
229 si.si_signo = SIGBUS;
231 si.si_code = BUS_ADRERR;
232 si.si_addr = (void __user *)address;
233 current->thread.arch.faultinfo = fi;
234 force_sig_info(SIGBUS, &si, current);
236 BUG_ON(err != -EFAULT);
237 si.si_signo = SIGSEGV;
238 si.si_addr = (void __user *) address;
239 current->thread.arch.faultinfo = fi;
240 force_sig_info(SIGSEGV, &si, current);
245 void relay_signal(int sig, struct uml_pt_regs *regs)
247 if (!UPT_IS_USER(regs)) {
249 printk(KERN_ERR "Bus error - the host /dev/shm or /tmp "
250 "mount likely just ran out of space\n");
251 panic("Kernel mode signal %d", sig);
254 arch_examine_signal(sig, regs);
256 current->thread.arch.faultinfo = *UPT_FAULTINFO(regs);
257 force_sig(sig, current);
260 void bus_handler(int sig, struct uml_pt_regs *regs)
262 if (current->thread.fault_catcher != NULL)
263 UML_LONGJMP(current->thread.fault_catcher, 1);
264 else relay_signal(sig, regs);
267 void winch(int sig, struct uml_pt_regs *regs)
269 do_IRQ(WINCH_IRQ, regs);