fault.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  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_counter.h> /* perf_swcounter_event */
  14. #include <asm/traps.h> /* dotraplinkage, ... */
  15. #include <asm/pgalloc.h> /* pgd_*(), ... */
  16. /*
  17. * Page fault error code bits:
  18. *
  19. * bit 0 == 0: no page found 1: protection fault
  20. * bit 1 == 0: read access 1: write access
  21. * bit 2 == 0: kernel-mode access 1: user-mode access
  22. * bit 3 == 1: use of reserved bit detected
  23. * bit 4 == 1: fault was an instruction fetch
  24. */
  25. enum x86_pf_error_code {
  26. PF_PROT = 1 << 0,
  27. PF_WRITE = 1 << 1,
  28. PF_USER = 1 << 2,
  29. PF_RSVD = 1 << 3,
  30. PF_INSTR = 1 << 4,
  31. };
  32. /*
  33. * Returns 0 if mmiotrace is disabled, or if the fault is not
  34. * handled by mmiotrace:
  35. */
  36. static inline int kmmio_fault(struct pt_regs *regs, unsigned long addr)
  37. {
  38. if (unlikely(is_kmmio_active()))
  39. if (kmmio_handler(regs, addr) == 1)
  40. return -1;
  41. return 0;
  42. }
  43. static inline int notify_page_fault(struct pt_regs *regs)
  44. {
  45. int ret = 0;
  46. /* kprobe_running() needs smp_processor_id() */
  47. if (kprobes_built_in() && !user_mode_vm(regs)) {
  48. preempt_disable();
  49. if (kprobe_running() && kprobe_fault_handler(regs, 14))
  50. ret = 1;
  51. preempt_enable();
  52. }
  53. return ret;
  54. }
  55. /*
  56. * Prefetch quirks:
  57. *
  58. * 32-bit mode:
  59. *
  60. * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
  61. * Check that here and ignore it.
  62. *
  63. * 64-bit mode:
  64. *
  65. * Sometimes the CPU reports invalid exceptions on prefetch.
  66. * Check that here and ignore it.
  67. *
  68. * Opcode checker based on code by Richard Brunner.
  69. */
  70. static inline int
  71. check_prefetch_opcode(struct pt_regs *regs, unsigned char *instr,
  72. unsigned char opcode, int *prefetch)
  73. {
  74. unsigned char instr_hi = opcode & 0xf0;
  75. unsigned char instr_lo = opcode & 0x0f;
  76. switch (instr_hi) {
  77. case 0x20:
  78. case 0x30:
  79. /*
  80. * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
  81. * In X86_64 long mode, the CPU will signal invalid
  82. * opcode if some of these prefixes are present so
  83. * X86_64 will never get here anyway
  84. */
  85. return ((instr_lo & 7) == 0x6);
  86. #ifdef CONFIG_X86_64
  87. case 0x40:
  88. /*
  89. * In AMD64 long mode 0x40..0x4F are valid REX prefixes
  90. * Need to figure out under what instruction mode the
  91. * instruction was issued. Could check the LDT for lm,
  92. * but for now it's good enough to assume that long
  93. * mode only uses well known segments or kernel.
  94. */
  95. return (!user_mode(regs)) || (regs->cs == __USER_CS);
  96. #endif
  97. case 0x60:
  98. /* 0x64 thru 0x67 are valid prefixes in all modes. */
  99. return (instr_lo & 0xC) == 0x4;
  100. case 0xF0:
  101. /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
  102. return !instr_lo || (instr_lo>>1) == 1;
  103. case 0x00:
  104. /* Prefetch instruction is 0x0F0D or 0x0F18 */
  105. if (probe_kernel_address(instr, opcode))
  106. return 0;
  107. *prefetch = (instr_lo == 0xF) &&
  108. (opcode == 0x0D || opcode == 0x18);
  109. return 0;
  110. default:
  111. return 0;
  112. }
  113. }
  114. static int
  115. is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr)
  116. {
  117. unsigned char *max_instr;
  118. unsigned char *instr;
  119. int prefetch = 0;
  120. /*
  121. * If it was a exec (instruction fetch) fault on NX page, then
  122. * do not ignore the fault:
  123. */
  124. if (error_code & PF_INSTR)
  125. return 0;
  126. instr = (void *)convert_ip_to_linear(current, regs);
  127. max_instr = instr + 15;
  128. if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE)
  129. return 0;
  130. while (instr < max_instr) {
  131. unsigned char opcode;
  132. if (probe_kernel_address(instr, opcode))
  133. break;
  134. instr++;
  135. if (!check_prefetch_opcode(regs, instr, opcode, &prefetch))
  136. break;
  137. }
  138. return prefetch;
  139. }
  140. static void
  141. force_sig_info_fault(int si_signo, int si_code, unsigned long address,
  142. struct task_struct *tsk)
  143. {
  144. siginfo_t info;
  145. info.si_signo = si_signo;
  146. info.si_errno = 0;
  147. info.si_code = si_code;
  148. info.si_addr = (void __user *)address;
  149. force_sig_info(si_signo, &info, tsk);
  150. }
  151. DEFINE_SPINLOCK(pgd_lock);
  152. LIST_HEAD(pgd_list);
  153. #ifdef CONFIG_X86_32
  154. static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
  155. {
  156. unsigned index = pgd_index(address);
  157. pgd_t *pgd_k;
  158. pud_t *pud, *pud_k;
  159. pmd_t *pmd, *pmd_k;
  160. pgd += index;
  161. pgd_k = init_mm.pgd + index;
  162. if (!pgd_present(*pgd_k))
  163. return NULL;
  164. /*
  165. * set_pgd(pgd, *pgd_k); here would be useless on PAE
  166. * and redundant with the set_pmd() on non-PAE. As would
  167. * set_pud.
  168. */
  169. pud = pud_offset(pgd, address);
  170. pud_k = pud_offset(pgd_k, address);
  171. if (!pud_present(*pud_k))
  172. return NULL;
  173. pmd = pmd_offset(pud, address);
  174. pmd_k = pmd_offset(pud_k, address);
  175. if (!pmd_present(*pmd_k))
  176. return NULL;
  177. if (!pmd_present(*pmd))
  178. set_pmd(pmd, *pmd_k);
  179. else
  180. BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
  181. return pmd_k;
  182. }
  183. void vmalloc_sync_all(void)
  184. {
  185. unsigned long address;
  186. if (SHARED_KERNEL_PMD)
  187. return;
  188. for (address = VMALLOC_START & PMD_MASK;
  189. address >= TASK_SIZE && address < FIXADDR_TOP;
  190. address += PMD_SIZE) {
  191. unsigned long flags;
  192. struct page *page;
  193. spin_lock_irqsave(&pgd_lock, flags);
  194. list_for_each_entry(page, &pgd_list, lru) {
  195. if (!vmalloc_sync_one(page_address(page), address))
  196. break;
  197. }
  198. spin_unlock_irqrestore(&pgd_lock, flags);
  199. }
  200. }
  201. /*
  202. * 32-bit:
  203. *
  204. * Handle a fault on the vmalloc or module mapping area
  205. */
  206. static noinline int vmalloc_fault(unsigned long address)
  207. {
  208. unsigned long pgd_paddr;
  209. pmd_t *pmd_k;
  210. pte_t *pte_k;
  211. /* Make sure we are in vmalloc area: */
  212. if (!(address >= VMALLOC_START && address < VMALLOC_END))
  213. return -1;
  214. /*
  215. * Synchronize this task's top level page-table
  216. * with the 'reference' page table.
  217. *
  218. * Do _not_ use "current" here. We might be inside
  219. * an interrupt in the middle of a task switch..
  220. */
  221. pgd_paddr = read_cr3();
  222. pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
  223. if (!pmd_k)
  224. return -1;
  225. pte_k = pte_offset_kernel(pmd_k, address);
  226. if (!pte_present(*pte_k))
  227. return -1;
  228. return 0;
  229. }
  230. /*
  231. * Did it hit the DOS screen memory VA from vm86 mode?
  232. */
  233. static inline void
  234. check_v8086_mode(struct pt_regs *regs, unsigned long address,
  235. struct task_struct *tsk)
  236. {
  237. unsigned long bit;
  238. if (!v8086_mode(regs))
  239. return;
  240. bit = (address - 0xA0000) >> PAGE_SHIFT;
  241. if (bit < 32)
  242. tsk->thread.screen_bitmap |= 1 << bit;
  243. }
  244. static void dump_pagetable(unsigned long address)
  245. {
  246. __typeof__(pte_val(__pte(0))) page;
  247. page = read_cr3();
  248. page = ((__typeof__(page) *) __va(page))[address >> PGDIR_SHIFT];
  249. #ifdef CONFIG_X86_PAE
  250. printk("*pdpt = %016Lx ", page);
  251. if ((page >> PAGE_SHIFT) < max_low_pfn
  252. && page & _PAGE_PRESENT) {
  253. page &= PAGE_MASK;
  254. page = ((__typeof__(page) *) __va(page))[(address >> PMD_SHIFT)
  255. & (PTRS_PER_PMD - 1)];
  256. printk(KERN_CONT "*pde = %016Lx ", page);
  257. page &= ~_PAGE_NX;
  258. }
  259. #else
  260. printk("*pde = %08lx ", page);
  261. #endif
  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 ((page >> PAGE_SHIFT) < max_low_pfn
  269. && (page & _PAGE_PRESENT)
  270. && !(page & _PAGE_PSE)) {
  271. page &= PAGE_MASK;
  272. page = ((__typeof__(page) *) __va(page))[(address >> PAGE_SHIFT)
  273. & (PTRS_PER_PTE - 1)];
  274. printk("*pte = %0*Lx ", sizeof(page)*2, (u64)page);
  275. }
  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 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. /*
  318. * Copy kernel mappings over when needed. This can also
  319. * happen within a race in page table update. In the later
  320. * case just flush:
  321. */
  322. pgd = pgd_offset(current->active_mm, address);
  323. pgd_ref = pgd_offset_k(address);
  324. if (pgd_none(*pgd_ref))
  325. return -1;
  326. if (pgd_none(*pgd))
  327. set_pgd(pgd, *pgd_ref);
  328. else
  329. BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
  330. /*
  331. * Below here mismatches are bugs because these lower tables
  332. * are shared:
  333. */
  334. pud = pud_offset(pgd, address);
  335. pud_ref = pud_offset(pgd_ref, address);
  336. if (pud_none(*pud_ref))
  337. return -1;
  338. if (pud_none(*pud) || pud_page_vaddr(*pud) != pud_page_vaddr(*pud_ref))
  339. BUG();
  340. pmd = pmd_offset(pud, address);
  341. pmd_ref = pmd_offset(pud_ref, address);
  342. if (pmd_none(*pmd_ref))
  343. return -1;
  344. if (pmd_none(*pmd) || pmd_page(*pmd) != pmd_page(*pmd_ref))
  345. BUG();
  346. pte_ref = pte_offset_kernel(pmd_ref, address);
  347. if (!pte_present(*pte_ref))
  348. return -1;
  349. pte = pte_offset_kernel(pmd, address);
  350. /*
  351. * Don't use pte_page here, because the mappings can point
  352. * outside mem_map, and the NUMA hash lookup cannot handle
  353. * that:
  354. */
  355. if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
  356. BUG();
  357. return 0;
  358. }
  359. static const char errata93_warning[] =
  360. KERN_ERR "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
  361. KERN_ERR "******* Working around it, but it may cause SEGVs or burn power.\n"
  362. KERN_ERR "******* Please consider a BIOS update.\n"
  363. KERN_ERR "******* 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 *pgd;
  380. pud_t *pud;
  381. pmd_t *pmd;
  382. pte_t *pte;
  383. pgd = (pgd_t *)read_cr3();
  384. pgd = __va((unsigned long)pgd & PHYSICAL_PAGE_MASK);
  385. pgd += pgd_index(address);
  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 (*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(KERN_CONT "%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. {
  662. struct task_struct *tsk = current;
  663. struct mm_struct *mm = tsk->mm;
  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. force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk);
  675. }
  676. static noinline void
  677. mm_fault_error(struct pt_regs *regs, unsigned long error_code,
  678. unsigned long address, unsigned int fault)
  679. {
  680. if (fault & VM_FAULT_OOM) {
  681. out_of_memory(regs, error_code, address);
  682. } else {
  683. if (fault & VM_FAULT_SIGBUS)
  684. do_sigbus(regs, error_code, address);
  685. else
  686. BUG();
  687. }
  688. }
  689. static int spurious_fault_check(unsigned long error_code, pte_t *pte)
  690. {
  691. if ((error_code & PF_WRITE) && !pte_write(*pte))
  692. return 0;
  693. if ((error_code & PF_INSTR) && !pte_exec(*pte))
  694. return 0;
  695. return 1;
  696. }
  697. /*
  698. * Handle a spurious fault caused by a stale TLB entry.
  699. *
  700. * This allows us to lazily refresh the TLB when increasing the
  701. * permissions of a kernel page (RO -> RW or NX -> X). Doing it
  702. * eagerly is very expensive since that implies doing a full
  703. * cross-processor TLB flush, even if no stale TLB entries exist
  704. * on other processors.
  705. *
  706. * There are no security implications to leaving a stale TLB when
  707. * increasing the permissions on a page.
  708. */
  709. static noinline int
  710. spurious_fault(unsigned long error_code, unsigned long address)
  711. {
  712. pgd_t *pgd;
  713. pud_t *pud;
  714. pmd_t *pmd;
  715. pte_t *pte;
  716. int ret;
  717. /* Reserved-bit violation or user access to kernel space? */
  718. if (error_code & (PF_USER | PF_RSVD))
  719. return 0;
  720. pgd = init_mm.pgd + pgd_index(address);
  721. if (!pgd_present(*pgd))
  722. return 0;
  723. pud = pud_offset(pgd, address);
  724. if (!pud_present(*pud))
  725. return 0;
  726. if (pud_large(*pud))
  727. return spurious_fault_check(error_code, (pte_t *) pud);
  728. pmd = pmd_offset(pud, address);
  729. if (!pmd_present(*pmd))
  730. return 0;
  731. if (pmd_large(*pmd))
  732. return spurious_fault_check(error_code, (pte_t *) pmd);
  733. pte = pte_offset_kernel(pmd, address);
  734. if (!pte_present(*pte))
  735. return 0;
  736. ret = spurious_fault_check(error_code, pte);
  737. if (!ret)
  738. return 0;
  739. /*
  740. * Make sure we have permissions in PMD.
  741. * If not, then there's a bug in the page tables:
  742. */
  743. ret = spurious_fault_check(error_code, (pte_t *) pmd);
  744. WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
  745. return ret;
  746. }
  747. int show_unhandled_signals = 1;
  748. static inline int
  749. access_error(unsigned long error_code, int write, struct vm_area_struct *vma)
  750. {
  751. if (write) {
  752. /* write, present and write, not present: */
  753. if (unlikely(!(vma->vm_flags & VM_WRITE)))
  754. return 1;
  755. return 0;
  756. }
  757. /* read, present: */
  758. if (unlikely(error_code & PF_PROT))
  759. return 1;
  760. /* read, not present: */
  761. if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
  762. return 1;
  763. return 0;
  764. }
  765. static int fault_in_kernel_space(unsigned long address)
  766. {
  767. return address >= TASK_SIZE_MAX;
  768. }
  769. /*
  770. * This routine handles page faults. It determines the address,
  771. * and the problem, and then passes it off to one of the appropriate
  772. * routines.
  773. */
  774. dotraplinkage void __kprobes
  775. do_page_fault(struct pt_regs *regs, unsigned long error_code)
  776. {
  777. struct vm_area_struct *vma;
  778. struct task_struct *tsk;
  779. unsigned long address;
  780. struct mm_struct *mm;
  781. int write;
  782. int fault;
  783. tsk = current;
  784. mm = tsk->mm;
  785. prefetchw(&mm->mmap_sem);
  786. /* Get the faulting address: */
  787. address = read_cr2();
  788. if (unlikely(kmmio_fault(regs, address)))
  789. return;
  790. /*
  791. * We fault-in kernel-space virtual memory on-demand. The
  792. * 'reference' page table is init_mm.pgd.
  793. *
  794. * NOTE! We MUST NOT take any locks for this case. We may
  795. * be in an interrupt or a critical region, and should
  796. * only copy the information from the master page table,
  797. * nothing more.
  798. *
  799. * This verifies that the fault happens in kernel space
  800. * (error_code & 4) == 0, and that the fault was not a
  801. * protection error (error_code & 9) == 0.
  802. */
  803. if (unlikely(fault_in_kernel_space(address))) {
  804. if (!(error_code & (PF_RSVD|PF_USER|PF_PROT)) &&
  805. vmalloc_fault(address) >= 0)
  806. return;
  807. /* Can handle a stale RO->RW TLB: */
  808. if (spurious_fault(error_code, address))
  809. return;
  810. /* kprobes don't want to hook the spurious faults: */
  811. if (notify_page_fault(regs))
  812. return;
  813. /*
  814. * Don't take the mm semaphore here. If we fixup a prefetch
  815. * fault we could otherwise deadlock:
  816. */
  817. bad_area_nosemaphore(regs, error_code, address);
  818. return;
  819. }
  820. /* kprobes don't want to hook the spurious faults: */
  821. if (unlikely(notify_page_fault(regs)))
  822. return;
  823. /*
  824. * It's safe to allow irq's after cr2 has been saved and the
  825. * vmalloc fault has been handled.
  826. *
  827. * User-mode registers count as a user access even for any
  828. * potential system fault or CPU buglet:
  829. */
  830. if (user_mode_vm(regs)) {
  831. local_irq_enable();
  832. error_code |= PF_USER;
  833. } else {
  834. if (regs->flags & X86_EFLAGS_IF)
  835. local_irq_enable();
  836. }
  837. if (unlikely(error_code & PF_RSVD))
  838. pgtable_bad(regs, error_code, address);
  839. perf_swcounter_event(PERF_COUNT_SW_PAGE_FAULTS, 1, 0, regs, address);
  840. /*
  841. * If we're in an interrupt, have no user context or are running
  842. * in an atomic region then we must not take the fault:
  843. */
  844. if (unlikely(in_atomic() || !mm)) {
  845. bad_area_nosemaphore(regs, error_code, address);
  846. return;
  847. }
  848. /*
  849. * When running in the kernel we expect faults to occur only to
  850. * addresses in user space. All other faults represent errors in
  851. * the kernel and should generate an OOPS. Unfortunately, in the
  852. * case of an erroneous fault occurring in a code path which already
  853. * holds mmap_sem we will deadlock attempting to validate the fault
  854. * against the address space. Luckily the kernel only validly
  855. * references user space from well defined areas of code, which are
  856. * listed in the exceptions table.
  857. *
  858. * As the vast majority of faults will be valid we will only perform
  859. * the source reference check when there is a possibility of a
  860. * deadlock. Attempt to lock the address space, if we cannot we then
  861. * validate the source. If this is invalid we can skip the address
  862. * space check, thus avoiding the deadlock:
  863. */
  864. if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
  865. if ((error_code & PF_USER) == 0 &&
  866. !search_exception_tables(regs->ip)) {
  867. bad_area_nosemaphore(regs, error_code, address);
  868. return;
  869. }
  870. down_read(&mm->mmap_sem);
  871. } else {
  872. /*
  873. * The above down_read_trylock() might have succeeded in
  874. * which case we'll have missed the might_sleep() from
  875. * down_read():
  876. */
  877. might_sleep();
  878. }
  879. vma = find_vma(mm, address);
  880. if (unlikely(!vma)) {
  881. bad_area(regs, error_code, address);
  882. return;
  883. }
  884. if (likely(vma->vm_start <= address))
  885. goto good_area;
  886. if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
  887. bad_area(regs, error_code, address);
  888. return;
  889. }
  890. if (error_code & PF_USER) {
  891. /*
  892. * Accessing the stack below %sp is always a bug.
  893. * The large cushion allows instructions like enter
  894. * and pusha to work. ("enter $65535, $31" pushes
  895. * 32 pointers and then decrements %sp by 65535.)
  896. */
  897. if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
  898. bad_area(regs, error_code, address);
  899. return;
  900. }
  901. }
  902. if (unlikely(expand_stack(vma, address))) {
  903. bad_area(regs, error_code, address);
  904. return;
  905. }
  906. /*
  907. * Ok, we have a good vm_area for this memory access, so
  908. * we can handle it..
  909. */
  910. good_area:
  911. write = error_code & PF_WRITE;
  912. if (unlikely(access_error(error_code, write, vma))) {
  913. bad_area_access_error(regs, error_code, address);
  914. return;
  915. }
  916. /*
  917. * If for any reason at all we couldn't handle the fault,
  918. * make sure we exit gracefully rather than endlessly redo
  919. * the fault:
  920. */
  921. fault = handle_mm_fault(mm, vma, address, write);
  922. if (unlikely(fault & VM_FAULT_ERROR)) {
  923. mm_fault_error(regs, error_code, address, fault);
  924. return;
  925. }
  926. if (fault & VM_FAULT_MAJOR) {
  927. tsk->maj_flt++;
  928. perf_swcounter_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, 0,
  929. regs, address);
  930. } else {
  931. tsk->min_flt++;
  932. perf_swcounter_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, 0,
  933. regs, address);
  934. }
  935. check_v8086_mode(regs, address, tsk);
  936. up_read(&mm->mmap_sem);
  937. }