fault.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  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/rcu.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. /* TODO: fixup for "mm-invoke-oom-killer-from-page-fault.patch" */
  669. static void
  670. out_of_memory(struct pt_regs *regs, unsigned long error_code,
  671. unsigned long address)
  672. {
  673. /*
  674. * We ran out of memory, call the OOM killer, and return the userspace
  675. * (which will retry the fault, or kill us if we got oom-killed):
  676. */
  677. up_read(&current->mm->mmap_sem);
  678. pagefault_out_of_memory();
  679. }
  680. static void
  681. do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address,
  682. unsigned int fault)
  683. {
  684. struct task_struct *tsk = current;
  685. struct mm_struct *mm = tsk->mm;
  686. int code = BUS_ADRERR;
  687. up_read(&mm->mmap_sem);
  688. /* Kernel mode? Handle exceptions or die: */
  689. if (!(error_code & PF_USER)) {
  690. no_context(regs, error_code, address, SIGBUS, BUS_ADRERR);
  691. return;
  692. }
  693. /* User-space => ok to do another page fault: */
  694. if (is_prefetch(regs, error_code, address))
  695. return;
  696. tsk->thread.cr2 = address;
  697. tsk->thread.error_code = error_code;
  698. tsk->thread.trap_nr = X86_TRAP_PF;
  699. #ifdef CONFIG_MEMORY_FAILURE
  700. if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
  701. printk(KERN_ERR
  702. "MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n",
  703. tsk->comm, tsk->pid, address);
  704. code = BUS_MCEERR_AR;
  705. }
  706. #endif
  707. force_sig_info_fault(SIGBUS, code, address, tsk, fault);
  708. }
  709. static noinline int
  710. mm_fault_error(struct pt_regs *regs, unsigned long error_code,
  711. unsigned long address, unsigned int fault)
  712. {
  713. /*
  714. * Pagefault was interrupted by SIGKILL. We have no reason to
  715. * continue pagefault.
  716. */
  717. if (fatal_signal_pending(current)) {
  718. if (!(fault & VM_FAULT_RETRY))
  719. up_read(&current->mm->mmap_sem);
  720. if (!(error_code & PF_USER))
  721. no_context(regs, error_code, address, 0, 0);
  722. return 1;
  723. }
  724. if (!(fault & VM_FAULT_ERROR))
  725. return 0;
  726. if (fault & VM_FAULT_OOM) {
  727. /* Kernel mode? Handle exceptions or die: */
  728. if (!(error_code & PF_USER)) {
  729. up_read(&current->mm->mmap_sem);
  730. no_context(regs, error_code, address,
  731. SIGSEGV, SEGV_MAPERR);
  732. return 1;
  733. }
  734. out_of_memory(regs, error_code, address);
  735. } else {
  736. if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|
  737. VM_FAULT_HWPOISON_LARGE))
  738. do_sigbus(regs, error_code, address, fault);
  739. else
  740. BUG();
  741. }
  742. return 1;
  743. }
  744. static int spurious_fault_check(unsigned long error_code, pte_t *pte)
  745. {
  746. if ((error_code & PF_WRITE) && !pte_write(*pte))
  747. return 0;
  748. if ((error_code & PF_INSTR) && !pte_exec(*pte))
  749. return 0;
  750. return 1;
  751. }
  752. /*
  753. * Handle a spurious fault caused by a stale TLB entry.
  754. *
  755. * This allows us to lazily refresh the TLB when increasing the
  756. * permissions of a kernel page (RO -> RW or NX -> X). Doing it
  757. * eagerly is very expensive since that implies doing a full
  758. * cross-processor TLB flush, even if no stale TLB entries exist
  759. * on other processors.
  760. *
  761. * There are no security implications to leaving a stale TLB when
  762. * increasing the permissions on a page.
  763. */
  764. static noinline __kprobes int
  765. spurious_fault(unsigned long error_code, unsigned long address)
  766. {
  767. pgd_t *pgd;
  768. pud_t *pud;
  769. pmd_t *pmd;
  770. pte_t *pte;
  771. int ret;
  772. /* Reserved-bit violation or user access to kernel space? */
  773. if (error_code & (PF_USER | PF_RSVD))
  774. return 0;
  775. pgd = init_mm.pgd + pgd_index(address);
  776. if (!pgd_present(*pgd))
  777. return 0;
  778. pud = pud_offset(pgd, address);
  779. if (!pud_present(*pud))
  780. return 0;
  781. if (pud_large(*pud))
  782. return spurious_fault_check(error_code, (pte_t *) pud);
  783. pmd = pmd_offset(pud, address);
  784. if (!pmd_present(*pmd))
  785. return 0;
  786. if (pmd_large(*pmd))
  787. return spurious_fault_check(error_code, (pte_t *) pmd);
  788. /*
  789. * Note: don't use pte_present() here, since it returns true
  790. * if the _PAGE_PROTNONE bit is set. However, this aliases the
  791. * _PAGE_GLOBAL bit, which for kernel pages give false positives
  792. * when CONFIG_DEBUG_PAGEALLOC is used.
  793. */
  794. pte = pte_offset_kernel(pmd, address);
  795. if (!(pte_flags(*pte) & _PAGE_PRESENT))
  796. return 0;
  797. ret = spurious_fault_check(error_code, pte);
  798. if (!ret)
  799. return 0;
  800. /*
  801. * Make sure we have permissions in PMD.
  802. * If not, then there's a bug in the page tables:
  803. */
  804. ret = spurious_fault_check(error_code, (pte_t *) pmd);
  805. WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
  806. return ret;
  807. }
  808. int show_unhandled_signals = 1;
  809. static inline int
  810. access_error(unsigned long error_code, struct vm_area_struct *vma)
  811. {
  812. if (error_code & PF_WRITE) {
  813. /* write, present and write, not present: */
  814. if (unlikely(!(vma->vm_flags & VM_WRITE)))
  815. return 1;
  816. return 0;
  817. }
  818. /* read, present: */
  819. if (unlikely(error_code & PF_PROT))
  820. return 1;
  821. /* read, not present: */
  822. if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
  823. return 1;
  824. return 0;
  825. }
  826. static int fault_in_kernel_space(unsigned long address)
  827. {
  828. return address >= TASK_SIZE_MAX;
  829. }
  830. /*
  831. * This routine handles page faults. It determines the address,
  832. * and the problem, and then passes it off to one of the appropriate
  833. * routines.
  834. */
  835. static void __kprobes
  836. __do_page_fault(struct pt_regs *regs, unsigned long error_code)
  837. {
  838. struct vm_area_struct *vma;
  839. struct task_struct *tsk;
  840. unsigned long address;
  841. struct mm_struct *mm;
  842. int fault;
  843. int write = error_code & PF_WRITE;
  844. unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE |
  845. (write ? FAULT_FLAG_WRITE : 0);
  846. tsk = current;
  847. mm = tsk->mm;
  848. /* Get the faulting address: */
  849. address = read_cr2();
  850. /*
  851. * Detect and handle instructions that would cause a page fault for
  852. * both a tracked kernel page and a userspace page.
  853. */
  854. if (kmemcheck_active(regs))
  855. kmemcheck_hide(regs);
  856. prefetchw(&mm->mmap_sem);
  857. if (unlikely(kmmio_fault(regs, address)))
  858. return;
  859. /*
  860. * We fault-in kernel-space virtual memory on-demand. The
  861. * 'reference' page table is init_mm.pgd.
  862. *
  863. * NOTE! We MUST NOT take any locks for this case. We may
  864. * be in an interrupt or a critical region, and should
  865. * only copy the information from the master page table,
  866. * nothing more.
  867. *
  868. * This verifies that the fault happens in kernel space
  869. * (error_code & 4) == 0, and that the fault was not a
  870. * protection error (error_code & 9) == 0.
  871. */
  872. if (unlikely(fault_in_kernel_space(address))) {
  873. if (!(error_code & (PF_RSVD | PF_USER | PF_PROT))) {
  874. if (vmalloc_fault(address) >= 0)
  875. return;
  876. if (kmemcheck_fault(regs, address, error_code))
  877. return;
  878. }
  879. /* Can handle a stale RO->RW TLB: */
  880. if (spurious_fault(error_code, address))
  881. return;
  882. /* kprobes don't want to hook the spurious faults: */
  883. if (notify_page_fault(regs))
  884. return;
  885. /*
  886. * Don't take the mm semaphore here. If we fixup a prefetch
  887. * fault we could otherwise deadlock:
  888. */
  889. bad_area_nosemaphore(regs, error_code, address);
  890. return;
  891. }
  892. /* kprobes don't want to hook the spurious faults: */
  893. if (unlikely(notify_page_fault(regs)))
  894. return;
  895. /*
  896. * It's safe to allow irq's after cr2 has been saved and the
  897. * vmalloc fault has been handled.
  898. *
  899. * User-mode registers count as a user access even for any
  900. * potential system fault or CPU buglet:
  901. */
  902. if (user_mode_vm(regs)) {
  903. local_irq_enable();
  904. error_code |= PF_USER;
  905. } else {
  906. if (regs->flags & X86_EFLAGS_IF)
  907. local_irq_enable();
  908. }
  909. if (unlikely(error_code & PF_RSVD))
  910. pgtable_bad(regs, error_code, address);
  911. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  912. /*
  913. * If we're in an interrupt, have no user context or are running
  914. * in an atomic region then we must not take the fault:
  915. */
  916. if (unlikely(in_atomic() || !mm)) {
  917. bad_area_nosemaphore(regs, error_code, address);
  918. return;
  919. }
  920. /*
  921. * When running in the kernel we expect faults to occur only to
  922. * addresses in user space. All other faults represent errors in
  923. * the kernel and should generate an OOPS. Unfortunately, in the
  924. * case of an erroneous fault occurring in a code path which already
  925. * holds mmap_sem we will deadlock attempting to validate the fault
  926. * against the address space. Luckily the kernel only validly
  927. * references user space from well defined areas of code, which are
  928. * listed in the exceptions table.
  929. *
  930. * As the vast majority of faults will be valid we will only perform
  931. * the source reference check when there is a possibility of a
  932. * deadlock. Attempt to lock the address space, if we cannot we then
  933. * validate the source. If this is invalid we can skip the address
  934. * space check, thus avoiding the deadlock:
  935. */
  936. if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
  937. if ((error_code & PF_USER) == 0 &&
  938. !search_exception_tables(regs->ip)) {
  939. bad_area_nosemaphore(regs, error_code, address);
  940. return;
  941. }
  942. retry:
  943. down_read(&mm->mmap_sem);
  944. } else {
  945. /*
  946. * The above down_read_trylock() might have succeeded in
  947. * which case we'll have missed the might_sleep() from
  948. * down_read():
  949. */
  950. might_sleep();
  951. }
  952. vma = find_vma(mm, address);
  953. if (unlikely(!vma)) {
  954. bad_area(regs, error_code, address);
  955. return;
  956. }
  957. if (likely(vma->vm_start <= address))
  958. goto good_area;
  959. if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
  960. bad_area(regs, error_code, address);
  961. return;
  962. }
  963. if (error_code & PF_USER) {
  964. /*
  965. * Accessing the stack below %sp is always a bug.
  966. * The large cushion allows instructions like enter
  967. * and pusha to work. ("enter $65535, $31" pushes
  968. * 32 pointers and then decrements %sp by 65535.)
  969. */
  970. if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
  971. bad_area(regs, error_code, address);
  972. return;
  973. }
  974. }
  975. if (unlikely(expand_stack(vma, address))) {
  976. bad_area(regs, error_code, address);
  977. return;
  978. }
  979. /*
  980. * Ok, we have a good vm_area for this memory access, so
  981. * we can handle it..
  982. */
  983. good_area:
  984. if (unlikely(access_error(error_code, vma))) {
  985. bad_area_access_error(regs, error_code, address);
  986. return;
  987. }
  988. /*
  989. * If for any reason at all we couldn't handle the fault,
  990. * make sure we exit gracefully rather than endlessly redo
  991. * the fault:
  992. */
  993. fault = handle_mm_fault(mm, vma, address, flags);
  994. if (unlikely(fault & (VM_FAULT_RETRY|VM_FAULT_ERROR))) {
  995. if (mm_fault_error(regs, error_code, address, fault))
  996. return;
  997. }
  998. /*
  999. * Major/minor page fault accounting is only done on the
  1000. * initial attempt. If we go through a retry, it is extremely
  1001. * likely that the page will be found in page cache at that point.
  1002. */
  1003. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  1004. if (fault & VM_FAULT_MAJOR) {
  1005. tsk->maj_flt++;
  1006. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
  1007. regs, address);
  1008. } else {
  1009. tsk->min_flt++;
  1010. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
  1011. regs, address);
  1012. }
  1013. if (fault & VM_FAULT_RETRY) {
  1014. /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
  1015. * of starvation. */
  1016. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  1017. goto retry;
  1018. }
  1019. }
  1020. check_v8086_mode(regs, address, tsk);
  1021. up_read(&mm->mmap_sem);
  1022. }
  1023. dotraplinkage void __kprobes
  1024. do_page_fault(struct pt_regs *regs, unsigned long error_code)
  1025. {
  1026. exception_enter(regs);
  1027. __do_page_fault(regs, error_code);
  1028. exception_exit(regs);
  1029. }