fault.c 24 KB

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