1 /* $FreeBSD: src/sys/kern/sysv_shm.c,v 1.45.2.6 2002/10/22 20:45:03 fjoe Exp $ */
2 /* $DragonFly: src/sys/kern/sysv_shm.c,v 1.3 2003/06/23 17:55:41 dillon Exp $ */
3 /* $NetBSD: sysv_shm.c,v 1.23 1994/07/04 23:25:12 glass Exp $ */
6 * Copyright (c) 1994 Adam Glass and Charles Hannum. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Adam Glass and Charles
20 * 4. The names of the authors may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include "opt_compat.h"
36 #include "opt_sysvipc.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/sysproto.h>
41 #include <sys/kernel.h>
42 #include <sys/sysctl.h>
45 #include <sys/malloc.h>
48 #include <sys/sysent.h>
52 #include <vm/vm_param.h>
55 #include <vm/vm_object.h>
56 #include <vm/vm_map.h>
57 #include <vm/vm_page.h>
58 #include <vm/vm_pager.h>
60 static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments");
63 static int oshmctl __P((struct proc *p, struct oshmctl_args *uap));
65 static int shmget_allocate_segment __P((struct proc *p, struct shmget_args *uap, int mode));
66 static int shmget_existing __P((struct proc *p, struct shmget_args *uap, int mode, int segnum));
68 /* XXX casting to (sy_call_t *) is bogus, as usual. */
69 static sy_call_t *shmcalls[] = {
70 (sy_call_t *)shmat, (sy_call_t *)oshmctl,
71 (sy_call_t *)shmdt, (sy_call_t *)shmget,
75 #define SHMSEG_FREE 0x0200
76 #define SHMSEG_REMOVED 0x0400
77 #define SHMSEG_ALLOCATED 0x0800
78 #define SHMSEG_WANTED 0x1000
80 static int shm_last_free, shm_nused, shm_committed, shmalloced;
81 static struct shmid_ds *shmsegs;
84 /* vm_offset_t kva; */
85 vm_object_t shm_object;
93 static void shm_deallocate_segment __P((struct shmid_ds *));
94 static int shm_find_segment_by_key __P((key_t));
95 static struct shmid_ds *shm_find_segment_by_shmid __P((int));
96 static int shm_delete_mapping __P((struct proc *, struct shmmap_state *));
97 static void shmrealloc __P((void));
98 static void shminit __P((void *));
104 #define SHMMAXPGS 8192 /* note: sysv shared memory is swap backed */
107 #define SHMMAX (SHMMAXPGS*PAGE_SIZE)
119 #define SHMALL (SHMMAXPGS)
122 struct shminfo shminfo = {
130 static int shm_use_phys;
132 TUNABLE_INT("kern.ipc.shmmin", &shminfo.shmmin);
133 TUNABLE_INT("kern.ipc.shmmni", &shminfo.shmmni);
134 TUNABLE_INT("kern.ipc.shmseg", &shminfo.shmseg);
135 TUNABLE_INT("kern.ipc.shmmaxpgs", &shminfo.shmall);
136 TUNABLE_INT("kern.ipc.shm_use_phys", &shm_use_phys);
138 SYSCTL_DECL(_kern_ipc);
139 SYSCTL_INT(_kern_ipc, OID_AUTO, shmmax, CTLFLAG_RW, &shminfo.shmmax, 0, "");
140 SYSCTL_INT(_kern_ipc, OID_AUTO, shmmin, CTLFLAG_RW, &shminfo.shmmin, 0, "");
141 SYSCTL_INT(_kern_ipc, OID_AUTO, shmmni, CTLFLAG_RD, &shminfo.shmmni, 0, "");
142 SYSCTL_INT(_kern_ipc, OID_AUTO, shmseg, CTLFLAG_RW, &shminfo.shmseg, 0, "");
143 SYSCTL_INT(_kern_ipc, OID_AUTO, shmall, CTLFLAG_RW, &shminfo.shmall, 0, "");
144 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_use_phys, CTLFLAG_RW, &shm_use_phys, 0, "");
147 shm_find_segment_by_key(key)
152 for (i = 0; i < shmalloced; i++)
153 if ((shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED) &&
154 shmsegs[i].shm_perm.key == key)
159 static struct shmid_ds *
160 shm_find_segment_by_shmid(shmid)
164 struct shmid_ds *shmseg;
166 segnum = IPCID_TO_IX(shmid);
167 if (segnum < 0 || segnum >= shmalloced)
169 shmseg = &shmsegs[segnum];
170 if ((shmseg->shm_perm.mode & (SHMSEG_ALLOCATED | SHMSEG_REMOVED))
171 != SHMSEG_ALLOCATED ||
172 shmseg->shm_perm.seq != IPCID_TO_SEQ(shmid))
178 shm_deallocate_segment(shmseg)
179 struct shmid_ds *shmseg;
181 struct shm_handle *shm_handle;
184 shm_handle = shmseg->shm_internal;
185 vm_object_deallocate(shm_handle->shm_object);
186 free((caddr_t)shm_handle, M_SHM);
187 shmseg->shm_internal = NULL;
188 size = round_page(shmseg->shm_segsz);
189 shm_committed -= btoc(size);
191 shmseg->shm_perm.mode = SHMSEG_FREE;
195 shm_delete_mapping(p, shmmap_s)
197 struct shmmap_state *shmmap_s;
199 struct shmid_ds *shmseg;
203 segnum = IPCID_TO_IX(shmmap_s->shmid);
204 shmseg = &shmsegs[segnum];
205 size = round_page(shmseg->shm_segsz);
206 result = vm_map_remove(&p->p_vmspace->vm_map, shmmap_s->va, shmmap_s->va + size);
207 if (result != KERN_SUCCESS)
209 shmmap_s->shmid = -1;
210 shmseg->shm_dtime = time_second;
211 if ((--shmseg->shm_nattch <= 0) &&
212 (shmseg->shm_perm.mode & SHMSEG_REMOVED)) {
213 shm_deallocate_segment(shmseg);
214 shm_last_free = segnum;
219 #ifndef _SYS_SYSPROTO_H_
226 shmdt(struct shmdt_args *uap)
228 struct proc *p = curproc;
229 struct shmmap_state *shmmap_s;
232 if (!jail_sysvipc_allowed && p->p_ucred->cr_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_
256 shmat(struct shmat_args *uap)
258 struct proc *p = curproc;
260 struct shmid_ds *shmseg;
261 struct shmmap_state *shmmap_s = NULL;
262 struct shm_handle *shm_handle;
263 vm_offset_t attach_va;
268 if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL)
271 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
272 if (shmmap_s == NULL) {
273 size = shminfo.shmseg * sizeof(struct shmmap_state);
274 shmmap_s = malloc(size, M_SHM, M_WAITOK);
275 for (i = 0; i < shminfo.shmseg; i++)
276 shmmap_s[i].shmid = -1;
277 p->p_vmspace->vm_shm = (caddr_t)shmmap_s;
279 shmseg = shm_find_segment_by_shmid(uap->shmid);
282 error = ipcperm(p, &shmseg->shm_perm,
283 (uap->shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
286 for (i = 0; i < shminfo.shmseg; i++) {
287 if (shmmap_s->shmid == -1)
291 if (i >= shminfo.shmseg)
293 size = round_page(shmseg->shm_segsz);
294 #ifdef VM_PROT_READ_IS_EXEC
295 prot = VM_PROT_READ | VM_PROT_EXECUTE;
299 if ((uap->shmflg & SHM_RDONLY) == 0)
300 prot |= VM_PROT_WRITE;
301 flags = MAP_ANON | MAP_SHARED;
304 if (uap->shmflg & SHM_RND)
305 attach_va = (vm_offset_t)uap->shmaddr & ~(SHMLBA-1);
306 else if (((vm_offset_t)uap->shmaddr & (SHMLBA-1)) == 0)
307 attach_va = (vm_offset_t)uap->shmaddr;
311 /* This is just a hint to vm_map_find() about where to put it. */
312 attach_va = round_page((vm_offset_t)p->p_vmspace->vm_taddr + maxtsiz + maxdsiz);
315 shm_handle = shmseg->shm_internal;
316 vm_object_reference(shm_handle->shm_object);
317 rv = vm_map_find(&p->p_vmspace->vm_map, shm_handle->shm_object,
318 0, &attach_va, size, (flags & MAP_FIXED)?0:1, prot, prot, 0);
319 if (rv != KERN_SUCCESS) {
322 vm_map_inherit(&p->p_vmspace->vm_map,
323 attach_va, attach_va + size, VM_INHERIT_SHARE);
325 shmmap_s->va = attach_va;
326 shmmap_s->shmid = uap->shmid;
327 shmseg->shm_lpid = p->p_pid;
328 shmseg->shm_atime = time_second;
329 shmseg->shm_nattch++;
330 p->p_retval[0] = attach_va;
335 struct ipc_perm shm_perm; /* operation perms */
336 int shm_segsz; /* size of segment (bytes) */
337 ushort shm_cpid; /* pid, creator */
338 ushort shm_lpid; /* pid, last operation */
339 short shm_nattch; /* no. of current attaches */
340 time_t shm_atime; /* last attach time */
341 time_t shm_dtime; /* last detach time */
342 time_t shm_ctime; /* last change time */
343 void *shm_handle; /* internal handle for shm segment */
346 struct oshmctl_args {
349 struct oshmid_ds *ubuf;
355 struct oshmctl_args *uap;
359 struct shmid_ds *shmseg;
360 struct oshmid_ds outbuf;
362 if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL)
365 shmseg = shm_find_segment_by_shmid(uap->shmid);
370 error = ipcperm(p, &shmseg->shm_perm, IPC_R);
373 outbuf.shm_perm = shmseg->shm_perm;
374 outbuf.shm_segsz = shmseg->shm_segsz;
375 outbuf.shm_cpid = shmseg->shm_cpid;
376 outbuf.shm_lpid = shmseg->shm_lpid;
377 outbuf.shm_nattch = shmseg->shm_nattch;
378 outbuf.shm_atime = shmseg->shm_atime;
379 outbuf.shm_dtime = shmseg->shm_dtime;
380 outbuf.shm_ctime = shmseg->shm_ctime;
381 outbuf.shm_handle = shmseg->shm_internal;
382 error = copyout((caddr_t)&outbuf, uap->ubuf, sizeof(outbuf));
387 /* XXX casting to (sy_call_t *) is bogus, as usual. */
388 return ((sy_call_t *)shmctl)(uap);
396 #ifndef _SYS_SYSPROTO_H_
400 struct shmid_ds *buf;
405 shmctl(struct shmctl_args *uap)
407 struct proc *p = curproc;
409 struct shmid_ds inbuf;
410 struct shmid_ds *shmseg;
412 if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL)
415 shmseg = shm_find_segment_by_shmid(uap->shmid);
420 error = ipcperm(p, &shmseg->shm_perm, IPC_R);
423 error = copyout((caddr_t)shmseg, uap->buf, sizeof(inbuf));
428 error = ipcperm(p, &shmseg->shm_perm, IPC_M);
431 error = copyin(uap->buf, (caddr_t)&inbuf, sizeof(inbuf));
434 shmseg->shm_perm.uid = inbuf.shm_perm.uid;
435 shmseg->shm_perm.gid = inbuf.shm_perm.gid;
436 shmseg->shm_perm.mode =
437 (shmseg->shm_perm.mode & ~ACCESSPERMS) |
438 (inbuf.shm_perm.mode & ACCESSPERMS);
439 shmseg->shm_ctime = time_second;
442 error = ipcperm(p, &shmseg->shm_perm, IPC_M);
445 shmseg->shm_perm.key = IPC_PRIVATE;
446 shmseg->shm_perm.mode |= SHMSEG_REMOVED;
447 if (shmseg->shm_nattch <= 0) {
448 shm_deallocate_segment(shmseg);
449 shm_last_free = IPCID_TO_IX(uap->shmid);
462 #ifndef _SYS_SYSPROTO_H_
471 shmget_existing(p, uap, mode, segnum)
473 struct shmget_args *uap;
477 struct shmid_ds *shmseg;
480 shmseg = &shmsegs[segnum];
481 if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
483 * This segment is in the process of being allocated. Wait
484 * until it's done, and look the key up again (in case the
485 * allocation failed or it was freed).
487 shmseg->shm_perm.mode |= SHMSEG_WANTED;
488 error = tsleep((caddr_t)shmseg, PLOCK | PCATCH, "shmget", 0);
493 if ((uap->shmflg & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL))
495 error = ipcperm(p, &shmseg->shm_perm, mode);
498 if (uap->size && uap->size > shmseg->shm_segsz)
500 p->p_retval[0] = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
505 shmget_allocate_segment(p, uap, mode)
507 struct shmget_args *uap;
510 int i, segnum, shmid, size;
511 struct ucred *cred = p->p_ucred;
512 struct shmid_ds *shmseg;
513 struct shm_handle *shm_handle;
515 if (uap->size < shminfo.shmmin || uap->size > shminfo.shmmax)
517 if (shm_nused >= shminfo.shmmni) /* any shmids left? */
519 size = round_page(uap->size);
520 if (shm_committed + btoc(size) > shminfo.shmall)
522 if (shm_last_free < 0) {
523 shmrealloc(); /* maybe expand the shmsegs[] array */
524 for (i = 0; i < shmalloced; i++)
525 if (shmsegs[i].shm_perm.mode & SHMSEG_FREE)
531 segnum = shm_last_free;
534 shmseg = &shmsegs[segnum];
536 * In case we sleep in malloc(), mark the segment present but deleted
537 * so that noone else tries to create the same key.
539 shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
540 shmseg->shm_perm.key = uap->key;
541 shmseg->shm_perm.seq = (shmseg->shm_perm.seq + 1) & 0x7fff;
542 shm_handle = (struct shm_handle *)
543 malloc(sizeof(struct shm_handle), M_SHM, M_WAITOK);
544 shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
547 * We make sure that we have allocated a pager before we need
551 shm_handle->shm_object =
552 vm_pager_allocate(OBJT_PHYS, 0, size, VM_PROT_DEFAULT, 0);
554 shm_handle->shm_object =
555 vm_pager_allocate(OBJT_SWAP, 0, size, VM_PROT_DEFAULT, 0);
557 vm_object_clear_flag(shm_handle->shm_object, OBJ_ONEMAPPING);
558 vm_object_set_flag(shm_handle->shm_object, OBJ_NOSPLIT);
560 shmseg->shm_internal = shm_handle;
561 shmseg->shm_perm.cuid = shmseg->shm_perm.uid = cred->cr_uid;
562 shmseg->shm_perm.cgid = shmseg->shm_perm.gid = cred->cr_gid;
563 shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) |
564 (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
565 shmseg->shm_segsz = uap->size;
566 shmseg->shm_cpid = p->p_pid;
567 shmseg->shm_lpid = shmseg->shm_nattch = 0;
568 shmseg->shm_atime = shmseg->shm_dtime = 0;
569 shmseg->shm_ctime = time_second;
570 shm_committed += btoc(size);
572 if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
574 * Somebody else wanted this key while we were asleep. Wake
577 shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
578 wakeup((caddr_t)shmseg);
580 p->p_retval[0] = shmid;
585 shmget(struct shmget_args *uap)
587 struct proc *p = curproc;
588 int segnum, mode, error;
590 if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL)
593 mode = uap->shmflg & ACCESSPERMS;
594 if (uap->key != IPC_PRIVATE) {
596 segnum = shm_find_segment_by_key(uap->key);
598 error = shmget_existing(p, uap, mode, segnum);
603 if ((uap->shmflg & IPC_CREAT) == 0)
606 return shmget_allocate_segment(p, uap, mode);
610 * shmsys_args(u_int which, int a2, ...) (VARARGS)
613 shmsys(struct shmsys_args *uap)
615 struct proc *p = curproc;
617 if (!jail_sysvipc_allowed && p->p_ucred->cr_prison != NULL)
620 if (uap->which >= sizeof(shmcalls)/sizeof(shmcalls[0]))
622 return ((*shmcalls[uap->which])(&uap->a2));
627 struct proc *p1, *p2;
629 struct shmmap_state *shmmap_s;
633 size = shminfo.shmseg * sizeof(struct shmmap_state);
634 shmmap_s = malloc(size, M_SHM, M_WAITOK);
635 bcopy((caddr_t)p1->p_vmspace->vm_shm, (caddr_t)shmmap_s, size);
636 p2->p_vmspace->vm_shm = (caddr_t)shmmap_s;
637 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
638 if (shmmap_s->shmid != -1)
639 shmsegs[IPCID_TO_IX(shmmap_s->shmid)].shm_nattch++;
646 struct shmmap_state *shmmap_s;
649 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
650 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
651 if (shmmap_s->shmid != -1)
652 shm_delete_mapping(p, shmmap_s);
653 free((caddr_t)p->p_vmspace->vm_shm, M_SHM);
654 p->p_vmspace->vm_shm = NULL;
661 struct shmid_ds *newsegs;
663 if (shmalloced >= shminfo.shmmni)
666 newsegs = malloc(shminfo.shmmni * sizeof(*newsegs), M_SHM, M_WAITOK);
669 for (i = 0; i < shmalloced; i++)
670 bcopy(&shmsegs[i], &newsegs[i], sizeof(newsegs[0]));
671 for (; i < shminfo.shmmni; i++) {
672 shmsegs[i].shm_perm.mode = SHMSEG_FREE;
673 shmsegs[i].shm_perm.seq = 0;
675 free(shmsegs, M_SHM);
677 shmalloced = shminfo.shmmni;
686 shminfo.shmmax = shminfo.shmall * PAGE_SIZE;
687 shmalloced = shminfo.shmmni;
688 shmsegs = malloc(shmalloced * sizeof(shmsegs[0]), M_SHM, M_WAITOK);
690 panic("cannot allocate initial memory for sysvshm");
691 for (i = 0; i < shmalloced; i++) {
692 shmsegs[i].shm_perm.mode = SHMSEG_FREE;
693 shmsegs[i].shm_perm.seq = 0;
699 SYSINIT(sysv_shm, SI_SUB_SYSV_SHM, SI_ORDER_FIRST, shminit, NULL);