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>
50 #include <sys/mplock2.h>
53 #include <vm/vm_param.h>
56 #include <vm/vm_object.h>
57 #include <vm/vm_map.h>
58 #include <vm/vm_page.h>
59 #include <vm/vm_pager.h>
61 static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments");
64 static int sys_oshmctl (struct proc *p, struct oshmctl_args *uap);
66 static int shmget_allocate_segment (struct proc *p, struct shmget_args *uap, int mode);
67 static int shmget_existing (struct proc *p, struct shmget_args *uap, int mode, int segnum);
69 /* XXX casting to (sy_call_t *) is bogus, as usual. */
70 static sy_call_t *shmcalls[] = {
71 (sy_call_t *)sys_shmat, (sy_call_t *)sys_oshmctl,
72 (sy_call_t *)sys_shmdt, (sy_call_t *)sys_shmget,
73 (sy_call_t *)sys_shmctl
76 #define SHMSEG_FREE 0x0200
77 #define SHMSEG_REMOVED 0x0400
78 #define SHMSEG_ALLOCATED 0x0800
79 #define SHMSEG_WANTED 0x1000
81 static int shm_last_free, shm_committed, shmalloced;
83 static struct shmid_ds *shmsegs;
86 /* vm_offset_t kva; */
87 vm_object_t shm_object;
95 static void shm_deallocate_segment (struct shmid_ds *);
96 static int shm_find_segment_by_key (key_t);
97 static struct shmid_ds *shm_find_segment_by_shmid (int);
98 static int shm_delete_mapping (struct vmspace *vm, struct shmmap_state *);
99 static void shmrealloc (void);
100 static void shminit (void *);
106 #define SHMMAXPGS 8192 /* note: sysv shared memory is swap backed */
109 #define SHMMAX (SHMMAXPGS*PAGE_SIZE)
121 #define SHMALL (SHMMAXPGS)
124 struct shminfo shminfo = {
132 static int shm_use_phys;
134 TUNABLE_INT("kern.ipc.shmmin", &shminfo.shmmin);
135 TUNABLE_INT("kern.ipc.shmmni", &shminfo.shmmni);
136 TUNABLE_INT("kern.ipc.shmseg", &shminfo.shmseg);
137 TUNABLE_INT("kern.ipc.shmmaxpgs", &shminfo.shmall);
138 TUNABLE_INT("kern.ipc.shm_use_phys", &shm_use_phys);
140 SYSCTL_INT(_kern_ipc, OID_AUTO, shmmax, CTLFLAG_RW, &shminfo.shmmax, 0,
141 "Max shared memory segment size");
142 SYSCTL_INT(_kern_ipc, OID_AUTO, shmmin, CTLFLAG_RW, &shminfo.shmmin, 0,
143 "Min shared memory segment size");
144 SYSCTL_INT(_kern_ipc, OID_AUTO, shmmni, CTLFLAG_RD, &shminfo.shmmni, 0,
145 "Max number of shared memory identifiers");
146 SYSCTL_INT(_kern_ipc, OID_AUTO, shmseg, CTLFLAG_RW, &shminfo.shmseg, 0,
147 "Max shared memory segments per process");
148 SYSCTL_INT(_kern_ipc, OID_AUTO, shmall, CTLFLAG_RW, &shminfo.shmall, 0,
149 "Max pages of shared memory");
150 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_use_phys, CTLFLAG_RW, &shm_use_phys, 0,
151 "Use phys pager allocation instead of swap pager allocation");
154 shm_find_segment_by_key(key_t key)
158 for (i = 0; i < shmalloced; i++)
159 if ((shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED) &&
160 shmsegs[i].shm_perm.key == key)
165 static struct shmid_ds *
166 shm_find_segment_by_shmid(int shmid)
169 struct shmid_ds *shmseg;
171 segnum = IPCID_TO_IX(shmid);
172 if (segnum < 0 || segnum >= shmalloced)
174 shmseg = &shmsegs[segnum];
175 if ((shmseg->shm_perm.mode & (SHMSEG_ALLOCATED | SHMSEG_REMOVED))
176 != SHMSEG_ALLOCATED ||
177 shmseg->shm_perm.seq != IPCID_TO_SEQ(shmid))
183 shm_deallocate_segment(struct shmid_ds *shmseg)
185 struct shm_handle *shm_handle;
188 shm_handle = shmseg->shm_internal;
189 vm_object_deallocate(shm_handle->shm_object);
190 kfree((caddr_t)shm_handle, M_SHM);
191 shmseg->shm_internal = NULL;
192 size = round_page(shmseg->shm_segsz);
193 shm_committed -= btoc(size);
195 shmseg->shm_perm.mode = SHMSEG_FREE;
199 shm_delete_mapping(struct vmspace *vm, struct shmmap_state *shmmap_s)
201 struct shmid_ds *shmseg;
205 segnum = IPCID_TO_IX(shmmap_s->shmid);
206 shmseg = &shmsegs[segnum];
207 size = round_page(shmseg->shm_segsz);
208 result = vm_map_remove(&vm->vm_map, shmmap_s->va, shmmap_s->va + size);
209 if (result != KERN_SUCCESS)
211 shmmap_s->shmid = -1;
212 shmseg->shm_dtime = time_second;
213 if ((--shmseg->shm_nattch <= 0) &&
214 (shmseg->shm_perm.mode & SHMSEG_REMOVED)) {
215 shm_deallocate_segment(shmseg);
216 shm_last_free = segnum;
225 sys_shmdt(struct shmdt_args *uap)
227 struct thread *td = curthread;
228 struct proc *p = td->td_proc;
229 struct shmmap_state *shmmap_s;
233 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
237 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
238 if (shmmap_s == NULL) {
242 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) {
243 if (shmmap_s->shmid != -1 &&
244 shmmap_s->va == (vm_offset_t)uap->shmaddr)
247 if (i == shminfo.shmseg)
250 error = shm_delete_mapping(p->p_vmspace, shmmap_s);
260 sys_shmat(struct shmat_args *uap)
262 struct thread *td = curthread;
263 struct proc *p = td->td_proc;
265 struct shmid_ds *shmseg;
266 struct shmmap_state *shmmap_s = NULL;
267 struct shm_handle *shm_handle;
268 vm_offset_t attach_va;
273 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
278 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
279 if (shmmap_s == NULL) {
280 size = shminfo.shmseg * sizeof(struct shmmap_state);
281 shmmap_s = kmalloc(size, M_SHM, M_WAITOK);
282 for (i = 0; i < shminfo.shmseg; i++)
283 shmmap_s[i].shmid = -1;
284 if (p->p_vmspace->vm_shm != NULL) {
285 kfree(shmmap_s, M_SHM);
288 p->p_vmspace->vm_shm = (caddr_t)shmmap_s;
290 shmseg = shm_find_segment_by_shmid(uap->shmid);
291 if (shmseg == NULL) {
295 error = ipcperm(p, &shmseg->shm_perm,
296 (uap->shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
299 for (i = 0; i < shminfo.shmseg; i++) {
300 if (shmmap_s->shmid == -1)
304 if (i >= shminfo.shmseg) {
308 size = round_page(shmseg->shm_segsz);
309 #ifdef VM_PROT_READ_IS_EXEC
310 prot = VM_PROT_READ | VM_PROT_EXECUTE;
314 if ((uap->shmflg & SHM_RDONLY) == 0)
315 prot |= VM_PROT_WRITE;
316 flags = MAP_ANON | MAP_SHARED;
319 if (uap->shmflg & SHM_RND) {
320 attach_va = (vm_offset_t)uap->shmaddr & ~(SHMLBA-1);
321 } else if (((vm_offset_t)uap->shmaddr & (SHMLBA-1)) == 0) {
322 attach_va = (vm_offset_t)uap->shmaddr;
329 * This is just a hint to vm_map_find() about where to put it.
331 attach_va = round_page((vm_offset_t)p->p_vmspace->vm_taddr + maxtsiz + maxdsiz);
334 shm_handle = shmseg->shm_internal;
335 vm_object_hold(shm_handle->shm_object);
336 vm_object_reference_locked(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 vm_object_drop(shm_handle->shm_object);
346 if (rv != KERN_SUCCESS) {
347 vm_object_deallocate(shm_handle->shm_object);
351 vm_map_inherit(&p->p_vmspace->vm_map,
352 attach_va, attach_va + size, VM_INHERIT_SHARE);
354 KKASSERT(shmmap_s->shmid == -1);
355 shmmap_s->va = attach_va;
356 shmmap_s->shmid = uap->shmid;
357 shmseg->shm_lpid = p->p_pid;
358 shmseg->shm_atime = time_second;
359 shmseg->shm_nattch++;
360 uap->sysmsg_resultp = (void *)attach_va;
368 struct ipc_perm shm_perm; /* operation perms */
369 int shm_segsz; /* size of segment (bytes) */
370 ushort shm_cpid; /* pid, creator */
371 ushort shm_lpid; /* pid, last operation */
372 short shm_nattch; /* no. of current attaches */
373 time_t shm_atime; /* last attach time */
374 time_t shm_dtime; /* last detach time */
375 time_t shm_ctime; /* last change time */
376 void *shm_handle; /* internal handle for shm segment */
379 struct oshmctl_args {
380 struct sysmsg sysmsg;
383 struct oshmid_ds *ubuf;
390 sys_oshmctl(struct proc *p, struct oshmctl_args *uap)
393 struct thread *td = curthread;
394 struct shmid_ds *shmseg;
395 struct oshmid_ds outbuf;
398 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
402 shmseg = shm_find_segment_by_shmid(uap->shmid);
403 if (shmseg == NULL) {
410 error = ipcperm(p, &shmseg->shm_perm, IPC_R);
413 outbuf.shm_perm = shmseg->shm_perm;
414 outbuf.shm_segsz = shmseg->shm_segsz;
415 outbuf.shm_cpid = shmseg->shm_cpid;
416 outbuf.shm_lpid = shmseg->shm_lpid;
417 outbuf.shm_nattch = shmseg->shm_nattch;
418 outbuf.shm_atime = shmseg->shm_atime;
419 outbuf.shm_dtime = shmseg->shm_dtime;
420 outbuf.shm_ctime = shmseg->shm_ctime;
421 outbuf.shm_handle = shmseg->shm_internal;
422 error = copyout((caddr_t)&outbuf, uap->ubuf, sizeof(outbuf));
425 /* XXX casting to (sy_call_t *) is bogus, as usual. */
426 error = sys_shmctl((struct shmctl_args *)uap);
440 sys_shmctl(struct shmctl_args *uap)
442 struct thread *td = curthread;
443 struct proc *p = td->td_proc;
445 struct shmid_ds inbuf;
446 struct shmid_ds *shmseg;
448 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
452 shmseg = shm_find_segment_by_shmid(uap->shmid);
453 if (shmseg == NULL) {
460 error = ipcperm(p, &shmseg->shm_perm, IPC_R);
462 error = copyout(shmseg, uap->buf, sizeof(inbuf));
465 error = ipcperm(p, &shmseg->shm_perm, IPC_M);
467 error = copyin(uap->buf, &inbuf, sizeof(inbuf));
469 shmseg->shm_perm.uid = inbuf.shm_perm.uid;
470 shmseg->shm_perm.gid = inbuf.shm_perm.gid;
471 shmseg->shm_perm.mode =
472 (shmseg->shm_perm.mode & ~ACCESSPERMS) |
473 (inbuf.shm_perm.mode & ACCESSPERMS);
474 shmseg->shm_ctime = time_second;
478 error = ipcperm(p, &shmseg->shm_perm, IPC_M);
480 shmseg->shm_perm.key = IPC_PRIVATE;
481 shmseg->shm_perm.mode |= SHMSEG_REMOVED;
482 if (shmseg->shm_nattch <= 0) {
483 shm_deallocate_segment(shmseg);
484 shm_last_free = IPCID_TO_IX(uap->shmid);
502 shmget_existing(struct proc *p, struct shmget_args *uap, int mode, int segnum)
504 struct shmid_ds *shmseg;
507 shmseg = &shmsegs[segnum];
508 if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
510 * This segment is in the process of being allocated. Wait
511 * until it's done, and look the key up again (in case the
512 * allocation failed or it was freed).
514 shmseg->shm_perm.mode |= SHMSEG_WANTED;
515 error = tsleep((caddr_t)shmseg, PCATCH, "shmget", 0);
520 if ((uap->shmflg & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL))
522 error = ipcperm(p, &shmseg->shm_perm, mode);
525 if (uap->size && uap->size > shmseg->shm_segsz)
527 uap->sysmsg_result = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
532 shmget_allocate_segment(struct proc *p, struct shmget_args *uap, int mode)
534 int i, segnum, shmid, size;
535 struct ucred *cred = p->p_ucred;
536 struct shmid_ds *shmseg;
537 struct shm_handle *shm_handle;
539 if (uap->size < shminfo.shmmin || uap->size > shminfo.shmmax)
541 if (shm_nused >= shminfo.shmmni) /* any shmids left? */
543 size = round_page(uap->size);
544 if (shm_committed + btoc(size) > shminfo.shmall)
546 if (shm_last_free < 0) {
547 shmrealloc(); /* maybe expand the shmsegs[] array */
548 for (i = 0; i < shmalloced; i++)
549 if (shmsegs[i].shm_perm.mode & SHMSEG_FREE)
555 segnum = shm_last_free;
558 shmseg = &shmsegs[segnum];
560 * In case we sleep in malloc(), mark the segment present but deleted
561 * so that noone else tries to create the same key.
563 shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
564 shmseg->shm_perm.key = uap->key;
565 shmseg->shm_perm.seq = (shmseg->shm_perm.seq + 1) & 0x7fff;
566 shm_handle = kmalloc(sizeof(struct shm_handle), M_SHM, M_WAITOK);
567 shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
570 * We make sure that we have allocated a pager before we need
574 shm_handle->shm_object =
575 phys_pager_alloc(NULL, size, VM_PROT_DEFAULT, 0);
577 shm_handle->shm_object =
578 swap_pager_alloc(NULL, size, VM_PROT_DEFAULT, 0);
580 vm_object_clear_flag(shm_handle->shm_object, OBJ_ONEMAPPING);
581 vm_object_set_flag(shm_handle->shm_object, OBJ_NOSPLIT);
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 = 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_second;
593 shm_committed += btoc(size);
595 if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
597 * Somebody else wanted this key while we were asleep. Wake
600 shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
601 wakeup((caddr_t)shmseg);
603 uap->sysmsg_result = shmid;
611 sys_shmget(struct shmget_args *uap)
613 struct thread *td = curthread;
614 struct proc *p = td->td_proc;
615 int segnum, mode, error;
617 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
620 mode = uap->shmflg & ACCESSPERMS;
623 if (uap->key != IPC_PRIVATE) {
625 segnum = shm_find_segment_by_key(uap->key);
627 error = shmget_existing(p, uap, mode, segnum);
632 if ((uap->shmflg & IPC_CREAT) == 0) {
637 error = shmget_allocate_segment(p, uap, mode);
644 * shmsys_args(int which, int a2, ...) (VARARGS)
649 sys_shmsys(struct shmsys_args *uap)
651 struct thread *td = curthread;
652 unsigned int which = (unsigned int)uap->which;
655 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
658 if (which >= NELEM(shmcalls))
661 bcopy(&uap->a2, &uap->which,
662 sizeof(struct shmsys_args) - offsetof(struct shmsys_args, a2));
663 error = ((*shmcalls[which])(uap));
670 shmfork(struct proc *p1, struct proc *p2)
672 struct shmmap_state *shmmap_s;
676 size = shminfo.shmseg * sizeof(struct shmmap_state);
677 shmmap_s = kmalloc(size, M_SHM, M_WAITOK);
678 bcopy((caddr_t)p1->p_vmspace->vm_shm, (caddr_t)shmmap_s, size);
679 p2->p_vmspace->vm_shm = (caddr_t)shmmap_s;
680 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
681 if (shmmap_s->shmid != -1)
682 shmsegs[IPCID_TO_IX(shmmap_s->shmid)].shm_nattch++;
686 shmexit(struct vmspace *vm)
688 struct shmmap_state *base, *shm;
691 if ((base = (struct shmmap_state *)vm->vm_shm) != NULL) {
693 for (i = 0, shm = base; i < shminfo.shmseg; i++, shm++) {
694 if (shm->shmid != -1)
695 shm_delete_mapping(vm, shm);
705 struct shmid_ds *newsegs;
707 if (shmalloced >= shminfo.shmmni)
710 newsegs = kmalloc(shminfo.shmmni * sizeof(*newsegs), M_SHM, M_WAITOK);
711 for (i = 0; i < shmalloced; i++)
712 bcopy(&shmsegs[i], &newsegs[i], sizeof(newsegs[0]));
713 for (; i < shminfo.shmmni; i++) {
714 shmsegs[i].shm_perm.mode = SHMSEG_FREE;
715 shmsegs[i].shm_perm.seq = 0;
717 kfree(shmsegs, M_SHM);
719 shmalloced = shminfo.shmmni;
727 shminfo.shmmax = shminfo.shmall * PAGE_SIZE;
728 shmalloced = shminfo.shmmni;
729 shmsegs = kmalloc(shmalloced * sizeof(shmsegs[0]), M_SHM, M_WAITOK);
730 for (i = 0; i < shmalloced; i++) {
731 shmsegs[i].shm_perm.mode = SHMSEG_FREE;
732 shmsegs[i].shm_perm.seq = 0;
738 SYSINIT(sysv_shm, SI_SUB_SYSV_SHM, SI_ORDER_FIRST, shminit, NULL);