vdso.c 8.4 KB

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