vdso.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * Adapted from arm64 version.
  3. *
  4. * Copyright (C) 2012 ARM Limited
  5. * Copyright (C) 2015 Mentor Graphics Corporation.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/elf.h>
  20. #include <linux/err.h>
  21. #include <linux/kernel.h>
  22. #include <linux/mm.h>
  23. #include <linux/of.h>
  24. #include <linux/printk.h>
  25. #include <linux/slab.h>
  26. #include <linux/timekeeper_internal.h>
  27. #include <linux/vmalloc.h>
  28. #include <asm/arch_timer.h>
  29. #include <asm/barrier.h>
  30. #include <asm/cacheflush.h>
  31. #include <asm/page.h>
  32. #include <asm/vdso.h>
  33. #include <asm/vdso_datapage.h>
  34. #include <clocksource/arm_arch_timer.h>
  35. #define MAX_SYMNAME 64
  36. static struct page **vdso_text_pagelist;
  37. /* Total number of pages needed for the data and text portions of the VDSO. */
  38. unsigned int vdso_total_pages __read_mostly;
  39. /*
  40. * The VDSO data page.
  41. */
  42. static union vdso_data_store vdso_data_store __page_aligned_data;
  43. static struct vdso_data *vdso_data = &vdso_data_store.data;
  44. static struct page *vdso_data_page;
  45. static struct vm_special_mapping vdso_data_mapping = {
  46. .name = "[vvar]",
  47. .pages = &vdso_data_page,
  48. };
  49. static struct vm_special_mapping vdso_text_mapping = {
  50. .name = "[vdso]",
  51. };
  52. struct elfinfo {
  53. Elf32_Ehdr *hdr; /* ptr to ELF */
  54. Elf32_Sym *dynsym; /* ptr to .dynsym section */
  55. unsigned long dynsymsize; /* size of .dynsym section */
  56. char *dynstr; /* ptr to .dynstr section */
  57. };
  58. /* Cached result of boot-time check for whether the arch timer exists,
  59. * and if so, whether the virtual counter is useable.
  60. */
  61. static bool cntvct_ok __read_mostly;
  62. static bool __init cntvct_functional(void)
  63. {
  64. struct device_node *np;
  65. bool ret = false;
  66. if (!IS_ENABLED(CONFIG_ARM_ARCH_TIMER))
  67. goto out;
  68. /* The arm_arch_timer core should export
  69. * arch_timer_use_virtual or similar so we don't have to do
  70. * this.
  71. */
  72. np = of_find_compatible_node(NULL, NULL, "arm,armv7-timer");
  73. if (!np)
  74. goto out_put;
  75. if (of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
  76. goto out_put;
  77. ret = true;
  78. out_put:
  79. of_node_put(np);
  80. out:
  81. return ret;
  82. }
  83. static void * __init find_section(Elf32_Ehdr *ehdr, const char *name,
  84. unsigned long *size)
  85. {
  86. Elf32_Shdr *sechdrs;
  87. unsigned int i;
  88. char *secnames;
  89. /* Grab section headers and strings so we can tell who is who */
  90. sechdrs = (void *)ehdr + ehdr->e_shoff;
  91. secnames = (void *)ehdr + sechdrs[ehdr->e_shstrndx].sh_offset;
  92. /* Find the section they want */
  93. for (i = 1; i < ehdr->e_shnum; i++) {
  94. if (strcmp(secnames + sechdrs[i].sh_name, name) == 0) {
  95. if (size)
  96. *size = sechdrs[i].sh_size;
  97. return (void *)ehdr + sechdrs[i].sh_offset;
  98. }
  99. }
  100. if (size)
  101. *size = 0;
  102. return NULL;
  103. }
  104. static Elf32_Sym * __init find_symbol(struct elfinfo *lib, const char *symname)
  105. {
  106. unsigned int i;
  107. for (i = 0; i < (lib->dynsymsize / sizeof(Elf32_Sym)); i++) {
  108. char name[MAX_SYMNAME], *c;
  109. if (lib->dynsym[i].st_name == 0)
  110. continue;
  111. strlcpy(name, lib->dynstr + lib->dynsym[i].st_name,
  112. MAX_SYMNAME);
  113. c = strchr(name, '@');
  114. if (c)
  115. *c = 0;
  116. if (strcmp(symname, name) == 0)
  117. return &lib->dynsym[i];
  118. }
  119. return NULL;
  120. }
  121. static void __init vdso_nullpatch_one(struct elfinfo *lib, const char *symname)
  122. {
  123. Elf32_Sym *sym;
  124. sym = find_symbol(lib, symname);
  125. if (!sym)
  126. return;
  127. sym->st_name = 0;
  128. }
  129. static void __init patch_vdso(void *ehdr)
  130. {
  131. struct elfinfo einfo;
  132. einfo = (struct elfinfo) {
  133. .hdr = ehdr,
  134. };
  135. einfo.dynsym = find_section(einfo.hdr, ".dynsym", &einfo.dynsymsize);
  136. einfo.dynstr = find_section(einfo.hdr, ".dynstr", NULL);
  137. /* If the virtual counter is absent or non-functional we don't
  138. * want programs to incur the slight additional overhead of
  139. * dispatching through the VDSO only to fall back to syscalls.
  140. */
  141. if (!cntvct_ok) {
  142. vdso_nullpatch_one(&einfo, "__vdso_gettimeofday");
  143. vdso_nullpatch_one(&einfo, "__vdso_clock_gettime");
  144. }
  145. }
  146. static int __init vdso_init(void)
  147. {
  148. unsigned int text_pages;
  149. int i;
  150. if (memcmp(&vdso_start, "\177ELF", 4)) {
  151. pr_err("VDSO is not a valid ELF object!\n");
  152. return -ENOEXEC;
  153. }
  154. text_pages = (&vdso_end - &vdso_start) >> PAGE_SHIFT;
  155. pr_debug("vdso: %i text pages at base %p\n", text_pages, &vdso_start);
  156. /* Allocate the VDSO text pagelist */
  157. vdso_text_pagelist = kcalloc(text_pages, sizeof(struct page *),
  158. GFP_KERNEL);
  159. if (vdso_text_pagelist == NULL)
  160. return -ENOMEM;
  161. /* Grab the VDSO data page. */
  162. vdso_data_page = virt_to_page(vdso_data);
  163. /* Grab the VDSO text pages. */
  164. for (i = 0; i < text_pages; i++) {
  165. struct page *page;
  166. page = virt_to_page(&vdso_start + i * PAGE_SIZE);
  167. vdso_text_pagelist[i] = page;
  168. }
  169. vdso_text_mapping.pages = vdso_text_pagelist;
  170. vdso_total_pages = 1; /* for the data/vvar page */
  171. vdso_total_pages += text_pages;
  172. cntvct_ok = cntvct_functional();
  173. patch_vdso(&vdso_start);
  174. return 0;
  175. }
  176. arch_initcall(vdso_init);
  177. static int install_vvar(struct mm_struct *mm, unsigned long addr)
  178. {
  179. struct vm_area_struct *vma;
  180. vma = _install_special_mapping(mm, addr, PAGE_SIZE,
  181. VM_READ | VM_MAYREAD,
  182. &vdso_data_mapping);
  183. return IS_ERR(vma) ? PTR_ERR(vma) : 0;
  184. }
  185. /* assumes mmap_sem is write-locked */
  186. void arm_install_vdso(struct mm_struct *mm, unsigned long addr)
  187. {
  188. struct vm_area_struct *vma;
  189. unsigned long len;
  190. mm->context.vdso = 0;
  191. if (vdso_text_pagelist == NULL)
  192. return;
  193. if (install_vvar(mm, addr))
  194. return;
  195. /* Account for vvar page. */
  196. addr += PAGE_SIZE;
  197. len = (vdso_total_pages - 1) << PAGE_SHIFT;
  198. vma = _install_special_mapping(mm, addr, len,
  199. VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
  200. &vdso_text_mapping);
  201. if (!IS_ERR(vma))
  202. mm->context.vdso = addr;
  203. }
  204. static void vdso_write_begin(struct vdso_data *vdata)
  205. {
  206. ++vdso_data->seq_count;
  207. smp_wmb(); /* Pairs with smp_rmb in vdso_read_retry */
  208. }
  209. static void vdso_write_end(struct vdso_data *vdata)
  210. {
  211. smp_wmb(); /* Pairs with smp_rmb in vdso_read_begin */
  212. ++vdso_data->seq_count;
  213. }
  214. static bool tk_is_cntvct(const struct timekeeper *tk)
  215. {
  216. if (!IS_ENABLED(CONFIG_ARM_ARCH_TIMER))
  217. return false;
  218. if (strcmp(tk->tkr_mono.clock->name, "arch_sys_counter") != 0)
  219. return false;
  220. return true;
  221. }
  222. /**
  223. * update_vsyscall - update the vdso data page
  224. *
  225. * Increment the sequence counter, making it odd, indicating to
  226. * userspace that an update is in progress. Update the fields used
  227. * for coarse clocks and, if the architected system timer is in use,
  228. * the fields used for high precision clocks. Increment the sequence
  229. * counter again, making it even, indicating to userspace that the
  230. * update is finished.
  231. *
  232. * Userspace is expected to sample seq_count before reading any other
  233. * fields from the data page. If seq_count is odd, userspace is
  234. * expected to wait until it becomes even. After copying data from
  235. * the page, userspace must sample seq_count again; if it has changed
  236. * from its previous value, userspace must retry the whole sequence.
  237. *
  238. * Calls to update_vsyscall are serialized by the timekeeping core.
  239. */
  240. void update_vsyscall(struct timekeeper *tk)
  241. {
  242. struct timespec64 *wtm = &tk->wall_to_monotonic;
  243. if (!cntvct_ok) {
  244. /* The entry points have been zeroed, so there is no
  245. * point in updating the data page.
  246. */
  247. return;
  248. }
  249. vdso_write_begin(vdso_data);
  250. vdso_data->tk_is_cntvct = tk_is_cntvct(tk);
  251. vdso_data->xtime_coarse_sec = tk->xtime_sec;
  252. vdso_data->xtime_coarse_nsec = (u32)(tk->tkr_mono.xtime_nsec >>
  253. tk->tkr_mono.shift);
  254. vdso_data->wtm_clock_sec = wtm->tv_sec;
  255. vdso_data->wtm_clock_nsec = wtm->tv_nsec;
  256. if (vdso_data->tk_is_cntvct) {
  257. vdso_data->cs_cycle_last = tk->tkr_mono.cycle_last;
  258. vdso_data->xtime_clock_sec = tk->xtime_sec;
  259. vdso_data->xtime_clock_snsec = tk->tkr_mono.xtime_nsec;
  260. vdso_data->cs_mult = tk->tkr_mono.mult;
  261. vdso_data->cs_shift = tk->tkr_mono.shift;
  262. vdso_data->cs_mask = tk->tkr_mono.mask;
  263. }
  264. vdso_write_end(vdso_data);
  265. flush_dcache_page(virt_to_page(vdso_data));
  266. }
  267. void update_vsyscall_tz(void)
  268. {
  269. vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
  270. vdso_data->tz_dsttime = sys_tz.tz_dsttime;
  271. flush_dcache_page(virt_to_page(vdso_data));
  272. }