fault.c 32 KB

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