also update spec file ...
[linux-flexiantxendom0-3.2.10.git] / arch / mips64 / mm / fault.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 1995 - 2000 by Ralf Baechle
7  * Copyright (C) 1999, 2000 by Silicon Graphics, Inc.
8  */
9 #include <linux/config.h>
10 #include <linux/signal.h>
11 #include <linux/sched.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/string.h>
16 #include <linux/types.h>
17 #include <linux/ptrace.h>
18 #include <linux/mman.h>
19 #include <linux/mm.h>
20 #include <linux/smp.h>
21 #include <linux/smp_lock.h>
22 #include <linux/version.h>
23 #include <linux/vt_kern.h>              /* For unblank_screen() */
24 #include <linux/module.h>
25
26 #include <asm/branch.h>
27 #include <asm/hardirq.h>
28 #include <asm/pgalloc.h>
29 #include <asm/mmu_context.h>
30 #include <asm/system.h>
31 #include <asm/uaccess.h>
32 #include <asm/ptrace.h>
33
34 #define development_version (LINUX_VERSION_CODE & 0x100)
35
36 /*
37  * Macro for exception fixup code to access integer registers.
38  */
39 #define dpf_reg(r) (regs->regs[r])
40
41 /*
42  * Unlock any spinlocks which will prevent us from getting the out
43  */
44 void bust_spinlocks(int yes)
45 {
46         int loglevel_save = console_loglevel;
47
48         if (yes) {
49                 oops_in_progress = 1;
50                 return;
51         }
52 #ifdef CONFIG_VT
53         unblank_screen();
54 #endif
55         oops_in_progress = 0;
56         /*
57          * OK, the message is on the console.  Now we call printk()
58          * without oops_in_progress set so that printk will give klogd
59          * a poke.  Hold onto your hats...
60          */
61         console_loglevel = 15;          /* NMI oopser may have shut the console up */
62         printk(" ");
63         console_loglevel = loglevel_save;
64 }
65
66 /*
67  * This routine handles page faults.  It determines the address,
68  * and the problem, and then passes it off to one of the appropriate
69  * routines.
70  */
71 asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long write,
72                               unsigned long address)
73 {
74         struct vm_area_struct * vma;
75         struct task_struct *tsk = current;
76         struct mm_struct *mm = tsk->mm;
77         const struct exception_table_entry *fixup;
78         siginfo_t info;
79
80 #if 0
81         printk("Cpu%d[%s:%d:%08lx:%ld:%08lx]\n", smp_processor_id(),
82                current->comm, current->pid, address, write, regs->cp0_epc);
83 #endif
84
85         /*
86          * We fault-in kernel-space virtual memory on-demand. The
87          * 'reference' page table is init_mm.pgd.
88          *
89          * NOTE! We MUST NOT take any locks for this case. We may
90          * be in an interrupt or a critical region, and should
91          * only copy the information from the master page table,
92          * nothing more.
93          */
94         if (address >= VMALLOC_START)
95                 goto vmalloc_fault;
96
97         info.si_code = SEGV_MAPERR;
98         /*
99          * If we're in an interrupt or have no user
100          * context, we must not take the fault..
101          */
102         if (in_atomic() || !mm)
103                 goto no_context;
104
105         down_read(&mm->mmap_sem);
106         vma = find_vma(mm, address);
107         if (!vma)
108                 goto bad_area;
109         if (vma->vm_start <= address)
110                 goto good_area;
111         if (!(vma->vm_flags & VM_GROWSDOWN))
112                 goto bad_area;
113         if (expand_stack(vma, address))
114                 goto bad_area;
115 /*
116  * Ok, we have a good vm_area for this memory access, so
117  * we can handle it..
118  */
119 good_area:
120         info.si_code = SEGV_ACCERR;
121
122         if (write) {
123                 if (!(vma->vm_flags & VM_WRITE))
124                         goto bad_area;
125         } else {
126                 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
127                         goto bad_area;
128         }
129
130 survive:
131         /*
132          * If for any reason at all we couldn't handle the fault,
133          * make sure we exit gracefully rather than endlessly redo
134          * the fault.
135          */
136         switch (handle_mm_fault(mm, vma, address, write)) {
137         case VM_FAULT_MINOR:
138                 tsk->min_flt++;
139                 break;
140         case VM_FAULT_MAJOR:
141                 tsk->maj_flt++;
142                 break;
143         case VM_FAULT_SIGBUS:
144                 goto do_sigbus;
145         case VM_FAULT_OOM:
146                 goto out_of_memory;
147         default:
148                 BUG();
149         }
150
151         up_read(&mm->mmap_sem);
152         return;
153
154 /*
155  * Something tried to access memory that isn't in our memory map..
156  * Fix it, but check if it's kernel or user first..
157  */
158 bad_area:
159         up_read(&mm->mmap_sem);
160
161         /* User mode accesses just cause a SIGSEGV */
162         if (user_mode(regs)) {
163                 tsk->thread.cp0_badvaddr = address;
164                 tsk->thread.error_code = write;
165 #if 0
166                 printk("do_page_fault() #2: sending SIGSEGV to %s for illegal %s\n"
167                        "%08lx (epc == %08lx, ra == %08lx)\n",
168                        tsk->comm,
169                        write ? "write access to" : "read access from",
170                        address,
171                        (unsigned long) regs->cp0_epc,
172                        (unsigned long) regs->regs[31]);
173 #endif
174                 info.si_signo = SIGSEGV;
175                 info.si_errno = 0;
176                 /* info.si_code has been set above */
177                 info.si_addr = (void *) address;
178                 force_sig_info(SIGSEGV, &info, tsk);
179                 return;
180         }
181
182 no_context:
183         /* Are we prepared to handle this kernel fault?  */
184         fixup = search_exception_tables(exception_epc(regs));
185         if (fixup) {
186                 unsigned long new_epc = fixup->nextinsn;
187
188                 tsk->thread.cp0_baduaddr = address;
189                 if (development_version)
190                         printk(KERN_DEBUG "%s: Exception at [<%lx>] (%lx)\n",
191                                tsk->comm, regs->cp0_epc, new_epc);
192                 regs->cp0_epc = new_epc;
193                 return;
194         }
195
196         /*
197          * Oops. The kernel tried to access some bad page. We'll have to
198          * terminate things with extreme prejudice.
199          */
200
201         bust_spinlocks(1);
202
203         printk(KERN_ALERT "Cpu %d Unable to handle kernel paging request at "
204                "address %016lx, epc == %016lx, ra == %016lx\n",
205                smp_processor_id(), address, regs->cp0_epc, regs->regs[31]);
206         die("Oops", regs);
207
208 /*
209  * We ran out of memory, or some other thing happened to us that made
210  * us unable to handle the page fault gracefully.
211  */
212 out_of_memory:
213         up_read(&mm->mmap_sem);
214         if (tsk->pid == 1) {
215                 yield();
216                 down_read(&mm->mmap_sem);
217                 goto survive;
218         }
219         printk("VM: killing process %s\n", tsk->comm);
220         if (user_mode(regs))
221                 do_exit(SIGKILL);
222         goto no_context;
223
224 do_sigbus:
225         up_read(&mm->mmap_sem);
226
227         /*
228          * Send a sigbus, regardless of whether we were in kernel
229          * or user mode.
230          */
231         tsk->thread.cp0_badvaddr = address;
232         info.si_signo = SIGBUS;
233         info.si_errno = 0;
234         info.si_code = BUS_ADRERR;
235         info.si_addr = (void *) address;
236         force_sig_info(SIGBUS, &info, tsk);
237
238         /* Kernel mode? Handle exceptions or die */
239         if (!user_mode(regs))
240                 goto no_context;
241
242         return;
243
244 vmalloc_fault:
245         panic("Pagefault for kernel virtual memory");
246 }