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