2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* $NetBSD: sysv_shm.c,v 1.23 1994/07/04 23:25:12 glass Exp $ */
28 * Copyright (c) 1994 Adam Glass and Charles Hannum. All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by Adam Glass and Charles
42 * 4. The names of the authors may not be used to endorse or promote products
43 * derived from this software without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
46 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
48 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
49 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
54 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 #include <sys/appleapiopts.h>
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/kernel.h>
64 #include <sys/malloc.h>
67 #include <sys/sysctl.h>
69 #include <mach/mach_types.h>
70 #include <mach/vm_inherit.h>
71 #include <vm/vm_map.h>
74 extern int shmat
__P((struct proc
*p
, struct shmat_args
*uap
, int *retval
));
76 extern int shmctl
__P((struct proc
*p
, struct shmctl_args
*uap
, int *retval
));
78 extern int shmdt
__P((struct proc
*p
, struct shmdt_args
*uap
, int *retval
));
80 extern int shmget
__P((struct proc
*p
, struct shmget_args
*uap
, int *retval
));
83 static void shminit
__P((void *));
84 SYSINIT(sysv_shm
, SI_SUB_SYSV_SHM
, SI_ORDER_FIRST
, shminit
, NULL
)
88 static int oshmctl
__P((struct proc
*p
, struct oshmctl_args
*uap
, int * retval
));
89 static int shmget_allocate_segment
__P((struct proc
*p
, struct shmget_args
*uap
, int mode
, int * retval
));
90 static int shmget_existing
__P((struct proc
*p
, struct shmget_args
*uap
, int mode
, int segnum
, int * retval
));
92 typedef int sy_call_t
__P((struct proc
*, void *, int *));
94 /* XXX casting to (sy_call_t *) is bogus, as usual. */
95 static sy_call_t
*shmcalls
[] = {
96 (sy_call_t
*)shmat
, (sy_call_t
*)oshmctl
,
97 (sy_call_t
*)shmdt
, (sy_call_t
*)shmget
,
101 #define SHMSEG_FREE 0x0200
102 #define SHMSEG_REMOVED 0x0400
103 #define SHMSEG_ALLOCATED 0x0800
104 #define SHMSEG_WANTED 0x1000
106 static int shm_last_free
, shm_nused
, shm_committed
;
107 struct shmid_ds
*shmsegs
;
108 static int shm_inited
= 0;
111 /* vm_offset_t kva; */
115 struct shmmap_state
{
120 static void shm_deallocate_segment
__P((struct shmid_ds
*));
121 static int shm_find_segment_by_key
__P((key_t
));
122 static struct shmid_ds
*shm_find_segment_by_shmid
__P((int));
123 static int shm_delete_mapping
__P((struct proc
*, struct shmmap_state
*));
125 #ifdef __APPLE_API_PRIVATE
126 struct shminfo shminfo
= {
127 -1, /* SHMMAX 4096 *1024 */
131 -1 /* SHMALL = 1024 */
133 #endif /* __APPLE_API_PRIVATE */
136 shm_find_segment_by_key(key
)
141 for (i
= 0; i
< shminfo
.shmmni
; i
++)
142 if ((shmsegs
[i
].shm_perm
.mode
& SHMSEG_ALLOCATED
) &&
143 shmsegs
[i
].shm_perm
.key
== key
)
148 static struct shmid_ds
*
149 shm_find_segment_by_shmid(shmid
)
153 struct shmid_ds
*shmseg
;
155 segnum
= IPCID_TO_IX(shmid
);
156 if (segnum
< 0 || segnum
>= shminfo
.shmmni
)
158 shmseg
= &shmsegs
[segnum
];
159 if ((shmseg
->shm_perm
.mode
& (SHMSEG_ALLOCATED
| SHMSEG_REMOVED
))
160 != SHMSEG_ALLOCATED
||
161 shmseg
->shm_perm
.seq
!= IPCID_TO_SEQ(shmid
))
167 shm_deallocate_segment(shmseg
)
168 struct shmid_ds
*shmseg
;
170 struct shm_handle
*shm_handle
;
171 struct shmmap_state
*shmmap_s
=NULL
;
175 shm_handle
= shmseg
->shm_internal
;
176 size
= round_page(shmseg
->shm_segsz
);
177 mach_destroy_memory_entry(shm_handle
->shm_object
);
178 FREE((caddr_t
)shm_handle
, M_SHM
);
179 shmseg
->shm_internal
= NULL
;
180 shm_committed
-= btoc(size
);
182 shmseg
->shm_perm
.mode
= SHMSEG_FREE
;
186 shm_delete_mapping(p
, shmmap_s
)
188 struct shmmap_state
*shmmap_s
;
190 struct shmid_ds
*shmseg
;
194 segnum
= IPCID_TO_IX(shmmap_s
->shmid
);
195 shmseg
= &shmsegs
[segnum
];
196 size
= round_page(shmseg
->shm_segsz
);
197 result
= vm_deallocate(current_map(), shmmap_s
->va
, size
);
198 if (result
!= KERN_SUCCESS
)
200 shmmap_s
->shmid
= -1;
201 shmseg
->shm_dtime
= time_second
;
202 if ((--shmseg
->shm_nattch
<= 0) &&
203 (shmseg
->shm_perm
.mode
& SHMSEG_REMOVED
)) {
204 shm_deallocate_segment(shmseg
);
205 shm_last_free
= segnum
;
215 shmdt(p
, uap
, retval
)
217 struct shmdt_args
*uap
;
220 struct shmmap_state
*shmmap_s
;
225 shmmap_s
= (struct shmmap_state
*)p
->vm_shm
;
226 if (shmmap_s
== NULL
)
228 for (i
= 0; i
< shminfo
.shmseg
; i
++, shmmap_s
++)
229 if (shmmap_s
->shmid
!= -1 &&
230 shmmap_s
->va
== (vm_offset_t
)uap
->shmaddr
)
232 if (i
== shminfo
.shmseg
)
234 return shm_delete_mapping(p
, shmmap_s
);
237 #ifndef _SYS_SYSPROTO_H_
246 shmat(p
, uap
, retval
)
248 struct shmat_args
*uap
;
252 struct ucred
*cred
= p
->p_ucred
;
253 struct shmid_ds
*shmseg
;
254 struct shmmap_state
*shmmap_s
= NULL
;
255 struct shm_handle
*shm_handle
;
256 vm_offset_t attach_va
;
263 shmmap_s
= (struct shmmap_state
*)p
->vm_shm
;
264 if (shmmap_s
== NULL
) {
265 size
= shminfo
.shmseg
* sizeof(struct shmmap_state
);
266 shmmap_s
= (struct shmmap_state
*)_MALLOC(size
, M_SHM
, M_WAITOK
);
267 for (i
= 0; i
< shminfo
.shmseg
; i
++)
268 shmmap_s
[i
].shmid
= -1;
269 p
->vm_shm
= (caddr_t
)shmmap_s
;
271 shmseg
= shm_find_segment_by_shmid(uap
->shmid
);
274 error
= ipcperm(cred
, &shmseg
->shm_perm
,
275 (uap
->shmflg
& SHM_RDONLY
) ? IPC_R
: IPC_R
|IPC_W
);
278 for (i
= 0; i
< shminfo
.shmseg
; i
++) {
279 if (shmmap_s
->shmid
== -1)
283 if (i
>= shminfo
.shmseg
)
285 size
= round_page(shmseg
->shm_segsz
);
287 if ((uap
->shmflg
& SHM_RDONLY
) == 0)
288 prot
|= VM_PROT_WRITE
;
289 flags
= MAP_ANON
| MAP_SHARED
;
292 if (uap
->shmflg
& SHM_RND
)
293 attach_va
= (vm_offset_t
)uap
->shmaddr
& ~(SHMLBA
-1);
294 else if (((vm_offset_t
)uap
->shmaddr
& (SHMLBA
-1)) == 0)
295 attach_va
= (vm_offset_t
)uap
->shmaddr
;
299 attach_va
= round_page(uap
->shmaddr
);
302 shm_handle
= shmseg
->shm_internal
;
303 rv
= vm_map(current_map(), &attach_va
, size
, 0, (flags
& MAP_FIXED
)? FALSE
: TRUE
,
304 shm_handle
->shm_object
, 0, FALSE
, prot
, prot
, VM_INHERIT_DEFAULT
);
305 if (rv
!= KERN_SUCCESS
)
307 rv
= vm_inherit(current_map(), attach_va
, size
,
309 if (rv
!= KERN_SUCCESS
) {
310 (void) vm_deallocate(current_map(), attach_va
, size
);
314 shmmap_s
->va
= attach_va
;
315 shmmap_s
->shmid
= uap
->shmid
;
316 shmseg
->shm_lpid
= p
->p_pid
;
317 shmseg
->shm_atime
= time_second
;
318 shmseg
->shm_nattch
++;
323 case KERN_INVALID_ADDRESS
:
326 case KERN_PROTECTION_FAILURE
:
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
;
353 oshmctl(p
, uap
, retval
)
355 struct oshmctl_args
*uap
;
360 struct ucred
*cred
= p
->p_ucred
;
361 struct shmid_ds
*shmseg
;
362 struct oshmid_ds outbuf
;
366 shmseg
= shm_find_segment_by_shmid(uap
->shmid
);
371 error
= ipcperm(cred
, &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
, retval
);
397 #ifndef _SYS_SYSPROTO_H_
401 struct shmid_ds
*buf
;
406 shmctl(p
, uap
, retval
)
408 struct shmctl_args
*uap
;
412 struct ucred
*cred
= p
->p_ucred
;
413 struct shmid_ds inbuf
;
414 struct shmid_ds
*shmseg
;
418 shmseg
= shm_find_segment_by_shmid(uap
->shmid
);
423 error
= ipcperm(cred
, &shmseg
->shm_perm
, IPC_R
);
426 error
= copyout((caddr_t
)shmseg
, uap
->buf
, sizeof(inbuf
));
431 error
= ipcperm(cred
, &shmseg
->shm_perm
, IPC_M
);
434 error
= copyin(uap
->buf
, (caddr_t
)&inbuf
, sizeof(inbuf
));
437 shmseg
->shm_perm
.uid
= inbuf
.shm_perm
.uid
;
438 shmseg
->shm_perm
.gid
= inbuf
.shm_perm
.gid
;
439 shmseg
->shm_perm
.mode
=
440 (shmseg
->shm_perm
.mode
& ~ACCESSPERMS
) |
441 (inbuf
.shm_perm
.mode
& ACCESSPERMS
);
442 shmseg
->shm_ctime
= time_second
;
445 error
= ipcperm(cred
, &shmseg
->shm_perm
, IPC_M
);
448 shmseg
->shm_perm
.key
= IPC_PRIVATE
;
449 shmseg
->shm_perm
.mode
|= SHMSEG_REMOVED
;
450 if (shmseg
->shm_nattch
<= 0) {
451 shm_deallocate_segment(shmseg
);
452 shm_last_free
= IPCID_TO_IX(uap
->shmid
);
465 #ifndef _SYS_SYSPROTO_H_
474 shmget_existing(p
, uap
, mode
, segnum
, retval
)
476 struct shmget_args
*uap
;
481 struct shmid_ds
*shmseg
;
482 struct ucred
*cred
= p
->p_ucred
;
485 shmseg
= &shmsegs
[segnum
];
486 if (shmseg
->shm_perm
.mode
& SHMSEG_REMOVED
) {
488 * This segment is in the process of being allocated. Wait
489 * until it's done, and look the key up again (in case the
490 * allocation failed or it was freed).
492 shmseg
->shm_perm
.mode
|= SHMSEG_WANTED
;
493 error
= tsleep((caddr_t
)shmseg
, PLOCK
| PCATCH
, "shmget", 0);
498 error
= ipcperm(cred
, &shmseg
->shm_perm
, mode
);
501 if (uap
->size
&& uap
->size
> shmseg
->shm_segsz
)
503 if ((uap
->shmflg
& (IPC_CREAT
| IPC_EXCL
)) == (IPC_CREAT
| IPC_EXCL
))
505 *retval
= IXSEQ_TO_IPCID(segnum
, shmseg
->shm_perm
);
510 shmget_allocate_segment(p
, uap
, mode
, retval
)
512 struct shmget_args
*uap
;
516 int i
, segnum
, shmid
, size
;
517 struct ucred
*cred
= p
->p_ucred
;
518 struct shmid_ds
*shmseg
;
519 struct shm_handle
*shm_handle
;
521 vm_offset_t user_addr
;
524 if (uap
->size
< shminfo
.shmmin
|| uap
->size
> shminfo
.shmmax
)
526 if (shm_nused
>= shminfo
.shmmni
) /* any shmids left? */
528 size
= round_page(uap
->size
);
529 if (shm_committed
+ btoc(size
) > shminfo
.shmall
)
531 if (shm_last_free
< 0) {
532 for (i
= 0; i
< shminfo
.shmmni
; i
++)
533 if (shmsegs
[i
].shm_perm
.mode
& SHMSEG_FREE
)
535 if (i
== shminfo
.shmmni
)
536 panic("shmseg free count inconsistent");
539 segnum
= shm_last_free
;
542 shmseg
= &shmsegs
[segnum
];
544 * In case we sleep in malloc(), mark the segment present but deleted
545 * so that noone else tries to create the same key.
547 kret
= vm_allocate(current_map(), &user_addr
, size
, TRUE
);
548 if (kret
!= KERN_SUCCESS
)
551 kret
= mach_make_memory_entry (current_map(), &size
,
552 user_addr
, VM_PROT_DEFAULT
, &mem_object
, 0);
554 if (kret
!= KERN_SUCCESS
)
556 shmseg
->shm_perm
.mode
= SHMSEG_ALLOCATED
| SHMSEG_REMOVED
;
557 shmseg
->shm_perm
.key
= uap
->key
;
558 shmseg
->shm_perm
.seq
= (shmseg
->shm_perm
.seq
+ 1) & 0x7fff;
559 shm_handle
= (struct shm_handle
*)
560 _MALLOC(sizeof(struct shm_handle
), M_SHM
, M_WAITOK
);
561 shm_handle
->shm_object
= mem_object
;
562 shmid
= IXSEQ_TO_IPCID(segnum
, shmseg
->shm_perm
);
564 shmseg
->shm_internal
= shm_handle
;
565 shmseg
->shm_perm
.cuid
= shmseg
->shm_perm
.uid
= cred
->cr_uid
;
566 shmseg
->shm_perm
.cgid
= shmseg
->shm_perm
.gid
= cred
->cr_gid
;
567 shmseg
->shm_perm
.mode
= (shmseg
->shm_perm
.mode
& SHMSEG_WANTED
) |
568 (mode
& ACCESSPERMS
) | SHMSEG_ALLOCATED
;
569 shmseg
->shm_segsz
= uap
->size
;
570 shmseg
->shm_cpid
= p
->p_pid
;
571 shmseg
->shm_lpid
= shmseg
->shm_nattch
= 0;
572 shmseg
->shm_atime
= shmseg
->shm_dtime
= 0;
573 shmseg
->shm_ctime
= time_second
;
574 shm_committed
+= btoc(size
);
576 if (shmseg
->shm_perm
.mode
& SHMSEG_WANTED
) {
578 * Somebody else wanted this key while we were asleep. Wake
581 shmseg
->shm_perm
.mode
&= ~SHMSEG_WANTED
;
582 wakeup((caddr_t
)shmseg
);
588 case KERN_INVALID_ADDRESS
:
591 case KERN_PROTECTION_FAILURE
:
600 shmget(p
, uap
, retval
)
602 struct shmget_args
*uap
;
605 int segnum
, mode
, error
;
610 mode
= uap
->shmflg
& ACCESSPERMS
;
611 if (uap
->key
!= IPC_PRIVATE
) {
613 segnum
= shm_find_segment_by_key(uap
->key
);
615 error
= shmget_existing(p
, uap
, mode
, segnum
, retval
);
620 if ((uap
->shmflg
& IPC_CREAT
) == 0)
623 return( shmget_allocate_segment(p
, uap
, mode
, retval
));;
635 shmsys(p
, uap
, retval
)
637 /* XXX actually varargs. */
638 struct shmsys_args
*uap
;
645 if (uap
->which
>= sizeof(shmcalls
)/sizeof(shmcalls
[0]))
647 return ((*shmcalls
[uap
->which
])(p
, &uap
->a2
, retval
));
652 struct proc
*p1
, *p2
;
654 struct shmmap_state
*shmmap_s
;
660 size
= shminfo
.shmseg
* sizeof(struct shmmap_state
);
661 shmmap_s
= (struct shmmap_state
*)_MALLOC(size
, M_SHM
, M_WAITOK
);
662 bcopy((caddr_t
)p1
->vm_shm
, (caddr_t
)shmmap_s
, size
);
663 p2
->vm_shm
= (caddr_t
)shmmap_s
;
664 for (i
= 0; i
< shminfo
.shmseg
; i
++, shmmap_s
++)
665 if (shmmap_s
->shmid
!= -1)
666 shmsegs
[IPCID_TO_IX(shmmap_s
->shmid
)].shm_nattch
++;
673 struct shmmap_state
*shmmap_s
;
676 shmmap_s
= (struct shmmap_state
*)p
->vm_shm
;
677 for (i
= 0; i
< shminfo
.shmseg
; i
++, shmmap_s
++)
678 if (shmmap_s
->shmid
!= -1)
679 shm_delete_mapping(p
, shmmap_s
);
680 FREE((caddr_t
)p
->vm_shm
, M_SHM
);
692 s
= sizeof(struct shmid_ds
) * shminfo
.shmmni
;
694 MALLOC(shmsegs
, struct shmid_ds
*, s
,
696 for (i
= 0; i
< shminfo
.shmmni
; i
++) {
697 shmsegs
[i
].shm_perm
.mode
= SHMSEG_FREE
;
698 shmsegs
[i
].shm_perm
.seq
= 0;
707 /* (struct sysctl_oid *oidp, void *arg1, int arg2, \
708 struct sysctl_req *req) */
710 sysctl_shminfo SYSCTL_HANDLER_ARGS
714 error
= SYSCTL_OUT(req
, arg1
, sizeof(int));
715 if (error
|| !req
->newptr
)
718 /* Set the values only if shared memory is not initialised */
720 if (error
= SYSCTL_IN(req
, arg1
, sizeof(int)))
722 if (arg1
== &shminfo
.shmmax
) {
723 if (shminfo
.shmmax
& PAGE_MASK
) {
729 /* Initialize only when all values are set */
730 if ((shminfo
.shmmax
!= -1) &&
731 (shminfo
.shmmin
!= -1) &&
732 (shminfo
.shmmni
!= -1) &&
733 (shminfo
.shmseg
!= -1) &&
734 (shminfo
.shmall
!= -1)) {
741 SYSCTL_NODE(_kern
, KERN_SYSV
, sysv
, CTLFLAG_RW
, 0, "SYSV");
743 SYSCTL_PROC(_kern_sysv
, KSYSV_SHMMAX
, shmmax
, CTLTYPE_INT
| CTLFLAG_RW
,
744 &shminfo
.shmmax
, 0, &sysctl_shminfo
,"I","shmmax");
746 SYSCTL_PROC(_kern_sysv
, KSYSV_SHMMIN
, shmmin
, CTLTYPE_INT
| CTLFLAG_RW
,
747 &shminfo
.shmmin
, 0, &sysctl_shminfo
,"I","shmmin");
749 SYSCTL_PROC(_kern_sysv
, KSYSV_SHMMNI
, shmmni
, CTLTYPE_INT
| CTLFLAG_RW
,
750 &shminfo
.shmmni
, 0, &sysctl_shminfo
,"I","shmmni");
752 SYSCTL_PROC(_kern_sysv
, KSYSV_SHMSEG
, shmseg
, CTLTYPE_INT
| CTLFLAG_RW
,
753 &shminfo
.shmseg
, 0, &sysctl_shminfo
,"I","shmseg");
755 SYSCTL_PROC(_kern_sysv
, KSYSV_SHMALL
, shmall
, CTLTYPE_INT
| CTLFLAG_RW
,
756 &shminfo
.shmall
, 0, &sysctl_shminfo
,"I","shmall");