1 // SPDX-License-Identifier: GPL-2.0-only
3 * Adapted from arm64 version.
5 * Copyright (C) 2012 ARM Limited
6 * Copyright (C) 2015 Mentor Graphics Corporation.
9 #include <linux/cache.h>
10 #include <linux/elf.h>
11 #include <linux/err.h>
12 #include <linux/kernel.h>
15 #include <linux/printk.h>
16 #include <linux/slab.h>
17 #include <linux/timekeeper_internal.h>
18 #include <linux/vmalloc.h>
19 #include <asm/arch_timer.h>
20 #include <asm/barrier.h>
21 #include <asm/cacheflush.h>
24 #include <clocksource/arm_arch_timer.h>
25 #include <vdso/helpers.h>
26 #include <vdso/vsyscall.h>
28 #define MAX_SYMNAME 64
30 static struct page **vdso_text_pagelist;
32 extern char vdso_start[], vdso_end[];
34 /* Total number of pages needed for the data and text portions of the VDSO. */
35 unsigned int vdso_total_pages __ro_after_init;
37 static union vdso_data_store vdso_data_store __page_aligned_data;
38 struct vdso_data *vdso_data = vdso_data_store.data;
40 static struct page *vdso_data_page __ro_after_init;
41 static const struct vm_special_mapping vdso_data_mapping = {
43 .pages = &vdso_data_page,
46 static int vdso_mremap(const struct vm_special_mapping *sm,
47 struct vm_area_struct *new_vma)
49 current->mm->context.vdso = new_vma->vm_start;
54 static struct vm_special_mapping vdso_text_mapping __ro_after_init = {
56 .mremap = vdso_mremap,
60 Elf32_Ehdr *hdr; /* ptr to ELF */
61 Elf32_Sym *dynsym; /* ptr to .dynsym section */
62 unsigned long dynsymsize; /* size of .dynsym section */
63 char *dynstr; /* ptr to .dynstr section */
66 /* Cached result of boot-time check for whether the arch timer exists,
67 * and if so, whether the virtual counter is useable.
69 bool cntvct_ok __ro_after_init;
71 static bool __init cntvct_functional(void)
73 struct device_node *np;
76 if (!IS_ENABLED(CONFIG_ARM_ARCH_TIMER))
79 /* The arm_arch_timer core should export
80 * arch_timer_use_virtual or similar so we don't have to do
83 np = of_find_compatible_node(NULL, NULL, "arm,armv7-timer");
85 np = of_find_compatible_node(NULL, NULL, "arm,armv8-timer");
89 if (of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
100 static void * __init find_section(Elf32_Ehdr *ehdr, const char *name,
107 /* Grab section headers and strings so we can tell who is who */
108 sechdrs = (void *)ehdr + ehdr->e_shoff;
109 secnames = (void *)ehdr + sechdrs[ehdr->e_shstrndx].sh_offset;
111 /* Find the section they want */
112 for (i = 1; i < ehdr->e_shnum; i++) {
113 if (strcmp(secnames + sechdrs[i].sh_name, name) == 0) {
115 *size = sechdrs[i].sh_size;
116 return (void *)ehdr + sechdrs[i].sh_offset;
125 static Elf32_Sym * __init find_symbol(struct elfinfo *lib, const char *symname)
129 for (i = 0; i < (lib->dynsymsize / sizeof(Elf32_Sym)); i++) {
130 char name[MAX_SYMNAME], *c;
132 if (lib->dynsym[i].st_name == 0)
134 strscpy(name, lib->dynstr + lib->dynsym[i].st_name,
136 c = strchr(name, '@');
139 if (strcmp(symname, name) == 0)
140 return &lib->dynsym[i];
145 static void __init vdso_nullpatch_one(struct elfinfo *lib, const char *symname)
149 sym = find_symbol(lib, symname);
156 static void __init patch_vdso(void *ehdr)
158 struct elfinfo einfo;
160 einfo = (struct elfinfo) {
164 einfo.dynsym = find_section(einfo.hdr, ".dynsym", &einfo.dynsymsize);
165 einfo.dynstr = find_section(einfo.hdr, ".dynstr", NULL);
167 /* If the virtual counter is absent or non-functional we don't
168 * want programs to incur the slight additional overhead of
169 * dispatching through the VDSO only to fall back to syscalls.
172 vdso_nullpatch_one(&einfo, "__vdso_gettimeofday");
173 vdso_nullpatch_one(&einfo, "__vdso_clock_gettime");
174 vdso_nullpatch_one(&einfo, "__vdso_clock_gettime64");
178 static int __init vdso_init(void)
180 unsigned int text_pages;
183 if (memcmp(vdso_start, "\177ELF", 4)) {
184 pr_err("VDSO is not a valid ELF object!\n");
188 text_pages = (vdso_end - vdso_start) >> PAGE_SHIFT;
190 /* Allocate the VDSO text pagelist */
191 vdso_text_pagelist = kcalloc(text_pages, sizeof(struct page *),
193 if (vdso_text_pagelist == NULL)
196 /* Grab the VDSO data page. */
197 vdso_data_page = virt_to_page(vdso_data);
199 /* Grab the VDSO text pages. */
200 for (i = 0; i < text_pages; i++) {
203 page = virt_to_page(vdso_start + i * PAGE_SIZE);
204 vdso_text_pagelist[i] = page;
207 vdso_text_mapping.pages = vdso_text_pagelist;
209 vdso_total_pages = 1; /* for the data/vvar page */
210 vdso_total_pages += text_pages;
212 cntvct_ok = cntvct_functional();
214 patch_vdso(vdso_start);
218 arch_initcall(vdso_init);
220 static int install_vvar(struct mm_struct *mm, unsigned long addr)
222 struct vm_area_struct *vma;
224 vma = _install_special_mapping(mm, addr, PAGE_SIZE,
225 VM_READ | VM_MAYREAD,
228 return PTR_ERR_OR_ZERO(vma);
231 /* assumes mmap_lock is write-locked */
232 void arm_install_vdso(struct mm_struct *mm, unsigned long addr)
234 struct vm_area_struct *vma;
237 mm->context.vdso = 0;
239 if (vdso_text_pagelist == NULL)
242 if (install_vvar(mm, addr))
245 /* Account for vvar page. */
247 len = (vdso_total_pages - 1) << PAGE_SHIFT;
249 vma = _install_special_mapping(mm, addr, len,
250 VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
254 mm->context.vdso = addr;