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"
20 #include "user_util.h"
21 #include "kern_util.h"
23 #include "chan_kern.h"
24 #include "mconsole_kern.h"
25 #include "2_5compat.h"
29 int handle_page_fault(unsigned long address, unsigned long ip,
30 int is_write, int is_user, int *code_out)
32 struct mm_struct *mm = current->mm;
33 struct vm_area_struct *vma;
40 *code_out = SEGV_MAPERR;
41 down_read(&mm->mmap_sem);
42 vma = find_vma(mm, address);
45 else if(vma->vm_start <= address)
47 else if(!(vma->vm_flags & VM_GROWSDOWN))
49 else if(expand_stack(vma, address))
53 *code_out = SEGV_ACCERR;
54 if(is_write && !(vma->vm_flags & VM_WRITE))
56 page = address & PAGE_MASK;
57 pgd = pgd_offset(mm, page);
58 pmd = pmd_offset(pgd, page);
61 switch (handle_mm_fault(mm, vma, address, is_write)){
77 pte = pte_offset_kernel(pmd, page);
78 } while(!pte_present(*pte));
80 *pte = pte_mkyoung(*pte);
81 if(pte_write(*pte)) *pte = pte_mkdirty(*pte);
82 flush_tlb_page(vma, page);
84 up_read(&mm->mmap_sem);
88 * We ran out of memory, or some other thing happened to us that made
89 * us unable to handle the page fault gracefully.
92 if (current->pid == 1) {
93 up_read(&mm->mmap_sem);
95 down_read(&mm->mmap_sem);
101 LIST_HEAD(physmem_remappers);
103 void register_remapper(struct remapper *info)
105 list_add(&info->list, &physmem_remappers);
108 static int check_remapped_addr(unsigned long address, int is_write)
110 struct remapper *remapper;
111 struct list_head *ele;
115 fd = phys_mapping(__pa(address), &offset);
119 list_for_each(ele, &physmem_remappers){
120 remapper = list_entry(ele, struct remapper, list);
121 if((*remapper->proc)(fd, address, is_write, offset))
128 unsigned long segv(unsigned long address, unsigned long ip, int is_write,
129 int is_user, void *sc)
135 if(!is_user && (address >= start_vm) && (address < end_vm)){
136 flush_tlb_kernel_vm();
139 else if(check_remapped_addr(address & PAGE_MASK, is_write))
141 else if(current->mm == NULL)
142 panic("Segfault with no mm");
143 err = handle_page_fault(address, ip, is_write, is_user, &si.si_code);
145 catcher = current->thread.fault_catcher;
148 else if(catcher != NULL){
149 current->thread.fault_addr = (void *) address;
150 do_longjmp(catcher, 1);
152 else if(current->thread.fault_addr != NULL)
153 panic("fault_addr set but no fault catcher");
154 else if(arch_fixup(ip, sc))
158 panic("Kernel mode fault at addr 0x%lx, ip 0x%lx",
162 si.si_signo = SIGBUS;
164 si.si_code = BUS_ADRERR;
165 si.si_addr = (void *)address;
166 force_sig_info(SIGBUS, &si, current);
168 else if(err == -ENOMEM){
169 printk("VM: killing process %s\n", current->comm);
173 si.si_signo = SIGSEGV;
174 si.si_addr = (void *) address;
175 current->thread.cr2 = address;
176 current->thread.err = is_write;
177 force_sig_info(SIGSEGV, &si, current);
182 void bad_segv(unsigned long address, unsigned long ip, int is_write)
186 si.si_signo = SIGSEGV;
187 si.si_code = SEGV_ACCERR;
188 si.si_addr = (void *) address;
189 current->thread.cr2 = address;
190 current->thread.err = is_write;
191 force_sig_info(SIGSEGV, &si, current);
194 void relay_signal(int sig, union uml_pt_regs *regs)
196 if(arch_handle_signal(sig, regs)) return;
197 if(!UPT_IS_USER(regs))
198 panic("Kernel mode signal %d", sig);
199 force_sig(sig, current);
202 void bus_handler(int sig, union uml_pt_regs *regs)
204 if(current->thread.fault_catcher != NULL)
205 do_longjmp(current->thread.fault_catcher, 1);
206 else relay_signal(sig, regs);
209 void winch(int sig, union uml_pt_regs *regs)
211 do_IRQ(WINCH_IRQ, regs);
218 spinlock_t trap_lock = SPIN_LOCK_UNLOCKED;
220 static int trap_index = 0;
222 int next_trap_index(int limit)
226 spin_lock(&trap_lock);
228 if(++trap_index == limit)
230 spin_unlock(&trap_lock);
235 * Overrides for Emacs so that we follow Linus's tabbing style.
236 * Emacs will notice this stuff at the end of the file and automatically
237 * adjust the settings for this buffer only. This must remain at the end
239 * ---------------------------------------------------------------------------
241 * c-file-style: "linux"