fault.c 32 KB

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