fault.c 29 KB

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