fault.c 25 KB

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