fault.c 28 KB

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