fault_64.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. /*
  2. * Copyright (C) 1995 Linus Torvalds
  3. * Copyright (C) 2001,2002 Andi Kleen, SuSE Labs.
  4. */
  5. #include <linux/signal.h>
  6. #include <linux/sched.h>
  7. #include <linux/kernel.h>
  8. #include <linux/errno.h>
  9. #include <linux/string.h>
  10. #include <linux/types.h>
  11. #include <linux/ptrace.h>
  12. #include <linux/mman.h>
  13. #include <linux/mm.h>
  14. #include <linux/smp.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/init.h>
  17. #include <linux/tty.h>
  18. #include <linux/vt_kern.h> /* For unblank_screen() */
  19. #include <linux/compiler.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/module.h>
  22. #include <linux/kprobes.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/kdebug.h>
  25. #include <asm/system.h>
  26. #include <asm/pgalloc.h>
  27. #include <asm/smp.h>
  28. #include <asm/tlbflush.h>
  29. #include <asm/proto.h>
  30. #include <asm-generic/sections.h>
  31. /*
  32. * Page fault error code bits
  33. * bit 0 == 0 means no page found, 1 means protection fault
  34. * bit 1 == 0 means read, 1 means write
  35. * bit 2 == 0 means kernel, 1 means user-mode
  36. * bit 3 == 1 means use of reserved bit detected
  37. * bit 4 == 1 means fault was an instruction fetch
  38. */
  39. #define PF_PROT (1<<0)
  40. #define PF_WRITE (1<<1)
  41. #define PF_USER (1<<2)
  42. #define PF_RSVD (1<<3)
  43. #define PF_INSTR (1<<4)
  44. static inline int notify_page_fault(struct pt_regs *regs)
  45. {
  46. #ifdef CONFIG_KPROBES
  47. int ret = 0;
  48. /* kprobe_running() needs smp_processor_id() */
  49. if (!user_mode(regs)) {
  50. preempt_disable();
  51. if (kprobe_running() && kprobe_fault_handler(regs, 14))
  52. ret = 1;
  53. preempt_enable();
  54. }
  55. return ret;
  56. #else
  57. return 0;
  58. #endif
  59. }
  60. #ifdef CONFIG_X86_32
  61. /*
  62. * Return EIP plus the CS segment base. The segment limit is also
  63. * adjusted, clamped to the kernel/user address space (whichever is
  64. * appropriate), and returned in *eip_limit.
  65. *
  66. * The segment is checked, because it might have been changed by another
  67. * task between the original faulting instruction and here.
  68. *
  69. * If CS is no longer a valid code segment, or if EIP is beyond the
  70. * limit, or if it is a kernel address when CS is not a kernel segment,
  71. * then the returned value will be greater than *eip_limit.
  72. *
  73. * This is slow, but is very rarely executed.
  74. */
  75. static inline unsigned long get_segment_eip(struct pt_regs *regs,
  76. unsigned long *eip_limit)
  77. {
  78. unsigned long ip = regs->ip;
  79. unsigned seg = regs->cs & 0xffff;
  80. u32 seg_ar, seg_limit, base, *desc;
  81. /* Unlikely, but must come before segment checks. */
  82. if (unlikely(regs->flags & VM_MASK)) {
  83. base = seg << 4;
  84. *eip_limit = base + 0xffff;
  85. return base + (ip & 0xffff);
  86. }
  87. /* The standard kernel/user address space limit. */
  88. *eip_limit = user_mode(regs) ? USER_DS.seg : KERNEL_DS.seg;
  89. /* By far the most common cases. */
  90. if (likely(SEGMENT_IS_FLAT_CODE(seg)))
  91. return ip;
  92. /* Check the segment exists, is within the current LDT/GDT size,
  93. that kernel/user (ring 0..3) has the appropriate privilege,
  94. that it's a code segment, and get the limit. */
  95. __asm__("larl %3,%0; lsll %3,%1"
  96. : "=&r" (seg_ar), "=r" (seg_limit) : "0" (0), "rm" (seg));
  97. if ((~seg_ar & 0x9800) || ip > seg_limit) {
  98. *eip_limit = 0;
  99. return 1; /* So that returned ip > *eip_limit. */
  100. }
  101. /* Get the GDT/LDT descriptor base.
  102. When you look for races in this code remember that
  103. LDT and other horrors are only used in user space. */
  104. if (seg & (1<<2)) {
  105. /* Must lock the LDT while reading it. */
  106. mutex_lock(&current->mm->context.lock);
  107. desc = current->mm->context.ldt;
  108. desc = (void *)desc + (seg & ~7);
  109. } else {
  110. /* Must disable preemption while reading the GDT. */
  111. desc = (u32 *)get_cpu_gdt_table(get_cpu());
  112. desc = (void *)desc + (seg & ~7);
  113. }
  114. /* Decode the code segment base from the descriptor */
  115. base = get_desc_base((struct desc_struct *)desc);
  116. if (seg & (1<<2))
  117. mutex_unlock(&current->mm->context.lock);
  118. else
  119. put_cpu();
  120. /* Adjust EIP and segment limit, and clamp at the kernel limit.
  121. It's legitimate for segments to wrap at 0xffffffff. */
  122. seg_limit += base;
  123. if (seg_limit < *eip_limit && seg_limit >= base)
  124. *eip_limit = seg_limit;
  125. return ip + base;
  126. }
  127. #endif
  128. /*
  129. * X86_32
  130. * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
  131. * Check that here and ignore it.
  132. *
  133. * X86_64
  134. * Sometimes the CPU reports invalid exceptions on prefetch.
  135. * Check that here and ignore it.
  136. *
  137. * Opcode checker based on code by Richard Brunner
  138. */
  139. static int is_prefetch(struct pt_regs *regs, unsigned long addr,
  140. unsigned long error_code)
  141. {
  142. unsigned char *instr;
  143. int scan_more = 1;
  144. int prefetch = 0;
  145. unsigned char *max_instr;
  146. #ifdef CONFIG_X86_32
  147. unsigned long limit;
  148. if (unlikely(boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
  149. boot_cpu_data.x86 >= 6)) {
  150. /* Catch an obscure case of prefetch inside an NX page. */
  151. if (nx_enabled && (error_code & PF_INSTR))
  152. return 0;
  153. } else {
  154. return 0;
  155. }
  156. instr = (unsigned char *)get_segment_eip(regs, &limit);
  157. #else
  158. /* If it was a exec fault ignore */
  159. if (error_code & PF_INSTR)
  160. return 0;
  161. instr = (unsigned char __user *)convert_rip_to_linear(current, regs);
  162. #endif
  163. max_instr = instr + 15;
  164. #ifdef CONFIG_X86_64
  165. if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE)
  166. return 0;
  167. #endif
  168. while (scan_more && instr < max_instr) {
  169. unsigned char opcode;
  170. unsigned char instr_hi;
  171. unsigned char instr_lo;
  172. #ifdef CONFIG_X86_32
  173. if (instr > (unsigned char *)limit)
  174. break;
  175. #endif
  176. if (probe_kernel_address(instr, opcode))
  177. break;
  178. instr_hi = opcode & 0xf0;
  179. instr_lo = opcode & 0x0f;
  180. instr++;
  181. switch (instr_hi) {
  182. case 0x20:
  183. case 0x30:
  184. /*
  185. * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
  186. * In X86_64 long mode, the CPU will signal invalid
  187. * opcode if some of these prefixes are present so
  188. * X86_64 will never get here anyway
  189. */
  190. scan_more = ((instr_lo & 7) == 0x6);
  191. break;
  192. #ifdef CONFIG_X86_64
  193. case 0x40:
  194. /*
  195. * In AMD64 long mode 0x40..0x4F are valid REX prefixes
  196. * Need to figure out under what instruction mode the
  197. * instruction was issued. Could check the LDT for lm,
  198. * but for now it's good enough to assume that long
  199. * mode only uses well known segments or kernel.
  200. */
  201. scan_more = (!user_mode(regs)) || (regs->cs == __USER_CS);
  202. break;
  203. #endif
  204. case 0x60:
  205. /* 0x64 thru 0x67 are valid prefixes in all modes. */
  206. scan_more = (instr_lo & 0xC) == 0x4;
  207. break;
  208. case 0xF0:
  209. /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
  210. scan_more = !instr_lo || (instr_lo>>1) == 1;
  211. break;
  212. case 0x00:
  213. /* Prefetch instruction is 0x0F0D or 0x0F18 */
  214. scan_more = 0;
  215. #ifdef CONFIG_X86_32
  216. if (instr > (unsigned char *)limit)
  217. break;
  218. #endif
  219. if (probe_kernel_address(instr, opcode))
  220. break;
  221. prefetch = (instr_lo == 0xF) &&
  222. (opcode == 0x0D || opcode == 0x18);
  223. break;
  224. default:
  225. scan_more = 0;
  226. break;
  227. }
  228. }
  229. return prefetch;
  230. }
  231. static void force_sig_info_fault(int si_signo, int si_code,
  232. unsigned long address, struct task_struct *tsk)
  233. {
  234. siginfo_t info;
  235. info.si_signo = si_signo;
  236. info.si_errno = 0;
  237. info.si_code = si_code;
  238. info.si_addr = (void __user *)address;
  239. force_sig_info(si_signo, &info, tsk);
  240. }
  241. static int bad_address(void *p)
  242. {
  243. unsigned long dummy;
  244. return probe_kernel_address((unsigned long *)p, dummy);
  245. }
  246. void dump_pagetable(unsigned long address)
  247. {
  248. pgd_t *pgd;
  249. pud_t *pud;
  250. pmd_t *pmd;
  251. pte_t *pte;
  252. pgd = (pgd_t *)read_cr3();
  253. pgd = __va((unsigned long)pgd & PHYSICAL_PAGE_MASK);
  254. pgd += pgd_index(address);
  255. if (bad_address(pgd)) goto bad;
  256. printk("PGD %lx ", pgd_val(*pgd));
  257. if (!pgd_present(*pgd)) goto ret;
  258. pud = pud_offset(pgd, address);
  259. if (bad_address(pud)) goto bad;
  260. printk("PUD %lx ", pud_val(*pud));
  261. if (!pud_present(*pud)) goto ret;
  262. pmd = pmd_offset(pud, address);
  263. if (bad_address(pmd)) goto bad;
  264. printk("PMD %lx ", pmd_val(*pmd));
  265. if (!pmd_present(*pmd) || pmd_large(*pmd)) goto ret;
  266. pte = pte_offset_kernel(pmd, address);
  267. if (bad_address(pte)) goto bad;
  268. printk("PTE %lx", pte_val(*pte));
  269. ret:
  270. printk("\n");
  271. return;
  272. bad:
  273. printk("BAD\n");
  274. }
  275. #ifdef CONFIG_X86_64
  276. static const char errata93_warning[] =
  277. KERN_ERR "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
  278. KERN_ERR "******* Working around it, but it may cause SEGVs or burn power.\n"
  279. KERN_ERR "******* Please consider a BIOS update.\n"
  280. KERN_ERR "******* Disabling USB legacy in the BIOS may also help.\n";
  281. /* Workaround for K8 erratum #93 & buggy BIOS.
  282. BIOS SMM functions are required to use a specific workaround
  283. to avoid corruption of the 64bit RIP register on C stepping K8.
  284. A lot of BIOS that didn't get tested properly miss this.
  285. The OS sees this as a page fault with the upper 32bits of RIP cleared.
  286. Try to work around it here.
  287. Note we only handle faults in kernel here. */
  288. static int is_errata93(struct pt_regs *regs, unsigned long address)
  289. {
  290. static int warned;
  291. if (address != regs->ip)
  292. return 0;
  293. if ((address >> 32) != 0)
  294. return 0;
  295. address |= 0xffffffffUL << 32;
  296. if ((address >= (u64)_stext && address <= (u64)_etext) ||
  297. (address >= MODULES_VADDR && address <= MODULES_END)) {
  298. if (!warned) {
  299. printk(errata93_warning);
  300. warned = 1;
  301. }
  302. regs->ip = address;
  303. return 1;
  304. }
  305. return 0;
  306. }
  307. #endif
  308. static noinline void pgtable_bad(unsigned long address, struct pt_regs *regs,
  309. unsigned long error_code)
  310. {
  311. unsigned long flags = oops_begin();
  312. struct task_struct *tsk;
  313. printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
  314. current->comm, address);
  315. dump_pagetable(address);
  316. tsk = current;
  317. tsk->thread.cr2 = address;
  318. tsk->thread.trap_no = 14;
  319. tsk->thread.error_code = error_code;
  320. if (__die("Bad pagetable", regs, error_code))
  321. regs = NULL;
  322. oops_end(flags, regs, SIGKILL);
  323. }
  324. /*
  325. * Handle a fault on the vmalloc area
  326. *
  327. * This assumes no large pages in there.
  328. */
  329. static int vmalloc_fault(unsigned long address)
  330. {
  331. pgd_t *pgd, *pgd_ref;
  332. pud_t *pud, *pud_ref;
  333. pmd_t *pmd, *pmd_ref;
  334. pte_t *pte, *pte_ref;
  335. /* Copy kernel mappings over when needed. This can also
  336. happen within a race in page table update. In the later
  337. case just flush. */
  338. pgd = pgd_offset(current->mm ?: &init_mm, address);
  339. pgd_ref = pgd_offset_k(address);
  340. if (pgd_none(*pgd_ref))
  341. return -1;
  342. if (pgd_none(*pgd))
  343. set_pgd(pgd, *pgd_ref);
  344. else
  345. BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
  346. /* Below here mismatches are bugs because these lower tables
  347. are shared */
  348. pud = pud_offset(pgd, address);
  349. pud_ref = pud_offset(pgd_ref, address);
  350. if (pud_none(*pud_ref))
  351. return -1;
  352. if (pud_none(*pud) || pud_page_vaddr(*pud) != pud_page_vaddr(*pud_ref))
  353. BUG();
  354. pmd = pmd_offset(pud, address);
  355. pmd_ref = pmd_offset(pud_ref, address);
  356. if (pmd_none(*pmd_ref))
  357. return -1;
  358. if (pmd_none(*pmd) || pmd_page(*pmd) != pmd_page(*pmd_ref))
  359. BUG();
  360. pte_ref = pte_offset_kernel(pmd_ref, address);
  361. if (!pte_present(*pte_ref))
  362. return -1;
  363. pte = pte_offset_kernel(pmd, address);
  364. /* Don't use pte_page here, because the mappings can point
  365. outside mem_map, and the NUMA hash lookup cannot handle
  366. that. */
  367. if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
  368. BUG();
  369. return 0;
  370. }
  371. int show_unhandled_signals = 1;
  372. /*
  373. * This routine handles page faults. It determines the address,
  374. * and the problem, and then passes it off to one of the appropriate
  375. * routines.
  376. */
  377. asmlinkage void __kprobes do_page_fault(struct pt_regs *regs,
  378. unsigned long error_code)
  379. {
  380. struct task_struct *tsk;
  381. struct mm_struct *mm;
  382. struct vm_area_struct *vma;
  383. unsigned long address;
  384. int write, fault;
  385. unsigned long flags;
  386. int si_code;
  387. /*
  388. * We can fault from pretty much anywhere, with unknown IRQ state.
  389. */
  390. trace_hardirqs_fixup();
  391. tsk = current;
  392. mm = tsk->mm;
  393. prefetchw(&mm->mmap_sem);
  394. /* get the address */
  395. address = read_cr2();
  396. si_code = SEGV_MAPERR;
  397. /*
  398. * We fault-in kernel-space virtual memory on-demand. The
  399. * 'reference' page table is init_mm.pgd.
  400. *
  401. * NOTE! We MUST NOT take any locks for this case. We may
  402. * be in an interrupt or a critical region, and should
  403. * only copy the information from the master page table,
  404. * nothing more.
  405. *
  406. * This verifies that the fault happens in kernel space
  407. * (error_code & 4) == 0, and that the fault was not a
  408. * protection error (error_code & 9) == 0.
  409. */
  410. if (unlikely(address >= TASK_SIZE64)) {
  411. /*
  412. * Don't check for the module range here: its PML4
  413. * is always initialized because it's shared with the main
  414. * kernel text. Only vmalloc may need PML4 syncups.
  415. */
  416. if (!(error_code & (PF_RSVD|PF_USER|PF_PROT)) &&
  417. ((address >= VMALLOC_START && address < VMALLOC_END))) {
  418. if (vmalloc_fault(address) >= 0)
  419. return;
  420. }
  421. if (notify_page_fault(regs))
  422. return;
  423. /*
  424. * Don't take the mm semaphore here. If we fixup a prefetch
  425. * fault we could otherwise deadlock.
  426. */
  427. goto bad_area_nosemaphore;
  428. }
  429. if (notify_page_fault(regs))
  430. return;
  431. if (likely(regs->flags & X86_EFLAGS_IF))
  432. local_irq_enable();
  433. if (unlikely(error_code & PF_RSVD))
  434. pgtable_bad(address, regs, error_code);
  435. /*
  436. * If we're in an interrupt, have no user context or are running in an
  437. * atomic region then we must not take the fault.
  438. */
  439. if (unlikely(in_atomic() || !mm))
  440. goto bad_area_nosemaphore;
  441. /*
  442. * User-mode registers count as a user access even for any
  443. * potential system fault or CPU buglet.
  444. */
  445. if (user_mode_vm(regs))
  446. error_code |= PF_USER;
  447. again:
  448. /* When running in the kernel we expect faults to occur only to
  449. * addresses in user space. All other faults represent errors in the
  450. * kernel and should generate an OOPS. Unfortunately, in the case of an
  451. * erroneous fault occurring in a code path which already holds mmap_sem
  452. * we will deadlock attempting to validate the fault against the
  453. * address space. Luckily the kernel only validly references user
  454. * space from well defined areas of code, which are listed in the
  455. * exceptions table.
  456. *
  457. * As the vast majority of faults will be valid we will only perform
  458. * the source reference check when there is a possibility of a deadlock.
  459. * Attempt to lock the address space, if we cannot we then validate the
  460. * source. If this is invalid we can skip the address space check,
  461. * thus avoiding the deadlock.
  462. */
  463. if (!down_read_trylock(&mm->mmap_sem)) {
  464. if ((error_code & PF_USER) == 0 &&
  465. !search_exception_tables(regs->ip))
  466. goto bad_area_nosemaphore;
  467. down_read(&mm->mmap_sem);
  468. }
  469. vma = find_vma(mm, address);
  470. if (!vma)
  471. goto bad_area;
  472. if (likely(vma->vm_start <= address))
  473. goto good_area;
  474. if (!(vma->vm_flags & VM_GROWSDOWN))
  475. goto bad_area;
  476. if (error_code & PF_USER) {
  477. /* Allow userspace just enough access below the stack pointer
  478. * to let the 'enter' instruction work.
  479. */
  480. if (address + 65536 + 32 * sizeof(unsigned long) < regs->sp)
  481. goto bad_area;
  482. }
  483. if (expand_stack(vma, address))
  484. goto bad_area;
  485. /*
  486. * Ok, we have a good vm_area for this memory access, so
  487. * we can handle it..
  488. */
  489. good_area:
  490. si_code = SEGV_ACCERR;
  491. write = 0;
  492. switch (error_code & (PF_PROT|PF_WRITE)) {
  493. default: /* 3: write, present */
  494. /* fall through */
  495. case PF_WRITE: /* write, not present */
  496. if (!(vma->vm_flags & VM_WRITE))
  497. goto bad_area;
  498. write++;
  499. break;
  500. case PF_PROT: /* read, present */
  501. goto bad_area;
  502. case 0: /* read, not present */
  503. if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
  504. goto bad_area;
  505. }
  506. /*
  507. * If for any reason at all we couldn't handle the fault,
  508. * make sure we exit gracefully rather than endlessly redo
  509. * the fault.
  510. */
  511. fault = handle_mm_fault(mm, vma, address, write);
  512. if (unlikely(fault & VM_FAULT_ERROR)) {
  513. if (fault & VM_FAULT_OOM)
  514. goto out_of_memory;
  515. else if (fault & VM_FAULT_SIGBUS)
  516. goto do_sigbus;
  517. BUG();
  518. }
  519. if (fault & VM_FAULT_MAJOR)
  520. tsk->maj_flt++;
  521. else
  522. tsk->min_flt++;
  523. up_read(&mm->mmap_sem);
  524. return;
  525. /*
  526. * Something tried to access memory that isn't in our memory map..
  527. * Fix it, but check if it's kernel or user first..
  528. */
  529. bad_area:
  530. up_read(&mm->mmap_sem);
  531. bad_area_nosemaphore:
  532. /* User mode accesses just cause a SIGSEGV */
  533. if (error_code & PF_USER) {
  534. /*
  535. * It's possible to have interrupts off here.
  536. */
  537. local_irq_enable();
  538. if (is_prefetch(regs, address, error_code))
  539. return;
  540. /* Work around K8 erratum #100 K8 in compat mode
  541. occasionally jumps to illegal addresses >4GB. We
  542. catch this here in the page fault handler because
  543. these addresses are not reachable. Just detect this
  544. case and return. Any code segment in LDT is
  545. compatibility mode. */
  546. if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) &&
  547. (address >> 32))
  548. return;
  549. if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
  550. printk_ratelimit()) {
  551. printk(
  552. "%s%s[%d]: segfault at %lx ip %lx sp %lx error %lx\n",
  553. tsk->pid > 1 ? KERN_INFO : KERN_EMERG,
  554. tsk->comm, tsk->pid, address, regs->ip,
  555. regs->sp, error_code);
  556. }
  557. tsk->thread.cr2 = address;
  558. /* Kernel addresses are always protection faults */
  559. tsk->thread.error_code = error_code | (address >= TASK_SIZE);
  560. tsk->thread.trap_no = 14;
  561. force_sig_info_fault(SIGSEGV, si_code, address, tsk);
  562. return;
  563. }
  564. no_context:
  565. /* Are we prepared to handle this kernel fault? */
  566. if (fixup_exception(regs))
  567. return;
  568. /*
  569. * Hall of shame of CPU/BIOS bugs.
  570. */
  571. if (is_prefetch(regs, address, error_code))
  572. return;
  573. if (is_errata93(regs, address))
  574. return;
  575. /*
  576. * Oops. The kernel tried to access some bad page. We'll have to
  577. * terminate things with extreme prejudice.
  578. */
  579. flags = oops_begin();
  580. if (address < PAGE_SIZE)
  581. printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
  582. else
  583. printk(KERN_ALERT "Unable to handle kernel paging request");
  584. printk(" at %016lx RIP: \n" KERN_ALERT, address);
  585. printk_address(regs->ip);
  586. dump_pagetable(address);
  587. tsk->thread.cr2 = address;
  588. tsk->thread.trap_no = 14;
  589. tsk->thread.error_code = error_code;
  590. if (__die("Oops", regs, error_code))
  591. regs = NULL;
  592. /* Executive summary in case the body of the oops scrolled away */
  593. printk(KERN_EMERG "CR2: %016lx\n", address);
  594. oops_end(flags, regs, SIGKILL);
  595. /*
  596. * We ran out of memory, or some other thing happened to us that made
  597. * us unable to handle the page fault gracefully.
  598. */
  599. out_of_memory:
  600. up_read(&mm->mmap_sem);
  601. if (is_global_init(current)) {
  602. yield();
  603. goto again;
  604. }
  605. printk("VM: killing process %s\n", tsk->comm);
  606. if (error_code & 4)
  607. do_group_exit(SIGKILL);
  608. goto no_context;
  609. do_sigbus:
  610. up_read(&mm->mmap_sem);
  611. /* Kernel mode? Handle exceptions or die */
  612. if (!(error_code & PF_USER))
  613. goto no_context;
  614. tsk->thread.cr2 = address;
  615. tsk->thread.error_code = error_code;
  616. tsk->thread.trap_no = 14;
  617. force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk);
  618. return;
  619. }
  620. DEFINE_SPINLOCK(pgd_lock);
  621. LIST_HEAD(pgd_list);
  622. void vmalloc_sync_all(void)
  623. {
  624. /* Note that races in the updates of insync and start aren't
  625. problematic:
  626. insync can only get set bits added, and updates to start are only
  627. improving performance (without affecting correctness if undone). */
  628. static DECLARE_BITMAP(insync, PTRS_PER_PGD);
  629. static unsigned long start = VMALLOC_START & PGDIR_MASK;
  630. unsigned long address;
  631. for (address = start; address <= VMALLOC_END; address += PGDIR_SIZE) {
  632. if (!test_bit(pgd_index(address), insync)) {
  633. const pgd_t *pgd_ref = pgd_offset_k(address);
  634. struct page *page;
  635. if (pgd_none(*pgd_ref))
  636. continue;
  637. spin_lock(&pgd_lock);
  638. list_for_each_entry(page, &pgd_list, lru) {
  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_vaddr(*pgd) != pgd_page_vaddr(*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. }