fault.c 26 KB

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