sysenter.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. #else
  69. /*
  70. * In the non-compat case the ELF coredumping code needs the fixmap:
  71. */
  72. __set_fixmap(FIX_VDSO, __pa(syscall_page), PAGE_KERNEL_RO);
  73. #endif
  74. if (!boot_cpu_has(X86_FEATURE_SEP)) {
  75. memcpy(syscall_page,
  76. &vsyscall_int80_start,
  77. &vsyscall_int80_end - &vsyscall_int80_start);
  78. return 0;
  79. }
  80. memcpy(syscall_page,
  81. &vsyscall_sysenter_start,
  82. &vsyscall_sysenter_end - &vsyscall_sysenter_start);
  83. return 0;
  84. }
  85. static struct page *syscall_nopage(struct vm_area_struct *vma,
  86. unsigned long adr, int *type)
  87. {
  88. struct page *p = virt_to_page(adr - vma->vm_start + syscall_page);
  89. get_page(p);
  90. return p;
  91. }
  92. /* Prevent VMA merging */
  93. static void syscall_vma_close(struct vm_area_struct *vma)
  94. {
  95. }
  96. static struct vm_operations_struct syscall_vm_ops = {
  97. .close = syscall_vma_close,
  98. .nopage = syscall_nopage,
  99. };
  100. /* Defined in vsyscall-sysenter.S */
  101. extern void SYSENTER_RETURN;
  102. /* Setup a VMA at program startup for the vsyscall page */
  103. int arch_setup_additional_pages(struct linux_binprm *bprm, int exstack)
  104. {
  105. struct vm_area_struct *vma;
  106. struct mm_struct *mm = current->mm;
  107. unsigned long addr;
  108. int ret;
  109. down_write(&mm->mmap_sem);
  110. addr = get_unmapped_area(NULL, 0, PAGE_SIZE, 0, 0);
  111. if (IS_ERR_VALUE(addr)) {
  112. ret = addr;
  113. goto up_fail;
  114. }
  115. vma = kmem_cache_zalloc(vm_area_cachep, SLAB_KERNEL);
  116. if (!vma) {
  117. ret = -ENOMEM;
  118. goto up_fail;
  119. }
  120. vma->vm_start = addr;
  121. vma->vm_end = addr + PAGE_SIZE;
  122. /* MAYWRITE to allow gdb to COW and set breakpoints */
  123. vma->vm_flags = VM_READ|VM_EXEC|VM_MAYREAD|VM_MAYEXEC|VM_MAYWRITE;
  124. vma->vm_flags |= mm->def_flags;
  125. vma->vm_page_prot = protection_map[vma->vm_flags & 7];
  126. vma->vm_ops = &syscall_vm_ops;
  127. vma->vm_mm = mm;
  128. ret = insert_vm_struct(mm, vma);
  129. if (unlikely(ret)) {
  130. kmem_cache_free(vm_area_cachep, vma);
  131. goto up_fail;
  132. }
  133. current->mm->context.vdso = (void *)addr;
  134. current_thread_info()->sysenter_return =
  135. (void *)VDSO_SYM(&SYSENTER_RETURN);
  136. mm->total_vm++;
  137. up_fail:
  138. up_write(&mm->mmap_sem);
  139. return ret;
  140. }
  141. const char *arch_vma_name(struct vm_area_struct *vma)
  142. {
  143. if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
  144. return "[vdso]";
  145. return NULL;
  146. }
  147. struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
  148. {
  149. return NULL;
  150. }
  151. int in_gate_area(struct task_struct *task, unsigned long addr)
  152. {
  153. return 0;
  154. }
  155. int in_gate_area_no_task(unsigned long addr)
  156. {
  157. return 0;
  158. }