fault.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. /*
  2. * linux/arch/i386/mm/fault.c
  3. *
  4. * Copyright (C) 1995 Linus Torvalds
  5. */
  6. #include <linux/signal.h>
  7. #include <linux/sched.h>
  8. #include <linux/kernel.h>
  9. #include <linux/errno.h>
  10. #include <linux/string.h>
  11. #include <linux/types.h>
  12. #include <linux/ptrace.h>
  13. #include <linux/mman.h>
  14. #include <linux/mm.h>
  15. #include <linux/smp.h>
  16. #include <linux/smp_lock.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/init.h>
  19. #include <linux/tty.h>
  20. #include <linux/vt_kern.h> /* For unblank_screen() */
  21. #include <linux/highmem.h>
  22. #include <linux/module.h>
  23. #include <linux/kprobes.h>
  24. #include <asm/system.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/desc.h>
  27. #include <asm/kdebug.h>
  28. extern void die(const char *,struct pt_regs *,long);
  29. #ifdef CONFIG_KPROBES
  30. ATOMIC_NOTIFIER_HEAD(notify_page_fault_chain);
  31. int register_page_fault_notifier(struct notifier_block *nb)
  32. {
  33. vmalloc_sync_all();
  34. return atomic_notifier_chain_register(&notify_page_fault_chain, nb);
  35. }
  36. int unregister_page_fault_notifier(struct notifier_block *nb)
  37. {
  38. return atomic_notifier_chain_unregister(&notify_page_fault_chain, nb);
  39. }
  40. static inline int notify_page_fault(enum die_val val, const char *str,
  41. struct pt_regs *regs, long err, int trap, int sig)
  42. {
  43. struct die_args args = {
  44. .regs = regs,
  45. .str = str,
  46. .err = err,
  47. .trapnr = trap,
  48. .signr = sig
  49. };
  50. return atomic_notifier_call_chain(&notify_page_fault_chain, val, &args);
  51. }
  52. #else
  53. static inline int notify_page_fault(enum die_val val, const char *str,
  54. struct pt_regs *regs, long err, int trap, int sig)
  55. {
  56. return NOTIFY_DONE;
  57. }
  58. #endif
  59. /*
  60. * Unlock any spinlocks which will prevent us from getting the
  61. * message out
  62. */
  63. void bust_spinlocks(int yes)
  64. {
  65. int loglevel_save = console_loglevel;
  66. if (yes) {
  67. oops_in_progress = 1;
  68. return;
  69. }
  70. #ifdef CONFIG_VT
  71. unblank_screen();
  72. #endif
  73. oops_in_progress = 0;
  74. /*
  75. * OK, the message is on the console. Now we call printk()
  76. * without oops_in_progress set so that printk will give klogd
  77. * a poke. Hold onto your hats...
  78. */
  79. console_loglevel = 15; /* NMI oopser may have shut the console up */
  80. printk(" ");
  81. console_loglevel = loglevel_save;
  82. }
  83. /*
  84. * Return EIP plus the CS segment base. The segment limit is also
  85. * adjusted, clamped to the kernel/user address space (whichever is
  86. * appropriate), and returned in *eip_limit.
  87. *
  88. * The segment is checked, because it might have been changed by another
  89. * task between the original faulting instruction and here.
  90. *
  91. * If CS is no longer a valid code segment, or if EIP is beyond the
  92. * limit, or if it is a kernel address when CS is not a kernel segment,
  93. * then the returned value will be greater than *eip_limit.
  94. *
  95. * This is slow, but is very rarely executed.
  96. */
  97. static inline unsigned long get_segment_eip(struct pt_regs *regs,
  98. unsigned long *eip_limit)
  99. {
  100. unsigned long eip = regs->eip;
  101. unsigned seg = regs->xcs & 0xffff;
  102. u32 seg_ar, seg_limit, base, *desc;
  103. /* Unlikely, but must come before segment checks. */
  104. if (unlikely(regs->eflags & VM_MASK)) {
  105. base = seg << 4;
  106. *eip_limit = base + 0xffff;
  107. return base + (eip & 0xffff);
  108. }
  109. /* The standard kernel/user address space limit. */
  110. *eip_limit = (seg & 3) ? USER_DS.seg : KERNEL_DS.seg;
  111. /* By far the most common cases. */
  112. if (likely(seg == __USER_CS || seg == __KERNEL_CS))
  113. return eip;
  114. /* Check the segment exists, is within the current LDT/GDT size,
  115. that kernel/user (ring 0..3) has the appropriate privilege,
  116. that it's a code segment, and get the limit. */
  117. __asm__ ("larl %3,%0; lsll %3,%1"
  118. : "=&r" (seg_ar), "=r" (seg_limit) : "0" (0), "rm" (seg));
  119. if ((~seg_ar & 0x9800) || eip > seg_limit) {
  120. *eip_limit = 0;
  121. return 1; /* So that returned eip > *eip_limit. */
  122. }
  123. /* Get the GDT/LDT descriptor base.
  124. When you look for races in this code remember that
  125. LDT and other horrors are only used in user space. */
  126. if (seg & (1<<2)) {
  127. /* Must lock the LDT while reading it. */
  128. down(&current->mm->context.sem);
  129. desc = current->mm->context.ldt;
  130. desc = (void *)desc + (seg & ~7);
  131. } else {
  132. /* Must disable preemption while reading the GDT. */
  133. desc = (u32 *)get_cpu_gdt_table(get_cpu());
  134. desc = (void *)desc + (seg & ~7);
  135. }
  136. /* Decode the code segment base from the descriptor */
  137. base = get_desc_base((unsigned long *)desc);
  138. if (seg & (1<<2)) {
  139. up(&current->mm->context.sem);
  140. } else
  141. put_cpu();
  142. /* Adjust EIP and segment limit, and clamp at the kernel limit.
  143. It's legitimate for segments to wrap at 0xffffffff. */
  144. seg_limit += base;
  145. if (seg_limit < *eip_limit && seg_limit >= base)
  146. *eip_limit = seg_limit;
  147. return eip + base;
  148. }
  149. /*
  150. * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
  151. * Check that here and ignore it.
  152. */
  153. static int __is_prefetch(struct pt_regs *regs, unsigned long addr)
  154. {
  155. unsigned long limit;
  156. unsigned long instr = get_segment_eip (regs, &limit);
  157. int scan_more = 1;
  158. int prefetch = 0;
  159. int i;
  160. for (i = 0; scan_more && i < 15; i++) {
  161. unsigned char opcode;
  162. unsigned char instr_hi;
  163. unsigned char instr_lo;
  164. if (instr > limit)
  165. break;
  166. if (__get_user(opcode, (unsigned char __user *) instr))
  167. break;
  168. instr_hi = opcode & 0xf0;
  169. instr_lo = opcode & 0x0f;
  170. instr++;
  171. switch (instr_hi) {
  172. case 0x20:
  173. case 0x30:
  174. /* Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes. */
  175. scan_more = ((instr_lo & 7) == 0x6);
  176. break;
  177. case 0x60:
  178. /* 0x64 thru 0x67 are valid prefixes in all modes. */
  179. scan_more = (instr_lo & 0xC) == 0x4;
  180. break;
  181. case 0xF0:
  182. /* 0xF0, 0xF2, and 0xF3 are valid prefixes */
  183. scan_more = !instr_lo || (instr_lo>>1) == 1;
  184. break;
  185. case 0x00:
  186. /* Prefetch instruction is 0x0F0D or 0x0F18 */
  187. scan_more = 0;
  188. if (instr > limit)
  189. break;
  190. if (__get_user(opcode, (unsigned char __user *) instr))
  191. break;
  192. prefetch = (instr_lo == 0xF) &&
  193. (opcode == 0x0D || opcode == 0x18);
  194. break;
  195. default:
  196. scan_more = 0;
  197. break;
  198. }
  199. }
  200. return prefetch;
  201. }
  202. static inline int is_prefetch(struct pt_regs *regs, unsigned long addr,
  203. unsigned long error_code)
  204. {
  205. if (unlikely(boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
  206. boot_cpu_data.x86 >= 6)) {
  207. /* Catch an obscure case of prefetch inside an NX page. */
  208. if (nx_enabled && (error_code & 16))
  209. return 0;
  210. return __is_prefetch(regs, addr);
  211. }
  212. return 0;
  213. }
  214. static noinline void force_sig_info_fault(int si_signo, int si_code,
  215. unsigned long address, struct task_struct *tsk)
  216. {
  217. siginfo_t info;
  218. info.si_signo = si_signo;
  219. info.si_errno = 0;
  220. info.si_code = si_code;
  221. info.si_addr = (void __user *)address;
  222. force_sig_info(si_signo, &info, tsk);
  223. }
  224. fastcall void do_invalid_op(struct pt_regs *, unsigned long);
  225. static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
  226. {
  227. unsigned index = pgd_index(address);
  228. pgd_t *pgd_k;
  229. pud_t *pud, *pud_k;
  230. pmd_t *pmd, *pmd_k;
  231. pgd += index;
  232. pgd_k = init_mm.pgd + index;
  233. if (!pgd_present(*pgd_k))
  234. return NULL;
  235. /*
  236. * set_pgd(pgd, *pgd_k); here would be useless on PAE
  237. * and redundant with the set_pmd() on non-PAE. As would
  238. * set_pud.
  239. */
  240. pud = pud_offset(pgd, address);
  241. pud_k = pud_offset(pgd_k, address);
  242. if (!pud_present(*pud_k))
  243. return NULL;
  244. pmd = pmd_offset(pud, address);
  245. pmd_k = pmd_offset(pud_k, address);
  246. if (!pmd_present(*pmd_k))
  247. return NULL;
  248. if (!pmd_present(*pmd))
  249. set_pmd(pmd, *pmd_k);
  250. else
  251. BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
  252. return pmd_k;
  253. }
  254. /*
  255. * Handle a fault on the vmalloc or module mapping area
  256. *
  257. * This assumes no large pages in there.
  258. */
  259. static inline int vmalloc_fault(unsigned long address)
  260. {
  261. unsigned long pgd_paddr;
  262. pmd_t *pmd_k;
  263. pte_t *pte_k;
  264. /*
  265. * Synchronize this task's top level page-table
  266. * with the 'reference' page table.
  267. *
  268. * Do _not_ use "current" here. We might be inside
  269. * an interrupt in the middle of a task switch..
  270. */
  271. pgd_paddr = read_cr3();
  272. pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
  273. if (!pmd_k)
  274. return -1;
  275. pte_k = pte_offset_kernel(pmd_k, address);
  276. if (!pte_present(*pte_k))
  277. return -1;
  278. return 0;
  279. }
  280. /*
  281. * This routine handles page faults. It determines the address,
  282. * and the problem, and then passes it off to one of the appropriate
  283. * routines.
  284. *
  285. * error_code:
  286. * bit 0 == 0 means no page found, 1 means protection fault
  287. * bit 1 == 0 means read, 1 means write
  288. * bit 2 == 0 means kernel, 1 means user-mode
  289. * bit 3 == 1 means use of reserved bit detected
  290. * bit 4 == 1 means fault was an instruction fetch
  291. */
  292. fastcall void __kprobes do_page_fault(struct pt_regs *regs,
  293. unsigned long error_code)
  294. {
  295. struct task_struct *tsk;
  296. struct mm_struct *mm;
  297. struct vm_area_struct * vma;
  298. unsigned long address;
  299. unsigned long page;
  300. int write, si_code;
  301. /* get the address */
  302. address = read_cr2();
  303. tsk = current;
  304. si_code = SEGV_MAPERR;
  305. /*
  306. * We fault-in kernel-space virtual memory on-demand. The
  307. * 'reference' page table is init_mm.pgd.
  308. *
  309. * NOTE! We MUST NOT take any locks for this case. We may
  310. * be in an interrupt or a critical region, and should
  311. * only copy the information from the master page table,
  312. * nothing more.
  313. *
  314. * This verifies that the fault happens in kernel space
  315. * (error_code & 4) == 0, and that the fault was not a
  316. * protection error (error_code & 9) == 0.
  317. */
  318. if (unlikely(address >= TASK_SIZE)) {
  319. if (!(error_code & 0x0000000d) && vmalloc_fault(address) >= 0)
  320. return;
  321. if (notify_page_fault(DIE_PAGE_FAULT, "page fault", regs, error_code, 14,
  322. SIGSEGV) == NOTIFY_STOP)
  323. return;
  324. /*
  325. * Don't take the mm semaphore here. If we fixup a prefetch
  326. * fault we could otherwise deadlock.
  327. */
  328. goto bad_area_nosemaphore;
  329. }
  330. if (notify_page_fault(DIE_PAGE_FAULT, "page fault", regs, error_code, 14,
  331. SIGSEGV) == NOTIFY_STOP)
  332. return;
  333. /* It's safe to allow irq's after cr2 has been saved and the vmalloc
  334. fault has been handled. */
  335. if (regs->eflags & (X86_EFLAGS_IF|VM_MASK))
  336. local_irq_enable();
  337. mm = tsk->mm;
  338. /*
  339. * If we're in an interrupt, have no user context or are running in an
  340. * atomic region then we must not take the fault..
  341. */
  342. if (in_atomic() || !mm)
  343. goto bad_area_nosemaphore;
  344. /* When running in the kernel we expect faults to occur only to
  345. * addresses in user space. All other faults represent errors in the
  346. * kernel and should generate an OOPS. Unfortunatly, in the case of an
  347. * erroneous fault occurring in a code path which already holds mmap_sem
  348. * we will deadlock attempting to validate the fault against the
  349. * address space. Luckily the kernel only validly references user
  350. * space from well defined areas of code, which are listed in the
  351. * exceptions table.
  352. *
  353. * As the vast majority of faults will be valid we will only perform
  354. * the source reference check when there is a possibilty of a deadlock.
  355. * Attempt to lock the address space, if we cannot we then validate the
  356. * source. If this is invalid we can skip the address space check,
  357. * thus avoiding the deadlock.
  358. */
  359. if (!down_read_trylock(&mm->mmap_sem)) {
  360. if ((error_code & 4) == 0 &&
  361. !search_exception_tables(regs->eip))
  362. goto bad_area_nosemaphore;
  363. down_read(&mm->mmap_sem);
  364. }
  365. vma = find_vma(mm, address);
  366. if (!vma)
  367. goto bad_area;
  368. if (vma->vm_start <= address)
  369. goto good_area;
  370. if (!(vma->vm_flags & VM_GROWSDOWN))
  371. goto bad_area;
  372. if (error_code & 4) {
  373. /*
  374. * Accessing the stack below %esp is always a bug.
  375. * The large cushion allows instructions like enter
  376. * and pusha to work. ("enter $65535,$31" pushes
  377. * 32 pointers and then decrements %esp by 65535.)
  378. */
  379. if (address + 65536 + 32 * sizeof(unsigned long) < regs->esp)
  380. goto bad_area;
  381. }
  382. if (expand_stack(vma, address))
  383. goto bad_area;
  384. /*
  385. * Ok, we have a good vm_area for this memory access, so
  386. * we can handle it..
  387. */
  388. good_area:
  389. si_code = SEGV_ACCERR;
  390. write = 0;
  391. switch (error_code & 3) {
  392. default: /* 3: write, present */
  393. #ifdef TEST_VERIFY_AREA
  394. if (regs->cs == KERNEL_CS)
  395. printk("WP fault at %08lx\n", regs->eip);
  396. #endif
  397. /* fall through */
  398. case 2: /* write, not present */
  399. if (!(vma->vm_flags & VM_WRITE))
  400. goto bad_area;
  401. write++;
  402. break;
  403. case 1: /* read, present */
  404. goto bad_area;
  405. case 0: /* read, not present */
  406. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  407. goto bad_area;
  408. }
  409. survive:
  410. /*
  411. * If for any reason at all we couldn't handle the fault,
  412. * make sure we exit gracefully rather than endlessly redo
  413. * the fault.
  414. */
  415. switch (handle_mm_fault(mm, vma, address, write)) {
  416. case VM_FAULT_MINOR:
  417. tsk->min_flt++;
  418. break;
  419. case VM_FAULT_MAJOR:
  420. tsk->maj_flt++;
  421. break;
  422. case VM_FAULT_SIGBUS:
  423. goto do_sigbus;
  424. case VM_FAULT_OOM:
  425. goto out_of_memory;
  426. default:
  427. BUG();
  428. }
  429. /*
  430. * Did it hit the DOS screen memory VA from vm86 mode?
  431. */
  432. if (regs->eflags & VM_MASK) {
  433. unsigned long bit = (address - 0xA0000) >> PAGE_SHIFT;
  434. if (bit < 32)
  435. tsk->thread.screen_bitmap |= 1 << bit;
  436. }
  437. up_read(&mm->mmap_sem);
  438. return;
  439. /*
  440. * Something tried to access memory that isn't in our memory map..
  441. * Fix it, but check if it's kernel or user first..
  442. */
  443. bad_area:
  444. up_read(&mm->mmap_sem);
  445. bad_area_nosemaphore:
  446. /* User mode accesses just cause a SIGSEGV */
  447. if (error_code & 4) {
  448. /*
  449. * Valid to do another page fault here because this one came
  450. * from user space.
  451. */
  452. if (is_prefetch(regs, address, error_code))
  453. return;
  454. tsk->thread.cr2 = address;
  455. /* Kernel addresses are always protection faults */
  456. tsk->thread.error_code = error_code | (address >= TASK_SIZE);
  457. tsk->thread.trap_no = 14;
  458. force_sig_info_fault(SIGSEGV, si_code, address, tsk);
  459. return;
  460. }
  461. #ifdef CONFIG_X86_F00F_BUG
  462. /*
  463. * Pentium F0 0F C7 C8 bug workaround.
  464. */
  465. if (boot_cpu_data.f00f_bug) {
  466. unsigned long nr;
  467. nr = (address - idt_descr.address) >> 3;
  468. if (nr == 6) {
  469. do_invalid_op(regs, 0);
  470. return;
  471. }
  472. }
  473. #endif
  474. no_context:
  475. /* Are we prepared to handle this kernel fault? */
  476. if (fixup_exception(regs))
  477. return;
  478. /*
  479. * Valid to do another page fault here, because if this fault
  480. * had been triggered by is_prefetch fixup_exception would have
  481. * handled it.
  482. */
  483. if (is_prefetch(regs, address, error_code))
  484. return;
  485. /*
  486. * Oops. The kernel tried to access some bad page. We'll have to
  487. * terminate things with extreme prejudice.
  488. */
  489. bust_spinlocks(1);
  490. if (oops_may_print()) {
  491. #ifdef CONFIG_X86_PAE
  492. if (error_code & 16) {
  493. pte_t *pte = lookup_address(address);
  494. if (pte && pte_present(*pte) && !pte_exec_kernel(*pte))
  495. printk(KERN_CRIT "kernel tried to execute "
  496. "NX-protected page - exploit attempt? "
  497. "(uid: %d)\n", current->uid);
  498. }
  499. #endif
  500. if (address < PAGE_SIZE)
  501. printk(KERN_ALERT "BUG: unable to handle kernel NULL "
  502. "pointer dereference");
  503. else
  504. printk(KERN_ALERT "BUG: unable to handle kernel paging"
  505. " request");
  506. printk(" at virtual address %08lx\n",address);
  507. printk(KERN_ALERT " printing eip:\n");
  508. printk("%08lx\n", regs->eip);
  509. }
  510. page = read_cr3();
  511. page = ((unsigned long *) __va(page))[address >> 22];
  512. if (oops_may_print())
  513. printk(KERN_ALERT "*pde = %08lx\n", page);
  514. /*
  515. * We must not directly access the pte in the highpte
  516. * case, the page table might be allocated in highmem.
  517. * And lets rather not kmap-atomic the pte, just in case
  518. * it's allocated already.
  519. */
  520. #ifndef CONFIG_HIGHPTE
  521. if ((page & 1) && oops_may_print()) {
  522. page &= PAGE_MASK;
  523. address &= 0x003ff000;
  524. page = ((unsigned long *) __va(page))[address >> PAGE_SHIFT];
  525. printk(KERN_ALERT "*pte = %08lx\n", page);
  526. }
  527. #endif
  528. tsk->thread.cr2 = address;
  529. tsk->thread.trap_no = 14;
  530. tsk->thread.error_code = error_code;
  531. die("Oops", regs, error_code);
  532. bust_spinlocks(0);
  533. do_exit(SIGKILL);
  534. /*
  535. * We ran out of memory, or some other thing happened to us that made
  536. * us unable to handle the page fault gracefully.
  537. */
  538. out_of_memory:
  539. up_read(&mm->mmap_sem);
  540. if (tsk->pid == 1) {
  541. yield();
  542. down_read(&mm->mmap_sem);
  543. goto survive;
  544. }
  545. printk("VM: killing process %s\n", tsk->comm);
  546. if (error_code & 4)
  547. do_exit(SIGKILL);
  548. goto no_context;
  549. do_sigbus:
  550. up_read(&mm->mmap_sem);
  551. /* Kernel mode? Handle exceptions or die */
  552. if (!(error_code & 4))
  553. goto no_context;
  554. /* User space => ok to do another page fault */
  555. if (is_prefetch(regs, address, error_code))
  556. return;
  557. tsk->thread.cr2 = address;
  558. tsk->thread.error_code = error_code;
  559. tsk->thread.trap_no = 14;
  560. force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk);
  561. }
  562. #ifndef CONFIG_X86_PAE
  563. void vmalloc_sync_all(void)
  564. {
  565. /*
  566. * Note that races in the updates of insync and start aren't
  567. * problematic: insync can only get set bits added, and updates to
  568. * start are only improving performance (without affecting correctness
  569. * if undone).
  570. */
  571. static DECLARE_BITMAP(insync, PTRS_PER_PGD);
  572. static unsigned long start = TASK_SIZE;
  573. unsigned long address;
  574. BUILD_BUG_ON(TASK_SIZE & ~PGDIR_MASK);
  575. for (address = start; address >= TASK_SIZE; address += PGDIR_SIZE) {
  576. if (!test_bit(pgd_index(address), insync)) {
  577. unsigned long flags;
  578. struct page *page;
  579. spin_lock_irqsave(&pgd_lock, flags);
  580. for (page = pgd_list; page; page =
  581. (struct page *)page->index)
  582. if (!vmalloc_sync_one(page_address(page),
  583. address)) {
  584. BUG_ON(page != pgd_list);
  585. break;
  586. }
  587. spin_unlock_irqrestore(&pgd_lock, flags);
  588. if (!page)
  589. set_bit(pgd_index(address), insync);
  590. }
  591. if (address == start && test_bit(pgd_index(address), insync))
  592. start = address + PGDIR_SIZE;
  593. }
  594. }
  595. #endif