2 * linux/arch/cris/mm/fault.c
4 * Copyright (C) 2000, 2001 Axis Communications AB
9 * Revision 1.1.1.1 2006/04/12 06:24:47 michaelc
10 * Original Broadcom V3.06.02V release
12 * Revision 1.11 2004/05/14 07:58:05 starvik
13 * Merge of changes from 2.4
15 * Revision 1.10 2003/10/27 14:51:24 starvik
18 * Revision 1.9 2003/10/27 14:50:42 starvik
19 * Changed do_page_fault signature
21 * Revision 1.8 2003/07/04 13:02:48 tobiasa
22 * Moved code snippet from arch/cris/mm/fault.c that searches for fixup code
23 * to seperate function in arch-specific files.
25 * Revision 1.7 2003/01/22 06:48:38 starvik
26 * Fixed warnings issued by GCC 3.2.1
28 * Revision 1.6 2003/01/09 14:42:52 starvik
29 * Merge of Linux 2.5.55
31 * Revision 1.5 2002/12/11 14:44:48 starvik
32 * Extracted v10 (ETRAX 100LX) specific stuff to arch/cris/arch-v10/mm
34 * Revision 1.4 2002/11/13 15:10:28 starvik
35 * pte_offset has been renamed to pte_offset_kernel
37 * Revision 1.3 2002/11/05 06:45:13 starvik
38 * Merge of Linux 2.5.45
40 * Revision 1.2 2001/12/18 13:35:22 bjornw
41 * Applied the 2.4.13->2.4.16 CRIS patch to 2.5.1 (is a copy of 2.4.15).
43 * Revision 1.20 2001/11/22 13:34:06 bjornw
44 * * Bug workaround (LX TR89): force a rerun of the whole of an interrupted
45 * unaligned write, because the second half of the write will be corrupted
46 * otherwise. Affected unaligned writes spanning not-yet mapped pages.
47 * * Optimization: use the wr_rd bit in R_MMU_CAUSE to know whether a miss
48 * was due to a read or a write (before we didn't know this until the next
49 * restart of the interrupted instruction, thus wasting one fault-irq)
51 * Revision 1.19 2001/11/12 19:02:10 pkj
52 * Fixed compiler warnings.
54 * Revision 1.18 2001/07/18 22:14:32 bjornw
55 * Enable interrupts in the bulk of do_page_fault
57 * Revision 1.17 2001/07/18 13:07:23 bjornw
58 * * Detect non-existant PTE's in vmalloc pmd synchronization
59 * * Remove comment about fast-paths for VMALLOC_START etc, because all that
60 * was totally bogus anyway it turned out :)
61 * * Fix detection of vmalloc-area synchronization
64 * Revision 1.16 2001/06/13 00:06:08 bjornw
65 * current_pgd should be volatile
67 * Revision 1.15 2001/06/13 00:02:23 bjornw
68 * Use a separate variable to store the current pgd to avoid races in schedule
70 * Revision 1.14 2001/05/16 17:41:07 hp
71 * Last comment tweak further tweaked.
73 * Revision 1.13 2001/05/15 00:58:44 hp
74 * Expand a bit on the comment why we compare address >= TASK_SIZE rather
75 * than >= VMALLOC_START.
77 * Revision 1.12 2001/04/04 10:51:14 bjornw
78 * mmap_sem is grabbed for reading
80 * Revision 1.11 2001/03/23 07:36:07 starvik
81 * Corrected according to review remarks
83 * Revision 1.10 2001/03/21 16:10:11 bjornw
84 * CRIS_FRAME_FIXUP not needed anymore, use FRAME_NORMAL
86 * Revision 1.9 2001/03/05 13:22:20 bjornw
87 * Spell-fix and fix in vmalloc_fault handling
89 * Revision 1.8 2000/11/22 14:45:31 bjornw
90 * * 2.4.0-test10 removed the set_pgdir instantaneous kernel global mapping
91 * into all processes. Instead we fill in the missing PTE entries on demand.
93 * Revision 1.7 2000/11/21 16:39:09 bjornw
94 * fixup switches frametype
96 * Revision 1.6 2000/11/17 16:54:08 bjornw
97 * More detailed siginfo reporting
102 #include <linux/mm.h>
103 #include <linux/interrupt.h>
104 #include <linux/module.h>
105 #include <asm/uaccess.h>
107 extern int find_fixup_code(struct pt_regs *);
108 extern void die_if_kernel(const char *, struct pt_regs *, long);
110 /* debug of low-level TLB reload */
119 /* debug of higher-level faults */
122 /* current active page directory */
124 volatile pgd_t *current_pgd;
127 * This routine handles page faults. It determines the address,
128 * and the problem, and then passes it off to one of the appropriate
131 * Notice that the address we're given is aligned to the page the fault
132 * occurred in, since we only get the PFN in R_MMU_CAUSE not the complete
136 * bit 0 == 0 means no page found, 1 means protection fault
137 * bit 1 == 0 means read, 1 means write
139 * If this routine detects a bad access, it returns 1, otherwise it
144 do_page_fault(unsigned long address, struct pt_regs *regs,
145 int protection, int writeaccess)
147 struct task_struct *tsk;
148 struct mm_struct *mm;
149 struct vm_area_struct * vma;
152 D(printk("Page fault for %X at %X, prot %d write %d\n",
153 address, regs->erp, protection, writeaccess));
158 * We fault-in kernel-space virtual memory on-demand. The
159 * 'reference' page table is init_mm.pgd.
161 * NOTE! We MUST NOT take any locks for this case. We may
162 * be in an interrupt or a critical region, and should
163 * only copy the information from the master page table,
166 * NOTE2: This is done so that, when updating the vmalloc
167 * mappings we don't have to walk all processes pgdirs and
168 * add the high mappings all at once. Instead we do it as they
169 * are used. However vmalloc'ed page entries have the PAGE_GLOBAL
170 * bit set so sometimes the TLB can use a lingering entry.
172 * This verifies that the fault happens in kernel space
173 * and that the fault was not a protection error (error_code & 1).
176 if (address >= VMALLOC_START &&
181 /* we can and should enable interrupts at this point */
185 info.si_code = SEGV_MAPERR;
188 * If we're in an interrupt or have no user
189 * context, we must not take the fault..
192 if (in_interrupt() || !mm)
195 down_read(&mm->mmap_sem);
196 vma = find_vma(mm, address);
199 if (vma->vm_start <= address)
201 if (!(vma->vm_flags & VM_GROWSDOWN))
203 if (user_mode(regs)) {
205 * accessing the stack below usp is always a bug.
206 * we get page-aligned addresses so we can only check
207 * if we're within a page from usp, but that might be
208 * enough to catch brutal errors at least.
210 if (address + PAGE_SIZE < rdusp())
213 if (expand_stack(vma, address))
217 * Ok, we have a good vm_area for this memory access, so
222 info.si_code = SEGV_ACCERR;
224 /* first do some preliminary protection checks */
227 if (!(vma->vm_flags & VM_WRITE))
230 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
235 * If for any reason at all we couldn't handle the fault,
236 * make sure we exit gracefully rather than endlessly redo
240 switch (handle_mm_fault(mm, vma, address, writeaccess)) {
253 up_read(&mm->mmap_sem);
257 * Something tried to access memory that isn't in our memory map..
258 * Fix it, but check if it's kernel or user first..
262 up_read(&mm->mmap_sem);
264 bad_area_nosemaphore:
265 DPG(show_registers(regs));
267 /* User mode accesses just cause a SIGSEGV */
269 if (user_mode(regs)) {
270 info.si_signo = SIGSEGV;
272 /* info.si_code has been set above */
273 info.si_addr = (void *)address;
274 force_sig_info(SIGSEGV, &info, tsk);
280 /* Are we prepared to handle this kernel fault?
282 * (The kernel has valid exception-points in the source
283 * when it acesses user-memory. When it fails in one
284 * of those points, we find it in a table and do a jump
285 * to some fixup code that loads an appropriate error
289 if (find_fixup_code(regs))
293 * Oops. The kernel tried to access some bad page. We'll have to
294 * terminate things with extreme prejudice.
297 if ((unsigned long) (address) < PAGE_SIZE)
298 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
300 printk(KERN_ALERT "Unable to handle kernel access");
301 printk(" at virtual address %08lx\n",address);
303 die_if_kernel("Oops", regs, (writeaccess << 1) | protection);
308 * We ran out of memory, or some other thing happened to us that made
309 * us unable to handle the page fault gracefully.
313 up_read(&mm->mmap_sem);
314 printk("VM: killing process %s\n", tsk->comm);
320 up_read(&mm->mmap_sem);
323 * Send a sigbus, regardless of whether we were in kernel
326 info.si_signo = SIGBUS;
328 info.si_code = BUS_ADRERR;
329 info.si_addr = (void *)address;
330 force_sig_info(SIGBUS, &info, tsk);
332 /* Kernel mode? Handle exceptions or die */
333 if (!user_mode(regs))
340 * Synchronize this task's top level page-table
341 * with the 'reference' page table.
343 * Use current_pgd instead of tsk->active_mm->pgd
344 * since the latter might be unavailable if this
345 * code is executed in a misfortunately run irq
346 * (like inside schedule() between switch_mm and
350 int offset = pgd_index(address);
355 pgd = (pgd_t *)current_pgd + offset;
356 pgd_k = init_mm.pgd + offset;
358 /* Since we're two-level, we don't need to do both
359 * set_pgd and set_pmd (they do the same thing). If
360 * we go three-level at some point, do the right thing
361 * with pgd_present and set_pgd here.
363 * Also, since the vmalloc area is global, we don't
364 * need to copy individual PTE's, it is enough to
365 * copy the pgd pointer into the pte page of the
366 * root task. If that is there, we'll find our pte if
370 pmd = pmd_offset(pgd, address);
371 pmd_k = pmd_offset(pgd_k, address);
373 if (!pmd_present(*pmd_k))
374 goto bad_area_nosemaphore;
376 set_pmd(pmd, *pmd_k);
378 /* Make sure the actual PTE exists as well to
379 * catch kernel vmalloc-area accesses to non-mapped
380 * addresses. If we don't do this, this will just
381 * silently loop forever.
384 pte_k = pte_offset_kernel(pmd_k, address);
385 if (!pte_present(*pte_k))