fault.c 38 KB

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