Merge to Fedora kernel-2.6.7-1.494 and VServer 1.9.1.12. Fix some previous merge...
[linux-2.6.git] / 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/tlbflush.h"
17 #include "asm/a.out.h"
18 #include "asm/current.h"
19 #include "asm/irq.h"
20 #include "user_util.h"
21 #include "kern_util.h"
22 #include "kern.h"
23 #include "chan_kern.h"
24 #include "mconsole_kern.h"
25 #include "2_5compat.h"
26 #include "mem.h"
27 #include "mem_kern.h"
28
29 int handle_page_fault(unsigned long address, unsigned long ip, 
30                       int is_write, int is_user, int *code_out)
31 {
32         struct mm_struct *mm = current->mm;
33         struct vm_area_struct *vma;
34         pgd_t *pgd;
35         pmd_t *pmd;
36         pte_t *pte;
37         unsigned long page;
38         int err = -EFAULT;
39
40         *code_out = SEGV_MAPERR;
41         down_read(&mm->mmap_sem);
42         vma = find_vma(mm, address);
43         if(!vma) 
44                 goto out;
45         else if(vma->vm_start <= address) 
46                 goto good_area;
47         else if(!(vma->vm_flags & VM_GROWSDOWN)) 
48                 goto out;
49         else if(expand_stack(vma, address)) 
50                 goto out;
51
52  good_area:
53         *code_out = SEGV_ACCERR;
54         if(is_write && !(vma->vm_flags & VM_WRITE)) 
55                 goto out;
56         page = address & PAGE_MASK;
57         if(page == (unsigned long) current_thread + PAGE_SIZE)
58                 panic("Kernel stack overflow");
59         pgd = pgd_offset(mm, page);
60         pmd = pmd_offset(pgd, page);
61         do {
62  survive:
63                 switch (handle_mm_fault(mm, vma, address, is_write)){
64                 case VM_FAULT_MINOR:
65                         current->min_flt++;
66                         break;
67                 case VM_FAULT_MAJOR:
68                         current->maj_flt++;
69                         break;
70                 case VM_FAULT_SIGBUS:
71                         err = -EACCES;
72                         goto out;
73                 case VM_FAULT_OOM:
74                         err = -ENOMEM;
75                         goto out_of_memory;
76                 default:
77                         if (current->pid == 1) {
78                                 up_read(&mm->mmap_sem);
79                                 yield();
80                                 down_read(&mm->mmap_sem);
81                                 goto survive;
82                         }
83                         goto out;
84                 }
85                 pte = pte_offset_kernel(pmd, page);
86         } while(!pte_present(*pte));
87         err = 0;
88         *pte = pte_mkyoung(*pte);
89         if(pte_write(*pte)) *pte = pte_mkdirty(*pte);
90         flush_tlb_page(vma, page);
91  out:
92         up_read(&mm->mmap_sem);
93         return(err);
94
95 /*
96  * We ran out of memory, or some other thing happened to us that made
97  * us unable to handle the page fault gracefully.
98  */
99 out_of_memory:
100         if (current->pid == 1) {
101                 up_read(&mm->mmap_sem);
102                 yield();
103                 down_read(&mm->mmap_sem);
104                 goto survive;
105         }
106         err = -ENOMEM;
107         goto out;
108 }
109
110 LIST_HEAD(physmem_remappers);
111
112 void register_remapper(struct remapper *info)
113 {
114         list_add(&info->list, &physmem_remappers);
115 }
116
117 static 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
137 unsigned long segv(unsigned long address, unsigned long ip, int is_write, 
138                    int is_user, void *sc)
139 {
140         struct siginfo si;
141         void *catcher;
142         int err;
143
144         if(!is_user && (address >= start_vm) && (address < end_vm)){
145                 flush_tlb_kernel_vm();
146                 return(0);
147         }
148         else if(check_remapped_addr(address & PAGE_MASK, is_write))
149                 return(0);
150         else if(current->mm == NULL)
151                 panic("Segfault with no mm");
152         err = handle_page_fault(address, ip, is_write, is_user, &si.si_code);
153
154         catcher = current->thread.fault_catcher;
155         if(!err)
156                 return(0);
157         else if(catcher != NULL){
158                 current->thread.fault_addr = (void *) address;
159                 do_longjmp(catcher, 1);
160         } 
161         else if(current->thread.fault_addr != NULL)
162                 panic("fault_addr set but no fault catcher");
163         else if(arch_fixup(ip, sc))
164                 return(0);
165
166         if(!is_user) 
167                 panic("Kernel mode fault at addr 0x%lx, ip 0x%lx", 
168                       address, ip);
169
170         if(err == -EACCES){
171                 si.si_signo = SIGBUS;
172                 si.si_errno = 0;
173                 si.si_code = BUS_ADRERR;
174                 si.si_addr = (void *)address;
175                 force_sig_info(SIGBUS, &si, current);
176         }
177         else if(err == -ENOMEM){
178                 printk("VM: killing process %s\n", current->comm);
179                 do_exit(SIGKILL);
180         }
181         else {
182                 si.si_signo = SIGSEGV;
183                 si.si_addr = (void *) address;
184                 current->thread.cr2 = address;
185                 current->thread.err = is_write;
186                 force_sig_info(SIGSEGV, &si, current);
187         }
188         return(0);
189 }
190
191 void bad_segv(unsigned long address, unsigned long ip, int is_write)
192 {
193         struct siginfo si;
194
195         si.si_signo = SIGSEGV;
196         si.si_code = SEGV_ACCERR;
197         si.si_addr = (void *) address;
198         current->thread.cr2 = address;
199         current->thread.err = is_write;
200         force_sig_info(SIGSEGV, &si, current);
201 }
202
203 void relay_signal(int sig, union uml_pt_regs *regs)
204 {
205         if(arch_handle_signal(sig, regs)) return;
206         if(!UPT_IS_USER(regs))
207                 panic("Kernel mode signal %d", sig);
208         force_sig(sig, current);
209 }
210
211 void bus_handler(int sig, union uml_pt_regs *regs)
212 {
213         if(current->thread.fault_catcher != NULL)
214                 do_longjmp(current->thread.fault_catcher, 1);
215         else relay_signal(sig, regs);
216 }
217
218 void winch(int sig, union uml_pt_regs *regs)
219 {
220         do_IRQ(WINCH_IRQ, regs);
221 }
222
223 void trap_init(void)
224 {
225 }
226
227 spinlock_t trap_lock = SPIN_LOCK_UNLOCKED;
228
229 static int trap_index = 0;
230
231 int next_trap_index(int limit)
232 {
233         int ret;
234
235         spin_lock(&trap_lock);
236         ret = trap_index;
237         if(++trap_index == limit)
238                 trap_index = 0;
239         spin_unlock(&trap_lock);
240         return(ret);
241 }
242
243 /*
244  * Overrides for Emacs so that we follow Linus's tabbing style.
245  * Emacs will notice this stuff at the end of the file and automatically
246  * adjust the settings for this buffer only.  This must remain at the end
247  * of the file.
248  * ---------------------------------------------------------------------------
249  * Local variables:
250  * c-file-style: "linux"
251  * End:
252  */