fault.c 32 KB

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