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$");
36 #include "opt_compat.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/rwlock.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;
65 shared_page_write(int base, int size, const void *data)
68 bcopy(data, shared_page_mapping + base, size);
72 shared_page_alloc_locked(int size, int align)
76 res = roundup(shared_page_free, align);
77 if (res + size >= IDX_TO_OFF(shared_page_obj->size))
80 shared_page_free = res + size;
85 shared_page_alloc(int size, int align)
89 sx_xlock(&shared_page_alloc_sx);
90 res = shared_page_alloc_locked(size, align);
91 sx_xunlock(&shared_page_alloc_sx);
96 shared_page_fill(int size, int align, const void *data)
100 sx_xlock(&shared_page_alloc_sx);
101 res = shared_page_alloc_locked(size, align);
103 shared_page_write(res, size, data);
104 sx_xunlock(&shared_page_alloc_sx);
109 shared_page_init(void *dummy __unused)
114 sx_init(&shared_page_alloc_sx, "shpsx");
115 shared_page_obj = vm_pager_allocate(OBJT_PHYS, 0, PAGE_SIZE,
116 VM_PROT_DEFAULT, 0, NULL);
117 VM_OBJECT_WLOCK(shared_page_obj);
118 m = vm_page_grab(shared_page_obj, 0, VM_ALLOC_NOBUSY | VM_ALLOC_ZERO);
119 m->valid = VM_PAGE_BITS_ALL;
120 VM_OBJECT_WUNLOCK(shared_page_obj);
121 addr = kva_alloc(PAGE_SIZE);
122 pmap_qenter(addr, &m, 1);
123 shared_page_mapping = (char *)addr;
126 SYSINIT(shp, SI_SUB_EXEC, SI_ORDER_FIRST, (sysinit_cfunc_t)shared_page_init,
130 * Push the timehands update to the shared page.
132 * The lockless update scheme is similar to the one used to update the
133 * in-kernel timehands, see sys/kern/kern_tc.c:tc_windup() (which
134 * calls us after the timehands are updated).
137 timehands_update(struct vdso_sv_tk *svtk)
139 struct vdso_timehands th;
140 struct vdso_timekeep *tk;
141 uint32_t enabled, idx;
143 enabled = tc_fill_vdso_timehands(&th);
145 idx = svtk->sv_timekeep_curr;
146 if (++idx >= VDSO_TH_NUM)
148 svtk->sv_timekeep_curr = idx;
149 if (++svtk->sv_timekeep_gen == 0)
150 svtk->sv_timekeep_gen = 1;
152 tk = (struct vdso_timekeep *)(shared_page_mapping +
153 svtk->sv_timekeep_off);
154 tk->tk_th[idx].th_gen = 0;
155 atomic_thread_fence_rel();
158 atomic_store_rel_32(&tk->tk_th[idx].th_gen, svtk->sv_timekeep_gen);
159 atomic_store_rel_32(&tk->tk_current, idx);
162 * The ordering of the assignment to tk_enabled relative to
163 * the update of the vdso_timehands is not important.
165 tk->tk_enabled = enabled;
168 #ifdef COMPAT_FREEBSD32
170 timehands_update32(struct vdso_sv_tk *svtk)
172 struct vdso_timehands32 th;
173 struct vdso_timekeep32 *tk;
174 uint32_t enabled, idx;
176 enabled = tc_fill_vdso_timehands32(&th);
178 idx = svtk->sv_timekeep_curr;
179 if (++idx >= VDSO_TH_NUM)
181 svtk->sv_timekeep_curr = idx;
182 if (++svtk->sv_timekeep_gen == 0)
183 svtk->sv_timekeep_gen = 1;
185 tk = (struct vdso_timekeep32 *)(shared_page_mapping +
186 svtk->sv_timekeep_off);
187 tk->tk_th[idx].th_gen = 0;
188 atomic_thread_fence_rel();
191 atomic_store_rel_32(&tk->tk_th[idx].th_gen, svtk->sv_timekeep_gen);
192 atomic_store_rel_32(&tk->tk_current, idx);
193 tk->tk_enabled = enabled;
198 * This is hackish, but easiest way to avoid creating list structures
199 * that needs to be iterated over from the hardclock interrupt
202 static struct vdso_sv_tk *host_svtk;
203 #ifdef COMPAT_FREEBSD32
204 static struct vdso_sv_tk *compat32_svtk;
208 timekeep_push_vdso(void)
211 if (host_svtk != NULL)
212 timehands_update(host_svtk);
213 #ifdef COMPAT_FREEBSD32
214 if (compat32_svtk != NULL)
215 timehands_update32(compat32_svtk);
222 struct vdso_sv_tk *svtk;
226 tk_ver = VDSO_TK_VER_CURR;
227 svtk = malloc(sizeof(struct vdso_sv_tk), M_TEMP, M_WAITOK | M_ZERO);
228 tk_base = shared_page_alloc(sizeof(struct vdso_timekeep) +
229 sizeof(struct vdso_timehands) * VDSO_TH_NUM, 16);
230 KASSERT(tk_base != -1, ("tk_base -1 for native"));
231 shared_page_write(tk_base + offsetof(struct vdso_timekeep, tk_ver),
232 sizeof(uint32_t), &tk_ver);
233 svtk->sv_timekeep_off = tk_base;
234 timekeep_push_vdso();
238 #ifdef COMPAT_FREEBSD32
240 alloc_sv_tk_compat32(void)
242 struct vdso_sv_tk *svtk;
246 svtk = malloc(sizeof(struct vdso_sv_tk), M_TEMP, M_WAITOK | M_ZERO);
247 tk_ver = VDSO_TK_VER_CURR;
248 tk_base = shared_page_alloc(sizeof(struct vdso_timekeep32) +
249 sizeof(struct vdso_timehands32) * VDSO_TH_NUM, 16);
250 KASSERT(tk_base != -1, ("tk_base -1 for 32bit"));
251 shared_page_write(tk_base + offsetof(struct vdso_timekeep32,
252 tk_ver), sizeof(uint32_t), &tk_ver);
253 svtk->sv_timekeep_off = tk_base;
254 timekeep_push_vdso();
260 exec_sysvec_init(void *param)
262 struct sysentvec *sv;
264 sv = (struct sysentvec *)param;
265 if ((sv->sv_flags & SV_SHP) == 0)
267 sv->sv_shared_page_obj = shared_page_obj;
268 sv->sv_sigcode_base = sv->sv_shared_page_base +
269 shared_page_fill(*(sv->sv_szsigcode), 16, sv->sv_sigcode);
270 if ((sv->sv_flags & SV_ABI_MASK) != SV_ABI_FREEBSD)
272 if ((sv->sv_flags & SV_TIMEKEEP) != 0) {
273 #ifdef COMPAT_FREEBSD32
274 if ((sv->sv_flags & SV_ILP32) != 0) {
275 KASSERT(compat32_svtk == NULL,
276 ("Compat32 already registered"));
277 compat32_svtk = alloc_sv_tk_compat32();
278 sv->sv_timekeep_base = sv->sv_shared_page_base +
279 compat32_svtk->sv_timekeep_off;
282 KASSERT(host_svtk == NULL, ("Host already registered"));
283 host_svtk = alloc_sv_tk();
284 sv->sv_timekeep_base = sv->sv_shared_page_base +
285 host_svtk->sv_timekeep_off;
286 #ifdef COMPAT_FREEBSD32