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.21 2008/01/06 16:55:51 swildner 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>
51 #include <sys/mplock2.h>
54 #include <vm/vm_param.h>
57 #include <vm/vm_object.h>
58 #include <vm/vm_map.h>
59 #include <vm/vm_page.h>
60 #include <vm/vm_pager.h>
62 static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments");
65 static int sys_oshmctl (struct proc *p, struct oshmctl_args *uap);
67 static int shmget_allocate_segment (struct proc *p, struct shmget_args *uap, int mode);
68 static int shmget_existing (struct proc *p, struct shmget_args *uap, int mode, int segnum);
70 /* XXX casting to (sy_call_t *) is bogus, as usual. */
71 static sy_call_t *shmcalls[] = {
72 (sy_call_t *)sys_shmat, (sy_call_t *)sys_oshmctl,
73 (sy_call_t *)sys_shmdt, (sy_call_t *)sys_shmget,
74 (sy_call_t *)sys_shmctl
77 #define SHMSEG_FREE 0x0200
78 #define SHMSEG_REMOVED 0x0400
79 #define SHMSEG_ALLOCATED 0x0800
80 #define SHMSEG_WANTED 0x1000
82 static int shm_last_free, shm_committed, shmalloced;
84 static struct shmid_ds *shmsegs;
87 /* vm_offset_t kva; */
88 vm_object_t shm_object;
96 static void shm_deallocate_segment (struct shmid_ds *);
97 static int shm_find_segment_by_key (key_t);
98 static struct shmid_ds *shm_find_segment_by_shmid (int);
99 static int shm_delete_mapping (struct vmspace *vm, struct shmmap_state *);
100 static void shmrealloc (void);
101 static void shminit (void *);
107 #define SHMMAXPGS 8192 /* note: sysv shared memory is swap backed */
110 #define SHMMAX (SHMMAXPGS*PAGE_SIZE)
122 #define SHMALL (SHMMAXPGS)
125 struct shminfo shminfo = {
133 static int shm_use_phys;
135 TUNABLE_INT("kern.ipc.shmmin", &shminfo.shmmin);
136 TUNABLE_INT("kern.ipc.shmmni", &shminfo.shmmni);
137 TUNABLE_INT("kern.ipc.shmseg", &shminfo.shmseg);
138 TUNABLE_INT("kern.ipc.shmmaxpgs", &shminfo.shmall);
139 TUNABLE_INT("kern.ipc.shm_use_phys", &shm_use_phys);
141 SYSCTL_INT(_kern_ipc, OID_AUTO, shmmax, CTLFLAG_RW, &shminfo.shmmax, 0,
142 "Max shared memory segment size");
143 SYSCTL_INT(_kern_ipc, OID_AUTO, shmmin, CTLFLAG_RW, &shminfo.shmmin, 0,
144 "Min shared memory segment size");
145 SYSCTL_INT(_kern_ipc, OID_AUTO, shmmni, CTLFLAG_RD, &shminfo.shmmni, 0,
146 "Max number of shared memory identifiers");
147 SYSCTL_INT(_kern_ipc, OID_AUTO, shmseg, CTLFLAG_RW, &shminfo.shmseg, 0,
148 "Max shared memory segments per process");
149 SYSCTL_INT(_kern_ipc, OID_AUTO, shmall, CTLFLAG_RW, &shminfo.shmall, 0,
150 "Max pages of shared memory");
151 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_use_phys, CTLFLAG_RW, &shm_use_phys, 0,
152 "Use phys pager allocation instead of swap pager allocation");
155 shm_find_segment_by_key(key_t key)
159 for (i = 0; i < shmalloced; i++)
160 if ((shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED) &&
161 shmsegs[i].shm_perm.key == key)
166 static struct shmid_ds *
167 shm_find_segment_by_shmid(int shmid)
170 struct shmid_ds *shmseg;
172 segnum = IPCID_TO_IX(shmid);
173 if (segnum < 0 || segnum >= shmalloced)
175 shmseg = &shmsegs[segnum];
176 if ((shmseg->shm_perm.mode & (SHMSEG_ALLOCATED | SHMSEG_REMOVED))
177 != SHMSEG_ALLOCATED ||
178 shmseg->shm_perm.seq != IPCID_TO_SEQ(shmid))
184 shm_deallocate_segment(struct shmid_ds *shmseg)
186 struct shm_handle *shm_handle;
189 shm_handle = shmseg->shm_internal;
190 vm_object_deallocate(shm_handle->shm_object);
191 kfree((caddr_t)shm_handle, M_SHM);
192 shmseg->shm_internal = NULL;
193 size = round_page(shmseg->shm_segsz);
194 shm_committed -= btoc(size);
196 shmseg->shm_perm.mode = SHMSEG_FREE;
200 shm_delete_mapping(struct vmspace *vm, struct shmmap_state *shmmap_s)
202 struct shmid_ds *shmseg;
206 segnum = IPCID_TO_IX(shmmap_s->shmid);
207 shmseg = &shmsegs[segnum];
208 size = round_page(shmseg->shm_segsz);
209 result = vm_map_remove(&vm->vm_map, shmmap_s->va, shmmap_s->va + size);
210 if (result != KERN_SUCCESS)
212 shmmap_s->shmid = -1;
213 shmseg->shm_dtime = time_second;
214 if ((--shmseg->shm_nattch <= 0) &&
215 (shmseg->shm_perm.mode & SHMSEG_REMOVED)) {
216 shm_deallocate_segment(shmseg);
217 shm_last_free = segnum;
226 sys_shmdt(struct shmdt_args *uap)
228 struct thread *td = curthread;
229 struct proc *p = td->td_proc;
230 struct shmmap_state *shmmap_s;
234 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
238 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
239 if (shmmap_s == NULL) {
243 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) {
244 if (shmmap_s->shmid != -1 &&
245 shmmap_s->va == (vm_offset_t)uap->shmaddr)
248 if (i == shminfo.shmseg)
251 error = shm_delete_mapping(p->p_vmspace, shmmap_s);
261 sys_shmat(struct shmat_args *uap)
263 struct thread *td = curthread;
264 struct proc *p = td->td_proc;
266 struct shmid_ds *shmseg;
267 struct shmmap_state *shmmap_s = NULL;
268 struct shm_handle *shm_handle;
269 vm_offset_t attach_va;
274 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
279 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
280 if (shmmap_s == NULL) {
281 size = shminfo.shmseg * sizeof(struct shmmap_state);
282 shmmap_s = kmalloc(size, M_SHM, M_WAITOK);
283 for (i = 0; i < shminfo.shmseg; i++)
284 shmmap_s[i].shmid = -1;
285 if (p->p_vmspace->vm_shm != NULL) {
286 kfree(shmmap_s, M_SHM);
289 p->p_vmspace->vm_shm = (caddr_t)shmmap_s;
291 shmseg = shm_find_segment_by_shmid(uap->shmid);
292 if (shmseg == NULL) {
296 error = ipcperm(p, &shmseg->shm_perm,
297 (uap->shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
300 for (i = 0; i < shminfo.shmseg; i++) {
301 if (shmmap_s->shmid == -1)
305 if (i >= shminfo.shmseg) {
309 size = round_page(shmseg->shm_segsz);
310 #ifdef VM_PROT_READ_IS_EXEC
311 prot = VM_PROT_READ | VM_PROT_EXECUTE;
315 if ((uap->shmflg & SHM_RDONLY) == 0)
316 prot |= VM_PROT_WRITE;
317 flags = MAP_ANON | MAP_SHARED;
320 if (uap->shmflg & SHM_RND) {
321 attach_va = (vm_offset_t)uap->shmaddr & ~(SHMLBA-1);
322 } else if (((vm_offset_t)uap->shmaddr & (SHMLBA-1)) == 0) {
323 attach_va = (vm_offset_t)uap->shmaddr;
330 * This is just a hint to vm_map_find() about where to put it.
332 attach_va = round_page((vm_offset_t)p->p_vmspace->vm_taddr + maxtsiz + maxdsiz);
335 shm_handle = shmseg->shm_internal;
336 vm_object_reference(shm_handle->shm_object);
337 rv = vm_map_find(&p->p_vmspace->vm_map,
338 shm_handle->shm_object, 0,
341 ((flags & MAP_FIXED) ? 0 : 1),
345 if (rv != KERN_SUCCESS) {
346 vm_object_deallocate(shm_handle->shm_object);
350 vm_map_inherit(&p->p_vmspace->vm_map,
351 attach_va, attach_va + size, VM_INHERIT_SHARE);
353 KKASSERT(shmmap_s->shmid == -1);
354 shmmap_s->va = attach_va;
355 shmmap_s->shmid = uap->shmid;
356 shmseg->shm_lpid = p->p_pid;
357 shmseg->shm_atime = time_second;
358 shmseg->shm_nattch++;
359 uap->sysmsg_resultp = (void *)attach_va;
367 struct ipc_perm shm_perm; /* operation perms */
368 int shm_segsz; /* size of segment (bytes) */
369 ushort shm_cpid; /* pid, creator */
370 ushort shm_lpid; /* pid, last operation */
371 short shm_nattch; /* no. of current attaches */
372 time_t shm_atime; /* last attach time */
373 time_t shm_dtime; /* last detach time */
374 time_t shm_ctime; /* last change time */
375 void *shm_handle; /* internal handle for shm segment */
378 struct oshmctl_args {
379 struct sysmsg sysmsg;
382 struct oshmid_ds *ubuf;
389 sys_oshmctl(struct proc *p, struct oshmctl_args *uap)
392 struct thread *td = curthread;
393 struct shmid_ds *shmseg;
394 struct oshmid_ds outbuf;
397 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
401 shmseg = shm_find_segment_by_shmid(uap->shmid);
402 if (shmseg == NULL) {
409 error = ipcperm(p, &shmseg->shm_perm, IPC_R);
412 outbuf.shm_perm = shmseg->shm_perm;
413 outbuf.shm_segsz = shmseg->shm_segsz;
414 outbuf.shm_cpid = shmseg->shm_cpid;
415 outbuf.shm_lpid = shmseg->shm_lpid;
416 outbuf.shm_nattch = shmseg->shm_nattch;
417 outbuf.shm_atime = shmseg->shm_atime;
418 outbuf.shm_dtime = shmseg->shm_dtime;
419 outbuf.shm_ctime = shmseg->shm_ctime;
420 outbuf.shm_handle = shmseg->shm_internal;
421 error = copyout((caddr_t)&outbuf, uap->ubuf, sizeof(outbuf));
424 /* XXX casting to (sy_call_t *) is bogus, as usual. */
425 error = sys_shmctl((struct shmctl_args *)uap);
439 sys_shmctl(struct shmctl_args *uap)
441 struct thread *td = curthread;
442 struct proc *p = td->td_proc;
444 struct shmid_ds inbuf;
445 struct shmid_ds *shmseg;
447 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
451 shmseg = shm_find_segment_by_shmid(uap->shmid);
452 if (shmseg == NULL) {
459 error = ipcperm(p, &shmseg->shm_perm, IPC_R);
461 error = copyout(shmseg, uap->buf, sizeof(inbuf));
464 error = ipcperm(p, &shmseg->shm_perm, IPC_M);
466 error = copyin(uap->buf, &inbuf, sizeof(inbuf));
468 shmseg->shm_perm.uid = inbuf.shm_perm.uid;
469 shmseg->shm_perm.gid = inbuf.shm_perm.gid;
470 shmseg->shm_perm.mode =
471 (shmseg->shm_perm.mode & ~ACCESSPERMS) |
472 (inbuf.shm_perm.mode & ACCESSPERMS);
473 shmseg->shm_ctime = time_second;
477 error = ipcperm(p, &shmseg->shm_perm, IPC_M);
479 shmseg->shm_perm.key = IPC_PRIVATE;
480 shmseg->shm_perm.mode |= SHMSEG_REMOVED;
481 if (shmseg->shm_nattch <= 0) {
482 shm_deallocate_segment(shmseg);
483 shm_last_free = IPCID_TO_IX(uap->shmid);
501 shmget_existing(struct proc *p, struct shmget_args *uap, int mode, int segnum)
503 struct shmid_ds *shmseg;
506 shmseg = &shmsegs[segnum];
507 if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
509 * This segment is in the process of being allocated. Wait
510 * until it's done, and look the key up again (in case the
511 * allocation failed or it was freed).
513 shmseg->shm_perm.mode |= SHMSEG_WANTED;
514 error = tsleep((caddr_t)shmseg, PCATCH, "shmget", 0);
519 if ((uap->shmflg & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL))
521 error = ipcperm(p, &shmseg->shm_perm, mode);
524 if (uap->size && uap->size > shmseg->shm_segsz)
526 uap->sysmsg_result = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
531 shmget_allocate_segment(struct proc *p, struct shmget_args *uap, int mode)
533 int i, segnum, shmid, size;
534 struct ucred *cred = p->p_ucred;
535 struct shmid_ds *shmseg;
536 struct shm_handle *shm_handle;
538 if (uap->size < shminfo.shmmin || uap->size > shminfo.shmmax)
540 if (shm_nused >= shminfo.shmmni) /* any shmids left? */
542 size = round_page(uap->size);
543 if (shm_committed + btoc(size) > shminfo.shmall)
545 if (shm_last_free < 0) {
546 shmrealloc(); /* maybe expand the shmsegs[] array */
547 for (i = 0; i < shmalloced; i++)
548 if (shmsegs[i].shm_perm.mode & SHMSEG_FREE)
554 segnum = shm_last_free;
557 shmseg = &shmsegs[segnum];
559 * In case we sleep in malloc(), mark the segment present but deleted
560 * so that noone else tries to create the same key.
562 shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
563 shmseg->shm_perm.key = uap->key;
564 shmseg->shm_perm.seq = (shmseg->shm_perm.seq + 1) & 0x7fff;
565 shm_handle = kmalloc(sizeof(struct shm_handle), M_SHM, M_WAITOK);
566 shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
569 * We make sure that we have allocated a pager before we need
573 shm_handle->shm_object =
574 phys_pager_alloc(NULL, size, VM_PROT_DEFAULT, 0);
576 shm_handle->shm_object =
577 swap_pager_alloc(NULL, size, VM_PROT_DEFAULT, 0);
579 vm_object_clear_flag(shm_handle->shm_object, OBJ_ONEMAPPING);
580 vm_object_set_flag(shm_handle->shm_object, OBJ_NOSPLIT);
582 shmseg->shm_internal = shm_handle;
583 shmseg->shm_perm.cuid = shmseg->shm_perm.uid = cred->cr_uid;
584 shmseg->shm_perm.cgid = shmseg->shm_perm.gid = cred->cr_gid;
585 shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) |
586 (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
587 shmseg->shm_segsz = uap->size;
588 shmseg->shm_cpid = p->p_pid;
589 shmseg->shm_lpid = shmseg->shm_nattch = 0;
590 shmseg->shm_atime = shmseg->shm_dtime = 0;
591 shmseg->shm_ctime = time_second;
592 shm_committed += btoc(size);
594 if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
596 * Somebody else wanted this key while we were asleep. Wake
599 shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
600 wakeup((caddr_t)shmseg);
602 uap->sysmsg_result = shmid;
610 sys_shmget(struct shmget_args *uap)
612 struct thread *td = curthread;
613 struct proc *p = td->td_proc;
614 int segnum, mode, error;
616 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
619 mode = uap->shmflg & ACCESSPERMS;
622 if (uap->key != IPC_PRIVATE) {
624 segnum = shm_find_segment_by_key(uap->key);
626 error = shmget_existing(p, uap, mode, segnum);
631 if ((uap->shmflg & IPC_CREAT) == 0) {
636 error = shmget_allocate_segment(p, uap, mode);
643 * shmsys_args(int which, int a2, ...) (VARARGS)
648 sys_shmsys(struct shmsys_args *uap)
650 struct thread *td = curthread;
651 unsigned int which = (unsigned int)uap->which;
654 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
657 if (which >= sizeof(shmcalls)/sizeof(shmcalls[0]))
660 bcopy(&uap->a2, &uap->which,
661 sizeof(struct shmsys_args) - offsetof(struct shmsys_args, a2));
662 error = ((*shmcalls[which])(uap));
669 shmfork(struct proc *p1, struct proc *p2)
671 struct shmmap_state *shmmap_s;
675 size = shminfo.shmseg * sizeof(struct shmmap_state);
676 shmmap_s = kmalloc(size, M_SHM, M_WAITOK);
677 bcopy((caddr_t)p1->p_vmspace->vm_shm, (caddr_t)shmmap_s, size);
678 p2->p_vmspace->vm_shm = (caddr_t)shmmap_s;
679 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
680 if (shmmap_s->shmid != -1)
681 shmsegs[IPCID_TO_IX(shmmap_s->shmid)].shm_nattch++;
685 shmexit(struct vmspace *vm)
687 struct shmmap_state *base, *shm;
690 if ((base = (struct shmmap_state *)vm->vm_shm) != NULL) {
692 for (i = 0, shm = base; i < shminfo.shmseg; i++, shm++) {
693 if (shm->shmid != -1)
694 shm_delete_mapping(vm, shm);
704 struct shmid_ds *newsegs;
706 if (shmalloced >= shminfo.shmmni)
709 newsegs = kmalloc(shminfo.shmmni * sizeof(*newsegs), M_SHM, M_WAITOK);
710 for (i = 0; i < shmalloced; i++)
711 bcopy(&shmsegs[i], &newsegs[i], sizeof(newsegs[0]));
712 for (; i < shminfo.shmmni; i++) {
713 shmsegs[i].shm_perm.mode = SHMSEG_FREE;
714 shmsegs[i].shm_perm.seq = 0;
716 kfree(shmsegs, M_SHM);
718 shmalloced = shminfo.shmmni;
726 shminfo.shmmax = shminfo.shmall * PAGE_SIZE;
727 shmalloced = shminfo.shmmni;
728 shmsegs = kmalloc(shmalloced * sizeof(shmsegs[0]), M_SHM, M_WAITOK);
729 for (i = 0; i < shmalloced; i++) {
730 shmsegs[i].shm_perm.mode = SHMSEG_FREE;
731 shmsegs[i].shm_perm.seq = 0;
737 SYSINIT(sysv_shm, SI_SUB_SYSV_SHM, SI_ORDER_FIRST, shminit, NULL);