fault_32.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. /*
  2. * Copyright (C) 1995 Linus Torvalds
  3. */
  4. #include <linux/signal.h>
  5. #include <linux/sched.h>
  6. #include <linux/kernel.h>
  7. #include <linux/errno.h>
  8. #include <linux/string.h>
  9. #include <linux/types.h>
  10. #include <linux/ptrace.h>
  11. #include <linux/mman.h>
  12. #include <linux/mm.h>
  13. #include <linux/smp.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/init.h>
  16. #include <linux/tty.h>
  17. #include <linux/vt_kern.h> /* For unblank_screen() */
  18. #include <linux/highmem.h>
  19. #include <linux/bootmem.h> /* for max_low_pfn */
  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/desc.h>
  27. #include <asm/segment.h>
  28. /*
  29. * Page fault error code bits
  30. * bit 0 == 0 means no page found, 1 means protection fault
  31. * bit 1 == 0 means read, 1 means write
  32. * bit 2 == 0 means kernel, 1 means user-mode
  33. * bit 3 == 1 means use of reserved bit detected
  34. * bit 4 == 1 means fault was an instruction fetch
  35. */
  36. #define PF_PROT (1<<0)
  37. #define PF_WRITE (1<<1)
  38. #define PF_USER (1<<2)
  39. #define PF_RSVD (1<<3)
  40. #define PF_INSTR (1<<4)
  41. static inline int notify_page_fault(struct pt_regs *regs)
  42. {
  43. #ifdef CONFIG_KPROBES
  44. int ret = 0;
  45. /* kprobe_running() needs smp_processor_id() */
  46. if (!user_mode_vm(regs)) {
  47. preempt_disable();
  48. if (kprobe_running() && kprobe_fault_handler(regs, 14))
  49. ret = 1;
  50. preempt_enable();
  51. }
  52. return ret;
  53. #else
  54. return 0;
  55. #endif
  56. }
  57. /*
  58. * X86_32
  59. * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
  60. * Check that here and ignore it.
  61. *
  62. * X86_64
  63. * Sometimes the CPU reports invalid exceptions on prefetch.
  64. * Check that here and ignore it.
  65. *
  66. * Opcode checker based on code by Richard Brunner
  67. */
  68. static int is_prefetch(struct pt_regs *regs, unsigned long addr,
  69. unsigned long error_code)
  70. {
  71. unsigned char *instr;
  72. int scan_more = 1;
  73. int prefetch = 0;
  74. unsigned char *max_instr;
  75. #ifdef CONFIG_X86_32
  76. if (unlikely(boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
  77. boot_cpu_data.x86 >= 6)) {
  78. /* Catch an obscure case of prefetch inside an NX page. */
  79. if (nx_enabled && (error_code & PF_INSTR))
  80. return 0;
  81. } else {
  82. return 0;
  83. }
  84. #else
  85. /* If it was a exec fault ignore */
  86. if (error_code & PF_INSTR)
  87. return 0;
  88. #endif
  89. instr = (unsigned char *)convert_ip_to_linear(current, regs);
  90. max_instr = instr + 15;
  91. if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE)
  92. return 0;
  93. while (scan_more && instr < max_instr) {
  94. unsigned char opcode;
  95. unsigned char instr_hi;
  96. unsigned char instr_lo;
  97. if (probe_kernel_address(instr, opcode))
  98. break;
  99. instr_hi = opcode & 0xf0;
  100. instr_lo = opcode & 0x0f;
  101. instr++;
  102. switch (instr_hi) {
  103. case 0x20:
  104. case 0x30:
  105. /*
  106. * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
  107. * In X86_64 long mode, the CPU will signal invalid
  108. * opcode if some of these prefixes are present so
  109. * X86_64 will never get here anyway
  110. */
  111. scan_more = ((instr_lo & 7) == 0x6);
  112. break;
  113. #ifdef CONFIG_X86_64
  114. case 0x40:
  115. /*
  116. * In AMD64 long mode 0x40..0x4F are valid REX prefixes
  117. * Need to figure out under what instruction mode the
  118. * instruction was issued. Could check the LDT for lm,
  119. * but for now it's good enough to assume that long
  120. * mode only uses well known segments or kernel.
  121. */
  122. scan_more = (!user_mode(regs)) || (regs->cs == __USER_CS);
  123. break;
  124. #endif
  125. case 0x60:
  126. /* 0x64 thru 0x67 are valid prefixes in all modes. */
  127. scan_more = (instr_lo & 0xC) == 0x4;
  128. break;
  129. case 0xF0:
  130. /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
  131. scan_more = !instr_lo || (instr_lo>>1) == 1;
  132. break;
  133. case 0x00:
  134. /* Prefetch instruction is 0x0F0D or 0x0F18 */
  135. scan_more = 0;
  136. if (probe_kernel_address(instr, opcode))
  137. break;
  138. prefetch = (instr_lo == 0xF) &&
  139. (opcode == 0x0D || opcode == 0x18);
  140. break;
  141. default:
  142. scan_more = 0;
  143. break;
  144. }
  145. }
  146. return prefetch;
  147. }
  148. static void force_sig_info_fault(int si_signo, int si_code,
  149. unsigned long address, struct task_struct *tsk)
  150. {
  151. siginfo_t info;
  152. info.si_signo = si_signo;
  153. info.si_errno = 0;
  154. info.si_code = si_code;
  155. info.si_addr = (void __user *)address;
  156. force_sig_info(si_signo, &info, tsk);
  157. }
  158. void dump_pagetable(unsigned long address)
  159. {
  160. __typeof__(pte_val(__pte(0))) page;
  161. page = read_cr3();
  162. page = ((__typeof__(page) *) __va(page))[address >> PGDIR_SHIFT];
  163. #ifdef CONFIG_X86_PAE
  164. printk("*pdpt = %016Lx ", page);
  165. if ((page >> PAGE_SHIFT) < max_low_pfn
  166. && page & _PAGE_PRESENT) {
  167. page &= PAGE_MASK;
  168. page = ((__typeof__(page) *) __va(page))[(address >> PMD_SHIFT)
  169. & (PTRS_PER_PMD - 1)];
  170. printk(KERN_CONT "*pde = %016Lx ", page);
  171. page &= ~_PAGE_NX;
  172. }
  173. #else
  174. printk("*pde = %08lx ", page);
  175. #endif
  176. /*
  177. * We must not directly access the pte in the highpte
  178. * case if the page table is located in highmem.
  179. * And let's rather not kmap-atomic the pte, just in case
  180. * it's allocated already.
  181. */
  182. if ((page >> PAGE_SHIFT) < max_low_pfn
  183. && (page & _PAGE_PRESENT)
  184. && !(page & _PAGE_PSE)) {
  185. page &= PAGE_MASK;
  186. page = ((__typeof__(page) *) __va(page))[(address >> PAGE_SHIFT)
  187. & (PTRS_PER_PTE - 1)];
  188. printk("*pte = %0*Lx ", sizeof(page)*2, (u64)page);
  189. }
  190. printk("\n");
  191. }
  192. static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
  193. {
  194. unsigned index = pgd_index(address);
  195. pgd_t *pgd_k;
  196. pud_t *pud, *pud_k;
  197. pmd_t *pmd, *pmd_k;
  198. pgd += index;
  199. pgd_k = init_mm.pgd + index;
  200. if (!pgd_present(*pgd_k))
  201. return NULL;
  202. /*
  203. * set_pgd(pgd, *pgd_k); here would be useless on PAE
  204. * and redundant with the set_pmd() on non-PAE. As would
  205. * set_pud.
  206. */
  207. pud = pud_offset(pgd, address);
  208. pud_k = pud_offset(pgd_k, address);
  209. if (!pud_present(*pud_k))
  210. return NULL;
  211. pmd = pmd_offset(pud, address);
  212. pmd_k = pmd_offset(pud_k, address);
  213. if (!pmd_present(*pmd_k))
  214. return NULL;
  215. if (!pmd_present(*pmd)) {
  216. set_pmd(pmd, *pmd_k);
  217. arch_flush_lazy_mmu_mode();
  218. } else
  219. BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
  220. return pmd_k;
  221. }
  222. #ifdef CONFIG_X86_64
  223. static const char errata93_warning[] =
  224. KERN_ERR "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
  225. KERN_ERR "******* Working around it, but it may cause SEGVs or burn power.\n"
  226. KERN_ERR "******* Please consider a BIOS update.\n"
  227. KERN_ERR "******* Disabling USB legacy in the BIOS may also help.\n";
  228. #endif
  229. /* Workaround for K8 erratum #93 & buggy BIOS.
  230. BIOS SMM functions are required to use a specific workaround
  231. to avoid corruption of the 64bit RIP register on C stepping K8.
  232. A lot of BIOS that didn't get tested properly miss this.
  233. The OS sees this as a page fault with the upper 32bits of RIP cleared.
  234. Try to work around it here.
  235. Note we only handle faults in kernel here.
  236. Does nothing for X86_32
  237. */
  238. static int is_errata93(struct pt_regs *regs, unsigned long address)
  239. {
  240. #ifdef CONFIG_X86_64
  241. static int warned;
  242. if (address != regs->ip)
  243. return 0;
  244. if ((address >> 32) != 0)
  245. return 0;
  246. address |= 0xffffffffUL << 32;
  247. if ((address >= (u64)_stext && address <= (u64)_etext) ||
  248. (address >= MODULES_VADDR && address <= MODULES_END)) {
  249. if (!warned) {
  250. printk(errata93_warning);
  251. warned = 1;
  252. }
  253. regs->ip = address;
  254. return 1;
  255. }
  256. #endif
  257. return 0;
  258. }
  259. /*
  260. * Work around K8 erratum #100 K8 in compat mode occasionally jumps to illegal
  261. * addresses >4GB. We catch this in the page fault handler because these
  262. * addresses are not reachable. Just detect this case and return. Any code
  263. * segment in LDT is compatibility mode.
  264. */
  265. static int is_errata100(struct pt_regs *regs, unsigned long address)
  266. {
  267. #ifdef CONFIG_X86_64
  268. if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) &&
  269. (address >> 32))
  270. return 1;
  271. #endif
  272. return 0;
  273. }
  274. void do_invalid_op(struct pt_regs *, unsigned long);
  275. static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
  276. {
  277. #ifdef CONFIG_X86_F00F_BUG
  278. unsigned long nr;
  279. /*
  280. * Pentium F0 0F C7 C8 bug workaround.
  281. */
  282. if (boot_cpu_data.f00f_bug) {
  283. nr = (address - idt_descr.address) >> 3;
  284. if (nr == 6) {
  285. do_invalid_op(regs, 0);
  286. return 1;
  287. }
  288. }
  289. #endif
  290. return 0;
  291. }
  292. static void show_fault_oops(struct pt_regs *regs, unsigned long error_code,
  293. unsigned long address)
  294. {
  295. if (!oops_may_print())
  296. return;
  297. #ifdef CONFIG_X86_PAE
  298. if (error_code & PF_INSTR) {
  299. int level;
  300. pte_t *pte = lookup_address(address, &level);
  301. if (pte && pte_present(*pte) && !pte_exec(*pte))
  302. printk(KERN_CRIT "kernel tried to execute "
  303. "NX-protected page - exploit attempt? "
  304. "(uid: %d)\n", current->uid);
  305. }
  306. #endif
  307. if (address < PAGE_SIZE)
  308. printk(KERN_ALERT "BUG: unable to handle kernel NULL "
  309. "pointer dereference");
  310. else
  311. printk(KERN_ALERT "BUG: unable to handle kernel paging"
  312. " request");
  313. printk(" at virtual address %08lx\n", address);
  314. printk(KERN_ALERT "printing ip: %08lx ", regs->ip);
  315. dump_pagetable(address);
  316. }
  317. /*
  318. * Handle a fault on the vmalloc or module mapping area
  319. *
  320. * This assumes no large pages in there.
  321. */
  322. static inline int vmalloc_fault(unsigned long address)
  323. {
  324. #ifdef CONFIG_X86_32
  325. unsigned long pgd_paddr;
  326. pmd_t *pmd_k;
  327. pte_t *pte_k;
  328. /*
  329. * Synchronize this task's top level page-table
  330. * with the 'reference' page table.
  331. *
  332. * Do _not_ use "current" here. We might be inside
  333. * an interrupt in the middle of a task switch..
  334. */
  335. pgd_paddr = read_cr3();
  336. pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
  337. if (!pmd_k)
  338. return -1;
  339. pte_k = pte_offset_kernel(pmd_k, address);
  340. if (!pte_present(*pte_k))
  341. return -1;
  342. return 0;
  343. #else
  344. pgd_t *pgd, *pgd_ref;
  345. pud_t *pud, *pud_ref;
  346. pmd_t *pmd, *pmd_ref;
  347. pte_t *pte, *pte_ref;
  348. /* Copy kernel mappings over when needed. This can also
  349. happen within a race in page table update. In the later
  350. case just flush. */
  351. pgd = pgd_offset(current->mm ?: &init_mm, address);
  352. pgd_ref = pgd_offset_k(address);
  353. if (pgd_none(*pgd_ref))
  354. return -1;
  355. if (pgd_none(*pgd))
  356. set_pgd(pgd, *pgd_ref);
  357. else
  358. BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
  359. /* Below here mismatches are bugs because these lower tables
  360. are shared */
  361. pud = pud_offset(pgd, address);
  362. pud_ref = pud_offset(pgd_ref, address);
  363. if (pud_none(*pud_ref))
  364. return -1;
  365. if (pud_none(*pud) || pud_page_vaddr(*pud) != pud_page_vaddr(*pud_ref))
  366. BUG();
  367. pmd = pmd_offset(pud, address);
  368. pmd_ref = pmd_offset(pud_ref, address);
  369. if (pmd_none(*pmd_ref))
  370. return -1;
  371. if (pmd_none(*pmd) || pmd_page(*pmd) != pmd_page(*pmd_ref))
  372. BUG();
  373. pte_ref = pte_offset_kernel(pmd_ref, address);
  374. if (!pte_present(*pte_ref))
  375. return -1;
  376. pte = pte_offset_kernel(pmd, address);
  377. /* Don't use pte_page here, because the mappings can point
  378. outside mem_map, and the NUMA hash lookup cannot handle
  379. that. */
  380. if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
  381. BUG();
  382. return 0;
  383. #endif
  384. }
  385. int show_unhandled_signals = 1;
  386. /*
  387. * This routine handles page faults. It determines the address,
  388. * and the problem, and then passes it off to one of the appropriate
  389. * routines.
  390. */
  391. void __kprobes do_page_fault(struct pt_regs *regs, unsigned long error_code)
  392. {
  393. struct task_struct *tsk;
  394. struct mm_struct *mm;
  395. struct vm_area_struct *vma;
  396. unsigned long address;
  397. int write, si_code;
  398. int fault;
  399. /*
  400. * We can fault from pretty much anywhere, with unknown IRQ state.
  401. */
  402. trace_hardirqs_fixup();
  403. tsk = current;
  404. mm = tsk->mm;
  405. prefetchw(&mm->mmap_sem);
  406. /* get the address */
  407. address = read_cr2();
  408. si_code = SEGV_MAPERR;
  409. if (notify_page_fault(regs))
  410. return;
  411. /*
  412. * We fault-in kernel-space virtual memory on-demand. The
  413. * 'reference' page table is init_mm.pgd.
  414. *
  415. * NOTE! We MUST NOT take any locks for this case. We may
  416. * be in an interrupt or a critical region, and should
  417. * only copy the information from the master page table,
  418. * nothing more.
  419. *
  420. * This verifies that the fault happens in kernel space
  421. * (error_code & 4) == 0, and that the fault was not a
  422. * protection error (error_code & 9) == 0.
  423. */
  424. if (unlikely(address >= TASK_SIZE)) {
  425. if (!(error_code & (PF_RSVD|PF_USER|PF_PROT)) &&
  426. vmalloc_fault(address) >= 0)
  427. return;
  428. /*
  429. * Don't take the mm semaphore here. If we fixup a prefetch
  430. * fault we could otherwise deadlock.
  431. */
  432. goto bad_area_nosemaphore;
  433. }
  434. /* It's safe to allow irq's after cr2 has been saved and the vmalloc
  435. fault has been handled. */
  436. if (regs->flags & (X86_EFLAGS_IF|VM_MASK))
  437. local_irq_enable();
  438. /*
  439. * If we're in an interrupt, have no user context or are running in an
  440. * atomic region then we must not take the fault.
  441. */
  442. if (in_atomic() || !mm)
  443. goto bad_area_nosemaphore;
  444. /* When running in the kernel we expect faults to occur only to
  445. * addresses in user space. All other faults represent errors in the
  446. * kernel and should generate an OOPS. Unfortunately, in the case of an
  447. * erroneous fault occurring in a code path which already holds mmap_sem
  448. * we will deadlock attempting to validate the fault against the
  449. * address space. Luckily the kernel only validly references user
  450. * space from well defined areas of code, which are listed in the
  451. * exceptions table.
  452. *
  453. * As the vast majority of faults will be valid we will only perform
  454. * the source reference check when there is a possibility of a deadlock.
  455. * Attempt to lock the address space, if we cannot we then validate the
  456. * source. If this is invalid we can skip the address space check,
  457. * thus avoiding the deadlock.
  458. */
  459. if (!down_read_trylock(&mm->mmap_sem)) {
  460. if ((error_code & PF_USER) == 0 &&
  461. !search_exception_tables(regs->ip))
  462. goto bad_area_nosemaphore;
  463. down_read(&mm->mmap_sem);
  464. }
  465. vma = find_vma(mm, address);
  466. if (!vma)
  467. goto bad_area;
  468. if (vma->vm_start <= address)
  469. goto good_area;
  470. if (!(vma->vm_flags & VM_GROWSDOWN))
  471. goto bad_area;
  472. if (error_code & PF_USER) {
  473. /*
  474. * Accessing the stack below %sp is always a bug.
  475. * The large cushion allows instructions like enter
  476. * and pusha to work. ("enter $65535,$31" pushes
  477. * 32 pointers and then decrements %sp by 65535.)
  478. */
  479. if (address + 65536 + 32 * sizeof(unsigned long) < regs->sp)
  480. goto bad_area;
  481. }
  482. if (expand_stack(vma, address))
  483. goto bad_area;
  484. /*
  485. * Ok, we have a good vm_area for this memory access, so
  486. * we can handle it..
  487. */
  488. good_area:
  489. si_code = SEGV_ACCERR;
  490. write = 0;
  491. switch (error_code & (PF_PROT|PF_WRITE)) {
  492. default: /* 3: write, present */
  493. /* fall through */
  494. case PF_WRITE: /* write, not present */
  495. if (!(vma->vm_flags & VM_WRITE))
  496. goto bad_area;
  497. write++;
  498. break;
  499. case PF_PROT: /* read, present */
  500. goto bad_area;
  501. case 0: /* read, not present */
  502. if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
  503. goto bad_area;
  504. }
  505. survive:
  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. #ifdef CONFIG_X86_32
  524. /*
  525. * Did it hit the DOS screen memory VA from vm86 mode?
  526. */
  527. if (v8086_mode(regs)) {
  528. unsigned long bit = (address - 0xA0000) >> PAGE_SHIFT;
  529. if (bit < 32)
  530. tsk->thread.screen_bitmap |= 1 << bit;
  531. }
  532. #endif
  533. up_read(&mm->mmap_sem);
  534. return;
  535. /*
  536. * Something tried to access memory that isn't in our memory map..
  537. * Fix it, but check if it's kernel or user first..
  538. */
  539. bad_area:
  540. up_read(&mm->mmap_sem);
  541. bad_area_nosemaphore:
  542. /* User mode accesses just cause a SIGSEGV */
  543. if (error_code & PF_USER) {
  544. /*
  545. * It's possible to have interrupts off here.
  546. */
  547. local_irq_enable();
  548. /*
  549. * Valid to do another page fault here because this one came
  550. * from user space.
  551. */
  552. if (is_prefetch(regs, address, error_code))
  553. return;
  554. if (is_errata100(regs, address))
  555. return;
  556. if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
  557. printk_ratelimit()) {
  558. printk(
  559. #ifdef CONFIG_X86_32
  560. "%s%s[%d]: segfault at %lx ip %08lx sp %08lx error %lx",
  561. #else
  562. "%s%s[%d]: segfault at %lx ip %lx sp %lx error %lx",
  563. #endif
  564. task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
  565. tsk->comm, task_pid_nr(tsk), address, regs->ip,
  566. regs->sp, error_code);
  567. print_vma_addr(" in ", regs->ip);
  568. printk("\n");
  569. }
  570. tsk->thread.cr2 = address;
  571. /* Kernel addresses are always protection faults */
  572. tsk->thread.error_code = error_code | (address >= TASK_SIZE);
  573. tsk->thread.trap_no = 14;
  574. force_sig_info_fault(SIGSEGV, si_code, address, tsk);
  575. return;
  576. }
  577. if (is_f00f_bug(regs, address))
  578. return;
  579. no_context:
  580. /* Are we prepared to handle this kernel fault? */
  581. if (fixup_exception(regs))
  582. return;
  583. /*
  584. * Valid to do another page fault here, because if this fault
  585. * had been triggered by is_prefetch fixup_exception would have
  586. * handled it.
  587. */
  588. if (is_prefetch(regs, address, error_code))
  589. return;
  590. if (is_errata93(regs, address))
  591. return;
  592. /*
  593. * Oops. The kernel tried to access some bad page. We'll have to
  594. * terminate things with extreme prejudice.
  595. */
  596. bust_spinlocks(1);
  597. show_fault_oops(regs, error_code, address);
  598. tsk->thread.cr2 = address;
  599. tsk->thread.trap_no = 14;
  600. tsk->thread.error_code = error_code;
  601. die("Oops", regs, error_code);
  602. bust_spinlocks(0);
  603. do_exit(SIGKILL);
  604. /*
  605. * We ran out of memory, or some other thing happened to us that made
  606. * us unable to handle the page fault gracefully.
  607. */
  608. out_of_memory:
  609. up_read(&mm->mmap_sem);
  610. if (is_global_init(tsk)) {
  611. yield();
  612. down_read(&mm->mmap_sem);
  613. goto survive;
  614. }
  615. printk("VM: killing process %s\n", tsk->comm);
  616. if (error_code & PF_USER)
  617. do_group_exit(SIGKILL);
  618. goto no_context;
  619. do_sigbus:
  620. up_read(&mm->mmap_sem);
  621. /* Kernel mode? Handle exceptions or die */
  622. if (!(error_code & PF_USER))
  623. goto no_context;
  624. /* User space => ok to do another page fault */
  625. if (is_prefetch(regs, address, error_code))
  626. return;
  627. tsk->thread.cr2 = address;
  628. tsk->thread.error_code = error_code;
  629. tsk->thread.trap_no = 14;
  630. force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk);
  631. }
  632. void vmalloc_sync_all(void)
  633. {
  634. /*
  635. * Note that races in the updates of insync and start aren't
  636. * problematic: insync can only get set bits added, and updates to
  637. * start are only improving performance (without affecting correctness
  638. * if undone).
  639. */
  640. static DECLARE_BITMAP(insync, PTRS_PER_PGD);
  641. static unsigned long start = TASK_SIZE;
  642. unsigned long address;
  643. if (SHARED_KERNEL_PMD)
  644. return;
  645. BUILD_BUG_ON(TASK_SIZE & ~PGDIR_MASK);
  646. for (address = start; address >= TASK_SIZE; address += PGDIR_SIZE) {
  647. if (!test_bit(pgd_index(address), insync)) {
  648. unsigned long flags;
  649. struct page *page;
  650. spin_lock_irqsave(&pgd_lock, flags);
  651. for (page = pgd_list; page; page =
  652. (struct page *)page->index)
  653. if (!vmalloc_sync_one(page_address(page),
  654. address)) {
  655. BUG_ON(page != pgd_list);
  656. break;
  657. }
  658. spin_unlock_irqrestore(&pgd_lock, flags);
  659. if (!page)
  660. set_bit(pgd_index(address), insync);
  661. }
  662. if (address == start && test_bit(pgd_index(address), insync))
  663. start = address + PGDIR_SIZE;
  664. }
  665. }