Deleted Added
full compact
kern_sharedpage.c (248084) kern_sharedpage.c (254025)
1/*-
2 * Copyright (c) 2010, 2012 Konstantin Belousov <kib@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2010, 2012 Konstantin Belousov <kib@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/kern/kern_sharedpage.c 248084 2013-03-09 02:32:23Z attilio $");
28__FBSDID("$FreeBSD: head/sys/kern/kern_sharedpage.c 254025 2013-08-07 06:21:20Z jeff $");
29
30#include "opt_compat.h"
31#include "opt_vm.h"
32
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/kernel.h>
36#include <sys/lock.h>
37#include <sys/rwlock.h>
38#include <sys/sysent.h>
39#include <sys/sysctl.h>
40#include <sys/vdso.h>
41
42#include <vm/vm.h>
43#include <vm/vm_param.h>
44#include <vm/pmap.h>
45#include <vm/vm_extern.h>
46#include <vm/vm_kern.h>
47#include <vm/vm_map.h>
48#include <vm/vm_object.h>
49#include <vm/vm_page.h>
50#include <vm/vm_pager.h>
51
52static struct sx shared_page_alloc_sx;
53static vm_object_t shared_page_obj;
54static int shared_page_free;
55char *shared_page_mapping;
56
57void
58shared_page_write(int base, int size, const void *data)
59{
60
61 bcopy(data, shared_page_mapping + base, size);
62}
63
64static int
65shared_page_alloc_locked(int size, int align)
66{
67 int res;
68
69 res = roundup(shared_page_free, align);
70 if (res + size >= IDX_TO_OFF(shared_page_obj->size))
71 res = -1;
72 else
73 shared_page_free = res + size;
74 return (res);
75}
76
77int
78shared_page_alloc(int size, int align)
79{
80 int res;
81
82 sx_xlock(&shared_page_alloc_sx);
83 res = shared_page_alloc_locked(size, align);
84 sx_xunlock(&shared_page_alloc_sx);
85 return (res);
86}
87
88int
89shared_page_fill(int size, int align, const void *data)
90{
91 int res;
92
93 sx_xlock(&shared_page_alloc_sx);
94 res = shared_page_alloc_locked(size, align);
95 if (res != -1)
96 shared_page_write(res, size, data);
97 sx_xunlock(&shared_page_alloc_sx);
98 return (res);
99}
100
101static void
102shared_page_init(void *dummy __unused)
103{
104 vm_page_t m;
105 vm_offset_t addr;
106
107 sx_init(&shared_page_alloc_sx, "shpsx");
108 shared_page_obj = vm_pager_allocate(OBJT_PHYS, 0, PAGE_SIZE,
109 VM_PROT_DEFAULT, 0, NULL);
110 VM_OBJECT_WLOCK(shared_page_obj);
111 m = vm_page_grab(shared_page_obj, 0, VM_ALLOC_RETRY | VM_ALLOC_NOBUSY |
112 VM_ALLOC_ZERO);
113 m->valid = VM_PAGE_BITS_ALL;
114 VM_OBJECT_WUNLOCK(shared_page_obj);
29
30#include "opt_compat.h"
31#include "opt_vm.h"
32
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/kernel.h>
36#include <sys/lock.h>
37#include <sys/rwlock.h>
38#include <sys/sysent.h>
39#include <sys/sysctl.h>
40#include <sys/vdso.h>
41
42#include <vm/vm.h>
43#include <vm/vm_param.h>
44#include <vm/pmap.h>
45#include <vm/vm_extern.h>
46#include <vm/vm_kern.h>
47#include <vm/vm_map.h>
48#include <vm/vm_object.h>
49#include <vm/vm_page.h>
50#include <vm/vm_pager.h>
51
52static struct sx shared_page_alloc_sx;
53static vm_object_t shared_page_obj;
54static int shared_page_free;
55char *shared_page_mapping;
56
57void
58shared_page_write(int base, int size, const void *data)
59{
60
61 bcopy(data, shared_page_mapping + base, size);
62}
63
64static int
65shared_page_alloc_locked(int size, int align)
66{
67 int res;
68
69 res = roundup(shared_page_free, align);
70 if (res + size >= IDX_TO_OFF(shared_page_obj->size))
71 res = -1;
72 else
73 shared_page_free = res + size;
74 return (res);
75}
76
77int
78shared_page_alloc(int size, int align)
79{
80 int res;
81
82 sx_xlock(&shared_page_alloc_sx);
83 res = shared_page_alloc_locked(size, align);
84 sx_xunlock(&shared_page_alloc_sx);
85 return (res);
86}
87
88int
89shared_page_fill(int size, int align, const void *data)
90{
91 int res;
92
93 sx_xlock(&shared_page_alloc_sx);
94 res = shared_page_alloc_locked(size, align);
95 if (res != -1)
96 shared_page_write(res, size, data);
97 sx_xunlock(&shared_page_alloc_sx);
98 return (res);
99}
100
101static void
102shared_page_init(void *dummy __unused)
103{
104 vm_page_t m;
105 vm_offset_t addr;
106
107 sx_init(&shared_page_alloc_sx, "shpsx");
108 shared_page_obj = vm_pager_allocate(OBJT_PHYS, 0, PAGE_SIZE,
109 VM_PROT_DEFAULT, 0, NULL);
110 VM_OBJECT_WLOCK(shared_page_obj);
111 m = vm_page_grab(shared_page_obj, 0, VM_ALLOC_RETRY | VM_ALLOC_NOBUSY |
112 VM_ALLOC_ZERO);
113 m->valid = VM_PAGE_BITS_ALL;
114 VM_OBJECT_WUNLOCK(shared_page_obj);
115 addr = kmem_alloc_nofault(kernel_map, PAGE_SIZE);
115 addr = kva_alloc(PAGE_SIZE);
116 pmap_qenter(addr, &m, 1);
117 shared_page_mapping = (char *)addr;
118}
119
120SYSINIT(shp, SI_SUB_EXEC, SI_ORDER_FIRST, (sysinit_cfunc_t)shared_page_init,
121 NULL);
122
123static void
124timehands_update(struct sysentvec *sv)
125{
126 struct vdso_timehands th;
127 struct vdso_timekeep *tk;
128 uint32_t enabled, idx;
129
130 enabled = tc_fill_vdso_timehands(&th);
131 tk = (struct vdso_timekeep *)(shared_page_mapping +
132 sv->sv_timekeep_off);
133 idx = sv->sv_timekeep_curr;
134 atomic_store_rel_32(&tk->tk_th[idx].th_gen, 0);
135 if (++idx >= VDSO_TH_NUM)
136 idx = 0;
137 sv->sv_timekeep_curr = idx;
138 if (++sv->sv_timekeep_gen == 0)
139 sv->sv_timekeep_gen = 1;
140 th.th_gen = 0;
141 if (enabled)
142 tk->tk_th[idx] = th;
143 tk->tk_enabled = enabled;
144 atomic_store_rel_32(&tk->tk_th[idx].th_gen, sv->sv_timekeep_gen);
145 tk->tk_current = idx;
146}
147
148#ifdef COMPAT_FREEBSD32
149static void
150timehands_update32(struct sysentvec *sv)
151{
152 struct vdso_timekeep32 *tk;
153 struct vdso_timehands32 th;
154 uint32_t enabled, idx;
155
156 enabled = tc_fill_vdso_timehands32(&th);
157 tk = (struct vdso_timekeep32 *)(shared_page_mapping +
158 sv->sv_timekeep_off);
159 idx = sv->sv_timekeep_curr;
160 atomic_store_rel_32(&tk->tk_th[idx].th_gen, 0);
161 if (++idx >= VDSO_TH_NUM)
162 idx = 0;
163 sv->sv_timekeep_curr = idx;
164 if (++sv->sv_timekeep_gen == 0)
165 sv->sv_timekeep_gen = 1;
166 th.th_gen = 0;
167 if (enabled)
168 tk->tk_th[idx] = th;
169 tk->tk_enabled = enabled;
170 atomic_store_rel_32(&tk->tk_th[idx].th_gen, sv->sv_timekeep_gen);
171 tk->tk_current = idx;
172}
173#endif
174
175/*
176 * This is hackish, but easiest way to avoid creating list structures
177 * that needs to be iterated over from the hardclock interrupt
178 * context.
179 */
180static struct sysentvec *host_sysentvec;
181#ifdef COMPAT_FREEBSD32
182static struct sysentvec *compat32_sysentvec;
183#endif
184
185void
186timekeep_push_vdso(void)
187{
188
189 if (host_sysentvec != NULL && host_sysentvec->sv_timekeep_base != 0)
190 timehands_update(host_sysentvec);
191#ifdef COMPAT_FREEBSD32
192 if (compat32_sysentvec != NULL &&
193 compat32_sysentvec->sv_timekeep_base != 0)
194 timehands_update32(compat32_sysentvec);
195#endif
196}
197
198void
199exec_sysvec_init(void *param)
200{
201 struct sysentvec *sv;
202 int tk_base;
203 uint32_t tk_ver;
204
205 sv = (struct sysentvec *)param;
206
207 if ((sv->sv_flags & SV_SHP) == 0)
208 return;
209 sv->sv_shared_page_obj = shared_page_obj;
210 sv->sv_sigcode_base = sv->sv_shared_page_base +
211 shared_page_fill(*(sv->sv_szsigcode), 16, sv->sv_sigcode);
212 if ((sv->sv_flags & SV_ABI_MASK) != SV_ABI_FREEBSD)
213 return;
214 tk_ver = VDSO_TK_VER_CURR;
215#ifdef COMPAT_FREEBSD32
216 if ((sv->sv_flags & SV_ILP32) != 0) {
217 tk_base = shared_page_alloc(sizeof(struct vdso_timekeep32) +
218 sizeof(struct vdso_timehands32) * VDSO_TH_NUM, 16);
219 KASSERT(tk_base != -1, ("tk_base -1 for 32bit"));
220 shared_page_write(tk_base + offsetof(struct vdso_timekeep32,
221 tk_ver), sizeof(uint32_t), &tk_ver);
222 KASSERT(compat32_sysentvec == 0,
223 ("Native compat32 already registered"));
224 compat32_sysentvec = sv;
225 } else {
226#endif
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,
231 tk_ver), sizeof(uint32_t), &tk_ver);
232 KASSERT(host_sysentvec == 0, ("Native already registered"));
233 host_sysentvec = sv;
234#ifdef COMPAT_FREEBSD32
235 }
236#endif
237 sv->sv_timekeep_base = sv->sv_shared_page_base + tk_base;
238 sv->sv_timekeep_off = tk_base;
239 timekeep_push_vdso();
240}
116 pmap_qenter(addr, &m, 1);
117 shared_page_mapping = (char *)addr;
118}
119
120SYSINIT(shp, SI_SUB_EXEC, SI_ORDER_FIRST, (sysinit_cfunc_t)shared_page_init,
121 NULL);
122
123static void
124timehands_update(struct sysentvec *sv)
125{
126 struct vdso_timehands th;
127 struct vdso_timekeep *tk;
128 uint32_t enabled, idx;
129
130 enabled = tc_fill_vdso_timehands(&th);
131 tk = (struct vdso_timekeep *)(shared_page_mapping +
132 sv->sv_timekeep_off);
133 idx = sv->sv_timekeep_curr;
134 atomic_store_rel_32(&tk->tk_th[idx].th_gen, 0);
135 if (++idx >= VDSO_TH_NUM)
136 idx = 0;
137 sv->sv_timekeep_curr = idx;
138 if (++sv->sv_timekeep_gen == 0)
139 sv->sv_timekeep_gen = 1;
140 th.th_gen = 0;
141 if (enabled)
142 tk->tk_th[idx] = th;
143 tk->tk_enabled = enabled;
144 atomic_store_rel_32(&tk->tk_th[idx].th_gen, sv->sv_timekeep_gen);
145 tk->tk_current = idx;
146}
147
148#ifdef COMPAT_FREEBSD32
149static void
150timehands_update32(struct sysentvec *sv)
151{
152 struct vdso_timekeep32 *tk;
153 struct vdso_timehands32 th;
154 uint32_t enabled, idx;
155
156 enabled = tc_fill_vdso_timehands32(&th);
157 tk = (struct vdso_timekeep32 *)(shared_page_mapping +
158 sv->sv_timekeep_off);
159 idx = sv->sv_timekeep_curr;
160 atomic_store_rel_32(&tk->tk_th[idx].th_gen, 0);
161 if (++idx >= VDSO_TH_NUM)
162 idx = 0;
163 sv->sv_timekeep_curr = idx;
164 if (++sv->sv_timekeep_gen == 0)
165 sv->sv_timekeep_gen = 1;
166 th.th_gen = 0;
167 if (enabled)
168 tk->tk_th[idx] = th;
169 tk->tk_enabled = enabled;
170 atomic_store_rel_32(&tk->tk_th[idx].th_gen, sv->sv_timekeep_gen);
171 tk->tk_current = idx;
172}
173#endif
174
175/*
176 * This is hackish, but easiest way to avoid creating list structures
177 * that needs to be iterated over from the hardclock interrupt
178 * context.
179 */
180static struct sysentvec *host_sysentvec;
181#ifdef COMPAT_FREEBSD32
182static struct sysentvec *compat32_sysentvec;
183#endif
184
185void
186timekeep_push_vdso(void)
187{
188
189 if (host_sysentvec != NULL && host_sysentvec->sv_timekeep_base != 0)
190 timehands_update(host_sysentvec);
191#ifdef COMPAT_FREEBSD32
192 if (compat32_sysentvec != NULL &&
193 compat32_sysentvec->sv_timekeep_base != 0)
194 timehands_update32(compat32_sysentvec);
195#endif
196}
197
198void
199exec_sysvec_init(void *param)
200{
201 struct sysentvec *sv;
202 int tk_base;
203 uint32_t tk_ver;
204
205 sv = (struct sysentvec *)param;
206
207 if ((sv->sv_flags & SV_SHP) == 0)
208 return;
209 sv->sv_shared_page_obj = shared_page_obj;
210 sv->sv_sigcode_base = sv->sv_shared_page_base +
211 shared_page_fill(*(sv->sv_szsigcode), 16, sv->sv_sigcode);
212 if ((sv->sv_flags & SV_ABI_MASK) != SV_ABI_FREEBSD)
213 return;
214 tk_ver = VDSO_TK_VER_CURR;
215#ifdef COMPAT_FREEBSD32
216 if ((sv->sv_flags & SV_ILP32) != 0) {
217 tk_base = shared_page_alloc(sizeof(struct vdso_timekeep32) +
218 sizeof(struct vdso_timehands32) * VDSO_TH_NUM, 16);
219 KASSERT(tk_base != -1, ("tk_base -1 for 32bit"));
220 shared_page_write(tk_base + offsetof(struct vdso_timekeep32,
221 tk_ver), sizeof(uint32_t), &tk_ver);
222 KASSERT(compat32_sysentvec == 0,
223 ("Native compat32 already registered"));
224 compat32_sysentvec = sv;
225 } else {
226#endif
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,
231 tk_ver), sizeof(uint32_t), &tk_ver);
232 KASSERT(host_sysentvec == 0, ("Native already registered"));
233 host_sysentvec = sv;
234#ifdef COMPAT_FREEBSD32
235 }
236#endif
237 sv->sv_timekeep_base = sv->sv_shared_page_base + tk_base;
238 sv->sv_timekeep_off = tk_base;
239 timekeep_push_vdso();
240}