fault.c 28 KB

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