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/tlbflush.h"
17 #include "asm/a.out.h"
18 #include "asm/current.h"
19 #include "user_util.h"
20 #include "kern_util.h"
22 #include "chan_kern.h"
23 #include "mconsole_kern.h"
24 #include "2_5compat.h"
26 int handle_page_fault(unsigned long address, unsigned long ip,
27 int is_write, int is_user, int *code_out)
29 struct mm_struct *mm = current->mm;
30 struct vm_area_struct *vma;
37 *code_out = SEGV_MAPERR;
38 down_read(&mm->mmap_sem);
39 vma = find_vma(mm, address);
42 else if(vma->vm_start <= address)
44 else if(!(vma->vm_flags & VM_GROWSDOWN))
46 else if(expand_stack(vma, address))
50 *code_out = SEGV_ACCERR;
51 if(is_write && !(vma->vm_flags & VM_WRITE))
53 page = address & PAGE_MASK;
54 if(page == (unsigned long) current->thread_info + PAGE_SIZE)
55 panic("Kernel stack overflow");
56 pgd = pgd_offset(mm, page);
57 pmd = pmd_offset(pgd, page);
60 switch (handle_mm_fault(mm, vma, address, is_write)){
76 pte = pte_offset_kernel(pmd, page);
77 } while(!pte_present(*pte));
78 *pte = pte_mkyoung(*pte);
79 if(pte_write(*pte)) *pte = pte_mkdirty(*pte);
80 flush_tlb_page(vma, page);
83 up_read(&mm->mmap_sem);
87 * We ran out of memory, or some other thing happened to us that made
88 * us unable to handle the page fault gracefully.
91 if (current->pid == 1) {
92 up_read(&mm->mmap_sem);
94 down_read(&mm->mmap_sem);
101 unsigned long segv(unsigned long address, unsigned long ip, int is_write,
102 int is_user, void *sc)
108 if(!is_user && (address >= start_vm) && (address < end_vm)){
109 flush_tlb_kernel_vm();
112 if(current->mm == NULL)
113 panic("Segfault with no mm");
114 err = handle_page_fault(address, ip, is_write, is_user, &si.si_code);
116 catcher = current->thread.fault_catcher;
119 else if(catcher != NULL){
120 current->thread.fault_addr = (void *) address;
121 do_longjmp(catcher, 1);
123 else if(current->thread.fault_addr != NULL){
124 panic("fault_addr set but no fault catcher");
126 else if(arch_fixup(ip, sc))
130 panic("Kernel mode fault at addr 0x%lx, ip 0x%lx",
134 si.si_signo = SIGBUS;
136 si.si_code = BUS_ADRERR;
137 si.si_addr = (void *)address;
138 force_sig_info(SIGBUS, &si, current);
140 else if(err == -ENOMEM){
141 printk("VM: killing process %s\n", current->comm);
145 si.si_signo = SIGSEGV;
146 si.si_addr = (void *) address;
147 current->thread.cr2 = address;
148 current->thread.err = is_write;
149 force_sig_info(SIGSEGV, &si, current);
154 void bad_segv(unsigned long address, unsigned long ip, int is_write)
158 printk(KERN_ERR "Unfixable SEGV in '%s' (pid %d) at 0x%lx "
159 "(ip 0x%lx)\n", current->comm, current->pid, address, ip);
160 si.si_signo = SIGSEGV;
161 si.si_code = SEGV_ACCERR;
162 si.si_addr = (void *) address;
163 current->thread.cr2 = address;
164 current->thread.err = is_write;
165 force_sig_info(SIGSEGV, &si, current);
168 void relay_signal(int sig, union uml_pt_regs *regs)
170 if(arch_handle_signal(sig, regs)) return;
171 if(!UPT_IS_USER(regs))
172 panic("Kernel mode signal %d", sig);
173 force_sig(sig, current);
176 void bus_handler(int sig, union uml_pt_regs *regs)
178 if(current->thread.fault_catcher != NULL)
179 do_longjmp(current->thread.fault_catcher, 1);
180 else relay_signal(sig, regs);
187 spinlock_t trap_lock = SPIN_LOCK_UNLOCKED;
189 static int trap_index = 0;
191 int next_trap_index(int limit)
195 spin_lock(&trap_lock);
197 if(++trap_index == limit)
199 spin_unlock(&trap_lock);
204 * Overrides for Emacs so that we follow Linus's tabbing style.
205 * Emacs will notice this stuff at the end of the file and automatically
206 * adjust the settings for this buffer only. This must remain at the end
208 * ---------------------------------------------------------------------------
210 * c-file-style: "linux"