fault.c 37 KB

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