vma.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. /* Put the vdso above the (randomized) stack with another randomized offset.
  39. This way there is no hole in the middle of address space.
  40. To save memory make sure it is still in the same PTE as the stack top.
  41. This doesn't give that many random bits.
  42. Only used for the 64-bit and x32 vdsos. */
  43. static unsigned long vdso_addr(unsigned long start, unsigned len)
  44. {
  45. #ifdef CONFIG_X86_32
  46. return 0;
  47. #else
  48. unsigned long addr, end;
  49. unsigned offset;
  50. end = (start + PMD_SIZE - 1) & PMD_MASK;
  51. if (end >= TASK_SIZE_MAX)
  52. end = TASK_SIZE_MAX;
  53. end -= len;
  54. /* This loses some more bits than a modulo, but is cheaper */
  55. offset = get_random_int() & (PTRS_PER_PTE - 1);
  56. addr = start + (offset << PAGE_SHIFT);
  57. if (addr >= end)
  58. addr = end;
  59. /*
  60. * page-align it here so that get_unmapped_area doesn't
  61. * align it wrongfully again to the next page. addr can come in 4K
  62. * unaligned here as a result of stack start randomization.
  63. */
  64. addr = PAGE_ALIGN(addr);
  65. addr = align_vdso_addr(addr);
  66. return addr;
  67. #endif
  68. }
  69. static int map_vdso(const struct vdso_image *image, bool calculate_addr)
  70. {
  71. struct mm_struct *mm = current->mm;
  72. struct vm_area_struct *vma;
  73. unsigned long addr, text_start;
  74. int ret = 0;
  75. static struct page *no_pages[] = {NULL};
  76. static struct vm_special_mapping vvar_mapping = {
  77. .name = "[vvar]",
  78. .pages = no_pages,
  79. };
  80. if (calculate_addr) {
  81. addr = vdso_addr(current->mm->start_stack,
  82. image->size - image->sym_vvar_start);
  83. } else {
  84. addr = 0;
  85. }
  86. down_write(&mm->mmap_sem);
  87. addr = get_unmapped_area(NULL, addr,
  88. image->size - image->sym_vvar_start, 0, 0);
  89. if (IS_ERR_VALUE(addr)) {
  90. ret = addr;
  91. goto up_fail;
  92. }
  93. text_start = addr - image->sym_vvar_start;
  94. current->mm->context.vdso = (void __user *)text_start;
  95. /*
  96. * MAYWRITE to allow gdb to COW and set breakpoints
  97. */
  98. vma = _install_special_mapping(mm,
  99. text_start,
  100. image->size,
  101. VM_READ|VM_EXEC|
  102. VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
  103. &image->text_mapping);
  104. if (IS_ERR(vma)) {
  105. ret = PTR_ERR(vma);
  106. goto up_fail;
  107. }
  108. vma = _install_special_mapping(mm,
  109. addr,
  110. -image->sym_vvar_start,
  111. VM_READ|VM_MAYREAD,
  112. &vvar_mapping);
  113. if (IS_ERR(vma)) {
  114. ret = PTR_ERR(vma);
  115. goto up_fail;
  116. }
  117. if (image->sym_vvar_page)
  118. ret = remap_pfn_range(vma,
  119. text_start + image->sym_vvar_page,
  120. __pa_symbol(&__vvar_page) >> PAGE_SHIFT,
  121. PAGE_SIZE,
  122. PAGE_READONLY);
  123. if (ret)
  124. goto up_fail;
  125. #ifdef CONFIG_HPET_TIMER
  126. if (hpet_address && image->sym_hpet_page) {
  127. ret = io_remap_pfn_range(vma,
  128. text_start + image->sym_hpet_page,
  129. hpet_address >> PAGE_SHIFT,
  130. PAGE_SIZE,
  131. pgprot_noncached(PAGE_READONLY));
  132. if (ret)
  133. goto up_fail;
  134. }
  135. #endif
  136. up_fail:
  137. if (ret)
  138. current->mm->context.vdso = NULL;
  139. up_write(&mm->mmap_sem);
  140. return ret;
  141. }
  142. #if defined(CONFIG_X86_32) || defined(CONFIG_COMPAT)
  143. static int load_vdso32(void)
  144. {
  145. int ret;
  146. if (vdso32_enabled != 1) /* Other values all mean "disabled" */
  147. return 0;
  148. ret = map_vdso(selected_vdso32, false);
  149. if (ret)
  150. return ret;
  151. if (selected_vdso32->sym_VDSO32_SYSENTER_RETURN)
  152. current_thread_info()->sysenter_return =
  153. current->mm->context.vdso +
  154. selected_vdso32->sym_VDSO32_SYSENTER_RETURN;
  155. return 0;
  156. }
  157. #endif
  158. #ifdef CONFIG_X86_64
  159. int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  160. {
  161. if (!vdso64_enabled)
  162. return 0;
  163. return map_vdso(&vdso_image_64, true);
  164. }
  165. #ifdef CONFIG_COMPAT
  166. int compat_arch_setup_additional_pages(struct linux_binprm *bprm,
  167. int uses_interp)
  168. {
  169. #ifdef CONFIG_X86_X32_ABI
  170. if (test_thread_flag(TIF_X32)) {
  171. if (!vdso64_enabled)
  172. return 0;
  173. return map_vdso(&vdso_image_x32, true);
  174. }
  175. #endif
  176. return load_vdso32();
  177. }
  178. #endif
  179. #else
  180. int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  181. {
  182. return load_vdso32();
  183. }
  184. #endif
  185. #ifdef CONFIG_X86_64
  186. static __init int vdso_setup(char *s)
  187. {
  188. vdso64_enabled = simple_strtoul(s, NULL, 0);
  189. return 0;
  190. }
  191. __setup("vdso=", vdso_setup);
  192. #endif
  193. #ifdef CONFIG_X86_64
  194. static void vgetcpu_cpu_init(void *arg)
  195. {
  196. int cpu = smp_processor_id();
  197. struct desc_struct d = { };
  198. unsigned long node = 0;
  199. #ifdef CONFIG_NUMA
  200. node = cpu_to_node(cpu);
  201. #endif
  202. if (cpu_has(&cpu_data(cpu), X86_FEATURE_RDTSCP))
  203. write_rdtscp_aux((node << 12) | cpu);
  204. /*
  205. * Store cpu number in limit so that it can be loaded
  206. * quickly in user space in vgetcpu. (12 bits for the CPU
  207. * and 8 bits for the node)
  208. */
  209. d.limit0 = cpu | ((node & 0xf) << 12);
  210. d.limit = node >> 4;
  211. d.type = 5; /* RO data, expand down, accessed */
  212. d.dpl = 3; /* Visible to user code */
  213. d.s = 1; /* Not a system segment */
  214. d.p = 1; /* Present */
  215. d.d = 1; /* 32-bit */
  216. write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_PER_CPU, &d, DESCTYPE_S);
  217. }
  218. static int
  219. vgetcpu_cpu_notifier(struct notifier_block *n, unsigned long action, void *arg)
  220. {
  221. long cpu = (long)arg;
  222. if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN)
  223. smp_call_function_single(cpu, vgetcpu_cpu_init, NULL, 1);
  224. return NOTIFY_DONE;
  225. }
  226. static int __init init_vdso(void)
  227. {
  228. init_vdso_image(&vdso_image_64);
  229. #ifdef CONFIG_X86_X32_ABI
  230. init_vdso_image(&vdso_image_x32);
  231. #endif
  232. cpu_notifier_register_begin();
  233. on_each_cpu(vgetcpu_cpu_init, NULL, 1);
  234. /* notifier priority > KVM */
  235. __hotcpu_notifier(vgetcpu_cpu_notifier, 30);
  236. cpu_notifier_register_done();
  237. return 0;
  238. }
  239. subsys_initcall(init_vdso);
  240. #endif /* CONFIG_X86_64 */