2 * Copyright (C) 2000, 2001 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
6 #include "linux/kernel.h"
8 #include "linux/sched.h"
10 #include "linux/spinlock.h"
11 #include "linux/config.h"
12 #include "linux/init.h"
13 #include "linux/ptrace.h"
14 #include "asm/semaphore.h"
15 #include "asm/pgtable.h"
16 #include "asm/pgalloc.h"
17 #include "asm/tlbflush.h"
18 #include "asm/a.out.h"
19 #include "asm/current.h"
21 #include "user_util.h"
22 #include "kern_util.h"
24 #include "chan_kern.h"
25 #include "mconsole_kern.h"
26 #include "2_5compat.h"
30 int handle_page_fault(unsigned long address, unsigned long ip,
31 int is_write, int is_user, int *code_out)
33 struct mm_struct *mm = current->mm;
34 struct vm_area_struct *vma;
42 *code_out = SEGV_MAPERR;
43 down_read(&mm->mmap_sem);
44 vma = find_vma(mm, address);
47 else if(vma->vm_start <= address)
49 else if(!(vma->vm_flags & VM_GROWSDOWN))
51 else if(is_user && !ARCH_IS_STACKGROW(address))
53 else if(expand_stack(vma, address))
57 *code_out = SEGV_ACCERR;
58 if(is_write && !(vma->vm_flags & VM_WRITE))
61 if(!(vma->vm_flags & (VM_READ | VM_EXEC)))
64 page = address & PAGE_MASK;
67 switch (handle_mm_fault(mm, vma, address, is_write)){
83 pgd = pgd_offset(mm, page);
84 pud = pud_offset(pgd, page);
85 pmd = pmd_offset(pud, page);
86 pte = pte_offset_kernel(pmd, page);
87 } while(!pte_present(*pte));
89 *pte = pte_mkyoung(*pte);
90 if(pte_write(*pte)) *pte = pte_mkdirty(*pte);
91 flush_tlb_page(vma, page);
93 up_read(&mm->mmap_sem);
97 * We ran out of memory, or some other thing happened to us that made
98 * us unable to handle the page fault gracefully.
101 if (current->pid == 1) {
102 up_read(&mm->mmap_sem);
104 down_read(&mm->mmap_sem);
111 * We give a *copy* of the faultinfo in the regs to segv.
112 * This must be done, since nesting SEGVs could overwrite
113 * the info in the regs. A pointer to the info then would
116 unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user, void *sc)
121 int is_write = FAULT_WRITE(fi);
122 unsigned long address = FAULT_ADDRESS(fi);
124 if(!is_user && (address >= start_vm) && (address < end_vm)){
125 flush_tlb_kernel_vm();
128 else if(current->mm == NULL)
129 panic("Segfault with no mm");
130 err = handle_page_fault(address, ip, is_write, is_user, &si.si_code);
132 catcher = current->thread.fault_catcher;
135 else if(catcher != NULL){
136 current->thread.fault_addr = (void *) address;
137 do_longjmp(catcher, 1);
139 else if(current->thread.fault_addr != NULL)
140 panic("fault_addr set but no fault catcher");
141 else if(!is_user && arch_fixup(ip, sc))
145 panic("Kernel mode fault at addr 0x%lx, ip 0x%lx",
149 si.si_signo = SIGBUS;
151 si.si_code = BUS_ADRERR;
152 si.si_addr = (void *)address;
153 current->thread.arch.faultinfo = fi;
154 force_sig_info(SIGBUS, &si, current);
156 else if(err == -ENOMEM){
157 printk("VM: killing process %s\n", current->comm);
161 si.si_signo = SIGSEGV;
162 si.si_addr = (void *) address;
163 current->thread.arch.faultinfo = fi;
164 force_sig_info(SIGSEGV, &si, current);
169 void bad_segv(struct faultinfo fi, unsigned long ip)
173 si.si_signo = SIGSEGV;
174 si.si_code = SEGV_ACCERR;
175 si.si_addr = (void *) FAULT_ADDRESS(fi);
176 current->thread.arch.faultinfo = fi;
177 force_sig_info(SIGSEGV, &si, current);
180 void relay_signal(int sig, union uml_pt_regs *regs)
182 if(arch_handle_signal(sig, regs)) return;
183 if(!UPT_IS_USER(regs))
184 panic("Kernel mode signal %d", sig);
185 current->thread.arch.faultinfo = *UPT_FAULTINFO(regs);
186 force_sig(sig, current);
189 void bus_handler(int sig, union uml_pt_regs *regs)
191 if(current->thread.fault_catcher != NULL)
192 do_longjmp(current->thread.fault_catcher, 1);
193 else relay_signal(sig, regs);
196 void winch(int sig, union uml_pt_regs *regs)
198 do_IRQ(WINCH_IRQ, regs);
205 DEFINE_SPINLOCK(trap_lock);
207 static int trap_index = 0;
209 int next_trap_index(int limit)
213 spin_lock(&trap_lock);
215 if(++trap_index == limit)
217 spin_unlock(&trap_lock);
222 * Overrides for Emacs so that we follow Linus's tabbing style.
223 * Emacs will notice this stuff at the end of the file and automatically
224 * adjust the settings for this buffer only. This must remain at the end
226 * ---------------------------------------------------------------------------
228 * c-file-style: "linux"