fault.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  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 <asm/system.h>
  29. #include <asm/desc.h>
  30. #include <asm/segment.h>
  31. #include <asm/pgalloc.h>
  32. #include <asm/smp.h>
  33. #include <asm/tlbflush.h>
  34. #include <asm/proto.h>
  35. #include <asm-generic/sections.h>
  36. #include <asm/traps.h>
  37. /*
  38. * Page fault error code bits
  39. * bit 0 == 0 means no page found, 1 means protection fault
  40. * bit 1 == 0 means read, 1 means write
  41. * bit 2 == 0 means kernel, 1 means user-mode
  42. * bit 3 == 1 means use of reserved bit detected
  43. * bit 4 == 1 means fault was an instruction fetch
  44. */
  45. #define PF_PROT (1<<0)
  46. #define PF_WRITE (1<<1)
  47. #define PF_USER (1<<2)
  48. #define PF_RSVD (1<<3)
  49. #define PF_INSTR (1<<4)
  50. static inline int kmmio_fault(struct pt_regs *regs, unsigned long addr)
  51. {
  52. #ifdef CONFIG_MMIOTRACE_HOOKS
  53. if (unlikely(is_kmmio_active()))
  54. if (kmmio_handler(regs, addr) == 1)
  55. return -1;
  56. #endif
  57. return 0;
  58. }
  59. static inline int notify_page_fault(struct pt_regs *regs)
  60. {
  61. #ifdef CONFIG_KPROBES
  62. int ret = 0;
  63. /* kprobe_running() needs smp_processor_id() */
  64. if (!user_mode_vm(regs)) {
  65. preempt_disable();
  66. if (kprobe_running() && kprobe_fault_handler(regs, 14))
  67. ret = 1;
  68. preempt_enable();
  69. }
  70. return ret;
  71. #else
  72. return 0;
  73. #endif
  74. }
  75. /*
  76. * X86_32
  77. * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
  78. * Check that here and ignore it.
  79. *
  80. * X86_64
  81. * Sometimes the CPU reports invalid exceptions on prefetch.
  82. * Check that here and ignore it.
  83. *
  84. * Opcode checker based on code by Richard Brunner
  85. */
  86. static int is_prefetch(struct pt_regs *regs, unsigned long addr,
  87. unsigned long error_code)
  88. {
  89. unsigned char *instr;
  90. int scan_more = 1;
  91. int prefetch = 0;
  92. unsigned char *max_instr;
  93. /*
  94. * If it was a exec (instruction fetch) fault on NX page, then
  95. * do not ignore the fault:
  96. */
  97. if (error_code & PF_INSTR)
  98. return 0;
  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. static 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) || pud_large(*pud))
  224. goto ret;
  225. pmd = pmd_offset(pud, address);
  226. if (bad_address(pmd)) goto bad;
  227. printk("PMD %lx ", pmd_val(*pmd));
  228. if (!pmd_present(*pmd) || pmd_large(*pmd)) goto ret;
  229. pte = pte_offset_kernel(pmd, address);
  230. if (bad_address(pte)) goto bad;
  231. printk("PTE %lx", pte_val(*pte));
  232. ret:
  233. printk("\n");
  234. return;
  235. bad:
  236. printk("BAD\n");
  237. #endif
  238. }
  239. #ifdef CONFIG_X86_32
  240. static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
  241. {
  242. unsigned index = pgd_index(address);
  243. pgd_t *pgd_k;
  244. pud_t *pud, *pud_k;
  245. pmd_t *pmd, *pmd_k;
  246. pgd += index;
  247. pgd_k = init_mm.pgd + index;
  248. if (!pgd_present(*pgd_k))
  249. return NULL;
  250. /*
  251. * set_pgd(pgd, *pgd_k); here would be useless on PAE
  252. * and redundant with the set_pmd() on non-PAE. As would
  253. * set_pud.
  254. */
  255. pud = pud_offset(pgd, address);
  256. pud_k = pud_offset(pgd_k, address);
  257. if (!pud_present(*pud_k))
  258. return NULL;
  259. pmd = pmd_offset(pud, address);
  260. pmd_k = pmd_offset(pud_k, address);
  261. if (!pmd_present(*pmd_k))
  262. return NULL;
  263. if (!pmd_present(*pmd)) {
  264. set_pmd(pmd, *pmd_k);
  265. arch_flush_lazy_mmu_mode();
  266. } else
  267. BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
  268. return pmd_k;
  269. }
  270. #endif
  271. #ifdef CONFIG_X86_64
  272. static const char errata93_warning[] =
  273. KERN_ERR "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
  274. KERN_ERR "******* Working around it, but it may cause SEGVs or burn power.\n"
  275. KERN_ERR "******* Please consider a BIOS update.\n"
  276. KERN_ERR "******* Disabling USB legacy in the BIOS may also help.\n";
  277. #endif
  278. /* Workaround for K8 erratum #93 & buggy BIOS.
  279. BIOS SMM functions are required to use a specific workaround
  280. to avoid corruption of the 64bit RIP register on C stepping K8.
  281. A lot of BIOS that didn't get tested properly miss this.
  282. The OS sees this as a page fault with the upper 32bits of RIP cleared.
  283. Try to work around it here.
  284. Note we only handle faults in kernel here.
  285. Does nothing for X86_32
  286. */
  287. static int is_errata93(struct pt_regs *regs, unsigned long address)
  288. {
  289. #ifdef CONFIG_X86_64
  290. static int warned;
  291. if (address != regs->ip)
  292. return 0;
  293. if ((address >> 32) != 0)
  294. return 0;
  295. address |= 0xffffffffUL << 32;
  296. if ((address >= (u64)_stext && address <= (u64)_etext) ||
  297. (address >= MODULES_VADDR && address <= MODULES_END)) {
  298. if (!warned) {
  299. printk(errata93_warning);
  300. warned = 1;
  301. }
  302. regs->ip = address;
  303. return 1;
  304. }
  305. #endif
  306. return 0;
  307. }
  308. /*
  309. * Work around K8 erratum #100 K8 in compat mode occasionally jumps to illegal
  310. * addresses >4GB. We catch this in the page fault handler because these
  311. * addresses are not reachable. Just detect this case and return. Any code
  312. * segment in LDT is compatibility mode.
  313. */
  314. static int is_errata100(struct pt_regs *regs, unsigned long address)
  315. {
  316. #ifdef CONFIG_X86_64
  317. if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) &&
  318. (address >> 32))
  319. return 1;
  320. #endif
  321. return 0;
  322. }
  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. #endif
  347. #ifdef CONFIG_X86_PAE
  348. if (error_code & PF_INSTR) {
  349. unsigned int level;
  350. pte_t *pte = lookup_address(address, &level);
  351. if (pte && pte_present(*pte) && !pte_exec(*pte))
  352. printk(KERN_CRIT "kernel tried to execute "
  353. "NX-protected page - exploit attempt? "
  354. "(uid: %d)\n", current->uid);
  355. }
  356. #endif
  357. printk(KERN_ALERT "BUG: unable to handle kernel ");
  358. if (address < PAGE_SIZE)
  359. printk(KERN_CONT "NULL pointer dereference");
  360. else
  361. printk(KERN_CONT "paging request");
  362. printk(KERN_CONT " at %p\n", (void *) address);
  363. printk(KERN_ALERT "IP:");
  364. printk_address(regs->ip, 1);
  365. dump_pagetable(address);
  366. }
  367. #ifdef CONFIG_X86_64
  368. static noinline void pgtable_bad(unsigned long address, struct pt_regs *regs,
  369. unsigned long error_code)
  370. {
  371. unsigned long flags = oops_begin();
  372. struct task_struct *tsk;
  373. printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
  374. current->comm, address);
  375. dump_pagetable(address);
  376. tsk = current;
  377. tsk->thread.cr2 = address;
  378. tsk->thread.trap_no = 14;
  379. tsk->thread.error_code = error_code;
  380. if (__die("Bad pagetable", regs, error_code))
  381. regs = NULL;
  382. oops_end(flags, regs, SIGKILL);
  383. }
  384. #endif
  385. static int spurious_fault_check(unsigned long error_code, pte_t *pte)
  386. {
  387. if ((error_code & PF_WRITE) && !pte_write(*pte))
  388. return 0;
  389. if ((error_code & PF_INSTR) && !pte_exec(*pte))
  390. return 0;
  391. return 1;
  392. }
  393. /*
  394. * Handle a spurious fault caused by a stale TLB entry. This allows
  395. * us to lazily refresh the TLB when increasing the permissions of a
  396. * kernel page (RO -> RW or NX -> X). Doing it eagerly is very
  397. * expensive since that implies doing a full cross-processor TLB
  398. * flush, even if no stale TLB entries exist on other processors.
  399. * There are no security implications to leaving a stale TLB when
  400. * increasing the permissions on a page.
  401. */
  402. static int spurious_fault(unsigned long address,
  403. unsigned long error_code)
  404. {
  405. pgd_t *pgd;
  406. pud_t *pud;
  407. pmd_t *pmd;
  408. pte_t *pte;
  409. /* Reserved-bit violation or user access to kernel space? */
  410. if (error_code & (PF_USER | PF_RSVD))
  411. return 0;
  412. pgd = init_mm.pgd + pgd_index(address);
  413. if (!pgd_present(*pgd))
  414. return 0;
  415. pud = pud_offset(pgd, address);
  416. if (!pud_present(*pud))
  417. return 0;
  418. if (pud_large(*pud))
  419. return spurious_fault_check(error_code, (pte_t *) pud);
  420. pmd = pmd_offset(pud, address);
  421. if (!pmd_present(*pmd))
  422. return 0;
  423. if (pmd_large(*pmd))
  424. return spurious_fault_check(error_code, (pte_t *) pmd);
  425. pte = pte_offset_kernel(pmd, address);
  426. if (!pte_present(*pte))
  427. return 0;
  428. return spurious_fault_check(error_code, pte);
  429. }
  430. /*
  431. * X86_32
  432. * Handle a fault on the vmalloc or module mapping area
  433. *
  434. * X86_64
  435. * Handle a fault on the vmalloc area
  436. *
  437. * This assumes no large pages in there.
  438. */
  439. static int vmalloc_fault(unsigned long address)
  440. {
  441. #ifdef CONFIG_X86_32
  442. unsigned long pgd_paddr;
  443. pmd_t *pmd_k;
  444. pte_t *pte_k;
  445. /* Make sure we are in vmalloc area */
  446. if (!(address >= VMALLOC_START && address < VMALLOC_END))
  447. return -1;
  448. /*
  449. * Synchronize this task's top level page-table
  450. * with the 'reference' page table.
  451. *
  452. * Do _not_ use "current" here. We might be inside
  453. * an interrupt in the middle of a task switch..
  454. */
  455. pgd_paddr = read_cr3();
  456. pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
  457. if (!pmd_k)
  458. return -1;
  459. pte_k = pte_offset_kernel(pmd_k, address);
  460. if (!pte_present(*pte_k))
  461. return -1;
  462. return 0;
  463. #else
  464. pgd_t *pgd, *pgd_ref;
  465. pud_t *pud, *pud_ref;
  466. pmd_t *pmd, *pmd_ref;
  467. pte_t *pte, *pte_ref;
  468. /* Make sure we are in vmalloc area */
  469. if (!(address >= VMALLOC_START && address < VMALLOC_END))
  470. return -1;
  471. /* Copy kernel mappings over when needed. This can also
  472. happen within a race in page table update. In the later
  473. case just flush. */
  474. pgd = pgd_offset(current->mm ?: &init_mm, address);
  475. pgd_ref = pgd_offset_k(address);
  476. if (pgd_none(*pgd_ref))
  477. return -1;
  478. if (pgd_none(*pgd))
  479. set_pgd(pgd, *pgd_ref);
  480. else
  481. BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
  482. /* Below here mismatches are bugs because these lower tables
  483. are shared */
  484. pud = pud_offset(pgd, address);
  485. pud_ref = pud_offset(pgd_ref, address);
  486. if (pud_none(*pud_ref))
  487. return -1;
  488. if (pud_none(*pud) || pud_page_vaddr(*pud) != pud_page_vaddr(*pud_ref))
  489. BUG();
  490. pmd = pmd_offset(pud, address);
  491. pmd_ref = pmd_offset(pud_ref, address);
  492. if (pmd_none(*pmd_ref))
  493. return -1;
  494. if (pmd_none(*pmd) || pmd_page(*pmd) != pmd_page(*pmd_ref))
  495. BUG();
  496. pte_ref = pte_offset_kernel(pmd_ref, address);
  497. if (!pte_present(*pte_ref))
  498. return -1;
  499. pte = pte_offset_kernel(pmd, address);
  500. /* Don't use pte_page here, because the mappings can point
  501. outside mem_map, and the NUMA hash lookup cannot handle
  502. that. */
  503. if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
  504. BUG();
  505. return 0;
  506. #endif
  507. }
  508. int show_unhandled_signals = 1;
  509. /*
  510. * This routine handles page faults. It determines the address,
  511. * and the problem, and then passes it off to one of the appropriate
  512. * routines.
  513. */
  514. #ifdef CONFIG_X86_64
  515. asmlinkage
  516. #endif
  517. void __kprobes do_page_fault(struct pt_regs *regs, unsigned long error_code)
  518. {
  519. struct task_struct *tsk;
  520. struct mm_struct *mm;
  521. struct vm_area_struct *vma;
  522. unsigned long address;
  523. int write, si_code;
  524. int fault;
  525. #ifdef CONFIG_X86_64
  526. unsigned long flags;
  527. #endif
  528. /*
  529. * We can fault from pretty much anywhere, with unknown IRQ state.
  530. */
  531. trace_hardirqs_fixup();
  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. #ifdef CONFIG_X86_32
  573. /* It's safe to allow irq's after cr2 has been saved and the vmalloc
  574. fault has been handled. */
  575. if (regs->flags & (X86_EFLAGS_IF | X86_VM_MASK))
  576. local_irq_enable();
  577. /*
  578. * If we're in an interrupt, have no user context or are running in an
  579. * atomic region then we must not take the fault.
  580. */
  581. if (in_atomic() || !mm)
  582. goto bad_area_nosemaphore;
  583. #else /* CONFIG_X86_64 */
  584. if (likely(regs->flags & X86_EFLAGS_IF))
  585. local_irq_enable();
  586. if (unlikely(error_code & PF_RSVD))
  587. pgtable_bad(address, regs, error_code);
  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. * User-mode registers count as a user access even for any
  596. * potential system fault or CPU buglet.
  597. */
  598. if (user_mode_vm(regs))
  599. error_code |= PF_USER;
  600. again:
  601. #endif
  602. /* When running in the kernel we expect faults to occur only to
  603. * addresses in user space. All other faults represent errors in the
  604. * kernel and should generate an OOPS. Unfortunately, in the case of an
  605. * erroneous fault occurring in a code path which already holds mmap_sem
  606. * we will deadlock attempting to validate the fault against the
  607. * address space. Luckily the kernel only validly references user
  608. * space from well defined areas of code, which are listed in the
  609. * exceptions table.
  610. *
  611. * As the vast majority of faults will be valid we will only perform
  612. * the source reference check when there is a possibility of a deadlock.
  613. * Attempt to lock the address space, if we cannot we then validate the
  614. * source. If this is invalid we can skip the address space check,
  615. * thus avoiding the deadlock.
  616. */
  617. if (!down_read_trylock(&mm->mmap_sem)) {
  618. if ((error_code & PF_USER) == 0 &&
  619. !search_exception_tables(regs->ip))
  620. goto bad_area_nosemaphore;
  621. down_read(&mm->mmap_sem);
  622. }
  623. vma = find_vma(mm, address);
  624. if (!vma)
  625. goto bad_area;
  626. if (vma->vm_start <= address)
  627. goto good_area;
  628. if (!(vma->vm_flags & VM_GROWSDOWN))
  629. goto bad_area;
  630. if (error_code & PF_USER) {
  631. /*
  632. * Accessing the stack below %sp is always a bug.
  633. * The large cushion allows instructions like enter
  634. * and pusha to work. ("enter $65535,$31" pushes
  635. * 32 pointers and then decrements %sp by 65535.)
  636. */
  637. if (address + 65536 + 32 * sizeof(unsigned long) < regs->sp)
  638. goto bad_area;
  639. }
  640. if (expand_stack(vma, address))
  641. goto bad_area;
  642. /*
  643. * Ok, we have a good vm_area for this memory access, so
  644. * we can handle it..
  645. */
  646. good_area:
  647. si_code = SEGV_ACCERR;
  648. write = 0;
  649. switch (error_code & (PF_PROT|PF_WRITE)) {
  650. default: /* 3: write, present */
  651. /* fall through */
  652. case PF_WRITE: /* write, not present */
  653. if (!(vma->vm_flags & VM_WRITE))
  654. goto bad_area;
  655. write++;
  656. break;
  657. case PF_PROT: /* read, present */
  658. goto bad_area;
  659. case 0: /* read, not present */
  660. if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
  661. goto bad_area;
  662. }
  663. #ifdef CONFIG_X86_32
  664. survive:
  665. #endif
  666. /*
  667. * If for any reason at all we couldn't handle the fault,
  668. * make sure we exit gracefully rather than endlessly redo
  669. * the fault.
  670. */
  671. fault = handle_mm_fault(mm, vma, address, write);
  672. if (unlikely(fault & VM_FAULT_ERROR)) {
  673. if (fault & VM_FAULT_OOM)
  674. goto out_of_memory;
  675. else if (fault & VM_FAULT_SIGBUS)
  676. goto do_sigbus;
  677. BUG();
  678. }
  679. if (fault & VM_FAULT_MAJOR)
  680. tsk->maj_flt++;
  681. else
  682. tsk->min_flt++;
  683. #ifdef CONFIG_X86_32
  684. /*
  685. * Did it hit the DOS screen memory VA from vm86 mode?
  686. */
  687. if (v8086_mode(regs)) {
  688. unsigned long bit = (address - 0xA0000) >> PAGE_SHIFT;
  689. if (bit < 32)
  690. tsk->thread.screen_bitmap |= 1 << bit;
  691. }
  692. #endif
  693. up_read(&mm->mmap_sem);
  694. return;
  695. /*
  696. * Something tried to access memory that isn't in our memory map..
  697. * Fix it, but check if it's kernel or user first..
  698. */
  699. bad_area:
  700. up_read(&mm->mmap_sem);
  701. bad_area_nosemaphore:
  702. /* User mode accesses just cause a SIGSEGV */
  703. if (error_code & PF_USER) {
  704. /*
  705. * It's possible to have interrupts off here.
  706. */
  707. local_irq_enable();
  708. /*
  709. * Valid to do another page fault here because this one came
  710. * from user space.
  711. */
  712. if (is_prefetch(regs, address, error_code))
  713. return;
  714. if (is_errata100(regs, address))
  715. return;
  716. if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
  717. printk_ratelimit()) {
  718. printk(
  719. "%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
  720. task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
  721. tsk->comm, task_pid_nr(tsk), address,
  722. (void *) regs->ip, (void *) regs->sp, error_code);
  723. print_vma_addr(" in ", regs->ip);
  724. printk("\n");
  725. }
  726. tsk->thread.cr2 = address;
  727. /* Kernel addresses are always protection faults */
  728. tsk->thread.error_code = error_code | (address >= TASK_SIZE);
  729. tsk->thread.trap_no = 14;
  730. force_sig_info_fault(SIGSEGV, si_code, address, tsk);
  731. return;
  732. }
  733. if (is_f00f_bug(regs, address))
  734. return;
  735. no_context:
  736. /* Are we prepared to handle this kernel fault? */
  737. if (fixup_exception(regs))
  738. return;
  739. /*
  740. * X86_32
  741. * Valid to do another page fault here, because if this fault
  742. * had been triggered by is_prefetch fixup_exception would have
  743. * handled it.
  744. *
  745. * X86_64
  746. * Hall of shame of CPU/BIOS bugs.
  747. */
  748. if (is_prefetch(regs, address, error_code))
  749. return;
  750. if (is_errata93(regs, address))
  751. return;
  752. /*
  753. * Oops. The kernel tried to access some bad page. We'll have to
  754. * terminate things with extreme prejudice.
  755. */
  756. #ifdef CONFIG_X86_32
  757. bust_spinlocks(1);
  758. #else
  759. flags = oops_begin();
  760. #endif
  761. show_fault_oops(regs, error_code, address);
  762. tsk->thread.cr2 = address;
  763. tsk->thread.trap_no = 14;
  764. tsk->thread.error_code = error_code;
  765. #ifdef CONFIG_X86_32
  766. die("Oops", regs, error_code);
  767. bust_spinlocks(0);
  768. do_exit(SIGKILL);
  769. #else
  770. if (__die("Oops", regs, error_code))
  771. regs = NULL;
  772. /* Executive summary in case the body of the oops scrolled away */
  773. printk(KERN_EMERG "CR2: %016lx\n", address);
  774. oops_end(flags, regs, SIGKILL);
  775. #endif
  776. /*
  777. * We ran out of memory, or some other thing happened to us that made
  778. * us unable to handle the page fault gracefully.
  779. */
  780. out_of_memory:
  781. up_read(&mm->mmap_sem);
  782. if (is_global_init(tsk)) {
  783. yield();
  784. #ifdef CONFIG_X86_32
  785. down_read(&mm->mmap_sem);
  786. goto survive;
  787. #else
  788. goto again;
  789. #endif
  790. }
  791. printk("VM: killing process %s\n", tsk->comm);
  792. if (error_code & PF_USER)
  793. do_group_exit(SIGKILL);
  794. goto no_context;
  795. do_sigbus:
  796. up_read(&mm->mmap_sem);
  797. /* Kernel mode? Handle exceptions or die */
  798. if (!(error_code & PF_USER))
  799. goto no_context;
  800. #ifdef CONFIG_X86_32
  801. /* User space => ok to do another page fault */
  802. if (is_prefetch(regs, address, error_code))
  803. return;
  804. #endif
  805. tsk->thread.cr2 = address;
  806. tsk->thread.error_code = error_code;
  807. tsk->thread.trap_no = 14;
  808. force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk);
  809. }
  810. DEFINE_SPINLOCK(pgd_lock);
  811. LIST_HEAD(pgd_list);
  812. void vmalloc_sync_all(void)
  813. {
  814. #ifdef CONFIG_X86_32
  815. unsigned long start = VMALLOC_START & PGDIR_MASK;
  816. unsigned long address;
  817. if (SHARED_KERNEL_PMD)
  818. return;
  819. BUILD_BUG_ON(TASK_SIZE & ~PGDIR_MASK);
  820. for (address = start; address >= TASK_SIZE; address += PGDIR_SIZE) {
  821. unsigned long flags;
  822. struct page *page;
  823. spin_lock_irqsave(&pgd_lock, flags);
  824. list_for_each_entry(page, &pgd_list, lru) {
  825. if (!vmalloc_sync_one(page_address(page),
  826. address))
  827. break;
  828. }
  829. spin_unlock_irqrestore(&pgd_lock, flags);
  830. }
  831. #else /* CONFIG_X86_64 */
  832. unsigned long start = VMALLOC_START & PGDIR_MASK;
  833. unsigned long address;
  834. for (address = start; address <= VMALLOC_END; address += PGDIR_SIZE) {
  835. const pgd_t *pgd_ref = pgd_offset_k(address);
  836. unsigned long flags;
  837. struct page *page;
  838. if (pgd_none(*pgd_ref))
  839. continue;
  840. spin_lock_irqsave(&pgd_lock, flags);
  841. list_for_each_entry(page, &pgd_list, lru) {
  842. pgd_t *pgd;
  843. pgd = (pgd_t *)page_address(page) + pgd_index(address);
  844. if (pgd_none(*pgd))
  845. set_pgd(pgd, *pgd_ref);
  846. else
  847. BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
  848. }
  849. spin_unlock_irqrestore(&pgd_lock, flags);
  850. }
  851. #endif
  852. }