fault.c 39 KB

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