fault.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566
  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/memblock.h> /* max_low_pfn */
  12. #include <linux/kfence.h> /* kfence_handle_page_fault */
  13. #include <linux/kprobes.h> /* NOKPROBE_SYMBOL, ... */
  14. #include <linux/mmiotrace.h> /* kmmio_handler, ... */
  15. #include <linux/perf_event.h> /* perf_sw_event */
  16. #include <linux/hugetlb.h> /* hstate_index_to_shift */
  17. #include <linux/prefetch.h> /* prefetchw */
  18. #include <linux/context_tracking.h> /* exception_enter(), ... */
  19. #include <linux/uaccess.h> /* faulthandler_disabled() */
  20. #include <linux/efi.h> /* efi_crash_gracefully_on_page_fault()*/
  21. #include <linux/mm_types.h>
  22. #include <linux/mm.h> /* find_and_lock_vma() */
  23. #include <asm/cpufeature.h> /* boot_cpu_has, ... */
  24. #include <asm/traps.h> /* dotraplinkage, ... */
  25. #include <asm/fixmap.h> /* VSYSCALL_ADDR */
  26. #include <asm/vsyscall.h> /* emulate_vsyscall */
  27. #include <asm/vm86.h> /* struct vm86 */
  28. #include <asm/mmu_context.h> /* vma_pkey() */
  29. #include <asm/efi.h> /* efi_crash_gracefully_on_page_fault()*/
  30. #include <asm/desc.h> /* store_idt(), ... */
  31. #include <asm/cpu_entry_area.h> /* exception stack */
  32. #include <asm/pgtable_areas.h> /* VMALLOC_START, ... */
  33. #include <asm/kvm_para.h> /* kvm_handle_async_pf */
  34. #include <asm/vdso.h> /* fixup_vdso_exception() */
  35. #include <asm/irq_stack.h>
  36. #define CREATE_TRACE_POINTS
  37. #include <asm/trace/exceptions.h>
  38. /*
  39. * Returns 0 if mmiotrace is disabled, or if the fault is not
  40. * handled by mmiotrace:
  41. */
  42. static nokprobe_inline int
  43. kmmio_fault(struct pt_regs *regs, unsigned long addr)
  44. {
  45. if (unlikely(is_kmmio_active()))
  46. if (kmmio_handler(regs, addr) == 1)
  47. return -1;
  48. return 0;
  49. }
  50. /*
  51. * Prefetch quirks:
  52. *
  53. * 32-bit mode:
  54. *
  55. * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
  56. * Check that here and ignore it. This is AMD erratum #91.
  57. *
  58. * 64-bit mode:
  59. *
  60. * Sometimes the CPU reports invalid exceptions on prefetch.
  61. * Check that here and ignore it.
  62. *
  63. * Opcode checker based on code by Richard Brunner.
  64. */
  65. static inline int
  66. check_prefetch_opcode(struct pt_regs *regs, unsigned char *instr,
  67. unsigned char opcode, int *prefetch)
  68. {
  69. unsigned char instr_hi = opcode & 0xf0;
  70. unsigned char instr_lo = opcode & 0x0f;
  71. switch (instr_hi) {
  72. case 0x20:
  73. case 0x30:
  74. /*
  75. * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
  76. * In X86_64 long mode, the CPU will signal invalid
  77. * opcode if some of these prefixes are present so
  78. * X86_64 will never get here anyway
  79. */
  80. return ((instr_lo & 7) == 0x6);
  81. #ifdef CONFIG_X86_64
  82. case 0x40:
  83. /*
  84. * In 64-bit mode 0x40..0x4F are valid REX prefixes
  85. */
  86. return (!user_mode(regs) || user_64bit_mode(regs));
  87. #endif
  88. case 0x60:
  89. /* 0x64 thru 0x67 are valid prefixes in all modes. */
  90. return (instr_lo & 0xC) == 0x4;
  91. case 0xF0:
  92. /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
  93. return !instr_lo || (instr_lo>>1) == 1;
  94. case 0x00:
  95. /* Prefetch instruction is 0x0F0D or 0x0F18 */
  96. if (get_kernel_nofault(opcode, instr))
  97. return 0;
  98. *prefetch = (instr_lo == 0xF) &&
  99. (opcode == 0x0D || opcode == 0x18);
  100. return 0;
  101. default:
  102. return 0;
  103. }
  104. }
  105. static bool is_amd_k8_pre_npt(void)
  106. {
  107. struct cpuinfo_x86 *c = &boot_cpu_data;
  108. return unlikely(IS_ENABLED(CONFIG_CPU_SUP_AMD) &&
  109. c->x86_vendor == X86_VENDOR_AMD &&
  110. c->x86 == 0xf && c->x86_model < 0x40);
  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. /* Erratum #91 affects AMD K8, pre-NPT CPUs */
  119. if (!is_amd_k8_pre_npt())
  120. return 0;
  121. /*
  122. * If it was a exec (instruction fetch) fault on NX page, then
  123. * do not ignore the fault:
  124. */
  125. if (error_code & X86_PF_INSTR)
  126. return 0;
  127. instr = (void *)convert_ip_to_linear(current, regs);
  128. max_instr = instr + 15;
  129. /*
  130. * This code has historically always bailed out if IP points to a
  131. * not-present page (e.g. due to a race). No one has ever
  132. * complained about this.
  133. */
  134. pagefault_disable();
  135. while (instr < max_instr) {
  136. unsigned char opcode;
  137. if (user_mode(regs)) {
  138. if (get_user(opcode, (unsigned char __user *) instr))
  139. break;
  140. } else {
  141. if (get_kernel_nofault(opcode, instr))
  142. break;
  143. }
  144. instr++;
  145. if (!check_prefetch_opcode(regs, instr, opcode, &prefetch))
  146. break;
  147. }
  148. pagefault_enable();
  149. return prefetch;
  150. }
  151. DEFINE_SPINLOCK(pgd_lock);
  152. LIST_HEAD(pgd_list);
  153. #ifdef CONFIG_X86_32
  154. static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
  155. {
  156. unsigned index = pgd_index(address);
  157. pgd_t *pgd_k;
  158. p4d_t *p4d, *p4d_k;
  159. pud_t *pud, *pud_k;
  160. pmd_t *pmd, *pmd_k;
  161. pgd += index;
  162. pgd_k = init_mm.pgd + index;
  163. if (!pgd_present(*pgd_k))
  164. return NULL;
  165. /*
  166. * set_pgd(pgd, *pgd_k); here would be useless on PAE
  167. * and redundant with the set_pmd() on non-PAE. As would
  168. * set_p4d/set_pud.
  169. */
  170. p4d = p4d_offset(pgd, address);
  171. p4d_k = p4d_offset(pgd_k, address);
  172. if (!p4d_present(*p4d_k))
  173. return NULL;
  174. pud = pud_offset(p4d, address);
  175. pud_k = pud_offset(p4d_k, address);
  176. if (!pud_present(*pud_k))
  177. return NULL;
  178. pmd = pmd_offset(pud, address);
  179. pmd_k = pmd_offset(pud_k, address);
  180. if (pmd_present(*pmd) != pmd_present(*pmd_k))
  181. set_pmd(pmd, *pmd_k);
  182. if (!pmd_present(*pmd_k))
  183. return NULL;
  184. else
  185. BUG_ON(pmd_pfn(*pmd) != pmd_pfn(*pmd_k));
  186. return pmd_k;
  187. }
  188. /*
  189. * Handle a fault on the vmalloc or module mapping area
  190. *
  191. * This is needed because there is a race condition between the time
  192. * when the vmalloc mapping code updates the PMD to the point in time
  193. * where it synchronizes this update with the other page-tables in the
  194. * system.
  195. *
  196. * In this race window another thread/CPU can map an area on the same
  197. * PMD, finds it already present and does not synchronize it with the
  198. * rest of the system yet. As a result v[mz]alloc might return areas
  199. * which are not mapped in every page-table in the system, causing an
  200. * unhandled page-fault when they are accessed.
  201. */
  202. static noinline int vmalloc_fault(unsigned long address)
  203. {
  204. unsigned long pgd_paddr;
  205. pmd_t *pmd_k;
  206. pte_t *pte_k;
  207. /* Make sure we are in vmalloc area: */
  208. if (!(address >= VMALLOC_START && address < VMALLOC_END))
  209. return -1;
  210. /*
  211. * Synchronize this task's top level page-table
  212. * with the 'reference' page table.
  213. *
  214. * Do _not_ use "current" here. We might be inside
  215. * an interrupt in the middle of a task switch..
  216. */
  217. pgd_paddr = read_cr3_pa();
  218. pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
  219. if (!pmd_k)
  220. return -1;
  221. if (pmd_large(*pmd_k))
  222. return 0;
  223. pte_k = pte_offset_kernel(pmd_k, address);
  224. if (!pte_present(*pte_k))
  225. return -1;
  226. return 0;
  227. }
  228. NOKPROBE_SYMBOL(vmalloc_fault);
  229. static void __arch_sync_kernel_mappings(unsigned long start, unsigned long end)
  230. {
  231. unsigned long addr;
  232. for (addr = start & PMD_MASK;
  233. addr >= TASK_SIZE_MAX && addr < VMALLOC_END;
  234. addr += PMD_SIZE) {
  235. struct page *page;
  236. spin_lock(&pgd_lock);
  237. list_for_each_entry(page, &pgd_list, lru) {
  238. spinlock_t *pgt_lock;
  239. /* the pgt_lock only for Xen */
  240. pgt_lock = &pgd_page_get_mm(page)->page_table_lock;
  241. spin_lock(pgt_lock);
  242. vmalloc_sync_one(page_address(page), addr);
  243. spin_unlock(pgt_lock);
  244. }
  245. spin_unlock(&pgd_lock);
  246. }
  247. }
  248. void arch_sync_kernel_mappings(unsigned long start, unsigned long end)
  249. {
  250. __arch_sync_kernel_mappings(start, end);
  251. #ifdef CONFIG_KMSAN
  252. /*
  253. * KMSAN maintains two additional metadata page mappings for the
  254. * [VMALLOC_START, VMALLOC_END) range. These mappings start at
  255. * KMSAN_VMALLOC_SHADOW_START and KMSAN_VMALLOC_ORIGIN_START and
  256. * have to be synced together with the vmalloc memory mapping.
  257. */
  258. if (start >= VMALLOC_START && end < VMALLOC_END) {
  259. __arch_sync_kernel_mappings(
  260. start - VMALLOC_START + KMSAN_VMALLOC_SHADOW_START,
  261. end - VMALLOC_START + KMSAN_VMALLOC_SHADOW_START);
  262. __arch_sync_kernel_mappings(
  263. start - VMALLOC_START + KMSAN_VMALLOC_ORIGIN_START,
  264. end - VMALLOC_START + KMSAN_VMALLOC_ORIGIN_START);
  265. }
  266. #endif
  267. }
  268. static bool low_pfn(unsigned long pfn)
  269. {
  270. return pfn < max_low_pfn;
  271. }
  272. static void dump_pagetable(unsigned long address)
  273. {
  274. pgd_t *base = __va(read_cr3_pa());
  275. pgd_t *pgd = &base[pgd_index(address)];
  276. p4d_t *p4d;
  277. pud_t *pud;
  278. pmd_t *pmd;
  279. pte_t *pte;
  280. #ifdef CONFIG_X86_PAE
  281. pr_info("*pdpt = %016Lx ", pgd_val(*pgd));
  282. if (!low_pfn(pgd_val(*pgd) >> PAGE_SHIFT) || !pgd_present(*pgd))
  283. goto out;
  284. #define pr_pde pr_cont
  285. #else
  286. #define pr_pde pr_info
  287. #endif
  288. p4d = p4d_offset(pgd, address);
  289. pud = pud_offset(p4d, address);
  290. pmd = pmd_offset(pud, address);
  291. pr_pde("*pde = %0*Lx ", sizeof(*pmd) * 2, (u64)pmd_val(*pmd));
  292. #undef pr_pde
  293. /*
  294. * We must not directly access the pte in the highpte
  295. * case if the page table is located in highmem.
  296. * And let's rather not kmap-atomic the pte, just in case
  297. * it's allocated already:
  298. */
  299. if (!low_pfn(pmd_pfn(*pmd)) || !pmd_present(*pmd) || pmd_large(*pmd))
  300. goto out;
  301. pte = pte_offset_kernel(pmd, address);
  302. pr_cont("*pte = %0*Lx ", sizeof(*pte) * 2, (u64)pte_val(*pte));
  303. out:
  304. pr_cont("\n");
  305. }
  306. #else /* CONFIG_X86_64: */
  307. #ifdef CONFIG_CPU_SUP_AMD
  308. static const char errata93_warning[] =
  309. KERN_ERR
  310. "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
  311. "******* Working around it, but it may cause SEGVs or burn power.\n"
  312. "******* Please consider a BIOS update.\n"
  313. "******* Disabling USB legacy in the BIOS may also help.\n";
  314. #endif
  315. static int bad_address(void *p)
  316. {
  317. unsigned long dummy;
  318. return get_kernel_nofault(dummy, (unsigned long *)p);
  319. }
  320. static void dump_pagetable(unsigned long address)
  321. {
  322. pgd_t *base = __va(read_cr3_pa());
  323. pgd_t *pgd = base + pgd_index(address);
  324. p4d_t *p4d;
  325. pud_t *pud;
  326. pmd_t *pmd;
  327. pte_t *pte;
  328. if (bad_address(pgd))
  329. goto bad;
  330. pr_info("PGD %lx ", pgd_val(*pgd));
  331. if (!pgd_present(*pgd))
  332. goto out;
  333. p4d = p4d_offset(pgd, address);
  334. if (bad_address(p4d))
  335. goto bad;
  336. pr_cont("P4D %lx ", p4d_val(*p4d));
  337. if (!p4d_present(*p4d) || p4d_large(*p4d))
  338. goto out;
  339. pud = pud_offset(p4d, address);
  340. if (bad_address(pud))
  341. goto bad;
  342. pr_cont("PUD %lx ", pud_val(*pud));
  343. if (!pud_present(*pud) || pud_large(*pud))
  344. goto out;
  345. pmd = pmd_offset(pud, address);
  346. if (bad_address(pmd))
  347. goto bad;
  348. pr_cont("PMD %lx ", pmd_val(*pmd));
  349. if (!pmd_present(*pmd) || pmd_large(*pmd))
  350. goto out;
  351. pte = pte_offset_kernel(pmd, address);
  352. if (bad_address(pte))
  353. goto bad;
  354. pr_cont("PTE %lx", pte_val(*pte));
  355. out:
  356. pr_cont("\n");
  357. return;
  358. bad:
  359. pr_info("BAD\n");
  360. }
  361. #endif /* CONFIG_X86_64 */
  362. /*
  363. * Workaround for K8 erratum #93 & buggy BIOS.
  364. *
  365. * BIOS SMM functions are required to use a specific workaround
  366. * to avoid corruption of the 64bit RIP register on C stepping K8.
  367. *
  368. * A lot of BIOS that didn't get tested properly miss this.
  369. *
  370. * The OS sees this as a page fault with the upper 32bits of RIP cleared.
  371. * Try to work around it here.
  372. *
  373. * Note we only handle faults in kernel here.
  374. * Does nothing on 32-bit.
  375. */
  376. static int is_errata93(struct pt_regs *regs, unsigned long address)
  377. {
  378. #if defined(CONFIG_X86_64) && defined(CONFIG_CPU_SUP_AMD)
  379. if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD
  380. || boot_cpu_data.x86 != 0xf)
  381. return 0;
  382. if (user_mode(regs))
  383. return 0;
  384. if (address != regs->ip)
  385. return 0;
  386. if ((address >> 32) != 0)
  387. return 0;
  388. address |= 0xffffffffUL << 32;
  389. if ((address >= (u64)_stext && address <= (u64)_etext) ||
  390. (address >= MODULES_VADDR && address <= MODULES_END)) {
  391. printk_once(errata93_warning);
  392. regs->ip = address;
  393. return 1;
  394. }
  395. #endif
  396. return 0;
  397. }
  398. /*
  399. * Work around K8 erratum #100 K8 in compat mode occasionally jumps
  400. * to illegal addresses >4GB.
  401. *
  402. * We catch this in the page fault handler because these addresses
  403. * are not reachable. Just detect this case and return. Any code
  404. * segment in LDT is compatibility mode.
  405. */
  406. static int is_errata100(struct pt_regs *regs, unsigned long address)
  407. {
  408. #ifdef CONFIG_X86_64
  409. if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32))
  410. return 1;
  411. #endif
  412. return 0;
  413. }
  414. /* Pentium F0 0F C7 C8 bug workaround: */
  415. static int is_f00f_bug(struct pt_regs *regs, unsigned long error_code,
  416. unsigned long address)
  417. {
  418. #ifdef CONFIG_X86_F00F_BUG
  419. if (boot_cpu_has_bug(X86_BUG_F00F) && !(error_code & X86_PF_USER) &&
  420. idt_is_f00f_address(address)) {
  421. handle_invalid_op(regs);
  422. return 1;
  423. }
  424. #endif
  425. return 0;
  426. }
  427. static void show_ldttss(const struct desc_ptr *gdt, const char *name, u16 index)
  428. {
  429. u32 offset = (index >> 3) * sizeof(struct desc_struct);
  430. unsigned long addr;
  431. struct ldttss_desc desc;
  432. if (index == 0) {
  433. pr_alert("%s: NULL\n", name);
  434. return;
  435. }
  436. if (offset + sizeof(struct ldttss_desc) >= gdt->size) {
  437. pr_alert("%s: 0x%hx -- out of bounds\n", name, index);
  438. return;
  439. }
  440. if (copy_from_kernel_nofault(&desc, (void *)(gdt->address + offset),
  441. sizeof(struct ldttss_desc))) {
  442. pr_alert("%s: 0x%hx -- GDT entry is not readable\n",
  443. name, index);
  444. return;
  445. }
  446. addr = desc.base0 | (desc.base1 << 16) | ((unsigned long)desc.base2 << 24);
  447. #ifdef CONFIG_X86_64
  448. addr |= ((u64)desc.base3 << 32);
  449. #endif
  450. pr_alert("%s: 0x%hx -- base=0x%lx limit=0x%x\n",
  451. name, index, addr, (desc.limit0 | (desc.limit1 << 16)));
  452. }
  453. static void
  454. show_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long address)
  455. {
  456. if (!oops_may_print())
  457. return;
  458. if (error_code & X86_PF_INSTR) {
  459. unsigned int level;
  460. pgd_t *pgd;
  461. pte_t *pte;
  462. pgd = __va(read_cr3_pa());
  463. pgd += pgd_index(address);
  464. pte = lookup_address_in_pgd(pgd, address, &level);
  465. if (pte && pte_present(*pte) && !pte_exec(*pte))
  466. pr_crit("kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n",
  467. from_kuid(&init_user_ns, current_uid()));
  468. if (pte && pte_present(*pte) && pte_exec(*pte) &&
  469. (pgd_flags(*pgd) & _PAGE_USER) &&
  470. (__read_cr4() & X86_CR4_SMEP))
  471. pr_crit("unable to execute userspace code (SMEP?) (uid: %d)\n",
  472. from_kuid(&init_user_ns, current_uid()));
  473. }
  474. if (address < PAGE_SIZE && !user_mode(regs))
  475. pr_alert("BUG: kernel NULL pointer dereference, address: %px\n",
  476. (void *)address);
  477. else
  478. pr_alert("BUG: unable to handle page fault for address: %px\n",
  479. (void *)address);
  480. pr_alert("#PF: %s %s in %s mode\n",
  481. (error_code & X86_PF_USER) ? "user" : "supervisor",
  482. (error_code & X86_PF_INSTR) ? "instruction fetch" :
  483. (error_code & X86_PF_WRITE) ? "write access" :
  484. "read access",
  485. user_mode(regs) ? "user" : "kernel");
  486. pr_alert("#PF: error_code(0x%04lx) - %s\n", error_code,
  487. !(error_code & X86_PF_PROT) ? "not-present page" :
  488. (error_code & X86_PF_RSVD) ? "reserved bit violation" :
  489. (error_code & X86_PF_PK) ? "protection keys violation" :
  490. "permissions violation");
  491. if (!(error_code & X86_PF_USER) && user_mode(regs)) {
  492. struct desc_ptr idt, gdt;
  493. u16 ldtr, tr;
  494. /*
  495. * This can happen for quite a few reasons. The more obvious
  496. * ones are faults accessing the GDT, or LDT. Perhaps
  497. * surprisingly, if the CPU tries to deliver a benign or
  498. * contributory exception from user code and gets a page fault
  499. * during delivery, the page fault can be delivered as though
  500. * it originated directly from user code. This could happen
  501. * due to wrong permissions on the IDT, GDT, LDT, TSS, or
  502. * kernel or IST stack.
  503. */
  504. store_idt(&idt);
  505. /* Usable even on Xen PV -- it's just slow. */
  506. native_store_gdt(&gdt);
  507. pr_alert("IDT: 0x%lx (limit=0x%hx) GDT: 0x%lx (limit=0x%hx)\n",
  508. idt.address, idt.size, gdt.address, gdt.size);
  509. store_ldt(ldtr);
  510. show_ldttss(&gdt, "LDTR", ldtr);
  511. store_tr(tr);
  512. show_ldttss(&gdt, "TR", tr);
  513. }
  514. dump_pagetable(address);
  515. }
  516. static noinline void
  517. pgtable_bad(struct pt_regs *regs, unsigned long error_code,
  518. unsigned long address)
  519. {
  520. struct task_struct *tsk;
  521. unsigned long flags;
  522. int sig;
  523. flags = oops_begin();
  524. tsk = current;
  525. sig = SIGKILL;
  526. printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
  527. tsk->comm, address);
  528. dump_pagetable(address);
  529. if (__die("Bad pagetable", regs, error_code))
  530. sig = 0;
  531. oops_end(flags, regs, sig);
  532. }
  533. static void sanitize_error_code(unsigned long address,
  534. unsigned long *error_code)
  535. {
  536. /*
  537. * To avoid leaking information about the kernel page
  538. * table layout, pretend that user-mode accesses to
  539. * kernel addresses are always protection faults.
  540. *
  541. * NB: This means that failed vsyscalls with vsyscall=none
  542. * will have the PROT bit. This doesn't leak any
  543. * information and does not appear to cause any problems.
  544. */
  545. if (address >= TASK_SIZE_MAX)
  546. *error_code |= X86_PF_PROT;
  547. }
  548. static void set_signal_archinfo(unsigned long address,
  549. unsigned long error_code)
  550. {
  551. struct task_struct *tsk = current;
  552. tsk->thread.trap_nr = X86_TRAP_PF;
  553. tsk->thread.error_code = error_code | X86_PF_USER;
  554. tsk->thread.cr2 = address;
  555. }
  556. static noinline void
  557. page_fault_oops(struct pt_regs *regs, unsigned long error_code,
  558. unsigned long address)
  559. {
  560. #ifdef CONFIG_VMAP_STACK
  561. struct stack_info info;
  562. #endif
  563. unsigned long flags;
  564. int sig;
  565. if (user_mode(regs)) {
  566. /*
  567. * Implicit kernel access from user mode? Skip the stack
  568. * overflow and EFI special cases.
  569. */
  570. goto oops;
  571. }
  572. #ifdef CONFIG_VMAP_STACK
  573. /*
  574. * Stack overflow? During boot, we can fault near the initial
  575. * stack in the direct map, but that's not an overflow -- check
  576. * that we're in vmalloc space to avoid this.
  577. */
  578. if (is_vmalloc_addr((void *)address) &&
  579. get_stack_guard_info((void *)address, &info)) {
  580. /*
  581. * We're likely to be running with very little stack space
  582. * left. It's plausible that we'd hit this condition but
  583. * double-fault even before we get this far, in which case
  584. * we're fine: the double-fault handler will deal with it.
  585. *
  586. * We don't want to make it all the way into the oops code
  587. * and then double-fault, though, because we're likely to
  588. * break the console driver and lose most of the stack dump.
  589. */
  590. call_on_stack(__this_cpu_ist_top_va(DF) - sizeof(void*),
  591. handle_stack_overflow,
  592. ASM_CALL_ARG3,
  593. , [arg1] "r" (regs), [arg2] "r" (address), [arg3] "r" (&info));
  594. unreachable();
  595. }
  596. #endif
  597. /*
  598. * Buggy firmware could access regions which might page fault. If
  599. * this happens, EFI has a special OOPS path that will try to
  600. * avoid hanging the system.
  601. */
  602. if (IS_ENABLED(CONFIG_EFI))
  603. efi_crash_gracefully_on_page_fault(address);
  604. /* Only not-present faults should be handled by KFENCE. */
  605. if (!(error_code & X86_PF_PROT) &&
  606. kfence_handle_page_fault(address, error_code & X86_PF_WRITE, regs))
  607. return;
  608. oops:
  609. /*
  610. * Oops. The kernel tried to access some bad page. We'll have to
  611. * terminate things with extreme prejudice:
  612. */
  613. flags = oops_begin();
  614. show_fault_oops(regs, error_code, address);
  615. if (task_stack_end_corrupted(current))
  616. printk(KERN_EMERG "Thread overran stack, or stack corrupted\n");
  617. sig = SIGKILL;
  618. if (__die("Oops", regs, error_code))
  619. sig = 0;
  620. /* Executive summary in case the body of the oops scrolled away */
  621. printk(KERN_DEFAULT "CR2: %016lx\n", address);
  622. oops_end(flags, regs, sig);
  623. }
  624. static noinline void
  625. kernelmode_fixup_or_oops(struct pt_regs *regs, unsigned long error_code,
  626. unsigned long address, int signal, int si_code,
  627. u32 pkey)
  628. {
  629. WARN_ON_ONCE(user_mode(regs));
  630. /* Are we prepared to handle this kernel fault? */
  631. if (fixup_exception(regs, X86_TRAP_PF, error_code, address)) {
  632. /*
  633. * Any interrupt that takes a fault gets the fixup. This makes
  634. * the below recursive fault logic only apply to a faults from
  635. * task context.
  636. */
  637. if (in_interrupt())
  638. return;
  639. /*
  640. * Per the above we're !in_interrupt(), aka. task context.
  641. *
  642. * In this case we need to make sure we're not recursively
  643. * faulting through the emulate_vsyscall() logic.
  644. */
  645. if (current->thread.sig_on_uaccess_err && signal) {
  646. sanitize_error_code(address, &error_code);
  647. set_signal_archinfo(address, error_code);
  648. if (si_code == SEGV_PKUERR) {
  649. force_sig_pkuerr((void __user *)address, pkey);
  650. } else {
  651. /* XXX: hwpoison faults will set the wrong code. */
  652. force_sig_fault(signal, si_code, (void __user *)address);
  653. }
  654. }
  655. /*
  656. * Barring that, we can do the fixup and be happy.
  657. */
  658. return;
  659. }
  660. /*
  661. * AMD erratum #91 manifests as a spurious page fault on a PREFETCH
  662. * instruction.
  663. */
  664. if (is_prefetch(regs, error_code, address))
  665. return;
  666. page_fault_oops(regs, error_code, address);
  667. }
  668. /*
  669. * Print out info about fatal segfaults, if the show_unhandled_signals
  670. * sysctl is set:
  671. */
  672. static inline void
  673. show_signal_msg(struct pt_regs *regs, unsigned long error_code,
  674. unsigned long address, struct task_struct *tsk)
  675. {
  676. const char *loglvl = task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG;
  677. /* This is a racy snapshot, but it's better than nothing. */
  678. int cpu = raw_smp_processor_id();
  679. if (!unhandled_signal(tsk, SIGSEGV))
  680. return;
  681. if (!printk_ratelimit())
  682. return;
  683. printk("%s%s[%d]: segfault at %lx ip %px sp %px error %lx",
  684. loglvl, tsk->comm, task_pid_nr(tsk), address,
  685. (void *)regs->ip, (void *)regs->sp, error_code);
  686. print_vma_addr(KERN_CONT " in ", regs->ip);
  687. /*
  688. * Dump the likely CPU where the fatal segfault happened.
  689. * This can help identify faulty hardware.
  690. */
  691. printk(KERN_CONT " likely on CPU %d (core %d, socket %d)", cpu,
  692. topology_core_id(cpu), topology_physical_package_id(cpu));
  693. printk(KERN_CONT "\n");
  694. show_opcodes(regs, loglvl);
  695. }
  696. /*
  697. * The (legacy) vsyscall page is the long page in the kernel portion
  698. * of the address space that has user-accessible permissions.
  699. */
  700. static bool is_vsyscall_vaddr(unsigned long vaddr)
  701. {
  702. return unlikely((vaddr & PAGE_MASK) == VSYSCALL_ADDR);
  703. }
  704. static void
  705. __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
  706. unsigned long address, u32 pkey, int si_code)
  707. {
  708. struct task_struct *tsk = current;
  709. if (!user_mode(regs)) {
  710. kernelmode_fixup_or_oops(regs, error_code, address,
  711. SIGSEGV, si_code, pkey);
  712. return;
  713. }
  714. if (!(error_code & X86_PF_USER)) {
  715. /* Implicit user access to kernel memory -- just oops */
  716. page_fault_oops(regs, error_code, address);
  717. return;
  718. }
  719. /*
  720. * User mode accesses just cause a SIGSEGV.
  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. sanitize_error_code(address, &error_code);
  733. if (fixup_vdso_exception(regs, X86_TRAP_PF, error_code, address))
  734. return;
  735. if (likely(show_unhandled_signals))
  736. show_signal_msg(regs, error_code, address, tsk);
  737. set_signal_archinfo(address, error_code);
  738. if (si_code == SEGV_PKUERR)
  739. force_sig_pkuerr((void __user *)address, pkey);
  740. else
  741. force_sig_fault(SIGSEGV, si_code, (void __user *)address);
  742. local_irq_disable();
  743. }
  744. static noinline void
  745. bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
  746. unsigned long address)
  747. {
  748. __bad_area_nosemaphore(regs, error_code, address, 0, SEGV_MAPERR);
  749. }
  750. static void
  751. __bad_area(struct pt_regs *regs, unsigned long error_code,
  752. unsigned long address, u32 pkey, int si_code)
  753. {
  754. struct mm_struct *mm = current->mm;
  755. /*
  756. * Something tried to access memory that isn't in our memory map..
  757. * Fix it, but check if it's kernel or user first..
  758. */
  759. mmap_read_unlock(mm);
  760. __bad_area_nosemaphore(regs, error_code, address, pkey, si_code);
  761. }
  762. static inline bool bad_area_access_from_pkeys(unsigned long error_code,
  763. struct vm_area_struct *vma)
  764. {
  765. /* This code is always called on the current mm */
  766. bool foreign = false;
  767. if (!cpu_feature_enabled(X86_FEATURE_OSPKE))
  768. return false;
  769. if (error_code & X86_PF_PK)
  770. return true;
  771. /* this checks permission keys on the VMA: */
  772. if (!arch_vma_access_permitted(vma, (error_code & X86_PF_WRITE),
  773. (error_code & X86_PF_INSTR), foreign))
  774. return true;
  775. return false;
  776. }
  777. static noinline void
  778. bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
  779. unsigned long address, struct vm_area_struct *vma)
  780. {
  781. /*
  782. * This OSPKE check is not strictly necessary at runtime.
  783. * But, doing it this way allows compiler optimizations
  784. * if pkeys are compiled out.
  785. */
  786. if (bad_area_access_from_pkeys(error_code, vma)) {
  787. /*
  788. * A protection key fault means that the PKRU value did not allow
  789. * access to some PTE. Userspace can figure out what PKRU was
  790. * from the XSAVE state. This function captures the pkey from
  791. * the vma and passes it to userspace so userspace can discover
  792. * which protection key was set on the PTE.
  793. *
  794. * If we get here, we know that the hardware signaled a X86_PF_PK
  795. * fault and that there was a VMA once we got in the fault
  796. * handler. It does *not* guarantee that the VMA we find here
  797. * was the one that we faulted on.
  798. *
  799. * 1. T1 : mprotect_key(foo, PAGE_SIZE, pkey=4);
  800. * 2. T1 : set PKRU to deny access to pkey=4, touches page
  801. * 3. T1 : faults...
  802. * 4. T2: mprotect_key(foo, PAGE_SIZE, pkey=5);
  803. * 5. T1 : enters fault handler, takes mmap_lock, etc...
  804. * 6. T1 : reaches here, sees vma_pkey(vma)=5, when we really
  805. * faulted on a pte with its pkey=4.
  806. */
  807. u32 pkey = vma_pkey(vma);
  808. __bad_area(regs, error_code, address, pkey, SEGV_PKUERR);
  809. } else {
  810. __bad_area(regs, error_code, address, 0, SEGV_ACCERR);
  811. }
  812. }
  813. static void
  814. do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address,
  815. vm_fault_t fault)
  816. {
  817. /* Kernel mode? Handle exceptions or die: */
  818. if (!user_mode(regs)) {
  819. kernelmode_fixup_or_oops(regs, error_code, address,
  820. SIGBUS, BUS_ADRERR, ARCH_DEFAULT_PKEY);
  821. return;
  822. }
  823. /* User-space => ok to do another page fault: */
  824. if (is_prefetch(regs, error_code, address))
  825. return;
  826. sanitize_error_code(address, &error_code);
  827. if (fixup_vdso_exception(regs, X86_TRAP_PF, error_code, address))
  828. return;
  829. set_signal_archinfo(address, error_code);
  830. #ifdef CONFIG_MEMORY_FAILURE
  831. if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
  832. struct task_struct *tsk = current;
  833. unsigned lsb = 0;
  834. pr_err(
  835. "MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n",
  836. tsk->comm, tsk->pid, address);
  837. if (fault & VM_FAULT_HWPOISON_LARGE)
  838. lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault));
  839. if (fault & VM_FAULT_HWPOISON)
  840. lsb = PAGE_SHIFT;
  841. force_sig_mceerr(BUS_MCEERR_AR, (void __user *)address, lsb);
  842. return;
  843. }
  844. #endif
  845. force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address);
  846. }
  847. static int spurious_kernel_fault_check(unsigned long error_code, pte_t *pte)
  848. {
  849. if ((error_code & X86_PF_WRITE) && !pte_write(*pte))
  850. return 0;
  851. if ((error_code & X86_PF_INSTR) && !pte_exec(*pte))
  852. return 0;
  853. return 1;
  854. }
  855. /*
  856. * Handle a spurious fault caused by a stale TLB entry.
  857. *
  858. * This allows us to lazily refresh the TLB when increasing the
  859. * permissions of a kernel page (RO -> RW or NX -> X). Doing it
  860. * eagerly is very expensive since that implies doing a full
  861. * cross-processor TLB flush, even if no stale TLB entries exist
  862. * on other processors.
  863. *
  864. * Spurious faults may only occur if the TLB contains an entry with
  865. * fewer permission than the page table entry. Non-present (P = 0)
  866. * and reserved bit (R = 1) faults are never spurious.
  867. *
  868. * There are no security implications to leaving a stale TLB when
  869. * increasing the permissions on a page.
  870. *
  871. * Returns non-zero if a spurious fault was handled, zero otherwise.
  872. *
  873. * See Intel Developer's Manual Vol 3 Section 4.10.4.3, bullet 3
  874. * (Optional Invalidation).
  875. */
  876. static noinline int
  877. spurious_kernel_fault(unsigned long error_code, unsigned long address)
  878. {
  879. pgd_t *pgd;
  880. p4d_t *p4d;
  881. pud_t *pud;
  882. pmd_t *pmd;
  883. pte_t *pte;
  884. int ret;
  885. /*
  886. * Only writes to RO or instruction fetches from NX may cause
  887. * spurious faults.
  888. *
  889. * These could be from user or supervisor accesses but the TLB
  890. * is only lazily flushed after a kernel mapping protection
  891. * change, so user accesses are not expected to cause spurious
  892. * faults.
  893. */
  894. if (error_code != (X86_PF_WRITE | X86_PF_PROT) &&
  895. error_code != (X86_PF_INSTR | X86_PF_PROT))
  896. return 0;
  897. pgd = init_mm.pgd + pgd_index(address);
  898. if (!pgd_present(*pgd))
  899. return 0;
  900. p4d = p4d_offset(pgd, address);
  901. if (!p4d_present(*p4d))
  902. return 0;
  903. if (p4d_large(*p4d))
  904. return spurious_kernel_fault_check(error_code, (pte_t *) p4d);
  905. pud = pud_offset(p4d, address);
  906. if (!pud_present(*pud))
  907. return 0;
  908. if (pud_large(*pud))
  909. return spurious_kernel_fault_check(error_code, (pte_t *) pud);
  910. pmd = pmd_offset(pud, address);
  911. if (!pmd_present(*pmd))
  912. return 0;
  913. if (pmd_large(*pmd))
  914. return spurious_kernel_fault_check(error_code, (pte_t *) pmd);
  915. pte = pte_offset_kernel(pmd, address);
  916. if (!pte_present(*pte))
  917. return 0;
  918. ret = spurious_kernel_fault_check(error_code, pte);
  919. if (!ret)
  920. return 0;
  921. /*
  922. * Make sure we have permissions in PMD.
  923. * If not, then there's a bug in the page tables:
  924. */
  925. ret = spurious_kernel_fault_check(error_code, (pte_t *) pmd);
  926. WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
  927. return ret;
  928. }
  929. NOKPROBE_SYMBOL(spurious_kernel_fault);
  930. int show_unhandled_signals = 1;
  931. static inline int
  932. access_error(unsigned long error_code, struct vm_area_struct *vma)
  933. {
  934. /* This is only called for the current mm, so: */
  935. bool foreign = false;
  936. /*
  937. * Read or write was blocked by protection keys. This is
  938. * always an unconditional error and can never result in
  939. * a follow-up action to resolve the fault, like a COW.
  940. */
  941. if (error_code & X86_PF_PK)
  942. return 1;
  943. /*
  944. * SGX hardware blocked the access. This usually happens
  945. * when the enclave memory contents have been destroyed, like
  946. * after a suspend/resume cycle. In any case, the kernel can't
  947. * fix the cause of the fault. Handle the fault as an access
  948. * error even in cases where no actual access violation
  949. * occurred. This allows userspace to rebuild the enclave in
  950. * response to the signal.
  951. */
  952. if (unlikely(error_code & X86_PF_SGX))
  953. return 1;
  954. /*
  955. * Make sure to check the VMA so that we do not perform
  956. * faults just to hit a X86_PF_PK as soon as we fill in a
  957. * page.
  958. */
  959. if (!arch_vma_access_permitted(vma, (error_code & X86_PF_WRITE),
  960. (error_code & X86_PF_INSTR), foreign))
  961. return 1;
  962. if (error_code & X86_PF_WRITE) {
  963. /* write, present and write, not present: */
  964. if (unlikely(!(vma->vm_flags & VM_WRITE)))
  965. return 1;
  966. return 0;
  967. }
  968. /* read, present: */
  969. if (unlikely(error_code & X86_PF_PROT))
  970. return 1;
  971. /* read, not present: */
  972. if (unlikely(!vma_is_accessible(vma)))
  973. return 1;
  974. return 0;
  975. }
  976. bool fault_in_kernel_space(unsigned long address)
  977. {
  978. /*
  979. * On 64-bit systems, the vsyscall page is at an address above
  980. * TASK_SIZE_MAX, but is not considered part of the kernel
  981. * address space.
  982. */
  983. if (IS_ENABLED(CONFIG_X86_64) && is_vsyscall_vaddr(address))
  984. return false;
  985. return address >= TASK_SIZE_MAX;
  986. }
  987. /*
  988. * Called for all faults where 'address' is part of the kernel address
  989. * space. Might get called for faults that originate from *code* that
  990. * ran in userspace or the kernel.
  991. */
  992. static void
  993. do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code,
  994. unsigned long address)
  995. {
  996. /*
  997. * Protection keys exceptions only happen on user pages. We
  998. * have no user pages in the kernel portion of the address
  999. * space, so do not expect them here.
  1000. */
  1001. WARN_ON_ONCE(hw_error_code & X86_PF_PK);
  1002. #ifdef CONFIG_X86_32
  1003. /*
  1004. * We can fault-in kernel-space virtual memory on-demand. The
  1005. * 'reference' page table is init_mm.pgd.
  1006. *
  1007. * NOTE! We MUST NOT take any locks for this case. We may
  1008. * be in an interrupt or a critical region, and should
  1009. * only copy the information from the master page table,
  1010. * nothing more.
  1011. *
  1012. * Before doing this on-demand faulting, ensure that the
  1013. * fault is not any of the following:
  1014. * 1. A fault on a PTE with a reserved bit set.
  1015. * 2. A fault caused by a user-mode access. (Do not demand-
  1016. * fault kernel memory due to user-mode accesses).
  1017. * 3. A fault caused by a page-level protection violation.
  1018. * (A demand fault would be on a non-present page which
  1019. * would have X86_PF_PROT==0).
  1020. *
  1021. * This is only needed to close a race condition on x86-32 in
  1022. * the vmalloc mapping/unmapping code. See the comment above
  1023. * vmalloc_fault() for details. On x86-64 the race does not
  1024. * exist as the vmalloc mappings don't need to be synchronized
  1025. * there.
  1026. */
  1027. if (!(hw_error_code & (X86_PF_RSVD | X86_PF_USER | X86_PF_PROT))) {
  1028. if (vmalloc_fault(address) >= 0)
  1029. return;
  1030. }
  1031. #endif
  1032. if (is_f00f_bug(regs, hw_error_code, address))
  1033. return;
  1034. /* Was the fault spurious, caused by lazy TLB invalidation? */
  1035. if (spurious_kernel_fault(hw_error_code, address))
  1036. return;
  1037. /* kprobes don't want to hook the spurious faults: */
  1038. if (WARN_ON_ONCE(kprobe_page_fault(regs, X86_TRAP_PF)))
  1039. return;
  1040. /*
  1041. * Note, despite being a "bad area", there are quite a few
  1042. * acceptable reasons to get here, such as erratum fixups
  1043. * and handling kernel code that can fault, like get_user().
  1044. *
  1045. * Don't take the mm semaphore here. If we fixup a prefetch
  1046. * fault we could otherwise deadlock:
  1047. */
  1048. bad_area_nosemaphore(regs, hw_error_code, address);
  1049. }
  1050. NOKPROBE_SYMBOL(do_kern_addr_fault);
  1051. /*
  1052. * Handle faults in the user portion of the address space. Nothing in here
  1053. * should check X86_PF_USER without a specific justification: for almost
  1054. * all purposes, we should treat a normal kernel access to user memory
  1055. * (e.g. get_user(), put_user(), etc.) the same as the WRUSS instruction.
  1056. * The one exception is AC flag handling, which is, per the x86
  1057. * architecture, special for WRUSS.
  1058. */
  1059. static inline
  1060. void do_user_addr_fault(struct pt_regs *regs,
  1061. unsigned long error_code,
  1062. unsigned long address)
  1063. {
  1064. struct vm_area_struct *vma;
  1065. struct task_struct *tsk;
  1066. struct mm_struct *mm;
  1067. vm_fault_t fault;
  1068. unsigned int flags = FAULT_FLAG_DEFAULT;
  1069. tsk = current;
  1070. mm = tsk->mm;
  1071. if (unlikely((error_code & (X86_PF_USER | X86_PF_INSTR)) == X86_PF_INSTR)) {
  1072. /*
  1073. * Whoops, this is kernel mode code trying to execute from
  1074. * user memory. Unless this is AMD erratum #93, which
  1075. * corrupts RIP such that it looks like a user address,
  1076. * this is unrecoverable. Don't even try to look up the
  1077. * VMA or look for extable entries.
  1078. */
  1079. if (is_errata93(regs, address))
  1080. return;
  1081. page_fault_oops(regs, error_code, address);
  1082. return;
  1083. }
  1084. /* kprobes don't want to hook the spurious faults: */
  1085. if (WARN_ON_ONCE(kprobe_page_fault(regs, X86_TRAP_PF)))
  1086. return;
  1087. /*
  1088. * Reserved bits are never expected to be set on
  1089. * entries in the user portion of the page tables.
  1090. */
  1091. if (unlikely(error_code & X86_PF_RSVD))
  1092. pgtable_bad(regs, error_code, address);
  1093. /*
  1094. * If SMAP is on, check for invalid kernel (supervisor) access to user
  1095. * pages in the user address space. The odd case here is WRUSS,
  1096. * which, according to the preliminary documentation, does not respect
  1097. * SMAP and will have the USER bit set so, in all cases, SMAP
  1098. * enforcement appears to be consistent with the USER bit.
  1099. */
  1100. if (unlikely(cpu_feature_enabled(X86_FEATURE_SMAP) &&
  1101. !(error_code & X86_PF_USER) &&
  1102. !(regs->flags & X86_EFLAGS_AC))) {
  1103. /*
  1104. * No extable entry here. This was a kernel access to an
  1105. * invalid pointer. get_kernel_nofault() will not get here.
  1106. */
  1107. page_fault_oops(regs, error_code, address);
  1108. return;
  1109. }
  1110. /*
  1111. * If we're in an interrupt, have no user context or are running
  1112. * in a region with pagefaults disabled then we must not take the fault
  1113. */
  1114. if (unlikely(faulthandler_disabled() || !mm)) {
  1115. bad_area_nosemaphore(regs, error_code, address);
  1116. return;
  1117. }
  1118. /*
  1119. * It's safe to allow irq's after cr2 has been saved and the
  1120. * vmalloc fault has been handled.
  1121. *
  1122. * User-mode registers count as a user access even for any
  1123. * potential system fault or CPU buglet:
  1124. */
  1125. if (user_mode(regs)) {
  1126. local_irq_enable();
  1127. flags |= FAULT_FLAG_USER;
  1128. } else {
  1129. if (regs->flags & X86_EFLAGS_IF)
  1130. local_irq_enable();
  1131. }
  1132. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  1133. if (error_code & X86_PF_WRITE)
  1134. flags |= FAULT_FLAG_WRITE;
  1135. if (error_code & X86_PF_INSTR)
  1136. flags |= FAULT_FLAG_INSTRUCTION;
  1137. #ifdef CONFIG_X86_64
  1138. /*
  1139. * Faults in the vsyscall page might need emulation. The
  1140. * vsyscall page is at a high address (>PAGE_OFFSET), but is
  1141. * considered to be part of the user address space.
  1142. *
  1143. * The vsyscall page does not have a "real" VMA, so do this
  1144. * emulation before we go searching for VMAs.
  1145. *
  1146. * PKRU never rejects instruction fetches, so we don't need
  1147. * to consider the PF_PK bit.
  1148. */
  1149. if (is_vsyscall_vaddr(address)) {
  1150. if (emulate_vsyscall(error_code, regs, address))
  1151. return;
  1152. }
  1153. #endif
  1154. if (!(flags & FAULT_FLAG_USER))
  1155. goto lock_mmap;
  1156. vma = lock_vma_under_rcu(mm, address);
  1157. if (!vma)
  1158. goto lock_mmap;
  1159. if (unlikely(access_error(error_code, vma))) {
  1160. vma_end_read(vma);
  1161. goto lock_mmap;
  1162. }
  1163. fault = handle_mm_fault(vma, address, flags | FAULT_FLAG_VMA_LOCK, regs);
  1164. if (!(fault & (VM_FAULT_RETRY | VM_FAULT_COMPLETED)))
  1165. vma_end_read(vma);
  1166. if (!(fault & VM_FAULT_RETRY)) {
  1167. count_vm_vma_lock_event(VMA_LOCK_SUCCESS);
  1168. goto done;
  1169. }
  1170. count_vm_vma_lock_event(VMA_LOCK_RETRY);
  1171. if (fault & VM_FAULT_MAJOR)
  1172. flags |= FAULT_FLAG_TRIED;
  1173. /* Quick path to respond to signals */
  1174. if (fault_signal_pending(fault, regs)) {
  1175. if (!user_mode(regs))
  1176. kernelmode_fixup_or_oops(regs, error_code, address,
  1177. SIGBUS, BUS_ADRERR,
  1178. ARCH_DEFAULT_PKEY);
  1179. return;
  1180. }
  1181. lock_mmap:
  1182. retry:
  1183. vma = lock_mm_and_find_vma(mm, address, regs);
  1184. if (unlikely(!vma)) {
  1185. bad_area_nosemaphore(regs, error_code, address);
  1186. return;
  1187. }
  1188. /*
  1189. * Ok, we have a good vm_area for this memory access, so
  1190. * we can handle it..
  1191. */
  1192. if (unlikely(access_error(error_code, vma))) {
  1193. bad_area_access_error(regs, error_code, address, vma);
  1194. return;
  1195. }
  1196. /*
  1197. * If for any reason at all we couldn't handle the fault,
  1198. * make sure we exit gracefully rather than endlessly redo
  1199. * the fault. Since we never set FAULT_FLAG_RETRY_NOWAIT, if
  1200. * we get VM_FAULT_RETRY back, the mmap_lock has been unlocked.
  1201. *
  1202. * Note that handle_userfault() may also release and reacquire mmap_lock
  1203. * (and not return with VM_FAULT_RETRY), when returning to userland to
  1204. * repeat the page fault later with a VM_FAULT_NOPAGE retval
  1205. * (potentially after handling any pending signal during the return to
  1206. * userland). The return to userland is identified whenever
  1207. * FAULT_FLAG_USER|FAULT_FLAG_KILLABLE are both set in flags.
  1208. */
  1209. fault = handle_mm_fault(vma, address, flags, regs);
  1210. if (fault_signal_pending(fault, regs)) {
  1211. /*
  1212. * Quick path to respond to signals. The core mm code
  1213. * has unlocked the mm for us if we get here.
  1214. */
  1215. if (!user_mode(regs))
  1216. kernelmode_fixup_or_oops(regs, error_code, address,
  1217. SIGBUS, BUS_ADRERR,
  1218. ARCH_DEFAULT_PKEY);
  1219. return;
  1220. }
  1221. /* The fault is fully completed (including releasing mmap lock) */
  1222. if (fault & VM_FAULT_COMPLETED)
  1223. return;
  1224. /*
  1225. * If we need to retry the mmap_lock has already been released,
  1226. * and if there is a fatal signal pending there is no guarantee
  1227. * that we made any progress. Handle this case first.
  1228. */
  1229. if (unlikely(fault & VM_FAULT_RETRY)) {
  1230. flags |= FAULT_FLAG_TRIED;
  1231. goto retry;
  1232. }
  1233. mmap_read_unlock(mm);
  1234. done:
  1235. if (likely(!(fault & VM_FAULT_ERROR)))
  1236. return;
  1237. if (fatal_signal_pending(current) && !user_mode(regs)) {
  1238. kernelmode_fixup_or_oops(regs, error_code, address,
  1239. 0, 0, ARCH_DEFAULT_PKEY);
  1240. return;
  1241. }
  1242. if (fault & VM_FAULT_OOM) {
  1243. /* Kernel mode? Handle exceptions or die: */
  1244. if (!user_mode(regs)) {
  1245. kernelmode_fixup_or_oops(regs, error_code, address,
  1246. SIGSEGV, SEGV_MAPERR,
  1247. ARCH_DEFAULT_PKEY);
  1248. return;
  1249. }
  1250. /*
  1251. * We ran out of memory, call the OOM killer, and return the
  1252. * userspace (which will retry the fault, or kill us if we got
  1253. * oom-killed):
  1254. */
  1255. pagefault_out_of_memory();
  1256. } else {
  1257. if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|
  1258. VM_FAULT_HWPOISON_LARGE))
  1259. do_sigbus(regs, error_code, address, fault);
  1260. else if (fault & VM_FAULT_SIGSEGV)
  1261. bad_area_nosemaphore(regs, error_code, address);
  1262. else
  1263. BUG();
  1264. }
  1265. }
  1266. NOKPROBE_SYMBOL(do_user_addr_fault);
  1267. static __always_inline void
  1268. trace_page_fault_entries(struct pt_regs *regs, unsigned long error_code,
  1269. unsigned long address)
  1270. {
  1271. if (!trace_pagefault_enabled())
  1272. return;
  1273. if (user_mode(regs))
  1274. trace_page_fault_user(address, regs, error_code);
  1275. else
  1276. trace_page_fault_kernel(address, regs, error_code);
  1277. }
  1278. static __always_inline void
  1279. handle_page_fault(struct pt_regs *regs, unsigned long error_code,
  1280. unsigned long address)
  1281. {
  1282. trace_page_fault_entries(regs, error_code, address);
  1283. if (unlikely(kmmio_fault(regs, address)))
  1284. return;
  1285. /* Was the fault on kernel-controlled part of the address space? */
  1286. if (unlikely(fault_in_kernel_space(address))) {
  1287. do_kern_addr_fault(regs, error_code, address);
  1288. } else {
  1289. do_user_addr_fault(regs, error_code, address);
  1290. /*
  1291. * User address page fault handling might have reenabled
  1292. * interrupts. Fixing up all potential exit points of
  1293. * do_user_addr_fault() and its leaf functions is just not
  1294. * doable w/o creating an unholy mess or turning the code
  1295. * upside down.
  1296. */
  1297. local_irq_disable();
  1298. }
  1299. }
  1300. DEFINE_IDTENTRY_RAW_ERRORCODE(exc_page_fault)
  1301. {
  1302. unsigned long address = read_cr2();
  1303. irqentry_state_t state;
  1304. prefetchw(&current->mm->mmap_lock);
  1305. /*
  1306. * KVM uses #PF vector to deliver 'page not present' events to guests
  1307. * (asynchronous page fault mechanism). The event happens when a
  1308. * userspace task is trying to access some valid (from guest's point of
  1309. * view) memory which is not currently mapped by the host (e.g. the
  1310. * memory is swapped out). Note, the corresponding "page ready" event
  1311. * which is injected when the memory becomes available, is delivered via
  1312. * an interrupt mechanism and not a #PF exception
  1313. * (see arch/x86/kernel/kvm.c: sysvec_kvm_asyncpf_interrupt()).
  1314. *
  1315. * We are relying on the interrupted context being sane (valid RSP,
  1316. * relevant locks not held, etc.), which is fine as long as the
  1317. * interrupted context had IF=1. We are also relying on the KVM
  1318. * async pf type field and CR2 being read consistently instead of
  1319. * getting values from real and async page faults mixed up.
  1320. *
  1321. * Fingers crossed.
  1322. *
  1323. * The async #PF handling code takes care of idtentry handling
  1324. * itself.
  1325. */
  1326. if (kvm_handle_async_pf(regs, (u32)address))
  1327. return;
  1328. /*
  1329. * Entry handling for valid #PF from kernel mode is slightly
  1330. * different: RCU is already watching and ct_irq_enter() must not
  1331. * be invoked because a kernel fault on a user space address might
  1332. * sleep.
  1333. *
  1334. * In case the fault hit a RCU idle region the conditional entry
  1335. * code reenabled RCU to avoid subsequent wreckage which helps
  1336. * debuggability.
  1337. */
  1338. state = irqentry_enter(regs);
  1339. instrumentation_begin();
  1340. handle_page_fault(regs, error_code, address);
  1341. instrumentation_end();
  1342. irqentry_exit(regs, state);
  1343. }