vdso.c 8.8 KB

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