drm/panel: samsung-s6e88a0-ams452ef01: transition to mipi_dsi wrapped functions
[drm/drm-misc.git] / arch / arm / kernel / vdso.c
blob29dd2f3c62fec64c7f290468421bfad1e739c667
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Adapted from arm64 version.
5 * Copyright (C) 2012 ARM Limited
6 * Copyright (C) 2015 Mentor Graphics Corporation.
7 */
9 #include <linux/cache.h>
10 #include <linux/elf.h>
11 #include <linux/err.h>
12 #include <linux/kernel.h>
13 #include <linux/mm.h>
14 #include <linux/of.h>
15 #include <linux/printk.h>
16 #include <linux/slab.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>
27 #define MAX_SYMNAME 64
29 static struct page **vdso_text_pagelist;
31 extern char vdso_start[], vdso_end[];
33 /* Total number of pages needed for the data and text portions of the VDSO. */
34 unsigned int vdso_total_pages __ro_after_init;
36 static union vdso_data_store vdso_data_store __page_aligned_data;
37 struct vdso_data *vdso_data = vdso_data_store.data;
39 static struct page *vdso_data_page __ro_after_init;
40 static const struct vm_special_mapping vdso_data_mapping = {
41 .name = "[vvar]",
42 .pages = &vdso_data_page,
45 static int vdso_mremap(const struct vm_special_mapping *sm,
46 struct vm_area_struct *new_vma)
48 current->mm->context.vdso = new_vma->vm_start;
50 return 0;
53 static struct vm_special_mapping vdso_text_mapping __ro_after_init = {
54 .name = "[vdso]",
55 .mremap = vdso_mremap,
58 struct elfinfo {
59 Elf32_Ehdr *hdr; /* ptr to ELF */
60 Elf32_Sym *dynsym; /* ptr to .dynsym section */
61 unsigned long dynsymsize; /* size of .dynsym section */
62 char *dynstr; /* ptr to .dynstr section */
65 /* Cached result of boot-time check for whether the arch timer exists,
66 * and if so, whether the virtual counter is useable.
68 bool cntvct_ok __ro_after_init;
70 static bool __init cntvct_functional(void)
72 struct device_node *np;
73 bool ret = false;
75 if (!IS_ENABLED(CONFIG_ARM_ARCH_TIMER))
76 goto out;
78 /* The arm_arch_timer core should export
79 * arch_timer_use_virtual or similar so we don't have to do
80 * this.
82 np = of_find_compatible_node(NULL, NULL, "arm,armv7-timer");
83 if (!np)
84 np = of_find_compatible_node(NULL, NULL, "arm,armv8-timer");
85 if (!np)
86 goto out_put;
88 if (of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
89 goto out_put;
91 ret = true;
93 out_put:
94 of_node_put(np);
95 out:
96 return ret;
99 static void * __init find_section(Elf32_Ehdr *ehdr, const char *name,
100 unsigned long *size)
102 Elf32_Shdr *sechdrs;
103 unsigned int i;
104 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;
110 /* Find the section they want */
111 for (i = 1; i < ehdr->e_shnum; i++) {
112 if (strcmp(secnames + sechdrs[i].sh_name, name) == 0) {
113 if (size)
114 *size = sechdrs[i].sh_size;
115 return (void *)ehdr + sechdrs[i].sh_offset;
119 if (size)
120 *size = 0;
121 return NULL;
124 static Elf32_Sym * __init find_symbol(struct elfinfo *lib, const char *symname)
126 unsigned int i;
128 for (i = 0; i < (lib->dynsymsize / sizeof(Elf32_Sym)); i++) {
129 char name[MAX_SYMNAME], *c;
131 if (lib->dynsym[i].st_name == 0)
132 continue;
133 strscpy(name, lib->dynstr + lib->dynsym[i].st_name,
134 MAX_SYMNAME);
135 c = strchr(name, '@');
136 if (c)
137 *c = 0;
138 if (strcmp(symname, name) == 0)
139 return &lib->dynsym[i];
141 return NULL;
144 static void __init vdso_nullpatch_one(struct elfinfo *lib, const char *symname)
146 Elf32_Sym *sym;
148 sym = find_symbol(lib, symname);
149 if (!sym)
150 return;
152 sym->st_name = 0;
155 static void __init patch_vdso(void *ehdr)
157 struct elfinfo einfo;
159 einfo = (struct elfinfo) {
160 .hdr = ehdr,
163 einfo.dynsym = find_section(einfo.hdr, ".dynsym", &einfo.dynsymsize);
164 einfo.dynstr = find_section(einfo.hdr, ".dynstr", NULL);
166 /* If the virtual counter is absent or non-functional we don't
167 * want programs to incur the slight additional overhead of
168 * dispatching through the VDSO only to fall back to syscalls.
170 if (!cntvct_ok) {
171 vdso_nullpatch_one(&einfo, "__vdso_gettimeofday");
172 vdso_nullpatch_one(&einfo, "__vdso_clock_gettime");
173 vdso_nullpatch_one(&einfo, "__vdso_clock_gettime64");
177 static int __init vdso_init(void)
179 unsigned int text_pages;
180 int i;
182 if (memcmp(vdso_start, "\177ELF", 4)) {
183 pr_err("VDSO is not a valid ELF object!\n");
184 return -ENOEXEC;
187 text_pages = (vdso_end - vdso_start) >> PAGE_SHIFT;
189 /* Allocate the VDSO text pagelist */
190 vdso_text_pagelist = kcalloc(text_pages, sizeof(struct page *),
191 GFP_KERNEL);
192 if (vdso_text_pagelist == NULL)
193 return -ENOMEM;
195 /* Grab the VDSO data page. */
196 vdso_data_page = virt_to_page(vdso_data);
198 /* Grab the VDSO text pages. */
199 for (i = 0; i < text_pages; i++) {
200 struct page *page;
202 page = virt_to_page(vdso_start + i * PAGE_SIZE);
203 vdso_text_pagelist[i] = page;
206 vdso_text_mapping.pages = vdso_text_pagelist;
208 vdso_total_pages = 1; /* for the data/vvar page */
209 vdso_total_pages += text_pages;
211 cntvct_ok = cntvct_functional();
213 patch_vdso(vdso_start);
215 return 0;
217 arch_initcall(vdso_init);
219 static int install_vvar(struct mm_struct *mm, unsigned long addr)
221 struct vm_area_struct *vma;
223 vma = _install_special_mapping(mm, addr, PAGE_SIZE,
224 VM_READ | VM_MAYREAD,
225 &vdso_data_mapping);
227 return PTR_ERR_OR_ZERO(vma);
230 /* assumes mmap_lock is write-locked */
231 void arm_install_vdso(struct mm_struct *mm, unsigned long addr)
233 struct vm_area_struct *vma;
234 unsigned long len;
236 mm->context.vdso = 0;
238 if (vdso_text_pagelist == NULL)
239 return;
241 if (install_vvar(mm, addr))
242 return;
244 /* Account for vvar page. */
245 addr += PAGE_SIZE;
246 len = (vdso_total_pages - 1) << PAGE_SHIFT;
248 vma = _install_special_mapping(mm, addr, len,
249 VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
250 &vdso_text_mapping);
252 if (!IS_ERR(vma))
253 mm->context.vdso = addr;