fault.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  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/vsyscall.h>
  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. static const char errata93_warning[] =
  357. KERN_ERR
  358. "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
  359. "******* Working around it, but it may cause SEGVs or burn power.\n"
  360. "******* Please consider a BIOS update.\n"
  361. "******* Disabling USB legacy in the BIOS may also help.\n";
  362. /*
  363. * No vm86 mode in 64-bit mode:
  364. */
  365. static inline void
  366. check_v8086_mode(struct pt_regs *regs, unsigned long address,
  367. struct task_struct *tsk)
  368. {
  369. }
  370. static int bad_address(void *p)
  371. {
  372. unsigned long dummy;
  373. return probe_kernel_address((unsigned long *)p, dummy);
  374. }
  375. static void dump_pagetable(unsigned long address)
  376. {
  377. pgd_t *base = __va(read_cr3() & PHYSICAL_PAGE_MASK);
  378. pgd_t *pgd = base + pgd_index(address);
  379. pud_t *pud;
  380. pmd_t *pmd;
  381. pte_t *pte;
  382. if (bad_address(pgd))
  383. goto bad;
  384. printk("PGD %lx ", pgd_val(*pgd));
  385. if (!pgd_present(*pgd))
  386. goto out;
  387. pud = pud_offset(pgd, address);
  388. if (bad_address(pud))
  389. goto bad;
  390. printk("PUD %lx ", pud_val(*pud));
  391. if (!pud_present(*pud) || pud_large(*pud))
  392. goto out;
  393. pmd = pmd_offset(pud, address);
  394. if (bad_address(pmd))
  395. goto bad;
  396. printk("PMD %lx ", pmd_val(*pmd));
  397. if (!pmd_present(*pmd) || pmd_large(*pmd))
  398. goto out;
  399. pte = pte_offset_kernel(pmd, address);
  400. if (bad_address(pte))
  401. goto bad;
  402. printk("PTE %lx", pte_val(*pte));
  403. out:
  404. printk("\n");
  405. return;
  406. bad:
  407. printk("BAD\n");
  408. }
  409. #endif /* CONFIG_X86_64 */
  410. /*
  411. * Workaround for K8 erratum #93 & buggy BIOS.
  412. *
  413. * BIOS SMM functions are required to use a specific workaround
  414. * to avoid corruption of the 64bit RIP register on C stepping K8.
  415. *
  416. * A lot of BIOS that didn't get tested properly miss this.
  417. *
  418. * The OS sees this as a page fault with the upper 32bits of RIP cleared.
  419. * Try to work around it here.
  420. *
  421. * Note we only handle faults in kernel here.
  422. * Does nothing on 32-bit.
  423. */
  424. static int is_errata93(struct pt_regs *regs, unsigned long address)
  425. {
  426. #ifdef CONFIG_X86_64
  427. if (address != regs->ip)
  428. return 0;
  429. if ((address >> 32) != 0)
  430. return 0;
  431. address |= 0xffffffffUL << 32;
  432. if ((address >= (u64)_stext && address <= (u64)_etext) ||
  433. (address >= MODULES_VADDR && address <= MODULES_END)) {
  434. printk_once(errata93_warning);
  435. regs->ip = address;
  436. return 1;
  437. }
  438. #endif
  439. return 0;
  440. }
  441. /*
  442. * Work around K8 erratum #100 K8 in compat mode occasionally jumps
  443. * to illegal addresses >4GB.
  444. *
  445. * We catch this in the page fault handler because these addresses
  446. * are not reachable. Just detect this case and return. Any code
  447. * segment in LDT is compatibility mode.
  448. */
  449. static int is_errata100(struct pt_regs *regs, unsigned long address)
  450. {
  451. #ifdef CONFIG_X86_64
  452. if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32))
  453. return 1;
  454. #endif
  455. return 0;
  456. }
  457. static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
  458. {
  459. #ifdef CONFIG_X86_F00F_BUG
  460. unsigned long nr;
  461. /*
  462. * Pentium F0 0F C7 C8 bug workaround:
  463. */
  464. if (boot_cpu_data.f00f_bug) {
  465. nr = (address - idt_descr.address) >> 3;
  466. if (nr == 6) {
  467. do_invalid_op(regs, 0);
  468. return 1;
  469. }
  470. }
  471. #endif
  472. return 0;
  473. }
  474. static const char nx_warning[] = KERN_CRIT
  475. "kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n";
  476. static void
  477. show_fault_oops(struct pt_regs *regs, unsigned long error_code,
  478. unsigned long address)
  479. {
  480. if (!oops_may_print())
  481. return;
  482. if (error_code & PF_INSTR) {
  483. unsigned int level;
  484. pte_t *pte = lookup_address(address, &level);
  485. if (pte && pte_present(*pte) && !pte_exec(*pte))
  486. printk(nx_warning, current_uid());
  487. }
  488. printk(KERN_ALERT "BUG: unable to handle kernel ");
  489. if (address < PAGE_SIZE)
  490. printk(KERN_CONT "NULL pointer dereference");
  491. else
  492. printk(KERN_CONT "paging request");
  493. printk(KERN_CONT " at %p\n", (void *) address);
  494. printk(KERN_ALERT "IP:");
  495. printk_address(regs->ip, 1);
  496. dump_pagetable(address);
  497. }
  498. static noinline void
  499. pgtable_bad(struct pt_regs *regs, unsigned long error_code,
  500. unsigned long address)
  501. {
  502. struct task_struct *tsk;
  503. unsigned long flags;
  504. int sig;
  505. flags = oops_begin();
  506. tsk = current;
  507. sig = SIGKILL;
  508. printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
  509. tsk->comm, address);
  510. dump_pagetable(address);
  511. tsk->thread.cr2 = address;
  512. tsk->thread.trap_no = 14;
  513. tsk->thread.error_code = error_code;
  514. if (__die("Bad pagetable", regs, error_code))
  515. sig = 0;
  516. oops_end(flags, regs, sig);
  517. }
  518. static noinline void
  519. no_context(struct pt_regs *regs, unsigned long error_code,
  520. unsigned long address)
  521. {
  522. struct task_struct *tsk = current;
  523. unsigned long *stackend;
  524. unsigned long flags;
  525. int sig;
  526. /* Are we prepared to handle this kernel fault? */
  527. if (fixup_exception(regs))
  528. return;
  529. /*
  530. * 32-bit:
  531. *
  532. * Valid to do another page fault here, because if this fault
  533. * had been triggered by is_prefetch fixup_exception would have
  534. * handled it.
  535. *
  536. * 64-bit:
  537. *
  538. * Hall of shame of CPU/BIOS bugs.
  539. */
  540. if (is_prefetch(regs, error_code, address))
  541. return;
  542. if (is_errata93(regs, address))
  543. return;
  544. /*
  545. * Oops. The kernel tried to access some bad page. We'll have to
  546. * terminate things with extreme prejudice:
  547. */
  548. flags = oops_begin();
  549. show_fault_oops(regs, error_code, address);
  550. stackend = end_of_stack(tsk);
  551. if (tsk != &init_task && *stackend != STACK_END_MAGIC)
  552. printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
  553. tsk->thread.cr2 = address;
  554. tsk->thread.trap_no = 14;
  555. tsk->thread.error_code = error_code;
  556. sig = SIGKILL;
  557. if (__die("Oops", regs, error_code))
  558. sig = 0;
  559. /* Executive summary in case the body of the oops scrolled away */
  560. printk(KERN_EMERG "CR2: %016lx\n", address);
  561. oops_end(flags, regs, sig);
  562. }
  563. /*
  564. * Print out info about fatal segfaults, if the show_unhandled_signals
  565. * sysctl is set:
  566. */
  567. static inline void
  568. show_signal_msg(struct pt_regs *regs, unsigned long error_code,
  569. unsigned long address, struct task_struct *tsk)
  570. {
  571. if (!unhandled_signal(tsk, SIGSEGV))
  572. return;
  573. if (!printk_ratelimit())
  574. return;
  575. printk("%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
  576. task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
  577. tsk->comm, task_pid_nr(tsk), address,
  578. (void *)regs->ip, (void *)regs->sp, error_code);
  579. print_vma_addr(KERN_CONT " in ", regs->ip);
  580. printk(KERN_CONT "\n");
  581. }
  582. static void
  583. __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
  584. unsigned long address, int si_code)
  585. {
  586. struct task_struct *tsk = current;
  587. /* User mode accesses just cause a SIGSEGV */
  588. if (error_code & PF_USER) {
  589. /*
  590. * It's possible to have interrupts off here:
  591. */
  592. local_irq_enable();
  593. /*
  594. * Valid to do another page fault here because this one came
  595. * from user space:
  596. */
  597. if (is_prefetch(regs, error_code, address))
  598. return;
  599. if (is_errata100(regs, address))
  600. return;
  601. #ifdef CONFIG_X86_64
  602. /*
  603. * Instruction fetch faults in the vsyscall page might need
  604. * emulation.
  605. */
  606. if (unlikely((error_code & PF_INSTR) &&
  607. ((address & ~0xfff) == VSYSCALL_START))) {
  608. if (emulate_vsyscall(regs, address))
  609. return;
  610. }
  611. #endif
  612. if (unlikely(show_unhandled_signals))
  613. show_signal_msg(regs, error_code, address, tsk);
  614. /* Kernel addresses are always protection faults: */
  615. tsk->thread.cr2 = address;
  616. tsk->thread.error_code = error_code | (address >= TASK_SIZE);
  617. tsk->thread.trap_no = 14;
  618. force_sig_info_fault(SIGSEGV, si_code, address, tsk, 0);
  619. return;
  620. }
  621. if (is_f00f_bug(regs, address))
  622. return;
  623. no_context(regs, error_code, address);
  624. }
  625. static noinline void
  626. bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
  627. unsigned long address)
  628. {
  629. __bad_area_nosemaphore(regs, error_code, address, SEGV_MAPERR);
  630. }
  631. static void
  632. __bad_area(struct pt_regs *regs, unsigned long error_code,
  633. unsigned long address, int si_code)
  634. {
  635. struct mm_struct *mm = current->mm;
  636. /*
  637. * Something tried to access memory that isn't in our memory map..
  638. * Fix it, but check if it's kernel or user first..
  639. */
  640. up_read(&mm->mmap_sem);
  641. __bad_area_nosemaphore(regs, error_code, address, si_code);
  642. }
  643. static noinline void
  644. bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
  645. {
  646. __bad_area(regs, error_code, address, SEGV_MAPERR);
  647. }
  648. static noinline void
  649. bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
  650. unsigned long address)
  651. {
  652. __bad_area(regs, error_code, address, SEGV_ACCERR);
  653. }
  654. /* TODO: fixup for "mm-invoke-oom-killer-from-page-fault.patch" */
  655. static void
  656. out_of_memory(struct pt_regs *regs, unsigned long error_code,
  657. unsigned long address)
  658. {
  659. /*
  660. * We ran out of memory, call the OOM killer, and return the userspace
  661. * (which will retry the fault, or kill us if we got oom-killed):
  662. */
  663. up_read(&current->mm->mmap_sem);
  664. pagefault_out_of_memory();
  665. }
  666. static void
  667. do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address,
  668. unsigned int fault)
  669. {
  670. struct task_struct *tsk = current;
  671. struct mm_struct *mm = tsk->mm;
  672. int code = BUS_ADRERR;
  673. up_read(&mm->mmap_sem);
  674. /* Kernel mode? Handle exceptions or die: */
  675. if (!(error_code & PF_USER)) {
  676. no_context(regs, error_code, address);
  677. return;
  678. }
  679. /* User-space => ok to do another page fault: */
  680. if (is_prefetch(regs, error_code, address))
  681. return;
  682. tsk->thread.cr2 = address;
  683. tsk->thread.error_code = error_code;
  684. tsk->thread.trap_no = 14;
  685. #ifdef CONFIG_MEMORY_FAILURE
  686. if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
  687. printk(KERN_ERR
  688. "MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n",
  689. tsk->comm, tsk->pid, address);
  690. code = BUS_MCEERR_AR;
  691. }
  692. #endif
  693. force_sig_info_fault(SIGBUS, code, address, tsk, fault);
  694. }
  695. static noinline int
  696. mm_fault_error(struct pt_regs *regs, unsigned long error_code,
  697. unsigned long address, unsigned int fault)
  698. {
  699. /*
  700. * Pagefault was interrupted by SIGKILL. We have no reason to
  701. * continue pagefault.
  702. */
  703. if (fatal_signal_pending(current)) {
  704. if (!(fault & VM_FAULT_RETRY))
  705. up_read(&current->mm->mmap_sem);
  706. if (!(error_code & PF_USER))
  707. no_context(regs, error_code, address);
  708. return 1;
  709. }
  710. if (!(fault & VM_FAULT_ERROR))
  711. return 0;
  712. if (fault & VM_FAULT_OOM) {
  713. /* Kernel mode? Handle exceptions or die: */
  714. if (!(error_code & PF_USER)) {
  715. up_read(&current->mm->mmap_sem);
  716. no_context(regs, error_code, address);
  717. return 1;
  718. }
  719. out_of_memory(regs, error_code, address);
  720. } else {
  721. if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|
  722. VM_FAULT_HWPOISON_LARGE))
  723. do_sigbus(regs, error_code, address, fault);
  724. else
  725. BUG();
  726. }
  727. return 1;
  728. }
  729. static int spurious_fault_check(unsigned long error_code, pte_t *pte)
  730. {
  731. if ((error_code & PF_WRITE) && !pte_write(*pte))
  732. return 0;
  733. if ((error_code & PF_INSTR) && !pte_exec(*pte))
  734. return 0;
  735. return 1;
  736. }
  737. /*
  738. * Handle a spurious fault caused by a stale TLB entry.
  739. *
  740. * This allows us to lazily refresh the TLB when increasing the
  741. * permissions of a kernel page (RO -> RW or NX -> X). Doing it
  742. * eagerly is very expensive since that implies doing a full
  743. * cross-processor TLB flush, even if no stale TLB entries exist
  744. * on other processors.
  745. *
  746. * There are no security implications to leaving a stale TLB when
  747. * increasing the permissions on a page.
  748. */
  749. static noinline __kprobes int
  750. spurious_fault(unsigned long error_code, unsigned long address)
  751. {
  752. pgd_t *pgd;
  753. pud_t *pud;
  754. pmd_t *pmd;
  755. pte_t *pte;
  756. int ret;
  757. /* Reserved-bit violation or user access to kernel space? */
  758. if (error_code & (PF_USER | PF_RSVD))
  759. return 0;
  760. pgd = init_mm.pgd + pgd_index(address);
  761. if (!pgd_present(*pgd))
  762. return 0;
  763. pud = pud_offset(pgd, address);
  764. if (!pud_present(*pud))
  765. return 0;
  766. if (pud_large(*pud))
  767. return spurious_fault_check(error_code, (pte_t *) pud);
  768. pmd = pmd_offset(pud, address);
  769. if (!pmd_present(*pmd))
  770. return 0;
  771. if (pmd_large(*pmd))
  772. return spurious_fault_check(error_code, (pte_t *) pmd);
  773. /*
  774. * Note: don't use pte_present() here, since it returns true
  775. * if the _PAGE_PROTNONE bit is set. However, this aliases the
  776. * _PAGE_GLOBAL bit, which for kernel pages give false positives
  777. * when CONFIG_DEBUG_PAGEALLOC is used.
  778. */
  779. pte = pte_offset_kernel(pmd, address);
  780. if (!(pte_flags(*pte) & _PAGE_PRESENT))
  781. return 0;
  782. ret = spurious_fault_check(error_code, pte);
  783. if (!ret)
  784. return 0;
  785. /*
  786. * Make sure we have permissions in PMD.
  787. * If not, then there's a bug in the page tables:
  788. */
  789. ret = spurious_fault_check(error_code, (pte_t *) pmd);
  790. WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
  791. return ret;
  792. }
  793. int show_unhandled_signals = 1;
  794. static inline int
  795. access_error(unsigned long error_code, struct vm_area_struct *vma)
  796. {
  797. if (error_code & PF_WRITE) {
  798. /* write, present and write, not present: */
  799. if (unlikely(!(vma->vm_flags & VM_WRITE)))
  800. return 1;
  801. return 0;
  802. }
  803. /* read, present: */
  804. if (unlikely(error_code & PF_PROT))
  805. return 1;
  806. /* read, not present: */
  807. if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
  808. return 1;
  809. return 0;
  810. }
  811. static int fault_in_kernel_space(unsigned long address)
  812. {
  813. return address >= TASK_SIZE_MAX;
  814. }
  815. /*
  816. * This routine handles page faults. It determines the address,
  817. * and the problem, and then passes it off to one of the appropriate
  818. * routines.
  819. */
  820. dotraplinkage void __kprobes
  821. do_page_fault(struct pt_regs *regs, unsigned long error_code)
  822. {
  823. struct vm_area_struct *vma;
  824. struct task_struct *tsk;
  825. unsigned long address;
  826. struct mm_struct *mm;
  827. int fault;
  828. int write = error_code & PF_WRITE;
  829. unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE |
  830. (write ? FAULT_FLAG_WRITE : 0);
  831. tsk = current;
  832. mm = tsk->mm;
  833. /* Get the faulting address: */
  834. address = read_cr2();
  835. /*
  836. * Detect and handle instructions that would cause a page fault for
  837. * both a tracked kernel page and a userspace page.
  838. */
  839. if (kmemcheck_active(regs))
  840. kmemcheck_hide(regs);
  841. prefetchw(&mm->mmap_sem);
  842. if (unlikely(kmmio_fault(regs, address)))
  843. return;
  844. /*
  845. * We fault-in kernel-space virtual memory on-demand. The
  846. * 'reference' page table is init_mm.pgd.
  847. *
  848. * NOTE! We MUST NOT take any locks for this case. We may
  849. * be in an interrupt or a critical region, and should
  850. * only copy the information from the master page table,
  851. * nothing more.
  852. *
  853. * This verifies that the fault happens in kernel space
  854. * (error_code & 4) == 0, and that the fault was not a
  855. * protection error (error_code & 9) == 0.
  856. */
  857. if (unlikely(fault_in_kernel_space(address))) {
  858. if (!(error_code & (PF_RSVD | PF_USER | PF_PROT))) {
  859. if (vmalloc_fault(address) >= 0)
  860. return;
  861. if (kmemcheck_fault(regs, address, error_code))
  862. return;
  863. }
  864. /* Can handle a stale RO->RW TLB: */
  865. if (spurious_fault(error_code, address))
  866. return;
  867. /* kprobes don't want to hook the spurious faults: */
  868. if (notify_page_fault(regs))
  869. return;
  870. /*
  871. * Don't take the mm semaphore here. If we fixup a prefetch
  872. * fault we could otherwise deadlock:
  873. */
  874. bad_area_nosemaphore(regs, error_code, address);
  875. return;
  876. }
  877. /* kprobes don't want to hook the spurious faults: */
  878. if (unlikely(notify_page_fault(regs)))
  879. return;
  880. /*
  881. * It's safe to allow irq's after cr2 has been saved and the
  882. * vmalloc fault has been handled.
  883. *
  884. * User-mode registers count as a user access even for any
  885. * potential system fault or CPU buglet:
  886. */
  887. if (user_mode_vm(regs)) {
  888. local_irq_enable();
  889. error_code |= PF_USER;
  890. } else {
  891. if (regs->flags & X86_EFLAGS_IF)
  892. local_irq_enable();
  893. }
  894. if (unlikely(error_code & PF_RSVD))
  895. pgtable_bad(regs, error_code, address);
  896. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  897. /*
  898. * If we're in an interrupt, have no user context or are running
  899. * in an atomic region then we must not take the fault:
  900. */
  901. if (unlikely(in_atomic() || !mm)) {
  902. bad_area_nosemaphore(regs, error_code, address);
  903. return;
  904. }
  905. /*
  906. * When running in the kernel we expect faults to occur only to
  907. * addresses in user space. All other faults represent errors in
  908. * the kernel and should generate an OOPS. Unfortunately, in the
  909. * case of an erroneous fault occurring in a code path which already
  910. * holds mmap_sem we will deadlock attempting to validate the fault
  911. * against the address space. Luckily the kernel only validly
  912. * references user space from well defined areas of code, which are
  913. * listed in the exceptions table.
  914. *
  915. * As the vast majority of faults will be valid we will only perform
  916. * the source reference check when there is a possibility of a
  917. * deadlock. Attempt to lock the address space, if we cannot we then
  918. * validate the source. If this is invalid we can skip the address
  919. * space check, thus avoiding the deadlock:
  920. */
  921. if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
  922. if ((error_code & PF_USER) == 0 &&
  923. !search_exception_tables(regs->ip)) {
  924. bad_area_nosemaphore(regs, error_code, address);
  925. return;
  926. }
  927. retry:
  928. down_read(&mm->mmap_sem);
  929. } else {
  930. /*
  931. * The above down_read_trylock() might have succeeded in
  932. * which case we'll have missed the might_sleep() from
  933. * down_read():
  934. */
  935. might_sleep();
  936. }
  937. vma = find_vma(mm, address);
  938. if (unlikely(!vma)) {
  939. bad_area(regs, error_code, address);
  940. return;
  941. }
  942. if (likely(vma->vm_start <= address))
  943. goto good_area;
  944. if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
  945. bad_area(regs, error_code, address);
  946. return;
  947. }
  948. if (error_code & PF_USER) {
  949. /*
  950. * Accessing the stack below %sp is always a bug.
  951. * The large cushion allows instructions like enter
  952. * and pusha to work. ("enter $65535, $31" pushes
  953. * 32 pointers and then decrements %sp by 65535.)
  954. */
  955. if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
  956. bad_area(regs, error_code, address);
  957. return;
  958. }
  959. }
  960. if (unlikely(expand_stack(vma, address))) {
  961. bad_area(regs, error_code, address);
  962. return;
  963. }
  964. /*
  965. * Ok, we have a good vm_area for this memory access, so
  966. * we can handle it..
  967. */
  968. good_area:
  969. if (unlikely(access_error(error_code, vma))) {
  970. bad_area_access_error(regs, error_code, address);
  971. return;
  972. }
  973. /*
  974. * If for any reason at all we couldn't handle the fault,
  975. * make sure we exit gracefully rather than endlessly redo
  976. * the fault:
  977. */
  978. fault = handle_mm_fault(mm, vma, address, flags);
  979. if (unlikely(fault & (VM_FAULT_RETRY|VM_FAULT_ERROR))) {
  980. if (mm_fault_error(regs, error_code, address, fault))
  981. return;
  982. }
  983. /*
  984. * Major/minor page fault accounting is only done on the
  985. * initial attempt. If we go through a retry, it is extremely
  986. * likely that the page will be found in page cache at that point.
  987. */
  988. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  989. if (fault & VM_FAULT_MAJOR) {
  990. tsk->maj_flt++;
  991. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
  992. regs, address);
  993. } else {
  994. tsk->min_flt++;
  995. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
  996. regs, address);
  997. }
  998. if (fault & VM_FAULT_RETRY) {
  999. /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
  1000. * of starvation. */
  1001. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  1002. goto retry;
  1003. }
  1004. }
  1005. check_v8086_mode(regs, address, tsk);
  1006. up_read(&mm->mmap_sem);
  1007. }