sysenter.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. unsigned int __read_mostly vdso_enabled = 1;
  28. EXPORT_SYMBOL_GPL(vdso_enabled);
  29. static int __init vdso_setup(char *s)
  30. {
  31. vdso_enabled = simple_strtoul(s, NULL, 0);
  32. return 1;
  33. }
  34. __setup("vdso=", vdso_setup);
  35. extern asmlinkage void sysenter_entry(void);
  36. void enable_sep_cpu(void)
  37. {
  38. int cpu = get_cpu();
  39. struct tss_struct *tss = &per_cpu(init_tss, cpu);
  40. if (!boot_cpu_has(X86_FEATURE_SEP)) {
  41. put_cpu();
  42. return;
  43. }
  44. tss->ss1 = __KERNEL_CS;
  45. tss->esp1 = sizeof(struct tss_struct) + (unsigned long) tss;
  46. wrmsr(MSR_IA32_SYSENTER_CS, __KERNEL_CS, 0);
  47. wrmsr(MSR_IA32_SYSENTER_ESP, tss->esp1, 0);
  48. wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long) sysenter_entry, 0);
  49. put_cpu();
  50. }
  51. /*
  52. * These symbols are defined by vsyscall.o to mark the bounds
  53. * of the ELF DSO images included therein.
  54. */
  55. extern const char vsyscall_int80_start, vsyscall_int80_end;
  56. extern const char vsyscall_sysenter_start, vsyscall_sysenter_end;
  57. static void *syscall_page;
  58. int __init sysenter_setup(void)
  59. {
  60. syscall_page = (void *)get_zeroed_page(GFP_ATOMIC);
  61. #ifdef CONFIG_COMPAT_VDSO
  62. __set_fixmap(FIX_VDSO, __pa(syscall_page), PAGE_READONLY);
  63. printk("Compat vDSO mapped to %08lx.\n", __fix_to_virt(FIX_VDSO));
  64. #else
  65. /*
  66. * In the non-compat case the ELF coredumping code needs the fixmap:
  67. */
  68. __set_fixmap(FIX_VDSO, __pa(syscall_page), PAGE_KERNEL_RO);
  69. #endif
  70. if (!boot_cpu_has(X86_FEATURE_SEP)) {
  71. memcpy(syscall_page,
  72. &vsyscall_int80_start,
  73. &vsyscall_int80_end - &vsyscall_int80_start);
  74. return 0;
  75. }
  76. memcpy(syscall_page,
  77. &vsyscall_sysenter_start,
  78. &vsyscall_sysenter_end - &vsyscall_sysenter_start);
  79. return 0;
  80. }
  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. vma->vm_flags |= mm->def_flags;
  121. vma->vm_page_prot = protection_map[vma->vm_flags & 7];
  122. vma->vm_ops = &syscall_vm_ops;
  123. vma->vm_mm = mm;
  124. ret = insert_vm_struct(mm, vma);
  125. if (unlikely(ret)) {
  126. kmem_cache_free(vm_area_cachep, vma);
  127. goto up_fail;
  128. }
  129. current->mm->context.vdso = (void *)addr;
  130. current_thread_info()->sysenter_return =
  131. (void *)VDSO_SYM(&SYSENTER_RETURN);
  132. mm->total_vm++;
  133. up_fail:
  134. up_write(&mm->mmap_sem);
  135. return ret;
  136. }
  137. const char *arch_vma_name(struct vm_area_struct *vma)
  138. {
  139. if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
  140. return "[vdso]";
  141. return NULL;
  142. }
  143. struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
  144. {
  145. return NULL;
  146. }
  147. int in_gate_area(struct task_struct *task, unsigned long addr)
  148. {
  149. return 0;
  150. }
  151. int in_gate_area_no_task(unsigned long addr)
  152. {
  153. return 0;
  154. }