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
24 #include <asm/branch.h>
25 #include <asm/hardirq.h>
26 #include <asm/pgalloc.h>
27 #include <asm/mmu_context.h>
28 #include <asm/softirq.h>
29 #include <asm/system.h>
30 #include <asm/uaccess.h>
31 #include <asm/ptrace.h>
32
33 #define development_version (LINUX_VERSION_CODE & 0x100)
34
35 /*
36 * Macro for exception fixup code to access integer registers.
37 */
38 #define dpf_reg(r) (regs->regs[r])
39
dodebug(abi64_no_regargs,struct pt_regs regs)40 asmlinkage void dodebug(abi64_no_regargs, struct pt_regs regs)
41 {
42 printk(KERN_DEBUG "Got syscall %ld, cpu %d proc %s:%d epc 0x%lx\n",
43 regs.regs[2], smp_processor_id(), current->comm, current->pid,
44 regs.cp0_epc);
45 }
46
dodebug2(abi64_no_regargs,struct pt_regs regs)47 asmlinkage void dodebug2(abi64_no_regargs, struct pt_regs regs)
48 {
49 unsigned long retaddr;
50
51 __asm__ __volatile__(
52 ".set noreorder\n\t"
53 "add %0,$0,$31\n\t"
54 ".set reorder"
55 : "=r" (retaddr));
56 printk(KERN_DEBUG "Got exception 0x%lx at 0x%lx\n", retaddr,
57 regs.cp0_epc);
58 }
59
60 extern spinlock_t timerlist_lock;
61
62 /*
63 * Unlock any spinlocks which will prevent us from getting the
64 * message out (timerlist_lock is acquired through the
65 * console unblank code)
66 */
bust_spinlocks(int yes)67 void bust_spinlocks(int yes)
68 {
69 spin_lock_init(&timerlist_lock);
70 if (yes) {
71 oops_in_progress = 1;
72 #ifdef CONFIG_SMP
73 /* Many serial drivers do __global_cli() */
74 global_irq_lock = SPIN_LOCK_UNLOCKED;
75 #endif
76 } else {
77 int loglevel_save = console_loglevel;
78 #ifdef CONFIG_VT
79 unblank_screen();
80 #endif
81 oops_in_progress = 0;
82 /*
83 * OK, the message is on the console. Now we call printk()
84 * without oops_in_progress set so that printk will give klogd
85 * a poke. Hold onto your hats...
86 */
87 console_loglevel = 15; /* NMI oopser may have shut the console up */
88 printk(" ");
89 console_loglevel = loglevel_save;
90 }
91 }
92
93 /*
94 * This routine handles page faults. It determines the address,
95 * and the problem, and then passes it off to one of the appropriate
96 * routines.
97 */
do_page_fault(struct pt_regs * regs,unsigned long write,unsigned long address)98 asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long write,
99 unsigned long address)
100 {
101 struct vm_area_struct * vma;
102 struct task_struct *tsk = current;
103 struct mm_struct *mm = tsk->mm;
104 unsigned long fixup;
105 siginfo_t info;
106
107 #if 0
108 printk("Cpu%d[%s:%d:%08lx:%ld:%08lx]\n", smp_processor_id(),
109 current->comm, current->pid, address, write, regs->cp0_epc);
110 #endif
111
112 /*
113 * We fault-in kernel-space virtual memory on-demand. The
114 * 'reference' page table is init_mm.pgd.
115 *
116 * NOTE! We MUST NOT take any locks for this case. We may
117 * be in an interrupt or a critical region, and should
118 * only copy the information from the master page table,
119 * nothing more.
120 */
121 if (address >= VMALLOC_START)
122 goto vmalloc_fault;
123
124 info.si_code = SEGV_MAPERR;
125 /*
126 * If we're in an interrupt or have no user
127 * context, we must not take the fault..
128 */
129 if (in_interrupt() || !mm)
130 goto no_context;
131
132 down_read(&mm->mmap_sem);
133 vma = find_vma(mm, address);
134 if (!vma)
135 goto bad_area;
136 if (vma->vm_start <= address)
137 goto good_area;
138 if (!(vma->vm_flags & VM_GROWSDOWN))
139 goto bad_area;
140 if (expand_stack(vma, address))
141 goto bad_area;
142 /*
143 * Ok, we have a good vm_area for this memory access, so
144 * we can handle it..
145 */
146 good_area:
147 info.si_code = SEGV_ACCERR;
148
149 if (write) {
150 if (!(vma->vm_flags & VM_WRITE))
151 goto bad_area;
152 } else {
153 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
154 goto bad_area;
155 }
156
157 survive:
158 /*
159 * If for any reason at all we couldn't handle the fault,
160 * make sure we exit gracefully rather than endlessly redo
161 * the fault.
162 */
163 switch (handle_mm_fault(mm, vma, address, write)) {
164 case 1:
165 tsk->min_flt++;
166 break;
167 case 2:
168 tsk->maj_flt++;
169 break;
170 case 0:
171 goto do_sigbus;
172 default:
173 goto out_of_memory;
174 }
175
176 up_read(&mm->mmap_sem);
177 return;
178
179 /*
180 * Something tried to access memory that isn't in our memory map..
181 * Fix it, but check if it's kernel or user first..
182 */
183 bad_area:
184 up_read(&mm->mmap_sem);
185
186 if (user_mode(regs)) {
187 tsk->thread.cp0_badvaddr = address;
188 tsk->thread.error_code = write;
189 #if 0
190 printk("do_page_fault() #2: sending SIGSEGV to %s for illegal %s\n"
191 "%08lx (epc == %08lx, ra == %08lx)\n",
192 tsk->comm,
193 write ? "write access to" : "read access from",
194 address,
195 (unsigned long) regs->cp0_epc,
196 (unsigned long) regs->regs[31]);
197 #endif
198 info.si_signo = SIGSEGV;
199 info.si_errno = 0;
200 /* info.si_code has been set above */
201 info.si_addr = (void *) address;
202 force_sig_info(SIGSEGV, &info, tsk);
203 return;
204 }
205
206 no_context:
207 /* Are we prepared to handle this kernel fault? */
208 fixup = search_exception_table(exception_epc(regs));
209 if (fixup) {
210 long new_epc;
211
212 tsk->thread.cp0_baduaddr = address;
213 new_epc = fixup_exception(dpf_reg, fixup, regs->cp0_epc);
214 if (development_version)
215 printk(KERN_DEBUG "%s: Exception at [<%lx>] (%lx)\n",
216 tsk->comm, regs->cp0_epc, new_epc);
217 regs->cp0_epc = new_epc;
218 return;
219 }
220
221 /*
222 * Oops. The kernel tried to access some bad page. We'll have to
223 * terminate things with extreme prejudice.
224 */
225
226 bust_spinlocks(1);
227
228 printk(KERN_ALERT "Cpu %d Unable to handle kernel paging request at "
229 "address %016lx, epc == %016lx, ra == %016lx\n",
230 smp_processor_id(), address, regs->cp0_epc, regs->regs[31]);
231 die("Oops", regs);
232
233 /*
234 * We ran out of memory, or some other thing happened to us that made
235 * us unable to handle the page fault gracefully.
236 */
237 out_of_memory:
238 if (tsk->pid == 1) {
239 yield();
240 goto survive;
241 }
242 up_read(&mm->mmap_sem);
243 printk(KERN_NOTICE "VM: killing process %s\n", tsk->comm);
244 if (user_mode(regs))
245 do_exit(SIGKILL);
246 goto no_context;
247
248 do_sigbus:
249 up_read(&mm->mmap_sem);
250
251 /*
252 * Send a sigbus, regardless of whether we were in kernel
253 * or user mode.
254 */
255 tsk->thread.cp0_badvaddr = address;
256 info.si_signo = SIGBUS;
257 info.si_errno = 0;
258 info.si_code = BUS_ADRERR;
259 info.si_addr = (void *) address;
260 force_sig_info(SIGBUS, &info, tsk);
261
262 /* Kernel mode? Handle exceptions or die */
263 if (!user_mode(regs))
264 goto no_context;
265
266 return;
267
268 vmalloc_fault:
269 die("Pagefault for kernel virtual memory", regs);
270 }
271