fault.c 27 KB

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