1139804Simp/*-
2237477Skib * Copyright (c) 2010, 2012 Konstantin Belousov <kib@FreeBSD.org>
31549Srgrimes * All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes *
141549Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
151541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
161541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
171549Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
181541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
191541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
201541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
211541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
221541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
231541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
241541Srgrimes * SUCH DAMAGE.
251541Srgrimes */
261541Srgrimes
27116182Sobrien#include <sys/cdefs.h>
28116182Sobrien__FBSDID("$FreeBSD$");
29116182Sobrien
30237433Skib#include "opt_compat.h"
31174982Salc#include "opt_vm.h"
3299226Speter
331541Srgrimes#include <sys/param.h>
341549Srgrimes#include <sys/systm.h>
35237477Skib#include <sys/kernel.h>
3676166Smarkm#include <sys/lock.h>
37248084Sattilio#include <sys/rwlock.h>
386380Ssos#include <sys/sysent.h>
3914235Speter#include <sys/sysctl.h>
40237433Skib#include <sys/vdso.h>
411541Srgrimes
421549Srgrimes#include <vm/vm.h>
4312662Sdg#include <vm/vm_param.h>
4412662Sdg#include <vm/pmap.h>
45237477Skib#include <vm/vm_extern.h>
46237477Skib#include <vm/vm_kern.h>
4712662Sdg#include <vm/vm_map.h>
4824994Sdg#include <vm/vm_object.h>
49237477Skib#include <vm/vm_page.h>
5032446Sdyson#include <vm/vm_pager.h>
511549Srgrimes
52237431Skibstatic struct sx shared_page_alloc_sx;
53217151Skibstatic vm_object_t shared_page_obj;
54217151Skibstatic int shared_page_free;
55237474Skibchar *shared_page_mapping;
56217151Skib
57237431Skibvoid
58237431Skibshared_page_write(int base, int size, const void *data)
59237431Skib{
60237431Skib
61237474Skib	bcopy(data, shared_page_mapping + base, size);
62237431Skib}
63237431Skib
64237431Skibstatic int
65237431Skibshared_page_alloc_locked(int size, int align)
66237431Skib{
67217151Skib	int res;
68217151Skib
69217151Skib	res = roundup(shared_page_free, align);
70217151Skib	if (res + size >= IDX_TO_OFF(shared_page_obj->size))
71217151Skib		res = -1;
72237431Skib	else
73217151Skib		shared_page_free = res + size;
74217151Skib	return (res);
75217151Skib}
76217151Skib
77237431Skibint
78237431Skibshared_page_alloc(int size, int align)
79237431Skib{
80237431Skib	int res;
81237431Skib
82237431Skib	sx_xlock(&shared_page_alloc_sx);
83237431Skib	res = shared_page_alloc_locked(size, align);
84237431Skib	sx_xunlock(&shared_page_alloc_sx);
85237431Skib	return (res);
86237431Skib}
87237431Skib
88237431Skibint
89237431Skibshared_page_fill(int size, int align, const void *data)
90237431Skib{
91237431Skib	int res;
92237431Skib
93237431Skib	sx_xlock(&shared_page_alloc_sx);
94237431Skib	res = shared_page_alloc_locked(size, align);
95237431Skib	if (res != -1)
96237431Skib		shared_page_write(res, size, data);
97237431Skib	sx_xunlock(&shared_page_alloc_sx);
98237431Skib	return (res);
99237431Skib}
100237431Skib
101217151Skibstatic void
102217151Skibshared_page_init(void *dummy __unused)
103217151Skib{
104217151Skib	vm_page_t m;
105237474Skib	vm_offset_t addr;
106217151Skib
107237431Skib	sx_init(&shared_page_alloc_sx, "shpsx");
108217151Skib	shared_page_obj = vm_pager_allocate(OBJT_PHYS, 0, PAGE_SIZE,
109217151Skib	    VM_PROT_DEFAULT, 0, NULL);
110248084Sattilio	VM_OBJECT_WLOCK(shared_page_obj);
111254649Skib	m = vm_page_grab(shared_page_obj, 0, VM_ALLOC_NOBUSY | VM_ALLOC_ZERO);
112217151Skib	m->valid = VM_PAGE_BITS_ALL;
113248084Sattilio	VM_OBJECT_WUNLOCK(shared_page_obj);
114254025Sjeff	addr = kva_alloc(PAGE_SIZE);
115237474Skib	pmap_qenter(addr, &m, 1);
116237474Skib	shared_page_mapping = (char *)addr;
117217151Skib}
118217151Skib
119217151SkibSYSINIT(shp, SI_SUB_EXEC, SI_ORDER_FIRST, (sysinit_cfunc_t)shared_page_init,
120217151Skib    NULL);
121217151Skib
122237433Skibstatic void
123237474Skibtimehands_update(struct sysentvec *sv)
124237433Skib{
125237433Skib	struct vdso_timehands th;
126237433Skib	struct vdso_timekeep *tk;
127237433Skib	uint32_t enabled, idx;
128237433Skib
129237433Skib	enabled = tc_fill_vdso_timehands(&th);
130237474Skib	tk = (struct vdso_timekeep *)(shared_page_mapping +
131237474Skib	    sv->sv_timekeep_off);
132237433Skib	idx = sv->sv_timekeep_curr;
133237433Skib	atomic_store_rel_32(&tk->tk_th[idx].th_gen, 0);
134237433Skib	if (++idx >= VDSO_TH_NUM)
135237433Skib		idx = 0;
136237433Skib	sv->sv_timekeep_curr = idx;
137237433Skib	if (++sv->sv_timekeep_gen == 0)
138237433Skib		sv->sv_timekeep_gen = 1;
139237433Skib	th.th_gen = 0;
140237433Skib	if (enabled)
141237433Skib		tk->tk_th[idx] = th;
142237433Skib	tk->tk_enabled = enabled;
143237433Skib	atomic_store_rel_32(&tk->tk_th[idx].th_gen, sv->sv_timekeep_gen);
144237433Skib	tk->tk_current = idx;
145237433Skib}
146237433Skib
147237433Skib#ifdef COMPAT_FREEBSD32
148237433Skibstatic void
149237474Skibtimehands_update32(struct sysentvec *sv)
150237433Skib{
151237433Skib	struct vdso_timekeep32 *tk;
152237433Skib	struct vdso_timehands32 th;
153237433Skib	uint32_t enabled, idx;
154237433Skib
155237433Skib	enabled = tc_fill_vdso_timehands32(&th);
156237474Skib	tk = (struct vdso_timekeep32 *)(shared_page_mapping +
157237474Skib	    sv->sv_timekeep_off);
158237433Skib	idx = sv->sv_timekeep_curr;
159237433Skib	atomic_store_rel_32(&tk->tk_th[idx].th_gen, 0);
160237433Skib	if (++idx >= VDSO_TH_NUM)
161237433Skib		idx = 0;
162237433Skib	sv->sv_timekeep_curr = idx;
163237433Skib	if (++sv->sv_timekeep_gen == 0)
164237433Skib		sv->sv_timekeep_gen = 1;
165237433Skib	th.th_gen = 0;
166237433Skib	if (enabled)
167237433Skib		tk->tk_th[idx] = th;
168237433Skib	tk->tk_enabled = enabled;
169237433Skib	atomic_store_rel_32(&tk->tk_th[idx].th_gen, sv->sv_timekeep_gen);
170237433Skib	tk->tk_current = idx;
171237433Skib}
172237433Skib#endif
173237433Skib
174237474Skib/*
175237474Skib * This is hackish, but easiest way to avoid creating list structures
176237474Skib * that needs to be iterated over from the hardclock interrupt
177237474Skib * context.
178237474Skib */
179237474Skibstatic struct sysentvec *host_sysentvec;
180237474Skib#ifdef COMPAT_FREEBSD32
181237474Skibstatic struct sysentvec *compat32_sysentvec;
182237474Skib#endif
183237474Skib
184217151Skibvoid
185237474Skibtimekeep_push_vdso(void)
186237474Skib{
187237474Skib
188237474Skib	if (host_sysentvec != NULL && host_sysentvec->sv_timekeep_base != 0)
189237474Skib		timehands_update(host_sysentvec);
190237474Skib#ifdef COMPAT_FREEBSD32
191237474Skib	if (compat32_sysentvec != NULL &&
192237474Skib	    compat32_sysentvec->sv_timekeep_base != 0)
193237474Skib		timehands_update32(compat32_sysentvec);
194237474Skib#endif
195237474Skib}
196237474Skib
197237474Skibvoid
198217151Skibexec_sysvec_init(void *param)
199217151Skib{
200217151Skib	struct sysentvec *sv;
201237433Skib	int tk_base;
202237433Skib	uint32_t tk_ver;
203217151Skib
204217151Skib	sv = (struct sysentvec *)param;
205217151Skib
206217151Skib	if ((sv->sv_flags & SV_SHP) == 0)
207217151Skib		return;
208217151Skib	sv->sv_shared_page_obj = shared_page_obj;
209217151Skib	sv->sv_sigcode_base = sv->sv_shared_page_base +
210217151Skib	    shared_page_fill(*(sv->sv_szsigcode), 16, sv->sv_sigcode);
211237474Skib	if ((sv->sv_flags & SV_ABI_MASK) != SV_ABI_FREEBSD)
212237474Skib		return;
213237433Skib	tk_ver = VDSO_TK_VER_CURR;
214237433Skib#ifdef COMPAT_FREEBSD32
215237433Skib	if ((sv->sv_flags & SV_ILP32) != 0) {
216237433Skib		tk_base = shared_page_alloc(sizeof(struct vdso_timekeep32) +
217237433Skib		    sizeof(struct vdso_timehands32) * VDSO_TH_NUM, 16);
218237433Skib		KASSERT(tk_base != -1, ("tk_base -1 for 32bit"));
219237433Skib		shared_page_write(tk_base + offsetof(struct vdso_timekeep32,
220237433Skib		    tk_ver), sizeof(uint32_t), &tk_ver);
221237474Skib		KASSERT(compat32_sysentvec == 0,
222237474Skib		    ("Native compat32 already registered"));
223237474Skib		compat32_sysentvec = sv;
224237433Skib	} else {
225237433Skib#endif
226237433Skib		tk_base = shared_page_alloc(sizeof(struct vdso_timekeep) +
227237433Skib		    sizeof(struct vdso_timehands) * VDSO_TH_NUM, 16);
228237433Skib		KASSERT(tk_base != -1, ("tk_base -1 for native"));
229237433Skib		shared_page_write(tk_base + offsetof(struct vdso_timekeep,
230237433Skib		    tk_ver), sizeof(uint32_t), &tk_ver);
231237474Skib		KASSERT(host_sysentvec == 0, ("Native already registered"));
232237474Skib		host_sysentvec = sv;
233237433Skib#ifdef COMPAT_FREEBSD32
234237433Skib	}
235237433Skib#endif
236237433Skib	sv->sv_timekeep_base = sv->sv_shared_page_base + tk_base;
237237433Skib	sv->sv_timekeep_off = tk_base;
238237474Skib	timekeep_push_vdso();
239217151Skib}
240