fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / arch / ia64 / mm / fault.c
1 /*
2  * MMU fault handling support.
3  *
4  * Copyright (C) 1998-2002 Hewlett-Packard Co
5  *      David Mosberger-Tang <davidm@hpl.hp.com>
6  */
7 #include <linux/sched.h>
8 #include <linux/kernel.h>
9 #include <linux/mm.h>
10 #include <linux/smp_lock.h>
11 #include <linux/interrupt.h>
12 #include <linux/kprobes.h>
13 #include <linux/vs_memory.h>
14
15 #include <asm/pgtable.h>
16 #include <asm/processor.h>
17 #include <asm/system.h>
18 #include <asm/uaccess.h>
19 #include <asm/kdebug.h>
20
21 extern void die (char *, struct pt_regs *, long);
22
23 #ifdef CONFIG_KPROBES
24 ATOMIC_NOTIFIER_HEAD(notify_page_fault_chain);
25
26 /* Hook to register for page fault notifications */
27 int register_page_fault_notifier(struct notifier_block *nb)
28 {
29         return atomic_notifier_chain_register(&notify_page_fault_chain, nb);
30 }
31
32 int unregister_page_fault_notifier(struct notifier_block *nb)
33 {
34         return atomic_notifier_chain_unregister(&notify_page_fault_chain, nb);
35 }
36
37 static inline int notify_page_fault(enum die_val val, const char *str,
38                         struct pt_regs *regs, long err, int trap, int sig)
39 {
40         struct die_args args = {
41                 .regs = regs,
42                 .str = str,
43                 .err = err,
44                 .trapnr = trap,
45                 .signr = sig
46         };
47         return atomic_notifier_call_chain(&notify_page_fault_chain, val, &args);
48 }
49 #else
50 static inline int notify_page_fault(enum die_val val, const char *str,
51                         struct pt_regs *regs, long err, int trap, int sig)
52 {
53         return NOTIFY_DONE;
54 }
55 #endif
56
57 /*
58  * Return TRUE if ADDRESS points at a page in the kernel's mapped segment
59  * (inside region 5, on ia64) and that page is present.
60  */
61 static int
62 mapped_kernel_page_is_present (unsigned long address)
63 {
64         pgd_t *pgd;
65         pud_t *pud;
66         pmd_t *pmd;
67         pte_t *ptep, pte;
68
69         pgd = pgd_offset_k(address);
70         if (pgd_none(*pgd) || pgd_bad(*pgd))
71                 return 0;
72
73         pud = pud_offset(pgd, address);
74         if (pud_none(*pud) || pud_bad(*pud))
75                 return 0;
76
77         pmd = pmd_offset(pud, address);
78         if (pmd_none(*pmd) || pmd_bad(*pmd))
79                 return 0;
80
81         ptep = pte_offset_kernel(pmd, address);
82         if (!ptep)
83                 return 0;
84
85         pte = *ptep;
86         return pte_present(pte);
87 }
88
89 void __kprobes
90 ia64_do_page_fault (unsigned long address, unsigned long isr, struct pt_regs *regs)
91 {
92         int signal = SIGSEGV, code = SEGV_MAPERR;
93         struct vm_area_struct *vma, *prev_vma;
94         struct mm_struct *mm = current->mm;
95         struct siginfo si;
96         unsigned long mask;
97
98         /* mmap_sem is performance critical.... */
99         prefetchw(&mm->mmap_sem);
100
101         /*
102          * If we're in an interrupt or have no user context, we must not take the fault..
103          */
104         if (in_atomic() || !mm)
105                 goto no_context;
106
107 #ifdef CONFIG_VIRTUAL_MEM_MAP
108         /*
109          * If fault is in region 5 and we are in the kernel, we may already
110          * have the mmap_sem (pfn_valid macro is called during mmap). There
111          * is no vma for region 5 addr's anyway, so skip getting the semaphore
112          * and go directly to the exception handling code.
113          */
114
115         if ((REGION_NUMBER(address) == 5) && !user_mode(regs))
116                 goto bad_area_no_up;
117 #endif
118
119         /*
120          * This is to handle the kprobes on user space access instructions
121          */
122         if (notify_page_fault(DIE_PAGE_FAULT, "page fault", regs, code, TRAP_BRKPT,
123                                         SIGSEGV) == NOTIFY_STOP)
124                 return;
125
126         down_read(&mm->mmap_sem);
127
128         vma = find_vma_prev(mm, address, &prev_vma);
129         if (!vma)
130                 goto bad_area;
131
132         /* find_vma_prev() returns vma such that address < vma->vm_end or NULL */
133         if (address < vma->vm_start)
134                 goto check_expansion;
135
136   good_area:
137         code = SEGV_ACCERR;
138
139         /* OK, we've got a good vm_area for this memory area.  Check the access permissions: */
140
141 #       define VM_READ_BIT      0
142 #       define VM_WRITE_BIT     1
143 #       define VM_EXEC_BIT      2
144
145 #       if (((1 << VM_READ_BIT) != VM_READ || (1 << VM_WRITE_BIT) != VM_WRITE) \
146             || (1 << VM_EXEC_BIT) != VM_EXEC)
147 #               error File is out of sync with <linux/mm.h>.  Please update.
148 #       endif
149
150         if (((isr >> IA64_ISR_R_BIT) & 1UL) && (!(vma->vm_flags & (VM_READ | VM_WRITE))))
151                 goto bad_area;
152
153         mask = (  (((isr >> IA64_ISR_X_BIT) & 1UL) << VM_EXEC_BIT)
154                 | (((isr >> IA64_ISR_W_BIT) & 1UL) << VM_WRITE_BIT));
155
156         if ((vma->vm_flags & mask) != mask)
157                 goto bad_area;
158
159   survive:
160         /*
161          * If for any reason at all we couldn't handle the fault, make
162          * sure we exit gracefully rather than endlessly redo the
163          * fault.
164          */
165         switch (handle_mm_fault(mm, vma, address, (mask & VM_WRITE) != 0)) {
166               case VM_FAULT_MINOR:
167                 ++current->min_flt;
168                 break;
169               case VM_FAULT_MAJOR:
170                 ++current->maj_flt;
171                 break;
172               case VM_FAULT_SIGBUS:
173                 /*
174                  * We ran out of memory, or some other thing happened
175                  * to us that made us unable to handle the page fault
176                  * gracefully.
177                  */
178                 signal = SIGBUS;
179                 goto bad_area;
180               case VM_FAULT_OOM:
181                 goto out_of_memory;
182               default:
183                 BUG();
184         }
185         up_read(&mm->mmap_sem);
186         return;
187
188   check_expansion:
189         if (!(prev_vma && (prev_vma->vm_flags & VM_GROWSUP) && (address == prev_vma->vm_end))) {
190                 if (!(vma->vm_flags & VM_GROWSDOWN))
191                         goto bad_area;
192                 if (REGION_NUMBER(address) != REGION_NUMBER(vma->vm_start)
193                     || REGION_OFFSET(address) >= RGN_MAP_LIMIT)
194                         goto bad_area;
195                 if (expand_stack(vma, address))
196                         goto bad_area;
197         } else {
198                 vma = prev_vma;
199                 if (REGION_NUMBER(address) != REGION_NUMBER(vma->vm_start)
200                     || REGION_OFFSET(address) >= RGN_MAP_LIMIT)
201                         goto bad_area;
202                 /*
203                  * Since the register backing store is accessed sequentially,
204                  * we disallow growing it by more than a page at a time.
205                  */
206                 if (address > vma->vm_end + PAGE_SIZE - sizeof(long))
207                         goto bad_area;
208                 if (expand_upwards(vma, address))
209                         goto bad_area;
210         }
211         goto good_area;
212
213   bad_area:
214         up_read(&mm->mmap_sem);
215 #ifdef CONFIG_VIRTUAL_MEM_MAP
216   bad_area_no_up:
217 #endif
218         if ((isr & IA64_ISR_SP)
219             || ((isr & IA64_ISR_NA) && (isr & IA64_ISR_CODE_MASK) == IA64_ISR_CODE_LFETCH))
220         {
221                 /*
222                  * This fault was due to a speculative load or lfetch.fault, set the "ed"
223                  * bit in the psr to ensure forward progress.  (Target register will get a
224                  * NaT for ld.s, lfetch will be canceled.)
225                  */
226                 ia64_psr(regs)->ed = 1;
227                 return;
228         }
229         if (user_mode(regs)) {
230                 si.si_signo = signal;
231                 si.si_errno = 0;
232                 si.si_code = code;
233                 si.si_addr = (void __user *) address;
234                 si.si_isr = isr;
235                 si.si_flags = __ISR_VALID;
236                 force_sig_info(signal, &si, current);
237                 return;
238         }
239
240   no_context:
241         if ((isr & IA64_ISR_SP)
242             || ((isr & IA64_ISR_NA) && (isr & IA64_ISR_CODE_MASK) == IA64_ISR_CODE_LFETCH))
243         {
244                 /*
245                  * This fault was due to a speculative load or lfetch.fault, set the "ed"
246                  * bit in the psr to ensure forward progress.  (Target register will get a
247                  * NaT for ld.s, lfetch will be canceled.)
248                  */
249                 ia64_psr(regs)->ed = 1;
250                 return;
251         }
252
253         /*
254          * Since we have no vma's for region 5, we might get here even if the address is
255          * valid, due to the VHPT walker inserting a non present translation that becomes
256          * stale. If that happens, the non present fault handler already purged the stale
257          * translation, which fixed the problem. So, we check to see if the translation is
258          * valid, and return if it is.
259          */
260         if (REGION_NUMBER(address) == 5 && mapped_kernel_page_is_present(address))
261                 return;
262
263         if (ia64_done_with_exception(regs))
264                 return;
265
266         /*
267          * Oops. The kernel tried to access some bad page. We'll have to terminate things
268          * with extreme prejudice.
269          */
270         bust_spinlocks(1);
271
272         if (address < PAGE_SIZE)
273                 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference (address %016lx)\n", address);
274         else
275                 printk(KERN_ALERT "Unable to handle kernel paging request at "
276                        "virtual address %016lx\n", address);
277         die("Oops", regs, isr);
278         bust_spinlocks(0);
279         do_exit(SIGKILL);
280         return;
281
282   out_of_memory:
283         up_read(&mm->mmap_sem);
284         if (is_init(current)) {
285                 yield();
286                 down_read(&mm->mmap_sem);
287                 goto survive;
288         }
289         printk(KERN_CRIT "VM: killing process %s\n", current->comm);
290         if (user_mode(regs))
291                 do_exit(SIGKILL);
292         goto no_context;
293 }