fault.c 35 KB

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