fault.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  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, address);
  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. perf_swcounter_event(PERF_COUNT_PAGE_FAULTS_MAJ, 1, 0,
  958. regs, address);
  959. } else {
  960. tsk->min_flt++;
  961. perf_swcounter_event(PERF_COUNT_PAGE_FAULTS_MIN, 1, 0,
  962. regs, address);
  963. }
  964. check_v8086_mode(regs, address, tsk);
  965. up_read(&mm->mmap_sem);
  966. }