fault.c 31 KB

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