vdso.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 <clocksource/arm_arch_timer.h>
  24. #include <vdso/helpers.h>
  25. #include <vdso/vsyscall.h>
  26. #define MAX_SYMNAME 64
  27. static struct page **vdso_text_pagelist;
  28. extern char vdso_start[], vdso_end[];
  29. /* Total number of pages needed for the data and text portions of the VDSO. */
  30. unsigned int vdso_total_pages __ro_after_init;
  31. static union vdso_data_store vdso_data_store __page_aligned_data;
  32. struct vdso_data *vdso_data = vdso_data_store.data;
  33. static struct page *vdso_data_page __ro_after_init;
  34. static const struct vm_special_mapping vdso_data_mapping = {
  35. .name = "[vvar]",
  36. .pages = &vdso_data_page,
  37. };
  38. static int vdso_mremap(const struct vm_special_mapping *sm,
  39. struct vm_area_struct *new_vma)
  40. {
  41. current->mm->context.vdso = new_vma->vm_start;
  42. return 0;
  43. }
  44. static struct vm_special_mapping vdso_text_mapping __ro_after_init = {
  45. .name = "[vdso]",
  46. .mremap = vdso_mremap,
  47. };
  48. struct elfinfo {
  49. Elf32_Ehdr *hdr; /* ptr to ELF */
  50. Elf32_Sym *dynsym; /* ptr to .dynsym section */
  51. unsigned long dynsymsize; /* size of .dynsym section */
  52. char *dynstr; /* ptr to .dynstr section */
  53. };
  54. /* Cached result of boot-time check for whether the arch timer exists,
  55. * and if so, whether the virtual counter is useable.
  56. */
  57. bool cntvct_ok __ro_after_init;
  58. static bool __init cntvct_functional(void)
  59. {
  60. struct device_node *np;
  61. bool ret = false;
  62. if (!IS_ENABLED(CONFIG_ARM_ARCH_TIMER))
  63. goto out;
  64. /* The arm_arch_timer core should export
  65. * arch_timer_use_virtual or similar so we don't have to do
  66. * this.
  67. */
  68. np = of_find_compatible_node(NULL, NULL, "arm,armv7-timer");
  69. if (!np)
  70. np = of_find_compatible_node(NULL, NULL, "arm,armv8-timer");
  71. if (!np)
  72. goto out_put;
  73. if (of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
  74. goto out_put;
  75. ret = true;
  76. out_put:
  77. of_node_put(np);
  78. out:
  79. return ret;
  80. }
  81. static void * __init find_section(Elf32_Ehdr *ehdr, const char *name,
  82. unsigned long *size)
  83. {
  84. Elf32_Shdr *sechdrs;
  85. unsigned int i;
  86. char *secnames;
  87. /* Grab section headers and strings so we can tell who is who */
  88. sechdrs = (void *)ehdr + ehdr->e_shoff;
  89. secnames = (void *)ehdr + sechdrs[ehdr->e_shstrndx].sh_offset;
  90. /* Find the section they want */
  91. for (i = 1; i < ehdr->e_shnum; i++) {
  92. if (strcmp(secnames + sechdrs[i].sh_name, name) == 0) {
  93. if (size)
  94. *size = sechdrs[i].sh_size;
  95. return (void *)ehdr + sechdrs[i].sh_offset;
  96. }
  97. }
  98. if (size)
  99. *size = 0;
  100. return NULL;
  101. }
  102. static Elf32_Sym * __init find_symbol(struct elfinfo *lib, const char *symname)
  103. {
  104. unsigned int i;
  105. for (i = 0; i < (lib->dynsymsize / sizeof(Elf32_Sym)); i++) {
  106. char name[MAX_SYMNAME], *c;
  107. if (lib->dynsym[i].st_name == 0)
  108. continue;
  109. strscpy(name, lib->dynstr + lib->dynsym[i].st_name,
  110. MAX_SYMNAME);
  111. c = strchr(name, '@');
  112. if (c)
  113. *c = 0;
  114. if (strcmp(symname, name) == 0)
  115. return &lib->dynsym[i];
  116. }
  117. return NULL;
  118. }
  119. static void __init vdso_nullpatch_one(struct elfinfo *lib, const char *symname)
  120. {
  121. Elf32_Sym *sym;
  122. sym = find_symbol(lib, symname);
  123. if (!sym)
  124. return;
  125. sym->st_name = 0;
  126. }
  127. static void __init patch_vdso(void *ehdr)
  128. {
  129. struct elfinfo einfo;
  130. einfo = (struct elfinfo) {
  131. .hdr = ehdr,
  132. };
  133. einfo.dynsym = find_section(einfo.hdr, ".dynsym", &einfo.dynsymsize);
  134. einfo.dynstr = find_section(einfo.hdr, ".dynstr", NULL);
  135. /* If the virtual counter is absent or non-functional we don't
  136. * want programs to incur the slight additional overhead of
  137. * dispatching through the VDSO only to fall back to syscalls.
  138. */
  139. if (!cntvct_ok) {
  140. vdso_nullpatch_one(&einfo, "__vdso_gettimeofday");
  141. vdso_nullpatch_one(&einfo, "__vdso_clock_gettime");
  142. vdso_nullpatch_one(&einfo, "__vdso_clock_gettime64");
  143. }
  144. }
  145. static int __init vdso_init(void)
  146. {
  147. unsigned int text_pages;
  148. int i;
  149. if (memcmp(vdso_start, "\177ELF", 4)) {
  150. pr_err("VDSO is not a valid ELF object!\n");
  151. return -ENOEXEC;
  152. }
  153. text_pages = (vdso_end - vdso_start) >> PAGE_SHIFT;
  154. /* Allocate the VDSO text pagelist */
  155. vdso_text_pagelist = kcalloc(text_pages, sizeof(struct page *),
  156. GFP_KERNEL);
  157. if (vdso_text_pagelist == NULL)
  158. return -ENOMEM;
  159. /* Grab the VDSO data page. */
  160. vdso_data_page = virt_to_page(vdso_data);
  161. /* Grab the VDSO text pages. */
  162. for (i = 0; i < text_pages; i++) {
  163. struct page *page;
  164. page = virt_to_page(vdso_start + i * PAGE_SIZE);
  165. vdso_text_pagelist[i] = page;
  166. }
  167. vdso_text_mapping.pages = vdso_text_pagelist;
  168. vdso_total_pages = 1; /* for the data/vvar page */
  169. vdso_total_pages += text_pages;
  170. cntvct_ok = cntvct_functional();
  171. patch_vdso(vdso_start);
  172. return 0;
  173. }
  174. arch_initcall(vdso_init);
  175. static int install_vvar(struct mm_struct *mm, unsigned long addr)
  176. {
  177. struct vm_area_struct *vma;
  178. vma = _install_special_mapping(mm, addr, PAGE_SIZE,
  179. VM_READ | VM_MAYREAD,
  180. &vdso_data_mapping);
  181. return PTR_ERR_OR_ZERO(vma);
  182. }
  183. /* assumes mmap_lock is write-locked */
  184. void arm_install_vdso(struct mm_struct *mm, unsigned long addr)
  185. {
  186. struct vm_area_struct *vma;
  187. unsigned long len;
  188. mm->context.vdso = 0;
  189. if (vdso_text_pagelist == NULL)
  190. return;
  191. if (install_vvar(mm, addr))
  192. return;
  193. /* Account for vvar page. */
  194. addr += PAGE_SIZE;
  195. len = (vdso_total_pages - 1) << PAGE_SHIFT;
  196. vma = _install_special_mapping(mm, addr, len,
  197. VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
  198. &vdso_text_mapping);
  199. if (!IS_ERR(vma))
  200. mm->context.vdso = addr;
  201. }