This commit was manufactured by cvs2svn to create branch
[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                         BUG();
78                 }
79                 pte = pte_offset_kernel(pmd, page);
80         } while(!pte_present(*pte));
81         err = 0;
82         *pte = pte_mkyoung(*pte);
83         if(pte_write(*pte)) *pte = pte_mkdirty(*pte);
84         flush_tlb_page(vma, page);
85  out:
86         up_read(&mm->mmap_sem);
87         return(err);
88
89 /*
90  * We ran out of memory, or some other thing happened to us that made
91  * us unable to handle the page fault gracefully.
92  */
93 out_of_memory:
94         if (current->pid == 1) {
95                 up_read(&mm->mmap_sem);
96                 yield();
97                 down_read(&mm->mmap_sem);
98                 goto survive;
99         }
100         goto out;
101 }
102
103 LIST_HEAD(physmem_remappers);
104
105 void register_remapper(struct remapper *info)
106 {
107         list_add(&info->list, &physmem_remappers);
108 }
109
110 static int check_remapped_addr(unsigned long address, int is_write, int is_user)
111 {
112         struct remapper *remapper;
113         struct list_head *ele;
114         __u64 offset;
115         int fd;
116
117         fd = phys_mapping(__pa(address), &offset);
118         if(fd == -1)
119                 return(0);
120
121         list_for_each(ele, &physmem_remappers){
122                 remapper = list_entry(ele, struct remapper, list);
123                 if((*remapper->proc)(fd, address, is_write, offset, is_user))
124                         return(1);
125         }
126
127         return(0);
128 }
129
130 unsigned long segv(unsigned long address, unsigned long ip, int is_write, 
131                    int is_user, void *sc)
132 {
133         struct siginfo si;
134         void *catcher;
135         int err;
136
137         if(!is_user && (address >= start_vm) && (address < end_vm)){
138                 flush_tlb_kernel_vm();
139                 return(0);
140         }
141         else if(check_remapped_addr(address & PAGE_MASK, is_write, is_user))
142                 return(0);
143         else if(current->mm == NULL)
144                 panic("Segfault with no mm");
145         err = handle_page_fault(address, ip, is_write, is_user, &si.si_code);
146
147         catcher = current->thread.fault_catcher;
148         if(!err)
149                 return(0);
150         else if(catcher != NULL){
151                 current->thread.fault_addr = (void *) address;
152                 do_longjmp(catcher, 1);
153         } 
154         else if(current->thread.fault_addr != NULL)
155                 panic("fault_addr set but no fault catcher");
156         else if(arch_fixup(ip, sc))
157                 return(0);
158
159         if(!is_user) 
160                 panic("Kernel mode fault at addr 0x%lx, ip 0x%lx", 
161                       address, ip);
162
163         if(err == -EACCES){
164                 si.si_signo = SIGBUS;
165                 si.si_errno = 0;
166                 si.si_code = BUS_ADRERR;
167                 si.si_addr = (void *)address;
168                 force_sig_info(SIGBUS, &si, current);
169         }
170         else if(err == -ENOMEM){
171                 printk("VM: killing process %s\n", current->comm);
172                 do_exit(SIGKILL);
173         }
174         else {
175                 si.si_signo = SIGSEGV;
176                 si.si_addr = (void *) address;
177                 current->thread.cr2 = address;
178                 current->thread.err = is_write;
179                 force_sig_info(SIGSEGV, &si, current);
180         }
181         return(0);
182 }
183
184 void bad_segv(unsigned long address, unsigned long ip, int is_write)
185 {
186         struct siginfo si;
187
188         si.si_signo = SIGSEGV;
189         si.si_code = SEGV_ACCERR;
190         si.si_addr = (void *) address;
191         current->thread.cr2 = address;
192         current->thread.err = is_write;
193         force_sig_info(SIGSEGV, &si, current);
194 }
195
196 void relay_signal(int sig, union uml_pt_regs *regs)
197 {
198         if(arch_handle_signal(sig, regs)) return;
199         if(!UPT_IS_USER(regs))
200                 panic("Kernel mode signal %d", sig);
201         force_sig(sig, current);
202 }
203
204 void bus_handler(int sig, union uml_pt_regs *regs)
205 {
206         if(current->thread.fault_catcher != NULL)
207                 do_longjmp(current->thread.fault_catcher, 1);
208         else relay_signal(sig, regs);
209 }
210
211 void winch(int sig, union uml_pt_regs *regs)
212 {
213         do_IRQ(WINCH_IRQ, regs);
214 }
215
216 void trap_init(void)
217 {
218 }
219
220 spinlock_t trap_lock = SPIN_LOCK_UNLOCKED;
221
222 static int trap_index = 0;
223
224 int next_trap_index(int limit)
225 {
226         int ret;
227
228         spin_lock(&trap_lock);
229         ret = trap_index;
230         if(++trap_index == limit)
231                 trap_index = 0;
232         spin_unlock(&trap_lock);
233         return(ret);
234 }
235
236 /*
237  * Overrides for Emacs so that we follow Linus's tabbing style.
238  * Emacs will notice this stuff at the end of the file and automatically
239  * adjust the settings for this buffer only.  This must remain at the end
240  * of the file.
241  * ---------------------------------------------------------------------------
242  * Local variables:
243  * c-file-style: "linux"
244  * End:
245  */