x86/efi: Enforce CONFIG_RELOCATABLE for EFI boot stub
[linux/fpc-iii.git] / arch / openrisc / mm / fault.c
blob0703acf7d3276811919fd3d398ada99b1b9c6d50
1 /*
2 * OpenRISC fault.c
4 * Linux architectural port borrowing liberally from similar works of
5 * others. All original copyrights apply as per the original source
6 * declaration.
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.
18 #include <linux/mm.h>
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
43 * routines.
45 * If this routine detects a bad access, it returns 1, otherwise it
46 * returns 0.
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;
53 struct mm_struct *mm;
54 struct vm_area_struct *vma;
55 siginfo_t info;
56 int fault;
57 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
59 tsk = current;
62 * We fault-in kernel-space virtual memory on-demand. The
63 * 'reference' page table is init_mm.pgd.
65 * NOTE! We MUST NOT take any locks for this case. We may
66 * be in an interrupt or a critical region, and should
67 * only copy the information from the master page table,
68 * nothing more.
70 * NOTE2: This is done so that, when updating the vmalloc
71 * mappings we don't have to walk all processes pgdirs and
72 * add the high mappings all at once. Instead we do it as they
73 * are used. However vmalloc'ed page entries have the PAGE_GLOBAL
74 * bit set so sometimes the TLB can use a lingering entry.
76 * This verifies that the fault happens in kernel space
77 * and that the fault was not a protection error.
80 if (address >= VMALLOC_START &&
81 (vector != 0x300 && vector != 0x400) &&
82 !user_mode(regs))
83 goto vmalloc_fault;
85 /* If exceptions were enabled, we can reenable them here */
86 if (user_mode(regs)) {
87 /* Exception was in userspace: reenable interrupts */
88 local_irq_enable();
89 flags |= FAULT_FLAG_USER;
90 } else {
91 /* If exception was in a syscall, then IRQ's may have
92 * been enabled or disabled. If they were enabled,
93 * reenable them.
95 if (regs->sr && (SPR_SR_IEE | SPR_SR_TEE))
96 local_irq_enable();
99 mm = tsk->mm;
100 info.si_code = SEGV_MAPERR;
103 * If we're in an interrupt or have no user
104 * context, we must not take the fault..
107 if (in_interrupt() || !mm)
108 goto no_context;
110 retry:
111 down_read(&mm->mmap_sem);
112 vma = find_vma(mm, address);
114 if (!vma)
115 goto bad_area;
117 if (vma->vm_start <= address)
118 goto good_area;
120 if (!(vma->vm_flags & VM_GROWSDOWN))
121 goto bad_area;
123 if (user_mode(regs)) {
125 * accessing the stack below usp is always a bug.
126 * we get page-aligned addresses so we can only check
127 * if we're within a page from usp, but that might be
128 * enough to catch brutal errors at least.
130 if (address + PAGE_SIZE < regs->sp)
131 goto bad_area;
133 if (expand_stack(vma, address))
134 goto bad_area;
137 * Ok, we have a good vm_area for this memory access, so
138 * we can handle it..
141 good_area:
142 info.si_code = SEGV_ACCERR;
144 /* first do some preliminary protection checks */
146 if (write_acc) {
147 if (!(vma->vm_flags & VM_WRITE))
148 goto bad_area;
149 flags |= FAULT_FLAG_WRITE;
150 } else {
151 /* not present */
152 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
153 goto bad_area;
156 /* are we trying to execute nonexecutable area */
157 if ((vector == 0x400) && !(vma->vm_page_prot.pgprot & _PAGE_EXEC))
158 goto bad_area;
161 * If for any reason at all we couldn't handle the fault,
162 * make sure we exit gracefully rather than endlessly redo
163 * the fault.
166 fault = handle_mm_fault(mm, vma, address, flags);
168 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
169 return;
171 if (unlikely(fault & VM_FAULT_ERROR)) {
172 if (fault & VM_FAULT_OOM)
173 goto out_of_memory;
174 else if (fault & VM_FAULT_SIGBUS)
175 goto do_sigbus;
176 BUG();
179 if (flags & FAULT_FLAG_ALLOW_RETRY) {
180 /*RGD modeled on Cris */
181 if (fault & VM_FAULT_MAJOR)
182 tsk->maj_flt++;
183 else
184 tsk->min_flt++;
185 if (fault & VM_FAULT_RETRY) {
186 flags &= ~FAULT_FLAG_ALLOW_RETRY;
187 flags |= FAULT_FLAG_TRIED;
189 /* No need to up_read(&mm->mmap_sem) as we would
190 * have already released it in __lock_page_or_retry
191 * in mm/filemap.c.
194 goto retry;
198 up_read(&mm->mmap_sem);
199 return;
202 * Something tried to access memory that isn't in our memory map..
203 * Fix it, but check if it's kernel or user first..
206 bad_area:
207 up_read(&mm->mmap_sem);
209 bad_area_nosemaphore:
211 /* User mode accesses just cause a SIGSEGV */
213 if (user_mode(regs)) {
214 info.si_signo = SIGSEGV;
215 info.si_errno = 0;
216 /* info.si_code has been set above */
217 info.si_addr = (void *)address;
218 force_sig_info(SIGSEGV, &info, tsk);
219 return;
222 no_context:
224 /* Are we prepared to handle this kernel fault?
226 * (The kernel has valid exception-points in the source
227 * when it acesses user-memory. When it fails in one
228 * of those points, we find it in a table and do a jump
229 * to some fixup code that loads an appropriate error
230 * code)
234 const struct exception_table_entry *entry;
236 __asm__ __volatile__("l.nop 42");
238 if ((entry = search_exception_tables(regs->pc)) != NULL) {
239 /* Adjust the instruction pointer in the stackframe */
240 regs->pc = entry->fixup;
241 return;
246 * Oops. The kernel tried to access some bad page. We'll have to
247 * terminate things with extreme prejudice.
250 if ((unsigned long)(address) < PAGE_SIZE)
251 printk(KERN_ALERT
252 "Unable to handle kernel NULL pointer dereference");
253 else
254 printk(KERN_ALERT "Unable to handle kernel access");
255 printk(" at virtual address 0x%08lx\n", address);
257 die("Oops", regs, write_acc);
259 do_exit(SIGKILL);
262 * We ran out of memory, or some other thing happened to us that made
263 * us unable to handle the page fault gracefully.
266 out_of_memory:
267 __asm__ __volatile__("l.nop 42");
268 __asm__ __volatile__("l.nop 1");
270 up_read(&mm->mmap_sem);
271 if (!user_mode(regs))
272 goto no_context;
273 pagefault_out_of_memory();
274 return;
276 do_sigbus:
277 up_read(&mm->mmap_sem);
280 * Send a sigbus, regardless of whether we were in kernel
281 * or user mode.
283 info.si_signo = SIGBUS;
284 info.si_errno = 0;
285 info.si_code = BUS_ADRERR;
286 info.si_addr = (void *)address;
287 force_sig_info(SIGBUS, &info, tsk);
289 /* Kernel mode? Handle exceptions or die */
290 if (!user_mode(regs))
291 goto no_context;
292 return;
294 vmalloc_fault:
297 * Synchronize this task's top level page-table
298 * with the 'reference' page table.
300 * Use current_pgd instead of tsk->active_mm->pgd
301 * since the latter might be unavailable if this
302 * code is executed in a misfortunately run irq
303 * (like inside schedule() between switch_mm and
304 * switch_to...).
307 int offset = pgd_index(address);
308 pgd_t *pgd, *pgd_k;
309 pud_t *pud, *pud_k;
310 pmd_t *pmd, *pmd_k;
311 pte_t *pte_k;
314 phx_warn("do_page_fault(): vmalloc_fault will not work, "
315 "since current_pgd assign a proper value somewhere\n"
316 "anyhow we don't need this at the moment\n");
318 phx_mmu("vmalloc_fault");
320 pgd = (pgd_t *)current_pgd + offset;
321 pgd_k = init_mm.pgd + offset;
323 /* Since we're two-level, we don't need to do both
324 * set_pgd and set_pmd (they do the same thing). If
325 * we go three-level at some point, do the right thing
326 * with pgd_present and set_pgd here.
328 * Also, since the vmalloc area is global, we don't
329 * need to copy individual PTE's, it is enough to
330 * copy the pgd pointer into the pte page of the
331 * root task. If that is there, we'll find our pte if
332 * it exists.
335 pud = pud_offset(pgd, address);
336 pud_k = pud_offset(pgd_k, address);
337 if (!pud_present(*pud_k))
338 goto no_context;
340 pmd = pmd_offset(pud, address);
341 pmd_k = pmd_offset(pud_k, address);
343 if (!pmd_present(*pmd_k))
344 goto bad_area_nosemaphore;
346 set_pmd(pmd, *pmd_k);
348 /* Make sure the actual PTE exists as well to
349 * catch kernel vmalloc-area accesses to non-mapped
350 * addresses. If we don't do this, this will just
351 * silently loop forever.
354 pte_k = pte_offset_kernel(pmd_k, address);
355 if (!pte_present(*pte_k))
356 goto no_context;
358 return;