vma.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Copyright 2007 Andi Kleen, SUSE Labs.
  3. * Subject to the GPL, v.2
  4. *
  5. * This contains most of the x86 vDSO kernel-side code.
  6. */
  7. #include <linux/mm.h>
  8. #include <linux/err.h>
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/init.h>
  12. #include <linux/random.h>
  13. #include <linux/elf.h>
  14. #include <linux/cpu.h>
  15. #include <asm/vgtod.h>
  16. #include <asm/proto.h>
  17. #include <asm/vdso.h>
  18. #include <asm/vvar.h>
  19. #include <asm/page.h>
  20. #include <asm/hpet.h>
  21. #include <asm/desc.h>
  22. #if defined(CONFIG_X86_64)
  23. unsigned int __read_mostly vdso64_enabled = 1;
  24. #endif
  25. void __init init_vdso_image(const struct vdso_image *image)
  26. {
  27. int i;
  28. int npages = (image->size) / PAGE_SIZE;
  29. BUG_ON(image->size % PAGE_SIZE != 0);
  30. for (i = 0; i < npages; i++)
  31. image->text_mapping.pages[i] =
  32. virt_to_page(image->data + i*PAGE_SIZE);
  33. apply_alternatives((struct alt_instr *)(image->data + image->alt),
  34. (struct alt_instr *)(image->data + image->alt +
  35. image->alt_len));
  36. }
  37. struct linux_binprm;
  38. /*
  39. * Put the vdso above the (randomized) stack with another randomized
  40. * offset. This way there is no hole in the middle of address space.
  41. * To save memory make sure it is still in the same PTE as the stack
  42. * top. This doesn't give that many random bits.
  43. *
  44. * Note that this algorithm is imperfect: the distribution of the vdso
  45. * start address within a PMD is biased toward the end.
  46. *
  47. * Only used for the 64-bit and x32 vdsos.
  48. */
  49. static unsigned long vdso_addr(unsigned long start, unsigned len)
  50. {
  51. #ifdef CONFIG_X86_32
  52. return 0;
  53. #else
  54. unsigned long addr, end;
  55. unsigned offset;
  56. /*
  57. * Round up the start address. It can start out unaligned as a result
  58. * of stack start randomization.
  59. */
  60. start = PAGE_ALIGN(start);
  61. /* Round the lowest possible end address up to a PMD boundary. */
  62. end = (start + len + PMD_SIZE - 1) & PMD_MASK;
  63. if (end >= TASK_SIZE_MAX)
  64. end = TASK_SIZE_MAX;
  65. end -= len;
  66. if (end > start) {
  67. offset = get_random_int() % (((end - start) >> PAGE_SHIFT) + 1);
  68. addr = start + (offset << PAGE_SHIFT);
  69. } else {
  70. addr = start;
  71. }
  72. /*
  73. * Forcibly align the final address in case we have a hardware
  74. * issue that requires alignment for performance reasons.
  75. */
  76. addr = align_vdso_addr(addr);
  77. return addr;
  78. #endif
  79. }
  80. static int map_vdso(const struct vdso_image *image, bool calculate_addr)
  81. {
  82. struct mm_struct *mm = current->mm;
  83. struct vm_area_struct *vma;
  84. unsigned long addr, text_start;
  85. int ret = 0;
  86. static struct page *no_pages[] = {NULL};
  87. static struct vm_special_mapping vvar_mapping = {
  88. .name = "[vvar]",
  89. .pages = no_pages,
  90. };
  91. if (calculate_addr) {
  92. addr = vdso_addr(current->mm->start_stack,
  93. image->size - image->sym_vvar_start);
  94. } else {
  95. addr = 0;
  96. }
  97. down_write(&mm->mmap_sem);
  98. addr = get_unmapped_area(NULL, addr,
  99. image->size - image->sym_vvar_start, 0, 0);
  100. if (IS_ERR_VALUE(addr)) {
  101. ret = addr;
  102. goto up_fail;
  103. }
  104. text_start = addr - image->sym_vvar_start;
  105. current->mm->context.vdso = (void __user *)text_start;
  106. /*
  107. * MAYWRITE to allow gdb to COW and set breakpoints
  108. */
  109. vma = _install_special_mapping(mm,
  110. text_start,
  111. image->size,
  112. VM_READ|VM_EXEC|
  113. VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
  114. &image->text_mapping);
  115. if (IS_ERR(vma)) {
  116. ret = PTR_ERR(vma);
  117. goto up_fail;
  118. }
  119. vma = _install_special_mapping(mm,
  120. addr,
  121. -image->sym_vvar_start,
  122. VM_READ|VM_MAYREAD,
  123. &vvar_mapping);
  124. if (IS_ERR(vma)) {
  125. ret = PTR_ERR(vma);
  126. goto up_fail;
  127. }
  128. if (image->sym_vvar_page)
  129. ret = remap_pfn_range(vma,
  130. text_start + image->sym_vvar_page,
  131. __pa_symbol(&__vvar_page) >> PAGE_SHIFT,
  132. PAGE_SIZE,
  133. PAGE_READONLY);
  134. if (ret)
  135. goto up_fail;
  136. #ifdef CONFIG_HPET_TIMER
  137. if (hpet_address && image->sym_hpet_page) {
  138. ret = io_remap_pfn_range(vma,
  139. text_start + image->sym_hpet_page,
  140. hpet_address >> PAGE_SHIFT,
  141. PAGE_SIZE,
  142. pgprot_noncached(PAGE_READONLY));
  143. if (ret)
  144. goto up_fail;
  145. }
  146. #endif
  147. up_fail:
  148. if (ret)
  149. current->mm->context.vdso = NULL;
  150. up_write(&mm->mmap_sem);
  151. return ret;
  152. }
  153. #if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
  154. static int load_vdso32(void)
  155. {
  156. int ret;
  157. if (vdso32_enabled != 1) /* Other values all mean "disabled" */
  158. return 0;
  159. ret = map_vdso(selected_vdso32, false);
  160. if (ret)
  161. return ret;
  162. if (selected_vdso32->sym_VDSO32_SYSENTER_RETURN)
  163. current_thread_info()->sysenter_return =
  164. current->mm->context.vdso +
  165. selected_vdso32->sym_VDSO32_SYSENTER_RETURN;
  166. return 0;
  167. }
  168. #endif
  169. #ifdef CONFIG_X86_64
  170. int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  171. {
  172. if (!vdso64_enabled)
  173. return 0;
  174. return map_vdso(&vdso_image_64, true);
  175. }
  176. #ifdef CONFIG_COMPAT
  177. int compat_arch_setup_additional_pages(struct linux_binprm *bprm,
  178. int uses_interp)
  179. {
  180. #ifdef CONFIG_X86_X32_ABI
  181. if (test_thread_flag(TIF_X32)) {
  182. if (!vdso64_enabled)
  183. return 0;
  184. return map_vdso(&vdso_image_x32, true);
  185. }
  186. #endif
  187. #ifdef CONFIG_IA32_EMULATION
  188. return load_vdso32();
  189. #else
  190. return 0;
  191. #endif
  192. }
  193. #endif
  194. #else
  195. int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  196. {
  197. return load_vdso32();
  198. }
  199. #endif
  200. #ifdef CONFIG_X86_64
  201. static __init int vdso_setup(char *s)
  202. {
  203. vdso64_enabled = simple_strtoul(s, NULL, 0);
  204. return 0;
  205. }
  206. __setup("vdso=", vdso_setup);
  207. #endif
  208. #ifdef CONFIG_X86_64
  209. static void vgetcpu_cpu_init(void *arg)
  210. {
  211. int cpu = smp_processor_id();
  212. struct desc_struct d = { };
  213. unsigned long node = 0;
  214. #ifdef CONFIG_NUMA
  215. node = cpu_to_node(cpu);
  216. #endif
  217. if (cpu_has(&cpu_data(cpu), X86_FEATURE_RDTSCP))
  218. write_rdtscp_aux((node << 12) | cpu);
  219. /*
  220. * Store cpu number in limit so that it can be loaded
  221. * quickly in user space in vgetcpu. (12 bits for the CPU
  222. * and 8 bits for the node)
  223. */
  224. d.limit0 = cpu | ((node & 0xf) << 12);
  225. d.limit = node >> 4;
  226. d.type = 5; /* RO data, expand down, accessed */
  227. d.dpl = 3; /* Visible to user code */
  228. d.s = 1; /* Not a system segment */
  229. d.p = 1; /* Present */
  230. d.d = 1; /* 32-bit */
  231. write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_PER_CPU, &d, DESCTYPE_S);
  232. }
  233. static int
  234. vgetcpu_cpu_notifier(struct notifier_block *n, unsigned long action, void *arg)
  235. {
  236. long cpu = (long)arg;
  237. if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN)
  238. smp_call_function_single(cpu, vgetcpu_cpu_init, NULL, 1);
  239. return NOTIFY_DONE;
  240. }
  241. static int __init init_vdso(void)
  242. {
  243. init_vdso_image(&vdso_image_64);
  244. #ifdef CONFIG_X86_X32_ABI
  245. init_vdso_image(&vdso_image_x32);
  246. #endif
  247. cpu_notifier_register_begin();
  248. on_each_cpu(vgetcpu_cpu_init, NULL, 1);
  249. /* notifier priority > KVM */
  250. __hotcpu_notifier(vgetcpu_cpu_notifier, 30);
  251. cpu_notifier_register_done();
  252. return 0;
  253. }
  254. subsys_initcall(init_vdso);
  255. #endif /* CONFIG_X86_64 */