ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 "user_util.h"
20 #include "kern_util.h"
21 #include "kern.h"
22 #include "chan_kern.h"
23 #include "mconsole_kern.h"
24 #include "2_5compat.h"
25
26 int handle_page_fault(unsigned long address, unsigned long ip, 
27                       int is_write, int is_user, int *code_out)
28 {
29         struct mm_struct *mm = current->mm;
30         struct vm_area_struct *vma;
31         pgd_t *pgd;
32         pmd_t *pmd;
33         pte_t *pte;
34         unsigned long page;
35         int err = -EFAULT;
36
37         *code_out = SEGV_MAPERR;
38         down_read(&mm->mmap_sem);
39         vma = find_vma(mm, address);
40         if(!vma) 
41                 goto out;
42         else if(vma->vm_start <= address) 
43                 goto good_area;
44         else if(!(vma->vm_flags & VM_GROWSDOWN)) 
45                 goto out;
46         else if(expand_stack(vma, address)) 
47                 goto out;
48
49  good_area:
50         *code_out = SEGV_ACCERR;
51         if(is_write && !(vma->vm_flags & VM_WRITE)) 
52                 goto out;
53         page = address & PAGE_MASK;
54         if(page == (unsigned long) current->thread_info + PAGE_SIZE)
55                 panic("Kernel stack overflow");
56         pgd = pgd_offset(mm, page);
57         pmd = pmd_offset(pgd, page);
58  survive:
59         do {
60                 switch (handle_mm_fault(mm, vma, address, is_write)){
61                 case VM_FAULT_MINOR:
62                         current->min_flt++;
63                         break;
64                 case VM_FAULT_MAJOR:
65                         current->maj_flt++;
66                         break;
67                 case VM_FAULT_SIGBUS:
68                         err = -EACCES;
69                         goto out;
70                 case VM_FAULT_OOM:
71                         err = -ENOMEM;
72                         goto out_of_memory;
73                 default:
74                         BUG();
75                 }
76                 pte = pte_offset_kernel(pmd, page);
77         } while(!pte_present(*pte));
78         *pte = pte_mkyoung(*pte);
79         if(pte_write(*pte)) *pte = pte_mkdirty(*pte);
80         flush_tlb_page(vma, page);
81         err = 0;
82  out:
83         up_read(&mm->mmap_sem);
84         return(err);
85
86 /*
87  * We ran out of memory, or some other thing happened to us that made
88  * us unable to handle the page fault gracefully.
89  */
90 out_of_memory:
91         if (current->pid == 1) {
92                 up_read(&mm->mmap_sem);
93                 yield();
94                 down_read(&mm->mmap_sem);
95                 goto survive;
96         }
97         err = -ENOMEM;
98         goto out;
99 }
100
101 unsigned long segv(unsigned long address, unsigned long ip, int is_write, 
102                    int is_user, void *sc)
103 {
104         struct siginfo si;
105         void *catcher;
106         int err;
107
108         if(!is_user && (address >= start_vm) && (address < end_vm)){
109                 flush_tlb_kernel_vm();
110                 return(0);
111         }
112         if(current->mm == NULL)
113                 panic("Segfault with no mm");
114         err = handle_page_fault(address, ip, is_write, is_user, &si.si_code);
115
116         catcher = current->thread.fault_catcher;
117         if(!err)
118                 return(0);
119         else if(catcher != NULL){
120                 current->thread.fault_addr = (void *) address;
121                 do_longjmp(catcher, 1);
122         } 
123         else if(current->thread.fault_addr != NULL){
124                 panic("fault_addr set but no fault catcher");
125         }
126         else if(arch_fixup(ip, sc))
127                 return(0);
128
129         if(!is_user) 
130                 panic("Kernel mode fault at addr 0x%lx, ip 0x%lx", 
131                       address, ip);
132
133         if(err == -EACCES){
134                 si.si_signo = SIGBUS;
135                 si.si_errno = 0;
136                 si.si_code = BUS_ADRERR;
137                 si.si_addr = (void *)address;
138                 force_sig_info(SIGBUS, &si, current);
139         }
140         else if(err == -ENOMEM){
141                 printk("VM: killing process %s\n", current->comm);
142                 do_exit(SIGKILL);
143         }
144         else {
145                 si.si_signo = SIGSEGV;
146                 si.si_addr = (void *) address;
147                 current->thread.cr2 = address;
148                 current->thread.err = is_write;
149                 force_sig_info(SIGSEGV, &si, current);
150         }
151         return(0);
152 }
153
154 void bad_segv(unsigned long address, unsigned long ip, int is_write)
155 {
156         struct siginfo si;
157
158         printk(KERN_ERR "Unfixable SEGV in '%s' (pid %d) at 0x%lx "
159                "(ip 0x%lx)\n", current->comm, current->pid, address, ip);
160         si.si_signo = SIGSEGV;
161         si.si_code = SEGV_ACCERR;
162         si.si_addr = (void *) address;
163         current->thread.cr2 = address;
164         current->thread.err = is_write;
165         force_sig_info(SIGSEGV, &si, current);
166 }
167
168 void relay_signal(int sig, union uml_pt_regs *regs)
169 {
170         if(arch_handle_signal(sig, regs)) return;
171         if(!UPT_IS_USER(regs))
172                 panic("Kernel mode signal %d", sig);
173         force_sig(sig, current);
174 }
175
176 void bus_handler(int sig, union uml_pt_regs *regs)
177 {
178         if(current->thread.fault_catcher != NULL)
179                 do_longjmp(current->thread.fault_catcher, 1);
180         else relay_signal(sig, regs);
181 }
182
183 void trap_init(void)
184 {
185 }
186
187 spinlock_t trap_lock = SPIN_LOCK_UNLOCKED;
188
189 static int trap_index = 0;
190
191 int next_trap_index(int limit)
192 {
193         int ret;
194
195         spin_lock(&trap_lock);
196         ret = trap_index;
197         if(++trap_index == limit)
198                 trap_index = 0;
199         spin_unlock(&trap_lock);
200         return(ret);
201 }
202
203 /*
204  * Overrides for Emacs so that we follow Linus's tabbing style.
205  * Emacs will notice this stuff at the end of the file and automatically
206  * adjust the settings for this buffer only.  This must remain at the end
207  * of the file.
208  * ---------------------------------------------------------------------------
209  * Local variables:
210  * c-file-style: "linux"
211  * End:
212  */