2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010, 2012 Konstantin Belousov <kib@FreeBSD.org>
5 * Copyright (c) 2015 The FreeBSD Foundation
8 * Portions of this software were developed by Konstantin Belousov
9 * under sponsorship from the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/rwlock.h>
44 #include <sys/stddef.h>
45 #include <sys/sysent.h>
46 #include <sys/sysctl.h>
50 #include <vm/vm_param.h>
52 #include <vm/vm_extern.h>
53 #include <vm/vm_kern.h>
54 #include <vm/vm_map.h>
55 #include <vm/vm_object.h>
56 #include <vm/vm_page.h>
57 #include <vm/vm_pager.h>
59 static struct sx shared_page_alloc_sx;
60 static vm_object_t shared_page_obj;
61 static int shared_page_free;
62 char *shared_page_mapping;
64 #ifdef RANDOM_FENESTRASX
65 static struct vdso_fxrng_generation *fxrng_shpage_mapping;
67 static bool fxrng_enabled = true;
68 SYSCTL_BOOL(_debug, OID_AUTO, fxrng_vdso_enable, CTLFLAG_RWTUN, &fxrng_enabled,
69 0, "Enable FXRNG VDSO");
73 shared_page_write(int base, int size, const void *data)
76 bcopy(data, shared_page_mapping + base, size);
80 shared_page_alloc_locked(int size, int align)
84 res = roundup(shared_page_free, align);
85 if (res + size >= IDX_TO_OFF(shared_page_obj->size))
88 shared_page_free = res + size;
93 shared_page_alloc(int size, int align)
97 sx_xlock(&shared_page_alloc_sx);
98 res = shared_page_alloc_locked(size, align);
99 sx_xunlock(&shared_page_alloc_sx);
104 shared_page_fill(int size, int align, const void *data)
108 sx_xlock(&shared_page_alloc_sx);
109 res = shared_page_alloc_locked(size, align);
111 shared_page_write(res, size, data);
112 sx_xunlock(&shared_page_alloc_sx);
117 shared_page_init(void *dummy __unused)
122 sx_init(&shared_page_alloc_sx, "shpsx");
123 shared_page_obj = vm_pager_allocate(OBJT_PHYS, 0, PAGE_SIZE,
124 VM_PROT_DEFAULT, 0, NULL);
125 VM_OBJECT_WLOCK(shared_page_obj);
126 m = vm_page_grab(shared_page_obj, 0, VM_ALLOC_ZERO);
127 VM_OBJECT_WUNLOCK(shared_page_obj);
130 addr = kva_alloc(PAGE_SIZE);
131 pmap_qenter(addr, &m, 1);
132 shared_page_mapping = (char *)addr;
135 SYSINIT(shp, SI_SUB_EXEC, SI_ORDER_FIRST, (sysinit_cfunc_t)shared_page_init,
139 * Push the timehands update to the shared page.
141 * The lockless update scheme is similar to the one used to update the
142 * in-kernel timehands, see sys/kern/kern_tc.c:tc_windup() (which
143 * calls us after the timehands are updated).
146 timehands_update(struct vdso_sv_tk *svtk)
148 struct vdso_timehands th;
149 struct vdso_timekeep *tk;
150 uint32_t enabled, idx;
152 enabled = tc_fill_vdso_timehands(&th);
154 idx = svtk->sv_timekeep_curr;
155 if (++idx >= VDSO_TH_NUM)
157 svtk->sv_timekeep_curr = idx;
158 if (++svtk->sv_timekeep_gen == 0)
159 svtk->sv_timekeep_gen = 1;
161 tk = (struct vdso_timekeep *)(shared_page_mapping +
162 svtk->sv_timekeep_off);
163 tk->tk_th[idx].th_gen = 0;
164 atomic_thread_fence_rel();
167 atomic_store_rel_32(&tk->tk_th[idx].th_gen, svtk->sv_timekeep_gen);
168 atomic_store_rel_32(&tk->tk_current, idx);
171 * The ordering of the assignment to tk_enabled relative to
172 * the update of the vdso_timehands is not important.
174 tk->tk_enabled = enabled;
177 #ifdef COMPAT_FREEBSD32
179 timehands_update32(struct vdso_sv_tk *svtk)
181 struct vdso_timehands32 th;
182 struct vdso_timekeep32 *tk;
183 uint32_t enabled, idx;
185 enabled = tc_fill_vdso_timehands32(&th);
187 idx = svtk->sv_timekeep_curr;
188 if (++idx >= VDSO_TH_NUM)
190 svtk->sv_timekeep_curr = idx;
191 if (++svtk->sv_timekeep_gen == 0)
192 svtk->sv_timekeep_gen = 1;
194 tk = (struct vdso_timekeep32 *)(shared_page_mapping +
195 svtk->sv_timekeep_off);
196 tk->tk_th[idx].th_gen = 0;
197 atomic_thread_fence_rel();
200 atomic_store_rel_32(&tk->tk_th[idx].th_gen, svtk->sv_timekeep_gen);
201 atomic_store_rel_32(&tk->tk_current, idx);
202 tk->tk_enabled = enabled;
207 * This is hackish, but easiest way to avoid creating list structures
208 * that needs to be iterated over from the hardclock interrupt
211 static struct vdso_sv_tk *host_svtk;
212 #ifdef COMPAT_FREEBSD32
213 static struct vdso_sv_tk *compat32_svtk;
217 timekeep_push_vdso(void)
220 if (host_svtk != NULL)
221 timehands_update(host_svtk);
222 #ifdef COMPAT_FREEBSD32
223 if (compat32_svtk != NULL)
224 timehands_update32(compat32_svtk);
231 struct vdso_sv_tk *svtk;
235 tk_ver = VDSO_TK_VER_CURR;
236 svtk = malloc(sizeof(struct vdso_sv_tk), M_TEMP, M_WAITOK | M_ZERO);
237 tk_base = shared_page_alloc(sizeof(struct vdso_timekeep) +
238 sizeof(struct vdso_timehands) * VDSO_TH_NUM, 16);
239 KASSERT(tk_base != -1, ("tk_base -1 for native"));
240 shared_page_write(tk_base + offsetof(struct vdso_timekeep, tk_ver),
241 sizeof(uint32_t), &tk_ver);
242 svtk->sv_timekeep_off = tk_base;
243 timekeep_push_vdso();
247 #ifdef COMPAT_FREEBSD32
249 alloc_sv_tk_compat32(void)
251 struct vdso_sv_tk *svtk;
255 svtk = malloc(sizeof(struct vdso_sv_tk), M_TEMP, M_WAITOK | M_ZERO);
256 tk_ver = VDSO_TK_VER_CURR;
257 tk_base = shared_page_alloc(sizeof(struct vdso_timekeep32) +
258 sizeof(struct vdso_timehands32) * VDSO_TH_NUM, 16);
259 KASSERT(tk_base != -1, ("tk_base -1 for 32bit"));
260 shared_page_write(tk_base + offsetof(struct vdso_timekeep32,
261 tk_ver), sizeof(uint32_t), &tk_ver);
262 svtk->sv_timekeep_off = tk_base;
263 timekeep_push_vdso();
268 #ifdef RANDOM_FENESTRASX
270 fxrng_push_seed_generation(uint64_t gen)
272 if (fxrng_shpage_mapping == NULL || !fxrng_enabled)
274 KASSERT(gen < INT32_MAX,
275 ("fxrng seed version shouldn't roll over a 32-bit counter "
276 "for approximately 456,000 years"));
277 atomic_store_rel_32(&fxrng_shpage_mapping->fx_generation32,
282 alloc_sv_fxrng_generation(void)
287 * Allocate a full cache line for the fxrng root generation (64-bit
288 * counter, or truncated 32-bit counter on ILP32 userspace). It is
289 * important that the line is not shared with frequently dirtied data,
290 * and the shared page allocator lacks a __read_mostly mechanism.
291 * However, PAGE_SIZE is typically large relative to the amount of
292 * stuff we've got in it so far, so maybe the possible waste isn't an
295 base = shared_page_alloc(CACHE_LINE_SIZE, CACHE_LINE_SIZE);
296 KASSERT(base != -1, ("%s: base allocation failed", __func__));
297 fxrng_shpage_mapping = (void *)(shared_page_mapping + base);
298 *fxrng_shpage_mapping = (struct vdso_fxrng_generation) {
299 .fx_vdso_version = VDSO_FXRNG_VER_CURR,
302 #endif /* RANDOM_FENESTRASX */
305 exec_sysvec_init(void *param)
307 struct sysentvec *sv;
308 #ifdef RANDOM_FENESTRASX
312 sv = (struct sysentvec *)param;
313 if ((sv->sv_flags & SV_SHP) == 0)
315 sv->sv_shared_page_obj = shared_page_obj;
316 sv->sv_sigcode_base = sv->sv_shared_page_base +
317 shared_page_fill(*(sv->sv_szsigcode), 16, sv->sv_sigcode);
318 if ((sv->sv_flags & SV_ABI_MASK) != SV_ABI_FREEBSD)
320 if ((sv->sv_flags & SV_TIMEKEEP) != 0) {
321 #ifdef COMPAT_FREEBSD32
322 if ((sv->sv_flags & SV_ILP32) != 0) {
323 KASSERT(compat32_svtk == NULL,
324 ("Compat32 already registered"));
325 compat32_svtk = alloc_sv_tk_compat32();
326 sv->sv_timekeep_base = sv->sv_shared_page_base +
327 compat32_svtk->sv_timekeep_off;
330 KASSERT(host_svtk == NULL, ("Host already registered"));
331 host_svtk = alloc_sv_tk();
332 sv->sv_timekeep_base = sv->sv_shared_page_base +
333 host_svtk->sv_timekeep_off;
334 #ifdef COMPAT_FREEBSD32
338 #ifdef RANDOM_FENESTRASX
339 if ((sv->sv_flags & SV_RNG_SEED_VER) != 0) {
341 * Only allocate a single VDSO entry for multiple sysentvecs,
342 * i.e., native and COMPAT32.
344 if (fxrng_shpage_mapping == NULL)
345 alloc_sv_fxrng_generation();
346 base = (char *)fxrng_shpage_mapping - shared_page_mapping;
347 sv->sv_fxrng_gen_base = sv->sv_shared_page_base + base;
353 exec_sysvec_init_secondary(struct sysentvec *sv, struct sysentvec *sv2)
355 MPASS((sv2->sv_flags & SV_ABI_MASK) == (sv->sv_flags & SV_ABI_MASK));
356 MPASS((sv2->sv_flags & SV_TIMEKEEP) == (sv->sv_flags & SV_TIMEKEEP));
357 MPASS((sv2->sv_flags & SV_SHP) != 0 && (sv->sv_flags & SV_SHP) != 0);
358 MPASS((sv2->sv_flags & SV_RNG_SEED_VER) ==
359 (sv->sv_flags & SV_RNG_SEED_VER));
361 sv2->sv_shared_page_obj = sv->sv_shared_page_obj;
362 sv2->sv_sigcode_base = sv2->sv_shared_page_base +
363 (sv->sv_sigcode_base - sv->sv_shared_page_base);
364 if ((sv2->sv_flags & SV_ABI_MASK) != SV_ABI_FREEBSD)
366 if ((sv2->sv_flags & SV_TIMEKEEP) != 0) {
367 sv2->sv_timekeep_base = sv2->sv_shared_page_base +
368 (sv->sv_timekeep_base - sv->sv_shared_page_base);
370 if ((sv2->sv_flags & SV_RNG_SEED_VER) != 0) {
371 sv2->sv_fxrng_gen_base = sv2->sv_shared_page_base +
372 (sv->sv_fxrng_gen_base - sv->sv_shared_page_base);