fault.c 32 KB

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