blob: 1de22d8a313a0d7ad158952ca2077fd15e5a305f [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
Bodo Stroesserc5784552005-05-05 16:15:31 -0700110/*
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
114 * give us bad data!
115 */
116unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user, void *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 struct siginfo si;
119 void *catcher;
120 int err;
Bodo Stroesserc5784552005-05-05 16:15:31 -0700121 int is_write = FAULT_WRITE(fi);
122 unsigned long address = FAULT_ADDRESS(fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 if(!is_user && (address >= start_vm) && (address < end_vm)){
125 flush_tlb_kernel_vm();
126 return(0);
127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 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);
131
132 catcher = current->thread.fault_catcher;
133 if(!err)
134 return(0);
135 else if(catcher != NULL){
136 current->thread.fault_addr = (void *) address;
137 do_longjmp(catcher, 1);
138 }
139 else if(current->thread.fault_addr != NULL)
140 panic("fault_addr set but no fault catcher");
Bodo Stroesserc5784552005-05-05 16:15:31 -0700141 else if(!is_user && arch_fixup(ip, sc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return(0);
143
144 if(!is_user)
145 panic("Kernel mode fault at addr 0x%lx, ip 0x%lx",
146 address, ip);
147
148 if(err == -EACCES){
149 si.si_signo = SIGBUS;
150 si.si_errno = 0;
151 si.si_code = BUS_ADRERR;
152 si.si_addr = (void *)address;
Bodo Stroesserc5784552005-05-05 16:15:31 -0700153 current->thread.arch.faultinfo = fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 force_sig_info(SIGBUS, &si, current);
155 }
156 else if(err == -ENOMEM){
157 printk("VM: killing process %s\n", current->comm);
158 do_exit(SIGKILL);
159 }
160 else {
161 si.si_signo = SIGSEGV;
162 si.si_addr = (void *) address;
Bodo Stroesserc5784552005-05-05 16:15:31 -0700163 current->thread.arch.faultinfo = fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 force_sig_info(SIGSEGV, &si, current);
165 }
166 return(0);
167}
168
Bodo Stroesserc5784552005-05-05 16:15:31 -0700169void bad_segv(struct faultinfo fi, unsigned long ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 struct siginfo si;
172
173 si.si_signo = SIGSEGV;
174 si.si_code = SEGV_ACCERR;
Bodo Stroesserc5784552005-05-05 16:15:31 -0700175 si.si_addr = (void *) FAULT_ADDRESS(fi);
176 current->thread.arch.faultinfo = fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 force_sig_info(SIGSEGV, &si, current);
178}
179
180void relay_signal(int sig, union uml_pt_regs *regs)
181{
182 if(arch_handle_signal(sig, regs)) return;
183 if(!UPT_IS_USER(regs))
184 panic("Kernel mode signal %d", sig);
Bodo Stroesserc5784552005-05-05 16:15:31 -0700185 current->thread.arch.faultinfo = *UPT_FAULTINFO(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 force_sig(sig, current);
187}
188
189void bus_handler(int sig, union uml_pt_regs *regs)
190{
191 if(current->thread.fault_catcher != NULL)
192 do_longjmp(current->thread.fault_catcher, 1);
193 else relay_signal(sig, regs);
194}
195
196void winch(int sig, union uml_pt_regs *regs)
197{
198 do_IRQ(WINCH_IRQ, regs);
199}
200
201void trap_init(void)
202{
203}
204
205DEFINE_SPINLOCK(trap_lock);
206
207static int trap_index = 0;
208
209int next_trap_index(int limit)
210{
211 int ret;
212
213 spin_lock(&trap_lock);
214 ret = trap_index;
215 if(++trap_index == limit)
216 trap_index = 0;
217 spin_unlock(&trap_lock);
218 return(ret);
219}
220
221/*
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
225 * of the file.
226 * ---------------------------------------------------------------------------
227 * Local variables:
228 * c-file-style: "linux"
229 * End:
230 */