fault.c 32 KB

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