xref: /onnv-gate/usr/src/uts/common/os/shm.c (revision 3379:ab5e51073e69)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52414Saguzovsk  * Common Development and Distribution License (the "License").
62414Saguzovsk  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*3379Ssl108498  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
270Sstevel@tonic-gate /*	  All Rights Reserved	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
310Sstevel@tonic-gate  * The Regents of the University of California
320Sstevel@tonic-gate  * All Rights Reserved
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
350Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
360Sstevel@tonic-gate  * contributors.
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
400Sstevel@tonic-gate 
410Sstevel@tonic-gate /*
420Sstevel@tonic-gate  * Inter-Process Communication Shared Memory Facility.
430Sstevel@tonic-gate  *
440Sstevel@tonic-gate  * See os/ipc.c for a description of common IPC functionality.
450Sstevel@tonic-gate  *
460Sstevel@tonic-gate  * Resource controls
470Sstevel@tonic-gate  * -----------------
480Sstevel@tonic-gate  *
492677Sml93401  * Control:      zone.max-shm-ids (rc_zone_shmmni)
502677Sml93401  * Description:  Maximum number of shared memory ids allowed a zone.
512677Sml93401  *
522677Sml93401  *   When shmget() is used to allocate a shared memory segment, one id
532677Sml93401  *   is allocated.  If the id allocation doesn't succeed, shmget()
542677Sml93401  *   fails and errno is set to ENOSPC.  Upon successful shmctl(,
552677Sml93401  *   IPC_RMID) the id is deallocated.
562677Sml93401  *
570Sstevel@tonic-gate  * Control:      project.max-shm-ids (rc_project_shmmni)
580Sstevel@tonic-gate  * Description:  Maximum number of shared memory ids allowed a project.
590Sstevel@tonic-gate  *
600Sstevel@tonic-gate  *   When shmget() is used to allocate a shared memory segment, one id
610Sstevel@tonic-gate  *   is allocated.  If the id allocation doesn't succeed, shmget()
620Sstevel@tonic-gate  *   fails and errno is set to ENOSPC.  Upon successful shmctl(,
630Sstevel@tonic-gate  *   IPC_RMID) the id is deallocated.
640Sstevel@tonic-gate  *
652677Sml93401  * Control:      zone.max-shm-memory (rc_zone_shmmax)
662677Sml93401  * Description:  Total amount of shared memory allowed a zone.
672677Sml93401  *
682677Sml93401  *   When shmget() is used to allocate a shared memory segment, the
692677Sml93401  *   segment's size is allocated against this limit.  If the space
702677Sml93401  *   allocation doesn't succeed, shmget() fails and errno is set to
712677Sml93401  *   EINVAL.  The size will be deallocated once the last process has
722677Sml93401  *   detached the segment and the segment has been successfully
732677Sml93401  *   shmctl(, IPC_RMID)ed.
742677Sml93401  *
750Sstevel@tonic-gate  * Control:      project.max-shm-memory (rc_project_shmmax)
760Sstevel@tonic-gate  * Description:  Total amount of shared memory allowed a project.
770Sstevel@tonic-gate  *
780Sstevel@tonic-gate  *   When shmget() is used to allocate a shared memory segment, the
790Sstevel@tonic-gate  *   segment's size is allocated against this limit.  If the space
800Sstevel@tonic-gate  *   allocation doesn't succeed, shmget() fails and errno is set to
810Sstevel@tonic-gate  *   EINVAL.  The size will be deallocated once the last process has
820Sstevel@tonic-gate  *   detached the segment and the segment has been successfully
830Sstevel@tonic-gate  *   shmctl(, IPC_RMID)ed.
840Sstevel@tonic-gate  */
850Sstevel@tonic-gate 
860Sstevel@tonic-gate #include <sys/types.h>
870Sstevel@tonic-gate #include <sys/param.h>
880Sstevel@tonic-gate #include <sys/cred.h>
890Sstevel@tonic-gate #include <sys/errno.h>
900Sstevel@tonic-gate #include <sys/time.h>
910Sstevel@tonic-gate #include <sys/kmem.h>
920Sstevel@tonic-gate #include <sys/user.h>
930Sstevel@tonic-gate #include <sys/proc.h>
940Sstevel@tonic-gate #include <sys/systm.h>
950Sstevel@tonic-gate #include <sys/prsystm.h>
960Sstevel@tonic-gate #include <sys/sysmacros.h>
970Sstevel@tonic-gate #include <sys/tuneable.h>
980Sstevel@tonic-gate #include <sys/vm.h>
990Sstevel@tonic-gate #include <sys/mman.h>
1000Sstevel@tonic-gate #include <sys/swap.h>
1010Sstevel@tonic-gate #include <sys/cmn_err.h>
1020Sstevel@tonic-gate #include <sys/debug.h>
1030Sstevel@tonic-gate #include <sys/lwpchan_impl.h>
1040Sstevel@tonic-gate #include <sys/avl.h>
1050Sstevel@tonic-gate #include <sys/modctl.h>
1060Sstevel@tonic-gate #include <sys/syscall.h>
1070Sstevel@tonic-gate #include <sys/task.h>
1080Sstevel@tonic-gate #include <sys/project.h>
1090Sstevel@tonic-gate #include <sys/policy.h>
1100Sstevel@tonic-gate #include <sys/zone.h>
1112768Ssl108498 #include <sys/rctl.h>
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate #include <sys/ipc.h>
1140Sstevel@tonic-gate #include <sys/ipc_impl.h>
1150Sstevel@tonic-gate #include <sys/shm.h>
1160Sstevel@tonic-gate #include <sys/shm_impl.h>
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate #include <vm/hat.h>
1190Sstevel@tonic-gate #include <vm/seg.h>
1200Sstevel@tonic-gate #include <vm/as.h>
1210Sstevel@tonic-gate #include <vm/seg_vn.h>
1220Sstevel@tonic-gate #include <vm/anon.h>
1230Sstevel@tonic-gate #include <vm/page.h>
1240Sstevel@tonic-gate #include <vm/vpage.h>
1250Sstevel@tonic-gate #include <vm/seg_spt.h>
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate #include <c2/audit.h>
1280Sstevel@tonic-gate 
1292768Ssl108498 static int shmem_lock(kshmid_t *sp, struct anon_map *amp);
1302768Ssl108498 static void shmem_unlock(kshmid_t *sp, struct anon_map *amp);
1310Sstevel@tonic-gate static void sa_add(struct proc *pp, caddr_t addr, size_t len, ulong_t flags,
1320Sstevel@tonic-gate 	kshmid_t *id);
133*3379Ssl108498 static void shm_rm_amp(kshmid_t *sp);
1340Sstevel@tonic-gate static void shm_dtor(kipc_perm_t *);
1350Sstevel@tonic-gate static void shm_rmid(kipc_perm_t *);
1360Sstevel@tonic-gate static void shm_remove_zone(zoneid_t, void *);
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate /*
1390Sstevel@tonic-gate  * Semantics for share_page_table and ism_off:
1400Sstevel@tonic-gate  *
1410Sstevel@tonic-gate  * These are hooks in /etc/system - only for internal testing purpose.
1420Sstevel@tonic-gate  *
1430Sstevel@tonic-gate  * Setting share_page_table automatically turns on the SHM_SHARE_MMU (ISM) flag
1440Sstevel@tonic-gate  * in a call to shmat(2). In other words, with share_page_table set, you always
1450Sstevel@tonic-gate  * get ISM, even if say, DISM is specified. It should really be called "ism_on".
1460Sstevel@tonic-gate  *
1470Sstevel@tonic-gate  * Setting ism_off turns off the SHM_SHARE_MMU flag from the flags passed to
1480Sstevel@tonic-gate  * shmat(2).
1490Sstevel@tonic-gate  *
1500Sstevel@tonic-gate  * If both share_page_table and ism_off are set, share_page_table prevails.
1510Sstevel@tonic-gate  *
1520Sstevel@tonic-gate  * Although these tunables should probably be removed, they do have some
1530Sstevel@tonic-gate  * external exposure; as long as they exist, they should at least work sensibly.
1540Sstevel@tonic-gate  */
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate int share_page_table;
1570Sstevel@tonic-gate int ism_off;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate /*
1600Sstevel@tonic-gate  * The following tunables are obsolete.  Though for compatibility we
1610Sstevel@tonic-gate  * still read and interpret shminfo_shmmax and shminfo_shmmni (see
1620Sstevel@tonic-gate  * os/project.c), the preferred mechanism for administrating the IPC
1630Sstevel@tonic-gate  * Shared Memory facility is through the resource controls described at
1640Sstevel@tonic-gate  * the top of this file.
1650Sstevel@tonic-gate  */
1660Sstevel@tonic-gate size_t	shminfo_shmmax = 0x800000;	/* (obsolete) */
1670Sstevel@tonic-gate int	shminfo_shmmni = 100;		/* (obsolete) */
1680Sstevel@tonic-gate size_t	shminfo_shmmin = 1;		/* (obsolete) */
1690Sstevel@tonic-gate int	shminfo_shmseg = 6;		/* (obsolete) */
1700Sstevel@tonic-gate 
1712677Sml93401 extern rctl_hndl_t rc_zone_shmmax;
1722677Sml93401 extern rctl_hndl_t rc_zone_shmmni;
1730Sstevel@tonic-gate extern rctl_hndl_t rc_project_shmmax;
1740Sstevel@tonic-gate extern rctl_hndl_t rc_project_shmmni;
1750Sstevel@tonic-gate static ipc_service_t *shm_svc;
1760Sstevel@tonic-gate static zone_key_t shm_zone_key;
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate /*
1790Sstevel@tonic-gate  * Module linkage information for the kernel.
1800Sstevel@tonic-gate  */
1810Sstevel@tonic-gate static uintptr_t shmsys(int, uintptr_t, uintptr_t, uintptr_t);
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate static struct sysent ipcshm_sysent = {
1840Sstevel@tonic-gate 	4,
1850Sstevel@tonic-gate #ifdef	_SYSCALL32_IMPL
1860Sstevel@tonic-gate 	SE_ARGC | SE_NOUNLOAD | SE_64RVAL,
1870Sstevel@tonic-gate #else	/* _SYSCALL32_IMPL */
1880Sstevel@tonic-gate 	SE_ARGC | SE_NOUNLOAD | SE_32RVAL1,
1890Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
1900Sstevel@tonic-gate 	(int (*)())shmsys
1910Sstevel@tonic-gate };
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate #ifdef	_SYSCALL32_IMPL
1940Sstevel@tonic-gate static struct sysent ipcshm_sysent32 = {
1950Sstevel@tonic-gate 	4,
1960Sstevel@tonic-gate 	SE_ARGC | SE_NOUNLOAD | SE_32RVAL1,
1970Sstevel@tonic-gate 	(int (*)())shmsys
1980Sstevel@tonic-gate };
1990Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate static struct modlsys modlsys = {
2020Sstevel@tonic-gate 	&mod_syscallops, "System V shared memory", &ipcshm_sysent
2030Sstevel@tonic-gate };
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate #ifdef	_SYSCALL32_IMPL
2060Sstevel@tonic-gate static struct modlsys modlsys32 = {
2070Sstevel@tonic-gate 	&mod_syscallops32, "32-bit System V shared memory", &ipcshm_sysent32
2080Sstevel@tonic-gate };
2090Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate static struct modlinkage modlinkage = {
2120Sstevel@tonic-gate 	MODREV_1,
2130Sstevel@tonic-gate 	&modlsys,
2140Sstevel@tonic-gate #ifdef	_SYSCALL32_IMPL
2150Sstevel@tonic-gate 	&modlsys32,
2160Sstevel@tonic-gate #endif
2170Sstevel@tonic-gate 	NULL
2180Sstevel@tonic-gate };
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate int
2220Sstevel@tonic-gate _init(void)
2230Sstevel@tonic-gate {
2240Sstevel@tonic-gate 	int result;
2250Sstevel@tonic-gate 
2262677Sml93401 	shm_svc = ipcs_create("shmids", rc_project_shmmni, rc_zone_shmmni,
2272677Sml93401 	    sizeof (kshmid_t), shm_dtor, shm_rmid, AT_IPC_SHM,
2282677Sml93401 	    offsetof(ipc_rqty_t, ipcq_shmmni));
2290Sstevel@tonic-gate 	zone_key_create(&shm_zone_key, NULL, shm_remove_zone, NULL);
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	if ((result = mod_install(&modlinkage)) == 0)
2320Sstevel@tonic-gate 		return (0);
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	(void) zone_key_delete(shm_zone_key);
2350Sstevel@tonic-gate 	ipcs_destroy(shm_svc);
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	return (result);
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate int
2410Sstevel@tonic-gate _fini(void)
2420Sstevel@tonic-gate {
2430Sstevel@tonic-gate 	return (EBUSY);
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate int
2470Sstevel@tonic-gate _info(struct modinfo *modinfop)
2480Sstevel@tonic-gate {
2490Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate /*
2530Sstevel@tonic-gate  * Shmat (attach shared segment) system call.
2540Sstevel@tonic-gate  */
2550Sstevel@tonic-gate static int
2560Sstevel@tonic-gate shmat(int shmid, caddr_t uaddr, int uflags, uintptr_t *rvp)
2570Sstevel@tonic-gate {
2580Sstevel@tonic-gate 	kshmid_t *sp;	/* shared memory header ptr */
2590Sstevel@tonic-gate 	size_t	size;
2600Sstevel@tonic-gate 	int	error = 0;
2610Sstevel@tonic-gate 	proc_t *pp = curproc;
2620Sstevel@tonic-gate 	struct as *as = pp->p_as;
2630Sstevel@tonic-gate 	struct segvn_crargs	crargs;	/* segvn create arguments */
2640Sstevel@tonic-gate 	kmutex_t	*lock;
2650Sstevel@tonic-gate 	struct seg 	*segspt = NULL;
2660Sstevel@tonic-gate 	caddr_t		addr = uaddr;
2670Sstevel@tonic-gate 	int		flags = (uflags & SHMAT_VALID_FLAGS_MASK);
2680Sstevel@tonic-gate 	int		useISM;
2690Sstevel@tonic-gate 	uchar_t		prot = PROT_ALL;
2700Sstevel@tonic-gate 	int result;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	if ((lock = ipc_lookup(shm_svc, shmid, (kipc_perm_t **)&sp)) == NULL)
2730Sstevel@tonic-gate 		return (EINVAL);
2740Sstevel@tonic-gate 	if (error = ipcperm_access(&sp->shm_perm, SHM_R, CRED()))
2750Sstevel@tonic-gate 		goto errret;
2760Sstevel@tonic-gate 	if ((flags & SHM_RDONLY) == 0 &&
2770Sstevel@tonic-gate 	    (error = ipcperm_access(&sp->shm_perm, SHM_W, CRED())))
2780Sstevel@tonic-gate 		goto errret;
2790Sstevel@tonic-gate 	if (spt_invalid(flags)) {
2800Sstevel@tonic-gate 		error = EINVAL;
2810Sstevel@tonic-gate 		goto errret;
2820Sstevel@tonic-gate 	}
2830Sstevel@tonic-gate 	if (ism_off)
2840Sstevel@tonic-gate 		flags = flags & ~SHM_SHARE_MMU;
2850Sstevel@tonic-gate 	if (share_page_table) {
2860Sstevel@tonic-gate 		flags = flags & ~SHM_PAGEABLE;
2870Sstevel@tonic-gate 		flags = flags | SHM_SHARE_MMU;
2880Sstevel@tonic-gate 	}
2890Sstevel@tonic-gate 	useISM = (spt_locked(flags) || spt_pageable(flags));
2900Sstevel@tonic-gate 	if (useISM && (error = ipcperm_access(&sp->shm_perm, SHM_W, CRED())))
2910Sstevel@tonic-gate 		goto errret;
2920Sstevel@tonic-gate 	if (useISM && isspt(sp)) {
2930Sstevel@tonic-gate 		uint_t newsptflags = flags | spt_flags(sp->shm_sptseg);
2940Sstevel@tonic-gate 		/*
2950Sstevel@tonic-gate 		 * If trying to change an existing {D}ISM segment from ISM
2960Sstevel@tonic-gate 		 * to DISM or vice versa, return error. Note that this
2970Sstevel@tonic-gate 		 * validation of flags needs to be done after the effect of
2980Sstevel@tonic-gate 		 * tunables such as ism_off and share_page_table, for
2990Sstevel@tonic-gate 		 * semantics that are consistent with the tunables' settings.
3000Sstevel@tonic-gate 		 */
3010Sstevel@tonic-gate 		if (spt_invalid(newsptflags)) {
3020Sstevel@tonic-gate 			error = EINVAL;
3030Sstevel@tonic-gate 			goto errret;
3040Sstevel@tonic-gate 		}
3050Sstevel@tonic-gate 	}
3060Sstevel@tonic-gate 	ANON_LOCK_ENTER(&sp->shm_amp->a_rwlock, RW_WRITER);
3070Sstevel@tonic-gate 	size = sp->shm_amp->size;
3080Sstevel@tonic-gate 	ANON_LOCK_EXIT(&sp->shm_amp->a_rwlock);
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	/* somewhere to record spt info for final detach */
3110Sstevel@tonic-gate 	if (sp->shm_sptinfo == NULL)
3120Sstevel@tonic-gate 		sp->shm_sptinfo = kmem_zalloc(sizeof (sptinfo_t), KM_SLEEP);
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	as_rangelock(as);
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	if (useISM) {
3170Sstevel@tonic-gate 		/*
3180Sstevel@tonic-gate 		 * Handle ISM
3190Sstevel@tonic-gate 		 */
3200Sstevel@tonic-gate 		uint_t	n, share_szc;
3210Sstevel@tonic-gate 		size_t	share_size;
3220Sstevel@tonic-gate 		struct	shm_data ssd;
3230Sstevel@tonic-gate 		uintptr_t align_hint;
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 		n = page_num_pagesizes();
3260Sstevel@tonic-gate 		if (n < 2) { /* large pages aren't supported */
3270Sstevel@tonic-gate 			as_rangeunlock(as);
3280Sstevel@tonic-gate 			error = EINVAL;
3290Sstevel@tonic-gate 			goto errret;
3300Sstevel@tonic-gate 		}
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 		/*
3330Sstevel@tonic-gate 		 * Pick a share pagesize to use, if (!isspt(sp)).
3340Sstevel@tonic-gate 		 * Otherwise use the already chosen page size.
3350Sstevel@tonic-gate 		 *
3360Sstevel@tonic-gate 		 * For the initial shmat (!isspt(sp)), where sptcreate is
3370Sstevel@tonic-gate 		 * called, map_pgsz is called to recommend a [D]ISM pagesize,
3380Sstevel@tonic-gate 		 * important for systems which offer more than one potential
3390Sstevel@tonic-gate 		 * [D]ISM pagesize.
3400Sstevel@tonic-gate 		 * If the shmat is just to attach to an already created
3410Sstevel@tonic-gate 		 * [D]ISM segment, then use the previously selected page size.
3420Sstevel@tonic-gate 		 */
3430Sstevel@tonic-gate 		if (!isspt(sp)) {
3442991Ssusans 			share_size = map_pgsz(MAPPGSZ_ISM, pp, addr, size, 0);
3450Sstevel@tonic-gate 			if (share_size == 0) {
3460Sstevel@tonic-gate 				as_rangeunlock(as);
3470Sstevel@tonic-gate 				error = EINVAL;
3480Sstevel@tonic-gate 				goto errret;
3490Sstevel@tonic-gate 			}
3500Sstevel@tonic-gate 			share_szc = page_szc(share_size);
3510Sstevel@tonic-gate 		} else {
3520Sstevel@tonic-gate 			share_szc = sp->shm_sptseg->s_szc;
3530Sstevel@tonic-gate 			share_size = page_get_pagesize(share_szc);
3540Sstevel@tonic-gate 		}
3550Sstevel@tonic-gate 		size = P2ROUNDUP(size, share_size);
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 		align_hint = share_size;
3580Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
3590Sstevel@tonic-gate 		/*
3600Sstevel@tonic-gate 		 * For 64 bit amd64, we want to share an entire page table
3610Sstevel@tonic-gate 		 * if possible. We know (ugh) that there are 512 entries in
3620Sstevel@tonic-gate 		 * in a page table. The number for 32 bit non-PAE should be
3630Sstevel@tonic-gate 		 * 1024, but I'm not going to special case that. Note using 512
3640Sstevel@tonic-gate 		 * won't cause a failure below. It retries with align_hint set
3650Sstevel@tonic-gate 		 * to share_size
3660Sstevel@tonic-gate 		 */
3670Sstevel@tonic-gate 		while (size >= 512 * (uint64_t)align_hint)
3680Sstevel@tonic-gate 			align_hint *= 512;
3690Sstevel@tonic-gate #endif /* __i386 || __amd64 */
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate #if defined(__sparcv9)
3720Sstevel@tonic-gate 		if (addr == 0 && curproc->p_model == DATAMODEL_LP64) {
3730Sstevel@tonic-gate 			/*
3740Sstevel@tonic-gate 			 * If no address has been passed in, and this is a
3750Sstevel@tonic-gate 			 * 64-bit process, we'll try to find an address
3760Sstevel@tonic-gate 			 * in the predict-ISM zone.
3770Sstevel@tonic-gate 			 */
3780Sstevel@tonic-gate 			caddr_t predbase = (caddr_t)PREDISM_1T_BASE;
3790Sstevel@tonic-gate 			size_t len = PREDISM_BOUND - PREDISM_1T_BASE;
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 			as_purge(as);
3820Sstevel@tonic-gate 			if (as_gap(as, size + share_size, &predbase, &len,
3830Sstevel@tonic-gate 			    AH_LO, (caddr_t)NULL) != -1) {
3840Sstevel@tonic-gate 				/*
3850Sstevel@tonic-gate 				 * We found an address which looks like a
3860Sstevel@tonic-gate 				 * candidate.  We want to round it up, and
3870Sstevel@tonic-gate 				 * then check that it's a valid user range.
3880Sstevel@tonic-gate 				 * This assures that we won't fail below.
3890Sstevel@tonic-gate 				 */
3900Sstevel@tonic-gate 				addr = (caddr_t)P2ROUNDUP((uintptr_t)predbase,
3910Sstevel@tonic-gate 				    share_size);
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 				if (valid_usr_range(addr, size, prot,
3940Sstevel@tonic-gate 				    as, as->a_userlimit) != RANGE_OKAY) {
3950Sstevel@tonic-gate 					addr = 0;
3960Sstevel@tonic-gate 				}
3970Sstevel@tonic-gate 			}
3980Sstevel@tonic-gate 		}
3990Sstevel@tonic-gate #endif /* __sparcv9 */
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 		if (addr == 0) {
4020Sstevel@tonic-gate 			for (;;) {
4030Sstevel@tonic-gate 				addr = (caddr_t)align_hint;
4040Sstevel@tonic-gate 				map_addr(&addr, size, 0ll, 1, MAP_ALIGN);
4050Sstevel@tonic-gate 				if (addr != NULL || align_hint == share_size)
4060Sstevel@tonic-gate 					break;
4070Sstevel@tonic-gate 				align_hint = share_size;
4080Sstevel@tonic-gate 			}
4090Sstevel@tonic-gate 			if (addr == NULL) {
4100Sstevel@tonic-gate 				as_rangeunlock(as);
4110Sstevel@tonic-gate 				error = ENOMEM;
4120Sstevel@tonic-gate 				goto errret;
4130Sstevel@tonic-gate 			}
4140Sstevel@tonic-gate 			ASSERT(((uintptr_t)addr & (align_hint - 1)) == 0);
4150Sstevel@tonic-gate 		} else {
4160Sstevel@tonic-gate 			/* Use the user-supplied attach address */
4170Sstevel@tonic-gate 			caddr_t base;
4180Sstevel@tonic-gate 			size_t len;
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 			/*
4210Sstevel@tonic-gate 			 * Check that the address range
4220Sstevel@tonic-gate 			 *  1) is properly aligned
4230Sstevel@tonic-gate 			 *  2) is correct in unix terms
4240Sstevel@tonic-gate 			 *  3) is within an unmapped address segment
4250Sstevel@tonic-gate 			 */
4260Sstevel@tonic-gate 			base = addr;
4270Sstevel@tonic-gate 			len = size;		/* use spt aligned size */
4280Sstevel@tonic-gate 			/* XXX - in SunOS, is sp->shm_segsz */
4290Sstevel@tonic-gate 			if ((uintptr_t)base & (share_size - 1)) {
4300Sstevel@tonic-gate 				error = EINVAL;
4310Sstevel@tonic-gate 				as_rangeunlock(as);
4320Sstevel@tonic-gate 				goto errret;
4330Sstevel@tonic-gate 			}
4340Sstevel@tonic-gate 			result = valid_usr_range(base, len, prot, as,
4350Sstevel@tonic-gate 			    as->a_userlimit);
4360Sstevel@tonic-gate 			if (result == RANGE_BADPROT) {
4370Sstevel@tonic-gate 				/*
4380Sstevel@tonic-gate 				 * We try to accomodate processors which
4390Sstevel@tonic-gate 				 * may not support execute permissions on
4400Sstevel@tonic-gate 				 * all ISM segments by trying the check
4410Sstevel@tonic-gate 				 * again but without PROT_EXEC.
4420Sstevel@tonic-gate 				 */
4430Sstevel@tonic-gate 				prot &= ~PROT_EXEC;
4440Sstevel@tonic-gate 				result = valid_usr_range(base, len, prot, as,
4450Sstevel@tonic-gate 				    as->a_userlimit);
4460Sstevel@tonic-gate 			}
4470Sstevel@tonic-gate 			as_purge(as);
4480Sstevel@tonic-gate 			if (result != RANGE_OKAY ||
4490Sstevel@tonic-gate 			    as_gap(as, len, &base, &len, AH_LO,
4500Sstevel@tonic-gate 			    (caddr_t)NULL) != 0) {
4510Sstevel@tonic-gate 				error = EINVAL;
4520Sstevel@tonic-gate 				as_rangeunlock(as);
4530Sstevel@tonic-gate 				goto errret;
4540Sstevel@tonic-gate 			}
4550Sstevel@tonic-gate 		}
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 		if (!isspt(sp)) {
4580Sstevel@tonic-gate 			error = sptcreate(size, &segspt, sp->shm_amp, prot,
4590Sstevel@tonic-gate 			    flags, share_szc);
4600Sstevel@tonic-gate 			if (error) {
4610Sstevel@tonic-gate 				as_rangeunlock(as);
4620Sstevel@tonic-gate 				goto errret;
4630Sstevel@tonic-gate 			}
4640Sstevel@tonic-gate 			sp->shm_sptinfo->sptas = segspt->s_as;
4650Sstevel@tonic-gate 			sp->shm_sptseg = segspt;
4660Sstevel@tonic-gate 			sp->shm_sptprot = prot;
4670Sstevel@tonic-gate 		} else if ((prot & sp->shm_sptprot) != sp->shm_sptprot) {
4680Sstevel@tonic-gate 			/*
4690Sstevel@tonic-gate 			 * Ensure we're attaching to an ISM segment with
4700Sstevel@tonic-gate 			 * fewer or equal permissions than what we're
4710Sstevel@tonic-gate 			 * allowed.  Fail if the segment has more
4720Sstevel@tonic-gate 			 * permissions than what we're allowed.
4730Sstevel@tonic-gate 			 */
4740Sstevel@tonic-gate 			error = EACCES;
4750Sstevel@tonic-gate 			as_rangeunlock(as);
4760Sstevel@tonic-gate 			goto errret;
4770Sstevel@tonic-gate 		}
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 		ssd.shm_sptseg = sp->shm_sptseg;
4800Sstevel@tonic-gate 		ssd.shm_sptas = sp->shm_sptinfo->sptas;
4810Sstevel@tonic-gate 		ssd.shm_amp = sp->shm_amp;
4820Sstevel@tonic-gate 		error = as_map(as, addr, size, segspt_shmattach, &ssd);
4830Sstevel@tonic-gate 		if (error == 0)
4840Sstevel@tonic-gate 			sp->shm_ismattch++; /* keep count of ISM attaches */
4850Sstevel@tonic-gate 	} else {
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 		/*
4880Sstevel@tonic-gate 		 * Normal case.
4890Sstevel@tonic-gate 		 */
4900Sstevel@tonic-gate 		if (flags & SHM_RDONLY)
4910Sstevel@tonic-gate 			prot &= ~PROT_WRITE;
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 		if (addr == 0) {
4940Sstevel@tonic-gate 			/* Let the system pick the attach address */
4950Sstevel@tonic-gate 			map_addr(&addr, size, 0ll, 1, 0);
4960Sstevel@tonic-gate 			if (addr == NULL) {
4970Sstevel@tonic-gate 				as_rangeunlock(as);
4980Sstevel@tonic-gate 				error = ENOMEM;
4990Sstevel@tonic-gate 				goto errret;
5000Sstevel@tonic-gate 			}
5010Sstevel@tonic-gate 		} else {
5020Sstevel@tonic-gate 			/* Use the user-supplied attach address */
5030Sstevel@tonic-gate 			caddr_t base;
5040Sstevel@tonic-gate 			size_t len;
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 			if (flags & SHM_RND)
5070Sstevel@tonic-gate 				addr = (caddr_t)((uintptr_t)addr &
5080Sstevel@tonic-gate 				    ~(SHMLBA - 1));
5090Sstevel@tonic-gate 			/*
5100Sstevel@tonic-gate 			 * Check that the address range
5110Sstevel@tonic-gate 			 *  1) is properly aligned
5120Sstevel@tonic-gate 			 *  2) is correct in unix terms
5130Sstevel@tonic-gate 			 *  3) is within an unmapped address segment
5140Sstevel@tonic-gate 			 */
5150Sstevel@tonic-gate 			base = addr;
5160Sstevel@tonic-gate 			len = size;		/* use aligned size */
5170Sstevel@tonic-gate 			/* XXX - in SunOS, is sp->shm_segsz */
5180Sstevel@tonic-gate 			if ((uintptr_t)base & PAGEOFFSET) {
5190Sstevel@tonic-gate 				error = EINVAL;
5200Sstevel@tonic-gate 				as_rangeunlock(as);
5210Sstevel@tonic-gate 				goto errret;
5220Sstevel@tonic-gate 			}
5230Sstevel@tonic-gate 			result = valid_usr_range(base, len, prot, as,
5240Sstevel@tonic-gate 			    as->a_userlimit);
5250Sstevel@tonic-gate 			if (result == RANGE_BADPROT) {
5260Sstevel@tonic-gate 				prot &= ~PROT_EXEC;
5270Sstevel@tonic-gate 				result = valid_usr_range(base, len, prot, as,
5280Sstevel@tonic-gate 				    as->a_userlimit);
5290Sstevel@tonic-gate 			}
5300Sstevel@tonic-gate 			as_purge(as);
5310Sstevel@tonic-gate 			if (result != RANGE_OKAY ||
5320Sstevel@tonic-gate 			    as_gap(as, len, &base, &len,
5330Sstevel@tonic-gate 			    AH_LO, (caddr_t)NULL) != 0) {
5340Sstevel@tonic-gate 				error = EINVAL;
5350Sstevel@tonic-gate 				as_rangeunlock(as);
5360Sstevel@tonic-gate 				goto errret;
5370Sstevel@tonic-gate 			}
5380Sstevel@tonic-gate 		}
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 		/* Initialize the create arguments and map the segment */
5410Sstevel@tonic-gate 		crargs = *(struct segvn_crargs *)zfod_argsp;
5420Sstevel@tonic-gate 		crargs.offset = 0;
5430Sstevel@tonic-gate 		crargs.type = MAP_SHARED;
5440Sstevel@tonic-gate 		crargs.amp = sp->shm_amp;
5450Sstevel@tonic-gate 		crargs.prot = prot;
5460Sstevel@tonic-gate 		crargs.maxprot = crargs.prot;
5470Sstevel@tonic-gate 		crargs.flags = 0;
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 		error = as_map(as, addr, size, segvn_create, &crargs);
5500Sstevel@tonic-gate 	}
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	as_rangeunlock(as);
5530Sstevel@tonic-gate 	if (error)
5540Sstevel@tonic-gate 		goto errret;
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 	/* record shmem range for the detach */
5570Sstevel@tonic-gate 	sa_add(pp, addr, (size_t)size, useISM ? SHMSA_ISM : 0, sp);
5580Sstevel@tonic-gate 	*rvp = (uintptr_t)addr;
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	sp->shm_atime = gethrestime_sec();
5610Sstevel@tonic-gate 	sp->shm_lpid = pp->p_pid;
5620Sstevel@tonic-gate 	ipc_hold(shm_svc, (kipc_perm_t *)sp);
5630Sstevel@tonic-gate errret:
5640Sstevel@tonic-gate 	mutex_exit(lock);
5650Sstevel@tonic-gate 	return (error);
5660Sstevel@tonic-gate }
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate static void
5690Sstevel@tonic-gate shm_dtor(kipc_perm_t *perm)
5700Sstevel@tonic-gate {
5710Sstevel@tonic-gate 	kshmid_t *sp = (kshmid_t *)perm;
5720Sstevel@tonic-gate 	uint_t cnt;
5732677Sml93401 	size_t rsize;
5740Sstevel@tonic-gate 
5752768Ssl108498 	if (sp->shm_lkcnt > 0) {
5762768Ssl108498 		shmem_unlock(sp, sp->shm_amp);
5772768Ssl108498 		sp->shm_lkcnt = 0;
5782768Ssl108498 	}
5792768Ssl108498 
5800Sstevel@tonic-gate 	if (sp->shm_sptinfo) {
5810Sstevel@tonic-gate 		if (isspt(sp))
5820Sstevel@tonic-gate 			sptdestroy(sp->shm_sptinfo->sptas, sp->shm_amp);
5830Sstevel@tonic-gate 		kmem_free(sp->shm_sptinfo, sizeof (sptinfo_t));
5840Sstevel@tonic-gate 	}
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	ANON_LOCK_ENTER(&sp->shm_amp->a_rwlock, RW_WRITER);
5870Sstevel@tonic-gate 	cnt = --sp->shm_amp->refcnt;
5880Sstevel@tonic-gate 	ANON_LOCK_EXIT(&sp->shm_amp->a_rwlock);
5890Sstevel@tonic-gate 	ASSERT(cnt == 0);
590*3379Ssl108498 	shm_rm_amp(sp);
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 	if (sp->shm_perm.ipc_id != IPC_ID_INVAL) {
5932677Sml93401 		rsize = ptob(btopr(sp->shm_segsz));
5940Sstevel@tonic-gate 		ipcs_lock(shm_svc);
5952677Sml93401 		sp->shm_perm.ipc_proj->kpj_data.kpd_shmmax -= rsize;
5962677Sml93401 		sp->shm_perm.ipc_zone->zone_shmmax -= rsize;
5970Sstevel@tonic-gate 		ipcs_unlock(shm_svc);
5980Sstevel@tonic-gate 	}
5990Sstevel@tonic-gate }
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate /* ARGSUSED */
6020Sstevel@tonic-gate static void
6030Sstevel@tonic-gate shm_rmid(kipc_perm_t *perm)
6040Sstevel@tonic-gate {
6050Sstevel@tonic-gate 	/* nothing to do */
6060Sstevel@tonic-gate }
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate /*
6090Sstevel@tonic-gate  * Shmctl system call.
6100Sstevel@tonic-gate  */
6110Sstevel@tonic-gate /* ARGSUSED */
6120Sstevel@tonic-gate static int
6130Sstevel@tonic-gate shmctl(int shmid, int cmd, void *arg)
6140Sstevel@tonic-gate {
6150Sstevel@tonic-gate 	kshmid_t		*sp;	/* shared memory header ptr */
6160Sstevel@tonic-gate 	STRUCT_DECL(shmid_ds, ds);	/* for SVR4 IPC_SET */
6170Sstevel@tonic-gate 	int			error = 0;
6180Sstevel@tonic-gate 	struct cred 		*cr = CRED();
6190Sstevel@tonic-gate 	kmutex_t		*lock;
6200Sstevel@tonic-gate 	model_t			mdl = get_udatamodel();
6210Sstevel@tonic-gate 	struct shmid_ds64	ds64;
6220Sstevel@tonic-gate 	shmatt_t		nattch;
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	STRUCT_INIT(ds, mdl);
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 	/*
6270Sstevel@tonic-gate 	 * Perform pre- or non-lookup actions (e.g. copyins, RMID).
6280Sstevel@tonic-gate 	 */
6290Sstevel@tonic-gate 	switch (cmd) {
6300Sstevel@tonic-gate 	case IPC_SET:
6310Sstevel@tonic-gate 		if (copyin(arg, STRUCT_BUF(ds), STRUCT_SIZE(ds)))
6320Sstevel@tonic-gate 			return (EFAULT);
6330Sstevel@tonic-gate 		break;
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	case IPC_SET64:
6360Sstevel@tonic-gate 		if (copyin(arg, &ds64, sizeof (struct shmid_ds64)))
6370Sstevel@tonic-gate 			return (EFAULT);
6380Sstevel@tonic-gate 		break;
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 	case IPC_RMID:
6410Sstevel@tonic-gate 		return (ipc_rmid(shm_svc, shmid, cr));
6420Sstevel@tonic-gate 	}
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 	if ((lock = ipc_lookup(shm_svc, shmid, (kipc_perm_t **)&sp)) == NULL)
6450Sstevel@tonic-gate 		return (EINVAL);
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	switch (cmd) {
6480Sstevel@tonic-gate 	/* Set ownership and permissions. */
6490Sstevel@tonic-gate 	case IPC_SET:
6500Sstevel@tonic-gate 		if (error = ipcperm_set(shm_svc, cr, &sp->shm_perm,
6510Sstevel@tonic-gate 		    &STRUCT_BUF(ds)->shm_perm, mdl))
6520Sstevel@tonic-gate 				break;
6530Sstevel@tonic-gate 		sp->shm_ctime = gethrestime_sec();
6540Sstevel@tonic-gate 		break;
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 	case IPC_STAT:
6570Sstevel@tonic-gate 		if (error = ipcperm_access(&sp->shm_perm, SHM_R, cr))
6580Sstevel@tonic-gate 			break;
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 		nattch = sp->shm_perm.ipc_ref - 1;
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 		ipcperm_stat(&STRUCT_BUF(ds)->shm_perm, &sp->shm_perm, mdl);
6630Sstevel@tonic-gate 		STRUCT_FSET(ds, shm_segsz, sp->shm_segsz);
6640Sstevel@tonic-gate 		STRUCT_FSETP(ds, shm_amp, NULL);	/* kernel addr */
6650Sstevel@tonic-gate 		STRUCT_FSET(ds, shm_lkcnt, sp->shm_lkcnt);
6660Sstevel@tonic-gate 		STRUCT_FSET(ds, shm_lpid, sp->shm_lpid);
6670Sstevel@tonic-gate 		STRUCT_FSET(ds, shm_cpid, sp->shm_cpid);
6680Sstevel@tonic-gate 		STRUCT_FSET(ds, shm_nattch, nattch);
6690Sstevel@tonic-gate 		STRUCT_FSET(ds, shm_cnattch, sp->shm_ismattch);
6700Sstevel@tonic-gate 		STRUCT_FSET(ds, shm_atime, sp->shm_atime);
6710Sstevel@tonic-gate 		STRUCT_FSET(ds, shm_dtime, sp->shm_dtime);
6720Sstevel@tonic-gate 		STRUCT_FSET(ds, shm_ctime, sp->shm_ctime);
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 		mutex_exit(lock);
6750Sstevel@tonic-gate 		if (copyout(STRUCT_BUF(ds), arg, STRUCT_SIZE(ds)))
6760Sstevel@tonic-gate 			return (EFAULT);
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 		return (0);
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	case IPC_SET64:
6810Sstevel@tonic-gate 		if (error = ipcperm_set64(shm_svc, cr,
6820Sstevel@tonic-gate 		    &sp->shm_perm, &ds64.shmx_perm))
6830Sstevel@tonic-gate 			break;
6840Sstevel@tonic-gate 		sp->shm_ctime = gethrestime_sec();
6850Sstevel@tonic-gate 		break;
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 	case IPC_STAT64:
6880Sstevel@tonic-gate 		nattch = sp->shm_perm.ipc_ref - 1;
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 		ipcperm_stat64(&ds64.shmx_perm, &sp->shm_perm);
6910Sstevel@tonic-gate 		ds64.shmx_segsz = sp->shm_segsz;
6920Sstevel@tonic-gate 		ds64.shmx_lkcnt = sp->shm_lkcnt;
6930Sstevel@tonic-gate 		ds64.shmx_lpid = sp->shm_lpid;
6940Sstevel@tonic-gate 		ds64.shmx_cpid = sp->shm_cpid;
6950Sstevel@tonic-gate 		ds64.shmx_nattch = nattch;
6960Sstevel@tonic-gate 		ds64.shmx_cnattch = sp->shm_ismattch;
6970Sstevel@tonic-gate 		ds64.shmx_atime = sp->shm_atime;
6980Sstevel@tonic-gate 		ds64.shmx_dtime = sp->shm_dtime;
6990Sstevel@tonic-gate 		ds64.shmx_ctime = sp->shm_ctime;
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 		mutex_exit(lock);
7020Sstevel@tonic-gate 		if (copyout(&ds64, arg, sizeof (struct shmid_ds64)))
7030Sstevel@tonic-gate 			return (EFAULT);
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 		return (0);
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 	/* Lock segment in memory */
7080Sstevel@tonic-gate 	case SHM_LOCK:
7090Sstevel@tonic-gate 		if ((error = secpolicy_lock_memory(cr)) != 0)
7100Sstevel@tonic-gate 			break;
7110Sstevel@tonic-gate 
7122768Ssl108498 		/* protect against overflow */
7132768Ssl108498 		if (sp->shm_lkcnt >= USHRT_MAX) {
7142768Ssl108498 			error = ENOMEM;
7152768Ssl108498 			break;
7162768Ssl108498 		}
7170Sstevel@tonic-gate 		if (!isspt(sp) && (sp->shm_lkcnt++ == 0)) {
7182768Ssl108498 			if (error = shmem_lock(sp, sp->shm_amp)) {
7190Sstevel@tonic-gate 			    ANON_LOCK_ENTER(&sp->shm_amp->a_rwlock, RW_WRITER);
7200Sstevel@tonic-gate 			    cmn_err(CE_NOTE,
7210Sstevel@tonic-gate 				"shmctl - couldn't lock %ld pages into memory",
7220Sstevel@tonic-gate 				sp->shm_amp->size);
7230Sstevel@tonic-gate 			    ANON_LOCK_EXIT(&sp->shm_amp->a_rwlock);
7240Sstevel@tonic-gate 			    error = ENOMEM;
7250Sstevel@tonic-gate 			    sp->shm_lkcnt--;
7260Sstevel@tonic-gate 			}
7270Sstevel@tonic-gate 		}
7280Sstevel@tonic-gate 		break;
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 	/* Unlock segment */
7310Sstevel@tonic-gate 	case SHM_UNLOCK:
7320Sstevel@tonic-gate 		if ((error = secpolicy_lock_memory(cr)) != 0)
7330Sstevel@tonic-gate 			break;
7340Sstevel@tonic-gate 
7352768Ssl108498 		if (sp->shm_lkcnt && (--sp->shm_lkcnt == 0)) {
7362768Ssl108498 			shmem_unlock(sp, sp->shm_amp);
7370Sstevel@tonic-gate 		}
7380Sstevel@tonic-gate 		break;
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 	default:
7410Sstevel@tonic-gate 		error = EINVAL;
7420Sstevel@tonic-gate 		break;
7430Sstevel@tonic-gate 	}
7440Sstevel@tonic-gate 	mutex_exit(lock);
7450Sstevel@tonic-gate 	return (error);
7460Sstevel@tonic-gate }
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate static void
7490Sstevel@tonic-gate shm_detach(proc_t *pp, segacct_t *sap)
7500Sstevel@tonic-gate {
7510Sstevel@tonic-gate 	kshmid_t	*sp = sap->sa_id;
7520Sstevel@tonic-gate 	size_t		len = sap->sa_len;
7530Sstevel@tonic-gate 	caddr_t		addr = sap->sa_addr;
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	/*
7560Sstevel@tonic-gate 	 * Discard lwpchan mappings.
7570Sstevel@tonic-gate 	 */
7580Sstevel@tonic-gate 	if (pp->p_lcp != NULL)
7590Sstevel@tonic-gate 		lwpchan_delete_mapping(pp, addr, addr + len);
7600Sstevel@tonic-gate 	(void) as_unmap(pp->p_as, addr, len);
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 	/*
7630Sstevel@tonic-gate 	 * Perform some detach-time accounting.
7640Sstevel@tonic-gate 	 */
7650Sstevel@tonic-gate 	(void) ipc_lock(shm_svc, sp->shm_perm.ipc_id);
7660Sstevel@tonic-gate 	if (sap->sa_flags & SHMSA_ISM)
7670Sstevel@tonic-gate 		sp->shm_ismattch--;
7680Sstevel@tonic-gate 	sp->shm_dtime = gethrestime_sec();
7690Sstevel@tonic-gate 	sp->shm_lpid = pp->p_pid;
7700Sstevel@tonic-gate 	ipc_rele(shm_svc, (kipc_perm_t *)sp);	/* Drops lock */
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	kmem_free(sap, sizeof (segacct_t));
7730Sstevel@tonic-gate }
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate static int
7760Sstevel@tonic-gate shmdt(caddr_t addr)
7770Sstevel@tonic-gate {
7780Sstevel@tonic-gate 	proc_t *pp = curproc;
7790Sstevel@tonic-gate 	segacct_t *sap, template;
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
7820Sstevel@tonic-gate 	prbarrier(pp);			/* block /proc.  See shmgetid(). */
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 	template.sa_addr = addr;
7850Sstevel@tonic-gate 	template.sa_len = 0;
7860Sstevel@tonic-gate 	if ((pp->p_segacct == NULL) ||
7870Sstevel@tonic-gate 	    ((sap = avl_find(pp->p_segacct, &template, NULL)) == NULL)) {
7880Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
7890Sstevel@tonic-gate 		return (EINVAL);
7900Sstevel@tonic-gate 	}
7912414Saguzovsk 	if (sap->sa_addr != addr) {
7922414Saguzovsk 		mutex_exit(&pp->p_lock);
7932414Saguzovsk 		return (EINVAL);
7942414Saguzovsk 	}
7950Sstevel@tonic-gate 	avl_remove(pp->p_segacct, sap);
7960Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	shm_detach(pp, sap);
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 	return (0);
8010Sstevel@tonic-gate }
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate /*
8040Sstevel@tonic-gate  * Remove all shared memory segments associated with a given zone.
8050Sstevel@tonic-gate  * Called by zone_shutdown when the zone is halted.
8060Sstevel@tonic-gate  */
8070Sstevel@tonic-gate /*ARGSUSED1*/
8080Sstevel@tonic-gate static void
8090Sstevel@tonic-gate shm_remove_zone(zoneid_t zoneid, void *arg)
8100Sstevel@tonic-gate {
8110Sstevel@tonic-gate 	ipc_remove_zone(shm_svc, zoneid);
8120Sstevel@tonic-gate }
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate /*
8150Sstevel@tonic-gate  * Shmget (create new shmem) system call.
8160Sstevel@tonic-gate  */
8170Sstevel@tonic-gate static int
8180Sstevel@tonic-gate shmget(key_t key, size_t size, int shmflg, uintptr_t *rvp)
8190Sstevel@tonic-gate {
8200Sstevel@tonic-gate 	proc_t		*pp = curproc;
8210Sstevel@tonic-gate 	kshmid_t	*sp;
8220Sstevel@tonic-gate 	kmutex_t	*lock;
8230Sstevel@tonic-gate 	int		error;
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate top:
8260Sstevel@tonic-gate 	if (error = ipc_get(shm_svc, key, shmflg, (kipc_perm_t **)&sp, &lock))
8270Sstevel@tonic-gate 		return (error);
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 	if (!IPC_FREE(&sp->shm_perm)) {
8300Sstevel@tonic-gate 		/*
8310Sstevel@tonic-gate 		 * A segment with the requested key exists.
8320Sstevel@tonic-gate 		 */
8330Sstevel@tonic-gate 		if (size > sp->shm_segsz) {
8340Sstevel@tonic-gate 			mutex_exit(lock);
8350Sstevel@tonic-gate 			return (EINVAL);
8360Sstevel@tonic-gate 		}
8370Sstevel@tonic-gate 	} else {
8380Sstevel@tonic-gate 		/*
8390Sstevel@tonic-gate 		 * A new segment should be created.
8400Sstevel@tonic-gate 		 */
8410Sstevel@tonic-gate 		size_t npages = btopr(size);
8420Sstevel@tonic-gate 		size_t rsize = ptob(npages);
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 		/*
8452677Sml93401 		 * Check rsize and the per-project and per-zone limit on
8462677Sml93401 		 * shared memory.  Checking rsize handles both the size == 0
8470Sstevel@tonic-gate 		 * case and the size < ULONG_MAX & PAGEMASK case (i.e.
8480Sstevel@tonic-gate 		 * rounding up wraps a size_t).
8490Sstevel@tonic-gate 		 */
8502677Sml93401 		if (rsize == 0 ||
8512677Sml93401 		    (rctl_test(rc_project_shmmax,
8520Sstevel@tonic-gate 		    pp->p_task->tk_proj->kpj_rctls, pp, rsize,
8532677Sml93401 		    RCA_SAFE) & RCT_DENY) ||
8542677Sml93401 		    (rctl_test(rc_zone_shmmax,
8552677Sml93401 		    pp->p_zone->zone_rctls, pp, rsize,
8560Sstevel@tonic-gate 		    RCA_SAFE) & RCT_DENY)) {
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
8590Sstevel@tonic-gate 			mutex_exit(lock);
8600Sstevel@tonic-gate 			ipc_cleanup(shm_svc, (kipc_perm_t *)sp);
8610Sstevel@tonic-gate 			return (EINVAL);
8620Sstevel@tonic-gate 		}
8630Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
8640Sstevel@tonic-gate 		mutex_exit(lock);
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 		if (anon_resv(rsize) == 0) {
8670Sstevel@tonic-gate 			ipc_cleanup(shm_svc, (kipc_perm_t *)sp);
8680Sstevel@tonic-gate 			return (ENOMEM);
8690Sstevel@tonic-gate 		}
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 		sp->shm_amp = anonmap_alloc(rsize, rsize);
8722768Ssl108498 		sp->shm_amp->a_sp = sp;
8730Sstevel@tonic-gate 		/*
8740Sstevel@tonic-gate 		 * Store the original user's requested size, in bytes,
8750Sstevel@tonic-gate 		 * rather than the page-aligned size.  The former is
8760Sstevel@tonic-gate 		 * used for IPC_STAT and shmget() lookups.  The latter
8770Sstevel@tonic-gate 		 * is saved in the anon_map structure and is used for
8780Sstevel@tonic-gate 		 * calls to the vm layer.
8790Sstevel@tonic-gate 		 */
8800Sstevel@tonic-gate 		sp->shm_segsz = size;
8810Sstevel@tonic-gate 		sp->shm_atime = sp->shm_dtime = 0;
8820Sstevel@tonic-gate 		sp->shm_ctime = gethrestime_sec();
8830Sstevel@tonic-gate 		sp->shm_lpid = (pid_t)0;
8840Sstevel@tonic-gate 		sp->shm_cpid = curproc->p_pid;
8850Sstevel@tonic-gate 		sp->shm_ismattch = 0;
8860Sstevel@tonic-gate 		sp->shm_sptinfo = NULL;
8870Sstevel@tonic-gate 		/*
8880Sstevel@tonic-gate 		 * Check limits one last time, push id into global
8890Sstevel@tonic-gate 		 * visibility, and update resource usage counts.
8900Sstevel@tonic-gate 		 */
8910Sstevel@tonic-gate 		if (error = ipc_commit_begin(shm_svc, key, shmflg,
8920Sstevel@tonic-gate 		    (kipc_perm_t *)sp)) {
8930Sstevel@tonic-gate 			if (error == EAGAIN)
8940Sstevel@tonic-gate 				goto top;
8950Sstevel@tonic-gate 			return (error);
8960Sstevel@tonic-gate 		}
8970Sstevel@tonic-gate 
8982677Sml93401 		if ((rctl_test(rc_project_shmmax,
8990Sstevel@tonic-gate 		    sp->shm_perm.ipc_proj->kpj_rctls, pp, rsize,
9002677Sml93401 		    RCA_SAFE) & RCT_DENY) ||
9012677Sml93401 		    (rctl_test(rc_zone_shmmax,
9022677Sml93401 		    sp->shm_perm.ipc_zone->zone_rctls, pp, rsize,
9032677Sml93401 		    RCA_SAFE) & RCT_DENY)) {
9040Sstevel@tonic-gate 			ipc_cleanup(shm_svc, (kipc_perm_t *)sp);
9050Sstevel@tonic-gate 			return (EINVAL);
9060Sstevel@tonic-gate 		}
9070Sstevel@tonic-gate 		sp->shm_perm.ipc_proj->kpj_data.kpd_shmmax += rsize;
9082677Sml93401 		sp->shm_perm.ipc_zone->zone_shmmax += rsize;
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 		lock = ipc_commit_end(shm_svc, &sp->shm_perm);
9110Sstevel@tonic-gate 	}
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate #ifdef C2_AUDIT
9140Sstevel@tonic-gate 	if (audit_active)
9150Sstevel@tonic-gate 		audit_ipcget(AT_IPC_SHM, (void *)sp);
9160Sstevel@tonic-gate #endif
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 	*rvp = (uintptr_t)(sp->shm_perm.ipc_id);
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 	mutex_exit(lock);
9210Sstevel@tonic-gate 	return (0);
9220Sstevel@tonic-gate }
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate /*
9250Sstevel@tonic-gate  * shmids system call.
9260Sstevel@tonic-gate  */
9270Sstevel@tonic-gate static int
9280Sstevel@tonic-gate shmids(int *buf, uint_t nids, uint_t *pnids)
9290Sstevel@tonic-gate {
9300Sstevel@tonic-gate 	return (ipc_ids(shm_svc, buf, nids, pnids));
9310Sstevel@tonic-gate }
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate /*
9340Sstevel@tonic-gate  * System entry point for shmat, shmctl, shmdt, and shmget system calls.
9350Sstevel@tonic-gate  */
9360Sstevel@tonic-gate static uintptr_t
9370Sstevel@tonic-gate shmsys(int opcode, uintptr_t a0, uintptr_t a1, uintptr_t a2)
9380Sstevel@tonic-gate {
9390Sstevel@tonic-gate 	int	error;
9400Sstevel@tonic-gate 	uintptr_t r_val = 0;
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate 	switch (opcode) {
9430Sstevel@tonic-gate 	case SHMAT:
9440Sstevel@tonic-gate 		error = shmat((int)a0, (caddr_t)a1, (int)a2, &r_val);
9450Sstevel@tonic-gate 		break;
9460Sstevel@tonic-gate 	case SHMCTL:
9470Sstevel@tonic-gate 		error = shmctl((int)a0, (int)a1, (void *)a2);
9480Sstevel@tonic-gate 		break;
9490Sstevel@tonic-gate 	case SHMDT:
9500Sstevel@tonic-gate 		error = shmdt((caddr_t)a0);
9510Sstevel@tonic-gate 		break;
9520Sstevel@tonic-gate 	case SHMGET:
9530Sstevel@tonic-gate 		error = shmget((key_t)a0, (size_t)a1, (int)a2, &r_val);
9540Sstevel@tonic-gate 		break;
9550Sstevel@tonic-gate 	case SHMIDS:
9560Sstevel@tonic-gate 		error = shmids((int *)a0, (uint_t)a1, (uint_t *)a2);
9570Sstevel@tonic-gate 		break;
9580Sstevel@tonic-gate 	default:
9590Sstevel@tonic-gate 		error = EINVAL;
9600Sstevel@tonic-gate 		break;
9610Sstevel@tonic-gate 	}
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate 	if (error)
9640Sstevel@tonic-gate 		return ((uintptr_t)set_errno(error));
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 	return (r_val);
9670Sstevel@tonic-gate }
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate /*
9700Sstevel@tonic-gate  * segacct_t comparator
9710Sstevel@tonic-gate  * This works as expected, with one minor change: the first of two real
9720Sstevel@tonic-gate  * segments with equal addresses is considered to be 'greater than' the
9730Sstevel@tonic-gate  * second.  We only return equal when searching using a template, in
9740Sstevel@tonic-gate  * which case we explicitly set the template segment's length to 0
9750Sstevel@tonic-gate  * (which is invalid for a real segment).
9760Sstevel@tonic-gate  */
9770Sstevel@tonic-gate static int
9780Sstevel@tonic-gate shm_sacompar(const void *x, const void *y)
9790Sstevel@tonic-gate {
9800Sstevel@tonic-gate 	segacct_t *sa1 = (segacct_t *)x;
9810Sstevel@tonic-gate 	segacct_t *sa2 = (segacct_t *)y;
9820Sstevel@tonic-gate 
9832414Saguzovsk 	if (sa1->sa_addr < sa2->sa_addr) {
9840Sstevel@tonic-gate 		return (-1);
9852414Saguzovsk 	} else if (sa2->sa_len != 0) {
9862414Saguzovsk 		if (sa1->sa_addr >= sa2->sa_addr + sa2->sa_len) {
9872414Saguzovsk 			return (1);
9882414Saguzovsk 		} else if (sa1->sa_len != 0) {
9892414Saguzovsk 			return (1);
9902414Saguzovsk 		} else {
9912414Saguzovsk 			return (0);
9922414Saguzovsk 		}
9932414Saguzovsk 	} else if (sa1->sa_addr > sa2->sa_addr) {
9940Sstevel@tonic-gate 		return (1);
9952414Saguzovsk 	} else {
9960Sstevel@tonic-gate 		return (0);
9972414Saguzovsk 	}
9980Sstevel@tonic-gate }
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate /*
10010Sstevel@tonic-gate  * add this record to the segacct list.
10020Sstevel@tonic-gate  */
10030Sstevel@tonic-gate static void
10040Sstevel@tonic-gate sa_add(struct proc *pp, caddr_t addr, size_t len, ulong_t flags, kshmid_t *id)
10050Sstevel@tonic-gate {
10060Sstevel@tonic-gate 	segacct_t *nsap;
10070Sstevel@tonic-gate 	avl_tree_t *tree = NULL;
10080Sstevel@tonic-gate 	avl_index_t where;
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate 	nsap = kmem_alloc(sizeof (segacct_t), KM_SLEEP);
10110Sstevel@tonic-gate 	nsap->sa_addr = addr;
10120Sstevel@tonic-gate 	nsap->sa_len  = len;
10130Sstevel@tonic-gate 	nsap->sa_flags = flags;
10140Sstevel@tonic-gate 	nsap->sa_id = id;
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 	if (pp->p_segacct == NULL)
10170Sstevel@tonic-gate 		tree = kmem_alloc(sizeof (avl_tree_t), KM_SLEEP);
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
10200Sstevel@tonic-gate 	prbarrier(pp);			/* block /proc.  See shmgetid(). */
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	if (pp->p_segacct == NULL) {
10230Sstevel@tonic-gate 		avl_create(tree, shm_sacompar, sizeof (segacct_t),
10240Sstevel@tonic-gate 		    offsetof(segacct_t, sa_tree));
10250Sstevel@tonic-gate 		pp->p_segacct = tree;
10260Sstevel@tonic-gate 	} else if (tree) {
10270Sstevel@tonic-gate 		kmem_free(tree, sizeof (avl_tree_t));
10280Sstevel@tonic-gate 	}
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate 	/*
10310Sstevel@tonic-gate 	 * We can ignore the result of avl_find, as the comparator will
10320Sstevel@tonic-gate 	 * never return equal for segments with non-zero length.  This
10330Sstevel@tonic-gate 	 * is a necessary hack to get around the fact that we do, in
10340Sstevel@tonic-gate 	 * fact, have duplicate keys.
10350Sstevel@tonic-gate 	 */
10360Sstevel@tonic-gate 	(void) avl_find(pp->p_segacct, nsap, &where);
10370Sstevel@tonic-gate 	avl_insert(pp->p_segacct, nsap, where);
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
10400Sstevel@tonic-gate }
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate /*
10430Sstevel@tonic-gate  * Duplicate parent's segacct records in child.
10440Sstevel@tonic-gate  */
10450Sstevel@tonic-gate void
10460Sstevel@tonic-gate shmfork(struct proc *ppp, struct proc *cpp)
10470Sstevel@tonic-gate {
10480Sstevel@tonic-gate 	segacct_t *sap;
10490Sstevel@tonic-gate 	kshmid_t *sp;
10500Sstevel@tonic-gate 	kmutex_t *mp;
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	ASSERT(ppp->p_segacct != NULL);
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 	/*
10550Sstevel@tonic-gate 	 * We are the only lwp running in the parent so nobody can
10560Sstevel@tonic-gate 	 * mess with our p_segacct list.  Thus it is safe to traverse
10570Sstevel@tonic-gate 	 * the list without holding p_lock.  This is essential because
10580Sstevel@tonic-gate 	 * we can't hold p_lock during a KM_SLEEP allocation.
10590Sstevel@tonic-gate 	 */
10600Sstevel@tonic-gate 	for (sap = (segacct_t *)avl_first(ppp->p_segacct); sap != NULL;
10610Sstevel@tonic-gate 	    sap = (segacct_t *)AVL_NEXT(ppp->p_segacct, sap)) {
10620Sstevel@tonic-gate 		sa_add(cpp, sap->sa_addr, sap->sa_len, sap->sa_flags,
10630Sstevel@tonic-gate 		    sap->sa_id);
10640Sstevel@tonic-gate 		sp = sap->sa_id;
10650Sstevel@tonic-gate 		mp = ipc_lock(shm_svc, sp->shm_perm.ipc_id);
10660Sstevel@tonic-gate 		if (sap->sa_flags & SHMSA_ISM)
10670Sstevel@tonic-gate 			sp->shm_ismattch++;
10680Sstevel@tonic-gate 		ipc_hold(shm_svc, (kipc_perm_t *)sp);
10690Sstevel@tonic-gate 		mutex_exit(mp);
10700Sstevel@tonic-gate 	}
10710Sstevel@tonic-gate }
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate /*
10740Sstevel@tonic-gate  * Detach shared memory segments from exiting process.
10750Sstevel@tonic-gate  */
10760Sstevel@tonic-gate void
10770Sstevel@tonic-gate shmexit(struct proc *pp)
10780Sstevel@tonic-gate {
10790Sstevel@tonic-gate 	segacct_t *sap;
10800Sstevel@tonic-gate 	avl_tree_t *tree;
10810Sstevel@tonic-gate 	void *cookie = NULL;
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 	ASSERT(pp->p_segacct != NULL);
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
10860Sstevel@tonic-gate 	prbarrier(pp);
10870Sstevel@tonic-gate 	tree = pp->p_segacct;
10880Sstevel@tonic-gate 	pp->p_segacct = NULL;
10890Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 	while ((sap = avl_destroy_nodes(tree, &cookie)) != NULL)
10920Sstevel@tonic-gate 		(void) shm_detach(pp, sap);
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate 	avl_destroy(tree);
10950Sstevel@tonic-gate 	kmem_free(tree, sizeof (avl_tree_t));
10960Sstevel@tonic-gate }
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate /*
10990Sstevel@tonic-gate  * At this time pages should be in memory, so just lock them.
11000Sstevel@tonic-gate  */
11010Sstevel@tonic-gate static void
11022768Ssl108498 lock_again(size_t npages, kshmid_t *sp, struct anon_map *amp)
11030Sstevel@tonic-gate {
11040Sstevel@tonic-gate 	struct anon *ap;
11050Sstevel@tonic-gate 	struct page *pp;
11060Sstevel@tonic-gate 	struct vnode *vp;
11072768Ssl108498 	u_offset_t off;
11080Sstevel@tonic-gate 	ulong_t anon_idx;
11090Sstevel@tonic-gate 	anon_sync_obj_t cookie;
11100Sstevel@tonic-gate 
11112768Ssl108498 	mutex_enter(&sp->shm_mlock);
11120Sstevel@tonic-gate 	ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
11130Sstevel@tonic-gate 	for (anon_idx = 0; npages != 0; anon_idx++, npages--) {
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 		anon_array_enter(amp, anon_idx, &cookie);
11160Sstevel@tonic-gate 		ap = anon_get_ptr(amp->ahp, anon_idx);
11172768Ssl108498 		ASSERT(ap != NULL);
11180Sstevel@tonic-gate 		swap_xlate(ap, &vp, &off);
11190Sstevel@tonic-gate 		anon_array_exit(&cookie);
11200Sstevel@tonic-gate 
11212768Ssl108498 		pp = page_lookup(vp, off, SE_SHARED);
11220Sstevel@tonic-gate 		if (pp == NULL) {
11230Sstevel@tonic-gate 			panic("lock_again: page not in the system");
11240Sstevel@tonic-gate 			/*NOTREACHED*/
11250Sstevel@tonic-gate 		}
11262768Ssl108498 		/* page should already be locked by caller */
11272768Ssl108498 		ASSERT(pp->p_lckcnt > 0);
11280Sstevel@tonic-gate 		(void) page_pp_lock(pp, 0, 0);
11290Sstevel@tonic-gate 		page_unlock(pp);
11300Sstevel@tonic-gate 	}
11310Sstevel@tonic-gate 	ANON_LOCK_EXIT(&amp->a_rwlock);
11322768Ssl108498 	mutex_exit(&sp->shm_mlock);
11330Sstevel@tonic-gate }
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate /*
11360Sstevel@tonic-gate  * Attach the shared memory segment to the process
11370Sstevel@tonic-gate  * address space and lock the pages.
11380Sstevel@tonic-gate  */
11390Sstevel@tonic-gate static int
11402768Ssl108498 shmem_lock(kshmid_t *sp, struct anon_map *amp)
11410Sstevel@tonic-gate {
11420Sstevel@tonic-gate 	size_t npages = btopr(amp->size);
11430Sstevel@tonic-gate 	struct as *as;
11440Sstevel@tonic-gate 	struct segvn_crargs crargs;
11452768Ssl108498 	uint_t error;
11460Sstevel@tonic-gate 
11472768Ssl108498 	/*
11482768Ssl108498 	 * A later ISM/DISM attach may increase the size of the amp, so
11492768Ssl108498 	 * cache the number of pages locked for the future shmem_unlock()
11502768Ssl108498 	 */
11512768Ssl108498 	sp->shm_lkpages = npages;
11520Sstevel@tonic-gate 
11532768Ssl108498 	as = as_alloc();
11540Sstevel@tonic-gate 	/* Initialize the create arguments and map the segment */
11550Sstevel@tonic-gate 	crargs = *(struct segvn_crargs *)zfod_argsp;	/* structure copy */
11560Sstevel@tonic-gate 	crargs.offset = (u_offset_t)0;
11570Sstevel@tonic-gate 	crargs.type = MAP_SHARED;
11580Sstevel@tonic-gate 	crargs.amp = amp;
11590Sstevel@tonic-gate 	crargs.prot = PROT_ALL;
11600Sstevel@tonic-gate 	crargs.maxprot = crargs.prot;
11610Sstevel@tonic-gate 	crargs.flags = 0;
11622768Ssl108498 	error = as_map(as, 0x0, amp->size, segvn_create, &crargs);
11630Sstevel@tonic-gate 	if (!error) {
11642768Ssl108498 		if ((error = as_ctl(as, 0x0, amp->size, MC_LOCK, 0, 0,
11650Sstevel@tonic-gate 			NULL, 0)) == 0) {
11662768Ssl108498 			lock_again(npages, sp, amp);
11670Sstevel@tonic-gate 		}
11682768Ssl108498 		(void) as_unmap(as, 0x0, amp->size);
11690Sstevel@tonic-gate 	}
11702768Ssl108498 	as_free(as);
11710Sstevel@tonic-gate 	return (error);
11720Sstevel@tonic-gate }
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate /*
11760Sstevel@tonic-gate  * Unlock shared memory
11770Sstevel@tonic-gate  */
11780Sstevel@tonic-gate static void
11792768Ssl108498 shmem_unlock(kshmid_t *sp, struct anon_map *amp)
11800Sstevel@tonic-gate {
11810Sstevel@tonic-gate 	struct anon *ap;
11822768Ssl108498 	pgcnt_t npages = sp->shm_lkpages;
11830Sstevel@tonic-gate 	struct vnode *vp;
11840Sstevel@tonic-gate 	struct page *pp;
11852768Ssl108498 	u_offset_t off;
11860Sstevel@tonic-gate 	ulong_t anon_idx;
11872768Ssl108498 	size_t unlocked_bytes = 0;
11882768Ssl108498 	kproject_t	*proj;
11892768Ssl108498 	anon_sync_obj_t cookie;
11900Sstevel@tonic-gate 
11912768Ssl108498 	proj = sp->shm_perm.ipc_proj;
11922768Ssl108498 	mutex_enter(&sp->shm_mlock);
11932768Ssl108498 	ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
11940Sstevel@tonic-gate 	for (anon_idx = 0; anon_idx < npages; anon_idx++) {
11950Sstevel@tonic-gate 
11962768Ssl108498 		anon_array_enter(amp, anon_idx, &cookie);
11970Sstevel@tonic-gate 		if ((ap = anon_get_ptr(amp->ahp, anon_idx)) == NULL) {
11982768Ssl108498 			panic("shmem_unlock: null app");
11992768Ssl108498 			/*NOTREACHED*/
12000Sstevel@tonic-gate 		}
12010Sstevel@tonic-gate 		swap_xlate(ap, &vp, &off);
12022768Ssl108498 		anon_array_exit(&cookie);
12030Sstevel@tonic-gate 		pp = page_lookup(vp, off, SE_SHARED);
12040Sstevel@tonic-gate 		if (pp == NULL) {
12052768Ssl108498 			panic("shmem_unlock: page not in the system");
12062768Ssl108498 			/*NOTREACHED*/
12070Sstevel@tonic-gate 		}
12082768Ssl108498 		/*
12092768Ssl108498 		 * Page should at least have once lock from previous
12102768Ssl108498 		 * shmem_lock
12112768Ssl108498 		 */
12122768Ssl108498 		ASSERT(pp->p_lckcnt > 0);
12132768Ssl108498 		page_pp_unlock(pp, 0, 0);
12142768Ssl108498 		if (pp->p_lckcnt == 0)
12152768Ssl108498 			unlocked_bytes += PAGESIZE;
12162768Ssl108498 
12170Sstevel@tonic-gate 		page_unlock(pp);
12180Sstevel@tonic-gate 	}
12192768Ssl108498 
12202768Ssl108498 	if (unlocked_bytes > 0) {
12212768Ssl108498 		rctl_decr_locked_mem(NULL, proj, unlocked_bytes, 0);
12222768Ssl108498 	}
12232768Ssl108498 
12242768Ssl108498 	ANON_LOCK_EXIT(&amp->a_rwlock);
12252768Ssl108498 	mutex_exit(&sp->shm_mlock);
12260Sstevel@tonic-gate }
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate /*
12290Sstevel@tonic-gate  * We call this routine when we have removed all references to this
12300Sstevel@tonic-gate  * amp.  This means all shmdt()s and the IPC_RMID have been done.
12310Sstevel@tonic-gate  */
12320Sstevel@tonic-gate static void
1233*3379Ssl108498 shm_rm_amp(kshmid_t *sp)
12340Sstevel@tonic-gate {
1235*3379Ssl108498 	struct anon_map *amp = sp->shm_amp;
1236*3379Ssl108498 	zone_t *zone;
1237*3379Ssl108498 
1238*3379Ssl108498 	/*
1239*3379Ssl108498 	 * This may return NULL if global zone is removing a shm created by
1240*3379Ssl108498 	 * a non-global zone that has been destroyed.
1241*3379Ssl108498 	 */
1242*3379Ssl108498 	zone = zone_find_by_id(sp->shm_perm.ipc_proj->kpj_zoneid);
12430Sstevel@tonic-gate 	/*
12440Sstevel@tonic-gate 	 * Free up the anon_map.
12450Sstevel@tonic-gate 	 */
12460Sstevel@tonic-gate 	lgrp_shm_policy_fini(amp, NULL);
12472414Saguzovsk 	if (amp->a_szc != 0) {
12482414Saguzovsk 		ANON_LOCK_ENTER(&amp->a_rwlock, RW_WRITER);
12492414Saguzovsk 		anon_shmap_free_pages(amp, 0, amp->size);
12502414Saguzovsk 		ANON_LOCK_EXIT(&amp->a_rwlock);
12512414Saguzovsk 	} else {
12522414Saguzovsk 		anon_free(amp->ahp, 0, amp->size);
12532414Saguzovsk 	}
1254*3379Ssl108498 	anon_unresv_zone(amp->swresv, zone);
1255*3379Ssl108498 	if (zone != NULL)
1256*3379Ssl108498 		zone_rele(zone);
12570Sstevel@tonic-gate 	anonmap_free(amp);
12580Sstevel@tonic-gate }
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate /*
12610Sstevel@tonic-gate  * Return the shared memory id for the process's virtual address.
12620Sstevel@tonic-gate  * Return SHMID_NONE if addr is not within a SysV shared memory segment.
12630Sstevel@tonic-gate  * Return SHMID_FREE if addr's SysV shared memory segment's id has been freed.
12640Sstevel@tonic-gate  *
12650Sstevel@tonic-gate  * shmgetid() is called from code in /proc with the process locked but
12660Sstevel@tonic-gate  * with pp->p_lock not held.  The address space lock is held, so we
12670Sstevel@tonic-gate  * cannot grab pp->p_lock here due to lock-ordering constraints.
12680Sstevel@tonic-gate  * Because of all this, modifications to the p_segacct list must only
12690Sstevel@tonic-gate  * be made after calling prbarrier() to ensure the process is not locked.
12700Sstevel@tonic-gate  * See shmdt() and sa_add(), above. shmgetid() may also be called on a
12710Sstevel@tonic-gate  * thread's own process without the process locked.
12720Sstevel@tonic-gate  */
12730Sstevel@tonic-gate int
12740Sstevel@tonic-gate shmgetid(proc_t *pp, caddr_t addr)
12750Sstevel@tonic-gate {
12760Sstevel@tonic-gate 	segacct_t *sap, template;
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&pp->p_lock));
12790Sstevel@tonic-gate 	ASSERT((pp->p_proc_flag & P_PR_LOCK) || pp == curproc);
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 	if (pp->p_segacct == NULL)
12820Sstevel@tonic-gate 		return (SHMID_NONE);
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate 	template.sa_addr = addr;
12850Sstevel@tonic-gate 	template.sa_len = 0;
12860Sstevel@tonic-gate 	if ((sap = avl_find(pp->p_segacct, &template, NULL)) == NULL)
12870Sstevel@tonic-gate 		return (SHMID_NONE);
12880Sstevel@tonic-gate 
12890Sstevel@tonic-gate 	if (IPC_FREE(&sap->sa_id->shm_perm))
12900Sstevel@tonic-gate 		return (SHMID_FREE);
12910Sstevel@tonic-gate 
12920Sstevel@tonic-gate 	return (sap->sa_id->shm_perm.ipc_id);
12930Sstevel@tonic-gate }
1294