vdso.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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/kernel.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/mm.h>
  27. #include <linux/sched.h>
  28. #include <linux/signal.h>
  29. #include <linux/slab.h>
  30. #include <linux/vmalloc.h>
  31. #include <asm/cacheflush.h>
  32. #include <asm/signal32.h>
  33. #include <asm/vdso.h>
  34. #include <asm/vdso_datapage.h>
  35. extern char vdso_start, vdso_end;
  36. static unsigned long vdso_pages;
  37. static struct page **vdso_pagelist;
  38. /*
  39. * The vDSO data page.
  40. */
  41. static union {
  42. struct vdso_data data;
  43. u8 page[PAGE_SIZE];
  44. } vdso_data_store __page_aligned_data;
  45. struct vdso_data *vdso_data = &vdso_data_store.data;
  46. #ifdef CONFIG_COMPAT
  47. /*
  48. * Create and map the vectors page for AArch32 tasks.
  49. */
  50. static struct page *vectors_page[1];
  51. static int alloc_vectors_page(void)
  52. {
  53. extern char __kuser_helper_start[], __kuser_helper_end[];
  54. int kuser_sz = __kuser_helper_end - __kuser_helper_start;
  55. unsigned long vpage;
  56. vpage = get_zeroed_page(GFP_ATOMIC);
  57. if (!vpage)
  58. return -ENOMEM;
  59. /* kuser helpers */
  60. memcpy((void *)vpage + 0x1000 - kuser_sz, __kuser_helper_start,
  61. kuser_sz);
  62. /* sigreturn code */
  63. memcpy((void *)vpage + AARCH32_KERN_SIGRET_CODE_OFFSET,
  64. aarch32_sigret_code, sizeof(aarch32_sigret_code));
  65. flush_icache_range(vpage, vpage + PAGE_SIZE);
  66. vectors_page[0] = virt_to_page(vpage);
  67. return 0;
  68. }
  69. arch_initcall(alloc_vectors_page);
  70. int aarch32_setup_vectors_page(struct linux_binprm *bprm, int uses_interp)
  71. {
  72. struct mm_struct *mm = current->mm;
  73. unsigned long addr = AARCH32_VECTORS_BASE;
  74. int ret;
  75. down_write(&mm->mmap_sem);
  76. current->mm->context.vdso = (void *)addr;
  77. /* Map vectors page at the high address. */
  78. ret = install_special_mapping(mm, addr, PAGE_SIZE,
  79. VM_READ|VM_EXEC|VM_MAYREAD|VM_MAYEXEC,
  80. vectors_page);
  81. up_write(&mm->mmap_sem);
  82. return ret;
  83. }
  84. #endif /* CONFIG_COMPAT */
  85. static int __init vdso_init(void)
  86. {
  87. struct page *pg;
  88. char *vbase;
  89. int i, ret = 0;
  90. vdso_pages = (&vdso_end - &vdso_start) >> PAGE_SHIFT;
  91. pr_info("vdso: %ld pages (%ld code, %ld data) at base %p\n",
  92. vdso_pages + 1, vdso_pages, 1L, &vdso_start);
  93. /* Allocate the vDSO pagelist, plus a page for the data. */
  94. vdso_pagelist = kzalloc(sizeof(struct page *) * (vdso_pages + 1),
  95. GFP_KERNEL);
  96. if (vdso_pagelist == NULL) {
  97. pr_err("Failed to allocate vDSO pagelist!\n");
  98. return -ENOMEM;
  99. }
  100. /* Grab the vDSO code pages. */
  101. for (i = 0; i < vdso_pages; i++) {
  102. pg = virt_to_page(&vdso_start + i*PAGE_SIZE);
  103. ClearPageReserved(pg);
  104. get_page(pg);
  105. vdso_pagelist[i] = pg;
  106. }
  107. /* Sanity check the shared object header. */
  108. vbase = vmap(vdso_pagelist, 1, 0, PAGE_KERNEL);
  109. if (vbase == NULL) {
  110. pr_err("Failed to map vDSO pagelist!\n");
  111. return -ENOMEM;
  112. } else if (memcmp(vbase, "\177ELF", 4)) {
  113. pr_err("vDSO is not a valid ELF object!\n");
  114. ret = -EINVAL;
  115. goto unmap;
  116. }
  117. /* Grab the vDSO data page. */
  118. pg = virt_to_page(vdso_data);
  119. get_page(pg);
  120. vdso_pagelist[i] = pg;
  121. unmap:
  122. vunmap(vbase);
  123. return ret;
  124. }
  125. arch_initcall(vdso_init);
  126. int arch_setup_additional_pages(struct linux_binprm *bprm,
  127. int uses_interp)
  128. {
  129. struct mm_struct *mm = current->mm;
  130. unsigned long vdso_base, vdso_mapping_len;
  131. int ret;
  132. /* Be sure to map the data page */
  133. vdso_mapping_len = (vdso_pages + 1) << PAGE_SHIFT;
  134. down_write(&mm->mmap_sem);
  135. vdso_base = get_unmapped_area(NULL, 0, vdso_mapping_len, 0, 0);
  136. if (IS_ERR_VALUE(vdso_base)) {
  137. ret = vdso_base;
  138. goto up_fail;
  139. }
  140. mm->context.vdso = (void *)vdso_base;
  141. ret = install_special_mapping(mm, vdso_base, vdso_mapping_len,
  142. VM_READ|VM_EXEC|
  143. VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
  144. vdso_pagelist);
  145. if (ret) {
  146. mm->context.vdso = NULL;
  147. goto up_fail;
  148. }
  149. up_fail:
  150. up_write(&mm->mmap_sem);
  151. return ret;
  152. }
  153. const char *arch_vma_name(struct vm_area_struct *vma)
  154. {
  155. /*
  156. * We can re-use the vdso pointer in mm_context_t for identifying
  157. * the vectors page for compat applications. The vDSO will always
  158. * sit above TASK_UNMAPPED_BASE and so we don't need to worry about
  159. * it conflicting with the vectors base.
  160. */
  161. if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso) {
  162. #ifdef CONFIG_COMPAT
  163. if (vma->vm_start == AARCH32_VECTORS_BASE)
  164. return "[vectors]";
  165. #endif
  166. return "[vdso]";
  167. }
  168. return NULL;
  169. }
  170. /*
  171. * We define AT_SYSINFO_EHDR, so we need these function stubs to keep
  172. * Linux happy.
  173. */
  174. int in_gate_area_no_mm(unsigned long addr)
  175. {
  176. return 0;
  177. }
  178. int in_gate_area(struct mm_struct *mm, unsigned long addr)
  179. {
  180. return 0;
  181. }
  182. struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
  183. {
  184. return NULL;
  185. }
  186. /*
  187. * Update the vDSO data page to keep in sync with kernel timekeeping.
  188. */
  189. void update_vsyscall(struct timespec *ts, struct timespec *wtm,
  190. struct clocksource *clock, u32 mult)
  191. {
  192. struct timespec xtime_coarse;
  193. u32 use_syscall = strcmp(clock->name, "arch_sys_counter");
  194. ++vdso_data->tb_seq_count;
  195. smp_wmb();
  196. xtime_coarse = __current_kernel_time();
  197. vdso_data->use_syscall = use_syscall;
  198. vdso_data->xtime_coarse_sec = xtime_coarse.tv_sec;
  199. vdso_data->xtime_coarse_nsec = xtime_coarse.tv_nsec;
  200. if (!use_syscall) {
  201. vdso_data->cs_cycle_last = clock->cycle_last;
  202. vdso_data->xtime_clock_sec = ts->tv_sec;
  203. vdso_data->xtime_clock_nsec = ts->tv_nsec;
  204. vdso_data->cs_mult = mult;
  205. vdso_data->cs_shift = clock->shift;
  206. vdso_data->wtm_clock_sec = wtm->tv_sec;
  207. vdso_data->wtm_clock_nsec = wtm->tv_nsec;
  208. }
  209. smp_wmb();
  210. ++vdso_data->tb_seq_count;
  211. }
  212. void update_vsyscall_tz(void)
  213. {
  214. ++vdso_data->tb_seq_count;
  215. smp_wmb();
  216. vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
  217. vdso_data->tz_dsttime = sys_tz.tz_dsttime;
  218. smp_wmb();
  219. ++vdso_data->tb_seq_count;
  220. }