fault.c 37 KB

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