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