xref: /onnv-gate/usr/src/uts/common/os/mem_config.c (revision 11066:cebb50cbe4f9)
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
51582Skchow  * Common Development and Distribution License (the "License").
61582Skchow  * 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 /*
2210106SJason.Beloro@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/cmn_err.h>
280Sstevel@tonic-gate #include <sys/vmem.h>
290Sstevel@tonic-gate #include <sys/kmem.h>
300Sstevel@tonic-gate #include <sys/systm.h>
310Sstevel@tonic-gate #include <sys/machsystm.h>	/* for page_freelist_coalesce() */
320Sstevel@tonic-gate #include <sys/errno.h>
330Sstevel@tonic-gate #include <sys/memnode.h>
340Sstevel@tonic-gate #include <sys/memlist.h>
350Sstevel@tonic-gate #include <sys/memlist_impl.h>
360Sstevel@tonic-gate #include <sys/tuneable.h>
370Sstevel@tonic-gate #include <sys/proc.h>
380Sstevel@tonic-gate #include <sys/disp.h>
390Sstevel@tonic-gate #include <sys/debug.h>
400Sstevel@tonic-gate #include <sys/vm.h>
410Sstevel@tonic-gate #include <sys/callb.h>
420Sstevel@tonic-gate #include <sys/memlist_plat.h>	/* for installed_top_size() */
430Sstevel@tonic-gate #include <sys/condvar_impl.h>	/* for CV_HAS_WAITERS() */
440Sstevel@tonic-gate #include <sys/dumphdr.h>	/* for dump_resize() */
450Sstevel@tonic-gate #include <sys/atomic.h>		/* for use in stats collection */
460Sstevel@tonic-gate #include <sys/rwlock.h>
470Sstevel@tonic-gate #include <sys/cpuvar.h>
480Sstevel@tonic-gate #include <vm/seg_kmem.h>
490Sstevel@tonic-gate #include <vm/seg_kpm.h>
500Sstevel@tonic-gate #include <vm/page.h>
511373Skchow #include <vm/vm_dep.h>
520Sstevel@tonic-gate #define	SUNDDI_IMPL		/* so sunddi.h will not redefine splx() et al */
530Sstevel@tonic-gate #include <sys/sunddi.h>
540Sstevel@tonic-gate #include <sys/mem_config.h>
550Sstevel@tonic-gate #include <sys/mem_cage.h>
560Sstevel@tonic-gate #include <sys/lgrp.h>
570Sstevel@tonic-gate #include <sys/ddi.h>
580Sstevel@tonic-gate #include <sys/modctl.h>
590Sstevel@tonic-gate 
600Sstevel@tonic-gate extern struct memlist *phys_avail;
610Sstevel@tonic-gate 
620Sstevel@tonic-gate extern void mem_node_add(pfn_t, pfn_t);
630Sstevel@tonic-gate extern void mem_node_del(pfn_t, pfn_t);
640Sstevel@tonic-gate 
650Sstevel@tonic-gate extern uint_t page_ctrs_adjust(int);
660Sstevel@tonic-gate static void kphysm_setup_post_add(pgcnt_t);
670Sstevel@tonic-gate static int kphysm_setup_pre_del(pgcnt_t);
680Sstevel@tonic-gate static void kphysm_setup_post_del(pgcnt_t, int);
690Sstevel@tonic-gate 
700Sstevel@tonic-gate static int kphysm_split_memseg(pfn_t base, pgcnt_t npgs);
710Sstevel@tonic-gate 
720Sstevel@tonic-gate static int delspan_reserve(pfn_t, pgcnt_t);
730Sstevel@tonic-gate static void delspan_unreserve(pfn_t, pgcnt_t);
740Sstevel@tonic-gate 
7510106SJason.Beloro@Sun.COM kmutex_t memseg_lists_lock;
7610106SJason.Beloro@Sun.COM struct memseg *memseg_va_avail;
7710106SJason.Beloro@Sun.COM struct memseg *memseg_alloc(void);
780Sstevel@tonic-gate static struct memseg *memseg_delete_junk;
790Sstevel@tonic-gate static struct memseg *memseg_edit_junk;
800Sstevel@tonic-gate void memseg_remap_init(void);
8110106SJason.Beloro@Sun.COM static void memseg_remap_to_dummy(struct memseg *);
820Sstevel@tonic-gate static void kphysm_addmem_error_undospan(pfn_t, pgcnt_t);
830Sstevel@tonic-gate static struct memseg *memseg_reuse(pgcnt_t);
840Sstevel@tonic-gate 
850Sstevel@tonic-gate static struct kmem_cache *memseg_cache;
860Sstevel@tonic-gate 
870Sstevel@tonic-gate /*
8810106SJason.Beloro@Sun.COM  * Interfaces to manage externally allocated
8910106SJason.Beloro@Sun.COM  * page_t memory (metadata) for a memseg.
9010106SJason.Beloro@Sun.COM  */
9110106SJason.Beloro@Sun.COM #pragma weak	memseg_alloc_meta
9210106SJason.Beloro@Sun.COM #pragma weak	memseg_free_meta
9310106SJason.Beloro@Sun.COM #pragma weak	memseg_get_metapfn
9410106SJason.Beloro@Sun.COM #pragma weak	memseg_remap_meta
9510106SJason.Beloro@Sun.COM 
9610106SJason.Beloro@Sun.COM extern int ppvm_enable;
9710106SJason.Beloro@Sun.COM extern page_t *ppvm_base;
9810106SJason.Beloro@Sun.COM extern int memseg_alloc_meta(pfn_t, pgcnt_t, void **, pgcnt_t *);
9910106SJason.Beloro@Sun.COM extern void memseg_free_meta(void *, pgcnt_t);
10010106SJason.Beloro@Sun.COM extern pfn_t memseg_get_metapfn(void *, pgcnt_t);
10110106SJason.Beloro@Sun.COM extern void memseg_remap_meta(struct memseg *);
10210106SJason.Beloro@Sun.COM static int memseg_is_dynamic(struct memseg *);
10310106SJason.Beloro@Sun.COM static int memseg_includes_meta(struct memseg *);
10410106SJason.Beloro@Sun.COM static pfn_t memseg_get_start(struct memseg *);
10510106SJason.Beloro@Sun.COM static void memseg_cpu_vm_flush(void);
10610106SJason.Beloro@Sun.COM 
10710106SJason.Beloro@Sun.COM int meta_alloc_enable;
10810106SJason.Beloro@Sun.COM 
10910106SJason.Beloro@Sun.COM /*
11010106SJason.Beloro@Sun.COM  * Add a chunk of memory to the system.
1110Sstevel@tonic-gate  * base: starting PAGESIZE page of new memory.
1120Sstevel@tonic-gate  * npgs: length in PAGESIZE pages.
1130Sstevel@tonic-gate  *
1140Sstevel@tonic-gate  * Adding mem this way doesn't increase the size of the hash tables;
1150Sstevel@tonic-gate  * growing them would be too hard.  This should be OK, but adding memory
1160Sstevel@tonic-gate  * dynamically most likely means more hash misses, since the tables will
1170Sstevel@tonic-gate  * be smaller than they otherwise would be.
1180Sstevel@tonic-gate  */
11910106SJason.Beloro@Sun.COM #ifdef	DEBUG
12010106SJason.Beloro@Sun.COM static int memseg_debug;
12110106SJason.Beloro@Sun.COM #define	MEMSEG_DEBUG(args...) if (memseg_debug) printf(args)
12210106SJason.Beloro@Sun.COM #else
12310106SJason.Beloro@Sun.COM #define	MEMSEG_DEBUG(...)
12410106SJason.Beloro@Sun.COM #endif
12510106SJason.Beloro@Sun.COM 
1260Sstevel@tonic-gate int
1270Sstevel@tonic-gate kphysm_add_memory_dynamic(pfn_t base, pgcnt_t npgs)
1280Sstevel@tonic-gate {
12910106SJason.Beloro@Sun.COM 	page_t *pp;
13010106SJason.Beloro@Sun.COM 	page_t		*opp, *oepp, *segpp;
1310Sstevel@tonic-gate 	struct memseg	*seg;
1320Sstevel@tonic-gate 	uint64_t	avmem;
1330Sstevel@tonic-gate 	pfn_t		pfn;
1340Sstevel@tonic-gate 	pfn_t		pt_base = base;
1350Sstevel@tonic-gate 	pgcnt_t		tpgs = npgs;
13610106SJason.Beloro@Sun.COM 	pgcnt_t		metapgs = 0;
1370Sstevel@tonic-gate 	int		exhausted;
1380Sstevel@tonic-gate 	pfn_t		pnum;
1390Sstevel@tonic-gate 	int		mnode;
1400Sstevel@tonic-gate 	caddr_t		vaddr;
1410Sstevel@tonic-gate 	int		reuse;
1420Sstevel@tonic-gate 	int		mlret;
14310106SJason.Beloro@Sun.COM 	int		rv;
14410106SJason.Beloro@Sun.COM 	int		flags;
14510106SJason.Beloro@Sun.COM 	int		meta_alloc = 0;
1460Sstevel@tonic-gate 	void		*mapva;
14710106SJason.Beloro@Sun.COM 	void		*metabase = (void *)base;
1480Sstevel@tonic-gate 	pgcnt_t		nkpmpgs = 0;
1490Sstevel@tonic-gate 	offset_t	kpm_pages_off;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	cmn_err(CE_CONT,
1520Sstevel@tonic-gate 	    "?kphysm_add_memory_dynamic: adding %ldK at 0x%" PRIx64 "\n",
1530Sstevel@tonic-gate 	    npgs << (PAGESHIFT - 10), (uint64_t)base << PAGESHIFT);
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	/*
1560Sstevel@tonic-gate 	 * Add this span in the delete list to prevent interactions.
1570Sstevel@tonic-gate 	 */
1580Sstevel@tonic-gate 	if (!delspan_reserve(base, npgs)) {
1590Sstevel@tonic-gate 		return (KPHYSM_ESPAN);
1600Sstevel@tonic-gate 	}
1610Sstevel@tonic-gate 	/*
1620Sstevel@tonic-gate 	 * Check to see if any of the memory span has been added
1630Sstevel@tonic-gate 	 * by trying an add to the installed memory list. This
1640Sstevel@tonic-gate 	 * forms the interlocking process for add.
1650Sstevel@tonic-gate 	 */
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	memlist_write_lock();
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	mlret = memlist_add_span((uint64_t)(pt_base) << PAGESHIFT,
1700Sstevel@tonic-gate 	    (uint64_t)(tpgs) << PAGESHIFT, &phys_install);
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	if (mlret == MEML_SPANOP_OK)
1730Sstevel@tonic-gate 		installed_top_size(phys_install, &physmax, &physinstalled);
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	memlist_write_unlock();
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	if (mlret != MEML_SPANOP_OK) {
1780Sstevel@tonic-gate 		if (mlret == MEML_SPANOP_EALLOC) {
1790Sstevel@tonic-gate 			delspan_unreserve(pt_base, tpgs);
1800Sstevel@tonic-gate 			return (KPHYSM_ERESOURCE);
18110106SJason.Beloro@Sun.COM 		} else if (mlret == MEML_SPANOP_ESPAN) {
1820Sstevel@tonic-gate 			delspan_unreserve(pt_base, tpgs);
1830Sstevel@tonic-gate 			return (KPHYSM_ESPAN);
1840Sstevel@tonic-gate 		} else {
1850Sstevel@tonic-gate 			delspan_unreserve(pt_base, tpgs);
1860Sstevel@tonic-gate 			return (KPHYSM_ERESOURCE);
1870Sstevel@tonic-gate 		}
1880Sstevel@tonic-gate 	}
1890Sstevel@tonic-gate 
19010106SJason.Beloro@Sun.COM 	if (meta_alloc_enable) {
19110106SJason.Beloro@Sun.COM 		/*
19210106SJason.Beloro@Sun.COM 		 * Allocate the page_t's from existing memory;
19310106SJason.Beloro@Sun.COM 		 * if that fails, allocate from the incoming memory.
19410106SJason.Beloro@Sun.COM 		 */
19510106SJason.Beloro@Sun.COM 		rv = memseg_alloc_meta(base, npgs, &metabase, &metapgs);
19610106SJason.Beloro@Sun.COM 		if (rv == KPHYSM_OK) {
19710106SJason.Beloro@Sun.COM 			ASSERT(metapgs);
19810106SJason.Beloro@Sun.COM 			ASSERT(btopr(npgs * sizeof (page_t)) <= metapgs);
19910106SJason.Beloro@Sun.COM 			meta_alloc = 1;
20010106SJason.Beloro@Sun.COM 			goto mapalloc;
20110106SJason.Beloro@Sun.COM 		}
20210106SJason.Beloro@Sun.COM 	}
20310106SJason.Beloro@Sun.COM 
2040Sstevel@tonic-gate 	/*
2050Sstevel@tonic-gate 	 * We store the page_t's for this new memory in the first
2060Sstevel@tonic-gate 	 * few pages of the chunk. Here, we go and get'em ...
2070Sstevel@tonic-gate 	 */
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	/*
2100Sstevel@tonic-gate 	 * The expression after the '-' gives the number of pages
2110Sstevel@tonic-gate 	 * that will fit in the new memory based on a requirement
2120Sstevel@tonic-gate 	 * of (PAGESIZE + sizeof (page_t)) bytes per page.
2130Sstevel@tonic-gate 	 */
2140Sstevel@tonic-gate 	metapgs = npgs - (((uint64_t)(npgs) << PAGESHIFT) /
2150Sstevel@tonic-gate 	    (PAGESIZE + sizeof (page_t)));
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	npgs -= metapgs;
2180Sstevel@tonic-gate 	base += metapgs;
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	ASSERT(btopr(npgs * sizeof (page_t)) <= metapgs);
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	exhausted = (metapgs == 0 || npgs == 0);
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	if (kpm_enable && !exhausted) {
2250Sstevel@tonic-gate 		pgcnt_t start, end, nkpmpgs_prelim;
2260Sstevel@tonic-gate 		size_t	ptsz;
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 		/*
2290Sstevel@tonic-gate 		 * A viable kpm large page mapping must not overlap two
2300Sstevel@tonic-gate 		 * dynamic memsegs. Therefore the total size is checked
2310Sstevel@tonic-gate 		 * to be at least kpm_pgsz and also whether start and end
2320Sstevel@tonic-gate 		 * points are at least kpm_pgsz aligned.
2330Sstevel@tonic-gate 		 */
2340Sstevel@tonic-gate 		if (ptokpmp(tpgs) < 1 || pmodkpmp(pt_base) ||
2350Sstevel@tonic-gate 		    pmodkpmp(base + npgs)) {
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 			kphysm_addmem_error_undospan(pt_base, tpgs);
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 			/*
2400Sstevel@tonic-gate 			 * There is no specific error code for violating
2410Sstevel@tonic-gate 			 * kpm granularity constraints.
2420Sstevel@tonic-gate 			 */
2430Sstevel@tonic-gate 			return (KPHYSM_ENOTVIABLE);
2440Sstevel@tonic-gate 		}
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 		start = kpmptop(ptokpmp(base));
2470Sstevel@tonic-gate 		end = kpmptop(ptokpmp(base + npgs));
2480Sstevel@tonic-gate 		nkpmpgs_prelim = ptokpmp(end - start);
2490Sstevel@tonic-gate 		ptsz = npgs * sizeof (page_t);
2500Sstevel@tonic-gate 		metapgs = btopr(ptsz + nkpmpgs_prelim * KPMPAGE_T_SZ);
2510Sstevel@tonic-gate 		exhausted = (tpgs <= metapgs);
2520Sstevel@tonic-gate 		if (!exhausted) {
2530Sstevel@tonic-gate 			npgs = tpgs - metapgs;
2540Sstevel@tonic-gate 			base = pt_base + metapgs;
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 			/* final nkpmpgs */
2570Sstevel@tonic-gate 			start = kpmptop(ptokpmp(base));
2580Sstevel@tonic-gate 			nkpmpgs = ptokpmp(end - start);
2590Sstevel@tonic-gate 			kpm_pages_off = ptsz +
2606242Smb158278 			    (nkpmpgs_prelim - nkpmpgs) * KPMPAGE_T_SZ;
2610Sstevel@tonic-gate 		}
2620Sstevel@tonic-gate 	}
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	/*
2650Sstevel@tonic-gate 	 * Is memory area supplied too small?
2660Sstevel@tonic-gate 	 */
2670Sstevel@tonic-gate 	if (exhausted) {
2680Sstevel@tonic-gate 		kphysm_addmem_error_undospan(pt_base, tpgs);
2690Sstevel@tonic-gate 		/*
2700Sstevel@tonic-gate 		 * There is no specific error code for 'too small'.
2710Sstevel@tonic-gate 		 */
2720Sstevel@tonic-gate 		return (KPHYSM_ERESOURCE);
2730Sstevel@tonic-gate 	}
2740Sstevel@tonic-gate 
27510106SJason.Beloro@Sun.COM mapalloc:
2760Sstevel@tonic-gate 	/*
2770Sstevel@tonic-gate 	 * We may re-use a previously allocated VA space for the page_ts
2780Sstevel@tonic-gate 	 * eventually, but we need to initialize and lock the pages first.
2790Sstevel@tonic-gate 	 */
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	/*
2820Sstevel@tonic-gate 	 * Get an address in the kernel address map, map
2830Sstevel@tonic-gate 	 * the page_t pages and see if we can touch them.
2840Sstevel@tonic-gate 	 */
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	mapva = vmem_alloc(heap_arena, ptob(metapgs), VM_NOSLEEP);
2870Sstevel@tonic-gate 	if (mapva == NULL) {
2880Sstevel@tonic-gate 		cmn_err(CE_WARN, "kphysm_add_memory_dynamic:"
2890Sstevel@tonic-gate 		    " Can't allocate VA for page_ts");
2900Sstevel@tonic-gate 
29110106SJason.Beloro@Sun.COM 		if (meta_alloc)
29210106SJason.Beloro@Sun.COM 			memseg_free_meta(metabase, metapgs);
2930Sstevel@tonic-gate 		kphysm_addmem_error_undospan(pt_base, tpgs);
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 		return (KPHYSM_ERESOURCE);
2960Sstevel@tonic-gate 	}
2970Sstevel@tonic-gate 	pp = mapva;
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	if (physmax < (pt_base + tpgs))
3000Sstevel@tonic-gate 		physmax = (pt_base + tpgs);
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	/*
3030Sstevel@tonic-gate 	 * In the remapping code we map one page at a time so we must do
3040Sstevel@tonic-gate 	 * the same here to match mapping sizes.
3050Sstevel@tonic-gate 	 */
3060Sstevel@tonic-gate 	pfn = pt_base;
3070Sstevel@tonic-gate 	vaddr = (caddr_t)pp;
3080Sstevel@tonic-gate 	for (pnum = 0; pnum < metapgs; pnum++) {
30910106SJason.Beloro@Sun.COM 		if (meta_alloc)
31010106SJason.Beloro@Sun.COM 			pfn = memseg_get_metapfn(metabase, (pgcnt_t)pnum);
3110Sstevel@tonic-gate 		hat_devload(kas.a_hat, vaddr, ptob(1), pfn,
3120Sstevel@tonic-gate 		    PROT_READ | PROT_WRITE,
3130Sstevel@tonic-gate 		    HAT_LOAD | HAT_LOAD_LOCK | HAT_LOAD_NOCONSIST);
3140Sstevel@tonic-gate 		pfn++;
3150Sstevel@tonic-gate 		vaddr += ptob(1);
3160Sstevel@tonic-gate 	}
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	if (ddi_peek32((dev_info_t *)NULL,
3190Sstevel@tonic-gate 	    (int32_t *)pp, (int32_t *)0) == DDI_FAILURE) {
3200Sstevel@tonic-gate 
3217694SJakub.Jirsa@Sun.COM 		cmn_err(CE_WARN, "kphysm_add_memory_dynamic:"
3220Sstevel@tonic-gate 		    " Can't access pp array at 0x%p [phys 0x%lx]",
3230Sstevel@tonic-gate 		    (void *)pp, pt_base);
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 		hat_unload(kas.a_hat, (caddr_t)pp, ptob(metapgs),
3260Sstevel@tonic-gate 		    HAT_UNLOAD_UNMAP|HAT_UNLOAD_UNLOCK);
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 		vmem_free(heap_arena, mapva, ptob(metapgs));
32910106SJason.Beloro@Sun.COM 		if (meta_alloc)
33010106SJason.Beloro@Sun.COM 			memseg_free_meta(metabase, metapgs);
3310Sstevel@tonic-gate 		kphysm_addmem_error_undospan(pt_base, tpgs);
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 		return (KPHYSM_EFAULT);
3340Sstevel@tonic-gate 	}
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	/*
3370Sstevel@tonic-gate 	 * Add this memory slice to its memory node translation.
3380Sstevel@tonic-gate 	 *
3390Sstevel@tonic-gate 	 * Note that right now, each node may have only one slice;
3400Sstevel@tonic-gate 	 * this may change with COD or in larger SSM systems with
3410Sstevel@tonic-gate 	 * nested latency groups, so we must not assume that the
3420Sstevel@tonic-gate 	 * node does not yet exist.
3430Sstevel@tonic-gate 	 */
3447923SChristopher.Baumbauer@Sun.COM 	pnum = pt_base + tpgs - 1;
34510106SJason.Beloro@Sun.COM 	mem_node_add_range(pt_base, pnum);
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	/*
3485331Samw 	 * Allocate or resize page counters as necessary to accommodate
3490Sstevel@tonic-gate 	 * the increase in memory pages.
3500Sstevel@tonic-gate 	 */
3510Sstevel@tonic-gate 	mnode = PFN_2_MEM_NODE(pnum);
35210106SJason.Beloro@Sun.COM 	PAGE_CTRS_ADJUST(base, npgs, rv);
35310106SJason.Beloro@Sun.COM 	if (rv) {
35410106SJason.Beloro@Sun.COM 
35510106SJason.Beloro@Sun.COM 		mem_node_del_range(pt_base, pnum);
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 		hat_unload(kas.a_hat, (caddr_t)pp, ptob(metapgs),
3580Sstevel@tonic-gate 		    HAT_UNLOAD_UNMAP|HAT_UNLOAD_UNLOCK);
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 		vmem_free(heap_arena, mapva, ptob(metapgs));
36110106SJason.Beloro@Sun.COM 		if (meta_alloc)
36210106SJason.Beloro@Sun.COM 			memseg_free_meta(metabase, metapgs);
3630Sstevel@tonic-gate 		kphysm_addmem_error_undospan(pt_base, tpgs);
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 		return (KPHYSM_ERESOURCE);
3660Sstevel@tonic-gate 	}
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	/*
3690Sstevel@tonic-gate 	 * Update the phys_avail memory list.
3700Sstevel@tonic-gate 	 * The phys_install list was done at the start.
3710Sstevel@tonic-gate 	 */
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	memlist_write_lock();
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	mlret = memlist_add_span((uint64_t)(base) << PAGESHIFT,
3760Sstevel@tonic-gate 	    (uint64_t)(npgs) << PAGESHIFT, &phys_avail);
3770Sstevel@tonic-gate 	ASSERT(mlret == MEML_SPANOP_OK);
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	memlist_write_unlock();
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	/* See if we can find a memseg to re-use. */
38210106SJason.Beloro@Sun.COM 	if (meta_alloc) {
38310106SJason.Beloro@Sun.COM 		seg = memseg_reuse(0);
38410106SJason.Beloro@Sun.COM 		reuse = 1;	/* force unmapping of temp mapva */
38510106SJason.Beloro@Sun.COM 		flags = MEMSEG_DYNAMIC | MEMSEG_META_ALLOC;
38610106SJason.Beloro@Sun.COM 		/*
38710106SJason.Beloro@Sun.COM 		 * There is a 1:1 fixed relationship between a pfn
38810106SJason.Beloro@Sun.COM 		 * and a page_t VA.  The pfn is used as an index into
38910106SJason.Beloro@Sun.COM 		 * the ppvm_base page_t table in order to calculate
39010106SJason.Beloro@Sun.COM 		 * the page_t base address for a given pfn range.
39110106SJason.Beloro@Sun.COM 		 */
39210106SJason.Beloro@Sun.COM 		segpp = ppvm_base + base;
39310106SJason.Beloro@Sun.COM 	} else {
39410106SJason.Beloro@Sun.COM 		seg = memseg_reuse(metapgs);
39510106SJason.Beloro@Sun.COM 		reuse = (seg != NULL);
39610106SJason.Beloro@Sun.COM 		flags = MEMSEG_DYNAMIC | MEMSEG_META_INCL;
39710106SJason.Beloro@Sun.COM 		segpp = pp;
39810106SJason.Beloro@Sun.COM 	}
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	/*
4010Sstevel@tonic-gate 	 * Initialize the memseg structure representing this memory
4020Sstevel@tonic-gate 	 * and add it to the existing list of memsegs. Do some basic
4030Sstevel@tonic-gate 	 * initialization and add the memory to the system.
4040Sstevel@tonic-gate 	 * In order to prevent lock deadlocks, the add_physmem()
4050Sstevel@tonic-gate 	 * code is repeated here, but split into several stages.
40610106SJason.Beloro@Sun.COM 	 *
40710106SJason.Beloro@Sun.COM 	 * If a memseg is reused, invalidate memseg pointers in
40810106SJason.Beloro@Sun.COM 	 * all cpu vm caches.  We need to do this this since the check
40910106SJason.Beloro@Sun.COM 	 * 	pp >= seg->pages && pp < seg->epages
41010106SJason.Beloro@Sun.COM 	 * used in various places is not atomic and so the first compare
41110106SJason.Beloro@Sun.COM 	 * can happen before reuse and the second compare after reuse.
41210106SJason.Beloro@Sun.COM 	 * The invalidation ensures that a memseg is not deferenced while
41310106SJason.Beloro@Sun.COM 	 * it's page/pfn pointers are changing.
4140Sstevel@tonic-gate 	 */
4150Sstevel@tonic-gate 	if (seg == NULL) {
41610106SJason.Beloro@Sun.COM 		seg = memseg_alloc();
41710106SJason.Beloro@Sun.COM 		ASSERT(seg != NULL);
41810106SJason.Beloro@Sun.COM 		seg->msegflags = flags;
41910106SJason.Beloro@Sun.COM 		MEMSEG_DEBUG("memseg_get: alloc seg=0x%p, pages=0x%p",
42010106SJason.Beloro@Sun.COM 		    (void *)seg, (void *)(seg->pages));
42110106SJason.Beloro@Sun.COM 		seg->pages = segpp;
4220Sstevel@tonic-gate 	} else {
42310106SJason.Beloro@Sun.COM 		ASSERT(seg->msegflags == flags);
42410106SJason.Beloro@Sun.COM 		ASSERT(seg->pages_base == seg->pages_end);
42510106SJason.Beloro@Sun.COM 		MEMSEG_DEBUG("memseg_get: reuse seg=0x%p, pages=0x%p",
42610106SJason.Beloro@Sun.COM 		    (void *)seg, (void *)(seg->pages));
42710106SJason.Beloro@Sun.COM 		if (meta_alloc) {
42810106SJason.Beloro@Sun.COM 			memseg_cpu_vm_flush();
42910106SJason.Beloro@Sun.COM 			seg->pages = segpp;
43010106SJason.Beloro@Sun.COM 		}
4310Sstevel@tonic-gate 	}
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	seg->epages = seg->pages + npgs;
4340Sstevel@tonic-gate 	seg->pages_base = base;
4350Sstevel@tonic-gate 	seg->pages_end = base + npgs;
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	/*
4380Sstevel@tonic-gate 	 * Initialize metadata. The page_ts are set to locked state
4390Sstevel@tonic-gate 	 * ready to be freed.
4400Sstevel@tonic-gate 	 */
4410Sstevel@tonic-gate 	bzero((caddr_t)pp, ptob(metapgs));
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	pfn = seg->pages_base;
4440Sstevel@tonic-gate 	/* Save the original pp base in case we reuse a memseg. */
4450Sstevel@tonic-gate 	opp = pp;
4460Sstevel@tonic-gate 	oepp = opp + npgs;
4470Sstevel@tonic-gate 	for (pp = opp; pp < oepp; pp++) {
4480Sstevel@tonic-gate 		pp->p_pagenum = pfn;
4490Sstevel@tonic-gate 		pfn++;
4500Sstevel@tonic-gate 		page_iolock_init(pp);
4510Sstevel@tonic-gate 		while (!page_lock(pp, SE_EXCL, (kmutex_t *)NULL, P_RECLAIM))
4520Sstevel@tonic-gate 			continue;
4530Sstevel@tonic-gate 		pp->p_offset = (u_offset_t)-1;
4540Sstevel@tonic-gate 	}
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 	if (reuse) {
4570Sstevel@tonic-gate 		/* Remap our page_ts to the re-used memseg VA space. */
4580Sstevel@tonic-gate 		pfn = pt_base;
4590Sstevel@tonic-gate 		vaddr = (caddr_t)seg->pages;
4600Sstevel@tonic-gate 		for (pnum = 0; pnum < metapgs; pnum++) {
46110106SJason.Beloro@Sun.COM 			if (meta_alloc)
46210106SJason.Beloro@Sun.COM 				pfn = memseg_get_metapfn(metabase,
46310106SJason.Beloro@Sun.COM 				    (pgcnt_t)pnum);
4640Sstevel@tonic-gate 			hat_devload(kas.a_hat, vaddr, ptob(1), pfn,
4650Sstevel@tonic-gate 			    PROT_READ | PROT_WRITE,
4660Sstevel@tonic-gate 			    HAT_LOAD_REMAP | HAT_LOAD | HAT_LOAD_NOCONSIST);
4670Sstevel@tonic-gate 			pfn++;
4680Sstevel@tonic-gate 			vaddr += ptob(1);
4690Sstevel@tonic-gate 		}
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 		hat_unload(kas.a_hat, (caddr_t)opp, ptob(metapgs),
4720Sstevel@tonic-gate 		    HAT_UNLOAD_UNMAP|HAT_UNLOAD_UNLOCK);
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 		vmem_free(heap_arena, mapva, ptob(metapgs));
4750Sstevel@tonic-gate 	}
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 	hat_kpm_addmem_mseg_update(seg, nkpmpgs, kpm_pages_off);
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	memsegs_lock(1);
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 	/*
4820Sstevel@tonic-gate 	 * The new memseg is inserted at the beginning of the list.
4830Sstevel@tonic-gate 	 * Not only does this save searching for the tail, but in the
4840Sstevel@tonic-gate 	 * case of a re-used memseg, it solves the problem of what
4857694SJakub.Jirsa@Sun.COM 	 * happens if some process has still got a pointer to the
4860Sstevel@tonic-gate 	 * memseg and follows the next pointer to continue traversing
4870Sstevel@tonic-gate 	 * the memsegs list.
4880Sstevel@tonic-gate 	 */
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	hat_kpm_addmem_mseg_insert(seg);
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	seg->next = memsegs;
4930Sstevel@tonic-gate 	membar_producer();
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	hat_kpm_addmem_memsegs_update(seg);
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	memsegs = seg;
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	build_pfn_hash();
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	total_pages += npgs;
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	/*
5040Sstevel@tonic-gate 	 * Recalculate the paging parameters now total_pages has changed.
5050Sstevel@tonic-gate 	 * This will also cause the clock hands to be reset before next use.
5060Sstevel@tonic-gate 	 */
5070Sstevel@tonic-gate 	setupclock(1);
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 	memsegs_unlock(1);
5100Sstevel@tonic-gate 
5111582Skchow 	PLCNT_MODIFY_MAX(seg->pages_base, (long)npgs);
5121582Skchow 
5130Sstevel@tonic-gate 	/*
5140Sstevel@tonic-gate 	 * Free the pages outside the lock to avoid locking loops.
5150Sstevel@tonic-gate 	 */
5160Sstevel@tonic-gate 	for (pp = seg->pages; pp < seg->epages; pp++) {
5170Sstevel@tonic-gate 		page_free(pp, 1);
5180Sstevel@tonic-gate 	}
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	/*
5210Sstevel@tonic-gate 	 * Now that we've updated the appropriate memory lists we
5220Sstevel@tonic-gate 	 * need to reset a number of globals, since we've increased memory.
5230Sstevel@tonic-gate 	 * Several have already been updated for us as noted above. The
5240Sstevel@tonic-gate 	 * globals we're interested in at this point are:
5250Sstevel@tonic-gate 	 *   physmax - highest page frame number.
5260Sstevel@tonic-gate 	 *   physinstalled - number of pages currently installed (done earlier)
5270Sstevel@tonic-gate 	 *   maxmem - max free pages in the system
5280Sstevel@tonic-gate 	 *   physmem - physical memory pages available
5290Sstevel@tonic-gate 	 *   availrmem - real memory available
5300Sstevel@tonic-gate 	 */
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	mutex_enter(&freemem_lock);
5330Sstevel@tonic-gate 	maxmem += npgs;
5340Sstevel@tonic-gate 	physmem += npgs;
5350Sstevel@tonic-gate 	availrmem += npgs;
5360Sstevel@tonic-gate 	availrmem_initial += npgs;
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 	mutex_exit(&freemem_lock);
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	dump_resize();
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	page_freelist_coalesce_all(mnode);
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 	kphysm_setup_post_add(npgs);
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 	cmn_err(CE_CONT, "?kphysm_add_memory_dynamic: mem = %ldK "
5470Sstevel@tonic-gate 	    "(0x%" PRIx64 ")\n",
5480Sstevel@tonic-gate 	    physinstalled << (PAGESHIFT - 10),
5490Sstevel@tonic-gate 	    (uint64_t)physinstalled << PAGESHIFT);
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	avmem = (uint64_t)freemem << PAGESHIFT;
5520Sstevel@tonic-gate 	cmn_err(CE_CONT, "?kphysm_add_memory_dynamic: "
5530Sstevel@tonic-gate 	    "avail mem = %" PRId64 "\n", avmem);
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	/*
5560Sstevel@tonic-gate 	 * Update lgroup generation number on single lgroup systems
5570Sstevel@tonic-gate 	 */
5580Sstevel@tonic-gate 	if (nlgrps == 1)
5590Sstevel@tonic-gate 		lgrp_config(LGRP_CONFIG_GEN_UPDATE, 0, 0);
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 	delspan_unreserve(pt_base, tpgs);
5620Sstevel@tonic-gate 	return (KPHYSM_OK);		/* Successfully added system memory */
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate }
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate /*
5670Sstevel@tonic-gate  * There are various error conditions in kphysm_add_memory_dynamic()
5680Sstevel@tonic-gate  * which require a rollback of already changed global state.
5690Sstevel@tonic-gate  */
5700Sstevel@tonic-gate static void
5710Sstevel@tonic-gate kphysm_addmem_error_undospan(pfn_t pt_base, pgcnt_t tpgs)
5720Sstevel@tonic-gate {
5730Sstevel@tonic-gate 	int mlret;
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 	/* Unreserve memory span. */
5760Sstevel@tonic-gate 	memlist_write_lock();
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 	mlret = memlist_delete_span(
5790Sstevel@tonic-gate 	    (uint64_t)(pt_base) << PAGESHIFT,
5800Sstevel@tonic-gate 	    (uint64_t)(tpgs) << PAGESHIFT, &phys_install);
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 	ASSERT(mlret == MEML_SPANOP_OK);
5830Sstevel@tonic-gate 	phys_install_has_changed();
5840Sstevel@tonic-gate 	installed_top_size(phys_install, &physmax, &physinstalled);
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	memlist_write_unlock();
5870Sstevel@tonic-gate 	delspan_unreserve(pt_base, tpgs);
5880Sstevel@tonic-gate }
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate /*
59110106SJason.Beloro@Sun.COM  * Only return an available memseg of exactly the right size
59210106SJason.Beloro@Sun.COM  * if size is required.
5930Sstevel@tonic-gate  * When the meta data area has it's own virtual address space
5940Sstevel@tonic-gate  * we will need to manage this more carefully and do best fit
5955331Samw  * allocations, possibly splitting an available area.
5960Sstevel@tonic-gate  */
59710106SJason.Beloro@Sun.COM struct memseg *
5980Sstevel@tonic-gate memseg_reuse(pgcnt_t metapgs)
5990Sstevel@tonic-gate {
60010106SJason.Beloro@Sun.COM 	int type;
6010Sstevel@tonic-gate 	struct memseg **segpp, *seg;
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	mutex_enter(&memseg_lists_lock);
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	segpp = &memseg_va_avail;
6060Sstevel@tonic-gate 	for (; (seg = *segpp) != NULL; segpp = &seg->lnext) {
6070Sstevel@tonic-gate 		caddr_t end;
6080Sstevel@tonic-gate 
60910106SJason.Beloro@Sun.COM 		/*
61010106SJason.Beloro@Sun.COM 		 * Make sure we are reusing the right segment type.
61110106SJason.Beloro@Sun.COM 		 */
61210106SJason.Beloro@Sun.COM 		type = metapgs ? MEMSEG_META_INCL : MEMSEG_META_ALLOC;
61310106SJason.Beloro@Sun.COM 
61410106SJason.Beloro@Sun.COM 		if ((seg->msegflags & (MEMSEG_META_INCL | MEMSEG_META_ALLOC))
61510106SJason.Beloro@Sun.COM 		    != type)
61610106SJason.Beloro@Sun.COM 			continue;
61710106SJason.Beloro@Sun.COM 
6180Sstevel@tonic-gate 		if (kpm_enable)
6190Sstevel@tonic-gate 			end = hat_kpm_mseg_reuse(seg);
6200Sstevel@tonic-gate 		else
6210Sstevel@tonic-gate 			end = (caddr_t)seg->epages;
6220Sstevel@tonic-gate 
62310106SJason.Beloro@Sun.COM 		/*
62410106SJason.Beloro@Sun.COM 		 * Check for the right size if it is provided.
62510106SJason.Beloro@Sun.COM 		 */
62610106SJason.Beloro@Sun.COM 		if (!metapgs || btopr(end - (caddr_t)seg->pages) == metapgs) {
6270Sstevel@tonic-gate 			*segpp = seg->lnext;
6280Sstevel@tonic-gate 			seg->lnext = NULL;
6290Sstevel@tonic-gate 			break;
6300Sstevel@tonic-gate 		}
6310Sstevel@tonic-gate 	}
6320Sstevel@tonic-gate 	mutex_exit(&memseg_lists_lock);
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	return (seg);
6350Sstevel@tonic-gate }
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate static uint_t handle_gen;
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate struct memdelspan {
6400Sstevel@tonic-gate 	struct memdelspan *mds_next;
6410Sstevel@tonic-gate 	pfn_t		mds_base;
6420Sstevel@tonic-gate 	pgcnt_t		mds_npgs;
6430Sstevel@tonic-gate 	uint_t		*mds_bitmap;
6440Sstevel@tonic-gate 	uint_t		*mds_bitmap_retired;
6450Sstevel@tonic-gate };
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate #define	NBPBMW		(sizeof (uint_t) * NBBY)
6480Sstevel@tonic-gate #define	MDS_BITMAPBYTES(MDSP) \
6490Sstevel@tonic-gate 	((((MDSP)->mds_npgs + NBPBMW - 1) / NBPBMW) * sizeof (uint_t))
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate struct transit_list {
6520Sstevel@tonic-gate 	struct transit_list	*trl_next;
6530Sstevel@tonic-gate 	struct memdelspan	*trl_spans;
6540Sstevel@tonic-gate 	int			trl_collect;
6550Sstevel@tonic-gate };
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate struct transit_list_head {
6580Sstevel@tonic-gate 	kmutex_t		trh_lock;
6590Sstevel@tonic-gate 	struct transit_list	*trh_head;
6600Sstevel@tonic-gate };
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate static struct transit_list_head transit_list_head;
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate struct mem_handle;
6650Sstevel@tonic-gate static void transit_list_collect(struct mem_handle *, int);
6660Sstevel@tonic-gate static void transit_list_insert(struct transit_list *);
6670Sstevel@tonic-gate static void transit_list_remove(struct transit_list *);
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate #ifdef DEBUG
6700Sstevel@tonic-gate #define	MEM_DEL_STATS
6710Sstevel@tonic-gate #endif /* DEBUG */
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate #ifdef MEM_DEL_STATS
6740Sstevel@tonic-gate static int mem_del_stat_print = 0;
6750Sstevel@tonic-gate struct mem_del_stat {
6760Sstevel@tonic-gate 	uint_t	nloop;
6770Sstevel@tonic-gate 	uint_t	need_free;
6780Sstevel@tonic-gate 	uint_t	free_loop;
6790Sstevel@tonic-gate 	uint_t	free_low;
6800Sstevel@tonic-gate 	uint_t	free_failed;
6810Sstevel@tonic-gate 	uint_t	ncheck;
6820Sstevel@tonic-gate 	uint_t	nopaget;
6830Sstevel@tonic-gate 	uint_t	lockfail;
6840Sstevel@tonic-gate 	uint_t	nfree;
6850Sstevel@tonic-gate 	uint_t	nreloc;
6860Sstevel@tonic-gate 	uint_t	nrelocfail;
6870Sstevel@tonic-gate 	uint_t	already_done;
6880Sstevel@tonic-gate 	uint_t	first_notfree;
6890Sstevel@tonic-gate 	uint_t	npplocked;
6900Sstevel@tonic-gate 	uint_t	nlockreloc;
6910Sstevel@tonic-gate 	uint_t	nnorepl;
6920Sstevel@tonic-gate 	uint_t	nmodreloc;
6930Sstevel@tonic-gate 	uint_t	ndestroy;
6940Sstevel@tonic-gate 	uint_t	nputpage;
6950Sstevel@tonic-gate 	uint_t	nnoreclaim;
6960Sstevel@tonic-gate 	uint_t	ndelay;
6970Sstevel@tonic-gate 	uint_t	demotefail;
6980Sstevel@tonic-gate 	uint64_t nticks_total;
6990Sstevel@tonic-gate 	uint64_t nticks_pgrp;
7000Sstevel@tonic-gate 	uint_t	retired;
7010Sstevel@tonic-gate 	uint_t	toxic;
7020Sstevel@tonic-gate 	uint_t	failing;
7030Sstevel@tonic-gate 	uint_t	modtoxic;
7040Sstevel@tonic-gate 	uint_t	npplkdtoxic;
7050Sstevel@tonic-gate 	uint_t	gptlmodfail;
7060Sstevel@tonic-gate 	uint_t	gptllckfail;
7070Sstevel@tonic-gate };
7080Sstevel@tonic-gate /*
7090Sstevel@tonic-gate  * The stat values are only incremented in the delete thread
7100Sstevel@tonic-gate  * so no locking or atomic required.
7110Sstevel@tonic-gate  */
7120Sstevel@tonic-gate #define	MDSTAT_INCR(MHP, FLD)	(MHP)->mh_delstat.FLD++
7130Sstevel@tonic-gate #define	MDSTAT_TOTAL(MHP, ntck)	((MHP)->mh_delstat.nticks_total += (ntck))
7140Sstevel@tonic-gate #define	MDSTAT_PGRP(MHP, ntck)	((MHP)->mh_delstat.nticks_pgrp += (ntck))
7150Sstevel@tonic-gate static void mem_del_stat_print_func(struct mem_handle *);
7160Sstevel@tonic-gate #define	MDSTAT_PRINT(MHP)	mem_del_stat_print_func((MHP))
7170Sstevel@tonic-gate #else /* MEM_DEL_STATS */
7180Sstevel@tonic-gate #define	MDSTAT_INCR(MHP, FLD)
7190Sstevel@tonic-gate #define	MDSTAT_TOTAL(MHP, ntck)
7200Sstevel@tonic-gate #define	MDSTAT_PGRP(MHP, ntck)
7210Sstevel@tonic-gate #define	MDSTAT_PRINT(MHP)
7220Sstevel@tonic-gate #endif /* MEM_DEL_STATS */
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate typedef enum mhnd_state {MHND_FREE = 0, MHND_INIT, MHND_STARTING,
7250Sstevel@tonic-gate 	MHND_RUNNING, MHND_DONE, MHND_RELEASE} mhnd_state_t;
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate /*
7280Sstevel@tonic-gate  * mh_mutex must be taken to examine or change mh_exthandle and mh_state.
7290Sstevel@tonic-gate  * The mutex may not be required for other fields, dependent on mh_state.
7300Sstevel@tonic-gate  */
7310Sstevel@tonic-gate struct mem_handle {
7320Sstevel@tonic-gate 	kmutex_t	mh_mutex;
7330Sstevel@tonic-gate 	struct mem_handle *mh_next;
7340Sstevel@tonic-gate 	memhandle_t	mh_exthandle;
7350Sstevel@tonic-gate 	mhnd_state_t	mh_state;
7360Sstevel@tonic-gate 	struct transit_list mh_transit;
7370Sstevel@tonic-gate 	pgcnt_t		mh_phys_pages;
7380Sstevel@tonic-gate 	pgcnt_t		mh_vm_pages;
7390Sstevel@tonic-gate 	pgcnt_t		mh_hold_todo;
7400Sstevel@tonic-gate 	void		(*mh_delete_complete)(void *, int error);
7410Sstevel@tonic-gate 	void		*mh_delete_complete_arg;
7420Sstevel@tonic-gate 	volatile uint_t mh_cancel;
7430Sstevel@tonic-gate 	volatile uint_t mh_dr_aio_cleanup_cancel;
7440Sstevel@tonic-gate 	volatile uint_t mh_aio_cleanup_done;
7450Sstevel@tonic-gate 	kcondvar_t	mh_cv;
7460Sstevel@tonic-gate 	kthread_id_t	mh_thread_id;
7470Sstevel@tonic-gate 	page_t		*mh_deleted;	/* link through p_next */
7480Sstevel@tonic-gate #ifdef MEM_DEL_STATS
7490Sstevel@tonic-gate 	struct mem_del_stat mh_delstat;
7500Sstevel@tonic-gate #endif /* MEM_DEL_STATS */
7510Sstevel@tonic-gate };
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate static struct mem_handle *mem_handle_head;
7540Sstevel@tonic-gate static kmutex_t mem_handle_list_mutex;
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate static struct mem_handle *
7570Sstevel@tonic-gate kphysm_allocate_mem_handle()
7580Sstevel@tonic-gate {
7590Sstevel@tonic-gate 	struct mem_handle *mhp;
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 	mhp = kmem_zalloc(sizeof (struct mem_handle), KM_SLEEP);
7620Sstevel@tonic-gate 	mutex_init(&mhp->mh_mutex, NULL, MUTEX_DEFAULT, NULL);
7630Sstevel@tonic-gate 	mutex_enter(&mem_handle_list_mutex);
7640Sstevel@tonic-gate 	mutex_enter(&mhp->mh_mutex);
7650Sstevel@tonic-gate 	/* handle_gen is protected by list mutex. */
766567Sdmick 	mhp->mh_exthandle = (memhandle_t)(uintptr_t)(++handle_gen);
7670Sstevel@tonic-gate 	mhp->mh_next = mem_handle_head;
7680Sstevel@tonic-gate 	mem_handle_head = mhp;
7690Sstevel@tonic-gate 	mutex_exit(&mem_handle_list_mutex);
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	return (mhp);
7720Sstevel@tonic-gate }
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate static void
7750Sstevel@tonic-gate kphysm_free_mem_handle(struct mem_handle *mhp)
7760Sstevel@tonic-gate {
7770Sstevel@tonic-gate 	struct mem_handle **mhpp;
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	ASSERT(mutex_owned(&mhp->mh_mutex));
7800Sstevel@tonic-gate 	ASSERT(mhp->mh_state == MHND_FREE);
7810Sstevel@tonic-gate 	/*
7820Sstevel@tonic-gate 	 * Exit the mutex to preserve locking order. This is OK
7830Sstevel@tonic-gate 	 * here as once in the FREE state, the handle cannot
7840Sstevel@tonic-gate 	 * be found by a lookup.
7850Sstevel@tonic-gate 	 */
7860Sstevel@tonic-gate 	mutex_exit(&mhp->mh_mutex);
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 	mutex_enter(&mem_handle_list_mutex);
7890Sstevel@tonic-gate 	mhpp = &mem_handle_head;
7900Sstevel@tonic-gate 	while (*mhpp != NULL && *mhpp != mhp)
7910Sstevel@tonic-gate 		mhpp = &(*mhpp)->mh_next;
7920Sstevel@tonic-gate 	ASSERT(*mhpp == mhp);
7930Sstevel@tonic-gate 	/*
7940Sstevel@tonic-gate 	 * No need to lock the handle (mh_mutex) as only
7950Sstevel@tonic-gate 	 * mh_next changing and this is the only thread that
7960Sstevel@tonic-gate 	 * can be referncing mhp.
7970Sstevel@tonic-gate 	 */
7980Sstevel@tonic-gate 	*mhpp = mhp->mh_next;
7990Sstevel@tonic-gate 	mutex_exit(&mem_handle_list_mutex);
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	mutex_destroy(&mhp->mh_mutex);
8020Sstevel@tonic-gate 	kmem_free(mhp, sizeof (struct mem_handle));
8030Sstevel@tonic-gate }
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate /*
8060Sstevel@tonic-gate  * This function finds the internal mem_handle corresponding to an
8070Sstevel@tonic-gate  * external handle and returns it with the mh_mutex held.
8080Sstevel@tonic-gate  */
8090Sstevel@tonic-gate static struct mem_handle *
8100Sstevel@tonic-gate kphysm_lookup_mem_handle(memhandle_t handle)
8110Sstevel@tonic-gate {
8120Sstevel@tonic-gate 	struct mem_handle *mhp;
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 	mutex_enter(&mem_handle_list_mutex);
8150Sstevel@tonic-gate 	for (mhp = mem_handle_head; mhp != NULL; mhp = mhp->mh_next) {
8160Sstevel@tonic-gate 		if (mhp->mh_exthandle == handle) {
8170Sstevel@tonic-gate 			mutex_enter(&mhp->mh_mutex);
8180Sstevel@tonic-gate 			/*
8190Sstevel@tonic-gate 			 * The state of the handle could have been changed
8200Sstevel@tonic-gate 			 * by kphysm_del_release() while waiting for mh_mutex.
8210Sstevel@tonic-gate 			 */
8220Sstevel@tonic-gate 			if (mhp->mh_state == MHND_FREE) {
8230Sstevel@tonic-gate 				mutex_exit(&mhp->mh_mutex);
8240Sstevel@tonic-gate 				continue;
8250Sstevel@tonic-gate 			}
8260Sstevel@tonic-gate 			break;
8270Sstevel@tonic-gate 		}
8280Sstevel@tonic-gate 	}
8290Sstevel@tonic-gate 	mutex_exit(&mem_handle_list_mutex);
8300Sstevel@tonic-gate 	return (mhp);
8310Sstevel@tonic-gate }
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate int
8340Sstevel@tonic-gate kphysm_del_gethandle(memhandle_t *xmhp)
8350Sstevel@tonic-gate {
8360Sstevel@tonic-gate 	struct mem_handle *mhp;
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 	mhp = kphysm_allocate_mem_handle();
8390Sstevel@tonic-gate 	/*
8400Sstevel@tonic-gate 	 * The handle is allocated using KM_SLEEP, so cannot fail.
8410Sstevel@tonic-gate 	 * If the implementation is changed, the correct error to return
8420Sstevel@tonic-gate 	 * here would be KPHYSM_ENOHANDLES.
8430Sstevel@tonic-gate 	 */
8440Sstevel@tonic-gate 	ASSERT(mhp->mh_state == MHND_FREE);
8450Sstevel@tonic-gate 	mhp->mh_state = MHND_INIT;
8460Sstevel@tonic-gate 	*xmhp = mhp->mh_exthandle;
8470Sstevel@tonic-gate 	mutex_exit(&mhp->mh_mutex);
8480Sstevel@tonic-gate 	return (KPHYSM_OK);
8490Sstevel@tonic-gate }
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate static int
8520Sstevel@tonic-gate overlapping(pfn_t b1, pgcnt_t l1, pfn_t b2, pgcnt_t l2)
8530Sstevel@tonic-gate {
8540Sstevel@tonic-gate 	pfn_t e1, e2;
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 	e1 = b1 + l1;
8570Sstevel@tonic-gate 	e2 = b2 + l2;
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 	return (!(b2 >= e1 || b1 >= e2));
8600Sstevel@tonic-gate }
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate static int can_remove_pgs(pgcnt_t);
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate static struct memdelspan *
8650Sstevel@tonic-gate span_to_install(pfn_t base, pgcnt_t npgs)
8660Sstevel@tonic-gate {
8670Sstevel@tonic-gate 	struct memdelspan *mdsp;
8680Sstevel@tonic-gate 	struct memdelspan *mdsp_new;
8690Sstevel@tonic-gate 	uint64_t address, size, thislen;
8700Sstevel@tonic-gate 	struct memlist *mlp;
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 	mdsp_new = NULL;
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 	address = (uint64_t)base << PAGESHIFT;
8750Sstevel@tonic-gate 	size = (uint64_t)npgs << PAGESHIFT;
8760Sstevel@tonic-gate 	while (size != 0) {
8770Sstevel@tonic-gate 		memlist_read_lock();
8780Sstevel@tonic-gate 		for (mlp = phys_install; mlp != NULL; mlp = mlp->next) {
8790Sstevel@tonic-gate 			if (address >= (mlp->address + mlp->size))
8800Sstevel@tonic-gate 				continue;
8810Sstevel@tonic-gate 			if ((address + size) > mlp->address)
8820Sstevel@tonic-gate 				break;
8830Sstevel@tonic-gate 		}
8840Sstevel@tonic-gate 		if (mlp == NULL) {
8850Sstevel@tonic-gate 			address += size;
8860Sstevel@tonic-gate 			size = 0;
8870Sstevel@tonic-gate 			thislen = 0;
8880Sstevel@tonic-gate 		} else {
8890Sstevel@tonic-gate 			if (address < mlp->address) {
8900Sstevel@tonic-gate 				size -= (mlp->address - address);
8910Sstevel@tonic-gate 				address = mlp->address;
8920Sstevel@tonic-gate 			}
8930Sstevel@tonic-gate 			ASSERT(address >= mlp->address);
8940Sstevel@tonic-gate 			if ((address + size) > (mlp->address + mlp->size)) {
8950Sstevel@tonic-gate 				thislen = mlp->size - (address - mlp->address);
8960Sstevel@tonic-gate 			} else {
8970Sstevel@tonic-gate 				thislen = size;
8980Sstevel@tonic-gate 			}
8990Sstevel@tonic-gate 		}
9000Sstevel@tonic-gate 		memlist_read_unlock();
9010Sstevel@tonic-gate 		/* TODO: phys_install could change now */
9020Sstevel@tonic-gate 		if (thislen == 0)
9030Sstevel@tonic-gate 			continue;
9040Sstevel@tonic-gate 		mdsp = kmem_zalloc(sizeof (struct memdelspan), KM_SLEEP);
9050Sstevel@tonic-gate 		mdsp->mds_base = btop(address);
9060Sstevel@tonic-gate 		mdsp->mds_npgs = btop(thislen);
9070Sstevel@tonic-gate 		mdsp->mds_next = mdsp_new;
9080Sstevel@tonic-gate 		mdsp_new = mdsp;
9090Sstevel@tonic-gate 		address += thislen;
9100Sstevel@tonic-gate 		size -= thislen;
9110Sstevel@tonic-gate 	}
9120Sstevel@tonic-gate 	return (mdsp_new);
9130Sstevel@tonic-gate }
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate static void
9160Sstevel@tonic-gate free_delspans(struct memdelspan *mdsp)
9170Sstevel@tonic-gate {
9180Sstevel@tonic-gate 	struct memdelspan *amdsp;
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 	while ((amdsp = mdsp) != NULL) {
9210Sstevel@tonic-gate 		mdsp = amdsp->mds_next;
9220Sstevel@tonic-gate 		kmem_free(amdsp, sizeof (struct memdelspan));
9230Sstevel@tonic-gate 	}
9240Sstevel@tonic-gate }
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate /*
9270Sstevel@tonic-gate  * Concatenate lists. No list ordering is required.
9280Sstevel@tonic-gate  */
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate static void
9310Sstevel@tonic-gate delspan_concat(struct memdelspan **mdspp, struct memdelspan *mdsp)
9320Sstevel@tonic-gate {
9330Sstevel@tonic-gate 	while (*mdspp != NULL)
9340Sstevel@tonic-gate 		mdspp = &(*mdspp)->mds_next;
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate 	*mdspp = mdsp;
9370Sstevel@tonic-gate }
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate /*
9400Sstevel@tonic-gate  * Given a new list of delspans, check there is no overlap with
9410Sstevel@tonic-gate  * all existing span activity (add or delete) and then concatenate
9420Sstevel@tonic-gate  * the new spans to the given list.
9430Sstevel@tonic-gate  * Return 1 for OK, 0 if overlapping.
9440Sstevel@tonic-gate  */
9450Sstevel@tonic-gate static int
9460Sstevel@tonic-gate delspan_insert(
9470Sstevel@tonic-gate 	struct transit_list *my_tlp,
9480Sstevel@tonic-gate 	struct memdelspan *mdsp_new)
9490Sstevel@tonic-gate {
9500Sstevel@tonic-gate 	struct transit_list_head *trh;
9510Sstevel@tonic-gate 	struct transit_list *tlp;
9520Sstevel@tonic-gate 	int ret;
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 	trh = &transit_list_head;
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 	ASSERT(my_tlp != NULL);
9570Sstevel@tonic-gate 	ASSERT(mdsp_new != NULL);
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate 	ret = 1;
9600Sstevel@tonic-gate 	mutex_enter(&trh->trh_lock);
9610Sstevel@tonic-gate 	/* ASSERT(my_tlp->trl_spans == NULL || tlp_in_list(trh, my_tlp)); */
9620Sstevel@tonic-gate 	for (tlp = trh->trh_head; tlp != NULL; tlp = tlp->trl_next) {
9630Sstevel@tonic-gate 		struct memdelspan *mdsp;
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 		for (mdsp = tlp->trl_spans; mdsp != NULL;
9660Sstevel@tonic-gate 		    mdsp = mdsp->mds_next) {
9670Sstevel@tonic-gate 			struct memdelspan *nmdsp;
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate 			for (nmdsp = mdsp_new; nmdsp != NULL;
9700Sstevel@tonic-gate 			    nmdsp = nmdsp->mds_next) {
9710Sstevel@tonic-gate 				if (overlapping(mdsp->mds_base, mdsp->mds_npgs,
9720Sstevel@tonic-gate 				    nmdsp->mds_base, nmdsp->mds_npgs)) {
9730Sstevel@tonic-gate 					ret = 0;
9740Sstevel@tonic-gate 					goto done;
9750Sstevel@tonic-gate 				}
9760Sstevel@tonic-gate 			}
9770Sstevel@tonic-gate 		}
9780Sstevel@tonic-gate 	}
9790Sstevel@tonic-gate done:
9800Sstevel@tonic-gate 	if (ret != 0) {
9810Sstevel@tonic-gate 		if (my_tlp->trl_spans == NULL)
9820Sstevel@tonic-gate 			transit_list_insert(my_tlp);
9830Sstevel@tonic-gate 		delspan_concat(&my_tlp->trl_spans, mdsp_new);
9840Sstevel@tonic-gate 	}
9850Sstevel@tonic-gate 	mutex_exit(&trh->trh_lock);
9860Sstevel@tonic-gate 	return (ret);
9870Sstevel@tonic-gate }
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate static void
9900Sstevel@tonic-gate delspan_remove(
9910Sstevel@tonic-gate 	struct transit_list *my_tlp,
9920Sstevel@tonic-gate 	pfn_t base,
9930Sstevel@tonic-gate 	pgcnt_t npgs)
9940Sstevel@tonic-gate {
9950Sstevel@tonic-gate 	struct transit_list_head *trh;
9960Sstevel@tonic-gate 	struct memdelspan *mdsp;
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 	trh = &transit_list_head;
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate 	ASSERT(my_tlp != NULL);
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate 	mutex_enter(&trh->trh_lock);
10030Sstevel@tonic-gate 	if ((mdsp = my_tlp->trl_spans) != NULL) {
10040Sstevel@tonic-gate 		if (npgs == 0) {
10050Sstevel@tonic-gate 			my_tlp->trl_spans = NULL;
10060Sstevel@tonic-gate 			free_delspans(mdsp);
10070Sstevel@tonic-gate 			transit_list_remove(my_tlp);
10080Sstevel@tonic-gate 		} else {
10090Sstevel@tonic-gate 			struct memdelspan **prv;
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 			prv = &my_tlp->trl_spans;
10120Sstevel@tonic-gate 			while (mdsp != NULL) {
10130Sstevel@tonic-gate 				pfn_t p_end;
10140Sstevel@tonic-gate 
10150Sstevel@tonic-gate 				p_end = mdsp->mds_base + mdsp->mds_npgs;
10160Sstevel@tonic-gate 				if (mdsp->mds_base >= base &&
10170Sstevel@tonic-gate 				    p_end <= (base + npgs)) {
10180Sstevel@tonic-gate 					*prv = mdsp->mds_next;
10190Sstevel@tonic-gate 					mdsp->mds_next = NULL;
10200Sstevel@tonic-gate 					free_delspans(mdsp);
10210Sstevel@tonic-gate 				} else {
10220Sstevel@tonic-gate 					prv = &mdsp->mds_next;
10230Sstevel@tonic-gate 				}
10240Sstevel@tonic-gate 				mdsp = *prv;
10250Sstevel@tonic-gate 			}
10260Sstevel@tonic-gate 			if (my_tlp->trl_spans == NULL)
10270Sstevel@tonic-gate 				transit_list_remove(my_tlp);
10280Sstevel@tonic-gate 		}
10290Sstevel@tonic-gate 	}
10300Sstevel@tonic-gate 	mutex_exit(&trh->trh_lock);
10310Sstevel@tonic-gate }
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate /*
10340Sstevel@tonic-gate  * Reserve interface for add to stop delete before add finished.
10350Sstevel@tonic-gate  * This list is only accessed through the delspan_insert/remove
10360Sstevel@tonic-gate  * functions and so is fully protected by the mutex in struct transit_list.
10370Sstevel@tonic-gate  */
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate static struct transit_list reserve_transit;
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate static int
10420Sstevel@tonic-gate delspan_reserve(pfn_t base, pgcnt_t npgs)
10430Sstevel@tonic-gate {
10440Sstevel@tonic-gate 	struct memdelspan *mdsp;
10450Sstevel@tonic-gate 	int ret;
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 	mdsp = kmem_zalloc(sizeof (struct memdelspan), KM_SLEEP);
10480Sstevel@tonic-gate 	mdsp->mds_base = base;
10490Sstevel@tonic-gate 	mdsp->mds_npgs = npgs;
10500Sstevel@tonic-gate 	if ((ret = delspan_insert(&reserve_transit, mdsp)) == 0) {
10510Sstevel@tonic-gate 		free_delspans(mdsp);
10520Sstevel@tonic-gate 	}
10530Sstevel@tonic-gate 	return (ret);
10540Sstevel@tonic-gate }
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate static void
10570Sstevel@tonic-gate delspan_unreserve(pfn_t base, pgcnt_t npgs)
10580Sstevel@tonic-gate {
10590Sstevel@tonic-gate 	delspan_remove(&reserve_transit, base, npgs);
10600Sstevel@tonic-gate }
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate /*
10630Sstevel@tonic-gate  * Return whether memseg was created by kphysm_add_memory_dynamic().
10640Sstevel@tonic-gate  */
10650Sstevel@tonic-gate static int
106610106SJason.Beloro@Sun.COM memseg_is_dynamic(struct memseg *seg)
10670Sstevel@tonic-gate {
106810106SJason.Beloro@Sun.COM 	return (seg->msegflags & MEMSEG_DYNAMIC);
10690Sstevel@tonic-gate }
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate int
10720Sstevel@tonic-gate kphysm_del_span(
10730Sstevel@tonic-gate 	memhandle_t handle,
10740Sstevel@tonic-gate 	pfn_t base,
10750Sstevel@tonic-gate 	pgcnt_t npgs)
10760Sstevel@tonic-gate {
10770Sstevel@tonic-gate 	struct mem_handle *mhp;
10780Sstevel@tonic-gate 	struct memseg *seg;
10790Sstevel@tonic-gate 	struct memdelspan *mdsp;
10800Sstevel@tonic-gate 	struct memdelspan *mdsp_new;
10810Sstevel@tonic-gate 	pgcnt_t phys_pages, vm_pages;
10820Sstevel@tonic-gate 	pfn_t p_end;
10830Sstevel@tonic-gate 	page_t *pp;
10840Sstevel@tonic-gate 	int ret;
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	mhp = kphysm_lookup_mem_handle(handle);
10870Sstevel@tonic-gate 	if (mhp == NULL) {
10880Sstevel@tonic-gate 		return (KPHYSM_EHANDLE);
10890Sstevel@tonic-gate 	}
10900Sstevel@tonic-gate 	if (mhp->mh_state != MHND_INIT) {
10910Sstevel@tonic-gate 		mutex_exit(&mhp->mh_mutex);
10920Sstevel@tonic-gate 		return (KPHYSM_ESEQUENCE);
10930Sstevel@tonic-gate 	}
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 	/*
10960Sstevel@tonic-gate 	 * Intersect the span with the installed memory list (phys_install).
10970Sstevel@tonic-gate 	 */
10980Sstevel@tonic-gate 	mdsp_new = span_to_install(base, npgs);
10990Sstevel@tonic-gate 	if (mdsp_new == NULL) {
11000Sstevel@tonic-gate 		/*
11010Sstevel@tonic-gate 		 * No physical memory in this range. Is this an
11020Sstevel@tonic-gate 		 * error? If an attempt to start the delete is made
11030Sstevel@tonic-gate 		 * for OK returns from del_span such as this, start will
11040Sstevel@tonic-gate 		 * return an error.
11050Sstevel@tonic-gate 		 * Could return KPHYSM_ENOWORK.
11060Sstevel@tonic-gate 		 */
11070Sstevel@tonic-gate 		/*
11080Sstevel@tonic-gate 		 * It is assumed that there are no error returns
11090Sstevel@tonic-gate 		 * from span_to_install() due to kmem_alloc failure.
11100Sstevel@tonic-gate 		 */
11110Sstevel@tonic-gate 		mutex_exit(&mhp->mh_mutex);
11120Sstevel@tonic-gate 		return (KPHYSM_OK);
11130Sstevel@tonic-gate 	}
11140Sstevel@tonic-gate 	/*
11150Sstevel@tonic-gate 	 * Does this span overlap an existing span?
11160Sstevel@tonic-gate 	 */
11170Sstevel@tonic-gate 	if (delspan_insert(&mhp->mh_transit, mdsp_new) == 0) {
11180Sstevel@tonic-gate 		/*
11190Sstevel@tonic-gate 		 * Differentiate between already on list for this handle
11200Sstevel@tonic-gate 		 * (KPHYSM_EDUP) and busy elsewhere (KPHYSM_EBUSY).
11210Sstevel@tonic-gate 		 */
11220Sstevel@tonic-gate 		ret = KPHYSM_EBUSY;
11230Sstevel@tonic-gate 		for (mdsp = mhp->mh_transit.trl_spans; mdsp != NULL;
11240Sstevel@tonic-gate 		    mdsp = mdsp->mds_next) {
11250Sstevel@tonic-gate 			if (overlapping(mdsp->mds_base, mdsp->mds_npgs,
11260Sstevel@tonic-gate 			    base, npgs)) {
11270Sstevel@tonic-gate 				ret = KPHYSM_EDUP;
11280Sstevel@tonic-gate 				break;
11290Sstevel@tonic-gate 			}
11300Sstevel@tonic-gate 		}
11310Sstevel@tonic-gate 		mutex_exit(&mhp->mh_mutex);
11320Sstevel@tonic-gate 		free_delspans(mdsp_new);
11330Sstevel@tonic-gate 		return (ret);
11340Sstevel@tonic-gate 	}
11350Sstevel@tonic-gate 	/*
11360Sstevel@tonic-gate 	 * At this point the spans in mdsp_new have been inserted into the
11370Sstevel@tonic-gate 	 * list of spans for this handle and thereby to the global list of
11380Sstevel@tonic-gate 	 * spans being processed. Each of these spans must now be checked
11390Sstevel@tonic-gate 	 * for relocatability. As a side-effect segments in the memseg list
11400Sstevel@tonic-gate 	 * may be split.
11410Sstevel@tonic-gate 	 *
11420Sstevel@tonic-gate 	 * Note that mdsp_new can no longer be used as it is now part of
11430Sstevel@tonic-gate 	 * a larger list. Select elements of this larger list based
11440Sstevel@tonic-gate 	 * on base and npgs.
11450Sstevel@tonic-gate 	 */
11460Sstevel@tonic-gate restart:
11470Sstevel@tonic-gate 	phys_pages = 0;
11480Sstevel@tonic-gate 	vm_pages = 0;
11490Sstevel@tonic-gate 	ret = KPHYSM_OK;
11500Sstevel@tonic-gate 	for (mdsp = mhp->mh_transit.trl_spans; mdsp != NULL;
11510Sstevel@tonic-gate 	    mdsp = mdsp->mds_next) {
11520Sstevel@tonic-gate 		pgcnt_t pages_checked;
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 		if (!overlapping(mdsp->mds_base, mdsp->mds_npgs, base, npgs)) {
11550Sstevel@tonic-gate 			continue;
11560Sstevel@tonic-gate 		}
11570Sstevel@tonic-gate 		p_end = mdsp->mds_base + mdsp->mds_npgs;
11580Sstevel@tonic-gate 		/*
11590Sstevel@tonic-gate 		 * The pages_checked count is a hack. All pages should be
11600Sstevel@tonic-gate 		 * checked for relocatability. Those not covered by memsegs
11610Sstevel@tonic-gate 		 * should be tested with arch_kphysm_del_span_ok().
11620Sstevel@tonic-gate 		 */
11630Sstevel@tonic-gate 		pages_checked = 0;
11640Sstevel@tonic-gate 		for (seg = memsegs; seg; seg = seg->next) {
11650Sstevel@tonic-gate 			pfn_t mseg_start;
11660Sstevel@tonic-gate 
11670Sstevel@tonic-gate 			if (seg->pages_base >= p_end ||
11680Sstevel@tonic-gate 			    seg->pages_end <= mdsp->mds_base) {
11690Sstevel@tonic-gate 				/* Span and memseg don't overlap. */
11700Sstevel@tonic-gate 				continue;
11710Sstevel@tonic-gate 			}
117210106SJason.Beloro@Sun.COM 			mseg_start = memseg_get_start(seg);
11730Sstevel@tonic-gate 			/* Check that segment is suitable for delete. */
117410106SJason.Beloro@Sun.COM 			if (memseg_includes_meta(seg)) {
11750Sstevel@tonic-gate 				/*
117610106SJason.Beloro@Sun.COM 				 * Check that this segment is completely
117710106SJason.Beloro@Sun.COM 				 * within the span.
11780Sstevel@tonic-gate 				 */
11790Sstevel@tonic-gate 				if (mseg_start < mdsp->mds_base ||
11800Sstevel@tonic-gate 				    seg->pages_end > p_end) {
11810Sstevel@tonic-gate 					ret = KPHYSM_EBUSY;
11820Sstevel@tonic-gate 					break;
11830Sstevel@tonic-gate 				}
11840Sstevel@tonic-gate 				pages_checked += seg->pages_end - mseg_start;
11850Sstevel@tonic-gate 			} else {
11860Sstevel@tonic-gate 				/*
11870Sstevel@tonic-gate 				 * If this segment is larger than the span,
11880Sstevel@tonic-gate 				 * try to split it. After the split, it
11890Sstevel@tonic-gate 				 * is necessary to restart.
11900Sstevel@tonic-gate 				 */
11910Sstevel@tonic-gate 				if (seg->pages_base < mdsp->mds_base ||
11920Sstevel@tonic-gate 				    seg->pages_end > p_end) {
11930Sstevel@tonic-gate 					pfn_t abase;
11940Sstevel@tonic-gate 					pgcnt_t anpgs;
11950Sstevel@tonic-gate 					int s_ret;
11960Sstevel@tonic-gate 
11970Sstevel@tonic-gate 					/* Split required.  */
11980Sstevel@tonic-gate 					if (mdsp->mds_base < seg->pages_base)
11990Sstevel@tonic-gate 						abase = seg->pages_base;
12000Sstevel@tonic-gate 					else
12010Sstevel@tonic-gate 						abase = mdsp->mds_base;
12020Sstevel@tonic-gate 					if (p_end > seg->pages_end)
12030Sstevel@tonic-gate 						anpgs = seg->pages_end - abase;
12040Sstevel@tonic-gate 					else
12050Sstevel@tonic-gate 						anpgs = p_end - abase;
12060Sstevel@tonic-gate 					s_ret = kphysm_split_memseg(abase,
12070Sstevel@tonic-gate 					    anpgs);
12080Sstevel@tonic-gate 					if (s_ret == 0) {
12090Sstevel@tonic-gate 						/* Split failed. */
12100Sstevel@tonic-gate 						ret = KPHYSM_ERESOURCE;
12110Sstevel@tonic-gate 						break;
12120Sstevel@tonic-gate 					}
12130Sstevel@tonic-gate 					goto restart;
12140Sstevel@tonic-gate 				}
12150Sstevel@tonic-gate 				pages_checked +=
12160Sstevel@tonic-gate 				    seg->pages_end - seg->pages_base;
12170Sstevel@tonic-gate 			}
12180Sstevel@tonic-gate 			/*
12190Sstevel@tonic-gate 			 * The memseg is wholly within the delete span.
12200Sstevel@tonic-gate 			 * The individual pages can now be checked.
12210Sstevel@tonic-gate 			 */
12220Sstevel@tonic-gate 			/* Cage test. */
12230Sstevel@tonic-gate 			for (pp = seg->pages; pp < seg->epages; pp++) {
12240Sstevel@tonic-gate 				if (PP_ISNORELOC(pp)) {
12250Sstevel@tonic-gate 					ret = KPHYSM_ENONRELOC;
12260Sstevel@tonic-gate 					break;
12270Sstevel@tonic-gate 				}
12280Sstevel@tonic-gate 			}
12290Sstevel@tonic-gate 			if (ret != KPHYSM_OK) {
12300Sstevel@tonic-gate 				break;
12310Sstevel@tonic-gate 			}
12320Sstevel@tonic-gate 			phys_pages += (seg->pages_end - mseg_start);
12330Sstevel@tonic-gate 			vm_pages += MSEG_NPAGES(seg);
12340Sstevel@tonic-gate 		}
12350Sstevel@tonic-gate 		if (ret != KPHYSM_OK)
12360Sstevel@tonic-gate 			break;
12370Sstevel@tonic-gate 		if (pages_checked != mdsp->mds_npgs) {
12380Sstevel@tonic-gate 			ret = KPHYSM_ENONRELOC;
12390Sstevel@tonic-gate 			break;
12400Sstevel@tonic-gate 		}
12410Sstevel@tonic-gate 	}
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 	if (ret == KPHYSM_OK) {
12440Sstevel@tonic-gate 		mhp->mh_phys_pages += phys_pages;
12450Sstevel@tonic-gate 		mhp->mh_vm_pages += vm_pages;
12460Sstevel@tonic-gate 	} else {
12470Sstevel@tonic-gate 		/*
12480Sstevel@tonic-gate 		 * Keep holding the mh_mutex to prevent it going away.
12490Sstevel@tonic-gate 		 */
12500Sstevel@tonic-gate 		delspan_remove(&mhp->mh_transit, base, npgs);
12510Sstevel@tonic-gate 	}
12520Sstevel@tonic-gate 	mutex_exit(&mhp->mh_mutex);
12530Sstevel@tonic-gate 	return (ret);
12540Sstevel@tonic-gate }
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate int
12570Sstevel@tonic-gate kphysm_del_span_query(
12580Sstevel@tonic-gate 	pfn_t base,
12590Sstevel@tonic-gate 	pgcnt_t npgs,
12600Sstevel@tonic-gate 	memquery_t *mqp)
12610Sstevel@tonic-gate {
12620Sstevel@tonic-gate 	struct memdelspan *mdsp;
12630Sstevel@tonic-gate 	struct memdelspan *mdsp_new;
12640Sstevel@tonic-gate 	int done_first_nonreloc;
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate 	mqp->phys_pages = 0;
12670Sstevel@tonic-gate 	mqp->managed = 0;
12680Sstevel@tonic-gate 	mqp->nonrelocatable = 0;
12690Sstevel@tonic-gate 	mqp->first_nonrelocatable = 0;
12700Sstevel@tonic-gate 	mqp->last_nonrelocatable = 0;
12710Sstevel@tonic-gate 
12720Sstevel@tonic-gate 	mdsp_new = span_to_install(base, npgs);
12730Sstevel@tonic-gate 	/*
12740Sstevel@tonic-gate 	 * It is OK to proceed here if mdsp_new == NULL.
12750Sstevel@tonic-gate 	 */
12760Sstevel@tonic-gate 	done_first_nonreloc = 0;
12770Sstevel@tonic-gate 	for (mdsp = mdsp_new; mdsp != NULL; mdsp = mdsp->mds_next) {
12780Sstevel@tonic-gate 		pfn_t sbase;
12790Sstevel@tonic-gate 		pgcnt_t snpgs;
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 		mqp->phys_pages += mdsp->mds_npgs;
12820Sstevel@tonic-gate 		sbase = mdsp->mds_base;
12830Sstevel@tonic-gate 		snpgs = mdsp->mds_npgs;
12840Sstevel@tonic-gate 		while (snpgs != 0) {
12850Sstevel@tonic-gate 			struct memseg *lseg, *seg;
12860Sstevel@tonic-gate 			pfn_t p_end;
12870Sstevel@tonic-gate 			page_t *pp;
12880Sstevel@tonic-gate 			pfn_t mseg_start;
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate 			p_end = sbase + snpgs;
12910Sstevel@tonic-gate 			/*
12920Sstevel@tonic-gate 			 * Find the lowest addressed memseg that starts
12930Sstevel@tonic-gate 			 * after sbase and account for it.
12940Sstevel@tonic-gate 			 * This is to catch dynamic memsegs whose start
12950Sstevel@tonic-gate 			 * is hidden.
12960Sstevel@tonic-gate 			 */
12970Sstevel@tonic-gate 			seg = NULL;
12980Sstevel@tonic-gate 			for (lseg = memsegs; lseg != NULL; lseg = lseg->next) {
12990Sstevel@tonic-gate 				if ((lseg->pages_base >= sbase) ||
13000Sstevel@tonic-gate 				    (lseg->pages_base < p_end &&
13010Sstevel@tonic-gate 				    lseg->pages_end > sbase)) {
13020Sstevel@tonic-gate 					if (seg == NULL ||
13030Sstevel@tonic-gate 					    seg->pages_base > lseg->pages_base)
13040Sstevel@tonic-gate 						seg = lseg;
13050Sstevel@tonic-gate 				}
13060Sstevel@tonic-gate 			}
13070Sstevel@tonic-gate 			if (seg != NULL) {
130810106SJason.Beloro@Sun.COM 				mseg_start = memseg_get_start(seg);
13090Sstevel@tonic-gate 				/*
13100Sstevel@tonic-gate 				 * Now have the full extent of the memseg so
13110Sstevel@tonic-gate 				 * do the range check.
13120Sstevel@tonic-gate 				 */
13130Sstevel@tonic-gate 				if (mseg_start >= p_end ||
13140Sstevel@tonic-gate 				    seg->pages_end <= sbase) {
13150Sstevel@tonic-gate 					/* Span does not overlap memseg. */
13160Sstevel@tonic-gate 					seg = NULL;
13170Sstevel@tonic-gate 				}
13180Sstevel@tonic-gate 			}
13190Sstevel@tonic-gate 			/*
13200Sstevel@tonic-gate 			 * Account for gap either before the segment if
13210Sstevel@tonic-gate 			 * there is one or to the end of the span.
13220Sstevel@tonic-gate 			 */
13230Sstevel@tonic-gate 			if (seg == NULL || mseg_start > sbase) {
13240Sstevel@tonic-gate 				pfn_t a_end;
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate 				a_end = (seg == NULL) ? p_end : mseg_start;
13270Sstevel@tonic-gate 				/*
13280Sstevel@tonic-gate 				 * Check with arch layer for relocatability.
13290Sstevel@tonic-gate 				 */
13300Sstevel@tonic-gate 				if (arch_kphysm_del_span_ok(sbase,
13310Sstevel@tonic-gate 				    (a_end - sbase))) {
13320Sstevel@tonic-gate 					/*
13330Sstevel@tonic-gate 					 * No non-relocatble pages in this
13340Sstevel@tonic-gate 					 * area, avoid the fine-grained
13350Sstevel@tonic-gate 					 * test.
13360Sstevel@tonic-gate 					 */
13370Sstevel@tonic-gate 					snpgs -= (a_end - sbase);
13380Sstevel@tonic-gate 					sbase = a_end;
13390Sstevel@tonic-gate 				}
13400Sstevel@tonic-gate 				while (sbase < a_end) {
13410Sstevel@tonic-gate 					if (!arch_kphysm_del_span_ok(sbase,
13420Sstevel@tonic-gate 					    1)) {
13430Sstevel@tonic-gate 						mqp->nonrelocatable++;
13440Sstevel@tonic-gate 						if (!done_first_nonreloc) {
13450Sstevel@tonic-gate 							mqp->
13460Sstevel@tonic-gate 							    first_nonrelocatable
13470Sstevel@tonic-gate 							    = sbase;
13480Sstevel@tonic-gate 							done_first_nonreloc = 1;
13490Sstevel@tonic-gate 						}
13500Sstevel@tonic-gate 						mqp->last_nonrelocatable =
13510Sstevel@tonic-gate 						    sbase;
13520Sstevel@tonic-gate 					}
13530Sstevel@tonic-gate 					sbase++;
13540Sstevel@tonic-gate 					snpgs--;
13550Sstevel@tonic-gate 				}
13560Sstevel@tonic-gate 			}
13570Sstevel@tonic-gate 			if (seg != NULL) {
13580Sstevel@tonic-gate 				ASSERT(mseg_start <= sbase);
13590Sstevel@tonic-gate 				if (seg->pages_base != mseg_start &&
13600Sstevel@tonic-gate 				    seg->pages_base > sbase) {
13610Sstevel@tonic-gate 					pgcnt_t skip_pgs;
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate 					/*
13640Sstevel@tonic-gate 					 * Skip the page_t area of a
13650Sstevel@tonic-gate 					 * dynamic memseg.
13660Sstevel@tonic-gate 					 */
13670Sstevel@tonic-gate 					skip_pgs = seg->pages_base - sbase;
13680Sstevel@tonic-gate 					if (snpgs <= skip_pgs) {
13690Sstevel@tonic-gate 						sbase += snpgs;
13700Sstevel@tonic-gate 						snpgs = 0;
13710Sstevel@tonic-gate 						continue;
13720Sstevel@tonic-gate 					}
13730Sstevel@tonic-gate 					snpgs -= skip_pgs;
13740Sstevel@tonic-gate 					sbase += skip_pgs;
13750Sstevel@tonic-gate 				}
13760Sstevel@tonic-gate 				ASSERT(snpgs != 0);
13770Sstevel@tonic-gate 				ASSERT(seg->pages_base <= sbase);
13780Sstevel@tonic-gate 				/*
13790Sstevel@tonic-gate 				 * The individual pages can now be checked.
13800Sstevel@tonic-gate 				 */
13810Sstevel@tonic-gate 				for (pp = seg->pages +
13820Sstevel@tonic-gate 				    (sbase - seg->pages_base);
13830Sstevel@tonic-gate 				    snpgs != 0 && pp < seg->epages; pp++) {
13840Sstevel@tonic-gate 					mqp->managed++;
13850Sstevel@tonic-gate 					if (PP_ISNORELOC(pp)) {
13860Sstevel@tonic-gate 						mqp->nonrelocatable++;
13870Sstevel@tonic-gate 						if (!done_first_nonreloc) {
13880Sstevel@tonic-gate 							mqp->
13890Sstevel@tonic-gate 							    first_nonrelocatable
13900Sstevel@tonic-gate 							    = sbase;
13910Sstevel@tonic-gate 							done_first_nonreloc = 1;
13920Sstevel@tonic-gate 						}
13930Sstevel@tonic-gate 						mqp->last_nonrelocatable =
13940Sstevel@tonic-gate 						    sbase;
13950Sstevel@tonic-gate 					}
13960Sstevel@tonic-gate 					sbase++;
13970Sstevel@tonic-gate 					snpgs--;
13980Sstevel@tonic-gate 				}
13990Sstevel@tonic-gate 			}
14000Sstevel@tonic-gate 		}
14010Sstevel@tonic-gate 	}
14020Sstevel@tonic-gate 
14030Sstevel@tonic-gate 	free_delspans(mdsp_new);
14040Sstevel@tonic-gate 
14050Sstevel@tonic-gate 	return (KPHYSM_OK);
14060Sstevel@tonic-gate }
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate /*
14090Sstevel@tonic-gate  * This release function can be called at any stage as follows:
14100Sstevel@tonic-gate  *	_gethandle only called
14110Sstevel@tonic-gate  *	_span(s) only called
14120Sstevel@tonic-gate  *	_start called but failed
14130Sstevel@tonic-gate  *	delete thread exited
14140Sstevel@tonic-gate  */
14150Sstevel@tonic-gate int
14160Sstevel@tonic-gate kphysm_del_release(memhandle_t handle)
14170Sstevel@tonic-gate {
14180Sstevel@tonic-gate 	struct mem_handle *mhp;
14190Sstevel@tonic-gate 
14200Sstevel@tonic-gate 	mhp = kphysm_lookup_mem_handle(handle);
14210Sstevel@tonic-gate 	if (mhp == NULL) {
14220Sstevel@tonic-gate 		return (KPHYSM_EHANDLE);
14230Sstevel@tonic-gate 	}
14240Sstevel@tonic-gate 	switch (mhp->mh_state) {
14250Sstevel@tonic-gate 	case MHND_STARTING:
14260Sstevel@tonic-gate 	case MHND_RUNNING:
14270Sstevel@tonic-gate 		mutex_exit(&mhp->mh_mutex);
14280Sstevel@tonic-gate 		return (KPHYSM_ENOTFINISHED);
14290Sstevel@tonic-gate 	case MHND_FREE:
14300Sstevel@tonic-gate 		ASSERT(mhp->mh_state != MHND_FREE);
14310Sstevel@tonic-gate 		mutex_exit(&mhp->mh_mutex);
14320Sstevel@tonic-gate 		return (KPHYSM_EHANDLE);
14330Sstevel@tonic-gate 	case MHND_INIT:
14340Sstevel@tonic-gate 		break;
14350Sstevel@tonic-gate 	case MHND_DONE:
14360Sstevel@tonic-gate 		break;
14370Sstevel@tonic-gate 	case MHND_RELEASE:
14380Sstevel@tonic-gate 		mutex_exit(&mhp->mh_mutex);
14390Sstevel@tonic-gate 		return (KPHYSM_ESEQUENCE);
14400Sstevel@tonic-gate 	default:
14410Sstevel@tonic-gate #ifdef DEBUG
14420Sstevel@tonic-gate 		cmn_err(CE_WARN, "kphysm_del_release(0x%p) state corrupt %d",
14430Sstevel@tonic-gate 		    (void *)mhp, mhp->mh_state);
14440Sstevel@tonic-gate #endif /* DEBUG */
14450Sstevel@tonic-gate 		mutex_exit(&mhp->mh_mutex);
14460Sstevel@tonic-gate 		return (KPHYSM_EHANDLE);
14470Sstevel@tonic-gate 	}
14480Sstevel@tonic-gate 	/*
14490Sstevel@tonic-gate 	 * Set state so that we can wait if necessary.
14500Sstevel@tonic-gate 	 * Also this means that we have read/write access to all
14510Sstevel@tonic-gate 	 * fields except mh_exthandle and mh_state.
14520Sstevel@tonic-gate 	 */
14530Sstevel@tonic-gate 	mhp->mh_state = MHND_RELEASE;
14540Sstevel@tonic-gate 	/*
14550Sstevel@tonic-gate 	 * The mem_handle cannot be de-allocated by any other operation
14560Sstevel@tonic-gate 	 * now, so no need to hold mh_mutex.
14570Sstevel@tonic-gate 	 */
14580Sstevel@tonic-gate 	mutex_exit(&mhp->mh_mutex);
14590Sstevel@tonic-gate 
14600Sstevel@tonic-gate 	delspan_remove(&mhp->mh_transit, 0, 0);
14610Sstevel@tonic-gate 	mhp->mh_phys_pages = 0;
14620Sstevel@tonic-gate 	mhp->mh_vm_pages = 0;
14630Sstevel@tonic-gate 	mhp->mh_hold_todo = 0;
14640Sstevel@tonic-gate 	mhp->mh_delete_complete = NULL;
14650Sstevel@tonic-gate 	mhp->mh_delete_complete_arg = NULL;
14660Sstevel@tonic-gate 	mhp->mh_cancel = 0;
14670Sstevel@tonic-gate 
14680Sstevel@tonic-gate 	mutex_enter(&mhp->mh_mutex);
14690Sstevel@tonic-gate 	ASSERT(mhp->mh_state == MHND_RELEASE);
14700Sstevel@tonic-gate 	mhp->mh_state = MHND_FREE;
14710Sstevel@tonic-gate 
14720Sstevel@tonic-gate 	kphysm_free_mem_handle(mhp);
14730Sstevel@tonic-gate 
14740Sstevel@tonic-gate 	return (KPHYSM_OK);
14750Sstevel@tonic-gate }
14760Sstevel@tonic-gate 
14770Sstevel@tonic-gate /*
14780Sstevel@tonic-gate  * This cancel function can only be called with the thread running.
14790Sstevel@tonic-gate  */
14800Sstevel@tonic-gate int
14810Sstevel@tonic-gate kphysm_del_cancel(memhandle_t handle)
14820Sstevel@tonic-gate {
14830Sstevel@tonic-gate 	struct mem_handle *mhp;
14840Sstevel@tonic-gate 
14850Sstevel@tonic-gate 	mhp = kphysm_lookup_mem_handle(handle);
14860Sstevel@tonic-gate 	if (mhp == NULL) {
14870Sstevel@tonic-gate 		return (KPHYSM_EHANDLE);
14880Sstevel@tonic-gate 	}
14890Sstevel@tonic-gate 	if (mhp->mh_state != MHND_STARTING && mhp->mh_state != MHND_RUNNING) {
14900Sstevel@tonic-gate 		mutex_exit(&mhp->mh_mutex);
14910Sstevel@tonic-gate 		return (KPHYSM_ENOTRUNNING);
14920Sstevel@tonic-gate 	}
14930Sstevel@tonic-gate 	/*
14940Sstevel@tonic-gate 	 * Set the cancel flag and wake the delete thread up.
14950Sstevel@tonic-gate 	 * The thread may be waiting on I/O, so the effect of the cancel
14960Sstevel@tonic-gate 	 * may be delayed.
14970Sstevel@tonic-gate 	 */
14980Sstevel@tonic-gate 	if (mhp->mh_cancel == 0) {
14990Sstevel@tonic-gate 		mhp->mh_cancel = KPHYSM_ECANCELLED;
15000Sstevel@tonic-gate 		cv_signal(&mhp->mh_cv);
15010Sstevel@tonic-gate 	}
15020Sstevel@tonic-gate 	mutex_exit(&mhp->mh_mutex);
15030Sstevel@tonic-gate 	return (KPHYSM_OK);
15040Sstevel@tonic-gate }
15050Sstevel@tonic-gate 
15060Sstevel@tonic-gate int
15070Sstevel@tonic-gate kphysm_del_status(
15080Sstevel@tonic-gate 	memhandle_t handle,
15090Sstevel@tonic-gate 	memdelstat_t *mdstp)
15100Sstevel@tonic-gate {
15110Sstevel@tonic-gate 	struct mem_handle *mhp;
15120Sstevel@tonic-gate 
15130Sstevel@tonic-gate 	mhp = kphysm_lookup_mem_handle(handle);
15140Sstevel@tonic-gate 	if (mhp == NULL) {
15150Sstevel@tonic-gate 		return (KPHYSM_EHANDLE);
15160Sstevel@tonic-gate 	}
15170Sstevel@tonic-gate 	/*
15180Sstevel@tonic-gate 	 * Calling kphysm_del_status() is allowed before the delete
15190Sstevel@tonic-gate 	 * is started to allow for status display.
15200Sstevel@tonic-gate 	 */
15210Sstevel@tonic-gate 	if (mhp->mh_state != MHND_INIT && mhp->mh_state != MHND_STARTING &&
15220Sstevel@tonic-gate 	    mhp->mh_state != MHND_RUNNING) {
15230Sstevel@tonic-gate 		mutex_exit(&mhp->mh_mutex);
15240Sstevel@tonic-gate 		return (KPHYSM_ENOTRUNNING);
15250Sstevel@tonic-gate 	}
15260Sstevel@tonic-gate 	mdstp->phys_pages = mhp->mh_phys_pages;
15270Sstevel@tonic-gate 	mdstp->managed = mhp->mh_vm_pages;
15280Sstevel@tonic-gate 	mdstp->collected = mhp->mh_vm_pages - mhp->mh_hold_todo;
15290Sstevel@tonic-gate 	mutex_exit(&mhp->mh_mutex);
15300Sstevel@tonic-gate 	return (KPHYSM_OK);
15310Sstevel@tonic-gate }
15320Sstevel@tonic-gate 
15330Sstevel@tonic-gate static int mem_delete_additional_pages = 100;
15340Sstevel@tonic-gate 
15350Sstevel@tonic-gate static int
15360Sstevel@tonic-gate can_remove_pgs(pgcnt_t npgs)
15370Sstevel@tonic-gate {
15380Sstevel@tonic-gate 	/*
15390Sstevel@tonic-gate 	 * If all pageable pages were paged out, freemem would
15400Sstevel@tonic-gate 	 * equal availrmem.  There is a minimum requirement for
15410Sstevel@tonic-gate 	 * availrmem.
15420Sstevel@tonic-gate 	 */
15430Sstevel@tonic-gate 	if ((availrmem - (tune.t_minarmem + mem_delete_additional_pages))
15440Sstevel@tonic-gate 	    < npgs)
15450Sstevel@tonic-gate 		return (0);
15460Sstevel@tonic-gate 	/* TODO: check swap space, etc. */
15470Sstevel@tonic-gate 	return (1);
15480Sstevel@tonic-gate }
15490Sstevel@tonic-gate 
15500Sstevel@tonic-gate static int
15510Sstevel@tonic-gate get_availrmem(pgcnt_t npgs)
15520Sstevel@tonic-gate {
15530Sstevel@tonic-gate 	int ret;
15540Sstevel@tonic-gate 
15550Sstevel@tonic-gate 	mutex_enter(&freemem_lock);
15560Sstevel@tonic-gate 	ret = can_remove_pgs(npgs);
15570Sstevel@tonic-gate 	if (ret != 0)
15580Sstevel@tonic-gate 		availrmem -= npgs;
15590Sstevel@tonic-gate 	mutex_exit(&freemem_lock);
15600Sstevel@tonic-gate 	return (ret);
15610Sstevel@tonic-gate }
15620Sstevel@tonic-gate 
15630Sstevel@tonic-gate static void
15640Sstevel@tonic-gate put_availrmem(pgcnt_t npgs)
15650Sstevel@tonic-gate {
15660Sstevel@tonic-gate 	mutex_enter(&freemem_lock);
15670Sstevel@tonic-gate 	availrmem += npgs;
15680Sstevel@tonic-gate 	mutex_exit(&freemem_lock);
15690Sstevel@tonic-gate }
15700Sstevel@tonic-gate 
15710Sstevel@tonic-gate #define	FREEMEM_INCR	100
15720Sstevel@tonic-gate static pgcnt_t freemem_incr = FREEMEM_INCR;
15730Sstevel@tonic-gate #define	DEL_FREE_WAIT_FRAC	4
15740Sstevel@tonic-gate #define	DEL_FREE_WAIT_TICKS	((hz+DEL_FREE_WAIT_FRAC-1)/DEL_FREE_WAIT_FRAC)
15750Sstevel@tonic-gate 
15760Sstevel@tonic-gate #define	DEL_BUSY_WAIT_FRAC	20
15770Sstevel@tonic-gate #define	DEL_BUSY_WAIT_TICKS	((hz+DEL_BUSY_WAIT_FRAC-1)/DEL_BUSY_WAIT_FRAC)
15780Sstevel@tonic-gate 
15790Sstevel@tonic-gate static void kphysm_del_cleanup(struct mem_handle *);
15800Sstevel@tonic-gate 
15810Sstevel@tonic-gate static void page_delete_collect(page_t *, struct mem_handle *);
15820Sstevel@tonic-gate 
15830Sstevel@tonic-gate static pgcnt_t
15840Sstevel@tonic-gate delthr_get_freemem(struct mem_handle *mhp)
15850Sstevel@tonic-gate {
15860Sstevel@tonic-gate 	pgcnt_t free_get;
15870Sstevel@tonic-gate 	int ret;
15880Sstevel@tonic-gate 
15890Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&mhp->mh_mutex));
15900Sstevel@tonic-gate 
15910Sstevel@tonic-gate 	MDSTAT_INCR(mhp, need_free);
15920Sstevel@tonic-gate 	/*
15930Sstevel@tonic-gate 	 * Get up to freemem_incr pages.
15940Sstevel@tonic-gate 	 */
15950Sstevel@tonic-gate 	free_get = freemem_incr;
15960Sstevel@tonic-gate 	if (free_get > mhp->mh_hold_todo)
15970Sstevel@tonic-gate 		free_get = mhp->mh_hold_todo;
15980Sstevel@tonic-gate 	/*
15990Sstevel@tonic-gate 	 * Take free_get pages away from freemem,
16000Sstevel@tonic-gate 	 * waiting if necessary.
16010Sstevel@tonic-gate 	 */
16020Sstevel@tonic-gate 
16030Sstevel@tonic-gate 	while (!mhp->mh_cancel) {
16040Sstevel@tonic-gate 		mutex_exit(&mhp->mh_mutex);
16050Sstevel@tonic-gate 		MDSTAT_INCR(mhp, free_loop);
16060Sstevel@tonic-gate 		/*
16070Sstevel@tonic-gate 		 * Duplicate test from page_create_throttle()
16080Sstevel@tonic-gate 		 * but don't override with !PG_WAIT.
16090Sstevel@tonic-gate 		 */
16100Sstevel@tonic-gate 		if (freemem < (free_get + throttlefree)) {
16110Sstevel@tonic-gate 			MDSTAT_INCR(mhp, free_low);
16120Sstevel@tonic-gate 			ret = 0;
16130Sstevel@tonic-gate 		} else {
16140Sstevel@tonic-gate 			ret = page_create_wait(free_get, 0);
16150Sstevel@tonic-gate 			if (ret == 0) {
16160Sstevel@tonic-gate 				/* EMPTY */
16170Sstevel@tonic-gate 				MDSTAT_INCR(mhp, free_failed);
16180Sstevel@tonic-gate 			}
16190Sstevel@tonic-gate 		}
16200Sstevel@tonic-gate 		if (ret != 0) {
16210Sstevel@tonic-gate 			mutex_enter(&mhp->mh_mutex);
16220Sstevel@tonic-gate 			return (free_get);
16230Sstevel@tonic-gate 		}
16240Sstevel@tonic-gate 
16250Sstevel@tonic-gate 		/*
16260Sstevel@tonic-gate 		 * Put pressure on pageout.
16270Sstevel@tonic-gate 		 */
16280Sstevel@tonic-gate 		page_needfree(free_get);
16290Sstevel@tonic-gate 		cv_signal(&proc_pageout->p_cv);
16300Sstevel@tonic-gate 
16310Sstevel@tonic-gate 		mutex_enter(&mhp->mh_mutex);
1632*11066Srafael.vanoni@sun.com 		(void) cv_reltimedwait(&mhp->mh_cv, &mhp->mh_mutex,
1633*11066Srafael.vanoni@sun.com 		    DEL_FREE_WAIT_TICKS, TR_CLOCK_TICK);
16340Sstevel@tonic-gate 		mutex_exit(&mhp->mh_mutex);
16350Sstevel@tonic-gate 		page_needfree(-(spgcnt_t)free_get);
16360Sstevel@tonic-gate 
16370Sstevel@tonic-gate 		mutex_enter(&mhp->mh_mutex);
16380Sstevel@tonic-gate 	}
16390Sstevel@tonic-gate 	return (0);
16400Sstevel@tonic-gate }
16410Sstevel@tonic-gate 
16420Sstevel@tonic-gate #define	DR_AIO_CLEANUP_DELAY	25000	/* 0.025secs, in usec */
16430Sstevel@tonic-gate #define	DR_AIO_CLEANUP_MAXLOOPS_NODELAY	100
16440Sstevel@tonic-gate /*
16450Sstevel@tonic-gate  * This function is run as a helper thread for delete_memory_thread.
16460Sstevel@tonic-gate  * It is needed in order to force kaio cleanup, so that pages used in kaio
16470Sstevel@tonic-gate  * will be unlocked and subsequently relocated by delete_memory_thread.
16480Sstevel@tonic-gate  * The address of the delete_memory_threads's mem_handle is passed in to
16490Sstevel@tonic-gate  * this thread function, and is used to set the mh_aio_cleanup_done member
16500Sstevel@tonic-gate  * prior to calling thread_exit().
16510Sstevel@tonic-gate  */
16520Sstevel@tonic-gate static void
16530Sstevel@tonic-gate dr_aio_cleanup_thread(caddr_t amhp)
16540Sstevel@tonic-gate {
16550Sstevel@tonic-gate 	proc_t *procp;
16560Sstevel@tonic-gate 	int (*aio_cleanup_dr_delete_memory)(proc_t *);
16570Sstevel@tonic-gate 	int cleaned;
16580Sstevel@tonic-gate 	int n = 0;
16590Sstevel@tonic-gate 	struct mem_handle *mhp;
16600Sstevel@tonic-gate 	volatile uint_t *pcancel;
16610Sstevel@tonic-gate 
16620Sstevel@tonic-gate 	mhp = (struct mem_handle *)amhp;
16630Sstevel@tonic-gate 	ASSERT(mhp != NULL);
16640Sstevel@tonic-gate 	pcancel = &mhp->mh_dr_aio_cleanup_cancel;
16650Sstevel@tonic-gate 	if (modload("sys", "kaio") == -1) {
16660Sstevel@tonic-gate 		mhp->mh_aio_cleanup_done = 1;
16670Sstevel@tonic-gate 		cmn_err(CE_WARN, "dr_aio_cleanup_thread: cannot load kaio");
16680Sstevel@tonic-gate 		thread_exit();
16690Sstevel@tonic-gate 	}
16700Sstevel@tonic-gate 	aio_cleanup_dr_delete_memory = (int (*)(proc_t *))
16710Sstevel@tonic-gate 	    modgetsymvalue("aio_cleanup_dr_delete_memory", 0);
16720Sstevel@tonic-gate 	if (aio_cleanup_dr_delete_memory == NULL) {
16730Sstevel@tonic-gate 		mhp->mh_aio_cleanup_done = 1;
16740Sstevel@tonic-gate 		cmn_err(CE_WARN,
16750Sstevel@tonic-gate 	    "aio_cleanup_dr_delete_memory not found in kaio");
16760Sstevel@tonic-gate 		thread_exit();
16770Sstevel@tonic-gate 	}
16780Sstevel@tonic-gate 	do {
16790Sstevel@tonic-gate 		cleaned = 0;
16800Sstevel@tonic-gate 		mutex_enter(&pidlock);
16810Sstevel@tonic-gate 		for (procp = practive; (*pcancel == 0) && (procp != NULL);
16820Sstevel@tonic-gate 		    procp = procp->p_next) {
16830Sstevel@tonic-gate 			mutex_enter(&procp->p_lock);
16840Sstevel@tonic-gate 			if (procp->p_aio != NULL) {
16850Sstevel@tonic-gate 				/* cleanup proc's outstanding kaio */
16860Sstevel@tonic-gate 				cleaned +=
16870Sstevel@tonic-gate 				    (*aio_cleanup_dr_delete_memory)(procp);
16880Sstevel@tonic-gate 			}
16890Sstevel@tonic-gate 			mutex_exit(&procp->p_lock);
16900Sstevel@tonic-gate 		}
16910Sstevel@tonic-gate 		mutex_exit(&pidlock);
16920Sstevel@tonic-gate 		if ((*pcancel == 0) &&
16930Sstevel@tonic-gate 		    (!cleaned || (++n == DR_AIO_CLEANUP_MAXLOOPS_NODELAY))) {
16940Sstevel@tonic-gate 			/* delay a bit before retrying all procs again */
16950Sstevel@tonic-gate 			delay(drv_usectohz(DR_AIO_CLEANUP_DELAY));
16960Sstevel@tonic-gate 			n = 0;
16970Sstevel@tonic-gate 		}
16980Sstevel@tonic-gate 	} while (*pcancel == 0);
16990Sstevel@tonic-gate 	mhp->mh_aio_cleanup_done = 1;
17000Sstevel@tonic-gate 	thread_exit();
17010Sstevel@tonic-gate }
17020Sstevel@tonic-gate 
17030Sstevel@tonic-gate static void
17040Sstevel@tonic-gate delete_memory_thread(caddr_t amhp)
17050Sstevel@tonic-gate {
17060Sstevel@tonic-gate 	struct mem_handle *mhp;
17070Sstevel@tonic-gate 	struct memdelspan *mdsp;
17080Sstevel@tonic-gate 	callb_cpr_t cprinfo;
17090Sstevel@tonic-gate 	page_t *pp_targ;
17100Sstevel@tonic-gate 	spgcnt_t freemem_left;
17110Sstevel@tonic-gate 	void (*del_complete_funcp)(void *, int error);
17120Sstevel@tonic-gate 	void *del_complete_arg;
17130Sstevel@tonic-gate 	int comp_code;
17140Sstevel@tonic-gate 	int ret;
17150Sstevel@tonic-gate 	int first_scan;
17160Sstevel@tonic-gate 	uint_t szc;
17170Sstevel@tonic-gate #ifdef MEM_DEL_STATS
17180Sstevel@tonic-gate 	uint64_t start_total, ntick_total;
17190Sstevel@tonic-gate 	uint64_t start_pgrp, ntick_pgrp;
17200Sstevel@tonic-gate #endif /* MEM_DEL_STATS */
17210Sstevel@tonic-gate 
17220Sstevel@tonic-gate 	mhp = (struct mem_handle *)amhp;
17230Sstevel@tonic-gate 
17240Sstevel@tonic-gate #ifdef MEM_DEL_STATS
17250Sstevel@tonic-gate 	start_total = ddi_get_lbolt();
17260Sstevel@tonic-gate #endif /* MEM_DEL_STATS */
17270Sstevel@tonic-gate 
17280Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &mhp->mh_mutex,
17290Sstevel@tonic-gate 	    callb_generic_cpr, "memdel");
17300Sstevel@tonic-gate 
17310Sstevel@tonic-gate 	mutex_enter(&mhp->mh_mutex);
17320Sstevel@tonic-gate 	ASSERT(mhp->mh_state == MHND_STARTING);
17330Sstevel@tonic-gate 
17340Sstevel@tonic-gate 	mhp->mh_state = MHND_RUNNING;
17350Sstevel@tonic-gate 	mhp->mh_thread_id = curthread;
17360Sstevel@tonic-gate 
17370Sstevel@tonic-gate 	mhp->mh_hold_todo = mhp->mh_vm_pages;
17380Sstevel@tonic-gate 	mutex_exit(&mhp->mh_mutex);
17390Sstevel@tonic-gate 
17400Sstevel@tonic-gate 	/* Allocate the remap pages now, if necessary. */
17410Sstevel@tonic-gate 	memseg_remap_init();
17420Sstevel@tonic-gate 
17430Sstevel@tonic-gate 	/*
17440Sstevel@tonic-gate 	 * Subtract from availrmem now if possible as availrmem
17450Sstevel@tonic-gate 	 * may not be available by the end of the delete.
17460Sstevel@tonic-gate 	 */
17470Sstevel@tonic-gate 	if (!get_availrmem(mhp->mh_vm_pages)) {
17480Sstevel@tonic-gate 		comp_code = KPHYSM_ENOTVIABLE;
17490Sstevel@tonic-gate 		mutex_enter(&mhp->mh_mutex);
17500Sstevel@tonic-gate 		goto early_exit;
17510Sstevel@tonic-gate 	}
17520Sstevel@tonic-gate 
17530Sstevel@tonic-gate 	ret = kphysm_setup_pre_del(mhp->mh_vm_pages);
17540Sstevel@tonic-gate 
17550Sstevel@tonic-gate 	mutex_enter(&mhp->mh_mutex);
17560Sstevel@tonic-gate 
17570Sstevel@tonic-gate 	if (ret != 0) {
17580Sstevel@tonic-gate 		mhp->mh_cancel = KPHYSM_EREFUSED;
17590Sstevel@tonic-gate 		goto refused;
17600Sstevel@tonic-gate 	}
17610Sstevel@tonic-gate 
17620Sstevel@tonic-gate 	transit_list_collect(mhp, 1);
17630Sstevel@tonic-gate 
17640Sstevel@tonic-gate 	for (mdsp = mhp->mh_transit.trl_spans; mdsp != NULL;
17650Sstevel@tonic-gate 	    mdsp = mdsp->mds_next) {
17660Sstevel@tonic-gate 		ASSERT(mdsp->mds_bitmap == NULL);
17670Sstevel@tonic-gate 		mdsp->mds_bitmap = kmem_zalloc(MDS_BITMAPBYTES(mdsp), KM_SLEEP);
17680Sstevel@tonic-gate 		mdsp->mds_bitmap_retired = kmem_zalloc(MDS_BITMAPBYTES(mdsp),
17696242Smb158278 		    KM_SLEEP);
17700Sstevel@tonic-gate 	}
17710Sstevel@tonic-gate 
17720Sstevel@tonic-gate 	first_scan = 1;
17730Sstevel@tonic-gate 	freemem_left = 0;
17740Sstevel@tonic-gate 	/*
17750Sstevel@tonic-gate 	 * Start dr_aio_cleanup_thread, which periodically iterates
17760Sstevel@tonic-gate 	 * through the process list and invokes aio cleanup.  This
17770Sstevel@tonic-gate 	 * is needed in order to avoid a deadly embrace between the
17780Sstevel@tonic-gate 	 * delete_memory_thread (waiting on writer lock for page, with the
17790Sstevel@tonic-gate 	 * exclusive-wanted bit set), kaio read request threads (waiting for a
17800Sstevel@tonic-gate 	 * reader lock on the same page that is wanted by the
17810Sstevel@tonic-gate 	 * delete_memory_thread), and threads waiting for kaio completion
17820Sstevel@tonic-gate 	 * (blocked on spt_amp->lock).
17830Sstevel@tonic-gate 	 */
17840Sstevel@tonic-gate 	mhp->mh_dr_aio_cleanup_cancel = 0;
17850Sstevel@tonic-gate 	mhp->mh_aio_cleanup_done = 0;
17860Sstevel@tonic-gate 	(void) thread_create(NULL, 0, dr_aio_cleanup_thread,
17870Sstevel@tonic-gate 	    (caddr_t)mhp, 0, &p0, TS_RUN, maxclsyspri - 1);
17880Sstevel@tonic-gate 	while ((mhp->mh_hold_todo != 0) && (mhp->mh_cancel == 0)) {
17890Sstevel@tonic-gate 		pgcnt_t collected;
17900Sstevel@tonic-gate 
17910Sstevel@tonic-gate 		MDSTAT_INCR(mhp, nloop);
17920Sstevel@tonic-gate 		collected = 0;
17930Sstevel@tonic-gate 		for (mdsp = mhp->mh_transit.trl_spans; (mdsp != NULL) &&
17940Sstevel@tonic-gate 		    (mhp->mh_cancel == 0); mdsp = mdsp->mds_next) {
17950Sstevel@tonic-gate 			pfn_t pfn, p_end;
17960Sstevel@tonic-gate 
17970Sstevel@tonic-gate 			p_end = mdsp->mds_base + mdsp->mds_npgs;
17980Sstevel@tonic-gate 			for (pfn = mdsp->mds_base; (pfn < p_end) &&
17990Sstevel@tonic-gate 			    (mhp->mh_cancel == 0); pfn++) {
18000Sstevel@tonic-gate 				page_t *pp, *tpp, *tpp_targ;
18010Sstevel@tonic-gate 				pgcnt_t bit;
18020Sstevel@tonic-gate 				struct vnode *vp;
18030Sstevel@tonic-gate 				u_offset_t offset;
18040Sstevel@tonic-gate 				int mod, result;
18050Sstevel@tonic-gate 				spgcnt_t pgcnt;
18060Sstevel@tonic-gate 
18070Sstevel@tonic-gate 				bit = pfn - mdsp->mds_base;
18080Sstevel@tonic-gate 				if ((mdsp->mds_bitmap[bit / NBPBMW] &
18090Sstevel@tonic-gate 				    (1 << (bit % NBPBMW))) != 0) {
18100Sstevel@tonic-gate 					MDSTAT_INCR(mhp, already_done);
18110Sstevel@tonic-gate 					continue;
18120Sstevel@tonic-gate 				}
18130Sstevel@tonic-gate 				if (freemem_left == 0) {
18140Sstevel@tonic-gate 					freemem_left += delthr_get_freemem(mhp);
18150Sstevel@tonic-gate 					if (freemem_left == 0)
18160Sstevel@tonic-gate 						break;
18170Sstevel@tonic-gate 				}
18180Sstevel@tonic-gate 
18190Sstevel@tonic-gate 				/*
18200Sstevel@tonic-gate 				 * Release mh_mutex - some of this
18210Sstevel@tonic-gate 				 * stuff takes some time (eg PUTPAGE).
18220Sstevel@tonic-gate 				 */
18230Sstevel@tonic-gate 
18240Sstevel@tonic-gate 				mutex_exit(&mhp->mh_mutex);
18250Sstevel@tonic-gate 				MDSTAT_INCR(mhp, ncheck);
18260Sstevel@tonic-gate 
18270Sstevel@tonic-gate 				pp = page_numtopp_nolock(pfn);
18280Sstevel@tonic-gate 				if (pp == NULL) {
18290Sstevel@tonic-gate 					/*
18300Sstevel@tonic-gate 					 * Not covered by a page_t - will
18310Sstevel@tonic-gate 					 * be dealt with elsewhere.
18320Sstevel@tonic-gate 					 */
18330Sstevel@tonic-gate 					MDSTAT_INCR(mhp, nopaget);
18340Sstevel@tonic-gate 					mutex_enter(&mhp->mh_mutex);
18350Sstevel@tonic-gate 					mdsp->mds_bitmap[bit / NBPBMW] |=
18360Sstevel@tonic-gate 					    (1 << (bit % NBPBMW));
18370Sstevel@tonic-gate 					continue;
18380Sstevel@tonic-gate 				}
18390Sstevel@tonic-gate 
18400Sstevel@tonic-gate 				if (!page_try_reclaim_lock(pp, SE_EXCL,
1841917Selowe 				    SE_EXCL_WANTED | SE_RETIRED)) {
1842917Selowe 					/*
1843917Selowe 					 * Page in use elsewhere.  Skip it.
1844917Selowe 					 */
1845917Selowe 					MDSTAT_INCR(mhp, lockfail);
1846917Selowe 					mutex_enter(&mhp->mh_mutex);
1847917Selowe 					continue;
18480Sstevel@tonic-gate 				}
18490Sstevel@tonic-gate 				/*
18500Sstevel@tonic-gate 				 * See if the cage expanded into the delete.
18510Sstevel@tonic-gate 				 * This can happen as we have to allow the
18520Sstevel@tonic-gate 				 * cage to expand.
18530Sstevel@tonic-gate 				 */
18540Sstevel@tonic-gate 				if (PP_ISNORELOC(pp)) {
1855917Selowe 					page_unlock(pp);
18560Sstevel@tonic-gate 					mutex_enter(&mhp->mh_mutex);
18570Sstevel@tonic-gate 					mhp->mh_cancel = KPHYSM_ENONRELOC;
18580Sstevel@tonic-gate 					break;
18590Sstevel@tonic-gate 				}
1860917Selowe 				if (PP_RETIRED(pp)) {
18610Sstevel@tonic-gate 					/*
18620Sstevel@tonic-gate 					 * Page has been retired and is
18630Sstevel@tonic-gate 					 * not part of the cage so we
18640Sstevel@tonic-gate 					 * can now do the accounting for
18650Sstevel@tonic-gate 					 * it.
18660Sstevel@tonic-gate 					 */
18670Sstevel@tonic-gate 					MDSTAT_INCR(mhp, retired);
18680Sstevel@tonic-gate 					mutex_enter(&mhp->mh_mutex);
18690Sstevel@tonic-gate 					mdsp->mds_bitmap[bit / NBPBMW]
18700Sstevel@tonic-gate 					    |= (1 << (bit % NBPBMW));
18710Sstevel@tonic-gate 					mdsp->mds_bitmap_retired[bit /
18720Sstevel@tonic-gate 					    NBPBMW] |=
18730Sstevel@tonic-gate 					    (1 << (bit % NBPBMW));
18740Sstevel@tonic-gate 					mhp->mh_hold_todo--;
18750Sstevel@tonic-gate 					continue;
18760Sstevel@tonic-gate 				}
18770Sstevel@tonic-gate 				ASSERT(freemem_left != 0);
18780Sstevel@tonic-gate 				if (PP_ISFREE(pp)) {
18790Sstevel@tonic-gate 					/*
18800Sstevel@tonic-gate 					 * Like page_reclaim() only 'freemem'
18810Sstevel@tonic-gate 					 * processing is already done.
18820Sstevel@tonic-gate 					 */
18830Sstevel@tonic-gate 					MDSTAT_INCR(mhp, nfree);
18840Sstevel@tonic-gate 				free_page_collect:
18850Sstevel@tonic-gate 					if (PP_ISAGED(pp)) {
18860Sstevel@tonic-gate 						page_list_sub(pp,
18870Sstevel@tonic-gate 						    PG_FREE_LIST);
18880Sstevel@tonic-gate 					} else {
18890Sstevel@tonic-gate 						page_list_sub(pp,
18900Sstevel@tonic-gate 						    PG_CACHE_LIST);
18910Sstevel@tonic-gate 					}
18920Sstevel@tonic-gate 					PP_CLRFREE(pp);
18930Sstevel@tonic-gate 					PP_CLRAGED(pp);
18940Sstevel@tonic-gate 					collected++;
18950Sstevel@tonic-gate 					mutex_enter(&mhp->mh_mutex);
18960Sstevel@tonic-gate 					page_delete_collect(pp, mhp);
18970Sstevel@tonic-gate 					mdsp->mds_bitmap[bit / NBPBMW] |=
18980Sstevel@tonic-gate 					    (1 << (bit % NBPBMW));
18990Sstevel@tonic-gate 					freemem_left--;
19000Sstevel@tonic-gate 					continue;
19010Sstevel@tonic-gate 				}
19020Sstevel@tonic-gate 				ASSERT(pp->p_vnode != NULL);
19030Sstevel@tonic-gate 				if (first_scan) {
19040Sstevel@tonic-gate 					MDSTAT_INCR(mhp, first_notfree);
19050Sstevel@tonic-gate 					page_unlock(pp);
19060Sstevel@tonic-gate 					mutex_enter(&mhp->mh_mutex);
19070Sstevel@tonic-gate 					continue;
19080Sstevel@tonic-gate 				}
19090Sstevel@tonic-gate 				/*
19100Sstevel@tonic-gate 				 * Keep stats on pages encountered that
1911917Selowe 				 * are marked for retirement.
19120Sstevel@tonic-gate 				 */
1913917Selowe 				if (PP_TOXIC(pp)) {
19140Sstevel@tonic-gate 					MDSTAT_INCR(mhp, toxic);
1915917Selowe 				} else if (PP_PR_REQ(pp)) {
19160Sstevel@tonic-gate 					MDSTAT_INCR(mhp, failing);
19170Sstevel@tonic-gate 				}
19180Sstevel@tonic-gate 				/*
19190Sstevel@tonic-gate 				 * In certain cases below, special exceptions
19200Sstevel@tonic-gate 				 * are made for pages that are toxic.  This
19210Sstevel@tonic-gate 				 * is because the current meaning of toxic
19220Sstevel@tonic-gate 				 * is that an uncorrectable error has been
19230Sstevel@tonic-gate 				 * previously associated with the page.
19240Sstevel@tonic-gate 				 */
19250Sstevel@tonic-gate 				if (pp->p_lckcnt != 0 || pp->p_cowcnt != 0) {
1926917Selowe 					if (!PP_TOXIC(pp)) {
19270Sstevel@tonic-gate 						/*
19280Sstevel@tonic-gate 						 * Must relocate locked in
19290Sstevel@tonic-gate 						 * memory pages.
19300Sstevel@tonic-gate 						 */
19310Sstevel@tonic-gate #ifdef MEM_DEL_STATS
19320Sstevel@tonic-gate 						start_pgrp = ddi_get_lbolt();
19330Sstevel@tonic-gate #endif /* MEM_DEL_STATS */
19340Sstevel@tonic-gate 						/*
19350Sstevel@tonic-gate 						 * Lock all constituent pages
19360Sstevel@tonic-gate 						 * of a large page to ensure
19370Sstevel@tonic-gate 						 * that p_szc won't change.
19380Sstevel@tonic-gate 						 */
19390Sstevel@tonic-gate 						if (!group_page_trylock(pp,
19400Sstevel@tonic-gate 						    SE_EXCL)) {
19410Sstevel@tonic-gate 							MDSTAT_INCR(mhp,
19420Sstevel@tonic-gate 							    gptllckfail);
19430Sstevel@tonic-gate 							page_unlock(pp);
19440Sstevel@tonic-gate 							mutex_enter(
19450Sstevel@tonic-gate 							    &mhp->mh_mutex);
19460Sstevel@tonic-gate 							continue;
19470Sstevel@tonic-gate 						}
19480Sstevel@tonic-gate 						MDSTAT_INCR(mhp, npplocked);
19490Sstevel@tonic-gate 						pp_targ =
19500Sstevel@tonic-gate 						    page_get_replacement_page(
19516242Smb158278 						    pp, NULL, 0);
19520Sstevel@tonic-gate 						if (pp_targ != NULL) {
19530Sstevel@tonic-gate #ifdef MEM_DEL_STATS
19540Sstevel@tonic-gate 							ntick_pgrp =
19550Sstevel@tonic-gate 							    (uint64_t)
19560Sstevel@tonic-gate 							    ddi_get_lbolt() -
19570Sstevel@tonic-gate 							    start_pgrp;
19580Sstevel@tonic-gate #endif /* MEM_DEL_STATS */
19590Sstevel@tonic-gate 							MDSTAT_PGRP(mhp,
19600Sstevel@tonic-gate 							    ntick_pgrp);
19610Sstevel@tonic-gate 							MDSTAT_INCR(mhp,
19620Sstevel@tonic-gate 							    nlockreloc);
19630Sstevel@tonic-gate 							goto reloc;
19640Sstevel@tonic-gate 						}
19650Sstevel@tonic-gate 						group_page_unlock(pp);
19660Sstevel@tonic-gate 						page_unlock(pp);
19670Sstevel@tonic-gate #ifdef MEM_DEL_STATS
19680Sstevel@tonic-gate 						ntick_pgrp =
19690Sstevel@tonic-gate 						    (uint64_t)ddi_get_lbolt() -
19700Sstevel@tonic-gate 						    start_pgrp;
19710Sstevel@tonic-gate #endif /* MEM_DEL_STATS */
19720Sstevel@tonic-gate 						MDSTAT_PGRP(mhp, ntick_pgrp);
19730Sstevel@tonic-gate 						MDSTAT_INCR(mhp, nnorepl);
19740Sstevel@tonic-gate 						mutex_enter(&mhp->mh_mutex);
19750Sstevel@tonic-gate 						continue;
19760Sstevel@tonic-gate 					} else {
19770Sstevel@tonic-gate 						/*
19780Sstevel@tonic-gate 						 * Cannot do anything about
19790Sstevel@tonic-gate 						 * this page because it is
19800Sstevel@tonic-gate 						 * toxic.
19810Sstevel@tonic-gate 						 */
19820Sstevel@tonic-gate 						MDSTAT_INCR(mhp, npplkdtoxic);
19830Sstevel@tonic-gate 						page_unlock(pp);
19840Sstevel@tonic-gate 						mutex_enter(&mhp->mh_mutex);
19850Sstevel@tonic-gate 						continue;
19860Sstevel@tonic-gate 					}
19870Sstevel@tonic-gate 				}
19880Sstevel@tonic-gate 				/*
19890Sstevel@tonic-gate 				 * Unload the mappings and check if mod bit
19900Sstevel@tonic-gate 				 * is set.
19910Sstevel@tonic-gate 				 */
19923290Sjohansen 				ASSERT(!PP_ISKAS(pp));
19930Sstevel@tonic-gate 				(void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD);
19940Sstevel@tonic-gate 				mod = hat_ismod(pp);
19950Sstevel@tonic-gate 
19960Sstevel@tonic-gate #ifdef MEM_DEL_STATS
19970Sstevel@tonic-gate 				start_pgrp = ddi_get_lbolt();
19980Sstevel@tonic-gate #endif /* MEM_DEL_STATS */
1999917Selowe 				if (mod && !PP_TOXIC(pp)) {
20000Sstevel@tonic-gate 					/*
20010Sstevel@tonic-gate 					 * Lock all constituent pages
20020Sstevel@tonic-gate 					 * of a large page to ensure
20030Sstevel@tonic-gate 					 * that p_szc won't change.
20040Sstevel@tonic-gate 					 */
20050Sstevel@tonic-gate 					if (!group_page_trylock(pp, SE_EXCL)) {
20060Sstevel@tonic-gate 						MDSTAT_INCR(mhp, gptlmodfail);
20070Sstevel@tonic-gate 						page_unlock(pp);
20080Sstevel@tonic-gate 						mutex_enter(&mhp->mh_mutex);
20090Sstevel@tonic-gate 						continue;
20100Sstevel@tonic-gate 					}
20110Sstevel@tonic-gate 					pp_targ = page_get_replacement_page(pp,
20120Sstevel@tonic-gate 					    NULL, 0);
20130Sstevel@tonic-gate 					if (pp_targ != NULL) {
20140Sstevel@tonic-gate 						MDSTAT_INCR(mhp, nmodreloc);
20150Sstevel@tonic-gate #ifdef MEM_DEL_STATS
20160Sstevel@tonic-gate 						ntick_pgrp =
20170Sstevel@tonic-gate 						    (uint64_t)ddi_get_lbolt() -
20186242Smb158278 						    start_pgrp;
20190Sstevel@tonic-gate #endif /* MEM_DEL_STATS */
20200Sstevel@tonic-gate 						MDSTAT_PGRP(mhp, ntick_pgrp);
20210Sstevel@tonic-gate 						goto reloc;
20220Sstevel@tonic-gate 					}
20230Sstevel@tonic-gate 					group_page_unlock(pp);
20240Sstevel@tonic-gate 				}
20250Sstevel@tonic-gate 
20260Sstevel@tonic-gate 				if (!page_try_demote_pages(pp)) {
20270Sstevel@tonic-gate 					MDSTAT_INCR(mhp, demotefail);
20280Sstevel@tonic-gate 					page_unlock(pp);
20290Sstevel@tonic-gate #ifdef MEM_DEL_STATS
20300Sstevel@tonic-gate 					ntick_pgrp = (uint64_t)ddi_get_lbolt() -
20310Sstevel@tonic-gate 					    start_pgrp;
20320Sstevel@tonic-gate #endif /* MEM_DEL_STATS */
20330Sstevel@tonic-gate 					MDSTAT_PGRP(mhp, ntick_pgrp);
20340Sstevel@tonic-gate 					mutex_enter(&mhp->mh_mutex);
20350Sstevel@tonic-gate 					continue;
20360Sstevel@tonic-gate 				}
20370Sstevel@tonic-gate 
20380Sstevel@tonic-gate 				/*
20390Sstevel@tonic-gate 				 * Regular 'page-out'.
20400Sstevel@tonic-gate 				 */
20410Sstevel@tonic-gate 				if (!mod) {
20420Sstevel@tonic-gate 					MDSTAT_INCR(mhp, ndestroy);
20430Sstevel@tonic-gate 					page_destroy(pp, 1);
20440Sstevel@tonic-gate 					/*
20450Sstevel@tonic-gate 					 * page_destroy was called with
20460Sstevel@tonic-gate 					 * dontfree. As long as p_lckcnt
20470Sstevel@tonic-gate 					 * and p_cowcnt are both zero, the
20480Sstevel@tonic-gate 					 * only additional action of
20490Sstevel@tonic-gate 					 * page_destroy with !dontfree is to
20500Sstevel@tonic-gate 					 * call page_free, so we can collect
20510Sstevel@tonic-gate 					 * the page here.
20520Sstevel@tonic-gate 					 */
20530Sstevel@tonic-gate 					collected++;
20540Sstevel@tonic-gate #ifdef MEM_DEL_STATS
20550Sstevel@tonic-gate 					ntick_pgrp = (uint64_t)ddi_get_lbolt() -
20560Sstevel@tonic-gate 					    start_pgrp;
20570Sstevel@tonic-gate #endif /* MEM_DEL_STATS */
20580Sstevel@tonic-gate 					MDSTAT_PGRP(mhp, ntick_pgrp);
20590Sstevel@tonic-gate 					mutex_enter(&mhp->mh_mutex);
20600Sstevel@tonic-gate 					page_delete_collect(pp, mhp);
20610Sstevel@tonic-gate 					mdsp->mds_bitmap[bit / NBPBMW] |=
20620Sstevel@tonic-gate 					    (1 << (bit % NBPBMW));
20630Sstevel@tonic-gate 					continue;
20640Sstevel@tonic-gate 				}
20650Sstevel@tonic-gate 				/*
20660Sstevel@tonic-gate 				 * The page is toxic and the mod bit is
20670Sstevel@tonic-gate 				 * set, we cannot do anything here to deal
20680Sstevel@tonic-gate 				 * with it.
20690Sstevel@tonic-gate 				 */
2070917Selowe 				if (PP_TOXIC(pp)) {
20710Sstevel@tonic-gate 					page_unlock(pp);
20720Sstevel@tonic-gate #ifdef MEM_DEL_STATS
20730Sstevel@tonic-gate 					ntick_pgrp = (uint64_t)ddi_get_lbolt() -
20740Sstevel@tonic-gate 					    start_pgrp;
20750Sstevel@tonic-gate #endif /* MEM_DEL_STATS */
20760Sstevel@tonic-gate 					MDSTAT_PGRP(mhp, ntick_pgrp);
20770Sstevel@tonic-gate 					MDSTAT_INCR(mhp, modtoxic);
20780Sstevel@tonic-gate 					mutex_enter(&mhp->mh_mutex);
20790Sstevel@tonic-gate 					continue;
20800Sstevel@tonic-gate 				}
20810Sstevel@tonic-gate 				MDSTAT_INCR(mhp, nputpage);
20820Sstevel@tonic-gate 				vp = pp->p_vnode;
20830Sstevel@tonic-gate 				offset = pp->p_offset;
20840Sstevel@tonic-gate 				VN_HOLD(vp);
20850Sstevel@tonic-gate 				page_unlock(pp);
20860Sstevel@tonic-gate 				(void) VOP_PUTPAGE(vp, offset, PAGESIZE,
20875331Samw 				    B_INVAL|B_FORCE, kcred, NULL);
20880Sstevel@tonic-gate 				VN_RELE(vp);
20890Sstevel@tonic-gate #ifdef MEM_DEL_STATS
20900Sstevel@tonic-gate 				ntick_pgrp = (uint64_t)ddi_get_lbolt() -
20910Sstevel@tonic-gate 				    start_pgrp;
20920Sstevel@tonic-gate #endif /* MEM_DEL_STATS */
20930Sstevel@tonic-gate 				MDSTAT_PGRP(mhp, ntick_pgrp);
20940Sstevel@tonic-gate 				/*
20950Sstevel@tonic-gate 				 * Try to get the page back immediately
20960Sstevel@tonic-gate 				 * so that it can be collected.
20970Sstevel@tonic-gate 				 */
20980Sstevel@tonic-gate 				pp = page_numtopp_nolock(pfn);
20990Sstevel@tonic-gate 				if (pp == NULL) {
21000Sstevel@tonic-gate 					MDSTAT_INCR(mhp, nnoreclaim);
21010Sstevel@tonic-gate 					/*
21020Sstevel@tonic-gate 					 * This should not happen as this
21030Sstevel@tonic-gate 					 * thread is deleting the page.
21040Sstevel@tonic-gate 					 * If this code is generalized, this
21050Sstevel@tonic-gate 					 * becomes a reality.
21060Sstevel@tonic-gate 					 */
21070Sstevel@tonic-gate #ifdef DEBUG
21080Sstevel@tonic-gate 					cmn_err(CE_WARN,
21090Sstevel@tonic-gate 					    "delete_memory_thread(0x%p) "
21100Sstevel@tonic-gate 					    "pfn 0x%lx has no page_t",
21110Sstevel@tonic-gate 					    (void *)mhp, pfn);
21120Sstevel@tonic-gate #endif /* DEBUG */
21130Sstevel@tonic-gate 					mutex_enter(&mhp->mh_mutex);
21140Sstevel@tonic-gate 					continue;
21150Sstevel@tonic-gate 				}
21160Sstevel@tonic-gate 				if (page_try_reclaim_lock(pp, SE_EXCL,
2117917Selowe 				    SE_EXCL_WANTED | SE_RETIRED)) {
21180Sstevel@tonic-gate 					if (PP_ISFREE(pp)) {
21190Sstevel@tonic-gate 						goto free_page_collect;
21200Sstevel@tonic-gate 					}
21210Sstevel@tonic-gate 					page_unlock(pp);
21220Sstevel@tonic-gate 				}
21230Sstevel@tonic-gate 				MDSTAT_INCR(mhp, nnoreclaim);
21240Sstevel@tonic-gate 				mutex_enter(&mhp->mh_mutex);
21250Sstevel@tonic-gate 				continue;
21260Sstevel@tonic-gate 
21270Sstevel@tonic-gate 			reloc:
21280Sstevel@tonic-gate 				/*
21290Sstevel@tonic-gate 				 * Got some freemem and a target
21300Sstevel@tonic-gate 				 * page, so move the data to avoid
21310Sstevel@tonic-gate 				 * I/O and lock problems.
21320Sstevel@tonic-gate 				 */
21330Sstevel@tonic-gate 				ASSERT(!page_iolock_assert(pp));
21340Sstevel@tonic-gate 				MDSTAT_INCR(mhp, nreloc);
21350Sstevel@tonic-gate 				/*
21360Sstevel@tonic-gate 				 * page_relocate() will return pgcnt: the
21370Sstevel@tonic-gate 				 * number of consecutive pages relocated.
21380Sstevel@tonic-gate 				 * If it is successful, pp will be a
21390Sstevel@tonic-gate 				 * linked list of the page structs that
21400Sstevel@tonic-gate 				 * were relocated. If page_relocate() is
21410Sstevel@tonic-gate 				 * unsuccessful, pp will be unmodified.
21420Sstevel@tonic-gate 				 */
21430Sstevel@tonic-gate #ifdef MEM_DEL_STATS
21440Sstevel@tonic-gate 				start_pgrp = ddi_get_lbolt();
21450Sstevel@tonic-gate #endif /* MEM_DEL_STATS */
21460Sstevel@tonic-gate 				result = page_relocate(&pp, &pp_targ, 0, 0,
21470Sstevel@tonic-gate 				    &pgcnt, NULL);
21480Sstevel@tonic-gate #ifdef MEM_DEL_STATS
21490Sstevel@tonic-gate 				ntick_pgrp = (uint64_t)ddi_get_lbolt() -
21500Sstevel@tonic-gate 				    start_pgrp;
21510Sstevel@tonic-gate #endif /* MEM_DEL_STATS */
21520Sstevel@tonic-gate 				MDSTAT_PGRP(mhp, ntick_pgrp);
21530Sstevel@tonic-gate 				if (result != 0) {
21540Sstevel@tonic-gate 					MDSTAT_INCR(mhp, nrelocfail);
21550Sstevel@tonic-gate 					/*
21560Sstevel@tonic-gate 					 * We did not succeed. We need
21570Sstevel@tonic-gate 					 * to give the pp_targ pages back.
21580Sstevel@tonic-gate 					 * page_free(pp_targ, 1) without
21590Sstevel@tonic-gate 					 * the freemem accounting.
21600Sstevel@tonic-gate 					 */
21610Sstevel@tonic-gate 					group_page_unlock(pp);
21620Sstevel@tonic-gate 					page_free_replacement_page(pp_targ);
21630Sstevel@tonic-gate 					page_unlock(pp);
21640Sstevel@tonic-gate 					mutex_enter(&mhp->mh_mutex);
21650Sstevel@tonic-gate 					continue;
21660Sstevel@tonic-gate 				}
21670Sstevel@tonic-gate 
21680Sstevel@tonic-gate 				/*
21690Sstevel@tonic-gate 				 * We will then collect pgcnt pages.
21700Sstevel@tonic-gate 				 */
21710Sstevel@tonic-gate 				ASSERT(pgcnt > 0);
21720Sstevel@tonic-gate 				mutex_enter(&mhp->mh_mutex);
21730Sstevel@tonic-gate 				/*
21740Sstevel@tonic-gate 				 * We need to make sure freemem_left is
21750Sstevel@tonic-gate 				 * large enough.
21760Sstevel@tonic-gate 				 */
21770Sstevel@tonic-gate 				while ((freemem_left < pgcnt) &&
21786242Smb158278 				    (!mhp->mh_cancel)) {
21790Sstevel@tonic-gate 					freemem_left +=
21806242Smb158278 					    delthr_get_freemem(mhp);
21810Sstevel@tonic-gate 				}
21820Sstevel@tonic-gate 
21830Sstevel@tonic-gate 				/*
21840Sstevel@tonic-gate 				 * Do not proceed if mh_cancel is set.
21850Sstevel@tonic-gate 				 */
21860Sstevel@tonic-gate 				if (mhp->mh_cancel) {
21870Sstevel@tonic-gate 					while (pp_targ != NULL) {
21880Sstevel@tonic-gate 						/*
21890Sstevel@tonic-gate 						 * Unlink and unlock each page.
21900Sstevel@tonic-gate 						 */
21910Sstevel@tonic-gate 						tpp_targ = pp_targ;
21920Sstevel@tonic-gate 						page_sub(&pp_targ, tpp_targ);
21930Sstevel@tonic-gate 						page_unlock(tpp_targ);
21940Sstevel@tonic-gate 					}
21950Sstevel@tonic-gate 					/*
21960Sstevel@tonic-gate 					 * We need to give the pp pages back.
21970Sstevel@tonic-gate 					 * page_free(pp, 1) without the
21980Sstevel@tonic-gate 					 * freemem accounting.
21990Sstevel@tonic-gate 					 */
22000Sstevel@tonic-gate 					page_free_replacement_page(pp);
22010Sstevel@tonic-gate 					break;
22020Sstevel@tonic-gate 				}
22030Sstevel@tonic-gate 
22040Sstevel@tonic-gate 				/* Now remove pgcnt from freemem_left */
22050Sstevel@tonic-gate 				freemem_left -= pgcnt;
22060Sstevel@tonic-gate 				ASSERT(freemem_left >= 0);
22070Sstevel@tonic-gate 				szc = pp->p_szc;
22080Sstevel@tonic-gate 				while (pp != NULL) {
22090Sstevel@tonic-gate 					/*
22100Sstevel@tonic-gate 					 * pp and pp_targ were passed back as
22110Sstevel@tonic-gate 					 * a linked list of pages.
22120Sstevel@tonic-gate 					 * Unlink and unlock each page.
22130Sstevel@tonic-gate 					 */
22140Sstevel@tonic-gate 					tpp_targ = pp_targ;
22150Sstevel@tonic-gate 					page_sub(&pp_targ, tpp_targ);
22160Sstevel@tonic-gate 					page_unlock(tpp_targ);
22170Sstevel@tonic-gate 					/*
22180Sstevel@tonic-gate 					 * The original page is now free
22190Sstevel@tonic-gate 					 * so remove it from the linked
22200Sstevel@tonic-gate 					 * list and collect it.
22210Sstevel@tonic-gate 					 */
22220Sstevel@tonic-gate 					tpp = pp;
22230Sstevel@tonic-gate 					page_sub(&pp, tpp);
22240Sstevel@tonic-gate 					pfn = page_pptonum(tpp);
22250Sstevel@tonic-gate 					collected++;
22260Sstevel@tonic-gate 					ASSERT(PAGE_EXCL(tpp));
22270Sstevel@tonic-gate 					ASSERT(tpp->p_vnode == NULL);
22280Sstevel@tonic-gate 					ASSERT(!hat_page_is_mapped(tpp));
22290Sstevel@tonic-gate 					ASSERT(tpp->p_szc == szc);
22300Sstevel@tonic-gate 					tpp->p_szc = 0;
22310Sstevel@tonic-gate 					page_delete_collect(tpp, mhp);
22320Sstevel@tonic-gate 					bit = pfn - mdsp->mds_base;
22330Sstevel@tonic-gate 					mdsp->mds_bitmap[bit / NBPBMW] |=
22346242Smb158278 					    (1 << (bit % NBPBMW));
22350Sstevel@tonic-gate 				}
22360Sstevel@tonic-gate 				ASSERT(pp_targ == NULL);
22370Sstevel@tonic-gate 			}
22380Sstevel@tonic-gate 		}
22390Sstevel@tonic-gate 		first_scan = 0;
22400Sstevel@tonic-gate 		if ((mhp->mh_cancel == 0) && (mhp->mh_hold_todo != 0) &&
22416242Smb158278 		    (collected == 0)) {
22420Sstevel@tonic-gate 			/*
22430Sstevel@tonic-gate 			 * This code is needed as we cannot wait
22440Sstevel@tonic-gate 			 * for a page to be locked OR the delete to
22450Sstevel@tonic-gate 			 * be cancelled.  Also, we must delay so
22460Sstevel@tonic-gate 			 * that other threads get a chance to run
22470Sstevel@tonic-gate 			 * on our cpu, otherwise page locks may be
22480Sstevel@tonic-gate 			 * held indefinitely by those threads.
22490Sstevel@tonic-gate 			 */
22500Sstevel@tonic-gate 			MDSTAT_INCR(mhp, ndelay);
22510Sstevel@tonic-gate 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
2252*11066Srafael.vanoni@sun.com 			(void) cv_reltimedwait(&mhp->mh_cv, &mhp->mh_mutex,
2253*11066Srafael.vanoni@sun.com 			    DEL_BUSY_WAIT_TICKS, TR_CLOCK_TICK);
22540Sstevel@tonic-gate 			CALLB_CPR_SAFE_END(&cprinfo, &mhp->mh_mutex);
22550Sstevel@tonic-gate 		}
22560Sstevel@tonic-gate 	}
22570Sstevel@tonic-gate 	/* stop the dr aio cleanup thread */
22580Sstevel@tonic-gate 	mhp->mh_dr_aio_cleanup_cancel = 1;
22590Sstevel@tonic-gate 	transit_list_collect(mhp, 0);
22600Sstevel@tonic-gate 	if (freemem_left != 0) {
22610Sstevel@tonic-gate 		/* Return any surplus. */
22620Sstevel@tonic-gate 		page_create_putback(freemem_left);
22630Sstevel@tonic-gate 		freemem_left = 0;
22640Sstevel@tonic-gate 	}
22650Sstevel@tonic-gate #ifdef MEM_DEL_STATS
22660Sstevel@tonic-gate 	ntick_total = (uint64_t)ddi_get_lbolt() - start_total;
22670Sstevel@tonic-gate #endif /* MEM_DEL_STATS */
22680Sstevel@tonic-gate 	MDSTAT_TOTAL(mhp, ntick_total);
22690Sstevel@tonic-gate 	MDSTAT_PRINT(mhp);
22700Sstevel@tonic-gate 
22710Sstevel@tonic-gate 	/*
22720Sstevel@tonic-gate 	 * If the memory delete was cancelled, exclusive-wanted bits must
2273917Selowe 	 * be cleared. If there are retired pages being deleted, they need
2274917Selowe 	 * to be unretired.
22750Sstevel@tonic-gate 	 */
22760Sstevel@tonic-gate 	for (mdsp = mhp->mh_transit.trl_spans; mdsp != NULL;
22770Sstevel@tonic-gate 	    mdsp = mdsp->mds_next) {
22780Sstevel@tonic-gate 		pfn_t pfn, p_end;
22790Sstevel@tonic-gate 
22800Sstevel@tonic-gate 		p_end = mdsp->mds_base + mdsp->mds_npgs;
22810Sstevel@tonic-gate 		for (pfn = mdsp->mds_base; pfn < p_end; pfn++) {
22820Sstevel@tonic-gate 			page_t *pp;
22830Sstevel@tonic-gate 			pgcnt_t bit;
22840Sstevel@tonic-gate 
22850Sstevel@tonic-gate 			bit = pfn - mdsp->mds_base;
22860Sstevel@tonic-gate 			if (mhp->mh_cancel) {
22870Sstevel@tonic-gate 				pp = page_numtopp_nolock(pfn);
22880Sstevel@tonic-gate 				if (pp != NULL) {
22890Sstevel@tonic-gate 					if ((mdsp->mds_bitmap[bit / NBPBMW] &
22900Sstevel@tonic-gate 					    (1 << (bit % NBPBMW))) == 0) {
22910Sstevel@tonic-gate 						page_lock_clr_exclwanted(pp);
22920Sstevel@tonic-gate 					}
22930Sstevel@tonic-gate 				}
22940Sstevel@tonic-gate 			} else {
22950Sstevel@tonic-gate 				pp = NULL;
22960Sstevel@tonic-gate 			}
22970Sstevel@tonic-gate 			if ((mdsp->mds_bitmap_retired[bit / NBPBMW] &
22980Sstevel@tonic-gate 			    (1 << (bit % NBPBMW))) != 0) {
22990Sstevel@tonic-gate 				/* do we already have pp? */
23000Sstevel@tonic-gate 				if (pp == NULL) {
23010Sstevel@tonic-gate 					pp = page_numtopp_nolock(pfn);
23020Sstevel@tonic-gate 				}
23030Sstevel@tonic-gate 				ASSERT(pp != NULL);
2304917Selowe 				ASSERT(PP_RETIRED(pp));
23050Sstevel@tonic-gate 				if (mhp->mh_cancel != 0) {
2306917Selowe 					page_unlock(pp);
23070Sstevel@tonic-gate 					/*
23080Sstevel@tonic-gate 					 * To satisfy ASSERT below in
23090Sstevel@tonic-gate 					 * cancel code.
23100Sstevel@tonic-gate 					 */
23110Sstevel@tonic-gate 					mhp->mh_hold_todo++;
23120Sstevel@tonic-gate 				} else {
23133253Smec 					(void) page_unretire_pp(pp,
23143253Smec 					    PR_UNR_CLEAN);
23150Sstevel@tonic-gate 				}
23160Sstevel@tonic-gate 			}
23170Sstevel@tonic-gate 		}
23180Sstevel@tonic-gate 	}
23190Sstevel@tonic-gate 	/*
23200Sstevel@tonic-gate 	 * Free retired page bitmap and collected page bitmap
23210Sstevel@tonic-gate 	 */
23220Sstevel@tonic-gate 	for (mdsp = mhp->mh_transit.trl_spans; mdsp != NULL;
23230Sstevel@tonic-gate 	    mdsp = mdsp->mds_next) {
23240Sstevel@tonic-gate 		ASSERT(mdsp->mds_bitmap_retired != NULL);
23250Sstevel@tonic-gate 		kmem_free(mdsp->mds_bitmap_retired, MDS_BITMAPBYTES(mdsp));
23260Sstevel@tonic-gate 		mdsp->mds_bitmap_retired = NULL;	/* Paranoia. */
23270Sstevel@tonic-gate 		ASSERT(mdsp->mds_bitmap != NULL);
23280Sstevel@tonic-gate 		kmem_free(mdsp->mds_bitmap, MDS_BITMAPBYTES(mdsp));
23290Sstevel@tonic-gate 		mdsp->mds_bitmap = NULL;	/* Paranoia. */
23300Sstevel@tonic-gate 	}
23310Sstevel@tonic-gate 
23320Sstevel@tonic-gate 	/* wait for our dr aio cancel thread to exit */
23330Sstevel@tonic-gate 	while (!(mhp->mh_aio_cleanup_done)) {
23340Sstevel@tonic-gate 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
23350Sstevel@tonic-gate 		delay(drv_usectohz(DR_AIO_CLEANUP_DELAY));
23360Sstevel@tonic-gate 		CALLB_CPR_SAFE_END(&cprinfo, &mhp->mh_mutex);
23370Sstevel@tonic-gate 	}
23380Sstevel@tonic-gate refused:
23390Sstevel@tonic-gate 	if (mhp->mh_cancel != 0) {
23400Sstevel@tonic-gate 		page_t *pp;
23410Sstevel@tonic-gate 
23420Sstevel@tonic-gate 		comp_code = mhp->mh_cancel;
23430Sstevel@tonic-gate 		/*
23440Sstevel@tonic-gate 		 * Go through list of deleted pages (mh_deleted) freeing
23450Sstevel@tonic-gate 		 * them.
23460Sstevel@tonic-gate 		 */
23470Sstevel@tonic-gate 		while ((pp = mhp->mh_deleted) != NULL) {
23480Sstevel@tonic-gate 			mhp->mh_deleted = pp->p_next;
23490Sstevel@tonic-gate 			mhp->mh_hold_todo++;
23500Sstevel@tonic-gate 			mutex_exit(&mhp->mh_mutex);
23510Sstevel@tonic-gate 			/* Restore p_next. */
23520Sstevel@tonic-gate 			pp->p_next = pp->p_prev;
23530Sstevel@tonic-gate 			if (PP_ISFREE(pp)) {
23540Sstevel@tonic-gate 				cmn_err(CE_PANIC,
23550Sstevel@tonic-gate 				    "page %p is free",
23560Sstevel@tonic-gate 				    (void *)pp);
23570Sstevel@tonic-gate 			}
23580Sstevel@tonic-gate 			page_free(pp, 1);
23590Sstevel@tonic-gate 			mutex_enter(&mhp->mh_mutex);
23600Sstevel@tonic-gate 		}
23610Sstevel@tonic-gate 		ASSERT(mhp->mh_hold_todo == mhp->mh_vm_pages);
23620Sstevel@tonic-gate 
23630Sstevel@tonic-gate 		mutex_exit(&mhp->mh_mutex);
23640Sstevel@tonic-gate 		put_availrmem(mhp->mh_vm_pages);
23650Sstevel@tonic-gate 		mutex_enter(&mhp->mh_mutex);
23660Sstevel@tonic-gate 
23670Sstevel@tonic-gate 		goto t_exit;
23680Sstevel@tonic-gate 	}
23690Sstevel@tonic-gate 
23700Sstevel@tonic-gate 	/*
23710Sstevel@tonic-gate 	 * All the pages are no longer in use and are exclusively locked.
23720Sstevel@tonic-gate 	 */
23730Sstevel@tonic-gate 
23740Sstevel@tonic-gate 	mhp->mh_deleted = NULL;
23750Sstevel@tonic-gate 
23760Sstevel@tonic-gate 	kphysm_del_cleanup(mhp);
23770Sstevel@tonic-gate 
23786242Smb158278 	/*
237910106SJason.Beloro@Sun.COM 	 * mem_node_del_range needs to be after kphysm_del_cleanup so
23806242Smb158278 	 * that the mem_node_config[] will remain intact for the cleanup.
23816242Smb158278 	 */
23826242Smb158278 	for (mdsp = mhp->mh_transit.trl_spans; mdsp != NULL;
23836242Smb158278 	    mdsp = mdsp->mds_next) {
238410106SJason.Beloro@Sun.COM 		mem_node_del_range(mdsp->mds_base,
238510106SJason.Beloro@Sun.COM 		    mdsp->mds_base + mdsp->mds_npgs - 1);
23866242Smb158278 	}
23876242Smb158278 
23880Sstevel@tonic-gate 	comp_code = KPHYSM_OK;
23890Sstevel@tonic-gate 
23900Sstevel@tonic-gate t_exit:
23910Sstevel@tonic-gate 	mutex_exit(&mhp->mh_mutex);
23920Sstevel@tonic-gate 	kphysm_setup_post_del(mhp->mh_vm_pages,
23930Sstevel@tonic-gate 	    (comp_code == KPHYSM_OK) ? 0 : 1);
23940Sstevel@tonic-gate 	mutex_enter(&mhp->mh_mutex);
23950Sstevel@tonic-gate 
23960Sstevel@tonic-gate early_exit:
23970Sstevel@tonic-gate 	/* mhp->mh_mutex exited by CALLB_CPR_EXIT() */
23980Sstevel@tonic-gate 	mhp->mh_state = MHND_DONE;
23990Sstevel@tonic-gate 	del_complete_funcp = mhp->mh_delete_complete;
24000Sstevel@tonic-gate 	del_complete_arg = mhp->mh_delete_complete_arg;
24010Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cprinfo);
24020Sstevel@tonic-gate 	(*del_complete_funcp)(del_complete_arg, comp_code);
24030Sstevel@tonic-gate 	thread_exit();
24040Sstevel@tonic-gate 	/*NOTREACHED*/
24050Sstevel@tonic-gate }
24060Sstevel@tonic-gate 
24070Sstevel@tonic-gate /*
24080Sstevel@tonic-gate  * Start the delete of the memory from the system.
24090Sstevel@tonic-gate  */
24100Sstevel@tonic-gate int
24110Sstevel@tonic-gate kphysm_del_start(
24120Sstevel@tonic-gate 	memhandle_t handle,
24130Sstevel@tonic-gate 	void (*complete)(void *, int),
24140Sstevel@tonic-gate 	void *complete_arg)
24150Sstevel@tonic-gate {
24160Sstevel@tonic-gate 	struct mem_handle *mhp;
24170Sstevel@tonic-gate 
24180Sstevel@tonic-gate 	mhp = kphysm_lookup_mem_handle(handle);
24190Sstevel@tonic-gate 	if (mhp == NULL) {
24200Sstevel@tonic-gate 		return (KPHYSM_EHANDLE);
24210Sstevel@tonic-gate 	}
24220Sstevel@tonic-gate 	switch (mhp->mh_state) {
24230Sstevel@tonic-gate 	case MHND_FREE:
24240Sstevel@tonic-gate 		ASSERT(mhp->mh_state != MHND_FREE);
24250Sstevel@tonic-gate 		mutex_exit(&mhp->mh_mutex);
24260Sstevel@tonic-gate 		return (KPHYSM_EHANDLE);
24270Sstevel@tonic-gate 	case MHND_INIT:
24280Sstevel@tonic-gate 		break;
24290Sstevel@tonic-gate 	case MHND_STARTING:
24300Sstevel@tonic-gate 	case MHND_RUNNING:
24310Sstevel@tonic-gate 		mutex_exit(&mhp->mh_mutex);
24320Sstevel@tonic-gate 		return (KPHYSM_ESEQUENCE);
24330Sstevel@tonic-gate 	case MHND_DONE:
24340Sstevel@tonic-gate 		mutex_exit(&mhp->mh_mutex);
24350Sstevel@tonic-gate 		return (KPHYSM_ESEQUENCE);
24360Sstevel@tonic-gate 	case MHND_RELEASE:
24370Sstevel@tonic-gate 		mutex_exit(&mhp->mh_mutex);
24380Sstevel@tonic-gate 		return (KPHYSM_ESEQUENCE);
24390Sstevel@tonic-gate 	default:
24400Sstevel@tonic-gate #ifdef DEBUG
24410Sstevel@tonic-gate 		cmn_err(CE_WARN, "kphysm_del_start(0x%p) state corrupt %d",
24420Sstevel@tonic-gate 		    (void *)mhp, mhp->mh_state);
24430Sstevel@tonic-gate #endif /* DEBUG */
24440Sstevel@tonic-gate 		mutex_exit(&mhp->mh_mutex);
24450Sstevel@tonic-gate 		return (KPHYSM_EHANDLE);
24460Sstevel@tonic-gate 	}
24470Sstevel@tonic-gate 
24480Sstevel@tonic-gate 	if (mhp->mh_transit.trl_spans == NULL) {
24490Sstevel@tonic-gate 		mutex_exit(&mhp->mh_mutex);
24500Sstevel@tonic-gate 		return (KPHYSM_ENOWORK);
24510Sstevel@tonic-gate 	}
24520Sstevel@tonic-gate 
24530Sstevel@tonic-gate 	ASSERT(complete != NULL);
24540Sstevel@tonic-gate 	mhp->mh_delete_complete = complete;
24550Sstevel@tonic-gate 	mhp->mh_delete_complete_arg = complete_arg;
24560Sstevel@tonic-gate 	mhp->mh_state = MHND_STARTING;
24570Sstevel@tonic-gate 	/*
24580Sstevel@tonic-gate 	 * Release the mutex in case thread_create sleeps.
24590Sstevel@tonic-gate 	 */
24600Sstevel@tonic-gate 	mutex_exit(&mhp->mh_mutex);
24610Sstevel@tonic-gate 
24620Sstevel@tonic-gate 	/*
24630Sstevel@tonic-gate 	 * The "obvious" process for this thread is pageout (proc_pageout)
24640Sstevel@tonic-gate 	 * but this gives the thread too much power over freemem
24650Sstevel@tonic-gate 	 * which results in freemem starvation.
24660Sstevel@tonic-gate 	 */
24670Sstevel@tonic-gate 	(void) thread_create(NULL, 0, delete_memory_thread, mhp, 0, &p0,
24680Sstevel@tonic-gate 	    TS_RUN, maxclsyspri - 1);
24690Sstevel@tonic-gate 
24700Sstevel@tonic-gate 	return (KPHYSM_OK);
24710Sstevel@tonic-gate }
24720Sstevel@tonic-gate 
24730Sstevel@tonic-gate static kmutex_t pp_dummy_lock;		/* Protects init. of pp_dummy. */
24740Sstevel@tonic-gate static caddr_t pp_dummy;
24750Sstevel@tonic-gate static pgcnt_t pp_dummy_npages;
24760Sstevel@tonic-gate static pfn_t *pp_dummy_pfn;	/* Array of dummy pfns. */
24770Sstevel@tonic-gate 
24780Sstevel@tonic-gate static void
24790Sstevel@tonic-gate memseg_remap_init_pages(page_t *pages, page_t *epages)
24800Sstevel@tonic-gate {
24810Sstevel@tonic-gate 	page_t *pp;
24820Sstevel@tonic-gate 
24830Sstevel@tonic-gate 	for (pp = pages; pp < epages; pp++) {
24840Sstevel@tonic-gate 		pp->p_pagenum = PFN_INVALID;	/* XXXX */
24850Sstevel@tonic-gate 		pp->p_offset = (u_offset_t)-1;
24860Sstevel@tonic-gate 		page_iolock_init(pp);
24870Sstevel@tonic-gate 		while (!page_lock(pp, SE_EXCL, (kmutex_t *)NULL, P_RECLAIM))
24880Sstevel@tonic-gate 			continue;
24890Sstevel@tonic-gate 		page_lock_delete(pp);
24900Sstevel@tonic-gate 	}
24910Sstevel@tonic-gate }
24920Sstevel@tonic-gate 
24930Sstevel@tonic-gate void
24940Sstevel@tonic-gate memseg_remap_init()
24950Sstevel@tonic-gate {
24960Sstevel@tonic-gate 	mutex_enter(&pp_dummy_lock);
24970Sstevel@tonic-gate 	if (pp_dummy == NULL) {
24980Sstevel@tonic-gate 		uint_t dpages;
24990Sstevel@tonic-gate 		int i;
25000Sstevel@tonic-gate 
25010Sstevel@tonic-gate 		/*
25020Sstevel@tonic-gate 		 * dpages starts off as the size of the structure and
25030Sstevel@tonic-gate 		 * ends up as the minimum number of pages that will
25040Sstevel@tonic-gate 		 * hold a whole number of page_t structures.
25050Sstevel@tonic-gate 		 */
25060Sstevel@tonic-gate 		dpages = sizeof (page_t);
25070Sstevel@tonic-gate 		ASSERT(dpages != 0);
25080Sstevel@tonic-gate 		ASSERT(dpages <= MMU_PAGESIZE);
25090Sstevel@tonic-gate 
25100Sstevel@tonic-gate 		while ((dpages & 1) == 0)
25110Sstevel@tonic-gate 			dpages >>= 1;
25120Sstevel@tonic-gate 
25130Sstevel@tonic-gate 		pp_dummy_npages = dpages;
25140Sstevel@tonic-gate 		/*
25150Sstevel@tonic-gate 		 * Allocate pp_dummy pages directly from static_arena,
25160Sstevel@tonic-gate 		 * since these are whole page allocations and are
25170Sstevel@tonic-gate 		 * referenced by physical address.  This also has the
25180Sstevel@tonic-gate 		 * nice fringe benefit of hiding the memory from
25190Sstevel@tonic-gate 		 * ::findleaks since it doesn't deal well with allocated
25200Sstevel@tonic-gate 		 * kernel heap memory that doesn't have any mappings.
25210Sstevel@tonic-gate 		 */
25220Sstevel@tonic-gate 		pp_dummy = vmem_xalloc(static_arena, ptob(pp_dummy_npages),
25230Sstevel@tonic-gate 		    PAGESIZE, 0, 0, NULL, NULL, VM_SLEEP);
25240Sstevel@tonic-gate 		bzero(pp_dummy, ptob(pp_dummy_npages));
25250Sstevel@tonic-gate 		ASSERT(((uintptr_t)pp_dummy & MMU_PAGEOFFSET) == 0);
25260Sstevel@tonic-gate 		pp_dummy_pfn = kmem_alloc(sizeof (*pp_dummy_pfn) *
25270Sstevel@tonic-gate 		    pp_dummy_npages, KM_SLEEP);
25280Sstevel@tonic-gate 		for (i = 0; i < pp_dummy_npages; i++) {
25290Sstevel@tonic-gate 			pp_dummy_pfn[i] = hat_getpfnum(kas.a_hat,
25300Sstevel@tonic-gate 			    &pp_dummy[MMU_PAGESIZE * i]);
25310Sstevel@tonic-gate 			ASSERT(pp_dummy_pfn[i] != PFN_INVALID);
25320Sstevel@tonic-gate 		}
25330Sstevel@tonic-gate 		/*
25340Sstevel@tonic-gate 		 * Initialize the page_t's to a known 'deleted' state
25350Sstevel@tonic-gate 		 * that matches the state of deleted pages.
25360Sstevel@tonic-gate 		 */
25370Sstevel@tonic-gate 		memseg_remap_init_pages((page_t *)pp_dummy,
25386242Smb158278 		    (page_t *)(pp_dummy + ptob(pp_dummy_npages)));
25390Sstevel@tonic-gate 		/* Remove kmem mappings for the pages for safety. */
25400Sstevel@tonic-gate 		hat_unload(kas.a_hat, pp_dummy, ptob(pp_dummy_npages),
25410Sstevel@tonic-gate 		    HAT_UNLOAD_UNLOCK);
25420Sstevel@tonic-gate 		/* Leave pp_dummy pointer set as flag that init is done. */
25430Sstevel@tonic-gate 	}
25440Sstevel@tonic-gate 	mutex_exit(&pp_dummy_lock);
25450Sstevel@tonic-gate }
25460Sstevel@tonic-gate 
254710106SJason.Beloro@Sun.COM /*
254810106SJason.Beloro@Sun.COM  * Remap a page-aglined range of page_t's to dummy pages.
254910106SJason.Beloro@Sun.COM  */
255010106SJason.Beloro@Sun.COM void
255110106SJason.Beloro@Sun.COM remap_to_dummy(caddr_t va, pgcnt_t metapgs)
25520Sstevel@tonic-gate {
255310106SJason.Beloro@Sun.COM 	int phase;
255410106SJason.Beloro@Sun.COM 
255510106SJason.Beloro@Sun.COM 	ASSERT(IS_P2ALIGNED((uint64_t)va, PAGESIZE));
255610106SJason.Beloro@Sun.COM 
255710106SJason.Beloro@Sun.COM 	/*
255810106SJason.Beloro@Sun.COM 	 * We may start remapping at a non-zero page offset
255910106SJason.Beloro@Sun.COM 	 * within the dummy pages since the low/high ends
256010106SJason.Beloro@Sun.COM 	 * of the outgoing pp's could be shared by other
256110106SJason.Beloro@Sun.COM 	 * memsegs (see memseg_remap_meta).
256210106SJason.Beloro@Sun.COM 	 */
256310106SJason.Beloro@Sun.COM 	phase = btop((uint64_t)va) % pp_dummy_npages;
256410106SJason.Beloro@Sun.COM 	ASSERT(PAGESIZE % sizeof (page_t) || phase == 0);
25650Sstevel@tonic-gate 
25660Sstevel@tonic-gate 	while (metapgs != 0) {
25670Sstevel@tonic-gate 		pgcnt_t n;
256810106SJason.Beloro@Sun.COM 		int i, j;
25690Sstevel@tonic-gate 
25700Sstevel@tonic-gate 		n = pp_dummy_npages;
25710Sstevel@tonic-gate 		if (n > metapgs)
25720Sstevel@tonic-gate 			n = metapgs;
25730Sstevel@tonic-gate 		for (i = 0; i < n; i++) {
257410106SJason.Beloro@Sun.COM 			j = (i + phase) % pp_dummy_npages;
257510106SJason.Beloro@Sun.COM 			hat_devload(kas.a_hat, va, ptob(1), pp_dummy_pfn[j],
25760Sstevel@tonic-gate 			    PROT_READ,
25770Sstevel@tonic-gate 			    HAT_LOAD | HAT_LOAD_NOCONSIST |
25780Sstevel@tonic-gate 			    HAT_LOAD_REMAP);
257910106SJason.Beloro@Sun.COM 			va += ptob(1);
25800Sstevel@tonic-gate 		}
25810Sstevel@tonic-gate 		metapgs -= n;
25820Sstevel@tonic-gate 	}
25830Sstevel@tonic-gate }
25840Sstevel@tonic-gate 
258510106SJason.Beloro@Sun.COM static void
258610106SJason.Beloro@Sun.COM memseg_remap_to_dummy(struct memseg *seg)
258710106SJason.Beloro@Sun.COM {
258810106SJason.Beloro@Sun.COM 	caddr_t pp;
258910106SJason.Beloro@Sun.COM 	pgcnt_t metapgs;
259010106SJason.Beloro@Sun.COM 
259110106SJason.Beloro@Sun.COM 	ASSERT(memseg_is_dynamic(seg));
259210106SJason.Beloro@Sun.COM 	ASSERT(pp_dummy != NULL);
259310106SJason.Beloro@Sun.COM 
259410106SJason.Beloro@Sun.COM 
259510106SJason.Beloro@Sun.COM 	if (!memseg_includes_meta(seg)) {
259610106SJason.Beloro@Sun.COM 		memseg_remap_meta(seg);
259710106SJason.Beloro@Sun.COM 		return;
259810106SJason.Beloro@Sun.COM 	}
259910106SJason.Beloro@Sun.COM 
260010106SJason.Beloro@Sun.COM 	pp = (caddr_t)seg->pages;
260110106SJason.Beloro@Sun.COM 	metapgs = seg->pages_base - memseg_get_start(seg);
260210106SJason.Beloro@Sun.COM 	ASSERT(metapgs != 0);
260310106SJason.Beloro@Sun.COM 
260410106SJason.Beloro@Sun.COM 	seg->pages_end = seg->pages_base;
260510106SJason.Beloro@Sun.COM 
260610106SJason.Beloro@Sun.COM 	remap_to_dummy(pp, metapgs);
260710106SJason.Beloro@Sun.COM }
260810106SJason.Beloro@Sun.COM 
26090Sstevel@tonic-gate /*
26100Sstevel@tonic-gate  * Transition all the deleted pages to the deleted state so that
26110Sstevel@tonic-gate  * page_lock will not wait. The page_lock_delete call will
26120Sstevel@tonic-gate  * also wake up any waiters.
26130Sstevel@tonic-gate  */
26140Sstevel@tonic-gate static void
26150Sstevel@tonic-gate memseg_lock_delete_all(struct memseg *seg)
26160Sstevel@tonic-gate {
26170Sstevel@tonic-gate 	page_t *pp;
26180Sstevel@tonic-gate 
26190Sstevel@tonic-gate 	for (pp = seg->pages; pp < seg->epages; pp++) {
26200Sstevel@tonic-gate 		pp->p_pagenum = PFN_INVALID;	/* XXXX */
26210Sstevel@tonic-gate 		page_lock_delete(pp);
26220Sstevel@tonic-gate 	}
26230Sstevel@tonic-gate }
26240Sstevel@tonic-gate 
26250Sstevel@tonic-gate static void
26260Sstevel@tonic-gate kphysm_del_cleanup(struct mem_handle *mhp)
26270Sstevel@tonic-gate {
26280Sstevel@tonic-gate 	struct memdelspan	*mdsp;
26290Sstevel@tonic-gate 	struct memseg		*seg;
26300Sstevel@tonic-gate 	struct memseg   	**segpp;
26310Sstevel@tonic-gate 	struct memseg		*seglist;
26320Sstevel@tonic-gate 	pfn_t			p_end;
26330Sstevel@tonic-gate 	uint64_t		avmem;
26340Sstevel@tonic-gate 	pgcnt_t			avpgs;
26350Sstevel@tonic-gate 	pgcnt_t			npgs;
26360Sstevel@tonic-gate 
26370Sstevel@tonic-gate 	avpgs = mhp->mh_vm_pages;
26380Sstevel@tonic-gate 
26390Sstevel@tonic-gate 	memsegs_lock(1);
26400Sstevel@tonic-gate 
26410Sstevel@tonic-gate 	/*
26420Sstevel@tonic-gate 	 * remove from main segment list.
26430Sstevel@tonic-gate 	 */
26440Sstevel@tonic-gate 	npgs = 0;
26450Sstevel@tonic-gate 	seglist = NULL;
26460Sstevel@tonic-gate 	for (mdsp = mhp->mh_transit.trl_spans; mdsp != NULL;
26470Sstevel@tonic-gate 	    mdsp = mdsp->mds_next) {
26480Sstevel@tonic-gate 		p_end = mdsp->mds_base + mdsp->mds_npgs;
26490Sstevel@tonic-gate 		for (segpp = &memsegs; (seg = *segpp) != NULL; ) {
26500Sstevel@tonic-gate 			if (seg->pages_base >= p_end ||
26510Sstevel@tonic-gate 			    seg->pages_end <= mdsp->mds_base) {
26520Sstevel@tonic-gate 				/* Span and memseg don't overlap. */
26530Sstevel@tonic-gate 				segpp = &((*segpp)->next);
26540Sstevel@tonic-gate 				continue;
26550Sstevel@tonic-gate 			}
26560Sstevel@tonic-gate 			ASSERT(seg->pages_base >= mdsp->mds_base);
26570Sstevel@tonic-gate 			ASSERT(seg->pages_end <= p_end);
26580Sstevel@tonic-gate 
26591373Skchow 			PLCNT_MODIFY_MAX(seg->pages_base,
26601373Skchow 			    seg->pages_base - seg->pages_end);
26611373Skchow 
26620Sstevel@tonic-gate 			/* Hide the memseg from future scans. */
26630Sstevel@tonic-gate 			hat_kpm_delmem_mseg_update(seg, segpp);
26640Sstevel@tonic-gate 			*segpp = seg->next;
26650Sstevel@tonic-gate 			membar_producer();	/* TODO: Needed? */
26660Sstevel@tonic-gate 			npgs += MSEG_NPAGES(seg);
26670Sstevel@tonic-gate 
26680Sstevel@tonic-gate 			/*
26690Sstevel@tonic-gate 			 * Leave the deleted segment's next pointer intact
26700Sstevel@tonic-gate 			 * in case a memsegs scanning loop is walking this
26710Sstevel@tonic-gate 			 * segment concurrently.
26720Sstevel@tonic-gate 			 */
26730Sstevel@tonic-gate 			seg->lnext = seglist;
26740Sstevel@tonic-gate 			seglist = seg;
26750Sstevel@tonic-gate 		}
26760Sstevel@tonic-gate 	}
26770Sstevel@tonic-gate 
26780Sstevel@tonic-gate 	build_pfn_hash();
26790Sstevel@tonic-gate 
26800Sstevel@tonic-gate 	ASSERT(npgs < total_pages);
26810Sstevel@tonic-gate 	total_pages -= npgs;
26820Sstevel@tonic-gate 
26830Sstevel@tonic-gate 	/*
26840Sstevel@tonic-gate 	 * Recalculate the paging parameters now total_pages has changed.
26850Sstevel@tonic-gate 	 * This will also cause the clock hands to be reset before next use.
26860Sstevel@tonic-gate 	 */
26870Sstevel@tonic-gate 	setupclock(1);
26880Sstevel@tonic-gate 
26890Sstevel@tonic-gate 	memsegs_unlock(1);
26900Sstevel@tonic-gate 
26910Sstevel@tonic-gate 	mutex_exit(&mhp->mh_mutex);
26920Sstevel@tonic-gate 
26930Sstevel@tonic-gate 	while ((seg = seglist) != NULL) {
26940Sstevel@tonic-gate 		pfn_t mseg_start;
26950Sstevel@tonic-gate 		pfn_t mseg_base, mseg_end;
26960Sstevel@tonic-gate 		pgcnt_t mseg_npgs;
26970Sstevel@tonic-gate 		int mlret;
26980Sstevel@tonic-gate 
26990Sstevel@tonic-gate 		seglist = seg->lnext;
27000Sstevel@tonic-gate 
27010Sstevel@tonic-gate 		/*
27020Sstevel@tonic-gate 		 * Put the page_t's into the deleted state to stop
27030Sstevel@tonic-gate 		 * cv_wait()s on the pages. When we remap, the dummy
27040Sstevel@tonic-gate 		 * page_t's will be in the same state.
27050Sstevel@tonic-gate 		 */
27060Sstevel@tonic-gate 		memseg_lock_delete_all(seg);
27070Sstevel@tonic-gate 		/*
27080Sstevel@tonic-gate 		 * Collect up information based on pages_base and pages_end
27090Sstevel@tonic-gate 		 * early so that we can flag early that the memseg has been
27100Sstevel@tonic-gate 		 * deleted by setting pages_end == pages_base.
27110Sstevel@tonic-gate 		 */
27120Sstevel@tonic-gate 		mseg_base = seg->pages_base;
27130Sstevel@tonic-gate 		mseg_end = seg->pages_end;
27140Sstevel@tonic-gate 		mseg_npgs = MSEG_NPAGES(seg);
271510106SJason.Beloro@Sun.COM 		mseg_start = memseg_get_start(seg);
271610106SJason.Beloro@Sun.COM 
271710106SJason.Beloro@Sun.COM 		if (memseg_is_dynamic(seg)) {
27180Sstevel@tonic-gate 			/* Remap the meta data to our special dummy area. */
271910106SJason.Beloro@Sun.COM 			memseg_remap_to_dummy(seg);
27200Sstevel@tonic-gate 
27210Sstevel@tonic-gate 			mutex_enter(&memseg_lists_lock);
27220Sstevel@tonic-gate 			seg->lnext = memseg_va_avail;
27230Sstevel@tonic-gate 			memseg_va_avail = seg;
27240Sstevel@tonic-gate 			mutex_exit(&memseg_lists_lock);
27250Sstevel@tonic-gate 		} else {
27260Sstevel@tonic-gate 			/*
27270Sstevel@tonic-gate 			 * For memory whose page_ts were allocated
27280Sstevel@tonic-gate 			 * at boot, we need to find a new use for
27290Sstevel@tonic-gate 			 * the page_t memory.
27300Sstevel@tonic-gate 			 * For the moment, just leak it.
27310Sstevel@tonic-gate 			 * (It is held in the memseg_delete_junk list.)
27320Sstevel@tonic-gate 			 */
273310106SJason.Beloro@Sun.COM 			seg->pages_end = seg->pages_base;
27340Sstevel@tonic-gate 
27350Sstevel@tonic-gate 			mutex_enter(&memseg_lists_lock);
27360Sstevel@tonic-gate 			seg->lnext = memseg_delete_junk;
27370Sstevel@tonic-gate 			memseg_delete_junk = seg;
27380Sstevel@tonic-gate 			mutex_exit(&memseg_lists_lock);
27390Sstevel@tonic-gate 		}
27400Sstevel@tonic-gate 
27410Sstevel@tonic-gate 		/* Must not use seg now as it could be re-used. */
27420Sstevel@tonic-gate 
27430Sstevel@tonic-gate 		memlist_write_lock();
27440Sstevel@tonic-gate 
27450Sstevel@tonic-gate 		mlret = memlist_delete_span(
27460Sstevel@tonic-gate 		    (uint64_t)(mseg_base) << PAGESHIFT,
27470Sstevel@tonic-gate 		    (uint64_t)(mseg_npgs) << PAGESHIFT,
27480Sstevel@tonic-gate 		    &phys_avail);
27490Sstevel@tonic-gate 		ASSERT(mlret == MEML_SPANOP_OK);
27500Sstevel@tonic-gate 
27510Sstevel@tonic-gate 		mlret = memlist_delete_span(
27520Sstevel@tonic-gate 		    (uint64_t)(mseg_start) << PAGESHIFT,
27530Sstevel@tonic-gate 		    (uint64_t)(mseg_end - mseg_start) <<
27540Sstevel@tonic-gate 		    PAGESHIFT,
27550Sstevel@tonic-gate 		    &phys_install);
27560Sstevel@tonic-gate 		ASSERT(mlret == MEML_SPANOP_OK);
27570Sstevel@tonic-gate 		phys_install_has_changed();
27580Sstevel@tonic-gate 
27590Sstevel@tonic-gate 		memlist_write_unlock();
27600Sstevel@tonic-gate 	}
27610Sstevel@tonic-gate 
27620Sstevel@tonic-gate 	memlist_read_lock();
27630Sstevel@tonic-gate 	installed_top_size(phys_install, &physmax, &physinstalled);
27640Sstevel@tonic-gate 	memlist_read_unlock();
27650Sstevel@tonic-gate 
27660Sstevel@tonic-gate 	mutex_enter(&freemem_lock);
27670Sstevel@tonic-gate 	maxmem -= avpgs;
27680Sstevel@tonic-gate 	physmem -= avpgs;
27690Sstevel@tonic-gate 	/* availrmem is adjusted during the delete. */
27700Sstevel@tonic-gate 	availrmem_initial -= avpgs;
27710Sstevel@tonic-gate 
27720Sstevel@tonic-gate 	mutex_exit(&freemem_lock);
27730Sstevel@tonic-gate 
27740Sstevel@tonic-gate 	dump_resize();
27750Sstevel@tonic-gate 
27760Sstevel@tonic-gate 	cmn_err(CE_CONT, "?kphysm_delete: mem = %ldK "
27770Sstevel@tonic-gate 	    "(0x%" PRIx64 ")\n",
27780Sstevel@tonic-gate 	    physinstalled << (PAGESHIFT - 10),
27790Sstevel@tonic-gate 	    (uint64_t)physinstalled << PAGESHIFT);
27800Sstevel@tonic-gate 
27810Sstevel@tonic-gate 	avmem = (uint64_t)freemem << PAGESHIFT;
27820Sstevel@tonic-gate 	cmn_err(CE_CONT, "?kphysm_delete: "
27830Sstevel@tonic-gate 	    "avail mem = %" PRId64 "\n", avmem);
27840Sstevel@tonic-gate 
27850Sstevel@tonic-gate 	/*
27860Sstevel@tonic-gate 	 * Update lgroup generation number on single lgroup systems
27870Sstevel@tonic-gate 	 */
27880Sstevel@tonic-gate 	if (nlgrps == 1)
27890Sstevel@tonic-gate 		lgrp_config(LGRP_CONFIG_GEN_UPDATE, 0, 0);
27900Sstevel@tonic-gate 
27910Sstevel@tonic-gate 	/* Successfully deleted system memory */
27920Sstevel@tonic-gate 	mutex_enter(&mhp->mh_mutex);
27930Sstevel@tonic-gate }
27940Sstevel@tonic-gate 
27950Sstevel@tonic-gate static uint_t mdel_nullvp_waiter;
27960Sstevel@tonic-gate 
27970Sstevel@tonic-gate static void
27980Sstevel@tonic-gate page_delete_collect(
27990Sstevel@tonic-gate 	page_t *pp,
28000Sstevel@tonic-gate 	struct mem_handle *mhp)
28010Sstevel@tonic-gate {
28020Sstevel@tonic-gate 	if (pp->p_vnode) {
28030Sstevel@tonic-gate 		page_hashout(pp, (kmutex_t *)NULL);
28040Sstevel@tonic-gate 		/* do not do PP_SETAGED(pp); */
28050Sstevel@tonic-gate 	} else {
28060Sstevel@tonic-gate 		kmutex_t *sep;
28070Sstevel@tonic-gate 
28080Sstevel@tonic-gate 		sep = page_se_mutex(pp);
28090Sstevel@tonic-gate 		mutex_enter(sep);
28100Sstevel@tonic-gate 		if (CV_HAS_WAITERS(&pp->p_cv)) {
28110Sstevel@tonic-gate 			mdel_nullvp_waiter++;
28120Sstevel@tonic-gate 			cv_broadcast(&pp->p_cv);
28130Sstevel@tonic-gate 		}
28140Sstevel@tonic-gate 		mutex_exit(sep);
28150Sstevel@tonic-gate 	}
28160Sstevel@tonic-gate 	ASSERT(pp->p_next == pp->p_prev);
28170Sstevel@tonic-gate 	ASSERT(pp->p_next == NULL || pp->p_next == pp);
28180Sstevel@tonic-gate 	pp->p_next = mhp->mh_deleted;
28190Sstevel@tonic-gate 	mhp->mh_deleted = pp;
28200Sstevel@tonic-gate 	ASSERT(mhp->mh_hold_todo != 0);
28210Sstevel@tonic-gate 	mhp->mh_hold_todo--;
28220Sstevel@tonic-gate }
28230Sstevel@tonic-gate 
28240Sstevel@tonic-gate static void
28250Sstevel@tonic-gate transit_list_collect(struct mem_handle *mhp, int v)
28260Sstevel@tonic-gate {
28270Sstevel@tonic-gate 	struct transit_list_head *trh;
28280Sstevel@tonic-gate 
28290Sstevel@tonic-gate 	trh = &transit_list_head;
28300Sstevel@tonic-gate 	mutex_enter(&trh->trh_lock);
28310Sstevel@tonic-gate 	mhp->mh_transit.trl_collect = v;
28320Sstevel@tonic-gate 	mutex_exit(&trh->trh_lock);
28330Sstevel@tonic-gate }
28340Sstevel@tonic-gate 
28350Sstevel@tonic-gate static void
28360Sstevel@tonic-gate transit_list_insert(struct transit_list *tlp)
28370Sstevel@tonic-gate {
28380Sstevel@tonic-gate 	struct transit_list_head *trh;
28390Sstevel@tonic-gate 
28400Sstevel@tonic-gate 	trh = &transit_list_head;
28410Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&trh->trh_lock));
28420Sstevel@tonic-gate 	tlp->trl_next = trh->trh_head;
28430Sstevel@tonic-gate 	trh->trh_head = tlp;
28440Sstevel@tonic-gate }
28450Sstevel@tonic-gate 
28460Sstevel@tonic-gate static void
28470Sstevel@tonic-gate transit_list_remove(struct transit_list *tlp)
28480Sstevel@tonic-gate {
28490Sstevel@tonic-gate 	struct transit_list_head *trh;
28500Sstevel@tonic-gate 	struct transit_list **tlpp;
28510Sstevel@tonic-gate 
28520Sstevel@tonic-gate 	trh = &transit_list_head;
28530Sstevel@tonic-gate 	tlpp = &trh->trh_head;
28540Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&trh->trh_lock));
28550Sstevel@tonic-gate 	while (*tlpp != NULL && *tlpp != tlp)
28560Sstevel@tonic-gate 		tlpp = &(*tlpp)->trl_next;
28570Sstevel@tonic-gate 	ASSERT(*tlpp != NULL);
28580Sstevel@tonic-gate 	if (*tlpp == tlp)
28590Sstevel@tonic-gate 		*tlpp = tlp->trl_next;
28600Sstevel@tonic-gate 	tlp->trl_next = NULL;
28610Sstevel@tonic-gate }
28620Sstevel@tonic-gate 
28630Sstevel@tonic-gate static struct transit_list *
28640Sstevel@tonic-gate pfnum_to_transit_list(struct transit_list_head *trh, pfn_t pfnum)
28650Sstevel@tonic-gate {
28660Sstevel@tonic-gate 	struct transit_list *tlp;
28670Sstevel@tonic-gate 
28680Sstevel@tonic-gate 	for (tlp = trh->trh_head; tlp != NULL; tlp = tlp->trl_next) {
28690Sstevel@tonic-gate 		struct memdelspan *mdsp;
28700Sstevel@tonic-gate 
28710Sstevel@tonic-gate 		for (mdsp = tlp->trl_spans; mdsp != NULL;
28720Sstevel@tonic-gate 		    mdsp = mdsp->mds_next) {
28730Sstevel@tonic-gate 			if (pfnum >= mdsp->mds_base &&
28740Sstevel@tonic-gate 			    pfnum < (mdsp->mds_base + mdsp->mds_npgs)) {
28750Sstevel@tonic-gate 				return (tlp);
28760Sstevel@tonic-gate 			}
28770Sstevel@tonic-gate 		}
28780Sstevel@tonic-gate 	}
28790Sstevel@tonic-gate 	return (NULL);
28800Sstevel@tonic-gate }
28810Sstevel@tonic-gate 
28820Sstevel@tonic-gate int
28830Sstevel@tonic-gate pfn_is_being_deleted(pfn_t pfnum)
28840Sstevel@tonic-gate {
28850Sstevel@tonic-gate 	struct transit_list_head *trh;
28860Sstevel@tonic-gate 	struct transit_list *tlp;
28870Sstevel@tonic-gate 	int ret;
28880Sstevel@tonic-gate 
28890Sstevel@tonic-gate 	trh = &transit_list_head;
28900Sstevel@tonic-gate 	if (trh->trh_head == NULL)
28910Sstevel@tonic-gate 		return (0);
28920Sstevel@tonic-gate 
28930Sstevel@tonic-gate 	mutex_enter(&trh->trh_lock);
28940Sstevel@tonic-gate 	tlp = pfnum_to_transit_list(trh, pfnum);
28950Sstevel@tonic-gate 	ret = (tlp != NULL && tlp->trl_collect);
28960Sstevel@tonic-gate 	mutex_exit(&trh->trh_lock);
28970Sstevel@tonic-gate 
28980Sstevel@tonic-gate 	return (ret);
28990Sstevel@tonic-gate }
29000Sstevel@tonic-gate 
29010Sstevel@tonic-gate #ifdef MEM_DEL_STATS
29020Sstevel@tonic-gate extern int hz;
29030Sstevel@tonic-gate static void
29040Sstevel@tonic-gate mem_del_stat_print_func(struct mem_handle *mhp)
29050Sstevel@tonic-gate {
29060Sstevel@tonic-gate 	uint64_t tmp;
29070Sstevel@tonic-gate 
29080Sstevel@tonic-gate 	if (mem_del_stat_print) {
29090Sstevel@tonic-gate 		printf("memory delete loop %x/%x, statistics%s\n",
29100Sstevel@tonic-gate 		    (uint_t)mhp->mh_transit.trl_spans->mds_base,
29110Sstevel@tonic-gate 		    (uint_t)mhp->mh_transit.trl_spans->mds_npgs,
29120Sstevel@tonic-gate 		    (mhp->mh_cancel ? " (cancelled)" : ""));
29130Sstevel@tonic-gate 		printf("\t%8u nloop\n", mhp->mh_delstat.nloop);
29140Sstevel@tonic-gate 		printf("\t%8u need_free\n", mhp->mh_delstat.need_free);
29150Sstevel@tonic-gate 		printf("\t%8u free_loop\n", mhp->mh_delstat.free_loop);
29160Sstevel@tonic-gate 		printf("\t%8u free_low\n", mhp->mh_delstat.free_low);
29170Sstevel@tonic-gate 		printf("\t%8u free_failed\n", mhp->mh_delstat.free_failed);
29180Sstevel@tonic-gate 		printf("\t%8u ncheck\n", mhp->mh_delstat.ncheck);
29190Sstevel@tonic-gate 		printf("\t%8u nopaget\n", mhp->mh_delstat.nopaget);
29200Sstevel@tonic-gate 		printf("\t%8u lockfail\n", mhp->mh_delstat.lockfail);
29210Sstevel@tonic-gate 		printf("\t%8u nfree\n", mhp->mh_delstat.nfree);
29220Sstevel@tonic-gate 		printf("\t%8u nreloc\n", mhp->mh_delstat.nreloc);
29230Sstevel@tonic-gate 		printf("\t%8u nrelocfail\n", mhp->mh_delstat.nrelocfail);
29240Sstevel@tonic-gate 		printf("\t%8u already_done\n", mhp->mh_delstat.already_done);
29250Sstevel@tonic-gate 		printf("\t%8u first_notfree\n", mhp->mh_delstat.first_notfree);
29260Sstevel@tonic-gate 		printf("\t%8u npplocked\n", mhp->mh_delstat.npplocked);
29270Sstevel@tonic-gate 		printf("\t%8u nlockreloc\n", mhp->mh_delstat.nlockreloc);
29280Sstevel@tonic-gate 		printf("\t%8u nnorepl\n", mhp->mh_delstat.nnorepl);
29290Sstevel@tonic-gate 		printf("\t%8u nmodreloc\n", mhp->mh_delstat.nmodreloc);
29300Sstevel@tonic-gate 		printf("\t%8u ndestroy\n", mhp->mh_delstat.ndestroy);
29310Sstevel@tonic-gate 		printf("\t%8u nputpage\n", mhp->mh_delstat.nputpage);
29320Sstevel@tonic-gate 		printf("\t%8u nnoreclaim\n", mhp->mh_delstat.nnoreclaim);
29330Sstevel@tonic-gate 		printf("\t%8u ndelay\n", mhp->mh_delstat.ndelay);
29340Sstevel@tonic-gate 		printf("\t%8u demotefail\n", mhp->mh_delstat.demotefail);
29350Sstevel@tonic-gate 		printf("\t%8u retired\n", mhp->mh_delstat.retired);
29360Sstevel@tonic-gate 		printf("\t%8u toxic\n", mhp->mh_delstat.toxic);
29370Sstevel@tonic-gate 		printf("\t%8u failing\n", mhp->mh_delstat.failing);
29380Sstevel@tonic-gate 		printf("\t%8u modtoxic\n", mhp->mh_delstat.modtoxic);
29390Sstevel@tonic-gate 		printf("\t%8u npplkdtoxic\n", mhp->mh_delstat.npplkdtoxic);
29400Sstevel@tonic-gate 		printf("\t%8u gptlmodfail\n", mhp->mh_delstat.gptlmodfail);
29410Sstevel@tonic-gate 		printf("\t%8u gptllckfail\n", mhp->mh_delstat.gptllckfail);
29420Sstevel@tonic-gate 		tmp = mhp->mh_delstat.nticks_total / hz;  /* seconds */
29430Sstevel@tonic-gate 		printf(
29440Sstevel@tonic-gate 		    "\t%"PRIu64" nticks_total - %"PRIu64" min %"PRIu64" sec\n",
29450Sstevel@tonic-gate 		    mhp->mh_delstat.nticks_total, tmp / 60, tmp % 60);
29460Sstevel@tonic-gate 
29470Sstevel@tonic-gate 		tmp = mhp->mh_delstat.nticks_pgrp / hz;  /* seconds */
29480Sstevel@tonic-gate 		printf(
29490Sstevel@tonic-gate 		    "\t%"PRIu64" nticks_pgrp - %"PRIu64" min %"PRIu64" sec\n",
29500Sstevel@tonic-gate 		    mhp->mh_delstat.nticks_pgrp, tmp / 60, tmp % 60);
29510Sstevel@tonic-gate 	}
29520Sstevel@tonic-gate }
29530Sstevel@tonic-gate #endif /* MEM_DEL_STATS */
29540Sstevel@tonic-gate 
29550Sstevel@tonic-gate struct mem_callback {
29560Sstevel@tonic-gate 	kphysm_setup_vector_t	*vec;
29570Sstevel@tonic-gate 	void			*arg;
29580Sstevel@tonic-gate };
29590Sstevel@tonic-gate 
29600Sstevel@tonic-gate #define	NMEMCALLBACKS		100
29610Sstevel@tonic-gate 
29620Sstevel@tonic-gate static struct mem_callback mem_callbacks[NMEMCALLBACKS];
29630Sstevel@tonic-gate static uint_t nmemcallbacks;
29640Sstevel@tonic-gate static krwlock_t mem_callback_rwlock;
29650Sstevel@tonic-gate 
29660Sstevel@tonic-gate int
29670Sstevel@tonic-gate kphysm_setup_func_register(kphysm_setup_vector_t *vec, void *arg)
29680Sstevel@tonic-gate {
29690Sstevel@tonic-gate 	uint_t i, found;
29700Sstevel@tonic-gate 
29710Sstevel@tonic-gate 	/*
29720Sstevel@tonic-gate 	 * This test will become more complicated when the version must
29730Sstevel@tonic-gate 	 * change.
29740Sstevel@tonic-gate 	 */
29750Sstevel@tonic-gate 	if (vec->version != KPHYSM_SETUP_VECTOR_VERSION)
29760Sstevel@tonic-gate 		return (EINVAL);
29770Sstevel@tonic-gate 
29780Sstevel@tonic-gate 	if (vec->post_add == NULL || vec->pre_del == NULL ||
29790Sstevel@tonic-gate 	    vec->post_del == NULL)
29800Sstevel@tonic-gate 		return (EINVAL);
29810Sstevel@tonic-gate 
29820Sstevel@tonic-gate 	rw_enter(&mem_callback_rwlock, RW_WRITER);
29830Sstevel@tonic-gate 	for (i = 0, found = 0; i < nmemcallbacks; i++) {
29840Sstevel@tonic-gate 		if (mem_callbacks[i].vec == NULL && found == 0)
29850Sstevel@tonic-gate 			found = i + 1;
29860Sstevel@tonic-gate 		if (mem_callbacks[i].vec == vec &&
29870Sstevel@tonic-gate 		    mem_callbacks[i].arg == arg) {
29880Sstevel@tonic-gate #ifdef DEBUG
29890Sstevel@tonic-gate 			/* Catch this in DEBUG kernels. */
29900Sstevel@tonic-gate 			cmn_err(CE_WARN, "kphysm_setup_func_register"
29910Sstevel@tonic-gate 			    "(0x%p, 0x%p) duplicate registration from 0x%p",
29920Sstevel@tonic-gate 			    (void *)vec, arg, (void *)caller());
29930Sstevel@tonic-gate #endif /* DEBUG */
29940Sstevel@tonic-gate 			rw_exit(&mem_callback_rwlock);
29950Sstevel@tonic-gate 			return (EEXIST);
29960Sstevel@tonic-gate 		}
29970Sstevel@tonic-gate 	}
29980Sstevel@tonic-gate 	if (found != 0) {
29990Sstevel@tonic-gate 		i = found - 1;
30000Sstevel@tonic-gate 	} else {
30010Sstevel@tonic-gate 		ASSERT(nmemcallbacks < NMEMCALLBACKS);
30020Sstevel@tonic-gate 		if (nmemcallbacks == NMEMCALLBACKS) {
30030Sstevel@tonic-gate 			rw_exit(&mem_callback_rwlock);
30040Sstevel@tonic-gate 			return (ENOMEM);
30050Sstevel@tonic-gate 		}
30060Sstevel@tonic-gate 		i = nmemcallbacks++;
30070Sstevel@tonic-gate 	}
30080Sstevel@tonic-gate 	mem_callbacks[i].vec = vec;
30090Sstevel@tonic-gate 	mem_callbacks[i].arg = arg;
30100Sstevel@tonic-gate 	rw_exit(&mem_callback_rwlock);
30110Sstevel@tonic-gate 	return (0);
30120Sstevel@tonic-gate }
30130Sstevel@tonic-gate 
30140Sstevel@tonic-gate void
30150Sstevel@tonic-gate kphysm_setup_func_unregister(kphysm_setup_vector_t *vec, void *arg)
30160Sstevel@tonic-gate {
30170Sstevel@tonic-gate 	uint_t i;
30180Sstevel@tonic-gate 
30190Sstevel@tonic-gate 	rw_enter(&mem_callback_rwlock, RW_WRITER);
30200Sstevel@tonic-gate 	for (i = 0; i < nmemcallbacks; i++) {
30210Sstevel@tonic-gate 		if (mem_callbacks[i].vec == vec &&
30220Sstevel@tonic-gate 		    mem_callbacks[i].arg == arg) {
30230Sstevel@tonic-gate 			mem_callbacks[i].vec = NULL;
30240Sstevel@tonic-gate 			mem_callbacks[i].arg = NULL;
30250Sstevel@tonic-gate 			if (i == (nmemcallbacks - 1))
30260Sstevel@tonic-gate 				nmemcallbacks--;
30270Sstevel@tonic-gate 			break;
30280Sstevel@tonic-gate 		}
30290Sstevel@tonic-gate 	}
30300Sstevel@tonic-gate 	rw_exit(&mem_callback_rwlock);
30310Sstevel@tonic-gate }
30320Sstevel@tonic-gate 
30330Sstevel@tonic-gate static void
30340Sstevel@tonic-gate kphysm_setup_post_add(pgcnt_t delta_pages)
30350Sstevel@tonic-gate {
30360Sstevel@tonic-gate 	uint_t i;
30370Sstevel@tonic-gate 
30380Sstevel@tonic-gate 	rw_enter(&mem_callback_rwlock, RW_READER);
30390Sstevel@tonic-gate 	for (i = 0; i < nmemcallbacks; i++) {
30400Sstevel@tonic-gate 		if (mem_callbacks[i].vec != NULL) {
30410Sstevel@tonic-gate 			(*mem_callbacks[i].vec->post_add)
30420Sstevel@tonic-gate 			    (mem_callbacks[i].arg, delta_pages);
30430Sstevel@tonic-gate 		}
30440Sstevel@tonic-gate 	}
30450Sstevel@tonic-gate 	rw_exit(&mem_callback_rwlock);
30460Sstevel@tonic-gate }
30470Sstevel@tonic-gate 
30480Sstevel@tonic-gate /*
30490Sstevel@tonic-gate  * Note the locking between pre_del and post_del: The reader lock is held
30500Sstevel@tonic-gate  * between the two calls to stop the set of functions from changing.
30510Sstevel@tonic-gate  */
30520Sstevel@tonic-gate 
30530Sstevel@tonic-gate static int
30540Sstevel@tonic-gate kphysm_setup_pre_del(pgcnt_t delta_pages)
30550Sstevel@tonic-gate {
30560Sstevel@tonic-gate 	uint_t i;
30570Sstevel@tonic-gate 	int ret;
30580Sstevel@tonic-gate 	int aret;
30590Sstevel@tonic-gate 
30600Sstevel@tonic-gate 	ret = 0;
30610Sstevel@tonic-gate 	rw_enter(&mem_callback_rwlock, RW_READER);
30620Sstevel@tonic-gate 	for (i = 0; i < nmemcallbacks; i++) {
30630Sstevel@tonic-gate 		if (mem_callbacks[i].vec != NULL) {
30640Sstevel@tonic-gate 			aret = (*mem_callbacks[i].vec->pre_del)
30650Sstevel@tonic-gate 			    (mem_callbacks[i].arg, delta_pages);
30660Sstevel@tonic-gate 			ret |= aret;
30670Sstevel@tonic-gate 		}
30680Sstevel@tonic-gate 	}
30690Sstevel@tonic-gate 
30700Sstevel@tonic-gate 	return (ret);
30710Sstevel@tonic-gate }
30720Sstevel@tonic-gate 
30730Sstevel@tonic-gate static void
30740Sstevel@tonic-gate kphysm_setup_post_del(pgcnt_t delta_pages, int cancelled)
30750Sstevel@tonic-gate {
30760Sstevel@tonic-gate 	uint_t i;
30770Sstevel@tonic-gate 
30780Sstevel@tonic-gate 	for (i = 0; i < nmemcallbacks; i++) {
30790Sstevel@tonic-gate 		if (mem_callbacks[i].vec != NULL) {
30800Sstevel@tonic-gate 			(*mem_callbacks[i].vec->post_del)
30810Sstevel@tonic-gate 			    (mem_callbacks[i].arg, delta_pages, cancelled);
30820Sstevel@tonic-gate 		}
30830Sstevel@tonic-gate 	}
30840Sstevel@tonic-gate 	rw_exit(&mem_callback_rwlock);
30850Sstevel@tonic-gate }
30860Sstevel@tonic-gate 
30870Sstevel@tonic-gate static int
30880Sstevel@tonic-gate kphysm_split_memseg(
30890Sstevel@tonic-gate 	pfn_t base,
30900Sstevel@tonic-gate 	pgcnt_t npgs)
30910Sstevel@tonic-gate {
30920Sstevel@tonic-gate 	struct memseg *seg;
30930Sstevel@tonic-gate 	struct memseg **segpp;
30940Sstevel@tonic-gate 	pgcnt_t size_low, size_high;
30950Sstevel@tonic-gate 	struct memseg *seg_low, *seg_mid, *seg_high;
30960Sstevel@tonic-gate 
30970Sstevel@tonic-gate 	/*
30980Sstevel@tonic-gate 	 * Lock the memsegs list against other updates now
30990Sstevel@tonic-gate 	 */
31000Sstevel@tonic-gate 	memsegs_lock(1);
31010Sstevel@tonic-gate 
31020Sstevel@tonic-gate 	/*
31030Sstevel@tonic-gate 	 * Find boot time memseg that wholly covers this area.
31040Sstevel@tonic-gate 	 */
31050Sstevel@tonic-gate 
31060Sstevel@tonic-gate 	/* First find the memseg with page 'base' in it. */
31070Sstevel@tonic-gate 	for (segpp = &memsegs; (seg = *segpp) != NULL;
31080Sstevel@tonic-gate 	    segpp = &((*segpp)->next)) {
31090Sstevel@tonic-gate 		if (base >= seg->pages_base && base < seg->pages_end)
31100Sstevel@tonic-gate 			break;
31110Sstevel@tonic-gate 	}
31120Sstevel@tonic-gate 	if (seg == NULL) {
31130Sstevel@tonic-gate 		memsegs_unlock(1);
31140Sstevel@tonic-gate 		return (0);
31150Sstevel@tonic-gate 	}
311610106SJason.Beloro@Sun.COM 	if (memseg_includes_meta(seg)) {
31170Sstevel@tonic-gate 		memsegs_unlock(1);
31180Sstevel@tonic-gate 		return (0);
31190Sstevel@tonic-gate 	}
31200Sstevel@tonic-gate 	if ((base + npgs) > seg->pages_end) {
31210Sstevel@tonic-gate 		memsegs_unlock(1);
31220Sstevel@tonic-gate 		return (0);
31230Sstevel@tonic-gate 	}
31240Sstevel@tonic-gate 
31250Sstevel@tonic-gate 	/*
31260Sstevel@tonic-gate 	 * Work out the size of the two segments that will
31270Sstevel@tonic-gate 	 * surround the new segment, one for low address
31280Sstevel@tonic-gate 	 * and one for high.
31290Sstevel@tonic-gate 	 */
31300Sstevel@tonic-gate 	ASSERT(base >= seg->pages_base);
31310Sstevel@tonic-gate 	size_low = base - seg->pages_base;
31320Sstevel@tonic-gate 	ASSERT(seg->pages_end >= (base + npgs));
31330Sstevel@tonic-gate 	size_high = seg->pages_end - (base + npgs);
31340Sstevel@tonic-gate 
31350Sstevel@tonic-gate 	/*
31360Sstevel@tonic-gate 	 * Sanity check.
31370Sstevel@tonic-gate 	 */
31380Sstevel@tonic-gate 	if ((size_low + size_high) == 0) {
31390Sstevel@tonic-gate 		memsegs_unlock(1);
31400Sstevel@tonic-gate 		return (0);
31410Sstevel@tonic-gate 	}
31420Sstevel@tonic-gate 
31430Sstevel@tonic-gate 	/*
31440Sstevel@tonic-gate 	 * Allocate the new structures. The old memseg will not be freed
31450Sstevel@tonic-gate 	 * as there may be a reference to it.
31460Sstevel@tonic-gate 	 */
31470Sstevel@tonic-gate 	seg_low = NULL;
31480Sstevel@tonic-gate 	seg_high = NULL;
31490Sstevel@tonic-gate 
315010106SJason.Beloro@Sun.COM 	if (size_low != 0)
315110106SJason.Beloro@Sun.COM 		seg_low = memseg_alloc();
315210106SJason.Beloro@Sun.COM 
315310106SJason.Beloro@Sun.COM 	seg_mid = memseg_alloc();
315410106SJason.Beloro@Sun.COM 
315510106SJason.Beloro@Sun.COM 	if (size_high != 0)
315610106SJason.Beloro@Sun.COM 		seg_high = memseg_alloc();
31570Sstevel@tonic-gate 
31580Sstevel@tonic-gate 	/*
31590Sstevel@tonic-gate 	 * All allocation done now.
31600Sstevel@tonic-gate 	 */
31610Sstevel@tonic-gate 	if (size_low != 0) {
31620Sstevel@tonic-gate 		seg_low->pages = seg->pages;
31630Sstevel@tonic-gate 		seg_low->epages = seg_low->pages + size_low;
31640Sstevel@tonic-gate 		seg_low->pages_base = seg->pages_base;
31650Sstevel@tonic-gate 		seg_low->pages_end = seg_low->pages_base + size_low;
31660Sstevel@tonic-gate 		seg_low->next = seg_mid;
316710106SJason.Beloro@Sun.COM 		seg_low->msegflags = seg->msegflags;
31680Sstevel@tonic-gate 	}
31690Sstevel@tonic-gate 	if (size_high != 0) {
31700Sstevel@tonic-gate 		seg_high->pages = seg->epages - size_high;
31710Sstevel@tonic-gate 		seg_high->epages = seg_high->pages + size_high;
31720Sstevel@tonic-gate 		seg_high->pages_base = seg->pages_end - size_high;
31730Sstevel@tonic-gate 		seg_high->pages_end = seg_high->pages_base + size_high;
31740Sstevel@tonic-gate 		seg_high->next = seg->next;
317510106SJason.Beloro@Sun.COM 		seg_high->msegflags = seg->msegflags;
31760Sstevel@tonic-gate 	}
31770Sstevel@tonic-gate 
31780Sstevel@tonic-gate 	seg_mid->pages = seg->pages + size_low;
31790Sstevel@tonic-gate 	seg_mid->pages_base = seg->pages_base + size_low;
31800Sstevel@tonic-gate 	seg_mid->epages = seg->epages - size_high;
31810Sstevel@tonic-gate 	seg_mid->pages_end = seg->pages_end - size_high;
31820Sstevel@tonic-gate 	seg_mid->next = (seg_high != NULL) ? seg_high : seg->next;
318310106SJason.Beloro@Sun.COM 	seg_mid->msegflags = seg->msegflags;
31840Sstevel@tonic-gate 
31850Sstevel@tonic-gate 	/*
31860Sstevel@tonic-gate 	 * Update hat_kpm specific info of all involved memsegs and
31870Sstevel@tonic-gate 	 * allow hat_kpm specific global chain updates.
31880Sstevel@tonic-gate 	 */
31890Sstevel@tonic-gate 	hat_kpm_split_mseg_update(seg, segpp, seg_low, seg_mid, seg_high);
31900Sstevel@tonic-gate 
31910Sstevel@tonic-gate 	/*
31920Sstevel@tonic-gate 	 * At this point we have two equivalent memseg sub-chains,
31930Sstevel@tonic-gate 	 * seg and seg_low/seg_mid/seg_high, which both chain on to
31940Sstevel@tonic-gate 	 * the same place in the global chain. By re-writing the pointer
31950Sstevel@tonic-gate 	 * in the previous element we switch atomically from using the old
31960Sstevel@tonic-gate 	 * (seg) to the new.
31970Sstevel@tonic-gate 	 */
31980Sstevel@tonic-gate 	*segpp = (seg_low != NULL) ? seg_low : seg_mid;
31990Sstevel@tonic-gate 
32000Sstevel@tonic-gate 	membar_enter();
32010Sstevel@tonic-gate 
32020Sstevel@tonic-gate 	build_pfn_hash();
32030Sstevel@tonic-gate 	memsegs_unlock(1);
32040Sstevel@tonic-gate 
32050Sstevel@tonic-gate 	/*
32060Sstevel@tonic-gate 	 * We leave the old segment, 'seg', intact as there may be
32070Sstevel@tonic-gate 	 * references to it. Also, as the value of total_pages has not
32080Sstevel@tonic-gate 	 * changed and the memsegs list is effectively the same when
32090Sstevel@tonic-gate 	 * accessed via the old or the new pointer, we do not have to
32100Sstevel@tonic-gate 	 * cause pageout_scanner() to re-evaluate its hand pointers.
32110Sstevel@tonic-gate 	 *
32120Sstevel@tonic-gate 	 * We currently do not re-use or reclaim the page_t memory.
32130Sstevel@tonic-gate 	 * If we do, then this may have to change.
32140Sstevel@tonic-gate 	 */
32150Sstevel@tonic-gate 
32160Sstevel@tonic-gate 	mutex_enter(&memseg_lists_lock);
32170Sstevel@tonic-gate 	seg->lnext = memseg_edit_junk;
32180Sstevel@tonic-gate 	memseg_edit_junk = seg;
32190Sstevel@tonic-gate 	mutex_exit(&memseg_lists_lock);
32200Sstevel@tonic-gate 
32210Sstevel@tonic-gate 	return (1);
32220Sstevel@tonic-gate }
32230Sstevel@tonic-gate 
32240Sstevel@tonic-gate /*
32250Sstevel@tonic-gate  * The sfmmu hat layer (e.g.) accesses some parts of the memseg
32260Sstevel@tonic-gate  * structure using physical addresses. Therefore a kmem_cache is
32270Sstevel@tonic-gate  * used with KMC_NOHASH to avoid page crossings within a memseg
32280Sstevel@tonic-gate  * structure. KMC_NOHASH requires that no external (outside of
32290Sstevel@tonic-gate  * slab) information is allowed. This, in turn, implies that the
32300Sstevel@tonic-gate  * cache's slabsize must be exactly a single page, since per-slab
32310Sstevel@tonic-gate  * information (e.g. the freelist for the slab) is kept at the
32320Sstevel@tonic-gate  * end of the slab, where it is easy to locate. Should be changed
32330Sstevel@tonic-gate  * when a more obvious kmem_cache interface/flag will become
32340Sstevel@tonic-gate  * available.
32350Sstevel@tonic-gate  */
32360Sstevel@tonic-gate void
32370Sstevel@tonic-gate mem_config_init()
32380Sstevel@tonic-gate {
32390Sstevel@tonic-gate 	memseg_cache = kmem_cache_create("memseg_cache", sizeof (struct memseg),
32406242Smb158278 	    0, NULL, NULL, NULL, NULL, static_arena, KMC_NOHASH);
32410Sstevel@tonic-gate }
324210106SJason.Beloro@Sun.COM 
324310106SJason.Beloro@Sun.COM struct memseg *
324410106SJason.Beloro@Sun.COM memseg_alloc()
324510106SJason.Beloro@Sun.COM {
324610106SJason.Beloro@Sun.COM 	struct memseg *seg;
324710106SJason.Beloro@Sun.COM 
324810106SJason.Beloro@Sun.COM 	seg = kmem_cache_alloc(memseg_cache, KM_SLEEP);
324910106SJason.Beloro@Sun.COM 	bzero(seg, sizeof (struct memseg));
325010106SJason.Beloro@Sun.COM 
325110106SJason.Beloro@Sun.COM 	return (seg);
325210106SJason.Beloro@Sun.COM }
325310106SJason.Beloro@Sun.COM 
325410106SJason.Beloro@Sun.COM /*
325510106SJason.Beloro@Sun.COM  * Return whether the page_t memory for this memseg
325610106SJason.Beloro@Sun.COM  * is included in the memseg itself.
325710106SJason.Beloro@Sun.COM  */
325810106SJason.Beloro@Sun.COM static int
325910106SJason.Beloro@Sun.COM memseg_includes_meta(struct memseg *seg)
326010106SJason.Beloro@Sun.COM {
326110106SJason.Beloro@Sun.COM 	return (seg->msegflags & MEMSEG_META_INCL);
326210106SJason.Beloro@Sun.COM }
326310106SJason.Beloro@Sun.COM 
326410106SJason.Beloro@Sun.COM pfn_t
326510106SJason.Beloro@Sun.COM memseg_get_start(struct memseg *seg)
326610106SJason.Beloro@Sun.COM {
326710106SJason.Beloro@Sun.COM 	pfn_t		pt_start;
326810106SJason.Beloro@Sun.COM 
326910106SJason.Beloro@Sun.COM 	if (memseg_includes_meta(seg)) {
327010106SJason.Beloro@Sun.COM 		pt_start = hat_getpfnum(kas.a_hat, (caddr_t)seg->pages);
327110106SJason.Beloro@Sun.COM 
327210106SJason.Beloro@Sun.COM 		/* Meta data is required to be at the beginning */
327310106SJason.Beloro@Sun.COM 		ASSERT(pt_start < seg->pages_base);
327410106SJason.Beloro@Sun.COM 	} else
327510106SJason.Beloro@Sun.COM 		pt_start = seg->pages_base;
327610106SJason.Beloro@Sun.COM 
327710106SJason.Beloro@Sun.COM 	return (pt_start);
327810106SJason.Beloro@Sun.COM }
327910106SJason.Beloro@Sun.COM 
328010106SJason.Beloro@Sun.COM /*
328110106SJason.Beloro@Sun.COM  * Invalidate memseg pointers in cpu private vm data caches.
328210106SJason.Beloro@Sun.COM  */
328310106SJason.Beloro@Sun.COM static void
328410106SJason.Beloro@Sun.COM memseg_cpu_vm_flush()
328510106SJason.Beloro@Sun.COM {
328610106SJason.Beloro@Sun.COM 	cpu_t *cp;
328710106SJason.Beloro@Sun.COM 	vm_cpu_data_t *vc;
328810106SJason.Beloro@Sun.COM 
328910106SJason.Beloro@Sun.COM 	mutex_enter(&cpu_lock);
329010106SJason.Beloro@Sun.COM 	pause_cpus(NULL);
329110106SJason.Beloro@Sun.COM 
329210106SJason.Beloro@Sun.COM 	cp = cpu_list;
329310106SJason.Beloro@Sun.COM 	do {
329410106SJason.Beloro@Sun.COM 		vc = cp->cpu_vm_data;
329510106SJason.Beloro@Sun.COM 		vc->vc_pnum_memseg = NULL;
329610106SJason.Beloro@Sun.COM 		vc->vc_pnext_memseg = NULL;
329710106SJason.Beloro@Sun.COM 
329810106SJason.Beloro@Sun.COM 	} while ((cp = cp->cpu_next) != cpu_list);
329910106SJason.Beloro@Sun.COM 
330010106SJason.Beloro@Sun.COM 	start_cpus();
330110106SJason.Beloro@Sun.COM 	mutex_exit(&cpu_lock);
330210106SJason.Beloro@Sun.COM }
3303