xref: /freebsd/sys/kern/kern_sharedpage.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
1a665ed98SKonstantin Belousov /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
38a36da99SPedro F. Giffuni  *
4a665ed98SKonstantin Belousov  * Copyright (c) 2010, 2012 Konstantin Belousov <kib@FreeBSD.org>
55e27d793SKonstantin Belousov  * Copyright (c) 2015 The FreeBSD Foundation
6a665ed98SKonstantin Belousov  * All rights reserved.
7a665ed98SKonstantin Belousov  *
85e27d793SKonstantin Belousov  * Portions of this software were developed by Konstantin Belousov
95e27d793SKonstantin Belousov  * under sponsorship from the FreeBSD Foundation.
105e27d793SKonstantin Belousov  *
11a665ed98SKonstantin Belousov  * Redistribution and use in source and binary forms, with or without
12a665ed98SKonstantin Belousov  * modification, are permitted provided that the following conditions
13a665ed98SKonstantin Belousov  * are met:
14a665ed98SKonstantin Belousov  * 1. Redistributions of source code must retain the above copyright
15a665ed98SKonstantin Belousov  *    notice, this list of conditions and the following disclaimer.
16a665ed98SKonstantin Belousov  * 2. Redistributions in binary form must reproduce the above copyright
17a665ed98SKonstantin Belousov  *    notice, this list of conditions and the following disclaimer in the
18a665ed98SKonstantin Belousov  *    documentation and/or other materials provided with the distribution.
19a665ed98SKonstantin Belousov  *
20a665ed98SKonstantin Belousov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21a665ed98SKonstantin Belousov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22a665ed98SKonstantin Belousov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23a665ed98SKonstantin Belousov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24a665ed98SKonstantin Belousov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25a665ed98SKonstantin Belousov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26a665ed98SKonstantin Belousov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27a665ed98SKonstantin Belousov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28a665ed98SKonstantin Belousov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29a665ed98SKonstantin Belousov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30a665ed98SKonstantin Belousov  * SUCH DAMAGE.
31a665ed98SKonstantin Belousov  */
32a665ed98SKonstantin Belousov 
33a665ed98SKonstantin Belousov #include <sys/cdefs.h>
34a665ed98SKonstantin Belousov #include "opt_vm.h"
35a665ed98SKonstantin Belousov 
36a665ed98SKonstantin Belousov #include <sys/param.h>
37a665ed98SKonstantin Belousov #include <sys/systm.h>
38a665ed98SKonstantin Belousov #include <sys/kernel.h>
39a665ed98SKonstantin Belousov #include <sys/lock.h>
405e27d793SKonstantin Belousov #include <sys/malloc.h>
4189f6b863SAttilio Rao #include <sys/rwlock.h>
42f8e8a06dSConrad Meyer #include <sys/stddef.h>
43a665ed98SKonstantin Belousov #include <sys/sysent.h>
44a665ed98SKonstantin Belousov #include <sys/sysctl.h>
45a665ed98SKonstantin Belousov #include <sys/vdso.h>
46a665ed98SKonstantin Belousov 
47a665ed98SKonstantin Belousov #include <vm/vm.h>
48a665ed98SKonstantin Belousov #include <vm/vm_param.h>
49a665ed98SKonstantin Belousov #include <vm/pmap.h>
50a665ed98SKonstantin Belousov #include <vm/vm_extern.h>
51a665ed98SKonstantin Belousov #include <vm/vm_kern.h>
52a665ed98SKonstantin Belousov #include <vm/vm_map.h>
53a665ed98SKonstantin Belousov #include <vm/vm_object.h>
54a665ed98SKonstantin Belousov #include <vm/vm_page.h>
55a665ed98SKonstantin Belousov #include <vm/vm_pager.h>
56a665ed98SKonstantin Belousov 
57a665ed98SKonstantin Belousov static struct sx shared_page_alloc_sx;
58a665ed98SKonstantin Belousov static vm_object_t shared_page_obj;
59a665ed98SKonstantin Belousov static int shared_page_free;
60a665ed98SKonstantin Belousov char *shared_page_mapping;
61a665ed98SKonstantin Belousov 
62f8e8a06dSConrad Meyer #ifdef RANDOM_FENESTRASX
63f8e8a06dSConrad Meyer static struct vdso_fxrng_generation *fxrng_shpage_mapping;
64f8e8a06dSConrad Meyer 
65f8e8a06dSConrad Meyer static bool fxrng_enabled = true;
66f8e8a06dSConrad Meyer SYSCTL_BOOL(_debug, OID_AUTO, fxrng_vdso_enable, CTLFLAG_RWTUN, &fxrng_enabled,
67f8e8a06dSConrad Meyer     0, "Enable FXRNG VDSO");
68f8e8a06dSConrad Meyer #endif
69f8e8a06dSConrad Meyer 
70a665ed98SKonstantin Belousov void
shared_page_write(int base,int size,const void * data)71a665ed98SKonstantin Belousov shared_page_write(int base, int size, const void *data)
72a665ed98SKonstantin Belousov {
73a665ed98SKonstantin Belousov 
74a665ed98SKonstantin Belousov 	bcopy(data, shared_page_mapping + base, size);
75a665ed98SKonstantin Belousov }
76a665ed98SKonstantin Belousov 
77a665ed98SKonstantin Belousov static int
shared_page_alloc_locked(int size,int align)78a665ed98SKonstantin Belousov shared_page_alloc_locked(int size, int align)
79a665ed98SKonstantin Belousov {
80a665ed98SKonstantin Belousov 	int res;
81a665ed98SKonstantin Belousov 
82a665ed98SKonstantin Belousov 	res = roundup(shared_page_free, align);
83a665ed98SKonstantin Belousov 	if (res + size >= IDX_TO_OFF(shared_page_obj->size))
84a665ed98SKonstantin Belousov 		res = -1;
85a665ed98SKonstantin Belousov 	else
86a665ed98SKonstantin Belousov 		shared_page_free = res + size;
87a665ed98SKonstantin Belousov 	return (res);
88a665ed98SKonstantin Belousov }
89a665ed98SKonstantin Belousov 
90a665ed98SKonstantin Belousov int
shared_page_alloc(int size,int align)91a665ed98SKonstantin Belousov shared_page_alloc(int size, int align)
92a665ed98SKonstantin Belousov {
93a665ed98SKonstantin Belousov 	int res;
94a665ed98SKonstantin Belousov 
95a665ed98SKonstantin Belousov 	sx_xlock(&shared_page_alloc_sx);
96a665ed98SKonstantin Belousov 	res = shared_page_alloc_locked(size, align);
97a665ed98SKonstantin Belousov 	sx_xunlock(&shared_page_alloc_sx);
98a665ed98SKonstantin Belousov 	return (res);
99a665ed98SKonstantin Belousov }
100a665ed98SKonstantin Belousov 
101a665ed98SKonstantin Belousov int
shared_page_fill(int size,int align,const void * data)102a665ed98SKonstantin Belousov shared_page_fill(int size, int align, const void *data)
103a665ed98SKonstantin Belousov {
104a665ed98SKonstantin Belousov 	int res;
105a665ed98SKonstantin Belousov 
106a665ed98SKonstantin Belousov 	sx_xlock(&shared_page_alloc_sx);
107a665ed98SKonstantin Belousov 	res = shared_page_alloc_locked(size, align);
108a665ed98SKonstantin Belousov 	if (res != -1)
109a665ed98SKonstantin Belousov 		shared_page_write(res, size, data);
110a665ed98SKonstantin Belousov 	sx_xunlock(&shared_page_alloc_sx);
111a665ed98SKonstantin Belousov 	return (res);
112a665ed98SKonstantin Belousov }
113a665ed98SKonstantin Belousov 
114a665ed98SKonstantin Belousov static void
shared_page_init(void * dummy __unused)115a665ed98SKonstantin Belousov shared_page_init(void *dummy __unused)
116a665ed98SKonstantin Belousov {
117a665ed98SKonstantin Belousov 	vm_page_t m;
118a665ed98SKonstantin Belousov 	vm_offset_t addr;
119a665ed98SKonstantin Belousov 
120a665ed98SKonstantin Belousov 	sx_init(&shared_page_alloc_sx, "shpsx");
121a665ed98SKonstantin Belousov 	shared_page_obj = vm_pager_allocate(OBJT_PHYS, 0, PAGE_SIZE,
122a665ed98SKonstantin Belousov 	    VM_PROT_DEFAULT, 0, NULL);
12389f6b863SAttilio Rao 	VM_OBJECT_WLOCK(shared_page_obj);
1244504268aSJeff Roberson 	m = vm_page_grab(shared_page_obj, 0, VM_ALLOC_ZERO);
12589f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(shared_page_obj);
1264504268aSJeff Roberson 	vm_page_valid(m);
1274504268aSJeff Roberson 	vm_page_xunbusy(m);
1285df87b21SJeff Roberson 	addr = kva_alloc(PAGE_SIZE);
129a665ed98SKonstantin Belousov 	pmap_qenter(addr, &m, 1);
130a665ed98SKonstantin Belousov 	shared_page_mapping = (char *)addr;
131a665ed98SKonstantin Belousov }
132a665ed98SKonstantin Belousov 
133a665ed98SKonstantin Belousov SYSINIT(shp, SI_SUB_EXEC, SI_ORDER_FIRST, (sysinit_cfunc_t)shared_page_init,
134a665ed98SKonstantin Belousov     NULL);
135a665ed98SKonstantin Belousov 
13635dfc644SKonstantin Belousov /*
13735dfc644SKonstantin Belousov  * Push the timehands update to the shared page.
13835dfc644SKonstantin Belousov  *
13935dfc644SKonstantin Belousov  * The lockless update scheme is similar to the one used to update the
14035dfc644SKonstantin Belousov  * in-kernel timehands, see sys/kern/kern_tc.c:tc_windup() (which
14135dfc644SKonstantin Belousov  * calls us after the timehands are updated).
14235dfc644SKonstantin Belousov  */
143a665ed98SKonstantin Belousov static void
timehands_update(struct vdso_sv_tk * svtk)1445e27d793SKonstantin Belousov timehands_update(struct vdso_sv_tk *svtk)
145a665ed98SKonstantin Belousov {
146a665ed98SKonstantin Belousov 	struct vdso_timehands th;
147a665ed98SKonstantin Belousov 	struct vdso_timekeep *tk;
148a665ed98SKonstantin Belousov 	uint32_t enabled, idx;
149a665ed98SKonstantin Belousov 
150a665ed98SKonstantin Belousov 	enabled = tc_fill_vdso_timehands(&th);
15135dfc644SKonstantin Belousov 	th.th_gen = 0;
1525e27d793SKonstantin Belousov 	idx = svtk->sv_timekeep_curr;
153a665ed98SKonstantin Belousov 	if (++idx >= VDSO_TH_NUM)
154a665ed98SKonstantin Belousov 		idx = 0;
1555e27d793SKonstantin Belousov 	svtk->sv_timekeep_curr = idx;
1565e27d793SKonstantin Belousov 	if (++svtk->sv_timekeep_gen == 0)
1575e27d793SKonstantin Belousov 		svtk->sv_timekeep_gen = 1;
15835dfc644SKonstantin Belousov 
15935dfc644SKonstantin Belousov 	tk = (struct vdso_timekeep *)(shared_page_mapping +
1605e27d793SKonstantin Belousov 	    svtk->sv_timekeep_off);
16135dfc644SKonstantin Belousov 	tk->tk_th[idx].th_gen = 0;
16235dfc644SKonstantin Belousov 	atomic_thread_fence_rel();
163a665ed98SKonstantin Belousov 	if (enabled)
164a665ed98SKonstantin Belousov 		tk->tk_th[idx] = th;
1655e27d793SKonstantin Belousov 	atomic_store_rel_32(&tk->tk_th[idx].th_gen, svtk->sv_timekeep_gen);
16635dfc644SKonstantin Belousov 	atomic_store_rel_32(&tk->tk_current, idx);
16735dfc644SKonstantin Belousov 
16835dfc644SKonstantin Belousov 	/*
16935dfc644SKonstantin Belousov 	 * The ordering of the assignment to tk_enabled relative to
17035dfc644SKonstantin Belousov 	 * the update of the vdso_timehands is not important.
17135dfc644SKonstantin Belousov 	 */
17235dfc644SKonstantin Belousov 	tk->tk_enabled = enabled;
173a665ed98SKonstantin Belousov }
174a665ed98SKonstantin Belousov 
175a665ed98SKonstantin Belousov #ifdef COMPAT_FREEBSD32
176a665ed98SKonstantin Belousov static void
timehands_update32(struct vdso_sv_tk * svtk)1775e27d793SKonstantin Belousov timehands_update32(struct vdso_sv_tk *svtk)
178a665ed98SKonstantin Belousov {
179a665ed98SKonstantin Belousov 	struct vdso_timehands32 th;
18035dfc644SKonstantin Belousov 	struct vdso_timekeep32 *tk;
181a665ed98SKonstantin Belousov 	uint32_t enabled, idx;
182a665ed98SKonstantin Belousov 
183a665ed98SKonstantin Belousov 	enabled = tc_fill_vdso_timehands32(&th);
18435dfc644SKonstantin Belousov 	th.th_gen = 0;
1855e27d793SKonstantin Belousov 	idx = svtk->sv_timekeep_curr;
186a665ed98SKonstantin Belousov 	if (++idx >= VDSO_TH_NUM)
187a665ed98SKonstantin Belousov 		idx = 0;
1885e27d793SKonstantin Belousov 	svtk->sv_timekeep_curr = idx;
1895e27d793SKonstantin Belousov 	if (++svtk->sv_timekeep_gen == 0)
1905e27d793SKonstantin Belousov 		svtk->sv_timekeep_gen = 1;
19135dfc644SKonstantin Belousov 
19235dfc644SKonstantin Belousov 	tk = (struct vdso_timekeep32 *)(shared_page_mapping +
1935e27d793SKonstantin Belousov 	    svtk->sv_timekeep_off);
19435dfc644SKonstantin Belousov 	tk->tk_th[idx].th_gen = 0;
19535dfc644SKonstantin Belousov 	atomic_thread_fence_rel();
196a665ed98SKonstantin Belousov 	if (enabled)
197a665ed98SKonstantin Belousov 		tk->tk_th[idx] = th;
1985e27d793SKonstantin Belousov 	atomic_store_rel_32(&tk->tk_th[idx].th_gen, svtk->sv_timekeep_gen);
19935dfc644SKonstantin Belousov 	atomic_store_rel_32(&tk->tk_current, idx);
20035dfc644SKonstantin Belousov 	tk->tk_enabled = enabled;
201a665ed98SKonstantin Belousov }
202a665ed98SKonstantin Belousov #endif
203a665ed98SKonstantin Belousov 
204a665ed98SKonstantin Belousov /*
205a665ed98SKonstantin Belousov  * This is hackish, but easiest way to avoid creating list structures
206a665ed98SKonstantin Belousov  * that needs to be iterated over from the hardclock interrupt
207a665ed98SKonstantin Belousov  * context.
208a665ed98SKonstantin Belousov  */
2095e27d793SKonstantin Belousov static struct vdso_sv_tk *host_svtk;
210a665ed98SKonstantin Belousov #ifdef COMPAT_FREEBSD32
2115e27d793SKonstantin Belousov static struct vdso_sv_tk *compat32_svtk;
212a665ed98SKonstantin Belousov #endif
213a665ed98SKonstantin Belousov 
214a665ed98SKonstantin Belousov void
timekeep_push_vdso(void)215a665ed98SKonstantin Belousov timekeep_push_vdso(void)
216a665ed98SKonstantin Belousov {
217a665ed98SKonstantin Belousov 
2185e27d793SKonstantin Belousov 	if (host_svtk != NULL)
2195e27d793SKonstantin Belousov 		timehands_update(host_svtk);
220a665ed98SKonstantin Belousov #ifdef COMPAT_FREEBSD32
2215e27d793SKonstantin Belousov 	if (compat32_svtk != NULL)
2225e27d793SKonstantin Belousov 		timehands_update32(compat32_svtk);
223a665ed98SKonstantin Belousov #endif
224a665ed98SKonstantin Belousov }
225a665ed98SKonstantin Belousov 
2265e27d793SKonstantin Belousov struct vdso_sv_tk *
alloc_sv_tk(void)2275e27d793SKonstantin Belousov alloc_sv_tk(void)
2285e27d793SKonstantin Belousov {
2295e27d793SKonstantin Belousov 	struct vdso_sv_tk *svtk;
2305e27d793SKonstantin Belousov 	int tk_base;
2315e27d793SKonstantin Belousov 	uint32_t tk_ver;
2325e27d793SKonstantin Belousov 
2335e27d793SKonstantin Belousov 	tk_ver = VDSO_TK_VER_CURR;
2345e27d793SKonstantin Belousov 	svtk = malloc(sizeof(struct vdso_sv_tk), M_TEMP, M_WAITOK | M_ZERO);
2355e27d793SKonstantin Belousov 	tk_base = shared_page_alloc(sizeof(struct vdso_timekeep) +
2365e27d793SKonstantin Belousov 	    sizeof(struct vdso_timehands) * VDSO_TH_NUM, 16);
2375e27d793SKonstantin Belousov 	KASSERT(tk_base != -1, ("tk_base -1 for native"));
2385e27d793SKonstantin Belousov 	shared_page_write(tk_base + offsetof(struct vdso_timekeep, tk_ver),
2395e27d793SKonstantin Belousov 	    sizeof(uint32_t), &tk_ver);
2405e27d793SKonstantin Belousov 	svtk->sv_timekeep_off = tk_base;
2415e27d793SKonstantin Belousov 	timekeep_push_vdso();
2425e27d793SKonstantin Belousov 	return (svtk);
2435e27d793SKonstantin Belousov }
2445e27d793SKonstantin Belousov 
2455e27d793SKonstantin Belousov #ifdef COMPAT_FREEBSD32
2465e27d793SKonstantin Belousov struct vdso_sv_tk *
alloc_sv_tk_compat32(void)2475e27d793SKonstantin Belousov alloc_sv_tk_compat32(void)
2485e27d793SKonstantin Belousov {
2495e27d793SKonstantin Belousov 	struct vdso_sv_tk *svtk;
2505e27d793SKonstantin Belousov 	int tk_base;
2515e27d793SKonstantin Belousov 	uint32_t tk_ver;
2525e27d793SKonstantin Belousov 
2535e27d793SKonstantin Belousov 	svtk = malloc(sizeof(struct vdso_sv_tk), M_TEMP, M_WAITOK | M_ZERO);
2545e27d793SKonstantin Belousov 	tk_ver = VDSO_TK_VER_CURR;
2555e27d793SKonstantin Belousov 	tk_base = shared_page_alloc(sizeof(struct vdso_timekeep32) +
2565e27d793SKonstantin Belousov 	    sizeof(struct vdso_timehands32) * VDSO_TH_NUM, 16);
2575e27d793SKonstantin Belousov 	KASSERT(tk_base != -1, ("tk_base -1 for 32bit"));
2585e27d793SKonstantin Belousov 	shared_page_write(tk_base + offsetof(struct vdso_timekeep32,
2595e27d793SKonstantin Belousov 	    tk_ver), sizeof(uint32_t), &tk_ver);
2605e27d793SKonstantin Belousov 	svtk->sv_timekeep_off = tk_base;
2615e27d793SKonstantin Belousov 	timekeep_push_vdso();
2625e27d793SKonstantin Belousov 	return (svtk);
2635e27d793SKonstantin Belousov }
2645e27d793SKonstantin Belousov #endif
2655e27d793SKonstantin Belousov 
266f8e8a06dSConrad Meyer #ifdef RANDOM_FENESTRASX
267f8e8a06dSConrad Meyer void
fxrng_push_seed_generation(uint64_t gen)268f8e8a06dSConrad Meyer fxrng_push_seed_generation(uint64_t gen)
269f8e8a06dSConrad Meyer {
270f8e8a06dSConrad Meyer 	if (fxrng_shpage_mapping == NULL || !fxrng_enabled)
271f8e8a06dSConrad Meyer 		return;
272f8e8a06dSConrad Meyer 	KASSERT(gen < INT32_MAX,
273f8e8a06dSConrad Meyer 	    ("fxrng seed version shouldn't roll over a 32-bit counter "
274f8e8a06dSConrad Meyer 	     "for approximately 456,000 years"));
275f8e8a06dSConrad Meyer 	atomic_store_rel_32(&fxrng_shpage_mapping->fx_generation32,
276f8e8a06dSConrad Meyer 	    (uint32_t)gen);
277f8e8a06dSConrad Meyer }
278f8e8a06dSConrad Meyer 
279f8e8a06dSConrad Meyer static void
alloc_sv_fxrng_generation(void)280f8e8a06dSConrad Meyer alloc_sv_fxrng_generation(void)
281f8e8a06dSConrad Meyer {
282f8e8a06dSConrad Meyer 	int base;
283f8e8a06dSConrad Meyer 
284f8e8a06dSConrad Meyer 	/*
285f8e8a06dSConrad Meyer 	 * Allocate a full cache line for the fxrng root generation (64-bit
286f8e8a06dSConrad Meyer 	 * counter, or truncated 32-bit counter on ILP32 userspace).  It is
287f8e8a06dSConrad Meyer 	 * important that the line is not shared with frequently dirtied data,
288f8e8a06dSConrad Meyer 	 * and the shared page allocator lacks a __read_mostly mechanism.
289f8e8a06dSConrad Meyer 	 * However, PAGE_SIZE is typically large relative to the amount of
290f8e8a06dSConrad Meyer 	 * stuff we've got in it so far, so maybe the possible waste isn't an
291f8e8a06dSConrad Meyer 	 * issue.
292f8e8a06dSConrad Meyer 	 */
293f8e8a06dSConrad Meyer 	base = shared_page_alloc(CACHE_LINE_SIZE, CACHE_LINE_SIZE);
294f8e8a06dSConrad Meyer 	KASSERT(base != -1, ("%s: base allocation failed", __func__));
295f8e8a06dSConrad Meyer 	fxrng_shpage_mapping = (void *)(shared_page_mapping + base);
296f8e8a06dSConrad Meyer 	*fxrng_shpage_mapping = (struct vdso_fxrng_generation) {
297f8e8a06dSConrad Meyer 		.fx_vdso_version = VDSO_FXRNG_VER_CURR,
298f8e8a06dSConrad Meyer 	};
299f8e8a06dSConrad Meyer }
300f8e8a06dSConrad Meyer #endif /* RANDOM_FENESTRASX */
301f8e8a06dSConrad Meyer 
302a665ed98SKonstantin Belousov void
exec_sysvec_init(void * param)303a665ed98SKonstantin Belousov exec_sysvec_init(void *param)
304a665ed98SKonstantin Belousov {
305a665ed98SKonstantin Belousov 	struct sysentvec *sv;
30621629e2aSDmitry Chagin 	u_int flags;
307ab4524b3SKonstantin Belousov 	int res;
308a665ed98SKonstantin Belousov 
309b39fa477SDmitry Chagin 	sv = param;
31021629e2aSDmitry Chagin 	flags = sv->sv_flags;
31121629e2aSDmitry Chagin 	if ((flags & SV_SHP) == 0)
312a665ed98SKonstantin Belousov 		return;
31321629e2aSDmitry Chagin 	MPASS(sv->sv_shared_page_obj == NULL);
31421629e2aSDmitry Chagin 	MPASS(sv->sv_shared_page_base != 0);
31521629e2aSDmitry Chagin 
316a665ed98SKonstantin Belousov 	sv->sv_shared_page_obj = shared_page_obj;
31721629e2aSDmitry Chagin 	if ((flags & SV_ABI_MASK) == SV_ABI_FREEBSD) {
318ab4524b3SKonstantin Belousov 		if ((flags & SV_DSO_SIG) != 0) {
319ab4524b3SKonstantin Belousov 			res = shared_page_fill((uintptr_t)sv->sv_szsigcode,
320ab4524b3SKonstantin Belousov 			    16, sv->sv_sigcode);
321ab4524b3SKonstantin Belousov 			if (res == -1)
322361971fbSKornel Dulęba 				panic("copying vdso to shared page");
323361971fbSKornel Dulęba 			sv->sv_vdso_offset = res;
324361971fbSKornel Dulęba 			sv->sv_sigcode_offset = res + sv->sv_sigcodeoff;
325ab4524b3SKonstantin Belousov 		} else {
326361971fbSKornel Dulęba 			res = shared_page_fill(*(sv->sv_szsigcode),
327361971fbSKornel Dulęba 			    16, sv->sv_sigcode);
328361971fbSKornel Dulęba 			if (res == -1)
329361971fbSKornel Dulęba 				panic("copying sigtramp to shared page");
330361971fbSKornel Dulęba 			sv->sv_sigcode_offset = res;
331ab4524b3SKonstantin Belousov 		}
33221629e2aSDmitry Chagin 	}
33321629e2aSDmitry Chagin 	if ((flags & SV_TIMEKEEP) != 0) {
334a665ed98SKonstantin Belousov #ifdef COMPAT_FREEBSD32
33521629e2aSDmitry Chagin 		if ((flags & SV_ILP32) != 0) {
33621629e2aSDmitry Chagin 			if ((flags & SV_ABI_MASK) == SV_ABI_FREEBSD) {
3375e27d793SKonstantin Belousov 				KASSERT(compat32_svtk == NULL,
3385e27d793SKonstantin Belousov 				    ("Compat32 already registered"));
3395e27d793SKonstantin Belousov 				compat32_svtk = alloc_sv_tk_compat32();
34021629e2aSDmitry Chagin 			} else {
34121629e2aSDmitry Chagin 				KASSERT(compat32_svtk != NULL,
34221629e2aSDmitry Chagin 				    ("Compat32 not registered"));
34321629e2aSDmitry Chagin 			}
344361971fbSKornel Dulęba 			sv->sv_timekeep_offset = compat32_svtk->sv_timekeep_off;
345a665ed98SKonstantin Belousov 		} else {
346a665ed98SKonstantin Belousov #endif
34721629e2aSDmitry Chagin 			if ((flags & SV_ABI_MASK) == SV_ABI_FREEBSD) {
34821629e2aSDmitry Chagin 				KASSERT(host_svtk == NULL,
34921629e2aSDmitry Chagin 				    ("Host already registered"));
3505e27d793SKonstantin Belousov 				host_svtk = alloc_sv_tk();
35121629e2aSDmitry Chagin 			} else {
35221629e2aSDmitry Chagin 				KASSERT(host_svtk != NULL,
35321629e2aSDmitry Chagin 				    ("Host not registered"));
35421629e2aSDmitry Chagin 			}
355361971fbSKornel Dulęba 			sv->sv_timekeep_offset = host_svtk->sv_timekeep_off;
356a665ed98SKonstantin Belousov #ifdef COMPAT_FREEBSD32
357a665ed98SKonstantin Belousov 		}
358a665ed98SKonstantin Belousov #endif
3595e27d793SKonstantin Belousov 	}
360f8e8a06dSConrad Meyer #ifdef RANDOM_FENESTRASX
36121629e2aSDmitry Chagin 	if ((flags & (SV_ABI_MASK | SV_RNG_SEED_VER)) ==
36221629e2aSDmitry Chagin 	    (SV_ABI_FREEBSD | SV_RNG_SEED_VER)) {
363f8e8a06dSConrad Meyer 		/*
364f8e8a06dSConrad Meyer 		 * Only allocate a single VDSO entry for multiple sysentvecs,
365f8e8a06dSConrad Meyer 		 * i.e., native and COMPAT32.
366f8e8a06dSConrad Meyer 		 */
367f8e8a06dSConrad Meyer 		if (fxrng_shpage_mapping == NULL)
368f8e8a06dSConrad Meyer 			alloc_sv_fxrng_generation();
369361971fbSKornel Dulęba 		sv->sv_fxrng_gen_offset =
370361971fbSKornel Dulęba 		    (char *)fxrng_shpage_mapping - shared_page_mapping;
371f8e8a06dSConrad Meyer 	}
372f8e8a06dSConrad Meyer #endif
373a665ed98SKonstantin Belousov }
3742b313da3SKonstantin Belousov 
3752b313da3SKonstantin Belousov void
exec_sysvec_init_secondary(struct sysentvec * sv,struct sysentvec * sv2)3762b313da3SKonstantin Belousov exec_sysvec_init_secondary(struct sysentvec *sv, struct sysentvec *sv2)
3772b313da3SKonstantin Belousov {
3782b313da3SKonstantin Belousov 	MPASS((sv2->sv_flags & SV_ABI_MASK) == (sv->sv_flags & SV_ABI_MASK));
3792b313da3SKonstantin Belousov 	MPASS((sv2->sv_flags & SV_TIMEKEEP) == (sv->sv_flags & SV_TIMEKEEP));
3802b313da3SKonstantin Belousov 	MPASS((sv2->sv_flags & SV_SHP) != 0 && (sv->sv_flags & SV_SHP) != 0);
381d2de6881SJustin Hibbits 	MPASS((sv2->sv_flags & SV_DSO_SIG) == (sv->sv_flags & SV_DSO_SIG));
382f8e8a06dSConrad Meyer 	MPASS((sv2->sv_flags & SV_RNG_SEED_VER) ==
383f8e8a06dSConrad Meyer 	    (sv->sv_flags & SV_RNG_SEED_VER));
3842b313da3SKonstantin Belousov 
3852b313da3SKonstantin Belousov 	sv2->sv_shared_page_obj = sv->sv_shared_page_obj;
386361971fbSKornel Dulęba 	sv2->sv_sigcode_offset = sv->sv_sigcode_offset;
387361971fbSKornel Dulęba 	sv2->sv_vdso_offset = sv->sv_vdso_offset;
3882b313da3SKonstantin Belousov 	if ((sv2->sv_flags & SV_ABI_MASK) != SV_ABI_FREEBSD)
3892b313da3SKonstantin Belousov 		return;
390361971fbSKornel Dulęba 	sv2->sv_timekeep_offset = sv->sv_timekeep_offset;
391361971fbSKornel Dulęba 	sv2->sv_fxrng_gen_offset = sv->sv_fxrng_gen_offset;
3922b313da3SKonstantin Belousov }
393