xref: /onnv-gate/usr/src/uts/common/vm/seg_map.c (revision 1841)
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
5*1841Spraks  * Common Development and Distribution License (the "License").
6*1841Spraks  * 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*1841Spraks  * Copyright 2006 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) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
310Sstevel@tonic-gate  * under license from the Regents of the University of California.
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
350Sstevel@tonic-gate 
360Sstevel@tonic-gate /*
370Sstevel@tonic-gate  * VM - generic vnode mapping segment.
380Sstevel@tonic-gate  *
390Sstevel@tonic-gate  * The segmap driver is used only by the kernel to get faster (than seg_vn)
400Sstevel@tonic-gate  * mappings [lower routine overhead; more persistent cache] to random
410Sstevel@tonic-gate  * vnode/offsets.  Note than the kernel may (and does) use seg_vn as well.
420Sstevel@tonic-gate  */
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include <sys/types.h>
450Sstevel@tonic-gate #include <sys/t_lock.h>
460Sstevel@tonic-gate #include <sys/param.h>
470Sstevel@tonic-gate #include <sys/sysmacros.h>
480Sstevel@tonic-gate #include <sys/buf.h>
490Sstevel@tonic-gate #include <sys/systm.h>
500Sstevel@tonic-gate #include <sys/vnode.h>
510Sstevel@tonic-gate #include <sys/mman.h>
520Sstevel@tonic-gate #include <sys/errno.h>
530Sstevel@tonic-gate #include <sys/cred.h>
540Sstevel@tonic-gate #include <sys/kmem.h>
550Sstevel@tonic-gate #include <sys/vtrace.h>
560Sstevel@tonic-gate #include <sys/cmn_err.h>
570Sstevel@tonic-gate #include <sys/debug.h>
580Sstevel@tonic-gate #include <sys/thread.h>
590Sstevel@tonic-gate #include <sys/dumphdr.h>
600Sstevel@tonic-gate #include <sys/bitmap.h>
610Sstevel@tonic-gate #include <sys/lgrp.h>
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #include <vm/seg_kmem.h>
640Sstevel@tonic-gate #include <vm/hat.h>
650Sstevel@tonic-gate #include <vm/as.h>
660Sstevel@tonic-gate #include <vm/seg.h>
670Sstevel@tonic-gate #include <vm/seg_kpm.h>
680Sstevel@tonic-gate #include <vm/seg_map.h>
690Sstevel@tonic-gate #include <vm/page.h>
700Sstevel@tonic-gate #include <vm/pvn.h>
710Sstevel@tonic-gate #include <vm/rm.h>
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate  * Private seg op routines.
750Sstevel@tonic-gate  */
760Sstevel@tonic-gate static void	segmap_free(struct seg *seg);
770Sstevel@tonic-gate faultcode_t segmap_fault(struct hat *hat, struct seg *seg, caddr_t addr,
780Sstevel@tonic-gate 			size_t len, enum fault_type type, enum seg_rw rw);
790Sstevel@tonic-gate static faultcode_t segmap_faulta(struct seg *seg, caddr_t addr);
800Sstevel@tonic-gate static int	segmap_checkprot(struct seg *seg, caddr_t addr, size_t len,
810Sstevel@tonic-gate 			uint_t prot);
820Sstevel@tonic-gate static int	segmap_kluster(struct seg *seg, caddr_t addr, ssize_t);
830Sstevel@tonic-gate static int	segmap_getprot(struct seg *seg, caddr_t addr, size_t len,
840Sstevel@tonic-gate 			uint_t *protv);
850Sstevel@tonic-gate static u_offset_t	segmap_getoffset(struct seg *seg, caddr_t addr);
860Sstevel@tonic-gate static int	segmap_gettype(struct seg *seg, caddr_t addr);
870Sstevel@tonic-gate static int	segmap_getvp(struct seg *seg, caddr_t addr, struct vnode **vpp);
880Sstevel@tonic-gate static void	segmap_dump(struct seg *seg);
890Sstevel@tonic-gate static int	segmap_pagelock(struct seg *seg, caddr_t addr, size_t len,
900Sstevel@tonic-gate 			struct page ***ppp, enum lock_type type,
910Sstevel@tonic-gate 			enum seg_rw rw);
920Sstevel@tonic-gate static void	segmap_badop(void);
930Sstevel@tonic-gate static int	segmap_getmemid(struct seg *seg, caddr_t addr, memid_t *memidp);
940Sstevel@tonic-gate static lgrp_mem_policy_info_t	*segmap_getpolicy(struct seg *seg,
950Sstevel@tonic-gate     caddr_t addr);
96670Selowe static int	segmap_capable(struct seg *seg, segcapability_t capability);
970Sstevel@tonic-gate 
980Sstevel@tonic-gate /* segkpm support */
990Sstevel@tonic-gate static caddr_t	segmap_pagecreate_kpm(struct seg *, vnode_t *, u_offset_t,
1000Sstevel@tonic-gate 			struct smap *, enum seg_rw);
1010Sstevel@tonic-gate struct smap	*get_smap_kpm(caddr_t, page_t **);
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate #define	SEGMAP_BADOP(t)	(t(*)())segmap_badop
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate static struct seg_ops segmap_ops = {
1060Sstevel@tonic-gate 	SEGMAP_BADOP(int),	/* dup */
1070Sstevel@tonic-gate 	SEGMAP_BADOP(int),	/* unmap */
1080Sstevel@tonic-gate 	segmap_free,
1090Sstevel@tonic-gate 	segmap_fault,
1100Sstevel@tonic-gate 	segmap_faulta,
1110Sstevel@tonic-gate 	SEGMAP_BADOP(int),	/* setprot */
1120Sstevel@tonic-gate 	segmap_checkprot,
1130Sstevel@tonic-gate 	segmap_kluster,
1140Sstevel@tonic-gate 	SEGMAP_BADOP(size_t),	/* swapout */
1150Sstevel@tonic-gate 	SEGMAP_BADOP(int),	/* sync */
1160Sstevel@tonic-gate 	SEGMAP_BADOP(size_t),	/* incore */
1170Sstevel@tonic-gate 	SEGMAP_BADOP(int),	/* lockop */
1180Sstevel@tonic-gate 	segmap_getprot,
1190Sstevel@tonic-gate 	segmap_getoffset,
1200Sstevel@tonic-gate 	segmap_gettype,
1210Sstevel@tonic-gate 	segmap_getvp,
1220Sstevel@tonic-gate 	SEGMAP_BADOP(int),	/* advise */
1230Sstevel@tonic-gate 	segmap_dump,
1240Sstevel@tonic-gate 	segmap_pagelock,	/* pagelock */
1250Sstevel@tonic-gate 	SEGMAP_BADOP(int),	/* setpgsz */
1260Sstevel@tonic-gate 	segmap_getmemid,	/* getmemid */
1270Sstevel@tonic-gate 	segmap_getpolicy,	/* getpolicy */
128670Selowe 	segmap_capable,		/* capable */
1290Sstevel@tonic-gate };
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate /*
1320Sstevel@tonic-gate  * Private segmap routines.
1330Sstevel@tonic-gate  */
1340Sstevel@tonic-gate static void	segmap_unlock(struct hat *hat, struct seg *seg, caddr_t addr,
1350Sstevel@tonic-gate 			size_t len, enum seg_rw rw, struct smap *smp);
1360Sstevel@tonic-gate static void	segmap_smapadd(struct smap *smp);
1370Sstevel@tonic-gate static struct smap *segmap_hashin(struct smap *smp, struct vnode *vp,
1380Sstevel@tonic-gate 			u_offset_t off, int hashid);
1390Sstevel@tonic-gate static void	segmap_hashout(struct smap *smp);
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate /*
1430Sstevel@tonic-gate  * Statistics for segmap operations.
1440Sstevel@tonic-gate  *
1450Sstevel@tonic-gate  * No explicit locking to protect these stats.
1460Sstevel@tonic-gate  */
1470Sstevel@tonic-gate struct segmapcnt segmapcnt = {
1480Sstevel@tonic-gate 	{ "fault",		KSTAT_DATA_ULONG },
1490Sstevel@tonic-gate 	{ "faulta",		KSTAT_DATA_ULONG },
1500Sstevel@tonic-gate 	{ "getmap",		KSTAT_DATA_ULONG },
1510Sstevel@tonic-gate 	{ "get_use",		KSTAT_DATA_ULONG },
1520Sstevel@tonic-gate 	{ "get_reclaim",	KSTAT_DATA_ULONG },
1530Sstevel@tonic-gate 	{ "get_reuse",		KSTAT_DATA_ULONG },
1540Sstevel@tonic-gate 	{ "get_unused",		KSTAT_DATA_ULONG },
1550Sstevel@tonic-gate 	{ "get_nofree",		KSTAT_DATA_ULONG },
1560Sstevel@tonic-gate 	{ "rel_async",		KSTAT_DATA_ULONG },
1570Sstevel@tonic-gate 	{ "rel_write",		KSTAT_DATA_ULONG },
1580Sstevel@tonic-gate 	{ "rel_free",		KSTAT_DATA_ULONG },
1590Sstevel@tonic-gate 	{ "rel_abort",		KSTAT_DATA_ULONG },
1600Sstevel@tonic-gate 	{ "rel_dontneed",	KSTAT_DATA_ULONG },
1610Sstevel@tonic-gate 	{ "release",		KSTAT_DATA_ULONG },
1620Sstevel@tonic-gate 	{ "pagecreate",		KSTAT_DATA_ULONG },
1630Sstevel@tonic-gate 	{ "free_notfree",	KSTAT_DATA_ULONG },
1640Sstevel@tonic-gate 	{ "free_dirty",		KSTAT_DATA_ULONG },
1650Sstevel@tonic-gate 	{ "free",		KSTAT_DATA_ULONG },
1660Sstevel@tonic-gate 	{ "stolen",		KSTAT_DATA_ULONG },
1670Sstevel@tonic-gate 	{ "get_nomtx",		KSTAT_DATA_ULONG }
1680Sstevel@tonic-gate };
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate kstat_named_t *segmapcnt_ptr = (kstat_named_t *)&segmapcnt;
1710Sstevel@tonic-gate uint_t segmapcnt_ndata = sizeof (segmapcnt) / sizeof (kstat_named_t);
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate /*
1740Sstevel@tonic-gate  * Return number of map pages in segment.
1750Sstevel@tonic-gate  */
1760Sstevel@tonic-gate #define	MAP_PAGES(seg)		((seg)->s_size >> MAXBSHIFT)
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate /*
1790Sstevel@tonic-gate  * Translate addr into smap number within segment.
1800Sstevel@tonic-gate  */
1810Sstevel@tonic-gate #define	MAP_PAGE(seg, addr)  (((addr) - (seg)->s_base) >> MAXBSHIFT)
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate /*
1840Sstevel@tonic-gate  * Translate addr in seg into struct smap pointer.
1850Sstevel@tonic-gate  */
1860Sstevel@tonic-gate #define	GET_SMAP(seg, addr)	\
1870Sstevel@tonic-gate 	&(((struct segmap_data *)((seg)->s_data))->smd_sm[MAP_PAGE(seg, addr)])
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate /*
1900Sstevel@tonic-gate  * Bit in map (16 bit bitmap).
1910Sstevel@tonic-gate  */
1920Sstevel@tonic-gate #define	SMAP_BIT_MASK(bitindex)	(1 << ((bitindex) & 0xf))
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate static int smd_colormsk = 0;
1950Sstevel@tonic-gate static int smd_ncolor = 0;
1960Sstevel@tonic-gate static int smd_nfree = 0;
1970Sstevel@tonic-gate static int smd_freemsk = 0;
1980Sstevel@tonic-gate #ifdef DEBUG
1990Sstevel@tonic-gate static int *colors_used;
2000Sstevel@tonic-gate #endif
2010Sstevel@tonic-gate static struct smap *smd_smap;
2020Sstevel@tonic-gate static struct smaphash *smd_hash;
2030Sstevel@tonic-gate #ifdef SEGMAP_HASHSTATS
2040Sstevel@tonic-gate static unsigned int *smd_hash_len;
2050Sstevel@tonic-gate #endif
2060Sstevel@tonic-gate static struct smfree *smd_free;
2070Sstevel@tonic-gate static ulong_t smd_hashmsk = 0;
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate #define	SEGMAP_MAXCOLOR		2
2100Sstevel@tonic-gate #define	SEGMAP_CACHE_PAD	64
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate union segmap_cpu {
2130Sstevel@tonic-gate 	struct {
2140Sstevel@tonic-gate 		uint32_t	scpu_free_ndx[SEGMAP_MAXCOLOR];
2150Sstevel@tonic-gate 		struct smap	*scpu_last_smap;
2160Sstevel@tonic-gate 		ulong_t		scpu_getmap;
2170Sstevel@tonic-gate 		ulong_t		scpu_release;
2180Sstevel@tonic-gate 		ulong_t		scpu_get_reclaim;
2190Sstevel@tonic-gate 		ulong_t		scpu_fault;
2200Sstevel@tonic-gate 		ulong_t		scpu_pagecreate;
2210Sstevel@tonic-gate 		ulong_t		scpu_get_reuse;
2220Sstevel@tonic-gate 	} scpu;
2230Sstevel@tonic-gate 	char	scpu_pad[SEGMAP_CACHE_PAD];
2240Sstevel@tonic-gate };
2250Sstevel@tonic-gate static union segmap_cpu *smd_cpu;
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate /*
2280Sstevel@tonic-gate  * There are three locks in seg_map:
2290Sstevel@tonic-gate  *	- per freelist mutexes
2300Sstevel@tonic-gate  *	- per hashchain mutexes
2310Sstevel@tonic-gate  *	- per smap mutexes
2320Sstevel@tonic-gate  *
2330Sstevel@tonic-gate  * The lock ordering is to get the smap mutex to lock down the slot
2340Sstevel@tonic-gate  * first then the hash lock (for hash in/out (vp, off) list) or the
2350Sstevel@tonic-gate  * freelist lock to put the slot back on the free list.
2360Sstevel@tonic-gate  *
2370Sstevel@tonic-gate  * The hash search is done by only holding the hashchain lock, when a wanted
2380Sstevel@tonic-gate  * slot is found, we drop the hashchain lock then lock the slot so there
2390Sstevel@tonic-gate  * is no overlapping of hashchain and smap locks. After the slot is
2400Sstevel@tonic-gate  * locked, we verify again if the slot is still what we are looking
2410Sstevel@tonic-gate  * for.
2420Sstevel@tonic-gate  *
2430Sstevel@tonic-gate  * Allocation of a free slot is done by holding the freelist lock,
2440Sstevel@tonic-gate  * then locking the smap slot at the head of the freelist. This is
2450Sstevel@tonic-gate  * in reversed lock order so mutex_tryenter() is used.
2460Sstevel@tonic-gate  *
2470Sstevel@tonic-gate  * The smap lock protects all fields in smap structure except for
2480Sstevel@tonic-gate  * the link fields for hash/free lists which are protected by
2490Sstevel@tonic-gate  * hashchain and freelist locks.
2500Sstevel@tonic-gate  */
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate #define	SHASHMTX(hashid)	(&smd_hash[hashid].sh_mtx)
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate #define	SMP2SMF(smp)		(&smd_free[(smp - smd_smap) & smd_freemsk])
2550Sstevel@tonic-gate #define	SMP2SMF_NDX(smp)	(ushort_t)((smp - smd_smap) & smd_freemsk)
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate #define	SMAPMTX(smp) (&smp->sm_mtx)
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate #define	SMAP_HASHFUNC(vp, off, hashid) \
2600Sstevel@tonic-gate 	{ \
2610Sstevel@tonic-gate 	hashid = ((((uintptr_t)(vp) >> 6) + ((uintptr_t)(vp) >> 3) + \
2620Sstevel@tonic-gate 		((off) >> MAXBSHIFT)) & smd_hashmsk); \
2630Sstevel@tonic-gate 	}
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate /*
2660Sstevel@tonic-gate  * The most frequently updated kstat counters are kept in the
2670Sstevel@tonic-gate  * per cpu array to avoid hot cache blocks. The update function
2680Sstevel@tonic-gate  * sums the cpu local counters to update the global counters.
2690Sstevel@tonic-gate  */
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate /* ARGSUSED */
2720Sstevel@tonic-gate int
2730Sstevel@tonic-gate segmap_kstat_update(kstat_t *ksp, int rw)
2740Sstevel@tonic-gate {
2750Sstevel@tonic-gate 	int i;
2760Sstevel@tonic-gate 	ulong_t	getmap, release, get_reclaim;
2770Sstevel@tonic-gate 	ulong_t	fault, pagecreate, get_reuse;
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
2800Sstevel@tonic-gate 		return (EACCES);
2810Sstevel@tonic-gate 	getmap = release = get_reclaim = (ulong_t)0;
2820Sstevel@tonic-gate 	fault = pagecreate = get_reuse = (ulong_t)0;
2830Sstevel@tonic-gate 	for (i = 0; i < max_ncpus; i++) {
2840Sstevel@tonic-gate 		getmap += smd_cpu[i].scpu.scpu_getmap;
2850Sstevel@tonic-gate 		release  += smd_cpu[i].scpu.scpu_release;
2860Sstevel@tonic-gate 		get_reclaim += smd_cpu[i].scpu.scpu_get_reclaim;
2870Sstevel@tonic-gate 		fault  += smd_cpu[i].scpu.scpu_fault;
2880Sstevel@tonic-gate 		pagecreate  += smd_cpu[i].scpu.scpu_pagecreate;
2890Sstevel@tonic-gate 		get_reuse += smd_cpu[i].scpu.scpu_get_reuse;
2900Sstevel@tonic-gate 	}
2910Sstevel@tonic-gate 	segmapcnt.smp_getmap.value.ul = getmap;
2920Sstevel@tonic-gate 	segmapcnt.smp_release.value.ul = release;
2930Sstevel@tonic-gate 	segmapcnt.smp_get_reclaim.value.ul = get_reclaim;
2940Sstevel@tonic-gate 	segmapcnt.smp_fault.value.ul = fault;
2950Sstevel@tonic-gate 	segmapcnt.smp_pagecreate.value.ul = pagecreate;
2960Sstevel@tonic-gate 	segmapcnt.smp_get_reuse.value.ul = get_reuse;
2970Sstevel@tonic-gate 	return (0);
2980Sstevel@tonic-gate }
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate int
3010Sstevel@tonic-gate segmap_create(struct seg *seg, void *argsp)
3020Sstevel@tonic-gate {
3030Sstevel@tonic-gate 	struct segmap_data *smd;
3040Sstevel@tonic-gate 	struct smap *smp;
3050Sstevel@tonic-gate 	struct smfree *sm;
3060Sstevel@tonic-gate 	struct segmap_crargs *a = (struct segmap_crargs *)argsp;
3070Sstevel@tonic-gate 	struct smaphash *shashp;
3080Sstevel@tonic-gate 	union segmap_cpu *scpu;
3090Sstevel@tonic-gate 	long i, npages;
3100Sstevel@tonic-gate 	size_t hashsz;
3110Sstevel@tonic-gate 	uint_t nfreelist;
3120Sstevel@tonic-gate 	extern void prefetch_smap_w(void *);
3130Sstevel@tonic-gate 	extern int max_ncpus;
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	ASSERT(seg->s_as && RW_WRITE_HELD(&seg->s_as->a_lock));
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	if (((uintptr_t)seg->s_base | seg->s_size) & MAXBOFFSET) {
3180Sstevel@tonic-gate 		panic("segkmap not MAXBSIZE aligned");
3190Sstevel@tonic-gate 		/*NOTREACHED*/
3200Sstevel@tonic-gate 	}
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	smd = kmem_zalloc(sizeof (struct segmap_data), KM_SLEEP);
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	seg->s_data = (void *)smd;
3250Sstevel@tonic-gate 	seg->s_ops = &segmap_ops;
3260Sstevel@tonic-gate 	smd->smd_prot = a->prot;
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	/*
3290Sstevel@tonic-gate 	 * Scale the number of smap freelists to be
3300Sstevel@tonic-gate 	 * proportional to max_ncpus * number of virtual colors.
3310Sstevel@tonic-gate 	 * The caller can over-ride this scaling by providing
3320Sstevel@tonic-gate 	 * a non-zero a->nfreelist argument.
3330Sstevel@tonic-gate 	 */
3340Sstevel@tonic-gate 	nfreelist = a->nfreelist;
3350Sstevel@tonic-gate 	if (nfreelist == 0)
3360Sstevel@tonic-gate 		nfreelist = max_ncpus;
3370Sstevel@tonic-gate 	else if (nfreelist < 0 || nfreelist > 4 * max_ncpus) {
3380Sstevel@tonic-gate 		cmn_err(CE_WARN, "segmap_create: nfreelist out of range "
3390Sstevel@tonic-gate 		"%d, using %d", nfreelist, max_ncpus);
3400Sstevel@tonic-gate 		nfreelist = max_ncpus;
3410Sstevel@tonic-gate 	}
3420Sstevel@tonic-gate 	if (nfreelist & (nfreelist - 1)) {
3430Sstevel@tonic-gate 		/* round up nfreelist to the next power of two. */
3440Sstevel@tonic-gate 		nfreelist = 1 << (highbit(nfreelist));
3450Sstevel@tonic-gate 	}
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	/*
3480Sstevel@tonic-gate 	 * Get the number of virtual colors - must be a power of 2.
3490Sstevel@tonic-gate 	 */
3500Sstevel@tonic-gate 	if (a->shmsize)
3510Sstevel@tonic-gate 		smd_ncolor = a->shmsize >> MAXBSHIFT;
3520Sstevel@tonic-gate 	else
3530Sstevel@tonic-gate 		smd_ncolor = 1;
3540Sstevel@tonic-gate 	ASSERT((smd_ncolor & (smd_ncolor - 1)) == 0);
3550Sstevel@tonic-gate 	ASSERT(smd_ncolor <= SEGMAP_MAXCOLOR);
3560Sstevel@tonic-gate 	smd_colormsk = smd_ncolor - 1;
3570Sstevel@tonic-gate 	smd->smd_nfree = smd_nfree = smd_ncolor * nfreelist;
3580Sstevel@tonic-gate 	smd_freemsk = smd_nfree - 1;
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	/*
3610Sstevel@tonic-gate 	 * Allocate and initialize the freelist headers.
3620Sstevel@tonic-gate 	 * Note that sm_freeq[1] starts out as the release queue. This
3630Sstevel@tonic-gate 	 * is known when the smap structures are initialized below.
3640Sstevel@tonic-gate 	 */
3650Sstevel@tonic-gate 	smd_free = smd->smd_free =
3660Sstevel@tonic-gate 	    kmem_zalloc(smd_nfree * sizeof (struct smfree), KM_SLEEP);
3670Sstevel@tonic-gate 	for (i = 0; i < smd_nfree; i++) {
3680Sstevel@tonic-gate 		sm = &smd->smd_free[i];
3690Sstevel@tonic-gate 		mutex_init(&sm->sm_freeq[0].smq_mtx, NULL, MUTEX_DEFAULT, NULL);
3700Sstevel@tonic-gate 		mutex_init(&sm->sm_freeq[1].smq_mtx, NULL, MUTEX_DEFAULT, NULL);
3710Sstevel@tonic-gate 		sm->sm_allocq = &sm->sm_freeq[0];
3720Sstevel@tonic-gate 		sm->sm_releq = &sm->sm_freeq[1];
3730Sstevel@tonic-gate 	}
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	/*
3760Sstevel@tonic-gate 	 * Allocate and initialize the smap hash chain headers.
3770Sstevel@tonic-gate 	 * Compute hash size rounding down to the next power of two.
3780Sstevel@tonic-gate 	 */
3790Sstevel@tonic-gate 	npages = MAP_PAGES(seg);
3800Sstevel@tonic-gate 	smd->smd_npages = npages;
3810Sstevel@tonic-gate 	hashsz = npages / SMAP_HASHAVELEN;
3820Sstevel@tonic-gate 	hashsz = 1 << (highbit(hashsz)-1);
3830Sstevel@tonic-gate 	smd_hashmsk = hashsz - 1;
3840Sstevel@tonic-gate 	smd_hash = smd->smd_hash =
3850Sstevel@tonic-gate 	    kmem_alloc(hashsz * sizeof (struct smaphash), KM_SLEEP);
3860Sstevel@tonic-gate #ifdef SEGMAP_HASHSTATS
3870Sstevel@tonic-gate 	smd_hash_len =
3880Sstevel@tonic-gate 	    kmem_zalloc(hashsz * sizeof (unsigned int), KM_SLEEP);
3890Sstevel@tonic-gate #endif
3900Sstevel@tonic-gate 	for (i = 0, shashp = smd_hash; i < hashsz; i++, shashp++) {
3910Sstevel@tonic-gate 		shashp->sh_hash_list = NULL;
3920Sstevel@tonic-gate 		mutex_init(&shashp->sh_mtx, NULL, MUTEX_DEFAULT, NULL);
3930Sstevel@tonic-gate 	}
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	/*
3960Sstevel@tonic-gate 	 * Allocate and initialize the smap structures.
3970Sstevel@tonic-gate 	 * Link all slots onto the appropriate freelist.
3980Sstevel@tonic-gate 	 * The smap array is large enough to affect boot time
3990Sstevel@tonic-gate 	 * on large systems, so use memory prefetching and only
4000Sstevel@tonic-gate 	 * go through the array 1 time. Inline a optimized version
4010Sstevel@tonic-gate 	 * of segmap_smapadd to add structures to freelists with
4020Sstevel@tonic-gate 	 * knowledge that no locks are needed here.
4030Sstevel@tonic-gate 	 */
4040Sstevel@tonic-gate 	smd_smap = smd->smd_sm =
4050Sstevel@tonic-gate 		kmem_alloc(sizeof (struct smap) * npages, KM_SLEEP);
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	for (smp = &smd->smd_sm[MAP_PAGES(seg) - 1];
4080Sstevel@tonic-gate 	    smp >= smd->smd_sm; smp--) {
4090Sstevel@tonic-gate 		struct smap *smpfreelist;
4100Sstevel@tonic-gate 		struct sm_freeq *releq;
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 		prefetch_smap_w((char *)smp);
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 		smp->sm_vp = NULL;
4150Sstevel@tonic-gate 		smp->sm_hash = NULL;
4160Sstevel@tonic-gate 		smp->sm_off = 0;
4170Sstevel@tonic-gate 		smp->sm_bitmap = 0;
4180Sstevel@tonic-gate 		smp->sm_refcnt = 0;
4190Sstevel@tonic-gate 		mutex_init(&smp->sm_mtx, NULL, MUTEX_DEFAULT, NULL);
4200Sstevel@tonic-gate 		smp->sm_free_ndx = SMP2SMF_NDX(smp);
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 		sm = SMP2SMF(smp);
4230Sstevel@tonic-gate 		releq = sm->sm_releq;
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 		smpfreelist = releq->smq_free;
4260Sstevel@tonic-gate 		if (smpfreelist == 0) {
4270Sstevel@tonic-gate 			releq->smq_free = smp->sm_next = smp->sm_prev = smp;
4280Sstevel@tonic-gate 		} else {
4290Sstevel@tonic-gate 			smp->sm_next = smpfreelist;
4300Sstevel@tonic-gate 			smp->sm_prev = smpfreelist->sm_prev;
4310Sstevel@tonic-gate 			smpfreelist->sm_prev = smp;
4320Sstevel@tonic-gate 			smp->sm_prev->sm_next = smp;
4330Sstevel@tonic-gate 			releq->smq_free = smp->sm_next;
4340Sstevel@tonic-gate 		}
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 		/*
4370Sstevel@tonic-gate 		 * sm_flag = 0 (no SM_QNDX_ZERO) implies smap on sm_freeq[1]
4380Sstevel@tonic-gate 		 */
4390Sstevel@tonic-gate 		smp->sm_flags = 0;
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate #ifdef	SEGKPM_SUPPORT
4420Sstevel@tonic-gate 		/*
4430Sstevel@tonic-gate 		 * Due to the fragile prefetch loop no
4440Sstevel@tonic-gate 		 * separate function is used here.
4450Sstevel@tonic-gate 		 */
4460Sstevel@tonic-gate 		smp->sm_kpme_next = NULL;
4470Sstevel@tonic-gate 		smp->sm_kpme_prev = NULL;
4480Sstevel@tonic-gate 		smp->sm_kpme_page = NULL;
4490Sstevel@tonic-gate #endif
4500Sstevel@tonic-gate 	}
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	/*
4530Sstevel@tonic-gate 	 * Allocate the per color indices that distribute allocation
4540Sstevel@tonic-gate 	 * requests over the free lists. Each cpu will have a private
4550Sstevel@tonic-gate 	 * rotor index to spread the allocations even across the available
4560Sstevel@tonic-gate 	 * smap freelists. Init the scpu_last_smap field to the first
4570Sstevel@tonic-gate 	 * smap element so there is no need to check for NULL.
4580Sstevel@tonic-gate 	 */
4590Sstevel@tonic-gate 	smd_cpu =
4600Sstevel@tonic-gate 		kmem_zalloc(sizeof (union segmap_cpu) * max_ncpus, KM_SLEEP);
4610Sstevel@tonic-gate 	for (i = 0, scpu = smd_cpu; i < max_ncpus; i++, scpu++) {
4620Sstevel@tonic-gate 		int j;
4630Sstevel@tonic-gate 		for (j = 0; j < smd_ncolor; j++)
4640Sstevel@tonic-gate 			scpu->scpu.scpu_free_ndx[j] = j;
4650Sstevel@tonic-gate 		scpu->scpu.scpu_last_smap = smd_smap;
4660Sstevel@tonic-gate 	}
4670Sstevel@tonic-gate 
468*1841Spraks 	if (vpm_enable) {
469*1841Spraks 		vpm_init();
470*1841Spraks 	}
471*1841Spraks 
4720Sstevel@tonic-gate #ifdef DEBUG
4730Sstevel@tonic-gate 	/*
4740Sstevel@tonic-gate 	 * Keep track of which colors are used more often.
4750Sstevel@tonic-gate 	 */
4760Sstevel@tonic-gate 	colors_used = kmem_zalloc(smd_nfree * sizeof (int), KM_SLEEP);
4770Sstevel@tonic-gate #endif /* DEBUG */
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	return (0);
4800Sstevel@tonic-gate }
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate static void
4830Sstevel@tonic-gate segmap_free(seg)
4840Sstevel@tonic-gate 	struct seg *seg;
4850Sstevel@tonic-gate {
4860Sstevel@tonic-gate 	ASSERT(seg->s_as && RW_WRITE_HELD(&seg->s_as->a_lock));
4870Sstevel@tonic-gate }
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate /*
4900Sstevel@tonic-gate  * Do a F_SOFTUNLOCK call over the range requested.
4910Sstevel@tonic-gate  * The range must have already been F_SOFTLOCK'ed.
4920Sstevel@tonic-gate  */
4930Sstevel@tonic-gate static void
4940Sstevel@tonic-gate segmap_unlock(
4950Sstevel@tonic-gate 	struct hat *hat,
4960Sstevel@tonic-gate 	struct seg *seg,
4970Sstevel@tonic-gate 	caddr_t addr,
4980Sstevel@tonic-gate 	size_t len,
4990Sstevel@tonic-gate 	enum seg_rw rw,
5000Sstevel@tonic-gate 	struct smap *smp)
5010Sstevel@tonic-gate {
5020Sstevel@tonic-gate 	page_t *pp;
5030Sstevel@tonic-gate 	caddr_t adr;
5040Sstevel@tonic-gate 	u_offset_t off;
5050Sstevel@tonic-gate 	struct vnode *vp;
5060Sstevel@tonic-gate 	kmutex_t *smtx;
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	ASSERT(smp->sm_refcnt > 0);
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate #ifdef lint
5110Sstevel@tonic-gate 	seg = seg;
5120Sstevel@tonic-gate #endif
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 	if (segmap_kpm && IS_KPM_ADDR(addr)) {
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 		/*
5170Sstevel@tonic-gate 		 * We're called only from segmap_fault and this was a
5180Sstevel@tonic-gate 		 * NOP in case of a kpm based smap, so dangerous things
5190Sstevel@tonic-gate 		 * must have happened in the meantime. Pages are prefaulted
5200Sstevel@tonic-gate 		 * and locked in segmap_getmapflt and they will not be
5210Sstevel@tonic-gate 		 * unlocked until segmap_release.
5220Sstevel@tonic-gate 		 */
5230Sstevel@tonic-gate 		panic("segmap_unlock: called with kpm addr %p", (void *)addr);
5240Sstevel@tonic-gate 		/*NOTREACHED*/
5250Sstevel@tonic-gate 	}
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	vp = smp->sm_vp;
5280Sstevel@tonic-gate 	off = smp->sm_off + (u_offset_t)((uintptr_t)addr & MAXBOFFSET);
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 	hat_unlock(hat, addr, P2ROUNDUP(len, PAGESIZE));
5310Sstevel@tonic-gate 	for (adr = addr; adr < addr + len; adr += PAGESIZE, off += PAGESIZE) {
5320Sstevel@tonic-gate 		ushort_t bitmask;
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 		/*
5350Sstevel@tonic-gate 		 * Use page_find() instead of page_lookup() to
5360Sstevel@tonic-gate 		 * find the page since we know that it has
5370Sstevel@tonic-gate 		 * "shared" lock.
5380Sstevel@tonic-gate 		 */
5390Sstevel@tonic-gate 		pp = page_find(vp, off);
5400Sstevel@tonic-gate 		if (pp == NULL) {
5410Sstevel@tonic-gate 			panic("segmap_unlock: page not found");
5420Sstevel@tonic-gate 			/*NOTREACHED*/
5430Sstevel@tonic-gate 		}
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 		if (rw == S_WRITE) {
5460Sstevel@tonic-gate 			hat_setrefmod(pp);
5470Sstevel@tonic-gate 		} else if (rw != S_OTHER) {
5480Sstevel@tonic-gate 			TRACE_3(TR_FAC_VM, TR_SEGMAP_FAULT,
5490Sstevel@tonic-gate 				"segmap_fault:pp %p vp %p offset %llx",
5500Sstevel@tonic-gate 				pp, vp, off);
5510Sstevel@tonic-gate 			hat_setref(pp);
5520Sstevel@tonic-gate 		}
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 		/*
5550Sstevel@tonic-gate 		 * Clear bitmap, if the bit corresponding to "off" is set,
5560Sstevel@tonic-gate 		 * since the page and translation are being unlocked.
5570Sstevel@tonic-gate 		 */
5580Sstevel@tonic-gate 		bitmask = SMAP_BIT_MASK((off - smp->sm_off) >> PAGESHIFT);
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 		/*
5610Sstevel@tonic-gate 		 * Large Files: Following assertion is to verify
5620Sstevel@tonic-gate 		 * the correctness of the cast to (int) above.
5630Sstevel@tonic-gate 		 */
5640Sstevel@tonic-gate 		ASSERT((u_offset_t)(off - smp->sm_off) <= INT_MAX);
5650Sstevel@tonic-gate 		smtx = SMAPMTX(smp);
5660Sstevel@tonic-gate 		mutex_enter(smtx);
5670Sstevel@tonic-gate 		if (smp->sm_bitmap & bitmask) {
5680Sstevel@tonic-gate 			smp->sm_bitmap &= ~bitmask;
5690Sstevel@tonic-gate 		}
5700Sstevel@tonic-gate 		mutex_exit(smtx);
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 		page_unlock(pp);
5730Sstevel@tonic-gate 	}
5740Sstevel@tonic-gate }
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate #define	MAXPPB	(MAXBSIZE/4096)	/* assumes minimum page size of 4k */
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate /*
5790Sstevel@tonic-gate  * This routine is called via a machine specific fault handling
5800Sstevel@tonic-gate  * routine.  It is also called by software routines wishing to
5810Sstevel@tonic-gate  * lock or unlock a range of addresses.
5820Sstevel@tonic-gate  *
5830Sstevel@tonic-gate  * Note that this routine expects a page-aligned "addr".
5840Sstevel@tonic-gate  */
5850Sstevel@tonic-gate faultcode_t
5860Sstevel@tonic-gate segmap_fault(
5870Sstevel@tonic-gate 	struct hat *hat,
5880Sstevel@tonic-gate 	struct seg *seg,
5890Sstevel@tonic-gate 	caddr_t addr,
5900Sstevel@tonic-gate 	size_t len,
5910Sstevel@tonic-gate 	enum fault_type type,
5920Sstevel@tonic-gate 	enum seg_rw rw)
5930Sstevel@tonic-gate {
5940Sstevel@tonic-gate 	struct segmap_data *smd = (struct segmap_data *)seg->s_data;
5950Sstevel@tonic-gate 	struct smap *smp;
5960Sstevel@tonic-gate 	page_t *pp, **ppp;
5970Sstevel@tonic-gate 	struct vnode *vp;
5980Sstevel@tonic-gate 	u_offset_t off;
5990Sstevel@tonic-gate 	page_t *pl[MAXPPB + 1];
6000Sstevel@tonic-gate 	uint_t prot;
6010Sstevel@tonic-gate 	u_offset_t addroff;
6020Sstevel@tonic-gate 	caddr_t adr;
6030Sstevel@tonic-gate 	int err;
6040Sstevel@tonic-gate 	u_offset_t sm_off;
6050Sstevel@tonic-gate 	int hat_flag;
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	if (segmap_kpm && IS_KPM_ADDR(addr)) {
6080Sstevel@tonic-gate 		int newpage;
6090Sstevel@tonic-gate 		kmutex_t *smtx;
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 		/*
6120Sstevel@tonic-gate 		 * Pages are successfully prefaulted and locked in
6130Sstevel@tonic-gate 		 * segmap_getmapflt and can't be unlocked until
6140Sstevel@tonic-gate 		 * segmap_release. No hat mappings have to be locked
6150Sstevel@tonic-gate 		 * and they also can't be unlocked as long as the
6160Sstevel@tonic-gate 		 * caller owns an active kpm addr.
6170Sstevel@tonic-gate 		 */
6180Sstevel@tonic-gate #ifndef DEBUG
6190Sstevel@tonic-gate 		if (type != F_SOFTUNLOCK)
6200Sstevel@tonic-gate 			return (0);
6210Sstevel@tonic-gate #endif
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 		if ((smp = get_smap_kpm(addr, NULL)) == NULL) {
6240Sstevel@tonic-gate 			panic("segmap_fault: smap not found "
6250Sstevel@tonic-gate 			    "for addr %p", (void *)addr);
6260Sstevel@tonic-gate 			/*NOTREACHED*/
6270Sstevel@tonic-gate 		}
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 		smtx = SMAPMTX(smp);
6300Sstevel@tonic-gate #ifdef	DEBUG
6310Sstevel@tonic-gate 		newpage = smp->sm_flags & SM_KPM_NEWPAGE;
6320Sstevel@tonic-gate 		if (newpage) {
6330Sstevel@tonic-gate 			cmn_err(CE_WARN, "segmap_fault: newpage? smp %p",
6340Sstevel@tonic-gate 				(void *)smp);
6350Sstevel@tonic-gate 		}
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 		if (type != F_SOFTUNLOCK) {
6380Sstevel@tonic-gate 			mutex_exit(smtx);
6390Sstevel@tonic-gate 			return (0);
6400Sstevel@tonic-gate 		}
6410Sstevel@tonic-gate #endif
6420Sstevel@tonic-gate 		mutex_exit(smtx);
6430Sstevel@tonic-gate 		vp = smp->sm_vp;
6440Sstevel@tonic-gate 		sm_off = smp->sm_off;
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 		if (vp == NULL)
6470Sstevel@tonic-gate 			return (FC_MAKE_ERR(EIO));
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 		ASSERT(smp->sm_refcnt > 0);
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 		addroff = (u_offset_t)((uintptr_t)addr & MAXBOFFSET);
6520Sstevel@tonic-gate 		if (addroff + len > MAXBSIZE)
6530Sstevel@tonic-gate 			panic("segmap_fault: endaddr %p exceeds MAXBSIZE chunk",
6540Sstevel@tonic-gate 			    (void *)(addr + len));
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 		off = sm_off + addroff;
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 		pp = page_find(vp, off);
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 		if (pp == NULL)
6610Sstevel@tonic-gate 			panic("segmap_fault: softunlock page not found");
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate 		/*
6640Sstevel@tonic-gate 		 * Set ref bit also here in case of S_OTHER to avoid the
6650Sstevel@tonic-gate 		 * overhead of supporting other cases than F_SOFTUNLOCK
6660Sstevel@tonic-gate 		 * with segkpm. We can do this because the underlying
6670Sstevel@tonic-gate 		 * pages are locked anyway.
6680Sstevel@tonic-gate 		 */
6690Sstevel@tonic-gate 		if (rw == S_WRITE) {
6700Sstevel@tonic-gate 			hat_setrefmod(pp);
6710Sstevel@tonic-gate 		} else {
6720Sstevel@tonic-gate 			TRACE_3(TR_FAC_VM, TR_SEGMAP_FAULT,
6730Sstevel@tonic-gate 				"segmap_fault:pp %p vp %p offset %llx",
6740Sstevel@tonic-gate 				pp, vp, off);
6750Sstevel@tonic-gate 			hat_setref(pp);
6760Sstevel@tonic-gate 		}
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 		return (0);
6790Sstevel@tonic-gate 	}
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	smd_cpu[CPU->cpu_seqid].scpu.scpu_fault++;
6820Sstevel@tonic-gate 	smp = GET_SMAP(seg, addr);
6830Sstevel@tonic-gate 	vp = smp->sm_vp;
6840Sstevel@tonic-gate 	sm_off = smp->sm_off;
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 	if (vp == NULL)
6870Sstevel@tonic-gate 		return (FC_MAKE_ERR(EIO));
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	ASSERT(smp->sm_refcnt > 0);
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 	addroff = (u_offset_t)((uintptr_t)addr & MAXBOFFSET);
6920Sstevel@tonic-gate 	if (addroff + len > MAXBSIZE) {
6930Sstevel@tonic-gate 		panic("segmap_fault: endaddr %p "
6940Sstevel@tonic-gate 		    "exceeds MAXBSIZE chunk", (void *)(addr + len));
6950Sstevel@tonic-gate 		/*NOTREACHED*/
6960Sstevel@tonic-gate 	}
6970Sstevel@tonic-gate 	off = sm_off + addroff;
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 	/*
7000Sstevel@tonic-gate 	 * First handle the easy stuff
7010Sstevel@tonic-gate 	 */
7020Sstevel@tonic-gate 	if (type == F_SOFTUNLOCK) {
7030Sstevel@tonic-gate 		segmap_unlock(hat, seg, addr, len, rw, smp);
7040Sstevel@tonic-gate 		return (0);
7050Sstevel@tonic-gate 	}
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 	TRACE_3(TR_FAC_VM, TR_SEGMAP_GETPAGE,
7080Sstevel@tonic-gate 		"segmap_getpage:seg %p addr %p vp %p", seg, addr, vp);
7090Sstevel@tonic-gate 	err = VOP_GETPAGE(vp, (offset_t)off, len, &prot, pl, MAXBSIZE,
7100Sstevel@tonic-gate 	    seg, addr, rw, CRED());
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 	if (err)
7130Sstevel@tonic-gate 		return (FC_MAKE_ERR(err));
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 	prot &= smd->smd_prot;
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 	/*
7180Sstevel@tonic-gate 	 * Handle all pages returned in the pl[] array.
7190Sstevel@tonic-gate 	 * This loop is coded on the assumption that if
7200Sstevel@tonic-gate 	 * there was no error from the VOP_GETPAGE routine,
7210Sstevel@tonic-gate 	 * that the page list returned will contain all the
7220Sstevel@tonic-gate 	 * needed pages for the vp from [off..off + len].
7230Sstevel@tonic-gate 	 */
7240Sstevel@tonic-gate 	ppp = pl;
7250Sstevel@tonic-gate 	while ((pp = *ppp++) != NULL) {
7260Sstevel@tonic-gate 		u_offset_t poff;
7270Sstevel@tonic-gate 		ASSERT(pp->p_vnode == vp);
7280Sstevel@tonic-gate 		hat_flag = HAT_LOAD;
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 		/*
7310Sstevel@tonic-gate 		 * Verify that the pages returned are within the range
7320Sstevel@tonic-gate 		 * of this segmap region.  Note that it is theoretically
7330Sstevel@tonic-gate 		 * possible for pages outside this range to be returned,
7340Sstevel@tonic-gate 		 * but it is not very likely.  If we cannot use the
7350Sstevel@tonic-gate 		 * page here, just release it and go on to the next one.
7360Sstevel@tonic-gate 		 */
7370Sstevel@tonic-gate 		if (pp->p_offset < sm_off ||
7380Sstevel@tonic-gate 		    pp->p_offset >= sm_off + MAXBSIZE) {
7390Sstevel@tonic-gate 			(void) page_release(pp, 1);
7400Sstevel@tonic-gate 			continue;
7410Sstevel@tonic-gate 		}
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 		ASSERT(hat == kas.a_hat);
7440Sstevel@tonic-gate 		poff = pp->p_offset;
7450Sstevel@tonic-gate 		adr = addr + (poff - off);
7460Sstevel@tonic-gate 		if (adr >= addr && adr < addr + len) {
7470Sstevel@tonic-gate 			hat_setref(pp);
7480Sstevel@tonic-gate 			TRACE_3(TR_FAC_VM, TR_SEGMAP_FAULT,
7490Sstevel@tonic-gate 			    "segmap_fault:pp %p vp %p offset %llx",
7500Sstevel@tonic-gate 			    pp, vp, poff);
7510Sstevel@tonic-gate 			if (type == F_SOFTLOCK)
7520Sstevel@tonic-gate 				hat_flag = HAT_LOAD_LOCK;
7530Sstevel@tonic-gate 		}
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 		/*
7560Sstevel@tonic-gate 		 * Deal with VMODSORT pages here. If we know this is a write
7570Sstevel@tonic-gate 		 * do the setmod now and allow write protection.
7580Sstevel@tonic-gate 		 * As long as it's modified or not S_OTHER, remove write
7590Sstevel@tonic-gate 		 * protection. With S_OTHER it's up to the FS to deal with this.
7600Sstevel@tonic-gate 		 */
7610Sstevel@tonic-gate 		if (IS_VMODSORT(vp)) {
7620Sstevel@tonic-gate 			if (rw == S_WRITE)
7630Sstevel@tonic-gate 				hat_setmod(pp);
7640Sstevel@tonic-gate 			else if (rw != S_OTHER && !hat_ismod(pp))
7650Sstevel@tonic-gate 				prot &= ~PROT_WRITE;
7660Sstevel@tonic-gate 		}
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 		hat_memload(hat, adr, pp, prot, hat_flag);
7690Sstevel@tonic-gate 		if (hat_flag != HAT_LOAD_LOCK)
7700Sstevel@tonic-gate 			page_unlock(pp);
7710Sstevel@tonic-gate 	}
7720Sstevel@tonic-gate 	return (0);
7730Sstevel@tonic-gate }
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate /*
7760Sstevel@tonic-gate  * This routine is used to start I/O on pages asynchronously.
7770Sstevel@tonic-gate  */
7780Sstevel@tonic-gate static faultcode_t
7790Sstevel@tonic-gate segmap_faulta(struct seg *seg, caddr_t addr)
7800Sstevel@tonic-gate {
7810Sstevel@tonic-gate 	struct smap *smp;
7820Sstevel@tonic-gate 	struct vnode *vp;
7830Sstevel@tonic-gate 	u_offset_t off;
7840Sstevel@tonic-gate 	int err;
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 	if (segmap_kpm && IS_KPM_ADDR(addr)) {
7870Sstevel@tonic-gate 		int	newpage;
7880Sstevel@tonic-gate 		kmutex_t *smtx;
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 		/*
7910Sstevel@tonic-gate 		 * Pages are successfully prefaulted and locked in
7920Sstevel@tonic-gate 		 * segmap_getmapflt and can't be unlocked until
7930Sstevel@tonic-gate 		 * segmap_release. No hat mappings have to be locked
7940Sstevel@tonic-gate 		 * and they also can't be unlocked as long as the
7950Sstevel@tonic-gate 		 * caller owns an active kpm addr.
7960Sstevel@tonic-gate 		 */
7970Sstevel@tonic-gate #ifdef	DEBUG
7980Sstevel@tonic-gate 		if ((smp = get_smap_kpm(addr, NULL)) == NULL) {
7990Sstevel@tonic-gate 			panic("segmap_faulta: smap not found "
8000Sstevel@tonic-gate 			    "for addr %p", (void *)addr);
8010Sstevel@tonic-gate 			/*NOTREACHED*/
8020Sstevel@tonic-gate 		}
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 		smtx = SMAPMTX(smp);
8050Sstevel@tonic-gate 		newpage = smp->sm_flags & SM_KPM_NEWPAGE;
8060Sstevel@tonic-gate 		mutex_exit(smtx);
8070Sstevel@tonic-gate 		if (newpage)
8080Sstevel@tonic-gate 			cmn_err(CE_WARN, "segmap_faulta: newpage? smp %p",
8090Sstevel@tonic-gate 			    (void *)smp);
8100Sstevel@tonic-gate #endif
8110Sstevel@tonic-gate 		return (0);
8120Sstevel@tonic-gate 	}
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 	segmapcnt.smp_faulta.value.ul++;
8150Sstevel@tonic-gate 	smp = GET_SMAP(seg, addr);
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	ASSERT(smp->sm_refcnt > 0);
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 	vp = smp->sm_vp;
8200Sstevel@tonic-gate 	off = smp->sm_off;
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	if (vp == NULL) {
8230Sstevel@tonic-gate 		cmn_err(CE_WARN, "segmap_faulta - no vp");
8240Sstevel@tonic-gate 		return (FC_MAKE_ERR(EIO));
8250Sstevel@tonic-gate 	}
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 	TRACE_3(TR_FAC_VM, TR_SEGMAP_GETPAGE,
8280Sstevel@tonic-gate 		"segmap_getpage:seg %p addr %p vp %p", seg, addr, vp);
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 	err = VOP_GETPAGE(vp, (offset_t)(off + ((offset_t)((uintptr_t)addr
8310Sstevel@tonic-gate 	    & MAXBOFFSET))), PAGESIZE, (uint_t *)NULL, (page_t **)NULL, 0,
8320Sstevel@tonic-gate 	    seg, addr, S_READ, CRED());
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 	if (err)
8350Sstevel@tonic-gate 		return (FC_MAKE_ERR(err));
8360Sstevel@tonic-gate 	return (0);
8370Sstevel@tonic-gate }
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate /*ARGSUSED*/
8400Sstevel@tonic-gate static int
8410Sstevel@tonic-gate segmap_checkprot(struct seg *seg, caddr_t addr, size_t len, uint_t prot)
8420Sstevel@tonic-gate {
8430Sstevel@tonic-gate 	struct segmap_data *smd = (struct segmap_data *)seg->s_data;
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 	ASSERT(seg->s_as && RW_LOCK_HELD(&seg->s_as->a_lock));
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate 	/*
8480Sstevel@tonic-gate 	 * Need not acquire the segment lock since
8490Sstevel@tonic-gate 	 * "smd_prot" is a read-only field.
8500Sstevel@tonic-gate 	 */
8510Sstevel@tonic-gate 	return (((smd->smd_prot & prot) != prot) ? EACCES : 0);
8520Sstevel@tonic-gate }
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate static int
8550Sstevel@tonic-gate segmap_getprot(struct seg *seg, caddr_t addr, size_t len, uint_t *protv)
8560Sstevel@tonic-gate {
8570Sstevel@tonic-gate 	struct segmap_data *smd = (struct segmap_data *)seg->s_data;
8580Sstevel@tonic-gate 	size_t pgno = seg_page(seg, addr + len) - seg_page(seg, addr) + 1;
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 	if (pgno != 0) {
8630Sstevel@tonic-gate 		do
8640Sstevel@tonic-gate 			protv[--pgno] = smd->smd_prot;
8650Sstevel@tonic-gate 		while (pgno != 0);
8660Sstevel@tonic-gate 	}
8670Sstevel@tonic-gate 	return (0);
8680Sstevel@tonic-gate }
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate static u_offset_t
8710Sstevel@tonic-gate segmap_getoffset(struct seg *seg, caddr_t addr)
8720Sstevel@tonic-gate {
8730Sstevel@tonic-gate 	struct segmap_data *smd = (struct segmap_data *)seg->s_data;
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 	ASSERT(seg->s_as && RW_READ_HELD(&seg->s_as->a_lock));
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 	return ((u_offset_t)smd->smd_sm->sm_off + (addr - seg->s_base));
8780Sstevel@tonic-gate }
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate /*ARGSUSED*/
8810Sstevel@tonic-gate static int
8820Sstevel@tonic-gate segmap_gettype(struct seg *seg, caddr_t addr)
8830Sstevel@tonic-gate {
8840Sstevel@tonic-gate 	ASSERT(seg->s_as && RW_READ_HELD(&seg->s_as->a_lock));
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 	return (MAP_SHARED);
8870Sstevel@tonic-gate }
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate /*ARGSUSED*/
8900Sstevel@tonic-gate static int
8910Sstevel@tonic-gate segmap_getvp(struct seg *seg, caddr_t addr, struct vnode **vpp)
8920Sstevel@tonic-gate {
8930Sstevel@tonic-gate 	struct segmap_data *smd = (struct segmap_data *)seg->s_data;
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate 	ASSERT(seg->s_as && RW_READ_HELD(&seg->s_as->a_lock));
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 	/* XXX - This doesn't make any sense */
8980Sstevel@tonic-gate 	*vpp = smd->smd_sm->sm_vp;
8990Sstevel@tonic-gate 	return (0);
9000Sstevel@tonic-gate }
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate /*
9030Sstevel@tonic-gate  * Check to see if it makes sense to do kluster/read ahead to
9040Sstevel@tonic-gate  * addr + delta relative to the mapping at addr.  We assume here
9050Sstevel@tonic-gate  * that delta is a signed PAGESIZE'd multiple (which can be negative).
9060Sstevel@tonic-gate  *
9070Sstevel@tonic-gate  * For segmap we always "approve" of this action from our standpoint.
9080Sstevel@tonic-gate  */
9090Sstevel@tonic-gate /*ARGSUSED*/
9100Sstevel@tonic-gate static int
9110Sstevel@tonic-gate segmap_kluster(struct seg *seg, caddr_t addr, ssize_t delta)
9120Sstevel@tonic-gate {
9130Sstevel@tonic-gate 	return (0);
9140Sstevel@tonic-gate }
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate static void
9170Sstevel@tonic-gate segmap_badop()
9180Sstevel@tonic-gate {
9190Sstevel@tonic-gate 	panic("segmap_badop");
9200Sstevel@tonic-gate 	/*NOTREACHED*/
9210Sstevel@tonic-gate }
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate /*
9240Sstevel@tonic-gate  * Special private segmap operations
9250Sstevel@tonic-gate  */
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate /*
9280Sstevel@tonic-gate  * Add smap to the appropriate free list.
9290Sstevel@tonic-gate  */
9300Sstevel@tonic-gate static void
9310Sstevel@tonic-gate segmap_smapadd(struct smap *smp)
9320Sstevel@tonic-gate {
9330Sstevel@tonic-gate 	struct smfree *sm;
9340Sstevel@tonic-gate 	struct smap *smpfreelist;
9350Sstevel@tonic-gate 	struct sm_freeq *releq;
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(SMAPMTX(smp)));
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate 	if (smp->sm_refcnt != 0) {
9400Sstevel@tonic-gate 		panic("segmap_smapadd");
9410Sstevel@tonic-gate 		/*NOTREACHED*/
9420Sstevel@tonic-gate 	}
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 	sm = &smd_free[smp->sm_free_ndx];
9450Sstevel@tonic-gate 	/*
9460Sstevel@tonic-gate 	 * Add to the tail of the release queue
9470Sstevel@tonic-gate 	 * Note that sm_releq and sm_allocq could toggle
9480Sstevel@tonic-gate 	 * before we get the lock. This does not affect
9490Sstevel@tonic-gate 	 * correctness as the 2 queues are only maintained
9500Sstevel@tonic-gate 	 * to reduce lock pressure.
9510Sstevel@tonic-gate 	 */
9520Sstevel@tonic-gate 	releq = sm->sm_releq;
9530Sstevel@tonic-gate 	if (releq == &sm->sm_freeq[0])
9540Sstevel@tonic-gate 		smp->sm_flags |= SM_QNDX_ZERO;
9550Sstevel@tonic-gate 	else
9560Sstevel@tonic-gate 		smp->sm_flags &= ~SM_QNDX_ZERO;
9570Sstevel@tonic-gate 	mutex_enter(&releq->smq_mtx);
9580Sstevel@tonic-gate 	smpfreelist = releq->smq_free;
9590Sstevel@tonic-gate 	if (smpfreelist == 0) {
9600Sstevel@tonic-gate 		int want;
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 		releq->smq_free = smp->sm_next = smp->sm_prev = smp;
9630Sstevel@tonic-gate 		/*
9640Sstevel@tonic-gate 		 * Both queue mutexes held to set sm_want;
9650Sstevel@tonic-gate 		 * snapshot the value before dropping releq mutex.
9660Sstevel@tonic-gate 		 * If sm_want appears after the releq mutex is dropped,
9670Sstevel@tonic-gate 		 * then the smap just freed is already gone.
9680Sstevel@tonic-gate 		 */
9690Sstevel@tonic-gate 		want = sm->sm_want;
9700Sstevel@tonic-gate 		mutex_exit(&releq->smq_mtx);
9710Sstevel@tonic-gate 		/*
9720Sstevel@tonic-gate 		 * See if there was a waiter before dropping the releq mutex
9730Sstevel@tonic-gate 		 * then recheck after obtaining sm_freeq[0] mutex as
9740Sstevel@tonic-gate 		 * the another thread may have already signaled.
9750Sstevel@tonic-gate 		 */
9760Sstevel@tonic-gate 		if (want) {
9770Sstevel@tonic-gate 			mutex_enter(&sm->sm_freeq[0].smq_mtx);
9780Sstevel@tonic-gate 			if (sm->sm_want)
9790Sstevel@tonic-gate 				cv_signal(&sm->sm_free_cv);
9800Sstevel@tonic-gate 			mutex_exit(&sm->sm_freeq[0].smq_mtx);
9810Sstevel@tonic-gate 		}
9820Sstevel@tonic-gate 	} else {
9830Sstevel@tonic-gate 		smp->sm_next = smpfreelist;
9840Sstevel@tonic-gate 		smp->sm_prev = smpfreelist->sm_prev;
9850Sstevel@tonic-gate 		smpfreelist->sm_prev = smp;
9860Sstevel@tonic-gate 		smp->sm_prev->sm_next = smp;
9870Sstevel@tonic-gate 		mutex_exit(&releq->smq_mtx);
9880Sstevel@tonic-gate 	}
9890Sstevel@tonic-gate }
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate 
9920Sstevel@tonic-gate static struct smap *
9930Sstevel@tonic-gate segmap_hashin(struct smap *smp, struct vnode *vp, u_offset_t off, int hashid)
9940Sstevel@tonic-gate {
9950Sstevel@tonic-gate 	struct smap **hpp;
9960Sstevel@tonic-gate 	struct smap *tmp;
9970Sstevel@tonic-gate 	kmutex_t *hmtx;
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(SMAPMTX(smp)));
10000Sstevel@tonic-gate 	ASSERT(smp->sm_vp == NULL);
10010Sstevel@tonic-gate 	ASSERT(smp->sm_hash == NULL);
10020Sstevel@tonic-gate 	ASSERT(smp->sm_prev == NULL);
10030Sstevel@tonic-gate 	ASSERT(smp->sm_next == NULL);
10040Sstevel@tonic-gate 	ASSERT(hashid >= 0 && hashid <= smd_hashmsk);
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	hmtx = SHASHMTX(hashid);
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 	mutex_enter(hmtx);
10090Sstevel@tonic-gate 	/*
10100Sstevel@tonic-gate 	 * First we need to verify that no one has created a smp
10110Sstevel@tonic-gate 	 * with (vp,off) as its tag before we us.
10120Sstevel@tonic-gate 	 */
10130Sstevel@tonic-gate 	for (tmp = smd_hash[hashid].sh_hash_list;
10140Sstevel@tonic-gate 	    tmp != NULL; tmp = tmp->sm_hash)
10150Sstevel@tonic-gate 		if (tmp->sm_vp == vp && tmp->sm_off == off)
10160Sstevel@tonic-gate 			break;
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate 	if (tmp == NULL) {
10190Sstevel@tonic-gate 		/*
10200Sstevel@tonic-gate 		 * No one created one yet.
10210Sstevel@tonic-gate 		 *
10220Sstevel@tonic-gate 		 * Funniness here - we don't increment the ref count on the
10230Sstevel@tonic-gate 		 * vnode * even though we have another pointer to it here.
10240Sstevel@tonic-gate 		 * The reason for this is that we don't want the fact that
10250Sstevel@tonic-gate 		 * a seg_map entry somewhere refers to a vnode to prevent the
10260Sstevel@tonic-gate 		 * vnode * itself from going away.  This is because this
10270Sstevel@tonic-gate 		 * reference to the vnode is a "soft one".  In the case where
10280Sstevel@tonic-gate 		 * a mapping is being used by a rdwr [or directory routine?]
10290Sstevel@tonic-gate 		 * there already has to be a non-zero ref count on the vnode.
10300Sstevel@tonic-gate 		 * In the case where the vp has been freed and the the smap
10310Sstevel@tonic-gate 		 * structure is on the free list, there are no pages in memory
10320Sstevel@tonic-gate 		 * that can refer to the vnode.  Thus even if we reuse the same
10330Sstevel@tonic-gate 		 * vnode/smap structure for a vnode which has the same
10340Sstevel@tonic-gate 		 * address but represents a different object, we are ok.
10350Sstevel@tonic-gate 		 */
10360Sstevel@tonic-gate 		smp->sm_vp = vp;
10370Sstevel@tonic-gate 		smp->sm_off = off;
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 		hpp = &smd_hash[hashid].sh_hash_list;
10400Sstevel@tonic-gate 		smp->sm_hash = *hpp;
10410Sstevel@tonic-gate 		*hpp = smp;
10420Sstevel@tonic-gate #ifdef SEGMAP_HASHSTATS
10430Sstevel@tonic-gate 		smd_hash_len[hashid]++;
10440Sstevel@tonic-gate #endif
10450Sstevel@tonic-gate 	}
10460Sstevel@tonic-gate 	mutex_exit(hmtx);
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate 	return (tmp);
10490Sstevel@tonic-gate }
10500Sstevel@tonic-gate 
10510Sstevel@tonic-gate static void
10520Sstevel@tonic-gate segmap_hashout(struct smap *smp)
10530Sstevel@tonic-gate {
10540Sstevel@tonic-gate 	struct smap **hpp, *hp;
10550Sstevel@tonic-gate 	struct vnode *vp;
10560Sstevel@tonic-gate 	kmutex_t *mtx;
10570Sstevel@tonic-gate 	int hashid;
10580Sstevel@tonic-gate 	u_offset_t off;
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(SMAPMTX(smp)));
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate 	vp = smp->sm_vp;
10630Sstevel@tonic-gate 	off = smp->sm_off;
10640Sstevel@tonic-gate 
10650Sstevel@tonic-gate 	SMAP_HASHFUNC(vp, off, hashid);	/* macro assigns hashid */
10660Sstevel@tonic-gate 	mtx = SHASHMTX(hashid);
10670Sstevel@tonic-gate 	mutex_enter(mtx);
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate 	hpp = &smd_hash[hashid].sh_hash_list;
10700Sstevel@tonic-gate 	for (;;) {
10710Sstevel@tonic-gate 		hp = *hpp;
10720Sstevel@tonic-gate 		if (hp == NULL) {
10730Sstevel@tonic-gate 			panic("segmap_hashout");
10740Sstevel@tonic-gate 			/*NOTREACHED*/
10750Sstevel@tonic-gate 		}
10760Sstevel@tonic-gate 		if (hp == smp)
10770Sstevel@tonic-gate 			break;
10780Sstevel@tonic-gate 		hpp = &hp->sm_hash;
10790Sstevel@tonic-gate 	}
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 	*hpp = smp->sm_hash;
10820Sstevel@tonic-gate 	smp->sm_hash = NULL;
10830Sstevel@tonic-gate #ifdef SEGMAP_HASHSTATS
10840Sstevel@tonic-gate 	smd_hash_len[hashid]--;
10850Sstevel@tonic-gate #endif
10860Sstevel@tonic-gate 	mutex_exit(mtx);
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 	smp->sm_vp = NULL;
10890Sstevel@tonic-gate 	smp->sm_off = (u_offset_t)0;
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate }
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate /*
10940Sstevel@tonic-gate  * Attempt to free unmodified, unmapped, and non locked segmap
10950Sstevel@tonic-gate  * pages.
10960Sstevel@tonic-gate  */
10970Sstevel@tonic-gate void
10980Sstevel@tonic-gate segmap_pagefree(struct vnode *vp, u_offset_t off)
10990Sstevel@tonic-gate {
11000Sstevel@tonic-gate 	u_offset_t pgoff;
11010Sstevel@tonic-gate 	page_t  *pp;
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate 	for (pgoff = off; pgoff < off + MAXBSIZE; pgoff += PAGESIZE) {
11040Sstevel@tonic-gate 
11050Sstevel@tonic-gate 		if ((pp = page_lookup_nowait(vp, pgoff, SE_EXCL)) == NULL)
11060Sstevel@tonic-gate 			continue;
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate 		switch (page_release(pp, 1)) {
11090Sstevel@tonic-gate 		case PGREL_NOTREL:
11100Sstevel@tonic-gate 			segmapcnt.smp_free_notfree.value.ul++;
11110Sstevel@tonic-gate 			break;
11120Sstevel@tonic-gate 		case PGREL_MOD:
11130Sstevel@tonic-gate 			segmapcnt.smp_free_dirty.value.ul++;
11140Sstevel@tonic-gate 			break;
11150Sstevel@tonic-gate 		case PGREL_CLEAN:
11160Sstevel@tonic-gate 			segmapcnt.smp_free.value.ul++;
11170Sstevel@tonic-gate 			break;
11180Sstevel@tonic-gate 		}
11190Sstevel@tonic-gate 	}
11200Sstevel@tonic-gate }
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate /*
11230Sstevel@tonic-gate  * Locks held on entry: smap lock
11240Sstevel@tonic-gate  * Locks held on exit : smap lock.
11250Sstevel@tonic-gate  */
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate static void
11280Sstevel@tonic-gate grab_smp(struct smap *smp, page_t *pp)
11290Sstevel@tonic-gate {
11300Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(SMAPMTX(smp)));
11310Sstevel@tonic-gate 	ASSERT(smp->sm_refcnt == 0);
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate 	if (smp->sm_vp != (struct vnode *)NULL) {
11340Sstevel@tonic-gate 		struct vnode	*vp = smp->sm_vp;
11350Sstevel@tonic-gate 		u_offset_t 	off = smp->sm_off;
11360Sstevel@tonic-gate 		/*
11370Sstevel@tonic-gate 		 * Destroy old vnode association and
11380Sstevel@tonic-gate 		 * unload any hardware translations to
11390Sstevel@tonic-gate 		 * the old object.
11400Sstevel@tonic-gate 		 */
11410Sstevel@tonic-gate 		smd_cpu[CPU->cpu_seqid].scpu.scpu_get_reuse++;
11420Sstevel@tonic-gate 		segmap_hashout(smp);
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 		/*
11450Sstevel@tonic-gate 		 * This node is off freelist and hashlist,
11460Sstevel@tonic-gate 		 * so there is no reason to drop/reacquire sm_mtx
11470Sstevel@tonic-gate 		 * across calls to hat_unload.
11480Sstevel@tonic-gate 		 */
11490Sstevel@tonic-gate 		if (segmap_kpm) {
11500Sstevel@tonic-gate 			caddr_t vaddr;
11510Sstevel@tonic-gate 			int hat_unload_needed = 0;
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 			/*
11540Sstevel@tonic-gate 			 * unload kpm mapping
11550Sstevel@tonic-gate 			 */
11560Sstevel@tonic-gate 			if (pp != NULL) {
11570Sstevel@tonic-gate 				vaddr = hat_kpm_page2va(pp, 1);
11580Sstevel@tonic-gate 				hat_kpm_mapout(pp, GET_KPME(smp), vaddr);
11590Sstevel@tonic-gate 				page_unlock(pp);
11600Sstevel@tonic-gate 			}
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate 			/*
11630Sstevel@tonic-gate 			 * Check if we have (also) the rare case of a
11640Sstevel@tonic-gate 			 * non kpm mapping.
11650Sstevel@tonic-gate 			 */
11660Sstevel@tonic-gate 			if (smp->sm_flags & SM_NOTKPM_RELEASED) {
11670Sstevel@tonic-gate 				hat_unload_needed = 1;
11680Sstevel@tonic-gate 				smp->sm_flags &= ~SM_NOTKPM_RELEASED;
11690Sstevel@tonic-gate 			}
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 			if (hat_unload_needed) {
11720Sstevel@tonic-gate 				hat_unload(kas.a_hat, segkmap->s_base +
11730Sstevel@tonic-gate 				    ((smp - smd_smap) * MAXBSIZE),
11740Sstevel@tonic-gate 				    MAXBSIZE, HAT_UNLOAD);
11750Sstevel@tonic-gate 			}
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate 		} else {
11780Sstevel@tonic-gate 			ASSERT(smp->sm_flags & SM_NOTKPM_RELEASED);
11790Sstevel@tonic-gate 			smp->sm_flags &= ~SM_NOTKPM_RELEASED;
11800Sstevel@tonic-gate 			hat_unload(kas.a_hat, segkmap->s_base +
11810Sstevel@tonic-gate 			    ((smp - smd_smap) * MAXBSIZE),
11820Sstevel@tonic-gate 			    MAXBSIZE, HAT_UNLOAD);
11830Sstevel@tonic-gate 		}
11840Sstevel@tonic-gate 		segmap_pagefree(vp, off);
11850Sstevel@tonic-gate 	}
11860Sstevel@tonic-gate }
11870Sstevel@tonic-gate 
11880Sstevel@tonic-gate static struct smap *
11890Sstevel@tonic-gate get_free_smp(int free_ndx)
11900Sstevel@tonic-gate {
11910Sstevel@tonic-gate 	struct smfree *sm;
11920Sstevel@tonic-gate 	kmutex_t *smtx;
11930Sstevel@tonic-gate 	struct smap *smp, *first;
11940Sstevel@tonic-gate 	struct sm_freeq *allocq, *releq;
11950Sstevel@tonic-gate 	struct kpme *kpme;
11960Sstevel@tonic-gate 	page_t *pp = NULL;
11970Sstevel@tonic-gate 	int end_ndx, page_locked = 0;
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate 	end_ndx = free_ndx;
12000Sstevel@tonic-gate 	sm = &smd_free[free_ndx];
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate retry_queue:
12030Sstevel@tonic-gate 	allocq = sm->sm_allocq;
12040Sstevel@tonic-gate 	mutex_enter(&allocq->smq_mtx);
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate 	if ((smp = allocq->smq_free) == NULL) {
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate skip_queue:
12090Sstevel@tonic-gate 		/*
12100Sstevel@tonic-gate 		 * The alloc list is empty or this queue is being skipped;
12110Sstevel@tonic-gate 		 * first see if the allocq toggled.
12120Sstevel@tonic-gate 		 */
12130Sstevel@tonic-gate 		if (sm->sm_allocq != allocq) {
12140Sstevel@tonic-gate 			/* queue changed */
12150Sstevel@tonic-gate 			mutex_exit(&allocq->smq_mtx);
12160Sstevel@tonic-gate 			goto retry_queue;
12170Sstevel@tonic-gate 		}
12180Sstevel@tonic-gate 		releq = sm->sm_releq;
12190Sstevel@tonic-gate 		if (!mutex_tryenter(&releq->smq_mtx)) {
12200Sstevel@tonic-gate 			/* cannot get releq; a free smp may be there now */
12210Sstevel@tonic-gate 			mutex_exit(&allocq->smq_mtx);
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 			/*
12240Sstevel@tonic-gate 			 * This loop could spin forever if this thread has
12250Sstevel@tonic-gate 			 * higher priority than the thread that is holding
12260Sstevel@tonic-gate 			 * releq->smq_mtx. In order to force the other thread
12270Sstevel@tonic-gate 			 * to run, we'll lock/unlock the mutex which is safe
12280Sstevel@tonic-gate 			 * since we just unlocked the allocq mutex.
12290Sstevel@tonic-gate 			 */
12300Sstevel@tonic-gate 			mutex_enter(&releq->smq_mtx);
12310Sstevel@tonic-gate 			mutex_exit(&releq->smq_mtx);
12320Sstevel@tonic-gate 			goto retry_queue;
12330Sstevel@tonic-gate 		}
12340Sstevel@tonic-gate 		if (releq->smq_free == NULL) {
12350Sstevel@tonic-gate 			/*
12360Sstevel@tonic-gate 			 * This freelist is empty.
12370Sstevel@tonic-gate 			 * This should not happen unless clients
12380Sstevel@tonic-gate 			 * are failing to release the segmap
12390Sstevel@tonic-gate 			 * window after accessing the data.
12400Sstevel@tonic-gate 			 * Before resorting to sleeping, try
12410Sstevel@tonic-gate 			 * the next list of the same color.
12420Sstevel@tonic-gate 			 */
12430Sstevel@tonic-gate 			free_ndx = (free_ndx + smd_ncolor) & smd_freemsk;
12440Sstevel@tonic-gate 			if (free_ndx != end_ndx) {
12450Sstevel@tonic-gate 				mutex_exit(&releq->smq_mtx);
12460Sstevel@tonic-gate 				mutex_exit(&allocq->smq_mtx);
12470Sstevel@tonic-gate 				sm = &smd_free[free_ndx];
12480Sstevel@tonic-gate 				goto retry_queue;
12490Sstevel@tonic-gate 			}
12500Sstevel@tonic-gate 			/*
12510Sstevel@tonic-gate 			 * Tried all freelists of the same color once,
12520Sstevel@tonic-gate 			 * wait on this list and hope something gets freed.
12530Sstevel@tonic-gate 			 */
12540Sstevel@tonic-gate 			segmapcnt.smp_get_nofree.value.ul++;
12550Sstevel@tonic-gate 			sm->sm_want++;
12560Sstevel@tonic-gate 			mutex_exit(&sm->sm_freeq[1].smq_mtx);
12570Sstevel@tonic-gate 			cv_wait(&sm->sm_free_cv,
12580Sstevel@tonic-gate 				&sm->sm_freeq[0].smq_mtx);
12590Sstevel@tonic-gate 			sm->sm_want--;
12600Sstevel@tonic-gate 			mutex_exit(&sm->sm_freeq[0].smq_mtx);
12610Sstevel@tonic-gate 			sm = &smd_free[free_ndx];
12620Sstevel@tonic-gate 			goto retry_queue;
12630Sstevel@tonic-gate 		} else {
12640Sstevel@tonic-gate 			/*
12650Sstevel@tonic-gate 			 * Something on the rele queue; flip the alloc
12660Sstevel@tonic-gate 			 * and rele queues and retry.
12670Sstevel@tonic-gate 			 */
12680Sstevel@tonic-gate 			sm->sm_allocq = releq;
12690Sstevel@tonic-gate 			sm->sm_releq = allocq;
12700Sstevel@tonic-gate 			mutex_exit(&allocq->smq_mtx);
12710Sstevel@tonic-gate 			mutex_exit(&releq->smq_mtx);
12720Sstevel@tonic-gate 			if (page_locked) {
12730Sstevel@tonic-gate 				delay(hz >> 2);
12740Sstevel@tonic-gate 				page_locked = 0;
12750Sstevel@tonic-gate 			}
12760Sstevel@tonic-gate 			goto retry_queue;
12770Sstevel@tonic-gate 		}
12780Sstevel@tonic-gate 	} else {
12790Sstevel@tonic-gate 		/*
12800Sstevel@tonic-gate 		 * Fastpath the case we get the smap mutex
12810Sstevel@tonic-gate 		 * on the first try.
12820Sstevel@tonic-gate 		 */
12830Sstevel@tonic-gate 		first = smp;
12840Sstevel@tonic-gate next_smap:
12850Sstevel@tonic-gate 		smtx = SMAPMTX(smp);
12860Sstevel@tonic-gate 		if (!mutex_tryenter(smtx)) {
12870Sstevel@tonic-gate 			/*
12880Sstevel@tonic-gate 			 * Another thread is trying to reclaim this slot.
12890Sstevel@tonic-gate 			 * Skip to the next queue or smap.
12900Sstevel@tonic-gate 			 */
12910Sstevel@tonic-gate 			if ((smp = smp->sm_next) == first) {
12920Sstevel@tonic-gate 				goto skip_queue;
12930Sstevel@tonic-gate 			} else {
12940Sstevel@tonic-gate 				goto next_smap;
12950Sstevel@tonic-gate 			}
12960Sstevel@tonic-gate 		} else {
12970Sstevel@tonic-gate 			/*
12980Sstevel@tonic-gate 			 * if kpme exists, get shared lock on the page
12990Sstevel@tonic-gate 			 */
13000Sstevel@tonic-gate 			if (segmap_kpm && smp->sm_vp != NULL) {
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate 				kpme = GET_KPME(smp);
13030Sstevel@tonic-gate 				pp = kpme->kpe_page;
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate 				if (pp != NULL) {
13060Sstevel@tonic-gate 					if (!page_trylock(pp, SE_SHARED)) {
13070Sstevel@tonic-gate 						smp = smp->sm_next;
13080Sstevel@tonic-gate 						mutex_exit(smtx);
13090Sstevel@tonic-gate 						page_locked = 1;
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate 						pp = NULL;
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate 						if (smp == first) {
13140Sstevel@tonic-gate 							goto skip_queue;
13150Sstevel@tonic-gate 						} else {
13160Sstevel@tonic-gate 							goto next_smap;
13170Sstevel@tonic-gate 						}
13180Sstevel@tonic-gate 					} else {
13190Sstevel@tonic-gate 						if (kpme->kpe_page == NULL) {
13200Sstevel@tonic-gate 							page_unlock(pp);
13210Sstevel@tonic-gate 							pp = NULL;
13220Sstevel@tonic-gate 						}
13230Sstevel@tonic-gate 					}
13240Sstevel@tonic-gate 				}
13250Sstevel@tonic-gate 			}
13260Sstevel@tonic-gate 
13270Sstevel@tonic-gate 			/*
13280Sstevel@tonic-gate 			 * At this point, we've selected smp.  Remove smp
13290Sstevel@tonic-gate 			 * from its freelist.  If smp is the first one in
13300Sstevel@tonic-gate 			 * the freelist, update the head of the freelist.
13310Sstevel@tonic-gate 			 */
13320Sstevel@tonic-gate 			if (first == smp) {
13330Sstevel@tonic-gate 				ASSERT(first == allocq->smq_free);
13340Sstevel@tonic-gate 				allocq->smq_free = smp->sm_next;
13350Sstevel@tonic-gate 			}
13360Sstevel@tonic-gate 
13370Sstevel@tonic-gate 			/*
13380Sstevel@tonic-gate 			 * if the head of the freelist still points to smp,
13390Sstevel@tonic-gate 			 * then there are no more free smaps in that list.
13400Sstevel@tonic-gate 			 */
13410Sstevel@tonic-gate 			if (allocq->smq_free == smp)
13420Sstevel@tonic-gate 				/*
13430Sstevel@tonic-gate 				 * Took the last one
13440Sstevel@tonic-gate 				 */
13450Sstevel@tonic-gate 				allocq->smq_free = NULL;
13460Sstevel@tonic-gate 			else {
13470Sstevel@tonic-gate 				smp->sm_prev->sm_next = smp->sm_next;
13480Sstevel@tonic-gate 				smp->sm_next->sm_prev = smp->sm_prev;
13490Sstevel@tonic-gate 			}
13500Sstevel@tonic-gate 			mutex_exit(&allocq->smq_mtx);
13510Sstevel@tonic-gate 			smp->sm_prev = smp->sm_next = NULL;
13520Sstevel@tonic-gate 
13530Sstevel@tonic-gate 			/*
13540Sstevel@tonic-gate 			 * if pp != NULL, pp must have been locked;
13550Sstevel@tonic-gate 			 * grab_smp() unlocks pp.
13560Sstevel@tonic-gate 			 */
13570Sstevel@tonic-gate 			ASSERT((pp == NULL) || PAGE_LOCKED(pp));
13580Sstevel@tonic-gate 			grab_smp(smp, pp);
13590Sstevel@tonic-gate 			/* return smp locked. */
13600Sstevel@tonic-gate 			ASSERT(SMAPMTX(smp) == smtx);
13610Sstevel@tonic-gate 			ASSERT(MUTEX_HELD(smtx));
13620Sstevel@tonic-gate 			return (smp);
13630Sstevel@tonic-gate 		}
13640Sstevel@tonic-gate 	}
13650Sstevel@tonic-gate }
13660Sstevel@tonic-gate 
13670Sstevel@tonic-gate /*
13680Sstevel@tonic-gate  * Special public segmap operations
13690Sstevel@tonic-gate  */
13700Sstevel@tonic-gate 
13710Sstevel@tonic-gate /*
13720Sstevel@tonic-gate  * Create pages (without using VOP_GETPAGE) and load up tranlations to them.
13730Sstevel@tonic-gate  * If softlock is TRUE, then set things up so that it looks like a call
13740Sstevel@tonic-gate  * to segmap_fault with F_SOFTLOCK.
13750Sstevel@tonic-gate  *
13760Sstevel@tonic-gate  * Returns 1, if a page is created by calling page_create_va(), or 0 otherwise.
13770Sstevel@tonic-gate  *
13780Sstevel@tonic-gate  * All fields in the generic segment (struct seg) are considered to be
13790Sstevel@tonic-gate  * read-only for "segmap" even though the kernel address space (kas) may
13800Sstevel@tonic-gate  * not be locked, hence no lock is needed to access them.
13810Sstevel@tonic-gate  */
13820Sstevel@tonic-gate int
13830Sstevel@tonic-gate segmap_pagecreate(struct seg *seg, caddr_t addr, size_t len, int softlock)
13840Sstevel@tonic-gate {
13850Sstevel@tonic-gate 	struct segmap_data *smd = (struct segmap_data *)seg->s_data;
13860Sstevel@tonic-gate 	page_t *pp;
13870Sstevel@tonic-gate 	u_offset_t off;
13880Sstevel@tonic-gate 	struct smap *smp;
13890Sstevel@tonic-gate 	struct vnode *vp;
13900Sstevel@tonic-gate 	caddr_t eaddr;
13910Sstevel@tonic-gate 	int newpage = 0;
13920Sstevel@tonic-gate 	uint_t prot;
13930Sstevel@tonic-gate 	kmutex_t *smtx;
13940Sstevel@tonic-gate 	int hat_flag;
13950Sstevel@tonic-gate 
13960Sstevel@tonic-gate 	ASSERT(seg->s_as == &kas);
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate 	if (segmap_kpm && IS_KPM_ADDR(addr)) {
13990Sstevel@tonic-gate 		/*
14000Sstevel@tonic-gate 		 * Pages are successfully prefaulted and locked in
14010Sstevel@tonic-gate 		 * segmap_getmapflt and can't be unlocked until
14020Sstevel@tonic-gate 		 * segmap_release. The SM_KPM_NEWPAGE flag is set
14030Sstevel@tonic-gate 		 * in segmap_pagecreate_kpm when new pages are created.
14040Sstevel@tonic-gate 		 * and it is returned as "newpage" indication here.
14050Sstevel@tonic-gate 		 */
14060Sstevel@tonic-gate 		if ((smp = get_smap_kpm(addr, NULL)) == NULL) {
14070Sstevel@tonic-gate 			panic("segmap_pagecreate: smap not found "
14080Sstevel@tonic-gate 			    "for addr %p", (void *)addr);
14090Sstevel@tonic-gate 			/*NOTREACHED*/
14100Sstevel@tonic-gate 		}
14110Sstevel@tonic-gate 
14120Sstevel@tonic-gate 		smtx = SMAPMTX(smp);
14130Sstevel@tonic-gate 		newpage = smp->sm_flags & SM_KPM_NEWPAGE;
14140Sstevel@tonic-gate 		smp->sm_flags &= ~SM_KPM_NEWPAGE;
14150Sstevel@tonic-gate 		mutex_exit(smtx);
14160Sstevel@tonic-gate 
14170Sstevel@tonic-gate 		return (newpage);
14180Sstevel@tonic-gate 	}
14190Sstevel@tonic-gate 
14200Sstevel@tonic-gate 	smd_cpu[CPU->cpu_seqid].scpu.scpu_pagecreate++;
14210Sstevel@tonic-gate 
14220Sstevel@tonic-gate 	eaddr = addr + len;
14230Sstevel@tonic-gate 	addr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
14240Sstevel@tonic-gate 
14250Sstevel@tonic-gate 	smp = GET_SMAP(seg, addr);
14260Sstevel@tonic-gate 
14270Sstevel@tonic-gate 	/*
14280Sstevel@tonic-gate 	 * We don't grab smp mutex here since we assume the smp
14290Sstevel@tonic-gate 	 * has a refcnt set already which prevents the slot from
14300Sstevel@tonic-gate 	 * changing its id.
14310Sstevel@tonic-gate 	 */
14320Sstevel@tonic-gate 	ASSERT(smp->sm_refcnt > 0);
14330Sstevel@tonic-gate 
14340Sstevel@tonic-gate 	vp = smp->sm_vp;
14350Sstevel@tonic-gate 	off = smp->sm_off + ((u_offset_t)((uintptr_t)addr & MAXBOFFSET));
14360Sstevel@tonic-gate 	prot = smd->smd_prot;
14370Sstevel@tonic-gate 
14380Sstevel@tonic-gate 	for (; addr < eaddr; addr += PAGESIZE, off += PAGESIZE) {
14390Sstevel@tonic-gate 		hat_flag = HAT_LOAD;
14400Sstevel@tonic-gate 		pp = page_lookup(vp, off, SE_SHARED);
14410Sstevel@tonic-gate 		if (pp == NULL) {
14420Sstevel@tonic-gate 			ushort_t bitindex;
14430Sstevel@tonic-gate 
14440Sstevel@tonic-gate 			if ((pp = page_create_va(vp, off,
14450Sstevel@tonic-gate 			    PAGESIZE, PG_WAIT, seg, addr)) == NULL) {
14460Sstevel@tonic-gate 				panic("segmap_pagecreate: page_create failed");
14470Sstevel@tonic-gate 				/*NOTREACHED*/
14480Sstevel@tonic-gate 			}
14490Sstevel@tonic-gate 			newpage = 1;
14500Sstevel@tonic-gate 			page_io_unlock(pp);
14510Sstevel@tonic-gate 
14520Sstevel@tonic-gate 			/*
14530Sstevel@tonic-gate 			 * Since pages created here do not contain valid
14540Sstevel@tonic-gate 			 * data until the caller writes into them, the
14550Sstevel@tonic-gate 			 * "exclusive" lock will not be dropped to prevent
14560Sstevel@tonic-gate 			 * other users from accessing the page.  We also
14570Sstevel@tonic-gate 			 * have to lock the translation to prevent a fault
14580Sstevel@tonic-gate 			 * from occuring when the virtual address mapped by
14590Sstevel@tonic-gate 			 * this page is written into.  This is necessary to
14600Sstevel@tonic-gate 			 * avoid a deadlock since we haven't dropped the
14610Sstevel@tonic-gate 			 * "exclusive" lock.
14620Sstevel@tonic-gate 			 */
14630Sstevel@tonic-gate 			bitindex = (ushort_t)((off - smp->sm_off) >> PAGESHIFT);
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate 			/*
14660Sstevel@tonic-gate 			 * Large Files: The following assertion is to
14670Sstevel@tonic-gate 			 * verify the cast above.
14680Sstevel@tonic-gate 			 */
14690Sstevel@tonic-gate 			ASSERT((u_offset_t)(off - smp->sm_off) <= INT_MAX);
14700Sstevel@tonic-gate 			smtx = SMAPMTX(smp);
14710Sstevel@tonic-gate 			mutex_enter(smtx);
14720Sstevel@tonic-gate 			smp->sm_bitmap |= SMAP_BIT_MASK(bitindex);
14730Sstevel@tonic-gate 			mutex_exit(smtx);
14740Sstevel@tonic-gate 
14750Sstevel@tonic-gate 			hat_flag = HAT_LOAD_LOCK;
14760Sstevel@tonic-gate 		} else if (softlock) {
14770Sstevel@tonic-gate 			hat_flag = HAT_LOAD_LOCK;
14780Sstevel@tonic-gate 		}
14790Sstevel@tonic-gate 
14800Sstevel@tonic-gate 		if (IS_VMODSORT(pp->p_vnode) && (prot & PROT_WRITE))
14810Sstevel@tonic-gate 			hat_setmod(pp);
14820Sstevel@tonic-gate 
14830Sstevel@tonic-gate 		hat_memload(kas.a_hat, addr, pp, prot, hat_flag);
14840Sstevel@tonic-gate 
14850Sstevel@tonic-gate 		if (hat_flag != HAT_LOAD_LOCK)
14860Sstevel@tonic-gate 			page_unlock(pp);
14870Sstevel@tonic-gate 
14880Sstevel@tonic-gate 		TRACE_5(TR_FAC_VM, TR_SEGMAP_PAGECREATE,
14890Sstevel@tonic-gate 		    "segmap_pagecreate:seg %p addr %p pp %p vp %p offset %llx",
14900Sstevel@tonic-gate 		    seg, addr, pp, vp, off);
14910Sstevel@tonic-gate 	}
14920Sstevel@tonic-gate 
14930Sstevel@tonic-gate 	return (newpage);
14940Sstevel@tonic-gate }
14950Sstevel@tonic-gate 
14960Sstevel@tonic-gate void
14970Sstevel@tonic-gate segmap_pageunlock(struct seg *seg, caddr_t addr, size_t len, enum seg_rw rw)
14980Sstevel@tonic-gate {
14990Sstevel@tonic-gate 	struct smap	*smp;
15000Sstevel@tonic-gate 	ushort_t	bitmask;
15010Sstevel@tonic-gate 	page_t		*pp;
15020Sstevel@tonic-gate 	struct	vnode	*vp;
15030Sstevel@tonic-gate 	u_offset_t	off;
15040Sstevel@tonic-gate 	caddr_t		eaddr;
15050Sstevel@tonic-gate 	kmutex_t	*smtx;
15060Sstevel@tonic-gate 
15070Sstevel@tonic-gate 	ASSERT(seg->s_as == &kas);
15080Sstevel@tonic-gate 
15090Sstevel@tonic-gate 	eaddr = addr + len;
15100Sstevel@tonic-gate 	addr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
15110Sstevel@tonic-gate 
15120Sstevel@tonic-gate 	if (segmap_kpm && IS_KPM_ADDR(addr)) {
15130Sstevel@tonic-gate 		/*
15140Sstevel@tonic-gate 		 * Pages are successfully prefaulted and locked in
15150Sstevel@tonic-gate 		 * segmap_getmapflt and can't be unlocked until
15160Sstevel@tonic-gate 		 * segmap_release, so no pages or hat mappings have
15170Sstevel@tonic-gate 		 * to be unlocked at this point.
15180Sstevel@tonic-gate 		 */
15190Sstevel@tonic-gate #ifdef DEBUG
15200Sstevel@tonic-gate 		if ((smp = get_smap_kpm(addr, NULL)) == NULL) {
15210Sstevel@tonic-gate 			panic("segmap_pageunlock: smap not found "
15220Sstevel@tonic-gate 			    "for addr %p", (void *)addr);
15230Sstevel@tonic-gate 			/*NOTREACHED*/
15240Sstevel@tonic-gate 		}
15250Sstevel@tonic-gate 
15260Sstevel@tonic-gate 		ASSERT(smp->sm_refcnt > 0);
15270Sstevel@tonic-gate 		mutex_exit(SMAPMTX(smp));
15280Sstevel@tonic-gate #endif
15290Sstevel@tonic-gate 		return;
15300Sstevel@tonic-gate 	}
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate 	smp = GET_SMAP(seg, addr);
15330Sstevel@tonic-gate 	smtx = SMAPMTX(smp);
15340Sstevel@tonic-gate 
15350Sstevel@tonic-gate 	ASSERT(smp->sm_refcnt > 0);
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate 	vp = smp->sm_vp;
15380Sstevel@tonic-gate 	off = smp->sm_off + ((u_offset_t)((uintptr_t)addr & MAXBOFFSET));
15390Sstevel@tonic-gate 
15400Sstevel@tonic-gate 	for (; addr < eaddr; addr += PAGESIZE, off += PAGESIZE) {
15410Sstevel@tonic-gate 		bitmask = SMAP_BIT_MASK((int)(off - smp->sm_off) >> PAGESHIFT);
15420Sstevel@tonic-gate 
15430Sstevel@tonic-gate 		/*
15440Sstevel@tonic-gate 		 * Large Files: Following assertion is to verify
15450Sstevel@tonic-gate 		 * the correctness of the cast to (int) above.
15460Sstevel@tonic-gate 		 */
15470Sstevel@tonic-gate 		ASSERT((u_offset_t)(off - smp->sm_off) <= INT_MAX);
15480Sstevel@tonic-gate 
15490Sstevel@tonic-gate 		/*
15500Sstevel@tonic-gate 		 * If the bit corresponding to "off" is set,
15510Sstevel@tonic-gate 		 * clear this bit in the bitmap, unlock translations,
15520Sstevel@tonic-gate 		 * and release the "exclusive" lock on the page.
15530Sstevel@tonic-gate 		 */
15540Sstevel@tonic-gate 		if (smp->sm_bitmap & bitmask) {
15550Sstevel@tonic-gate 			mutex_enter(smtx);
15560Sstevel@tonic-gate 			smp->sm_bitmap &= ~bitmask;
15570Sstevel@tonic-gate 			mutex_exit(smtx);
15580Sstevel@tonic-gate 
15590Sstevel@tonic-gate 			hat_unlock(kas.a_hat, addr, PAGESIZE);
15600Sstevel@tonic-gate 
15610Sstevel@tonic-gate 			/*
15620Sstevel@tonic-gate 			 * Use page_find() instead of page_lookup() to
15630Sstevel@tonic-gate 			 * find the page since we know that it has
15640Sstevel@tonic-gate 			 * "exclusive" lock.
15650Sstevel@tonic-gate 			 */
15660Sstevel@tonic-gate 			pp = page_find(vp, off);
15670Sstevel@tonic-gate 			if (pp == NULL) {
15680Sstevel@tonic-gate 				panic("segmap_pageunlock: page not found");
15690Sstevel@tonic-gate 				/*NOTREACHED*/
15700Sstevel@tonic-gate 			}
15710Sstevel@tonic-gate 			if (rw == S_WRITE) {
15720Sstevel@tonic-gate 				hat_setrefmod(pp);
15730Sstevel@tonic-gate 			} else if (rw != S_OTHER) {
15740Sstevel@tonic-gate 				hat_setref(pp);
15750Sstevel@tonic-gate 			}
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate 			page_unlock(pp);
15780Sstevel@tonic-gate 		}
15790Sstevel@tonic-gate 	}
15800Sstevel@tonic-gate }
15810Sstevel@tonic-gate 
15820Sstevel@tonic-gate caddr_t
15830Sstevel@tonic-gate segmap_getmap(struct seg *seg, struct vnode *vp, u_offset_t off)
15840Sstevel@tonic-gate {
15850Sstevel@tonic-gate 	return (segmap_getmapflt(seg, vp, off, MAXBSIZE, 0, S_OTHER));
15860Sstevel@tonic-gate }
15870Sstevel@tonic-gate 
15880Sstevel@tonic-gate /*
15890Sstevel@tonic-gate  * This is the magic virtual address that offset 0 of an ELF
15900Sstevel@tonic-gate  * file gets mapped to in user space. This is used to pick
15910Sstevel@tonic-gate  * the vac color on the freelist.
15920Sstevel@tonic-gate  */
15930Sstevel@tonic-gate #define	ELF_OFFZERO_VA	(0x10000)
15940Sstevel@tonic-gate /*
15950Sstevel@tonic-gate  * segmap_getmap allocates a MAXBSIZE big slot to map the vnode vp
15960Sstevel@tonic-gate  * in the range <off, off + len). off doesn't need to be MAXBSIZE aligned.
15970Sstevel@tonic-gate  * The return address is  always MAXBSIZE aligned.
15980Sstevel@tonic-gate  *
15990Sstevel@tonic-gate  * If forcefault is nonzero and the MMU translations haven't yet been created,
16000Sstevel@tonic-gate  * segmap_getmap will call segmap_fault(..., F_INVAL, rw) to create them.
16010Sstevel@tonic-gate  */
16020Sstevel@tonic-gate caddr_t
16030Sstevel@tonic-gate segmap_getmapflt(
16040Sstevel@tonic-gate 	struct seg *seg,
16050Sstevel@tonic-gate 	struct vnode *vp,
16060Sstevel@tonic-gate 	u_offset_t off,
16070Sstevel@tonic-gate 	size_t len,
16080Sstevel@tonic-gate 	int forcefault,
16090Sstevel@tonic-gate 	enum seg_rw rw)
16100Sstevel@tonic-gate {
16110Sstevel@tonic-gate 	struct smap *smp, *nsmp;
16120Sstevel@tonic-gate 	extern struct vnode *common_specvp();
16130Sstevel@tonic-gate 	caddr_t baseaddr;			/* MAXBSIZE aligned */
16140Sstevel@tonic-gate 	u_offset_t baseoff;
16150Sstevel@tonic-gate 	int newslot;
16160Sstevel@tonic-gate 	caddr_t vaddr;
16170Sstevel@tonic-gate 	int color, hashid;
16180Sstevel@tonic-gate 	kmutex_t *hashmtx, *smapmtx;
16190Sstevel@tonic-gate 	struct smfree *sm;
16200Sstevel@tonic-gate 	page_t	*pp;
16210Sstevel@tonic-gate 	struct kpme *kpme;
16220Sstevel@tonic-gate 	uint_t	prot;
16230Sstevel@tonic-gate 	caddr_t base;
16240Sstevel@tonic-gate 	page_t	*pl[MAXPPB + 1];
16250Sstevel@tonic-gate 	int	error;
16260Sstevel@tonic-gate 	int	is_kpm = 1;
16270Sstevel@tonic-gate 
16280Sstevel@tonic-gate 	ASSERT(seg->s_as == &kas);
16290Sstevel@tonic-gate 	ASSERT(seg == segkmap);
16300Sstevel@tonic-gate 
16310Sstevel@tonic-gate 	baseoff = off & (offset_t)MAXBMASK;
16320Sstevel@tonic-gate 	if (off + len > baseoff + MAXBSIZE) {
16330Sstevel@tonic-gate 		panic("segmap_getmap bad len");
16340Sstevel@tonic-gate 		/*NOTREACHED*/
16350Sstevel@tonic-gate 	}
16360Sstevel@tonic-gate 
16370Sstevel@tonic-gate 	/*
16380Sstevel@tonic-gate 	 * If this is a block device we have to be sure to use the
16390Sstevel@tonic-gate 	 * "common" block device vnode for the mapping.
16400Sstevel@tonic-gate 	 */
16410Sstevel@tonic-gate 	if (vp->v_type == VBLK)
16420Sstevel@tonic-gate 		vp = common_specvp(vp);
16430Sstevel@tonic-gate 
16440Sstevel@tonic-gate 	smd_cpu[CPU->cpu_seqid].scpu.scpu_getmap++;
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate 	if (segmap_kpm == 0 ||
16470Sstevel@tonic-gate 	    (forcefault == SM_PAGECREATE && rw != S_WRITE)) {
16480Sstevel@tonic-gate 		is_kpm = 0;
16490Sstevel@tonic-gate 	}
16500Sstevel@tonic-gate 
16510Sstevel@tonic-gate 	SMAP_HASHFUNC(vp, off, hashid);	/* macro assigns hashid */
16520Sstevel@tonic-gate 	hashmtx = SHASHMTX(hashid);
16530Sstevel@tonic-gate 
16540Sstevel@tonic-gate retry_hash:
16550Sstevel@tonic-gate 	mutex_enter(hashmtx);
16560Sstevel@tonic-gate 	for (smp = smd_hash[hashid].sh_hash_list;
16570Sstevel@tonic-gate 	    smp != NULL; smp = smp->sm_hash)
16580Sstevel@tonic-gate 		if (smp->sm_vp == vp && smp->sm_off == baseoff)
16590Sstevel@tonic-gate 			break;
16600Sstevel@tonic-gate 	mutex_exit(hashmtx);
16610Sstevel@tonic-gate 
16620Sstevel@tonic-gate vrfy_smp:
16630Sstevel@tonic-gate 	if (smp != NULL) {
16640Sstevel@tonic-gate 
16650Sstevel@tonic-gate 		ASSERT(vp->v_count != 0);
16660Sstevel@tonic-gate 
16670Sstevel@tonic-gate 		/*
16680Sstevel@tonic-gate 		 * Get smap lock and recheck its tag. The hash lock
16690Sstevel@tonic-gate 		 * is dropped since the hash is based on (vp, off)
16700Sstevel@tonic-gate 		 * and (vp, off) won't change when we have smap mtx.
16710Sstevel@tonic-gate 		 */
16720Sstevel@tonic-gate 		smapmtx = SMAPMTX(smp);
16730Sstevel@tonic-gate 		mutex_enter(smapmtx);
16740Sstevel@tonic-gate 		if (smp->sm_vp != vp || smp->sm_off != baseoff) {
16750Sstevel@tonic-gate 			mutex_exit(smapmtx);
16760Sstevel@tonic-gate 			goto retry_hash;
16770Sstevel@tonic-gate 		}
16780Sstevel@tonic-gate 
16790Sstevel@tonic-gate 		if (smp->sm_refcnt == 0) {
16800Sstevel@tonic-gate 
16810Sstevel@tonic-gate 			smd_cpu[CPU->cpu_seqid].scpu.scpu_get_reclaim++;
16820Sstevel@tonic-gate 
16830Sstevel@tonic-gate 			/*
16840Sstevel@tonic-gate 			 * Could still be on the free list. However, this
16850Sstevel@tonic-gate 			 * could also be an smp that is transitioning from
16860Sstevel@tonic-gate 			 * the free list when we have too much contention
16870Sstevel@tonic-gate 			 * for the smapmtx's. In this case, we have an
16880Sstevel@tonic-gate 			 * unlocked smp that is not on the free list any
16890Sstevel@tonic-gate 			 * longer, but still has a 0 refcnt.  The only way
16900Sstevel@tonic-gate 			 * to be sure is to check the freelist pointers.
16910Sstevel@tonic-gate 			 * Since we now have the smapmtx, we are guaranteed
16920Sstevel@tonic-gate 			 * that the (vp, off) won't change, so we are safe
16930Sstevel@tonic-gate 			 * to reclaim it.  get_free_smp() knows that this
16940Sstevel@tonic-gate 			 * can happen, and it will check the refcnt.
16950Sstevel@tonic-gate 			 */
16960Sstevel@tonic-gate 
16970Sstevel@tonic-gate 			if ((smp->sm_next != NULL)) {
16980Sstevel@tonic-gate 				struct sm_freeq *freeq;
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate 				ASSERT(smp->sm_prev != NULL);
17010Sstevel@tonic-gate 				sm = &smd_free[smp->sm_free_ndx];
17020Sstevel@tonic-gate 
17030Sstevel@tonic-gate 				if (smp->sm_flags & SM_QNDX_ZERO)
17040Sstevel@tonic-gate 					freeq = &sm->sm_freeq[0];
17050Sstevel@tonic-gate 				else
17060Sstevel@tonic-gate 					freeq = &sm->sm_freeq[1];
17070Sstevel@tonic-gate 
17080Sstevel@tonic-gate 				mutex_enter(&freeq->smq_mtx);
17090Sstevel@tonic-gate 				if (freeq->smq_free != smp) {
17100Sstevel@tonic-gate 					/*
17110Sstevel@tonic-gate 					 * fastpath normal case
17120Sstevel@tonic-gate 					 */
17130Sstevel@tonic-gate 					smp->sm_prev->sm_next = smp->sm_next;
17140Sstevel@tonic-gate 					smp->sm_next->sm_prev = smp->sm_prev;
17150Sstevel@tonic-gate 				} else if (smp == smp->sm_next) {
17160Sstevel@tonic-gate 					/*
17170Sstevel@tonic-gate 					 * Taking the last smap on freelist
17180Sstevel@tonic-gate 					 */
17190Sstevel@tonic-gate 					freeq->smq_free = NULL;
17200Sstevel@tonic-gate 				} else {
17210Sstevel@tonic-gate 					/*
17220Sstevel@tonic-gate 					 * Reclaiming 1st smap on list
17230Sstevel@tonic-gate 					 */
17240Sstevel@tonic-gate 					freeq->smq_free = smp->sm_next;
17250Sstevel@tonic-gate 					smp->sm_prev->sm_next = smp->sm_next;
17260Sstevel@tonic-gate 					smp->sm_next->sm_prev = smp->sm_prev;
17270Sstevel@tonic-gate 				}
17280Sstevel@tonic-gate 				mutex_exit(&freeq->smq_mtx);
17290Sstevel@tonic-gate 				smp->sm_prev = smp->sm_next = NULL;
17300Sstevel@tonic-gate 			} else {
17310Sstevel@tonic-gate 				ASSERT(smp->sm_prev == NULL);
17320Sstevel@tonic-gate 				segmapcnt.smp_stolen.value.ul++;
17330Sstevel@tonic-gate 			}
17340Sstevel@tonic-gate 
17350Sstevel@tonic-gate 		} else {
17360Sstevel@tonic-gate 			segmapcnt.smp_get_use.value.ul++;
17370Sstevel@tonic-gate 		}
17380Sstevel@tonic-gate 		smp->sm_refcnt++;		/* another user */
17390Sstevel@tonic-gate 
17400Sstevel@tonic-gate 		/*
17410Sstevel@tonic-gate 		 * We don't invoke segmap_fault via TLB miss, so we set ref
17420Sstevel@tonic-gate 		 * and mod bits in advance. For S_OTHER  we set them in
17430Sstevel@tonic-gate 		 * segmap_fault F_SOFTUNLOCK.
17440Sstevel@tonic-gate 		 */
17450Sstevel@tonic-gate 		if (is_kpm) {
17460Sstevel@tonic-gate 			if (rw == S_WRITE) {
17470Sstevel@tonic-gate 				smp->sm_flags |= SM_WRITE_DATA;
17480Sstevel@tonic-gate 			} else if (rw == S_READ) {
17490Sstevel@tonic-gate 				smp->sm_flags |= SM_READ_DATA;
17500Sstevel@tonic-gate 			}
17510Sstevel@tonic-gate 		}
17520Sstevel@tonic-gate 		mutex_exit(smapmtx);
17530Sstevel@tonic-gate 
17540Sstevel@tonic-gate 		newslot = 0;
17550Sstevel@tonic-gate 	} else {
17560Sstevel@tonic-gate 
17570Sstevel@tonic-gate 		uint32_t free_ndx, *free_ndxp;
17580Sstevel@tonic-gate 		union segmap_cpu *scpu;
17590Sstevel@tonic-gate 
17600Sstevel@tonic-gate 		/*
17610Sstevel@tonic-gate 		 * On a PAC machine or a machine with anti-alias
17620Sstevel@tonic-gate 		 * hardware, smd_colormsk will be zero.
17630Sstevel@tonic-gate 		 *
17640Sstevel@tonic-gate 		 * On a VAC machine- pick color by offset in the file
17650Sstevel@tonic-gate 		 * so we won't get VAC conflicts on elf files.
17660Sstevel@tonic-gate 		 * On data files, color does not matter but we
17670Sstevel@tonic-gate 		 * don't know what kind of file it is so we always
17680Sstevel@tonic-gate 		 * pick color by offset. This causes color
17690Sstevel@tonic-gate 		 * corresponding to file offset zero to be used more
17700Sstevel@tonic-gate 		 * heavily.
17710Sstevel@tonic-gate 		 */
17720Sstevel@tonic-gate 		color = (baseoff >> MAXBSHIFT) & smd_colormsk;
17730Sstevel@tonic-gate 		scpu = smd_cpu+CPU->cpu_seqid;
17740Sstevel@tonic-gate 		free_ndxp = &scpu->scpu.scpu_free_ndx[color];
17750Sstevel@tonic-gate 		free_ndx = (*free_ndxp += smd_ncolor) & smd_freemsk;
17760Sstevel@tonic-gate #ifdef DEBUG
17770Sstevel@tonic-gate 		colors_used[free_ndx]++;
17780Sstevel@tonic-gate #endif /* DEBUG */
17790Sstevel@tonic-gate 
17800Sstevel@tonic-gate 		/*
17810Sstevel@tonic-gate 		 * Get a locked smp slot from the free list.
17820Sstevel@tonic-gate 		 */
17830Sstevel@tonic-gate 		smp = get_free_smp(free_ndx);
17840Sstevel@tonic-gate 		smapmtx = SMAPMTX(smp);
17850Sstevel@tonic-gate 
17860Sstevel@tonic-gate 		ASSERT(smp->sm_vp == NULL);
17870Sstevel@tonic-gate 
17880Sstevel@tonic-gate 		if ((nsmp = segmap_hashin(smp, vp, baseoff, hashid)) != NULL) {
17890Sstevel@tonic-gate 			/*
17900Sstevel@tonic-gate 			 * Failed to hashin, there exists one now.
17910Sstevel@tonic-gate 			 * Return the smp we just allocated.
17920Sstevel@tonic-gate 			 */
17930Sstevel@tonic-gate 			segmap_smapadd(smp);
17940Sstevel@tonic-gate 			mutex_exit(smapmtx);
17950Sstevel@tonic-gate 
17960Sstevel@tonic-gate 			smp = nsmp;
17970Sstevel@tonic-gate 			goto vrfy_smp;
17980Sstevel@tonic-gate 		}
17990Sstevel@tonic-gate 		smp->sm_refcnt++;		/* another user */
18000Sstevel@tonic-gate 
18010Sstevel@tonic-gate 		/*
18020Sstevel@tonic-gate 		 * We don't invoke segmap_fault via TLB miss, so we set ref
18030Sstevel@tonic-gate 		 * and mod bits in advance. For S_OTHER  we set them in
18040Sstevel@tonic-gate 		 * segmap_fault F_SOFTUNLOCK.
18050Sstevel@tonic-gate 		 */
18060Sstevel@tonic-gate 		if (is_kpm) {
18070Sstevel@tonic-gate 			if (rw == S_WRITE) {
18080Sstevel@tonic-gate 				smp->sm_flags |= SM_WRITE_DATA;
18090Sstevel@tonic-gate 			} else if (rw == S_READ) {
18100Sstevel@tonic-gate 				smp->sm_flags |= SM_READ_DATA;
18110Sstevel@tonic-gate 			}
18120Sstevel@tonic-gate 		}
18130Sstevel@tonic-gate 		mutex_exit(smapmtx);
18140Sstevel@tonic-gate 
18150Sstevel@tonic-gate 		newslot = 1;
18160Sstevel@tonic-gate 	}
18170Sstevel@tonic-gate 
18180Sstevel@tonic-gate 	if (!is_kpm)
18190Sstevel@tonic-gate 		goto use_segmap_range;
18200Sstevel@tonic-gate 
18210Sstevel@tonic-gate 	/*
18220Sstevel@tonic-gate 	 * Use segkpm
18230Sstevel@tonic-gate 	 */
18240Sstevel@tonic-gate 	ASSERT(PAGESIZE == MAXBSIZE);
18250Sstevel@tonic-gate 
18260Sstevel@tonic-gate 	/*
18270Sstevel@tonic-gate 	 * remember the last smp faulted on this cpu.
18280Sstevel@tonic-gate 	 */
18290Sstevel@tonic-gate 	(smd_cpu+CPU->cpu_seqid)->scpu.scpu_last_smap = smp;
18300Sstevel@tonic-gate 
18310Sstevel@tonic-gate 	if (forcefault == SM_PAGECREATE) {
18320Sstevel@tonic-gate 		baseaddr = segmap_pagecreate_kpm(seg, vp, baseoff, smp, rw);
18330Sstevel@tonic-gate 		return (baseaddr);
18340Sstevel@tonic-gate 	}
18350Sstevel@tonic-gate 
18360Sstevel@tonic-gate 	if (newslot == 0 &&
18370Sstevel@tonic-gate 	    (pp = GET_KPME(smp)->kpe_page) != NULL) {
18380Sstevel@tonic-gate 
18390Sstevel@tonic-gate 		/* fastpath */
18400Sstevel@tonic-gate 		switch (rw) {
18410Sstevel@tonic-gate 		case S_READ:
18420Sstevel@tonic-gate 		case S_WRITE:
18430Sstevel@tonic-gate 			if (page_trylock(pp, SE_SHARED)) {
18440Sstevel@tonic-gate 				if (PP_ISFREE(pp) ||
18450Sstevel@tonic-gate 				    !(pp->p_vnode == vp &&
18460Sstevel@tonic-gate 				    pp->p_offset == baseoff)) {
18470Sstevel@tonic-gate 					page_unlock(pp);
18480Sstevel@tonic-gate 					pp = page_lookup(vp, baseoff,
18490Sstevel@tonic-gate 						SE_SHARED);
18500Sstevel@tonic-gate 				}
18510Sstevel@tonic-gate 			} else {
18520Sstevel@tonic-gate 				pp = page_lookup(vp, baseoff, SE_SHARED);
18530Sstevel@tonic-gate 			}
18540Sstevel@tonic-gate 
18550Sstevel@tonic-gate 			if (pp == NULL) {
18560Sstevel@tonic-gate 				ASSERT(GET_KPME(smp)->kpe_page == NULL);
18570Sstevel@tonic-gate 				break;
18580Sstevel@tonic-gate 			}
18590Sstevel@tonic-gate 
18600Sstevel@tonic-gate 			if (rw == S_WRITE &&
18610Sstevel@tonic-gate 			    hat_page_getattr(pp, P_MOD | P_REF) !=
18620Sstevel@tonic-gate 			    (P_MOD | P_REF)) {
18630Sstevel@tonic-gate 				page_unlock(pp);
18640Sstevel@tonic-gate 				break;
18650Sstevel@tonic-gate 			}
18660Sstevel@tonic-gate 
18670Sstevel@tonic-gate 			/*
18680Sstevel@tonic-gate 			 * We have the p_selock as reader, grab_smp
18690Sstevel@tonic-gate 			 * can't hit us, we have bumped the smap
18700Sstevel@tonic-gate 			 * refcnt and hat_pageunload needs the
18710Sstevel@tonic-gate 			 * p_selock exclusive.
18720Sstevel@tonic-gate 			 */
18730Sstevel@tonic-gate 			kpme = GET_KPME(smp);
18740Sstevel@tonic-gate 			if (kpme->kpe_page == pp) {
18750Sstevel@tonic-gate 				baseaddr = hat_kpm_page2va(pp, 0);
18760Sstevel@tonic-gate 			} else if (kpme->kpe_page == NULL) {
18770Sstevel@tonic-gate 				baseaddr = hat_kpm_mapin(pp, kpme);
18780Sstevel@tonic-gate 			} else {
18790Sstevel@tonic-gate 				panic("segmap_getmapflt: stale "
18800Sstevel@tonic-gate 				    "kpme page, kpme %p", (void *)kpme);
18810Sstevel@tonic-gate 				/*NOTREACHED*/
18820Sstevel@tonic-gate 			}
18830Sstevel@tonic-gate 
18840Sstevel@tonic-gate 			/*
18850Sstevel@tonic-gate 			 * We don't invoke segmap_fault via TLB miss,
18860Sstevel@tonic-gate 			 * so we set ref and mod bits in advance.
18870Sstevel@tonic-gate 			 * For S_OTHER and we set them in segmap_fault
18880Sstevel@tonic-gate 			 * F_SOFTUNLOCK.
18890Sstevel@tonic-gate 			 */
18900Sstevel@tonic-gate 			if (rw == S_READ && !hat_isref(pp))
18910Sstevel@tonic-gate 				hat_setref(pp);
18920Sstevel@tonic-gate 
18930Sstevel@tonic-gate 			return (baseaddr);
18940Sstevel@tonic-gate 		default:
18950Sstevel@tonic-gate 			break;
18960Sstevel@tonic-gate 		}
18970Sstevel@tonic-gate 	}
18980Sstevel@tonic-gate 
18990Sstevel@tonic-gate 	base = segkpm_create_va(baseoff);
19000Sstevel@tonic-gate 	error = VOP_GETPAGE(vp, (offset_t)baseoff, len, &prot, pl, MAXBSIZE,
19010Sstevel@tonic-gate 	    seg, base, rw, CRED());
19020Sstevel@tonic-gate 
19030Sstevel@tonic-gate 	pp = pl[0];
19040Sstevel@tonic-gate 	if (error || pp == NULL) {
19050Sstevel@tonic-gate 		/*
19060Sstevel@tonic-gate 		 * Use segmap address slot and let segmap_fault deal
19070Sstevel@tonic-gate 		 * with the error cases. There is no error return
19080Sstevel@tonic-gate 		 * possible here.
19090Sstevel@tonic-gate 		 */
19100Sstevel@tonic-gate 		goto use_segmap_range;
19110Sstevel@tonic-gate 	}
19120Sstevel@tonic-gate 
19130Sstevel@tonic-gate 	ASSERT(pl[1] == NULL);
19140Sstevel@tonic-gate 
19150Sstevel@tonic-gate 	/*
19160Sstevel@tonic-gate 	 * When prot is not returned w/ PROT_ALL the returned pages
19170Sstevel@tonic-gate 	 * are not backed by fs blocks. For most of the segmap users
19180Sstevel@tonic-gate 	 * this is no problem, they don't write to the pages in the
19190Sstevel@tonic-gate 	 * same request and therefore don't rely on a following
19200Sstevel@tonic-gate 	 * trap driven segmap_fault. With SM_LOCKPROTO users it
19210Sstevel@tonic-gate 	 * is more secure to use segkmap adresses to allow
19220Sstevel@tonic-gate 	 * protection segmap_fault's.
19230Sstevel@tonic-gate 	 */
19240Sstevel@tonic-gate 	if (prot != PROT_ALL && forcefault == SM_LOCKPROTO) {
19250Sstevel@tonic-gate 		/*
19260Sstevel@tonic-gate 		 * Use segmap address slot and let segmap_fault
19270Sstevel@tonic-gate 		 * do the error return.
19280Sstevel@tonic-gate 		 */
19290Sstevel@tonic-gate 		ASSERT(rw != S_WRITE);
19300Sstevel@tonic-gate 		ASSERT(PAGE_LOCKED(pp));
19310Sstevel@tonic-gate 		page_unlock(pp);
19320Sstevel@tonic-gate 		forcefault = 0;
19330Sstevel@tonic-gate 		goto use_segmap_range;
19340Sstevel@tonic-gate 	}
19350Sstevel@tonic-gate 
19360Sstevel@tonic-gate 	/*
19370Sstevel@tonic-gate 	 * We have the p_selock as reader, grab_smp can't hit us, we
19380Sstevel@tonic-gate 	 * have bumped the smap refcnt and hat_pageunload needs the
19390Sstevel@tonic-gate 	 * p_selock exclusive.
19400Sstevel@tonic-gate 	 */
19410Sstevel@tonic-gate 	kpme = GET_KPME(smp);
19420Sstevel@tonic-gate 	if (kpme->kpe_page == pp) {
19430Sstevel@tonic-gate 		baseaddr = hat_kpm_page2va(pp, 0);
19440Sstevel@tonic-gate 	} else if (kpme->kpe_page == NULL) {
19450Sstevel@tonic-gate 		baseaddr = hat_kpm_mapin(pp, kpme);
19460Sstevel@tonic-gate 	} else {
19470Sstevel@tonic-gate 		panic("segmap_getmapflt: stale kpme page after "
19480Sstevel@tonic-gate 		    "VOP_GETPAGE, kpme %p", (void *)kpme);
19490Sstevel@tonic-gate 		/*NOTREACHED*/
19500Sstevel@tonic-gate 	}
19510Sstevel@tonic-gate 
19520Sstevel@tonic-gate 	smd_cpu[CPU->cpu_seqid].scpu.scpu_fault++;
19530Sstevel@tonic-gate 
19540Sstevel@tonic-gate 	return (baseaddr);
19550Sstevel@tonic-gate 
19560Sstevel@tonic-gate 
19570Sstevel@tonic-gate use_segmap_range:
19580Sstevel@tonic-gate 	baseaddr = seg->s_base + ((smp - smd_smap) * MAXBSIZE);
19590Sstevel@tonic-gate 	TRACE_4(TR_FAC_VM, TR_SEGMAP_GETMAP,
19600Sstevel@tonic-gate 	    "segmap_getmap:seg %p addr %p vp %p offset %llx",
19610Sstevel@tonic-gate 	    seg, baseaddr, vp, baseoff);
19620Sstevel@tonic-gate 
19630Sstevel@tonic-gate 	/*
19640Sstevel@tonic-gate 	 * Prefault the translations
19650Sstevel@tonic-gate 	 */
19660Sstevel@tonic-gate 	vaddr = baseaddr + (off - baseoff);
19670Sstevel@tonic-gate 	if (forcefault && (newslot || !hat_probe(kas.a_hat, vaddr))) {
19680Sstevel@tonic-gate 
19690Sstevel@tonic-gate 		caddr_t pgaddr = (caddr_t)((uintptr_t)vaddr &
19700Sstevel@tonic-gate 		    (uintptr_t)PAGEMASK);
19710Sstevel@tonic-gate 
19720Sstevel@tonic-gate 		(void) segmap_fault(kas.a_hat, seg, pgaddr,
19730Sstevel@tonic-gate 		    (vaddr + len - pgaddr + PAGESIZE - 1) & (uintptr_t)PAGEMASK,
19740Sstevel@tonic-gate 		    F_INVAL, rw);
19750Sstevel@tonic-gate 	}
19760Sstevel@tonic-gate 
19770Sstevel@tonic-gate 	return (baseaddr);
19780Sstevel@tonic-gate }
19790Sstevel@tonic-gate 
19800Sstevel@tonic-gate int
19810Sstevel@tonic-gate segmap_release(struct seg *seg, caddr_t addr, uint_t flags)
19820Sstevel@tonic-gate {
19830Sstevel@tonic-gate 	struct smap	*smp;
19840Sstevel@tonic-gate 	int 		error;
19850Sstevel@tonic-gate 	int		bflags = 0;
19860Sstevel@tonic-gate 	struct vnode	*vp;
19870Sstevel@tonic-gate 	u_offset_t	offset;
19880Sstevel@tonic-gate 	kmutex_t	*smtx;
19890Sstevel@tonic-gate 	int		is_kpm = 0;
19900Sstevel@tonic-gate 	page_t		*pp;
19910Sstevel@tonic-gate 
19920Sstevel@tonic-gate 	if (segmap_kpm && IS_KPM_ADDR(addr)) {
19930Sstevel@tonic-gate 
19940Sstevel@tonic-gate 		if (((uintptr_t)addr & MAXBOFFSET) != 0) {
19950Sstevel@tonic-gate 			panic("segmap_release: addr %p not "
19960Sstevel@tonic-gate 			    "MAXBSIZE aligned", (void *)addr);
19970Sstevel@tonic-gate 			/*NOTREACHED*/
19980Sstevel@tonic-gate 		}
19990Sstevel@tonic-gate 
20000Sstevel@tonic-gate 		if ((smp = get_smap_kpm(addr, &pp)) == NULL) {
20010Sstevel@tonic-gate 			panic("segmap_release: smap not found "
20020Sstevel@tonic-gate 			    "for addr %p", (void *)addr);
20030Sstevel@tonic-gate 			/*NOTREACHED*/
20040Sstevel@tonic-gate 		}
20050Sstevel@tonic-gate 
20060Sstevel@tonic-gate 		TRACE_3(TR_FAC_VM, TR_SEGMAP_RELMAP,
20070Sstevel@tonic-gate 			"segmap_relmap:seg %p addr %p smp %p",
20080Sstevel@tonic-gate 			seg, addr, smp);
20090Sstevel@tonic-gate 
20100Sstevel@tonic-gate 		smtx = SMAPMTX(smp);
20110Sstevel@tonic-gate 
20120Sstevel@tonic-gate 		/*
20130Sstevel@tonic-gate 		 * For compatibilty reasons segmap_pagecreate_kpm sets this
20140Sstevel@tonic-gate 		 * flag to allow a following segmap_pagecreate to return
20150Sstevel@tonic-gate 		 * this as "newpage" flag. When segmap_pagecreate is not
20160Sstevel@tonic-gate 		 * called at all we clear it now.
20170Sstevel@tonic-gate 		 */
20180Sstevel@tonic-gate 		smp->sm_flags &= ~SM_KPM_NEWPAGE;
20190Sstevel@tonic-gate 		is_kpm = 1;
20200Sstevel@tonic-gate 		if (smp->sm_flags & SM_WRITE_DATA) {
20210Sstevel@tonic-gate 			hat_setrefmod(pp);
20220Sstevel@tonic-gate 		} else if (smp->sm_flags & SM_READ_DATA) {
20230Sstevel@tonic-gate 			hat_setref(pp);
20240Sstevel@tonic-gate 		}
20250Sstevel@tonic-gate 	} else {
20260Sstevel@tonic-gate 		if (addr < seg->s_base || addr >= seg->s_base + seg->s_size ||
20270Sstevel@tonic-gate 		    ((uintptr_t)addr & MAXBOFFSET) != 0) {
20280Sstevel@tonic-gate 			panic("segmap_release: bad addr %p", (void *)addr);
20290Sstevel@tonic-gate 			/*NOTREACHED*/
20300Sstevel@tonic-gate 		}
20310Sstevel@tonic-gate 		smp = GET_SMAP(seg, addr);
20320Sstevel@tonic-gate 
20330Sstevel@tonic-gate 		TRACE_3(TR_FAC_VM, TR_SEGMAP_RELMAP,
20340Sstevel@tonic-gate 			"segmap_relmap:seg %p addr %p smp %p",
20350Sstevel@tonic-gate 			seg, addr, smp);
20360Sstevel@tonic-gate 
20370Sstevel@tonic-gate 		smtx = SMAPMTX(smp);
20380Sstevel@tonic-gate 		mutex_enter(smtx);
20390Sstevel@tonic-gate 		smp->sm_flags |= SM_NOTKPM_RELEASED;
20400Sstevel@tonic-gate 	}
20410Sstevel@tonic-gate 
20420Sstevel@tonic-gate 	ASSERT(smp->sm_refcnt > 0);
20430Sstevel@tonic-gate 
20440Sstevel@tonic-gate 	/*
20450Sstevel@tonic-gate 	 * Need to call VOP_PUTPAGE() if any flags (except SM_DONTNEED)
20460Sstevel@tonic-gate 	 * are set.
20470Sstevel@tonic-gate 	 */
20480Sstevel@tonic-gate 	if ((flags & ~SM_DONTNEED) != 0) {
20490Sstevel@tonic-gate 		if (flags & SM_WRITE)
20500Sstevel@tonic-gate 			segmapcnt.smp_rel_write.value.ul++;
20510Sstevel@tonic-gate 		if (flags & SM_ASYNC) {
20520Sstevel@tonic-gate 			bflags |= B_ASYNC;
20530Sstevel@tonic-gate 			segmapcnt.smp_rel_async.value.ul++;
20540Sstevel@tonic-gate 		}
20550Sstevel@tonic-gate 		if (flags & SM_INVAL) {
20560Sstevel@tonic-gate 			bflags |= B_INVAL;
20570Sstevel@tonic-gate 			segmapcnt.smp_rel_abort.value.ul++;
20580Sstevel@tonic-gate 		}
20590Sstevel@tonic-gate 		if (flags & SM_DESTROY) {
20600Sstevel@tonic-gate 			bflags |= (B_INVAL|B_TRUNC);
20610Sstevel@tonic-gate 			segmapcnt.smp_rel_abort.value.ul++;
20620Sstevel@tonic-gate 		}
20630Sstevel@tonic-gate 		if (smp->sm_refcnt == 1) {
20640Sstevel@tonic-gate 			/*
20650Sstevel@tonic-gate 			 * We only bother doing the FREE and DONTNEED flags
20660Sstevel@tonic-gate 			 * if no one else is still referencing this mapping.
20670Sstevel@tonic-gate 			 */
20680Sstevel@tonic-gate 			if (flags & SM_FREE) {
20690Sstevel@tonic-gate 				bflags |= B_FREE;
20700Sstevel@tonic-gate 				segmapcnt.smp_rel_free.value.ul++;
20710Sstevel@tonic-gate 			}
20720Sstevel@tonic-gate 			if (flags & SM_DONTNEED) {
20730Sstevel@tonic-gate 				bflags |= B_DONTNEED;
20740Sstevel@tonic-gate 				segmapcnt.smp_rel_dontneed.value.ul++;
20750Sstevel@tonic-gate 			}
20760Sstevel@tonic-gate 		}
20770Sstevel@tonic-gate 	} else {
20780Sstevel@tonic-gate 		smd_cpu[CPU->cpu_seqid].scpu.scpu_release++;
20790Sstevel@tonic-gate 	}
20800Sstevel@tonic-gate 
20810Sstevel@tonic-gate 	vp = smp->sm_vp;
20820Sstevel@tonic-gate 	offset = smp->sm_off;
20830Sstevel@tonic-gate 
20840Sstevel@tonic-gate 	if (--smp->sm_refcnt == 0) {
20850Sstevel@tonic-gate 
2086848Sstans 		smp->sm_flags &= ~(SM_WRITE_DATA | SM_READ_DATA);
2087848Sstans 
20880Sstevel@tonic-gate 		if (flags & (SM_INVAL|SM_DESTROY)) {
20890Sstevel@tonic-gate 			segmap_hashout(smp);	/* remove map info */
20900Sstevel@tonic-gate 			if (is_kpm) {
20910Sstevel@tonic-gate 				hat_kpm_mapout(pp, GET_KPME(smp), addr);
20920Sstevel@tonic-gate 				if (smp->sm_flags & SM_NOTKPM_RELEASED) {
20930Sstevel@tonic-gate 					smp->sm_flags &= ~SM_NOTKPM_RELEASED;
20940Sstevel@tonic-gate 					hat_unload(kas.a_hat, addr, MAXBSIZE,
20950Sstevel@tonic-gate 						HAT_UNLOAD);
20960Sstevel@tonic-gate 				}
20970Sstevel@tonic-gate 
20980Sstevel@tonic-gate 			} else {
20990Sstevel@tonic-gate 				if (segmap_kpm)
21000Sstevel@tonic-gate 					segkpm_mapout_validkpme(GET_KPME(smp));
21010Sstevel@tonic-gate 
21020Sstevel@tonic-gate 				smp->sm_flags &= ~SM_NOTKPM_RELEASED;
21030Sstevel@tonic-gate 				hat_unload(kas.a_hat, addr, MAXBSIZE,
21040Sstevel@tonic-gate 					HAT_UNLOAD);
21050Sstevel@tonic-gate 			}
21060Sstevel@tonic-gate 		}
21070Sstevel@tonic-gate 		segmap_smapadd(smp);	/* add to free list */
21080Sstevel@tonic-gate 	}
21090Sstevel@tonic-gate 
21100Sstevel@tonic-gate 	mutex_exit(smtx);
21110Sstevel@tonic-gate 
21120Sstevel@tonic-gate 	if (is_kpm)
21130Sstevel@tonic-gate 		page_unlock(pp);
21140Sstevel@tonic-gate 	/*
21150Sstevel@tonic-gate 	 * Now invoke VOP_PUTPAGE() if any flags (except SM_DONTNEED)
21160Sstevel@tonic-gate 	 * are set.
21170Sstevel@tonic-gate 	 */
21180Sstevel@tonic-gate 	if ((flags & ~SM_DONTNEED) != 0) {
21190Sstevel@tonic-gate 		error = VOP_PUTPAGE(vp, offset, MAXBSIZE,
21200Sstevel@tonic-gate 		    bflags, CRED());
21210Sstevel@tonic-gate 	} else {
21220Sstevel@tonic-gate 		error = 0;
21230Sstevel@tonic-gate 	}
21240Sstevel@tonic-gate 
21250Sstevel@tonic-gate 	return (error);
21260Sstevel@tonic-gate }
21270Sstevel@tonic-gate 
21280Sstevel@tonic-gate /*
21290Sstevel@tonic-gate  * Dump the pages belonging to this segmap segment.
21300Sstevel@tonic-gate  */
21310Sstevel@tonic-gate static void
21320Sstevel@tonic-gate segmap_dump(struct seg *seg)
21330Sstevel@tonic-gate {
21340Sstevel@tonic-gate 	struct segmap_data *smd;
21350Sstevel@tonic-gate 	struct smap *smp, *smp_end;
21360Sstevel@tonic-gate 	page_t *pp;
21370Sstevel@tonic-gate 	pfn_t pfn;
21380Sstevel@tonic-gate 	u_offset_t off;
21390Sstevel@tonic-gate 	caddr_t addr;
21400Sstevel@tonic-gate 
21410Sstevel@tonic-gate 	smd = (struct segmap_data *)seg->s_data;
21420Sstevel@tonic-gate 	addr = seg->s_base;
21430Sstevel@tonic-gate 	for (smp = smd->smd_sm, smp_end = smp + smd->smd_npages;
21440Sstevel@tonic-gate 	    smp < smp_end; smp++) {
21450Sstevel@tonic-gate 
21460Sstevel@tonic-gate 		if (smp->sm_refcnt) {
21470Sstevel@tonic-gate 			for (off = 0; off < MAXBSIZE; off += PAGESIZE) {
21480Sstevel@tonic-gate 				int we_own_it = 0;
21490Sstevel@tonic-gate 
21500Sstevel@tonic-gate 				/*
21510Sstevel@tonic-gate 				 * If pp == NULL, the page either does
21520Sstevel@tonic-gate 				 * not exist or is exclusively locked.
21530Sstevel@tonic-gate 				 * So determine if it exists before
21540Sstevel@tonic-gate 				 * searching for it.
21550Sstevel@tonic-gate 				 */
21560Sstevel@tonic-gate 				if ((pp = page_lookup_nowait(smp->sm_vp,
21570Sstevel@tonic-gate 				    smp->sm_off + off, SE_SHARED)))
21580Sstevel@tonic-gate 					we_own_it = 1;
21590Sstevel@tonic-gate 				else
21600Sstevel@tonic-gate 					pp = page_exists(smp->sm_vp,
21610Sstevel@tonic-gate 					    smp->sm_off + off);
21620Sstevel@tonic-gate 
21630Sstevel@tonic-gate 				if (pp) {
21640Sstevel@tonic-gate 					pfn = page_pptonum(pp);
21650Sstevel@tonic-gate 					dump_addpage(seg->s_as,
21660Sstevel@tonic-gate 						addr + off, pfn);
21670Sstevel@tonic-gate 					if (we_own_it)
21680Sstevel@tonic-gate 						page_unlock(pp);
21690Sstevel@tonic-gate 				}
21700Sstevel@tonic-gate 				dump_timeleft = dump_timeout;
21710Sstevel@tonic-gate 			}
21720Sstevel@tonic-gate 		}
21730Sstevel@tonic-gate 		addr += MAXBSIZE;
21740Sstevel@tonic-gate 	}
21750Sstevel@tonic-gate }
21760Sstevel@tonic-gate 
21770Sstevel@tonic-gate /*ARGSUSED*/
21780Sstevel@tonic-gate static int
21790Sstevel@tonic-gate segmap_pagelock(struct seg *seg, caddr_t addr, size_t len,
21800Sstevel@tonic-gate     struct page ***ppp, enum lock_type type, enum seg_rw rw)
21810Sstevel@tonic-gate {
21820Sstevel@tonic-gate 	return (ENOTSUP);
21830Sstevel@tonic-gate }
21840Sstevel@tonic-gate 
21850Sstevel@tonic-gate static int
21860Sstevel@tonic-gate segmap_getmemid(struct seg *seg, caddr_t addr, memid_t *memidp)
21870Sstevel@tonic-gate {
21880Sstevel@tonic-gate 	struct segmap_data *smd = (struct segmap_data *)seg->s_data;
21890Sstevel@tonic-gate 
21900Sstevel@tonic-gate 	memidp->val[0] = (uintptr_t)smd->smd_sm->sm_vp;
21910Sstevel@tonic-gate 	memidp->val[1] = smd->smd_sm->sm_off + (uintptr_t)(addr - seg->s_base);
21920Sstevel@tonic-gate 	return (0);
21930Sstevel@tonic-gate }
21940Sstevel@tonic-gate 
21950Sstevel@tonic-gate /*ARGSUSED*/
21960Sstevel@tonic-gate static lgrp_mem_policy_info_t *
21970Sstevel@tonic-gate segmap_getpolicy(struct seg *seg, caddr_t addr)
21980Sstevel@tonic-gate {
21990Sstevel@tonic-gate 	return (NULL);
22000Sstevel@tonic-gate }
22010Sstevel@tonic-gate 
2202670Selowe /*ARGSUSED*/
2203670Selowe static int
2204670Selowe segmap_capable(struct seg *seg, segcapability_t capability)
2205670Selowe {
2206670Selowe 	return (0);
2207670Selowe }
2208670Selowe 
22090Sstevel@tonic-gate 
22100Sstevel@tonic-gate #ifdef	SEGKPM_SUPPORT
22110Sstevel@tonic-gate 
22120Sstevel@tonic-gate /*
22130Sstevel@tonic-gate  * segkpm support routines
22140Sstevel@tonic-gate  */
22150Sstevel@tonic-gate 
22160Sstevel@tonic-gate static caddr_t
22170Sstevel@tonic-gate segmap_pagecreate_kpm(struct seg *seg, vnode_t *vp, u_offset_t off,
22180Sstevel@tonic-gate 	struct smap *smp, enum seg_rw rw)
22190Sstevel@tonic-gate {
22200Sstevel@tonic-gate 	caddr_t	base;
22210Sstevel@tonic-gate 	page_t	*pp;
22220Sstevel@tonic-gate 	int	newpage = 0;
22230Sstevel@tonic-gate 	struct kpme	*kpme;
22240Sstevel@tonic-gate 
22250Sstevel@tonic-gate 	ASSERT(smp->sm_refcnt > 0);
22260Sstevel@tonic-gate 
22270Sstevel@tonic-gate 	if ((pp = page_lookup(vp, off, SE_SHARED)) == NULL) {
22280Sstevel@tonic-gate 		kmutex_t *smtx;
22290Sstevel@tonic-gate 
22300Sstevel@tonic-gate 		base = segkpm_create_va(off);
22310Sstevel@tonic-gate 
22320Sstevel@tonic-gate 		if ((pp = page_create_va(vp, off, PAGESIZE, PG_WAIT,
22330Sstevel@tonic-gate 		    seg, base)) == NULL) {
22340Sstevel@tonic-gate 			panic("segmap_pagecreate_kpm: "
22350Sstevel@tonic-gate 			    "page_create failed");
22360Sstevel@tonic-gate 			/*NOTREACHED*/
22370Sstevel@tonic-gate 		}
22380Sstevel@tonic-gate 
22390Sstevel@tonic-gate 		newpage = 1;
22400Sstevel@tonic-gate 		page_io_unlock(pp);
22410Sstevel@tonic-gate 		ASSERT((u_offset_t)(off - smp->sm_off) <= INT_MAX);
22420Sstevel@tonic-gate 
22430Sstevel@tonic-gate 		/*
22440Sstevel@tonic-gate 		 * Mark this here until the following segmap_pagecreate
22450Sstevel@tonic-gate 		 * or segmap_release.
22460Sstevel@tonic-gate 		 */
22470Sstevel@tonic-gate 		smtx = SMAPMTX(smp);
22480Sstevel@tonic-gate 		mutex_enter(smtx);
22490Sstevel@tonic-gate 		smp->sm_flags |= SM_KPM_NEWPAGE;
22500Sstevel@tonic-gate 		mutex_exit(smtx);
22510Sstevel@tonic-gate 	}
22520Sstevel@tonic-gate 
22530Sstevel@tonic-gate 	kpme = GET_KPME(smp);
22540Sstevel@tonic-gate 	if (!newpage && kpme->kpe_page == pp)
22550Sstevel@tonic-gate 		base = hat_kpm_page2va(pp, 0);
22560Sstevel@tonic-gate 	else
22570Sstevel@tonic-gate 		base = hat_kpm_mapin(pp, kpme);
22580Sstevel@tonic-gate 
22590Sstevel@tonic-gate 	/*
22600Sstevel@tonic-gate 	 * FS code may decide not to call segmap_pagecreate and we
22610Sstevel@tonic-gate 	 * don't invoke segmap_fault via TLB miss, so we have to set
22620Sstevel@tonic-gate 	 * ref and mod bits in advance.
22630Sstevel@tonic-gate 	 */
22640Sstevel@tonic-gate 	if (rw == S_WRITE) {
22650Sstevel@tonic-gate 		hat_setrefmod(pp);
22660Sstevel@tonic-gate 	} else {
22670Sstevel@tonic-gate 		ASSERT(rw == S_READ);
22680Sstevel@tonic-gate 		hat_setref(pp);
22690Sstevel@tonic-gate 	}
22700Sstevel@tonic-gate 
22710Sstevel@tonic-gate 	smd_cpu[CPU->cpu_seqid].scpu.scpu_pagecreate++;
22720Sstevel@tonic-gate 
22730Sstevel@tonic-gate 	return (base);
22740Sstevel@tonic-gate }
22750Sstevel@tonic-gate 
22760Sstevel@tonic-gate /*
22770Sstevel@tonic-gate  * Find the smap structure corresponding to the
22780Sstevel@tonic-gate  * KPM addr and return it locked.
22790Sstevel@tonic-gate  */
22800Sstevel@tonic-gate struct smap *
22810Sstevel@tonic-gate get_smap_kpm(caddr_t addr, page_t **ppp)
22820Sstevel@tonic-gate {
22830Sstevel@tonic-gate 	struct smap	*smp;
22840Sstevel@tonic-gate 	struct vnode	*vp;
22850Sstevel@tonic-gate 	u_offset_t	offset;
22860Sstevel@tonic-gate 	caddr_t		baseaddr = (caddr_t)((uintptr_t)addr & MAXBMASK);
22870Sstevel@tonic-gate 	int		hashid;
22880Sstevel@tonic-gate 	kmutex_t	*hashmtx;
22890Sstevel@tonic-gate 	page_t		*pp;
22900Sstevel@tonic-gate 	union segmap_cpu *scpu;
22910Sstevel@tonic-gate 
22920Sstevel@tonic-gate 	pp = hat_kpm_vaddr2page(baseaddr);
22930Sstevel@tonic-gate 
22940Sstevel@tonic-gate 	ASSERT(pp && !PP_ISFREE(pp));
22950Sstevel@tonic-gate 	ASSERT(PAGE_LOCKED(pp));
22960Sstevel@tonic-gate 	ASSERT(((uintptr_t)pp->p_offset & MAXBOFFSET) == 0);
22970Sstevel@tonic-gate 
22980Sstevel@tonic-gate 	vp = pp->p_vnode;
22990Sstevel@tonic-gate 	offset = pp->p_offset;
23000Sstevel@tonic-gate 	ASSERT(vp != NULL);
23010Sstevel@tonic-gate 
23020Sstevel@tonic-gate 	/*
23030Sstevel@tonic-gate 	 * Assume the last smap used on this cpu is the one needed.
23040Sstevel@tonic-gate 	 */
23050Sstevel@tonic-gate 	scpu = smd_cpu+CPU->cpu_seqid;
23060Sstevel@tonic-gate 	smp = scpu->scpu.scpu_last_smap;
23070Sstevel@tonic-gate 	mutex_enter(&smp->sm_mtx);
23080Sstevel@tonic-gate 	if (smp->sm_vp == vp && smp->sm_off == offset) {
23090Sstevel@tonic-gate 		ASSERT(smp->sm_refcnt > 0);
23100Sstevel@tonic-gate 	} else {
23110Sstevel@tonic-gate 		/*
23120Sstevel@tonic-gate 		 * Assumption wrong, find the smap on the hash chain.
23130Sstevel@tonic-gate 		 */
23140Sstevel@tonic-gate 		mutex_exit(&smp->sm_mtx);
23150Sstevel@tonic-gate 		SMAP_HASHFUNC(vp, offset, hashid); /* macro assigns hashid */
23160Sstevel@tonic-gate 		hashmtx = SHASHMTX(hashid);
23170Sstevel@tonic-gate 
23180Sstevel@tonic-gate 		mutex_enter(hashmtx);
23190Sstevel@tonic-gate 		smp = smd_hash[hashid].sh_hash_list;
23200Sstevel@tonic-gate 		for (; smp != NULL; smp = smp->sm_hash) {
23210Sstevel@tonic-gate 			if (smp->sm_vp == vp && smp->sm_off == offset)
23220Sstevel@tonic-gate 				break;
23230Sstevel@tonic-gate 		}
23240Sstevel@tonic-gate 		mutex_exit(hashmtx);
23250Sstevel@tonic-gate 		if (smp) {
23260Sstevel@tonic-gate 			mutex_enter(&smp->sm_mtx);
23270Sstevel@tonic-gate 			ASSERT(smp->sm_vp == vp && smp->sm_off == offset);
23280Sstevel@tonic-gate 		}
23290Sstevel@tonic-gate 	}
23300Sstevel@tonic-gate 
23310Sstevel@tonic-gate 	if (ppp)
23320Sstevel@tonic-gate 		*ppp = smp ? pp : NULL;
23330Sstevel@tonic-gate 
23340Sstevel@tonic-gate 	return (smp);
23350Sstevel@tonic-gate }
23360Sstevel@tonic-gate 
23370Sstevel@tonic-gate #else	/* SEGKPM_SUPPORT */
23380Sstevel@tonic-gate 
23390Sstevel@tonic-gate /* segkpm stubs */
23400Sstevel@tonic-gate 
23410Sstevel@tonic-gate /*ARGSUSED*/
23420Sstevel@tonic-gate static caddr_t
23430Sstevel@tonic-gate segmap_pagecreate_kpm(struct seg *seg, vnode_t *vp, u_offset_t off,
23440Sstevel@tonic-gate 	struct smap *smp, enum seg_rw rw)
23450Sstevel@tonic-gate {
23460Sstevel@tonic-gate 	return (NULL);
23470Sstevel@tonic-gate }
23480Sstevel@tonic-gate 
23490Sstevel@tonic-gate /*ARGSUSED*/
23500Sstevel@tonic-gate struct smap *
23510Sstevel@tonic-gate get_smap_kpm(caddr_t addr, page_t **ppp)
23520Sstevel@tonic-gate {
23530Sstevel@tonic-gate 	return (NULL);
23540Sstevel@tonic-gate }
23550Sstevel@tonic-gate 
23560Sstevel@tonic-gate #endif	/* SEGKPM_SUPPORT */
2357