fault.c 37 KB

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