fault.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  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 <asm/traps.h> /* dotraplinkage, ... */
  14. #include <asm/pgalloc.h> /* pgd_*(), ... */
  15. /*
  16. * Page fault error code bits:
  17. *
  18. * bit 0 == 0: no page found 1: protection fault
  19. * bit 1 == 0: read access 1: write access
  20. * bit 2 == 0: kernel-mode access 1: user-mode access
  21. * bit 3 == 1: use of reserved bit detected
  22. * bit 4 == 1: fault was an instruction fetch
  23. */
  24. enum x86_pf_error_code {
  25. PF_PROT = 1 << 0,
  26. PF_WRITE = 1 << 1,
  27. PF_USER = 1 << 2,
  28. PF_RSVD = 1 << 3,
  29. PF_INSTR = 1 << 4,
  30. };
  31. /*
  32. * Returns 0 if mmiotrace is disabled, or if the fault is not
  33. * handled by mmiotrace:
  34. */
  35. static inline int kmmio_fault(struct pt_regs *regs, unsigned long addr)
  36. {
  37. if (unlikely(is_kmmio_active()))
  38. if (kmmio_handler(regs, addr) == 1)
  39. return -1;
  40. return 0;
  41. }
  42. static inline int notify_page_fault(struct pt_regs *regs)
  43. {
  44. int ret = 0;
  45. /* kprobe_running() needs smp_processor_id() */
  46. if (kprobes_built_in() && !user_mode_vm(regs)) {
  47. preempt_disable();
  48. if (kprobe_running() && kprobe_fault_handler(regs, 14))
  49. ret = 1;
  50. preempt_enable();
  51. }
  52. return ret;
  53. }
  54. /*
  55. * Prefetch quirks:
  56. *
  57. * 32-bit mode:
  58. *
  59. * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
  60. * Check that here and ignore it.
  61. *
  62. * 64-bit mode:
  63. *
  64. * Sometimes the CPU reports invalid exceptions on prefetch.
  65. * Check that here and ignore it.
  66. *
  67. * Opcode checker based on code by Richard Brunner.
  68. */
  69. static inline int
  70. check_prefetch_opcode(struct pt_regs *regs, unsigned char *instr,
  71. unsigned char opcode, int *prefetch)
  72. {
  73. unsigned char instr_hi = opcode & 0xf0;
  74. unsigned char instr_lo = opcode & 0x0f;
  75. switch (instr_hi) {
  76. case 0x20:
  77. case 0x30:
  78. /*
  79. * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
  80. * In X86_64 long mode, the CPU will signal invalid
  81. * opcode if some of these prefixes are present so
  82. * X86_64 will never get here anyway
  83. */
  84. return ((instr_lo & 7) == 0x6);
  85. #ifdef CONFIG_X86_64
  86. case 0x40:
  87. /*
  88. * In AMD64 long mode 0x40..0x4F are valid REX prefixes
  89. * Need to figure out under what instruction mode the
  90. * instruction was issued. Could check the LDT for lm,
  91. * but for now it's good enough to assume that long
  92. * mode only uses well known segments or kernel.
  93. */
  94. return (!user_mode(regs)) || (regs->cs == __USER_CS);
  95. #endif
  96. case 0x60:
  97. /* 0x64 thru 0x67 are valid prefixes in all modes. */
  98. return (instr_lo & 0xC) == 0x4;
  99. case 0xF0:
  100. /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
  101. return !instr_lo || (instr_lo>>1) == 1;
  102. case 0x00:
  103. /* Prefetch instruction is 0x0F0D or 0x0F18 */
  104. if (probe_kernel_address(instr, opcode))
  105. return 0;
  106. *prefetch = (instr_lo == 0xF) &&
  107. (opcode == 0x0D || opcode == 0x18);
  108. return 0;
  109. default:
  110. return 0;
  111. }
  112. }
  113. static int
  114. is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr)
  115. {
  116. unsigned char *max_instr;
  117. unsigned char *instr;
  118. int prefetch = 0;
  119. /*
  120. * If it was a exec (instruction fetch) fault on NX page, then
  121. * do not ignore the fault:
  122. */
  123. if (error_code & PF_INSTR)
  124. return 0;
  125. instr = (void *)convert_ip_to_linear(current, regs);
  126. max_instr = instr + 15;
  127. if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE)
  128. return 0;
  129. while (instr < max_instr) {
  130. unsigned char opcode;
  131. if (probe_kernel_address(instr, opcode))
  132. break;
  133. instr++;
  134. if (!check_prefetch_opcode(regs, instr, opcode, &prefetch))
  135. break;
  136. }
  137. return prefetch;
  138. }
  139. static void
  140. force_sig_info_fault(int si_signo, int si_code, unsigned long address,
  141. struct task_struct *tsk)
  142. {
  143. siginfo_t info;
  144. info.si_signo = si_signo;
  145. info.si_errno = 0;
  146. info.si_code = si_code;
  147. info.si_addr = (void __user *)address;
  148. force_sig_info(si_signo, &info, tsk);
  149. }
  150. DEFINE_SPINLOCK(pgd_lock);
  151. LIST_HEAD(pgd_list);
  152. #ifdef CONFIG_X86_32
  153. static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
  154. {
  155. unsigned index = pgd_index(address);
  156. pgd_t *pgd_k;
  157. pud_t *pud, *pud_k;
  158. pmd_t *pmd, *pmd_k;
  159. pgd += index;
  160. pgd_k = init_mm.pgd + index;
  161. if (!pgd_present(*pgd_k))
  162. return NULL;
  163. /*
  164. * set_pgd(pgd, *pgd_k); here would be useless on PAE
  165. * and redundant with the set_pmd() on non-PAE. As would
  166. * set_pud.
  167. */
  168. pud = pud_offset(pgd, address);
  169. pud_k = pud_offset(pgd_k, address);
  170. if (!pud_present(*pud_k))
  171. return NULL;
  172. pmd = pmd_offset(pud, address);
  173. pmd_k = pmd_offset(pud_k, address);
  174. if (!pmd_present(*pmd_k))
  175. return NULL;
  176. if (!pmd_present(*pmd)) {
  177. set_pmd(pmd, *pmd_k);
  178. arch_flush_lazy_mmu_mode();
  179. } else {
  180. BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
  181. }
  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 "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
  362. KERN_ERR "******* Working around it, but it may cause SEGVs or burn power.\n"
  363. KERN_ERR "******* Please consider a BIOS update.\n"
  364. KERN_ERR "******* Disabling USB legacy in the BIOS may also help.\n";
  365. /*
  366. * No vm86 mode in 64-bit mode:
  367. */
  368. static inline void
  369. check_v8086_mode(struct pt_regs *regs, unsigned long address,
  370. struct task_struct *tsk)
  371. {
  372. }
  373. static int bad_address(void *p)
  374. {
  375. unsigned long dummy;
  376. return probe_kernel_address((unsigned long *)p, dummy);
  377. }
  378. static void dump_pagetable(unsigned long address)
  379. {
  380. pgd_t *pgd;
  381. pud_t *pud;
  382. pmd_t *pmd;
  383. pte_t *pte;
  384. pgd = (pgd_t *)read_cr3();
  385. pgd = __va((unsigned long)pgd & PHYSICAL_PAGE_MASK);
  386. pgd += pgd_index(address);
  387. if (bad_address(pgd))
  388. goto bad;
  389. printk("PGD %lx ", pgd_val(*pgd));
  390. if (!pgd_present(*pgd))
  391. goto out;
  392. pud = pud_offset(pgd, address);
  393. if (bad_address(pud))
  394. goto bad;
  395. printk("PUD %lx ", pud_val(*pud));
  396. if (!pud_present(*pud) || pud_large(*pud))
  397. goto out;
  398. pmd = pmd_offset(pud, address);
  399. if (bad_address(pmd))
  400. goto bad;
  401. printk("PMD %lx ", pmd_val(*pmd));
  402. if (!pmd_present(*pmd) || pmd_large(*pmd))
  403. goto out;
  404. pte = pte_offset_kernel(pmd, address);
  405. if (bad_address(pte))
  406. goto bad;
  407. printk("PTE %lx", pte_val(*pte));
  408. out:
  409. printk("\n");
  410. return;
  411. bad:
  412. printk("BAD\n");
  413. }
  414. #endif /* CONFIG_X86_64 */
  415. /*
  416. * Workaround for K8 erratum #93 & buggy BIOS.
  417. *
  418. * BIOS SMM functions are required to use a specific workaround
  419. * to avoid corruption of the 64bit RIP register on C stepping K8.
  420. *
  421. * A lot of BIOS that didn't get tested properly miss this.
  422. *
  423. * The OS sees this as a page fault with the upper 32bits of RIP cleared.
  424. * Try to work around it here.
  425. *
  426. * Note we only handle faults in kernel here.
  427. * Does nothing on 32-bit.
  428. */
  429. static int is_errata93(struct pt_regs *regs, unsigned long address)
  430. {
  431. #ifdef CONFIG_X86_64
  432. static int once;
  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. if (!once) {
  441. printk(errata93_warning);
  442. once = 1;
  443. }
  444. regs->ip = address;
  445. return 1;
  446. }
  447. #endif
  448. return 0;
  449. }
  450. /*
  451. * Work around K8 erratum #100 K8 in compat mode occasionally jumps
  452. * to illegal addresses >4GB.
  453. *
  454. * We catch this in the page fault handler because these addresses
  455. * are not reachable. Just detect this case and return. Any code
  456. * segment in LDT is compatibility mode.
  457. */
  458. static int is_errata100(struct pt_regs *regs, unsigned long address)
  459. {
  460. #ifdef CONFIG_X86_64
  461. if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32))
  462. return 1;
  463. #endif
  464. return 0;
  465. }
  466. static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
  467. {
  468. #ifdef CONFIG_X86_F00F_BUG
  469. unsigned long nr;
  470. /*
  471. * Pentium F0 0F C7 C8 bug workaround:
  472. */
  473. if (boot_cpu_data.f00f_bug) {
  474. nr = (address - idt_descr.address) >> 3;
  475. if (nr == 6) {
  476. do_invalid_op(regs, 0);
  477. return 1;
  478. }
  479. }
  480. #endif
  481. return 0;
  482. }
  483. static const char nx_warning[] = KERN_CRIT
  484. "kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n";
  485. static void
  486. show_fault_oops(struct pt_regs *regs, unsigned long error_code,
  487. unsigned long address)
  488. {
  489. if (!oops_may_print())
  490. return;
  491. if (error_code & PF_INSTR) {
  492. unsigned int level;
  493. pte_t *pte = lookup_address(address, &level);
  494. if (pte && pte_present(*pte) && !pte_exec(*pte))
  495. printk(nx_warning, current_uid());
  496. }
  497. printk(KERN_ALERT "BUG: unable to handle kernel ");
  498. if (address < PAGE_SIZE)
  499. printk(KERN_CONT "NULL pointer dereference");
  500. else
  501. printk(KERN_CONT "paging request");
  502. printk(KERN_CONT " at %p\n", (void *) address);
  503. printk(KERN_ALERT "IP:");
  504. printk_address(regs->ip, 1);
  505. dump_pagetable(address);
  506. }
  507. static noinline void
  508. pgtable_bad(struct pt_regs *regs, unsigned long error_code,
  509. unsigned long address)
  510. {
  511. struct task_struct *tsk;
  512. unsigned long flags;
  513. int sig;
  514. flags = oops_begin();
  515. tsk = current;
  516. sig = SIGKILL;
  517. printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
  518. tsk->comm, address);
  519. dump_pagetable(address);
  520. tsk->thread.cr2 = address;
  521. tsk->thread.trap_no = 14;
  522. tsk->thread.error_code = error_code;
  523. if (__die("Bad pagetable", regs, error_code))
  524. sig = 0;
  525. oops_end(flags, regs, sig);
  526. }
  527. static noinline void
  528. no_context(struct pt_regs *regs, unsigned long error_code,
  529. unsigned long address)
  530. {
  531. struct task_struct *tsk = current;
  532. unsigned long *stackend;
  533. unsigned long flags;
  534. int sig;
  535. /* Are we prepared to handle this kernel fault? */
  536. if (fixup_exception(regs))
  537. return;
  538. /*
  539. * 32-bit:
  540. *
  541. * Valid to do another page fault here, because if this fault
  542. * had been triggered by is_prefetch fixup_exception would have
  543. * handled it.
  544. *
  545. * 64-bit:
  546. *
  547. * Hall of shame of CPU/BIOS bugs.
  548. */
  549. if (is_prefetch(regs, error_code, address))
  550. return;
  551. if (is_errata93(regs, address))
  552. return;
  553. /*
  554. * Oops. The kernel tried to access some bad page. We'll have to
  555. * terminate things with extreme prejudice:
  556. */
  557. flags = oops_begin();
  558. show_fault_oops(regs, error_code, address);
  559. stackend = end_of_stack(tsk);
  560. if (*stackend != STACK_END_MAGIC)
  561. printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
  562. tsk->thread.cr2 = address;
  563. tsk->thread.trap_no = 14;
  564. tsk->thread.error_code = error_code;
  565. sig = SIGKILL;
  566. if (__die("Oops", regs, error_code))
  567. sig = 0;
  568. /* Executive summary in case the body of the oops scrolled away */
  569. printk(KERN_EMERG "CR2: %016lx\n", address);
  570. oops_end(flags, regs, sig);
  571. }
  572. /*
  573. * Print out info about fatal segfaults, if the show_unhandled_signals
  574. * sysctl is set:
  575. */
  576. static inline void
  577. show_signal_msg(struct pt_regs *regs, unsigned long error_code,
  578. unsigned long address, struct task_struct *tsk)
  579. {
  580. if (!unhandled_signal(tsk, SIGSEGV))
  581. return;
  582. if (!printk_ratelimit())
  583. return;
  584. printk(KERN_CONT "%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
  585. task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
  586. tsk->comm, task_pid_nr(tsk), address,
  587. (void *)regs->ip, (void *)regs->sp, error_code);
  588. print_vma_addr(KERN_CONT " in ", regs->ip);
  589. printk(KERN_CONT "\n");
  590. }
  591. static void
  592. __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
  593. unsigned long address, int si_code)
  594. {
  595. struct task_struct *tsk = current;
  596. /* User mode accesses just cause a SIGSEGV */
  597. if (error_code & PF_USER) {
  598. /*
  599. * It's possible to have interrupts off here:
  600. */
  601. local_irq_enable();
  602. /*
  603. * Valid to do another page fault here because this one came
  604. * from user space:
  605. */
  606. if (is_prefetch(regs, error_code, address))
  607. return;
  608. if (is_errata100(regs, address))
  609. return;
  610. if (unlikely(show_unhandled_signals))
  611. show_signal_msg(regs, error_code, address, tsk);
  612. /* Kernel addresses are always protection faults: */
  613. tsk->thread.cr2 = address;
  614. tsk->thread.error_code = error_code | (address >= TASK_SIZE);
  615. tsk->thread.trap_no = 14;
  616. force_sig_info_fault(SIGSEGV, si_code, address, tsk);
  617. return;
  618. }
  619. if (is_f00f_bug(regs, address))
  620. return;
  621. no_context(regs, error_code, address);
  622. }
  623. static noinline void
  624. bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
  625. unsigned long address)
  626. {
  627. __bad_area_nosemaphore(regs, error_code, address, SEGV_MAPERR);
  628. }
  629. static void
  630. __bad_area(struct pt_regs *regs, unsigned long error_code,
  631. unsigned long address, int si_code)
  632. {
  633. struct mm_struct *mm = current->mm;
  634. /*
  635. * Something tried to access memory that isn't in our memory map..
  636. * Fix it, but check if it's kernel or user first..
  637. */
  638. up_read(&mm->mmap_sem);
  639. __bad_area_nosemaphore(regs, error_code, address, si_code);
  640. }
  641. static noinline void
  642. bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
  643. {
  644. __bad_area(regs, error_code, address, SEGV_MAPERR);
  645. }
  646. static noinline void
  647. bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
  648. unsigned long address)
  649. {
  650. __bad_area(regs, error_code, address, SEGV_ACCERR);
  651. }
  652. /* TODO: fixup for "mm-invoke-oom-killer-from-page-fault.patch" */
  653. static void
  654. out_of_memory(struct pt_regs *regs, unsigned long error_code,
  655. unsigned long address)
  656. {
  657. /*
  658. * We ran out of memory, call the OOM killer, and return the userspace
  659. * (which will retry the fault, or kill us if we got oom-killed):
  660. */
  661. up_read(&current->mm->mmap_sem);
  662. pagefault_out_of_memory();
  663. }
  664. static void
  665. do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address)
  666. {
  667. struct task_struct *tsk = current;
  668. struct mm_struct *mm = tsk->mm;
  669. up_read(&mm->mmap_sem);
  670. /* Kernel mode? Handle exceptions or die: */
  671. if (!(error_code & PF_USER))
  672. no_context(regs, error_code, address);
  673. /* User-space => ok to do another page fault: */
  674. if (is_prefetch(regs, error_code, address))
  675. return;
  676. tsk->thread.cr2 = address;
  677. tsk->thread.error_code = error_code;
  678. tsk->thread.trap_no = 14;
  679. force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk);
  680. }
  681. static noinline void
  682. mm_fault_error(struct pt_regs *regs, unsigned long error_code,
  683. unsigned long address, unsigned int fault)
  684. {
  685. if (fault & VM_FAULT_OOM) {
  686. out_of_memory(regs, error_code, address);
  687. } else {
  688. if (fault & VM_FAULT_SIGBUS)
  689. do_sigbus(regs, error_code, address);
  690. else
  691. BUG();
  692. }
  693. }
  694. static int spurious_fault_check(unsigned long error_code, pte_t *pte)
  695. {
  696. if ((error_code & PF_WRITE) && !pte_write(*pte))
  697. return 0;
  698. if ((error_code & PF_INSTR) && !pte_exec(*pte))
  699. return 0;
  700. return 1;
  701. }
  702. /*
  703. * Handle a spurious fault caused by a stale TLB entry.
  704. *
  705. * This allows us to lazily refresh the TLB when increasing the
  706. * permissions of a kernel page (RO -> RW or NX -> X). Doing it
  707. * eagerly is very expensive since that implies doing a full
  708. * cross-processor TLB flush, even if no stale TLB entries exist
  709. * on other processors.
  710. *
  711. * There are no security implications to leaving a stale TLB when
  712. * increasing the permissions on a page.
  713. */
  714. static noinline int
  715. spurious_fault(unsigned long error_code, unsigned long address)
  716. {
  717. pgd_t *pgd;
  718. pud_t *pud;
  719. pmd_t *pmd;
  720. pte_t *pte;
  721. int ret;
  722. /* Reserved-bit violation or user access to kernel space? */
  723. if (error_code & (PF_USER | PF_RSVD))
  724. return 0;
  725. pgd = init_mm.pgd + pgd_index(address);
  726. if (!pgd_present(*pgd))
  727. return 0;
  728. pud = pud_offset(pgd, address);
  729. if (!pud_present(*pud))
  730. return 0;
  731. if (pud_large(*pud))
  732. return spurious_fault_check(error_code, (pte_t *) pud);
  733. pmd = pmd_offset(pud, address);
  734. if (!pmd_present(*pmd))
  735. return 0;
  736. if (pmd_large(*pmd))
  737. return spurious_fault_check(error_code, (pte_t *) pmd);
  738. pte = pte_offset_kernel(pmd, address);
  739. if (!pte_present(*pte))
  740. return 0;
  741. ret = spurious_fault_check(error_code, pte);
  742. if (!ret)
  743. return 0;
  744. /*
  745. * Make sure we have permissions in PMD.
  746. * If not, then there's a bug in the page tables:
  747. */
  748. ret = spurious_fault_check(error_code, (pte_t *) pmd);
  749. WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
  750. return ret;
  751. }
  752. int show_unhandled_signals = 1;
  753. static inline int
  754. access_error(unsigned long error_code, int write, struct vm_area_struct *vma)
  755. {
  756. if (write) {
  757. /* write, present and write, not present: */
  758. if (unlikely(!(vma->vm_flags & VM_WRITE)))
  759. return 1;
  760. return 0;
  761. }
  762. /* read, present: */
  763. if (unlikely(error_code & PF_PROT))
  764. return 1;
  765. /* read, not present: */
  766. if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
  767. return 1;
  768. return 0;
  769. }
  770. static int fault_in_kernel_space(unsigned long address)
  771. {
  772. return address >= TASK_SIZE_MAX;
  773. }
  774. /*
  775. * This routine handles page faults. It determines the address,
  776. * and the problem, and then passes it off to one of the appropriate
  777. * routines.
  778. */
  779. dotraplinkage void __kprobes
  780. do_page_fault(struct pt_regs *regs, unsigned long error_code)
  781. {
  782. struct vm_area_struct *vma;
  783. struct task_struct *tsk;
  784. unsigned long address;
  785. struct mm_struct *mm;
  786. int write;
  787. int fault;
  788. tsk = current;
  789. mm = tsk->mm;
  790. prefetchw(&mm->mmap_sem);
  791. /* Get the faulting address: */
  792. address = read_cr2();
  793. if (unlikely(kmmio_fault(regs, address)))
  794. return;
  795. /*
  796. * We fault-in kernel-space virtual memory on-demand. The
  797. * 'reference' page table is init_mm.pgd.
  798. *
  799. * NOTE! We MUST NOT take any locks for this case. We may
  800. * be in an interrupt or a critical region, and should
  801. * only copy the information from the master page table,
  802. * nothing more.
  803. *
  804. * This verifies that the fault happens in kernel space
  805. * (error_code & 4) == 0, and that the fault was not a
  806. * protection error (error_code & 9) == 0.
  807. */
  808. if (unlikely(fault_in_kernel_space(address))) {
  809. if (!(error_code & (PF_RSVD|PF_USER|PF_PROT)) &&
  810. vmalloc_fault(address) >= 0)
  811. return;
  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. /*
  845. * If we're in an interrupt, have no user context or are running
  846. * in an atomic region then we must not take the fault:
  847. */
  848. if (unlikely(in_atomic() || !mm)) {
  849. bad_area_nosemaphore(regs, error_code, address);
  850. return;
  851. }
  852. /*
  853. * When running in the kernel we expect faults to occur only to
  854. * addresses in user space. All other faults represent errors in
  855. * the kernel and should generate an OOPS. Unfortunately, in the
  856. * case of an erroneous fault occurring in a code path which already
  857. * holds mmap_sem we will deadlock attempting to validate the fault
  858. * against the address space. Luckily the kernel only validly
  859. * references user space from well defined areas of code, which are
  860. * listed in the exceptions table.
  861. *
  862. * As the vast majority of faults will be valid we will only perform
  863. * the source reference check when there is a possibility of a
  864. * deadlock. Attempt to lock the address space, if we cannot we then
  865. * validate the source. If this is invalid we can skip the address
  866. * space check, thus avoiding the deadlock:
  867. */
  868. if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
  869. if ((error_code & PF_USER) == 0 &&
  870. !search_exception_tables(regs->ip)) {
  871. bad_area_nosemaphore(regs, error_code, address);
  872. return;
  873. }
  874. down_read(&mm->mmap_sem);
  875. } else {
  876. /*
  877. * The above down_read_trylock() might have succeeded in
  878. * which case we'll have missed the might_sleep() from
  879. * down_read():
  880. */
  881. might_sleep();
  882. }
  883. vma = find_vma(mm, address);
  884. if (unlikely(!vma)) {
  885. bad_area(regs, error_code, address);
  886. return;
  887. }
  888. if (likely(vma->vm_start <= address))
  889. goto good_area;
  890. if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
  891. bad_area(regs, error_code, address);
  892. return;
  893. }
  894. if (error_code & PF_USER) {
  895. /*
  896. * Accessing the stack below %sp is always a bug.
  897. * The large cushion allows instructions like enter
  898. * and pusha to work. ("enter $65535, $31" pushes
  899. * 32 pointers and then decrements %sp by 65535.)
  900. */
  901. if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
  902. bad_area(regs, error_code, address);
  903. return;
  904. }
  905. }
  906. if (unlikely(expand_stack(vma, address))) {
  907. bad_area(regs, error_code, address);
  908. return;
  909. }
  910. /*
  911. * Ok, we have a good vm_area for this memory access, so
  912. * we can handle it..
  913. */
  914. good_area:
  915. write = error_code & PF_WRITE;
  916. if (unlikely(access_error(error_code, write, vma))) {
  917. bad_area_access_error(regs, error_code, address);
  918. return;
  919. }
  920. /*
  921. * If for any reason at all we couldn't handle the fault,
  922. * make sure we exit gracefully rather than endlessly redo
  923. * the fault:
  924. */
  925. fault = handle_mm_fault(mm, vma, address, write);
  926. if (unlikely(fault & VM_FAULT_ERROR)) {
  927. mm_fault_error(regs, error_code, address, fault);
  928. return;
  929. }
  930. if (fault & VM_FAULT_MAJOR)
  931. tsk->maj_flt++;
  932. else
  933. tsk->min_flt++;
  934. check_v8086_mode(regs, address, tsk);
  935. up_read(&mm->mmap_sem);
  936. }