fault.c 25 KB

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