xref: /onnv-gate/usr/src/uts/common/vm/seg_vn.c (revision 670)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
320Sstevel@tonic-gate  * The Regents of the University of California
330Sstevel@tonic-gate  * All Rights Reserved
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
360Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
370Sstevel@tonic-gate  * contributors.
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate  * VM - shared or copy-on-write from a vnode/anonymous memory.
440Sstevel@tonic-gate  */
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #include <sys/types.h>
470Sstevel@tonic-gate #include <sys/param.h>
480Sstevel@tonic-gate #include <sys/t_lock.h>
490Sstevel@tonic-gate #include <sys/errno.h>
500Sstevel@tonic-gate #include <sys/systm.h>
510Sstevel@tonic-gate #include <sys/mman.h>
520Sstevel@tonic-gate #include <sys/debug.h>
530Sstevel@tonic-gate #include <sys/cred.h>
540Sstevel@tonic-gate #include <sys/vmsystm.h>
550Sstevel@tonic-gate #include <sys/tuneable.h>
560Sstevel@tonic-gate #include <sys/bitmap.h>
570Sstevel@tonic-gate #include <sys/swap.h>
580Sstevel@tonic-gate #include <sys/kmem.h>
590Sstevel@tonic-gate #include <sys/sysmacros.h>
600Sstevel@tonic-gate #include <sys/vtrace.h>
610Sstevel@tonic-gate #include <sys/cmn_err.h>
620Sstevel@tonic-gate #include <sys/vm.h>
630Sstevel@tonic-gate #include <sys/dumphdr.h>
640Sstevel@tonic-gate #include <sys/lgrp.h>
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #include <vm/hat.h>
670Sstevel@tonic-gate #include <vm/as.h>
680Sstevel@tonic-gate #include <vm/seg.h>
690Sstevel@tonic-gate #include <vm/seg_vn.h>
700Sstevel@tonic-gate #include <vm/pvn.h>
710Sstevel@tonic-gate #include <vm/anon.h>
720Sstevel@tonic-gate #include <vm/page.h>
730Sstevel@tonic-gate #include <vm/vpage.h>
740Sstevel@tonic-gate 
750Sstevel@tonic-gate /*
760Sstevel@tonic-gate  * Private seg op routines.
770Sstevel@tonic-gate  */
780Sstevel@tonic-gate static int	segvn_dup(struct seg *seg, struct seg *newseg);
790Sstevel@tonic-gate static int	segvn_unmap(struct seg *seg, caddr_t addr, size_t len);
800Sstevel@tonic-gate static void	segvn_free(struct seg *seg);
810Sstevel@tonic-gate static faultcode_t segvn_fault(struct hat *hat, struct seg *seg,
820Sstevel@tonic-gate 		    caddr_t addr, size_t len, enum fault_type type,
830Sstevel@tonic-gate 		    enum seg_rw rw);
840Sstevel@tonic-gate static faultcode_t segvn_faulta(struct seg *seg, caddr_t addr);
850Sstevel@tonic-gate static int	segvn_setprot(struct seg *seg, caddr_t addr,
860Sstevel@tonic-gate 		    size_t len, uint_t prot);
870Sstevel@tonic-gate static int	segvn_checkprot(struct seg *seg, caddr_t addr,
880Sstevel@tonic-gate 		    size_t len, uint_t prot);
890Sstevel@tonic-gate static int	segvn_kluster(struct seg *seg, caddr_t addr, ssize_t delta);
900Sstevel@tonic-gate static size_t	segvn_swapout(struct seg *seg);
910Sstevel@tonic-gate static int	segvn_sync(struct seg *seg, caddr_t addr, size_t len,
920Sstevel@tonic-gate 		    int attr, uint_t flags);
930Sstevel@tonic-gate static size_t	segvn_incore(struct seg *seg, caddr_t addr, size_t len,
940Sstevel@tonic-gate 		    char *vec);
950Sstevel@tonic-gate static int	segvn_lockop(struct seg *seg, caddr_t addr, size_t len,
960Sstevel@tonic-gate 		    int attr, int op, ulong_t *lockmap, size_t pos);
970Sstevel@tonic-gate static int	segvn_getprot(struct seg *seg, caddr_t addr, size_t len,
980Sstevel@tonic-gate 		    uint_t *protv);
990Sstevel@tonic-gate static u_offset_t	segvn_getoffset(struct seg *seg, caddr_t addr);
1000Sstevel@tonic-gate static int	segvn_gettype(struct seg *seg, caddr_t addr);
1010Sstevel@tonic-gate static int	segvn_getvp(struct seg *seg, caddr_t addr,
1020Sstevel@tonic-gate 		    struct vnode **vpp);
1030Sstevel@tonic-gate static int	segvn_advise(struct seg *seg, caddr_t addr, size_t len,
1040Sstevel@tonic-gate 		    uint_t behav);
1050Sstevel@tonic-gate static void	segvn_dump(struct seg *seg);
1060Sstevel@tonic-gate static int	segvn_pagelock(struct seg *seg, caddr_t addr, size_t len,
1070Sstevel@tonic-gate 		    struct page ***ppp, enum lock_type type, enum seg_rw rw);
1080Sstevel@tonic-gate static int	segvn_setpagesize(struct seg *seg, caddr_t addr, size_t len,
1090Sstevel@tonic-gate 		    uint_t szc);
1100Sstevel@tonic-gate static int	segvn_getmemid(struct seg *seg, caddr_t addr,
1110Sstevel@tonic-gate 		    memid_t *memidp);
1120Sstevel@tonic-gate static lgrp_mem_policy_info_t	*segvn_getpolicy(struct seg *, caddr_t);
113*670Selowe static int	segvn_capable(struct seg *seg, segcapability_t capable);
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate struct	seg_ops segvn_ops = {
1160Sstevel@tonic-gate 	segvn_dup,
1170Sstevel@tonic-gate 	segvn_unmap,
1180Sstevel@tonic-gate 	segvn_free,
1190Sstevel@tonic-gate 	segvn_fault,
1200Sstevel@tonic-gate 	segvn_faulta,
1210Sstevel@tonic-gate 	segvn_setprot,
1220Sstevel@tonic-gate 	segvn_checkprot,
1230Sstevel@tonic-gate 	segvn_kluster,
1240Sstevel@tonic-gate 	segvn_swapout,
1250Sstevel@tonic-gate 	segvn_sync,
1260Sstevel@tonic-gate 	segvn_incore,
1270Sstevel@tonic-gate 	segvn_lockop,
1280Sstevel@tonic-gate 	segvn_getprot,
1290Sstevel@tonic-gate 	segvn_getoffset,
1300Sstevel@tonic-gate 	segvn_gettype,
1310Sstevel@tonic-gate 	segvn_getvp,
1320Sstevel@tonic-gate 	segvn_advise,
1330Sstevel@tonic-gate 	segvn_dump,
1340Sstevel@tonic-gate 	segvn_pagelock,
1350Sstevel@tonic-gate 	segvn_setpagesize,
1360Sstevel@tonic-gate 	segvn_getmemid,
1370Sstevel@tonic-gate 	segvn_getpolicy,
138*670Selowe 	segvn_capable,
1390Sstevel@tonic-gate };
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate /*
1420Sstevel@tonic-gate  * Common zfod structures, provided as a shorthand for others to use.
1430Sstevel@tonic-gate  */
1440Sstevel@tonic-gate static segvn_crargs_t zfod_segvn_crargs =
1450Sstevel@tonic-gate 	SEGVN_ZFOD_ARGS(PROT_ZFOD, PROT_ALL);
1460Sstevel@tonic-gate static segvn_crargs_t kzfod_segvn_crargs =
1470Sstevel@tonic-gate 	SEGVN_ZFOD_ARGS(PROT_ZFOD & ~PROT_USER,
1480Sstevel@tonic-gate 	PROT_ALL & ~PROT_USER);
1490Sstevel@tonic-gate static segvn_crargs_t stack_noexec_crargs =
1500Sstevel@tonic-gate 	SEGVN_ZFOD_ARGS(PROT_ZFOD & ~PROT_EXEC, PROT_ALL);
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate caddr_t	zfod_argsp = (caddr_t)&zfod_segvn_crargs;	/* user zfod argsp */
1530Sstevel@tonic-gate caddr_t	kzfod_argsp = (caddr_t)&kzfod_segvn_crargs;	/* kernel zfod argsp */
1540Sstevel@tonic-gate caddr_t	stack_exec_argsp = (caddr_t)&zfod_segvn_crargs;	/* executable stack */
1550Sstevel@tonic-gate caddr_t	stack_noexec_argsp = (caddr_t)&stack_noexec_crargs; /* noexec stack */
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate #define	vpgtob(n)	((n) * sizeof (struct vpage))	/* For brevity */
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate size_t	segvn_comb_thrshld = UINT_MAX;	/* patchable -- see 1196681 */
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate static int	segvn_concat(struct seg *, struct seg *, int);
1620Sstevel@tonic-gate static int	segvn_extend_prev(struct seg *, struct seg *,
1630Sstevel@tonic-gate 		    struct segvn_crargs *, size_t);
1640Sstevel@tonic-gate static int	segvn_extend_next(struct seg *, struct seg *,
1650Sstevel@tonic-gate 		    struct segvn_crargs *, size_t);
1660Sstevel@tonic-gate static void	segvn_softunlock(struct seg *, caddr_t, size_t, enum seg_rw);
1670Sstevel@tonic-gate static void	segvn_pagelist_rele(page_t **);
1680Sstevel@tonic-gate static void	segvn_setvnode_mpss(vnode_t *);
1690Sstevel@tonic-gate static void	segvn_relocate_pages(page_t **, page_t *);
1700Sstevel@tonic-gate static int	segvn_full_szcpages(page_t **, uint_t, int *, uint_t *);
1710Sstevel@tonic-gate static int	segvn_fill_vp_pages(struct segvn_data *, vnode_t *, u_offset_t,
1720Sstevel@tonic-gate     uint_t, page_t **, page_t **, uint_t *, int *);
1730Sstevel@tonic-gate static faultcode_t segvn_fault_vnodepages(struct hat *, struct seg *, caddr_t,
1740Sstevel@tonic-gate     caddr_t, enum fault_type, enum seg_rw, caddr_t, caddr_t, int);
1750Sstevel@tonic-gate static faultcode_t segvn_fault_anonpages(struct hat *, struct seg *, caddr_t,
1760Sstevel@tonic-gate     caddr_t, enum fault_type, enum seg_rw, caddr_t, caddr_t, int);
1770Sstevel@tonic-gate static faultcode_t segvn_faultpage(struct hat *, struct seg *, caddr_t,
1780Sstevel@tonic-gate     u_offset_t, struct vpage *, page_t **, uint_t,
1790Sstevel@tonic-gate     enum fault_type, enum seg_rw, int);
1800Sstevel@tonic-gate static void	segvn_vpage(struct seg *);
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate static void segvn_purge(struct seg *seg);
1830Sstevel@tonic-gate static int segvn_reclaim(struct seg *, caddr_t, size_t, struct page **,
1840Sstevel@tonic-gate     enum seg_rw);
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate static int sameprot(struct seg *, caddr_t, size_t);
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate static int segvn_demote_range(struct seg *, caddr_t, size_t, int);
1890Sstevel@tonic-gate static int segvn_clrszc(struct seg *);
1900Sstevel@tonic-gate static struct seg *segvn_split_seg(struct seg *, caddr_t);
1910Sstevel@tonic-gate static int segvn_claim_pages(struct seg *, struct vpage *, u_offset_t,
1920Sstevel@tonic-gate     ulong_t, uint_t);
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate static struct kmem_cache *segvn_cache;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate #ifdef VM_STATS
1970Sstevel@tonic-gate static struct segvnvmstats_str {
1980Sstevel@tonic-gate 	ulong_t	fill_vp_pages[31];
1990Sstevel@tonic-gate 	ulong_t fltvnpages[49];
2000Sstevel@tonic-gate 	ulong_t	fullszcpages[10];
2010Sstevel@tonic-gate 	ulong_t	relocatepages[3];
2020Sstevel@tonic-gate 	ulong_t	fltanpages[17];
2030Sstevel@tonic-gate 	ulong_t pagelock[3];
2040Sstevel@tonic-gate 	ulong_t	demoterange[3];
2050Sstevel@tonic-gate } segvnvmstats;
2060Sstevel@tonic-gate #endif /* VM_STATS */
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate #define	SDR_RANGE	1		/* demote entire range */
2090Sstevel@tonic-gate #define	SDR_END		2		/* demote non aligned ends only */
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate #define	CALC_LPG_REGION(pgsz, seg, addr, len, lpgaddr, lpgeaddr) {            \
2120Sstevel@tonic-gate 		if ((len) != 0) { 		      	      		      \
2130Sstevel@tonic-gate 			lpgaddr = (caddr_t)P2ALIGN((uintptr_t)(addr), pgsz);  \
2140Sstevel@tonic-gate 			ASSERT(lpgaddr >= (seg)->s_base);	      	      \
2150Sstevel@tonic-gate 			lpgeaddr = (caddr_t)P2ROUNDUP((uintptr_t)((addr) +    \
2160Sstevel@tonic-gate 			    (len)), pgsz);				      \
2170Sstevel@tonic-gate 			ASSERT(lpgeaddr > lpgaddr);		      	      \
2180Sstevel@tonic-gate 			ASSERT(lpgeaddr <= (seg)->s_base + (seg)->s_size);    \
2190Sstevel@tonic-gate 		} else {					      	      \
2200Sstevel@tonic-gate 			lpgeaddr = lpgaddr = (addr);	      		      \
2210Sstevel@tonic-gate 		}							      \
2220Sstevel@tonic-gate 	}
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate /*ARGSUSED*/
2250Sstevel@tonic-gate static int
2260Sstevel@tonic-gate segvn_cache_constructor(void *buf, void *cdrarg, int kmflags)
2270Sstevel@tonic-gate {
2280Sstevel@tonic-gate 	struct segvn_data *svd = buf;
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	rw_init(&svd->lock, NULL, RW_DEFAULT, NULL);
2310Sstevel@tonic-gate 	mutex_init(&svd->segp_slock, NULL, MUTEX_DEFAULT, NULL);
2320Sstevel@tonic-gate 	return (0);
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate /*ARGSUSED1*/
2360Sstevel@tonic-gate static void
2370Sstevel@tonic-gate segvn_cache_destructor(void *buf, void *cdrarg)
2380Sstevel@tonic-gate {
2390Sstevel@tonic-gate 	struct segvn_data *svd = buf;
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	rw_destroy(&svd->lock);
2420Sstevel@tonic-gate 	mutex_destroy(&svd->segp_slock);
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate /*
2460Sstevel@tonic-gate  * Patching this variable to non-zero allows the system to run with
2470Sstevel@tonic-gate  * stacks marked as "not executable".  It's a bit of a kludge, but is
2480Sstevel@tonic-gate  * provided as a tweakable for platforms that export those ABIs
2490Sstevel@tonic-gate  * (e.g. sparc V8) that have executable stacks enabled by default.
2500Sstevel@tonic-gate  * There are also some restrictions for platforms that don't actually
2510Sstevel@tonic-gate  * implement 'noexec' protections.
2520Sstevel@tonic-gate  *
2530Sstevel@tonic-gate  * Once enabled, the system is (therefore) unable to provide a fully
2540Sstevel@tonic-gate  * ABI-compliant execution environment, though practically speaking,
2550Sstevel@tonic-gate  * most everything works.  The exceptions are generally some interpreters
2560Sstevel@tonic-gate  * and debuggers that create executable code on the stack and jump
2570Sstevel@tonic-gate  * into it (without explicitly mprotecting the address range to include
2580Sstevel@tonic-gate  * PROT_EXEC).
2590Sstevel@tonic-gate  *
2600Sstevel@tonic-gate  * One important class of applications that are disabled are those
2610Sstevel@tonic-gate  * that have been transformed into malicious agents using one of the
2620Sstevel@tonic-gate  * numerous "buffer overflow" attacks.  See 4007890.
2630Sstevel@tonic-gate  */
2640Sstevel@tonic-gate int noexec_user_stack = 0;
2650Sstevel@tonic-gate int noexec_user_stack_log = 1;
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate int segvn_lpg_disable = 0;
2680Sstevel@tonic-gate uint_t segvn_maxpgszc = 0;
2690Sstevel@tonic-gate 
27063Saguzovsk ulong_t segvn_vmpss_clrszc_cnt;
27163Saguzovsk ulong_t segvn_vmpss_clrszc_err;
27263Saguzovsk ulong_t segvn_fltvnpages_clrszc_cnt;
2730Sstevel@tonic-gate ulong_t segvn_fltvnpages_clrszc_err;
2740Sstevel@tonic-gate ulong_t segvn_setpgsz_align_err;
2750Sstevel@tonic-gate ulong_t segvn_setpgsz_getattr_err;
2760Sstevel@tonic-gate ulong_t segvn_setpgsz_eof_err;
2770Sstevel@tonic-gate ulong_t segvn_faultvnmpss_align_err1;
2780Sstevel@tonic-gate ulong_t segvn_faultvnmpss_align_err2;
2790Sstevel@tonic-gate ulong_t segvn_faultvnmpss_align_err3;
2800Sstevel@tonic-gate ulong_t segvn_faultvnmpss_align_err4;
2810Sstevel@tonic-gate ulong_t segvn_faultvnmpss_align_err5;
2820Sstevel@tonic-gate ulong_t	segvn_vmpss_pageio_deadlk_err;
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate /*
2850Sstevel@tonic-gate  * Initialize segvn data structures
2860Sstevel@tonic-gate  */
2870Sstevel@tonic-gate void
2880Sstevel@tonic-gate segvn_init(void)
2890Sstevel@tonic-gate {
2900Sstevel@tonic-gate 	uint_t maxszc;
2910Sstevel@tonic-gate 	uint_t szc;
2920Sstevel@tonic-gate 	size_t pgsz;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	segvn_cache = kmem_cache_create("segvn_cache",
2950Sstevel@tonic-gate 		sizeof (struct segvn_data), 0,
2960Sstevel@tonic-gate 		segvn_cache_constructor, segvn_cache_destructor, NULL,
2970Sstevel@tonic-gate 		NULL, NULL, 0);
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	if (segvn_lpg_disable != 0)
3000Sstevel@tonic-gate 		return;
3010Sstevel@tonic-gate 	szc = maxszc = page_num_pagesizes() - 1;
3020Sstevel@tonic-gate 	if (szc == 0) {
3030Sstevel@tonic-gate 		segvn_lpg_disable = 1;
3040Sstevel@tonic-gate 		return;
3050Sstevel@tonic-gate 	}
3060Sstevel@tonic-gate 	if (page_get_pagesize(0) != PAGESIZE) {
3070Sstevel@tonic-gate 		panic("segvn_init: bad szc 0");
3080Sstevel@tonic-gate 		/*NOTREACHED*/
3090Sstevel@tonic-gate 	}
3100Sstevel@tonic-gate 	while (szc != 0) {
3110Sstevel@tonic-gate 		pgsz = page_get_pagesize(szc);
3120Sstevel@tonic-gate 		if (pgsz <= PAGESIZE || !IS_P2ALIGNED(pgsz, pgsz)) {
3130Sstevel@tonic-gate 			panic("segvn_init: bad szc %d", szc);
3140Sstevel@tonic-gate 			/*NOTREACHED*/
3150Sstevel@tonic-gate 		}
3160Sstevel@tonic-gate 		szc--;
3170Sstevel@tonic-gate 	}
3180Sstevel@tonic-gate 	if (segvn_maxpgszc == 0 || segvn_maxpgszc > maxszc)
3190Sstevel@tonic-gate 		segvn_maxpgszc = maxszc;
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate #define	SEGVN_PAGEIO	((void *)0x1)
3230Sstevel@tonic-gate #define	SEGVN_NOPAGEIO	((void *)0x2)
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate static void
3260Sstevel@tonic-gate segvn_setvnode_mpss(vnode_t *vp)
3270Sstevel@tonic-gate {
3280Sstevel@tonic-gate 	int err;
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	ASSERT(vp->v_mpssdata == NULL ||
3310Sstevel@tonic-gate 	    vp->v_mpssdata == SEGVN_PAGEIO ||
3320Sstevel@tonic-gate 	    vp->v_mpssdata == SEGVN_NOPAGEIO);
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 	if (vp->v_mpssdata == NULL) {
3350Sstevel@tonic-gate 		if (vn_vmpss_usepageio(vp)) {
3360Sstevel@tonic-gate 			err = VOP_PAGEIO(vp, (page_t *)NULL,
3370Sstevel@tonic-gate 			    (u_offset_t)0, 0, 0, CRED());
3380Sstevel@tonic-gate 		} else {
3390Sstevel@tonic-gate 			err = ENOSYS;
3400Sstevel@tonic-gate 		}
3410Sstevel@tonic-gate 		/*
3420Sstevel@tonic-gate 		 * set v_mpssdata just once per vnode life
3430Sstevel@tonic-gate 		 * so that it never changes.
3440Sstevel@tonic-gate 		 */
3450Sstevel@tonic-gate 		mutex_enter(&vp->v_lock);
3460Sstevel@tonic-gate 		if (vp->v_mpssdata == NULL) {
3470Sstevel@tonic-gate 			if (err == EINVAL) {
3480Sstevel@tonic-gate 				vp->v_mpssdata = SEGVN_PAGEIO;
3490Sstevel@tonic-gate 			} else {
3500Sstevel@tonic-gate 				vp->v_mpssdata = SEGVN_NOPAGEIO;
3510Sstevel@tonic-gate 			}
3520Sstevel@tonic-gate 		}
3530Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
3540Sstevel@tonic-gate 	}
3550Sstevel@tonic-gate }
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate int
3580Sstevel@tonic-gate segvn_create(struct seg *seg, void *argsp)
3590Sstevel@tonic-gate {
3600Sstevel@tonic-gate 	struct segvn_crargs *a = (struct segvn_crargs *)argsp;
3610Sstevel@tonic-gate 	struct segvn_data *svd;
3620Sstevel@tonic-gate 	size_t swresv = 0;
3630Sstevel@tonic-gate 	struct cred *cred;
3640Sstevel@tonic-gate 	struct anon_map *amp;
3650Sstevel@tonic-gate 	int error = 0;
3660Sstevel@tonic-gate 	size_t pgsz;
3670Sstevel@tonic-gate 	lgrp_mem_policy_t mpolicy = LGRP_MEM_POLICY_DEFAULT;
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	if (a->type != MAP_PRIVATE && a->type != MAP_SHARED) {
3730Sstevel@tonic-gate 		panic("segvn_create type");
3740Sstevel@tonic-gate 		/*NOTREACHED*/
3750Sstevel@tonic-gate 	}
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	/*
3780Sstevel@tonic-gate 	 * Check arguments.  If a shared anon structure is given then
3790Sstevel@tonic-gate 	 * it is illegal to also specify a vp.
3800Sstevel@tonic-gate 	 */
3810Sstevel@tonic-gate 	if (a->amp != NULL && a->vp != NULL) {
3820Sstevel@tonic-gate 		panic("segvn_create anon_map");
3830Sstevel@tonic-gate 		/*NOTREACHED*/
3840Sstevel@tonic-gate 	}
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 	/* MAP_NORESERVE on a MAP_SHARED segment is meaningless. */
3870Sstevel@tonic-gate 	if (a->type == MAP_SHARED)
3880Sstevel@tonic-gate 		a->flags &= ~MAP_NORESERVE;
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	if (a->szc != 0) {
3910Sstevel@tonic-gate 		if (segvn_lpg_disable != 0 || a->amp != NULL ||
3920Sstevel@tonic-gate 		    (a->type == MAP_SHARED && a->vp == NULL) ||
3930Sstevel@tonic-gate 		    (a->flags & MAP_NORESERVE) || seg->s_as == &kas) {
3940Sstevel@tonic-gate 			a->szc = 0;
3950Sstevel@tonic-gate 		} else {
3960Sstevel@tonic-gate 			if (a->szc > segvn_maxpgszc)
3970Sstevel@tonic-gate 				a->szc = segvn_maxpgszc;
3980Sstevel@tonic-gate 			pgsz = page_get_pagesize(a->szc);
3990Sstevel@tonic-gate 			if (!IS_P2ALIGNED(seg->s_base, pgsz) ||
4000Sstevel@tonic-gate 			    !IS_P2ALIGNED(seg->s_size, pgsz)) {
4010Sstevel@tonic-gate 				a->szc = 0;
4020Sstevel@tonic-gate 			} else if (a->vp != NULL) {
4030Sstevel@tonic-gate 				extern struct vnode kvp;
4040Sstevel@tonic-gate 				if (IS_SWAPFSVP(a->vp) || a->vp == &kvp) {
4050Sstevel@tonic-gate 					/*
4060Sstevel@tonic-gate 					 * paranoid check.
4070Sstevel@tonic-gate 					 * hat_page_demote() is not supported
4080Sstevel@tonic-gate 					 * on swapfs pages.
4090Sstevel@tonic-gate 					 */
4100Sstevel@tonic-gate 					a->szc = 0;
4110Sstevel@tonic-gate 				} else if (map_addr_vacalign_check(seg->s_base,
4120Sstevel@tonic-gate 				    a->offset & PAGEMASK)) {
4130Sstevel@tonic-gate 					a->szc = 0;
4140Sstevel@tonic-gate 				}
4150Sstevel@tonic-gate 			}
4160Sstevel@tonic-gate 		}
4170Sstevel@tonic-gate 	}
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	/*
4200Sstevel@tonic-gate 	 * If segment may need private pages, reserve them now.
4210Sstevel@tonic-gate 	 */
4220Sstevel@tonic-gate 	if (!(a->flags & MAP_NORESERVE) && ((a->vp == NULL && a->amp == NULL) ||
4230Sstevel@tonic-gate 	    (a->type == MAP_PRIVATE && (a->prot & PROT_WRITE)))) {
4240Sstevel@tonic-gate 		if (anon_resv(seg->s_size) == 0)
4250Sstevel@tonic-gate 			return (EAGAIN);
4260Sstevel@tonic-gate 		swresv = seg->s_size;
4270Sstevel@tonic-gate 		TRACE_3(TR_FAC_VM, TR_ANON_PROC, "anon proc:%p %lu %u",
4280Sstevel@tonic-gate 			seg, swresv, 1);
4290Sstevel@tonic-gate 	}
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	/*
4320Sstevel@tonic-gate 	 * Reserve any mapping structures that may be required.
4330Sstevel@tonic-gate 	 */
4340Sstevel@tonic-gate 	hat_map(seg->s_as->a_hat, seg->s_base, seg->s_size, HAT_MAP);
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	if (a->cred) {
4370Sstevel@tonic-gate 		cred = a->cred;
4380Sstevel@tonic-gate 		crhold(cred);
4390Sstevel@tonic-gate 	} else {
4400Sstevel@tonic-gate 		crhold(cred = CRED());
4410Sstevel@tonic-gate 	}
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	/* Inform the vnode of the new mapping */
4440Sstevel@tonic-gate 	if (a->vp) {
4450Sstevel@tonic-gate 		error = VOP_ADDMAP(a->vp, a->offset & PAGEMASK,
4460Sstevel@tonic-gate 		    seg->s_as, seg->s_base, seg->s_size, a->prot,
4470Sstevel@tonic-gate 		    a->maxprot, a->type, cred);
4480Sstevel@tonic-gate 		if (error) {
4490Sstevel@tonic-gate 			if (swresv != 0) {
4500Sstevel@tonic-gate 				anon_unresv(swresv);
4510Sstevel@tonic-gate 				TRACE_3(TR_FAC_VM, TR_ANON_PROC,
4520Sstevel@tonic-gate 					"anon proc:%p %lu %u",
4530Sstevel@tonic-gate 					seg, swresv, 0);
4540Sstevel@tonic-gate 			}
4550Sstevel@tonic-gate 			crfree(cred);
4560Sstevel@tonic-gate 			hat_unload(seg->s_as->a_hat, seg->s_base,
4570Sstevel@tonic-gate 				seg->s_size, HAT_UNLOAD_UNMAP);
4580Sstevel@tonic-gate 			return (error);
4590Sstevel@tonic-gate 		}
4600Sstevel@tonic-gate 	}
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	/*
4630Sstevel@tonic-gate 	 * If more than one segment in the address space, and
4640Sstevel@tonic-gate 	 * they're adjacent virtually, try to concatenate them.
4650Sstevel@tonic-gate 	 * Don't concatenate if an explicit anon_map structure
4660Sstevel@tonic-gate 	 * was supplied (e.g., SystemV shared memory).
4670Sstevel@tonic-gate 	 */
4680Sstevel@tonic-gate 	if (a->amp == NULL) {
4690Sstevel@tonic-gate 		struct seg *pseg, *nseg;
4700Sstevel@tonic-gate 		struct segvn_data *psvd, *nsvd;
4710Sstevel@tonic-gate 		lgrp_mem_policy_t ppolicy, npolicy;
4720Sstevel@tonic-gate 		uint_t	lgrp_mem_policy_flags = 0;
4730Sstevel@tonic-gate 		extern lgrp_mem_policy_t lgrp_mem_default_policy;
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 		/*
4760Sstevel@tonic-gate 		 * Memory policy flags (lgrp_mem_policy_flags) is valid when
4770Sstevel@tonic-gate 		 * extending stack/heap segments.
4780Sstevel@tonic-gate 		 */
4790Sstevel@tonic-gate 		if ((a->vp == NULL) && (a->type == MAP_PRIVATE) &&
4800Sstevel@tonic-gate 			!(a->flags & MAP_NORESERVE) && (seg->s_as != &kas)) {
4810Sstevel@tonic-gate 			lgrp_mem_policy_flags = a->lgrp_mem_policy_flags;
4820Sstevel@tonic-gate 		} else {
4830Sstevel@tonic-gate 			/*
4840Sstevel@tonic-gate 			 * Get policy when not extending it from another segment
4850Sstevel@tonic-gate 			 */
4860Sstevel@tonic-gate 			mpolicy = lgrp_mem_policy_default(seg->s_size, a->type);
4870Sstevel@tonic-gate 		}
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 		/*
4900Sstevel@tonic-gate 		 * First, try to concatenate the previous and new segments
4910Sstevel@tonic-gate 		 */
4920Sstevel@tonic-gate 		pseg = AS_SEGPREV(seg->s_as, seg);
4930Sstevel@tonic-gate 		if (pseg != NULL &&
4940Sstevel@tonic-gate 		    pseg->s_base + pseg->s_size == seg->s_base &&
4950Sstevel@tonic-gate 		    pseg->s_ops == &segvn_ops) {
4960Sstevel@tonic-gate 			/*
4970Sstevel@tonic-gate 			 * Get memory allocation policy from previous segment.
4980Sstevel@tonic-gate 			 * When extension is specified (e.g. for heap) apply
4990Sstevel@tonic-gate 			 * this policy to the new segment regardless of the
5000Sstevel@tonic-gate 			 * outcome of segment concatenation.  Extension occurs
5010Sstevel@tonic-gate 			 * for non-default policy otherwise default policy is
5020Sstevel@tonic-gate 			 * used and is based on extended segment size.
5030Sstevel@tonic-gate 			 */
5040Sstevel@tonic-gate 			psvd = (struct segvn_data *)pseg->s_data;
5050Sstevel@tonic-gate 			ppolicy = psvd->policy_info.mem_policy;
5060Sstevel@tonic-gate 			if (lgrp_mem_policy_flags ==
5070Sstevel@tonic-gate 			    LGRP_MP_FLAG_EXTEND_UP) {
5080Sstevel@tonic-gate 				if (ppolicy != lgrp_mem_default_policy) {
5090Sstevel@tonic-gate 					mpolicy = ppolicy;
5100Sstevel@tonic-gate 				} else {
5110Sstevel@tonic-gate 					mpolicy = lgrp_mem_policy_default(
5120Sstevel@tonic-gate 					    pseg->s_size + seg->s_size,
5130Sstevel@tonic-gate 					    a->type);
5140Sstevel@tonic-gate 				}
5150Sstevel@tonic-gate 			}
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 			if (mpolicy == ppolicy &&
5180Sstevel@tonic-gate 			    (pseg->s_size + seg->s_size <=
5190Sstevel@tonic-gate 			    segvn_comb_thrshld || psvd->amp == NULL) &&
5200Sstevel@tonic-gate 			    segvn_extend_prev(pseg, seg, a, swresv) == 0) {
5210Sstevel@tonic-gate 				/*
5220Sstevel@tonic-gate 				 * success! now try to concatenate
5230Sstevel@tonic-gate 				 * with following seg
5240Sstevel@tonic-gate 				 */
5250Sstevel@tonic-gate 				crfree(cred);
5260Sstevel@tonic-gate 				nseg = AS_SEGNEXT(pseg->s_as, pseg);
5270Sstevel@tonic-gate 				if (nseg != NULL &&
5280Sstevel@tonic-gate 				    nseg != pseg &&
5290Sstevel@tonic-gate 				    nseg->s_ops == &segvn_ops &&
5300Sstevel@tonic-gate 				    pseg->s_base + pseg->s_size ==
5310Sstevel@tonic-gate 				    nseg->s_base)
5320Sstevel@tonic-gate 					(void) segvn_concat(pseg, nseg, 0);
5330Sstevel@tonic-gate 				ASSERT(pseg->s_szc == 0 ||
5340Sstevel@tonic-gate 				    (a->szc == pseg->s_szc &&
5350Sstevel@tonic-gate 				    IS_P2ALIGNED(pseg->s_base, pgsz) &&
5360Sstevel@tonic-gate 				    IS_P2ALIGNED(pseg->s_size, pgsz)));
5370Sstevel@tonic-gate 				return (0);
5380Sstevel@tonic-gate 			}
5390Sstevel@tonic-gate 		}
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 		/*
5420Sstevel@tonic-gate 		 * Failed, so try to concatenate with following seg
5430Sstevel@tonic-gate 		 */
5440Sstevel@tonic-gate 		nseg = AS_SEGNEXT(seg->s_as, seg);
5450Sstevel@tonic-gate 		if (nseg != NULL &&
5460Sstevel@tonic-gate 		    seg->s_base + seg->s_size == nseg->s_base &&
5470Sstevel@tonic-gate 		    nseg->s_ops == &segvn_ops) {
5480Sstevel@tonic-gate 			/*
5490Sstevel@tonic-gate 			 * Get memory allocation policy from next segment.
5500Sstevel@tonic-gate 			 * When extension is specified (e.g. for stack) apply
5510Sstevel@tonic-gate 			 * this policy to the new segment regardless of the
5520Sstevel@tonic-gate 			 * outcome of segment concatenation.  Extension occurs
5530Sstevel@tonic-gate 			 * for non-default policy otherwise default policy is
5540Sstevel@tonic-gate 			 * used and is based on extended segment size.
5550Sstevel@tonic-gate 			 */
5560Sstevel@tonic-gate 			nsvd = (struct segvn_data *)nseg->s_data;
5570Sstevel@tonic-gate 			npolicy = nsvd->policy_info.mem_policy;
5580Sstevel@tonic-gate 			if (lgrp_mem_policy_flags ==
5590Sstevel@tonic-gate 			    LGRP_MP_FLAG_EXTEND_DOWN) {
5600Sstevel@tonic-gate 				if (npolicy != lgrp_mem_default_policy) {
5610Sstevel@tonic-gate 					mpolicy = npolicy;
5620Sstevel@tonic-gate 				} else {
5630Sstevel@tonic-gate 					mpolicy = lgrp_mem_policy_default(
5640Sstevel@tonic-gate 					    nseg->s_size + seg->s_size,
5650Sstevel@tonic-gate 					    a->type);
5660Sstevel@tonic-gate 				}
5670Sstevel@tonic-gate 			}
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 			if (mpolicy == npolicy &&
5700Sstevel@tonic-gate 			    segvn_extend_next(seg, nseg, a, swresv) == 0) {
5710Sstevel@tonic-gate 				crfree(cred);
5720Sstevel@tonic-gate 				ASSERT(nseg->s_szc == 0 ||
5730Sstevel@tonic-gate 				    (a->szc == nseg->s_szc &&
5740Sstevel@tonic-gate 				    IS_P2ALIGNED(nseg->s_base, pgsz) &&
5750Sstevel@tonic-gate 				    IS_P2ALIGNED(nseg->s_size, pgsz)));
5760Sstevel@tonic-gate 				return (0);
5770Sstevel@tonic-gate 			}
5780Sstevel@tonic-gate 		}
5790Sstevel@tonic-gate 	}
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 	if (a->vp != NULL) {
5820Sstevel@tonic-gate 		VN_HOLD(a->vp);
5830Sstevel@tonic-gate 		if (a->type == MAP_SHARED)
5840Sstevel@tonic-gate 			lgrp_shm_policy_init(NULL, a->vp);
5850Sstevel@tonic-gate 	}
5860Sstevel@tonic-gate 	svd = kmem_cache_alloc(segvn_cache, KM_SLEEP);
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	seg->s_ops = &segvn_ops;
5890Sstevel@tonic-gate 	seg->s_data = (void *)svd;
5900Sstevel@tonic-gate 	seg->s_szc = a->szc;
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 	svd->vp = a->vp;
5930Sstevel@tonic-gate 	/*
5940Sstevel@tonic-gate 	 * Anonymous mappings have no backing file so the offset is meaningless.
5950Sstevel@tonic-gate 	 */
5960Sstevel@tonic-gate 	svd->offset = a->vp ? (a->offset & PAGEMASK) : 0;
5970Sstevel@tonic-gate 	svd->prot = a->prot;
5980Sstevel@tonic-gate 	svd->maxprot = a->maxprot;
5990Sstevel@tonic-gate 	svd->pageprot = 0;
6000Sstevel@tonic-gate 	svd->type = a->type;
6010Sstevel@tonic-gate 	svd->vpage = NULL;
6020Sstevel@tonic-gate 	svd->cred = cred;
6030Sstevel@tonic-gate 	svd->advice = MADV_NORMAL;
6040Sstevel@tonic-gate 	svd->pageadvice = 0;
6050Sstevel@tonic-gate 	svd->flags = (ushort_t)a->flags;
6060Sstevel@tonic-gate 	svd->softlockcnt = 0;
6070Sstevel@tonic-gate 	if (a->szc != 0 && a->vp != NULL) {
6080Sstevel@tonic-gate 		segvn_setvnode_mpss(a->vp);
6090Sstevel@tonic-gate 	}
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	amp = a->amp;
6120Sstevel@tonic-gate 	if ((svd->amp = amp) == NULL) {
6130Sstevel@tonic-gate 		svd->anon_index = 0;
6140Sstevel@tonic-gate 		if (svd->type == MAP_SHARED) {
6150Sstevel@tonic-gate 			svd->swresv = 0;
6160Sstevel@tonic-gate 			/*
6170Sstevel@tonic-gate 			 * Shared mappings to a vp need no other setup.
6180Sstevel@tonic-gate 			 * If we have a shared mapping to an anon_map object
6190Sstevel@tonic-gate 			 * which hasn't been allocated yet,  allocate the
6200Sstevel@tonic-gate 			 * struct now so that it will be properly shared
6210Sstevel@tonic-gate 			 * by remembering the swap reservation there.
6220Sstevel@tonic-gate 			 */
6230Sstevel@tonic-gate 			if (a->vp == NULL) {
6240Sstevel@tonic-gate 				svd->amp = anonmap_alloc(seg->s_size, swresv);
6250Sstevel@tonic-gate 				svd->amp->a_szc = seg->s_szc;
6260Sstevel@tonic-gate 			}
6270Sstevel@tonic-gate 		} else {
6280Sstevel@tonic-gate 			/*
6290Sstevel@tonic-gate 			 * Private mapping (with or without a vp).
6300Sstevel@tonic-gate 			 * Allocate anon_map when needed.
6310Sstevel@tonic-gate 			 */
6320Sstevel@tonic-gate 			svd->swresv = swresv;
6330Sstevel@tonic-gate 		}
6340Sstevel@tonic-gate 	} else {
6350Sstevel@tonic-gate 		pgcnt_t anon_num;
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 		/*
6380Sstevel@tonic-gate 		 * Mapping to an existing anon_map structure without a vp.
6390Sstevel@tonic-gate 		 * For now we will insure that the segment size isn't larger
6400Sstevel@tonic-gate 		 * than the size - offset gives us.  Later on we may wish to
6410Sstevel@tonic-gate 		 * have the anon array dynamically allocated itself so that
6420Sstevel@tonic-gate 		 * we don't always have to allocate all the anon pointer slots.
6430Sstevel@tonic-gate 		 * This of course involves adding extra code to check that we
6440Sstevel@tonic-gate 		 * aren't trying to use an anon pointer slot beyond the end
6450Sstevel@tonic-gate 		 * of the currently allocated anon array.
6460Sstevel@tonic-gate 		 */
6470Sstevel@tonic-gate 		if ((amp->size - a->offset) < seg->s_size) {
6480Sstevel@tonic-gate 			panic("segvn_create anon_map size");
6490Sstevel@tonic-gate 			/*NOTREACHED*/
6500Sstevel@tonic-gate 		}
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 		anon_num = btopr(a->offset);
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 		if (a->type == MAP_SHARED) {
6550Sstevel@tonic-gate 			/*
6560Sstevel@tonic-gate 			 * SHARED mapping to a given anon_map.
6570Sstevel@tonic-gate 			 */
6580Sstevel@tonic-gate 			ANON_LOCK_ENTER(&amp->a_rwlock, RW_WRITER);
6590Sstevel@tonic-gate 			amp->refcnt++;
6600Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
6610Sstevel@tonic-gate 			svd->anon_index = anon_num;
6620Sstevel@tonic-gate 			svd->swresv = 0;
6630Sstevel@tonic-gate 		} else {
6640Sstevel@tonic-gate 			/*
6650Sstevel@tonic-gate 			 * PRIVATE mapping to a given anon_map.
6660Sstevel@tonic-gate 			 * Make sure that all the needed anon
6670Sstevel@tonic-gate 			 * structures are created (so that we will
6680Sstevel@tonic-gate 			 * share the underlying pages if nothing
6690Sstevel@tonic-gate 			 * is written by this mapping) and then
6700Sstevel@tonic-gate 			 * duplicate the anon array as is done
6710Sstevel@tonic-gate 			 * when a privately mapped segment is dup'ed.
6720Sstevel@tonic-gate 			 */
6730Sstevel@tonic-gate 			struct anon *ap;
6740Sstevel@tonic-gate 			caddr_t addr;
6750Sstevel@tonic-gate 			caddr_t eaddr;
6760Sstevel@tonic-gate 			ulong_t	anon_idx;
6770Sstevel@tonic-gate 			int hat_flag = HAT_LOAD;
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 			if (svd->flags & MAP_TEXT) {
6800Sstevel@tonic-gate 				hat_flag |= HAT_LOAD_TEXT;
6810Sstevel@tonic-gate 			}
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 			svd->amp = anonmap_alloc(seg->s_size, 0);
6840Sstevel@tonic-gate 			svd->amp->a_szc = seg->s_szc;
6850Sstevel@tonic-gate 			svd->anon_index = 0;
6860Sstevel@tonic-gate 			svd->swresv = swresv;
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 			/*
6890Sstevel@tonic-gate 			 * Prevent 2 threads from allocating anon
6900Sstevel@tonic-gate 			 * slots simultaneously.
6910Sstevel@tonic-gate 			 */
6920Sstevel@tonic-gate 			ANON_LOCK_ENTER(&amp->a_rwlock, RW_WRITER);
6930Sstevel@tonic-gate 			eaddr = seg->s_base + seg->s_size;
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 			for (anon_idx = anon_num, addr = seg->s_base;
6960Sstevel@tonic-gate 			    addr < eaddr; addr += PAGESIZE, anon_idx++) {
6970Sstevel@tonic-gate 				page_t *pp;
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 				if ((ap = anon_get_ptr(amp->ahp,
7000Sstevel@tonic-gate 				    anon_idx)) != NULL)
7010Sstevel@tonic-gate 					continue;
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 				/*
7040Sstevel@tonic-gate 				 * Allocate the anon struct now.
7050Sstevel@tonic-gate 				 * Might as well load up translation
7060Sstevel@tonic-gate 				 * to the page while we're at it...
7070Sstevel@tonic-gate 				 */
7080Sstevel@tonic-gate 				pp = anon_zero(seg, addr, &ap, cred);
7090Sstevel@tonic-gate 				if (ap == NULL || pp == NULL) {
7100Sstevel@tonic-gate 					panic("segvn_create anon_zero");
7110Sstevel@tonic-gate 					/*NOTREACHED*/
7120Sstevel@tonic-gate 				}
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 				/*
7150Sstevel@tonic-gate 				 * Re-acquire the anon_map lock and
7160Sstevel@tonic-gate 				 * initialize the anon array entry.
7170Sstevel@tonic-gate 				 */
7180Sstevel@tonic-gate 				ASSERT(anon_get_ptr(amp->ahp,
7190Sstevel@tonic-gate 				    anon_idx) == NULL);
7200Sstevel@tonic-gate 				(void) anon_set_ptr(amp->ahp, anon_idx, ap,
7210Sstevel@tonic-gate 				    ANON_SLEEP);
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 				ASSERT(seg->s_szc == 0);
7240Sstevel@tonic-gate 				ASSERT(!IS_VMODSORT(pp->p_vnode));
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 				hat_memload(seg->s_as->a_hat, addr, pp,
7270Sstevel@tonic-gate 					svd->prot & ~PROT_WRITE, hat_flag);
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 				page_unlock(pp);
7300Sstevel@tonic-gate 			}
7310Sstevel@tonic-gate 			ASSERT(seg->s_szc == 0);
7320Sstevel@tonic-gate 			anon_dup(amp->ahp, anon_num, svd->amp->ahp,
7330Sstevel@tonic-gate 			    0, seg->s_size);
7340Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
7350Sstevel@tonic-gate 		}
7360Sstevel@tonic-gate 	}
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 	/*
7390Sstevel@tonic-gate 	 * Set default memory allocation policy for segment
7400Sstevel@tonic-gate 	 *
7410Sstevel@tonic-gate 	 * Always set policy for private memory at least for initialization
7420Sstevel@tonic-gate 	 * even if this is a shared memory segment
7430Sstevel@tonic-gate 	 */
7440Sstevel@tonic-gate 	(void) lgrp_privm_policy_set(mpolicy, &svd->policy_info, seg->s_size);
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 	if (svd->type == MAP_SHARED)
7470Sstevel@tonic-gate 		(void) lgrp_shm_policy_set(mpolicy, svd->amp, svd->anon_index,
7480Sstevel@tonic-gate 		    svd->vp, svd->offset, seg->s_size);
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 	return (0);
7510Sstevel@tonic-gate }
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate /*
7540Sstevel@tonic-gate  * Concatenate two existing segments, if possible.
7550Sstevel@tonic-gate  * Return 0 on success, -1 if two segments are not compatible
7560Sstevel@tonic-gate  * or -2 on memory allocation failure.
7570Sstevel@tonic-gate  * If private == 1 then try and concat segments with private pages.
7580Sstevel@tonic-gate  */
7590Sstevel@tonic-gate static int
7600Sstevel@tonic-gate segvn_concat(struct seg *seg1, struct seg *seg2, int private)
7610Sstevel@tonic-gate {
7620Sstevel@tonic-gate 	struct segvn_data *svd1 = seg1->s_data;
7630Sstevel@tonic-gate 	struct segvn_data *svd2 = seg2->s_data;
7640Sstevel@tonic-gate 	struct anon_map *amp1 = svd1->amp;
7650Sstevel@tonic-gate 	struct anon_map *amp2 = svd2->amp;
7660Sstevel@tonic-gate 	struct vpage *vpage1 = svd1->vpage;
7670Sstevel@tonic-gate 	struct vpage *vpage2 = svd2->vpage, *nvpage = NULL;
7680Sstevel@tonic-gate 	size_t size, nvpsize;
7690Sstevel@tonic-gate 	pgcnt_t npages1, npages2;
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	ASSERT(seg1->s_as && seg2->s_as && seg1->s_as == seg2->s_as);
7720Sstevel@tonic-gate 	ASSERT(AS_WRITE_HELD(seg1->s_as, &seg1->s_as->a_lock));
7730Sstevel@tonic-gate 	ASSERT(seg1->s_ops == seg2->s_ops);
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	/* both segments exist, try to merge them */
7760Sstevel@tonic-gate #define	incompat(x)	(svd1->x != svd2->x)
7770Sstevel@tonic-gate 	if (incompat(vp) || incompat(maxprot) ||
7780Sstevel@tonic-gate 	    (!svd1->pageadvice && !svd2->pageadvice && incompat(advice)) ||
7790Sstevel@tonic-gate 	    (!svd1->pageprot && !svd2->pageprot && incompat(prot)) ||
7800Sstevel@tonic-gate 	    incompat(type) || incompat(cred) || incompat(flags) ||
7810Sstevel@tonic-gate 	    seg1->s_szc != seg2->s_szc || incompat(policy_info.mem_policy) ||
7820Sstevel@tonic-gate 	    (svd2->softlockcnt > 0))
7830Sstevel@tonic-gate 		return (-1);
7840Sstevel@tonic-gate #undef incompat
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 	/*
7870Sstevel@tonic-gate 	 * vp == NULL implies zfod, offset doesn't matter
7880Sstevel@tonic-gate 	 */
7890Sstevel@tonic-gate 	if (svd1->vp != NULL &&
7900Sstevel@tonic-gate 	    svd1->offset + seg1->s_size != svd2->offset) {
7910Sstevel@tonic-gate 		return (-1);
7920Sstevel@tonic-gate 	}
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	/*
7950Sstevel@tonic-gate 	 * Fail early if we're not supposed to concatenate
7960Sstevel@tonic-gate 	 * private pages.
7970Sstevel@tonic-gate 	 */
7980Sstevel@tonic-gate 	if ((private == 0 || svd1->type != MAP_PRIVATE) &&
7990Sstevel@tonic-gate 	    (amp1 != NULL || amp2 != NULL)) {
8000Sstevel@tonic-gate 		return (-1);
8010Sstevel@tonic-gate 	}
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	/*
8040Sstevel@tonic-gate 	 * If either seg has vpages, create a new merged vpage array.
8050Sstevel@tonic-gate 	 */
8060Sstevel@tonic-gate 	if (vpage1 != NULL || vpage2 != NULL) {
8070Sstevel@tonic-gate 		struct vpage *vp;
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 		npages1 = seg_pages(seg1);
8100Sstevel@tonic-gate 		npages2 = seg_pages(seg2);
8110Sstevel@tonic-gate 		nvpsize = vpgtob(npages1 + npages2);
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 		if ((nvpage = kmem_zalloc(nvpsize, KM_NOSLEEP)) == NULL) {
8140Sstevel@tonic-gate 			return (-2);
8150Sstevel@tonic-gate 		}
8160Sstevel@tonic-gate 		if (vpage1 != NULL) {
8170Sstevel@tonic-gate 			bcopy(vpage1, nvpage, vpgtob(npages1));
8180Sstevel@tonic-gate 		}
8190Sstevel@tonic-gate 		if (vpage2 != NULL) {
8200Sstevel@tonic-gate 			bcopy(vpage2, nvpage + npages1, vpgtob(npages2));
8210Sstevel@tonic-gate 		}
8220Sstevel@tonic-gate 		for (vp = nvpage; vp < nvpage + npages1; vp++) {
8230Sstevel@tonic-gate 			if (svd2->pageprot && !svd1->pageprot) {
8240Sstevel@tonic-gate 				VPP_SETPROT(vp, svd1->prot);
8250Sstevel@tonic-gate 			}
8260Sstevel@tonic-gate 			if (svd2->pageadvice && !svd1->pageadvice) {
8270Sstevel@tonic-gate 				VPP_SETADVICE(vp, svd1->advice);
8280Sstevel@tonic-gate 			}
8290Sstevel@tonic-gate 		}
8300Sstevel@tonic-gate 		for (vp = nvpage + npages1;
8310Sstevel@tonic-gate 		    vp < nvpage + npages1 + npages2; vp++) {
8320Sstevel@tonic-gate 			if (svd1->pageprot && !svd2->pageprot) {
8330Sstevel@tonic-gate 				VPP_SETPROT(vp, svd2->prot);
8340Sstevel@tonic-gate 			}
8350Sstevel@tonic-gate 			if (svd1->pageadvice && !svd2->pageadvice) {
8360Sstevel@tonic-gate 				VPP_SETADVICE(vp, svd2->advice);
8370Sstevel@tonic-gate 			}
8380Sstevel@tonic-gate 		}
8390Sstevel@tonic-gate 	}
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 	/*
8420Sstevel@tonic-gate 	 * If either segment has private pages, create a new merged anon
8430Sstevel@tonic-gate 	 * array.
8440Sstevel@tonic-gate 	 */
8450Sstevel@tonic-gate 	if (amp1 != NULL || amp2 != NULL) {
8460Sstevel@tonic-gate 		struct anon_hdr *nahp;
8470Sstevel@tonic-gate 		struct anon_map *namp = NULL;
8480Sstevel@tonic-gate 		size_t asize = seg1->s_size + seg2->s_size;
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 		if ((nahp = anon_create(btop(asize), ANON_NOSLEEP)) == NULL) {
8510Sstevel@tonic-gate 			if (nvpage != NULL) {
8520Sstevel@tonic-gate 				kmem_free(nvpage, nvpsize);
8530Sstevel@tonic-gate 			}
8540Sstevel@tonic-gate 			return (-2);
8550Sstevel@tonic-gate 		}
8560Sstevel@tonic-gate 		if (amp1 != NULL) {
8570Sstevel@tonic-gate 			/*
8580Sstevel@tonic-gate 			 * XXX anon rwlock is not really needed because
8590Sstevel@tonic-gate 			 * this is a private segment and we are writers.
8600Sstevel@tonic-gate 			 */
8610Sstevel@tonic-gate 			ANON_LOCK_ENTER(&amp1->a_rwlock, RW_WRITER);
8620Sstevel@tonic-gate 			ASSERT(amp1->refcnt == 1);
8630Sstevel@tonic-gate 			if (anon_copy_ptr(amp1->ahp, svd1->anon_index,
8640Sstevel@tonic-gate 			    nahp, 0, btop(seg1->s_size), ANON_NOSLEEP)) {
8650Sstevel@tonic-gate 				anon_release(nahp, btop(asize));
8660Sstevel@tonic-gate 				ANON_LOCK_EXIT(&amp1->a_rwlock);
8670Sstevel@tonic-gate 				if (nvpage != NULL) {
8680Sstevel@tonic-gate 					kmem_free(nvpage, nvpsize);
8690Sstevel@tonic-gate 				}
8700Sstevel@tonic-gate 				return (-2);
8710Sstevel@tonic-gate 			}
8720Sstevel@tonic-gate 		}
8730Sstevel@tonic-gate 		if (amp2 != NULL) {
8740Sstevel@tonic-gate 			ANON_LOCK_ENTER(&amp2->a_rwlock, RW_WRITER);
8750Sstevel@tonic-gate 			ASSERT(amp2->refcnt == 1);
8760Sstevel@tonic-gate 			if (anon_copy_ptr(amp2->ahp, svd2->anon_index,
8770Sstevel@tonic-gate 			    nahp, btop(seg1->s_size), btop(seg2->s_size),
8780Sstevel@tonic-gate 			    ANON_NOSLEEP)) {
8790Sstevel@tonic-gate 				anon_release(nahp, btop(asize));
8800Sstevel@tonic-gate 				ANON_LOCK_EXIT(&amp2->a_rwlock);
8810Sstevel@tonic-gate 				if (amp1 != NULL) {
8820Sstevel@tonic-gate 					ANON_LOCK_EXIT(&amp1->a_rwlock);
8830Sstevel@tonic-gate 				}
8840Sstevel@tonic-gate 				if (nvpage != NULL) {
8850Sstevel@tonic-gate 					kmem_free(nvpage, nvpsize);
8860Sstevel@tonic-gate 				}
8870Sstevel@tonic-gate 				return (-2);
8880Sstevel@tonic-gate 			}
8890Sstevel@tonic-gate 		}
8900Sstevel@tonic-gate 		if (amp1 != NULL) {
8910Sstevel@tonic-gate 			namp = amp1;
8920Sstevel@tonic-gate 			anon_release(amp1->ahp, btop(amp1->size));
8930Sstevel@tonic-gate 		}
8940Sstevel@tonic-gate 		if (amp2 != NULL) {
8950Sstevel@tonic-gate 			if (namp == NULL) {
8960Sstevel@tonic-gate 				ASSERT(amp1 == NULL);
8970Sstevel@tonic-gate 				namp = amp2;
8980Sstevel@tonic-gate 				anon_release(amp2->ahp, btop(amp2->size));
8990Sstevel@tonic-gate 			} else {
9000Sstevel@tonic-gate 				amp2->refcnt--;
9010Sstevel@tonic-gate 				ANON_LOCK_EXIT(&amp2->a_rwlock);
9020Sstevel@tonic-gate 				anonmap_free(amp2);
9030Sstevel@tonic-gate 			}
9040Sstevel@tonic-gate 			svd2->amp = NULL; /* needed for seg_free */
9050Sstevel@tonic-gate 		}
9060Sstevel@tonic-gate 		namp->ahp = nahp;
9070Sstevel@tonic-gate 		namp->size = asize;
9080Sstevel@tonic-gate 		svd1->amp = namp;
9090Sstevel@tonic-gate 		svd1->anon_index = 0;
9100Sstevel@tonic-gate 		ANON_LOCK_EXIT(&namp->a_rwlock);
9110Sstevel@tonic-gate 	}
9120Sstevel@tonic-gate 	/*
9130Sstevel@tonic-gate 	 * Now free the old vpage structures.
9140Sstevel@tonic-gate 	 */
9150Sstevel@tonic-gate 	if (nvpage != NULL) {
9160Sstevel@tonic-gate 		if (vpage1 != NULL) {
9170Sstevel@tonic-gate 			kmem_free(vpage1, vpgtob(npages1));
9180Sstevel@tonic-gate 		}
9190Sstevel@tonic-gate 		if (vpage2 != NULL) {
9200Sstevel@tonic-gate 			svd2->vpage = NULL;
9210Sstevel@tonic-gate 			kmem_free(vpage2, vpgtob(npages2));
9220Sstevel@tonic-gate 		}
9230Sstevel@tonic-gate 		if (svd2->pageprot) {
9240Sstevel@tonic-gate 			svd1->pageprot = 1;
9250Sstevel@tonic-gate 		}
9260Sstevel@tonic-gate 		if (svd2->pageadvice) {
9270Sstevel@tonic-gate 			svd1->pageadvice = 1;
9280Sstevel@tonic-gate 		}
9290Sstevel@tonic-gate 		svd1->vpage = nvpage;
9300Sstevel@tonic-gate 	}
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 	/* all looks ok, merge segments */
9330Sstevel@tonic-gate 	svd1->swresv += svd2->swresv;
9340Sstevel@tonic-gate 	svd2->swresv = 0;  /* so seg_free doesn't release swap space */
9350Sstevel@tonic-gate 	size = seg2->s_size;
9360Sstevel@tonic-gate 	seg_free(seg2);
9370Sstevel@tonic-gate 	seg1->s_size += size;
9380Sstevel@tonic-gate 	return (0);
9390Sstevel@tonic-gate }
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate /*
9420Sstevel@tonic-gate  * Extend the previous segment (seg1) to include the
9430Sstevel@tonic-gate  * new segment (seg2 + a), if possible.
9440Sstevel@tonic-gate  * Return 0 on success.
9450Sstevel@tonic-gate  */
9460Sstevel@tonic-gate static int
9470Sstevel@tonic-gate segvn_extend_prev(seg1, seg2, a, swresv)
9480Sstevel@tonic-gate 	struct seg *seg1, *seg2;
9490Sstevel@tonic-gate 	struct segvn_crargs *a;
9500Sstevel@tonic-gate 	size_t swresv;
9510Sstevel@tonic-gate {
9520Sstevel@tonic-gate 	struct segvn_data *svd1 = (struct segvn_data *)seg1->s_data;
9530Sstevel@tonic-gate 	size_t size;
9540Sstevel@tonic-gate 	struct anon_map *amp1;
9550Sstevel@tonic-gate 	struct vpage *new_vpage;
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate 	/*
9580Sstevel@tonic-gate 	 * We don't need any segment level locks for "segvn" data
9590Sstevel@tonic-gate 	 * since the address space is "write" locked.
9600Sstevel@tonic-gate 	 */
9610Sstevel@tonic-gate 	ASSERT(seg1->s_as && AS_WRITE_HELD(seg1->s_as, &seg1->s_as->a_lock));
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate 	/* second segment is new, try to extend first */
9640Sstevel@tonic-gate 	/* XXX - should also check cred */
9650Sstevel@tonic-gate 	if (svd1->vp != a->vp || svd1->maxprot != a->maxprot ||
9660Sstevel@tonic-gate 	    (!svd1->pageprot && (svd1->prot != a->prot)) ||
9670Sstevel@tonic-gate 	    svd1->type != a->type || svd1->flags != a->flags ||
9680Sstevel@tonic-gate 	    seg1->s_szc != a->szc)
9690Sstevel@tonic-gate 		return (-1);
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate 	/* vp == NULL implies zfod, offset doesn't matter */
9720Sstevel@tonic-gate 	if (svd1->vp != NULL &&
9730Sstevel@tonic-gate 	    svd1->offset + seg1->s_size != (a->offset & PAGEMASK))
9740Sstevel@tonic-gate 		return (-1);
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate 	amp1 = svd1->amp;
9770Sstevel@tonic-gate 	if (amp1) {
9780Sstevel@tonic-gate 		pgcnt_t newpgs;
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 		/*
9810Sstevel@tonic-gate 		 * Segment has private pages, can data structures
9820Sstevel@tonic-gate 		 * be expanded?
9830Sstevel@tonic-gate 		 *
9840Sstevel@tonic-gate 		 * Acquire the anon_map lock to prevent it from changing,
9850Sstevel@tonic-gate 		 * if it is shared.  This ensures that the anon_map
9860Sstevel@tonic-gate 		 * will not change while a thread which has a read/write
9870Sstevel@tonic-gate 		 * lock on an address space references it.
9880Sstevel@tonic-gate 		 * XXX - Don't need the anon_map lock at all if "refcnt"
9890Sstevel@tonic-gate 		 * is 1.
9900Sstevel@tonic-gate 		 *
9910Sstevel@tonic-gate 		 * Can't grow a MAP_SHARED segment with an anonmap because
9920Sstevel@tonic-gate 		 * there may be existing anon slots where we want to extend
9930Sstevel@tonic-gate 		 * the segment and we wouldn't know what to do with them
9940Sstevel@tonic-gate 		 * (e.g., for tmpfs right thing is to just leave them there,
9950Sstevel@tonic-gate 		 * for /dev/zero they should be cleared out).
9960Sstevel@tonic-gate 		 */
9970Sstevel@tonic-gate 		if (svd1->type == MAP_SHARED)
9980Sstevel@tonic-gate 			return (-1);
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate 		ANON_LOCK_ENTER(&amp1->a_rwlock, RW_WRITER);
10010Sstevel@tonic-gate 		if (amp1->refcnt > 1) {
10020Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp1->a_rwlock);
10030Sstevel@tonic-gate 			return (-1);
10040Sstevel@tonic-gate 		}
10050Sstevel@tonic-gate 		newpgs = anon_grow(amp1->ahp, &svd1->anon_index,
10060Sstevel@tonic-gate 		    btop(seg1->s_size), btop(seg2->s_size), ANON_NOSLEEP);
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 		if (newpgs == 0) {
10090Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp1->a_rwlock);
10100Sstevel@tonic-gate 			return (-1);
10110Sstevel@tonic-gate 		}
10120Sstevel@tonic-gate 		amp1->size = ptob(newpgs);
10130Sstevel@tonic-gate 		ANON_LOCK_EXIT(&amp1->a_rwlock);
10140Sstevel@tonic-gate 	}
10150Sstevel@tonic-gate 	if (svd1->vpage != NULL) {
10160Sstevel@tonic-gate 		new_vpage =
10170Sstevel@tonic-gate 		    kmem_zalloc(vpgtob(seg_pages(seg1) + seg_pages(seg2)),
10180Sstevel@tonic-gate 			KM_NOSLEEP);
10190Sstevel@tonic-gate 		if (new_vpage == NULL)
10200Sstevel@tonic-gate 			return (-1);
10210Sstevel@tonic-gate 		bcopy(svd1->vpage, new_vpage, vpgtob(seg_pages(seg1)));
10220Sstevel@tonic-gate 		kmem_free(svd1->vpage, vpgtob(seg_pages(seg1)));
10230Sstevel@tonic-gate 		svd1->vpage = new_vpage;
10240Sstevel@tonic-gate 		if (svd1->pageprot) {
10250Sstevel@tonic-gate 			struct vpage *vp, *evp;
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate 			vp = new_vpage + seg_pages(seg1);
10280Sstevel@tonic-gate 			evp = vp + seg_pages(seg2);
10290Sstevel@tonic-gate 			for (; vp < evp; vp++)
10300Sstevel@tonic-gate 				VPP_SETPROT(vp, a->prot);
10310Sstevel@tonic-gate 		}
10320Sstevel@tonic-gate 	}
10330Sstevel@tonic-gate 	size = seg2->s_size;
10340Sstevel@tonic-gate 	seg_free(seg2);
10350Sstevel@tonic-gate 	seg1->s_size += size;
10360Sstevel@tonic-gate 	svd1->swresv += swresv;
10370Sstevel@tonic-gate 	return (0);
10380Sstevel@tonic-gate }
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate /*
10410Sstevel@tonic-gate  * Extend the next segment (seg2) to include the
10420Sstevel@tonic-gate  * new segment (seg1 + a), if possible.
10430Sstevel@tonic-gate  * Return 0 on success.
10440Sstevel@tonic-gate  */
10450Sstevel@tonic-gate static int
10460Sstevel@tonic-gate segvn_extend_next(
10470Sstevel@tonic-gate 	struct seg *seg1,
10480Sstevel@tonic-gate 	struct seg *seg2,
10490Sstevel@tonic-gate 	struct segvn_crargs *a,
10500Sstevel@tonic-gate 	size_t swresv)
10510Sstevel@tonic-gate {
10520Sstevel@tonic-gate 	struct segvn_data *svd2 = (struct segvn_data *)seg2->s_data;
10530Sstevel@tonic-gate 	size_t size;
10540Sstevel@tonic-gate 	struct anon_map *amp2;
10550Sstevel@tonic-gate 	struct vpage *new_vpage;
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate 	/*
10580Sstevel@tonic-gate 	 * We don't need any segment level locks for "segvn" data
10590Sstevel@tonic-gate 	 * since the address space is "write" locked.
10600Sstevel@tonic-gate 	 */
10610Sstevel@tonic-gate 	ASSERT(seg2->s_as && AS_WRITE_HELD(seg2->s_as, &seg2->s_as->a_lock));
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate 	/* first segment is new, try to extend second */
10640Sstevel@tonic-gate 	/* XXX - should also check cred */
10650Sstevel@tonic-gate 	if (svd2->vp != a->vp || svd2->maxprot != a->maxprot ||
10660Sstevel@tonic-gate 	    (!svd2->pageprot && (svd2->prot != a->prot)) ||
10670Sstevel@tonic-gate 	    svd2->type != a->type || svd2->flags != a->flags ||
10680Sstevel@tonic-gate 	    seg2->s_szc != a->szc)
10690Sstevel@tonic-gate 		return (-1);
10700Sstevel@tonic-gate 	/* vp == NULL implies zfod, offset doesn't matter */
10710Sstevel@tonic-gate 	if (svd2->vp != NULL &&
10720Sstevel@tonic-gate 	    (a->offset & PAGEMASK) + seg1->s_size != svd2->offset)
10730Sstevel@tonic-gate 		return (-1);
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 	amp2 = svd2->amp;
10760Sstevel@tonic-gate 	if (amp2) {
10770Sstevel@tonic-gate 		pgcnt_t newpgs;
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 		/*
10800Sstevel@tonic-gate 		 * Segment has private pages, can data structures
10810Sstevel@tonic-gate 		 * be expanded?
10820Sstevel@tonic-gate 		 *
10830Sstevel@tonic-gate 		 * Acquire the anon_map lock to prevent it from changing,
10840Sstevel@tonic-gate 		 * if it is shared.  This ensures that the anon_map
10850Sstevel@tonic-gate 		 * will not change while a thread which has a read/write
10860Sstevel@tonic-gate 		 * lock on an address space references it.
10870Sstevel@tonic-gate 		 *
10880Sstevel@tonic-gate 		 * XXX - Don't need the anon_map lock at all if "refcnt"
10890Sstevel@tonic-gate 		 * is 1.
10900Sstevel@tonic-gate 		 */
10910Sstevel@tonic-gate 		if (svd2->type == MAP_SHARED)
10920Sstevel@tonic-gate 			return (-1);
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate 		ANON_LOCK_ENTER(&amp2->a_rwlock, RW_WRITER);
10950Sstevel@tonic-gate 		if (amp2->refcnt > 1) {
10960Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp2->a_rwlock);
10970Sstevel@tonic-gate 			return (-1);
10980Sstevel@tonic-gate 		}
10990Sstevel@tonic-gate 		newpgs = anon_grow(amp2->ahp, &svd2->anon_index,
11000Sstevel@tonic-gate 		    btop(seg2->s_size), btop(seg1->s_size),
11010Sstevel@tonic-gate 		    ANON_NOSLEEP | ANON_GROWDOWN);
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate 		if (newpgs == 0) {
11040Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp2->a_rwlock);
11050Sstevel@tonic-gate 			return (-1);
11060Sstevel@tonic-gate 		}
11070Sstevel@tonic-gate 		amp2->size = ptob(newpgs);
11080Sstevel@tonic-gate 		ANON_LOCK_EXIT(&amp2->a_rwlock);
11090Sstevel@tonic-gate 	}
11100Sstevel@tonic-gate 	if (svd2->vpage != NULL) {
11110Sstevel@tonic-gate 		new_vpage =
11120Sstevel@tonic-gate 		    kmem_zalloc(vpgtob(seg_pages(seg1) + seg_pages(seg2)),
11130Sstevel@tonic-gate 			KM_NOSLEEP);
11140Sstevel@tonic-gate 		if (new_vpage == NULL) {
11150Sstevel@tonic-gate 			/* Not merging segments so adjust anon_index back */
11160Sstevel@tonic-gate 			if (amp2)
11170Sstevel@tonic-gate 				svd2->anon_index += seg_pages(seg1);
11180Sstevel@tonic-gate 			return (-1);
11190Sstevel@tonic-gate 		}
11200Sstevel@tonic-gate 		bcopy(svd2->vpage, new_vpage + seg_pages(seg1),
11210Sstevel@tonic-gate 		    vpgtob(seg_pages(seg2)));
11220Sstevel@tonic-gate 		kmem_free(svd2->vpage, vpgtob(seg_pages(seg2)));
11230Sstevel@tonic-gate 		svd2->vpage = new_vpage;
11240Sstevel@tonic-gate 		if (svd2->pageprot) {
11250Sstevel@tonic-gate 			struct vpage *vp, *evp;
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 			vp = new_vpage;
11280Sstevel@tonic-gate 			evp = vp + seg_pages(seg1);
11290Sstevel@tonic-gate 			for (; vp < evp; vp++)
11300Sstevel@tonic-gate 				VPP_SETPROT(vp, a->prot);
11310Sstevel@tonic-gate 		}
11320Sstevel@tonic-gate 	}
11330Sstevel@tonic-gate 	size = seg1->s_size;
11340Sstevel@tonic-gate 	seg_free(seg1);
11350Sstevel@tonic-gate 	seg2->s_size += size;
11360Sstevel@tonic-gate 	seg2->s_base -= size;
11370Sstevel@tonic-gate 	svd2->offset -= size;
11380Sstevel@tonic-gate 	svd2->swresv += swresv;
11390Sstevel@tonic-gate 	return (0);
11400Sstevel@tonic-gate }
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate static int
11430Sstevel@tonic-gate segvn_dup(struct seg *seg, struct seg *newseg)
11440Sstevel@tonic-gate {
11450Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
11460Sstevel@tonic-gate 	struct segvn_data *newsvd;
11470Sstevel@tonic-gate 	pgcnt_t npages = seg_pages(seg);
11480Sstevel@tonic-gate 	int error = 0;
11490Sstevel@tonic-gate 	uint_t prot;
11500Sstevel@tonic-gate 	size_t len;
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 	/*
11550Sstevel@tonic-gate 	 * If segment has anon reserved, reserve more for the new seg.
11560Sstevel@tonic-gate 	 * For a MAP_NORESERVE segment swresv will be a count of all the
11570Sstevel@tonic-gate 	 * allocated anon slots; thus we reserve for the child as many slots
11580Sstevel@tonic-gate 	 * as the parent has allocated. This semantic prevents the child or
11590Sstevel@tonic-gate 	 * parent from dieing during a copy-on-write fault caused by trying
11600Sstevel@tonic-gate 	 * to write a shared pre-existing anon page.
11610Sstevel@tonic-gate 	 */
11620Sstevel@tonic-gate 	if ((len = svd->swresv) != 0) {
11630Sstevel@tonic-gate 		if (anon_resv(svd->swresv) == 0)
11640Sstevel@tonic-gate 			return (ENOMEM);
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate 		TRACE_3(TR_FAC_VM, TR_ANON_PROC, "anon proc:%p %lu %u",
11670Sstevel@tonic-gate 			seg, len, 0);
11680Sstevel@tonic-gate 	}
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate 	newsvd = kmem_cache_alloc(segvn_cache, KM_SLEEP);
11710Sstevel@tonic-gate 
11720Sstevel@tonic-gate 	newseg->s_ops = &segvn_ops;
11730Sstevel@tonic-gate 	newseg->s_data = (void *)newsvd;
11740Sstevel@tonic-gate 	newseg->s_szc = seg->s_szc;
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate 	if ((newsvd->vp = svd->vp) != NULL) {
11770Sstevel@tonic-gate 		VN_HOLD(svd->vp);
11780Sstevel@tonic-gate 		if (svd->type == MAP_SHARED)
11790Sstevel@tonic-gate 			lgrp_shm_policy_init(NULL, svd->vp);
11800Sstevel@tonic-gate 	}
11810Sstevel@tonic-gate 	newsvd->offset = svd->offset;
11820Sstevel@tonic-gate 	newsvd->prot = svd->prot;
11830Sstevel@tonic-gate 	newsvd->maxprot = svd->maxprot;
11840Sstevel@tonic-gate 	newsvd->pageprot = svd->pageprot;
11850Sstevel@tonic-gate 	newsvd->type = svd->type;
11860Sstevel@tonic-gate 	newsvd->cred = svd->cred;
11870Sstevel@tonic-gate 	crhold(newsvd->cred);
11880Sstevel@tonic-gate 	newsvd->advice = svd->advice;
11890Sstevel@tonic-gate 	newsvd->pageadvice = svd->pageadvice;
11900Sstevel@tonic-gate 	newsvd->swresv = svd->swresv;
11910Sstevel@tonic-gate 	newsvd->flags = svd->flags;
11920Sstevel@tonic-gate 	newsvd->softlockcnt = 0;
11930Sstevel@tonic-gate 	newsvd->policy_info = svd->policy_info;
11940Sstevel@tonic-gate 	if ((newsvd->amp = svd->amp) == NULL) {
11950Sstevel@tonic-gate 		/*
11960Sstevel@tonic-gate 		 * Not attaching to a shared anon object.
11970Sstevel@tonic-gate 		 */
11980Sstevel@tonic-gate 		newsvd->anon_index = 0;
11990Sstevel@tonic-gate 	} else {
12000Sstevel@tonic-gate 		struct anon_map *amp;
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate 		amp = svd->amp;
12030Sstevel@tonic-gate 		if (svd->type == MAP_SHARED) {
12040Sstevel@tonic-gate 			ANON_LOCK_ENTER(&amp->a_rwlock, RW_WRITER);
12050Sstevel@tonic-gate 			amp->refcnt++;
12060Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
12070Sstevel@tonic-gate 			newsvd->anon_index = svd->anon_index;
12080Sstevel@tonic-gate 		} else {
12090Sstevel@tonic-gate 			int reclaim = 1;
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate 			/*
12120Sstevel@tonic-gate 			 * Allocate and initialize new anon_map structure.
12130Sstevel@tonic-gate 			 */
12140Sstevel@tonic-gate 			newsvd->amp = anonmap_alloc(newseg->s_size, 0);
12150Sstevel@tonic-gate 			newsvd->amp->a_szc = newseg->s_szc;
12160Sstevel@tonic-gate 			newsvd->anon_index = 0;
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate 			/*
12190Sstevel@tonic-gate 			 * We don't have to acquire the anon_map lock
12200Sstevel@tonic-gate 			 * for the new segment (since it belongs to an
12210Sstevel@tonic-gate 			 * address space that is still not associated
12220Sstevel@tonic-gate 			 * with any process), or the segment in the old
12230Sstevel@tonic-gate 			 * address space (since all threads in it
12240Sstevel@tonic-gate 			 * are stopped while duplicating the address space).
12250Sstevel@tonic-gate 			 */
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate 			/*
12280Sstevel@tonic-gate 			 * The goal of the following code is to make sure that
12290Sstevel@tonic-gate 			 * softlocked pages do not end up as copy on write
12300Sstevel@tonic-gate 			 * pages.  This would cause problems where one
12310Sstevel@tonic-gate 			 * thread writes to a page that is COW and a different
12320Sstevel@tonic-gate 			 * thread in the same process has softlocked it.  The
12330Sstevel@tonic-gate 			 * softlock lock would move away from this process
12340Sstevel@tonic-gate 			 * because the write would cause this process to get
12350Sstevel@tonic-gate 			 * a copy (without the softlock).
12360Sstevel@tonic-gate 			 *
12370Sstevel@tonic-gate 			 * The strategy here is to just break the
12380Sstevel@tonic-gate 			 * sharing on pages that could possibly be
12390Sstevel@tonic-gate 			 * softlocked.
12400Sstevel@tonic-gate 			 */
12410Sstevel@tonic-gate retry:
12420Sstevel@tonic-gate 			if (svd->softlockcnt) {
12430Sstevel@tonic-gate 				struct anon *ap, *newap;
12440Sstevel@tonic-gate 				size_t i;
12450Sstevel@tonic-gate 				uint_t vpprot;
12460Sstevel@tonic-gate 				page_t *anon_pl[1+1], *pp;
12470Sstevel@tonic-gate 				caddr_t addr;
12480Sstevel@tonic-gate 				ulong_t anon_idx = 0;
12490Sstevel@tonic-gate 
12500Sstevel@tonic-gate 				/*
12510Sstevel@tonic-gate 				 * The softlock count might be non zero
12520Sstevel@tonic-gate 				 * because some pages are still stuck in the
12530Sstevel@tonic-gate 				 * cache for lazy reclaim. Flush the cache
12540Sstevel@tonic-gate 				 * now. This should drop the count to zero.
12550Sstevel@tonic-gate 				 * [or there is really I/O going on to these
12560Sstevel@tonic-gate 				 * pages]. Note, we have the writers lock so
12570Sstevel@tonic-gate 				 * nothing gets inserted during the flush.
12580Sstevel@tonic-gate 				 */
12590Sstevel@tonic-gate 				if (reclaim == 1) {
12600Sstevel@tonic-gate 					segvn_purge(seg);
12610Sstevel@tonic-gate 					reclaim = 0;
12620Sstevel@tonic-gate 					goto retry;
12630Sstevel@tonic-gate 				}
12640Sstevel@tonic-gate 				i = btopr(seg->s_size);
12650Sstevel@tonic-gate 				addr = seg->s_base;
12660Sstevel@tonic-gate 				/*
12670Sstevel@tonic-gate 				 * XXX break cow sharing using PAGESIZE
12680Sstevel@tonic-gate 				 * pages. They will be relocated into larger
12690Sstevel@tonic-gate 				 * pages at fault time.
12700Sstevel@tonic-gate 				 */
12710Sstevel@tonic-gate 				while (i-- > 0) {
12720Sstevel@tonic-gate 					if (ap = anon_get_ptr(amp->ahp,
12730Sstevel@tonic-gate 					    anon_idx)) {
12740Sstevel@tonic-gate 						error = anon_getpage(&ap,
12750Sstevel@tonic-gate 						    &vpprot, anon_pl, PAGESIZE,
12760Sstevel@tonic-gate 						    seg, addr, S_READ,
12770Sstevel@tonic-gate 						    svd->cred);
12780Sstevel@tonic-gate 						if (error) {
12790Sstevel@tonic-gate 							newsvd->vpage = NULL;
12800Sstevel@tonic-gate 							goto out;
12810Sstevel@tonic-gate 						}
12820Sstevel@tonic-gate 						/*
12830Sstevel@tonic-gate 						 * prot need not be computed
12840Sstevel@tonic-gate 						 * below 'cause anon_private is
12850Sstevel@tonic-gate 						 * going to ignore it anyway
12860Sstevel@tonic-gate 						 * as child doesn't inherit
12870Sstevel@tonic-gate 						 * pagelock from parent.
12880Sstevel@tonic-gate 						 */
12890Sstevel@tonic-gate 						prot = svd->pageprot ?
12900Sstevel@tonic-gate 						    VPP_PROT(
12910Sstevel@tonic-gate 						    &svd->vpage[
12920Sstevel@tonic-gate 						    seg_page(seg, addr)])
12930Sstevel@tonic-gate 						    : svd->prot;
12940Sstevel@tonic-gate 						pp = anon_private(&newap,
12950Sstevel@tonic-gate 						    newseg, addr, prot,
12960Sstevel@tonic-gate 						    anon_pl[0],	0,
12970Sstevel@tonic-gate 						    newsvd->cred);
12980Sstevel@tonic-gate 						if (pp == NULL) {
12990Sstevel@tonic-gate 							/* no mem abort */
13000Sstevel@tonic-gate 							newsvd->vpage = NULL;
13010Sstevel@tonic-gate 							error = ENOMEM;
13020Sstevel@tonic-gate 							goto out;
13030Sstevel@tonic-gate 						}
13040Sstevel@tonic-gate 						(void) anon_set_ptr(
13050Sstevel@tonic-gate 						    newsvd->amp->ahp, anon_idx,
13060Sstevel@tonic-gate 						    newap, ANON_SLEEP);
13070Sstevel@tonic-gate 						page_unlock(pp);
13080Sstevel@tonic-gate 					}
13090Sstevel@tonic-gate 					addr += PAGESIZE;
13100Sstevel@tonic-gate 					anon_idx++;
13110Sstevel@tonic-gate 				}
13120Sstevel@tonic-gate 			} else {	/* common case */
13130Sstevel@tonic-gate 				if (seg->s_szc != 0) {
13140Sstevel@tonic-gate 					/*
13150Sstevel@tonic-gate 					 * If at least one of anon slots of a
13160Sstevel@tonic-gate 					 * large page exists then make sure
13170Sstevel@tonic-gate 					 * all anon slots of a large page
13180Sstevel@tonic-gate 					 * exist to avoid partial cow sharing
13190Sstevel@tonic-gate 					 * of a large page in the future.
13200Sstevel@tonic-gate 					 */
13210Sstevel@tonic-gate 					anon_dup_fill_holes(amp->ahp,
13220Sstevel@tonic-gate 					    svd->anon_index, newsvd->amp->ahp,
13230Sstevel@tonic-gate 					    0, seg->s_size, seg->s_szc,
13240Sstevel@tonic-gate 					    svd->vp != NULL);
13250Sstevel@tonic-gate 				} else {
13260Sstevel@tonic-gate 					anon_dup(amp->ahp, svd->anon_index,
13270Sstevel@tonic-gate 					    newsvd->amp->ahp, 0, seg->s_size);
13280Sstevel@tonic-gate 				}
13290Sstevel@tonic-gate 
13300Sstevel@tonic-gate 				hat_clrattr(seg->s_as->a_hat, seg->s_base,
13310Sstevel@tonic-gate 				    seg->s_size, PROT_WRITE);
13320Sstevel@tonic-gate 			}
13330Sstevel@tonic-gate 		}
13340Sstevel@tonic-gate 	}
13350Sstevel@tonic-gate 	/*
13360Sstevel@tonic-gate 	 * If necessary, create a vpage structure for the new segment.
13370Sstevel@tonic-gate 	 * Do not copy any page lock indications.
13380Sstevel@tonic-gate 	 */
13390Sstevel@tonic-gate 	if (svd->vpage != NULL) {
13400Sstevel@tonic-gate 		uint_t i;
13410Sstevel@tonic-gate 		struct vpage *ovp = svd->vpage;
13420Sstevel@tonic-gate 		struct vpage *nvp;
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate 		nvp = newsvd->vpage =
13450Sstevel@tonic-gate 		    kmem_alloc(vpgtob(npages), KM_SLEEP);
13460Sstevel@tonic-gate 		for (i = 0; i < npages; i++) {
13470Sstevel@tonic-gate 			*nvp = *ovp++;
13480Sstevel@tonic-gate 			VPP_CLRPPLOCK(nvp++);
13490Sstevel@tonic-gate 		}
13500Sstevel@tonic-gate 	} else
13510Sstevel@tonic-gate 		newsvd->vpage = NULL;
13520Sstevel@tonic-gate 
13530Sstevel@tonic-gate 	/* Inform the vnode of the new mapping */
13540Sstevel@tonic-gate 	if (newsvd->vp != NULL) {
13550Sstevel@tonic-gate 		error = VOP_ADDMAP(newsvd->vp, (offset_t)newsvd->offset,
13560Sstevel@tonic-gate 		    newseg->s_as, newseg->s_base, newseg->s_size, newsvd->prot,
13570Sstevel@tonic-gate 		    newsvd->maxprot, newsvd->type, newsvd->cred);
13580Sstevel@tonic-gate 	}
13590Sstevel@tonic-gate out:
13600Sstevel@tonic-gate 	return (error);
13610Sstevel@tonic-gate }
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate 
13640Sstevel@tonic-gate /*
13650Sstevel@tonic-gate  * callback function used by segvn_unmap to invoke free_vp_pages() for only
13660Sstevel@tonic-gate  * those pages actually processed by the HAT
13670Sstevel@tonic-gate  */
13680Sstevel@tonic-gate extern int free_pages;
13690Sstevel@tonic-gate 
13700Sstevel@tonic-gate static void
13710Sstevel@tonic-gate segvn_hat_unload_callback(hat_callback_t *cb)
13720Sstevel@tonic-gate {
13730Sstevel@tonic-gate 	struct seg		*seg = cb->hcb_data;
13740Sstevel@tonic-gate 	struct segvn_data	*svd = (struct segvn_data *)seg->s_data;
13750Sstevel@tonic-gate 	size_t			len;
13760Sstevel@tonic-gate 	u_offset_t		off;
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate 	ASSERT(svd->vp != NULL);
13790Sstevel@tonic-gate 	ASSERT(cb->hcb_end_addr > cb->hcb_start_addr);
13800Sstevel@tonic-gate 	ASSERT(cb->hcb_start_addr >= seg->s_base);
13810Sstevel@tonic-gate 
13820Sstevel@tonic-gate 	len = cb->hcb_end_addr - cb->hcb_start_addr;
13830Sstevel@tonic-gate 	off = cb->hcb_start_addr - seg->s_base;
13840Sstevel@tonic-gate 	free_vp_pages(svd->vp, svd->offset + off, len);
13850Sstevel@tonic-gate }
13860Sstevel@tonic-gate 
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate static int
13890Sstevel@tonic-gate segvn_unmap(struct seg *seg, caddr_t addr, size_t len)
13900Sstevel@tonic-gate {
13910Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
13920Sstevel@tonic-gate 	struct segvn_data *nsvd;
13930Sstevel@tonic-gate 	struct seg *nseg;
13940Sstevel@tonic-gate 	struct anon_map *amp;
13950Sstevel@tonic-gate 	pgcnt_t	opages;		/* old segment size in pages */
13960Sstevel@tonic-gate 	pgcnt_t	npages;		/* new segment size in pages */
13970Sstevel@tonic-gate 	pgcnt_t	dpages;		/* pages being deleted (unmapped) */
13980Sstevel@tonic-gate 	hat_callback_t callback;	/* used for free_vp_pages() */
13990Sstevel@tonic-gate 	hat_callback_t *cbp = NULL;
14000Sstevel@tonic-gate 	caddr_t nbase;
14010Sstevel@tonic-gate 	size_t nsize;
14020Sstevel@tonic-gate 	size_t oswresv;
14030Sstevel@tonic-gate 	int reclaim = 1;
14040Sstevel@tonic-gate 
14050Sstevel@tonic-gate 	/*
14060Sstevel@tonic-gate 	 * We don't need any segment level locks for "segvn" data
14070Sstevel@tonic-gate 	 * since the address space is "write" locked.
14080Sstevel@tonic-gate 	 */
14090Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate 	/*
14120Sstevel@tonic-gate 	 * Fail the unmap if pages are SOFTLOCKed through this mapping.
14130Sstevel@tonic-gate 	 * softlockcnt is protected from change by the as write lock.
14140Sstevel@tonic-gate 	 */
14150Sstevel@tonic-gate retry:
14160Sstevel@tonic-gate 	if (svd->softlockcnt > 0) {
14170Sstevel@tonic-gate 		/*
14180Sstevel@tonic-gate 		 * since we do have the writers lock nobody can fill
14190Sstevel@tonic-gate 		 * the cache during the purge. The flush either succeeds
14200Sstevel@tonic-gate 		 * or we still have pending I/Os.
14210Sstevel@tonic-gate 		 */
14220Sstevel@tonic-gate 		if (reclaim == 1) {
14230Sstevel@tonic-gate 			segvn_purge(seg);
14240Sstevel@tonic-gate 			reclaim = 0;
14250Sstevel@tonic-gate 			goto retry;
14260Sstevel@tonic-gate 		}
14270Sstevel@tonic-gate 		return (EAGAIN);
14280Sstevel@tonic-gate 	}
14290Sstevel@tonic-gate 
14300Sstevel@tonic-gate 	/*
14310Sstevel@tonic-gate 	 * Check for bad sizes
14320Sstevel@tonic-gate 	 */
14330Sstevel@tonic-gate 	if (addr < seg->s_base || addr + len > seg->s_base + seg->s_size ||
14340Sstevel@tonic-gate 	    (len & PAGEOFFSET) || ((uintptr_t)addr & PAGEOFFSET)) {
14350Sstevel@tonic-gate 		panic("segvn_unmap");
14360Sstevel@tonic-gate 		/*NOTREACHED*/
14370Sstevel@tonic-gate 	}
14380Sstevel@tonic-gate 
14390Sstevel@tonic-gate 	if (seg->s_szc != 0) {
14400Sstevel@tonic-gate 		size_t pgsz = page_get_pagesize(seg->s_szc);
14410Sstevel@tonic-gate 		int err;
14420Sstevel@tonic-gate 		if (!IS_P2ALIGNED(addr, pgsz) || !IS_P2ALIGNED(len, pgsz)) {
14430Sstevel@tonic-gate 			ASSERT(seg->s_base != addr || seg->s_size != len);
14440Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.demoterange[0]);
14450Sstevel@tonic-gate 			err = segvn_demote_range(seg, addr, len, SDR_END);
14460Sstevel@tonic-gate 			if (err == 0) {
14470Sstevel@tonic-gate 				return (IE_RETRY);
14480Sstevel@tonic-gate 			}
14490Sstevel@tonic-gate 			return (err);
14500Sstevel@tonic-gate 		}
14510Sstevel@tonic-gate 	}
14520Sstevel@tonic-gate 
14530Sstevel@tonic-gate 	/* Inform the vnode of the unmapping. */
14540Sstevel@tonic-gate 	if (svd->vp) {
14550Sstevel@tonic-gate 		int error;
14560Sstevel@tonic-gate 
14570Sstevel@tonic-gate 		error = VOP_DELMAP(svd->vp,
14580Sstevel@tonic-gate 			(offset_t)svd->offset + (uintptr_t)(addr - seg->s_base),
14590Sstevel@tonic-gate 			seg->s_as, addr, len, svd->prot, svd->maxprot,
14600Sstevel@tonic-gate 			svd->type, svd->cred);
14610Sstevel@tonic-gate 
14620Sstevel@tonic-gate 		if (error == EAGAIN)
14630Sstevel@tonic-gate 			return (error);
14640Sstevel@tonic-gate 	}
14650Sstevel@tonic-gate 	/*
14660Sstevel@tonic-gate 	 * Remove any page locks set through this mapping.
14670Sstevel@tonic-gate 	 */
14680Sstevel@tonic-gate 	(void) segvn_lockop(seg, addr, len, 0, MC_UNLOCK, NULL, 0);
14690Sstevel@tonic-gate 
14700Sstevel@tonic-gate 	/*
14710Sstevel@tonic-gate 	 * Unload any hardware translations in the range to be taken out.
14720Sstevel@tonic-gate 	 * Use a callback to invoke free_vp_pages() effectively.
14730Sstevel@tonic-gate 	 */
14740Sstevel@tonic-gate 	if (svd->vp != NULL && free_pages != 0) {
14750Sstevel@tonic-gate 		callback.hcb_data = seg;
14760Sstevel@tonic-gate 		callback.hcb_function = segvn_hat_unload_callback;
14770Sstevel@tonic-gate 		cbp = &callback;
14780Sstevel@tonic-gate 	}
14790Sstevel@tonic-gate 	hat_unload_callback(seg->s_as->a_hat, addr, len, HAT_UNLOAD_UNMAP, cbp);
14800Sstevel@tonic-gate 
14810Sstevel@tonic-gate 	/*
14820Sstevel@tonic-gate 	 * Check for entire segment
14830Sstevel@tonic-gate 	 */
14840Sstevel@tonic-gate 	if (addr == seg->s_base && len == seg->s_size) {
14850Sstevel@tonic-gate 		seg_free(seg);
14860Sstevel@tonic-gate 		return (0);
14870Sstevel@tonic-gate 	}
14880Sstevel@tonic-gate 
14890Sstevel@tonic-gate 	opages = seg_pages(seg);
14900Sstevel@tonic-gate 	dpages = btop(len);
14910Sstevel@tonic-gate 	npages = opages - dpages;
14920Sstevel@tonic-gate 	amp = svd->amp;
14930Sstevel@tonic-gate 
14940Sstevel@tonic-gate 	/*
14950Sstevel@tonic-gate 	 * Check for beginning of segment
14960Sstevel@tonic-gate 	 */
14970Sstevel@tonic-gate 	if (addr == seg->s_base) {
14980Sstevel@tonic-gate 		if (svd->vpage != NULL) {
14990Sstevel@tonic-gate 			size_t nbytes;
15000Sstevel@tonic-gate 			struct vpage *ovpage;
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate 			ovpage = svd->vpage;	/* keep pointer to vpage */
15030Sstevel@tonic-gate 
15040Sstevel@tonic-gate 			nbytes = vpgtob(npages);
15050Sstevel@tonic-gate 			svd->vpage = kmem_alloc(nbytes, KM_SLEEP);
15060Sstevel@tonic-gate 			bcopy(&ovpage[dpages], svd->vpage, nbytes);
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate 			/* free up old vpage */
15090Sstevel@tonic-gate 			kmem_free(ovpage, vpgtob(opages));
15100Sstevel@tonic-gate 		}
15110Sstevel@tonic-gate 		if (amp != NULL) {
15120Sstevel@tonic-gate 			ANON_LOCK_ENTER(&amp->a_rwlock, RW_WRITER);
15130Sstevel@tonic-gate 			if (amp->refcnt == 1 || svd->type == MAP_PRIVATE) {
15140Sstevel@tonic-gate 				/*
15150Sstevel@tonic-gate 				 * Free up now unused parts of anon_map array.
15160Sstevel@tonic-gate 				 */
15170Sstevel@tonic-gate 				if (seg->s_szc != 0) {
15180Sstevel@tonic-gate 					anon_free_pages(amp->ahp,
15190Sstevel@tonic-gate 					    svd->anon_index, len, seg->s_szc);
15200Sstevel@tonic-gate 				} else {
15210Sstevel@tonic-gate 					anon_free(amp->ahp, svd->anon_index,
15220Sstevel@tonic-gate 					    len);
15230Sstevel@tonic-gate 				}
15240Sstevel@tonic-gate 
15250Sstevel@tonic-gate 				/*
15260Sstevel@tonic-gate 				 * Unreserve swap space for the unmapped chunk
15270Sstevel@tonic-gate 				 * of this segment in case it's MAP_SHARED
15280Sstevel@tonic-gate 				 */
15290Sstevel@tonic-gate 				if (svd->type == MAP_SHARED) {
15300Sstevel@tonic-gate 					anon_unresv(len);
15310Sstevel@tonic-gate 					amp->swresv -= len;
15320Sstevel@tonic-gate 				}
15330Sstevel@tonic-gate 			}
15340Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
15350Sstevel@tonic-gate 			svd->anon_index += dpages;
15360Sstevel@tonic-gate 		}
15370Sstevel@tonic-gate 		if (svd->vp != NULL)
15380Sstevel@tonic-gate 			svd->offset += len;
15390Sstevel@tonic-gate 
15400Sstevel@tonic-gate 		if (svd->swresv) {
15410Sstevel@tonic-gate 			if (svd->flags & MAP_NORESERVE) {
15420Sstevel@tonic-gate 				ASSERT(amp);
15430Sstevel@tonic-gate 				oswresv = svd->swresv;
15440Sstevel@tonic-gate 
15450Sstevel@tonic-gate 				svd->swresv = ptob(anon_pages(amp->ahp,
15460Sstevel@tonic-gate 				    svd->anon_index, npages));
15470Sstevel@tonic-gate 				anon_unresv(oswresv - svd->swresv);
15480Sstevel@tonic-gate 			} else {
15490Sstevel@tonic-gate 				anon_unresv(len);
15500Sstevel@tonic-gate 				svd->swresv -= len;
15510Sstevel@tonic-gate 			}
15520Sstevel@tonic-gate 			TRACE_3(TR_FAC_VM, TR_ANON_PROC, "anon proc:%p %lu %u",
15530Sstevel@tonic-gate 				seg, len, 0);
15540Sstevel@tonic-gate 		}
15550Sstevel@tonic-gate 
15560Sstevel@tonic-gate 		seg->s_base += len;
15570Sstevel@tonic-gate 		seg->s_size -= len;
15580Sstevel@tonic-gate 		return (0);
15590Sstevel@tonic-gate 	}
15600Sstevel@tonic-gate 
15610Sstevel@tonic-gate 	/*
15620Sstevel@tonic-gate 	 * Check for end of segment
15630Sstevel@tonic-gate 	 */
15640Sstevel@tonic-gate 	if (addr + len == seg->s_base + seg->s_size) {
15650Sstevel@tonic-gate 		if (svd->vpage != NULL) {
15660Sstevel@tonic-gate 			size_t nbytes;
15670Sstevel@tonic-gate 			struct vpage *ovpage;
15680Sstevel@tonic-gate 
15690Sstevel@tonic-gate 			ovpage = svd->vpage;	/* keep pointer to vpage */
15700Sstevel@tonic-gate 
15710Sstevel@tonic-gate 			nbytes = vpgtob(npages);
15720Sstevel@tonic-gate 			svd->vpage = kmem_alloc(nbytes, KM_SLEEP);
15730Sstevel@tonic-gate 			bcopy(ovpage, svd->vpage, nbytes);
15740Sstevel@tonic-gate 
15750Sstevel@tonic-gate 			/* free up old vpage */
15760Sstevel@tonic-gate 			kmem_free(ovpage, vpgtob(opages));
15770Sstevel@tonic-gate 
15780Sstevel@tonic-gate 		}
15790Sstevel@tonic-gate 		if (amp != NULL) {
15800Sstevel@tonic-gate 			ANON_LOCK_ENTER(&amp->a_rwlock, RW_WRITER);
15810Sstevel@tonic-gate 			if (amp->refcnt == 1 || svd->type == MAP_PRIVATE) {
15820Sstevel@tonic-gate 				/*
15830Sstevel@tonic-gate 				 * Free up now unused parts of anon_map array
15840Sstevel@tonic-gate 				 */
15850Sstevel@tonic-gate 				if (seg->s_szc != 0) {
15860Sstevel@tonic-gate 					ulong_t an_idx = svd->anon_index +
15870Sstevel@tonic-gate 					    npages;
15880Sstevel@tonic-gate 					anon_free_pages(amp->ahp, an_idx,
15890Sstevel@tonic-gate 					    len, seg->s_szc);
15900Sstevel@tonic-gate 				} else {
15910Sstevel@tonic-gate 					anon_free(amp->ahp,
15920Sstevel@tonic-gate 					    svd->anon_index + npages, len);
15930Sstevel@tonic-gate 				}
15940Sstevel@tonic-gate 				/*
15950Sstevel@tonic-gate 				 * Unreserve swap space for the unmapped chunk
15960Sstevel@tonic-gate 				 * of this segment in case it's MAP_SHARED
15970Sstevel@tonic-gate 				 */
15980Sstevel@tonic-gate 				if (svd->type == MAP_SHARED) {
15990Sstevel@tonic-gate 					anon_unresv(len);
16000Sstevel@tonic-gate 					amp->swresv -= len;
16010Sstevel@tonic-gate 				}
16020Sstevel@tonic-gate 			}
16030Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
16040Sstevel@tonic-gate 		}
16050Sstevel@tonic-gate 
16060Sstevel@tonic-gate 		if (svd->swresv) {
16070Sstevel@tonic-gate 			if (svd->flags & MAP_NORESERVE) {
16080Sstevel@tonic-gate 				ASSERT(amp);
16090Sstevel@tonic-gate 				oswresv = svd->swresv;
16100Sstevel@tonic-gate 				svd->swresv = ptob(anon_pages(amp->ahp,
16110Sstevel@tonic-gate 					svd->anon_index, npages));
16120Sstevel@tonic-gate 				anon_unresv(oswresv - svd->swresv);
16130Sstevel@tonic-gate 			} else {
16140Sstevel@tonic-gate 				anon_unresv(len);
16150Sstevel@tonic-gate 				svd->swresv -= len;
16160Sstevel@tonic-gate 			}
16170Sstevel@tonic-gate 			TRACE_3(TR_FAC_VM, TR_ANON_PROC,
16180Sstevel@tonic-gate 				"anon proc:%p %lu %u", seg, len, 0);
16190Sstevel@tonic-gate 		}
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate 		seg->s_size -= len;
16220Sstevel@tonic-gate 		return (0);
16230Sstevel@tonic-gate 	}
16240Sstevel@tonic-gate 
16250Sstevel@tonic-gate 	/*
16260Sstevel@tonic-gate 	 * The section to go is in the middle of the segment,
16270Sstevel@tonic-gate 	 * have to make it into two segments.  nseg is made for
16280Sstevel@tonic-gate 	 * the high end while seg is cut down at the low end.
16290Sstevel@tonic-gate 	 */
16300Sstevel@tonic-gate 	nbase = addr + len;				/* new seg base */
16310Sstevel@tonic-gate 	nsize = (seg->s_base + seg->s_size) - nbase;	/* new seg size */
16320Sstevel@tonic-gate 	seg->s_size = addr - seg->s_base;		/* shrink old seg */
16330Sstevel@tonic-gate 	nseg = seg_alloc(seg->s_as, nbase, nsize);
16340Sstevel@tonic-gate 	if (nseg == NULL) {
16350Sstevel@tonic-gate 		panic("segvn_unmap seg_alloc");
16360Sstevel@tonic-gate 		/*NOTREACHED*/
16370Sstevel@tonic-gate 	}
16380Sstevel@tonic-gate 	nseg->s_ops = seg->s_ops;
16390Sstevel@tonic-gate 	nsvd = kmem_cache_alloc(segvn_cache, KM_SLEEP);
16400Sstevel@tonic-gate 	nseg->s_data = (void *)nsvd;
16410Sstevel@tonic-gate 	nseg->s_szc = seg->s_szc;
16420Sstevel@tonic-gate 	*nsvd = *svd;
16430Sstevel@tonic-gate 	nsvd->offset = svd->offset + (uintptr_t)(nseg->s_base - seg->s_base);
16440Sstevel@tonic-gate 	nsvd->swresv = 0;
16450Sstevel@tonic-gate 	nsvd->softlockcnt = 0;
16460Sstevel@tonic-gate 
16470Sstevel@tonic-gate 	if (svd->vp != NULL) {
16480Sstevel@tonic-gate 		VN_HOLD(nsvd->vp);
16490Sstevel@tonic-gate 		if (nsvd->type == MAP_SHARED)
16500Sstevel@tonic-gate 			lgrp_shm_policy_init(NULL, nsvd->vp);
16510Sstevel@tonic-gate 	}
16520Sstevel@tonic-gate 	crhold(svd->cred);
16530Sstevel@tonic-gate 
16540Sstevel@tonic-gate 	if (svd->vpage == NULL) {
16550Sstevel@tonic-gate 		nsvd->vpage = NULL;
16560Sstevel@tonic-gate 	} else {
16570Sstevel@tonic-gate 		/* need to split vpage into two arrays */
16580Sstevel@tonic-gate 		size_t nbytes;
16590Sstevel@tonic-gate 		struct vpage *ovpage;
16600Sstevel@tonic-gate 
16610Sstevel@tonic-gate 		ovpage = svd->vpage;		/* keep pointer to vpage */
16620Sstevel@tonic-gate 
16630Sstevel@tonic-gate 		npages = seg_pages(seg);	/* seg has shrunk */
16640Sstevel@tonic-gate 		nbytes = vpgtob(npages);
16650Sstevel@tonic-gate 		svd->vpage = kmem_alloc(nbytes, KM_SLEEP);
16660Sstevel@tonic-gate 
16670Sstevel@tonic-gate 		bcopy(ovpage, svd->vpage, nbytes);
16680Sstevel@tonic-gate 
16690Sstevel@tonic-gate 		npages = seg_pages(nseg);
16700Sstevel@tonic-gate 		nbytes = vpgtob(npages);
16710Sstevel@tonic-gate 		nsvd->vpage = kmem_alloc(nbytes, KM_SLEEP);
16720Sstevel@tonic-gate 
16730Sstevel@tonic-gate 		bcopy(&ovpage[opages - npages], nsvd->vpage, nbytes);
16740Sstevel@tonic-gate 
16750Sstevel@tonic-gate 		/* free up old vpage */
16760Sstevel@tonic-gate 		kmem_free(ovpage, vpgtob(opages));
16770Sstevel@tonic-gate 	}
16780Sstevel@tonic-gate 
16790Sstevel@tonic-gate 	if (amp == NULL) {
16800Sstevel@tonic-gate 		nsvd->amp = NULL;
16810Sstevel@tonic-gate 		nsvd->anon_index = 0;
16820Sstevel@tonic-gate 	} else {
16830Sstevel@tonic-gate 		/*
16840Sstevel@tonic-gate 		 * Need to create a new anon map for the new segment.
16850Sstevel@tonic-gate 		 * We'll also allocate a new smaller array for the old
16860Sstevel@tonic-gate 		 * smaller segment to save space.
16870Sstevel@tonic-gate 		 */
16880Sstevel@tonic-gate 		opages = btop((uintptr_t)(addr - seg->s_base));
16890Sstevel@tonic-gate 		ANON_LOCK_ENTER(&amp->a_rwlock, RW_WRITER);
16900Sstevel@tonic-gate 		if (amp->refcnt == 1 || svd->type == MAP_PRIVATE) {
16910Sstevel@tonic-gate 			/*
16920Sstevel@tonic-gate 			 * Free up now unused parts of anon_map array
16930Sstevel@tonic-gate 			 */
16940Sstevel@tonic-gate 			if (seg->s_szc != 0) {
16950Sstevel@tonic-gate 				ulong_t an_idx = svd->anon_index + opages;
16960Sstevel@tonic-gate 				anon_free_pages(amp->ahp, an_idx, len,
16970Sstevel@tonic-gate 				    seg->s_szc);
16980Sstevel@tonic-gate 			} else {
16990Sstevel@tonic-gate 				anon_free(amp->ahp, svd->anon_index + opages,
17000Sstevel@tonic-gate 				    len);
17010Sstevel@tonic-gate 			}
17020Sstevel@tonic-gate 
17030Sstevel@tonic-gate 			/*
17040Sstevel@tonic-gate 			 * Unreserve swap space for the unmapped chunk
17050Sstevel@tonic-gate 			 * of this segment in case it's MAP_SHARED
17060Sstevel@tonic-gate 			 */
17070Sstevel@tonic-gate 			if (svd->type == MAP_SHARED) {
17080Sstevel@tonic-gate 				anon_unresv(len);
17090Sstevel@tonic-gate 				amp->swresv -= len;
17100Sstevel@tonic-gate 			}
17110Sstevel@tonic-gate 		}
17120Sstevel@tonic-gate 
17130Sstevel@tonic-gate 		nsvd->anon_index = svd->anon_index +
17140Sstevel@tonic-gate 		    btop((uintptr_t)(nseg->s_base - seg->s_base));
17150Sstevel@tonic-gate 		if (svd->type == MAP_SHARED) {
17160Sstevel@tonic-gate 			ASSERT(seg->s_szc == 0);
17170Sstevel@tonic-gate 			amp->refcnt++;
17180Sstevel@tonic-gate 			nsvd->amp = amp;
17190Sstevel@tonic-gate 		} else {
17200Sstevel@tonic-gate 			struct anon_map *namp;
17210Sstevel@tonic-gate 			struct anon_hdr *nahp;
17220Sstevel@tonic-gate 
17230Sstevel@tonic-gate 			ASSERT(svd->type == MAP_PRIVATE);
17240Sstevel@tonic-gate 			nahp = anon_create(btop(seg->s_size), ANON_SLEEP);
17250Sstevel@tonic-gate 			namp = anonmap_alloc(nseg->s_size, 0);
17260Sstevel@tonic-gate 			namp->a_szc = seg->s_szc;
17270Sstevel@tonic-gate 			(void) anon_copy_ptr(amp->ahp, svd->anon_index, nahp,
17280Sstevel@tonic-gate 			    0, btop(seg->s_size), ANON_SLEEP);
17290Sstevel@tonic-gate 			(void) anon_copy_ptr(amp->ahp, nsvd->anon_index,
17300Sstevel@tonic-gate 			    namp->ahp, 0, btop(nseg->s_size), ANON_SLEEP);
17310Sstevel@tonic-gate 			anon_release(amp->ahp, btop(amp->size));
17320Sstevel@tonic-gate 			svd->anon_index = 0;
17330Sstevel@tonic-gate 			nsvd->anon_index = 0;
17340Sstevel@tonic-gate 			amp->ahp = nahp;
17350Sstevel@tonic-gate 			amp->size = seg->s_size;
17360Sstevel@tonic-gate 			nsvd->amp = namp;
17370Sstevel@tonic-gate 		}
17380Sstevel@tonic-gate 		ANON_LOCK_EXIT(&amp->a_rwlock);
17390Sstevel@tonic-gate 	}
17400Sstevel@tonic-gate 	if (svd->swresv) {
17410Sstevel@tonic-gate 		if (svd->flags & MAP_NORESERVE) {
17420Sstevel@tonic-gate 			ASSERT(amp);
17430Sstevel@tonic-gate 			oswresv = svd->swresv;
17440Sstevel@tonic-gate 			svd->swresv = ptob(anon_pages(amp->ahp,
17450Sstevel@tonic-gate 				svd->anon_index, btop(seg->s_size)));
17460Sstevel@tonic-gate 			nsvd->swresv = ptob(anon_pages(nsvd->amp->ahp,
17470Sstevel@tonic-gate 				nsvd->anon_index, btop(nseg->s_size)));
17480Sstevel@tonic-gate 			ASSERT(oswresv >= (svd->swresv + nsvd->swresv));
17490Sstevel@tonic-gate 			anon_unresv(oswresv - (svd->swresv + nsvd->swresv));
17500Sstevel@tonic-gate 		} else {
17510Sstevel@tonic-gate 			if (seg->s_size + nseg->s_size + len != svd->swresv) {
17520Sstevel@tonic-gate 				panic("segvn_unmap: "
17530Sstevel@tonic-gate 				    "cannot split swap reservation");
17540Sstevel@tonic-gate 				/*NOTREACHED*/
17550Sstevel@tonic-gate 			}
17560Sstevel@tonic-gate 			anon_unresv(len);
17570Sstevel@tonic-gate 			svd->swresv = seg->s_size;
17580Sstevel@tonic-gate 			nsvd->swresv = nseg->s_size;
17590Sstevel@tonic-gate 		}
17600Sstevel@tonic-gate 		TRACE_3(TR_FAC_VM, TR_ANON_PROC, "anon proc:%p %lu %u",
17610Sstevel@tonic-gate 			seg, len, 0);
17620Sstevel@tonic-gate 	}
17630Sstevel@tonic-gate 
17640Sstevel@tonic-gate 	return (0);			/* I'm glad that's all over with! */
17650Sstevel@tonic-gate }
17660Sstevel@tonic-gate 
17670Sstevel@tonic-gate static void
17680Sstevel@tonic-gate segvn_free(struct seg *seg)
17690Sstevel@tonic-gate {
17700Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
17710Sstevel@tonic-gate 	pgcnt_t npages = seg_pages(seg);
17720Sstevel@tonic-gate 	struct anon_map *amp;
17730Sstevel@tonic-gate 	size_t len;
17740Sstevel@tonic-gate 
17750Sstevel@tonic-gate 	/*
17760Sstevel@tonic-gate 	 * We don't need any segment level locks for "segvn" data
17770Sstevel@tonic-gate 	 * since the address space is "write" locked.
17780Sstevel@tonic-gate 	 */
17790Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
17800Sstevel@tonic-gate 
17810Sstevel@tonic-gate 	/*
17820Sstevel@tonic-gate 	 * Be sure to unlock pages. XXX Why do things get free'ed instead
17830Sstevel@tonic-gate 	 * of unmapped? XXX
17840Sstevel@tonic-gate 	 */
17850Sstevel@tonic-gate 	(void) segvn_lockop(seg, seg->s_base, seg->s_size,
17860Sstevel@tonic-gate 	    0, MC_UNLOCK, NULL, 0);
17870Sstevel@tonic-gate 
17880Sstevel@tonic-gate 	/*
17890Sstevel@tonic-gate 	 * Deallocate the vpage and anon pointers if necessary and possible.
17900Sstevel@tonic-gate 	 */
17910Sstevel@tonic-gate 	if (svd->vpage != NULL) {
17920Sstevel@tonic-gate 		kmem_free(svd->vpage, vpgtob(npages));
17930Sstevel@tonic-gate 		svd->vpage = NULL;
17940Sstevel@tonic-gate 	}
17950Sstevel@tonic-gate 	if ((amp = svd->amp) != NULL) {
17960Sstevel@tonic-gate 		/*
17970Sstevel@tonic-gate 		 * If there are no more references to this anon_map
17980Sstevel@tonic-gate 		 * structure, then deallocate the structure after freeing
17990Sstevel@tonic-gate 		 * up all the anon slot pointers that we can.
18000Sstevel@tonic-gate 		 */
18010Sstevel@tonic-gate 		ANON_LOCK_ENTER(&amp->a_rwlock, RW_WRITER);
18020Sstevel@tonic-gate 		if (--amp->refcnt == 0) {
18030Sstevel@tonic-gate 			if (svd->type == MAP_PRIVATE) {
18040Sstevel@tonic-gate 				/*
18050Sstevel@tonic-gate 				 * Private - we only need to anon_free
18060Sstevel@tonic-gate 				 * the part that this segment refers to.
18070Sstevel@tonic-gate 				 */
18080Sstevel@tonic-gate 				if (seg->s_szc != 0) {
18090Sstevel@tonic-gate 					anon_free_pages(amp->ahp,
18100Sstevel@tonic-gate 					    svd->anon_index, seg->s_size,
18110Sstevel@tonic-gate 					    seg->s_szc);
18120Sstevel@tonic-gate 				} else {
18130Sstevel@tonic-gate 					anon_free(amp->ahp, svd->anon_index,
18140Sstevel@tonic-gate 					    seg->s_size);
18150Sstevel@tonic-gate 				}
18160Sstevel@tonic-gate 			} else {
18170Sstevel@tonic-gate 				/*
18180Sstevel@tonic-gate 				 * Shared - anon_free the entire
18190Sstevel@tonic-gate 				 * anon_map's worth of stuff and
18200Sstevel@tonic-gate 				 * release any swap reservation.
18210Sstevel@tonic-gate 				 */
18220Sstevel@tonic-gate 				ASSERT(seg->s_szc == 0);
18230Sstevel@tonic-gate 				anon_free(amp->ahp, 0, amp->size);
18240Sstevel@tonic-gate 				if ((len = amp->swresv) != 0) {
18250Sstevel@tonic-gate 					anon_unresv(len);
18260Sstevel@tonic-gate 					TRACE_3(TR_FAC_VM, TR_ANON_PROC,
18270Sstevel@tonic-gate 						"anon proc:%p %lu %u",
18280Sstevel@tonic-gate 						seg, len, 0);
18290Sstevel@tonic-gate 				}
18300Sstevel@tonic-gate 			}
18310Sstevel@tonic-gate 			svd->amp = NULL;
18320Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
18330Sstevel@tonic-gate 			anonmap_free(amp);
18340Sstevel@tonic-gate 		} else if (svd->type == MAP_PRIVATE) {
18350Sstevel@tonic-gate 			/*
18360Sstevel@tonic-gate 			 * We had a private mapping which still has
18370Sstevel@tonic-gate 			 * a held anon_map so just free up all the
18380Sstevel@tonic-gate 			 * anon slot pointers that we were using.
18390Sstevel@tonic-gate 			 */
18400Sstevel@tonic-gate 			if (seg->s_szc != 0) {
18410Sstevel@tonic-gate 				anon_free_pages(amp->ahp, svd->anon_index,
18420Sstevel@tonic-gate 				    seg->s_size, seg->s_szc);
18430Sstevel@tonic-gate 			} else {
18440Sstevel@tonic-gate 				anon_free(amp->ahp, svd->anon_index,
18450Sstevel@tonic-gate 				    seg->s_size);
18460Sstevel@tonic-gate 			}
18470Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
18480Sstevel@tonic-gate 		} else {
18490Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
18500Sstevel@tonic-gate 		}
18510Sstevel@tonic-gate 	}
18520Sstevel@tonic-gate 
18530Sstevel@tonic-gate 	/*
18540Sstevel@tonic-gate 	 * Release swap reservation.
18550Sstevel@tonic-gate 	 */
18560Sstevel@tonic-gate 	if ((len = svd->swresv) != 0) {
18570Sstevel@tonic-gate 		anon_unresv(svd->swresv);
18580Sstevel@tonic-gate 		TRACE_3(TR_FAC_VM, TR_ANON_PROC, "anon proc:%p %lu %u",
18590Sstevel@tonic-gate 			seg, len, 0);
18600Sstevel@tonic-gate 		svd->swresv = 0;
18610Sstevel@tonic-gate 	}
18620Sstevel@tonic-gate 	/*
18630Sstevel@tonic-gate 	 * Release claim on vnode, credentials, and finally free the
18640Sstevel@tonic-gate 	 * private data.
18650Sstevel@tonic-gate 	 */
18660Sstevel@tonic-gate 	if (svd->vp != NULL) {
18670Sstevel@tonic-gate 		if (svd->type == MAP_SHARED)
18680Sstevel@tonic-gate 			lgrp_shm_policy_fini(NULL, svd->vp);
18690Sstevel@tonic-gate 		VN_RELE(svd->vp);
18700Sstevel@tonic-gate 		svd->vp = NULL;
18710Sstevel@tonic-gate 	}
18720Sstevel@tonic-gate 	crfree(svd->cred);
18730Sstevel@tonic-gate 	svd->cred = NULL;
18740Sstevel@tonic-gate 
18750Sstevel@tonic-gate 	seg->s_data = NULL;
18760Sstevel@tonic-gate 	kmem_cache_free(segvn_cache, svd);
18770Sstevel@tonic-gate }
18780Sstevel@tonic-gate 
18790Sstevel@tonic-gate /*
18800Sstevel@tonic-gate  * Do a F_SOFTUNLOCK call over the range requested.  The range must have
18810Sstevel@tonic-gate  * already been F_SOFTLOCK'ed.
18820Sstevel@tonic-gate  * Caller must always match addr and len of a softunlock with a previous
18830Sstevel@tonic-gate  * softlock with exactly the same addr and len.
18840Sstevel@tonic-gate  */
18850Sstevel@tonic-gate static void
18860Sstevel@tonic-gate segvn_softunlock(struct seg *seg, caddr_t addr, size_t len, enum seg_rw rw)
18870Sstevel@tonic-gate {
18880Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
18890Sstevel@tonic-gate 	page_t *pp;
18900Sstevel@tonic-gate 	caddr_t adr;
18910Sstevel@tonic-gate 	struct vnode *vp;
18920Sstevel@tonic-gate 	u_offset_t offset;
18930Sstevel@tonic-gate 	ulong_t anon_index;
18940Sstevel@tonic-gate 	struct anon_map *amp;
18950Sstevel@tonic-gate 	struct anon *ap = NULL;
18960Sstevel@tonic-gate 
18970Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
18980Sstevel@tonic-gate 	ASSERT(SEGVN_LOCK_HELD(seg->s_as, &svd->lock));
18990Sstevel@tonic-gate 
19000Sstevel@tonic-gate 	if ((amp = svd->amp) != NULL)
19010Sstevel@tonic-gate 		anon_index = svd->anon_index + seg_page(seg, addr);
19020Sstevel@tonic-gate 
19030Sstevel@tonic-gate 	hat_unlock(seg->s_as->a_hat, addr, len);
19040Sstevel@tonic-gate 	for (adr = addr; adr < addr + len; adr += PAGESIZE) {
19050Sstevel@tonic-gate 		if (amp != NULL) {
19060Sstevel@tonic-gate 			ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
19070Sstevel@tonic-gate 			if ((ap = anon_get_ptr(amp->ahp, anon_index++))
19080Sstevel@tonic-gate 								!= NULL) {
19090Sstevel@tonic-gate 				swap_xlate(ap, &vp, &offset);
19100Sstevel@tonic-gate 			} else {
19110Sstevel@tonic-gate 				vp = svd->vp;
19120Sstevel@tonic-gate 				offset = svd->offset +
19130Sstevel@tonic-gate 				    (uintptr_t)(adr - seg->s_base);
19140Sstevel@tonic-gate 			}
19150Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
19160Sstevel@tonic-gate 		} else {
19170Sstevel@tonic-gate 			vp = svd->vp;
19180Sstevel@tonic-gate 			offset = svd->offset +
19190Sstevel@tonic-gate 			    (uintptr_t)(adr - seg->s_base);
19200Sstevel@tonic-gate 		}
19210Sstevel@tonic-gate 
19220Sstevel@tonic-gate 		/*
19230Sstevel@tonic-gate 		 * Use page_find() instead of page_lookup() to
19240Sstevel@tonic-gate 		 * find the page since we know that it is locked.
19250Sstevel@tonic-gate 		 */
19260Sstevel@tonic-gate 		pp = page_find(vp, offset);
19270Sstevel@tonic-gate 		if (pp == NULL) {
19280Sstevel@tonic-gate 			panic(
19290Sstevel@tonic-gate 			    "segvn_softunlock: addr %p, ap %p, vp %p, off %llx",
19300Sstevel@tonic-gate 			    (void *)adr, (void *)ap, (void *)vp, offset);
19310Sstevel@tonic-gate 			/*NOTREACHED*/
19320Sstevel@tonic-gate 		}
19330Sstevel@tonic-gate 
19340Sstevel@tonic-gate 		if (rw == S_WRITE) {
19350Sstevel@tonic-gate 			hat_setrefmod(pp);
19360Sstevel@tonic-gate 			if (seg->s_as->a_vbits)
19370Sstevel@tonic-gate 				hat_setstat(seg->s_as, adr, PAGESIZE,
19380Sstevel@tonic-gate 				    P_REF | P_MOD);
19390Sstevel@tonic-gate 		} else if (rw != S_OTHER) {
19400Sstevel@tonic-gate 			hat_setref(pp);
19410Sstevel@tonic-gate 			if (seg->s_as->a_vbits)
19420Sstevel@tonic-gate 				hat_setstat(seg->s_as, adr, PAGESIZE, P_REF);
19430Sstevel@tonic-gate 		}
19440Sstevel@tonic-gate 		TRACE_3(TR_FAC_VM, TR_SEGVN_FAULT,
19450Sstevel@tonic-gate 			"segvn_fault:pp %p vp %p offset %llx", pp, vp, offset);
19460Sstevel@tonic-gate 		page_unlock(pp);
19470Sstevel@tonic-gate 	}
19480Sstevel@tonic-gate 	mutex_enter(&freemem_lock); /* for availrmem */
19490Sstevel@tonic-gate 	availrmem += btop(len);
19500Sstevel@tonic-gate 	segvn_pages_locked -= btop(len);
19510Sstevel@tonic-gate 	svd->softlockcnt -= btop(len);
19520Sstevel@tonic-gate 	mutex_exit(&freemem_lock);
19530Sstevel@tonic-gate 	if (svd->softlockcnt == 0) {
19540Sstevel@tonic-gate 		/*
19550Sstevel@tonic-gate 		 * All SOFTLOCKS are gone. Wakeup any waiting
19560Sstevel@tonic-gate 		 * unmappers so they can try again to unmap.
19570Sstevel@tonic-gate 		 * Check for waiters first without the mutex
19580Sstevel@tonic-gate 		 * held so we don't always grab the mutex on
19590Sstevel@tonic-gate 		 * softunlocks.
19600Sstevel@tonic-gate 		 */
19610Sstevel@tonic-gate 		if (AS_ISUNMAPWAIT(seg->s_as)) {
19620Sstevel@tonic-gate 			mutex_enter(&seg->s_as->a_contents);
19630Sstevel@tonic-gate 			if (AS_ISUNMAPWAIT(seg->s_as)) {
19640Sstevel@tonic-gate 				AS_CLRUNMAPWAIT(seg->s_as);
19650Sstevel@tonic-gate 				cv_broadcast(&seg->s_as->a_cv);
19660Sstevel@tonic-gate 			}
19670Sstevel@tonic-gate 			mutex_exit(&seg->s_as->a_contents);
19680Sstevel@tonic-gate 		}
19690Sstevel@tonic-gate 	}
19700Sstevel@tonic-gate }
19710Sstevel@tonic-gate 
19720Sstevel@tonic-gate #define	PAGE_HANDLED	((page_t *)-1)
19730Sstevel@tonic-gate 
19740Sstevel@tonic-gate /*
19750Sstevel@tonic-gate  * Release all the pages in the NULL terminated ppp list
19760Sstevel@tonic-gate  * which haven't already been converted to PAGE_HANDLED.
19770Sstevel@tonic-gate  */
19780Sstevel@tonic-gate static void
19790Sstevel@tonic-gate segvn_pagelist_rele(page_t **ppp)
19800Sstevel@tonic-gate {
19810Sstevel@tonic-gate 	for (; *ppp != NULL; ppp++) {
19820Sstevel@tonic-gate 		if (*ppp != PAGE_HANDLED)
19830Sstevel@tonic-gate 			page_unlock(*ppp);
19840Sstevel@tonic-gate 	}
19850Sstevel@tonic-gate }
19860Sstevel@tonic-gate 
19870Sstevel@tonic-gate static int stealcow = 1;
19880Sstevel@tonic-gate 
19890Sstevel@tonic-gate /*
19900Sstevel@tonic-gate  * Workaround for viking chip bug.  See bug id 1220902.
19910Sstevel@tonic-gate  * To fix this down in pagefault() would require importing so
19920Sstevel@tonic-gate  * much as and segvn code as to be unmaintainable.
19930Sstevel@tonic-gate  */
19940Sstevel@tonic-gate int enable_mbit_wa = 0;
19950Sstevel@tonic-gate 
19960Sstevel@tonic-gate /*
19970Sstevel@tonic-gate  * Handles all the dirty work of getting the right
19980Sstevel@tonic-gate  * anonymous pages and loading up the translations.
19990Sstevel@tonic-gate  * This routine is called only from segvn_fault()
20000Sstevel@tonic-gate  * when looping over the range of addresses requested.
20010Sstevel@tonic-gate  *
20020Sstevel@tonic-gate  * The basic algorithm here is:
20030Sstevel@tonic-gate  * 	If this is an anon_zero case
20040Sstevel@tonic-gate  *		Call anon_zero to allocate page
20050Sstevel@tonic-gate  *		Load up translation
20060Sstevel@tonic-gate  *		Return
20070Sstevel@tonic-gate  *	endif
20080Sstevel@tonic-gate  *	If this is an anon page
20090Sstevel@tonic-gate  *		Use anon_getpage to get the page
20100Sstevel@tonic-gate  *	else
20110Sstevel@tonic-gate  *		Find page in pl[] list passed in
20120Sstevel@tonic-gate  *	endif
20130Sstevel@tonic-gate  *	If not a cow
20140Sstevel@tonic-gate  *		Load up the translation to the page
20150Sstevel@tonic-gate  *		return
20160Sstevel@tonic-gate  *	endif
20170Sstevel@tonic-gate  *	Call anon_private to handle cow
20180Sstevel@tonic-gate  *	Load up (writable) translation to new page
20190Sstevel@tonic-gate  */
20200Sstevel@tonic-gate static faultcode_t
20210Sstevel@tonic-gate segvn_faultpage(
20220Sstevel@tonic-gate 	struct hat *hat,		/* the hat to use for mapping */
20230Sstevel@tonic-gate 	struct seg *seg,		/* seg_vn of interest */
20240Sstevel@tonic-gate 	caddr_t addr,			/* address in as */
20250Sstevel@tonic-gate 	u_offset_t off,			/* offset in vp */
20260Sstevel@tonic-gate 	struct vpage *vpage,		/* pointer to vpage for vp, off */
20270Sstevel@tonic-gate 	page_t *pl[],			/* object source page pointer */
20280Sstevel@tonic-gate 	uint_t vpprot,			/* access allowed to object pages */
20290Sstevel@tonic-gate 	enum fault_type type,		/* type of fault */
20300Sstevel@tonic-gate 	enum seg_rw rw,			/* type of access at fault */
20310Sstevel@tonic-gate 	int brkcow)			/* we may need to break cow */
20320Sstevel@tonic-gate {
20330Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
20340Sstevel@tonic-gate 	page_t *pp, **ppp;
20350Sstevel@tonic-gate 	uint_t pageflags = 0;
20360Sstevel@tonic-gate 	page_t *anon_pl[1 + 1];
20370Sstevel@tonic-gate 	page_t *opp = NULL;		/* original page */
20380Sstevel@tonic-gate 	uint_t prot;
20390Sstevel@tonic-gate 	int err;
20400Sstevel@tonic-gate 	int cow;
20410Sstevel@tonic-gate 	int claim;
20420Sstevel@tonic-gate 	int steal = 0;
20430Sstevel@tonic-gate 	ulong_t anon_index;
20440Sstevel@tonic-gate 	struct anon *ap, *oldap;
20450Sstevel@tonic-gate 	struct anon_map *amp;
20460Sstevel@tonic-gate 	int hat_flag = (type == F_SOFTLOCK) ? HAT_LOAD_LOCK : HAT_LOAD;
20470Sstevel@tonic-gate 	int anon_lock = 0;
20480Sstevel@tonic-gate 	anon_sync_obj_t cookie;
20490Sstevel@tonic-gate 
20500Sstevel@tonic-gate 	if (svd->flags & MAP_TEXT) {
20510Sstevel@tonic-gate 		hat_flag |= HAT_LOAD_TEXT;
20520Sstevel@tonic-gate 	}
20530Sstevel@tonic-gate 
20540Sstevel@tonic-gate 	ASSERT(SEGVN_READ_HELD(seg->s_as, &svd->lock));
20550Sstevel@tonic-gate 	ASSERT(seg->s_szc == 0);
20560Sstevel@tonic-gate 
20570Sstevel@tonic-gate 	/*
20580Sstevel@tonic-gate 	 * Initialize protection value for this page.
20590Sstevel@tonic-gate 	 * If we have per page protection values check it now.
20600Sstevel@tonic-gate 	 */
20610Sstevel@tonic-gate 	if (svd->pageprot) {
20620Sstevel@tonic-gate 		uint_t protchk;
20630Sstevel@tonic-gate 
20640Sstevel@tonic-gate 		switch (rw) {
20650Sstevel@tonic-gate 		case S_READ:
20660Sstevel@tonic-gate 			protchk = PROT_READ;
20670Sstevel@tonic-gate 			break;
20680Sstevel@tonic-gate 		case S_WRITE:
20690Sstevel@tonic-gate 			protchk = PROT_WRITE;
20700Sstevel@tonic-gate 			break;
20710Sstevel@tonic-gate 		case S_EXEC:
20720Sstevel@tonic-gate 			protchk = PROT_EXEC;
20730Sstevel@tonic-gate 			break;
20740Sstevel@tonic-gate 		case S_OTHER:
20750Sstevel@tonic-gate 		default:
20760Sstevel@tonic-gate 			protchk = PROT_READ | PROT_WRITE | PROT_EXEC;
20770Sstevel@tonic-gate 			break;
20780Sstevel@tonic-gate 		}
20790Sstevel@tonic-gate 
20800Sstevel@tonic-gate 		prot = VPP_PROT(vpage);
20810Sstevel@tonic-gate 		if ((prot & protchk) == 0)
20820Sstevel@tonic-gate 			return (FC_PROT);	/* illegal access type */
20830Sstevel@tonic-gate 	} else {
20840Sstevel@tonic-gate 		prot = svd->prot;
20850Sstevel@tonic-gate 	}
20860Sstevel@tonic-gate 
20870Sstevel@tonic-gate 	if (type == F_SOFTLOCK) {
20880Sstevel@tonic-gate 		mutex_enter(&freemem_lock);
20890Sstevel@tonic-gate 		if (availrmem <= tune.t_minarmem) {
20900Sstevel@tonic-gate 			mutex_exit(&freemem_lock);
20910Sstevel@tonic-gate 			return (FC_MAKE_ERR(ENOMEM));	/* out of real memory */
20920Sstevel@tonic-gate 		} else {
20930Sstevel@tonic-gate 			svd->softlockcnt++;
20940Sstevel@tonic-gate 			availrmem--;
20950Sstevel@tonic-gate 			segvn_pages_locked++;
20960Sstevel@tonic-gate 		}
20970Sstevel@tonic-gate 		mutex_exit(&freemem_lock);
20980Sstevel@tonic-gate 	}
20990Sstevel@tonic-gate 
21000Sstevel@tonic-gate 	/*
21010Sstevel@tonic-gate 	 * Always acquire the anon array lock to prevent 2 threads from
21020Sstevel@tonic-gate 	 * allocating separate anon slots for the same "addr".
21030Sstevel@tonic-gate 	 */
21040Sstevel@tonic-gate 
21050Sstevel@tonic-gate 	if ((amp = svd->amp) != NULL) {
21060Sstevel@tonic-gate 		ASSERT(RW_READ_HELD(&amp->a_rwlock));
21070Sstevel@tonic-gate 		anon_index = svd->anon_index + seg_page(seg, addr);
21080Sstevel@tonic-gate 		anon_array_enter(amp, anon_index, &cookie);
21090Sstevel@tonic-gate 		anon_lock = 1;
21100Sstevel@tonic-gate 	}
21110Sstevel@tonic-gate 
21120Sstevel@tonic-gate 	if (svd->vp == NULL && amp != NULL) {
21130Sstevel@tonic-gate 		if ((ap = anon_get_ptr(amp->ahp, anon_index)) == NULL) {
21140Sstevel@tonic-gate 			/*
21150Sstevel@tonic-gate 			 * Allocate a (normally) writable anonymous page of
21160Sstevel@tonic-gate 			 * zeroes. If no advance reservations, reserve now.
21170Sstevel@tonic-gate 			 */
21180Sstevel@tonic-gate 			if (svd->flags & MAP_NORESERVE) {
21190Sstevel@tonic-gate 				if (anon_resv(ptob(1))) {
21200Sstevel@tonic-gate 					svd->swresv += ptob(1);
21210Sstevel@tonic-gate 				} else {
21220Sstevel@tonic-gate 					err = ENOMEM;
21230Sstevel@tonic-gate 					goto out;
21240Sstevel@tonic-gate 				}
21250Sstevel@tonic-gate 			}
21260Sstevel@tonic-gate 			if ((pp = anon_zero(seg, addr, &ap,
21270Sstevel@tonic-gate 			    svd->cred)) == NULL) {
21280Sstevel@tonic-gate 				err = ENOMEM;
21290Sstevel@tonic-gate 				goto out;	/* out of swap space */
21300Sstevel@tonic-gate 			}
21310Sstevel@tonic-gate 			/*
21320Sstevel@tonic-gate 			 * Re-acquire the anon_map lock and
21330Sstevel@tonic-gate 			 * initialize the anon array entry.
21340Sstevel@tonic-gate 			 */
21350Sstevel@tonic-gate 			(void) anon_set_ptr(amp->ahp, anon_index, ap,
21360Sstevel@tonic-gate 				ANON_SLEEP);
21370Sstevel@tonic-gate 			if (enable_mbit_wa) {
21380Sstevel@tonic-gate 				if (rw == S_WRITE)
21390Sstevel@tonic-gate 					hat_setmod(pp);
21400Sstevel@tonic-gate 				else if (!hat_ismod(pp))
21410Sstevel@tonic-gate 					prot &= ~PROT_WRITE;
21420Sstevel@tonic-gate 			}
21430Sstevel@tonic-gate 			/*
21440Sstevel@tonic-gate 			 * If AS_PAGLCK is set in a_flags (via memcntl(2)
21450Sstevel@tonic-gate 			 * with MC_LOCKAS, MCL_FUTURE) and this is a
21460Sstevel@tonic-gate 			 * MAP_NORESERVE segment, we may need to
21470Sstevel@tonic-gate 			 * permanently lock the page as it is being faulted
21480Sstevel@tonic-gate 			 * for the first time. The following text applies
21490Sstevel@tonic-gate 			 * only to MAP_NORESERVE segments:
21500Sstevel@tonic-gate 			 *
21510Sstevel@tonic-gate 			 * As per memcntl(2), if this segment was created
21520Sstevel@tonic-gate 			 * after MCL_FUTURE was applied (a "future"
21530Sstevel@tonic-gate 			 * segment), its pages must be locked.  If this
21540Sstevel@tonic-gate 			 * segment existed at MCL_FUTURE application (a
21550Sstevel@tonic-gate 			 * "past" segment), the interface is unclear.
21560Sstevel@tonic-gate 			 *
21570Sstevel@tonic-gate 			 * We decide to lock only if vpage is present:
21580Sstevel@tonic-gate 			 *
21590Sstevel@tonic-gate 			 * - "future" segments will have a vpage array (see
21600Sstevel@tonic-gate 			 *    as_map), and so will be locked as required
21610Sstevel@tonic-gate 			 *
21620Sstevel@tonic-gate 			 * - "past" segments may not have a vpage array,
21630Sstevel@tonic-gate 			 *    depending on whether events (such as
21640Sstevel@tonic-gate 			 *    mprotect) have occurred. Locking if vpage
21650Sstevel@tonic-gate 			 *    exists will preserve legacy behavior.  Not
21660Sstevel@tonic-gate 			 *    locking if vpage is absent, will not break
21670Sstevel@tonic-gate 			 *    the interface or legacy behavior.  Note that
21680Sstevel@tonic-gate 			 *    allocating vpage here if it's absent requires
21690Sstevel@tonic-gate 			 *    upgrading the segvn reader lock, the cost of
21700Sstevel@tonic-gate 			 *    which does not seem worthwhile.
21710Sstevel@tonic-gate 			 */
21720Sstevel@tonic-gate 			if (AS_ISPGLCK(seg->s_as) && vpage != NULL &&
21730Sstevel@tonic-gate 			    (svd->flags & MAP_NORESERVE)) {
21740Sstevel@tonic-gate 				claim = VPP_PROT(vpage) & PROT_WRITE;
21750Sstevel@tonic-gate 				ASSERT(svd->type == MAP_PRIVATE);
21760Sstevel@tonic-gate 				if (page_pp_lock(pp, claim, 0))
21770Sstevel@tonic-gate 					VPP_SETPPLOCK(vpage);
21780Sstevel@tonic-gate 			}
21790Sstevel@tonic-gate 
21800Sstevel@tonic-gate 
21810Sstevel@tonic-gate 			/*
21820Sstevel@tonic-gate 			 * Handle pages that have been marked for migration
21830Sstevel@tonic-gate 			 */
21840Sstevel@tonic-gate 			if (lgrp_optimizations())
21850Sstevel@tonic-gate 				page_migrate(seg, addr, &pp, 1);
21860Sstevel@tonic-gate 			hat_memload(hat, addr, pp, prot, hat_flag);
21870Sstevel@tonic-gate 
21880Sstevel@tonic-gate 			if (!(hat_flag & HAT_LOAD_LOCK))
21890Sstevel@tonic-gate 				page_unlock(pp);
21900Sstevel@tonic-gate 
21910Sstevel@tonic-gate 			anon_array_exit(&cookie);
21920Sstevel@tonic-gate 			return (0);
21930Sstevel@tonic-gate 		}
21940Sstevel@tonic-gate 	}
21950Sstevel@tonic-gate 
21960Sstevel@tonic-gate 	/*
21970Sstevel@tonic-gate 	 * Obtain the page structure via anon_getpage() if it is
21980Sstevel@tonic-gate 	 * a private copy of an object (the result of a previous
21990Sstevel@tonic-gate 	 * copy-on-write).
22000Sstevel@tonic-gate 	 */
22010Sstevel@tonic-gate 	if (amp != NULL) {
22020Sstevel@tonic-gate 		if ((ap = anon_get_ptr(amp->ahp, anon_index)) != NULL) {
22030Sstevel@tonic-gate 			err = anon_getpage(&ap, &vpprot, anon_pl, PAGESIZE,
22040Sstevel@tonic-gate 			    seg, addr, rw, svd->cred);
22050Sstevel@tonic-gate 			if (err)
22060Sstevel@tonic-gate 				goto out;
22070Sstevel@tonic-gate 
22080Sstevel@tonic-gate 			if (svd->type == MAP_SHARED) {
22090Sstevel@tonic-gate 				/*
22100Sstevel@tonic-gate 				 * If this is a shared mapping to an
22110Sstevel@tonic-gate 				 * anon_map, then ignore the write
22120Sstevel@tonic-gate 				 * permissions returned by anon_getpage().
22130Sstevel@tonic-gate 				 * They apply to the private mappings
22140Sstevel@tonic-gate 				 * of this anon_map.
22150Sstevel@tonic-gate 				 */
22160Sstevel@tonic-gate 				vpprot |= PROT_WRITE;
22170Sstevel@tonic-gate 			}
22180Sstevel@tonic-gate 			opp = anon_pl[0];
22190Sstevel@tonic-gate 		}
22200Sstevel@tonic-gate 	}
22210Sstevel@tonic-gate 
22220Sstevel@tonic-gate 	/*
22230Sstevel@tonic-gate 	 * Search the pl[] list passed in if it is from the
22240Sstevel@tonic-gate 	 * original object (i.e., not a private copy).
22250Sstevel@tonic-gate 	 */
22260Sstevel@tonic-gate 	if (opp == NULL) {
22270Sstevel@tonic-gate 		/*
22280Sstevel@tonic-gate 		 * Find original page.  We must be bringing it in
22290Sstevel@tonic-gate 		 * from the list in pl[].
22300Sstevel@tonic-gate 		 */
22310Sstevel@tonic-gate 		for (ppp = pl; (opp = *ppp) != NULL; ppp++) {
22320Sstevel@tonic-gate 			if (opp == PAGE_HANDLED)
22330Sstevel@tonic-gate 				continue;
22340Sstevel@tonic-gate 			ASSERT(opp->p_vnode == svd->vp); /* XXX */
22350Sstevel@tonic-gate 			if (opp->p_offset == off)
22360Sstevel@tonic-gate 				break;
22370Sstevel@tonic-gate 		}
22380Sstevel@tonic-gate 		if (opp == NULL) {
22390Sstevel@tonic-gate 			panic("segvn_faultpage not found");
22400Sstevel@tonic-gate 			/*NOTREACHED*/
22410Sstevel@tonic-gate 		}
22420Sstevel@tonic-gate 		*ppp = PAGE_HANDLED;
22430Sstevel@tonic-gate 
22440Sstevel@tonic-gate 	}
22450Sstevel@tonic-gate 
22460Sstevel@tonic-gate 	ASSERT(PAGE_LOCKED(opp));
22470Sstevel@tonic-gate 
22480Sstevel@tonic-gate 	TRACE_3(TR_FAC_VM, TR_SEGVN_FAULT,
22490Sstevel@tonic-gate 		"segvn_fault:pp %p vp %p offset %llx",
22500Sstevel@tonic-gate 		opp, NULL, 0);
22510Sstevel@tonic-gate 
22520Sstevel@tonic-gate 	/*
22530Sstevel@tonic-gate 	 * The fault is treated as a copy-on-write fault if a
22540Sstevel@tonic-gate 	 * write occurs on a private segment and the object
22550Sstevel@tonic-gate 	 * page (i.e., mapping) is write protected.  We assume
22560Sstevel@tonic-gate 	 * that fatal protection checks have already been made.
22570Sstevel@tonic-gate 	 */
22580Sstevel@tonic-gate 
22590Sstevel@tonic-gate 	cow = brkcow && ((vpprot & PROT_WRITE) == 0);
22600Sstevel@tonic-gate 
22610Sstevel@tonic-gate 	/*
22620Sstevel@tonic-gate 	 * If not a copy-on-write case load the translation
22630Sstevel@tonic-gate 	 * and return.
22640Sstevel@tonic-gate 	 */
22650Sstevel@tonic-gate 	if (cow == 0) {
22660Sstevel@tonic-gate 		if (IS_VMODSORT(opp->p_vnode) || enable_mbit_wa) {
22670Sstevel@tonic-gate 			if (rw == S_WRITE)
22680Sstevel@tonic-gate 				hat_setmod(opp);
22690Sstevel@tonic-gate 			else if (rw != S_OTHER && !hat_ismod(opp))
22700Sstevel@tonic-gate 				prot &= ~PROT_WRITE;
22710Sstevel@tonic-gate 		}
22720Sstevel@tonic-gate 
22730Sstevel@tonic-gate 		/*
22740Sstevel@tonic-gate 		 * Handle pages that have been marked for migration
22750Sstevel@tonic-gate 		 */
22760Sstevel@tonic-gate 		if (lgrp_optimizations())
22770Sstevel@tonic-gate 			page_migrate(seg, addr, &opp, 1);
22780Sstevel@tonic-gate 
22790Sstevel@tonic-gate 		hat_memload(hat, addr, opp, prot & vpprot, hat_flag);
22800Sstevel@tonic-gate 
22810Sstevel@tonic-gate 		if (!(hat_flag & HAT_LOAD_LOCK))
22820Sstevel@tonic-gate 			page_unlock(opp);
22830Sstevel@tonic-gate 
22840Sstevel@tonic-gate 		if (anon_lock) {
22850Sstevel@tonic-gate 			anon_array_exit(&cookie);
22860Sstevel@tonic-gate 		}
22870Sstevel@tonic-gate 		return (0);
22880Sstevel@tonic-gate 	}
22890Sstevel@tonic-gate 
22900Sstevel@tonic-gate 	hat_setref(opp);
22910Sstevel@tonic-gate 
22920Sstevel@tonic-gate 	ASSERT(amp != NULL && anon_lock);
22930Sstevel@tonic-gate 
22940Sstevel@tonic-gate 	/*
22950Sstevel@tonic-gate 	 * Steal the page only if it isn't a private page
22960Sstevel@tonic-gate 	 * since stealing a private page is not worth the effort.
22970Sstevel@tonic-gate 	 */
22980Sstevel@tonic-gate 	if ((ap = anon_get_ptr(amp->ahp, anon_index)) == NULL)
22990Sstevel@tonic-gate 		steal = 1;
23000Sstevel@tonic-gate 
23010Sstevel@tonic-gate 	/*
23020Sstevel@tonic-gate 	 * Steal the original page if the following conditions are true:
23030Sstevel@tonic-gate 	 *
230463Saguzovsk 	 * We are low on memory, the page is not private, page is not large,
230563Saguzovsk 	 * not shared, not modified, not `locked' or if we have it `locked'
23060Sstevel@tonic-gate 	 * (i.e., p_cowcnt == 1 and p_lckcnt == 0, which also implies
23070Sstevel@tonic-gate 	 * that the page is not shared) and if it doesn't have any
23080Sstevel@tonic-gate 	 * translations. page_struct_lock isn't needed to look at p_cowcnt
23090Sstevel@tonic-gate 	 * and p_lckcnt because we first get exclusive lock on page.
23100Sstevel@tonic-gate 	 */
23110Sstevel@tonic-gate 	(void) hat_pagesync(opp, HAT_SYNC_DONTZERO | HAT_SYNC_STOPON_MOD);
23120Sstevel@tonic-gate 
231363Saguzovsk 	if (stealcow && freemem < minfree && steal && opp->p_szc == 0 &&
23140Sstevel@tonic-gate 	    page_tryupgrade(opp) && !hat_ismod(opp) &&
23150Sstevel@tonic-gate 	    ((opp->p_lckcnt == 0 && opp->p_cowcnt == 0) ||
23160Sstevel@tonic-gate 	    (opp->p_lckcnt == 0 && opp->p_cowcnt == 1 &&
23170Sstevel@tonic-gate 	    vpage != NULL && VPP_ISPPLOCK(vpage)))) {
23180Sstevel@tonic-gate 		/*
23190Sstevel@tonic-gate 		 * Check if this page has other translations
23200Sstevel@tonic-gate 		 * after unloading our translation.
23210Sstevel@tonic-gate 		 */
23220Sstevel@tonic-gate 		if (hat_page_is_mapped(opp)) {
23230Sstevel@tonic-gate 			hat_unload(seg->s_as->a_hat, addr, PAGESIZE,
23240Sstevel@tonic-gate 				HAT_UNLOAD);
23250Sstevel@tonic-gate 		}
23260Sstevel@tonic-gate 
23270Sstevel@tonic-gate 		/*
23280Sstevel@tonic-gate 		 * hat_unload() might sync back someone else's recent
23290Sstevel@tonic-gate 		 * modification, so check again.
23300Sstevel@tonic-gate 		 */
23310Sstevel@tonic-gate 		if (!hat_ismod(opp) && !hat_page_is_mapped(opp))
23320Sstevel@tonic-gate 			pageflags |= STEAL_PAGE;
23330Sstevel@tonic-gate 	}
23340Sstevel@tonic-gate 
23350Sstevel@tonic-gate 	/*
23360Sstevel@tonic-gate 	 * If we have a vpage pointer, see if it indicates that we have
23370Sstevel@tonic-gate 	 * ``locked'' the page we map -- if so, tell anon_private to
23380Sstevel@tonic-gate 	 * transfer the locking resource to the new page.
23390Sstevel@tonic-gate 	 *
23400Sstevel@tonic-gate 	 * See Statement at the beginning of segvn_lockop regarding
23410Sstevel@tonic-gate 	 * the way lockcnts/cowcnts are handled during COW.
23420Sstevel@tonic-gate 	 *
23430Sstevel@tonic-gate 	 */
23440Sstevel@tonic-gate 	if (vpage != NULL && VPP_ISPPLOCK(vpage))
23450Sstevel@tonic-gate 		pageflags |= LOCK_PAGE;
23460Sstevel@tonic-gate 
23470Sstevel@tonic-gate 	/*
23480Sstevel@tonic-gate 	 * Allocate a private page and perform the copy.
23490Sstevel@tonic-gate 	 * For MAP_NORESERVE reserve swap space now, unless this
23500Sstevel@tonic-gate 	 * is a cow fault on an existing anon page in which case
23510Sstevel@tonic-gate 	 * MAP_NORESERVE will have made advance reservations.
23520Sstevel@tonic-gate 	 */
23530Sstevel@tonic-gate 	if ((svd->flags & MAP_NORESERVE) && (ap == NULL)) {
23540Sstevel@tonic-gate 		if (anon_resv(ptob(1))) {
23550Sstevel@tonic-gate 			svd->swresv += ptob(1);
23560Sstevel@tonic-gate 		} else {
23570Sstevel@tonic-gate 			page_unlock(opp);
23580Sstevel@tonic-gate 			err = ENOMEM;
23590Sstevel@tonic-gate 			goto out;
23600Sstevel@tonic-gate 		}
23610Sstevel@tonic-gate 	}
23620Sstevel@tonic-gate 	oldap = ap;
23630Sstevel@tonic-gate 	pp = anon_private(&ap, seg, addr, prot, opp, pageflags, svd->cred);
23640Sstevel@tonic-gate 	if (pp == NULL) {
23650Sstevel@tonic-gate 		err = ENOMEM;	/* out of swap space */
23660Sstevel@tonic-gate 		goto out;
23670Sstevel@tonic-gate 	}
23680Sstevel@tonic-gate 
23690Sstevel@tonic-gate 	/*
23700Sstevel@tonic-gate 	 * If we copied away from an anonymous page, then
23710Sstevel@tonic-gate 	 * we are one step closer to freeing up an anon slot.
23720Sstevel@tonic-gate 	 *
23730Sstevel@tonic-gate 	 * NOTE:  The original anon slot must be released while
23740Sstevel@tonic-gate 	 * holding the "anon_map" lock.  This is necessary to prevent
23750Sstevel@tonic-gate 	 * other threads from obtaining a pointer to the anon slot
23760Sstevel@tonic-gate 	 * which may be freed if its "refcnt" is 1.
23770Sstevel@tonic-gate 	 */
23780Sstevel@tonic-gate 	if (oldap != NULL)
23790Sstevel@tonic-gate 		anon_decref(oldap);
23800Sstevel@tonic-gate 
23810Sstevel@tonic-gate 	(void) anon_set_ptr(amp->ahp, anon_index, ap, ANON_SLEEP);
23820Sstevel@tonic-gate 
23830Sstevel@tonic-gate 	ASSERT(!IS_VMODSORT(pp->p_vnode));
23840Sstevel@tonic-gate 	if (enable_mbit_wa) {
23850Sstevel@tonic-gate 		if (rw == S_WRITE)
23860Sstevel@tonic-gate 			hat_setmod(pp);
23870Sstevel@tonic-gate 		else if (!hat_ismod(pp))
23880Sstevel@tonic-gate 			prot &= ~PROT_WRITE;
23890Sstevel@tonic-gate 	}
23900Sstevel@tonic-gate 
23910Sstevel@tonic-gate 
23920Sstevel@tonic-gate 	/*
23930Sstevel@tonic-gate 	 * Handle pages that have been marked for migration
23940Sstevel@tonic-gate 	 */
23950Sstevel@tonic-gate 	if (lgrp_optimizations())
23960Sstevel@tonic-gate 		page_migrate(seg, addr, &pp, 1);
23970Sstevel@tonic-gate 	hat_memload(hat, addr, pp, prot, hat_flag);
23980Sstevel@tonic-gate 
23990Sstevel@tonic-gate 	if (!(hat_flag & HAT_LOAD_LOCK))
24000Sstevel@tonic-gate 		page_unlock(pp);
24010Sstevel@tonic-gate 
24020Sstevel@tonic-gate 	ASSERT(anon_lock);
24030Sstevel@tonic-gate 	anon_array_exit(&cookie);
24040Sstevel@tonic-gate 	return (0);
24050Sstevel@tonic-gate out:
24060Sstevel@tonic-gate 	if (anon_lock)
24070Sstevel@tonic-gate 		anon_array_exit(&cookie);
24080Sstevel@tonic-gate 
24090Sstevel@tonic-gate 	if (type == F_SOFTLOCK) {
24100Sstevel@tonic-gate 		mutex_enter(&freemem_lock);
24110Sstevel@tonic-gate 		availrmem++;
24120Sstevel@tonic-gate 		segvn_pages_locked--;
24130Sstevel@tonic-gate 		svd->softlockcnt--;
24140Sstevel@tonic-gate 		mutex_exit(&freemem_lock);
24150Sstevel@tonic-gate 	}
24160Sstevel@tonic-gate 	return (FC_MAKE_ERR(err));
24170Sstevel@tonic-gate }
24180Sstevel@tonic-gate 
24190Sstevel@tonic-gate /*
24200Sstevel@tonic-gate  * relocate a bunch of smaller targ pages into one large repl page. all targ
24210Sstevel@tonic-gate  * pages must be complete pages smaller than replacement pages.
24220Sstevel@tonic-gate  * it's assumed that no page's szc can change since they are all PAGESIZE or
24230Sstevel@tonic-gate  * complete large pages locked SHARED.
24240Sstevel@tonic-gate  */
24250Sstevel@tonic-gate static void
24260Sstevel@tonic-gate segvn_relocate_pages(page_t **targ, page_t *replacement)
24270Sstevel@tonic-gate {
24280Sstevel@tonic-gate 	page_t *pp;
24290Sstevel@tonic-gate 	pgcnt_t repl_npgs, curnpgs;
24300Sstevel@tonic-gate 	pgcnt_t i;
24310Sstevel@tonic-gate 	uint_t repl_szc = replacement->p_szc;
24320Sstevel@tonic-gate 	page_t *first_repl = replacement;
24330Sstevel@tonic-gate 	page_t *repl;
24340Sstevel@tonic-gate 	spgcnt_t npgs;
24350Sstevel@tonic-gate 
24360Sstevel@tonic-gate 	VM_STAT_ADD(segvnvmstats.relocatepages[0]);
24370Sstevel@tonic-gate 
24380Sstevel@tonic-gate 	ASSERT(repl_szc != 0);
24390Sstevel@tonic-gate 	npgs = repl_npgs = page_get_pagecnt(repl_szc);
24400Sstevel@tonic-gate 
24410Sstevel@tonic-gate 	i = 0;
24420Sstevel@tonic-gate 	while (repl_npgs) {
24430Sstevel@tonic-gate 		spgcnt_t nreloc;
24440Sstevel@tonic-gate 		int err;
24450Sstevel@tonic-gate 		ASSERT(replacement != NULL);
24460Sstevel@tonic-gate 		pp = targ[i];
24470Sstevel@tonic-gate 		ASSERT(pp->p_szc < repl_szc);
24480Sstevel@tonic-gate 		ASSERT(PAGE_EXCL(pp));
24490Sstevel@tonic-gate 		ASSERT(!PP_ISFREE(pp));
24500Sstevel@tonic-gate 		curnpgs = page_get_pagecnt(pp->p_szc);
24510Sstevel@tonic-gate 		if (curnpgs == 1) {
24520Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.relocatepages[1]);
24530Sstevel@tonic-gate 			repl = replacement;
24540Sstevel@tonic-gate 			page_sub(&replacement, repl);
24550Sstevel@tonic-gate 			ASSERT(PAGE_EXCL(repl));
24560Sstevel@tonic-gate 			ASSERT(!PP_ISFREE(repl));
24570Sstevel@tonic-gate 			ASSERT(repl->p_szc == repl_szc);
24580Sstevel@tonic-gate 		} else {
24590Sstevel@tonic-gate 			page_t *repl_savepp;
24600Sstevel@tonic-gate 			int j;
24610Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.relocatepages[2]);
24620Sstevel@tonic-gate 			repl_savepp = replacement;
24630Sstevel@tonic-gate 			for (j = 0; j < curnpgs; j++) {
24640Sstevel@tonic-gate 				repl = replacement;
24650Sstevel@tonic-gate 				page_sub(&replacement, repl);
24660Sstevel@tonic-gate 				ASSERT(PAGE_EXCL(repl));
24670Sstevel@tonic-gate 				ASSERT(!PP_ISFREE(repl));
24680Sstevel@tonic-gate 				ASSERT(repl->p_szc == repl_szc);
24690Sstevel@tonic-gate 				ASSERT(page_pptonum(targ[i + j]) ==
24700Sstevel@tonic-gate 				    page_pptonum(targ[i]) + j);
24710Sstevel@tonic-gate 			}
24720Sstevel@tonic-gate 			repl = repl_savepp;
24730Sstevel@tonic-gate 			ASSERT(IS_P2ALIGNED(page_pptonum(repl), curnpgs));
24740Sstevel@tonic-gate 		}
24750Sstevel@tonic-gate 		err = page_relocate(&pp, &repl, 0, 1, &nreloc, NULL);
24760Sstevel@tonic-gate 		if (err || nreloc != curnpgs) {
24770Sstevel@tonic-gate 			panic("segvn_relocate_pages: "
24780Sstevel@tonic-gate 			    "page_relocate failed err=%d curnpgs=%ld "
24790Sstevel@tonic-gate 			    "nreloc=%ld", err, curnpgs, nreloc);
24800Sstevel@tonic-gate 		}
24810Sstevel@tonic-gate 		ASSERT(curnpgs <= repl_npgs);
24820Sstevel@tonic-gate 		repl_npgs -= curnpgs;
24830Sstevel@tonic-gate 		i += curnpgs;
24840Sstevel@tonic-gate 	}
24850Sstevel@tonic-gate 	ASSERT(replacement == NULL);
24860Sstevel@tonic-gate 
24870Sstevel@tonic-gate 	repl = first_repl;
24880Sstevel@tonic-gate 	repl_npgs = npgs;
24890Sstevel@tonic-gate 	for (i = 0; i < repl_npgs; i++) {
24900Sstevel@tonic-gate 		ASSERT(PAGE_EXCL(repl));
24910Sstevel@tonic-gate 		ASSERT(!PP_ISFREE(repl));
24920Sstevel@tonic-gate 		targ[i] = repl;
24930Sstevel@tonic-gate 		page_downgrade(targ[i]);
2494414Skchow 		repl++;
24950Sstevel@tonic-gate 	}
24960Sstevel@tonic-gate }
24970Sstevel@tonic-gate 
24980Sstevel@tonic-gate /*
24990Sstevel@tonic-gate  * Check if all pages in ppa array are complete smaller than szc pages and
25000Sstevel@tonic-gate  * their roots will still be aligned relative to their current size if the
25010Sstevel@tonic-gate  * entire ppa array is relocated into one szc page. If these conditions are
25020Sstevel@tonic-gate  * not met return 0.
25030Sstevel@tonic-gate  *
25040Sstevel@tonic-gate  * If all pages are properly aligned attempt to upgrade their locks
25050Sstevel@tonic-gate  * to exclusive mode. If it fails set *upgrdfail to 1 and return 0.
25060Sstevel@tonic-gate  * upgrdfail was set to 0 by caller.
25070Sstevel@tonic-gate  *
25080Sstevel@tonic-gate  * Return 1 if all pages are aligned and locked exclusively.
25090Sstevel@tonic-gate  *
25100Sstevel@tonic-gate  * If all pages in ppa array happen to be physically contiguous to make one
25110Sstevel@tonic-gate  * szc page and all exclusive locks are successfully obtained promote the page
25120Sstevel@tonic-gate  * size to szc and set *pszc to szc. Return 1 with pages locked shared.
25130Sstevel@tonic-gate  */
25140Sstevel@tonic-gate static int
25150Sstevel@tonic-gate segvn_full_szcpages(page_t **ppa, uint_t szc, int *upgrdfail, uint_t *pszc)
25160Sstevel@tonic-gate {
25170Sstevel@tonic-gate 	page_t *pp;
25180Sstevel@tonic-gate 	pfn_t pfn;
25190Sstevel@tonic-gate 	pgcnt_t totnpgs = page_get_pagecnt(szc);
25200Sstevel@tonic-gate 	pfn_t first_pfn;
25210Sstevel@tonic-gate 	int contig = 1;
25220Sstevel@tonic-gate 	pgcnt_t i;
25230Sstevel@tonic-gate 	pgcnt_t j;
25240Sstevel@tonic-gate 	uint_t curszc;
25250Sstevel@tonic-gate 	pgcnt_t curnpgs;
25260Sstevel@tonic-gate 	int root = 0;
25270Sstevel@tonic-gate 
25280Sstevel@tonic-gate 	ASSERT(szc > 0);
25290Sstevel@tonic-gate 
25300Sstevel@tonic-gate 	VM_STAT_ADD(segvnvmstats.fullszcpages[0]);
25310Sstevel@tonic-gate 
25320Sstevel@tonic-gate 	for (i = 0; i < totnpgs; i++) {
25330Sstevel@tonic-gate 		pp = ppa[i];
25340Sstevel@tonic-gate 		ASSERT(PAGE_SHARED(pp));
25350Sstevel@tonic-gate 		ASSERT(!PP_ISFREE(pp));
25360Sstevel@tonic-gate 		pfn = page_pptonum(pp);
25370Sstevel@tonic-gate 		if (i == 0) {
25380Sstevel@tonic-gate 			if (!IS_P2ALIGNED(pfn, totnpgs)) {
25390Sstevel@tonic-gate 				contig = 0;
25400Sstevel@tonic-gate 			} else {
25410Sstevel@tonic-gate 				first_pfn = pfn;
25420Sstevel@tonic-gate 			}
25430Sstevel@tonic-gate 		} else if (contig && pfn != first_pfn + i) {
25440Sstevel@tonic-gate 			contig = 0;
25450Sstevel@tonic-gate 		}
25460Sstevel@tonic-gate 		if (pp->p_szc == 0) {
25470Sstevel@tonic-gate 			if (root) {
25480Sstevel@tonic-gate 				VM_STAT_ADD(segvnvmstats.fullszcpages[1]);
25490Sstevel@tonic-gate 				return (0);
25500Sstevel@tonic-gate 			}
25510Sstevel@tonic-gate 		} else if (!root) {
25520Sstevel@tonic-gate 			if ((curszc = pp->p_szc) >= szc) {
25530Sstevel@tonic-gate 				VM_STAT_ADD(segvnvmstats.fullszcpages[2]);
25540Sstevel@tonic-gate 				return (0);
25550Sstevel@tonic-gate 			}
25560Sstevel@tonic-gate 			if (curszc == 0) {
25570Sstevel@tonic-gate 				/*
25580Sstevel@tonic-gate 				 * p_szc changed means we don't have all pages
25590Sstevel@tonic-gate 				 * locked. return failure.
25600Sstevel@tonic-gate 				 */
25610Sstevel@tonic-gate 				VM_STAT_ADD(segvnvmstats.fullszcpages[3]);
25620Sstevel@tonic-gate 				return (0);
25630Sstevel@tonic-gate 			}
25640Sstevel@tonic-gate 			curnpgs = page_get_pagecnt(curszc);
25650Sstevel@tonic-gate 			if (!IS_P2ALIGNED(pfn, curnpgs) ||
25660Sstevel@tonic-gate 			    !IS_P2ALIGNED(i, curnpgs)) {
25670Sstevel@tonic-gate 				VM_STAT_ADD(segvnvmstats.fullszcpages[4]);
25680Sstevel@tonic-gate 				return (0);
25690Sstevel@tonic-gate 			}
25700Sstevel@tonic-gate 			root = 1;
25710Sstevel@tonic-gate 		} else {
25720Sstevel@tonic-gate 			ASSERT(i > 0);
25730Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fullszcpages[5]);
25740Sstevel@tonic-gate 			if (pp->p_szc != curszc) {
25750Sstevel@tonic-gate 				VM_STAT_ADD(segvnvmstats.fullszcpages[6]);
25760Sstevel@tonic-gate 				return (0);
25770Sstevel@tonic-gate 			}
25780Sstevel@tonic-gate 			if (pfn - 1 != page_pptonum(ppa[i - 1])) {
25790Sstevel@tonic-gate 				panic("segvn_full_szcpages: "
25800Sstevel@tonic-gate 				    "large page not physically contiguous");
25810Sstevel@tonic-gate 			}
25820Sstevel@tonic-gate 			if (P2PHASE(pfn, curnpgs) == curnpgs - 1) {
25830Sstevel@tonic-gate 				root = 0;
25840Sstevel@tonic-gate 			}
25850Sstevel@tonic-gate 		}
25860Sstevel@tonic-gate 	}
25870Sstevel@tonic-gate 
25880Sstevel@tonic-gate 	for (i = 0; i < totnpgs; i++) {
25890Sstevel@tonic-gate 		ASSERT(ppa[i]->p_szc < szc);
25900Sstevel@tonic-gate 		if (!page_tryupgrade(ppa[i])) {
25910Sstevel@tonic-gate 			for (j = 0; j < i; j++) {
25920Sstevel@tonic-gate 				page_downgrade(ppa[j]);
25930Sstevel@tonic-gate 			}
25940Sstevel@tonic-gate 			*pszc = ppa[i]->p_szc;
25950Sstevel@tonic-gate 			*upgrdfail = 1;
25960Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fullszcpages[7]);
25970Sstevel@tonic-gate 			return (0);
25980Sstevel@tonic-gate 		}
25990Sstevel@tonic-gate 	}
26000Sstevel@tonic-gate 
26010Sstevel@tonic-gate 	/*
26020Sstevel@tonic-gate 	 * When a page is put a free cachelist its szc is set to 0.  if file
26030Sstevel@tonic-gate 	 * system reclaimed pages from cachelist targ pages will be physically
26040Sstevel@tonic-gate 	 * contiguous with 0 p_szc.  in this case just upgrade szc of targ
26050Sstevel@tonic-gate 	 * pages without any relocations.
26060Sstevel@tonic-gate 	 * To avoid any hat issues with previous small mappings
26070Sstevel@tonic-gate 	 * hat_pageunload() the target pages first.
26080Sstevel@tonic-gate 	 */
26090Sstevel@tonic-gate 	if (contig) {
26100Sstevel@tonic-gate 		VM_STAT_ADD(segvnvmstats.fullszcpages[8]);
26110Sstevel@tonic-gate 		for (i = 0; i < totnpgs; i++) {
26120Sstevel@tonic-gate 			(void) hat_pageunload(ppa[i], HAT_FORCE_PGUNLOAD);
26130Sstevel@tonic-gate 		}
26140Sstevel@tonic-gate 		for (i = 0; i < totnpgs; i++) {
26150Sstevel@tonic-gate 			ppa[i]->p_szc = szc;
26160Sstevel@tonic-gate 		}
26170Sstevel@tonic-gate 		for (i = 0; i < totnpgs; i++) {
26180Sstevel@tonic-gate 			ASSERT(PAGE_EXCL(ppa[i]));
26190Sstevel@tonic-gate 			page_downgrade(ppa[i]);
26200Sstevel@tonic-gate 		}
26210Sstevel@tonic-gate 		if (pszc != NULL) {
26220Sstevel@tonic-gate 			*pszc = szc;
26230Sstevel@tonic-gate 		}
26240Sstevel@tonic-gate 	}
26250Sstevel@tonic-gate 	VM_STAT_ADD(segvnvmstats.fullszcpages[9]);
26260Sstevel@tonic-gate 	return (1);
26270Sstevel@tonic-gate }
26280Sstevel@tonic-gate 
26290Sstevel@tonic-gate /*
26300Sstevel@tonic-gate  * Create physically contiguous pages for [vp, off] - [vp, off +
26310Sstevel@tonic-gate  * page_size(szc)) range and for private segment return them in ppa array.
26320Sstevel@tonic-gate  * Pages are created either via IO or relocations.
26330Sstevel@tonic-gate  *
26340Sstevel@tonic-gate  * Return 1 on sucess and 0 on failure.
26350Sstevel@tonic-gate  *
26360Sstevel@tonic-gate  * If physically contiguos pages already exist for this range return 1 without
26370Sstevel@tonic-gate  * filling ppa array. Caller initializes ppa[0] as NULL to detect that ppa
26380Sstevel@tonic-gate  * array wasn't filled. In this case caller fills ppa array via VOP_GETPAGE().
26390Sstevel@tonic-gate  */
26400Sstevel@tonic-gate 
26410Sstevel@tonic-gate static int
26420Sstevel@tonic-gate segvn_fill_vp_pages(struct segvn_data *svd, vnode_t *vp, u_offset_t off,
26430Sstevel@tonic-gate     uint_t szc, page_t **ppa, page_t **ppplist, uint_t *ret_pszc,
26440Sstevel@tonic-gate     int *downsize)
26450Sstevel@tonic-gate 
26460Sstevel@tonic-gate {
26470Sstevel@tonic-gate 	page_t *pplist = *ppplist;
26480Sstevel@tonic-gate 	size_t pgsz = page_get_pagesize(szc);
26490Sstevel@tonic-gate 	pgcnt_t pages = btop(pgsz);
26500Sstevel@tonic-gate 	ulong_t start_off = off;
26510Sstevel@tonic-gate 	u_offset_t eoff = off + pgsz;
26520Sstevel@tonic-gate 	spgcnt_t nreloc;
26530Sstevel@tonic-gate 	u_offset_t io_off = off;
26540Sstevel@tonic-gate 	size_t io_len;
26550Sstevel@tonic-gate 	page_t *io_pplist = NULL;
26560Sstevel@tonic-gate 	page_t *done_pplist = NULL;
26570Sstevel@tonic-gate 	pgcnt_t pgidx = 0;
26580Sstevel@tonic-gate 	page_t *pp;
26590Sstevel@tonic-gate 	page_t *newpp;
26600Sstevel@tonic-gate 	page_t *targpp;
26610Sstevel@tonic-gate 	int io_err = 0;
26620Sstevel@tonic-gate 	int i;
26630Sstevel@tonic-gate 	pfn_t pfn;
26640Sstevel@tonic-gate 	ulong_t ppages;
26650Sstevel@tonic-gate 	page_t *targ_pplist = NULL;
26660Sstevel@tonic-gate 	page_t *repl_pplist = NULL;
26670Sstevel@tonic-gate 	page_t *tmp_pplist;
26680Sstevel@tonic-gate 	int nios = 0;
26690Sstevel@tonic-gate 	uint_t pszc;
26700Sstevel@tonic-gate 	struct vattr va;
26710Sstevel@tonic-gate 
26720Sstevel@tonic-gate 	VM_STAT_ADD(segvnvmstats.fill_vp_pages[0]);
26730Sstevel@tonic-gate 
26740Sstevel@tonic-gate 	ASSERT(szc != 0);
26750Sstevel@tonic-gate 	ASSERT(pplist->p_szc == szc);
26760Sstevel@tonic-gate 
26770Sstevel@tonic-gate 	/*
26780Sstevel@tonic-gate 	 * downsize will be set to 1 only if we fail to lock pages. this will
26790Sstevel@tonic-gate 	 * allow subsequent faults to try to relocate the page again. If we
26800Sstevel@tonic-gate 	 * fail due to misalignment don't downsize and let the caller map the
26810Sstevel@tonic-gate 	 * whole region with small mappings to avoid more faults into the area
26820Sstevel@tonic-gate 	 * where we can't get large pages anyway.
26830Sstevel@tonic-gate 	 */
26840Sstevel@tonic-gate 	*downsize = 0;
26850Sstevel@tonic-gate 
26860Sstevel@tonic-gate 	while (off < eoff) {
26870Sstevel@tonic-gate 		newpp = pplist;
26880Sstevel@tonic-gate 		ASSERT(newpp != NULL);
26890Sstevel@tonic-gate 		ASSERT(PAGE_EXCL(newpp));
26900Sstevel@tonic-gate 		ASSERT(!PP_ISFREE(newpp));
26910Sstevel@tonic-gate 		/*
26920Sstevel@tonic-gate 		 * we pass NULL for nrelocp to page_lookup_create()
26930Sstevel@tonic-gate 		 * so that it doesn't relocate. We relocate here
26940Sstevel@tonic-gate 		 * later only after we make sure we can lock all
26950Sstevel@tonic-gate 		 * pages in the range we handle and they are all
26960Sstevel@tonic-gate 		 * aligned.
26970Sstevel@tonic-gate 		 */
26980Sstevel@tonic-gate 		pp = page_lookup_create(vp, off, SE_SHARED, newpp, NULL, 0);
26990Sstevel@tonic-gate 		ASSERT(pp != NULL);
27000Sstevel@tonic-gate 		ASSERT(!PP_ISFREE(pp));
27010Sstevel@tonic-gate 		ASSERT(pp->p_vnode == vp);
27020Sstevel@tonic-gate 		ASSERT(pp->p_offset == off);
27030Sstevel@tonic-gate 		if (pp == newpp) {
27040Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fill_vp_pages[1]);
27050Sstevel@tonic-gate 			page_sub(&pplist, pp);
27060Sstevel@tonic-gate 			ASSERT(PAGE_EXCL(pp));
27070Sstevel@tonic-gate 			ASSERT(page_iolock_assert(pp));
27080Sstevel@tonic-gate 			page_list_concat(&io_pplist, &pp);
27090Sstevel@tonic-gate 			off += PAGESIZE;
27100Sstevel@tonic-gate 			continue;
27110Sstevel@tonic-gate 		}
27120Sstevel@tonic-gate 		VM_STAT_ADD(segvnvmstats.fill_vp_pages[2]);
27130Sstevel@tonic-gate 		pfn = page_pptonum(pp);
27140Sstevel@tonic-gate 		pszc = pp->p_szc;
27150Sstevel@tonic-gate 		if (pszc >= szc && targ_pplist == NULL && io_pplist == NULL &&
27160Sstevel@tonic-gate 		    IS_P2ALIGNED(pfn, pages)) {
27170Sstevel@tonic-gate 			ASSERT(repl_pplist == NULL);
27180Sstevel@tonic-gate 			ASSERT(done_pplist == NULL);
27190Sstevel@tonic-gate 			ASSERT(pplist == *ppplist);
27200Sstevel@tonic-gate 			page_unlock(pp);
27210Sstevel@tonic-gate 			page_free_replacement_page(pplist);
27220Sstevel@tonic-gate 			page_create_putback(pages);
27230Sstevel@tonic-gate 			*ppplist = NULL;
27240Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fill_vp_pages[3]);
27250Sstevel@tonic-gate 			return (1);
27260Sstevel@tonic-gate 		}
27270Sstevel@tonic-gate 		if (pszc >= szc) {
27280Sstevel@tonic-gate 			page_unlock(pp);
27290Sstevel@tonic-gate 			segvn_faultvnmpss_align_err1++;
27300Sstevel@tonic-gate 			goto out;
27310Sstevel@tonic-gate 		}
27320Sstevel@tonic-gate 		ppages = page_get_pagecnt(pszc);
27330Sstevel@tonic-gate 		if (!IS_P2ALIGNED(pfn, ppages)) {
27340Sstevel@tonic-gate 			ASSERT(pszc > 0);
27350Sstevel@tonic-gate 			/*
27360Sstevel@tonic-gate 			 * sizing down to pszc won't help.
27370Sstevel@tonic-gate 			 */
27380Sstevel@tonic-gate 			page_unlock(pp);
27390Sstevel@tonic-gate 			segvn_faultvnmpss_align_err2++;
27400Sstevel@tonic-gate 			goto out;
27410Sstevel@tonic-gate 		}
27420Sstevel@tonic-gate 		pfn = page_pptonum(newpp);
27430Sstevel@tonic-gate 		if (!IS_P2ALIGNED(pfn, ppages)) {
27440Sstevel@tonic-gate 			ASSERT(pszc > 0);
27450Sstevel@tonic-gate 			/*
27460Sstevel@tonic-gate 			 * sizing down to pszc won't help.
27470Sstevel@tonic-gate 			 */
27480Sstevel@tonic-gate 			page_unlock(pp);
27490Sstevel@tonic-gate 			segvn_faultvnmpss_align_err3++;
27500Sstevel@tonic-gate 			goto out;
27510Sstevel@tonic-gate 		}
27520Sstevel@tonic-gate 		if (!PAGE_EXCL(pp)) {
27530Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fill_vp_pages[4]);
27540Sstevel@tonic-gate 			page_unlock(pp);
27550Sstevel@tonic-gate 			*downsize = 1;
27560Sstevel@tonic-gate 			*ret_pszc = pp->p_szc;
27570Sstevel@tonic-gate 			goto out;
27580Sstevel@tonic-gate 		}
27590Sstevel@tonic-gate 		targpp = pp;
27600Sstevel@tonic-gate 		if (io_pplist != NULL) {
27610Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fill_vp_pages[5]);
27620Sstevel@tonic-gate 			io_len = off - io_off;
27630Sstevel@tonic-gate 			/*
27640Sstevel@tonic-gate 			 * Some file systems like NFS don't check EOF
27650Sstevel@tonic-gate 			 * conditions in VOP_PAGEIO(). Check it here
27660Sstevel@tonic-gate 			 * now that pages are locked SE_EXCL. Any file
27670Sstevel@tonic-gate 			 * truncation will wait until the pages are
27680Sstevel@tonic-gate 			 * unlocked so no need to worry that file will
27690Sstevel@tonic-gate 			 * be truncated after we check its size here.
27700Sstevel@tonic-gate 			 * XXX fix NFS to remove this check.
27710Sstevel@tonic-gate 			 */
27720Sstevel@tonic-gate 			va.va_mask = AT_SIZE;
27730Sstevel@tonic-gate 			if (VOP_GETATTR(vp, &va, ATTR_HINT, svd->cred) != 0) {
27740Sstevel@tonic-gate 				VM_STAT_ADD(segvnvmstats.fill_vp_pages[6]);
27750Sstevel@tonic-gate 				page_unlock(targpp);
27760Sstevel@tonic-gate 				goto out;
27770Sstevel@tonic-gate 			}
27780Sstevel@tonic-gate 			if (btopr(va.va_size) < btopr(io_off + io_len)) {
27790Sstevel@tonic-gate 				VM_STAT_ADD(segvnvmstats.fill_vp_pages[7]);
27800Sstevel@tonic-gate 				*downsize = 1;
27810Sstevel@tonic-gate 				*ret_pszc = 0;
27820Sstevel@tonic-gate 				page_unlock(targpp);
27830Sstevel@tonic-gate 				goto out;
27840Sstevel@tonic-gate 			}
27850Sstevel@tonic-gate 			io_err = VOP_PAGEIO(vp, io_pplist, io_off, io_len,
27860Sstevel@tonic-gate 				B_READ, svd->cred);
27870Sstevel@tonic-gate 			if (io_err) {
27880Sstevel@tonic-gate 				VM_STAT_ADD(segvnvmstats.fill_vp_pages[8]);
27890Sstevel@tonic-gate 				page_unlock(targpp);
27900Sstevel@tonic-gate 				if (io_err == EDEADLK) {
27910Sstevel@tonic-gate 					segvn_vmpss_pageio_deadlk_err++;
27920Sstevel@tonic-gate 				}
27930Sstevel@tonic-gate 				goto out;
27940Sstevel@tonic-gate 			}
27950Sstevel@tonic-gate 			nios++;
27960Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fill_vp_pages[9]);
27970Sstevel@tonic-gate 			while (io_pplist != NULL) {
27980Sstevel@tonic-gate 				pp = io_pplist;
27990Sstevel@tonic-gate 				page_sub(&io_pplist, pp);
28000Sstevel@tonic-gate 				ASSERT(page_iolock_assert(pp));
28010Sstevel@tonic-gate 				page_io_unlock(pp);
28020Sstevel@tonic-gate 				pgidx = (pp->p_offset - start_off) >>
28030Sstevel@tonic-gate 				    PAGESHIFT;
28040Sstevel@tonic-gate 				ASSERT(pgidx < pages);
28050Sstevel@tonic-gate 				ppa[pgidx] = pp;
28060Sstevel@tonic-gate 				page_list_concat(&done_pplist, &pp);
28070Sstevel@tonic-gate 			}
28080Sstevel@tonic-gate 		}
28090Sstevel@tonic-gate 		pp = targpp;
28100Sstevel@tonic-gate 		ASSERT(PAGE_EXCL(pp));
28110Sstevel@tonic-gate 		ASSERT(pp->p_szc <= pszc);
28120Sstevel@tonic-gate 		if (pszc != 0 && !group_page_trylock(pp, SE_EXCL)) {
28130Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fill_vp_pages[10]);
28140Sstevel@tonic-gate 			page_unlock(pp);
28150Sstevel@tonic-gate 			*downsize = 1;
28160Sstevel@tonic-gate 			*ret_pszc = pp->p_szc;
28170Sstevel@tonic-gate 			goto out;
28180Sstevel@tonic-gate 		}
28190Sstevel@tonic-gate 		VM_STAT_ADD(segvnvmstats.fill_vp_pages[11]);
28200Sstevel@tonic-gate 		/*
28210Sstevel@tonic-gate 		 * page szc chould have changed before the entire group was
28220Sstevel@tonic-gate 		 * locked. reread page szc.
28230Sstevel@tonic-gate 		 */
28240Sstevel@tonic-gate 		pszc = pp->p_szc;
28250Sstevel@tonic-gate 		ppages = page_get_pagecnt(pszc);
28260Sstevel@tonic-gate 
28270Sstevel@tonic-gate 		/* link just the roots */
28280Sstevel@tonic-gate 		page_list_concat(&targ_pplist, &pp);
28290Sstevel@tonic-gate 		page_sub(&pplist, newpp);
28300Sstevel@tonic-gate 		page_list_concat(&repl_pplist, &newpp);
28310Sstevel@tonic-gate 		off += PAGESIZE;
28320Sstevel@tonic-gate 		while (--ppages != 0) {
28330Sstevel@tonic-gate 			newpp = pplist;
28340Sstevel@tonic-gate 			page_sub(&pplist, newpp);
28350Sstevel@tonic-gate 			off += PAGESIZE;
28360Sstevel@tonic-gate 		}
28370Sstevel@tonic-gate 		io_off = off;
28380Sstevel@tonic-gate 	}
28390Sstevel@tonic-gate 	if (io_pplist != NULL) {
28400Sstevel@tonic-gate 		VM_STAT_ADD(segvnvmstats.fill_vp_pages[12]);
28410Sstevel@tonic-gate 		io_len = eoff - io_off;
28420Sstevel@tonic-gate 		va.va_mask = AT_SIZE;
28430Sstevel@tonic-gate 		if (VOP_GETATTR(vp, &va, ATTR_HINT, svd->cred) != 0) {
28440Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fill_vp_pages[13]);
28450Sstevel@tonic-gate 			goto out;
28460Sstevel@tonic-gate 		}
28470Sstevel@tonic-gate 		if (btopr(va.va_size) < btopr(io_off + io_len)) {
28480Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fill_vp_pages[14]);
28490Sstevel@tonic-gate 			*downsize = 1;
28500Sstevel@tonic-gate 			*ret_pszc = 0;
28510Sstevel@tonic-gate 			goto out;
28520Sstevel@tonic-gate 		}
28530Sstevel@tonic-gate 		io_err = VOP_PAGEIO(vp, io_pplist, io_off, io_len,
28540Sstevel@tonic-gate 		    B_READ, svd->cred);
28550Sstevel@tonic-gate 		if (io_err) {
28560Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fill_vp_pages[15]);
28570Sstevel@tonic-gate 			if (io_err == EDEADLK) {
28580Sstevel@tonic-gate 				segvn_vmpss_pageio_deadlk_err++;
28590Sstevel@tonic-gate 			}
28600Sstevel@tonic-gate 			goto out;
28610Sstevel@tonic-gate 		}
28620Sstevel@tonic-gate 		nios++;
28630Sstevel@tonic-gate 		while (io_pplist != NULL) {
28640Sstevel@tonic-gate 			pp = io_pplist;
28650Sstevel@tonic-gate 			page_sub(&io_pplist, pp);
28660Sstevel@tonic-gate 			ASSERT(page_iolock_assert(pp));
28670Sstevel@tonic-gate 			page_io_unlock(pp);
28680Sstevel@tonic-gate 			pgidx = (pp->p_offset - start_off) >> PAGESHIFT;
28690Sstevel@tonic-gate 			ASSERT(pgidx < pages);
28700Sstevel@tonic-gate 			ppa[pgidx] = pp;
28710Sstevel@tonic-gate 		}
28720Sstevel@tonic-gate 	}
28730Sstevel@tonic-gate 	/*
28740Sstevel@tonic-gate 	 * we're now bound to succeed or panic.
28750Sstevel@tonic-gate 	 * remove pages from done_pplist. it's not needed anymore.
28760Sstevel@tonic-gate 	 */
28770Sstevel@tonic-gate 	while (done_pplist != NULL) {
28780Sstevel@tonic-gate 		pp = done_pplist;
28790Sstevel@tonic-gate 		page_sub(&done_pplist, pp);
28800Sstevel@tonic-gate 	}
28810Sstevel@tonic-gate 	VM_STAT_ADD(segvnvmstats.fill_vp_pages[16]);
28820Sstevel@tonic-gate 	ASSERT(pplist == NULL);
28830Sstevel@tonic-gate 	*ppplist = NULL;
28840Sstevel@tonic-gate 	while (targ_pplist != NULL) {
28850Sstevel@tonic-gate 		int ret;
28860Sstevel@tonic-gate 		VM_STAT_ADD(segvnvmstats.fill_vp_pages[17]);
28870Sstevel@tonic-gate 		ASSERT(repl_pplist);
28880Sstevel@tonic-gate 		pp = targ_pplist;
28890Sstevel@tonic-gate 		page_sub(&targ_pplist, pp);
28900Sstevel@tonic-gate 		pgidx = (pp->p_offset - start_off) >> PAGESHIFT;
28910Sstevel@tonic-gate 		newpp = repl_pplist;
28920Sstevel@tonic-gate 		page_sub(&repl_pplist, newpp);
28930Sstevel@tonic-gate #ifdef DEBUG
28940Sstevel@tonic-gate 		pfn = page_pptonum(pp);
28950Sstevel@tonic-gate 		pszc = pp->p_szc;
28960Sstevel@tonic-gate 		ppages = page_get_pagecnt(pszc);
28970Sstevel@tonic-gate 		ASSERT(IS_P2ALIGNED(pfn, ppages));
28980Sstevel@tonic-gate 		pfn = page_pptonum(newpp);
28990Sstevel@tonic-gate 		ASSERT(IS_P2ALIGNED(pfn, ppages));
29000Sstevel@tonic-gate 		ASSERT(P2PHASE(pfn, pages) == pgidx);
29010Sstevel@tonic-gate #endif
29020Sstevel@tonic-gate 		nreloc = 0;
29030Sstevel@tonic-gate 		ret = page_relocate(&pp, &newpp, 0, 1, &nreloc, NULL);
29040Sstevel@tonic-gate 		if (ret != 0 || nreloc == 0) {
29050Sstevel@tonic-gate 			panic("segvn_fill_vp_pages: "
29060Sstevel@tonic-gate 			    "page_relocate failed");
29070Sstevel@tonic-gate 		}
29080Sstevel@tonic-gate 		pp = newpp;
29090Sstevel@tonic-gate 		while (nreloc-- != 0) {
29100Sstevel@tonic-gate 			ASSERT(PAGE_EXCL(pp));
29110Sstevel@tonic-gate 			ASSERT(pp->p_vnode == vp);
29120Sstevel@tonic-gate 			ASSERT(pgidx ==
29130Sstevel@tonic-gate 			    ((pp->p_offset - start_off) >> PAGESHIFT));
29140Sstevel@tonic-gate 			ppa[pgidx++] = pp;
2915414Skchow 			pp++;
29160Sstevel@tonic-gate 		}
29170Sstevel@tonic-gate 	}
29180Sstevel@tonic-gate 
29190Sstevel@tonic-gate 	if (svd->type == MAP_PRIVATE) {
29200Sstevel@tonic-gate 		VM_STAT_ADD(segvnvmstats.fill_vp_pages[18]);
29210Sstevel@tonic-gate 		for (i = 0; i < pages; i++) {
29220Sstevel@tonic-gate 			ASSERT(ppa[i] != NULL);
29230Sstevel@tonic-gate 			ASSERT(PAGE_EXCL(ppa[i]));
29240Sstevel@tonic-gate 			ASSERT(ppa[i]->p_vnode == vp);
29250Sstevel@tonic-gate 			ASSERT(ppa[i]->p_offset ==
29260Sstevel@tonic-gate 			    start_off + (i << PAGESHIFT));
29270Sstevel@tonic-gate 			page_downgrade(ppa[i]);
29280Sstevel@tonic-gate 		}
29290Sstevel@tonic-gate 		ppa[pages] = NULL;
29300Sstevel@tonic-gate 	} else {
29310Sstevel@tonic-gate 		VM_STAT_ADD(segvnvmstats.fill_vp_pages[19]);
29320Sstevel@tonic-gate 		/*
29330Sstevel@tonic-gate 		 * the caller will still call VOP_GETPAGE() for shared segments
29340Sstevel@tonic-gate 		 * to check FS write permissions. For private segments we map
29350Sstevel@tonic-gate 		 * file read only anyway.  so no VOP_GETPAGE is needed.
29360Sstevel@tonic-gate 		 */
29370Sstevel@tonic-gate 		for (i = 0; i < pages; i++) {
29380Sstevel@tonic-gate 			ASSERT(ppa[i] != NULL);
29390Sstevel@tonic-gate 			ASSERT(PAGE_EXCL(ppa[i]));
29400Sstevel@tonic-gate 			ASSERT(ppa[i]->p_vnode == vp);
29410Sstevel@tonic-gate 			ASSERT(ppa[i]->p_offset ==
29420Sstevel@tonic-gate 			    start_off + (i << PAGESHIFT));
29430Sstevel@tonic-gate 			page_unlock(ppa[i]);
29440Sstevel@tonic-gate 		}
29450Sstevel@tonic-gate 		ppa[0] = NULL;
29460Sstevel@tonic-gate 	}
29470Sstevel@tonic-gate 
29480Sstevel@tonic-gate 	return (1);
29490Sstevel@tonic-gate out:
29500Sstevel@tonic-gate 	/*
29510Sstevel@tonic-gate 	 * Do the cleanup. Unlock target pages we didn't relocate. They are
29520Sstevel@tonic-gate 	 * linked on targ_pplist by root pages. reassemble unused replacement
29530Sstevel@tonic-gate 	 * and io pages back to pplist.
29540Sstevel@tonic-gate 	 */
29550Sstevel@tonic-gate 	if (io_pplist != NULL) {
29560Sstevel@tonic-gate 		VM_STAT_ADD(segvnvmstats.fill_vp_pages[20]);
29570Sstevel@tonic-gate 		pp = io_pplist;
29580Sstevel@tonic-gate 		do {
29590Sstevel@tonic-gate 			ASSERT(pp->p_vnode == vp);
29600Sstevel@tonic-gate 			ASSERT(pp->p_offset == io_off);
29610Sstevel@tonic-gate 			ASSERT(page_iolock_assert(pp));
29620Sstevel@tonic-gate 			page_io_unlock(pp);
29630Sstevel@tonic-gate 			page_hashout(pp, NULL);
29640Sstevel@tonic-gate 			io_off += PAGESIZE;
29650Sstevel@tonic-gate 		} while ((pp = pp->p_next) != io_pplist);
29660Sstevel@tonic-gate 		page_list_concat(&io_pplist, &pplist);
29670Sstevel@tonic-gate 		pplist = io_pplist;
29680Sstevel@tonic-gate 	}
29690Sstevel@tonic-gate 	tmp_pplist = NULL;
29700Sstevel@tonic-gate 	while (targ_pplist != NULL) {
29710Sstevel@tonic-gate 		VM_STAT_ADD(segvnvmstats.fill_vp_pages[21]);
29720Sstevel@tonic-gate 		pp = targ_pplist;
29730Sstevel@tonic-gate 		ASSERT(PAGE_EXCL(pp));
29740Sstevel@tonic-gate 		page_sub(&targ_pplist, pp);
29750Sstevel@tonic-gate 
29760Sstevel@tonic-gate 		pszc = pp->p_szc;
29770Sstevel@tonic-gate 		ppages = page_get_pagecnt(pszc);
29780Sstevel@tonic-gate 		ASSERT(IS_P2ALIGNED(page_pptonum(pp), ppages));
29790Sstevel@tonic-gate 
29800Sstevel@tonic-gate 		if (pszc != 0) {
29810Sstevel@tonic-gate 			group_page_unlock(pp);
29820Sstevel@tonic-gate 		}
29830Sstevel@tonic-gate 		page_unlock(pp);
29840Sstevel@tonic-gate 
29850Sstevel@tonic-gate 		pp = repl_pplist;
29860Sstevel@tonic-gate 		ASSERT(pp != NULL);
29870Sstevel@tonic-gate 		ASSERT(PAGE_EXCL(pp));
29880Sstevel@tonic-gate 		ASSERT(pp->p_szc == szc);
29890Sstevel@tonic-gate 		page_sub(&repl_pplist, pp);
29900Sstevel@tonic-gate 
29910Sstevel@tonic-gate 		ASSERT(IS_P2ALIGNED(page_pptonum(pp), ppages));
29920Sstevel@tonic-gate 
29930Sstevel@tonic-gate 		/* relink replacement page */
29940Sstevel@tonic-gate 		page_list_concat(&tmp_pplist, &pp);
29950Sstevel@tonic-gate 		while (--ppages != 0) {
29960Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fill_vp_pages[22]);
2997414Skchow 			pp++;
29980Sstevel@tonic-gate 			ASSERT(PAGE_EXCL(pp));
29990Sstevel@tonic-gate 			ASSERT(pp->p_szc == szc);
30000Sstevel@tonic-gate 			page_list_concat(&tmp_pplist, &pp);
30010Sstevel@tonic-gate 		}
30020Sstevel@tonic-gate 	}
30030Sstevel@tonic-gate 	if (tmp_pplist != NULL) {
30040Sstevel@tonic-gate 		VM_STAT_ADD(segvnvmstats.fill_vp_pages[23]);
30050Sstevel@tonic-gate 		page_list_concat(&tmp_pplist, &pplist);
30060Sstevel@tonic-gate 		pplist = tmp_pplist;
30070Sstevel@tonic-gate 	}
30080Sstevel@tonic-gate 	/*
30090Sstevel@tonic-gate 	 * at this point all pages are either on done_pplist or
30100Sstevel@tonic-gate 	 * pplist. They can't be all on done_pplist otherwise
30110Sstevel@tonic-gate 	 * we'd've been done.
30120Sstevel@tonic-gate 	 */
30130Sstevel@tonic-gate 	ASSERT(pplist != NULL);
30140Sstevel@tonic-gate 	if (nios != 0) {
30150Sstevel@tonic-gate 		VM_STAT_ADD(segvnvmstats.fill_vp_pages[24]);
30160Sstevel@tonic-gate 		pp = pplist;
30170Sstevel@tonic-gate 		do {
30180Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fill_vp_pages[25]);
30190Sstevel@tonic-gate 			ASSERT(pp->p_szc == szc);
30200Sstevel@tonic-gate 			ASSERT(PAGE_EXCL(pp));
30210Sstevel@tonic-gate 			ASSERT(pp->p_vnode != vp);
30220Sstevel@tonic-gate 			pp->p_szc = 0;
30230Sstevel@tonic-gate 		} while ((pp = pp->p_next) != pplist);
30240Sstevel@tonic-gate 
30250Sstevel@tonic-gate 		pp = done_pplist;
30260Sstevel@tonic-gate 		do {
30270Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fill_vp_pages[26]);
30280Sstevel@tonic-gate 			ASSERT(pp->p_szc == szc);
30290Sstevel@tonic-gate 			ASSERT(PAGE_EXCL(pp));
30300Sstevel@tonic-gate 			ASSERT(pp->p_vnode == vp);
30310Sstevel@tonic-gate 			pp->p_szc = 0;
30320Sstevel@tonic-gate 		} while ((pp = pp->p_next) != done_pplist);
30330Sstevel@tonic-gate 
30340Sstevel@tonic-gate 		while (pplist != NULL) {
30350Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fill_vp_pages[27]);
30360Sstevel@tonic-gate 			pp = pplist;
30370Sstevel@tonic-gate 			page_sub(&pplist, pp);
30380Sstevel@tonic-gate 			page_free(pp, 0);
30390Sstevel@tonic-gate 		}
30400Sstevel@tonic-gate 
30410Sstevel@tonic-gate 		while (done_pplist != NULL) {
30420Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fill_vp_pages[28]);
30430Sstevel@tonic-gate 			pp = done_pplist;
30440Sstevel@tonic-gate 			page_sub(&done_pplist, pp);
30450Sstevel@tonic-gate 			page_unlock(pp);
30460Sstevel@tonic-gate 		}
30470Sstevel@tonic-gate 		*ppplist = NULL;
30480Sstevel@tonic-gate 		return (0);
30490Sstevel@tonic-gate 	}
30500Sstevel@tonic-gate 	ASSERT(pplist == *ppplist);
30510Sstevel@tonic-gate 	if (io_err) {
30520Sstevel@tonic-gate 		VM_STAT_ADD(segvnvmstats.fill_vp_pages[29]);
30530Sstevel@tonic-gate 		/*
30540Sstevel@tonic-gate 		 * don't downsize on io error.
30550Sstevel@tonic-gate 		 * see if vop_getpage succeeds.
30560Sstevel@tonic-gate 		 * pplist may still be used in this case
30570Sstevel@tonic-gate 		 * for relocations.
30580Sstevel@tonic-gate 		 */
30590Sstevel@tonic-gate 		return (0);
30600Sstevel@tonic-gate 	}
30610Sstevel@tonic-gate 	VM_STAT_ADD(segvnvmstats.fill_vp_pages[30]);
30620Sstevel@tonic-gate 	page_free_replacement_page(pplist);
30630Sstevel@tonic-gate 	page_create_putback(pages);
30640Sstevel@tonic-gate 	*ppplist = NULL;
30650Sstevel@tonic-gate 	return (0);
30660Sstevel@tonic-gate }
30670Sstevel@tonic-gate 
30680Sstevel@tonic-gate int segvn_anypgsz = 0;
30690Sstevel@tonic-gate 
30700Sstevel@tonic-gate #define	SEGVN_RESTORE_SOFTLOCK(type, pages) 		\
30710Sstevel@tonic-gate 		if ((type) == F_SOFTLOCK) {		\
30720Sstevel@tonic-gate 			mutex_enter(&freemem_lock);	\
30730Sstevel@tonic-gate 			availrmem += (pages);		\
30740Sstevel@tonic-gate 			segvn_pages_locked -= (pages);	\
30750Sstevel@tonic-gate 			svd->softlockcnt -= (pages);	\
30760Sstevel@tonic-gate 			mutex_exit(&freemem_lock);	\
30770Sstevel@tonic-gate 		}
30780Sstevel@tonic-gate 
30790Sstevel@tonic-gate #define	SEGVN_UPDATE_MODBITS(ppa, pages, rw, prot, vpprot)		\
30800Sstevel@tonic-gate 		if (IS_VMODSORT((ppa)[0]->p_vnode)) {			\
30810Sstevel@tonic-gate 			if ((rw) == S_WRITE) {				\
30820Sstevel@tonic-gate 				for (i = 0; i < (pages); i++) {		\
30830Sstevel@tonic-gate 					ASSERT((ppa)[i]->p_vnode ==	\
30840Sstevel@tonic-gate 					    (ppa)[0]->p_vnode);		\
30850Sstevel@tonic-gate 					hat_setmod((ppa)[i]);		\
30860Sstevel@tonic-gate 				}					\
30870Sstevel@tonic-gate 			} else if ((rw) != S_OTHER &&			\
30880Sstevel@tonic-gate 			    ((prot) & (vpprot) & PROT_WRITE)) {		\
30890Sstevel@tonic-gate 				for (i = 0; i < (pages); i++) {		\
30900Sstevel@tonic-gate 					ASSERT((ppa)[i]->p_vnode ==	\
30910Sstevel@tonic-gate 					    (ppa)[0]->p_vnode);		\
30920Sstevel@tonic-gate 					if (!hat_ismod((ppa)[i])) {	\
30930Sstevel@tonic-gate 						prot &= ~PROT_WRITE;	\
30940Sstevel@tonic-gate 						break;			\
30950Sstevel@tonic-gate 					}				\
30960Sstevel@tonic-gate 				}					\
30970Sstevel@tonic-gate 			}						\
30980Sstevel@tonic-gate 		}
30990Sstevel@tonic-gate 
31000Sstevel@tonic-gate #ifdef  VM_STATS
31010Sstevel@tonic-gate 
31020Sstevel@tonic-gate #define	SEGVN_VMSTAT_FLTVNPAGES(idx)					\
31030Sstevel@tonic-gate 		VM_STAT_ADD(segvnvmstats.fltvnpages[(idx)]);
31040Sstevel@tonic-gate 
31050Sstevel@tonic-gate #else /* VM_STATS */
31060Sstevel@tonic-gate 
31070Sstevel@tonic-gate #define	SEGVN_VMSTAT_FLTVNPAGES(idx)
31080Sstevel@tonic-gate 
31090Sstevel@tonic-gate #endif
31100Sstevel@tonic-gate 
31110Sstevel@tonic-gate static faultcode_t
31120Sstevel@tonic-gate segvn_fault_vnodepages(struct hat *hat, struct seg *seg, caddr_t lpgaddr,
31130Sstevel@tonic-gate     caddr_t lpgeaddr, enum fault_type type, enum seg_rw rw, caddr_t addr,
31140Sstevel@tonic-gate     caddr_t eaddr, int brkcow)
31150Sstevel@tonic-gate {
31160Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
31170Sstevel@tonic-gate 	struct anon_map *amp = svd->amp;
31180Sstevel@tonic-gate 	uchar_t segtype = svd->type;
31190Sstevel@tonic-gate 	uint_t szc = seg->s_szc;
31200Sstevel@tonic-gate 	size_t pgsz = page_get_pagesize(szc);
31210Sstevel@tonic-gate 	size_t maxpgsz = pgsz;
31220Sstevel@tonic-gate 	pgcnt_t pages = btop(pgsz);
31230Sstevel@tonic-gate 	pgcnt_t maxpages = pages;
31240Sstevel@tonic-gate 	size_t ppasize = (pages + 1) * sizeof (page_t *);
31250Sstevel@tonic-gate 	caddr_t a = lpgaddr;
31260Sstevel@tonic-gate 	caddr_t	maxlpgeaddr = lpgeaddr;
31270Sstevel@tonic-gate 	u_offset_t off = svd->offset + (uintptr_t)(a - seg->s_base);
31280Sstevel@tonic-gate 	ulong_t aindx = svd->anon_index + seg_page(seg, a);
31290Sstevel@tonic-gate 	struct vpage *vpage = (svd->vpage != NULL) ?
31300Sstevel@tonic-gate 	    &svd->vpage[seg_page(seg, a)] : NULL;
31310Sstevel@tonic-gate 	vnode_t *vp = svd->vp;
31320Sstevel@tonic-gate 	page_t **ppa;
31330Sstevel@tonic-gate 	uint_t	pszc;
31340Sstevel@tonic-gate 	size_t	ppgsz;
31350Sstevel@tonic-gate 	pgcnt_t	ppages;
31360Sstevel@tonic-gate 	faultcode_t err = 0;
31370Sstevel@tonic-gate 	int ierr;
31380Sstevel@tonic-gate 	int vop_size_err = 0;
31390Sstevel@tonic-gate 	uint_t protchk, prot, vpprot;
31400Sstevel@tonic-gate 	ulong_t i;
31410Sstevel@tonic-gate 	int hat_flag = (type == F_SOFTLOCK) ? HAT_LOAD_LOCK : HAT_LOAD;
31420Sstevel@tonic-gate 	anon_sync_obj_t an_cookie;
31430Sstevel@tonic-gate 	enum seg_rw arw;
31440Sstevel@tonic-gate 	int alloc_failed = 0;
31450Sstevel@tonic-gate 	int adjszc_chk;
31460Sstevel@tonic-gate 	struct vattr va;
31470Sstevel@tonic-gate 	int xhat = 0;
31480Sstevel@tonic-gate 	page_t *pplist;
31490Sstevel@tonic-gate 	pfn_t pfn;
31500Sstevel@tonic-gate 	int physcontig;
31510Sstevel@tonic-gate 	int upgrdfail;
31520Sstevel@tonic-gate 	int segvn_anypgsz_vnode = 0; /* for now map vnode with 2 page sizes */
31530Sstevel@tonic-gate 
31540Sstevel@tonic-gate 	ASSERT(szc != 0);
31550Sstevel@tonic-gate 	ASSERT(vp != NULL);
31560Sstevel@tonic-gate 	ASSERT(brkcow == 0 || amp != NULL);
31570Sstevel@tonic-gate 	ASSERT(enable_mbit_wa == 0); /* no mbit simulations with large pages */
31580Sstevel@tonic-gate 	ASSERT(!(svd->flags & MAP_NORESERVE));
31590Sstevel@tonic-gate 	ASSERT(type != F_SOFTUNLOCK);
31600Sstevel@tonic-gate 	ASSERT(IS_P2ALIGNED(a, maxpgsz));
31610Sstevel@tonic-gate 	ASSERT(amp == NULL || IS_P2ALIGNED(aindx, maxpages));
31620Sstevel@tonic-gate 	ASSERT(SEGVN_LOCK_HELD(seg->s_as, &svd->lock));
31630Sstevel@tonic-gate 	ASSERT(seg->s_szc < NBBY * sizeof (int));
316463Saguzovsk 	ASSERT(type != F_SOFTLOCK || lpgeaddr - a == maxpgsz);
31650Sstevel@tonic-gate 
31660Sstevel@tonic-gate 	VM_STAT_COND_ADD(type == F_SOFTLOCK, segvnvmstats.fltvnpages[0]);
31670Sstevel@tonic-gate 	VM_STAT_COND_ADD(type != F_SOFTLOCK, segvnvmstats.fltvnpages[1]);
31680Sstevel@tonic-gate 
31690Sstevel@tonic-gate 	if (svd->flags & MAP_TEXT) {
31700Sstevel@tonic-gate 		hat_flag |= HAT_LOAD_TEXT;
31710Sstevel@tonic-gate 	}
31720Sstevel@tonic-gate 
31730Sstevel@tonic-gate 	if (svd->pageprot) {
31740Sstevel@tonic-gate 		switch (rw) {
31750Sstevel@tonic-gate 		case S_READ:
31760Sstevel@tonic-gate 			protchk = PROT_READ;
31770Sstevel@tonic-gate 			break;
31780Sstevel@tonic-gate 		case S_WRITE:
31790Sstevel@tonic-gate 			protchk = PROT_WRITE;
31800Sstevel@tonic-gate 			break;
31810Sstevel@tonic-gate 		case S_EXEC:
31820Sstevel@tonic-gate 			protchk = PROT_EXEC;
31830Sstevel@tonic-gate 			break;
31840Sstevel@tonic-gate 		case S_OTHER:
31850Sstevel@tonic-gate 		default:
31860Sstevel@tonic-gate 			protchk = PROT_READ | PROT_WRITE | PROT_EXEC;
31870Sstevel@tonic-gate 			break;
31880Sstevel@tonic-gate 		}
31890Sstevel@tonic-gate 	} else {
31900Sstevel@tonic-gate 		prot = svd->prot;
31910Sstevel@tonic-gate 		/* caller has already done segment level protection check. */
31920Sstevel@tonic-gate 	}
31930Sstevel@tonic-gate 
31940Sstevel@tonic-gate 	if (seg->s_as->a_hat != hat) {
31950Sstevel@tonic-gate 		xhat = 1;
31960Sstevel@tonic-gate 	}
31970Sstevel@tonic-gate 
31980Sstevel@tonic-gate 	if (rw == S_WRITE && segtype == MAP_PRIVATE) {
31990Sstevel@tonic-gate 		SEGVN_VMSTAT_FLTVNPAGES(2);
32000Sstevel@tonic-gate 		arw = S_READ;
32010Sstevel@tonic-gate 	} else {
32020Sstevel@tonic-gate 		arw = rw;
32030Sstevel@tonic-gate 	}
32040Sstevel@tonic-gate 
32050Sstevel@tonic-gate 	ppa = kmem_alloc(ppasize, KM_SLEEP);
32060Sstevel@tonic-gate 
32070Sstevel@tonic-gate 	VM_STAT_COND_ADD(amp != NULL, segvnvmstats.fltvnpages[3]);
32080Sstevel@tonic-gate 
32090Sstevel@tonic-gate 	for (;;) {
32100Sstevel@tonic-gate 		adjszc_chk = 0;
32110Sstevel@tonic-gate 		for (; a < lpgeaddr; a += pgsz, off += pgsz, aindx += pages) {
32120Sstevel@tonic-gate 			if (adjszc_chk) {
32130Sstevel@tonic-gate 				while (szc < seg->s_szc) {
32140Sstevel@tonic-gate 					uintptr_t e;
32150Sstevel@tonic-gate 					uint_t tszc;
32160Sstevel@tonic-gate 					tszc = segvn_anypgsz_vnode ? szc + 1 :
32170Sstevel@tonic-gate 					    seg->s_szc;
32180Sstevel@tonic-gate 					ppgsz = page_get_pagesize(tszc);
32190Sstevel@tonic-gate 					if (!IS_P2ALIGNED(a, ppgsz) ||
32200Sstevel@tonic-gate 					    ((alloc_failed >> tszc) &
32210Sstevel@tonic-gate 						0x1)) {
32220Sstevel@tonic-gate 						break;
32230Sstevel@tonic-gate 					}
32240Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(4);
32250Sstevel@tonic-gate 					szc = tszc;
32260Sstevel@tonic-gate 					pgsz = ppgsz;
32270Sstevel@tonic-gate 					pages = btop(pgsz);
32280Sstevel@tonic-gate 					e = P2ROUNDUP((uintptr_t)eaddr, pgsz);
32290Sstevel@tonic-gate 					lpgeaddr = (caddr_t)e;
32300Sstevel@tonic-gate 				}
32310Sstevel@tonic-gate 			}
32320Sstevel@tonic-gate 
32330Sstevel@tonic-gate 		again:
32340Sstevel@tonic-gate 			if (IS_P2ALIGNED(a, maxpgsz) && amp != NULL) {
32350Sstevel@tonic-gate 				ASSERT(IS_P2ALIGNED(aindx, maxpages));
32360Sstevel@tonic-gate 				ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
32370Sstevel@tonic-gate 				anon_array_enter(amp, aindx, &an_cookie);
32380Sstevel@tonic-gate 				if (anon_get_ptr(amp->ahp, aindx) != NULL) {
32390Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(5);
32400Sstevel@tonic-gate 					if (anon_pages(amp->ahp, aindx,
32410Sstevel@tonic-gate 					    maxpages) != maxpages) {
32420Sstevel@tonic-gate 						panic("segvn_fault_vnodepages:"
32430Sstevel@tonic-gate 						    " empty anon slots\n");
32440Sstevel@tonic-gate 					}
32450Sstevel@tonic-gate 					anon_array_exit(&an_cookie);
32460Sstevel@tonic-gate 					ANON_LOCK_EXIT(&amp->a_rwlock);
32470Sstevel@tonic-gate 					err = segvn_fault_anonpages(hat, seg,
32480Sstevel@tonic-gate 					    a, a + maxpgsz, type, rw,
32490Sstevel@tonic-gate 					    MAX(a, addr),
32500Sstevel@tonic-gate 					    MIN(a + maxpgsz, eaddr), brkcow);
32510Sstevel@tonic-gate 					if (err != 0) {
32520Sstevel@tonic-gate 						SEGVN_VMSTAT_FLTVNPAGES(6);
32530Sstevel@tonic-gate 						goto out;
32540Sstevel@tonic-gate 					}
32550Sstevel@tonic-gate 					if (szc < seg->s_szc) {
32560Sstevel@tonic-gate 						szc = seg->s_szc;
32570Sstevel@tonic-gate 						pgsz = maxpgsz;
32580Sstevel@tonic-gate 						pages = maxpages;
32590Sstevel@tonic-gate 						lpgeaddr = maxlpgeaddr;
32600Sstevel@tonic-gate 					}
32610Sstevel@tonic-gate 					goto next;
32620Sstevel@tonic-gate 				} else if (anon_pages(amp->ahp, aindx,
32630Sstevel@tonic-gate 				    maxpages)) {
32640Sstevel@tonic-gate 					panic("segvn_fault_vnodepages:"
32650Sstevel@tonic-gate 						" non empty anon slots\n");
32660Sstevel@tonic-gate 				} else {
32670Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(7);
32680Sstevel@tonic-gate 					anon_array_exit(&an_cookie);
32690Sstevel@tonic-gate 					ANON_LOCK_EXIT(&amp->a_rwlock);
32700Sstevel@tonic-gate 				}
32710Sstevel@tonic-gate 			}
32720Sstevel@tonic-gate 			ASSERT(!brkcow || IS_P2ALIGNED(a, maxpgsz));
32730Sstevel@tonic-gate 
32740Sstevel@tonic-gate 			if (svd->pageprot != 0 && IS_P2ALIGNED(a, maxpgsz)) {
32750Sstevel@tonic-gate 				ASSERT(vpage != NULL);
32760Sstevel@tonic-gate 				prot = VPP_PROT(vpage);
32770Sstevel@tonic-gate 				ASSERT(sameprot(seg, a, maxpgsz));
32780Sstevel@tonic-gate 				if ((prot & protchk) == 0) {
32790Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(8);
32800Sstevel@tonic-gate 					err = FC_PROT;
32810Sstevel@tonic-gate 					goto out;
32820Sstevel@tonic-gate 				}
32830Sstevel@tonic-gate 			}
32840Sstevel@tonic-gate 			if (type == F_SOFTLOCK) {
32850Sstevel@tonic-gate 				mutex_enter(&freemem_lock);
32860Sstevel@tonic-gate 				if (availrmem < tune.t_minarmem + pages) {
32870Sstevel@tonic-gate 					mutex_exit(&freemem_lock);
32880Sstevel@tonic-gate 					err = FC_MAKE_ERR(ENOMEM);
32890Sstevel@tonic-gate 					goto out;
32900Sstevel@tonic-gate 				} else {
32910Sstevel@tonic-gate 					availrmem -= pages;
32920Sstevel@tonic-gate 					segvn_pages_locked += pages;
32930Sstevel@tonic-gate 					svd->softlockcnt += pages;
32940Sstevel@tonic-gate 				}
32950Sstevel@tonic-gate 				mutex_exit(&freemem_lock);
32960Sstevel@tonic-gate 			}
32970Sstevel@tonic-gate 
32980Sstevel@tonic-gate 			pplist = NULL;
32990Sstevel@tonic-gate 			physcontig = 0;
33000Sstevel@tonic-gate 			ppa[0] = NULL;
33010Sstevel@tonic-gate 			if (!brkcow && szc &&
33020Sstevel@tonic-gate 			    !page_exists_physcontig(vp, off, szc,
33030Sstevel@tonic-gate 				segtype == MAP_PRIVATE ? ppa : NULL)) {
33040Sstevel@tonic-gate 				SEGVN_VMSTAT_FLTVNPAGES(9);
33050Sstevel@tonic-gate 				if (page_alloc_pages(seg, a, &pplist, NULL,
330663Saguzovsk 				    szc, 0) && type != F_SOFTLOCK) {
33070Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(10);
33080Sstevel@tonic-gate 					pszc = 0;
33090Sstevel@tonic-gate 					ierr = -1;
33100Sstevel@tonic-gate 					alloc_failed |= (1 << szc);
33110Sstevel@tonic-gate 					break;
33120Sstevel@tonic-gate 				}
331363Saguzovsk 				if (pplist != NULL &&
331463Saguzovsk 				    vp->v_mpssdata == SEGVN_PAGEIO) {
33150Sstevel@tonic-gate 					int downsize;
33160Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(11);
33170Sstevel@tonic-gate 					physcontig = segvn_fill_vp_pages(svd,
33180Sstevel@tonic-gate 					    vp, off, szc, ppa, &pplist,
33190Sstevel@tonic-gate 					    &pszc, &downsize);
33200Sstevel@tonic-gate 					ASSERT(!physcontig || pplist == NULL);
332163Saguzovsk 					if (!physcontig && downsize &&
332263Saguzovsk 					    type != F_SOFTLOCK) {
33230Sstevel@tonic-gate 						ASSERT(pplist == NULL);
33240Sstevel@tonic-gate 						SEGVN_VMSTAT_FLTVNPAGES(12);
33250Sstevel@tonic-gate 						ierr = -1;
33260Sstevel@tonic-gate 						break;
33270Sstevel@tonic-gate 					}
33280Sstevel@tonic-gate 					ASSERT(!physcontig ||
33290Sstevel@tonic-gate 					    segtype == MAP_PRIVATE ||
33300Sstevel@tonic-gate 					    ppa[0] == NULL);
33310Sstevel@tonic-gate 					if (physcontig && ppa[0] == NULL) {
33320Sstevel@tonic-gate 						physcontig = 0;
33330Sstevel@tonic-gate 					}
33340Sstevel@tonic-gate 				}
33350Sstevel@tonic-gate 			} else if (!brkcow && szc && ppa[0] != NULL) {
33360Sstevel@tonic-gate 				SEGVN_VMSTAT_FLTVNPAGES(13);
33370Sstevel@tonic-gate 				ASSERT(segtype == MAP_PRIVATE);
33380Sstevel@tonic-gate 				physcontig = 1;
33390Sstevel@tonic-gate 			}
33400Sstevel@tonic-gate 
33410Sstevel@tonic-gate 			if (!physcontig) {
33420Sstevel@tonic-gate 				SEGVN_VMSTAT_FLTVNPAGES(14);
33430Sstevel@tonic-gate 				ppa[0] = NULL;
33440Sstevel@tonic-gate 				ierr = VOP_GETPAGE(vp, (offset_t)off, pgsz,
33450Sstevel@tonic-gate 				    &vpprot, ppa, pgsz, seg, a, arw,
33460Sstevel@tonic-gate 				    svd->cred);
33470Sstevel@tonic-gate 				if (segtype == MAP_PRIVATE) {
33480Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(15);
33490Sstevel@tonic-gate 					vpprot &= ~PROT_WRITE;
33500Sstevel@tonic-gate 				}
33510Sstevel@tonic-gate 			} else {
33520Sstevel@tonic-gate 				ASSERT(segtype == MAP_PRIVATE);
33530Sstevel@tonic-gate 				SEGVN_VMSTAT_FLTVNPAGES(16);
33540Sstevel@tonic-gate 				vpprot = PROT_ALL & ~PROT_WRITE;
33550Sstevel@tonic-gate 				ierr = 0;
33560Sstevel@tonic-gate 			}
33570Sstevel@tonic-gate 
33580Sstevel@tonic-gate 			if (ierr != 0) {
33590Sstevel@tonic-gate 				SEGVN_VMSTAT_FLTVNPAGES(17);
33600Sstevel@tonic-gate 				if (pplist != NULL) {
33610Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(18);
33620Sstevel@tonic-gate 					page_free_replacement_page(pplist);
33630Sstevel@tonic-gate 					page_create_putback(pages);
33640Sstevel@tonic-gate 				}
33650Sstevel@tonic-gate 				SEGVN_RESTORE_SOFTLOCK(type, pages);
33660Sstevel@tonic-gate 				if (a + pgsz <= eaddr) {
33670Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(19);
33680Sstevel@tonic-gate 					err = FC_MAKE_ERR(ierr);
33690Sstevel@tonic-gate 					goto out;
33700Sstevel@tonic-gate 				}
33710Sstevel@tonic-gate 				va.va_mask = AT_SIZE;
33720Sstevel@tonic-gate 				if (VOP_GETATTR(vp, &va, 0, svd->cred) != 0) {
33730Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(20);
33740Sstevel@tonic-gate 					err = FC_MAKE_ERR(EIO);
33750Sstevel@tonic-gate 					goto out;
33760Sstevel@tonic-gate 				}
33770Sstevel@tonic-gate 				if (btopr(va.va_size) >= btopr(off + pgsz)) {
33780Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(21);
3379490Ssusans 					err = FC_MAKE_ERR(ierr);
33800Sstevel@tonic-gate 					goto out;
33810Sstevel@tonic-gate 				}
33820Sstevel@tonic-gate 				if (btopr(va.va_size) <
33830Sstevel@tonic-gate 				    btopr(off + (eaddr - a))) {
33840Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(22);
3385490Ssusans 					err = FC_MAKE_ERR(ierr);
33860Sstevel@tonic-gate 					goto out;
33870Sstevel@tonic-gate 				}
33880Sstevel@tonic-gate 				if (brkcow || type == F_SOFTLOCK) {
33890Sstevel@tonic-gate 					/* can't reduce map area */
33900Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(23);
33910Sstevel@tonic-gate 					vop_size_err = 1;
33920Sstevel@tonic-gate 					goto out;
33930Sstevel@tonic-gate 				}
33940Sstevel@tonic-gate 				SEGVN_VMSTAT_FLTVNPAGES(24);
33950Sstevel@tonic-gate 				ASSERT(szc != 0);
33960Sstevel@tonic-gate 				pszc = 0;
33970Sstevel@tonic-gate 				ierr = -1;
33980Sstevel@tonic-gate 				break;
33990Sstevel@tonic-gate 			}
34000Sstevel@tonic-gate 
34010Sstevel@tonic-gate 			if (amp != NULL) {
34020Sstevel@tonic-gate 				ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
34030Sstevel@tonic-gate 				anon_array_enter(amp, aindx, &an_cookie);
34040Sstevel@tonic-gate 			}
34050Sstevel@tonic-gate 			if (amp != NULL &&
34060Sstevel@tonic-gate 			    anon_get_ptr(amp->ahp, aindx) != NULL) {
34070Sstevel@tonic-gate 				ulong_t taindx = P2ALIGN(aindx, maxpages);
34080Sstevel@tonic-gate 
34090Sstevel@tonic-gate 				SEGVN_VMSTAT_FLTVNPAGES(25);
34100Sstevel@tonic-gate 				if (anon_pages(amp->ahp, taindx, maxpages) !=
34110Sstevel@tonic-gate 				    maxpages) {
34120Sstevel@tonic-gate 					panic("segvn_fault_vnodepages:"
34130Sstevel@tonic-gate 					    " empty anon slots\n");
34140Sstevel@tonic-gate 				}
34150Sstevel@tonic-gate 				for (i = 0; i < pages; i++) {
34160Sstevel@tonic-gate 					page_unlock(ppa[i]);
34170Sstevel@tonic-gate 				}
34180Sstevel@tonic-gate 				anon_array_exit(&an_cookie);
34190Sstevel@tonic-gate 				ANON_LOCK_EXIT(&amp->a_rwlock);
34200Sstevel@tonic-gate 				if (pplist != NULL) {
34210Sstevel@tonic-gate 					page_free_replacement_page(pplist);
34220Sstevel@tonic-gate 					page_create_putback(pages);
34230Sstevel@tonic-gate 				}
34240Sstevel@tonic-gate 				SEGVN_RESTORE_SOFTLOCK(type, pages);
34250Sstevel@tonic-gate 				if (szc < seg->s_szc) {
34260Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(26);
34270Sstevel@tonic-gate 					/*
34280Sstevel@tonic-gate 					 * For private segments SOFTLOCK
34290Sstevel@tonic-gate 					 * either always breaks cow (any rw
34300Sstevel@tonic-gate 					 * type except S_READ_NOCOW) or
34310Sstevel@tonic-gate 					 * address space is locked as writer
34320Sstevel@tonic-gate 					 * (S_READ_NOCOW case) and anon slots
34330Sstevel@tonic-gate 					 * can't show up on second check.
34340Sstevel@tonic-gate 					 * Therefore if we are here for
34350Sstevel@tonic-gate 					 * SOFTLOCK case it must be a cow
34360Sstevel@tonic-gate 					 * break but cow break never reduces
34370Sstevel@tonic-gate 					 * szc. Thus the assert below.
34380Sstevel@tonic-gate 					 */
34390Sstevel@tonic-gate 					ASSERT(!brkcow && type != F_SOFTLOCK);
34400Sstevel@tonic-gate 					pszc = seg->s_szc;
34410Sstevel@tonic-gate 					ierr = -2;
34420Sstevel@tonic-gate 					break;
34430Sstevel@tonic-gate 				}
34440Sstevel@tonic-gate 				ASSERT(IS_P2ALIGNED(a, maxpgsz));
34450Sstevel@tonic-gate 				goto again;
34460Sstevel@tonic-gate 			}
34470Sstevel@tonic-gate #ifdef DEBUG
34480Sstevel@tonic-gate 			if (amp != NULL) {
34490Sstevel@tonic-gate 				ulong_t taindx = P2ALIGN(aindx, maxpages);
34500Sstevel@tonic-gate 				ASSERT(!anon_pages(amp->ahp, taindx, maxpages));
34510Sstevel@tonic-gate 			}
34520Sstevel@tonic-gate #endif /* DEBUG */
34530Sstevel@tonic-gate 
34540Sstevel@tonic-gate 			if (brkcow) {
34550Sstevel@tonic-gate 				ASSERT(amp != NULL);
34560Sstevel@tonic-gate 				ASSERT(pplist == NULL);
34570Sstevel@tonic-gate 				ASSERT(szc == seg->s_szc);
34580Sstevel@tonic-gate 				ASSERT(IS_P2ALIGNED(a, maxpgsz));
34590Sstevel@tonic-gate 				ASSERT(IS_P2ALIGNED(aindx, maxpages));
34600Sstevel@tonic-gate 				SEGVN_VMSTAT_FLTVNPAGES(27);
34610Sstevel@tonic-gate 				ierr = anon_map_privatepages(amp, aindx, szc,
34620Sstevel@tonic-gate 				    seg, a, prot, ppa, vpage, segvn_anypgsz,
34630Sstevel@tonic-gate 				    svd->cred);
34640Sstevel@tonic-gate 				if (ierr != 0) {
34650Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(28);
34660Sstevel@tonic-gate 					anon_array_exit(&an_cookie);
34670Sstevel@tonic-gate 					ANON_LOCK_EXIT(&amp->a_rwlock);
34680Sstevel@tonic-gate 					SEGVN_RESTORE_SOFTLOCK(type, pages);
34690Sstevel@tonic-gate 					err = FC_MAKE_ERR(ierr);
34700Sstevel@tonic-gate 					goto out;
34710Sstevel@tonic-gate 				}
34720Sstevel@tonic-gate 
34730Sstevel@tonic-gate 				ASSERT(!IS_VMODSORT(ppa[0]->p_vnode));
34740Sstevel@tonic-gate 				/*
34750Sstevel@tonic-gate 				 * p_szc can't be changed for locked
34760Sstevel@tonic-gate 				 * swapfs pages.
34770Sstevel@tonic-gate 				 */
34780Sstevel@tonic-gate 				hat_memload_array(hat, a, pgsz, ppa, prot,
34790Sstevel@tonic-gate 				    hat_flag);
34800Sstevel@tonic-gate 
34810Sstevel@tonic-gate 				if (!(hat_flag & HAT_LOAD_LOCK)) {
34820Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(29);
34830Sstevel@tonic-gate 					for (i = 0; i < pages; i++) {
34840Sstevel@tonic-gate 						page_unlock(ppa[i]);
34850Sstevel@tonic-gate 					}
34860Sstevel@tonic-gate 				}
34870Sstevel@tonic-gate 				anon_array_exit(&an_cookie);
34880Sstevel@tonic-gate 				ANON_LOCK_EXIT(&amp->a_rwlock);
34890Sstevel@tonic-gate 				goto next;
34900Sstevel@tonic-gate 			}
34910Sstevel@tonic-gate 
34920Sstevel@tonic-gate 			pfn = page_pptonum(ppa[0]);
34930Sstevel@tonic-gate 			/*
34940Sstevel@tonic-gate 			 * hat_page_demote() needs an EXCl lock on one of
34950Sstevel@tonic-gate 			 * constituent page_t's and it decreases root's p_szc
34960Sstevel@tonic-gate 			 * last. This means if root's p_szc is equal szc and
34970Sstevel@tonic-gate 			 * all its constituent pages are locked
34980Sstevel@tonic-gate 			 * hat_page_demote() that could have changed p_szc to
34990Sstevel@tonic-gate 			 * szc is already done and no new have page_demote()
35000Sstevel@tonic-gate 			 * can start for this large page.
35010Sstevel@tonic-gate 			 */
35020Sstevel@tonic-gate 
35030Sstevel@tonic-gate 			/*
35040Sstevel@tonic-gate 			 * we need to make sure same mapping size is used for
35050Sstevel@tonic-gate 			 * the same address range if there's a possibility the
35060Sstevel@tonic-gate 			 * adddress is already mapped because hat layer panics
35070Sstevel@tonic-gate 			 * when translation is loaded for the range already
35080Sstevel@tonic-gate 			 * mapped with a different page size.  We achieve it
35090Sstevel@tonic-gate 			 * by always using largest page size possible subject
35100Sstevel@tonic-gate 			 * to the constraints of page size, segment page size
35110Sstevel@tonic-gate 			 * and page alignment.  Since mappings are invalidated
35120Sstevel@tonic-gate 			 * when those constraints change and make it
35130Sstevel@tonic-gate 			 * impossible to use previously used mapping size no
35140Sstevel@tonic-gate 			 * mapping size conflicts should happen.
35150Sstevel@tonic-gate 			 */
35160Sstevel@tonic-gate 
35170Sstevel@tonic-gate 		chkszc:
35180Sstevel@tonic-gate 			if ((pszc = ppa[0]->p_szc) == szc &&
35190Sstevel@tonic-gate 			    IS_P2ALIGNED(pfn, pages)) {
35200Sstevel@tonic-gate 
35210Sstevel@tonic-gate 				SEGVN_VMSTAT_FLTVNPAGES(30);
35220Sstevel@tonic-gate #ifdef DEBUG
35230Sstevel@tonic-gate 				for (i = 0; i < pages; i++) {
35240Sstevel@tonic-gate 					ASSERT(PAGE_LOCKED(ppa[i]));
35250Sstevel@tonic-gate 					ASSERT(!PP_ISFREE(ppa[i]));
35260Sstevel@tonic-gate 					ASSERT(page_pptonum(ppa[i]) ==
35270Sstevel@tonic-gate 					    pfn + i);
35280Sstevel@tonic-gate 					ASSERT(ppa[i]->p_szc == szc);
35290Sstevel@tonic-gate 					ASSERT(ppa[i]->p_vnode == vp);
35300Sstevel@tonic-gate 					ASSERT(ppa[i]->p_offset ==
35310Sstevel@tonic-gate 					    off + (i << PAGESHIFT));
35320Sstevel@tonic-gate 				}
353363Saguzovsk #endif /* DEBUG */
35340Sstevel@tonic-gate 				/*
35350Sstevel@tonic-gate 				 * All pages are of szc we need and they are
35360Sstevel@tonic-gate 				 * all locked so they can't change szc. load
35370Sstevel@tonic-gate 				 * translations.
35380Sstevel@tonic-gate 				 *
35390Sstevel@tonic-gate 				 * if page got promoted since last check
35400Sstevel@tonic-gate 				 * we don't need pplist.
35410Sstevel@tonic-gate 				 */
35420Sstevel@tonic-gate 				if (pplist != NULL) {
35430Sstevel@tonic-gate 					page_free_replacement_page(pplist);
35440Sstevel@tonic-gate 					page_create_putback(pages);
35450Sstevel@tonic-gate 				}
35460Sstevel@tonic-gate 				if (PP_ISMIGRATE(ppa[0])) {
35470Sstevel@tonic-gate 					page_migrate(seg, a, ppa, pages);
35480Sstevel@tonic-gate 				}
35490Sstevel@tonic-gate 				SEGVN_UPDATE_MODBITS(ppa, pages, rw,
35500Sstevel@tonic-gate 				    prot, vpprot);
35510Sstevel@tonic-gate 				if (!xhat) {
35520Sstevel@tonic-gate 					hat_memload_array(hat, a, pgsz, ppa,
35530Sstevel@tonic-gate 					    prot & vpprot, hat_flag);
35540Sstevel@tonic-gate 				} else {
35550Sstevel@tonic-gate 					/*
35560Sstevel@tonic-gate 					 * avoid large xhat mappings to FS
35570Sstevel@tonic-gate 					 * pages so that hat_page_demote()
35580Sstevel@tonic-gate 					 * doesn't need to check for xhat
35590Sstevel@tonic-gate 					 * large mappings.
35600Sstevel@tonic-gate 					 */
35610Sstevel@tonic-gate 					for (i = 0; i < pages; i++) {
35620Sstevel@tonic-gate 						hat_memload(hat,
35630Sstevel@tonic-gate 						    a + (i << PAGESHIFT),
35640Sstevel@tonic-gate 						    ppa[i], prot & vpprot,
35650Sstevel@tonic-gate 						    hat_flag);
35660Sstevel@tonic-gate 					}
35670Sstevel@tonic-gate 				}
35680Sstevel@tonic-gate 
35690Sstevel@tonic-gate 				if (!(hat_flag & HAT_LOAD_LOCK)) {
35700Sstevel@tonic-gate 					for (i = 0; i < pages; i++) {
35710Sstevel@tonic-gate 						page_unlock(ppa[i]);
35720Sstevel@tonic-gate 					}
35730Sstevel@tonic-gate 				}
35740Sstevel@tonic-gate 				if (amp != NULL) {
35750Sstevel@tonic-gate 					anon_array_exit(&an_cookie);
35760Sstevel@tonic-gate 					ANON_LOCK_EXIT(&amp->a_rwlock);
35770Sstevel@tonic-gate 				}
35780Sstevel@tonic-gate 				goto next;
35790Sstevel@tonic-gate 			}
35800Sstevel@tonic-gate 
35810Sstevel@tonic-gate 			/*
35820Sstevel@tonic-gate 			 * See if upsize is possible.
35830Sstevel@tonic-gate 			 */
35840Sstevel@tonic-gate 			if (pszc > szc && szc < seg->s_szc &&
35850Sstevel@tonic-gate 			    (segvn_anypgsz_vnode || pszc >= seg->s_szc)) {
35860Sstevel@tonic-gate 				pgcnt_t aphase;
35870Sstevel@tonic-gate 				uint_t pszc1 = MIN(pszc, seg->s_szc);
35880Sstevel@tonic-gate 				ppgsz = page_get_pagesize(pszc1);
35890Sstevel@tonic-gate 				ppages = btop(ppgsz);
35900Sstevel@tonic-gate 				aphase = btop(P2PHASE((uintptr_t)a, ppgsz));
35910Sstevel@tonic-gate 
359263Saguzovsk 				ASSERT(type != F_SOFTLOCK);
359363Saguzovsk 
35940Sstevel@tonic-gate 				SEGVN_VMSTAT_FLTVNPAGES(31);
35950Sstevel@tonic-gate 				if (aphase != P2PHASE(pfn, ppages)) {
35960Sstevel@tonic-gate 					segvn_faultvnmpss_align_err4++;
35970Sstevel@tonic-gate 				} else {
35980Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(32);
35990Sstevel@tonic-gate 					if (pplist != NULL) {
36000Sstevel@tonic-gate 						page_t *pl = pplist;
36010Sstevel@tonic-gate 						page_free_replacement_page(pl);
36020Sstevel@tonic-gate 						page_create_putback(pages);
36030Sstevel@tonic-gate 					}
36040Sstevel@tonic-gate 					for (i = 0; i < pages; i++) {
36050Sstevel@tonic-gate 						page_unlock(ppa[i]);
36060Sstevel@tonic-gate 					}
36070Sstevel@tonic-gate 					if (amp != NULL) {
36080Sstevel@tonic-gate 						anon_array_exit(&an_cookie);
36090Sstevel@tonic-gate 						ANON_LOCK_EXIT(&amp->a_rwlock);
36100Sstevel@tonic-gate 					}
36110Sstevel@tonic-gate 					pszc = pszc1;
36120Sstevel@tonic-gate 					ierr = -2;
36130Sstevel@tonic-gate 					break;
36140Sstevel@tonic-gate 				}
36150Sstevel@tonic-gate 			}
36160Sstevel@tonic-gate 
36170Sstevel@tonic-gate 			/*
36180Sstevel@tonic-gate 			 * check if we should use smallest mapping size.
36190Sstevel@tonic-gate 			 */
36200Sstevel@tonic-gate 			upgrdfail = 0;
36210Sstevel@tonic-gate 			if (szc == 0 || xhat ||
36220Sstevel@tonic-gate 			    (pszc >= szc &&
36230Sstevel@tonic-gate 			    !IS_P2ALIGNED(pfn, pages)) ||
36240Sstevel@tonic-gate 			    (pszc < szc &&
36250Sstevel@tonic-gate 			    !segvn_full_szcpages(ppa, szc, &upgrdfail,
36260Sstevel@tonic-gate 				&pszc))) {
36270Sstevel@tonic-gate 
362863Saguzovsk 				if (upgrdfail && type != F_SOFTLOCK) {
36290Sstevel@tonic-gate 					/*
36300Sstevel@tonic-gate 					 * segvn_full_szcpages failed to lock
36310Sstevel@tonic-gate 					 * all pages EXCL. Size down.
36320Sstevel@tonic-gate 					 */
36330Sstevel@tonic-gate 					ASSERT(pszc < szc);
36340Sstevel@tonic-gate 
36350Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(33);
36360Sstevel@tonic-gate 
36370Sstevel@tonic-gate 					if (pplist != NULL) {
36380Sstevel@tonic-gate 						page_t *pl = pplist;
36390Sstevel@tonic-gate 						page_free_replacement_page(pl);
36400Sstevel@tonic-gate 						page_create_putback(pages);
36410Sstevel@tonic-gate 					}
36420Sstevel@tonic-gate 
36430Sstevel@tonic-gate 					for (i = 0; i < pages; i++) {
36440Sstevel@tonic-gate 						page_unlock(ppa[i]);
36450Sstevel@tonic-gate 					}
36460Sstevel@tonic-gate 					if (amp != NULL) {
36470Sstevel@tonic-gate 						anon_array_exit(&an_cookie);
36480Sstevel@tonic-gate 						ANON_LOCK_EXIT(&amp->a_rwlock);
36490Sstevel@tonic-gate 					}
36500Sstevel@tonic-gate 					ierr = -1;
36510Sstevel@tonic-gate 					break;
36520Sstevel@tonic-gate 				}
36530Sstevel@tonic-gate 				if (szc != 0 && !xhat) {
36540Sstevel@tonic-gate 					segvn_faultvnmpss_align_err5++;
36550Sstevel@tonic-gate 				}
36560Sstevel@tonic-gate 				SEGVN_VMSTAT_FLTVNPAGES(34);
36570Sstevel@tonic-gate 				if (pplist != NULL) {
36580Sstevel@tonic-gate 					page_free_replacement_page(pplist);
36590Sstevel@tonic-gate 					page_create_putback(pages);
36600Sstevel@tonic-gate 				}
36610Sstevel@tonic-gate 				SEGVN_UPDATE_MODBITS(ppa, pages, rw,
36620Sstevel@tonic-gate 				    prot, vpprot);
36630Sstevel@tonic-gate 				for (i = 0; i < pages; i++) {
36640Sstevel@tonic-gate 					hat_memload(hat, a + (i << PAGESHIFT),
36650Sstevel@tonic-gate 					    ppa[i], prot & vpprot, hat_flag);
36660Sstevel@tonic-gate 				}
36670Sstevel@tonic-gate 				if (!(hat_flag & HAT_LOAD_LOCK)) {
36680Sstevel@tonic-gate 					for (i = 0; i < pages; i++) {
36690Sstevel@tonic-gate 						page_unlock(ppa[i]);
36700Sstevel@tonic-gate 					}
36710Sstevel@tonic-gate 				}
36720Sstevel@tonic-gate 				if (amp != NULL) {
36730Sstevel@tonic-gate 					anon_array_exit(&an_cookie);
36740Sstevel@tonic-gate 					ANON_LOCK_EXIT(&amp->a_rwlock);
36750Sstevel@tonic-gate 				}
36760Sstevel@tonic-gate 				goto next;
36770Sstevel@tonic-gate 			}
36780Sstevel@tonic-gate 
36790Sstevel@tonic-gate 			if (pszc == szc) {
36800Sstevel@tonic-gate 				/*
36810Sstevel@tonic-gate 				 * segvn_full_szcpages() upgraded pages szc.
36820Sstevel@tonic-gate 				 */
36830Sstevel@tonic-gate 				ASSERT(pszc == ppa[0]->p_szc);
36840Sstevel@tonic-gate 				ASSERT(IS_P2ALIGNED(pfn, pages));
36850Sstevel@tonic-gate 				goto chkszc;
36860Sstevel@tonic-gate 			}
36870Sstevel@tonic-gate 
36880Sstevel@tonic-gate 			if (pszc > szc) {
36890Sstevel@tonic-gate 				kmutex_t *szcmtx;
36900Sstevel@tonic-gate 				SEGVN_VMSTAT_FLTVNPAGES(35);
36910Sstevel@tonic-gate 				/*
36920Sstevel@tonic-gate 				 * p_szc of ppa[0] can change since we haven't
36930Sstevel@tonic-gate 				 * locked all constituent pages. Call
36940Sstevel@tonic-gate 				 * page_lock_szc() to prevent szc changes.
36950Sstevel@tonic-gate 				 * This should be a rare case that happens when
36960Sstevel@tonic-gate 				 * multiple segments use a different page size
36970Sstevel@tonic-gate 				 * to map the same file offsets.
36980Sstevel@tonic-gate 				 */
36990Sstevel@tonic-gate 				szcmtx = page_szc_lock(ppa[0]);
37000Sstevel@tonic-gate 				pszc = ppa[0]->p_szc;
37010Sstevel@tonic-gate 				ASSERT(szcmtx != NULL || pszc == 0);
37020Sstevel@tonic-gate 				ASSERT(ppa[0]->p_szc <= pszc);
37030Sstevel@tonic-gate 				if (pszc <= szc) {
37040Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(36);
37050Sstevel@tonic-gate 					if (szcmtx != NULL) {
37060Sstevel@tonic-gate 						mutex_exit(szcmtx);
37070Sstevel@tonic-gate 					}
37080Sstevel@tonic-gate 					goto chkszc;
37090Sstevel@tonic-gate 				}
37100Sstevel@tonic-gate 				if (pplist != NULL) {
37110Sstevel@tonic-gate 					/*
37120Sstevel@tonic-gate 					 * page got promoted since last check.
37130Sstevel@tonic-gate 					 * we don't need preaalocated large
37140Sstevel@tonic-gate 					 * page.
37150Sstevel@tonic-gate 					 */
37160Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(37);
37170Sstevel@tonic-gate 					page_free_replacement_page(pplist);
37180Sstevel@tonic-gate 					page_create_putback(pages);
37190Sstevel@tonic-gate 				}
37200Sstevel@tonic-gate 				SEGVN_UPDATE_MODBITS(ppa, pages, rw,
37210Sstevel@tonic-gate 				    prot, vpprot);
37220Sstevel@tonic-gate 				hat_memload_array(hat, a, pgsz, ppa,
37230Sstevel@tonic-gate 				    prot & vpprot, hat_flag);
37240Sstevel@tonic-gate 				mutex_exit(szcmtx);
37250Sstevel@tonic-gate 				if (!(hat_flag & HAT_LOAD_LOCK)) {
37260Sstevel@tonic-gate 					for (i = 0; i < pages; i++) {
37270Sstevel@tonic-gate 						page_unlock(ppa[i]);
37280Sstevel@tonic-gate 					}
37290Sstevel@tonic-gate 				}
37300Sstevel@tonic-gate 				if (amp != NULL) {
37310Sstevel@tonic-gate 					anon_array_exit(&an_cookie);
37320Sstevel@tonic-gate 					ANON_LOCK_EXIT(&amp->a_rwlock);
37330Sstevel@tonic-gate 				}
37340Sstevel@tonic-gate 				goto next;
37350Sstevel@tonic-gate 			}
37360Sstevel@tonic-gate 
37370Sstevel@tonic-gate 			/*
37380Sstevel@tonic-gate 			 * if page got demoted since last check
37390Sstevel@tonic-gate 			 * we could have not allocated larger page.
37400Sstevel@tonic-gate 			 * allocate now.
37410Sstevel@tonic-gate 			 */
37420Sstevel@tonic-gate 			if (pplist == NULL &&
374363Saguzovsk 			    page_alloc_pages(seg, a, &pplist, NULL, szc, 0) &&
374463Saguzovsk 			    type != F_SOFTLOCK) {
37450Sstevel@tonic-gate 				SEGVN_VMSTAT_FLTVNPAGES(38);
37460Sstevel@tonic-gate 				for (i = 0; i < pages; i++) {
37470Sstevel@tonic-gate 					page_unlock(ppa[i]);
37480Sstevel@tonic-gate 				}
37490Sstevel@tonic-gate 				if (amp != NULL) {
37500Sstevel@tonic-gate 					anon_array_exit(&an_cookie);
37510Sstevel@tonic-gate 					ANON_LOCK_EXIT(&amp->a_rwlock);
37520Sstevel@tonic-gate 				}
37530Sstevel@tonic-gate 				ierr = -1;
37540Sstevel@tonic-gate 				alloc_failed |= (1 << szc);
37550Sstevel@tonic-gate 				break;
37560Sstevel@tonic-gate 			}
37570Sstevel@tonic-gate 
37580Sstevel@tonic-gate 			SEGVN_VMSTAT_FLTVNPAGES(39);
37590Sstevel@tonic-gate 
376063Saguzovsk 			if (pplist != NULL) {
376163Saguzovsk 				segvn_relocate_pages(ppa, pplist);
376263Saguzovsk #ifdef DEBUG
376363Saguzovsk 			} else {
376463Saguzovsk 				ASSERT(type == F_SOFTLOCK);
376563Saguzovsk 				SEGVN_VMSTAT_FLTVNPAGES(40);
376663Saguzovsk #endif /* DEBUG */
376763Saguzovsk 			}
37680Sstevel@tonic-gate 
37690Sstevel@tonic-gate 			SEGVN_UPDATE_MODBITS(ppa, pages, rw, prot, vpprot);
377063Saguzovsk 
377163Saguzovsk 			if (pplist == NULL && segvn_anypgsz_vnode == 0) {
377263Saguzovsk 				ASSERT(type == F_SOFTLOCK);
377363Saguzovsk 				for (i = 0; i < pages; i++) {
377463Saguzovsk 					ASSERT(ppa[i]->p_szc < szc);
377563Saguzovsk 					hat_memload(hat, a + (i << PAGESHIFT),
377663Saguzovsk 					    ppa[i], prot & vpprot, hat_flag);
377763Saguzovsk 				}
377863Saguzovsk 			} else {
377963Saguzovsk 				ASSERT(pplist != NULL || type == F_SOFTLOCK);
378063Saguzovsk 				hat_memload_array(hat, a, pgsz, ppa,
378163Saguzovsk 				    prot & vpprot, hat_flag);
378263Saguzovsk 			}
37830Sstevel@tonic-gate 			if (!(hat_flag & HAT_LOAD_LOCK)) {
37840Sstevel@tonic-gate 				for (i = 0; i < pages; i++) {
37850Sstevel@tonic-gate 					ASSERT(PAGE_SHARED(ppa[i]));
37860Sstevel@tonic-gate 					page_unlock(ppa[i]);
37870Sstevel@tonic-gate 				}
37880Sstevel@tonic-gate 			}
37890Sstevel@tonic-gate 			if (amp != NULL) {
37900Sstevel@tonic-gate 				anon_array_exit(&an_cookie);
37910Sstevel@tonic-gate 				ANON_LOCK_EXIT(&amp->a_rwlock);
37920Sstevel@tonic-gate 			}
37930Sstevel@tonic-gate 
37940Sstevel@tonic-gate 		next:
37950Sstevel@tonic-gate 			if (vpage != NULL) {
37960Sstevel@tonic-gate 				vpage += pages;
37970Sstevel@tonic-gate 			}
37980Sstevel@tonic-gate 			adjszc_chk = 1;
37990Sstevel@tonic-gate 		}
38000Sstevel@tonic-gate 		if (a == lpgeaddr)
38010Sstevel@tonic-gate 			break;
38020Sstevel@tonic-gate 		ASSERT(a < lpgeaddr);
380363Saguzovsk 
380463Saguzovsk 		ASSERT(!brkcow && type != F_SOFTLOCK);
380563Saguzovsk 
38060Sstevel@tonic-gate 		/*
38070Sstevel@tonic-gate 		 * ierr == -1 means we failed to map with a large page.
38080Sstevel@tonic-gate 		 * (either due to allocation/relocation failures or
38090Sstevel@tonic-gate 		 * misalignment with other mappings to this file.
38100Sstevel@tonic-gate 		 *
38110Sstevel@tonic-gate 		 * ierr == -2 means some other thread allocated a large page
38120Sstevel@tonic-gate 		 * after we gave up tp map with a large page.  retry with
38130Sstevel@tonic-gate 		 * larger mapping.
38140Sstevel@tonic-gate 		 */
38150Sstevel@tonic-gate 		ASSERT(ierr == -1 || ierr == -2);
38160Sstevel@tonic-gate 		ASSERT(ierr == -2 || szc != 0);
38170Sstevel@tonic-gate 		ASSERT(ierr == -1 || szc < seg->s_szc);
38180Sstevel@tonic-gate 		if (ierr == -2) {
381963Saguzovsk 			SEGVN_VMSTAT_FLTVNPAGES(41);
38200Sstevel@tonic-gate 			ASSERT(pszc > szc && pszc <= seg->s_szc);
38210Sstevel@tonic-gate 			szc = pszc;
38220Sstevel@tonic-gate 		} else if (segvn_anypgsz_vnode) {
382363Saguzovsk 			SEGVN_VMSTAT_FLTVNPAGES(42);
38240Sstevel@tonic-gate 			szc--;
38250Sstevel@tonic-gate 		} else {
382663Saguzovsk 			SEGVN_VMSTAT_FLTVNPAGES(43);
38270Sstevel@tonic-gate 			ASSERT(pszc < szc);
38280Sstevel@tonic-gate 			/*
38290Sstevel@tonic-gate 			 * other process created pszc large page.
38300Sstevel@tonic-gate 			 * but we still have to drop to 0 szc.
38310Sstevel@tonic-gate 			 */
38320Sstevel@tonic-gate 			szc = 0;
38330Sstevel@tonic-gate 		}
38340Sstevel@tonic-gate 
38350Sstevel@tonic-gate 		pgsz = page_get_pagesize(szc);
38360Sstevel@tonic-gate 		pages = btop(pgsz);
383763Saguzovsk 		if (ierr == -2) {
38380Sstevel@tonic-gate 			/*
38390Sstevel@tonic-gate 			 * Size up case. Note lpgaddr may only be needed for
38400Sstevel@tonic-gate 			 * softlock case so we don't adjust it here.
38410Sstevel@tonic-gate 			 */
38420Sstevel@tonic-gate 			a = (caddr_t)P2ALIGN((uintptr_t)a, pgsz);
38430Sstevel@tonic-gate 			ASSERT(a >= lpgaddr);
38440Sstevel@tonic-gate 			lpgeaddr = (caddr_t)P2ROUNDUP((uintptr_t)eaddr, pgsz);
38450Sstevel@tonic-gate 			off = svd->offset + (uintptr_t)(a - seg->s_base);
38460Sstevel@tonic-gate 			aindx = svd->anon_index + seg_page(seg, a);
38470Sstevel@tonic-gate 			vpage = (svd->vpage != NULL) ?
38480Sstevel@tonic-gate 			    &svd->vpage[seg_page(seg, a)] : NULL;
38490Sstevel@tonic-gate 		} else {
38500Sstevel@tonic-gate 			/*
38510Sstevel@tonic-gate 			 * Size down case. Note lpgaddr may only be needed for
38520Sstevel@tonic-gate 			 * softlock case so we don't adjust it here.
38530Sstevel@tonic-gate 			 */
38540Sstevel@tonic-gate 			ASSERT(IS_P2ALIGNED(a, pgsz));
38550Sstevel@tonic-gate 			ASSERT(IS_P2ALIGNED(lpgeaddr, pgsz));
38560Sstevel@tonic-gate 			lpgeaddr = (caddr_t)P2ROUNDUP((uintptr_t)eaddr, pgsz);
38570Sstevel@tonic-gate 			ASSERT(a < lpgeaddr);
38580Sstevel@tonic-gate 			if (a < addr) {
38590Sstevel@tonic-gate 				SEGVN_VMSTAT_FLTVNPAGES(44);
38600Sstevel@tonic-gate 				/*
38610Sstevel@tonic-gate 				 * The beginning of the large page region can
38620Sstevel@tonic-gate 				 * be pulled to the right to make a smaller
38630Sstevel@tonic-gate 				 * region. We haven't yet faulted a single
38640Sstevel@tonic-gate 				 * page.
38650Sstevel@tonic-gate 				 */
38660Sstevel@tonic-gate 				a = (caddr_t)P2ALIGN((uintptr_t)addr, pgsz);
38670Sstevel@tonic-gate 				ASSERT(a >= lpgaddr);
38680Sstevel@tonic-gate 				off = svd->offset +
38690Sstevel@tonic-gate 				    (uintptr_t)(a - seg->s_base);
38700Sstevel@tonic-gate 				aindx = svd->anon_index + seg_page(seg, a);
38710Sstevel@tonic-gate 				vpage = (svd->vpage != NULL) ?
38720Sstevel@tonic-gate 				    &svd->vpage[seg_page(seg, a)] : NULL;
38730Sstevel@tonic-gate 			}
38740Sstevel@tonic-gate 		}
38750Sstevel@tonic-gate 	}
38760Sstevel@tonic-gate out:
38770Sstevel@tonic-gate 	kmem_free(ppa, ppasize);
38780Sstevel@tonic-gate 	if (!err && !vop_size_err) {
38790Sstevel@tonic-gate 		SEGVN_VMSTAT_FLTVNPAGES(45);
38800Sstevel@tonic-gate 		return (0);
38810Sstevel@tonic-gate 	}
38820Sstevel@tonic-gate 	if (type == F_SOFTLOCK && a > lpgaddr) {
38830Sstevel@tonic-gate 		SEGVN_VMSTAT_FLTVNPAGES(46);
38840Sstevel@tonic-gate 		segvn_softunlock(seg, lpgaddr, a - lpgaddr, S_OTHER);
38850Sstevel@tonic-gate 	}
38860Sstevel@tonic-gate 	if (!vop_size_err) {
38870Sstevel@tonic-gate 		SEGVN_VMSTAT_FLTVNPAGES(47);
38880Sstevel@tonic-gate 		return (err);
38890Sstevel@tonic-gate 	}
38900Sstevel@tonic-gate 	ASSERT(brkcow || type == F_SOFTLOCK);
38910Sstevel@tonic-gate 	/*
38920Sstevel@tonic-gate 	 * Large page end is mapped beyond the end of file and it's a cow
38930Sstevel@tonic-gate 	 * fault or softlock so we can't reduce the map area.  For now just
38940Sstevel@tonic-gate 	 * demote the segment. This should really only happen if the end of
38950Sstevel@tonic-gate 	 * the file changed after the mapping was established since when large
38960Sstevel@tonic-gate 	 * page segments are created we make sure they don't extend beyond the
38970Sstevel@tonic-gate 	 * end of the file.
38980Sstevel@tonic-gate 	 */
38990Sstevel@tonic-gate 	SEGVN_VMSTAT_FLTVNPAGES(48);
39000Sstevel@tonic-gate 
39010Sstevel@tonic-gate 	SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
39020Sstevel@tonic-gate 	SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_WRITER);
39030Sstevel@tonic-gate 	err = 0;
39040Sstevel@tonic-gate 	if (seg->s_szc != 0) {
390563Saguzovsk 		segvn_fltvnpages_clrszc_cnt++;
390663Saguzovsk 		ASSERT(svd->softlockcnt == 0);
39070Sstevel@tonic-gate 		err = segvn_clrszc(seg);
39080Sstevel@tonic-gate 		if (err != 0) {
39090Sstevel@tonic-gate 			segvn_fltvnpages_clrszc_err++;
39100Sstevel@tonic-gate 		}
39110Sstevel@tonic-gate 	}
39120Sstevel@tonic-gate 	ASSERT(err || seg->s_szc == 0);
39130Sstevel@tonic-gate 	SEGVN_LOCK_DOWNGRADE(seg->s_as, &svd->lock);
39140Sstevel@tonic-gate 	/* segvn_fault will do its job as if szc had been zero to begin with */
39150Sstevel@tonic-gate 	return (err == 0 ? IE_RETRY : FC_MAKE_ERR(err));
39160Sstevel@tonic-gate }
39170Sstevel@tonic-gate 
39180Sstevel@tonic-gate /*
39190Sstevel@tonic-gate  * This routine will attempt to fault in one large page.
39200Sstevel@tonic-gate  * it will use smaller pages if that fails.
39210Sstevel@tonic-gate  * It should only be called for pure anonymous segments.
39220Sstevel@tonic-gate  */
39230Sstevel@tonic-gate static faultcode_t
39240Sstevel@tonic-gate segvn_fault_anonpages(struct hat *hat, struct seg *seg, caddr_t lpgaddr,
39250Sstevel@tonic-gate     caddr_t lpgeaddr, enum fault_type type, enum seg_rw rw, caddr_t addr,
39260Sstevel@tonic-gate     caddr_t eaddr, int brkcow)
39270Sstevel@tonic-gate {
39280Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
39290Sstevel@tonic-gate 	struct anon_map *amp = svd->amp;
39300Sstevel@tonic-gate 	uchar_t segtype = svd->type;
39310Sstevel@tonic-gate 	uint_t szc = seg->s_szc;
39320Sstevel@tonic-gate 	size_t pgsz = page_get_pagesize(szc);
39330Sstevel@tonic-gate 	size_t maxpgsz = pgsz;
39340Sstevel@tonic-gate 	pgcnt_t pages = btop(pgsz);
39350Sstevel@tonic-gate 	size_t ppasize = pages * sizeof (page_t *);
39360Sstevel@tonic-gate 	caddr_t a = lpgaddr;
39370Sstevel@tonic-gate 	ulong_t aindx = svd->anon_index + seg_page(seg, a);
39380Sstevel@tonic-gate 	struct vpage *vpage = (svd->vpage != NULL) ?
39390Sstevel@tonic-gate 	    &svd->vpage[seg_page(seg, a)] : NULL;
39400Sstevel@tonic-gate 	page_t **ppa;
39410Sstevel@tonic-gate 	uint_t	ppa_szc;
39420Sstevel@tonic-gate 	faultcode_t err;
39430Sstevel@tonic-gate 	int ierr;
39440Sstevel@tonic-gate 	uint_t protchk, prot, vpprot;
39450Sstevel@tonic-gate 	int i;
39460Sstevel@tonic-gate 	int hat_flag = (type == F_SOFTLOCK) ? HAT_LOAD_LOCK : HAT_LOAD;
39470Sstevel@tonic-gate 	anon_sync_obj_t cookie;
39480Sstevel@tonic-gate 
39490Sstevel@tonic-gate 	ASSERT(szc != 0);
39500Sstevel@tonic-gate 	ASSERT(amp != NULL);
39510Sstevel@tonic-gate 	ASSERT(enable_mbit_wa == 0); /* no mbit simulations with large pages */
39520Sstevel@tonic-gate 	ASSERT(!(svd->flags & MAP_NORESERVE));
39530Sstevel@tonic-gate 	ASSERT(type != F_SOFTUNLOCK);
39540Sstevel@tonic-gate 	ASSERT(segtype == MAP_PRIVATE);
39550Sstevel@tonic-gate 	ASSERT(IS_P2ALIGNED(a, maxpgsz));
39560Sstevel@tonic-gate 
39570Sstevel@tonic-gate 	ASSERT(SEGVN_LOCK_HELD(seg->s_as, &svd->lock));
39580Sstevel@tonic-gate 
39590Sstevel@tonic-gate 	VM_STAT_COND_ADD(type == F_SOFTLOCK, segvnvmstats.fltanpages[0]);
39600Sstevel@tonic-gate 	VM_STAT_COND_ADD(type != F_SOFTLOCK, segvnvmstats.fltanpages[1]);
39610Sstevel@tonic-gate 
39620Sstevel@tonic-gate 	if (svd->flags & MAP_TEXT) {
39630Sstevel@tonic-gate 		hat_flag |= HAT_LOAD_TEXT;
39640Sstevel@tonic-gate 	}
39650Sstevel@tonic-gate 
39660Sstevel@tonic-gate 	if (svd->pageprot) {
39670Sstevel@tonic-gate 		switch (rw) {
39680Sstevel@tonic-gate 		case S_READ:
39690Sstevel@tonic-gate 			protchk = PROT_READ;
39700Sstevel@tonic-gate 			break;
39710Sstevel@tonic-gate 		case S_WRITE:
39720Sstevel@tonic-gate 			protchk = PROT_WRITE;
39730Sstevel@tonic-gate 			break;
39740Sstevel@tonic-gate 		case S_EXEC:
39750Sstevel@tonic-gate 			protchk = PROT_EXEC;
39760Sstevel@tonic-gate 			break;
39770Sstevel@tonic-gate 		case S_OTHER:
39780Sstevel@tonic-gate 		default:
39790Sstevel@tonic-gate 			protchk = PROT_READ | PROT_WRITE | PROT_EXEC;
39800Sstevel@tonic-gate 			break;
39810Sstevel@tonic-gate 		}
39820Sstevel@tonic-gate 		VM_STAT_ADD(segvnvmstats.fltanpages[2]);
39830Sstevel@tonic-gate 	} else {
39840Sstevel@tonic-gate 		prot = svd->prot;
39850Sstevel@tonic-gate 		/* caller has already done segment level protection check. */
39860Sstevel@tonic-gate 	}
39870Sstevel@tonic-gate 
39880Sstevel@tonic-gate 	ppa = kmem_alloc(ppasize, KM_SLEEP);
39890Sstevel@tonic-gate 	ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
39900Sstevel@tonic-gate 	for (;;) {
39910Sstevel@tonic-gate 		for (; a < lpgeaddr; a += pgsz, aindx += pages) {
39920Sstevel@tonic-gate 			if (svd->pageprot != 0 && IS_P2ALIGNED(a, maxpgsz)) {
39930Sstevel@tonic-gate 				VM_STAT_ADD(segvnvmstats.fltanpages[3]);
39940Sstevel@tonic-gate 				ASSERT(vpage != NULL);
39950Sstevel@tonic-gate 				prot = VPP_PROT(vpage);
39960Sstevel@tonic-gate 				ASSERT(sameprot(seg, a, maxpgsz));
39970Sstevel@tonic-gate 				if ((prot & protchk) == 0) {
39980Sstevel@tonic-gate 					err = FC_PROT;
39990Sstevel@tonic-gate 					goto error;
40000Sstevel@tonic-gate 				}
40010Sstevel@tonic-gate 			}
40020Sstevel@tonic-gate 			if (type == F_SOFTLOCK) {
40030Sstevel@tonic-gate 				mutex_enter(&freemem_lock);
40040Sstevel@tonic-gate 				if (availrmem < tune.t_minarmem + pages) {
40050Sstevel@tonic-gate 					mutex_exit(&freemem_lock);
40060Sstevel@tonic-gate 					err = FC_MAKE_ERR(ENOMEM);
40070Sstevel@tonic-gate 					goto error;
40080Sstevel@tonic-gate 				} else {
40090Sstevel@tonic-gate 					availrmem -= pages;
40100Sstevel@tonic-gate 					segvn_pages_locked += pages;
40110Sstevel@tonic-gate 					svd->softlockcnt += pages;
40120Sstevel@tonic-gate 				}
40130Sstevel@tonic-gate 				mutex_exit(&freemem_lock);
40140Sstevel@tonic-gate 			}
40150Sstevel@tonic-gate 			anon_array_enter(amp, aindx, &cookie);
40160Sstevel@tonic-gate 			ppa_szc = (uint_t)-1;
40170Sstevel@tonic-gate 			ierr = anon_map_getpages(amp, aindx, szc, seg, a,
40180Sstevel@tonic-gate 				prot, &vpprot, ppa, &ppa_szc, vpage, rw, brkcow,
40190Sstevel@tonic-gate 				segvn_anypgsz, svd->cred);
40200Sstevel@tonic-gate 			if (ierr != 0) {
40210Sstevel@tonic-gate 				anon_array_exit(&cookie);
40220Sstevel@tonic-gate 				VM_STAT_ADD(segvnvmstats.fltanpages[4]);
40230Sstevel@tonic-gate 				if (type == F_SOFTLOCK) {
40240Sstevel@tonic-gate 					VM_STAT_ADD(segvnvmstats.fltanpages[5]);
40250Sstevel@tonic-gate 					mutex_enter(&freemem_lock);
40260Sstevel@tonic-gate 					availrmem += pages;
40270Sstevel@tonic-gate 					segvn_pages_locked -= pages;
40280Sstevel@tonic-gate 					svd->softlockcnt -= pages;
40290Sstevel@tonic-gate 					mutex_exit(&freemem_lock);
40300Sstevel@tonic-gate 				}
40310Sstevel@tonic-gate 				if (ierr > 0) {
40320Sstevel@tonic-gate 					VM_STAT_ADD(segvnvmstats.fltanpages[6]);
40330Sstevel@tonic-gate 					err = FC_MAKE_ERR(ierr);
40340Sstevel@tonic-gate 					goto error;
40350Sstevel@tonic-gate 				}
40360Sstevel@tonic-gate 				break;
40370Sstevel@tonic-gate 			}
40380Sstevel@tonic-gate 
40390Sstevel@tonic-gate 			ASSERT(!IS_VMODSORT(ppa[0]->p_vnode));
40400Sstevel@tonic-gate 
40410Sstevel@tonic-gate 			/*
40420Sstevel@tonic-gate 			 * Handle pages that have been marked for migration
40430Sstevel@tonic-gate 			 */
40440Sstevel@tonic-gate 			if (lgrp_optimizations())
40450Sstevel@tonic-gate 				page_migrate(seg, a, ppa, pages);
40460Sstevel@tonic-gate 
40470Sstevel@tonic-gate 			hat_memload_array(hat, a, pgsz, ppa,
40480Sstevel@tonic-gate 			    prot & vpprot, hat_flag);
40490Sstevel@tonic-gate 
40500Sstevel@tonic-gate 			if (hat_flag & HAT_LOAD_LOCK) {
40510Sstevel@tonic-gate 				VM_STAT_ADD(segvnvmstats.fltanpages[7]);
40520Sstevel@tonic-gate 			} else {
40530Sstevel@tonic-gate 				VM_STAT_ADD(segvnvmstats.fltanpages[8]);
40540Sstevel@tonic-gate 				for (i = 0; i < pages; i++)
40550Sstevel@tonic-gate 					page_unlock(ppa[i]);
40560Sstevel@tonic-gate 			}
40570Sstevel@tonic-gate 			if (vpage != NULL)
40580Sstevel@tonic-gate 				vpage += pages;
40590Sstevel@tonic-gate 
40600Sstevel@tonic-gate 			anon_array_exit(&cookie);
40610Sstevel@tonic-gate 		}
40620Sstevel@tonic-gate 		if (a == lpgeaddr)
40630Sstevel@tonic-gate 			break;
40640Sstevel@tonic-gate 		ASSERT(a < lpgeaddr);
40650Sstevel@tonic-gate 		/*
40660Sstevel@tonic-gate 		 * ierr == -1 means we failed to allocate a large page.
40670Sstevel@tonic-gate 		 * so do a size down operation.
40680Sstevel@tonic-gate 		 *
40690Sstevel@tonic-gate 		 * ierr == -2 means some other process that privately shares
40700Sstevel@tonic-gate 		 * pages with this process has allocated a larger page and we
40710Sstevel@tonic-gate 		 * need to retry with larger pages. So do a size up
40720Sstevel@tonic-gate 		 * operation. This relies on the fact that large pages are
40730Sstevel@tonic-gate 		 * never partially shared i.e. if we share any constituent
40740Sstevel@tonic-gate 		 * page of a large page with another process we must share the
40750Sstevel@tonic-gate 		 * entire large page. Note this cannot happen for SOFTLOCK
40760Sstevel@tonic-gate 		 * case, unless current address (a) is at the beginning of the
40770Sstevel@tonic-gate 		 * next page size boundary because the other process couldn't
40780Sstevel@tonic-gate 		 * have relocated locked pages.
40790Sstevel@tonic-gate 		 */
40800Sstevel@tonic-gate 		ASSERT(ierr == -1 || ierr == -2);
40810Sstevel@tonic-gate 		if (segvn_anypgsz) {
40820Sstevel@tonic-gate 			ASSERT(ierr == -2 || szc != 0);
40830Sstevel@tonic-gate 			ASSERT(ierr == -1 || szc < seg->s_szc);
40840Sstevel@tonic-gate 			szc = (ierr == -1) ? szc - 1 : szc + 1;
40850Sstevel@tonic-gate 		} else {
40860Sstevel@tonic-gate 			/*
40870Sstevel@tonic-gate 			 * For non COW faults and segvn_anypgsz == 0
40880Sstevel@tonic-gate 			 * we need to be careful not to loop forever
40890Sstevel@tonic-gate 			 * if existing page is found with szc other
40900Sstevel@tonic-gate 			 * than 0 or seg->s_szc. This could be due
40910Sstevel@tonic-gate 			 * to page relocations on behalf of DR or
40920Sstevel@tonic-gate 			 * more likely large page creation. For this
40930Sstevel@tonic-gate 			 * case simply re-size to existing page's szc
40940Sstevel@tonic-gate 			 * if returned by anon_map_getpages().
40950Sstevel@tonic-gate 			 */
40960Sstevel@tonic-gate 			if (ppa_szc == (uint_t)-1) {
40970Sstevel@tonic-gate 				szc = (ierr == -1) ? 0 : seg->s_szc;
40980Sstevel@tonic-gate 			} else {
40990Sstevel@tonic-gate 				ASSERT(ppa_szc <= seg->s_szc);
41000Sstevel@tonic-gate 				ASSERT(ierr == -2 || ppa_szc < szc);
41010Sstevel@tonic-gate 				ASSERT(ierr == -1 || ppa_szc > szc);
41020Sstevel@tonic-gate 				szc = ppa_szc;
41030Sstevel@tonic-gate 			}
41040Sstevel@tonic-gate 		}
41050Sstevel@tonic-gate 
41060Sstevel@tonic-gate 		pgsz = page_get_pagesize(szc);
41070Sstevel@tonic-gate 		pages = btop(pgsz);
41080Sstevel@tonic-gate 		ASSERT(type != F_SOFTLOCK || ierr == -1 ||
41090Sstevel@tonic-gate 		    (IS_P2ALIGNED(a, pgsz) && IS_P2ALIGNED(lpgeaddr, pgsz)));
41100Sstevel@tonic-gate 		if (type == F_SOFTLOCK) {
41110Sstevel@tonic-gate 			/*
41120Sstevel@tonic-gate 			 * For softlocks we cannot reduce the fault area
41130Sstevel@tonic-gate 			 * (calculated based on the largest page size for this
41140Sstevel@tonic-gate 			 * segment) for size down and a is already next
41150Sstevel@tonic-gate 			 * page size aligned as assertted above for size
41160Sstevel@tonic-gate 			 * ups. Therefore just continue in case of softlock.
41170Sstevel@tonic-gate 			 */
41180Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fltanpages[9]);
41190Sstevel@tonic-gate 			continue; /* keep lint happy */
41200Sstevel@tonic-gate 		} else if (ierr == -2) {
41210Sstevel@tonic-gate 
41220Sstevel@tonic-gate 			/*
41230Sstevel@tonic-gate 			 * Size up case. Note lpgaddr may only be needed for
41240Sstevel@tonic-gate 			 * softlock case so we don't adjust it here.
41250Sstevel@tonic-gate 			 */
41260Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fltanpages[10]);
41270Sstevel@tonic-gate 			a = (caddr_t)P2ALIGN((uintptr_t)a, pgsz);
41280Sstevel@tonic-gate 			ASSERT(a >= lpgaddr);
41290Sstevel@tonic-gate 			lpgeaddr = (caddr_t)P2ROUNDUP((uintptr_t)eaddr, pgsz);
41300Sstevel@tonic-gate 			aindx = svd->anon_index + seg_page(seg, a);
41310Sstevel@tonic-gate 			vpage = (svd->vpage != NULL) ?
41320Sstevel@tonic-gate 			    &svd->vpage[seg_page(seg, a)] : NULL;
41330Sstevel@tonic-gate 		} else {
41340Sstevel@tonic-gate 			/*
41350Sstevel@tonic-gate 			 * Size down case. Note lpgaddr may only be needed for
41360Sstevel@tonic-gate 			 * softlock case so we don't adjust it here.
41370Sstevel@tonic-gate 			 */
41380Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fltanpages[11]);
41390Sstevel@tonic-gate 			ASSERT(IS_P2ALIGNED(a, pgsz));
41400Sstevel@tonic-gate 			ASSERT(IS_P2ALIGNED(lpgeaddr, pgsz));
41410Sstevel@tonic-gate 			lpgeaddr = (caddr_t)P2ROUNDUP((uintptr_t)eaddr, pgsz);
41420Sstevel@tonic-gate 			ASSERT(a < lpgeaddr);
41430Sstevel@tonic-gate 			if (a < addr) {
41440Sstevel@tonic-gate 				/*
41450Sstevel@tonic-gate 				 * The beginning of the large page region can
41460Sstevel@tonic-gate 				 * be pulled to the right to make a smaller
41470Sstevel@tonic-gate 				 * region. We haven't yet faulted a single
41480Sstevel@tonic-gate 				 * page.
41490Sstevel@tonic-gate 				 */
41500Sstevel@tonic-gate 				VM_STAT_ADD(segvnvmstats.fltanpages[12]);
41510Sstevel@tonic-gate 				a = (caddr_t)P2ALIGN((uintptr_t)addr, pgsz);
41520Sstevel@tonic-gate 				ASSERT(a >= lpgaddr);
41530Sstevel@tonic-gate 				aindx = svd->anon_index + seg_page(seg, a);
41540Sstevel@tonic-gate 				vpage = (svd->vpage != NULL) ?
41550Sstevel@tonic-gate 				    &svd->vpage[seg_page(seg, a)] : NULL;
41560Sstevel@tonic-gate 			}
41570Sstevel@tonic-gate 		}
41580Sstevel@tonic-gate 	}
41590Sstevel@tonic-gate 	VM_STAT_ADD(segvnvmstats.fltanpages[13]);
41600Sstevel@tonic-gate 	ANON_LOCK_EXIT(&amp->a_rwlock);
41610Sstevel@tonic-gate 	kmem_free(ppa, ppasize);
41620Sstevel@tonic-gate 	return (0);
41630Sstevel@tonic-gate error:
41640Sstevel@tonic-gate 	VM_STAT_ADD(segvnvmstats.fltanpages[14]);
41650Sstevel@tonic-gate 	ANON_LOCK_EXIT(&amp->a_rwlock);
41660Sstevel@tonic-gate 	kmem_free(ppa, ppasize);
41670Sstevel@tonic-gate 	if (type == F_SOFTLOCK && a > lpgaddr) {
41680Sstevel@tonic-gate 		VM_STAT_ADD(segvnvmstats.fltanpages[15]);
41690Sstevel@tonic-gate 		segvn_softunlock(seg, lpgaddr, a - lpgaddr, S_OTHER);
41700Sstevel@tonic-gate 	}
41710Sstevel@tonic-gate 	return (err);
41720Sstevel@tonic-gate }
41730Sstevel@tonic-gate 
41740Sstevel@tonic-gate int fltadvice = 1;	/* set to free behind pages for sequential access */
41750Sstevel@tonic-gate 
41760Sstevel@tonic-gate /*
41770Sstevel@tonic-gate  * This routine is called via a machine specific fault handling routine.
41780Sstevel@tonic-gate  * It is also called by software routines wishing to lock or unlock
41790Sstevel@tonic-gate  * a range of addresses.
41800Sstevel@tonic-gate  *
41810Sstevel@tonic-gate  * Here is the basic algorithm:
41820Sstevel@tonic-gate  *	If unlocking
41830Sstevel@tonic-gate  *		Call segvn_softunlock
41840Sstevel@tonic-gate  *		Return
41850Sstevel@tonic-gate  *	endif
41860Sstevel@tonic-gate  *	Checking and set up work
41870Sstevel@tonic-gate  *	If we will need some non-anonymous pages
41880Sstevel@tonic-gate  *		Call VOP_GETPAGE over the range of non-anonymous pages
41890Sstevel@tonic-gate  *	endif
41900Sstevel@tonic-gate  *	Loop over all addresses requested
41910Sstevel@tonic-gate  *		Call segvn_faultpage passing in page list
41920Sstevel@tonic-gate  *		    to load up translations and handle anonymous pages
41930Sstevel@tonic-gate  *	endloop
41940Sstevel@tonic-gate  *	Load up translation to any additional pages in page list not
41950Sstevel@tonic-gate  *	    already handled that fit into this segment
41960Sstevel@tonic-gate  */
41970Sstevel@tonic-gate static faultcode_t
41980Sstevel@tonic-gate segvn_fault(struct hat *hat, struct seg *seg, caddr_t addr, size_t len,
41990Sstevel@tonic-gate     enum fault_type type, enum seg_rw rw)
42000Sstevel@tonic-gate {
42010Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
42020Sstevel@tonic-gate 	page_t **plp, **ppp, *pp;
42030Sstevel@tonic-gate 	u_offset_t off;
42040Sstevel@tonic-gate 	caddr_t a;
42050Sstevel@tonic-gate 	struct vpage *vpage;
42060Sstevel@tonic-gate 	uint_t vpprot, prot;
42070Sstevel@tonic-gate 	int err;
42080Sstevel@tonic-gate 	page_t *pl[PVN_GETPAGE_NUM + 1];
42090Sstevel@tonic-gate 	size_t plsz, pl_alloc_sz;
42100Sstevel@tonic-gate 	size_t page;
42110Sstevel@tonic-gate 	ulong_t anon_index;
42120Sstevel@tonic-gate 	struct anon_map *amp;
42130Sstevel@tonic-gate 	int dogetpage = 0;
42140Sstevel@tonic-gate 	caddr_t	lpgaddr, lpgeaddr;
42150Sstevel@tonic-gate 	size_t pgsz;
42160Sstevel@tonic-gate 	anon_sync_obj_t cookie;
42170Sstevel@tonic-gate 	int brkcow = BREAK_COW_SHARE(rw, type, svd->type);
42180Sstevel@tonic-gate 
42190Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
42200Sstevel@tonic-gate 
42210Sstevel@tonic-gate 	/*
42220Sstevel@tonic-gate 	 * First handle the easy stuff
42230Sstevel@tonic-gate 	 */
42240Sstevel@tonic-gate 	if (type == F_SOFTUNLOCK) {
422563Saguzovsk 		if (rw == S_READ_NOCOW) {
422663Saguzovsk 			rw = S_READ;
422763Saguzovsk 			ASSERT(AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
422863Saguzovsk 		}
42290Sstevel@tonic-gate 		SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
42300Sstevel@tonic-gate 		pgsz = (seg->s_szc == 0) ? PAGESIZE :
42310Sstevel@tonic-gate 		    page_get_pagesize(seg->s_szc);
42320Sstevel@tonic-gate 		VM_STAT_COND_ADD(pgsz > PAGESIZE, segvnvmstats.fltanpages[16]);
42330Sstevel@tonic-gate 		CALC_LPG_REGION(pgsz, seg, addr, len, lpgaddr, lpgeaddr);
42340Sstevel@tonic-gate 		segvn_softunlock(seg, lpgaddr, lpgeaddr - lpgaddr, rw);
42350Sstevel@tonic-gate 		SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
42360Sstevel@tonic-gate 		return (0);
42370Sstevel@tonic-gate 	}
42380Sstevel@tonic-gate 
42390Sstevel@tonic-gate top:
42400Sstevel@tonic-gate 	SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
42410Sstevel@tonic-gate 
42420Sstevel@tonic-gate 	/*
42430Sstevel@tonic-gate 	 * If we have the same protections for the entire segment,
42440Sstevel@tonic-gate 	 * insure that the access being attempted is legitimate.
42450Sstevel@tonic-gate 	 */
42460Sstevel@tonic-gate 
42470Sstevel@tonic-gate 	if (svd->pageprot == 0) {
42480Sstevel@tonic-gate 		uint_t protchk;
42490Sstevel@tonic-gate 
42500Sstevel@tonic-gate 		switch (rw) {
42510Sstevel@tonic-gate 		case S_READ:
425263Saguzovsk 		case S_READ_NOCOW:
42530Sstevel@tonic-gate 			protchk = PROT_READ;
42540Sstevel@tonic-gate 			break;
42550Sstevel@tonic-gate 		case S_WRITE:
42560Sstevel@tonic-gate 			protchk = PROT_WRITE;
42570Sstevel@tonic-gate 			break;
42580Sstevel@tonic-gate 		case S_EXEC:
42590Sstevel@tonic-gate 			protchk = PROT_EXEC;
42600Sstevel@tonic-gate 			break;
42610Sstevel@tonic-gate 		case S_OTHER:
42620Sstevel@tonic-gate 		default:
42630Sstevel@tonic-gate 			protchk = PROT_READ | PROT_WRITE | PROT_EXEC;
42640Sstevel@tonic-gate 			break;
42650Sstevel@tonic-gate 		}
42660Sstevel@tonic-gate 
42670Sstevel@tonic-gate 		if ((svd->prot & protchk) == 0) {
42680Sstevel@tonic-gate 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
42690Sstevel@tonic-gate 			return (FC_PROT);	/* illegal access type */
42700Sstevel@tonic-gate 		}
42710Sstevel@tonic-gate 	}
42720Sstevel@tonic-gate 
42730Sstevel@tonic-gate 	/*
427463Saguzovsk 	 * We can't allow the long term use of softlocks for vmpss segments,
427563Saguzovsk 	 * because in some file truncation cases we should be able to demote
427663Saguzovsk 	 * the segment, which requires that there are no softlocks.  The
427763Saguzovsk 	 * only case where it's ok to allow a SOFTLOCK fault against a vmpss
427863Saguzovsk 	 * segment is S_READ_NOCOW, where the caller holds the address space
427963Saguzovsk 	 * locked as writer and calls softunlock before dropping the as lock.
428063Saguzovsk 	 * S_READ_NOCOW is used by /proc to read memory from another user.
428163Saguzovsk 	 *
428263Saguzovsk 	 * Another deadlock between SOFTLOCK and file truncation can happen
428363Saguzovsk 	 * because segvn_fault_vnodepages() calls the FS one pagesize at
428463Saguzovsk 	 * a time. A second VOP_GETPAGE() call by segvn_fault_vnodepages()
428563Saguzovsk 	 * can cause a deadlock because the first set of page_t's remain
428663Saguzovsk 	 * locked SE_SHARED.  To avoid this, we demote segments on a first
428763Saguzovsk 	 * SOFTLOCK if they have a length greater than the segment's
428863Saguzovsk 	 * page size.
428963Saguzovsk 	 *
429063Saguzovsk 	 * So for now, we only avoid demoting a segment on a SOFTLOCK when
429163Saguzovsk 	 * the access type is S_READ_NOCOW and the fault length is less than
429263Saguzovsk 	 * or equal to the segment's page size. While this is quite restrictive,
429363Saguzovsk 	 * it should be the most common case of SOFTLOCK against a vmpss
429463Saguzovsk 	 * segment.
429563Saguzovsk 	 *
429663Saguzovsk 	 * For S_READ_NOCOW, it's safe not to do a copy on write because the
429763Saguzovsk 	 * caller makes sure no COW will be caused by another thread for a
429863Saguzovsk 	 * softlocked page.
429963Saguzovsk 	 */
430063Saguzovsk 	if (type == F_SOFTLOCK && svd->vp != NULL && seg->s_szc != 0) {
430163Saguzovsk 		int demote = 0;
430263Saguzovsk 
430363Saguzovsk 		if (rw != S_READ_NOCOW) {
430463Saguzovsk 			demote = 1;
430563Saguzovsk 		}
430663Saguzovsk 		if (!demote && len > PAGESIZE) {
430763Saguzovsk 			pgsz = page_get_pagesize(seg->s_szc);
430863Saguzovsk 			CALC_LPG_REGION(pgsz, seg, addr, len, lpgaddr,
430963Saguzovsk 			    lpgeaddr);
431063Saguzovsk 			if (lpgeaddr - lpgaddr > pgsz) {
431163Saguzovsk 				demote = 1;
431263Saguzovsk 			}
431363Saguzovsk 		}
431463Saguzovsk 
431563Saguzovsk 		ASSERT(demote || AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
431663Saguzovsk 
431763Saguzovsk 		if (demote) {
431863Saguzovsk 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
431963Saguzovsk 			SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_WRITER);
432063Saguzovsk 			if (seg->s_szc != 0) {
432163Saguzovsk 				segvn_vmpss_clrszc_cnt++;
432263Saguzovsk 				ASSERT(svd->softlockcnt == 0);
432363Saguzovsk 				err = segvn_clrszc(seg);
432463Saguzovsk 				if (err) {
432563Saguzovsk 					segvn_vmpss_clrszc_err++;
432663Saguzovsk 					SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
432763Saguzovsk 					return (FC_MAKE_ERR(err));
432863Saguzovsk 				}
432963Saguzovsk 			}
433063Saguzovsk 			ASSERT(seg->s_szc == 0);
433163Saguzovsk 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
433263Saguzovsk 			goto top;
433363Saguzovsk 		}
433463Saguzovsk 	}
433563Saguzovsk 
433663Saguzovsk 	/*
43370Sstevel@tonic-gate 	 * Check to see if we need to allocate an anon_map structure.
43380Sstevel@tonic-gate 	 */
43390Sstevel@tonic-gate 	if (svd->amp == NULL && (svd->vp == NULL || brkcow)) {
43400Sstevel@tonic-gate 		/*
43410Sstevel@tonic-gate 		 * Drop the "read" lock on the segment and acquire
43420Sstevel@tonic-gate 		 * the "write" version since we have to allocate the
43430Sstevel@tonic-gate 		 * anon_map.
43440Sstevel@tonic-gate 		 */
43450Sstevel@tonic-gate 		SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
43460Sstevel@tonic-gate 		SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_WRITER);
43470Sstevel@tonic-gate 
43480Sstevel@tonic-gate 		if (svd->amp == NULL) {
43490Sstevel@tonic-gate 			svd->amp = anonmap_alloc(seg->s_size, 0);
43500Sstevel@tonic-gate 			svd->amp->a_szc = seg->s_szc;
43510Sstevel@tonic-gate 		}
43520Sstevel@tonic-gate 		SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
43530Sstevel@tonic-gate 
43540Sstevel@tonic-gate 		/*
43550Sstevel@tonic-gate 		 * Start all over again since segment protections
43560Sstevel@tonic-gate 		 * may have changed after we dropped the "read" lock.
43570Sstevel@tonic-gate 		 */
43580Sstevel@tonic-gate 		goto top;
43590Sstevel@tonic-gate 	}
43600Sstevel@tonic-gate 
4361329Saguzovsk 	/*
4362329Saguzovsk 	 * S_READ_NOCOW vs S_READ distinction was
4363329Saguzovsk 	 * only needed for the code above. After
4364329Saguzovsk 	 * that we treat it as S_READ.
4365329Saguzovsk 	 */
4366329Saguzovsk 	if (rw == S_READ_NOCOW) {
4367329Saguzovsk 		ASSERT(type == F_SOFTLOCK);
4368329Saguzovsk 		ASSERT(AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
4369329Saguzovsk 		rw = S_READ;
4370329Saguzovsk 	}
4371329Saguzovsk 
43720Sstevel@tonic-gate 	amp = svd->amp;
43730Sstevel@tonic-gate 
43740Sstevel@tonic-gate 	/*
43750Sstevel@tonic-gate 	 * MADV_SEQUENTIAL work is ignored for large page segments.
43760Sstevel@tonic-gate 	 */
43770Sstevel@tonic-gate 	if (seg->s_szc != 0) {
43780Sstevel@tonic-gate 		pgsz = page_get_pagesize(seg->s_szc);
43790Sstevel@tonic-gate 		ASSERT(SEGVN_LOCK_HELD(seg->s_as, &svd->lock));
43800Sstevel@tonic-gate 		/*
43810Sstevel@tonic-gate 		 * We may need to do relocations so purge seg_pcache to allow
43820Sstevel@tonic-gate 		 * pages to be locked exclusively.
43830Sstevel@tonic-gate 		 */
43840Sstevel@tonic-gate 		if (svd->softlockcnt != 0)
43850Sstevel@tonic-gate 			segvn_purge(seg);
43860Sstevel@tonic-gate 		CALC_LPG_REGION(pgsz, seg, addr, len, lpgaddr, lpgeaddr);
43870Sstevel@tonic-gate 		if (svd->vp == NULL) {
43880Sstevel@tonic-gate 			ASSERT(svd->type == MAP_PRIVATE);
43890Sstevel@tonic-gate 			err = segvn_fault_anonpages(hat, seg, lpgaddr,
43900Sstevel@tonic-gate 			    lpgeaddr, type, rw, addr, addr + len, brkcow);
43910Sstevel@tonic-gate 		} else {
43920Sstevel@tonic-gate 			err = segvn_fault_vnodepages(hat, seg, lpgaddr,
43930Sstevel@tonic-gate 				lpgeaddr, type, rw, addr, addr + len, brkcow);
43940Sstevel@tonic-gate 			if (err == IE_RETRY) {
43950Sstevel@tonic-gate 				ASSERT(seg->s_szc == 0);
43960Sstevel@tonic-gate 				ASSERT(SEGVN_READ_HELD(seg->s_as, &svd->lock));
439763Saguzovsk 				SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
439863Saguzovsk 				goto top;
43990Sstevel@tonic-gate 			}
44000Sstevel@tonic-gate 		}
44010Sstevel@tonic-gate 		SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
44020Sstevel@tonic-gate 		return (err);
44030Sstevel@tonic-gate 	}
44040Sstevel@tonic-gate 
44050Sstevel@tonic-gate 	page = seg_page(seg, addr);
44060Sstevel@tonic-gate 	if (amp != NULL) {
44070Sstevel@tonic-gate 		anon_index = svd->anon_index + page;
44080Sstevel@tonic-gate 
44090Sstevel@tonic-gate 		if ((type == F_PROT) && (rw == S_READ) &&
44100Sstevel@tonic-gate 		    svd->type == MAP_PRIVATE && svd->pageprot == 0) {
44110Sstevel@tonic-gate 			size_t index = anon_index;
44120Sstevel@tonic-gate 			struct anon *ap;
44130Sstevel@tonic-gate 
44140Sstevel@tonic-gate 			ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
44150Sstevel@tonic-gate 			/*
44160Sstevel@tonic-gate 			 * The fast path could apply to S_WRITE also, except
44170Sstevel@tonic-gate 			 * that the protection fault could be caused by lazy
44180Sstevel@tonic-gate 			 * tlb flush when ro->rw. In this case, the pte is
44190Sstevel@tonic-gate 			 * RW already. But RO in the other cpu's tlb causes
44200Sstevel@tonic-gate 			 * the fault. Since hat_chgprot won't do anything if
44210Sstevel@tonic-gate 			 * pte doesn't change, we may end up faulting
44220Sstevel@tonic-gate 			 * indefinitely until the RO tlb entry gets replaced.
44230Sstevel@tonic-gate 			 */
44240Sstevel@tonic-gate 			for (a = addr; a < addr + len; a += PAGESIZE, index++) {
44250Sstevel@tonic-gate 				anon_array_enter(amp, index, &cookie);
44260Sstevel@tonic-gate 				ap = anon_get_ptr(amp->ahp, index);
44270Sstevel@tonic-gate 				anon_array_exit(&cookie);
44280Sstevel@tonic-gate 				if ((ap == NULL) || (ap->an_refcnt != 1)) {
44290Sstevel@tonic-gate 					ANON_LOCK_EXIT(&amp->a_rwlock);
44300Sstevel@tonic-gate 					goto slow;
44310Sstevel@tonic-gate 				}
44320Sstevel@tonic-gate 			}
44330Sstevel@tonic-gate 			hat_chgprot(seg->s_as->a_hat, addr, len, svd->prot);
44340Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
44350Sstevel@tonic-gate 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
44360Sstevel@tonic-gate 			return (0);
44370Sstevel@tonic-gate 		}
44380Sstevel@tonic-gate 	}
44390Sstevel@tonic-gate slow:
44400Sstevel@tonic-gate 
44410Sstevel@tonic-gate 	if (svd->vpage == NULL)
44420Sstevel@tonic-gate 		vpage = NULL;
44430Sstevel@tonic-gate 	else
44440Sstevel@tonic-gate 		vpage = &svd->vpage[page];
44450Sstevel@tonic-gate 
44460Sstevel@tonic-gate 	off = svd->offset + (uintptr_t)(addr - seg->s_base);
44470Sstevel@tonic-gate 
44480Sstevel@tonic-gate 	/*
44490Sstevel@tonic-gate 	 * If MADV_SEQUENTIAL has been set for the particular page we
44500Sstevel@tonic-gate 	 * are faulting on, free behind all pages in the segment and put
44510Sstevel@tonic-gate 	 * them on the free list.
44520Sstevel@tonic-gate 	 */
44530Sstevel@tonic-gate 	if ((page != 0) && fltadvice) {	/* not if first page in segment */
44540Sstevel@tonic-gate 		struct vpage *vpp;
44550Sstevel@tonic-gate 		ulong_t fanon_index;
44560Sstevel@tonic-gate 		size_t fpage;
44570Sstevel@tonic-gate 		u_offset_t pgoff, fpgoff;
44580Sstevel@tonic-gate 		struct vnode *fvp;
44590Sstevel@tonic-gate 		struct anon *fap = NULL;
44600Sstevel@tonic-gate 
44610Sstevel@tonic-gate 		if (svd->advice == MADV_SEQUENTIAL ||
44620Sstevel@tonic-gate 		    (svd->pageadvice &&
44630Sstevel@tonic-gate 		    VPP_ADVICE(vpage) == MADV_SEQUENTIAL)) {
44640Sstevel@tonic-gate 			pgoff = off - PAGESIZE;
44650Sstevel@tonic-gate 			fpage = page - 1;
44660Sstevel@tonic-gate 			if (vpage != NULL)
44670Sstevel@tonic-gate 				vpp = &svd->vpage[fpage];
44680Sstevel@tonic-gate 			if (amp != NULL)
44690Sstevel@tonic-gate 				fanon_index = svd->anon_index + fpage;
44700Sstevel@tonic-gate 
44710Sstevel@tonic-gate 			while (pgoff > svd->offset) {
44720Sstevel@tonic-gate 				if (svd->advice != MADV_SEQUENTIAL &&
44730Sstevel@tonic-gate 				    (!svd->pageadvice || (vpage &&
44740Sstevel@tonic-gate 				    VPP_ADVICE(vpp) != MADV_SEQUENTIAL)))
44750Sstevel@tonic-gate 					break;
44760Sstevel@tonic-gate 
44770Sstevel@tonic-gate 				/*
44780Sstevel@tonic-gate 				 * If this is an anon page, we must find the
44790Sstevel@tonic-gate 				 * correct <vp, offset> for it
44800Sstevel@tonic-gate 				 */
44810Sstevel@tonic-gate 				fap = NULL;
44820Sstevel@tonic-gate 				if (amp != NULL) {
44830Sstevel@tonic-gate 					ANON_LOCK_ENTER(&amp->a_rwlock,
44840Sstevel@tonic-gate 						RW_READER);
44850Sstevel@tonic-gate 					anon_array_enter(amp, fanon_index,
44860Sstevel@tonic-gate 						&cookie);
44870Sstevel@tonic-gate 					fap = anon_get_ptr(amp->ahp,
44880Sstevel@tonic-gate 					    fanon_index);
44890Sstevel@tonic-gate 					if (fap != NULL) {
44900Sstevel@tonic-gate 						swap_xlate(fap, &fvp, &fpgoff);
44910Sstevel@tonic-gate 					} else {
44920Sstevel@tonic-gate 						fpgoff = pgoff;
44930Sstevel@tonic-gate 						fvp = svd->vp;
44940Sstevel@tonic-gate 					}
44950Sstevel@tonic-gate 					anon_array_exit(&cookie);
44960Sstevel@tonic-gate 					ANON_LOCK_EXIT(&amp->a_rwlock);
44970Sstevel@tonic-gate 				} else {
44980Sstevel@tonic-gate 					fpgoff = pgoff;
44990Sstevel@tonic-gate 					fvp = svd->vp;
45000Sstevel@tonic-gate 				}
45010Sstevel@tonic-gate 				if (fvp == NULL)
45020Sstevel@tonic-gate 					break;	/* XXX */
45030Sstevel@tonic-gate 				/*
45040Sstevel@tonic-gate 				 * Skip pages that are free or have an
45050Sstevel@tonic-gate 				 * "exclusive" lock.
45060Sstevel@tonic-gate 				 */
45070Sstevel@tonic-gate 				pp = page_lookup_nowait(fvp, fpgoff, SE_SHARED);
45080Sstevel@tonic-gate 				if (pp == NULL)
45090Sstevel@tonic-gate 					break;
45100Sstevel@tonic-gate 				/*
45110Sstevel@tonic-gate 				 * We don't need the page_struct_lock to test
45120Sstevel@tonic-gate 				 * as this is only advisory; even if we
45130Sstevel@tonic-gate 				 * acquire it someone might race in and lock
45140Sstevel@tonic-gate 				 * the page after we unlock and before the
45150Sstevel@tonic-gate 				 * PUTPAGE, then VOP_PUTPAGE will do nothing.
45160Sstevel@tonic-gate 				 */
45170Sstevel@tonic-gate 				if (pp->p_lckcnt == 0 && pp->p_cowcnt == 0) {
45180Sstevel@tonic-gate 					/*
45190Sstevel@tonic-gate 					 * Hold the vnode before releasing
45200Sstevel@tonic-gate 					 * the page lock to prevent it from
45210Sstevel@tonic-gate 					 * being freed and re-used by some
45220Sstevel@tonic-gate 					 * other thread.
45230Sstevel@tonic-gate 					 */
45240Sstevel@tonic-gate 					VN_HOLD(fvp);
45250Sstevel@tonic-gate 					page_unlock(pp);
45260Sstevel@tonic-gate 					/*
45270Sstevel@tonic-gate 					 * We should build a page list
45280Sstevel@tonic-gate 					 * to kluster putpages XXX
45290Sstevel@tonic-gate 					 */
45300Sstevel@tonic-gate 					(void) VOP_PUTPAGE(fvp,
45310Sstevel@tonic-gate 					    (offset_t)fpgoff, PAGESIZE,
45320Sstevel@tonic-gate 					    (B_DONTNEED|B_FREE|B_ASYNC),
45330Sstevel@tonic-gate 					    svd->cred);
45340Sstevel@tonic-gate 					VN_RELE(fvp);
45350Sstevel@tonic-gate 				} else {
45360Sstevel@tonic-gate 					/*
45370Sstevel@tonic-gate 					 * XXX - Should the loop terminate if
45380Sstevel@tonic-gate 					 * the page is `locked'?
45390Sstevel@tonic-gate 					 */
45400Sstevel@tonic-gate 					page_unlock(pp);
45410Sstevel@tonic-gate 				}
45420Sstevel@tonic-gate 				--vpp;
45430Sstevel@tonic-gate 				--fanon_index;
45440Sstevel@tonic-gate 				pgoff -= PAGESIZE;
45450Sstevel@tonic-gate 			}
45460Sstevel@tonic-gate 		}
45470Sstevel@tonic-gate 	}
45480Sstevel@tonic-gate 
45490Sstevel@tonic-gate 	plp = pl;
45500Sstevel@tonic-gate 	*plp = NULL;
45510Sstevel@tonic-gate 	pl_alloc_sz = 0;
45520Sstevel@tonic-gate 
45530Sstevel@tonic-gate 	/*
45540Sstevel@tonic-gate 	 * See if we need to call VOP_GETPAGE for
45550Sstevel@tonic-gate 	 * *any* of the range being faulted on.
45560Sstevel@tonic-gate 	 * We can skip all of this work if there
45570Sstevel@tonic-gate 	 * was no original vnode.
45580Sstevel@tonic-gate 	 */
45590Sstevel@tonic-gate 	if (svd->vp != NULL) {
45600Sstevel@tonic-gate 		u_offset_t vp_off;
45610Sstevel@tonic-gate 		size_t vp_len;
45620Sstevel@tonic-gate 		struct anon *ap;
45630Sstevel@tonic-gate 		vnode_t *vp;
45640Sstevel@tonic-gate 
45650Sstevel@tonic-gate 		vp_off = off;
45660Sstevel@tonic-gate 		vp_len = len;
45670Sstevel@tonic-gate 
45680Sstevel@tonic-gate 		if (amp == NULL)
45690Sstevel@tonic-gate 			dogetpage = 1;
45700Sstevel@tonic-gate 		else {
45710Sstevel@tonic-gate 			/*
45720Sstevel@tonic-gate 			 * Only acquire reader lock to prevent amp->ahp
45730Sstevel@tonic-gate 			 * from being changed.  It's ok to miss pages,
45740Sstevel@tonic-gate 			 * hence we don't do anon_array_enter
45750Sstevel@tonic-gate 			 */
45760Sstevel@tonic-gate 			ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
45770Sstevel@tonic-gate 			ap = anon_get_ptr(amp->ahp, anon_index);
45780Sstevel@tonic-gate 
45790Sstevel@tonic-gate 			if (len <= PAGESIZE)
45800Sstevel@tonic-gate 				/* inline non_anon() */
45810Sstevel@tonic-gate 				dogetpage = (ap == NULL);
45820Sstevel@tonic-gate 			else
45830Sstevel@tonic-gate 				dogetpage = non_anon(amp->ahp, anon_index,
45840Sstevel@tonic-gate 				    &vp_off, &vp_len);
45850Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
45860Sstevel@tonic-gate 		}
45870Sstevel@tonic-gate 
45880Sstevel@tonic-gate 		if (dogetpage) {
45890Sstevel@tonic-gate 			enum seg_rw arw;
45900Sstevel@tonic-gate 			struct as *as = seg->s_as;
45910Sstevel@tonic-gate 
45920Sstevel@tonic-gate 			if (len > ptob((sizeof (pl) / sizeof (pl[0])) - 1)) {
45930Sstevel@tonic-gate 				/*
45940Sstevel@tonic-gate 				 * Page list won't fit in local array,
45950Sstevel@tonic-gate 				 * allocate one of the needed size.
45960Sstevel@tonic-gate 				 */
45970Sstevel@tonic-gate 				pl_alloc_sz =
45980Sstevel@tonic-gate 				    (btop(len) + 1) * sizeof (page_t *);
45990Sstevel@tonic-gate 				plp = kmem_alloc(pl_alloc_sz, KM_SLEEP);
46000Sstevel@tonic-gate 				plp[0] = NULL;
46010Sstevel@tonic-gate 				plsz = len;
46020Sstevel@tonic-gate 			} else if (rw == S_WRITE && svd->type == MAP_PRIVATE ||
46030Sstevel@tonic-gate 			    rw == S_OTHER ||
46040Sstevel@tonic-gate 			    (((size_t)(addr + PAGESIZE) <
46050Sstevel@tonic-gate 			    (size_t)(seg->s_base + seg->s_size)) &&
46060Sstevel@tonic-gate 			    hat_probe(as->a_hat, addr + PAGESIZE))) {
46070Sstevel@tonic-gate 				/*
46080Sstevel@tonic-gate 				 * Ask VOP_GETPAGE to return the exact number
46090Sstevel@tonic-gate 				 * of pages if
46100Sstevel@tonic-gate 				 * (a) this is a COW fault, or
46110Sstevel@tonic-gate 				 * (b) this is a software fault, or
46120Sstevel@tonic-gate 				 * (c) next page is already mapped.
46130Sstevel@tonic-gate 				 */
46140Sstevel@tonic-gate 				plsz = len;
46150Sstevel@tonic-gate 			} else {
46160Sstevel@tonic-gate 				/*
46170Sstevel@tonic-gate 				 * Ask VOP_GETPAGE to return adjacent pages
46180Sstevel@tonic-gate 				 * within the segment.
46190Sstevel@tonic-gate 				 */
46200Sstevel@tonic-gate 				plsz = MIN((size_t)PVN_GETPAGE_SZ, (size_t)
46210Sstevel@tonic-gate 					((seg->s_base + seg->s_size) - addr));
46220Sstevel@tonic-gate 				ASSERT((addr + plsz) <=
46230Sstevel@tonic-gate 				    (seg->s_base + seg->s_size));
46240Sstevel@tonic-gate 			}
46250Sstevel@tonic-gate 
46260Sstevel@tonic-gate 			/*
46270Sstevel@tonic-gate 			 * Need to get some non-anonymous pages.
46280Sstevel@tonic-gate 			 * We need to make only one call to GETPAGE to do
46290Sstevel@tonic-gate 			 * this to prevent certain deadlocking conditions
46300Sstevel@tonic-gate 			 * when we are doing locking.  In this case
46310Sstevel@tonic-gate 			 * non_anon() should have picked up the smallest
46320Sstevel@tonic-gate 			 * range which includes all the non-anonymous
46330Sstevel@tonic-gate 			 * pages in the requested range.  We have to
46340Sstevel@tonic-gate 			 * be careful regarding which rw flag to pass in
46350Sstevel@tonic-gate 			 * because on a private mapping, the underlying
46360Sstevel@tonic-gate 			 * object is never allowed to be written.
46370Sstevel@tonic-gate 			 */
46380Sstevel@tonic-gate 			if (rw == S_WRITE && svd->type == MAP_PRIVATE) {
46390Sstevel@tonic-gate 				arw = S_READ;
46400Sstevel@tonic-gate 			} else {
46410Sstevel@tonic-gate 				arw = rw;
46420Sstevel@tonic-gate 			}
46430Sstevel@tonic-gate 			vp = svd->vp;
46440Sstevel@tonic-gate 			TRACE_3(TR_FAC_VM, TR_SEGVN_GETPAGE,
46450Sstevel@tonic-gate 				"segvn_getpage:seg %p addr %p vp %p",
46460Sstevel@tonic-gate 				seg, addr, vp);
46470Sstevel@tonic-gate 			err = VOP_GETPAGE(vp, (offset_t)vp_off, vp_len,
46480Sstevel@tonic-gate 			    &vpprot, plp, plsz, seg, addr + (vp_off - off), arw,
46490Sstevel@tonic-gate 			    svd->cred);
46500Sstevel@tonic-gate 			if (err) {
46510Sstevel@tonic-gate 				SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
46520Sstevel@tonic-gate 				segvn_pagelist_rele(plp);
46530Sstevel@tonic-gate 				if (pl_alloc_sz)
46540Sstevel@tonic-gate 					kmem_free(plp, pl_alloc_sz);
46550Sstevel@tonic-gate 				return (FC_MAKE_ERR(err));
46560Sstevel@tonic-gate 			}
46570Sstevel@tonic-gate 			if (svd->type == MAP_PRIVATE)
46580Sstevel@tonic-gate 				vpprot &= ~PROT_WRITE;
46590Sstevel@tonic-gate 		}
46600Sstevel@tonic-gate 	}
46610Sstevel@tonic-gate 
46620Sstevel@tonic-gate 	/*
46630Sstevel@tonic-gate 	 * N.B. at this time the plp array has all the needed non-anon
46640Sstevel@tonic-gate 	 * pages in addition to (possibly) having some adjacent pages.
46650Sstevel@tonic-gate 	 */
46660Sstevel@tonic-gate 
46670Sstevel@tonic-gate 	/*
46680Sstevel@tonic-gate 	 * Always acquire the anon_array_lock to prevent
46690Sstevel@tonic-gate 	 * 2 threads from allocating separate anon slots for
46700Sstevel@tonic-gate 	 * the same "addr".
46710Sstevel@tonic-gate 	 *
46720Sstevel@tonic-gate 	 * If this is a copy-on-write fault and we don't already
46730Sstevel@tonic-gate 	 * have the anon_array_lock, acquire it to prevent the
46740Sstevel@tonic-gate 	 * fault routine from handling multiple copy-on-write faults
46750Sstevel@tonic-gate 	 * on the same "addr" in the same address space.
46760Sstevel@tonic-gate 	 *
46770Sstevel@tonic-gate 	 * Only one thread should deal with the fault since after
46780Sstevel@tonic-gate 	 * it is handled, the other threads can acquire a translation
46790Sstevel@tonic-gate 	 * to the newly created private page.  This prevents two or
46800Sstevel@tonic-gate 	 * more threads from creating different private pages for the
46810Sstevel@tonic-gate 	 * same fault.
46820Sstevel@tonic-gate 	 *
46830Sstevel@tonic-gate 	 * We grab "serialization" lock here if this is a MAP_PRIVATE segment
46840Sstevel@tonic-gate 	 * to prevent deadlock between this thread and another thread
46850Sstevel@tonic-gate 	 * which has soft-locked this page and wants to acquire serial_lock.
46860Sstevel@tonic-gate 	 * ( bug 4026339 )
46870Sstevel@tonic-gate 	 *
46880Sstevel@tonic-gate 	 * The fix for bug 4026339 becomes unnecessary when using the
46890Sstevel@tonic-gate 	 * locking scheme with per amp rwlock and a global set of hash
46900Sstevel@tonic-gate 	 * lock, anon_array_lock.  If we steal a vnode page when low
46910Sstevel@tonic-gate 	 * on memory and upgrad the page lock through page_rename,
46920Sstevel@tonic-gate 	 * then the page is PAGE_HANDLED, nothing needs to be done
46930Sstevel@tonic-gate 	 * for this page after returning from segvn_faultpage.
46940Sstevel@tonic-gate 	 *
46950Sstevel@tonic-gate 	 * But really, the page lock should be downgraded after
46960Sstevel@tonic-gate 	 * the stolen page is page_rename'd.
46970Sstevel@tonic-gate 	 */
46980Sstevel@tonic-gate 
46990Sstevel@tonic-gate 	if (amp != NULL)
47000Sstevel@tonic-gate 		ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
47010Sstevel@tonic-gate 
47020Sstevel@tonic-gate 	/*
47030Sstevel@tonic-gate 	 * Ok, now loop over the address range and handle faults
47040Sstevel@tonic-gate 	 */
47050Sstevel@tonic-gate 	for (a = addr; a < addr + len; a += PAGESIZE, off += PAGESIZE) {
47060Sstevel@tonic-gate 		err = segvn_faultpage(hat, seg, a, off, vpage, plp, vpprot,
47070Sstevel@tonic-gate 		    type, rw, brkcow);
47080Sstevel@tonic-gate 		if (err) {
47090Sstevel@tonic-gate 			if (amp != NULL)
47100Sstevel@tonic-gate 				ANON_LOCK_EXIT(&amp->a_rwlock);
47110Sstevel@tonic-gate 			if (type == F_SOFTLOCK && a > addr)
47120Sstevel@tonic-gate 				segvn_softunlock(seg, addr, (a - addr),
47130Sstevel@tonic-gate 				    S_OTHER);
47140Sstevel@tonic-gate 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
47150Sstevel@tonic-gate 			segvn_pagelist_rele(plp);
47160Sstevel@tonic-gate 			if (pl_alloc_sz)
47170Sstevel@tonic-gate 				kmem_free(plp, pl_alloc_sz);
47180Sstevel@tonic-gate 			return (err);
47190Sstevel@tonic-gate 		}
47200Sstevel@tonic-gate 		if (vpage) {
47210Sstevel@tonic-gate 			vpage++;
47220Sstevel@tonic-gate 		} else if (svd->vpage) {
47230Sstevel@tonic-gate 			page = seg_page(seg, addr);
47240Sstevel@tonic-gate 			vpage = &svd->vpage[++page];
47250Sstevel@tonic-gate 		}
47260Sstevel@tonic-gate 	}
47270Sstevel@tonic-gate 
47280Sstevel@tonic-gate 	/* Didn't get pages from the underlying fs so we're done */
47290Sstevel@tonic-gate 	if (!dogetpage)
47300Sstevel@tonic-gate 		goto done;
47310Sstevel@tonic-gate 
47320Sstevel@tonic-gate 	/*
47330Sstevel@tonic-gate 	 * Now handle any other pages in the list returned.
47340Sstevel@tonic-gate 	 * If the page can be used, load up the translations now.
47350Sstevel@tonic-gate 	 * Note that the for loop will only be entered if "plp"
47360Sstevel@tonic-gate 	 * is pointing to a non-NULL page pointer which means that
47370Sstevel@tonic-gate 	 * VOP_GETPAGE() was called and vpprot has been initialized.
47380Sstevel@tonic-gate 	 */
47390Sstevel@tonic-gate 	if (svd->pageprot == 0)
47400Sstevel@tonic-gate 		prot = svd->prot & vpprot;
47410Sstevel@tonic-gate 
47420Sstevel@tonic-gate 
47430Sstevel@tonic-gate 	/*
47440Sstevel@tonic-gate 	 * Large Files: diff should be unsigned value because we started
47450Sstevel@tonic-gate 	 * supporting > 2GB segment sizes from 2.5.1 and when a
47460Sstevel@tonic-gate 	 * large file of size > 2GB gets mapped to address space
47470Sstevel@tonic-gate 	 * the diff value can be > 2GB.
47480Sstevel@tonic-gate 	 */
47490Sstevel@tonic-gate 
47500Sstevel@tonic-gate 	for (ppp = plp; (pp = *ppp) != NULL; ppp++) {
47510Sstevel@tonic-gate 		size_t diff;
47520Sstevel@tonic-gate 		struct anon *ap;
47530Sstevel@tonic-gate 		int anon_index;
47540Sstevel@tonic-gate 		anon_sync_obj_t cookie;
47550Sstevel@tonic-gate 		int hat_flag = HAT_LOAD_ADV;
47560Sstevel@tonic-gate 
47570Sstevel@tonic-gate 		if (svd->flags & MAP_TEXT) {
47580Sstevel@tonic-gate 			hat_flag |= HAT_LOAD_TEXT;
47590Sstevel@tonic-gate 		}
47600Sstevel@tonic-gate 
47610Sstevel@tonic-gate 		if (pp == PAGE_HANDLED)
47620Sstevel@tonic-gate 			continue;
47630Sstevel@tonic-gate 
47640Sstevel@tonic-gate 		if (pp->p_offset >=  svd->offset &&
47650Sstevel@tonic-gate 			(pp->p_offset < svd->offset + seg->s_size)) {
47660Sstevel@tonic-gate 
47670Sstevel@tonic-gate 			diff = pp->p_offset - svd->offset;
47680Sstevel@tonic-gate 
47690Sstevel@tonic-gate 			/*
47700Sstevel@tonic-gate 			 * Large Files: Following is the assertion
47710Sstevel@tonic-gate 			 * validating the above cast.
47720Sstevel@tonic-gate 			 */
47730Sstevel@tonic-gate 			ASSERT(svd->vp == pp->p_vnode);
47740Sstevel@tonic-gate 
47750Sstevel@tonic-gate 			page = btop(diff);
47760Sstevel@tonic-gate 			if (svd->pageprot)
47770Sstevel@tonic-gate 				prot = VPP_PROT(&svd->vpage[page]) & vpprot;
47780Sstevel@tonic-gate 
47790Sstevel@tonic-gate 			/*
47800Sstevel@tonic-gate 			 * Prevent other threads in the address space from
47810Sstevel@tonic-gate 			 * creating private pages (i.e., allocating anon slots)
47820Sstevel@tonic-gate 			 * while we are in the process of loading translations
47830Sstevel@tonic-gate 			 * to additional pages returned by the underlying
47840Sstevel@tonic-gate 			 * object.
47850Sstevel@tonic-gate 			 */
47860Sstevel@tonic-gate 			if (amp != NULL) {
47870Sstevel@tonic-gate 				anon_index = svd->anon_index + page;
47880Sstevel@tonic-gate 				anon_array_enter(amp, anon_index, &cookie);
47890Sstevel@tonic-gate 				ap = anon_get_ptr(amp->ahp, anon_index);
47900Sstevel@tonic-gate 			}
47910Sstevel@tonic-gate 			if ((amp == NULL) || (ap == NULL)) {
47920Sstevel@tonic-gate 				if (IS_VMODSORT(pp->p_vnode) ||
47930Sstevel@tonic-gate 				    enable_mbit_wa) {
47940Sstevel@tonic-gate 					if (rw == S_WRITE)
47950Sstevel@tonic-gate 						hat_setmod(pp);
47960Sstevel@tonic-gate 					else if (rw != S_OTHER &&
47970Sstevel@tonic-gate 					    !hat_ismod(pp))
47980Sstevel@tonic-gate 						prot &= ~PROT_WRITE;
47990Sstevel@tonic-gate 				}
48000Sstevel@tonic-gate 				/*
48010Sstevel@tonic-gate 				 * Skip mapping read ahead pages marked
48020Sstevel@tonic-gate 				 * for migration, so they will get migrated
48030Sstevel@tonic-gate 				 * properly on fault
48040Sstevel@tonic-gate 				 */
48050Sstevel@tonic-gate 				if ((prot & PROT_READ) && !PP_ISMIGRATE(pp)) {
48060Sstevel@tonic-gate 					hat_memload(hat, seg->s_base + diff,
48070Sstevel@tonic-gate 						pp, prot, hat_flag);
48080Sstevel@tonic-gate 				}
48090Sstevel@tonic-gate 			}
48100Sstevel@tonic-gate 			if (amp != NULL)
48110Sstevel@tonic-gate 				anon_array_exit(&cookie);
48120Sstevel@tonic-gate 		}
48130Sstevel@tonic-gate 		page_unlock(pp);
48140Sstevel@tonic-gate 	}
48150Sstevel@tonic-gate done:
48160Sstevel@tonic-gate 	if (amp != NULL)
48170Sstevel@tonic-gate 		ANON_LOCK_EXIT(&amp->a_rwlock);
48180Sstevel@tonic-gate 	SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
48190Sstevel@tonic-gate 	if (pl_alloc_sz)
48200Sstevel@tonic-gate 		kmem_free(plp, pl_alloc_sz);
48210Sstevel@tonic-gate 	return (0);
48220Sstevel@tonic-gate }
48230Sstevel@tonic-gate 
48240Sstevel@tonic-gate /*
48250Sstevel@tonic-gate  * This routine is used to start I/O on pages asynchronously.  XXX it will
48260Sstevel@tonic-gate  * only create PAGESIZE pages. At fault time they will be relocated into
48270Sstevel@tonic-gate  * larger pages.
48280Sstevel@tonic-gate  */
48290Sstevel@tonic-gate static faultcode_t
48300Sstevel@tonic-gate segvn_faulta(struct seg *seg, caddr_t addr)
48310Sstevel@tonic-gate {
48320Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
48330Sstevel@tonic-gate 	int err;
48340Sstevel@tonic-gate 	struct anon_map *amp;
48350Sstevel@tonic-gate 	vnode_t *vp;
48360Sstevel@tonic-gate 
48370Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
48380Sstevel@tonic-gate 
48390Sstevel@tonic-gate 	SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
48400Sstevel@tonic-gate 	if ((amp = svd->amp) != NULL) {
48410Sstevel@tonic-gate 		struct anon *ap;
48420Sstevel@tonic-gate 
48430Sstevel@tonic-gate 		/*
48440Sstevel@tonic-gate 		 * Reader lock to prevent amp->ahp from being changed.
48450Sstevel@tonic-gate 		 * This is advisory, it's ok to miss a page, so
48460Sstevel@tonic-gate 		 * we don't do anon_array_enter lock.
48470Sstevel@tonic-gate 		 */
48480Sstevel@tonic-gate 		ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
48490Sstevel@tonic-gate 		if ((ap = anon_get_ptr(amp->ahp,
48500Sstevel@tonic-gate 			svd->anon_index + seg_page(seg, addr))) != NULL) {
48510Sstevel@tonic-gate 
48520Sstevel@tonic-gate 			err = anon_getpage(&ap, NULL, NULL,
48530Sstevel@tonic-gate 			    0, seg, addr, S_READ, svd->cred);
48540Sstevel@tonic-gate 
48550Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
48560Sstevel@tonic-gate 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
48570Sstevel@tonic-gate 			if (err)
48580Sstevel@tonic-gate 				return (FC_MAKE_ERR(err));
48590Sstevel@tonic-gate 			return (0);
48600Sstevel@tonic-gate 		}
48610Sstevel@tonic-gate 		ANON_LOCK_EXIT(&amp->a_rwlock);
48620Sstevel@tonic-gate 	}
48630Sstevel@tonic-gate 
48640Sstevel@tonic-gate 	if (svd->vp == NULL) {
48650Sstevel@tonic-gate 		SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
48660Sstevel@tonic-gate 		return (0);			/* zfod page - do nothing now */
48670Sstevel@tonic-gate 	}
48680Sstevel@tonic-gate 
48690Sstevel@tonic-gate 	vp = svd->vp;
48700Sstevel@tonic-gate 	TRACE_3(TR_FAC_VM, TR_SEGVN_GETPAGE,
48710Sstevel@tonic-gate 		"segvn_getpage:seg %p addr %p vp %p", seg, addr, vp);
48720Sstevel@tonic-gate 	err = VOP_GETPAGE(vp,
48730Sstevel@tonic-gate 	    (offset_t)(svd->offset + (uintptr_t)(addr - seg->s_base)),
48740Sstevel@tonic-gate 	    PAGESIZE, NULL, NULL, 0, seg, addr,
48750Sstevel@tonic-gate 	    S_OTHER, svd->cred);
48760Sstevel@tonic-gate 
48770Sstevel@tonic-gate 	SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
48780Sstevel@tonic-gate 	if (err)
48790Sstevel@tonic-gate 		return (FC_MAKE_ERR(err));
48800Sstevel@tonic-gate 	return (0);
48810Sstevel@tonic-gate }
48820Sstevel@tonic-gate 
48830Sstevel@tonic-gate static int
48840Sstevel@tonic-gate segvn_setprot(struct seg *seg, caddr_t addr, size_t len, uint_t prot)
48850Sstevel@tonic-gate {
48860Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
48870Sstevel@tonic-gate 	struct vpage *svp, *evp;
48880Sstevel@tonic-gate 	struct vnode *vp;
48890Sstevel@tonic-gate 	size_t pgsz;
48900Sstevel@tonic-gate 	pgcnt_t pgcnt;
48910Sstevel@tonic-gate 	anon_sync_obj_t cookie;
48920Sstevel@tonic-gate 
48930Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
48940Sstevel@tonic-gate 
48950Sstevel@tonic-gate 	if ((svd->maxprot & prot) != prot)
48960Sstevel@tonic-gate 		return (EACCES);			/* violated maxprot */
48970Sstevel@tonic-gate 
48980Sstevel@tonic-gate 	SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_WRITER);
48990Sstevel@tonic-gate 
49000Sstevel@tonic-gate 	/* return if prot is the same */
49010Sstevel@tonic-gate 	if (!svd->pageprot && svd->prot == prot) {
49020Sstevel@tonic-gate 		SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
49030Sstevel@tonic-gate 		return (0);
49040Sstevel@tonic-gate 	}
49050Sstevel@tonic-gate 
49060Sstevel@tonic-gate 	/*
49070Sstevel@tonic-gate 	 * Since we change protections we first have to flush the cache.
49080Sstevel@tonic-gate 	 * This makes sure all the pagelock calls have to recheck
49090Sstevel@tonic-gate 	 * protections.
49100Sstevel@tonic-gate 	 */
49110Sstevel@tonic-gate 	if (svd->softlockcnt > 0) {
49120Sstevel@tonic-gate 		/*
49130Sstevel@tonic-gate 		 * Since we do have the segvn writers lock nobody can fill
49140Sstevel@tonic-gate 		 * the cache with entries belonging to this seg during
49150Sstevel@tonic-gate 		 * the purge. The flush either succeeds or we still have
49160Sstevel@tonic-gate 		 * pending I/Os.
49170Sstevel@tonic-gate 		 */
49180Sstevel@tonic-gate 		segvn_purge(seg);
49190Sstevel@tonic-gate 		if (svd->softlockcnt > 0) {
49200Sstevel@tonic-gate 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
49210Sstevel@tonic-gate 			return (EAGAIN);
49220Sstevel@tonic-gate 		}
49230Sstevel@tonic-gate 	}
49240Sstevel@tonic-gate 
49250Sstevel@tonic-gate 	if (seg->s_szc != 0) {
49260Sstevel@tonic-gate 		int err;
49270Sstevel@tonic-gate 		pgsz = page_get_pagesize(seg->s_szc);
49280Sstevel@tonic-gate 		pgcnt = pgsz >> PAGESHIFT;
49290Sstevel@tonic-gate 		ASSERT(IS_P2ALIGNED(pgcnt, pgcnt));
49300Sstevel@tonic-gate 		if (!IS_P2ALIGNED(addr, pgsz) || !IS_P2ALIGNED(len, pgsz)) {
49310Sstevel@tonic-gate 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
49320Sstevel@tonic-gate 			ASSERT(seg->s_base != addr || seg->s_size != len);
49330Sstevel@tonic-gate 			/*
49340Sstevel@tonic-gate 			 * If we are holding the as lock as a reader then
49350Sstevel@tonic-gate 			 * we need to return IE_RETRY and let the as
49360Sstevel@tonic-gate 			 * layer drop and re-aquire the lock as a writer.
49370Sstevel@tonic-gate 			 */
49380Sstevel@tonic-gate 			if (AS_READ_HELD(seg->s_as, &seg->s_as->a_lock))
49390Sstevel@tonic-gate 				return (IE_RETRY);
49400Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.demoterange[1]);
49410Sstevel@tonic-gate 			err = segvn_demote_range(seg, addr, len, SDR_END);
49420Sstevel@tonic-gate 			if (err == 0)
49430Sstevel@tonic-gate 				return (IE_RETRY);
49440Sstevel@tonic-gate 			if (err == ENOMEM)
49450Sstevel@tonic-gate 				return (IE_NOMEM);
49460Sstevel@tonic-gate 			return (err);
49470Sstevel@tonic-gate 		}
49480Sstevel@tonic-gate 	}
49490Sstevel@tonic-gate 
49500Sstevel@tonic-gate 
49510Sstevel@tonic-gate 	/*
49520Sstevel@tonic-gate 	 * If it's a private mapping and we're making it writable
49530Sstevel@tonic-gate 	 * and no swap space has been reserved, have to reserve
49540Sstevel@tonic-gate 	 * it all now.  If it's a private mapping to a file (i.e., vp != NULL)
49550Sstevel@tonic-gate 	 * and we're removing write permission on the entire segment and
49560Sstevel@tonic-gate 	 * we haven't modified any pages, we can release the swap space.
49570Sstevel@tonic-gate 	 */
49580Sstevel@tonic-gate 	if (svd->type == MAP_PRIVATE) {
49590Sstevel@tonic-gate 		if (prot & PROT_WRITE) {
49600Sstevel@tonic-gate 			size_t sz;
49610Sstevel@tonic-gate 			if (svd->swresv == 0 && !(svd->flags & MAP_NORESERVE)) {
49620Sstevel@tonic-gate 				if (anon_resv(seg->s_size) == 0) {
49630Sstevel@tonic-gate 					SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
49640Sstevel@tonic-gate 					return (IE_NOMEM);
49650Sstevel@tonic-gate 				}
49660Sstevel@tonic-gate 				sz = svd->swresv = seg->s_size;
49670Sstevel@tonic-gate 				TRACE_3(TR_FAC_VM, TR_ANON_PROC,
49680Sstevel@tonic-gate 					"anon proc:%p %lu %u",
49690Sstevel@tonic-gate 					seg, sz, 1);
49700Sstevel@tonic-gate 			}
49710Sstevel@tonic-gate 		} else {
49720Sstevel@tonic-gate 			/*
49730Sstevel@tonic-gate 			 * Swap space is released only if this segment
49740Sstevel@tonic-gate 			 * does not map anonymous memory, since read faults
49750Sstevel@tonic-gate 			 * on such segments still need an anon slot to read
49760Sstevel@tonic-gate 			 * in the data.
49770Sstevel@tonic-gate 			 */
49780Sstevel@tonic-gate 			if (svd->swresv != 0 && svd->vp != NULL &&
49790Sstevel@tonic-gate 			    svd->amp == NULL && addr == seg->s_base &&
49800Sstevel@tonic-gate 			    len == seg->s_size && svd->pageprot == 0) {
49810Sstevel@tonic-gate 				anon_unresv(svd->swresv);
49820Sstevel@tonic-gate 				svd->swresv = 0;
49830Sstevel@tonic-gate 				TRACE_3(TR_FAC_VM, TR_ANON_PROC,
49840Sstevel@tonic-gate 					"anon proc:%p %lu %u",
49850Sstevel@tonic-gate 					seg, 0, 0);
49860Sstevel@tonic-gate 			}
49870Sstevel@tonic-gate 		}
49880Sstevel@tonic-gate 	}
49890Sstevel@tonic-gate 
49900Sstevel@tonic-gate 	if (addr == seg->s_base && len == seg->s_size && svd->pageprot == 0) {
49910Sstevel@tonic-gate 		if (svd->prot == prot) {
49920Sstevel@tonic-gate 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
49930Sstevel@tonic-gate 			return (0);			/* all done */
49940Sstevel@tonic-gate 		}
49950Sstevel@tonic-gate 		svd->prot = (uchar_t)prot;
49960Sstevel@tonic-gate 	} else {
49970Sstevel@tonic-gate 		struct anon *ap = NULL;
49980Sstevel@tonic-gate 		page_t *pp;
49990Sstevel@tonic-gate 		u_offset_t offset, off;
50000Sstevel@tonic-gate 		struct anon_map *amp;
50010Sstevel@tonic-gate 		ulong_t anon_idx = 0;
50020Sstevel@tonic-gate 
50030Sstevel@tonic-gate 		/*
50040Sstevel@tonic-gate 		 * A vpage structure exists or else the change does not
50050Sstevel@tonic-gate 		 * involve the entire segment.  Establish a vpage structure
50060Sstevel@tonic-gate 		 * if none is there.  Then, for each page in the range,
50070Sstevel@tonic-gate 		 * adjust its individual permissions.  Note that write-
50080Sstevel@tonic-gate 		 * enabling a MAP_PRIVATE page can affect the claims for
50090Sstevel@tonic-gate 		 * locked down memory.  Overcommitting memory terminates
50100Sstevel@tonic-gate 		 * the operation.
50110Sstevel@tonic-gate 		 */
50120Sstevel@tonic-gate 		segvn_vpage(seg);
50130Sstevel@tonic-gate 		if ((amp = svd->amp) != NULL) {
50140Sstevel@tonic-gate 			anon_idx = svd->anon_index + seg_page(seg, addr);
50150Sstevel@tonic-gate 			ASSERT(seg->s_szc == 0 ||
50160Sstevel@tonic-gate 			    IS_P2ALIGNED(anon_idx, pgcnt));
50170Sstevel@tonic-gate 			ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
50180Sstevel@tonic-gate 		}
50190Sstevel@tonic-gate 
50200Sstevel@tonic-gate 		offset = svd->offset + (uintptr_t)(addr - seg->s_base);
50210Sstevel@tonic-gate 		evp = &svd->vpage[seg_page(seg, addr + len)];
50220Sstevel@tonic-gate 
50230Sstevel@tonic-gate 		/*
50240Sstevel@tonic-gate 		 * See Statement at the beginning of segvn_lockop regarding
50250Sstevel@tonic-gate 		 * the way cowcnts and lckcnts are handled.
50260Sstevel@tonic-gate 		 */
50270Sstevel@tonic-gate 		for (svp = &svd->vpage[seg_page(seg, addr)]; svp < evp; svp++) {
50280Sstevel@tonic-gate 
50290Sstevel@tonic-gate 			ASSERT(seg->s_szc == 0 ||
50300Sstevel@tonic-gate 			    (svd->vp != NULL || svd->type == MAP_PRIVATE));
50310Sstevel@tonic-gate 
50320Sstevel@tonic-gate 			if (seg->s_szc != 0 && svd->type == MAP_PRIVATE) {
50330Sstevel@tonic-gate 				if (amp != NULL) {
50340Sstevel@tonic-gate 					anon_array_enter(amp, anon_idx,
50350Sstevel@tonic-gate 					    &cookie);
50360Sstevel@tonic-gate 				}
50370Sstevel@tonic-gate 				if (IS_P2ALIGNED(anon_idx, pgcnt) &&
50380Sstevel@tonic-gate 				    !segvn_claim_pages(seg, svp, offset,
50390Sstevel@tonic-gate 					anon_idx, prot)) {
50400Sstevel@tonic-gate 					if (amp != NULL) {
50410Sstevel@tonic-gate 						anon_array_exit(&cookie);
50420Sstevel@tonic-gate 					}
50430Sstevel@tonic-gate 					break;
50440Sstevel@tonic-gate 				}
50450Sstevel@tonic-gate 				if (amp != NULL) {
50460Sstevel@tonic-gate 					anon_array_exit(&cookie);
50470Sstevel@tonic-gate 				}
50480Sstevel@tonic-gate 				anon_idx++;
50490Sstevel@tonic-gate 			} else {
50500Sstevel@tonic-gate 				if (amp != NULL) {
50510Sstevel@tonic-gate 					anon_array_enter(amp, anon_idx,
50520Sstevel@tonic-gate 						&cookie);
50530Sstevel@tonic-gate 					ap = anon_get_ptr(amp->ahp, anon_idx++);
50540Sstevel@tonic-gate 				}
50550Sstevel@tonic-gate 
50560Sstevel@tonic-gate 				if (VPP_ISPPLOCK(svp) &&
50570Sstevel@tonic-gate 				    (VPP_PROT(svp) != prot) &&
50580Sstevel@tonic-gate 				    (svd->type == MAP_PRIVATE)) {
50590Sstevel@tonic-gate 
50600Sstevel@tonic-gate 					if (amp == NULL || ap == NULL) {
50610Sstevel@tonic-gate 						vp = svd->vp;
50620Sstevel@tonic-gate 						off = offset;
50630Sstevel@tonic-gate 					} else
50640Sstevel@tonic-gate 						swap_xlate(ap, &vp, &off);
50650Sstevel@tonic-gate 					if (amp != NULL)
50660Sstevel@tonic-gate 						anon_array_exit(&cookie);
50670Sstevel@tonic-gate 
50680Sstevel@tonic-gate 					if ((pp = page_lookup(vp, off,
50690Sstevel@tonic-gate 					    SE_SHARED)) == NULL) {
50700Sstevel@tonic-gate 						panic("segvn_setprot: no page");
50710Sstevel@tonic-gate 						/*NOTREACHED*/
50720Sstevel@tonic-gate 					}
50730Sstevel@tonic-gate 					ASSERT(seg->s_szc == 0);
50740Sstevel@tonic-gate 					if ((VPP_PROT(svp) ^ prot) &
50750Sstevel@tonic-gate 					    PROT_WRITE) {
50760Sstevel@tonic-gate 						if (prot & PROT_WRITE) {
50770Sstevel@tonic-gate 						    if (!page_addclaim(pp)) {
50780Sstevel@tonic-gate 							page_unlock(pp);
50790Sstevel@tonic-gate 							break;
50800Sstevel@tonic-gate 						    }
50810Sstevel@tonic-gate 						} else {
50820Sstevel@tonic-gate 						    if (!page_subclaim(pp)) {
50830Sstevel@tonic-gate 							page_unlock(pp);
50840Sstevel@tonic-gate 							break;
50850Sstevel@tonic-gate 						    }
50860Sstevel@tonic-gate 						}
50870Sstevel@tonic-gate 					}
50880Sstevel@tonic-gate 					page_unlock(pp);
50890Sstevel@tonic-gate 				} else if (amp != NULL)
50900Sstevel@tonic-gate 					anon_array_exit(&cookie);
50910Sstevel@tonic-gate 			}
50920Sstevel@tonic-gate 			VPP_SETPROT(svp, prot);
50930Sstevel@tonic-gate 			offset += PAGESIZE;
50940Sstevel@tonic-gate 		}
50950Sstevel@tonic-gate 		if (amp != NULL)
50960Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
50970Sstevel@tonic-gate 
50980Sstevel@tonic-gate 		/*
50990Sstevel@tonic-gate 		 * Did we terminate prematurely?  If so, simply unload
51000Sstevel@tonic-gate 		 * the translations to the things we've updated so far.
51010Sstevel@tonic-gate 		 */
51020Sstevel@tonic-gate 		if (svp != evp) {
51030Sstevel@tonic-gate 			len = (svp - &svd->vpage[seg_page(seg, addr)]) *
51040Sstevel@tonic-gate 			    PAGESIZE;
51050Sstevel@tonic-gate 			ASSERT(seg->s_szc == 0 || IS_P2ALIGNED(len, pgsz));
51060Sstevel@tonic-gate 			if (len != 0)
51070Sstevel@tonic-gate 				hat_unload(seg->s_as->a_hat, addr,
51080Sstevel@tonic-gate 				    len, HAT_UNLOAD);
51090Sstevel@tonic-gate 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
51100Sstevel@tonic-gate 			return (IE_NOMEM);
51110Sstevel@tonic-gate 		}
51120Sstevel@tonic-gate 	}
51130Sstevel@tonic-gate 
51140Sstevel@tonic-gate 	if ((prot & PROT_WRITE) != 0 || (prot & ~PROT_USER) == PROT_NONE) {
51150Sstevel@tonic-gate 		/*
51160Sstevel@tonic-gate 		 * Either private or shared data with write access (in
51170Sstevel@tonic-gate 		 * which case we need to throw out all former translations
51180Sstevel@tonic-gate 		 * so that we get the right translations set up on fault
51190Sstevel@tonic-gate 		 * and we don't allow write access to any copy-on-write pages
51200Sstevel@tonic-gate 		 * that might be around or to prevent write access to pages
51210Sstevel@tonic-gate 		 * representing holes in a file), or we don't have permission
51220Sstevel@tonic-gate 		 * to access the memory at all (in which case we have to
51230Sstevel@tonic-gate 		 * unload any current translations that might exist).
51240Sstevel@tonic-gate 		 */
51250Sstevel@tonic-gate 		hat_unload(seg->s_as->a_hat, addr, len, HAT_UNLOAD);
51260Sstevel@tonic-gate 	} else {
51270Sstevel@tonic-gate 		/*
51280Sstevel@tonic-gate 		 * A shared mapping or a private mapping in which write
51290Sstevel@tonic-gate 		 * protection is going to be denied - just change all the
51300Sstevel@tonic-gate 		 * protections over the range of addresses in question.
51310Sstevel@tonic-gate 		 * segvn does not support any other attributes other
51320Sstevel@tonic-gate 		 * than prot so we can use hat_chgattr.
51330Sstevel@tonic-gate 		 */
51340Sstevel@tonic-gate 		hat_chgattr(seg->s_as->a_hat, addr, len, prot);
51350Sstevel@tonic-gate 	}
51360Sstevel@tonic-gate 
51370Sstevel@tonic-gate 	SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
51380Sstevel@tonic-gate 
51390Sstevel@tonic-gate 	return (0);
51400Sstevel@tonic-gate }
51410Sstevel@tonic-gate 
51420Sstevel@tonic-gate /*
51430Sstevel@tonic-gate  * segvn_setpagesize is called via SEGOP_SETPAGESIZE from as_setpagesize,
51440Sstevel@tonic-gate  * to determine if the seg is capable of mapping the requested szc.
51450Sstevel@tonic-gate  */
51460Sstevel@tonic-gate static int
51470Sstevel@tonic-gate segvn_setpagesize(struct seg *seg, caddr_t addr, size_t len, uint_t szc)
51480Sstevel@tonic-gate {
51490Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
51500Sstevel@tonic-gate 	struct segvn_data *nsvd;
51510Sstevel@tonic-gate 	struct anon_map *amp = svd->amp;
51520Sstevel@tonic-gate 	struct seg *nseg;
51530Sstevel@tonic-gate 	caddr_t eaddr = addr + len, a;
51540Sstevel@tonic-gate 	size_t pgsz = page_get_pagesize(szc);
51550Sstevel@tonic-gate 	int err;
51560Sstevel@tonic-gate 	u_offset_t off = svd->offset + (uintptr_t)(addr - seg->s_base);
51570Sstevel@tonic-gate 	extern struct vnode kvp;
51580Sstevel@tonic-gate 
51590Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
51600Sstevel@tonic-gate 	ASSERT(addr >= seg->s_base && eaddr <= seg->s_base + seg->s_size);
51610Sstevel@tonic-gate 
51620Sstevel@tonic-gate 	if (seg->s_szc == szc || segvn_lpg_disable != 0) {
51630Sstevel@tonic-gate 		return (0);
51640Sstevel@tonic-gate 	}
51650Sstevel@tonic-gate 
51660Sstevel@tonic-gate 	/*
51670Sstevel@tonic-gate 	 * addr should always be pgsz aligned but eaddr may be misaligned if
51680Sstevel@tonic-gate 	 * it's at the end of the segment.
51690Sstevel@tonic-gate 	 *
51700Sstevel@tonic-gate 	 * XXX we should assert this condition since as_setpagesize() logic
51710Sstevel@tonic-gate 	 * guarantees it.
51720Sstevel@tonic-gate 	 */
51730Sstevel@tonic-gate 	if (!IS_P2ALIGNED(addr, pgsz) ||
51740Sstevel@tonic-gate 	    (!IS_P2ALIGNED(eaddr, pgsz) &&
51750Sstevel@tonic-gate 		eaddr != seg->s_base + seg->s_size)) {
51760Sstevel@tonic-gate 
51770Sstevel@tonic-gate 		segvn_setpgsz_align_err++;
51780Sstevel@tonic-gate 		return (EINVAL);
51790Sstevel@tonic-gate 	}
51800Sstevel@tonic-gate 
51810Sstevel@tonic-gate 	if ((svd->vp == NULL && svd->type == MAP_SHARED) ||
51820Sstevel@tonic-gate 	    (svd->flags & MAP_NORESERVE) || seg->s_as == &kas ||
51830Sstevel@tonic-gate 	    szc > segvn_maxpgszc) {
51840Sstevel@tonic-gate 		return (EINVAL);
51850Sstevel@tonic-gate 	}
51860Sstevel@tonic-gate 
51870Sstevel@tonic-gate 	/* paranoid check */
51880Sstevel@tonic-gate 	if (svd->vp != NULL &&
51890Sstevel@tonic-gate 	    (IS_SWAPFSVP(svd->vp) || svd->vp == &kvp)) {
51900Sstevel@tonic-gate 		    return (EINVAL);
51910Sstevel@tonic-gate 	}
51920Sstevel@tonic-gate 
51930Sstevel@tonic-gate 	if (seg->s_szc == 0 && svd->vp != NULL &&
51940Sstevel@tonic-gate 	    map_addr_vacalign_check(addr, off)) {
51950Sstevel@tonic-gate 		return (EINVAL);
51960Sstevel@tonic-gate 	}
51970Sstevel@tonic-gate 
51980Sstevel@tonic-gate 	/*
51990Sstevel@tonic-gate 	 * Check that protections are the same within new page
52000Sstevel@tonic-gate 	 * size boundaries.
52010Sstevel@tonic-gate 	 */
52020Sstevel@tonic-gate 	if (svd->pageprot) {
52030Sstevel@tonic-gate 		for (a = addr; a < eaddr; a += pgsz) {
52040Sstevel@tonic-gate 			if ((a + pgsz) > eaddr) {
52050Sstevel@tonic-gate 				if (!sameprot(seg, a, eaddr - a)) {
52060Sstevel@tonic-gate 					return (EINVAL);
52070Sstevel@tonic-gate 				}
52080Sstevel@tonic-gate 			} else {
52090Sstevel@tonic-gate 				if (!sameprot(seg, a, pgsz)) {
52100Sstevel@tonic-gate 					return (EINVAL);
52110Sstevel@tonic-gate 				}
52120Sstevel@tonic-gate 			}
52130Sstevel@tonic-gate 		}
52140Sstevel@tonic-gate 	}
52150Sstevel@tonic-gate 
52160Sstevel@tonic-gate 	/*
52170Sstevel@tonic-gate 	 * Since we are changing page size we first have to flush
52180Sstevel@tonic-gate 	 * the cache. This makes sure all the pagelock calls have
52190Sstevel@tonic-gate 	 * to recheck protections.
52200Sstevel@tonic-gate 	 */
52210Sstevel@tonic-gate 	if (svd->softlockcnt > 0) {
52220Sstevel@tonic-gate 		/*
52230Sstevel@tonic-gate 		 * Since we do have the segvn writers lock nobody can fill
52240Sstevel@tonic-gate 		 * the cache with entries belonging to this seg during
52250Sstevel@tonic-gate 		 * the purge. The flush either succeeds or we still have
52260Sstevel@tonic-gate 		 * pending I/Os.
52270Sstevel@tonic-gate 		 */
52280Sstevel@tonic-gate 		segvn_purge(seg);
52290Sstevel@tonic-gate 		if (svd->softlockcnt > 0) {
52300Sstevel@tonic-gate 			return (EAGAIN);
52310Sstevel@tonic-gate 		}
52320Sstevel@tonic-gate 	}
52330Sstevel@tonic-gate 
52340Sstevel@tonic-gate 	/*
52350Sstevel@tonic-gate 	 * Operation for sub range of existing segment.
52360Sstevel@tonic-gate 	 */
52370Sstevel@tonic-gate 	if (addr != seg->s_base || eaddr != (seg->s_base + seg->s_size)) {
52380Sstevel@tonic-gate 		if (szc < seg->s_szc) {
52390Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.demoterange[2]);
52400Sstevel@tonic-gate 			err = segvn_demote_range(seg, addr, len, SDR_RANGE);
52410Sstevel@tonic-gate 			if (err == 0) {
52420Sstevel@tonic-gate 				return (IE_RETRY);
52430Sstevel@tonic-gate 			}
52440Sstevel@tonic-gate 			if (err == ENOMEM) {
52450Sstevel@tonic-gate 				return (IE_NOMEM);
52460Sstevel@tonic-gate 			}
52470Sstevel@tonic-gate 			return (err);
52480Sstevel@tonic-gate 		}
52490Sstevel@tonic-gate 		if (addr != seg->s_base) {
52500Sstevel@tonic-gate 			nseg = segvn_split_seg(seg, addr);
52510Sstevel@tonic-gate 			if (eaddr != (nseg->s_base + nseg->s_size)) {
52520Sstevel@tonic-gate 				/* eaddr is szc aligned */
52530Sstevel@tonic-gate 				(void) segvn_split_seg(nseg, eaddr);
52540Sstevel@tonic-gate 			}
52550Sstevel@tonic-gate 			return (IE_RETRY);
52560Sstevel@tonic-gate 		}
52570Sstevel@tonic-gate 		if (eaddr != (seg->s_base + seg->s_size)) {
52580Sstevel@tonic-gate 			/* eaddr is szc aligned */
52590Sstevel@tonic-gate 			(void) segvn_split_seg(seg, eaddr);
52600Sstevel@tonic-gate 		}
52610Sstevel@tonic-gate 		return (IE_RETRY);
52620Sstevel@tonic-gate 	}
52630Sstevel@tonic-gate 
52640Sstevel@tonic-gate 	/*
52650Sstevel@tonic-gate 	 * Break any low level sharing and reset seg->s_szc to 0.
52660Sstevel@tonic-gate 	 */
52670Sstevel@tonic-gate 	if ((err = segvn_clrszc(seg)) != 0) {
52680Sstevel@tonic-gate 		if (err == ENOMEM) {
52690Sstevel@tonic-gate 			err = IE_NOMEM;
52700Sstevel@tonic-gate 		}
52710Sstevel@tonic-gate 		return (err);
52720Sstevel@tonic-gate 	}
52730Sstevel@tonic-gate 	ASSERT(seg->s_szc == 0);
52740Sstevel@tonic-gate 
52750Sstevel@tonic-gate 	/*
52760Sstevel@tonic-gate 	 * If the end of the current segment is not pgsz aligned
52770Sstevel@tonic-gate 	 * then attempt to concatenate with the next segment.
52780Sstevel@tonic-gate 	 */
52790Sstevel@tonic-gate 	if (!IS_P2ALIGNED(eaddr, pgsz)) {
52800Sstevel@tonic-gate 		nseg = AS_SEGNEXT(seg->s_as, seg);
52810Sstevel@tonic-gate 		if (nseg == NULL || nseg == seg || eaddr != nseg->s_base) {
52820Sstevel@tonic-gate 			return (ENOMEM);
52830Sstevel@tonic-gate 		}
52840Sstevel@tonic-gate 		if (nseg->s_ops != &segvn_ops) {
52850Sstevel@tonic-gate 			return (EINVAL);
52860Sstevel@tonic-gate 		}
52870Sstevel@tonic-gate 		nsvd = (struct segvn_data *)nseg->s_data;
52880Sstevel@tonic-gate 		if (nsvd->softlockcnt > 0) {
52890Sstevel@tonic-gate 			segvn_purge(nseg);
52900Sstevel@tonic-gate 			if (nsvd->softlockcnt > 0) {
52910Sstevel@tonic-gate 				return (EAGAIN);
52920Sstevel@tonic-gate 			}
52930Sstevel@tonic-gate 		}
52940Sstevel@tonic-gate 		err = segvn_clrszc(nseg);
52950Sstevel@tonic-gate 		if (err == ENOMEM) {
52960Sstevel@tonic-gate 			err = IE_NOMEM;
52970Sstevel@tonic-gate 		}
52980Sstevel@tonic-gate 		if (err != 0) {
52990Sstevel@tonic-gate 			return (err);
53000Sstevel@tonic-gate 		}
53010Sstevel@tonic-gate 		err = segvn_concat(seg, nseg, 1);
53020Sstevel@tonic-gate 		if (err == -1) {
53030Sstevel@tonic-gate 			return (EINVAL);
53040Sstevel@tonic-gate 		}
53050Sstevel@tonic-gate 		if (err == -2) {
53060Sstevel@tonic-gate 			return (IE_NOMEM);
53070Sstevel@tonic-gate 		}
53080Sstevel@tonic-gate 		return (IE_RETRY);
53090Sstevel@tonic-gate 	}
53100Sstevel@tonic-gate 
53110Sstevel@tonic-gate 	/*
53120Sstevel@tonic-gate 	 * May need to re-align anon array to
53130Sstevel@tonic-gate 	 * new szc.
53140Sstevel@tonic-gate 	 */
53150Sstevel@tonic-gate 	if (amp != NULL) {
53160Sstevel@tonic-gate 		pgcnt_t pgcnt = pgsz >> PAGESHIFT;
53170Sstevel@tonic-gate 		if (!IS_P2ALIGNED(svd->anon_index, pgcnt)) {
53180Sstevel@tonic-gate 			struct anon_hdr *nahp;
53190Sstevel@tonic-gate 
53200Sstevel@tonic-gate 			ANON_LOCK_ENTER(&amp->a_rwlock, RW_WRITER);
53210Sstevel@tonic-gate 			ASSERT(amp->refcnt == 1);
53220Sstevel@tonic-gate 			nahp = anon_create(btop(amp->size), ANON_NOSLEEP);
53230Sstevel@tonic-gate 			if (nahp == NULL) {
53240Sstevel@tonic-gate 				ANON_LOCK_EXIT(&amp->a_rwlock);
53250Sstevel@tonic-gate 				return (IE_NOMEM);
53260Sstevel@tonic-gate 			}
53270Sstevel@tonic-gate 			if (anon_copy_ptr(amp->ahp, svd->anon_index,
53280Sstevel@tonic-gate 				nahp, 0, btop(seg->s_size), ANON_NOSLEEP)) {
53290Sstevel@tonic-gate 				anon_release(nahp, btop(amp->size));
53300Sstevel@tonic-gate 				ANON_LOCK_EXIT(&amp->a_rwlock);
53310Sstevel@tonic-gate 				return (IE_NOMEM);
53320Sstevel@tonic-gate 			}
53330Sstevel@tonic-gate 			anon_release(amp->ahp, btop(amp->size));
53340Sstevel@tonic-gate 			amp->ahp = nahp;
53350Sstevel@tonic-gate 			svd->anon_index = 0;
53360Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
53370Sstevel@tonic-gate 		}
53380Sstevel@tonic-gate 	}
53390Sstevel@tonic-gate 	if (svd->vp != NULL && szc != 0) {
53400Sstevel@tonic-gate 		struct vattr va;
53410Sstevel@tonic-gate 		u_offset_t eoffpage = svd->offset;
53420Sstevel@tonic-gate 		va.va_mask = AT_SIZE;
53430Sstevel@tonic-gate 		eoffpage += seg->s_size;
53440Sstevel@tonic-gate 		eoffpage = btopr(eoffpage);
53450Sstevel@tonic-gate 		if (VOP_GETATTR(svd->vp, &va, 0, svd->cred) != 0) {
53460Sstevel@tonic-gate 			segvn_setpgsz_getattr_err++;
53470Sstevel@tonic-gate 			return (EINVAL);
53480Sstevel@tonic-gate 		}
53490Sstevel@tonic-gate 		if (btopr(va.va_size) < eoffpage) {
53500Sstevel@tonic-gate 			segvn_setpgsz_eof_err++;
53510Sstevel@tonic-gate 			return (EINVAL);
53520Sstevel@tonic-gate 		}
53530Sstevel@tonic-gate 		if (amp != NULL) {
53540Sstevel@tonic-gate 			/*
53550Sstevel@tonic-gate 			 * anon_fill_cow_holes() may call VOP_GETPAGE().
53560Sstevel@tonic-gate 			 * don't take anon map lock here to avoid holding it
53570Sstevel@tonic-gate 			 * across VOP_GETPAGE() calls that may call back into
53580Sstevel@tonic-gate 			 * segvn for klsutering checks. We don't really need
53590Sstevel@tonic-gate 			 * anon map lock here since it's a private segment and
53600Sstevel@tonic-gate 			 * we hold as level lock as writers.
53610Sstevel@tonic-gate 			 */
53620Sstevel@tonic-gate 			if ((err = anon_fill_cow_holes(seg, seg->s_base,
53630Sstevel@tonic-gate 			    amp->ahp, svd->anon_index, svd->vp, svd->offset,
53640Sstevel@tonic-gate 			    seg->s_size, szc, svd->prot, svd->vpage,
53650Sstevel@tonic-gate 			    svd->cred)) != 0) {
53660Sstevel@tonic-gate 				return (EINVAL);
53670Sstevel@tonic-gate 			}
53680Sstevel@tonic-gate 		}
53690Sstevel@tonic-gate 		segvn_setvnode_mpss(svd->vp);
53700Sstevel@tonic-gate 	}
53710Sstevel@tonic-gate 
53720Sstevel@tonic-gate 	if (amp != NULL) {
53730Sstevel@tonic-gate 		ANON_LOCK_ENTER(&amp->a_rwlock, RW_WRITER);
53740Sstevel@tonic-gate 		amp->a_szc = szc;
53750Sstevel@tonic-gate 		ANON_LOCK_EXIT(&amp->a_rwlock);
53760Sstevel@tonic-gate 	}
53770Sstevel@tonic-gate 
53780Sstevel@tonic-gate 	seg->s_szc = szc;
53790Sstevel@tonic-gate 
53800Sstevel@tonic-gate 	return (0);
53810Sstevel@tonic-gate }
53820Sstevel@tonic-gate 
53830Sstevel@tonic-gate static int
53840Sstevel@tonic-gate segvn_clrszc(struct seg *seg)
53850Sstevel@tonic-gate {
53860Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
53870Sstevel@tonic-gate 	struct anon_map *amp = svd->amp;
53880Sstevel@tonic-gate 	size_t pgsz;
53890Sstevel@tonic-gate 	pgcnt_t pages;
53900Sstevel@tonic-gate 	int err = 0;
53910Sstevel@tonic-gate 	caddr_t a = seg->s_base;
53920Sstevel@tonic-gate 	caddr_t ea = a + seg->s_size;
53930Sstevel@tonic-gate 	ulong_t an_idx = svd->anon_index;
53940Sstevel@tonic-gate 	vnode_t *vp = svd->vp;
53950Sstevel@tonic-gate 	struct vpage *vpage = svd->vpage;
53960Sstevel@tonic-gate 	page_t *anon_pl[1 + 1], *pp;
53970Sstevel@tonic-gate 	struct anon *ap, *oldap;
53980Sstevel@tonic-gate 	uint_t prot = svd->prot, vpprot;
53990Sstevel@tonic-gate 
54000Sstevel@tonic-gate 	ASSERT(AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock) ||
54010Sstevel@tonic-gate 	    SEGVN_WRITE_HELD(seg->s_as, &svd->lock));
54020Sstevel@tonic-gate 	ASSERT(svd->type == MAP_PRIVATE ||
54030Sstevel@tonic-gate 	    (vp != NULL && svd->amp == NULL));
54040Sstevel@tonic-gate 
54050Sstevel@tonic-gate 	if (vp == NULL && amp == NULL) {
54060Sstevel@tonic-gate 		seg->s_szc = 0;
54070Sstevel@tonic-gate 		return (0);
54080Sstevel@tonic-gate 	}
54090Sstevel@tonic-gate 
54100Sstevel@tonic-gate 	/*
54110Sstevel@tonic-gate 	 * do HAT_UNLOAD_UNMAP since we are changing the pagesize.
54120Sstevel@tonic-gate 	 * unload argument is 0 when we are freeing the segment
54130Sstevel@tonic-gate 	 * and unload was already done.
54140Sstevel@tonic-gate 	 */
54150Sstevel@tonic-gate 	hat_unload(seg->s_as->a_hat, seg->s_base, seg->s_size,
54160Sstevel@tonic-gate 	    HAT_UNLOAD_UNMAP);
54170Sstevel@tonic-gate 
54180Sstevel@tonic-gate 	if (amp == NULL) {
54190Sstevel@tonic-gate 		seg->s_szc = 0;
54200Sstevel@tonic-gate 		return (0);
54210Sstevel@tonic-gate 	}
54220Sstevel@tonic-gate 
54230Sstevel@tonic-gate 	pgsz = page_get_pagesize(seg->s_szc);
54240Sstevel@tonic-gate 	pages = btop(pgsz);
54250Sstevel@tonic-gate 
54260Sstevel@tonic-gate 	/*
54270Sstevel@tonic-gate 	 * XXX anon rwlock is not really needed because this is a
54280Sstevel@tonic-gate 	 * private segment and we are writers.
54290Sstevel@tonic-gate 	 */
54300Sstevel@tonic-gate 	ANON_LOCK_ENTER(&amp->a_rwlock, RW_WRITER);
54310Sstevel@tonic-gate 
54320Sstevel@tonic-gate 	for (; a < ea; a += pgsz, an_idx += pages) {
54330Sstevel@tonic-gate 		if ((oldap = anon_get_ptr(amp->ahp, an_idx)) != NULL) {
54340Sstevel@tonic-gate 			if (svd->pageprot != 0) {
54350Sstevel@tonic-gate 				ASSERT(vpage != NULL);
54360Sstevel@tonic-gate 				prot = VPP_PROT(vpage);
54370Sstevel@tonic-gate 				ASSERT(sameprot(seg, a, pgsz));
54380Sstevel@tonic-gate 			}
54390Sstevel@tonic-gate 			if (seg->s_szc != 0) {
54400Sstevel@tonic-gate 				ASSERT(vp == NULL || anon_pages(amp->ahp,
54410Sstevel@tonic-gate 				    an_idx, pages) == pages);
54420Sstevel@tonic-gate 				if ((err = anon_map_demotepages(amp, an_idx,
54430Sstevel@tonic-gate 				    seg, a, prot, vpage, svd->cred)) != 0) {
54440Sstevel@tonic-gate 					goto out;
54450Sstevel@tonic-gate 				}
54460Sstevel@tonic-gate 			} else {
54470Sstevel@tonic-gate 				if (oldap->an_refcnt == 1) {
54480Sstevel@tonic-gate 					continue;
54490Sstevel@tonic-gate 				}
54500Sstevel@tonic-gate 				if ((err = anon_getpage(&oldap, &vpprot,
54510Sstevel@tonic-gate 				    anon_pl, PAGESIZE, seg, a, S_READ,
54520Sstevel@tonic-gate 				    svd->cred))) {
54530Sstevel@tonic-gate 					goto out;
54540Sstevel@tonic-gate 				}
54550Sstevel@tonic-gate 				if ((pp = anon_private(&ap, seg, a, prot,
54560Sstevel@tonic-gate 				    anon_pl[0], 0, svd->cred)) == NULL) {
54570Sstevel@tonic-gate 					err = ENOMEM;
54580Sstevel@tonic-gate 					goto out;
54590Sstevel@tonic-gate 				}
54600Sstevel@tonic-gate 				anon_decref(oldap);
54610Sstevel@tonic-gate 				(void) anon_set_ptr(amp->ahp, an_idx, ap,
54620Sstevel@tonic-gate 				    ANON_SLEEP);
54630Sstevel@tonic-gate 				page_unlock(pp);
54640Sstevel@tonic-gate 			}
54650Sstevel@tonic-gate 		}
54660Sstevel@tonic-gate 		vpage = (vpage == NULL) ? NULL : vpage + pages;
54670Sstevel@tonic-gate 	}
54680Sstevel@tonic-gate 
54690Sstevel@tonic-gate 	amp->a_szc = 0;
54700Sstevel@tonic-gate 	seg->s_szc = 0;
54710Sstevel@tonic-gate out:
54720Sstevel@tonic-gate 	ANON_LOCK_EXIT(&amp->a_rwlock);
54730Sstevel@tonic-gate 	return (err);
54740Sstevel@tonic-gate }
54750Sstevel@tonic-gate 
54760Sstevel@tonic-gate static int
54770Sstevel@tonic-gate segvn_claim_pages(
54780Sstevel@tonic-gate 	struct seg *seg,
54790Sstevel@tonic-gate 	struct vpage *svp,
54800Sstevel@tonic-gate 	u_offset_t off,
54810Sstevel@tonic-gate 	ulong_t anon_idx,
54820Sstevel@tonic-gate 	uint_t prot)
54830Sstevel@tonic-gate {
54840Sstevel@tonic-gate 	pgcnt_t	pgcnt = page_get_pagecnt(seg->s_szc);
54850Sstevel@tonic-gate 	size_t ppasize = (pgcnt + 1) * sizeof (page_t *);
54860Sstevel@tonic-gate 	page_t	**ppa;
54870Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
54880Sstevel@tonic-gate 	struct anon_map *amp = svd->amp;
54890Sstevel@tonic-gate 	struct vpage *evp = svp + pgcnt;
54900Sstevel@tonic-gate 	caddr_t addr = ((uintptr_t)(svp - svd->vpage) << PAGESHIFT)
54910Sstevel@tonic-gate 	    + seg->s_base;
54920Sstevel@tonic-gate 	struct anon *ap;
54930Sstevel@tonic-gate 	struct vnode *vp = svd->vp;
54940Sstevel@tonic-gate 	page_t *pp;
54950Sstevel@tonic-gate 	pgcnt_t pg_idx, i;
54960Sstevel@tonic-gate 	int err = 0;
54970Sstevel@tonic-gate 	anoff_t	aoff;
54980Sstevel@tonic-gate 	int anon = (amp != NULL) ? 1 : 0;
54990Sstevel@tonic-gate 
55000Sstevel@tonic-gate 	ASSERT(svd->type == MAP_PRIVATE);
55010Sstevel@tonic-gate 	ASSERT(svd->vpage != NULL);
55020Sstevel@tonic-gate 	ASSERT(seg->s_szc != 0);
55030Sstevel@tonic-gate 	ASSERT(IS_P2ALIGNED(pgcnt, pgcnt));
55040Sstevel@tonic-gate 	ASSERT(amp == NULL || IS_P2ALIGNED(anon_idx, pgcnt));
55050Sstevel@tonic-gate 	ASSERT(sameprot(seg, addr, pgcnt << PAGESHIFT));
55060Sstevel@tonic-gate 
55070Sstevel@tonic-gate 	if (VPP_PROT(svp) == prot)
55080Sstevel@tonic-gate 		return (1);
55090Sstevel@tonic-gate 	if (!((VPP_PROT(svp) ^ prot) & PROT_WRITE))
55100Sstevel@tonic-gate 		return (1);
55110Sstevel@tonic-gate 
55120Sstevel@tonic-gate 	ppa = kmem_alloc(ppasize, KM_SLEEP);
55130Sstevel@tonic-gate 	if (anon && vp != NULL) {
55140Sstevel@tonic-gate 		if (anon_get_ptr(amp->ahp, anon_idx) == NULL) {
55150Sstevel@tonic-gate 			anon = 0;
55160Sstevel@tonic-gate 			ASSERT(!anon_pages(amp->ahp, anon_idx, pgcnt));
55170Sstevel@tonic-gate 		}
55180Sstevel@tonic-gate 		ASSERT(!anon ||
55190Sstevel@tonic-gate 		    anon_pages(amp->ahp, anon_idx, pgcnt) == pgcnt);
55200Sstevel@tonic-gate 	}
55210Sstevel@tonic-gate 
55220Sstevel@tonic-gate 	for (*ppa = NULL, pg_idx = 0; svp < evp; svp++, anon_idx++) {
55230Sstevel@tonic-gate 		if (!VPP_ISPPLOCK(svp))
55240Sstevel@tonic-gate 			continue;
55250Sstevel@tonic-gate 		if (anon) {
55260Sstevel@tonic-gate 			ap = anon_get_ptr(amp->ahp, anon_idx);
55270Sstevel@tonic-gate 			if (ap == NULL) {
55280Sstevel@tonic-gate 				panic("segvn_claim_pages: no anon slot");
55290Sstevel@tonic-gate 			}
55300Sstevel@tonic-gate 			swap_xlate(ap, &vp, &aoff);
55310Sstevel@tonic-gate 			off = (u_offset_t)aoff;
55320Sstevel@tonic-gate 		}
55330Sstevel@tonic-gate 		ASSERT(vp != NULL);
55340Sstevel@tonic-gate 		if ((pp = page_lookup(vp,
55350Sstevel@tonic-gate 		    (u_offset_t)off, SE_SHARED)) == NULL) {
55360Sstevel@tonic-gate 			panic("segvn_claim_pages: no page");
55370Sstevel@tonic-gate 		}
55380Sstevel@tonic-gate 		ppa[pg_idx++] = pp;
55390Sstevel@tonic-gate 		off += PAGESIZE;
55400Sstevel@tonic-gate 	}
55410Sstevel@tonic-gate 
55420Sstevel@tonic-gate 	if (ppa[0] == NULL) {
55430Sstevel@tonic-gate 		kmem_free(ppa, ppasize);
55440Sstevel@tonic-gate 		return (1);
55450Sstevel@tonic-gate 	}
55460Sstevel@tonic-gate 
55470Sstevel@tonic-gate 	ASSERT(pg_idx <= pgcnt);
55480Sstevel@tonic-gate 	ppa[pg_idx] = NULL;
55490Sstevel@tonic-gate 
55500Sstevel@tonic-gate 	if (prot & PROT_WRITE)
55510Sstevel@tonic-gate 		err = page_addclaim_pages(ppa);
55520Sstevel@tonic-gate 	else
55530Sstevel@tonic-gate 		err = page_subclaim_pages(ppa);
55540Sstevel@tonic-gate 
55550Sstevel@tonic-gate 	for (i = 0; i < pg_idx; i++) {
55560Sstevel@tonic-gate 		ASSERT(ppa[i] != NULL);
55570Sstevel@tonic-gate 		page_unlock(ppa[i]);
55580Sstevel@tonic-gate 	}
55590Sstevel@tonic-gate 
55600Sstevel@tonic-gate 	kmem_free(ppa, ppasize);
55610Sstevel@tonic-gate 	return (err);
55620Sstevel@tonic-gate }
55630Sstevel@tonic-gate 
55640Sstevel@tonic-gate /*
55650Sstevel@tonic-gate  * Returns right (upper address) segment if split occured.
55660Sstevel@tonic-gate  * If the address is equal to the beginning or end of its segment it returns
55670Sstevel@tonic-gate  * the current segment.
55680Sstevel@tonic-gate  */
55690Sstevel@tonic-gate static struct seg *
55700Sstevel@tonic-gate segvn_split_seg(struct seg *seg, caddr_t addr)
55710Sstevel@tonic-gate {
55720Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
55730Sstevel@tonic-gate 	struct seg *nseg;
55740Sstevel@tonic-gate 	size_t nsize;
55750Sstevel@tonic-gate 	struct segvn_data *nsvd;
55760Sstevel@tonic-gate 
55770Sstevel@tonic-gate 	ASSERT(AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
55780Sstevel@tonic-gate 	ASSERT(svd->type == MAP_PRIVATE || svd->amp == NULL);
55790Sstevel@tonic-gate 	ASSERT(addr >= seg->s_base);
55800Sstevel@tonic-gate 	ASSERT(addr <= seg->s_base + seg->s_size);
55810Sstevel@tonic-gate 
55820Sstevel@tonic-gate 	if (addr == seg->s_base || addr == seg->s_base + seg->s_size)
55830Sstevel@tonic-gate 		return (seg);
55840Sstevel@tonic-gate 
55850Sstevel@tonic-gate 	nsize = seg->s_base + seg->s_size - addr;
55860Sstevel@tonic-gate 	seg->s_size = addr - seg->s_base;
55870Sstevel@tonic-gate 	nseg = seg_alloc(seg->s_as, addr, nsize);
55880Sstevel@tonic-gate 	ASSERT(nseg != NULL);
55890Sstevel@tonic-gate 	nseg->s_ops = seg->s_ops;
55900Sstevel@tonic-gate 	nsvd = kmem_cache_alloc(segvn_cache, KM_SLEEP);
55910Sstevel@tonic-gate 	nseg->s_data = (void *)nsvd;
55920Sstevel@tonic-gate 	nseg->s_szc = seg->s_szc;
55930Sstevel@tonic-gate 	*nsvd = *svd;
55940Sstevel@tonic-gate 	rw_init(&nsvd->lock, NULL, RW_DEFAULT, NULL);
55950Sstevel@tonic-gate 
55960Sstevel@tonic-gate 	if (nsvd->vp != NULL) {
55970Sstevel@tonic-gate 		VN_HOLD(nsvd->vp);
55980Sstevel@tonic-gate 		nsvd->offset = svd->offset +
55990Sstevel@tonic-gate 		    (uintptr_t)(nseg->s_base - seg->s_base);
56000Sstevel@tonic-gate 		if (nsvd->type == MAP_SHARED)
56010Sstevel@tonic-gate 			lgrp_shm_policy_init(NULL, nsvd->vp);
56020Sstevel@tonic-gate 	} else {
56030Sstevel@tonic-gate 		/*
56040Sstevel@tonic-gate 		 * The offset for an anonymous segment has no signifigance in
56050Sstevel@tonic-gate 		 * terms of an offset into a file. If we were to use the above
56060Sstevel@tonic-gate 		 * calculation instead, the structures read out of
56070Sstevel@tonic-gate 		 * /proc/<pid>/xmap would be more difficult to decipher since
56080Sstevel@tonic-gate 		 * it would be unclear whether two seemingly contiguous
56090Sstevel@tonic-gate 		 * prxmap_t structures represented different segments or a
56100Sstevel@tonic-gate 		 * single segment that had been split up into multiple prxmap_t
56110Sstevel@tonic-gate 		 * structures (e.g. if some part of the segment had not yet
56120Sstevel@tonic-gate 		 * been faulted in).
56130Sstevel@tonic-gate 		 */
56140Sstevel@tonic-gate 		nsvd->offset = 0;
56150Sstevel@tonic-gate 	}
56160Sstevel@tonic-gate 
56170Sstevel@tonic-gate 	ASSERT(svd->softlockcnt == 0);
56180Sstevel@tonic-gate 	crhold(svd->cred);
56190Sstevel@tonic-gate 
56200Sstevel@tonic-gate 	if (svd->vpage != NULL) {
56210Sstevel@tonic-gate 		size_t bytes = vpgtob(seg_pages(seg));
56220Sstevel@tonic-gate 		size_t nbytes = vpgtob(seg_pages(nseg));
56230Sstevel@tonic-gate 		struct vpage *ovpage = svd->vpage;
56240Sstevel@tonic-gate 
56250Sstevel@tonic-gate 		svd->vpage = kmem_alloc(bytes, KM_SLEEP);
56260Sstevel@tonic-gate 		bcopy(ovpage, svd->vpage, bytes);
56270Sstevel@tonic-gate 		nsvd->vpage = kmem_alloc(nbytes, KM_SLEEP);
56280Sstevel@tonic-gate 		bcopy(ovpage + seg_pages(seg), nsvd->vpage, nbytes);
56290Sstevel@tonic-gate 		kmem_free(ovpage, bytes + nbytes);
56300Sstevel@tonic-gate 	}
56310Sstevel@tonic-gate 	if (svd->amp != NULL) {
56320Sstevel@tonic-gate 		struct anon_map *oamp = svd->amp, *namp;
56330Sstevel@tonic-gate 		struct anon_hdr *nahp;
56340Sstevel@tonic-gate 
56350Sstevel@tonic-gate 		ANON_LOCK_ENTER(&oamp->a_rwlock, RW_WRITER);
56360Sstevel@tonic-gate 		ASSERT(oamp->refcnt == 1);
56370Sstevel@tonic-gate 		nahp = anon_create(btop(seg->s_size), ANON_SLEEP);
56380Sstevel@tonic-gate 		(void) anon_copy_ptr(oamp->ahp, svd->anon_index,
56390Sstevel@tonic-gate 		    nahp, 0, btop(seg->s_size), ANON_SLEEP);
56400Sstevel@tonic-gate 
56410Sstevel@tonic-gate 		namp = anonmap_alloc(nseg->s_size, 0);
56420Sstevel@tonic-gate 		namp->a_szc = nseg->s_szc;
56430Sstevel@tonic-gate 		(void) anon_copy_ptr(oamp->ahp,
56440Sstevel@tonic-gate 		    svd->anon_index + btop(seg->s_size),
56450Sstevel@tonic-gate 		    namp->ahp, 0, btop(nseg->s_size), ANON_SLEEP);
56460Sstevel@tonic-gate 		anon_release(oamp->ahp, btop(oamp->size));
56470Sstevel@tonic-gate 		oamp->ahp = nahp;
56480Sstevel@tonic-gate 		oamp->size = seg->s_size;
56490Sstevel@tonic-gate 		svd->anon_index = 0;
56500Sstevel@tonic-gate 		nsvd->amp = namp;
56510Sstevel@tonic-gate 		nsvd->anon_index = 0;
56520Sstevel@tonic-gate 		ANON_LOCK_EXIT(&oamp->a_rwlock);
56530Sstevel@tonic-gate 	}
56540Sstevel@tonic-gate 
56550Sstevel@tonic-gate 	/*
56560Sstevel@tonic-gate 	 * Split amount of swap reserve
56570Sstevel@tonic-gate 	 */
56580Sstevel@tonic-gate 	if (svd->swresv) {
56590Sstevel@tonic-gate 		/*
56600Sstevel@tonic-gate 		 * For MAP_NORESERVE, only allocate swap reserve for pages
56610Sstevel@tonic-gate 		 * being used.  Other segments get enough to cover whole
56620Sstevel@tonic-gate 		 * segment.
56630Sstevel@tonic-gate 		 */
56640Sstevel@tonic-gate 		if (svd->flags & MAP_NORESERVE) {
56650Sstevel@tonic-gate 			size_t	oswresv;
56660Sstevel@tonic-gate 
56670Sstevel@tonic-gate 			ASSERT(svd->amp);
56680Sstevel@tonic-gate 			oswresv = svd->swresv;
56690Sstevel@tonic-gate 			svd->swresv = ptob(anon_pages(svd->amp->ahp,
56700Sstevel@tonic-gate 				svd->anon_index, btop(seg->s_size)));
56710Sstevel@tonic-gate 			nsvd->swresv = ptob(anon_pages(nsvd->amp->ahp,
56720Sstevel@tonic-gate 				nsvd->anon_index, btop(nseg->s_size)));
56730Sstevel@tonic-gate 			ASSERT(oswresv >= (svd->swresv + nsvd->swresv));
56740Sstevel@tonic-gate 		} else {
56750Sstevel@tonic-gate 			ASSERT(svd->swresv == seg->s_size + nseg->s_size);
56760Sstevel@tonic-gate 			svd->swresv = seg->s_size;
56770Sstevel@tonic-gate 			nsvd->swresv = nseg->s_size;
56780Sstevel@tonic-gate 		}
56790Sstevel@tonic-gate 	}
56800Sstevel@tonic-gate 
56810Sstevel@tonic-gate 	return (nseg);
56820Sstevel@tonic-gate }
56830Sstevel@tonic-gate 
56840Sstevel@tonic-gate 
56850Sstevel@tonic-gate /*
56860Sstevel@tonic-gate  * called on memory operations (unmap, setprot, setpagesize) for a subset
56870Sstevel@tonic-gate  * of a large page segment to either demote the memory range (SDR_RANGE)
56880Sstevel@tonic-gate  * or the ends (SDR_END) by addr/len.
56890Sstevel@tonic-gate  *
56900Sstevel@tonic-gate  * returns 0 on success. returns errno, including ENOMEM, on failure.
56910Sstevel@tonic-gate  */
56920Sstevel@tonic-gate static int
56930Sstevel@tonic-gate segvn_demote_range(struct seg *seg, caddr_t addr, size_t len, int flag)
56940Sstevel@tonic-gate {
56950Sstevel@tonic-gate 	caddr_t eaddr = addr + len;
56960Sstevel@tonic-gate 	caddr_t lpgaddr, lpgeaddr;
56970Sstevel@tonic-gate 	struct seg *nseg;
56980Sstevel@tonic-gate 	struct seg *badseg1 = NULL;
56990Sstevel@tonic-gate 	struct seg *badseg2 = NULL;
57000Sstevel@tonic-gate 	size_t pgsz;
57010Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
57020Sstevel@tonic-gate 	int err;
57030Sstevel@tonic-gate 
57040Sstevel@tonic-gate 	ASSERT(AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
57050Sstevel@tonic-gate 	ASSERT(seg->s_szc != 0);
57060Sstevel@tonic-gate 	pgsz = page_get_pagesize(seg->s_szc);
57070Sstevel@tonic-gate 	ASSERT(seg->s_base != addr || seg->s_size != len);
57080Sstevel@tonic-gate 	ASSERT(addr >= seg->s_base && eaddr <= seg->s_base + seg->s_size);
57090Sstevel@tonic-gate 	ASSERT(svd->softlockcnt == 0);
57100Sstevel@tonic-gate 	ASSERT(svd->type == MAP_PRIVATE ||
57110Sstevel@tonic-gate 	    (svd->vp != NULL && svd->amp == NULL));
57120Sstevel@tonic-gate 
57130Sstevel@tonic-gate 	CALC_LPG_REGION(pgsz, seg, addr, len, lpgaddr, lpgeaddr);
57140Sstevel@tonic-gate 	ASSERT(flag == SDR_RANGE || eaddr < lpgeaddr || addr > lpgaddr);
57150Sstevel@tonic-gate 	if (flag == SDR_RANGE) {
57160Sstevel@tonic-gate 		/* demote entire range */
57170Sstevel@tonic-gate 		badseg1 = nseg = segvn_split_seg(seg, lpgaddr);
57180Sstevel@tonic-gate 		(void) segvn_split_seg(nseg, lpgeaddr);
57190Sstevel@tonic-gate 		ASSERT(badseg1->s_base == lpgaddr);
57200Sstevel@tonic-gate 		ASSERT(badseg1->s_size == lpgeaddr - lpgaddr);
57210Sstevel@tonic-gate 	} else if (addr != lpgaddr) {
57220Sstevel@tonic-gate 		ASSERT(flag == SDR_END);
57230Sstevel@tonic-gate 		badseg1 = nseg = segvn_split_seg(seg, lpgaddr);
57240Sstevel@tonic-gate 		if (eaddr != lpgeaddr && eaddr > lpgaddr + pgsz &&
57250Sstevel@tonic-gate 		    eaddr < lpgaddr + 2 * pgsz) {
57260Sstevel@tonic-gate 			(void) segvn_split_seg(nseg, lpgeaddr);
57270Sstevel@tonic-gate 			ASSERT(badseg1->s_base == lpgaddr);
57280Sstevel@tonic-gate 			ASSERT(badseg1->s_size == 2 * pgsz);
57290Sstevel@tonic-gate 		} else {
57300Sstevel@tonic-gate 			nseg = segvn_split_seg(nseg, lpgaddr + pgsz);
57310Sstevel@tonic-gate 			ASSERT(badseg1->s_base == lpgaddr);
57320Sstevel@tonic-gate 			ASSERT(badseg1->s_size == pgsz);
57330Sstevel@tonic-gate 			if (eaddr != lpgeaddr && eaddr > lpgaddr + pgsz) {
57340Sstevel@tonic-gate 				ASSERT(lpgeaddr - lpgaddr > 2 * pgsz);
57350Sstevel@tonic-gate 				nseg = segvn_split_seg(nseg, lpgeaddr - pgsz);
57360Sstevel@tonic-gate 				badseg2 = nseg;
57370Sstevel@tonic-gate 				(void) segvn_split_seg(nseg, lpgeaddr);
57380Sstevel@tonic-gate 				ASSERT(badseg2->s_base == lpgeaddr - pgsz);
57390Sstevel@tonic-gate 				ASSERT(badseg2->s_size == pgsz);
57400Sstevel@tonic-gate 			}
57410Sstevel@tonic-gate 		}
57420Sstevel@tonic-gate 	} else {
57430Sstevel@tonic-gate 		ASSERT(flag == SDR_END);
57440Sstevel@tonic-gate 		ASSERT(eaddr < lpgeaddr);
57450Sstevel@tonic-gate 		badseg1 = nseg = segvn_split_seg(seg, lpgeaddr - pgsz);
57460Sstevel@tonic-gate 		(void) segvn_split_seg(nseg, lpgeaddr);
57470Sstevel@tonic-gate 		ASSERT(badseg1->s_base == lpgeaddr - pgsz);
57480Sstevel@tonic-gate 		ASSERT(badseg1->s_size == pgsz);
57490Sstevel@tonic-gate 	}
57500Sstevel@tonic-gate 
57510Sstevel@tonic-gate 	ASSERT(badseg1 != NULL);
57520Sstevel@tonic-gate 	ASSERT(badseg1->s_szc != 0);
57530Sstevel@tonic-gate 	ASSERT(page_get_pagesize(badseg1->s_szc) == pgsz);
57540Sstevel@tonic-gate 	ASSERT(flag == SDR_RANGE || badseg1->s_size == pgsz ||
57550Sstevel@tonic-gate 	    badseg1->s_size == 2 * pgsz);
57560Sstevel@tonic-gate 	if (err = segvn_clrszc(badseg1)) {
57570Sstevel@tonic-gate 		return (err);
57580Sstevel@tonic-gate 	}
57590Sstevel@tonic-gate 	ASSERT(badseg1->s_szc == 0);
57600Sstevel@tonic-gate 
57610Sstevel@tonic-gate 	if (badseg2 == NULL)
57620Sstevel@tonic-gate 		return (0);
57630Sstevel@tonic-gate 	ASSERT(badseg2->s_szc != 0);
57640Sstevel@tonic-gate 	ASSERT(page_get_pagesize(badseg2->s_szc) == pgsz);
57650Sstevel@tonic-gate 	ASSERT(badseg2->s_size == pgsz);
57660Sstevel@tonic-gate 	ASSERT(sameprot(badseg2, badseg2->s_base, badseg2->s_size));
57670Sstevel@tonic-gate 	if (err = segvn_clrszc(badseg2)) {
57680Sstevel@tonic-gate 		return (err);
57690Sstevel@tonic-gate 	}
57700Sstevel@tonic-gate 	ASSERT(badseg2->s_szc == 0);
57710Sstevel@tonic-gate 	return (0);
57720Sstevel@tonic-gate }
57730Sstevel@tonic-gate 
57740Sstevel@tonic-gate static int
57750Sstevel@tonic-gate segvn_checkprot(struct seg *seg, caddr_t addr, size_t len, uint_t prot)
57760Sstevel@tonic-gate {
57770Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
57780Sstevel@tonic-gate 	struct vpage *vp, *evp;
57790Sstevel@tonic-gate 
57800Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
57810Sstevel@tonic-gate 
57820Sstevel@tonic-gate 	SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
57830Sstevel@tonic-gate 	/*
57840Sstevel@tonic-gate 	 * If segment protection can be used, simply check against them.
57850Sstevel@tonic-gate 	 */
57860Sstevel@tonic-gate 	if (svd->pageprot == 0) {
57870Sstevel@tonic-gate 		int err;
57880Sstevel@tonic-gate 
57890Sstevel@tonic-gate 		err = ((svd->prot & prot) != prot) ? EACCES : 0;
57900Sstevel@tonic-gate 		SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
57910Sstevel@tonic-gate 		return (err);
57920Sstevel@tonic-gate 	}
57930Sstevel@tonic-gate 
57940Sstevel@tonic-gate 	/*
57950Sstevel@tonic-gate 	 * Have to check down to the vpage level.
57960Sstevel@tonic-gate 	 */
57970Sstevel@tonic-gate 	evp = &svd->vpage[seg_page(seg, addr + len)];
57980Sstevel@tonic-gate 	for (vp = &svd->vpage[seg_page(seg, addr)]; vp < evp; vp++) {
57990Sstevel@tonic-gate 		if ((VPP_PROT(vp) & prot) != prot) {
58000Sstevel@tonic-gate 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
58010Sstevel@tonic-gate 			return (EACCES);
58020Sstevel@tonic-gate 		}
58030Sstevel@tonic-gate 	}
58040Sstevel@tonic-gate 	SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
58050Sstevel@tonic-gate 	return (0);
58060Sstevel@tonic-gate }
58070Sstevel@tonic-gate 
58080Sstevel@tonic-gate static int
58090Sstevel@tonic-gate segvn_getprot(struct seg *seg, caddr_t addr, size_t len, uint_t *protv)
58100Sstevel@tonic-gate {
58110Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
58120Sstevel@tonic-gate 	size_t pgno = seg_page(seg, addr + len) - seg_page(seg, addr) + 1;
58130Sstevel@tonic-gate 
58140Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
58150Sstevel@tonic-gate 
58160Sstevel@tonic-gate 	if (pgno != 0) {
58170Sstevel@tonic-gate 		SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
58180Sstevel@tonic-gate 		if (svd->pageprot == 0) {
58190Sstevel@tonic-gate 			do
58200Sstevel@tonic-gate 				protv[--pgno] = svd->prot;
58210Sstevel@tonic-gate 			while (pgno != 0);
58220Sstevel@tonic-gate 		} else {
58230Sstevel@tonic-gate 			size_t pgoff = seg_page(seg, addr);
58240Sstevel@tonic-gate 
58250Sstevel@tonic-gate 			do {
58260Sstevel@tonic-gate 				pgno--;
58270Sstevel@tonic-gate 				protv[pgno] = VPP_PROT(&svd->vpage[pgno+pgoff]);
58280Sstevel@tonic-gate 			} while (pgno != 0);
58290Sstevel@tonic-gate 		}
58300Sstevel@tonic-gate 		SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
58310Sstevel@tonic-gate 	}
58320Sstevel@tonic-gate 	return (0);
58330Sstevel@tonic-gate }
58340Sstevel@tonic-gate 
58350Sstevel@tonic-gate static u_offset_t
58360Sstevel@tonic-gate segvn_getoffset(struct seg *seg, caddr_t addr)
58370Sstevel@tonic-gate {
58380Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
58390Sstevel@tonic-gate 
58400Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
58410Sstevel@tonic-gate 
58420Sstevel@tonic-gate 	return (svd->offset + (uintptr_t)(addr - seg->s_base));
58430Sstevel@tonic-gate }
58440Sstevel@tonic-gate 
58450Sstevel@tonic-gate /*ARGSUSED*/
58460Sstevel@tonic-gate static int
58470Sstevel@tonic-gate segvn_gettype(struct seg *seg, caddr_t addr)
58480Sstevel@tonic-gate {
58490Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
58500Sstevel@tonic-gate 
58510Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
58520Sstevel@tonic-gate 
58530Sstevel@tonic-gate 	return (svd->type | (svd->flags & MAP_NORESERVE));
58540Sstevel@tonic-gate }
58550Sstevel@tonic-gate 
58560Sstevel@tonic-gate /*ARGSUSED*/
58570Sstevel@tonic-gate static int
58580Sstevel@tonic-gate segvn_getvp(struct seg *seg, caddr_t addr, struct vnode **vpp)
58590Sstevel@tonic-gate {
58600Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
58610Sstevel@tonic-gate 
58620Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
58630Sstevel@tonic-gate 
58640Sstevel@tonic-gate 	*vpp = svd->vp;
58650Sstevel@tonic-gate 	return (0);
58660Sstevel@tonic-gate }
58670Sstevel@tonic-gate 
58680Sstevel@tonic-gate /*
58690Sstevel@tonic-gate  * Check to see if it makes sense to do kluster/read ahead to
58700Sstevel@tonic-gate  * addr + delta relative to the mapping at addr.  We assume here
58710Sstevel@tonic-gate  * that delta is a signed PAGESIZE'd multiple (which can be negative).
58720Sstevel@tonic-gate  *
58730Sstevel@tonic-gate  * For segvn, we currently "approve" of the action if we are
58740Sstevel@tonic-gate  * still in the segment and it maps from the same vp/off,
58750Sstevel@tonic-gate  * or if the advice stored in segvn_data or vpages allows it.
58760Sstevel@tonic-gate  * Currently, klustering is not allowed only if MADV_RANDOM is set.
58770Sstevel@tonic-gate  */
58780Sstevel@tonic-gate static int
58790Sstevel@tonic-gate segvn_kluster(struct seg *seg, caddr_t addr, ssize_t delta)
58800Sstevel@tonic-gate {
58810Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
58820Sstevel@tonic-gate 	struct anon *oap, *ap;
58830Sstevel@tonic-gate 	ssize_t pd;
58840Sstevel@tonic-gate 	size_t page;
58850Sstevel@tonic-gate 	struct vnode *vp1, *vp2;
58860Sstevel@tonic-gate 	u_offset_t off1, off2;
58870Sstevel@tonic-gate 	struct anon_map *amp;
58880Sstevel@tonic-gate 
58890Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
58900Sstevel@tonic-gate 	ASSERT(AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock) ||
58910Sstevel@tonic-gate 	    SEGVN_LOCK_HELD(seg->s_as, &svd->lock));
58920Sstevel@tonic-gate 
58930Sstevel@tonic-gate 	if (addr + delta < seg->s_base ||
58940Sstevel@tonic-gate 	    addr + delta >= (seg->s_base + seg->s_size))
58950Sstevel@tonic-gate 		return (-1);		/* exceeded segment bounds */
58960Sstevel@tonic-gate 
58970Sstevel@tonic-gate 	pd = delta / (ssize_t)PAGESIZE;	/* divide to preserve sign bit */
58980Sstevel@tonic-gate 	page = seg_page(seg, addr);
58990Sstevel@tonic-gate 
59000Sstevel@tonic-gate 	/*
59010Sstevel@tonic-gate 	 * Check to see if either of the pages addr or addr + delta
59020Sstevel@tonic-gate 	 * have advice set that prevents klustering (if MADV_RANDOM advice
59030Sstevel@tonic-gate 	 * is set for entire segment, or MADV_SEQUENTIAL is set and delta
59040Sstevel@tonic-gate 	 * is negative).
59050Sstevel@tonic-gate 	 */
59060Sstevel@tonic-gate 	if (svd->advice == MADV_RANDOM ||
59070Sstevel@tonic-gate 	    svd->advice == MADV_SEQUENTIAL && delta < 0)
59080Sstevel@tonic-gate 		return (-1);
59090Sstevel@tonic-gate 	else if (svd->pageadvice && svd->vpage) {
59100Sstevel@tonic-gate 		struct vpage *bvpp, *evpp;
59110Sstevel@tonic-gate 
59120Sstevel@tonic-gate 		bvpp = &svd->vpage[page];
59130Sstevel@tonic-gate 		evpp = &svd->vpage[page + pd];
59140Sstevel@tonic-gate 		if (VPP_ADVICE(bvpp) == MADV_RANDOM ||
59150Sstevel@tonic-gate 		    VPP_ADVICE(evpp) == MADV_SEQUENTIAL && delta < 0)
59160Sstevel@tonic-gate 			return (-1);
59170Sstevel@tonic-gate 		if (VPP_ADVICE(bvpp) != VPP_ADVICE(evpp) &&
59180Sstevel@tonic-gate 		    VPP_ADVICE(evpp) == MADV_RANDOM)
59190Sstevel@tonic-gate 			return (-1);
59200Sstevel@tonic-gate 	}
59210Sstevel@tonic-gate 
59220Sstevel@tonic-gate 	if (svd->type == MAP_SHARED)
59230Sstevel@tonic-gate 		return (0);		/* shared mapping - all ok */
59240Sstevel@tonic-gate 
59250Sstevel@tonic-gate 	if ((amp = svd->amp) == NULL)
59260Sstevel@tonic-gate 		return (0);		/* off original vnode */
59270Sstevel@tonic-gate 
59280Sstevel@tonic-gate 	page += svd->anon_index;
59290Sstevel@tonic-gate 
59300Sstevel@tonic-gate 	ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
59310Sstevel@tonic-gate 
59320Sstevel@tonic-gate 	oap = anon_get_ptr(amp->ahp, page);
59330Sstevel@tonic-gate 	ap = anon_get_ptr(amp->ahp, page + pd);
59340Sstevel@tonic-gate 
59350Sstevel@tonic-gate 	ANON_LOCK_EXIT(&amp->a_rwlock);
59360Sstevel@tonic-gate 
59370Sstevel@tonic-gate 	if ((oap == NULL && ap != NULL) || (oap != NULL && ap == NULL)) {
59380Sstevel@tonic-gate 		return (-1);		/* one with and one without an anon */
59390Sstevel@tonic-gate 	}
59400Sstevel@tonic-gate 
59410Sstevel@tonic-gate 	if (oap == NULL) {		/* implies that ap == NULL */
59420Sstevel@tonic-gate 		return (0);		/* off original vnode */
59430Sstevel@tonic-gate 	}
59440Sstevel@tonic-gate 
59450Sstevel@tonic-gate 	/*
59460Sstevel@tonic-gate 	 * Now we know we have two anon pointers - check to
59470Sstevel@tonic-gate 	 * see if they happen to be properly allocated.
59480Sstevel@tonic-gate 	 */
59490Sstevel@tonic-gate 
59500Sstevel@tonic-gate 	/*
59510Sstevel@tonic-gate 	 * XXX We cheat here and don't lock the anon slots. We can't because
59520Sstevel@tonic-gate 	 * we may have been called from the anon layer which might already
59530Sstevel@tonic-gate 	 * have locked them. We are holding a refcnt on the slots so they
59540Sstevel@tonic-gate 	 * can't disappear. The worst that will happen is we'll get the wrong
59550Sstevel@tonic-gate 	 * names (vp, off) for the slots and make a poor klustering decision.
59560Sstevel@tonic-gate 	 */
59570Sstevel@tonic-gate 	swap_xlate(ap, &vp1, &off1);
59580Sstevel@tonic-gate 	swap_xlate(oap, &vp2, &off2);
59590Sstevel@tonic-gate 
59600Sstevel@tonic-gate 
59610Sstevel@tonic-gate 	if (!VOP_CMP(vp1, vp2) || off1 - off2 != delta)
59620Sstevel@tonic-gate 		return (-1);
59630Sstevel@tonic-gate 	return (0);
59640Sstevel@tonic-gate }
59650Sstevel@tonic-gate 
59660Sstevel@tonic-gate /*
59670Sstevel@tonic-gate  * Swap the pages of seg out to secondary storage, returning the
59680Sstevel@tonic-gate  * number of bytes of storage freed.
59690Sstevel@tonic-gate  *
59700Sstevel@tonic-gate  * The basic idea is first to unload all translations and then to call
59710Sstevel@tonic-gate  * VOP_PUTPAGE() for all newly-unmapped pages, to push them out to the
59720Sstevel@tonic-gate  * swap device.  Pages to which other segments have mappings will remain
59730Sstevel@tonic-gate  * mapped and won't be swapped.  Our caller (as_swapout) has already
59740Sstevel@tonic-gate  * performed the unloading step.
59750Sstevel@tonic-gate  *
59760Sstevel@tonic-gate  * The value returned is intended to correlate well with the process's
59770Sstevel@tonic-gate  * memory requirements.  However, there are some caveats:
59780Sstevel@tonic-gate  * 1)	When given a shared segment as argument, this routine will
59790Sstevel@tonic-gate  *	only succeed in swapping out pages for the last sharer of the
59800Sstevel@tonic-gate  *	segment.  (Previous callers will only have decremented mapping
59810Sstevel@tonic-gate  *	reference counts.)
59820Sstevel@tonic-gate  * 2)	We assume that the hat layer maintains a large enough translation
59830Sstevel@tonic-gate  *	cache to capture process reference patterns.
59840Sstevel@tonic-gate  */
59850Sstevel@tonic-gate static size_t
59860Sstevel@tonic-gate segvn_swapout(struct seg *seg)
59870Sstevel@tonic-gate {
59880Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
59890Sstevel@tonic-gate 	struct anon_map *amp;
59900Sstevel@tonic-gate 	pgcnt_t pgcnt = 0;
59910Sstevel@tonic-gate 	pgcnt_t npages;
59920Sstevel@tonic-gate 	pgcnt_t page;
59930Sstevel@tonic-gate 	ulong_t anon_index;
59940Sstevel@tonic-gate 
59950Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
59960Sstevel@tonic-gate 
59970Sstevel@tonic-gate 	SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
59980Sstevel@tonic-gate 	/*
59990Sstevel@tonic-gate 	 * Find pages unmapped by our caller and force them
60000Sstevel@tonic-gate 	 * out to the virtual swap device.
60010Sstevel@tonic-gate 	 */
60020Sstevel@tonic-gate 	if ((amp = svd->amp) != NULL)
60030Sstevel@tonic-gate 		anon_index = svd->anon_index;
60040Sstevel@tonic-gate 	npages = seg->s_size >> PAGESHIFT;
60050Sstevel@tonic-gate 	for (page = 0; page < npages; page++) {
60060Sstevel@tonic-gate 		page_t *pp;
60070Sstevel@tonic-gate 		struct anon *ap;
60080Sstevel@tonic-gate 		struct vnode *vp;
60090Sstevel@tonic-gate 		u_offset_t off;
60100Sstevel@tonic-gate 		anon_sync_obj_t cookie;
60110Sstevel@tonic-gate 
60120Sstevel@tonic-gate 		/*
60130Sstevel@tonic-gate 		 * Obtain <vp, off> pair for the page, then look it up.
60140Sstevel@tonic-gate 		 *
60150Sstevel@tonic-gate 		 * Note that this code is willing to consider regular
60160Sstevel@tonic-gate 		 * pages as well as anon pages.  Is this appropriate here?
60170Sstevel@tonic-gate 		 */
60180Sstevel@tonic-gate 		ap = NULL;
60190Sstevel@tonic-gate 		if (amp != NULL) {
60200Sstevel@tonic-gate 			ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
60210Sstevel@tonic-gate 			anon_array_enter(amp, anon_index + page, &cookie);
60220Sstevel@tonic-gate 			ap = anon_get_ptr(amp->ahp, anon_index + page);
60230Sstevel@tonic-gate 			if (ap != NULL) {
60240Sstevel@tonic-gate 				swap_xlate(ap, &vp, &off);
60250Sstevel@tonic-gate 			} else {
60260Sstevel@tonic-gate 				vp = svd->vp;
60270Sstevel@tonic-gate 				off = svd->offset + ptob(page);
60280Sstevel@tonic-gate 			}
60290Sstevel@tonic-gate 			anon_array_exit(&cookie);
60300Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
60310Sstevel@tonic-gate 		} else {
60320Sstevel@tonic-gate 			vp = svd->vp;
60330Sstevel@tonic-gate 			off = svd->offset + ptob(page);
60340Sstevel@tonic-gate 		}
60350Sstevel@tonic-gate 		if (vp == NULL) {		/* untouched zfod page */
60360Sstevel@tonic-gate 			ASSERT(ap == NULL);
60370Sstevel@tonic-gate 			continue;
60380Sstevel@tonic-gate 		}
60390Sstevel@tonic-gate 
60400Sstevel@tonic-gate 		pp = page_lookup_nowait(vp, off, SE_SHARED);
60410Sstevel@tonic-gate 		if (pp == NULL)
60420Sstevel@tonic-gate 			continue;
60430Sstevel@tonic-gate 
60440Sstevel@tonic-gate 
60450Sstevel@tonic-gate 		/*
60460Sstevel@tonic-gate 		 * Examine the page to see whether it can be tossed out,
60470Sstevel@tonic-gate 		 * keeping track of how many we've found.
60480Sstevel@tonic-gate 		 */
60490Sstevel@tonic-gate 		if (!page_tryupgrade(pp)) {
60500Sstevel@tonic-gate 			/*
60510Sstevel@tonic-gate 			 * If the page has an i/o lock and no mappings,
60520Sstevel@tonic-gate 			 * it's very likely that the page is being
60530Sstevel@tonic-gate 			 * written out as a result of klustering.
60540Sstevel@tonic-gate 			 * Assume this is so and take credit for it here.
60550Sstevel@tonic-gate 			 */
60560Sstevel@tonic-gate 			if (!page_io_trylock(pp)) {
60570Sstevel@tonic-gate 				if (!hat_page_is_mapped(pp))
60580Sstevel@tonic-gate 					pgcnt++;
60590Sstevel@tonic-gate 			} else {
60600Sstevel@tonic-gate 				page_io_unlock(pp);
60610Sstevel@tonic-gate 			}
60620Sstevel@tonic-gate 			page_unlock(pp);
60630Sstevel@tonic-gate 			continue;
60640Sstevel@tonic-gate 		}
60650Sstevel@tonic-gate 		ASSERT(!page_iolock_assert(pp));
60660Sstevel@tonic-gate 
60670Sstevel@tonic-gate 
60680Sstevel@tonic-gate 		/*
60690Sstevel@tonic-gate 		 * Skip if page is locked or has mappings.
60700Sstevel@tonic-gate 		 * We don't need the page_struct_lock to look at lckcnt
60710Sstevel@tonic-gate 		 * and cowcnt because the page is exclusive locked.
60720Sstevel@tonic-gate 		 */
60730Sstevel@tonic-gate 		if (pp->p_lckcnt != 0 || pp->p_cowcnt != 0 ||
60740Sstevel@tonic-gate 		    hat_page_is_mapped(pp)) {
60750Sstevel@tonic-gate 			page_unlock(pp);
60760Sstevel@tonic-gate 			continue;
60770Sstevel@tonic-gate 		}
60780Sstevel@tonic-gate 
60790Sstevel@tonic-gate 		/*
60800Sstevel@tonic-gate 		 * dispose skips large pages so try to demote first.
60810Sstevel@tonic-gate 		 */
60820Sstevel@tonic-gate 		if (pp->p_szc != 0 && !page_try_demote_pages(pp)) {
60830Sstevel@tonic-gate 			page_unlock(pp);
60840Sstevel@tonic-gate 			/*
60850Sstevel@tonic-gate 			 * XXX should skip the remaining page_t's of this
60860Sstevel@tonic-gate 			 * large page.
60870Sstevel@tonic-gate 			 */
60880Sstevel@tonic-gate 			continue;
60890Sstevel@tonic-gate 		}
60900Sstevel@tonic-gate 
60910Sstevel@tonic-gate 		ASSERT(pp->p_szc == 0);
60920Sstevel@tonic-gate 
60930Sstevel@tonic-gate 		/*
60940Sstevel@tonic-gate 		 * No longer mapped -- we can toss it out.  How
60950Sstevel@tonic-gate 		 * we do so depends on whether or not it's dirty.
60960Sstevel@tonic-gate 		 */
60970Sstevel@tonic-gate 		if (hat_ismod(pp) && pp->p_vnode) {
60980Sstevel@tonic-gate 			/*
60990Sstevel@tonic-gate 			 * We must clean the page before it can be
61000Sstevel@tonic-gate 			 * freed.  Setting B_FREE will cause pvn_done
61010Sstevel@tonic-gate 			 * to free the page when the i/o completes.
61020Sstevel@tonic-gate 			 * XXX:	This also causes it to be accounted
61030Sstevel@tonic-gate 			 *	as a pageout instead of a swap: need
61040Sstevel@tonic-gate 			 *	B_SWAPOUT bit to use instead of B_FREE.
61050Sstevel@tonic-gate 			 *
61060Sstevel@tonic-gate 			 * Hold the vnode before releasing the page lock
61070Sstevel@tonic-gate 			 * to prevent it from being freed and re-used by
61080Sstevel@tonic-gate 			 * some other thread.
61090Sstevel@tonic-gate 			 */
61100Sstevel@tonic-gate 			VN_HOLD(vp);
61110Sstevel@tonic-gate 			page_unlock(pp);
61120Sstevel@tonic-gate 
61130Sstevel@tonic-gate 			/*
61140Sstevel@tonic-gate 			 * Queue all i/o requests for the pageout thread
61150Sstevel@tonic-gate 			 * to avoid saturating the pageout devices.
61160Sstevel@tonic-gate 			 */
61170Sstevel@tonic-gate 			if (!queue_io_request(vp, off))
61180Sstevel@tonic-gate 				VN_RELE(vp);
61190Sstevel@tonic-gate 		} else {
61200Sstevel@tonic-gate 			/*
61210Sstevel@tonic-gate 			 * The page was clean, free it.
61220Sstevel@tonic-gate 			 *
61230Sstevel@tonic-gate 			 * XXX:	Can we ever encounter modified pages
61240Sstevel@tonic-gate 			 *	with no associated vnode here?
61250Sstevel@tonic-gate 			 */
61260Sstevel@tonic-gate 			ASSERT(pp->p_vnode != NULL);
61270Sstevel@tonic-gate 			/*LINTED: constant in conditional context*/
61280Sstevel@tonic-gate 			VN_DISPOSE(pp, B_FREE, 0, kcred);
61290Sstevel@tonic-gate 		}
61300Sstevel@tonic-gate 
61310Sstevel@tonic-gate 		/*
61320Sstevel@tonic-gate 		 * Credit now even if i/o is in progress.
61330Sstevel@tonic-gate 		 */
61340Sstevel@tonic-gate 		pgcnt++;
61350Sstevel@tonic-gate 	}
61360Sstevel@tonic-gate 	SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
61370Sstevel@tonic-gate 
61380Sstevel@tonic-gate 	/*
61390Sstevel@tonic-gate 	 * Wakeup pageout to initiate i/o on all queued requests.
61400Sstevel@tonic-gate 	 */
61410Sstevel@tonic-gate 	cv_signal_pageout();
61420Sstevel@tonic-gate 	return (ptob(pgcnt));
61430Sstevel@tonic-gate }
61440Sstevel@tonic-gate 
61450Sstevel@tonic-gate /*
61460Sstevel@tonic-gate  * Synchronize primary storage cache with real object in virtual memory.
61470Sstevel@tonic-gate  *
61480Sstevel@tonic-gate  * XXX - Anonymous pages should not be sync'ed out at all.
61490Sstevel@tonic-gate  */
61500Sstevel@tonic-gate static int
61510Sstevel@tonic-gate segvn_sync(struct seg *seg, caddr_t addr, size_t len, int attr, uint_t flags)
61520Sstevel@tonic-gate {
61530Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
61540Sstevel@tonic-gate 	struct vpage *vpp;
61550Sstevel@tonic-gate 	page_t *pp;
61560Sstevel@tonic-gate 	u_offset_t offset;
61570Sstevel@tonic-gate 	struct vnode *vp;
61580Sstevel@tonic-gate 	u_offset_t off;
61590Sstevel@tonic-gate 	caddr_t eaddr;
61600Sstevel@tonic-gate 	int bflags;
61610Sstevel@tonic-gate 	int err = 0;
61620Sstevel@tonic-gate 	int segtype;
61630Sstevel@tonic-gate 	int pageprot;
61640Sstevel@tonic-gate 	int prot;
61650Sstevel@tonic-gate 	ulong_t anon_index;
61660Sstevel@tonic-gate 	struct anon_map *amp;
61670Sstevel@tonic-gate 	struct anon *ap;
61680Sstevel@tonic-gate 	anon_sync_obj_t cookie;
61690Sstevel@tonic-gate 
61700Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
61710Sstevel@tonic-gate 
61720Sstevel@tonic-gate 	SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
61730Sstevel@tonic-gate 
61740Sstevel@tonic-gate 	if (svd->softlockcnt > 0) {
61750Sstevel@tonic-gate 		/*
61760Sstevel@tonic-gate 		 * flush all pages from seg cache
61770Sstevel@tonic-gate 		 * otherwise we may deadlock in swap_putpage
61780Sstevel@tonic-gate 		 * for B_INVAL page (4175402).
61790Sstevel@tonic-gate 		 *
61800Sstevel@tonic-gate 		 * Even if we grab segvn WRITER's lock or segp_slock
61810Sstevel@tonic-gate 		 * here, there might be another thread which could've
61820Sstevel@tonic-gate 		 * successfully performed lookup/insert just before
61830Sstevel@tonic-gate 		 * we acquired the lock here.  So, grabbing either
61840Sstevel@tonic-gate 		 * lock here is of not much use.  Until we devise
61850Sstevel@tonic-gate 		 * a strategy at upper layers to solve the
61860Sstevel@tonic-gate 		 * synchronization issues completely, we expect
61870Sstevel@tonic-gate 		 * applications to handle this appropriately.
61880Sstevel@tonic-gate 		 */
61890Sstevel@tonic-gate 		segvn_purge(seg);
61900Sstevel@tonic-gate 		if (svd->softlockcnt > 0) {
61910Sstevel@tonic-gate 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
61920Sstevel@tonic-gate 			return (EAGAIN);
61930Sstevel@tonic-gate 		}
61940Sstevel@tonic-gate 	}
61950Sstevel@tonic-gate 
61960Sstevel@tonic-gate 	vpp = svd->vpage;
61970Sstevel@tonic-gate 	offset = svd->offset + (uintptr_t)(addr - seg->s_base);
61980Sstevel@tonic-gate 	bflags = ((flags & MS_ASYNC) ? B_ASYNC : 0) |
61990Sstevel@tonic-gate 	    ((flags & MS_INVALIDATE) ? B_INVAL : 0);
62000Sstevel@tonic-gate 
62010Sstevel@tonic-gate 	if (attr) {
62020Sstevel@tonic-gate 		pageprot = attr & ~(SHARED|PRIVATE);
62030Sstevel@tonic-gate 		segtype = (attr & SHARED) ? MAP_SHARED : MAP_PRIVATE;
62040Sstevel@tonic-gate 
62050Sstevel@tonic-gate 		/*
62060Sstevel@tonic-gate 		 * We are done if the segment types don't match
62070Sstevel@tonic-gate 		 * or if we have segment level protections and
62080Sstevel@tonic-gate 		 * they don't match.
62090Sstevel@tonic-gate 		 */
62100Sstevel@tonic-gate 		if (svd->type != segtype) {
62110Sstevel@tonic-gate 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
62120Sstevel@tonic-gate 			return (0);
62130Sstevel@tonic-gate 		}
62140Sstevel@tonic-gate 		if (vpp == NULL) {
62150Sstevel@tonic-gate 			if (svd->prot != pageprot) {
62160Sstevel@tonic-gate 				SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
62170Sstevel@tonic-gate 				return (0);
62180Sstevel@tonic-gate 			}
62190Sstevel@tonic-gate 			prot = svd->prot;
62200Sstevel@tonic-gate 		} else
62210Sstevel@tonic-gate 			vpp = &svd->vpage[seg_page(seg, addr)];
62220Sstevel@tonic-gate 
62230Sstevel@tonic-gate 	} else if (svd->vp && svd->amp == NULL &&
62240Sstevel@tonic-gate 	    (flags & MS_INVALIDATE) == 0) {
62250Sstevel@tonic-gate 
62260Sstevel@tonic-gate 		/*
62270Sstevel@tonic-gate 		 * No attributes, no anonymous pages and MS_INVALIDATE flag
62280Sstevel@tonic-gate 		 * is not on, just use one big request.
62290Sstevel@tonic-gate 		 */
62300Sstevel@tonic-gate 		err = VOP_PUTPAGE(svd->vp, (offset_t)offset, len,
62310Sstevel@tonic-gate 		    bflags, svd->cred);
62320Sstevel@tonic-gate 		SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
62330Sstevel@tonic-gate 		return (err);
62340Sstevel@tonic-gate 	}
62350Sstevel@tonic-gate 
62360Sstevel@tonic-gate 	if ((amp = svd->amp) != NULL)
62370Sstevel@tonic-gate 		anon_index = svd->anon_index + seg_page(seg, addr);
62380Sstevel@tonic-gate 
62390Sstevel@tonic-gate 	for (eaddr = addr + len; addr < eaddr; addr += PAGESIZE) {
62400Sstevel@tonic-gate 		ap = NULL;
62410Sstevel@tonic-gate 		if (amp != NULL) {
62420Sstevel@tonic-gate 			ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
62430Sstevel@tonic-gate 			anon_array_enter(amp, anon_index, &cookie);
62440Sstevel@tonic-gate 			ap = anon_get_ptr(amp->ahp, anon_index++);
62450Sstevel@tonic-gate 			if (ap != NULL) {
62460Sstevel@tonic-gate 				swap_xlate(ap, &vp, &off);
62470Sstevel@tonic-gate 			} else {
62480Sstevel@tonic-gate 				vp = svd->vp;
62490Sstevel@tonic-gate 				off = offset;
62500Sstevel@tonic-gate 			}
62510Sstevel@tonic-gate 			anon_array_exit(&cookie);
62520Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
62530Sstevel@tonic-gate 		} else {
62540Sstevel@tonic-gate 			vp = svd->vp;
62550Sstevel@tonic-gate 			off = offset;
62560Sstevel@tonic-gate 		}
62570Sstevel@tonic-gate 		offset += PAGESIZE;
62580Sstevel@tonic-gate 
62590Sstevel@tonic-gate 		if (vp == NULL)		/* untouched zfod page */
62600Sstevel@tonic-gate 			continue;
62610Sstevel@tonic-gate 
62620Sstevel@tonic-gate 		if (attr) {
62630Sstevel@tonic-gate 			if (vpp) {
62640Sstevel@tonic-gate 				prot = VPP_PROT(vpp);
62650Sstevel@tonic-gate 				vpp++;
62660Sstevel@tonic-gate 			}
62670Sstevel@tonic-gate 			if (prot != pageprot) {
62680Sstevel@tonic-gate 				continue;
62690Sstevel@tonic-gate 			}
62700Sstevel@tonic-gate 		}
62710Sstevel@tonic-gate 
62720Sstevel@tonic-gate 		/*
62730Sstevel@tonic-gate 		 * See if any of these pages are locked --  if so, then we
62740Sstevel@tonic-gate 		 * will have to truncate an invalidate request at the first
62750Sstevel@tonic-gate 		 * locked one. We don't need the page_struct_lock to test
62760Sstevel@tonic-gate 		 * as this is only advisory; even if we acquire it someone
62770Sstevel@tonic-gate 		 * might race in and lock the page after we unlock and before
62780Sstevel@tonic-gate 		 * we do the PUTPAGE, then PUTPAGE simply does nothing.
62790Sstevel@tonic-gate 		 */
62800Sstevel@tonic-gate 		if (flags & MS_INVALIDATE) {
62810Sstevel@tonic-gate 			if ((pp = page_lookup(vp, off, SE_SHARED)) != NULL) {
62820Sstevel@tonic-gate 				if (pp->p_lckcnt != 0 || pp->p_cowcnt != 0) {
62830Sstevel@tonic-gate 					page_unlock(pp);
62840Sstevel@tonic-gate 					SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
62850Sstevel@tonic-gate 					return (EBUSY);
62860Sstevel@tonic-gate 				}
62870Sstevel@tonic-gate 				if (ap != NULL && pp->p_szc != 0 &&
62880Sstevel@tonic-gate 				    page_tryupgrade(pp)) {
62890Sstevel@tonic-gate 					if (pp->p_lckcnt == 0 &&
62900Sstevel@tonic-gate 					    pp->p_cowcnt == 0) {
62910Sstevel@tonic-gate 						/*
62920Sstevel@tonic-gate 						 * swapfs VN_DISPOSE() won't
62930Sstevel@tonic-gate 						 * invalidate large pages.
62940Sstevel@tonic-gate 						 * Attempt to demote.
62950Sstevel@tonic-gate 						 * XXX can't help it if it
62960Sstevel@tonic-gate 						 * fails. But for swapfs
62970Sstevel@tonic-gate 						 * pages it is no big deal.
62980Sstevel@tonic-gate 						 */
62990Sstevel@tonic-gate 						(void) page_try_demote_pages(
63000Sstevel@tonic-gate 						    pp);
63010Sstevel@tonic-gate 				    }
63020Sstevel@tonic-gate 				}
63030Sstevel@tonic-gate 				page_unlock(pp);
63040Sstevel@tonic-gate 			}
63050Sstevel@tonic-gate 		} else if (svd->type == MAP_SHARED && amp != NULL) {
63060Sstevel@tonic-gate 			/*
63070Sstevel@tonic-gate 			 * Avoid writting out to disk ISM's large pages
63080Sstevel@tonic-gate 			 * because segspt_free_pages() relies on NULL an_pvp
63090Sstevel@tonic-gate 			 * of anon slots of such pages.
63100Sstevel@tonic-gate 			 */
63110Sstevel@tonic-gate 
63120Sstevel@tonic-gate 			ASSERT(svd->vp == NULL);
63130Sstevel@tonic-gate 			/*
63140Sstevel@tonic-gate 			 * swapfs uses page_lookup_nowait if not freeing or
63150Sstevel@tonic-gate 			 * invalidating and skips a page if
63160Sstevel@tonic-gate 			 * page_lookup_nowait returns NULL.
63170Sstevel@tonic-gate 			 */
63180Sstevel@tonic-gate 			pp = page_lookup_nowait(vp, off, SE_SHARED);
63190Sstevel@tonic-gate 			if (pp == NULL) {
63200Sstevel@tonic-gate 				continue;
63210Sstevel@tonic-gate 			}
63220Sstevel@tonic-gate 			if (pp->p_szc != 0) {
63230Sstevel@tonic-gate 				page_unlock(pp);
63240Sstevel@tonic-gate 				continue;
63250Sstevel@tonic-gate 			}
63260Sstevel@tonic-gate 
63270Sstevel@tonic-gate 			/*
63280Sstevel@tonic-gate 			 * Note ISM pages are created large so (vp, off)'s
63290Sstevel@tonic-gate 			 * page cannot suddenly become large after we unlock
63300Sstevel@tonic-gate 			 * pp.
63310Sstevel@tonic-gate 			 */
63320Sstevel@tonic-gate 			page_unlock(pp);
63330Sstevel@tonic-gate 		}
63340Sstevel@tonic-gate 		/*
63350Sstevel@tonic-gate 		 * XXX - Should ultimately try to kluster
63360Sstevel@tonic-gate 		 * calls to VOP_PUTPAGE() for performance.
63370Sstevel@tonic-gate 		 */
63380Sstevel@tonic-gate 		VN_HOLD(vp);
63390Sstevel@tonic-gate 		err = VOP_PUTPAGE(vp, (offset_t)off, PAGESIZE,
63400Sstevel@tonic-gate 		    bflags, svd->cred);
63410Sstevel@tonic-gate 		VN_RELE(vp);
63420Sstevel@tonic-gate 		if (err)
63430Sstevel@tonic-gate 			break;
63440Sstevel@tonic-gate 	}
63450Sstevel@tonic-gate 	SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
63460Sstevel@tonic-gate 	return (err);
63470Sstevel@tonic-gate }
63480Sstevel@tonic-gate 
63490Sstevel@tonic-gate /*
63500Sstevel@tonic-gate  * Determine if we have data corresponding to pages in the
63510Sstevel@tonic-gate  * primary storage virtual memory cache (i.e., "in core").
63520Sstevel@tonic-gate  */
63530Sstevel@tonic-gate static size_t
63540Sstevel@tonic-gate segvn_incore(struct seg *seg, caddr_t addr, size_t len, char *vec)
63550Sstevel@tonic-gate {
63560Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
63570Sstevel@tonic-gate 	struct vnode *vp, *avp;
63580Sstevel@tonic-gate 	u_offset_t offset, aoffset;
63590Sstevel@tonic-gate 	size_t p, ep;
63600Sstevel@tonic-gate 	int ret;
63610Sstevel@tonic-gate 	struct vpage *vpp;
63620Sstevel@tonic-gate 	page_t *pp;
63630Sstevel@tonic-gate 	uint_t start;
63640Sstevel@tonic-gate 	struct anon_map *amp;		/* XXX - for locknest */
63650Sstevel@tonic-gate 	struct anon *ap;
63660Sstevel@tonic-gate 	uint_t attr;
63670Sstevel@tonic-gate 	anon_sync_obj_t cookie;
63680Sstevel@tonic-gate 
63690Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
63700Sstevel@tonic-gate 
63710Sstevel@tonic-gate 	SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
63720Sstevel@tonic-gate 	if (svd->amp == NULL && svd->vp == NULL) {
63730Sstevel@tonic-gate 		SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
63740Sstevel@tonic-gate 		bzero(vec, btopr(len));
63750Sstevel@tonic-gate 		return (len);	/* no anonymous pages created yet */
63760Sstevel@tonic-gate 	}
63770Sstevel@tonic-gate 
63780Sstevel@tonic-gate 	p = seg_page(seg, addr);
63790Sstevel@tonic-gate 	ep = seg_page(seg, addr + len);
63800Sstevel@tonic-gate 	start = svd->vp ? SEG_PAGE_VNODEBACKED : 0;
63810Sstevel@tonic-gate 
63820Sstevel@tonic-gate 	amp = svd->amp;
63830Sstevel@tonic-gate 	for (; p < ep; p++, addr += PAGESIZE) {
63840Sstevel@tonic-gate 		vpp = (svd->vpage) ? &svd->vpage[p]: NULL;
63850Sstevel@tonic-gate 		ret = start;
63860Sstevel@tonic-gate 		ap = NULL;
63870Sstevel@tonic-gate 		avp = NULL;
63880Sstevel@tonic-gate 		/* Grab the vnode/offset for the anon slot */
63890Sstevel@tonic-gate 		if (amp != NULL) {
63900Sstevel@tonic-gate 			ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
63910Sstevel@tonic-gate 			anon_array_enter(amp, svd->anon_index + p, &cookie);
63920Sstevel@tonic-gate 			ap = anon_get_ptr(amp->ahp, svd->anon_index + p);
63930Sstevel@tonic-gate 			if (ap != NULL) {
63940Sstevel@tonic-gate 				swap_xlate(ap, &avp, &aoffset);
63950Sstevel@tonic-gate 			}
63960Sstevel@tonic-gate 			anon_array_exit(&cookie);
63970Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
63980Sstevel@tonic-gate 		}
63990Sstevel@tonic-gate 		if ((avp != NULL) && page_exists(avp, aoffset)) {
64000Sstevel@tonic-gate 			/* A page exists for the anon slot */
64010Sstevel@tonic-gate 			ret |= SEG_PAGE_INCORE;
64020Sstevel@tonic-gate 
64030Sstevel@tonic-gate 			/*
64040Sstevel@tonic-gate 			 * If page is mapped and writable
64050Sstevel@tonic-gate 			 */
64060Sstevel@tonic-gate 			attr = (uint_t)0;
64070Sstevel@tonic-gate 			if ((hat_getattr(seg->s_as->a_hat, addr,
64080Sstevel@tonic-gate 			    &attr) != -1) && (attr & PROT_WRITE)) {
64090Sstevel@tonic-gate 				ret |= SEG_PAGE_ANON;
64100Sstevel@tonic-gate 			}
64110Sstevel@tonic-gate 			/*
64120Sstevel@tonic-gate 			 * Don't get page_struct lock for lckcnt and cowcnt,
64130Sstevel@tonic-gate 			 * since this is purely advisory.
64140Sstevel@tonic-gate 			 */
64150Sstevel@tonic-gate 			if ((pp = page_lookup_nowait(avp, aoffset,
64160Sstevel@tonic-gate 			    SE_SHARED)) != NULL) {
64170Sstevel@tonic-gate 				if (pp->p_lckcnt)
64180Sstevel@tonic-gate 					ret |= SEG_PAGE_SOFTLOCK;
64190Sstevel@tonic-gate 				if (pp->p_cowcnt)
64200Sstevel@tonic-gate 					ret |= SEG_PAGE_HASCOW;
64210Sstevel@tonic-gate 				page_unlock(pp);
64220Sstevel@tonic-gate 			}
64230Sstevel@tonic-gate 		}
64240Sstevel@tonic-gate 
64250Sstevel@tonic-gate 		/* Gather vnode statistics */
64260Sstevel@tonic-gate 		vp = svd->vp;
64270Sstevel@tonic-gate 		offset = svd->offset + (uintptr_t)(addr - seg->s_base);
64280Sstevel@tonic-gate 
64290Sstevel@tonic-gate 		if (vp != NULL) {
64300Sstevel@tonic-gate 			/*
64310Sstevel@tonic-gate 			 * Try to obtain a "shared" lock on the page
64320Sstevel@tonic-gate 			 * without blocking.  If this fails, determine
64330Sstevel@tonic-gate 			 * if the page is in memory.
64340Sstevel@tonic-gate 			 */
64350Sstevel@tonic-gate 			pp = page_lookup_nowait(vp, offset, SE_SHARED);
64360Sstevel@tonic-gate 			if ((pp == NULL) && (page_exists(vp, offset))) {
64370Sstevel@tonic-gate 				/* Page is incore, and is named */
64380Sstevel@tonic-gate 				ret |= (SEG_PAGE_INCORE | SEG_PAGE_VNODE);
64390Sstevel@tonic-gate 			}
64400Sstevel@tonic-gate 			/*
64410Sstevel@tonic-gate 			 * Don't get page_struct lock for lckcnt and cowcnt,
64420Sstevel@tonic-gate 			 * since this is purely advisory.
64430Sstevel@tonic-gate 			 */
64440Sstevel@tonic-gate 			if (pp != NULL) {
64450Sstevel@tonic-gate 				ret |= (SEG_PAGE_INCORE | SEG_PAGE_VNODE);
64460Sstevel@tonic-gate 				if (pp->p_lckcnt)
64470Sstevel@tonic-gate 					ret |= SEG_PAGE_SOFTLOCK;
64480Sstevel@tonic-gate 				if (pp->p_cowcnt)
64490Sstevel@tonic-gate 					ret |= SEG_PAGE_HASCOW;
64500Sstevel@tonic-gate 				page_unlock(pp);
64510Sstevel@tonic-gate 			}
64520Sstevel@tonic-gate 		}
64530Sstevel@tonic-gate 
64540Sstevel@tonic-gate 		/* Gather virtual page information */
64550Sstevel@tonic-gate 		if (vpp) {
64560Sstevel@tonic-gate 			if (VPP_ISPPLOCK(vpp))
64570Sstevel@tonic-gate 				ret |= SEG_PAGE_LOCKED;
64580Sstevel@tonic-gate 			vpp++;
64590Sstevel@tonic-gate 		}
64600Sstevel@tonic-gate 
64610Sstevel@tonic-gate 		*vec++ = (char)ret;
64620Sstevel@tonic-gate 	}
64630Sstevel@tonic-gate 	SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
64640Sstevel@tonic-gate 	return (len);
64650Sstevel@tonic-gate }
64660Sstevel@tonic-gate 
64670Sstevel@tonic-gate /*
64680Sstevel@tonic-gate  * Statement for p_cowcnts/p_lckcnts.
64690Sstevel@tonic-gate  *
64700Sstevel@tonic-gate  * p_cowcnt is updated while mlock/munlocking MAP_PRIVATE and PROT_WRITE region
64710Sstevel@tonic-gate  * irrespective of the following factors or anything else:
64720Sstevel@tonic-gate  *
64730Sstevel@tonic-gate  *	(1) anon slots are populated or not
64740Sstevel@tonic-gate  *	(2) cow is broken or not
64750Sstevel@tonic-gate  *	(3) refcnt on ap is 1 or greater than 1
64760Sstevel@tonic-gate  *
64770Sstevel@tonic-gate  * If it's not MAP_PRIVATE and PROT_WRITE, p_lckcnt is updated during mlock
64780Sstevel@tonic-gate  * and munlock.
64790Sstevel@tonic-gate  *
64800Sstevel@tonic-gate  *
64810Sstevel@tonic-gate  * Handling p_cowcnts/p_lckcnts during copy-on-write fault:
64820Sstevel@tonic-gate  *
64830Sstevel@tonic-gate  *	if vpage has PROT_WRITE
64840Sstevel@tonic-gate  *		transfer cowcnt on the oldpage -> cowcnt on the newpage
64850Sstevel@tonic-gate  *	else
64860Sstevel@tonic-gate  *		transfer lckcnt on the oldpage -> lckcnt on the newpage
64870Sstevel@tonic-gate  *
64880Sstevel@tonic-gate  *	During copy-on-write, decrement p_cowcnt on the oldpage and increment
64890Sstevel@tonic-gate  *	p_cowcnt on the newpage *if* the corresponding vpage has PROT_WRITE.
64900Sstevel@tonic-gate  *
64910Sstevel@tonic-gate  *	We may also break COW if softlocking on read access in the physio case.
64920Sstevel@tonic-gate  *	In this case, vpage may not have PROT_WRITE. So, we need to decrement
64930Sstevel@tonic-gate  *	p_lckcnt on the oldpage and increment p_lckcnt on the newpage *if* the
64940Sstevel@tonic-gate  *	vpage doesn't have PROT_WRITE.
64950Sstevel@tonic-gate  *
64960Sstevel@tonic-gate  *
64970Sstevel@tonic-gate  * Handling p_cowcnts/p_lckcnts during mprotect on mlocked region:
64980Sstevel@tonic-gate  *
64990Sstevel@tonic-gate  * 	If a MAP_PRIVATE region loses PROT_WRITE, we decrement p_cowcnt and
65000Sstevel@tonic-gate  *	increment p_lckcnt by calling page_subclaim() which takes care of
65010Sstevel@tonic-gate  * 	availrmem accounting and p_lckcnt overflow.
65020Sstevel@tonic-gate  *
65030Sstevel@tonic-gate  *	If a MAP_PRIVATE region gains PROT_WRITE, we decrement p_lckcnt and
65040Sstevel@tonic-gate  *	increment p_cowcnt by calling page_addclaim() which takes care of
65050Sstevel@tonic-gate  *	availrmem availability and p_cowcnt overflow.
65060Sstevel@tonic-gate  */
65070Sstevel@tonic-gate 
65080Sstevel@tonic-gate /*
65090Sstevel@tonic-gate  * Lock down (or unlock) pages mapped by this segment.
65100Sstevel@tonic-gate  *
65110Sstevel@tonic-gate  * XXX only creates PAGESIZE pages if anon slots are not initialized.
65120Sstevel@tonic-gate  * At fault time they will be relocated into larger pages.
65130Sstevel@tonic-gate  */
65140Sstevel@tonic-gate static int
65150Sstevel@tonic-gate segvn_lockop(struct seg *seg, caddr_t addr, size_t len,
65160Sstevel@tonic-gate     int attr, int op, ulong_t *lockmap, size_t pos)
65170Sstevel@tonic-gate {
65180Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
65190Sstevel@tonic-gate 	struct vpage *vpp;
65200Sstevel@tonic-gate 	struct vpage *evp;
65210Sstevel@tonic-gate 	page_t *pp;
65220Sstevel@tonic-gate 	u_offset_t offset;
65230Sstevel@tonic-gate 	u_offset_t off;
65240Sstevel@tonic-gate 	int segtype;
65250Sstevel@tonic-gate 	int pageprot;
65260Sstevel@tonic-gate 	int claim;
65270Sstevel@tonic-gate 	struct vnode *vp;
65280Sstevel@tonic-gate 	ulong_t anon_index;
65290Sstevel@tonic-gate 	struct anon_map *amp;
65300Sstevel@tonic-gate 	struct anon *ap;
65310Sstevel@tonic-gate 	struct vattr va;
65320Sstevel@tonic-gate 	anon_sync_obj_t cookie;
65330Sstevel@tonic-gate 
65340Sstevel@tonic-gate 	/*
65350Sstevel@tonic-gate 	 * Hold write lock on address space because may split or concatenate
65360Sstevel@tonic-gate 	 * segments
65370Sstevel@tonic-gate 	 */
65380Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
65390Sstevel@tonic-gate 
65400Sstevel@tonic-gate 	SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_WRITER);
65410Sstevel@tonic-gate 	if (attr) {
65420Sstevel@tonic-gate 		pageprot = attr & ~(SHARED|PRIVATE);
65430Sstevel@tonic-gate 		segtype = attr & SHARED ? MAP_SHARED : MAP_PRIVATE;
65440Sstevel@tonic-gate 
65450Sstevel@tonic-gate 		/*
65460Sstevel@tonic-gate 		 * We are done if the segment types don't match
65470Sstevel@tonic-gate 		 * or if we have segment level protections and
65480Sstevel@tonic-gate 		 * they don't match.
65490Sstevel@tonic-gate 		 */
65500Sstevel@tonic-gate 		if (svd->type != segtype) {
65510Sstevel@tonic-gate 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
65520Sstevel@tonic-gate 			return (0);
65530Sstevel@tonic-gate 		}
65540Sstevel@tonic-gate 		if (svd->pageprot == 0 && svd->prot != pageprot) {
65550Sstevel@tonic-gate 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
65560Sstevel@tonic-gate 			return (0);
65570Sstevel@tonic-gate 		}
65580Sstevel@tonic-gate 	}
65590Sstevel@tonic-gate 
65600Sstevel@tonic-gate 	/*
65610Sstevel@tonic-gate 	 * If we're locking, then we must create a vpage structure if
65620Sstevel@tonic-gate 	 * none exists.  If we're unlocking, then check to see if there
65630Sstevel@tonic-gate 	 * is a vpage --  if not, then we could not have locked anything.
65640Sstevel@tonic-gate 	 */
65650Sstevel@tonic-gate 
65660Sstevel@tonic-gate 	if ((vpp = svd->vpage) == NULL) {
65670Sstevel@tonic-gate 		if (op == MC_LOCK)
65680Sstevel@tonic-gate 			segvn_vpage(seg);
65690Sstevel@tonic-gate 		else {
65700Sstevel@tonic-gate 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
65710Sstevel@tonic-gate 			return (0);
65720Sstevel@tonic-gate 		}
65730Sstevel@tonic-gate 	}
65740Sstevel@tonic-gate 
65750Sstevel@tonic-gate 	/*
65760Sstevel@tonic-gate 	 * The anonymous data vector (i.e., previously
65770Sstevel@tonic-gate 	 * unreferenced mapping to swap space) can be allocated
65780Sstevel@tonic-gate 	 * by lazily testing for its existence.
65790Sstevel@tonic-gate 	 */
65800Sstevel@tonic-gate 	if (op == MC_LOCK && svd->amp == NULL && svd->vp == NULL) {
65810Sstevel@tonic-gate 		svd->amp = anonmap_alloc(seg->s_size, 0);
65820Sstevel@tonic-gate 		svd->amp->a_szc = seg->s_szc;
65830Sstevel@tonic-gate 	}
65840Sstevel@tonic-gate 
65850Sstevel@tonic-gate 	if ((amp = svd->amp) != NULL) {
65860Sstevel@tonic-gate 		anon_index = svd->anon_index + seg_page(seg, addr);
65870Sstevel@tonic-gate 	}
65880Sstevel@tonic-gate 
65890Sstevel@tonic-gate 	offset = svd->offset + (uintptr_t)(addr - seg->s_base);
65900Sstevel@tonic-gate 	evp = &svd->vpage[seg_page(seg, addr + len)];
65910Sstevel@tonic-gate 
65920Sstevel@tonic-gate 	/*
65930Sstevel@tonic-gate 	 * Loop over all pages in the range.  Process if we're locking and
65940Sstevel@tonic-gate 	 * page has not already been locked in this mapping; or if we're
65950Sstevel@tonic-gate 	 * unlocking and the page has been locked.
65960Sstevel@tonic-gate 	 */
65970Sstevel@tonic-gate 	for (vpp = &svd->vpage[seg_page(seg, addr)]; vpp < evp;
65980Sstevel@tonic-gate 	    vpp++, pos++, addr += PAGESIZE, offset += PAGESIZE, anon_index++) {
65990Sstevel@tonic-gate 		if ((attr == 0 || VPP_PROT(vpp) == pageprot) &&
66000Sstevel@tonic-gate 		    ((op == MC_LOCK && !VPP_ISPPLOCK(vpp)) ||
66010Sstevel@tonic-gate 		    (op == MC_UNLOCK && VPP_ISPPLOCK(vpp)))) {
66020Sstevel@tonic-gate 
66030Sstevel@tonic-gate 			if (amp != NULL)
66040Sstevel@tonic-gate 				ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
66050Sstevel@tonic-gate 			/*
66060Sstevel@tonic-gate 			 * If this isn't a MAP_NORESERVE segment and
66070Sstevel@tonic-gate 			 * we're locking, allocate anon slots if they
66080Sstevel@tonic-gate 			 * don't exist.  The page is brought in later on.
66090Sstevel@tonic-gate 			 */
66100Sstevel@tonic-gate 			if (op == MC_LOCK && svd->vp == NULL &&
66110Sstevel@tonic-gate 			    ((svd->flags & MAP_NORESERVE) == 0) &&
66120Sstevel@tonic-gate 			    amp != NULL &&
66130Sstevel@tonic-gate 			    ((ap = anon_get_ptr(amp->ahp, anon_index))
66140Sstevel@tonic-gate 								== NULL)) {
66150Sstevel@tonic-gate 				anon_array_enter(amp, anon_index, &cookie);
66160Sstevel@tonic-gate 
66170Sstevel@tonic-gate 				if ((ap = anon_get_ptr(amp->ahp,
66180Sstevel@tonic-gate 						anon_index)) == NULL) {
66190Sstevel@tonic-gate 					pp = anon_zero(seg, addr, &ap,
66200Sstevel@tonic-gate 					    svd->cred);
66210Sstevel@tonic-gate 					if (pp == NULL) {
66220Sstevel@tonic-gate 						anon_array_exit(&cookie);
66230Sstevel@tonic-gate 						ANON_LOCK_EXIT(&amp->a_rwlock);
66240Sstevel@tonic-gate 						SEGVN_LOCK_EXIT(seg->s_as,
66250Sstevel@tonic-gate 						    &svd->lock);
66260Sstevel@tonic-gate 						return (ENOMEM);
66270Sstevel@tonic-gate 					}
66280Sstevel@tonic-gate 					ASSERT(anon_get_ptr(amp->ahp,
66290Sstevel@tonic-gate 						anon_index) == NULL);
66300Sstevel@tonic-gate 					(void) anon_set_ptr(amp->ahp,
66310Sstevel@tonic-gate 						anon_index, ap, ANON_SLEEP);
66320Sstevel@tonic-gate 					page_unlock(pp);
66330Sstevel@tonic-gate 				}
66340Sstevel@tonic-gate 				anon_array_exit(&cookie);
66350Sstevel@tonic-gate 			}
66360Sstevel@tonic-gate 
66370Sstevel@tonic-gate 			/*
66380Sstevel@tonic-gate 			 * Get name for page, accounting for
66390Sstevel@tonic-gate 			 * existence of private copy.
66400Sstevel@tonic-gate 			 */
66410Sstevel@tonic-gate 			ap = NULL;
66420Sstevel@tonic-gate 			if (amp != NULL) {
66430Sstevel@tonic-gate 				anon_array_enter(amp, anon_index, &cookie);
66440Sstevel@tonic-gate 				ap = anon_get_ptr(amp->ahp, anon_index);
66450Sstevel@tonic-gate 				if (ap != NULL) {
66460Sstevel@tonic-gate 					swap_xlate(ap, &vp, &off);
66470Sstevel@tonic-gate 				} else {
66480Sstevel@tonic-gate 					if (svd->vp == NULL &&
66490Sstevel@tonic-gate 					    (svd->flags & MAP_NORESERVE)) {
66500Sstevel@tonic-gate 						anon_array_exit(&cookie);
66510Sstevel@tonic-gate 						ANON_LOCK_EXIT(&amp->a_rwlock);
66520Sstevel@tonic-gate 						continue;
66530Sstevel@tonic-gate 					}
66540Sstevel@tonic-gate 					vp = svd->vp;
66550Sstevel@tonic-gate 					off = offset;
66560Sstevel@tonic-gate 				}
66570Sstevel@tonic-gate 				anon_array_exit(&cookie);
66580Sstevel@tonic-gate 				ANON_LOCK_EXIT(&amp->a_rwlock);
66590Sstevel@tonic-gate 			} else {
66600Sstevel@tonic-gate 				vp = svd->vp;
66610Sstevel@tonic-gate 				off = offset;
66620Sstevel@tonic-gate 			}
66630Sstevel@tonic-gate 
66640Sstevel@tonic-gate 			/*
66650Sstevel@tonic-gate 			 * Get page frame.  It's ok if the page is
66660Sstevel@tonic-gate 			 * not available when we're unlocking, as this
66670Sstevel@tonic-gate 			 * may simply mean that a page we locked got
66680Sstevel@tonic-gate 			 * truncated out of existence after we locked it.
66690Sstevel@tonic-gate 			 *
66700Sstevel@tonic-gate 			 * Invoke VOP_GETPAGE() to obtain the page struct
66710Sstevel@tonic-gate 			 * since we may need to read it from disk if its
66720Sstevel@tonic-gate 			 * been paged out.
66730Sstevel@tonic-gate 			 */
66740Sstevel@tonic-gate 			if (op != MC_LOCK)
66750Sstevel@tonic-gate 				pp = page_lookup(vp, off, SE_SHARED);
66760Sstevel@tonic-gate 			else {
66770Sstevel@tonic-gate 				page_t *pl[1 + 1];
66780Sstevel@tonic-gate 				int error;
66790Sstevel@tonic-gate 
66800Sstevel@tonic-gate 				ASSERT(vp != NULL);
66810Sstevel@tonic-gate 
66820Sstevel@tonic-gate 				error = VOP_GETPAGE(vp, (offset_t)off, PAGESIZE,
66830Sstevel@tonic-gate 				    (uint_t *)NULL, pl, PAGESIZE, seg, addr,
66840Sstevel@tonic-gate 				    S_OTHER, svd->cred);
66850Sstevel@tonic-gate 
66860Sstevel@tonic-gate 				/*
66870Sstevel@tonic-gate 				 * If the error is EDEADLK then we must bounce
66880Sstevel@tonic-gate 				 * up and drop all vm subsystem locks and then
66890Sstevel@tonic-gate 				 * retry the operation later
66900Sstevel@tonic-gate 				 * This behavior is a temporary measure because
66910Sstevel@tonic-gate 				 * ufs/sds logging is badly designed and will
66920Sstevel@tonic-gate 				 * deadlock if we don't allow this bounce to
66930Sstevel@tonic-gate 				 * happen.  The real solution is to re-design
66940Sstevel@tonic-gate 				 * the logging code to work properly.  See bug
66950Sstevel@tonic-gate 				 * 4125102 for details of the problem.
66960Sstevel@tonic-gate 				 */
66970Sstevel@tonic-gate 				if (error == EDEADLK) {
66980Sstevel@tonic-gate 					SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
66990Sstevel@tonic-gate 					return (error);
67000Sstevel@tonic-gate 				}
67010Sstevel@tonic-gate 				/*
67020Sstevel@tonic-gate 				 * Quit if we fail to fault in the page.  Treat
67030Sstevel@tonic-gate 				 * the failure as an error, unless the addr
67040Sstevel@tonic-gate 				 * is mapped beyond the end of a file.
67050Sstevel@tonic-gate 				 */
67060Sstevel@tonic-gate 				if (error && svd->vp) {
67070Sstevel@tonic-gate 					va.va_mask = AT_SIZE;
67080Sstevel@tonic-gate 					if (VOP_GETATTR(svd->vp, &va, 0,
67090Sstevel@tonic-gate 					    svd->cred) != 0) {
67100Sstevel@tonic-gate 						SEGVN_LOCK_EXIT(seg->s_as,
67110Sstevel@tonic-gate 						    &svd->lock);
67120Sstevel@tonic-gate 						return (EIO);
67130Sstevel@tonic-gate 					}
67140Sstevel@tonic-gate 					if (btopr(va.va_size) >=
67150Sstevel@tonic-gate 					    btopr(off + 1)) {
67160Sstevel@tonic-gate 						SEGVN_LOCK_EXIT(seg->s_as,
67170Sstevel@tonic-gate 						    &svd->lock);
67180Sstevel@tonic-gate 						return (EIO);
67190Sstevel@tonic-gate 					}
67200Sstevel@tonic-gate 					SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
67210Sstevel@tonic-gate 					return (0);
67220Sstevel@tonic-gate 				} else if (error) {
67230Sstevel@tonic-gate 					SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
67240Sstevel@tonic-gate 					return (EIO);
67250Sstevel@tonic-gate 				}
67260Sstevel@tonic-gate 				pp = pl[0];
67270Sstevel@tonic-gate 				ASSERT(pp != NULL);
67280Sstevel@tonic-gate 			}
67290Sstevel@tonic-gate 
67300Sstevel@tonic-gate 			/*
67310Sstevel@tonic-gate 			 * See Statement at the beginning of this routine.
67320Sstevel@tonic-gate 			 *
67330Sstevel@tonic-gate 			 * claim is always set if MAP_PRIVATE and PROT_WRITE
67340Sstevel@tonic-gate 			 * irrespective of following factors:
67350Sstevel@tonic-gate 			 *
67360Sstevel@tonic-gate 			 * (1) anon slots are populated or not
67370Sstevel@tonic-gate 			 * (2) cow is broken or not
67380Sstevel@tonic-gate 			 * (3) refcnt on ap is 1 or greater than 1
67390Sstevel@tonic-gate 			 *
67400Sstevel@tonic-gate 			 * See 4140683 for details
67410Sstevel@tonic-gate 			 */
67420Sstevel@tonic-gate 			claim = ((VPP_PROT(vpp) & PROT_WRITE) &&
67430Sstevel@tonic-gate 				(svd->type == MAP_PRIVATE));
67440Sstevel@tonic-gate 
67450Sstevel@tonic-gate 			/*
67460Sstevel@tonic-gate 			 * Perform page-level operation appropriate to
67470Sstevel@tonic-gate 			 * operation.  If locking, undo the SOFTLOCK
67480Sstevel@tonic-gate 			 * performed to bring the page into memory
67490Sstevel@tonic-gate 			 * after setting the lock.  If unlocking,
67500Sstevel@tonic-gate 			 * and no page was found, account for the claim
67510Sstevel@tonic-gate 			 * separately.
67520Sstevel@tonic-gate 			 */
67530Sstevel@tonic-gate 			if (op == MC_LOCK) {
67540Sstevel@tonic-gate 				int ret = 1;	/* Assume success */
67550Sstevel@tonic-gate 
67560Sstevel@tonic-gate 				/*
67570Sstevel@tonic-gate 				 * Make sure another thread didn't lock
67580Sstevel@tonic-gate 				 * the page after we released the segment
67590Sstevel@tonic-gate 				 * lock.
67600Sstevel@tonic-gate 				 */
67610Sstevel@tonic-gate 				if ((attr == 0 || VPP_PROT(vpp) == pageprot) &&
67620Sstevel@tonic-gate 				    !VPP_ISPPLOCK(vpp)) {
67630Sstevel@tonic-gate 					ret = page_pp_lock(pp, claim, 0);
67640Sstevel@tonic-gate 					if (ret != 0) {
67650Sstevel@tonic-gate 						VPP_SETPPLOCK(vpp);
67660Sstevel@tonic-gate 						if (lockmap != (ulong_t *)NULL)
67670Sstevel@tonic-gate 							BT_SET(lockmap, pos);
67680Sstevel@tonic-gate 					}
67690Sstevel@tonic-gate 				}
67700Sstevel@tonic-gate 				page_unlock(pp);
67710Sstevel@tonic-gate 				if (ret == 0) {
67720Sstevel@tonic-gate 					SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
67730Sstevel@tonic-gate 					return (EAGAIN);
67740Sstevel@tonic-gate 				}
67750Sstevel@tonic-gate 			} else {
67760Sstevel@tonic-gate 				if (pp != NULL) {
67770Sstevel@tonic-gate 					if ((attr == 0 ||
67780Sstevel@tonic-gate 					    VPP_PROT(vpp) == pageprot) &&
67790Sstevel@tonic-gate 					    VPP_ISPPLOCK(vpp))
67800Sstevel@tonic-gate 						page_pp_unlock(pp, claim, 0);
67810Sstevel@tonic-gate 					page_unlock(pp);
67820Sstevel@tonic-gate 				}
67830Sstevel@tonic-gate 				VPP_CLRPPLOCK(vpp);
67840Sstevel@tonic-gate 			}
67850Sstevel@tonic-gate 		}
67860Sstevel@tonic-gate 	}
67870Sstevel@tonic-gate 	SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
67880Sstevel@tonic-gate 	return (0);
67890Sstevel@tonic-gate }
67900Sstevel@tonic-gate 
67910Sstevel@tonic-gate /*
67920Sstevel@tonic-gate  * Set advice from user for specified pages
67930Sstevel@tonic-gate  * There are 5 types of advice:
67940Sstevel@tonic-gate  *	MADV_NORMAL	- Normal (default) behavior (whatever that is)
67950Sstevel@tonic-gate  *	MADV_RANDOM	- Random page references
67960Sstevel@tonic-gate  *				do not allow readahead or 'klustering'
67970Sstevel@tonic-gate  *	MADV_SEQUENTIAL	- Sequential page references
67980Sstevel@tonic-gate  *				Pages previous to the one currently being
67990Sstevel@tonic-gate  *				accessed (determined by fault) are 'not needed'
68000Sstevel@tonic-gate  *				and are freed immediately
68010Sstevel@tonic-gate  *	MADV_WILLNEED	- Pages are likely to be used (fault ahead in mctl)
68020Sstevel@tonic-gate  *	MADV_DONTNEED	- Pages are not needed (synced out in mctl)
68030Sstevel@tonic-gate  *	MADV_FREE	- Contents can be discarded
68040Sstevel@tonic-gate  *	MADV_ACCESS_DEFAULT- Default access
68050Sstevel@tonic-gate  *	MADV_ACCESS_LWP	- Next LWP will access heavily
68060Sstevel@tonic-gate  *	MADV_ACCESS_MANY- Many LWPs or processes will access heavily
68070Sstevel@tonic-gate  */
68080Sstevel@tonic-gate static int
68090Sstevel@tonic-gate segvn_advise(struct seg *seg, caddr_t addr, size_t len, uint_t behav)
68100Sstevel@tonic-gate {
68110Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
68120Sstevel@tonic-gate 	size_t page;
68130Sstevel@tonic-gate 	int err = 0;
68140Sstevel@tonic-gate 	int already_set;
68150Sstevel@tonic-gate 	struct anon_map *amp;
68160Sstevel@tonic-gate 	ulong_t anon_index;
68170Sstevel@tonic-gate 	struct seg *next;
68180Sstevel@tonic-gate 	lgrp_mem_policy_t policy;
68190Sstevel@tonic-gate 	struct seg *prev;
68200Sstevel@tonic-gate 	struct vnode *vp;
68210Sstevel@tonic-gate 
68220Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
68230Sstevel@tonic-gate 
68240Sstevel@tonic-gate 	/*
68250Sstevel@tonic-gate 	 * In case of MADV_FREE, we won't be modifying any segment private
68260Sstevel@tonic-gate 	 * data structures; so, we only need to grab READER's lock
68270Sstevel@tonic-gate 	 */
68280Sstevel@tonic-gate 	if (behav != MADV_FREE)
68290Sstevel@tonic-gate 		SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_WRITER);
68300Sstevel@tonic-gate 	else
68310Sstevel@tonic-gate 		SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
68320Sstevel@tonic-gate 
68330Sstevel@tonic-gate 	/*
68340Sstevel@tonic-gate 	 * Large pages are assumed to be only turned on when accesses to the
68350Sstevel@tonic-gate 	 * segment's address range have spatial and temporal locality. That
68360Sstevel@tonic-gate 	 * justifies ignoring MADV_SEQUENTIAL for large page segments.
68370Sstevel@tonic-gate 	 * Also, ignore advice affecting lgroup memory allocation
68380Sstevel@tonic-gate 	 * if don't need to do lgroup optimizations on this system
68390Sstevel@tonic-gate 	 */
68400Sstevel@tonic-gate 
68410Sstevel@tonic-gate 	if ((behav == MADV_SEQUENTIAL && seg->s_szc != 0) ||
68420Sstevel@tonic-gate 	    (!lgrp_optimizations() && (behav == MADV_ACCESS_DEFAULT ||
68430Sstevel@tonic-gate 	    behav == MADV_ACCESS_LWP || behav == MADV_ACCESS_MANY))) {
68440Sstevel@tonic-gate 		SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
68450Sstevel@tonic-gate 		return (0);
68460Sstevel@tonic-gate 	}
68470Sstevel@tonic-gate 
68480Sstevel@tonic-gate 	if (behav == MADV_SEQUENTIAL || behav == MADV_ACCESS_DEFAULT ||
68490Sstevel@tonic-gate 	    behav == MADV_ACCESS_LWP || behav == MADV_ACCESS_MANY) {
68500Sstevel@tonic-gate 		/*
68510Sstevel@tonic-gate 		 * Since we are going to unload hat mappings
68520Sstevel@tonic-gate 		 * we first have to flush the cache. Otherwise
68530Sstevel@tonic-gate 		 * this might lead to system panic if another
68540Sstevel@tonic-gate 		 * thread is doing physio on the range whose
68550Sstevel@tonic-gate 		 * mappings are unloaded by madvise(3C).
68560Sstevel@tonic-gate 		 */
68570Sstevel@tonic-gate 		if (svd->softlockcnt > 0) {
68580Sstevel@tonic-gate 			/*
68590Sstevel@tonic-gate 			 * Since we do have the segvn writers lock
68600Sstevel@tonic-gate 			 * nobody can fill the cache with entries
68610Sstevel@tonic-gate 			 * belonging to this seg during the purge.
68620Sstevel@tonic-gate 			 * The flush either succeeds or we still
68630Sstevel@tonic-gate 			 * have pending I/Os. In the later case,
68640Sstevel@tonic-gate 			 * madvise(3C) fails.
68650Sstevel@tonic-gate 			 */
68660Sstevel@tonic-gate 			segvn_purge(seg);
68670Sstevel@tonic-gate 			if (svd->softlockcnt > 0) {
68680Sstevel@tonic-gate 				/*
68690Sstevel@tonic-gate 				 * Since madvise(3C) is advisory and
68700Sstevel@tonic-gate 				 * it's not part of UNIX98, madvise(3C)
68710Sstevel@tonic-gate 				 * failure here doesn't cause any hardship.
68720Sstevel@tonic-gate 				 * Note that we don't block in "as" layer.
68730Sstevel@tonic-gate 				 */
68740Sstevel@tonic-gate 				SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
68750Sstevel@tonic-gate 				return (EAGAIN);
68760Sstevel@tonic-gate 			}
68770Sstevel@tonic-gate 		}
68780Sstevel@tonic-gate 	}
68790Sstevel@tonic-gate 
68800Sstevel@tonic-gate 	amp = svd->amp;
68810Sstevel@tonic-gate 	vp = svd->vp;
68820Sstevel@tonic-gate 	if (behav == MADV_FREE) {
68830Sstevel@tonic-gate 		/*
68840Sstevel@tonic-gate 		 * MADV_FREE is not supported for segments with
68850Sstevel@tonic-gate 		 * underlying object; if anonmap is NULL, anon slots
68860Sstevel@tonic-gate 		 * are not yet populated and there is nothing for
68870Sstevel@tonic-gate 		 * us to do. As MADV_FREE is advisory, we don't
68880Sstevel@tonic-gate 		 * return error in either case.
68890Sstevel@tonic-gate 		 */
68900Sstevel@tonic-gate 		if (vp || amp == NULL) {
68910Sstevel@tonic-gate 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
68920Sstevel@tonic-gate 			return (0);
68930Sstevel@tonic-gate 		}
68940Sstevel@tonic-gate 
68950Sstevel@tonic-gate 		page = seg_page(seg, addr);
68960Sstevel@tonic-gate 		ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
68970Sstevel@tonic-gate 		anon_disclaim(amp, svd->anon_index + page, len, 0);
68980Sstevel@tonic-gate 		ANON_LOCK_EXIT(&amp->a_rwlock);
68990Sstevel@tonic-gate 		SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
69000Sstevel@tonic-gate 		return (0);
69010Sstevel@tonic-gate 	}
69020Sstevel@tonic-gate 
69030Sstevel@tonic-gate 	/*
69040Sstevel@tonic-gate 	 * If advice is to be applied to entire segment,
69050Sstevel@tonic-gate 	 * use advice field in seg_data structure
69060Sstevel@tonic-gate 	 * otherwise use appropriate vpage entry.
69070Sstevel@tonic-gate 	 */
69080Sstevel@tonic-gate 	if ((addr == seg->s_base) && (len == seg->s_size)) {
69090Sstevel@tonic-gate 		switch (behav) {
69100Sstevel@tonic-gate 		case MADV_ACCESS_LWP:
69110Sstevel@tonic-gate 		case MADV_ACCESS_MANY:
69120Sstevel@tonic-gate 		case MADV_ACCESS_DEFAULT:
69130Sstevel@tonic-gate 			/*
69140Sstevel@tonic-gate 			 * Set memory allocation policy for this segment
69150Sstevel@tonic-gate 			 */
69160Sstevel@tonic-gate 			policy = lgrp_madv_to_policy(behav, len, svd->type);
69170Sstevel@tonic-gate 			if (svd->type == MAP_SHARED)
69180Sstevel@tonic-gate 				already_set = lgrp_shm_policy_set(policy, amp,
69190Sstevel@tonic-gate 				    svd->anon_index, vp, svd->offset, len);
69200Sstevel@tonic-gate 			else {
69210Sstevel@tonic-gate 				/*
69220Sstevel@tonic-gate 				 * For private memory, need writers lock on
69230Sstevel@tonic-gate 				 * address space because the segment may be
69240Sstevel@tonic-gate 				 * split or concatenated when changing policy
69250Sstevel@tonic-gate 				 */
69260Sstevel@tonic-gate 				if (AS_READ_HELD(seg->s_as,
69270Sstevel@tonic-gate 				    &seg->s_as->a_lock)) {
69280Sstevel@tonic-gate 					SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
69290Sstevel@tonic-gate 					return (IE_RETRY);
69300Sstevel@tonic-gate 				}
69310Sstevel@tonic-gate 
69320Sstevel@tonic-gate 				already_set = lgrp_privm_policy_set(policy,
69330Sstevel@tonic-gate 				    &svd->policy_info, len);
69340Sstevel@tonic-gate 			}
69350Sstevel@tonic-gate 
69360Sstevel@tonic-gate 			/*
69370Sstevel@tonic-gate 			 * If policy set already and it shouldn't be reapplied,
69380Sstevel@tonic-gate 			 * don't do anything.
69390Sstevel@tonic-gate 			 */
69400Sstevel@tonic-gate 			if (already_set &&
69410Sstevel@tonic-gate 			    !LGRP_MEM_POLICY_REAPPLICABLE(policy))
69420Sstevel@tonic-gate 				break;
69430Sstevel@tonic-gate 
69440Sstevel@tonic-gate 			/*
69450Sstevel@tonic-gate 			 * Mark any existing pages in given range for
69460Sstevel@tonic-gate 			 * migration
69470Sstevel@tonic-gate 			 */
69480Sstevel@tonic-gate 			page_mark_migrate(seg, addr, len, amp, svd->anon_index,
69490Sstevel@tonic-gate 			    vp, svd->offset, 1);
69500Sstevel@tonic-gate 
69510Sstevel@tonic-gate 			/*
69520Sstevel@tonic-gate 			 * If same policy set already or this is a shared
69530Sstevel@tonic-gate 			 * memory segment, don't need to try to concatenate
69540Sstevel@tonic-gate 			 * segment with adjacent ones.
69550Sstevel@tonic-gate 			 */
69560Sstevel@tonic-gate 			if (already_set || svd->type == MAP_SHARED)
69570Sstevel@tonic-gate 				break;
69580Sstevel@tonic-gate 
69590Sstevel@tonic-gate 			/*
69600Sstevel@tonic-gate 			 * Try to concatenate this segment with previous
69610Sstevel@tonic-gate 			 * one and next one, since we changed policy for
69620Sstevel@tonic-gate 			 * this one and it may be compatible with adjacent
69630Sstevel@tonic-gate 			 * ones now.
69640Sstevel@tonic-gate 			 */
69650Sstevel@tonic-gate 			prev = AS_SEGPREV(seg->s_as, seg);
69660Sstevel@tonic-gate 			next = AS_SEGNEXT(seg->s_as, seg);
69670Sstevel@tonic-gate 
69680Sstevel@tonic-gate 			if (next && next->s_ops == &segvn_ops &&
69690Sstevel@tonic-gate 			    addr + len == next->s_base)
69700Sstevel@tonic-gate 				(void) segvn_concat(seg, next, 1);
69710Sstevel@tonic-gate 
69720Sstevel@tonic-gate 			if (prev && prev->s_ops == &segvn_ops &&
69730Sstevel@tonic-gate 			    addr == prev->s_base + prev->s_size) {
69740Sstevel@tonic-gate 				/*
69750Sstevel@tonic-gate 				 * Drop lock for private data of current
69760Sstevel@tonic-gate 				 * segment before concatenating (deleting) it
69770Sstevel@tonic-gate 				 * and return IE_REATTACH to tell as_ctl() that
69780Sstevel@tonic-gate 				 * current segment has changed
69790Sstevel@tonic-gate 				 */
69800Sstevel@tonic-gate 				SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
69810Sstevel@tonic-gate 				if (!segvn_concat(prev, seg, 1))
69820Sstevel@tonic-gate 					err = IE_REATTACH;
69830Sstevel@tonic-gate 
69840Sstevel@tonic-gate 				return (err);
69850Sstevel@tonic-gate 			}
69860Sstevel@tonic-gate 			break;
69870Sstevel@tonic-gate 
69880Sstevel@tonic-gate 		case MADV_SEQUENTIAL:
69890Sstevel@tonic-gate 			/*
69900Sstevel@tonic-gate 			 * unloading mapping guarantees
69910Sstevel@tonic-gate 			 * detection in segvn_fault
69920Sstevel@tonic-gate 			 */
69930Sstevel@tonic-gate 			ASSERT(seg->s_szc == 0);
69940Sstevel@tonic-gate 			hat_unload(seg->s_as->a_hat, addr, len,
69950Sstevel@tonic-gate 				HAT_UNLOAD);
69960Sstevel@tonic-gate 			/* FALLTHROUGH */
69970Sstevel@tonic-gate 		case MADV_NORMAL:
69980Sstevel@tonic-gate 		case MADV_RANDOM:
69990Sstevel@tonic-gate 			svd->advice = (uchar_t)behav;
70000Sstevel@tonic-gate 			svd->pageadvice = 0;
70010Sstevel@tonic-gate 			break;
70020Sstevel@tonic-gate 		case MADV_WILLNEED:	/* handled in memcntl */
70030Sstevel@tonic-gate 		case MADV_DONTNEED:	/* handled in memcntl */
70040Sstevel@tonic-gate 		case MADV_FREE:		/* handled above */
70050Sstevel@tonic-gate 			break;
70060Sstevel@tonic-gate 		default:
70070Sstevel@tonic-gate 			err = EINVAL;
70080Sstevel@tonic-gate 		}
70090Sstevel@tonic-gate 	} else {
70100Sstevel@tonic-gate 		caddr_t			eaddr;
70110Sstevel@tonic-gate 		struct seg		*new_seg;
70120Sstevel@tonic-gate 		struct segvn_data	*new_svd;
70130Sstevel@tonic-gate 		u_offset_t		off;
70140Sstevel@tonic-gate 		caddr_t			oldeaddr;
70150Sstevel@tonic-gate 
70160Sstevel@tonic-gate 		page = seg_page(seg, addr);
70170Sstevel@tonic-gate 
70180Sstevel@tonic-gate 		segvn_vpage(seg);
70190Sstevel@tonic-gate 
70200Sstevel@tonic-gate 		switch (behav) {
70210Sstevel@tonic-gate 			struct vpage *bvpp, *evpp;
70220Sstevel@tonic-gate 
70230Sstevel@tonic-gate 		case MADV_ACCESS_LWP:
70240Sstevel@tonic-gate 		case MADV_ACCESS_MANY:
70250Sstevel@tonic-gate 		case MADV_ACCESS_DEFAULT:
70260Sstevel@tonic-gate 			/*
70270Sstevel@tonic-gate 			 * Set memory allocation policy for portion of this
70280Sstevel@tonic-gate 			 * segment
70290Sstevel@tonic-gate 			 */
70300Sstevel@tonic-gate 
70310Sstevel@tonic-gate 			/*
70320Sstevel@tonic-gate 			 * Align address and length of advice to page
70330Sstevel@tonic-gate 			 * boundaries for large pages
70340Sstevel@tonic-gate 			 */
70350Sstevel@tonic-gate 			if (seg->s_szc != 0) {
70360Sstevel@tonic-gate 				size_t	pgsz;
70370Sstevel@tonic-gate 
70380Sstevel@tonic-gate 				pgsz = page_get_pagesize(seg->s_szc);
70390Sstevel@tonic-gate 				addr = (caddr_t)P2ALIGN((uintptr_t)addr, pgsz);
70400Sstevel@tonic-gate 				len = P2ROUNDUP(len, pgsz);
70410Sstevel@tonic-gate 			}
70420Sstevel@tonic-gate 
70430Sstevel@tonic-gate 			/*
70440Sstevel@tonic-gate 			 * Check to see whether policy is set already
70450Sstevel@tonic-gate 			 */
70460Sstevel@tonic-gate 			policy = lgrp_madv_to_policy(behav, len, svd->type);
70470Sstevel@tonic-gate 
70480Sstevel@tonic-gate 			anon_index = svd->anon_index + page;
70490Sstevel@tonic-gate 			off = svd->offset + (uintptr_t)(addr - seg->s_base);
70500Sstevel@tonic-gate 
70510Sstevel@tonic-gate 			if (svd->type == MAP_SHARED)
70520Sstevel@tonic-gate 				already_set = lgrp_shm_policy_set(policy, amp,
70530Sstevel@tonic-gate 				    anon_index, vp, off, len);
70540Sstevel@tonic-gate 			else
70550Sstevel@tonic-gate 				already_set =
70560Sstevel@tonic-gate 				    (policy == svd->policy_info.mem_policy);
70570Sstevel@tonic-gate 
70580Sstevel@tonic-gate 			/*
70590Sstevel@tonic-gate 			 * If policy set already and it shouldn't be reapplied,
70600Sstevel@tonic-gate 			 * don't do anything.
70610Sstevel@tonic-gate 			 */
70620Sstevel@tonic-gate 			if (already_set &&
70630Sstevel@tonic-gate 			    !LGRP_MEM_POLICY_REAPPLICABLE(policy))
70640Sstevel@tonic-gate 				break;
70650Sstevel@tonic-gate 
70660Sstevel@tonic-gate 			/*
70670Sstevel@tonic-gate 			 * For private memory, need writers lock on
70680Sstevel@tonic-gate 			 * address space because the segment may be
70690Sstevel@tonic-gate 			 * split or concatenated when changing policy
70700Sstevel@tonic-gate 			 */
70710Sstevel@tonic-gate 			if (svd->type == MAP_PRIVATE &&
70720Sstevel@tonic-gate 			    AS_READ_HELD(seg->s_as, &seg->s_as->a_lock)) {
70730Sstevel@tonic-gate 				SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
70740Sstevel@tonic-gate 				return (IE_RETRY);
70750Sstevel@tonic-gate 			}
70760Sstevel@tonic-gate 
70770Sstevel@tonic-gate 			/*
70780Sstevel@tonic-gate 			 * Mark any existing pages in given range for
70790Sstevel@tonic-gate 			 * migration
70800Sstevel@tonic-gate 			 */
70810Sstevel@tonic-gate 			page_mark_migrate(seg, addr, len, amp, svd->anon_index,
70820Sstevel@tonic-gate 			    vp, svd->offset, 1);
70830Sstevel@tonic-gate 
70840Sstevel@tonic-gate 			/*
70850Sstevel@tonic-gate 			 * Don't need to try to split or concatenate
70860Sstevel@tonic-gate 			 * segments, since policy is same or this is a shared
70870Sstevel@tonic-gate 			 * memory segment
70880Sstevel@tonic-gate 			 */
70890Sstevel@tonic-gate 			if (already_set || svd->type == MAP_SHARED)
70900Sstevel@tonic-gate 				break;
70910Sstevel@tonic-gate 
70920Sstevel@tonic-gate 			/*
70930Sstevel@tonic-gate 			 * Split off new segment if advice only applies to a
70940Sstevel@tonic-gate 			 * portion of existing segment starting in middle
70950Sstevel@tonic-gate 			 */
70960Sstevel@tonic-gate 			new_seg = NULL;
70970Sstevel@tonic-gate 			eaddr = addr + len;
70980Sstevel@tonic-gate 			oldeaddr = seg->s_base + seg->s_size;
70990Sstevel@tonic-gate 			if (addr > seg->s_base) {
71000Sstevel@tonic-gate 				/*
71010Sstevel@tonic-gate 				 * Must flush I/O page cache
71020Sstevel@tonic-gate 				 * before splitting segment
71030Sstevel@tonic-gate 				 */
71040Sstevel@tonic-gate 				if (svd->softlockcnt > 0)
71050Sstevel@tonic-gate 					segvn_purge(seg);
71060Sstevel@tonic-gate 
71070Sstevel@tonic-gate 				/*
71080Sstevel@tonic-gate 				 * Split segment and return IE_REATTACH to tell
71090Sstevel@tonic-gate 				 * as_ctl() that current segment changed
71100Sstevel@tonic-gate 				 */
71110Sstevel@tonic-gate 				new_seg = segvn_split_seg(seg, addr);
71120Sstevel@tonic-gate 				new_svd = (struct segvn_data *)new_seg->s_data;
71130Sstevel@tonic-gate 				err = IE_REATTACH;
71140Sstevel@tonic-gate 
71150Sstevel@tonic-gate 				/*
71160Sstevel@tonic-gate 				 * If new segment ends where old one
71170Sstevel@tonic-gate 				 * did, try to concatenate the new
71180Sstevel@tonic-gate 				 * segment with next one.
71190Sstevel@tonic-gate 				 */
71200Sstevel@tonic-gate 				if (eaddr == oldeaddr) {
71210Sstevel@tonic-gate 					/*
71220Sstevel@tonic-gate 					 * Set policy for new segment
71230Sstevel@tonic-gate 					 */
71240Sstevel@tonic-gate 					(void) lgrp_privm_policy_set(policy,
71250Sstevel@tonic-gate 					    &new_svd->policy_info,
71260Sstevel@tonic-gate 					    new_seg->s_size);
71270Sstevel@tonic-gate 
71280Sstevel@tonic-gate 					next = AS_SEGNEXT(new_seg->s_as,
71290Sstevel@tonic-gate 					    new_seg);
71300Sstevel@tonic-gate 
71310Sstevel@tonic-gate 					if (next &&
71320Sstevel@tonic-gate 					    next->s_ops == &segvn_ops &&
71330Sstevel@tonic-gate 					    eaddr == next->s_base)
71340Sstevel@tonic-gate 						(void) segvn_concat(new_seg,
71350Sstevel@tonic-gate 						    next, 1);
71360Sstevel@tonic-gate 				}
71370Sstevel@tonic-gate 			}
71380Sstevel@tonic-gate 
71390Sstevel@tonic-gate 			/*
71400Sstevel@tonic-gate 			 * Split off end of existing segment if advice only
71410Sstevel@tonic-gate 			 * applies to a portion of segment ending before
71420Sstevel@tonic-gate 			 * end of the existing segment
71430Sstevel@tonic-gate 			 */
71440Sstevel@tonic-gate 			if (eaddr < oldeaddr) {
71450Sstevel@tonic-gate 				/*
71460Sstevel@tonic-gate 				 * Must flush I/O page cache
71470Sstevel@tonic-gate 				 * before splitting segment
71480Sstevel@tonic-gate 				 */
71490Sstevel@tonic-gate 				if (svd->softlockcnt > 0)
71500Sstevel@tonic-gate 					segvn_purge(seg);
71510Sstevel@tonic-gate 
71520Sstevel@tonic-gate 				/*
71530Sstevel@tonic-gate 				 * If beginning of old segment was already
71540Sstevel@tonic-gate 				 * split off, use new segment to split end off
71550Sstevel@tonic-gate 				 * from.
71560Sstevel@tonic-gate 				 */
71570Sstevel@tonic-gate 				if (new_seg != NULL && new_seg != seg) {
71580Sstevel@tonic-gate 					/*
71590Sstevel@tonic-gate 					 * Split segment
71600Sstevel@tonic-gate 					 */
71610Sstevel@tonic-gate 					(void) segvn_split_seg(new_seg, eaddr);
71620Sstevel@tonic-gate 
71630Sstevel@tonic-gate 					/*
71640Sstevel@tonic-gate 					 * Set policy for new segment
71650Sstevel@tonic-gate 					 */
71660Sstevel@tonic-gate 					(void) lgrp_privm_policy_set(policy,
71670Sstevel@tonic-gate 					    &new_svd->policy_info,
71680Sstevel@tonic-gate 					    new_seg->s_size);
71690Sstevel@tonic-gate 				} else {
71700Sstevel@tonic-gate 					/*
71710Sstevel@tonic-gate 					 * Split segment and return IE_REATTACH
71720Sstevel@tonic-gate 					 * to tell as_ctl() that current
71730Sstevel@tonic-gate 					 * segment changed
71740Sstevel@tonic-gate 					 */
71750Sstevel@tonic-gate 					(void) segvn_split_seg(seg, eaddr);
71760Sstevel@tonic-gate 					err = IE_REATTACH;
71770Sstevel@tonic-gate 
71780Sstevel@tonic-gate 					(void) lgrp_privm_policy_set(policy,
71790Sstevel@tonic-gate 					    &svd->policy_info, seg->s_size);
71800Sstevel@tonic-gate 
71810Sstevel@tonic-gate 					/*
71820Sstevel@tonic-gate 					 * If new segment starts where old one
71830Sstevel@tonic-gate 					 * did, try to concatenate it with
71840Sstevel@tonic-gate 					 * previous segment.
71850Sstevel@tonic-gate 					 */
71860Sstevel@tonic-gate 					if (addr == seg->s_base) {
71870Sstevel@tonic-gate 						prev = AS_SEGPREV(seg->s_as,
71880Sstevel@tonic-gate 						    seg);
71890Sstevel@tonic-gate 
71900Sstevel@tonic-gate 						/*
71910Sstevel@tonic-gate 						 * Drop lock for private data
71920Sstevel@tonic-gate 						 * of current segment before
71930Sstevel@tonic-gate 						 * concatenating (deleting) it
71940Sstevel@tonic-gate 						 */
71950Sstevel@tonic-gate 						if (prev &&
71960Sstevel@tonic-gate 						    prev->s_ops ==
71970Sstevel@tonic-gate 						    &segvn_ops &&
71980Sstevel@tonic-gate 						    addr == prev->s_base +
71990Sstevel@tonic-gate 						    prev->s_size) {
72000Sstevel@tonic-gate 							SEGVN_LOCK_EXIT(
72010Sstevel@tonic-gate 							    seg->s_as,
72020Sstevel@tonic-gate 							    &svd->lock);
72030Sstevel@tonic-gate 							(void) segvn_concat(
72040Sstevel@tonic-gate 							    prev, seg, 1);
72050Sstevel@tonic-gate 							return (err);
72060Sstevel@tonic-gate 						}
72070Sstevel@tonic-gate 					}
72080Sstevel@tonic-gate 				}
72090Sstevel@tonic-gate 			}
72100Sstevel@tonic-gate 			break;
72110Sstevel@tonic-gate 		case MADV_SEQUENTIAL:
72120Sstevel@tonic-gate 			ASSERT(seg->s_szc == 0);
72130Sstevel@tonic-gate 			hat_unload(seg->s_as->a_hat, addr, len, HAT_UNLOAD);
72140Sstevel@tonic-gate 			/* FALLTHROUGH */
72150Sstevel@tonic-gate 		case MADV_NORMAL:
72160Sstevel@tonic-gate 		case MADV_RANDOM:
72170Sstevel@tonic-gate 			bvpp = &svd->vpage[page];
72180Sstevel@tonic-gate 			evpp = &svd->vpage[page + (len >> PAGESHIFT)];
72190Sstevel@tonic-gate 			for (; bvpp < evpp; bvpp++)
72200Sstevel@tonic-gate 				VPP_SETADVICE(bvpp, behav);
72210Sstevel@tonic-gate 			svd->advice = MADV_NORMAL;
72220Sstevel@tonic-gate 			break;
72230Sstevel@tonic-gate 		case MADV_WILLNEED:	/* handled in memcntl */
72240Sstevel@tonic-gate 		case MADV_DONTNEED:	/* handled in memcntl */
72250Sstevel@tonic-gate 		case MADV_FREE:		/* handled above */
72260Sstevel@tonic-gate 			break;
72270Sstevel@tonic-gate 		default:
72280Sstevel@tonic-gate 			err = EINVAL;
72290Sstevel@tonic-gate 		}
72300Sstevel@tonic-gate 	}
72310Sstevel@tonic-gate 	SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
72320Sstevel@tonic-gate 	return (err);
72330Sstevel@tonic-gate }
72340Sstevel@tonic-gate 
72350Sstevel@tonic-gate /*
72360Sstevel@tonic-gate  * Create a vpage structure for this seg.
72370Sstevel@tonic-gate  */
72380Sstevel@tonic-gate static void
72390Sstevel@tonic-gate segvn_vpage(struct seg *seg)
72400Sstevel@tonic-gate {
72410Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
72420Sstevel@tonic-gate 	struct vpage *vp, *evp;
72430Sstevel@tonic-gate 
72440Sstevel@tonic-gate 	ASSERT(SEGVN_WRITE_HELD(seg->s_as, &svd->lock));
72450Sstevel@tonic-gate 
72460Sstevel@tonic-gate 	/*
72470Sstevel@tonic-gate 	 * If no vpage structure exists, allocate one.  Copy the protections
72480Sstevel@tonic-gate 	 * and the advice from the segment itself to the individual pages.
72490Sstevel@tonic-gate 	 */
72500Sstevel@tonic-gate 	if (svd->vpage == NULL) {
72510Sstevel@tonic-gate 		svd->pageprot = 1;
72520Sstevel@tonic-gate 		svd->pageadvice = 1;
72530Sstevel@tonic-gate 		svd->vpage = kmem_zalloc(seg_pages(seg) * sizeof (struct vpage),
72540Sstevel@tonic-gate 		    KM_SLEEP);
72550Sstevel@tonic-gate 		evp = &svd->vpage[seg_page(seg, seg->s_base + seg->s_size)];
72560Sstevel@tonic-gate 		for (vp = svd->vpage; vp < evp; vp++) {
72570Sstevel@tonic-gate 			VPP_SETPROT(vp, svd->prot);
72580Sstevel@tonic-gate 			VPP_SETADVICE(vp, svd->advice);
72590Sstevel@tonic-gate 		}
72600Sstevel@tonic-gate 	}
72610Sstevel@tonic-gate }
72620Sstevel@tonic-gate 
72630Sstevel@tonic-gate /*
72640Sstevel@tonic-gate  * Dump the pages belonging to this segvn segment.
72650Sstevel@tonic-gate  */
72660Sstevel@tonic-gate static void
72670Sstevel@tonic-gate segvn_dump(struct seg *seg)
72680Sstevel@tonic-gate {
72690Sstevel@tonic-gate 	struct segvn_data *svd;
72700Sstevel@tonic-gate 	page_t *pp;
72710Sstevel@tonic-gate 	struct anon_map *amp;
72720Sstevel@tonic-gate 	ulong_t	anon_index;
72730Sstevel@tonic-gate 	struct vnode *vp;
72740Sstevel@tonic-gate 	u_offset_t off, offset;
72750Sstevel@tonic-gate 	pfn_t pfn;
72760Sstevel@tonic-gate 	pgcnt_t page, npages;
72770Sstevel@tonic-gate 	caddr_t addr;
72780Sstevel@tonic-gate 
72790Sstevel@tonic-gate 	npages = seg_pages(seg);
72800Sstevel@tonic-gate 	svd = (struct segvn_data *)seg->s_data;
72810Sstevel@tonic-gate 	vp = svd->vp;
72820Sstevel@tonic-gate 	off = offset = svd->offset;
72830Sstevel@tonic-gate 	addr = seg->s_base;
72840Sstevel@tonic-gate 
72850Sstevel@tonic-gate 	if ((amp = svd->amp) != NULL) {
72860Sstevel@tonic-gate 		anon_index = svd->anon_index;
72870Sstevel@tonic-gate 		ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
72880Sstevel@tonic-gate 	}
72890Sstevel@tonic-gate 
72900Sstevel@tonic-gate 	for (page = 0; page < npages; page++, offset += PAGESIZE) {
72910Sstevel@tonic-gate 		struct anon *ap;
72920Sstevel@tonic-gate 		int we_own_it = 0;
72930Sstevel@tonic-gate 
72940Sstevel@tonic-gate 		if (amp && (ap = anon_get_ptr(svd->amp->ahp, anon_index++))) {
72950Sstevel@tonic-gate 			swap_xlate_nopanic(ap, &vp, &off);
72960Sstevel@tonic-gate 		} else {
72970Sstevel@tonic-gate 			vp = svd->vp;
72980Sstevel@tonic-gate 			off = offset;
72990Sstevel@tonic-gate 		}
73000Sstevel@tonic-gate 
73010Sstevel@tonic-gate 		/*
73020Sstevel@tonic-gate 		 * If pp == NULL, the page either does not exist
73030Sstevel@tonic-gate 		 * or is exclusively locked.  So determine if it
73040Sstevel@tonic-gate 		 * exists before searching for it.
73050Sstevel@tonic-gate 		 */
73060Sstevel@tonic-gate 
73070Sstevel@tonic-gate 		if ((pp = page_lookup_nowait(vp, off, SE_SHARED)))
73080Sstevel@tonic-gate 			we_own_it = 1;
73090Sstevel@tonic-gate 		else
73100Sstevel@tonic-gate 			pp = page_exists(vp, off);
73110Sstevel@tonic-gate 
73120Sstevel@tonic-gate 		if (pp) {
73130Sstevel@tonic-gate 			pfn = page_pptonum(pp);
73140Sstevel@tonic-gate 			dump_addpage(seg->s_as, addr, pfn);
73150Sstevel@tonic-gate 			if (we_own_it)
73160Sstevel@tonic-gate 				page_unlock(pp);
73170Sstevel@tonic-gate 		}
73180Sstevel@tonic-gate 		addr += PAGESIZE;
73190Sstevel@tonic-gate 		dump_timeleft = dump_timeout;
73200Sstevel@tonic-gate 	}
73210Sstevel@tonic-gate 
73220Sstevel@tonic-gate 	if (amp != NULL)
73230Sstevel@tonic-gate 		ANON_LOCK_EXIT(&amp->a_rwlock);
73240Sstevel@tonic-gate }
73250Sstevel@tonic-gate 
73260Sstevel@tonic-gate /*
73270Sstevel@tonic-gate  * lock/unlock anon pages over a given range. Return shadow list
73280Sstevel@tonic-gate  */
73290Sstevel@tonic-gate static int
73300Sstevel@tonic-gate segvn_pagelock(struct seg *seg, caddr_t addr, size_t len, struct page ***ppp,
73310Sstevel@tonic-gate     enum lock_type type, enum seg_rw rw)
73320Sstevel@tonic-gate {
73330Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
73340Sstevel@tonic-gate 	size_t np, adjustpages = 0, npages = (len >> PAGESHIFT);
73350Sstevel@tonic-gate 	ulong_t anon_index;
73360Sstevel@tonic-gate 	uint_t protchk;
73370Sstevel@tonic-gate 	uint_t error;
73380Sstevel@tonic-gate 	struct anon_map *amp;
73390Sstevel@tonic-gate 	struct page **pplist, **pl, *pp;
73400Sstevel@tonic-gate 	caddr_t a;
73410Sstevel@tonic-gate 	size_t page;
73420Sstevel@tonic-gate 	caddr_t lpgaddr, lpgeaddr;
73430Sstevel@tonic-gate 
73440Sstevel@tonic-gate 	TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_SEGVN_START,
73450Sstevel@tonic-gate 		"segvn_pagelock: start seg %p addr %p", seg, addr);
73460Sstevel@tonic-gate 
73470Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
73480Sstevel@tonic-gate 	if (seg->s_szc != 0 && (type == L_PAGELOCK || type == L_PAGEUNLOCK)) {
73490Sstevel@tonic-gate 		/*
73500Sstevel@tonic-gate 		 * We are adjusting the pagelock region to the large page size
73510Sstevel@tonic-gate 		 * boundary because the unlocked part of a large page cannot
73520Sstevel@tonic-gate 		 * be freed anyway unless all constituent pages of a large
73530Sstevel@tonic-gate 		 * page are locked. Therefore this adjustment allows us to
73540Sstevel@tonic-gate 		 * decrement availrmem by the right value (note we don't want
73550Sstevel@tonic-gate 		 * to just decrement availrem by the large page size without
73560Sstevel@tonic-gate 		 * adjusting addr and len because then we may end up
73570Sstevel@tonic-gate 		 * decrementing availrmem by large page size for every
73580Sstevel@tonic-gate 		 * constituent page locked by a new as_pagelock call).
73590Sstevel@tonic-gate 		 * as_pageunlock caller must always match as_pagelock call's
73600Sstevel@tonic-gate 		 * addr and len.
73610Sstevel@tonic-gate 		 *
73620Sstevel@tonic-gate 		 * Note segment's page size cannot change while we are holding
73630Sstevel@tonic-gate 		 * as lock.  And then it cannot change while softlockcnt is
73640Sstevel@tonic-gate 		 * not 0. This will allow us to correctly recalculate large
73650Sstevel@tonic-gate 		 * page size region for the matching pageunlock/reclaim call.
73660Sstevel@tonic-gate 		 *
73670Sstevel@tonic-gate 		 * for pageunlock *ppp points to the pointer of page_t that
73680Sstevel@tonic-gate 		 * corresponds to the real unadjusted start address. Similar
73690Sstevel@tonic-gate 		 * for pagelock *ppp must point to the pointer of page_t that
73700Sstevel@tonic-gate 		 * corresponds to the real unadjusted start address.
73710Sstevel@tonic-gate 		 */
73720Sstevel@tonic-gate 		size_t pgsz = page_get_pagesize(seg->s_szc);
73730Sstevel@tonic-gate 		CALC_LPG_REGION(pgsz, seg, addr, len, lpgaddr, lpgeaddr);
73740Sstevel@tonic-gate 		adjustpages = ((uintptr_t)(addr - lpgaddr)) >> PAGESHIFT;
73750Sstevel@tonic-gate 	}
73760Sstevel@tonic-gate 
73770Sstevel@tonic-gate 	if (type == L_PAGEUNLOCK) {
73780Sstevel@tonic-gate 
73790Sstevel@tonic-gate 		/*
73800Sstevel@tonic-gate 		 * update hat ref bits for /proc. We need to make sure
73810Sstevel@tonic-gate 		 * that threads tracing the ref and mod bits of the
73820Sstevel@tonic-gate 		 * address space get the right data.
73830Sstevel@tonic-gate 		 * Note: page ref and mod bits are updated at reclaim time
73840Sstevel@tonic-gate 		 */
73850Sstevel@tonic-gate 		if (seg->s_as->a_vbits) {
73860Sstevel@tonic-gate 			for (a = addr; a < addr + len; a += PAGESIZE) {
73870Sstevel@tonic-gate 				if (rw == S_WRITE) {
73880Sstevel@tonic-gate 					hat_setstat(seg->s_as, a,
73890Sstevel@tonic-gate 					    PAGESIZE, P_REF | P_MOD);
73900Sstevel@tonic-gate 				} else {
73910Sstevel@tonic-gate 					hat_setstat(seg->s_as, a,
73920Sstevel@tonic-gate 					    PAGESIZE, P_REF);
73930Sstevel@tonic-gate 				}
73940Sstevel@tonic-gate 			}
73950Sstevel@tonic-gate 		}
73960Sstevel@tonic-gate 		SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
73970Sstevel@tonic-gate 		if (seg->s_szc != 0) {
73980Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.pagelock[0]);
73990Sstevel@tonic-gate 			seg_pinactive(seg, lpgaddr, lpgeaddr - lpgaddr,
74000Sstevel@tonic-gate 			    *ppp - adjustpages, rw, segvn_reclaim);
74010Sstevel@tonic-gate 		} else {
74020Sstevel@tonic-gate 			seg_pinactive(seg, addr, len, *ppp, rw, segvn_reclaim);
74030Sstevel@tonic-gate 		}
74040Sstevel@tonic-gate 
74050Sstevel@tonic-gate 		/*
74060Sstevel@tonic-gate 		 * If someone is blocked while unmapping, we purge
74070Sstevel@tonic-gate 		 * segment page cache and thus reclaim pplist synchronously
74080Sstevel@tonic-gate 		 * without waiting for seg_pasync_thread. This speeds up
74090Sstevel@tonic-gate 		 * unmapping in cases where munmap(2) is called, while
74100Sstevel@tonic-gate 		 * raw async i/o is still in progress or where a thread
74110Sstevel@tonic-gate 		 * exits on data fault in a multithreaded application.
74120Sstevel@tonic-gate 		 */
74130Sstevel@tonic-gate 		if (AS_ISUNMAPWAIT(seg->s_as) && (svd->softlockcnt > 0)) {
74140Sstevel@tonic-gate 			/*
74150Sstevel@tonic-gate 			 * Even if we grab segvn WRITER's lock or segp_slock
74160Sstevel@tonic-gate 			 * here, there might be another thread which could've
74170Sstevel@tonic-gate 			 * successfully performed lookup/insert just before
74180Sstevel@tonic-gate 			 * we acquired the lock here.  So, grabbing either
74190Sstevel@tonic-gate 			 * lock here is of not much use.  Until we devise
74200Sstevel@tonic-gate 			 * a strategy at upper layers to solve the
74210Sstevel@tonic-gate 			 * synchronization issues completely, we expect
74220Sstevel@tonic-gate 			 * applications to handle this appropriately.
74230Sstevel@tonic-gate 			 */
74240Sstevel@tonic-gate 			segvn_purge(seg);
74250Sstevel@tonic-gate 		}
74260Sstevel@tonic-gate 		SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
74270Sstevel@tonic-gate 		TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_SEGVN_UNLOCK_END,
74280Sstevel@tonic-gate 			"segvn_pagelock: unlock seg %p addr %p", seg, addr);
74290Sstevel@tonic-gate 		return (0);
74300Sstevel@tonic-gate 	} else if (type == L_PAGERECLAIM) {
74310Sstevel@tonic-gate 		VM_STAT_COND_ADD(seg->s_szc != 0, segvnvmstats.pagelock[1]);
74320Sstevel@tonic-gate 		SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
74330Sstevel@tonic-gate 		(void) segvn_reclaim(seg, addr, len, *ppp, rw);
74340Sstevel@tonic-gate 		SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
74350Sstevel@tonic-gate 		TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_SEGVN_UNLOCK_END,
74360Sstevel@tonic-gate 			"segvn_pagelock: reclaim seg %p addr %p", seg, addr);
74370Sstevel@tonic-gate 		return (0);
74380Sstevel@tonic-gate 	}
74390Sstevel@tonic-gate 
74400Sstevel@tonic-gate 	if (seg->s_szc != 0) {
74410Sstevel@tonic-gate 		VM_STAT_ADD(segvnvmstats.pagelock[2]);
74420Sstevel@tonic-gate 		addr = lpgaddr;
74430Sstevel@tonic-gate 		len = lpgeaddr - lpgaddr;
74440Sstevel@tonic-gate 		npages = (len >> PAGESHIFT);
74450Sstevel@tonic-gate 	}
74460Sstevel@tonic-gate 
74470Sstevel@tonic-gate 	/*
74480Sstevel@tonic-gate 	 * for now we only support pagelock to anon memory. We've to check
74490Sstevel@tonic-gate 	 * protections for vnode objects and call into the vnode driver.
74500Sstevel@tonic-gate 	 * That's too much for a fast path. Let the fault entry point handle it.
74510Sstevel@tonic-gate 	 */
74520Sstevel@tonic-gate 	if (svd->vp != NULL) {
74530Sstevel@tonic-gate 		TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_SEGVN_MISS_END,
74540Sstevel@tonic-gate 		    "segvn_pagelock: mapped vnode seg %p addr %p", seg, addr);
74550Sstevel@tonic-gate 		*ppp = NULL;
74560Sstevel@tonic-gate 		return (ENOTSUP);
74570Sstevel@tonic-gate 	}
74580Sstevel@tonic-gate 
74590Sstevel@tonic-gate 	/*
74600Sstevel@tonic-gate 	 * if anonmap is not yet created, let the fault entry point populate it
74610Sstevel@tonic-gate 	 * with anon ptrs.
74620Sstevel@tonic-gate 	 */
74630Sstevel@tonic-gate 	if ((amp = svd->amp) == NULL) {
74640Sstevel@tonic-gate 		TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_SEGVN_MISS_END,
74650Sstevel@tonic-gate 		    "segvn_pagelock: anonmap null seg %p addr %p", seg, addr);
74660Sstevel@tonic-gate 		*ppp = NULL;
74670Sstevel@tonic-gate 		return (EFAULT);
74680Sstevel@tonic-gate 	}
74690Sstevel@tonic-gate 
74700Sstevel@tonic-gate 	SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
74710Sstevel@tonic-gate 
74720Sstevel@tonic-gate 	/*
74730Sstevel@tonic-gate 	 * we acquire segp_slock to prevent duplicate entries
74740Sstevel@tonic-gate 	 * in seg_pcache
74750Sstevel@tonic-gate 	 */
74760Sstevel@tonic-gate 	mutex_enter(&svd->segp_slock);
74770Sstevel@tonic-gate 
74780Sstevel@tonic-gate 	/*
74790Sstevel@tonic-gate 	 * try to find pages in segment page cache
74800Sstevel@tonic-gate 	 */
74810Sstevel@tonic-gate 	pplist = seg_plookup(seg, addr, len, rw);
74820Sstevel@tonic-gate 	if (pplist != NULL) {
74830Sstevel@tonic-gate 		mutex_exit(&svd->segp_slock);
74840Sstevel@tonic-gate 		SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
74850Sstevel@tonic-gate 		*ppp = pplist + adjustpages;
74860Sstevel@tonic-gate 		TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_SEGVN_HIT_END,
74870Sstevel@tonic-gate 			"segvn_pagelock: cache hit seg %p addr %p", seg, addr);
74880Sstevel@tonic-gate 		return (0);
74890Sstevel@tonic-gate 	}
74900Sstevel@tonic-gate 
74910Sstevel@tonic-gate 	if (rw == S_READ) {
74920Sstevel@tonic-gate 		protchk = PROT_READ;
74930Sstevel@tonic-gate 	} else {
74940Sstevel@tonic-gate 		protchk = PROT_WRITE;
74950Sstevel@tonic-gate 	}
74960Sstevel@tonic-gate 
74970Sstevel@tonic-gate 	if (svd->pageprot == 0) {
74980Sstevel@tonic-gate 		if ((svd->prot & protchk) == 0) {
74990Sstevel@tonic-gate 			mutex_exit(&svd->segp_slock);
75000Sstevel@tonic-gate 			error = EFAULT;
75010Sstevel@tonic-gate 			goto out;
75020Sstevel@tonic-gate 		}
75030Sstevel@tonic-gate 	} else {
75040Sstevel@tonic-gate 		/*
75050Sstevel@tonic-gate 		 * check page protections
75060Sstevel@tonic-gate 		 */
75070Sstevel@tonic-gate 		for (a = addr; a < addr + len; a += PAGESIZE) {
75080Sstevel@tonic-gate 			struct vpage *vp;
75090Sstevel@tonic-gate 
75100Sstevel@tonic-gate 			vp = &svd->vpage[seg_page(seg, a)];
75110Sstevel@tonic-gate 			if ((VPP_PROT(vp) & protchk) == 0) {
75120Sstevel@tonic-gate 				mutex_exit(&svd->segp_slock);
75130Sstevel@tonic-gate 				error = EFAULT;
75140Sstevel@tonic-gate 				goto out;
75150Sstevel@tonic-gate 			}
75160Sstevel@tonic-gate 		}
75170Sstevel@tonic-gate 	}
75180Sstevel@tonic-gate 
75190Sstevel@tonic-gate 	mutex_enter(&freemem_lock);
75200Sstevel@tonic-gate 	if (availrmem < tune.t_minarmem + npages) {
75210Sstevel@tonic-gate 		mutex_exit(&freemem_lock);
75220Sstevel@tonic-gate 		mutex_exit(&svd->segp_slock);
75230Sstevel@tonic-gate 		error = ENOMEM;
75240Sstevel@tonic-gate 		goto out;
75250Sstevel@tonic-gate 	} else {
75260Sstevel@tonic-gate 		svd->softlockcnt += npages;
75270Sstevel@tonic-gate 		availrmem -= npages;
75280Sstevel@tonic-gate 		segvn_pages_locked += npages;
75290Sstevel@tonic-gate 	}
75300Sstevel@tonic-gate 	mutex_exit(&freemem_lock);
75310Sstevel@tonic-gate 
75320Sstevel@tonic-gate 	pplist = kmem_alloc(sizeof (page_t *) * npages, KM_SLEEP);
75330Sstevel@tonic-gate 	pl = pplist;
75340Sstevel@tonic-gate 	*ppp = pplist + adjustpages;
75350Sstevel@tonic-gate 
75360Sstevel@tonic-gate 	page = seg_page(seg, addr);
75370Sstevel@tonic-gate 	anon_index = svd->anon_index + page;
75380Sstevel@tonic-gate 
75390Sstevel@tonic-gate 	ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
75400Sstevel@tonic-gate 	for (a = addr; a < addr + len; a += PAGESIZE, anon_index++) {
75410Sstevel@tonic-gate 		struct anon *ap;
75420Sstevel@tonic-gate 		struct vnode *vp;
75430Sstevel@tonic-gate 		u_offset_t off;
75440Sstevel@tonic-gate 		anon_sync_obj_t cookie;
75450Sstevel@tonic-gate 
75460Sstevel@tonic-gate 		anon_array_enter(amp, anon_index, &cookie);
75470Sstevel@tonic-gate 		ap = anon_get_ptr(amp->ahp, anon_index);
75480Sstevel@tonic-gate 		if (ap == NULL) {
75490Sstevel@tonic-gate 			anon_array_exit(&cookie);
75500Sstevel@tonic-gate 			break;
75510Sstevel@tonic-gate 		} else {
75520Sstevel@tonic-gate 			/*
75530Sstevel@tonic-gate 			 * We must never use seg_pcache for COW pages
75540Sstevel@tonic-gate 			 * because we might end up with original page still
75550Sstevel@tonic-gate 			 * lying in seg_pcache even after private page is
75560Sstevel@tonic-gate 			 * created. This leads to data corruption as
75570Sstevel@tonic-gate 			 * aio_write refers to the page still in cache
75580Sstevel@tonic-gate 			 * while all other accesses refer to the private
75590Sstevel@tonic-gate 			 * page.
75600Sstevel@tonic-gate 			 */
75610Sstevel@tonic-gate 			if (ap->an_refcnt != 1) {
75620Sstevel@tonic-gate 				anon_array_exit(&cookie);
75630Sstevel@tonic-gate 				break;
75640Sstevel@tonic-gate 			}
75650Sstevel@tonic-gate 		}
75660Sstevel@tonic-gate 		swap_xlate(ap, &vp, &off);
75670Sstevel@tonic-gate 		anon_array_exit(&cookie);
75680Sstevel@tonic-gate 
75690Sstevel@tonic-gate 		pp = page_lookup_nowait(vp, off, SE_SHARED);
75700Sstevel@tonic-gate 		if (pp == NULL) {
75710Sstevel@tonic-gate 			break;
75720Sstevel@tonic-gate 		}
75730Sstevel@tonic-gate 		*pplist++ = pp;
75740Sstevel@tonic-gate 	}
75750Sstevel@tonic-gate 	ANON_LOCK_EXIT(&amp->a_rwlock);
75760Sstevel@tonic-gate 
75770Sstevel@tonic-gate 	if (a >= addr + len) {
75780Sstevel@tonic-gate 		(void) seg_pinsert(seg, addr, len, pl, rw, SEGP_ASYNC_FLUSH,
75790Sstevel@tonic-gate 			segvn_reclaim);
75800Sstevel@tonic-gate 		mutex_exit(&svd->segp_slock);
75810Sstevel@tonic-gate 		SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
75820Sstevel@tonic-gate 		TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_SEGVN_FILL_END,
75830Sstevel@tonic-gate 		    "segvn_pagelock: cache fill seg %p addr %p", seg, addr);
75840Sstevel@tonic-gate 		return (0);
75850Sstevel@tonic-gate 	}
75860Sstevel@tonic-gate 
75870Sstevel@tonic-gate 	mutex_exit(&svd->segp_slock);
75880Sstevel@tonic-gate 	error = EFAULT;
75890Sstevel@tonic-gate 	pplist = pl;
75900Sstevel@tonic-gate 	np = ((uintptr_t)(a - addr)) >> PAGESHIFT;
75910Sstevel@tonic-gate 	while (np > (uint_t)0) {
75920Sstevel@tonic-gate 		page_unlock(*pplist);
75930Sstevel@tonic-gate 		np--;
75940Sstevel@tonic-gate 		pplist++;
75950Sstevel@tonic-gate 	}
75960Sstevel@tonic-gate 	kmem_free(pl, sizeof (page_t *) * npages);
75970Sstevel@tonic-gate 	mutex_enter(&freemem_lock);
75980Sstevel@tonic-gate 	svd->softlockcnt -= npages;
75990Sstevel@tonic-gate 	availrmem += npages;
76000Sstevel@tonic-gate 	segvn_pages_locked -= npages;
76010Sstevel@tonic-gate 	mutex_exit(&freemem_lock);
76020Sstevel@tonic-gate 	if (svd->softlockcnt <= 0) {
76030Sstevel@tonic-gate 		if (AS_ISUNMAPWAIT(seg->s_as)) {
76040Sstevel@tonic-gate 			mutex_enter(&seg->s_as->a_contents);
76050Sstevel@tonic-gate 			if (AS_ISUNMAPWAIT(seg->s_as)) {
76060Sstevel@tonic-gate 				AS_CLRUNMAPWAIT(seg->s_as);
76070Sstevel@tonic-gate 				cv_broadcast(&seg->s_as->a_cv);
76080Sstevel@tonic-gate 			}
76090Sstevel@tonic-gate 			mutex_exit(&seg->s_as->a_contents);
76100Sstevel@tonic-gate 		}
76110Sstevel@tonic-gate 	}
76120Sstevel@tonic-gate 
76130Sstevel@tonic-gate out:
76140Sstevel@tonic-gate 	SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
76150Sstevel@tonic-gate 	*ppp = NULL;
76160Sstevel@tonic-gate 	TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_SEGVN_MISS_END,
76170Sstevel@tonic-gate 		"segvn_pagelock: cache miss seg %p addr %p", seg, addr);
76180Sstevel@tonic-gate 	return (error);
76190Sstevel@tonic-gate }
76200Sstevel@tonic-gate 
76210Sstevel@tonic-gate /*
76220Sstevel@tonic-gate  * purge any cached pages in the I/O page cache
76230Sstevel@tonic-gate  */
76240Sstevel@tonic-gate static void
76250Sstevel@tonic-gate segvn_purge(struct seg *seg)
76260Sstevel@tonic-gate {
76270Sstevel@tonic-gate 	seg_ppurge(seg);
76280Sstevel@tonic-gate }
76290Sstevel@tonic-gate 
76300Sstevel@tonic-gate static int
76310Sstevel@tonic-gate segvn_reclaim(struct seg *seg, caddr_t addr, size_t len, struct page **pplist,
76320Sstevel@tonic-gate 	enum seg_rw rw)
76330Sstevel@tonic-gate {
76340Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
76350Sstevel@tonic-gate 	pgcnt_t np, npages;
76360Sstevel@tonic-gate 	struct page **pl;
76370Sstevel@tonic-gate 
76380Sstevel@tonic-gate #ifdef lint
76390Sstevel@tonic-gate 	addr = addr;
76400Sstevel@tonic-gate #endif
76410Sstevel@tonic-gate 
76420Sstevel@tonic-gate 	npages = np = (len >> PAGESHIFT);
76430Sstevel@tonic-gate 	ASSERT(npages);
76440Sstevel@tonic-gate 	pl = pplist;
76450Sstevel@tonic-gate 	if (seg->s_szc != 0) {
76460Sstevel@tonic-gate 		size_t pgsz = page_get_pagesize(seg->s_szc);
76470Sstevel@tonic-gate 		if (!IS_P2ALIGNED(addr, pgsz) || !IS_P2ALIGNED(len, pgsz)) {
76480Sstevel@tonic-gate 			panic("segvn_reclaim: unaligned addr or len");
76490Sstevel@tonic-gate 			/*NOTREACHED*/
76500Sstevel@tonic-gate 		}
76510Sstevel@tonic-gate 	}
76520Sstevel@tonic-gate 
76530Sstevel@tonic-gate 	while (np > (uint_t)0) {
76540Sstevel@tonic-gate 		if (rw == S_WRITE) {
76550Sstevel@tonic-gate 			hat_setrefmod(*pplist);
76560Sstevel@tonic-gate 		} else {
76570Sstevel@tonic-gate 			hat_setref(*pplist);
76580Sstevel@tonic-gate 		}
76590Sstevel@tonic-gate 		page_unlock(*pplist);
76600Sstevel@tonic-gate 		np--;
76610Sstevel@tonic-gate 		pplist++;
76620Sstevel@tonic-gate 	}
76630Sstevel@tonic-gate 	kmem_free(pl, sizeof (page_t *) * npages);
76640Sstevel@tonic-gate 
76650Sstevel@tonic-gate 	mutex_enter(&freemem_lock);
76660Sstevel@tonic-gate 	availrmem += npages;
76670Sstevel@tonic-gate 	segvn_pages_locked -= npages;
76680Sstevel@tonic-gate 	svd->softlockcnt -= npages;
76690Sstevel@tonic-gate 	mutex_exit(&freemem_lock);
76700Sstevel@tonic-gate 	if (svd->softlockcnt <= 0) {
76710Sstevel@tonic-gate 		if (AS_ISUNMAPWAIT(seg->s_as)) {
76720Sstevel@tonic-gate 			mutex_enter(&seg->s_as->a_contents);
76730Sstevel@tonic-gate 			if (AS_ISUNMAPWAIT(seg->s_as)) {
76740Sstevel@tonic-gate 				AS_CLRUNMAPWAIT(seg->s_as);
76750Sstevel@tonic-gate 				cv_broadcast(&seg->s_as->a_cv);
76760Sstevel@tonic-gate 			}
76770Sstevel@tonic-gate 			mutex_exit(&seg->s_as->a_contents);
76780Sstevel@tonic-gate 		}
76790Sstevel@tonic-gate 	}
76800Sstevel@tonic-gate 	return (0);
76810Sstevel@tonic-gate }
76820Sstevel@tonic-gate /*
76830Sstevel@tonic-gate  * get a memory ID for an addr in a given segment
76840Sstevel@tonic-gate  *
76850Sstevel@tonic-gate  * XXX only creates PAGESIZE pages if anon slots are not initialized.
76860Sstevel@tonic-gate  * At fault time they will be relocated into larger pages.
76870Sstevel@tonic-gate  */
76880Sstevel@tonic-gate static int
76890Sstevel@tonic-gate segvn_getmemid(struct seg *seg, caddr_t addr, memid_t *memidp)
76900Sstevel@tonic-gate {
76910Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
76920Sstevel@tonic-gate 	struct anon 	*ap = NULL;
76930Sstevel@tonic-gate 	ulong_t		anon_index;
76940Sstevel@tonic-gate 	struct anon_map	*amp;
76950Sstevel@tonic-gate 	anon_sync_obj_t cookie;
76960Sstevel@tonic-gate 
76970Sstevel@tonic-gate 	if (svd->type == MAP_PRIVATE) {
76980Sstevel@tonic-gate 		memidp->val[0] = (uintptr_t)seg->s_as;
76990Sstevel@tonic-gate 		memidp->val[1] = (uintptr_t)addr;
77000Sstevel@tonic-gate 		return (0);
77010Sstevel@tonic-gate 	}
77020Sstevel@tonic-gate 
77030Sstevel@tonic-gate 	if (svd->type == MAP_SHARED) {
77040Sstevel@tonic-gate 		if (svd->vp) {
77050Sstevel@tonic-gate 			memidp->val[0] = (uintptr_t)svd->vp;
77060Sstevel@tonic-gate 			memidp->val[1] = (u_longlong_t)svd->offset +
77070Sstevel@tonic-gate 			    (uintptr_t)(addr - seg->s_base);
77080Sstevel@tonic-gate 			return (0);
77090Sstevel@tonic-gate 		} else {
77100Sstevel@tonic-gate 
77110Sstevel@tonic-gate 			SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
77120Sstevel@tonic-gate 			if ((amp = svd->amp) != NULL) {
77130Sstevel@tonic-gate 				anon_index = svd->anon_index +
77140Sstevel@tonic-gate 				    seg_page(seg, addr);
77150Sstevel@tonic-gate 			}
77160Sstevel@tonic-gate 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
77170Sstevel@tonic-gate 
77180Sstevel@tonic-gate 			ASSERT(amp != NULL);
77190Sstevel@tonic-gate 
77200Sstevel@tonic-gate 			ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
77210Sstevel@tonic-gate 			anon_array_enter(amp, anon_index, &cookie);
77220Sstevel@tonic-gate 			ap = anon_get_ptr(amp->ahp, anon_index);
77230Sstevel@tonic-gate 			if (ap == NULL) {
77240Sstevel@tonic-gate 				page_t		*pp;
77250Sstevel@tonic-gate 
77260Sstevel@tonic-gate 				pp = anon_zero(seg, addr, &ap, svd->cred);
77270Sstevel@tonic-gate 				if (pp == NULL) {
77280Sstevel@tonic-gate 					anon_array_exit(&cookie);
77290Sstevel@tonic-gate 					ANON_LOCK_EXIT(&amp->a_rwlock);
77300Sstevel@tonic-gate 					return (ENOMEM);
77310Sstevel@tonic-gate 				}
77320Sstevel@tonic-gate 				ASSERT(anon_get_ptr(amp->ahp, anon_index)
77330Sstevel@tonic-gate 								== NULL);
77340Sstevel@tonic-gate 				(void) anon_set_ptr(amp->ahp, anon_index,
77350Sstevel@tonic-gate 				    ap, ANON_SLEEP);
77360Sstevel@tonic-gate 				page_unlock(pp);
77370Sstevel@tonic-gate 			}
77380Sstevel@tonic-gate 
77390Sstevel@tonic-gate 			anon_array_exit(&cookie);
77400Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
77410Sstevel@tonic-gate 
77420Sstevel@tonic-gate 			memidp->val[0] = (uintptr_t)ap;
77430Sstevel@tonic-gate 			memidp->val[1] = (uintptr_t)addr & PAGEOFFSET;
77440Sstevel@tonic-gate 			return (0);
77450Sstevel@tonic-gate 		}
77460Sstevel@tonic-gate 	}
77470Sstevel@tonic-gate 	return (EINVAL);
77480Sstevel@tonic-gate }
77490Sstevel@tonic-gate 
77500Sstevel@tonic-gate static int
77510Sstevel@tonic-gate sameprot(struct seg *seg, caddr_t a, size_t len)
77520Sstevel@tonic-gate {
77530Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
77540Sstevel@tonic-gate 	struct vpage *vpage;
77550Sstevel@tonic-gate 	spgcnt_t pages = btop(len);
77560Sstevel@tonic-gate 	uint_t prot;
77570Sstevel@tonic-gate 
77580Sstevel@tonic-gate 	if (svd->pageprot == 0)
77590Sstevel@tonic-gate 		return (1);
77600Sstevel@tonic-gate 
77610Sstevel@tonic-gate 	ASSERT(svd->vpage != NULL);
77620Sstevel@tonic-gate 
77630Sstevel@tonic-gate 	vpage = &svd->vpage[seg_page(seg, a)];
77640Sstevel@tonic-gate 	prot = VPP_PROT(vpage);
77650Sstevel@tonic-gate 	vpage++;
77660Sstevel@tonic-gate 	pages--;
77670Sstevel@tonic-gate 	while (pages-- > 0) {
77680Sstevel@tonic-gate 		if (prot != VPP_PROT(vpage))
77690Sstevel@tonic-gate 			return (0);
77700Sstevel@tonic-gate 		vpage++;
77710Sstevel@tonic-gate 	}
77720Sstevel@tonic-gate 	return (1);
77730Sstevel@tonic-gate }
77740Sstevel@tonic-gate 
77750Sstevel@tonic-gate /*
77760Sstevel@tonic-gate  * Get memory allocation policy info for specified address in given segment
77770Sstevel@tonic-gate  */
77780Sstevel@tonic-gate static lgrp_mem_policy_info_t *
77790Sstevel@tonic-gate segvn_getpolicy(struct seg *seg, caddr_t addr)
77800Sstevel@tonic-gate {
77810Sstevel@tonic-gate 	struct anon_map		*amp;
77820Sstevel@tonic-gate 	ulong_t			anon_index;
77830Sstevel@tonic-gate 	lgrp_mem_policy_info_t	*policy_info;
77840Sstevel@tonic-gate 	struct segvn_data	*svn_data;
77850Sstevel@tonic-gate 	u_offset_t		vn_off;
77860Sstevel@tonic-gate 	vnode_t			*vp;
77870Sstevel@tonic-gate 
77880Sstevel@tonic-gate 	ASSERT(seg != NULL);
77890Sstevel@tonic-gate 
77900Sstevel@tonic-gate 	svn_data = (struct segvn_data *)seg->s_data;
77910Sstevel@tonic-gate 	if (svn_data == NULL)
77920Sstevel@tonic-gate 		return (NULL);
77930Sstevel@tonic-gate 
77940Sstevel@tonic-gate 	/*
77950Sstevel@tonic-gate 	 * Get policy info for private or shared memory
77960Sstevel@tonic-gate 	 */
77970Sstevel@tonic-gate 	if (svn_data->type != MAP_SHARED)
77980Sstevel@tonic-gate 		policy_info = &svn_data->policy_info;
77990Sstevel@tonic-gate 	else {
78000Sstevel@tonic-gate 		amp = svn_data->amp;
78010Sstevel@tonic-gate 		anon_index = svn_data->anon_index + seg_page(seg, addr);
78020Sstevel@tonic-gate 		vp = svn_data->vp;
78030Sstevel@tonic-gate 		vn_off = svn_data->offset + (uintptr_t)(addr - seg->s_base);
78040Sstevel@tonic-gate 		policy_info = lgrp_shm_policy_get(amp, anon_index, vp, vn_off);
78050Sstevel@tonic-gate 	}
78060Sstevel@tonic-gate 
78070Sstevel@tonic-gate 	return (policy_info);
78080Sstevel@tonic-gate }
7809*670Selowe 
7810*670Selowe /*ARGSUSED*/
7811*670Selowe static int
7812*670Selowe segvn_capable(struct seg *seg, segcapability_t capability)
7813*670Selowe {
7814*670Selowe 	return (0);
7815*670Selowe }
7816