2 * linux/arch/cris/mm/fault.c
4 * Copyright (C) 2000, 2001 Axis Communications AB
9 * Revision 1.8 2003/07/04 13:02:48 tobiasa
10 * Moved code snippet from arch/cris/mm/fault.c that searches for fixup code
11 * to separate function in arch-specific files.
13 * Revision 1.7 2003/01/22 06:48:38 starvik
14 * Fixed warnings issued by GCC 3.2.1
16 * Revision 1.6 2003/01/09 14:42:52 starvik
17 * Merge of Linux 2.5.55
19 * Revision 1.5 2002/12/11 14:44:48 starvik
20 * Extracted v10 (ETRAX 100LX) specific stuff to arch/cris/arch-v10/mm
22 * Revision 1.4 2002/11/13 15:10:28 starvik
23 * pte_offset has been renamed to pte_offset_kernel
25 * Revision 1.3 2002/11/05 06:45:13 starvik
26 * Merge of Linux 2.5.45
28 * Revision 1.2 2001/12/18 13:35:22 bjornw
29 * Applied the 2.4.13->2.4.16 CRIS patch to 2.5.1 (is a copy of 2.4.15).
31 * Revision 1.20 2001/11/22 13:34:06 bjornw
32 * * Bug workaround (LX TR89): force a rerun of the whole of an interrupted
33 * unaligned write, because the second half of the write will be corrupted
34 * otherwise. Affected unaligned writes spanning not-yet mapped pages.
35 * * Optimization: use the wr_rd bit in R_MMU_CAUSE to know whether a miss
36 * was due to a read or a write (before we didn't know this until the next
37 * restart of the interrupted instruction, thus wasting one fault-irq)
39 * Revision 1.19 2001/11/12 19:02:10 pkj
40 * Fixed compiler warnings.
42 * Revision 1.18 2001/07/18 22:14:32 bjornw
43 * Enable interrupts in the bulk of do_page_fault
45 * Revision 1.17 2001/07/18 13:07:23 bjornw
46 * * Detect non-existant PTE's in vmalloc pmd synchronization
47 * * Remove comment about fast-paths for VMALLOC_START etc, because all that
48 * was totally bogus anyway it turned out :)
49 * * Fix detection of vmalloc-area synchronization
52 * Revision 1.16 2001/06/13 00:06:08 bjornw
53 * current_pgd should be volatile
55 * Revision 1.15 2001/06/13 00:02:23 bjornw
56 * Use a separate variable to store the current pgd to avoid races in schedule
58 * Revision 1.14 2001/05/16 17:41:07 hp
59 * Last comment tweak further tweaked.
61 * Revision 1.13 2001/05/15 00:58:44 hp
62 * Expand a bit on the comment why we compare address >= TASK_SIZE rather
63 * than >= VMALLOC_START.
65 * Revision 1.12 2001/04/04 10:51:14 bjornw
66 * mmap_sem is grabbed for reading
68 * Revision 1.11 2001/03/23 07:36:07 starvik
69 * Corrected according to review remarks
71 * Revision 1.10 2001/03/21 16:10:11 bjornw
72 * CRIS_FRAME_FIXUP not needed anymore, use FRAME_NORMAL
74 * Revision 1.9 2001/03/05 13:22:20 bjornw
75 * Spell-fix and fix in vmalloc_fault handling
77 * Revision 1.8 2000/11/22 14:45:31 bjornw
78 * * 2.4.0-test10 removed the set_pgdir instantaneous kernel global mapping
79 * into all processes. Instead we fill in the missing PTE entries on demand.
81 * Revision 1.7 2000/11/21 16:39:09 bjornw
82 * fixup switches frametype
84 * Revision 1.6 2000/11/17 16:54:08 bjornw
85 * More detailed siginfo reporting
91 #include <linux/interrupt.h>
92 #include <linux/module.h>
93 #include <asm/uaccess.h>
95 extern int find_fixup_code(struct pt_regs *);
96 extern void die_if_kernel(const char *, struct pt_regs *, long);
98 asmlinkage void do_invalid_op (struct pt_regs *, unsigned long);
99 asmlinkage void do_page_fault(unsigned long address, struct pt_regs *regs,
102 /* debug of low-level TLB reload */
111 /* debug of higher-level faults */
114 /* current active page directory */
116 volatile pgd_t *current_pgd;
119 * This routine handles page faults. It determines the address,
120 * and the problem, and then passes it off to one of the appropriate
123 * Notice that the address we're given is aligned to the page the fault
124 * occurred in, since we only get the PFN in R_MMU_CAUSE not the complete
128 * bit 0 == 0 means no page found, 1 means protection fault
129 * bit 1 == 0 means read, 1 means write
131 * If this routine detects a bad access, it returns 1, otherwise it
136 do_page_fault(unsigned long address, struct pt_regs *regs,
139 struct task_struct *tsk;
140 struct mm_struct *mm;
141 struct vm_area_struct * vma;
148 * We fault-in kernel-space virtual memory on-demand. The
149 * 'reference' page table is init_mm.pgd.
151 * NOTE! We MUST NOT take any locks for this case. We may
152 * be in an interrupt or a critical region, and should
153 * only copy the information from the master page table,
156 * NOTE2: This is done so that, when updating the vmalloc
157 * mappings we don't have to walk all processes pgdirs and
158 * add the high mappings all at once. Instead we do it as they
159 * are used. However vmalloc'ed page entries have the PAGE_GLOBAL
160 * bit set so sometimes the TLB can use a lingering entry.
162 * This verifies that the fault happens in kernel space
163 * and that the fault was not a protection error (error_code & 1).
166 if (address >= VMALLOC_START &&
171 /* we can and should enable interrupts at this point */
175 writeaccess = error_code & 2;
176 info.si_code = SEGV_MAPERR;
179 * If we're in an interrupt or have no user
180 * context, we must not take the fault..
183 if (in_interrupt() || !mm)
186 down_read(&mm->mmap_sem);
187 vma = find_vma(mm, address);
190 if (vma->vm_start <= address)
192 if (!(vma->vm_flags & VM_GROWSDOWN))
194 if (user_mode(regs)) {
196 * accessing the stack below usp is always a bug.
197 * we get page-aligned addresses so we can only check
198 * if we're within a page from usp, but that might be
199 * enough to catch brutal errors at least.
201 if (address + PAGE_SIZE < rdusp())
204 if (expand_stack(vma, address))
208 * Ok, we have a good vm_area for this memory access, so
213 info.si_code = SEGV_ACCERR;
215 /* first do some preliminary protection checks */
218 if (!(vma->vm_flags & VM_WRITE))
221 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
226 * If for any reason at all we couldn't handle the fault,
227 * make sure we exit gracefully rather than endlessly redo
231 switch (handle_mm_fault(mm, vma, address, writeaccess)) {
244 up_read(&mm->mmap_sem);
248 * Something tried to access memory that isn't in our memory map..
249 * Fix it, but check if it's kernel or user first..
253 up_read(&mm->mmap_sem);
255 bad_area_nosemaphore:
256 DPG(show_registers(regs));
258 /* User mode accesses just cause a SIGSEGV */
260 if (user_mode(regs)) {
261 info.si_signo = SIGSEGV;
263 /* info.si_code has been set above */
264 info.si_addr = (void *)address;
265 force_sig_info(SIGSEGV, &info, tsk);
271 /* Are we prepared to handle this kernel fault?
273 * (The kernel has valid exception-points in the source
274 * when it acesses user-memory. When it fails in one
275 * of those points, we find it in a table and do a jump
276 * to some fixup code that loads an appropriate error
280 if (find_fixup_code(regs))
284 * Oops. The kernel tried to access some bad page. We'll have to
285 * terminate things with extreme prejudice.
288 if ((unsigned long) (address) < PAGE_SIZE)
289 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
291 printk(KERN_ALERT "Unable to handle kernel access");
292 printk(" at virtual address %08lx\n",address);
294 die_if_kernel("Oops", regs, error_code);
299 * We ran out of memory, or some other thing happened to us that made
300 * us unable to handle the page fault gracefully.
304 up_read(&mm->mmap_sem);
305 printk("VM: killing process %s\n", tsk->comm);
311 up_read(&mm->mmap_sem);
314 * Send a sigbus, regardless of whether we were in kernel
317 info.si_signo = SIGBUS;
319 info.si_code = BUS_ADRERR;
320 info.si_addr = (void *)address;
321 force_sig_info(SIGBUS, &info, tsk);
323 /* Kernel mode? Handle exceptions or die */
324 if (!user_mode(regs))
331 * Synchronize this task's top level page-table
332 * with the 'reference' page table.
334 * Use current_pgd instead of tsk->active_mm->pgd
335 * since the latter might be unavailable if this
336 * code is executed in a misfortunately run irq
337 * (like inside schedule() between switch_mm and
341 int offset = pgd_index(address);
346 pgd = (pgd_t *)current_pgd + offset;
347 pgd_k = init_mm.pgd + offset;
349 /* Since we're two-level, we don't need to do both
350 * set_pgd and set_pmd (they do the same thing). If
351 * we go three-level at some point, do the right thing
352 * with pgd_present and set_pgd here.
354 * Also, since the vmalloc area is global, we don't
355 * need to copy individual PTE's, it is enough to
356 * copy the pgd pointer into the pte page of the
357 * root task. If that is there, we'll find our pte if
361 pmd = pmd_offset(pgd, address);
362 pmd_k = pmd_offset(pgd_k, address);
364 if (!pmd_present(*pmd_k))
365 goto bad_area_nosemaphore;
367 set_pmd(pmd, *pmd_k);
369 /* Make sure the actual PTE exists as well to
370 * catch kernel vmalloc-area accesses to non-mapped
371 * addresses. If we don't do this, this will just
372 * silently loop forever.
375 pte_k = pte_offset_kernel(pmd_k, address);
376 if (!pte_present(*pte_k))