fault.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  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. int ret;
  572. /* Reserved-bit violation or user access to kernel space? */
  573. if (error_code & (PF_USER | PF_RSVD))
  574. return 0;
  575. pgd = init_mm.pgd + pgd_index(address);
  576. if (!pgd_present(*pgd))
  577. return 0;
  578. pud = pud_offset(pgd, address);
  579. if (!pud_present(*pud))
  580. return 0;
  581. if (pud_large(*pud))
  582. return spurious_fault_check(error_code, (pte_t *) pud);
  583. pmd = pmd_offset(pud, address);
  584. if (!pmd_present(*pmd))
  585. return 0;
  586. if (pmd_large(*pmd))
  587. return spurious_fault_check(error_code, (pte_t *) pmd);
  588. pte = pte_offset_kernel(pmd, address);
  589. if (!pte_present(*pte))
  590. return 0;
  591. ret = spurious_fault_check(error_code, pte);
  592. if (!ret)
  593. return 0;
  594. /*
  595. * Make sure we have permissions in PMD
  596. * If not, then there's a bug in the page tables.
  597. */
  598. ret = spurious_fault_check(error_code, (pte_t *) pmd);
  599. WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
  600. return ret;
  601. }
  602. /*
  603. * X86_32
  604. * Handle a fault on the vmalloc or module mapping area
  605. *
  606. * X86_64
  607. * Handle a fault on the vmalloc area
  608. *
  609. * This assumes no large pages in there.
  610. */
  611. static noinline int vmalloc_fault(unsigned long address)
  612. {
  613. #ifdef CONFIG_X86_32
  614. unsigned long pgd_paddr;
  615. pmd_t *pmd_k;
  616. pte_t *pte_k;
  617. /* Make sure we are in vmalloc area */
  618. if (!(address >= VMALLOC_START && address < VMALLOC_END))
  619. return -1;
  620. /*
  621. * Synchronize this task's top level page-table
  622. * with the 'reference' page table.
  623. *
  624. * Do _not_ use "current" here. We might be inside
  625. * an interrupt in the middle of a task switch..
  626. */
  627. pgd_paddr = read_cr3();
  628. pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
  629. if (!pmd_k)
  630. return -1;
  631. pte_k = pte_offset_kernel(pmd_k, address);
  632. if (!pte_present(*pte_k))
  633. return -1;
  634. return 0;
  635. #else
  636. pgd_t *pgd, *pgd_ref;
  637. pud_t *pud, *pud_ref;
  638. pmd_t *pmd, *pmd_ref;
  639. pte_t *pte, *pte_ref;
  640. /* Make sure we are in vmalloc area */
  641. if (!(address >= VMALLOC_START && address < VMALLOC_END))
  642. return -1;
  643. /* Copy kernel mappings over when needed. This can also
  644. happen within a race in page table update. In the later
  645. case just flush. */
  646. pgd = pgd_offset(current->active_mm, address);
  647. pgd_ref = pgd_offset_k(address);
  648. if (pgd_none(*pgd_ref))
  649. return -1;
  650. if (pgd_none(*pgd))
  651. set_pgd(pgd, *pgd_ref);
  652. else
  653. BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
  654. /* Below here mismatches are bugs because these lower tables
  655. are shared */
  656. pud = pud_offset(pgd, address);
  657. pud_ref = pud_offset(pgd_ref, address);
  658. if (pud_none(*pud_ref))
  659. return -1;
  660. if (pud_none(*pud) || pud_page_vaddr(*pud) != pud_page_vaddr(*pud_ref))
  661. BUG();
  662. pmd = pmd_offset(pud, address);
  663. pmd_ref = pmd_offset(pud_ref, address);
  664. if (pmd_none(*pmd_ref))
  665. return -1;
  666. if (pmd_none(*pmd) || pmd_page(*pmd) != pmd_page(*pmd_ref))
  667. BUG();
  668. pte_ref = pte_offset_kernel(pmd_ref, address);
  669. if (!pte_present(*pte_ref))
  670. return -1;
  671. pte = pte_offset_kernel(pmd, address);
  672. /* Don't use pte_page here, because the mappings can point
  673. outside mem_map, and the NUMA hash lookup cannot handle
  674. that. */
  675. if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
  676. BUG();
  677. return 0;
  678. #endif
  679. }
  680. int show_unhandled_signals = 1;
  681. static inline int access_error(unsigned long error_code, int write,
  682. struct vm_area_struct *vma)
  683. {
  684. if (write) {
  685. /* write, present and write, not present */
  686. if (unlikely(!(vma->vm_flags & VM_WRITE)))
  687. return 1;
  688. } else if (unlikely(error_code & PF_PROT)) {
  689. /* read, present */
  690. return 1;
  691. } else {
  692. /* read, not present */
  693. if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
  694. return 1;
  695. }
  696. return 0;
  697. }
  698. static int fault_in_kernel_space(unsigned long address)
  699. {
  700. #ifdef CONFIG_X86_32
  701. return address >= TASK_SIZE;
  702. #else /* !CONFIG_X86_32 */
  703. return address >= TASK_SIZE64;
  704. #endif /* CONFIG_X86_32 */
  705. }
  706. /*
  707. * This routine handles page faults. It determines the address,
  708. * and the problem, and then passes it off to one of the appropriate
  709. * routines.
  710. */
  711. #ifdef CONFIG_X86_64
  712. asmlinkage
  713. #endif
  714. void __kprobes do_page_fault(struct pt_regs *regs, unsigned long error_code)
  715. {
  716. unsigned long address;
  717. struct task_struct *tsk;
  718. struct mm_struct *mm;
  719. struct vm_area_struct *vma;
  720. int write;
  721. int fault;
  722. tsk = current;
  723. mm = tsk->mm;
  724. prefetchw(&mm->mmap_sem);
  725. /* get the address */
  726. address = read_cr2();
  727. if (unlikely(kmmio_fault(regs, address)))
  728. return;
  729. /*
  730. * We fault-in kernel-space virtual memory on-demand. The
  731. * 'reference' page table is init_mm.pgd.
  732. *
  733. * NOTE! We MUST NOT take any locks for this case. We may
  734. * be in an interrupt or a critical region, and should
  735. * only copy the information from the master page table,
  736. * nothing more.
  737. *
  738. * This verifies that the fault happens in kernel space
  739. * (error_code & 4) == 0, and that the fault was not a
  740. * protection error (error_code & 9) == 0.
  741. */
  742. if (unlikely(fault_in_kernel_space(address))) {
  743. if (!(error_code & (PF_RSVD|PF_USER|PF_PROT)) &&
  744. vmalloc_fault(address) >= 0)
  745. return;
  746. /* Can handle a stale RO->RW TLB */
  747. if (spurious_fault(error_code, address))
  748. return;
  749. /* kprobes don't want to hook the spurious faults. */
  750. if (notify_page_fault(regs))
  751. return;
  752. /*
  753. * Don't take the mm semaphore here. If we fixup a prefetch
  754. * fault we could otherwise deadlock.
  755. */
  756. bad_area_nosemaphore(regs, error_code, address);
  757. return;
  758. }
  759. /* kprobes don't want to hook the spurious faults. */
  760. if (unlikely(notify_page_fault(regs)))
  761. return;
  762. /*
  763. * It's safe to allow irq's after cr2 has been saved and the
  764. * vmalloc fault has been handled.
  765. *
  766. * User-mode registers count as a user access even for any
  767. * potential system fault or CPU buglet.
  768. */
  769. if (user_mode_vm(regs)) {
  770. local_irq_enable();
  771. error_code |= PF_USER;
  772. } else if (regs->flags & X86_EFLAGS_IF)
  773. local_irq_enable();
  774. #ifdef CONFIG_X86_64
  775. if (unlikely(error_code & PF_RSVD))
  776. pgtable_bad(regs, error_code, address);
  777. #endif
  778. /*
  779. * If we're in an interrupt, have no user context or are running in an
  780. * atomic region then we must not take the fault.
  781. */
  782. if (unlikely(in_atomic() || !mm)) {
  783. bad_area_nosemaphore(regs, error_code, address);
  784. return;
  785. }
  786. /*
  787. * When running in the kernel we expect faults to occur only to
  788. * addresses in user space. All other faults represent errors in the
  789. * kernel and should generate an OOPS. Unfortunately, in the case of an
  790. * erroneous fault occurring in a code path which already holds mmap_sem
  791. * we will deadlock attempting to validate the fault against the
  792. * address space. Luckily the kernel only validly references user
  793. * space from well defined areas of code, which are listed in the
  794. * exceptions table.
  795. *
  796. * As the vast majority of faults will be valid we will only perform
  797. * the source reference check when there is a possibility of a deadlock.
  798. * Attempt to lock the address space, if we cannot we then validate the
  799. * source. If this is invalid we can skip the address space check,
  800. * thus avoiding the deadlock.
  801. */
  802. if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
  803. if ((error_code & PF_USER) == 0 &&
  804. !search_exception_tables(regs->ip)) {
  805. bad_area_nosemaphore(regs, error_code, address);
  806. return;
  807. }
  808. down_read(&mm->mmap_sem);
  809. } else {
  810. /*
  811. * The above down_read_trylock() might have succeeded in which
  812. * case we'll have missed the might_sleep() from down_read().
  813. */
  814. might_sleep();
  815. }
  816. vma = find_vma(mm, address);
  817. if (unlikely(!vma)) {
  818. bad_area(regs, error_code, address);
  819. return;
  820. }
  821. if (likely(vma->vm_start <= address))
  822. goto good_area;
  823. if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
  824. bad_area(regs, error_code, address);
  825. return;
  826. }
  827. if (error_code & PF_USER) {
  828. /*
  829. * Accessing the stack below %sp is always a bug.
  830. * The large cushion allows instructions like enter
  831. * and pusha to work. ("enter $65535,$31" pushes
  832. * 32 pointers and then decrements %sp by 65535.)
  833. */
  834. if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
  835. bad_area(regs, error_code, address);
  836. return;
  837. }
  838. }
  839. if (unlikely(expand_stack(vma, address))) {
  840. bad_area(regs, error_code, address);
  841. return;
  842. }
  843. /*
  844. * Ok, we have a good vm_area for this memory access, so
  845. * we can handle it..
  846. */
  847. good_area:
  848. write = error_code & PF_WRITE;
  849. if (unlikely(access_error(error_code, write, vma))) {
  850. bad_area_access_error(regs, error_code, address);
  851. return;
  852. }
  853. /*
  854. * If for any reason at all we couldn't handle the fault,
  855. * make sure we exit gracefully rather than endlessly redo
  856. * the fault.
  857. */
  858. fault = handle_mm_fault(mm, vma, address, write);
  859. if (unlikely(fault & VM_FAULT_ERROR)) {
  860. mm_fault_error(regs, error_code, address, fault);
  861. return;
  862. }
  863. if (fault & VM_FAULT_MAJOR)
  864. tsk->maj_flt++;
  865. else
  866. tsk->min_flt++;
  867. #ifdef CONFIG_X86_32
  868. /*
  869. * Did it hit the DOS screen memory VA from vm86 mode?
  870. */
  871. if (v8086_mode(regs)) {
  872. unsigned long bit = (address - 0xA0000) >> PAGE_SHIFT;
  873. if (bit < 32)
  874. tsk->thread.screen_bitmap |= 1 << bit;
  875. }
  876. #endif
  877. up_read(&mm->mmap_sem);
  878. }
  879. DEFINE_SPINLOCK(pgd_lock);
  880. LIST_HEAD(pgd_list);
  881. void vmalloc_sync_all(void)
  882. {
  883. unsigned long address;
  884. #ifdef CONFIG_X86_32
  885. if (SHARED_KERNEL_PMD)
  886. return;
  887. for (address = VMALLOC_START & PMD_MASK;
  888. address >= TASK_SIZE && address < FIXADDR_TOP;
  889. address += PMD_SIZE) {
  890. unsigned long flags;
  891. struct page *page;
  892. spin_lock_irqsave(&pgd_lock, flags);
  893. list_for_each_entry(page, &pgd_list, lru) {
  894. if (!vmalloc_sync_one(page_address(page),
  895. address))
  896. break;
  897. }
  898. spin_unlock_irqrestore(&pgd_lock, flags);
  899. }
  900. #else /* CONFIG_X86_64 */
  901. for (address = VMALLOC_START & PGDIR_MASK; address <= VMALLOC_END;
  902. address += PGDIR_SIZE) {
  903. const pgd_t *pgd_ref = pgd_offset_k(address);
  904. unsigned long flags;
  905. struct page *page;
  906. if (pgd_none(*pgd_ref))
  907. continue;
  908. spin_lock_irqsave(&pgd_lock, flags);
  909. list_for_each_entry(page, &pgd_list, lru) {
  910. pgd_t *pgd;
  911. pgd = (pgd_t *)page_address(page) + pgd_index(address);
  912. if (pgd_none(*pgd))
  913. set_pgd(pgd, *pgd_ref);
  914. else
  915. BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
  916. }
  917. spin_unlock_irqrestore(&pgd_lock, flags);
  918. }
  919. #endif
  920. }