vdso32-setup.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 <asm/cpufeature.h>
  19. #include <asm/msr.h>
  20. #include <asm/pgtable.h>
  21. #include <asm/unistd.h>
  22. #include <asm/elf.h>
  23. #include <asm/tlbflush.h>
  24. #include <asm/vdso.h>
  25. #include <asm/proto.h>
  26. enum {
  27. VDSO_DISABLED = 0,
  28. VDSO_ENABLED = 1,
  29. VDSO_COMPAT = 2,
  30. };
  31. #ifdef CONFIG_COMPAT_VDSO
  32. #define VDSO_DEFAULT VDSO_COMPAT
  33. #else
  34. #define VDSO_DEFAULT VDSO_ENABLED
  35. #endif
  36. #ifdef CONFIG_X86_64
  37. #define vdso_enabled sysctl_vsyscall32
  38. #define arch_setup_additional_pages syscall32_setup_pages
  39. #endif
  40. /*
  41. * This is the difference between the prelinked addresses in the vDSO images
  42. * and the VDSO_HIGH_BASE address where CONFIG_COMPAT_VDSO places the vDSO
  43. * in the user address space.
  44. */
  45. #define VDSO_ADDR_ADJUST (VDSO_HIGH_BASE - (unsigned long)VDSO32_PRELINK)
  46. /*
  47. * Should the kernel map a VDSO page into processes and pass its
  48. * address down to glibc upon exec()?
  49. */
  50. unsigned int __read_mostly vdso_enabled = VDSO_DEFAULT;
  51. static int __init vdso_setup(char *s)
  52. {
  53. vdso_enabled = simple_strtoul(s, NULL, 0);
  54. return 1;
  55. }
  56. /*
  57. * For consistency, the argument vdso32=[012] affects the 32-bit vDSO
  58. * behavior on both 64-bit and 32-bit kernels.
  59. * On 32-bit kernels, vdso=[012] means the same thing.
  60. */
  61. __setup("vdso32=", vdso_setup);
  62. #ifdef CONFIG_X86_32
  63. __setup_param("vdso=", vdso32_setup, vdso_setup, 0);
  64. EXPORT_SYMBOL_GPL(vdso_enabled);
  65. #endif
  66. static __init void reloc_symtab(Elf32_Ehdr *ehdr,
  67. unsigned offset, unsigned size)
  68. {
  69. Elf32_Sym *sym = (void *)ehdr + offset;
  70. unsigned nsym = size / sizeof(*sym);
  71. unsigned i;
  72. for(i = 0; i < nsym; i++, sym++) {
  73. if (sym->st_shndx == SHN_UNDEF ||
  74. sym->st_shndx == SHN_ABS)
  75. continue; /* skip */
  76. if (sym->st_shndx > SHN_LORESERVE) {
  77. printk(KERN_INFO "VDSO: unexpected st_shndx %x\n",
  78. sym->st_shndx);
  79. continue;
  80. }
  81. switch(ELF_ST_TYPE(sym->st_info)) {
  82. case STT_OBJECT:
  83. case STT_FUNC:
  84. case STT_SECTION:
  85. case STT_FILE:
  86. sym->st_value += VDSO_ADDR_ADJUST;
  87. }
  88. }
  89. }
  90. static __init void reloc_dyn(Elf32_Ehdr *ehdr, unsigned offset)
  91. {
  92. Elf32_Dyn *dyn = (void *)ehdr + offset;
  93. for(; dyn->d_tag != DT_NULL; dyn++)
  94. switch(dyn->d_tag) {
  95. case DT_PLTGOT:
  96. case DT_HASH:
  97. case DT_STRTAB:
  98. case DT_SYMTAB:
  99. case DT_RELA:
  100. case DT_INIT:
  101. case DT_FINI:
  102. case DT_REL:
  103. case DT_DEBUG:
  104. case DT_JMPREL:
  105. case DT_VERSYM:
  106. case DT_VERDEF:
  107. case DT_VERNEED:
  108. case DT_ADDRRNGLO ... DT_ADDRRNGHI:
  109. /* definitely pointers needing relocation */
  110. dyn->d_un.d_ptr += VDSO_ADDR_ADJUST;
  111. break;
  112. case DT_ENCODING ... OLD_DT_LOOS-1:
  113. case DT_LOOS ... DT_HIOS-1:
  114. /* Tags above DT_ENCODING are pointers if
  115. they're even */
  116. if (dyn->d_tag >= DT_ENCODING &&
  117. (dyn->d_tag & 1) == 0)
  118. dyn->d_un.d_ptr += VDSO_ADDR_ADJUST;
  119. break;
  120. case DT_VERDEFNUM:
  121. case DT_VERNEEDNUM:
  122. case DT_FLAGS_1:
  123. case DT_RELACOUNT:
  124. case DT_RELCOUNT:
  125. case DT_VALRNGLO ... DT_VALRNGHI:
  126. /* definitely not pointers */
  127. break;
  128. case OLD_DT_LOOS ... DT_LOOS-1:
  129. case DT_HIOS ... DT_VALRNGLO-1:
  130. default:
  131. if (dyn->d_tag > DT_ENCODING)
  132. printk(KERN_INFO "VDSO: unexpected DT_tag %x\n",
  133. dyn->d_tag);
  134. break;
  135. }
  136. }
  137. static __init void relocate_vdso(Elf32_Ehdr *ehdr)
  138. {
  139. Elf32_Phdr *phdr;
  140. Elf32_Shdr *shdr;
  141. int i;
  142. BUG_ON(memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0 ||
  143. !elf_check_arch_ia32(ehdr) ||
  144. ehdr->e_type != ET_DYN);
  145. ehdr->e_entry += VDSO_ADDR_ADJUST;
  146. /* rebase phdrs */
  147. phdr = (void *)ehdr + ehdr->e_phoff;
  148. for (i = 0; i < ehdr->e_phnum; i++) {
  149. phdr[i].p_vaddr += VDSO_ADDR_ADJUST;
  150. /* relocate dynamic stuff */
  151. if (phdr[i].p_type == PT_DYNAMIC)
  152. reloc_dyn(ehdr, phdr[i].p_offset);
  153. }
  154. /* rebase sections */
  155. shdr = (void *)ehdr + ehdr->e_shoff;
  156. for(i = 0; i < ehdr->e_shnum; i++) {
  157. if (!(shdr[i].sh_flags & SHF_ALLOC))
  158. continue;
  159. shdr[i].sh_addr += VDSO_ADDR_ADJUST;
  160. if (shdr[i].sh_type == SHT_SYMTAB ||
  161. shdr[i].sh_type == SHT_DYNSYM)
  162. reloc_symtab(ehdr, shdr[i].sh_offset,
  163. shdr[i].sh_size);
  164. }
  165. }
  166. static struct page *vdso32_pages[1];
  167. #ifdef CONFIG_X86_64
  168. #define vdso32_sysenter() (boot_cpu_has(X86_FEATURE_SYSENTER32))
  169. #define vdso32_syscall() (boot_cpu_has(X86_FEATURE_SYSCALL32))
  170. /* May not be __init: called during resume */
  171. void syscall32_cpu_init(void)
  172. {
  173. /* Load these always in case some future AMD CPU supports
  174. SYSENTER from compat mode too. */
  175. checking_wrmsrl(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
  176. checking_wrmsrl(MSR_IA32_SYSENTER_ESP, 0ULL);
  177. checking_wrmsrl(MSR_IA32_SYSENTER_EIP, (u64)ia32_sysenter_target);
  178. wrmsrl(MSR_CSTAR, ia32_cstar_target);
  179. }
  180. #define compat_uses_vma 1
  181. static inline void map_compat_vdso(int map)
  182. {
  183. }
  184. #else /* CONFIG_X86_32 */
  185. #define vdso32_sysenter() (boot_cpu_has(X86_FEATURE_SEP))
  186. #define vdso32_syscall() (0)
  187. void enable_sep_cpu(void)
  188. {
  189. int cpu = get_cpu();
  190. struct tss_struct *tss = &per_cpu(init_tss, cpu);
  191. if (!boot_cpu_has(X86_FEATURE_SEP)) {
  192. put_cpu();
  193. return;
  194. }
  195. tss->x86_tss.ss1 = __KERNEL_CS;
  196. tss->x86_tss.sp1 = sizeof(struct tss_struct) + (unsigned long) tss;
  197. wrmsr(MSR_IA32_SYSENTER_CS, __KERNEL_CS, 0);
  198. wrmsr(MSR_IA32_SYSENTER_ESP, tss->x86_tss.sp1, 0);
  199. wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long) ia32_sysenter_target, 0);
  200. put_cpu();
  201. }
  202. static struct vm_area_struct gate_vma;
  203. static int __init gate_vma_init(void)
  204. {
  205. gate_vma.vm_mm = NULL;
  206. gate_vma.vm_start = FIXADDR_USER_START;
  207. gate_vma.vm_end = FIXADDR_USER_END;
  208. gate_vma.vm_flags = VM_READ | VM_MAYREAD | VM_EXEC | VM_MAYEXEC;
  209. gate_vma.vm_page_prot = __P101;
  210. return 0;
  211. }
  212. #define compat_uses_vma 0
  213. static void map_compat_vdso(int map)
  214. {
  215. static int vdso_mapped;
  216. if (map == vdso_mapped)
  217. return;
  218. vdso_mapped = map;
  219. __set_fixmap(FIX_VDSO, page_to_pfn(vdso32_pages[0]) << PAGE_SHIFT,
  220. map ? PAGE_READONLY_EXEC : PAGE_NONE);
  221. /* flush stray tlbs */
  222. flush_tlb_all();
  223. }
  224. #endif /* CONFIG_X86_64 */
  225. int __init sysenter_setup(void)
  226. {
  227. void *syscall_page = (void *)get_zeroed_page(GFP_ATOMIC);
  228. const void *vsyscall;
  229. size_t vsyscall_len;
  230. vdso32_pages[0] = virt_to_page(syscall_page);
  231. #ifdef CONFIG_X86_32
  232. gate_vma_init();
  233. #endif
  234. if (vdso32_syscall()) {
  235. vsyscall = &vdso32_syscall_start;
  236. vsyscall_len = &vdso32_syscall_end - &vdso32_syscall_start;
  237. } else if (vdso32_sysenter()){
  238. vsyscall = &vdso32_sysenter_start;
  239. vsyscall_len = &vdso32_sysenter_end - &vdso32_sysenter_start;
  240. } else {
  241. vsyscall = &vdso32_int80_start;
  242. vsyscall_len = &vdso32_int80_end - &vdso32_int80_start;
  243. }
  244. memcpy(syscall_page, vsyscall, vsyscall_len);
  245. relocate_vdso(syscall_page);
  246. return 0;
  247. }
  248. /* Setup a VMA at program startup for the vsyscall page */
  249. int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  250. {
  251. struct mm_struct *mm = current->mm;
  252. unsigned long addr;
  253. int ret = 0;
  254. bool compat;
  255. #ifdef CONFIG_X86_X32_ABI
  256. if (test_thread_flag(TIF_X32))
  257. return x32_setup_additional_pages(bprm, uses_interp);
  258. #endif
  259. if (vdso_enabled == VDSO_DISABLED)
  260. return 0;
  261. down_write(&mm->mmap_sem);
  262. /* Test compat mode once here, in case someone
  263. changes it via sysctl */
  264. compat = (vdso_enabled == VDSO_COMPAT);
  265. map_compat_vdso(compat);
  266. if (compat)
  267. addr = VDSO_HIGH_BASE;
  268. else {
  269. addr = get_unmapped_area(NULL, 0, PAGE_SIZE, 0, 0);
  270. if (IS_ERR_VALUE(addr)) {
  271. ret = addr;
  272. goto up_fail;
  273. }
  274. }
  275. current->mm->context.vdso = (void *)addr;
  276. if (compat_uses_vma || !compat) {
  277. /*
  278. * MAYWRITE to allow gdb to COW and set breakpoints
  279. */
  280. ret = install_special_mapping(mm, addr, PAGE_SIZE,
  281. VM_READ|VM_EXEC|
  282. VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
  283. vdso32_pages);
  284. if (ret)
  285. goto up_fail;
  286. }
  287. current_thread_info()->sysenter_return =
  288. VDSO32_SYMBOL(addr, SYSENTER_RETURN);
  289. up_fail:
  290. if (ret)
  291. current->mm->context.vdso = NULL;
  292. up_write(&mm->mmap_sem);
  293. return ret;
  294. }
  295. #ifdef CONFIG_X86_64
  296. subsys_initcall(sysenter_setup);
  297. #ifdef CONFIG_SYSCTL
  298. /* Register vsyscall32 into the ABI table */
  299. #include <linux/sysctl.h>
  300. static ctl_table abi_table2[] = {
  301. {
  302. .procname = "vsyscall32",
  303. .data = &sysctl_vsyscall32,
  304. .maxlen = sizeof(int),
  305. .mode = 0644,
  306. .proc_handler = proc_dointvec
  307. },
  308. {}
  309. };
  310. static ctl_table abi_root_table2[] = {
  311. {
  312. .procname = "abi",
  313. .mode = 0555,
  314. .child = abi_table2
  315. },
  316. {}
  317. };
  318. static __init int ia32_binfmt_init(void)
  319. {
  320. register_sysctl_table(abi_root_table2);
  321. return 0;
  322. }
  323. __initcall(ia32_binfmt_init);
  324. #endif
  325. #else /* CONFIG_X86_32 */
  326. const char *arch_vma_name(struct vm_area_struct *vma)
  327. {
  328. if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
  329. return "[vdso]";
  330. return NULL;
  331. }
  332. struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
  333. {
  334. /*
  335. * Check to see if the corresponding task was created in compat vdso
  336. * mode.
  337. */
  338. if (mm && mm->context.vdso == (void *)VDSO_HIGH_BASE)
  339. return &gate_vma;
  340. return NULL;
  341. }
  342. int in_gate_area(struct mm_struct *mm, unsigned long addr)
  343. {
  344. const struct vm_area_struct *vma = get_gate_vma(mm);
  345. return vma && addr >= vma->vm_start && addr < vma->vm_end;
  346. }
  347. int in_gate_area_no_mm(unsigned long addr)
  348. {
  349. return 0;
  350. }
  351. #endif /* CONFIG_X86_64 */