2 * Copyright (c) 2000-2002 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@
26 * Copyright (c) 1990, 1996-1998 Apple Computer, Inc.
27 * All Rights Reserved.
30 * posix_shm.c : Support for POSIX shared memory APIs
33 * Author: Ananthakrishna Ramesh
41 #include <sys/cdefs.h>
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/filedesc.h>
50 #include <sys/mount.h>
51 #include <sys/namei.h>
52 #include <sys/vnode.h>
53 #include <sys/ioctl.h>
55 #include <sys/malloc.h>
58 #include <mach/mach_types.h>
59 #include <mach/vm_prot.h>
60 #include <mach/vm_inherit.h>
61 #include <mach/kern_return.h>
62 #include <mach/memory_object_control.h>
65 #define PSHMNAMLEN 31 /* maximum name segment length we bother with */
68 unsigned int pshm_flags
;
69 unsigned int pshm_usecount
;
74 char pshm_name
[PSHMNAMLEN
+ 1]; /* segment name */
75 void * pshm_memobject
;
77 unsigned int pshm_readcount
;
78 unsigned int pshm_writecount
;
79 struct proc
* pshm_proc
;
80 #endif /* DIAGNOSTIC */
82 #define PSHMINFO_NULL (struct pshminfo *)0
85 #define PSHM_DEFINED 2
86 #define PSHM_ALLOCATED 4
88 #define PSHM_INUSE 0x10
89 #define PSHM_REMOVED 0x20
90 #define PSHM_INCREATE 0x40
91 #define PSHM_INDELETE 0x80
94 LIST_ENTRY(pshmcache
) pshm_hash
; /* hash chain */
95 struct pshminfo
*pshminfo
; /* vnode the name refers to */
96 int pshm_nlen
; /* length of name */
97 char pshm_name
[PSHMNAMLEN
+ 1]; /* segment name */
99 #define PSHMCACHE_NULL (struct pshmcache *)0
102 long goodhits
; /* hits that we can really use */
103 long neghits
; /* negative hits that we can use */
104 long badhits
; /* hits we must drop */
105 long falsehits
; /* hits with id mismatch */
106 long miss
; /* misses */
107 long longnames
; /* long names that ignore cache */
111 char *pshm_nameptr
; /* pointer to looked up name */
112 long pshm_namelen
; /* length of looked up component */
113 u_long pshm_hash
; /* hash value of looked up name */
119 struct pshminfo
*pinfo
;
120 unsigned int pshm_usecount
;
122 unsigned int readcnt
;
123 unsigned int writecnt
;
126 #define PSHMNODE_NULL (struct pshmnode *)0
129 #define PSHMHASH(pnp) \
130 (&pshmhashtbl[(pnp)->pshm_hash & pshmhash])
131 LIST_HEAD(pshmhashhead
, pshmcache
) *pshmhashtbl
; /* Hash Table */
132 u_long pshmhash
; /* size of hash table - 1 */
133 long pshmnument
; /* number of cache entries allocated */
134 struct pshmstats pshmstats
; /* cache effectiveness statistics */
136 static int pshm_read
__P((struct file
*fp
, struct uio
*uio
,
137 struct ucred
*cred
, int flags
, struct proc
*p
));
138 static int pshm_write
__P((struct file
*fp
, struct uio
*uio
,
139 struct ucred
*cred
, int flags
, struct proc
*p
));
140 static int pshm_ioctl
__P((struct file
*fp
, u_long com
,
141 caddr_t data
, struct proc
*p
));
142 static int pshm_select
__P((struct file
*fp
, int which
, void *wql
,
144 static int pshm_closefile
__P((struct file
*fp
, struct proc
*p
));
146 struct fileops pshmops
=
147 { pshm_read
, pshm_write
, pshm_ioctl
, pshm_select
, pshm_closefile
};
150 * Lookup an entry in the cache
153 * status of -1 is returned if matches
154 * If the lookup determines that the name does not exist
155 * (negative cacheing), a status of ENOENT is returned. If the lookup
156 * fails, a status of zero is returned.
160 pshm_cache_search(pshmp
, pnp
, pcache
)
161 struct pshminfo
**pshmp
;
162 struct pshmname
*pnp
;
163 struct pshmcache
**pcache
;
165 register struct pshmcache
*pcp
, *nnp
;
166 register struct pshmhashhead
*pcpp
;
168 if (pnp
->pshm_namelen
> PSHMNAMLEN
) {
169 pshmstats
.longnames
++;
173 pcpp
= PSHMHASH(pnp
);
174 for (pcp
= pcpp
->lh_first
; pcp
!= 0; pcp
= nnp
) {
175 nnp
= pcp
->pshm_hash
.le_next
;
176 if (pcp
->pshm_nlen
== pnp
->pshm_namelen
&&
177 !bcmp(pcp
->pshm_name
, pnp
->pshm_nameptr
, (u_int
)pcp
-> pshm_nlen
))
186 /* We found a "positive" match, return the vnode */
188 pshmstats
.goodhits
++;
190 *pshmp
= pcp
->pshminfo
;
196 * We found a "negative" match, ENOENT notifies client of this match.
197 * The nc_vpid field records whether this is a whiteout.
204 * Add an entry to the cache.
207 pshm_cache_add(pshmp
, pnp
)
208 struct pshminfo
*pshmp
;
209 struct pshmname
*pnp
;
211 register struct pshmcache
*pcp
;
212 register struct pshmhashhead
*pcpp
;
213 register struct pshminfo
*dpinfo
;
214 register struct pshmcache
*dpcp
;
217 if (pnp
->pshm_namelen
> NCHNAMLEN
)
218 panic("cache_enter: name too long");
222 * We allocate a new entry if we are less than the maximum
223 * allowed and the one at the front of the LRU list is in use.
224 * Otherwise we use the one at the front of the LRU list.
226 pcp
= (struct pshmcache
*)_MALLOC(sizeof(struct pshmcache
), M_SHM
, M_WAITOK
);
227 /* if the entry has already been added by some one else return */
228 if (pshm_cache_search(&dpinfo
, pnp
, &dpcp
) == -1) {
234 bzero(pcp
, sizeof(struct pshmcache
));
236 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
237 * For negative entries, we have to record whether it is a whiteout.
238 * the whiteout flag is stored in the nc_vpid field which is
241 pcp
->pshminfo
= pshmp
;
242 pcp
->pshm_nlen
= pnp
->pshm_namelen
;
243 bcopy(pnp
->pshm_nameptr
, pcp
->pshm_name
, (unsigned)pcp
->pshm_nlen
);
244 pcpp
= PSHMHASH(pnp
);
247 register struct pshmcache
*p
;
249 for (p
= pcpp
->lh_first
; p
!= 0; p
= p
->pshm_hash
.le_next
)
251 panic("cache_enter: duplicate");
254 LIST_INSERT_HEAD(pcpp
, pcp
, pshm_hash
);
259 * Name cache initialization, from vfs_init() when we are booting
264 pshmhashtbl
= hashinit(desiredvnodes
, M_SHM
, &pshmhash
);
268 * Invalidate a all entries to particular vnode.
270 * We actually just increment the v_id, that will do it. The entries will
271 * be purged by lookup as they get found. If the v_id wraps around, we
272 * need to ditch the entire cache, to avoid confusion. No valid vnode will
273 * ever have (v_id == 0).
276 pshm_cache_purge(void)
278 struct pshmcache
*pcp
;
279 struct pshmhashhead
*pcpp
;
281 for (pcpp
= &pshmhashtbl
[pshmhash
]; pcpp
>= pshmhashtbl
; pcpp
--) {
282 while (pcp
= pcpp
->lh_first
)
283 pshm_cache_delete(pcp
);
287 pshm_cache_delete(pcp
)
288 struct pshmcache
*pcp
;
291 if (pcp
->pshm_hash
.le_prev
== 0)
292 panic("namecache purge le_prev");
293 if (pcp
->pshm_hash
.le_next
== pcp
)
294 panic("namecache purge le_next");
295 #endif /* DIAGNOSTIC */
296 LIST_REMOVE(pcp
, pshm_hash
);
297 pcp
->pshm_hash
.le_prev
= 0;
302 struct shm_open_args
{
309 shm_open(p
, uap
, retval
)
311 register struct shm_open_args
*uap
;
314 register struct filedesc
*fdp
= p
->p_fd
;
315 register struct file
*fp
;
316 register struct vnode
*vp
;
319 int type
, indx
, error
;
321 struct pshminfo
*pinfo
;
322 extern struct fileops pshmops
;
326 size_t pathlen
, plen
;
328 int cmode
= uap
->mode
;
330 struct pshmnode
* pnode
= PSHMNODE_NULL
;
331 struct pshmcache
* pcache
= PSHMCACHE_NULL
;
335 pinfo
= PSHMINFO_NULL
;
337 MALLOC_ZONE(pnbuf
, caddr_t
,
338 MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
339 pathlen
= MAXPATHLEN
;
340 error
= copyinstr(uap
->name
, pnbuf
,
341 MAXPATHLEN
, &pathlen
);
345 if (pathlen
> PSHMNAMLEN
) {
346 error
= ENAMETOOLONG
;
351 #ifdef PSXSHM_NAME_RESTRICT
353 if (*nameptr
== '/') {
354 while (*(nameptr
++) == '/') {
363 #endif /* PSXSHM_NAME_RESTRICT */
367 nd
.pshm_nameptr
= nameptr
;
368 nd
.pshm_namelen
= plen
;
371 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
372 nd
.pshm_hash
+= (unsigned char)*cp
* i
;
375 error
= pshm_cache_search(&pinfo
, &nd
, &pcache
);
377 if (error
== ENOENT
) {
386 fmode
= FFLAGS(uap
->oflag
);
387 if ((fmode
& (FREAD
| FWRITE
))==0)
390 if (error
= falloc(p
, &nfp
, &indx
))
396 if (fmode
& O_CREAT
) {
397 if ((fmode
& O_EXCL
) && incache
) {
398 /* shm obj exists and opened O_EXCL */
400 if (pinfo
->pshm_flags
& PSHM_INDELETE
) {
407 /* create a new one */
408 pinfo
= (struct pshminfo
*)_MALLOC(sizeof(struct pshminfo
), M_SHM
, M_WAITOK
);
409 bzero(pinfo
, sizeof(struct pshminfo
));
411 pinfo
->pshm_flags
= PSHM_DEFINED
| PSHM_INCREATE
;
412 pinfo
->pshm_usecount
= 1;
413 pinfo
->pshm_mode
= cmode
;
414 pinfo
->pshm_uid
= p
->p_ucred
->cr_uid
;
415 pinfo
->pshm_gid
= p
->p_ucred
->cr_gid
;
418 if( pinfo
->pshm_flags
& PSHM_INDELETE
) {
422 if (error
= pshm_access(pinfo
, fmode
, p
->p_ucred
, p
))
427 /* O_CREAT is not set and the shm obecj does not exist */
431 if( pinfo
->pshm_flags
& PSHM_INDELETE
) {
435 if (error
= pshm_access(pinfo
, fmode
, p
->p_ucred
, p
))
438 if (fmode
& O_TRUNC
) {
444 pinfo
->pshm_writecount
++;
446 pinfo
->pshm_readcount
++;
448 pnode
= (struct pshmnode
*)_MALLOC(sizeof(struct pshmnode
), M_SHM
, M_WAITOK
);
449 bzero(pnode
, sizeof(struct pshmnode
));
452 if (error
= pshm_cache_add(pinfo
, &nd
)) {
456 pinfo
->pshm_flags
&= ~PSHM_INCREATE
;
457 pinfo
->pshm_usecount
++;
458 pnode
->pinfo
= pinfo
;
459 fp
->f_flag
= fmode
& FMASK
;
460 fp
->f_type
= DTYPE_PSXSHM
;
461 fp
->f_ops
= &pshmops
;
462 fp
->f_data
= (caddr_t
)pnode
;
463 *fdflags(p
, indx
) &= ~UF_RESERVED
;
465 _FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
477 _FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
484 pshm_truncate(p
, fp
, fd
, length
, retval
)
491 struct pshminfo
* pinfo
;
492 struct pshmnode
* pnode
;
494 vm_offset_t user_addr
;
498 if (fp
->f_type
!= DTYPE_PSXSHM
) {
503 if (((pnode
= (struct pshmnode
*)fp
->f_data
)) == PSHMNODE_NULL
)
506 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
)
508 if ((pinfo
->pshm_flags
& (PSHM_DEFINED
| PSHM_ALLOCATED
))
513 size
= round_page (length
);
514 kret
= vm_allocate(current_map(), &user_addr
, size
, TRUE
);
515 if (kret
!= KERN_SUCCESS
)
518 kret
= mach_make_memory_entry (current_map(), &size
,
519 user_addr
, VM_PROT_DEFAULT
, &mem_object
, 0);
521 if (kret
!= KERN_SUCCESS
)
524 vm_deallocate(current_map(), user_addr
, size
);
526 pinfo
->pshm_flags
&= ~PSHM_DEFINED
;
527 pinfo
->pshm_flags
= PSHM_ALLOCATED
;
528 pinfo
->pshm_memobject
= mem_object
;
529 pinfo
->pshm_length
= size
;
534 case KERN_INVALID_ADDRESS
:
537 case KERN_PROTECTION_FAILURE
:
547 struct pshmnode
*pnode
;
550 struct pshminfo
*pinfo
;
552 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
)
555 bzero(sb
, sizeof(struct stat
));
556 sb
->st_mode
= pinfo
->pshm_mode
;
557 sb
->st_uid
= pinfo
->pshm_uid
;
558 sb
->st_gid
= pinfo
->pshm_gid
;
559 sb
->st_size
= pinfo
->pshm_length
;
565 pshm_access(struct pshminfo
*pinfo
, int mode
, struct ucred
*cred
, struct proc
*p
)
571 /* Otherwise, user id 0 always gets access. */
572 if (cred
->cr_uid
== 0)
577 /* Otherwise, check the owner. */
578 if (cred
->cr_uid
== pinfo
->pshm_uid
) {
583 return ((pinfo
->pshm_mode
& mask
) == mask
? 0 : EACCES
);
586 /* Otherwise, check the groups. */
587 for (i
= 0, gp
= cred
->cr_groups
; i
< cred
->cr_ngroups
; i
++, gp
++)
588 if (pinfo
->pshm_gid
== *gp
) {
593 return ((pinfo
->pshm_mode
& mask
) == mask
? 0 : EACCES
);
596 /* Otherwise, check everyone else. */
601 return ((pinfo
->pshm_mode
& mask
) == mask
? 0 : EACCES
);
610 #ifdef DOUBLE_ALIGN_PARAMS
617 pshm_mmap(struct proc
*p
, struct mmap_args
*uap
, register_t
*retval
, struct file
*fp
, vm_size_t pageoff
)
619 vm_offset_t user_addr
= uap
->addr
;
620 vm_size_t user_size
= uap
->len
;
621 int prot
= uap
->prot
;
622 int flags
= uap
->flags
;
623 vm_object_offset_t file_pos
= (vm_object_offset_t
)uap
->pos
;
626 boolean_t find_space
,docow
;
628 struct pshminfo
* pinfo
;
629 struct pshmnode
* pnode
;
635 if ((flags
& MAP_SHARED
) == 0)
639 if ((prot
& PROT_WRITE
) && ((fp
->f_flag
& FWRITE
) == 0)) {
643 if (((pnode
= (struct pshmnode
*)fp
->f_data
)) == PSHMNODE_NULL
)
646 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
)
649 if ((pinfo
->pshm_flags
& PSHM_ALLOCATED
) != PSHM_ALLOCATED
) {
652 if (user_size
> pinfo
->pshm_length
) {
655 if ((off_t
)user_size
+ file_pos
> pinfo
->pshm_length
) {
658 if ((mem_object
= pinfo
->pshm_memobject
) == NULL
) {
663 user_map
= current_map();
665 if ((flags
& MAP_FIXED
) == 0) {
667 user_addr
= round_page(user_addr
);
669 if (user_addr
!= trunc_page(user_addr
))
672 (void) vm_deallocate(user_map
, user_addr
, user_size
);
676 kret
= vm_map_64(user_map
, &user_addr
, user_size
,
677 0, find_space
, pinfo
->pshm_memobject
, file_pos
, docow
,
678 prot
, VM_PROT_DEFAULT
,
681 if (kret
!= KERN_SUCCESS
)
683 kret
= vm_inherit(user_map
, user_addr
, user_size
,
685 if (kret
!= KERN_SUCCESS
) {
686 (void) vm_deallocate(user_map
, user_addr
, user_size
);
689 pnode
->mapp_addr
= user_addr
;
690 pnode
->map_size
= user_size
;
691 pinfo
->pshm_flags
|= (PSHM_MAPPED
| PSHM_INUSE
);
695 *fdflags(p
, fd
) |= UF_MAPPED
;
696 *retval
= (register_t
)(user_addr
+ pageoff
);
698 case KERN_INVALID_ADDRESS
:
701 case KERN_PROTECTION_FAILURE
:
709 struct shm_unlink_args
{
714 shm_unlink(p
, uap
, retval
)
716 register struct shm_unlink_args
*uap
;
719 register struct filedesc
*fdp
= p
->p_fd
;
720 register struct file
*fp
;
724 struct pshminfo
*pinfo
;
725 extern struct fileops pshmops
;
729 size_t pathlen
, plen
;
732 struct pshmnode
* pnode
= PSHMNODE_NULL
;
733 struct pshmcache
*pcache
= PSHMCACHE_NULL
;
736 pinfo
= PSHMINFO_NULL
;
738 MALLOC_ZONE(pnbuf
, caddr_t
,
739 MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
740 pathlen
= MAXPATHLEN
;
741 error
= copyinstr(uap
->name
, pnbuf
,
742 MAXPATHLEN
, &pathlen
);
746 if (pathlen
> PSHMNAMLEN
) {
747 error
= ENAMETOOLONG
;
752 #ifdef PSXSHM_NAME_RESTRICT
754 if (*nameptr
== '/') {
755 while (*(nameptr
++) == '/') {
764 #endif /* PSXSHM_NAME_RESTRICT */
768 nd
.pshm_nameptr
= nameptr
;
769 nd
.pshm_namelen
= plen
;
772 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
773 nd
.pshm_hash
+= (unsigned char)*cp
* i
;
776 error
= pshm_cache_search(&pinfo
, &nd
, &pcache
);
778 if (error
== ENOENT
) {
789 if ((pinfo
->pshm_flags
& (PSHM_DEFINED
| PSHM_ALLOCATED
))==0) {
793 if (pinfo
->pshm_flags
& PSHM_INDELETE
) {
798 if (pinfo
->pshm_memobject
== NULL
) {
803 pinfo
->pshm_flags
|= PSHM_INDELETE
;
804 pinfo
->pshm_usecount
--;
805 kret
= mach_destroy_memory_entry(pinfo
->pshm_memobject
);
806 pshm_cache_delete(pcache
);
807 _FREE(pcache
, M_SHM
);
808 pinfo
->pshm_flags
|= PSHM_REMOVED
;
811 _FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
815 case KERN_INVALID_ADDRESS
:
816 case KERN_PROTECTION_FAILURE
:
824 pshm_close(pnode
, flags
, cred
, p
)
825 register struct pshmnode
*pnode
;
832 register struct pshminfo
*pinfo
;
834 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
)
837 if ((pinfo
->pshm_flags
& PSHM_ALLOCATED
) != PSHM_ALLOCATED
) {
841 if(!pinfo
->pshm_usecount
) {
842 kprintf("negative usecount in pshm_close\n");
844 #endif /* DIAGNOSTIC */
845 pinfo
->pshm_usecount
--;
847 if ((pinfo
->pshm_flags
& PSHM_REMOVED
) && !pinfo
->pshm_usecount
) {
855 pshm_closefile(fp
, p
)
859 return (pshm_close(((struct pshmnode
*)fp
->f_data
), fp
->f_flag
,
864 pshm_read(fp
, uio
, cred
, flags
, p
)
875 pshm_write(fp
, uio
, cred
, flags
, p
)
886 pshm_ioctl(fp
, com
, data
, p
)
896 pshm_select(fp
, which
, wql
, p
)