fault.c 14 KB

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