fault.c 27 KB

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