ef75a9ac7ca46fdc480ba102718a1073a20ffd2a
[bcm963xx.git] / kernel / linux / 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  2006/04/12 06:24:47  michaelc
10  *  Original Broadcom V3.06.02V release
11  *
12  *  Revision 1.11  2004/05/14 07:58:05  starvik
13  *  Merge of changes from 2.4
14  *
15  *  Revision 1.10  2003/10/27 14:51:24  starvik
16  *  Removed debugcode
17  *
18  *  Revision 1.9  2003/10/27 14:50:42  starvik
19  *  Changed do_page_fault signature
20  *
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.
24  *
25  *  Revision 1.7  2003/01/22 06:48:38  starvik
26  *  Fixed warnings issued by GCC 3.2.1
27  *
28  *  Revision 1.6  2003/01/09 14:42:52  starvik
29  *  Merge of Linux 2.5.55
30  *
31  *  Revision 1.5  2002/12/11 14:44:48  starvik
32  *  Extracted v10 (ETRAX 100LX) specific stuff to arch/cris/arch-v10/mm
33  *
34  *  Revision 1.4  2002/11/13 15:10:28  starvik
35  *  pte_offset has been renamed to pte_offset_kernel
36  *
37  *  Revision 1.3  2002/11/05 06:45:13  starvik
38  *  Merge of Linux 2.5.45
39  *
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).
42  *
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)
50  *
51  *  Revision 1.19  2001/11/12 19:02:10  pkj
52  *  Fixed compiler warnings.
53  *
54  *  Revision 1.18  2001/07/18 22:14:32  bjornw
55  *  Enable interrupts in the bulk of do_page_fault
56  *
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
62  *  * Add some comments
63  *
64  *  Revision 1.16  2001/06/13 00:06:08  bjornw
65  *  current_pgd should be volatile
66  *
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
69  *
70  *  Revision 1.14  2001/05/16 17:41:07  hp
71  *  Last comment tweak further tweaked.
72  *
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.
76  *
77  *  Revision 1.12  2001/04/04 10:51:14  bjornw
78  *  mmap_sem is grabbed for reading
79  *
80  *  Revision 1.11  2001/03/23 07:36:07  starvik
81  *  Corrected according to review remarks
82  *
83  *  Revision 1.10  2001/03/21 16:10:11  bjornw
84  *  CRIS_FRAME_FIXUP not needed anymore, use FRAME_NORMAL
85  *
86  *  Revision 1.9  2001/03/05 13:22:20  bjornw
87  *  Spell-fix and fix in vmalloc_fault handling
88  *
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.
92  *
93  *  Revision 1.7  2000/11/21 16:39:09  bjornw
94  *  fixup switches frametype
95  *
96  *  Revision 1.6  2000/11/17 16:54:08  bjornw
97  *  More detailed siginfo reporting
98  *
99  *
100  */
101
102 #include <linux/mm.h>
103 #include <linux/interrupt.h>
104 #include <linux/module.h>
105 #include <asm/uaccess.h>
106
107 extern int find_fixup_code(struct pt_regs *);
108 extern void die_if_kernel(const char *, struct pt_regs *, long);
109
110 /* debug of low-level TLB reload */
111 #undef DEBUG
112
113 #ifdef DEBUG
114 #define D(x) x
115 #else
116 #define D(x)
117 #endif
118
119 /* debug of higher-level faults */
120 #define DPG(x)
121
122 /* current active page directory */
123
124 volatile pgd_t *current_pgd;
125
126 /*
127  * This routine handles page faults.  It determines the address,
128  * and the problem, and then passes it off to one of the appropriate
129  * routines.
130  *
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
133  * address.
134  *
135  * error_code:
136  *      bit 0 == 0 means no page found, 1 means protection fault
137  *      bit 1 == 0 means read, 1 means write
138  *
139  * If this routine detects a bad access, it returns 1, otherwise it
140  * returns 0.
141  */
142
143 asmlinkage void
144 do_page_fault(unsigned long address, struct pt_regs *regs,
145               int protection, int writeaccess)
146 {
147         struct task_struct *tsk;
148         struct mm_struct *mm;
149         struct vm_area_struct * vma;
150         siginfo_t info;
151
152         D(printk("Page fault for %X at %X, prot %d write %d\n",
153                  address, regs->erp, protection, writeaccess));
154
155         tsk = current;
156
157         /*
158          * We fault-in kernel-space virtual memory on-demand. The
159          * 'reference' page table is init_mm.pgd.
160          *
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,
164          * nothing more.
165          *
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.
171          *
172          * This verifies that the fault happens in kernel space
173          * and that the fault was not a protection error (error_code & 1).
174          */
175
176         if (address >= VMALLOC_START &&
177             !protection &&
178             !user_mode(regs))
179                 goto vmalloc_fault;
180
181         /* we can and should enable interrupts at this point */
182         sti();
183
184         mm = tsk->mm;
185         info.si_code = SEGV_MAPERR;
186
187         /*
188          * If we're in an interrupt or have no user
189          * context, we must not take the fault..
190          */
191
192         if (in_interrupt() || !mm)
193                 goto no_context;
194
195         down_read(&mm->mmap_sem);
196         vma = find_vma(mm, address);
197         if (!vma)
198                 goto bad_area;
199         if (vma->vm_start <= address)
200                 goto good_area;
201         if (!(vma->vm_flags & VM_GROWSDOWN))
202                 goto bad_area;
203         if (user_mode(regs)) {
204                 /*
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.
209                  */
210                 if (address + PAGE_SIZE < rdusp())
211                         goto bad_area;
212         }
213         if (expand_stack(vma, address))
214                 goto bad_area;
215
216         /*
217          * Ok, we have a good vm_area for this memory access, so
218          * we can handle it..
219          */
220
221  good_area:
222         info.si_code = SEGV_ACCERR;
223
224         /* first do some preliminary protection checks */
225
226         if (writeaccess) {
227                 if (!(vma->vm_flags & VM_WRITE))
228                         goto bad_area;
229         } else {
230                 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
231                         goto bad_area;
232         }
233
234         /*
235          * If for any reason at all we couldn't handle the fault,
236          * make sure we exit gracefully rather than endlessly redo
237          * the fault.
238          */
239
240         switch (handle_mm_fault(mm, vma, address, writeaccess)) {
241         case 1:
242                 tsk->min_flt++;
243                 break;
244         case 2:
245                 tsk->maj_flt++;
246                 break;
247         case 0:
248                 goto do_sigbus;
249         default:
250                 goto out_of_memory;
251         }
252
253         up_read(&mm->mmap_sem);
254         return;
255
256         /*
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..
259          */
260
261  bad_area:
262         up_read(&mm->mmap_sem);
263
264  bad_area_nosemaphore:
265         DPG(show_registers(regs));
266
267         /* User mode accesses just cause a SIGSEGV */
268
269         if (user_mode(regs)) {
270                 info.si_signo = SIGSEGV;
271                 info.si_errno = 0;
272                 /* info.si_code has been set above */
273                 info.si_addr = (void *)address;
274                 force_sig_info(SIGSEGV, &info, tsk);
275                 return;
276         }
277
278  no_context:
279
280         /* Are we prepared to handle this kernel fault?
281          *
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
286          *  code)
287          */
288
289         if (find_fixup_code(regs))
290                 return;
291
292         /*
293          * Oops. The kernel tried to access some bad page. We'll have to
294          * terminate things with extreme prejudice.
295          */
296
297         if ((unsigned long) (address) < PAGE_SIZE)
298                 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
299         else
300                 printk(KERN_ALERT "Unable to handle kernel access");
301         printk(" at virtual address %08lx\n",address);
302
303         die_if_kernel("Oops", regs, (writeaccess << 1) | protection);
304
305         do_exit(SIGKILL);
306
307         /*
308          * We ran out of memory, or some other thing happened to us that made
309          * us unable to handle the page fault gracefully.
310          */
311
312  out_of_memory:
313         up_read(&mm->mmap_sem);
314         printk("VM: killing process %s\n", tsk->comm);
315         if (user_mode(regs))
316                 do_exit(SIGKILL);
317         goto no_context;
318
319  do_sigbus:
320         up_read(&mm->mmap_sem);
321
322         /*
323          * Send a sigbus, regardless of whether we were in kernel
324          * or user mode.
325          */
326         info.si_signo = SIGBUS;
327         info.si_errno = 0;
328         info.si_code = BUS_ADRERR;
329         info.si_addr = (void *)address;
330         force_sig_info(SIGBUS, &info, tsk);
331
332         /* Kernel mode? Handle exceptions or die */
333         if (!user_mode(regs))
334                 goto no_context;
335         return;
336
337 vmalloc_fault:
338         {
339                 /*
340                  * Synchronize this task's top level page-table
341                  * with the 'reference' page table.
342                  *
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
347                  *  switch_to...).
348                  */
349
350                 int offset = pgd_index(address);
351                 pgd_t *pgd, *pgd_k;
352                 pmd_t *pmd, *pmd_k;
353                 pte_t *pte_k;
354
355                 pgd = (pgd_t *)current_pgd + offset;
356                 pgd_k = init_mm.pgd + offset;
357
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. 
362                  * 
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
367                  * it exists.
368                  */
369
370                 pmd = pmd_offset(pgd, address);
371                 pmd_k = pmd_offset(pgd_k, address);
372
373                 if (!pmd_present(*pmd_k))
374                         goto bad_area_nosemaphore;
375
376                 set_pmd(pmd, *pmd_k);
377
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.
382                  */
383
384                 pte_k = pte_offset_kernel(pmd_k, address);
385                 if (!pte_present(*pte_k))
386                         goto no_context;
387
388                 return;
389         }
390 }