blob: 449a437f5d7043da98a0e42ecb23dbf936ee8c58 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/x86-64/mm/fault.c
3 *
4 * Copyright (C) 1995 Linus Torvalds
5 * Copyright (C) 2001,2002 Andi Kleen, SuSE Labs.
6 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/signal.h>
9#include <linux/sched.h>
10#include <linux/kernel.h>
11#include <linux/errno.h>
12#include <linux/string.h>
13#include <linux/types.h>
14#include <linux/ptrace.h>
15#include <linux/mman.h>
16#include <linux/mm.h>
17#include <linux/smp.h>
18#include <linux/smp_lock.h>
19#include <linux/interrupt.h>
20#include <linux/init.h>
21#include <linux/tty.h>
22#include <linux/vt_kern.h> /* For unblank_screen() */
23#include <linux/compiler.h>
24#include <linux/module.h>
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -070025#include <linux/kprobes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include <asm/system.h>
28#include <asm/uaccess.h>
29#include <asm/pgalloc.h>
30#include <asm/smp.h>
31#include <asm/tlbflush.h>
32#include <asm/proto.h>
33#include <asm/kdebug.h>
34#include <asm-generic/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Andi Kleen66c58152006-01-11 22:44:09 +010036/* Page fault error code bits */
37#define PF_PROT (1<<0) /* or no page found */
38#define PF_WRITE (1<<1)
39#define PF_USER (1<<2)
40#define PF_RSVD (1<<3)
41#define PF_INSTR (1<<4)
42
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -070043#ifdef CONFIG_KPROBES
44ATOMIC_NOTIFIER_HEAD(notify_page_fault_chain);
45
46/* Hook to register for page fault notifications */
47int register_page_fault_notifier(struct notifier_block *nb)
48{
49 vmalloc_sync_all();
50 return atomic_notifier_chain_register(&notify_page_fault_chain, nb);
51}
52
53int unregister_page_fault_notifier(struct notifier_block *nb)
54{
55 return atomic_notifier_chain_unregister(&notify_page_fault_chain, nb);
56}
57
58static inline int notify_page_fault(enum die_val val, const char *str,
59 struct pt_regs *regs, long err, int trap, int sig)
60{
61 struct die_args args = {
62 .regs = regs,
63 .str = str,
64 .err = err,
65 .trapnr = trap,
66 .signr = sig
67 };
68 return atomic_notifier_call_chain(&notify_page_fault_chain, val, &args);
69}
70#else
71static inline int notify_page_fault(enum die_val val, const char *str,
72 struct pt_regs *regs, long err, int trap, int sig)
73{
74 return NOTIFY_DONE;
75}
76#endif
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078void bust_spinlocks(int yes)
79{
80 int loglevel_save = console_loglevel;
81 if (yes) {
82 oops_in_progress = 1;
83 } else {
84#ifdef CONFIG_VT
85 unblank_screen();
86#endif
87 oops_in_progress = 0;
88 /*
89 * OK, the message is on the console. Now we call printk()
90 * without oops_in_progress set so that printk will give klogd
91 * a poke. Hold onto your hats...
92 */
93 console_loglevel = 15; /* NMI oopser may have shut the console up */
94 printk(" ");
95 console_loglevel = loglevel_save;
96 }
97}
98
99/* Sometimes the CPU reports invalid exceptions on prefetch.
100 Check that here and ignore.
101 Opcode checker based on code by Richard Brunner */
102static noinline int is_prefetch(struct pt_regs *regs, unsigned long addr,
103 unsigned long error_code)
104{
Andi Kleendd2994f2006-09-26 10:52:33 +0200105 unsigned char __user *instr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 int scan_more = 1;
107 int prefetch = 0;
Andi Kleenf1290ec2005-04-16 15:24:59 -0700108 unsigned char *max_instr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 /* If it was a exec fault ignore */
Andi Kleen66c58152006-01-11 22:44:09 +0100111 if (error_code & PF_INSTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return 0;
113
Andi Kleendd2994f2006-09-26 10:52:33 +0200114 instr = (unsigned char __user *)convert_rip_to_linear(current, regs);
Andi Kleenf1290ec2005-04-16 15:24:59 -0700115 max_instr = instr + 15;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700117 if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return 0;
119
120 while (scan_more && instr < max_instr) {
121 unsigned char opcode;
122 unsigned char instr_hi;
123 unsigned char instr_lo;
124
Andi Kleendd2994f2006-09-26 10:52:33 +0200125 if (__get_user(opcode, (char __user *)instr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 break;
127
128 instr_hi = opcode & 0xf0;
129 instr_lo = opcode & 0x0f;
130 instr++;
131
132 switch (instr_hi) {
133 case 0x20:
134 case 0x30:
135 /* Values 0x26,0x2E,0x36,0x3E are valid x86
136 prefixes. In long mode, the CPU will signal
137 invalid opcode if some of these prefixes are
138 present so we will never get here anyway */
139 scan_more = ((instr_lo & 7) == 0x6);
140 break;
141
142 case 0x40:
143 /* In AMD64 long mode, 0x40 to 0x4F are valid REX prefixes
144 Need to figure out under what instruction mode the
145 instruction was issued ... */
146 /* Could check the LDT for lm, but for now it's good
147 enough to assume that long mode only uses well known
148 segments or kernel. */
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700149 scan_more = (!user_mode(regs)) || (regs->cs == __USER_CS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 break;
151
152 case 0x60:
153 /* 0x64 thru 0x67 are valid prefixes in all modes. */
154 scan_more = (instr_lo & 0xC) == 0x4;
155 break;
156 case 0xF0:
157 /* 0xF0, 0xF2, and 0xF3 are valid prefixes in all modes. */
158 scan_more = !instr_lo || (instr_lo>>1) == 1;
159 break;
160 case 0x00:
161 /* Prefetch instruction is 0x0F0D or 0x0F18 */
162 scan_more = 0;
Andi Kleendd2994f2006-09-26 10:52:33 +0200163 if (__get_user(opcode, (char __user *)instr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 break;
165 prefetch = (instr_lo == 0xF) &&
166 (opcode == 0x0D || opcode == 0x18);
167 break;
168 default:
169 scan_more = 0;
170 break;
171 }
172 }
173 return prefetch;
174}
175
176static int bad_address(void *p)
177{
178 unsigned long dummy;
Andi Kleendd2994f2006-09-26 10:52:33 +0200179 return __get_user(dummy, (unsigned long __user *)p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182void dump_pagetable(unsigned long address)
183{
184 pgd_t *pgd;
185 pud_t *pud;
186 pmd_t *pmd;
187 pte_t *pte;
188
189 asm("movq %%cr3,%0" : "=r" (pgd));
190
191 pgd = __va((unsigned long)pgd & PHYSICAL_PAGE_MASK);
192 pgd += pgd_index(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (bad_address(pgd)) goto bad;
Jan Beulichd646bce2006-02-03 21:51:47 +0100194 printk("PGD %lx ", pgd_val(*pgd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (!pgd_present(*pgd)) goto ret;
196
Andi Kleend2ae5b52006-06-26 13:57:56 +0200197 pud = pud_offset(pgd, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (bad_address(pud)) goto bad;
199 printk("PUD %lx ", pud_val(*pud));
200 if (!pud_present(*pud)) goto ret;
201
202 pmd = pmd_offset(pud, address);
203 if (bad_address(pmd)) goto bad;
204 printk("PMD %lx ", pmd_val(*pmd));
205 if (!pmd_present(*pmd)) goto ret;
206
207 pte = pte_offset_kernel(pmd, address);
208 if (bad_address(pte)) goto bad;
209 printk("PTE %lx", pte_val(*pte));
210ret:
211 printk("\n");
212 return;
213bad:
214 printk("BAD\n");
215}
216
217static const char errata93_warning[] =
218KERN_ERR "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
219KERN_ERR "******* Working around it, but it may cause SEGVs or burn power.\n"
220KERN_ERR "******* Please consider a BIOS update.\n"
221KERN_ERR "******* Disabling USB legacy in the BIOS may also help.\n";
222
223/* Workaround for K8 erratum #93 & buggy BIOS.
224 BIOS SMM functions are required to use a specific workaround
225 to avoid corruption of the 64bit RIP register on C stepping K8.
226 A lot of BIOS that didn't get tested properly miss this.
227 The OS sees this as a page fault with the upper 32bits of RIP cleared.
228 Try to work around it here.
229 Note we only handle faults in kernel here. */
230
231static int is_errata93(struct pt_regs *regs, unsigned long address)
232{
233 static int warned;
234 if (address != regs->rip)
235 return 0;
236 if ((address >> 32) != 0)
237 return 0;
238 address |= 0xffffffffUL << 32;
239 if ((address >= (u64)_stext && address <= (u64)_etext) ||
240 (address >= MODULES_VADDR && address <= MODULES_END)) {
241 if (!warned) {
242 printk(errata93_warning);
243 warned = 1;
244 }
245 regs->rip = address;
246 return 1;
247 }
248 return 0;
249}
250
251int unhandled_signal(struct task_struct *tsk, int sig)
252{
253 if (tsk->pid == 1)
254 return 1;
Andi Kleen5e5ec102005-08-19 06:56:04 +0200255 if (tsk->ptrace & PT_PTRACED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return 0;
257 return (tsk->sighand->action[sig-1].sa.sa_handler == SIG_IGN) ||
258 (tsk->sighand->action[sig-1].sa.sa_handler == SIG_DFL);
259}
260
261static noinline void pgtable_bad(unsigned long address, struct pt_regs *regs,
262 unsigned long error_code)
263{
Jan Beulich12091402005-09-12 18:49:24 +0200264 unsigned long flags = oops_begin();
Jan Beulich6e3f3612006-01-11 22:42:14 +0100265 struct task_struct *tsk;
Jan Beulich12091402005-09-12 18:49:24 +0200266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
268 current->comm, address);
269 dump_pagetable(address);
Jan Beulich6e3f3612006-01-11 22:42:14 +0100270 tsk = current;
271 tsk->thread.cr2 = address;
272 tsk->thread.trap_no = 14;
273 tsk->thread.error_code = error_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 __die("Bad pagetable", regs, error_code);
Jan Beulich12091402005-09-12 18:49:24 +0200275 oops_end(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 do_exit(SIGKILL);
277}
278
279/*
Andi Kleenf95190b2006-01-11 22:44:00 +0100280 * Handle a fault on the vmalloc area
Andi Kleen3b9ba4d2005-05-16 21:53:31 -0700281 *
282 * This assumes no large pages in there.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 */
284static int vmalloc_fault(unsigned long address)
285{
286 pgd_t *pgd, *pgd_ref;
287 pud_t *pud, *pud_ref;
288 pmd_t *pmd, *pmd_ref;
289 pte_t *pte, *pte_ref;
290
291 /* Copy kernel mappings over when needed. This can also
292 happen within a race in page table update. In the later
293 case just flush. */
294
295 pgd = pgd_offset(current->mm ?: &init_mm, address);
296 pgd_ref = pgd_offset_k(address);
297 if (pgd_none(*pgd_ref))
298 return -1;
299 if (pgd_none(*pgd))
300 set_pgd(pgd, *pgd_ref);
Jan Beulich8c914cb2006-03-25 16:29:40 +0100301 else
302 BUG_ON(pgd_page(*pgd) != pgd_page(*pgd_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 /* Below here mismatches are bugs because these lower tables
305 are shared */
306
307 pud = pud_offset(pgd, address);
308 pud_ref = pud_offset(pgd_ref, address);
309 if (pud_none(*pud_ref))
310 return -1;
311 if (pud_none(*pud) || pud_page(*pud) != pud_page(*pud_ref))
312 BUG();
313 pmd = pmd_offset(pud, address);
314 pmd_ref = pmd_offset(pud_ref, address);
315 if (pmd_none(*pmd_ref))
316 return -1;
317 if (pmd_none(*pmd) || pmd_page(*pmd) != pmd_page(*pmd_ref))
318 BUG();
319 pte_ref = pte_offset_kernel(pmd_ref, address);
320 if (!pte_present(*pte_ref))
321 return -1;
322 pte = pte_offset_kernel(pmd, address);
Andi Kleen3b9ba4d2005-05-16 21:53:31 -0700323 /* Don't use pte_page here, because the mappings can point
324 outside mem_map, and the NUMA hash lookup cannot handle
325 that. */
326 if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return 0;
329}
330
331int page_fault_trace = 0;
332int exception_trace = 1;
333
334/*
335 * This routine handles page faults. It determines the address,
336 * and the problem, and then passes it off to one of the appropriate
337 * routines.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700339asmlinkage void __kprobes do_page_fault(struct pt_regs *regs,
340 unsigned long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 struct task_struct *tsk;
343 struct mm_struct *mm;
344 struct vm_area_struct * vma;
345 unsigned long address;
346 const struct exception_table_entry *fixup;
347 int write;
Jan Beulich12091402005-09-12 18:49:24 +0200348 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 siginfo_t info;
350
Arjan van de Vena9ba9a32006-03-25 16:30:10 +0100351 tsk = current;
352 mm = tsk->mm;
353 prefetchw(&mm->mmap_sem);
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 /* get the address */
356 __asm__("movq %%cr2,%0":"=r" (address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 info.si_code = SEGV_MAPERR;
359
360
361 /*
362 * We fault-in kernel-space virtual memory on-demand. The
363 * 'reference' page table is init_mm.pgd.
364 *
365 * NOTE! We MUST NOT take any locks for this case. We may
366 * be in an interrupt or a critical region, and should
367 * only copy the information from the master page table,
368 * nothing more.
369 *
370 * This verifies that the fault happens in kernel space
371 * (error_code & 4) == 0, and that the fault was not a
Jan Beulich8b1bde92006-01-11 22:42:23 +0100372 * protection error (error_code & 9) == 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 */
Suresh Siddha84929802005-06-21 17:14:32 -0700374 if (unlikely(address >= TASK_SIZE64)) {
Andi Kleenf95190b2006-01-11 22:44:00 +0100375 /*
376 * Don't check for the module range here: its PML4
377 * is always initialized because it's shared with the main
378 * kernel text. Only vmalloc may need PML4 syncups.
379 */
Andi Kleen66c58152006-01-11 22:44:09 +0100380 if (!(error_code & (PF_RSVD|PF_USER|PF_PROT)) &&
Andi Kleenf95190b2006-01-11 22:44:00 +0100381 ((address >= VMALLOC_START && address < VMALLOC_END))) {
Jan Beulich8c914cb2006-03-25 16:29:40 +0100382 if (vmalloc_fault(address) >= 0)
383 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -0700385 if (notify_page_fault(DIE_PAGE_FAULT, "page fault", regs, error_code, 14,
Jan Beulich8c914cb2006-03-25 16:29:40 +0100386 SIGSEGV) == NOTIFY_STOP)
387 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 /*
389 * Don't take the mm semaphore here. If we fixup a prefetch
390 * fault we could otherwise deadlock.
391 */
392 goto bad_area_nosemaphore;
393 }
394
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -0700395 if (notify_page_fault(DIE_PAGE_FAULT, "page fault", regs, error_code, 14,
Jan Beulich8c914cb2006-03-25 16:29:40 +0100396 SIGSEGV) == NOTIFY_STOP)
397 return;
398
399 if (likely(regs->eflags & X86_EFLAGS_IF))
400 local_irq_enable();
401
402 if (unlikely(page_fault_trace))
403 printk("pagefault rip:%lx rsp:%lx cs:%lu ss:%lu address %lx error %lx\n",
404 regs->rip,regs->rsp,regs->cs,regs->ss,address,error_code);
405
Andi Kleen66c58152006-01-11 22:44:09 +0100406 if (unlikely(error_code & PF_RSVD))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 pgtable_bad(address, regs, error_code);
408
409 /*
410 * If we're in an interrupt or have no user
411 * context, we must not take the fault..
412 */
413 if (unlikely(in_atomic() || !mm))
414 goto bad_area_nosemaphore;
415
416 again:
417 /* When running in the kernel we expect faults to occur only to
418 * addresses in user space. All other faults represent errors in the
419 * kernel and should generate an OOPS. Unfortunatly, in the case of an
Adrian Bunk80f72282006-06-30 18:27:16 +0200420 * erroneous fault occurring in a code path which already holds mmap_sem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 * we will deadlock attempting to validate the fault against the
422 * address space. Luckily the kernel only validly references user
423 * space from well defined areas of code, which are listed in the
424 * exceptions table.
425 *
426 * As the vast majority of faults will be valid we will only perform
427 * the source reference check when there is a possibilty of a deadlock.
428 * Attempt to lock the address space, if we cannot we then validate the
429 * source. If this is invalid we can skip the address space check,
430 * thus avoiding the deadlock.
431 */
432 if (!down_read_trylock(&mm->mmap_sem)) {
Andi Kleen66c58152006-01-11 22:44:09 +0100433 if ((error_code & PF_USER) == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 !search_exception_tables(regs->rip))
435 goto bad_area_nosemaphore;
436 down_read(&mm->mmap_sem);
437 }
438
439 vma = find_vma(mm, address);
440 if (!vma)
441 goto bad_area;
442 if (likely(vma->vm_start <= address))
443 goto good_area;
444 if (!(vma->vm_flags & VM_GROWSDOWN))
445 goto bad_area;
446 if (error_code & 4) {
Chuck Ebbert03fdc2c2006-06-26 13:59:50 +0200447 /* Allow userspace just enough access below the stack pointer
448 * to let the 'enter' instruction work.
449 */
450 if (address + 65536 + 32 * sizeof(unsigned long) < regs->rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 goto bad_area;
452 }
453 if (expand_stack(vma, address))
454 goto bad_area;
455/*
456 * Ok, we have a good vm_area for this memory access, so
457 * we can handle it..
458 */
459good_area:
460 info.si_code = SEGV_ACCERR;
461 write = 0;
Andi Kleen66c58152006-01-11 22:44:09 +0100462 switch (error_code & (PF_PROT|PF_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 default: /* 3: write, present */
464 /* fall through */
Andi Kleen66c58152006-01-11 22:44:09 +0100465 case PF_WRITE: /* write, not present */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (!(vma->vm_flags & VM_WRITE))
467 goto bad_area;
468 write++;
469 break;
Andi Kleen66c58152006-01-11 22:44:09 +0100470 case PF_PROT: /* read, present */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 goto bad_area;
Andi Kleen66c58152006-01-11 22:44:09 +0100472 case 0: /* read, not present */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
474 goto bad_area;
475 }
476
477 /*
478 * If for any reason at all we couldn't handle the fault,
479 * make sure we exit gracefully rather than endlessly redo
480 * the fault.
481 */
482 switch (handle_mm_fault(mm, vma, address, write)) {
Alexander Nyberg96800212005-08-04 16:14:57 +0200483 case VM_FAULT_MINOR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 tsk->min_flt++;
485 break;
Alexander Nyberg96800212005-08-04 16:14:57 +0200486 case VM_FAULT_MAJOR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 tsk->maj_flt++;
488 break;
Alexander Nyberg96800212005-08-04 16:14:57 +0200489 case VM_FAULT_SIGBUS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 goto do_sigbus;
491 default:
492 goto out_of_memory;
493 }
494
495 up_read(&mm->mmap_sem);
496 return;
497
498/*
499 * Something tried to access memory that isn't in our memory map..
500 * Fix it, but check if it's kernel or user first..
501 */
502bad_area:
503 up_read(&mm->mmap_sem);
504
505bad_area_nosemaphore:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 /* User mode accesses just cause a SIGSEGV */
Andi Kleen66c58152006-01-11 22:44:09 +0100507 if (error_code & PF_USER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (is_prefetch(regs, address, error_code))
509 return;
510
511 /* Work around K8 erratum #100 K8 in compat mode
512 occasionally jumps to illegal addresses >4GB. We
513 catch this here in the page fault handler because
514 these addresses are not reachable. Just detect this
515 case and return. Any code segment in LDT is
516 compatibility mode. */
517 if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) &&
518 (address >> 32))
519 return;
520
521 if (exception_trace && unhandled_signal(tsk, SIGSEGV)) {
522 printk(
523 "%s%s[%d]: segfault at %016lx rip %016lx rsp %016lx error %lx\n",
524 tsk->pid > 1 ? KERN_INFO : KERN_EMERG,
525 tsk->comm, tsk->pid, address, regs->rip,
526 regs->rsp, error_code);
527 }
528
529 tsk->thread.cr2 = address;
530 /* Kernel addresses are always protection faults */
531 tsk->thread.error_code = error_code | (address >= TASK_SIZE);
532 tsk->thread.trap_no = 14;
533 info.si_signo = SIGSEGV;
534 info.si_errno = 0;
535 /* info.si_code has been set above */
536 info.si_addr = (void __user *)address;
537 force_sig_info(SIGSEGV, &info, tsk);
538 return;
539 }
540
541no_context:
542
543 /* Are we prepared to handle this kernel fault? */
544 fixup = search_exception_tables(regs->rip);
545 if (fixup) {
546 regs->rip = fixup->fixup;
547 return;
548 }
549
550 /*
551 * Hall of shame of CPU/BIOS bugs.
552 */
553
554 if (is_prefetch(regs, address, error_code))
555 return;
556
557 if (is_errata93(regs, address))
558 return;
559
560/*
561 * Oops. The kernel tried to access some bad page. We'll have to
562 * terminate things with extreme prejudice.
563 */
564
Jan Beulich12091402005-09-12 18:49:24 +0200565 flags = oops_begin();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 if (address < PAGE_SIZE)
568 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
569 else
570 printk(KERN_ALERT "Unable to handle kernel paging request");
571 printk(" at %016lx RIP: \n" KERN_ALERT,address);
572 printk_address(regs->rip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 dump_pagetable(address);
Jan Beulich6e3f3612006-01-11 22:42:14 +0100574 tsk->thread.cr2 = address;
575 tsk->thread.trap_no = 14;
576 tsk->thread.error_code = error_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 __die("Oops", regs, error_code);
578 /* Executive summary in case the body of the oops scrolled away */
579 printk(KERN_EMERG "CR2: %016lx\n", address);
Jan Beulich12091402005-09-12 18:49:24 +0200580 oops_end(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 do_exit(SIGKILL);
582
583/*
584 * We ran out of memory, or some other thing happened to us that made
585 * us unable to handle the page fault gracefully.
586 */
587out_of_memory:
588 up_read(&mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (current->pid == 1) {
590 yield();
591 goto again;
592 }
593 printk("VM: killing process %s\n", tsk->comm);
594 if (error_code & 4)
595 do_exit(SIGKILL);
596 goto no_context;
597
598do_sigbus:
599 up_read(&mm->mmap_sem);
600
601 /* Kernel mode? Handle exceptions or die */
Andi Kleen66c58152006-01-11 22:44:09 +0100602 if (!(error_code & PF_USER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 goto no_context;
604
605 tsk->thread.cr2 = address;
606 tsk->thread.error_code = error_code;
607 tsk->thread.trap_no = 14;
608 info.si_signo = SIGBUS;
609 info.si_errno = 0;
610 info.si_code = BUS_ADRERR;
611 info.si_addr = (void __user *)address;
612 force_sig_info(SIGBUS, &info, tsk);
613 return;
614}
Andi Kleen9e43e1b2005-11-05 17:25:54 +0100615
Jan Beulich8c914cb2006-03-25 16:29:40 +0100616DEFINE_SPINLOCK(pgd_lock);
617struct page *pgd_list;
618
619void vmalloc_sync_all(void)
620{
621 /* Note that races in the updates of insync and start aren't
622 problematic:
623 insync can only get set bits added, and updates to start are only
624 improving performance (without affecting correctness if undone). */
625 static DECLARE_BITMAP(insync, PTRS_PER_PGD);
626 static unsigned long start = VMALLOC_START & PGDIR_MASK;
627 unsigned long address;
628
629 for (address = start; address <= VMALLOC_END; address += PGDIR_SIZE) {
630 if (!test_bit(pgd_index(address), insync)) {
631 const pgd_t *pgd_ref = pgd_offset_k(address);
632 struct page *page;
633
634 if (pgd_none(*pgd_ref))
635 continue;
636 spin_lock(&pgd_lock);
637 for (page = pgd_list; page;
638 page = (struct page *)page->index) {
639 pgd_t *pgd;
640 pgd = (pgd_t *)page_address(page) + pgd_index(address);
641 if (pgd_none(*pgd))
642 set_pgd(pgd, *pgd_ref);
643 else
644 BUG_ON(pgd_page(*pgd) != pgd_page(*pgd_ref));
645 }
646 spin_unlock(&pgd_lock);
647 set_bit(pgd_index(address), insync);
648 }
649 if (address == start)
650 start = address + PGDIR_SIZE;
651 }
652 /* Check that there is no need to do the same for the modules area. */
653 BUILD_BUG_ON(!(MODULES_VADDR > __START_KERNEL));
654 BUILD_BUG_ON(!(((MODULES_END - 1) & PGDIR_MASK) ==
655 (__START_KERNEL & PGDIR_MASK)));
656}
657
Andi Kleen9e43e1b2005-11-05 17:25:54 +0100658static int __init enable_pagefaulttrace(char *str)
659{
660 page_fault_trace = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800661 return 1;
Andi Kleen9e43e1b2005-11-05 17:25:54 +0100662}
663__setup("pagefaulttrace", enable_pagefaulttrace);