fault.c 26 KB

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