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