vdso32-setup.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. /*
  211. * Make sure the vDSO gets into every core dump.
  212. * Dumping its contents makes post-mortem fully interpretable later
  213. * without matching up the same kernel and hardware config to see
  214. * what PC values meant.
  215. */
  216. gate_vma.vm_flags |= VM_ALWAYSDUMP;
  217. return 0;
  218. }
  219. #define compat_uses_vma 0
  220. static void map_compat_vdso(int map)
  221. {
  222. static int vdso_mapped;
  223. if (map == vdso_mapped)
  224. return;
  225. vdso_mapped = map;
  226. __set_fixmap(FIX_VDSO, page_to_pfn(vdso32_pages[0]) << PAGE_SHIFT,
  227. map ? PAGE_READONLY_EXEC : PAGE_NONE);
  228. /* flush stray tlbs */
  229. flush_tlb_all();
  230. }
  231. #endif /* CONFIG_X86_64 */
  232. int __init sysenter_setup(void)
  233. {
  234. void *syscall_page = (void *)get_zeroed_page(GFP_ATOMIC);
  235. const void *vsyscall;
  236. size_t vsyscall_len;
  237. vdso32_pages[0] = virt_to_page(syscall_page);
  238. #ifdef CONFIG_X86_32
  239. gate_vma_init();
  240. #endif
  241. if (vdso32_syscall()) {
  242. vsyscall = &vdso32_syscall_start;
  243. vsyscall_len = &vdso32_syscall_end - &vdso32_syscall_start;
  244. } else if (vdso32_sysenter()){
  245. vsyscall = &vdso32_sysenter_start;
  246. vsyscall_len = &vdso32_sysenter_end - &vdso32_sysenter_start;
  247. } else {
  248. vsyscall = &vdso32_int80_start;
  249. vsyscall_len = &vdso32_int80_end - &vdso32_int80_start;
  250. }
  251. memcpy(syscall_page, vsyscall, vsyscall_len);
  252. relocate_vdso(syscall_page);
  253. return 0;
  254. }
  255. /* Setup a VMA at program startup for the vsyscall page */
  256. int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  257. {
  258. struct mm_struct *mm = current->mm;
  259. unsigned long addr;
  260. int ret = 0;
  261. bool compat;
  262. if (vdso_enabled == VDSO_DISABLED)
  263. return 0;
  264. down_write(&mm->mmap_sem);
  265. /* Test compat mode once here, in case someone
  266. changes it via sysctl */
  267. compat = (vdso_enabled == VDSO_COMPAT);
  268. map_compat_vdso(compat);
  269. if (compat)
  270. addr = VDSO_HIGH_BASE;
  271. else {
  272. addr = get_unmapped_area(NULL, 0, PAGE_SIZE, 0, 0);
  273. if (IS_ERR_VALUE(addr)) {
  274. ret = addr;
  275. goto up_fail;
  276. }
  277. }
  278. current->mm->context.vdso = (void *)addr;
  279. if (compat_uses_vma || !compat) {
  280. /*
  281. * MAYWRITE to allow gdb to COW and set breakpoints
  282. *
  283. * Make sure the vDSO gets into every core dump.
  284. * Dumping its contents makes post-mortem fully
  285. * interpretable later without matching up the same
  286. * kernel and hardware config to see what PC values
  287. * meant.
  288. */
  289. ret = install_special_mapping(mm, addr, PAGE_SIZE,
  290. VM_READ|VM_EXEC|
  291. VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC|
  292. VM_ALWAYSDUMP,
  293. vdso32_pages);
  294. if (ret)
  295. goto up_fail;
  296. }
  297. current_thread_info()->sysenter_return =
  298. VDSO32_SYMBOL(addr, SYSENTER_RETURN);
  299. up_fail:
  300. if (ret)
  301. current->mm->context.vdso = NULL;
  302. up_write(&mm->mmap_sem);
  303. return ret;
  304. }
  305. #ifdef CONFIG_X86_64
  306. subsys_initcall(sysenter_setup);
  307. #ifdef CONFIG_SYSCTL
  308. /* Register vsyscall32 into the ABI table */
  309. #include <linux/sysctl.h>
  310. static ctl_table abi_table2[] = {
  311. {
  312. .procname = "vsyscall32",
  313. .data = &sysctl_vsyscall32,
  314. .maxlen = sizeof(int),
  315. .mode = 0644,
  316. .proc_handler = proc_dointvec
  317. },
  318. {}
  319. };
  320. static ctl_table abi_root_table2[] = {
  321. {
  322. .procname = "abi",
  323. .mode = 0555,
  324. .child = abi_table2
  325. },
  326. {}
  327. };
  328. static __init int ia32_binfmt_init(void)
  329. {
  330. register_sysctl_table(abi_root_table2);
  331. return 0;
  332. }
  333. __initcall(ia32_binfmt_init);
  334. #endif
  335. #else /* CONFIG_X86_32 */
  336. const char *arch_vma_name(struct vm_area_struct *vma)
  337. {
  338. if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
  339. return "[vdso]";
  340. return NULL;
  341. }
  342. struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
  343. {
  344. /*
  345. * Check to see if the corresponding task was created in compat vdso
  346. * mode.
  347. */
  348. if (mm && mm->context.vdso == (void *)VDSO_HIGH_BASE)
  349. return &gate_vma;
  350. return NULL;
  351. }
  352. int in_gate_area(struct mm_struct *mm, unsigned long addr)
  353. {
  354. const struct vm_area_struct *vma = get_gate_vma(mm);
  355. return vma && addr >= vma->vm_start && addr < vma->vm_end;
  356. }
  357. int in_gate_area_no_mm(unsigned long addr)
  358. {
  359. return 0;
  360. }
  361. #endif /* CONFIG_X86_64 */