fault.c 24 KB

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