fault.c 39 KB

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