[PATCH] 64bit bugs in s2io
[hh.org.git] / arch / cris / mm / fault.c
blob934c51078ccee4bafca2b68b7752b1745a63ee03
1 /*
2 * linux/arch/cris/mm/fault.c
4 * Copyright (C) 2000, 2001 Axis Communications AB
6 * Authors: Bjorn Wesen
7 *
8 * $Log: fault.c,v $
9 * Revision 1.20 2005/03/04 08:16:18 starvik
10 * Merge of Linux 2.6.11.
12 * Revision 1.19 2005/01/14 10:07:59 starvik
13 * Fixed warning.
15 * Revision 1.18 2005/01/12 08:10:14 starvik
16 * Readded the change of frametype when handling kernel page fault fixup
17 * for v10. This is necessary to avoid that the CPU remakes the faulting
18 * access.
20 * Revision 1.17 2005/01/11 13:53:05 starvik
21 * Use raw_printk.
23 * Revision 1.16 2004/12/17 11:39:41 starvik
24 * SMP support.
26 * Revision 1.15 2004/11/23 18:36:18 starvik
27 * Stack is now non-executable.
28 * Signal handler trampolines are placed in a reserved page mapped into all
29 * processes.
31 * Revision 1.14 2004/11/23 07:10:21 starvik
32 * Moved find_fixup_code to generic code.
34 * Revision 1.13 2004/11/23 07:00:54 starvik
35 * Actually use the execute permission bit in the MMU. This makes it possible
36 * to prevent e.g. attacks where executable code is put on the stack.
38 * Revision 1.12 2004/09/29 06:16:04 starvik
39 * Use instruction_pointer
41 * Revision 1.11 2004/05/14 07:58:05 starvik
42 * Merge of changes from 2.4
44 * Revision 1.10 2003/10/27 14:51:24 starvik
45 * Removed debugcode
47 * Revision 1.9 2003/10/27 14:50:42 starvik
48 * Changed do_page_fault signature
50 * Revision 1.8 2003/07/04 13:02:48 tobiasa
51 * Moved code snippet from arch/cris/mm/fault.c that searches for fixup code
52 * to seperate function in arch-specific files.
54 * Revision 1.7 2003/01/22 06:48:38 starvik
55 * Fixed warnings issued by GCC 3.2.1
57 * Revision 1.6 2003/01/09 14:42:52 starvik
58 * Merge of Linux 2.5.55
60 * Revision 1.5 2002/12/11 14:44:48 starvik
61 * Extracted v10 (ETRAX 100LX) specific stuff to arch/cris/arch-v10/mm
63 * Revision 1.4 2002/11/13 15:10:28 starvik
64 * pte_offset has been renamed to pte_offset_kernel
66 * Revision 1.3 2002/11/05 06:45:13 starvik
67 * Merge of Linux 2.5.45
69 * Revision 1.2 2001/12/18 13:35:22 bjornw
70 * Applied the 2.4.13->2.4.16 CRIS patch to 2.5.1 (is a copy of 2.4.15).
72 * Revision 1.20 2001/11/22 13:34:06 bjornw
73 * * Bug workaround (LX TR89): force a rerun of the whole of an interrupted
74 * unaligned write, because the second half of the write will be corrupted
75 * otherwise. Affected unaligned writes spanning not-yet mapped pages.
76 * * Optimization: use the wr_rd bit in R_MMU_CAUSE to know whether a miss
77 * was due to a read or a write (before we didn't know this until the next
78 * restart of the interrupted instruction, thus wasting one fault-irq)
80 * Revision 1.19 2001/11/12 19:02:10 pkj
81 * Fixed compiler warnings.
83 * Revision 1.18 2001/07/18 22:14:32 bjornw
84 * Enable interrupts in the bulk of do_page_fault
86 * Revision 1.17 2001/07/18 13:07:23 bjornw
87 * * Detect non-existant PTE's in vmalloc pmd synchronization
88 * * Remove comment about fast-paths for VMALLOC_START etc, because all that
89 * was totally bogus anyway it turned out :)
90 * * Fix detection of vmalloc-area synchronization
91 * * Add some comments
93 * Revision 1.16 2001/06/13 00:06:08 bjornw
94 * current_pgd should be volatile
96 * Revision 1.15 2001/06/13 00:02:23 bjornw
97 * Use a separate variable to store the current pgd to avoid races in schedule
99 * Revision 1.14 2001/05/16 17:41:07 hp
100 * Last comment tweak further tweaked.
102 * Revision 1.13 2001/05/15 00:58:44 hp
103 * Expand a bit on the comment why we compare address >= TASK_SIZE rather
104 * than >= VMALLOC_START.
106 * Revision 1.12 2001/04/04 10:51:14 bjornw
107 * mmap_sem is grabbed for reading
109 * Revision 1.11 2001/03/23 07:36:07 starvik
110 * Corrected according to review remarks
112 * Revision 1.10 2001/03/21 16:10:11 bjornw
113 * CRIS_FRAME_FIXUP not needed anymore, use FRAME_NORMAL
115 * Revision 1.9 2001/03/05 13:22:20 bjornw
116 * Spell-fix and fix in vmalloc_fault handling
118 * Revision 1.8 2000/11/22 14:45:31 bjornw
119 * * 2.4.0-test10 removed the set_pgdir instantaneous kernel global mapping
120 * into all processes. Instead we fill in the missing PTE entries on demand.
122 * Revision 1.7 2000/11/21 16:39:09 bjornw
123 * fixup switches frametype
125 * Revision 1.6 2000/11/17 16:54:08 bjornw
126 * More detailed siginfo reporting
131 #include <linux/mm.h>
132 #include <linux/interrupt.h>
133 #include <linux/module.h>
134 #include <asm/uaccess.h>
136 extern int find_fixup_code(struct pt_regs *);
137 extern void die_if_kernel(const char *, struct pt_regs *, long);
138 extern int raw_printk(const char *fmt, ...);
140 /* debug of low-level TLB reload */
141 #undef DEBUG
143 #ifdef DEBUG
144 #define D(x) x
145 #else
146 #define D(x)
147 #endif
149 /* debug of higher-level faults */
150 #define DPG(x)
152 /* current active page directory */
154 volatile DEFINE_PER_CPU(pgd_t *,current_pgd);
155 unsigned long cris_signal_return_page;
158 * This routine handles page faults. It determines the address,
159 * and the problem, and then passes it off to one of the appropriate
160 * routines.
162 * Notice that the address we're given is aligned to the page the fault
163 * occurred in, since we only get the PFN in R_MMU_CAUSE not the complete
164 * address.
166 * error_code:
167 * bit 0 == 0 means no page found, 1 means protection fault
168 * bit 1 == 0 means read, 1 means write
170 * If this routine detects a bad access, it returns 1, otherwise it
171 * returns 0.
174 asmlinkage void
175 do_page_fault(unsigned long address, struct pt_regs *regs,
176 int protection, int writeaccess)
178 struct task_struct *tsk;
179 struct mm_struct *mm;
180 struct vm_area_struct * vma;
181 siginfo_t info;
183 D(printk("Page fault for %lX on %X at %lX, prot %d write %d\n",
184 address, smp_processor_id(), instruction_pointer(regs),
185 protection, writeaccess));
187 tsk = current;
190 * We fault-in kernel-space virtual memory on-demand. The
191 * 'reference' page table is init_mm.pgd.
193 * NOTE! We MUST NOT take any locks for this case. We may
194 * be in an interrupt or a critical region, and should
195 * only copy the information from the master page table,
196 * nothing more.
198 * NOTE2: This is done so that, when updating the vmalloc
199 * mappings we don't have to walk all processes pgdirs and
200 * add the high mappings all at once. Instead we do it as they
201 * are used. However vmalloc'ed page entries have the PAGE_GLOBAL
202 * bit set so sometimes the TLB can use a lingering entry.
204 * This verifies that the fault happens in kernel space
205 * and that the fault was not a protection error (error_code & 1).
208 if (address >= VMALLOC_START &&
209 !protection &&
210 !user_mode(regs))
211 goto vmalloc_fault;
213 /* When stack execution is not allowed we store the signal
214 * trampolines in the reserved cris_signal_return_page.
215 * Handle this in the exact same way as vmalloc (we know
216 * that the mapping is there and is valid so no need to
217 * call handle_mm_fault).
219 if (cris_signal_return_page &&
220 address == cris_signal_return_page &&
221 !protection && user_mode(regs))
222 goto vmalloc_fault;
224 /* we can and should enable interrupts at this point */
225 local_irq_enable();
227 mm = tsk->mm;
228 info.si_code = SEGV_MAPERR;
231 * If we're in an interrupt or have no user
232 * context, we must not take the fault..
235 if (in_interrupt() || !mm)
236 goto no_context;
238 down_read(&mm->mmap_sem);
239 vma = find_vma(mm, address);
240 if (!vma)
241 goto bad_area;
242 if (vma->vm_start <= address)
243 goto good_area;
244 if (!(vma->vm_flags & VM_GROWSDOWN))
245 goto bad_area;
246 if (user_mode(regs)) {
248 * accessing the stack below usp is always a bug.
249 * we get page-aligned addresses so we can only check
250 * if we're within a page from usp, but that might be
251 * enough to catch brutal errors at least.
253 if (address + PAGE_SIZE < rdusp())
254 goto bad_area;
256 if (expand_stack(vma, address))
257 goto bad_area;
260 * Ok, we have a good vm_area for this memory access, so
261 * we can handle it..
264 good_area:
265 info.si_code = SEGV_ACCERR;
267 /* first do some preliminary protection checks */
269 if (writeaccess == 2){
270 if (!(vma->vm_flags & VM_EXEC))
271 goto bad_area;
272 } else if (writeaccess == 1) {
273 if (!(vma->vm_flags & VM_WRITE))
274 goto bad_area;
275 } else {
276 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
277 goto bad_area;
281 * If for any reason at all we couldn't handle the fault,
282 * make sure we exit gracefully rather than endlessly redo
283 * the fault.
286 switch (handle_mm_fault(mm, vma, address, writeaccess & 1)) {
287 case VM_FAULT_MINOR:
288 tsk->min_flt++;
289 break;
290 case VM_FAULT_MAJOR:
291 tsk->maj_flt++;
292 break;
293 case VM_FAULT_SIGBUS:
294 goto do_sigbus;
295 default:
296 goto out_of_memory;
299 up_read(&mm->mmap_sem);
300 return;
303 * Something tried to access memory that isn't in our memory map..
304 * Fix it, but check if it's kernel or user first..
307 bad_area:
308 up_read(&mm->mmap_sem);
310 bad_area_nosemaphore:
311 DPG(show_registers(regs));
313 /* User mode accesses just cause a SIGSEGV */
315 if (user_mode(regs)) {
316 info.si_signo = SIGSEGV;
317 info.si_errno = 0;
318 /* info.si_code has been set above */
319 info.si_addr = (void *)address;
320 force_sig_info(SIGSEGV, &info, tsk);
321 return;
324 no_context:
326 /* Are we prepared to handle this kernel fault?
328 * (The kernel has valid exception-points in the source
329 * when it acesses user-memory. When it fails in one
330 * of those points, we find it in a table and do a jump
331 * to some fixup code that loads an appropriate error
332 * code)
335 if (find_fixup_code(regs))
336 return;
339 * Oops. The kernel tried to access some bad page. We'll have to
340 * terminate things with extreme prejudice.
343 if ((unsigned long) (address) < PAGE_SIZE)
344 raw_printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
345 else
346 raw_printk(KERN_ALERT "Unable to handle kernel access");
347 raw_printk(" at virtual address %08lx\n",address);
349 die_if_kernel("Oops", regs, (writeaccess << 1) | protection);
351 do_exit(SIGKILL);
354 * We ran out of memory, or some other thing happened to us that made
355 * us unable to handle the page fault gracefully.
358 out_of_memory:
359 up_read(&mm->mmap_sem);
360 printk("VM: killing process %s\n", tsk->comm);
361 if (user_mode(regs))
362 do_exit(SIGKILL);
363 goto no_context;
365 do_sigbus:
366 up_read(&mm->mmap_sem);
369 * Send a sigbus, regardless of whether we were in kernel
370 * or user mode.
372 info.si_signo = SIGBUS;
373 info.si_errno = 0;
374 info.si_code = BUS_ADRERR;
375 info.si_addr = (void *)address;
376 force_sig_info(SIGBUS, &info, tsk);
378 /* Kernel mode? Handle exceptions or die */
379 if (!user_mode(regs))
380 goto no_context;
381 return;
383 vmalloc_fault:
386 * Synchronize this task's top level page-table
387 * with the 'reference' page table.
389 * Use current_pgd instead of tsk->active_mm->pgd
390 * since the latter might be unavailable if this
391 * code is executed in a misfortunately run irq
392 * (like inside schedule() between switch_mm and
393 * switch_to...).
396 int offset = pgd_index(address);
397 pgd_t *pgd, *pgd_k;
398 pud_t *pud, *pud_k;
399 pmd_t *pmd, *pmd_k;
400 pte_t *pte_k;
402 pgd = (pgd_t *)per_cpu(current_pgd, smp_processor_id()) + offset;
403 pgd_k = init_mm.pgd + offset;
405 /* Since we're two-level, we don't need to do both
406 * set_pgd and set_pmd (they do the same thing). If
407 * we go three-level at some point, do the right thing
408 * with pgd_present and set_pgd here.
410 * Also, since the vmalloc area is global, we don't
411 * need to copy individual PTE's, it is enough to
412 * copy the pgd pointer into the pte page of the
413 * root task. If that is there, we'll find our pte if
414 * it exists.
417 pud = pud_offset(pgd, address);
418 pud_k = pud_offset(pgd_k, address);
419 if (!pud_present(*pud_k))
420 goto no_context;
422 pmd = pmd_offset(pud, address);
423 pmd_k = pmd_offset(pud_k, address);
425 if (!pmd_present(*pmd_k))
426 goto bad_area_nosemaphore;
428 set_pmd(pmd, *pmd_k);
430 /* Make sure the actual PTE exists as well to
431 * catch kernel vmalloc-area accesses to non-mapped
432 * addresses. If we don't do this, this will just
433 * silently loop forever.
436 pte_k = pte_offset_kernel(pmd_k, address);
437 if (!pte_present(*pte_k))
438 goto no_context;
440 return;
444 /* Find fixup code. */
446 find_fixup_code(struct pt_regs *regs)
448 const struct exception_table_entry *fixup;
450 if ((fixup = search_exception_tables(instruction_pointer(regs))) != 0) {
451 /* Adjust the instruction pointer in the stackframe. */
452 instruction_pointer(regs) = fixup->fixup;
453 arch_fixup(regs);
454 return 1;
457 return 0;