vdso32-setup.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. /*
  167. * These symbols are defined by vdso32.S to mark the bounds
  168. * of the ELF DSO images included therein.
  169. */
  170. extern const char vdso32_default_start, vdso32_default_end;
  171. extern const char vdso32_sysenter_start, vdso32_sysenter_end;
  172. static struct page *vdso32_pages[1];
  173. #ifdef CONFIG_X86_64
  174. static int use_sysenter __read_mostly = -1;
  175. #define vdso32_sysenter() (use_sysenter > 0)
  176. /* May not be __init: called during resume */
  177. void syscall32_cpu_init(void)
  178. {
  179. if (use_sysenter < 0) {
  180. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
  181. use_sysenter = 1;
  182. if (boot_cpu_data.x86_vendor == X86_VENDOR_CENTAUR)
  183. use_sysenter = 1;
  184. }
  185. /* Load these always in case some future AMD CPU supports
  186. SYSENTER from compat mode too. */
  187. checking_wrmsrl(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
  188. checking_wrmsrl(MSR_IA32_SYSENTER_ESP, 0ULL);
  189. checking_wrmsrl(MSR_IA32_SYSENTER_EIP, (u64)ia32_sysenter_target);
  190. wrmsrl(MSR_CSTAR, ia32_cstar_target);
  191. }
  192. #define compat_uses_vma 1
  193. static inline void map_compat_vdso(int map)
  194. {
  195. }
  196. #else /* CONFIG_X86_32 */
  197. #define vdso32_sysenter() (boot_cpu_has(X86_FEATURE_SEP))
  198. void enable_sep_cpu(void)
  199. {
  200. int cpu = get_cpu();
  201. struct tss_struct *tss = &per_cpu(init_tss, cpu);
  202. if (!boot_cpu_has(X86_FEATURE_SEP)) {
  203. put_cpu();
  204. return;
  205. }
  206. tss->x86_tss.ss1 = __KERNEL_CS;
  207. tss->x86_tss.sp1 = sizeof(struct tss_struct) + (unsigned long) tss;
  208. wrmsr(MSR_IA32_SYSENTER_CS, __KERNEL_CS, 0);
  209. wrmsr(MSR_IA32_SYSENTER_ESP, tss->x86_tss.sp1, 0);
  210. wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long) ia32_sysenter_target, 0);
  211. put_cpu();
  212. }
  213. static struct vm_area_struct gate_vma;
  214. static int __init gate_vma_init(void)
  215. {
  216. gate_vma.vm_mm = NULL;
  217. gate_vma.vm_start = FIXADDR_USER_START;
  218. gate_vma.vm_end = FIXADDR_USER_END;
  219. gate_vma.vm_flags = VM_READ | VM_MAYREAD | VM_EXEC | VM_MAYEXEC;
  220. gate_vma.vm_page_prot = __P101;
  221. /*
  222. * Make sure the vDSO gets into every core dump.
  223. * Dumping its contents makes post-mortem fully interpretable later
  224. * without matching up the same kernel and hardware config to see
  225. * what PC values meant.
  226. */
  227. gate_vma.vm_flags |= VM_ALWAYSDUMP;
  228. return 0;
  229. }
  230. #define compat_uses_vma 0
  231. static void map_compat_vdso(int map)
  232. {
  233. static int vdso_mapped;
  234. if (map == vdso_mapped)
  235. return;
  236. vdso_mapped = map;
  237. __set_fixmap(FIX_VDSO, page_to_pfn(vdso32_pages[0]) << PAGE_SHIFT,
  238. map ? PAGE_READONLY_EXEC : PAGE_NONE);
  239. /* flush stray tlbs */
  240. flush_tlb_all();
  241. }
  242. #endif /* CONFIG_X86_64 */
  243. int __init sysenter_setup(void)
  244. {
  245. void *syscall_page = (void *)get_zeroed_page(GFP_ATOMIC);
  246. const void *vsyscall;
  247. size_t vsyscall_len;
  248. vdso32_pages[0] = virt_to_page(syscall_page);
  249. #ifdef CONFIG_X86_32
  250. gate_vma_init();
  251. #endif
  252. if (!vdso32_sysenter()) {
  253. vsyscall = &vdso32_default_start;
  254. vsyscall_len = &vdso32_default_end - &vdso32_default_start;
  255. } else {
  256. vsyscall = &vdso32_sysenter_start;
  257. vsyscall_len = &vdso32_sysenter_end - &vdso32_sysenter_start;
  258. }
  259. memcpy(syscall_page, vsyscall, vsyscall_len);
  260. relocate_vdso(syscall_page);
  261. return 0;
  262. }
  263. /* Setup a VMA at program startup for the vsyscall page */
  264. int arch_setup_additional_pages(struct linux_binprm *bprm, int exstack)
  265. {
  266. struct mm_struct *mm = current->mm;
  267. unsigned long addr;
  268. int ret = 0;
  269. bool compat;
  270. if (vdso_enabled == VDSO_DISABLED)
  271. return 0;
  272. down_write(&mm->mmap_sem);
  273. /* Test compat mode once here, in case someone
  274. changes it via sysctl */
  275. compat = (vdso_enabled == VDSO_COMPAT);
  276. map_compat_vdso(compat);
  277. if (compat)
  278. addr = VDSO_HIGH_BASE;
  279. else {
  280. addr = get_unmapped_area(NULL, 0, PAGE_SIZE, 0, 0);
  281. if (IS_ERR_VALUE(addr)) {
  282. ret = addr;
  283. goto up_fail;
  284. }
  285. }
  286. if (compat_uses_vma || !compat) {
  287. /*
  288. * MAYWRITE to allow gdb to COW and set breakpoints
  289. *
  290. * Make sure the vDSO gets into every core dump.
  291. * Dumping its contents makes post-mortem fully
  292. * interpretable later without matching up the same
  293. * kernel and hardware config to see what PC values
  294. * meant.
  295. */
  296. ret = install_special_mapping(mm, addr, PAGE_SIZE,
  297. VM_READ|VM_EXEC|
  298. VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC|
  299. VM_ALWAYSDUMP,
  300. vdso32_pages);
  301. if (ret)
  302. goto up_fail;
  303. }
  304. current->mm->context.vdso = (void *)addr;
  305. current_thread_info()->sysenter_return =
  306. VDSO32_SYMBOL(addr, SYSENTER_RETURN);
  307. up_fail:
  308. up_write(&mm->mmap_sem);
  309. return ret;
  310. }
  311. #ifdef CONFIG_X86_64
  312. __initcall(sysenter_setup);
  313. #ifdef CONFIG_SYSCTL
  314. /* Register vsyscall32 into the ABI table */
  315. #include <linux/sysctl.h>
  316. static ctl_table abi_table2[] = {
  317. {
  318. .procname = "vsyscall32",
  319. .data = &sysctl_vsyscall32,
  320. .maxlen = sizeof(int),
  321. .mode = 0644,
  322. .proc_handler = proc_dointvec
  323. },
  324. {}
  325. };
  326. static ctl_table abi_root_table2[] = {
  327. {
  328. .ctl_name = CTL_ABI,
  329. .procname = "abi",
  330. .mode = 0555,
  331. .child = abi_table2
  332. },
  333. {}
  334. };
  335. static __init int ia32_binfmt_init(void)
  336. {
  337. register_sysctl_table(abi_root_table2);
  338. return 0;
  339. }
  340. __initcall(ia32_binfmt_init);
  341. #endif
  342. #else /* CONFIG_X86_32 */
  343. const char *arch_vma_name(struct vm_area_struct *vma)
  344. {
  345. if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
  346. return "[vdso]";
  347. return NULL;
  348. }
  349. struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
  350. {
  351. struct mm_struct *mm = tsk->mm;
  352. /* Check to see if this task was created in compat vdso mode */
  353. if (mm && mm->context.vdso == (void *)VDSO_HIGH_BASE)
  354. return &gate_vma;
  355. return NULL;
  356. }
  357. int in_gate_area(struct task_struct *task, unsigned long addr)
  358. {
  359. const struct vm_area_struct *vma = get_gate_vma(task);
  360. return vma && addr >= vma->vm_start && addr < vma->vm_end;
  361. }
  362. int in_gate_area_no_task(unsigned long addr)
  363. {
  364. return 0;
  365. }
  366. #endif /* CONFIG_X86_64 */