Automatic merge of /spare/repo/netdev-2.6 branch forcedeth
[linux-2.6] / arch / um / kernel / trap_kern.c
1 /* 
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
30 int 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;
51         else if(is_user && !ARCH_IS_STACKGROW(address))
52                 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;
60
61         if(!(vma->vm_flags & (VM_READ | VM_EXEC)))
62                 goto out;
63
64         page = address & PAGE_MASK;
65         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  */
100 out_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
110 /*
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  */
116 unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user, void *sc)
117 {
118         struct siginfo si;
119         void *catcher;
120         int err;
121         int is_write = FAULT_WRITE(fi);
122         unsigned long address = FAULT_ADDRESS(fi);
123
124         if(!is_user && (address >= start_vm) && (address < end_vm)){
125                 flush_tlb_kernel_vm();
126                 return(0);
127         }
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);
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");
141         else if(!is_user && arch_fixup(ip, sc))
142                 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;
153                 current->thread.arch.faultinfo = fi;
154                 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;
163                 current->thread.arch.faultinfo = fi;
164                 force_sig_info(SIGSEGV, &si, current);
165         }
166         return(0);
167 }
168
169 void bad_segv(struct faultinfo fi, unsigned long ip)
170 {
171         struct siginfo si;
172
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);
178 }
179
180 void 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);
185         current->thread.arch.faultinfo = *UPT_FAULTINFO(regs);
186         force_sig(sig, current);
187 }
188
189 void 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
196 void winch(int sig, union uml_pt_regs *regs)
197 {
198         do_IRQ(WINCH_IRQ, regs);
199 }
200
201 void trap_init(void)
202 {
203 }
204
205 DEFINE_SPINLOCK(trap_lock);
206
207 static int trap_index = 0;
208
209 int 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  */