fault.c 29 KB

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