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/sysent.h>
45 #include <sys/sysctl.h>
49 #include <vm/vm_param.h>
51 #include <vm/vm_extern.h>
52 #include <vm/vm_kern.h>
53 #include <vm/vm_map.h>
54 #include <vm/vm_object.h>
55 #include <vm/vm_page.h>
56 #include <vm/vm_pager.h>
58 static struct sx shared_page_alloc_sx;
59 static vm_object_t shared_page_obj;
60 static int shared_page_free;
61 char *shared_page_mapping;
64 shared_page_write(int base, int size, const void *data)
67 bcopy(data, shared_page_mapping + base, size);
71 shared_page_alloc_locked(int size, int align)
75 res = roundup(shared_page_free, align);
76 if (res + size >= IDX_TO_OFF(shared_page_obj->size))
79 shared_page_free = res + size;
84 shared_page_alloc(int size, int align)
88 sx_xlock(&shared_page_alloc_sx);
89 res = shared_page_alloc_locked(size, align);
90 sx_xunlock(&shared_page_alloc_sx);
95 shared_page_fill(int size, int align, const void *data)
99 sx_xlock(&shared_page_alloc_sx);
100 res = shared_page_alloc_locked(size, align);
102 shared_page_write(res, size, data);
103 sx_xunlock(&shared_page_alloc_sx);
108 shared_page_init(void *dummy __unused)
113 sx_init(&shared_page_alloc_sx, "shpsx");
114 shared_page_obj = vm_pager_allocate(OBJT_PHYS, 0, PAGE_SIZE,
115 VM_PROT_DEFAULT, 0, NULL);
116 VM_OBJECT_WLOCK(shared_page_obj);
117 m = vm_page_grab(shared_page_obj, 0, VM_ALLOC_NOBUSY | VM_ALLOC_ZERO);
118 m->valid = VM_PAGE_BITS_ALL;
119 VM_OBJECT_WUNLOCK(shared_page_obj);
120 addr = kva_alloc(PAGE_SIZE);
121 pmap_qenter(addr, &m, 1);
122 shared_page_mapping = (char *)addr;
125 SYSINIT(shp, SI_SUB_EXEC, SI_ORDER_FIRST, (sysinit_cfunc_t)shared_page_init,
129 * Push the timehands update to the shared page.
131 * The lockless update scheme is similar to the one used to update the
132 * in-kernel timehands, see sys/kern/kern_tc.c:tc_windup() (which
133 * calls us after the timehands are updated).
136 timehands_update(struct vdso_sv_tk *svtk)
138 struct vdso_timehands th;
139 struct vdso_timekeep *tk;
140 uint32_t enabled, idx;
142 enabled = tc_fill_vdso_timehands(&th);
144 idx = svtk->sv_timekeep_curr;
145 if (++idx >= VDSO_TH_NUM)
147 svtk->sv_timekeep_curr = idx;
148 if (++svtk->sv_timekeep_gen == 0)
149 svtk->sv_timekeep_gen = 1;
151 tk = (struct vdso_timekeep *)(shared_page_mapping +
152 svtk->sv_timekeep_off);
153 tk->tk_th[idx].th_gen = 0;
154 atomic_thread_fence_rel();
157 atomic_store_rel_32(&tk->tk_th[idx].th_gen, svtk->sv_timekeep_gen);
158 atomic_store_rel_32(&tk->tk_current, idx);
161 * The ordering of the assignment to tk_enabled relative to
162 * the update of the vdso_timehands is not important.
164 tk->tk_enabled = enabled;
167 #ifdef COMPAT_FREEBSD32
169 timehands_update32(struct vdso_sv_tk *svtk)
171 struct vdso_timehands32 th;
172 struct vdso_timekeep32 *tk;
173 uint32_t enabled, idx;
175 enabled = tc_fill_vdso_timehands32(&th);
177 idx = svtk->sv_timekeep_curr;
178 if (++idx >= VDSO_TH_NUM)
180 svtk->sv_timekeep_curr = idx;
181 if (++svtk->sv_timekeep_gen == 0)
182 svtk->sv_timekeep_gen = 1;
184 tk = (struct vdso_timekeep32 *)(shared_page_mapping +
185 svtk->sv_timekeep_off);
186 tk->tk_th[idx].th_gen = 0;
187 atomic_thread_fence_rel();
190 atomic_store_rel_32(&tk->tk_th[idx].th_gen, svtk->sv_timekeep_gen);
191 atomic_store_rel_32(&tk->tk_current, idx);
192 tk->tk_enabled = enabled;
197 * This is hackish, but easiest way to avoid creating list structures
198 * that needs to be iterated over from the hardclock interrupt
201 static struct vdso_sv_tk *host_svtk;
202 #ifdef COMPAT_FREEBSD32
203 static struct vdso_sv_tk *compat32_svtk;
207 timekeep_push_vdso(void)
210 if (host_svtk != NULL)
211 timehands_update(host_svtk);
212 #ifdef COMPAT_FREEBSD32
213 if (compat32_svtk != NULL)
214 timehands_update32(compat32_svtk);
221 struct vdso_sv_tk *svtk;
225 tk_ver = VDSO_TK_VER_CURR;
226 svtk = malloc(sizeof(struct vdso_sv_tk), M_TEMP, M_WAITOK | M_ZERO);
227 tk_base = shared_page_alloc(sizeof(struct vdso_timekeep) +
228 sizeof(struct vdso_timehands) * VDSO_TH_NUM, 16);
229 KASSERT(tk_base != -1, ("tk_base -1 for native"));
230 shared_page_write(tk_base + offsetof(struct vdso_timekeep, tk_ver),
231 sizeof(uint32_t), &tk_ver);
232 svtk->sv_timekeep_off = tk_base;
233 timekeep_push_vdso();
237 #ifdef COMPAT_FREEBSD32
239 alloc_sv_tk_compat32(void)
241 struct vdso_sv_tk *svtk;
245 svtk = malloc(sizeof(struct vdso_sv_tk), M_TEMP, M_WAITOK | M_ZERO);
246 tk_ver = VDSO_TK_VER_CURR;
247 tk_base = shared_page_alloc(sizeof(struct vdso_timekeep32) +
248 sizeof(struct vdso_timehands32) * VDSO_TH_NUM, 16);
249 KASSERT(tk_base != -1, ("tk_base -1 for 32bit"));
250 shared_page_write(tk_base + offsetof(struct vdso_timekeep32,
251 tk_ver), sizeof(uint32_t), &tk_ver);
252 svtk->sv_timekeep_off = tk_base;
253 timekeep_push_vdso();
259 exec_sysvec_init(void *param)
261 struct sysentvec *sv;
263 sv = (struct sysentvec *)param;
264 if ((sv->sv_flags & SV_SHP) == 0)
266 sv->sv_shared_page_obj = shared_page_obj;
267 sv->sv_sigcode_base = sv->sv_shared_page_base +
268 shared_page_fill(*(sv->sv_szsigcode), 16, sv->sv_sigcode);
269 if ((sv->sv_flags & SV_ABI_MASK) != SV_ABI_FREEBSD)
271 if ((sv->sv_flags & SV_TIMEKEEP) != 0) {
272 #ifdef COMPAT_FREEBSD32
273 if ((sv->sv_flags & SV_ILP32) != 0) {
274 KASSERT(compat32_svtk == NULL,
275 ("Compat32 already registered"));
276 compat32_svtk = alloc_sv_tk_compat32();
277 sv->sv_timekeep_base = sv->sv_shared_page_base +
278 compat32_svtk->sv_timekeep_off;
281 KASSERT(host_svtk == NULL, ("Host already registered"));
282 host_svtk = alloc_sv_tk();
283 sv->sv_timekeep_base = sv->sv_shared_page_base +
284 host_svtk->sv_timekeep_off;
285 #ifdef COMPAT_FREEBSD32