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