vdso.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Adapted from arm64 version.
  4. *
  5. * Copyright (C) 2012 ARM Limited
  6. * Copyright (C) 2015 Mentor Graphics Corporation.
  7. */
  8. #include <linux/cache.h>
  9. #include <linux/elf.h>
  10. #include <linux/err.h>
  11. #include <linux/kernel.h>
  12. #include <linux/mm.h>
  13. #include <linux/of.h>
  14. #include <linux/printk.h>
  15. #include <linux/slab.h>
  16. #include <linux/timekeeper_internal.h>
  17. #include <linux/vmalloc.h>
  18. #include <asm/arch_timer.h>
  19. #include <asm/barrier.h>
  20. #include <asm/cacheflush.h>
  21. #include <asm/page.h>
  22. #include <asm/vdso.h>
  23. #include <asm/vdso_datapage.h>
  24. #include <clocksource/arm_arch_timer.h>
  25. #include <vdso/helpers.h>
  26. #include <vdso/vsyscall.h>
  27. #define MAX_SYMNAME 64
  28. static struct page **vdso_text_pagelist;
  29. extern char vdso_start[], vdso_end[];
  30. /* Total number of pages needed for the data and text portions of the VDSO. */
  31. unsigned int vdso_total_pages __ro_after_init;
  32. /*
  33. * The VDSO data page.
  34. */
  35. static union vdso_data_store vdso_data_store __page_aligned_data;
  36. struct vdso_data *vdso_data = vdso_data_store.data;
  37. static struct page *vdso_data_page __ro_after_init;
  38. static const struct vm_special_mapping vdso_data_mapping = {
  39. .name = "[vvar]",
  40. .pages = &vdso_data_page,
  41. };
  42. static int vdso_mremap(const struct vm_special_mapping *sm,
  43. struct vm_area_struct *new_vma)
  44. {
  45. current->mm->context.vdso = new_vma->vm_start;
  46. return 0;
  47. }
  48. static struct vm_special_mapping vdso_text_mapping __ro_after_init = {
  49. .name = "[vdso]",
  50. .mremap = vdso_mremap,
  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. bool cntvct_ok __ro_after_init;
  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. np = of_find_compatible_node(NULL, NULL, "arm,armv8-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. vdso_nullpatch_one(&einfo, "__vdso_clock_gettime64");
  147. }
  148. }
  149. static int __init vdso_init(void)
  150. {
  151. unsigned int text_pages;
  152. int i;
  153. if (memcmp(vdso_start, "\177ELF", 4)) {
  154. pr_err("VDSO is not a valid ELF object!\n");
  155. return -ENOEXEC;
  156. }
  157. text_pages = (vdso_end - vdso_start) >> PAGE_SHIFT;
  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_lock 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. }