fault.c 26 KB

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