vdso32-setup.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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, 4) != 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. printk("Compat vDSO mapped to %08lx.\n", __fix_to_virt(FIX_VDSO));
  252. #endif
  253. if (!vdso32_sysenter()) {
  254. vsyscall = &vdso32_default_start;
  255. vsyscall_len = &vdso32_default_end - &vdso32_default_start;
  256. } else {
  257. vsyscall = &vdso32_sysenter_start;
  258. vsyscall_len = &vdso32_sysenter_end - &vdso32_sysenter_start;
  259. }
  260. memcpy(syscall_page, vsyscall, vsyscall_len);
  261. relocate_vdso(syscall_page);
  262. return 0;
  263. }
  264. /* Setup a VMA at program startup for the vsyscall page */
  265. int arch_setup_additional_pages(struct linux_binprm *bprm, int exstack)
  266. {
  267. struct mm_struct *mm = current->mm;
  268. unsigned long addr;
  269. int ret = 0;
  270. bool compat;
  271. if (vdso_enabled == VDSO_DISABLED)
  272. return 0;
  273. down_write(&mm->mmap_sem);
  274. /* Test compat mode once here, in case someone
  275. changes it via sysctl */
  276. compat = (vdso_enabled == VDSO_COMPAT);
  277. map_compat_vdso(compat);
  278. if (compat)
  279. addr = VDSO_HIGH_BASE;
  280. else {
  281. addr = get_unmapped_area(NULL, 0, PAGE_SIZE, 0, 0);
  282. if (IS_ERR_VALUE(addr)) {
  283. ret = addr;
  284. goto up_fail;
  285. }
  286. }
  287. if (compat_uses_vma || !compat) {
  288. /*
  289. * MAYWRITE to allow gdb to COW and set breakpoints
  290. *
  291. * Make sure the vDSO gets into every core dump.
  292. * Dumping its contents makes post-mortem fully
  293. * interpretable later without matching up the same
  294. * kernel and hardware config to see what PC values
  295. * meant.
  296. */
  297. ret = install_special_mapping(mm, addr, PAGE_SIZE,
  298. VM_READ|VM_EXEC|
  299. VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC|
  300. VM_ALWAYSDUMP,
  301. vdso32_pages);
  302. if (ret)
  303. goto up_fail;
  304. }
  305. current->mm->context.vdso = (void *)addr;
  306. current_thread_info()->sysenter_return =
  307. VDSO32_SYMBOL(addr, SYSENTER_RETURN);
  308. up_fail:
  309. up_write(&mm->mmap_sem);
  310. return ret;
  311. }
  312. #ifdef CONFIG_X86_64
  313. __initcall(sysenter_setup);
  314. #ifdef CONFIG_SYSCTL
  315. /* Register vsyscall32 into the ABI table */
  316. #include <linux/sysctl.h>
  317. static ctl_table abi_table2[] = {
  318. {
  319. .procname = "vsyscall32",
  320. .data = &sysctl_vsyscall32,
  321. .maxlen = sizeof(int),
  322. .mode = 0644,
  323. .proc_handler = proc_dointvec
  324. },
  325. {}
  326. };
  327. static ctl_table abi_root_table2[] = {
  328. {
  329. .ctl_name = CTL_ABI,
  330. .procname = "abi",
  331. .mode = 0555,
  332. .child = abi_table2
  333. },
  334. {}
  335. };
  336. static __init int ia32_binfmt_init(void)
  337. {
  338. register_sysctl_table(abi_root_table2);
  339. return 0;
  340. }
  341. __initcall(ia32_binfmt_init);
  342. #endif
  343. #else /* CONFIG_X86_32 */
  344. const char *arch_vma_name(struct vm_area_struct *vma)
  345. {
  346. if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
  347. return "[vdso]";
  348. return NULL;
  349. }
  350. struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
  351. {
  352. struct mm_struct *mm = tsk->mm;
  353. /* Check to see if this task was created in compat vdso mode */
  354. if (mm && mm->context.vdso == (void *)VDSO_HIGH_BASE)
  355. return &gate_vma;
  356. return NULL;
  357. }
  358. int in_gate_area(struct task_struct *task, unsigned long addr)
  359. {
  360. const struct vm_area_struct *vma = get_gate_vma(task);
  361. return vma && addr >= vma->vm_start && addr < vma->vm_end;
  362. }
  363. int in_gate_area_no_task(unsigned long addr)
  364. {
  365. return 0;
  366. }
  367. #endif /* CONFIG_X86_64 */