sysv_shm.c revision 1.77 1 1.77 simonb /* $NetBSD: sysv_shm.c,v 1.77 2004/04/25 16:42:41 simonb Exp $ */
2 1.52 thorpej
3 1.52 thorpej /*-
4 1.52 thorpej * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 1.52 thorpej * All rights reserved.
6 1.52 thorpej *
7 1.52 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.52 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.52 thorpej * NASA Ames Research Center.
10 1.52 thorpej *
11 1.52 thorpej * Redistribution and use in source and binary forms, with or without
12 1.52 thorpej * modification, are permitted provided that the following conditions
13 1.52 thorpej * are met:
14 1.52 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.52 thorpej * notice, this list of conditions and the following disclaimer.
16 1.52 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.52 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.52 thorpej * documentation and/or other materials provided with the distribution.
19 1.52 thorpej * 3. All advertising materials mentioning features or use of this software
20 1.52 thorpej * must display the following acknowledgement:
21 1.52 thorpej * This product includes software developed by the NetBSD
22 1.52 thorpej * Foundation, Inc. and its contributors.
23 1.52 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.52 thorpej * contributors may be used to endorse or promote products derived
25 1.52 thorpej * from this software without specific prior written permission.
26 1.52 thorpej *
27 1.52 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.52 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.52 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.52 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.52 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.52 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.52 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.52 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.52 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.52 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.52 thorpej * POSSIBILITY OF SUCH DAMAGE.
38 1.52 thorpej */
39 1.22 cgd
40 1.11 hpeyerl /*
41 1.48 mycroft * Copyright (c) 1994 Adam Glass and Charles M. Hannum. All rights reserved.
42 1.11 hpeyerl *
43 1.11 hpeyerl * Redistribution and use in source and binary forms, with or without
44 1.11 hpeyerl * modification, are permitted provided that the following conditions
45 1.11 hpeyerl * are met:
46 1.11 hpeyerl * 1. Redistributions of source code must retain the above copyright
47 1.11 hpeyerl * notice, this list of conditions and the following disclaimer.
48 1.17 mycroft * 2. Redistributions in binary form must reproduce the above copyright
49 1.17 mycroft * notice, this list of conditions and the following disclaimer in the
50 1.17 mycroft * documentation and/or other materials provided with the distribution.
51 1.17 mycroft * 3. All advertising materials mentioning features or use of this software
52 1.17 mycroft * must display the following acknowledgement:
53 1.48 mycroft * This product includes software developed by Adam Glass and Charles M.
54 1.17 mycroft * Hannum.
55 1.17 mycroft * 4. The names of the authors may not be used to endorse or promote products
56 1.11 hpeyerl * derived from this software without specific prior written permission.
57 1.11 hpeyerl *
58 1.17 mycroft * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
59 1.17 mycroft * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
60 1.17 mycroft * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
61 1.17 mycroft * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
62 1.17 mycroft * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
63 1.17 mycroft * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
64 1.17 mycroft * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
65 1.17 mycroft * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
66 1.17 mycroft * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
67 1.17 mycroft * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68 1.11 hpeyerl */
69 1.62 lukem
70 1.62 lukem #include <sys/cdefs.h>
71 1.77 simonb __KERNEL_RCSID(0, "$NetBSD: sysv_shm.c,v 1.77 2004/04/25 16:42:41 simonb Exp $");
72 1.43 mrg
73 1.50 tron #define SYSVSHM
74 1.11 hpeyerl
75 1.11 hpeyerl #include <sys/param.h>
76 1.11 hpeyerl #include <sys/kernel.h>
77 1.11 hpeyerl #include <sys/shm.h>
78 1.11 hpeyerl #include <sys/malloc.h>
79 1.11 hpeyerl #include <sys/mman.h>
80 1.12 mycroft #include <sys/stat.h>
81 1.56 simonb #include <sys/sysctl.h>
82 1.56 simonb #include <sys/mount.h> /* XXX for <sys/syscallargs.h> */
83 1.65 thorpej #include <sys/sa.h>
84 1.56 simonb #include <sys/syscallargs.h>
85 1.69 drochner #include <sys/queue.h>
86 1.69 drochner #include <sys/pool.h>
87 1.35 christos
88 1.60 thorpej #include <uvm/uvm_extern.h>
89 1.75 christos #include <uvm/uvm_object.h>
90 1.60 thorpej
91 1.76 junyoung struct shmid_ds *shm_find_segment_by_shmid(int, int);
92 1.67 thorpej
93 1.71 jdolecek static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments");
94 1.35 christos
95 1.11 hpeyerl /*
96 1.11 hpeyerl * Provides the following externally accessible functions:
97 1.11 hpeyerl *
98 1.41 thorpej * shminit(void); initialization
99 1.41 thorpej * shmexit(struct vmspace *) cleanup
100 1.41 thorpej * shmfork(struct vmspace *, struct vmspace *) fork handling
101 1.11 hpeyerl *
102 1.11 hpeyerl * Structures:
103 1.11 hpeyerl * shmsegs (an array of 'struct shmid_ds')
104 1.11 hpeyerl * per proc array of 'struct shmmap_state'
105 1.11 hpeyerl */
106 1.12 mycroft
107 1.16 mycroft #define SHMSEG_FREE 0x0200
108 1.16 mycroft #define SHMSEG_REMOVED 0x0400
109 1.16 mycroft #define SHMSEG_ALLOCATED 0x0800
110 1.16 mycroft #define SHMSEG_WANTED 0x1000
111 1.11 hpeyerl
112 1.71 jdolecek static int shm_last_free, shm_nused, shm_committed;
113 1.55 simonb struct shmid_ds *shmsegs;
114 1.11 hpeyerl
115 1.11 hpeyerl struct shm_handle {
116 1.42 mrg struct uvm_object *shm_object;
117 1.11 hpeyerl };
118 1.11 hpeyerl
119 1.69 drochner struct shmmap_entry {
120 1.69 drochner SLIST_ENTRY(shmmap_entry) next;
121 1.47 eeh vaddr_t va;
122 1.11 hpeyerl int shmid;
123 1.11 hpeyerl };
124 1.11 hpeyerl
125 1.77 simonb static POOL_INIT(shmmap_entry_pool, sizeof(struct shmmap_entry), 0, 0, 0,
126 1.77 simonb "shmmp", 0);
127 1.69 drochner
128 1.69 drochner struct shmmap_state {
129 1.69 drochner unsigned int nitems;
130 1.69 drochner unsigned int nrefs;
131 1.69 drochner SLIST_HEAD(, shmmap_entry) entries;
132 1.69 drochner };
133 1.69 drochner
134 1.76 junyoung static int shm_find_segment_by_key(key_t);
135 1.76 junyoung static void shm_deallocate_segment(struct shmid_ds *);
136 1.76 junyoung static void shm_delete_mapping(struct vmspace *, struct shmmap_state *,
137 1.76 junyoung struct shmmap_entry *);
138 1.76 junyoung static int shmget_existing(struct proc *, struct sys_shmget_args *,
139 1.76 junyoung int, int, register_t *);
140 1.76 junyoung static int shmget_allocate_segment(struct proc *, struct sys_shmget_args *,
141 1.76 junyoung int, register_t *);
142 1.76 junyoung static struct shmmap_state *shmmap_getprivate(struct proc *);
143 1.76 junyoung static struct shmmap_entry *shm_find_mapping(struct shmmap_state *, vaddr_t);
144 1.11 hpeyerl
145 1.12 mycroft static int
146 1.12 mycroft shm_find_segment_by_key(key)
147 1.11 hpeyerl key_t key;
148 1.11 hpeyerl {
149 1.11 hpeyerl int i;
150 1.11 hpeyerl
151 1.12 mycroft for (i = 0; i < shminfo.shmmni; i++)
152 1.12 mycroft if ((shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED) &&
153 1.52 thorpej shmsegs[i].shm_perm._key == key)
154 1.12 mycroft return i;
155 1.11 hpeyerl return -1;
156 1.11 hpeyerl }
157 1.11 hpeyerl
158 1.28 christos struct shmid_ds *
159 1.64 fvdl shm_find_segment_by_shmid(shmid, findremoved)
160 1.11 hpeyerl int shmid;
161 1.64 fvdl int findremoved;
162 1.11 hpeyerl {
163 1.11 hpeyerl int segnum;
164 1.11 hpeyerl struct shmid_ds *shmseg;
165 1.11 hpeyerl
166 1.11 hpeyerl segnum = IPCID_TO_IX(shmid);
167 1.12 mycroft if (segnum < 0 || segnum >= shminfo.shmmni)
168 1.11 hpeyerl return NULL;
169 1.11 hpeyerl shmseg = &shmsegs[segnum];
170 1.64 fvdl if ((shmseg->shm_perm.mode & SHMSEG_ALLOCATED) == 0)
171 1.64 fvdl return NULL;
172 1.64 fvdl if (!findremoved && ((shmseg->shm_perm.mode & SHMSEG_REMOVED) != 0))
173 1.64 fvdl return NULL;
174 1.64 fvdl if (shmseg->shm_perm._seq != IPCID_TO_SEQ(shmid))
175 1.11 hpeyerl return NULL;
176 1.11 hpeyerl return shmseg;
177 1.11 hpeyerl }
178 1.11 hpeyerl
179 1.12 mycroft static void
180 1.12 mycroft shm_deallocate_segment(shmseg)
181 1.12 mycroft struct shmid_ds *shmseg;
182 1.12 mycroft {
183 1.74 christos struct shm_handle *shm_handle = shmseg->_shm_internal;
184 1.74 christos struct uvm_object *uobj = shm_handle->shm_object;
185 1.74 christos size_t size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
186 1.12 mycroft
187 1.74 christos (*uobj->pgops->pgo_detach)(uobj);
188 1.12 mycroft free((caddr_t)shm_handle, M_SHM);
189 1.52 thorpej shmseg->_shm_internal = NULL;
190 1.14 mycroft shm_committed -= btoc(size);
191 1.12 mycroft shmseg->shm_perm.mode = SHMSEG_FREE;
192 1.25 mycroft shm_nused--;
193 1.12 mycroft }
194 1.12 mycroft
195 1.61 chs static void
196 1.69 drochner shm_delete_mapping(vm, shmmap_s, shmmap_se)
197 1.41 thorpej struct vmspace *vm;
198 1.11 hpeyerl struct shmmap_state *shmmap_s;
199 1.69 drochner struct shmmap_entry *shmmap_se;
200 1.11 hpeyerl {
201 1.12 mycroft struct shmid_ds *shmseg;
202 1.61 chs int segnum;
203 1.14 mycroft size_t size;
204 1.76 junyoung
205 1.69 drochner segnum = IPCID_TO_IX(shmmap_se->shmid);
206 1.72 jdolecek #ifdef DEBUG
207 1.72 jdolecek if (segnum < 0 || segnum >= shminfo.shmmni)
208 1.73 simonb panic("shm_delete_mapping: vmspace %p state %p entry %p - "
209 1.73 simonb "entry segment ID bad (%d)",
210 1.73 simonb vm, shmmap_s, shmmap_se, segnum);
211 1.72 jdolecek #endif
212 1.12 mycroft shmseg = &shmsegs[segnum];
213 1.53 ragge size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
214 1.69 drochner uvm_deallocate(&vm->vm_map, shmmap_se->va, size);
215 1.69 drochner SLIST_REMOVE(&shmmap_s->entries, shmmap_se, shmmap_entry, next);
216 1.69 drochner shmmap_s->nitems--;
217 1.69 drochner pool_put(&shmmap_entry_pool, shmmap_se);
218 1.11 hpeyerl shmseg->shm_dtime = time.tv_sec;
219 1.12 mycroft if ((--shmseg->shm_nattch <= 0) &&
220 1.11 hpeyerl (shmseg->shm_perm.mode & SHMSEG_REMOVED)) {
221 1.12 mycroft shm_deallocate_segment(shmseg);
222 1.12 mycroft shm_last_free = segnum;
223 1.11 hpeyerl }
224 1.11 hpeyerl }
225 1.11 hpeyerl
226 1.69 drochner /*
227 1.69 drochner * Get a non-shared shm map for that vmspace.
228 1.69 drochner * 3 cases:
229 1.69 drochner * - no shm map present: create a fresh one
230 1.69 drochner * - a shm map with refcount=1, just used by ourselves: fine
231 1.69 drochner * - a shared shm map: copy to a fresh one and adjust refcounts
232 1.69 drochner */
233 1.69 drochner static struct shmmap_state *
234 1.69 drochner shmmap_getprivate(struct proc *p)
235 1.69 drochner {
236 1.69 drochner struct shmmap_state *oshmmap_s, *shmmap_s;
237 1.69 drochner struct shmmap_entry *oshmmap_se, *shmmap_se;
238 1.69 drochner
239 1.69 drochner oshmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
240 1.69 drochner if (oshmmap_s && oshmmap_s->nrefs == 1)
241 1.69 drochner return (oshmmap_s);
242 1.69 drochner
243 1.69 drochner shmmap_s = malloc(sizeof(struct shmmap_state), M_SHM, M_WAITOK);
244 1.69 drochner memset(shmmap_s, 0, sizeof(struct shmmap_state));
245 1.69 drochner shmmap_s->nrefs = 1;
246 1.69 drochner SLIST_INIT(&shmmap_s->entries);
247 1.69 drochner p->p_vmspace->vm_shm = (caddr_t)shmmap_s;
248 1.69 drochner
249 1.69 drochner if (!oshmmap_s)
250 1.69 drochner return (shmmap_s);
251 1.69 drochner
252 1.69 drochner #ifdef SHMDEBUG
253 1.70 drochner printf("shmmap_getprivate: vm %p split (%d entries), was used by %d\n",
254 1.69 drochner p->p_vmspace, oshmmap_s->nitems, oshmmap_s->nrefs);
255 1.69 drochner #endif
256 1.69 drochner SLIST_FOREACH(oshmmap_se, &oshmmap_s->entries, next) {
257 1.69 drochner shmmap_se = pool_get(&shmmap_entry_pool, PR_WAITOK);
258 1.69 drochner shmmap_se->va = oshmmap_se->va;
259 1.69 drochner shmmap_se->shmid = oshmmap_se->shmid;
260 1.69 drochner SLIST_INSERT_HEAD(&shmmap_s->entries, shmmap_se, next);
261 1.69 drochner }
262 1.69 drochner shmmap_s->nitems = oshmmap_s->nitems;
263 1.69 drochner oshmmap_s->nrefs--;
264 1.69 drochner return (shmmap_s);
265 1.69 drochner }
266 1.69 drochner
267 1.70 drochner static struct shmmap_entry *
268 1.70 drochner shm_find_mapping(map, va)
269 1.70 drochner struct shmmap_state *map;
270 1.70 drochner vaddr_t va;
271 1.70 drochner {
272 1.70 drochner struct shmmap_entry *shmmap_se;
273 1.70 drochner
274 1.70 drochner SLIST_FOREACH(shmmap_se, &map->entries, next) {
275 1.70 drochner if (shmmap_se->va == va)
276 1.70 drochner return shmmap_se;
277 1.70 drochner }
278 1.70 drochner return 0;
279 1.70 drochner }
280 1.70 drochner
281 1.12 mycroft int
282 1.65 thorpej sys_shmdt(l, v, retval)
283 1.65 thorpej struct lwp *l;
284 1.32 thorpej void *v;
285 1.32 thorpej register_t *retval;
286 1.32 thorpej {
287 1.33 mycroft struct sys_shmdt_args /* {
288 1.44 kleink syscallarg(const void *) shmaddr;
289 1.32 thorpej } */ *uap = v;
290 1.65 thorpej struct proc *p = l->l_proc;
291 1.70 drochner struct shmmap_state *shmmap_s, *shmmap_s1;
292 1.69 drochner struct shmmap_entry *shmmap_se;
293 1.11 hpeyerl
294 1.12 mycroft shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
295 1.38 christos if (shmmap_s == NULL)
296 1.38 christos return EINVAL;
297 1.38 christos
298 1.70 drochner shmmap_se = shm_find_mapping(shmmap_s, (vaddr_t)SCARG(uap, shmaddr));
299 1.70 drochner if (!shmmap_se)
300 1.70 drochner return EINVAL;
301 1.70 drochner
302 1.70 drochner shmmap_s1 = shmmap_getprivate(p);
303 1.70 drochner if (shmmap_s1 != shmmap_s) {
304 1.70 drochner /* map has been copied, lookup entry in new map */
305 1.70 drochner shmmap_se = shm_find_mapping(shmmap_s1,
306 1.70 drochner (vaddr_t)SCARG(uap, shmaddr));
307 1.70 drochner KASSERT(shmmap_se != NULL);
308 1.70 drochner }
309 1.69 drochner #ifdef SHMDEBUG
310 1.70 drochner printf("shmdt: vm %p: remove %d @%lx\n",
311 1.70 drochner p->p_vmspace, shmmap_se->shmid, shmmap_se->va);
312 1.69 drochner #endif
313 1.70 drochner shm_delete_mapping(p->p_vmspace, shmmap_s1, shmmap_se);
314 1.70 drochner return 0;
315 1.11 hpeyerl }
316 1.11 hpeyerl
317 1.12 mycroft int
318 1.65 thorpej sys_shmat(l, v, retval)
319 1.65 thorpej struct lwp *l;
320 1.32 thorpej void *v;
321 1.32 thorpej register_t *retval;
322 1.32 thorpej {
323 1.33 mycroft struct sys_shmat_args /* {
324 1.26 cgd syscallarg(int) shmid;
325 1.44 kleink syscallarg(const void *) shmaddr;
326 1.35 christos syscallarg(int) shmflg;
327 1.32 thorpej } */ *uap = v;
328 1.65 thorpej struct proc *p = l->l_proc;
329 1.64 fvdl vaddr_t attach_va;
330 1.64 fvdl int error;
331 1.64 fvdl
332 1.64 fvdl error = shmat1(p, SCARG(uap, shmid), SCARG(uap, shmaddr),
333 1.64 fvdl SCARG(uap, shmflg), &attach_va, 0);
334 1.64 fvdl if (error != 0)
335 1.64 fvdl return error;
336 1.64 fvdl retval[0] = attach_va;
337 1.64 fvdl return 0;
338 1.64 fvdl }
339 1.64 fvdl
340 1.64 fvdl int
341 1.64 fvdl shmat1(p, shmid, shmaddr, shmflg, attachp, findremoved)
342 1.64 fvdl struct proc *p;
343 1.64 fvdl int shmid;
344 1.64 fvdl const void *shmaddr;
345 1.64 fvdl int shmflg;
346 1.64 fvdl vaddr_t *attachp;
347 1.64 fvdl int findremoved;
348 1.64 fvdl {
349 1.69 drochner int error, flags;
350 1.12 mycroft struct ucred *cred = p->p_ucred;
351 1.11 hpeyerl struct shmid_ds *shmseg;
352 1.69 drochner struct shmmap_state *shmmap_s;
353 1.74 christos struct uvm_object *uobj;
354 1.47 eeh vaddr_t attach_va;
355 1.11 hpeyerl vm_prot_t prot;
356 1.47 eeh vsize_t size;
357 1.69 drochner struct shmmap_entry *shmmap_se;
358 1.11 hpeyerl
359 1.64 fvdl shmseg = shm_find_segment_by_shmid(shmid, findremoved);
360 1.11 hpeyerl if (shmseg == NULL)
361 1.11 hpeyerl return EINVAL;
362 1.35 christos error = ipcperm(cred, &shmseg->shm_perm,
363 1.64 fvdl (shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
364 1.35 christos if (error)
365 1.12 mycroft return error;
366 1.69 drochner
367 1.69 drochner shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
368 1.69 drochner if (shmmap_s && shmmap_s->nitems >= shminfo.shmseg)
369 1.12 mycroft return EMFILE;
370 1.69 drochner
371 1.53 ragge size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
372 1.12 mycroft prot = VM_PROT_READ;
373 1.64 fvdl if ((shmflg & SHM_RDONLY) == 0)
374 1.12 mycroft prot |= VM_PROT_WRITE;
375 1.12 mycroft flags = MAP_ANON | MAP_SHARED;
376 1.64 fvdl if (shmaddr) {
377 1.12 mycroft flags |= MAP_FIXED;
378 1.76 junyoung if (shmflg & SHM_RND)
379 1.26 cgd attach_va =
380 1.64 fvdl (vaddr_t)shmaddr & ~(SHMLBA-1);
381 1.64 fvdl else if (((vaddr_t)shmaddr & (SHMLBA-1)) == 0)
382 1.64 fvdl attach_va = (vaddr_t)shmaddr;
383 1.11 hpeyerl else
384 1.14 mycroft return EINVAL;
385 1.12 mycroft } else {
386 1.66 atatat /* This is just a hint to uvm_mmap() about where to put it. */
387 1.68 atatat attach_va = VM_DEFAULT_ADDRESS(p->p_vmspace->vm_daddr, size);
388 1.11 hpeyerl }
389 1.74 christos uobj = ((struct shm_handle *)shmseg->_shm_internal)->shm_object;
390 1.74 christos (*uobj->pgops->pgo_reference)(uobj);
391 1.61 chs error = uvm_map(&p->p_vmspace->vm_map, &attach_va, size,
392 1.74 christos uobj, 0, 0,
393 1.61 chs UVM_MAPFLAG(prot, prot, UVM_INH_SHARE, UVM_ADV_RANDOM, 0));
394 1.61 chs if (error) {
395 1.74 christos (*uobj->pgops->pgo_detach)(uobj);
396 1.61 chs return error;
397 1.42 mrg }
398 1.69 drochner shmmap_se = pool_get(&shmmap_entry_pool, PR_WAITOK);
399 1.69 drochner shmmap_se->va = attach_va;
400 1.69 drochner shmmap_se->shmid = shmid;
401 1.69 drochner shmmap_s = shmmap_getprivate(p);
402 1.69 drochner #ifdef SHMDEBUG
403 1.69 drochner printf("shmat: vm %p: add %d @%lx\n", p->p_vmspace, shmid, attach_va);
404 1.69 drochner #endif
405 1.69 drochner SLIST_INSERT_HEAD(&shmmap_s->entries, shmmap_se, next);
406 1.69 drochner shmmap_s->nitems++;
407 1.11 hpeyerl shmseg->shm_lpid = p->p_pid;
408 1.11 hpeyerl shmseg->shm_atime = time.tv_sec;
409 1.11 hpeyerl shmseg->shm_nattch++;
410 1.64 fvdl *attachp = attach_va;
411 1.11 hpeyerl return 0;
412 1.11 hpeyerl }
413 1.11 hpeyerl
414 1.12 mycroft int
415 1.65 thorpej sys___shmctl13(l, v, retval)
416 1.65 thorpej struct lwp *l;
417 1.32 thorpej void *v;
418 1.32 thorpej register_t *retval;
419 1.32 thorpej {
420 1.52 thorpej struct sys___shmctl13_args /* {
421 1.26 cgd syscallarg(int) shmid;
422 1.26 cgd syscallarg(int) cmd;
423 1.26 cgd syscallarg(struct shmid_ds *) buf;
424 1.76 junyoung } */ *uap = v;
425 1.65 thorpej struct proc *p = l->l_proc;
426 1.52 thorpej struct shmid_ds shmbuf;
427 1.52 thorpej int cmd, error;
428 1.52 thorpej
429 1.52 thorpej cmd = SCARG(uap, cmd);
430 1.52 thorpej
431 1.52 thorpej if (cmd == IPC_SET) {
432 1.52 thorpej error = copyin(SCARG(uap, buf), &shmbuf, sizeof(shmbuf));
433 1.52 thorpej if (error)
434 1.52 thorpej return (error);
435 1.52 thorpej }
436 1.52 thorpej
437 1.52 thorpej error = shmctl1(p, SCARG(uap, shmid), cmd,
438 1.52 thorpej (cmd == IPC_SET || cmd == IPC_STAT) ? &shmbuf : NULL);
439 1.52 thorpej
440 1.52 thorpej if (error == 0 && cmd == IPC_STAT)
441 1.52 thorpej error = copyout(&shmbuf, SCARG(uap, buf), sizeof(shmbuf));
442 1.52 thorpej
443 1.52 thorpej return (error);
444 1.52 thorpej }
445 1.52 thorpej
446 1.52 thorpej int
447 1.52 thorpej shmctl1(p, shmid, cmd, shmbuf)
448 1.52 thorpej struct proc *p;
449 1.52 thorpej int shmid;
450 1.52 thorpej int cmd;
451 1.52 thorpej struct shmid_ds *shmbuf;
452 1.52 thorpej {
453 1.12 mycroft struct ucred *cred = p->p_ucred;
454 1.11 hpeyerl struct shmid_ds *shmseg;
455 1.52 thorpej int error = 0;
456 1.11 hpeyerl
457 1.64 fvdl shmseg = shm_find_segment_by_shmid(shmid, 0);
458 1.11 hpeyerl if (shmseg == NULL)
459 1.11 hpeyerl return EINVAL;
460 1.52 thorpej switch (cmd) {
461 1.11 hpeyerl case IPC_STAT:
462 1.35 christos if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_R)) != 0)
463 1.11 hpeyerl return error;
464 1.52 thorpej memcpy(shmbuf, shmseg, sizeof(struct shmid_ds));
465 1.11 hpeyerl break;
466 1.11 hpeyerl case IPC_SET:
467 1.35 christos if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0)
468 1.13 mycroft return error;
469 1.52 thorpej shmseg->shm_perm.uid = shmbuf->shm_perm.uid;
470 1.52 thorpej shmseg->shm_perm.gid = shmbuf->shm_perm.gid;
471 1.12 mycroft shmseg->shm_perm.mode =
472 1.12 mycroft (shmseg->shm_perm.mode & ~ACCESSPERMS) |
473 1.52 thorpej (shmbuf->shm_perm.mode & ACCESSPERMS);
474 1.12 mycroft shmseg->shm_ctime = time.tv_sec;
475 1.11 hpeyerl break;
476 1.11 hpeyerl case IPC_RMID:
477 1.35 christos if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0)
478 1.13 mycroft return error;
479 1.52 thorpej shmseg->shm_perm._key = IPC_PRIVATE;
480 1.12 mycroft shmseg->shm_perm.mode |= SHMSEG_REMOVED;
481 1.12 mycroft if (shmseg->shm_nattch <= 0) {
482 1.12 mycroft shm_deallocate_segment(shmseg);
483 1.52 thorpej shm_last_free = IPCID_TO_IX(shmid);
484 1.11 hpeyerl }
485 1.11 hpeyerl break;
486 1.11 hpeyerl case SHM_LOCK:
487 1.11 hpeyerl case SHM_UNLOCK:
488 1.11 hpeyerl default:
489 1.11 hpeyerl return EINVAL;
490 1.11 hpeyerl }
491 1.11 hpeyerl return 0;
492 1.11 hpeyerl }
493 1.11 hpeyerl
494 1.12 mycroft static int
495 1.12 mycroft shmget_existing(p, uap, mode, segnum, retval)
496 1.11 hpeyerl struct proc *p;
497 1.33 mycroft struct sys_shmget_args /* {
498 1.26 cgd syscallarg(key_t) key;
499 1.44 kleink syscallarg(size_t) size;
500 1.35 christos syscallarg(int) shmflg;
501 1.26 cgd } */ *uap;
502 1.11 hpeyerl int mode;
503 1.11 hpeyerl int segnum;
504 1.26 cgd register_t *retval;
505 1.11 hpeyerl {
506 1.12 mycroft struct shmid_ds *shmseg;
507 1.12 mycroft struct ucred *cred = p->p_ucred;
508 1.11 hpeyerl int error;
509 1.11 hpeyerl
510 1.11 hpeyerl shmseg = &shmsegs[segnum];
511 1.16 mycroft if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
512 1.16 mycroft /*
513 1.16 mycroft * This segment is in the process of being allocated. Wait
514 1.16 mycroft * until it's done, and look the key up again (in case the
515 1.16 mycroft * allocation failed or it was freed).
516 1.16 mycroft */
517 1.16 mycroft shmseg->shm_perm.mode |= SHMSEG_WANTED;
518 1.35 christos error = tsleep((caddr_t)shmseg, PLOCK | PCATCH, "shmget", 0);
519 1.35 christos if (error)
520 1.16 mycroft return error;
521 1.16 mycroft return EAGAIN;
522 1.16 mycroft }
523 1.35 christos if ((error = ipcperm(cred, &shmseg->shm_perm, mode)) != 0)
524 1.11 hpeyerl return error;
525 1.26 cgd if (SCARG(uap, size) && SCARG(uap, size) > shmseg->shm_segsz)
526 1.11 hpeyerl return EINVAL;
527 1.37 christos if ((SCARG(uap, shmflg) & (IPC_CREAT | IPC_EXCL)) ==
528 1.26 cgd (IPC_CREAT | IPC_EXCL))
529 1.12 mycroft return EEXIST;
530 1.11 hpeyerl *retval = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
531 1.11 hpeyerl return 0;
532 1.11 hpeyerl }
533 1.11 hpeyerl
534 1.14 mycroft static int
535 1.14 mycroft shmget_allocate_segment(p, uap, mode, retval)
536 1.14 mycroft struct proc *p;
537 1.33 mycroft struct sys_shmget_args /* {
538 1.26 cgd syscallarg(key_t) key;
539 1.44 kleink syscallarg(size_t) size;
540 1.35 christos syscallarg(int) shmflg;
541 1.26 cgd } */ *uap;
542 1.14 mycroft int mode;
543 1.26 cgd register_t *retval;
544 1.14 mycroft {
545 1.39 drochner int i, segnum, shmid, size;
546 1.14 mycroft struct ucred *cred = p->p_ucred;
547 1.14 mycroft struct shmid_ds *shmseg;
548 1.14 mycroft struct shm_handle *shm_handle;
549 1.40 drochner int error = 0;
550 1.76 junyoung
551 1.26 cgd if (SCARG(uap, size) < shminfo.shmmin ||
552 1.26 cgd SCARG(uap, size) > shminfo.shmmax)
553 1.14 mycroft return EINVAL;
554 1.14 mycroft if (shm_nused >= shminfo.shmmni) /* any shmids left? */
555 1.14 mycroft return ENOSPC;
556 1.53 ragge size = (SCARG(uap, size) + PGOFSET) & ~PGOFSET;
557 1.14 mycroft if (shm_committed + btoc(size) > shminfo.shmall)
558 1.14 mycroft return ENOMEM;
559 1.14 mycroft if (shm_last_free < 0) {
560 1.14 mycroft for (i = 0; i < shminfo.shmmni; i++)
561 1.14 mycroft if (shmsegs[i].shm_perm.mode & SHMSEG_FREE)
562 1.14 mycroft break;
563 1.14 mycroft if (i == shminfo.shmmni)
564 1.14 mycroft panic("shmseg free count inconsistent");
565 1.14 mycroft segnum = i;
566 1.14 mycroft } else {
567 1.14 mycroft segnum = shm_last_free;
568 1.14 mycroft shm_last_free = -1;
569 1.14 mycroft }
570 1.14 mycroft shmseg = &shmsegs[segnum];
571 1.14 mycroft /*
572 1.14 mycroft * In case we sleep in malloc(), mark the segment present but deleted
573 1.14 mycroft * so that noone else tries to create the same key.
574 1.14 mycroft */
575 1.14 mycroft shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
576 1.52 thorpej shmseg->shm_perm._key = SCARG(uap, key);
577 1.52 thorpej shmseg->shm_perm._seq = (shmseg->shm_perm._seq + 1) & 0x7fff;
578 1.14 mycroft shm_handle = (struct shm_handle *)
579 1.14 mycroft malloc(sizeof(struct shm_handle), M_SHM, M_WAITOK);
580 1.14 mycroft shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
581 1.42 mrg
582 1.42 mrg shm_handle->shm_object = uao_create(size, 0);
583 1.42 mrg
584 1.52 thorpej shmseg->_shm_internal = shm_handle;
585 1.14 mycroft shmseg->shm_perm.cuid = shmseg->shm_perm.uid = cred->cr_uid;
586 1.14 mycroft shmseg->shm_perm.cgid = shmseg->shm_perm.gid = cred->cr_gid;
587 1.16 mycroft shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) |
588 1.16 mycroft (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
589 1.26 cgd shmseg->shm_segsz = SCARG(uap, size);
590 1.14 mycroft shmseg->shm_cpid = p->p_pid;
591 1.14 mycroft shmseg->shm_lpid = shmseg->shm_nattch = 0;
592 1.14 mycroft shmseg->shm_atime = shmseg->shm_dtime = 0;
593 1.14 mycroft shmseg->shm_ctime = time.tv_sec;
594 1.14 mycroft shm_committed += btoc(size);
595 1.14 mycroft shm_nused++;
596 1.40 drochner
597 1.51 mrg *retval = shmid;
598 1.16 mycroft if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
599 1.16 mycroft /*
600 1.16 mycroft * Somebody else wanted this key while we were asleep. Wake
601 1.16 mycroft * them up now.
602 1.16 mycroft */
603 1.16 mycroft shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
604 1.16 mycroft wakeup((caddr_t)shmseg);
605 1.16 mycroft }
606 1.40 drochner return error;
607 1.14 mycroft }
608 1.14 mycroft
609 1.12 mycroft int
610 1.65 thorpej sys_shmget(l, v, retval)
611 1.65 thorpej struct lwp *l;
612 1.32 thorpej void *v;
613 1.32 thorpej register_t *retval;
614 1.32 thorpej {
615 1.33 mycroft struct sys_shmget_args /* {
616 1.26 cgd syscallarg(key_t) key;
617 1.26 cgd syscallarg(int) size;
618 1.35 christos syscallarg(int) shmflg;
619 1.32 thorpej } */ *uap = v;
620 1.65 thorpej struct proc *p = l->l_proc;
621 1.11 hpeyerl int segnum, mode, error;
622 1.11 hpeyerl
623 1.26 cgd mode = SCARG(uap, shmflg) & ACCESSPERMS;
624 1.26 cgd if (SCARG(uap, key) != IPC_PRIVATE) {
625 1.16 mycroft again:
626 1.26 cgd segnum = shm_find_segment_by_key(SCARG(uap, key));
627 1.16 mycroft if (segnum >= 0) {
628 1.16 mycroft error = shmget_existing(p, uap, mode, segnum, retval);
629 1.16 mycroft if (error == EAGAIN)
630 1.16 mycroft goto again;
631 1.16 mycroft return error;
632 1.16 mycroft }
633 1.76 junyoung if ((SCARG(uap, shmflg) & IPC_CREAT) == 0)
634 1.14 mycroft return ENOENT;
635 1.11 hpeyerl }
636 1.14 mycroft return shmget_allocate_segment(p, uap, mode, retval);
637 1.11 hpeyerl }
638 1.11 hpeyerl
639 1.12 mycroft void
640 1.41 thorpej shmfork(vm1, vm2)
641 1.41 thorpej struct vmspace *vm1, *vm2;
642 1.11 hpeyerl {
643 1.11 hpeyerl struct shmmap_state *shmmap_s;
644 1.69 drochner struct shmmap_entry *shmmap_se;
645 1.69 drochner
646 1.69 drochner vm2->vm_shm = vm1->vm_shm;
647 1.11 hpeyerl
648 1.69 drochner if (vm1->vm_shm == NULL)
649 1.38 christos return;
650 1.41 thorpej
651 1.69 drochner #ifdef SHMDEBUG
652 1.69 drochner printf("shmfork %p->%p\n", vm1, vm2);
653 1.69 drochner #endif
654 1.69 drochner
655 1.69 drochner shmmap_s = (struct shmmap_state *)vm1->vm_shm;
656 1.69 drochner
657 1.69 drochner SLIST_FOREACH(shmmap_se, &shmmap_s->entries, next)
658 1.69 drochner shmsegs[IPCID_TO_IX(shmmap_se->shmid)].shm_nattch++;
659 1.69 drochner shmmap_s->nrefs++;
660 1.11 hpeyerl }
661 1.11 hpeyerl
662 1.12 mycroft void
663 1.41 thorpej shmexit(vm)
664 1.41 thorpej struct vmspace *vm;
665 1.11 hpeyerl {
666 1.12 mycroft struct shmmap_state *shmmap_s;
667 1.69 drochner struct shmmap_entry *shmmap_se;
668 1.11 hpeyerl
669 1.41 thorpej shmmap_s = (struct shmmap_state *)vm->vm_shm;
670 1.38 christos if (shmmap_s == NULL)
671 1.38 christos return;
672 1.69 drochner
673 1.41 thorpej vm->vm_shm = NULL;
674 1.69 drochner
675 1.69 drochner if (--shmmap_s->nrefs > 0) {
676 1.69 drochner #ifdef SHMDEBUG
677 1.70 drochner printf("shmexit: vm %p drop ref (%d entries), now used by %d\n",
678 1.69 drochner vm, shmmap_s->nitems, shmmap_s->nrefs);
679 1.69 drochner #endif
680 1.69 drochner SLIST_FOREACH(shmmap_se, &shmmap_s->entries, next)
681 1.69 drochner shmsegs[IPCID_TO_IX(shmmap_se->shmid)].shm_nattch--;
682 1.69 drochner return;
683 1.69 drochner }
684 1.69 drochner
685 1.69 drochner #ifdef SHMDEBUG
686 1.69 drochner printf("shmexit: vm %p cleanup (%d entries)\n", vm, shmmap_s->nitems);
687 1.69 drochner #endif
688 1.69 drochner while (!SLIST_EMPTY(&shmmap_s->entries)) {
689 1.69 drochner shmmap_se = SLIST_FIRST(&shmmap_s->entries);
690 1.69 drochner shm_delete_mapping(vm, shmmap_s, shmmap_se);
691 1.69 drochner }
692 1.69 drochner KASSERT(shmmap_s->nitems == 0);
693 1.69 drochner free(shmmap_s, M_SHM);
694 1.11 hpeyerl }
695 1.11 hpeyerl
696 1.12 mycroft void
697 1.12 mycroft shminit()
698 1.11 hpeyerl {
699 1.71 jdolecek int i, sz;
700 1.71 jdolecek vaddr_t v;
701 1.71 jdolecek
702 1.71 jdolecek /* Allocate pageable memory for our structures */
703 1.71 jdolecek sz = shminfo.shmmni * sizeof(struct shmid_ds);
704 1.71 jdolecek if ((v = uvm_km_alloc(kernel_map, round_page(sz))) == 0)
705 1.71 jdolecek panic("sysv_shm: cannot allocate memory");
706 1.71 jdolecek shmsegs = (void *)v;
707 1.24 deraadt
708 1.60 thorpej shminfo.shmmax *= PAGE_SIZE;
709 1.11 hpeyerl
710 1.11 hpeyerl for (i = 0; i < shminfo.shmmni; i++) {
711 1.11 hpeyerl shmsegs[i].shm_perm.mode = SHMSEG_FREE;
712 1.52 thorpej shmsegs[i].shm_perm._seq = 0;
713 1.11 hpeyerl }
714 1.11 hpeyerl shm_last_free = 0;
715 1.11 hpeyerl shm_nused = 0;
716 1.11 hpeyerl shm_committed = 0;
717 1.11 hpeyerl }
718