vma.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Set up the VMAs to tell the VM about the vDSO.
  3. * Copyright 2007 Andi Kleen, SUSE Labs.
  4. * Subject to the GPL, v.2
  5. */
  6. #include <linux/mm.h>
  7. #include <linux/err.h>
  8. #include <linux/sched.h>
  9. #include <linux/slab.h>
  10. #include <linux/init.h>
  11. #include <linux/random.h>
  12. #include <linux/elf.h>
  13. #include <asm/vsyscall.h>
  14. #include <asm/vgtod.h>
  15. #include <asm/proto.h>
  16. #include <asm/vdso.h>
  17. #include <asm/page.h>
  18. #include <asm/hpet.h>
  19. #if defined(CONFIG_X86_64)
  20. unsigned int __read_mostly vdso64_enabled = 1;
  21. extern unsigned short vdso_sync_cpuid;
  22. #endif
  23. void __init init_vdso_image(const struct vdso_image *image)
  24. {
  25. int i;
  26. int npages = (image->size) / PAGE_SIZE;
  27. BUG_ON(image->size % PAGE_SIZE != 0);
  28. for (i = 0; i < npages; i++)
  29. image->text_mapping.pages[i] =
  30. virt_to_page(image->data + i*PAGE_SIZE);
  31. apply_alternatives((struct alt_instr *)(image->data + image->alt),
  32. (struct alt_instr *)(image->data + image->alt +
  33. image->alt_len));
  34. }
  35. #if defined(CONFIG_X86_64)
  36. static int __init init_vdso(void)
  37. {
  38. init_vdso_image(&vdso_image_64);
  39. #ifdef CONFIG_X86_X32_ABI
  40. init_vdso_image(&vdso_image_x32);
  41. #endif
  42. return 0;
  43. }
  44. subsys_initcall(init_vdso);
  45. #endif
  46. struct linux_binprm;
  47. /* Put the vdso above the (randomized) stack with another randomized offset.
  48. This way there is no hole in the middle of address space.
  49. To save memory make sure it is still in the same PTE as the stack top.
  50. This doesn't give that many random bits.
  51. Only used for the 64-bit and x32 vdsos. */
  52. static unsigned long vdso_addr(unsigned long start, unsigned len)
  53. {
  54. #ifdef CONFIG_X86_32
  55. return 0;
  56. #else
  57. unsigned long addr, end;
  58. unsigned offset;
  59. end = (start + PMD_SIZE - 1) & PMD_MASK;
  60. if (end >= TASK_SIZE_MAX)
  61. end = TASK_SIZE_MAX;
  62. end -= len;
  63. /* This loses some more bits than a modulo, but is cheaper */
  64. offset = get_random_int() & (PTRS_PER_PTE - 1);
  65. addr = start + (offset << PAGE_SHIFT);
  66. if (addr >= end)
  67. addr = end;
  68. /*
  69. * page-align it here so that get_unmapped_area doesn't
  70. * align it wrongfully again to the next page. addr can come in 4K
  71. * unaligned here as a result of stack start randomization.
  72. */
  73. addr = PAGE_ALIGN(addr);
  74. addr = align_vdso_addr(addr);
  75. return addr;
  76. #endif
  77. }
  78. static int map_vdso(const struct vdso_image *image, bool calculate_addr)
  79. {
  80. struct mm_struct *mm = current->mm;
  81. struct vm_area_struct *vma;
  82. unsigned long addr, text_start;
  83. int ret = 0;
  84. static struct page *no_pages[] = {NULL};
  85. static struct vm_special_mapping vvar_mapping = {
  86. .name = "[vvar]",
  87. .pages = no_pages,
  88. };
  89. if (calculate_addr) {
  90. addr = vdso_addr(current->mm->start_stack,
  91. image->size - image->sym_vvar_start);
  92. } else {
  93. addr = 0;
  94. }
  95. down_write(&mm->mmap_sem);
  96. addr = get_unmapped_area(NULL, addr,
  97. image->size - image->sym_vvar_start, 0, 0);
  98. if (IS_ERR_VALUE(addr)) {
  99. ret = addr;
  100. goto up_fail;
  101. }
  102. text_start = addr - image->sym_vvar_start;
  103. current->mm->context.vdso = (void __user *)text_start;
  104. /*
  105. * MAYWRITE to allow gdb to COW and set breakpoints
  106. */
  107. vma = _install_special_mapping(mm,
  108. text_start,
  109. image->size,
  110. VM_READ|VM_EXEC|
  111. VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
  112. &image->text_mapping);
  113. if (IS_ERR(vma)) {
  114. ret = PTR_ERR(vma);
  115. goto up_fail;
  116. }
  117. vma = _install_special_mapping(mm,
  118. addr,
  119. -image->sym_vvar_start,
  120. VM_READ|VM_MAYREAD,
  121. &vvar_mapping);
  122. if (IS_ERR(vma)) {
  123. ret = PTR_ERR(vma);
  124. goto up_fail;
  125. }
  126. if (image->sym_vvar_page)
  127. ret = remap_pfn_range(vma,
  128. text_start + image->sym_vvar_page,
  129. __pa_symbol(&__vvar_page) >> PAGE_SHIFT,
  130. PAGE_SIZE,
  131. PAGE_READONLY);
  132. if (ret)
  133. goto up_fail;
  134. #ifdef CONFIG_HPET_TIMER
  135. if (hpet_address && image->sym_hpet_page) {
  136. ret = io_remap_pfn_range(vma,
  137. text_start + image->sym_hpet_page,
  138. hpet_address >> PAGE_SHIFT,
  139. PAGE_SIZE,
  140. pgprot_noncached(PAGE_READONLY));
  141. if (ret)
  142. goto up_fail;
  143. }
  144. #endif
  145. up_fail:
  146. if (ret)
  147. current->mm->context.vdso = NULL;
  148. up_write(&mm->mmap_sem);
  149. return ret;
  150. }
  151. #if defined(CONFIG_X86_32) || defined(CONFIG_COMPAT)
  152. static int load_vdso32(void)
  153. {
  154. int ret;
  155. if (vdso32_enabled != 1) /* Other values all mean "disabled" */
  156. return 0;
  157. ret = map_vdso(selected_vdso32, false);
  158. if (ret)
  159. return ret;
  160. if (selected_vdso32->sym_VDSO32_SYSENTER_RETURN)
  161. current_thread_info()->sysenter_return =
  162. current->mm->context.vdso +
  163. selected_vdso32->sym_VDSO32_SYSENTER_RETURN;
  164. return 0;
  165. }
  166. #endif
  167. #ifdef CONFIG_X86_64
  168. int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  169. {
  170. if (!vdso64_enabled)
  171. return 0;
  172. return map_vdso(&vdso_image_64, true);
  173. }
  174. #ifdef CONFIG_COMPAT
  175. int compat_arch_setup_additional_pages(struct linux_binprm *bprm,
  176. int uses_interp)
  177. {
  178. #ifdef CONFIG_X86_X32_ABI
  179. if (test_thread_flag(TIF_X32)) {
  180. if (!vdso64_enabled)
  181. return 0;
  182. return map_vdso(&vdso_image_x32, true);
  183. }
  184. #endif
  185. return load_vdso32();
  186. }
  187. #endif
  188. #else
  189. int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  190. {
  191. return load_vdso32();
  192. }
  193. #endif
  194. #ifdef CONFIG_X86_64
  195. static __init int vdso_setup(char *s)
  196. {
  197. vdso64_enabled = simple_strtoul(s, NULL, 0);
  198. return 0;
  199. }
  200. __setup("vdso=", vdso_setup);
  201. #endif