fault.c 37 KB

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