vdso32-setup.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * (C) Copyright 2002 Linus Torvalds
  3. * Portions based on the vdso-randomization code from exec-shield:
  4. * Copyright(C) 2005-2006, Red Hat, Inc., Ingo Molnar
  5. *
  6. * This file contains the needed initializations to support sysenter.
  7. */
  8. #include <linux/init.h>
  9. #include <linux/smp.h>
  10. #include <linux/thread_info.h>
  11. #include <linux/sched.h>
  12. #include <linux/gfp.h>
  13. #include <linux/string.h>
  14. #include <linux/elf.h>
  15. #include <linux/mm.h>
  16. #include <linux/err.h>
  17. #include <linux/module.h>
  18. #include <linux/slab.h>
  19. #include <asm/cpufeature.h>
  20. #include <asm/msr.h>
  21. #include <asm/pgtable.h>
  22. #include <asm/unistd.h>
  23. #include <asm/elf.h>
  24. #include <asm/tlbflush.h>
  25. #include <asm/vdso.h>
  26. #include <asm/proto.h>
  27. #include <asm/fixmap.h>
  28. #include <asm/hpet.h>
  29. #include <asm/vvar.h>
  30. #ifdef CONFIG_COMPAT_VDSO
  31. #define VDSO_DEFAULT 0
  32. #else
  33. #define VDSO_DEFAULT 1
  34. #endif
  35. #ifdef CONFIG_X86_64
  36. #define vdso_enabled sysctl_vsyscall32
  37. #define arch_setup_additional_pages syscall32_setup_pages
  38. extern int sysctl_ldt16;
  39. #endif
  40. /*
  41. * Should the kernel map a VDSO page into processes and pass its
  42. * address down to glibc upon exec()?
  43. */
  44. unsigned int __read_mostly vdso_enabled = VDSO_DEFAULT;
  45. static int __init vdso_setup(char *s)
  46. {
  47. vdso_enabled = simple_strtoul(s, NULL, 0);
  48. if (vdso_enabled > 1)
  49. pr_warn("vdso32 values other than 0 and 1 are no longer allowed; vdso disabled\n");
  50. return 1;
  51. }
  52. /*
  53. * For consistency, the argument vdso32=[012] affects the 32-bit vDSO
  54. * behavior on both 64-bit and 32-bit kernels.
  55. * On 32-bit kernels, vdso=[012] means the same thing.
  56. */
  57. __setup("vdso32=", vdso_setup);
  58. #ifdef CONFIG_X86_32
  59. __setup_param("vdso=", vdso32_setup, vdso_setup, 0);
  60. EXPORT_SYMBOL_GPL(vdso_enabled);
  61. #endif
  62. static struct page **vdso32_pages;
  63. static unsigned vdso32_size;
  64. #ifdef CONFIG_X86_64
  65. #define vdso32_sysenter() (boot_cpu_has(X86_FEATURE_SYSENTER32))
  66. #define vdso32_syscall() (boot_cpu_has(X86_FEATURE_SYSCALL32))
  67. /* May not be __init: called during resume */
  68. void syscall32_cpu_init(void)
  69. {
  70. /* Load these always in case some future AMD CPU supports
  71. SYSENTER from compat mode too. */
  72. wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
  73. wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
  74. wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)ia32_sysenter_target);
  75. wrmsrl(MSR_CSTAR, ia32_cstar_target);
  76. }
  77. #else /* CONFIG_X86_32 */
  78. #define vdso32_sysenter() (boot_cpu_has(X86_FEATURE_SEP))
  79. #define vdso32_syscall() (0)
  80. void enable_sep_cpu(void)
  81. {
  82. int cpu = get_cpu();
  83. struct tss_struct *tss = &per_cpu(init_tss, cpu);
  84. if (!boot_cpu_has(X86_FEATURE_SEP)) {
  85. put_cpu();
  86. return;
  87. }
  88. tss->x86_tss.ss1 = __KERNEL_CS;
  89. tss->x86_tss.sp1 = sizeof(struct tss_struct) + (unsigned long) tss;
  90. wrmsr(MSR_IA32_SYSENTER_CS, __KERNEL_CS, 0);
  91. wrmsr(MSR_IA32_SYSENTER_ESP, tss->x86_tss.sp1, 0);
  92. wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long) ia32_sysenter_target, 0);
  93. put_cpu();
  94. }
  95. #endif /* CONFIG_X86_64 */
  96. int __init sysenter_setup(void)
  97. {
  98. char *vdso32_start, *vdso32_end;
  99. int npages, i;
  100. #ifdef CONFIG_COMPAT
  101. if (vdso32_syscall()) {
  102. vdso32_start = vdso32_syscall_start;
  103. vdso32_end = vdso32_syscall_end;
  104. vdso32_pages = vdso32_syscall_pages;
  105. } else
  106. #endif
  107. if (vdso32_sysenter()) {
  108. vdso32_start = vdso32_sysenter_start;
  109. vdso32_end = vdso32_sysenter_end;
  110. vdso32_pages = vdso32_sysenter_pages;
  111. } else {
  112. vdso32_start = vdso32_int80_start;
  113. vdso32_end = vdso32_int80_end;
  114. vdso32_pages = vdso32_int80_pages;
  115. }
  116. npages = ((vdso32_end - vdso32_start) + PAGE_SIZE - 1) / PAGE_SIZE;
  117. vdso32_size = npages << PAGE_SHIFT;
  118. for (i = 0; i < npages; i++)
  119. vdso32_pages[i] = virt_to_page(vdso32_start + i*PAGE_SIZE);
  120. patch_vdso32(vdso32_start, vdso32_size);
  121. return 0;
  122. }
  123. /* Setup a VMA at program startup for the vsyscall page */
  124. int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  125. {
  126. struct mm_struct *mm = current->mm;
  127. unsigned long addr;
  128. int ret = 0;
  129. struct vm_area_struct *vma;
  130. static struct page *no_pages[] = {NULL};
  131. #ifdef CONFIG_X86_X32_ABI
  132. if (test_thread_flag(TIF_X32))
  133. return x32_setup_additional_pages(bprm, uses_interp);
  134. #endif
  135. if (vdso_enabled != 1) /* Other values all mean "disabled" */
  136. return 0;
  137. down_write(&mm->mmap_sem);
  138. addr = get_unmapped_area(NULL, 0, vdso32_size + VDSO_OFFSET(VDSO_PREV_PAGES), 0, 0);
  139. if (IS_ERR_VALUE(addr)) {
  140. ret = addr;
  141. goto up_fail;
  142. }
  143. addr += VDSO_OFFSET(VDSO_PREV_PAGES);
  144. current->mm->context.vdso = (void *)addr;
  145. /*
  146. * MAYWRITE to allow gdb to COW and set breakpoints
  147. */
  148. ret = install_special_mapping(mm,
  149. addr,
  150. vdso32_size,
  151. VM_READ|VM_EXEC|
  152. VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
  153. vdso32_pages);
  154. if (ret)
  155. goto up_fail;
  156. vma = _install_special_mapping(mm,
  157. addr - VDSO_OFFSET(VDSO_PREV_PAGES),
  158. VDSO_OFFSET(VDSO_PREV_PAGES),
  159. VM_READ,
  160. no_pages);
  161. if (IS_ERR(vma)) {
  162. ret = PTR_ERR(vma);
  163. goto up_fail;
  164. }
  165. ret = remap_pfn_range(vma,
  166. addr - VDSO_OFFSET(VDSO_VVAR_PAGE),
  167. __pa_symbol(&__vvar_page) >> PAGE_SHIFT,
  168. PAGE_SIZE,
  169. PAGE_READONLY);
  170. if (ret)
  171. goto up_fail;
  172. #ifdef CONFIG_HPET_TIMER
  173. if (hpet_address) {
  174. ret = io_remap_pfn_range(vma,
  175. addr - VDSO_OFFSET(VDSO_HPET_PAGE),
  176. hpet_address >> PAGE_SHIFT,
  177. PAGE_SIZE,
  178. pgprot_noncached(PAGE_READONLY));
  179. if (ret)
  180. goto up_fail;
  181. }
  182. #endif
  183. current_thread_info()->sysenter_return =
  184. VDSO32_SYMBOL(addr, SYSENTER_RETURN);
  185. up_fail:
  186. if (ret)
  187. current->mm->context.vdso = NULL;
  188. up_write(&mm->mmap_sem);
  189. return ret;
  190. }
  191. #ifdef CONFIG_X86_64
  192. subsys_initcall(sysenter_setup);
  193. #ifdef CONFIG_SYSCTL
  194. /* Register vsyscall32 into the ABI table */
  195. #include <linux/sysctl.h>
  196. static struct ctl_table abi_table2[] = {
  197. {
  198. .procname = "vsyscall32",
  199. .data = &sysctl_vsyscall32,
  200. .maxlen = sizeof(int),
  201. .mode = 0644,
  202. .proc_handler = proc_dointvec
  203. },
  204. {
  205. .procname = "ldt16",
  206. .data = &sysctl_ldt16,
  207. .maxlen = sizeof(int),
  208. .mode = 0644,
  209. .proc_handler = proc_dointvec
  210. },
  211. {}
  212. };
  213. static struct ctl_table abi_root_table2[] = {
  214. {
  215. .procname = "abi",
  216. .mode = 0555,
  217. .child = abi_table2
  218. },
  219. {}
  220. };
  221. static __init int ia32_binfmt_init(void)
  222. {
  223. register_sysctl_table(abi_root_table2);
  224. return 0;
  225. }
  226. __initcall(ia32_binfmt_init);
  227. #endif
  228. #else /* CONFIG_X86_32 */
  229. const char *arch_vma_name(struct vm_area_struct *vma)
  230. {
  231. if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
  232. return "[vdso]";
  233. return NULL;
  234. }
  235. struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
  236. {
  237. return NULL;
  238. }
  239. int in_gate_area(struct mm_struct *mm, unsigned long addr)
  240. {
  241. return 0;
  242. }
  243. int in_gate_area_no_mm(unsigned long addr)
  244. {
  245. return 0;
  246. }
  247. #endif /* CONFIG_X86_64 */