fault.c 28 KB

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