sysenter.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * linux/arch/i386/kernel/sysenter.c
  3. *
  4. * (C) Copyright 2002 Linus Torvalds
  5. * Portions based on the vdso-randomization code from exec-shield:
  6. * Copyright(C) 2005-2006, Red Hat, Inc., Ingo Molnar
  7. *
  8. * This file contains the needed initializations to support sysenter.
  9. */
  10. #include <linux/init.h>
  11. #include <linux/smp.h>
  12. #include <linux/thread_info.h>
  13. #include <linux/sched.h>
  14. #include <linux/gfp.h>
  15. #include <linux/string.h>
  16. #include <linux/elf.h>
  17. #include <linux/mm.h>
  18. #include <linux/module.h>
  19. #include <asm/cpufeature.h>
  20. #include <asm/msr.h>
  21. #include <asm/pgtable.h>
  22. #include <asm/unistd.h>
  23. /*
  24. * Should the kernel map a VDSO page into processes and pass its
  25. * address down to glibc upon exec()?
  26. */
  27. #ifdef CONFIG_PARAVIRT
  28. unsigned int __read_mostly vdso_enabled = 0;
  29. #else
  30. unsigned int __read_mostly vdso_enabled = 1;
  31. #endif
  32. EXPORT_SYMBOL_GPL(vdso_enabled);
  33. static int __init vdso_setup(char *s)
  34. {
  35. vdso_enabled = simple_strtoul(s, NULL, 0);
  36. return 1;
  37. }
  38. __setup("vdso=", vdso_setup);
  39. extern asmlinkage void sysenter_entry(void);
  40. void enable_sep_cpu(void)
  41. {
  42. int cpu = get_cpu();
  43. struct tss_struct *tss = &per_cpu(init_tss, cpu);
  44. if (!boot_cpu_has(X86_FEATURE_SEP)) {
  45. put_cpu();
  46. return;
  47. }
  48. tss->ss1 = __KERNEL_CS;
  49. tss->esp1 = sizeof(struct tss_struct) + (unsigned long) tss;
  50. wrmsr(MSR_IA32_SYSENTER_CS, __KERNEL_CS, 0);
  51. wrmsr(MSR_IA32_SYSENTER_ESP, tss->esp1, 0);
  52. wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long) sysenter_entry, 0);
  53. put_cpu();
  54. }
  55. /*
  56. * These symbols are defined by vsyscall.o to mark the bounds
  57. * of the ELF DSO images included therein.
  58. */
  59. extern const char vsyscall_int80_start, vsyscall_int80_end;
  60. extern const char vsyscall_sysenter_start, vsyscall_sysenter_end;
  61. static void *syscall_page;
  62. int __init sysenter_setup(void)
  63. {
  64. syscall_page = (void *)get_zeroed_page(GFP_ATOMIC);
  65. #ifdef CONFIG_COMPAT_VDSO
  66. __set_fixmap(FIX_VDSO, __pa(syscall_page), PAGE_READONLY);
  67. printk("Compat vDSO mapped to %08lx.\n", __fix_to_virt(FIX_VDSO));
  68. #endif
  69. if (!boot_cpu_has(X86_FEATURE_SEP)) {
  70. memcpy(syscall_page,
  71. &vsyscall_int80_start,
  72. &vsyscall_int80_end - &vsyscall_int80_start);
  73. return 0;
  74. }
  75. memcpy(syscall_page,
  76. &vsyscall_sysenter_start,
  77. &vsyscall_sysenter_end - &vsyscall_sysenter_start);
  78. return 0;
  79. }
  80. #ifndef CONFIG_COMPAT_VDSO
  81. static struct page *syscall_nopage(struct vm_area_struct *vma,
  82. unsigned long adr, int *type)
  83. {
  84. struct page *p = virt_to_page(adr - vma->vm_start + syscall_page);
  85. get_page(p);
  86. return p;
  87. }
  88. /* Prevent VMA merging */
  89. static void syscall_vma_close(struct vm_area_struct *vma)
  90. {
  91. }
  92. static struct vm_operations_struct syscall_vm_ops = {
  93. .close = syscall_vma_close,
  94. .nopage = syscall_nopage,
  95. };
  96. /* Defined in vsyscall-sysenter.S */
  97. extern void SYSENTER_RETURN;
  98. /* Setup a VMA at program startup for the vsyscall page */
  99. int arch_setup_additional_pages(struct linux_binprm *bprm, int exstack)
  100. {
  101. struct vm_area_struct *vma;
  102. struct mm_struct *mm = current->mm;
  103. unsigned long addr;
  104. int ret;
  105. down_write(&mm->mmap_sem);
  106. addr = get_unmapped_area(NULL, 0, PAGE_SIZE, 0, 0);
  107. if (IS_ERR_VALUE(addr)) {
  108. ret = addr;
  109. goto up_fail;
  110. }
  111. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  112. if (!vma) {
  113. ret = -ENOMEM;
  114. goto up_fail;
  115. }
  116. vma->vm_start = addr;
  117. vma->vm_end = addr + PAGE_SIZE;
  118. /* MAYWRITE to allow gdb to COW and set breakpoints */
  119. vma->vm_flags = VM_READ|VM_EXEC|VM_MAYREAD|VM_MAYEXEC|VM_MAYWRITE;
  120. /*
  121. * Make sure the vDSO gets into every core dump.
  122. * Dumping its contents makes post-mortem fully interpretable later
  123. * without matching up the same kernel and hardware config to see
  124. * what PC values meant.
  125. */
  126. vma->vm_flags |= VM_ALWAYSDUMP;
  127. vma->vm_flags |= mm->def_flags;
  128. vma->vm_page_prot = protection_map[vma->vm_flags & 7];
  129. vma->vm_ops = &syscall_vm_ops;
  130. vma->vm_mm = mm;
  131. ret = insert_vm_struct(mm, vma);
  132. if (unlikely(ret)) {
  133. kmem_cache_free(vm_area_cachep, vma);
  134. goto up_fail;
  135. }
  136. current->mm->context.vdso = (void *)addr;
  137. current_thread_info()->sysenter_return =
  138. (void *)VDSO_SYM(&SYSENTER_RETURN);
  139. mm->total_vm++;
  140. up_fail:
  141. up_write(&mm->mmap_sem);
  142. return ret;
  143. }
  144. const char *arch_vma_name(struct vm_area_struct *vma)
  145. {
  146. if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
  147. return "[vdso]";
  148. return NULL;
  149. }
  150. struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
  151. {
  152. return NULL;
  153. }
  154. int in_gate_area(struct task_struct *task, unsigned long addr)
  155. {
  156. return 0;
  157. }
  158. int in_gate_area_no_task(unsigned long addr)
  159. {
  160. return 0;
  161. }
  162. #endif