vdso.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2012 ARM Limited
  3. // Copyright (C) 2005-2017 Andes Technology Corporation
  4. #include <linux/cache.h>
  5. #include <linux/clocksource.h>
  6. #include <linux/elf.h>
  7. #include <linux/err.h>
  8. #include <linux/errno.h>
  9. #include <linux/gfp.h>
  10. #include <linux/kernel.h>
  11. #include <linux/mm.h>
  12. #include <linux/sched.h>
  13. #include <linux/signal.h>
  14. #include <linux/slab.h>
  15. #include <linux/timekeeper_internal.h>
  16. #include <linux/vmalloc.h>
  17. #include <linux/random.h>
  18. #include <asm/cacheflush.h>
  19. #include <asm/vdso.h>
  20. #include <asm/vdso_datapage.h>
  21. #include <asm/vdso_timer_info.h>
  22. #include <asm/cache_info.h>
  23. extern struct cache_info L1_cache_info[2];
  24. extern char vdso_start[], vdso_end[];
  25. static unsigned long vdso_pages __ro_after_init;
  26. static unsigned long timer_mapping_base;
  27. struct timer_info_t timer_info = {
  28. .cycle_count_down = true,
  29. .mapping_base = EMPTY_TIMER_MAPPING,
  30. .cycle_count_reg_offset = EMPTY_REG_OFFSET
  31. };
  32. /*
  33. * The vDSO data page.
  34. */
  35. static struct page *no_pages[] = { NULL };
  36. static union {
  37. struct vdso_data data;
  38. u8 page[PAGE_SIZE];
  39. } vdso_data_store __page_aligned_data;
  40. struct vdso_data *vdso_data = &vdso_data_store.data;
  41. static struct vm_special_mapping vdso_spec[2] __ro_after_init = {
  42. {
  43. .name = "[vvar]",
  44. .pages = no_pages,
  45. },
  46. {
  47. .name = "[vdso]",
  48. },
  49. };
  50. static void get_timer_node_info(void)
  51. {
  52. timer_mapping_base = timer_info.mapping_base;
  53. vdso_data->cycle_count_offset =
  54. timer_info.cycle_count_reg_offset;
  55. vdso_data->cycle_count_down =
  56. timer_info.cycle_count_down;
  57. }
  58. static int __init vdso_init(void)
  59. {
  60. int i;
  61. struct page **vdso_pagelist;
  62. if (memcmp(vdso_start, "\177ELF", 4)) {
  63. pr_err("vDSO is not a valid ELF object!\n");
  64. return -EINVAL;
  65. }
  66. /* Creat a timer io mapping to get clock cycles counter */
  67. get_timer_node_info();
  68. vdso_pages = (vdso_end - vdso_start) >> PAGE_SHIFT;
  69. pr_info("vdso: %ld pages (%ld code @ %p, %ld data @ %p)\n",
  70. vdso_pages + 1, vdso_pages, vdso_start, 1L, vdso_data);
  71. /* Allocate the vDSO pagelist */
  72. vdso_pagelist = kcalloc(vdso_pages, sizeof(struct page *), GFP_KERNEL);
  73. if (vdso_pagelist == NULL)
  74. return -ENOMEM;
  75. for (i = 0; i < vdso_pages; i++)
  76. vdso_pagelist[i] = virt_to_page(vdso_start + i * PAGE_SIZE);
  77. vdso_spec[1].pages = &vdso_pagelist[0];
  78. return 0;
  79. }
  80. arch_initcall(vdso_init);
  81. unsigned long inline vdso_random_addr(unsigned long vdso_mapping_len)
  82. {
  83. unsigned long start = current->mm->mmap_base, end, offset, addr;
  84. start = PAGE_ALIGN(start);
  85. /* Round the lowest possible end address up to a PMD boundary. */
  86. end = (start + vdso_mapping_len + PMD_SIZE - 1) & PMD_MASK;
  87. if (end >= TASK_SIZE)
  88. end = TASK_SIZE;
  89. end -= vdso_mapping_len;
  90. if (end > start) {
  91. offset = get_random_int() % (((end - start) >> PAGE_SHIFT) + 1);
  92. addr = start + (offset << PAGE_SHIFT);
  93. } else {
  94. addr = start;
  95. }
  96. return addr;
  97. }
  98. int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  99. {
  100. struct mm_struct *mm = current->mm;
  101. unsigned long vdso_base, vdso_text_len, vdso_mapping_len;
  102. struct vm_area_struct *vma;
  103. unsigned long addr = 0;
  104. pgprot_t prot;
  105. int ret, vvar_page_num = 2;
  106. vdso_text_len = vdso_pages << PAGE_SHIFT;
  107. if(timer_mapping_base == EMPTY_VALUE)
  108. vvar_page_num = 1;
  109. /* Be sure to map the data page */
  110. vdso_mapping_len = vdso_text_len + vvar_page_num * PAGE_SIZE;
  111. #ifdef CONFIG_CPU_CACHE_ALIASING
  112. vdso_mapping_len += L1_cache_info[DCACHE].aliasing_num - 1;
  113. #endif
  114. if (down_write_killable(&mm->mmap_sem))
  115. return -EINTR;
  116. addr = vdso_random_addr(vdso_mapping_len);
  117. vdso_base = get_unmapped_area(NULL, addr, vdso_mapping_len, 0, 0);
  118. if (IS_ERR_VALUE(vdso_base)) {
  119. ret = vdso_base;
  120. goto up_fail;
  121. }
  122. #ifdef CONFIG_CPU_CACHE_ALIASING
  123. {
  124. unsigned int aliasing_mask =
  125. L1_cache_info[DCACHE].aliasing_mask;
  126. unsigned int page_colour_ofs;
  127. page_colour_ofs = ((unsigned int)vdso_data & aliasing_mask) -
  128. (vdso_base & aliasing_mask);
  129. vdso_base += page_colour_ofs & aliasing_mask;
  130. }
  131. #endif
  132. vma = _install_special_mapping(mm, vdso_base, vvar_page_num * PAGE_SIZE,
  133. VM_READ | VM_MAYREAD, &vdso_spec[0]);
  134. if (IS_ERR(vma)) {
  135. ret = PTR_ERR(vma);
  136. goto up_fail;
  137. }
  138. /*Map vdata to user space */
  139. ret = io_remap_pfn_range(vma, vdso_base,
  140. virt_to_phys(vdso_data) >> PAGE_SHIFT,
  141. PAGE_SIZE, vma->vm_page_prot);
  142. if (ret)
  143. goto up_fail;
  144. /*Map timer to user space */
  145. vdso_base += PAGE_SIZE;
  146. prot = __pgprot(_PAGE_V | _PAGE_M_UR_KR | _PAGE_D | _PAGE_C_DEV);
  147. ret = io_remap_pfn_range(vma, vdso_base, timer_mapping_base >> PAGE_SHIFT,
  148. PAGE_SIZE, prot);
  149. if (ret)
  150. goto up_fail;
  151. /*Map vdso to user space */
  152. vdso_base += PAGE_SIZE;
  153. mm->context.vdso = (void *)vdso_base;
  154. vma = _install_special_mapping(mm, vdso_base, vdso_text_len,
  155. VM_READ | VM_EXEC |
  156. VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
  157. &vdso_spec[1]);
  158. if (IS_ERR(vma)) {
  159. ret = PTR_ERR(vma);
  160. goto up_fail;
  161. }
  162. up_write(&mm->mmap_sem);
  163. return 0;
  164. up_fail:
  165. mm->context.vdso = NULL;
  166. up_write(&mm->mmap_sem);
  167. return ret;
  168. }
  169. static void vdso_write_begin(struct vdso_data *vdata)
  170. {
  171. ++vdso_data->seq_count;
  172. smp_wmb(); /* Pairs with smp_rmb in vdso_read_retry */
  173. }
  174. static void vdso_write_end(struct vdso_data *vdata)
  175. {
  176. smp_wmb(); /* Pairs with smp_rmb in vdso_read_begin */
  177. ++vdso_data->seq_count;
  178. }
  179. void update_vsyscall(struct timekeeper *tk)
  180. {
  181. vdso_write_begin(vdso_data);
  182. vdso_data->cs_mask = tk->tkr_mono.mask;
  183. vdso_data->cs_mult = tk->tkr_mono.mult;
  184. vdso_data->cs_shift = tk->tkr_mono.shift;
  185. vdso_data->cs_cycle_last = tk->tkr_mono.cycle_last;
  186. vdso_data->wtm_clock_sec = tk->wall_to_monotonic.tv_sec;
  187. vdso_data->wtm_clock_nsec = tk->wall_to_monotonic.tv_nsec;
  188. vdso_data->xtime_clock_sec = tk->xtime_sec;
  189. vdso_data->xtime_clock_nsec = tk->tkr_mono.xtime_nsec;
  190. vdso_data->xtime_coarse_sec = tk->xtime_sec;
  191. vdso_data->xtime_coarse_nsec = tk->tkr_mono.xtime_nsec >>
  192. tk->tkr_mono.shift;
  193. vdso_write_end(vdso_data);
  194. }
  195. void update_vsyscall_tz(void)
  196. {
  197. vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
  198. vdso_data->tz_dsttime = sys_tz.tz_dsttime;
  199. }