fault.c 38 KB

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