sysv_shm.c revision 1.69 1 1.69 drochner /* $NetBSD: sysv_shm.c,v 1.69 2003/09/09 15:02:45 drochner 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.69 drochner __KERNEL_RCSID(0, "$NetBSD: sysv_shm.c,v 1.69 2003/09/09 15:02:45 drochner 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.60 thorpej
90 1.64 fvdl struct shmid_ds *shm_find_segment_by_shmid __P((int, int));
91 1.67 thorpej
92 1.67 thorpej MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments");
93 1.35 christos
94 1.11 hpeyerl /*
95 1.11 hpeyerl * Provides the following externally accessible functions:
96 1.11 hpeyerl *
97 1.41 thorpej * shminit(void); initialization
98 1.41 thorpej * shmexit(struct vmspace *) cleanup
99 1.41 thorpej * shmfork(struct vmspace *, struct vmspace *) fork handling
100 1.11 hpeyerl *
101 1.11 hpeyerl * Structures:
102 1.11 hpeyerl * shmsegs (an array of 'struct shmid_ds')
103 1.11 hpeyerl * per proc array of 'struct shmmap_state'
104 1.11 hpeyerl */
105 1.12 mycroft
106 1.16 mycroft #define SHMSEG_FREE 0x0200
107 1.16 mycroft #define SHMSEG_REMOVED 0x0400
108 1.16 mycroft #define SHMSEG_ALLOCATED 0x0800
109 1.16 mycroft #define SHMSEG_WANTED 0x1000
110 1.11 hpeyerl
111 1.55 simonb int shm_last_free, shm_nused, shm_committed;
112 1.55 simonb struct shmid_ds *shmsegs;
113 1.11 hpeyerl
114 1.11 hpeyerl struct shm_handle {
115 1.42 mrg struct uvm_object *shm_object;
116 1.11 hpeyerl };
117 1.11 hpeyerl
118 1.69 drochner struct shmmap_entry {
119 1.69 drochner SLIST_ENTRY(shmmap_entry) next;
120 1.47 eeh vaddr_t va;
121 1.11 hpeyerl int shmid;
122 1.11 hpeyerl };
123 1.11 hpeyerl
124 1.69 drochner struct pool shmmap_entry_pool;
125 1.69 drochner
126 1.69 drochner struct shmmap_state {
127 1.69 drochner unsigned int nitems;
128 1.69 drochner unsigned int nrefs;
129 1.69 drochner SLIST_HEAD(, shmmap_entry) entries;
130 1.69 drochner };
131 1.69 drochner
132 1.35 christos static int shm_find_segment_by_key __P((key_t));
133 1.12 mycroft static void shm_deallocate_segment __P((struct shmid_ds *));
134 1.69 drochner static void shm_delete_mapping __P((struct vmspace *, struct shmmap_state *,
135 1.69 drochner struct shmmap_entry *));
136 1.35 christos static int shmget_existing __P((struct proc *, struct sys_shmget_args *,
137 1.35 christos int, int, register_t *));
138 1.35 christos static int shmget_allocate_segment __P((struct proc *, struct sys_shmget_args *,
139 1.35 christos int, register_t *));
140 1.69 drochner static struct shmmap_state *shmmap_getprivate __P((struct proc *));
141 1.11 hpeyerl
142 1.12 mycroft static int
143 1.12 mycroft shm_find_segment_by_key(key)
144 1.11 hpeyerl key_t key;
145 1.11 hpeyerl {
146 1.11 hpeyerl int i;
147 1.11 hpeyerl
148 1.12 mycroft for (i = 0; i < shminfo.shmmni; i++)
149 1.12 mycroft if ((shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED) &&
150 1.52 thorpej shmsegs[i].shm_perm._key == key)
151 1.12 mycroft return i;
152 1.11 hpeyerl return -1;
153 1.11 hpeyerl }
154 1.11 hpeyerl
155 1.28 christos struct shmid_ds *
156 1.64 fvdl shm_find_segment_by_shmid(shmid, findremoved)
157 1.11 hpeyerl int shmid;
158 1.64 fvdl int findremoved;
159 1.11 hpeyerl {
160 1.11 hpeyerl int segnum;
161 1.11 hpeyerl struct shmid_ds *shmseg;
162 1.11 hpeyerl
163 1.11 hpeyerl segnum = IPCID_TO_IX(shmid);
164 1.12 mycroft if (segnum < 0 || segnum >= shminfo.shmmni)
165 1.11 hpeyerl return NULL;
166 1.11 hpeyerl shmseg = &shmsegs[segnum];
167 1.64 fvdl if ((shmseg->shm_perm.mode & SHMSEG_ALLOCATED) == 0)
168 1.64 fvdl return NULL;
169 1.64 fvdl if (!findremoved && ((shmseg->shm_perm.mode & SHMSEG_REMOVED) != 0))
170 1.64 fvdl return NULL;
171 1.64 fvdl if (shmseg->shm_perm._seq != IPCID_TO_SEQ(shmid))
172 1.11 hpeyerl return NULL;
173 1.11 hpeyerl return shmseg;
174 1.11 hpeyerl }
175 1.11 hpeyerl
176 1.12 mycroft static void
177 1.12 mycroft shm_deallocate_segment(shmseg)
178 1.12 mycroft struct shmid_ds *shmseg;
179 1.12 mycroft {
180 1.12 mycroft struct shm_handle *shm_handle;
181 1.14 mycroft size_t size;
182 1.12 mycroft
183 1.52 thorpej shm_handle = shmseg->_shm_internal;
184 1.53 ragge size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
185 1.42 mrg uao_detach(shm_handle->shm_object);
186 1.12 mycroft free((caddr_t)shm_handle, M_SHM);
187 1.52 thorpej shmseg->_shm_internal = NULL;
188 1.14 mycroft shm_committed -= btoc(size);
189 1.12 mycroft shmseg->shm_perm.mode = SHMSEG_FREE;
190 1.25 mycroft shm_nused--;
191 1.12 mycroft }
192 1.12 mycroft
193 1.61 chs static void
194 1.69 drochner shm_delete_mapping(vm, shmmap_s, shmmap_se)
195 1.41 thorpej struct vmspace *vm;
196 1.11 hpeyerl struct shmmap_state *shmmap_s;
197 1.69 drochner struct shmmap_entry *shmmap_se;
198 1.11 hpeyerl {
199 1.12 mycroft struct shmid_ds *shmseg;
200 1.61 chs int segnum;
201 1.14 mycroft size_t size;
202 1.11 hpeyerl
203 1.69 drochner segnum = IPCID_TO_IX(shmmap_se->shmid);
204 1.12 mycroft shmseg = &shmsegs[segnum];
205 1.53 ragge size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
206 1.69 drochner uvm_deallocate(&vm->vm_map, shmmap_se->va, size);
207 1.69 drochner SLIST_REMOVE(&shmmap_s->entries, shmmap_se, shmmap_entry, next);
208 1.69 drochner shmmap_s->nitems--;
209 1.69 drochner pool_put(&shmmap_entry_pool, shmmap_se);
210 1.11 hpeyerl shmseg->shm_dtime = time.tv_sec;
211 1.12 mycroft if ((--shmseg->shm_nattch <= 0) &&
212 1.11 hpeyerl (shmseg->shm_perm.mode & SHMSEG_REMOVED)) {
213 1.12 mycroft shm_deallocate_segment(shmseg);
214 1.12 mycroft shm_last_free = segnum;
215 1.11 hpeyerl }
216 1.11 hpeyerl }
217 1.11 hpeyerl
218 1.69 drochner /*
219 1.69 drochner * Get a non-shared shm map for that vmspace.
220 1.69 drochner * 3 cases:
221 1.69 drochner * - no shm map present: create a fresh one
222 1.69 drochner * - a shm map with refcount=1, just used by ourselves: fine
223 1.69 drochner * - a shared shm map: copy to a fresh one and adjust refcounts
224 1.69 drochner */
225 1.69 drochner static struct shmmap_state *
226 1.69 drochner shmmap_getprivate(struct proc *p)
227 1.69 drochner {
228 1.69 drochner struct shmmap_state *oshmmap_s, *shmmap_s;
229 1.69 drochner struct shmmap_entry *oshmmap_se, *shmmap_se;
230 1.69 drochner
231 1.69 drochner oshmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
232 1.69 drochner if (oshmmap_s && oshmmap_s->nrefs == 1)
233 1.69 drochner return (oshmmap_s);
234 1.69 drochner
235 1.69 drochner shmmap_s = malloc(sizeof(struct shmmap_state), M_SHM, M_WAITOK);
236 1.69 drochner memset(shmmap_s, 0, sizeof(struct shmmap_state));
237 1.69 drochner shmmap_s->nrefs = 1;
238 1.69 drochner SLIST_INIT(&shmmap_s->entries);
239 1.69 drochner p->p_vmspace->vm_shm = (caddr_t)shmmap_s;
240 1.69 drochner
241 1.69 drochner if (!oshmmap_s)
242 1.69 drochner return (shmmap_s);
243 1.69 drochner
244 1.69 drochner #ifdef SHMDEBUG
245 1.69 drochner printf("shmmap_getprivate: vm %p split (%d entries), used by %d\n",
246 1.69 drochner p->p_vmspace, oshmmap_s->nitems, oshmmap_s->nrefs);
247 1.69 drochner #endif
248 1.69 drochner SLIST_FOREACH(oshmmap_se, &oshmmap_s->entries, next) {
249 1.69 drochner shmmap_se = pool_get(&shmmap_entry_pool, PR_WAITOK);
250 1.69 drochner shmmap_se->va = oshmmap_se->va;
251 1.69 drochner shmmap_se->shmid = oshmmap_se->shmid;
252 1.69 drochner SLIST_INSERT_HEAD(&shmmap_s->entries, shmmap_se, next);
253 1.69 drochner }
254 1.69 drochner shmmap_s->nitems = oshmmap_s->nitems;
255 1.69 drochner oshmmap_s->nrefs--;
256 1.69 drochner return (shmmap_s);
257 1.69 drochner }
258 1.69 drochner
259 1.12 mycroft int
260 1.65 thorpej sys_shmdt(l, v, retval)
261 1.65 thorpej struct lwp *l;
262 1.32 thorpej void *v;
263 1.32 thorpej register_t *retval;
264 1.32 thorpej {
265 1.33 mycroft struct sys_shmdt_args /* {
266 1.44 kleink syscallarg(const void *) shmaddr;
267 1.32 thorpej } */ *uap = v;
268 1.65 thorpej struct proc *p = l->l_proc;
269 1.12 mycroft struct shmmap_state *shmmap_s;
270 1.69 drochner struct shmmap_entry *shmmap_se;
271 1.11 hpeyerl
272 1.12 mycroft shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
273 1.38 christos if (shmmap_s == NULL)
274 1.38 christos return EINVAL;
275 1.38 christos
276 1.69 drochner SLIST_FOREACH(shmmap_se, &shmmap_s->entries, next) {
277 1.69 drochner if (shmmap_se->va == (vaddr_t)SCARG(uap, shmaddr)) {
278 1.69 drochner shmmap_s = shmmap_getprivate(p);
279 1.69 drochner #ifdef SHMDEBUG
280 1.69 drochner printf("shmdt: vm %p: remove %d @%lx\n",
281 1.69 drochner p->p_vmspace, shmmap_se->shmid, shmmap_se->va);
282 1.69 drochner #endif
283 1.69 drochner shm_delete_mapping(p->p_vmspace, shmmap_s, shmmap_se);
284 1.69 drochner return 0;
285 1.69 drochner }
286 1.69 drochner }
287 1.69 drochner
288 1.69 drochner /* not found */
289 1.69 drochner return EINVAL;
290 1.11 hpeyerl }
291 1.11 hpeyerl
292 1.12 mycroft int
293 1.65 thorpej sys_shmat(l, v, retval)
294 1.65 thorpej struct lwp *l;
295 1.32 thorpej void *v;
296 1.32 thorpej register_t *retval;
297 1.32 thorpej {
298 1.33 mycroft struct sys_shmat_args /* {
299 1.26 cgd syscallarg(int) shmid;
300 1.44 kleink syscallarg(const void *) shmaddr;
301 1.35 christos syscallarg(int) shmflg;
302 1.32 thorpej } */ *uap = v;
303 1.65 thorpej struct proc *p = l->l_proc;
304 1.64 fvdl vaddr_t attach_va;
305 1.64 fvdl int error;
306 1.64 fvdl
307 1.64 fvdl error = shmat1(p, SCARG(uap, shmid), SCARG(uap, shmaddr),
308 1.64 fvdl SCARG(uap, shmflg), &attach_va, 0);
309 1.64 fvdl if (error != 0)
310 1.64 fvdl return error;
311 1.64 fvdl retval[0] = attach_va;
312 1.64 fvdl return 0;
313 1.64 fvdl }
314 1.64 fvdl
315 1.64 fvdl int
316 1.64 fvdl shmat1(p, shmid, shmaddr, shmflg, attachp, findremoved)
317 1.64 fvdl struct proc *p;
318 1.64 fvdl int shmid;
319 1.64 fvdl const void *shmaddr;
320 1.64 fvdl int shmflg;
321 1.64 fvdl vaddr_t *attachp;
322 1.64 fvdl int findremoved;
323 1.64 fvdl {
324 1.69 drochner int error, flags;
325 1.12 mycroft struct ucred *cred = p->p_ucred;
326 1.11 hpeyerl struct shmid_ds *shmseg;
327 1.69 drochner struct shmmap_state *shmmap_s;
328 1.39 drochner struct shm_handle *shm_handle;
329 1.47 eeh vaddr_t attach_va;
330 1.11 hpeyerl vm_prot_t prot;
331 1.47 eeh vsize_t size;
332 1.69 drochner struct shmmap_entry *shmmap_se;
333 1.11 hpeyerl
334 1.64 fvdl shmseg = shm_find_segment_by_shmid(shmid, findremoved);
335 1.11 hpeyerl if (shmseg == NULL)
336 1.11 hpeyerl return EINVAL;
337 1.35 christos error = ipcperm(cred, &shmseg->shm_perm,
338 1.64 fvdl (shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
339 1.35 christos if (error)
340 1.12 mycroft return error;
341 1.69 drochner
342 1.69 drochner shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
343 1.69 drochner if (shmmap_s && shmmap_s->nitems >= shminfo.shmseg)
344 1.12 mycroft return EMFILE;
345 1.69 drochner
346 1.53 ragge size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
347 1.12 mycroft prot = VM_PROT_READ;
348 1.64 fvdl if ((shmflg & SHM_RDONLY) == 0)
349 1.12 mycroft prot |= VM_PROT_WRITE;
350 1.12 mycroft flags = MAP_ANON | MAP_SHARED;
351 1.64 fvdl if (shmaddr) {
352 1.12 mycroft flags |= MAP_FIXED;
353 1.64 fvdl if (shmflg & SHM_RND)
354 1.26 cgd attach_va =
355 1.64 fvdl (vaddr_t)shmaddr & ~(SHMLBA-1);
356 1.64 fvdl else if (((vaddr_t)shmaddr & (SHMLBA-1)) == 0)
357 1.64 fvdl attach_va = (vaddr_t)shmaddr;
358 1.11 hpeyerl else
359 1.14 mycroft return EINVAL;
360 1.12 mycroft } else {
361 1.66 atatat /* This is just a hint to uvm_mmap() about where to put it. */
362 1.68 atatat attach_va = VM_DEFAULT_ADDRESS(p->p_vmspace->vm_daddr, size);
363 1.11 hpeyerl }
364 1.52 thorpej shm_handle = shmseg->_shm_internal;
365 1.42 mrg uao_reference(shm_handle->shm_object);
366 1.61 chs error = uvm_map(&p->p_vmspace->vm_map, &attach_va, size,
367 1.61 chs shm_handle->shm_object, 0, 0,
368 1.61 chs UVM_MAPFLAG(prot, prot, UVM_INH_SHARE, UVM_ADV_RANDOM, 0));
369 1.61 chs if (error) {
370 1.61 chs return error;
371 1.42 mrg }
372 1.69 drochner shmmap_se = pool_get(&shmmap_entry_pool, PR_WAITOK);
373 1.69 drochner shmmap_se->va = attach_va;
374 1.69 drochner shmmap_se->shmid = shmid;
375 1.69 drochner shmmap_s = shmmap_getprivate(p);
376 1.69 drochner #ifdef SHMDEBUG
377 1.69 drochner printf("shmat: vm %p: add %d @%lx\n", p->p_vmspace, shmid, attach_va);
378 1.69 drochner #endif
379 1.69 drochner SLIST_INSERT_HEAD(&shmmap_s->entries, shmmap_se, next);
380 1.69 drochner shmmap_s->nitems++;
381 1.11 hpeyerl shmseg->shm_lpid = p->p_pid;
382 1.11 hpeyerl shmseg->shm_atime = time.tv_sec;
383 1.11 hpeyerl shmseg->shm_nattch++;
384 1.64 fvdl *attachp = attach_va;
385 1.11 hpeyerl return 0;
386 1.11 hpeyerl }
387 1.11 hpeyerl
388 1.12 mycroft int
389 1.65 thorpej sys___shmctl13(l, v, retval)
390 1.65 thorpej struct lwp *l;
391 1.32 thorpej void *v;
392 1.32 thorpej register_t *retval;
393 1.32 thorpej {
394 1.52 thorpej struct sys___shmctl13_args /* {
395 1.26 cgd syscallarg(int) shmid;
396 1.26 cgd syscallarg(int) cmd;
397 1.26 cgd syscallarg(struct shmid_ds *) buf;
398 1.52 thorpej } */ *uap = v;
399 1.65 thorpej struct proc *p = l->l_proc;
400 1.52 thorpej struct shmid_ds shmbuf;
401 1.52 thorpej int cmd, error;
402 1.52 thorpej
403 1.52 thorpej cmd = SCARG(uap, cmd);
404 1.52 thorpej
405 1.52 thorpej if (cmd == IPC_SET) {
406 1.52 thorpej error = copyin(SCARG(uap, buf), &shmbuf, sizeof(shmbuf));
407 1.52 thorpej if (error)
408 1.52 thorpej return (error);
409 1.52 thorpej }
410 1.52 thorpej
411 1.52 thorpej error = shmctl1(p, SCARG(uap, shmid), cmd,
412 1.52 thorpej (cmd == IPC_SET || cmd == IPC_STAT) ? &shmbuf : NULL);
413 1.52 thorpej
414 1.52 thorpej if (error == 0 && cmd == IPC_STAT)
415 1.52 thorpej error = copyout(&shmbuf, SCARG(uap, buf), sizeof(shmbuf));
416 1.52 thorpej
417 1.52 thorpej return (error);
418 1.52 thorpej }
419 1.52 thorpej
420 1.52 thorpej int
421 1.52 thorpej shmctl1(p, shmid, cmd, shmbuf)
422 1.52 thorpej struct proc *p;
423 1.52 thorpej int shmid;
424 1.52 thorpej int cmd;
425 1.52 thorpej struct shmid_ds *shmbuf;
426 1.52 thorpej {
427 1.12 mycroft struct ucred *cred = p->p_ucred;
428 1.11 hpeyerl struct shmid_ds *shmseg;
429 1.52 thorpej int error = 0;
430 1.11 hpeyerl
431 1.64 fvdl shmseg = shm_find_segment_by_shmid(shmid, 0);
432 1.11 hpeyerl if (shmseg == NULL)
433 1.11 hpeyerl return EINVAL;
434 1.52 thorpej switch (cmd) {
435 1.11 hpeyerl case IPC_STAT:
436 1.35 christos if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_R)) != 0)
437 1.11 hpeyerl return error;
438 1.52 thorpej memcpy(shmbuf, shmseg, sizeof(struct shmid_ds));
439 1.11 hpeyerl break;
440 1.11 hpeyerl case IPC_SET:
441 1.35 christos if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0)
442 1.13 mycroft return error;
443 1.52 thorpej shmseg->shm_perm.uid = shmbuf->shm_perm.uid;
444 1.52 thorpej shmseg->shm_perm.gid = shmbuf->shm_perm.gid;
445 1.12 mycroft shmseg->shm_perm.mode =
446 1.12 mycroft (shmseg->shm_perm.mode & ~ACCESSPERMS) |
447 1.52 thorpej (shmbuf->shm_perm.mode & ACCESSPERMS);
448 1.12 mycroft shmseg->shm_ctime = time.tv_sec;
449 1.11 hpeyerl break;
450 1.11 hpeyerl case IPC_RMID:
451 1.35 christos if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0)
452 1.13 mycroft return error;
453 1.52 thorpej shmseg->shm_perm._key = IPC_PRIVATE;
454 1.12 mycroft shmseg->shm_perm.mode |= SHMSEG_REMOVED;
455 1.12 mycroft if (shmseg->shm_nattch <= 0) {
456 1.12 mycroft shm_deallocate_segment(shmseg);
457 1.52 thorpej shm_last_free = IPCID_TO_IX(shmid);
458 1.11 hpeyerl }
459 1.11 hpeyerl break;
460 1.11 hpeyerl case SHM_LOCK:
461 1.11 hpeyerl case SHM_UNLOCK:
462 1.11 hpeyerl default:
463 1.11 hpeyerl return EINVAL;
464 1.11 hpeyerl }
465 1.11 hpeyerl return 0;
466 1.11 hpeyerl }
467 1.11 hpeyerl
468 1.12 mycroft static int
469 1.12 mycroft shmget_existing(p, uap, mode, segnum, retval)
470 1.11 hpeyerl struct proc *p;
471 1.33 mycroft struct sys_shmget_args /* {
472 1.26 cgd syscallarg(key_t) key;
473 1.44 kleink syscallarg(size_t) size;
474 1.35 christos syscallarg(int) shmflg;
475 1.26 cgd } */ *uap;
476 1.11 hpeyerl int mode;
477 1.11 hpeyerl int segnum;
478 1.26 cgd register_t *retval;
479 1.11 hpeyerl {
480 1.12 mycroft struct shmid_ds *shmseg;
481 1.12 mycroft struct ucred *cred = p->p_ucred;
482 1.11 hpeyerl int error;
483 1.11 hpeyerl
484 1.11 hpeyerl shmseg = &shmsegs[segnum];
485 1.16 mycroft if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
486 1.16 mycroft /*
487 1.16 mycroft * This segment is in the process of being allocated. Wait
488 1.16 mycroft * until it's done, and look the key up again (in case the
489 1.16 mycroft * allocation failed or it was freed).
490 1.16 mycroft */
491 1.16 mycroft shmseg->shm_perm.mode |= SHMSEG_WANTED;
492 1.35 christos error = tsleep((caddr_t)shmseg, PLOCK | PCATCH, "shmget", 0);
493 1.35 christos if (error)
494 1.16 mycroft return error;
495 1.16 mycroft return EAGAIN;
496 1.16 mycroft }
497 1.35 christos if ((error = ipcperm(cred, &shmseg->shm_perm, mode)) != 0)
498 1.11 hpeyerl return error;
499 1.26 cgd if (SCARG(uap, size) && SCARG(uap, size) > shmseg->shm_segsz)
500 1.11 hpeyerl return EINVAL;
501 1.37 christos if ((SCARG(uap, shmflg) & (IPC_CREAT | IPC_EXCL)) ==
502 1.26 cgd (IPC_CREAT | IPC_EXCL))
503 1.12 mycroft return EEXIST;
504 1.11 hpeyerl *retval = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
505 1.11 hpeyerl return 0;
506 1.11 hpeyerl }
507 1.11 hpeyerl
508 1.14 mycroft static int
509 1.14 mycroft shmget_allocate_segment(p, uap, mode, retval)
510 1.14 mycroft struct proc *p;
511 1.33 mycroft struct sys_shmget_args /* {
512 1.26 cgd syscallarg(key_t) key;
513 1.44 kleink syscallarg(size_t) size;
514 1.35 christos syscallarg(int) shmflg;
515 1.26 cgd } */ *uap;
516 1.14 mycroft int mode;
517 1.26 cgd register_t *retval;
518 1.14 mycroft {
519 1.39 drochner int i, segnum, shmid, size;
520 1.14 mycroft struct ucred *cred = p->p_ucred;
521 1.14 mycroft struct shmid_ds *shmseg;
522 1.14 mycroft struct shm_handle *shm_handle;
523 1.40 drochner int error = 0;
524 1.14 mycroft
525 1.26 cgd if (SCARG(uap, size) < shminfo.shmmin ||
526 1.26 cgd SCARG(uap, size) > shminfo.shmmax)
527 1.14 mycroft return EINVAL;
528 1.14 mycroft if (shm_nused >= shminfo.shmmni) /* any shmids left? */
529 1.14 mycroft return ENOSPC;
530 1.53 ragge size = (SCARG(uap, size) + PGOFSET) & ~PGOFSET;
531 1.14 mycroft if (shm_committed + btoc(size) > shminfo.shmall)
532 1.14 mycroft return ENOMEM;
533 1.14 mycroft if (shm_last_free < 0) {
534 1.14 mycroft for (i = 0; i < shminfo.shmmni; i++)
535 1.14 mycroft if (shmsegs[i].shm_perm.mode & SHMSEG_FREE)
536 1.14 mycroft break;
537 1.14 mycroft if (i == shminfo.shmmni)
538 1.14 mycroft panic("shmseg free count inconsistent");
539 1.14 mycroft segnum = i;
540 1.14 mycroft } else {
541 1.14 mycroft segnum = shm_last_free;
542 1.14 mycroft shm_last_free = -1;
543 1.14 mycroft }
544 1.14 mycroft shmseg = &shmsegs[segnum];
545 1.14 mycroft /*
546 1.14 mycroft * In case we sleep in malloc(), mark the segment present but deleted
547 1.14 mycroft * so that noone else tries to create the same key.
548 1.14 mycroft */
549 1.14 mycroft shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
550 1.52 thorpej shmseg->shm_perm._key = SCARG(uap, key);
551 1.52 thorpej shmseg->shm_perm._seq = (shmseg->shm_perm._seq + 1) & 0x7fff;
552 1.14 mycroft shm_handle = (struct shm_handle *)
553 1.14 mycroft malloc(sizeof(struct shm_handle), M_SHM, M_WAITOK);
554 1.14 mycroft shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
555 1.42 mrg
556 1.42 mrg shm_handle->shm_object = uao_create(size, 0);
557 1.42 mrg
558 1.52 thorpej shmseg->_shm_internal = shm_handle;
559 1.14 mycroft shmseg->shm_perm.cuid = shmseg->shm_perm.uid = cred->cr_uid;
560 1.14 mycroft shmseg->shm_perm.cgid = shmseg->shm_perm.gid = cred->cr_gid;
561 1.16 mycroft shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) |
562 1.16 mycroft (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
563 1.26 cgd shmseg->shm_segsz = SCARG(uap, size);
564 1.14 mycroft shmseg->shm_cpid = p->p_pid;
565 1.14 mycroft shmseg->shm_lpid = shmseg->shm_nattch = 0;
566 1.14 mycroft shmseg->shm_atime = shmseg->shm_dtime = 0;
567 1.14 mycroft shmseg->shm_ctime = time.tv_sec;
568 1.14 mycroft shm_committed += btoc(size);
569 1.14 mycroft shm_nused++;
570 1.40 drochner
571 1.51 mrg *retval = shmid;
572 1.16 mycroft if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
573 1.16 mycroft /*
574 1.16 mycroft * Somebody else wanted this key while we were asleep. Wake
575 1.16 mycroft * them up now.
576 1.16 mycroft */
577 1.16 mycroft shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
578 1.16 mycroft wakeup((caddr_t)shmseg);
579 1.16 mycroft }
580 1.40 drochner return error;
581 1.14 mycroft }
582 1.14 mycroft
583 1.12 mycroft int
584 1.65 thorpej sys_shmget(l, v, retval)
585 1.65 thorpej struct lwp *l;
586 1.32 thorpej void *v;
587 1.32 thorpej register_t *retval;
588 1.32 thorpej {
589 1.33 mycroft struct sys_shmget_args /* {
590 1.26 cgd syscallarg(key_t) key;
591 1.26 cgd syscallarg(int) size;
592 1.35 christos syscallarg(int) shmflg;
593 1.32 thorpej } */ *uap = v;
594 1.65 thorpej struct proc *p = l->l_proc;
595 1.11 hpeyerl int segnum, mode, error;
596 1.11 hpeyerl
597 1.26 cgd mode = SCARG(uap, shmflg) & ACCESSPERMS;
598 1.26 cgd if (SCARG(uap, key) != IPC_PRIVATE) {
599 1.16 mycroft again:
600 1.26 cgd segnum = shm_find_segment_by_key(SCARG(uap, key));
601 1.16 mycroft if (segnum >= 0) {
602 1.16 mycroft error = shmget_existing(p, uap, mode, segnum, retval);
603 1.16 mycroft if (error == EAGAIN)
604 1.16 mycroft goto again;
605 1.16 mycroft return error;
606 1.16 mycroft }
607 1.26 cgd if ((SCARG(uap, shmflg) & IPC_CREAT) == 0)
608 1.14 mycroft return ENOENT;
609 1.11 hpeyerl }
610 1.14 mycroft return shmget_allocate_segment(p, uap, mode, retval);
611 1.11 hpeyerl }
612 1.11 hpeyerl
613 1.12 mycroft void
614 1.41 thorpej shmfork(vm1, vm2)
615 1.41 thorpej struct vmspace *vm1, *vm2;
616 1.11 hpeyerl {
617 1.11 hpeyerl struct shmmap_state *shmmap_s;
618 1.69 drochner struct shmmap_entry *shmmap_se;
619 1.69 drochner
620 1.69 drochner vm2->vm_shm = vm1->vm_shm;
621 1.11 hpeyerl
622 1.69 drochner if (vm1->vm_shm == NULL)
623 1.38 christos return;
624 1.41 thorpej
625 1.69 drochner #ifdef SHMDEBUG
626 1.69 drochner printf("shmfork %p->%p\n", vm1, vm2);
627 1.69 drochner #endif
628 1.69 drochner
629 1.69 drochner shmmap_s = (struct shmmap_state *)vm1->vm_shm;
630 1.69 drochner
631 1.69 drochner SLIST_FOREACH(shmmap_se, &shmmap_s->entries, next)
632 1.69 drochner shmsegs[IPCID_TO_IX(shmmap_se->shmid)].shm_nattch++;
633 1.69 drochner shmmap_s->nrefs++;
634 1.11 hpeyerl }
635 1.11 hpeyerl
636 1.12 mycroft void
637 1.41 thorpej shmexit(vm)
638 1.41 thorpej struct vmspace *vm;
639 1.11 hpeyerl {
640 1.12 mycroft struct shmmap_state *shmmap_s;
641 1.69 drochner struct shmmap_entry *shmmap_se;
642 1.11 hpeyerl
643 1.41 thorpej shmmap_s = (struct shmmap_state *)vm->vm_shm;
644 1.38 christos if (shmmap_s == NULL)
645 1.38 christos return;
646 1.69 drochner
647 1.41 thorpej vm->vm_shm = NULL;
648 1.69 drochner
649 1.69 drochner if (--shmmap_s->nrefs > 0) {
650 1.69 drochner #ifdef SHMDEBUG
651 1.69 drochner printf("shmexit: vm %p drop ref (%d entries), used by %d\n",
652 1.69 drochner vm, shmmap_s->nitems, shmmap_s->nrefs);
653 1.69 drochner #endif
654 1.69 drochner SLIST_FOREACH(shmmap_se, &shmmap_s->entries, next)
655 1.69 drochner shmsegs[IPCID_TO_IX(shmmap_se->shmid)].shm_nattch--;
656 1.69 drochner return;
657 1.69 drochner }
658 1.69 drochner
659 1.69 drochner #ifdef SHMDEBUG
660 1.69 drochner printf("shmexit: vm %p cleanup (%d entries)\n", vm, shmmap_s->nitems);
661 1.69 drochner #endif
662 1.69 drochner while (!SLIST_EMPTY(&shmmap_s->entries)) {
663 1.69 drochner shmmap_se = SLIST_FIRST(&shmmap_s->entries);
664 1.69 drochner shm_delete_mapping(vm, shmmap_s, shmmap_se);
665 1.69 drochner }
666 1.69 drochner KASSERT(shmmap_s->nitems == 0);
667 1.69 drochner free(shmmap_s, M_SHM);
668 1.11 hpeyerl }
669 1.11 hpeyerl
670 1.12 mycroft void
671 1.12 mycroft shminit()
672 1.11 hpeyerl {
673 1.11 hpeyerl int i;
674 1.24 deraadt
675 1.60 thorpej shminfo.shmmax *= PAGE_SIZE;
676 1.11 hpeyerl
677 1.11 hpeyerl for (i = 0; i < shminfo.shmmni; i++) {
678 1.11 hpeyerl shmsegs[i].shm_perm.mode = SHMSEG_FREE;
679 1.52 thorpej shmsegs[i].shm_perm._seq = 0;
680 1.11 hpeyerl }
681 1.11 hpeyerl shm_last_free = 0;
682 1.11 hpeyerl shm_nused = 0;
683 1.11 hpeyerl shm_committed = 0;
684 1.69 drochner
685 1.69 drochner pool_init(&shmmap_entry_pool, sizeof(struct shmmap_entry), 0, 0, 0,
686 1.69 drochner "shmmp", 0);
687 1.11 hpeyerl }
688