fault.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  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_MAX && 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. if (pmd_huge(*pmd_k))
  303. return 0;
  304. pte_k = pte_offset_kernel(pmd_k, address);
  305. if (!pte_present(*pte_k))
  306. return -1;
  307. return 0;
  308. }
  309. NOKPROBE_SYMBOL(vmalloc_fault);
  310. /*
  311. * Did it hit the DOS screen memory VA from vm86 mode?
  312. */
  313. static inline void
  314. check_v8086_mode(struct pt_regs *regs, unsigned long address,
  315. struct task_struct *tsk)
  316. {
  317. #ifdef CONFIG_VM86
  318. unsigned long bit;
  319. if (!v8086_mode(regs) || !tsk->thread.vm86)
  320. return;
  321. bit = (address - 0xA0000) >> PAGE_SHIFT;
  322. if (bit < 32)
  323. tsk->thread.vm86->screen_bitmap |= 1 << bit;
  324. #endif
  325. }
  326. static bool low_pfn(unsigned long pfn)
  327. {
  328. return pfn < max_low_pfn;
  329. }
  330. static void dump_pagetable(unsigned long address)
  331. {
  332. pgd_t *base = __va(read_cr3());
  333. pgd_t *pgd = &base[pgd_index(address)];
  334. pmd_t *pmd;
  335. pte_t *pte;
  336. #ifdef CONFIG_X86_PAE
  337. printk("*pdpt = %016Lx ", pgd_val(*pgd));
  338. if (!low_pfn(pgd_val(*pgd) >> PAGE_SHIFT) || !pgd_present(*pgd))
  339. goto out;
  340. #endif
  341. pmd = pmd_offset(pud_offset(pgd, address), address);
  342. printk(KERN_CONT "*pde = %0*Lx ", sizeof(*pmd) * 2, (u64)pmd_val(*pmd));
  343. /*
  344. * We must not directly access the pte in the highpte
  345. * case if the page table is located in highmem.
  346. * And let's rather not kmap-atomic the pte, just in case
  347. * it's allocated already:
  348. */
  349. if (!low_pfn(pmd_pfn(*pmd)) || !pmd_present(*pmd) || pmd_large(*pmd))
  350. goto out;
  351. pte = pte_offset_kernel(pmd, address);
  352. printk("*pte = %0*Lx ", sizeof(*pte) * 2, (u64)pte_val(*pte));
  353. out:
  354. printk("\n");
  355. }
  356. #else /* CONFIG_X86_64: */
  357. void vmalloc_sync_all(void)
  358. {
  359. sync_global_pgds(VMALLOC_START & PGDIR_MASK, VMALLOC_END, 0);
  360. }
  361. /*
  362. * 64-bit:
  363. *
  364. * Handle a fault on the vmalloc area
  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_t *)__va(read_cr3()) + pgd_index(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_pfn(*pud) != pud_pfn(*pud_ref))
  400. BUG();
  401. if (pud_huge(*pud))
  402. return 0;
  403. pmd = pmd_offset(pud, address);
  404. pmd_ref = pmd_offset(pud_ref, address);
  405. if (pmd_none(*pmd_ref))
  406. return -1;
  407. if (pmd_none(*pmd) || pmd_pfn(*pmd) != pmd_pfn(*pmd_ref))
  408. BUG();
  409. if (pmd_huge(*pmd))
  410. return 0;
  411. pte_ref = pte_offset_kernel(pmd_ref, address);
  412. if (!pte_present(*pte_ref))
  413. return -1;
  414. pte = pte_offset_kernel(pmd, address);
  415. /*
  416. * Don't use pte_page here, because the mappings can point
  417. * outside mem_map, and the NUMA hash lookup cannot handle
  418. * that:
  419. */
  420. if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
  421. BUG();
  422. return 0;
  423. }
  424. NOKPROBE_SYMBOL(vmalloc_fault);
  425. #ifdef CONFIG_CPU_SUP_AMD
  426. static const char errata93_warning[] =
  427. KERN_ERR
  428. "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
  429. "******* Working around it, but it may cause SEGVs or burn power.\n"
  430. "******* Please consider a BIOS update.\n"
  431. "******* Disabling USB legacy in the BIOS may also help.\n";
  432. #endif
  433. /*
  434. * No vm86 mode in 64-bit mode:
  435. */
  436. static inline void
  437. check_v8086_mode(struct pt_regs *regs, unsigned long address,
  438. struct task_struct *tsk)
  439. {
  440. }
  441. static int bad_address(void *p)
  442. {
  443. unsigned long dummy;
  444. return probe_kernel_address((unsigned long *)p, dummy);
  445. }
  446. static void dump_pagetable(unsigned long address)
  447. {
  448. pgd_t *base = __va(read_cr3() & PHYSICAL_PAGE_MASK);
  449. pgd_t *pgd = base + pgd_index(address);
  450. pud_t *pud;
  451. pmd_t *pmd;
  452. pte_t *pte;
  453. if (bad_address(pgd))
  454. goto bad;
  455. printk("PGD %lx ", pgd_val(*pgd));
  456. if (!pgd_present(*pgd))
  457. goto out;
  458. pud = pud_offset(pgd, address);
  459. if (bad_address(pud))
  460. goto bad;
  461. printk("PUD %lx ", pud_val(*pud));
  462. if (!pud_present(*pud) || pud_large(*pud))
  463. goto out;
  464. pmd = pmd_offset(pud, address);
  465. if (bad_address(pmd))
  466. goto bad;
  467. printk("PMD %lx ", pmd_val(*pmd));
  468. if (!pmd_present(*pmd) || pmd_large(*pmd))
  469. goto out;
  470. pte = pte_offset_kernel(pmd, address);
  471. if (bad_address(pte))
  472. goto bad;
  473. printk("PTE %lx", pte_val(*pte));
  474. out:
  475. printk("\n");
  476. return;
  477. bad:
  478. printk("BAD\n");
  479. }
  480. #endif /* CONFIG_X86_64 */
  481. /*
  482. * Workaround for K8 erratum #93 & buggy BIOS.
  483. *
  484. * BIOS SMM functions are required to use a specific workaround
  485. * to avoid corruption of the 64bit RIP register on C stepping K8.
  486. *
  487. * A lot of BIOS that didn't get tested properly miss this.
  488. *
  489. * The OS sees this as a page fault with the upper 32bits of RIP cleared.
  490. * Try to work around it here.
  491. *
  492. * Note we only handle faults in kernel here.
  493. * Does nothing on 32-bit.
  494. */
  495. static int is_errata93(struct pt_regs *regs, unsigned long address)
  496. {
  497. #if defined(CONFIG_X86_64) && defined(CONFIG_CPU_SUP_AMD)
  498. if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD
  499. || boot_cpu_data.x86 != 0xf)
  500. return 0;
  501. if (address != regs->ip)
  502. return 0;
  503. if ((address >> 32) != 0)
  504. return 0;
  505. address |= 0xffffffffUL << 32;
  506. if ((address >= (u64)_stext && address <= (u64)_etext) ||
  507. (address >= MODULES_VADDR && address <= MODULES_END)) {
  508. printk_once(errata93_warning);
  509. regs->ip = address;
  510. return 1;
  511. }
  512. #endif
  513. return 0;
  514. }
  515. /*
  516. * Work around K8 erratum #100 K8 in compat mode occasionally jumps
  517. * to illegal addresses >4GB.
  518. *
  519. * We catch this in the page fault handler because these addresses
  520. * are not reachable. Just detect this case and return. Any code
  521. * segment in LDT is compatibility mode.
  522. */
  523. static int is_errata100(struct pt_regs *regs, unsigned long address)
  524. {
  525. #ifdef CONFIG_X86_64
  526. if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32))
  527. return 1;
  528. #endif
  529. return 0;
  530. }
  531. static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
  532. {
  533. #ifdef CONFIG_X86_F00F_BUG
  534. unsigned long nr;
  535. /*
  536. * Pentium F0 0F C7 C8 bug workaround:
  537. */
  538. if (boot_cpu_has_bug(X86_BUG_F00F)) {
  539. nr = (address - idt_descr.address) >> 3;
  540. if (nr == 6) {
  541. do_invalid_op(regs, 0);
  542. return 1;
  543. }
  544. }
  545. #endif
  546. return 0;
  547. }
  548. static const char nx_warning[] = KERN_CRIT
  549. "kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n";
  550. static const char smep_warning[] = KERN_CRIT
  551. "unable to execute userspace code (SMEP?) (uid: %d)\n";
  552. static void
  553. show_fault_oops(struct pt_regs *regs, unsigned long error_code,
  554. unsigned long address)
  555. {
  556. if (!oops_may_print())
  557. return;
  558. if (error_code & PF_INSTR) {
  559. unsigned int level;
  560. pgd_t *pgd;
  561. pte_t *pte;
  562. pgd = __va(read_cr3() & PHYSICAL_PAGE_MASK);
  563. pgd += pgd_index(address);
  564. pte = lookup_address_in_pgd(pgd, address, &level);
  565. if (pte && pte_present(*pte) && !pte_exec(*pte))
  566. printk(nx_warning, from_kuid(&init_user_ns, current_uid()));
  567. if (pte && pte_present(*pte) && pte_exec(*pte) &&
  568. (pgd_flags(*pgd) & _PAGE_USER) &&
  569. (__read_cr4() & X86_CR4_SMEP))
  570. printk(smep_warning, from_kuid(&init_user_ns, current_uid()));
  571. }
  572. printk(KERN_ALERT "BUG: unable to handle kernel ");
  573. if (address < PAGE_SIZE)
  574. printk(KERN_CONT "NULL pointer dereference");
  575. else
  576. printk(KERN_CONT "paging request");
  577. printk(KERN_CONT " at %p\n", (void *) address);
  578. printk(KERN_ALERT "IP:");
  579. printk_address(regs->ip);
  580. dump_pagetable(address);
  581. }
  582. static noinline void
  583. pgtable_bad(struct pt_regs *regs, unsigned long error_code,
  584. unsigned long address)
  585. {
  586. struct task_struct *tsk;
  587. unsigned long flags;
  588. int sig;
  589. flags = oops_begin();
  590. tsk = current;
  591. sig = SIGKILL;
  592. printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
  593. tsk->comm, address);
  594. dump_pagetable(address);
  595. tsk->thread.cr2 = address;
  596. tsk->thread.trap_nr = X86_TRAP_PF;
  597. tsk->thread.error_code = error_code;
  598. if (__die("Bad pagetable", regs, error_code))
  599. sig = 0;
  600. oops_end(flags, regs, sig);
  601. }
  602. static noinline void
  603. no_context(struct pt_regs *regs, unsigned long error_code,
  604. unsigned long address, int signal, int si_code)
  605. {
  606. struct task_struct *tsk = current;
  607. unsigned long flags;
  608. int sig;
  609. /* No context means no VMA to pass down */
  610. struct vm_area_struct *vma = NULL;
  611. /* Are we prepared to handle this kernel fault? */
  612. if (fixup_exception(regs, X86_TRAP_PF)) {
  613. /*
  614. * Any interrupt that takes a fault gets the fixup. This makes
  615. * the below recursive fault logic only apply to a faults from
  616. * task context.
  617. */
  618. if (in_interrupt())
  619. return;
  620. /*
  621. * Per the above we're !in_interrupt(), aka. task context.
  622. *
  623. * In this case we need to make sure we're not recursively
  624. * faulting through the emulate_vsyscall() logic.
  625. */
  626. if (current->thread.sig_on_uaccess_err && signal) {
  627. tsk->thread.trap_nr = X86_TRAP_PF;
  628. tsk->thread.error_code = error_code | PF_USER;
  629. tsk->thread.cr2 = address;
  630. /* XXX: hwpoison faults will set the wrong code. */
  631. force_sig_info_fault(signal, si_code, address,
  632. tsk, vma, 0);
  633. }
  634. /*
  635. * Barring that, we can do the fixup and be happy.
  636. */
  637. return;
  638. }
  639. /*
  640. * 32-bit:
  641. *
  642. * Valid to do another page fault here, because if this fault
  643. * had been triggered by is_prefetch fixup_exception would have
  644. * handled it.
  645. *
  646. * 64-bit:
  647. *
  648. * Hall of shame of CPU/BIOS bugs.
  649. */
  650. if (is_prefetch(regs, error_code, address))
  651. return;
  652. if (is_errata93(regs, address))
  653. return;
  654. /*
  655. * Oops. The kernel tried to access some bad page. We'll have to
  656. * terminate things with extreme prejudice:
  657. */
  658. flags = oops_begin();
  659. show_fault_oops(regs, error_code, address);
  660. if (task_stack_end_corrupted(tsk))
  661. printk(KERN_EMERG "Thread overran stack, or stack corrupted\n");
  662. tsk->thread.cr2 = address;
  663. tsk->thread.trap_nr = X86_TRAP_PF;
  664. tsk->thread.error_code = error_code;
  665. sig = SIGKILL;
  666. if (__die("Oops", regs, error_code))
  667. sig = 0;
  668. /* Executive summary in case the body of the oops scrolled away */
  669. printk(KERN_DEFAULT "CR2: %016lx\n", address);
  670. oops_end(flags, regs, sig);
  671. }
  672. /*
  673. * Print out info about fatal segfaults, if the show_unhandled_signals
  674. * sysctl is set:
  675. */
  676. static inline void
  677. show_signal_msg(struct pt_regs *regs, unsigned long error_code,
  678. unsigned long address, struct task_struct *tsk)
  679. {
  680. if (!unhandled_signal(tsk, SIGSEGV))
  681. return;
  682. if (!printk_ratelimit())
  683. return;
  684. printk("%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
  685. task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
  686. tsk->comm, task_pid_nr(tsk), address,
  687. (void *)regs->ip, (void *)regs->sp, error_code);
  688. print_vma_addr(KERN_CONT " in ", regs->ip);
  689. printk(KERN_CONT "\n");
  690. }
  691. static void
  692. __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
  693. unsigned long address, struct vm_area_struct *vma,
  694. int si_code)
  695. {
  696. struct task_struct *tsk = current;
  697. /* User mode accesses just cause a SIGSEGV */
  698. if (error_code & PF_USER) {
  699. /*
  700. * It's possible to have interrupts off here:
  701. */
  702. local_irq_enable();
  703. /*
  704. * Valid to do another page fault here because this one came
  705. * from user space:
  706. */
  707. if (is_prefetch(regs, error_code, address))
  708. return;
  709. if (is_errata100(regs, address))
  710. return;
  711. #ifdef CONFIG_X86_64
  712. /*
  713. * Instruction fetch faults in the vsyscall page might need
  714. * emulation.
  715. */
  716. if (unlikely((error_code & PF_INSTR) &&
  717. ((address & ~0xfff) == VSYSCALL_ADDR))) {
  718. if (emulate_vsyscall(regs, address))
  719. return;
  720. }
  721. #endif
  722. /*
  723. * To avoid leaking information about the kernel page table
  724. * layout, pretend that user-mode accesses to kernel addresses
  725. * are always protection faults.
  726. */
  727. if (address >= TASK_SIZE_MAX)
  728. error_code |= PF_PROT;
  729. if (likely(show_unhandled_signals))
  730. show_signal_msg(regs, error_code, address, tsk);
  731. tsk->thread.cr2 = address;
  732. tsk->thread.error_code = error_code;
  733. tsk->thread.trap_nr = X86_TRAP_PF;
  734. force_sig_info_fault(SIGSEGV, si_code, address, tsk, vma, 0);
  735. return;
  736. }
  737. if (is_f00f_bug(regs, address))
  738. return;
  739. no_context(regs, error_code, address, SIGSEGV, si_code);
  740. }
  741. static noinline void
  742. bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
  743. unsigned long address, struct vm_area_struct *vma)
  744. {
  745. __bad_area_nosemaphore(regs, error_code, address, vma, SEGV_MAPERR);
  746. }
  747. static void
  748. __bad_area(struct pt_regs *regs, unsigned long error_code,
  749. unsigned long address, struct vm_area_struct *vma, int si_code)
  750. {
  751. struct mm_struct *mm = current->mm;
  752. /*
  753. * Something tried to access memory that isn't in our memory map..
  754. * Fix it, but check if it's kernel or user first..
  755. */
  756. up_read(&mm->mmap_sem);
  757. __bad_area_nosemaphore(regs, error_code, address, vma, si_code);
  758. }
  759. static noinline void
  760. bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
  761. {
  762. __bad_area(regs, error_code, address, NULL, SEGV_MAPERR);
  763. }
  764. static inline bool bad_area_access_from_pkeys(unsigned long error_code,
  765. struct vm_area_struct *vma)
  766. {
  767. /* This code is always called on the current mm */
  768. bool foreign = false;
  769. if (!boot_cpu_has(X86_FEATURE_OSPKE))
  770. return false;
  771. if (error_code & PF_PK)
  772. return true;
  773. /* this checks permission keys on the VMA: */
  774. if (!arch_vma_access_permitted(vma, (error_code & PF_WRITE),
  775. (error_code & PF_INSTR), foreign))
  776. return true;
  777. return false;
  778. }
  779. static noinline void
  780. bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
  781. unsigned long address, struct vm_area_struct *vma)
  782. {
  783. /*
  784. * This OSPKE check is not strictly necessary at runtime.
  785. * But, doing it this way allows compiler optimizations
  786. * if pkeys are compiled out.
  787. */
  788. if (bad_area_access_from_pkeys(error_code, vma))
  789. __bad_area(regs, error_code, address, vma, SEGV_PKUERR);
  790. else
  791. __bad_area(regs, error_code, address, vma, SEGV_ACCERR);
  792. }
  793. static void
  794. do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address,
  795. struct vm_area_struct *vma, unsigned int fault)
  796. {
  797. struct task_struct *tsk = current;
  798. int code = BUS_ADRERR;
  799. /* Kernel mode? Handle exceptions or die: */
  800. if (!(error_code & PF_USER)) {
  801. no_context(regs, error_code, address, SIGBUS, BUS_ADRERR);
  802. return;
  803. }
  804. /* User-space => ok to do another page fault: */
  805. if (is_prefetch(regs, error_code, address))
  806. return;
  807. tsk->thread.cr2 = address;
  808. tsk->thread.error_code = error_code;
  809. tsk->thread.trap_nr = X86_TRAP_PF;
  810. #ifdef CONFIG_MEMORY_FAILURE
  811. if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
  812. printk(KERN_ERR
  813. "MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n",
  814. tsk->comm, tsk->pid, address);
  815. code = BUS_MCEERR_AR;
  816. }
  817. #endif
  818. force_sig_info_fault(SIGBUS, code, address, tsk, vma, fault);
  819. }
  820. static noinline void
  821. mm_fault_error(struct pt_regs *regs, unsigned long error_code,
  822. unsigned long address, struct vm_area_struct *vma,
  823. unsigned int fault)
  824. {
  825. if (fatal_signal_pending(current) && !(error_code & PF_USER)) {
  826. no_context(regs, error_code, address, 0, 0);
  827. return;
  828. }
  829. if (fault & VM_FAULT_OOM) {
  830. /* Kernel mode? Handle exceptions or die: */
  831. if (!(error_code & PF_USER)) {
  832. no_context(regs, error_code, address,
  833. SIGSEGV, SEGV_MAPERR);
  834. return;
  835. }
  836. /*
  837. * We ran out of memory, call the OOM killer, and return the
  838. * userspace (which will retry the fault, or kill us if we got
  839. * oom-killed):
  840. */
  841. pagefault_out_of_memory();
  842. } else {
  843. if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|
  844. VM_FAULT_HWPOISON_LARGE))
  845. do_sigbus(regs, error_code, address, vma, fault);
  846. else if (fault & VM_FAULT_SIGSEGV)
  847. bad_area_nosemaphore(regs, error_code, address, vma);
  848. else
  849. BUG();
  850. }
  851. }
  852. static int spurious_fault_check(unsigned long error_code, pte_t *pte)
  853. {
  854. if ((error_code & PF_WRITE) && !pte_write(*pte))
  855. return 0;
  856. if ((error_code & PF_INSTR) && !pte_exec(*pte))
  857. return 0;
  858. /*
  859. * Note: We do not do lazy flushing on protection key
  860. * changes, so no spurious fault will ever set PF_PK.
  861. */
  862. if ((error_code & PF_PK))
  863. return 1;
  864. return 1;
  865. }
  866. /*
  867. * Handle a spurious fault caused by a stale TLB entry.
  868. *
  869. * This allows us to lazily refresh the TLB when increasing the
  870. * permissions of a kernel page (RO -> RW or NX -> X). Doing it
  871. * eagerly is very expensive since that implies doing a full
  872. * cross-processor TLB flush, even if no stale TLB entries exist
  873. * on other processors.
  874. *
  875. * Spurious faults may only occur if the TLB contains an entry with
  876. * fewer permission than the page table entry. Non-present (P = 0)
  877. * and reserved bit (R = 1) faults are never spurious.
  878. *
  879. * There are no security implications to leaving a stale TLB when
  880. * increasing the permissions on a page.
  881. *
  882. * Returns non-zero if a spurious fault was handled, zero otherwise.
  883. *
  884. * See Intel Developer's Manual Vol 3 Section 4.10.4.3, bullet 3
  885. * (Optional Invalidation).
  886. */
  887. static noinline int
  888. spurious_fault(unsigned long error_code, unsigned long address)
  889. {
  890. pgd_t *pgd;
  891. pud_t *pud;
  892. pmd_t *pmd;
  893. pte_t *pte;
  894. int ret;
  895. /*
  896. * Only writes to RO or instruction fetches from NX may cause
  897. * spurious faults.
  898. *
  899. * These could be from user or supervisor accesses but the TLB
  900. * is only lazily flushed after a kernel mapping protection
  901. * change, so user accesses are not expected to cause spurious
  902. * faults.
  903. */
  904. if (error_code != (PF_WRITE | PF_PROT)
  905. && error_code != (PF_INSTR | PF_PROT))
  906. return 0;
  907. pgd = init_mm.pgd + pgd_index(address);
  908. if (!pgd_present(*pgd))
  909. return 0;
  910. pud = pud_offset(pgd, address);
  911. if (!pud_present(*pud))
  912. return 0;
  913. if (pud_large(*pud))
  914. return spurious_fault_check(error_code, (pte_t *) pud);
  915. pmd = pmd_offset(pud, address);
  916. if (!pmd_present(*pmd))
  917. return 0;
  918. if (pmd_large(*pmd))
  919. return spurious_fault_check(error_code, (pte_t *) pmd);
  920. pte = pte_offset_kernel(pmd, address);
  921. if (!pte_present(*pte))
  922. return 0;
  923. ret = spurious_fault_check(error_code, pte);
  924. if (!ret)
  925. return 0;
  926. /*
  927. * Make sure we have permissions in PMD.
  928. * If not, then there's a bug in the page tables:
  929. */
  930. ret = spurious_fault_check(error_code, (pte_t *) pmd);
  931. WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
  932. return ret;
  933. }
  934. NOKPROBE_SYMBOL(spurious_fault);
  935. int show_unhandled_signals = 1;
  936. static inline int
  937. access_error(unsigned long error_code, struct vm_area_struct *vma)
  938. {
  939. /* This is only called for the current mm, so: */
  940. bool foreign = false;
  941. /*
  942. * Make sure to check the VMA so that we do not perform
  943. * faults just to hit a PF_PK as soon as we fill in a
  944. * page.
  945. */
  946. if (!arch_vma_access_permitted(vma, (error_code & PF_WRITE),
  947. (error_code & PF_INSTR), foreign))
  948. return 1;
  949. if (error_code & PF_WRITE) {
  950. /* write, present and write, not present: */
  951. if (unlikely(!(vma->vm_flags & VM_WRITE)))
  952. return 1;
  953. return 0;
  954. }
  955. /* read, present: */
  956. if (unlikely(error_code & PF_PROT))
  957. return 1;
  958. /* read, not present: */
  959. if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
  960. return 1;
  961. return 0;
  962. }
  963. static int fault_in_kernel_space(unsigned long address)
  964. {
  965. return address >= TASK_SIZE_MAX;
  966. }
  967. static inline bool smap_violation(int error_code, struct pt_regs *regs)
  968. {
  969. if (!IS_ENABLED(CONFIG_X86_SMAP))
  970. return false;
  971. if (!static_cpu_has(X86_FEATURE_SMAP))
  972. return false;
  973. if (error_code & PF_USER)
  974. return false;
  975. if (!user_mode(regs) && (regs->flags & X86_EFLAGS_AC))
  976. return false;
  977. return true;
  978. }
  979. /*
  980. * This routine handles page faults. It determines the address,
  981. * and the problem, and then passes it off to one of the appropriate
  982. * routines.
  983. *
  984. * This function must have noinline because both callers
  985. * {,trace_}do_page_fault() have notrace on. Having this an actual function
  986. * guarantees there's a function trace entry.
  987. */
  988. static noinline void
  989. __do_page_fault(struct pt_regs *regs, unsigned long error_code,
  990. unsigned long address)
  991. {
  992. struct vm_area_struct *vma;
  993. struct task_struct *tsk;
  994. struct mm_struct *mm;
  995. int fault, major = 0;
  996. unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  997. tsk = current;
  998. mm = tsk->mm;
  999. /*
  1000. * Detect and handle instructions that would cause a page fault for
  1001. * both a tracked kernel page and a userspace page.
  1002. */
  1003. if (kmemcheck_active(regs))
  1004. kmemcheck_hide(regs);
  1005. prefetchw(&mm->mmap_sem);
  1006. if (unlikely(kmmio_fault(regs, address)))
  1007. return;
  1008. /*
  1009. * We fault-in kernel-space virtual memory on-demand. The
  1010. * 'reference' page table is init_mm.pgd.
  1011. *
  1012. * NOTE! We MUST NOT take any locks for this case. We may
  1013. * be in an interrupt or a critical region, and should
  1014. * only copy the information from the master page table,
  1015. * nothing more.
  1016. *
  1017. * This verifies that the fault happens in kernel space
  1018. * (error_code & 4) == 0, and that the fault was not a
  1019. * protection error (error_code & 9) == 0.
  1020. */
  1021. if (unlikely(fault_in_kernel_space(address))) {
  1022. if (!(error_code & (PF_RSVD | PF_USER | PF_PROT))) {
  1023. if (vmalloc_fault(address) >= 0)
  1024. return;
  1025. if (kmemcheck_fault(regs, address, error_code))
  1026. return;
  1027. }
  1028. /* Can handle a stale RO->RW TLB: */
  1029. if (spurious_fault(error_code, address))
  1030. return;
  1031. /* kprobes don't want to hook the spurious faults: */
  1032. if (kprobes_fault(regs))
  1033. return;
  1034. /*
  1035. * Don't take the mm semaphore here. If we fixup a prefetch
  1036. * fault we could otherwise deadlock:
  1037. */
  1038. bad_area_nosemaphore(regs, error_code, address, NULL);
  1039. return;
  1040. }
  1041. /* kprobes don't want to hook the spurious faults: */
  1042. if (unlikely(kprobes_fault(regs)))
  1043. return;
  1044. if (unlikely(error_code & PF_RSVD))
  1045. pgtable_bad(regs, error_code, address);
  1046. if (unlikely(smap_violation(error_code, regs))) {
  1047. bad_area_nosemaphore(regs, error_code, address, NULL);
  1048. return;
  1049. }
  1050. /*
  1051. * If we're in an interrupt, have no user context or are running
  1052. * in a region with pagefaults disabled then we must not take the fault
  1053. */
  1054. if (unlikely(faulthandler_disabled() || !mm)) {
  1055. bad_area_nosemaphore(regs, error_code, address, NULL);
  1056. return;
  1057. }
  1058. /*
  1059. * It's safe to allow irq's after cr2 has been saved and the
  1060. * vmalloc fault has been handled.
  1061. *
  1062. * User-mode registers count as a user access even for any
  1063. * potential system fault or CPU buglet:
  1064. */
  1065. if (user_mode(regs)) {
  1066. local_irq_enable();
  1067. error_code |= PF_USER;
  1068. flags |= FAULT_FLAG_USER;
  1069. } else {
  1070. if (regs->flags & X86_EFLAGS_IF)
  1071. local_irq_enable();
  1072. }
  1073. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  1074. if (error_code & PF_WRITE)
  1075. flags |= FAULT_FLAG_WRITE;
  1076. if (error_code & PF_INSTR)
  1077. flags |= FAULT_FLAG_INSTRUCTION;
  1078. /*
  1079. * When running in the kernel we expect faults to occur only to
  1080. * addresses in user space. All other faults represent errors in
  1081. * the kernel and should generate an OOPS. Unfortunately, in the
  1082. * case of an erroneous fault occurring in a code path which already
  1083. * holds mmap_sem we will deadlock attempting to validate the fault
  1084. * against the address space. Luckily the kernel only validly
  1085. * references user space from well defined areas of code, which are
  1086. * listed in the exceptions table.
  1087. *
  1088. * As the vast majority of faults will be valid we will only perform
  1089. * the source reference check when there is a possibility of a
  1090. * deadlock. Attempt to lock the address space, if we cannot we then
  1091. * validate the source. If this is invalid we can skip the address
  1092. * space check, thus avoiding the deadlock:
  1093. */
  1094. if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
  1095. if ((error_code & PF_USER) == 0 &&
  1096. !search_exception_tables(regs->ip)) {
  1097. bad_area_nosemaphore(regs, error_code, address, NULL);
  1098. return;
  1099. }
  1100. retry:
  1101. down_read(&mm->mmap_sem);
  1102. } else {
  1103. /*
  1104. * The above down_read_trylock() might have succeeded in
  1105. * which case we'll have missed the might_sleep() from
  1106. * down_read():
  1107. */
  1108. might_sleep();
  1109. }
  1110. vma = find_vma(mm, address);
  1111. if (unlikely(!vma)) {
  1112. bad_area(regs, error_code, address);
  1113. return;
  1114. }
  1115. if (likely(vma->vm_start <= address))
  1116. goto good_area;
  1117. if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
  1118. bad_area(regs, error_code, address);
  1119. return;
  1120. }
  1121. if (error_code & PF_USER) {
  1122. /*
  1123. * Accessing the stack below %sp is always a bug.
  1124. * The large cushion allows instructions like enter
  1125. * and pusha to work. ("enter $65535, $31" pushes
  1126. * 32 pointers and then decrements %sp by 65535.)
  1127. */
  1128. if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
  1129. bad_area(regs, error_code, address);
  1130. return;
  1131. }
  1132. }
  1133. if (unlikely(expand_stack(vma, address))) {
  1134. bad_area(regs, error_code, address);
  1135. return;
  1136. }
  1137. /*
  1138. * Ok, we have a good vm_area for this memory access, so
  1139. * we can handle it..
  1140. */
  1141. good_area:
  1142. if (unlikely(access_error(error_code, vma))) {
  1143. bad_area_access_error(regs, error_code, address, vma);
  1144. return;
  1145. }
  1146. /*
  1147. * If for any reason at all we couldn't handle the fault,
  1148. * make sure we exit gracefully rather than endlessly redo
  1149. * the fault. Since we never set FAULT_FLAG_RETRY_NOWAIT, if
  1150. * we get VM_FAULT_RETRY back, the mmap_sem has been unlocked.
  1151. */
  1152. fault = handle_mm_fault(vma, address, flags);
  1153. major |= fault & VM_FAULT_MAJOR;
  1154. /*
  1155. * If we need to retry the mmap_sem has already been released,
  1156. * and if there is a fatal signal pending there is no guarantee
  1157. * that we made any progress. Handle this case first.
  1158. */
  1159. if (unlikely(fault & VM_FAULT_RETRY)) {
  1160. /* Retry at most once */
  1161. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  1162. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  1163. flags |= FAULT_FLAG_TRIED;
  1164. if (!fatal_signal_pending(tsk))
  1165. goto retry;
  1166. }
  1167. /* User mode? Just return to handle the fatal exception */
  1168. if (flags & FAULT_FLAG_USER)
  1169. return;
  1170. /* Not returning to user mode? Handle exceptions or die: */
  1171. no_context(regs, error_code, address, SIGBUS, BUS_ADRERR);
  1172. return;
  1173. }
  1174. up_read(&mm->mmap_sem);
  1175. if (unlikely(fault & VM_FAULT_ERROR)) {
  1176. mm_fault_error(regs, error_code, address, vma, fault);
  1177. return;
  1178. }
  1179. /*
  1180. * Major/minor page fault accounting. If any of the events
  1181. * returned VM_FAULT_MAJOR, we account it as a major fault.
  1182. */
  1183. if (major) {
  1184. tsk->maj_flt++;
  1185. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address);
  1186. } else {
  1187. tsk->min_flt++;
  1188. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address);
  1189. }
  1190. check_v8086_mode(regs, address, tsk);
  1191. }
  1192. NOKPROBE_SYMBOL(__do_page_fault);
  1193. dotraplinkage void notrace
  1194. do_page_fault(struct pt_regs *regs, unsigned long error_code)
  1195. {
  1196. unsigned long address = read_cr2(); /* Get the faulting address */
  1197. enum ctx_state prev_state;
  1198. /*
  1199. * We must have this function tagged with __kprobes, notrace and call
  1200. * read_cr2() before calling anything else. To avoid calling any kind
  1201. * of tracing machinery before we've observed the CR2 value.
  1202. *
  1203. * exception_{enter,exit}() contain all sorts of tracepoints.
  1204. */
  1205. prev_state = exception_enter();
  1206. __do_page_fault(regs, error_code, address);
  1207. exception_exit(prev_state);
  1208. }
  1209. NOKPROBE_SYMBOL(do_page_fault);
  1210. #ifdef CONFIG_TRACING
  1211. static nokprobe_inline void
  1212. trace_page_fault_entries(unsigned long address, struct pt_regs *regs,
  1213. unsigned long error_code)
  1214. {
  1215. if (user_mode(regs))
  1216. trace_page_fault_user(address, regs, error_code);
  1217. else
  1218. trace_page_fault_kernel(address, regs, error_code);
  1219. }
  1220. dotraplinkage void notrace
  1221. trace_do_page_fault(struct pt_regs *regs, unsigned long error_code)
  1222. {
  1223. /*
  1224. * The exception_enter and tracepoint processing could
  1225. * trigger another page faults (user space callchain
  1226. * reading) and destroy the original cr2 value, so read
  1227. * the faulting address now.
  1228. */
  1229. unsigned long address = read_cr2();
  1230. enum ctx_state prev_state;
  1231. prev_state = exception_enter();
  1232. trace_page_fault_entries(address, regs, error_code);
  1233. __do_page_fault(regs, error_code, address);
  1234. exception_exit(prev_state);
  1235. }
  1236. NOKPROBE_SYMBOL(trace_do_page_fault);
  1237. #endif /* CONFIG_TRACING */