2 * Adapted from arm64 version.
4 * Copyright (C) 2012 ARM Limited
5 * Copyright (C) 2015 Mentor Graphics Corporation.
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.
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.
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/>.
20 #include <linux/elf.h>
21 #include <linux/err.h>
22 #include <linux/kernel.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>
34 #include <asm/vdso_datapage.h>
35 #include <clocksource/arm_arch_timer.h>
37 #define MAX_SYMNAME 64
39 static struct page
**vdso_text_pagelist
;
41 /* Total number of pages needed for the data and text portions of the VDSO. */
42 unsigned int vdso_total_pages __read_mostly
;
47 static union vdso_data_store vdso_data_store __page_aligned_data
;
48 static struct vdso_data
*vdso_data
= &vdso_data_store
.data
;
50 static struct page
*vdso_data_page
;
51 static struct vm_special_mapping vdso_data_mapping
= {
53 .pages
= &vdso_data_page
,
56 static struct vm_special_mapping vdso_text_mapping
= {
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 */
67 /* Cached result of boot-time check for whether the arch timer exists,
68 * and if so, whether the virtual counter is useable.
70 static bool cntvct_ok __read_mostly
;
72 static bool __init
cntvct_functional(void)
74 struct device_node
*np
;
77 if (!IS_ENABLED(CONFIG_ARM_ARCH_TIMER
))
80 /* The arm_arch_timer core should export
81 * arch_timer_use_virtual or similar so we don't have to do
84 np
= of_find_compatible_node(NULL
, NULL
, "arm,armv7-timer");
88 if (of_property_read_bool(np
, "arm,cpu-registers-not-fw-configured"))
99 static void * __init
find_section(Elf32_Ehdr
*ehdr
, const char *name
,
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) {
114 *size
= sechdrs
[i
].sh_size
;
115 return (void *)ehdr
+ sechdrs
[i
].sh_offset
;
124 static Elf32_Sym
* __init
find_symbol(struct elfinfo
*lib
, const char *symname
)
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)
133 strlcpy(name
, lib
->dynstr
+ lib
->dynsym
[i
].st_name
,
135 c
= strchr(name
, '@');
138 if (strcmp(symname
, name
) == 0)
139 return &lib
->dynsym
[i
];
144 static void __init
vdso_nullpatch_one(struct elfinfo
*lib
, const char *symname
)
148 sym
= find_symbol(lib
, symname
);
155 static void __init
patch_vdso(void *ehdr
)
157 struct elfinfo einfo
;
159 einfo
= (struct elfinfo
) {
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.
171 vdso_nullpatch_one(&einfo
, "__vdso_gettimeofday");
172 vdso_nullpatch_one(&einfo
, "__vdso_clock_gettime");
176 static int __init
vdso_init(void)
178 unsigned int text_pages
;
181 if (memcmp(&vdso_start
, "\177ELF", 4)) {
182 pr_err("VDSO is not a valid ELF object!\n");
186 text_pages
= (&vdso_end
- &vdso_start
) >> PAGE_SHIFT
;
187 pr_debug("vdso: %i text pages at base %p\n", text_pages
, &vdso_start
);
189 /* Allocate the VDSO text pagelist */
190 vdso_text_pagelist
= kcalloc(text_pages
, sizeof(struct page
*),
192 if (vdso_text_pagelist
== NULL
)
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
++) {
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
);
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
,
227 return IS_ERR(vma
) ? PTR_ERR(vma
) : 0;
230 /* assumes mmap_sem is write-locked */
231 void arm_install_vdso(struct mm_struct
*mm
, unsigned long addr
)
233 struct vm_area_struct
*vma
;
236 mm
->context
.vdso
= 0;
238 if (vdso_text_pagelist
== NULL
)
241 if (install_vvar(mm
, addr
))
244 /* Account for vvar page. */
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
,
253 mm
->context
.vdso
= addr
;
256 static void vdso_write_begin(struct vdso_data
*vdata
)
258 ++vdso_data
->seq_count
;
259 smp_wmb(); /* Pairs with smp_rmb in vdso_read_retry */
262 static void vdso_write_end(struct vdso_data
*vdata
)
264 smp_wmb(); /* Pairs with smp_rmb in vdso_read_begin */
265 ++vdso_data
->seq_count
;
268 static bool tk_is_cntvct(const struct timekeeper
*tk
)
270 if (!IS_ENABLED(CONFIG_ARM_ARCH_TIMER
))
273 if (strcmp(tk
->tkr_mono
.clock
->name
, "arch_sys_counter") != 0)
280 * update_vsyscall - update the vdso data page
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.
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.
295 * Calls to update_vsyscall are serialized by the timekeeping core.
297 void update_vsyscall(struct timekeeper
*tk
)
299 struct timespec xtime_coarse
;
300 struct timespec64
*wtm
= &tk
->wall_to_monotonic
;
303 /* The entry points have been zeroed, so there is no
304 * point in updating the data page.
309 vdso_write_begin(vdso_data
);
311 xtime_coarse
= __current_kernel_time();
312 vdso_data
->tk_is_cntvct
= tk_is_cntvct(tk
);
313 vdso_data
->xtime_coarse_sec
= xtime_coarse
.tv_sec
;
314 vdso_data
->xtime_coarse_nsec
= xtime_coarse
.tv_nsec
;
315 vdso_data
->wtm_clock_sec
= wtm
->tv_sec
;
316 vdso_data
->wtm_clock_nsec
= wtm
->tv_nsec
;
318 if (vdso_data
->tk_is_cntvct
) {
319 vdso_data
->cs_cycle_last
= tk
->tkr_mono
.cycle_last
;
320 vdso_data
->xtime_clock_sec
= tk
->xtime_sec
;
321 vdso_data
->xtime_clock_snsec
= tk
->tkr_mono
.xtime_nsec
;
322 vdso_data
->cs_mult
= tk
->tkr_mono
.mult
;
323 vdso_data
->cs_shift
= tk
->tkr_mono
.shift
;
324 vdso_data
->cs_mask
= tk
->tkr_mono
.mask
;
327 vdso_write_end(vdso_data
);
329 flush_dcache_page(virt_to_page(vdso_data
));
332 void update_vsyscall_tz(void)
334 vdso_data
->tz_minuteswest
= sys_tz
.tz_minuteswest
;
335 vdso_data
->tz_dsttime
= sys_tz
.tz_dsttime
;
336 flush_dcache_page(virt_to_page(vdso_data
));