fault_32.c 16 KB

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