xref: /onnv-gate/usr/src/uts/sun4u/os/ppage.c (revision 6955:0d4f7a277fd3)
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
52241Shuah  * Common Development and Distribution License (the "License").
62241Shuah  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*6955Sbpramod  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/systm.h>
300Sstevel@tonic-gate #include <sys/archsystm.h>
310Sstevel@tonic-gate #include <sys/machsystm.h>
320Sstevel@tonic-gate #include <sys/t_lock.h>
330Sstevel@tonic-gate #include <sys/vmem.h>
340Sstevel@tonic-gate #include <sys/mman.h>
350Sstevel@tonic-gate #include <sys/vm.h>
360Sstevel@tonic-gate #include <sys/cpu.h>
370Sstevel@tonic-gate #include <sys/cmn_err.h>
380Sstevel@tonic-gate #include <sys/cpuvar.h>
390Sstevel@tonic-gate #include <sys/atomic.h>
400Sstevel@tonic-gate #include <vm/as.h>
410Sstevel@tonic-gate #include <vm/hat.h>
420Sstevel@tonic-gate #include <vm/as.h>
430Sstevel@tonic-gate #include <vm/page.h>
440Sstevel@tonic-gate #include <vm/seg.h>
450Sstevel@tonic-gate #include <vm/seg_kmem.h>
46*6955Sbpramod #include <vm/seg_kpm.h>
470Sstevel@tonic-gate #include <vm/hat_sfmmu.h>
480Sstevel@tonic-gate #include <sys/debug.h>
490Sstevel@tonic-gate #include <sys/cpu_module.h>
50*6955Sbpramod #include <sys/mem_cage.h>
510Sstevel@tonic-gate 
520Sstevel@tonic-gate /*
530Sstevel@tonic-gate  * A quick way to generate a cache consistent address to map in a page.
540Sstevel@tonic-gate  * users: ppcopy, pagezero, /proc, dev/mem
550Sstevel@tonic-gate  *
560Sstevel@tonic-gate  * The ppmapin/ppmapout routines provide a quick way of generating a cache
570Sstevel@tonic-gate  * consistent address by reserving a given amount of kernel address space.
580Sstevel@tonic-gate  * The base is PPMAPBASE and its size is PPMAPSIZE.  This memory is divided
590Sstevel@tonic-gate  * into x number of sets, where x is the number of colors for the virtual
600Sstevel@tonic-gate  * cache. The number of colors is how many times a page can be mapped
610Sstevel@tonic-gate  * simulatenously in the cache.  For direct map caches this translates to
620Sstevel@tonic-gate  * the number of pages in the cache.
630Sstevel@tonic-gate  * Each set will be assigned a group of virtual pages from the reserved memory
640Sstevel@tonic-gate  * depending on its virtual color.
650Sstevel@tonic-gate  * When trying to assign a virtual address we will find out the color for the
660Sstevel@tonic-gate  * physical page in question (if applicable).  Then we will try to find an
670Sstevel@tonic-gate  * available virtual page from the set of the appropiate color.
680Sstevel@tonic-gate  */
690Sstevel@tonic-gate 
700Sstevel@tonic-gate #define	clsettoarray(color, set) ((color * nsets) + set)
710Sstevel@tonic-gate 
720Sstevel@tonic-gate int pp_slots = 4;		/* small default, tuned by cpu module */
730Sstevel@tonic-gate 
740Sstevel@tonic-gate /* tuned by cpu module, default is "safe" */
750Sstevel@tonic-gate int pp_consistent_coloring = PPAGE_STORES_POLLUTE | PPAGE_LOADS_POLLUTE;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate static caddr_t	ppmap_vaddrs[PPMAPSIZE / MMU_PAGESIZE];
780Sstevel@tonic-gate static int	nsets;			/* number of sets */
790Sstevel@tonic-gate static int	ppmap_pages;		/* generate align mask */
800Sstevel@tonic-gate static int	ppmap_shift;		/* set selector */
810Sstevel@tonic-gate 
820Sstevel@tonic-gate #ifdef PPDEBUG
830Sstevel@tonic-gate #define		MAXCOLORS	16	/* for debug only */
840Sstevel@tonic-gate static int	ppalloc_noslot = 0;	/* # of allocations from kernelmap */
850Sstevel@tonic-gate static int	align_hits[MAXCOLORS];
860Sstevel@tonic-gate static int	pp_allocs;		/* # of ppmapin requests */
870Sstevel@tonic-gate #endif /* PPDEBUG */
880Sstevel@tonic-gate 
890Sstevel@tonic-gate /*
900Sstevel@tonic-gate  * There are only 64 TLB entries on spitfire, 16 on cheetah
910Sstevel@tonic-gate  * (fully-associative TLB) so we allow the cpu module to tune the
920Sstevel@tonic-gate  * number to use here via pp_slots.
930Sstevel@tonic-gate  */
940Sstevel@tonic-gate static struct ppmap_va {
950Sstevel@tonic-gate 	caddr_t	ppmap_slots[MAXPP_SLOTS];
960Sstevel@tonic-gate } ppmap_va[NCPU];
970Sstevel@tonic-gate 
980Sstevel@tonic-gate void
ppmapinit(void)990Sstevel@tonic-gate ppmapinit(void)
1000Sstevel@tonic-gate {
1010Sstevel@tonic-gate 	int color, nset, setsize;
1020Sstevel@tonic-gate 	caddr_t va;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	ASSERT(pp_slots <= MAXPP_SLOTS);
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	va = (caddr_t)PPMAPBASE;
1070Sstevel@tonic-gate 	if (cache & CACHE_VAC) {
1080Sstevel@tonic-gate 		int a;
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 		ppmap_pages = mmu_btop(shm_alignment);
1110Sstevel@tonic-gate 		nsets = PPMAPSIZE / shm_alignment;
1120Sstevel@tonic-gate 		setsize = shm_alignment;
1130Sstevel@tonic-gate 		ppmap_shift = MMU_PAGESHIFT;
1140Sstevel@tonic-gate 		a = ppmap_pages;
1150Sstevel@tonic-gate 		while (a >>= 1)
1160Sstevel@tonic-gate 			ppmap_shift++;
1170Sstevel@tonic-gate 	} else {
1180Sstevel@tonic-gate 		/*
1190Sstevel@tonic-gate 		 * If we do not have a virtual indexed cache we simply
1200Sstevel@tonic-gate 		 * have only one set containing all pages.
1210Sstevel@tonic-gate 		 */
1220Sstevel@tonic-gate 		ppmap_pages = 1;
1230Sstevel@tonic-gate 		nsets = mmu_btop(PPMAPSIZE);
1240Sstevel@tonic-gate 		setsize = MMU_PAGESIZE;
1250Sstevel@tonic-gate 		ppmap_shift = MMU_PAGESHIFT;
1260Sstevel@tonic-gate 	}
1270Sstevel@tonic-gate 	for (color = 0; color < ppmap_pages; color++) {
1280Sstevel@tonic-gate 		for (nset = 0; nset < nsets; nset++) {
1290Sstevel@tonic-gate 			ppmap_vaddrs[clsettoarray(color, nset)] =
1300Sstevel@tonic-gate 			    (caddr_t)((uintptr_t)va + (nset * setsize));
1310Sstevel@tonic-gate 		}
1320Sstevel@tonic-gate 		va += MMU_PAGESIZE;
1330Sstevel@tonic-gate 	}
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate /*
1370Sstevel@tonic-gate  * Allocate a cache consistent virtual address to map a page, pp,
1380Sstevel@tonic-gate  * with protection, vprot; and map it in the MMU, using the most
1390Sstevel@tonic-gate  * efficient means possible.  The argument avoid is a virtual address
1400Sstevel@tonic-gate  * hint which when masked yields an offset into a virtual cache
1410Sstevel@tonic-gate  * that should be avoided when allocating an address to map in a
1420Sstevel@tonic-gate  * page.  An avoid arg of -1 means you don't care, for instance pagezero.
1430Sstevel@tonic-gate  *
1440Sstevel@tonic-gate  * machine dependent, depends on virtual address space layout,
1450Sstevel@tonic-gate  * understands that all kernel addresses have bit 31 set.
1460Sstevel@tonic-gate  *
1470Sstevel@tonic-gate  * NOTE: For sun4 platforms the meaning of the hint argument is opposite from
1480Sstevel@tonic-gate  * that found in other architectures.  In other architectures the hint
1490Sstevel@tonic-gate  * (called avoid) was used to ask ppmapin to NOT use the specified cache color.
1500Sstevel@tonic-gate  * This was used to avoid virtual cache trashing in the bcopy.  Unfortunately
1510Sstevel@tonic-gate  * in the case of a COW,  this later on caused a cache aliasing conflict.  In
1520Sstevel@tonic-gate  * sun4, the bcopy routine uses the block ld/st instructions so we don't have
1530Sstevel@tonic-gate  * to worry about virtual cache trashing.  Actually, by using the hint to choose
1540Sstevel@tonic-gate  * the right color we can almost guarantee a cache conflict will not occur.
1550Sstevel@tonic-gate  */
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate caddr_t
ppmapin(page_t * pp,uint_t vprot,caddr_t hint)1580Sstevel@tonic-gate ppmapin(page_t *pp, uint_t vprot, caddr_t hint)
1590Sstevel@tonic-gate {
1600Sstevel@tonic-gate 	int color, nset, index, start;
1610Sstevel@tonic-gate 	caddr_t va;
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate #ifdef PPDEBUG
1640Sstevel@tonic-gate 	pp_allocs++;
1650Sstevel@tonic-gate #endif /* PPDEBUG */
1660Sstevel@tonic-gate 	if (cache & CACHE_VAC) {
1670Sstevel@tonic-gate 		color = sfmmu_get_ppvcolor(pp);
1680Sstevel@tonic-gate 		if (color == -1) {
1690Sstevel@tonic-gate 			if ((intptr_t)hint != -1L) {
1700Sstevel@tonic-gate 				color = addr_to_vcolor(hint);
1710Sstevel@tonic-gate 			} else {
1720Sstevel@tonic-gate 				color = addr_to_vcolor(mmu_ptob(pp->p_pagenum));
1730Sstevel@tonic-gate 			}
1740Sstevel@tonic-gate 		}
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	} else {
1770Sstevel@tonic-gate 		/*
1780Sstevel@tonic-gate 		 * For physical caches, we can pick any address we want.
1790Sstevel@tonic-gate 		 */
1800Sstevel@tonic-gate 		color = 0;
1810Sstevel@tonic-gate 	}
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	start = color;
1840Sstevel@tonic-gate 	do {
1850Sstevel@tonic-gate 		for (nset = 0; nset < nsets; nset++) {
1860Sstevel@tonic-gate 			index = clsettoarray(color, nset);
1870Sstevel@tonic-gate 			va = ppmap_vaddrs[index];
1880Sstevel@tonic-gate 			if (va != NULL) {
1890Sstevel@tonic-gate #ifdef PPDEBUG
1900Sstevel@tonic-gate 				align_hits[color]++;
1910Sstevel@tonic-gate #endif /* PPDEBUG */
1920Sstevel@tonic-gate 				if (casptr(&ppmap_vaddrs[index],
1930Sstevel@tonic-gate 				    va, NULL) == va) {
1940Sstevel@tonic-gate 					hat_memload(kas.a_hat, va, pp,
195*6955Sbpramod 					    vprot | HAT_NOSYNC,
196*6955Sbpramod 					    HAT_LOAD_LOCK);
1970Sstevel@tonic-gate 					return (va);
1980Sstevel@tonic-gate 				}
1990Sstevel@tonic-gate 			}
2000Sstevel@tonic-gate 		}
2010Sstevel@tonic-gate 		/*
2020Sstevel@tonic-gate 		 * first pick didn't succeed, try another
2030Sstevel@tonic-gate 		 */
2040Sstevel@tonic-gate 		if (++color == ppmap_pages)
2050Sstevel@tonic-gate 			color = 0;
2060Sstevel@tonic-gate 	} while (color != start);
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate #ifdef PPDEBUG
2090Sstevel@tonic-gate 	ppalloc_noslot++;
2100Sstevel@tonic-gate #endif /* PPDEBUG */
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	/*
2130Sstevel@tonic-gate 	 * No free slots; get a random one from the kernel heap area.
2140Sstevel@tonic-gate 	 */
2150Sstevel@tonic-gate 	va = vmem_alloc(heap_arena, PAGESIZE, VM_SLEEP);
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	hat_memload(kas.a_hat, va, pp, vprot | HAT_NOSYNC, HAT_LOAD_LOCK);
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	return (va);
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate void
ppmapout(caddr_t va)2240Sstevel@tonic-gate ppmapout(caddr_t va)
2250Sstevel@tonic-gate {
2260Sstevel@tonic-gate 	int color, nset, index;
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	if (va >= kernelheap && va < ekernelheap) {
2290Sstevel@tonic-gate 		/*
2300Sstevel@tonic-gate 		 * Space came from kernelmap, flush the page and
2310Sstevel@tonic-gate 		 * return the space.
2320Sstevel@tonic-gate 		 */
2330Sstevel@tonic-gate 		hat_unload(kas.a_hat, va, PAGESIZE,
2340Sstevel@tonic-gate 		    (HAT_UNLOAD_NOSYNC | HAT_UNLOAD_UNLOCK));
2350Sstevel@tonic-gate 		vmem_free(heap_arena, va, PAGESIZE);
2360Sstevel@tonic-gate 	} else {
2370Sstevel@tonic-gate 		/*
2380Sstevel@tonic-gate 		 * Space came from ppmap_vaddrs[], give it back.
2390Sstevel@tonic-gate 		 */
2400Sstevel@tonic-gate 		color = addr_to_vcolor(va);
2410Sstevel@tonic-gate 		ASSERT((cache & CACHE_VAC)? (color < ppmap_pages) : 1);
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 		nset = ((uintptr_t)va >> ppmap_shift) & (nsets - 1);
2440Sstevel@tonic-gate 		index = clsettoarray(color, nset);
2450Sstevel@tonic-gate 		hat_unload(kas.a_hat, va, PAGESIZE,
2460Sstevel@tonic-gate 		    (HAT_UNLOAD_NOSYNC | HAT_UNLOAD_UNLOCK));
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 		ASSERT(ppmap_vaddrs[index] == NULL);
2490Sstevel@tonic-gate 		ppmap_vaddrs[index] = va;
2500Sstevel@tonic-gate 	}
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate #ifdef DEBUG
2540Sstevel@tonic-gate #define	PP_STAT_ADD(stat)	(stat)++
2550Sstevel@tonic-gate uint_t pload, ploadfail;
2560Sstevel@tonic-gate uint_t ppzero, ppzero_short;
2570Sstevel@tonic-gate #else
2580Sstevel@tonic-gate #define	PP_STAT_ADD(stat)
2590Sstevel@tonic-gate #endif /* DEBUG */
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate /*
2620Sstevel@tonic-gate  * Find a slot in per CPU page copy area. Load up a locked TLB in the
2630Sstevel@tonic-gate  * running cpu. We don't call hat layer to load up the tte since the
2640Sstevel@tonic-gate  * mapping is only temporary. If the thread migrates it'll get a TLB
2650Sstevel@tonic-gate  * miss trap and TLB/TSB miss handler will panic since there is no
2660Sstevel@tonic-gate  * official hat record of this mapping.
2670Sstevel@tonic-gate  */
2680Sstevel@tonic-gate static caddr_t
pp_load_tlb(processorid_t cpu,caddr_t ** pslot,page_t * pp,uint_t prot)2690Sstevel@tonic-gate pp_load_tlb(processorid_t cpu, caddr_t **pslot, page_t *pp, uint_t prot)
2700Sstevel@tonic-gate {
2710Sstevel@tonic-gate 	struct ppmap_va	*ppmap;
2720Sstevel@tonic-gate 	tte_t		tte;
2730Sstevel@tonic-gate 	caddr_t		*myslot;
2740Sstevel@tonic-gate 	caddr_t		va;
2750Sstevel@tonic-gate 	long		i, start, stride;
2760Sstevel@tonic-gate 	int		vcolor;
2770Sstevel@tonic-gate 	uint_t		flags, strict_flag;
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	PP_STAT_ADD(pload);
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	ppmap = &ppmap_va[cpu];
2820Sstevel@tonic-gate 	va = (caddr_t)(PPMAP_FAST_BASE + (MMU_PAGESIZE * MAXPP_SLOTS) * cpu);
2830Sstevel@tonic-gate 	myslot = ppmap->ppmap_slots;
2840Sstevel@tonic-gate 	ASSERT(addr_to_vcolor(va) == 0);
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	if (prot & TTE_HWWR_INT) {
2870Sstevel@tonic-gate 		flags = PPAGE_STORE_VCOLORING | PPAGE_STORES_POLLUTE;
2880Sstevel@tonic-gate 		strict_flag = PPAGE_STORES_POLLUTE;
2890Sstevel@tonic-gate 	} else {
2900Sstevel@tonic-gate 		flags = PPAGE_LOAD_VCOLORING | PPAGE_LOADS_POLLUTE;
2910Sstevel@tonic-gate 		strict_flag = PPAGE_LOADS_POLLUTE;
2920Sstevel@tonic-gate 	}
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	/*
2950Sstevel@tonic-gate 	 * If consistent handling is required then keep the current
2960Sstevel@tonic-gate 	 * vcolor of the page.  Furthermore, if loads or stores can
2970Sstevel@tonic-gate 	 * pollute the VAC then using a "new" page (unassigned vcolor)
2980Sstevel@tonic-gate 	 * won't work and we have to return a failure.
2990Sstevel@tonic-gate 	 */
3000Sstevel@tonic-gate 	if (pp_consistent_coloring & flags) {
3010Sstevel@tonic-gate 		vcolor = sfmmu_get_ppvcolor(pp);
3020Sstevel@tonic-gate 		if ((vcolor == -1) &&
3030Sstevel@tonic-gate 		    (pp_consistent_coloring & strict_flag))
3040Sstevel@tonic-gate 			return (NULL);
3050Sstevel@tonic-gate 		/* else keep the current vcolor of the page */
3060Sstevel@tonic-gate 	} else {
3070Sstevel@tonic-gate 		vcolor = -1;
3080Sstevel@tonic-gate 	}
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	if (vcolor != -1) {
3110Sstevel@tonic-gate 		va += MMU_PAGESIZE * vcolor;
3120Sstevel@tonic-gate 		start = vcolor;
3130Sstevel@tonic-gate 		stride = ppmap_pages; /* number of colors */
3140Sstevel@tonic-gate 		myslot += vcolor;
3150Sstevel@tonic-gate 	} else {
3160Sstevel@tonic-gate 		start = 0;
3170Sstevel@tonic-gate 		stride = 1;
3180Sstevel@tonic-gate 	}
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	for (i = start; i < pp_slots; i += stride) {
3210Sstevel@tonic-gate 		if (*myslot == NULL) {
3220Sstevel@tonic-gate 			if (casptr(myslot, NULL, va) == NULL)
3230Sstevel@tonic-gate 				break;
3240Sstevel@tonic-gate 		}
3250Sstevel@tonic-gate 		myslot += stride;
3260Sstevel@tonic-gate 		va += MMU_PAGESIZE * stride;
3270Sstevel@tonic-gate 	}
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	if (i >= pp_slots) {
3300Sstevel@tonic-gate 		PP_STAT_ADD(ploadfail);
3310Sstevel@tonic-gate 		return (NULL);
3320Sstevel@tonic-gate 	}
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 	ASSERT(vcolor == -1 || addr_to_vcolor(va) == vcolor);
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	/*
3370Sstevel@tonic-gate 	 * Now we have a slot we can use, make the tte.
3380Sstevel@tonic-gate 	 */
3390Sstevel@tonic-gate 	tte.tte_inthi = TTE_VALID_INT | TTE_PFN_INTHI(pp->p_pagenum);
3400Sstevel@tonic-gate 	tte.tte_intlo = TTE_PFN_INTLO(pp->p_pagenum) | TTE_CP_INT |
3410Sstevel@tonic-gate 	    TTE_CV_INT | TTE_PRIV_INT | TTE_LCK_INT | prot;
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	ASSERT(CPU->cpu_id == cpu);
3442241Shuah 	sfmmu_dtlb_ld_kva(va, &tte);
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	*pslot = myslot;	/* Return ptr to the slot we used. */
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	return (va);
3490Sstevel@tonic-gate }
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate static void
pp_unload_tlb(caddr_t * pslot,caddr_t va)3520Sstevel@tonic-gate pp_unload_tlb(caddr_t *pslot, caddr_t va)
3530Sstevel@tonic-gate {
3540Sstevel@tonic-gate 	ASSERT(*pslot == va);
3550Sstevel@tonic-gate 
3562241Shuah 	vtag_flushpage(va, (uint64_t)ksfmmup);
3570Sstevel@tonic-gate 	*pslot = NULL;				/* release the slot */
3580Sstevel@tonic-gate }
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate /*
3610Sstevel@tonic-gate  * Common copy routine which attempts to use hwblkpagecopy.  If this routine
3620Sstevel@tonic-gate  * can't be used, failure (0) will be returned.  Otherwise, a PAGESIZE page
3630Sstevel@tonic-gate  * will be copied and success (1) will be returned.
3640Sstevel@tonic-gate  */
3650Sstevel@tonic-gate int
ppcopy_common(page_t * fm_pp,page_t * to_pp)3660Sstevel@tonic-gate ppcopy_common(page_t *fm_pp, page_t *to_pp)
3670Sstevel@tonic-gate {
3680Sstevel@tonic-gate 	caddr_t fm_va, to_va;
3690Sstevel@tonic-gate 	caddr_t	*fm_slot, *to_slot;
3700Sstevel@tonic-gate 	processorid_t cpu;
3713253Smec 	label_t ljb;
3723253Smec 	int ret = 1;
3730Sstevel@tonic-gate 
374*6955Sbpramod 	ASSERT(fm_pp != NULL && PAGE_LOCKED(fm_pp));
375*6955Sbpramod 	ASSERT(to_pp != NULL && PAGE_LOCKED(to_pp));
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	/*
3780Sstevel@tonic-gate 	 * If we can't use VIS block loads and stores we can't use
3790Sstevel@tonic-gate 	 * pp_load_tlb/pp_unload_tlb due to the possibility of
3800Sstevel@tonic-gate 	 * d$ aliasing.
3810Sstevel@tonic-gate 	 */
3820Sstevel@tonic-gate 	if (!use_hw_bcopy && (cache & CACHE_VAC))
3830Sstevel@tonic-gate 		return (0);
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 	kpreempt_disable();
3860Sstevel@tonic-gate 	cpu = CPU->cpu_id;
3870Sstevel@tonic-gate 	fm_va = pp_load_tlb(cpu, &fm_slot, fm_pp, 0);
3880Sstevel@tonic-gate 	if (fm_va == NULL) {
3890Sstevel@tonic-gate 		kpreempt_enable();
3900Sstevel@tonic-gate 		return (0);
3910Sstevel@tonic-gate 	}
3920Sstevel@tonic-gate 	to_va = pp_load_tlb(cpu, &to_slot, to_pp, TTE_HWWR_INT);
3930Sstevel@tonic-gate 	if (to_va == NULL) {
3940Sstevel@tonic-gate 		pp_unload_tlb(fm_slot, fm_va);
3950Sstevel@tonic-gate 		kpreempt_enable();
3960Sstevel@tonic-gate 		return (0);
3970Sstevel@tonic-gate 	}
3983253Smec 	if (on_fault(&ljb)) {
3993253Smec 		ret = 0;
4003253Smec 		goto faulted;
4013253Smec 	}
4020Sstevel@tonic-gate 	hwblkpagecopy(fm_va, to_va);
4033253Smec 	no_fault();
4043253Smec faulted:
4050Sstevel@tonic-gate 	ASSERT(CPU->cpu_id == cpu);
4060Sstevel@tonic-gate 	pp_unload_tlb(fm_slot, fm_va);
4070Sstevel@tonic-gate 	pp_unload_tlb(to_slot, to_va);
4080Sstevel@tonic-gate 	kpreempt_enable();
4093253Smec 	return (ret);
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate /*
4130Sstevel@tonic-gate  * Routine to copy kernel pages during relocation.  It will copy one
4140Sstevel@tonic-gate  * PAGESIZE page to another PAGESIZE page.  This function may be called
4150Sstevel@tonic-gate  * above LOCK_LEVEL so it should not grab any locks.
4160Sstevel@tonic-gate  */
4170Sstevel@tonic-gate void
ppcopy_kernel__relocatable(page_t * fm_pp,page_t * to_pp)4180Sstevel@tonic-gate ppcopy_kernel__relocatable(page_t *fm_pp, page_t *to_pp)
4190Sstevel@tonic-gate {
4200Sstevel@tonic-gate 	uint64_t fm_pa, to_pa;
4210Sstevel@tonic-gate 	size_t nbytes;
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	fm_pa = (uint64_t)(fm_pp->p_pagenum) << MMU_PAGESHIFT;
4240Sstevel@tonic-gate 	to_pa = (uint64_t)(to_pp->p_pagenum) << MMU_PAGESHIFT;
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	nbytes = MMU_PAGESIZE;
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	for (; nbytes > 0; fm_pa += 32, to_pa += 32, nbytes -= 32)
4290Sstevel@tonic-gate 		hw_pa_bcopy32(fm_pa, to_pa);
4300Sstevel@tonic-gate }
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate /*
4330Sstevel@tonic-gate  * Copy the data from the physical page represented by "frompp" to
4340Sstevel@tonic-gate  * that represented by "topp".
4350Sstevel@tonic-gate  *
4360Sstevel@tonic-gate  * Try to use per cpu mapping first, if that fails then call pp_mapin
4370Sstevel@tonic-gate  * to load it.
4383253Smec  *
4393253Smec  * Returns one on success or zero on some sort of fault while doing the copy.
4400Sstevel@tonic-gate  */
4413253Smec int
ppcopy(page_t * fm_pp,page_t * to_pp)4420Sstevel@tonic-gate ppcopy(page_t *fm_pp, page_t *to_pp)
4430Sstevel@tonic-gate {
4440Sstevel@tonic-gate 	caddr_t fm_va, to_va;
4453253Smec 	label_t ljb;
4463253Smec 	int ret = 1;
447*6955Sbpramod 	boolean_t	use_kpm = B_FALSE;
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	/* Try the fast path first */
4500Sstevel@tonic-gate 	if (ppcopy_common(fm_pp, to_pp))
4513253Smec 		return (1);
4520Sstevel@tonic-gate 
453*6955Sbpramod 	/*
454*6955Sbpramod 	 * Try to map using KPM if enabled and we are the cageout thread.
455*6955Sbpramod 	 * If it fails, fall back to ppmapin/ppmaput
456*6955Sbpramod 	 */
457*6955Sbpramod 
458*6955Sbpramod 	if (kpm_enable) {
459*6955Sbpramod 		if (curthread == kcage_cageout_thread)
460*6955Sbpramod 			use_kpm = B_TRUE;
461*6955Sbpramod 	}
462*6955Sbpramod 
463*6955Sbpramod 	if (use_kpm) {
464*6955Sbpramod 		if ((fm_va = hat_kpm_mapin(fm_pp, NULL)) == NULL ||
465*6955Sbpramod 		    (to_va = hat_kpm_mapin(to_pp, NULL)) == NULL) {
466*6955Sbpramod 			if (fm_va != NULL)
467*6955Sbpramod 				hat_kpm_mapout(fm_pp, NULL, fm_va);
468*6955Sbpramod 			use_kpm = B_FALSE;
469*6955Sbpramod 		}
470*6955Sbpramod 	}
471*6955Sbpramod 
472*6955Sbpramod 	if (use_kpm == B_FALSE) {
473*6955Sbpramod 		/* do the slow path */
474*6955Sbpramod 		fm_va = ppmapin(fm_pp, PROT_READ, (caddr_t)-1);
475*6955Sbpramod 		to_va = ppmapin(to_pp, PROT_READ | PROT_WRITE, fm_va);
476*6955Sbpramod 		if (on_fault(&ljb)) {
477*6955Sbpramod 			ret = 0;
478*6955Sbpramod 			goto faulted;
479*6955Sbpramod 		}
4803253Smec 	}
4810Sstevel@tonic-gate 	bcopy(fm_va, to_va, PAGESIZE);
4823253Smec 	no_fault();
4833253Smec faulted:
484*6955Sbpramod 	/* unmap */
485*6955Sbpramod 	if (use_kpm == B_TRUE) {
486*6955Sbpramod 		hat_kpm_mapout(fm_pp, NULL, fm_va);
487*6955Sbpramod 		hat_kpm_mapout(to_pp, NULL, to_va);
488*6955Sbpramod 	} else {
489*6955Sbpramod 		ppmapout(fm_va);
490*6955Sbpramod 		ppmapout(to_va);
491*6955Sbpramod 	}
4923253Smec 	return (ret);
4930Sstevel@tonic-gate }
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate /*
4960Sstevel@tonic-gate  * Zero the physical page from off to off + len given by `pp'
4970Sstevel@tonic-gate  * without changing the reference and modified bits of page.
4980Sstevel@tonic-gate  *
4990Sstevel@tonic-gate  * Again, we'll try per cpu mapping first.
5000Sstevel@tonic-gate  */
5010Sstevel@tonic-gate void
pagezero(page_t * pp,uint_t off,uint_t len)5020Sstevel@tonic-gate pagezero(page_t *pp, uint_t off, uint_t len)
5030Sstevel@tonic-gate {
5040Sstevel@tonic-gate 	caddr_t va;
5050Sstevel@tonic-gate 	caddr_t *slot;
5060Sstevel@tonic-gate 	int fast = 1;
5070Sstevel@tonic-gate 	processorid_t cpu;
5080Sstevel@tonic-gate 	extern int hwblkclr(void *, size_t);
5090Sstevel@tonic-gate 	extern int use_hw_bzero;
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	ASSERT((int)len > 0 && (int)off >= 0 && off + len <= PAGESIZE);
5120Sstevel@tonic-gate 	ASSERT(PAGE_LOCKED(pp));
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 	PP_STAT_ADD(ppzero);
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	if (len != MMU_PAGESIZE || !use_hw_bzero) {
5170Sstevel@tonic-gate 		/*
5180Sstevel@tonic-gate 		 * Since the fast path doesn't do anything about
5190Sstevel@tonic-gate 		 * VAC coloring, we make sure bcopy h/w will be used.
5200Sstevel@tonic-gate 		 */
5210Sstevel@tonic-gate 		fast = 0;
5220Sstevel@tonic-gate 		va = NULL;
5230Sstevel@tonic-gate 		PP_STAT_ADD(ppzero_short);
5240Sstevel@tonic-gate 	}
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	kpreempt_disable();
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	if (fast) {
5290Sstevel@tonic-gate 		cpu = CPU->cpu_id;
5300Sstevel@tonic-gate 		va = pp_load_tlb(cpu, &slot, pp, TTE_HWWR_INT);
5310Sstevel@tonic-gate 	}
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	if (va == NULL) {
5340Sstevel@tonic-gate 		/*
5350Sstevel@tonic-gate 		 * We are here either length != MMU_PAGESIZE or pp_load_tlb()
5360Sstevel@tonic-gate 		 * returns NULL or use_hw_bzero is disabled.
5370Sstevel@tonic-gate 		 */
5380Sstevel@tonic-gate 		va = ppmapin(pp, PROT_READ | PROT_WRITE, (caddr_t)-1);
5390Sstevel@tonic-gate 		fast = 0;
5400Sstevel@tonic-gate 	}
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	if (hwblkclr(va + off, len)) {
5430Sstevel@tonic-gate 		/*
5440Sstevel@tonic-gate 		 * We may not have used block commit asi.
5450Sstevel@tonic-gate 		 * So flush the I-$ manually
5460Sstevel@tonic-gate 		 */
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 		ASSERT(fast == 0);
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 		sync_icache(va + off, len);
5510Sstevel@tonic-gate 	} else {
5520Sstevel@tonic-gate 		/*
5530Sstevel@tonic-gate 		 * We have used blk commit, and flushed the I-$. However we
5540Sstevel@tonic-gate 		 * still may have an instruction in the pipeline. Only a flush
5550Sstevel@tonic-gate 		 * instruction will invalidate that.
5560Sstevel@tonic-gate 		 */
5570Sstevel@tonic-gate 		doflush(va);
5580Sstevel@tonic-gate 	}
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	if (fast) {
5610Sstevel@tonic-gate 		ASSERT(CPU->cpu_id == cpu);
5620Sstevel@tonic-gate 		pp_unload_tlb(slot, va);
5630Sstevel@tonic-gate 	} else {
5640Sstevel@tonic-gate 		ppmapout(va);
5650Sstevel@tonic-gate 	}
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	kpreempt_enable();
5680Sstevel@tonic-gate }
569