2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1990, 1996-1998 Apple Computer, Inc.
24 * All Rights Reserved.
27 * posix_shm.c : Support for POSIX shared memory APIs
30 * Author: Ananthakrishna Ramesh
38 #include <sys/cdefs.h>
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
43 #include <sys/filedesc.h>
47 #include <sys/mount.h>
48 #include <sys/namei.h>
49 #include <sys/vnode.h>
50 #include <sys/ioctl.h>
52 #include <sys/malloc.h>
55 #include <mach/mach_types.h>
56 #include <mach/vm_prot.h>
57 #include <mach/vm_inherit.h>
58 #include <mach/kern_return.h>
59 #include <mach/memory_object_control.h>
62 #define PSHMNAMLEN 31 /* maximum name segment length we bother with */
65 unsigned int pshm_flags
;
66 unsigned int pshm_usecount
;
71 char pshm_name
[PSHMNAMLEN
+ 1]; /* segment name */
72 void * pshm_memobject
;
74 unsigned int pshm_readcount
;
75 unsigned int pshm_writecount
;
76 struct proc
* pshm_proc
;
77 #endif /* DIAGNOSTIC */
79 #define PSHMINFO_NULL (struct pshminfo *)0
82 #define PSHM_DEFINED 2
83 #define PSHM_ALLOCATED 4
85 #define PSHM_INUSE 0x10
86 #define PSHM_REMOVED 0x20
87 #define PSHM_INCREATE 0x40
88 #define PSHM_INDELETE 0x80
91 LIST_ENTRY(pshmcache
) pshm_hash
; /* hash chain */
92 struct pshminfo
*pshminfo
; /* vnode the name refers to */
93 int pshm_nlen
; /* length of name */
94 char pshm_name
[PSHMNAMLEN
+ 1]; /* segment name */
96 #define PSHMCACHE_NULL (struct pshmcache *)0
99 long goodhits
; /* hits that we can really use */
100 long neghits
; /* negative hits that we can use */
101 long badhits
; /* hits we must drop */
102 long falsehits
; /* hits with id mismatch */
103 long miss
; /* misses */
104 long longnames
; /* long names that ignore cache */
108 char *pshm_nameptr
; /* pointer to looked up name */
109 long pshm_namelen
; /* length of looked up component */
110 u_long pshm_hash
; /* hash value of looked up name */
116 struct pshminfo
*pinfo
;
117 unsigned int pshm_usecount
;
119 unsigned int readcnt
;
120 unsigned int writecnt
;
123 #define PSHMNODE_NULL (struct pshmnode *)0
126 #define PSHMHASH(pnp) \
127 (&pshmhashtbl[(pnp)->pshm_hash & pshmhash])
128 LIST_HEAD(pshmhashhead
, pshmcache
) *pshmhashtbl
; /* Hash Table */
129 u_long pshmhash
; /* size of hash table - 1 */
130 long pshmnument
; /* number of cache entries allocated */
131 struct pshmstats pshmstats
; /* cache effectiveness statistics */
133 static int pshm_read
__P((struct file
*fp
, struct uio
*uio
,
134 struct ucred
*cred
, int flags
, struct proc
*p
));
135 static int pshm_write
__P((struct file
*fp
, struct uio
*uio
,
136 struct ucred
*cred
, int flags
, struct proc
*p
));
137 static int pshm_ioctl
__P((struct file
*fp
, u_long com
,
138 caddr_t data
, struct proc
*p
));
139 static int pshm_select
__P((struct file
*fp
, int which
, void *wql
,
141 static int pshm_closefile
__P((struct file
*fp
, struct proc
*p
));
143 struct fileops pshmops
=
144 { pshm_read
, pshm_write
, pshm_ioctl
, pshm_select
, pshm_closefile
};
147 * Lookup an entry in the cache
150 * status of -1 is returned if matches
151 * If the lookup determines that the name does not exist
152 * (negative cacheing), a status of ENOENT is returned. If the lookup
153 * fails, a status of zero is returned.
157 pshm_cache_search(pshmp
, pnp
, pcache
)
158 struct pshminfo
**pshmp
;
159 struct pshmname
*pnp
;
160 struct pshmcache
**pcache
;
162 register struct pshmcache
*pcp
, *nnp
;
163 register struct pshmhashhead
*pcpp
;
165 if (pnp
->pshm_namelen
> PSHMNAMLEN
) {
166 pshmstats
.longnames
++;
170 pcpp
= PSHMHASH(pnp
);
171 for (pcp
= pcpp
->lh_first
; pcp
!= 0; pcp
= nnp
) {
172 nnp
= pcp
->pshm_hash
.le_next
;
173 if (pcp
->pshm_nlen
== pnp
->pshm_namelen
&&
174 !bcmp(pcp
->pshm_name
, pnp
->pshm_nameptr
, (u_int
)pcp
-> pshm_nlen
))
183 /* We found a "positive" match, return the vnode */
185 pshmstats
.goodhits
++;
187 *pshmp
= pcp
->pshminfo
;
193 * We found a "negative" match, ENOENT notifies client of this match.
194 * The nc_vpid field records whether this is a whiteout.
201 * Add an entry to the cache.
204 pshm_cache_add(pshmp
, pnp
)
205 struct pshminfo
*pshmp
;
206 struct pshmname
*pnp
;
208 register struct pshmcache
*pcp
;
209 register struct pshmhashhead
*pcpp
;
210 register struct pshminfo
*dpinfo
;
211 register struct pshmcache
*dpcp
;
214 if (pnp
->pshm_namelen
> NCHNAMLEN
)
215 panic("cache_enter: name too long");
219 * We allocate a new entry if we are less than the maximum
220 * allowed and the one at the front of the LRU list is in use.
221 * Otherwise we use the one at the front of the LRU list.
223 pcp
= (struct pshmcache
*)_MALLOC(sizeof(struct pshmcache
), M_SHM
, M_WAITOK
);
224 /* if the entry has already been added by some one else return */
225 if (pshm_cache_search(&dpinfo
, pnp
, &dpcp
) == -1) {
231 bzero(pcp
, sizeof(struct pshmcache
));
233 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
234 * For negative entries, we have to record whether it is a whiteout.
235 * the whiteout flag is stored in the nc_vpid field which is
238 pcp
->pshminfo
= pshmp
;
239 pcp
->pshm_nlen
= pnp
->pshm_namelen
;
240 bcopy(pnp
->pshm_nameptr
, pcp
->pshm_name
, (unsigned)pcp
->pshm_nlen
);
241 pcpp
= PSHMHASH(pnp
);
244 register struct pshmcache
*p
;
246 for (p
= pcpp
->lh_first
; p
!= 0; p
= p
->pshm_hash
.le_next
)
248 panic("cache_enter: duplicate");
251 LIST_INSERT_HEAD(pcpp
, pcp
, pshm_hash
);
256 * Name cache initialization, from vfs_init() when we are booting
261 pshmhashtbl
= hashinit(desiredvnodes
, M_SHM
, &pshmhash
);
265 * Invalidate a all entries to particular vnode.
267 * We actually just increment the v_id, that will do it. The entries will
268 * be purged by lookup as they get found. If the v_id wraps around, we
269 * need to ditch the entire cache, to avoid confusion. No valid vnode will
270 * ever have (v_id == 0).
273 pshm_cache_purge(void)
275 struct pshmcache
*pcp
;
276 struct pshmhashhead
*pcpp
;
278 for (pcpp
= &pshmhashtbl
[pshmhash
]; pcpp
>= pshmhashtbl
; pcpp
--) {
279 while (pcp
= pcpp
->lh_first
)
280 pshm_cache_delete(pcp
);
284 pshm_cache_delete(pcp
)
285 struct pshmcache
*pcp
;
288 if (pcp
->pshm_hash
.le_prev
== 0)
289 panic("namecache purge le_prev");
290 if (pcp
->pshm_hash
.le_next
== pcp
)
291 panic("namecache purge le_next");
292 #endif /* DIAGNOSTIC */
293 LIST_REMOVE(pcp
, pshm_hash
);
294 pcp
->pshm_hash
.le_prev
= 0;
299 struct shm_open_args
{
306 shm_open(p
, uap
, retval
)
308 register struct shm_open_args
*uap
;
311 register struct filedesc
*fdp
= p
->p_fd
;
312 register struct file
*fp
;
313 register struct vnode
*vp
;
316 int type
, indx
, error
;
318 struct pshminfo
*pinfo
;
319 extern struct fileops pshmops
;
323 size_t pathlen
, plen
;
325 int cmode
= uap
->mode
;
327 struct pshmnode
* pnode
= PSHMNODE_NULL
;
328 struct pshmcache
* pcache
= PSHMCACHE_NULL
;
332 pinfo
= PSHMINFO_NULL
;
334 MALLOC_ZONE(pnbuf
, caddr_t
,
335 MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
336 pathlen
= MAXPATHLEN
;
337 error
= copyinstr(uap
->name
, pnbuf
,
338 MAXPATHLEN
, &pathlen
);
342 if (pathlen
> PSHMNAMLEN
) {
343 error
= ENAMETOOLONG
;
348 #ifdef PSXSHM_NAME_RESTRICT
350 if (*nameptr
== '/') {
351 while (*(nameptr
++) == '/') {
360 #endif /* PSXSHM_NAME_RESTRICT */
364 nd
.pshm_nameptr
= nameptr
;
365 nd
.pshm_namelen
= plen
;
368 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
369 nd
.pshm_hash
+= (unsigned char)*cp
* i
;
372 error
= pshm_cache_search(&pinfo
, &nd
, &pcache
);
374 if (error
== ENOENT
) {
383 fmode
= FFLAGS(uap
->oflag
);
384 if ((fmode
& (FREAD
| FWRITE
))==0)
387 if (error
= falloc(p
, &nfp
, &indx
))
393 if (fmode
& O_CREAT
) {
394 if ((fmode
& O_EXCL
) && incache
) {
395 /* shm obj exists and opened O_EXCL */
397 if (pinfo
->pshm_flags
& PSHM_INDELETE
) {
404 /* create a new one */
405 pinfo
= (struct pshminfo
*)_MALLOC(sizeof(struct pshminfo
), M_SHM
, M_WAITOK
);
406 bzero(pinfo
, sizeof(struct pshminfo
));
408 pinfo
->pshm_flags
= PSHM_DEFINED
| PSHM_INCREATE
;
409 pinfo
->pshm_usecount
= 1;
410 pinfo
->pshm_mode
= cmode
;
411 pinfo
->pshm_uid
= p
->p_ucred
->cr_uid
;
412 pinfo
->pshm_gid
= p
->p_ucred
->cr_gid
;
415 if( pinfo
->pshm_flags
& PSHM_INDELETE
) {
419 if (error
= pshm_access(pinfo
, fmode
, p
->p_ucred
, p
))
424 /* O_CREAT is not set and the shm obecj does not exist */
428 if( pinfo
->pshm_flags
& PSHM_INDELETE
) {
432 if (error
= pshm_access(pinfo
, fmode
, p
->p_ucred
, p
))
435 if (fmode
& O_TRUNC
) {
441 pinfo
->pshm_writecount
++;
443 pinfo
->pshm_readcount
++;
445 pnode
= (struct pshmnode
*)_MALLOC(sizeof(struct pshmnode
), M_SHM
, M_WAITOK
);
446 bzero(pnode
, sizeof(struct pshmnode
));
449 if (error
= pshm_cache_add(pinfo
, &nd
)) {
453 pinfo
->pshm_flags
&= ~PSHM_INCREATE
;
454 pinfo
->pshm_usecount
++;
455 pnode
->pinfo
= pinfo
;
456 fp
->f_flag
= fmode
& FMASK
;
457 fp
->f_type
= DTYPE_PSXSHM
;
458 fp
->f_ops
= &pshmops
;
459 fp
->f_data
= (caddr_t
)pnode
;
460 *fdflags(p
, indx
) &= ~UF_RESERVED
;
462 _FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
474 _FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
481 pshm_truncate(p
, fp
, fd
, length
, retval
)
488 struct pshminfo
* pinfo
;
489 struct pshmnode
* pnode
;
491 vm_offset_t user_addr
;
495 if (fp
->f_type
!= DTYPE_PSXSHM
) {
500 if (((pnode
= (struct pshmnode
*)fp
->f_data
)) == PSHMNODE_NULL
)
503 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
)
505 if ((pinfo
->pshm_flags
& (PSHM_DEFINED
| PSHM_ALLOCATED
))
510 size
= round_page (length
);
511 kret
= vm_allocate(current_map(), &user_addr
, size
, TRUE
);
512 if (kret
!= KERN_SUCCESS
)
515 kret
= mach_make_memory_entry (current_map(), &size
,
516 user_addr
, VM_PROT_DEFAULT
, &mem_object
, 0);
518 if (kret
!= KERN_SUCCESS
)
521 vm_deallocate(current_map(), user_addr
, size
);
523 pinfo
->pshm_flags
&= ~PSHM_DEFINED
;
524 pinfo
->pshm_flags
= PSHM_ALLOCATED
;
525 pinfo
->pshm_memobject
= mem_object
;
526 pinfo
->pshm_length
= size
;
531 case KERN_INVALID_ADDRESS
:
534 case KERN_PROTECTION_FAILURE
:
544 struct pshmnode
*pnode
;
547 struct pshminfo
*pinfo
;
549 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
)
552 bzero(sb
, sizeof(struct stat
));
553 sb
->st_mode
= pinfo
->pshm_mode
;
554 sb
->st_uid
= pinfo
->pshm_uid
;
555 sb
->st_gid
= pinfo
->pshm_gid
;
556 sb
->st_size
= pinfo
->pshm_length
;
562 pshm_access(struct pshminfo
*pinfo
, int mode
, struct ucred
*cred
, struct proc
*p
)
568 /* Otherwise, user id 0 always gets access. */
569 if (cred
->cr_uid
== 0)
574 /* Otherwise, check the owner. */
575 if (cred
->cr_uid
== pinfo
->pshm_uid
) {
580 return ((pinfo
->pshm_mode
& mask
) == mask
? 0 : EACCES
);
583 /* Otherwise, check the groups. */
584 for (i
= 0, gp
= cred
->cr_groups
; i
< cred
->cr_ngroups
; i
++, gp
++)
585 if (pinfo
->pshm_gid
== *gp
) {
590 return ((pinfo
->pshm_mode
& mask
) == mask
? 0 : EACCES
);
593 /* Otherwise, check everyone else. */
598 return ((pinfo
->pshm_mode
& mask
) == mask
? 0 : EACCES
);
607 #ifdef DOUBLE_ALIGN_PARAMS
614 pshm_mmap(struct proc
*p
, struct mmap_args
*uap
, register_t
*retval
, struct file
*fp
, vm_size_t pageoff
)
616 vm_offset_t user_addr
= uap
->addr
;
617 vm_size_t user_size
= uap
->len
;
618 int prot
= uap
->prot
;
619 int flags
= uap
->flags
;
620 vm_object_offset_t file_pos
= (vm_object_offset_t
)uap
->pos
;
623 boolean_t find_space
,docow
;
625 struct pshminfo
* pinfo
;
626 struct pshmnode
* pnode
;
632 if ((flags
& MAP_SHARED
) == 0)
636 if ((prot
& PROT_WRITE
) && ((fp
->f_flag
& FWRITE
) == 0)) {
640 if (((pnode
= (struct pshmnode
*)fp
->f_data
)) == PSHMNODE_NULL
)
643 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
)
646 if ((pinfo
->pshm_flags
& PSHM_ALLOCATED
) != PSHM_ALLOCATED
) {
649 if (user_size
> pinfo
->pshm_length
) {
652 if ((off_t
)user_size
+ file_pos
> pinfo
->pshm_length
) {
655 if ((mem_object
= pinfo
->pshm_memobject
) == NULL
) {
660 user_map
= current_map();
662 if ((flags
& MAP_FIXED
) == 0) {
664 user_addr
= round_page(user_addr
);
666 if (user_addr
!= trunc_page(user_addr
))
669 (void) vm_deallocate(user_map
, user_addr
, user_size
);
673 kret
= vm_map_64(user_map
, &user_addr
, user_size
,
674 0, find_space
, pinfo
->pshm_memobject
, file_pos
, docow
,
675 prot
, VM_PROT_DEFAULT
,
678 if (kret
!= KERN_SUCCESS
)
680 kret
= vm_inherit(user_map
, user_addr
, user_size
,
682 if (kret
!= KERN_SUCCESS
) {
683 (void) vm_deallocate(user_map
, user_addr
, user_size
);
686 pnode
->mapp_addr
= user_addr
;
687 pnode
->map_size
= user_size
;
688 pinfo
->pshm_flags
|= (PSHM_MAPPED
| PSHM_INUSE
);
692 *fdflags(p
, fd
) |= UF_MAPPED
;
693 *retval
= (register_t
)(user_addr
+ pageoff
);
695 case KERN_INVALID_ADDRESS
:
698 case KERN_PROTECTION_FAILURE
:
706 struct shm_unlink_args
{
711 shm_unlink(p
, uap
, retval
)
713 register struct shm_unlink_args
*uap
;
716 register struct filedesc
*fdp
= p
->p_fd
;
717 register struct file
*fp
;
721 struct pshminfo
*pinfo
;
722 extern struct fileops pshmops
;
726 size_t pathlen
, plen
;
729 struct pshmnode
* pnode
= PSHMNODE_NULL
;
730 struct pshmcache
*pcache
= PSHMCACHE_NULL
;
733 pinfo
= PSHMINFO_NULL
;
735 MALLOC_ZONE(pnbuf
, caddr_t
,
736 MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
737 pathlen
= MAXPATHLEN
;
738 error
= copyinstr(uap
->name
, pnbuf
,
739 MAXPATHLEN
, &pathlen
);
743 if (pathlen
> PSHMNAMLEN
) {
744 error
= ENAMETOOLONG
;
749 #ifdef PSXSHM_NAME_RESTRICT
751 if (*nameptr
== '/') {
752 while (*(nameptr
++) == '/') {
761 #endif /* PSXSHM_NAME_RESTRICT */
765 nd
.pshm_nameptr
= nameptr
;
766 nd
.pshm_namelen
= plen
;
769 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
770 nd
.pshm_hash
+= (unsigned char)*cp
* i
;
773 error
= pshm_cache_search(&pinfo
, &nd
, &pcache
);
775 if (error
== ENOENT
) {
786 if ((pinfo
->pshm_flags
& (PSHM_DEFINED
| PSHM_ALLOCATED
))==0) {
790 if (pinfo
->pshm_flags
& PSHM_INDELETE
) {
795 if (pinfo
->pshm_memobject
== NULL
) {
800 pinfo
->pshm_flags
|= PSHM_INDELETE
;
801 pinfo
->pshm_usecount
--;
802 kret
= mach_destroy_memory_entry(pinfo
->pshm_memobject
);
803 pshm_cache_delete(pcache
);
804 _FREE(pcache
, M_SHM
);
805 pinfo
->pshm_flags
|= PSHM_REMOVED
;
808 _FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
812 case KERN_INVALID_ADDRESS
:
813 case KERN_PROTECTION_FAILURE
:
821 pshm_close(pnode
, flags
, cred
, p
)
822 register struct pshmnode
*pnode
;
829 register struct pshminfo
*pinfo
;
831 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
)
834 if ((pinfo
->pshm_flags
& PSHM_ALLOCATED
) != PSHM_ALLOCATED
) {
838 if(!pinfo
->pshm_usecount
) {
839 kprintf("negative usecount in pshm_close\n");
841 #endif /* DIAGNOSTIC */
842 pinfo
->pshm_usecount
--;
844 if ((pinfo
->pshm_flags
& PSHM_REMOVED
) && !pinfo
->pshm_usecount
) {
852 pshm_closefile(fp
, p
)
856 return (pshm_close(((struct pshmnode
*)fp
->f_data
), fp
->f_flag
,
861 pshm_read(fp
, uio
, cred
, flags
, p
)
872 pshm_write(fp
, uio
, cred
, flags
, p
)
883 pshm_ioctl(fp
, com
, data
, p
)
893 pshm_select(fp
, which
, wql
, p
)