fault_32.c 24 KB

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