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 static int pshm_kqfilter
__P((struct file
*fp
, struct knote
*kn
, struct proc
*p
));
148 struct fileops pshmops
=
149 { pshm_read
, pshm_write
, pshm_ioctl
, pshm_select
, pshm_closefile
, pshm_kqfilter
};
152 * Lookup an entry in the cache
155 * status of -1 is returned if matches
156 * If the lookup determines that the name does not exist
157 * (negative cacheing), a status of ENOENT is returned. If the lookup
158 * fails, a status of zero is returned.
162 pshm_cache_search(pshmp
, pnp
, pcache
)
163 struct pshminfo
**pshmp
;
164 struct pshmname
*pnp
;
165 struct pshmcache
**pcache
;
167 register struct pshmcache
*pcp
, *nnp
;
168 register struct pshmhashhead
*pcpp
;
170 if (pnp
->pshm_namelen
> PSHMNAMLEN
) {
171 pshmstats
.longnames
++;
175 pcpp
= PSHMHASH(pnp
);
176 for (pcp
= pcpp
->lh_first
; pcp
!= 0; pcp
= nnp
) {
177 nnp
= pcp
->pshm_hash
.le_next
;
178 if (pcp
->pshm_nlen
== pnp
->pshm_namelen
&&
179 !bcmp(pcp
->pshm_name
, pnp
->pshm_nameptr
, (u_int
)pcp
-> pshm_nlen
))
188 /* We found a "positive" match, return the vnode */
190 pshmstats
.goodhits
++;
192 *pshmp
= pcp
->pshminfo
;
198 * We found a "negative" match, ENOENT notifies client of this match.
199 * The nc_vpid field records whether this is a whiteout.
206 * Add an entry to the cache.
209 pshm_cache_add(pshmp
, pnp
)
210 struct pshminfo
*pshmp
;
211 struct pshmname
*pnp
;
213 register struct pshmcache
*pcp
;
214 register struct pshmhashhead
*pcpp
;
215 struct pshminfo
*dpinfo
;
216 struct pshmcache
*dpcp
;
219 if (pnp
->pshm_namelen
> NCHNAMLEN
)
220 panic("cache_enter: name too long");
224 * We allocate a new entry if we are less than the maximum
225 * allowed and the one at the front of the LRU list is in use.
226 * Otherwise we use the one at the front of the LRU list.
228 pcp
= (struct pshmcache
*)_MALLOC(sizeof(struct pshmcache
), M_SHM
, M_WAITOK
);
229 /* if the entry has already been added by some one else return */
230 if (pshm_cache_search(&dpinfo
, pnp
, &dpcp
) == -1) {
236 bzero(pcp
, sizeof(struct pshmcache
));
238 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
239 * For negative entries, we have to record whether it is a whiteout.
240 * the whiteout flag is stored in the nc_vpid field which is
243 pcp
->pshminfo
= pshmp
;
244 pcp
->pshm_nlen
= pnp
->pshm_namelen
;
245 bcopy(pnp
->pshm_nameptr
, pcp
->pshm_name
, (unsigned)pcp
->pshm_nlen
);
246 pcpp
= PSHMHASH(pnp
);
249 register struct pshmcache
*p
;
251 for (p
= pcpp
->lh_first
; p
!= 0; p
= p
->pshm_hash
.le_next
)
253 panic("cache_enter: duplicate");
256 LIST_INSERT_HEAD(pcpp
, pcp
, pshm_hash
);
261 * Name cache initialization, from vfs_init() when we are booting
266 pshmhashtbl
= hashinit(desiredvnodes
, M_SHM
, &pshmhash
);
270 * Invalidate a all entries to particular vnode.
272 * We actually just increment the v_id, that will do it. The entries will
273 * be purged by lookup as they get found. If the v_id wraps around, we
274 * need to ditch the entire cache, to avoid confusion. No valid vnode will
275 * ever have (v_id == 0).
278 pshm_cache_purge(void)
280 struct pshmcache
*pcp
;
281 struct pshmhashhead
*pcpp
;
283 for (pcpp
= &pshmhashtbl
[pshmhash
]; pcpp
>= pshmhashtbl
; pcpp
--) {
284 while (pcp
= pcpp
->lh_first
)
285 pshm_cache_delete(pcp
);
289 pshm_cache_delete(pcp
)
290 struct pshmcache
*pcp
;
293 if (pcp
->pshm_hash
.le_prev
== 0)
294 panic("namecache purge le_prev");
295 if (pcp
->pshm_hash
.le_next
== pcp
)
296 panic("namecache purge le_next");
297 #endif /* DIAGNOSTIC */
298 LIST_REMOVE(pcp
, pshm_hash
);
299 pcp
->pshm_hash
.le_prev
= 0;
304 struct shm_open_args
{
311 shm_open(p
, uap
, retval
)
313 register struct shm_open_args
*uap
;
316 register struct filedesc
*fdp
= p
->p_fd
;
317 register struct file
*fp
;
318 register struct vnode
*vp
;
321 int type
, indx
, error
;
323 struct pshminfo
*pinfo
;
324 extern struct fileops pshmops
;
328 size_t pathlen
, plen
;
330 int cmode
= uap
->mode
;
332 struct pshmnode
* pnode
= PSHMNODE_NULL
;
333 struct pshmcache
* pcache
= PSHMCACHE_NULL
;
337 pinfo
= PSHMINFO_NULL
;
339 MALLOC_ZONE(pnbuf
, caddr_t
,
340 MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
341 pathlen
= MAXPATHLEN
;
342 error
= copyinstr((void *)uap
->name
, (void *)pnbuf
,
343 MAXPATHLEN
, &pathlen
);
347 if (pathlen
> PSHMNAMLEN
) {
348 error
= ENAMETOOLONG
;
353 #ifdef PSXSHM_NAME_RESTRICT
355 if (*nameptr
== '/') {
356 while (*(nameptr
++) == '/') {
365 #endif /* PSXSHM_NAME_RESTRICT */
369 nd
.pshm_nameptr
= nameptr
;
370 nd
.pshm_namelen
= plen
;
373 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
374 nd
.pshm_hash
+= (unsigned char)*cp
* i
;
377 error
= pshm_cache_search(&pinfo
, &nd
, &pcache
);
379 if (error
== ENOENT
) {
388 fmode
= FFLAGS(uap
->oflag
);
389 if ((fmode
& (FREAD
| FWRITE
))==0) {
394 if (error
= falloc(p
, &nfp
, &indx
))
400 if (fmode
& O_CREAT
) {
401 if ((fmode
& O_EXCL
) && incache
) {
402 /* shm obj exists and opened O_EXCL */
404 if (pinfo
->pshm_flags
& PSHM_INDELETE
) {
411 /* create a new one */
412 pinfo
= (struct pshminfo
*)_MALLOC(sizeof(struct pshminfo
), M_SHM
, M_WAITOK
);
413 bzero(pinfo
, sizeof(struct pshminfo
));
415 pinfo
->pshm_flags
= PSHM_DEFINED
| PSHM_INCREATE
;
416 pinfo
->pshm_usecount
= 1;
417 pinfo
->pshm_mode
= cmode
;
418 pinfo
->pshm_uid
= p
->p_ucred
->cr_uid
;
419 pinfo
->pshm_gid
= p
->p_ucred
->cr_gid
;
422 if( pinfo
->pshm_flags
& PSHM_INDELETE
) {
426 if (error
= pshm_access(pinfo
, fmode
, p
->p_ucred
, p
))
431 /* O_CREAT is not set and the shm obecj does not exist */
435 if( pinfo
->pshm_flags
& PSHM_INDELETE
) {
439 if (error
= pshm_access(pinfo
, fmode
, p
->p_ucred
, p
))
442 if (fmode
& O_TRUNC
) {
448 pinfo
->pshm_writecount
++;
450 pinfo
->pshm_readcount
++;
452 pnode
= (struct pshmnode
*)_MALLOC(sizeof(struct pshmnode
), M_SHM
, M_WAITOK
);
453 bzero(pnode
, sizeof(struct pshmnode
));
456 if (error
= pshm_cache_add(pinfo
, &nd
)) {
460 pinfo
->pshm_flags
&= ~PSHM_INCREATE
;
461 pinfo
->pshm_usecount
++;
462 pnode
->pinfo
= pinfo
;
463 fp
->f_flag
= fmode
& FMASK
;
464 fp
->f_type
= DTYPE_PSXSHM
;
465 fp
->f_ops
= &pshmops
;
466 fp
->f_data
= (caddr_t
)pnode
;
467 *fdflags(p
, indx
) &= ~UF_RESERVED
;
469 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
481 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
488 pshm_truncate(p
, fp
, fd
, length
, retval
)
495 struct pshminfo
* pinfo
;
496 struct pshmnode
* pnode
;
498 vm_offset_t user_addr
;
502 if (fp
->f_type
!= DTYPE_PSXSHM
) {
507 if (((pnode
= (struct pshmnode
*)fp
->f_data
)) == PSHMNODE_NULL
)
510 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
)
512 if ((pinfo
->pshm_flags
& (PSHM_DEFINED
| PSHM_ALLOCATED
))
517 size
= round_page_64(length
);
518 kret
= vm_allocate(current_map(), &user_addr
, size
, TRUE
);
519 if (kret
!= KERN_SUCCESS
)
522 kret
= mach_make_memory_entry (current_map(), &size
,
523 user_addr
, VM_PROT_DEFAULT
, &mem_object
, 0);
525 if (kret
!= KERN_SUCCESS
)
528 vm_deallocate(current_map(), user_addr
, size
);
530 pinfo
->pshm_flags
&= ~PSHM_DEFINED
;
531 pinfo
->pshm_flags
= PSHM_ALLOCATED
;
532 pinfo
->pshm_memobject
= mem_object
;
533 pinfo
->pshm_length
= size
;
538 case KERN_INVALID_ADDRESS
:
541 case KERN_PROTECTION_FAILURE
:
551 struct pshmnode
*pnode
;
554 struct pshminfo
*pinfo
;
556 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
)
559 bzero(sb
, sizeof(struct stat
));
560 sb
->st_mode
= pinfo
->pshm_mode
;
561 sb
->st_uid
= pinfo
->pshm_uid
;
562 sb
->st_gid
= pinfo
->pshm_gid
;
563 sb
->st_size
= pinfo
->pshm_length
;
569 pshm_access(struct pshminfo
*pinfo
, int mode
, struct ucred
*cred
, struct proc
*p
)
575 /* Otherwise, user id 0 always gets access. */
576 if (cred
->cr_uid
== 0)
581 /* Otherwise, check the owner. */
582 if (cred
->cr_uid
== pinfo
->pshm_uid
) {
587 return ((pinfo
->pshm_mode
& mask
) == mask
? 0 : EACCES
);
590 /* Otherwise, check the groups. */
591 for (i
= 0, gp
= cred
->cr_groups
; i
< cred
->cr_ngroups
; i
++, gp
++)
592 if (pinfo
->pshm_gid
== *gp
) {
597 return ((pinfo
->pshm_mode
& mask
) == mask
? 0 : EACCES
);
600 /* Otherwise, check everyone else. */
605 return ((pinfo
->pshm_mode
& mask
) == mask
? 0 : EACCES
);
614 #ifdef DOUBLE_ALIGN_PARAMS
621 pshm_mmap(struct proc
*p
, struct mmap_args
*uap
, register_t
*retval
, struct file
*fp
, vm_size_t pageoff
)
623 vm_offset_t user_addr
= (vm_offset_t
)uap
->addr
;
624 vm_size_t user_size
= (vm_size_t
)uap
->len
;
625 int prot
= uap
->prot
;
626 int flags
= uap
->flags
;
627 vm_object_offset_t file_pos
= (vm_object_offset_t
)uap
->pos
;
630 boolean_t find_space
,docow
;
632 struct pshminfo
* pinfo
;
633 struct pshmnode
* pnode
;
639 if ((flags
& MAP_SHARED
) == 0)
643 if ((prot
& PROT_WRITE
) && ((fp
->f_flag
& FWRITE
) == 0)) {
647 if (((pnode
= (struct pshmnode
*)fp
->f_data
)) == PSHMNODE_NULL
)
650 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
)
653 if ((pinfo
->pshm_flags
& PSHM_ALLOCATED
) != PSHM_ALLOCATED
) {
656 if (user_size
> pinfo
->pshm_length
) {
659 if ((off_t
)user_size
+ file_pos
> pinfo
->pshm_length
) {
662 if ((mem_object
= pinfo
->pshm_memobject
) == NULL
) {
667 user_map
= current_map();
669 if ((flags
& MAP_FIXED
) == 0) {
671 user_addr
= round_page_32(user_addr
);
673 if (user_addr
!= trunc_page_32(user_addr
))
676 (void) vm_deallocate(user_map
, user_addr
, user_size
);
680 kret
= vm_map_64(user_map
, &user_addr
, user_size
,
681 0, find_space
, pinfo
->pshm_memobject
, file_pos
, docow
,
682 prot
, VM_PROT_DEFAULT
,
685 if (kret
!= KERN_SUCCESS
)
687 kret
= vm_inherit(user_map
, user_addr
, user_size
,
689 if (kret
!= KERN_SUCCESS
) {
690 (void) vm_deallocate(user_map
, user_addr
, user_size
);
693 pnode
->mapp_addr
= user_addr
;
694 pnode
->map_size
= user_size
;
695 pinfo
->pshm_flags
|= (PSHM_MAPPED
| PSHM_INUSE
);
699 *fdflags(p
, fd
) |= UF_MAPPED
;
700 *retval
= (register_t
)(user_addr
+ pageoff
);
702 case KERN_INVALID_ADDRESS
:
705 case KERN_PROTECTION_FAILURE
:
713 struct shm_unlink_args
{
718 shm_unlink(p
, uap
, retval
)
720 register struct shm_unlink_args
*uap
;
723 register struct filedesc
*fdp
= p
->p_fd
;
724 register struct file
*fp
;
728 struct pshminfo
*pinfo
;
729 extern struct fileops pshmops
;
733 size_t pathlen
, plen
;
736 struct pshmnode
* pnode
= PSHMNODE_NULL
;
737 struct pshmcache
*pcache
= PSHMCACHE_NULL
;
740 pinfo
= PSHMINFO_NULL
;
742 MALLOC_ZONE(pnbuf
, caddr_t
,
743 MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
744 pathlen
= MAXPATHLEN
;
745 error
= copyinstr((void *)uap
->name
, (void *)pnbuf
,
746 MAXPATHLEN
, &pathlen
);
750 if (pathlen
> PSHMNAMLEN
) {
751 error
= ENAMETOOLONG
;
756 #ifdef PSXSHM_NAME_RESTRICT
758 if (*nameptr
== '/') {
759 while (*(nameptr
++) == '/') {
768 #endif /* PSXSHM_NAME_RESTRICT */
772 nd
.pshm_nameptr
= nameptr
;
773 nd
.pshm_namelen
= plen
;
776 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
777 nd
.pshm_hash
+= (unsigned char)*cp
* i
;
780 error
= pshm_cache_search(&pinfo
, &nd
, &pcache
);
782 if (error
== ENOENT
) {
793 if ((pinfo
->pshm_flags
& (PSHM_DEFINED
| PSHM_ALLOCATED
))==0) {
797 if (pinfo
->pshm_flags
& PSHM_INDELETE
) {
802 if (pinfo
->pshm_memobject
== NULL
) {
807 pinfo
->pshm_flags
|= PSHM_INDELETE
;
808 pinfo
->pshm_usecount
--;
809 kret
= mach_destroy_memory_entry(pinfo
->pshm_memobject
);
810 pshm_cache_delete(pcache
);
811 _FREE(pcache
, M_SHM
);
812 pinfo
->pshm_flags
|= PSHM_REMOVED
;
815 FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
819 case KERN_INVALID_ADDRESS
:
820 case KERN_PROTECTION_FAILURE
:
828 pshm_close(pnode
, flags
, cred
, p
)
829 register struct pshmnode
*pnode
;
836 register struct pshminfo
*pinfo
;
838 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
)
841 if ((pinfo
->pshm_flags
& PSHM_ALLOCATED
) != PSHM_ALLOCATED
) {
845 if(!pinfo
->pshm_usecount
) {
846 kprintf("negative usecount in pshm_close\n");
848 #endif /* DIAGNOSTIC */
849 pinfo
->pshm_usecount
--;
851 if ((pinfo
->pshm_flags
& PSHM_REMOVED
) && !pinfo
->pshm_usecount
) {
859 pshm_closefile(fp
, p
)
863 return (pshm_close(((struct pshmnode
*)fp
->f_data
), fp
->f_flag
,
868 pshm_read(fp
, uio
, cred
, flags
, p
)
879 pshm_write(fp
, uio
, cred
, flags
, p
)
890 pshm_ioctl(fp
, com
, data
, p
)
900 pshm_select(fp
, which
, wql
, p
)
910 pshm_kqfilter(fp
, kn
, p
)