1 /* MN10300 MMU Fault handler
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)
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.
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>
22 #include <linux/smp.h>
23 #include <linux/interrupt.h>
24 #include <linux/init.h>
25 #include <linux/vt_kern.h> /* For unblank_screen() */
27 #include <asm/system.h>
28 #include <asm/uaccess.h>
29 #include <asm/pgalloc.h>
30 #include <asm/hardirq.h>
31 #include <asm/gdb-stub.h>
32 #include <asm/cpu-regs.h>
35 * Unlock any spinlocks which will prevent us from getting the
38 void bust_spinlocks(int yes
)
43 /* Many serial drivers do __global_cli() */
47 int loglevel_save
= console_loglevel
;
53 * OK, the message is on the console. Now we call printk()
54 * without oops_in_progress set so that printk will give klogd
55 * a poke. Hold onto your hats...
57 console_loglevel
= 15; /* NMI oopser may have shut the console
60 console_loglevel
= loglevel_save
;
64 void do_BUG(const char *file
, int line
)
67 printk(KERN_EMERG
"------------[ cut here ]------------\n");
68 printk(KERN_EMERG
"kernel BUG at %s:%d!\n", file
, line
);
72 static void print_pagetable_entries(pgd_t
*pgdir
, unsigned long address
)
78 pgd
= pgdir
+ __pgd_offset(address
);
79 printk(KERN_DEBUG
"pgd entry %p: %016Lx\n",
80 pgd
, (long long) pgd_val(*pgd
));
82 if (!pgd_present(*pgd
)) {
83 printk(KERN_DEBUG
"... pgd not present!\n");
86 pmd
= pmd_offset(pgd
, address
);
87 printk(KERN_DEBUG
"pmd entry %p: %016Lx\n",
88 pmd
, (long long)pmd_val(*pmd
));
90 if (!pmd_present(*pmd
)) {
91 printk(KERN_DEBUG
"... pmd not present!\n");
94 pte
= pte_offset(pmd
, address
);
95 printk(KERN_DEBUG
"pte entry %p: %016Lx\n",
96 pte
, (long long) pte_val(*pte
));
98 if (!pte_present(*pte
))
99 printk(KERN_DEBUG
"... pte not present!\n");
103 asmlinkage
void monitor_signal(struct pt_regs
*);
106 * This routine handles page faults. It determines the address,
107 * and the problem, and then passes it off to one of the appropriate
111 * - LSW: either MMUFCR_IFC or MMUFCR_DFC as appropriate
112 * - MSW: 0 if data access, 1 if instruction access
113 * - bit 0: TLB miss flag
114 * - bit 1: initial write
115 * - bit 2: page invalid
116 * - bit 3: protection violation
117 * - bit 4: accessor (0=user 1=kernel)
118 * - bit 5: 0=read 1=write
119 * - bit 6-8: page protection spec
120 * - bit 9: illegal address
121 * - bit 16: 0=data 1=ins
124 asmlinkage
void do_page_fault(struct pt_regs
*regs
, unsigned long fault_code
,
125 unsigned long address
)
127 struct vm_area_struct
*vma
;
128 struct task_struct
*tsk
;
129 struct mm_struct
*mm
;
134 #ifdef CONFIG_GDBSTUB
135 /* handle GDB stub causing a fault */
137 gdbstub_exception(regs
, TBR
& TBR_INT_CODE
);
143 printk(KERN_DEBUG
"--- do_page_fault(%p,%s:%04lx,%08lx)\n",
145 fault_code
& 0x10000 ? "ins" : "data",
146 fault_code
& 0xffff, address
);
152 * We fault-in kernel-space virtual memory on-demand. The
153 * 'reference' page table is init_mm.pgd.
155 * NOTE! We MUST NOT take any locks for this case. We may
156 * be in an interrupt or a critical region, and should
157 * only copy the information from the master page table,
160 * This verifies that the fault happens in kernel space
161 * and that the fault was a page not present (invalid) error
163 if (address
>= VMALLOC_START
&& address
< VMALLOC_END
&&
164 (fault_code
& MMUFCR_xFC_ACCESS
) == MMUFCR_xFC_ACCESS_SR
&&
165 (fault_code
& MMUFCR_xFC_PGINVAL
) == MMUFCR_xFC_PGINVAL
170 info
.si_code
= SEGV_MAPERR
;
173 * If we're in an interrupt or have no user
174 * context, we must not take the fault..
176 if (in_atomic() || !mm
)
179 down_read(&mm
->mmap_sem
);
181 vma
= find_vma(mm
, address
);
184 if (vma
->vm_start
<= address
)
186 if (!(vma
->vm_flags
& VM_GROWSDOWN
))
189 if ((fault_code
& MMUFCR_xFC_ACCESS
) == MMUFCR_xFC_ACCESS_USR
) {
190 /* accessing the stack below the stack pointer is always a
192 if ((address
& PAGE_MASK
) + 2 * PAGE_SIZE
< regs
->sp
) {
195 "[%d] ### Access below stack @%lx (sp=%lx)\n",
196 current
->pid
, address
, regs
->sp
);
198 "vma [%08x - %08x]\n",
199 vma
->vm_start
, vma
->vm_end
);
200 show_registers(regs
);
202 "[%d] ### Code: [%08lx]"
203 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
206 ((u8
*) regs
->pc
)[0],
207 ((u8
*) regs
->pc
)[1],
208 ((u8
*) regs
->pc
)[2],
209 ((u8
*) regs
->pc
)[3],
210 ((u8
*) regs
->pc
)[4],
211 ((u8
*) regs
->pc
)[5],
212 ((u8
*) regs
->pc
)[6],
220 if (expand_stack(vma
, address
))
224 * Ok, we have a good vm_area for this memory access, so
228 info
.si_code
= SEGV_ACCERR
;
230 switch (fault_code
& (MMUFCR_xFC_PGINVAL
|MMUFCR_xFC_TYPE
)) {
231 default: /* 3: write, present */
232 case MMUFCR_xFC_TYPE_WRITE
:
233 #ifdef TEST_VERIFY_AREA
234 if ((fault_code
& MMUFCR_xFC_ACCESS
) == MMUFCR_xFC_ACCESS_SR
)
235 printk(KERN_DEBUG
"WP fault at %08lx\n", regs
->pc
);
237 /* write to absent page */
238 case MMUFCR_xFC_PGINVAL
| MMUFCR_xFC_TYPE_WRITE
:
239 if (!(vma
->vm_flags
& VM_WRITE
))
244 /* read from protected page */
245 case MMUFCR_xFC_TYPE_READ
:
248 /* read from absent page present */
249 case MMUFCR_xFC_PGINVAL
| MMUFCR_xFC_TYPE_READ
:
250 if (!(vma
->vm_flags
& (VM_READ
| VM_EXEC
)))
256 * If for any reason at all we couldn't handle the fault,
257 * make sure we exit gracefully rather than endlessly redo
260 fault
= handle_mm_fault(mm
, vma
, address
, write
? FAULT_FLAG_WRITE
: 0);
261 if (unlikely(fault
& VM_FAULT_ERROR
)) {
262 if (fault
& VM_FAULT_OOM
)
264 else if (fault
& VM_FAULT_SIGBUS
)
268 if (fault
& VM_FAULT_MAJOR
)
273 up_read(&mm
->mmap_sem
);
277 * Something tried to access memory that isn't in our memory map..
278 * Fix it, but check if it's kernel or user first..
281 up_read(&mm
->mmap_sem
);
282 monitor_signal(regs
);
284 /* User mode accesses just cause a SIGSEGV */
285 if ((fault_code
& MMUFCR_xFC_ACCESS
) == MMUFCR_xFC_ACCESS_USR
) {
286 info
.si_signo
= SIGSEGV
;
288 /* info.si_code has been set above */
289 info
.si_addr
= (void *)address
;
290 force_sig_info(SIGSEGV
, &info
, tsk
);
295 monitor_signal(regs
);
296 /* Are we prepared to handle this kernel fault? */
297 if (fixup_exception(regs
))
301 * Oops. The kernel tried to access some bad page. We'll have to
302 * terminate things with extreme prejudice.
307 if (address
< PAGE_SIZE
)
309 "Unable to handle kernel NULL pointer dereference");
312 "Unable to handle kernel paging request");
313 printk(" at virtual address %08lx\n", address
);
314 printk(" printing pc:\n");
315 printk(KERN_ALERT
"%08lx\n", regs
->pc
);
317 #ifdef CONFIG_GDBSTUB
319 regs
, fault_code
& 0x00010000 ? EXCEP_IAERROR
: EXCEP_DAERROR
);
323 page
= ((unsigned long *) __va(page
))[address
>> 22];
324 printk(KERN_ALERT
"*pde = %08lx\n", page
);
327 address
&= 0x003ff000;
328 page
= ((unsigned long *) __va(page
))[address
>> PAGE_SHIFT
];
329 printk(KERN_ALERT
"*pte = %08lx\n", page
);
332 die("Oops", regs
, fault_code
);
336 * We ran out of memory, or some other thing happened to us that made
337 * us unable to handle the page fault gracefully.
340 up_read(&mm
->mmap_sem
);
341 monitor_signal(regs
);
342 printk(KERN_ALERT
"VM: killing process %s\n", tsk
->comm
);
343 if ((fault_code
& MMUFCR_xFC_ACCESS
) == MMUFCR_xFC_ACCESS_USR
)
348 up_read(&mm
->mmap_sem
);
349 monitor_signal(regs
);
352 * Send a sigbus, regardless of whether we were in kernel
355 info
.si_signo
= SIGBUS
;
357 info
.si_code
= BUS_ADRERR
;
358 info
.si_addr
= (void *)address
;
359 force_sig_info(SIGBUS
, &info
, tsk
);
361 /* Kernel mode? Handle exceptions or die */
362 if ((fault_code
& MMUFCR_xFC_ACCESS
) == MMUFCR_xFC_ACCESS_SR
)
369 * Synchronize this task's top level page-table
370 * with the 'reference' page table.
372 * Do _not_ use "tsk" here. We might be inside
373 * an interrupt in the middle of a task switch..
375 int index
= pgd_index(address
);
381 pgd_k
= init_mm
.pgd
+ index
;
383 if (!pgd_present(*pgd_k
))
386 pud_k
= pud_offset(pgd_k
, address
);
387 if (!pud_present(*pud_k
))
390 pmd_k
= pmd_offset(pud_k
, address
);
391 if (!pmd_present(*pmd_k
))
394 pgd
= (pgd_t
*) PTBR
+ index
;
395 pud
= pud_offset(pgd
, address
);
396 pmd
= pmd_offset(pud
, address
);
397 set_pmd(pmd
, *pmd_k
);
399 pte_k
= pte_offset_kernel(pmd_k
, address
);
400 if (!pte_present(*pte_k
))