fault.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  1. /*
  2. * Copyright (C) 1995 Linus Torvalds
  3. * Copyright (C) 2001, 2002 Andi Kleen, SuSE Labs.
  4. * Copyright (C) 2008-2009, Red Hat Inc., Ingo Molnar
  5. */
  6. #include <linux/magic.h> /* STACK_END_MAGIC */
  7. #include <linux/sched.h> /* test_thread_flag(), ... */
  8. #include <linux/kdebug.h> /* oops_begin/end, ... */
  9. #include <linux/module.h> /* search_exception_table */
  10. #include <linux/bootmem.h> /* max_low_pfn */
  11. #include <linux/kprobes.h> /* __kprobes, ... */
  12. #include <linux/mmiotrace.h> /* kmmio_handler, ... */
  13. #include <linux/perf_event.h> /* perf_sw_event */
  14. #include <linux/hugetlb.h> /* hstate_index_to_shift */
  15. #include <asm/traps.h> /* dotraplinkage, ... */
  16. #include <asm/pgalloc.h> /* pgd_*(), ... */
  17. #include <asm/kmemcheck.h> /* kmemcheck_*(), ... */
  18. /*
  19. * Page fault error code bits:
  20. *
  21. * bit 0 == 0: no page found 1: protection fault
  22. * bit 1 == 0: read access 1: write access
  23. * bit 2 == 0: kernel-mode access 1: user-mode access
  24. * bit 3 == 1: use of reserved bit detected
  25. * bit 4 == 1: fault was an instruction fetch
  26. */
  27. enum x86_pf_error_code {
  28. PF_PROT = 1 << 0,
  29. PF_WRITE = 1 << 1,
  30. PF_USER = 1 << 2,
  31. PF_RSVD = 1 << 3,
  32. PF_INSTR = 1 << 4,
  33. };
  34. /*
  35. * Returns 0 if mmiotrace is disabled, or if the fault is not
  36. * handled by mmiotrace:
  37. */
  38. static inline int __kprobes
  39. kmmio_fault(struct pt_regs *regs, unsigned long addr)
  40. {
  41. if (unlikely(is_kmmio_active()))
  42. if (kmmio_handler(regs, addr) == 1)
  43. return -1;
  44. return 0;
  45. }
  46. static inline int __kprobes notify_page_fault(struct pt_regs *regs)
  47. {
  48. int ret = 0;
  49. /* kprobe_running() needs smp_processor_id() */
  50. if (kprobes_built_in() && !user_mode_vm(regs)) {
  51. preempt_disable();
  52. if (kprobe_running() && kprobe_fault_handler(regs, 14))
  53. ret = 1;
  54. preempt_enable();
  55. }
  56. return ret;
  57. }
  58. /*
  59. * Prefetch quirks:
  60. *
  61. * 32-bit mode:
  62. *
  63. * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
  64. * Check that here and ignore it.
  65. *
  66. * 64-bit mode:
  67. *
  68. * Sometimes the CPU reports invalid exceptions on prefetch.
  69. * Check that here and ignore it.
  70. *
  71. * Opcode checker based on code by Richard Brunner.
  72. */
  73. static inline int
  74. check_prefetch_opcode(struct pt_regs *regs, unsigned char *instr,
  75. unsigned char opcode, int *prefetch)
  76. {
  77. unsigned char instr_hi = opcode & 0xf0;
  78. unsigned char instr_lo = opcode & 0x0f;
  79. switch (instr_hi) {
  80. case 0x20:
  81. case 0x30:
  82. /*
  83. * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
  84. * In X86_64 long mode, the CPU will signal invalid
  85. * opcode if some of these prefixes are present so
  86. * X86_64 will never get here anyway
  87. */
  88. return ((instr_lo & 7) == 0x6);
  89. #ifdef CONFIG_X86_64
  90. case 0x40:
  91. /*
  92. * In AMD64 long mode 0x40..0x4F are valid REX prefixes
  93. * Need to figure out under what instruction mode the
  94. * instruction was issued. Could check the LDT for lm,
  95. * but for now it's good enough to assume that long
  96. * mode only uses well known segments or kernel.
  97. */
  98. return (!user_mode(regs)) || (regs->cs == __USER_CS);
  99. #endif
  100. case 0x60:
  101. /* 0x64 thru 0x67 are valid prefixes in all modes. */
  102. return (instr_lo & 0xC) == 0x4;
  103. case 0xF0:
  104. /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
  105. return !instr_lo || (instr_lo>>1) == 1;
  106. case 0x00:
  107. /* Prefetch instruction is 0x0F0D or 0x0F18 */
  108. if (probe_kernel_address(instr, opcode))
  109. return 0;
  110. *prefetch = (instr_lo == 0xF) &&
  111. (opcode == 0x0D || opcode == 0x18);
  112. return 0;
  113. default:
  114. return 0;
  115. }
  116. }
  117. static int
  118. is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr)
  119. {
  120. unsigned char *max_instr;
  121. unsigned char *instr;
  122. int prefetch = 0;
  123. /*
  124. * If it was a exec (instruction fetch) fault on NX page, then
  125. * do not ignore the fault:
  126. */
  127. if (error_code & PF_INSTR)
  128. return 0;
  129. instr = (void *)convert_ip_to_linear(current, regs);
  130. max_instr = instr + 15;
  131. if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE)
  132. return 0;
  133. while (instr < max_instr) {
  134. unsigned char opcode;
  135. if (probe_kernel_address(instr, opcode))
  136. break;
  137. instr++;
  138. if (!check_prefetch_opcode(regs, instr, opcode, &prefetch))
  139. break;
  140. }
  141. return prefetch;
  142. }
  143. static void
  144. force_sig_info_fault(int si_signo, int si_code, unsigned long address,
  145. struct task_struct *tsk, int fault)
  146. {
  147. unsigned lsb = 0;
  148. siginfo_t info;
  149. info.si_signo = si_signo;
  150. info.si_errno = 0;
  151. info.si_code = si_code;
  152. info.si_addr = (void __user *)address;
  153. if (fault & VM_FAULT_HWPOISON_LARGE)
  154. lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault));
  155. if (fault & VM_FAULT_HWPOISON)
  156. lsb = PAGE_SHIFT;
  157. info.si_addr_lsb = lsb;
  158. force_sig_info(si_signo, &info, tsk);
  159. }
  160. DEFINE_SPINLOCK(pgd_lock);
  161. LIST_HEAD(pgd_list);
  162. #ifdef CONFIG_X86_32
  163. static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
  164. {
  165. unsigned index = pgd_index(address);
  166. pgd_t *pgd_k;
  167. pud_t *pud, *pud_k;
  168. pmd_t *pmd, *pmd_k;
  169. pgd += index;
  170. pgd_k = init_mm.pgd + index;
  171. if (!pgd_present(*pgd_k))
  172. return NULL;
  173. /*
  174. * set_pgd(pgd, *pgd_k); here would be useless on PAE
  175. * and redundant with the set_pmd() on non-PAE. As would
  176. * set_pud.
  177. */
  178. pud = pud_offset(pgd, address);
  179. pud_k = pud_offset(pgd_k, address);
  180. if (!pud_present(*pud_k))
  181. return NULL;
  182. pmd = pmd_offset(pud, address);
  183. pmd_k = pmd_offset(pud_k, address);
  184. if (!pmd_present(*pmd_k))
  185. return NULL;
  186. if (!pmd_present(*pmd))
  187. set_pmd(pmd, *pmd_k);
  188. else
  189. BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
  190. return pmd_k;
  191. }
  192. void vmalloc_sync_all(void)
  193. {
  194. unsigned long address;
  195. if (SHARED_KERNEL_PMD)
  196. return;
  197. for (address = VMALLOC_START & PMD_MASK;
  198. address >= TASK_SIZE && address < FIXADDR_TOP;
  199. address += PMD_SIZE) {
  200. unsigned long flags;
  201. struct page *page;
  202. spin_lock_irqsave(&pgd_lock, flags);
  203. list_for_each_entry(page, &pgd_list, lru) {
  204. if (!vmalloc_sync_one(page_address(page), address))
  205. break;
  206. }
  207. spin_unlock_irqrestore(&pgd_lock, flags);
  208. }
  209. }
  210. /*
  211. * 32-bit:
  212. *
  213. * Handle a fault on the vmalloc or module mapping area
  214. */
  215. static noinline __kprobes int vmalloc_fault(unsigned long address)
  216. {
  217. unsigned long pgd_paddr;
  218. pmd_t *pmd_k;
  219. pte_t *pte_k;
  220. /* Make sure we are in vmalloc area: */
  221. if (!(address >= VMALLOC_START && address < VMALLOC_END))
  222. return -1;
  223. /*
  224. * Synchronize this task's top level page-table
  225. * with the 'reference' page table.
  226. *
  227. * Do _not_ use "current" here. We might be inside
  228. * an interrupt in the middle of a task switch..
  229. */
  230. pgd_paddr = read_cr3();
  231. pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
  232. if (!pmd_k)
  233. return -1;
  234. pte_k = pte_offset_kernel(pmd_k, address);
  235. if (!pte_present(*pte_k))
  236. return -1;
  237. return 0;
  238. }
  239. /*
  240. * Did it hit the DOS screen memory VA from vm86 mode?
  241. */
  242. static inline void
  243. check_v8086_mode(struct pt_regs *regs, unsigned long address,
  244. struct task_struct *tsk)
  245. {
  246. unsigned long bit;
  247. if (!v8086_mode(regs))
  248. return;
  249. bit = (address - 0xA0000) >> PAGE_SHIFT;
  250. if (bit < 32)
  251. tsk->thread.screen_bitmap |= 1 << bit;
  252. }
  253. static bool low_pfn(unsigned long pfn)
  254. {
  255. return pfn < max_low_pfn;
  256. }
  257. static void dump_pagetable(unsigned long address)
  258. {
  259. pgd_t *base = __va(read_cr3());
  260. pgd_t *pgd = &base[pgd_index(address)];
  261. pmd_t *pmd;
  262. pte_t *pte;
  263. #ifdef CONFIG_X86_PAE
  264. printk("*pdpt = %016Lx ", pgd_val(*pgd));
  265. if (!low_pfn(pgd_val(*pgd) >> PAGE_SHIFT) || !pgd_present(*pgd))
  266. goto out;
  267. #endif
  268. pmd = pmd_offset(pud_offset(pgd, address), address);
  269. printk(KERN_CONT "*pde = %0*Lx ", sizeof(*pmd) * 2, (u64)pmd_val(*pmd));
  270. /*
  271. * We must not directly access the pte in the highpte
  272. * case if the page table is located in highmem.
  273. * And let's rather not kmap-atomic the pte, just in case
  274. * it's allocated already:
  275. */
  276. if (!low_pfn(pmd_pfn(*pmd)) || !pmd_present(*pmd) || pmd_large(*pmd))
  277. goto out;
  278. pte = pte_offset_kernel(pmd, address);
  279. printk("*pte = %0*Lx ", sizeof(*pte) * 2, (u64)pte_val(*pte));
  280. out:
  281. printk("\n");
  282. }
  283. #else /* CONFIG_X86_64: */
  284. void vmalloc_sync_all(void)
  285. {
  286. unsigned long address;
  287. for (address = VMALLOC_START & PGDIR_MASK; address <= VMALLOC_END;
  288. address += PGDIR_SIZE) {
  289. const pgd_t *pgd_ref = pgd_offset_k(address);
  290. unsigned long flags;
  291. struct page *page;
  292. if (pgd_none(*pgd_ref))
  293. continue;
  294. spin_lock_irqsave(&pgd_lock, flags);
  295. list_for_each_entry(page, &pgd_list, lru) {
  296. pgd_t *pgd;
  297. pgd = (pgd_t *)page_address(page) + pgd_index(address);
  298. if (pgd_none(*pgd))
  299. set_pgd(pgd, *pgd_ref);
  300. else
  301. BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
  302. }
  303. spin_unlock_irqrestore(&pgd_lock, flags);
  304. }
  305. }
  306. /*
  307. * 64-bit:
  308. *
  309. * Handle a fault on the vmalloc area
  310. *
  311. * This assumes no large pages in there.
  312. */
  313. static noinline __kprobes int vmalloc_fault(unsigned long address)
  314. {
  315. pgd_t *pgd, *pgd_ref;
  316. pud_t *pud, *pud_ref;
  317. pmd_t *pmd, *pmd_ref;
  318. pte_t *pte, *pte_ref;
  319. /* Make sure we are in vmalloc area: */
  320. if (!(address >= VMALLOC_START && address < VMALLOC_END))
  321. return -1;
  322. /*
  323. * Copy kernel mappings over when needed. This can also
  324. * happen within a race in page table update. In the later
  325. * case just flush:
  326. */
  327. pgd = pgd_offset(current->active_mm, address);
  328. pgd_ref = pgd_offset_k(address);
  329. if (pgd_none(*pgd_ref))
  330. return -1;
  331. if (pgd_none(*pgd))
  332. set_pgd(pgd, *pgd_ref);
  333. else
  334. BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
  335. /*
  336. * Below here mismatches are bugs because these lower tables
  337. * are shared:
  338. */
  339. pud = pud_offset(pgd, address);
  340. pud_ref = pud_offset(pgd_ref, address);
  341. if (pud_none(*pud_ref))
  342. return -1;
  343. if (pud_none(*pud) || pud_page_vaddr(*pud) != pud_page_vaddr(*pud_ref))
  344. BUG();
  345. pmd = pmd_offset(pud, address);
  346. pmd_ref = pmd_offset(pud_ref, address);
  347. if (pmd_none(*pmd_ref))
  348. return -1;
  349. if (pmd_none(*pmd) || pmd_page(*pmd) != pmd_page(*pmd_ref))
  350. BUG();
  351. pte_ref = pte_offset_kernel(pmd_ref, address);
  352. if (!pte_present(*pte_ref))
  353. return -1;
  354. pte = pte_offset_kernel(pmd, address);
  355. /*
  356. * Don't use pte_page here, because the mappings can point
  357. * outside mem_map, and the NUMA hash lookup cannot handle
  358. * that:
  359. */
  360. if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
  361. BUG();
  362. return 0;
  363. }
  364. static const char errata93_warning[] =
  365. KERN_ERR
  366. "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
  367. "******* Working around it, but it may cause SEGVs or burn power.\n"
  368. "******* Please consider a BIOS update.\n"
  369. "******* Disabling USB legacy in the BIOS may also help.\n";
  370. /*
  371. * No vm86 mode in 64-bit mode:
  372. */
  373. static inline void
  374. check_v8086_mode(struct pt_regs *regs, unsigned long address,
  375. struct task_struct *tsk)
  376. {
  377. }
  378. static int bad_address(void *p)
  379. {
  380. unsigned long dummy;
  381. return probe_kernel_address((unsigned long *)p, dummy);
  382. }
  383. static void dump_pagetable(unsigned long address)
  384. {
  385. pgd_t *base = __va(read_cr3() & PHYSICAL_PAGE_MASK);
  386. pgd_t *pgd = base + pgd_index(address);
  387. pud_t *pud;
  388. pmd_t *pmd;
  389. pte_t *pte;
  390. if (bad_address(pgd))
  391. goto bad;
  392. printk("PGD %lx ", pgd_val(*pgd));
  393. if (!pgd_present(*pgd))
  394. goto out;
  395. pud = pud_offset(pgd, address);
  396. if (bad_address(pud))
  397. goto bad;
  398. printk("PUD %lx ", pud_val(*pud));
  399. if (!pud_present(*pud) || pud_large(*pud))
  400. goto out;
  401. pmd = pmd_offset(pud, address);
  402. if (bad_address(pmd))
  403. goto bad;
  404. printk("PMD %lx ", pmd_val(*pmd));
  405. if (!pmd_present(*pmd) || pmd_large(*pmd))
  406. goto out;
  407. pte = pte_offset_kernel(pmd, address);
  408. if (bad_address(pte))
  409. goto bad;
  410. printk("PTE %lx", pte_val(*pte));
  411. out:
  412. printk("\n");
  413. return;
  414. bad:
  415. printk("BAD\n");
  416. }
  417. #endif /* CONFIG_X86_64 */
  418. /*
  419. * Workaround for K8 erratum #93 & buggy BIOS.
  420. *
  421. * BIOS SMM functions are required to use a specific workaround
  422. * to avoid corruption of the 64bit RIP register on C stepping K8.
  423. *
  424. * A lot of BIOS that didn't get tested properly miss this.
  425. *
  426. * The OS sees this as a page fault with the upper 32bits of RIP cleared.
  427. * Try to work around it here.
  428. *
  429. * Note we only handle faults in kernel here.
  430. * Does nothing on 32-bit.
  431. */
  432. static int is_errata93(struct pt_regs *regs, unsigned long address)
  433. {
  434. #ifdef CONFIG_X86_64
  435. if (address != regs->ip)
  436. return 0;
  437. if ((address >> 32) != 0)
  438. return 0;
  439. address |= 0xffffffffUL << 32;
  440. if ((address >= (u64)_stext && address <= (u64)_etext) ||
  441. (address >= MODULES_VADDR && address <= MODULES_END)) {
  442. printk_once(errata93_warning);
  443. regs->ip = address;
  444. return 1;
  445. }
  446. #endif
  447. return 0;
  448. }
  449. /*
  450. * Work around K8 erratum #100 K8 in compat mode occasionally jumps
  451. * to illegal addresses >4GB.
  452. *
  453. * We catch this in the page fault handler because these addresses
  454. * are not reachable. Just detect this case and return. Any code
  455. * segment in LDT is compatibility mode.
  456. */
  457. static int is_errata100(struct pt_regs *regs, unsigned long address)
  458. {
  459. #ifdef CONFIG_X86_64
  460. if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32))
  461. return 1;
  462. #endif
  463. return 0;
  464. }
  465. static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
  466. {
  467. #ifdef CONFIG_X86_F00F_BUG
  468. unsigned long nr;
  469. /*
  470. * Pentium F0 0F C7 C8 bug workaround:
  471. */
  472. if (boot_cpu_data.f00f_bug) {
  473. nr = (address - idt_descr.address) >> 3;
  474. if (nr == 6) {
  475. do_invalid_op(regs, 0);
  476. return 1;
  477. }
  478. }
  479. #endif
  480. return 0;
  481. }
  482. static const char nx_warning[] = KERN_CRIT
  483. "kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n";
  484. static void
  485. show_fault_oops(struct pt_regs *regs, unsigned long error_code,
  486. unsigned long address)
  487. {
  488. if (!oops_may_print())
  489. return;
  490. if (error_code & PF_INSTR) {
  491. unsigned int level;
  492. pte_t *pte = lookup_address(address, &level);
  493. if (pte && pte_present(*pte) && !pte_exec(*pte))
  494. printk(nx_warning, current_uid());
  495. }
  496. printk(KERN_ALERT "BUG: unable to handle kernel ");
  497. if (address < PAGE_SIZE)
  498. printk(KERN_CONT "NULL pointer dereference");
  499. else
  500. printk(KERN_CONT "paging request");
  501. printk(KERN_CONT " at %p\n", (void *) address);
  502. printk(KERN_ALERT "IP:");
  503. printk_address(regs->ip, 1);
  504. dump_pagetable(address);
  505. }
  506. static noinline void
  507. pgtable_bad(struct pt_regs *regs, unsigned long error_code,
  508. unsigned long address)
  509. {
  510. struct task_struct *tsk;
  511. unsigned long flags;
  512. int sig;
  513. flags = oops_begin();
  514. tsk = current;
  515. sig = SIGKILL;
  516. printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
  517. tsk->comm, address);
  518. dump_pagetable(address);
  519. tsk->thread.cr2 = address;
  520. tsk->thread.trap_no = 14;
  521. tsk->thread.error_code = error_code;
  522. if (__die("Bad pagetable", regs, error_code))
  523. sig = 0;
  524. oops_end(flags, regs, sig);
  525. }
  526. static noinline void
  527. no_context(struct pt_regs *regs, unsigned long error_code,
  528. unsigned long address)
  529. {
  530. struct task_struct *tsk = current;
  531. unsigned long *stackend;
  532. unsigned long flags;
  533. int sig;
  534. /* Are we prepared to handle this kernel fault? */
  535. if (fixup_exception(regs))
  536. return;
  537. /*
  538. * 32-bit:
  539. *
  540. * Valid to do another page fault here, because if this fault
  541. * had been triggered by is_prefetch fixup_exception would have
  542. * handled it.
  543. *
  544. * 64-bit:
  545. *
  546. * Hall of shame of CPU/BIOS bugs.
  547. */
  548. if (is_prefetch(regs, error_code, address))
  549. return;
  550. if (is_errata93(regs, address))
  551. return;
  552. /*
  553. * Oops. The kernel tried to access some bad page. We'll have to
  554. * terminate things with extreme prejudice:
  555. */
  556. flags = oops_begin();
  557. show_fault_oops(regs, error_code, address);
  558. stackend = end_of_stack(tsk);
  559. if (tsk != &init_task && *stackend != STACK_END_MAGIC)
  560. printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
  561. tsk->thread.cr2 = address;
  562. tsk->thread.trap_no = 14;
  563. tsk->thread.error_code = error_code;
  564. sig = SIGKILL;
  565. if (__die("Oops", regs, error_code))
  566. sig = 0;
  567. /* Executive summary in case the body of the oops scrolled away */
  568. printk(KERN_EMERG "CR2: %016lx\n", address);
  569. oops_end(flags, regs, sig);
  570. }
  571. /*
  572. * Print out info about fatal segfaults, if the show_unhandled_signals
  573. * sysctl is set:
  574. */
  575. static inline void
  576. show_signal_msg(struct pt_regs *regs, unsigned long error_code,
  577. unsigned long address, struct task_struct *tsk)
  578. {
  579. if (!unhandled_signal(tsk, SIGSEGV))
  580. return;
  581. if (!printk_ratelimit())
  582. return;
  583. printk("%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
  584. task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
  585. tsk->comm, task_pid_nr(tsk), address,
  586. (void *)regs->ip, (void *)regs->sp, error_code);
  587. print_vma_addr(KERN_CONT " in ", regs->ip);
  588. printk(KERN_CONT "\n");
  589. }
  590. static void
  591. __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
  592. unsigned long address, int si_code)
  593. {
  594. struct task_struct *tsk = current;
  595. /* User mode accesses just cause a SIGSEGV */
  596. if (error_code & PF_USER) {
  597. /*
  598. * It's possible to have interrupts off here:
  599. */
  600. local_irq_enable();
  601. /*
  602. * Valid to do another page fault here because this one came
  603. * from user space:
  604. */
  605. if (is_prefetch(regs, error_code, address))
  606. return;
  607. if (is_errata100(regs, address))
  608. return;
  609. if (unlikely(show_unhandled_signals))
  610. show_signal_msg(regs, error_code, address, tsk);
  611. /* Kernel addresses are always protection faults: */
  612. tsk->thread.cr2 = address;
  613. tsk->thread.error_code = error_code | (address >= TASK_SIZE);
  614. tsk->thread.trap_no = 14;
  615. force_sig_info_fault(SIGSEGV, si_code, address, tsk, 0);
  616. return;
  617. }
  618. if (is_f00f_bug(regs, address))
  619. return;
  620. no_context(regs, error_code, address);
  621. }
  622. static noinline void
  623. bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
  624. unsigned long address)
  625. {
  626. __bad_area_nosemaphore(regs, error_code, address, SEGV_MAPERR);
  627. }
  628. static void
  629. __bad_area(struct pt_regs *regs, unsigned long error_code,
  630. unsigned long address, int si_code)
  631. {
  632. struct mm_struct *mm = current->mm;
  633. /*
  634. * Something tried to access memory that isn't in our memory map..
  635. * Fix it, but check if it's kernel or user first..
  636. */
  637. up_read(&mm->mmap_sem);
  638. __bad_area_nosemaphore(regs, error_code, address, si_code);
  639. }
  640. static noinline void
  641. bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
  642. {
  643. __bad_area(regs, error_code, address, SEGV_MAPERR);
  644. }
  645. static noinline void
  646. bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
  647. unsigned long address)
  648. {
  649. __bad_area(regs, error_code, address, SEGV_ACCERR);
  650. }
  651. /* TODO: fixup for "mm-invoke-oom-killer-from-page-fault.patch" */
  652. static void
  653. out_of_memory(struct pt_regs *regs, unsigned long error_code,
  654. unsigned long address)
  655. {
  656. /*
  657. * We ran out of memory, call the OOM killer, and return the userspace
  658. * (which will retry the fault, or kill us if we got oom-killed):
  659. */
  660. up_read(&current->mm->mmap_sem);
  661. pagefault_out_of_memory();
  662. }
  663. static void
  664. do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address,
  665. unsigned int fault)
  666. {
  667. struct task_struct *tsk = current;
  668. struct mm_struct *mm = tsk->mm;
  669. int code = BUS_ADRERR;
  670. up_read(&mm->mmap_sem);
  671. /* Kernel mode? Handle exceptions or die: */
  672. if (!(error_code & PF_USER)) {
  673. no_context(regs, error_code, address);
  674. return;
  675. }
  676. /* User-space => ok to do another page fault: */
  677. if (is_prefetch(regs, error_code, address))
  678. return;
  679. tsk->thread.cr2 = address;
  680. tsk->thread.error_code = error_code;
  681. tsk->thread.trap_no = 14;
  682. #ifdef CONFIG_MEMORY_FAILURE
  683. if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
  684. printk(KERN_ERR
  685. "MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n",
  686. tsk->comm, tsk->pid, address);
  687. code = BUS_MCEERR_AR;
  688. }
  689. #endif
  690. force_sig_info_fault(SIGBUS, code, address, tsk, fault);
  691. }
  692. static noinline void
  693. mm_fault_error(struct pt_regs *regs, unsigned long error_code,
  694. unsigned long address, unsigned int fault)
  695. {
  696. if (fault & VM_FAULT_OOM) {
  697. out_of_memory(regs, error_code, address);
  698. } else {
  699. if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|
  700. VM_FAULT_HWPOISON_LARGE))
  701. do_sigbus(regs, error_code, address, fault);
  702. else
  703. BUG();
  704. }
  705. }
  706. static int spurious_fault_check(unsigned long error_code, pte_t *pte)
  707. {
  708. if ((error_code & PF_WRITE) && !pte_write(*pte))
  709. return 0;
  710. if ((error_code & PF_INSTR) && !pte_exec(*pte))
  711. return 0;
  712. return 1;
  713. }
  714. /*
  715. * Handle a spurious fault caused by a stale TLB entry.
  716. *
  717. * This allows us to lazily refresh the TLB when increasing the
  718. * permissions of a kernel page (RO -> RW or NX -> X). Doing it
  719. * eagerly is very expensive since that implies doing a full
  720. * cross-processor TLB flush, even if no stale TLB entries exist
  721. * on other processors.
  722. *
  723. * There are no security implications to leaving a stale TLB when
  724. * increasing the permissions on a page.
  725. */
  726. static noinline __kprobes int
  727. spurious_fault(unsigned long error_code, unsigned long address)
  728. {
  729. pgd_t *pgd;
  730. pud_t *pud;
  731. pmd_t *pmd;
  732. pte_t *pte;
  733. int ret;
  734. /* Reserved-bit violation or user access to kernel space? */
  735. if (error_code & (PF_USER | PF_RSVD))
  736. return 0;
  737. pgd = init_mm.pgd + pgd_index(address);
  738. if (!pgd_present(*pgd))
  739. return 0;
  740. pud = pud_offset(pgd, address);
  741. if (!pud_present(*pud))
  742. return 0;
  743. if (pud_large(*pud))
  744. return spurious_fault_check(error_code, (pte_t *) pud);
  745. pmd = pmd_offset(pud, address);
  746. if (!pmd_present(*pmd))
  747. return 0;
  748. if (pmd_large(*pmd))
  749. return spurious_fault_check(error_code, (pte_t *) pmd);
  750. pte = pte_offset_kernel(pmd, address);
  751. if (!pte_present(*pte))
  752. return 0;
  753. ret = spurious_fault_check(error_code, pte);
  754. if (!ret)
  755. return 0;
  756. /*
  757. * Make sure we have permissions in PMD.
  758. * If not, then there's a bug in the page tables:
  759. */
  760. ret = spurious_fault_check(error_code, (pte_t *) pmd);
  761. WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
  762. return ret;
  763. }
  764. int show_unhandled_signals = 1;
  765. static inline int
  766. access_error(unsigned long error_code, int write, struct vm_area_struct *vma)
  767. {
  768. if (write) {
  769. /* write, present and write, not present: */
  770. if (unlikely(!(vma->vm_flags & VM_WRITE)))
  771. return 1;
  772. return 0;
  773. }
  774. /* read, present: */
  775. if (unlikely(error_code & PF_PROT))
  776. return 1;
  777. /* read, not present: */
  778. if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
  779. return 1;
  780. return 0;
  781. }
  782. static int fault_in_kernel_space(unsigned long address)
  783. {
  784. return address >= TASK_SIZE_MAX;
  785. }
  786. /*
  787. * This routine handles page faults. It determines the address,
  788. * and the problem, and then passes it off to one of the appropriate
  789. * routines.
  790. */
  791. dotraplinkage void __kprobes
  792. do_page_fault(struct pt_regs *regs, unsigned long error_code)
  793. {
  794. struct vm_area_struct *vma;
  795. struct task_struct *tsk;
  796. unsigned long address;
  797. struct mm_struct *mm;
  798. int write;
  799. int fault;
  800. tsk = current;
  801. mm = tsk->mm;
  802. /* Get the faulting address: */
  803. address = read_cr2();
  804. /*
  805. * Detect and handle instructions that would cause a page fault for
  806. * both a tracked kernel page and a userspace page.
  807. */
  808. if (kmemcheck_active(regs))
  809. kmemcheck_hide(regs);
  810. prefetchw(&mm->mmap_sem);
  811. if (unlikely(kmmio_fault(regs, address)))
  812. return;
  813. /*
  814. * We fault-in kernel-space virtual memory on-demand. The
  815. * 'reference' page table is init_mm.pgd.
  816. *
  817. * NOTE! We MUST NOT take any locks for this case. We may
  818. * be in an interrupt or a critical region, and should
  819. * only copy the information from the master page table,
  820. * nothing more.
  821. *
  822. * This verifies that the fault happens in kernel space
  823. * (error_code & 4) == 0, and that the fault was not a
  824. * protection error (error_code & 9) == 0.
  825. */
  826. if (unlikely(fault_in_kernel_space(address))) {
  827. if (!(error_code & (PF_RSVD | PF_USER | PF_PROT))) {
  828. if (vmalloc_fault(address) >= 0)
  829. return;
  830. if (kmemcheck_fault(regs, address, error_code))
  831. return;
  832. }
  833. /* Can handle a stale RO->RW TLB: */
  834. if (spurious_fault(error_code, address))
  835. return;
  836. /* kprobes don't want to hook the spurious faults: */
  837. if (notify_page_fault(regs))
  838. return;
  839. /*
  840. * Don't take the mm semaphore here. If we fixup a prefetch
  841. * fault we could otherwise deadlock:
  842. */
  843. bad_area_nosemaphore(regs, error_code, address);
  844. return;
  845. }
  846. /* kprobes don't want to hook the spurious faults: */
  847. if (unlikely(notify_page_fault(regs)))
  848. return;
  849. /*
  850. * It's safe to allow irq's after cr2 has been saved and the
  851. * vmalloc fault has been handled.
  852. *
  853. * User-mode registers count as a user access even for any
  854. * potential system fault or CPU buglet:
  855. */
  856. if (user_mode_vm(regs)) {
  857. local_irq_enable();
  858. error_code |= PF_USER;
  859. } else {
  860. if (regs->flags & X86_EFLAGS_IF)
  861. local_irq_enable();
  862. }
  863. if (unlikely(error_code & PF_RSVD))
  864. pgtable_bad(regs, error_code, address);
  865. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, 0, regs, address);
  866. /*
  867. * If we're in an interrupt, have no user context or are running
  868. * in an atomic region then we must not take the fault:
  869. */
  870. if (unlikely(in_atomic() || !mm)) {
  871. bad_area_nosemaphore(regs, error_code, address);
  872. return;
  873. }
  874. /*
  875. * When running in the kernel we expect faults to occur only to
  876. * addresses in user space. All other faults represent errors in
  877. * the kernel and should generate an OOPS. Unfortunately, in the
  878. * case of an erroneous fault occurring in a code path which already
  879. * holds mmap_sem we will deadlock attempting to validate the fault
  880. * against the address space. Luckily the kernel only validly
  881. * references user space from well defined areas of code, which are
  882. * listed in the exceptions table.
  883. *
  884. * As the vast majority of faults will be valid we will only perform
  885. * the source reference check when there is a possibility of a
  886. * deadlock. Attempt to lock the address space, if we cannot we then
  887. * validate the source. If this is invalid we can skip the address
  888. * space check, thus avoiding the deadlock:
  889. */
  890. if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
  891. if ((error_code & PF_USER) == 0 &&
  892. !search_exception_tables(regs->ip)) {
  893. bad_area_nosemaphore(regs, error_code, address);
  894. return;
  895. }
  896. down_read(&mm->mmap_sem);
  897. } else {
  898. /*
  899. * The above down_read_trylock() might have succeeded in
  900. * which case we'll have missed the might_sleep() from
  901. * down_read():
  902. */
  903. might_sleep();
  904. }
  905. vma = find_vma(mm, address);
  906. if (unlikely(!vma)) {
  907. bad_area(regs, error_code, address);
  908. return;
  909. }
  910. if (likely(vma->vm_start <= address))
  911. goto good_area;
  912. if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
  913. bad_area(regs, error_code, address);
  914. return;
  915. }
  916. if (error_code & PF_USER) {
  917. /*
  918. * Accessing the stack below %sp is always a bug.
  919. * The large cushion allows instructions like enter
  920. * and pusha to work. ("enter $65535, $31" pushes
  921. * 32 pointers and then decrements %sp by 65535.)
  922. */
  923. if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
  924. bad_area(regs, error_code, address);
  925. return;
  926. }
  927. }
  928. if (unlikely(expand_stack(vma, address))) {
  929. bad_area(regs, error_code, address);
  930. return;
  931. }
  932. /*
  933. * Ok, we have a good vm_area for this memory access, so
  934. * we can handle it..
  935. */
  936. good_area:
  937. write = error_code & PF_WRITE;
  938. if (unlikely(access_error(error_code, write, vma))) {
  939. bad_area_access_error(regs, error_code, address);
  940. return;
  941. }
  942. /*
  943. * If for any reason at all we couldn't handle the fault,
  944. * make sure we exit gracefully rather than endlessly redo
  945. * the fault:
  946. */
  947. fault = handle_mm_fault(mm, vma, address, write ? FAULT_FLAG_WRITE : 0);
  948. if (unlikely(fault & VM_FAULT_ERROR)) {
  949. mm_fault_error(regs, error_code, address, fault);
  950. return;
  951. }
  952. if (fault & VM_FAULT_MAJOR) {
  953. tsk->maj_flt++;
  954. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, 0,
  955. regs, address);
  956. } else {
  957. tsk->min_flt++;
  958. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, 0,
  959. regs, address);
  960. }
  961. check_v8086_mode(regs, address, tsk);
  962. up_read(&mm->mmap_sem);
  963. }