Linux Audio

Check our new training course

In-person Linux kernel drivers training

Jun 16-20, 2025
Register
Loading...
v4.6
 
  1/*
  2 * Adapted from arm64 version.
  3 *
  4 * Copyright (C) 2012 ARM Limited
  5 * Copyright (C) 2015 Mentor Graphics Corporation.
  6 *
  7 * This program is free software; you can redistribute it and/or modify
  8 * it under the terms of the GNU General Public License version 2 as
  9 * published by the Free Software Foundation.
 10 *
 11 * This program is distributed in the hope that it will be useful,
 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14 * GNU General Public License for more details.
 15 *
 16 * You should have received a copy of the GNU General Public License
 17 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 18 */
 19
 
 20#include <linux/elf.h>
 21#include <linux/err.h>
 22#include <linux/kernel.h>
 23#include <linux/mm.h>
 24#include <linux/of.h>
 25#include <linux/printk.h>
 26#include <linux/slab.h>
 27#include <linux/timekeeper_internal.h>
 28#include <linux/vmalloc.h>
 29#include <asm/arch_timer.h>
 30#include <asm/barrier.h>
 31#include <asm/cacheflush.h>
 32#include <asm/page.h>
 33#include <asm/vdso.h>
 34#include <asm/vdso_datapage.h>
 35#include <clocksource/arm_arch_timer.h>
 
 
 36
 37#define MAX_SYMNAME	64
 38
 39static struct page **vdso_text_pagelist;
 40
 
 
 41/* Total number of pages needed for the data and text portions of the VDSO. */
 42unsigned int vdso_total_pages __read_mostly;
 43
 44/*
 45 * The VDSO data page.
 46 */
 47static union vdso_data_store vdso_data_store __page_aligned_data;
 48static struct vdso_data *vdso_data = &vdso_data_store.data;
 49
 50static struct page *vdso_data_page;
 51static struct vm_special_mapping vdso_data_mapping = {
 52	.name = "[vvar]",
 53	.pages = &vdso_data_page,
 54};
 55
 56static struct vm_special_mapping vdso_text_mapping = {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 57	.name = "[vdso]",
 
 58};
 59
 60struct elfinfo {
 61	Elf32_Ehdr	*hdr;		/* ptr to ELF */
 62	Elf32_Sym	*dynsym;	/* ptr to .dynsym section */
 63	unsigned long	dynsymsize;	/* size of .dynsym section */
 64	char		*dynstr;	/* ptr to .dynstr section */
 65};
 66
 67/* Cached result of boot-time check for whether the arch timer exists,
 68 * and if so, whether the virtual counter is useable.
 69 */
 70static bool cntvct_ok __read_mostly;
 71
 72static bool __init cntvct_functional(void)
 73{
 74	struct device_node *np;
 75	bool ret = false;
 76
 77	if (!IS_ENABLED(CONFIG_ARM_ARCH_TIMER))
 78		goto out;
 79
 80	/* The arm_arch_timer core should export
 81	 * arch_timer_use_virtual or similar so we don't have to do
 82	 * this.
 83	 */
 84	np = of_find_compatible_node(NULL, NULL, "arm,armv7-timer");
 85	if (!np)
 
 
 86		goto out_put;
 87
 88	if (of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
 89		goto out_put;
 90
 91	ret = true;
 92
 93out_put:
 94	of_node_put(np);
 95out:
 96	return ret;
 97}
 98
 99static void * __init find_section(Elf32_Ehdr *ehdr, const char *name,
100				  unsigned long *size)
101{
102	Elf32_Shdr *sechdrs;
103	unsigned int i;
104	char *secnames;
105
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;
109
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;
116		}
117	}
118
119	if (size)
120		*size = 0;
121	return NULL;
122}
123
124static Elf32_Sym * __init find_symbol(struct elfinfo *lib, const char *symname)
125{
126	unsigned int i;
127
128	for (i = 0; i < (lib->dynsymsize / sizeof(Elf32_Sym)); i++) {
129		char name[MAX_SYMNAME], *c;
130
131		if (lib->dynsym[i].st_name == 0)
132			continue;
133		strlcpy(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];
140	}
141	return NULL;
142}
143
144static void __init vdso_nullpatch_one(struct elfinfo *lib, const char *symname)
145{
146	Elf32_Sym *sym;
147
148	sym = find_symbol(lib, symname);
149	if (!sym)
150		return;
151
152	sym->st_name = 0;
153}
154
155static void __init patch_vdso(void *ehdr)
156{
157	struct elfinfo einfo;
158
159	einfo = (struct elfinfo) {
160		.hdr = ehdr,
161	};
162
163	einfo.dynsym = find_section(einfo.hdr, ".dynsym", &einfo.dynsymsize);
164	einfo.dynstr = find_section(einfo.hdr, ".dynstr", NULL);
165
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.
169	 */
170	if (!cntvct_ok) {
171		vdso_nullpatch_one(&einfo, "__vdso_gettimeofday");
172		vdso_nullpatch_one(&einfo, "__vdso_clock_gettime");
 
173	}
174}
175
176static int __init vdso_init(void)
177{
178	unsigned int text_pages;
179	int i;
180
181	if (memcmp(&vdso_start, "\177ELF", 4)) {
182		pr_err("VDSO is not a valid ELF object!\n");
183		return -ENOEXEC;
184	}
185
186	text_pages = (&vdso_end - &vdso_start) >> PAGE_SHIFT;
187	pr_debug("vdso: %i text pages at base %p\n", text_pages, &vdso_start);
188
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;
194
195	/* Grab the VDSO data page. */
196	vdso_data_page = virt_to_page(vdso_data);
197
198	/* Grab the VDSO text pages. */
199	for (i = 0; i < text_pages; i++) {
200		struct page *page;
201
202		page = virt_to_page(&vdso_start + i * PAGE_SIZE);
203		vdso_text_pagelist[i] = page;
204	}
205
206	vdso_text_mapping.pages = vdso_text_pagelist;
207
208	vdso_total_pages = 1; /* for the data/vvar page */
209	vdso_total_pages += text_pages;
210
211	cntvct_ok = cntvct_functional();
212
213	patch_vdso(&vdso_start);
214
215	return 0;
216}
217arch_initcall(vdso_init);
218
219static int install_vvar(struct mm_struct *mm, unsigned long addr)
220{
221	struct vm_area_struct *vma;
222
223	vma = _install_special_mapping(mm, addr, PAGE_SIZE,
224				       VM_READ | VM_MAYREAD,
225				       &vdso_data_mapping);
226
227	return PTR_ERR_OR_ZERO(vma);
228}
229
230/* assumes mmap_sem is write-locked */
231void arm_install_vdso(struct mm_struct *mm, unsigned long addr)
232{
233	struct vm_area_struct *vma;
234	unsigned long len;
235
236	mm->context.vdso = 0;
237
238	if (vdso_text_pagelist == NULL)
239		return;
240
241	if (install_vvar(mm, addr))
242		return;
243
244	/* Account for vvar page. */
245	addr += PAGE_SIZE;
246	len = (vdso_total_pages - 1) << PAGE_SHIFT;
247
248	vma = _install_special_mapping(mm, addr, len,
249		VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
250		&vdso_text_mapping);
251
252	if (!IS_ERR(vma))
253		mm->context.vdso = addr;
254}
255
256static void vdso_write_begin(struct vdso_data *vdata)
257{
258	++vdso_data->seq_count;
259	smp_wmb(); /* Pairs with smp_rmb in vdso_read_retry */
260}
261
262static void vdso_write_end(struct vdso_data *vdata)
263{
264	smp_wmb(); /* Pairs with smp_rmb in vdso_read_begin */
265	++vdso_data->seq_count;
266}
267
268static bool tk_is_cntvct(const struct timekeeper *tk)
269{
270	if (!IS_ENABLED(CONFIG_ARM_ARCH_TIMER))
271		return false;
272
273	if (strcmp(tk->tkr_mono.clock->name, "arch_sys_counter") != 0)
274		return false;
275
276	return true;
277}
278
279/**
280 * update_vsyscall - update the vdso data page
281 *
282 * Increment the sequence counter, making it odd, indicating to
283 * userspace that an update is in progress.  Update the fields used
284 * for coarse clocks and, if the architected system timer is in use,
285 * the fields used for high precision clocks.  Increment the sequence
286 * counter again, making it even, indicating to userspace that the
287 * update is finished.
288 *
289 * Userspace is expected to sample seq_count before reading any other
290 * fields from the data page.  If seq_count is odd, userspace is
291 * expected to wait until it becomes even.  After copying data from
292 * the page, userspace must sample seq_count again; if it has changed
293 * from its previous value, userspace must retry the whole sequence.
294 *
295 * Calls to update_vsyscall are serialized by the timekeeping core.
296 */
297void update_vsyscall(struct timekeeper *tk)
298{
299	struct timespec64 *wtm = &tk->wall_to_monotonic;
300
301	if (!cntvct_ok) {
302		/* The entry points have been zeroed, so there is no
303		 * point in updating the data page.
304		 */
305		return;
306	}
307
308	vdso_write_begin(vdso_data);
309
310	vdso_data->tk_is_cntvct			= tk_is_cntvct(tk);
311	vdso_data->xtime_coarse_sec		= tk->xtime_sec;
312	vdso_data->xtime_coarse_nsec		= (u32)(tk->tkr_mono.xtime_nsec >>
313							tk->tkr_mono.shift);
314	vdso_data->wtm_clock_sec		= wtm->tv_sec;
315	vdso_data->wtm_clock_nsec		= wtm->tv_nsec;
316
317	if (vdso_data->tk_is_cntvct) {
318		vdso_data->cs_cycle_last	= tk->tkr_mono.cycle_last;
319		vdso_data->xtime_clock_sec	= tk->xtime_sec;
320		vdso_data->xtime_clock_snsec	= tk->tkr_mono.xtime_nsec;
321		vdso_data->cs_mult		= tk->tkr_mono.mult;
322		vdso_data->cs_shift		= tk->tkr_mono.shift;
323		vdso_data->cs_mask		= tk->tkr_mono.mask;
324	}
325
326	vdso_write_end(vdso_data);
327
328	flush_dcache_page(virt_to_page(vdso_data));
329}
330
331void update_vsyscall_tz(void)
332{
333	vdso_data->tz_minuteswest	= sys_tz.tz_minuteswest;
334	vdso_data->tz_dsttime		= sys_tz.tz_dsttime;
335	flush_dcache_page(virt_to_page(vdso_data));
336}
v5.9
  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/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/timekeeper_internal.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 <asm/vdso_datapage.h>
 25#include <clocksource/arm_arch_timer.h>
 26#include <vdso/helpers.h>
 27#include <vdso/vsyscall.h>
 28
 29#define MAX_SYMNAME	64
 30
 31static struct page **vdso_text_pagelist;
 32
 33extern char vdso_start[], vdso_end[];
 34
 35/* Total number of pages needed for the data and text portions of the VDSO. */
 36unsigned int vdso_total_pages __ro_after_init;
 37
 38/*
 39 * The VDSO data page.
 40 */
 41static union vdso_data_store vdso_data_store __page_aligned_data;
 42struct vdso_data *vdso_data = vdso_data_store.data;
 43
 44static struct page *vdso_data_page __ro_after_init;
 45static const struct vm_special_mapping vdso_data_mapping = {
 46	.name = "[vvar]",
 47	.pages = &vdso_data_page,
 48};
 49
 50static int vdso_mremap(const struct vm_special_mapping *sm,
 51		struct vm_area_struct *new_vma)
 52{
 53	unsigned long new_size = new_vma->vm_end - new_vma->vm_start;
 54	unsigned long vdso_size;
 55
 56	/* without VVAR page */
 57	vdso_size = (vdso_total_pages - 1) << PAGE_SHIFT;
 58
 59	if (vdso_size != new_size)
 60		return -EINVAL;
 61
 62	current->mm->context.vdso = new_vma->vm_start;
 63
 64	return 0;
 65}
 66
 67static struct vm_special_mapping vdso_text_mapping __ro_after_init = {
 68	.name = "[vdso]",
 69	.mremap = vdso_mremap,
 70};
 71
 72struct elfinfo {
 73	Elf32_Ehdr	*hdr;		/* ptr to ELF */
 74	Elf32_Sym	*dynsym;	/* ptr to .dynsym section */
 75	unsigned long	dynsymsize;	/* size of .dynsym section */
 76	char		*dynstr;	/* ptr to .dynstr section */
 77};
 78
 79/* Cached result of boot-time check for whether the arch timer exists,
 80 * and if so, whether the virtual counter is useable.
 81 */
 82bool cntvct_ok __ro_after_init;
 83
 84static bool __init cntvct_functional(void)
 85{
 86	struct device_node *np;
 87	bool ret = false;
 88
 89	if (!IS_ENABLED(CONFIG_ARM_ARCH_TIMER))
 90		goto out;
 91
 92	/* The arm_arch_timer core should export
 93	 * arch_timer_use_virtual or similar so we don't have to do
 94	 * this.
 95	 */
 96	np = of_find_compatible_node(NULL, NULL, "arm,armv7-timer");
 97	if (!np)
 98		np = of_find_compatible_node(NULL, NULL, "arm,armv8-timer");
 99	if (!np)
100		goto out_put;
101
102	if (of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
103		goto out_put;
104
105	ret = true;
106
107out_put:
108	of_node_put(np);
109out:
110	return ret;
111}
112
113static void * __init find_section(Elf32_Ehdr *ehdr, const char *name,
114				  unsigned long *size)
115{
116	Elf32_Shdr *sechdrs;
117	unsigned int i;
118	char *secnames;
119
120	/* Grab section headers and strings so we can tell who is who */
121	sechdrs = (void *)ehdr + ehdr->e_shoff;
122	secnames = (void *)ehdr + sechdrs[ehdr->e_shstrndx].sh_offset;
123
124	/* Find the section they want */
125	for (i = 1; i < ehdr->e_shnum; i++) {
126		if (strcmp(secnames + sechdrs[i].sh_name, name) == 0) {
127			if (size)
128				*size = sechdrs[i].sh_size;
129			return (void *)ehdr + sechdrs[i].sh_offset;
130		}
131	}
132
133	if (size)
134		*size = 0;
135	return NULL;
136}
137
138static Elf32_Sym * __init find_symbol(struct elfinfo *lib, const char *symname)
139{
140	unsigned int i;
141
142	for (i = 0; i < (lib->dynsymsize / sizeof(Elf32_Sym)); i++) {
143		char name[MAX_SYMNAME], *c;
144
145		if (lib->dynsym[i].st_name == 0)
146			continue;
147		strlcpy(name, lib->dynstr + lib->dynsym[i].st_name,
148			MAX_SYMNAME);
149		c = strchr(name, '@');
150		if (c)
151			*c = 0;
152		if (strcmp(symname, name) == 0)
153			return &lib->dynsym[i];
154	}
155	return NULL;
156}
157
158static void __init vdso_nullpatch_one(struct elfinfo *lib, const char *symname)
159{
160	Elf32_Sym *sym;
161
162	sym = find_symbol(lib, symname);
163	if (!sym)
164		return;
165
166	sym->st_name = 0;
167}
168
169static void __init patch_vdso(void *ehdr)
170{
171	struct elfinfo einfo;
172
173	einfo = (struct elfinfo) {
174		.hdr = ehdr,
175	};
176
177	einfo.dynsym = find_section(einfo.hdr, ".dynsym", &einfo.dynsymsize);
178	einfo.dynstr = find_section(einfo.hdr, ".dynstr", NULL);
179
180	/* If the virtual counter is absent or non-functional we don't
181	 * want programs to incur the slight additional overhead of
182	 * dispatching through the VDSO only to fall back to syscalls.
183	 */
184	if (!cntvct_ok) {
185		vdso_nullpatch_one(&einfo, "__vdso_gettimeofday");
186		vdso_nullpatch_one(&einfo, "__vdso_clock_gettime");
187		vdso_nullpatch_one(&einfo, "__vdso_clock_gettime64");
188	}
189}
190
191static int __init vdso_init(void)
192{
193	unsigned int text_pages;
194	int i;
195
196	if (memcmp(vdso_start, "\177ELF", 4)) {
197		pr_err("VDSO is not a valid ELF object!\n");
198		return -ENOEXEC;
199	}
200
201	text_pages = (vdso_end - vdso_start) >> PAGE_SHIFT;
 
202
203	/* Allocate the VDSO text pagelist */
204	vdso_text_pagelist = kcalloc(text_pages, sizeof(struct page *),
205				     GFP_KERNEL);
206	if (vdso_text_pagelist == NULL)
207		return -ENOMEM;
208
209	/* Grab the VDSO data page. */
210	vdso_data_page = virt_to_page(vdso_data);
211
212	/* Grab the VDSO text pages. */
213	for (i = 0; i < text_pages; i++) {
214		struct page *page;
215
216		page = virt_to_page(vdso_start + i * PAGE_SIZE);
217		vdso_text_pagelist[i] = page;
218	}
219
220	vdso_text_mapping.pages = vdso_text_pagelist;
221
222	vdso_total_pages = 1; /* for the data/vvar page */
223	vdso_total_pages += text_pages;
224
225	cntvct_ok = cntvct_functional();
226
227	patch_vdso(vdso_start);
228
229	return 0;
230}
231arch_initcall(vdso_init);
232
233static int install_vvar(struct mm_struct *mm, unsigned long addr)
234{
235	struct vm_area_struct *vma;
236
237	vma = _install_special_mapping(mm, addr, PAGE_SIZE,
238				       VM_READ | VM_MAYREAD,
239				       &vdso_data_mapping);
240
241	return PTR_ERR_OR_ZERO(vma);
242}
243
244/* assumes mmap_lock is write-locked */
245void arm_install_vdso(struct mm_struct *mm, unsigned long addr)
246{
247	struct vm_area_struct *vma;
248	unsigned long len;
249
250	mm->context.vdso = 0;
251
252	if (vdso_text_pagelist == NULL)
253		return;
254
255	if (install_vvar(mm, addr))
256		return;
257
258	/* Account for vvar page. */
259	addr += PAGE_SIZE;
260	len = (vdso_total_pages - 1) << PAGE_SHIFT;
261
262	vma = _install_special_mapping(mm, addr, len,
263		VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
264		&vdso_text_mapping);
265
266	if (!IS_ERR(vma))
267		mm->context.vdso = addr;
268}
269