Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #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 Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 25 | #include <linux/kprobes.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Andi Kleen | 66c5815 | 2006-01-11 22:44:09 +0100 | [diff] [blame] | 36 | /* 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 Keshavamurthy | 1bd858a | 2006-06-26 00:25:25 -0700 | [diff] [blame] | 43 | #ifdef CONFIG_KPROBES |
| 44 | ATOMIC_NOTIFIER_HEAD(notify_page_fault_chain); |
| 45 | |
| 46 | /* Hook to register for page fault notifications */ |
| 47 | int register_page_fault_notifier(struct notifier_block *nb) |
| 48 | { |
| 49 | vmalloc_sync_all(); |
| 50 | return atomic_notifier_chain_register(¬ify_page_fault_chain, nb); |
| 51 | } |
| 52 | |
| 53 | int unregister_page_fault_notifier(struct notifier_block *nb) |
| 54 | { |
| 55 | return atomic_notifier_chain_unregister(¬ify_page_fault_chain, nb); |
| 56 | } |
| 57 | |
| 58 | static 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(¬ify_page_fault_chain, val, &args); |
| 69 | } |
| 70 | #else |
| 71 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | void 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 */ |
| 102 | static noinline int is_prefetch(struct pt_regs *regs, unsigned long addr, |
| 103 | unsigned long error_code) |
| 104 | { |
Andi Kleen | dd2994f | 2006-09-26 10:52:33 +0200 | [diff] [blame^] | 105 | unsigned char __user *instr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | int scan_more = 1; |
| 107 | int prefetch = 0; |
Andi Kleen | f1290ec | 2005-04-16 15:24:59 -0700 | [diff] [blame] | 108 | unsigned char *max_instr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
| 110 | /* If it was a exec fault ignore */ |
Andi Kleen | 66c5815 | 2006-01-11 22:44:09 +0100 | [diff] [blame] | 111 | if (error_code & PF_INSTR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | return 0; |
| 113 | |
Andi Kleen | dd2994f | 2006-09-26 10:52:33 +0200 | [diff] [blame^] | 114 | instr = (unsigned char __user *)convert_rip_to_linear(current, regs); |
Andi Kleen | f1290ec | 2005-04-16 15:24:59 -0700 | [diff] [blame] | 115 | max_instr = instr + 15; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
Vincent Hanquez | 76381fe | 2005-06-23 00:08:46 -0700 | [diff] [blame] | 117 | if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | 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 Kleen | dd2994f | 2006-09-26 10:52:33 +0200 | [diff] [blame^] | 125 | if (__get_user(opcode, (char __user *)instr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | 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 Hanquez | 76381fe | 2005-06-23 00:08:46 -0700 | [diff] [blame] | 149 | scan_more = (!user_mode(regs)) || (regs->cs == __USER_CS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | 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 Kleen | dd2994f | 2006-09-26 10:52:33 +0200 | [diff] [blame^] | 163 | if (__get_user(opcode, (char __user *)instr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | 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 | |
| 176 | static int bad_address(void *p) |
| 177 | { |
| 178 | unsigned long dummy; |
Andi Kleen | dd2994f | 2006-09-26 10:52:33 +0200 | [diff] [blame^] | 179 | return __get_user(dummy, (unsigned long __user *)p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | void 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | if (bad_address(pgd)) goto bad; |
Jan Beulich | d646bce | 2006-02-03 21:51:47 +0100 | [diff] [blame] | 194 | printk("PGD %lx ", pgd_val(*pgd)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | if (!pgd_present(*pgd)) goto ret; |
| 196 | |
Andi Kleen | d2ae5b5 | 2006-06-26 13:57:56 +0200 | [diff] [blame] | 197 | pud = pud_offset(pgd, address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | 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)); |
| 210 | ret: |
| 211 | printk("\n"); |
| 212 | return; |
| 213 | bad: |
| 214 | printk("BAD\n"); |
| 215 | } |
| 216 | |
| 217 | static const char errata93_warning[] = |
| 218 | KERN_ERR "******* Your BIOS seems to not contain a fix for K8 errata #93\n" |
| 219 | KERN_ERR "******* Working around it, but it may cause SEGVs or burn power.\n" |
| 220 | KERN_ERR "******* Please consider a BIOS update.\n" |
| 221 | KERN_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 | |
| 231 | static 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 | |
| 251 | int unhandled_signal(struct task_struct *tsk, int sig) |
| 252 | { |
| 253 | if (tsk->pid == 1) |
| 254 | return 1; |
Andi Kleen | 5e5ec10 | 2005-08-19 06:56:04 +0200 | [diff] [blame] | 255 | if (tsk->ptrace & PT_PTRACED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | 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 | |
| 261 | static noinline void pgtable_bad(unsigned long address, struct pt_regs *regs, |
| 262 | unsigned long error_code) |
| 263 | { |
Jan Beulich | 1209140 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 264 | unsigned long flags = oops_begin(); |
Jan Beulich | 6e3f361 | 2006-01-11 22:42:14 +0100 | [diff] [blame] | 265 | struct task_struct *tsk; |
Jan Beulich | 1209140 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 266 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | printk(KERN_ALERT "%s: Corrupted page table at address %lx\n", |
| 268 | current->comm, address); |
| 269 | dump_pagetable(address); |
Jan Beulich | 6e3f361 | 2006-01-11 22:42:14 +0100 | [diff] [blame] | 270 | tsk = current; |
| 271 | tsk->thread.cr2 = address; |
| 272 | tsk->thread.trap_no = 14; |
| 273 | tsk->thread.error_code = error_code; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | __die("Bad pagetable", regs, error_code); |
Jan Beulich | 1209140 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 275 | oops_end(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | do_exit(SIGKILL); |
| 277 | } |
| 278 | |
| 279 | /* |
Andi Kleen | f95190b | 2006-01-11 22:44:00 +0100 | [diff] [blame] | 280 | * Handle a fault on the vmalloc area |
Andi Kleen | 3b9ba4d | 2005-05-16 21:53:31 -0700 | [diff] [blame] | 281 | * |
| 282 | * This assumes no large pages in there. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | */ |
| 284 | static 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 Beulich | 8c914cb | 2006-03-25 16:29:40 +0100 | [diff] [blame] | 301 | else |
| 302 | BUG_ON(pgd_page(*pgd) != pgd_page(*pgd_ref)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | |
| 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 Kleen | 3b9ba4d | 2005-05-16 21:53:31 -0700 | [diff] [blame] | 323 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | int page_fault_trace = 0; |
| 332 | int 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | */ |
Prasanna S Panchamukhi | 0f2fbdc | 2005-09-06 15:19:28 -0700 | [diff] [blame] | 339 | asmlinkage void __kprobes do_page_fault(struct pt_regs *regs, |
| 340 | unsigned long error_code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | { |
| 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 Beulich | 1209140 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 348 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | siginfo_t info; |
| 350 | |
Arjan van de Ven | a9ba9a3 | 2006-03-25 16:30:10 +0100 | [diff] [blame] | 351 | tsk = current; |
| 352 | mm = tsk->mm; |
| 353 | prefetchw(&mm->mmap_sem); |
| 354 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | /* get the address */ |
| 356 | __asm__("movq %%cr2,%0":"=r" (address)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | 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 Beulich | 8b1bde9 | 2006-01-11 22:42:23 +0100 | [diff] [blame] | 372 | * protection error (error_code & 9) == 0. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | */ |
Suresh Siddha | 8492980 | 2005-06-21 17:14:32 -0700 | [diff] [blame] | 374 | if (unlikely(address >= TASK_SIZE64)) { |
Andi Kleen | f95190b | 2006-01-11 22:44:00 +0100 | [diff] [blame] | 375 | /* |
| 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 Kleen | 66c5815 | 2006-01-11 22:44:09 +0100 | [diff] [blame] | 380 | if (!(error_code & (PF_RSVD|PF_USER|PF_PROT)) && |
Andi Kleen | f95190b | 2006-01-11 22:44:00 +0100 | [diff] [blame] | 381 | ((address >= VMALLOC_START && address < VMALLOC_END))) { |
Jan Beulich | 8c914cb | 2006-03-25 16:29:40 +0100 | [diff] [blame] | 382 | if (vmalloc_fault(address) >= 0) |
| 383 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | } |
Anil S Keshavamurthy | 1bd858a | 2006-06-26 00:25:25 -0700 | [diff] [blame] | 385 | if (notify_page_fault(DIE_PAGE_FAULT, "page fault", regs, error_code, 14, |
Jan Beulich | 8c914cb | 2006-03-25 16:29:40 +0100 | [diff] [blame] | 386 | SIGSEGV) == NOTIFY_STOP) |
| 387 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | /* |
| 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 Keshavamurthy | 1bd858a | 2006-06-26 00:25:25 -0700 | [diff] [blame] | 395 | if (notify_page_fault(DIE_PAGE_FAULT, "page fault", regs, error_code, 14, |
Jan Beulich | 8c914cb | 2006-03-25 16:29:40 +0100 | [diff] [blame] | 396 | 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 Kleen | 66c5815 | 2006-01-11 22:44:09 +0100 | [diff] [blame] | 406 | if (unlikely(error_code & PF_RSVD)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | 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 Bunk | 80f7228 | 2006-06-30 18:27:16 +0200 | [diff] [blame] | 420 | * erroneous fault occurring in a code path which already holds mmap_sem |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | * 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 Kleen | 66c5815 | 2006-01-11 22:44:09 +0100 | [diff] [blame] | 433 | if ((error_code & PF_USER) == 0 && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | !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 Ebbert | 03fdc2c | 2006-06-26 13:59:50 +0200 | [diff] [blame] | 447 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | 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 | */ |
| 459 | good_area: |
| 460 | info.si_code = SEGV_ACCERR; |
| 461 | write = 0; |
Andi Kleen | 66c5815 | 2006-01-11 22:44:09 +0100 | [diff] [blame] | 462 | switch (error_code & (PF_PROT|PF_WRITE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | default: /* 3: write, present */ |
| 464 | /* fall through */ |
Andi Kleen | 66c5815 | 2006-01-11 22:44:09 +0100 | [diff] [blame] | 465 | case PF_WRITE: /* write, not present */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | if (!(vma->vm_flags & VM_WRITE)) |
| 467 | goto bad_area; |
| 468 | write++; |
| 469 | break; |
Andi Kleen | 66c5815 | 2006-01-11 22:44:09 +0100 | [diff] [blame] | 470 | case PF_PROT: /* read, present */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | goto bad_area; |
Andi Kleen | 66c5815 | 2006-01-11 22:44:09 +0100 | [diff] [blame] | 472 | case 0: /* read, not present */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | 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 Nyberg | 9680021 | 2005-08-04 16:14:57 +0200 | [diff] [blame] | 483 | case VM_FAULT_MINOR: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | tsk->min_flt++; |
| 485 | break; |
Alexander Nyberg | 9680021 | 2005-08-04 16:14:57 +0200 | [diff] [blame] | 486 | case VM_FAULT_MAJOR: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | tsk->maj_flt++; |
| 488 | break; |
Alexander Nyberg | 9680021 | 2005-08-04 16:14:57 +0200 | [diff] [blame] | 489 | case VM_FAULT_SIGBUS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | 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 | */ |
| 502 | bad_area: |
| 503 | up_read(&mm->mmap_sem); |
| 504 | |
| 505 | bad_area_nosemaphore: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | /* User mode accesses just cause a SIGSEGV */ |
Andi Kleen | 66c5815 | 2006-01-11 22:44:09 +0100 | [diff] [blame] | 507 | if (error_code & PF_USER) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | 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 | |
| 541 | no_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 Beulich | 1209140 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 565 | flags = oops_begin(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | dump_pagetable(address); |
Jan Beulich | 6e3f361 | 2006-01-11 22:42:14 +0100 | [diff] [blame] | 574 | tsk->thread.cr2 = address; |
| 575 | tsk->thread.trap_no = 14; |
| 576 | tsk->thread.error_code = error_code; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | __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 Beulich | 1209140 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 580 | oops_end(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | 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 | */ |
| 587 | out_of_memory: |
| 588 | up_read(&mm->mmap_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | 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 | |
| 598 | do_sigbus: |
| 599 | up_read(&mm->mmap_sem); |
| 600 | |
| 601 | /* Kernel mode? Handle exceptions or die */ |
Andi Kleen | 66c5815 | 2006-01-11 22:44:09 +0100 | [diff] [blame] | 602 | if (!(error_code & PF_USER)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | 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 Kleen | 9e43e1b | 2005-11-05 17:25:54 +0100 | [diff] [blame] | 615 | |
Jan Beulich | 8c914cb | 2006-03-25 16:29:40 +0100 | [diff] [blame] | 616 | DEFINE_SPINLOCK(pgd_lock); |
| 617 | struct page *pgd_list; |
| 618 | |
| 619 | void 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 Kleen | 9e43e1b | 2005-11-05 17:25:54 +0100 | [diff] [blame] | 658 | static int __init enable_pagefaulttrace(char *str) |
| 659 | { |
| 660 | page_fault_trace = 1; |
OGAWA Hirofumi | 9b41046 | 2006-03-31 02:30:33 -0800 | [diff] [blame] | 661 | return 1; |
Andi Kleen | 9e43e1b | 2005-11-05 17:25:54 +0100 | [diff] [blame] | 662 | } |
| 663 | __setup("pagefaulttrace", enable_pagefaulttrace); |