vdso.c 8.5 KB

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