fault.c 25 KB

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