fault.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  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/highmem.h>
  21. #include <linux/bootmem.h> /* for max_low_pfn */
  22. #include <linux/vmalloc.h>
  23. #include <linux/module.h>
  24. #include <linux/kprobes.h>
  25. #include <linux/uaccess.h>
  26. #include <linux/kdebug.h>
  27. #include <asm/system.h>
  28. #include <asm/desc.h>
  29. #include <asm/segment.h>
  30. #include <asm/pgalloc.h>
  31. #include <asm/smp.h>
  32. #include <asm/tlbflush.h>
  33. #include <asm/proto.h>
  34. #include <asm-generic/sections.h>
  35. /*
  36. * Page fault error code bits
  37. * bit 0 == 0 means no page found, 1 means protection fault
  38. * bit 1 == 0 means read, 1 means write
  39. * bit 2 == 0 means kernel, 1 means user-mode
  40. * bit 3 == 1 means use of reserved bit detected
  41. * bit 4 == 1 means fault was an instruction fetch
  42. */
  43. #define PF_PROT (1<<0)
  44. #define PF_WRITE (1<<1)
  45. #define PF_USER (1<<2)
  46. #define PF_RSVD (1<<3)
  47. #define PF_INSTR (1<<4)
  48. static inline int notify_page_fault(struct pt_regs *regs)
  49. {
  50. #ifdef CONFIG_KPROBES
  51. int ret = 0;
  52. /* kprobe_running() needs smp_processor_id() */
  53. if (!user_mode_vm(regs)) {
  54. preempt_disable();
  55. if (kprobe_running() && kprobe_fault_handler(regs, 14))
  56. ret = 1;
  57. preempt_enable();
  58. }
  59. return ret;
  60. #else
  61. return 0;
  62. #endif
  63. }
  64. /*
  65. * X86_32
  66. * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
  67. * Check that here and ignore it.
  68. *
  69. * X86_64
  70. * Sometimes the CPU reports invalid exceptions on prefetch.
  71. * Check that here and ignore it.
  72. *
  73. * Opcode checker based on code by Richard Brunner
  74. */
  75. static int is_prefetch(struct pt_regs *regs, unsigned long addr,
  76. unsigned long error_code)
  77. {
  78. unsigned char *instr;
  79. int scan_more = 1;
  80. int prefetch = 0;
  81. unsigned char *max_instr;
  82. /*
  83. * If it was a exec (instruction fetch) fault on NX page, then
  84. * do not ignore the fault:
  85. */
  86. if (error_code & PF_INSTR)
  87. return 0;
  88. instr = (unsigned char *)convert_ip_to_linear(current, regs);
  89. max_instr = instr + 15;
  90. if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE)
  91. return 0;
  92. while (scan_more && instr < max_instr) {
  93. unsigned char opcode;
  94. unsigned char instr_hi;
  95. unsigned char instr_lo;
  96. if (probe_kernel_address(instr, opcode))
  97. break;
  98. instr_hi = opcode & 0xf0;
  99. instr_lo = opcode & 0x0f;
  100. instr++;
  101. switch (instr_hi) {
  102. case 0x20:
  103. case 0x30:
  104. /*
  105. * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
  106. * In X86_64 long mode, the CPU will signal invalid
  107. * opcode if some of these prefixes are present so
  108. * X86_64 will never get here anyway
  109. */
  110. scan_more = ((instr_lo & 7) == 0x6);
  111. break;
  112. #ifdef CONFIG_X86_64
  113. case 0x40:
  114. /*
  115. * In AMD64 long mode 0x40..0x4F are valid REX prefixes
  116. * Need to figure out under what instruction mode the
  117. * instruction was issued. Could check the LDT for lm,
  118. * but for now it's good enough to assume that long
  119. * mode only uses well known segments or kernel.
  120. */
  121. scan_more = (!user_mode(regs)) || (regs->cs == __USER_CS);
  122. break;
  123. #endif
  124. case 0x60:
  125. /* 0x64 thru 0x67 are valid prefixes in all modes. */
  126. scan_more = (instr_lo & 0xC) == 0x4;
  127. break;
  128. case 0xF0:
  129. /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
  130. scan_more = !instr_lo || (instr_lo>>1) == 1;
  131. break;
  132. case 0x00:
  133. /* Prefetch instruction is 0x0F0D or 0x0F18 */
  134. scan_more = 0;
  135. if (probe_kernel_address(instr, opcode))
  136. break;
  137. prefetch = (instr_lo == 0xF) &&
  138. (opcode == 0x0D || opcode == 0x18);
  139. break;
  140. default:
  141. scan_more = 0;
  142. break;
  143. }
  144. }
  145. return prefetch;
  146. }
  147. static void force_sig_info_fault(int si_signo, int si_code,
  148. unsigned long address, struct task_struct *tsk)
  149. {
  150. siginfo_t info;
  151. info.si_signo = si_signo;
  152. info.si_errno = 0;
  153. info.si_code = si_code;
  154. info.si_addr = (void __user *)address;
  155. force_sig_info(si_signo, &info, tsk);
  156. }
  157. #ifdef CONFIG_X86_64
  158. static int bad_address(void *p)
  159. {
  160. unsigned long dummy;
  161. return probe_kernel_address((unsigned long *)p, dummy);
  162. }
  163. #endif
  164. static void dump_pagetable(unsigned long address)
  165. {
  166. #ifdef CONFIG_X86_32
  167. __typeof__(pte_val(__pte(0))) page;
  168. page = read_cr3();
  169. page = ((__typeof__(page) *) __va(page))[address >> PGDIR_SHIFT];
  170. #ifdef CONFIG_X86_PAE
  171. printk("*pdpt = %016Lx ", page);
  172. if ((page >> PAGE_SHIFT) < max_low_pfn
  173. && page & _PAGE_PRESENT) {
  174. page &= PAGE_MASK;
  175. page = ((__typeof__(page) *) __va(page))[(address >> PMD_SHIFT)
  176. & (PTRS_PER_PMD - 1)];
  177. printk(KERN_CONT "*pde = %016Lx ", page);
  178. page &= ~_PAGE_NX;
  179. }
  180. #else
  181. printk("*pde = %08lx ", page);
  182. #endif
  183. /*
  184. * We must not directly access the pte in the highpte
  185. * case if the page table is located in highmem.
  186. * And let's rather not kmap-atomic the pte, just in case
  187. * it's allocated already.
  188. */
  189. if ((page >> PAGE_SHIFT) < max_low_pfn
  190. && (page & _PAGE_PRESENT)
  191. && !(page & _PAGE_PSE)) {
  192. page &= PAGE_MASK;
  193. page = ((__typeof__(page) *) __va(page))[(address >> PAGE_SHIFT)
  194. & (PTRS_PER_PTE - 1)];
  195. printk("*pte = %0*Lx ", sizeof(page)*2, (u64)page);
  196. }
  197. printk("\n");
  198. #else /* CONFIG_X86_64 */
  199. pgd_t *pgd;
  200. pud_t *pud;
  201. pmd_t *pmd;
  202. pte_t *pte;
  203. pgd = (pgd_t *)read_cr3();
  204. pgd = __va((unsigned long)pgd & PHYSICAL_PAGE_MASK);
  205. pgd += pgd_index(address);
  206. if (bad_address(pgd)) goto bad;
  207. printk("PGD %lx ", pgd_val(*pgd));
  208. if (!pgd_present(*pgd)) goto ret;
  209. pud = pud_offset(pgd, address);
  210. if (bad_address(pud)) goto bad;
  211. printk("PUD %lx ", pud_val(*pud));
  212. if (!pud_present(*pud) || pud_large(*pud))
  213. 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. #endif
  337. #ifdef CONFIG_X86_PAE
  338. if (error_code & PF_INSTR) {
  339. unsigned int level;
  340. pte_t *pte = lookup_address(address, &level);
  341. if (pte && pte_present(*pte) && !pte_exec(*pte))
  342. printk(KERN_CRIT "kernel tried to execute "
  343. "NX-protected page - exploit attempt? "
  344. "(uid: %d)\n", current->uid);
  345. }
  346. #endif
  347. printk(KERN_ALERT "BUG: unable to handle kernel ");
  348. if (address < PAGE_SIZE)
  349. printk(KERN_CONT "NULL pointer dereference");
  350. else
  351. printk(KERN_CONT "paging request");
  352. printk(KERN_CONT " at %p\n", (void *) address);
  353. printk(KERN_ALERT "IP:");
  354. printk_address(regs->ip, 1);
  355. dump_pagetable(address);
  356. }
  357. #ifdef CONFIG_X86_64
  358. static noinline void pgtable_bad(unsigned long address, struct pt_regs *regs,
  359. unsigned long error_code)
  360. {
  361. unsigned long flags = oops_begin();
  362. struct task_struct *tsk;
  363. printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
  364. current->comm, address);
  365. dump_pagetable(address);
  366. tsk = current;
  367. tsk->thread.cr2 = address;
  368. tsk->thread.trap_no = 14;
  369. tsk->thread.error_code = error_code;
  370. if (__die("Bad pagetable", regs, error_code))
  371. regs = NULL;
  372. oops_end(flags, regs, SIGKILL);
  373. }
  374. #endif
  375. static int spurious_fault_check(unsigned long error_code, pte_t *pte)
  376. {
  377. if ((error_code & PF_WRITE) && !pte_write(*pte))
  378. return 0;
  379. if ((error_code & PF_INSTR) && !pte_exec(*pte))
  380. return 0;
  381. return 1;
  382. }
  383. /*
  384. * Handle a spurious fault caused by a stale TLB entry. This allows
  385. * us to lazily refresh the TLB when increasing the permissions of a
  386. * kernel page (RO -> RW or NX -> X). Doing it eagerly is very
  387. * expensive since that implies doing a full cross-processor TLB
  388. * flush, even if no stale TLB entries exist on other processors.
  389. * There are no security implications to leaving a stale TLB when
  390. * increasing the permissions on a page.
  391. */
  392. static int spurious_fault(unsigned long address,
  393. unsigned long error_code)
  394. {
  395. pgd_t *pgd;
  396. pud_t *pud;
  397. pmd_t *pmd;
  398. pte_t *pte;
  399. /* Reserved-bit violation or user access to kernel space? */
  400. if (error_code & (PF_USER | PF_RSVD))
  401. return 0;
  402. pgd = init_mm.pgd + pgd_index(address);
  403. if (!pgd_present(*pgd))
  404. return 0;
  405. pud = pud_offset(pgd, address);
  406. if (!pud_present(*pud))
  407. return 0;
  408. if (pud_large(*pud))
  409. return spurious_fault_check(error_code, (pte_t *) pud);
  410. pmd = pmd_offset(pud, address);
  411. if (!pmd_present(*pmd))
  412. return 0;
  413. if (pmd_large(*pmd))
  414. return spurious_fault_check(error_code, (pte_t *) pmd);
  415. pte = pte_offset_kernel(pmd, address);
  416. if (!pte_present(*pte))
  417. return 0;
  418. return spurious_fault_check(error_code, pte);
  419. }
  420. /*
  421. * X86_32
  422. * Handle a fault on the vmalloc or module mapping area
  423. *
  424. * X86_64
  425. * Handle a fault on the vmalloc area
  426. *
  427. * This assumes no large pages in there.
  428. */
  429. static int vmalloc_fault(unsigned long address)
  430. {
  431. #ifdef CONFIG_X86_32
  432. unsigned long pgd_paddr;
  433. pmd_t *pmd_k;
  434. pte_t *pte_k;
  435. /* Make sure we are in vmalloc area */
  436. if (!(address >= VMALLOC_START && address < VMALLOC_END))
  437. return -1;
  438. /*
  439. * Synchronize this task's top level page-table
  440. * with the 'reference' page table.
  441. *
  442. * Do _not_ use "current" here. We might be inside
  443. * an interrupt in the middle of a task switch..
  444. */
  445. pgd_paddr = read_cr3();
  446. pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
  447. if (!pmd_k)
  448. return -1;
  449. pte_k = pte_offset_kernel(pmd_k, address);
  450. if (!pte_present(*pte_k))
  451. return -1;
  452. return 0;
  453. #else
  454. pgd_t *pgd, *pgd_ref;
  455. pud_t *pud, *pud_ref;
  456. pmd_t *pmd, *pmd_ref;
  457. pte_t *pte, *pte_ref;
  458. /* Make sure we are in vmalloc area */
  459. if (!(address >= VMALLOC_START && address < VMALLOC_END))
  460. return -1;
  461. /* Copy kernel mappings over when needed. This can also
  462. happen within a race in page table update. In the later
  463. case just flush. */
  464. pgd = pgd_offset(current->mm ?: &init_mm, address);
  465. pgd_ref = pgd_offset_k(address);
  466. if (pgd_none(*pgd_ref))
  467. return -1;
  468. if (pgd_none(*pgd))
  469. set_pgd(pgd, *pgd_ref);
  470. else
  471. BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
  472. /* Below here mismatches are bugs because these lower tables
  473. are shared */
  474. pud = pud_offset(pgd, address);
  475. pud_ref = pud_offset(pgd_ref, address);
  476. if (pud_none(*pud_ref))
  477. return -1;
  478. if (pud_none(*pud) || pud_page_vaddr(*pud) != pud_page_vaddr(*pud_ref))
  479. BUG();
  480. pmd = pmd_offset(pud, address);
  481. pmd_ref = pmd_offset(pud_ref, address);
  482. if (pmd_none(*pmd_ref))
  483. return -1;
  484. if (pmd_none(*pmd) || pmd_page(*pmd) != pmd_page(*pmd_ref))
  485. BUG();
  486. pte_ref = pte_offset_kernel(pmd_ref, address);
  487. if (!pte_present(*pte_ref))
  488. return -1;
  489. pte = pte_offset_kernel(pmd, address);
  490. /* Don't use pte_page here, because the mappings can point
  491. outside mem_map, and the NUMA hash lookup cannot handle
  492. that. */
  493. if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
  494. BUG();
  495. return 0;
  496. #endif
  497. }
  498. int show_unhandled_signals = 1;
  499. /*
  500. * This routine handles page faults. It determines the address,
  501. * and the problem, and then passes it off to one of the appropriate
  502. * routines.
  503. */
  504. #ifdef CONFIG_X86_64
  505. asmlinkage
  506. #endif
  507. void __kprobes do_page_fault(struct pt_regs *regs, unsigned long error_code)
  508. {
  509. struct task_struct *tsk;
  510. struct mm_struct *mm;
  511. struct vm_area_struct *vma;
  512. unsigned long address;
  513. int write, si_code;
  514. int fault;
  515. #ifdef CONFIG_X86_64
  516. unsigned long flags;
  517. #endif
  518. /*
  519. * We can fault from pretty much anywhere, with unknown IRQ state.
  520. */
  521. trace_hardirqs_fixup();
  522. tsk = current;
  523. mm = tsk->mm;
  524. prefetchw(&mm->mmap_sem);
  525. /* get the address */
  526. address = read_cr2();
  527. si_code = SEGV_MAPERR;
  528. if (notify_page_fault(regs))
  529. return;
  530. /*
  531. * We fault-in kernel-space virtual memory on-demand. The
  532. * 'reference' page table is init_mm.pgd.
  533. *
  534. * NOTE! We MUST NOT take any locks for this case. We may
  535. * be in an interrupt or a critical region, and should
  536. * only copy the information from the master page table,
  537. * nothing more.
  538. *
  539. * This verifies that the fault happens in kernel space
  540. * (error_code & 4) == 0, and that the fault was not a
  541. * protection error (error_code & 9) == 0.
  542. */
  543. #ifdef CONFIG_X86_32
  544. if (unlikely(address >= TASK_SIZE)) {
  545. #else
  546. if (unlikely(address >= TASK_SIZE64)) {
  547. #endif
  548. if (!(error_code & (PF_RSVD|PF_USER|PF_PROT)) &&
  549. vmalloc_fault(address) >= 0)
  550. return;
  551. /* Can handle a stale RO->RW TLB */
  552. if (spurious_fault(address, error_code))
  553. return;
  554. /*
  555. * Don't take the mm semaphore here. If we fixup a prefetch
  556. * fault we could otherwise deadlock.
  557. */
  558. goto bad_area_nosemaphore;
  559. }
  560. #ifdef CONFIG_X86_32
  561. /* It's safe to allow irq's after cr2 has been saved and the vmalloc
  562. fault has been handled. */
  563. if (regs->flags & (X86_EFLAGS_IF | X86_VM_MASK))
  564. local_irq_enable();
  565. /*
  566. * If we're in an interrupt, have no user context or are running in an
  567. * atomic region then we must not take the fault.
  568. */
  569. if (in_atomic() || !mm)
  570. goto bad_area_nosemaphore;
  571. #else /* CONFIG_X86_64 */
  572. if (likely(regs->flags & X86_EFLAGS_IF))
  573. local_irq_enable();
  574. if (unlikely(error_code & PF_RSVD))
  575. pgtable_bad(address, regs, error_code);
  576. /*
  577. * If we're in an interrupt, have no user context or are running in an
  578. * atomic region then we must not take the fault.
  579. */
  580. if (unlikely(in_atomic() || !mm))
  581. goto bad_area_nosemaphore;
  582. /*
  583. * User-mode registers count as a user access even for any
  584. * potential system fault or CPU buglet.
  585. */
  586. if (user_mode_vm(regs))
  587. error_code |= PF_USER;
  588. again:
  589. #endif
  590. /* When running in the kernel we expect faults to occur only to
  591. * addresses in user space. All other faults represent errors in the
  592. * kernel and should generate an OOPS. Unfortunately, in the case of an
  593. * erroneous fault occurring in a code path which already holds mmap_sem
  594. * we will deadlock attempting to validate the fault against the
  595. * address space. Luckily the kernel only validly references user
  596. * space from well defined areas of code, which are listed in the
  597. * exceptions table.
  598. *
  599. * As the vast majority of faults will be valid we will only perform
  600. * the source reference check when there is a possibility of a deadlock.
  601. * Attempt to lock the address space, if we cannot we then validate the
  602. * source. If this is invalid we can skip the address space check,
  603. * thus avoiding the deadlock.
  604. */
  605. if (!down_read_trylock(&mm->mmap_sem)) {
  606. if ((error_code & PF_USER) == 0 &&
  607. !search_exception_tables(regs->ip))
  608. goto bad_area_nosemaphore;
  609. down_read(&mm->mmap_sem);
  610. }
  611. vma = find_vma(mm, address);
  612. if (!vma)
  613. goto bad_area;
  614. if (vma->vm_start <= address)
  615. goto good_area;
  616. if (!(vma->vm_flags & VM_GROWSDOWN))
  617. goto bad_area;
  618. if (error_code & PF_USER) {
  619. /*
  620. * Accessing the stack below %sp is always a bug.
  621. * The large cushion allows instructions like enter
  622. * and pusha to work. ("enter $65535,$31" pushes
  623. * 32 pointers and then decrements %sp by 65535.)
  624. */
  625. if (address + 65536 + 32 * sizeof(unsigned long) < regs->sp)
  626. goto bad_area;
  627. }
  628. if (expand_stack(vma, address))
  629. goto bad_area;
  630. /*
  631. * Ok, we have a good vm_area for this memory access, so
  632. * we can handle it..
  633. */
  634. good_area:
  635. si_code = SEGV_ACCERR;
  636. write = 0;
  637. switch (error_code & (PF_PROT|PF_WRITE)) {
  638. default: /* 3: write, present */
  639. /* fall through */
  640. case PF_WRITE: /* write, not present */
  641. if (!(vma->vm_flags & VM_WRITE))
  642. goto bad_area;
  643. write++;
  644. break;
  645. case PF_PROT: /* read, present */
  646. goto bad_area;
  647. case 0: /* read, not present */
  648. if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
  649. goto bad_area;
  650. }
  651. #ifdef CONFIG_X86_32
  652. survive:
  653. #endif
  654. /*
  655. * If for any reason at all we couldn't handle the fault,
  656. * make sure we exit gracefully rather than endlessly redo
  657. * the fault.
  658. */
  659. fault = handle_mm_fault(mm, vma, address, write);
  660. if (unlikely(fault & VM_FAULT_ERROR)) {
  661. if (fault & VM_FAULT_OOM)
  662. goto out_of_memory;
  663. else if (fault & VM_FAULT_SIGBUS)
  664. goto do_sigbus;
  665. BUG();
  666. }
  667. if (fault & VM_FAULT_MAJOR)
  668. tsk->maj_flt++;
  669. else
  670. tsk->min_flt++;
  671. #ifdef CONFIG_X86_32
  672. /*
  673. * Did it hit the DOS screen memory VA from vm86 mode?
  674. */
  675. if (v8086_mode(regs)) {
  676. unsigned long bit = (address - 0xA0000) >> PAGE_SHIFT;
  677. if (bit < 32)
  678. tsk->thread.screen_bitmap |= 1 << bit;
  679. }
  680. #endif
  681. up_read(&mm->mmap_sem);
  682. return;
  683. /*
  684. * Something tried to access memory that isn't in our memory map..
  685. * Fix it, but check if it's kernel or user first..
  686. */
  687. bad_area:
  688. up_read(&mm->mmap_sem);
  689. bad_area_nosemaphore:
  690. /* User mode accesses just cause a SIGSEGV */
  691. if (error_code & PF_USER) {
  692. /*
  693. * It's possible to have interrupts off here.
  694. */
  695. local_irq_enable();
  696. /*
  697. * Valid to do another page fault here because this one came
  698. * from user space.
  699. */
  700. if (is_prefetch(regs, address, error_code))
  701. return;
  702. if (is_errata100(regs, address))
  703. return;
  704. if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
  705. printk_ratelimit()) {
  706. printk(
  707. "%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
  708. task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
  709. tsk->comm, task_pid_nr(tsk), address,
  710. (void *) regs->ip, (void *) regs->sp, error_code);
  711. print_vma_addr(" in ", regs->ip);
  712. printk("\n");
  713. }
  714. tsk->thread.cr2 = address;
  715. /* Kernel addresses are always protection faults */
  716. tsk->thread.error_code = error_code | (address >= TASK_SIZE);
  717. tsk->thread.trap_no = 14;
  718. force_sig_info_fault(SIGSEGV, si_code, address, tsk);
  719. return;
  720. }
  721. if (is_f00f_bug(regs, address))
  722. return;
  723. no_context:
  724. /* Are we prepared to handle this kernel fault? */
  725. if (fixup_exception(regs))
  726. return;
  727. /*
  728. * X86_32
  729. * Valid to do another page fault here, because if this fault
  730. * had been triggered by is_prefetch fixup_exception would have
  731. * handled it.
  732. *
  733. * X86_64
  734. * Hall of shame of CPU/BIOS bugs.
  735. */
  736. if (is_prefetch(regs, address, error_code))
  737. return;
  738. if (is_errata93(regs, address))
  739. return;
  740. /*
  741. * Oops. The kernel tried to access some bad page. We'll have to
  742. * terminate things with extreme prejudice.
  743. */
  744. #ifdef CONFIG_X86_32
  745. bust_spinlocks(1);
  746. #else
  747. flags = oops_begin();
  748. #endif
  749. show_fault_oops(regs, error_code, address);
  750. tsk->thread.cr2 = address;
  751. tsk->thread.trap_no = 14;
  752. tsk->thread.error_code = error_code;
  753. #ifdef CONFIG_X86_32
  754. die("Oops", regs, error_code);
  755. bust_spinlocks(0);
  756. do_exit(SIGKILL);
  757. #else
  758. if (__die("Oops", regs, error_code))
  759. regs = NULL;
  760. /* Executive summary in case the body of the oops scrolled away */
  761. printk(KERN_EMERG "CR2: %016lx\n", address);
  762. oops_end(flags, regs, SIGKILL);
  763. #endif
  764. /*
  765. * We ran out of memory, or some other thing happened to us that made
  766. * us unable to handle the page fault gracefully.
  767. */
  768. out_of_memory:
  769. up_read(&mm->mmap_sem);
  770. if (is_global_init(tsk)) {
  771. yield();
  772. #ifdef CONFIG_X86_32
  773. down_read(&mm->mmap_sem);
  774. goto survive;
  775. #else
  776. goto again;
  777. #endif
  778. }
  779. printk("VM: killing process %s\n", tsk->comm);
  780. if (error_code & PF_USER)
  781. do_group_exit(SIGKILL);
  782. goto no_context;
  783. do_sigbus:
  784. up_read(&mm->mmap_sem);
  785. /* Kernel mode? Handle exceptions or die */
  786. if (!(error_code & PF_USER))
  787. goto no_context;
  788. #ifdef CONFIG_X86_32
  789. /* User space => ok to do another page fault */
  790. if (is_prefetch(regs, address, error_code))
  791. return;
  792. #endif
  793. tsk->thread.cr2 = address;
  794. tsk->thread.error_code = error_code;
  795. tsk->thread.trap_no = 14;
  796. force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk);
  797. }
  798. DEFINE_SPINLOCK(pgd_lock);
  799. LIST_HEAD(pgd_list);
  800. void vmalloc_sync_all(void)
  801. {
  802. #ifdef CONFIG_X86_32
  803. /*
  804. * Note that races in the updates of insync and start aren't
  805. * problematic: insync can only get set bits added, and updates to
  806. * start are only improving performance (without affecting correctness
  807. * if undone).
  808. */
  809. static DECLARE_BITMAP(insync, PTRS_PER_PGD);
  810. static unsigned long start = TASK_SIZE;
  811. unsigned long address;
  812. if (SHARED_KERNEL_PMD)
  813. return;
  814. BUILD_BUG_ON(TASK_SIZE & ~PGDIR_MASK);
  815. for (address = start; address >= TASK_SIZE; address += PGDIR_SIZE) {
  816. if (!test_bit(pgd_index(address), insync)) {
  817. unsigned long flags;
  818. struct page *page;
  819. spin_lock_irqsave(&pgd_lock, flags);
  820. list_for_each_entry(page, &pgd_list, lru) {
  821. if (!vmalloc_sync_one(page_address(page),
  822. address))
  823. break;
  824. }
  825. spin_unlock_irqrestore(&pgd_lock, flags);
  826. if (!page)
  827. set_bit(pgd_index(address), insync);
  828. }
  829. if (address == start && test_bit(pgd_index(address), insync))
  830. start = address + PGDIR_SIZE;
  831. }
  832. #else /* CONFIG_X86_64 */
  833. /*
  834. * Note that races in the updates of insync and start aren't
  835. * problematic: insync can only get set bits added, and updates to
  836. * start are only improving performance (without affecting correctness
  837. * if undone).
  838. */
  839. static DECLARE_BITMAP(insync, PTRS_PER_PGD);
  840. static unsigned long start = VMALLOC_START & PGDIR_MASK;
  841. unsigned long address;
  842. for (address = start; address <= VMALLOC_END; address += PGDIR_SIZE) {
  843. if (!test_bit(pgd_index(address), insync)) {
  844. const pgd_t *pgd_ref = pgd_offset_k(address);
  845. unsigned long flags;
  846. struct page *page;
  847. if (pgd_none(*pgd_ref))
  848. continue;
  849. spin_lock_irqsave(&pgd_lock, flags);
  850. list_for_each_entry(page, &pgd_list, lru) {
  851. pgd_t *pgd;
  852. pgd = (pgd_t *)page_address(page) + pgd_index(address);
  853. if (pgd_none(*pgd))
  854. set_pgd(pgd, *pgd_ref);
  855. else
  856. BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
  857. }
  858. spin_unlock_irqrestore(&pgd_lock, flags);
  859. set_bit(pgd_index(address), insync);
  860. }
  861. if (address == start)
  862. start = address + PGDIR_SIZE;
  863. }
  864. #endif
  865. }