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