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
53085Strevtom  * Common Development and Distribution License (the "License").
63085Strevtom  * 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 /*
223085Strevtom  * 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * The following routines implement the hat layer's
300Sstevel@tonic-gate  * recording of the referenced and modified bits.
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <sys/param.h>
350Sstevel@tonic-gate #include <sys/systm.h>
360Sstevel@tonic-gate #include <sys/debug.h>
370Sstevel@tonic-gate #include <sys/kmem.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate /*
400Sstevel@tonic-gate  * Note, usage of cmn_err requires you not hold any hat layer locks.
410Sstevel@tonic-gate  */
420Sstevel@tonic-gate #include <sys/cmn_err.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include <vm/as.h>
450Sstevel@tonic-gate #include <vm/hat.h>
460Sstevel@tonic-gate 
470Sstevel@tonic-gate kmutex_t hat_statlock;		/* protects all hat statistics data */
480Sstevel@tonic-gate struct hrmstat *hrm_memlist;	/* tracks memory alloced for hrm_blist blocks */
490Sstevel@tonic-gate struct hrmstat **hrm_hashtab;	/* hash table for finding blocks quickly */
500Sstevel@tonic-gate struct hrmstat *hrm_blist;
510Sstevel@tonic-gate int hrm_blist_incr = HRM_BLIST_INCR;
520Sstevel@tonic-gate int hrm_blist_lowater = HRM_BLIST_INCR/2;
530Sstevel@tonic-gate int hrm_blist_num = 0;
540Sstevel@tonic-gate int hrm_blist_total = 0;
550Sstevel@tonic-gate int hrm_mlockinited = 0;
560Sstevel@tonic-gate int hrm_allocfailmsg = 0;	/* print a message when allocations fail */
570Sstevel@tonic-gate int hrm_allocfail = 0;
580Sstevel@tonic-gate 
590Sstevel@tonic-gate static struct hrmstat	*hrm_balloc(void);
600Sstevel@tonic-gate static void	hrm_link(struct hrmstat *);
610Sstevel@tonic-gate static void	hrm_setbits(struct hrmstat *, caddr_t, uint_t);
620Sstevel@tonic-gate static void	hrm_hashout(struct hrmstat *);
630Sstevel@tonic-gate static void	hrm_getblk(int);
640Sstevel@tonic-gate 
650Sstevel@tonic-gate #define	hrm_hash(as, addr) \
660Sstevel@tonic-gate 	(HRM_HASHMASK & \
670Sstevel@tonic-gate 	(((uintptr_t)(addr) >> HRM_BASESHIFT) ^ ((uintptr_t)(as) >> 2)))
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #define	hrm_match(hrm, as, addr) \
700Sstevel@tonic-gate 	(((hrm)->hrm_as == (as) && \
710Sstevel@tonic-gate 	((hrm)->hrm_base == ((uintptr_t)(addr) & HRM_BASEMASK))) ? 1 : 0)
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate  * reserve enough statistic blocks for
750Sstevel@tonic-gate  * chunk of bytes (pages) in a given as.
760Sstevel@tonic-gate  */
770Sstevel@tonic-gate /* ARGSUSED */
780Sstevel@tonic-gate void
790Sstevel@tonic-gate hat_resvstat(size_t chunk, struct as *as, caddr_t addr)
800Sstevel@tonic-gate {
810Sstevel@tonic-gate 	int nhrm = btop(chunk)/HRM_PAGES;
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	if (nhrm < HRM_BLIST_INCR)
840Sstevel@tonic-gate 		nhrm = 0;	/* preallocate at least HRM_BLIST_INCR */
850Sstevel@tonic-gate 	hrm_getblk(nhrm);
860Sstevel@tonic-gate }
870Sstevel@tonic-gate 
880Sstevel@tonic-gate /*
890Sstevel@tonic-gate  * Start the statistics gathering for an address space.
900Sstevel@tonic-gate  * Return -1 if we can't do it, otherwise return an opaque
910Sstevel@tonic-gate  * identifier to be used when querying for the gathered statistics.
920Sstevel@tonic-gate  * The identifier is an unused bit in a_vbits.
930Sstevel@tonic-gate  * Bit 0 is reserved for swsmon.
940Sstevel@tonic-gate  */
950Sstevel@tonic-gate int
960Sstevel@tonic-gate hat_startstat(struct as *as)
970Sstevel@tonic-gate {
980Sstevel@tonic-gate 	uint_t nbits;		/* number of bits */
990Sstevel@tonic-gate 	uint_t bn;		/* bit number */
1000Sstevel@tonic-gate 	uint_t id;		/* new vbit, identifier */
1010Sstevel@tonic-gate 	uint_t vbits;		/* used vbits of address space */
1020Sstevel@tonic-gate 	size_t chunk;		/* mapped size for stats */
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	/*
1050Sstevel@tonic-gate 	 * If the refmod saving memory allocator runs out, print
1060Sstevel@tonic-gate 	 * a warning message about how to fix it, see comment at
1070Sstevel@tonic-gate 	 * the beginning of hat_setstat.
1080Sstevel@tonic-gate 	 */
1090Sstevel@tonic-gate 	if (hrm_allocfailmsg) {
1100Sstevel@tonic-gate 		cmn_err(CE_WARN,
1110Sstevel@tonic-gate 		    "hrm_balloc failures occured, increase hrm_blist_incr");
1120Sstevel@tonic-gate 		hrm_allocfailmsg = 0;
1130Sstevel@tonic-gate 	}
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	/*
1160Sstevel@tonic-gate 	 * Verify that a buffer of statistics blocks exists
1170Sstevel@tonic-gate 	 * and allocate more, if needed.
1180Sstevel@tonic-gate 	 */
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	chunk = hat_get_mapped_size(as->a_hat);
1210Sstevel@tonic-gate 	chunk = (btop(chunk)/HRM_PAGES);
1220Sstevel@tonic-gate 	if (chunk < HRM_BLIST_INCR)
1230Sstevel@tonic-gate 		chunk = 0;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	hrm_getblk((int)chunk);
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	/*
1280Sstevel@tonic-gate 	 * Find a unused id in the given address space.
1290Sstevel@tonic-gate 	 */
1300Sstevel@tonic-gate 	hat_enter(as->a_hat);
1310Sstevel@tonic-gate 	vbits = as->a_vbits;
1320Sstevel@tonic-gate 	nbits = sizeof (as->a_vbits) * NBBY;
1330Sstevel@tonic-gate 	for (bn = 1, id = 2; bn < (nbits - 1); bn++, id <<= 1)
1340Sstevel@tonic-gate 		if ((id & vbits) == 0)
1350Sstevel@tonic-gate 			break;
1360Sstevel@tonic-gate 	if (bn >= (nbits - 1)) {
1370Sstevel@tonic-gate 		hat_exit(as->a_hat);
1380Sstevel@tonic-gate 		return (-1);
1390Sstevel@tonic-gate 	}
1400Sstevel@tonic-gate 	as->a_vbits |= id;
1410Sstevel@tonic-gate 	hat_exit(as->a_hat);
1420Sstevel@tonic-gate 	(void) hat_stats_enable(as->a_hat);
1430Sstevel@tonic-gate 	return (id);
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate /*
1470Sstevel@tonic-gate  * Record referenced and modified information for an address space.
1480Sstevel@tonic-gate  * Rmbits is a word containing the referenced bit in bit position 1
1490Sstevel@tonic-gate  * and the modified bit in bit position 0.
1500Sstevel@tonic-gate  *
1510Sstevel@tonic-gate  * For current informational uses, one can rerun any program using
1520Sstevel@tonic-gate  * this facility after modifying the hrm_blist_incr to be a larger
1530Sstevel@tonic-gate  * amount so that a larger buffer of blocks will be maintained.
1540Sstevel@tonic-gate  */
1550Sstevel@tonic-gate void
1560Sstevel@tonic-gate hat_setstat(struct as *as, caddr_t addr, size_t len, uint_t rmbits)
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate 	struct hrmstat	*hrm;
1590Sstevel@tonic-gate 	uint_t		vbits, newbits, nb;
1600Sstevel@tonic-gate 	int		h;
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	ASSERT(len == PAGESIZE);
1630Sstevel@tonic-gate 	ASSERT((rmbits & ~(P_MOD|P_REF)) == 0);
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	if (rmbits == 0)
1660Sstevel@tonic-gate 		return;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	mutex_enter(&hat_statlock);
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	/*
1710Sstevel@tonic-gate 	 * Search the hash list for the as and addr we are looking for
1720Sstevel@tonic-gate 	 * and set the ref and mod bits in every block that matches.
1730Sstevel@tonic-gate 	 */
1740Sstevel@tonic-gate 	vbits = 0;
1750Sstevel@tonic-gate 	h = hrm_hash(as, addr);
1760Sstevel@tonic-gate 	for (hrm = hrm_hashtab[h]; hrm; hrm = hrm->hrm_hnext) {
1770Sstevel@tonic-gate 		if (hrm_match(hrm, as, addr)) {
1780Sstevel@tonic-gate 			hrm_setbits(hrm, addr, rmbits);
1790Sstevel@tonic-gate 			vbits |= hrm->hrm_id;
1800Sstevel@tonic-gate 		}
1810Sstevel@tonic-gate 	}
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	/*
1840Sstevel@tonic-gate 	 * If we didn't find a block for all of the enabled
1850Sstevel@tonic-gate 	 * vpages bits, then allocate and initialize a block
1860Sstevel@tonic-gate 	 * for each bit that was not found.
1870Sstevel@tonic-gate 	 */
1880Sstevel@tonic-gate 	if (vbits != as->a_vbits) {
189*3258Strevtom 		newbits = (vbits ^ as->a_vbits) & as->a_vbits;
1900Sstevel@tonic-gate 		while (newbits) {
1910Sstevel@tonic-gate 			if (ffs(newbits))
1920Sstevel@tonic-gate 				nb = 1 << (ffs(newbits)-1);
1930Sstevel@tonic-gate 			hrm = (struct hrmstat *)hrm_balloc();
1940Sstevel@tonic-gate 			if (hrm == NULL) {
1950Sstevel@tonic-gate 				hrm_allocfailmsg = 1;
1960Sstevel@tonic-gate 				hrm_allocfail++;
1970Sstevel@tonic-gate 				mutex_exit(&hat_statlock);
1980Sstevel@tonic-gate 				return;
1990Sstevel@tonic-gate 			}
2000Sstevel@tonic-gate 			hrm->hrm_as = as;
2010Sstevel@tonic-gate 			hrm->hrm_base = (uintptr_t)addr & HRM_BASEMASK;
2020Sstevel@tonic-gate 			hrm->hrm_id = nb;
2030Sstevel@tonic-gate 			hrm_link(hrm);
2040Sstevel@tonic-gate 			hrm_setbits(hrm, addr, rmbits);
2050Sstevel@tonic-gate 			newbits &= ~nb;
2060Sstevel@tonic-gate 		}
2070Sstevel@tonic-gate 	}
2080Sstevel@tonic-gate 	mutex_exit(&hat_statlock);
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate /*
2120Sstevel@tonic-gate  * Free the resources used to maintain the referenced and modified
2130Sstevel@tonic-gate  * statistics for the virtual page view of an address space
2140Sstevel@tonic-gate  * identified by id.
2150Sstevel@tonic-gate  */
2160Sstevel@tonic-gate void
2170Sstevel@tonic-gate hat_freestat(struct as *as, int id)
2180Sstevel@tonic-gate {
219*3258Strevtom 	struct hrmstat *hrm;
220*3258Strevtom 	struct hrmstat *prev_ahrm;
221*3258Strevtom 	struct hrmstat *hrm_tmplist;
222*3258Strevtom 	struct hrmstat *hrm_next;
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	hat_stats_disable(as->a_hat);	/* tell the hat layer to stop */
2250Sstevel@tonic-gate 	hat_enter(as->a_hat);
2260Sstevel@tonic-gate 	if (id == 0)
2270Sstevel@tonic-gate 		as->a_vbits = 0;
2280Sstevel@tonic-gate 	else
2290Sstevel@tonic-gate 		as->a_vbits &= ~id;
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	if ((hrm = as->a_hrm) == NULL) {
2320Sstevel@tonic-gate 		hat_exit(as->a_hat);
2330Sstevel@tonic-gate 		return;
2340Sstevel@tonic-gate 	}
2350Sstevel@tonic-gate 	hat_exit(as->a_hat);
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	mutex_enter(&hat_statlock);
238*3258Strevtom 
2390Sstevel@tonic-gate 	for (prev_ahrm = NULL; hrm; hrm = hrm->hrm_anext) {
2400Sstevel@tonic-gate 		if ((id == hrm->hrm_id) || (id == NULL)) {
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 			hrm_hashout(hrm);
2430Sstevel@tonic-gate 			hrm->hrm_hnext = hrm_blist;
2440Sstevel@tonic-gate 			hrm_blist = hrm;
2450Sstevel@tonic-gate 			hrm_blist_num++;
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 			if (prev_ahrm == NULL)
2480Sstevel@tonic-gate 				as->a_hrm = hrm->hrm_anext;
2490Sstevel@tonic-gate 			else
2500Sstevel@tonic-gate 				prev_ahrm->hrm_anext = hrm->hrm_anext;
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 		} else
2530Sstevel@tonic-gate 			prev_ahrm = hrm;
2540Sstevel@tonic-gate 	}
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	/*
2570Sstevel@tonic-gate 	 * If all statistics blocks are free,
2580Sstevel@tonic-gate 	 * return the memory to the system.
2590Sstevel@tonic-gate 	 */
2600Sstevel@tonic-gate 	if (hrm_blist_num == hrm_blist_total) {
2610Sstevel@tonic-gate 		/* zero the block list since we are giving back its memory */
2620Sstevel@tonic-gate 		hrm_blist = NULL;
2630Sstevel@tonic-gate 		hrm_blist_num = 0;
2640Sstevel@tonic-gate 		hrm_blist_total = 0;
265*3258Strevtom 		hrm_tmplist = hrm_memlist;
2663085Strevtom 		hrm_memlist = NULL;
2673085Strevtom 	} else {
268*3258Strevtom 		hrm_tmplist = NULL;
2693085Strevtom 	}
2703085Strevtom 
2713085Strevtom 	mutex_exit(&hat_statlock);
2723085Strevtom 
273*3258Strevtom 	/*
274*3258Strevtom 	 * If there are any hrmstat structures to be freed, this must only
275*3258Strevtom 	 * be done after we've released hat_statlock.
276*3258Strevtom 	 */
277*3258Strevtom 	while (hrm_tmplist != NULL) {
278*3258Strevtom 		hrm_next = hrm_tmplist->hrm_hnext;
279*3258Strevtom 		kmem_free(hrm_tmplist, hrm_tmplist->hrm_base);
280*3258Strevtom 		hrm_tmplist = hrm_next;
2810Sstevel@tonic-gate 	}
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate /*
2850Sstevel@tonic-gate  * Grab memory for statistics gathering of the hat layer.
2860Sstevel@tonic-gate  */
2870Sstevel@tonic-gate static void
2880Sstevel@tonic-gate hrm_getblk(int chunk)
2890Sstevel@tonic-gate {
2900Sstevel@tonic-gate 	struct hrmstat *hrm, *l;
2910Sstevel@tonic-gate 	int i;
2920Sstevel@tonic-gate 	int hrm_incr;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	mutex_enter(&hat_statlock);
2950Sstevel@tonic-gate 	if ((hrm_blist == NULL) ||
2960Sstevel@tonic-gate 	    (hrm_blist_num <= hrm_blist_lowater) ||
297830Speterte 	    (chunk && (hrm_blist_num < chunk))) {
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 		mutex_exit(&hat_statlock);
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 		hrm_incr = chunk? chunk : hrm_blist_incr;
3020Sstevel@tonic-gate 		hrm = kmem_zalloc(sizeof (struct hrmstat) * hrm_incr, KM_SLEEP);
3030Sstevel@tonic-gate 		hrm->hrm_base = sizeof (struct hrmstat) * hrm_incr;
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 		/*
3060Sstevel@tonic-gate 		 * thread the allocated blocks onto a freelist
3070Sstevel@tonic-gate 		 * using the first block to hold information for
3080Sstevel@tonic-gate 		 * freeing them all later
3090Sstevel@tonic-gate 		 */
3100Sstevel@tonic-gate 		mutex_enter(&hat_statlock);
3110Sstevel@tonic-gate 		hrm->hrm_hnext = hrm_memlist;
3120Sstevel@tonic-gate 		hrm_memlist = hrm;
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 		hrm_blist_total += (hrm_incr - 1);
3150Sstevel@tonic-gate 		for (i = 1; i < hrm_incr; i++) {
3160Sstevel@tonic-gate 			l = &hrm[i];
3170Sstevel@tonic-gate 			l->hrm_hnext = hrm_blist;
3180Sstevel@tonic-gate 			hrm_blist = l;
3190Sstevel@tonic-gate 			hrm_blist_num++;
3200Sstevel@tonic-gate 		}
3210Sstevel@tonic-gate 	}
3220Sstevel@tonic-gate 	mutex_exit(&hat_statlock);
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate static void
3260Sstevel@tonic-gate hrm_hashin(struct hrmstat *hrm)
3270Sstevel@tonic-gate {
3280Sstevel@tonic-gate 	int 		h;
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&hat_statlock));
3310Sstevel@tonic-gate 	h = hrm_hash(hrm->hrm_as, hrm->hrm_base);
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	hrm->hrm_hnext = hrm_hashtab[h];
3340Sstevel@tonic-gate 	hrm_hashtab[h] = hrm;
3350Sstevel@tonic-gate }
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate static void
3380Sstevel@tonic-gate hrm_hashout(struct hrmstat *hrm)
3390Sstevel@tonic-gate {
3400Sstevel@tonic-gate 	struct hrmstat	*list, **prev_hrm;
3410Sstevel@tonic-gate 	int		h;
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&hat_statlock));
3440Sstevel@tonic-gate 	h = hrm_hash(hrm->hrm_as, hrm->hrm_base);
3450Sstevel@tonic-gate 	list = hrm_hashtab[h];
3460Sstevel@tonic-gate 	prev_hrm = &hrm_hashtab[h];
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	while (list) {
3490Sstevel@tonic-gate 		if (list == hrm) {
3500Sstevel@tonic-gate 			*prev_hrm = list->hrm_hnext;
3510Sstevel@tonic-gate 			return;
3520Sstevel@tonic-gate 		}
3530Sstevel@tonic-gate 		prev_hrm = &list->hrm_hnext;
3540Sstevel@tonic-gate 		list = list->hrm_hnext;
3550Sstevel@tonic-gate 	}
3560Sstevel@tonic-gate }
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate /*
3600Sstevel@tonic-gate  * Link a statistic block into an address space and also put it
3610Sstevel@tonic-gate  * on the hash list for future references.
3620Sstevel@tonic-gate  */
3630Sstevel@tonic-gate static void
3640Sstevel@tonic-gate hrm_link(struct hrmstat *hrm)
3650Sstevel@tonic-gate {
3660Sstevel@tonic-gate 	struct as *as = hrm->hrm_as;
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&hat_statlock));
3690Sstevel@tonic-gate 	hrm->hrm_anext = as->a_hrm;
3700Sstevel@tonic-gate 	as->a_hrm = hrm;
3710Sstevel@tonic-gate 	hrm_hashin(hrm);
3720Sstevel@tonic-gate }
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate /*
3750Sstevel@tonic-gate  * Allocate a block for statistics keeping.
3760Sstevel@tonic-gate  * Returns NULL if blocks are unavailable.
3770Sstevel@tonic-gate  */
3780Sstevel@tonic-gate static struct hrmstat *
3790Sstevel@tonic-gate hrm_balloc(void)
3800Sstevel@tonic-gate {
3810Sstevel@tonic-gate 	struct hrmstat *hrm;
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&hat_statlock));
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 	hrm = hrm_blist;
3860Sstevel@tonic-gate 	if (hrm != NULL) {
3870Sstevel@tonic-gate 		hrm_blist = hrm->hrm_hnext;
3880Sstevel@tonic-gate 		hrm_blist_num--;
3890Sstevel@tonic-gate 		hrm->hrm_hnext = NULL;
3900Sstevel@tonic-gate 	}
3910Sstevel@tonic-gate 	return (hrm);
3920Sstevel@tonic-gate }
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate /*
3950Sstevel@tonic-gate  * Set the ref and mod bits for addr within statistics block hrm.
3960Sstevel@tonic-gate  */
3970Sstevel@tonic-gate static void
3980Sstevel@tonic-gate hrm_setbits(struct hrmstat *hrm, caddr_t addr, uint_t bits)
3990Sstevel@tonic-gate {
4000Sstevel@tonic-gate 	uint_t po, bo, spb;
4010Sstevel@tonic-gate 	uint_t nbits;
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 	po = ((uintptr_t)addr & HRM_BASEOFFSET) >> MMU_PAGESHIFT; /* pg off */
4040Sstevel@tonic-gate 	bo = po / (NBBY / 2);			/* which byte in bit array */
4050Sstevel@tonic-gate 	spb = (3 - (po & 3)) * 2;		/* shift position within byte */
4060Sstevel@tonic-gate 	nbits = bits << spb;			/* bit mask */
4070Sstevel@tonic-gate 	hrm->hrm_bits[bo] |= nbits;
4080Sstevel@tonic-gate }
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate /*
4110Sstevel@tonic-gate  * Return collected statistics about an address space.
4120Sstevel@tonic-gate  * If clearflag is set, atomically read and zero the bits.
4130Sstevel@tonic-gate  *
4140Sstevel@tonic-gate  * Fill in the data array supplied with the referenced and
4150Sstevel@tonic-gate  * modified bits collected for address range [addr ... addr + len]
4160Sstevel@tonic-gate  * in address space, as, uniquely identified by id.
4170Sstevel@tonic-gate  * The destination is a byte array.  We fill in three bits per byte:
4180Sstevel@tonic-gate  * referenced, modified, and hwmapped bits.
4190Sstevel@tonic-gate  * Kernel only interface, can't fault on destination data array.
4200Sstevel@tonic-gate  *
4210Sstevel@tonic-gate  */
4220Sstevel@tonic-gate void
4230Sstevel@tonic-gate hat_getstat(struct as *as, caddr_t addr, size_t len, uint_t id,
4240Sstevel@tonic-gate     caddr_t datap, int clearflag)
4250Sstevel@tonic-gate {
4260Sstevel@tonic-gate 	size_t	np;		/* number of pages */
4270Sstevel@tonic-gate 	caddr_t	a;
4280Sstevel@tonic-gate 	char 	*dp;
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	np = btop(len);
4310Sstevel@tonic-gate 	bzero(datap, np);
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	hat_sync(as->a_hat, addr, len, clearflag);
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	/* allocate more statistics blocks if needed */
4360Sstevel@tonic-gate 	hrm_getblk(0);
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	mutex_enter(&hat_statlock);
4390Sstevel@tonic-gate 	if (hrm_hashtab == NULL) {
4400Sstevel@tonic-gate 		/* can happen when victim process exits */
4410Sstevel@tonic-gate 		mutex_exit(&hat_statlock);
4420Sstevel@tonic-gate 		return;
4430Sstevel@tonic-gate 	}
4440Sstevel@tonic-gate 	dp = datap;
4450Sstevel@tonic-gate 	a = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
4460Sstevel@tonic-gate 	while (a < addr + len) {
4470Sstevel@tonic-gate 		struct hrmstat	*hrm;
4480Sstevel@tonic-gate 		size_t	n;		/* number of pages, temp */
4490Sstevel@tonic-gate 		int	h;		/* hash index */
4500Sstevel@tonic-gate 		uint_t	po;
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 		h = hrm_hash(as, a);
4530Sstevel@tonic-gate 		n = (HRM_PAGES -
4540Sstevel@tonic-gate 			(((uintptr_t)a & HRM_PAGEMASK) >> MMU_PAGESHIFT));
4550Sstevel@tonic-gate 		if (n > np)
4560Sstevel@tonic-gate 			n = np;
4570Sstevel@tonic-gate 		po = ((uintptr_t)a & HRM_BASEOFFSET) >> MMU_PAGESHIFT;
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 		for (hrm = hrm_hashtab[h]; hrm; hrm = hrm->hrm_hnext) {
4600Sstevel@tonic-gate 			if (hrm->hrm_as == as &&
4610Sstevel@tonic-gate 			    hrm->hrm_base == ((uintptr_t)a & HRM_BASEMASK) &&
4620Sstevel@tonic-gate 			    id == hrm->hrm_id) {
4630Sstevel@tonic-gate 				int i, nr;
4640Sstevel@tonic-gate 				uint_t bo, spb;
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 				/*
4670Sstevel@tonic-gate 				 * Extract leading unaligned bits.
4680Sstevel@tonic-gate 				 */
4690Sstevel@tonic-gate 				i = 0;
4700Sstevel@tonic-gate 				while (i < n && (po & 3)) {
4710Sstevel@tonic-gate 					bo = po / (NBBY / 2);
4720Sstevel@tonic-gate 					spb = (3 - (po & 3)) * 2;
4730Sstevel@tonic-gate 					*dp++ |= (hrm->hrm_bits[bo] >> spb) & 3;
4740Sstevel@tonic-gate 					if (clearflag)
4750Sstevel@tonic-gate 						hrm->hrm_bits[bo] &= ~(3<<spb);
4760Sstevel@tonic-gate 					po++;
4770Sstevel@tonic-gate 					i++;
4780Sstevel@tonic-gate 				}
4790Sstevel@tonic-gate 				/*
4800Sstevel@tonic-gate 				 * Extract aligned bits.
4810Sstevel@tonic-gate 				 */
4820Sstevel@tonic-gate 				nr = n/4*4;
4830Sstevel@tonic-gate 				bo = po / (NBBY / 2);
4840Sstevel@tonic-gate 				while (i < nr) {
4850Sstevel@tonic-gate 					int bits = hrm->hrm_bits[bo];
4860Sstevel@tonic-gate 					*dp++ |= (bits >> 6) & 3;
4870Sstevel@tonic-gate 					*dp++ |= (bits >> 4) & 3;
4880Sstevel@tonic-gate 					*dp++ |= (bits >> 2) & 3;
4890Sstevel@tonic-gate 					*dp++ |= (bits >> 0) & 3;
4900Sstevel@tonic-gate 					if (clearflag)
4910Sstevel@tonic-gate 						hrm->hrm_bits[bo] = 0;
4920Sstevel@tonic-gate 					bo++;
4930Sstevel@tonic-gate 					po += 4;
4940Sstevel@tonic-gate 					i += 4;
4950Sstevel@tonic-gate 				}
4960Sstevel@tonic-gate 				/*
4970Sstevel@tonic-gate 				 * Extract trailing unaligned bits.
4980Sstevel@tonic-gate 				 */
4990Sstevel@tonic-gate 				while (i < n) {
5000Sstevel@tonic-gate 					bo = po / (NBBY / 2);
5010Sstevel@tonic-gate 					spb = (3 - (po & 3)) * 2;
5020Sstevel@tonic-gate 					*dp++ |= (hrm->hrm_bits[bo] >> spb) & 3;
5030Sstevel@tonic-gate 					if (clearflag)
5040Sstevel@tonic-gate 						hrm->hrm_bits[bo] &= ~(3<<spb);
5050Sstevel@tonic-gate 					po++;
5060Sstevel@tonic-gate 					i++;
5070Sstevel@tonic-gate 				}
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 				break;
5100Sstevel@tonic-gate 			}
5110Sstevel@tonic-gate 		}
5120Sstevel@tonic-gate 		if (hrm == NULL)
5130Sstevel@tonic-gate 			dp += n;
5140Sstevel@tonic-gate 		np -= n;
5150Sstevel@tonic-gate 		a += n * MMU_PAGESIZE;
5160Sstevel@tonic-gate 	}
5170Sstevel@tonic-gate 	mutex_exit(&hat_statlock);
5180Sstevel@tonic-gate }
519