fault.c 37 KB

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