fault.c 38 KB

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