fault.c 26 KB

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