fault.c 14 KB

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