File Index
Files and directories
Sign in
FILE

vdso.c

File preview with syntax highlighting.

linux-6.19.14/arch/arm/kernel/vdso.c C
  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
  9#include <linux/cache.h>
 10#include <linux/vdso_datastore.h>
 11#include <linux/elf.h>
 12#include <linux/err.h>
 13#include <linux/kernel.h>
 14#include <linux/mm.h>
 15#include <linux/of.h>
 16#include <linux/printk.h>
 17#include <linux/slab.h>
 18#include <linux/vmalloc.h>
 19#include <asm/arch_timer.h>
 20#include <asm/barrier.h>
 21#include <asm/cacheflush.h>
 22#include <asm/page.h>
 23#include <asm/vdso.h>
 24#include <clocksource/arm_arch_timer.h>
 25#include <vdso/helpers.h>
 26#include <vdso/vsyscall.h>
 27
 28#define MAX_SYMNAME	64
 29
 30static struct page **vdso_text_pagelist;
 31
 32extern char vdso_start[], vdso_end[];
 33
 34/* Total number of pages needed for the data and text portions of the VDSO. */
 35unsigned int vdso_total_pages __ro_after_init;
 36
 37static int vdso_mremap(const struct vm_special_mapping *sm,
 38		struct vm_area_struct *new_vma)
 39{
 40	current->mm->context.vdso = new_vma->vm_start;
 41
 42	return 0;
 43}
 44
 45static struct vm_special_mapping vdso_text_mapping __ro_after_init = {
 46	.name = "[vdso]",
 47	.mremap = vdso_mremap,
 48};
 49
 50struct elfinfo {
 51	Elf32_Ehdr	*hdr;		/* ptr to ELF */
 52	Elf32_Sym	*dynsym;	/* ptr to .dynsym section */
 53	unsigned long	dynsymsize;	/* size of .dynsym section */
 54	char		*dynstr;	/* ptr to .dynstr section */
 55};
 56
 57/* Boot-time check for whether the arch timer exists, and if so,
 58 * whether the virtual counter is usable.
 59 */
 60static bool __init cntvct_functional(void)
 61{
 62	struct device_node *np;
 63	bool ret = false;
 64
 65	if (!IS_ENABLED(CONFIG_ARM_ARCH_TIMER))
 66		goto out;
 67
 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
 78	if (of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
 79		goto out_put;
 80
 81	ret = true;
 82
 83out_put:
 84	of_node_put(np);
 85out:
 86	return ret;
 87}
 88
 89static void * __init find_section(Elf32_Ehdr *ehdr, const char *name,
 90				  unsigned long *size)
 91{
 92	Elf32_Shdr *sechdrs;
 93	unsigned int i;
 94	char *secnames;
 95
 96	/* Grab section headers and strings so we can tell who is who */
 97	sechdrs = (void *)ehdr + ehdr->e_shoff;
 98	secnames = (void *)ehdr + sechdrs[ehdr->e_shstrndx].sh_offset;
 99
100	/* Find the section they want */
101	for (i = 1; i < ehdr->e_shnum; i++) {
102		if (strcmp(secnames + sechdrs[i].sh_name, name) == 0) {
103			if (size)
104				*size = sechdrs[i].sh_size;
105			return (void *)ehdr + sechdrs[i].sh_offset;
106		}
107	}
108
109	if (size)
110		*size = 0;
111	return NULL;
112}
113
114static Elf32_Sym * __init find_symbol(struct elfinfo *lib, const char *symname)
115{
116	unsigned int i;
117
118	for (i = 0; i < (lib->dynsymsize / sizeof(Elf32_Sym)); i++) {
119		char name[MAX_SYMNAME], *c;
120
121		if (lib->dynsym[i].st_name == 0)
122			continue;
123		strscpy(name, lib->dynstr + lib->dynsym[i].st_name,
124			MAX_SYMNAME);
125		c = strchr(name, '@');
126		if (c)
127			*c = 0;
128		if (strcmp(symname, name) == 0)
129			return &lib->dynsym[i];
130	}
131	return NULL;
132}
133
134static void __init vdso_nullpatch_one(struct elfinfo *lib, const char *symname)
135{
136	Elf32_Sym *sym;
137
138	sym = find_symbol(lib, symname);
139	if (!sym)
140		return;
141
142	sym->st_name = 0;
143}
144
145static void __init patch_vdso(void *ehdr)
146{
147	struct elfinfo einfo;
148
149	einfo = (struct elfinfo) {
150		.hdr = ehdr,
151	};
152
153	einfo.dynsym = find_section(einfo.hdr, ".dynsym", &einfo.dynsymsize);
154	einfo.dynstr = find_section(einfo.hdr, ".dynstr", NULL);
155
156	/* If the virtual counter is absent or non-functional we don't
157	 * want programs to incur the slight additional overhead of
158	 * dispatching through the VDSO only to fall back to syscalls.
159	 */
160	if (!cntvct_functional()) {
161		vdso_nullpatch_one(&einfo, "__vdso_gettimeofday");
162		vdso_nullpatch_one(&einfo, "__vdso_clock_gettime");
163		vdso_nullpatch_one(&einfo, "__vdso_clock_gettime64");
164		vdso_nullpatch_one(&einfo, "__vdso_clock_getres");
165	}
166}
167
168static int __init vdso_init(void)
169{
170	unsigned int text_pages;
171	int i;
172
173	if (memcmp(vdso_start, "\177ELF", 4)) {
174		pr_err("VDSO is not a valid ELF object!\n");
175		return -ENOEXEC;
176	}
177
178	text_pages = (vdso_end - vdso_start) >> PAGE_SHIFT;
179
180	/* Allocate the VDSO text pagelist */
181	vdso_text_pagelist = kcalloc(text_pages, sizeof(struct page *),
182				     GFP_KERNEL);
183	if (vdso_text_pagelist == NULL)
184		return -ENOMEM;
185
186	/* Grab the VDSO text pages. */
187	for (i = 0; i < text_pages; i++) {
188		struct page *page;
189
190		page = virt_to_page(vdso_start + i * PAGE_SIZE);
191		vdso_text_pagelist[i] = page;
192	}
193
194	vdso_text_mapping.pages = vdso_text_pagelist;
195
196	vdso_total_pages = VDSO_NR_PAGES; /* for the data/vvar pages */
197	vdso_total_pages += text_pages;
198
199	patch_vdso(vdso_start);
200
201	return 0;
202}
203arch_initcall(vdso_init);
204
205static_assert(__VDSO_PAGES == VDSO_NR_PAGES);
206
207/* assumes mmap_lock is write-locked */
208void arm_install_vdso(struct mm_struct *mm, unsigned long addr)
209{
210	struct vm_area_struct *vma;
211	unsigned long len;
212
213	mm->context.vdso = 0;
214
215	if (vdso_text_pagelist == NULL)
216		return;
217
218	if (IS_ERR(vdso_install_vvar_mapping(mm, addr)))
219		return;
220
221	/* Account for vvar pages. */
222	addr += VDSO_NR_PAGES * PAGE_SIZE;
223	len = (vdso_total_pages - VDSO_NR_PAGES) << PAGE_SHIFT;
224
225	vma = _install_special_mapping(mm, addr, len,
226		VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
227		&vdso_text_mapping);
228
229	if (!IS_ERR(vma))
230		mm->context.vdso = addr;
231}
232
~/ Read-only file preview.