fault.c 26 KB

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