fault.c 24 KB

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