fault.c 26 KB

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