vma.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. /*
  48. * Put the vdso above the (randomized) stack with another randomized
  49. * offset. This way there is no hole in the middle of address space.
  50. * To save memory make sure it is still in the same PTE as the stack
  51. * top. This doesn't give that many random bits.
  52. *
  53. * Note that this algorithm is imperfect: the distribution of the vdso
  54. * start address within a PMD is biased toward the end.
  55. *
  56. * Only used for the 64-bit and x32 vdsos.
  57. */
  58. static unsigned long vdso_addr(unsigned long start, unsigned len)
  59. {
  60. #ifdef CONFIG_X86_32
  61. return 0;
  62. #else
  63. unsigned long addr, end;
  64. unsigned offset;
  65. /*
  66. * Round up the start address. It can start out unaligned as a result
  67. * of stack start randomization.
  68. */
  69. start = PAGE_ALIGN(start);
  70. /* Round the lowest possible end address up to a PMD boundary. */
  71. end = (start + len + PMD_SIZE - 1) & PMD_MASK;
  72. if (end >= TASK_SIZE_MAX)
  73. end = TASK_SIZE_MAX;
  74. end -= len;
  75. if (end > start) {
  76. offset = get_random_int() % (((end - start) >> PAGE_SHIFT) + 1);
  77. addr = start + (offset << PAGE_SHIFT);
  78. } else {
  79. addr = start;
  80. }
  81. /*
  82. * Forcibly align the final address in case we have a hardware
  83. * issue that requires alignment for performance reasons.
  84. */
  85. addr = align_vdso_addr(addr);
  86. return addr;
  87. #endif
  88. }
  89. static int map_vdso(const struct vdso_image *image, bool calculate_addr)
  90. {
  91. struct mm_struct *mm = current->mm;
  92. struct vm_area_struct *vma;
  93. unsigned long addr, text_start;
  94. int ret = 0;
  95. static struct page *no_pages[] = {NULL};
  96. static struct vm_special_mapping vvar_mapping = {
  97. .name = "[vvar]",
  98. .pages = no_pages,
  99. };
  100. if (calculate_addr) {
  101. addr = vdso_addr(current->mm->start_stack,
  102. image->size - image->sym_vvar_start);
  103. } else {
  104. addr = 0;
  105. }
  106. down_write(&mm->mmap_sem);
  107. addr = get_unmapped_area(NULL, addr,
  108. image->size - image->sym_vvar_start, 0, 0);
  109. if (IS_ERR_VALUE(addr)) {
  110. ret = addr;
  111. goto up_fail;
  112. }
  113. text_start = addr - image->sym_vvar_start;
  114. current->mm->context.vdso = (void __user *)text_start;
  115. /*
  116. * MAYWRITE to allow gdb to COW and set breakpoints
  117. */
  118. vma = _install_special_mapping(mm,
  119. text_start,
  120. image->size,
  121. VM_READ|VM_EXEC|
  122. VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
  123. &image->text_mapping);
  124. if (IS_ERR(vma)) {
  125. ret = PTR_ERR(vma);
  126. goto up_fail;
  127. }
  128. vma = _install_special_mapping(mm,
  129. addr,
  130. -image->sym_vvar_start,
  131. VM_READ|VM_MAYREAD,
  132. &vvar_mapping);
  133. if (IS_ERR(vma)) {
  134. ret = PTR_ERR(vma);
  135. goto up_fail;
  136. }
  137. if (image->sym_vvar_page)
  138. ret = remap_pfn_range(vma,
  139. text_start + image->sym_vvar_page,
  140. __pa_symbol(&__vvar_page) >> PAGE_SHIFT,
  141. PAGE_SIZE,
  142. PAGE_READONLY);
  143. if (ret)
  144. goto up_fail;
  145. #ifdef CONFIG_HPET_TIMER
  146. if (hpet_address && image->sym_hpet_page) {
  147. ret = io_remap_pfn_range(vma,
  148. text_start + image->sym_hpet_page,
  149. hpet_address >> PAGE_SHIFT,
  150. PAGE_SIZE,
  151. pgprot_noncached(PAGE_READONLY));
  152. if (ret)
  153. goto up_fail;
  154. }
  155. #endif
  156. up_fail:
  157. if (ret)
  158. current->mm->context.vdso = NULL;
  159. up_write(&mm->mmap_sem);
  160. return ret;
  161. }
  162. #if defined(CONFIG_X86_32) || defined(CONFIG_COMPAT)
  163. static int load_vdso32(void)
  164. {
  165. int ret;
  166. if (vdso32_enabled != 1) /* Other values all mean "disabled" */
  167. return 0;
  168. ret = map_vdso(selected_vdso32, false);
  169. if (ret)
  170. return ret;
  171. if (selected_vdso32->sym_VDSO32_SYSENTER_RETURN)
  172. current_thread_info()->sysenter_return =
  173. current->mm->context.vdso +
  174. selected_vdso32->sym_VDSO32_SYSENTER_RETURN;
  175. return 0;
  176. }
  177. #endif
  178. #ifdef CONFIG_X86_64
  179. int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  180. {
  181. if (!vdso64_enabled)
  182. return 0;
  183. return map_vdso(&vdso_image_64, true);
  184. }
  185. #ifdef CONFIG_COMPAT
  186. int compat_arch_setup_additional_pages(struct linux_binprm *bprm,
  187. int uses_interp)
  188. {
  189. #ifdef CONFIG_X86_X32_ABI
  190. if (test_thread_flag(TIF_X32)) {
  191. if (!vdso64_enabled)
  192. return 0;
  193. return map_vdso(&vdso_image_x32, true);
  194. }
  195. #endif
  196. return load_vdso32();
  197. }
  198. #endif
  199. #else
  200. int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  201. {
  202. return load_vdso32();
  203. }
  204. #endif
  205. #ifdef CONFIG_X86_64
  206. static __init int vdso_setup(char *s)
  207. {
  208. vdso64_enabled = simple_strtoul(s, NULL, 0);
  209. return 0;
  210. }
  211. __setup("vdso=", vdso_setup);
  212. #endif