fault.c 29 KB

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