2 * Copyright (c) 2000 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 int pshm_read
__P((struct file
*fp
, struct uio
*uio
,
134 struct ucred
*cred
));
135 int pshm_write
__P((struct file
*fp
, struct uio
*uio
,
136 struct ucred
*cred
));
137 int pshm_ioctl
__P((struct file
*fp
, u_long com
,
138 caddr_t data
, struct proc
*p
));
139 int pshm_select
__P((struct file
*fp
, int which
,
141 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
};
149 * Lookup an entry in the cache
152 * status of -1 is returned if matches
153 * If the lookup determines that the name does not exist
154 * (negative cacheing), a status of ENOENT is returned. If the lookup
155 * fails, a status of zero is returned.
159 pshm_cache_search(pshmp
, pnp
, pcache
)
160 struct pshminfo
**pshmp
;
161 struct pshmname
*pnp
;
162 struct pshmcache
**pcache
;
164 register struct pshmcache
*pcp
, *nnp
;
165 register struct pshmhashhead
*pcpp
;
167 if (pnp
->pshm_namelen
> PSHMNAMLEN
) {
168 pshmstats
.longnames
++;
172 pcpp
= PSHMHASH(pnp
);
173 for (pcp
= pcpp
->lh_first
; pcp
!= 0; pcp
= nnp
) {
174 nnp
= pcp
->pshm_hash
.le_next
;
175 if (pcp
->pshm_nlen
== pnp
->pshm_namelen
&&
176 !bcmp(pcp
->pshm_name
, pnp
->pshm_nameptr
, (u_int
)pcp
-> pshm_nlen
))
185 /* We found a "positive" match, return the vnode */
187 pshmstats
.goodhits
++;
189 *pshmp
= pcp
->pshminfo
;
195 * We found a "negative" match, ENOENT notifies client of this match.
196 * The nc_vpid field records whether this is a whiteout.
203 * Add an entry to the cache.
206 pshm_cache_add(pshmp
, pnp
)
207 struct pshminfo
*pshmp
;
208 struct pshmname
*pnp
;
210 register struct pshmcache
*pcp
;
211 register struct pshmhashhead
*pcpp
;
212 register struct pshminfo
*dpinfo
;
213 register struct pshmcache
*dpcp
;
216 if (pnp
->pshm_namelen
> NCHNAMLEN
)
217 panic("cache_enter: name too long");
221 * We allocate a new entry if we are less than the maximum
222 * allowed and the one at the front of the LRU list is in use.
223 * Otherwise we use the one at the front of the LRU list.
225 pcp
= (struct pshmcache
*)_MALLOC(sizeof(struct pshmcache
), M_SHM
, M_WAITOK
);
226 /* if the entry has already been added by some one else return */
227 if (pshm_cache_search(&dpinfo
, pnp
, &dpcp
) == -1) {
233 bzero(pcp
, sizeof(struct pshmcache
));
235 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
236 * For negative entries, we have to record whether it is a whiteout.
237 * the whiteout flag is stored in the nc_vpid field which is
240 pcp
->pshminfo
= pshmp
;
241 pcp
->pshm_nlen
= pnp
->pshm_namelen
;
242 bcopy(pnp
->pshm_nameptr
, pcp
->pshm_name
, (unsigned)pcp
->pshm_nlen
);
243 pcpp
= PSHMHASH(pnp
);
246 register struct pshmcache
*p
;
248 for (p
= pcpp
->lh_first
; p
!= 0; p
= p
->pshm_hash
.le_next
)
250 panic("cache_enter: duplicate");
253 LIST_INSERT_HEAD(pcpp
, pcp
, pshm_hash
);
258 * Name cache initialization, from vfs_init() when we are booting
263 pshmhashtbl
= hashinit(desiredvnodes
, M_SHM
, &pshmhash
);
267 * Invalidate a all entries to particular vnode.
269 * We actually just increment the v_id, that will do it. The entries will
270 * be purged by lookup as they get found. If the v_id wraps around, we
271 * need to ditch the entire cache, to avoid confusion. No valid vnode will
272 * ever have (v_id == 0).
275 pshm_cache_purge(void)
277 struct pshmcache
*pcp
;
278 struct pshmhashhead
*pcpp
;
280 for (pcpp
= &pshmhashtbl
[pshmhash
]; pcpp
>= pshmhashtbl
; pcpp
--) {
281 while (pcp
= pcpp
->lh_first
)
282 pshm_cache_delete(pcp
);
286 pshm_cache_delete(pcp
)
287 struct pshmcache
*pcp
;
290 if (pcp
->pshm_hash
.le_prev
== 0)
291 panic("namecache purge le_prev");
292 if (pcp
->pshm_hash
.le_next
== pcp
)
293 panic("namecache purge le_next");
294 #endif /* DIAGNOSTIC */
295 LIST_REMOVE(pcp
, pshm_hash
);
296 pcp
->pshm_hash
.le_prev
= 0;
301 struct shm_open_args
{
308 shm_open(p
, uap
, retval
)
310 register struct shm_open_args
*uap
;
313 register struct filedesc
*fdp
= p
->p_fd
;
314 register struct file
*fp
;
315 register struct vnode
*vp
;
318 int type
, indx
, error
;
320 struct pshminfo
*pinfo
;
321 extern struct fileops pshmops
;
325 size_t pathlen
, plen
;
327 int cmode
= uap
->mode
;
329 struct pshmnode
* pnode
= PSHMNODE_NULL
;
330 struct pshmcache
* pcache
= PSHMCACHE_NULL
;
333 pinfo
= PSHMINFO_NULL
;
335 MALLOC_ZONE(pnbuf
, caddr_t
,
336 MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
337 pathlen
= MAXPATHLEN
;
338 error
= copyinstr(uap
->name
, pnbuf
,
339 MAXPATHLEN
, &pathlen
);
343 if (pathlen
> PSHMNAMLEN
) {
344 error
= ENAMETOOLONG
;
349 #ifdef PSXSHM_NAME_RESTRICT
351 if (*nameptr
== '/') {
352 while (*(nameptr
++) == '/') {
361 #endif /* PSXSHM_NAME_RESTRICT */
365 nd
.pshm_nameptr
= nameptr
;
366 nd
.pshm_namelen
= plen
;
369 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
370 nd
.pshm_hash
+= (unsigned char)*cp
* i
;
373 error
= pshm_cache_search(&pinfo
, &nd
, &pcache
);
375 if (error
== ENOENT
) {
384 fmode
= FFLAGS(uap
->oflag
);
385 if ((fmode
& (FREAD
| FWRITE
))==0)
388 if (error
= falloc(p
, &nfp
, &indx
))
394 if (fmode
& O_CREAT
) {
395 if ((fmode
& O_EXCL
) && incache
) {
396 /* shm obj exists and opened O_EXCL */
398 if (pinfo
->pshm_flags
& PSHM_INDELETE
) {
405 /* create a new one */
406 pinfo
= (struct pshminfo
*)_MALLOC(sizeof(struct pshminfo
), M_SHM
, M_WAITOK
);
407 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
);
471 _FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
478 pshm_truncate(p
, fp
, fd
, length
, retval
)
485 struct pshminfo
* pinfo
;
486 struct pshmnode
* pnode
;
488 vm_offset_t user_addr
;
492 if (fp
->f_type
!= DTYPE_PSXSHM
) {
497 if (((pnode
= (struct pshmnode
*)fp
->f_data
)) == PSHMNODE_NULL
)
500 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
)
502 if ((pinfo
->pshm_flags
& (PSHM_DEFINED
| PSHM_ALLOCATED
))
507 size
= round_page (length
);
508 kret
= vm_allocate(current_map(), &user_addr
, size
, TRUE
);
509 if (kret
!= KERN_SUCCESS
)
512 kret
= mach_make_memory_entry (current_map(), &size
,
513 user_addr
, VM_PROT_DEFAULT
, &mem_object
, 0);
515 if (kret
!= KERN_SUCCESS
)
518 vm_deallocate(current_map(), user_addr
, size
);
520 pinfo
->pshm_flags
&= ~PSHM_DEFINED
;
521 pinfo
->pshm_flags
= PSHM_ALLOCATED
;
522 pinfo
->pshm_memobject
= mem_object
;
523 pinfo
->pshm_length
= size
;
528 case KERN_INVALID_ADDRESS
:
531 case KERN_PROTECTION_FAILURE
:
541 struct pshmnode
*pnode
;
544 struct pshminfo
*pinfo
;
546 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
)
549 bzero(sb
, sizeof(struct stat
));
550 sb
->st_mode
= pinfo
->pshm_mode
;
551 sb
->st_uid
= pinfo
->pshm_uid
;
552 sb
->st_gid
= pinfo
->pshm_gid
;
553 sb
->st_size
= pinfo
->pshm_length
;
559 pshm_access(struct pshminfo
*pinfo
, int mode
, struct ucred
*cred
, struct proc
*p
)
565 /* Otherwise, user id 0 always gets access. */
566 if (cred
->cr_uid
== 0)
571 /* Otherwise, check the owner. */
572 if (cred
->cr_uid
== pinfo
->pshm_uid
) {
577 return ((pinfo
->pshm_mode
& mask
) == mask
? 0 : EACCES
);
580 /* Otherwise, check the groups. */
581 for (i
= 0, gp
= cred
->cr_groups
; i
< cred
->cr_ngroups
; i
++, gp
++)
582 if (pinfo
->pshm_gid
== *gp
) {
587 return ((pinfo
->pshm_mode
& mask
) == mask
? 0 : EACCES
);
590 /* Otherwise, check everyone else. */
595 return ((pinfo
->pshm_mode
& mask
) == mask
? 0 : EACCES
);
603 #ifdef DOUBLE_ALIGN_PARAMS
610 pshm_mmap(struct proc
*p
, struct mmap_args
*uap
, register_t
*retval
, struct file
*fp
, vm_size_t pageoff
)
612 vm_offset_t user_addr
= uap
->addr
;
613 vm_size_t user_size
= uap
->len
;
614 int prot
= uap
->prot
;
615 int flags
= uap
->flags
;
616 vm_object_offset_t file_pos
= (vm_object_offset_t
)uap
->pos
;
619 boolean_t find_space
,docow
;
621 struct pshminfo
* pinfo
;
622 struct pshmnode
* pnode
;
628 if ((flags
& MAP_SHARED
) == 0)
632 if ((prot
& PROT_WRITE
) && ((fp
->f_flag
& FWRITE
) == 0)) {
636 if (((pnode
= (struct pshmnode
*)fp
->f_data
)) == PSHMNODE_NULL
)
639 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
)
642 if ((pinfo
->pshm_flags
& PSHM_ALLOCATED
) != PSHM_ALLOCATED
) {
645 if (user_size
> pinfo
->pshm_length
) {
648 if ((off_t
)user_size
+ file_pos
> pinfo
->pshm_length
) {
651 if ((mem_object
= pinfo
->pshm_memobject
) == NULL
) {
656 user_map
= current_map();
658 if ((flags
& MAP_FIXED
) == 0) {
660 user_addr
= round_page(user_addr
);
662 if (user_addr
!= trunc_page(user_addr
))
665 (void) vm_deallocate(user_map
, user_addr
, user_size
);
669 kret
= vm_map_64(user_map
, &user_addr
, user_size
,
670 0, find_space
, pinfo
->pshm_memobject
, file_pos
, docow
,
671 prot
, VM_PROT_DEFAULT
,
674 if (kret
!= KERN_SUCCESS
)
676 kret
= vm_inherit(user_map
, user_addr
, user_size
,
678 if (kret
!= KERN_SUCCESS
) {
679 (void) vm_deallocate(user_map
, user_addr
, user_size
);
682 pnode
->mapp_addr
= user_addr
;
683 pnode
->map_size
= user_size
;
684 pinfo
->pshm_flags
|= (PSHM_MAPPED
| PSHM_INUSE
);
688 *fdflags(p
, fd
) |= UF_MAPPED
;
689 *retval
= (register_t
)(user_addr
+ pageoff
);
691 case KERN_INVALID_ADDRESS
:
694 case KERN_PROTECTION_FAILURE
:
702 struct shm_unlink_args
{
707 shm_unlink(p
, uap
, retval
)
709 register struct shm_unlink_args
*uap
;
712 register struct filedesc
*fdp
= p
->p_fd
;
713 register struct file
*fp
;
717 struct pshminfo
*pinfo
;
718 extern struct fileops pshmops
;
722 size_t pathlen
, plen
;
725 struct pshmnode
* pnode
= PSHMNODE_NULL
;
726 struct pshmcache
*pcache
= PSHMCACHE_NULL
;
729 pinfo
= PSHMINFO_NULL
;
731 MALLOC_ZONE(pnbuf
, caddr_t
,
732 MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
733 pathlen
= MAXPATHLEN
;
734 error
= copyinstr(uap
->name
, pnbuf
,
735 MAXPATHLEN
, &pathlen
);
739 if (pathlen
> PSHMNAMLEN
) {
740 error
= ENAMETOOLONG
;
745 #ifdef PSXSHM_NAME_RESTRICT
747 if (*nameptr
== '/') {
748 while (*(nameptr
++) == '/') {
757 #endif /* PSXSHM_NAME_RESTRICT */
761 nd
.pshm_nameptr
= nameptr
;
762 nd
.pshm_namelen
= plen
;
765 for (cp
= nameptr
, i
=1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
766 nd
.pshm_hash
+= (unsigned char)*cp
* i
;
769 error
= pshm_cache_search(&pinfo
, &nd
, &pcache
);
771 if (error
== ENOENT
) {
782 if ((pinfo
->pshm_flags
& (PSHM_DEFINED
| PSHM_ALLOCATED
))==0) {
786 if (pinfo
->pshm_flags
& PSHM_INDELETE
) {
791 if (pinfo
->pshm_memobject
== NULL
) {
796 pinfo
->pshm_flags
|= PSHM_INDELETE
;
797 pinfo
->pshm_usecount
--;
798 kret
= mach_destroy_memory_entry(pinfo
->pshm_memobject
);
799 pshm_cache_delete(pcache
);
800 _FREE(pcache
, M_SHM
);
801 pinfo
->pshm_flags
|= PSHM_REMOVED
;
804 _FREE_ZONE(pnbuf
, MAXPATHLEN
, M_NAMEI
);
808 case KERN_INVALID_ADDRESS
:
809 case KERN_PROTECTION_FAILURE
:
816 pshm_closefile(fp
, p
)
821 return (pshm_close(((struct pshmnode
*)fp
->f_data
), fp
->f_flag
,
826 pshm_close(pnode
, flags
, cred
, p
)
827 register struct pshmnode
*pnode
;
834 register struct pshminfo
*pinfo
;
836 if ((pinfo
= pnode
->pinfo
) == PSHMINFO_NULL
)
839 if ((pinfo
->pshm_flags
& PSHM_ALLOCATED
) != PSHM_ALLOCATED
) {
843 if(!pinfo
->pshm_usecount
) {
844 kprintf("negative usecount in pshm_close\n");
846 #endif /* DIAGNOSTIC */
847 pinfo
->pshm_usecount
--;
849 if ((pinfo
->pshm_flags
& PSHM_REMOVED
) && !pinfo
->pshm_usecount
) {
856 pshm_read(struct file
*fp
, struct uio
*uio
, struct ucred
*cred
)
861 pshm_write(struct file
*fp
, struct uio
*uio
, struct ucred
*cred
)
866 pshm_ioctl(struct file
*fp
, u_long com
, caddr_t data
, struct proc
*p
)
871 pshm_select(struct file
*fp
, int which
, struct proc
*p
)