xref: /titanic_53/usr/src/uts/common/os/shm.c (revision c6939658adb0a356a77bc28f7df252ceb4a8f6cc)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
507b65a64Saguzovsk  * Common Development and Distribution License (the "License").
607b65a64Saguzovsk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2207b65a64Saguzovsk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate  * The Regents of the University of California
327c478bd9Sstevel@tonic-gate  * All Rights Reserved
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate  * contributors.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * Inter-Process Communication Shared Memory Facility.
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  * See os/ipc.c for a description of common IPC functionality.
457c478bd9Sstevel@tonic-gate  *
467c478bd9Sstevel@tonic-gate  * Resource controls
477c478bd9Sstevel@tonic-gate  * -----------------
487c478bd9Sstevel@tonic-gate  *
49824c205fSml93401  * Control:      zone.max-shm-ids (rc_zone_shmmni)
50824c205fSml93401  * Description:  Maximum number of shared memory ids allowed a zone.
51824c205fSml93401  *
52824c205fSml93401  *   When shmget() is used to allocate a shared memory segment, one id
53824c205fSml93401  *   is allocated.  If the id allocation doesn't succeed, shmget()
54824c205fSml93401  *   fails and errno is set to ENOSPC.  Upon successful shmctl(,
55824c205fSml93401  *   IPC_RMID) the id is deallocated.
56824c205fSml93401  *
577c478bd9Sstevel@tonic-gate  * Control:      project.max-shm-ids (rc_project_shmmni)
587c478bd9Sstevel@tonic-gate  * Description:  Maximum number of shared memory ids allowed a project.
597c478bd9Sstevel@tonic-gate  *
607c478bd9Sstevel@tonic-gate  *   When shmget() is used to allocate a shared memory segment, one id
617c478bd9Sstevel@tonic-gate  *   is allocated.  If the id allocation doesn't succeed, shmget()
627c478bd9Sstevel@tonic-gate  *   fails and errno is set to ENOSPC.  Upon successful shmctl(,
637c478bd9Sstevel@tonic-gate  *   IPC_RMID) the id is deallocated.
647c478bd9Sstevel@tonic-gate  *
65824c205fSml93401  * Control:      zone.max-shm-memory (rc_zone_shmmax)
66824c205fSml93401  * Description:  Total amount of shared memory allowed a zone.
67824c205fSml93401  *
68824c205fSml93401  *   When shmget() is used to allocate a shared memory segment, the
69824c205fSml93401  *   segment's size is allocated against this limit.  If the space
70824c205fSml93401  *   allocation doesn't succeed, shmget() fails and errno is set to
71824c205fSml93401  *   EINVAL.  The size will be deallocated once the last process has
72824c205fSml93401  *   detached the segment and the segment has been successfully
73824c205fSml93401  *   shmctl(, IPC_RMID)ed.
74824c205fSml93401  *
757c478bd9Sstevel@tonic-gate  * Control:      project.max-shm-memory (rc_project_shmmax)
767c478bd9Sstevel@tonic-gate  * Description:  Total amount of shared memory allowed a project.
777c478bd9Sstevel@tonic-gate  *
787c478bd9Sstevel@tonic-gate  *   When shmget() is used to allocate a shared memory segment, the
797c478bd9Sstevel@tonic-gate  *   segment's size is allocated against this limit.  If the space
807c478bd9Sstevel@tonic-gate  *   allocation doesn't succeed, shmget() fails and errno is set to
817c478bd9Sstevel@tonic-gate  *   EINVAL.  The size will be deallocated once the last process has
827c478bd9Sstevel@tonic-gate  *   detached the segment and the segment has been successfully
837c478bd9Sstevel@tonic-gate  *   shmctl(, IPC_RMID)ed.
847c478bd9Sstevel@tonic-gate  */
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate #include <sys/types.h>
877c478bd9Sstevel@tonic-gate #include <sys/param.h>
887c478bd9Sstevel@tonic-gate #include <sys/cred.h>
897c478bd9Sstevel@tonic-gate #include <sys/errno.h>
907c478bd9Sstevel@tonic-gate #include <sys/time.h>
917c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
927c478bd9Sstevel@tonic-gate #include <sys/user.h>
937c478bd9Sstevel@tonic-gate #include <sys/proc.h>
947c478bd9Sstevel@tonic-gate #include <sys/systm.h>
957c478bd9Sstevel@tonic-gate #include <sys/prsystm.h>
967c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
977c478bd9Sstevel@tonic-gate #include <sys/tuneable.h>
987c478bd9Sstevel@tonic-gate #include <sys/vm.h>
997c478bd9Sstevel@tonic-gate #include <sys/mman.h>
1007c478bd9Sstevel@tonic-gate #include <sys/swap.h>
1017c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
1027c478bd9Sstevel@tonic-gate #include <sys/debug.h>
1037c478bd9Sstevel@tonic-gate #include <sys/lwpchan_impl.h>
1047c478bd9Sstevel@tonic-gate #include <sys/avl.h>
1057c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
1067c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
1077c478bd9Sstevel@tonic-gate #include <sys/task.h>
1087c478bd9Sstevel@tonic-gate #include <sys/project.h>
1097c478bd9Sstevel@tonic-gate #include <sys/policy.h>
1107c478bd9Sstevel@tonic-gate #include <sys/zone.h>
111*c6939658Ssl108498 #include <sys/rctl.h>
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate #include <sys/ipc.h>
1147c478bd9Sstevel@tonic-gate #include <sys/ipc_impl.h>
1157c478bd9Sstevel@tonic-gate #include <sys/shm.h>
1167c478bd9Sstevel@tonic-gate #include <sys/shm_impl.h>
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate #include <vm/hat.h>
1197c478bd9Sstevel@tonic-gate #include <vm/seg.h>
1207c478bd9Sstevel@tonic-gate #include <vm/as.h>
1217c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h>
1227c478bd9Sstevel@tonic-gate #include <vm/anon.h>
1237c478bd9Sstevel@tonic-gate #include <vm/page.h>
1247c478bd9Sstevel@tonic-gate #include <vm/vpage.h>
1257c478bd9Sstevel@tonic-gate #include <vm/seg_spt.h>
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate #include <c2/audit.h>
1287c478bd9Sstevel@tonic-gate 
129*c6939658Ssl108498 static int shmem_lock(kshmid_t *sp, struct anon_map *amp);
130*c6939658Ssl108498 static void shmem_unlock(kshmid_t *sp, struct anon_map *amp);
1317c478bd9Sstevel@tonic-gate static void sa_add(struct proc *pp, caddr_t addr, size_t len, ulong_t flags,
1327c478bd9Sstevel@tonic-gate 	kshmid_t *id);
133*c6939658Ssl108498 static void shm_rm_amp(struct anon_map *amp);
1347c478bd9Sstevel@tonic-gate static void shm_dtor(kipc_perm_t *);
1357c478bd9Sstevel@tonic-gate static void shm_rmid(kipc_perm_t *);
1367c478bd9Sstevel@tonic-gate static void shm_remove_zone(zoneid_t, void *);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate  * Semantics for share_page_table and ism_off:
1407c478bd9Sstevel@tonic-gate  *
1417c478bd9Sstevel@tonic-gate  * These are hooks in /etc/system - only for internal testing purpose.
1427c478bd9Sstevel@tonic-gate  *
1437c478bd9Sstevel@tonic-gate  * Setting share_page_table automatically turns on the SHM_SHARE_MMU (ISM) flag
1447c478bd9Sstevel@tonic-gate  * in a call to shmat(2). In other words, with share_page_table set, you always
1457c478bd9Sstevel@tonic-gate  * get ISM, even if say, DISM is specified. It should really be called "ism_on".
1467c478bd9Sstevel@tonic-gate  *
1477c478bd9Sstevel@tonic-gate  * Setting ism_off turns off the SHM_SHARE_MMU flag from the flags passed to
1487c478bd9Sstevel@tonic-gate  * shmat(2).
1497c478bd9Sstevel@tonic-gate  *
1507c478bd9Sstevel@tonic-gate  * If both share_page_table and ism_off are set, share_page_table prevails.
1517c478bd9Sstevel@tonic-gate  *
1527c478bd9Sstevel@tonic-gate  * Although these tunables should probably be removed, they do have some
1537c478bd9Sstevel@tonic-gate  * external exposure; as long as they exist, they should at least work sensibly.
1547c478bd9Sstevel@tonic-gate  */
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate int share_page_table;
1577c478bd9Sstevel@tonic-gate int ism_off;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate /*
1607c478bd9Sstevel@tonic-gate  * The following tunables are obsolete.  Though for compatibility we
1617c478bd9Sstevel@tonic-gate  * still read and interpret shminfo_shmmax and shminfo_shmmni (see
1627c478bd9Sstevel@tonic-gate  * os/project.c), the preferred mechanism for administrating the IPC
1637c478bd9Sstevel@tonic-gate  * Shared Memory facility is through the resource controls described at
1647c478bd9Sstevel@tonic-gate  * the top of this file.
1657c478bd9Sstevel@tonic-gate  */
1667c478bd9Sstevel@tonic-gate size_t	shminfo_shmmax = 0x800000;	/* (obsolete) */
1677c478bd9Sstevel@tonic-gate int	shminfo_shmmni = 100;		/* (obsolete) */
1687c478bd9Sstevel@tonic-gate size_t	shminfo_shmmin = 1;		/* (obsolete) */
1697c478bd9Sstevel@tonic-gate int	shminfo_shmseg = 6;		/* (obsolete) */
1707c478bd9Sstevel@tonic-gate 
171824c205fSml93401 extern rctl_hndl_t rc_zone_shmmax;
172824c205fSml93401 extern rctl_hndl_t rc_zone_shmmni;
1737c478bd9Sstevel@tonic-gate extern rctl_hndl_t rc_project_shmmax;
1747c478bd9Sstevel@tonic-gate extern rctl_hndl_t rc_project_shmmni;
1757c478bd9Sstevel@tonic-gate static ipc_service_t *shm_svc;
1767c478bd9Sstevel@tonic-gate static zone_key_t shm_zone_key;
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate /*
1797c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
1807c478bd9Sstevel@tonic-gate  */
1817c478bd9Sstevel@tonic-gate static uintptr_t shmsys(int, uintptr_t, uintptr_t, uintptr_t);
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate static struct sysent ipcshm_sysent = {
1847c478bd9Sstevel@tonic-gate 	4,
1857c478bd9Sstevel@tonic-gate #ifdef	_SYSCALL32_IMPL
1867c478bd9Sstevel@tonic-gate 	SE_ARGC | SE_NOUNLOAD | SE_64RVAL,
1877c478bd9Sstevel@tonic-gate #else	/* _SYSCALL32_IMPL */
1887c478bd9Sstevel@tonic-gate 	SE_ARGC | SE_NOUNLOAD | SE_32RVAL1,
1897c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
1907c478bd9Sstevel@tonic-gate 	(int (*)())shmsys
1917c478bd9Sstevel@tonic-gate };
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate #ifdef	_SYSCALL32_IMPL
1947c478bd9Sstevel@tonic-gate static struct sysent ipcshm_sysent32 = {
1957c478bd9Sstevel@tonic-gate 	4,
1967c478bd9Sstevel@tonic-gate 	SE_ARGC | SE_NOUNLOAD | SE_32RVAL1,
1977c478bd9Sstevel@tonic-gate 	(int (*)())shmsys
1987c478bd9Sstevel@tonic-gate };
1997c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate static struct modlsys modlsys = {
2027c478bd9Sstevel@tonic-gate 	&mod_syscallops, "System V shared memory", &ipcshm_sysent
2037c478bd9Sstevel@tonic-gate };
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate #ifdef	_SYSCALL32_IMPL
2067c478bd9Sstevel@tonic-gate static struct modlsys modlsys32 = {
2077c478bd9Sstevel@tonic-gate 	&mod_syscallops32, "32-bit System V shared memory", &ipcshm_sysent32
2087c478bd9Sstevel@tonic-gate };
2097c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
2127c478bd9Sstevel@tonic-gate 	MODREV_1,
2137c478bd9Sstevel@tonic-gate 	&modlsys,
2147c478bd9Sstevel@tonic-gate #ifdef	_SYSCALL32_IMPL
2157c478bd9Sstevel@tonic-gate 	&modlsys32,
2167c478bd9Sstevel@tonic-gate #endif
2177c478bd9Sstevel@tonic-gate 	NULL
2187c478bd9Sstevel@tonic-gate };
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate int
2227c478bd9Sstevel@tonic-gate _init(void)
2237c478bd9Sstevel@tonic-gate {
2247c478bd9Sstevel@tonic-gate 	int result;
2257c478bd9Sstevel@tonic-gate 
226824c205fSml93401 	shm_svc = ipcs_create("shmids", rc_project_shmmni, rc_zone_shmmni,
227824c205fSml93401 	    sizeof (kshmid_t), shm_dtor, shm_rmid, AT_IPC_SHM,
228824c205fSml93401 	    offsetof(ipc_rqty_t, ipcq_shmmni));
2297c478bd9Sstevel@tonic-gate 	zone_key_create(&shm_zone_key, NULL, shm_remove_zone, NULL);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	if ((result = mod_install(&modlinkage)) == 0)
2327c478bd9Sstevel@tonic-gate 		return (0);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	(void) zone_key_delete(shm_zone_key);
2357c478bd9Sstevel@tonic-gate 	ipcs_destroy(shm_svc);
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	return (result);
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate int
2417c478bd9Sstevel@tonic-gate _fini(void)
2427c478bd9Sstevel@tonic-gate {
2437c478bd9Sstevel@tonic-gate 	return (EBUSY);
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate int
2477c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2487c478bd9Sstevel@tonic-gate {
2497c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate /*
2537c478bd9Sstevel@tonic-gate  * Shmat (attach shared segment) system call.
2547c478bd9Sstevel@tonic-gate  */
2557c478bd9Sstevel@tonic-gate static int
2567c478bd9Sstevel@tonic-gate shmat(int shmid, caddr_t uaddr, int uflags, uintptr_t *rvp)
2577c478bd9Sstevel@tonic-gate {
2587c478bd9Sstevel@tonic-gate 	kshmid_t *sp;	/* shared memory header ptr */
2597c478bd9Sstevel@tonic-gate 	size_t	size;
2607c478bd9Sstevel@tonic-gate 	int	error = 0;
2617c478bd9Sstevel@tonic-gate 	proc_t *pp = curproc;
2627c478bd9Sstevel@tonic-gate 	struct as *as = pp->p_as;
2637c478bd9Sstevel@tonic-gate 	struct segvn_crargs	crargs;	/* segvn create arguments */
2647c478bd9Sstevel@tonic-gate 	kmutex_t	*lock;
2657c478bd9Sstevel@tonic-gate 	struct seg 	*segspt = NULL;
2667c478bd9Sstevel@tonic-gate 	caddr_t		addr = uaddr;
2677c478bd9Sstevel@tonic-gate 	int		flags = (uflags & SHMAT_VALID_FLAGS_MASK);
2687c478bd9Sstevel@tonic-gate 	int		useISM;
2697c478bd9Sstevel@tonic-gate 	uchar_t		prot = PROT_ALL;
2707c478bd9Sstevel@tonic-gate 	int result;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	if ((lock = ipc_lookup(shm_svc, shmid, (kipc_perm_t **)&sp)) == NULL)
2737c478bd9Sstevel@tonic-gate 		return (EINVAL);
2747c478bd9Sstevel@tonic-gate 	if (error = ipcperm_access(&sp->shm_perm, SHM_R, CRED()))
2757c478bd9Sstevel@tonic-gate 		goto errret;
2767c478bd9Sstevel@tonic-gate 	if ((flags & SHM_RDONLY) == 0 &&
2777c478bd9Sstevel@tonic-gate 	    (error = ipcperm_access(&sp->shm_perm, SHM_W, CRED())))
2787c478bd9Sstevel@tonic-gate 		goto errret;
2797c478bd9Sstevel@tonic-gate 	if (spt_invalid(flags)) {
2807c478bd9Sstevel@tonic-gate 		error = EINVAL;
2817c478bd9Sstevel@tonic-gate 		goto errret;
2827c478bd9Sstevel@tonic-gate 	}
2837c478bd9Sstevel@tonic-gate 	if (ism_off)
2847c478bd9Sstevel@tonic-gate 		flags = flags & ~SHM_SHARE_MMU;
2857c478bd9Sstevel@tonic-gate 	if (share_page_table) {
2867c478bd9Sstevel@tonic-gate 		flags = flags & ~SHM_PAGEABLE;
2877c478bd9Sstevel@tonic-gate 		flags = flags | SHM_SHARE_MMU;
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 	useISM = (spt_locked(flags) || spt_pageable(flags));
2907c478bd9Sstevel@tonic-gate 	if (useISM && (error = ipcperm_access(&sp->shm_perm, SHM_W, CRED())))
2917c478bd9Sstevel@tonic-gate 		goto errret;
2927c478bd9Sstevel@tonic-gate 	if (useISM && isspt(sp)) {
2937c478bd9Sstevel@tonic-gate 		uint_t newsptflags = flags | spt_flags(sp->shm_sptseg);
2947c478bd9Sstevel@tonic-gate 		/*
2957c478bd9Sstevel@tonic-gate 		 * If trying to change an existing {D}ISM segment from ISM
2967c478bd9Sstevel@tonic-gate 		 * to DISM or vice versa, return error. Note that this
2977c478bd9Sstevel@tonic-gate 		 * validation of flags needs to be done after the effect of
2987c478bd9Sstevel@tonic-gate 		 * tunables such as ism_off and share_page_table, for
2997c478bd9Sstevel@tonic-gate 		 * semantics that are consistent with the tunables' settings.
3007c478bd9Sstevel@tonic-gate 		 */
3017c478bd9Sstevel@tonic-gate 		if (spt_invalid(newsptflags)) {
3027c478bd9Sstevel@tonic-gate 			error = EINVAL;
3037c478bd9Sstevel@tonic-gate 			goto errret;
3047c478bd9Sstevel@tonic-gate 		}
3057c478bd9Sstevel@tonic-gate 	}
3067c478bd9Sstevel@tonic-gate 	ANON_LOCK_ENTER(&sp->shm_amp->a_rwlock, RW_WRITER);
3077c478bd9Sstevel@tonic-gate 	size = sp->shm_amp->size;
3087c478bd9Sstevel@tonic-gate 	ANON_LOCK_EXIT(&sp->shm_amp->a_rwlock);
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	/* somewhere to record spt info for final detach */
3117c478bd9Sstevel@tonic-gate 	if (sp->shm_sptinfo == NULL)
3127c478bd9Sstevel@tonic-gate 		sp->shm_sptinfo = kmem_zalloc(sizeof (sptinfo_t), KM_SLEEP);
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	as_rangelock(as);
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	if (useISM) {
3177c478bd9Sstevel@tonic-gate 		/*
3187c478bd9Sstevel@tonic-gate 		 * Handle ISM
3197c478bd9Sstevel@tonic-gate 		 */
3207c478bd9Sstevel@tonic-gate 		uint_t	n, share_szc;
3217c478bd9Sstevel@tonic-gate 		size_t	share_size;
3227c478bd9Sstevel@tonic-gate 		struct	shm_data ssd;
3237c478bd9Sstevel@tonic-gate 		uintptr_t align_hint;
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 		n = page_num_pagesizes();
3267c478bd9Sstevel@tonic-gate 		if (n < 2) { /* large pages aren't supported */
3277c478bd9Sstevel@tonic-gate 			as_rangeunlock(as);
3287c478bd9Sstevel@tonic-gate 			error = EINVAL;
3297c478bd9Sstevel@tonic-gate 			goto errret;
3307c478bd9Sstevel@tonic-gate 		}
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 		/*
3337c478bd9Sstevel@tonic-gate 		 * Pick a share pagesize to use, if (!isspt(sp)).
3347c478bd9Sstevel@tonic-gate 		 * Otherwise use the already chosen page size.
3357c478bd9Sstevel@tonic-gate 		 *
3367c478bd9Sstevel@tonic-gate 		 * For the initial shmat (!isspt(sp)), where sptcreate is
3377c478bd9Sstevel@tonic-gate 		 * called, map_pgsz is called to recommend a [D]ISM pagesize,
3387c478bd9Sstevel@tonic-gate 		 * important for systems which offer more than one potential
3397c478bd9Sstevel@tonic-gate 		 * [D]ISM pagesize.
3407c478bd9Sstevel@tonic-gate 		 * If the shmat is just to attach to an already created
3417c478bd9Sstevel@tonic-gate 		 * [D]ISM segment, then use the previously selected page size.
3427c478bd9Sstevel@tonic-gate 		 */
3437c478bd9Sstevel@tonic-gate 		if (!isspt(sp)) {
3447c478bd9Sstevel@tonic-gate 			share_size = map_pgsz(MAPPGSZ_ISM,
3457c478bd9Sstevel@tonic-gate 			    pp, addr, size, NULL);
3467c478bd9Sstevel@tonic-gate 			if (share_size == 0) {
3477c478bd9Sstevel@tonic-gate 				as_rangeunlock(as);
3487c478bd9Sstevel@tonic-gate 				error = EINVAL;
3497c478bd9Sstevel@tonic-gate 				goto errret;
3507c478bd9Sstevel@tonic-gate 			}
3517c478bd9Sstevel@tonic-gate 			share_szc = page_szc(share_size);
3527c478bd9Sstevel@tonic-gate 		} else {
3537c478bd9Sstevel@tonic-gate 			share_szc = sp->shm_sptseg->s_szc;
3547c478bd9Sstevel@tonic-gate 			share_size = page_get_pagesize(share_szc);
3557c478bd9Sstevel@tonic-gate 		}
3567c478bd9Sstevel@tonic-gate 		size = P2ROUNDUP(size, share_size);
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 		align_hint = share_size;
3597c478bd9Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
3607c478bd9Sstevel@tonic-gate 		/*
3617c478bd9Sstevel@tonic-gate 		 * For 64 bit amd64, we want to share an entire page table
3627c478bd9Sstevel@tonic-gate 		 * if possible. We know (ugh) that there are 512 entries in
3637c478bd9Sstevel@tonic-gate 		 * in a page table. The number for 32 bit non-PAE should be
3647c478bd9Sstevel@tonic-gate 		 * 1024, but I'm not going to special case that. Note using 512
3657c478bd9Sstevel@tonic-gate 		 * won't cause a failure below. It retries with align_hint set
3667c478bd9Sstevel@tonic-gate 		 * to share_size
3677c478bd9Sstevel@tonic-gate 		 */
3687c478bd9Sstevel@tonic-gate 		while (size >= 512 * (uint64_t)align_hint)
3697c478bd9Sstevel@tonic-gate 			align_hint *= 512;
3707c478bd9Sstevel@tonic-gate #endif /* __i386 || __amd64 */
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate #if defined(__sparcv9)
3737c478bd9Sstevel@tonic-gate 		if (addr == 0 && curproc->p_model == DATAMODEL_LP64) {
3747c478bd9Sstevel@tonic-gate 			/*
3757c478bd9Sstevel@tonic-gate 			 * If no address has been passed in, and this is a
3767c478bd9Sstevel@tonic-gate 			 * 64-bit process, we'll try to find an address
3777c478bd9Sstevel@tonic-gate 			 * in the predict-ISM zone.
3787c478bd9Sstevel@tonic-gate 			 */
3797c478bd9Sstevel@tonic-gate 			caddr_t predbase = (caddr_t)PREDISM_1T_BASE;
3807c478bd9Sstevel@tonic-gate 			size_t len = PREDISM_BOUND - PREDISM_1T_BASE;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 			as_purge(as);
3837c478bd9Sstevel@tonic-gate 			if (as_gap(as, size + share_size, &predbase, &len,
3847c478bd9Sstevel@tonic-gate 			    AH_LO, (caddr_t)NULL) != -1) {
3857c478bd9Sstevel@tonic-gate 				/*
3867c478bd9Sstevel@tonic-gate 				 * We found an address which looks like a
3877c478bd9Sstevel@tonic-gate 				 * candidate.  We want to round it up, and
3887c478bd9Sstevel@tonic-gate 				 * then check that it's a valid user range.
3897c478bd9Sstevel@tonic-gate 				 * This assures that we won't fail below.
3907c478bd9Sstevel@tonic-gate 				 */
3917c478bd9Sstevel@tonic-gate 				addr = (caddr_t)P2ROUNDUP((uintptr_t)predbase,
3927c478bd9Sstevel@tonic-gate 				    share_size);
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 				if (valid_usr_range(addr, size, prot,
3957c478bd9Sstevel@tonic-gate 				    as, as->a_userlimit) != RANGE_OKAY) {
3967c478bd9Sstevel@tonic-gate 					addr = 0;
3977c478bd9Sstevel@tonic-gate 				}
3987c478bd9Sstevel@tonic-gate 			}
3997c478bd9Sstevel@tonic-gate 		}
4007c478bd9Sstevel@tonic-gate #endif /* __sparcv9 */
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 		if (addr == 0) {
4037c478bd9Sstevel@tonic-gate 			for (;;) {
4047c478bd9Sstevel@tonic-gate 				addr = (caddr_t)align_hint;
4057c478bd9Sstevel@tonic-gate 				map_addr(&addr, size, 0ll, 1, MAP_ALIGN);
4067c478bd9Sstevel@tonic-gate 				if (addr != NULL || align_hint == share_size)
4077c478bd9Sstevel@tonic-gate 					break;
4087c478bd9Sstevel@tonic-gate 				align_hint = share_size;
4097c478bd9Sstevel@tonic-gate 			}
4107c478bd9Sstevel@tonic-gate 			if (addr == NULL) {
4117c478bd9Sstevel@tonic-gate 				as_rangeunlock(as);
4127c478bd9Sstevel@tonic-gate 				error = ENOMEM;
4137c478bd9Sstevel@tonic-gate 				goto errret;
4147c478bd9Sstevel@tonic-gate 			}
4157c478bd9Sstevel@tonic-gate 			ASSERT(((uintptr_t)addr & (align_hint - 1)) == 0);
4167c478bd9Sstevel@tonic-gate 		} else {
4177c478bd9Sstevel@tonic-gate 			/* Use the user-supplied attach address */
4187c478bd9Sstevel@tonic-gate 			caddr_t base;
4197c478bd9Sstevel@tonic-gate 			size_t len;
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 			/*
4227c478bd9Sstevel@tonic-gate 			 * Check that the address range
4237c478bd9Sstevel@tonic-gate 			 *  1) is properly aligned
4247c478bd9Sstevel@tonic-gate 			 *  2) is correct in unix terms
4257c478bd9Sstevel@tonic-gate 			 *  3) is within an unmapped address segment
4267c478bd9Sstevel@tonic-gate 			 */
4277c478bd9Sstevel@tonic-gate 			base = addr;
4287c478bd9Sstevel@tonic-gate 			len = size;		/* use spt aligned size */
4297c478bd9Sstevel@tonic-gate 			/* XXX - in SunOS, is sp->shm_segsz */
4307c478bd9Sstevel@tonic-gate 			if ((uintptr_t)base & (share_size - 1)) {
4317c478bd9Sstevel@tonic-gate 				error = EINVAL;
4327c478bd9Sstevel@tonic-gate 				as_rangeunlock(as);
4337c478bd9Sstevel@tonic-gate 				goto errret;
4347c478bd9Sstevel@tonic-gate 			}
4357c478bd9Sstevel@tonic-gate 			result = valid_usr_range(base, len, prot, as,
4367c478bd9Sstevel@tonic-gate 			    as->a_userlimit);
4377c478bd9Sstevel@tonic-gate 			if (result == RANGE_BADPROT) {
4387c478bd9Sstevel@tonic-gate 				/*
4397c478bd9Sstevel@tonic-gate 				 * We try to accomodate processors which
4407c478bd9Sstevel@tonic-gate 				 * may not support execute permissions on
4417c478bd9Sstevel@tonic-gate 				 * all ISM segments by trying the check
4427c478bd9Sstevel@tonic-gate 				 * again but without PROT_EXEC.
4437c478bd9Sstevel@tonic-gate 				 */
4447c478bd9Sstevel@tonic-gate 				prot &= ~PROT_EXEC;
4457c478bd9Sstevel@tonic-gate 				result = valid_usr_range(base, len, prot, as,
4467c478bd9Sstevel@tonic-gate 				    as->a_userlimit);
4477c478bd9Sstevel@tonic-gate 			}
4487c478bd9Sstevel@tonic-gate 			as_purge(as);
4497c478bd9Sstevel@tonic-gate 			if (result != RANGE_OKAY ||
4507c478bd9Sstevel@tonic-gate 			    as_gap(as, len, &base, &len, AH_LO,
4517c478bd9Sstevel@tonic-gate 			    (caddr_t)NULL) != 0) {
4527c478bd9Sstevel@tonic-gate 				error = EINVAL;
4537c478bd9Sstevel@tonic-gate 				as_rangeunlock(as);
4547c478bd9Sstevel@tonic-gate 				goto errret;
4557c478bd9Sstevel@tonic-gate 			}
4567c478bd9Sstevel@tonic-gate 		}
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 		if (!isspt(sp)) {
4597c478bd9Sstevel@tonic-gate 			error = sptcreate(size, &segspt, sp->shm_amp, prot,
4607c478bd9Sstevel@tonic-gate 			    flags, share_szc);
4617c478bd9Sstevel@tonic-gate 			if (error) {
4627c478bd9Sstevel@tonic-gate 				as_rangeunlock(as);
4637c478bd9Sstevel@tonic-gate 				goto errret;
4647c478bd9Sstevel@tonic-gate 			}
4657c478bd9Sstevel@tonic-gate 			sp->shm_sptinfo->sptas = segspt->s_as;
4667c478bd9Sstevel@tonic-gate 			sp->shm_sptseg = segspt;
4677c478bd9Sstevel@tonic-gate 			sp->shm_sptprot = prot;
4687c478bd9Sstevel@tonic-gate 		} else if ((prot & sp->shm_sptprot) != sp->shm_sptprot) {
4697c478bd9Sstevel@tonic-gate 			/*
4707c478bd9Sstevel@tonic-gate 			 * Ensure we're attaching to an ISM segment with
4717c478bd9Sstevel@tonic-gate 			 * fewer or equal permissions than what we're
4727c478bd9Sstevel@tonic-gate 			 * allowed.  Fail if the segment has more
4737c478bd9Sstevel@tonic-gate 			 * permissions than what we're allowed.
4747c478bd9Sstevel@tonic-gate 			 */
4757c478bd9Sstevel@tonic-gate 			error = EACCES;
4767c478bd9Sstevel@tonic-gate 			as_rangeunlock(as);
4777c478bd9Sstevel@tonic-gate 			goto errret;
4787c478bd9Sstevel@tonic-gate 		}
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 		ssd.shm_sptseg = sp->shm_sptseg;
4817c478bd9Sstevel@tonic-gate 		ssd.shm_sptas = sp->shm_sptinfo->sptas;
4827c478bd9Sstevel@tonic-gate 		ssd.shm_amp = sp->shm_amp;
4837c478bd9Sstevel@tonic-gate 		error = as_map(as, addr, size, segspt_shmattach, &ssd);
4847c478bd9Sstevel@tonic-gate 		if (error == 0)
4857c478bd9Sstevel@tonic-gate 			sp->shm_ismattch++; /* keep count of ISM attaches */
4867c478bd9Sstevel@tonic-gate 	} else {
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 		/*
4897c478bd9Sstevel@tonic-gate 		 * Normal case.
4907c478bd9Sstevel@tonic-gate 		 */
4917c478bd9Sstevel@tonic-gate 		if (flags & SHM_RDONLY)
4927c478bd9Sstevel@tonic-gate 			prot &= ~PROT_WRITE;
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 		if (addr == 0) {
4957c478bd9Sstevel@tonic-gate 			/* Let the system pick the attach address */
4967c478bd9Sstevel@tonic-gate 			map_addr(&addr, size, 0ll, 1, 0);
4977c478bd9Sstevel@tonic-gate 			if (addr == NULL) {
4987c478bd9Sstevel@tonic-gate 				as_rangeunlock(as);
4997c478bd9Sstevel@tonic-gate 				error = ENOMEM;
5007c478bd9Sstevel@tonic-gate 				goto errret;
5017c478bd9Sstevel@tonic-gate 			}
5027c478bd9Sstevel@tonic-gate 		} else {
5037c478bd9Sstevel@tonic-gate 			/* Use the user-supplied attach address */
5047c478bd9Sstevel@tonic-gate 			caddr_t base;
5057c478bd9Sstevel@tonic-gate 			size_t len;
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 			if (flags & SHM_RND)
5087c478bd9Sstevel@tonic-gate 				addr = (caddr_t)((uintptr_t)addr &
5097c478bd9Sstevel@tonic-gate 				    ~(SHMLBA - 1));
5107c478bd9Sstevel@tonic-gate 			/*
5117c478bd9Sstevel@tonic-gate 			 * Check that the address range
5127c478bd9Sstevel@tonic-gate 			 *  1) is properly aligned
5137c478bd9Sstevel@tonic-gate 			 *  2) is correct in unix terms
5147c478bd9Sstevel@tonic-gate 			 *  3) is within an unmapped address segment
5157c478bd9Sstevel@tonic-gate 			 */
5167c478bd9Sstevel@tonic-gate 			base = addr;
5177c478bd9Sstevel@tonic-gate 			len = size;		/* use aligned size */
5187c478bd9Sstevel@tonic-gate 			/* XXX - in SunOS, is sp->shm_segsz */
5197c478bd9Sstevel@tonic-gate 			if ((uintptr_t)base & PAGEOFFSET) {
5207c478bd9Sstevel@tonic-gate 				error = EINVAL;
5217c478bd9Sstevel@tonic-gate 				as_rangeunlock(as);
5227c478bd9Sstevel@tonic-gate 				goto errret;
5237c478bd9Sstevel@tonic-gate 			}
5247c478bd9Sstevel@tonic-gate 			result = valid_usr_range(base, len, prot, as,
5257c478bd9Sstevel@tonic-gate 			    as->a_userlimit);
5267c478bd9Sstevel@tonic-gate 			if (result == RANGE_BADPROT) {
5277c478bd9Sstevel@tonic-gate 				prot &= ~PROT_EXEC;
5287c478bd9Sstevel@tonic-gate 				result = valid_usr_range(base, len, prot, as,
5297c478bd9Sstevel@tonic-gate 				    as->a_userlimit);
5307c478bd9Sstevel@tonic-gate 			}
5317c478bd9Sstevel@tonic-gate 			as_purge(as);
5327c478bd9Sstevel@tonic-gate 			if (result != RANGE_OKAY ||
5337c478bd9Sstevel@tonic-gate 			    as_gap(as, len, &base, &len,
5347c478bd9Sstevel@tonic-gate 			    AH_LO, (caddr_t)NULL) != 0) {
5357c478bd9Sstevel@tonic-gate 				error = EINVAL;
5367c478bd9Sstevel@tonic-gate 				as_rangeunlock(as);
5377c478bd9Sstevel@tonic-gate 				goto errret;
5387c478bd9Sstevel@tonic-gate 			}
5397c478bd9Sstevel@tonic-gate 		}
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 		/* Initialize the create arguments and map the segment */
5427c478bd9Sstevel@tonic-gate 		crargs = *(struct segvn_crargs *)zfod_argsp;
5437c478bd9Sstevel@tonic-gate 		crargs.offset = 0;
5447c478bd9Sstevel@tonic-gate 		crargs.type = MAP_SHARED;
5457c478bd9Sstevel@tonic-gate 		crargs.amp = sp->shm_amp;
5467c478bd9Sstevel@tonic-gate 		crargs.prot = prot;
5477c478bd9Sstevel@tonic-gate 		crargs.maxprot = crargs.prot;
5487c478bd9Sstevel@tonic-gate 		crargs.flags = 0;
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 		error = as_map(as, addr, size, segvn_create, &crargs);
5517c478bd9Sstevel@tonic-gate 	}
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	as_rangeunlock(as);
5547c478bd9Sstevel@tonic-gate 	if (error)
5557c478bd9Sstevel@tonic-gate 		goto errret;
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	/* record shmem range for the detach */
5587c478bd9Sstevel@tonic-gate 	sa_add(pp, addr, (size_t)size, useISM ? SHMSA_ISM : 0, sp);
5597c478bd9Sstevel@tonic-gate 	*rvp = (uintptr_t)addr;
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 	sp->shm_atime = gethrestime_sec();
5627c478bd9Sstevel@tonic-gate 	sp->shm_lpid = pp->p_pid;
5637c478bd9Sstevel@tonic-gate 	ipc_hold(shm_svc, (kipc_perm_t *)sp);
5647c478bd9Sstevel@tonic-gate errret:
5657c478bd9Sstevel@tonic-gate 	mutex_exit(lock);
5667c478bd9Sstevel@tonic-gate 	return (error);
5677c478bd9Sstevel@tonic-gate }
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate static void
5707c478bd9Sstevel@tonic-gate shm_dtor(kipc_perm_t *perm)
5717c478bd9Sstevel@tonic-gate {
5727c478bd9Sstevel@tonic-gate 	kshmid_t *sp = (kshmid_t *)perm;
5737c478bd9Sstevel@tonic-gate 	uint_t cnt;
574824c205fSml93401 	size_t rsize;
5757c478bd9Sstevel@tonic-gate 
576*c6939658Ssl108498 	if (sp->shm_lkcnt > 0) {
577*c6939658Ssl108498 		shmem_unlock(sp, sp->shm_amp);
578*c6939658Ssl108498 		sp->shm_lkcnt = 0;
579*c6939658Ssl108498 	}
580*c6939658Ssl108498 
5817c478bd9Sstevel@tonic-gate 	if (sp->shm_sptinfo) {
5827c478bd9Sstevel@tonic-gate 		if (isspt(sp))
5837c478bd9Sstevel@tonic-gate 			sptdestroy(sp->shm_sptinfo->sptas, sp->shm_amp);
5847c478bd9Sstevel@tonic-gate 		kmem_free(sp->shm_sptinfo, sizeof (sptinfo_t));
5857c478bd9Sstevel@tonic-gate 	}
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	ANON_LOCK_ENTER(&sp->shm_amp->a_rwlock, RW_WRITER);
5887c478bd9Sstevel@tonic-gate 	cnt = --sp->shm_amp->refcnt;
5897c478bd9Sstevel@tonic-gate 	ANON_LOCK_EXIT(&sp->shm_amp->a_rwlock);
5907c478bd9Sstevel@tonic-gate 	ASSERT(cnt == 0);
591*c6939658Ssl108498 	shm_rm_amp(sp->shm_amp);
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	if (sp->shm_perm.ipc_id != IPC_ID_INVAL) {
594824c205fSml93401 		rsize = ptob(btopr(sp->shm_segsz));
5957c478bd9Sstevel@tonic-gate 		ipcs_lock(shm_svc);
596824c205fSml93401 		sp->shm_perm.ipc_proj->kpj_data.kpd_shmmax -= rsize;
597824c205fSml93401 		sp->shm_perm.ipc_zone->zone_shmmax -= rsize;
5987c478bd9Sstevel@tonic-gate 		ipcs_unlock(shm_svc);
5997c478bd9Sstevel@tonic-gate 	}
6007c478bd9Sstevel@tonic-gate }
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate /* ARGSUSED */
6037c478bd9Sstevel@tonic-gate static void
6047c478bd9Sstevel@tonic-gate shm_rmid(kipc_perm_t *perm)
6057c478bd9Sstevel@tonic-gate {
6067c478bd9Sstevel@tonic-gate 	/* nothing to do */
6077c478bd9Sstevel@tonic-gate }
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate /*
6107c478bd9Sstevel@tonic-gate  * Shmctl system call.
6117c478bd9Sstevel@tonic-gate  */
6127c478bd9Sstevel@tonic-gate /* ARGSUSED */
6137c478bd9Sstevel@tonic-gate static int
6147c478bd9Sstevel@tonic-gate shmctl(int shmid, int cmd, void *arg)
6157c478bd9Sstevel@tonic-gate {
6167c478bd9Sstevel@tonic-gate 	kshmid_t		*sp;	/* shared memory header ptr */
6177c478bd9Sstevel@tonic-gate 	STRUCT_DECL(shmid_ds, ds);	/* for SVR4 IPC_SET */
6187c478bd9Sstevel@tonic-gate 	int			error = 0;
6197c478bd9Sstevel@tonic-gate 	struct cred 		*cr = CRED();
6207c478bd9Sstevel@tonic-gate 	kmutex_t		*lock;
6217c478bd9Sstevel@tonic-gate 	model_t			mdl = get_udatamodel();
6227c478bd9Sstevel@tonic-gate 	struct shmid_ds64	ds64;
6237c478bd9Sstevel@tonic-gate 	shmatt_t		nattch;
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	STRUCT_INIT(ds, mdl);
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	/*
6287c478bd9Sstevel@tonic-gate 	 * Perform pre- or non-lookup actions (e.g. copyins, RMID).
6297c478bd9Sstevel@tonic-gate 	 */
6307c478bd9Sstevel@tonic-gate 	switch (cmd) {
6317c478bd9Sstevel@tonic-gate 	case IPC_SET:
6327c478bd9Sstevel@tonic-gate 		if (copyin(arg, STRUCT_BUF(ds), STRUCT_SIZE(ds)))
6337c478bd9Sstevel@tonic-gate 			return (EFAULT);
6347c478bd9Sstevel@tonic-gate 		break;
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	case IPC_SET64:
6377c478bd9Sstevel@tonic-gate 		if (copyin(arg, &ds64, sizeof (struct shmid_ds64)))
6387c478bd9Sstevel@tonic-gate 			return (EFAULT);
6397c478bd9Sstevel@tonic-gate 		break;
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	case IPC_RMID:
6427c478bd9Sstevel@tonic-gate 		return (ipc_rmid(shm_svc, shmid, cr));
6437c478bd9Sstevel@tonic-gate 	}
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 	if ((lock = ipc_lookup(shm_svc, shmid, (kipc_perm_t **)&sp)) == NULL)
6467c478bd9Sstevel@tonic-gate 		return (EINVAL);
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	switch (cmd) {
6497c478bd9Sstevel@tonic-gate 	/* Set ownership and permissions. */
6507c478bd9Sstevel@tonic-gate 	case IPC_SET:
6517c478bd9Sstevel@tonic-gate 		if (error = ipcperm_set(shm_svc, cr, &sp->shm_perm,
6527c478bd9Sstevel@tonic-gate 		    &STRUCT_BUF(ds)->shm_perm, mdl))
6537c478bd9Sstevel@tonic-gate 				break;
6547c478bd9Sstevel@tonic-gate 		sp->shm_ctime = gethrestime_sec();
6557c478bd9Sstevel@tonic-gate 		break;
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	case IPC_STAT:
6587c478bd9Sstevel@tonic-gate 		if (error = ipcperm_access(&sp->shm_perm, SHM_R, cr))
6597c478bd9Sstevel@tonic-gate 			break;
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 		nattch = sp->shm_perm.ipc_ref - 1;
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 		ipcperm_stat(&STRUCT_BUF(ds)->shm_perm, &sp->shm_perm, mdl);
6647c478bd9Sstevel@tonic-gate 		STRUCT_FSET(ds, shm_segsz, sp->shm_segsz);
6657c478bd9Sstevel@tonic-gate 		STRUCT_FSETP(ds, shm_amp, NULL);	/* kernel addr */
6667c478bd9Sstevel@tonic-gate 		STRUCT_FSET(ds, shm_lkcnt, sp->shm_lkcnt);
6677c478bd9Sstevel@tonic-gate 		STRUCT_FSET(ds, shm_lpid, sp->shm_lpid);
6687c478bd9Sstevel@tonic-gate 		STRUCT_FSET(ds, shm_cpid, sp->shm_cpid);
6697c478bd9Sstevel@tonic-gate 		STRUCT_FSET(ds, shm_nattch, nattch);
6707c478bd9Sstevel@tonic-gate 		STRUCT_FSET(ds, shm_cnattch, sp->shm_ismattch);
6717c478bd9Sstevel@tonic-gate 		STRUCT_FSET(ds, shm_atime, sp->shm_atime);
6727c478bd9Sstevel@tonic-gate 		STRUCT_FSET(ds, shm_dtime, sp->shm_dtime);
6737c478bd9Sstevel@tonic-gate 		STRUCT_FSET(ds, shm_ctime, sp->shm_ctime);
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 		mutex_exit(lock);
6767c478bd9Sstevel@tonic-gate 		if (copyout(STRUCT_BUF(ds), arg, STRUCT_SIZE(ds)))
6777c478bd9Sstevel@tonic-gate 			return (EFAULT);
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 		return (0);
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	case IPC_SET64:
6827c478bd9Sstevel@tonic-gate 		if (error = ipcperm_set64(shm_svc, cr,
6837c478bd9Sstevel@tonic-gate 		    &sp->shm_perm, &ds64.shmx_perm))
6847c478bd9Sstevel@tonic-gate 			break;
6857c478bd9Sstevel@tonic-gate 		sp->shm_ctime = gethrestime_sec();
6867c478bd9Sstevel@tonic-gate 		break;
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	case IPC_STAT64:
6897c478bd9Sstevel@tonic-gate 		nattch = sp->shm_perm.ipc_ref - 1;
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 		ipcperm_stat64(&ds64.shmx_perm, &sp->shm_perm);
6927c478bd9Sstevel@tonic-gate 		ds64.shmx_segsz = sp->shm_segsz;
6937c478bd9Sstevel@tonic-gate 		ds64.shmx_lkcnt = sp->shm_lkcnt;
6947c478bd9Sstevel@tonic-gate 		ds64.shmx_lpid = sp->shm_lpid;
6957c478bd9Sstevel@tonic-gate 		ds64.shmx_cpid = sp->shm_cpid;
6967c478bd9Sstevel@tonic-gate 		ds64.shmx_nattch = nattch;
6977c478bd9Sstevel@tonic-gate 		ds64.shmx_cnattch = sp->shm_ismattch;
6987c478bd9Sstevel@tonic-gate 		ds64.shmx_atime = sp->shm_atime;
6997c478bd9Sstevel@tonic-gate 		ds64.shmx_dtime = sp->shm_dtime;
7007c478bd9Sstevel@tonic-gate 		ds64.shmx_ctime = sp->shm_ctime;
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 		mutex_exit(lock);
7037c478bd9Sstevel@tonic-gate 		if (copyout(&ds64, arg, sizeof (struct shmid_ds64)))
7047c478bd9Sstevel@tonic-gate 			return (EFAULT);
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 		return (0);
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 	/* Lock segment in memory */
7097c478bd9Sstevel@tonic-gate 	case SHM_LOCK:
7107c478bd9Sstevel@tonic-gate 		if ((error = secpolicy_lock_memory(cr)) != 0)
7117c478bd9Sstevel@tonic-gate 			break;
7127c478bd9Sstevel@tonic-gate 
713*c6939658Ssl108498 		/* protect against overflow */
714*c6939658Ssl108498 		if (sp->shm_lkcnt >= USHRT_MAX) {
715*c6939658Ssl108498 			error = ENOMEM;
716*c6939658Ssl108498 			break;
717*c6939658Ssl108498 		}
7187c478bd9Sstevel@tonic-gate 		if (!isspt(sp) && (sp->shm_lkcnt++ == 0)) {
719*c6939658Ssl108498 			if (error = shmem_lock(sp, sp->shm_amp)) {
7207c478bd9Sstevel@tonic-gate 			    ANON_LOCK_ENTER(&sp->shm_amp->a_rwlock, RW_WRITER);
7217c478bd9Sstevel@tonic-gate 			    cmn_err(CE_NOTE,
7227c478bd9Sstevel@tonic-gate 				"shmctl - couldn't lock %ld pages into memory",
7237c478bd9Sstevel@tonic-gate 				sp->shm_amp->size);
7247c478bd9Sstevel@tonic-gate 			    ANON_LOCK_EXIT(&sp->shm_amp->a_rwlock);
7257c478bd9Sstevel@tonic-gate 			    error = ENOMEM;
7267c478bd9Sstevel@tonic-gate 			    sp->shm_lkcnt--;
7277c478bd9Sstevel@tonic-gate 			}
7287c478bd9Sstevel@tonic-gate 		}
7297c478bd9Sstevel@tonic-gate 		break;
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	/* Unlock segment */
7327c478bd9Sstevel@tonic-gate 	case SHM_UNLOCK:
7337c478bd9Sstevel@tonic-gate 		if ((error = secpolicy_lock_memory(cr)) != 0)
7347c478bd9Sstevel@tonic-gate 			break;
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 		if (sp->shm_lkcnt && (--sp->shm_lkcnt == 0)) {
737*c6939658Ssl108498 			shmem_unlock(sp, sp->shm_amp);
7387c478bd9Sstevel@tonic-gate 		}
7397c478bd9Sstevel@tonic-gate 		break;
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	default:
7427c478bd9Sstevel@tonic-gate 		error = EINVAL;
7437c478bd9Sstevel@tonic-gate 		break;
7447c478bd9Sstevel@tonic-gate 	}
7457c478bd9Sstevel@tonic-gate 	mutex_exit(lock);
7467c478bd9Sstevel@tonic-gate 	return (error);
7477c478bd9Sstevel@tonic-gate }
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate static void
7507c478bd9Sstevel@tonic-gate shm_detach(proc_t *pp, segacct_t *sap)
7517c478bd9Sstevel@tonic-gate {
7527c478bd9Sstevel@tonic-gate 	kshmid_t	*sp = sap->sa_id;
7537c478bd9Sstevel@tonic-gate 	size_t		len = sap->sa_len;
7547c478bd9Sstevel@tonic-gate 	caddr_t		addr = sap->sa_addr;
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	/*
7577c478bd9Sstevel@tonic-gate 	 * Discard lwpchan mappings.
7587c478bd9Sstevel@tonic-gate 	 */
7597c478bd9Sstevel@tonic-gate 	if (pp->p_lcp != NULL)
7607c478bd9Sstevel@tonic-gate 		lwpchan_delete_mapping(pp, addr, addr + len);
7617c478bd9Sstevel@tonic-gate 	(void) as_unmap(pp->p_as, addr, len);
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	/*
7647c478bd9Sstevel@tonic-gate 	 * Perform some detach-time accounting.
7657c478bd9Sstevel@tonic-gate 	 */
7667c478bd9Sstevel@tonic-gate 	(void) ipc_lock(shm_svc, sp->shm_perm.ipc_id);
7677c478bd9Sstevel@tonic-gate 	if (sap->sa_flags & SHMSA_ISM)
7687c478bd9Sstevel@tonic-gate 		sp->shm_ismattch--;
7697c478bd9Sstevel@tonic-gate 	sp->shm_dtime = gethrestime_sec();
7707c478bd9Sstevel@tonic-gate 	sp->shm_lpid = pp->p_pid;
7717c478bd9Sstevel@tonic-gate 	ipc_rele(shm_svc, (kipc_perm_t *)sp);	/* Drops lock */
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	kmem_free(sap, sizeof (segacct_t));
7747c478bd9Sstevel@tonic-gate }
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate static int
7777c478bd9Sstevel@tonic-gate shmdt(caddr_t addr)
7787c478bd9Sstevel@tonic-gate {
7797c478bd9Sstevel@tonic-gate 	proc_t *pp = curproc;
7807c478bd9Sstevel@tonic-gate 	segacct_t *sap, template;
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
7837c478bd9Sstevel@tonic-gate 	prbarrier(pp);			/* block /proc.  See shmgetid(). */
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 	template.sa_addr = addr;
7867c478bd9Sstevel@tonic-gate 	template.sa_len = 0;
7877c478bd9Sstevel@tonic-gate 	if ((pp->p_segacct == NULL) ||
7887c478bd9Sstevel@tonic-gate 	    ((sap = avl_find(pp->p_segacct, &template, NULL)) == NULL)) {
7897c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
7907c478bd9Sstevel@tonic-gate 		return (EINVAL);
7917c478bd9Sstevel@tonic-gate 	}
79207b65a64Saguzovsk 	if (sap->sa_addr != addr) {
79307b65a64Saguzovsk 		mutex_exit(&pp->p_lock);
79407b65a64Saguzovsk 		return (EINVAL);
79507b65a64Saguzovsk 	}
7967c478bd9Sstevel@tonic-gate 	avl_remove(pp->p_segacct, sap);
7977c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	shm_detach(pp, sap);
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 	return (0);
8027c478bd9Sstevel@tonic-gate }
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate /*
8057c478bd9Sstevel@tonic-gate  * Remove all shared memory segments associated with a given zone.
8067c478bd9Sstevel@tonic-gate  * Called by zone_shutdown when the zone is halted.
8077c478bd9Sstevel@tonic-gate  */
8087c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
8097c478bd9Sstevel@tonic-gate static void
8107c478bd9Sstevel@tonic-gate shm_remove_zone(zoneid_t zoneid, void *arg)
8117c478bd9Sstevel@tonic-gate {
8127c478bd9Sstevel@tonic-gate 	ipc_remove_zone(shm_svc, zoneid);
8137c478bd9Sstevel@tonic-gate }
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate /*
8167c478bd9Sstevel@tonic-gate  * Shmget (create new shmem) system call.
8177c478bd9Sstevel@tonic-gate  */
8187c478bd9Sstevel@tonic-gate static int
8197c478bd9Sstevel@tonic-gate shmget(key_t key, size_t size, int shmflg, uintptr_t *rvp)
8207c478bd9Sstevel@tonic-gate {
8217c478bd9Sstevel@tonic-gate 	proc_t		*pp = curproc;
8227c478bd9Sstevel@tonic-gate 	kshmid_t	*sp;
8237c478bd9Sstevel@tonic-gate 	kmutex_t	*lock;
8247c478bd9Sstevel@tonic-gate 	int		error;
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate top:
8277c478bd9Sstevel@tonic-gate 	if (error = ipc_get(shm_svc, key, shmflg, (kipc_perm_t **)&sp, &lock))
8287c478bd9Sstevel@tonic-gate 		return (error);
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 	if (!IPC_FREE(&sp->shm_perm)) {
8317c478bd9Sstevel@tonic-gate 		/*
8327c478bd9Sstevel@tonic-gate 		 * A segment with the requested key exists.
8337c478bd9Sstevel@tonic-gate 		 */
8347c478bd9Sstevel@tonic-gate 		if (size > sp->shm_segsz) {
8357c478bd9Sstevel@tonic-gate 			mutex_exit(lock);
8367c478bd9Sstevel@tonic-gate 			return (EINVAL);
8377c478bd9Sstevel@tonic-gate 		}
8387c478bd9Sstevel@tonic-gate 	} else {
8397c478bd9Sstevel@tonic-gate 		/*
8407c478bd9Sstevel@tonic-gate 		 * A new segment should be created.
8417c478bd9Sstevel@tonic-gate 		 */
8427c478bd9Sstevel@tonic-gate 		size_t npages = btopr(size);
8437c478bd9Sstevel@tonic-gate 		size_t rsize = ptob(npages);
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 		/*
846824c205fSml93401 		 * Check rsize and the per-project and per-zone limit on
847824c205fSml93401 		 * shared memory.  Checking rsize handles both the size == 0
8487c478bd9Sstevel@tonic-gate 		 * case and the size < ULONG_MAX & PAGEMASK case (i.e.
8497c478bd9Sstevel@tonic-gate 		 * rounding up wraps a size_t).
8507c478bd9Sstevel@tonic-gate 		 */
851824c205fSml93401 		if (rsize == 0 ||
852824c205fSml93401 		    (rctl_test(rc_project_shmmax,
8537c478bd9Sstevel@tonic-gate 		    pp->p_task->tk_proj->kpj_rctls, pp, rsize,
854824c205fSml93401 		    RCA_SAFE) & RCT_DENY) ||
855824c205fSml93401 		    (rctl_test(rc_zone_shmmax,
856824c205fSml93401 		    pp->p_zone->zone_rctls, pp, rsize,
8577c478bd9Sstevel@tonic-gate 		    RCA_SAFE) & RCT_DENY)) {
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
8607c478bd9Sstevel@tonic-gate 			mutex_exit(lock);
8617c478bd9Sstevel@tonic-gate 			ipc_cleanup(shm_svc, (kipc_perm_t *)sp);
8627c478bd9Sstevel@tonic-gate 			return (EINVAL);
8637c478bd9Sstevel@tonic-gate 		}
8647c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
8657c478bd9Sstevel@tonic-gate 		mutex_exit(lock);
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 		if (anon_resv(rsize) == 0) {
8687c478bd9Sstevel@tonic-gate 			ipc_cleanup(shm_svc, (kipc_perm_t *)sp);
8697c478bd9Sstevel@tonic-gate 			return (ENOMEM);
8707c478bd9Sstevel@tonic-gate 		}
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 		sp->shm_amp = anonmap_alloc(rsize, rsize);
873*c6939658Ssl108498 		sp->shm_amp->a_sp = sp;
8747c478bd9Sstevel@tonic-gate 		/*
8757c478bd9Sstevel@tonic-gate 		 * Store the original user's requested size, in bytes,
8767c478bd9Sstevel@tonic-gate 		 * rather than the page-aligned size.  The former is
8777c478bd9Sstevel@tonic-gate 		 * used for IPC_STAT and shmget() lookups.  The latter
8787c478bd9Sstevel@tonic-gate 		 * is saved in the anon_map structure and is used for
8797c478bd9Sstevel@tonic-gate 		 * calls to the vm layer.
8807c478bd9Sstevel@tonic-gate 		 */
8817c478bd9Sstevel@tonic-gate 		sp->shm_segsz = size;
8827c478bd9Sstevel@tonic-gate 		sp->shm_atime = sp->shm_dtime = 0;
8837c478bd9Sstevel@tonic-gate 		sp->shm_ctime = gethrestime_sec();
8847c478bd9Sstevel@tonic-gate 		sp->shm_lpid = (pid_t)0;
8857c478bd9Sstevel@tonic-gate 		sp->shm_cpid = curproc->p_pid;
8867c478bd9Sstevel@tonic-gate 		sp->shm_ismattch = 0;
8877c478bd9Sstevel@tonic-gate 		sp->shm_sptinfo = NULL;
8887c478bd9Sstevel@tonic-gate 		/*
8897c478bd9Sstevel@tonic-gate 		 * Check limits one last time, push id into global
8907c478bd9Sstevel@tonic-gate 		 * visibility, and update resource usage counts.
8917c478bd9Sstevel@tonic-gate 		 */
8927c478bd9Sstevel@tonic-gate 		if (error = ipc_commit_begin(shm_svc, key, shmflg,
8937c478bd9Sstevel@tonic-gate 		    (kipc_perm_t *)sp)) {
8947c478bd9Sstevel@tonic-gate 			if (error == EAGAIN)
8957c478bd9Sstevel@tonic-gate 				goto top;
8967c478bd9Sstevel@tonic-gate 			return (error);
8977c478bd9Sstevel@tonic-gate 		}
8987c478bd9Sstevel@tonic-gate 
899824c205fSml93401 		if ((rctl_test(rc_project_shmmax,
9007c478bd9Sstevel@tonic-gate 		    sp->shm_perm.ipc_proj->kpj_rctls, pp, rsize,
901824c205fSml93401 		    RCA_SAFE) & RCT_DENY) ||
902824c205fSml93401 		    (rctl_test(rc_zone_shmmax,
903824c205fSml93401 		    sp->shm_perm.ipc_zone->zone_rctls, pp, rsize,
904824c205fSml93401 		    RCA_SAFE) & RCT_DENY)) {
9057c478bd9Sstevel@tonic-gate 			ipc_cleanup(shm_svc, (kipc_perm_t *)sp);
9067c478bd9Sstevel@tonic-gate 			return (EINVAL);
9077c478bd9Sstevel@tonic-gate 		}
9087c478bd9Sstevel@tonic-gate 		sp->shm_perm.ipc_proj->kpj_data.kpd_shmmax += rsize;
909824c205fSml93401 		sp->shm_perm.ipc_zone->zone_shmmax += rsize;
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 		lock = ipc_commit_end(shm_svc, &sp->shm_perm);
9127c478bd9Sstevel@tonic-gate 	}
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
9157c478bd9Sstevel@tonic-gate 	if (audit_active)
9167c478bd9Sstevel@tonic-gate 		audit_ipcget(AT_IPC_SHM, (void *)sp);
9177c478bd9Sstevel@tonic-gate #endif
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 	*rvp = (uintptr_t)(sp->shm_perm.ipc_id);
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 	mutex_exit(lock);
9227c478bd9Sstevel@tonic-gate 	return (0);
9237c478bd9Sstevel@tonic-gate }
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate /*
9267c478bd9Sstevel@tonic-gate  * shmids system call.
9277c478bd9Sstevel@tonic-gate  */
9287c478bd9Sstevel@tonic-gate static int
9297c478bd9Sstevel@tonic-gate shmids(int *buf, uint_t nids, uint_t *pnids)
9307c478bd9Sstevel@tonic-gate {
9317c478bd9Sstevel@tonic-gate 	return (ipc_ids(shm_svc, buf, nids, pnids));
9327c478bd9Sstevel@tonic-gate }
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate /*
9357c478bd9Sstevel@tonic-gate  * System entry point for shmat, shmctl, shmdt, and shmget system calls.
9367c478bd9Sstevel@tonic-gate  */
9377c478bd9Sstevel@tonic-gate static uintptr_t
9387c478bd9Sstevel@tonic-gate shmsys(int opcode, uintptr_t a0, uintptr_t a1, uintptr_t a2)
9397c478bd9Sstevel@tonic-gate {
9407c478bd9Sstevel@tonic-gate 	int	error;
9417c478bd9Sstevel@tonic-gate 	uintptr_t r_val = 0;
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 	switch (opcode) {
9447c478bd9Sstevel@tonic-gate 	case SHMAT:
9457c478bd9Sstevel@tonic-gate 		error = shmat((int)a0, (caddr_t)a1, (int)a2, &r_val);
9467c478bd9Sstevel@tonic-gate 		break;
9477c478bd9Sstevel@tonic-gate 	case SHMCTL:
9487c478bd9Sstevel@tonic-gate 		error = shmctl((int)a0, (int)a1, (void *)a2);
9497c478bd9Sstevel@tonic-gate 		break;
9507c478bd9Sstevel@tonic-gate 	case SHMDT:
9517c478bd9Sstevel@tonic-gate 		error = shmdt((caddr_t)a0);
9527c478bd9Sstevel@tonic-gate 		break;
9537c478bd9Sstevel@tonic-gate 	case SHMGET:
9547c478bd9Sstevel@tonic-gate 		error = shmget((key_t)a0, (size_t)a1, (int)a2, &r_val);
9557c478bd9Sstevel@tonic-gate 		break;
9567c478bd9Sstevel@tonic-gate 	case SHMIDS:
9577c478bd9Sstevel@tonic-gate 		error = shmids((int *)a0, (uint_t)a1, (uint_t *)a2);
9587c478bd9Sstevel@tonic-gate 		break;
9597c478bd9Sstevel@tonic-gate 	default:
9607c478bd9Sstevel@tonic-gate 		error = EINVAL;
9617c478bd9Sstevel@tonic-gate 		break;
9627c478bd9Sstevel@tonic-gate 	}
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 	if (error)
9657c478bd9Sstevel@tonic-gate 		return ((uintptr_t)set_errno(error));
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 	return (r_val);
9687c478bd9Sstevel@tonic-gate }
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate /*
9717c478bd9Sstevel@tonic-gate  * segacct_t comparator
9727c478bd9Sstevel@tonic-gate  * This works as expected, with one minor change: the first of two real
9737c478bd9Sstevel@tonic-gate  * segments with equal addresses is considered to be 'greater than' the
9747c478bd9Sstevel@tonic-gate  * second.  We only return equal when searching using a template, in
9757c478bd9Sstevel@tonic-gate  * which case we explicitly set the template segment's length to 0
9767c478bd9Sstevel@tonic-gate  * (which is invalid for a real segment).
9777c478bd9Sstevel@tonic-gate  */
9787c478bd9Sstevel@tonic-gate static int
9797c478bd9Sstevel@tonic-gate shm_sacompar(const void *x, const void *y)
9807c478bd9Sstevel@tonic-gate {
9817c478bd9Sstevel@tonic-gate 	segacct_t *sa1 = (segacct_t *)x;
9827c478bd9Sstevel@tonic-gate 	segacct_t *sa2 = (segacct_t *)y;
9837c478bd9Sstevel@tonic-gate 
98407b65a64Saguzovsk 	if (sa1->sa_addr < sa2->sa_addr) {
9857c478bd9Sstevel@tonic-gate 		return (-1);
98607b65a64Saguzovsk 	} else if (sa2->sa_len != 0) {
98707b65a64Saguzovsk 		if (sa1->sa_addr >= sa2->sa_addr + sa2->sa_len) {
9887c478bd9Sstevel@tonic-gate 			return (1);
98907b65a64Saguzovsk 		} else if (sa1->sa_len != 0) {
99007b65a64Saguzovsk 			return (1);
99107b65a64Saguzovsk 		} else {
9927c478bd9Sstevel@tonic-gate 			return (0);
99307b65a64Saguzovsk 		}
99407b65a64Saguzovsk 	} else if (sa1->sa_addr > sa2->sa_addr) {
9957c478bd9Sstevel@tonic-gate 		return (1);
99607b65a64Saguzovsk 	} else {
99707b65a64Saguzovsk 		return (0);
99807b65a64Saguzovsk 	}
9997c478bd9Sstevel@tonic-gate }
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate /*
10027c478bd9Sstevel@tonic-gate  * add this record to the segacct list.
10037c478bd9Sstevel@tonic-gate  */
10047c478bd9Sstevel@tonic-gate static void
10057c478bd9Sstevel@tonic-gate sa_add(struct proc *pp, caddr_t addr, size_t len, ulong_t flags, kshmid_t *id)
10067c478bd9Sstevel@tonic-gate {
10077c478bd9Sstevel@tonic-gate 	segacct_t *nsap;
10087c478bd9Sstevel@tonic-gate 	avl_tree_t *tree = NULL;
10097c478bd9Sstevel@tonic-gate 	avl_index_t where;
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 	nsap = kmem_alloc(sizeof (segacct_t), KM_SLEEP);
10127c478bd9Sstevel@tonic-gate 	nsap->sa_addr = addr;
10137c478bd9Sstevel@tonic-gate 	nsap->sa_len  = len;
10147c478bd9Sstevel@tonic-gate 	nsap->sa_flags = flags;
10157c478bd9Sstevel@tonic-gate 	nsap->sa_id = id;
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 	if (pp->p_segacct == NULL)
10187c478bd9Sstevel@tonic-gate 		tree = kmem_alloc(sizeof (avl_tree_t), KM_SLEEP);
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
10217c478bd9Sstevel@tonic-gate 	prbarrier(pp);			/* block /proc.  See shmgetid(). */
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	if (pp->p_segacct == NULL) {
10247c478bd9Sstevel@tonic-gate 		avl_create(tree, shm_sacompar, sizeof (segacct_t),
10257c478bd9Sstevel@tonic-gate 		    offsetof(segacct_t, sa_tree));
10267c478bd9Sstevel@tonic-gate 		pp->p_segacct = tree;
10277c478bd9Sstevel@tonic-gate 	} else if (tree) {
10287c478bd9Sstevel@tonic-gate 		kmem_free(tree, sizeof (avl_tree_t));
10297c478bd9Sstevel@tonic-gate 	}
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 	/*
10327c478bd9Sstevel@tonic-gate 	 * We can ignore the result of avl_find, as the comparator will
10337c478bd9Sstevel@tonic-gate 	 * never return equal for segments with non-zero length.  This
10347c478bd9Sstevel@tonic-gate 	 * is a necessary hack to get around the fact that we do, in
10357c478bd9Sstevel@tonic-gate 	 * fact, have duplicate keys.
10367c478bd9Sstevel@tonic-gate 	 */
10377c478bd9Sstevel@tonic-gate 	(void) avl_find(pp->p_segacct, nsap, &where);
10387c478bd9Sstevel@tonic-gate 	avl_insert(pp->p_segacct, nsap, where);
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
10417c478bd9Sstevel@tonic-gate }
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate /*
10447c478bd9Sstevel@tonic-gate  * Duplicate parent's segacct records in child.
10457c478bd9Sstevel@tonic-gate  */
10467c478bd9Sstevel@tonic-gate void
10477c478bd9Sstevel@tonic-gate shmfork(struct proc *ppp, struct proc *cpp)
10487c478bd9Sstevel@tonic-gate {
10497c478bd9Sstevel@tonic-gate 	segacct_t *sap;
10507c478bd9Sstevel@tonic-gate 	kshmid_t *sp;
10517c478bd9Sstevel@tonic-gate 	kmutex_t *mp;
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 	ASSERT(ppp->p_segacct != NULL);
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 	/*
10567c478bd9Sstevel@tonic-gate 	 * We are the only lwp running in the parent so nobody can
10577c478bd9Sstevel@tonic-gate 	 * mess with our p_segacct list.  Thus it is safe to traverse
10587c478bd9Sstevel@tonic-gate 	 * the list without holding p_lock.  This is essential because
10597c478bd9Sstevel@tonic-gate 	 * we can't hold p_lock during a KM_SLEEP allocation.
10607c478bd9Sstevel@tonic-gate 	 */
10617c478bd9Sstevel@tonic-gate 	for (sap = (segacct_t *)avl_first(ppp->p_segacct); sap != NULL;
10627c478bd9Sstevel@tonic-gate 	    sap = (segacct_t *)AVL_NEXT(ppp->p_segacct, sap)) {
10637c478bd9Sstevel@tonic-gate 		sa_add(cpp, sap->sa_addr, sap->sa_len, sap->sa_flags,
10647c478bd9Sstevel@tonic-gate 		    sap->sa_id);
10657c478bd9Sstevel@tonic-gate 		sp = sap->sa_id;
10667c478bd9Sstevel@tonic-gate 		mp = ipc_lock(shm_svc, sp->shm_perm.ipc_id);
10677c478bd9Sstevel@tonic-gate 		if (sap->sa_flags & SHMSA_ISM)
10687c478bd9Sstevel@tonic-gate 			sp->shm_ismattch++;
10697c478bd9Sstevel@tonic-gate 		ipc_hold(shm_svc, (kipc_perm_t *)sp);
10707c478bd9Sstevel@tonic-gate 		mutex_exit(mp);
10717c478bd9Sstevel@tonic-gate 	}
10727c478bd9Sstevel@tonic-gate }
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate /*
10757c478bd9Sstevel@tonic-gate  * Detach shared memory segments from exiting process.
10767c478bd9Sstevel@tonic-gate  */
10777c478bd9Sstevel@tonic-gate void
10787c478bd9Sstevel@tonic-gate shmexit(struct proc *pp)
10797c478bd9Sstevel@tonic-gate {
10807c478bd9Sstevel@tonic-gate 	segacct_t *sap;
10817c478bd9Sstevel@tonic-gate 	avl_tree_t *tree;
10827c478bd9Sstevel@tonic-gate 	void *cookie = NULL;
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 	ASSERT(pp->p_segacct != NULL);
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
10877c478bd9Sstevel@tonic-gate 	prbarrier(pp);
10887c478bd9Sstevel@tonic-gate 	tree = pp->p_segacct;
10897c478bd9Sstevel@tonic-gate 	pp->p_segacct = NULL;
10907c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 	while ((sap = avl_destroy_nodes(tree, &cookie)) != NULL)
10937c478bd9Sstevel@tonic-gate 		(void) shm_detach(pp, sap);
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 	avl_destroy(tree);
10967c478bd9Sstevel@tonic-gate 	kmem_free(tree, sizeof (avl_tree_t));
10977c478bd9Sstevel@tonic-gate }
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate /*
11007c478bd9Sstevel@tonic-gate  * At this time pages should be in memory, so just lock them.
11017c478bd9Sstevel@tonic-gate  */
11027c478bd9Sstevel@tonic-gate static void
1103*c6939658Ssl108498 lock_again(size_t npages, kshmid_t *sp, struct anon_map *amp)
11047c478bd9Sstevel@tonic-gate {
11057c478bd9Sstevel@tonic-gate 	struct anon *ap;
11067c478bd9Sstevel@tonic-gate 	struct page *pp;
11077c478bd9Sstevel@tonic-gate 	struct vnode *vp;
1108*c6939658Ssl108498 	u_offset_t off;
11097c478bd9Sstevel@tonic-gate 	ulong_t anon_idx;
11107c478bd9Sstevel@tonic-gate 	anon_sync_obj_t cookie;
11117c478bd9Sstevel@tonic-gate 
1112*c6939658Ssl108498 	mutex_enter(&sp->shm_mlock);
11137c478bd9Sstevel@tonic-gate 	ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
11147c478bd9Sstevel@tonic-gate 	for (anon_idx = 0; npages != 0; anon_idx++, npages--) {
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 		anon_array_enter(amp, anon_idx, &cookie);
11177c478bd9Sstevel@tonic-gate 		ap = anon_get_ptr(amp->ahp, anon_idx);
1118*c6939658Ssl108498 		ASSERT(ap != NULL);
11197c478bd9Sstevel@tonic-gate 		swap_xlate(ap, &vp, &off);
11207c478bd9Sstevel@tonic-gate 		anon_array_exit(&cookie);
11217c478bd9Sstevel@tonic-gate 
1122*c6939658Ssl108498 		pp = page_lookup(vp, off, SE_SHARED);
11237c478bd9Sstevel@tonic-gate 		if (pp == NULL) {
11247c478bd9Sstevel@tonic-gate 			panic("lock_again: page not in the system");
11257c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
11267c478bd9Sstevel@tonic-gate 		}
1127*c6939658Ssl108498 		/* page should already be locked by caller */
1128*c6939658Ssl108498 		ASSERT(pp->p_lckcnt > 0);
11297c478bd9Sstevel@tonic-gate 		(void) page_pp_lock(pp, 0, 0);
11307c478bd9Sstevel@tonic-gate 		page_unlock(pp);
11317c478bd9Sstevel@tonic-gate 	}
11327c478bd9Sstevel@tonic-gate 	ANON_LOCK_EXIT(&amp->a_rwlock);
1133*c6939658Ssl108498 	mutex_exit(&sp->shm_mlock);
11347c478bd9Sstevel@tonic-gate }
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate /*
11377c478bd9Sstevel@tonic-gate  * Attach the shared memory segment to the process
11387c478bd9Sstevel@tonic-gate  * address space and lock the pages.
11397c478bd9Sstevel@tonic-gate  */
11407c478bd9Sstevel@tonic-gate static int
1141*c6939658Ssl108498 shmem_lock(kshmid_t *sp, struct anon_map *amp)
11427c478bd9Sstevel@tonic-gate {
11437c478bd9Sstevel@tonic-gate 	size_t npages = btopr(amp->size);
11447c478bd9Sstevel@tonic-gate 	struct as *as;
11457c478bd9Sstevel@tonic-gate 	struct segvn_crargs crargs;
1146*c6939658Ssl108498 	uint_t error;
11477c478bd9Sstevel@tonic-gate 
1148*c6939658Ssl108498 	/*
1149*c6939658Ssl108498 	 * A later ISM/DISM attach may increase the size of the amp, so
1150*c6939658Ssl108498 	 * cache the number of pages locked for the future shmem_unlock()
1151*c6939658Ssl108498 	 */
1152*c6939658Ssl108498 	sp->shm_lkpages = npages;
11537c478bd9Sstevel@tonic-gate 
1154*c6939658Ssl108498 	as = as_alloc();
11557c478bd9Sstevel@tonic-gate 	/* Initialize the create arguments and map the segment */
11567c478bd9Sstevel@tonic-gate 	crargs = *(struct segvn_crargs *)zfod_argsp;	/* structure copy */
11577c478bd9Sstevel@tonic-gate 	crargs.offset = (u_offset_t)0;
11587c478bd9Sstevel@tonic-gate 	crargs.type = MAP_SHARED;
11597c478bd9Sstevel@tonic-gate 	crargs.amp = amp;
11607c478bd9Sstevel@tonic-gate 	crargs.prot = PROT_ALL;
11617c478bd9Sstevel@tonic-gate 	crargs.maxprot = crargs.prot;
11627c478bd9Sstevel@tonic-gate 	crargs.flags = 0;
1163*c6939658Ssl108498 	error = as_map(as, 0x0, amp->size, segvn_create, &crargs);
11647c478bd9Sstevel@tonic-gate 	if (!error) {
1165*c6939658Ssl108498 		if ((error = as_ctl(as, 0x0, amp->size, MC_LOCK, 0, 0,
11667c478bd9Sstevel@tonic-gate 			NULL, 0)) == 0) {
1167*c6939658Ssl108498 			lock_again(npages, sp, amp);
11687c478bd9Sstevel@tonic-gate 		}
1169*c6939658Ssl108498 		(void) as_unmap(as, 0x0, amp->size);
11707c478bd9Sstevel@tonic-gate 	}
1171*c6939658Ssl108498 	as_free(as);
11727c478bd9Sstevel@tonic-gate 	return (error);
11737c478bd9Sstevel@tonic-gate }
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate /*
11777c478bd9Sstevel@tonic-gate  * Unlock shared memory
11787c478bd9Sstevel@tonic-gate  */
11797c478bd9Sstevel@tonic-gate static void
1180*c6939658Ssl108498 shmem_unlock(kshmid_t *sp, struct anon_map *amp)
11817c478bd9Sstevel@tonic-gate {
11827c478bd9Sstevel@tonic-gate 	struct anon *ap;
1183*c6939658Ssl108498 	pgcnt_t npages = sp->shm_lkpages;
11847c478bd9Sstevel@tonic-gate 	struct vnode *vp;
11857c478bd9Sstevel@tonic-gate 	struct page *pp;
1186*c6939658Ssl108498 	u_offset_t off;
11877c478bd9Sstevel@tonic-gate 	ulong_t anon_idx;
1188*c6939658Ssl108498 	size_t unlocked_bytes = 0;
1189*c6939658Ssl108498 	kproject_t	*proj;
1190*c6939658Ssl108498 	anon_sync_obj_t cookie;
11917c478bd9Sstevel@tonic-gate 
1192*c6939658Ssl108498 	proj = sp->shm_perm.ipc_proj;
1193*c6939658Ssl108498 	mutex_enter(&sp->shm_mlock);
1194*c6939658Ssl108498 	ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
11957c478bd9Sstevel@tonic-gate 	for (anon_idx = 0; anon_idx < npages; anon_idx++) {
11967c478bd9Sstevel@tonic-gate 
1197*c6939658Ssl108498 		anon_array_enter(amp, anon_idx, &cookie);
11987c478bd9Sstevel@tonic-gate 		if ((ap = anon_get_ptr(amp->ahp, anon_idx)) == NULL) {
11997c478bd9Sstevel@tonic-gate 			panic("shmem_unlock: null app");
12007c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
12017c478bd9Sstevel@tonic-gate 		}
12027c478bd9Sstevel@tonic-gate 		swap_xlate(ap, &vp, &off);
1203*c6939658Ssl108498 		anon_array_exit(&cookie);
12047c478bd9Sstevel@tonic-gate 		pp = page_lookup(vp, off, SE_SHARED);
12057c478bd9Sstevel@tonic-gate 		if (pp == NULL) {
12067c478bd9Sstevel@tonic-gate 			panic("shmem_unlock: page not in the system");
12077c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
12087c478bd9Sstevel@tonic-gate 		}
1209*c6939658Ssl108498 		/*
1210*c6939658Ssl108498 		 * Page should at least have once lock from previous
1211*c6939658Ssl108498 		 * shmem_lock
1212*c6939658Ssl108498 		 */
1213*c6939658Ssl108498 		ASSERT(pp->p_lckcnt > 0);
12147c478bd9Sstevel@tonic-gate 		page_pp_unlock(pp, 0, 0);
1215*c6939658Ssl108498 		if (pp->p_lckcnt == 0)
1216*c6939658Ssl108498 			unlocked_bytes += PAGESIZE;
1217*c6939658Ssl108498 
12187c478bd9Sstevel@tonic-gate 		page_unlock(pp);
12197c478bd9Sstevel@tonic-gate 	}
1220*c6939658Ssl108498 
1221*c6939658Ssl108498 	if (unlocked_bytes > 0) {
1222*c6939658Ssl108498 		rctl_decr_locked_mem(NULL, proj, unlocked_bytes, 0);
1223*c6939658Ssl108498 	}
1224*c6939658Ssl108498 
1225*c6939658Ssl108498 	ANON_LOCK_EXIT(&amp->a_rwlock);
1226*c6939658Ssl108498 	mutex_exit(&sp->shm_mlock);
12277c478bd9Sstevel@tonic-gate }
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate /*
12307c478bd9Sstevel@tonic-gate  * We call this routine when we have removed all references to this
12317c478bd9Sstevel@tonic-gate  * amp.  This means all shmdt()s and the IPC_RMID have been done.
12327c478bd9Sstevel@tonic-gate  */
12337c478bd9Sstevel@tonic-gate static void
1234*c6939658Ssl108498 shm_rm_amp(struct anon_map *amp)
12357c478bd9Sstevel@tonic-gate {
12367c478bd9Sstevel@tonic-gate 	/*
12377c478bd9Sstevel@tonic-gate 	 * Free up the anon_map.
12387c478bd9Sstevel@tonic-gate 	 */
12397c478bd9Sstevel@tonic-gate 	lgrp_shm_policy_fini(amp, NULL);
124007b65a64Saguzovsk 	if (amp->a_szc != 0) {
124107b65a64Saguzovsk 		ANON_LOCK_ENTER(&amp->a_rwlock, RW_WRITER);
124207b65a64Saguzovsk 		anon_shmap_free_pages(amp, 0, amp->size);
124307b65a64Saguzovsk 		ANON_LOCK_EXIT(&amp->a_rwlock);
124407b65a64Saguzovsk 	} else {
12457c478bd9Sstevel@tonic-gate 		anon_free(amp->ahp, 0, amp->size);
124607b65a64Saguzovsk 	}
12477c478bd9Sstevel@tonic-gate 	anon_unresv(amp->swresv);
12487c478bd9Sstevel@tonic-gate 	anonmap_free(amp);
12497c478bd9Sstevel@tonic-gate }
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate /*
12527c478bd9Sstevel@tonic-gate  * Return the shared memory id for the process's virtual address.
12537c478bd9Sstevel@tonic-gate  * Return SHMID_NONE if addr is not within a SysV shared memory segment.
12547c478bd9Sstevel@tonic-gate  * Return SHMID_FREE if addr's SysV shared memory segment's id has been freed.
12557c478bd9Sstevel@tonic-gate  *
12567c478bd9Sstevel@tonic-gate  * shmgetid() is called from code in /proc with the process locked but
12577c478bd9Sstevel@tonic-gate  * with pp->p_lock not held.  The address space lock is held, so we
12587c478bd9Sstevel@tonic-gate  * cannot grab pp->p_lock here due to lock-ordering constraints.
12597c478bd9Sstevel@tonic-gate  * Because of all this, modifications to the p_segacct list must only
12607c478bd9Sstevel@tonic-gate  * be made after calling prbarrier() to ensure the process is not locked.
12617c478bd9Sstevel@tonic-gate  * See shmdt() and sa_add(), above. shmgetid() may also be called on a
12627c478bd9Sstevel@tonic-gate  * thread's own process without the process locked.
12637c478bd9Sstevel@tonic-gate  */
12647c478bd9Sstevel@tonic-gate int
12657c478bd9Sstevel@tonic-gate shmgetid(proc_t *pp, caddr_t addr)
12667c478bd9Sstevel@tonic-gate {
12677c478bd9Sstevel@tonic-gate 	segacct_t *sap, template;
12687c478bd9Sstevel@tonic-gate 
12697c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&pp->p_lock));
12707c478bd9Sstevel@tonic-gate 	ASSERT((pp->p_proc_flag & P_PR_LOCK) || pp == curproc);
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate 	if (pp->p_segacct == NULL)
12737c478bd9Sstevel@tonic-gate 		return (SHMID_NONE);
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate 	template.sa_addr = addr;
12767c478bd9Sstevel@tonic-gate 	template.sa_len = 0;
12777c478bd9Sstevel@tonic-gate 	if ((sap = avl_find(pp->p_segacct, &template, NULL)) == NULL)
12787c478bd9Sstevel@tonic-gate 		return (SHMID_NONE);
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate 	if (IPC_FREE(&sap->sa_id->shm_perm))
12817c478bd9Sstevel@tonic-gate 		return (SHMID_FREE);
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate 	return (sap->sa_id->shm_perm.ipc_id);
12847c478bd9Sstevel@tonic-gate }
1285