fault.c 27 KB

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