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 */
8 #include <linux/config.h>
9 #include <linux/signal.h>
10 #include <linux/sched.h>
11 #include <linux/interrupt.h>
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/string.h>
15 #include <linux/types.h>
16 #include <linux/ptrace.h>
17 #include <linux/mman.h>
18 #include <linux/mm.h>
19 #include <linux/smp.h>
20 #include <linux/smp_lock.h>
21 #include <linux/version.h>
22 #include <linux/vt_kern.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
32 #define development_version (LINUX_VERSION_CODE & 0x100)
33
34 /*
35 * Macro for exception fixup code to access integer registers.
36 */
37 #define dpf_reg(r) (regs->regs[r])
38
39 extern spinlock_t timerlist_lock;
40
41 /*
42 * Unlock any spinlocks which will prevent us from getting the
43 * message out (timerlist_lock is acquired through the
44 * console unblank code)
45 */
bust_spinlocks(int yes)46 void bust_spinlocks(int yes)
47 {
48 spin_lock_init(&timerlist_lock);
49 if (yes) {
50 oops_in_progress = 1;
51 #ifdef CONFIG_SMP
52 /* Many serial drivers do __global_cli() */
53 global_irq_lock = SPIN_LOCK_UNLOCKED;
54 #endif
55 } else {
56 int loglevel_save = console_loglevel;
57 #ifdef CONFIG_VT
58 unblank_screen();
59 #endif
60 oops_in_progress = 0;
61 /*
62 * OK, the message is on the console. Now we call printk()
63 * without oops_in_progress set so that printk will give klogd
64 * a poke. Hold onto your hats...
65 */
66 console_loglevel = 15; /* NMI oopser may have shut the console up */
67 printk(" ");
68 console_loglevel = loglevel_save;
69 }
70 }
71
72 /*
73 * This routine handles page faults. It determines the address,
74 * and the problem, and then passes it off to one of the appropriate
75 * routines.
76 */
do_page_fault(struct pt_regs * regs,unsigned long write,unsigned long address)77 asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long write,
78 unsigned long address)
79 {
80 struct vm_area_struct * vma;
81 struct task_struct *tsk = current;
82 struct mm_struct *mm = tsk->mm;
83 unsigned long fixup;
84 siginfo_t info;
85
86 /*
87 * We fault-in kernel-space virtual memory on-demand. The
88 * 'reference' page table is init_mm.pgd.
89 *
90 * NOTE! We MUST NOT take any locks for this case. We may
91 * be in an interrupt or a critical region, and should
92 * only copy the information from the master page table,
93 * nothing more.
94 */
95 if (address >= VMALLOC_START)
96 goto vmalloc_fault;
97
98 info.si_code = SEGV_MAPERR;
99 /*
100 * If we're in an interrupt or have no user
101 * context, we must not take the fault..
102 */
103 if (in_interrupt() || !mm)
104 goto no_context;
105 #if 0
106 printk("[%s:%d:%08lx:%ld:%08lx]\n", current->comm, current->pid,
107 address, write, regs->cp0_epc);
108 #endif
109 down_read(&mm->mmap_sem);
110 vma = find_vma(mm, address);
111 if (!vma)
112 goto bad_area;
113 if (vma->vm_start <= address)
114 goto good_area;
115 if (!(vma->vm_flags & VM_GROWSDOWN))
116 goto bad_area;
117 if (expand_stack(vma, address))
118 goto bad_area;
119 /*
120 * Ok, we have a good vm_area for this memory access, so
121 * we can handle it..
122 */
123 good_area:
124 info.si_code = SEGV_ACCERR;
125
126 if (write) {
127 if (!(vma->vm_flags & VM_WRITE))
128 goto bad_area;
129 } else {
130 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
131 goto bad_area;
132 }
133
134 survive:
135 /*
136 * If for any reason at all we couldn't handle the fault,
137 * make sure we exit gracefully rather than endlessly redo
138 * the fault.
139 */
140 switch (handle_mm_fault(mm, vma, address, write)) {
141 case 1:
142 tsk->min_flt++;
143 break;
144 case 2:
145 tsk->maj_flt++;
146 break;
147 case 0:
148 goto do_sigbus;
149 default:
150 goto out_of_memory;
151 }
152
153 up_read(&mm->mmap_sem);
154 return;
155
156 /*
157 * Something tried to access memory that isn't in our memory map..
158 * Fix it, but check if it's kernel or user first..
159 */
160 bad_area:
161 up_read(&mm->mmap_sem);
162
163 /* User mode accesses just cause a SIGSEGV */
164 if (user_mode(regs)) {
165 tsk->thread.cp0_badvaddr = address;
166 tsk->thread.error_code = write;
167 #if 0
168 printk("do_page_fault() #2: sending SIGSEGV to %s for illegal %s\n"
169 "%08lx (epc == %08lx, ra == %08lx)\n",
170 tsk->comm,
171 write ? "write access to" : "read access from",
172 address,
173 (unsigned long) regs->cp0_epc,
174 (unsigned long) regs->regs[31]);
175 #endif
176 info.si_signo = SIGSEGV;
177 info.si_errno = 0;
178 /* info.si_code has been set above */
179 info.si_addr = (void *) address;
180 force_sig_info(SIGSEGV, &info, tsk);
181 return;
182 }
183
184 no_context:
185 /* Are we prepared to handle this kernel fault? */
186 fixup = search_exception_table(exception_epc(regs));
187 if (fixup) {
188 long new_epc;
189
190 tsk->thread.cp0_baduaddr = address;
191 new_epc = fixup_exception(dpf_reg, fixup, regs->cp0_epc);
192 if (development_version)
193 printk(KERN_DEBUG "%s: Exception at [<%lx>] (%lx)\n",
194 tsk->comm, regs->cp0_epc, new_epc);
195 regs->cp0_epc = new_epc;
196 return;
197 }
198
199 /*
200 * Oops. The kernel tried to access some bad page. We'll have to
201 * terminate things with extreme prejudice.
202 */
203 printk(KERN_ALERT "Unable to handle kernel paging request at virtual "
204 "address %08lx, epc == %08lx, ra == %08lx\n",
205 address, regs->cp0_epc, regs->regs[31]);
206 die("Oops", regs);
207 /* Game over. */
208
209 /*
210 * We ran out of memory, or some other thing happened to us that made
211 * us unable to handle the page fault gracefully.
212 */
213 out_of_memory:
214 if (tsk->pid == 1) {
215 yield();
216 goto survive;
217 }
218 up_read(&mm->mmap_sem);
219 printk(KERN_NOTICE "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 {
246 /*
247 * Synchronize this task's top level page-table
248 * with the 'reference' page table.
249 *
250 * Do _not_ use "tsk" here. We might be inside
251 * an interrupt in the middle of a task switch..
252 */
253 int offset = __pgd_offset(address);
254 pgd_t *pgd, *pgd_k;
255 pmd_t *pmd, *pmd_k;
256 pte_t *pte_k;
257
258 pgd = (pgd_t *) pgd_current[smp_processor_id()] + offset;
259 pgd_k = init_mm.pgd + offset;
260
261 if (!pgd_present(*pgd_k))
262 goto no_context;
263 set_pgd(pgd, *pgd_k);
264
265 pmd = pmd_offset(pgd, address);
266 pmd_k = pmd_offset(pgd_k, address);
267 if (!pmd_present(*pmd_k))
268 goto no_context;
269 set_pmd(pmd, *pmd_k);
270
271 pte_k = pte_offset(pmd_k, address);
272 if (!pte_present(*pte_k))
273 goto no_context;
274 return;
275 }
276 }
277