1 /* $FreeBSD: src/sys/kern/sysv_shm.c,v 1.45.2.6 2002/10/22 20:45:03 fjoe Exp $ */
2 /* $NetBSD: sysv_shm.c,v 1.23 1994/07/04 23:25:12 glass Exp $ */
5 * Copyright (c) 1994 Adam Glass and Charles Hannum. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Adam Glass and Charles
19 * 4. The names of the authors may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "opt_compat.h"
35 #include "opt_sysvipc.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/sysproto.h>
40 #include <sys/kernel.h>
41 #include <sys/sysctl.h>
44 #include <sys/malloc.h>
47 #include <sys/sysent.h>
51 #include <vm/vm_param.h>
54 #include <vm/vm_object.h>
55 #include <vm/vm_map.h>
56 #include <vm/vm_page.h>
57 #include <vm/vm_pager.h>
59 static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments");
62 static int oshmctl __P((struct proc *p, struct oshmctl_args *uap));
64 static int shmget_allocate_segment __P((struct proc *p, struct shmget_args *uap, int mode));
65 static int shmget_existing __P((struct proc *p, struct shmget_args *uap, int mode, int segnum));
67 /* XXX casting to (sy_call_t *) is bogus, as usual. */
68 static sy_call_t *shmcalls[] = {
69 (sy_call_t *)shmat, (sy_call_t *)oshmctl,
70 (sy_call_t *)shmdt, (sy_call_t *)shmget,
74 #define SHMSEG_FREE 0x0200
75 #define SHMSEG_REMOVED 0x0400
76 #define SHMSEG_ALLOCATED 0x0800
77 #define SHMSEG_WANTED 0x1000
79 static int shm_last_free, shm_nused, shm_committed, shmalloced;
80 static struct shmid_ds *shmsegs;
83 /* vm_offset_t kva; */
84 vm_object_t shm_object;
92 static void shm_deallocate_segment __P((struct shmid_ds *));
93 static int shm_find_segment_by_key __P((key_t));
94 static struct shmid_ds *shm_find_segment_by_shmid __P((int));
95 static int shm_delete_mapping __P((struct proc *, struct shmmap_state *));
96 static void shmrealloc __P((void));
97 static void shminit __P((void *));
103 #define SHMMAXPGS 8192 /* note: sysv shared memory is swap backed */
106 #define SHMMAX (SHMMAXPGS*PAGE_SIZE)
118 #define SHMALL (SHMMAXPGS)
121 struct shminfo shminfo = {
129 static int shm_use_phys;
131 TUNABLE_INT("kern.ipc.shmmin", &shminfo.shmmin);
132 TUNABLE_INT("kern.ipc.shmmni", &shminfo.shmmni);
133 TUNABLE_INT("kern.ipc.shmseg", &shminfo.shmseg);
134 TUNABLE_INT("kern.ipc.shmmaxpgs", &shminfo.shmall);
135 TUNABLE_INT("kern.ipc.shm_use_phys", &shm_use_phys);
137 SYSCTL_DECL(_kern_ipc);
138 SYSCTL_INT(_kern_ipc, OID_AUTO, shmmax, CTLFLAG_RW, &shminfo.shmmax, 0, "");
139 SYSCTL_INT(_kern_ipc, OID_AUTO, shmmin, CTLFLAG_RW, &shminfo.shmmin, 0, "");
140 SYSCTL_INT(_kern_ipc, OID_AUTO, shmmni, CTLFLAG_RD, &shminfo.shmmni, 0, "");
141 SYSCTL_INT(_kern_ipc, OID_AUTO, shmseg, CTLFLAG_RW, &shminfo.shmseg, 0, "");
142 SYSCTL_INT(_kern_ipc, OID_AUTO, shmall, CTLFLAG_RW, &shminfo.shmall, 0, "");
143 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_use_phys, CTLFLAG_RW, &shm_use_phys, 0, "");
146 shm_find_segment_by_key(key)
151 for (i = 0; i < shmalloced; i++)
152 if ((shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED) &&
153 shmsegs[i].shm_perm.key == key)
158 static struct shmid_ds *
159 shm_find_segment_by_shmid(shmid)
163 struct shmid_ds *shmseg;
165 segnum = IPCID_TO_IX(shmid);
166 if (segnum < 0 || segnum >= shmalloced)
168 shmseg = &shmsegs[segnum];
169 if ((shmseg->shm_perm.mode & (SHMSEG_ALLOCATED | SHMSEG_REMOVED))
170 != SHMSEG_ALLOCATED ||
171 shmseg->shm_perm.seq != IPCID_TO_SEQ(shmid))
177 shm_deallocate_segment(shmseg)
178 struct shmid_ds *shmseg;
180 struct shm_handle *shm_handle;
183 shm_handle = shmseg->shm_internal;
184 vm_object_deallocate(shm_handle->shm_object);
185 free((caddr_t)shm_handle, M_SHM);
186 shmseg->shm_internal = NULL;
187 size = round_page(shmseg->shm_segsz);
188 shm_committed -= btoc(size);
190 shmseg->shm_perm.mode = SHMSEG_FREE;
194 shm_delete_mapping(p, shmmap_s)
196 struct shmmap_state *shmmap_s;
198 struct shmid_ds *shmseg;
202 segnum = IPCID_TO_IX(shmmap_s->shmid);
203 shmseg = &shmsegs[segnum];
204 size = round_page(shmseg->shm_segsz);
205 result = vm_map_remove(&p->p_vmspace->vm_map, shmmap_s->va, shmmap_s->va + size);
206 if (result != KERN_SUCCESS)
208 shmmap_s->shmid = -1;
209 shmseg->shm_dtime = time_second;
210 if ((--shmseg->shm_nattch <= 0) &&
211 (shmseg->shm_perm.mode & SHMSEG_REMOVED)) {
212 shm_deallocate_segment(shmseg);
213 shm_last_free = segnum;
218 #ifndef _SYS_SYSPROTO_H_
227 struct shmdt_args *uap;
229 struct shmmap_state *shmmap_s;
232 if (!jail_sysvipc_allowed && p->p_prison != NULL)
235 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
236 if (shmmap_s == NULL)
238 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
239 if (shmmap_s->shmid != -1 &&
240 shmmap_s->va == (vm_offset_t)uap->shmaddr)
242 if (i == shminfo.shmseg)
244 return shm_delete_mapping(p, shmmap_s);
247 #ifndef _SYS_SYSPROTO_H_
258 struct shmat_args *uap;
261 struct shmid_ds *shmseg;
262 struct shmmap_state *shmmap_s = NULL;
263 struct shm_handle *shm_handle;
264 vm_offset_t attach_va;
269 if (!jail_sysvipc_allowed && p->p_prison != NULL)
272 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
273 if (shmmap_s == NULL) {
274 size = shminfo.shmseg * sizeof(struct shmmap_state);
275 shmmap_s = malloc(size, M_SHM, M_WAITOK);
276 for (i = 0; i < shminfo.shmseg; i++)
277 shmmap_s[i].shmid = -1;
278 p->p_vmspace->vm_shm = (caddr_t)shmmap_s;
280 shmseg = shm_find_segment_by_shmid(uap->shmid);
283 error = ipcperm(p, &shmseg->shm_perm,
284 (uap->shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
287 for (i = 0; i < shminfo.shmseg; i++) {
288 if (shmmap_s->shmid == -1)
292 if (i >= shminfo.shmseg)
294 size = round_page(shmseg->shm_segsz);
295 #ifdef VM_PROT_READ_IS_EXEC
296 prot = VM_PROT_READ | VM_PROT_EXECUTE;
300 if ((uap->shmflg & SHM_RDONLY) == 0)
301 prot |= VM_PROT_WRITE;
302 flags = MAP_ANON | MAP_SHARED;
305 if (uap->shmflg & SHM_RND)
306 attach_va = (vm_offset_t)uap->shmaddr & ~(SHMLBA-1);
307 else if (((vm_offset_t)uap->shmaddr & (SHMLBA-1)) == 0)
308 attach_va = (vm_offset_t)uap->shmaddr;
312 /* This is just a hint to vm_map_find() about where to put it. */
313 attach_va = round_page((vm_offset_t)p->p_vmspace->vm_taddr + maxtsiz + maxdsiz);
316 shm_handle = shmseg->shm_internal;
317 vm_object_reference(shm_handle->shm_object);
318 rv = vm_map_find(&p->p_vmspace->vm_map, shm_handle->shm_object,
319 0, &attach_va, size, (flags & MAP_FIXED)?0:1, prot, prot, 0);
320 if (rv != KERN_SUCCESS) {
323 vm_map_inherit(&p->p_vmspace->vm_map,
324 attach_va, attach_va + size, VM_INHERIT_SHARE);
326 shmmap_s->va = attach_va;
327 shmmap_s->shmid = uap->shmid;
328 shmseg->shm_lpid = p->p_pid;
329 shmseg->shm_atime = time_second;
330 shmseg->shm_nattch++;
331 p->p_retval[0] = attach_va;
336 struct ipc_perm shm_perm; /* operation perms */
337 int shm_segsz; /* size of segment (bytes) */
338 ushort shm_cpid; /* pid, creator */
339 ushort shm_lpid; /* pid, last operation */
340 short shm_nattch; /* no. of current attaches */
341 time_t shm_atime; /* last attach time */
342 time_t shm_dtime; /* last detach time */
343 time_t shm_ctime; /* last change time */
344 void *shm_handle; /* internal handle for shm segment */
347 struct oshmctl_args {
350 struct oshmid_ds *ubuf;
356 struct oshmctl_args *uap;
360 struct shmid_ds *shmseg;
361 struct oshmid_ds outbuf;
363 if (!jail_sysvipc_allowed && p->p_prison != NULL)
366 shmseg = shm_find_segment_by_shmid(uap->shmid);
371 error = ipcperm(p, &shmseg->shm_perm, IPC_R);
374 outbuf.shm_perm = shmseg->shm_perm;
375 outbuf.shm_segsz = shmseg->shm_segsz;
376 outbuf.shm_cpid = shmseg->shm_cpid;
377 outbuf.shm_lpid = shmseg->shm_lpid;
378 outbuf.shm_nattch = shmseg->shm_nattch;
379 outbuf.shm_atime = shmseg->shm_atime;
380 outbuf.shm_dtime = shmseg->shm_dtime;
381 outbuf.shm_ctime = shmseg->shm_ctime;
382 outbuf.shm_handle = shmseg->shm_internal;
383 error = copyout((caddr_t)&outbuf, uap->ubuf, sizeof(outbuf));
388 /* XXX casting to (sy_call_t *) is bogus, as usual. */
389 return ((sy_call_t *)shmctl)(p, uap);
397 #ifndef _SYS_SYSPROTO_H_
401 struct shmid_ds *buf;
408 struct shmctl_args *uap;
411 struct shmid_ds inbuf;
412 struct shmid_ds *shmseg;
414 if (!jail_sysvipc_allowed && p->p_prison != NULL)
417 shmseg = shm_find_segment_by_shmid(uap->shmid);
422 error = ipcperm(p, &shmseg->shm_perm, IPC_R);
425 error = copyout((caddr_t)shmseg, uap->buf, sizeof(inbuf));
430 error = ipcperm(p, &shmseg->shm_perm, IPC_M);
433 error = copyin(uap->buf, (caddr_t)&inbuf, sizeof(inbuf));
436 shmseg->shm_perm.uid = inbuf.shm_perm.uid;
437 shmseg->shm_perm.gid = inbuf.shm_perm.gid;
438 shmseg->shm_perm.mode =
439 (shmseg->shm_perm.mode & ~ACCESSPERMS) |
440 (inbuf.shm_perm.mode & ACCESSPERMS);
441 shmseg->shm_ctime = time_second;
444 error = ipcperm(p, &shmseg->shm_perm, IPC_M);
447 shmseg->shm_perm.key = IPC_PRIVATE;
448 shmseg->shm_perm.mode |= SHMSEG_REMOVED;
449 if (shmseg->shm_nattch <= 0) {
450 shm_deallocate_segment(shmseg);
451 shm_last_free = IPCID_TO_IX(uap->shmid);
464 #ifndef _SYS_SYSPROTO_H_
473 shmget_existing(p, uap, mode, segnum)
475 struct shmget_args *uap;
479 struct shmid_ds *shmseg;
482 shmseg = &shmsegs[segnum];
483 if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
485 * This segment is in the process of being allocated. Wait
486 * until it's done, and look the key up again (in case the
487 * allocation failed or it was freed).
489 shmseg->shm_perm.mode |= SHMSEG_WANTED;
490 error = tsleep((caddr_t)shmseg, PLOCK | PCATCH, "shmget", 0);
495 if ((uap->shmflg & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL))
497 error = ipcperm(p, &shmseg->shm_perm, mode);
500 if (uap->size && uap->size > shmseg->shm_segsz)
502 p->p_retval[0] = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
507 shmget_allocate_segment(p, uap, mode)
509 struct shmget_args *uap;
512 int i, segnum, shmid, size;
513 struct ucred *cred = p->p_ucred;
514 struct shmid_ds *shmseg;
515 struct shm_handle *shm_handle;
517 if (uap->size < shminfo.shmmin || uap->size > shminfo.shmmax)
519 if (shm_nused >= shminfo.shmmni) /* any shmids left? */
521 size = round_page(uap->size);
522 if (shm_committed + btoc(size) > shminfo.shmall)
524 if (shm_last_free < 0) {
525 shmrealloc(); /* maybe expand the shmsegs[] array */
526 for (i = 0; i < shmalloced; i++)
527 if (shmsegs[i].shm_perm.mode & SHMSEG_FREE)
533 segnum = shm_last_free;
536 shmseg = &shmsegs[segnum];
538 * In case we sleep in malloc(), mark the segment present but deleted
539 * so that noone else tries to create the same key.
541 shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
542 shmseg->shm_perm.key = uap->key;
543 shmseg->shm_perm.seq = (shmseg->shm_perm.seq + 1) & 0x7fff;
544 shm_handle = (struct shm_handle *)
545 malloc(sizeof(struct shm_handle), M_SHM, M_WAITOK);
546 shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
549 * We make sure that we have allocated a pager before we need
553 shm_handle->shm_object =
554 vm_pager_allocate(OBJT_PHYS, 0, size, VM_PROT_DEFAULT, 0);
556 shm_handle->shm_object =
557 vm_pager_allocate(OBJT_SWAP, 0, size, VM_PROT_DEFAULT, 0);
559 vm_object_clear_flag(shm_handle->shm_object, OBJ_ONEMAPPING);
560 vm_object_set_flag(shm_handle->shm_object, OBJ_NOSPLIT);
562 shmseg->shm_internal = shm_handle;
563 shmseg->shm_perm.cuid = shmseg->shm_perm.uid = cred->cr_uid;
564 shmseg->shm_perm.cgid = shmseg->shm_perm.gid = cred->cr_gid;
565 shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) |
566 (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
567 shmseg->shm_segsz = uap->size;
568 shmseg->shm_cpid = p->p_pid;
569 shmseg->shm_lpid = shmseg->shm_nattch = 0;
570 shmseg->shm_atime = shmseg->shm_dtime = 0;
571 shmseg->shm_ctime = time_second;
572 shm_committed += btoc(size);
574 if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
576 * Somebody else wanted this key while we were asleep. Wake
579 shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
580 wakeup((caddr_t)shmseg);
582 p->p_retval[0] = shmid;
589 struct shmget_args *uap;
591 int segnum, mode, error;
593 if (!jail_sysvipc_allowed && p->p_prison != NULL)
596 mode = uap->shmflg & ACCESSPERMS;
597 if (uap->key != IPC_PRIVATE) {
599 segnum = shm_find_segment_by_key(uap->key);
601 error = shmget_existing(p, uap, mode, segnum);
606 if ((uap->shmflg & IPC_CREAT) == 0)
609 return shmget_allocate_segment(p, uap, mode);
615 /* XXX actually varargs. */
616 struct shmsys_args /* {
624 if (!jail_sysvipc_allowed && p->p_prison != NULL)
627 if (uap->which >= sizeof(shmcalls)/sizeof(shmcalls[0]))
629 return ((*shmcalls[uap->which])(p, &uap->a2));
634 struct proc *p1, *p2;
636 struct shmmap_state *shmmap_s;
640 size = shminfo.shmseg * sizeof(struct shmmap_state);
641 shmmap_s = malloc(size, M_SHM, M_WAITOK);
642 bcopy((caddr_t)p1->p_vmspace->vm_shm, (caddr_t)shmmap_s, size);
643 p2->p_vmspace->vm_shm = (caddr_t)shmmap_s;
644 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
645 if (shmmap_s->shmid != -1)
646 shmsegs[IPCID_TO_IX(shmmap_s->shmid)].shm_nattch++;
653 struct shmmap_state *shmmap_s;
656 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
657 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
658 if (shmmap_s->shmid != -1)
659 shm_delete_mapping(p, shmmap_s);
660 free((caddr_t)p->p_vmspace->vm_shm, M_SHM);
661 p->p_vmspace->vm_shm = NULL;
668 struct shmid_ds *newsegs;
670 if (shmalloced >= shminfo.shmmni)
673 newsegs = malloc(shminfo.shmmni * sizeof(*newsegs), M_SHM, M_WAITOK);
676 for (i = 0; i < shmalloced; i++)
677 bcopy(&shmsegs[i], &newsegs[i], sizeof(newsegs[0]));
678 for (; i < shminfo.shmmni; i++) {
679 shmsegs[i].shm_perm.mode = SHMSEG_FREE;
680 shmsegs[i].shm_perm.seq = 0;
682 free(shmsegs, M_SHM);
684 shmalloced = shminfo.shmmni;
693 shminfo.shmmax = shminfo.shmall * PAGE_SIZE;
694 shmalloced = shminfo.shmmni;
695 shmsegs = malloc(shmalloced * sizeof(shmsegs[0]), M_SHM, M_WAITOK);
697 panic("cannot allocate initial memory for sysvshm");
698 for (i = 0; i < shmalloced; i++) {
699 shmsegs[i].shm_perm.mode = SHMSEG_FREE;
700 shmsegs[i].shm_perm.seq = 0;
706 SYSINIT(sysv_shm, SI_SUB_SYSV_SHM, SI_ORDER_FIRST, shminit, NULL);