fault.c 26 KB

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