fault.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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_counter.h> /* perf_swcounter_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. force_sig_info(si_signo, &info, tsk);
  151. }
  152. DEFINE_SPINLOCK(pgd_lock);
  153. LIST_HEAD(pgd_list);
  154. #ifdef CONFIG_X86_32
  155. static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
  156. {
  157. unsigned index = pgd_index(address);
  158. pgd_t *pgd_k;
  159. pud_t *pud, *pud_k;
  160. pmd_t *pmd, *pmd_k;
  161. pgd += index;
  162. pgd_k = init_mm.pgd + index;
  163. if (!pgd_present(*pgd_k))
  164. return NULL;
  165. /*
  166. * set_pgd(pgd, *pgd_k); here would be useless on PAE
  167. * and redundant with the set_pmd() on non-PAE. As would
  168. * set_pud.
  169. */
  170. pud = pud_offset(pgd, address);
  171. pud_k = pud_offset(pgd_k, address);
  172. if (!pud_present(*pud_k))
  173. return NULL;
  174. pmd = pmd_offset(pud, address);
  175. pmd_k = pmd_offset(pud_k, address);
  176. if (!pmd_present(*pmd_k))
  177. return NULL;
  178. if (!pmd_present(*pmd))
  179. set_pmd(pmd, *pmd_k);
  180. else
  181. BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
  182. return pmd_k;
  183. }
  184. void vmalloc_sync_all(void)
  185. {
  186. unsigned long address;
  187. if (SHARED_KERNEL_PMD)
  188. return;
  189. for (address = VMALLOC_START & PMD_MASK;
  190. address >= TASK_SIZE && address < FIXADDR_TOP;
  191. address += PMD_SIZE) {
  192. unsigned long flags;
  193. struct page *page;
  194. spin_lock_irqsave(&pgd_lock, flags);
  195. list_for_each_entry(page, &pgd_list, lru) {
  196. if (!vmalloc_sync_one(page_address(page), address))
  197. break;
  198. }
  199. spin_unlock_irqrestore(&pgd_lock, flags);
  200. }
  201. }
  202. /*
  203. * 32-bit:
  204. *
  205. * Handle a fault on the vmalloc or module mapping area
  206. */
  207. static noinline int vmalloc_fault(unsigned long address)
  208. {
  209. unsigned long pgd_paddr;
  210. pmd_t *pmd_k;
  211. pte_t *pte_k;
  212. /* Make sure we are in vmalloc area: */
  213. if (!(address >= VMALLOC_START && address < VMALLOC_END))
  214. return -1;
  215. /*
  216. * Synchronize this task's top level page-table
  217. * with the 'reference' page table.
  218. *
  219. * Do _not_ use "current" here. We might be inside
  220. * an interrupt in the middle of a task switch..
  221. */
  222. pgd_paddr = read_cr3();
  223. pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
  224. if (!pmd_k)
  225. return -1;
  226. pte_k = pte_offset_kernel(pmd_k, address);
  227. if (!pte_present(*pte_k))
  228. return -1;
  229. return 0;
  230. }
  231. /*
  232. * Did it hit the DOS screen memory VA from vm86 mode?
  233. */
  234. static inline void
  235. check_v8086_mode(struct pt_regs *regs, unsigned long address,
  236. struct task_struct *tsk)
  237. {
  238. unsigned long bit;
  239. if (!v8086_mode(regs))
  240. return;
  241. bit = (address - 0xA0000) >> PAGE_SHIFT;
  242. if (bit < 32)
  243. tsk->thread.screen_bitmap |= 1 << bit;
  244. }
  245. static void dump_pagetable(unsigned long address)
  246. {
  247. __typeof__(pte_val(__pte(0))) page;
  248. page = read_cr3();
  249. page = ((__typeof__(page) *) __va(page))[address >> PGDIR_SHIFT];
  250. #ifdef CONFIG_X86_PAE
  251. printk("*pdpt = %016Lx ", page);
  252. if ((page >> PAGE_SHIFT) < max_low_pfn
  253. && page & _PAGE_PRESENT) {
  254. page &= PAGE_MASK;
  255. page = ((__typeof__(page) *) __va(page))[(address >> PMD_SHIFT)
  256. & (PTRS_PER_PMD - 1)];
  257. printk(KERN_CONT "*pde = %016Lx ", page);
  258. page &= ~_PAGE_NX;
  259. }
  260. #else
  261. printk("*pde = %08lx ", page);
  262. #endif
  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 ((page >> PAGE_SHIFT) < max_low_pfn
  270. && (page & _PAGE_PRESENT)
  271. && !(page & _PAGE_PSE)) {
  272. page &= PAGE_MASK;
  273. page = ((__typeof__(page) *) __va(page))[(address >> PAGE_SHIFT)
  274. & (PTRS_PER_PTE - 1)];
  275. printk("*pte = %0*Lx ", sizeof(page)*2, (u64)page);
  276. }
  277. printk("\n");
  278. }
  279. #else /* CONFIG_X86_64: */
  280. void vmalloc_sync_all(void)
  281. {
  282. unsigned long address;
  283. for (address = VMALLOC_START & PGDIR_MASK; address <= VMALLOC_END;
  284. address += PGDIR_SIZE) {
  285. const pgd_t *pgd_ref = pgd_offset_k(address);
  286. unsigned long flags;
  287. struct page *page;
  288. if (pgd_none(*pgd_ref))
  289. continue;
  290. spin_lock_irqsave(&pgd_lock, flags);
  291. list_for_each_entry(page, &pgd_list, lru) {
  292. pgd_t *pgd;
  293. pgd = (pgd_t *)page_address(page) + pgd_index(address);
  294. if (pgd_none(*pgd))
  295. set_pgd(pgd, *pgd_ref);
  296. else
  297. BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
  298. }
  299. spin_unlock_irqrestore(&pgd_lock, flags);
  300. }
  301. }
  302. /*
  303. * 64-bit:
  304. *
  305. * Handle a fault on the vmalloc area
  306. *
  307. * This assumes no large pages in there.
  308. */
  309. static noinline int vmalloc_fault(unsigned long address)
  310. {
  311. pgd_t *pgd, *pgd_ref;
  312. pud_t *pud, *pud_ref;
  313. pmd_t *pmd, *pmd_ref;
  314. pte_t *pte, *pte_ref;
  315. /* Make sure we are in vmalloc area: */
  316. if (!(address >= VMALLOC_START && address < VMALLOC_END))
  317. return -1;
  318. /*
  319. * Copy kernel mappings over when needed. This can also
  320. * happen within a race in page table update. In the later
  321. * case just flush:
  322. */
  323. pgd = pgd_offset(current->active_mm, address);
  324. pgd_ref = pgd_offset_k(address);
  325. if (pgd_none(*pgd_ref))
  326. return -1;
  327. if (pgd_none(*pgd))
  328. set_pgd(pgd, *pgd_ref);
  329. else
  330. BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
  331. /*
  332. * Below here mismatches are bugs because these lower tables
  333. * are shared:
  334. */
  335. pud = pud_offset(pgd, address);
  336. pud_ref = pud_offset(pgd_ref, address);
  337. if (pud_none(*pud_ref))
  338. return -1;
  339. if (pud_none(*pud) || pud_page_vaddr(*pud) != pud_page_vaddr(*pud_ref))
  340. BUG();
  341. pmd = pmd_offset(pud, address);
  342. pmd_ref = pmd_offset(pud_ref, address);
  343. if (pmd_none(*pmd_ref))
  344. return -1;
  345. if (pmd_none(*pmd) || pmd_page(*pmd) != pmd_page(*pmd_ref))
  346. BUG();
  347. pte_ref = pte_offset_kernel(pmd_ref, address);
  348. if (!pte_present(*pte_ref))
  349. return -1;
  350. pte = pte_offset_kernel(pmd, address);
  351. /*
  352. * Don't use pte_page here, because the mappings can point
  353. * outside mem_map, and the NUMA hash lookup cannot handle
  354. * that:
  355. */
  356. if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
  357. BUG();
  358. return 0;
  359. }
  360. static const char errata93_warning[] =
  361. KERN_ERR "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
  362. KERN_ERR "******* Working around it, but it may cause SEGVs or burn power.\n"
  363. KERN_ERR "******* Please consider a BIOS update.\n"
  364. KERN_ERR "******* Disabling USB legacy in the BIOS may also help.\n";
  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 *pgd;
  381. pud_t *pud;
  382. pmd_t *pmd;
  383. pte_t *pte;
  384. pgd = (pgd_t *)read_cr3();
  385. pgd = __va((unsigned long)pgd & PHYSICAL_PAGE_MASK);
  386. pgd += pgd_index(address);
  387. if (bad_address(pgd))
  388. goto bad;
  389. printk("PGD %lx ", pgd_val(*pgd));
  390. if (!pgd_present(*pgd))
  391. goto out;
  392. pud = pud_offset(pgd, address);
  393. if (bad_address(pud))
  394. goto bad;
  395. printk("PUD %lx ", pud_val(*pud));
  396. if (!pud_present(*pud) || pud_large(*pud))
  397. goto out;
  398. pmd = pmd_offset(pud, address);
  399. if (bad_address(pmd))
  400. goto bad;
  401. printk("PMD %lx ", pmd_val(*pmd));
  402. if (!pmd_present(*pmd) || pmd_large(*pmd))
  403. goto out;
  404. pte = pte_offset_kernel(pmd, address);
  405. if (bad_address(pte))
  406. goto bad;
  407. printk("PTE %lx", pte_val(*pte));
  408. out:
  409. printk("\n");
  410. return;
  411. bad:
  412. printk("BAD\n");
  413. }
  414. #endif /* CONFIG_X86_64 */
  415. /*
  416. * Workaround for K8 erratum #93 & buggy BIOS.
  417. *
  418. * BIOS SMM functions are required to use a specific workaround
  419. * to avoid corruption of the 64bit RIP register on C stepping K8.
  420. *
  421. * A lot of BIOS that didn't get tested properly miss this.
  422. *
  423. * The OS sees this as a page fault with the upper 32bits of RIP cleared.
  424. * Try to work around it here.
  425. *
  426. * Note we only handle faults in kernel here.
  427. * Does nothing on 32-bit.
  428. */
  429. static int is_errata93(struct pt_regs *regs, unsigned long address)
  430. {
  431. #ifdef CONFIG_X86_64
  432. if (address != regs->ip)
  433. return 0;
  434. if ((address >> 32) != 0)
  435. return 0;
  436. address |= 0xffffffffUL << 32;
  437. if ((address >= (u64)_stext && address <= (u64)_etext) ||
  438. (address >= MODULES_VADDR && address <= MODULES_END)) {
  439. printk_once(errata93_warning);
  440. regs->ip = address;
  441. return 1;
  442. }
  443. #endif
  444. return 0;
  445. }
  446. /*
  447. * Work around K8 erratum #100 K8 in compat mode occasionally jumps
  448. * to illegal addresses >4GB.
  449. *
  450. * We catch this in the page fault handler because these addresses
  451. * are not reachable. Just detect this case and return. Any code
  452. * segment in LDT is compatibility mode.
  453. */
  454. static int is_errata100(struct pt_regs *regs, unsigned long address)
  455. {
  456. #ifdef CONFIG_X86_64
  457. if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32))
  458. return 1;
  459. #endif
  460. return 0;
  461. }
  462. static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
  463. {
  464. #ifdef CONFIG_X86_F00F_BUG
  465. unsigned long nr;
  466. /*
  467. * Pentium F0 0F C7 C8 bug workaround:
  468. */
  469. if (boot_cpu_data.f00f_bug) {
  470. nr = (address - idt_descr.address) >> 3;
  471. if (nr == 6) {
  472. do_invalid_op(regs, 0);
  473. return 1;
  474. }
  475. }
  476. #endif
  477. return 0;
  478. }
  479. static const char nx_warning[] = KERN_CRIT
  480. "kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n";
  481. static void
  482. show_fault_oops(struct pt_regs *regs, unsigned long error_code,
  483. unsigned long address)
  484. {
  485. if (!oops_may_print())
  486. return;
  487. if (error_code & PF_INSTR) {
  488. unsigned int level;
  489. pte_t *pte = lookup_address(address, &level);
  490. if (pte && pte_present(*pte) && !pte_exec(*pte))
  491. printk(nx_warning, current_uid());
  492. }
  493. printk(KERN_ALERT "BUG: unable to handle kernel ");
  494. if (address < PAGE_SIZE)
  495. printk(KERN_CONT "NULL pointer dereference");
  496. else
  497. printk(KERN_CONT "paging request");
  498. printk(KERN_CONT " at %p\n", (void *) address);
  499. printk(KERN_ALERT "IP:");
  500. printk_address(regs->ip, 1);
  501. dump_pagetable(address);
  502. }
  503. static noinline void
  504. pgtable_bad(struct pt_regs *regs, unsigned long error_code,
  505. unsigned long address)
  506. {
  507. struct task_struct *tsk;
  508. unsigned long flags;
  509. int sig;
  510. flags = oops_begin();
  511. tsk = current;
  512. sig = SIGKILL;
  513. printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
  514. tsk->comm, address);
  515. dump_pagetable(address);
  516. tsk->thread.cr2 = address;
  517. tsk->thread.trap_no = 14;
  518. tsk->thread.error_code = error_code;
  519. if (__die("Bad pagetable", regs, error_code))
  520. sig = 0;
  521. oops_end(flags, regs, sig);
  522. }
  523. static noinline void
  524. no_context(struct pt_regs *regs, unsigned long error_code,
  525. unsigned long address)
  526. {
  527. struct task_struct *tsk = current;
  528. unsigned long *stackend;
  529. unsigned long flags;
  530. int sig;
  531. /* Are we prepared to handle this kernel fault? */
  532. if (fixup_exception(regs))
  533. return;
  534. /*
  535. * 32-bit:
  536. *
  537. * Valid to do another page fault here, because if this fault
  538. * had been triggered by is_prefetch fixup_exception would have
  539. * handled it.
  540. *
  541. * 64-bit:
  542. *
  543. * Hall of shame of CPU/BIOS bugs.
  544. */
  545. if (is_prefetch(regs, error_code, address))
  546. return;
  547. if (is_errata93(regs, address))
  548. return;
  549. /*
  550. * Oops. The kernel tried to access some bad page. We'll have to
  551. * terminate things with extreme prejudice:
  552. */
  553. flags = oops_begin();
  554. show_fault_oops(regs, error_code, address);
  555. stackend = end_of_stack(tsk);
  556. if (*stackend != STACK_END_MAGIC)
  557. printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
  558. tsk->thread.cr2 = address;
  559. tsk->thread.trap_no = 14;
  560. tsk->thread.error_code = error_code;
  561. sig = SIGKILL;
  562. if (__die("Oops", regs, error_code))
  563. sig = 0;
  564. /* Executive summary in case the body of the oops scrolled away */
  565. printk(KERN_EMERG "CR2: %016lx\n", address);
  566. oops_end(flags, regs, sig);
  567. }
  568. /*
  569. * Print out info about fatal segfaults, if the show_unhandled_signals
  570. * sysctl is set:
  571. */
  572. static inline void
  573. show_signal_msg(struct pt_regs *regs, unsigned long error_code,
  574. unsigned long address, struct task_struct *tsk)
  575. {
  576. if (!unhandled_signal(tsk, SIGSEGV))
  577. return;
  578. if (!printk_ratelimit())
  579. return;
  580. printk(KERN_CONT "%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
  581. task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
  582. tsk->comm, task_pid_nr(tsk), address,
  583. (void *)regs->ip, (void *)regs->sp, error_code);
  584. print_vma_addr(KERN_CONT " in ", regs->ip);
  585. printk(KERN_CONT "\n");
  586. }
  587. static void
  588. __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
  589. unsigned long address, int si_code)
  590. {
  591. struct task_struct *tsk = current;
  592. /* User mode accesses just cause a SIGSEGV */
  593. if (error_code & PF_USER) {
  594. /*
  595. * It's possible to have interrupts off here:
  596. */
  597. local_irq_enable();
  598. /*
  599. * Valid to do another page fault here because this one came
  600. * from user space:
  601. */
  602. if (is_prefetch(regs, error_code, address))
  603. return;
  604. if (is_errata100(regs, address))
  605. return;
  606. if (unlikely(show_unhandled_signals))
  607. show_signal_msg(regs, error_code, address, tsk);
  608. /* Kernel addresses are always protection faults: */
  609. tsk->thread.cr2 = address;
  610. tsk->thread.error_code = error_code | (address >= TASK_SIZE);
  611. tsk->thread.trap_no = 14;
  612. force_sig_info_fault(SIGSEGV, si_code, address, tsk);
  613. return;
  614. }
  615. if (is_f00f_bug(regs, address))
  616. return;
  617. no_context(regs, error_code, address);
  618. }
  619. static noinline void
  620. bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
  621. unsigned long address)
  622. {
  623. __bad_area_nosemaphore(regs, error_code, address, SEGV_MAPERR);
  624. }
  625. static void
  626. __bad_area(struct pt_regs *regs, unsigned long error_code,
  627. unsigned long address, int si_code)
  628. {
  629. struct mm_struct *mm = current->mm;
  630. /*
  631. * Something tried to access memory that isn't in our memory map..
  632. * Fix it, but check if it's kernel or user first..
  633. */
  634. up_read(&mm->mmap_sem);
  635. __bad_area_nosemaphore(regs, error_code, address, si_code);
  636. }
  637. static noinline void
  638. bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
  639. {
  640. __bad_area(regs, error_code, address, SEGV_MAPERR);
  641. }
  642. static noinline void
  643. bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
  644. unsigned long address)
  645. {
  646. __bad_area(regs, error_code, address, SEGV_ACCERR);
  647. }
  648. /* TODO: fixup for "mm-invoke-oom-killer-from-page-fault.patch" */
  649. static void
  650. out_of_memory(struct pt_regs *regs, unsigned long error_code,
  651. unsigned long address)
  652. {
  653. /*
  654. * We ran out of memory, call the OOM killer, and return the userspace
  655. * (which will retry the fault, or kill us if we got oom-killed):
  656. */
  657. up_read(&current->mm->mmap_sem);
  658. pagefault_out_of_memory();
  659. }
  660. static void
  661. do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address)
  662. {
  663. struct task_struct *tsk = current;
  664. struct mm_struct *mm = tsk->mm;
  665. up_read(&mm->mmap_sem);
  666. /* Kernel mode? Handle exceptions or die: */
  667. if (!(error_code & PF_USER))
  668. no_context(regs, error_code, address);
  669. /* User-space => ok to do another page fault: */
  670. if (is_prefetch(regs, error_code, address))
  671. return;
  672. tsk->thread.cr2 = address;
  673. tsk->thread.error_code = error_code;
  674. tsk->thread.trap_no = 14;
  675. force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk);
  676. }
  677. static noinline void
  678. mm_fault_error(struct pt_regs *regs, unsigned long error_code,
  679. unsigned long address, unsigned int fault)
  680. {
  681. if (fault & VM_FAULT_OOM) {
  682. out_of_memory(regs, error_code, address);
  683. } else {
  684. if (fault & VM_FAULT_SIGBUS)
  685. do_sigbus(regs, error_code, address);
  686. else
  687. BUG();
  688. }
  689. }
  690. static int spurious_fault_check(unsigned long error_code, pte_t *pte)
  691. {
  692. if ((error_code & PF_WRITE) && !pte_write(*pte))
  693. return 0;
  694. if ((error_code & PF_INSTR) && !pte_exec(*pte))
  695. return 0;
  696. return 1;
  697. }
  698. /*
  699. * Handle a spurious fault caused by a stale TLB entry.
  700. *
  701. * This allows us to lazily refresh the TLB when increasing the
  702. * permissions of a kernel page (RO -> RW or NX -> X). Doing it
  703. * eagerly is very expensive since that implies doing a full
  704. * cross-processor TLB flush, even if no stale TLB entries exist
  705. * on other processors.
  706. *
  707. * There are no security implications to leaving a stale TLB when
  708. * increasing the permissions on a page.
  709. */
  710. static noinline int
  711. spurious_fault(unsigned long error_code, unsigned long address)
  712. {
  713. pgd_t *pgd;
  714. pud_t *pud;
  715. pmd_t *pmd;
  716. pte_t *pte;
  717. int ret;
  718. /* Reserved-bit violation or user access to kernel space? */
  719. if (error_code & (PF_USER | PF_RSVD))
  720. return 0;
  721. pgd = init_mm.pgd + pgd_index(address);
  722. if (!pgd_present(*pgd))
  723. return 0;
  724. pud = pud_offset(pgd, address);
  725. if (!pud_present(*pud))
  726. return 0;
  727. if (pud_large(*pud))
  728. return spurious_fault_check(error_code, (pte_t *) pud);
  729. pmd = pmd_offset(pud, address);
  730. if (!pmd_present(*pmd))
  731. return 0;
  732. if (pmd_large(*pmd))
  733. return spurious_fault_check(error_code, (pte_t *) pmd);
  734. pte = pte_offset_kernel(pmd, address);
  735. if (!pte_present(*pte))
  736. return 0;
  737. ret = spurious_fault_check(error_code, pte);
  738. if (!ret)
  739. return 0;
  740. /*
  741. * Make sure we have permissions in PMD.
  742. * If not, then there's a bug in the page tables:
  743. */
  744. ret = spurious_fault_check(error_code, (pte_t *) pmd);
  745. WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
  746. return ret;
  747. }
  748. int show_unhandled_signals = 1;
  749. static inline int
  750. access_error(unsigned long error_code, int write, struct vm_area_struct *vma)
  751. {
  752. if (write) {
  753. /* write, present and write, not present: */
  754. if (unlikely(!(vma->vm_flags & VM_WRITE)))
  755. return 1;
  756. return 0;
  757. }
  758. /* read, present: */
  759. if (unlikely(error_code & PF_PROT))
  760. return 1;
  761. /* read, not present: */
  762. if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
  763. return 1;
  764. return 0;
  765. }
  766. static int fault_in_kernel_space(unsigned long address)
  767. {
  768. return address >= TASK_SIZE_MAX;
  769. }
  770. /*
  771. * This routine handles page faults. It determines the address,
  772. * and the problem, and then passes it off to one of the appropriate
  773. * routines.
  774. */
  775. dotraplinkage void __kprobes
  776. do_page_fault(struct pt_regs *regs, unsigned long error_code)
  777. {
  778. struct vm_area_struct *vma;
  779. struct task_struct *tsk;
  780. unsigned long address;
  781. struct mm_struct *mm;
  782. int write;
  783. int fault;
  784. tsk = current;
  785. mm = tsk->mm;
  786. /* Get the faulting address: */
  787. address = read_cr2();
  788. /*
  789. * Detect and handle instructions that would cause a page fault for
  790. * both a tracked kernel page and a userspace page.
  791. */
  792. if (kmemcheck_active(regs))
  793. kmemcheck_hide(regs);
  794. prefetchw(&mm->mmap_sem);
  795. if (unlikely(kmmio_fault(regs, address)))
  796. return;
  797. /*
  798. * We fault-in kernel-space virtual memory on-demand. The
  799. * 'reference' page table is init_mm.pgd.
  800. *
  801. * NOTE! We MUST NOT take any locks for this case. We may
  802. * be in an interrupt or a critical region, and should
  803. * only copy the information from the master page table,
  804. * nothing more.
  805. *
  806. * This verifies that the fault happens in kernel space
  807. * (error_code & 4) == 0, and that the fault was not a
  808. * protection error (error_code & 9) == 0.
  809. */
  810. if (unlikely(fault_in_kernel_space(address))) {
  811. if (!(error_code & (PF_RSVD | PF_USER | PF_PROT))) {
  812. if (vmalloc_fault(address) >= 0)
  813. return;
  814. if (kmemcheck_fault(regs, address, error_code))
  815. return;
  816. }
  817. /* Can handle a stale RO->RW TLB: */
  818. if (spurious_fault(error_code, address))
  819. return;
  820. /* kprobes don't want to hook the spurious faults: */
  821. if (notify_page_fault(regs))
  822. return;
  823. /*
  824. * Don't take the mm semaphore here. If we fixup a prefetch
  825. * fault we could otherwise deadlock:
  826. */
  827. bad_area_nosemaphore(regs, error_code, address);
  828. return;
  829. }
  830. /* kprobes don't want to hook the spurious faults: */
  831. if (unlikely(notify_page_fault(regs)))
  832. return;
  833. /*
  834. * It's safe to allow irq's after cr2 has been saved and the
  835. * vmalloc fault has been handled.
  836. *
  837. * User-mode registers count as a user access even for any
  838. * potential system fault or CPU buglet:
  839. */
  840. if (user_mode_vm(regs)) {
  841. local_irq_enable();
  842. error_code |= PF_USER;
  843. } else {
  844. if (regs->flags & X86_EFLAGS_IF)
  845. local_irq_enable();
  846. }
  847. if (unlikely(error_code & PF_RSVD))
  848. pgtable_bad(regs, error_code, address);
  849. perf_swcounter_event(PERF_COUNT_SW_PAGE_FAULTS, 1, 0, regs, address);
  850. /*
  851. * If we're in an interrupt, have no user context or are running
  852. * in an atomic region then we must not take the fault:
  853. */
  854. if (unlikely(in_atomic() || !mm)) {
  855. bad_area_nosemaphore(regs, error_code, address);
  856. return;
  857. }
  858. /*
  859. * When running in the kernel we expect faults to occur only to
  860. * addresses in user space. All other faults represent errors in
  861. * the kernel and should generate an OOPS. Unfortunately, in the
  862. * case of an erroneous fault occurring in a code path which already
  863. * holds mmap_sem we will deadlock attempting to validate the fault
  864. * against the address space. Luckily the kernel only validly
  865. * references user space from well defined areas of code, which are
  866. * listed in the exceptions table.
  867. *
  868. * As the vast majority of faults will be valid we will only perform
  869. * the source reference check when there is a possibility of a
  870. * deadlock. Attempt to lock the address space, if we cannot we then
  871. * validate the source. If this is invalid we can skip the address
  872. * space check, thus avoiding the deadlock:
  873. */
  874. if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
  875. if ((error_code & PF_USER) == 0 &&
  876. !search_exception_tables(regs->ip)) {
  877. bad_area_nosemaphore(regs, error_code, address);
  878. return;
  879. }
  880. down_read(&mm->mmap_sem);
  881. } else {
  882. /*
  883. * The above down_read_trylock() might have succeeded in
  884. * which case we'll have missed the might_sleep() from
  885. * down_read():
  886. */
  887. might_sleep();
  888. }
  889. vma = find_vma(mm, address);
  890. if (unlikely(!vma)) {
  891. bad_area(regs, error_code, address);
  892. return;
  893. }
  894. if (likely(vma->vm_start <= address))
  895. goto good_area;
  896. if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
  897. bad_area(regs, error_code, address);
  898. return;
  899. }
  900. if (error_code & PF_USER) {
  901. /*
  902. * Accessing the stack below %sp is always a bug.
  903. * The large cushion allows instructions like enter
  904. * and pusha to work. ("enter $65535, $31" pushes
  905. * 32 pointers and then decrements %sp by 65535.)
  906. */
  907. if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
  908. bad_area(regs, error_code, address);
  909. return;
  910. }
  911. }
  912. if (unlikely(expand_stack(vma, address))) {
  913. bad_area(regs, error_code, address);
  914. return;
  915. }
  916. /*
  917. * Ok, we have a good vm_area for this memory access, so
  918. * we can handle it..
  919. */
  920. good_area:
  921. write = error_code & PF_WRITE;
  922. if (unlikely(access_error(error_code, write, vma))) {
  923. bad_area_access_error(regs, error_code, address);
  924. return;
  925. }
  926. /*
  927. * If for any reason at all we couldn't handle the fault,
  928. * make sure we exit gracefully rather than endlessly redo
  929. * the fault:
  930. */
  931. fault = handle_mm_fault(mm, vma, address, write ? FAULT_FLAG_WRITE : 0);
  932. if (unlikely(fault & VM_FAULT_ERROR)) {
  933. mm_fault_error(regs, error_code, address, fault);
  934. return;
  935. }
  936. if (fault & VM_FAULT_MAJOR) {
  937. tsk->maj_flt++;
  938. perf_swcounter_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, 0,
  939. regs, address);
  940. } else {
  941. tsk->min_flt++;
  942. perf_swcounter_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, 0,
  943. regs, address);
  944. }
  945. check_v8086_mode(regs, address, tsk);
  946. up_read(&mm->mmap_sem);
  947. }