fault.c 17 KB

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