fault.c 38 KB

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