fault.c 27 KB

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