fault.c 23 KB

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