vdso.c 8.9 KB

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