blob: 162abfbced69c8120c23c0174210beacfaf9e182 [file] [log] [blame]
Jonas Bonn61e85e32011-06-04 11:06:11 +03001/*
2 * OpenRISC fault.c
3 *
4 * Linux architectural port borrowing liberally from similar works of
5 * others. All original copyrights apply as per the original source
6 * declaration.
7 *
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>
11 *
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.
16 */
17
18#include <linux/mm.h>
19#include <linux/interrupt.h>
20#include <linux/module.h>
21#include <linux/sched.h>
22
23#include <asm/uaccess.h>
24#include <asm/siginfo.h>
25#include <asm/signal.h>
26
27#define NUM_TLB_ENTRIES 64
28#define TLB_OFFSET(add) (((add) >> PAGE_SHIFT) & (NUM_TLB_ENTRIES-1))
29
30unsigned long pte_misses; /* updated by do_page_fault() */
31unsigned long pte_errors; /* updated by do_page_fault() */
32
33/* __PHX__ :: - check the vmalloc_fault in do_page_fault()
34 * - also look into include/asm-or32/mmu_context.h
35 */
36volatile pgd_t *current_pgd;
37
38extern void die(char *, struct pt_regs *, long);
39
40/*
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.
44 *
45 * If this routine detects a bad access, it returns 1, otherwise it
46 * returns 0.
47 */
48
49asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long address,
50 unsigned long vector, int write_acc)
51{
52 struct task_struct *tsk;
53 struct mm_struct *mm;
54 struct vm_area_struct *vma;
55 siginfo_t info;
56 int fault;
57
58 tsk = current;
59
60 /*
61 * We fault-in kernel-space virtual memory on-demand. The
62 * 'reference' page table is init_mm.pgd.
63 *
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,
67 * nothing more.
68 *
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.
74 *
75 * This verifies that the fault happens in kernel space
76 * and that the fault was not a protection error.
77 */
78
79 if (address >= VMALLOC_START &&
80 (vector != 0x300 && vector != 0x400) &&
81 !user_mode(regs))
82 goto vmalloc_fault;
83
84 /* If exceptions were enabled, we can reenable them here */
85 if (user_mode(regs)) {
86 /* Exception was in userspace: reenable interrupts */
87 local_irq_enable();
88 } else {
89 /* If exception was in a syscall, then IRQ's may have
90 * been enabled or disabled. If they were enabled,
91 * reenable them.
92 */
93 if (regs->sr && (SPR_SR_IEE | SPR_SR_TEE))
94 local_irq_enable();
95 }
96
97 mm = tsk->mm;
98 info.si_code = SEGV_MAPERR;
99
100 /*
101 * If we're in an interrupt or have no user
102 * context, we must not take the fault..
103 */
104
105 if (in_interrupt() || !mm)
106 goto no_context;
107
108 down_read(&mm->mmap_sem);
109 vma = find_vma(mm, address);
110
111 if (!vma)
112 goto bad_area;
113
114 if (vma->vm_start <= address)
115 goto good_area;
116
117 if (!(vma->vm_flags & VM_GROWSDOWN))
118 goto bad_area;
119
120 if (user_mode(regs)) {
121 /*
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.
126 */
127 if (address + PAGE_SIZE < regs->sp)
128 goto bad_area;
129 }
130 if (expand_stack(vma, address))
131 goto bad_area;
132
133 /*
134 * Ok, we have a good vm_area for this memory access, so
135 * we can handle it..
136 */
137
138good_area:
139 info.si_code = SEGV_ACCERR;
140
141 /* first do some preliminary protection checks */
142
143 if (write_acc) {
144 if (!(vma->vm_flags & VM_WRITE))
145 goto bad_area;
146 } else {
147 /* not present */
148 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
149 goto bad_area;
150 }
151
152 /* are we trying to execute nonexecutable area */
153 if ((vector == 0x400) && !(vma->vm_page_prot.pgprot & _PAGE_EXEC))
154 goto bad_area;
155
156 /*
157 * If for any reason at all we couldn't handle the fault,
158 * make sure we exit gracefully rather than endlessly redo
159 * the fault.
160 */
161
162 fault = handle_mm_fault(mm, vma, address, write_acc);
163 if (unlikely(fault & VM_FAULT_ERROR)) {
164 if (fault & VM_FAULT_OOM)
165 goto out_of_memory;
Linus Torvalds3f7e5102015-01-29 10:51:32 -0800166 else if (fault & VM_FAULT_SIGSEGV)
167 goto bad_area;
Jonas Bonn61e85e32011-06-04 11:06:11 +0300168 else if (fault & VM_FAULT_SIGBUS)
169 goto do_sigbus;
170 BUG();
171 }
172 /*RGD modeled on Cris */
173 if (fault & VM_FAULT_MAJOR)
174 tsk->maj_flt++;
175 else
176 tsk->min_flt++;
177
178 up_read(&mm->mmap_sem);
179 return;
180
181 /*
182 * Something tried to access memory that isn't in our memory map..
183 * Fix it, but check if it's kernel or user first..
184 */
185
186bad_area:
187 up_read(&mm->mmap_sem);
188
189bad_area_nosemaphore:
190
191 /* User mode accesses just cause a SIGSEGV */
192
193 if (user_mode(regs)) {
194 info.si_signo = SIGSEGV;
195 info.si_errno = 0;
196 /* info.si_code has been set above */
197 info.si_addr = (void *)address;
198 force_sig_info(SIGSEGV, &info, tsk);
199 return;
200 }
201
202no_context:
203
204 /* Are we prepared to handle this kernel fault?
205 *
206 * (The kernel has valid exception-points in the source
207 * when it acesses user-memory. When it fails in one
208 * of those points, we find it in a table and do a jump
209 * to some fixup code that loads an appropriate error
210 * code)
211 */
212
213 {
214 const struct exception_table_entry *entry;
215
216 __asm__ __volatile__("l.nop 42");
217
218 if ((entry = search_exception_tables(regs->pc)) != NULL) {
219 /* Adjust the instruction pointer in the stackframe */
220 regs->pc = entry->fixup;
221 return;
222 }
223 }
224
225 /*
226 * Oops. The kernel tried to access some bad page. We'll have to
227 * terminate things with extreme prejudice.
228 */
229
230 if ((unsigned long)(address) < PAGE_SIZE)
231 printk(KERN_ALERT
232 "Unable to handle kernel NULL pointer dereference");
233 else
234 printk(KERN_ALERT "Unable to handle kernel access");
235 printk(" at virtual address 0x%08lx\n", address);
236
237 die("Oops", regs, write_acc);
238
239 do_exit(SIGKILL);
240
241 /*
242 * We ran out of memory, or some other thing happened to us that made
243 * us unable to handle the page fault gracefully.
244 */
245
246out_of_memory:
247 __asm__ __volatile__("l.nop 42");
248 __asm__ __volatile__("l.nop 1");
249
250 up_read(&mm->mmap_sem);
251 printk("VM: killing process %s\n", tsk->comm);
252 if (user_mode(regs))
253 do_exit(SIGKILL);
254 goto no_context;
255
256do_sigbus:
257 up_read(&mm->mmap_sem);
258
259 /*
260 * Send a sigbus, regardless of whether we were in kernel
261 * or user mode.
262 */
263 info.si_signo = SIGBUS;
264 info.si_errno = 0;
265 info.si_code = BUS_ADRERR;
266 info.si_addr = (void *)address;
267 force_sig_info(SIGBUS, &info, tsk);
268
269 /* Kernel mode? Handle exceptions or die */
270 if (!user_mode(regs))
271 goto no_context;
272 return;
273
274vmalloc_fault:
275 {
276 /*
277 * Synchronize this task's top level page-table
278 * with the 'reference' page table.
279 *
280 * Use current_pgd instead of tsk->active_mm->pgd
281 * since the latter might be unavailable if this
282 * code is executed in a misfortunately run irq
283 * (like inside schedule() between switch_mm and
284 * switch_to...).
285 */
286
287 int offset = pgd_index(address);
288 pgd_t *pgd, *pgd_k;
289 pud_t *pud, *pud_k;
290 pmd_t *pmd, *pmd_k;
291 pte_t *pte_k;
292
293/*
294 phx_warn("do_page_fault(): vmalloc_fault will not work, "
295 "since current_pgd assign a proper value somewhere\n"
296 "anyhow we don't need this at the moment\n");
297
298 phx_mmu("vmalloc_fault");
299*/
300 pgd = (pgd_t *)current_pgd + offset;
301 pgd_k = init_mm.pgd + offset;
302
303 /* Since we're two-level, we don't need to do both
304 * set_pgd and set_pmd (they do the same thing). If
305 * we go three-level at some point, do the right thing
306 * with pgd_present and set_pgd here.
307 *
308 * Also, since the vmalloc area is global, we don't
309 * need to copy individual PTE's, it is enough to
310 * copy the pgd pointer into the pte page of the
311 * root task. If that is there, we'll find our pte if
312 * it exists.
313 */
314
315 pud = pud_offset(pgd, address);
316 pud_k = pud_offset(pgd_k, address);
317 if (!pud_present(*pud_k))
318 goto no_context;
319
320 pmd = pmd_offset(pud, address);
321 pmd_k = pmd_offset(pud_k, address);
322
323 if (!pmd_present(*pmd_k))
324 goto bad_area_nosemaphore;
325
326 set_pmd(pmd, *pmd_k);
327
328 /* Make sure the actual PTE exists as well to
329 * catch kernel vmalloc-area accesses to non-mapped
330 * addresses. If we don't do this, this will just
331 * silently loop forever.
332 */
333
334 pte_k = pte_offset_kernel(pmd_k, address);
335 if (!pte_present(*pte_k))
336 goto no_context;
337
338 return;
339 }
340}