Merge branch 'stable' of git://git.kernel.org/pub/scm/linux/kernel/git/cmetcalf/linux...
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / mn10300 / mm / fault.c
1 /* MN10300 MMU Fault handler
2 *
3 * Copyright (C) 2007 Matsushita Electric Industrial Co., Ltd.
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5 * Modified by David Howells (dhowells@redhat.com)
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public Licence
9 * as published by the Free Software Foundation; either version
10 * 2 of the Licence, or (at your option) any later version.
11 */
12
13 #include <linux/signal.h>
14 #include <linux/sched.h>
15 #include <linux/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/string.h>
18 #include <linux/types.h>
19 #include <linux/ptrace.h>
20 #include <linux/mman.h>
21 #include <linux/mm.h>
22 #include <linux/smp.h>
23 #include <linux/interrupt.h>
24 #include <linux/init.h>
25 #include <linux/vt_kern.h> /* For unblank_screen() */
26
27 #include <asm/uaccess.h>
28 #include <asm/pgalloc.h>
29 #include <asm/hardirq.h>
30 #include <asm/cpu-regs.h>
31 #include <asm/debugger.h>
32 #include <asm/gdb-stub.h>
33
34 /*
35 * Unlock any spinlocks which will prevent us from getting the
36 * message out
37 */
38 void bust_spinlocks(int yes)
39 {
40 if (yes) {
41 oops_in_progress = 1;
42 } else {
43 int loglevel_save = console_loglevel;
44 #ifdef CONFIG_VT
45 unblank_screen();
46 #endif
47 oops_in_progress = 0;
48 /*
49 * OK, the message is on the console. Now we call printk()
50 * without oops_in_progress set so that printk will give klogd
51 * a poke. Hold onto your hats...
52 */
53 console_loglevel = 15; /* NMI oopser may have shut the console
54 * up */
55 printk(" ");
56 console_loglevel = loglevel_save;
57 }
58 }
59
60 void do_BUG(const char *file, int line)
61 {
62 bust_spinlocks(1);
63 printk(KERN_EMERG "------------[ cut here ]------------\n");
64 printk(KERN_EMERG "kernel BUG at %s:%d!\n", file, line);
65 }
66
67 #if 0
68 static void print_pagetable_entries(pgd_t *pgdir, unsigned long address)
69 {
70 pgd_t *pgd;
71 pmd_t *pmd;
72 pte_t *pte;
73
74 pgd = pgdir + __pgd_offset(address);
75 printk(KERN_DEBUG "pgd entry %p: %016Lx\n",
76 pgd, (long long) pgd_val(*pgd));
77
78 if (!pgd_present(*pgd)) {
79 printk(KERN_DEBUG "... pgd not present!\n");
80 return;
81 }
82 pmd = pmd_offset(pgd, address);
83 printk(KERN_DEBUG "pmd entry %p: %016Lx\n",
84 pmd, (long long)pmd_val(*pmd));
85
86 if (!pmd_present(*pmd)) {
87 printk(KERN_DEBUG "... pmd not present!\n");
88 return;
89 }
90 pte = pte_offset(pmd, address);
91 printk(KERN_DEBUG "pte entry %p: %016Lx\n",
92 pte, (long long) pte_val(*pte));
93
94 if (!pte_present(*pte))
95 printk(KERN_DEBUG "... pte not present!\n");
96 }
97 #endif
98
99 /*
100 * This routine handles page faults. It determines the address,
101 * and the problem, and then passes it off to one of the appropriate
102 * routines.
103 *
104 * fault_code:
105 * - LSW: either MMUFCR_IFC or MMUFCR_DFC as appropriate
106 * - MSW: 0 if data access, 1 if instruction access
107 * - bit 0: TLB miss flag
108 * - bit 1: initial write
109 * - bit 2: page invalid
110 * - bit 3: protection violation
111 * - bit 4: accessor (0=user 1=kernel)
112 * - bit 5: 0=read 1=write
113 * - bit 6-8: page protection spec
114 * - bit 9: illegal address
115 * - bit 16: 0=data 1=ins
116 *
117 */
118 asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long fault_code,
119 unsigned long address)
120 {
121 struct vm_area_struct *vma;
122 struct task_struct *tsk;
123 struct mm_struct *mm;
124 unsigned long page;
125 siginfo_t info;
126 int fault;
127 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
128
129 #ifdef CONFIG_GDBSTUB
130 /* handle GDB stub causing a fault */
131 if (gdbstub_busy) {
132 gdbstub_exception(regs, TBR & TBR_INT_CODE);
133 return;
134 }
135 #endif
136
137 #if 0
138 printk(KERN_DEBUG "--- do_page_fault(%p,%s:%04lx,%08lx)\n",
139 regs,
140 fault_code & 0x10000 ? "ins" : "data",
141 fault_code & 0xffff, address);
142 #endif
143
144 tsk = current;
145
146 /*
147 * We fault-in kernel-space virtual memory on-demand. The
148 * 'reference' page table is init_mm.pgd.
149 *
150 * NOTE! We MUST NOT take any locks for this case. We may
151 * be in an interrupt or a critical region, and should
152 * only copy the information from the master page table,
153 * nothing more.
154 *
155 * This verifies that the fault happens in kernel space
156 * and that the fault was a page not present (invalid) error
157 */
158 if (address >= VMALLOC_START && address < VMALLOC_END &&
159 (fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_SR &&
160 (fault_code & MMUFCR_xFC_PGINVAL) == MMUFCR_xFC_PGINVAL
161 )
162 goto vmalloc_fault;
163
164 mm = tsk->mm;
165 info.si_code = SEGV_MAPERR;
166
167 /*
168 * If we're in an interrupt or have no user
169 * context, we must not take the fault..
170 */
171 if (in_atomic() || !mm)
172 goto no_context;
173
174 retry:
175 down_read(&mm->mmap_sem);
176
177 vma = find_vma(mm, address);
178 if (!vma)
179 goto bad_area;
180 if (vma->vm_start <= address)
181 goto good_area;
182 if (!(vma->vm_flags & VM_GROWSDOWN))
183 goto bad_area;
184
185 if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_USR) {
186 /* accessing the stack below the stack pointer is always a
187 * bug */
188 if ((address & PAGE_MASK) + 2 * PAGE_SIZE < regs->sp) {
189 #if 0
190 printk(KERN_WARNING
191 "[%d] ### Access below stack @%lx (sp=%lx)\n",
192 current->pid, address, regs->sp);
193 printk(KERN_WARNING
194 "vma [%08x - %08x]\n",
195 vma->vm_start, vma->vm_end);
196 show_registers(regs);
197 printk(KERN_WARNING
198 "[%d] ### Code: [%08lx]"
199 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
200 current->pid,
201 regs->pc,
202 ((u8 *) regs->pc)[0],
203 ((u8 *) regs->pc)[1],
204 ((u8 *) regs->pc)[2],
205 ((u8 *) regs->pc)[3],
206 ((u8 *) regs->pc)[4],
207 ((u8 *) regs->pc)[5],
208 ((u8 *) regs->pc)[6],
209 ((u8 *) regs->pc)[7]
210 );
211 #endif
212 goto bad_area;
213 }
214 }
215
216 if (expand_stack(vma, address))
217 goto bad_area;
218
219 /*
220 * Ok, we have a good vm_area for this memory access, so
221 * we can handle it..
222 */
223 good_area:
224 info.si_code = SEGV_ACCERR;
225 switch (fault_code & (MMUFCR_xFC_PGINVAL|MMUFCR_xFC_TYPE)) {
226 default: /* 3: write, present */
227 case MMUFCR_xFC_TYPE_WRITE:
228 #ifdef TEST_VERIFY_AREA
229 if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_SR)
230 printk(KERN_DEBUG "WP fault at %08lx\n", regs->pc);
231 #endif
232 /* write to absent page */
233 case MMUFCR_xFC_PGINVAL | MMUFCR_xFC_TYPE_WRITE:
234 if (!(vma->vm_flags & VM_WRITE))
235 goto bad_area;
236 flags |= FAULT_FLAG_WRITE;
237 break;
238
239 /* read from protected page */
240 case MMUFCR_xFC_TYPE_READ:
241 goto bad_area;
242
243 /* read from absent page present */
244 case MMUFCR_xFC_PGINVAL | MMUFCR_xFC_TYPE_READ:
245 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
246 goto bad_area;
247 break;
248 }
249
250 /*
251 * If for any reason at all we couldn't handle the fault,
252 * make sure we exit gracefully rather than endlessly redo
253 * the fault.
254 */
255 fault = handle_mm_fault(mm, vma, address, flags);
256
257 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
258 return;
259
260 if (unlikely(fault & VM_FAULT_ERROR)) {
261 if (fault & VM_FAULT_OOM)
262 goto out_of_memory;
263 else if (fault & VM_FAULT_SIGBUS)
264 goto do_sigbus;
265 BUG();
266 }
267 if (flags & FAULT_FLAG_ALLOW_RETRY) {
268 if (fault & VM_FAULT_MAJOR)
269 current->maj_flt++;
270 else
271 current->min_flt++;
272 if (fault & VM_FAULT_RETRY) {
273 flags &= ~FAULT_FLAG_ALLOW_RETRY;
274
275 /* No need to up_read(&mm->mmap_sem) as we would
276 * have already released it in __lock_page_or_retry
277 * in mm/filemap.c.
278 */
279
280 goto retry;
281 }
282 }
283
284 up_read(&mm->mmap_sem);
285 return;
286
287 /*
288 * Something tried to access memory that isn't in our memory map..
289 * Fix it, but check if it's kernel or user first..
290 */
291 bad_area:
292 up_read(&mm->mmap_sem);
293
294 /* User mode accesses just cause a SIGSEGV */
295 if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_USR) {
296 info.si_signo = SIGSEGV;
297 info.si_errno = 0;
298 /* info.si_code has been set above */
299 info.si_addr = (void *)address;
300 force_sig_info(SIGSEGV, &info, tsk);
301 return;
302 }
303
304 no_context:
305 /* Are we prepared to handle this kernel fault? */
306 if (fixup_exception(regs))
307 return;
308
309 /*
310 * Oops. The kernel tried to access some bad page. We'll have to
311 * terminate things with extreme prejudice.
312 */
313
314 bust_spinlocks(1);
315
316 if (address < PAGE_SIZE)
317 printk(KERN_ALERT
318 "Unable to handle kernel NULL pointer dereference");
319 else
320 printk(KERN_ALERT
321 "Unable to handle kernel paging request");
322 printk(" at virtual address %08lx\n", address);
323 printk(" printing pc:\n");
324 printk(KERN_ALERT "%08lx\n", regs->pc);
325
326 debugger_intercept(fault_code & 0x00010000 ? EXCEP_IAERROR : EXCEP_DAERROR,
327 SIGSEGV, SEGV_ACCERR, regs);
328
329 page = PTBR;
330 page = ((unsigned long *) __va(page))[address >> 22];
331 printk(KERN_ALERT "*pde = %08lx\n", page);
332 if (page & 1) {
333 page &= PAGE_MASK;
334 address &= 0x003ff000;
335 page = ((unsigned long *) __va(page))[address >> PAGE_SHIFT];
336 printk(KERN_ALERT "*pte = %08lx\n", page);
337 }
338
339 die("Oops", regs, fault_code);
340 do_exit(SIGKILL);
341
342 /*
343 * We ran out of memory, or some other thing happened to us that made
344 * us unable to handle the page fault gracefully.
345 */
346 out_of_memory:
347 up_read(&mm->mmap_sem);
348 printk(KERN_ALERT "VM: killing process %s\n", tsk->comm);
349 if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_USR)
350 do_exit(SIGKILL);
351 goto no_context;
352
353 do_sigbus:
354 up_read(&mm->mmap_sem);
355
356 /*
357 * Send a sigbus, regardless of whether we were in kernel
358 * or user mode.
359 */
360 info.si_signo = SIGBUS;
361 info.si_errno = 0;
362 info.si_code = BUS_ADRERR;
363 info.si_addr = (void *)address;
364 force_sig_info(SIGBUS, &info, tsk);
365
366 /* Kernel mode? Handle exceptions or die */
367 if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_SR)
368 goto no_context;
369 return;
370
371 vmalloc_fault:
372 {
373 /*
374 * Synchronize this task's top level page-table
375 * with the 'reference' page table.
376 *
377 * Do _not_ use "tsk" here. We might be inside
378 * an interrupt in the middle of a task switch..
379 */
380 int index = pgd_index(address);
381 pgd_t *pgd, *pgd_k;
382 pud_t *pud, *pud_k;
383 pmd_t *pmd, *pmd_k;
384 pte_t *pte_k;
385
386 pgd_k = init_mm.pgd + index;
387
388 if (!pgd_present(*pgd_k))
389 goto no_context;
390
391 pud_k = pud_offset(pgd_k, address);
392 if (!pud_present(*pud_k))
393 goto no_context;
394
395 pmd_k = pmd_offset(pud_k, address);
396 if (!pmd_present(*pmd_k))
397 goto no_context;
398
399 pgd = (pgd_t *) PTBR + index;
400 pud = pud_offset(pgd, address);
401 pmd = pmd_offset(pud, address);
402 set_pmd(pmd, *pmd_k);
403
404 pte_k = pte_offset_kernel(pmd_k, address);
405 if (!pte_present(*pte_k))
406 goto no_context;
407 return;
408 }
409 }