fault.c 27 KB

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