4 * Linux architectural port borrowing liberally from similar works of
5 * others. All original copyrights apply as per the original source
8 * Modifications for the OpenRISC architecture:
9 * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
10 * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
19 #include <linux/interrupt.h>
20 #include <linux/module.h>
21 #include <linux/sched.h>
23 #include <asm/uaccess.h>
24 #include <asm/siginfo.h>
25 #include <asm/signal.h>
27 #define NUM_TLB_ENTRIES 64
28 #define TLB_OFFSET(add) (((add) >> PAGE_SHIFT) & (NUM_TLB_ENTRIES-1))
30 unsigned long pte_misses
; /* updated by do_page_fault() */
31 unsigned long pte_errors
; /* updated by do_page_fault() */
33 /* __PHX__ :: - check the vmalloc_fault in do_page_fault()
34 * - also look into include/asm-or32/mmu_context.h
36 volatile pgd_t
*current_pgd
;
38 extern void die(char *, struct pt_regs
*, long);
41 * This routine handles page faults. It determines the address,
42 * and the problem, and then passes it off to one of the appropriate
45 * If this routine detects a bad access, it returns 1, otherwise it
49 asmlinkage
void do_page_fault(struct pt_regs
*regs
, unsigned long address
,
50 unsigned long vector
, int write_acc
)
52 struct task_struct
*tsk
;
54 struct vm_area_struct
*vma
;
61 * We fault-in kernel-space virtual memory on-demand. The
62 * 'reference' page table is init_mm.pgd.
64 * NOTE! We MUST NOT take any locks for this case. We may
65 * be in an interrupt or a critical region, and should
66 * only copy the information from the master page table,
69 * NOTE2: This is done so that, when updating the vmalloc
70 * mappings we don't have to walk all processes pgdirs and
71 * add the high mappings all at once. Instead we do it as they
72 * are used. However vmalloc'ed page entries have the PAGE_GLOBAL
73 * bit set so sometimes the TLB can use a lingering entry.
75 * This verifies that the fault happens in kernel space
76 * and that the fault was not a protection error.
79 if (address
>= VMALLOC_START
&&
80 (vector
!= 0x300 && vector
!= 0x400) &&
84 /* If exceptions were enabled, we can reenable them here */
85 if (user_mode(regs
)) {
86 /* Exception was in userspace: reenable interrupts */
89 /* If exception was in a syscall, then IRQ's may have
90 * been enabled or disabled. If they were enabled,
93 if (regs
->sr
&& (SPR_SR_IEE
| SPR_SR_TEE
))
98 info
.si_code
= SEGV_MAPERR
;
101 * If we're in an interrupt or have no user
102 * context, we must not take the fault..
105 if (in_interrupt() || !mm
)
108 down_read(&mm
->mmap_sem
);
109 vma
= find_vma(mm
, address
);
114 if (vma
->vm_start
<= address
)
117 if (!(vma
->vm_flags
& VM_GROWSDOWN
))
120 if (user_mode(regs
)) {
122 * accessing the stack below usp is always a bug.
123 * we get page-aligned addresses so we can only check
124 * if we're within a page from usp, but that might be
125 * enough to catch brutal errors at least.
127 if (address
+ PAGE_SIZE
< regs
->sp
)
130 if (expand_stack(vma
, address
))
134 * Ok, we have a good vm_area for this memory access, so
139 info
.si_code
= SEGV_ACCERR
;
141 /* first do some preliminary protection checks */
144 if (!(vma
->vm_flags
& VM_WRITE
))
148 if (!(vma
->vm_flags
& (VM_READ
| VM_EXEC
)))
152 /* are we trying to execute nonexecutable area */
153 if ((vector
== 0x400) && !(vma
->vm_page_prot
.pgprot
& _PAGE_EXEC
))
157 * If for any reason at all we couldn't handle the fault,
158 * make sure we exit gracefully rather than endlessly redo
162 fault
= handle_mm_fault(mm
, vma
, address
, write_acc
);
163 if (unlikely(fault
& VM_FAULT_ERROR
)) {
164 if (fault
& VM_FAULT_OOM
)
166 else if (fault
& VM_FAULT_SIGBUS
)
170 /*RGD modeled on Cris */
171 if (fault
& VM_FAULT_MAJOR
)
176 up_read(&mm
->mmap_sem
);
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..
185 up_read(&mm
->mmap_sem
);
187 bad_area_nosemaphore
:
189 /* User mode accesses just cause a SIGSEGV */
191 if (user_mode(regs
)) {
192 info
.si_signo
= SIGSEGV
;
194 /* info.si_code has been set above */
195 info
.si_addr
= (void *)address
;
196 force_sig_info(SIGSEGV
, &info
, tsk
);
202 /* Are we prepared to handle this kernel fault?
204 * (The kernel has valid exception-points in the source
205 * when it acesses user-memory. When it fails in one
206 * of those points, we find it in a table and do a jump
207 * to some fixup code that loads an appropriate error
212 const struct exception_table_entry
*entry
;
214 __asm__
__volatile__("l.nop 42");
216 if ((entry
= search_exception_tables(regs
->pc
)) != NULL
) {
217 /* Adjust the instruction pointer in the stackframe */
218 regs
->pc
= entry
->fixup
;
224 * Oops. The kernel tried to access some bad page. We'll have to
225 * terminate things with extreme prejudice.
228 if ((unsigned long)(address
) < PAGE_SIZE
)
230 "Unable to handle kernel NULL pointer dereference");
232 printk(KERN_ALERT
"Unable to handle kernel access");
233 printk(" at virtual address 0x%08lx\n", address
);
235 die("Oops", regs
, write_acc
);
240 * We ran out of memory, or some other thing happened to us that made
241 * us unable to handle the page fault gracefully.
245 __asm__
__volatile__("l.nop 42");
246 __asm__
__volatile__("l.nop 1");
248 up_read(&mm
->mmap_sem
);
249 printk("VM: killing process %s\n", tsk
->comm
);
255 up_read(&mm
->mmap_sem
);
258 * Send a sigbus, regardless of whether we were in kernel
261 info
.si_signo
= SIGBUS
;
263 info
.si_code
= BUS_ADRERR
;
264 info
.si_addr
= (void *)address
;
265 force_sig_info(SIGBUS
, &info
, tsk
);
267 /* Kernel mode? Handle exceptions or die */
268 if (!user_mode(regs
))
275 * Synchronize this task's top level page-table
276 * with the 'reference' page table.
278 * Use current_pgd instead of tsk->active_mm->pgd
279 * since the latter might be unavailable if this
280 * code is executed in a misfortunately run irq
281 * (like inside schedule() between switch_mm and
285 int offset
= pgd_index(address
);
292 phx_warn("do_page_fault(): vmalloc_fault will not work, "
293 "since current_pgd assign a proper value somewhere\n"
294 "anyhow we don't need this at the moment\n");
296 phx_mmu("vmalloc_fault");
298 pgd
= (pgd_t
*)current_pgd
+ offset
;
299 pgd_k
= init_mm
.pgd
+ offset
;
301 /* Since we're two-level, we don't need to do both
302 * set_pgd and set_pmd (they do the same thing). If
303 * we go three-level at some point, do the right thing
304 * with pgd_present and set_pgd here.
306 * Also, since the vmalloc area is global, we don't
307 * need to copy individual PTE's, it is enough to
308 * copy the pgd pointer into the pte page of the
309 * root task. If that is there, we'll find our pte if
313 pud
= pud_offset(pgd
, address
);
314 pud_k
= pud_offset(pgd_k
, address
);
315 if (!pud_present(*pud_k
))
318 pmd
= pmd_offset(pud
, address
);
319 pmd_k
= pmd_offset(pud_k
, address
);
321 if (!pmd_present(*pmd_k
))
322 goto bad_area_nosemaphore
;
324 set_pmd(pmd
, *pmd_k
);
326 /* Make sure the actual PTE exists as well to
327 * catch kernel vmalloc-area accesses to non-mapped
328 * addresses. If we don't do this, this will just
329 * silently loop forever.
332 pte_k
= pte_offset_kernel(pmd_k
, address
);
333 if (!pte_present(*pte_k
))