blob: 9ae2eff0d8f3fbb392ccaccb54249e9a0d99f8d2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2000, 2001 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
5
6#include "linux/kernel.h"
7#include "asm/errno.h"
8#include "linux/sched.h"
9#include "linux/mm.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"
20#include "asm/irq.h"
21#include "user_util.h"
22#include "kern_util.h"
23#include "kern.h"
24#include "chan_kern.h"
25#include "mconsole_kern.h"
26#include "2_5compat.h"
27#include "mem.h"
28#include "mem_kern.h"
29
30int handle_page_fault(unsigned long address, unsigned long ip,
31 int is_write, int is_user, int *code_out)
32{
33 struct mm_struct *mm = current->mm;
34 struct vm_area_struct *vma;
35 pgd_t *pgd;
36 pud_t *pud;
37 pmd_t *pmd;
38 pte_t *pte;
39 unsigned long page;
40 int err = -EFAULT;
41
42 *code_out = SEGV_MAPERR;
43 down_read(&mm->mmap_sem);
44 vma = find_vma(mm, address);
45 if(!vma)
46 goto out;
47 else if(vma->vm_start <= address)
48 goto good_area;
49 else if(!(vma->vm_flags & VM_GROWSDOWN))
50 goto out;
Jeff Dike2d58cc92005-05-06 21:30:55 -070051 else if(is_user && !ARCH_IS_STACKGROW(address))
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 goto out;
53 else if(expand_stack(vma, address))
54 goto out;
55
56 good_area:
57 *code_out = SEGV_ACCERR;
58 if(is_write && !(vma->vm_flags & VM_WRITE))
59 goto out;
Jeff Dike13479d52005-05-20 13:59:08 -070060
61 if(!(vma->vm_flags & (VM_READ | VM_EXEC)))
62 goto out;
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 page = address & PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 do {
66 survive:
67 switch (handle_mm_fault(mm, vma, address, is_write)){
68 case VM_FAULT_MINOR:
69 current->min_flt++;
70 break;
71 case VM_FAULT_MAJOR:
72 current->maj_flt++;
73 break;
74 case VM_FAULT_SIGBUS:
75 err = -EACCES;
76 goto out;
77 case VM_FAULT_OOM:
78 err = -ENOMEM;
79 goto out_of_memory;
80 default:
81 BUG();
82 }
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));
88 err = 0;
89 *pte = pte_mkyoung(*pte);
90 if(pte_write(*pte)) *pte = pte_mkdirty(*pte);
91 flush_tlb_page(vma, page);
92 out:
93 up_read(&mm->mmap_sem);
94 return(err);
95
96/*
97 * We ran out of memory, or some other thing happened to us that made
98 * us unable to handle the page fault gracefully.
99 */
100out_of_memory:
101 if (current->pid == 1) {
102 up_read(&mm->mmap_sem);
103 yield();
104 down_read(&mm->mmap_sem);
105 goto survive;
106 }
107 goto out;
108}
109
110LIST_HEAD(physmem_remappers);
111
112void register_remapper(struct remapper *info)
113{
114 list_add(&info->list, &physmem_remappers);
115}
116
117static int check_remapped_addr(unsigned long address, int is_write)
118{
119 struct remapper *remapper;
120 struct list_head *ele;
121 __u64 offset;
122 int fd;
123
124 fd = phys_mapping(__pa(address), &offset);
125 if(fd == -1)
126 return(0);
127
128 list_for_each(ele, &physmem_remappers){
129 remapper = list_entry(ele, struct remapper, list);
130 if((*remapper->proc)(fd, address, is_write, offset))
131 return(1);
132 }
133
134 return(0);
135}
136
Bodo Stroesserc5784552005-05-05 16:15:31 -0700137/*
138 * We give a *copy* of the faultinfo in the regs to segv.
139 * This must be done, since nesting SEGVs could overwrite
140 * the info in the regs. A pointer to the info then would
141 * give us bad data!
142 */
143unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user, void *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 struct siginfo si;
146 void *catcher;
147 int err;
Bodo Stroesserc5784552005-05-05 16:15:31 -0700148 int is_write = FAULT_WRITE(fi);
149 unsigned long address = FAULT_ADDRESS(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 if(!is_user && (address >= start_vm) && (address < end_vm)){
152 flush_tlb_kernel_vm();
153 return(0);
154 }
155 else if(check_remapped_addr(address & PAGE_MASK, is_write))
156 return(0);
157 else if(current->mm == NULL)
158 panic("Segfault with no mm");
159 err = handle_page_fault(address, ip, is_write, is_user, &si.si_code);
160
161 catcher = current->thread.fault_catcher;
162 if(!err)
163 return(0);
164 else if(catcher != NULL){
165 current->thread.fault_addr = (void *) address;
166 do_longjmp(catcher, 1);
167 }
168 else if(current->thread.fault_addr != NULL)
169 panic("fault_addr set but no fault catcher");
Bodo Stroesserc5784552005-05-05 16:15:31 -0700170 else if(!is_user && arch_fixup(ip, sc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return(0);
172
173 if(!is_user)
174 panic("Kernel mode fault at addr 0x%lx, ip 0x%lx",
175 address, ip);
176
177 if(err == -EACCES){
178 si.si_signo = SIGBUS;
179 si.si_errno = 0;
180 si.si_code = BUS_ADRERR;
181 si.si_addr = (void *)address;
Bodo Stroesserc5784552005-05-05 16:15:31 -0700182 current->thread.arch.faultinfo = fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 force_sig_info(SIGBUS, &si, current);
184 }
185 else if(err == -ENOMEM){
186 printk("VM: killing process %s\n", current->comm);
187 do_exit(SIGKILL);
188 }
189 else {
190 si.si_signo = SIGSEGV;
191 si.si_addr = (void *) address;
Bodo Stroesserc5784552005-05-05 16:15:31 -0700192 current->thread.arch.faultinfo = fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 force_sig_info(SIGSEGV, &si, current);
194 }
195 return(0);
196}
197
Bodo Stroesserc5784552005-05-05 16:15:31 -0700198void bad_segv(struct faultinfo fi, unsigned long ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 struct siginfo si;
201
202 si.si_signo = SIGSEGV;
203 si.si_code = SEGV_ACCERR;
Bodo Stroesserc5784552005-05-05 16:15:31 -0700204 si.si_addr = (void *) FAULT_ADDRESS(fi);
205 current->thread.arch.faultinfo = fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 force_sig_info(SIGSEGV, &si, current);
207}
208
209void relay_signal(int sig, union uml_pt_regs *regs)
210{
211 if(arch_handle_signal(sig, regs)) return;
212 if(!UPT_IS_USER(regs))
213 panic("Kernel mode signal %d", sig);
Bodo Stroesserc5784552005-05-05 16:15:31 -0700214 current->thread.arch.faultinfo = *UPT_FAULTINFO(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 force_sig(sig, current);
216}
217
218void bus_handler(int sig, union uml_pt_regs *regs)
219{
220 if(current->thread.fault_catcher != NULL)
221 do_longjmp(current->thread.fault_catcher, 1);
222 else relay_signal(sig, regs);
223}
224
225void winch(int sig, union uml_pt_regs *regs)
226{
227 do_IRQ(WINCH_IRQ, regs);
228}
229
230void trap_init(void)
231{
232}
233
234DEFINE_SPINLOCK(trap_lock);
235
236static int trap_index = 0;
237
238int next_trap_index(int limit)
239{
240 int ret;
241
242 spin_lock(&trap_lock);
243 ret = trap_index;
244 if(++trap_index == limit)
245 trap_index = 0;
246 spin_unlock(&trap_lock);
247 return(ret);
248}
249
250/*
251 * Overrides for Emacs so that we follow Linus's tabbing style.
252 * Emacs will notice this stuff at the end of the file and automatically
253 * adjust the settings for this buffer only. This must remain at the end
254 * of the file.
255 * ---------------------------------------------------------------------------
256 * Local variables:
257 * c-file-style: "linux"
258 * End:
259 */