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