vdso.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * VDSO implementation for AArch64 and vector page setup for AArch32.
  3. *
  4. * Copyright (C) 2012 ARM Limited
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * Author: Will Deacon <will.deacon@arm.com>
  19. */
  20. #include <linux/cache.h>
  21. #include <linux/clocksource.h>
  22. #include <linux/elf.h>
  23. #include <linux/err.h>
  24. #include <linux/errno.h>
  25. #include <linux/gfp.h>
  26. #include <linux/kernel.h>
  27. #include <linux/mm.h>
  28. #include <linux/sched.h>
  29. #include <linux/signal.h>
  30. #include <linux/slab.h>
  31. #include <linux/timekeeper_internal.h>
  32. #include <linux/vmalloc.h>
  33. #include <asm/cacheflush.h>
  34. #include <asm/signal32.h>
  35. #include <asm/vdso.h>
  36. #include <asm/vdso_datapage.h>
  37. extern char vdso_start[], vdso_end[];
  38. static unsigned long vdso_pages __ro_after_init;
  39. /*
  40. * The vDSO data page.
  41. */
  42. static union {
  43. struct vdso_data data;
  44. u8 page[PAGE_SIZE];
  45. } vdso_data_store __page_aligned_data;
  46. struct vdso_data *vdso_data = &vdso_data_store.data;
  47. #ifdef CONFIG_COMPAT
  48. /*
  49. * Create and map the vectors page for AArch32 tasks.
  50. */
  51. static struct page *vectors_page[1] __ro_after_init;
  52. static int __init alloc_vectors_page(void)
  53. {
  54. extern char __kuser_helper_start[], __kuser_helper_end[];
  55. extern char __aarch32_sigret_code_start[], __aarch32_sigret_code_end[];
  56. int kuser_sz = __kuser_helper_end - __kuser_helper_start;
  57. int sigret_sz = __aarch32_sigret_code_end - __aarch32_sigret_code_start;
  58. unsigned long vpage;
  59. vpage = get_zeroed_page(GFP_ATOMIC);
  60. if (!vpage)
  61. return -ENOMEM;
  62. /* kuser helpers */
  63. memcpy((void *)vpage + 0x1000 - kuser_sz, __kuser_helper_start,
  64. kuser_sz);
  65. /* sigreturn code */
  66. memcpy((void *)vpage + AARCH32_KERN_SIGRET_CODE_OFFSET,
  67. __aarch32_sigret_code_start, sigret_sz);
  68. flush_icache_range(vpage, vpage + PAGE_SIZE);
  69. vectors_page[0] = virt_to_page(vpage);
  70. return 0;
  71. }
  72. arch_initcall(alloc_vectors_page);
  73. int aarch32_setup_vectors_page(struct linux_binprm *bprm, int uses_interp)
  74. {
  75. struct mm_struct *mm = current->mm;
  76. unsigned long addr = AARCH32_VECTORS_BASE;
  77. static const struct vm_special_mapping spec = {
  78. .name = "[vectors]",
  79. .pages = vectors_page,
  80. };
  81. void *ret;
  82. if (down_write_killable(&mm->mmap_sem))
  83. return -EINTR;
  84. current->mm->context.vdso = (void *)addr;
  85. /* Map vectors page at the high address. */
  86. ret = _install_special_mapping(mm, addr, PAGE_SIZE,
  87. VM_READ|VM_EXEC|VM_MAYREAD|VM_MAYEXEC,
  88. &spec);
  89. up_write(&mm->mmap_sem);
  90. return PTR_ERR_OR_ZERO(ret);
  91. }
  92. #endif /* CONFIG_COMPAT */
  93. static int vdso_mremap(const struct vm_special_mapping *sm,
  94. struct vm_area_struct *new_vma)
  95. {
  96. unsigned long new_size = new_vma->vm_end - new_vma->vm_start;
  97. unsigned long vdso_size = vdso_end - vdso_start;
  98. if (vdso_size != new_size)
  99. return -EINVAL;
  100. current->mm->context.vdso = (void *)new_vma->vm_start;
  101. return 0;
  102. }
  103. static struct vm_special_mapping vdso_spec[2] __ro_after_init = {
  104. {
  105. .name = "[vvar]",
  106. },
  107. {
  108. .name = "[vdso]",
  109. .mremap = vdso_mremap,
  110. },
  111. };
  112. static int __init vdso_init(void)
  113. {
  114. int i;
  115. struct page **vdso_pagelist;
  116. unsigned long pfn;
  117. if (memcmp(vdso_start, "\177ELF", 4)) {
  118. pr_err("vDSO is not a valid ELF object!\n");
  119. return -EINVAL;
  120. }
  121. vdso_pages = (vdso_end - vdso_start) >> PAGE_SHIFT;
  122. pr_info("vdso: %ld pages (%ld code @ %p, %ld data @ %p)\n",
  123. vdso_pages + 1, vdso_pages, vdso_start, 1L, vdso_data);
  124. /* Allocate the vDSO pagelist, plus a page for the data. */
  125. vdso_pagelist = kcalloc(vdso_pages + 1, sizeof(struct page *),
  126. GFP_KERNEL);
  127. if (vdso_pagelist == NULL)
  128. return -ENOMEM;
  129. /* Grab the vDSO data page. */
  130. vdso_pagelist[0] = phys_to_page(__pa_symbol(vdso_data));
  131. /* Grab the vDSO code pages. */
  132. pfn = sym_to_pfn(vdso_start);
  133. for (i = 0; i < vdso_pages; i++)
  134. vdso_pagelist[i + 1] = pfn_to_page(pfn + i);
  135. vdso_spec[0].pages = &vdso_pagelist[0];
  136. vdso_spec[1].pages = &vdso_pagelist[1];
  137. return 0;
  138. }
  139. arch_initcall(vdso_init);
  140. int arch_setup_additional_pages(struct linux_binprm *bprm,
  141. int uses_interp)
  142. {
  143. struct mm_struct *mm = current->mm;
  144. unsigned long vdso_base, vdso_text_len, vdso_mapping_len;
  145. void *ret;
  146. vdso_text_len = vdso_pages << PAGE_SHIFT;
  147. /* Be sure to map the data page */
  148. vdso_mapping_len = vdso_text_len + PAGE_SIZE;
  149. if (down_write_killable(&mm->mmap_sem))
  150. return -EINTR;
  151. vdso_base = get_unmapped_area(NULL, 0, vdso_mapping_len, 0, 0);
  152. if (IS_ERR_VALUE(vdso_base)) {
  153. ret = ERR_PTR(vdso_base);
  154. goto up_fail;
  155. }
  156. ret = _install_special_mapping(mm, vdso_base, PAGE_SIZE,
  157. VM_READ|VM_MAYREAD,
  158. &vdso_spec[0]);
  159. if (IS_ERR(ret))
  160. goto up_fail;
  161. vdso_base += PAGE_SIZE;
  162. mm->context.vdso = (void *)vdso_base;
  163. ret = _install_special_mapping(mm, vdso_base, vdso_text_len,
  164. VM_READ|VM_EXEC|
  165. VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
  166. &vdso_spec[1]);
  167. if (IS_ERR(ret))
  168. goto up_fail;
  169. up_write(&mm->mmap_sem);
  170. return 0;
  171. up_fail:
  172. mm->context.vdso = NULL;
  173. up_write(&mm->mmap_sem);
  174. return PTR_ERR(ret);
  175. }
  176. /*
  177. * Update the vDSO data page to keep in sync with kernel timekeeping.
  178. */
  179. void update_vsyscall(struct timekeeper *tk)
  180. {
  181. u32 use_syscall = !tk->tkr_mono.clock->archdata.vdso_direct;
  182. ++vdso_data->tb_seq_count;
  183. smp_wmb();
  184. vdso_data->use_syscall = use_syscall;
  185. vdso_data->xtime_coarse_sec = tk->xtime_sec;
  186. vdso_data->xtime_coarse_nsec = tk->tkr_mono.xtime_nsec >>
  187. tk->tkr_mono.shift;
  188. vdso_data->wtm_clock_sec = tk->wall_to_monotonic.tv_sec;
  189. vdso_data->wtm_clock_nsec = tk->wall_to_monotonic.tv_nsec;
  190. if (!use_syscall) {
  191. /* tkr_mono.cycle_last == tkr_raw.cycle_last */
  192. vdso_data->cs_cycle_last = tk->tkr_mono.cycle_last;
  193. vdso_data->raw_time_sec = tk->raw_sec;
  194. vdso_data->raw_time_nsec = tk->tkr_raw.xtime_nsec;
  195. vdso_data->xtime_clock_sec = tk->xtime_sec;
  196. vdso_data->xtime_clock_nsec = tk->tkr_mono.xtime_nsec;
  197. vdso_data->cs_mono_mult = tk->tkr_mono.mult;
  198. vdso_data->cs_raw_mult = tk->tkr_raw.mult;
  199. /* tkr_mono.shift == tkr_raw.shift */
  200. vdso_data->cs_shift = tk->tkr_mono.shift;
  201. }
  202. smp_wmb();
  203. ++vdso_data->tb_seq_count;
  204. }
  205. void update_vsyscall_tz(void)
  206. {
  207. vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
  208. vdso_data->tz_dsttime = sys_tz.tz_dsttime;
  209. }