fault_32.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /*
  2. * Copyright (C) 1995 Linus Torvalds
  3. */
  4. #include <linux/signal.h>
  5. #include <linux/sched.h>
  6. #include <linux/kernel.h>
  7. #include <linux/errno.h>
  8. #include <linux/string.h>
  9. #include <linux/types.h>
  10. #include <linux/ptrace.h>
  11. #include <linux/mman.h>
  12. #include <linux/mm.h>
  13. #include <linux/smp.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/init.h>
  16. #include <linux/tty.h>
  17. #include <linux/vt_kern.h> /* For unblank_screen() */
  18. #include <linux/highmem.h>
  19. #include <linux/bootmem.h> /* for max_low_pfn */
  20. #include <linux/vmalloc.h>
  21. #include <linux/module.h>
  22. #include <linux/kprobes.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/kdebug.h>
  25. #include <asm/system.h>
  26. #include <asm/desc.h>
  27. #include <asm/segment.h>
  28. /*
  29. * Page fault error code bits
  30. * bit 0 == 0 means no page found, 1 means protection fault
  31. * bit 1 == 0 means read, 1 means write
  32. * bit 2 == 0 means kernel, 1 means user-mode
  33. * bit 3 == 1 means use of reserved bit detected
  34. * bit 4 == 1 means fault was an instruction fetch
  35. */
  36. #define PF_PROT (1<<0)
  37. #define PF_WRITE (1<<1)
  38. #define PF_USER (1<<2)
  39. #define PF_RSVD (1<<3)
  40. #define PF_INSTR (1<<4)
  41. static inline int notify_page_fault(struct pt_regs *regs)
  42. {
  43. #ifdef CONFIG_KPROBES
  44. int ret = 0;
  45. /* kprobe_running() needs smp_processor_id() */
  46. if (!user_mode_vm(regs)) {
  47. preempt_disable();
  48. if (kprobe_running() && kprobe_fault_handler(regs, 14))
  49. ret = 1;
  50. preempt_enable();
  51. }
  52. return ret;
  53. #else
  54. return 0;
  55. #endif
  56. }
  57. /*
  58. * X86_32
  59. * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
  60. * Check that here and ignore it.
  61. *
  62. * X86_64
  63. * Sometimes the CPU reports invalid exceptions on prefetch.
  64. * Check that here and ignore it.
  65. *
  66. * Opcode checker based on code by Richard Brunner
  67. */
  68. static int is_prefetch(struct pt_regs *regs, unsigned long addr,
  69. unsigned long error_code)
  70. {
  71. unsigned char *instr;
  72. int scan_more = 1;
  73. int prefetch = 0;
  74. unsigned char *max_instr;
  75. #ifdef CONFIG_X86_32
  76. unsigned long limit;
  77. if (unlikely(boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
  78. boot_cpu_data.x86 >= 6)) {
  79. /* Catch an obscure case of prefetch inside an NX page. */
  80. if (nx_enabled && (error_code & PF_INSTR))
  81. return 0;
  82. } else {
  83. return 0;
  84. }
  85. instr = (unsigned char *)get_segment_eip(regs, &limit);
  86. #else
  87. /* If it was a exec fault ignore */
  88. if (error_code & PF_INSTR)
  89. return 0;
  90. instr = (unsigned char __user *)convert_rip_to_linear(current, regs);
  91. #endif
  92. max_instr = instr + 15;
  93. #ifdef CONFIG_X86_64
  94. if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE)
  95. return 0;
  96. #endif
  97. while (scan_more && instr < max_instr) {
  98. unsigned char opcode;
  99. unsigned char instr_hi;
  100. unsigned char instr_lo;
  101. #ifdef CONFIG_X86_32
  102. if (instr > (unsigned char *)limit)
  103. break;
  104. #endif
  105. if (probe_kernel_address(instr, opcode))
  106. break;
  107. instr_hi = opcode & 0xf0;
  108. instr_lo = opcode & 0x0f;
  109. instr++;
  110. switch (instr_hi) {
  111. case 0x20:
  112. case 0x30:
  113. /*
  114. * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
  115. * In X86_64 long mode, the CPU will signal invalid
  116. * opcode if some of these prefixes are present so
  117. * X86_64 will never get here anyway
  118. */
  119. scan_more = ((instr_lo & 7) == 0x6);
  120. break;
  121. #ifdef CONFIG_X86_64
  122. case 0x40:
  123. /*
  124. * In AMD64 long mode 0x40..0x4F are valid REX prefixes
  125. * Need to figure out under what instruction mode the
  126. * instruction was issued. Could check the LDT for lm,
  127. * but for now it's good enough to assume that long
  128. * mode only uses well known segments or kernel.
  129. */
  130. scan_more = (!user_mode(regs)) || (regs->cs == __USER_CS);
  131. break;
  132. #endif
  133. case 0x60:
  134. /* 0x64 thru 0x67 are valid prefixes in all modes. */
  135. scan_more = (instr_lo & 0xC) == 0x4;
  136. break;
  137. case 0xF0:
  138. /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
  139. scan_more = !instr_lo || (instr_lo>>1) == 1;
  140. break;
  141. case 0x00:
  142. /* Prefetch instruction is 0x0F0D or 0x0F18 */
  143. scan_more = 0;
  144. #ifdef CONFIG_X86_32
  145. if (instr > (unsigned char *)limit)
  146. break;
  147. #endif
  148. if (probe_kernel_address(instr, opcode))
  149. break;
  150. prefetch = (instr_lo == 0xF) &&
  151. (opcode == 0x0D || opcode == 0x18);
  152. break;
  153. default:
  154. scan_more = 0;
  155. break;
  156. }
  157. }
  158. return prefetch;
  159. }
  160. static void force_sig_info_fault(int si_signo, int si_code,
  161. unsigned long address, struct task_struct *tsk)
  162. {
  163. siginfo_t info;
  164. info.si_signo = si_signo;
  165. info.si_errno = 0;
  166. info.si_code = si_code;
  167. info.si_addr = (void __user *)address;
  168. force_sig_info(si_signo, &info, tsk);
  169. }
  170. void do_invalid_op(struct pt_regs *, unsigned long);
  171. static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
  172. {
  173. unsigned index = pgd_index(address);
  174. pgd_t *pgd_k;
  175. pud_t *pud, *pud_k;
  176. pmd_t *pmd, *pmd_k;
  177. pgd += index;
  178. pgd_k = init_mm.pgd + index;
  179. if (!pgd_present(*pgd_k))
  180. return NULL;
  181. /*
  182. * set_pgd(pgd, *pgd_k); here would be useless on PAE
  183. * and redundant with the set_pmd() on non-PAE. As would
  184. * set_pud.
  185. */
  186. pud = pud_offset(pgd, address);
  187. pud_k = pud_offset(pgd_k, address);
  188. if (!pud_present(*pud_k))
  189. return NULL;
  190. pmd = pmd_offset(pud, address);
  191. pmd_k = pmd_offset(pud_k, address);
  192. if (!pmd_present(*pmd_k))
  193. return NULL;
  194. if (!pmd_present(*pmd)) {
  195. set_pmd(pmd, *pmd_k);
  196. arch_flush_lazy_mmu_mode();
  197. } else
  198. BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
  199. return pmd_k;
  200. }
  201. #ifdef CONFIG_X86_64
  202. static const char errata93_warning[] =
  203. KERN_ERR "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
  204. KERN_ERR "******* Working around it, but it may cause SEGVs or burn power.\n"
  205. KERN_ERR "******* Please consider a BIOS update.\n"
  206. KERN_ERR "******* Disabling USB legacy in the BIOS may also help.\n";
  207. /* Workaround for K8 erratum #93 & buggy BIOS.
  208. BIOS SMM functions are required to use a specific workaround
  209. to avoid corruption of the 64bit RIP register on C stepping K8.
  210. A lot of BIOS that didn't get tested properly miss this.
  211. The OS sees this as a page fault with the upper 32bits of RIP cleared.
  212. Try to work around it here.
  213. Note we only handle faults in kernel here. */
  214. static int is_errata93(struct pt_regs *regs, unsigned long address)
  215. {
  216. static int warned;
  217. if (address != regs->ip)
  218. return 0;
  219. if ((address >> 32) != 0)
  220. return 0;
  221. address |= 0xffffffffUL << 32;
  222. if ((address >= (u64)_stext && address <= (u64)_etext) ||
  223. (address >= MODULES_VADDR && address <= MODULES_END)) {
  224. if (!warned) {
  225. printk(errata93_warning);
  226. warned = 1;
  227. }
  228. regs->ip = address;
  229. return 1;
  230. }
  231. return 0;
  232. }
  233. #endif
  234. /*
  235. * Handle a fault on the vmalloc or module mapping area
  236. *
  237. * This assumes no large pages in there.
  238. */
  239. static inline int vmalloc_fault(unsigned long address)
  240. {
  241. unsigned long pgd_paddr;
  242. pmd_t *pmd_k;
  243. pte_t *pte_k;
  244. /*
  245. * Synchronize this task's top level page-table
  246. * with the 'reference' page table.
  247. *
  248. * Do _not_ use "current" here. We might be inside
  249. * an interrupt in the middle of a task switch..
  250. */
  251. pgd_paddr = read_cr3();
  252. pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
  253. if (!pmd_k)
  254. return -1;
  255. pte_k = pte_offset_kernel(pmd_k, address);
  256. if (!pte_present(*pte_k))
  257. return -1;
  258. return 0;
  259. }
  260. int show_unhandled_signals = 1;
  261. /*
  262. * This routine handles page faults. It determines the address,
  263. * and the problem, and then passes it off to one of the appropriate
  264. * routines.
  265. */
  266. void __kprobes do_page_fault(struct pt_regs *regs, unsigned long error_code)
  267. {
  268. struct task_struct *tsk;
  269. struct mm_struct *mm;
  270. struct vm_area_struct *vma;
  271. unsigned long address;
  272. int write, si_code;
  273. int fault;
  274. /*
  275. * We can fault from pretty much anywhere, with unknown IRQ state.
  276. */
  277. trace_hardirqs_fixup();
  278. /* get the address */
  279. address = read_cr2();
  280. tsk = current;
  281. si_code = SEGV_MAPERR;
  282. /*
  283. * We fault-in kernel-space virtual memory on-demand. The
  284. * 'reference' page table is init_mm.pgd.
  285. *
  286. * NOTE! We MUST NOT take any locks for this case. We may
  287. * be in an interrupt or a critical region, and should
  288. * only copy the information from the master page table,
  289. * nothing more.
  290. *
  291. * This verifies that the fault happens in kernel space
  292. * (error_code & 4) == 0, and that the fault was not a
  293. * protection error (error_code & 9) == 0.
  294. */
  295. if (unlikely(address >= TASK_SIZE)) {
  296. if (!(error_code & (PF_RSVD|PF_USER|PF_PROT)) &&
  297. vmalloc_fault(address) >= 0)
  298. return;
  299. if (notify_page_fault(regs))
  300. return;
  301. /*
  302. * Don't take the mm semaphore here. If we fixup a prefetch
  303. * fault we could otherwise deadlock.
  304. */
  305. goto bad_area_nosemaphore;
  306. }
  307. if (notify_page_fault(regs))
  308. return;
  309. /* It's safe to allow irq's after cr2 has been saved and the vmalloc
  310. fault has been handled. */
  311. if (regs->flags & (X86_EFLAGS_IF|VM_MASK))
  312. local_irq_enable();
  313. mm = tsk->mm;
  314. /*
  315. * If we're in an interrupt, have no user context or are running in an
  316. * atomic region then we must not take the fault.
  317. */
  318. if (in_atomic() || !mm)
  319. goto bad_area_nosemaphore;
  320. /* When running in the kernel we expect faults to occur only to
  321. * addresses in user space. All other faults represent errors in the
  322. * kernel and should generate an OOPS. Unfortunately, in the case of an
  323. * erroneous fault occurring in a code path which already holds mmap_sem
  324. * we will deadlock attempting to validate the fault against the
  325. * address space. Luckily the kernel only validly references user
  326. * space from well defined areas of code, which are listed in the
  327. * exceptions table.
  328. *
  329. * As the vast majority of faults will be valid we will only perform
  330. * the source reference check when there is a possibility of a deadlock.
  331. * Attempt to lock the address space, if we cannot we then validate the
  332. * source. If this is invalid we can skip the address space check,
  333. * thus avoiding the deadlock.
  334. */
  335. if (!down_read_trylock(&mm->mmap_sem)) {
  336. if ((error_code & PF_USER) == 0 &&
  337. !search_exception_tables(regs->ip))
  338. goto bad_area_nosemaphore;
  339. down_read(&mm->mmap_sem);
  340. }
  341. vma = find_vma(mm, address);
  342. if (!vma)
  343. goto bad_area;
  344. if (vma->vm_start <= address)
  345. goto good_area;
  346. if (!(vma->vm_flags & VM_GROWSDOWN))
  347. goto bad_area;
  348. if (error_code & PF_USER) {
  349. /*
  350. * Accessing the stack below %sp is always a bug.
  351. * The large cushion allows instructions like enter
  352. * and pusha to work. ("enter $65535,$31" pushes
  353. * 32 pointers and then decrements %sp by 65535.)
  354. */
  355. if (address + 65536 + 32 * sizeof(unsigned long) < regs->sp)
  356. goto bad_area;
  357. }
  358. if (expand_stack(vma, address))
  359. goto bad_area;
  360. /*
  361. * Ok, we have a good vm_area for this memory access, so
  362. * we can handle it..
  363. */
  364. good_area:
  365. si_code = SEGV_ACCERR;
  366. write = 0;
  367. switch (error_code & (PF_PROT|PF_WRITE)) {
  368. default: /* 3: write, present */
  369. /* fall through */
  370. case PF_WRITE: /* write, not present */
  371. if (!(vma->vm_flags & VM_WRITE))
  372. goto bad_area;
  373. write++;
  374. break;
  375. case PF_PROT: /* read, present */
  376. goto bad_area;
  377. case 0: /* read, not present */
  378. if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
  379. goto bad_area;
  380. }
  381. survive:
  382. /*
  383. * If for any reason at all we couldn't handle the fault,
  384. * make sure we exit gracefully rather than endlessly redo
  385. * the fault.
  386. */
  387. fault = handle_mm_fault(mm, vma, address, write);
  388. if (unlikely(fault & VM_FAULT_ERROR)) {
  389. if (fault & VM_FAULT_OOM)
  390. goto out_of_memory;
  391. else if (fault & VM_FAULT_SIGBUS)
  392. goto do_sigbus;
  393. BUG();
  394. }
  395. if (fault & VM_FAULT_MAJOR)
  396. tsk->maj_flt++;
  397. else
  398. tsk->min_flt++;
  399. /*
  400. * Did it hit the DOS screen memory VA from vm86 mode?
  401. */
  402. if (regs->flags & VM_MASK) {
  403. unsigned long bit = (address - 0xA0000) >> PAGE_SHIFT;
  404. if (bit < 32)
  405. tsk->thread.screen_bitmap |= 1 << bit;
  406. }
  407. up_read(&mm->mmap_sem);
  408. return;
  409. /*
  410. * Something tried to access memory that isn't in our memory map..
  411. * Fix it, but check if it's kernel or user first..
  412. */
  413. bad_area:
  414. up_read(&mm->mmap_sem);
  415. bad_area_nosemaphore:
  416. /* User mode accesses just cause a SIGSEGV */
  417. if (error_code & PF_USER) {
  418. /*
  419. * It's possible to have interrupts off here.
  420. */
  421. local_irq_enable();
  422. /*
  423. * Valid to do another page fault here because this one came
  424. * from user space.
  425. */
  426. if (is_prefetch(regs, address, error_code))
  427. return;
  428. if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
  429. printk_ratelimit()) {
  430. printk("%s%s[%d]: segfault at %08lx ip %08lx "
  431. "sp %08lx error %lx\n",
  432. task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
  433. tsk->comm, task_pid_nr(tsk), address, regs->ip,
  434. regs->sp, error_code);
  435. }
  436. tsk->thread.cr2 = address;
  437. /* Kernel addresses are always protection faults */
  438. tsk->thread.error_code = error_code | (address >= TASK_SIZE);
  439. tsk->thread.trap_no = 14;
  440. force_sig_info_fault(SIGSEGV, si_code, address, tsk);
  441. return;
  442. }
  443. #ifdef CONFIG_X86_F00F_BUG
  444. /*
  445. * Pentium F0 0F C7 C8 bug workaround.
  446. */
  447. if (boot_cpu_data.f00f_bug) {
  448. unsigned long nr;
  449. nr = (address - idt_descr.address) >> 3;
  450. if (nr == 6) {
  451. do_invalid_op(regs, 0);
  452. return;
  453. }
  454. }
  455. #endif
  456. no_context:
  457. /* Are we prepared to handle this kernel fault? */
  458. if (fixup_exception(regs))
  459. return;
  460. /*
  461. * Valid to do another page fault here, because if this fault
  462. * had been triggered by is_prefetch fixup_exception would have
  463. * handled it.
  464. */
  465. if (is_prefetch(regs, address, error_code))
  466. return;
  467. /*
  468. * Oops. The kernel tried to access some bad page. We'll have to
  469. * terminate things with extreme prejudice.
  470. */
  471. bust_spinlocks(1);
  472. if (oops_may_print()) {
  473. __typeof__(pte_val(__pte(0))) page;
  474. #ifdef CONFIG_X86_PAE
  475. if (error_code & PF_INSTR) {
  476. pte_t *pte = lookup_address(address);
  477. if (pte && pte_present(*pte) && !pte_exec_kernel(*pte))
  478. printk(KERN_CRIT "kernel tried to execute "
  479. "NX-protected page - exploit attempt? "
  480. "(uid: %d)\n", current->uid);
  481. }
  482. #endif
  483. if (address < PAGE_SIZE)
  484. printk(KERN_ALERT "BUG: unable to handle kernel NULL "
  485. "pointer dereference");
  486. else
  487. printk(KERN_ALERT "BUG: unable to handle kernel paging"
  488. " request");
  489. printk(" at virtual address %08lx\n", address);
  490. printk(KERN_ALERT "printing ip: %08lx ", regs->ip);
  491. page = read_cr3();
  492. page = ((__typeof__(page) *) __va(page))[address >> PGDIR_SHIFT];
  493. #ifdef CONFIG_X86_PAE
  494. printk("*pdpt = %016Lx ", page);
  495. if ((page >> PAGE_SHIFT) < max_low_pfn
  496. && page & _PAGE_PRESENT) {
  497. page &= PAGE_MASK;
  498. page = ((__typeof__(page) *) __va(page))[(address >> PMD_SHIFT)
  499. & (PTRS_PER_PMD - 1)];
  500. printk(KERN_CONT "*pde = %016Lx ", page);
  501. page &= ~_PAGE_NX;
  502. }
  503. #else
  504. printk("*pde = %08lx ", page);
  505. #endif
  506. /*
  507. * We must not directly access the pte in the highpte
  508. * case if the page table is located in highmem.
  509. * And let's rather not kmap-atomic the pte, just in case
  510. * it's allocated already.
  511. */
  512. if ((page >> PAGE_SHIFT) < max_low_pfn
  513. && (page & _PAGE_PRESENT)
  514. && !(page & _PAGE_PSE)) {
  515. page &= PAGE_MASK;
  516. page = ((__typeof__(page) *) __va(page))[(address >> PAGE_SHIFT)
  517. & (PTRS_PER_PTE - 1)];
  518. printk("*pte = %0*Lx ", sizeof(page)*2, (u64)page);
  519. }
  520. printk("\n");
  521. }
  522. tsk->thread.cr2 = address;
  523. tsk->thread.trap_no = 14;
  524. tsk->thread.error_code = error_code;
  525. die("Oops", regs, error_code);
  526. bust_spinlocks(0);
  527. do_exit(SIGKILL);
  528. /*
  529. * We ran out of memory, or some other thing happened to us that made
  530. * us unable to handle the page fault gracefully.
  531. */
  532. out_of_memory:
  533. up_read(&mm->mmap_sem);
  534. if (is_global_init(tsk)) {
  535. yield();
  536. down_read(&mm->mmap_sem);
  537. goto survive;
  538. }
  539. printk("VM: killing process %s\n", tsk->comm);
  540. if (error_code & PF_USER)
  541. do_group_exit(SIGKILL);
  542. goto no_context;
  543. do_sigbus:
  544. up_read(&mm->mmap_sem);
  545. /* Kernel mode? Handle exceptions or die */
  546. if (!(error_code & PF_USER))
  547. goto no_context;
  548. /* User space => ok to do another page fault */
  549. if (is_prefetch(regs, address, error_code))
  550. return;
  551. tsk->thread.cr2 = address;
  552. tsk->thread.error_code = error_code;
  553. tsk->thread.trap_no = 14;
  554. force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk);
  555. }
  556. void vmalloc_sync_all(void)
  557. {
  558. /*
  559. * Note that races in the updates of insync and start aren't
  560. * problematic: insync can only get set bits added, and updates to
  561. * start are only improving performance (without affecting correctness
  562. * if undone).
  563. */
  564. static DECLARE_BITMAP(insync, PTRS_PER_PGD);
  565. static unsigned long start = TASK_SIZE;
  566. unsigned long address;
  567. if (SHARED_KERNEL_PMD)
  568. return;
  569. BUILD_BUG_ON(TASK_SIZE & ~PGDIR_MASK);
  570. for (address = start; address >= TASK_SIZE; address += PGDIR_SIZE) {
  571. if (!test_bit(pgd_index(address), insync)) {
  572. unsigned long flags;
  573. struct page *page;
  574. spin_lock_irqsave(&pgd_lock, flags);
  575. for (page = pgd_list; page; page =
  576. (struct page *)page->index)
  577. if (!vmalloc_sync_one(page_address(page),
  578. address)) {
  579. BUG_ON(page != pgd_list);
  580. break;
  581. }
  582. spin_unlock_irqrestore(&pgd_lock, flags);
  583. if (!page)
  584. set_bit(pgd_index(address), insync);
  585. }
  586. if (address == start && test_bit(pgd_index(address), insync))
  587. start = address + PGDIR_SIZE;
  588. }
  589. }