xref: /onnv-gate/usr/src/uts/common/vm/seg_vn.c (revision 3351)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52414Saguzovsk  * Common Development and Distribution License (the "License").
62414Saguzovsk  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*3351Saguzovsk  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
310Sstevel@tonic-gate  * The Regents of the University of California
320Sstevel@tonic-gate  * All Rights Reserved
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
350Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
360Sstevel@tonic-gate  * contributors.
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
400Sstevel@tonic-gate 
410Sstevel@tonic-gate /*
420Sstevel@tonic-gate  * VM - shared or copy-on-write from a vnode/anonymous memory.
430Sstevel@tonic-gate  */
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #include <sys/types.h>
460Sstevel@tonic-gate #include <sys/param.h>
470Sstevel@tonic-gate #include <sys/t_lock.h>
480Sstevel@tonic-gate #include <sys/errno.h>
490Sstevel@tonic-gate #include <sys/systm.h>
500Sstevel@tonic-gate #include <sys/mman.h>
510Sstevel@tonic-gate #include <sys/debug.h>
520Sstevel@tonic-gate #include <sys/cred.h>
530Sstevel@tonic-gate #include <sys/vmsystm.h>
540Sstevel@tonic-gate #include <sys/tuneable.h>
550Sstevel@tonic-gate #include <sys/bitmap.h>
560Sstevel@tonic-gate #include <sys/swap.h>
570Sstevel@tonic-gate #include <sys/kmem.h>
580Sstevel@tonic-gate #include <sys/sysmacros.h>
590Sstevel@tonic-gate #include <sys/vtrace.h>
600Sstevel@tonic-gate #include <sys/cmn_err.h>
610Sstevel@tonic-gate #include <sys/vm.h>
620Sstevel@tonic-gate #include <sys/dumphdr.h>
630Sstevel@tonic-gate #include <sys/lgrp.h>
640Sstevel@tonic-gate 
650Sstevel@tonic-gate #include <vm/hat.h>
660Sstevel@tonic-gate #include <vm/as.h>
670Sstevel@tonic-gate #include <vm/seg.h>
680Sstevel@tonic-gate #include <vm/seg_vn.h>
690Sstevel@tonic-gate #include <vm/pvn.h>
700Sstevel@tonic-gate #include <vm/anon.h>
710Sstevel@tonic-gate #include <vm/page.h>
720Sstevel@tonic-gate #include <vm/vpage.h>
732768Ssl108498 #include <sys/proc.h>
742768Ssl108498 #include <sys/task.h>
752768Ssl108498 #include <sys/project.h>
762768Ssl108498 #include <sys/zone.h>
772768Ssl108498 #include <sys/shm_impl.h>
780Sstevel@tonic-gate /*
790Sstevel@tonic-gate  * Private seg op routines.
800Sstevel@tonic-gate  */
810Sstevel@tonic-gate static int	segvn_dup(struct seg *seg, struct seg *newseg);
820Sstevel@tonic-gate static int	segvn_unmap(struct seg *seg, caddr_t addr, size_t len);
830Sstevel@tonic-gate static void	segvn_free(struct seg *seg);
840Sstevel@tonic-gate static faultcode_t segvn_fault(struct hat *hat, struct seg *seg,
850Sstevel@tonic-gate 		    caddr_t addr, size_t len, enum fault_type type,
860Sstevel@tonic-gate 		    enum seg_rw rw);
870Sstevel@tonic-gate static faultcode_t segvn_faulta(struct seg *seg, caddr_t addr);
880Sstevel@tonic-gate static int	segvn_setprot(struct seg *seg, caddr_t addr,
890Sstevel@tonic-gate 		    size_t len, uint_t prot);
900Sstevel@tonic-gate static int	segvn_checkprot(struct seg *seg, caddr_t addr,
910Sstevel@tonic-gate 		    size_t len, uint_t prot);
920Sstevel@tonic-gate static int	segvn_kluster(struct seg *seg, caddr_t addr, ssize_t delta);
930Sstevel@tonic-gate static size_t	segvn_swapout(struct seg *seg);
940Sstevel@tonic-gate static int	segvn_sync(struct seg *seg, caddr_t addr, size_t len,
950Sstevel@tonic-gate 		    int attr, uint_t flags);
960Sstevel@tonic-gate static size_t	segvn_incore(struct seg *seg, caddr_t addr, size_t len,
970Sstevel@tonic-gate 		    char *vec);
980Sstevel@tonic-gate static int	segvn_lockop(struct seg *seg, caddr_t addr, size_t len,
990Sstevel@tonic-gate 		    int attr, int op, ulong_t *lockmap, size_t pos);
1000Sstevel@tonic-gate static int	segvn_getprot(struct seg *seg, caddr_t addr, size_t len,
1010Sstevel@tonic-gate 		    uint_t *protv);
1020Sstevel@tonic-gate static u_offset_t	segvn_getoffset(struct seg *seg, caddr_t addr);
1030Sstevel@tonic-gate static int	segvn_gettype(struct seg *seg, caddr_t addr);
1040Sstevel@tonic-gate static int	segvn_getvp(struct seg *seg, caddr_t addr,
1050Sstevel@tonic-gate 		    struct vnode **vpp);
1060Sstevel@tonic-gate static int	segvn_advise(struct seg *seg, caddr_t addr, size_t len,
1070Sstevel@tonic-gate 		    uint_t behav);
1080Sstevel@tonic-gate static void	segvn_dump(struct seg *seg);
1090Sstevel@tonic-gate static int	segvn_pagelock(struct seg *seg, caddr_t addr, size_t len,
1100Sstevel@tonic-gate 		    struct page ***ppp, enum lock_type type, enum seg_rw rw);
1110Sstevel@tonic-gate static int	segvn_setpagesize(struct seg *seg, caddr_t addr, size_t len,
1120Sstevel@tonic-gate 		    uint_t szc);
1130Sstevel@tonic-gate static int	segvn_getmemid(struct seg *seg, caddr_t addr,
1140Sstevel@tonic-gate 		    memid_t *memidp);
1150Sstevel@tonic-gate static lgrp_mem_policy_info_t	*segvn_getpolicy(struct seg *, caddr_t);
116670Selowe static int	segvn_capable(struct seg *seg, segcapability_t capable);
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate struct	seg_ops segvn_ops = {
1190Sstevel@tonic-gate 	segvn_dup,
1200Sstevel@tonic-gate 	segvn_unmap,
1210Sstevel@tonic-gate 	segvn_free,
1220Sstevel@tonic-gate 	segvn_fault,
1230Sstevel@tonic-gate 	segvn_faulta,
1240Sstevel@tonic-gate 	segvn_setprot,
1250Sstevel@tonic-gate 	segvn_checkprot,
1260Sstevel@tonic-gate 	segvn_kluster,
1270Sstevel@tonic-gate 	segvn_swapout,
1280Sstevel@tonic-gate 	segvn_sync,
1290Sstevel@tonic-gate 	segvn_incore,
1300Sstevel@tonic-gate 	segvn_lockop,
1310Sstevel@tonic-gate 	segvn_getprot,
1320Sstevel@tonic-gate 	segvn_getoffset,
1330Sstevel@tonic-gate 	segvn_gettype,
1340Sstevel@tonic-gate 	segvn_getvp,
1350Sstevel@tonic-gate 	segvn_advise,
1360Sstevel@tonic-gate 	segvn_dump,
1370Sstevel@tonic-gate 	segvn_pagelock,
1380Sstevel@tonic-gate 	segvn_setpagesize,
1390Sstevel@tonic-gate 	segvn_getmemid,
1400Sstevel@tonic-gate 	segvn_getpolicy,
141670Selowe 	segvn_capable,
1420Sstevel@tonic-gate };
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate /*
1450Sstevel@tonic-gate  * Common zfod structures, provided as a shorthand for others to use.
1460Sstevel@tonic-gate  */
1470Sstevel@tonic-gate static segvn_crargs_t zfod_segvn_crargs =
1480Sstevel@tonic-gate 	SEGVN_ZFOD_ARGS(PROT_ZFOD, PROT_ALL);
1490Sstevel@tonic-gate static segvn_crargs_t kzfod_segvn_crargs =
1500Sstevel@tonic-gate 	SEGVN_ZFOD_ARGS(PROT_ZFOD & ~PROT_USER,
1510Sstevel@tonic-gate 	PROT_ALL & ~PROT_USER);
1520Sstevel@tonic-gate static segvn_crargs_t stack_noexec_crargs =
1530Sstevel@tonic-gate 	SEGVN_ZFOD_ARGS(PROT_ZFOD & ~PROT_EXEC, PROT_ALL);
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate caddr_t	zfod_argsp = (caddr_t)&zfod_segvn_crargs;	/* user zfod argsp */
1560Sstevel@tonic-gate caddr_t	kzfod_argsp = (caddr_t)&kzfod_segvn_crargs;	/* kernel zfod argsp */
1570Sstevel@tonic-gate caddr_t	stack_exec_argsp = (caddr_t)&zfod_segvn_crargs;	/* executable stack */
1580Sstevel@tonic-gate caddr_t	stack_noexec_argsp = (caddr_t)&stack_noexec_crargs; /* noexec stack */
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate #define	vpgtob(n)	((n) * sizeof (struct vpage))	/* For brevity */
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate size_t	segvn_comb_thrshld = UINT_MAX;	/* patchable -- see 1196681 */
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate static int	segvn_concat(struct seg *, struct seg *, int);
1650Sstevel@tonic-gate static int	segvn_extend_prev(struct seg *, struct seg *,
1660Sstevel@tonic-gate 		    struct segvn_crargs *, size_t);
1670Sstevel@tonic-gate static int	segvn_extend_next(struct seg *, struct seg *,
1680Sstevel@tonic-gate 		    struct segvn_crargs *, size_t);
1690Sstevel@tonic-gate static void	segvn_softunlock(struct seg *, caddr_t, size_t, enum seg_rw);
1700Sstevel@tonic-gate static void	segvn_pagelist_rele(page_t **);
1710Sstevel@tonic-gate static void	segvn_setvnode_mpss(vnode_t *);
1720Sstevel@tonic-gate static void	segvn_relocate_pages(page_t **, page_t *);
1730Sstevel@tonic-gate static int	segvn_full_szcpages(page_t **, uint_t, int *, uint_t *);
1740Sstevel@tonic-gate static int	segvn_fill_vp_pages(struct segvn_data *, vnode_t *, u_offset_t,
1750Sstevel@tonic-gate     uint_t, page_t **, page_t **, uint_t *, int *);
1760Sstevel@tonic-gate static faultcode_t segvn_fault_vnodepages(struct hat *, struct seg *, caddr_t,
1770Sstevel@tonic-gate     caddr_t, enum fault_type, enum seg_rw, caddr_t, caddr_t, int);
1780Sstevel@tonic-gate static faultcode_t segvn_fault_anonpages(struct hat *, struct seg *, caddr_t,
1790Sstevel@tonic-gate     caddr_t, enum fault_type, enum seg_rw, caddr_t, caddr_t, int);
1800Sstevel@tonic-gate static faultcode_t segvn_faultpage(struct hat *, struct seg *, caddr_t,
1810Sstevel@tonic-gate     u_offset_t, struct vpage *, page_t **, uint_t,
1822414Saguzovsk     enum fault_type, enum seg_rw, int, int);
1830Sstevel@tonic-gate static void	segvn_vpage(struct seg *);
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate static void segvn_purge(struct seg *seg);
1860Sstevel@tonic-gate static int segvn_reclaim(struct seg *, caddr_t, size_t, struct page **,
1870Sstevel@tonic-gate     enum seg_rw);
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate static int sameprot(struct seg *, caddr_t, size_t);
1900Sstevel@tonic-gate 
1912414Saguzovsk static int segvn_demote_range(struct seg *, caddr_t, size_t, int, uint_t);
1920Sstevel@tonic-gate static int segvn_clrszc(struct seg *);
1930Sstevel@tonic-gate static struct seg *segvn_split_seg(struct seg *, caddr_t);
1940Sstevel@tonic-gate static int segvn_claim_pages(struct seg *, struct vpage *, u_offset_t,
1950Sstevel@tonic-gate     ulong_t, uint_t);
1960Sstevel@tonic-gate 
1972414Saguzovsk static int segvn_pp_lock_anonpages(page_t *, int);
1982414Saguzovsk static void segvn_pp_unlock_anonpages(page_t *, int);
1992414Saguzovsk 
2000Sstevel@tonic-gate static struct kmem_cache *segvn_cache;
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate #ifdef VM_STATS
2030Sstevel@tonic-gate static struct segvnvmstats_str {
2040Sstevel@tonic-gate 	ulong_t	fill_vp_pages[31];
2050Sstevel@tonic-gate 	ulong_t fltvnpages[49];
2060Sstevel@tonic-gate 	ulong_t	fullszcpages[10];
2070Sstevel@tonic-gate 	ulong_t	relocatepages[3];
2080Sstevel@tonic-gate 	ulong_t	fltanpages[17];
2090Sstevel@tonic-gate 	ulong_t pagelock[3];
2100Sstevel@tonic-gate 	ulong_t	demoterange[3];
2110Sstevel@tonic-gate } segvnvmstats;
2120Sstevel@tonic-gate #endif /* VM_STATS */
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate #define	SDR_RANGE	1		/* demote entire range */
2150Sstevel@tonic-gate #define	SDR_END		2		/* demote non aligned ends only */
2160Sstevel@tonic-gate 
2172768Ssl108498 #define	CALC_LPG_REGION(pgsz, seg, addr, len, lpgaddr, lpgeaddr) {	    \
2180Sstevel@tonic-gate 		if ((len) != 0) { 		      	      		      \
2190Sstevel@tonic-gate 			lpgaddr = (caddr_t)P2ALIGN((uintptr_t)(addr), pgsz);  \
2200Sstevel@tonic-gate 			ASSERT(lpgaddr >= (seg)->s_base);	      	      \
2210Sstevel@tonic-gate 			lpgeaddr = (caddr_t)P2ROUNDUP((uintptr_t)((addr) +    \
2220Sstevel@tonic-gate 			    (len)), pgsz);				      \
2230Sstevel@tonic-gate 			ASSERT(lpgeaddr > lpgaddr);		      	      \
2240Sstevel@tonic-gate 			ASSERT(lpgeaddr <= (seg)->s_base + (seg)->s_size);    \
2250Sstevel@tonic-gate 		} else {					      	      \
2260Sstevel@tonic-gate 			lpgeaddr = lpgaddr = (addr);	      		      \
2270Sstevel@tonic-gate 		}							      \
2280Sstevel@tonic-gate 	}
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate /*ARGSUSED*/
2310Sstevel@tonic-gate static int
2320Sstevel@tonic-gate segvn_cache_constructor(void *buf, void *cdrarg, int kmflags)
2330Sstevel@tonic-gate {
2340Sstevel@tonic-gate 	struct segvn_data *svd = buf;
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	rw_init(&svd->lock, NULL, RW_DEFAULT, NULL);
2370Sstevel@tonic-gate 	mutex_init(&svd->segp_slock, NULL, MUTEX_DEFAULT, NULL);
2380Sstevel@tonic-gate 	return (0);
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate /*ARGSUSED1*/
2420Sstevel@tonic-gate static void
2430Sstevel@tonic-gate segvn_cache_destructor(void *buf, void *cdrarg)
2440Sstevel@tonic-gate {
2450Sstevel@tonic-gate 	struct segvn_data *svd = buf;
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	rw_destroy(&svd->lock);
2480Sstevel@tonic-gate 	mutex_destroy(&svd->segp_slock);
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate /*
2520Sstevel@tonic-gate  * Patching this variable to non-zero allows the system to run with
2530Sstevel@tonic-gate  * stacks marked as "not executable".  It's a bit of a kludge, but is
2540Sstevel@tonic-gate  * provided as a tweakable for platforms that export those ABIs
2550Sstevel@tonic-gate  * (e.g. sparc V8) that have executable stacks enabled by default.
2560Sstevel@tonic-gate  * There are also some restrictions for platforms that don't actually
2570Sstevel@tonic-gate  * implement 'noexec' protections.
2580Sstevel@tonic-gate  *
2590Sstevel@tonic-gate  * Once enabled, the system is (therefore) unable to provide a fully
2600Sstevel@tonic-gate  * ABI-compliant execution environment, though practically speaking,
2610Sstevel@tonic-gate  * most everything works.  The exceptions are generally some interpreters
2620Sstevel@tonic-gate  * and debuggers that create executable code on the stack and jump
2630Sstevel@tonic-gate  * into it (without explicitly mprotecting the address range to include
2640Sstevel@tonic-gate  * PROT_EXEC).
2650Sstevel@tonic-gate  *
2660Sstevel@tonic-gate  * One important class of applications that are disabled are those
2670Sstevel@tonic-gate  * that have been transformed into malicious agents using one of the
2680Sstevel@tonic-gate  * numerous "buffer overflow" attacks.  See 4007890.
2690Sstevel@tonic-gate  */
2700Sstevel@tonic-gate int noexec_user_stack = 0;
2710Sstevel@tonic-gate int noexec_user_stack_log = 1;
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate int segvn_lpg_disable = 0;
2740Sstevel@tonic-gate uint_t segvn_maxpgszc = 0;
2750Sstevel@tonic-gate 
27663Saguzovsk ulong_t segvn_vmpss_clrszc_cnt;
27763Saguzovsk ulong_t segvn_vmpss_clrszc_err;
27863Saguzovsk ulong_t segvn_fltvnpages_clrszc_cnt;
2790Sstevel@tonic-gate ulong_t segvn_fltvnpages_clrszc_err;
2800Sstevel@tonic-gate ulong_t segvn_setpgsz_align_err;
2812414Saguzovsk ulong_t segvn_setpgsz_anon_align_err;
2820Sstevel@tonic-gate ulong_t segvn_setpgsz_getattr_err;
2830Sstevel@tonic-gate ulong_t segvn_setpgsz_eof_err;
2840Sstevel@tonic-gate ulong_t segvn_faultvnmpss_align_err1;
2850Sstevel@tonic-gate ulong_t segvn_faultvnmpss_align_err2;
2860Sstevel@tonic-gate ulong_t segvn_faultvnmpss_align_err3;
2870Sstevel@tonic-gate ulong_t segvn_faultvnmpss_align_err4;
2880Sstevel@tonic-gate ulong_t segvn_faultvnmpss_align_err5;
2890Sstevel@tonic-gate ulong_t	segvn_vmpss_pageio_deadlk_err;
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate /*
2920Sstevel@tonic-gate  * Initialize segvn data structures
2930Sstevel@tonic-gate  */
2940Sstevel@tonic-gate void
2950Sstevel@tonic-gate segvn_init(void)
2960Sstevel@tonic-gate {
2970Sstevel@tonic-gate 	uint_t maxszc;
2980Sstevel@tonic-gate 	uint_t szc;
2990Sstevel@tonic-gate 	size_t pgsz;
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	segvn_cache = kmem_cache_create("segvn_cache",
3020Sstevel@tonic-gate 		sizeof (struct segvn_data), 0,
3030Sstevel@tonic-gate 		segvn_cache_constructor, segvn_cache_destructor, NULL,
3040Sstevel@tonic-gate 		NULL, NULL, 0);
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	if (segvn_lpg_disable != 0)
3070Sstevel@tonic-gate 		return;
3080Sstevel@tonic-gate 	szc = maxszc = page_num_pagesizes() - 1;
3090Sstevel@tonic-gate 	if (szc == 0) {
3100Sstevel@tonic-gate 		segvn_lpg_disable = 1;
3110Sstevel@tonic-gate 		return;
3120Sstevel@tonic-gate 	}
3130Sstevel@tonic-gate 	if (page_get_pagesize(0) != PAGESIZE) {
3140Sstevel@tonic-gate 		panic("segvn_init: bad szc 0");
3150Sstevel@tonic-gate 		/*NOTREACHED*/
3160Sstevel@tonic-gate 	}
3170Sstevel@tonic-gate 	while (szc != 0) {
3180Sstevel@tonic-gate 		pgsz = page_get_pagesize(szc);
3190Sstevel@tonic-gate 		if (pgsz <= PAGESIZE || !IS_P2ALIGNED(pgsz, pgsz)) {
3200Sstevel@tonic-gate 			panic("segvn_init: bad szc %d", szc);
3210Sstevel@tonic-gate 			/*NOTREACHED*/
3220Sstevel@tonic-gate 		}
3230Sstevel@tonic-gate 		szc--;
3240Sstevel@tonic-gate 	}
3250Sstevel@tonic-gate 	if (segvn_maxpgszc == 0 || segvn_maxpgszc > maxszc)
3260Sstevel@tonic-gate 		segvn_maxpgszc = maxszc;
3270Sstevel@tonic-gate }
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate #define	SEGVN_PAGEIO	((void *)0x1)
3300Sstevel@tonic-gate #define	SEGVN_NOPAGEIO	((void *)0x2)
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate static void
3330Sstevel@tonic-gate segvn_setvnode_mpss(vnode_t *vp)
3340Sstevel@tonic-gate {
3350Sstevel@tonic-gate 	int err;
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	ASSERT(vp->v_mpssdata == NULL ||
3380Sstevel@tonic-gate 	    vp->v_mpssdata == SEGVN_PAGEIO ||
3390Sstevel@tonic-gate 	    vp->v_mpssdata == SEGVN_NOPAGEIO);
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	if (vp->v_mpssdata == NULL) {
3420Sstevel@tonic-gate 		if (vn_vmpss_usepageio(vp)) {
3430Sstevel@tonic-gate 			err = VOP_PAGEIO(vp, (page_t *)NULL,
3440Sstevel@tonic-gate 			    (u_offset_t)0, 0, 0, CRED());
3450Sstevel@tonic-gate 		} else {
3460Sstevel@tonic-gate 			err = ENOSYS;
3470Sstevel@tonic-gate 		}
3480Sstevel@tonic-gate 		/*
3490Sstevel@tonic-gate 		 * set v_mpssdata just once per vnode life
3500Sstevel@tonic-gate 		 * so that it never changes.
3510Sstevel@tonic-gate 		 */
3520Sstevel@tonic-gate 		mutex_enter(&vp->v_lock);
3530Sstevel@tonic-gate 		if (vp->v_mpssdata == NULL) {
3540Sstevel@tonic-gate 			if (err == EINVAL) {
3550Sstevel@tonic-gate 				vp->v_mpssdata = SEGVN_PAGEIO;
3560Sstevel@tonic-gate 			} else {
3570Sstevel@tonic-gate 				vp->v_mpssdata = SEGVN_NOPAGEIO;
3580Sstevel@tonic-gate 			}
3590Sstevel@tonic-gate 		}
3600Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
3610Sstevel@tonic-gate 	}
3620Sstevel@tonic-gate }
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate int
3650Sstevel@tonic-gate segvn_create(struct seg *seg, void *argsp)
3660Sstevel@tonic-gate {
3670Sstevel@tonic-gate 	struct segvn_crargs *a = (struct segvn_crargs *)argsp;
3680Sstevel@tonic-gate 	struct segvn_data *svd;
3690Sstevel@tonic-gate 	size_t swresv = 0;
3700Sstevel@tonic-gate 	struct cred *cred;
3710Sstevel@tonic-gate 	struct anon_map *amp;
3720Sstevel@tonic-gate 	int error = 0;
3730Sstevel@tonic-gate 	size_t pgsz;
3740Sstevel@tonic-gate 	lgrp_mem_policy_t mpolicy = LGRP_MEM_POLICY_DEFAULT;
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	if (a->type != MAP_PRIVATE && a->type != MAP_SHARED) {
3800Sstevel@tonic-gate 		panic("segvn_create type");
3810Sstevel@tonic-gate 		/*NOTREACHED*/
3820Sstevel@tonic-gate 	}
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	/*
3850Sstevel@tonic-gate 	 * Check arguments.  If a shared anon structure is given then
3860Sstevel@tonic-gate 	 * it is illegal to also specify a vp.
3870Sstevel@tonic-gate 	 */
3880Sstevel@tonic-gate 	if (a->amp != NULL && a->vp != NULL) {
3890Sstevel@tonic-gate 		panic("segvn_create anon_map");
3900Sstevel@tonic-gate 		/*NOTREACHED*/
3910Sstevel@tonic-gate 	}
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	/* MAP_NORESERVE on a MAP_SHARED segment is meaningless. */
3940Sstevel@tonic-gate 	if (a->type == MAP_SHARED)
3950Sstevel@tonic-gate 		a->flags &= ~MAP_NORESERVE;
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	if (a->szc != 0) {
3982991Ssusans 		if (segvn_lpg_disable != 0 || (a->szc == AS_MAP_NO_LPOOB) ||
3992414Saguzovsk 		    (a->amp != NULL && a->type == MAP_PRIVATE) ||
4000Sstevel@tonic-gate 		    (a->flags & MAP_NORESERVE) || seg->s_as == &kas) {
4010Sstevel@tonic-gate 			a->szc = 0;
4020Sstevel@tonic-gate 		} else {
4030Sstevel@tonic-gate 			if (a->szc > segvn_maxpgszc)
4040Sstevel@tonic-gate 				a->szc = segvn_maxpgszc;
4050Sstevel@tonic-gate 			pgsz = page_get_pagesize(a->szc);
4060Sstevel@tonic-gate 			if (!IS_P2ALIGNED(seg->s_base, pgsz) ||
4070Sstevel@tonic-gate 			    !IS_P2ALIGNED(seg->s_size, pgsz)) {
4080Sstevel@tonic-gate 				a->szc = 0;
4090Sstevel@tonic-gate 			} else if (a->vp != NULL) {
4100Sstevel@tonic-gate 				extern struct vnode kvp;
4113290Sjohansen 				if (IS_SWAPFSVP(a->vp) || VN_ISKAS(a->vp)) {
4120Sstevel@tonic-gate 					/*
4130Sstevel@tonic-gate 					 * paranoid check.
4140Sstevel@tonic-gate 					 * hat_page_demote() is not supported
4150Sstevel@tonic-gate 					 * on swapfs pages.
4160Sstevel@tonic-gate 					 */
4170Sstevel@tonic-gate 					a->szc = 0;
4180Sstevel@tonic-gate 				} else if (map_addr_vacalign_check(seg->s_base,
4190Sstevel@tonic-gate 				    a->offset & PAGEMASK)) {
4200Sstevel@tonic-gate 					a->szc = 0;
4210Sstevel@tonic-gate 				}
4222414Saguzovsk 			} else if (a->amp != NULL) {
4232414Saguzovsk 				pgcnt_t anum = btopr(a->offset);
4242414Saguzovsk 				pgcnt_t pgcnt = page_get_pagecnt(a->szc);
4252414Saguzovsk 				if (!IS_P2ALIGNED(anum, pgcnt)) {
4262414Saguzovsk 					a->szc = 0;
4272414Saguzovsk 				}
4280Sstevel@tonic-gate 			}
4290Sstevel@tonic-gate 		}
4300Sstevel@tonic-gate 	}
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	/*
4330Sstevel@tonic-gate 	 * If segment may need private pages, reserve them now.
4340Sstevel@tonic-gate 	 */
4350Sstevel@tonic-gate 	if (!(a->flags & MAP_NORESERVE) && ((a->vp == NULL && a->amp == NULL) ||
4360Sstevel@tonic-gate 	    (a->type == MAP_PRIVATE && (a->prot & PROT_WRITE)))) {
4370Sstevel@tonic-gate 		if (anon_resv(seg->s_size) == 0)
4380Sstevel@tonic-gate 			return (EAGAIN);
4390Sstevel@tonic-gate 		swresv = seg->s_size;
4400Sstevel@tonic-gate 		TRACE_3(TR_FAC_VM, TR_ANON_PROC, "anon proc:%p %lu %u",
4410Sstevel@tonic-gate 			seg, swresv, 1);
4420Sstevel@tonic-gate 	}
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	/*
4450Sstevel@tonic-gate 	 * Reserve any mapping structures that may be required.
4460Sstevel@tonic-gate 	 */
4470Sstevel@tonic-gate 	hat_map(seg->s_as->a_hat, seg->s_base, seg->s_size, HAT_MAP);
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	if (a->cred) {
4500Sstevel@tonic-gate 		cred = a->cred;
4510Sstevel@tonic-gate 		crhold(cred);
4520Sstevel@tonic-gate 	} else {
4530Sstevel@tonic-gate 		crhold(cred = CRED());
4540Sstevel@tonic-gate 	}
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 	/* Inform the vnode of the new mapping */
4570Sstevel@tonic-gate 	if (a->vp) {
4580Sstevel@tonic-gate 		error = VOP_ADDMAP(a->vp, a->offset & PAGEMASK,
4590Sstevel@tonic-gate 		    seg->s_as, seg->s_base, seg->s_size, a->prot,
4600Sstevel@tonic-gate 		    a->maxprot, a->type, cred);
4610Sstevel@tonic-gate 		if (error) {
4620Sstevel@tonic-gate 			if (swresv != 0) {
4630Sstevel@tonic-gate 				anon_unresv(swresv);
4640Sstevel@tonic-gate 				TRACE_3(TR_FAC_VM, TR_ANON_PROC,
4650Sstevel@tonic-gate 					"anon proc:%p %lu %u",
4660Sstevel@tonic-gate 					seg, swresv, 0);
4670Sstevel@tonic-gate 			}
4680Sstevel@tonic-gate 			crfree(cred);
4690Sstevel@tonic-gate 			hat_unload(seg->s_as->a_hat, seg->s_base,
4700Sstevel@tonic-gate 				seg->s_size, HAT_UNLOAD_UNMAP);
4710Sstevel@tonic-gate 			return (error);
4720Sstevel@tonic-gate 		}
4730Sstevel@tonic-gate 	}
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	/*
4760Sstevel@tonic-gate 	 * If more than one segment in the address space, and
4770Sstevel@tonic-gate 	 * they're adjacent virtually, try to concatenate them.
4780Sstevel@tonic-gate 	 * Don't concatenate if an explicit anon_map structure
4790Sstevel@tonic-gate 	 * was supplied (e.g., SystemV shared memory).
4800Sstevel@tonic-gate 	 */
4810Sstevel@tonic-gate 	if (a->amp == NULL) {
4820Sstevel@tonic-gate 		struct seg *pseg, *nseg;
4830Sstevel@tonic-gate 		struct segvn_data *psvd, *nsvd;
4840Sstevel@tonic-gate 		lgrp_mem_policy_t ppolicy, npolicy;
4850Sstevel@tonic-gate 		uint_t	lgrp_mem_policy_flags = 0;
4860Sstevel@tonic-gate 		extern lgrp_mem_policy_t lgrp_mem_default_policy;
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 		/*
4890Sstevel@tonic-gate 		 * Memory policy flags (lgrp_mem_policy_flags) is valid when
4900Sstevel@tonic-gate 		 * extending stack/heap segments.
4910Sstevel@tonic-gate 		 */
4920Sstevel@tonic-gate 		if ((a->vp == NULL) && (a->type == MAP_PRIVATE) &&
4930Sstevel@tonic-gate 			!(a->flags & MAP_NORESERVE) && (seg->s_as != &kas)) {
4940Sstevel@tonic-gate 			lgrp_mem_policy_flags = a->lgrp_mem_policy_flags;
4950Sstevel@tonic-gate 		} else {
4960Sstevel@tonic-gate 			/*
4970Sstevel@tonic-gate 			 * Get policy when not extending it from another segment
4980Sstevel@tonic-gate 			 */
4990Sstevel@tonic-gate 			mpolicy = lgrp_mem_policy_default(seg->s_size, a->type);
5000Sstevel@tonic-gate 		}
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 		/*
5030Sstevel@tonic-gate 		 * First, try to concatenate the previous and new segments
5040Sstevel@tonic-gate 		 */
5050Sstevel@tonic-gate 		pseg = AS_SEGPREV(seg->s_as, seg);
5060Sstevel@tonic-gate 		if (pseg != NULL &&
5070Sstevel@tonic-gate 		    pseg->s_base + pseg->s_size == seg->s_base &&
5080Sstevel@tonic-gate 		    pseg->s_ops == &segvn_ops) {
5090Sstevel@tonic-gate 			/*
5100Sstevel@tonic-gate 			 * Get memory allocation policy from previous segment.
5110Sstevel@tonic-gate 			 * When extension is specified (e.g. for heap) apply
5120Sstevel@tonic-gate 			 * this policy to the new segment regardless of the
5130Sstevel@tonic-gate 			 * outcome of segment concatenation.  Extension occurs
5140Sstevel@tonic-gate 			 * for non-default policy otherwise default policy is
5150Sstevel@tonic-gate 			 * used and is based on extended segment size.
5160Sstevel@tonic-gate 			 */
5170Sstevel@tonic-gate 			psvd = (struct segvn_data *)pseg->s_data;
5180Sstevel@tonic-gate 			ppolicy = psvd->policy_info.mem_policy;
5190Sstevel@tonic-gate 			if (lgrp_mem_policy_flags ==
5200Sstevel@tonic-gate 			    LGRP_MP_FLAG_EXTEND_UP) {
5210Sstevel@tonic-gate 				if (ppolicy != lgrp_mem_default_policy) {
5220Sstevel@tonic-gate 					mpolicy = ppolicy;
5230Sstevel@tonic-gate 				} else {
5240Sstevel@tonic-gate 					mpolicy = lgrp_mem_policy_default(
5250Sstevel@tonic-gate 					    pseg->s_size + seg->s_size,
5260Sstevel@tonic-gate 					    a->type);
5270Sstevel@tonic-gate 				}
5280Sstevel@tonic-gate 			}
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 			if (mpolicy == ppolicy &&
5310Sstevel@tonic-gate 			    (pseg->s_size + seg->s_size <=
5320Sstevel@tonic-gate 			    segvn_comb_thrshld || psvd->amp == NULL) &&
5330Sstevel@tonic-gate 			    segvn_extend_prev(pseg, seg, a, swresv) == 0) {
5340Sstevel@tonic-gate 				/*
5350Sstevel@tonic-gate 				 * success! now try to concatenate
5360Sstevel@tonic-gate 				 * with following seg
5370Sstevel@tonic-gate 				 */
5380Sstevel@tonic-gate 				crfree(cred);
5390Sstevel@tonic-gate 				nseg = AS_SEGNEXT(pseg->s_as, pseg);
5400Sstevel@tonic-gate 				if (nseg != NULL &&
5410Sstevel@tonic-gate 				    nseg != pseg &&
5420Sstevel@tonic-gate 				    nseg->s_ops == &segvn_ops &&
5430Sstevel@tonic-gate 				    pseg->s_base + pseg->s_size ==
5440Sstevel@tonic-gate 				    nseg->s_base)
5450Sstevel@tonic-gate 					(void) segvn_concat(pseg, nseg, 0);
5460Sstevel@tonic-gate 				ASSERT(pseg->s_szc == 0 ||
5470Sstevel@tonic-gate 				    (a->szc == pseg->s_szc &&
5480Sstevel@tonic-gate 				    IS_P2ALIGNED(pseg->s_base, pgsz) &&
5490Sstevel@tonic-gate 				    IS_P2ALIGNED(pseg->s_size, pgsz)));
5500Sstevel@tonic-gate 				return (0);
5510Sstevel@tonic-gate 			}
5520Sstevel@tonic-gate 		}
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 		/*
5550Sstevel@tonic-gate 		 * Failed, so try to concatenate with following seg
5560Sstevel@tonic-gate 		 */
5570Sstevel@tonic-gate 		nseg = AS_SEGNEXT(seg->s_as, seg);
5580Sstevel@tonic-gate 		if (nseg != NULL &&
5590Sstevel@tonic-gate 		    seg->s_base + seg->s_size == nseg->s_base &&
5600Sstevel@tonic-gate 		    nseg->s_ops == &segvn_ops) {
5610Sstevel@tonic-gate 			/*
5620Sstevel@tonic-gate 			 * Get memory allocation policy from next segment.
5630Sstevel@tonic-gate 			 * When extension is specified (e.g. for stack) apply
5640Sstevel@tonic-gate 			 * this policy to the new segment regardless of the
5650Sstevel@tonic-gate 			 * outcome of segment concatenation.  Extension occurs
5660Sstevel@tonic-gate 			 * for non-default policy otherwise default policy is
5670Sstevel@tonic-gate 			 * used and is based on extended segment size.
5680Sstevel@tonic-gate 			 */
5690Sstevel@tonic-gate 			nsvd = (struct segvn_data *)nseg->s_data;
5700Sstevel@tonic-gate 			npolicy = nsvd->policy_info.mem_policy;
5710Sstevel@tonic-gate 			if (lgrp_mem_policy_flags ==
5720Sstevel@tonic-gate 			    LGRP_MP_FLAG_EXTEND_DOWN) {
5730Sstevel@tonic-gate 				if (npolicy != lgrp_mem_default_policy) {
5740Sstevel@tonic-gate 					mpolicy = npolicy;
5750Sstevel@tonic-gate 				} else {
5760Sstevel@tonic-gate 					mpolicy = lgrp_mem_policy_default(
5770Sstevel@tonic-gate 					    nseg->s_size + seg->s_size,
5780Sstevel@tonic-gate 					    a->type);
5790Sstevel@tonic-gate 				}
5800Sstevel@tonic-gate 			}
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 			if (mpolicy == npolicy &&
5830Sstevel@tonic-gate 			    segvn_extend_next(seg, nseg, a, swresv) == 0) {
5840Sstevel@tonic-gate 				crfree(cred);
5850Sstevel@tonic-gate 				ASSERT(nseg->s_szc == 0 ||
5860Sstevel@tonic-gate 				    (a->szc == nseg->s_szc &&
5870Sstevel@tonic-gate 				    IS_P2ALIGNED(nseg->s_base, pgsz) &&
5880Sstevel@tonic-gate 				    IS_P2ALIGNED(nseg->s_size, pgsz)));
5890Sstevel@tonic-gate 				return (0);
5900Sstevel@tonic-gate 			}
5910Sstevel@tonic-gate 		}
5920Sstevel@tonic-gate 	}
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	if (a->vp != NULL) {
5950Sstevel@tonic-gate 		VN_HOLD(a->vp);
5960Sstevel@tonic-gate 		if (a->type == MAP_SHARED)
5970Sstevel@tonic-gate 			lgrp_shm_policy_init(NULL, a->vp);
5980Sstevel@tonic-gate 	}
5990Sstevel@tonic-gate 	svd = kmem_cache_alloc(segvn_cache, KM_SLEEP);
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 	seg->s_ops = &segvn_ops;
6020Sstevel@tonic-gate 	seg->s_data = (void *)svd;
6030Sstevel@tonic-gate 	seg->s_szc = a->szc;
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	svd->vp = a->vp;
6060Sstevel@tonic-gate 	/*
6070Sstevel@tonic-gate 	 * Anonymous mappings have no backing file so the offset is meaningless.
6080Sstevel@tonic-gate 	 */
6090Sstevel@tonic-gate 	svd->offset = a->vp ? (a->offset & PAGEMASK) : 0;
6100Sstevel@tonic-gate 	svd->prot = a->prot;
6110Sstevel@tonic-gate 	svd->maxprot = a->maxprot;
6120Sstevel@tonic-gate 	svd->pageprot = 0;
6130Sstevel@tonic-gate 	svd->type = a->type;
6140Sstevel@tonic-gate 	svd->vpage = NULL;
6150Sstevel@tonic-gate 	svd->cred = cred;
6160Sstevel@tonic-gate 	svd->advice = MADV_NORMAL;
6170Sstevel@tonic-gate 	svd->pageadvice = 0;
6180Sstevel@tonic-gate 	svd->flags = (ushort_t)a->flags;
6190Sstevel@tonic-gate 	svd->softlockcnt = 0;
6200Sstevel@tonic-gate 	if (a->szc != 0 && a->vp != NULL) {
6210Sstevel@tonic-gate 		segvn_setvnode_mpss(a->vp);
6220Sstevel@tonic-gate 	}
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	amp = a->amp;
6250Sstevel@tonic-gate 	if ((svd->amp = amp) == NULL) {
6260Sstevel@tonic-gate 		svd->anon_index = 0;
6270Sstevel@tonic-gate 		if (svd->type == MAP_SHARED) {
6280Sstevel@tonic-gate 			svd->swresv = 0;
6290Sstevel@tonic-gate 			/*
6300Sstevel@tonic-gate 			 * Shared mappings to a vp need no other setup.
6310Sstevel@tonic-gate 			 * If we have a shared mapping to an anon_map object
6320Sstevel@tonic-gate 			 * which hasn't been allocated yet,  allocate the
6330Sstevel@tonic-gate 			 * struct now so that it will be properly shared
6340Sstevel@tonic-gate 			 * by remembering the swap reservation there.
6350Sstevel@tonic-gate 			 */
6360Sstevel@tonic-gate 			if (a->vp == NULL) {
6370Sstevel@tonic-gate 				svd->amp = anonmap_alloc(seg->s_size, swresv);
6380Sstevel@tonic-gate 				svd->amp->a_szc = seg->s_szc;
6390Sstevel@tonic-gate 			}
6400Sstevel@tonic-gate 		} else {
6410Sstevel@tonic-gate 			/*
6420Sstevel@tonic-gate 			 * Private mapping (with or without a vp).
6430Sstevel@tonic-gate 			 * Allocate anon_map when needed.
6440Sstevel@tonic-gate 			 */
6450Sstevel@tonic-gate 			svd->swresv = swresv;
6460Sstevel@tonic-gate 		}
6470Sstevel@tonic-gate 	} else {
6480Sstevel@tonic-gate 		pgcnt_t anon_num;
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 		/*
6510Sstevel@tonic-gate 		 * Mapping to an existing anon_map structure without a vp.
6520Sstevel@tonic-gate 		 * For now we will insure that the segment size isn't larger
6530Sstevel@tonic-gate 		 * than the size - offset gives us.  Later on we may wish to
6540Sstevel@tonic-gate 		 * have the anon array dynamically allocated itself so that
6550Sstevel@tonic-gate 		 * we don't always have to allocate all the anon pointer slots.
6560Sstevel@tonic-gate 		 * This of course involves adding extra code to check that we
6570Sstevel@tonic-gate 		 * aren't trying to use an anon pointer slot beyond the end
6580Sstevel@tonic-gate 		 * of the currently allocated anon array.
6590Sstevel@tonic-gate 		 */
6600Sstevel@tonic-gate 		if ((amp->size - a->offset) < seg->s_size) {
6610Sstevel@tonic-gate 			panic("segvn_create anon_map size");
6620Sstevel@tonic-gate 			/*NOTREACHED*/
6630Sstevel@tonic-gate 		}
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 		anon_num = btopr(a->offset);
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 		if (a->type == MAP_SHARED) {
6680Sstevel@tonic-gate 			/*
6690Sstevel@tonic-gate 			 * SHARED mapping to a given anon_map.
6700Sstevel@tonic-gate 			 */
6710Sstevel@tonic-gate 			ANON_LOCK_ENTER(&amp->a_rwlock, RW_WRITER);
6720Sstevel@tonic-gate 			amp->refcnt++;
6732414Saguzovsk 			if (a->szc > amp->a_szc) {
6742414Saguzovsk 				amp->a_szc = a->szc;
6752414Saguzovsk 			}
6760Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
6770Sstevel@tonic-gate 			svd->anon_index = anon_num;
6780Sstevel@tonic-gate 			svd->swresv = 0;
6790Sstevel@tonic-gate 		} else {
6800Sstevel@tonic-gate 			/*
6810Sstevel@tonic-gate 			 * PRIVATE mapping to a given anon_map.
6820Sstevel@tonic-gate 			 * Make sure that all the needed anon
6830Sstevel@tonic-gate 			 * structures are created (so that we will
6840Sstevel@tonic-gate 			 * share the underlying pages if nothing
6850Sstevel@tonic-gate 			 * is written by this mapping) and then
6860Sstevel@tonic-gate 			 * duplicate the anon array as is done
6870Sstevel@tonic-gate 			 * when a privately mapped segment is dup'ed.
6880Sstevel@tonic-gate 			 */
6890Sstevel@tonic-gate 			struct anon *ap;
6900Sstevel@tonic-gate 			caddr_t addr;
6910Sstevel@tonic-gate 			caddr_t eaddr;
6920Sstevel@tonic-gate 			ulong_t	anon_idx;
6930Sstevel@tonic-gate 			int hat_flag = HAT_LOAD;
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 			if (svd->flags & MAP_TEXT) {
6960Sstevel@tonic-gate 				hat_flag |= HAT_LOAD_TEXT;
6970Sstevel@tonic-gate 			}
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 			svd->amp = anonmap_alloc(seg->s_size, 0);
7000Sstevel@tonic-gate 			svd->amp->a_szc = seg->s_szc;
7010Sstevel@tonic-gate 			svd->anon_index = 0;
7020Sstevel@tonic-gate 			svd->swresv = swresv;
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 			/*
7050Sstevel@tonic-gate 			 * Prevent 2 threads from allocating anon
7060Sstevel@tonic-gate 			 * slots simultaneously.
7070Sstevel@tonic-gate 			 */
7080Sstevel@tonic-gate 			ANON_LOCK_ENTER(&amp->a_rwlock, RW_WRITER);
7090Sstevel@tonic-gate 			eaddr = seg->s_base + seg->s_size;
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 			for (anon_idx = anon_num, addr = seg->s_base;
7120Sstevel@tonic-gate 			    addr < eaddr; addr += PAGESIZE, anon_idx++) {
7130Sstevel@tonic-gate 				page_t *pp;
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 				if ((ap = anon_get_ptr(amp->ahp,
7160Sstevel@tonic-gate 				    anon_idx)) != NULL)
7170Sstevel@tonic-gate 					continue;
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 				/*
7200Sstevel@tonic-gate 				 * Allocate the anon struct now.
7210Sstevel@tonic-gate 				 * Might as well load up translation
7220Sstevel@tonic-gate 				 * to the page while we're at it...
7230Sstevel@tonic-gate 				 */
7240Sstevel@tonic-gate 				pp = anon_zero(seg, addr, &ap, cred);
7250Sstevel@tonic-gate 				if (ap == NULL || pp == NULL) {
7260Sstevel@tonic-gate 					panic("segvn_create anon_zero");
7270Sstevel@tonic-gate 					/*NOTREACHED*/
7280Sstevel@tonic-gate 				}
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 				/*
7310Sstevel@tonic-gate 				 * Re-acquire the anon_map lock and
7320Sstevel@tonic-gate 				 * initialize the anon array entry.
7330Sstevel@tonic-gate 				 */
7340Sstevel@tonic-gate 				ASSERT(anon_get_ptr(amp->ahp,
7350Sstevel@tonic-gate 				    anon_idx) == NULL);
7360Sstevel@tonic-gate 				(void) anon_set_ptr(amp->ahp, anon_idx, ap,
7370Sstevel@tonic-gate 				    ANON_SLEEP);
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 				ASSERT(seg->s_szc == 0);
7400Sstevel@tonic-gate 				ASSERT(!IS_VMODSORT(pp->p_vnode));
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 				hat_memload(seg->s_as->a_hat, addr, pp,
7430Sstevel@tonic-gate 					svd->prot & ~PROT_WRITE, hat_flag);
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 				page_unlock(pp);
7460Sstevel@tonic-gate 			}
7470Sstevel@tonic-gate 			ASSERT(seg->s_szc == 0);
7480Sstevel@tonic-gate 			anon_dup(amp->ahp, anon_num, svd->amp->ahp,
7490Sstevel@tonic-gate 			    0, seg->s_size);
7500Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
7510Sstevel@tonic-gate 		}
7520Sstevel@tonic-gate 	}
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 	/*
7550Sstevel@tonic-gate 	 * Set default memory allocation policy for segment
7560Sstevel@tonic-gate 	 *
7570Sstevel@tonic-gate 	 * Always set policy for private memory at least for initialization
7580Sstevel@tonic-gate 	 * even if this is a shared memory segment
7590Sstevel@tonic-gate 	 */
7600Sstevel@tonic-gate 	(void) lgrp_privm_policy_set(mpolicy, &svd->policy_info, seg->s_size);
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 	if (svd->type == MAP_SHARED)
7630Sstevel@tonic-gate 		(void) lgrp_shm_policy_set(mpolicy, svd->amp, svd->anon_index,
7640Sstevel@tonic-gate 		    svd->vp, svd->offset, seg->s_size);
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 	return (0);
7670Sstevel@tonic-gate }
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate /*
7700Sstevel@tonic-gate  * Concatenate two existing segments, if possible.
7710Sstevel@tonic-gate  * Return 0 on success, -1 if two segments are not compatible
7720Sstevel@tonic-gate  * or -2 on memory allocation failure.
7732414Saguzovsk  * If amp_cat == 1 then try and concat segments with anon maps
7740Sstevel@tonic-gate  */
7750Sstevel@tonic-gate static int
7762414Saguzovsk segvn_concat(struct seg *seg1, struct seg *seg2, int amp_cat)
7770Sstevel@tonic-gate {
7780Sstevel@tonic-gate 	struct segvn_data *svd1 = seg1->s_data;
7790Sstevel@tonic-gate 	struct segvn_data *svd2 = seg2->s_data;
7800Sstevel@tonic-gate 	struct anon_map *amp1 = svd1->amp;
7810Sstevel@tonic-gate 	struct anon_map *amp2 = svd2->amp;
7820Sstevel@tonic-gate 	struct vpage *vpage1 = svd1->vpage;
7830Sstevel@tonic-gate 	struct vpage *vpage2 = svd2->vpage, *nvpage = NULL;
7840Sstevel@tonic-gate 	size_t size, nvpsize;
7850Sstevel@tonic-gate 	pgcnt_t npages1, npages2;
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	ASSERT(seg1->s_as && seg2->s_as && seg1->s_as == seg2->s_as);
7880Sstevel@tonic-gate 	ASSERT(AS_WRITE_HELD(seg1->s_as, &seg1->s_as->a_lock));
7890Sstevel@tonic-gate 	ASSERT(seg1->s_ops == seg2->s_ops);
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 	/* both segments exist, try to merge them */
7920Sstevel@tonic-gate #define	incompat(x)	(svd1->x != svd2->x)
7930Sstevel@tonic-gate 	if (incompat(vp) || incompat(maxprot) ||
7940Sstevel@tonic-gate 	    (!svd1->pageadvice && !svd2->pageadvice && incompat(advice)) ||
7950Sstevel@tonic-gate 	    (!svd1->pageprot && !svd2->pageprot && incompat(prot)) ||
7960Sstevel@tonic-gate 	    incompat(type) || incompat(cred) || incompat(flags) ||
7970Sstevel@tonic-gate 	    seg1->s_szc != seg2->s_szc || incompat(policy_info.mem_policy) ||
7980Sstevel@tonic-gate 	    (svd2->softlockcnt > 0))
7990Sstevel@tonic-gate 		return (-1);
8000Sstevel@tonic-gate #undef incompat
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 	/*
8030Sstevel@tonic-gate 	 * vp == NULL implies zfod, offset doesn't matter
8040Sstevel@tonic-gate 	 */
8050Sstevel@tonic-gate 	if (svd1->vp != NULL &&
8060Sstevel@tonic-gate 	    svd1->offset + seg1->s_size != svd2->offset) {
8070Sstevel@tonic-gate 		return (-1);
8080Sstevel@tonic-gate 	}
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 	/*
8110Sstevel@tonic-gate 	 * Fail early if we're not supposed to concatenate
8122414Saguzovsk 	 * segments with non NULL amp.
8130Sstevel@tonic-gate 	 */
8142414Saguzovsk 	if (amp_cat == 0 && (amp1 != NULL || amp2 != NULL)) {
8150Sstevel@tonic-gate 		return (-1);
8160Sstevel@tonic-gate 	}
8170Sstevel@tonic-gate 
8182414Saguzovsk 	if (svd1->vp == NULL && svd1->type == MAP_SHARED) {
8192414Saguzovsk 		if (amp1 != amp2) {
8202414Saguzovsk 			return (-1);
8212414Saguzovsk 		}
8222414Saguzovsk 		if (amp1 != NULL && svd1->anon_index + btop(seg1->s_size) !=
8232414Saguzovsk 		    svd2->anon_index) {
8242414Saguzovsk 			return (-1);
8252414Saguzovsk 		}
8262414Saguzovsk 		ASSERT(amp1 == NULL || amp1->refcnt >= 2);
8272414Saguzovsk 	}
8282414Saguzovsk 
8290Sstevel@tonic-gate 	/*
8300Sstevel@tonic-gate 	 * If either seg has vpages, create a new merged vpage array.
8310Sstevel@tonic-gate 	 */
8320Sstevel@tonic-gate 	if (vpage1 != NULL || vpage2 != NULL) {
8330Sstevel@tonic-gate 		struct vpage *vp;
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 		npages1 = seg_pages(seg1);
8360Sstevel@tonic-gate 		npages2 = seg_pages(seg2);
8370Sstevel@tonic-gate 		nvpsize = vpgtob(npages1 + npages2);
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 		if ((nvpage = kmem_zalloc(nvpsize, KM_NOSLEEP)) == NULL) {
8400Sstevel@tonic-gate 			return (-2);
8410Sstevel@tonic-gate 		}
8420Sstevel@tonic-gate 		if (vpage1 != NULL) {
8430Sstevel@tonic-gate 			bcopy(vpage1, nvpage, vpgtob(npages1));
8440Sstevel@tonic-gate 		}
8450Sstevel@tonic-gate 		if (vpage2 != NULL) {
8460Sstevel@tonic-gate 			bcopy(vpage2, nvpage + npages1, vpgtob(npages2));
8470Sstevel@tonic-gate 		}
8480Sstevel@tonic-gate 		for (vp = nvpage; vp < nvpage + npages1; vp++) {
8490Sstevel@tonic-gate 			if (svd2->pageprot && !svd1->pageprot) {
8500Sstevel@tonic-gate 				VPP_SETPROT(vp, svd1->prot);
8510Sstevel@tonic-gate 			}
8520Sstevel@tonic-gate 			if (svd2->pageadvice && !svd1->pageadvice) {
8530Sstevel@tonic-gate 				VPP_SETADVICE(vp, svd1->advice);
8540Sstevel@tonic-gate 			}
8550Sstevel@tonic-gate 		}
8560Sstevel@tonic-gate 		for (vp = nvpage + npages1;
8570Sstevel@tonic-gate 		    vp < nvpage + npages1 + npages2; vp++) {
8580Sstevel@tonic-gate 			if (svd1->pageprot && !svd2->pageprot) {
8590Sstevel@tonic-gate 				VPP_SETPROT(vp, svd2->prot);
8600Sstevel@tonic-gate 			}
8610Sstevel@tonic-gate 			if (svd1->pageadvice && !svd2->pageadvice) {
8620Sstevel@tonic-gate 				VPP_SETADVICE(vp, svd2->advice);
8630Sstevel@tonic-gate 			}
8640Sstevel@tonic-gate 		}
8650Sstevel@tonic-gate 	}
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 	/*
8680Sstevel@tonic-gate 	 * If either segment has private pages, create a new merged anon
8692414Saguzovsk 	 * array. If mergeing shared anon segments just decrement anon map's
8702414Saguzovsk 	 * refcnt.
8710Sstevel@tonic-gate 	 */
8722414Saguzovsk 	if (amp1 != NULL && svd1->type == MAP_SHARED) {
8732414Saguzovsk 		ASSERT(amp1 == amp2 && svd1->vp == NULL);
8742414Saguzovsk 		ANON_LOCK_ENTER(&amp1->a_rwlock, RW_WRITER);
8752414Saguzovsk 		ASSERT(amp1->refcnt >= 2);
8762414Saguzovsk 		amp1->refcnt--;
8772414Saguzovsk 		ANON_LOCK_EXIT(&amp1->a_rwlock);
8782414Saguzovsk 		svd2->amp = NULL;
8792414Saguzovsk 	} else if (amp1 != NULL || amp2 != NULL) {
8800Sstevel@tonic-gate 		struct anon_hdr *nahp;
8810Sstevel@tonic-gate 		struct anon_map *namp = NULL;
8822414Saguzovsk 		size_t asize;
8832414Saguzovsk 
8842414Saguzovsk 		ASSERT(svd1->type == MAP_PRIVATE);
8852414Saguzovsk 
8862414Saguzovsk 		asize = seg1->s_size + seg2->s_size;
8870Sstevel@tonic-gate 		if ((nahp = anon_create(btop(asize), ANON_NOSLEEP)) == NULL) {
8880Sstevel@tonic-gate 			if (nvpage != NULL) {
8890Sstevel@tonic-gate 				kmem_free(nvpage, nvpsize);
8900Sstevel@tonic-gate 			}
8910Sstevel@tonic-gate 			return (-2);
8920Sstevel@tonic-gate 		}
8930Sstevel@tonic-gate 		if (amp1 != NULL) {
8940Sstevel@tonic-gate 			/*
8950Sstevel@tonic-gate 			 * XXX anon rwlock is not really needed because
8960Sstevel@tonic-gate 			 * this is a private segment and we are writers.
8970Sstevel@tonic-gate 			 */
8980Sstevel@tonic-gate 			ANON_LOCK_ENTER(&amp1->a_rwlock, RW_WRITER);
8990Sstevel@tonic-gate 			ASSERT(amp1->refcnt == 1);
9000Sstevel@tonic-gate 			if (anon_copy_ptr(amp1->ahp, svd1->anon_index,
9010Sstevel@tonic-gate 			    nahp, 0, btop(seg1->s_size), ANON_NOSLEEP)) {
9020Sstevel@tonic-gate 				anon_release(nahp, btop(asize));
9030Sstevel@tonic-gate 				ANON_LOCK_EXIT(&amp1->a_rwlock);
9040Sstevel@tonic-gate 				if (nvpage != NULL) {
9050Sstevel@tonic-gate 					kmem_free(nvpage, nvpsize);
9060Sstevel@tonic-gate 				}
9070Sstevel@tonic-gate 				return (-2);
9080Sstevel@tonic-gate 			}
9090Sstevel@tonic-gate 		}
9100Sstevel@tonic-gate 		if (amp2 != NULL) {
9110Sstevel@tonic-gate 			ANON_LOCK_ENTER(&amp2->a_rwlock, RW_WRITER);
9120Sstevel@tonic-gate 			ASSERT(amp2->refcnt == 1);
9130Sstevel@tonic-gate 			if (anon_copy_ptr(amp2->ahp, svd2->anon_index,
9140Sstevel@tonic-gate 			    nahp, btop(seg1->s_size), btop(seg2->s_size),
9150Sstevel@tonic-gate 			    ANON_NOSLEEP)) {
9160Sstevel@tonic-gate 				anon_release(nahp, btop(asize));
9170Sstevel@tonic-gate 				ANON_LOCK_EXIT(&amp2->a_rwlock);
9180Sstevel@tonic-gate 				if (amp1 != NULL) {
9190Sstevel@tonic-gate 					ANON_LOCK_EXIT(&amp1->a_rwlock);
9200Sstevel@tonic-gate 				}
9210Sstevel@tonic-gate 				if (nvpage != NULL) {
9220Sstevel@tonic-gate 					kmem_free(nvpage, nvpsize);
9230Sstevel@tonic-gate 				}
9240Sstevel@tonic-gate 				return (-2);
9250Sstevel@tonic-gate 			}
9260Sstevel@tonic-gate 		}
9270Sstevel@tonic-gate 		if (amp1 != NULL) {
9280Sstevel@tonic-gate 			namp = amp1;
9290Sstevel@tonic-gate 			anon_release(amp1->ahp, btop(amp1->size));
9300Sstevel@tonic-gate 		}
9310Sstevel@tonic-gate 		if (amp2 != NULL) {
9320Sstevel@tonic-gate 			if (namp == NULL) {
9330Sstevel@tonic-gate 				ASSERT(amp1 == NULL);
9340Sstevel@tonic-gate 				namp = amp2;
9350Sstevel@tonic-gate 				anon_release(amp2->ahp, btop(amp2->size));
9360Sstevel@tonic-gate 			} else {
9370Sstevel@tonic-gate 				amp2->refcnt--;
9380Sstevel@tonic-gate 				ANON_LOCK_EXIT(&amp2->a_rwlock);
9390Sstevel@tonic-gate 				anonmap_free(amp2);
9400Sstevel@tonic-gate 			}
9410Sstevel@tonic-gate 			svd2->amp = NULL; /* needed for seg_free */
9420Sstevel@tonic-gate 		}
9430Sstevel@tonic-gate 		namp->ahp = nahp;
9440Sstevel@tonic-gate 		namp->size = asize;
9450Sstevel@tonic-gate 		svd1->amp = namp;
9460Sstevel@tonic-gate 		svd1->anon_index = 0;
9470Sstevel@tonic-gate 		ANON_LOCK_EXIT(&namp->a_rwlock);
9480Sstevel@tonic-gate 	}
9490Sstevel@tonic-gate 	/*
9500Sstevel@tonic-gate 	 * Now free the old vpage structures.
9510Sstevel@tonic-gate 	 */
9520Sstevel@tonic-gate 	if (nvpage != NULL) {
9530Sstevel@tonic-gate 		if (vpage1 != NULL) {
9540Sstevel@tonic-gate 			kmem_free(vpage1, vpgtob(npages1));
9550Sstevel@tonic-gate 		}
9560Sstevel@tonic-gate 		if (vpage2 != NULL) {
9570Sstevel@tonic-gate 			svd2->vpage = NULL;
9580Sstevel@tonic-gate 			kmem_free(vpage2, vpgtob(npages2));
9590Sstevel@tonic-gate 		}
9600Sstevel@tonic-gate 		if (svd2->pageprot) {
9610Sstevel@tonic-gate 			svd1->pageprot = 1;
9620Sstevel@tonic-gate 		}
9630Sstevel@tonic-gate 		if (svd2->pageadvice) {
9640Sstevel@tonic-gate 			svd1->pageadvice = 1;
9650Sstevel@tonic-gate 		}
9660Sstevel@tonic-gate 		svd1->vpage = nvpage;
9670Sstevel@tonic-gate 	}
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate 	/* all looks ok, merge segments */
9700Sstevel@tonic-gate 	svd1->swresv += svd2->swresv;
9710Sstevel@tonic-gate 	svd2->swresv = 0;  /* so seg_free doesn't release swap space */
9720Sstevel@tonic-gate 	size = seg2->s_size;
9730Sstevel@tonic-gate 	seg_free(seg2);
9740Sstevel@tonic-gate 	seg1->s_size += size;
9750Sstevel@tonic-gate 	return (0);
9760Sstevel@tonic-gate }
9770Sstevel@tonic-gate 
9780Sstevel@tonic-gate /*
9790Sstevel@tonic-gate  * Extend the previous segment (seg1) to include the
9800Sstevel@tonic-gate  * new segment (seg2 + a), if possible.
9810Sstevel@tonic-gate  * Return 0 on success.
9820Sstevel@tonic-gate  */
9830Sstevel@tonic-gate static int
9840Sstevel@tonic-gate segvn_extend_prev(seg1, seg2, a, swresv)
9850Sstevel@tonic-gate 	struct seg *seg1, *seg2;
9860Sstevel@tonic-gate 	struct segvn_crargs *a;
9870Sstevel@tonic-gate 	size_t swresv;
9880Sstevel@tonic-gate {
9890Sstevel@tonic-gate 	struct segvn_data *svd1 = (struct segvn_data *)seg1->s_data;
9900Sstevel@tonic-gate 	size_t size;
9910Sstevel@tonic-gate 	struct anon_map *amp1;
9920Sstevel@tonic-gate 	struct vpage *new_vpage;
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 	/*
9950Sstevel@tonic-gate 	 * We don't need any segment level locks for "segvn" data
9960Sstevel@tonic-gate 	 * since the address space is "write" locked.
9970Sstevel@tonic-gate 	 */
9980Sstevel@tonic-gate 	ASSERT(seg1->s_as && AS_WRITE_HELD(seg1->s_as, &seg1->s_as->a_lock));
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate 	/* second segment is new, try to extend first */
10010Sstevel@tonic-gate 	/* XXX - should also check cred */
10020Sstevel@tonic-gate 	if (svd1->vp != a->vp || svd1->maxprot != a->maxprot ||
10030Sstevel@tonic-gate 	    (!svd1->pageprot && (svd1->prot != a->prot)) ||
10040Sstevel@tonic-gate 	    svd1->type != a->type || svd1->flags != a->flags ||
10050Sstevel@tonic-gate 	    seg1->s_szc != a->szc)
10060Sstevel@tonic-gate 		return (-1);
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 	/* vp == NULL implies zfod, offset doesn't matter */
10090Sstevel@tonic-gate 	if (svd1->vp != NULL &&
10100Sstevel@tonic-gate 	    svd1->offset + seg1->s_size != (a->offset & PAGEMASK))
10110Sstevel@tonic-gate 		return (-1);
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 	amp1 = svd1->amp;
10140Sstevel@tonic-gate 	if (amp1) {
10150Sstevel@tonic-gate 		pgcnt_t newpgs;
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate 		/*
10180Sstevel@tonic-gate 		 * Segment has private pages, can data structures
10190Sstevel@tonic-gate 		 * be expanded?
10200Sstevel@tonic-gate 		 *
10210Sstevel@tonic-gate 		 * Acquire the anon_map lock to prevent it from changing,
10220Sstevel@tonic-gate 		 * if it is shared.  This ensures that the anon_map
10230Sstevel@tonic-gate 		 * will not change while a thread which has a read/write
10240Sstevel@tonic-gate 		 * lock on an address space references it.
10250Sstevel@tonic-gate 		 * XXX - Don't need the anon_map lock at all if "refcnt"
10260Sstevel@tonic-gate 		 * is 1.
10270Sstevel@tonic-gate 		 *
10280Sstevel@tonic-gate 		 * Can't grow a MAP_SHARED segment with an anonmap because
10290Sstevel@tonic-gate 		 * there may be existing anon slots where we want to extend
10300Sstevel@tonic-gate 		 * the segment and we wouldn't know what to do with them
10310Sstevel@tonic-gate 		 * (e.g., for tmpfs right thing is to just leave them there,
10320Sstevel@tonic-gate 		 * for /dev/zero they should be cleared out).
10330Sstevel@tonic-gate 		 */
10340Sstevel@tonic-gate 		if (svd1->type == MAP_SHARED)
10350Sstevel@tonic-gate 			return (-1);
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 		ANON_LOCK_ENTER(&amp1->a_rwlock, RW_WRITER);
10380Sstevel@tonic-gate 		if (amp1->refcnt > 1) {
10390Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp1->a_rwlock);
10400Sstevel@tonic-gate 			return (-1);
10410Sstevel@tonic-gate 		}
10420Sstevel@tonic-gate 		newpgs = anon_grow(amp1->ahp, &svd1->anon_index,
10430Sstevel@tonic-gate 		    btop(seg1->s_size), btop(seg2->s_size), ANON_NOSLEEP);
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate 		if (newpgs == 0) {
10460Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp1->a_rwlock);
10470Sstevel@tonic-gate 			return (-1);
10480Sstevel@tonic-gate 		}
10490Sstevel@tonic-gate 		amp1->size = ptob(newpgs);
10500Sstevel@tonic-gate 		ANON_LOCK_EXIT(&amp1->a_rwlock);
10510Sstevel@tonic-gate 	}
10520Sstevel@tonic-gate 	if (svd1->vpage != NULL) {
10530Sstevel@tonic-gate 		new_vpage =
10540Sstevel@tonic-gate 		    kmem_zalloc(vpgtob(seg_pages(seg1) + seg_pages(seg2)),
10550Sstevel@tonic-gate 			KM_NOSLEEP);
10560Sstevel@tonic-gate 		if (new_vpage == NULL)
10570Sstevel@tonic-gate 			return (-1);
10580Sstevel@tonic-gate 		bcopy(svd1->vpage, new_vpage, vpgtob(seg_pages(seg1)));
10590Sstevel@tonic-gate 		kmem_free(svd1->vpage, vpgtob(seg_pages(seg1)));
10600Sstevel@tonic-gate 		svd1->vpage = new_vpage;
10610Sstevel@tonic-gate 		if (svd1->pageprot) {
10620Sstevel@tonic-gate 			struct vpage *vp, *evp;
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate 			vp = new_vpage + seg_pages(seg1);
10650Sstevel@tonic-gate 			evp = vp + seg_pages(seg2);
10660Sstevel@tonic-gate 			for (; vp < evp; vp++)
10670Sstevel@tonic-gate 				VPP_SETPROT(vp, a->prot);
10680Sstevel@tonic-gate 		}
10690Sstevel@tonic-gate 	}
10700Sstevel@tonic-gate 	size = seg2->s_size;
10710Sstevel@tonic-gate 	seg_free(seg2);
10720Sstevel@tonic-gate 	seg1->s_size += size;
10730Sstevel@tonic-gate 	svd1->swresv += swresv;
10740Sstevel@tonic-gate 	return (0);
10750Sstevel@tonic-gate }
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate /*
10780Sstevel@tonic-gate  * Extend the next segment (seg2) to include the
10790Sstevel@tonic-gate  * new segment (seg1 + a), if possible.
10800Sstevel@tonic-gate  * Return 0 on success.
10810Sstevel@tonic-gate  */
10820Sstevel@tonic-gate static int
10830Sstevel@tonic-gate segvn_extend_next(
10840Sstevel@tonic-gate 	struct seg *seg1,
10850Sstevel@tonic-gate 	struct seg *seg2,
10860Sstevel@tonic-gate 	struct segvn_crargs *a,
10870Sstevel@tonic-gate 	size_t swresv)
10880Sstevel@tonic-gate {
10890Sstevel@tonic-gate 	struct segvn_data *svd2 = (struct segvn_data *)seg2->s_data;
10900Sstevel@tonic-gate 	size_t size;
10910Sstevel@tonic-gate 	struct anon_map *amp2;
10920Sstevel@tonic-gate 	struct vpage *new_vpage;
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate 	/*
10950Sstevel@tonic-gate 	 * We don't need any segment level locks for "segvn" data
10960Sstevel@tonic-gate 	 * since the address space is "write" locked.
10970Sstevel@tonic-gate 	 */
10980Sstevel@tonic-gate 	ASSERT(seg2->s_as && AS_WRITE_HELD(seg2->s_as, &seg2->s_as->a_lock));
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate 	/* first segment is new, try to extend second */
11010Sstevel@tonic-gate 	/* XXX - should also check cred */
11020Sstevel@tonic-gate 	if (svd2->vp != a->vp || svd2->maxprot != a->maxprot ||
11030Sstevel@tonic-gate 	    (!svd2->pageprot && (svd2->prot != a->prot)) ||
11040Sstevel@tonic-gate 	    svd2->type != a->type || svd2->flags != a->flags ||
11050Sstevel@tonic-gate 	    seg2->s_szc != a->szc)
11060Sstevel@tonic-gate 		return (-1);
11070Sstevel@tonic-gate 	/* vp == NULL implies zfod, offset doesn't matter */
11080Sstevel@tonic-gate 	if (svd2->vp != NULL &&
11090Sstevel@tonic-gate 	    (a->offset & PAGEMASK) + seg1->s_size != svd2->offset)
11100Sstevel@tonic-gate 		return (-1);
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate 	amp2 = svd2->amp;
11130Sstevel@tonic-gate 	if (amp2) {
11140Sstevel@tonic-gate 		pgcnt_t newpgs;
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 		/*
11170Sstevel@tonic-gate 		 * Segment has private pages, can data structures
11180Sstevel@tonic-gate 		 * be expanded?
11190Sstevel@tonic-gate 		 *
11200Sstevel@tonic-gate 		 * Acquire the anon_map lock to prevent it from changing,
11210Sstevel@tonic-gate 		 * if it is shared.  This ensures that the anon_map
11220Sstevel@tonic-gate 		 * will not change while a thread which has a read/write
11230Sstevel@tonic-gate 		 * lock on an address space references it.
11240Sstevel@tonic-gate 		 *
11250Sstevel@tonic-gate 		 * XXX - Don't need the anon_map lock at all if "refcnt"
11260Sstevel@tonic-gate 		 * is 1.
11270Sstevel@tonic-gate 		 */
11280Sstevel@tonic-gate 		if (svd2->type == MAP_SHARED)
11290Sstevel@tonic-gate 			return (-1);
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate 		ANON_LOCK_ENTER(&amp2->a_rwlock, RW_WRITER);
11320Sstevel@tonic-gate 		if (amp2->refcnt > 1) {
11330Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp2->a_rwlock);
11340Sstevel@tonic-gate 			return (-1);
11350Sstevel@tonic-gate 		}
11360Sstevel@tonic-gate 		newpgs = anon_grow(amp2->ahp, &svd2->anon_index,
11370Sstevel@tonic-gate 		    btop(seg2->s_size), btop(seg1->s_size),
11380Sstevel@tonic-gate 		    ANON_NOSLEEP | ANON_GROWDOWN);
11390Sstevel@tonic-gate 
11400Sstevel@tonic-gate 		if (newpgs == 0) {
11410Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp2->a_rwlock);
11420Sstevel@tonic-gate 			return (-1);
11430Sstevel@tonic-gate 		}
11440Sstevel@tonic-gate 		amp2->size = ptob(newpgs);
11450Sstevel@tonic-gate 		ANON_LOCK_EXIT(&amp2->a_rwlock);
11460Sstevel@tonic-gate 	}
11470Sstevel@tonic-gate 	if (svd2->vpage != NULL) {
11480Sstevel@tonic-gate 		new_vpage =
11490Sstevel@tonic-gate 		    kmem_zalloc(vpgtob(seg_pages(seg1) + seg_pages(seg2)),
11500Sstevel@tonic-gate 			KM_NOSLEEP);
11510Sstevel@tonic-gate 		if (new_vpage == NULL) {
11520Sstevel@tonic-gate 			/* Not merging segments so adjust anon_index back */
11530Sstevel@tonic-gate 			if (amp2)
11540Sstevel@tonic-gate 				svd2->anon_index += seg_pages(seg1);
11550Sstevel@tonic-gate 			return (-1);
11560Sstevel@tonic-gate 		}
11570Sstevel@tonic-gate 		bcopy(svd2->vpage, new_vpage + seg_pages(seg1),
11580Sstevel@tonic-gate 		    vpgtob(seg_pages(seg2)));
11590Sstevel@tonic-gate 		kmem_free(svd2->vpage, vpgtob(seg_pages(seg2)));
11600Sstevel@tonic-gate 		svd2->vpage = new_vpage;
11610Sstevel@tonic-gate 		if (svd2->pageprot) {
11620Sstevel@tonic-gate 			struct vpage *vp, *evp;
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate 			vp = new_vpage;
11650Sstevel@tonic-gate 			evp = vp + seg_pages(seg1);
11660Sstevel@tonic-gate 			for (; vp < evp; vp++)
11670Sstevel@tonic-gate 				VPP_SETPROT(vp, a->prot);
11680Sstevel@tonic-gate 		}
11690Sstevel@tonic-gate 	}
11700Sstevel@tonic-gate 	size = seg1->s_size;
11710Sstevel@tonic-gate 	seg_free(seg1);
11720Sstevel@tonic-gate 	seg2->s_size += size;
11730Sstevel@tonic-gate 	seg2->s_base -= size;
11740Sstevel@tonic-gate 	svd2->offset -= size;
11750Sstevel@tonic-gate 	svd2->swresv += swresv;
11760Sstevel@tonic-gate 	return (0);
11770Sstevel@tonic-gate }
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate static int
11800Sstevel@tonic-gate segvn_dup(struct seg *seg, struct seg *newseg)
11810Sstevel@tonic-gate {
11820Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
11830Sstevel@tonic-gate 	struct segvn_data *newsvd;
11840Sstevel@tonic-gate 	pgcnt_t npages = seg_pages(seg);
11850Sstevel@tonic-gate 	int error = 0;
11860Sstevel@tonic-gate 	uint_t prot;
11870Sstevel@tonic-gate 	size_t len;
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
11900Sstevel@tonic-gate 
11910Sstevel@tonic-gate 	/*
11920Sstevel@tonic-gate 	 * If segment has anon reserved, reserve more for the new seg.
11930Sstevel@tonic-gate 	 * For a MAP_NORESERVE segment swresv will be a count of all the
11940Sstevel@tonic-gate 	 * allocated anon slots; thus we reserve for the child as many slots
11950Sstevel@tonic-gate 	 * as the parent has allocated. This semantic prevents the child or
11960Sstevel@tonic-gate 	 * parent from dieing during a copy-on-write fault caused by trying
11970Sstevel@tonic-gate 	 * to write a shared pre-existing anon page.
11980Sstevel@tonic-gate 	 */
11990Sstevel@tonic-gate 	if ((len = svd->swresv) != 0) {
12000Sstevel@tonic-gate 		if (anon_resv(svd->swresv) == 0)
12010Sstevel@tonic-gate 			return (ENOMEM);
12020Sstevel@tonic-gate 
12030Sstevel@tonic-gate 		TRACE_3(TR_FAC_VM, TR_ANON_PROC, "anon proc:%p %lu %u",
12040Sstevel@tonic-gate 			seg, len, 0);
12050Sstevel@tonic-gate 	}
12060Sstevel@tonic-gate 
12070Sstevel@tonic-gate 	newsvd = kmem_cache_alloc(segvn_cache, KM_SLEEP);
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate 	newseg->s_ops = &segvn_ops;
12100Sstevel@tonic-gate 	newseg->s_data = (void *)newsvd;
12110Sstevel@tonic-gate 	newseg->s_szc = seg->s_szc;
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate 	if ((newsvd->vp = svd->vp) != NULL) {
12140Sstevel@tonic-gate 		VN_HOLD(svd->vp);
12150Sstevel@tonic-gate 		if (svd->type == MAP_SHARED)
12160Sstevel@tonic-gate 			lgrp_shm_policy_init(NULL, svd->vp);
12170Sstevel@tonic-gate 	}
12180Sstevel@tonic-gate 	newsvd->offset = svd->offset;
12190Sstevel@tonic-gate 	newsvd->prot = svd->prot;
12200Sstevel@tonic-gate 	newsvd->maxprot = svd->maxprot;
12210Sstevel@tonic-gate 	newsvd->pageprot = svd->pageprot;
12220Sstevel@tonic-gate 	newsvd->type = svd->type;
12230Sstevel@tonic-gate 	newsvd->cred = svd->cred;
12240Sstevel@tonic-gate 	crhold(newsvd->cred);
12250Sstevel@tonic-gate 	newsvd->advice = svd->advice;
12260Sstevel@tonic-gate 	newsvd->pageadvice = svd->pageadvice;
12270Sstevel@tonic-gate 	newsvd->swresv = svd->swresv;
12280Sstevel@tonic-gate 	newsvd->flags = svd->flags;
12290Sstevel@tonic-gate 	newsvd->softlockcnt = 0;
12300Sstevel@tonic-gate 	newsvd->policy_info = svd->policy_info;
12310Sstevel@tonic-gate 	if ((newsvd->amp = svd->amp) == NULL) {
12320Sstevel@tonic-gate 		/*
12330Sstevel@tonic-gate 		 * Not attaching to a shared anon object.
12340Sstevel@tonic-gate 		 */
12350Sstevel@tonic-gate 		newsvd->anon_index = 0;
12360Sstevel@tonic-gate 	} else {
12370Sstevel@tonic-gate 		struct anon_map *amp;
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate 		amp = svd->amp;
12400Sstevel@tonic-gate 		if (svd->type == MAP_SHARED) {
12410Sstevel@tonic-gate 			ANON_LOCK_ENTER(&amp->a_rwlock, RW_WRITER);
12420Sstevel@tonic-gate 			amp->refcnt++;
12430Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
12440Sstevel@tonic-gate 			newsvd->anon_index = svd->anon_index;
12450Sstevel@tonic-gate 		} else {
12460Sstevel@tonic-gate 			int reclaim = 1;
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate 			/*
12490Sstevel@tonic-gate 			 * Allocate and initialize new anon_map structure.
12500Sstevel@tonic-gate 			 */
12510Sstevel@tonic-gate 			newsvd->amp = anonmap_alloc(newseg->s_size, 0);
12520Sstevel@tonic-gate 			newsvd->amp->a_szc = newseg->s_szc;
12530Sstevel@tonic-gate 			newsvd->anon_index = 0;
12540Sstevel@tonic-gate 
12550Sstevel@tonic-gate 			/*
12560Sstevel@tonic-gate 			 * We don't have to acquire the anon_map lock
12570Sstevel@tonic-gate 			 * for the new segment (since it belongs to an
12580Sstevel@tonic-gate 			 * address space that is still not associated
12590Sstevel@tonic-gate 			 * with any process), or the segment in the old
12600Sstevel@tonic-gate 			 * address space (since all threads in it
12610Sstevel@tonic-gate 			 * are stopped while duplicating the address space).
12620Sstevel@tonic-gate 			 */
12630Sstevel@tonic-gate 
12640Sstevel@tonic-gate 			/*
12650Sstevel@tonic-gate 			 * The goal of the following code is to make sure that
12660Sstevel@tonic-gate 			 * softlocked pages do not end up as copy on write
12670Sstevel@tonic-gate 			 * pages.  This would cause problems where one
12680Sstevel@tonic-gate 			 * thread writes to a page that is COW and a different
12690Sstevel@tonic-gate 			 * thread in the same process has softlocked it.  The
12700Sstevel@tonic-gate 			 * softlock lock would move away from this process
12710Sstevel@tonic-gate 			 * because the write would cause this process to get
12720Sstevel@tonic-gate 			 * a copy (without the softlock).
12730Sstevel@tonic-gate 			 *
12740Sstevel@tonic-gate 			 * The strategy here is to just break the
12750Sstevel@tonic-gate 			 * sharing on pages that could possibly be
12760Sstevel@tonic-gate 			 * softlocked.
12770Sstevel@tonic-gate 			 */
12780Sstevel@tonic-gate retry:
12790Sstevel@tonic-gate 			if (svd->softlockcnt) {
12800Sstevel@tonic-gate 				struct anon *ap, *newap;
12810Sstevel@tonic-gate 				size_t i;
12820Sstevel@tonic-gate 				uint_t vpprot;
12830Sstevel@tonic-gate 				page_t *anon_pl[1+1], *pp;
12840Sstevel@tonic-gate 				caddr_t addr;
12850Sstevel@tonic-gate 				ulong_t anon_idx = 0;
12860Sstevel@tonic-gate 
12870Sstevel@tonic-gate 				/*
12880Sstevel@tonic-gate 				 * The softlock count might be non zero
12890Sstevel@tonic-gate 				 * because some pages are still stuck in the
12900Sstevel@tonic-gate 				 * cache for lazy reclaim. Flush the cache
12910Sstevel@tonic-gate 				 * now. This should drop the count to zero.
12920Sstevel@tonic-gate 				 * [or there is really I/O going on to these
12930Sstevel@tonic-gate 				 * pages]. Note, we have the writers lock so
12940Sstevel@tonic-gate 				 * nothing gets inserted during the flush.
12950Sstevel@tonic-gate 				 */
12960Sstevel@tonic-gate 				if (reclaim == 1) {
12970Sstevel@tonic-gate 					segvn_purge(seg);
12980Sstevel@tonic-gate 					reclaim = 0;
12990Sstevel@tonic-gate 					goto retry;
13000Sstevel@tonic-gate 				}
13010Sstevel@tonic-gate 				i = btopr(seg->s_size);
13020Sstevel@tonic-gate 				addr = seg->s_base;
13030Sstevel@tonic-gate 				/*
13040Sstevel@tonic-gate 				 * XXX break cow sharing using PAGESIZE
13050Sstevel@tonic-gate 				 * pages. They will be relocated into larger
13060Sstevel@tonic-gate 				 * pages at fault time.
13070Sstevel@tonic-gate 				 */
13080Sstevel@tonic-gate 				while (i-- > 0) {
13090Sstevel@tonic-gate 					if (ap = anon_get_ptr(amp->ahp,
13100Sstevel@tonic-gate 					    anon_idx)) {
13110Sstevel@tonic-gate 						error = anon_getpage(&ap,
13120Sstevel@tonic-gate 						    &vpprot, anon_pl, PAGESIZE,
13130Sstevel@tonic-gate 						    seg, addr, S_READ,
13140Sstevel@tonic-gate 						    svd->cred);
13150Sstevel@tonic-gate 						if (error) {
13160Sstevel@tonic-gate 							newsvd->vpage = NULL;
13170Sstevel@tonic-gate 							goto out;
13180Sstevel@tonic-gate 						}
13190Sstevel@tonic-gate 						/*
13200Sstevel@tonic-gate 						 * prot need not be computed
13210Sstevel@tonic-gate 						 * below 'cause anon_private is
13220Sstevel@tonic-gate 						 * going to ignore it anyway
13230Sstevel@tonic-gate 						 * as child doesn't inherit
13240Sstevel@tonic-gate 						 * pagelock from parent.
13250Sstevel@tonic-gate 						 */
13260Sstevel@tonic-gate 						prot = svd->pageprot ?
13270Sstevel@tonic-gate 						    VPP_PROT(
13280Sstevel@tonic-gate 						    &svd->vpage[
13290Sstevel@tonic-gate 						    seg_page(seg, addr)])
13300Sstevel@tonic-gate 						    : svd->prot;
13310Sstevel@tonic-gate 						pp = anon_private(&newap,
13320Sstevel@tonic-gate 						    newseg, addr, prot,
13330Sstevel@tonic-gate 						    anon_pl[0],	0,
13340Sstevel@tonic-gate 						    newsvd->cred);
13350Sstevel@tonic-gate 						if (pp == NULL) {
13360Sstevel@tonic-gate 							/* no mem abort */
13370Sstevel@tonic-gate 							newsvd->vpage = NULL;
13380Sstevel@tonic-gate 							error = ENOMEM;
13390Sstevel@tonic-gate 							goto out;
13400Sstevel@tonic-gate 						}
13410Sstevel@tonic-gate 						(void) anon_set_ptr(
13420Sstevel@tonic-gate 						    newsvd->amp->ahp, anon_idx,
13430Sstevel@tonic-gate 						    newap, ANON_SLEEP);
13440Sstevel@tonic-gate 						page_unlock(pp);
13450Sstevel@tonic-gate 					}
13460Sstevel@tonic-gate 					addr += PAGESIZE;
13470Sstevel@tonic-gate 					anon_idx++;
13480Sstevel@tonic-gate 				}
13490Sstevel@tonic-gate 			} else {	/* common case */
13500Sstevel@tonic-gate 				if (seg->s_szc != 0) {
13510Sstevel@tonic-gate 					/*
13520Sstevel@tonic-gate 					 * If at least one of anon slots of a
13530Sstevel@tonic-gate 					 * large page exists then make sure
13540Sstevel@tonic-gate 					 * all anon slots of a large page
13550Sstevel@tonic-gate 					 * exist to avoid partial cow sharing
13560Sstevel@tonic-gate 					 * of a large page in the future.
13570Sstevel@tonic-gate 					 */
13580Sstevel@tonic-gate 					anon_dup_fill_holes(amp->ahp,
13590Sstevel@tonic-gate 					    svd->anon_index, newsvd->amp->ahp,
13600Sstevel@tonic-gate 					    0, seg->s_size, seg->s_szc,
13610Sstevel@tonic-gate 					    svd->vp != NULL);
13620Sstevel@tonic-gate 				} else {
13630Sstevel@tonic-gate 					anon_dup(amp->ahp, svd->anon_index,
13640Sstevel@tonic-gate 					    newsvd->amp->ahp, 0, seg->s_size);
13650Sstevel@tonic-gate 				}
13660Sstevel@tonic-gate 
13670Sstevel@tonic-gate 				hat_clrattr(seg->s_as->a_hat, seg->s_base,
13680Sstevel@tonic-gate 				    seg->s_size, PROT_WRITE);
13690Sstevel@tonic-gate 			}
13700Sstevel@tonic-gate 		}
13710Sstevel@tonic-gate 	}
13720Sstevel@tonic-gate 	/*
13730Sstevel@tonic-gate 	 * If necessary, create a vpage structure for the new segment.
13740Sstevel@tonic-gate 	 * Do not copy any page lock indications.
13750Sstevel@tonic-gate 	 */
13760Sstevel@tonic-gate 	if (svd->vpage != NULL) {
13770Sstevel@tonic-gate 		uint_t i;
13780Sstevel@tonic-gate 		struct vpage *ovp = svd->vpage;
13790Sstevel@tonic-gate 		struct vpage *nvp;
13800Sstevel@tonic-gate 
13810Sstevel@tonic-gate 		nvp = newsvd->vpage =
13820Sstevel@tonic-gate 		    kmem_alloc(vpgtob(npages), KM_SLEEP);
13830Sstevel@tonic-gate 		for (i = 0; i < npages; i++) {
13840Sstevel@tonic-gate 			*nvp = *ovp++;
13850Sstevel@tonic-gate 			VPP_CLRPPLOCK(nvp++);
13860Sstevel@tonic-gate 		}
13870Sstevel@tonic-gate 	} else
13880Sstevel@tonic-gate 		newsvd->vpage = NULL;
13890Sstevel@tonic-gate 
13900Sstevel@tonic-gate 	/* Inform the vnode of the new mapping */
13910Sstevel@tonic-gate 	if (newsvd->vp != NULL) {
13920Sstevel@tonic-gate 		error = VOP_ADDMAP(newsvd->vp, (offset_t)newsvd->offset,
13930Sstevel@tonic-gate 		    newseg->s_as, newseg->s_base, newseg->s_size, newsvd->prot,
13940Sstevel@tonic-gate 		    newsvd->maxprot, newsvd->type, newsvd->cred);
13950Sstevel@tonic-gate 	}
13960Sstevel@tonic-gate out:
13970Sstevel@tonic-gate 	return (error);
13980Sstevel@tonic-gate }
13990Sstevel@tonic-gate 
14000Sstevel@tonic-gate 
14010Sstevel@tonic-gate /*
14020Sstevel@tonic-gate  * callback function used by segvn_unmap to invoke free_vp_pages() for only
14030Sstevel@tonic-gate  * those pages actually processed by the HAT
14040Sstevel@tonic-gate  */
14050Sstevel@tonic-gate extern int free_pages;
14060Sstevel@tonic-gate 
14070Sstevel@tonic-gate static void
14080Sstevel@tonic-gate segvn_hat_unload_callback(hat_callback_t *cb)
14090Sstevel@tonic-gate {
14100Sstevel@tonic-gate 	struct seg		*seg = cb->hcb_data;
14110Sstevel@tonic-gate 	struct segvn_data	*svd = (struct segvn_data *)seg->s_data;
14120Sstevel@tonic-gate 	size_t			len;
14130Sstevel@tonic-gate 	u_offset_t		off;
14140Sstevel@tonic-gate 
14150Sstevel@tonic-gate 	ASSERT(svd->vp != NULL);
14160Sstevel@tonic-gate 	ASSERT(cb->hcb_end_addr > cb->hcb_start_addr);
14170Sstevel@tonic-gate 	ASSERT(cb->hcb_start_addr >= seg->s_base);
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate 	len = cb->hcb_end_addr - cb->hcb_start_addr;
14200Sstevel@tonic-gate 	off = cb->hcb_start_addr - seg->s_base;
14210Sstevel@tonic-gate 	free_vp_pages(svd->vp, svd->offset + off, len);
14220Sstevel@tonic-gate }
14230Sstevel@tonic-gate 
14240Sstevel@tonic-gate 
14250Sstevel@tonic-gate static int
14260Sstevel@tonic-gate segvn_unmap(struct seg *seg, caddr_t addr, size_t len)
14270Sstevel@tonic-gate {
14280Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
14290Sstevel@tonic-gate 	struct segvn_data *nsvd;
14300Sstevel@tonic-gate 	struct seg *nseg;
14310Sstevel@tonic-gate 	struct anon_map *amp;
14320Sstevel@tonic-gate 	pgcnt_t	opages;		/* old segment size in pages */
14330Sstevel@tonic-gate 	pgcnt_t	npages;		/* new segment size in pages */
14340Sstevel@tonic-gate 	pgcnt_t	dpages;		/* pages being deleted (unmapped) */
14350Sstevel@tonic-gate 	hat_callback_t callback;	/* used for free_vp_pages() */
14360Sstevel@tonic-gate 	hat_callback_t *cbp = NULL;
14370Sstevel@tonic-gate 	caddr_t nbase;
14380Sstevel@tonic-gate 	size_t nsize;
14390Sstevel@tonic-gate 	size_t oswresv;
14400Sstevel@tonic-gate 	int reclaim = 1;
14410Sstevel@tonic-gate 
14420Sstevel@tonic-gate 	/*
14430Sstevel@tonic-gate 	 * We don't need any segment level locks for "segvn" data
14440Sstevel@tonic-gate 	 * since the address space is "write" locked.
14450Sstevel@tonic-gate 	 */
14460Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
14470Sstevel@tonic-gate 
14480Sstevel@tonic-gate 	/*
14490Sstevel@tonic-gate 	 * Fail the unmap if pages are SOFTLOCKed through this mapping.
14500Sstevel@tonic-gate 	 * softlockcnt is protected from change by the as write lock.
14510Sstevel@tonic-gate 	 */
14520Sstevel@tonic-gate retry:
14530Sstevel@tonic-gate 	if (svd->softlockcnt > 0) {
14540Sstevel@tonic-gate 		/*
14550Sstevel@tonic-gate 		 * since we do have the writers lock nobody can fill
14560Sstevel@tonic-gate 		 * the cache during the purge. The flush either succeeds
14570Sstevel@tonic-gate 		 * or we still have pending I/Os.
14580Sstevel@tonic-gate 		 */
14590Sstevel@tonic-gate 		if (reclaim == 1) {
14600Sstevel@tonic-gate 			segvn_purge(seg);
14610Sstevel@tonic-gate 			reclaim = 0;
14620Sstevel@tonic-gate 			goto retry;
14630Sstevel@tonic-gate 		}
14640Sstevel@tonic-gate 		return (EAGAIN);
14650Sstevel@tonic-gate 	}
14660Sstevel@tonic-gate 
14670Sstevel@tonic-gate 	/*
14680Sstevel@tonic-gate 	 * Check for bad sizes
14690Sstevel@tonic-gate 	 */
14700Sstevel@tonic-gate 	if (addr < seg->s_base || addr + len > seg->s_base + seg->s_size ||
14710Sstevel@tonic-gate 	    (len & PAGEOFFSET) || ((uintptr_t)addr & PAGEOFFSET)) {
14720Sstevel@tonic-gate 		panic("segvn_unmap");
14730Sstevel@tonic-gate 		/*NOTREACHED*/
14740Sstevel@tonic-gate 	}
14750Sstevel@tonic-gate 
14760Sstevel@tonic-gate 	if (seg->s_szc != 0) {
14770Sstevel@tonic-gate 		size_t pgsz = page_get_pagesize(seg->s_szc);
14780Sstevel@tonic-gate 		int err;
14790Sstevel@tonic-gate 		if (!IS_P2ALIGNED(addr, pgsz) || !IS_P2ALIGNED(len, pgsz)) {
14800Sstevel@tonic-gate 			ASSERT(seg->s_base != addr || seg->s_size != len);
14810Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.demoterange[0]);
14822414Saguzovsk 			err = segvn_demote_range(seg, addr, len, SDR_END, 0);
14830Sstevel@tonic-gate 			if (err == 0) {
14840Sstevel@tonic-gate 				return (IE_RETRY);
14850Sstevel@tonic-gate 			}
14860Sstevel@tonic-gate 			return (err);
14870Sstevel@tonic-gate 		}
14880Sstevel@tonic-gate 	}
14890Sstevel@tonic-gate 
14900Sstevel@tonic-gate 	/* Inform the vnode of the unmapping. */
14910Sstevel@tonic-gate 	if (svd->vp) {
14920Sstevel@tonic-gate 		int error;
14930Sstevel@tonic-gate 
14940Sstevel@tonic-gate 		error = VOP_DELMAP(svd->vp,
14950Sstevel@tonic-gate 			(offset_t)svd->offset + (uintptr_t)(addr - seg->s_base),
14960Sstevel@tonic-gate 			seg->s_as, addr, len, svd->prot, svd->maxprot,
14970Sstevel@tonic-gate 			svd->type, svd->cred);
14980Sstevel@tonic-gate 
14990Sstevel@tonic-gate 		if (error == EAGAIN)
15000Sstevel@tonic-gate 			return (error);
15010Sstevel@tonic-gate 	}
15020Sstevel@tonic-gate 	/*
15030Sstevel@tonic-gate 	 * Remove any page locks set through this mapping.
15040Sstevel@tonic-gate 	 */
15050Sstevel@tonic-gate 	(void) segvn_lockop(seg, addr, len, 0, MC_UNLOCK, NULL, 0);
15060Sstevel@tonic-gate 
15070Sstevel@tonic-gate 	/*
15080Sstevel@tonic-gate 	 * Unload any hardware translations in the range to be taken out.
15090Sstevel@tonic-gate 	 * Use a callback to invoke free_vp_pages() effectively.
15100Sstevel@tonic-gate 	 */
15110Sstevel@tonic-gate 	if (svd->vp != NULL && free_pages != 0) {
15120Sstevel@tonic-gate 		callback.hcb_data = seg;
15130Sstevel@tonic-gate 		callback.hcb_function = segvn_hat_unload_callback;
15140Sstevel@tonic-gate 		cbp = &callback;
15150Sstevel@tonic-gate 	}
15160Sstevel@tonic-gate 	hat_unload_callback(seg->s_as->a_hat, addr, len, HAT_UNLOAD_UNMAP, cbp);
15170Sstevel@tonic-gate 
15180Sstevel@tonic-gate 	/*
15190Sstevel@tonic-gate 	 * Check for entire segment
15200Sstevel@tonic-gate 	 */
15210Sstevel@tonic-gate 	if (addr == seg->s_base && len == seg->s_size) {
15220Sstevel@tonic-gate 		seg_free(seg);
15230Sstevel@tonic-gate 		return (0);
15240Sstevel@tonic-gate 	}
15250Sstevel@tonic-gate 
15260Sstevel@tonic-gate 	opages = seg_pages(seg);
15270Sstevel@tonic-gate 	dpages = btop(len);
15280Sstevel@tonic-gate 	npages = opages - dpages;
15290Sstevel@tonic-gate 	amp = svd->amp;
15302414Saguzovsk 	ASSERT(amp == NULL || amp->a_szc >= seg->s_szc);
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate 	/*
15330Sstevel@tonic-gate 	 * Check for beginning of segment
15340Sstevel@tonic-gate 	 */
15350Sstevel@tonic-gate 	if (addr == seg->s_base) {
15360Sstevel@tonic-gate 		if (svd->vpage != NULL) {
15370Sstevel@tonic-gate 			size_t nbytes;
15380Sstevel@tonic-gate 			struct vpage *ovpage;
15390Sstevel@tonic-gate 
15400Sstevel@tonic-gate 			ovpage = svd->vpage;	/* keep pointer to vpage */
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate 			nbytes = vpgtob(npages);
15430Sstevel@tonic-gate 			svd->vpage = kmem_alloc(nbytes, KM_SLEEP);
15440Sstevel@tonic-gate 			bcopy(&ovpage[dpages], svd->vpage, nbytes);
15450Sstevel@tonic-gate 
15460Sstevel@tonic-gate 			/* free up old vpage */
15470Sstevel@tonic-gate 			kmem_free(ovpage, vpgtob(opages));
15480Sstevel@tonic-gate 		}
15490Sstevel@tonic-gate 		if (amp != NULL) {
15500Sstevel@tonic-gate 			ANON_LOCK_ENTER(&amp->a_rwlock, RW_WRITER);
15510Sstevel@tonic-gate 			if (amp->refcnt == 1 || svd->type == MAP_PRIVATE) {
15520Sstevel@tonic-gate 				/*
15530Sstevel@tonic-gate 				 * Free up now unused parts of anon_map array.
15540Sstevel@tonic-gate 				 */
15552414Saguzovsk 				if (amp->a_szc == seg->s_szc) {
15562414Saguzovsk 					if (seg->s_szc != 0) {
15572414Saguzovsk 						anon_free_pages(amp->ahp,
15582414Saguzovsk 						    svd->anon_index, len,
15592414Saguzovsk 						    seg->s_szc);
15602414Saguzovsk 					} else {
15612414Saguzovsk 						anon_free(amp->ahp,
15622414Saguzovsk 						    svd->anon_index,
15632414Saguzovsk 						    len);
15642414Saguzovsk 					}
15650Sstevel@tonic-gate 				} else {
15662414Saguzovsk 					ASSERT(svd->type == MAP_SHARED);
15672414Saguzovsk 					ASSERT(amp->a_szc > seg->s_szc);
15682414Saguzovsk 					anon_shmap_free_pages(amp,
15692414Saguzovsk 					    svd->anon_index, len);
15700Sstevel@tonic-gate 				}
15710Sstevel@tonic-gate 
15720Sstevel@tonic-gate 				/*
15732414Saguzovsk 				 * Unreserve swap space for the
15742414Saguzovsk 				 * unmapped chunk of this segment in
15752414Saguzovsk 				 * case it's MAP_SHARED
15760Sstevel@tonic-gate 				 */
15770Sstevel@tonic-gate 				if (svd->type == MAP_SHARED) {
15780Sstevel@tonic-gate 					anon_unresv(len);
15790Sstevel@tonic-gate 					amp->swresv -= len;
15800Sstevel@tonic-gate 				}
15810Sstevel@tonic-gate 			}
15820Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
15830Sstevel@tonic-gate 			svd->anon_index += dpages;
15840Sstevel@tonic-gate 		}
15850Sstevel@tonic-gate 		if (svd->vp != NULL)
15860Sstevel@tonic-gate 			svd->offset += len;
15870Sstevel@tonic-gate 
15880Sstevel@tonic-gate 		if (svd->swresv) {
15890Sstevel@tonic-gate 			if (svd->flags & MAP_NORESERVE) {
15900Sstevel@tonic-gate 				ASSERT(amp);
15910Sstevel@tonic-gate 				oswresv = svd->swresv;
15920Sstevel@tonic-gate 
15930Sstevel@tonic-gate 				svd->swresv = ptob(anon_pages(amp->ahp,
15940Sstevel@tonic-gate 				    svd->anon_index, npages));
15950Sstevel@tonic-gate 				anon_unresv(oswresv - svd->swresv);
15960Sstevel@tonic-gate 			} else {
15970Sstevel@tonic-gate 				anon_unresv(len);
15980Sstevel@tonic-gate 				svd->swresv -= len;
15990Sstevel@tonic-gate 			}
16000Sstevel@tonic-gate 			TRACE_3(TR_FAC_VM, TR_ANON_PROC, "anon proc:%p %lu %u",
16010Sstevel@tonic-gate 				seg, len, 0);
16020Sstevel@tonic-gate 		}
16030Sstevel@tonic-gate 
16040Sstevel@tonic-gate 		seg->s_base += len;
16050Sstevel@tonic-gate 		seg->s_size -= len;
16060Sstevel@tonic-gate 		return (0);
16070Sstevel@tonic-gate 	}
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate 	/*
16100Sstevel@tonic-gate 	 * Check for end of segment
16110Sstevel@tonic-gate 	 */
16120Sstevel@tonic-gate 	if (addr + len == seg->s_base + seg->s_size) {
16130Sstevel@tonic-gate 		if (svd->vpage != NULL) {
16140Sstevel@tonic-gate 			size_t nbytes;
16150Sstevel@tonic-gate 			struct vpage *ovpage;
16160Sstevel@tonic-gate 
16170Sstevel@tonic-gate 			ovpage = svd->vpage;	/* keep pointer to vpage */
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate 			nbytes = vpgtob(npages);
16200Sstevel@tonic-gate 			svd->vpage = kmem_alloc(nbytes, KM_SLEEP);
16210Sstevel@tonic-gate 			bcopy(ovpage, svd->vpage, nbytes);
16220Sstevel@tonic-gate 
16230Sstevel@tonic-gate 			/* free up old vpage */
16240Sstevel@tonic-gate 			kmem_free(ovpage, vpgtob(opages));
16250Sstevel@tonic-gate 
16260Sstevel@tonic-gate 		}
16270Sstevel@tonic-gate 		if (amp != NULL) {
16280Sstevel@tonic-gate 			ANON_LOCK_ENTER(&amp->a_rwlock, RW_WRITER);
16290Sstevel@tonic-gate 			if (amp->refcnt == 1 || svd->type == MAP_PRIVATE) {
16300Sstevel@tonic-gate 				/*
16312414Saguzovsk 				 * Free up now unused parts of anon_map array.
16320Sstevel@tonic-gate 				 */
16332414Saguzovsk 				ulong_t an_idx = svd->anon_index + npages;
16342414Saguzovsk 				if (amp->a_szc == seg->s_szc) {
16352414Saguzovsk 					if (seg->s_szc != 0) {
16362414Saguzovsk 						anon_free_pages(amp->ahp,
16372414Saguzovsk 						    an_idx, len,
16382414Saguzovsk 						    seg->s_szc);
16392414Saguzovsk 					} else {
16402414Saguzovsk 						anon_free(amp->ahp, an_idx,
16412414Saguzovsk 						    len);
16422414Saguzovsk 					}
16430Sstevel@tonic-gate 				} else {
16442414Saguzovsk 					ASSERT(svd->type == MAP_SHARED);
16452414Saguzovsk 					ASSERT(amp->a_szc > seg->s_szc);
16462414Saguzovsk 					anon_shmap_free_pages(amp,
16472414Saguzovsk 					    an_idx, len);
16480Sstevel@tonic-gate 				}
16492414Saguzovsk 
16500Sstevel@tonic-gate 				/*
16512414Saguzovsk 				 * Unreserve swap space for the
16522414Saguzovsk 				 * unmapped chunk of this segment in
16532414Saguzovsk 				 * case it's MAP_SHARED
16540Sstevel@tonic-gate 				 */
16550Sstevel@tonic-gate 				if (svd->type == MAP_SHARED) {
16560Sstevel@tonic-gate 					anon_unresv(len);
16570Sstevel@tonic-gate 					amp->swresv -= len;
16580Sstevel@tonic-gate 				}
16590Sstevel@tonic-gate 			}
16600Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
16610Sstevel@tonic-gate 		}
16620Sstevel@tonic-gate 
16630Sstevel@tonic-gate 		if (svd->swresv) {
16640Sstevel@tonic-gate 			if (svd->flags & MAP_NORESERVE) {
16650Sstevel@tonic-gate 				ASSERT(amp);
16660Sstevel@tonic-gate 				oswresv = svd->swresv;
16670Sstevel@tonic-gate 				svd->swresv = ptob(anon_pages(amp->ahp,
16680Sstevel@tonic-gate 					svd->anon_index, npages));
16690Sstevel@tonic-gate 				anon_unresv(oswresv - svd->swresv);
16700Sstevel@tonic-gate 			} else {
16710Sstevel@tonic-gate 				anon_unresv(len);
16720Sstevel@tonic-gate 				svd->swresv -= len;
16730Sstevel@tonic-gate 			}
16740Sstevel@tonic-gate 			TRACE_3(TR_FAC_VM, TR_ANON_PROC,
16750Sstevel@tonic-gate 				"anon proc:%p %lu %u", seg, len, 0);
16760Sstevel@tonic-gate 		}
16770Sstevel@tonic-gate 
16780Sstevel@tonic-gate 		seg->s_size -= len;
16790Sstevel@tonic-gate 		return (0);
16800Sstevel@tonic-gate 	}
16810Sstevel@tonic-gate 
16820Sstevel@tonic-gate 	/*
16830Sstevel@tonic-gate 	 * The section to go is in the middle of the segment,
16840Sstevel@tonic-gate 	 * have to make it into two segments.  nseg is made for
16850Sstevel@tonic-gate 	 * the high end while seg is cut down at the low end.
16860Sstevel@tonic-gate 	 */
16870Sstevel@tonic-gate 	nbase = addr + len;				/* new seg base */
16880Sstevel@tonic-gate 	nsize = (seg->s_base + seg->s_size) - nbase;	/* new seg size */
16890Sstevel@tonic-gate 	seg->s_size = addr - seg->s_base;		/* shrink old seg */
16900Sstevel@tonic-gate 	nseg = seg_alloc(seg->s_as, nbase, nsize);
16910Sstevel@tonic-gate 	if (nseg == NULL) {
16920Sstevel@tonic-gate 		panic("segvn_unmap seg_alloc");
16930Sstevel@tonic-gate 		/*NOTREACHED*/
16940Sstevel@tonic-gate 	}
16950Sstevel@tonic-gate 	nseg->s_ops = seg->s_ops;
16960Sstevel@tonic-gate 	nsvd = kmem_cache_alloc(segvn_cache, KM_SLEEP);
16970Sstevel@tonic-gate 	nseg->s_data = (void *)nsvd;
16980Sstevel@tonic-gate 	nseg->s_szc = seg->s_szc;
16990Sstevel@tonic-gate 	*nsvd = *svd;
17000Sstevel@tonic-gate 	nsvd->offset = svd->offset + (uintptr_t)(nseg->s_base - seg->s_base);
17010Sstevel@tonic-gate 	nsvd->swresv = 0;
17020Sstevel@tonic-gate 	nsvd->softlockcnt = 0;
17030Sstevel@tonic-gate 
17040Sstevel@tonic-gate 	if (svd->vp != NULL) {
17050Sstevel@tonic-gate 		VN_HOLD(nsvd->vp);
17060Sstevel@tonic-gate 		if (nsvd->type == MAP_SHARED)
17070Sstevel@tonic-gate 			lgrp_shm_policy_init(NULL, nsvd->vp);
17080Sstevel@tonic-gate 	}
17090Sstevel@tonic-gate 	crhold(svd->cred);
17100Sstevel@tonic-gate 
17110Sstevel@tonic-gate 	if (svd->vpage == NULL) {
17120Sstevel@tonic-gate 		nsvd->vpage = NULL;
17130Sstevel@tonic-gate 	} else {
17140Sstevel@tonic-gate 		/* need to split vpage into two arrays */
17150Sstevel@tonic-gate 		size_t nbytes;
17160Sstevel@tonic-gate 		struct vpage *ovpage;
17170Sstevel@tonic-gate 
17180Sstevel@tonic-gate 		ovpage = svd->vpage;		/* keep pointer to vpage */
17190Sstevel@tonic-gate 
17200Sstevel@tonic-gate 		npages = seg_pages(seg);	/* seg has shrunk */
17210Sstevel@tonic-gate 		nbytes = vpgtob(npages);
17220Sstevel@tonic-gate 		svd->vpage = kmem_alloc(nbytes, KM_SLEEP);
17230Sstevel@tonic-gate 
17240Sstevel@tonic-gate 		bcopy(ovpage, svd->vpage, nbytes);
17250Sstevel@tonic-gate 
17260Sstevel@tonic-gate 		npages = seg_pages(nseg);
17270Sstevel@tonic-gate 		nbytes = vpgtob(npages);
17280Sstevel@tonic-gate 		nsvd->vpage = kmem_alloc(nbytes, KM_SLEEP);
17290Sstevel@tonic-gate 
17300Sstevel@tonic-gate 		bcopy(&ovpage[opages - npages], nsvd->vpage, nbytes);
17310Sstevel@tonic-gate 
17320Sstevel@tonic-gate 		/* free up old vpage */
17330Sstevel@tonic-gate 		kmem_free(ovpage, vpgtob(opages));
17340Sstevel@tonic-gate 	}
17350Sstevel@tonic-gate 
17360Sstevel@tonic-gate 	if (amp == NULL) {
17370Sstevel@tonic-gate 		nsvd->amp = NULL;
17380Sstevel@tonic-gate 		nsvd->anon_index = 0;
17390Sstevel@tonic-gate 	} else {
17400Sstevel@tonic-gate 		/*
17410Sstevel@tonic-gate 		 * Need to create a new anon map for the new segment.
17420Sstevel@tonic-gate 		 * We'll also allocate a new smaller array for the old
17430Sstevel@tonic-gate 		 * smaller segment to save space.
17440Sstevel@tonic-gate 		 */
17450Sstevel@tonic-gate 		opages = btop((uintptr_t)(addr - seg->s_base));
17460Sstevel@tonic-gate 		ANON_LOCK_ENTER(&amp->a_rwlock, RW_WRITER);
17470Sstevel@tonic-gate 		if (amp->refcnt == 1 || svd->type == MAP_PRIVATE) {
17480Sstevel@tonic-gate 			/*
17492414Saguzovsk 			 * Free up now unused parts of anon_map array.
17500Sstevel@tonic-gate 			 */
17512414Saguzovsk 			ulong_t an_idx = svd->anon_index + opages;
17522414Saguzovsk 			if (amp->a_szc == seg->s_szc) {
17532414Saguzovsk 				if (seg->s_szc != 0) {
17542414Saguzovsk 					anon_free_pages(amp->ahp, an_idx, len,
17552414Saguzovsk 					    seg->s_szc);
17562414Saguzovsk 				} else {
17572414Saguzovsk 					anon_free(amp->ahp, an_idx,
17582414Saguzovsk 					    len);
17592414Saguzovsk 				}
17600Sstevel@tonic-gate 			} else {
17612414Saguzovsk 				ASSERT(svd->type == MAP_SHARED);
17622414Saguzovsk 				ASSERT(amp->a_szc > seg->s_szc);
17632414Saguzovsk 				anon_shmap_free_pages(amp, an_idx, len);
17640Sstevel@tonic-gate 			}
17650Sstevel@tonic-gate 
17660Sstevel@tonic-gate 			/*
17672414Saguzovsk 			 * Unreserve swap space for the
17682414Saguzovsk 			 * unmapped chunk of this segment in
17692414Saguzovsk 			 * case it's MAP_SHARED
17700Sstevel@tonic-gate 			 */
17710Sstevel@tonic-gate 			if (svd->type == MAP_SHARED) {
17720Sstevel@tonic-gate 				anon_unresv(len);
17730Sstevel@tonic-gate 				amp->swresv -= len;
17740Sstevel@tonic-gate 			}
17750Sstevel@tonic-gate 		}
17760Sstevel@tonic-gate 		nsvd->anon_index = svd->anon_index +
17770Sstevel@tonic-gate 		    btop((uintptr_t)(nseg->s_base - seg->s_base));
17780Sstevel@tonic-gate 		if (svd->type == MAP_SHARED) {
17790Sstevel@tonic-gate 			amp->refcnt++;
17800Sstevel@tonic-gate 			nsvd->amp = amp;
17810Sstevel@tonic-gate 		} else {
17820Sstevel@tonic-gate 			struct anon_map *namp;
17830Sstevel@tonic-gate 			struct anon_hdr *nahp;
17840Sstevel@tonic-gate 
17850Sstevel@tonic-gate 			ASSERT(svd->type == MAP_PRIVATE);
17860Sstevel@tonic-gate 			nahp = anon_create(btop(seg->s_size), ANON_SLEEP);
17870Sstevel@tonic-gate 			namp = anonmap_alloc(nseg->s_size, 0);
17880Sstevel@tonic-gate 			namp->a_szc = seg->s_szc;
17890Sstevel@tonic-gate 			(void) anon_copy_ptr(amp->ahp, svd->anon_index, nahp,
17900Sstevel@tonic-gate 			    0, btop(seg->s_size), ANON_SLEEP);
17910Sstevel@tonic-gate 			(void) anon_copy_ptr(amp->ahp, nsvd->anon_index,
17920Sstevel@tonic-gate 			    namp->ahp, 0, btop(nseg->s_size), ANON_SLEEP);
17930Sstevel@tonic-gate 			anon_release(amp->ahp, btop(amp->size));
17940Sstevel@tonic-gate 			svd->anon_index = 0;
17950Sstevel@tonic-gate 			nsvd->anon_index = 0;
17960Sstevel@tonic-gate 			amp->ahp = nahp;
17970Sstevel@tonic-gate 			amp->size = seg->s_size;
17980Sstevel@tonic-gate 			nsvd->amp = namp;
17990Sstevel@tonic-gate 		}
18000Sstevel@tonic-gate 		ANON_LOCK_EXIT(&amp->a_rwlock);
18010Sstevel@tonic-gate 	}
18020Sstevel@tonic-gate 	if (svd->swresv) {
18030Sstevel@tonic-gate 		if (svd->flags & MAP_NORESERVE) {
18040Sstevel@tonic-gate 			ASSERT(amp);
18050Sstevel@tonic-gate 			oswresv = svd->swresv;
18060Sstevel@tonic-gate 			svd->swresv = ptob(anon_pages(amp->ahp,
18070Sstevel@tonic-gate 				svd->anon_index, btop(seg->s_size)));
18080Sstevel@tonic-gate 			nsvd->swresv = ptob(anon_pages(nsvd->amp->ahp,
18090Sstevel@tonic-gate 				nsvd->anon_index, btop(nseg->s_size)));
18100Sstevel@tonic-gate 			ASSERT(oswresv >= (svd->swresv + nsvd->swresv));
18110Sstevel@tonic-gate 			anon_unresv(oswresv - (svd->swresv + nsvd->swresv));
18120Sstevel@tonic-gate 		} else {
18130Sstevel@tonic-gate 			if (seg->s_size + nseg->s_size + len != svd->swresv) {
18140Sstevel@tonic-gate 				panic("segvn_unmap: "
18150Sstevel@tonic-gate 				    "cannot split swap reservation");
18160Sstevel@tonic-gate 				/*NOTREACHED*/
18170Sstevel@tonic-gate 			}
18180Sstevel@tonic-gate 			anon_unresv(len);
18190Sstevel@tonic-gate 			svd->swresv = seg->s_size;
18200Sstevel@tonic-gate 			nsvd->swresv = nseg->s_size;
18210Sstevel@tonic-gate 		}
18220Sstevel@tonic-gate 		TRACE_3(TR_FAC_VM, TR_ANON_PROC, "anon proc:%p %lu %u",
18230Sstevel@tonic-gate 			seg, len, 0);
18240Sstevel@tonic-gate 	}
18250Sstevel@tonic-gate 
18260Sstevel@tonic-gate 	return (0);			/* I'm glad that's all over with! */
18270Sstevel@tonic-gate }
18280Sstevel@tonic-gate 
18290Sstevel@tonic-gate static void
18300Sstevel@tonic-gate segvn_free(struct seg *seg)
18310Sstevel@tonic-gate {
18320Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
18330Sstevel@tonic-gate 	pgcnt_t npages = seg_pages(seg);
18340Sstevel@tonic-gate 	struct anon_map *amp;
18350Sstevel@tonic-gate 	size_t len;
18360Sstevel@tonic-gate 
18370Sstevel@tonic-gate 	/*
18380Sstevel@tonic-gate 	 * We don't need any segment level locks for "segvn" data
18390Sstevel@tonic-gate 	 * since the address space is "write" locked.
18400Sstevel@tonic-gate 	 */
18410Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
18420Sstevel@tonic-gate 
18430Sstevel@tonic-gate 	/*
18440Sstevel@tonic-gate 	 * Be sure to unlock pages. XXX Why do things get free'ed instead
18450Sstevel@tonic-gate 	 * of unmapped? XXX
18460Sstevel@tonic-gate 	 */
18470Sstevel@tonic-gate 	(void) segvn_lockop(seg, seg->s_base, seg->s_size,
18480Sstevel@tonic-gate 	    0, MC_UNLOCK, NULL, 0);
18490Sstevel@tonic-gate 
18500Sstevel@tonic-gate 	/*
18510Sstevel@tonic-gate 	 * Deallocate the vpage and anon pointers if necessary and possible.
18520Sstevel@tonic-gate 	 */
18530Sstevel@tonic-gate 	if (svd->vpage != NULL) {
18540Sstevel@tonic-gate 		kmem_free(svd->vpage, vpgtob(npages));
18550Sstevel@tonic-gate 		svd->vpage = NULL;
18560Sstevel@tonic-gate 	}
18570Sstevel@tonic-gate 	if ((amp = svd->amp) != NULL) {
18580Sstevel@tonic-gate 		/*
18590Sstevel@tonic-gate 		 * If there are no more references to this anon_map
18600Sstevel@tonic-gate 		 * structure, then deallocate the structure after freeing
18610Sstevel@tonic-gate 		 * up all the anon slot pointers that we can.
18620Sstevel@tonic-gate 		 */
18630Sstevel@tonic-gate 		ANON_LOCK_ENTER(&amp->a_rwlock, RW_WRITER);
18642414Saguzovsk 		ASSERT(amp->a_szc >= seg->s_szc);
18650Sstevel@tonic-gate 		if (--amp->refcnt == 0) {
18660Sstevel@tonic-gate 			if (svd->type == MAP_PRIVATE) {
18670Sstevel@tonic-gate 				/*
18680Sstevel@tonic-gate 				 * Private - we only need to anon_free
18690Sstevel@tonic-gate 				 * the part that this segment refers to.
18700Sstevel@tonic-gate 				 */
18710Sstevel@tonic-gate 				if (seg->s_szc != 0) {
18720Sstevel@tonic-gate 					anon_free_pages(amp->ahp,
18730Sstevel@tonic-gate 					    svd->anon_index, seg->s_size,
18740Sstevel@tonic-gate 					    seg->s_szc);
18750Sstevel@tonic-gate 				} else {
18760Sstevel@tonic-gate 					anon_free(amp->ahp, svd->anon_index,
18770Sstevel@tonic-gate 					    seg->s_size);
18780Sstevel@tonic-gate 				}
18790Sstevel@tonic-gate 			} else {
18800Sstevel@tonic-gate 				/*
18810Sstevel@tonic-gate 				 * Shared - anon_free the entire
18820Sstevel@tonic-gate 				 * anon_map's worth of stuff and
18830Sstevel@tonic-gate 				 * release any swap reservation.
18840Sstevel@tonic-gate 				 */
18852414Saguzovsk 				if (amp->a_szc != 0) {
18862414Saguzovsk 					anon_shmap_free_pages(amp, 0,
18872414Saguzovsk 					    amp->size);
18882414Saguzovsk 				} else {
18892414Saguzovsk 					anon_free(amp->ahp, 0, amp->size);
18902414Saguzovsk 				}
18910Sstevel@tonic-gate 				if ((len = amp->swresv) != 0) {
18920Sstevel@tonic-gate 					anon_unresv(len);
18930Sstevel@tonic-gate 					TRACE_3(TR_FAC_VM, TR_ANON_PROC,
18940Sstevel@tonic-gate 						"anon proc:%p %lu %u",
18950Sstevel@tonic-gate 						seg, len, 0);
18960Sstevel@tonic-gate 				}
18970Sstevel@tonic-gate 			}
18980Sstevel@tonic-gate 			svd->amp = NULL;
18990Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
19000Sstevel@tonic-gate 			anonmap_free(amp);
19010Sstevel@tonic-gate 		} else if (svd->type == MAP_PRIVATE) {
19020Sstevel@tonic-gate 			/*
19030Sstevel@tonic-gate 			 * We had a private mapping which still has
19040Sstevel@tonic-gate 			 * a held anon_map so just free up all the
19050Sstevel@tonic-gate 			 * anon slot pointers that we were using.
19060Sstevel@tonic-gate 			 */
19070Sstevel@tonic-gate 			if (seg->s_szc != 0) {
19080Sstevel@tonic-gate 				anon_free_pages(amp->ahp, svd->anon_index,
19090Sstevel@tonic-gate 				    seg->s_size, seg->s_szc);
19100Sstevel@tonic-gate 			} else {
19110Sstevel@tonic-gate 				anon_free(amp->ahp, svd->anon_index,
19120Sstevel@tonic-gate 				    seg->s_size);
19130Sstevel@tonic-gate 			}
19140Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
19150Sstevel@tonic-gate 		} else {
19160Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
19170Sstevel@tonic-gate 		}
19180Sstevel@tonic-gate 	}
19190Sstevel@tonic-gate 
19200Sstevel@tonic-gate 	/*
19210Sstevel@tonic-gate 	 * Release swap reservation.
19220Sstevel@tonic-gate 	 */
19230Sstevel@tonic-gate 	if ((len = svd->swresv) != 0) {
19240Sstevel@tonic-gate 		anon_unresv(svd->swresv);
19250Sstevel@tonic-gate 		TRACE_3(TR_FAC_VM, TR_ANON_PROC, "anon proc:%p %lu %u",
19260Sstevel@tonic-gate 			seg, len, 0);
19270Sstevel@tonic-gate 		svd->swresv = 0;
19280Sstevel@tonic-gate 	}
19290Sstevel@tonic-gate 	/*
19300Sstevel@tonic-gate 	 * Release claim on vnode, credentials, and finally free the
19310Sstevel@tonic-gate 	 * private data.
19320Sstevel@tonic-gate 	 */
19330Sstevel@tonic-gate 	if (svd->vp != NULL) {
19340Sstevel@tonic-gate 		if (svd->type == MAP_SHARED)
19350Sstevel@tonic-gate 			lgrp_shm_policy_fini(NULL, svd->vp);
19360Sstevel@tonic-gate 		VN_RELE(svd->vp);
19370Sstevel@tonic-gate 		svd->vp = NULL;
19380Sstevel@tonic-gate 	}
19390Sstevel@tonic-gate 	crfree(svd->cred);
19400Sstevel@tonic-gate 	svd->cred = NULL;
19410Sstevel@tonic-gate 
19420Sstevel@tonic-gate 	seg->s_data = NULL;
19430Sstevel@tonic-gate 	kmem_cache_free(segvn_cache, svd);
19440Sstevel@tonic-gate }
19450Sstevel@tonic-gate 
19462414Saguzovsk ulong_t segvn_lpglck_limit = 0;
19472414Saguzovsk /*
19482414Saguzovsk  * Support routines used by segvn_pagelock() and softlock faults for anonymous
19492414Saguzovsk  * pages to implement availrmem accounting in a way that makes sure the
19502414Saguzovsk  * same memory is accounted just once for all softlock/pagelock purposes.
19512414Saguzovsk  * This prevents a bug when availrmem is quickly incorrectly exausted from
19522414Saguzovsk  * several pagelocks to different parts of the same large page since each
19532414Saguzovsk  * pagelock has to decrement availrmem by the size of the entire large
19542414Saguzovsk  * page. Note those pages are not COW shared until softunlock/pageunlock so
19552414Saguzovsk  * we don't need to use cow style accounting here.  We also need to make sure
19562414Saguzovsk  * the entire large page is accounted even if softlock range is less than the
19572414Saguzovsk  * entire large page because large anon pages can't be demoted when any of
19582414Saguzovsk  * constituent pages is locked. The caller calls this routine for every page_t
19592414Saguzovsk  * it locks. The very first page in the range may not be the root page of a
19602414Saguzovsk  * large page. For all other pages it's guranteed we are going to visit the
19612414Saguzovsk  * root of a particular large page before any other constituent page as we are
19622414Saguzovsk  * locking sequential pages belonging to the same anon map. So we do all the
19632414Saguzovsk  * locking when the root is encountered except for the very first page.  Since
19642414Saguzovsk  * softlocking is not supported (except S_READ_NOCOW special case) for vmpss
19652414Saguzovsk  * segments and since vnode pages can be demoted without locking all
19662414Saguzovsk  * constituent pages vnode pages don't come here.  Unlocking relies on the
19672414Saguzovsk  * fact that pagesize can't change whenever any of constituent large pages is
19682414Saguzovsk  * locked at least SE_SHARED. This allows unlocking code to find the right
19692414Saguzovsk  * root and decrement availrmem by the same amount it was incremented when the
19702414Saguzovsk  * page was locked.
19712414Saguzovsk  */
19722414Saguzovsk static int
19732414Saguzovsk segvn_pp_lock_anonpages(page_t *pp, int first)
19742414Saguzovsk {
19752414Saguzovsk 	pgcnt_t		pages;
19762414Saguzovsk 	pfn_t		pfn;
19772414Saguzovsk 	uchar_t		szc = pp->p_szc;
19782414Saguzovsk 
19792414Saguzovsk 	ASSERT(PAGE_LOCKED(pp));
19802414Saguzovsk 	ASSERT(pp->p_vnode != NULL);
19812414Saguzovsk 	ASSERT(IS_SWAPFSVP(pp->p_vnode));
19822414Saguzovsk 
19832414Saguzovsk 	/*
19842414Saguzovsk 	 * pagesize won't change as long as any constituent page is locked.
19852414Saguzovsk 	 */
19862414Saguzovsk 	pages = page_get_pagecnt(pp->p_szc);
19872414Saguzovsk 	pfn = page_pptonum(pp);
19882414Saguzovsk 
19892414Saguzovsk 	if (!first) {
19902414Saguzovsk 		if (!IS_P2ALIGNED(pfn, pages)) {
19912414Saguzovsk #ifdef DEBUG
19922414Saguzovsk 			pp = &pp[-(spgcnt_t)(pfn & (pages - 1))];
19932414Saguzovsk 			pfn = page_pptonum(pp);
19942414Saguzovsk 			ASSERT(IS_P2ALIGNED(pfn, pages));
19952414Saguzovsk 			ASSERT(pp->p_szc == szc);
19962414Saguzovsk 			ASSERT(pp->p_vnode != NULL);
19972414Saguzovsk 			ASSERT(IS_SWAPFSVP(pp->p_vnode));
19982414Saguzovsk 			ASSERT(pp->p_slckcnt != 0);
19992414Saguzovsk #endif /* DEBUG */
20002414Saguzovsk 			return (1);
20012414Saguzovsk 		}
20022414Saguzovsk 	} else if (!IS_P2ALIGNED(pfn, pages)) {
20032414Saguzovsk 		pp = &pp[-(spgcnt_t)(pfn & (pages - 1))];
20042414Saguzovsk #ifdef DEBUG
20052414Saguzovsk 		pfn = page_pptonum(pp);
20062414Saguzovsk 		ASSERT(IS_P2ALIGNED(pfn, pages));
20072414Saguzovsk 		ASSERT(pp->p_szc == szc);
20082414Saguzovsk 		ASSERT(pp->p_vnode != NULL);
20092414Saguzovsk 		ASSERT(IS_SWAPFSVP(pp->p_vnode));
20102414Saguzovsk #endif /* DEBUG */
20112414Saguzovsk 	}
20122414Saguzovsk 
20132414Saguzovsk 	/*
20142414Saguzovsk 	 * pp is a root page.
20152414Saguzovsk 	 * We haven't locked this large page yet.
20162414Saguzovsk 	 */
20172414Saguzovsk 	page_struct_lock(pp);
20182414Saguzovsk 	if (pp->p_slckcnt != 0) {
20192414Saguzovsk 		if (pp->p_slckcnt < PAGE_SLOCK_MAXIMUM) {
20202414Saguzovsk 			pp->p_slckcnt++;
20212414Saguzovsk 			page_struct_unlock(pp);
20222414Saguzovsk 			return (1);
20232414Saguzovsk 		}
20242414Saguzovsk 		page_struct_unlock(pp);
20252414Saguzovsk 		segvn_lpglck_limit++;
20262414Saguzovsk 		return (0);
20272414Saguzovsk 	}
20282414Saguzovsk 	mutex_enter(&freemem_lock);
20292414Saguzovsk 	if (availrmem < tune.t_minarmem + pages) {
20302414Saguzovsk 		mutex_exit(&freemem_lock);
20312414Saguzovsk 		page_struct_unlock(pp);
20322414Saguzovsk 		return (0);
20332414Saguzovsk 	}
20342414Saguzovsk 	pp->p_slckcnt++;
20352414Saguzovsk 	availrmem -= pages;
20362414Saguzovsk 	mutex_exit(&freemem_lock);
20372414Saguzovsk 	page_struct_unlock(pp);
20382414Saguzovsk 	return (1);
20392414Saguzovsk }
20402414Saguzovsk 
20412414Saguzovsk static void
20422414Saguzovsk segvn_pp_unlock_anonpages(page_t *pp, int first)
20432414Saguzovsk {
20442414Saguzovsk 	pgcnt_t		pages;
20452414Saguzovsk 	pfn_t		pfn;
20462414Saguzovsk 
20472414Saguzovsk 	ASSERT(PAGE_LOCKED(pp));
20482414Saguzovsk 	ASSERT(pp->p_vnode != NULL);
20492414Saguzovsk 	ASSERT(IS_SWAPFSVP(pp->p_vnode));
20502414Saguzovsk 
20512414Saguzovsk 	/*
20522414Saguzovsk 	 * pagesize won't change as long as any constituent page is locked.
20532414Saguzovsk 	 */
20542414Saguzovsk 	pages = page_get_pagecnt(pp->p_szc);
20552414Saguzovsk 	pfn = page_pptonum(pp);
20562414Saguzovsk 
20572414Saguzovsk 	if (!first) {
20582414Saguzovsk 		if (!IS_P2ALIGNED(pfn, pages)) {
20592414Saguzovsk 			return;
20602414Saguzovsk 		}
20612414Saguzovsk 	} else if (!IS_P2ALIGNED(pfn, pages)) {
20622414Saguzovsk 		pp = &pp[-(spgcnt_t)(pfn & (pages - 1))];
20632414Saguzovsk #ifdef DEBUG
20642414Saguzovsk 		pfn = page_pptonum(pp);
20652414Saguzovsk 		ASSERT(IS_P2ALIGNED(pfn, pages));
20662414Saguzovsk #endif /* DEBUG */
20672414Saguzovsk 	}
20682414Saguzovsk 	ASSERT(pp->p_vnode != NULL);
20692414Saguzovsk 	ASSERT(IS_SWAPFSVP(pp->p_vnode));
20702414Saguzovsk 	ASSERT(pp->p_slckcnt != 0);
20712414Saguzovsk 	page_struct_lock(pp);
20722414Saguzovsk 	if (--pp->p_slckcnt == 0) {
20732414Saguzovsk 		mutex_enter(&freemem_lock);
20742414Saguzovsk 		availrmem += pages;
20752414Saguzovsk 		mutex_exit(&freemem_lock);
20762414Saguzovsk 	}
20772414Saguzovsk 	page_struct_unlock(pp);
20782414Saguzovsk }
20792414Saguzovsk 
20800Sstevel@tonic-gate /*
20810Sstevel@tonic-gate  * Do a F_SOFTUNLOCK call over the range requested.  The range must have
20820Sstevel@tonic-gate  * already been F_SOFTLOCK'ed.
20830Sstevel@tonic-gate  * Caller must always match addr and len of a softunlock with a previous
20840Sstevel@tonic-gate  * softlock with exactly the same addr and len.
20850Sstevel@tonic-gate  */
20860Sstevel@tonic-gate static void
20870Sstevel@tonic-gate segvn_softunlock(struct seg *seg, caddr_t addr, size_t len, enum seg_rw rw)
20880Sstevel@tonic-gate {
20890Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
20900Sstevel@tonic-gate 	page_t *pp;
20910Sstevel@tonic-gate 	caddr_t adr;
20920Sstevel@tonic-gate 	struct vnode *vp;
20930Sstevel@tonic-gate 	u_offset_t offset;
20940Sstevel@tonic-gate 	ulong_t anon_index;
20950Sstevel@tonic-gate 	struct anon_map *amp;
20960Sstevel@tonic-gate 	struct anon *ap = NULL;
20970Sstevel@tonic-gate 
20980Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
20990Sstevel@tonic-gate 	ASSERT(SEGVN_LOCK_HELD(seg->s_as, &svd->lock));
21000Sstevel@tonic-gate 
21010Sstevel@tonic-gate 	if ((amp = svd->amp) != NULL)
21020Sstevel@tonic-gate 		anon_index = svd->anon_index + seg_page(seg, addr);
21030Sstevel@tonic-gate 
21040Sstevel@tonic-gate 	hat_unlock(seg->s_as->a_hat, addr, len);
21050Sstevel@tonic-gate 	for (adr = addr; adr < addr + len; adr += PAGESIZE) {
21060Sstevel@tonic-gate 		if (amp != NULL) {
21070Sstevel@tonic-gate 			ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
21080Sstevel@tonic-gate 			if ((ap = anon_get_ptr(amp->ahp, anon_index++))
21090Sstevel@tonic-gate 								!= NULL) {
21100Sstevel@tonic-gate 				swap_xlate(ap, &vp, &offset);
21110Sstevel@tonic-gate 			} else {
21120Sstevel@tonic-gate 				vp = svd->vp;
21130Sstevel@tonic-gate 				offset = svd->offset +
21140Sstevel@tonic-gate 				    (uintptr_t)(adr - seg->s_base);
21150Sstevel@tonic-gate 			}
21160Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
21170Sstevel@tonic-gate 		} else {
21180Sstevel@tonic-gate 			vp = svd->vp;
21190Sstevel@tonic-gate 			offset = svd->offset +
21200Sstevel@tonic-gate 			    (uintptr_t)(adr - seg->s_base);
21210Sstevel@tonic-gate 		}
21220Sstevel@tonic-gate 
21230Sstevel@tonic-gate 		/*
21240Sstevel@tonic-gate 		 * Use page_find() instead of page_lookup() to
21250Sstevel@tonic-gate 		 * find the page since we know that it is locked.
21260Sstevel@tonic-gate 		 */
21270Sstevel@tonic-gate 		pp = page_find(vp, offset);
21280Sstevel@tonic-gate 		if (pp == NULL) {
21290Sstevel@tonic-gate 			panic(
21300Sstevel@tonic-gate 			    "segvn_softunlock: addr %p, ap %p, vp %p, off %llx",
21310Sstevel@tonic-gate 			    (void *)adr, (void *)ap, (void *)vp, offset);
21320Sstevel@tonic-gate 			/*NOTREACHED*/
21330Sstevel@tonic-gate 		}
21340Sstevel@tonic-gate 
21350Sstevel@tonic-gate 		if (rw == S_WRITE) {
21360Sstevel@tonic-gate 			hat_setrefmod(pp);
21370Sstevel@tonic-gate 			if (seg->s_as->a_vbits)
21380Sstevel@tonic-gate 				hat_setstat(seg->s_as, adr, PAGESIZE,
21390Sstevel@tonic-gate 				    P_REF | P_MOD);
21400Sstevel@tonic-gate 		} else if (rw != S_OTHER) {
21410Sstevel@tonic-gate 			hat_setref(pp);
21420Sstevel@tonic-gate 			if (seg->s_as->a_vbits)
21430Sstevel@tonic-gate 				hat_setstat(seg->s_as, adr, PAGESIZE, P_REF);
21440Sstevel@tonic-gate 		}
21450Sstevel@tonic-gate 		TRACE_3(TR_FAC_VM, TR_SEGVN_FAULT,
21460Sstevel@tonic-gate 			"segvn_fault:pp %p vp %p offset %llx", pp, vp, offset);
21472414Saguzovsk 		if (svd->vp == NULL) {
21482414Saguzovsk 			segvn_pp_unlock_anonpages(pp, adr == addr);
21492414Saguzovsk 		}
21500Sstevel@tonic-gate 		page_unlock(pp);
21510Sstevel@tonic-gate 	}
21520Sstevel@tonic-gate 	mutex_enter(&freemem_lock); /* for availrmem */
21532414Saguzovsk 	if (svd->vp != NULL) {
21542414Saguzovsk 		availrmem += btop(len);
21552414Saguzovsk 	}
21560Sstevel@tonic-gate 	segvn_pages_locked -= btop(len);
21570Sstevel@tonic-gate 	svd->softlockcnt -= btop(len);
21580Sstevel@tonic-gate 	mutex_exit(&freemem_lock);
21590Sstevel@tonic-gate 	if (svd->softlockcnt == 0) {
21600Sstevel@tonic-gate 		/*
21610Sstevel@tonic-gate 		 * All SOFTLOCKS are gone. Wakeup any waiting
21620Sstevel@tonic-gate 		 * unmappers so they can try again to unmap.
21630Sstevel@tonic-gate 		 * Check for waiters first without the mutex
21640Sstevel@tonic-gate 		 * held so we don't always grab the mutex on
21650Sstevel@tonic-gate 		 * softunlocks.
21660Sstevel@tonic-gate 		 */
21670Sstevel@tonic-gate 		if (AS_ISUNMAPWAIT(seg->s_as)) {
21680Sstevel@tonic-gate 			mutex_enter(&seg->s_as->a_contents);
21690Sstevel@tonic-gate 			if (AS_ISUNMAPWAIT(seg->s_as)) {
21700Sstevel@tonic-gate 				AS_CLRUNMAPWAIT(seg->s_as);
21710Sstevel@tonic-gate 				cv_broadcast(&seg->s_as->a_cv);
21720Sstevel@tonic-gate 			}
21730Sstevel@tonic-gate 			mutex_exit(&seg->s_as->a_contents);
21740Sstevel@tonic-gate 		}
21750Sstevel@tonic-gate 	}
21760Sstevel@tonic-gate }
21770Sstevel@tonic-gate 
21780Sstevel@tonic-gate #define	PAGE_HANDLED	((page_t *)-1)
21790Sstevel@tonic-gate 
21800Sstevel@tonic-gate /*
21810Sstevel@tonic-gate  * Release all the pages in the NULL terminated ppp list
21820Sstevel@tonic-gate  * which haven't already been converted to PAGE_HANDLED.
21830Sstevel@tonic-gate  */
21840Sstevel@tonic-gate static void
21850Sstevel@tonic-gate segvn_pagelist_rele(page_t **ppp)
21860Sstevel@tonic-gate {
21870Sstevel@tonic-gate 	for (; *ppp != NULL; ppp++) {
21880Sstevel@tonic-gate 		if (*ppp != PAGE_HANDLED)
21890Sstevel@tonic-gate 			page_unlock(*ppp);
21900Sstevel@tonic-gate 	}
21910Sstevel@tonic-gate }
21920Sstevel@tonic-gate 
21930Sstevel@tonic-gate static int stealcow = 1;
21940Sstevel@tonic-gate 
21950Sstevel@tonic-gate /*
21960Sstevel@tonic-gate  * Workaround for viking chip bug.  See bug id 1220902.
21970Sstevel@tonic-gate  * To fix this down in pagefault() would require importing so
21980Sstevel@tonic-gate  * much as and segvn code as to be unmaintainable.
21990Sstevel@tonic-gate  */
22000Sstevel@tonic-gate int enable_mbit_wa = 0;
22010Sstevel@tonic-gate 
22020Sstevel@tonic-gate /*
22030Sstevel@tonic-gate  * Handles all the dirty work of getting the right
22040Sstevel@tonic-gate  * anonymous pages and loading up the translations.
22050Sstevel@tonic-gate  * This routine is called only from segvn_fault()
22060Sstevel@tonic-gate  * when looping over the range of addresses requested.
22070Sstevel@tonic-gate  *
22080Sstevel@tonic-gate  * The basic algorithm here is:
22090Sstevel@tonic-gate  * 	If this is an anon_zero case
22100Sstevel@tonic-gate  *		Call anon_zero to allocate page
22110Sstevel@tonic-gate  *		Load up translation
22120Sstevel@tonic-gate  *		Return
22130Sstevel@tonic-gate  *	endif
22140Sstevel@tonic-gate  *	If this is an anon page
22150Sstevel@tonic-gate  *		Use anon_getpage to get the page
22160Sstevel@tonic-gate  *	else
22170Sstevel@tonic-gate  *		Find page in pl[] list passed in
22180Sstevel@tonic-gate  *	endif
22190Sstevel@tonic-gate  *	If not a cow
22200Sstevel@tonic-gate  *		Load up the translation to the page
22210Sstevel@tonic-gate  *		return
22220Sstevel@tonic-gate  *	endif
22230Sstevel@tonic-gate  *	Call anon_private to handle cow
22240Sstevel@tonic-gate  *	Load up (writable) translation to new page
22250Sstevel@tonic-gate  */
22260Sstevel@tonic-gate static faultcode_t
22270Sstevel@tonic-gate segvn_faultpage(
22280Sstevel@tonic-gate 	struct hat *hat,		/* the hat to use for mapping */
22290Sstevel@tonic-gate 	struct seg *seg,		/* seg_vn of interest */
22300Sstevel@tonic-gate 	caddr_t addr,			/* address in as */
22310Sstevel@tonic-gate 	u_offset_t off,			/* offset in vp */
22320Sstevel@tonic-gate 	struct vpage *vpage,		/* pointer to vpage for vp, off */
22330Sstevel@tonic-gate 	page_t *pl[],			/* object source page pointer */
22340Sstevel@tonic-gate 	uint_t vpprot,			/* access allowed to object pages */
22350Sstevel@tonic-gate 	enum fault_type type,		/* type of fault */
22360Sstevel@tonic-gate 	enum seg_rw rw,			/* type of access at fault */
22372414Saguzovsk 	int brkcow,			/* we may need to break cow */
22382414Saguzovsk 	int first)			/* first page for this fault if 1 */
22390Sstevel@tonic-gate {
22400Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
22410Sstevel@tonic-gate 	page_t *pp, **ppp;
22420Sstevel@tonic-gate 	uint_t pageflags = 0;
22430Sstevel@tonic-gate 	page_t *anon_pl[1 + 1];
22440Sstevel@tonic-gate 	page_t *opp = NULL;		/* original page */
22450Sstevel@tonic-gate 	uint_t prot;
22460Sstevel@tonic-gate 	int err;
22470Sstevel@tonic-gate 	int cow;
22480Sstevel@tonic-gate 	int claim;
22490Sstevel@tonic-gate 	int steal = 0;
22500Sstevel@tonic-gate 	ulong_t anon_index;
22510Sstevel@tonic-gate 	struct anon *ap, *oldap;
22520Sstevel@tonic-gate 	struct anon_map *amp;
22530Sstevel@tonic-gate 	int hat_flag = (type == F_SOFTLOCK) ? HAT_LOAD_LOCK : HAT_LOAD;
22540Sstevel@tonic-gate 	int anon_lock = 0;
22550Sstevel@tonic-gate 	anon_sync_obj_t cookie;
22560Sstevel@tonic-gate 
22570Sstevel@tonic-gate 	if (svd->flags & MAP_TEXT) {
22580Sstevel@tonic-gate 		hat_flag |= HAT_LOAD_TEXT;
22590Sstevel@tonic-gate 	}
22600Sstevel@tonic-gate 
22610Sstevel@tonic-gate 	ASSERT(SEGVN_READ_HELD(seg->s_as, &svd->lock));
22620Sstevel@tonic-gate 	ASSERT(seg->s_szc == 0);
22630Sstevel@tonic-gate 
22640Sstevel@tonic-gate 	/*
22650Sstevel@tonic-gate 	 * Initialize protection value for this page.
22660Sstevel@tonic-gate 	 * If we have per page protection values check it now.
22670Sstevel@tonic-gate 	 */
22680Sstevel@tonic-gate 	if (svd->pageprot) {
22690Sstevel@tonic-gate 		uint_t protchk;
22700Sstevel@tonic-gate 
22710Sstevel@tonic-gate 		switch (rw) {
22720Sstevel@tonic-gate 		case S_READ:
22730Sstevel@tonic-gate 			protchk = PROT_READ;
22740Sstevel@tonic-gate 			break;
22750Sstevel@tonic-gate 		case S_WRITE:
22760Sstevel@tonic-gate 			protchk = PROT_WRITE;
22770Sstevel@tonic-gate 			break;
22780Sstevel@tonic-gate 		case S_EXEC:
22790Sstevel@tonic-gate 			protchk = PROT_EXEC;
22800Sstevel@tonic-gate 			break;
22810Sstevel@tonic-gate 		case S_OTHER:
22820Sstevel@tonic-gate 		default:
22830Sstevel@tonic-gate 			protchk = PROT_READ | PROT_WRITE | PROT_EXEC;
22840Sstevel@tonic-gate 			break;
22850Sstevel@tonic-gate 		}
22860Sstevel@tonic-gate 
22870Sstevel@tonic-gate 		prot = VPP_PROT(vpage);
22880Sstevel@tonic-gate 		if ((prot & protchk) == 0)
22890Sstevel@tonic-gate 			return (FC_PROT);	/* illegal access type */
22900Sstevel@tonic-gate 	} else {
22910Sstevel@tonic-gate 		prot = svd->prot;
22920Sstevel@tonic-gate 	}
22930Sstevel@tonic-gate 
22942414Saguzovsk 	if (type == F_SOFTLOCK && svd->vp != NULL) {
22950Sstevel@tonic-gate 		mutex_enter(&freemem_lock);
22960Sstevel@tonic-gate 		if (availrmem <= tune.t_minarmem) {
22970Sstevel@tonic-gate 			mutex_exit(&freemem_lock);
22980Sstevel@tonic-gate 			return (FC_MAKE_ERR(ENOMEM));	/* out of real memory */
22990Sstevel@tonic-gate 		} else {
23002414Saguzovsk 			availrmem--;
23010Sstevel@tonic-gate 			svd->softlockcnt++;
23020Sstevel@tonic-gate 			segvn_pages_locked++;
23030Sstevel@tonic-gate 		}
23040Sstevel@tonic-gate 		mutex_exit(&freemem_lock);
23050Sstevel@tonic-gate 	}
23060Sstevel@tonic-gate 
23070Sstevel@tonic-gate 	/*
23080Sstevel@tonic-gate 	 * Always acquire the anon array lock to prevent 2 threads from
23090Sstevel@tonic-gate 	 * allocating separate anon slots for the same "addr".
23100Sstevel@tonic-gate 	 */
23110Sstevel@tonic-gate 
23120Sstevel@tonic-gate 	if ((amp = svd->amp) != NULL) {
23130Sstevel@tonic-gate 		ASSERT(RW_READ_HELD(&amp->a_rwlock));
23140Sstevel@tonic-gate 		anon_index = svd->anon_index + seg_page(seg, addr);
23150Sstevel@tonic-gate 		anon_array_enter(amp, anon_index, &cookie);
23160Sstevel@tonic-gate 		anon_lock = 1;
23170Sstevel@tonic-gate 	}
23180Sstevel@tonic-gate 
23190Sstevel@tonic-gate 	if (svd->vp == NULL && amp != NULL) {
23200Sstevel@tonic-gate 		if ((ap = anon_get_ptr(amp->ahp, anon_index)) == NULL) {
23210Sstevel@tonic-gate 			/*
23220Sstevel@tonic-gate 			 * Allocate a (normally) writable anonymous page of
23230Sstevel@tonic-gate 			 * zeroes. If no advance reservations, reserve now.
23240Sstevel@tonic-gate 			 */
23250Sstevel@tonic-gate 			if (svd->flags & MAP_NORESERVE) {
23263247Sgjelinek 				if (anon_resv_zone(ptob(1),
23273247Sgjelinek 				    seg->s_as->a_proc->p_zone)) {
23283247Sgjelinek 					atomic_add_long(&svd->swresv, ptob(1));
23290Sstevel@tonic-gate 				} else {
23300Sstevel@tonic-gate 					err = ENOMEM;
23310Sstevel@tonic-gate 					goto out;
23320Sstevel@tonic-gate 				}
23330Sstevel@tonic-gate 			}
23340Sstevel@tonic-gate 			if ((pp = anon_zero(seg, addr, &ap,
23350Sstevel@tonic-gate 			    svd->cred)) == NULL) {
23360Sstevel@tonic-gate 				err = ENOMEM;
23370Sstevel@tonic-gate 				goto out;	/* out of swap space */
23380Sstevel@tonic-gate 			}
23390Sstevel@tonic-gate 			/*
23400Sstevel@tonic-gate 			 * Re-acquire the anon_map lock and
23410Sstevel@tonic-gate 			 * initialize the anon array entry.
23420Sstevel@tonic-gate 			 */
23430Sstevel@tonic-gate 			(void) anon_set_ptr(amp->ahp, anon_index, ap,
23440Sstevel@tonic-gate 				ANON_SLEEP);
23452414Saguzovsk 
23462414Saguzovsk 			ASSERT(pp->p_szc == 0);
23472505Saguzovsk 
23482505Saguzovsk 			/*
23492505Saguzovsk 			 * Handle pages that have been marked for migration
23502505Saguzovsk 			 */
23512505Saguzovsk 			if (lgrp_optimizations())
23522505Saguzovsk 				page_migrate(seg, addr, &pp, 1);
23532505Saguzovsk 
23542414Saguzovsk 			if (type == F_SOFTLOCK) {
23552414Saguzovsk 				if (!segvn_pp_lock_anonpages(pp, first)) {
23562414Saguzovsk 					page_unlock(pp);
23572414Saguzovsk 					err = ENOMEM;
23582414Saguzovsk 					goto out;
23592414Saguzovsk 				} else {
23602414Saguzovsk 					mutex_enter(&freemem_lock);
23612414Saguzovsk 					svd->softlockcnt++;
23622414Saguzovsk 					segvn_pages_locked++;
23632414Saguzovsk 					mutex_exit(&freemem_lock);
23642414Saguzovsk 				}
23652414Saguzovsk 			}
23662414Saguzovsk 
23670Sstevel@tonic-gate 			if (enable_mbit_wa) {
23680Sstevel@tonic-gate 				if (rw == S_WRITE)
23690Sstevel@tonic-gate 					hat_setmod(pp);
23700Sstevel@tonic-gate 				else if (!hat_ismod(pp))
23710Sstevel@tonic-gate 					prot &= ~PROT_WRITE;
23720Sstevel@tonic-gate 			}
23730Sstevel@tonic-gate 			/*
23740Sstevel@tonic-gate 			 * If AS_PAGLCK is set in a_flags (via memcntl(2)
23750Sstevel@tonic-gate 			 * with MC_LOCKAS, MCL_FUTURE) and this is a
23760Sstevel@tonic-gate 			 * MAP_NORESERVE segment, we may need to
23770Sstevel@tonic-gate 			 * permanently lock the page as it is being faulted
23780Sstevel@tonic-gate 			 * for the first time. The following text applies
23790Sstevel@tonic-gate 			 * only to MAP_NORESERVE segments:
23800Sstevel@tonic-gate 			 *
23810Sstevel@tonic-gate 			 * As per memcntl(2), if this segment was created
23820Sstevel@tonic-gate 			 * after MCL_FUTURE was applied (a "future"
23830Sstevel@tonic-gate 			 * segment), its pages must be locked.  If this
23840Sstevel@tonic-gate 			 * segment existed at MCL_FUTURE application (a
23850Sstevel@tonic-gate 			 * "past" segment), the interface is unclear.
23860Sstevel@tonic-gate 			 *
23870Sstevel@tonic-gate 			 * We decide to lock only if vpage is present:
23880Sstevel@tonic-gate 			 *
23890Sstevel@tonic-gate 			 * - "future" segments will have a vpage array (see
23900Sstevel@tonic-gate 			 *    as_map), and so will be locked as required
23910Sstevel@tonic-gate 			 *
23920Sstevel@tonic-gate 			 * - "past" segments may not have a vpage array,
23930Sstevel@tonic-gate 			 *    depending on whether events (such as
23940Sstevel@tonic-gate 			 *    mprotect) have occurred. Locking if vpage
23950Sstevel@tonic-gate 			 *    exists will preserve legacy behavior.  Not
23960Sstevel@tonic-gate 			 *    locking if vpage is absent, will not break
23970Sstevel@tonic-gate 			 *    the interface or legacy behavior.  Note that
23980Sstevel@tonic-gate 			 *    allocating vpage here if it's absent requires
23990Sstevel@tonic-gate 			 *    upgrading the segvn reader lock, the cost of
24000Sstevel@tonic-gate 			 *    which does not seem worthwhile.
24012768Ssl108498 			 *
24022768Ssl108498 			 * Usually testing and setting VPP_ISPPLOCK and
24032768Ssl108498 			 * VPP_SETPPLOCK requires holding the segvn lock as
24042768Ssl108498 			 * writer, but in this case all readers are
24052768Ssl108498 			 * serializing on the anon array lock.
24060Sstevel@tonic-gate 			 */
24070Sstevel@tonic-gate 			if (AS_ISPGLCK(seg->s_as) && vpage != NULL &&
24082768Ssl108498 			    (svd->flags & MAP_NORESERVE) &&
24092768Ssl108498 			    !VPP_ISPPLOCK(vpage)) {
24102768Ssl108498 				proc_t *p = seg->s_as->a_proc;
24110Sstevel@tonic-gate 				ASSERT(svd->type == MAP_PRIVATE);
24122768Ssl108498 				mutex_enter(&p->p_lock);
24132768Ssl108498 				if (rctl_incr_locked_mem(p, NULL, PAGESIZE,
24142768Ssl108498 				    1) == 0) {
24152768Ssl108498 					claim = VPP_PROT(vpage) & PROT_WRITE;
24162768Ssl108498 					if (page_pp_lock(pp, claim, 0)) {
24172768Ssl108498 						VPP_SETPPLOCK(vpage);
24182768Ssl108498 					} else {
24192768Ssl108498 						rctl_decr_locked_mem(p, NULL,
24202768Ssl108498 						    PAGESIZE, 1);
24212768Ssl108498 					}
24222768Ssl108498 				}
24232768Ssl108498 				mutex_exit(&p->p_lock);
24240Sstevel@tonic-gate 			}
24250Sstevel@tonic-gate 
24260Sstevel@tonic-gate 			hat_memload(hat, addr, pp, prot, hat_flag);
24270Sstevel@tonic-gate 
24280Sstevel@tonic-gate 			if (!(hat_flag & HAT_LOAD_LOCK))
24290Sstevel@tonic-gate 				page_unlock(pp);
24300Sstevel@tonic-gate 
24310Sstevel@tonic-gate 			anon_array_exit(&cookie);
24320Sstevel@tonic-gate 			return (0);
24330Sstevel@tonic-gate 		}
24340Sstevel@tonic-gate 	}
24350Sstevel@tonic-gate 
24360Sstevel@tonic-gate 	/*
24370Sstevel@tonic-gate 	 * Obtain the page structure via anon_getpage() if it is
24380Sstevel@tonic-gate 	 * a private copy of an object (the result of a previous
24390Sstevel@tonic-gate 	 * copy-on-write).
24400Sstevel@tonic-gate 	 */
24410Sstevel@tonic-gate 	if (amp != NULL) {
24420Sstevel@tonic-gate 		if ((ap = anon_get_ptr(amp->ahp, anon_index)) != NULL) {
24430Sstevel@tonic-gate 			err = anon_getpage(&ap, &vpprot, anon_pl, PAGESIZE,
24440Sstevel@tonic-gate 			    seg, addr, rw, svd->cred);
24450Sstevel@tonic-gate 			if (err)
24460Sstevel@tonic-gate 				goto out;
24470Sstevel@tonic-gate 
24480Sstevel@tonic-gate 			if (svd->type == MAP_SHARED) {
24490Sstevel@tonic-gate 				/*
24500Sstevel@tonic-gate 				 * If this is a shared mapping to an
24510Sstevel@tonic-gate 				 * anon_map, then ignore the write
24520Sstevel@tonic-gate 				 * permissions returned by anon_getpage().
24530Sstevel@tonic-gate 				 * They apply to the private mappings
24540Sstevel@tonic-gate 				 * of this anon_map.
24550Sstevel@tonic-gate 				 */
24560Sstevel@tonic-gate 				vpprot |= PROT_WRITE;
24570Sstevel@tonic-gate 			}
24580Sstevel@tonic-gate 			opp = anon_pl[0];
24590Sstevel@tonic-gate 		}
24600Sstevel@tonic-gate 	}
24610Sstevel@tonic-gate 
24620Sstevel@tonic-gate 	/*
24630Sstevel@tonic-gate 	 * Search the pl[] list passed in if it is from the
24640Sstevel@tonic-gate 	 * original object (i.e., not a private copy).
24650Sstevel@tonic-gate 	 */
24660Sstevel@tonic-gate 	if (opp == NULL) {
24670Sstevel@tonic-gate 		/*
24680Sstevel@tonic-gate 		 * Find original page.  We must be bringing it in
24690Sstevel@tonic-gate 		 * from the list in pl[].
24700Sstevel@tonic-gate 		 */
24710Sstevel@tonic-gate 		for (ppp = pl; (opp = *ppp) != NULL; ppp++) {
24720Sstevel@tonic-gate 			if (opp == PAGE_HANDLED)
24730Sstevel@tonic-gate 				continue;
24740Sstevel@tonic-gate 			ASSERT(opp->p_vnode == svd->vp); /* XXX */
24750Sstevel@tonic-gate 			if (opp->p_offset == off)
24760Sstevel@tonic-gate 				break;
24770Sstevel@tonic-gate 		}
24780Sstevel@tonic-gate 		if (opp == NULL) {
24790Sstevel@tonic-gate 			panic("segvn_faultpage not found");
24800Sstevel@tonic-gate 			/*NOTREACHED*/
24810Sstevel@tonic-gate 		}
24820Sstevel@tonic-gate 		*ppp = PAGE_HANDLED;
24830Sstevel@tonic-gate 
24840Sstevel@tonic-gate 	}
24850Sstevel@tonic-gate 
24860Sstevel@tonic-gate 	ASSERT(PAGE_LOCKED(opp));
24870Sstevel@tonic-gate 
24880Sstevel@tonic-gate 	TRACE_3(TR_FAC_VM, TR_SEGVN_FAULT,
24890Sstevel@tonic-gate 		"segvn_fault:pp %p vp %p offset %llx",
24900Sstevel@tonic-gate 		opp, NULL, 0);
24910Sstevel@tonic-gate 
24920Sstevel@tonic-gate 	/*
24930Sstevel@tonic-gate 	 * The fault is treated as a copy-on-write fault if a
24940Sstevel@tonic-gate 	 * write occurs on a private segment and the object
24950Sstevel@tonic-gate 	 * page (i.e., mapping) is write protected.  We assume
24960Sstevel@tonic-gate 	 * that fatal protection checks have already been made.
24970Sstevel@tonic-gate 	 */
24980Sstevel@tonic-gate 
24990Sstevel@tonic-gate 	cow = brkcow && ((vpprot & PROT_WRITE) == 0);
25000Sstevel@tonic-gate 
25010Sstevel@tonic-gate 	/*
25020Sstevel@tonic-gate 	 * If not a copy-on-write case load the translation
25030Sstevel@tonic-gate 	 * and return.
25040Sstevel@tonic-gate 	 */
25050Sstevel@tonic-gate 	if (cow == 0) {
25062505Saguzovsk 
25072505Saguzovsk 		/*
25082505Saguzovsk 		 * Handle pages that have been marked for migration
25092505Saguzovsk 		 */
25102505Saguzovsk 		if (lgrp_optimizations())
25112505Saguzovsk 			page_migrate(seg, addr, &opp, 1);
25122505Saguzovsk 
25132414Saguzovsk 		if (type == F_SOFTLOCK && svd->vp == NULL) {
25142414Saguzovsk 
25152414Saguzovsk 			ASSERT(opp->p_szc == 0 ||
25162414Saguzovsk 			    (svd->type == MAP_SHARED &&
25172414Saguzovsk 				amp != NULL && amp->a_szc != 0));
25182414Saguzovsk 
25192414Saguzovsk 			if (!segvn_pp_lock_anonpages(opp, first)) {
25202414Saguzovsk 				page_unlock(opp);
25212414Saguzovsk 				err = ENOMEM;
25222414Saguzovsk 				goto out;
25232414Saguzovsk 			} else {
25242414Saguzovsk 				mutex_enter(&freemem_lock);
25252414Saguzovsk 				svd->softlockcnt++;
25262414Saguzovsk 				segvn_pages_locked++;
25272414Saguzovsk 				mutex_exit(&freemem_lock);
25282414Saguzovsk 			}
25292414Saguzovsk 		}
25300Sstevel@tonic-gate 		if (IS_VMODSORT(opp->p_vnode) || enable_mbit_wa) {
25310Sstevel@tonic-gate 			if (rw == S_WRITE)
25320Sstevel@tonic-gate 				hat_setmod(opp);
25330Sstevel@tonic-gate 			else if (rw != S_OTHER && !hat_ismod(opp))
25340Sstevel@tonic-gate 				prot &= ~PROT_WRITE;
25350Sstevel@tonic-gate 		}
25360Sstevel@tonic-gate 
25370Sstevel@tonic-gate 		hat_memload(hat, addr, opp, prot & vpprot, hat_flag);
25380Sstevel@tonic-gate 
25390Sstevel@tonic-gate 		if (!(hat_flag & HAT_LOAD_LOCK))
25400Sstevel@tonic-gate 			page_unlock(opp);
25410Sstevel@tonic-gate 
25420Sstevel@tonic-gate 		if (anon_lock) {
25430Sstevel@tonic-gate 			anon_array_exit(&cookie);
25440Sstevel@tonic-gate 		}
25450Sstevel@tonic-gate 		return (0);
25460Sstevel@tonic-gate 	}
25470Sstevel@tonic-gate 
25480Sstevel@tonic-gate 	hat_setref(opp);
25490Sstevel@tonic-gate 
25500Sstevel@tonic-gate 	ASSERT(amp != NULL && anon_lock);
25510Sstevel@tonic-gate 
25520Sstevel@tonic-gate 	/*
25530Sstevel@tonic-gate 	 * Steal the page only if it isn't a private page
25540Sstevel@tonic-gate 	 * since stealing a private page is not worth the effort.
25550Sstevel@tonic-gate 	 */
25560Sstevel@tonic-gate 	if ((ap = anon_get_ptr(amp->ahp, anon_index)) == NULL)
25570Sstevel@tonic-gate 		steal = 1;
25580Sstevel@tonic-gate 
25590Sstevel@tonic-gate 	/*
25600Sstevel@tonic-gate 	 * Steal the original page if the following conditions are true:
25610Sstevel@tonic-gate 	 *
256263Saguzovsk 	 * We are low on memory, the page is not private, page is not large,
256363Saguzovsk 	 * not shared, not modified, not `locked' or if we have it `locked'
25640Sstevel@tonic-gate 	 * (i.e., p_cowcnt == 1 and p_lckcnt == 0, which also implies
25650Sstevel@tonic-gate 	 * that the page is not shared) and if it doesn't have any
25660Sstevel@tonic-gate 	 * translations. page_struct_lock isn't needed to look at p_cowcnt
25670Sstevel@tonic-gate 	 * and p_lckcnt because we first get exclusive lock on page.
25680Sstevel@tonic-gate 	 */
25690Sstevel@tonic-gate 	(void) hat_pagesync(opp, HAT_SYNC_DONTZERO | HAT_SYNC_STOPON_MOD);
25700Sstevel@tonic-gate 
257163Saguzovsk 	if (stealcow && freemem < minfree && steal && opp->p_szc == 0 &&
25720Sstevel@tonic-gate 	    page_tryupgrade(opp) && !hat_ismod(opp) &&
25730Sstevel@tonic-gate 	    ((opp->p_lckcnt == 0 && opp->p_cowcnt == 0) ||
25740Sstevel@tonic-gate 	    (opp->p_lckcnt == 0 && opp->p_cowcnt == 1 &&
25750Sstevel@tonic-gate 	    vpage != NULL && VPP_ISPPLOCK(vpage)))) {
25760Sstevel@tonic-gate 		/*
25770Sstevel@tonic-gate 		 * Check if this page has other translations
25780Sstevel@tonic-gate 		 * after unloading our translation.
25790Sstevel@tonic-gate 		 */
25800Sstevel@tonic-gate 		if (hat_page_is_mapped(opp)) {
25810Sstevel@tonic-gate 			hat_unload(seg->s_as->a_hat, addr, PAGESIZE,
25820Sstevel@tonic-gate 				HAT_UNLOAD);
25830Sstevel@tonic-gate 		}
25840Sstevel@tonic-gate 
25850Sstevel@tonic-gate 		/*
25860Sstevel@tonic-gate 		 * hat_unload() might sync back someone else's recent
25870Sstevel@tonic-gate 		 * modification, so check again.
25880Sstevel@tonic-gate 		 */
25890Sstevel@tonic-gate 		if (!hat_ismod(opp) && !hat_page_is_mapped(opp))
25900Sstevel@tonic-gate 			pageflags |= STEAL_PAGE;
25910Sstevel@tonic-gate 	}
25920Sstevel@tonic-gate 
25930Sstevel@tonic-gate 	/*
25940Sstevel@tonic-gate 	 * If we have a vpage pointer, see if it indicates that we have
25950Sstevel@tonic-gate 	 * ``locked'' the page we map -- if so, tell anon_private to
25960Sstevel@tonic-gate 	 * transfer the locking resource to the new page.
25970Sstevel@tonic-gate 	 *
25980Sstevel@tonic-gate 	 * See Statement at the beginning of segvn_lockop regarding
25990Sstevel@tonic-gate 	 * the way lockcnts/cowcnts are handled during COW.
26000Sstevel@tonic-gate 	 *
26010Sstevel@tonic-gate 	 */
26020Sstevel@tonic-gate 	if (vpage != NULL && VPP_ISPPLOCK(vpage))
26030Sstevel@tonic-gate 		pageflags |= LOCK_PAGE;
26040Sstevel@tonic-gate 
26050Sstevel@tonic-gate 	/*
26060Sstevel@tonic-gate 	 * Allocate a private page and perform the copy.
26070Sstevel@tonic-gate 	 * For MAP_NORESERVE reserve swap space now, unless this
26080Sstevel@tonic-gate 	 * is a cow fault on an existing anon page in which case
26090Sstevel@tonic-gate 	 * MAP_NORESERVE will have made advance reservations.
26100Sstevel@tonic-gate 	 */
26110Sstevel@tonic-gate 	if ((svd->flags & MAP_NORESERVE) && (ap == NULL)) {
26120Sstevel@tonic-gate 		if (anon_resv(ptob(1))) {
26130Sstevel@tonic-gate 			svd->swresv += ptob(1);
26140Sstevel@tonic-gate 		} else {
26150Sstevel@tonic-gate 			page_unlock(opp);
26160Sstevel@tonic-gate 			err = ENOMEM;
26170Sstevel@tonic-gate 			goto out;
26180Sstevel@tonic-gate 		}
26190Sstevel@tonic-gate 	}
26200Sstevel@tonic-gate 	oldap = ap;
26210Sstevel@tonic-gate 	pp = anon_private(&ap, seg, addr, prot, opp, pageflags, svd->cred);
26220Sstevel@tonic-gate 	if (pp == NULL) {
26230Sstevel@tonic-gate 		err = ENOMEM;	/* out of swap space */
26240Sstevel@tonic-gate 		goto out;
26250Sstevel@tonic-gate 	}
26260Sstevel@tonic-gate 
26270Sstevel@tonic-gate 	/*
26280Sstevel@tonic-gate 	 * If we copied away from an anonymous page, then
26290Sstevel@tonic-gate 	 * we are one step closer to freeing up an anon slot.
26300Sstevel@tonic-gate 	 *
26310Sstevel@tonic-gate 	 * NOTE:  The original anon slot must be released while
26320Sstevel@tonic-gate 	 * holding the "anon_map" lock.  This is necessary to prevent
26330Sstevel@tonic-gate 	 * other threads from obtaining a pointer to the anon slot
26340Sstevel@tonic-gate 	 * which may be freed if its "refcnt" is 1.
26350Sstevel@tonic-gate 	 */
26360Sstevel@tonic-gate 	if (oldap != NULL)
26370Sstevel@tonic-gate 		anon_decref(oldap);
26380Sstevel@tonic-gate 
26390Sstevel@tonic-gate 	(void) anon_set_ptr(amp->ahp, anon_index, ap, ANON_SLEEP);
26400Sstevel@tonic-gate 
26412505Saguzovsk 	/*
26422505Saguzovsk 	 * Handle pages that have been marked for migration
26432505Saguzovsk 	 */
26442505Saguzovsk 	if (lgrp_optimizations())
26452505Saguzovsk 		page_migrate(seg, addr, &pp, 1);
26462505Saguzovsk 
26472414Saguzovsk 	ASSERT(pp->p_szc == 0);
26482414Saguzovsk 	if (type == F_SOFTLOCK && svd->vp == NULL) {
26492414Saguzovsk 		if (!segvn_pp_lock_anonpages(pp, first)) {
26502414Saguzovsk 			page_unlock(pp);
26512414Saguzovsk 			err = ENOMEM;
26522414Saguzovsk 			goto out;
26532414Saguzovsk 		} else {
26542414Saguzovsk 			mutex_enter(&freemem_lock);
26552414Saguzovsk 			svd->softlockcnt++;
26562414Saguzovsk 			segvn_pages_locked++;
26572414Saguzovsk 			mutex_exit(&freemem_lock);
26582414Saguzovsk 		}
26592414Saguzovsk 	}
26602414Saguzovsk 
26610Sstevel@tonic-gate 	ASSERT(!IS_VMODSORT(pp->p_vnode));
26620Sstevel@tonic-gate 	if (enable_mbit_wa) {
26630Sstevel@tonic-gate 		if (rw == S_WRITE)
26640Sstevel@tonic-gate 			hat_setmod(pp);
26650Sstevel@tonic-gate 		else if (!hat_ismod(pp))
26660Sstevel@tonic-gate 			prot &= ~PROT_WRITE;
26670Sstevel@tonic-gate 	}
26680Sstevel@tonic-gate 
26690Sstevel@tonic-gate 	hat_memload(hat, addr, pp, prot, hat_flag);
26700Sstevel@tonic-gate 
26710Sstevel@tonic-gate 	if (!(hat_flag & HAT_LOAD_LOCK))
26720Sstevel@tonic-gate 		page_unlock(pp);
26730Sstevel@tonic-gate 
26740Sstevel@tonic-gate 	ASSERT(anon_lock);
26750Sstevel@tonic-gate 	anon_array_exit(&cookie);
26760Sstevel@tonic-gate 	return (0);
26770Sstevel@tonic-gate out:
26780Sstevel@tonic-gate 	if (anon_lock)
26790Sstevel@tonic-gate 		anon_array_exit(&cookie);
26800Sstevel@tonic-gate 
26812414Saguzovsk 	if (type == F_SOFTLOCK && svd->vp != NULL) {
26820Sstevel@tonic-gate 		mutex_enter(&freemem_lock);
26830Sstevel@tonic-gate 		availrmem++;
26840Sstevel@tonic-gate 		segvn_pages_locked--;
26850Sstevel@tonic-gate 		svd->softlockcnt--;
26860Sstevel@tonic-gate 		mutex_exit(&freemem_lock);
26870Sstevel@tonic-gate 	}
26880Sstevel@tonic-gate 	return (FC_MAKE_ERR(err));
26890Sstevel@tonic-gate }
26900Sstevel@tonic-gate 
26910Sstevel@tonic-gate /*
26920Sstevel@tonic-gate  * relocate a bunch of smaller targ pages into one large repl page. all targ
26930Sstevel@tonic-gate  * pages must be complete pages smaller than replacement pages.
26940Sstevel@tonic-gate  * it's assumed that no page's szc can change since they are all PAGESIZE or
26950Sstevel@tonic-gate  * complete large pages locked SHARED.
26960Sstevel@tonic-gate  */
26970Sstevel@tonic-gate static void
26980Sstevel@tonic-gate segvn_relocate_pages(page_t **targ, page_t *replacement)
26990Sstevel@tonic-gate {
27000Sstevel@tonic-gate 	page_t *pp;
27010Sstevel@tonic-gate 	pgcnt_t repl_npgs, curnpgs;
27020Sstevel@tonic-gate 	pgcnt_t i;
27030Sstevel@tonic-gate 	uint_t repl_szc = replacement->p_szc;
27040Sstevel@tonic-gate 	page_t *first_repl = replacement;
27050Sstevel@tonic-gate 	page_t *repl;
27060Sstevel@tonic-gate 	spgcnt_t npgs;
27070Sstevel@tonic-gate 
27080Sstevel@tonic-gate 	VM_STAT_ADD(segvnvmstats.relocatepages[0]);
27090Sstevel@tonic-gate 
27100Sstevel@tonic-gate 	ASSERT(repl_szc != 0);
27110Sstevel@tonic-gate 	npgs = repl_npgs = page_get_pagecnt(repl_szc);
27120Sstevel@tonic-gate 
27130Sstevel@tonic-gate 	i = 0;
27140Sstevel@tonic-gate 	while (repl_npgs) {
27150Sstevel@tonic-gate 		spgcnt_t nreloc;
27160Sstevel@tonic-gate 		int err;
27170Sstevel@tonic-gate 		ASSERT(replacement != NULL);
27180Sstevel@tonic-gate 		pp = targ[i];
27190Sstevel@tonic-gate 		ASSERT(pp->p_szc < repl_szc);
27200Sstevel@tonic-gate 		ASSERT(PAGE_EXCL(pp));
27210Sstevel@tonic-gate 		ASSERT(!PP_ISFREE(pp));
27220Sstevel@tonic-gate 		curnpgs = page_get_pagecnt(pp->p_szc);
27230Sstevel@tonic-gate 		if (curnpgs == 1) {
27240Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.relocatepages[1]);
27250Sstevel@tonic-gate 			repl = replacement;
27260Sstevel@tonic-gate 			page_sub(&replacement, repl);
27270Sstevel@tonic-gate 			ASSERT(PAGE_EXCL(repl));
27280Sstevel@tonic-gate 			ASSERT(!PP_ISFREE(repl));
27290Sstevel@tonic-gate 			ASSERT(repl->p_szc == repl_szc);
27300Sstevel@tonic-gate 		} else {
27310Sstevel@tonic-gate 			page_t *repl_savepp;
27320Sstevel@tonic-gate 			int j;
27330Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.relocatepages[2]);
27340Sstevel@tonic-gate 			repl_savepp = replacement;
27350Sstevel@tonic-gate 			for (j = 0; j < curnpgs; j++) {
27360Sstevel@tonic-gate 				repl = replacement;
27370Sstevel@tonic-gate 				page_sub(&replacement, repl);
27380Sstevel@tonic-gate 				ASSERT(PAGE_EXCL(repl));
27390Sstevel@tonic-gate 				ASSERT(!PP_ISFREE(repl));
27400Sstevel@tonic-gate 				ASSERT(repl->p_szc == repl_szc);
27410Sstevel@tonic-gate 				ASSERT(page_pptonum(targ[i + j]) ==
27420Sstevel@tonic-gate 				    page_pptonum(targ[i]) + j);
27430Sstevel@tonic-gate 			}
27440Sstevel@tonic-gate 			repl = repl_savepp;
27450Sstevel@tonic-gate 			ASSERT(IS_P2ALIGNED(page_pptonum(repl), curnpgs));
27460Sstevel@tonic-gate 		}
27470Sstevel@tonic-gate 		err = page_relocate(&pp, &repl, 0, 1, &nreloc, NULL);
27480Sstevel@tonic-gate 		if (err || nreloc != curnpgs) {
27490Sstevel@tonic-gate 			panic("segvn_relocate_pages: "
27500Sstevel@tonic-gate 			    "page_relocate failed err=%d curnpgs=%ld "
27510Sstevel@tonic-gate 			    "nreloc=%ld", err, curnpgs, nreloc);
27520Sstevel@tonic-gate 		}
27530Sstevel@tonic-gate 		ASSERT(curnpgs <= repl_npgs);
27540Sstevel@tonic-gate 		repl_npgs -= curnpgs;
27550Sstevel@tonic-gate 		i += curnpgs;
27560Sstevel@tonic-gate 	}
27570Sstevel@tonic-gate 	ASSERT(replacement == NULL);
27580Sstevel@tonic-gate 
27590Sstevel@tonic-gate 	repl = first_repl;
27600Sstevel@tonic-gate 	repl_npgs = npgs;
27610Sstevel@tonic-gate 	for (i = 0; i < repl_npgs; i++) {
27620Sstevel@tonic-gate 		ASSERT(PAGE_EXCL(repl));
27630Sstevel@tonic-gate 		ASSERT(!PP_ISFREE(repl));
27640Sstevel@tonic-gate 		targ[i] = repl;
27650Sstevel@tonic-gate 		page_downgrade(targ[i]);
2766414Skchow 		repl++;
27670Sstevel@tonic-gate 	}
27680Sstevel@tonic-gate }
27690Sstevel@tonic-gate 
27700Sstevel@tonic-gate /*
27710Sstevel@tonic-gate  * Check if all pages in ppa array are complete smaller than szc pages and
27720Sstevel@tonic-gate  * their roots will still be aligned relative to their current size if the
27730Sstevel@tonic-gate  * entire ppa array is relocated into one szc page. If these conditions are
27740Sstevel@tonic-gate  * not met return 0.
27750Sstevel@tonic-gate  *
27760Sstevel@tonic-gate  * If all pages are properly aligned attempt to upgrade their locks
27770Sstevel@tonic-gate  * to exclusive mode. If it fails set *upgrdfail to 1 and return 0.
27780Sstevel@tonic-gate  * upgrdfail was set to 0 by caller.
27790Sstevel@tonic-gate  *
27800Sstevel@tonic-gate  * Return 1 if all pages are aligned and locked exclusively.
27810Sstevel@tonic-gate  *
27820Sstevel@tonic-gate  * If all pages in ppa array happen to be physically contiguous to make one
27830Sstevel@tonic-gate  * szc page and all exclusive locks are successfully obtained promote the page
27840Sstevel@tonic-gate  * size to szc and set *pszc to szc. Return 1 with pages locked shared.
27850Sstevel@tonic-gate  */
27860Sstevel@tonic-gate static int
27870Sstevel@tonic-gate segvn_full_szcpages(page_t **ppa, uint_t szc, int *upgrdfail, uint_t *pszc)
27880Sstevel@tonic-gate {
27890Sstevel@tonic-gate 	page_t *pp;
27900Sstevel@tonic-gate 	pfn_t pfn;
27910Sstevel@tonic-gate 	pgcnt_t totnpgs = page_get_pagecnt(szc);
27920Sstevel@tonic-gate 	pfn_t first_pfn;
27930Sstevel@tonic-gate 	int contig = 1;
27940Sstevel@tonic-gate 	pgcnt_t i;
27950Sstevel@tonic-gate 	pgcnt_t j;
27960Sstevel@tonic-gate 	uint_t curszc;
27970Sstevel@tonic-gate 	pgcnt_t curnpgs;
27980Sstevel@tonic-gate 	int root = 0;
27990Sstevel@tonic-gate 
28000Sstevel@tonic-gate 	ASSERT(szc > 0);
28010Sstevel@tonic-gate 
28020Sstevel@tonic-gate 	VM_STAT_ADD(segvnvmstats.fullszcpages[0]);
28030Sstevel@tonic-gate 
28040Sstevel@tonic-gate 	for (i = 0; i < totnpgs; i++) {
28050Sstevel@tonic-gate 		pp = ppa[i];
28060Sstevel@tonic-gate 		ASSERT(PAGE_SHARED(pp));
28070Sstevel@tonic-gate 		ASSERT(!PP_ISFREE(pp));
28080Sstevel@tonic-gate 		pfn = page_pptonum(pp);
28090Sstevel@tonic-gate 		if (i == 0) {
28100Sstevel@tonic-gate 			if (!IS_P2ALIGNED(pfn, totnpgs)) {
28110Sstevel@tonic-gate 				contig = 0;
28120Sstevel@tonic-gate 			} else {
28130Sstevel@tonic-gate 				first_pfn = pfn;
28140Sstevel@tonic-gate 			}
28150Sstevel@tonic-gate 		} else if (contig && pfn != first_pfn + i) {
28160Sstevel@tonic-gate 			contig = 0;
28170Sstevel@tonic-gate 		}
28180Sstevel@tonic-gate 		if (pp->p_szc == 0) {
28190Sstevel@tonic-gate 			if (root) {
28200Sstevel@tonic-gate 				VM_STAT_ADD(segvnvmstats.fullszcpages[1]);
28210Sstevel@tonic-gate 				return (0);
28220Sstevel@tonic-gate 			}
28230Sstevel@tonic-gate 		} else if (!root) {
28240Sstevel@tonic-gate 			if ((curszc = pp->p_szc) >= szc) {
28250Sstevel@tonic-gate 				VM_STAT_ADD(segvnvmstats.fullszcpages[2]);
28260Sstevel@tonic-gate 				return (0);
28270Sstevel@tonic-gate 			}
28280Sstevel@tonic-gate 			if (curszc == 0) {
28290Sstevel@tonic-gate 				/*
28300Sstevel@tonic-gate 				 * p_szc changed means we don't have all pages
28310Sstevel@tonic-gate 				 * locked. return failure.
28320Sstevel@tonic-gate 				 */
28330Sstevel@tonic-gate 				VM_STAT_ADD(segvnvmstats.fullszcpages[3]);
28340Sstevel@tonic-gate 				return (0);
28350Sstevel@tonic-gate 			}
28360Sstevel@tonic-gate 			curnpgs = page_get_pagecnt(curszc);
28370Sstevel@tonic-gate 			if (!IS_P2ALIGNED(pfn, curnpgs) ||
28380Sstevel@tonic-gate 			    !IS_P2ALIGNED(i, curnpgs)) {
28390Sstevel@tonic-gate 				VM_STAT_ADD(segvnvmstats.fullszcpages[4]);
28400Sstevel@tonic-gate 				return (0);
28410Sstevel@tonic-gate 			}
28420Sstevel@tonic-gate 			root = 1;
28430Sstevel@tonic-gate 		} else {
28440Sstevel@tonic-gate 			ASSERT(i > 0);
28450Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fullszcpages[5]);
28460Sstevel@tonic-gate 			if (pp->p_szc != curszc) {
28470Sstevel@tonic-gate 				VM_STAT_ADD(segvnvmstats.fullszcpages[6]);
28480Sstevel@tonic-gate 				return (0);
28490Sstevel@tonic-gate 			}
28500Sstevel@tonic-gate 			if (pfn - 1 != page_pptonum(ppa[i - 1])) {
28510Sstevel@tonic-gate 				panic("segvn_full_szcpages: "
28520Sstevel@tonic-gate 				    "large page not physically contiguous");
28530Sstevel@tonic-gate 			}
28540Sstevel@tonic-gate 			if (P2PHASE(pfn, curnpgs) == curnpgs - 1) {
28550Sstevel@tonic-gate 				root = 0;
28560Sstevel@tonic-gate 			}
28570Sstevel@tonic-gate 		}
28580Sstevel@tonic-gate 	}
28590Sstevel@tonic-gate 
28600Sstevel@tonic-gate 	for (i = 0; i < totnpgs; i++) {
28610Sstevel@tonic-gate 		ASSERT(ppa[i]->p_szc < szc);
28620Sstevel@tonic-gate 		if (!page_tryupgrade(ppa[i])) {
28630Sstevel@tonic-gate 			for (j = 0; j < i; j++) {
28640Sstevel@tonic-gate 				page_downgrade(ppa[j]);
28650Sstevel@tonic-gate 			}
28660Sstevel@tonic-gate 			*pszc = ppa[i]->p_szc;
28670Sstevel@tonic-gate 			*upgrdfail = 1;
28680Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fullszcpages[7]);
28690Sstevel@tonic-gate 			return (0);
28700Sstevel@tonic-gate 		}
28710Sstevel@tonic-gate 	}
28720Sstevel@tonic-gate 
28730Sstevel@tonic-gate 	/*
28740Sstevel@tonic-gate 	 * When a page is put a free cachelist its szc is set to 0.  if file
28750Sstevel@tonic-gate 	 * system reclaimed pages from cachelist targ pages will be physically
28760Sstevel@tonic-gate 	 * contiguous with 0 p_szc.  in this case just upgrade szc of targ
28770Sstevel@tonic-gate 	 * pages without any relocations.
28780Sstevel@tonic-gate 	 * To avoid any hat issues with previous small mappings
28790Sstevel@tonic-gate 	 * hat_pageunload() the target pages first.
28800Sstevel@tonic-gate 	 */
28810Sstevel@tonic-gate 	if (contig) {
28820Sstevel@tonic-gate 		VM_STAT_ADD(segvnvmstats.fullszcpages[8]);
28830Sstevel@tonic-gate 		for (i = 0; i < totnpgs; i++) {
28840Sstevel@tonic-gate 			(void) hat_pageunload(ppa[i], HAT_FORCE_PGUNLOAD);
28850Sstevel@tonic-gate 		}
28860Sstevel@tonic-gate 		for (i = 0; i < totnpgs; i++) {
28870Sstevel@tonic-gate 			ppa[i]->p_szc = szc;
28880Sstevel@tonic-gate 		}
28890Sstevel@tonic-gate 		for (i = 0; i < totnpgs; i++) {
28900Sstevel@tonic-gate 			ASSERT(PAGE_EXCL(ppa[i]));
28910Sstevel@tonic-gate 			page_downgrade(ppa[i]);
28920Sstevel@tonic-gate 		}
28930Sstevel@tonic-gate 		if (pszc != NULL) {
28940Sstevel@tonic-gate 			*pszc = szc;
28950Sstevel@tonic-gate 		}
28960Sstevel@tonic-gate 	}
28970Sstevel@tonic-gate 	VM_STAT_ADD(segvnvmstats.fullszcpages[9]);
28980Sstevel@tonic-gate 	return (1);
28990Sstevel@tonic-gate }
29000Sstevel@tonic-gate 
29010Sstevel@tonic-gate /*
29020Sstevel@tonic-gate  * Create physically contiguous pages for [vp, off] - [vp, off +
29030Sstevel@tonic-gate  * page_size(szc)) range and for private segment return them in ppa array.
29040Sstevel@tonic-gate  * Pages are created either via IO or relocations.
29050Sstevel@tonic-gate  *
29060Sstevel@tonic-gate  * Return 1 on sucess and 0 on failure.
29070Sstevel@tonic-gate  *
29080Sstevel@tonic-gate  * If physically contiguos pages already exist for this range return 1 without
29090Sstevel@tonic-gate  * filling ppa array. Caller initializes ppa[0] as NULL to detect that ppa
29100Sstevel@tonic-gate  * array wasn't filled. In this case caller fills ppa array via VOP_GETPAGE().
29110Sstevel@tonic-gate  */
29120Sstevel@tonic-gate 
29130Sstevel@tonic-gate static int
29140Sstevel@tonic-gate segvn_fill_vp_pages(struct segvn_data *svd, vnode_t *vp, u_offset_t off,
29150Sstevel@tonic-gate     uint_t szc, page_t **ppa, page_t **ppplist, uint_t *ret_pszc,
29160Sstevel@tonic-gate     int *downsize)
29170Sstevel@tonic-gate 
29180Sstevel@tonic-gate {
29190Sstevel@tonic-gate 	page_t *pplist = *ppplist;
29200Sstevel@tonic-gate 	size_t pgsz = page_get_pagesize(szc);
29210Sstevel@tonic-gate 	pgcnt_t pages = btop(pgsz);
29220Sstevel@tonic-gate 	ulong_t start_off = off;
29230Sstevel@tonic-gate 	u_offset_t eoff = off + pgsz;
29240Sstevel@tonic-gate 	spgcnt_t nreloc;
29250Sstevel@tonic-gate 	u_offset_t io_off = off;
29260Sstevel@tonic-gate 	size_t io_len;
29270Sstevel@tonic-gate 	page_t *io_pplist = NULL;
29280Sstevel@tonic-gate 	page_t *done_pplist = NULL;
29290Sstevel@tonic-gate 	pgcnt_t pgidx = 0;
29300Sstevel@tonic-gate 	page_t *pp;
29310Sstevel@tonic-gate 	page_t *newpp;
29320Sstevel@tonic-gate 	page_t *targpp;
29330Sstevel@tonic-gate 	int io_err = 0;
29340Sstevel@tonic-gate 	int i;
29350Sstevel@tonic-gate 	pfn_t pfn;
29360Sstevel@tonic-gate 	ulong_t ppages;
29370Sstevel@tonic-gate 	page_t *targ_pplist = NULL;
29380Sstevel@tonic-gate 	page_t *repl_pplist = NULL;
29390Sstevel@tonic-gate 	page_t *tmp_pplist;
29400Sstevel@tonic-gate 	int nios = 0;
29410Sstevel@tonic-gate 	uint_t pszc;
29420Sstevel@tonic-gate 	struct vattr va;
29430Sstevel@tonic-gate 
29440Sstevel@tonic-gate 	VM_STAT_ADD(segvnvmstats.fill_vp_pages[0]);
29450Sstevel@tonic-gate 
29460Sstevel@tonic-gate 	ASSERT(szc != 0);
29470Sstevel@tonic-gate 	ASSERT(pplist->p_szc == szc);
29480Sstevel@tonic-gate 
29490Sstevel@tonic-gate 	/*
29500Sstevel@tonic-gate 	 * downsize will be set to 1 only if we fail to lock pages. this will
29510Sstevel@tonic-gate 	 * allow subsequent faults to try to relocate the page again. If we
29520Sstevel@tonic-gate 	 * fail due to misalignment don't downsize and let the caller map the
29530Sstevel@tonic-gate 	 * whole region with small mappings to avoid more faults into the area
29540Sstevel@tonic-gate 	 * where we can't get large pages anyway.
29550Sstevel@tonic-gate 	 */
29560Sstevel@tonic-gate 	*downsize = 0;
29570Sstevel@tonic-gate 
29580Sstevel@tonic-gate 	while (off < eoff) {
29590Sstevel@tonic-gate 		newpp = pplist;
29600Sstevel@tonic-gate 		ASSERT(newpp != NULL);
29610Sstevel@tonic-gate 		ASSERT(PAGE_EXCL(newpp));
29620Sstevel@tonic-gate 		ASSERT(!PP_ISFREE(newpp));
29630Sstevel@tonic-gate 		/*
29640Sstevel@tonic-gate 		 * we pass NULL for nrelocp to page_lookup_create()
29650Sstevel@tonic-gate 		 * so that it doesn't relocate. We relocate here
29660Sstevel@tonic-gate 		 * later only after we make sure we can lock all
29670Sstevel@tonic-gate 		 * pages in the range we handle and they are all
29680Sstevel@tonic-gate 		 * aligned.
29690Sstevel@tonic-gate 		 */
29700Sstevel@tonic-gate 		pp = page_lookup_create(vp, off, SE_SHARED, newpp, NULL, 0);
29710Sstevel@tonic-gate 		ASSERT(pp != NULL);
29720Sstevel@tonic-gate 		ASSERT(!PP_ISFREE(pp));
29730Sstevel@tonic-gate 		ASSERT(pp->p_vnode == vp);
29740Sstevel@tonic-gate 		ASSERT(pp->p_offset == off);
29750Sstevel@tonic-gate 		if (pp == newpp) {
29760Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fill_vp_pages[1]);
29770Sstevel@tonic-gate 			page_sub(&pplist, pp);
29780Sstevel@tonic-gate 			ASSERT(PAGE_EXCL(pp));
29790Sstevel@tonic-gate 			ASSERT(page_iolock_assert(pp));
29800Sstevel@tonic-gate 			page_list_concat(&io_pplist, &pp);
29810Sstevel@tonic-gate 			off += PAGESIZE;
29820Sstevel@tonic-gate 			continue;
29830Sstevel@tonic-gate 		}
29840Sstevel@tonic-gate 		VM_STAT_ADD(segvnvmstats.fill_vp_pages[2]);
29850Sstevel@tonic-gate 		pfn = page_pptonum(pp);
29860Sstevel@tonic-gate 		pszc = pp->p_szc;
29870Sstevel@tonic-gate 		if (pszc >= szc && targ_pplist == NULL && io_pplist == NULL &&
29880Sstevel@tonic-gate 		    IS_P2ALIGNED(pfn, pages)) {
29890Sstevel@tonic-gate 			ASSERT(repl_pplist == NULL);
29900Sstevel@tonic-gate 			ASSERT(done_pplist == NULL);
29910Sstevel@tonic-gate 			ASSERT(pplist == *ppplist);
29920Sstevel@tonic-gate 			page_unlock(pp);
29930Sstevel@tonic-gate 			page_free_replacement_page(pplist);
29940Sstevel@tonic-gate 			page_create_putback(pages);
29950Sstevel@tonic-gate 			*ppplist = NULL;
29960Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fill_vp_pages[3]);
29970Sstevel@tonic-gate 			return (1);
29980Sstevel@tonic-gate 		}
29990Sstevel@tonic-gate 		if (pszc >= szc) {
30000Sstevel@tonic-gate 			page_unlock(pp);
30010Sstevel@tonic-gate 			segvn_faultvnmpss_align_err1++;
30020Sstevel@tonic-gate 			goto out;
30030Sstevel@tonic-gate 		}
30040Sstevel@tonic-gate 		ppages = page_get_pagecnt(pszc);
30050Sstevel@tonic-gate 		if (!IS_P2ALIGNED(pfn, ppages)) {
30060Sstevel@tonic-gate 			ASSERT(pszc > 0);
30070Sstevel@tonic-gate 			/*
30080Sstevel@tonic-gate 			 * sizing down to pszc won't help.
30090Sstevel@tonic-gate 			 */
30100Sstevel@tonic-gate 			page_unlock(pp);
30110Sstevel@tonic-gate 			segvn_faultvnmpss_align_err2++;
30120Sstevel@tonic-gate 			goto out;
30130Sstevel@tonic-gate 		}
30140Sstevel@tonic-gate 		pfn = page_pptonum(newpp);
30150Sstevel@tonic-gate 		if (!IS_P2ALIGNED(pfn, ppages)) {
30160Sstevel@tonic-gate 			ASSERT(pszc > 0);
30170Sstevel@tonic-gate 			/*
30180Sstevel@tonic-gate 			 * sizing down to pszc won't help.
30190Sstevel@tonic-gate 			 */
30200Sstevel@tonic-gate 			page_unlock(pp);
30210Sstevel@tonic-gate 			segvn_faultvnmpss_align_err3++;
30220Sstevel@tonic-gate 			goto out;
30230Sstevel@tonic-gate 		}
30240Sstevel@tonic-gate 		if (!PAGE_EXCL(pp)) {
30250Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fill_vp_pages[4]);
30260Sstevel@tonic-gate 			page_unlock(pp);
30270Sstevel@tonic-gate 			*downsize = 1;
30280Sstevel@tonic-gate 			*ret_pszc = pp->p_szc;
30290Sstevel@tonic-gate 			goto out;
30300Sstevel@tonic-gate 		}
30310Sstevel@tonic-gate 		targpp = pp;
30320Sstevel@tonic-gate 		if (io_pplist != NULL) {
30330Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fill_vp_pages[5]);
30340Sstevel@tonic-gate 			io_len = off - io_off;
30350Sstevel@tonic-gate 			/*
30360Sstevel@tonic-gate 			 * Some file systems like NFS don't check EOF
30370Sstevel@tonic-gate 			 * conditions in VOP_PAGEIO(). Check it here
30380Sstevel@tonic-gate 			 * now that pages are locked SE_EXCL. Any file
30390Sstevel@tonic-gate 			 * truncation will wait until the pages are
30400Sstevel@tonic-gate 			 * unlocked so no need to worry that file will
30410Sstevel@tonic-gate 			 * be truncated after we check its size here.
30420Sstevel@tonic-gate 			 * XXX fix NFS to remove this check.
30430Sstevel@tonic-gate 			 */
30440Sstevel@tonic-gate 			va.va_mask = AT_SIZE;
30450Sstevel@tonic-gate 			if (VOP_GETATTR(vp, &va, ATTR_HINT, svd->cred) != 0) {
30460Sstevel@tonic-gate 				VM_STAT_ADD(segvnvmstats.fill_vp_pages[6]);
30470Sstevel@tonic-gate 				page_unlock(targpp);
30480Sstevel@tonic-gate 				goto out;
30490Sstevel@tonic-gate 			}
30500Sstevel@tonic-gate 			if (btopr(va.va_size) < btopr(io_off + io_len)) {
30510Sstevel@tonic-gate 				VM_STAT_ADD(segvnvmstats.fill_vp_pages[7]);
30520Sstevel@tonic-gate 				*downsize = 1;
30530Sstevel@tonic-gate 				*ret_pszc = 0;
30540Sstevel@tonic-gate 				page_unlock(targpp);
30550Sstevel@tonic-gate 				goto out;
30560Sstevel@tonic-gate 			}
30570Sstevel@tonic-gate 			io_err = VOP_PAGEIO(vp, io_pplist, io_off, io_len,
30580Sstevel@tonic-gate 				B_READ, svd->cred);
30590Sstevel@tonic-gate 			if (io_err) {
30600Sstevel@tonic-gate 				VM_STAT_ADD(segvnvmstats.fill_vp_pages[8]);
30610Sstevel@tonic-gate 				page_unlock(targpp);
30620Sstevel@tonic-gate 				if (io_err == EDEADLK) {
30630Sstevel@tonic-gate 					segvn_vmpss_pageio_deadlk_err++;
30640Sstevel@tonic-gate 				}
30650Sstevel@tonic-gate 				goto out;
30660Sstevel@tonic-gate 			}
30670Sstevel@tonic-gate 			nios++;
30680Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fill_vp_pages[9]);
30690Sstevel@tonic-gate 			while (io_pplist != NULL) {
30700Sstevel@tonic-gate 				pp = io_pplist;
30710Sstevel@tonic-gate 				page_sub(&io_pplist, pp);
30720Sstevel@tonic-gate 				ASSERT(page_iolock_assert(pp));
30730Sstevel@tonic-gate 				page_io_unlock(pp);
30740Sstevel@tonic-gate 				pgidx = (pp->p_offset - start_off) >>
30750Sstevel@tonic-gate 				    PAGESHIFT;
30760Sstevel@tonic-gate 				ASSERT(pgidx < pages);
30770Sstevel@tonic-gate 				ppa[pgidx] = pp;
30780Sstevel@tonic-gate 				page_list_concat(&done_pplist, &pp);
30790Sstevel@tonic-gate 			}
30800Sstevel@tonic-gate 		}
30810Sstevel@tonic-gate 		pp = targpp;
30820Sstevel@tonic-gate 		ASSERT(PAGE_EXCL(pp));
30830Sstevel@tonic-gate 		ASSERT(pp->p_szc <= pszc);
30840Sstevel@tonic-gate 		if (pszc != 0 && !group_page_trylock(pp, SE_EXCL)) {
30850Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fill_vp_pages[10]);
30860Sstevel@tonic-gate 			page_unlock(pp);
30870Sstevel@tonic-gate 			*downsize = 1;
30880Sstevel@tonic-gate 			*ret_pszc = pp->p_szc;
30890Sstevel@tonic-gate 			goto out;
30900Sstevel@tonic-gate 		}
30910Sstevel@tonic-gate 		VM_STAT_ADD(segvnvmstats.fill_vp_pages[11]);
30920Sstevel@tonic-gate 		/*
30930Sstevel@tonic-gate 		 * page szc chould have changed before the entire group was
30940Sstevel@tonic-gate 		 * locked. reread page szc.
30950Sstevel@tonic-gate 		 */
30960Sstevel@tonic-gate 		pszc = pp->p_szc;
30970Sstevel@tonic-gate 		ppages = page_get_pagecnt(pszc);
30980Sstevel@tonic-gate 
30990Sstevel@tonic-gate 		/* link just the roots */
31000Sstevel@tonic-gate 		page_list_concat(&targ_pplist, &pp);
31010Sstevel@tonic-gate 		page_sub(&pplist, newpp);
31020Sstevel@tonic-gate 		page_list_concat(&repl_pplist, &newpp);
31030Sstevel@tonic-gate 		off += PAGESIZE;
31040Sstevel@tonic-gate 		while (--ppages != 0) {
31050Sstevel@tonic-gate 			newpp = pplist;
31060Sstevel@tonic-gate 			page_sub(&pplist, newpp);
31070Sstevel@tonic-gate 			off += PAGESIZE;
31080Sstevel@tonic-gate 		}
31090Sstevel@tonic-gate 		io_off = off;
31100Sstevel@tonic-gate 	}
31110Sstevel@tonic-gate 	if (io_pplist != NULL) {
31120Sstevel@tonic-gate 		VM_STAT_ADD(segvnvmstats.fill_vp_pages[12]);
31130Sstevel@tonic-gate 		io_len = eoff - io_off;
31140Sstevel@tonic-gate 		va.va_mask = AT_SIZE;
31150Sstevel@tonic-gate 		if (VOP_GETATTR(vp, &va, ATTR_HINT, svd->cred) != 0) {
31160Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fill_vp_pages[13]);
31170Sstevel@tonic-gate 			goto out;
31180Sstevel@tonic-gate 		}
31190Sstevel@tonic-gate 		if (btopr(va.va_size) < btopr(io_off + io_len)) {
31200Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fill_vp_pages[14]);
31210Sstevel@tonic-gate 			*downsize = 1;
31220Sstevel@tonic-gate 			*ret_pszc = 0;
31230Sstevel@tonic-gate 			goto out;
31240Sstevel@tonic-gate 		}
31250Sstevel@tonic-gate 		io_err = VOP_PAGEIO(vp, io_pplist, io_off, io_len,
31260Sstevel@tonic-gate 		    B_READ, svd->cred);
31270Sstevel@tonic-gate 		if (io_err) {
31280Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fill_vp_pages[15]);
31290Sstevel@tonic-gate 			if (io_err == EDEADLK) {
31300Sstevel@tonic-gate 				segvn_vmpss_pageio_deadlk_err++;
31310Sstevel@tonic-gate 			}
31320Sstevel@tonic-gate 			goto out;
31330Sstevel@tonic-gate 		}
31340Sstevel@tonic-gate 		nios++;
31350Sstevel@tonic-gate 		while (io_pplist != NULL) {
31360Sstevel@tonic-gate 			pp = io_pplist;
31370Sstevel@tonic-gate 			page_sub(&io_pplist, pp);
31380Sstevel@tonic-gate 			ASSERT(page_iolock_assert(pp));
31390Sstevel@tonic-gate 			page_io_unlock(pp);
31400Sstevel@tonic-gate 			pgidx = (pp->p_offset - start_off) >> PAGESHIFT;
31410Sstevel@tonic-gate 			ASSERT(pgidx < pages);
31420Sstevel@tonic-gate 			ppa[pgidx] = pp;
31430Sstevel@tonic-gate 		}
31440Sstevel@tonic-gate 	}
31450Sstevel@tonic-gate 	/*
31460Sstevel@tonic-gate 	 * we're now bound to succeed or panic.
31470Sstevel@tonic-gate 	 * remove pages from done_pplist. it's not needed anymore.
31480Sstevel@tonic-gate 	 */
31490Sstevel@tonic-gate 	while (done_pplist != NULL) {
31500Sstevel@tonic-gate 		pp = done_pplist;
31510Sstevel@tonic-gate 		page_sub(&done_pplist, pp);
31520Sstevel@tonic-gate 	}
31530Sstevel@tonic-gate 	VM_STAT_ADD(segvnvmstats.fill_vp_pages[16]);
31540Sstevel@tonic-gate 	ASSERT(pplist == NULL);
31550Sstevel@tonic-gate 	*ppplist = NULL;
31560Sstevel@tonic-gate 	while (targ_pplist != NULL) {
31570Sstevel@tonic-gate 		int ret;
31580Sstevel@tonic-gate 		VM_STAT_ADD(segvnvmstats.fill_vp_pages[17]);
31590Sstevel@tonic-gate 		ASSERT(repl_pplist);
31600Sstevel@tonic-gate 		pp = targ_pplist;
31610Sstevel@tonic-gate 		page_sub(&targ_pplist, pp);
31620Sstevel@tonic-gate 		pgidx = (pp->p_offset - start_off) >> PAGESHIFT;
31630Sstevel@tonic-gate 		newpp = repl_pplist;
31640Sstevel@tonic-gate 		page_sub(&repl_pplist, newpp);
31650Sstevel@tonic-gate #ifdef DEBUG
31660Sstevel@tonic-gate 		pfn = page_pptonum(pp);
31670Sstevel@tonic-gate 		pszc = pp->p_szc;
31680Sstevel@tonic-gate 		ppages = page_get_pagecnt(pszc);
31690Sstevel@tonic-gate 		ASSERT(IS_P2ALIGNED(pfn, ppages));
31700Sstevel@tonic-gate 		pfn = page_pptonum(newpp);
31710Sstevel@tonic-gate 		ASSERT(IS_P2ALIGNED(pfn, ppages));
31720Sstevel@tonic-gate 		ASSERT(P2PHASE(pfn, pages) == pgidx);
31730Sstevel@tonic-gate #endif
31740Sstevel@tonic-gate 		nreloc = 0;
31750Sstevel@tonic-gate 		ret = page_relocate(&pp, &newpp, 0, 1, &nreloc, NULL);
31760Sstevel@tonic-gate 		if (ret != 0 || nreloc == 0) {
31770Sstevel@tonic-gate 			panic("segvn_fill_vp_pages: "
31780Sstevel@tonic-gate 			    "page_relocate failed");
31790Sstevel@tonic-gate 		}
31800Sstevel@tonic-gate 		pp = newpp;
31810Sstevel@tonic-gate 		while (nreloc-- != 0) {
31820Sstevel@tonic-gate 			ASSERT(PAGE_EXCL(pp));
31830Sstevel@tonic-gate 			ASSERT(pp->p_vnode == vp);
31840Sstevel@tonic-gate 			ASSERT(pgidx ==
31850Sstevel@tonic-gate 			    ((pp->p_offset - start_off) >> PAGESHIFT));
31860Sstevel@tonic-gate 			ppa[pgidx++] = pp;
3187414Skchow 			pp++;
31880Sstevel@tonic-gate 		}
31890Sstevel@tonic-gate 	}
31900Sstevel@tonic-gate 
31910Sstevel@tonic-gate 	if (svd->type == MAP_PRIVATE) {
31920Sstevel@tonic-gate 		VM_STAT_ADD(segvnvmstats.fill_vp_pages[18]);
31930Sstevel@tonic-gate 		for (i = 0; i < pages; i++) {
31940Sstevel@tonic-gate 			ASSERT(ppa[i] != NULL);
31950Sstevel@tonic-gate 			ASSERT(PAGE_EXCL(ppa[i]));
31960Sstevel@tonic-gate 			ASSERT(ppa[i]->p_vnode == vp);
31970Sstevel@tonic-gate 			ASSERT(ppa[i]->p_offset ==
31980Sstevel@tonic-gate 			    start_off + (i << PAGESHIFT));
31990Sstevel@tonic-gate 			page_downgrade(ppa[i]);
32000Sstevel@tonic-gate 		}
32010Sstevel@tonic-gate 		ppa[pages] = NULL;
32020Sstevel@tonic-gate 	} else {
32030Sstevel@tonic-gate 		VM_STAT_ADD(segvnvmstats.fill_vp_pages[19]);
32040Sstevel@tonic-gate 		/*
32050Sstevel@tonic-gate 		 * the caller will still call VOP_GETPAGE() for shared segments
32060Sstevel@tonic-gate 		 * to check FS write permissions. For private segments we map
32070Sstevel@tonic-gate 		 * file read only anyway.  so no VOP_GETPAGE is needed.
32080Sstevel@tonic-gate 		 */
32090Sstevel@tonic-gate 		for (i = 0; i < pages; i++) {
32100Sstevel@tonic-gate 			ASSERT(ppa[i] != NULL);
32110Sstevel@tonic-gate 			ASSERT(PAGE_EXCL(ppa[i]));
32120Sstevel@tonic-gate 			ASSERT(ppa[i]->p_vnode == vp);
32130Sstevel@tonic-gate 			ASSERT(ppa[i]->p_offset ==
32140Sstevel@tonic-gate 			    start_off + (i << PAGESHIFT));
32150Sstevel@tonic-gate 			page_unlock(ppa[i]);
32160Sstevel@tonic-gate 		}
32170Sstevel@tonic-gate 		ppa[0] = NULL;
32180Sstevel@tonic-gate 	}
32190Sstevel@tonic-gate 
32200Sstevel@tonic-gate 	return (1);
32210Sstevel@tonic-gate out:
32220Sstevel@tonic-gate 	/*
32230Sstevel@tonic-gate 	 * Do the cleanup. Unlock target pages we didn't relocate. They are
32240Sstevel@tonic-gate 	 * linked on targ_pplist by root pages. reassemble unused replacement
32250Sstevel@tonic-gate 	 * and io pages back to pplist.
32260Sstevel@tonic-gate 	 */
32270Sstevel@tonic-gate 	if (io_pplist != NULL) {
32280Sstevel@tonic-gate 		VM_STAT_ADD(segvnvmstats.fill_vp_pages[20]);
32290Sstevel@tonic-gate 		pp = io_pplist;
32300Sstevel@tonic-gate 		do {
32310Sstevel@tonic-gate 			ASSERT(pp->p_vnode == vp);
32320Sstevel@tonic-gate 			ASSERT(pp->p_offset == io_off);
32330Sstevel@tonic-gate 			ASSERT(page_iolock_assert(pp));
32340Sstevel@tonic-gate 			page_io_unlock(pp);
32350Sstevel@tonic-gate 			page_hashout(pp, NULL);
32360Sstevel@tonic-gate 			io_off += PAGESIZE;
32370Sstevel@tonic-gate 		} while ((pp = pp->p_next) != io_pplist);
32380Sstevel@tonic-gate 		page_list_concat(&io_pplist, &pplist);
32390Sstevel@tonic-gate 		pplist = io_pplist;
32400Sstevel@tonic-gate 	}
32410Sstevel@tonic-gate 	tmp_pplist = NULL;
32420Sstevel@tonic-gate 	while (targ_pplist != NULL) {
32430Sstevel@tonic-gate 		VM_STAT_ADD(segvnvmstats.fill_vp_pages[21]);
32440Sstevel@tonic-gate 		pp = targ_pplist;
32450Sstevel@tonic-gate 		ASSERT(PAGE_EXCL(pp));
32460Sstevel@tonic-gate 		page_sub(&targ_pplist, pp);
32470Sstevel@tonic-gate 
32480Sstevel@tonic-gate 		pszc = pp->p_szc;
32490Sstevel@tonic-gate 		ppages = page_get_pagecnt(pszc);
32500Sstevel@tonic-gate 		ASSERT(IS_P2ALIGNED(page_pptonum(pp), ppages));
32510Sstevel@tonic-gate 
32520Sstevel@tonic-gate 		if (pszc != 0) {
32530Sstevel@tonic-gate 			group_page_unlock(pp);
32540Sstevel@tonic-gate 		}
32550Sstevel@tonic-gate 		page_unlock(pp);
32560Sstevel@tonic-gate 
32570Sstevel@tonic-gate 		pp = repl_pplist;
32580Sstevel@tonic-gate 		ASSERT(pp != NULL);
32590Sstevel@tonic-gate 		ASSERT(PAGE_EXCL(pp));
32600Sstevel@tonic-gate 		ASSERT(pp->p_szc == szc);
32610Sstevel@tonic-gate 		page_sub(&repl_pplist, pp);
32620Sstevel@tonic-gate 
32630Sstevel@tonic-gate 		ASSERT(IS_P2ALIGNED(page_pptonum(pp), ppages));
32640Sstevel@tonic-gate 
32650Sstevel@tonic-gate 		/* relink replacement page */
32660Sstevel@tonic-gate 		page_list_concat(&tmp_pplist, &pp);
32670Sstevel@tonic-gate 		while (--ppages != 0) {
32680Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fill_vp_pages[22]);
3269414Skchow 			pp++;
32700Sstevel@tonic-gate 			ASSERT(PAGE_EXCL(pp));
32710Sstevel@tonic-gate 			ASSERT(pp->p_szc == szc);
32720Sstevel@tonic-gate 			page_list_concat(&tmp_pplist, &pp);
32730Sstevel@tonic-gate 		}
32740Sstevel@tonic-gate 	}
32750Sstevel@tonic-gate 	if (tmp_pplist != NULL) {
32760Sstevel@tonic-gate 		VM_STAT_ADD(segvnvmstats.fill_vp_pages[23]);
32770Sstevel@tonic-gate 		page_list_concat(&tmp_pplist, &pplist);
32780Sstevel@tonic-gate 		pplist = tmp_pplist;
32790Sstevel@tonic-gate 	}
32800Sstevel@tonic-gate 	/*
32810Sstevel@tonic-gate 	 * at this point all pages are either on done_pplist or
32820Sstevel@tonic-gate 	 * pplist. They can't be all on done_pplist otherwise
32830Sstevel@tonic-gate 	 * we'd've been done.
32840Sstevel@tonic-gate 	 */
32850Sstevel@tonic-gate 	ASSERT(pplist != NULL);
32860Sstevel@tonic-gate 	if (nios != 0) {
32870Sstevel@tonic-gate 		VM_STAT_ADD(segvnvmstats.fill_vp_pages[24]);
32880Sstevel@tonic-gate 		pp = pplist;
32890Sstevel@tonic-gate 		do {
32900Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fill_vp_pages[25]);
32910Sstevel@tonic-gate 			ASSERT(pp->p_szc == szc);
32920Sstevel@tonic-gate 			ASSERT(PAGE_EXCL(pp));
32930Sstevel@tonic-gate 			ASSERT(pp->p_vnode != vp);
32940Sstevel@tonic-gate 			pp->p_szc = 0;
32950Sstevel@tonic-gate 		} while ((pp = pp->p_next) != pplist);
32960Sstevel@tonic-gate 
32970Sstevel@tonic-gate 		pp = done_pplist;
32980Sstevel@tonic-gate 		do {
32990Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fill_vp_pages[26]);
33000Sstevel@tonic-gate 			ASSERT(pp->p_szc == szc);
33010Sstevel@tonic-gate 			ASSERT(PAGE_EXCL(pp));
33020Sstevel@tonic-gate 			ASSERT(pp->p_vnode == vp);
33030Sstevel@tonic-gate 			pp->p_szc = 0;
33040Sstevel@tonic-gate 		} while ((pp = pp->p_next) != done_pplist);
33050Sstevel@tonic-gate 
33060Sstevel@tonic-gate 		while (pplist != NULL) {
33070Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fill_vp_pages[27]);
33080Sstevel@tonic-gate 			pp = pplist;
33090Sstevel@tonic-gate 			page_sub(&pplist, pp);
33100Sstevel@tonic-gate 			page_free(pp, 0);
33110Sstevel@tonic-gate 		}
33120Sstevel@tonic-gate 
33130Sstevel@tonic-gate 		while (done_pplist != NULL) {
33140Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fill_vp_pages[28]);
33150Sstevel@tonic-gate 			pp = done_pplist;
33160Sstevel@tonic-gate 			page_sub(&done_pplist, pp);
33170Sstevel@tonic-gate 			page_unlock(pp);
33180Sstevel@tonic-gate 		}
33190Sstevel@tonic-gate 		*ppplist = NULL;
33200Sstevel@tonic-gate 		return (0);
33210Sstevel@tonic-gate 	}
33220Sstevel@tonic-gate 	ASSERT(pplist == *ppplist);
33230Sstevel@tonic-gate 	if (io_err) {
33240Sstevel@tonic-gate 		VM_STAT_ADD(segvnvmstats.fill_vp_pages[29]);
33250Sstevel@tonic-gate 		/*
33260Sstevel@tonic-gate 		 * don't downsize on io error.
33270Sstevel@tonic-gate 		 * see if vop_getpage succeeds.
33280Sstevel@tonic-gate 		 * pplist may still be used in this case
33290Sstevel@tonic-gate 		 * for relocations.
33300Sstevel@tonic-gate 		 */
33310Sstevel@tonic-gate 		return (0);
33320Sstevel@tonic-gate 	}
33330Sstevel@tonic-gate 	VM_STAT_ADD(segvnvmstats.fill_vp_pages[30]);
33340Sstevel@tonic-gate 	page_free_replacement_page(pplist);
33350Sstevel@tonic-gate 	page_create_putback(pages);
33360Sstevel@tonic-gate 	*ppplist = NULL;
33370Sstevel@tonic-gate 	return (0);
33380Sstevel@tonic-gate }
33390Sstevel@tonic-gate 
33400Sstevel@tonic-gate int segvn_anypgsz = 0;
33410Sstevel@tonic-gate 
33420Sstevel@tonic-gate #define	SEGVN_RESTORE_SOFTLOCK(type, pages) 		\
33430Sstevel@tonic-gate 		if ((type) == F_SOFTLOCK) {		\
33440Sstevel@tonic-gate 			mutex_enter(&freemem_lock);	\
33450Sstevel@tonic-gate 			availrmem += (pages);		\
33460Sstevel@tonic-gate 			segvn_pages_locked -= (pages);	\
33470Sstevel@tonic-gate 			svd->softlockcnt -= (pages);	\
33480Sstevel@tonic-gate 			mutex_exit(&freemem_lock);	\
33490Sstevel@tonic-gate 		}
33500Sstevel@tonic-gate 
33510Sstevel@tonic-gate #define	SEGVN_UPDATE_MODBITS(ppa, pages, rw, prot, vpprot)		\
33520Sstevel@tonic-gate 		if (IS_VMODSORT((ppa)[0]->p_vnode)) {			\
33530Sstevel@tonic-gate 			if ((rw) == S_WRITE) {				\
33540Sstevel@tonic-gate 				for (i = 0; i < (pages); i++) {		\
33550Sstevel@tonic-gate 					ASSERT((ppa)[i]->p_vnode ==	\
33560Sstevel@tonic-gate 					    (ppa)[0]->p_vnode);		\
33570Sstevel@tonic-gate 					hat_setmod((ppa)[i]);		\
33580Sstevel@tonic-gate 				}					\
33590Sstevel@tonic-gate 			} else if ((rw) != S_OTHER &&			\
33600Sstevel@tonic-gate 			    ((prot) & (vpprot) & PROT_WRITE)) {		\
33610Sstevel@tonic-gate 				for (i = 0; i < (pages); i++) {		\
33620Sstevel@tonic-gate 					ASSERT((ppa)[i]->p_vnode ==	\
33630Sstevel@tonic-gate 					    (ppa)[0]->p_vnode);		\
33640Sstevel@tonic-gate 					if (!hat_ismod((ppa)[i])) {	\
33650Sstevel@tonic-gate 						prot &= ~PROT_WRITE;	\
33660Sstevel@tonic-gate 						break;			\
33670Sstevel@tonic-gate 					}				\
33680Sstevel@tonic-gate 				}					\
33690Sstevel@tonic-gate 			}						\
33700Sstevel@tonic-gate 		}
33710Sstevel@tonic-gate 
33720Sstevel@tonic-gate #ifdef  VM_STATS
33730Sstevel@tonic-gate 
33740Sstevel@tonic-gate #define	SEGVN_VMSTAT_FLTVNPAGES(idx)					\
33750Sstevel@tonic-gate 		VM_STAT_ADD(segvnvmstats.fltvnpages[(idx)]);
33760Sstevel@tonic-gate 
33770Sstevel@tonic-gate #else /* VM_STATS */
33780Sstevel@tonic-gate 
33790Sstevel@tonic-gate #define	SEGVN_VMSTAT_FLTVNPAGES(idx)
33800Sstevel@tonic-gate 
33810Sstevel@tonic-gate #endif
33820Sstevel@tonic-gate 
33830Sstevel@tonic-gate static faultcode_t
33840Sstevel@tonic-gate segvn_fault_vnodepages(struct hat *hat, struct seg *seg, caddr_t lpgaddr,
33850Sstevel@tonic-gate     caddr_t lpgeaddr, enum fault_type type, enum seg_rw rw, caddr_t addr,
33860Sstevel@tonic-gate     caddr_t eaddr, int brkcow)
33870Sstevel@tonic-gate {
33880Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
33890Sstevel@tonic-gate 	struct anon_map *amp = svd->amp;
33900Sstevel@tonic-gate 	uchar_t segtype = svd->type;
33910Sstevel@tonic-gate 	uint_t szc = seg->s_szc;
33920Sstevel@tonic-gate 	size_t pgsz = page_get_pagesize(szc);
33930Sstevel@tonic-gate 	size_t maxpgsz = pgsz;
33940Sstevel@tonic-gate 	pgcnt_t pages = btop(pgsz);
33950Sstevel@tonic-gate 	pgcnt_t maxpages = pages;
33960Sstevel@tonic-gate 	size_t ppasize = (pages + 1) * sizeof (page_t *);
33970Sstevel@tonic-gate 	caddr_t a = lpgaddr;
33980Sstevel@tonic-gate 	caddr_t	maxlpgeaddr = lpgeaddr;
33990Sstevel@tonic-gate 	u_offset_t off = svd->offset + (uintptr_t)(a - seg->s_base);
34000Sstevel@tonic-gate 	ulong_t aindx = svd->anon_index + seg_page(seg, a);
34010Sstevel@tonic-gate 	struct vpage *vpage = (svd->vpage != NULL) ?
34020Sstevel@tonic-gate 	    &svd->vpage[seg_page(seg, a)] : NULL;
34030Sstevel@tonic-gate 	vnode_t *vp = svd->vp;
34040Sstevel@tonic-gate 	page_t **ppa;
34050Sstevel@tonic-gate 	uint_t	pszc;
34060Sstevel@tonic-gate 	size_t	ppgsz;
34070Sstevel@tonic-gate 	pgcnt_t	ppages;
34080Sstevel@tonic-gate 	faultcode_t err = 0;
34090Sstevel@tonic-gate 	int ierr;
34100Sstevel@tonic-gate 	int vop_size_err = 0;
34110Sstevel@tonic-gate 	uint_t protchk, prot, vpprot;
34120Sstevel@tonic-gate 	ulong_t i;
34130Sstevel@tonic-gate 	int hat_flag = (type == F_SOFTLOCK) ? HAT_LOAD_LOCK : HAT_LOAD;
34140Sstevel@tonic-gate 	anon_sync_obj_t an_cookie;
34150Sstevel@tonic-gate 	enum seg_rw arw;
34160Sstevel@tonic-gate 	int alloc_failed = 0;
34170Sstevel@tonic-gate 	int adjszc_chk;
34180Sstevel@tonic-gate 	struct vattr va;
34190Sstevel@tonic-gate 	int xhat = 0;
34200Sstevel@tonic-gate 	page_t *pplist;
34210Sstevel@tonic-gate 	pfn_t pfn;
34220Sstevel@tonic-gate 	int physcontig;
34230Sstevel@tonic-gate 	int upgrdfail;
34240Sstevel@tonic-gate 	int segvn_anypgsz_vnode = 0; /* for now map vnode with 2 page sizes */
34250Sstevel@tonic-gate 
34260Sstevel@tonic-gate 	ASSERT(szc != 0);
34270Sstevel@tonic-gate 	ASSERT(vp != NULL);
34280Sstevel@tonic-gate 	ASSERT(brkcow == 0 || amp != NULL);
34290Sstevel@tonic-gate 	ASSERT(enable_mbit_wa == 0); /* no mbit simulations with large pages */
34300Sstevel@tonic-gate 	ASSERT(!(svd->flags & MAP_NORESERVE));
34310Sstevel@tonic-gate 	ASSERT(type != F_SOFTUNLOCK);
34320Sstevel@tonic-gate 	ASSERT(IS_P2ALIGNED(a, maxpgsz));
34330Sstevel@tonic-gate 	ASSERT(amp == NULL || IS_P2ALIGNED(aindx, maxpages));
34340Sstevel@tonic-gate 	ASSERT(SEGVN_LOCK_HELD(seg->s_as, &svd->lock));
34350Sstevel@tonic-gate 	ASSERT(seg->s_szc < NBBY * sizeof (int));
343663Saguzovsk 	ASSERT(type != F_SOFTLOCK || lpgeaddr - a == maxpgsz);
34370Sstevel@tonic-gate 
34380Sstevel@tonic-gate 	VM_STAT_COND_ADD(type == F_SOFTLOCK, segvnvmstats.fltvnpages[0]);
34390Sstevel@tonic-gate 	VM_STAT_COND_ADD(type != F_SOFTLOCK, segvnvmstats.fltvnpages[1]);
34400Sstevel@tonic-gate 
34410Sstevel@tonic-gate 	if (svd->flags & MAP_TEXT) {
34420Sstevel@tonic-gate 		hat_flag |= HAT_LOAD_TEXT;
34430Sstevel@tonic-gate 	}
34440Sstevel@tonic-gate 
34450Sstevel@tonic-gate 	if (svd->pageprot) {
34460Sstevel@tonic-gate 		switch (rw) {
34470Sstevel@tonic-gate 		case S_READ:
34480Sstevel@tonic-gate 			protchk = PROT_READ;
34490Sstevel@tonic-gate 			break;
34500Sstevel@tonic-gate 		case S_WRITE:
34510Sstevel@tonic-gate 			protchk = PROT_WRITE;
34520Sstevel@tonic-gate 			break;
34530Sstevel@tonic-gate 		case S_EXEC:
34540Sstevel@tonic-gate 			protchk = PROT_EXEC;
34550Sstevel@tonic-gate 			break;
34560Sstevel@tonic-gate 		case S_OTHER:
34570Sstevel@tonic-gate 		default:
34580Sstevel@tonic-gate 			protchk = PROT_READ | PROT_WRITE | PROT_EXEC;
34590Sstevel@tonic-gate 			break;
34600Sstevel@tonic-gate 		}
34610Sstevel@tonic-gate 	} else {
34620Sstevel@tonic-gate 		prot = svd->prot;
34630Sstevel@tonic-gate 		/* caller has already done segment level protection check. */
34640Sstevel@tonic-gate 	}
34650Sstevel@tonic-gate 
34660Sstevel@tonic-gate 	if (seg->s_as->a_hat != hat) {
34670Sstevel@tonic-gate 		xhat = 1;
34680Sstevel@tonic-gate 	}
34690Sstevel@tonic-gate 
34700Sstevel@tonic-gate 	if (rw == S_WRITE && segtype == MAP_PRIVATE) {
34710Sstevel@tonic-gate 		SEGVN_VMSTAT_FLTVNPAGES(2);
34720Sstevel@tonic-gate 		arw = S_READ;
34730Sstevel@tonic-gate 	} else {
34740Sstevel@tonic-gate 		arw = rw;
34750Sstevel@tonic-gate 	}
34760Sstevel@tonic-gate 
34770Sstevel@tonic-gate 	ppa = kmem_alloc(ppasize, KM_SLEEP);
34780Sstevel@tonic-gate 
34790Sstevel@tonic-gate 	VM_STAT_COND_ADD(amp != NULL, segvnvmstats.fltvnpages[3]);
34800Sstevel@tonic-gate 
34810Sstevel@tonic-gate 	for (;;) {
34820Sstevel@tonic-gate 		adjszc_chk = 0;
34830Sstevel@tonic-gate 		for (; a < lpgeaddr; a += pgsz, off += pgsz, aindx += pages) {
34840Sstevel@tonic-gate 			if (adjszc_chk) {
34850Sstevel@tonic-gate 				while (szc < seg->s_szc) {
34860Sstevel@tonic-gate 					uintptr_t e;
34870Sstevel@tonic-gate 					uint_t tszc;
34880Sstevel@tonic-gate 					tszc = segvn_anypgsz_vnode ? szc + 1 :
34890Sstevel@tonic-gate 					    seg->s_szc;
34900Sstevel@tonic-gate 					ppgsz = page_get_pagesize(tszc);
34910Sstevel@tonic-gate 					if (!IS_P2ALIGNED(a, ppgsz) ||
34920Sstevel@tonic-gate 					    ((alloc_failed >> tszc) &
34930Sstevel@tonic-gate 						0x1)) {
34940Sstevel@tonic-gate 						break;
34950Sstevel@tonic-gate 					}
34960Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(4);
34970Sstevel@tonic-gate 					szc = tszc;
34980Sstevel@tonic-gate 					pgsz = ppgsz;
34990Sstevel@tonic-gate 					pages = btop(pgsz);
35000Sstevel@tonic-gate 					e = P2ROUNDUP((uintptr_t)eaddr, pgsz);
35010Sstevel@tonic-gate 					lpgeaddr = (caddr_t)e;
35020Sstevel@tonic-gate 				}
35030Sstevel@tonic-gate 			}
35040Sstevel@tonic-gate 
35050Sstevel@tonic-gate 		again:
35060Sstevel@tonic-gate 			if (IS_P2ALIGNED(a, maxpgsz) && amp != NULL) {
35070Sstevel@tonic-gate 				ASSERT(IS_P2ALIGNED(aindx, maxpages));
35080Sstevel@tonic-gate 				ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
35090Sstevel@tonic-gate 				anon_array_enter(amp, aindx, &an_cookie);
35100Sstevel@tonic-gate 				if (anon_get_ptr(amp->ahp, aindx) != NULL) {
35110Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(5);
35120Sstevel@tonic-gate 					if (anon_pages(amp->ahp, aindx,
35130Sstevel@tonic-gate 					    maxpages) != maxpages) {
35140Sstevel@tonic-gate 						panic("segvn_fault_vnodepages:"
35150Sstevel@tonic-gate 						    " empty anon slots\n");
35160Sstevel@tonic-gate 					}
35170Sstevel@tonic-gate 					anon_array_exit(&an_cookie);
35180Sstevel@tonic-gate 					ANON_LOCK_EXIT(&amp->a_rwlock);
35190Sstevel@tonic-gate 					err = segvn_fault_anonpages(hat, seg,
35200Sstevel@tonic-gate 					    a, a + maxpgsz, type, rw,
35210Sstevel@tonic-gate 					    MAX(a, addr),
35220Sstevel@tonic-gate 					    MIN(a + maxpgsz, eaddr), brkcow);
35230Sstevel@tonic-gate 					if (err != 0) {
35240Sstevel@tonic-gate 						SEGVN_VMSTAT_FLTVNPAGES(6);
35250Sstevel@tonic-gate 						goto out;
35260Sstevel@tonic-gate 					}
35270Sstevel@tonic-gate 					if (szc < seg->s_szc) {
35280Sstevel@tonic-gate 						szc = seg->s_szc;
35290Sstevel@tonic-gate 						pgsz = maxpgsz;
35300Sstevel@tonic-gate 						pages = maxpages;
35310Sstevel@tonic-gate 						lpgeaddr = maxlpgeaddr;
35320Sstevel@tonic-gate 					}
35330Sstevel@tonic-gate 					goto next;
35340Sstevel@tonic-gate 				} else if (anon_pages(amp->ahp, aindx,
35350Sstevel@tonic-gate 				    maxpages)) {
35360Sstevel@tonic-gate 					panic("segvn_fault_vnodepages:"
35370Sstevel@tonic-gate 						" non empty anon slots\n");
35380Sstevel@tonic-gate 				} else {
35390Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(7);
35400Sstevel@tonic-gate 					anon_array_exit(&an_cookie);
35410Sstevel@tonic-gate 					ANON_LOCK_EXIT(&amp->a_rwlock);
35420Sstevel@tonic-gate 				}
35430Sstevel@tonic-gate 			}
35440Sstevel@tonic-gate 			ASSERT(!brkcow || IS_P2ALIGNED(a, maxpgsz));
35450Sstevel@tonic-gate 
35460Sstevel@tonic-gate 			if (svd->pageprot != 0 && IS_P2ALIGNED(a, maxpgsz)) {
35470Sstevel@tonic-gate 				ASSERT(vpage != NULL);
35480Sstevel@tonic-gate 				prot = VPP_PROT(vpage);
35490Sstevel@tonic-gate 				ASSERT(sameprot(seg, a, maxpgsz));
35500Sstevel@tonic-gate 				if ((prot & protchk) == 0) {
35510Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(8);
35520Sstevel@tonic-gate 					err = FC_PROT;
35530Sstevel@tonic-gate 					goto out;
35540Sstevel@tonic-gate 				}
35550Sstevel@tonic-gate 			}
35560Sstevel@tonic-gate 			if (type == F_SOFTLOCK) {
35570Sstevel@tonic-gate 				mutex_enter(&freemem_lock);
35580Sstevel@tonic-gate 				if (availrmem < tune.t_minarmem + pages) {
35590Sstevel@tonic-gate 					mutex_exit(&freemem_lock);
35600Sstevel@tonic-gate 					err = FC_MAKE_ERR(ENOMEM);
35610Sstevel@tonic-gate 					goto out;
35620Sstevel@tonic-gate 				} else {
35630Sstevel@tonic-gate 					availrmem -= pages;
35640Sstevel@tonic-gate 					segvn_pages_locked += pages;
35650Sstevel@tonic-gate 					svd->softlockcnt += pages;
35660Sstevel@tonic-gate 				}
35670Sstevel@tonic-gate 				mutex_exit(&freemem_lock);
35680Sstevel@tonic-gate 			}
35690Sstevel@tonic-gate 
35700Sstevel@tonic-gate 			pplist = NULL;
35710Sstevel@tonic-gate 			physcontig = 0;
35720Sstevel@tonic-gate 			ppa[0] = NULL;
35730Sstevel@tonic-gate 			if (!brkcow && szc &&
35740Sstevel@tonic-gate 			    !page_exists_physcontig(vp, off, szc,
35750Sstevel@tonic-gate 				segtype == MAP_PRIVATE ? ppa : NULL)) {
35760Sstevel@tonic-gate 				SEGVN_VMSTAT_FLTVNPAGES(9);
3577749Ssusans 				if (page_alloc_pages(vp, seg, a, &pplist, NULL,
357863Saguzovsk 				    szc, 0) && type != F_SOFTLOCK) {
35790Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(10);
35800Sstevel@tonic-gate 					pszc = 0;
35810Sstevel@tonic-gate 					ierr = -1;
35820Sstevel@tonic-gate 					alloc_failed |= (1 << szc);
35830Sstevel@tonic-gate 					break;
35840Sstevel@tonic-gate 				}
358563Saguzovsk 				if (pplist != NULL &&
358663Saguzovsk 				    vp->v_mpssdata == SEGVN_PAGEIO) {
35870Sstevel@tonic-gate 					int downsize;
35880Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(11);
35890Sstevel@tonic-gate 					physcontig = segvn_fill_vp_pages(svd,
35900Sstevel@tonic-gate 					    vp, off, szc, ppa, &pplist,
35910Sstevel@tonic-gate 					    &pszc, &downsize);
35920Sstevel@tonic-gate 					ASSERT(!physcontig || pplist == NULL);
359363Saguzovsk 					if (!physcontig && downsize &&
359463Saguzovsk 					    type != F_SOFTLOCK) {
35950Sstevel@tonic-gate 						ASSERT(pplist == NULL);
35960Sstevel@tonic-gate 						SEGVN_VMSTAT_FLTVNPAGES(12);
35970Sstevel@tonic-gate 						ierr = -1;
35980Sstevel@tonic-gate 						break;
35990Sstevel@tonic-gate 					}
36000Sstevel@tonic-gate 					ASSERT(!physcontig ||
36010Sstevel@tonic-gate 					    segtype == MAP_PRIVATE ||
36020Sstevel@tonic-gate 					    ppa[0] == NULL);
36030Sstevel@tonic-gate 					if (physcontig && ppa[0] == NULL) {
36040Sstevel@tonic-gate 						physcontig = 0;
36050Sstevel@tonic-gate 					}
36060Sstevel@tonic-gate 				}
36070Sstevel@tonic-gate 			} else if (!brkcow && szc && ppa[0] != NULL) {
36080Sstevel@tonic-gate 				SEGVN_VMSTAT_FLTVNPAGES(13);
36090Sstevel@tonic-gate 				ASSERT(segtype == MAP_PRIVATE);
36100Sstevel@tonic-gate 				physcontig = 1;
36110Sstevel@tonic-gate 			}
36120Sstevel@tonic-gate 
36130Sstevel@tonic-gate 			if (!physcontig) {
36140Sstevel@tonic-gate 				SEGVN_VMSTAT_FLTVNPAGES(14);
36150Sstevel@tonic-gate 				ppa[0] = NULL;
36160Sstevel@tonic-gate 				ierr = VOP_GETPAGE(vp, (offset_t)off, pgsz,
36170Sstevel@tonic-gate 				    &vpprot, ppa, pgsz, seg, a, arw,
36180Sstevel@tonic-gate 				    svd->cred);
3619*3351Saguzovsk #ifdef DEBUG
3620*3351Saguzovsk 				if (ierr == 0) {
3621*3351Saguzovsk 					for (i = 0; i < pages; i++) {
3622*3351Saguzovsk 						ASSERT(PAGE_LOCKED(ppa[i]));
3623*3351Saguzovsk 						ASSERT(!PP_ISFREE(ppa[i]));
3624*3351Saguzovsk 						ASSERT(ppa[i]->p_vnode == vp);
3625*3351Saguzovsk 						ASSERT(ppa[i]->p_offset ==
3626*3351Saguzovsk 						    off + (i << PAGESHIFT));
3627*3351Saguzovsk 					}
3628*3351Saguzovsk 				}
3629*3351Saguzovsk #endif /* DEBUG */
36300Sstevel@tonic-gate 				if (segtype == MAP_PRIVATE) {
36310Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(15);
36320Sstevel@tonic-gate 					vpprot &= ~PROT_WRITE;
36330Sstevel@tonic-gate 				}
36340Sstevel@tonic-gate 			} else {
36350Sstevel@tonic-gate 				ASSERT(segtype == MAP_PRIVATE);
36360Sstevel@tonic-gate 				SEGVN_VMSTAT_FLTVNPAGES(16);
36370Sstevel@tonic-gate 				vpprot = PROT_ALL & ~PROT_WRITE;
36380Sstevel@tonic-gate 				ierr = 0;
36390Sstevel@tonic-gate 			}
36400Sstevel@tonic-gate 
36410Sstevel@tonic-gate 			if (ierr != 0) {
36420Sstevel@tonic-gate 				SEGVN_VMSTAT_FLTVNPAGES(17);
36430Sstevel@tonic-gate 				if (pplist != NULL) {
36440Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(18);
36450Sstevel@tonic-gate 					page_free_replacement_page(pplist);
36460Sstevel@tonic-gate 					page_create_putback(pages);
36470Sstevel@tonic-gate 				}
36480Sstevel@tonic-gate 				SEGVN_RESTORE_SOFTLOCK(type, pages);
36490Sstevel@tonic-gate 				if (a + pgsz <= eaddr) {
36500Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(19);
36510Sstevel@tonic-gate 					err = FC_MAKE_ERR(ierr);
36520Sstevel@tonic-gate 					goto out;
36530Sstevel@tonic-gate 				}
36540Sstevel@tonic-gate 				va.va_mask = AT_SIZE;
36550Sstevel@tonic-gate 				if (VOP_GETATTR(vp, &va, 0, svd->cred) != 0) {
36560Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(20);
36570Sstevel@tonic-gate 					err = FC_MAKE_ERR(EIO);
36580Sstevel@tonic-gate 					goto out;
36590Sstevel@tonic-gate 				}
36600Sstevel@tonic-gate 				if (btopr(va.va_size) >= btopr(off + pgsz)) {
36610Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(21);
3662490Ssusans 					err = FC_MAKE_ERR(ierr);
36630Sstevel@tonic-gate 					goto out;
36640Sstevel@tonic-gate 				}
36650Sstevel@tonic-gate 				if (btopr(va.va_size) <
36660Sstevel@tonic-gate 				    btopr(off + (eaddr - a))) {
36670Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(22);
3668490Ssusans 					err = FC_MAKE_ERR(ierr);
36690Sstevel@tonic-gate 					goto out;
36700Sstevel@tonic-gate 				}
36710Sstevel@tonic-gate 				if (brkcow || type == F_SOFTLOCK) {
36720Sstevel@tonic-gate 					/* can't reduce map area */
36730Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(23);
36740Sstevel@tonic-gate 					vop_size_err = 1;
36750Sstevel@tonic-gate 					goto out;
36760Sstevel@tonic-gate 				}
36770Sstevel@tonic-gate 				SEGVN_VMSTAT_FLTVNPAGES(24);
36780Sstevel@tonic-gate 				ASSERT(szc != 0);
36790Sstevel@tonic-gate 				pszc = 0;
36800Sstevel@tonic-gate 				ierr = -1;
36810Sstevel@tonic-gate 				break;
36820Sstevel@tonic-gate 			}
36830Sstevel@tonic-gate 
36840Sstevel@tonic-gate 			if (amp != NULL) {
36850Sstevel@tonic-gate 				ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
36860Sstevel@tonic-gate 				anon_array_enter(amp, aindx, &an_cookie);
36870Sstevel@tonic-gate 			}
36880Sstevel@tonic-gate 			if (amp != NULL &&
36890Sstevel@tonic-gate 			    anon_get_ptr(amp->ahp, aindx) != NULL) {
36900Sstevel@tonic-gate 				ulong_t taindx = P2ALIGN(aindx, maxpages);
36910Sstevel@tonic-gate 
36920Sstevel@tonic-gate 				SEGVN_VMSTAT_FLTVNPAGES(25);
36930Sstevel@tonic-gate 				if (anon_pages(amp->ahp, taindx, maxpages) !=
36940Sstevel@tonic-gate 				    maxpages) {
36950Sstevel@tonic-gate 					panic("segvn_fault_vnodepages:"
36960Sstevel@tonic-gate 					    " empty anon slots\n");
36970Sstevel@tonic-gate 				}
36980Sstevel@tonic-gate 				for (i = 0; i < pages; i++) {
36990Sstevel@tonic-gate 					page_unlock(ppa[i]);
37000Sstevel@tonic-gate 				}
37010Sstevel@tonic-gate 				anon_array_exit(&an_cookie);
37020Sstevel@tonic-gate 				ANON_LOCK_EXIT(&amp->a_rwlock);
37030Sstevel@tonic-gate 				if (pplist != NULL) {
37040Sstevel@tonic-gate 					page_free_replacement_page(pplist);
37050Sstevel@tonic-gate 					page_create_putback(pages);
37060Sstevel@tonic-gate 				}
37070Sstevel@tonic-gate 				SEGVN_RESTORE_SOFTLOCK(type, pages);
37080Sstevel@tonic-gate 				if (szc < seg->s_szc) {
37090Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(26);
37100Sstevel@tonic-gate 					/*
37110Sstevel@tonic-gate 					 * For private segments SOFTLOCK
37120Sstevel@tonic-gate 					 * either always breaks cow (any rw
37130Sstevel@tonic-gate 					 * type except S_READ_NOCOW) or
37140Sstevel@tonic-gate 					 * address space is locked as writer
37150Sstevel@tonic-gate 					 * (S_READ_NOCOW case) and anon slots
37160Sstevel@tonic-gate 					 * can't show up on second check.
37170Sstevel@tonic-gate 					 * Therefore if we are here for
37180Sstevel@tonic-gate 					 * SOFTLOCK case it must be a cow
37190Sstevel@tonic-gate 					 * break but cow break never reduces
37200Sstevel@tonic-gate 					 * szc. Thus the assert below.
37210Sstevel@tonic-gate 					 */
37220Sstevel@tonic-gate 					ASSERT(!brkcow && type != F_SOFTLOCK);
37230Sstevel@tonic-gate 					pszc = seg->s_szc;
37240Sstevel@tonic-gate 					ierr = -2;
37250Sstevel@tonic-gate 					break;
37260Sstevel@tonic-gate 				}
37270Sstevel@tonic-gate 				ASSERT(IS_P2ALIGNED(a, maxpgsz));
37280Sstevel@tonic-gate 				goto again;
37290Sstevel@tonic-gate 			}
37300Sstevel@tonic-gate #ifdef DEBUG
37310Sstevel@tonic-gate 			if (amp != NULL) {
37320Sstevel@tonic-gate 				ulong_t taindx = P2ALIGN(aindx, maxpages);
37330Sstevel@tonic-gate 				ASSERT(!anon_pages(amp->ahp, taindx, maxpages));
37340Sstevel@tonic-gate 			}
37350Sstevel@tonic-gate #endif /* DEBUG */
37360Sstevel@tonic-gate 
37370Sstevel@tonic-gate 			if (brkcow) {
37380Sstevel@tonic-gate 				ASSERT(amp != NULL);
37390Sstevel@tonic-gate 				ASSERT(pplist == NULL);
37400Sstevel@tonic-gate 				ASSERT(szc == seg->s_szc);
37410Sstevel@tonic-gate 				ASSERT(IS_P2ALIGNED(a, maxpgsz));
37420Sstevel@tonic-gate 				ASSERT(IS_P2ALIGNED(aindx, maxpages));
37430Sstevel@tonic-gate 				SEGVN_VMSTAT_FLTVNPAGES(27);
37440Sstevel@tonic-gate 				ierr = anon_map_privatepages(amp, aindx, szc,
37450Sstevel@tonic-gate 				    seg, a, prot, ppa, vpage, segvn_anypgsz,
37460Sstevel@tonic-gate 				    svd->cred);
37470Sstevel@tonic-gate 				if (ierr != 0) {
37480Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(28);
37490Sstevel@tonic-gate 					anon_array_exit(&an_cookie);
37500Sstevel@tonic-gate 					ANON_LOCK_EXIT(&amp->a_rwlock);
37510Sstevel@tonic-gate 					SEGVN_RESTORE_SOFTLOCK(type, pages);
37520Sstevel@tonic-gate 					err = FC_MAKE_ERR(ierr);
37530Sstevel@tonic-gate 					goto out;
37540Sstevel@tonic-gate 				}
37550Sstevel@tonic-gate 
37560Sstevel@tonic-gate 				ASSERT(!IS_VMODSORT(ppa[0]->p_vnode));
37570Sstevel@tonic-gate 				/*
37580Sstevel@tonic-gate 				 * p_szc can't be changed for locked
37590Sstevel@tonic-gate 				 * swapfs pages.
37600Sstevel@tonic-gate 				 */
37610Sstevel@tonic-gate 				hat_memload_array(hat, a, pgsz, ppa, prot,
37620Sstevel@tonic-gate 				    hat_flag);
37630Sstevel@tonic-gate 
37640Sstevel@tonic-gate 				if (!(hat_flag & HAT_LOAD_LOCK)) {
37650Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(29);
37660Sstevel@tonic-gate 					for (i = 0; i < pages; i++) {
37670Sstevel@tonic-gate 						page_unlock(ppa[i]);
37680Sstevel@tonic-gate 					}
37690Sstevel@tonic-gate 				}
37700Sstevel@tonic-gate 				anon_array_exit(&an_cookie);
37710Sstevel@tonic-gate 				ANON_LOCK_EXIT(&amp->a_rwlock);
37720Sstevel@tonic-gate 				goto next;
37730Sstevel@tonic-gate 			}
37740Sstevel@tonic-gate 
37750Sstevel@tonic-gate 			pfn = page_pptonum(ppa[0]);
37760Sstevel@tonic-gate 			/*
37770Sstevel@tonic-gate 			 * hat_page_demote() needs an EXCl lock on one of
37780Sstevel@tonic-gate 			 * constituent page_t's and it decreases root's p_szc
37790Sstevel@tonic-gate 			 * last. This means if root's p_szc is equal szc and
37800Sstevel@tonic-gate 			 * all its constituent pages are locked
37810Sstevel@tonic-gate 			 * hat_page_demote() that could have changed p_szc to
37820Sstevel@tonic-gate 			 * szc is already done and no new have page_demote()
37830Sstevel@tonic-gate 			 * can start for this large page.
37840Sstevel@tonic-gate 			 */
37850Sstevel@tonic-gate 
37860Sstevel@tonic-gate 			/*
37870Sstevel@tonic-gate 			 * we need to make sure same mapping size is used for
37880Sstevel@tonic-gate 			 * the same address range if there's a possibility the
37890Sstevel@tonic-gate 			 * adddress is already mapped because hat layer panics
37900Sstevel@tonic-gate 			 * when translation is loaded for the range already
37910Sstevel@tonic-gate 			 * mapped with a different page size.  We achieve it
37920Sstevel@tonic-gate 			 * by always using largest page size possible subject
37930Sstevel@tonic-gate 			 * to the constraints of page size, segment page size
37940Sstevel@tonic-gate 			 * and page alignment.  Since mappings are invalidated
37950Sstevel@tonic-gate 			 * when those constraints change and make it
37960Sstevel@tonic-gate 			 * impossible to use previously used mapping size no
37970Sstevel@tonic-gate 			 * mapping size conflicts should happen.
37980Sstevel@tonic-gate 			 */
37990Sstevel@tonic-gate 
38000Sstevel@tonic-gate 		chkszc:
38010Sstevel@tonic-gate 			if ((pszc = ppa[0]->p_szc) == szc &&
38020Sstevel@tonic-gate 			    IS_P2ALIGNED(pfn, pages)) {
38030Sstevel@tonic-gate 
38040Sstevel@tonic-gate 				SEGVN_VMSTAT_FLTVNPAGES(30);
38050Sstevel@tonic-gate #ifdef DEBUG
38060Sstevel@tonic-gate 				for (i = 0; i < pages; i++) {
38070Sstevel@tonic-gate 					ASSERT(PAGE_LOCKED(ppa[i]));
38080Sstevel@tonic-gate 					ASSERT(!PP_ISFREE(ppa[i]));
38090Sstevel@tonic-gate 					ASSERT(page_pptonum(ppa[i]) ==
38100Sstevel@tonic-gate 					    pfn + i);
38110Sstevel@tonic-gate 					ASSERT(ppa[i]->p_szc == szc);
38120Sstevel@tonic-gate 					ASSERT(ppa[i]->p_vnode == vp);
38130Sstevel@tonic-gate 					ASSERT(ppa[i]->p_offset ==
38140Sstevel@tonic-gate 					    off + (i << PAGESHIFT));
38150Sstevel@tonic-gate 				}
381663Saguzovsk #endif /* DEBUG */
38170Sstevel@tonic-gate 				/*
38180Sstevel@tonic-gate 				 * All pages are of szc we need and they are
38190Sstevel@tonic-gate 				 * all locked so they can't change szc. load
38200Sstevel@tonic-gate 				 * translations.
38210Sstevel@tonic-gate 				 *
38220Sstevel@tonic-gate 				 * if page got promoted since last check
38230Sstevel@tonic-gate 				 * we don't need pplist.
38240Sstevel@tonic-gate 				 */
38250Sstevel@tonic-gate 				if (pplist != NULL) {
38260Sstevel@tonic-gate 					page_free_replacement_page(pplist);
38270Sstevel@tonic-gate 					page_create_putback(pages);
38280Sstevel@tonic-gate 				}
38290Sstevel@tonic-gate 				if (PP_ISMIGRATE(ppa[0])) {
38300Sstevel@tonic-gate 					page_migrate(seg, a, ppa, pages);
38310Sstevel@tonic-gate 				}
38320Sstevel@tonic-gate 				SEGVN_UPDATE_MODBITS(ppa, pages, rw,
38330Sstevel@tonic-gate 				    prot, vpprot);
38340Sstevel@tonic-gate 				if (!xhat) {
38350Sstevel@tonic-gate 					hat_memload_array(hat, a, pgsz, ppa,
38360Sstevel@tonic-gate 					    prot & vpprot, hat_flag);
38370Sstevel@tonic-gate 				} else {
38380Sstevel@tonic-gate 					/*
38390Sstevel@tonic-gate 					 * avoid large xhat mappings to FS
38400Sstevel@tonic-gate 					 * pages so that hat_page_demote()
38410Sstevel@tonic-gate 					 * doesn't need to check for xhat
38420Sstevel@tonic-gate 					 * large mappings.
38430Sstevel@tonic-gate 					 */
38440Sstevel@tonic-gate 					for (i = 0; i < pages; i++) {
38450Sstevel@tonic-gate 						hat_memload(hat,
38460Sstevel@tonic-gate 						    a + (i << PAGESHIFT),
38470Sstevel@tonic-gate 						    ppa[i], prot & vpprot,
38480Sstevel@tonic-gate 						    hat_flag);
38490Sstevel@tonic-gate 					}
38500Sstevel@tonic-gate 				}
38510Sstevel@tonic-gate 
38520Sstevel@tonic-gate 				if (!(hat_flag & HAT_LOAD_LOCK)) {
38530Sstevel@tonic-gate 					for (i = 0; i < pages; i++) {
38540Sstevel@tonic-gate 						page_unlock(ppa[i]);
38550Sstevel@tonic-gate 					}
38560Sstevel@tonic-gate 				}
38570Sstevel@tonic-gate 				if (amp != NULL) {
38580Sstevel@tonic-gate 					anon_array_exit(&an_cookie);
38590Sstevel@tonic-gate 					ANON_LOCK_EXIT(&amp->a_rwlock);
38600Sstevel@tonic-gate 				}
38610Sstevel@tonic-gate 				goto next;
38620Sstevel@tonic-gate 			}
38630Sstevel@tonic-gate 
38640Sstevel@tonic-gate 			/*
38650Sstevel@tonic-gate 			 * See if upsize is possible.
38660Sstevel@tonic-gate 			 */
38670Sstevel@tonic-gate 			if (pszc > szc && szc < seg->s_szc &&
38680Sstevel@tonic-gate 			    (segvn_anypgsz_vnode || pszc >= seg->s_szc)) {
38690Sstevel@tonic-gate 				pgcnt_t aphase;
38700Sstevel@tonic-gate 				uint_t pszc1 = MIN(pszc, seg->s_szc);
38710Sstevel@tonic-gate 				ppgsz = page_get_pagesize(pszc1);
38720Sstevel@tonic-gate 				ppages = btop(ppgsz);
38730Sstevel@tonic-gate 				aphase = btop(P2PHASE((uintptr_t)a, ppgsz));
38740Sstevel@tonic-gate 
387563Saguzovsk 				ASSERT(type != F_SOFTLOCK);
387663Saguzovsk 
38770Sstevel@tonic-gate 				SEGVN_VMSTAT_FLTVNPAGES(31);
38780Sstevel@tonic-gate 				if (aphase != P2PHASE(pfn, ppages)) {
38790Sstevel@tonic-gate 					segvn_faultvnmpss_align_err4++;
38800Sstevel@tonic-gate 				} else {
38810Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(32);
38820Sstevel@tonic-gate 					if (pplist != NULL) {
38830Sstevel@tonic-gate 						page_t *pl = pplist;
38840Sstevel@tonic-gate 						page_free_replacement_page(pl);
38850Sstevel@tonic-gate 						page_create_putback(pages);
38860Sstevel@tonic-gate 					}
38870Sstevel@tonic-gate 					for (i = 0; i < pages; i++) {
38880Sstevel@tonic-gate 						page_unlock(ppa[i]);
38890Sstevel@tonic-gate 					}
38900Sstevel@tonic-gate 					if (amp != NULL) {
38910Sstevel@tonic-gate 						anon_array_exit(&an_cookie);
38920Sstevel@tonic-gate 						ANON_LOCK_EXIT(&amp->a_rwlock);
38930Sstevel@tonic-gate 					}
38940Sstevel@tonic-gate 					pszc = pszc1;
38950Sstevel@tonic-gate 					ierr = -2;
38960Sstevel@tonic-gate 					break;
38970Sstevel@tonic-gate 				}
38980Sstevel@tonic-gate 			}
38990Sstevel@tonic-gate 
39000Sstevel@tonic-gate 			/*
39010Sstevel@tonic-gate 			 * check if we should use smallest mapping size.
39020Sstevel@tonic-gate 			 */
39030Sstevel@tonic-gate 			upgrdfail = 0;
39040Sstevel@tonic-gate 			if (szc == 0 || xhat ||
39050Sstevel@tonic-gate 			    (pszc >= szc &&
39060Sstevel@tonic-gate 			    !IS_P2ALIGNED(pfn, pages)) ||
39070Sstevel@tonic-gate 			    (pszc < szc &&
39080Sstevel@tonic-gate 			    !segvn_full_szcpages(ppa, szc, &upgrdfail,
39090Sstevel@tonic-gate 				&pszc))) {
39100Sstevel@tonic-gate 
391163Saguzovsk 				if (upgrdfail && type != F_SOFTLOCK) {
39120Sstevel@tonic-gate 					/*
39130Sstevel@tonic-gate 					 * segvn_full_szcpages failed to lock
39140Sstevel@tonic-gate 					 * all pages EXCL. Size down.
39150Sstevel@tonic-gate 					 */
39160Sstevel@tonic-gate 					ASSERT(pszc < szc);
39170Sstevel@tonic-gate 
39180Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(33);
39190Sstevel@tonic-gate 
39200Sstevel@tonic-gate 					if (pplist != NULL) {
39210Sstevel@tonic-gate 						page_t *pl = pplist;
39220Sstevel@tonic-gate 						page_free_replacement_page(pl);
39230Sstevel@tonic-gate 						page_create_putback(pages);
39240Sstevel@tonic-gate 					}
39250Sstevel@tonic-gate 
39260Sstevel@tonic-gate 					for (i = 0; i < pages; i++) {
39270Sstevel@tonic-gate 						page_unlock(ppa[i]);
39280Sstevel@tonic-gate 					}
39290Sstevel@tonic-gate 					if (amp != NULL) {
39300Sstevel@tonic-gate 						anon_array_exit(&an_cookie);
39310Sstevel@tonic-gate 						ANON_LOCK_EXIT(&amp->a_rwlock);
39320Sstevel@tonic-gate 					}
39330Sstevel@tonic-gate 					ierr = -1;
39340Sstevel@tonic-gate 					break;
39350Sstevel@tonic-gate 				}
3936*3351Saguzovsk 				if (szc != 0 && !xhat && !upgrdfail) {
39370Sstevel@tonic-gate 					segvn_faultvnmpss_align_err5++;
39380Sstevel@tonic-gate 				}
39390Sstevel@tonic-gate 				SEGVN_VMSTAT_FLTVNPAGES(34);
39400Sstevel@tonic-gate 				if (pplist != NULL) {
39410Sstevel@tonic-gate 					page_free_replacement_page(pplist);
39420Sstevel@tonic-gate 					page_create_putback(pages);
39430Sstevel@tonic-gate 				}
39440Sstevel@tonic-gate 				SEGVN_UPDATE_MODBITS(ppa, pages, rw,
39450Sstevel@tonic-gate 				    prot, vpprot);
39462414Saguzovsk 				if (upgrdfail && segvn_anypgsz_vnode) {
39472414Saguzovsk 					/* SOFTLOCK case */
39482414Saguzovsk 					hat_memload_array(hat, a, pgsz,
39492414Saguzovsk 					    ppa, prot & vpprot, hat_flag);
39502414Saguzovsk 				} else {
39512414Saguzovsk 					for (i = 0; i < pages; i++) {
39522414Saguzovsk 						hat_memload(hat,
39532414Saguzovsk 						    a + (i << PAGESHIFT),
39542414Saguzovsk 						    ppa[i], prot & vpprot,
39552414Saguzovsk 						    hat_flag);
39562414Saguzovsk 					}
39570Sstevel@tonic-gate 				}
39580Sstevel@tonic-gate 				if (!(hat_flag & HAT_LOAD_LOCK)) {
39590Sstevel@tonic-gate 					for (i = 0; i < pages; i++) {
39600Sstevel@tonic-gate 						page_unlock(ppa[i]);
39610Sstevel@tonic-gate 					}
39620Sstevel@tonic-gate 				}
39630Sstevel@tonic-gate 				if (amp != NULL) {
39640Sstevel@tonic-gate 					anon_array_exit(&an_cookie);
39650Sstevel@tonic-gate 					ANON_LOCK_EXIT(&amp->a_rwlock);
39660Sstevel@tonic-gate 				}
39670Sstevel@tonic-gate 				goto next;
39680Sstevel@tonic-gate 			}
39690Sstevel@tonic-gate 
39700Sstevel@tonic-gate 			if (pszc == szc) {
39710Sstevel@tonic-gate 				/*
39720Sstevel@tonic-gate 				 * segvn_full_szcpages() upgraded pages szc.
39730Sstevel@tonic-gate 				 */
39740Sstevel@tonic-gate 				ASSERT(pszc == ppa[0]->p_szc);
39750Sstevel@tonic-gate 				ASSERT(IS_P2ALIGNED(pfn, pages));
39760Sstevel@tonic-gate 				goto chkszc;
39770Sstevel@tonic-gate 			}
39780Sstevel@tonic-gate 
39790Sstevel@tonic-gate 			if (pszc > szc) {
39800Sstevel@tonic-gate 				kmutex_t *szcmtx;
39810Sstevel@tonic-gate 				SEGVN_VMSTAT_FLTVNPAGES(35);
39820Sstevel@tonic-gate 				/*
39830Sstevel@tonic-gate 				 * p_szc of ppa[0] can change since we haven't
39840Sstevel@tonic-gate 				 * locked all constituent pages. Call
39850Sstevel@tonic-gate 				 * page_lock_szc() to prevent szc changes.
39860Sstevel@tonic-gate 				 * This should be a rare case that happens when
39870Sstevel@tonic-gate 				 * multiple segments use a different page size
39880Sstevel@tonic-gate 				 * to map the same file offsets.
39890Sstevel@tonic-gate 				 */
39900Sstevel@tonic-gate 				szcmtx = page_szc_lock(ppa[0]);
39910Sstevel@tonic-gate 				pszc = ppa[0]->p_szc;
39920Sstevel@tonic-gate 				ASSERT(szcmtx != NULL || pszc == 0);
39930Sstevel@tonic-gate 				ASSERT(ppa[0]->p_szc <= pszc);
39940Sstevel@tonic-gate 				if (pszc <= szc) {
39950Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(36);
39960Sstevel@tonic-gate 					if (szcmtx != NULL) {
39970Sstevel@tonic-gate 						mutex_exit(szcmtx);
39980Sstevel@tonic-gate 					}
39990Sstevel@tonic-gate 					goto chkszc;
40000Sstevel@tonic-gate 				}
40010Sstevel@tonic-gate 				if (pplist != NULL) {
40020Sstevel@tonic-gate 					/*
40030Sstevel@tonic-gate 					 * page got promoted since last check.
40040Sstevel@tonic-gate 					 * we don't need preaalocated large
40050Sstevel@tonic-gate 					 * page.
40060Sstevel@tonic-gate 					 */
40070Sstevel@tonic-gate 					SEGVN_VMSTAT_FLTVNPAGES(37);
40080Sstevel@tonic-gate 					page_free_replacement_page(pplist);
40090Sstevel@tonic-gate 					page_create_putback(pages);
40100Sstevel@tonic-gate 				}
40110Sstevel@tonic-gate 				SEGVN_UPDATE_MODBITS(ppa, pages, rw,
40120Sstevel@tonic-gate 				    prot, vpprot);
40130Sstevel@tonic-gate 				hat_memload_array(hat, a, pgsz, ppa,
40140Sstevel@tonic-gate 				    prot & vpprot, hat_flag);
40150Sstevel@tonic-gate 				mutex_exit(szcmtx);
40160Sstevel@tonic-gate 				if (!(hat_flag & HAT_LOAD_LOCK)) {
40170Sstevel@tonic-gate 					for (i = 0; i < pages; i++) {
40180Sstevel@tonic-gate 						page_unlock(ppa[i]);
40190Sstevel@tonic-gate 					}
40200Sstevel@tonic-gate 				}
40210Sstevel@tonic-gate 				if (amp != NULL) {
40220Sstevel@tonic-gate 					anon_array_exit(&an_cookie);
40230Sstevel@tonic-gate 					ANON_LOCK_EXIT(&amp->a_rwlock);
40240Sstevel@tonic-gate 				}
40250Sstevel@tonic-gate 				goto next;
40260Sstevel@tonic-gate 			}
40270Sstevel@tonic-gate 
40280Sstevel@tonic-gate 			/*
40290Sstevel@tonic-gate 			 * if page got demoted since last check
40300Sstevel@tonic-gate 			 * we could have not allocated larger page.
40310Sstevel@tonic-gate 			 * allocate now.
40320Sstevel@tonic-gate 			 */
40330Sstevel@tonic-gate 			if (pplist == NULL &&
4034749Ssusans 			    page_alloc_pages(vp, seg, a, &pplist, NULL,
4035749Ssusans 				szc, 0) && type != F_SOFTLOCK) {
40360Sstevel@tonic-gate 				SEGVN_VMSTAT_FLTVNPAGES(38);
40370Sstevel@tonic-gate 				for (i = 0; i < pages; i++) {
40380Sstevel@tonic-gate 					page_unlock(ppa[i]);
40390Sstevel@tonic-gate 				}
40400Sstevel@tonic-gate 				if (amp != NULL) {
40410Sstevel@tonic-gate 					anon_array_exit(&an_cookie);
40420Sstevel@tonic-gate 					ANON_LOCK_EXIT(&amp->a_rwlock);
40430Sstevel@tonic-gate 				}
40440Sstevel@tonic-gate 				ierr = -1;
40450Sstevel@tonic-gate 				alloc_failed |= (1 << szc);
40460Sstevel@tonic-gate 				break;
40470Sstevel@tonic-gate 			}
40480Sstevel@tonic-gate 
40490Sstevel@tonic-gate 			SEGVN_VMSTAT_FLTVNPAGES(39);
40500Sstevel@tonic-gate 
405163Saguzovsk 			if (pplist != NULL) {
405263Saguzovsk 				segvn_relocate_pages(ppa, pplist);
405363Saguzovsk #ifdef DEBUG
405463Saguzovsk 			} else {
405563Saguzovsk 				ASSERT(type == F_SOFTLOCK);
405663Saguzovsk 				SEGVN_VMSTAT_FLTVNPAGES(40);
405763Saguzovsk #endif /* DEBUG */
405863Saguzovsk 			}
40590Sstevel@tonic-gate 
40600Sstevel@tonic-gate 			SEGVN_UPDATE_MODBITS(ppa, pages, rw, prot, vpprot);
406163Saguzovsk 
406263Saguzovsk 			if (pplist == NULL && segvn_anypgsz_vnode == 0) {
406363Saguzovsk 				ASSERT(type == F_SOFTLOCK);
406463Saguzovsk 				for (i = 0; i < pages; i++) {
406563Saguzovsk 					ASSERT(ppa[i]->p_szc < szc);
406663Saguzovsk 					hat_memload(hat, a + (i << PAGESHIFT),
406763Saguzovsk 					    ppa[i], prot & vpprot, hat_flag);
406863Saguzovsk 				}
406963Saguzovsk 			} else {
407063Saguzovsk 				ASSERT(pplist != NULL || type == F_SOFTLOCK);
407163Saguzovsk 				hat_memload_array(hat, a, pgsz, ppa,
407263Saguzovsk 				    prot & vpprot, hat_flag);
407363Saguzovsk 			}
40740Sstevel@tonic-gate 			if (!(hat_flag & HAT_LOAD_LOCK)) {
40750Sstevel@tonic-gate 				for (i = 0; i < pages; i++) {
40760Sstevel@tonic-gate 					ASSERT(PAGE_SHARED(ppa[i]));
40770Sstevel@tonic-gate 					page_unlock(ppa[i]);
40780Sstevel@tonic-gate 				}
40790Sstevel@tonic-gate 			}
40800Sstevel@tonic-gate 			if (amp != NULL) {
40810Sstevel@tonic-gate 				anon_array_exit(&an_cookie);
40820Sstevel@tonic-gate 				ANON_LOCK_EXIT(&amp->a_rwlock);
40830Sstevel@tonic-gate 			}
40840Sstevel@tonic-gate 
40850Sstevel@tonic-gate 		next:
40860Sstevel@tonic-gate 			if (vpage != NULL) {
40870Sstevel@tonic-gate 				vpage += pages;
40880Sstevel@tonic-gate 			}
40890Sstevel@tonic-gate 			adjszc_chk = 1;
40900Sstevel@tonic-gate 		}
40910Sstevel@tonic-gate 		if (a == lpgeaddr)
40920Sstevel@tonic-gate 			break;
40930Sstevel@tonic-gate 		ASSERT(a < lpgeaddr);
409463Saguzovsk 
409563Saguzovsk 		ASSERT(!brkcow && type != F_SOFTLOCK);
409663Saguzovsk 
40970Sstevel@tonic-gate 		/*
40980Sstevel@tonic-gate 		 * ierr == -1 means we failed to map with a large page.
40990Sstevel@tonic-gate 		 * (either due to allocation/relocation failures or
41000Sstevel@tonic-gate 		 * misalignment with other mappings to this file.
41010Sstevel@tonic-gate 		 *
41020Sstevel@tonic-gate 		 * ierr == -2 means some other thread allocated a large page
41030Sstevel@tonic-gate 		 * after we gave up tp map with a large page.  retry with
41040Sstevel@tonic-gate 		 * larger mapping.
41050Sstevel@tonic-gate 		 */
41060Sstevel@tonic-gate 		ASSERT(ierr == -1 || ierr == -2);
41070Sstevel@tonic-gate 		ASSERT(ierr == -2 || szc != 0);
41080Sstevel@tonic-gate 		ASSERT(ierr == -1 || szc < seg->s_szc);
41090Sstevel@tonic-gate 		if (ierr == -2) {
411063Saguzovsk 			SEGVN_VMSTAT_FLTVNPAGES(41);
41110Sstevel@tonic-gate 			ASSERT(pszc > szc && pszc <= seg->s_szc);
41120Sstevel@tonic-gate 			szc = pszc;
41130Sstevel@tonic-gate 		} else if (segvn_anypgsz_vnode) {
411463Saguzovsk 			SEGVN_VMSTAT_FLTVNPAGES(42);
41150Sstevel@tonic-gate 			szc--;
41160Sstevel@tonic-gate 		} else {
411763Saguzovsk 			SEGVN_VMSTAT_FLTVNPAGES(43);
41180Sstevel@tonic-gate 			ASSERT(pszc < szc);
41190Sstevel@tonic-gate 			/*
41200Sstevel@tonic-gate 			 * other process created pszc large page.
41210Sstevel@tonic-gate 			 * but we still have to drop to 0 szc.
41220Sstevel@tonic-gate 			 */
41230Sstevel@tonic-gate 			szc = 0;
41240Sstevel@tonic-gate 		}
41250Sstevel@tonic-gate 
41260Sstevel@tonic-gate 		pgsz = page_get_pagesize(szc);
41270Sstevel@tonic-gate 		pages = btop(pgsz);
412863Saguzovsk 		if (ierr == -2) {
41290Sstevel@tonic-gate 			/*
41300Sstevel@tonic-gate 			 * Size up case. Note lpgaddr may only be needed for
41310Sstevel@tonic-gate 			 * softlock case so we don't adjust it here.
41320Sstevel@tonic-gate 			 */
41330Sstevel@tonic-gate 			a = (caddr_t)P2ALIGN((uintptr_t)a, pgsz);
41340Sstevel@tonic-gate 			ASSERT(a >= lpgaddr);
41350Sstevel@tonic-gate 			lpgeaddr = (caddr_t)P2ROUNDUP((uintptr_t)eaddr, pgsz);
41360Sstevel@tonic-gate 			off = svd->offset + (uintptr_t)(a - seg->s_base);
41370Sstevel@tonic-gate 			aindx = svd->anon_index + seg_page(seg, a);
41380Sstevel@tonic-gate 			vpage = (svd->vpage != NULL) ?
41390Sstevel@tonic-gate 			    &svd->vpage[seg_page(seg, a)] : NULL;
41400Sstevel@tonic-gate 		} else {
41410Sstevel@tonic-gate 			/*
41420Sstevel@tonic-gate 			 * Size down case. Note lpgaddr may only be needed for
41430Sstevel@tonic-gate 			 * softlock case so we don't adjust it here.
41440Sstevel@tonic-gate 			 */
41450Sstevel@tonic-gate 			ASSERT(IS_P2ALIGNED(a, pgsz));
41460Sstevel@tonic-gate 			ASSERT(IS_P2ALIGNED(lpgeaddr, pgsz));
41470Sstevel@tonic-gate 			lpgeaddr = (caddr_t)P2ROUNDUP((uintptr_t)eaddr, pgsz);
41480Sstevel@tonic-gate 			ASSERT(a < lpgeaddr);
41490Sstevel@tonic-gate 			if (a < addr) {
41500Sstevel@tonic-gate 				SEGVN_VMSTAT_FLTVNPAGES(44);
41510Sstevel@tonic-gate 				/*
41520Sstevel@tonic-gate 				 * The beginning of the large page region can
41530Sstevel@tonic-gate 				 * be pulled to the right to make a smaller
41540Sstevel@tonic-gate 				 * region. We haven't yet faulted a single
41550Sstevel@tonic-gate 				 * page.
41560Sstevel@tonic-gate 				 */
41570Sstevel@tonic-gate 				a = (caddr_t)P2ALIGN((uintptr_t)addr, pgsz);
41580Sstevel@tonic-gate 				ASSERT(a >= lpgaddr);
41590Sstevel@tonic-gate 				off = svd->offset +
41600Sstevel@tonic-gate 				    (uintptr_t)(a - seg->s_base);
41610Sstevel@tonic-gate 				aindx = svd->anon_index + seg_page(seg, a);
41620Sstevel@tonic-gate 				vpage = (svd->vpage != NULL) ?
41630Sstevel@tonic-gate 				    &svd->vpage[seg_page(seg, a)] : NULL;
41640Sstevel@tonic-gate 			}
41650Sstevel@tonic-gate 		}
41660Sstevel@tonic-gate 	}
41670Sstevel@tonic-gate out:
41680Sstevel@tonic-gate 	kmem_free(ppa, ppasize);
41690Sstevel@tonic-gate 	if (!err && !vop_size_err) {
41700Sstevel@tonic-gate 		SEGVN_VMSTAT_FLTVNPAGES(45);
41710Sstevel@tonic-gate 		return (0);
41720Sstevel@tonic-gate 	}
41730Sstevel@tonic-gate 	if (type == F_SOFTLOCK && a > lpgaddr) {
41740Sstevel@tonic-gate 		SEGVN_VMSTAT_FLTVNPAGES(46);
41750Sstevel@tonic-gate 		segvn_softunlock(seg, lpgaddr, a - lpgaddr, S_OTHER);
41760Sstevel@tonic-gate 	}
41770Sstevel@tonic-gate 	if (!vop_size_err) {
41780Sstevel@tonic-gate 		SEGVN_VMSTAT_FLTVNPAGES(47);
41790Sstevel@tonic-gate 		return (err);
41800Sstevel@tonic-gate 	}
41810Sstevel@tonic-gate 	ASSERT(brkcow || type == F_SOFTLOCK);
41820Sstevel@tonic-gate 	/*
41830Sstevel@tonic-gate 	 * Large page end is mapped beyond the end of file and it's a cow
41840Sstevel@tonic-gate 	 * fault or softlock so we can't reduce the map area.  For now just
41850Sstevel@tonic-gate 	 * demote the segment. This should really only happen if the end of
41860Sstevel@tonic-gate 	 * the file changed after the mapping was established since when large
41870Sstevel@tonic-gate 	 * page segments are created we make sure they don't extend beyond the
41880Sstevel@tonic-gate 	 * end of the file.
41890Sstevel@tonic-gate 	 */
41900Sstevel@tonic-gate 	SEGVN_VMSTAT_FLTVNPAGES(48);
41910Sstevel@tonic-gate 
41920Sstevel@tonic-gate 	SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
41930Sstevel@tonic-gate 	SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_WRITER);
41940Sstevel@tonic-gate 	err = 0;
41950Sstevel@tonic-gate 	if (seg->s_szc != 0) {
419663Saguzovsk 		segvn_fltvnpages_clrszc_cnt++;
419763Saguzovsk 		ASSERT(svd->softlockcnt == 0);
41980Sstevel@tonic-gate 		err = segvn_clrszc(seg);
41990Sstevel@tonic-gate 		if (err != 0) {
42000Sstevel@tonic-gate 			segvn_fltvnpages_clrszc_err++;
42010Sstevel@tonic-gate 		}
42020Sstevel@tonic-gate 	}
42030Sstevel@tonic-gate 	ASSERT(err || seg->s_szc == 0);
42040Sstevel@tonic-gate 	SEGVN_LOCK_DOWNGRADE(seg->s_as, &svd->lock);
42050Sstevel@tonic-gate 	/* segvn_fault will do its job as if szc had been zero to begin with */
42060Sstevel@tonic-gate 	return (err == 0 ? IE_RETRY : FC_MAKE_ERR(err));
42070Sstevel@tonic-gate }
42080Sstevel@tonic-gate 
42090Sstevel@tonic-gate /*
42100Sstevel@tonic-gate  * This routine will attempt to fault in one large page.
42110Sstevel@tonic-gate  * it will use smaller pages if that fails.
42120Sstevel@tonic-gate  * It should only be called for pure anonymous segments.
42130Sstevel@tonic-gate  */
42140Sstevel@tonic-gate static faultcode_t
42150Sstevel@tonic-gate segvn_fault_anonpages(struct hat *hat, struct seg *seg, caddr_t lpgaddr,
42160Sstevel@tonic-gate     caddr_t lpgeaddr, enum fault_type type, enum seg_rw rw, caddr_t addr,
42170Sstevel@tonic-gate     caddr_t eaddr, int brkcow)
42180Sstevel@tonic-gate {
42190Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
42200Sstevel@tonic-gate 	struct anon_map *amp = svd->amp;
42210Sstevel@tonic-gate 	uchar_t segtype = svd->type;
42220Sstevel@tonic-gate 	uint_t szc = seg->s_szc;
42230Sstevel@tonic-gate 	size_t pgsz = page_get_pagesize(szc);
42240Sstevel@tonic-gate 	size_t maxpgsz = pgsz;
42250Sstevel@tonic-gate 	pgcnt_t pages = btop(pgsz);
42260Sstevel@tonic-gate 	size_t ppasize = pages * sizeof (page_t *);
42270Sstevel@tonic-gate 	caddr_t a = lpgaddr;
42280Sstevel@tonic-gate 	ulong_t aindx = svd->anon_index + seg_page(seg, a);
42290Sstevel@tonic-gate 	struct vpage *vpage = (svd->vpage != NULL) ?
42300Sstevel@tonic-gate 	    &svd->vpage[seg_page(seg, a)] : NULL;
42310Sstevel@tonic-gate 	page_t **ppa;
42320Sstevel@tonic-gate 	uint_t	ppa_szc;
42330Sstevel@tonic-gate 	faultcode_t err;
42340Sstevel@tonic-gate 	int ierr;
42350Sstevel@tonic-gate 	uint_t protchk, prot, vpprot;
42362414Saguzovsk 	ulong_t i;
42370Sstevel@tonic-gate 	int hat_flag = (type == F_SOFTLOCK) ? HAT_LOAD_LOCK : HAT_LOAD;
42380Sstevel@tonic-gate 	anon_sync_obj_t cookie;
42392414Saguzovsk 	int first = 1;
42402414Saguzovsk 	int adjszc_chk;
42412414Saguzovsk 	int purged = 0;
42420Sstevel@tonic-gate 
42430Sstevel@tonic-gate 	ASSERT(szc != 0);
42440Sstevel@tonic-gate 	ASSERT(amp != NULL);
42450Sstevel@tonic-gate 	ASSERT(enable_mbit_wa == 0); /* no mbit simulations with large pages */
42460Sstevel@tonic-gate 	ASSERT(!(svd->flags & MAP_NORESERVE));
42470Sstevel@tonic-gate 	ASSERT(type != F_SOFTUNLOCK);
42480Sstevel@tonic-gate 	ASSERT(IS_P2ALIGNED(a, maxpgsz));
42490Sstevel@tonic-gate 
42500Sstevel@tonic-gate 	ASSERT(SEGVN_LOCK_HELD(seg->s_as, &svd->lock));
42510Sstevel@tonic-gate 
42520Sstevel@tonic-gate 	VM_STAT_COND_ADD(type == F_SOFTLOCK, segvnvmstats.fltanpages[0]);
42530Sstevel@tonic-gate 	VM_STAT_COND_ADD(type != F_SOFTLOCK, segvnvmstats.fltanpages[1]);
42540Sstevel@tonic-gate 
42550Sstevel@tonic-gate 	if (svd->flags & MAP_TEXT) {
42560Sstevel@tonic-gate 		hat_flag |= HAT_LOAD_TEXT;
42570Sstevel@tonic-gate 	}
42580Sstevel@tonic-gate 
42590Sstevel@tonic-gate 	if (svd->pageprot) {
42600Sstevel@tonic-gate 		switch (rw) {
42610Sstevel@tonic-gate 		case S_READ:
42620Sstevel@tonic-gate 			protchk = PROT_READ;
42630Sstevel@tonic-gate 			break;
42640Sstevel@tonic-gate 		case S_WRITE:
42650Sstevel@tonic-gate 			protchk = PROT_WRITE;
42660Sstevel@tonic-gate 			break;
42670Sstevel@tonic-gate 		case S_EXEC:
42680Sstevel@tonic-gate 			protchk = PROT_EXEC;
42690Sstevel@tonic-gate 			break;
42700Sstevel@tonic-gate 		case S_OTHER:
42710Sstevel@tonic-gate 		default:
42720Sstevel@tonic-gate 			protchk = PROT_READ | PROT_WRITE | PROT_EXEC;
42730Sstevel@tonic-gate 			break;
42740Sstevel@tonic-gate 		}
42750Sstevel@tonic-gate 		VM_STAT_ADD(segvnvmstats.fltanpages[2]);
42760Sstevel@tonic-gate 	} else {
42770Sstevel@tonic-gate 		prot = svd->prot;
42780Sstevel@tonic-gate 		/* caller has already done segment level protection check. */
42790Sstevel@tonic-gate 	}
42800Sstevel@tonic-gate 
42810Sstevel@tonic-gate 	ppa = kmem_alloc(ppasize, KM_SLEEP);
42820Sstevel@tonic-gate 	ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
42830Sstevel@tonic-gate 	for (;;) {
42842414Saguzovsk 		adjszc_chk = 0;
42850Sstevel@tonic-gate 		for (; a < lpgeaddr; a += pgsz, aindx += pages) {
42860Sstevel@tonic-gate 			if (svd->pageprot != 0 && IS_P2ALIGNED(a, maxpgsz)) {
42870Sstevel@tonic-gate 				VM_STAT_ADD(segvnvmstats.fltanpages[3]);
42880Sstevel@tonic-gate 				ASSERT(vpage != NULL);
42890Sstevel@tonic-gate 				prot = VPP_PROT(vpage);
42900Sstevel@tonic-gate 				ASSERT(sameprot(seg, a, maxpgsz));
42910Sstevel@tonic-gate 				if ((prot & protchk) == 0) {
42920Sstevel@tonic-gate 					err = FC_PROT;
42930Sstevel@tonic-gate 					goto error;
42940Sstevel@tonic-gate 				}
42950Sstevel@tonic-gate 			}
42962414Saguzovsk 			if (adjszc_chk && IS_P2ALIGNED(a, maxpgsz) &&
42972414Saguzovsk 			    pgsz < maxpgsz) {
42982414Saguzovsk 				ASSERT(a > lpgaddr);
42992414Saguzovsk 				szc = seg->s_szc;
43002414Saguzovsk 				pgsz = maxpgsz;
43012414Saguzovsk 				pages = btop(pgsz);
43022414Saguzovsk 				ASSERT(IS_P2ALIGNED(aindx, pages));
43032414Saguzovsk 				lpgeaddr = (caddr_t)P2ROUNDUP((uintptr_t)eaddr,
43042414Saguzovsk 				    pgsz);
43052414Saguzovsk 			}
43062414Saguzovsk 			if (type == F_SOFTLOCK && svd->vp != NULL) {
43070Sstevel@tonic-gate 				mutex_enter(&freemem_lock);
43080Sstevel@tonic-gate 				if (availrmem < tune.t_minarmem + pages) {
43090Sstevel@tonic-gate 					mutex_exit(&freemem_lock);
43100Sstevel@tonic-gate 					err = FC_MAKE_ERR(ENOMEM);
43110Sstevel@tonic-gate 					goto error;
43120Sstevel@tonic-gate 				} else {
43130Sstevel@tonic-gate 					availrmem -= pages;
43140Sstevel@tonic-gate 					segvn_pages_locked += pages;
43150Sstevel@tonic-gate 					svd->softlockcnt += pages;
43160Sstevel@tonic-gate 				}
43170Sstevel@tonic-gate 				mutex_exit(&freemem_lock);
43180Sstevel@tonic-gate 			}
43190Sstevel@tonic-gate 			anon_array_enter(amp, aindx, &cookie);
43200Sstevel@tonic-gate 			ppa_szc = (uint_t)-1;
43210Sstevel@tonic-gate 			ierr = anon_map_getpages(amp, aindx, szc, seg, a,
43220Sstevel@tonic-gate 				prot, &vpprot, ppa, &ppa_szc, vpage, rw, brkcow,
43230Sstevel@tonic-gate 				segvn_anypgsz, svd->cred);
43240Sstevel@tonic-gate 			if (ierr != 0) {
43250Sstevel@tonic-gate 				anon_array_exit(&cookie);
43260Sstevel@tonic-gate 				VM_STAT_ADD(segvnvmstats.fltanpages[4]);
43272414Saguzovsk 				if (type == F_SOFTLOCK && svd->vp != NULL) {
43280Sstevel@tonic-gate 					VM_STAT_ADD(segvnvmstats.fltanpages[5]);
43290Sstevel@tonic-gate 					mutex_enter(&freemem_lock);
43300Sstevel@tonic-gate 					availrmem += pages;
43310Sstevel@tonic-gate 					segvn_pages_locked -= pages;
43320Sstevel@tonic-gate 					svd->softlockcnt -= pages;
43330Sstevel@tonic-gate 					mutex_exit(&freemem_lock);
43340Sstevel@tonic-gate 				}
43350Sstevel@tonic-gate 				if (ierr > 0) {
43360Sstevel@tonic-gate 					VM_STAT_ADD(segvnvmstats.fltanpages[6]);
43370Sstevel@tonic-gate 					err = FC_MAKE_ERR(ierr);
43380Sstevel@tonic-gate 					goto error;
43390Sstevel@tonic-gate 				}
43400Sstevel@tonic-gate 				break;
43410Sstevel@tonic-gate 			}
43420Sstevel@tonic-gate 
43430Sstevel@tonic-gate 			ASSERT(!IS_VMODSORT(ppa[0]->p_vnode));
43440Sstevel@tonic-gate 
43452414Saguzovsk 			ASSERT(segtype == MAP_SHARED ||
43462414Saguzovsk 			    ppa[0]->p_szc <= szc);
43472414Saguzovsk 			ASSERT(segtype == MAP_PRIVATE ||
43482414Saguzovsk 			    ppa[0]->p_szc >= szc);
43492414Saguzovsk 
43502505Saguzovsk 			/*
43512505Saguzovsk 			 * Handle pages that have been marked for migration
43522505Saguzovsk 			 */
43532505Saguzovsk 			if (lgrp_optimizations())
43542505Saguzovsk 				page_migrate(seg, a, ppa, pages);
43552505Saguzovsk 
43562414Saguzovsk 			if (type == F_SOFTLOCK && svd->vp == NULL) {
43572414Saguzovsk 				/*
43582414Saguzovsk 				 * All pages in ppa array belong to the same
43592414Saguzovsk 				 * large page. This means it's ok to call
43602414Saguzovsk 				 * segvn_pp_lock_anonpages just for ppa[0].
43612414Saguzovsk 				 */
43622414Saguzovsk 				if (!segvn_pp_lock_anonpages(ppa[0], first)) {
43632414Saguzovsk 					for (i = 0; i < pages; i++) {
43642414Saguzovsk 						page_unlock(ppa[i]);
43652414Saguzovsk 					}
43662414Saguzovsk 					err = FC_MAKE_ERR(ENOMEM);
43672414Saguzovsk 					goto error;
43682414Saguzovsk 				}
43692414Saguzovsk 				first = 0;
43702414Saguzovsk 				mutex_enter(&freemem_lock);
43712414Saguzovsk 				svd->softlockcnt += pages;
43722414Saguzovsk 				segvn_pages_locked += pages;
43732414Saguzovsk 				mutex_exit(&freemem_lock);
43742414Saguzovsk 			}
43752414Saguzovsk 
43762414Saguzovsk 			if (segtype == MAP_SHARED) {
43772414Saguzovsk 				vpprot |= PROT_WRITE;
43782414Saguzovsk 			}
43792414Saguzovsk 
43800Sstevel@tonic-gate 			hat_memload_array(hat, a, pgsz, ppa,
43810Sstevel@tonic-gate 			    prot & vpprot, hat_flag);
43820Sstevel@tonic-gate 
43830Sstevel@tonic-gate 			if (hat_flag & HAT_LOAD_LOCK) {
43840Sstevel@tonic-gate 				VM_STAT_ADD(segvnvmstats.fltanpages[7]);
43850Sstevel@tonic-gate 			} else {
43860Sstevel@tonic-gate 				VM_STAT_ADD(segvnvmstats.fltanpages[8]);
43870Sstevel@tonic-gate 				for (i = 0; i < pages; i++)
43880Sstevel@tonic-gate 					page_unlock(ppa[i]);
43890Sstevel@tonic-gate 			}
43900Sstevel@tonic-gate 			if (vpage != NULL)
43910Sstevel@tonic-gate 				vpage += pages;
43920Sstevel@tonic-gate 
43930Sstevel@tonic-gate 			anon_array_exit(&cookie);
43942414Saguzovsk 			adjszc_chk = 1;
43950Sstevel@tonic-gate 		}
43960Sstevel@tonic-gate 		if (a == lpgeaddr)
43970Sstevel@tonic-gate 			break;
43980Sstevel@tonic-gate 		ASSERT(a < lpgeaddr);
43990Sstevel@tonic-gate 		/*
44000Sstevel@tonic-gate 		 * ierr == -1 means we failed to allocate a large page.
44010Sstevel@tonic-gate 		 * so do a size down operation.
44020Sstevel@tonic-gate 		 *
44030Sstevel@tonic-gate 		 * ierr == -2 means some other process that privately shares
44040Sstevel@tonic-gate 		 * pages with this process has allocated a larger page and we
44050Sstevel@tonic-gate 		 * need to retry with larger pages. So do a size up
44060Sstevel@tonic-gate 		 * operation. This relies on the fact that large pages are
44070Sstevel@tonic-gate 		 * never partially shared i.e. if we share any constituent
44080Sstevel@tonic-gate 		 * page of a large page with another process we must share the
44090Sstevel@tonic-gate 		 * entire large page. Note this cannot happen for SOFTLOCK
44100Sstevel@tonic-gate 		 * case, unless current address (a) is at the beginning of the
44110Sstevel@tonic-gate 		 * next page size boundary because the other process couldn't
44120Sstevel@tonic-gate 		 * have relocated locked pages.
44130Sstevel@tonic-gate 		 */
44140Sstevel@tonic-gate 		ASSERT(ierr == -1 || ierr == -2);
44152414Saguzovsk 		/*
44162414Saguzovsk 		 * For the very first relocation failure try to purge this
44172414Saguzovsk 		 * segment's cache so that the relocator can obtain an
44182414Saguzovsk 		 * exclusive lock on pages we want to relocate.
44192414Saguzovsk 		 */
44202414Saguzovsk 		if (!purged && ierr == -1 && ppa_szc != (uint_t)-1 &&
44212414Saguzovsk 		    svd->softlockcnt != 0) {
44222414Saguzovsk 			purged = 1;
44232414Saguzovsk 			segvn_purge(seg);
44242414Saguzovsk 			continue;
44252414Saguzovsk 		}
44262414Saguzovsk 
44270Sstevel@tonic-gate 		if (segvn_anypgsz) {
44280Sstevel@tonic-gate 			ASSERT(ierr == -2 || szc != 0);
44290Sstevel@tonic-gate 			ASSERT(ierr == -1 || szc < seg->s_szc);
44300Sstevel@tonic-gate 			szc = (ierr == -1) ? szc - 1 : szc + 1;
44310Sstevel@tonic-gate 		} else {
44320Sstevel@tonic-gate 			/*
44330Sstevel@tonic-gate 			 * For non COW faults and segvn_anypgsz == 0
44340Sstevel@tonic-gate 			 * we need to be careful not to loop forever
44350Sstevel@tonic-gate 			 * if existing page is found with szc other
44360Sstevel@tonic-gate 			 * than 0 or seg->s_szc. This could be due
44370Sstevel@tonic-gate 			 * to page relocations on behalf of DR or
44380Sstevel@tonic-gate 			 * more likely large page creation. For this
44390Sstevel@tonic-gate 			 * case simply re-size to existing page's szc
44400Sstevel@tonic-gate 			 * if returned by anon_map_getpages().
44410Sstevel@tonic-gate 			 */
44420Sstevel@tonic-gate 			if (ppa_szc == (uint_t)-1) {
44430Sstevel@tonic-gate 				szc = (ierr == -1) ? 0 : seg->s_szc;
44440Sstevel@tonic-gate 			} else {
44450Sstevel@tonic-gate 				ASSERT(ppa_szc <= seg->s_szc);
44460Sstevel@tonic-gate 				ASSERT(ierr == -2 || ppa_szc < szc);
44470Sstevel@tonic-gate 				ASSERT(ierr == -1 || ppa_szc > szc);
44480Sstevel@tonic-gate 				szc = ppa_szc;
44490Sstevel@tonic-gate 			}
44500Sstevel@tonic-gate 		}
44510Sstevel@tonic-gate 
44520Sstevel@tonic-gate 		pgsz = page_get_pagesize(szc);
44530Sstevel@tonic-gate 		pages = btop(pgsz);
44540Sstevel@tonic-gate 		ASSERT(type != F_SOFTLOCK || ierr == -1 ||
44550Sstevel@tonic-gate 		    (IS_P2ALIGNED(a, pgsz) && IS_P2ALIGNED(lpgeaddr, pgsz)));
44560Sstevel@tonic-gate 		if (type == F_SOFTLOCK) {
44570Sstevel@tonic-gate 			/*
44580Sstevel@tonic-gate 			 * For softlocks we cannot reduce the fault area
44590Sstevel@tonic-gate 			 * (calculated based on the largest page size for this
44600Sstevel@tonic-gate 			 * segment) for size down and a is already next
44610Sstevel@tonic-gate 			 * page size aligned as assertted above for size
44620Sstevel@tonic-gate 			 * ups. Therefore just continue in case of softlock.
44630Sstevel@tonic-gate 			 */
44640Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fltanpages[9]);
44650Sstevel@tonic-gate 			continue; /* keep lint happy */
44660Sstevel@tonic-gate 		} else if (ierr == -2) {
44670Sstevel@tonic-gate 
44680Sstevel@tonic-gate 			/*
44690Sstevel@tonic-gate 			 * Size up case. Note lpgaddr may only be needed for
44700Sstevel@tonic-gate 			 * softlock case so we don't adjust it here.
44710Sstevel@tonic-gate 			 */
44720Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fltanpages[10]);
44730Sstevel@tonic-gate 			a = (caddr_t)P2ALIGN((uintptr_t)a, pgsz);
44740Sstevel@tonic-gate 			ASSERT(a >= lpgaddr);
44750Sstevel@tonic-gate 			lpgeaddr = (caddr_t)P2ROUNDUP((uintptr_t)eaddr, pgsz);
44760Sstevel@tonic-gate 			aindx = svd->anon_index + seg_page(seg, a);
44770Sstevel@tonic-gate 			vpage = (svd->vpage != NULL) ?
44780Sstevel@tonic-gate 			    &svd->vpage[seg_page(seg, a)] : NULL;
44790Sstevel@tonic-gate 		} else {
44800Sstevel@tonic-gate 			/*
44810Sstevel@tonic-gate 			 * Size down case. Note lpgaddr may only be needed for
44820Sstevel@tonic-gate 			 * softlock case so we don't adjust it here.
44830Sstevel@tonic-gate 			 */
44840Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.fltanpages[11]);
44850Sstevel@tonic-gate 			ASSERT(IS_P2ALIGNED(a, pgsz));
44860Sstevel@tonic-gate 			ASSERT(IS_P2ALIGNED(lpgeaddr, pgsz));
44870Sstevel@tonic-gate 			lpgeaddr = (caddr_t)P2ROUNDUP((uintptr_t)eaddr, pgsz);
44880Sstevel@tonic-gate 			ASSERT(a < lpgeaddr);
44890Sstevel@tonic-gate 			if (a < addr) {
44900Sstevel@tonic-gate 				/*
44910Sstevel@tonic-gate 				 * The beginning of the large page region can
44920Sstevel@tonic-gate 				 * be pulled to the right to make a smaller
44930Sstevel@tonic-gate 				 * region. We haven't yet faulted a single
44940Sstevel@tonic-gate 				 * page.
44950Sstevel@tonic-gate 				 */
44960Sstevel@tonic-gate 				VM_STAT_ADD(segvnvmstats.fltanpages[12]);
44970Sstevel@tonic-gate 				a = (caddr_t)P2ALIGN((uintptr_t)addr, pgsz);
44980Sstevel@tonic-gate 				ASSERT(a >= lpgaddr);
44990Sstevel@tonic-gate 				aindx = svd->anon_index + seg_page(seg, a);
45000Sstevel@tonic-gate 				vpage = (svd->vpage != NULL) ?
45010Sstevel@tonic-gate 				    &svd->vpage[seg_page(seg, a)] : NULL;
45020Sstevel@tonic-gate 			}
45030Sstevel@tonic-gate 		}
45040Sstevel@tonic-gate 	}
45050Sstevel@tonic-gate 	VM_STAT_ADD(segvnvmstats.fltanpages[13]);
45060Sstevel@tonic-gate 	ANON_LOCK_EXIT(&amp->a_rwlock);
45070Sstevel@tonic-gate 	kmem_free(ppa, ppasize);
45080Sstevel@tonic-gate 	return (0);
45090Sstevel@tonic-gate error:
45100Sstevel@tonic-gate 	VM_STAT_ADD(segvnvmstats.fltanpages[14]);
45110Sstevel@tonic-gate 	ANON_LOCK_EXIT(&amp->a_rwlock);
45120Sstevel@tonic-gate 	kmem_free(ppa, ppasize);
45130Sstevel@tonic-gate 	if (type == F_SOFTLOCK && a > lpgaddr) {
45140Sstevel@tonic-gate 		VM_STAT_ADD(segvnvmstats.fltanpages[15]);
45150Sstevel@tonic-gate 		segvn_softunlock(seg, lpgaddr, a - lpgaddr, S_OTHER);
45160Sstevel@tonic-gate 	}
45170Sstevel@tonic-gate 	return (err);
45180Sstevel@tonic-gate }
45190Sstevel@tonic-gate 
45200Sstevel@tonic-gate int fltadvice = 1;	/* set to free behind pages for sequential access */
45210Sstevel@tonic-gate 
45220Sstevel@tonic-gate /*
45230Sstevel@tonic-gate  * This routine is called via a machine specific fault handling routine.
45240Sstevel@tonic-gate  * It is also called by software routines wishing to lock or unlock
45250Sstevel@tonic-gate  * a range of addresses.
45260Sstevel@tonic-gate  *
45270Sstevel@tonic-gate  * Here is the basic algorithm:
45280Sstevel@tonic-gate  *	If unlocking
45290Sstevel@tonic-gate  *		Call segvn_softunlock
45300Sstevel@tonic-gate  *		Return
45310Sstevel@tonic-gate  *	endif
45320Sstevel@tonic-gate  *	Checking and set up work
45330Sstevel@tonic-gate  *	If we will need some non-anonymous pages
45340Sstevel@tonic-gate  *		Call VOP_GETPAGE over the range of non-anonymous pages
45350Sstevel@tonic-gate  *	endif
45360Sstevel@tonic-gate  *	Loop over all addresses requested
45370Sstevel@tonic-gate  *		Call segvn_faultpage passing in page list
45380Sstevel@tonic-gate  *		    to load up translations and handle anonymous pages
45390Sstevel@tonic-gate  *	endloop
45400Sstevel@tonic-gate  *	Load up translation to any additional pages in page list not
45410Sstevel@tonic-gate  *	    already handled that fit into this segment
45420Sstevel@tonic-gate  */
45430Sstevel@tonic-gate static faultcode_t
45440Sstevel@tonic-gate segvn_fault(struct hat *hat, struct seg *seg, caddr_t addr, size_t len,
45450Sstevel@tonic-gate     enum fault_type type, enum seg_rw rw)
45460Sstevel@tonic-gate {
45470Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
45480Sstevel@tonic-gate 	page_t **plp, **ppp, *pp;
45490Sstevel@tonic-gate 	u_offset_t off;
45500Sstevel@tonic-gate 	caddr_t a;
45510Sstevel@tonic-gate 	struct vpage *vpage;
45520Sstevel@tonic-gate 	uint_t vpprot, prot;
45530Sstevel@tonic-gate 	int err;
45540Sstevel@tonic-gate 	page_t *pl[PVN_GETPAGE_NUM + 1];
45550Sstevel@tonic-gate 	size_t plsz, pl_alloc_sz;
45560Sstevel@tonic-gate 	size_t page;
45570Sstevel@tonic-gate 	ulong_t anon_index;
45580Sstevel@tonic-gate 	struct anon_map *amp;
45590Sstevel@tonic-gate 	int dogetpage = 0;
45600Sstevel@tonic-gate 	caddr_t	lpgaddr, lpgeaddr;
45610Sstevel@tonic-gate 	size_t pgsz;
45620Sstevel@tonic-gate 	anon_sync_obj_t cookie;
45630Sstevel@tonic-gate 	int brkcow = BREAK_COW_SHARE(rw, type, svd->type);
45640Sstevel@tonic-gate 
45650Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
45660Sstevel@tonic-gate 
45670Sstevel@tonic-gate 	/*
45680Sstevel@tonic-gate 	 * First handle the easy stuff
45690Sstevel@tonic-gate 	 */
45700Sstevel@tonic-gate 	if (type == F_SOFTUNLOCK) {
457163Saguzovsk 		if (rw == S_READ_NOCOW) {
457263Saguzovsk 			rw = S_READ;
457363Saguzovsk 			ASSERT(AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
457463Saguzovsk 		}
45750Sstevel@tonic-gate 		SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
45760Sstevel@tonic-gate 		pgsz = (seg->s_szc == 0) ? PAGESIZE :
45770Sstevel@tonic-gate 		    page_get_pagesize(seg->s_szc);
45780Sstevel@tonic-gate 		VM_STAT_COND_ADD(pgsz > PAGESIZE, segvnvmstats.fltanpages[16]);
45790Sstevel@tonic-gate 		CALC_LPG_REGION(pgsz, seg, addr, len, lpgaddr, lpgeaddr);
45800Sstevel@tonic-gate 		segvn_softunlock(seg, lpgaddr, lpgeaddr - lpgaddr, rw);
45810Sstevel@tonic-gate 		SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
45820Sstevel@tonic-gate 		return (0);
45830Sstevel@tonic-gate 	}
45840Sstevel@tonic-gate 
45850Sstevel@tonic-gate top:
45860Sstevel@tonic-gate 	SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
45870Sstevel@tonic-gate 
45880Sstevel@tonic-gate 	/*
45890Sstevel@tonic-gate 	 * If we have the same protections for the entire segment,
45900Sstevel@tonic-gate 	 * insure that the access being attempted is legitimate.
45910Sstevel@tonic-gate 	 */
45920Sstevel@tonic-gate 
45930Sstevel@tonic-gate 	if (svd->pageprot == 0) {
45940Sstevel@tonic-gate 		uint_t protchk;
45950Sstevel@tonic-gate 
45960Sstevel@tonic-gate 		switch (rw) {
45970Sstevel@tonic-gate 		case S_READ:
459863Saguzovsk 		case S_READ_NOCOW:
45990Sstevel@tonic-gate 			protchk = PROT_READ;
46000Sstevel@tonic-gate 			break;
46010Sstevel@tonic-gate 		case S_WRITE:
46020Sstevel@tonic-gate 			protchk = PROT_WRITE;
46030Sstevel@tonic-gate 			break;
46040Sstevel@tonic-gate 		case S_EXEC:
46050Sstevel@tonic-gate 			protchk = PROT_EXEC;
46060Sstevel@tonic-gate 			break;
46070Sstevel@tonic-gate 		case S_OTHER:
46080Sstevel@tonic-gate 		default:
46090Sstevel@tonic-gate 			protchk = PROT_READ | PROT_WRITE | PROT_EXEC;
46100Sstevel@tonic-gate 			break;
46110Sstevel@tonic-gate 		}
46120Sstevel@tonic-gate 
46130Sstevel@tonic-gate 		if ((svd->prot & protchk) == 0) {
46140Sstevel@tonic-gate 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
46150Sstevel@tonic-gate 			return (FC_PROT);	/* illegal access type */
46160Sstevel@tonic-gate 		}
46170Sstevel@tonic-gate 	}
46180Sstevel@tonic-gate 
46190Sstevel@tonic-gate 	/*
462063Saguzovsk 	 * We can't allow the long term use of softlocks for vmpss segments,
462163Saguzovsk 	 * because in some file truncation cases we should be able to demote
462263Saguzovsk 	 * the segment, which requires that there are no softlocks.  The
462363Saguzovsk 	 * only case where it's ok to allow a SOFTLOCK fault against a vmpss
462463Saguzovsk 	 * segment is S_READ_NOCOW, where the caller holds the address space
462563Saguzovsk 	 * locked as writer and calls softunlock before dropping the as lock.
462663Saguzovsk 	 * S_READ_NOCOW is used by /proc to read memory from another user.
462763Saguzovsk 	 *
462863Saguzovsk 	 * Another deadlock between SOFTLOCK and file truncation can happen
462963Saguzovsk 	 * because segvn_fault_vnodepages() calls the FS one pagesize at
463063Saguzovsk 	 * a time. A second VOP_GETPAGE() call by segvn_fault_vnodepages()
463163Saguzovsk 	 * can cause a deadlock because the first set of page_t's remain
463263Saguzovsk 	 * locked SE_SHARED.  To avoid this, we demote segments on a first
463363Saguzovsk 	 * SOFTLOCK if they have a length greater than the segment's
463463Saguzovsk 	 * page size.
463563Saguzovsk 	 *
463663Saguzovsk 	 * So for now, we only avoid demoting a segment on a SOFTLOCK when
463763Saguzovsk 	 * the access type is S_READ_NOCOW and the fault length is less than
463863Saguzovsk 	 * or equal to the segment's page size. While this is quite restrictive,
463963Saguzovsk 	 * it should be the most common case of SOFTLOCK against a vmpss
464063Saguzovsk 	 * segment.
464163Saguzovsk 	 *
464263Saguzovsk 	 * For S_READ_NOCOW, it's safe not to do a copy on write because the
464363Saguzovsk 	 * caller makes sure no COW will be caused by another thread for a
464463Saguzovsk 	 * softlocked page.
464563Saguzovsk 	 */
464663Saguzovsk 	if (type == F_SOFTLOCK && svd->vp != NULL && seg->s_szc != 0) {
464763Saguzovsk 		int demote = 0;
464863Saguzovsk 
464963Saguzovsk 		if (rw != S_READ_NOCOW) {
465063Saguzovsk 			demote = 1;
465163Saguzovsk 		}
465263Saguzovsk 		if (!demote && len > PAGESIZE) {
465363Saguzovsk 			pgsz = page_get_pagesize(seg->s_szc);
465463Saguzovsk 			CALC_LPG_REGION(pgsz, seg, addr, len, lpgaddr,
465563Saguzovsk 			    lpgeaddr);
465663Saguzovsk 			if (lpgeaddr - lpgaddr > pgsz) {
465763Saguzovsk 				demote = 1;
465863Saguzovsk 			}
465963Saguzovsk 		}
466063Saguzovsk 
466163Saguzovsk 		ASSERT(demote || AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
466263Saguzovsk 
466363Saguzovsk 		if (demote) {
466463Saguzovsk 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
466563Saguzovsk 			SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_WRITER);
466663Saguzovsk 			if (seg->s_szc != 0) {
466763Saguzovsk 				segvn_vmpss_clrszc_cnt++;
466863Saguzovsk 				ASSERT(svd->softlockcnt == 0);
466963Saguzovsk 				err = segvn_clrszc(seg);
467063Saguzovsk 				if (err) {
467163Saguzovsk 					segvn_vmpss_clrszc_err++;
467263Saguzovsk 					SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
467363Saguzovsk 					return (FC_MAKE_ERR(err));
467463Saguzovsk 				}
467563Saguzovsk 			}
467663Saguzovsk 			ASSERT(seg->s_szc == 0);
467763Saguzovsk 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
467863Saguzovsk 			goto top;
467963Saguzovsk 		}
468063Saguzovsk 	}
468163Saguzovsk 
468263Saguzovsk 	/*
46830Sstevel@tonic-gate 	 * Check to see if we need to allocate an anon_map structure.
46840Sstevel@tonic-gate 	 */
46850Sstevel@tonic-gate 	if (svd->amp == NULL && (svd->vp == NULL || brkcow)) {
46860Sstevel@tonic-gate 		/*
46870Sstevel@tonic-gate 		 * Drop the "read" lock on the segment and acquire
46880Sstevel@tonic-gate 		 * the "write" version since we have to allocate the
46890Sstevel@tonic-gate 		 * anon_map.
46900Sstevel@tonic-gate 		 */
46910Sstevel@tonic-gate 		SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
46920Sstevel@tonic-gate 		SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_WRITER);
46930Sstevel@tonic-gate 
46940Sstevel@tonic-gate 		if (svd->amp == NULL) {
46950Sstevel@tonic-gate 			svd->amp = anonmap_alloc(seg->s_size, 0);
46960Sstevel@tonic-gate 			svd->amp->a_szc = seg->s_szc;
46970Sstevel@tonic-gate 		}
46980Sstevel@tonic-gate 		SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
46990Sstevel@tonic-gate 
47000Sstevel@tonic-gate 		/*
47010Sstevel@tonic-gate 		 * Start all over again since segment protections
47020Sstevel@tonic-gate 		 * may have changed after we dropped the "read" lock.
47030Sstevel@tonic-gate 		 */
47040Sstevel@tonic-gate 		goto top;
47050Sstevel@tonic-gate 	}
47060Sstevel@tonic-gate 
4707329Saguzovsk 	/*
4708329Saguzovsk 	 * S_READ_NOCOW vs S_READ distinction was
4709329Saguzovsk 	 * only needed for the code above. After
4710329Saguzovsk 	 * that we treat it as S_READ.
4711329Saguzovsk 	 */
4712329Saguzovsk 	if (rw == S_READ_NOCOW) {
4713329Saguzovsk 		ASSERT(type == F_SOFTLOCK);
4714329Saguzovsk 		ASSERT(AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
4715329Saguzovsk 		rw = S_READ;
4716329Saguzovsk 	}
4717329Saguzovsk 
47180Sstevel@tonic-gate 	amp = svd->amp;
47190Sstevel@tonic-gate 
47200Sstevel@tonic-gate 	/*
47210Sstevel@tonic-gate 	 * MADV_SEQUENTIAL work is ignored for large page segments.
47220Sstevel@tonic-gate 	 */
47230Sstevel@tonic-gate 	if (seg->s_szc != 0) {
47240Sstevel@tonic-gate 		pgsz = page_get_pagesize(seg->s_szc);
47250Sstevel@tonic-gate 		ASSERT(SEGVN_LOCK_HELD(seg->s_as, &svd->lock));
47260Sstevel@tonic-gate 		CALC_LPG_REGION(pgsz, seg, addr, len, lpgaddr, lpgeaddr);
47270Sstevel@tonic-gate 		if (svd->vp == NULL) {
47280Sstevel@tonic-gate 			err = segvn_fault_anonpages(hat, seg, lpgaddr,
47290Sstevel@tonic-gate 			    lpgeaddr, type, rw, addr, addr + len, brkcow);
47300Sstevel@tonic-gate 		} else {
47310Sstevel@tonic-gate 			err = segvn_fault_vnodepages(hat, seg, lpgaddr,
47320Sstevel@tonic-gate 				lpgeaddr, type, rw, addr, addr + len, brkcow);
47330Sstevel@tonic-gate 			if (err == IE_RETRY) {
47340Sstevel@tonic-gate 				ASSERT(seg->s_szc == 0);
47350Sstevel@tonic-gate 				ASSERT(SEGVN_READ_HELD(seg->s_as, &svd->lock));
473663Saguzovsk 				SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
473763Saguzovsk 				goto top;
47380Sstevel@tonic-gate 			}
47390Sstevel@tonic-gate 		}
47400Sstevel@tonic-gate 		SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
47410Sstevel@tonic-gate 		return (err);
47420Sstevel@tonic-gate 	}
47430Sstevel@tonic-gate 
47440Sstevel@tonic-gate 	page = seg_page(seg, addr);
47450Sstevel@tonic-gate 	if (amp != NULL) {
47460Sstevel@tonic-gate 		anon_index = svd->anon_index + page;
47470Sstevel@tonic-gate 
47480Sstevel@tonic-gate 		if ((type == F_PROT) && (rw == S_READ) &&
47490Sstevel@tonic-gate 		    svd->type == MAP_PRIVATE && svd->pageprot == 0) {
47500Sstevel@tonic-gate 			size_t index = anon_index;
47510Sstevel@tonic-gate 			struct anon *ap;
47520Sstevel@tonic-gate 
47530Sstevel@tonic-gate 			ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
47540Sstevel@tonic-gate 			/*
47550Sstevel@tonic-gate 			 * The fast path could apply to S_WRITE also, except
47560Sstevel@tonic-gate 			 * that the protection fault could be caused by lazy
47570Sstevel@tonic-gate 			 * tlb flush when ro->rw. In this case, the pte is
47580Sstevel@tonic-gate 			 * RW already. But RO in the other cpu's tlb causes
47590Sstevel@tonic-gate 			 * the fault. Since hat_chgprot won't do anything if
47600Sstevel@tonic-gate 			 * pte doesn't change, we may end up faulting
47610Sstevel@tonic-gate 			 * indefinitely until the RO tlb entry gets replaced.
47620Sstevel@tonic-gate 			 */
47630Sstevel@tonic-gate 			for (a = addr; a < addr + len; a += PAGESIZE, index++) {
47640Sstevel@tonic-gate 				anon_array_enter(amp, index, &cookie);
47650Sstevel@tonic-gate 				ap = anon_get_ptr(amp->ahp, index);
47660Sstevel@tonic-gate 				anon_array_exit(&cookie);
47670Sstevel@tonic-gate 				if ((ap == NULL) || (ap->an_refcnt != 1)) {
47680Sstevel@tonic-gate 					ANON_LOCK_EXIT(&amp->a_rwlock);
47690Sstevel@tonic-gate 					goto slow;
47700Sstevel@tonic-gate 				}
47710Sstevel@tonic-gate 			}
47720Sstevel@tonic-gate 			hat_chgprot(seg->s_as->a_hat, addr, len, svd->prot);
47730Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
47740Sstevel@tonic-gate 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
47750Sstevel@tonic-gate 			return (0);
47760Sstevel@tonic-gate 		}
47770Sstevel@tonic-gate 	}
47780Sstevel@tonic-gate slow:
47790Sstevel@tonic-gate 
47800Sstevel@tonic-gate 	if (svd->vpage == NULL)
47810Sstevel@tonic-gate 		vpage = NULL;
47820Sstevel@tonic-gate 	else
47830Sstevel@tonic-gate 		vpage = &svd->vpage[page];
47840Sstevel@tonic-gate 
47850Sstevel@tonic-gate 	off = svd->offset + (uintptr_t)(addr - seg->s_base);
47860Sstevel@tonic-gate 
47870Sstevel@tonic-gate 	/*
47880Sstevel@tonic-gate 	 * If MADV_SEQUENTIAL has been set for the particular page we
47890Sstevel@tonic-gate 	 * are faulting on, free behind all pages in the segment and put
47900Sstevel@tonic-gate 	 * them on the free list.
47910Sstevel@tonic-gate 	 */
47920Sstevel@tonic-gate 	if ((page != 0) && fltadvice) {	/* not if first page in segment */
47930Sstevel@tonic-gate 		struct vpage *vpp;
47940Sstevel@tonic-gate 		ulong_t fanon_index;
47950Sstevel@tonic-gate 		size_t fpage;
47960Sstevel@tonic-gate 		u_offset_t pgoff, fpgoff;
47970Sstevel@tonic-gate 		struct vnode *fvp;
47980Sstevel@tonic-gate 		struct anon *fap = NULL;
47990Sstevel@tonic-gate 
48000Sstevel@tonic-gate 		if (svd->advice == MADV_SEQUENTIAL ||
48010Sstevel@tonic-gate 		    (svd->pageadvice &&
48020Sstevel@tonic-gate 		    VPP_ADVICE(vpage) == MADV_SEQUENTIAL)) {
48030Sstevel@tonic-gate 			pgoff = off - PAGESIZE;
48040Sstevel@tonic-gate 			fpage = page - 1;
48050Sstevel@tonic-gate 			if (vpage != NULL)
48060Sstevel@tonic-gate 				vpp = &svd->vpage[fpage];
48070Sstevel@tonic-gate 			if (amp != NULL)
48080Sstevel@tonic-gate 				fanon_index = svd->anon_index + fpage;
48090Sstevel@tonic-gate 
48100Sstevel@tonic-gate 			while (pgoff > svd->offset) {
48110Sstevel@tonic-gate 				if (svd->advice != MADV_SEQUENTIAL &&
48120Sstevel@tonic-gate 				    (!svd->pageadvice || (vpage &&
48130Sstevel@tonic-gate 				    VPP_ADVICE(vpp) != MADV_SEQUENTIAL)))
48140Sstevel@tonic-gate 					break;
48150Sstevel@tonic-gate 
48160Sstevel@tonic-gate 				/*
48170Sstevel@tonic-gate 				 * If this is an anon page, we must find the
48180Sstevel@tonic-gate 				 * correct <vp, offset> for it
48190Sstevel@tonic-gate 				 */
48200Sstevel@tonic-gate 				fap = NULL;
48210Sstevel@tonic-gate 				if (amp != NULL) {
48220Sstevel@tonic-gate 					ANON_LOCK_ENTER(&amp->a_rwlock,
48230Sstevel@tonic-gate 						RW_READER);
48240Sstevel@tonic-gate 					anon_array_enter(amp, fanon_index,
48250Sstevel@tonic-gate 						&cookie);
48260Sstevel@tonic-gate 					fap = anon_get_ptr(amp->ahp,
48270Sstevel@tonic-gate 					    fanon_index);
48280Sstevel@tonic-gate 					if (fap != NULL) {
48290Sstevel@tonic-gate 						swap_xlate(fap, &fvp, &fpgoff);
48300Sstevel@tonic-gate 					} else {
48310Sstevel@tonic-gate 						fpgoff = pgoff;
48320Sstevel@tonic-gate 						fvp = svd->vp;
48330Sstevel@tonic-gate 					}
48340Sstevel@tonic-gate 					anon_array_exit(&cookie);
48350Sstevel@tonic-gate 					ANON_LOCK_EXIT(&amp->a_rwlock);
48360Sstevel@tonic-gate 				} else {
48370Sstevel@tonic-gate 					fpgoff = pgoff;
48380Sstevel@tonic-gate 					fvp = svd->vp;
48390Sstevel@tonic-gate 				}
48400Sstevel@tonic-gate 				if (fvp == NULL)
48410Sstevel@tonic-gate 					break;	/* XXX */
48420Sstevel@tonic-gate 				/*
48430Sstevel@tonic-gate 				 * Skip pages that are free or have an
48440Sstevel@tonic-gate 				 * "exclusive" lock.
48450Sstevel@tonic-gate 				 */
48460Sstevel@tonic-gate 				pp = page_lookup_nowait(fvp, fpgoff, SE_SHARED);
48470Sstevel@tonic-gate 				if (pp == NULL)
48480Sstevel@tonic-gate 					break;
48490Sstevel@tonic-gate 				/*
48500Sstevel@tonic-gate 				 * We don't need the page_struct_lock to test
48510Sstevel@tonic-gate 				 * as this is only advisory; even if we
48520Sstevel@tonic-gate 				 * acquire it someone might race in and lock
48530Sstevel@tonic-gate 				 * the page after we unlock and before the
48540Sstevel@tonic-gate 				 * PUTPAGE, then VOP_PUTPAGE will do nothing.
48550Sstevel@tonic-gate 				 */
48560Sstevel@tonic-gate 				if (pp->p_lckcnt == 0 && pp->p_cowcnt == 0) {
48570Sstevel@tonic-gate 					/*
48580Sstevel@tonic-gate 					 * Hold the vnode before releasing
48590Sstevel@tonic-gate 					 * the page lock to prevent it from
48600Sstevel@tonic-gate 					 * being freed and re-used by some
48610Sstevel@tonic-gate 					 * other thread.
48620Sstevel@tonic-gate 					 */
48630Sstevel@tonic-gate 					VN_HOLD(fvp);
48640Sstevel@tonic-gate 					page_unlock(pp);
48650Sstevel@tonic-gate 					/*
48660Sstevel@tonic-gate 					 * We should build a page list
48670Sstevel@tonic-gate 					 * to kluster putpages XXX
48680Sstevel@tonic-gate 					 */
48690Sstevel@tonic-gate 					(void) VOP_PUTPAGE(fvp,
48700Sstevel@tonic-gate 					    (offset_t)fpgoff, PAGESIZE,
48710Sstevel@tonic-gate 					    (B_DONTNEED|B_FREE|B_ASYNC),
48720Sstevel@tonic-gate 					    svd->cred);
48730Sstevel@tonic-gate 					VN_RELE(fvp);
48740Sstevel@tonic-gate 				} else {
48750Sstevel@tonic-gate 					/*
48760Sstevel@tonic-gate 					 * XXX - Should the loop terminate if
48770Sstevel@tonic-gate 					 * the page is `locked'?
48780Sstevel@tonic-gate 					 */
48790Sstevel@tonic-gate 					page_unlock(pp);
48800Sstevel@tonic-gate 				}
48810Sstevel@tonic-gate 				--vpp;
48820Sstevel@tonic-gate 				--fanon_index;
48830Sstevel@tonic-gate 				pgoff -= PAGESIZE;
48840Sstevel@tonic-gate 			}
48850Sstevel@tonic-gate 		}
48860Sstevel@tonic-gate 	}
48870Sstevel@tonic-gate 
48880Sstevel@tonic-gate 	plp = pl;
48890Sstevel@tonic-gate 	*plp = NULL;
48900Sstevel@tonic-gate 	pl_alloc_sz = 0;
48910Sstevel@tonic-gate 
48920Sstevel@tonic-gate 	/*
48930Sstevel@tonic-gate 	 * See if we need to call VOP_GETPAGE for
48940Sstevel@tonic-gate 	 * *any* of the range being faulted on.
48950Sstevel@tonic-gate 	 * We can skip all of this work if there
48960Sstevel@tonic-gate 	 * was no original vnode.
48970Sstevel@tonic-gate 	 */
48980Sstevel@tonic-gate 	if (svd->vp != NULL) {
48990Sstevel@tonic-gate 		u_offset_t vp_off;
49000Sstevel@tonic-gate 		size_t vp_len;
49010Sstevel@tonic-gate 		struct anon *ap;
49020Sstevel@tonic-gate 		vnode_t *vp;
49030Sstevel@tonic-gate 
49040Sstevel@tonic-gate 		vp_off = off;
49050Sstevel@tonic-gate 		vp_len = len;
49060Sstevel@tonic-gate 
49070Sstevel@tonic-gate 		if (amp == NULL)
49080Sstevel@tonic-gate 			dogetpage = 1;
49090Sstevel@tonic-gate 		else {
49100Sstevel@tonic-gate 			/*
49110Sstevel@tonic-gate 			 * Only acquire reader lock to prevent amp->ahp
49120Sstevel@tonic-gate 			 * from being changed.  It's ok to miss pages,
49130Sstevel@tonic-gate 			 * hence we don't do anon_array_enter
49140Sstevel@tonic-gate 			 */
49150Sstevel@tonic-gate 			ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
49160Sstevel@tonic-gate 			ap = anon_get_ptr(amp->ahp, anon_index);
49170Sstevel@tonic-gate 
49180Sstevel@tonic-gate 			if (len <= PAGESIZE)
49190Sstevel@tonic-gate 				/* inline non_anon() */
49200Sstevel@tonic-gate 				dogetpage = (ap == NULL);
49210Sstevel@tonic-gate 			else
49220Sstevel@tonic-gate 				dogetpage = non_anon(amp->ahp, anon_index,
49230Sstevel@tonic-gate 				    &vp_off, &vp_len);
49240Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
49250Sstevel@tonic-gate 		}
49260Sstevel@tonic-gate 
49270Sstevel@tonic-gate 		if (dogetpage) {
49280Sstevel@tonic-gate 			enum seg_rw arw;
49290Sstevel@tonic-gate 			struct as *as = seg->s_as;
49300Sstevel@tonic-gate 
49310Sstevel@tonic-gate 			if (len > ptob((sizeof (pl) / sizeof (pl[0])) - 1)) {
49320Sstevel@tonic-gate 				/*
49330Sstevel@tonic-gate 				 * Page list won't fit in local array,
49340Sstevel@tonic-gate 				 * allocate one of the needed size.
49350Sstevel@tonic-gate 				 */
49360Sstevel@tonic-gate 				pl_alloc_sz =
49370Sstevel@tonic-gate 				    (btop(len) + 1) * sizeof (page_t *);
49380Sstevel@tonic-gate 				plp = kmem_alloc(pl_alloc_sz, KM_SLEEP);
49390Sstevel@tonic-gate 				plp[0] = NULL;
49400Sstevel@tonic-gate 				plsz = len;
49410Sstevel@tonic-gate 			} else if (rw == S_WRITE && svd->type == MAP_PRIVATE ||
49420Sstevel@tonic-gate 			    rw == S_OTHER ||
49430Sstevel@tonic-gate 			    (((size_t)(addr + PAGESIZE) <
49440Sstevel@tonic-gate 			    (size_t)(seg->s_base + seg->s_size)) &&
49450Sstevel@tonic-gate 			    hat_probe(as->a_hat, addr + PAGESIZE))) {
49460Sstevel@tonic-gate 				/*
49470Sstevel@tonic-gate 				 * Ask VOP_GETPAGE to return the exact number
49480Sstevel@tonic-gate 				 * of pages if
49490Sstevel@tonic-gate 				 * (a) this is a COW fault, or
49500Sstevel@tonic-gate 				 * (b) this is a software fault, or
49510Sstevel@tonic-gate 				 * (c) next page is already mapped.
49520Sstevel@tonic-gate 				 */
49530Sstevel@tonic-gate 				plsz = len;
49540Sstevel@tonic-gate 			} else {
49550Sstevel@tonic-gate 				/*
49560Sstevel@tonic-gate 				 * Ask VOP_GETPAGE to return adjacent pages
49570Sstevel@tonic-gate 				 * within the segment.
49580Sstevel@tonic-gate 				 */
49590Sstevel@tonic-gate 				plsz = MIN((size_t)PVN_GETPAGE_SZ, (size_t)
49600Sstevel@tonic-gate 					((seg->s_base + seg->s_size) - addr));
49610Sstevel@tonic-gate 				ASSERT((addr + plsz) <=
49620Sstevel@tonic-gate 				    (seg->s_base + seg->s_size));
49630Sstevel@tonic-gate 			}
49640Sstevel@tonic-gate 
49650Sstevel@tonic-gate 			/*
49660Sstevel@tonic-gate 			 * Need to get some non-anonymous pages.
49670Sstevel@tonic-gate 			 * We need to make only one call to GETPAGE to do
49680Sstevel@tonic-gate 			 * this to prevent certain deadlocking conditions
49690Sstevel@tonic-gate 			 * when we are doing locking.  In this case
49700Sstevel@tonic-gate 			 * non_anon() should have picked up the smallest
49710Sstevel@tonic-gate 			 * range which includes all the non-anonymous
49720Sstevel@tonic-gate 			 * pages in the requested range.  We have to
49730Sstevel@tonic-gate 			 * be careful regarding which rw flag to pass in
49740Sstevel@tonic-gate 			 * because on a private mapping, the underlying
49750Sstevel@tonic-gate 			 * object is never allowed to be written.
49760Sstevel@tonic-gate 			 */
49770Sstevel@tonic-gate 			if (rw == S_WRITE && svd->type == MAP_PRIVATE) {
49780Sstevel@tonic-gate 				arw = S_READ;
49790Sstevel@tonic-gate 			} else {
49800Sstevel@tonic-gate 				arw = rw;
49810Sstevel@tonic-gate 			}
49820Sstevel@tonic-gate 			vp = svd->vp;
49830Sstevel@tonic-gate 			TRACE_3(TR_FAC_VM, TR_SEGVN_GETPAGE,
49840Sstevel@tonic-gate 				"segvn_getpage:seg %p addr %p vp %p",
49850Sstevel@tonic-gate 				seg, addr, vp);
49860Sstevel@tonic-gate 			err = VOP_GETPAGE(vp, (offset_t)vp_off, vp_len,
49870Sstevel@tonic-gate 			    &vpprot, plp, plsz, seg, addr + (vp_off - off), arw,
49880Sstevel@tonic-gate 			    svd->cred);
49890Sstevel@tonic-gate 			if (err) {
49900Sstevel@tonic-gate 				SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
49910Sstevel@tonic-gate 				segvn_pagelist_rele(plp);
49920Sstevel@tonic-gate 				if (pl_alloc_sz)
49930Sstevel@tonic-gate 					kmem_free(plp, pl_alloc_sz);
49940Sstevel@tonic-gate 				return (FC_MAKE_ERR(err));
49950Sstevel@tonic-gate 			}
49960Sstevel@tonic-gate 			if (svd->type == MAP_PRIVATE)
49970Sstevel@tonic-gate 				vpprot &= ~PROT_WRITE;
49980Sstevel@tonic-gate 		}
49990Sstevel@tonic-gate 	}
50000Sstevel@tonic-gate 
50010Sstevel@tonic-gate 	/*
50020Sstevel@tonic-gate 	 * N.B. at this time the plp array has all the needed non-anon
50030Sstevel@tonic-gate 	 * pages in addition to (possibly) having some adjacent pages.
50040Sstevel@tonic-gate 	 */
50050Sstevel@tonic-gate 
50060Sstevel@tonic-gate 	/*
50070Sstevel@tonic-gate 	 * Always acquire the anon_array_lock to prevent
50080Sstevel@tonic-gate 	 * 2 threads from allocating separate anon slots for
50090Sstevel@tonic-gate 	 * the same "addr".
50100Sstevel@tonic-gate 	 *
50110Sstevel@tonic-gate 	 * If this is a copy-on-write fault and we don't already
50120Sstevel@tonic-gate 	 * have the anon_array_lock, acquire it to prevent the
50130Sstevel@tonic-gate 	 * fault routine from handling multiple copy-on-write faults
50140Sstevel@tonic-gate 	 * on the same "addr" in the same address space.
50150Sstevel@tonic-gate 	 *
50160Sstevel@tonic-gate 	 * Only one thread should deal with the fault since after
50170Sstevel@tonic-gate 	 * it is handled, the other threads can acquire a translation
50180Sstevel@tonic-gate 	 * to the newly created private page.  This prevents two or
50190Sstevel@tonic-gate 	 * more threads from creating different private pages for the
50200Sstevel@tonic-gate 	 * same fault.
50210Sstevel@tonic-gate 	 *
50220Sstevel@tonic-gate 	 * We grab "serialization" lock here if this is a MAP_PRIVATE segment
50230Sstevel@tonic-gate 	 * to prevent deadlock between this thread and another thread
50240Sstevel@tonic-gate 	 * which has soft-locked this page and wants to acquire serial_lock.
50250Sstevel@tonic-gate 	 * ( bug 4026339 )
50260Sstevel@tonic-gate 	 *
50270Sstevel@tonic-gate 	 * The fix for bug 4026339 becomes unnecessary when using the
50280Sstevel@tonic-gate 	 * locking scheme with per amp rwlock and a global set of hash
50290Sstevel@tonic-gate 	 * lock, anon_array_lock.  If we steal a vnode page when low
50300Sstevel@tonic-gate 	 * on memory and upgrad the page lock through page_rename,
50310Sstevel@tonic-gate 	 * then the page is PAGE_HANDLED, nothing needs to be done
50320Sstevel@tonic-gate 	 * for this page after returning from segvn_faultpage.
50330Sstevel@tonic-gate 	 *
50340Sstevel@tonic-gate 	 * But really, the page lock should be downgraded after
50350Sstevel@tonic-gate 	 * the stolen page is page_rename'd.
50360Sstevel@tonic-gate 	 */
50370Sstevel@tonic-gate 
50380Sstevel@tonic-gate 	if (amp != NULL)
50390Sstevel@tonic-gate 		ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
50400Sstevel@tonic-gate 
50410Sstevel@tonic-gate 	/*
50420Sstevel@tonic-gate 	 * Ok, now loop over the address range and handle faults
50430Sstevel@tonic-gate 	 */
50440Sstevel@tonic-gate 	for (a = addr; a < addr + len; a += PAGESIZE, off += PAGESIZE) {
50450Sstevel@tonic-gate 		err = segvn_faultpage(hat, seg, a, off, vpage, plp, vpprot,
50462414Saguzovsk 		    type, rw, brkcow, a == addr);
50470Sstevel@tonic-gate 		if (err) {
50480Sstevel@tonic-gate 			if (amp != NULL)
50490Sstevel@tonic-gate 				ANON_LOCK_EXIT(&amp->a_rwlock);
50502414Saguzovsk 			if (type == F_SOFTLOCK && a > addr) {
50510Sstevel@tonic-gate 				segvn_softunlock(seg, addr, (a - addr),
50520Sstevel@tonic-gate 				    S_OTHER);
50532414Saguzovsk 			}
50540Sstevel@tonic-gate 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
50550Sstevel@tonic-gate 			segvn_pagelist_rele(plp);
50560Sstevel@tonic-gate 			if (pl_alloc_sz)
50570Sstevel@tonic-gate 				kmem_free(plp, pl_alloc_sz);
50580Sstevel@tonic-gate 			return (err);
50590Sstevel@tonic-gate 		}
50600Sstevel@tonic-gate 		if (vpage) {
50610Sstevel@tonic-gate 			vpage++;
50620Sstevel@tonic-gate 		} else if (svd->vpage) {
50630Sstevel@tonic-gate 			page = seg_page(seg, addr);
50640Sstevel@tonic-gate 			vpage = &svd->vpage[++page];
50650Sstevel@tonic-gate 		}
50660Sstevel@tonic-gate 	}
50670Sstevel@tonic-gate 
50680Sstevel@tonic-gate 	/* Didn't get pages from the underlying fs so we're done */
50690Sstevel@tonic-gate 	if (!dogetpage)
50700Sstevel@tonic-gate 		goto done;
50710Sstevel@tonic-gate 
50720Sstevel@tonic-gate 	/*
50730Sstevel@tonic-gate 	 * Now handle any other pages in the list returned.
50740Sstevel@tonic-gate 	 * If the page can be used, load up the translations now.
50750Sstevel@tonic-gate 	 * Note that the for loop will only be entered if "plp"
50760Sstevel@tonic-gate 	 * is pointing to a non-NULL page pointer which means that
50770Sstevel@tonic-gate 	 * VOP_GETPAGE() was called and vpprot has been initialized.
50780Sstevel@tonic-gate 	 */
50790Sstevel@tonic-gate 	if (svd->pageprot == 0)
50800Sstevel@tonic-gate 		prot = svd->prot & vpprot;
50810Sstevel@tonic-gate 
50820Sstevel@tonic-gate 
50830Sstevel@tonic-gate 	/*
50840Sstevel@tonic-gate 	 * Large Files: diff should be unsigned value because we started
50850Sstevel@tonic-gate 	 * supporting > 2GB segment sizes from 2.5.1 and when a
50860Sstevel@tonic-gate 	 * large file of size > 2GB gets mapped to address space
50870Sstevel@tonic-gate 	 * the diff value can be > 2GB.
50880Sstevel@tonic-gate 	 */
50890Sstevel@tonic-gate 
50900Sstevel@tonic-gate 	for (ppp = plp; (pp = *ppp) != NULL; ppp++) {
50910Sstevel@tonic-gate 		size_t diff;
50920Sstevel@tonic-gate 		struct anon *ap;
50930Sstevel@tonic-gate 		int anon_index;
50940Sstevel@tonic-gate 		anon_sync_obj_t cookie;
50950Sstevel@tonic-gate 		int hat_flag = HAT_LOAD_ADV;
50960Sstevel@tonic-gate 
50970Sstevel@tonic-gate 		if (svd->flags & MAP_TEXT) {
50980Sstevel@tonic-gate 			hat_flag |= HAT_LOAD_TEXT;
50990Sstevel@tonic-gate 		}
51000Sstevel@tonic-gate 
51010Sstevel@tonic-gate 		if (pp == PAGE_HANDLED)
51020Sstevel@tonic-gate 			continue;
51030Sstevel@tonic-gate 
51040Sstevel@tonic-gate 		if (pp->p_offset >=  svd->offset &&
51050Sstevel@tonic-gate 			(pp->p_offset < svd->offset + seg->s_size)) {
51060Sstevel@tonic-gate 
51070Sstevel@tonic-gate 			diff = pp->p_offset - svd->offset;
51080Sstevel@tonic-gate 
51090Sstevel@tonic-gate 			/*
51100Sstevel@tonic-gate 			 * Large Files: Following is the assertion
51110Sstevel@tonic-gate 			 * validating the above cast.
51120Sstevel@tonic-gate 			 */
51130Sstevel@tonic-gate 			ASSERT(svd->vp == pp->p_vnode);
51140Sstevel@tonic-gate 
51150Sstevel@tonic-gate 			page = btop(diff);
51160Sstevel@tonic-gate 			if (svd->pageprot)
51170Sstevel@tonic-gate 				prot = VPP_PROT(&svd->vpage[page]) & vpprot;
51180Sstevel@tonic-gate 
51190Sstevel@tonic-gate 			/*
51200Sstevel@tonic-gate 			 * Prevent other threads in the address space from
51210Sstevel@tonic-gate 			 * creating private pages (i.e., allocating anon slots)
51220Sstevel@tonic-gate 			 * while we are in the process of loading translations
51230Sstevel@tonic-gate 			 * to additional pages returned by the underlying
51240Sstevel@tonic-gate 			 * object.
51250Sstevel@tonic-gate 			 */
51260Sstevel@tonic-gate 			if (amp != NULL) {
51270Sstevel@tonic-gate 				anon_index = svd->anon_index + page;
51280Sstevel@tonic-gate 				anon_array_enter(amp, anon_index, &cookie);
51290Sstevel@tonic-gate 				ap = anon_get_ptr(amp->ahp, anon_index);
51300Sstevel@tonic-gate 			}
51310Sstevel@tonic-gate 			if ((amp == NULL) || (ap == NULL)) {
51320Sstevel@tonic-gate 				if (IS_VMODSORT(pp->p_vnode) ||
51330Sstevel@tonic-gate 				    enable_mbit_wa) {
51340Sstevel@tonic-gate 					if (rw == S_WRITE)
51350Sstevel@tonic-gate 						hat_setmod(pp);
51360Sstevel@tonic-gate 					else if (rw != S_OTHER &&
51370Sstevel@tonic-gate 					    !hat_ismod(pp))
51380Sstevel@tonic-gate 						prot &= ~PROT_WRITE;
51390Sstevel@tonic-gate 				}
51400Sstevel@tonic-gate 				/*
51410Sstevel@tonic-gate 				 * Skip mapping read ahead pages marked
51420Sstevel@tonic-gate 				 * for migration, so they will get migrated
51430Sstevel@tonic-gate 				 * properly on fault
51440Sstevel@tonic-gate 				 */
51450Sstevel@tonic-gate 				if ((prot & PROT_READ) && !PP_ISMIGRATE(pp)) {
51460Sstevel@tonic-gate 					hat_memload(hat, seg->s_base + diff,
51470Sstevel@tonic-gate 						pp, prot, hat_flag);
51480Sstevel@tonic-gate 				}
51490Sstevel@tonic-gate 			}
51500Sstevel@tonic-gate 			if (amp != NULL)
51510Sstevel@tonic-gate 				anon_array_exit(&cookie);
51520Sstevel@tonic-gate 		}
51530Sstevel@tonic-gate 		page_unlock(pp);
51540Sstevel@tonic-gate 	}
51550Sstevel@tonic-gate done:
51560Sstevel@tonic-gate 	if (amp != NULL)
51570Sstevel@tonic-gate 		ANON_LOCK_EXIT(&amp->a_rwlock);
51580Sstevel@tonic-gate 	SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
51590Sstevel@tonic-gate 	if (pl_alloc_sz)
51600Sstevel@tonic-gate 		kmem_free(plp, pl_alloc_sz);
51610Sstevel@tonic-gate 	return (0);
51620Sstevel@tonic-gate }
51630Sstevel@tonic-gate 
51640Sstevel@tonic-gate /*
51650Sstevel@tonic-gate  * This routine is used to start I/O on pages asynchronously.  XXX it will
51660Sstevel@tonic-gate  * only create PAGESIZE pages. At fault time they will be relocated into
51670Sstevel@tonic-gate  * larger pages.
51680Sstevel@tonic-gate  */
51690Sstevel@tonic-gate static faultcode_t
51700Sstevel@tonic-gate segvn_faulta(struct seg *seg, caddr_t addr)
51710Sstevel@tonic-gate {
51720Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
51730Sstevel@tonic-gate 	int err;
51740Sstevel@tonic-gate 	struct anon_map *amp;
51750Sstevel@tonic-gate 	vnode_t *vp;
51760Sstevel@tonic-gate 
51770Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
51780Sstevel@tonic-gate 
51790Sstevel@tonic-gate 	SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
51800Sstevel@tonic-gate 	if ((amp = svd->amp) != NULL) {
51810Sstevel@tonic-gate 		struct anon *ap;
51820Sstevel@tonic-gate 
51830Sstevel@tonic-gate 		/*
51840Sstevel@tonic-gate 		 * Reader lock to prevent amp->ahp from being changed.
51850Sstevel@tonic-gate 		 * This is advisory, it's ok to miss a page, so
51860Sstevel@tonic-gate 		 * we don't do anon_array_enter lock.
51870Sstevel@tonic-gate 		 */
51880Sstevel@tonic-gate 		ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
51890Sstevel@tonic-gate 		if ((ap = anon_get_ptr(amp->ahp,
51900Sstevel@tonic-gate 			svd->anon_index + seg_page(seg, addr))) != NULL) {
51910Sstevel@tonic-gate 
51920Sstevel@tonic-gate 			err = anon_getpage(&ap, NULL, NULL,
51930Sstevel@tonic-gate 			    0, seg, addr, S_READ, svd->cred);
51940Sstevel@tonic-gate 
51950Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
51960Sstevel@tonic-gate 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
51970Sstevel@tonic-gate 			if (err)
51980Sstevel@tonic-gate 				return (FC_MAKE_ERR(err));
51990Sstevel@tonic-gate 			return (0);
52000Sstevel@tonic-gate 		}
52010Sstevel@tonic-gate 		ANON_LOCK_EXIT(&amp->a_rwlock);
52020Sstevel@tonic-gate 	}
52030Sstevel@tonic-gate 
52040Sstevel@tonic-gate 	if (svd->vp == NULL) {
52050Sstevel@tonic-gate 		SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
52060Sstevel@tonic-gate 		return (0);			/* zfod page - do nothing now */
52070Sstevel@tonic-gate 	}
52080Sstevel@tonic-gate 
52090Sstevel@tonic-gate 	vp = svd->vp;
52100Sstevel@tonic-gate 	TRACE_3(TR_FAC_VM, TR_SEGVN_GETPAGE,
52110Sstevel@tonic-gate 		"segvn_getpage:seg %p addr %p vp %p", seg, addr, vp);
52120Sstevel@tonic-gate 	err = VOP_GETPAGE(vp,
52130Sstevel@tonic-gate 	    (offset_t)(svd->offset + (uintptr_t)(addr - seg->s_base)),
52140Sstevel@tonic-gate 	    PAGESIZE, NULL, NULL, 0, seg, addr,
52150Sstevel@tonic-gate 	    S_OTHER, svd->cred);
52160Sstevel@tonic-gate 
52170Sstevel@tonic-gate 	SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
52180Sstevel@tonic-gate 	if (err)
52190Sstevel@tonic-gate 		return (FC_MAKE_ERR(err));
52200Sstevel@tonic-gate 	return (0);
52210Sstevel@tonic-gate }
52220Sstevel@tonic-gate 
52230Sstevel@tonic-gate static int
52240Sstevel@tonic-gate segvn_setprot(struct seg *seg, caddr_t addr, size_t len, uint_t prot)
52250Sstevel@tonic-gate {
52260Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
52270Sstevel@tonic-gate 	struct vpage *svp, *evp;
52280Sstevel@tonic-gate 	struct vnode *vp;
52290Sstevel@tonic-gate 	size_t pgsz;
52300Sstevel@tonic-gate 	pgcnt_t pgcnt;
52310Sstevel@tonic-gate 	anon_sync_obj_t cookie;
52320Sstevel@tonic-gate 
52330Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
52340Sstevel@tonic-gate 
52350Sstevel@tonic-gate 	if ((svd->maxprot & prot) != prot)
52360Sstevel@tonic-gate 		return (EACCES);			/* violated maxprot */
52370Sstevel@tonic-gate 
52380Sstevel@tonic-gate 	SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_WRITER);
52390Sstevel@tonic-gate 
52400Sstevel@tonic-gate 	/* return if prot is the same */
52410Sstevel@tonic-gate 	if (!svd->pageprot && svd->prot == prot) {
52420Sstevel@tonic-gate 		SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
52430Sstevel@tonic-gate 		return (0);
52440Sstevel@tonic-gate 	}
52450Sstevel@tonic-gate 
52460Sstevel@tonic-gate 	/*
52470Sstevel@tonic-gate 	 * Since we change protections we first have to flush the cache.
52480Sstevel@tonic-gate 	 * This makes sure all the pagelock calls have to recheck
52490Sstevel@tonic-gate 	 * protections.
52500Sstevel@tonic-gate 	 */
52510Sstevel@tonic-gate 	if (svd->softlockcnt > 0) {
52520Sstevel@tonic-gate 		/*
52530Sstevel@tonic-gate 		 * Since we do have the segvn writers lock nobody can fill
52540Sstevel@tonic-gate 		 * the cache with entries belonging to this seg during
52550Sstevel@tonic-gate 		 * the purge. The flush either succeeds or we still have
52560Sstevel@tonic-gate 		 * pending I/Os.
52570Sstevel@tonic-gate 		 */
52580Sstevel@tonic-gate 		segvn_purge(seg);
52590Sstevel@tonic-gate 		if (svd->softlockcnt > 0) {
52600Sstevel@tonic-gate 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
52610Sstevel@tonic-gate 			return (EAGAIN);
52620Sstevel@tonic-gate 		}
52630Sstevel@tonic-gate 	}
52640Sstevel@tonic-gate 
52650Sstevel@tonic-gate 	if (seg->s_szc != 0) {
52660Sstevel@tonic-gate 		int err;
52670Sstevel@tonic-gate 		pgsz = page_get_pagesize(seg->s_szc);
52680Sstevel@tonic-gate 		pgcnt = pgsz >> PAGESHIFT;
52690Sstevel@tonic-gate 		ASSERT(IS_P2ALIGNED(pgcnt, pgcnt));
52700Sstevel@tonic-gate 		if (!IS_P2ALIGNED(addr, pgsz) || !IS_P2ALIGNED(len, pgsz)) {
52710Sstevel@tonic-gate 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
52720Sstevel@tonic-gate 			ASSERT(seg->s_base != addr || seg->s_size != len);
52730Sstevel@tonic-gate 			/*
52740Sstevel@tonic-gate 			 * If we are holding the as lock as a reader then
52750Sstevel@tonic-gate 			 * we need to return IE_RETRY and let the as
52760Sstevel@tonic-gate 			 * layer drop and re-aquire the lock as a writer.
52770Sstevel@tonic-gate 			 */
52780Sstevel@tonic-gate 			if (AS_READ_HELD(seg->s_as, &seg->s_as->a_lock))
52790Sstevel@tonic-gate 				return (IE_RETRY);
52800Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.demoterange[1]);
52812414Saguzovsk 			if (svd->type == MAP_PRIVATE || svd->vp != NULL) {
52822414Saguzovsk 				err = segvn_demote_range(seg, addr, len,
52832414Saguzovsk 				    SDR_END, 0);
52842414Saguzovsk 			} else {
52852991Ssusans 				uint_t szcvec = map_pgszcvec(seg->s_base,
52862991Ssusans 				    pgsz, (uintptr_t)seg->s_base,
52872991Ssusans 				    (svd->flags & MAP_TEXT), MAPPGSZC_SHM, 0);
52882414Saguzovsk 				err = segvn_demote_range(seg, addr, len,
52892414Saguzovsk 				    SDR_END, szcvec);
52902414Saguzovsk 			}
52910Sstevel@tonic-gate 			if (err == 0)
52920Sstevel@tonic-gate 				return (IE_RETRY);
52930Sstevel@tonic-gate 			if (err == ENOMEM)
52940Sstevel@tonic-gate 				return (IE_NOMEM);
52950Sstevel@tonic-gate 			return (err);
52960Sstevel@tonic-gate 		}
52970Sstevel@tonic-gate 	}
52980Sstevel@tonic-gate 
52990Sstevel@tonic-gate 
53000Sstevel@tonic-gate 	/*
53010Sstevel@tonic-gate 	 * If it's a private mapping and we're making it writable
53020Sstevel@tonic-gate 	 * and no swap space has been reserved, have to reserve
53030Sstevel@tonic-gate 	 * it all now.  If it's a private mapping to a file (i.e., vp != NULL)
53040Sstevel@tonic-gate 	 * and we're removing write permission on the entire segment and
53050Sstevel@tonic-gate 	 * we haven't modified any pages, we can release the swap space.
53060Sstevel@tonic-gate 	 */
53070Sstevel@tonic-gate 	if (svd->type == MAP_PRIVATE) {
53080Sstevel@tonic-gate 		if (prot & PROT_WRITE) {
53090Sstevel@tonic-gate 			size_t sz;
53100Sstevel@tonic-gate 			if (svd->swresv == 0 && !(svd->flags & MAP_NORESERVE)) {
53110Sstevel@tonic-gate 				if (anon_resv(seg->s_size) == 0) {
53120Sstevel@tonic-gate 					SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
53130Sstevel@tonic-gate 					return (IE_NOMEM);
53140Sstevel@tonic-gate 				}
53150Sstevel@tonic-gate 				sz = svd->swresv = seg->s_size;
53160Sstevel@tonic-gate 				TRACE_3(TR_FAC_VM, TR_ANON_PROC,
53170Sstevel@tonic-gate 					"anon proc:%p %lu %u",
53180Sstevel@tonic-gate 					seg, sz, 1);
53190Sstevel@tonic-gate 			}
53200Sstevel@tonic-gate 		} else {
53210Sstevel@tonic-gate 			/*
53220Sstevel@tonic-gate 			 * Swap space is released only if this segment
53230Sstevel@tonic-gate 			 * does not map anonymous memory, since read faults
53240Sstevel@tonic-gate 			 * on such segments still need an anon slot to read
53250Sstevel@tonic-gate 			 * in the data.
53260Sstevel@tonic-gate 			 */
53270Sstevel@tonic-gate 			if (svd->swresv != 0 && svd->vp != NULL &&
53280Sstevel@tonic-gate 			    svd->amp == NULL && addr == seg->s_base &&
53290Sstevel@tonic-gate 			    len == seg->s_size && svd->pageprot == 0) {
53300Sstevel@tonic-gate 				anon_unresv(svd->swresv);
53310Sstevel@tonic-gate 				svd->swresv = 0;
53320Sstevel@tonic-gate 				TRACE_3(TR_FAC_VM, TR_ANON_PROC,
53330Sstevel@tonic-gate 					"anon proc:%p %lu %u",
53340Sstevel@tonic-gate 					seg, 0, 0);
53350Sstevel@tonic-gate 			}
53360Sstevel@tonic-gate 		}
53370Sstevel@tonic-gate 	}
53380Sstevel@tonic-gate 
53390Sstevel@tonic-gate 	if (addr == seg->s_base && len == seg->s_size && svd->pageprot == 0) {
53400Sstevel@tonic-gate 		if (svd->prot == prot) {
53410Sstevel@tonic-gate 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
53420Sstevel@tonic-gate 			return (0);			/* all done */
53430Sstevel@tonic-gate 		}
53440Sstevel@tonic-gate 		svd->prot = (uchar_t)prot;
53452414Saguzovsk 	} else if (svd->type == MAP_PRIVATE) {
53460Sstevel@tonic-gate 		struct anon *ap = NULL;
53470Sstevel@tonic-gate 		page_t *pp;
53480Sstevel@tonic-gate 		u_offset_t offset, off;
53490Sstevel@tonic-gate 		struct anon_map *amp;
53500Sstevel@tonic-gate 		ulong_t anon_idx = 0;
53510Sstevel@tonic-gate 
53520Sstevel@tonic-gate 		/*
53530Sstevel@tonic-gate 		 * A vpage structure exists or else the change does not
53540Sstevel@tonic-gate 		 * involve the entire segment.  Establish a vpage structure
53550Sstevel@tonic-gate 		 * if none is there.  Then, for each page in the range,
53560Sstevel@tonic-gate 		 * adjust its individual permissions.  Note that write-
53570Sstevel@tonic-gate 		 * enabling a MAP_PRIVATE page can affect the claims for
53580Sstevel@tonic-gate 		 * locked down memory.  Overcommitting memory terminates
53590Sstevel@tonic-gate 		 * the operation.
53600Sstevel@tonic-gate 		 */
53610Sstevel@tonic-gate 		segvn_vpage(seg);
53620Sstevel@tonic-gate 		if ((amp = svd->amp) != NULL) {
53630Sstevel@tonic-gate 			anon_idx = svd->anon_index + seg_page(seg, addr);
53640Sstevel@tonic-gate 			ASSERT(seg->s_szc == 0 ||
53650Sstevel@tonic-gate 			    IS_P2ALIGNED(anon_idx, pgcnt));
53660Sstevel@tonic-gate 			ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
53670Sstevel@tonic-gate 		}
53680Sstevel@tonic-gate 
53690Sstevel@tonic-gate 		offset = svd->offset + (uintptr_t)(addr - seg->s_base);
53700Sstevel@tonic-gate 		evp = &svd->vpage[seg_page(seg, addr + len)];
53710Sstevel@tonic-gate 
53720Sstevel@tonic-gate 		/*
53730Sstevel@tonic-gate 		 * See Statement at the beginning of segvn_lockop regarding
53740Sstevel@tonic-gate 		 * the way cowcnts and lckcnts are handled.
53750Sstevel@tonic-gate 		 */
53760Sstevel@tonic-gate 		for (svp = &svd->vpage[seg_page(seg, addr)]; svp < evp; svp++) {
53770Sstevel@tonic-gate 
53782414Saguzovsk 			if (seg->s_szc != 0) {
53790Sstevel@tonic-gate 				if (amp != NULL) {
53800Sstevel@tonic-gate 					anon_array_enter(amp, anon_idx,
53810Sstevel@tonic-gate 					    &cookie);
53820Sstevel@tonic-gate 				}
53830Sstevel@tonic-gate 				if (IS_P2ALIGNED(anon_idx, pgcnt) &&
53840Sstevel@tonic-gate 				    !segvn_claim_pages(seg, svp, offset,
53850Sstevel@tonic-gate 					anon_idx, prot)) {
53860Sstevel@tonic-gate 					if (amp != NULL) {
53870Sstevel@tonic-gate 						anon_array_exit(&cookie);
53880Sstevel@tonic-gate 					}
53890Sstevel@tonic-gate 					break;
53900Sstevel@tonic-gate 				}
53910Sstevel@tonic-gate 				if (amp != NULL) {
53920Sstevel@tonic-gate 					anon_array_exit(&cookie);
53930Sstevel@tonic-gate 				}
53940Sstevel@tonic-gate 				anon_idx++;
53950Sstevel@tonic-gate 			} else {
53960Sstevel@tonic-gate 				if (amp != NULL) {
53970Sstevel@tonic-gate 					anon_array_enter(amp, anon_idx,
53980Sstevel@tonic-gate 						&cookie);
53990Sstevel@tonic-gate 					ap = anon_get_ptr(amp->ahp, anon_idx++);
54000Sstevel@tonic-gate 				}
54010Sstevel@tonic-gate 
54020Sstevel@tonic-gate 				if (VPP_ISPPLOCK(svp) &&
54032414Saguzovsk 				    VPP_PROT(svp) != prot) {
54040Sstevel@tonic-gate 
54050Sstevel@tonic-gate 					if (amp == NULL || ap == NULL) {
54060Sstevel@tonic-gate 						vp = svd->vp;
54070Sstevel@tonic-gate 						off = offset;
54080Sstevel@tonic-gate 					} else
54090Sstevel@tonic-gate 						swap_xlate(ap, &vp, &off);
54100Sstevel@tonic-gate 					if (amp != NULL)
54110Sstevel@tonic-gate 						anon_array_exit(&cookie);
54120Sstevel@tonic-gate 
54130Sstevel@tonic-gate 					if ((pp = page_lookup(vp, off,
54140Sstevel@tonic-gate 					    SE_SHARED)) == NULL) {
54150Sstevel@tonic-gate 						panic("segvn_setprot: no page");
54160Sstevel@tonic-gate 						/*NOTREACHED*/
54170Sstevel@tonic-gate 					}
54180Sstevel@tonic-gate 					ASSERT(seg->s_szc == 0);
54190Sstevel@tonic-gate 					if ((VPP_PROT(svp) ^ prot) &
54200Sstevel@tonic-gate 					    PROT_WRITE) {
54210Sstevel@tonic-gate 						if (prot & PROT_WRITE) {
54220Sstevel@tonic-gate 						    if (!page_addclaim(pp)) {
54230Sstevel@tonic-gate 							page_unlock(pp);
54240Sstevel@tonic-gate 							break;
54250Sstevel@tonic-gate 						    }
54260Sstevel@tonic-gate 						} else {
54270Sstevel@tonic-gate 						    if (!page_subclaim(pp)) {
54280Sstevel@tonic-gate 							page_unlock(pp);
54290Sstevel@tonic-gate 							break;
54300Sstevel@tonic-gate 						    }
54310Sstevel@tonic-gate 						}
54320Sstevel@tonic-gate 					}
54330Sstevel@tonic-gate 					page_unlock(pp);
54340Sstevel@tonic-gate 				} else if (amp != NULL)
54350Sstevel@tonic-gate 					anon_array_exit(&cookie);
54360Sstevel@tonic-gate 			}
54370Sstevel@tonic-gate 			VPP_SETPROT(svp, prot);
54380Sstevel@tonic-gate 			offset += PAGESIZE;
54390Sstevel@tonic-gate 		}
54400Sstevel@tonic-gate 		if (amp != NULL)
54410Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
54420Sstevel@tonic-gate 
54430Sstevel@tonic-gate 		/*
54440Sstevel@tonic-gate 		 * Did we terminate prematurely?  If so, simply unload
54450Sstevel@tonic-gate 		 * the translations to the things we've updated so far.
54460Sstevel@tonic-gate 		 */
54470Sstevel@tonic-gate 		if (svp != evp) {
54480Sstevel@tonic-gate 			len = (svp - &svd->vpage[seg_page(seg, addr)]) *
54490Sstevel@tonic-gate 			    PAGESIZE;
54500Sstevel@tonic-gate 			ASSERT(seg->s_szc == 0 || IS_P2ALIGNED(len, pgsz));
54510Sstevel@tonic-gate 			if (len != 0)
54520Sstevel@tonic-gate 				hat_unload(seg->s_as->a_hat, addr,
54530Sstevel@tonic-gate 				    len, HAT_UNLOAD);
54540Sstevel@tonic-gate 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
54550Sstevel@tonic-gate 			return (IE_NOMEM);
54560Sstevel@tonic-gate 		}
54572414Saguzovsk 	} else {
54582414Saguzovsk 		segvn_vpage(seg);
54592414Saguzovsk 		evp = &svd->vpage[seg_page(seg, addr + len)];
54602414Saguzovsk 		for (svp = &svd->vpage[seg_page(seg, addr)]; svp < evp; svp++) {
54612414Saguzovsk 			VPP_SETPROT(svp, prot);
54622414Saguzovsk 		}
54632414Saguzovsk 	}
54642414Saguzovsk 
54652414Saguzovsk 	if (((prot & PROT_WRITE) != 0 &&
54662414Saguzovsk 	    (svd->vp != NULL || svd->type == MAP_PRIVATE)) ||
54672414Saguzovsk 	    (prot & ~PROT_USER) == PROT_NONE) {
54680Sstevel@tonic-gate 		/*
54690Sstevel@tonic-gate 		 * Either private or shared data with write access (in
54700Sstevel@tonic-gate 		 * which case we need to throw out all former translations
54710Sstevel@tonic-gate 		 * so that we get the right translations set up on fault
54720Sstevel@tonic-gate 		 * and we don't allow write access to any copy-on-write pages
54730Sstevel@tonic-gate 		 * that might be around or to prevent write access to pages
54740Sstevel@tonic-gate 		 * representing holes in a file), or we don't have permission
54750Sstevel@tonic-gate 		 * to access the memory at all (in which case we have to
54760Sstevel@tonic-gate 		 * unload any current translations that might exist).
54770Sstevel@tonic-gate 		 */
54780Sstevel@tonic-gate 		hat_unload(seg->s_as->a_hat, addr, len, HAT_UNLOAD);
54790Sstevel@tonic-gate 	} else {
54800Sstevel@tonic-gate 		/*
54810Sstevel@tonic-gate 		 * A shared mapping or a private mapping in which write
54820Sstevel@tonic-gate 		 * protection is going to be denied - just change all the
54830Sstevel@tonic-gate 		 * protections over the range of addresses in question.
54840Sstevel@tonic-gate 		 * segvn does not support any other attributes other
54850Sstevel@tonic-gate 		 * than prot so we can use hat_chgattr.
54860Sstevel@tonic-gate 		 */
54870Sstevel@tonic-gate 		hat_chgattr(seg->s_as->a_hat, addr, len, prot);
54880Sstevel@tonic-gate 	}
54890Sstevel@tonic-gate 
54900Sstevel@tonic-gate 	SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
54910Sstevel@tonic-gate 
54920Sstevel@tonic-gate 	return (0);
54930Sstevel@tonic-gate }
54940Sstevel@tonic-gate 
54950Sstevel@tonic-gate /*
54960Sstevel@tonic-gate  * segvn_setpagesize is called via SEGOP_SETPAGESIZE from as_setpagesize,
54970Sstevel@tonic-gate  * to determine if the seg is capable of mapping the requested szc.
54980Sstevel@tonic-gate  */
54990Sstevel@tonic-gate static int
55000Sstevel@tonic-gate segvn_setpagesize(struct seg *seg, caddr_t addr, size_t len, uint_t szc)
55010Sstevel@tonic-gate {
55020Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
55030Sstevel@tonic-gate 	struct segvn_data *nsvd;
55040Sstevel@tonic-gate 	struct anon_map *amp = svd->amp;
55050Sstevel@tonic-gate 	struct seg *nseg;
55060Sstevel@tonic-gate 	caddr_t eaddr = addr + len, a;
55070Sstevel@tonic-gate 	size_t pgsz = page_get_pagesize(szc);
55082414Saguzovsk 	pgcnt_t pgcnt = page_get_pagecnt(szc);
55090Sstevel@tonic-gate 	int err;
55100Sstevel@tonic-gate 	u_offset_t off = svd->offset + (uintptr_t)(addr - seg->s_base);
55110Sstevel@tonic-gate 	extern struct vnode kvp;
55120Sstevel@tonic-gate 
55130Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
55140Sstevel@tonic-gate 	ASSERT(addr >= seg->s_base && eaddr <= seg->s_base + seg->s_size);
55150Sstevel@tonic-gate 
55160Sstevel@tonic-gate 	if (seg->s_szc == szc || segvn_lpg_disable != 0) {
55170Sstevel@tonic-gate 		return (0);
55180Sstevel@tonic-gate 	}
55190Sstevel@tonic-gate 
55200Sstevel@tonic-gate 	/*
55210Sstevel@tonic-gate 	 * addr should always be pgsz aligned but eaddr may be misaligned if
55220Sstevel@tonic-gate 	 * it's at the end of the segment.
55230Sstevel@tonic-gate 	 *
55240Sstevel@tonic-gate 	 * XXX we should assert this condition since as_setpagesize() logic
55250Sstevel@tonic-gate 	 * guarantees it.
55260Sstevel@tonic-gate 	 */
55270Sstevel@tonic-gate 	if (!IS_P2ALIGNED(addr, pgsz) ||
55280Sstevel@tonic-gate 	    (!IS_P2ALIGNED(eaddr, pgsz) &&
55290Sstevel@tonic-gate 		eaddr != seg->s_base + seg->s_size)) {
55300Sstevel@tonic-gate 
55310Sstevel@tonic-gate 		segvn_setpgsz_align_err++;
55320Sstevel@tonic-gate 		return (EINVAL);
55330Sstevel@tonic-gate 	}
55340Sstevel@tonic-gate 
55352414Saguzovsk 	if (amp != NULL && svd->type == MAP_SHARED) {
55362414Saguzovsk 		ulong_t an_idx = svd->anon_index + seg_page(seg, addr);
55372414Saguzovsk 		if (!IS_P2ALIGNED(an_idx, pgcnt)) {
55382414Saguzovsk 
55392414Saguzovsk 			segvn_setpgsz_anon_align_err++;
55402414Saguzovsk 			return (EINVAL);
55412414Saguzovsk 		}
55422414Saguzovsk 	}
55432414Saguzovsk 
55442414Saguzovsk 	if ((svd->flags & MAP_NORESERVE) || seg->s_as == &kas ||
55450Sstevel@tonic-gate 	    szc > segvn_maxpgszc) {
55460Sstevel@tonic-gate 		return (EINVAL);
55470Sstevel@tonic-gate 	}
55480Sstevel@tonic-gate 
55490Sstevel@tonic-gate 	/* paranoid check */
55500Sstevel@tonic-gate 	if (svd->vp != NULL &&
55513290Sjohansen 	    (IS_SWAPFSVP(svd->vp) || VN_ISKAS(svd->vp))) {
55520Sstevel@tonic-gate 		    return (EINVAL);
55530Sstevel@tonic-gate 	}
55540Sstevel@tonic-gate 
55550Sstevel@tonic-gate 	if (seg->s_szc == 0 && svd->vp != NULL &&
55560Sstevel@tonic-gate 	    map_addr_vacalign_check(addr, off)) {
55570Sstevel@tonic-gate 		return (EINVAL);
55580Sstevel@tonic-gate 	}
55590Sstevel@tonic-gate 
55600Sstevel@tonic-gate 	/*
55610Sstevel@tonic-gate 	 * Check that protections are the same within new page
55620Sstevel@tonic-gate 	 * size boundaries.
55630Sstevel@tonic-gate 	 */
55640Sstevel@tonic-gate 	if (svd->pageprot) {
55650Sstevel@tonic-gate 		for (a = addr; a < eaddr; a += pgsz) {
55660Sstevel@tonic-gate 			if ((a + pgsz) > eaddr) {
55670Sstevel@tonic-gate 				if (!sameprot(seg, a, eaddr - a)) {
55680Sstevel@tonic-gate 					return (EINVAL);
55690Sstevel@tonic-gate 				}
55700Sstevel@tonic-gate 			} else {
55710Sstevel@tonic-gate 				if (!sameprot(seg, a, pgsz)) {
55720Sstevel@tonic-gate 					return (EINVAL);
55730Sstevel@tonic-gate 				}
55740Sstevel@tonic-gate 			}
55750Sstevel@tonic-gate 		}
55760Sstevel@tonic-gate 	}
55770Sstevel@tonic-gate 
55780Sstevel@tonic-gate 	/*
55790Sstevel@tonic-gate 	 * Since we are changing page size we first have to flush
55800Sstevel@tonic-gate 	 * the cache. This makes sure all the pagelock calls have
55810Sstevel@tonic-gate 	 * to recheck protections.
55820Sstevel@tonic-gate 	 */
55830Sstevel@tonic-gate 	if (svd->softlockcnt > 0) {
55840Sstevel@tonic-gate 		/*
55850Sstevel@tonic-gate 		 * Since we do have the segvn writers lock nobody can fill
55860Sstevel@tonic-gate 		 * the cache with entries belonging to this seg during
55870Sstevel@tonic-gate 		 * the purge. The flush either succeeds or we still have
55880Sstevel@tonic-gate 		 * pending I/Os.
55890Sstevel@tonic-gate 		 */
55900Sstevel@tonic-gate 		segvn_purge(seg);
55910Sstevel@tonic-gate 		if (svd->softlockcnt > 0) {
55920Sstevel@tonic-gate 			return (EAGAIN);
55930Sstevel@tonic-gate 		}
55940Sstevel@tonic-gate 	}
55950Sstevel@tonic-gate 
55960Sstevel@tonic-gate 	/*
55970Sstevel@tonic-gate 	 * Operation for sub range of existing segment.
55980Sstevel@tonic-gate 	 */
55990Sstevel@tonic-gate 	if (addr != seg->s_base || eaddr != (seg->s_base + seg->s_size)) {
56000Sstevel@tonic-gate 		if (szc < seg->s_szc) {
56010Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.demoterange[2]);
56022414Saguzovsk 			err = segvn_demote_range(seg, addr, len, SDR_RANGE, 0);
56030Sstevel@tonic-gate 			if (err == 0) {
56040Sstevel@tonic-gate 				return (IE_RETRY);
56050Sstevel@tonic-gate 			}
56060Sstevel@tonic-gate 			if (err == ENOMEM) {
56070Sstevel@tonic-gate 				return (IE_NOMEM);
56080Sstevel@tonic-gate 			}
56090Sstevel@tonic-gate 			return (err);
56100Sstevel@tonic-gate 		}
56110Sstevel@tonic-gate 		if (addr != seg->s_base) {
56120Sstevel@tonic-gate 			nseg = segvn_split_seg(seg, addr);
56130Sstevel@tonic-gate 			if (eaddr != (nseg->s_base + nseg->s_size)) {
56140Sstevel@tonic-gate 				/* eaddr is szc aligned */
56150Sstevel@tonic-gate 				(void) segvn_split_seg(nseg, eaddr);
56160Sstevel@tonic-gate 			}
56170Sstevel@tonic-gate 			return (IE_RETRY);
56180Sstevel@tonic-gate 		}
56190Sstevel@tonic-gate 		if (eaddr != (seg->s_base + seg->s_size)) {
56200Sstevel@tonic-gate 			/* eaddr is szc aligned */
56210Sstevel@tonic-gate 			(void) segvn_split_seg(seg, eaddr);
56220Sstevel@tonic-gate 		}
56230Sstevel@tonic-gate 		return (IE_RETRY);
56240Sstevel@tonic-gate 	}
56250Sstevel@tonic-gate 
56260Sstevel@tonic-gate 	/*
56270Sstevel@tonic-gate 	 * Break any low level sharing and reset seg->s_szc to 0.
56280Sstevel@tonic-gate 	 */
56290Sstevel@tonic-gate 	if ((err = segvn_clrszc(seg)) != 0) {
56300Sstevel@tonic-gate 		if (err == ENOMEM) {
56310Sstevel@tonic-gate 			err = IE_NOMEM;
56320Sstevel@tonic-gate 		}
56330Sstevel@tonic-gate 		return (err);
56340Sstevel@tonic-gate 	}
56350Sstevel@tonic-gate 	ASSERT(seg->s_szc == 0);
56360Sstevel@tonic-gate 
56370Sstevel@tonic-gate 	/*
56380Sstevel@tonic-gate 	 * If the end of the current segment is not pgsz aligned
56390Sstevel@tonic-gate 	 * then attempt to concatenate with the next segment.
56400Sstevel@tonic-gate 	 */
56410Sstevel@tonic-gate 	if (!IS_P2ALIGNED(eaddr, pgsz)) {
56420Sstevel@tonic-gate 		nseg = AS_SEGNEXT(seg->s_as, seg);
56430Sstevel@tonic-gate 		if (nseg == NULL || nseg == seg || eaddr != nseg->s_base) {
56440Sstevel@tonic-gate 			return (ENOMEM);
56450Sstevel@tonic-gate 		}
56460Sstevel@tonic-gate 		if (nseg->s_ops != &segvn_ops) {
56470Sstevel@tonic-gate 			return (EINVAL);
56480Sstevel@tonic-gate 		}
56490Sstevel@tonic-gate 		nsvd = (struct segvn_data *)nseg->s_data;
56500Sstevel@tonic-gate 		if (nsvd->softlockcnt > 0) {
56510Sstevel@tonic-gate 			segvn_purge(nseg);
56520Sstevel@tonic-gate 			if (nsvd->softlockcnt > 0) {
56530Sstevel@tonic-gate 				return (EAGAIN);
56540Sstevel@tonic-gate 			}
56550Sstevel@tonic-gate 		}
56560Sstevel@tonic-gate 		err = segvn_clrszc(nseg);
56570Sstevel@tonic-gate 		if (err == ENOMEM) {
56580Sstevel@tonic-gate 			err = IE_NOMEM;
56590Sstevel@tonic-gate 		}
56600Sstevel@tonic-gate 		if (err != 0) {
56610Sstevel@tonic-gate 			return (err);
56620Sstevel@tonic-gate 		}
56630Sstevel@tonic-gate 		err = segvn_concat(seg, nseg, 1);
56640Sstevel@tonic-gate 		if (err == -1) {
56650Sstevel@tonic-gate 			return (EINVAL);
56660Sstevel@tonic-gate 		}
56670Sstevel@tonic-gate 		if (err == -2) {
56680Sstevel@tonic-gate 			return (IE_NOMEM);
56690Sstevel@tonic-gate 		}
56700Sstevel@tonic-gate 		return (IE_RETRY);
56710Sstevel@tonic-gate 	}
56720Sstevel@tonic-gate 
56730Sstevel@tonic-gate 	/*
56740Sstevel@tonic-gate 	 * May need to re-align anon array to
56750Sstevel@tonic-gate 	 * new szc.
56760Sstevel@tonic-gate 	 */
56770Sstevel@tonic-gate 	if (amp != NULL) {
56780Sstevel@tonic-gate 		if (!IS_P2ALIGNED(svd->anon_index, pgcnt)) {
56790Sstevel@tonic-gate 			struct anon_hdr *nahp;
56800Sstevel@tonic-gate 
56812414Saguzovsk 			ASSERT(svd->type == MAP_PRIVATE);
56822414Saguzovsk 
56830Sstevel@tonic-gate 			ANON_LOCK_ENTER(&amp->a_rwlock, RW_WRITER);
56840Sstevel@tonic-gate 			ASSERT(amp->refcnt == 1);
56850Sstevel@tonic-gate 			nahp = anon_create(btop(amp->size), ANON_NOSLEEP);
56860Sstevel@tonic-gate 			if (nahp == NULL) {
56870Sstevel@tonic-gate 				ANON_LOCK_EXIT(&amp->a_rwlock);
56880Sstevel@tonic-gate 				return (IE_NOMEM);
56890Sstevel@tonic-gate 			}
56900Sstevel@tonic-gate 			if (anon_copy_ptr(amp->ahp, svd->anon_index,
56910Sstevel@tonic-gate 				nahp, 0, btop(seg->s_size), ANON_NOSLEEP)) {
56920Sstevel@tonic-gate 				anon_release(nahp, btop(amp->size));
56930Sstevel@tonic-gate 				ANON_LOCK_EXIT(&amp->a_rwlock);
56940Sstevel@tonic-gate 				return (IE_NOMEM);
56950Sstevel@tonic-gate 			}
56960Sstevel@tonic-gate 			anon_release(amp->ahp, btop(amp->size));
56970Sstevel@tonic-gate 			amp->ahp = nahp;
56980Sstevel@tonic-gate 			svd->anon_index = 0;
56990Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
57000Sstevel@tonic-gate 		}
57010Sstevel@tonic-gate 	}
57020Sstevel@tonic-gate 	if (svd->vp != NULL && szc != 0) {
57030Sstevel@tonic-gate 		struct vattr va;
57040Sstevel@tonic-gate 		u_offset_t eoffpage = svd->offset;
57050Sstevel@tonic-gate 		va.va_mask = AT_SIZE;
57060Sstevel@tonic-gate 		eoffpage += seg->s_size;
57070Sstevel@tonic-gate 		eoffpage = btopr(eoffpage);
57080Sstevel@tonic-gate 		if (VOP_GETATTR(svd->vp, &va, 0, svd->cred) != 0) {
57090Sstevel@tonic-gate 			segvn_setpgsz_getattr_err++;
57100Sstevel@tonic-gate 			return (EINVAL);
57110Sstevel@tonic-gate 		}
57120Sstevel@tonic-gate 		if (btopr(va.va_size) < eoffpage) {
57130Sstevel@tonic-gate 			segvn_setpgsz_eof_err++;
57140Sstevel@tonic-gate 			return (EINVAL);
57150Sstevel@tonic-gate 		}
57160Sstevel@tonic-gate 		if (amp != NULL) {
57170Sstevel@tonic-gate 			/*
57180Sstevel@tonic-gate 			 * anon_fill_cow_holes() may call VOP_GETPAGE().
57190Sstevel@tonic-gate 			 * don't take anon map lock here to avoid holding it
57200Sstevel@tonic-gate 			 * across VOP_GETPAGE() calls that may call back into
57210Sstevel@tonic-gate 			 * segvn for klsutering checks. We don't really need
57220Sstevel@tonic-gate 			 * anon map lock here since it's a private segment and
57230Sstevel@tonic-gate 			 * we hold as level lock as writers.
57240Sstevel@tonic-gate 			 */
57250Sstevel@tonic-gate 			if ((err = anon_fill_cow_holes(seg, seg->s_base,
57260Sstevel@tonic-gate 			    amp->ahp, svd->anon_index, svd->vp, svd->offset,
57270Sstevel@tonic-gate 			    seg->s_size, szc, svd->prot, svd->vpage,
57280Sstevel@tonic-gate 			    svd->cred)) != 0) {
57290Sstevel@tonic-gate 				return (EINVAL);
57300Sstevel@tonic-gate 			}
57310Sstevel@tonic-gate 		}
57320Sstevel@tonic-gate 		segvn_setvnode_mpss(svd->vp);
57330Sstevel@tonic-gate 	}
57340Sstevel@tonic-gate 
57350Sstevel@tonic-gate 	if (amp != NULL) {
57360Sstevel@tonic-gate 		ANON_LOCK_ENTER(&amp->a_rwlock, RW_WRITER);
57372414Saguzovsk 		if (svd->type == MAP_PRIVATE) {
57382414Saguzovsk 			amp->a_szc = szc;
57392414Saguzovsk 		} else if (szc > amp->a_szc) {
57402414Saguzovsk 			amp->a_szc = szc;
57412414Saguzovsk 		}
57420Sstevel@tonic-gate 		ANON_LOCK_EXIT(&amp->a_rwlock);
57430Sstevel@tonic-gate 	}
57440Sstevel@tonic-gate 
57450Sstevel@tonic-gate 	seg->s_szc = szc;
57460Sstevel@tonic-gate 
57470Sstevel@tonic-gate 	return (0);
57480Sstevel@tonic-gate }
57490Sstevel@tonic-gate 
57500Sstevel@tonic-gate static int
57510Sstevel@tonic-gate segvn_clrszc(struct seg *seg)
57520Sstevel@tonic-gate {
57530Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
57540Sstevel@tonic-gate 	struct anon_map *amp = svd->amp;
57550Sstevel@tonic-gate 	size_t pgsz;
57560Sstevel@tonic-gate 	pgcnt_t pages;
57570Sstevel@tonic-gate 	int err = 0;
57580Sstevel@tonic-gate 	caddr_t a = seg->s_base;
57590Sstevel@tonic-gate 	caddr_t ea = a + seg->s_size;
57600Sstevel@tonic-gate 	ulong_t an_idx = svd->anon_index;
57610Sstevel@tonic-gate 	vnode_t *vp = svd->vp;
57620Sstevel@tonic-gate 	struct vpage *vpage = svd->vpage;
57630Sstevel@tonic-gate 	page_t *anon_pl[1 + 1], *pp;
57640Sstevel@tonic-gate 	struct anon *ap, *oldap;
57650Sstevel@tonic-gate 	uint_t prot = svd->prot, vpprot;
57660Sstevel@tonic-gate 
57670Sstevel@tonic-gate 	ASSERT(AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock) ||
57680Sstevel@tonic-gate 	    SEGVN_WRITE_HELD(seg->s_as, &svd->lock));
57690Sstevel@tonic-gate 
57700Sstevel@tonic-gate 	if (vp == NULL && amp == NULL) {
57710Sstevel@tonic-gate 		seg->s_szc = 0;
57720Sstevel@tonic-gate 		return (0);
57730Sstevel@tonic-gate 	}
57740Sstevel@tonic-gate 
57750Sstevel@tonic-gate 	/*
57760Sstevel@tonic-gate 	 * do HAT_UNLOAD_UNMAP since we are changing the pagesize.
57770Sstevel@tonic-gate 	 * unload argument is 0 when we are freeing the segment
57780Sstevel@tonic-gate 	 * and unload was already done.
57790Sstevel@tonic-gate 	 */
57800Sstevel@tonic-gate 	hat_unload(seg->s_as->a_hat, seg->s_base, seg->s_size,
57810Sstevel@tonic-gate 	    HAT_UNLOAD_UNMAP);
57820Sstevel@tonic-gate 
57832414Saguzovsk 	if (amp == NULL || svd->type == MAP_SHARED) {
57840Sstevel@tonic-gate 		seg->s_szc = 0;
57850Sstevel@tonic-gate 		return (0);
57860Sstevel@tonic-gate 	}
57870Sstevel@tonic-gate 
57880Sstevel@tonic-gate 	pgsz = page_get_pagesize(seg->s_szc);
57890Sstevel@tonic-gate 	pages = btop(pgsz);
57900Sstevel@tonic-gate 
57910Sstevel@tonic-gate 	/*
57920Sstevel@tonic-gate 	 * XXX anon rwlock is not really needed because this is a
57930Sstevel@tonic-gate 	 * private segment and we are writers.
57940Sstevel@tonic-gate 	 */
57950Sstevel@tonic-gate 	ANON_LOCK_ENTER(&amp->a_rwlock, RW_WRITER);
57960Sstevel@tonic-gate 
57970Sstevel@tonic-gate 	for (; a < ea; a += pgsz, an_idx += pages) {
57980Sstevel@tonic-gate 		if ((oldap = anon_get_ptr(amp->ahp, an_idx)) != NULL) {
57990Sstevel@tonic-gate 			if (svd->pageprot != 0) {
58000Sstevel@tonic-gate 				ASSERT(vpage != NULL);
58010Sstevel@tonic-gate 				prot = VPP_PROT(vpage);
58020Sstevel@tonic-gate 				ASSERT(sameprot(seg, a, pgsz));
58030Sstevel@tonic-gate 			}
58040Sstevel@tonic-gate 			if (seg->s_szc != 0) {
58050Sstevel@tonic-gate 				ASSERT(vp == NULL || anon_pages(amp->ahp,
58060Sstevel@tonic-gate 				    an_idx, pages) == pages);
58070Sstevel@tonic-gate 				if ((err = anon_map_demotepages(amp, an_idx,
58080Sstevel@tonic-gate 				    seg, a, prot, vpage, svd->cred)) != 0) {
58090Sstevel@tonic-gate 					goto out;
58100Sstevel@tonic-gate 				}
58110Sstevel@tonic-gate 			} else {
58120Sstevel@tonic-gate 				if (oldap->an_refcnt == 1) {
58130Sstevel@tonic-gate 					continue;
58140Sstevel@tonic-gate 				}
58150Sstevel@tonic-gate 				if ((err = anon_getpage(&oldap, &vpprot,
58160Sstevel@tonic-gate 				    anon_pl, PAGESIZE, seg, a, S_READ,
58170Sstevel@tonic-gate 				    svd->cred))) {
58180Sstevel@tonic-gate 					goto out;
58190Sstevel@tonic-gate 				}
58200Sstevel@tonic-gate 				if ((pp = anon_private(&ap, seg, a, prot,
58210Sstevel@tonic-gate 				    anon_pl[0], 0, svd->cred)) == NULL) {
58220Sstevel@tonic-gate 					err = ENOMEM;
58230Sstevel@tonic-gate 					goto out;
58240Sstevel@tonic-gate 				}
58250Sstevel@tonic-gate 				anon_decref(oldap);
58260Sstevel@tonic-gate 				(void) anon_set_ptr(amp->ahp, an_idx, ap,
58270Sstevel@tonic-gate 				    ANON_SLEEP);
58280Sstevel@tonic-gate 				page_unlock(pp);
58290Sstevel@tonic-gate 			}
58300Sstevel@tonic-gate 		}
58310Sstevel@tonic-gate 		vpage = (vpage == NULL) ? NULL : vpage + pages;
58320Sstevel@tonic-gate 	}
58330Sstevel@tonic-gate 
58340Sstevel@tonic-gate 	amp->a_szc = 0;
58350Sstevel@tonic-gate 	seg->s_szc = 0;
58360Sstevel@tonic-gate out:
58370Sstevel@tonic-gate 	ANON_LOCK_EXIT(&amp->a_rwlock);
58380Sstevel@tonic-gate 	return (err);
58390Sstevel@tonic-gate }
58400Sstevel@tonic-gate 
58410Sstevel@tonic-gate static int
58420Sstevel@tonic-gate segvn_claim_pages(
58430Sstevel@tonic-gate 	struct seg *seg,
58440Sstevel@tonic-gate 	struct vpage *svp,
58450Sstevel@tonic-gate 	u_offset_t off,
58460Sstevel@tonic-gate 	ulong_t anon_idx,
58470Sstevel@tonic-gate 	uint_t prot)
58480Sstevel@tonic-gate {
58490Sstevel@tonic-gate 	pgcnt_t	pgcnt = page_get_pagecnt(seg->s_szc);
58500Sstevel@tonic-gate 	size_t ppasize = (pgcnt + 1) * sizeof (page_t *);
58510Sstevel@tonic-gate 	page_t	**ppa;
58520Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
58530Sstevel@tonic-gate 	struct anon_map *amp = svd->amp;
58540Sstevel@tonic-gate 	struct vpage *evp = svp + pgcnt;
58550Sstevel@tonic-gate 	caddr_t addr = ((uintptr_t)(svp - svd->vpage) << PAGESHIFT)
58560Sstevel@tonic-gate 	    + seg->s_base;
58570Sstevel@tonic-gate 	struct anon *ap;
58580Sstevel@tonic-gate 	struct vnode *vp = svd->vp;
58590Sstevel@tonic-gate 	page_t *pp;
58600Sstevel@tonic-gate 	pgcnt_t pg_idx, i;
58610Sstevel@tonic-gate 	int err = 0;
58622768Ssl108498 	anoff_t aoff;
58630Sstevel@tonic-gate 	int anon = (amp != NULL) ? 1 : 0;
58640Sstevel@tonic-gate 
58650Sstevel@tonic-gate 	ASSERT(svd->type == MAP_PRIVATE);
58660Sstevel@tonic-gate 	ASSERT(svd->vpage != NULL);
58670Sstevel@tonic-gate 	ASSERT(seg->s_szc != 0);
58680Sstevel@tonic-gate 	ASSERT(IS_P2ALIGNED(pgcnt, pgcnt));
58690Sstevel@tonic-gate 	ASSERT(amp == NULL || IS_P2ALIGNED(anon_idx, pgcnt));
58700Sstevel@tonic-gate 	ASSERT(sameprot(seg, addr, pgcnt << PAGESHIFT));
58710Sstevel@tonic-gate 
58720Sstevel@tonic-gate 	if (VPP_PROT(svp) == prot)
58730Sstevel@tonic-gate 		return (1);
58740Sstevel@tonic-gate 	if (!((VPP_PROT(svp) ^ prot) & PROT_WRITE))
58750Sstevel@tonic-gate 		return (1);
58760Sstevel@tonic-gate 
58770Sstevel@tonic-gate 	ppa = kmem_alloc(ppasize, KM_SLEEP);
58780Sstevel@tonic-gate 	if (anon && vp != NULL) {
58790Sstevel@tonic-gate 		if (anon_get_ptr(amp->ahp, anon_idx) == NULL) {
58800Sstevel@tonic-gate 			anon = 0;
58810Sstevel@tonic-gate 			ASSERT(!anon_pages(amp->ahp, anon_idx, pgcnt));
58820Sstevel@tonic-gate 		}
58830Sstevel@tonic-gate 		ASSERT(!anon ||
58840Sstevel@tonic-gate 		    anon_pages(amp->ahp, anon_idx, pgcnt) == pgcnt);
58850Sstevel@tonic-gate 	}
58860Sstevel@tonic-gate 
58870Sstevel@tonic-gate 	for (*ppa = NULL, pg_idx = 0; svp < evp; svp++, anon_idx++) {
58880Sstevel@tonic-gate 		if (!VPP_ISPPLOCK(svp))
58890Sstevel@tonic-gate 			continue;
58900Sstevel@tonic-gate 		if (anon) {
58910Sstevel@tonic-gate 			ap = anon_get_ptr(amp->ahp, anon_idx);
58920Sstevel@tonic-gate 			if (ap == NULL) {
58930Sstevel@tonic-gate 				panic("segvn_claim_pages: no anon slot");
58940Sstevel@tonic-gate 			}
58950Sstevel@tonic-gate 			swap_xlate(ap, &vp, &aoff);
58960Sstevel@tonic-gate 			off = (u_offset_t)aoff;
58970Sstevel@tonic-gate 		}
58980Sstevel@tonic-gate 		ASSERT(vp != NULL);
58990Sstevel@tonic-gate 		if ((pp = page_lookup(vp,
59000Sstevel@tonic-gate 		    (u_offset_t)off, SE_SHARED)) == NULL) {
59010Sstevel@tonic-gate 			panic("segvn_claim_pages: no page");
59020Sstevel@tonic-gate 		}
59030Sstevel@tonic-gate 		ppa[pg_idx++] = pp;
59040Sstevel@tonic-gate 		off += PAGESIZE;
59050Sstevel@tonic-gate 	}
59060Sstevel@tonic-gate 
59070Sstevel@tonic-gate 	if (ppa[0] == NULL) {
59080Sstevel@tonic-gate 		kmem_free(ppa, ppasize);
59090Sstevel@tonic-gate 		return (1);
59100Sstevel@tonic-gate 	}
59110Sstevel@tonic-gate 
59120Sstevel@tonic-gate 	ASSERT(pg_idx <= pgcnt);
59130Sstevel@tonic-gate 	ppa[pg_idx] = NULL;
59140Sstevel@tonic-gate 
59150Sstevel@tonic-gate 	if (prot & PROT_WRITE)
59160Sstevel@tonic-gate 		err = page_addclaim_pages(ppa);
59170Sstevel@tonic-gate 	else
59180Sstevel@tonic-gate 		err = page_subclaim_pages(ppa);
59190Sstevel@tonic-gate 
59200Sstevel@tonic-gate 	for (i = 0; i < pg_idx; i++) {
59210Sstevel@tonic-gate 		ASSERT(ppa[i] != NULL);
59220Sstevel@tonic-gate 		page_unlock(ppa[i]);
59230Sstevel@tonic-gate 	}
59240Sstevel@tonic-gate 
59250Sstevel@tonic-gate 	kmem_free(ppa, ppasize);
59260Sstevel@tonic-gate 	return (err);
59270Sstevel@tonic-gate }
59280Sstevel@tonic-gate 
59290Sstevel@tonic-gate /*
59300Sstevel@tonic-gate  * Returns right (upper address) segment if split occured.
59310Sstevel@tonic-gate  * If the address is equal to the beginning or end of its segment it returns
59320Sstevel@tonic-gate  * the current segment.
59330Sstevel@tonic-gate  */
59340Sstevel@tonic-gate static struct seg *
59350Sstevel@tonic-gate segvn_split_seg(struct seg *seg, caddr_t addr)
59360Sstevel@tonic-gate {
59370Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
59380Sstevel@tonic-gate 	struct seg *nseg;
59390Sstevel@tonic-gate 	size_t nsize;
59400Sstevel@tonic-gate 	struct segvn_data *nsvd;
59410Sstevel@tonic-gate 
59420Sstevel@tonic-gate 	ASSERT(AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
59430Sstevel@tonic-gate 	ASSERT(addr >= seg->s_base);
59440Sstevel@tonic-gate 	ASSERT(addr <= seg->s_base + seg->s_size);
59450Sstevel@tonic-gate 
59460Sstevel@tonic-gate 	if (addr == seg->s_base || addr == seg->s_base + seg->s_size)
59470Sstevel@tonic-gate 		return (seg);
59480Sstevel@tonic-gate 
59490Sstevel@tonic-gate 	nsize = seg->s_base + seg->s_size - addr;
59500Sstevel@tonic-gate 	seg->s_size = addr - seg->s_base;
59510Sstevel@tonic-gate 	nseg = seg_alloc(seg->s_as, addr, nsize);
59520Sstevel@tonic-gate 	ASSERT(nseg != NULL);
59530Sstevel@tonic-gate 	nseg->s_ops = seg->s_ops;
59540Sstevel@tonic-gate 	nsvd = kmem_cache_alloc(segvn_cache, KM_SLEEP);
59550Sstevel@tonic-gate 	nseg->s_data = (void *)nsvd;
59560Sstevel@tonic-gate 	nseg->s_szc = seg->s_szc;
59570Sstevel@tonic-gate 	*nsvd = *svd;
59580Sstevel@tonic-gate 	rw_init(&nsvd->lock, NULL, RW_DEFAULT, NULL);
59590Sstevel@tonic-gate 
59600Sstevel@tonic-gate 	if (nsvd->vp != NULL) {
59610Sstevel@tonic-gate 		VN_HOLD(nsvd->vp);
59620Sstevel@tonic-gate 		nsvd->offset = svd->offset +
59630Sstevel@tonic-gate 		    (uintptr_t)(nseg->s_base - seg->s_base);
59640Sstevel@tonic-gate 		if (nsvd->type == MAP_SHARED)
59650Sstevel@tonic-gate 			lgrp_shm_policy_init(NULL, nsvd->vp);
59660Sstevel@tonic-gate 	} else {
59670Sstevel@tonic-gate 		/*
59680Sstevel@tonic-gate 		 * The offset for an anonymous segment has no signifigance in
59690Sstevel@tonic-gate 		 * terms of an offset into a file. If we were to use the above
59700Sstevel@tonic-gate 		 * calculation instead, the structures read out of
59710Sstevel@tonic-gate 		 * /proc/<pid>/xmap would be more difficult to decipher since
59720Sstevel@tonic-gate 		 * it would be unclear whether two seemingly contiguous
59730Sstevel@tonic-gate 		 * prxmap_t structures represented different segments or a
59740Sstevel@tonic-gate 		 * single segment that had been split up into multiple prxmap_t
59750Sstevel@tonic-gate 		 * structures (e.g. if some part of the segment had not yet
59760Sstevel@tonic-gate 		 * been faulted in).
59770Sstevel@tonic-gate 		 */
59780Sstevel@tonic-gate 		nsvd->offset = 0;
59790Sstevel@tonic-gate 	}
59800Sstevel@tonic-gate 
59810Sstevel@tonic-gate 	ASSERT(svd->softlockcnt == 0);
59820Sstevel@tonic-gate 	crhold(svd->cred);
59830Sstevel@tonic-gate 
59840Sstevel@tonic-gate 	if (svd->vpage != NULL) {
59850Sstevel@tonic-gate 		size_t bytes = vpgtob(seg_pages(seg));
59860Sstevel@tonic-gate 		size_t nbytes = vpgtob(seg_pages(nseg));
59870Sstevel@tonic-gate 		struct vpage *ovpage = svd->vpage;
59880Sstevel@tonic-gate 
59890Sstevel@tonic-gate 		svd->vpage = kmem_alloc(bytes, KM_SLEEP);
59900Sstevel@tonic-gate 		bcopy(ovpage, svd->vpage, bytes);
59910Sstevel@tonic-gate 		nsvd->vpage = kmem_alloc(nbytes, KM_SLEEP);
59920Sstevel@tonic-gate 		bcopy(ovpage + seg_pages(seg), nsvd->vpage, nbytes);
59930Sstevel@tonic-gate 		kmem_free(ovpage, bytes + nbytes);
59940Sstevel@tonic-gate 	}
59952414Saguzovsk 	if (svd->amp != NULL && svd->type == MAP_PRIVATE) {
59960Sstevel@tonic-gate 		struct anon_map *oamp = svd->amp, *namp;
59970Sstevel@tonic-gate 		struct anon_hdr *nahp;
59980Sstevel@tonic-gate 
59990Sstevel@tonic-gate 		ANON_LOCK_ENTER(&oamp->a_rwlock, RW_WRITER);
60000Sstevel@tonic-gate 		ASSERT(oamp->refcnt == 1);
60010Sstevel@tonic-gate 		nahp = anon_create(btop(seg->s_size), ANON_SLEEP);
60020Sstevel@tonic-gate 		(void) anon_copy_ptr(oamp->ahp, svd->anon_index,
60030Sstevel@tonic-gate 		    nahp, 0, btop(seg->s_size), ANON_SLEEP);
60040Sstevel@tonic-gate 
60050Sstevel@tonic-gate 		namp = anonmap_alloc(nseg->s_size, 0);
60060Sstevel@tonic-gate 		namp->a_szc = nseg->s_szc;
60070Sstevel@tonic-gate 		(void) anon_copy_ptr(oamp->ahp,
60080Sstevel@tonic-gate 		    svd->anon_index + btop(seg->s_size),
60090Sstevel@tonic-gate 		    namp->ahp, 0, btop(nseg->s_size), ANON_SLEEP);
60100Sstevel@tonic-gate 		anon_release(oamp->ahp, btop(oamp->size));
60110Sstevel@tonic-gate 		oamp->ahp = nahp;
60120Sstevel@tonic-gate 		oamp->size = seg->s_size;
60130Sstevel@tonic-gate 		svd->anon_index = 0;
60140Sstevel@tonic-gate 		nsvd->amp = namp;
60150Sstevel@tonic-gate 		nsvd->anon_index = 0;
60160Sstevel@tonic-gate 		ANON_LOCK_EXIT(&oamp->a_rwlock);
60172414Saguzovsk 	} else if (svd->amp != NULL) {
60182414Saguzovsk 		pgcnt_t pgcnt = page_get_pagecnt(seg->s_szc);
60192414Saguzovsk 		ASSERT(svd->amp == nsvd->amp);
60202414Saguzovsk 		ASSERT(seg->s_szc <= svd->amp->a_szc);
60212414Saguzovsk 		nsvd->anon_index = svd->anon_index + seg_pages(seg);
60222414Saguzovsk 		ASSERT(IS_P2ALIGNED(nsvd->anon_index, pgcnt));
60232414Saguzovsk 		ANON_LOCK_ENTER(&svd->amp->a_rwlock, RW_WRITER);
60242414Saguzovsk 		svd->amp->refcnt++;
60252414Saguzovsk 		ANON_LOCK_EXIT(&svd->amp->a_rwlock);
60260Sstevel@tonic-gate 	}
60270Sstevel@tonic-gate 
60280Sstevel@tonic-gate 	/*
60290Sstevel@tonic-gate 	 * Split amount of swap reserve
60300Sstevel@tonic-gate 	 */
60310Sstevel@tonic-gate 	if (svd->swresv) {
60320Sstevel@tonic-gate 		/*
60330Sstevel@tonic-gate 		 * For MAP_NORESERVE, only allocate swap reserve for pages
60340Sstevel@tonic-gate 		 * being used.  Other segments get enough to cover whole
60350Sstevel@tonic-gate 		 * segment.
60360Sstevel@tonic-gate 		 */
60370Sstevel@tonic-gate 		if (svd->flags & MAP_NORESERVE) {
60380Sstevel@tonic-gate 			size_t	oswresv;
60390Sstevel@tonic-gate 
60400Sstevel@tonic-gate 			ASSERT(svd->amp);
60410Sstevel@tonic-gate 			oswresv = svd->swresv;
60420Sstevel@tonic-gate 			svd->swresv = ptob(anon_pages(svd->amp->ahp,
60430Sstevel@tonic-gate 				svd->anon_index, btop(seg->s_size)));
60440Sstevel@tonic-gate 			nsvd->swresv = ptob(anon_pages(nsvd->amp->ahp,
60450Sstevel@tonic-gate 				nsvd->anon_index, btop(nseg->s_size)));
60460Sstevel@tonic-gate 			ASSERT(oswresv >= (svd->swresv + nsvd->swresv));
60470Sstevel@tonic-gate 		} else {
60480Sstevel@tonic-gate 			ASSERT(svd->swresv == seg->s_size + nseg->s_size);
60490Sstevel@tonic-gate 			svd->swresv = seg->s_size;
60500Sstevel@tonic-gate 			nsvd->swresv = nseg->s_size;
60510Sstevel@tonic-gate 		}
60520Sstevel@tonic-gate 	}
60530Sstevel@tonic-gate 
60540Sstevel@tonic-gate 	return (nseg);
60550Sstevel@tonic-gate }
60560Sstevel@tonic-gate 
60570Sstevel@tonic-gate /*
60580Sstevel@tonic-gate  * called on memory operations (unmap, setprot, setpagesize) for a subset
60590Sstevel@tonic-gate  * of a large page segment to either demote the memory range (SDR_RANGE)
60600Sstevel@tonic-gate  * or the ends (SDR_END) by addr/len.
60610Sstevel@tonic-gate  *
60620Sstevel@tonic-gate  * returns 0 on success. returns errno, including ENOMEM, on failure.
60630Sstevel@tonic-gate  */
60640Sstevel@tonic-gate static int
60652414Saguzovsk segvn_demote_range(
60662414Saguzovsk 	struct seg *seg,
60672414Saguzovsk 	caddr_t addr,
60682414Saguzovsk 	size_t len,
60692414Saguzovsk 	int flag,
60702414Saguzovsk 	uint_t szcvec)
60710Sstevel@tonic-gate {
60720Sstevel@tonic-gate 	caddr_t eaddr = addr + len;
60730Sstevel@tonic-gate 	caddr_t lpgaddr, lpgeaddr;
60740Sstevel@tonic-gate 	struct seg *nseg;
60750Sstevel@tonic-gate 	struct seg *badseg1 = NULL;
60760Sstevel@tonic-gate 	struct seg *badseg2 = NULL;
60770Sstevel@tonic-gate 	size_t pgsz;
60780Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
60790Sstevel@tonic-gate 	int err;
60802414Saguzovsk 	uint_t szc = seg->s_szc;
60812414Saguzovsk 	uint_t tszcvec;
60820Sstevel@tonic-gate 
60830Sstevel@tonic-gate 	ASSERT(AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
60842414Saguzovsk 	ASSERT(szc != 0);
60852414Saguzovsk 	pgsz = page_get_pagesize(szc);
60860Sstevel@tonic-gate 	ASSERT(seg->s_base != addr || seg->s_size != len);
60870Sstevel@tonic-gate 	ASSERT(addr >= seg->s_base && eaddr <= seg->s_base + seg->s_size);
60880Sstevel@tonic-gate 	ASSERT(svd->softlockcnt == 0);
60892414Saguzovsk 	ASSERT(szcvec == 0 || (flag == SDR_END && svd->type == MAP_SHARED));
60900Sstevel@tonic-gate 
60910Sstevel@tonic-gate 	CALC_LPG_REGION(pgsz, seg, addr, len, lpgaddr, lpgeaddr);
60920Sstevel@tonic-gate 	ASSERT(flag == SDR_RANGE || eaddr < lpgeaddr || addr > lpgaddr);
60930Sstevel@tonic-gate 	if (flag == SDR_RANGE) {
60940Sstevel@tonic-gate 		/* demote entire range */
60950Sstevel@tonic-gate 		badseg1 = nseg = segvn_split_seg(seg, lpgaddr);
60960Sstevel@tonic-gate 		(void) segvn_split_seg(nseg, lpgeaddr);
60970Sstevel@tonic-gate 		ASSERT(badseg1->s_base == lpgaddr);
60980Sstevel@tonic-gate 		ASSERT(badseg1->s_size == lpgeaddr - lpgaddr);
60990Sstevel@tonic-gate 	} else if (addr != lpgaddr) {
61000Sstevel@tonic-gate 		ASSERT(flag == SDR_END);
61010Sstevel@tonic-gate 		badseg1 = nseg = segvn_split_seg(seg, lpgaddr);
61020Sstevel@tonic-gate 		if (eaddr != lpgeaddr && eaddr > lpgaddr + pgsz &&
61030Sstevel@tonic-gate 		    eaddr < lpgaddr + 2 * pgsz) {
61040Sstevel@tonic-gate 			(void) segvn_split_seg(nseg, lpgeaddr);
61050Sstevel@tonic-gate 			ASSERT(badseg1->s_base == lpgaddr);
61060Sstevel@tonic-gate 			ASSERT(badseg1->s_size == 2 * pgsz);
61070Sstevel@tonic-gate 		} else {
61080Sstevel@tonic-gate 			nseg = segvn_split_seg(nseg, lpgaddr + pgsz);
61090Sstevel@tonic-gate 			ASSERT(badseg1->s_base == lpgaddr);
61100Sstevel@tonic-gate 			ASSERT(badseg1->s_size == pgsz);
61110Sstevel@tonic-gate 			if (eaddr != lpgeaddr && eaddr > lpgaddr + pgsz) {
61120Sstevel@tonic-gate 				ASSERT(lpgeaddr - lpgaddr > 2 * pgsz);
61130Sstevel@tonic-gate 				nseg = segvn_split_seg(nseg, lpgeaddr - pgsz);
61140Sstevel@tonic-gate 				badseg2 = nseg;
61150Sstevel@tonic-gate 				(void) segvn_split_seg(nseg, lpgeaddr);
61160Sstevel@tonic-gate 				ASSERT(badseg2->s_base == lpgeaddr - pgsz);
61170Sstevel@tonic-gate 				ASSERT(badseg2->s_size == pgsz);
61180Sstevel@tonic-gate 			}
61190Sstevel@tonic-gate 		}
61200Sstevel@tonic-gate 	} else {
61210Sstevel@tonic-gate 		ASSERT(flag == SDR_END);
61220Sstevel@tonic-gate 		ASSERT(eaddr < lpgeaddr);
61230Sstevel@tonic-gate 		badseg1 = nseg = segvn_split_seg(seg, lpgeaddr - pgsz);
61240Sstevel@tonic-gate 		(void) segvn_split_seg(nseg, lpgeaddr);
61250Sstevel@tonic-gate 		ASSERT(badseg1->s_base == lpgeaddr - pgsz);
61260Sstevel@tonic-gate 		ASSERT(badseg1->s_size == pgsz);
61270Sstevel@tonic-gate 	}
61280Sstevel@tonic-gate 
61290Sstevel@tonic-gate 	ASSERT(badseg1 != NULL);
61302414Saguzovsk 	ASSERT(badseg1->s_szc == szc);
61310Sstevel@tonic-gate 	ASSERT(flag == SDR_RANGE || badseg1->s_size == pgsz ||
61320Sstevel@tonic-gate 	    badseg1->s_size == 2 * pgsz);
61332414Saguzovsk 	ASSERT(sameprot(badseg1, badseg1->s_base, pgsz));
61342414Saguzovsk 	ASSERT(badseg1->s_size == pgsz ||
61352414Saguzovsk 	    sameprot(badseg1, badseg1->s_base + pgsz, pgsz));
61360Sstevel@tonic-gate 	if (err = segvn_clrszc(badseg1)) {
61370Sstevel@tonic-gate 		return (err);
61380Sstevel@tonic-gate 	}
61390Sstevel@tonic-gate 	ASSERT(badseg1->s_szc == 0);
61400Sstevel@tonic-gate 
61412414Saguzovsk 	if (szc > 1 && (tszcvec = P2PHASE(szcvec, 1 << szc)) > 1) {
61422414Saguzovsk 		uint_t tszc = highbit(tszcvec) - 1;
61432414Saguzovsk 		caddr_t ta = MAX(addr, badseg1->s_base);
61442414Saguzovsk 		caddr_t te;
61452414Saguzovsk 		size_t tpgsz = page_get_pagesize(tszc);
61462414Saguzovsk 
61472414Saguzovsk 		ASSERT(svd->type == MAP_SHARED);
61482414Saguzovsk 		ASSERT(flag == SDR_END);
61492414Saguzovsk 		ASSERT(tszc < szc && tszc > 0);
61502414Saguzovsk 
61512414Saguzovsk 		if (eaddr > badseg1->s_base + badseg1->s_size) {
61522414Saguzovsk 			te = badseg1->s_base + badseg1->s_size;
61532414Saguzovsk 		} else {
61542414Saguzovsk 			te = eaddr;
61552414Saguzovsk 		}
61562414Saguzovsk 
61572414Saguzovsk 		ASSERT(ta <= te);
61582414Saguzovsk 		badseg1->s_szc = tszc;
61592414Saguzovsk 		if (!IS_P2ALIGNED(ta, tpgsz) || !IS_P2ALIGNED(te, tpgsz)) {
61602414Saguzovsk 			if (badseg2 != NULL) {
61612414Saguzovsk 				err = segvn_demote_range(badseg1, ta, te - ta,
61622414Saguzovsk 				    SDR_END, tszcvec);
61632414Saguzovsk 				if (err != 0) {
61642414Saguzovsk 					return (err);
61652414Saguzovsk 				}
61662414Saguzovsk 			} else {
61672414Saguzovsk 				return (segvn_demote_range(badseg1, ta,
61682414Saguzovsk 				    te - ta, SDR_END, tszcvec));
61692414Saguzovsk 			}
61702414Saguzovsk 		}
61712414Saguzovsk 	}
61722414Saguzovsk 
61730Sstevel@tonic-gate 	if (badseg2 == NULL)
61740Sstevel@tonic-gate 		return (0);
61752414Saguzovsk 	ASSERT(badseg2->s_szc == szc);
61760Sstevel@tonic-gate 	ASSERT(badseg2->s_size == pgsz);
61770Sstevel@tonic-gate 	ASSERT(sameprot(badseg2, badseg2->s_base, badseg2->s_size));
61780Sstevel@tonic-gate 	if (err = segvn_clrszc(badseg2)) {
61790Sstevel@tonic-gate 		return (err);
61800Sstevel@tonic-gate 	}
61810Sstevel@tonic-gate 	ASSERT(badseg2->s_szc == 0);
61822414Saguzovsk 
61832414Saguzovsk 	if (szc > 1 && (tszcvec = P2PHASE(szcvec, 1 << szc)) > 1) {
61842414Saguzovsk 		uint_t tszc = highbit(tszcvec) - 1;
61852414Saguzovsk 		size_t tpgsz = page_get_pagesize(tszc);
61862414Saguzovsk 
61872414Saguzovsk 		ASSERT(svd->type == MAP_SHARED);
61882414Saguzovsk 		ASSERT(flag == SDR_END);
61892414Saguzovsk 		ASSERT(tszc < szc && tszc > 0);
61902414Saguzovsk 		ASSERT(badseg2->s_base > addr);
61912414Saguzovsk 		ASSERT(eaddr > badseg2->s_base);
61922414Saguzovsk 		ASSERT(eaddr < badseg2->s_base + badseg2->s_size);
61932414Saguzovsk 
61942414Saguzovsk 		badseg2->s_szc = tszc;
61952414Saguzovsk 		if (!IS_P2ALIGNED(eaddr, tpgsz)) {
61962414Saguzovsk 			return (segvn_demote_range(badseg2, badseg2->s_base,
61972414Saguzovsk 			    eaddr - badseg2->s_base, SDR_END, tszcvec));
61982414Saguzovsk 		}
61992414Saguzovsk 	}
62002414Saguzovsk 
62010Sstevel@tonic-gate 	return (0);
62020Sstevel@tonic-gate }
62030Sstevel@tonic-gate 
62040Sstevel@tonic-gate static int
62050Sstevel@tonic-gate segvn_checkprot(struct seg *seg, caddr_t addr, size_t len, uint_t prot)
62060Sstevel@tonic-gate {
62070Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
62080Sstevel@tonic-gate 	struct vpage *vp, *evp;
62090Sstevel@tonic-gate 
62100Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
62110Sstevel@tonic-gate 
62120Sstevel@tonic-gate 	SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
62130Sstevel@tonic-gate 	/*
62140Sstevel@tonic-gate 	 * If segment protection can be used, simply check against them.
62150Sstevel@tonic-gate 	 */
62160Sstevel@tonic-gate 	if (svd->pageprot == 0) {
62170Sstevel@tonic-gate 		int err;
62180Sstevel@tonic-gate 
62190Sstevel@tonic-gate 		err = ((svd->prot & prot) != prot) ? EACCES : 0;
62200Sstevel@tonic-gate 		SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
62210Sstevel@tonic-gate 		return (err);
62220Sstevel@tonic-gate 	}
62230Sstevel@tonic-gate 
62240Sstevel@tonic-gate 	/*
62250Sstevel@tonic-gate 	 * Have to check down to the vpage level.
62260Sstevel@tonic-gate 	 */
62270Sstevel@tonic-gate 	evp = &svd->vpage[seg_page(seg, addr + len)];
62280Sstevel@tonic-gate 	for (vp = &svd->vpage[seg_page(seg, addr)]; vp < evp; vp++) {
62290Sstevel@tonic-gate 		if ((VPP_PROT(vp) & prot) != prot) {
62300Sstevel@tonic-gate 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
62310Sstevel@tonic-gate 			return (EACCES);
62320Sstevel@tonic-gate 		}
62330Sstevel@tonic-gate 	}
62340Sstevel@tonic-gate 	SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
62350Sstevel@tonic-gate 	return (0);
62360Sstevel@tonic-gate }
62370Sstevel@tonic-gate 
62380Sstevel@tonic-gate static int
62390Sstevel@tonic-gate segvn_getprot(struct seg *seg, caddr_t addr, size_t len, uint_t *protv)
62400Sstevel@tonic-gate {
62410Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
62420Sstevel@tonic-gate 	size_t pgno = seg_page(seg, addr + len) - seg_page(seg, addr) + 1;
62430Sstevel@tonic-gate 
62440Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
62450Sstevel@tonic-gate 
62460Sstevel@tonic-gate 	if (pgno != 0) {
62470Sstevel@tonic-gate 		SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
62480Sstevel@tonic-gate 		if (svd->pageprot == 0) {
62490Sstevel@tonic-gate 			do
62500Sstevel@tonic-gate 				protv[--pgno] = svd->prot;
62510Sstevel@tonic-gate 			while (pgno != 0);
62520Sstevel@tonic-gate 		} else {
62530Sstevel@tonic-gate 			size_t pgoff = seg_page(seg, addr);
62540Sstevel@tonic-gate 
62550Sstevel@tonic-gate 			do {
62560Sstevel@tonic-gate 				pgno--;
62570Sstevel@tonic-gate 				protv[pgno] = VPP_PROT(&svd->vpage[pgno+pgoff]);
62580Sstevel@tonic-gate 			} while (pgno != 0);
62590Sstevel@tonic-gate 		}
62600Sstevel@tonic-gate 		SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
62610Sstevel@tonic-gate 	}
62620Sstevel@tonic-gate 	return (0);
62630Sstevel@tonic-gate }
62640Sstevel@tonic-gate 
62650Sstevel@tonic-gate static u_offset_t
62660Sstevel@tonic-gate segvn_getoffset(struct seg *seg, caddr_t addr)
62670Sstevel@tonic-gate {
62680Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
62690Sstevel@tonic-gate 
62700Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
62710Sstevel@tonic-gate 
62720Sstevel@tonic-gate 	return (svd->offset + (uintptr_t)(addr - seg->s_base));
62730Sstevel@tonic-gate }
62740Sstevel@tonic-gate 
62750Sstevel@tonic-gate /*ARGSUSED*/
62760Sstevel@tonic-gate static int
62770Sstevel@tonic-gate segvn_gettype(struct seg *seg, caddr_t addr)
62780Sstevel@tonic-gate {
62790Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
62800Sstevel@tonic-gate 
62810Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
62820Sstevel@tonic-gate 
62832991Ssusans 	return (svd->type | (svd->flags & (MAP_NORESERVE | MAP_TEXT |
62842991Ssusans 	    MAP_INITDATA)));
62850Sstevel@tonic-gate }
62860Sstevel@tonic-gate 
62870Sstevel@tonic-gate /*ARGSUSED*/
62880Sstevel@tonic-gate static int
62890Sstevel@tonic-gate segvn_getvp(struct seg *seg, caddr_t addr, struct vnode **vpp)
62900Sstevel@tonic-gate {
62910Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
62920Sstevel@tonic-gate 
62930Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
62940Sstevel@tonic-gate 
62950Sstevel@tonic-gate 	*vpp = svd->vp;
62960Sstevel@tonic-gate 	return (0);
62970Sstevel@tonic-gate }
62980Sstevel@tonic-gate 
62990Sstevel@tonic-gate /*
63000Sstevel@tonic-gate  * Check to see if it makes sense to do kluster/read ahead to
63010Sstevel@tonic-gate  * addr + delta relative to the mapping at addr.  We assume here
63020Sstevel@tonic-gate  * that delta is a signed PAGESIZE'd multiple (which can be negative).
63030Sstevel@tonic-gate  *
63040Sstevel@tonic-gate  * For segvn, we currently "approve" of the action if we are
63050Sstevel@tonic-gate  * still in the segment and it maps from the same vp/off,
63060Sstevel@tonic-gate  * or if the advice stored in segvn_data or vpages allows it.
63070Sstevel@tonic-gate  * Currently, klustering is not allowed only if MADV_RANDOM is set.
63080Sstevel@tonic-gate  */
63090Sstevel@tonic-gate static int
63100Sstevel@tonic-gate segvn_kluster(struct seg *seg, caddr_t addr, ssize_t delta)
63110Sstevel@tonic-gate {
63120Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
63130Sstevel@tonic-gate 	struct anon *oap, *ap;
63140Sstevel@tonic-gate 	ssize_t pd;
63150Sstevel@tonic-gate 	size_t page;
63160Sstevel@tonic-gate 	struct vnode *vp1, *vp2;
63170Sstevel@tonic-gate 	u_offset_t off1, off2;
63180Sstevel@tonic-gate 	struct anon_map *amp;
63190Sstevel@tonic-gate 
63200Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
63210Sstevel@tonic-gate 	ASSERT(AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock) ||
63220Sstevel@tonic-gate 	    SEGVN_LOCK_HELD(seg->s_as, &svd->lock));
63230Sstevel@tonic-gate 
63240Sstevel@tonic-gate 	if (addr + delta < seg->s_base ||
63250Sstevel@tonic-gate 	    addr + delta >= (seg->s_base + seg->s_size))
63260Sstevel@tonic-gate 		return (-1);		/* exceeded segment bounds */
63270Sstevel@tonic-gate 
63280Sstevel@tonic-gate 	pd = delta / (ssize_t)PAGESIZE;	/* divide to preserve sign bit */
63290Sstevel@tonic-gate 	page = seg_page(seg, addr);
63300Sstevel@tonic-gate 
63310Sstevel@tonic-gate 	/*
63320Sstevel@tonic-gate 	 * Check to see if either of the pages addr or addr + delta
63330Sstevel@tonic-gate 	 * have advice set that prevents klustering (if MADV_RANDOM advice
63340Sstevel@tonic-gate 	 * is set for entire segment, or MADV_SEQUENTIAL is set and delta
63350Sstevel@tonic-gate 	 * is negative).
63360Sstevel@tonic-gate 	 */
63370Sstevel@tonic-gate 	if (svd->advice == MADV_RANDOM ||
63380Sstevel@tonic-gate 	    svd->advice == MADV_SEQUENTIAL && delta < 0)
63390Sstevel@tonic-gate 		return (-1);
63400Sstevel@tonic-gate 	else if (svd->pageadvice && svd->vpage) {
63410Sstevel@tonic-gate 		struct vpage *bvpp, *evpp;
63420Sstevel@tonic-gate 
63430Sstevel@tonic-gate 		bvpp = &svd->vpage[page];
63440Sstevel@tonic-gate 		evpp = &svd->vpage[page + pd];
63450Sstevel@tonic-gate 		if (VPP_ADVICE(bvpp) == MADV_RANDOM ||
63460Sstevel@tonic-gate 		    VPP_ADVICE(evpp) == MADV_SEQUENTIAL && delta < 0)
63470Sstevel@tonic-gate 			return (-1);
63480Sstevel@tonic-gate 		if (VPP_ADVICE(bvpp) != VPP_ADVICE(evpp) &&
63490Sstevel@tonic-gate 		    VPP_ADVICE(evpp) == MADV_RANDOM)
63500Sstevel@tonic-gate 			return (-1);
63510Sstevel@tonic-gate 	}
63520Sstevel@tonic-gate 
63530Sstevel@tonic-gate 	if (svd->type == MAP_SHARED)
63540Sstevel@tonic-gate 		return (0);		/* shared mapping - all ok */
63550Sstevel@tonic-gate 
63560Sstevel@tonic-gate 	if ((amp = svd->amp) == NULL)
63570Sstevel@tonic-gate 		return (0);		/* off original vnode */
63580Sstevel@tonic-gate 
63590Sstevel@tonic-gate 	page += svd->anon_index;
63600Sstevel@tonic-gate 
63610Sstevel@tonic-gate 	ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
63620Sstevel@tonic-gate 
63630Sstevel@tonic-gate 	oap = anon_get_ptr(amp->ahp, page);
63640Sstevel@tonic-gate 	ap = anon_get_ptr(amp->ahp, page + pd);
63650Sstevel@tonic-gate 
63660Sstevel@tonic-gate 	ANON_LOCK_EXIT(&amp->a_rwlock);
63670Sstevel@tonic-gate 
63680Sstevel@tonic-gate 	if ((oap == NULL && ap != NULL) || (oap != NULL && ap == NULL)) {
63690Sstevel@tonic-gate 		return (-1);		/* one with and one without an anon */
63700Sstevel@tonic-gate 	}
63710Sstevel@tonic-gate 
63720Sstevel@tonic-gate 	if (oap == NULL) {		/* implies that ap == NULL */
63730Sstevel@tonic-gate 		return (0);		/* off original vnode */
63740Sstevel@tonic-gate 	}
63750Sstevel@tonic-gate 
63760Sstevel@tonic-gate 	/*
63770Sstevel@tonic-gate 	 * Now we know we have two anon pointers - check to
63780Sstevel@tonic-gate 	 * see if they happen to be properly allocated.
63790Sstevel@tonic-gate 	 */
63800Sstevel@tonic-gate 
63810Sstevel@tonic-gate 	/*
63820Sstevel@tonic-gate 	 * XXX We cheat here and don't lock the anon slots. We can't because
63830Sstevel@tonic-gate 	 * we may have been called from the anon layer which might already
63840Sstevel@tonic-gate 	 * have locked them. We are holding a refcnt on the slots so they
63850Sstevel@tonic-gate 	 * can't disappear. The worst that will happen is we'll get the wrong
63860Sstevel@tonic-gate 	 * names (vp, off) for the slots and make a poor klustering decision.
63870Sstevel@tonic-gate 	 */
63880Sstevel@tonic-gate 	swap_xlate(ap, &vp1, &off1);
63890Sstevel@tonic-gate 	swap_xlate(oap, &vp2, &off2);
63900Sstevel@tonic-gate 
63910Sstevel@tonic-gate 
63920Sstevel@tonic-gate 	if (!VOP_CMP(vp1, vp2) || off1 - off2 != delta)
63930Sstevel@tonic-gate 		return (-1);
63940Sstevel@tonic-gate 	return (0);
63950Sstevel@tonic-gate }
63960Sstevel@tonic-gate 
63970Sstevel@tonic-gate /*
63980Sstevel@tonic-gate  * Swap the pages of seg out to secondary storage, returning the
63990Sstevel@tonic-gate  * number of bytes of storage freed.
64000Sstevel@tonic-gate  *
64010Sstevel@tonic-gate  * The basic idea is first to unload all translations and then to call
64020Sstevel@tonic-gate  * VOP_PUTPAGE() for all newly-unmapped pages, to push them out to the
64030Sstevel@tonic-gate  * swap device.  Pages to which other segments have mappings will remain
64040Sstevel@tonic-gate  * mapped and won't be swapped.  Our caller (as_swapout) has already
64050Sstevel@tonic-gate  * performed the unloading step.
64060Sstevel@tonic-gate  *
64070Sstevel@tonic-gate  * The value returned is intended to correlate well with the process's
64080Sstevel@tonic-gate  * memory requirements.  However, there are some caveats:
64090Sstevel@tonic-gate  * 1)	When given a shared segment as argument, this routine will
64100Sstevel@tonic-gate  *	only succeed in swapping out pages for the last sharer of the
64110Sstevel@tonic-gate  *	segment.  (Previous callers will only have decremented mapping
64120Sstevel@tonic-gate  *	reference counts.)
64130Sstevel@tonic-gate  * 2)	We assume that the hat layer maintains a large enough translation
64140Sstevel@tonic-gate  *	cache to capture process reference patterns.
64150Sstevel@tonic-gate  */
64160Sstevel@tonic-gate static size_t
64170Sstevel@tonic-gate segvn_swapout(struct seg *seg)
64180Sstevel@tonic-gate {
64190Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
64200Sstevel@tonic-gate 	struct anon_map *amp;
64210Sstevel@tonic-gate 	pgcnt_t pgcnt = 0;
64220Sstevel@tonic-gate 	pgcnt_t npages;
64230Sstevel@tonic-gate 	pgcnt_t page;
64240Sstevel@tonic-gate 	ulong_t anon_index;
64250Sstevel@tonic-gate 
64260Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
64270Sstevel@tonic-gate 
64280Sstevel@tonic-gate 	SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
64290Sstevel@tonic-gate 	/*
64300Sstevel@tonic-gate 	 * Find pages unmapped by our caller and force them
64310Sstevel@tonic-gate 	 * out to the virtual swap device.
64320Sstevel@tonic-gate 	 */
64330Sstevel@tonic-gate 	if ((amp = svd->amp) != NULL)
64340Sstevel@tonic-gate 		anon_index = svd->anon_index;
64350Sstevel@tonic-gate 	npages = seg->s_size >> PAGESHIFT;
64360Sstevel@tonic-gate 	for (page = 0; page < npages; page++) {
64370Sstevel@tonic-gate 		page_t *pp;
64380Sstevel@tonic-gate 		struct anon *ap;
64390Sstevel@tonic-gate 		struct vnode *vp;
64400Sstevel@tonic-gate 		u_offset_t off;
64410Sstevel@tonic-gate 		anon_sync_obj_t cookie;
64420Sstevel@tonic-gate 
64430Sstevel@tonic-gate 		/*
64440Sstevel@tonic-gate 		 * Obtain <vp, off> pair for the page, then look it up.
64450Sstevel@tonic-gate 		 *
64460Sstevel@tonic-gate 		 * Note that this code is willing to consider regular
64470Sstevel@tonic-gate 		 * pages as well as anon pages.  Is this appropriate here?
64480Sstevel@tonic-gate 		 */
64490Sstevel@tonic-gate 		ap = NULL;
64500Sstevel@tonic-gate 		if (amp != NULL) {
64510Sstevel@tonic-gate 			ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
6452888Scwb 			if (anon_array_try_enter(amp, anon_index + page,
6453888Scwb 						&cookie)) {
6454888Scwb 				ANON_LOCK_EXIT(&amp->a_rwlock);
6455888Scwb 				continue;
6456888Scwb 			}
64570Sstevel@tonic-gate 			ap = anon_get_ptr(amp->ahp, anon_index + page);
64580Sstevel@tonic-gate 			if (ap != NULL) {
64590Sstevel@tonic-gate 				swap_xlate(ap, &vp, &off);
64600Sstevel@tonic-gate 			} else {
64610Sstevel@tonic-gate 				vp = svd->vp;
64620Sstevel@tonic-gate 				off = svd->offset + ptob(page);
64630Sstevel@tonic-gate 			}
64640Sstevel@tonic-gate 			anon_array_exit(&cookie);
64650Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
64660Sstevel@tonic-gate 		} else {
64670Sstevel@tonic-gate 			vp = svd->vp;
64680Sstevel@tonic-gate 			off = svd->offset + ptob(page);
64690Sstevel@tonic-gate 		}
64700Sstevel@tonic-gate 		if (vp == NULL) {		/* untouched zfod page */
64710Sstevel@tonic-gate 			ASSERT(ap == NULL);
64720Sstevel@tonic-gate 			continue;
64730Sstevel@tonic-gate 		}
64740Sstevel@tonic-gate 
64750Sstevel@tonic-gate 		pp = page_lookup_nowait(vp, off, SE_SHARED);
64760Sstevel@tonic-gate 		if (pp == NULL)
64770Sstevel@tonic-gate 			continue;
64780Sstevel@tonic-gate 
64790Sstevel@tonic-gate 
64800Sstevel@tonic-gate 		/*
64810Sstevel@tonic-gate 		 * Examine the page to see whether it can be tossed out,
64820Sstevel@tonic-gate 		 * keeping track of how many we've found.
64830Sstevel@tonic-gate 		 */
64840Sstevel@tonic-gate 		if (!page_tryupgrade(pp)) {
64850Sstevel@tonic-gate 			/*
64860Sstevel@tonic-gate 			 * If the page has an i/o lock and no mappings,
64870Sstevel@tonic-gate 			 * it's very likely that the page is being
64880Sstevel@tonic-gate 			 * written out as a result of klustering.
64890Sstevel@tonic-gate 			 * Assume this is so and take credit for it here.
64900Sstevel@tonic-gate 			 */
64910Sstevel@tonic-gate 			if (!page_io_trylock(pp)) {
64920Sstevel@tonic-gate 				if (!hat_page_is_mapped(pp))
64930Sstevel@tonic-gate 					pgcnt++;
64940Sstevel@tonic-gate 			} else {
64950Sstevel@tonic-gate 				page_io_unlock(pp);
64960Sstevel@tonic-gate 			}
64970Sstevel@tonic-gate 			page_unlock(pp);
64980Sstevel@tonic-gate 			continue;
64990Sstevel@tonic-gate 		}
65000Sstevel@tonic-gate 		ASSERT(!page_iolock_assert(pp));
65010Sstevel@tonic-gate 
65020Sstevel@tonic-gate 
65030Sstevel@tonic-gate 		/*
65040Sstevel@tonic-gate 		 * Skip if page is locked or has mappings.
65050Sstevel@tonic-gate 		 * We don't need the page_struct_lock to look at lckcnt
65060Sstevel@tonic-gate 		 * and cowcnt because the page is exclusive locked.
65070Sstevel@tonic-gate 		 */
65080Sstevel@tonic-gate 		if (pp->p_lckcnt != 0 || pp->p_cowcnt != 0 ||
65090Sstevel@tonic-gate 		    hat_page_is_mapped(pp)) {
65100Sstevel@tonic-gate 			page_unlock(pp);
65110Sstevel@tonic-gate 			continue;
65120Sstevel@tonic-gate 		}
65130Sstevel@tonic-gate 
65140Sstevel@tonic-gate 		/*
65150Sstevel@tonic-gate 		 * dispose skips large pages so try to demote first.
65160Sstevel@tonic-gate 		 */
65170Sstevel@tonic-gate 		if (pp->p_szc != 0 && !page_try_demote_pages(pp)) {
65180Sstevel@tonic-gate 			page_unlock(pp);
65190Sstevel@tonic-gate 			/*
65200Sstevel@tonic-gate 			 * XXX should skip the remaining page_t's of this
65210Sstevel@tonic-gate 			 * large page.
65220Sstevel@tonic-gate 			 */
65230Sstevel@tonic-gate 			continue;
65240Sstevel@tonic-gate 		}
65250Sstevel@tonic-gate 
65260Sstevel@tonic-gate 		ASSERT(pp->p_szc == 0);
65270Sstevel@tonic-gate 
65280Sstevel@tonic-gate 		/*
65290Sstevel@tonic-gate 		 * No longer mapped -- we can toss it out.  How
65300Sstevel@tonic-gate 		 * we do so depends on whether or not it's dirty.
65310Sstevel@tonic-gate 		 */
65320Sstevel@tonic-gate 		if (hat_ismod(pp) && pp->p_vnode) {
65330Sstevel@tonic-gate 			/*
65340Sstevel@tonic-gate 			 * We must clean the page before it can be
65350Sstevel@tonic-gate 			 * freed.  Setting B_FREE will cause pvn_done
65360Sstevel@tonic-gate 			 * to free the page when the i/o completes.
65370Sstevel@tonic-gate 			 * XXX:	This also causes it to be accounted
65380Sstevel@tonic-gate 			 *	as a pageout instead of a swap: need
65390Sstevel@tonic-gate 			 *	B_SWAPOUT bit to use instead of B_FREE.
65400Sstevel@tonic-gate 			 *
65410Sstevel@tonic-gate 			 * Hold the vnode before releasing the page lock
65420Sstevel@tonic-gate 			 * to prevent it from being freed and re-used by
65430Sstevel@tonic-gate 			 * some other thread.
65440Sstevel@tonic-gate 			 */
65450Sstevel@tonic-gate 			VN_HOLD(vp);
65460Sstevel@tonic-gate 			page_unlock(pp);
65470Sstevel@tonic-gate 
65480Sstevel@tonic-gate 			/*
65490Sstevel@tonic-gate 			 * Queue all i/o requests for the pageout thread
65500Sstevel@tonic-gate 			 * to avoid saturating the pageout devices.
65510Sstevel@tonic-gate 			 */
65520Sstevel@tonic-gate 			if (!queue_io_request(vp, off))
65530Sstevel@tonic-gate 				VN_RELE(vp);
65540Sstevel@tonic-gate 		} else {
65550Sstevel@tonic-gate 			/*
65560Sstevel@tonic-gate 			 * The page was clean, free it.
65570Sstevel@tonic-gate 			 *
65580Sstevel@tonic-gate 			 * XXX:	Can we ever encounter modified pages
65590Sstevel@tonic-gate 			 *	with no associated vnode here?
65600Sstevel@tonic-gate 			 */
65610Sstevel@tonic-gate 			ASSERT(pp->p_vnode != NULL);
65620Sstevel@tonic-gate 			/*LINTED: constant in conditional context*/
65630Sstevel@tonic-gate 			VN_DISPOSE(pp, B_FREE, 0, kcred);
65640Sstevel@tonic-gate 		}
65650Sstevel@tonic-gate 
65660Sstevel@tonic-gate 		/*
65670Sstevel@tonic-gate 		 * Credit now even if i/o is in progress.
65680Sstevel@tonic-gate 		 */
65690Sstevel@tonic-gate 		pgcnt++;
65700Sstevel@tonic-gate 	}
65710Sstevel@tonic-gate 	SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
65720Sstevel@tonic-gate 
65730Sstevel@tonic-gate 	/*
65740Sstevel@tonic-gate 	 * Wakeup pageout to initiate i/o on all queued requests.
65750Sstevel@tonic-gate 	 */
65760Sstevel@tonic-gate 	cv_signal_pageout();
65770Sstevel@tonic-gate 	return (ptob(pgcnt));
65780Sstevel@tonic-gate }
65790Sstevel@tonic-gate 
65800Sstevel@tonic-gate /*
65810Sstevel@tonic-gate  * Synchronize primary storage cache with real object in virtual memory.
65820Sstevel@tonic-gate  *
65830Sstevel@tonic-gate  * XXX - Anonymous pages should not be sync'ed out at all.
65840Sstevel@tonic-gate  */
65850Sstevel@tonic-gate static int
65860Sstevel@tonic-gate segvn_sync(struct seg *seg, caddr_t addr, size_t len, int attr, uint_t flags)
65870Sstevel@tonic-gate {
65880Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
65890Sstevel@tonic-gate 	struct vpage *vpp;
65900Sstevel@tonic-gate 	page_t *pp;
65910Sstevel@tonic-gate 	u_offset_t offset;
65920Sstevel@tonic-gate 	struct vnode *vp;
65930Sstevel@tonic-gate 	u_offset_t off;
65940Sstevel@tonic-gate 	caddr_t eaddr;
65950Sstevel@tonic-gate 	int bflags;
65960Sstevel@tonic-gate 	int err = 0;
65970Sstevel@tonic-gate 	int segtype;
65980Sstevel@tonic-gate 	int pageprot;
65990Sstevel@tonic-gate 	int prot;
66000Sstevel@tonic-gate 	ulong_t anon_index;
66010Sstevel@tonic-gate 	struct anon_map *amp;
66020Sstevel@tonic-gate 	struct anon *ap;
66030Sstevel@tonic-gate 	anon_sync_obj_t cookie;
66040Sstevel@tonic-gate 
66050Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
66060Sstevel@tonic-gate 
66070Sstevel@tonic-gate 	SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
66080Sstevel@tonic-gate 
66090Sstevel@tonic-gate 	if (svd->softlockcnt > 0) {
66100Sstevel@tonic-gate 		/*
66110Sstevel@tonic-gate 		 * flush all pages from seg cache
66120Sstevel@tonic-gate 		 * otherwise we may deadlock in swap_putpage
66130Sstevel@tonic-gate 		 * for B_INVAL page (4175402).
66140Sstevel@tonic-gate 		 *
66150Sstevel@tonic-gate 		 * Even if we grab segvn WRITER's lock or segp_slock
66160Sstevel@tonic-gate 		 * here, there might be another thread which could've
66170Sstevel@tonic-gate 		 * successfully performed lookup/insert just before
66180Sstevel@tonic-gate 		 * we acquired the lock here.  So, grabbing either
66190Sstevel@tonic-gate 		 * lock here is of not much use.  Until we devise
66200Sstevel@tonic-gate 		 * a strategy at upper layers to solve the
66210Sstevel@tonic-gate 		 * synchronization issues completely, we expect
66220Sstevel@tonic-gate 		 * applications to handle this appropriately.
66230Sstevel@tonic-gate 		 */
66240Sstevel@tonic-gate 		segvn_purge(seg);
66250Sstevel@tonic-gate 		if (svd->softlockcnt > 0) {
66260Sstevel@tonic-gate 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
66270Sstevel@tonic-gate 			return (EAGAIN);
66280Sstevel@tonic-gate 		}
66290Sstevel@tonic-gate 	}
66300Sstevel@tonic-gate 
66310Sstevel@tonic-gate 	vpp = svd->vpage;
66320Sstevel@tonic-gate 	offset = svd->offset + (uintptr_t)(addr - seg->s_base);
66330Sstevel@tonic-gate 	bflags = ((flags & MS_ASYNC) ? B_ASYNC : 0) |
66340Sstevel@tonic-gate 	    ((flags & MS_INVALIDATE) ? B_INVAL : 0);
66350Sstevel@tonic-gate 
66360Sstevel@tonic-gate 	if (attr) {
66370Sstevel@tonic-gate 		pageprot = attr & ~(SHARED|PRIVATE);
66380Sstevel@tonic-gate 		segtype = (attr & SHARED) ? MAP_SHARED : MAP_PRIVATE;
66390Sstevel@tonic-gate 
66400Sstevel@tonic-gate 		/*
66410Sstevel@tonic-gate 		 * We are done if the segment types don't match
66420Sstevel@tonic-gate 		 * or if we have segment level protections and
66430Sstevel@tonic-gate 		 * they don't match.
66440Sstevel@tonic-gate 		 */
66450Sstevel@tonic-gate 		if (svd->type != segtype) {
66460Sstevel@tonic-gate 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
66470Sstevel@tonic-gate 			return (0);
66480Sstevel@tonic-gate 		}
66490Sstevel@tonic-gate 		if (vpp == NULL) {
66500Sstevel@tonic-gate 			if (svd->prot != pageprot) {
66510Sstevel@tonic-gate 				SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
66520Sstevel@tonic-gate 				return (0);
66530Sstevel@tonic-gate 			}
66540Sstevel@tonic-gate 			prot = svd->prot;
66550Sstevel@tonic-gate 		} else
66560Sstevel@tonic-gate 			vpp = &svd->vpage[seg_page(seg, addr)];
66570Sstevel@tonic-gate 
66580Sstevel@tonic-gate 	} else if (svd->vp && svd->amp == NULL &&
66590Sstevel@tonic-gate 	    (flags & MS_INVALIDATE) == 0) {
66600Sstevel@tonic-gate 
66610Sstevel@tonic-gate 		/*
66620Sstevel@tonic-gate 		 * No attributes, no anonymous pages and MS_INVALIDATE flag
66630Sstevel@tonic-gate 		 * is not on, just use one big request.
66640Sstevel@tonic-gate 		 */
66650Sstevel@tonic-gate 		err = VOP_PUTPAGE(svd->vp, (offset_t)offset, len,
66660Sstevel@tonic-gate 		    bflags, svd->cred);
66670Sstevel@tonic-gate 		SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
66680Sstevel@tonic-gate 		return (err);
66690Sstevel@tonic-gate 	}
66700Sstevel@tonic-gate 
66710Sstevel@tonic-gate 	if ((amp = svd->amp) != NULL)
66720Sstevel@tonic-gate 		anon_index = svd->anon_index + seg_page(seg, addr);
66730Sstevel@tonic-gate 
66740Sstevel@tonic-gate 	for (eaddr = addr + len; addr < eaddr; addr += PAGESIZE) {
66750Sstevel@tonic-gate 		ap = NULL;
66760Sstevel@tonic-gate 		if (amp != NULL) {
66770Sstevel@tonic-gate 			ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
66780Sstevel@tonic-gate 			anon_array_enter(amp, anon_index, &cookie);
66790Sstevel@tonic-gate 			ap = anon_get_ptr(amp->ahp, anon_index++);
66800Sstevel@tonic-gate 			if (ap != NULL) {
66810Sstevel@tonic-gate 				swap_xlate(ap, &vp, &off);
66820Sstevel@tonic-gate 			} else {
66830Sstevel@tonic-gate 				vp = svd->vp;
66840Sstevel@tonic-gate 				off = offset;
66850Sstevel@tonic-gate 			}
66860Sstevel@tonic-gate 			anon_array_exit(&cookie);
66870Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
66880Sstevel@tonic-gate 		} else {
66890Sstevel@tonic-gate 			vp = svd->vp;
66900Sstevel@tonic-gate 			off = offset;
66910Sstevel@tonic-gate 		}
66920Sstevel@tonic-gate 		offset += PAGESIZE;
66930Sstevel@tonic-gate 
66940Sstevel@tonic-gate 		if (vp == NULL)		/* untouched zfod page */
66950Sstevel@tonic-gate 			continue;
66960Sstevel@tonic-gate 
66970Sstevel@tonic-gate 		if (attr) {
66980Sstevel@tonic-gate 			if (vpp) {
66990Sstevel@tonic-gate 				prot = VPP_PROT(vpp);
67000Sstevel@tonic-gate 				vpp++;
67010Sstevel@tonic-gate 			}
67020Sstevel@tonic-gate 			if (prot != pageprot) {
67030Sstevel@tonic-gate 				continue;
67040Sstevel@tonic-gate 			}
67050Sstevel@tonic-gate 		}
67060Sstevel@tonic-gate 
67070Sstevel@tonic-gate 		/*
67080Sstevel@tonic-gate 		 * See if any of these pages are locked --  if so, then we
67090Sstevel@tonic-gate 		 * will have to truncate an invalidate request at the first
67100Sstevel@tonic-gate 		 * locked one. We don't need the page_struct_lock to test
67110Sstevel@tonic-gate 		 * as this is only advisory; even if we acquire it someone
67120Sstevel@tonic-gate 		 * might race in and lock the page after we unlock and before
67130Sstevel@tonic-gate 		 * we do the PUTPAGE, then PUTPAGE simply does nothing.
67140Sstevel@tonic-gate 		 */
67150Sstevel@tonic-gate 		if (flags & MS_INVALIDATE) {
67160Sstevel@tonic-gate 			if ((pp = page_lookup(vp, off, SE_SHARED)) != NULL) {
67170Sstevel@tonic-gate 				if (pp->p_lckcnt != 0 || pp->p_cowcnt != 0) {
67180Sstevel@tonic-gate 					page_unlock(pp);
67190Sstevel@tonic-gate 					SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
67200Sstevel@tonic-gate 					return (EBUSY);
67210Sstevel@tonic-gate 				}
67220Sstevel@tonic-gate 				if (ap != NULL && pp->p_szc != 0 &&
67230Sstevel@tonic-gate 				    page_tryupgrade(pp)) {
67240Sstevel@tonic-gate 					if (pp->p_lckcnt == 0 &&
67250Sstevel@tonic-gate 					    pp->p_cowcnt == 0) {
67260Sstevel@tonic-gate 						/*
67270Sstevel@tonic-gate 						 * swapfs VN_DISPOSE() won't
67280Sstevel@tonic-gate 						 * invalidate large pages.
67290Sstevel@tonic-gate 						 * Attempt to demote.
67300Sstevel@tonic-gate 						 * XXX can't help it if it
67310Sstevel@tonic-gate 						 * fails. But for swapfs
67320Sstevel@tonic-gate 						 * pages it is no big deal.
67330Sstevel@tonic-gate 						 */
67340Sstevel@tonic-gate 						(void) page_try_demote_pages(
67350Sstevel@tonic-gate 						    pp);
67360Sstevel@tonic-gate 				    }
67370Sstevel@tonic-gate 				}
67380Sstevel@tonic-gate 				page_unlock(pp);
67390Sstevel@tonic-gate 			}
67400Sstevel@tonic-gate 		} else if (svd->type == MAP_SHARED && amp != NULL) {
67410Sstevel@tonic-gate 			/*
67420Sstevel@tonic-gate 			 * Avoid writting out to disk ISM's large pages
67430Sstevel@tonic-gate 			 * because segspt_free_pages() relies on NULL an_pvp
67440Sstevel@tonic-gate 			 * of anon slots of such pages.
67450Sstevel@tonic-gate 			 */
67460Sstevel@tonic-gate 
67470Sstevel@tonic-gate 			ASSERT(svd->vp == NULL);
67480Sstevel@tonic-gate 			/*
67490Sstevel@tonic-gate 			 * swapfs uses page_lookup_nowait if not freeing or
67500Sstevel@tonic-gate 			 * invalidating and skips a page if
67510Sstevel@tonic-gate 			 * page_lookup_nowait returns NULL.
67520Sstevel@tonic-gate 			 */
67530Sstevel@tonic-gate 			pp = page_lookup_nowait(vp, off, SE_SHARED);
67540Sstevel@tonic-gate 			if (pp == NULL) {
67550Sstevel@tonic-gate 				continue;
67560Sstevel@tonic-gate 			}
67570Sstevel@tonic-gate 			if (pp->p_szc != 0) {
67580Sstevel@tonic-gate 				page_unlock(pp);
67590Sstevel@tonic-gate 				continue;
67600Sstevel@tonic-gate 			}
67610Sstevel@tonic-gate 
67620Sstevel@tonic-gate 			/*
67630Sstevel@tonic-gate 			 * Note ISM pages are created large so (vp, off)'s
67640Sstevel@tonic-gate 			 * page cannot suddenly become large after we unlock
67650Sstevel@tonic-gate 			 * pp.
67660Sstevel@tonic-gate 			 */
67670Sstevel@tonic-gate 			page_unlock(pp);
67680Sstevel@tonic-gate 		}
67690Sstevel@tonic-gate 		/*
67700Sstevel@tonic-gate 		 * XXX - Should ultimately try to kluster
67710Sstevel@tonic-gate 		 * calls to VOP_PUTPAGE() for performance.
67720Sstevel@tonic-gate 		 */
67730Sstevel@tonic-gate 		VN_HOLD(vp);
67740Sstevel@tonic-gate 		err = VOP_PUTPAGE(vp, (offset_t)off, PAGESIZE,
67750Sstevel@tonic-gate 		    bflags, svd->cred);
67760Sstevel@tonic-gate 		VN_RELE(vp);
67770Sstevel@tonic-gate 		if (err)
67780Sstevel@tonic-gate 			break;
67790Sstevel@tonic-gate 	}
67800Sstevel@tonic-gate 	SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
67810Sstevel@tonic-gate 	return (err);
67820Sstevel@tonic-gate }
67830Sstevel@tonic-gate 
67840Sstevel@tonic-gate /*
67850Sstevel@tonic-gate  * Determine if we have data corresponding to pages in the
67860Sstevel@tonic-gate  * primary storage virtual memory cache (i.e., "in core").
67870Sstevel@tonic-gate  */
67880Sstevel@tonic-gate static size_t
67890Sstevel@tonic-gate segvn_incore(struct seg *seg, caddr_t addr, size_t len, char *vec)
67900Sstevel@tonic-gate {
67910Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
67920Sstevel@tonic-gate 	struct vnode *vp, *avp;
67930Sstevel@tonic-gate 	u_offset_t offset, aoffset;
67940Sstevel@tonic-gate 	size_t p, ep;
67950Sstevel@tonic-gate 	int ret;
67960Sstevel@tonic-gate 	struct vpage *vpp;
67970Sstevel@tonic-gate 	page_t *pp;
67980Sstevel@tonic-gate 	uint_t start;
67990Sstevel@tonic-gate 	struct anon_map *amp;		/* XXX - for locknest */
68000Sstevel@tonic-gate 	struct anon *ap;
68010Sstevel@tonic-gate 	uint_t attr;
68020Sstevel@tonic-gate 	anon_sync_obj_t cookie;
68030Sstevel@tonic-gate 
68040Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
68050Sstevel@tonic-gate 
68060Sstevel@tonic-gate 	SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
68070Sstevel@tonic-gate 	if (svd->amp == NULL && svd->vp == NULL) {
68080Sstevel@tonic-gate 		SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
68090Sstevel@tonic-gate 		bzero(vec, btopr(len));
68100Sstevel@tonic-gate 		return (len);	/* no anonymous pages created yet */
68110Sstevel@tonic-gate 	}
68120Sstevel@tonic-gate 
68130Sstevel@tonic-gate 	p = seg_page(seg, addr);
68140Sstevel@tonic-gate 	ep = seg_page(seg, addr + len);
68150Sstevel@tonic-gate 	start = svd->vp ? SEG_PAGE_VNODEBACKED : 0;
68160Sstevel@tonic-gate 
68170Sstevel@tonic-gate 	amp = svd->amp;
68180Sstevel@tonic-gate 	for (; p < ep; p++, addr += PAGESIZE) {
68190Sstevel@tonic-gate 		vpp = (svd->vpage) ? &svd->vpage[p]: NULL;
68200Sstevel@tonic-gate 		ret = start;
68210Sstevel@tonic-gate 		ap = NULL;
68220Sstevel@tonic-gate 		avp = NULL;
68230Sstevel@tonic-gate 		/* Grab the vnode/offset for the anon slot */
68240Sstevel@tonic-gate 		if (amp != NULL) {
68250Sstevel@tonic-gate 			ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
68260Sstevel@tonic-gate 			anon_array_enter(amp, svd->anon_index + p, &cookie);
68270Sstevel@tonic-gate 			ap = anon_get_ptr(amp->ahp, svd->anon_index + p);
68280Sstevel@tonic-gate 			if (ap != NULL) {
68290Sstevel@tonic-gate 				swap_xlate(ap, &avp, &aoffset);
68300Sstevel@tonic-gate 			}
68310Sstevel@tonic-gate 			anon_array_exit(&cookie);
68320Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
68330Sstevel@tonic-gate 		}
68340Sstevel@tonic-gate 		if ((avp != NULL) && page_exists(avp, aoffset)) {
68350Sstevel@tonic-gate 			/* A page exists for the anon slot */
68360Sstevel@tonic-gate 			ret |= SEG_PAGE_INCORE;
68370Sstevel@tonic-gate 
68380Sstevel@tonic-gate 			/*
68390Sstevel@tonic-gate 			 * If page is mapped and writable
68400Sstevel@tonic-gate 			 */
68410Sstevel@tonic-gate 			attr = (uint_t)0;
68420Sstevel@tonic-gate 			if ((hat_getattr(seg->s_as->a_hat, addr,
68430Sstevel@tonic-gate 			    &attr) != -1) && (attr & PROT_WRITE)) {
68440Sstevel@tonic-gate 				ret |= SEG_PAGE_ANON;
68450Sstevel@tonic-gate 			}
68460Sstevel@tonic-gate 			/*
68470Sstevel@tonic-gate 			 * Don't get page_struct lock for lckcnt and cowcnt,
68480Sstevel@tonic-gate 			 * since this is purely advisory.
68490Sstevel@tonic-gate 			 */
68500Sstevel@tonic-gate 			if ((pp = page_lookup_nowait(avp, aoffset,
68510Sstevel@tonic-gate 			    SE_SHARED)) != NULL) {
68520Sstevel@tonic-gate 				if (pp->p_lckcnt)
68530Sstevel@tonic-gate 					ret |= SEG_PAGE_SOFTLOCK;
68540Sstevel@tonic-gate 				if (pp->p_cowcnt)
68550Sstevel@tonic-gate 					ret |= SEG_PAGE_HASCOW;
68560Sstevel@tonic-gate 				page_unlock(pp);
68570Sstevel@tonic-gate 			}
68580Sstevel@tonic-gate 		}
68590Sstevel@tonic-gate 
68600Sstevel@tonic-gate 		/* Gather vnode statistics */
68610Sstevel@tonic-gate 		vp = svd->vp;
68620Sstevel@tonic-gate 		offset = svd->offset + (uintptr_t)(addr - seg->s_base);
68630Sstevel@tonic-gate 
68640Sstevel@tonic-gate 		if (vp != NULL) {
68650Sstevel@tonic-gate 			/*
68660Sstevel@tonic-gate 			 * Try to obtain a "shared" lock on the page
68670Sstevel@tonic-gate 			 * without blocking.  If this fails, determine
68680Sstevel@tonic-gate 			 * if the page is in memory.
68690Sstevel@tonic-gate 			 */
68700Sstevel@tonic-gate 			pp = page_lookup_nowait(vp, offset, SE_SHARED);
68710Sstevel@tonic-gate 			if ((pp == NULL) && (page_exists(vp, offset))) {
68720Sstevel@tonic-gate 				/* Page is incore, and is named */
68730Sstevel@tonic-gate 				ret |= (SEG_PAGE_INCORE | SEG_PAGE_VNODE);
68740Sstevel@tonic-gate 			}
68750Sstevel@tonic-gate 			/*
68760Sstevel@tonic-gate 			 * Don't get page_struct lock for lckcnt and cowcnt,
68770Sstevel@tonic-gate 			 * since this is purely advisory.
68780Sstevel@tonic-gate 			 */
68790Sstevel@tonic-gate 			if (pp != NULL) {
68800Sstevel@tonic-gate 				ret |= (SEG_PAGE_INCORE | SEG_PAGE_VNODE);
68810Sstevel@tonic-gate 				if (pp->p_lckcnt)
68820Sstevel@tonic-gate 					ret |= SEG_PAGE_SOFTLOCK;
68830Sstevel@tonic-gate 				if (pp->p_cowcnt)
68840Sstevel@tonic-gate 					ret |= SEG_PAGE_HASCOW;
68850Sstevel@tonic-gate 				page_unlock(pp);
68860Sstevel@tonic-gate 			}
68870Sstevel@tonic-gate 		}
68880Sstevel@tonic-gate 
68890Sstevel@tonic-gate 		/* Gather virtual page information */
68900Sstevel@tonic-gate 		if (vpp) {
68910Sstevel@tonic-gate 			if (VPP_ISPPLOCK(vpp))
68920Sstevel@tonic-gate 				ret |= SEG_PAGE_LOCKED;
68930Sstevel@tonic-gate 			vpp++;
68940Sstevel@tonic-gate 		}
68950Sstevel@tonic-gate 
68960Sstevel@tonic-gate 		*vec++ = (char)ret;
68970Sstevel@tonic-gate 	}
68980Sstevel@tonic-gate 	SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
68990Sstevel@tonic-gate 	return (len);
69000Sstevel@tonic-gate }
69010Sstevel@tonic-gate 
69020Sstevel@tonic-gate /*
69030Sstevel@tonic-gate  * Statement for p_cowcnts/p_lckcnts.
69040Sstevel@tonic-gate  *
69050Sstevel@tonic-gate  * p_cowcnt is updated while mlock/munlocking MAP_PRIVATE and PROT_WRITE region
69060Sstevel@tonic-gate  * irrespective of the following factors or anything else:
69070Sstevel@tonic-gate  *
69080Sstevel@tonic-gate  *	(1) anon slots are populated or not
69090Sstevel@tonic-gate  *	(2) cow is broken or not
69100Sstevel@tonic-gate  *	(3) refcnt on ap is 1 or greater than 1
69110Sstevel@tonic-gate  *
69120Sstevel@tonic-gate  * If it's not MAP_PRIVATE and PROT_WRITE, p_lckcnt is updated during mlock
69130Sstevel@tonic-gate  * and munlock.
69140Sstevel@tonic-gate  *
69150Sstevel@tonic-gate  *
69160Sstevel@tonic-gate  * Handling p_cowcnts/p_lckcnts during copy-on-write fault:
69170Sstevel@tonic-gate  *
69180Sstevel@tonic-gate  *	if vpage has PROT_WRITE
69190Sstevel@tonic-gate  *		transfer cowcnt on the oldpage -> cowcnt on the newpage
69200Sstevel@tonic-gate  *	else
69210Sstevel@tonic-gate  *		transfer lckcnt on the oldpage -> lckcnt on the newpage
69220Sstevel@tonic-gate  *
69230Sstevel@tonic-gate  *	During copy-on-write, decrement p_cowcnt on the oldpage and increment
69240Sstevel@tonic-gate  *	p_cowcnt on the newpage *if* the corresponding vpage has PROT_WRITE.
69250Sstevel@tonic-gate  *
69260Sstevel@tonic-gate  *	We may also break COW if softlocking on read access in the physio case.
69270Sstevel@tonic-gate  *	In this case, vpage may not have PROT_WRITE. So, we need to decrement
69280Sstevel@tonic-gate  *	p_lckcnt on the oldpage and increment p_lckcnt on the newpage *if* the
69290Sstevel@tonic-gate  *	vpage doesn't have PROT_WRITE.
69300Sstevel@tonic-gate  *
69310Sstevel@tonic-gate  *
69320Sstevel@tonic-gate  * Handling p_cowcnts/p_lckcnts during mprotect on mlocked region:
69330Sstevel@tonic-gate  *
69340Sstevel@tonic-gate  * 	If a MAP_PRIVATE region loses PROT_WRITE, we decrement p_cowcnt and
69350Sstevel@tonic-gate  *	increment p_lckcnt by calling page_subclaim() which takes care of
69360Sstevel@tonic-gate  * 	availrmem accounting and p_lckcnt overflow.
69370Sstevel@tonic-gate  *
69380Sstevel@tonic-gate  *	If a MAP_PRIVATE region gains PROT_WRITE, we decrement p_lckcnt and
69390Sstevel@tonic-gate  *	increment p_cowcnt by calling page_addclaim() which takes care of
69400Sstevel@tonic-gate  *	availrmem availability and p_cowcnt overflow.
69410Sstevel@tonic-gate  */
69420Sstevel@tonic-gate 
69430Sstevel@tonic-gate /*
69440Sstevel@tonic-gate  * Lock down (or unlock) pages mapped by this segment.
69450Sstevel@tonic-gate  *
69460Sstevel@tonic-gate  * XXX only creates PAGESIZE pages if anon slots are not initialized.
69470Sstevel@tonic-gate  * At fault time they will be relocated into larger pages.
69480Sstevel@tonic-gate  */
69490Sstevel@tonic-gate static int
69500Sstevel@tonic-gate segvn_lockop(struct seg *seg, caddr_t addr, size_t len,
69510Sstevel@tonic-gate     int attr, int op, ulong_t *lockmap, size_t pos)
69520Sstevel@tonic-gate {
69530Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
69540Sstevel@tonic-gate 	struct vpage *vpp;
69550Sstevel@tonic-gate 	struct vpage *evp;
69560Sstevel@tonic-gate 	page_t *pp;
69570Sstevel@tonic-gate 	u_offset_t offset;
69580Sstevel@tonic-gate 	u_offset_t off;
69590Sstevel@tonic-gate 	int segtype;
69600Sstevel@tonic-gate 	int pageprot;
69610Sstevel@tonic-gate 	int claim;
69620Sstevel@tonic-gate 	struct vnode *vp;
69630Sstevel@tonic-gate 	ulong_t anon_index;
69640Sstevel@tonic-gate 	struct anon_map *amp;
69650Sstevel@tonic-gate 	struct anon *ap;
69660Sstevel@tonic-gate 	struct vattr va;
69670Sstevel@tonic-gate 	anon_sync_obj_t cookie;
69682768Ssl108498 	struct kshmid *sp = NULL;
69692768Ssl108498 	struct proc	*p = curproc;
69702768Ssl108498 	kproject_t	*proj = NULL;
69712768Ssl108498 	int chargeproc = 1;
69722768Ssl108498 	size_t locked_bytes = 0;
69732768Ssl108498 	size_t unlocked_bytes = 0;
69742768Ssl108498 	int err = 0;
69750Sstevel@tonic-gate 
69760Sstevel@tonic-gate 	/*
69770Sstevel@tonic-gate 	 * Hold write lock on address space because may split or concatenate
69780Sstevel@tonic-gate 	 * segments
69790Sstevel@tonic-gate 	 */
69800Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
69810Sstevel@tonic-gate 
69822768Ssl108498 	/*
69832768Ssl108498 	 * If this is a shm, use shm's project and zone, else use
69842768Ssl108498 	 * project and zone of calling process
69852768Ssl108498 	 */
69862768Ssl108498 
69872768Ssl108498 	/* Determine if this segment backs a sysV shm */
69882768Ssl108498 	if (svd->amp != NULL && svd->amp->a_sp != NULL) {
69892768Ssl108498 		sp = svd->amp->a_sp;
69902768Ssl108498 		proj = sp->shm_perm.ipc_proj;
69912768Ssl108498 		chargeproc = 0;
69922768Ssl108498 	}
69932768Ssl108498 
69940Sstevel@tonic-gate 	SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_WRITER);
69950Sstevel@tonic-gate 	if (attr) {
69960Sstevel@tonic-gate 		pageprot = attr & ~(SHARED|PRIVATE);
69970Sstevel@tonic-gate 		segtype = attr & SHARED ? MAP_SHARED : MAP_PRIVATE;
69980Sstevel@tonic-gate 
69990Sstevel@tonic-gate 		/*
70000Sstevel@tonic-gate 		 * We are done if the segment types don't match
70010Sstevel@tonic-gate 		 * or if we have segment level protections and
70020Sstevel@tonic-gate 		 * they don't match.
70030Sstevel@tonic-gate 		 */
70040Sstevel@tonic-gate 		if (svd->type != segtype) {
70050Sstevel@tonic-gate 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
70060Sstevel@tonic-gate 			return (0);
70070Sstevel@tonic-gate 		}
70080Sstevel@tonic-gate 		if (svd->pageprot == 0 && svd->prot != pageprot) {
70090Sstevel@tonic-gate 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
70100Sstevel@tonic-gate 			return (0);
70110Sstevel@tonic-gate 		}
70120Sstevel@tonic-gate 	}
70130Sstevel@tonic-gate 
70140Sstevel@tonic-gate 	/*
70150Sstevel@tonic-gate 	 * If we're locking, then we must create a vpage structure if
70160Sstevel@tonic-gate 	 * none exists.  If we're unlocking, then check to see if there
70170Sstevel@tonic-gate 	 * is a vpage --  if not, then we could not have locked anything.
70180Sstevel@tonic-gate 	 */
70190Sstevel@tonic-gate 
70200Sstevel@tonic-gate 	if ((vpp = svd->vpage) == NULL) {
70210Sstevel@tonic-gate 		if (op == MC_LOCK)
70220Sstevel@tonic-gate 			segvn_vpage(seg);
70230Sstevel@tonic-gate 		else {
70240Sstevel@tonic-gate 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
70250Sstevel@tonic-gate 			return (0);
70260Sstevel@tonic-gate 		}
70270Sstevel@tonic-gate 	}
70280Sstevel@tonic-gate 
70290Sstevel@tonic-gate 	/*
70300Sstevel@tonic-gate 	 * The anonymous data vector (i.e., previously
70310Sstevel@tonic-gate 	 * unreferenced mapping to swap space) can be allocated
70320Sstevel@tonic-gate 	 * by lazily testing for its existence.
70330Sstevel@tonic-gate 	 */
70340Sstevel@tonic-gate 	if (op == MC_LOCK && svd->amp == NULL && svd->vp == NULL) {
70350Sstevel@tonic-gate 		svd->amp = anonmap_alloc(seg->s_size, 0);
70360Sstevel@tonic-gate 		svd->amp->a_szc = seg->s_szc;
70370Sstevel@tonic-gate 	}
70380Sstevel@tonic-gate 
70390Sstevel@tonic-gate 	if ((amp = svd->amp) != NULL) {
70400Sstevel@tonic-gate 		anon_index = svd->anon_index + seg_page(seg, addr);
70410Sstevel@tonic-gate 	}
70420Sstevel@tonic-gate 
70430Sstevel@tonic-gate 	offset = svd->offset + (uintptr_t)(addr - seg->s_base);
70440Sstevel@tonic-gate 	evp = &svd->vpage[seg_page(seg, addr + len)];
70450Sstevel@tonic-gate 
70462768Ssl108498 	if (sp != NULL)
70472768Ssl108498 		mutex_enter(&sp->shm_mlock);
70482768Ssl108498 
70492768Ssl108498 	/* determine number of unlocked bytes in range for lock operation */
70502768Ssl108498 	if (op == MC_LOCK) {
70512768Ssl108498 
70522768Ssl108498 		if (sp == NULL) {
70532768Ssl108498 			for (vpp = &svd->vpage[seg_page(seg, addr)]; vpp < evp;
70542768Ssl108498 			    vpp++) {
70552768Ssl108498 				if (!VPP_ISPPLOCK(vpp))
70562768Ssl108498 					unlocked_bytes += PAGESIZE;
70572768Ssl108498 			}
70582768Ssl108498 		} else {
70592768Ssl108498 			ulong_t		i_idx, i_edx;
70602768Ssl108498 			anon_sync_obj_t	i_cookie;
70612768Ssl108498 			struct anon	*i_ap;
70622768Ssl108498 			struct vnode	*i_vp;
70632768Ssl108498 			u_offset_t	i_off;
70642768Ssl108498 
70652768Ssl108498 			/* Only count sysV pages once for locked memory */
70662768Ssl108498 			i_edx = svd->anon_index + seg_page(seg, addr + len);
70672768Ssl108498 			ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
70682768Ssl108498 			for (i_idx = anon_index; i_idx < i_edx; i_idx++) {
70692768Ssl108498 				anon_array_enter(amp, i_idx, &i_cookie);
70702768Ssl108498 				i_ap = anon_get_ptr(amp->ahp, i_idx);
70712768Ssl108498 				if (i_ap == NULL) {
70722768Ssl108498 					unlocked_bytes += PAGESIZE;
70732768Ssl108498 					anon_array_exit(&i_cookie);
70742768Ssl108498 					continue;
70752768Ssl108498 				}
70762768Ssl108498 				swap_xlate(i_ap, &i_vp, &i_off);
70772768Ssl108498 				anon_array_exit(&i_cookie);
70782768Ssl108498 				pp = page_lookup(i_vp, i_off, SE_SHARED);
70792768Ssl108498 				if (pp == NULL) {
70802768Ssl108498 					unlocked_bytes += PAGESIZE;
70812768Ssl108498 					continue;
70822768Ssl108498 				} else if (pp->p_lckcnt == 0)
70832768Ssl108498 					unlocked_bytes += PAGESIZE;
70842768Ssl108498 				page_unlock(pp);
70852768Ssl108498 			}
70862768Ssl108498 			ANON_LOCK_EXIT(&amp->a_rwlock);
70872768Ssl108498 		}
70882768Ssl108498 
70892768Ssl108498 		mutex_enter(&p->p_lock);
70902768Ssl108498 		err = rctl_incr_locked_mem(p, proj, unlocked_bytes,
70912768Ssl108498 		    chargeproc);
70922768Ssl108498 		mutex_exit(&p->p_lock);
70932768Ssl108498 
70942768Ssl108498 		if (err) {
70952768Ssl108498 			if (sp != NULL)
70962768Ssl108498 				mutex_exit(&sp->shm_mlock);
70972768Ssl108498 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
70982768Ssl108498 			return (err);
70992768Ssl108498 		}
71002768Ssl108498 	}
71010Sstevel@tonic-gate 	/*
71020Sstevel@tonic-gate 	 * Loop over all pages in the range.  Process if we're locking and
71030Sstevel@tonic-gate 	 * page has not already been locked in this mapping; or if we're
71040Sstevel@tonic-gate 	 * unlocking and the page has been locked.
71050Sstevel@tonic-gate 	 */
71060Sstevel@tonic-gate 	for (vpp = &svd->vpage[seg_page(seg, addr)]; vpp < evp;
71070Sstevel@tonic-gate 	    vpp++, pos++, addr += PAGESIZE, offset += PAGESIZE, anon_index++) {
71080Sstevel@tonic-gate 		if ((attr == 0 || VPP_PROT(vpp) == pageprot) &&
71090Sstevel@tonic-gate 		    ((op == MC_LOCK && !VPP_ISPPLOCK(vpp)) ||
71100Sstevel@tonic-gate 		    (op == MC_UNLOCK && VPP_ISPPLOCK(vpp)))) {
71110Sstevel@tonic-gate 
71120Sstevel@tonic-gate 			if (amp != NULL)
71130Sstevel@tonic-gate 				ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
71140Sstevel@tonic-gate 			/*
71150Sstevel@tonic-gate 			 * If this isn't a MAP_NORESERVE segment and
71160Sstevel@tonic-gate 			 * we're locking, allocate anon slots if they
71170Sstevel@tonic-gate 			 * don't exist.  The page is brought in later on.
71180Sstevel@tonic-gate 			 */
71190Sstevel@tonic-gate 			if (op == MC_LOCK && svd->vp == NULL &&
71200Sstevel@tonic-gate 			    ((svd->flags & MAP_NORESERVE) == 0) &&
71210Sstevel@tonic-gate 			    amp != NULL &&
71220Sstevel@tonic-gate 			    ((ap = anon_get_ptr(amp->ahp, anon_index))
71230Sstevel@tonic-gate 								== NULL)) {
71240Sstevel@tonic-gate 				anon_array_enter(amp, anon_index, &cookie);
71250Sstevel@tonic-gate 
71260Sstevel@tonic-gate 				if ((ap = anon_get_ptr(amp->ahp,
71270Sstevel@tonic-gate 						anon_index)) == NULL) {
71280Sstevel@tonic-gate 					pp = anon_zero(seg, addr, &ap,
71290Sstevel@tonic-gate 					    svd->cred);
71300Sstevel@tonic-gate 					if (pp == NULL) {
71310Sstevel@tonic-gate 						anon_array_exit(&cookie);
71320Sstevel@tonic-gate 						ANON_LOCK_EXIT(&amp->a_rwlock);
71332768Ssl108498 						err = ENOMEM;
71342768Ssl108498 						goto out;
71350Sstevel@tonic-gate 					}
71360Sstevel@tonic-gate 					ASSERT(anon_get_ptr(amp->ahp,
71370Sstevel@tonic-gate 						anon_index) == NULL);
71380Sstevel@tonic-gate 					(void) anon_set_ptr(amp->ahp,
71390Sstevel@tonic-gate 						anon_index, ap, ANON_SLEEP);
71400Sstevel@tonic-gate 					page_unlock(pp);
71410Sstevel@tonic-gate 				}
71420Sstevel@tonic-gate 				anon_array_exit(&cookie);
71430Sstevel@tonic-gate 			}
71440Sstevel@tonic-gate 
71450Sstevel@tonic-gate 			/*
71460Sstevel@tonic-gate 			 * Get name for page, accounting for
71470Sstevel@tonic-gate 			 * existence of private copy.
71480Sstevel@tonic-gate 			 */
71490Sstevel@tonic-gate 			ap = NULL;
71500Sstevel@tonic-gate 			if (amp != NULL) {
71510Sstevel@tonic-gate 				anon_array_enter(amp, anon_index, &cookie);
71520Sstevel@tonic-gate 				ap = anon_get_ptr(amp->ahp, anon_index);
71530Sstevel@tonic-gate 				if (ap != NULL) {
71540Sstevel@tonic-gate 					swap_xlate(ap, &vp, &off);
71550Sstevel@tonic-gate 				} else {
71560Sstevel@tonic-gate 					if (svd->vp == NULL &&
71570Sstevel@tonic-gate 					    (svd->flags & MAP_NORESERVE)) {
71580Sstevel@tonic-gate 						anon_array_exit(&cookie);
71590Sstevel@tonic-gate 						ANON_LOCK_EXIT(&amp->a_rwlock);
71600Sstevel@tonic-gate 						continue;
71610Sstevel@tonic-gate 					}
71620Sstevel@tonic-gate 					vp = svd->vp;
71630Sstevel@tonic-gate 					off = offset;
71640Sstevel@tonic-gate 				}
71650Sstevel@tonic-gate 				anon_array_exit(&cookie);
71660Sstevel@tonic-gate 				ANON_LOCK_EXIT(&amp->a_rwlock);
71670Sstevel@tonic-gate 			} else {
71680Sstevel@tonic-gate 				vp = svd->vp;
71690Sstevel@tonic-gate 				off = offset;
71700Sstevel@tonic-gate 			}
71710Sstevel@tonic-gate 
71720Sstevel@tonic-gate 			/*
71730Sstevel@tonic-gate 			 * Get page frame.  It's ok if the page is
71740Sstevel@tonic-gate 			 * not available when we're unlocking, as this
71750Sstevel@tonic-gate 			 * may simply mean that a page we locked got
71760Sstevel@tonic-gate 			 * truncated out of existence after we locked it.
71770Sstevel@tonic-gate 			 *
71780Sstevel@tonic-gate 			 * Invoke VOP_GETPAGE() to obtain the page struct
71790Sstevel@tonic-gate 			 * since we may need to read it from disk if its
71800Sstevel@tonic-gate 			 * been paged out.
71810Sstevel@tonic-gate 			 */
71820Sstevel@tonic-gate 			if (op != MC_LOCK)
71830Sstevel@tonic-gate 				pp = page_lookup(vp, off, SE_SHARED);
71840Sstevel@tonic-gate 			else {
71850Sstevel@tonic-gate 				page_t *pl[1 + 1];
71860Sstevel@tonic-gate 				int error;
71870Sstevel@tonic-gate 
71880Sstevel@tonic-gate 				ASSERT(vp != NULL);
71890Sstevel@tonic-gate 
71900Sstevel@tonic-gate 				error = VOP_GETPAGE(vp, (offset_t)off, PAGESIZE,
71910Sstevel@tonic-gate 				    (uint_t *)NULL, pl, PAGESIZE, seg, addr,
71920Sstevel@tonic-gate 				    S_OTHER, svd->cred);
71930Sstevel@tonic-gate 
71940Sstevel@tonic-gate 				/*
71950Sstevel@tonic-gate 				 * If the error is EDEADLK then we must bounce
71960Sstevel@tonic-gate 				 * up and drop all vm subsystem locks and then
71970Sstevel@tonic-gate 				 * retry the operation later
71980Sstevel@tonic-gate 				 * This behavior is a temporary measure because
71990Sstevel@tonic-gate 				 * ufs/sds logging is badly designed and will
72000Sstevel@tonic-gate 				 * deadlock if we don't allow this bounce to
72010Sstevel@tonic-gate 				 * happen.  The real solution is to re-design
72020Sstevel@tonic-gate 				 * the logging code to work properly.  See bug
72030Sstevel@tonic-gate 				 * 4125102 for details of the problem.
72040Sstevel@tonic-gate 				 */
72050Sstevel@tonic-gate 				if (error == EDEADLK) {
72062768Ssl108498 					err = error;
72072768Ssl108498 					goto out;
72080Sstevel@tonic-gate 				}
72090Sstevel@tonic-gate 				/*
72100Sstevel@tonic-gate 				 * Quit if we fail to fault in the page.  Treat
72110Sstevel@tonic-gate 				 * the failure as an error, unless the addr
72120Sstevel@tonic-gate 				 * is mapped beyond the end of a file.
72130Sstevel@tonic-gate 				 */
72140Sstevel@tonic-gate 				if (error && svd->vp) {
72150Sstevel@tonic-gate 					va.va_mask = AT_SIZE;
72160Sstevel@tonic-gate 					if (VOP_GETATTR(svd->vp, &va, 0,
72170Sstevel@tonic-gate 					    svd->cred) != 0) {
72182768Ssl108498 						err = EIO;
72192768Ssl108498 						goto out;
72200Sstevel@tonic-gate 					}
72210Sstevel@tonic-gate 					if (btopr(va.va_size) >=
72220Sstevel@tonic-gate 					    btopr(off + 1)) {
72232768Ssl108498 						err = EIO;
72242768Ssl108498 						goto out;
72250Sstevel@tonic-gate 					}
72262768Ssl108498 					goto out;
72272768Ssl108498 
72280Sstevel@tonic-gate 				} else if (error) {
72292768Ssl108498 					err = EIO;
72302768Ssl108498 					goto out;
72310Sstevel@tonic-gate 				}
72320Sstevel@tonic-gate 				pp = pl[0];
72330Sstevel@tonic-gate 				ASSERT(pp != NULL);
72340Sstevel@tonic-gate 			}
72350Sstevel@tonic-gate 
72360Sstevel@tonic-gate 			/*
72370Sstevel@tonic-gate 			 * See Statement at the beginning of this routine.
72380Sstevel@tonic-gate 			 *
72390Sstevel@tonic-gate 			 * claim is always set if MAP_PRIVATE and PROT_WRITE
72400Sstevel@tonic-gate 			 * irrespective of following factors:
72410Sstevel@tonic-gate 			 *
72420Sstevel@tonic-gate 			 * (1) anon slots are populated or not
72430Sstevel@tonic-gate 			 * (2) cow is broken or not
72440Sstevel@tonic-gate 			 * (3) refcnt on ap is 1 or greater than 1
72450Sstevel@tonic-gate 			 *
72460Sstevel@tonic-gate 			 * See 4140683 for details
72470Sstevel@tonic-gate 			 */
72480Sstevel@tonic-gate 			claim = ((VPP_PROT(vpp) & PROT_WRITE) &&
72490Sstevel@tonic-gate 				(svd->type == MAP_PRIVATE));
72500Sstevel@tonic-gate 
72510Sstevel@tonic-gate 			/*
72520Sstevel@tonic-gate 			 * Perform page-level operation appropriate to
72530Sstevel@tonic-gate 			 * operation.  If locking, undo the SOFTLOCK
72540Sstevel@tonic-gate 			 * performed to bring the page into memory
72550Sstevel@tonic-gate 			 * after setting the lock.  If unlocking,
72560Sstevel@tonic-gate 			 * and no page was found, account for the claim
72570Sstevel@tonic-gate 			 * separately.
72580Sstevel@tonic-gate 			 */
72590Sstevel@tonic-gate 			if (op == MC_LOCK) {
72600Sstevel@tonic-gate 				int ret = 1;	/* Assume success */
72610Sstevel@tonic-gate 
72622768Ssl108498 				ASSERT(!VPP_ISPPLOCK(vpp));
72632768Ssl108498 
72642768Ssl108498 				ret = page_pp_lock(pp, claim, 0);
72652768Ssl108498 				if (ret == 0) {
72662768Ssl108498 					/* locking page failed */
72672768Ssl108498 					page_unlock(pp);
72682768Ssl108498 					err = EAGAIN;
72692768Ssl108498 					goto out;
72700Sstevel@tonic-gate 				}
72712768Ssl108498 				VPP_SETPPLOCK(vpp);
72722768Ssl108498 				if (sp != NULL) {
72732768Ssl108498 					if (pp->p_lckcnt == 1)
72742768Ssl108498 						locked_bytes += PAGESIZE;
72752768Ssl108498 				} else
72762768Ssl108498 					locked_bytes += PAGESIZE;
72772768Ssl108498 
72782768Ssl108498 				if (lockmap != (ulong_t *)NULL)
72792768Ssl108498 					BT_SET(lockmap, pos);
72802768Ssl108498 
72810Sstevel@tonic-gate 				page_unlock(pp);
72820Sstevel@tonic-gate 			} else {
72832768Ssl108498 				ASSERT(VPP_ISPPLOCK(vpp));
72840Sstevel@tonic-gate 				if (pp != NULL) {
72852768Ssl108498 					/* sysV pages should be locked */
72862768Ssl108498 					ASSERT(sp == NULL || pp->p_lckcnt > 0);
72872768Ssl108498 					page_pp_unlock(pp, claim, 0);
72882768Ssl108498 					if (sp != NULL) {
72892768Ssl108498 						if (pp->p_lckcnt == 0)
72902768Ssl108498 							unlocked_bytes
72912768Ssl108498 							    += PAGESIZE;
72922768Ssl108498 					} else
72932768Ssl108498 						unlocked_bytes += PAGESIZE;
72940Sstevel@tonic-gate 					page_unlock(pp);
72952768Ssl108498 				} else {
72962779Ssl108498 					ASSERT(sp == NULL);
72972768Ssl108498 					unlocked_bytes += PAGESIZE;
72980Sstevel@tonic-gate 				}
72990Sstevel@tonic-gate 				VPP_CLRPPLOCK(vpp);
73000Sstevel@tonic-gate 			}
73010Sstevel@tonic-gate 		}
73020Sstevel@tonic-gate 	}
73032768Ssl108498 out:
73042768Ssl108498 	if (op == MC_LOCK) {
73052768Ssl108498 		/* Credit back bytes that did not get locked */
73062768Ssl108498 		if ((unlocked_bytes - locked_bytes) > 0) {
73072768Ssl108498 			if (proj == NULL)
73082768Ssl108498 				mutex_enter(&p->p_lock);
73092768Ssl108498 			rctl_decr_locked_mem(p, proj,
73102768Ssl108498 			    (unlocked_bytes - locked_bytes), chargeproc);
73112768Ssl108498 			if (proj == NULL)
73122768Ssl108498 				mutex_exit(&p->p_lock);
73132768Ssl108498 		}
73142768Ssl108498 
73152768Ssl108498 	} else {
73162768Ssl108498 		/* Account bytes that were unlocked */
73172768Ssl108498 		if (unlocked_bytes > 0) {
73182768Ssl108498 			if (proj == NULL)
73192768Ssl108498 				mutex_enter(&p->p_lock);
73202768Ssl108498 			rctl_decr_locked_mem(p, proj, unlocked_bytes,
73212768Ssl108498 			    chargeproc);
73222768Ssl108498 			if (proj == NULL)
73232768Ssl108498 				mutex_exit(&p->p_lock);
73242768Ssl108498 		}
73252768Ssl108498 	}
73262768Ssl108498 	if (sp != NULL)
73272768Ssl108498 		mutex_exit(&sp->shm_mlock);
73280Sstevel@tonic-gate 	SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
73292768Ssl108498 
73302768Ssl108498 	return (err);
73310Sstevel@tonic-gate }
73320Sstevel@tonic-gate 
73330Sstevel@tonic-gate /*
73340Sstevel@tonic-gate  * Set advice from user for specified pages
73350Sstevel@tonic-gate  * There are 5 types of advice:
73360Sstevel@tonic-gate  *	MADV_NORMAL	- Normal (default) behavior (whatever that is)
73370Sstevel@tonic-gate  *	MADV_RANDOM	- Random page references
73380Sstevel@tonic-gate  *				do not allow readahead or 'klustering'
73390Sstevel@tonic-gate  *	MADV_SEQUENTIAL	- Sequential page references
73400Sstevel@tonic-gate  *				Pages previous to the one currently being
73410Sstevel@tonic-gate  *				accessed (determined by fault) are 'not needed'
73420Sstevel@tonic-gate  *				and are freed immediately
73430Sstevel@tonic-gate  *	MADV_WILLNEED	- Pages are likely to be used (fault ahead in mctl)
73440Sstevel@tonic-gate  *	MADV_DONTNEED	- Pages are not needed (synced out in mctl)
73450Sstevel@tonic-gate  *	MADV_FREE	- Contents can be discarded
73460Sstevel@tonic-gate  *	MADV_ACCESS_DEFAULT- Default access
73470Sstevel@tonic-gate  *	MADV_ACCESS_LWP	- Next LWP will access heavily
73480Sstevel@tonic-gate  *	MADV_ACCESS_MANY- Many LWPs or processes will access heavily
73490Sstevel@tonic-gate  */
73500Sstevel@tonic-gate static int
73510Sstevel@tonic-gate segvn_advise(struct seg *seg, caddr_t addr, size_t len, uint_t behav)
73520Sstevel@tonic-gate {
73530Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
73540Sstevel@tonic-gate 	size_t page;
73550Sstevel@tonic-gate 	int err = 0;
73560Sstevel@tonic-gate 	int already_set;
73570Sstevel@tonic-gate 	struct anon_map *amp;
73580Sstevel@tonic-gate 	ulong_t anon_index;
73590Sstevel@tonic-gate 	struct seg *next;
73600Sstevel@tonic-gate 	lgrp_mem_policy_t policy;
73610Sstevel@tonic-gate 	struct seg *prev;
73620Sstevel@tonic-gate 	struct vnode *vp;
73630Sstevel@tonic-gate 
73640Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
73650Sstevel@tonic-gate 
73660Sstevel@tonic-gate 	/*
73670Sstevel@tonic-gate 	 * In case of MADV_FREE, we won't be modifying any segment private
73680Sstevel@tonic-gate 	 * data structures; so, we only need to grab READER's lock
73690Sstevel@tonic-gate 	 */
73700Sstevel@tonic-gate 	if (behav != MADV_FREE)
73710Sstevel@tonic-gate 		SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_WRITER);
73720Sstevel@tonic-gate 	else
73730Sstevel@tonic-gate 		SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
73740Sstevel@tonic-gate 
73750Sstevel@tonic-gate 	/*
73760Sstevel@tonic-gate 	 * Large pages are assumed to be only turned on when accesses to the
73770Sstevel@tonic-gate 	 * segment's address range have spatial and temporal locality. That
73780Sstevel@tonic-gate 	 * justifies ignoring MADV_SEQUENTIAL for large page segments.
73790Sstevel@tonic-gate 	 * Also, ignore advice affecting lgroup memory allocation
73800Sstevel@tonic-gate 	 * if don't need to do lgroup optimizations on this system
73810Sstevel@tonic-gate 	 */
73820Sstevel@tonic-gate 
73830Sstevel@tonic-gate 	if ((behav == MADV_SEQUENTIAL && seg->s_szc != 0) ||
73840Sstevel@tonic-gate 	    (!lgrp_optimizations() && (behav == MADV_ACCESS_DEFAULT ||
73850Sstevel@tonic-gate 	    behav == MADV_ACCESS_LWP || behav == MADV_ACCESS_MANY))) {
73860Sstevel@tonic-gate 		SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
73870Sstevel@tonic-gate 		return (0);
73880Sstevel@tonic-gate 	}
73890Sstevel@tonic-gate 
73900Sstevel@tonic-gate 	if (behav == MADV_SEQUENTIAL || behav == MADV_ACCESS_DEFAULT ||
73910Sstevel@tonic-gate 	    behav == MADV_ACCESS_LWP || behav == MADV_ACCESS_MANY) {
73920Sstevel@tonic-gate 		/*
73930Sstevel@tonic-gate 		 * Since we are going to unload hat mappings
73940Sstevel@tonic-gate 		 * we first have to flush the cache. Otherwise
73950Sstevel@tonic-gate 		 * this might lead to system panic if another
73960Sstevel@tonic-gate 		 * thread is doing physio on the range whose
73970Sstevel@tonic-gate 		 * mappings are unloaded by madvise(3C).
73980Sstevel@tonic-gate 		 */
73990Sstevel@tonic-gate 		if (svd->softlockcnt > 0) {
74000Sstevel@tonic-gate 			/*
74010Sstevel@tonic-gate 			 * Since we do have the segvn writers lock
74020Sstevel@tonic-gate 			 * nobody can fill the cache with entries
74030Sstevel@tonic-gate 			 * belonging to this seg during the purge.
74040Sstevel@tonic-gate 			 * The flush either succeeds or we still
74050Sstevel@tonic-gate 			 * have pending I/Os. In the later case,
74060Sstevel@tonic-gate 			 * madvise(3C) fails.
74070Sstevel@tonic-gate 			 */
74080Sstevel@tonic-gate 			segvn_purge(seg);
74090Sstevel@tonic-gate 			if (svd->softlockcnt > 0) {
74100Sstevel@tonic-gate 				/*
74110Sstevel@tonic-gate 				 * Since madvise(3C) is advisory and
74120Sstevel@tonic-gate 				 * it's not part of UNIX98, madvise(3C)
74130Sstevel@tonic-gate 				 * failure here doesn't cause any hardship.
74140Sstevel@tonic-gate 				 * Note that we don't block in "as" layer.
74150Sstevel@tonic-gate 				 */
74160Sstevel@tonic-gate 				SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
74170Sstevel@tonic-gate 				return (EAGAIN);
74180Sstevel@tonic-gate 			}
74190Sstevel@tonic-gate 		}
74200Sstevel@tonic-gate 	}
74210Sstevel@tonic-gate 
74220Sstevel@tonic-gate 	amp = svd->amp;
74230Sstevel@tonic-gate 	vp = svd->vp;
74240Sstevel@tonic-gate 	if (behav == MADV_FREE) {
74250Sstevel@tonic-gate 		/*
74260Sstevel@tonic-gate 		 * MADV_FREE is not supported for segments with
74270Sstevel@tonic-gate 		 * underlying object; if anonmap is NULL, anon slots
74280Sstevel@tonic-gate 		 * are not yet populated and there is nothing for
74290Sstevel@tonic-gate 		 * us to do. As MADV_FREE is advisory, we don't
74300Sstevel@tonic-gate 		 * return error in either case.
74310Sstevel@tonic-gate 		 */
74320Sstevel@tonic-gate 		if (vp || amp == NULL) {
74330Sstevel@tonic-gate 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
74340Sstevel@tonic-gate 			return (0);
74350Sstevel@tonic-gate 		}
74360Sstevel@tonic-gate 
74370Sstevel@tonic-gate 		page = seg_page(seg, addr);
74380Sstevel@tonic-gate 		ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
74390Sstevel@tonic-gate 		anon_disclaim(amp, svd->anon_index + page, len, 0);
74400Sstevel@tonic-gate 		ANON_LOCK_EXIT(&amp->a_rwlock);
74410Sstevel@tonic-gate 		SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
74420Sstevel@tonic-gate 		return (0);
74430Sstevel@tonic-gate 	}
74440Sstevel@tonic-gate 
74450Sstevel@tonic-gate 	/*
74460Sstevel@tonic-gate 	 * If advice is to be applied to entire segment,
74470Sstevel@tonic-gate 	 * use advice field in seg_data structure
74480Sstevel@tonic-gate 	 * otherwise use appropriate vpage entry.
74490Sstevel@tonic-gate 	 */
74500Sstevel@tonic-gate 	if ((addr == seg->s_base) && (len == seg->s_size)) {
74510Sstevel@tonic-gate 		switch (behav) {
74520Sstevel@tonic-gate 		case MADV_ACCESS_LWP:
74530Sstevel@tonic-gate 		case MADV_ACCESS_MANY:
74540Sstevel@tonic-gate 		case MADV_ACCESS_DEFAULT:
74550Sstevel@tonic-gate 			/*
74560Sstevel@tonic-gate 			 * Set memory allocation policy for this segment
74570Sstevel@tonic-gate 			 */
74580Sstevel@tonic-gate 			policy = lgrp_madv_to_policy(behav, len, svd->type);
74590Sstevel@tonic-gate 			if (svd->type == MAP_SHARED)
74600Sstevel@tonic-gate 				already_set = lgrp_shm_policy_set(policy, amp,
74610Sstevel@tonic-gate 				    svd->anon_index, vp, svd->offset, len);
74620Sstevel@tonic-gate 			else {
74630Sstevel@tonic-gate 				/*
74640Sstevel@tonic-gate 				 * For private memory, need writers lock on
74650Sstevel@tonic-gate 				 * address space because the segment may be
74660Sstevel@tonic-gate 				 * split or concatenated when changing policy
74670Sstevel@tonic-gate 				 */
74680Sstevel@tonic-gate 				if (AS_READ_HELD(seg->s_as,
74690Sstevel@tonic-gate 				    &seg->s_as->a_lock)) {
74700Sstevel@tonic-gate 					SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
74710Sstevel@tonic-gate 					return (IE_RETRY);
74720Sstevel@tonic-gate 				}
74730Sstevel@tonic-gate 
74740Sstevel@tonic-gate 				already_set = lgrp_privm_policy_set(policy,
74750Sstevel@tonic-gate 				    &svd->policy_info, len);
74760Sstevel@tonic-gate 			}
74770Sstevel@tonic-gate 
74780Sstevel@tonic-gate 			/*
74790Sstevel@tonic-gate 			 * If policy set already and it shouldn't be reapplied,
74800Sstevel@tonic-gate 			 * don't do anything.
74810Sstevel@tonic-gate 			 */
74820Sstevel@tonic-gate 			if (already_set &&
74830Sstevel@tonic-gate 			    !LGRP_MEM_POLICY_REAPPLICABLE(policy))
74840Sstevel@tonic-gate 				break;
74850Sstevel@tonic-gate 
74860Sstevel@tonic-gate 			/*
74870Sstevel@tonic-gate 			 * Mark any existing pages in given range for
74880Sstevel@tonic-gate 			 * migration
74890Sstevel@tonic-gate 			 */
74900Sstevel@tonic-gate 			page_mark_migrate(seg, addr, len, amp, svd->anon_index,
74910Sstevel@tonic-gate 			    vp, svd->offset, 1);
74920Sstevel@tonic-gate 
74930Sstevel@tonic-gate 			/*
74940Sstevel@tonic-gate 			 * If same policy set already or this is a shared
74950Sstevel@tonic-gate 			 * memory segment, don't need to try to concatenate
74960Sstevel@tonic-gate 			 * segment with adjacent ones.
74970Sstevel@tonic-gate 			 */
74980Sstevel@tonic-gate 			if (already_set || svd->type == MAP_SHARED)
74990Sstevel@tonic-gate 				break;
75000Sstevel@tonic-gate 
75010Sstevel@tonic-gate 			/*
75020Sstevel@tonic-gate 			 * Try to concatenate this segment with previous
75030Sstevel@tonic-gate 			 * one and next one, since we changed policy for
75040Sstevel@tonic-gate 			 * this one and it may be compatible with adjacent
75050Sstevel@tonic-gate 			 * ones now.
75060Sstevel@tonic-gate 			 */
75070Sstevel@tonic-gate 			prev = AS_SEGPREV(seg->s_as, seg);
75080Sstevel@tonic-gate 			next = AS_SEGNEXT(seg->s_as, seg);
75090Sstevel@tonic-gate 
75100Sstevel@tonic-gate 			if (next && next->s_ops == &segvn_ops &&
75110Sstevel@tonic-gate 			    addr + len == next->s_base)
75120Sstevel@tonic-gate 				(void) segvn_concat(seg, next, 1);
75130Sstevel@tonic-gate 
75140Sstevel@tonic-gate 			if (prev && prev->s_ops == &segvn_ops &&
75150Sstevel@tonic-gate 			    addr == prev->s_base + prev->s_size) {
75160Sstevel@tonic-gate 				/*
75170Sstevel@tonic-gate 				 * Drop lock for private data of current
75180Sstevel@tonic-gate 				 * segment before concatenating (deleting) it
75190Sstevel@tonic-gate 				 * and return IE_REATTACH to tell as_ctl() that
75200Sstevel@tonic-gate 				 * current segment has changed
75210Sstevel@tonic-gate 				 */
75220Sstevel@tonic-gate 				SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
75230Sstevel@tonic-gate 				if (!segvn_concat(prev, seg, 1))
75240Sstevel@tonic-gate 					err = IE_REATTACH;
75250Sstevel@tonic-gate 
75260Sstevel@tonic-gate 				return (err);
75270Sstevel@tonic-gate 			}
75280Sstevel@tonic-gate 			break;
75290Sstevel@tonic-gate 
75300Sstevel@tonic-gate 		case MADV_SEQUENTIAL:
75310Sstevel@tonic-gate 			/*
75320Sstevel@tonic-gate 			 * unloading mapping guarantees
75330Sstevel@tonic-gate 			 * detection in segvn_fault
75340Sstevel@tonic-gate 			 */
75350Sstevel@tonic-gate 			ASSERT(seg->s_szc == 0);
75360Sstevel@tonic-gate 			hat_unload(seg->s_as->a_hat, addr, len,
75370Sstevel@tonic-gate 				HAT_UNLOAD);
75380Sstevel@tonic-gate 			/* FALLTHROUGH */
75390Sstevel@tonic-gate 		case MADV_NORMAL:
75400Sstevel@tonic-gate 		case MADV_RANDOM:
75410Sstevel@tonic-gate 			svd->advice = (uchar_t)behav;
75420Sstevel@tonic-gate 			svd->pageadvice = 0;
75430Sstevel@tonic-gate 			break;
75440Sstevel@tonic-gate 		case MADV_WILLNEED:	/* handled in memcntl */
75450Sstevel@tonic-gate 		case MADV_DONTNEED:	/* handled in memcntl */
75460Sstevel@tonic-gate 		case MADV_FREE:		/* handled above */
75470Sstevel@tonic-gate 			break;
75480Sstevel@tonic-gate 		default:
75490Sstevel@tonic-gate 			err = EINVAL;
75500Sstevel@tonic-gate 		}
75510Sstevel@tonic-gate 	} else {
75520Sstevel@tonic-gate 		caddr_t			eaddr;
75530Sstevel@tonic-gate 		struct seg		*new_seg;
75540Sstevel@tonic-gate 		struct segvn_data	*new_svd;
75550Sstevel@tonic-gate 		u_offset_t		off;
75560Sstevel@tonic-gate 		caddr_t			oldeaddr;
75570Sstevel@tonic-gate 
75580Sstevel@tonic-gate 		page = seg_page(seg, addr);
75590Sstevel@tonic-gate 
75600Sstevel@tonic-gate 		segvn_vpage(seg);
75610Sstevel@tonic-gate 
75620Sstevel@tonic-gate 		switch (behav) {
75630Sstevel@tonic-gate 			struct vpage *bvpp, *evpp;
75640Sstevel@tonic-gate 
75650Sstevel@tonic-gate 		case MADV_ACCESS_LWP:
75660Sstevel@tonic-gate 		case MADV_ACCESS_MANY:
75670Sstevel@tonic-gate 		case MADV_ACCESS_DEFAULT:
75680Sstevel@tonic-gate 			/*
75690Sstevel@tonic-gate 			 * Set memory allocation policy for portion of this
75700Sstevel@tonic-gate 			 * segment
75710Sstevel@tonic-gate 			 */
75720Sstevel@tonic-gate 
75730Sstevel@tonic-gate 			/*
75740Sstevel@tonic-gate 			 * Align address and length of advice to page
75750Sstevel@tonic-gate 			 * boundaries for large pages
75760Sstevel@tonic-gate 			 */
75770Sstevel@tonic-gate 			if (seg->s_szc != 0) {
75780Sstevel@tonic-gate 				size_t	pgsz;
75790Sstevel@tonic-gate 
75800Sstevel@tonic-gate 				pgsz = page_get_pagesize(seg->s_szc);
75810Sstevel@tonic-gate 				addr = (caddr_t)P2ALIGN((uintptr_t)addr, pgsz);
75820Sstevel@tonic-gate 				len = P2ROUNDUP(len, pgsz);
75830Sstevel@tonic-gate 			}
75840Sstevel@tonic-gate 
75850Sstevel@tonic-gate 			/*
75860Sstevel@tonic-gate 			 * Check to see whether policy is set already
75870Sstevel@tonic-gate 			 */
75880Sstevel@tonic-gate 			policy = lgrp_madv_to_policy(behav, len, svd->type);
75890Sstevel@tonic-gate 
75900Sstevel@tonic-gate 			anon_index = svd->anon_index + page;
75910Sstevel@tonic-gate 			off = svd->offset + (uintptr_t)(addr - seg->s_base);
75920Sstevel@tonic-gate 
75930Sstevel@tonic-gate 			if (svd->type == MAP_SHARED)
75940Sstevel@tonic-gate 				already_set = lgrp_shm_policy_set(policy, amp,
75950Sstevel@tonic-gate 				    anon_index, vp, off, len);
75960Sstevel@tonic-gate 			else
75970Sstevel@tonic-gate 				already_set =
75980Sstevel@tonic-gate 				    (policy == svd->policy_info.mem_policy);
75990Sstevel@tonic-gate 
76000Sstevel@tonic-gate 			/*
76010Sstevel@tonic-gate 			 * If policy set already and it shouldn't be reapplied,
76020Sstevel@tonic-gate 			 * don't do anything.
76030Sstevel@tonic-gate 			 */
76040Sstevel@tonic-gate 			if (already_set &&
76050Sstevel@tonic-gate 			    !LGRP_MEM_POLICY_REAPPLICABLE(policy))
76060Sstevel@tonic-gate 				break;
76070Sstevel@tonic-gate 
76080Sstevel@tonic-gate 			/*
76090Sstevel@tonic-gate 			 * For private memory, need writers lock on
76100Sstevel@tonic-gate 			 * address space because the segment may be
76110Sstevel@tonic-gate 			 * split or concatenated when changing policy
76120Sstevel@tonic-gate 			 */
76130Sstevel@tonic-gate 			if (svd->type == MAP_PRIVATE &&
76140Sstevel@tonic-gate 			    AS_READ_HELD(seg->s_as, &seg->s_as->a_lock)) {
76150Sstevel@tonic-gate 				SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
76160Sstevel@tonic-gate 				return (IE_RETRY);
76170Sstevel@tonic-gate 			}
76180Sstevel@tonic-gate 
76190Sstevel@tonic-gate 			/*
76200Sstevel@tonic-gate 			 * Mark any existing pages in given range for
76210Sstevel@tonic-gate 			 * migration
76220Sstevel@tonic-gate 			 */
76230Sstevel@tonic-gate 			page_mark_migrate(seg, addr, len, amp, svd->anon_index,
76240Sstevel@tonic-gate 			    vp, svd->offset, 1);
76250Sstevel@tonic-gate 
76260Sstevel@tonic-gate 			/*
76270Sstevel@tonic-gate 			 * Don't need to try to split or concatenate
76280Sstevel@tonic-gate 			 * segments, since policy is same or this is a shared
76290Sstevel@tonic-gate 			 * memory segment
76300Sstevel@tonic-gate 			 */
76310Sstevel@tonic-gate 			if (already_set || svd->type == MAP_SHARED)
76320Sstevel@tonic-gate 				break;
76330Sstevel@tonic-gate 
76340Sstevel@tonic-gate 			/*
76350Sstevel@tonic-gate 			 * Split off new segment if advice only applies to a
76360Sstevel@tonic-gate 			 * portion of existing segment starting in middle
76370Sstevel@tonic-gate 			 */
76380Sstevel@tonic-gate 			new_seg = NULL;
76390Sstevel@tonic-gate 			eaddr = addr + len;
76400Sstevel@tonic-gate 			oldeaddr = seg->s_base + seg->s_size;
76410Sstevel@tonic-gate 			if (addr > seg->s_base) {
76420Sstevel@tonic-gate 				/*
76430Sstevel@tonic-gate 				 * Must flush I/O page cache
76440Sstevel@tonic-gate 				 * before splitting segment
76450Sstevel@tonic-gate 				 */
76460Sstevel@tonic-gate 				if (svd->softlockcnt > 0)
76470Sstevel@tonic-gate 					segvn_purge(seg);
76480Sstevel@tonic-gate 
76490Sstevel@tonic-gate 				/*
76500Sstevel@tonic-gate 				 * Split segment and return IE_REATTACH to tell
76510Sstevel@tonic-gate 				 * as_ctl() that current segment changed
76520Sstevel@tonic-gate 				 */
76530Sstevel@tonic-gate 				new_seg = segvn_split_seg(seg, addr);
76540Sstevel@tonic-gate 				new_svd = (struct segvn_data *)new_seg->s_data;
76550Sstevel@tonic-gate 				err = IE_REATTACH;
76560Sstevel@tonic-gate 
76570Sstevel@tonic-gate 				/*
76580Sstevel@tonic-gate 				 * If new segment ends where old one
76590Sstevel@tonic-gate 				 * did, try to concatenate the new
76600Sstevel@tonic-gate 				 * segment with next one.
76610Sstevel@tonic-gate 				 */
76620Sstevel@tonic-gate 				if (eaddr == oldeaddr) {
76630Sstevel@tonic-gate 					/*
76640Sstevel@tonic-gate 					 * Set policy for new segment
76650Sstevel@tonic-gate 					 */
76660Sstevel@tonic-gate 					(void) lgrp_privm_policy_set(policy,
76670Sstevel@tonic-gate 					    &new_svd->policy_info,
76680Sstevel@tonic-gate 					    new_seg->s_size);
76690Sstevel@tonic-gate 
76700Sstevel@tonic-gate 					next = AS_SEGNEXT(new_seg->s_as,
76710Sstevel@tonic-gate 					    new_seg);
76720Sstevel@tonic-gate 
76730Sstevel@tonic-gate 					if (next &&
76740Sstevel@tonic-gate 					    next->s_ops == &segvn_ops &&
76750Sstevel@tonic-gate 					    eaddr == next->s_base)
76760Sstevel@tonic-gate 						(void) segvn_concat(new_seg,
76770Sstevel@tonic-gate 						    next, 1);
76780Sstevel@tonic-gate 				}
76790Sstevel@tonic-gate 			}
76800Sstevel@tonic-gate 
76810Sstevel@tonic-gate 			/*
76820Sstevel@tonic-gate 			 * Split off end of existing segment if advice only
76830Sstevel@tonic-gate 			 * applies to a portion of segment ending before
76840Sstevel@tonic-gate 			 * end of the existing segment
76850Sstevel@tonic-gate 			 */
76860Sstevel@tonic-gate 			if (eaddr < oldeaddr) {
76870Sstevel@tonic-gate 				/*
76880Sstevel@tonic-gate 				 * Must flush I/O page cache
76890Sstevel@tonic-gate 				 * before splitting segment
76900Sstevel@tonic-gate 				 */
76910Sstevel@tonic-gate 				if (svd->softlockcnt > 0)
76920Sstevel@tonic-gate 					segvn_purge(seg);
76930Sstevel@tonic-gate 
76940Sstevel@tonic-gate 				/*
76950Sstevel@tonic-gate 				 * If beginning of old segment was already
76960Sstevel@tonic-gate 				 * split off, use new segment to split end off
76970Sstevel@tonic-gate 				 * from.
76980Sstevel@tonic-gate 				 */
76990Sstevel@tonic-gate 				if (new_seg != NULL && new_seg != seg) {
77000Sstevel@tonic-gate 					/*
77010Sstevel@tonic-gate 					 * Split segment
77020Sstevel@tonic-gate 					 */
77030Sstevel@tonic-gate 					(void) segvn_split_seg(new_seg, eaddr);
77040Sstevel@tonic-gate 
77050Sstevel@tonic-gate 					/*
77060Sstevel@tonic-gate 					 * Set policy for new segment
77070Sstevel@tonic-gate 					 */
77080Sstevel@tonic-gate 					(void) lgrp_privm_policy_set(policy,
77090Sstevel@tonic-gate 					    &new_svd->policy_info,
77100Sstevel@tonic-gate 					    new_seg->s_size);
77110Sstevel@tonic-gate 				} else {
77120Sstevel@tonic-gate 					/*
77130Sstevel@tonic-gate 					 * Split segment and return IE_REATTACH
77140Sstevel@tonic-gate 					 * to tell as_ctl() that current
77150Sstevel@tonic-gate 					 * segment changed
77160Sstevel@tonic-gate 					 */
77170Sstevel@tonic-gate 					(void) segvn_split_seg(seg, eaddr);
77180Sstevel@tonic-gate 					err = IE_REATTACH;
77190Sstevel@tonic-gate 
77200Sstevel@tonic-gate 					(void) lgrp_privm_policy_set(policy,
77210Sstevel@tonic-gate 					    &svd->policy_info, seg->s_size);
77220Sstevel@tonic-gate 
77230Sstevel@tonic-gate 					/*
77240Sstevel@tonic-gate 					 * If new segment starts where old one
77250Sstevel@tonic-gate 					 * did, try to concatenate it with
77260Sstevel@tonic-gate 					 * previous segment.
77270Sstevel@tonic-gate 					 */
77280Sstevel@tonic-gate 					if (addr == seg->s_base) {
77290Sstevel@tonic-gate 						prev = AS_SEGPREV(seg->s_as,
77300Sstevel@tonic-gate 						    seg);
77310Sstevel@tonic-gate 
77320Sstevel@tonic-gate 						/*
77330Sstevel@tonic-gate 						 * Drop lock for private data
77340Sstevel@tonic-gate 						 * of current segment before
77350Sstevel@tonic-gate 						 * concatenating (deleting) it
77360Sstevel@tonic-gate 						 */
77370Sstevel@tonic-gate 						if (prev &&
77380Sstevel@tonic-gate 						    prev->s_ops ==
77390Sstevel@tonic-gate 						    &segvn_ops &&
77400Sstevel@tonic-gate 						    addr == prev->s_base +
77410Sstevel@tonic-gate 						    prev->s_size) {
77420Sstevel@tonic-gate 							SEGVN_LOCK_EXIT(
77430Sstevel@tonic-gate 							    seg->s_as,
77440Sstevel@tonic-gate 							    &svd->lock);
77450Sstevel@tonic-gate 							(void) segvn_concat(
77460Sstevel@tonic-gate 							    prev, seg, 1);
77470Sstevel@tonic-gate 							return (err);
77480Sstevel@tonic-gate 						}
77490Sstevel@tonic-gate 					}
77500Sstevel@tonic-gate 				}
77510Sstevel@tonic-gate 			}
77520Sstevel@tonic-gate 			break;
77530Sstevel@tonic-gate 		case MADV_SEQUENTIAL:
77540Sstevel@tonic-gate 			ASSERT(seg->s_szc == 0);
77550Sstevel@tonic-gate 			hat_unload(seg->s_as->a_hat, addr, len, HAT_UNLOAD);
77560Sstevel@tonic-gate 			/* FALLTHROUGH */
77570Sstevel@tonic-gate 		case MADV_NORMAL:
77580Sstevel@tonic-gate 		case MADV_RANDOM:
77590Sstevel@tonic-gate 			bvpp = &svd->vpage[page];
77600Sstevel@tonic-gate 			evpp = &svd->vpage[page + (len >> PAGESHIFT)];
77610Sstevel@tonic-gate 			for (; bvpp < evpp; bvpp++)
77620Sstevel@tonic-gate 				VPP_SETADVICE(bvpp, behav);
77630Sstevel@tonic-gate 			svd->advice = MADV_NORMAL;
77640Sstevel@tonic-gate 			break;
77650Sstevel@tonic-gate 		case MADV_WILLNEED:	/* handled in memcntl */
77660Sstevel@tonic-gate 		case MADV_DONTNEED:	/* handled in memcntl */
77670Sstevel@tonic-gate 		case MADV_FREE:		/* handled above */
77680Sstevel@tonic-gate 			break;
77690Sstevel@tonic-gate 		default:
77700Sstevel@tonic-gate 			err = EINVAL;
77710Sstevel@tonic-gate 		}
77720Sstevel@tonic-gate 	}
77730Sstevel@tonic-gate 	SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
77740Sstevel@tonic-gate 	return (err);
77750Sstevel@tonic-gate }
77760Sstevel@tonic-gate 
77770Sstevel@tonic-gate /*
77780Sstevel@tonic-gate  * Create a vpage structure for this seg.
77790Sstevel@tonic-gate  */
77800Sstevel@tonic-gate static void
77810Sstevel@tonic-gate segvn_vpage(struct seg *seg)
77820Sstevel@tonic-gate {
77830Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
77840Sstevel@tonic-gate 	struct vpage *vp, *evp;
77850Sstevel@tonic-gate 
77860Sstevel@tonic-gate 	ASSERT(SEGVN_WRITE_HELD(seg->s_as, &svd->lock));
77870Sstevel@tonic-gate 
77880Sstevel@tonic-gate 	/*
77890Sstevel@tonic-gate 	 * If no vpage structure exists, allocate one.  Copy the protections
77900Sstevel@tonic-gate 	 * and the advice from the segment itself to the individual pages.
77910Sstevel@tonic-gate 	 */
77920Sstevel@tonic-gate 	if (svd->vpage == NULL) {
77930Sstevel@tonic-gate 		svd->pageprot = 1;
77940Sstevel@tonic-gate 		svd->pageadvice = 1;
77950Sstevel@tonic-gate 		svd->vpage = kmem_zalloc(seg_pages(seg) * sizeof (struct vpage),
77960Sstevel@tonic-gate 		    KM_SLEEP);
77970Sstevel@tonic-gate 		evp = &svd->vpage[seg_page(seg, seg->s_base + seg->s_size)];
77980Sstevel@tonic-gate 		for (vp = svd->vpage; vp < evp; vp++) {
77990Sstevel@tonic-gate 			VPP_SETPROT(vp, svd->prot);
78000Sstevel@tonic-gate 			VPP_SETADVICE(vp, svd->advice);
78010Sstevel@tonic-gate 		}
78020Sstevel@tonic-gate 	}
78030Sstevel@tonic-gate }
78040Sstevel@tonic-gate 
78050Sstevel@tonic-gate /*
78060Sstevel@tonic-gate  * Dump the pages belonging to this segvn segment.
78070Sstevel@tonic-gate  */
78080Sstevel@tonic-gate static void
78090Sstevel@tonic-gate segvn_dump(struct seg *seg)
78100Sstevel@tonic-gate {
78110Sstevel@tonic-gate 	struct segvn_data *svd;
78120Sstevel@tonic-gate 	page_t *pp;
78130Sstevel@tonic-gate 	struct anon_map *amp;
78140Sstevel@tonic-gate 	ulong_t	anon_index;
78150Sstevel@tonic-gate 	struct vnode *vp;
78160Sstevel@tonic-gate 	u_offset_t off, offset;
78170Sstevel@tonic-gate 	pfn_t pfn;
78180Sstevel@tonic-gate 	pgcnt_t page, npages;
78190Sstevel@tonic-gate 	caddr_t addr;
78200Sstevel@tonic-gate 
78210Sstevel@tonic-gate 	npages = seg_pages(seg);
78220Sstevel@tonic-gate 	svd = (struct segvn_data *)seg->s_data;
78230Sstevel@tonic-gate 	vp = svd->vp;
78240Sstevel@tonic-gate 	off = offset = svd->offset;
78250Sstevel@tonic-gate 	addr = seg->s_base;
78260Sstevel@tonic-gate 
78270Sstevel@tonic-gate 	if ((amp = svd->amp) != NULL) {
78280Sstevel@tonic-gate 		anon_index = svd->anon_index;
78290Sstevel@tonic-gate 		ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
78300Sstevel@tonic-gate 	}
78310Sstevel@tonic-gate 
78320Sstevel@tonic-gate 	for (page = 0; page < npages; page++, offset += PAGESIZE) {
78330Sstevel@tonic-gate 		struct anon *ap;
78340Sstevel@tonic-gate 		int we_own_it = 0;
78350Sstevel@tonic-gate 
78360Sstevel@tonic-gate 		if (amp && (ap = anon_get_ptr(svd->amp->ahp, anon_index++))) {
78370Sstevel@tonic-gate 			swap_xlate_nopanic(ap, &vp, &off);
78380Sstevel@tonic-gate 		} else {
78390Sstevel@tonic-gate 			vp = svd->vp;
78400Sstevel@tonic-gate 			off = offset;
78410Sstevel@tonic-gate 		}
78420Sstevel@tonic-gate 
78430Sstevel@tonic-gate 		/*
78440Sstevel@tonic-gate 		 * If pp == NULL, the page either does not exist
78450Sstevel@tonic-gate 		 * or is exclusively locked.  So determine if it
78460Sstevel@tonic-gate 		 * exists before searching for it.
78470Sstevel@tonic-gate 		 */
78480Sstevel@tonic-gate 
78490Sstevel@tonic-gate 		if ((pp = page_lookup_nowait(vp, off, SE_SHARED)))
78500Sstevel@tonic-gate 			we_own_it = 1;
78510Sstevel@tonic-gate 		else
78520Sstevel@tonic-gate 			pp = page_exists(vp, off);
78530Sstevel@tonic-gate 
78540Sstevel@tonic-gate 		if (pp) {
78550Sstevel@tonic-gate 			pfn = page_pptonum(pp);
78560Sstevel@tonic-gate 			dump_addpage(seg->s_as, addr, pfn);
78570Sstevel@tonic-gate 			if (we_own_it)
78580Sstevel@tonic-gate 				page_unlock(pp);
78590Sstevel@tonic-gate 		}
78600Sstevel@tonic-gate 		addr += PAGESIZE;
78610Sstevel@tonic-gate 		dump_timeleft = dump_timeout;
78620Sstevel@tonic-gate 	}
78630Sstevel@tonic-gate 
78640Sstevel@tonic-gate 	if (amp != NULL)
78650Sstevel@tonic-gate 		ANON_LOCK_EXIT(&amp->a_rwlock);
78660Sstevel@tonic-gate }
78670Sstevel@tonic-gate 
78680Sstevel@tonic-gate /*
78690Sstevel@tonic-gate  * lock/unlock anon pages over a given range. Return shadow list
78700Sstevel@tonic-gate  */
78710Sstevel@tonic-gate static int
78720Sstevel@tonic-gate segvn_pagelock(struct seg *seg, caddr_t addr, size_t len, struct page ***ppp,
78730Sstevel@tonic-gate     enum lock_type type, enum seg_rw rw)
78740Sstevel@tonic-gate {
78750Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
78760Sstevel@tonic-gate 	size_t np, adjustpages = 0, npages = (len >> PAGESHIFT);
78770Sstevel@tonic-gate 	ulong_t anon_index;
78780Sstevel@tonic-gate 	uint_t protchk;
78790Sstevel@tonic-gate 	uint_t error;
78800Sstevel@tonic-gate 	struct anon_map *amp;
78810Sstevel@tonic-gate 	struct page **pplist, **pl, *pp;
78820Sstevel@tonic-gate 	caddr_t a;
78830Sstevel@tonic-gate 	size_t page;
78840Sstevel@tonic-gate 	caddr_t lpgaddr, lpgeaddr;
78852414Saguzovsk 	pgcnt_t szc0_npages = 0;
78860Sstevel@tonic-gate 
78870Sstevel@tonic-gate 	TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_SEGVN_START,
78880Sstevel@tonic-gate 		"segvn_pagelock: start seg %p addr %p", seg, addr);
78890Sstevel@tonic-gate 
78900Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
78910Sstevel@tonic-gate 	if (seg->s_szc != 0 && (type == L_PAGELOCK || type == L_PAGEUNLOCK)) {
78920Sstevel@tonic-gate 		/*
78930Sstevel@tonic-gate 		 * We are adjusting the pagelock region to the large page size
78940Sstevel@tonic-gate 		 * boundary because the unlocked part of a large page cannot
78950Sstevel@tonic-gate 		 * be freed anyway unless all constituent pages of a large
78960Sstevel@tonic-gate 		 * page are locked. Therefore this adjustment allows us to
78970Sstevel@tonic-gate 		 * decrement availrmem by the right value (note we don't want
78980Sstevel@tonic-gate 		 * to just decrement availrem by the large page size without
78990Sstevel@tonic-gate 		 * adjusting addr and len because then we may end up
79000Sstevel@tonic-gate 		 * decrementing availrmem by large page size for every
79010Sstevel@tonic-gate 		 * constituent page locked by a new as_pagelock call).
79020Sstevel@tonic-gate 		 * as_pageunlock caller must always match as_pagelock call's
79030Sstevel@tonic-gate 		 * addr and len.
79040Sstevel@tonic-gate 		 *
79050Sstevel@tonic-gate 		 * Note segment's page size cannot change while we are holding
79060Sstevel@tonic-gate 		 * as lock.  And then it cannot change while softlockcnt is
79070Sstevel@tonic-gate 		 * not 0. This will allow us to correctly recalculate large
79080Sstevel@tonic-gate 		 * page size region for the matching pageunlock/reclaim call.
79090Sstevel@tonic-gate 		 *
79100Sstevel@tonic-gate 		 * for pageunlock *ppp points to the pointer of page_t that
79110Sstevel@tonic-gate 		 * corresponds to the real unadjusted start address. Similar
79120Sstevel@tonic-gate 		 * for pagelock *ppp must point to the pointer of page_t that
79130Sstevel@tonic-gate 		 * corresponds to the real unadjusted start address.
79140Sstevel@tonic-gate 		 */
79150Sstevel@tonic-gate 		size_t pgsz = page_get_pagesize(seg->s_szc);
79160Sstevel@tonic-gate 		CALC_LPG_REGION(pgsz, seg, addr, len, lpgaddr, lpgeaddr);
79170Sstevel@tonic-gate 		adjustpages = ((uintptr_t)(addr - lpgaddr)) >> PAGESHIFT;
79180Sstevel@tonic-gate 	}
79190Sstevel@tonic-gate 
79200Sstevel@tonic-gate 	if (type == L_PAGEUNLOCK) {
79210Sstevel@tonic-gate 
79220Sstevel@tonic-gate 		/*
79230Sstevel@tonic-gate 		 * update hat ref bits for /proc. We need to make sure
79240Sstevel@tonic-gate 		 * that threads tracing the ref and mod bits of the
79250Sstevel@tonic-gate 		 * address space get the right data.
79260Sstevel@tonic-gate 		 * Note: page ref and mod bits are updated at reclaim time
79270Sstevel@tonic-gate 		 */
79280Sstevel@tonic-gate 		if (seg->s_as->a_vbits) {
79290Sstevel@tonic-gate 			for (a = addr; a < addr + len; a += PAGESIZE) {
79300Sstevel@tonic-gate 				if (rw == S_WRITE) {
79310Sstevel@tonic-gate 					hat_setstat(seg->s_as, a,
79320Sstevel@tonic-gate 					    PAGESIZE, P_REF | P_MOD);
79330Sstevel@tonic-gate 				} else {
79340Sstevel@tonic-gate 					hat_setstat(seg->s_as, a,
79350Sstevel@tonic-gate 					    PAGESIZE, P_REF);
79360Sstevel@tonic-gate 				}
79370Sstevel@tonic-gate 			}
79380Sstevel@tonic-gate 		}
79390Sstevel@tonic-gate 		SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
79400Sstevel@tonic-gate 		if (seg->s_szc != 0) {
79410Sstevel@tonic-gate 			VM_STAT_ADD(segvnvmstats.pagelock[0]);
79420Sstevel@tonic-gate 			seg_pinactive(seg, lpgaddr, lpgeaddr - lpgaddr,
79430Sstevel@tonic-gate 			    *ppp - adjustpages, rw, segvn_reclaim);
79440Sstevel@tonic-gate 		} else {
79450Sstevel@tonic-gate 			seg_pinactive(seg, addr, len, *ppp, rw, segvn_reclaim);
79460Sstevel@tonic-gate 		}
79470Sstevel@tonic-gate 
79480Sstevel@tonic-gate 		/*
79490Sstevel@tonic-gate 		 * If someone is blocked while unmapping, we purge
79500Sstevel@tonic-gate 		 * segment page cache and thus reclaim pplist synchronously
79510Sstevel@tonic-gate 		 * without waiting for seg_pasync_thread. This speeds up
79520Sstevel@tonic-gate 		 * unmapping in cases where munmap(2) is called, while
79530Sstevel@tonic-gate 		 * raw async i/o is still in progress or where a thread
79540Sstevel@tonic-gate 		 * exits on data fault in a multithreaded application.
79550Sstevel@tonic-gate 		 */
79560Sstevel@tonic-gate 		if (AS_ISUNMAPWAIT(seg->s_as) && (svd->softlockcnt > 0)) {
79570Sstevel@tonic-gate 			/*
79580Sstevel@tonic-gate 			 * Even if we grab segvn WRITER's lock or segp_slock
79590Sstevel@tonic-gate 			 * here, there might be another thread which could've
79600Sstevel@tonic-gate 			 * successfully performed lookup/insert just before
79610Sstevel@tonic-gate 			 * we acquired the lock here.  So, grabbing either
79620Sstevel@tonic-gate 			 * lock here is of not much use.  Until we devise
79630Sstevel@tonic-gate 			 * a strategy at upper layers to solve the
79640Sstevel@tonic-gate 			 * synchronization issues completely, we expect
79650Sstevel@tonic-gate 			 * applications to handle this appropriately.
79660Sstevel@tonic-gate 			 */
79670Sstevel@tonic-gate 			segvn_purge(seg);
79680Sstevel@tonic-gate 		}
79690Sstevel@tonic-gate 		SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
79700Sstevel@tonic-gate 		TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_SEGVN_UNLOCK_END,
79710Sstevel@tonic-gate 			"segvn_pagelock: unlock seg %p addr %p", seg, addr);
79720Sstevel@tonic-gate 		return (0);
79730Sstevel@tonic-gate 	} else if (type == L_PAGERECLAIM) {
79740Sstevel@tonic-gate 		VM_STAT_COND_ADD(seg->s_szc != 0, segvnvmstats.pagelock[1]);
79750Sstevel@tonic-gate 		SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
79760Sstevel@tonic-gate 		(void) segvn_reclaim(seg, addr, len, *ppp, rw);
79770Sstevel@tonic-gate 		SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
79780Sstevel@tonic-gate 		TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_SEGVN_UNLOCK_END,
79790Sstevel@tonic-gate 			"segvn_pagelock: reclaim seg %p addr %p", seg, addr);
79800Sstevel@tonic-gate 		return (0);
79810Sstevel@tonic-gate 	}
79820Sstevel@tonic-gate 
79830Sstevel@tonic-gate 	if (seg->s_szc != 0) {
79840Sstevel@tonic-gate 		VM_STAT_ADD(segvnvmstats.pagelock[2]);
79850Sstevel@tonic-gate 		addr = lpgaddr;
79860Sstevel@tonic-gate 		len = lpgeaddr - lpgaddr;
79870Sstevel@tonic-gate 		npages = (len >> PAGESHIFT);
79880Sstevel@tonic-gate 	}
79890Sstevel@tonic-gate 
79900Sstevel@tonic-gate 	/*
79910Sstevel@tonic-gate 	 * for now we only support pagelock to anon memory. We've to check
79920Sstevel@tonic-gate 	 * protections for vnode objects and call into the vnode driver.
79930Sstevel@tonic-gate 	 * That's too much for a fast path. Let the fault entry point handle it.
79940Sstevel@tonic-gate 	 */
79950Sstevel@tonic-gate 	if (svd->vp != NULL) {
79960Sstevel@tonic-gate 		TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_SEGVN_MISS_END,
79970Sstevel@tonic-gate 		    "segvn_pagelock: mapped vnode seg %p addr %p", seg, addr);
79980Sstevel@tonic-gate 		*ppp = NULL;
79990Sstevel@tonic-gate 		return (ENOTSUP);
80000Sstevel@tonic-gate 	}
80010Sstevel@tonic-gate 
80020Sstevel@tonic-gate 	/*
80030Sstevel@tonic-gate 	 * if anonmap is not yet created, let the fault entry point populate it
80040Sstevel@tonic-gate 	 * with anon ptrs.
80050Sstevel@tonic-gate 	 */
80060Sstevel@tonic-gate 	if ((amp = svd->amp) == NULL) {
80070Sstevel@tonic-gate 		TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_SEGVN_MISS_END,
80080Sstevel@tonic-gate 		    "segvn_pagelock: anonmap null seg %p addr %p", seg, addr);
80090Sstevel@tonic-gate 		*ppp = NULL;
80100Sstevel@tonic-gate 		return (EFAULT);
80110Sstevel@tonic-gate 	}
80120Sstevel@tonic-gate 
80130Sstevel@tonic-gate 	SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
80140Sstevel@tonic-gate 
80150Sstevel@tonic-gate 	/*
80160Sstevel@tonic-gate 	 * we acquire segp_slock to prevent duplicate entries
80170Sstevel@tonic-gate 	 * in seg_pcache
80180Sstevel@tonic-gate 	 */
80190Sstevel@tonic-gate 	mutex_enter(&svd->segp_slock);
80200Sstevel@tonic-gate 
80210Sstevel@tonic-gate 	/*
80220Sstevel@tonic-gate 	 * try to find pages in segment page cache
80230Sstevel@tonic-gate 	 */
80240Sstevel@tonic-gate 	pplist = seg_plookup(seg, addr, len, rw);
80250Sstevel@tonic-gate 	if (pplist != NULL) {
80260Sstevel@tonic-gate 		mutex_exit(&svd->segp_slock);
80270Sstevel@tonic-gate 		SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
80280Sstevel@tonic-gate 		*ppp = pplist + adjustpages;
80290Sstevel@tonic-gate 		TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_SEGVN_HIT_END,
80300Sstevel@tonic-gate 			"segvn_pagelock: cache hit seg %p addr %p", seg, addr);
80310Sstevel@tonic-gate 		return (0);
80320Sstevel@tonic-gate 	}
80330Sstevel@tonic-gate 
80340Sstevel@tonic-gate 	if (rw == S_READ) {
80350Sstevel@tonic-gate 		protchk = PROT_READ;
80360Sstevel@tonic-gate 	} else {
80370Sstevel@tonic-gate 		protchk = PROT_WRITE;
80380Sstevel@tonic-gate 	}
80390Sstevel@tonic-gate 
80400Sstevel@tonic-gate 	if (svd->pageprot == 0) {
80410Sstevel@tonic-gate 		if ((svd->prot & protchk) == 0) {
80420Sstevel@tonic-gate 			mutex_exit(&svd->segp_slock);
80430Sstevel@tonic-gate 			error = EFAULT;
80440Sstevel@tonic-gate 			goto out;
80450Sstevel@tonic-gate 		}
80460Sstevel@tonic-gate 	} else {
80470Sstevel@tonic-gate 		/*
80480Sstevel@tonic-gate 		 * check page protections
80490Sstevel@tonic-gate 		 */
80500Sstevel@tonic-gate 		for (a = addr; a < addr + len; a += PAGESIZE) {
80510Sstevel@tonic-gate 			struct vpage *vp;
80520Sstevel@tonic-gate 
80530Sstevel@tonic-gate 			vp = &svd->vpage[seg_page(seg, a)];
80540Sstevel@tonic-gate 			if ((VPP_PROT(vp) & protchk) == 0) {
80550Sstevel@tonic-gate 				mutex_exit(&svd->segp_slock);
80560Sstevel@tonic-gate 				error = EFAULT;
80570Sstevel@tonic-gate 				goto out;
80580Sstevel@tonic-gate 			}
80590Sstevel@tonic-gate 		}
80600Sstevel@tonic-gate 	}
80610Sstevel@tonic-gate 
80622414Saguzovsk 	/*
80632414Saguzovsk 	 * Avoid per page overhead of segvn_pp_lock_anonpages() for small
80642414Saguzovsk 	 * pages. For large pages segvn_pp_lock_anonpages() only does real
80652414Saguzovsk 	 * work once per large page.  The tradeoff is that we may decrement
80662414Saguzovsk 	 * availrmem more than once for the same page but this is ok
80672414Saguzovsk 	 * for small pages.
80682414Saguzovsk 	 */
80692414Saguzovsk 	if (seg->s_szc == 0) {
80702414Saguzovsk 		mutex_enter(&freemem_lock);
80712414Saguzovsk 		if (availrmem < tune.t_minarmem + npages) {
80722414Saguzovsk 			mutex_exit(&freemem_lock);
80732414Saguzovsk 			mutex_exit(&svd->segp_slock);
80742414Saguzovsk 			error = ENOMEM;
80752414Saguzovsk 			goto out;
80762414Saguzovsk 		}
80772414Saguzovsk 		availrmem -= npages;
80780Sstevel@tonic-gate 		mutex_exit(&freemem_lock);
80792414Saguzovsk 	}
80800Sstevel@tonic-gate 
80810Sstevel@tonic-gate 	pplist = kmem_alloc(sizeof (page_t *) * npages, KM_SLEEP);
80820Sstevel@tonic-gate 	pl = pplist;
80830Sstevel@tonic-gate 	*ppp = pplist + adjustpages;
80840Sstevel@tonic-gate 
80850Sstevel@tonic-gate 	page = seg_page(seg, addr);
80860Sstevel@tonic-gate 	anon_index = svd->anon_index + page;
80870Sstevel@tonic-gate 
80880Sstevel@tonic-gate 	ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
80890Sstevel@tonic-gate 	for (a = addr; a < addr + len; a += PAGESIZE, anon_index++) {
80900Sstevel@tonic-gate 		struct anon *ap;
80910Sstevel@tonic-gate 		struct vnode *vp;
80920Sstevel@tonic-gate 		u_offset_t off;
80930Sstevel@tonic-gate 		anon_sync_obj_t cookie;
80940Sstevel@tonic-gate 
80950Sstevel@tonic-gate 		anon_array_enter(amp, anon_index, &cookie);
80960Sstevel@tonic-gate 		ap = anon_get_ptr(amp->ahp, anon_index);
80970Sstevel@tonic-gate 		if (ap == NULL) {
80980Sstevel@tonic-gate 			anon_array_exit(&cookie);
80990Sstevel@tonic-gate 			break;
81000Sstevel@tonic-gate 		} else {
81010Sstevel@tonic-gate 			/*
81020Sstevel@tonic-gate 			 * We must never use seg_pcache for COW pages
81030Sstevel@tonic-gate 			 * because we might end up with original page still
81040Sstevel@tonic-gate 			 * lying in seg_pcache even after private page is
81050Sstevel@tonic-gate 			 * created. This leads to data corruption as
81060Sstevel@tonic-gate 			 * aio_write refers to the page still in cache
81070Sstevel@tonic-gate 			 * while all other accesses refer to the private
81080Sstevel@tonic-gate 			 * page.
81090Sstevel@tonic-gate 			 */
81100Sstevel@tonic-gate 			if (ap->an_refcnt != 1) {
81110Sstevel@tonic-gate 				anon_array_exit(&cookie);
81120Sstevel@tonic-gate 				break;
81130Sstevel@tonic-gate 			}
81140Sstevel@tonic-gate 		}
81150Sstevel@tonic-gate 		swap_xlate(ap, &vp, &off);
81160Sstevel@tonic-gate 		anon_array_exit(&cookie);
81170Sstevel@tonic-gate 
81180Sstevel@tonic-gate 		pp = page_lookup_nowait(vp, off, SE_SHARED);
81190Sstevel@tonic-gate 		if (pp == NULL) {
81200Sstevel@tonic-gate 			break;
81210Sstevel@tonic-gate 		}
81222414Saguzovsk 		if (seg->s_szc != 0 || pp->p_szc != 0) {
81232414Saguzovsk 			if (!segvn_pp_lock_anonpages(pp, a == addr)) {
81242414Saguzovsk 				page_unlock(pp);
81252414Saguzovsk 				break;
81262414Saguzovsk 			}
81272414Saguzovsk 		} else {
81282414Saguzovsk 			szc0_npages++;
81292414Saguzovsk 		}
81300Sstevel@tonic-gate 		*pplist++ = pp;
81310Sstevel@tonic-gate 	}
81320Sstevel@tonic-gate 	ANON_LOCK_EXIT(&amp->a_rwlock);
81330Sstevel@tonic-gate 
81342414Saguzovsk 	ASSERT(npages >= szc0_npages);
81352414Saguzovsk 
81360Sstevel@tonic-gate 	if (a >= addr + len) {
81372414Saguzovsk 		mutex_enter(&freemem_lock);
81382414Saguzovsk 		if (seg->s_szc == 0 && npages != szc0_npages) {
81392414Saguzovsk 			ASSERT(svd->type == MAP_SHARED && amp->a_szc > 0);
81402414Saguzovsk 			availrmem += (npages - szc0_npages);
81412414Saguzovsk 		}
81422414Saguzovsk 		svd->softlockcnt += npages;
81432414Saguzovsk 		segvn_pages_locked += npages;
81442414Saguzovsk 		mutex_exit(&freemem_lock);
81450Sstevel@tonic-gate 		(void) seg_pinsert(seg, addr, len, pl, rw, SEGP_ASYNC_FLUSH,
81460Sstevel@tonic-gate 			segvn_reclaim);
81470Sstevel@tonic-gate 		mutex_exit(&svd->segp_slock);
81480Sstevel@tonic-gate 		SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
81490Sstevel@tonic-gate 		TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_SEGVN_FILL_END,
81500Sstevel@tonic-gate 		    "segvn_pagelock: cache fill seg %p addr %p", seg, addr);
81510Sstevel@tonic-gate 		return (0);
81520Sstevel@tonic-gate 	}
81530Sstevel@tonic-gate 
81540Sstevel@tonic-gate 	mutex_exit(&svd->segp_slock);
81552414Saguzovsk 	if (seg->s_szc == 0) {
81562414Saguzovsk 		mutex_enter(&freemem_lock);
81572414Saguzovsk 		availrmem += npages;
81582414Saguzovsk 		mutex_exit(&freemem_lock);
81592414Saguzovsk 	}
81600Sstevel@tonic-gate 	error = EFAULT;
81610Sstevel@tonic-gate 	pplist = pl;
81620Sstevel@tonic-gate 	np = ((uintptr_t)(a - addr)) >> PAGESHIFT;
81630Sstevel@tonic-gate 	while (np > (uint_t)0) {
81642414Saguzovsk 		ASSERT(PAGE_LOCKED(*pplist));
81652414Saguzovsk 		if (seg->s_szc != 0 || (*pplist)->p_szc != 0) {
81662414Saguzovsk 			segvn_pp_unlock_anonpages(*pplist, pplist == pl);
81672414Saguzovsk 		}
81680Sstevel@tonic-gate 		page_unlock(*pplist);
81690Sstevel@tonic-gate 		np--;
81700Sstevel@tonic-gate 		pplist++;
81710Sstevel@tonic-gate 	}
81720Sstevel@tonic-gate 	kmem_free(pl, sizeof (page_t *) * npages);
81730Sstevel@tonic-gate out:
81740Sstevel@tonic-gate 	SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
81750Sstevel@tonic-gate 	*ppp = NULL;
81760Sstevel@tonic-gate 	TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_SEGVN_MISS_END,
81770Sstevel@tonic-gate 		"segvn_pagelock: cache miss seg %p addr %p", seg, addr);
81780Sstevel@tonic-gate 	return (error);
81790Sstevel@tonic-gate }
81800Sstevel@tonic-gate 
81810Sstevel@tonic-gate /*
81820Sstevel@tonic-gate  * purge any cached pages in the I/O page cache
81830Sstevel@tonic-gate  */
81840Sstevel@tonic-gate static void
81850Sstevel@tonic-gate segvn_purge(struct seg *seg)
81860Sstevel@tonic-gate {
81870Sstevel@tonic-gate 	seg_ppurge(seg);
81880Sstevel@tonic-gate }
81890Sstevel@tonic-gate 
81900Sstevel@tonic-gate static int
81910Sstevel@tonic-gate segvn_reclaim(struct seg *seg, caddr_t addr, size_t len, struct page **pplist,
81920Sstevel@tonic-gate 	enum seg_rw rw)
81930Sstevel@tonic-gate {
81940Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
81950Sstevel@tonic-gate 	pgcnt_t np, npages;
81960Sstevel@tonic-gate 	struct page **pl;
81972414Saguzovsk 	pgcnt_t szc0_npages = 0;
81980Sstevel@tonic-gate 
81990Sstevel@tonic-gate #ifdef lint
82000Sstevel@tonic-gate 	addr = addr;
82010Sstevel@tonic-gate #endif
82020Sstevel@tonic-gate 
82030Sstevel@tonic-gate 	npages = np = (len >> PAGESHIFT);
82040Sstevel@tonic-gate 	ASSERT(npages);
82050Sstevel@tonic-gate 	pl = pplist;
82060Sstevel@tonic-gate 	if (seg->s_szc != 0) {
82070Sstevel@tonic-gate 		size_t pgsz = page_get_pagesize(seg->s_szc);
82080Sstevel@tonic-gate 		if (!IS_P2ALIGNED(addr, pgsz) || !IS_P2ALIGNED(len, pgsz)) {
82090Sstevel@tonic-gate 			panic("segvn_reclaim: unaligned addr or len");
82100Sstevel@tonic-gate 			/*NOTREACHED*/
82110Sstevel@tonic-gate 		}
82120Sstevel@tonic-gate 	}
82130Sstevel@tonic-gate 
82142414Saguzovsk 	ASSERT(svd->vp == NULL && svd->amp != NULL);
82152414Saguzovsk 
82160Sstevel@tonic-gate 	while (np > (uint_t)0) {
82170Sstevel@tonic-gate 		if (rw == S_WRITE) {
82180Sstevel@tonic-gate 			hat_setrefmod(*pplist);
82190Sstevel@tonic-gate 		} else {
82200Sstevel@tonic-gate 			hat_setref(*pplist);
82210Sstevel@tonic-gate 		}
82222414Saguzovsk 		if (seg->s_szc != 0 || (*pplist)->p_szc != 0) {
82232414Saguzovsk 			segvn_pp_unlock_anonpages(*pplist, pplist == pl);
82242414Saguzovsk 		} else {
82252414Saguzovsk 			szc0_npages++;
82262414Saguzovsk 		}
82270Sstevel@tonic-gate 		page_unlock(*pplist);
82280Sstevel@tonic-gate 		np--;
82290Sstevel@tonic-gate 		pplist++;
82300Sstevel@tonic-gate 	}
82310Sstevel@tonic-gate 	kmem_free(pl, sizeof (page_t *) * npages);
82320Sstevel@tonic-gate 
82330Sstevel@tonic-gate 	mutex_enter(&freemem_lock);
82340Sstevel@tonic-gate 	segvn_pages_locked -= npages;
82350Sstevel@tonic-gate 	svd->softlockcnt -= npages;
82362414Saguzovsk 	if (szc0_npages != 0) {
82372414Saguzovsk 		availrmem += szc0_npages;
82382414Saguzovsk 	}
82390Sstevel@tonic-gate 	mutex_exit(&freemem_lock);
82400Sstevel@tonic-gate 	if (svd->softlockcnt <= 0) {
82410Sstevel@tonic-gate 		if (AS_ISUNMAPWAIT(seg->s_as)) {
82420Sstevel@tonic-gate 			mutex_enter(&seg->s_as->a_contents);
82430Sstevel@tonic-gate 			if (AS_ISUNMAPWAIT(seg->s_as)) {
82440Sstevel@tonic-gate 				AS_CLRUNMAPWAIT(seg->s_as);
82450Sstevel@tonic-gate 				cv_broadcast(&seg->s_as->a_cv);
82460Sstevel@tonic-gate 			}
82470Sstevel@tonic-gate 			mutex_exit(&seg->s_as->a_contents);
82480Sstevel@tonic-gate 		}
82490Sstevel@tonic-gate 	}
82500Sstevel@tonic-gate 	return (0);
82510Sstevel@tonic-gate }
82520Sstevel@tonic-gate /*
82530Sstevel@tonic-gate  * get a memory ID for an addr in a given segment
82540Sstevel@tonic-gate  *
82550Sstevel@tonic-gate  * XXX only creates PAGESIZE pages if anon slots are not initialized.
82560Sstevel@tonic-gate  * At fault time they will be relocated into larger pages.
82570Sstevel@tonic-gate  */
82580Sstevel@tonic-gate static int
82590Sstevel@tonic-gate segvn_getmemid(struct seg *seg, caddr_t addr, memid_t *memidp)
82600Sstevel@tonic-gate {
82610Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
82620Sstevel@tonic-gate 	struct anon 	*ap = NULL;
82630Sstevel@tonic-gate 	ulong_t		anon_index;
82640Sstevel@tonic-gate 	struct anon_map	*amp;
82650Sstevel@tonic-gate 	anon_sync_obj_t cookie;
82660Sstevel@tonic-gate 
82670Sstevel@tonic-gate 	if (svd->type == MAP_PRIVATE) {
82680Sstevel@tonic-gate 		memidp->val[0] = (uintptr_t)seg->s_as;
82690Sstevel@tonic-gate 		memidp->val[1] = (uintptr_t)addr;
82700Sstevel@tonic-gate 		return (0);
82710Sstevel@tonic-gate 	}
82720Sstevel@tonic-gate 
82730Sstevel@tonic-gate 	if (svd->type == MAP_SHARED) {
82740Sstevel@tonic-gate 		if (svd->vp) {
82750Sstevel@tonic-gate 			memidp->val[0] = (uintptr_t)svd->vp;
82760Sstevel@tonic-gate 			memidp->val[1] = (u_longlong_t)svd->offset +
82770Sstevel@tonic-gate 			    (uintptr_t)(addr - seg->s_base);
82780Sstevel@tonic-gate 			return (0);
82790Sstevel@tonic-gate 		} else {
82800Sstevel@tonic-gate 
82810Sstevel@tonic-gate 			SEGVN_LOCK_ENTER(seg->s_as, &svd->lock, RW_READER);
82820Sstevel@tonic-gate 			if ((amp = svd->amp) != NULL) {
82830Sstevel@tonic-gate 				anon_index = svd->anon_index +
82840Sstevel@tonic-gate 				    seg_page(seg, addr);
82850Sstevel@tonic-gate 			}
82860Sstevel@tonic-gate 			SEGVN_LOCK_EXIT(seg->s_as, &svd->lock);
82870Sstevel@tonic-gate 
82880Sstevel@tonic-gate 			ASSERT(amp != NULL);
82890Sstevel@tonic-gate 
82900Sstevel@tonic-gate 			ANON_LOCK_ENTER(&amp->a_rwlock, RW_READER);
82910Sstevel@tonic-gate 			anon_array_enter(amp, anon_index, &cookie);
82920Sstevel@tonic-gate 			ap = anon_get_ptr(amp->ahp, anon_index);
82930Sstevel@tonic-gate 			if (ap == NULL) {
82940Sstevel@tonic-gate 				page_t		*pp;
82950Sstevel@tonic-gate 
82960Sstevel@tonic-gate 				pp = anon_zero(seg, addr, &ap, svd->cred);
82970Sstevel@tonic-gate 				if (pp == NULL) {
82980Sstevel@tonic-gate 					anon_array_exit(&cookie);
82990Sstevel@tonic-gate 					ANON_LOCK_EXIT(&amp->a_rwlock);
83000Sstevel@tonic-gate 					return (ENOMEM);
83010Sstevel@tonic-gate 				}
83020Sstevel@tonic-gate 				ASSERT(anon_get_ptr(amp->ahp, anon_index)
83030Sstevel@tonic-gate 								== NULL);
83040Sstevel@tonic-gate 				(void) anon_set_ptr(amp->ahp, anon_index,
83050Sstevel@tonic-gate 				    ap, ANON_SLEEP);
83060Sstevel@tonic-gate 				page_unlock(pp);
83070Sstevel@tonic-gate 			}
83080Sstevel@tonic-gate 
83090Sstevel@tonic-gate 			anon_array_exit(&cookie);
83100Sstevel@tonic-gate 			ANON_LOCK_EXIT(&amp->a_rwlock);
83110Sstevel@tonic-gate 
83120Sstevel@tonic-gate 			memidp->val[0] = (uintptr_t)ap;
83130Sstevel@tonic-gate 			memidp->val[1] = (uintptr_t)addr & PAGEOFFSET;
83140Sstevel@tonic-gate 			return (0);
83150Sstevel@tonic-gate 		}
83160Sstevel@tonic-gate 	}
83170Sstevel@tonic-gate 	return (EINVAL);
83180Sstevel@tonic-gate }
83190Sstevel@tonic-gate 
83200Sstevel@tonic-gate static int
83210Sstevel@tonic-gate sameprot(struct seg *seg, caddr_t a, size_t len)
83220Sstevel@tonic-gate {
83230Sstevel@tonic-gate 	struct segvn_data *svd = (struct segvn_data *)seg->s_data;
83240Sstevel@tonic-gate 	struct vpage *vpage;
83250Sstevel@tonic-gate 	spgcnt_t pages = btop(len);
83260Sstevel@tonic-gate 	uint_t prot;
83270Sstevel@tonic-gate 
83280Sstevel@tonic-gate 	if (svd->pageprot == 0)
83290Sstevel@tonic-gate 		return (1);
83300Sstevel@tonic-gate 
83310Sstevel@tonic-gate 	ASSERT(svd->vpage != NULL);
83320Sstevel@tonic-gate 
83330Sstevel@tonic-gate 	vpage = &svd->vpage[seg_page(seg, a)];
83340Sstevel@tonic-gate 	prot = VPP_PROT(vpage);
83350Sstevel@tonic-gate 	vpage++;
83360Sstevel@tonic-gate 	pages--;
83370Sstevel@tonic-gate 	while (pages-- > 0) {
83380Sstevel@tonic-gate 		if (prot != VPP_PROT(vpage))
83390Sstevel@tonic-gate 			return (0);
83400Sstevel@tonic-gate 		vpage++;
83410Sstevel@tonic-gate 	}
83420Sstevel@tonic-gate 	return (1);
83430Sstevel@tonic-gate }
83440Sstevel@tonic-gate 
83450Sstevel@tonic-gate /*
83460Sstevel@tonic-gate  * Get memory allocation policy info for specified address in given segment
83470Sstevel@tonic-gate  */
83480Sstevel@tonic-gate static lgrp_mem_policy_info_t *
83490Sstevel@tonic-gate segvn_getpolicy(struct seg *seg, caddr_t addr)
83500Sstevel@tonic-gate {
83510Sstevel@tonic-gate 	struct anon_map		*amp;
83520Sstevel@tonic-gate 	ulong_t			anon_index;
83530Sstevel@tonic-gate 	lgrp_mem_policy_info_t	*policy_info;
83540Sstevel@tonic-gate 	struct segvn_data	*svn_data;
83550Sstevel@tonic-gate 	u_offset_t		vn_off;
83560Sstevel@tonic-gate 	vnode_t			*vp;
83570Sstevel@tonic-gate 
83580Sstevel@tonic-gate 	ASSERT(seg != NULL);
83590Sstevel@tonic-gate 
83600Sstevel@tonic-gate 	svn_data = (struct segvn_data *)seg->s_data;
83610Sstevel@tonic-gate 	if (svn_data == NULL)
83620Sstevel@tonic-gate 		return (NULL);
83630Sstevel@tonic-gate 
83640Sstevel@tonic-gate 	/*
83650Sstevel@tonic-gate 	 * Get policy info for private or shared memory
83660Sstevel@tonic-gate 	 */
83670Sstevel@tonic-gate 	if (svn_data->type != MAP_SHARED)
83680Sstevel@tonic-gate 		policy_info = &svn_data->policy_info;
83690Sstevel@tonic-gate 	else {
83700Sstevel@tonic-gate 		amp = svn_data->amp;
83710Sstevel@tonic-gate 		anon_index = svn_data->anon_index + seg_page(seg, addr);
83720Sstevel@tonic-gate 		vp = svn_data->vp;
83730Sstevel@tonic-gate 		vn_off = svn_data->offset + (uintptr_t)(addr - seg->s_base);
83740Sstevel@tonic-gate 		policy_info = lgrp_shm_policy_get(amp, anon_index, vp, vn_off);
83750Sstevel@tonic-gate 	}
83760Sstevel@tonic-gate 
83770Sstevel@tonic-gate 	return (policy_info);
83780Sstevel@tonic-gate }
8379670Selowe 
8380670Selowe /*ARGSUSED*/
8381670Selowe static int
8382670Selowe segvn_capable(struct seg *seg, segcapability_t capability)
8383670Selowe {
8384670Selowe 	return (0);
8385670Selowe }
8386