fault_32.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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. #ifdef CONFIG_X86_64
  159. static int bad_address(void *p)
  160. {
  161. unsigned long dummy;
  162. return probe_kernel_address((unsigned long *)p, dummy);
  163. }
  164. #endif
  165. void dump_pagetable(unsigned long address)
  166. {
  167. #ifdef CONFIG_X86_32
  168. __typeof__(pte_val(__pte(0))) page;
  169. page = read_cr3();
  170. page = ((__typeof__(page) *) __va(page))[address >> PGDIR_SHIFT];
  171. #ifdef CONFIG_X86_PAE
  172. printk("*pdpt = %016Lx ", page);
  173. if ((page >> PAGE_SHIFT) < max_low_pfn
  174. && page & _PAGE_PRESENT) {
  175. page &= PAGE_MASK;
  176. page = ((__typeof__(page) *) __va(page))[(address >> PMD_SHIFT)
  177. & (PTRS_PER_PMD - 1)];
  178. printk(KERN_CONT "*pde = %016Lx ", page);
  179. page &= ~_PAGE_NX;
  180. }
  181. #else
  182. printk("*pde = %08lx ", page);
  183. #endif
  184. /*
  185. * We must not directly access the pte in the highpte
  186. * case if the page table is located in highmem.
  187. * And let's rather not kmap-atomic the pte, just in case
  188. * it's allocated already.
  189. */
  190. if ((page >> PAGE_SHIFT) < max_low_pfn
  191. && (page & _PAGE_PRESENT)
  192. && !(page & _PAGE_PSE)) {
  193. page &= PAGE_MASK;
  194. page = ((__typeof__(page) *) __va(page))[(address >> PAGE_SHIFT)
  195. & (PTRS_PER_PTE - 1)];
  196. printk("*pte = %0*Lx ", sizeof(page)*2, (u64)page);
  197. }
  198. printk("\n");
  199. #else /* CONFIG_X86_64 */
  200. pgd_t *pgd;
  201. pud_t *pud;
  202. pmd_t *pmd;
  203. pte_t *pte;
  204. pgd = (pgd_t *)read_cr3();
  205. pgd = __va((unsigned long)pgd & PHYSICAL_PAGE_MASK);
  206. pgd += pgd_index(address);
  207. if (bad_address(pgd)) goto bad;
  208. printk("PGD %lx ", pgd_val(*pgd));
  209. if (!pgd_present(*pgd)) goto ret;
  210. pud = pud_offset(pgd, address);
  211. if (bad_address(pud)) goto bad;
  212. printk("PUD %lx ", pud_val(*pud));
  213. if (!pud_present(*pud)) goto ret;
  214. pmd = pmd_offset(pud, address);
  215. if (bad_address(pmd)) goto bad;
  216. printk("PMD %lx ", pmd_val(*pmd));
  217. if (!pmd_present(*pmd) || pmd_large(*pmd)) goto ret;
  218. pte = pte_offset_kernel(pmd, address);
  219. if (bad_address(pte)) goto bad;
  220. printk("PTE %lx", pte_val(*pte));
  221. ret:
  222. printk("\n");
  223. return;
  224. bad:
  225. printk("BAD\n");
  226. #endif
  227. }
  228. #ifdef CONFIG_X86_32
  229. static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
  230. {
  231. unsigned index = pgd_index(address);
  232. pgd_t *pgd_k;
  233. pud_t *pud, *pud_k;
  234. pmd_t *pmd, *pmd_k;
  235. pgd += index;
  236. pgd_k = init_mm.pgd + index;
  237. if (!pgd_present(*pgd_k))
  238. return NULL;
  239. /*
  240. * set_pgd(pgd, *pgd_k); here would be useless on PAE
  241. * and redundant with the set_pmd() on non-PAE. As would
  242. * set_pud.
  243. */
  244. pud = pud_offset(pgd, address);
  245. pud_k = pud_offset(pgd_k, address);
  246. if (!pud_present(*pud_k))
  247. return NULL;
  248. pmd = pmd_offset(pud, address);
  249. pmd_k = pmd_offset(pud_k, address);
  250. if (!pmd_present(*pmd_k))
  251. return NULL;
  252. if (!pmd_present(*pmd)) {
  253. set_pmd(pmd, *pmd_k);
  254. arch_flush_lazy_mmu_mode();
  255. } else
  256. BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
  257. return pmd_k;
  258. }
  259. #endif
  260. #ifdef CONFIG_X86_64
  261. static const char errata93_warning[] =
  262. KERN_ERR "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
  263. KERN_ERR "******* Working around it, but it may cause SEGVs or burn power.\n"
  264. KERN_ERR "******* Please consider a BIOS update.\n"
  265. KERN_ERR "******* Disabling USB legacy in the BIOS may also help.\n";
  266. #endif
  267. /* Workaround for K8 erratum #93 & buggy BIOS.
  268. BIOS SMM functions are required to use a specific workaround
  269. to avoid corruption of the 64bit RIP register on C stepping K8.
  270. A lot of BIOS that didn't get tested properly miss this.
  271. The OS sees this as a page fault with the upper 32bits of RIP cleared.
  272. Try to work around it here.
  273. Note we only handle faults in kernel here.
  274. Does nothing for X86_32
  275. */
  276. static int is_errata93(struct pt_regs *regs, unsigned long address)
  277. {
  278. #ifdef CONFIG_X86_64
  279. static int warned;
  280. if (address != regs->ip)
  281. return 0;
  282. if ((address >> 32) != 0)
  283. return 0;
  284. address |= 0xffffffffUL << 32;
  285. if ((address >= (u64)_stext && address <= (u64)_etext) ||
  286. (address >= MODULES_VADDR && address <= MODULES_END)) {
  287. if (!warned) {
  288. printk(errata93_warning);
  289. warned = 1;
  290. }
  291. regs->ip = address;
  292. return 1;
  293. }
  294. #endif
  295. return 0;
  296. }
  297. /*
  298. * Work around K8 erratum #100 K8 in compat mode occasionally jumps to illegal
  299. * addresses >4GB. We catch this in the page fault handler because these
  300. * addresses are not reachable. Just detect this case and return. Any code
  301. * segment in LDT is compatibility mode.
  302. */
  303. static int is_errata100(struct pt_regs *regs, unsigned long address)
  304. {
  305. #ifdef CONFIG_X86_64
  306. if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) &&
  307. (address >> 32))
  308. return 1;
  309. #endif
  310. return 0;
  311. }
  312. void do_invalid_op(struct pt_regs *, unsigned long);
  313. static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
  314. {
  315. #ifdef CONFIG_X86_F00F_BUG
  316. unsigned long nr;
  317. /*
  318. * Pentium F0 0F C7 C8 bug workaround.
  319. */
  320. if (boot_cpu_data.f00f_bug) {
  321. nr = (address - idt_descr.address) >> 3;
  322. if (nr == 6) {
  323. do_invalid_op(regs, 0);
  324. return 1;
  325. }
  326. }
  327. #endif
  328. return 0;
  329. }
  330. static void show_fault_oops(struct pt_regs *regs, unsigned long error_code,
  331. unsigned long address)
  332. {
  333. #ifdef CONFIG_X86_32
  334. if (!oops_may_print())
  335. return;
  336. #ifdef CONFIG_X86_PAE
  337. if (error_code & PF_INSTR) {
  338. int level;
  339. pte_t *pte = lookup_address(address, &level);
  340. if (pte && pte_present(*pte) && !pte_exec(*pte))
  341. printk(KERN_CRIT "kernel tried to execute "
  342. "NX-protected page - exploit attempt? "
  343. "(uid: %d)\n", current->uid);
  344. }
  345. #endif
  346. printk(KERN_ALERT "BUG: unable to handle kernel ");
  347. if (address < PAGE_SIZE)
  348. printk(KERN_CONT "NULL pointer dereference");
  349. else
  350. printk(KERN_CONT "paging request");
  351. printk(KERN_CONT " at %08lx\n", address);
  352. printk(KERN_ALERT "IP:");
  353. printk_address(regs->ip, 1);
  354. dump_pagetable(address);
  355. #else /* CONFIG_X86_64 */
  356. printk(KERN_ALERT "BUG: unable to handle kernel ");
  357. if (address < PAGE_SIZE)
  358. printk(KERN_CONT "NULL pointer dereference");
  359. else
  360. printk(KERN_CONT "paging request");
  361. printk(KERN_CONT " at %016lx\n", address);
  362. printk(KERN_ALERT "IP:");
  363. printk_address(regs->ip, 1);
  364. dump_pagetable(address);
  365. #endif
  366. }
  367. #ifdef CONFIG_X86_64
  368. static noinline void pgtable_bad(unsigned long address, struct pt_regs *regs,
  369. unsigned long error_code)
  370. {
  371. unsigned long flags = oops_begin();
  372. struct task_struct *tsk;
  373. printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
  374. current->comm, address);
  375. dump_pagetable(address);
  376. tsk = current;
  377. tsk->thread.cr2 = address;
  378. tsk->thread.trap_no = 14;
  379. tsk->thread.error_code = error_code;
  380. if (__die("Bad pagetable", regs, error_code))
  381. regs = NULL;
  382. oops_end(flags, regs, SIGKILL);
  383. }
  384. #endif
  385. /*
  386. * Handle a fault on the vmalloc or module mapping area
  387. *
  388. * This assumes no large pages in there.
  389. */
  390. static inline int vmalloc_fault(unsigned long address)
  391. {
  392. #ifdef CONFIG_X86_32
  393. unsigned long pgd_paddr;
  394. pmd_t *pmd_k;
  395. pte_t *pte_k;
  396. /*
  397. * Synchronize this task's top level page-table
  398. * with the 'reference' page table.
  399. *
  400. * Do _not_ use "current" here. We might be inside
  401. * an interrupt in the middle of a task switch..
  402. */
  403. pgd_paddr = read_cr3();
  404. pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
  405. if (!pmd_k)
  406. return -1;
  407. pte_k = pte_offset_kernel(pmd_k, address);
  408. if (!pte_present(*pte_k))
  409. return -1;
  410. return 0;
  411. #else
  412. pgd_t *pgd, *pgd_ref;
  413. pud_t *pud, *pud_ref;
  414. pmd_t *pmd, *pmd_ref;
  415. pte_t *pte, *pte_ref;
  416. /* Copy kernel mappings over when needed. This can also
  417. happen within a race in page table update. In the later
  418. case just flush. */
  419. pgd = pgd_offset(current->mm ?: &init_mm, address);
  420. pgd_ref = pgd_offset_k(address);
  421. if (pgd_none(*pgd_ref))
  422. return -1;
  423. if (pgd_none(*pgd))
  424. set_pgd(pgd, *pgd_ref);
  425. else
  426. BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
  427. /* Below here mismatches are bugs because these lower tables
  428. are shared */
  429. pud = pud_offset(pgd, address);
  430. pud_ref = pud_offset(pgd_ref, address);
  431. if (pud_none(*pud_ref))
  432. return -1;
  433. if (pud_none(*pud) || pud_page_vaddr(*pud) != pud_page_vaddr(*pud_ref))
  434. BUG();
  435. pmd = pmd_offset(pud, address);
  436. pmd_ref = pmd_offset(pud_ref, address);
  437. if (pmd_none(*pmd_ref))
  438. return -1;
  439. if (pmd_none(*pmd) || pmd_page(*pmd) != pmd_page(*pmd_ref))
  440. BUG();
  441. pte_ref = pte_offset_kernel(pmd_ref, address);
  442. if (!pte_present(*pte_ref))
  443. return -1;
  444. pte = pte_offset_kernel(pmd, address);
  445. /* Don't use pte_page here, because the mappings can point
  446. outside mem_map, and the NUMA hash lookup cannot handle
  447. that. */
  448. if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
  449. BUG();
  450. return 0;
  451. #endif
  452. }
  453. int show_unhandled_signals = 1;
  454. /*
  455. * This routine handles page faults. It determines the address,
  456. * and the problem, and then passes it off to one of the appropriate
  457. * routines.
  458. */
  459. void __kprobes do_page_fault(struct pt_regs *regs, unsigned long error_code)
  460. {
  461. struct task_struct *tsk;
  462. struct mm_struct *mm;
  463. struct vm_area_struct *vma;
  464. unsigned long address;
  465. int write, si_code;
  466. int fault;
  467. /*
  468. * We can fault from pretty much anywhere, with unknown IRQ state.
  469. */
  470. trace_hardirqs_fixup();
  471. tsk = current;
  472. mm = tsk->mm;
  473. prefetchw(&mm->mmap_sem);
  474. /* get the address */
  475. address = read_cr2();
  476. si_code = SEGV_MAPERR;
  477. if (notify_page_fault(regs))
  478. return;
  479. /*
  480. * We fault-in kernel-space virtual memory on-demand. The
  481. * 'reference' page table is init_mm.pgd.
  482. *
  483. * NOTE! We MUST NOT take any locks for this case. We may
  484. * be in an interrupt or a critical region, and should
  485. * only copy the information from the master page table,
  486. * nothing more.
  487. *
  488. * This verifies that the fault happens in kernel space
  489. * (error_code & 4) == 0, and that the fault was not a
  490. * protection error (error_code & 9) == 0.
  491. */
  492. if (unlikely(address >= TASK_SIZE)) {
  493. if (!(error_code & (PF_RSVD|PF_USER|PF_PROT)) &&
  494. vmalloc_fault(address) >= 0)
  495. return;
  496. /*
  497. * Don't take the mm semaphore here. If we fixup a prefetch
  498. * fault we could otherwise deadlock.
  499. */
  500. goto bad_area_nosemaphore;
  501. }
  502. /* It's safe to allow irq's after cr2 has been saved and the vmalloc
  503. fault has been handled. */
  504. if (regs->flags & (X86_EFLAGS_IF|VM_MASK))
  505. local_irq_enable();
  506. /*
  507. * If we're in an interrupt, have no user context or are running in an
  508. * atomic region then we must not take the fault.
  509. */
  510. if (in_atomic() || !mm)
  511. goto bad_area_nosemaphore;
  512. /* When running in the kernel we expect faults to occur only to
  513. * addresses in user space. All other faults represent errors in the
  514. * kernel and should generate an OOPS. Unfortunately, in the case of an
  515. * erroneous fault occurring in a code path which already holds mmap_sem
  516. * we will deadlock attempting to validate the fault against the
  517. * address space. Luckily the kernel only validly references user
  518. * space from well defined areas of code, which are listed in the
  519. * exceptions table.
  520. *
  521. * As the vast majority of faults will be valid we will only perform
  522. * the source reference check when there is a possibility of a deadlock.
  523. * Attempt to lock the address space, if we cannot we then validate the
  524. * source. If this is invalid we can skip the address space check,
  525. * thus avoiding the deadlock.
  526. */
  527. if (!down_read_trylock(&mm->mmap_sem)) {
  528. if ((error_code & PF_USER) == 0 &&
  529. !search_exception_tables(regs->ip))
  530. goto bad_area_nosemaphore;
  531. down_read(&mm->mmap_sem);
  532. }
  533. vma = find_vma(mm, address);
  534. if (!vma)
  535. goto bad_area;
  536. if (vma->vm_start <= address)
  537. goto good_area;
  538. if (!(vma->vm_flags & VM_GROWSDOWN))
  539. goto bad_area;
  540. if (error_code & PF_USER) {
  541. /*
  542. * Accessing the stack below %sp is always a bug.
  543. * The large cushion allows instructions like enter
  544. * and pusha to work. ("enter $65535,$31" pushes
  545. * 32 pointers and then decrements %sp by 65535.)
  546. */
  547. if (address + 65536 + 32 * sizeof(unsigned long) < regs->sp)
  548. goto bad_area;
  549. }
  550. if (expand_stack(vma, address))
  551. goto bad_area;
  552. /*
  553. * Ok, we have a good vm_area for this memory access, so
  554. * we can handle it..
  555. */
  556. good_area:
  557. si_code = SEGV_ACCERR;
  558. write = 0;
  559. switch (error_code & (PF_PROT|PF_WRITE)) {
  560. default: /* 3: write, present */
  561. /* fall through */
  562. case PF_WRITE: /* write, not present */
  563. if (!(vma->vm_flags & VM_WRITE))
  564. goto bad_area;
  565. write++;
  566. break;
  567. case PF_PROT: /* read, present */
  568. goto bad_area;
  569. case 0: /* read, not present */
  570. if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
  571. goto bad_area;
  572. }
  573. survive:
  574. /*
  575. * If for any reason at all we couldn't handle the fault,
  576. * make sure we exit gracefully rather than endlessly redo
  577. * the fault.
  578. */
  579. fault = handle_mm_fault(mm, vma, address, write);
  580. if (unlikely(fault & VM_FAULT_ERROR)) {
  581. if (fault & VM_FAULT_OOM)
  582. goto out_of_memory;
  583. else if (fault & VM_FAULT_SIGBUS)
  584. goto do_sigbus;
  585. BUG();
  586. }
  587. if (fault & VM_FAULT_MAJOR)
  588. tsk->maj_flt++;
  589. else
  590. tsk->min_flt++;
  591. #ifdef CONFIG_X86_32
  592. /*
  593. * Did it hit the DOS screen memory VA from vm86 mode?
  594. */
  595. if (v8086_mode(regs)) {
  596. unsigned long bit = (address - 0xA0000) >> PAGE_SHIFT;
  597. if (bit < 32)
  598. tsk->thread.screen_bitmap |= 1 << bit;
  599. }
  600. #endif
  601. up_read(&mm->mmap_sem);
  602. return;
  603. /*
  604. * Something tried to access memory that isn't in our memory map..
  605. * Fix it, but check if it's kernel or user first..
  606. */
  607. bad_area:
  608. up_read(&mm->mmap_sem);
  609. bad_area_nosemaphore:
  610. /* User mode accesses just cause a SIGSEGV */
  611. if (error_code & PF_USER) {
  612. /*
  613. * It's possible to have interrupts off here.
  614. */
  615. local_irq_enable();
  616. /*
  617. * Valid to do another page fault here because this one came
  618. * from user space.
  619. */
  620. if (is_prefetch(regs, address, error_code))
  621. return;
  622. if (is_errata100(regs, address))
  623. return;
  624. if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
  625. printk_ratelimit()) {
  626. printk(
  627. #ifdef CONFIG_X86_32
  628. "%s%s[%d]: segfault at %lx ip %08lx sp %08lx error %lx",
  629. #else
  630. "%s%s[%d]: segfault at %lx ip %lx sp %lx error %lx",
  631. #endif
  632. task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
  633. tsk->comm, task_pid_nr(tsk), address, regs->ip,
  634. regs->sp, error_code);
  635. print_vma_addr(" in ", regs->ip);
  636. printk("\n");
  637. }
  638. tsk->thread.cr2 = address;
  639. /* Kernel addresses are always protection faults */
  640. tsk->thread.error_code = error_code | (address >= TASK_SIZE);
  641. tsk->thread.trap_no = 14;
  642. force_sig_info_fault(SIGSEGV, si_code, address, tsk);
  643. return;
  644. }
  645. if (is_f00f_bug(regs, address))
  646. return;
  647. no_context:
  648. /* Are we prepared to handle this kernel fault? */
  649. if (fixup_exception(regs))
  650. return;
  651. /*
  652. * Valid to do another page fault here, because if this fault
  653. * had been triggered by is_prefetch fixup_exception would have
  654. * handled it.
  655. */
  656. if (is_prefetch(regs, address, error_code))
  657. return;
  658. if (is_errata93(regs, address))
  659. return;
  660. /*
  661. * Oops. The kernel tried to access some bad page. We'll have to
  662. * terminate things with extreme prejudice.
  663. */
  664. bust_spinlocks(1);
  665. show_fault_oops(regs, error_code, address);
  666. tsk->thread.cr2 = address;
  667. tsk->thread.trap_no = 14;
  668. tsk->thread.error_code = error_code;
  669. die("Oops", regs, error_code);
  670. bust_spinlocks(0);
  671. do_exit(SIGKILL);
  672. /*
  673. * We ran out of memory, or some other thing happened to us that made
  674. * us unable to handle the page fault gracefully.
  675. */
  676. out_of_memory:
  677. up_read(&mm->mmap_sem);
  678. if (is_global_init(tsk)) {
  679. yield();
  680. down_read(&mm->mmap_sem);
  681. goto survive;
  682. }
  683. printk("VM: killing process %s\n", tsk->comm);
  684. if (error_code & PF_USER)
  685. do_group_exit(SIGKILL);
  686. goto no_context;
  687. do_sigbus:
  688. up_read(&mm->mmap_sem);
  689. /* Kernel mode? Handle exceptions or die */
  690. if (!(error_code & PF_USER))
  691. goto no_context;
  692. /* User space => ok to do another page fault */
  693. if (is_prefetch(regs, address, error_code))
  694. return;
  695. tsk->thread.cr2 = address;
  696. tsk->thread.error_code = error_code;
  697. tsk->thread.trap_no = 14;
  698. force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk);
  699. }
  700. void vmalloc_sync_all(void)
  701. {
  702. #ifdef CONFIG_X86_32
  703. /*
  704. * Note that races in the updates of insync and start aren't
  705. * problematic: insync can only get set bits added, and updates to
  706. * start are only improving performance (without affecting correctness
  707. * if undone).
  708. */
  709. static DECLARE_BITMAP(insync, PTRS_PER_PGD);
  710. static unsigned long start = TASK_SIZE;
  711. unsigned long address;
  712. if (SHARED_KERNEL_PMD)
  713. return;
  714. BUILD_BUG_ON(TASK_SIZE & ~PGDIR_MASK);
  715. for (address = start; address >= TASK_SIZE; address += PGDIR_SIZE) {
  716. if (!test_bit(pgd_index(address), insync)) {
  717. unsigned long flags;
  718. struct page *page;
  719. spin_lock_irqsave(&pgd_lock, flags);
  720. for (page = pgd_list; page; page =
  721. (struct page *)page->index)
  722. if (!vmalloc_sync_one(page_address(page),
  723. address)) {
  724. BUG_ON(page != pgd_list);
  725. break;
  726. }
  727. spin_unlock_irqrestore(&pgd_lock, flags);
  728. if (!page)
  729. set_bit(pgd_index(address), insync);
  730. }
  731. if (address == start && test_bit(pgd_index(address), insync))
  732. start = address + PGDIR_SIZE;
  733. }
  734. #else /* CONFIG_X86_64 */
  735. /*
  736. * Note that races in the updates of insync and start aren't
  737. * problematic: insync can only get set bits added, and updates to
  738. * start are only improving performance (without affecting correctness
  739. * if undone).
  740. */
  741. static DECLARE_BITMAP(insync, PTRS_PER_PGD);
  742. static unsigned long start = VMALLOC_START & PGDIR_MASK;
  743. unsigned long address;
  744. for (address = start; address <= VMALLOC_END; address += PGDIR_SIZE) {
  745. if (!test_bit(pgd_index(address), insync)) {
  746. const pgd_t *pgd_ref = pgd_offset_k(address);
  747. struct page *page;
  748. if (pgd_none(*pgd_ref))
  749. continue;
  750. spin_lock(&pgd_lock);
  751. list_for_each_entry(page, &pgd_list, lru) {
  752. pgd_t *pgd;
  753. pgd = (pgd_t *)page_address(page) + pgd_index(address);
  754. if (pgd_none(*pgd))
  755. set_pgd(pgd, *pgd_ref);
  756. else
  757. BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
  758. }
  759. spin_unlock(&pgd_lock);
  760. set_bit(pgd_index(address), insync);
  761. }
  762. if (address == start)
  763. start = address + PGDIR_SIZE;
  764. }
  765. /* Check that there is no need to do the same for the modules area. */
  766. BUILD_BUG_ON(!(MODULES_VADDR > __START_KERNEL));
  767. BUILD_BUG_ON(!(((MODULES_END - 1) & PGDIR_MASK) ==
  768. (__START_KERNEL & PGDIR_MASK)));
  769. #endif
  770. }