fault.c 29 KB

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