fault.c 26 KB

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