fault.c 38 KB

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