make oldconfig will rebuild these...
[linux-2.4.21-pre4.git] / arch / cris / mm / fault.c
1 /*
2  *  linux/arch/cris/mm/fault.c
3  *
4  *  Copyright (C) 2000, 2001  Axis Communications AB
5  *
6  *  Authors:  Bjorn Wesen 
7  * 
8  *  $Log: fault.c,v $
9  *  Revision 1.1.1.1  2005/04/11 02:49:58  jack
10  *  first release
11  *
12  *  Revision 1.1.1.1  2005/01/10 13:16:04  jack
13  *  First release
14  *
15  *  Revision 1.21  2002/05/28 14:24:56  bjornw
16  *  Corrected typo
17  *
18  *  Revision 1.20  2001/11/22 13:34:06  bjornw
19  *  * Bug workaround (LX TR89): force a rerun of the whole of an interrupted
20  *    unaligned write, because the second half of the write will be corrupted
21  *    otherwise. Affected unaligned writes spanning not-yet mapped pages.
22  *  * Optimization: use the wr_rd bit in R_MMU_CAUSE to know whether a miss
23  *    was due to a read or a write (before we didn't know this until the next
24  *    restart of the interrupted instruction, thus wasting one fault-irq)
25  *
26  *  Revision 1.19  2001/11/12 19:02:10  pkj
27  *  Fixed compiler warnings.
28  *
29  *  Revision 1.18  2001/07/18 22:14:32  bjornw
30  *  Enable interrupts in the bulk of do_page_fault
31  *
32  *  Revision 1.17  2001/07/18 13:07:23  bjornw
33  *  * Detect non-existant PTE's in vmalloc pmd synchronization
34  *  * Remove comment about fast-paths for VMALLOC_START etc, because all that
35  *    was totally bogus anyway it turned out :)
36  *  * Fix detection of vmalloc-area synchronization
37  *  * Add some comments
38  *
39  *  Revision 1.16  2001/06/13 00:06:08  bjornw
40  *  current_pgd should be volatile
41  *
42  *  Revision 1.15  2001/06/13 00:02:23  bjornw
43  *  Use a separate variable to store the current pgd to avoid races in schedule
44  *
45  *  Revision 1.14  2001/05/16 17:41:07  hp
46  *  Last comment tweak further tweaked.
47  *
48  *  Revision 1.13  2001/05/15 00:58:44  hp
49  *  Expand a bit on the comment why we compare address >= TASK_SIZE rather
50  *  than >= VMALLOC_START.
51  *
52  *  Revision 1.12  2001/04/04 10:51:14  bjornw
53  *  mmap_sem is grabbed for reading
54  *
55  *  Revision 1.11  2001/03/23 07:36:07  starvik
56  *  Corrected according to review remarks
57  *
58  *  Revision 1.10  2001/03/21 16:10:11  bjornw
59  *  CRIS_FRAME_FIXUP not needed anymore, use FRAME_NORMAL
60  *
61  *  Revision 1.9  2001/03/05 13:22:20  bjornw
62  *  Spell-fix and fix in vmalloc_fault handling
63  *
64  *  Revision 1.8  2000/11/22 14:45:31  bjornw
65  *  * 2.4.0-test10 removed the set_pgdir instantaneous kernel global mapping
66  *    into all processes. Instead we fill in the missing PTE entries on demand.
67  *
68  *  Revision 1.7  2000/11/21 16:39:09  bjornw
69  *  fixup switches frametype
70  *
71  *  Revision 1.6  2000/11/17 16:54:08  bjornw
72  *  More detailed siginfo reporting
73  *
74  *
75  */
76
77 #include <linux/signal.h>
78 #include <linux/sched.h>
79 #include <linux/kernel.h>
80 #include <linux/errno.h>
81 #include <linux/string.h>
82 #include <linux/types.h>
83 #include <linux/ptrace.h>
84 #include <linux/mman.h>
85 #include <linux/mm.h>
86 #include <linux/interrupt.h>
87
88 #include <asm/system.h>
89 #include <asm/segment.h>
90 #include <asm/pgtable.h>
91 #include <asm/uaccess.h>
92 #include <asm/svinto.h>
93
94 extern void die_if_kernel(const char *,struct pt_regs *,long);
95
96 asmlinkage void do_invalid_op (struct pt_regs *, unsigned long);
97 asmlinkage void do_page_fault(unsigned long address, struct pt_regs *regs,
98                               int error_code);
99
100 /* debug of low-level TLB reload */
101 #undef DEBUG
102
103 #ifdef DEBUG
104 #define D(x) x
105 #else
106 #define D(x)
107 #endif
108
109 /* debug of higher-level faults */
110 #define DPG(x)
111
112 /* current active page directory */
113
114 volatile pgd_t *current_pgd;
115
116 /* fast TLB-fill fault handler
117  * this is called from entry.S with interrupts disabled
118  */
119
120 void
121 handle_mmu_bus_fault(struct pt_regs *regs)
122 {
123         int cause, select;
124 #ifdef DEBUG
125         int index;
126         int page_id;
127         int acc, inv;
128 #endif
129         int miss, we, writeac;
130         pmd_t *pmd;
131         pte_t pte;
132         int errcode;
133         unsigned long address;
134
135         cause = *R_MMU_CAUSE;
136         select = *R_TLB_SELECT;
137
138         address = cause & PAGE_MASK; /* get faulting address */
139
140 #ifdef DEBUG
141         page_id = IO_EXTRACT(R_MMU_CAUSE,  page_id,   cause);
142         acc     = IO_EXTRACT(R_MMU_CAUSE,  acc_excp,  cause);
143         inv     = IO_EXTRACT(R_MMU_CAUSE,  inv_excp,  cause);  
144         index   = IO_EXTRACT(R_TLB_SELECT, index,     select);
145 #endif
146         miss    = IO_EXTRACT(R_MMU_CAUSE,  miss_excp, cause);
147         we      = IO_EXTRACT(R_MMU_CAUSE,  we_excp,   cause);
148         writeac = IO_EXTRACT(R_MMU_CAUSE,  wr_rd,     cause);
149
150         /* ETRAX 100LX TR89 bugfix: if the second half of an unaligned
151          * write causes a MMU-fault, it will not be restarted correctly.
152          * This could happen if a write crosses a page-boundary and the
153          * second page is not yet COW'ed or even loaded. The workaround
154          * is to clear the unaligned bit in the CPU status record, so 
155          * that the CPU will rerun both the first and second halves of
156          * the instruction. This will not have any sideeffects unless
157          * the first half goes to any device or memory that can't be
158          * written twice, and which is mapped through the MMU.
159          *
160          * We only need to do this for writes.
161          */
162
163         if(writeac)
164                 regs->csrinstr &= ~(1 << 5);
165         
166         /* Set errcode's R/W flag according to the mode which caused the
167          * fault
168          */
169
170         errcode = writeac << 1;
171
172         D(printk("bus_fault from IRP 0x%lx: addr 0x%lx, miss %d, inv %d, we %d, acc %d, dx %d pid %d\n",
173                  regs->irp, address, miss, inv, we, acc, index, page_id));
174
175         /* for a miss, we need to reload the TLB entry */
176
177         if (miss) {
178                 /* see if the pte exists at all
179                  * refer through current_pgd, dont use mm->pgd
180                  */
181
182                 pmd = (pmd_t *)(current_pgd + pgd_index(address));
183                 if (pmd_none(*pmd))
184                         goto dofault;
185                 if (pmd_bad(*pmd)) {
186                         printk("bad pgdir entry 0x%lx at 0x%p\n", *(unsigned long*)pmd, pmd);
187                         pmd_clear(pmd);
188                         return;
189                 }
190                 pte = *pte_offset(pmd, address);
191                 if (!pte_present(pte))
192                         goto dofault;
193
194 #ifdef DEBUG
195                 printk(" found pte %lx pg %p ", pte_val(pte), pte_page(pte));
196                 if (pte_val(pte) & _PAGE_SILENT_WRITE)
197                         printk("Silent-W ");
198                 if (pte_val(pte) & _PAGE_KERNEL)
199                         printk("Kernel ");
200                 if (pte_val(pte) & _PAGE_SILENT_READ)
201                         printk("Silent-R ");
202                 if (pte_val(pte) & _PAGE_GLOBAL)
203                         printk("Global ");
204                 if (pte_val(pte) & _PAGE_PRESENT)
205                         printk("Present ");
206                 if (pte_val(pte) & _PAGE_ACCESSED)
207                         printk("Accessed ");
208                 if (pte_val(pte) & _PAGE_MODIFIED)
209                         printk("Modified ");
210                 if (pte_val(pte) & _PAGE_READ)
211                         printk("Readable ");
212                 if (pte_val(pte) & _PAGE_WRITE)
213                         printk("Writeable ");
214                 printk("\n");
215 #endif
216
217                 /* load up the chosen TLB entry
218                  * this assumes the pte format is the same as the TLB_LO layout.
219                  *
220                  * the write to R_TLB_LO also writes the vpn and page_id fields from
221                  * R_MMU_CAUSE, which we in this case obviously want to keep
222                  */
223
224                 *R_TLB_LO = pte_val(pte);
225
226                 return;
227         } 
228
229         errcode = 1 | (we << 1);
230
231  dofault:
232         /* leave it to the MM system fault handler below */
233         D(printk("do_page_fault %lx errcode %d\n", address, errcode));
234         do_page_fault(address, regs, errcode);
235 }
236
237 /*
238  * This routine handles page faults.  It determines the address,
239  * and the problem, and then passes it off to one of the appropriate
240  * routines.
241  *
242  * Notice that the address we're given is aligned to the page the fault
243  * occurred in, since we only get the PFN in R_MMU_CAUSE not the complete
244  * address.
245  *
246  * error_code:
247  *      bit 0 == 0 means no page found, 1 means protection fault
248  *      bit 1 == 0 means read, 1 means write
249  *
250  * If this routine detects a bad access, it returns 1, otherwise it
251  * returns 0.
252  */
253
254 asmlinkage void
255 do_page_fault(unsigned long address, struct pt_regs *regs,
256               int error_code)
257 {
258         struct task_struct *tsk;
259         struct mm_struct *mm;
260         struct vm_area_struct * vma;
261         int writeaccess;
262         unsigned long fixup;
263         siginfo_t info;
264
265         tsk = current;
266
267         /*
268          * We fault-in kernel-space virtual memory on-demand. The
269          * 'reference' page table is init_mm.pgd.
270          *
271          * NOTE! We MUST NOT take any locks for this case. We may
272          * be in an interrupt or a critical region, and should
273          * only copy the information from the master page table,
274          * nothing more.
275          *
276          * NOTE2: This is done so that, when updating the vmalloc
277          * mappings we don't have to walk all processes pgdirs and
278          * add the high mappings all at once. Instead we do it as they
279          * are used. However vmalloc'ed page entries have the PAGE_GLOBAL
280          * bit set so sometimes the TLB can use a lingering entry.
281          *
282          * This verifies that the fault happens in kernel space
283          * and that the fault was not a protection error (error_code & 1).
284          */
285
286         if (address >= VMALLOC_START &&
287             !(error_code & 1) &&
288             !user_mode(regs))
289                 goto vmalloc_fault;
290
291         /* we can and should enable interrupts at this point */
292         sti();
293
294         mm = tsk->mm;
295         writeaccess = error_code & 2;
296         info.si_code = SEGV_MAPERR;
297
298         /*
299          * If we're in an interrupt or have no user
300          * context, we must not take the fault..
301          */
302
303         if (in_interrupt() || !mm)
304                 goto no_context;
305
306         down_read(&mm->mmap_sem);
307         vma = find_vma(mm, address);
308         if (!vma)
309                 goto bad_area;
310         if (vma->vm_start <= address)
311                 goto good_area;
312         if (!(vma->vm_flags & VM_GROWSDOWN))
313                 goto bad_area;
314         if (user_mode(regs)) {
315                 /*
316                  * accessing the stack below usp is always a bug.
317                  * we get page-aligned addresses so we can only check
318                  * if we're within a page from usp, but that might be
319                  * enough to catch brutal errors at least.
320                  */
321                 if (address + PAGE_SIZE < rdusp())
322                         goto bad_area;
323         }
324         if (expand_stack(vma, address))
325                 goto bad_area;
326
327         /*
328          * Ok, we have a good vm_area for this memory access, so
329          * we can handle it..
330          */
331
332  good_area:
333         info.si_code = SEGV_ACCERR;
334
335         /* first do some preliminary protection checks */
336
337         if (writeaccess) {
338                 if (!(vma->vm_flags & VM_WRITE))
339                         goto bad_area;
340         } else {
341                 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
342                         goto bad_area;
343         }
344
345         /*
346          * If for any reason at all we couldn't handle the fault,
347          * make sure we exit gracefully rather than endlessly redo
348          * the fault.
349          */
350
351         switch (handle_mm_fault(mm, vma, address, writeaccess)) {
352         case 1:
353                 tsk->min_flt++;
354                 break;
355         case 2:
356                 tsk->maj_flt++;
357                 break;
358         case 0:
359                 goto do_sigbus;
360         default:
361                 goto out_of_memory;
362         }
363
364         up_read(&mm->mmap_sem);
365         return;
366
367         /*
368          * Something tried to access memory that isn't in our memory map..
369          * Fix it, but check if it's kernel or user first..
370          */
371
372  bad_area:
373         up_read(&mm->mmap_sem);
374
375  bad_area_nosemaphore:
376         DPG(show_registers(regs));
377
378         /* User mode accesses just cause a SIGSEGV */
379
380         if (user_mode(regs)) {
381                 info.si_signo = SIGSEGV;
382                 info.si_errno = 0;
383                 /* info.si_code has been set above */
384                 info.si_addr = (void *)address;
385                 force_sig_info(SIGSEGV, &info, tsk);
386                 return;
387         }
388
389  no_context:
390
391         /* Are we prepared to handle this kernel fault?
392          *
393          * (The kernel has valid exception-points in the source 
394          *  when it acesses user-memory. When it fails in one
395          *  of those points, we find it in a table and do a jump
396          *  to some fixup code that loads an appropriate error
397          *  code)
398          */
399
400         if ((fixup = search_exception_table(regs->irp)) != 0) {
401                 /* Adjust the instruction pointer in the stackframe */
402
403                 regs->irp = fixup;
404
405                 /* We do not want to return by restoring the CPU-state
406                  * anymore, so switch frame-types (see ptrace.h)
407                  */
408
409                 regs->frametype = CRIS_FRAME_NORMAL;
410
411                 D(printk("doing fixup to 0x%lx\n", fixup));
412                 return;
413         }
414
415         /*
416          * Oops. The kernel tried to access some bad page. We'll have to
417          * terminate things with extreme prejudice.
418          */
419
420         if ((unsigned long) (address) < PAGE_SIZE)
421                 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
422         else
423                 printk(KERN_ALERT "Unable to handle kernel access");
424         printk(" at virtual address %08lx\n",address);
425
426         die_if_kernel("Oops", regs, error_code);
427
428         do_exit(SIGKILL);
429
430         /*
431          * We ran out of memory, or some other thing happened to us that made
432          * us unable to handle the page fault gracefully.
433          */
434
435  out_of_memory:
436         up_read(&mm->mmap_sem);
437         printk("VM: killing process %s\n", tsk->comm);
438         if (user_mode(regs))
439                 do_exit(SIGKILL);
440         goto no_context;
441
442  do_sigbus:
443         up_read(&mm->mmap_sem);
444
445         /*
446          * Send a sigbus, regardless of whether we were in kernel
447          * or user mode.
448          */
449         info.si_signo = SIGBUS;
450         info.si_errno = 0;
451         info.si_code = BUS_ADRERR;
452         info.si_addr = (void *)address;
453         force_sig_info(SIGBUS, &info, tsk);
454
455         /* Kernel mode? Handle exceptions or die */
456         if (!user_mode(regs))
457                 goto no_context;
458         return;
459
460 vmalloc_fault:
461         {
462                 /*
463                  * Synchronize this task's top level page-table
464                  * with the 'reference' page table.
465                  *
466                  * Use current_pgd instead of tsk->active_mm->pgd
467                  * since the latter might be unavailable if this
468                  * code is executed in a misfortunately run irq
469                  * (like inside schedule() between switch_mm and
470                  *  switch_to...).
471                  */
472
473                 int offset = pgd_index(address);
474                 pgd_t *pgd, *pgd_k;
475                 pmd_t *pmd, *pmd_k;
476                 pte_t *pte_k;
477
478                 pgd = (pgd_t *)current_pgd + offset;
479                 pgd_k = init_mm.pgd + offset;
480
481                 /* Since we're two-level, we don't need to do both
482                  * set_pgd and set_pmd (they do the same thing). If
483                  * we go three-level at some point, do the right thing
484                  * with pgd_present and set_pgd here. 
485                  * 
486                  * Also, since the vmalloc area is global, we don't
487                  * need to copy individual PTE's, it is enough to
488                  * copy the pgd pointer into the pte page of the
489                  * root task. If that is there, we'll find our pte if
490                  * it exists.
491                  */
492
493                 pmd = pmd_offset(pgd, address);
494                 pmd_k = pmd_offset(pgd_k, address);
495
496                 if (!pmd_present(*pmd_k))
497                         goto bad_area_nosemaphore;
498
499                 set_pmd(pmd, *pmd_k);
500
501                 /* Make sure the actual PTE exists as well to
502                  * catch kernel vmalloc-area accesses to non-mapped
503                  * addresses. If we don't do this, this will just
504                  * silently loop forever.
505                  */
506
507                 pte_k = pte_offset(pmd_k, address);
508                 if (!pte_present(*pte_k))
509                         goto no_context;
510
511                 return;
512         }
513 }