fault.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494
  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/sched/task_stack.h> /* task_stack_*(), ... */
  8. #include <linux/kdebug.h> /* oops_begin/end, ... */
  9. #include <linux/extable.h> /* search_exception_tables */
  10. #include <linux/bootmem.h> /* max_low_pfn */
  11. #include <linux/kprobes.h> /* NOKPROBE_SYMBOL, ... */
  12. #include <linux/mmiotrace.h> /* kmmio_handler, ... */
  13. #include <linux/perf_event.h> /* perf_sw_event */
  14. #include <linux/hugetlb.h> /* hstate_index_to_shift */
  15. #include <linux/prefetch.h> /* prefetchw */
  16. #include <linux/context_tracking.h> /* exception_enter(), ... */
  17. #include <linux/uaccess.h> /* faulthandler_disabled() */
  18. #include <asm/cpufeature.h> /* boot_cpu_has, ... */
  19. #include <asm/traps.h> /* dotraplinkage, ... */
  20. #include <asm/pgalloc.h> /* pgd_*(), ... */
  21. #include <asm/kmemcheck.h> /* kmemcheck_*(), ... */
  22. #include <asm/fixmap.h> /* VSYSCALL_ADDR */
  23. #include <asm/vsyscall.h> /* emulate_vsyscall */
  24. #include <asm/vm86.h> /* struct vm86 */
  25. #include <asm/mmu_context.h> /* vma_pkey() */
  26. #define CREATE_TRACE_POINTS
  27. #include <asm/trace/exceptions.h>
  28. /*
  29. * Page fault error code bits:
  30. *
  31. * bit 0 == 0: no page found 1: protection fault
  32. * bit 1 == 0: read access 1: write access
  33. * bit 2 == 0: kernel-mode access 1: user-mode access
  34. * bit 3 == 1: use of reserved bit detected
  35. * bit 4 == 1: fault was an instruction fetch
  36. * bit 5 == 1: protection keys block access
  37. */
  38. enum x86_pf_error_code {
  39. PF_PROT = 1 << 0,
  40. PF_WRITE = 1 << 1,
  41. PF_USER = 1 << 2,
  42. PF_RSVD = 1 << 3,
  43. PF_INSTR = 1 << 4,
  44. PF_PK = 1 << 5,
  45. };
  46. /*
  47. * Returns 0 if mmiotrace is disabled, or if the fault is not
  48. * handled by mmiotrace:
  49. */
  50. static nokprobe_inline int
  51. kmmio_fault(struct pt_regs *regs, unsigned long addr)
  52. {
  53. if (unlikely(is_kmmio_active()))
  54. if (kmmio_handler(regs, addr) == 1)
  55. return -1;
  56. return 0;
  57. }
  58. static nokprobe_inline int kprobes_fault(struct pt_regs *regs)
  59. {
  60. int ret = 0;
  61. /* kprobe_running() needs smp_processor_id() */
  62. if (kprobes_built_in() && !user_mode(regs)) {
  63. preempt_disable();
  64. if (kprobe_running() && kprobe_fault_handler(regs, 14))
  65. ret = 1;
  66. preempt_enable();
  67. }
  68. return ret;
  69. }
  70. /*
  71. * Prefetch quirks:
  72. *
  73. * 32-bit mode:
  74. *
  75. * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
  76. * Check that here and ignore it.
  77. *
  78. * 64-bit mode:
  79. *
  80. * Sometimes the CPU reports invalid exceptions on prefetch.
  81. * Check that here and ignore it.
  82. *
  83. * Opcode checker based on code by Richard Brunner.
  84. */
  85. static inline int
  86. check_prefetch_opcode(struct pt_regs *regs, unsigned char *instr,
  87. unsigned char opcode, int *prefetch)
  88. {
  89. unsigned char instr_hi = opcode & 0xf0;
  90. unsigned char instr_lo = opcode & 0x0f;
  91. switch (instr_hi) {
  92. case 0x20:
  93. case 0x30:
  94. /*
  95. * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
  96. * In X86_64 long mode, the CPU will signal invalid
  97. * opcode if some of these prefixes are present so
  98. * X86_64 will never get here anyway
  99. */
  100. return ((instr_lo & 7) == 0x6);
  101. #ifdef CONFIG_X86_64
  102. case 0x40:
  103. /*
  104. * In AMD64 long mode 0x40..0x4F are valid REX prefixes
  105. * Need to figure out under what instruction mode the
  106. * instruction was issued. Could check the LDT for lm,
  107. * but for now it's good enough to assume that long
  108. * mode only uses well known segments or kernel.
  109. */
  110. return (!user_mode(regs) || user_64bit_mode(regs));
  111. #endif
  112. case 0x60:
  113. /* 0x64 thru 0x67 are valid prefixes in all modes. */
  114. return (instr_lo & 0xC) == 0x4;
  115. case 0xF0:
  116. /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
  117. return !instr_lo || (instr_lo>>1) == 1;
  118. case 0x00:
  119. /* Prefetch instruction is 0x0F0D or 0x0F18 */
  120. if (probe_kernel_address(instr, opcode))
  121. return 0;
  122. *prefetch = (instr_lo == 0xF) &&
  123. (opcode == 0x0D || opcode == 0x18);
  124. return 0;
  125. default:
  126. return 0;
  127. }
  128. }
  129. static int
  130. is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr)
  131. {
  132. unsigned char *max_instr;
  133. unsigned char *instr;
  134. int prefetch = 0;
  135. /*
  136. * If it was a exec (instruction fetch) fault on NX page, then
  137. * do not ignore the fault:
  138. */
  139. if (error_code & PF_INSTR)
  140. return 0;
  141. instr = (void *)convert_ip_to_linear(current, regs);
  142. max_instr = instr + 15;
  143. if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE_MAX)
  144. return 0;
  145. while (instr < max_instr) {
  146. unsigned char opcode;
  147. if (probe_kernel_address(instr, opcode))
  148. break;
  149. instr++;
  150. if (!check_prefetch_opcode(regs, instr, opcode, &prefetch))
  151. break;
  152. }
  153. return prefetch;
  154. }
  155. /*
  156. * A protection key fault means that the PKRU value did not allow
  157. * access to some PTE. Userspace can figure out what PKRU was
  158. * from the XSAVE state, and this function fills out a field in
  159. * siginfo so userspace can discover which protection key was set
  160. * on the PTE.
  161. *
  162. * If we get here, we know that the hardware signaled a PF_PK
  163. * fault and that there was a VMA once we got in the fault
  164. * handler. It does *not* guarantee that the VMA we find here
  165. * was the one that we faulted on.
  166. *
  167. * 1. T1 : mprotect_key(foo, PAGE_SIZE, pkey=4);
  168. * 2. T1 : set PKRU to deny access to pkey=4, touches page
  169. * 3. T1 : faults...
  170. * 4. T2: mprotect_key(foo, PAGE_SIZE, pkey=5);
  171. * 5. T1 : enters fault handler, takes mmap_sem, etc...
  172. * 6. T1 : reaches here, sees vma_pkey(vma)=5, when we really
  173. * faulted on a pte with its pkey=4.
  174. */
  175. static void fill_sig_info_pkey(int si_code, siginfo_t *info,
  176. struct vm_area_struct *vma)
  177. {
  178. /* This is effectively an #ifdef */
  179. if (!boot_cpu_has(X86_FEATURE_OSPKE))
  180. return;
  181. /* Fault not from Protection Keys: nothing to do */
  182. if (si_code != SEGV_PKUERR)
  183. return;
  184. /*
  185. * force_sig_info_fault() is called from a number of
  186. * contexts, some of which have a VMA and some of which
  187. * do not. The PF_PK handing happens after we have a
  188. * valid VMA, so we should never reach this without a
  189. * valid VMA.
  190. */
  191. if (!vma) {
  192. WARN_ONCE(1, "PKU fault with no VMA passed in");
  193. info->si_pkey = 0;
  194. return;
  195. }
  196. /*
  197. * si_pkey should be thought of as a strong hint, but not
  198. * absolutely guranteed to be 100% accurate because of
  199. * the race explained above.
  200. */
  201. info->si_pkey = vma_pkey(vma);
  202. }
  203. static void
  204. force_sig_info_fault(int si_signo, int si_code, unsigned long address,
  205. struct task_struct *tsk, struct vm_area_struct *vma,
  206. int fault)
  207. {
  208. unsigned lsb = 0;
  209. siginfo_t info;
  210. info.si_signo = si_signo;
  211. info.si_errno = 0;
  212. info.si_code = si_code;
  213. info.si_addr = (void __user *)address;
  214. if (fault & VM_FAULT_HWPOISON_LARGE)
  215. lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault));
  216. if (fault & VM_FAULT_HWPOISON)
  217. lsb = PAGE_SHIFT;
  218. info.si_addr_lsb = lsb;
  219. fill_sig_info_pkey(si_code, &info, vma);
  220. force_sig_info(si_signo, &info, tsk);
  221. }
  222. DEFINE_SPINLOCK(pgd_lock);
  223. LIST_HEAD(pgd_list);
  224. #ifdef CONFIG_X86_32
  225. static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
  226. {
  227. unsigned index = pgd_index(address);
  228. pgd_t *pgd_k;
  229. pud_t *pud, *pud_k;
  230. pmd_t *pmd, *pmd_k;
  231. pgd += index;
  232. pgd_k = init_mm.pgd + index;
  233. if (!pgd_present(*pgd_k))
  234. return NULL;
  235. /*
  236. * set_pgd(pgd, *pgd_k); here would be useless on PAE
  237. * and redundant with the set_pmd() on non-PAE. As would
  238. * set_pud.
  239. */
  240. pud = pud_offset(pgd, address);
  241. pud_k = pud_offset(pgd_k, address);
  242. if (!pud_present(*pud_k))
  243. return NULL;
  244. pmd = pmd_offset(pud, address);
  245. pmd_k = pmd_offset(pud_k, address);
  246. if (!pmd_present(*pmd_k))
  247. return NULL;
  248. if (!pmd_present(*pmd))
  249. set_pmd(pmd, *pmd_k);
  250. else
  251. BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
  252. return pmd_k;
  253. }
  254. void vmalloc_sync_all(void)
  255. {
  256. unsigned long address;
  257. if (SHARED_KERNEL_PMD)
  258. return;
  259. for (address = VMALLOC_START & PMD_MASK;
  260. address >= TASK_SIZE_MAX && address < FIXADDR_TOP;
  261. address += PMD_SIZE) {
  262. struct page *page;
  263. spin_lock(&pgd_lock);
  264. list_for_each_entry(page, &pgd_list, lru) {
  265. spinlock_t *pgt_lock;
  266. pmd_t *ret;
  267. /* the pgt_lock only for Xen */
  268. pgt_lock = &pgd_page_get_mm(page)->page_table_lock;
  269. spin_lock(pgt_lock);
  270. ret = vmalloc_sync_one(page_address(page), address);
  271. spin_unlock(pgt_lock);
  272. if (!ret)
  273. break;
  274. }
  275. spin_unlock(&pgd_lock);
  276. }
  277. }
  278. /*
  279. * 32-bit:
  280. *
  281. * Handle a fault on the vmalloc or module mapping area
  282. */
  283. static noinline int vmalloc_fault(unsigned long address)
  284. {
  285. unsigned long pgd_paddr;
  286. pmd_t *pmd_k;
  287. pte_t *pte_k;
  288. /* Make sure we are in vmalloc area: */
  289. if (!(address >= VMALLOC_START && address < VMALLOC_END))
  290. return -1;
  291. WARN_ON_ONCE(in_nmi());
  292. /*
  293. * Synchronize this task's top level page-table
  294. * with the 'reference' page table.
  295. *
  296. * Do _not_ use "current" here. We might be inside
  297. * an interrupt in the middle of a task switch..
  298. */
  299. pgd_paddr = read_cr3();
  300. pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
  301. if (!pmd_k)
  302. return -1;
  303. if (pmd_huge(*pmd_k))
  304. return 0;
  305. pte_k = pte_offset_kernel(pmd_k, address);
  306. if (!pte_present(*pte_k))
  307. return -1;
  308. return 0;
  309. }
  310. NOKPROBE_SYMBOL(vmalloc_fault);
  311. /*
  312. * Did it hit the DOS screen memory VA from vm86 mode?
  313. */
  314. static inline void
  315. check_v8086_mode(struct pt_regs *regs, unsigned long address,
  316. struct task_struct *tsk)
  317. {
  318. #ifdef CONFIG_VM86
  319. unsigned long bit;
  320. if (!v8086_mode(regs) || !tsk->thread.vm86)
  321. return;
  322. bit = (address - 0xA0000) >> PAGE_SHIFT;
  323. if (bit < 32)
  324. tsk->thread.vm86->screen_bitmap |= 1 << bit;
  325. #endif
  326. }
  327. static bool low_pfn(unsigned long pfn)
  328. {
  329. return pfn < max_low_pfn;
  330. }
  331. static void dump_pagetable(unsigned long address)
  332. {
  333. pgd_t *base = __va(read_cr3());
  334. pgd_t *pgd = &base[pgd_index(address)];
  335. pmd_t *pmd;
  336. pte_t *pte;
  337. #ifdef CONFIG_X86_PAE
  338. printk("*pdpt = %016Lx ", pgd_val(*pgd));
  339. if (!low_pfn(pgd_val(*pgd) >> PAGE_SHIFT) || !pgd_present(*pgd))
  340. goto out;
  341. #endif
  342. pmd = pmd_offset(pud_offset(pgd, address), address);
  343. printk(KERN_CONT "*pde = %0*Lx ", sizeof(*pmd) * 2, (u64)pmd_val(*pmd));
  344. /*
  345. * We must not directly access the pte in the highpte
  346. * case if the page table is located in highmem.
  347. * And let's rather not kmap-atomic the pte, just in case
  348. * it's allocated already:
  349. */
  350. if (!low_pfn(pmd_pfn(*pmd)) || !pmd_present(*pmd) || pmd_large(*pmd))
  351. goto out;
  352. pte = pte_offset_kernel(pmd, address);
  353. printk("*pte = %0*Lx ", sizeof(*pte) * 2, (u64)pte_val(*pte));
  354. out:
  355. printk("\n");
  356. }
  357. #else /* CONFIG_X86_64: */
  358. void vmalloc_sync_all(void)
  359. {
  360. sync_global_pgds(VMALLOC_START & PGDIR_MASK, VMALLOC_END);
  361. }
  362. /*
  363. * 64-bit:
  364. *
  365. * Handle a fault on the vmalloc area
  366. */
  367. static noinline int vmalloc_fault(unsigned long address)
  368. {
  369. pgd_t *pgd, *pgd_ref;
  370. pud_t *pud, *pud_ref;
  371. pmd_t *pmd, *pmd_ref;
  372. pte_t *pte, *pte_ref;
  373. /* Make sure we are in vmalloc area: */
  374. if (!(address >= VMALLOC_START && address < VMALLOC_END))
  375. return -1;
  376. WARN_ON_ONCE(in_nmi());
  377. /*
  378. * Copy kernel mappings over when needed. This can also
  379. * happen within a race in page table update. In the later
  380. * case just flush:
  381. */
  382. pgd = (pgd_t *)__va(read_cr3()) + pgd_index(address);
  383. pgd_ref = pgd_offset_k(address);
  384. if (pgd_none(*pgd_ref))
  385. return -1;
  386. if (pgd_none(*pgd)) {
  387. set_pgd(pgd, *pgd_ref);
  388. arch_flush_lazy_mmu_mode();
  389. } else {
  390. BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
  391. }
  392. /*
  393. * Below here mismatches are bugs because these lower tables
  394. * are shared:
  395. */
  396. pud = pud_offset(pgd, address);
  397. pud_ref = pud_offset(pgd_ref, address);
  398. if (pud_none(*pud_ref))
  399. return -1;
  400. if (pud_none(*pud) || pud_pfn(*pud) != pud_pfn(*pud_ref))
  401. BUG();
  402. if (pud_huge(*pud))
  403. return 0;
  404. pmd = pmd_offset(pud, address);
  405. pmd_ref = pmd_offset(pud_ref, address);
  406. if (pmd_none(*pmd_ref))
  407. return -1;
  408. if (pmd_none(*pmd) || pmd_pfn(*pmd) != pmd_pfn(*pmd_ref))
  409. BUG();
  410. if (pmd_huge(*pmd))
  411. return 0;
  412. pte_ref = pte_offset_kernel(pmd_ref, address);
  413. if (!pte_present(*pte_ref))
  414. return -1;
  415. pte = pte_offset_kernel(pmd, address);
  416. /*
  417. * Don't use pte_page here, because the mappings can point
  418. * outside mem_map, and the NUMA hash lookup cannot handle
  419. * that:
  420. */
  421. if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
  422. BUG();
  423. return 0;
  424. }
  425. NOKPROBE_SYMBOL(vmalloc_fault);
  426. #ifdef CONFIG_CPU_SUP_AMD
  427. static const char errata93_warning[] =
  428. KERN_ERR
  429. "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
  430. "******* Working around it, but it may cause SEGVs or burn power.\n"
  431. "******* Please consider a BIOS update.\n"
  432. "******* Disabling USB legacy in the BIOS may also help.\n";
  433. #endif
  434. /*
  435. * No vm86 mode in 64-bit mode:
  436. */
  437. static inline void
  438. check_v8086_mode(struct pt_regs *regs, unsigned long address,
  439. struct task_struct *tsk)
  440. {
  441. }
  442. static int bad_address(void *p)
  443. {
  444. unsigned long dummy;
  445. return probe_kernel_address((unsigned long *)p, dummy);
  446. }
  447. static void dump_pagetable(unsigned long address)
  448. {
  449. pgd_t *base = __va(read_cr3() & PHYSICAL_PAGE_MASK);
  450. pgd_t *pgd = base + pgd_index(address);
  451. pud_t *pud;
  452. pmd_t *pmd;
  453. pte_t *pte;
  454. if (bad_address(pgd))
  455. goto bad;
  456. printk("PGD %lx ", pgd_val(*pgd));
  457. if (!pgd_present(*pgd))
  458. goto out;
  459. pud = pud_offset(pgd, address);
  460. if (bad_address(pud))
  461. goto bad;
  462. printk("PUD %lx ", pud_val(*pud));
  463. if (!pud_present(*pud) || pud_large(*pud))
  464. goto out;
  465. pmd = pmd_offset(pud, address);
  466. if (bad_address(pmd))
  467. goto bad;
  468. printk("PMD %lx ", pmd_val(*pmd));
  469. if (!pmd_present(*pmd) || pmd_large(*pmd))
  470. goto out;
  471. pte = pte_offset_kernel(pmd, address);
  472. if (bad_address(pte))
  473. goto bad;
  474. printk("PTE %lx", pte_val(*pte));
  475. out:
  476. printk("\n");
  477. return;
  478. bad:
  479. printk("BAD\n");
  480. }
  481. #endif /* CONFIG_X86_64 */
  482. /*
  483. * Workaround for K8 erratum #93 & buggy BIOS.
  484. *
  485. * BIOS SMM functions are required to use a specific workaround
  486. * to avoid corruption of the 64bit RIP register on C stepping K8.
  487. *
  488. * A lot of BIOS that didn't get tested properly miss this.
  489. *
  490. * The OS sees this as a page fault with the upper 32bits of RIP cleared.
  491. * Try to work around it here.
  492. *
  493. * Note we only handle faults in kernel here.
  494. * Does nothing on 32-bit.
  495. */
  496. static int is_errata93(struct pt_regs *regs, unsigned long address)
  497. {
  498. #if defined(CONFIG_X86_64) && defined(CONFIG_CPU_SUP_AMD)
  499. if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD
  500. || boot_cpu_data.x86 != 0xf)
  501. return 0;
  502. if (address != regs->ip)
  503. return 0;
  504. if ((address >> 32) != 0)
  505. return 0;
  506. address |= 0xffffffffUL << 32;
  507. if ((address >= (u64)_stext && address <= (u64)_etext) ||
  508. (address >= MODULES_VADDR && address <= MODULES_END)) {
  509. printk_once(errata93_warning);
  510. regs->ip = address;
  511. return 1;
  512. }
  513. #endif
  514. return 0;
  515. }
  516. /*
  517. * Work around K8 erratum #100 K8 in compat mode occasionally jumps
  518. * to illegal addresses >4GB.
  519. *
  520. * We catch this in the page fault handler because these addresses
  521. * are not reachable. Just detect this case and return. Any code
  522. * segment in LDT is compatibility mode.
  523. */
  524. static int is_errata100(struct pt_regs *regs, unsigned long address)
  525. {
  526. #ifdef CONFIG_X86_64
  527. if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32))
  528. return 1;
  529. #endif
  530. return 0;
  531. }
  532. static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
  533. {
  534. #ifdef CONFIG_X86_F00F_BUG
  535. unsigned long nr;
  536. /*
  537. * Pentium F0 0F C7 C8 bug workaround:
  538. */
  539. if (boot_cpu_has_bug(X86_BUG_F00F)) {
  540. nr = (address - idt_descr.address) >> 3;
  541. if (nr == 6) {
  542. do_invalid_op(regs, 0);
  543. return 1;
  544. }
  545. }
  546. #endif
  547. return 0;
  548. }
  549. static const char nx_warning[] = KERN_CRIT
  550. "kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n";
  551. static const char smep_warning[] = KERN_CRIT
  552. "unable to execute userspace code (SMEP?) (uid: %d)\n";
  553. static void
  554. show_fault_oops(struct pt_regs *regs, unsigned long error_code,
  555. unsigned long address)
  556. {
  557. if (!oops_may_print())
  558. return;
  559. if (error_code & PF_INSTR) {
  560. unsigned int level;
  561. pgd_t *pgd;
  562. pte_t *pte;
  563. pgd = __va(read_cr3() & PHYSICAL_PAGE_MASK);
  564. pgd += pgd_index(address);
  565. pte = lookup_address_in_pgd(pgd, address, &level);
  566. if (pte && pte_present(*pte) && !pte_exec(*pte))
  567. printk(nx_warning, from_kuid(&init_user_ns, current_uid()));
  568. if (pte && pte_present(*pte) && pte_exec(*pte) &&
  569. (pgd_flags(*pgd) & _PAGE_USER) &&
  570. (__read_cr4() & X86_CR4_SMEP))
  571. printk(smep_warning, from_kuid(&init_user_ns, current_uid()));
  572. }
  573. printk(KERN_ALERT "BUG: unable to handle kernel ");
  574. if (address < PAGE_SIZE)
  575. printk(KERN_CONT "NULL pointer dereference");
  576. else
  577. printk(KERN_CONT "paging request");
  578. printk(KERN_CONT " at %p\n", (void *) address);
  579. printk(KERN_ALERT "IP: %pS\n", (void *)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. * Read or write was blocked by protection keys. This is
  974. * always an unconditional error and can never result in
  975. * a follow-up action to resolve the fault, like a COW.
  976. */
  977. if (error_code & PF_PK)
  978. return 1;
  979. /*
  980. * Make sure to check the VMA so that we do not perform
  981. * faults just to hit a PF_PK as soon as we fill in a
  982. * page.
  983. */
  984. if (!arch_vma_access_permitted(vma, (error_code & PF_WRITE),
  985. (error_code & PF_INSTR), foreign))
  986. return 1;
  987. if (error_code & PF_WRITE) {
  988. /* write, present and write, not present: */
  989. if (unlikely(!(vma->vm_flags & VM_WRITE)))
  990. return 1;
  991. return 0;
  992. }
  993. /* read, present: */
  994. if (unlikely(error_code & PF_PROT))
  995. return 1;
  996. /* read, not present: */
  997. if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
  998. return 1;
  999. return 0;
  1000. }
  1001. static int fault_in_kernel_space(unsigned long address)
  1002. {
  1003. return address >= TASK_SIZE_MAX;
  1004. }
  1005. static inline bool smap_violation(int error_code, struct pt_regs *regs)
  1006. {
  1007. if (!IS_ENABLED(CONFIG_X86_SMAP))
  1008. return false;
  1009. if (!static_cpu_has(X86_FEATURE_SMAP))
  1010. return false;
  1011. if (error_code & PF_USER)
  1012. return false;
  1013. if (!user_mode(regs) && (regs->flags & X86_EFLAGS_AC))
  1014. return false;
  1015. return true;
  1016. }
  1017. /*
  1018. * This routine handles page faults. It determines the address,
  1019. * and the problem, and then passes it off to one of the appropriate
  1020. * routines.
  1021. *
  1022. * This function must have noinline because both callers
  1023. * {,trace_}do_page_fault() have notrace on. Having this an actual function
  1024. * guarantees there's a function trace entry.
  1025. */
  1026. static noinline void
  1027. __do_page_fault(struct pt_regs *regs, unsigned long error_code,
  1028. unsigned long address)
  1029. {
  1030. struct vm_area_struct *vma;
  1031. struct task_struct *tsk;
  1032. struct mm_struct *mm;
  1033. int fault, major = 0;
  1034. unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  1035. tsk = current;
  1036. mm = tsk->mm;
  1037. /*
  1038. * Detect and handle instructions that would cause a page fault for
  1039. * both a tracked kernel page and a userspace page.
  1040. */
  1041. if (kmemcheck_active(regs))
  1042. kmemcheck_hide(regs);
  1043. prefetchw(&mm->mmap_sem);
  1044. if (unlikely(kmmio_fault(regs, address)))
  1045. return;
  1046. /*
  1047. * We fault-in kernel-space virtual memory on-demand. The
  1048. * 'reference' page table is init_mm.pgd.
  1049. *
  1050. * NOTE! We MUST NOT take any locks for this case. We may
  1051. * be in an interrupt or a critical region, and should
  1052. * only copy the information from the master page table,
  1053. * nothing more.
  1054. *
  1055. * This verifies that the fault happens in kernel space
  1056. * (error_code & 4) == 0, and that the fault was not a
  1057. * protection error (error_code & 9) == 0.
  1058. */
  1059. if (unlikely(fault_in_kernel_space(address))) {
  1060. if (!(error_code & (PF_RSVD | PF_USER | PF_PROT))) {
  1061. if (vmalloc_fault(address) >= 0)
  1062. return;
  1063. if (kmemcheck_fault(regs, address, error_code))
  1064. return;
  1065. }
  1066. /* Can handle a stale RO->RW TLB: */
  1067. if (spurious_fault(error_code, address))
  1068. return;
  1069. /* kprobes don't want to hook the spurious faults: */
  1070. if (kprobes_fault(regs))
  1071. return;
  1072. /*
  1073. * Don't take the mm semaphore here. If we fixup a prefetch
  1074. * fault we could otherwise deadlock:
  1075. */
  1076. bad_area_nosemaphore(regs, error_code, address, NULL);
  1077. return;
  1078. }
  1079. /* kprobes don't want to hook the spurious faults: */
  1080. if (unlikely(kprobes_fault(regs)))
  1081. return;
  1082. if (unlikely(error_code & PF_RSVD))
  1083. pgtable_bad(regs, error_code, address);
  1084. if (unlikely(smap_violation(error_code, regs))) {
  1085. bad_area_nosemaphore(regs, error_code, address, NULL);
  1086. return;
  1087. }
  1088. /*
  1089. * If we're in an interrupt, have no user context or are running
  1090. * in a region with pagefaults disabled then we must not take the fault
  1091. */
  1092. if (unlikely(faulthandler_disabled() || !mm)) {
  1093. bad_area_nosemaphore(regs, error_code, address, NULL);
  1094. return;
  1095. }
  1096. /*
  1097. * It's safe to allow irq's after cr2 has been saved and the
  1098. * vmalloc fault has been handled.
  1099. *
  1100. * User-mode registers count as a user access even for any
  1101. * potential system fault or CPU buglet:
  1102. */
  1103. if (user_mode(regs)) {
  1104. local_irq_enable();
  1105. error_code |= PF_USER;
  1106. flags |= FAULT_FLAG_USER;
  1107. } else {
  1108. if (regs->flags & X86_EFLAGS_IF)
  1109. local_irq_enable();
  1110. }
  1111. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  1112. if (error_code & PF_WRITE)
  1113. flags |= FAULT_FLAG_WRITE;
  1114. if (error_code & PF_INSTR)
  1115. flags |= FAULT_FLAG_INSTRUCTION;
  1116. /*
  1117. * When running in the kernel we expect faults to occur only to
  1118. * addresses in user space. All other faults represent errors in
  1119. * the kernel and should generate an OOPS. Unfortunately, in the
  1120. * case of an erroneous fault occurring in a code path which already
  1121. * holds mmap_sem we will deadlock attempting to validate the fault
  1122. * against the address space. Luckily the kernel only validly
  1123. * references user space from well defined areas of code, which are
  1124. * listed in the exceptions table.
  1125. *
  1126. * As the vast majority of faults will be valid we will only perform
  1127. * the source reference check when there is a possibility of a
  1128. * deadlock. Attempt to lock the address space, if we cannot we then
  1129. * validate the source. If this is invalid we can skip the address
  1130. * space check, thus avoiding the deadlock:
  1131. */
  1132. if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
  1133. if ((error_code & PF_USER) == 0 &&
  1134. !search_exception_tables(regs->ip)) {
  1135. bad_area_nosemaphore(regs, error_code, address, NULL);
  1136. return;
  1137. }
  1138. retry:
  1139. down_read(&mm->mmap_sem);
  1140. } else {
  1141. /*
  1142. * The above down_read_trylock() might have succeeded in
  1143. * which case we'll have missed the might_sleep() from
  1144. * down_read():
  1145. */
  1146. might_sleep();
  1147. }
  1148. vma = find_vma(mm, address);
  1149. if (unlikely(!vma)) {
  1150. bad_area(regs, error_code, address);
  1151. return;
  1152. }
  1153. if (likely(vma->vm_start <= address))
  1154. goto good_area;
  1155. if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
  1156. bad_area(regs, error_code, address);
  1157. return;
  1158. }
  1159. if (error_code & PF_USER) {
  1160. /*
  1161. * Accessing the stack below %sp is always a bug.
  1162. * The large cushion allows instructions like enter
  1163. * and pusha to work. ("enter $65535, $31" pushes
  1164. * 32 pointers and then decrements %sp by 65535.)
  1165. */
  1166. if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
  1167. bad_area(regs, error_code, address);
  1168. return;
  1169. }
  1170. }
  1171. if (unlikely(expand_stack(vma, address))) {
  1172. bad_area(regs, error_code, address);
  1173. return;
  1174. }
  1175. /*
  1176. * Ok, we have a good vm_area for this memory access, so
  1177. * we can handle it..
  1178. */
  1179. good_area:
  1180. if (unlikely(access_error(error_code, vma))) {
  1181. bad_area_access_error(regs, error_code, address, vma);
  1182. return;
  1183. }
  1184. /*
  1185. * If for any reason at all we couldn't handle the fault,
  1186. * make sure we exit gracefully rather than endlessly redo
  1187. * the fault. Since we never set FAULT_FLAG_RETRY_NOWAIT, if
  1188. * we get VM_FAULT_RETRY back, the mmap_sem has been unlocked.
  1189. */
  1190. fault = handle_mm_fault(vma, address, flags);
  1191. major |= fault & VM_FAULT_MAJOR;
  1192. /*
  1193. * If we need to retry the mmap_sem has already been released,
  1194. * and if there is a fatal signal pending there is no guarantee
  1195. * that we made any progress. Handle this case first.
  1196. */
  1197. if (unlikely(fault & VM_FAULT_RETRY)) {
  1198. /* Retry at most once */
  1199. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  1200. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  1201. flags |= FAULT_FLAG_TRIED;
  1202. if (!fatal_signal_pending(tsk))
  1203. goto retry;
  1204. }
  1205. /* User mode? Just return to handle the fatal exception */
  1206. if (flags & FAULT_FLAG_USER)
  1207. return;
  1208. /* Not returning to user mode? Handle exceptions or die: */
  1209. no_context(regs, error_code, address, SIGBUS, BUS_ADRERR);
  1210. return;
  1211. }
  1212. up_read(&mm->mmap_sem);
  1213. if (unlikely(fault & VM_FAULT_ERROR)) {
  1214. mm_fault_error(regs, error_code, address, vma, fault);
  1215. return;
  1216. }
  1217. /*
  1218. * Major/minor page fault accounting. If any of the events
  1219. * returned VM_FAULT_MAJOR, we account it as a major fault.
  1220. */
  1221. if (major) {
  1222. tsk->maj_flt++;
  1223. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address);
  1224. } else {
  1225. tsk->min_flt++;
  1226. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address);
  1227. }
  1228. check_v8086_mode(regs, address, tsk);
  1229. }
  1230. NOKPROBE_SYMBOL(__do_page_fault);
  1231. dotraplinkage void notrace
  1232. do_page_fault(struct pt_regs *regs, unsigned long error_code)
  1233. {
  1234. unsigned long address = read_cr2(); /* Get the faulting address */
  1235. enum ctx_state prev_state;
  1236. /*
  1237. * We must have this function tagged with __kprobes, notrace and call
  1238. * read_cr2() before calling anything else. To avoid calling any kind
  1239. * of tracing machinery before we've observed the CR2 value.
  1240. *
  1241. * exception_{enter,exit}() contain all sorts of tracepoints.
  1242. */
  1243. prev_state = exception_enter();
  1244. __do_page_fault(regs, error_code, address);
  1245. exception_exit(prev_state);
  1246. }
  1247. NOKPROBE_SYMBOL(do_page_fault);
  1248. #ifdef CONFIG_TRACING
  1249. static nokprobe_inline void
  1250. trace_page_fault_entries(unsigned long address, struct pt_regs *regs,
  1251. unsigned long error_code)
  1252. {
  1253. if (user_mode(regs))
  1254. trace_page_fault_user(address, regs, error_code);
  1255. else
  1256. trace_page_fault_kernel(address, regs, error_code);
  1257. }
  1258. dotraplinkage void notrace
  1259. trace_do_page_fault(struct pt_regs *regs, unsigned long error_code)
  1260. {
  1261. /*
  1262. * The exception_enter and tracepoint processing could
  1263. * trigger another page faults (user space callchain
  1264. * reading) and destroy the original cr2 value, so read
  1265. * the faulting address now.
  1266. */
  1267. unsigned long address = read_cr2();
  1268. enum ctx_state prev_state;
  1269. prev_state = exception_enter();
  1270. trace_page_fault_entries(address, regs, error_code);
  1271. __do_page_fault(regs, error_code, address);
  1272. exception_exit(prev_state);
  1273. }
  1274. NOKPROBE_SYMBOL(trace_do_page_fault);
  1275. #endif /* CONFIG_TRACING */