fault.c 27 KB

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