fault.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485
  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/module.h> /* search_exception_table */
  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, 0);
  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:");
  579. printk_address(regs->ip);
  580. dump_pagetable(address);
  581. }
  582. static noinline void
  583. pgtable_bad(struct pt_regs *regs, unsigned long error_code,
  584. unsigned long address)
  585. {
  586. struct task_struct *tsk;
  587. unsigned long flags;
  588. int sig;
  589. flags = oops_begin();
  590. tsk = current;
  591. sig = SIGKILL;
  592. printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
  593. tsk->comm, address);
  594. dump_pagetable(address);
  595. tsk->thread.cr2 = address;
  596. tsk->thread.trap_nr = X86_TRAP_PF;
  597. tsk->thread.error_code = error_code;
  598. if (__die("Bad pagetable", regs, error_code))
  599. sig = 0;
  600. oops_end(flags, regs, sig);
  601. }
  602. static noinline void
  603. no_context(struct pt_regs *regs, unsigned long error_code,
  604. unsigned long address, int signal, int si_code)
  605. {
  606. struct task_struct *tsk = current;
  607. unsigned long flags;
  608. int sig;
  609. /* No context means no VMA to pass down */
  610. struct vm_area_struct *vma = NULL;
  611. /* Are we prepared to handle this kernel fault? */
  612. if (fixup_exception(regs, X86_TRAP_PF)) {
  613. /*
  614. * Any interrupt that takes a fault gets the fixup. This makes
  615. * the below recursive fault logic only apply to a faults from
  616. * task context.
  617. */
  618. if (in_interrupt())
  619. return;
  620. /*
  621. * Per the above we're !in_interrupt(), aka. task context.
  622. *
  623. * In this case we need to make sure we're not recursively
  624. * faulting through the emulate_vsyscall() logic.
  625. */
  626. if (current->thread.sig_on_uaccess_err && signal) {
  627. tsk->thread.trap_nr = X86_TRAP_PF;
  628. tsk->thread.error_code = error_code | PF_USER;
  629. tsk->thread.cr2 = address;
  630. /* XXX: hwpoison faults will set the wrong code. */
  631. force_sig_info_fault(signal, si_code, address,
  632. tsk, vma, 0);
  633. }
  634. /*
  635. * Barring that, we can do the fixup and be happy.
  636. */
  637. return;
  638. }
  639. #ifdef CONFIG_VMAP_STACK
  640. /*
  641. * Stack overflow? During boot, we can fault near the initial
  642. * stack in the direct map, but that's not an overflow -- check
  643. * that we're in vmalloc space to avoid this.
  644. */
  645. if (is_vmalloc_addr((void *)address) &&
  646. (((unsigned long)tsk->stack - 1 - address < PAGE_SIZE) ||
  647. address - ((unsigned long)tsk->stack + THREAD_SIZE) < PAGE_SIZE)) {
  648. register void *__sp asm("rsp");
  649. unsigned long stack = this_cpu_read(orig_ist.ist[DOUBLEFAULT_STACK]) - sizeof(void *);
  650. /*
  651. * We're likely to be running with very little stack space
  652. * left. It's plausible that we'd hit this condition but
  653. * double-fault even before we get this far, in which case
  654. * we're fine: the double-fault handler will deal with it.
  655. *
  656. * We don't want to make it all the way into the oops code
  657. * and then double-fault, though, because we're likely to
  658. * break the console driver and lose most of the stack dump.
  659. */
  660. asm volatile ("movq %[stack], %%rsp\n\t"
  661. "call handle_stack_overflow\n\t"
  662. "1: jmp 1b"
  663. : "+r" (__sp)
  664. : "D" ("kernel stack overflow (page fault)"),
  665. "S" (regs), "d" (address),
  666. [stack] "rm" (stack));
  667. unreachable();
  668. }
  669. #endif
  670. /*
  671. * 32-bit:
  672. *
  673. * Valid to do another page fault here, because if this fault
  674. * had been triggered by is_prefetch fixup_exception would have
  675. * handled it.
  676. *
  677. * 64-bit:
  678. *
  679. * Hall of shame of CPU/BIOS bugs.
  680. */
  681. if (is_prefetch(regs, error_code, address))
  682. return;
  683. if (is_errata93(regs, address))
  684. return;
  685. /*
  686. * Oops. The kernel tried to access some bad page. We'll have to
  687. * terminate things with extreme prejudice:
  688. */
  689. flags = oops_begin();
  690. show_fault_oops(regs, error_code, address);
  691. if (task_stack_end_corrupted(tsk))
  692. printk(KERN_EMERG "Thread overran stack, or stack corrupted\n");
  693. tsk->thread.cr2 = address;
  694. tsk->thread.trap_nr = X86_TRAP_PF;
  695. tsk->thread.error_code = error_code;
  696. sig = SIGKILL;
  697. if (__die("Oops", regs, error_code))
  698. sig = 0;
  699. /* Executive summary in case the body of the oops scrolled away */
  700. printk(KERN_DEFAULT "CR2: %016lx\n", address);
  701. oops_end(flags, regs, sig);
  702. }
  703. /*
  704. * Print out info about fatal segfaults, if the show_unhandled_signals
  705. * sysctl is set:
  706. */
  707. static inline void
  708. show_signal_msg(struct pt_regs *regs, unsigned long error_code,
  709. unsigned long address, struct task_struct *tsk)
  710. {
  711. if (!unhandled_signal(tsk, SIGSEGV))
  712. return;
  713. if (!printk_ratelimit())
  714. return;
  715. printk("%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
  716. task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
  717. tsk->comm, task_pid_nr(tsk), address,
  718. (void *)regs->ip, (void *)regs->sp, error_code);
  719. print_vma_addr(KERN_CONT " in ", regs->ip);
  720. printk(KERN_CONT "\n");
  721. }
  722. static void
  723. __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
  724. unsigned long address, struct vm_area_struct *vma,
  725. int si_code)
  726. {
  727. struct task_struct *tsk = current;
  728. /* User mode accesses just cause a SIGSEGV */
  729. if (error_code & PF_USER) {
  730. /*
  731. * It's possible to have interrupts off here:
  732. */
  733. local_irq_enable();
  734. /*
  735. * Valid to do another page fault here because this one came
  736. * from user space:
  737. */
  738. if (is_prefetch(regs, error_code, address))
  739. return;
  740. if (is_errata100(regs, address))
  741. return;
  742. #ifdef CONFIG_X86_64
  743. /*
  744. * Instruction fetch faults in the vsyscall page might need
  745. * emulation.
  746. */
  747. if (unlikely((error_code & PF_INSTR) &&
  748. ((address & ~0xfff) == VSYSCALL_ADDR))) {
  749. if (emulate_vsyscall(regs, address))
  750. return;
  751. }
  752. #endif
  753. /*
  754. * To avoid leaking information about the kernel page table
  755. * layout, pretend that user-mode accesses to kernel addresses
  756. * are always protection faults.
  757. */
  758. if (address >= TASK_SIZE_MAX)
  759. error_code |= PF_PROT;
  760. if (likely(show_unhandled_signals))
  761. show_signal_msg(regs, error_code, address, tsk);
  762. tsk->thread.cr2 = address;
  763. tsk->thread.error_code = error_code;
  764. tsk->thread.trap_nr = X86_TRAP_PF;
  765. force_sig_info_fault(SIGSEGV, si_code, address, tsk, vma, 0);
  766. return;
  767. }
  768. if (is_f00f_bug(regs, address))
  769. return;
  770. no_context(regs, error_code, address, SIGSEGV, si_code);
  771. }
  772. static noinline void
  773. bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
  774. unsigned long address, struct vm_area_struct *vma)
  775. {
  776. __bad_area_nosemaphore(regs, error_code, address, vma, SEGV_MAPERR);
  777. }
  778. static void
  779. __bad_area(struct pt_regs *regs, unsigned long error_code,
  780. unsigned long address, struct vm_area_struct *vma, int si_code)
  781. {
  782. struct mm_struct *mm = current->mm;
  783. /*
  784. * Something tried to access memory that isn't in our memory map..
  785. * Fix it, but check if it's kernel or user first..
  786. */
  787. up_read(&mm->mmap_sem);
  788. __bad_area_nosemaphore(regs, error_code, address, vma, si_code);
  789. }
  790. static noinline void
  791. bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
  792. {
  793. __bad_area(regs, error_code, address, NULL, SEGV_MAPERR);
  794. }
  795. static inline bool bad_area_access_from_pkeys(unsigned long error_code,
  796. struct vm_area_struct *vma)
  797. {
  798. /* This code is always called on the current mm */
  799. bool foreign = false;
  800. if (!boot_cpu_has(X86_FEATURE_OSPKE))
  801. return false;
  802. if (error_code & PF_PK)
  803. return true;
  804. /* this checks permission keys on the VMA: */
  805. if (!arch_vma_access_permitted(vma, (error_code & PF_WRITE),
  806. (error_code & PF_INSTR), foreign))
  807. return true;
  808. return false;
  809. }
  810. static noinline void
  811. bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
  812. unsigned long address, struct vm_area_struct *vma)
  813. {
  814. /*
  815. * This OSPKE check is not strictly necessary at runtime.
  816. * But, doing it this way allows compiler optimizations
  817. * if pkeys are compiled out.
  818. */
  819. if (bad_area_access_from_pkeys(error_code, vma))
  820. __bad_area(regs, error_code, address, vma, SEGV_PKUERR);
  821. else
  822. __bad_area(regs, error_code, address, vma, SEGV_ACCERR);
  823. }
  824. static void
  825. do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address,
  826. struct vm_area_struct *vma, unsigned int fault)
  827. {
  828. struct task_struct *tsk = current;
  829. int code = BUS_ADRERR;
  830. /* Kernel mode? Handle exceptions or die: */
  831. if (!(error_code & PF_USER)) {
  832. no_context(regs, error_code, address, SIGBUS, BUS_ADRERR);
  833. return;
  834. }
  835. /* User-space => ok to do another page fault: */
  836. if (is_prefetch(regs, error_code, address))
  837. return;
  838. tsk->thread.cr2 = address;
  839. tsk->thread.error_code = error_code;
  840. tsk->thread.trap_nr = X86_TRAP_PF;
  841. #ifdef CONFIG_MEMORY_FAILURE
  842. if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
  843. printk(KERN_ERR
  844. "MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n",
  845. tsk->comm, tsk->pid, address);
  846. code = BUS_MCEERR_AR;
  847. }
  848. #endif
  849. force_sig_info_fault(SIGBUS, code, address, tsk, vma, fault);
  850. }
  851. static noinline void
  852. mm_fault_error(struct pt_regs *regs, unsigned long error_code,
  853. unsigned long address, struct vm_area_struct *vma,
  854. unsigned int fault)
  855. {
  856. if (fatal_signal_pending(current) && !(error_code & PF_USER)) {
  857. no_context(regs, error_code, address, 0, 0);
  858. return;
  859. }
  860. if (fault & VM_FAULT_OOM) {
  861. /* Kernel mode? Handle exceptions or die: */
  862. if (!(error_code & PF_USER)) {
  863. no_context(regs, error_code, address,
  864. SIGSEGV, SEGV_MAPERR);
  865. return;
  866. }
  867. /*
  868. * We ran out of memory, call the OOM killer, and return the
  869. * userspace (which will retry the fault, or kill us if we got
  870. * oom-killed):
  871. */
  872. pagefault_out_of_memory();
  873. } else {
  874. if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|
  875. VM_FAULT_HWPOISON_LARGE))
  876. do_sigbus(regs, error_code, address, vma, fault);
  877. else if (fault & VM_FAULT_SIGSEGV)
  878. bad_area_nosemaphore(regs, error_code, address, vma);
  879. else
  880. BUG();
  881. }
  882. }
  883. static int spurious_fault_check(unsigned long error_code, pte_t *pte)
  884. {
  885. if ((error_code & PF_WRITE) && !pte_write(*pte))
  886. return 0;
  887. if ((error_code & PF_INSTR) && !pte_exec(*pte))
  888. return 0;
  889. /*
  890. * Note: We do not do lazy flushing on protection key
  891. * changes, so no spurious fault will ever set PF_PK.
  892. */
  893. if ((error_code & PF_PK))
  894. return 1;
  895. return 1;
  896. }
  897. /*
  898. * Handle a spurious fault caused by a stale TLB entry.
  899. *
  900. * This allows us to lazily refresh the TLB when increasing the
  901. * permissions of a kernel page (RO -> RW or NX -> X). Doing it
  902. * eagerly is very expensive since that implies doing a full
  903. * cross-processor TLB flush, even if no stale TLB entries exist
  904. * on other processors.
  905. *
  906. * Spurious faults may only occur if the TLB contains an entry with
  907. * fewer permission than the page table entry. Non-present (P = 0)
  908. * and reserved bit (R = 1) faults are never spurious.
  909. *
  910. * There are no security implications to leaving a stale TLB when
  911. * increasing the permissions on a page.
  912. *
  913. * Returns non-zero if a spurious fault was handled, zero otherwise.
  914. *
  915. * See Intel Developer's Manual Vol 3 Section 4.10.4.3, bullet 3
  916. * (Optional Invalidation).
  917. */
  918. static noinline int
  919. spurious_fault(unsigned long error_code, unsigned long address)
  920. {
  921. pgd_t *pgd;
  922. pud_t *pud;
  923. pmd_t *pmd;
  924. pte_t *pte;
  925. int ret;
  926. /*
  927. * Only writes to RO or instruction fetches from NX may cause
  928. * spurious faults.
  929. *
  930. * These could be from user or supervisor accesses but the TLB
  931. * is only lazily flushed after a kernel mapping protection
  932. * change, so user accesses are not expected to cause spurious
  933. * faults.
  934. */
  935. if (error_code != (PF_WRITE | PF_PROT)
  936. && error_code != (PF_INSTR | PF_PROT))
  937. return 0;
  938. pgd = init_mm.pgd + pgd_index(address);
  939. if (!pgd_present(*pgd))
  940. return 0;
  941. pud = pud_offset(pgd, address);
  942. if (!pud_present(*pud))
  943. return 0;
  944. if (pud_large(*pud))
  945. return spurious_fault_check(error_code, (pte_t *) pud);
  946. pmd = pmd_offset(pud, address);
  947. if (!pmd_present(*pmd))
  948. return 0;
  949. if (pmd_large(*pmd))
  950. return spurious_fault_check(error_code, (pte_t *) pmd);
  951. pte = pte_offset_kernel(pmd, address);
  952. if (!pte_present(*pte))
  953. return 0;
  954. ret = spurious_fault_check(error_code, pte);
  955. if (!ret)
  956. return 0;
  957. /*
  958. * Make sure we have permissions in PMD.
  959. * If not, then there's a bug in the page tables:
  960. */
  961. ret = spurious_fault_check(error_code, (pte_t *) pmd);
  962. WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
  963. return ret;
  964. }
  965. NOKPROBE_SYMBOL(spurious_fault);
  966. int show_unhandled_signals = 1;
  967. static inline int
  968. access_error(unsigned long error_code, struct vm_area_struct *vma)
  969. {
  970. /* This is only called for the current mm, so: */
  971. bool foreign = false;
  972. /*
  973. * Make sure to check the VMA so that we do not perform
  974. * faults just to hit a PF_PK as soon as we fill in a
  975. * page.
  976. */
  977. if (!arch_vma_access_permitted(vma, (error_code & PF_WRITE),
  978. (error_code & PF_INSTR), foreign))
  979. return 1;
  980. if (error_code & PF_WRITE) {
  981. /* write, present and write, not present: */
  982. if (unlikely(!(vma->vm_flags & VM_WRITE)))
  983. return 1;
  984. return 0;
  985. }
  986. /* read, present: */
  987. if (unlikely(error_code & PF_PROT))
  988. return 1;
  989. /* read, not present: */
  990. if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
  991. return 1;
  992. return 0;
  993. }
  994. static int fault_in_kernel_space(unsigned long address)
  995. {
  996. return address >= TASK_SIZE_MAX;
  997. }
  998. static inline bool smap_violation(int error_code, struct pt_regs *regs)
  999. {
  1000. if (!IS_ENABLED(CONFIG_X86_SMAP))
  1001. return false;
  1002. if (!static_cpu_has(X86_FEATURE_SMAP))
  1003. return false;
  1004. if (error_code & PF_USER)
  1005. return false;
  1006. if (!user_mode(regs) && (regs->flags & X86_EFLAGS_AC))
  1007. return false;
  1008. return true;
  1009. }
  1010. /*
  1011. * This routine handles page faults. It determines the address,
  1012. * and the problem, and then passes it off to one of the appropriate
  1013. * routines.
  1014. *
  1015. * This function must have noinline because both callers
  1016. * {,trace_}do_page_fault() have notrace on. Having this an actual function
  1017. * guarantees there's a function trace entry.
  1018. */
  1019. static noinline void
  1020. __do_page_fault(struct pt_regs *regs, unsigned long error_code,
  1021. unsigned long address)
  1022. {
  1023. struct vm_area_struct *vma;
  1024. struct task_struct *tsk;
  1025. struct mm_struct *mm;
  1026. int fault, major = 0;
  1027. unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  1028. tsk = current;
  1029. mm = tsk->mm;
  1030. /*
  1031. * Detect and handle instructions that would cause a page fault for
  1032. * both a tracked kernel page and a userspace page.
  1033. */
  1034. if (kmemcheck_active(regs))
  1035. kmemcheck_hide(regs);
  1036. prefetchw(&mm->mmap_sem);
  1037. if (unlikely(kmmio_fault(regs, address)))
  1038. return;
  1039. /*
  1040. * We fault-in kernel-space virtual memory on-demand. The
  1041. * 'reference' page table is init_mm.pgd.
  1042. *
  1043. * NOTE! We MUST NOT take any locks for this case. We may
  1044. * be in an interrupt or a critical region, and should
  1045. * only copy the information from the master page table,
  1046. * nothing more.
  1047. *
  1048. * This verifies that the fault happens in kernel space
  1049. * (error_code & 4) == 0, and that the fault was not a
  1050. * protection error (error_code & 9) == 0.
  1051. */
  1052. if (unlikely(fault_in_kernel_space(address))) {
  1053. if (!(error_code & (PF_RSVD | PF_USER | PF_PROT))) {
  1054. if (vmalloc_fault(address) >= 0)
  1055. return;
  1056. if (kmemcheck_fault(regs, address, error_code))
  1057. return;
  1058. }
  1059. /* Can handle a stale RO->RW TLB: */
  1060. if (spurious_fault(error_code, address))
  1061. return;
  1062. /* kprobes don't want to hook the spurious faults: */
  1063. if (kprobes_fault(regs))
  1064. return;
  1065. /*
  1066. * Don't take the mm semaphore here. If we fixup a prefetch
  1067. * fault we could otherwise deadlock:
  1068. */
  1069. bad_area_nosemaphore(regs, error_code, address, NULL);
  1070. return;
  1071. }
  1072. /* kprobes don't want to hook the spurious faults: */
  1073. if (unlikely(kprobes_fault(regs)))
  1074. return;
  1075. if (unlikely(error_code & PF_RSVD))
  1076. pgtable_bad(regs, error_code, address);
  1077. if (unlikely(smap_violation(error_code, regs))) {
  1078. bad_area_nosemaphore(regs, error_code, address, NULL);
  1079. return;
  1080. }
  1081. /*
  1082. * If we're in an interrupt, have no user context or are running
  1083. * in a region with pagefaults disabled then we must not take the fault
  1084. */
  1085. if (unlikely(faulthandler_disabled() || !mm)) {
  1086. bad_area_nosemaphore(regs, error_code, address, NULL);
  1087. return;
  1088. }
  1089. /*
  1090. * It's safe to allow irq's after cr2 has been saved and the
  1091. * vmalloc fault has been handled.
  1092. *
  1093. * User-mode registers count as a user access even for any
  1094. * potential system fault or CPU buglet:
  1095. */
  1096. if (user_mode(regs)) {
  1097. local_irq_enable();
  1098. error_code |= PF_USER;
  1099. flags |= FAULT_FLAG_USER;
  1100. } else {
  1101. if (regs->flags & X86_EFLAGS_IF)
  1102. local_irq_enable();
  1103. }
  1104. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  1105. if (error_code & PF_WRITE)
  1106. flags |= FAULT_FLAG_WRITE;
  1107. if (error_code & PF_INSTR)
  1108. flags |= FAULT_FLAG_INSTRUCTION;
  1109. /*
  1110. * When running in the kernel we expect faults to occur only to
  1111. * addresses in user space. All other faults represent errors in
  1112. * the kernel and should generate an OOPS. Unfortunately, in the
  1113. * case of an erroneous fault occurring in a code path which already
  1114. * holds mmap_sem we will deadlock attempting to validate the fault
  1115. * against the address space. Luckily the kernel only validly
  1116. * references user space from well defined areas of code, which are
  1117. * listed in the exceptions table.
  1118. *
  1119. * As the vast majority of faults will be valid we will only perform
  1120. * the source reference check when there is a possibility of a
  1121. * deadlock. Attempt to lock the address space, if we cannot we then
  1122. * validate the source. If this is invalid we can skip the address
  1123. * space check, thus avoiding the deadlock:
  1124. */
  1125. if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
  1126. if ((error_code & PF_USER) == 0 &&
  1127. !search_exception_tables(regs->ip)) {
  1128. bad_area_nosemaphore(regs, error_code, address, NULL);
  1129. return;
  1130. }
  1131. retry:
  1132. down_read(&mm->mmap_sem);
  1133. } else {
  1134. /*
  1135. * The above down_read_trylock() might have succeeded in
  1136. * which case we'll have missed the might_sleep() from
  1137. * down_read():
  1138. */
  1139. might_sleep();
  1140. }
  1141. vma = find_vma(mm, address);
  1142. if (unlikely(!vma)) {
  1143. bad_area(regs, error_code, address);
  1144. return;
  1145. }
  1146. if (likely(vma->vm_start <= address))
  1147. goto good_area;
  1148. if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
  1149. bad_area(regs, error_code, address);
  1150. return;
  1151. }
  1152. if (error_code & PF_USER) {
  1153. /*
  1154. * Accessing the stack below %sp is always a bug.
  1155. * The large cushion allows instructions like enter
  1156. * and pusha to work. ("enter $65535, $31" pushes
  1157. * 32 pointers and then decrements %sp by 65535.)
  1158. */
  1159. if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
  1160. bad_area(regs, error_code, address);
  1161. return;
  1162. }
  1163. }
  1164. if (unlikely(expand_stack(vma, address))) {
  1165. bad_area(regs, error_code, address);
  1166. return;
  1167. }
  1168. /*
  1169. * Ok, we have a good vm_area for this memory access, so
  1170. * we can handle it..
  1171. */
  1172. good_area:
  1173. if (unlikely(access_error(error_code, vma))) {
  1174. bad_area_access_error(regs, error_code, address, vma);
  1175. return;
  1176. }
  1177. /*
  1178. * If for any reason at all we couldn't handle the fault,
  1179. * make sure we exit gracefully rather than endlessly redo
  1180. * the fault. Since we never set FAULT_FLAG_RETRY_NOWAIT, if
  1181. * we get VM_FAULT_RETRY back, the mmap_sem has been unlocked.
  1182. */
  1183. fault = handle_mm_fault(vma, address, flags);
  1184. major |= fault & VM_FAULT_MAJOR;
  1185. /*
  1186. * If we need to retry the mmap_sem has already been released,
  1187. * and if there is a fatal signal pending there is no guarantee
  1188. * that we made any progress. Handle this case first.
  1189. */
  1190. if (unlikely(fault & VM_FAULT_RETRY)) {
  1191. /* Retry at most once */
  1192. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  1193. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  1194. flags |= FAULT_FLAG_TRIED;
  1195. if (!fatal_signal_pending(tsk))
  1196. goto retry;
  1197. }
  1198. /* User mode? Just return to handle the fatal exception */
  1199. if (flags & FAULT_FLAG_USER)
  1200. return;
  1201. /* Not returning to user mode? Handle exceptions or die: */
  1202. no_context(regs, error_code, address, SIGBUS, BUS_ADRERR);
  1203. return;
  1204. }
  1205. up_read(&mm->mmap_sem);
  1206. if (unlikely(fault & VM_FAULT_ERROR)) {
  1207. mm_fault_error(regs, error_code, address, vma, fault);
  1208. return;
  1209. }
  1210. /*
  1211. * Major/minor page fault accounting. If any of the events
  1212. * returned VM_FAULT_MAJOR, we account it as a major fault.
  1213. */
  1214. if (major) {
  1215. tsk->maj_flt++;
  1216. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address);
  1217. } else {
  1218. tsk->min_flt++;
  1219. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address);
  1220. }
  1221. check_v8086_mode(regs, address, tsk);
  1222. }
  1223. NOKPROBE_SYMBOL(__do_page_fault);
  1224. dotraplinkage void notrace
  1225. do_page_fault(struct pt_regs *regs, unsigned long error_code)
  1226. {
  1227. unsigned long address = read_cr2(); /* Get the faulting address */
  1228. enum ctx_state prev_state;
  1229. /*
  1230. * We must have this function tagged with __kprobes, notrace and call
  1231. * read_cr2() before calling anything else. To avoid calling any kind
  1232. * of tracing machinery before we've observed the CR2 value.
  1233. *
  1234. * exception_{enter,exit}() contain all sorts of tracepoints.
  1235. */
  1236. prev_state = exception_enter();
  1237. __do_page_fault(regs, error_code, address);
  1238. exception_exit(prev_state);
  1239. }
  1240. NOKPROBE_SYMBOL(do_page_fault);
  1241. #ifdef CONFIG_TRACING
  1242. static nokprobe_inline void
  1243. trace_page_fault_entries(unsigned long address, struct pt_regs *regs,
  1244. unsigned long error_code)
  1245. {
  1246. if (user_mode(regs))
  1247. trace_page_fault_user(address, regs, error_code);
  1248. else
  1249. trace_page_fault_kernel(address, regs, error_code);
  1250. }
  1251. dotraplinkage void notrace
  1252. trace_do_page_fault(struct pt_regs *regs, unsigned long error_code)
  1253. {
  1254. /*
  1255. * The exception_enter and tracepoint processing could
  1256. * trigger another page faults (user space callchain
  1257. * reading) and destroy the original cr2 value, so read
  1258. * the faulting address now.
  1259. */
  1260. unsigned long address = read_cr2();
  1261. enum ctx_state prev_state;
  1262. prev_state = exception_enter();
  1263. trace_page_fault_entries(address, regs, error_code);
  1264. __do_page_fault(regs, error_code, address);
  1265. exception_exit(prev_state);
  1266. }
  1267. NOKPROBE_SYMBOL(trace_do_page_fault);
  1268. #endif /* CONFIG_TRACING */