fault.c 26 KB

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