fault.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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 = current;
  379. tsk->thread.cr2 = address;
  380. tsk->thread.trap_no = 14;
  381. tsk->thread.error_code = error_code;
  382. if (__die("Bad pagetable", regs, error_code))
  383. sig = 0;
  384. oops_end(flags, regs, sig);
  385. }
  386. #endif
  387. static noinline void no_context(struct pt_regs *regs,
  388. unsigned long error_code, unsigned long address)
  389. {
  390. struct task_struct *tsk = current;
  391. unsigned long *stackend;
  392. #ifdef CONFIG_X86_64
  393. unsigned long flags;
  394. int sig;
  395. #endif
  396. /* Are we prepared to handle this kernel fault? */
  397. if (fixup_exception(regs))
  398. return;
  399. /*
  400. * X86_32
  401. * Valid to do another page fault here, because if this fault
  402. * had been triggered by is_prefetch fixup_exception would have
  403. * handled it.
  404. *
  405. * X86_64
  406. * Hall of shame of CPU/BIOS bugs.
  407. */
  408. if (is_prefetch(regs, error_code, address))
  409. return;
  410. if (is_errata93(regs, address))
  411. return;
  412. /*
  413. * Oops. The kernel tried to access some bad page. We'll have to
  414. * terminate things with extreme prejudice.
  415. */
  416. #ifdef CONFIG_X86_32
  417. bust_spinlocks(1);
  418. #else
  419. flags = oops_begin();
  420. #endif
  421. show_fault_oops(regs, error_code, address);
  422. stackend = end_of_stack(tsk);
  423. if (*stackend != STACK_END_MAGIC)
  424. printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
  425. tsk->thread.cr2 = address;
  426. tsk->thread.trap_no = 14;
  427. tsk->thread.error_code = error_code;
  428. #ifdef CONFIG_X86_32
  429. die("Oops", regs, error_code);
  430. bust_spinlocks(0);
  431. do_exit(SIGKILL);
  432. #else
  433. sig = SIGKILL;
  434. if (__die("Oops", regs, error_code))
  435. sig = 0;
  436. /* Executive summary in case the body of the oops scrolled away */
  437. printk(KERN_EMERG "CR2: %016lx\n", address);
  438. oops_end(flags, regs, sig);
  439. #endif
  440. }
  441. static void __bad_area_nosemaphore(struct pt_regs *regs,
  442. unsigned long error_code, unsigned long address,
  443. int si_code)
  444. {
  445. struct task_struct *tsk = current;
  446. /* User mode accesses just cause a SIGSEGV */
  447. if (error_code & PF_USER) {
  448. /*
  449. * It's possible to have interrupts off here.
  450. */
  451. local_irq_enable();
  452. /*
  453. * Valid to do another page fault here because this one came
  454. * from user space.
  455. */
  456. if (is_prefetch(regs, error_code, address))
  457. return;
  458. if (is_errata100(regs, address))
  459. return;
  460. if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
  461. printk_ratelimit()) {
  462. printk(
  463. "%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
  464. task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
  465. tsk->comm, task_pid_nr(tsk), address,
  466. (void *) regs->ip, (void *) regs->sp, error_code);
  467. print_vma_addr(" in ", regs->ip);
  468. printk("\n");
  469. }
  470. tsk->thread.cr2 = address;
  471. /* Kernel addresses are always protection faults */
  472. tsk->thread.error_code = error_code | (address >= TASK_SIZE);
  473. tsk->thread.trap_no = 14;
  474. force_sig_info_fault(SIGSEGV, si_code, address, tsk);
  475. return;
  476. }
  477. if (is_f00f_bug(regs, address))
  478. return;
  479. no_context(regs, error_code, address);
  480. }
  481. static noinline void bad_area_nosemaphore(struct pt_regs *regs,
  482. unsigned long error_code, unsigned long address)
  483. {
  484. __bad_area_nosemaphore(regs, error_code, address, SEGV_MAPERR);
  485. }
  486. static void __bad_area(struct pt_regs *regs,
  487. unsigned long error_code, unsigned long address,
  488. int si_code)
  489. {
  490. struct mm_struct *mm = current->mm;
  491. /*
  492. * Something tried to access memory that isn't in our memory map..
  493. * Fix it, but check if it's kernel or user first..
  494. */
  495. up_read(&mm->mmap_sem);
  496. __bad_area_nosemaphore(regs, error_code, address, si_code);
  497. }
  498. static noinline void bad_area(struct pt_regs *regs,
  499. unsigned long error_code, unsigned long address)
  500. {
  501. __bad_area(regs, error_code, address, SEGV_MAPERR);
  502. }
  503. static noinline void bad_area_access_error(struct pt_regs *regs,
  504. unsigned long error_code, unsigned long address)
  505. {
  506. __bad_area(regs, error_code, address, SEGV_ACCERR);
  507. }
  508. /* TODO: fixup for "mm-invoke-oom-killer-from-page-fault.patch" */
  509. static void out_of_memory(struct pt_regs *regs,
  510. unsigned long error_code, unsigned long address)
  511. {
  512. /*
  513. * We ran out of memory, call the OOM killer, and return the userspace
  514. * (which will retry the fault, or kill us if we got oom-killed).
  515. */
  516. up_read(&current->mm->mmap_sem);
  517. pagefault_out_of_memory();
  518. }
  519. static void do_sigbus(struct pt_regs *regs,
  520. unsigned long error_code, unsigned long address)
  521. {
  522. struct task_struct *tsk = current;
  523. struct mm_struct *mm = tsk->mm;
  524. up_read(&mm->mmap_sem);
  525. /* Kernel mode? Handle exceptions or die */
  526. if (!(error_code & PF_USER))
  527. no_context(regs, error_code, address);
  528. #ifdef CONFIG_X86_32
  529. /* User space => ok to do another page fault */
  530. if (is_prefetch(regs, error_code, address))
  531. return;
  532. #endif
  533. tsk->thread.cr2 = address;
  534. tsk->thread.error_code = error_code;
  535. tsk->thread.trap_no = 14;
  536. force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk);
  537. }
  538. static noinline void mm_fault_error(struct pt_regs *regs,
  539. unsigned long error_code, unsigned long address, unsigned int fault)
  540. {
  541. if (fault & VM_FAULT_OOM)
  542. out_of_memory(regs, error_code, address);
  543. else if (fault & VM_FAULT_SIGBUS)
  544. do_sigbus(regs, error_code, address);
  545. else
  546. BUG();
  547. }
  548. static int spurious_fault_check(unsigned long error_code, pte_t *pte)
  549. {
  550. if ((error_code & PF_WRITE) && !pte_write(*pte))
  551. return 0;
  552. if ((error_code & PF_INSTR) && !pte_exec(*pte))
  553. return 0;
  554. return 1;
  555. }
  556. /*
  557. * Handle a spurious fault caused by a stale TLB entry. This allows
  558. * us to lazily refresh the TLB when increasing the permissions of a
  559. * kernel page (RO -> RW or NX -> X). Doing it eagerly is very
  560. * expensive since that implies doing a full cross-processor TLB
  561. * flush, even if no stale TLB entries exist on other processors.
  562. * There are no security implications to leaving a stale TLB when
  563. * increasing the permissions on a page.
  564. */
  565. static noinline int spurious_fault(unsigned long error_code,
  566. unsigned long address)
  567. {
  568. pgd_t *pgd;
  569. pud_t *pud;
  570. pmd_t *pmd;
  571. pte_t *pte;
  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. return spurious_fault_check(error_code, pte);
  592. }
  593. /*
  594. * X86_32
  595. * Handle a fault on the vmalloc or module mapping area
  596. *
  597. * X86_64
  598. * Handle a fault on the vmalloc area
  599. *
  600. * This assumes no large pages in there.
  601. */
  602. static noinline int vmalloc_fault(unsigned long address)
  603. {
  604. #ifdef CONFIG_X86_32
  605. unsigned long pgd_paddr;
  606. pmd_t *pmd_k;
  607. pte_t *pte_k;
  608. /* Make sure we are in vmalloc area */
  609. if (!(address >= VMALLOC_START && address < VMALLOC_END))
  610. return -1;
  611. /*
  612. * Synchronize this task's top level page-table
  613. * with the 'reference' page table.
  614. *
  615. * Do _not_ use "current" here. We might be inside
  616. * an interrupt in the middle of a task switch..
  617. */
  618. pgd_paddr = read_cr3();
  619. pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
  620. if (!pmd_k)
  621. return -1;
  622. pte_k = pte_offset_kernel(pmd_k, address);
  623. if (!pte_present(*pte_k))
  624. return -1;
  625. return 0;
  626. #else
  627. pgd_t *pgd, *pgd_ref;
  628. pud_t *pud, *pud_ref;
  629. pmd_t *pmd, *pmd_ref;
  630. pte_t *pte, *pte_ref;
  631. /* Make sure we are in vmalloc area */
  632. if (!(address >= VMALLOC_START && address < VMALLOC_END))
  633. return -1;
  634. /* Copy kernel mappings over when needed. This can also
  635. happen within a race in page table update. In the later
  636. case just flush. */
  637. pgd = pgd_offset(current->active_mm, address);
  638. pgd_ref = pgd_offset_k(address);
  639. if (pgd_none(*pgd_ref))
  640. return -1;
  641. if (pgd_none(*pgd))
  642. set_pgd(pgd, *pgd_ref);
  643. else
  644. BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
  645. /* Below here mismatches are bugs because these lower tables
  646. are shared */
  647. pud = pud_offset(pgd, address);
  648. pud_ref = pud_offset(pgd_ref, address);
  649. if (pud_none(*pud_ref))
  650. return -1;
  651. if (pud_none(*pud) || pud_page_vaddr(*pud) != pud_page_vaddr(*pud_ref))
  652. BUG();
  653. pmd = pmd_offset(pud, address);
  654. pmd_ref = pmd_offset(pud_ref, address);
  655. if (pmd_none(*pmd_ref))
  656. return -1;
  657. if (pmd_none(*pmd) || pmd_page(*pmd) != pmd_page(*pmd_ref))
  658. BUG();
  659. pte_ref = pte_offset_kernel(pmd_ref, address);
  660. if (!pte_present(*pte_ref))
  661. return -1;
  662. pte = pte_offset_kernel(pmd, address);
  663. /* Don't use pte_page here, because the mappings can point
  664. outside mem_map, and the NUMA hash lookup cannot handle
  665. that. */
  666. if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
  667. BUG();
  668. return 0;
  669. #endif
  670. }
  671. int show_unhandled_signals = 1;
  672. static inline int access_error(unsigned long error_code, int write,
  673. struct vm_area_struct *vma)
  674. {
  675. if (write) {
  676. /* write, present and write, not present */
  677. if (unlikely(!(vma->vm_flags & VM_WRITE)))
  678. return 1;
  679. } else if (unlikely(error_code & PF_PROT)) {
  680. /* read, present */
  681. return 1;
  682. } else {
  683. /* read, not present */
  684. if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
  685. return 1;
  686. }
  687. return 0;
  688. }
  689. /*
  690. * This routine handles page faults. It determines the address,
  691. * and the problem, and then passes it off to one of the appropriate
  692. * routines.
  693. */
  694. #ifdef CONFIG_X86_64
  695. asmlinkage
  696. #endif
  697. void __kprobes do_page_fault(struct pt_regs *regs, unsigned long error_code)
  698. {
  699. unsigned long address;
  700. struct task_struct *tsk;
  701. struct mm_struct *mm;
  702. struct vm_area_struct *vma;
  703. int write;
  704. int fault;
  705. tsk = current;
  706. mm = tsk->mm;
  707. prefetchw(&mm->mmap_sem);
  708. /* get the address */
  709. address = read_cr2();
  710. if (unlikely(notify_page_fault(regs)))
  711. return;
  712. if (unlikely(kmmio_fault(regs, address)))
  713. return;
  714. /*
  715. * We fault-in kernel-space virtual memory on-demand. The
  716. * 'reference' page table is init_mm.pgd.
  717. *
  718. * NOTE! We MUST NOT take any locks for this case. We may
  719. * be in an interrupt or a critical region, and should
  720. * only copy the information from the master page table,
  721. * nothing more.
  722. *
  723. * This verifies that the fault happens in kernel space
  724. * (error_code & 4) == 0, and that the fault was not a
  725. * protection error (error_code & 9) == 0.
  726. */
  727. #ifdef CONFIG_X86_32
  728. if (unlikely(address >= TASK_SIZE)) {
  729. #else
  730. if (unlikely(address >= TASK_SIZE64)) {
  731. #endif
  732. if (!(error_code & (PF_RSVD|PF_USER|PF_PROT)) &&
  733. vmalloc_fault(address) >= 0)
  734. return;
  735. /* Can handle a stale RO->RW TLB */
  736. if (spurious_fault(error_code, address))
  737. return;
  738. /*
  739. * Don't take the mm semaphore here. If we fixup a prefetch
  740. * fault we could otherwise deadlock.
  741. */
  742. bad_area_nosemaphore(regs, error_code, address);
  743. return;
  744. }
  745. /*
  746. * It's safe to allow irq's after cr2 has been saved and the
  747. * vmalloc fault has been handled.
  748. *
  749. * User-mode registers count as a user access even for any
  750. * potential system fault or CPU buglet.
  751. */
  752. if (user_mode_vm(regs)) {
  753. local_irq_enable();
  754. error_code |= PF_USER;
  755. } else if (regs->flags & X86_EFLAGS_IF)
  756. local_irq_enable();
  757. #ifdef CONFIG_X86_64
  758. if (unlikely(error_code & PF_RSVD))
  759. pgtable_bad(regs, error_code, address);
  760. #endif
  761. /*
  762. * If we're in an interrupt, have no user context or are running in an
  763. * atomic region then we must not take the fault.
  764. */
  765. if (unlikely(in_atomic() || !mm)) {
  766. bad_area_nosemaphore(regs, error_code, address);
  767. return;
  768. }
  769. /*
  770. * When running in the kernel we expect faults to occur only to
  771. * addresses in user space. All other faults represent errors in the
  772. * kernel and should generate an OOPS. Unfortunately, in the case of an
  773. * erroneous fault occurring in a code path which already holds mmap_sem
  774. * we will deadlock attempting to validate the fault against the
  775. * address space. Luckily the kernel only validly references user
  776. * space from well defined areas of code, which are listed in the
  777. * exceptions table.
  778. *
  779. * As the vast majority of faults will be valid we will only perform
  780. * the source reference check when there is a possibility of a deadlock.
  781. * Attempt to lock the address space, if we cannot we then validate the
  782. * source. If this is invalid we can skip the address space check,
  783. * thus avoiding the deadlock.
  784. */
  785. if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
  786. if ((error_code & PF_USER) == 0 &&
  787. !search_exception_tables(regs->ip)) {
  788. bad_area_nosemaphore(regs, error_code, address);
  789. return;
  790. }
  791. down_read(&mm->mmap_sem);
  792. }
  793. vma = find_vma(mm, address);
  794. if (unlikely(!vma)) {
  795. bad_area(regs, error_code, address);
  796. return;
  797. }
  798. if (likely(vma->vm_start <= address))
  799. goto good_area;
  800. if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
  801. bad_area(regs, error_code, address);
  802. return;
  803. }
  804. if (error_code & PF_USER) {
  805. /*
  806. * Accessing the stack below %sp is always a bug.
  807. * The large cushion allows instructions like enter
  808. * and pusha to work. ("enter $65535,$31" pushes
  809. * 32 pointers and then decrements %sp by 65535.)
  810. */
  811. if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
  812. bad_area(regs, error_code, address);
  813. return;
  814. }
  815. }
  816. if (unlikely(expand_stack(vma, address))) {
  817. bad_area(regs, error_code, address);
  818. return;
  819. }
  820. /*
  821. * Ok, we have a good vm_area for this memory access, so
  822. * we can handle it..
  823. */
  824. good_area:
  825. write = error_code & PF_WRITE;
  826. if (unlikely(access_error(error_code, write, vma))) {
  827. bad_area_access_error(regs, error_code, address);
  828. return;
  829. }
  830. /*
  831. * If for any reason at all we couldn't handle the fault,
  832. * make sure we exit gracefully rather than endlessly redo
  833. * the fault.
  834. */
  835. fault = handle_mm_fault(mm, vma, address, write);
  836. if (unlikely(fault & VM_FAULT_ERROR)) {
  837. mm_fault_error(regs, error_code, address, fault);
  838. return;
  839. }
  840. if (fault & VM_FAULT_MAJOR)
  841. tsk->maj_flt++;
  842. else
  843. tsk->min_flt++;
  844. #ifdef CONFIG_X86_32
  845. /*
  846. * Did it hit the DOS screen memory VA from vm86 mode?
  847. */
  848. if (v8086_mode(regs)) {
  849. unsigned long bit = (address - 0xA0000) >> PAGE_SHIFT;
  850. if (bit < 32)
  851. tsk->thread.screen_bitmap |= 1 << bit;
  852. }
  853. #endif
  854. up_read(&mm->mmap_sem);
  855. }
  856. DEFINE_SPINLOCK(pgd_lock);
  857. LIST_HEAD(pgd_list);
  858. void vmalloc_sync_all(void)
  859. {
  860. unsigned long address;
  861. #ifdef CONFIG_X86_32
  862. if (SHARED_KERNEL_PMD)
  863. return;
  864. for (address = VMALLOC_START & PMD_MASK;
  865. address >= TASK_SIZE && address < FIXADDR_TOP;
  866. address += PMD_SIZE) {
  867. unsigned long flags;
  868. struct page *page;
  869. spin_lock_irqsave(&pgd_lock, flags);
  870. list_for_each_entry(page, &pgd_list, lru) {
  871. if (!vmalloc_sync_one(page_address(page),
  872. address))
  873. break;
  874. }
  875. spin_unlock_irqrestore(&pgd_lock, flags);
  876. }
  877. #else /* CONFIG_X86_64 */
  878. for (address = VMALLOC_START & PGDIR_MASK; address <= VMALLOC_END;
  879. address += PGDIR_SIZE) {
  880. const pgd_t *pgd_ref = pgd_offset_k(address);
  881. unsigned long flags;
  882. struct page *page;
  883. if (pgd_none(*pgd_ref))
  884. continue;
  885. spin_lock_irqsave(&pgd_lock, flags);
  886. list_for_each_entry(page, &pgd_list, lru) {
  887. pgd_t *pgd;
  888. pgd = (pgd_t *)page_address(page) + pgd_index(address);
  889. if (pgd_none(*pgd))
  890. set_pgd(pgd, *pgd_ref);
  891. else
  892. BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
  893. }
  894. spin_unlock_irqrestore(&pgd_lock, flags);
  895. }
  896. #endif
  897. }