fault.c 27 KB

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