xref: /illumos-gate/usr/src/uts/i86pc/vm/vm_machdep.c (revision aa042c4b4d612c2b8e5c5107863fff3165da3659)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*aa042c4bSkchow  * Common Development and Distribution License (the "License").
6*aa042c4bSkchow  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22e21bae1bSkchow  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
277c478bd9Sstevel@tonic-gate /*	All Rights Reserved   */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
317c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate  * UNIX machine dependent virtual memory support.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include <sys/types.h>
417c478bd9Sstevel@tonic-gate #include <sys/param.h>
427c478bd9Sstevel@tonic-gate #include <sys/systm.h>
437c478bd9Sstevel@tonic-gate #include <sys/user.h>
447c478bd9Sstevel@tonic-gate #include <sys/proc.h>
457c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
467c478bd9Sstevel@tonic-gate #include <sys/vmem.h>
477c478bd9Sstevel@tonic-gate #include <sys/buf.h>
487c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
497c478bd9Sstevel@tonic-gate #include <sys/lgrp.h>
507c478bd9Sstevel@tonic-gate #include <sys/disp.h>
517c478bd9Sstevel@tonic-gate #include <sys/vm.h>
527c478bd9Sstevel@tonic-gate #include <sys/mman.h>
537c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
547c478bd9Sstevel@tonic-gate #include <sys/cred.h>
557c478bd9Sstevel@tonic-gate #include <sys/exec.h>
567c478bd9Sstevel@tonic-gate #include <sys/exechdr.h>
577c478bd9Sstevel@tonic-gate #include <sys/debug.h>
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #include <vm/hat.h>
607c478bd9Sstevel@tonic-gate #include <vm/as.h>
617c478bd9Sstevel@tonic-gate #include <vm/seg.h>
627c478bd9Sstevel@tonic-gate #include <vm/seg_kp.h>
637c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h>
647c478bd9Sstevel@tonic-gate #include <vm/page.h>
657c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
667c478bd9Sstevel@tonic-gate #include <vm/seg_kpm.h>
677c478bd9Sstevel@tonic-gate #include <vm/vm_dep.h>
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #include <sys/cpu.h>
707c478bd9Sstevel@tonic-gate #include <sys/vm_machparam.h>
717c478bd9Sstevel@tonic-gate #include <sys/memlist.h>
727c478bd9Sstevel@tonic-gate #include <sys/bootconf.h> /* XXX the memlist stuff belongs in memlist_plat.h */
737c478bd9Sstevel@tonic-gate #include <vm/hat_i86.h>
747c478bd9Sstevel@tonic-gate #include <sys/x86_archext.h>
757c478bd9Sstevel@tonic-gate #include <sys/elf_386.h>
767c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
777c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
787c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
817c478bd9Sstevel@tonic-gate #include <sys/ddidmareq.h>
827c478bd9Sstevel@tonic-gate #include <sys/promif.h>
837c478bd9Sstevel@tonic-gate #include <sys/memnode.h>
847c478bd9Sstevel@tonic-gate #include <sys/stack.h>
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate uint_t vac_colors = 0;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate int largepagesupport = 0;
897c478bd9Sstevel@tonic-gate extern uint_t page_create_new;
907c478bd9Sstevel@tonic-gate extern uint_t page_create_exists;
917c478bd9Sstevel@tonic-gate extern uint_t page_create_putbacks;
927c478bd9Sstevel@tonic-gate extern uint_t page_create_putbacks;
937c478bd9Sstevel@tonic-gate extern uintptr_t eprom_kernelbase;
947c478bd9Sstevel@tonic-gate extern int use_sse_pagecopy, use_sse_pagezero;	/* in ml/float.s */
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /* 4g memory management */
977c478bd9Sstevel@tonic-gate pgcnt_t		maxmem4g;
987c478bd9Sstevel@tonic-gate pgcnt_t		freemem4g;
997c478bd9Sstevel@tonic-gate int		physmax4g;
1007c478bd9Sstevel@tonic-gate int		desfree4gshift = 4;	/* maxmem4g shift to derive DESFREE4G */
1017c478bd9Sstevel@tonic-gate int		lotsfree4gshift = 3;
1027c478bd9Sstevel@tonic-gate 
10307ad560dSkchow /* 16m memory management: desired number of free pages below 16m. */
10407ad560dSkchow pgcnt_t		desfree16m = 0x380;
10507ad560dSkchow 
1067c478bd9Sstevel@tonic-gate #ifdef VM_STATS
1077c478bd9Sstevel@tonic-gate struct {
1087c478bd9Sstevel@tonic-gate 	ulong_t	pga_alloc;
1097c478bd9Sstevel@tonic-gate 	ulong_t	pga_notfullrange;
1107c478bd9Sstevel@tonic-gate 	ulong_t	pga_nulldmaattr;
1117c478bd9Sstevel@tonic-gate 	ulong_t	pga_allocok;
1127c478bd9Sstevel@tonic-gate 	ulong_t	pga_allocfailed;
1137c478bd9Sstevel@tonic-gate 	ulong_t	pgma_alloc;
1147c478bd9Sstevel@tonic-gate 	ulong_t	pgma_allocok;
1157c478bd9Sstevel@tonic-gate 	ulong_t	pgma_allocfailed;
1167c478bd9Sstevel@tonic-gate 	ulong_t	pgma_allocempty;
1177c478bd9Sstevel@tonic-gate } pga_vmstats;
1187c478bd9Sstevel@tonic-gate #endif
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate uint_t mmu_page_sizes;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate /* How many page sizes the users can see */
1237c478bd9Sstevel@tonic-gate uint_t mmu_exported_page_sizes;
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate size_t auto_lpg_va_default = MMU_PAGESIZE; /* used by zmap() */
126beb1bda0Sdavemq /*
127beb1bda0Sdavemq  * Number of pages in 1 GB.  Don't enable automatic large pages if we have
128beb1bda0Sdavemq  * fewer than this many pages.
129beb1bda0Sdavemq  */
130beb1bda0Sdavemq pgcnt_t auto_lpg_min_physmem = 1 << (30 - MMU_PAGESHIFT);
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate /*
1337c478bd9Sstevel@tonic-gate  * Return the optimum page size for a given mapping
1347c478bd9Sstevel@tonic-gate  */
1357c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1367c478bd9Sstevel@tonic-gate size_t
1377c478bd9Sstevel@tonic-gate map_pgsz(int maptype, struct proc *p, caddr_t addr, size_t len, int *remap)
1387c478bd9Sstevel@tonic-gate {
1397c478bd9Sstevel@tonic-gate 	level_t l;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	if (remap)
1427c478bd9Sstevel@tonic-gate 		*remap = 0;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	switch (maptype) {
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	case MAPPGSZ_STK:
1477c478bd9Sstevel@tonic-gate 	case MAPPGSZ_HEAP:
1487c478bd9Sstevel@tonic-gate 	case MAPPGSZ_VA:
1497c478bd9Sstevel@tonic-gate 		/*
1507c478bd9Sstevel@tonic-gate 		 * use the pages size that best fits len
1517c478bd9Sstevel@tonic-gate 		 */
1527c478bd9Sstevel@tonic-gate 		for (l = mmu.max_page_level; l > 0; --l) {
1537c478bd9Sstevel@tonic-gate 			if (len < LEVEL_SIZE(l))
1547c478bd9Sstevel@tonic-gate 				continue;
1557c478bd9Sstevel@tonic-gate 			break;
1567c478bd9Sstevel@tonic-gate 		}
1577c478bd9Sstevel@tonic-gate 		return (LEVEL_SIZE(l));
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	/*
1607c478bd9Sstevel@tonic-gate 	 * for ISM use the 1st large page size.
1617c478bd9Sstevel@tonic-gate 	 */
1627c478bd9Sstevel@tonic-gate 	case MAPPGSZ_ISM:
1637c478bd9Sstevel@tonic-gate 		if (mmu.max_page_level == 0)
1647c478bd9Sstevel@tonic-gate 			return (MMU_PAGESIZE);
1657c478bd9Sstevel@tonic-gate 		return (LEVEL_SIZE(1));
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 	return (0);
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate /*
1717c478bd9Sstevel@tonic-gate  * This can be patched via /etc/system to allow large pages
1727c478bd9Sstevel@tonic-gate  * to be used for mapping application and libraries text segments.
1737c478bd9Sstevel@tonic-gate  */
1747c478bd9Sstevel@tonic-gate int	use_text_largepages = 0;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate /*
1777c478bd9Sstevel@tonic-gate  * Return a bit vector of large page size codes that
1787c478bd9Sstevel@tonic-gate  * can be used to map [addr, addr + len) region.
1797c478bd9Sstevel@tonic-gate  */
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1827c478bd9Sstevel@tonic-gate uint_t
1837c478bd9Sstevel@tonic-gate map_execseg_pgszcvec(int text, caddr_t addr, size_t len)
1847c478bd9Sstevel@tonic-gate {
1857c478bd9Sstevel@tonic-gate 	size_t	pgsz;
1867c478bd9Sstevel@tonic-gate 	caddr_t a;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	if (!text || !use_text_largepages ||
1897c478bd9Sstevel@tonic-gate 	    mmu.max_page_level == 0)
1907c478bd9Sstevel@tonic-gate 		return (0);
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	pgsz = LEVEL_SIZE(1);
1937c478bd9Sstevel@tonic-gate 	a = (caddr_t)P2ROUNDUP((uintptr_t)addr, pgsz);
1947c478bd9Sstevel@tonic-gate 	if (a < addr || a >= addr + len) {
1957c478bd9Sstevel@tonic-gate 		return (0);
1967c478bd9Sstevel@tonic-gate 	}
1977c478bd9Sstevel@tonic-gate 	len -= (a - addr);
1987c478bd9Sstevel@tonic-gate 	if (len < pgsz) {
1997c478bd9Sstevel@tonic-gate 		return (0);
2007c478bd9Sstevel@tonic-gate 	}
2017c478bd9Sstevel@tonic-gate 	return (1 << 1);
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate /*
2057c478bd9Sstevel@tonic-gate  * Handle a pagefault.
2067c478bd9Sstevel@tonic-gate  */
2077c478bd9Sstevel@tonic-gate faultcode_t
2087c478bd9Sstevel@tonic-gate pagefault(
2097c478bd9Sstevel@tonic-gate 	caddr_t addr,
2107c478bd9Sstevel@tonic-gate 	enum fault_type type,
2117c478bd9Sstevel@tonic-gate 	enum seg_rw rw,
2127c478bd9Sstevel@tonic-gate 	int iskernel)
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate 	struct as *as;
2157c478bd9Sstevel@tonic-gate 	struct hat *hat;
2167c478bd9Sstevel@tonic-gate 	struct proc *p;
2177c478bd9Sstevel@tonic-gate 	kthread_t *t;
2187c478bd9Sstevel@tonic-gate 	faultcode_t res;
2197c478bd9Sstevel@tonic-gate 	caddr_t base;
2207c478bd9Sstevel@tonic-gate 	size_t len;
2217c478bd9Sstevel@tonic-gate 	int err;
2227c478bd9Sstevel@tonic-gate 	int mapped_red;
2237c478bd9Sstevel@tonic-gate 	uintptr_t ea;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	ASSERT_STACK_ALIGNED();
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	if (INVALID_VADDR(addr))
2287c478bd9Sstevel@tonic-gate 		return (FC_NOMAP);
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	mapped_red = segkp_map_red();
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	if (iskernel) {
2337c478bd9Sstevel@tonic-gate 		as = &kas;
2347c478bd9Sstevel@tonic-gate 		hat = as->a_hat;
2357c478bd9Sstevel@tonic-gate 	} else {
2367c478bd9Sstevel@tonic-gate 		t = curthread;
2377c478bd9Sstevel@tonic-gate 		p = ttoproc(t);
2387c478bd9Sstevel@tonic-gate 		as = p->p_as;
2397c478bd9Sstevel@tonic-gate 		hat = as->a_hat;
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	/*
2437c478bd9Sstevel@tonic-gate 	 * Dispatch pagefault.
2447c478bd9Sstevel@tonic-gate 	 */
2457c478bd9Sstevel@tonic-gate 	res = as_fault(hat, as, addr, 1, type, rw);
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	/*
2487c478bd9Sstevel@tonic-gate 	 * If this isn't a potential unmapped hole in the user's
2497c478bd9Sstevel@tonic-gate 	 * UNIX data or stack segments, just return status info.
2507c478bd9Sstevel@tonic-gate 	 */
2517c478bd9Sstevel@tonic-gate 	if (res != FC_NOMAP || iskernel)
2527c478bd9Sstevel@tonic-gate 		goto out;
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	/*
2557c478bd9Sstevel@tonic-gate 	 * Check to see if we happened to faulted on a currently unmapped
2567c478bd9Sstevel@tonic-gate 	 * part of the UNIX data or stack segments.  If so, create a zfod
2577c478bd9Sstevel@tonic-gate 	 * mapping there and then try calling the fault routine again.
2587c478bd9Sstevel@tonic-gate 	 */
2597c478bd9Sstevel@tonic-gate 	base = p->p_brkbase;
2607c478bd9Sstevel@tonic-gate 	len = p->p_brksize;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	if (addr < base || addr >= base + len) {		/* data seg? */
2637c478bd9Sstevel@tonic-gate 		base = (caddr_t)p->p_usrstack - p->p_stksize;
2647c478bd9Sstevel@tonic-gate 		len = p->p_stksize;
2657c478bd9Sstevel@tonic-gate 		if (addr < base || addr >= p->p_usrstack) {	/* stack seg? */
2667c478bd9Sstevel@tonic-gate 			/* not in either UNIX data or stack segments */
2677c478bd9Sstevel@tonic-gate 			res = FC_NOMAP;
2687c478bd9Sstevel@tonic-gate 			goto out;
2697c478bd9Sstevel@tonic-gate 		}
2707c478bd9Sstevel@tonic-gate 	}
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	/*
2737c478bd9Sstevel@tonic-gate 	 * the rest of this function implements a 3.X 4.X 5.X compatibility
2747c478bd9Sstevel@tonic-gate 	 * This code is probably not needed anymore
2757c478bd9Sstevel@tonic-gate 	 */
2767c478bd9Sstevel@tonic-gate 	if (p->p_model == DATAMODEL_ILP32) {
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 		/* expand the gap to the page boundaries on each side */
2797c478bd9Sstevel@tonic-gate 		ea = P2ROUNDUP((uintptr_t)base + len, MMU_PAGESIZE);
2807c478bd9Sstevel@tonic-gate 		base = (caddr_t)P2ALIGN((uintptr_t)base, MMU_PAGESIZE);
2817c478bd9Sstevel@tonic-gate 		len = ea - (uintptr_t)base;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 		as_rangelock(as);
2847c478bd9Sstevel@tonic-gate 		if (as_gap(as, MMU_PAGESIZE, &base, &len, AH_CONTAIN, addr) ==
2857c478bd9Sstevel@tonic-gate 		    0) {
2867c478bd9Sstevel@tonic-gate 			err = as_map(as, base, len, segvn_create, zfod_argsp);
2877c478bd9Sstevel@tonic-gate 			as_rangeunlock(as);
2887c478bd9Sstevel@tonic-gate 			if (err) {
2897c478bd9Sstevel@tonic-gate 				res = FC_MAKE_ERR(err);
2907c478bd9Sstevel@tonic-gate 				goto out;
2917c478bd9Sstevel@tonic-gate 			}
2927c478bd9Sstevel@tonic-gate 		} else {
2937c478bd9Sstevel@tonic-gate 			/*
2947c478bd9Sstevel@tonic-gate 			 * This page is already mapped by another thread after
2957c478bd9Sstevel@tonic-gate 			 * we returned from as_fault() above.  We just fall
2967c478bd9Sstevel@tonic-gate 			 * through as_fault() below.
2977c478bd9Sstevel@tonic-gate 			 */
2987c478bd9Sstevel@tonic-gate 			as_rangeunlock(as);
2997c478bd9Sstevel@tonic-gate 		}
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 		res = as_fault(hat, as, addr, 1, F_INVAL, rw);
3027c478bd9Sstevel@tonic-gate 	}
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate out:
3057c478bd9Sstevel@tonic-gate 	if (mapped_red)
3067c478bd9Sstevel@tonic-gate 		segkp_unmap_red();
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	return (res);
3097c478bd9Sstevel@tonic-gate }
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate void
3127c478bd9Sstevel@tonic-gate map_addr(caddr_t *addrp, size_t len, offset_t off, int vacalign, uint_t flags)
3137c478bd9Sstevel@tonic-gate {
3147c478bd9Sstevel@tonic-gate 	struct proc *p = curproc;
3157c478bd9Sstevel@tonic-gate 	caddr_t userlimit = (flags & _MAP_LOW32) ?
3167c478bd9Sstevel@tonic-gate 	    (caddr_t)_userlimit32 : p->p_as->a_userlimit;
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	map_addr_proc(addrp, len, off, vacalign, userlimit, curproc, flags);
3197c478bd9Sstevel@tonic-gate }
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3227c478bd9Sstevel@tonic-gate int
3237c478bd9Sstevel@tonic-gate map_addr_vacalign_check(caddr_t addr, u_offset_t off)
3247c478bd9Sstevel@tonic-gate {
3257c478bd9Sstevel@tonic-gate 	return (0);
3267c478bd9Sstevel@tonic-gate }
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate /*
3297c478bd9Sstevel@tonic-gate  * map_addr_proc() is the routine called when the system is to
3307c478bd9Sstevel@tonic-gate  * choose an address for the user.  We will pick an address
3317c478bd9Sstevel@tonic-gate  * range which is the highest available below kernelbase.
3327c478bd9Sstevel@tonic-gate  *
3337c478bd9Sstevel@tonic-gate  * addrp is a value/result parameter.
3347c478bd9Sstevel@tonic-gate  *	On input it is a hint from the user to be used in a completely
3357c478bd9Sstevel@tonic-gate  *	machine dependent fashion.  We decide to completely ignore this hint.
3367c478bd9Sstevel@tonic-gate  *
3377c478bd9Sstevel@tonic-gate  *	On output it is NULL if no address can be found in the current
3387c478bd9Sstevel@tonic-gate  *	processes address space or else an address that is currently
3397c478bd9Sstevel@tonic-gate  *	not mapped for len bytes with a page of red zone on either side.
3407c478bd9Sstevel@tonic-gate  *
3417c478bd9Sstevel@tonic-gate  *	align is not needed on x86 (it's for viturally addressed caches)
3427c478bd9Sstevel@tonic-gate  */
3437c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3447c478bd9Sstevel@tonic-gate void
3457c478bd9Sstevel@tonic-gate map_addr_proc(
3467c478bd9Sstevel@tonic-gate 	caddr_t *addrp,
3477c478bd9Sstevel@tonic-gate 	size_t len,
3487c478bd9Sstevel@tonic-gate 	offset_t off,
3497c478bd9Sstevel@tonic-gate 	int vacalign,
3507c478bd9Sstevel@tonic-gate 	caddr_t userlimit,
3517c478bd9Sstevel@tonic-gate 	struct proc *p,
3527c478bd9Sstevel@tonic-gate 	uint_t flags)
3537c478bd9Sstevel@tonic-gate {
3547c478bd9Sstevel@tonic-gate 	struct as *as = p->p_as;
3557c478bd9Sstevel@tonic-gate 	caddr_t addr;
3567c478bd9Sstevel@tonic-gate 	caddr_t base;
3577c478bd9Sstevel@tonic-gate 	size_t slen;
3587c478bd9Sstevel@tonic-gate 	size_t align_amount;
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	ASSERT32(userlimit == as->a_userlimit);
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	base = p->p_brkbase;
3637c478bd9Sstevel@tonic-gate #if defined(__amd64)
3647c478bd9Sstevel@tonic-gate 	/*
3657c478bd9Sstevel@tonic-gate 	 * XX64 Yes, this needs more work.
3667c478bd9Sstevel@tonic-gate 	 */
3677c478bd9Sstevel@tonic-gate 	if (p->p_model == DATAMODEL_NATIVE) {
3687c478bd9Sstevel@tonic-gate 		if (userlimit < as->a_userlimit) {
3697c478bd9Sstevel@tonic-gate 			/*
3707c478bd9Sstevel@tonic-gate 			 * This happens when a program wants to map
3717c478bd9Sstevel@tonic-gate 			 * something in a range that's accessible to a
3727c478bd9Sstevel@tonic-gate 			 * program in a smaller address space.  For example,
3737c478bd9Sstevel@tonic-gate 			 * a 64-bit program calling mmap32(2) to guarantee
3747c478bd9Sstevel@tonic-gate 			 * that the returned address is below 4Gbytes.
3757c478bd9Sstevel@tonic-gate 			 */
3767c478bd9Sstevel@tonic-gate 			ASSERT((uintptr_t)userlimit < ADDRESS_C(0xffffffff));
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 			if (userlimit > base)
3797c478bd9Sstevel@tonic-gate 				slen = userlimit - base;
3807c478bd9Sstevel@tonic-gate 			else {
3817c478bd9Sstevel@tonic-gate 				*addrp = NULL;
3827c478bd9Sstevel@tonic-gate 				return;
3837c478bd9Sstevel@tonic-gate 			}
3847c478bd9Sstevel@tonic-gate 		} else {
3857c478bd9Sstevel@tonic-gate 			/*
3867c478bd9Sstevel@tonic-gate 			 * XX64 This layout is probably wrong .. but in
3877c478bd9Sstevel@tonic-gate 			 * the event we make the amd64 address space look
3887c478bd9Sstevel@tonic-gate 			 * like sparcv9 i.e. with the stack -above- the
3897c478bd9Sstevel@tonic-gate 			 * heap, this bit of code might even be correct.
3907c478bd9Sstevel@tonic-gate 			 */
3917c478bd9Sstevel@tonic-gate 			slen = p->p_usrstack - base -
3927c478bd9Sstevel@tonic-gate 			    (((size_t)rctl_enforced_value(
3937c478bd9Sstevel@tonic-gate 			    rctlproc_legacy[RLIMIT_STACK],
3947c478bd9Sstevel@tonic-gate 			    p->p_rctls, p) + PAGEOFFSET) & PAGEMASK);
3957c478bd9Sstevel@tonic-gate 		}
3967c478bd9Sstevel@tonic-gate 	} else
3977c478bd9Sstevel@tonic-gate #endif
3987c478bd9Sstevel@tonic-gate 		slen = userlimit - base;
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	len = (len + PAGEOFFSET) & PAGEMASK;
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	/*
4037c478bd9Sstevel@tonic-gate 	 * Redzone for each side of the request. This is done to leave
4047c478bd9Sstevel@tonic-gate 	 * one page unmapped between segments. This is not required, but
4057c478bd9Sstevel@tonic-gate 	 * it's useful for the user because if their program strays across
4067c478bd9Sstevel@tonic-gate 	 * a segment boundary, it will catch a fault immediately making
4077c478bd9Sstevel@tonic-gate 	 * debugging a little easier.
4087c478bd9Sstevel@tonic-gate 	 */
4097c478bd9Sstevel@tonic-gate 	len += 2 * MMU_PAGESIZE;
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	/*
4127c478bd9Sstevel@tonic-gate 	 * figure out what the alignment should be
4137c478bd9Sstevel@tonic-gate 	 *
4147c478bd9Sstevel@tonic-gate 	 * XX64 -- is there an ELF_AMD64_MAXPGSZ or is it the same????
4157c478bd9Sstevel@tonic-gate 	 */
4167c478bd9Sstevel@tonic-gate 	if (len <= ELF_386_MAXPGSZ) {
4177c478bd9Sstevel@tonic-gate 		/*
4187c478bd9Sstevel@tonic-gate 		 * Align virtual addresses to ensure that ELF shared libraries
4197c478bd9Sstevel@tonic-gate 		 * are mapped with the appropriate alignment constraints by
4207c478bd9Sstevel@tonic-gate 		 * the run-time linker.
4217c478bd9Sstevel@tonic-gate 		 */
4227c478bd9Sstevel@tonic-gate 		align_amount = ELF_386_MAXPGSZ;
4237c478bd9Sstevel@tonic-gate 	} else {
4247c478bd9Sstevel@tonic-gate 		int l = mmu.max_page_level;
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 		while (l && len < LEVEL_SIZE(l))
4277c478bd9Sstevel@tonic-gate 			--l;
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 		align_amount = LEVEL_SIZE(l);
4307c478bd9Sstevel@tonic-gate 	}
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	if ((flags & MAP_ALIGN) && ((uintptr_t)*addrp > align_amount))
4337c478bd9Sstevel@tonic-gate 		align_amount = (uintptr_t)*addrp;
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	len += align_amount;
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	/*
4387c478bd9Sstevel@tonic-gate 	 * Look for a large enough hole starting below userlimit.
4397c478bd9Sstevel@tonic-gate 	 * After finding it, use the upper part.  Addition of PAGESIZE
4407c478bd9Sstevel@tonic-gate 	 * is for the redzone as described above.
4417c478bd9Sstevel@tonic-gate 	 */
4427c478bd9Sstevel@tonic-gate 	if (as_gap(as, len, &base, &slen, AH_HI, NULL) == 0) {
4437c478bd9Sstevel@tonic-gate 		caddr_t as_addr;
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 		addr = base + slen - len + MMU_PAGESIZE;
4467c478bd9Sstevel@tonic-gate 		as_addr = addr;
4477c478bd9Sstevel@tonic-gate 		/*
4487c478bd9Sstevel@tonic-gate 		 * Round address DOWN to the alignment amount,
4497c478bd9Sstevel@tonic-gate 		 * add the offset, and if this address is less
4507c478bd9Sstevel@tonic-gate 		 * than the original address, add alignment amount.
4517c478bd9Sstevel@tonic-gate 		 */
4527c478bd9Sstevel@tonic-gate 		addr = (caddr_t)((uintptr_t)addr & (~(align_amount - 1)));
4537c478bd9Sstevel@tonic-gate 		addr += (uintptr_t)(off & (align_amount - 1));
4547c478bd9Sstevel@tonic-gate 		if (addr < as_addr)
4557c478bd9Sstevel@tonic-gate 			addr += align_amount;
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 		ASSERT(addr <= (as_addr + align_amount));
4587c478bd9Sstevel@tonic-gate 		ASSERT(((uintptr_t)addr & (align_amount - 1)) ==
4597c478bd9Sstevel@tonic-gate 		    ((uintptr_t)(off & (align_amount - 1))));
4607c478bd9Sstevel@tonic-gate 		*addrp = addr;
4617c478bd9Sstevel@tonic-gate 	} else {
4627c478bd9Sstevel@tonic-gate 		*addrp = NULL;	/* no more virtual space */
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate }
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate /*
4677c478bd9Sstevel@tonic-gate  * Determine whether [base, base+len] contains a valid range of
4687c478bd9Sstevel@tonic-gate  * addresses at least minlen long. base and len are adjusted if
4697c478bd9Sstevel@tonic-gate  * required to provide a valid range.
4707c478bd9Sstevel@tonic-gate  */
4717c478bd9Sstevel@tonic-gate /*ARGSUSED3*/
4727c478bd9Sstevel@tonic-gate int
4737c478bd9Sstevel@tonic-gate valid_va_range(caddr_t *basep, size_t *lenp, size_t minlen, int dir)
4747c478bd9Sstevel@tonic-gate {
4757c478bd9Sstevel@tonic-gate 	uintptr_t hi, lo;
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	lo = (uintptr_t)*basep;
4787c478bd9Sstevel@tonic-gate 	hi = lo + *lenp;
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	/*
4817c478bd9Sstevel@tonic-gate 	 * If hi rolled over the top, try cutting back.
4827c478bd9Sstevel@tonic-gate 	 */
4837c478bd9Sstevel@tonic-gate 	if (hi < lo) {
4847c478bd9Sstevel@tonic-gate 		if (0 - lo + hi < minlen)
4857c478bd9Sstevel@tonic-gate 			return (0);
4867c478bd9Sstevel@tonic-gate 		if (0 - lo < minlen)
4877c478bd9Sstevel@tonic-gate 			return (0);
4887c478bd9Sstevel@tonic-gate 		*lenp = 0 - lo;
4897c478bd9Sstevel@tonic-gate 	} else if (hi - lo < minlen) {
4907c478bd9Sstevel@tonic-gate 		return (0);
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate #if defined(__amd64)
4937c478bd9Sstevel@tonic-gate 	/*
4947c478bd9Sstevel@tonic-gate 	 * Deal with a possible hole in the address range between
4957c478bd9Sstevel@tonic-gate 	 * hole_start and hole_end that should never be mapped.
4967c478bd9Sstevel@tonic-gate 	 */
4977c478bd9Sstevel@tonic-gate 	if (lo < hole_start) {
4987c478bd9Sstevel@tonic-gate 		if (hi > hole_start) {
4997c478bd9Sstevel@tonic-gate 			if (hi < hole_end) {
5007c478bd9Sstevel@tonic-gate 				hi = hole_start;
5017c478bd9Sstevel@tonic-gate 			} else {
5027c478bd9Sstevel@tonic-gate 				/* lo < hole_start && hi >= hole_end */
5037c478bd9Sstevel@tonic-gate 				if (dir == AH_LO) {
5047c478bd9Sstevel@tonic-gate 					/*
5057c478bd9Sstevel@tonic-gate 					 * prefer lowest range
5067c478bd9Sstevel@tonic-gate 					 */
5077c478bd9Sstevel@tonic-gate 					if (hole_start - lo >= minlen)
5087c478bd9Sstevel@tonic-gate 						hi = hole_start;
5097c478bd9Sstevel@tonic-gate 					else if (hi - hole_end >= minlen)
5107c478bd9Sstevel@tonic-gate 						lo = hole_end;
5117c478bd9Sstevel@tonic-gate 					else
5127c478bd9Sstevel@tonic-gate 						return (0);
5137c478bd9Sstevel@tonic-gate 				} else {
5147c478bd9Sstevel@tonic-gate 					/*
5157c478bd9Sstevel@tonic-gate 					 * prefer highest range
5167c478bd9Sstevel@tonic-gate 					 */
5177c478bd9Sstevel@tonic-gate 					if (hi - hole_end >= minlen)
5187c478bd9Sstevel@tonic-gate 						lo = hole_end;
5197c478bd9Sstevel@tonic-gate 					else if (hole_start - lo >= minlen)
5207c478bd9Sstevel@tonic-gate 						hi = hole_start;
5217c478bd9Sstevel@tonic-gate 					else
5227c478bd9Sstevel@tonic-gate 						return (0);
5237c478bd9Sstevel@tonic-gate 				}
5247c478bd9Sstevel@tonic-gate 			}
5257c478bd9Sstevel@tonic-gate 		}
5267c478bd9Sstevel@tonic-gate 	} else {
5277c478bd9Sstevel@tonic-gate 		/* lo >= hole_start */
5287c478bd9Sstevel@tonic-gate 		if (hi < hole_end)
5297c478bd9Sstevel@tonic-gate 			return (0);
5307c478bd9Sstevel@tonic-gate 		if (lo < hole_end)
5317c478bd9Sstevel@tonic-gate 			lo = hole_end;
5327c478bd9Sstevel@tonic-gate 	}
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 	if (hi - lo < minlen)
5357c478bd9Sstevel@tonic-gate 		return (0);
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	*basep = (caddr_t)lo;
5387c478bd9Sstevel@tonic-gate 	*lenp = hi - lo;
5397c478bd9Sstevel@tonic-gate #endif
5407c478bd9Sstevel@tonic-gate 	return (1);
5417c478bd9Sstevel@tonic-gate }
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate /*
5447c478bd9Sstevel@tonic-gate  * Determine whether [addr, addr+len] are valid user addresses.
5457c478bd9Sstevel@tonic-gate  */
5467c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5477c478bd9Sstevel@tonic-gate int
5487c478bd9Sstevel@tonic-gate valid_usr_range(caddr_t addr, size_t len, uint_t prot, struct as *as,
5497c478bd9Sstevel@tonic-gate     caddr_t userlimit)
5507c478bd9Sstevel@tonic-gate {
5517c478bd9Sstevel@tonic-gate 	caddr_t eaddr = addr + len;
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	if (eaddr <= addr || addr >= userlimit || eaddr > userlimit)
5547c478bd9Sstevel@tonic-gate 		return (RANGE_BADADDR);
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate #if defined(__amd64)
5577c478bd9Sstevel@tonic-gate 	/*
5587c478bd9Sstevel@tonic-gate 	 * Check for the VA hole
5597c478bd9Sstevel@tonic-gate 	 */
5607c478bd9Sstevel@tonic-gate 	if (eaddr > (caddr_t)hole_start && addr < (caddr_t)hole_end)
5617c478bd9Sstevel@tonic-gate 		return (RANGE_BADADDR);
5627c478bd9Sstevel@tonic-gate #endif
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	return (RANGE_OKAY);
5657c478bd9Sstevel@tonic-gate }
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate /*
5687c478bd9Sstevel@tonic-gate  * Return 1 if the page frame is onboard memory, else 0.
5697c478bd9Sstevel@tonic-gate  */
5707c478bd9Sstevel@tonic-gate int
5717c478bd9Sstevel@tonic-gate pf_is_memory(pfn_t pf)
5727c478bd9Sstevel@tonic-gate {
5737c478bd9Sstevel@tonic-gate 	return (address_in_memlist(phys_install, mmu_ptob((uint64_t)pf), 1));
5747c478bd9Sstevel@tonic-gate }
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate /*
5787c478bd9Sstevel@tonic-gate  * initialized by page_coloring_init().
5797c478bd9Sstevel@tonic-gate  */
5807c478bd9Sstevel@tonic-gate uint_t	page_colors;
5817c478bd9Sstevel@tonic-gate uint_t	page_colors_mask;
5827c478bd9Sstevel@tonic-gate uint_t	page_coloring_shift;
5837c478bd9Sstevel@tonic-gate int	cpu_page_colors;
5847c478bd9Sstevel@tonic-gate static uint_t	l2_colors;
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate /*
5877c478bd9Sstevel@tonic-gate  * Page freelists and cachelists are dynamically allocated once mnoderangecnt
5887c478bd9Sstevel@tonic-gate  * and page_colors are calculated from the l2 cache n-way set size.  Within a
5897c478bd9Sstevel@tonic-gate  * mnode range, the page freelist and cachelist are hashed into bins based on
5907c478bd9Sstevel@tonic-gate  * color. This makes it easier to search for a page within a specific memory
5917c478bd9Sstevel@tonic-gate  * range.
5927c478bd9Sstevel@tonic-gate  */
5937c478bd9Sstevel@tonic-gate #define	PAGE_COLORS_MIN	16
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate page_t ****page_freelists;
5967c478bd9Sstevel@tonic-gate page_t ***page_cachelists;
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate /*
5997c478bd9Sstevel@tonic-gate  * As the PC architecture evolved memory up was clumped into several
6007c478bd9Sstevel@tonic-gate  * ranges for various historical I/O devices to do DMA.
6017c478bd9Sstevel@tonic-gate  * < 16Meg - ISA bus
6027c478bd9Sstevel@tonic-gate  * < 2Gig - ???
6037c478bd9Sstevel@tonic-gate  * < 4Gig - PCI bus or drivers that don't understand PAE mode
6047c478bd9Sstevel@tonic-gate  */
6057c478bd9Sstevel@tonic-gate static pfn_t arch_memranges[NUM_MEM_RANGES] = {
6067c478bd9Sstevel@tonic-gate     0x100000,	/* pfn range for 4G and above */
6077c478bd9Sstevel@tonic-gate     0x80000,	/* pfn range for 2G-4G */
6087c478bd9Sstevel@tonic-gate     0x01000,	/* pfn range for 16M-2G */
6097c478bd9Sstevel@tonic-gate     0x00000,	/* pfn range for 0-16M */
6107c478bd9Sstevel@tonic-gate };
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate /*
6137c478bd9Sstevel@tonic-gate  * These are changed during startup if the machine has limited memory.
6147c478bd9Sstevel@tonic-gate  */
6157c478bd9Sstevel@tonic-gate pfn_t *memranges = &arch_memranges[0];
6167c478bd9Sstevel@tonic-gate int nranges = NUM_MEM_RANGES;
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate /*
6197c478bd9Sstevel@tonic-gate  * Used by page layer to know about page sizes
6207c478bd9Sstevel@tonic-gate  */
6217c478bd9Sstevel@tonic-gate hw_pagesize_t hw_page_array[MAX_NUM_LEVEL + 1];
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate /*
6247c478bd9Sstevel@tonic-gate  * This can be patched via /etc/system to allow old non-PAE aware device
6257c478bd9Sstevel@tonic-gate  * drivers to use kmem_alloc'd memory on 32 bit systems with > 4Gig RAM.
6267c478bd9Sstevel@tonic-gate  */
6277c478bd9Sstevel@tonic-gate #if defined(__i386)
628*aa042c4bSkchow int restricted_kmemalloc = 0;
6297c478bd9Sstevel@tonic-gate #elif defined(__amd64)
6307c478bd9Sstevel@tonic-gate int restricted_kmemalloc = 0;
6317c478bd9Sstevel@tonic-gate #endif
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate kmutex_t	*fpc_mutex[NPC_MUTEX];
6347c478bd9Sstevel@tonic-gate kmutex_t	*cpc_mutex[NPC_MUTEX];
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate /*
6387c478bd9Sstevel@tonic-gate  * return the memrange containing pfn
6397c478bd9Sstevel@tonic-gate  */
6407c478bd9Sstevel@tonic-gate int
6417c478bd9Sstevel@tonic-gate memrange_num(pfn_t pfn)
6427c478bd9Sstevel@tonic-gate {
6437c478bd9Sstevel@tonic-gate 	int n;
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 	for (n = 0; n < nranges - 1; ++n) {
6467c478bd9Sstevel@tonic-gate 		if (pfn >= memranges[n])
6477c478bd9Sstevel@tonic-gate 			break;
6487c478bd9Sstevel@tonic-gate 	}
6497c478bd9Sstevel@tonic-gate 	return (n);
6507c478bd9Sstevel@tonic-gate }
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate /*
6537c478bd9Sstevel@tonic-gate  * return the mnoderange containing pfn
6547c478bd9Sstevel@tonic-gate  */
6557c478bd9Sstevel@tonic-gate int
6567c478bd9Sstevel@tonic-gate pfn_2_mtype(pfn_t pfn)
6577c478bd9Sstevel@tonic-gate {
6587c478bd9Sstevel@tonic-gate 	int	n;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	for (n = mnoderangecnt - 1; n >= 0; n--) {
6617c478bd9Sstevel@tonic-gate 		if (pfn >= mnoderanges[n].mnr_pfnlo) {
6627c478bd9Sstevel@tonic-gate 			break;
6637c478bd9Sstevel@tonic-gate 		}
6647c478bd9Sstevel@tonic-gate 	}
6657c478bd9Sstevel@tonic-gate 	return (n);
6667c478bd9Sstevel@tonic-gate }
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate /*
6697c478bd9Sstevel@tonic-gate  * is_contigpage_free:
6707c478bd9Sstevel@tonic-gate  *	returns a page list of contiguous pages. It minimally has to return
6717c478bd9Sstevel@tonic-gate  *	minctg pages. Caller determines minctg based on the scatter-gather
6727c478bd9Sstevel@tonic-gate  *	list length.
6737c478bd9Sstevel@tonic-gate  *
6747c478bd9Sstevel@tonic-gate  *	pfnp is set to the next page frame to search on return.
6757c478bd9Sstevel@tonic-gate  */
6767c478bd9Sstevel@tonic-gate static page_t *
6777c478bd9Sstevel@tonic-gate is_contigpage_free(
6787c478bd9Sstevel@tonic-gate 	pfn_t *pfnp,
6797c478bd9Sstevel@tonic-gate 	pgcnt_t *pgcnt,
6807c478bd9Sstevel@tonic-gate 	pgcnt_t minctg,
6817c478bd9Sstevel@tonic-gate 	uint64_t pfnseg,
6827c478bd9Sstevel@tonic-gate 	int iolock)
6837c478bd9Sstevel@tonic-gate {
6847c478bd9Sstevel@tonic-gate 	int	i = 0;
6857c478bd9Sstevel@tonic-gate 	pfn_t	pfn = *pfnp;
6867c478bd9Sstevel@tonic-gate 	page_t	*pp;
6877c478bd9Sstevel@tonic-gate 	page_t	*plist = NULL;
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	/*
6907c478bd9Sstevel@tonic-gate 	 * fail if pfn + minctg crosses a segment boundary.
6917c478bd9Sstevel@tonic-gate 	 * Adjust for next starting pfn to begin at segment boundary.
6927c478bd9Sstevel@tonic-gate 	 */
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	if (((*pfnp + minctg - 1) & pfnseg) < (*pfnp & pfnseg)) {
6957c478bd9Sstevel@tonic-gate 		*pfnp = roundup(*pfnp, pfnseg + 1);
6967c478bd9Sstevel@tonic-gate 		return (NULL);
6977c478bd9Sstevel@tonic-gate 	}
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	do {
7007c478bd9Sstevel@tonic-gate retry:
7017c478bd9Sstevel@tonic-gate 		pp = page_numtopp_nolock(pfn + i);
7027c478bd9Sstevel@tonic-gate 		if ((pp == NULL) ||
7037c478bd9Sstevel@tonic-gate 		    (page_trylock(pp, SE_EXCL) == 0)) {
7047c478bd9Sstevel@tonic-gate 			(*pfnp)++;
7057c478bd9Sstevel@tonic-gate 			break;
7067c478bd9Sstevel@tonic-gate 		}
7077c478bd9Sstevel@tonic-gate 		if (page_pptonum(pp) != pfn + i) {
7087c478bd9Sstevel@tonic-gate 			page_unlock(pp);
7097c478bd9Sstevel@tonic-gate 			goto retry;
7107c478bd9Sstevel@tonic-gate 		}
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 		if (!(PP_ISFREE(pp))) {
7137c478bd9Sstevel@tonic-gate 			page_unlock(pp);
7147c478bd9Sstevel@tonic-gate 			(*pfnp)++;
7157c478bd9Sstevel@tonic-gate 			break;
7167c478bd9Sstevel@tonic-gate 		}
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 		if (!PP_ISAGED(pp)) {
7197c478bd9Sstevel@tonic-gate 			page_list_sub(pp, PG_CACHE_LIST);
7207c478bd9Sstevel@tonic-gate 			page_hashout(pp, (kmutex_t *)NULL);
7217c478bd9Sstevel@tonic-gate 		} else {
7227c478bd9Sstevel@tonic-gate 			page_list_sub(pp, PG_FREE_LIST);
7237c478bd9Sstevel@tonic-gate 		}
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 		if (iolock)
7267c478bd9Sstevel@tonic-gate 			page_io_lock(pp);
7277c478bd9Sstevel@tonic-gate 		page_list_concat(&plist, &pp);
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 		/*
7307c478bd9Sstevel@tonic-gate 		 * exit loop when pgcnt satisfied or segment boundary reached.
7317c478bd9Sstevel@tonic-gate 		 */
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 	} while ((++i < *pgcnt) && ((pfn + i) & pfnseg));
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 	*pfnp += i;		/* set to next pfn to search */
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	if (i >= minctg) {
7387c478bd9Sstevel@tonic-gate 		*pgcnt -= i;
7397c478bd9Sstevel@tonic-gate 		return (plist);
7407c478bd9Sstevel@tonic-gate 	}
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	/*
7437c478bd9Sstevel@tonic-gate 	 * failure: minctg not satisfied.
7447c478bd9Sstevel@tonic-gate 	 *
7457c478bd9Sstevel@tonic-gate 	 * if next request crosses segment boundary, set next pfn
7467c478bd9Sstevel@tonic-gate 	 * to search from the segment boundary.
7477c478bd9Sstevel@tonic-gate 	 */
7487c478bd9Sstevel@tonic-gate 	if (((*pfnp + minctg - 1) & pfnseg) < (*pfnp & pfnseg))
7497c478bd9Sstevel@tonic-gate 		*pfnp = roundup(*pfnp, pfnseg + 1);
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	/* clean up any pages already allocated */
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	while (plist) {
7547c478bd9Sstevel@tonic-gate 		pp = plist;
7557c478bd9Sstevel@tonic-gate 		page_sub(&plist, pp);
7567c478bd9Sstevel@tonic-gate 		page_list_add(pp, PG_FREE_LIST | PG_LIST_TAIL);
7577c478bd9Sstevel@tonic-gate 		if (iolock)
7587c478bd9Sstevel@tonic-gate 			page_io_unlock(pp);
7597c478bd9Sstevel@tonic-gate 		page_unlock(pp);
7607c478bd9Sstevel@tonic-gate 	}
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 	return (NULL);
7637c478bd9Sstevel@tonic-gate }
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate /*
7667c478bd9Sstevel@tonic-gate  * verify that pages being returned from allocator have correct DMA attribute
7677c478bd9Sstevel@tonic-gate  */
7687c478bd9Sstevel@tonic-gate #ifndef DEBUG
7697c478bd9Sstevel@tonic-gate #define	check_dma(a, b, c) (0)
7707c478bd9Sstevel@tonic-gate #else
7717c478bd9Sstevel@tonic-gate static void
7727c478bd9Sstevel@tonic-gate check_dma(ddi_dma_attr_t *dma_attr, page_t *pp, int cnt)
7737c478bd9Sstevel@tonic-gate {
7747c478bd9Sstevel@tonic-gate 	if (dma_attr == NULL)
7757c478bd9Sstevel@tonic-gate 		return;
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 	while (cnt-- > 0) {
7787c478bd9Sstevel@tonic-gate 		if (mmu_ptob((uint64_t)pp->p_pagenum) <
7797c478bd9Sstevel@tonic-gate 		    dma_attr->dma_attr_addr_lo)
7807c478bd9Sstevel@tonic-gate 			panic("PFN (pp=%p) below dma_attr_addr_lo", pp);
7817c478bd9Sstevel@tonic-gate 		if (mmu_ptob((uint64_t)pp->p_pagenum) >=
7827c478bd9Sstevel@tonic-gate 		    dma_attr->dma_attr_addr_hi)
7837c478bd9Sstevel@tonic-gate 			panic("PFN (pp=%p) above dma_attr_addr_hi", pp);
7847c478bd9Sstevel@tonic-gate 		pp = pp->p_next;
7857c478bd9Sstevel@tonic-gate 	}
7867c478bd9Sstevel@tonic-gate }
7877c478bd9Sstevel@tonic-gate #endif
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate static kmutex_t	contig_lock;
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate #define	CONTIG_LOCK()	mutex_enter(&contig_lock);
7927c478bd9Sstevel@tonic-gate #define	CONTIG_UNLOCK()	mutex_exit(&contig_lock);
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate #define	PFN_16M		(mmu_btop((uint64_t)0x1000000))
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate static page_t *
7977c478bd9Sstevel@tonic-gate page_get_contigpage(pgcnt_t *pgcnt, ddi_dma_attr_t *mattr, int iolock)
7987c478bd9Sstevel@tonic-gate {
7997c478bd9Sstevel@tonic-gate 	pfn_t		pfn;
8007c478bd9Sstevel@tonic-gate 	int		sgllen;
8017c478bd9Sstevel@tonic-gate 	uint64_t	pfnseg;
8027c478bd9Sstevel@tonic-gate 	pgcnt_t		minctg;
8037c478bd9Sstevel@tonic-gate 	page_t		*pplist = NULL, *plist;
8047c478bd9Sstevel@tonic-gate 	uint64_t	lo, hi;
8057c478bd9Sstevel@tonic-gate 	pgcnt_t		pfnalign = 0;
8067c478bd9Sstevel@tonic-gate 	static pfn_t	startpfn;
8077c478bd9Sstevel@tonic-gate 	static pgcnt_t	lastctgcnt;
8087c478bd9Sstevel@tonic-gate 	uintptr_t	align;
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 	CONTIG_LOCK();
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 	if (mattr) {
8137c478bd9Sstevel@tonic-gate 		lo = mmu_btop((mattr->dma_attr_addr_lo + MMU_PAGEOFFSET));
8147c478bd9Sstevel@tonic-gate 		hi = mmu_btop(mattr->dma_attr_addr_hi);
8157c478bd9Sstevel@tonic-gate 		if (hi >= physmax)
8167c478bd9Sstevel@tonic-gate 			hi = physmax - 1;
8177c478bd9Sstevel@tonic-gate 		sgllen = mattr->dma_attr_sgllen;
8187c478bd9Sstevel@tonic-gate 		pfnseg = mmu_btop(mattr->dma_attr_seg);
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 		align = maxbit(mattr->dma_attr_align, mattr->dma_attr_minxfer);
8217c478bd9Sstevel@tonic-gate 		if (align > MMU_PAGESIZE)
8227c478bd9Sstevel@tonic-gate 			pfnalign = mmu_btop(align);
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 		/*
8257c478bd9Sstevel@tonic-gate 		 * in order to satisfy the request, must minimally
8267c478bd9Sstevel@tonic-gate 		 * acquire minctg contiguous pages
8277c478bd9Sstevel@tonic-gate 		 */
8287c478bd9Sstevel@tonic-gate 		minctg = howmany(*pgcnt, sgllen);
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 		ASSERT(hi >= lo);
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 		/*
8337c478bd9Sstevel@tonic-gate 		 * start from where last searched if the minctg >= lastctgcnt
8347c478bd9Sstevel@tonic-gate 		 */
8357c478bd9Sstevel@tonic-gate 		if (minctg < lastctgcnt || startpfn < lo || startpfn > hi)
8367c478bd9Sstevel@tonic-gate 			startpfn = lo;
8377c478bd9Sstevel@tonic-gate 	} else {
8387c478bd9Sstevel@tonic-gate 		hi = physmax - 1;
8397c478bd9Sstevel@tonic-gate 		lo = 0;
8407c478bd9Sstevel@tonic-gate 		sgllen = 1;
8417c478bd9Sstevel@tonic-gate 		pfnseg = mmu.highest_pfn;
8427c478bd9Sstevel@tonic-gate 		minctg = *pgcnt;
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 		if (minctg < lastctgcnt)
8457c478bd9Sstevel@tonic-gate 			startpfn = lo;
8467c478bd9Sstevel@tonic-gate 	}
8477c478bd9Sstevel@tonic-gate 	lastctgcnt = minctg;
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 	ASSERT(pfnseg + 1 >= (uint64_t)minctg);
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 	/* conserve 16m memory - start search above 16m when possible */
8527c478bd9Sstevel@tonic-gate 	if (hi > PFN_16M && startpfn < PFN_16M)
8537c478bd9Sstevel@tonic-gate 		startpfn = PFN_16M;
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	pfn = startpfn;
8567c478bd9Sstevel@tonic-gate 	if (pfnalign)
8577c478bd9Sstevel@tonic-gate 		pfn = P2ROUNDUP(pfn, pfnalign);
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	while (pfn + minctg - 1 <= hi) {
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 		plist = is_contigpage_free(&pfn, pgcnt, minctg, pfnseg, iolock);
8627c478bd9Sstevel@tonic-gate 		if (plist) {
8637c478bd9Sstevel@tonic-gate 			page_list_concat(&pplist, &plist);
8647c478bd9Sstevel@tonic-gate 			sgllen--;
8657c478bd9Sstevel@tonic-gate 			/*
8667c478bd9Sstevel@tonic-gate 			 * return when contig pages no longer needed
8677c478bd9Sstevel@tonic-gate 			 */
8687c478bd9Sstevel@tonic-gate 			if (!*pgcnt || ((*pgcnt <= sgllen) && !pfnalign)) {
8697c478bd9Sstevel@tonic-gate 				startpfn = pfn;
8707c478bd9Sstevel@tonic-gate 				CONTIG_UNLOCK();
8717c478bd9Sstevel@tonic-gate 				check_dma(mattr, pplist, *pgcnt);
8727c478bd9Sstevel@tonic-gate 				return (pplist);
8737c478bd9Sstevel@tonic-gate 			}
8747c478bd9Sstevel@tonic-gate 			minctg = howmany(*pgcnt, sgllen);
8757c478bd9Sstevel@tonic-gate 		}
8767c478bd9Sstevel@tonic-gate 		if (pfnalign)
8777c478bd9Sstevel@tonic-gate 			pfn = P2ROUNDUP(pfn, pfnalign);
8787c478bd9Sstevel@tonic-gate 	}
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	/* cannot find contig pages in specified range */
8817c478bd9Sstevel@tonic-gate 	if (startpfn == lo) {
8827c478bd9Sstevel@tonic-gate 		CONTIG_UNLOCK();
8837c478bd9Sstevel@tonic-gate 		return (NULL);
8847c478bd9Sstevel@tonic-gate 	}
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	/* did not start with lo previously */
8877c478bd9Sstevel@tonic-gate 	pfn = lo;
8887c478bd9Sstevel@tonic-gate 	if (pfnalign)
8897c478bd9Sstevel@tonic-gate 		pfn = P2ROUNDUP(pfn, pfnalign);
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 	/* allow search to go above startpfn */
8927c478bd9Sstevel@tonic-gate 	while (pfn < startpfn) {
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 		plist = is_contigpage_free(&pfn, pgcnt, minctg, pfnseg, iolock);
8957c478bd9Sstevel@tonic-gate 		if (plist != NULL) {
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 			page_list_concat(&pplist, &plist);
8987c478bd9Sstevel@tonic-gate 			sgllen--;
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 			/*
9017c478bd9Sstevel@tonic-gate 			 * return when contig pages no longer needed
9027c478bd9Sstevel@tonic-gate 			 */
9037c478bd9Sstevel@tonic-gate 			if (!*pgcnt || ((*pgcnt <= sgllen) && !pfnalign)) {
9047c478bd9Sstevel@tonic-gate 				startpfn = pfn;
9057c478bd9Sstevel@tonic-gate 				CONTIG_UNLOCK();
9067c478bd9Sstevel@tonic-gate 				check_dma(mattr, pplist, *pgcnt);
9077c478bd9Sstevel@tonic-gate 				return (pplist);
9087c478bd9Sstevel@tonic-gate 			}
9097c478bd9Sstevel@tonic-gate 			minctg = howmany(*pgcnt, sgllen);
9107c478bd9Sstevel@tonic-gate 		}
9117c478bd9Sstevel@tonic-gate 		if (pfnalign)
9127c478bd9Sstevel@tonic-gate 			pfn = P2ROUNDUP(pfn, pfnalign);
9137c478bd9Sstevel@tonic-gate 	}
9147c478bd9Sstevel@tonic-gate 	CONTIG_UNLOCK();
9157c478bd9Sstevel@tonic-gate 	return (NULL);
9167c478bd9Sstevel@tonic-gate }
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate /*
9197c478bd9Sstevel@tonic-gate  * combine mem_node_config and memrange memory ranges into one data
9207c478bd9Sstevel@tonic-gate  * structure to be used for page list management.
9217c478bd9Sstevel@tonic-gate  *
9227c478bd9Sstevel@tonic-gate  * mnode_range_cnt() calculates the number of memory ranges for mnode and
9237c478bd9Sstevel@tonic-gate  * memranges[]. Used to determine the size of page lists and mnoderanges.
9247c478bd9Sstevel@tonic-gate  *
9257c478bd9Sstevel@tonic-gate  * mnode_range_setup() initializes mnoderanges.
9267c478bd9Sstevel@tonic-gate  */
9277c478bd9Sstevel@tonic-gate mnoderange_t	*mnoderanges;
9287c478bd9Sstevel@tonic-gate int		mnoderangecnt;
9297c478bd9Sstevel@tonic-gate int		mtype4g;
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate int
9327c478bd9Sstevel@tonic-gate mnode_range_cnt()
9337c478bd9Sstevel@tonic-gate {
9347c478bd9Sstevel@tonic-gate 	int	mri;
9357c478bd9Sstevel@tonic-gate 	int	mnrcnt = 0;
9367c478bd9Sstevel@tonic-gate 	int	mnode;
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 	for (mnode = 0; mnode < max_mem_nodes; mnode++) {
9397c478bd9Sstevel@tonic-gate 		if (mem_node_config[mnode].exists == 0)
9407c478bd9Sstevel@tonic-gate 			continue;
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 		mri = nranges - 1;
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 		/* find the memranges index below contained in mnode range */
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 		while (MEMRANGEHI(mri) < mem_node_config[mnode].physbase)
9477c478bd9Sstevel@tonic-gate 			mri--;
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 		/*
9507c478bd9Sstevel@tonic-gate 		 * increment mnode range counter when memranges or mnode
9517c478bd9Sstevel@tonic-gate 		 * boundary is reached.
9527c478bd9Sstevel@tonic-gate 		 */
9537c478bd9Sstevel@tonic-gate 		while (mri >= 0 &&
9547c478bd9Sstevel@tonic-gate 		    mem_node_config[mnode].physmax >= MEMRANGELO(mri)) {
9557c478bd9Sstevel@tonic-gate 			mnrcnt++;
9567c478bd9Sstevel@tonic-gate 			if (mem_node_config[mnode].physmax > MEMRANGEHI(mri))
9577c478bd9Sstevel@tonic-gate 				mri--;
9587c478bd9Sstevel@tonic-gate 			else
9597c478bd9Sstevel@tonic-gate 				break;
9607c478bd9Sstevel@tonic-gate 		}
9617c478bd9Sstevel@tonic-gate 	}
9627c478bd9Sstevel@tonic-gate 	return (mnrcnt);
9637c478bd9Sstevel@tonic-gate }
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate void
9667c478bd9Sstevel@tonic-gate mnode_range_setup(mnoderange_t *mnoderanges)
9677c478bd9Sstevel@tonic-gate {
9687c478bd9Sstevel@tonic-gate 	int	mnode, mri;
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 	for (mnode = 0; mnode < max_mem_nodes; mnode++) {
9717c478bd9Sstevel@tonic-gate 		if (mem_node_config[mnode].exists == 0)
9727c478bd9Sstevel@tonic-gate 			continue;
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 		mri = nranges - 1;
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 		while (MEMRANGEHI(mri) < mem_node_config[mnode].physbase)
9777c478bd9Sstevel@tonic-gate 			mri--;
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 		while (mri >= 0 && mem_node_config[mnode].physmax >=
9807c478bd9Sstevel@tonic-gate 		    MEMRANGELO(mri)) {
9817c478bd9Sstevel@tonic-gate 			mnoderanges->mnr_pfnlo =
9827c478bd9Sstevel@tonic-gate 			    MAX(MEMRANGELO(mri),
9837c478bd9Sstevel@tonic-gate 				mem_node_config[mnode].physbase);
9847c478bd9Sstevel@tonic-gate 			mnoderanges->mnr_pfnhi =
9857c478bd9Sstevel@tonic-gate 			    MIN(MEMRANGEHI(mri),
9867c478bd9Sstevel@tonic-gate 				mem_node_config[mnode].physmax);
9877c478bd9Sstevel@tonic-gate 			mnoderanges->mnr_mnode = mnode;
9887c478bd9Sstevel@tonic-gate 			mnoderanges->mnr_memrange = mri;
9897c478bd9Sstevel@tonic-gate 			mnoderanges++;
9907c478bd9Sstevel@tonic-gate 			if (mem_node_config[mnode].physmax > MEMRANGEHI(mri))
9917c478bd9Sstevel@tonic-gate 				mri--;
9927c478bd9Sstevel@tonic-gate 			else
9937c478bd9Sstevel@tonic-gate 				break;
9947c478bd9Sstevel@tonic-gate 		}
9957c478bd9Sstevel@tonic-gate 	}
9967c478bd9Sstevel@tonic-gate }
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate /*
9997c478bd9Sstevel@tonic-gate  * Determine if the mnode range specified in mtype contains memory belonging
10007c478bd9Sstevel@tonic-gate  * to memory node mnode.  If flags & PGI_MT_RANGE is set then mtype contains
100107ad560dSkchow  * the range of indices from high pfn to 0, 16m or 4g.
10027c478bd9Sstevel@tonic-gate  *
10037c478bd9Sstevel@tonic-gate  * Return first mnode range type index found otherwise return -1 if none found.
10047c478bd9Sstevel@tonic-gate  */
10057c478bd9Sstevel@tonic-gate int
10067c478bd9Sstevel@tonic-gate mtype_func(int mnode, int mtype, uint_t flags)
10077c478bd9Sstevel@tonic-gate {
10087c478bd9Sstevel@tonic-gate 	if (flags & PGI_MT_RANGE) {
100907ad560dSkchow 		int	mtlim;
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 		if (flags & PGI_MT_NEXT)
10127c478bd9Sstevel@tonic-gate 			mtype--;
101307ad560dSkchow 		if (flags & PGI_MT_RANGE0)
101407ad560dSkchow 			mtlim = 0;
101507ad560dSkchow 		else if (flags & PGI_MT_RANGE4G)
101607ad560dSkchow 			mtlim = mtype4g + 1;	/* exclude 0-4g range */
101707ad560dSkchow 		else if (flags & PGI_MT_RANGE16M)
101807ad560dSkchow 			mtlim = 1;		/* exclude 0-16m range */
10197c478bd9Sstevel@tonic-gate 		while (mtype >= mtlim) {
10207c478bd9Sstevel@tonic-gate 			if (mnoderanges[mtype].mnr_mnode == mnode)
10217c478bd9Sstevel@tonic-gate 				return (mtype);
10227c478bd9Sstevel@tonic-gate 			mtype--;
10237c478bd9Sstevel@tonic-gate 		}
10247c478bd9Sstevel@tonic-gate 	} else {
10257c478bd9Sstevel@tonic-gate 		if (mnoderanges[mtype].mnr_mnode == mnode)
10267c478bd9Sstevel@tonic-gate 			return (mtype);
10277c478bd9Sstevel@tonic-gate 	}
10287c478bd9Sstevel@tonic-gate 	return (-1);
10297c478bd9Sstevel@tonic-gate }
10307c478bd9Sstevel@tonic-gate 
1031e21bae1bSkchow /*
1032e21bae1bSkchow  * Update the page list max counts with the pfn range specified by the
1033e21bae1bSkchow  * input parameters.  Called from add_physmem() when physical memory with
1034e21bae1bSkchow  * page_t's are initially added to the page lists.
1035e21bae1bSkchow  */
1036e21bae1bSkchow void
1037e21bae1bSkchow mtype_modify_max(pfn_t startpfn, long cnt)
1038e21bae1bSkchow {
1039e21bae1bSkchow 	int	mtype = 0;
1040e21bae1bSkchow 	pfn_t	endpfn = startpfn + cnt, pfn;
1041e21bae1bSkchow 	pgcnt_t	inc;
1042e21bae1bSkchow 
1043e21bae1bSkchow 	ASSERT(cnt > 0);
1044e21bae1bSkchow 
1045e21bae1bSkchow 	for (pfn = startpfn; pfn < endpfn; ) {
1046e21bae1bSkchow 		if (pfn <= mnoderanges[mtype].mnr_pfnhi) {
1047e21bae1bSkchow 			if (endpfn < mnoderanges[mtype].mnr_pfnhi) {
1048e21bae1bSkchow 				inc = endpfn - pfn;
1049e21bae1bSkchow 			} else {
1050e21bae1bSkchow 				inc = mnoderanges[mtype].mnr_pfnhi - pfn + 1;
1051e21bae1bSkchow 			}
1052e21bae1bSkchow 			mnoderanges[mtype].mnr_mt_pgmax += inc;
1053e21bae1bSkchow 			if (physmax4g && mtype <= mtype4g)
1054e21bae1bSkchow 				maxmem4g += inc;
1055e21bae1bSkchow 			pfn += inc;
1056e21bae1bSkchow 		}
1057e21bae1bSkchow 		mtype++;
1058e21bae1bSkchow 		ASSERT(mtype < mnoderangecnt || pfn >= endpfn);
1059e21bae1bSkchow 	}
1060e21bae1bSkchow }
1061e21bae1bSkchow 
1062affbd3ccSkchow /*
1063affbd3ccSkchow  * Returns the free page count for mnode
1064affbd3ccSkchow  */
1065affbd3ccSkchow int
1066affbd3ccSkchow mnode_pgcnt(int mnode)
1067affbd3ccSkchow {
1068affbd3ccSkchow 	int	mtype = mnoderangecnt - 1;
1069affbd3ccSkchow 	int	flags = PGI_MT_RANGE0;
1070affbd3ccSkchow 	pgcnt_t	pgcnt = 0;
1071affbd3ccSkchow 
1072affbd3ccSkchow 	mtype = mtype_func(mnode, mtype, flags);
1073affbd3ccSkchow 
1074affbd3ccSkchow 	while (mtype != -1) {
107507ad560dSkchow 		pgcnt += MTYPE_FREEMEM(mtype);
1076affbd3ccSkchow 		mtype = mtype_func(mnode, mtype, flags | PGI_MT_NEXT);
1077affbd3ccSkchow 	}
1078affbd3ccSkchow 	return (pgcnt);
1079affbd3ccSkchow }
1080affbd3ccSkchow 
10817c478bd9Sstevel@tonic-gate /*
10827c478bd9Sstevel@tonic-gate  * Initialize page coloring variables based on the l2 cache parameters.
10837c478bd9Sstevel@tonic-gate  * Calculate and return memory needed for page coloring data structures.
10847c478bd9Sstevel@tonic-gate  */
10857c478bd9Sstevel@tonic-gate size_t
10867c478bd9Sstevel@tonic-gate page_coloring_init(uint_t l2_sz, int l2_linesz, int l2_assoc)
10877c478bd9Sstevel@tonic-gate {
10887c478bd9Sstevel@tonic-gate 	size_t	colorsz = 0;
10897c478bd9Sstevel@tonic-gate 	int	i;
10907c478bd9Sstevel@tonic-gate 	int	colors;
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 	/*
10937c478bd9Sstevel@tonic-gate 	 * Reduce the memory ranges lists if we don't have large amounts
10947c478bd9Sstevel@tonic-gate 	 * of memory. This avoids searching known empty free lists.
10957c478bd9Sstevel@tonic-gate 	 */
10967c478bd9Sstevel@tonic-gate 	i = memrange_num(physmax);
10977c478bd9Sstevel@tonic-gate 	memranges += i;
10987c478bd9Sstevel@tonic-gate 	nranges -= i;
10997c478bd9Sstevel@tonic-gate #if defined(__i386)
11007c478bd9Sstevel@tonic-gate 	if (i > 0)
11017c478bd9Sstevel@tonic-gate 		restricted_kmemalloc = 0;
11027c478bd9Sstevel@tonic-gate #endif
11037c478bd9Sstevel@tonic-gate 	/* physmax greater than 4g */
11047c478bd9Sstevel@tonic-gate 	if (i == 0)
11057c478bd9Sstevel@tonic-gate 		physmax4g = 1;
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 	/*
11087c478bd9Sstevel@tonic-gate 	 * setup pagesize for generic page layer
11097c478bd9Sstevel@tonic-gate 	 */
11107c478bd9Sstevel@tonic-gate 	for (i = 0; i <= mmu.max_page_level; ++i) {
11117c478bd9Sstevel@tonic-gate 		hw_page_array[i].hp_size = LEVEL_SIZE(i);
11127c478bd9Sstevel@tonic-gate 		hw_page_array[i].hp_shift = LEVEL_SHIFT(i);
11137c478bd9Sstevel@tonic-gate 		hw_page_array[i].hp_pgcnt = LEVEL_SIZE(i) >> LEVEL_SHIFT(0);
11147c478bd9Sstevel@tonic-gate 	}
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 	ASSERT(ISP2(l2_sz));
11177c478bd9Sstevel@tonic-gate 	ASSERT(ISP2(l2_linesz));
11187c478bd9Sstevel@tonic-gate 	ASSERT(l2_sz > MMU_PAGESIZE);
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 	/* l2_assoc is 0 for fully associative l2 cache */
11217c478bd9Sstevel@tonic-gate 	if (l2_assoc)
11227c478bd9Sstevel@tonic-gate 		l2_colors = MAX(1, l2_sz / (l2_assoc * MMU_PAGESIZE));
11237c478bd9Sstevel@tonic-gate 	else
11247c478bd9Sstevel@tonic-gate 		l2_colors = 1;
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 	/* for scalability, configure at least PAGE_COLORS_MIN color bins */
11277c478bd9Sstevel@tonic-gate 	page_colors = MAX(l2_colors, PAGE_COLORS_MIN);
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 	/*
11307c478bd9Sstevel@tonic-gate 	 * cpu_page_colors is non-zero when a page color may be spread across
11317c478bd9Sstevel@tonic-gate 	 * multiple bins.
11327c478bd9Sstevel@tonic-gate 	 */
11337c478bd9Sstevel@tonic-gate 	if (l2_colors < page_colors)
11347c478bd9Sstevel@tonic-gate 		cpu_page_colors = l2_colors;
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	ASSERT(ISP2(page_colors));
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 	page_colors_mask = page_colors - 1;
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate 	ASSERT(ISP2(CPUSETSIZE()));
11417c478bd9Sstevel@tonic-gate 	page_coloring_shift = lowbit(CPUSETSIZE());
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 	/* size for mnoderanges */
11447c478bd9Sstevel@tonic-gate 	mnoderangecnt = mnode_range_cnt();
11457c478bd9Sstevel@tonic-gate 	colorsz = mnoderangecnt * sizeof (mnoderange_t);
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 	/* size for fpc_mutex and cpc_mutex */
11487c478bd9Sstevel@tonic-gate 	colorsz += (2 * max_mem_nodes * sizeof (kmutex_t) * NPC_MUTEX);
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 	/* size of page_freelists */
11517c478bd9Sstevel@tonic-gate 	colorsz += mnoderangecnt * sizeof (page_t ***);
11527c478bd9Sstevel@tonic-gate 	colorsz += mnoderangecnt * mmu_page_sizes * sizeof (page_t **);
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 	for (i = 0; i < mmu_page_sizes; i++) {
11557c478bd9Sstevel@tonic-gate 		colors = page_get_pagecolors(i);
11567c478bd9Sstevel@tonic-gate 		colorsz += mnoderangecnt * colors * sizeof (page_t *);
11577c478bd9Sstevel@tonic-gate 	}
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate 	/* size of page_cachelists */
11607c478bd9Sstevel@tonic-gate 	colorsz += mnoderangecnt * sizeof (page_t **);
11617c478bd9Sstevel@tonic-gate 	colorsz += mnoderangecnt * page_colors * sizeof (page_t *);
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	return (colorsz);
11647c478bd9Sstevel@tonic-gate }
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate /*
11677c478bd9Sstevel@tonic-gate  * Called once at startup to configure page_coloring data structures and
11687c478bd9Sstevel@tonic-gate  * does the 1st page_free()/page_freelist_add().
11697c478bd9Sstevel@tonic-gate  */
11707c478bd9Sstevel@tonic-gate void
11717c478bd9Sstevel@tonic-gate page_coloring_setup(caddr_t pcmemaddr)
11727c478bd9Sstevel@tonic-gate {
11737c478bd9Sstevel@tonic-gate 	int	i;
11747c478bd9Sstevel@tonic-gate 	int	j;
11757c478bd9Sstevel@tonic-gate 	int	k;
11767c478bd9Sstevel@tonic-gate 	caddr_t	addr;
11777c478bd9Sstevel@tonic-gate 	int	colors;
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 	/*
11807c478bd9Sstevel@tonic-gate 	 * do page coloring setup
11817c478bd9Sstevel@tonic-gate 	 */
11827c478bd9Sstevel@tonic-gate 	addr = pcmemaddr;
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate 	mnoderanges = (mnoderange_t *)addr;
11857c478bd9Sstevel@tonic-gate 	addr += (mnoderangecnt * sizeof (mnoderange_t));
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate 	mnode_range_setup(mnoderanges);
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate 	if (physmax4g)
11907c478bd9Sstevel@tonic-gate 		mtype4g = pfn_2_mtype(0xfffff);
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 	for (k = 0; k < NPC_MUTEX; k++) {
11937c478bd9Sstevel@tonic-gate 		fpc_mutex[k] = (kmutex_t *)addr;
11947c478bd9Sstevel@tonic-gate 		addr += (max_mem_nodes * sizeof (kmutex_t));
11957c478bd9Sstevel@tonic-gate 	}
11967c478bd9Sstevel@tonic-gate 	for (k = 0; k < NPC_MUTEX; k++) {
11977c478bd9Sstevel@tonic-gate 		cpc_mutex[k] = (kmutex_t *)addr;
11987c478bd9Sstevel@tonic-gate 		addr += (max_mem_nodes * sizeof (kmutex_t));
11997c478bd9Sstevel@tonic-gate 	}
12007c478bd9Sstevel@tonic-gate 	page_freelists = (page_t ****)addr;
12017c478bd9Sstevel@tonic-gate 	addr += (mnoderangecnt * sizeof (page_t ***));
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate 	page_cachelists = (page_t ***)addr;
12047c478bd9Sstevel@tonic-gate 	addr += (mnoderangecnt * sizeof (page_t **));
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 	for (i = 0; i < mnoderangecnt; i++) {
12077c478bd9Sstevel@tonic-gate 		page_freelists[i] = (page_t ***)addr;
12087c478bd9Sstevel@tonic-gate 		addr += (mmu_page_sizes * sizeof (page_t **));
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 		for (j = 0; j < mmu_page_sizes; j++) {
12117c478bd9Sstevel@tonic-gate 			colors = page_get_pagecolors(j);
12127c478bd9Sstevel@tonic-gate 			page_freelists[i][j] = (page_t **)addr;
12137c478bd9Sstevel@tonic-gate 			addr += (colors * sizeof (page_t *));
12147c478bd9Sstevel@tonic-gate 		}
12157c478bd9Sstevel@tonic-gate 		page_cachelists[i] = (page_t **)addr;
12167c478bd9Sstevel@tonic-gate 		addr += (page_colors * sizeof (page_t *));
12177c478bd9Sstevel@tonic-gate 	}
12187c478bd9Sstevel@tonic-gate }
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate /*ARGSUSED*/
12217c478bd9Sstevel@tonic-gate int
12227c478bd9Sstevel@tonic-gate bp_color(struct buf *bp)
12237c478bd9Sstevel@tonic-gate {
12247c478bd9Sstevel@tonic-gate 	return (0);
12257c478bd9Sstevel@tonic-gate }
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate /*
12287c478bd9Sstevel@tonic-gate  * get a page from any list with the given mnode
12297c478bd9Sstevel@tonic-gate  */
12307c478bd9Sstevel@tonic-gate page_t *
12317c478bd9Sstevel@tonic-gate page_get_mnode_anylist(ulong_t origbin, uchar_t szc, uint_t flags,
12327c478bd9Sstevel@tonic-gate     int mnode, int mtype, ddi_dma_attr_t *dma_attr)
12337c478bd9Sstevel@tonic-gate {
12347c478bd9Sstevel@tonic-gate 	kmutex_t	*pcm;
12357c478bd9Sstevel@tonic-gate 	int		i;
12367c478bd9Sstevel@tonic-gate 	page_t		*pp;
12377c478bd9Sstevel@tonic-gate 	page_t		*first_pp;
12387c478bd9Sstevel@tonic-gate 	uint64_t	pgaddr;
12397c478bd9Sstevel@tonic-gate 	ulong_t		bin;
12407c478bd9Sstevel@tonic-gate 	int		mtypestart;
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate 	VM_STAT_ADD(pga_vmstats.pgma_alloc);
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate 	ASSERT((flags & PG_MATCH_COLOR) == 0);
12457c478bd9Sstevel@tonic-gate 	ASSERT(szc == 0);
12467c478bd9Sstevel@tonic-gate 	ASSERT(dma_attr != NULL);
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 
12497c478bd9Sstevel@tonic-gate 	MTYPE_START(mnode, mtype, flags);
12507c478bd9Sstevel@tonic-gate 	if (mtype < 0) {
12517c478bd9Sstevel@tonic-gate 		VM_STAT_ADD(pga_vmstats.pgma_allocempty);
12527c478bd9Sstevel@tonic-gate 		return (NULL);
12537c478bd9Sstevel@tonic-gate 	}
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate 	mtypestart = mtype;
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 	bin = origbin;
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate 	/*
12607c478bd9Sstevel@tonic-gate 	 * check up to page_colors + 1 bins - origbin may be checked twice
12617c478bd9Sstevel@tonic-gate 	 * because of BIN_STEP skip
12627c478bd9Sstevel@tonic-gate 	 */
12637c478bd9Sstevel@tonic-gate 	do {
12647c478bd9Sstevel@tonic-gate 		i = 0;
12657c478bd9Sstevel@tonic-gate 		while (i <= page_colors) {
12667c478bd9Sstevel@tonic-gate 			if (PAGE_FREELISTS(mnode, szc, bin, mtype) == NULL)
12677c478bd9Sstevel@tonic-gate 				goto nextfreebin;
12687c478bd9Sstevel@tonic-gate 
12697c478bd9Sstevel@tonic-gate 			pcm = PC_BIN_MUTEX(mnode, bin, PG_FREE_LIST);
12707c478bd9Sstevel@tonic-gate 			mutex_enter(pcm);
12717c478bd9Sstevel@tonic-gate 			pp = PAGE_FREELISTS(mnode, szc, bin, mtype);
12727c478bd9Sstevel@tonic-gate 			first_pp = pp;
12737c478bd9Sstevel@tonic-gate 			while (pp != NULL) {
12747c478bd9Sstevel@tonic-gate 				if (page_trylock(pp, SE_EXCL) == 0) {
12757c478bd9Sstevel@tonic-gate 					pp = pp->p_next;
12767c478bd9Sstevel@tonic-gate 					if (pp == first_pp) {
12777c478bd9Sstevel@tonic-gate 						pp = NULL;
12787c478bd9Sstevel@tonic-gate 					}
12797c478bd9Sstevel@tonic-gate 					continue;
12807c478bd9Sstevel@tonic-gate 				}
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate 				ASSERT(PP_ISFREE(pp));
12837c478bd9Sstevel@tonic-gate 				ASSERT(PP_ISAGED(pp));
12847c478bd9Sstevel@tonic-gate 				ASSERT(pp->p_vnode == NULL);
12857c478bd9Sstevel@tonic-gate 				ASSERT(pp->p_hash == NULL);
12867c478bd9Sstevel@tonic-gate 				ASSERT(pp->p_offset == (u_offset_t)-1);
12877c478bd9Sstevel@tonic-gate 				ASSERT(pp->p_szc == szc);
12887c478bd9Sstevel@tonic-gate 				ASSERT(PFN_2_MEM_NODE(pp->p_pagenum) == mnode);
12897c478bd9Sstevel@tonic-gate 				/* check if page within DMA attributes */
12907c478bd9Sstevel@tonic-gate 				pgaddr = mmu_ptob((uint64_t)(pp->p_pagenum));
12917c478bd9Sstevel@tonic-gate 
12927c478bd9Sstevel@tonic-gate 				if ((pgaddr >= dma_attr->dma_attr_addr_lo) &&
12937c478bd9Sstevel@tonic-gate 				    (pgaddr + MMU_PAGESIZE - 1 <=
12947c478bd9Sstevel@tonic-gate 				    dma_attr->dma_attr_addr_hi)) {
12957c478bd9Sstevel@tonic-gate 					break;
12967c478bd9Sstevel@tonic-gate 				}
12977c478bd9Sstevel@tonic-gate 
12987c478bd9Sstevel@tonic-gate 				/* continue looking */
12997c478bd9Sstevel@tonic-gate 				page_unlock(pp);
13007c478bd9Sstevel@tonic-gate 				pp = pp->p_next;
13017c478bd9Sstevel@tonic-gate 				if (pp == first_pp)
13027c478bd9Sstevel@tonic-gate 					pp = NULL;
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate 			}
13057c478bd9Sstevel@tonic-gate 			if (pp != NULL) {
13067c478bd9Sstevel@tonic-gate 				ASSERT(mtype == PP_2_MTYPE(pp));
13077c478bd9Sstevel@tonic-gate 				ASSERT(pp->p_szc == 0);
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate 				/* found a page with specified DMA attributes */
13107c478bd9Sstevel@tonic-gate 				page_sub(&PAGE_FREELISTS(mnode, szc, bin,
13117c478bd9Sstevel@tonic-gate 				    mtype), pp);
1312affbd3ccSkchow 				page_ctr_sub(mnode, mtype, pp, PG_FREE_LIST);
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate 				if ((PP_ISFREE(pp) == 0) ||
13157c478bd9Sstevel@tonic-gate 				    (PP_ISAGED(pp) == 0)) {
13167c478bd9Sstevel@tonic-gate 					cmn_err(CE_PANIC, "page %p is not free",
13177c478bd9Sstevel@tonic-gate 					    (void *)pp);
13187c478bd9Sstevel@tonic-gate 				}
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 				mutex_exit(pcm);
13217c478bd9Sstevel@tonic-gate 				check_dma(dma_attr, pp, 1);
13227c478bd9Sstevel@tonic-gate 				VM_STAT_ADD(pga_vmstats.pgma_allocok);
13237c478bd9Sstevel@tonic-gate 				return (pp);
13247c478bd9Sstevel@tonic-gate 			}
13257c478bd9Sstevel@tonic-gate 			mutex_exit(pcm);
13267c478bd9Sstevel@tonic-gate nextfreebin:
13277c478bd9Sstevel@tonic-gate 			pp = page_freelist_fill(szc, bin, mnode, mtype,
13287c478bd9Sstevel@tonic-gate 			    mmu_btop(dma_attr->dma_attr_addr_hi + 1));
13297c478bd9Sstevel@tonic-gate 			if (pp)
13307c478bd9Sstevel@tonic-gate 				return (pp);
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate 			/* try next bin */
13337c478bd9Sstevel@tonic-gate 			bin += (i == 0) ? BIN_STEP : 1;
13347c478bd9Sstevel@tonic-gate 			bin &= page_colors_mask;
13357c478bd9Sstevel@tonic-gate 			i++;
13367c478bd9Sstevel@tonic-gate 		}
1337affbd3ccSkchow 		MTYPE_NEXT(mnode, mtype, flags);
1338affbd3ccSkchow 	} while (mtype >= 0);
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate 	/* failed to find a page in the freelist; try it in the cachelist */
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate 	/* reset mtype start for cachelist search */
13437c478bd9Sstevel@tonic-gate 	mtype = mtypestart;
13447c478bd9Sstevel@tonic-gate 	ASSERT(mtype >= 0);
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate 	/* start with the bin of matching color */
13477c478bd9Sstevel@tonic-gate 	bin = origbin;
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate 	do {
13507c478bd9Sstevel@tonic-gate 		for (i = 0; i <= page_colors; i++) {
13517c478bd9Sstevel@tonic-gate 			if (PAGE_CACHELISTS(mnode, bin, mtype) == NULL)
13527c478bd9Sstevel@tonic-gate 				goto nextcachebin;
13537c478bd9Sstevel@tonic-gate 			pcm = PC_BIN_MUTEX(mnode, bin, PG_CACHE_LIST);
13547c478bd9Sstevel@tonic-gate 			mutex_enter(pcm);
13557c478bd9Sstevel@tonic-gate 			pp = PAGE_CACHELISTS(mnode, bin, mtype);
13567c478bd9Sstevel@tonic-gate 			first_pp = pp;
13577c478bd9Sstevel@tonic-gate 			while (pp != NULL) {
13587c478bd9Sstevel@tonic-gate 				if (page_trylock(pp, SE_EXCL) == 0) {
13597c478bd9Sstevel@tonic-gate 					pp = pp->p_next;
13607c478bd9Sstevel@tonic-gate 					if (pp == first_pp)
13617c478bd9Sstevel@tonic-gate 						break;
13627c478bd9Sstevel@tonic-gate 					continue;
13637c478bd9Sstevel@tonic-gate 				}
13647c478bd9Sstevel@tonic-gate 				ASSERT(pp->p_vnode);
13657c478bd9Sstevel@tonic-gate 				ASSERT(PP_ISAGED(pp) == 0);
13667c478bd9Sstevel@tonic-gate 				ASSERT(pp->p_szc == 0);
13677c478bd9Sstevel@tonic-gate 				ASSERT(PFN_2_MEM_NODE(pp->p_pagenum) == mnode);
13687c478bd9Sstevel@tonic-gate 
13697c478bd9Sstevel@tonic-gate 				/* check if page within DMA attributes */
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate 				pgaddr = ptob((uint64_t)(pp->p_pagenum));
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate 				if ((pgaddr >= dma_attr->dma_attr_addr_lo) &&
13747c478bd9Sstevel@tonic-gate 				    (pgaddr + MMU_PAGESIZE - 1 <=
13757c478bd9Sstevel@tonic-gate 				    dma_attr->dma_attr_addr_hi)) {
13767c478bd9Sstevel@tonic-gate 					break;
13777c478bd9Sstevel@tonic-gate 				}
13787c478bd9Sstevel@tonic-gate 
13797c478bd9Sstevel@tonic-gate 				/* continue looking */
13807c478bd9Sstevel@tonic-gate 				page_unlock(pp);
13817c478bd9Sstevel@tonic-gate 				pp = pp->p_next;
13827c478bd9Sstevel@tonic-gate 				if (pp == first_pp)
13837c478bd9Sstevel@tonic-gate 					pp = NULL;
13847c478bd9Sstevel@tonic-gate 			}
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate 			if (pp != NULL) {
13877c478bd9Sstevel@tonic-gate 				ASSERT(mtype == PP_2_MTYPE(pp));
13887c478bd9Sstevel@tonic-gate 				ASSERT(pp->p_szc == 0);
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate 				/* found a page with specified DMA attributes */
13917c478bd9Sstevel@tonic-gate 				page_sub(&PAGE_CACHELISTS(mnode, bin,
13927c478bd9Sstevel@tonic-gate 				    mtype), pp);
1393affbd3ccSkchow 				page_ctr_sub(mnode, mtype, pp, PG_CACHE_LIST);
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate 				mutex_exit(pcm);
13967c478bd9Sstevel@tonic-gate 				ASSERT(pp->p_vnode);
13977c478bd9Sstevel@tonic-gate 				ASSERT(PP_ISAGED(pp) == 0);
13987c478bd9Sstevel@tonic-gate 				check_dma(dma_attr, pp, 1);
13997c478bd9Sstevel@tonic-gate 				VM_STAT_ADD(pga_vmstats.pgma_allocok);
14007c478bd9Sstevel@tonic-gate 				return (pp);
14017c478bd9Sstevel@tonic-gate 			}
14027c478bd9Sstevel@tonic-gate 			mutex_exit(pcm);
14037c478bd9Sstevel@tonic-gate nextcachebin:
14047c478bd9Sstevel@tonic-gate 			bin += (i == 0) ? BIN_STEP : 1;
14057c478bd9Sstevel@tonic-gate 			bin &= page_colors_mask;
14067c478bd9Sstevel@tonic-gate 		}
1407affbd3ccSkchow 		MTYPE_NEXT(mnode, mtype, flags);
1408affbd3ccSkchow 	} while (mtype >= 0);
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate 	VM_STAT_ADD(pga_vmstats.pgma_allocfailed);
14117c478bd9Sstevel@tonic-gate 	return (NULL);
14127c478bd9Sstevel@tonic-gate }
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate /*
14157c478bd9Sstevel@tonic-gate  * This function is similar to page_get_freelist()/page_get_cachelist()
14167c478bd9Sstevel@tonic-gate  * but it searches both the lists to find a page with the specified
14177c478bd9Sstevel@tonic-gate  * color (or no color) and DMA attributes. The search is done in the
14187c478bd9Sstevel@tonic-gate  * freelist first and then in the cache list within the highest memory
14197c478bd9Sstevel@tonic-gate  * range (based on DMA attributes) before searching in the lower
14207c478bd9Sstevel@tonic-gate  * memory ranges.
14217c478bd9Sstevel@tonic-gate  *
14227c478bd9Sstevel@tonic-gate  * Note: This function is called only by page_create_io().
14237c478bd9Sstevel@tonic-gate  */
14247c478bd9Sstevel@tonic-gate /*ARGSUSED*/
14257c478bd9Sstevel@tonic-gate page_t *
14267c478bd9Sstevel@tonic-gate page_get_anylist(struct vnode *vp, u_offset_t off, struct as *as, caddr_t vaddr,
14277c478bd9Sstevel@tonic-gate     size_t size, uint_t flags, ddi_dma_attr_t *dma_attr, lgrp_t	*lgrp)
14287c478bd9Sstevel@tonic-gate {
14297c478bd9Sstevel@tonic-gate 	uint_t		bin;
14307c478bd9Sstevel@tonic-gate 	int		mtype;
14317c478bd9Sstevel@tonic-gate 	page_t		*pp;
14327c478bd9Sstevel@tonic-gate 	int		n;
14337c478bd9Sstevel@tonic-gate 	int		m;
14347c478bd9Sstevel@tonic-gate 	int		szc;
14357c478bd9Sstevel@tonic-gate 	int		fullrange;
14367c478bd9Sstevel@tonic-gate 	int		mnode;
14377c478bd9Sstevel@tonic-gate 	int		local_failed_stat = 0;
14387c478bd9Sstevel@tonic-gate 	lgrp_mnode_cookie_t	lgrp_cookie;
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate 	VM_STAT_ADD(pga_vmstats.pga_alloc);
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate 	/* only base pagesize currently supported */
14437c478bd9Sstevel@tonic-gate 	if (size != MMU_PAGESIZE)
14447c478bd9Sstevel@tonic-gate 		return (NULL);
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate 	/*
14477c478bd9Sstevel@tonic-gate 	 * If we're passed a specific lgroup, we use it.  Otherwise,
14487c478bd9Sstevel@tonic-gate 	 * assume first-touch placement is desired.
14497c478bd9Sstevel@tonic-gate 	 */
14507c478bd9Sstevel@tonic-gate 	if (!LGRP_EXISTS(lgrp))
14517c478bd9Sstevel@tonic-gate 		lgrp = lgrp_home_lgrp();
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 	/* LINTED */
14547c478bd9Sstevel@tonic-gate 	AS_2_BIN(as, seg, vp, vaddr, bin);
14557c478bd9Sstevel@tonic-gate 
14567c478bd9Sstevel@tonic-gate 	/*
14577c478bd9Sstevel@tonic-gate 	 * Only hold one freelist or cachelist lock at a time, that way we
14587c478bd9Sstevel@tonic-gate 	 * can start anywhere and not have to worry about lock
14597c478bd9Sstevel@tonic-gate 	 * ordering.
14607c478bd9Sstevel@tonic-gate 	 */
14617c478bd9Sstevel@tonic-gate 	if (dma_attr == NULL) {
14627c478bd9Sstevel@tonic-gate 		n = 0;
14637c478bd9Sstevel@tonic-gate 		m = mnoderangecnt - 1;
14647c478bd9Sstevel@tonic-gate 		fullrange = 1;
14657c478bd9Sstevel@tonic-gate 		VM_STAT_ADD(pga_vmstats.pga_nulldmaattr);
14667c478bd9Sstevel@tonic-gate 	} else {
14677c478bd9Sstevel@tonic-gate 		pfn_t pfnlo = mmu_btop(dma_attr->dma_attr_addr_lo);
14687c478bd9Sstevel@tonic-gate 		pfn_t pfnhi = mmu_btop(dma_attr->dma_attr_addr_hi);
14697c478bd9Sstevel@tonic-gate 
14707c478bd9Sstevel@tonic-gate 		/*
14717c478bd9Sstevel@tonic-gate 		 * We can guarantee alignment only for page boundary.
14727c478bd9Sstevel@tonic-gate 		 */
14737c478bd9Sstevel@tonic-gate 		if (dma_attr->dma_attr_align > MMU_PAGESIZE)
14747c478bd9Sstevel@tonic-gate 			return (NULL);
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate 		n = pfn_2_mtype(pfnlo);
14777c478bd9Sstevel@tonic-gate 		m = pfn_2_mtype(pfnhi);
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate 		fullrange = ((pfnlo == mnoderanges[n].mnr_pfnlo) &&
14807c478bd9Sstevel@tonic-gate 		    (pfnhi >= mnoderanges[m].mnr_pfnhi));
14817c478bd9Sstevel@tonic-gate 	}
14827c478bd9Sstevel@tonic-gate 	VM_STAT_COND_ADD(fullrange == 0, pga_vmstats.pga_notfullrange);
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate 	if (n > m)
14857c478bd9Sstevel@tonic-gate 		return (NULL);
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate 	szc = 0;
14887c478bd9Sstevel@tonic-gate 
14897c478bd9Sstevel@tonic-gate 	/* cylcing thru mtype handled by RANGE0 if n == 0 */
14907c478bd9Sstevel@tonic-gate 	if (n == 0) {
14917c478bd9Sstevel@tonic-gate 		flags |= PGI_MT_RANGE0;
14927c478bd9Sstevel@tonic-gate 		n = m;
14937c478bd9Sstevel@tonic-gate 	}
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate 	/*
14967c478bd9Sstevel@tonic-gate 	 * Try local memory node first, but try remote if we can't
14977c478bd9Sstevel@tonic-gate 	 * get a page of the right color.
14987c478bd9Sstevel@tonic-gate 	 */
14997c478bd9Sstevel@tonic-gate 	LGRP_MNODE_COOKIE_INIT(lgrp_cookie, lgrp, LGRP_SRCH_HIER);
15007c478bd9Sstevel@tonic-gate 	while ((mnode = lgrp_memnode_choose(&lgrp_cookie)) >= 0) {
15017c478bd9Sstevel@tonic-gate 		/*
15027c478bd9Sstevel@tonic-gate 		 * allocate pages from high pfn to low.
15037c478bd9Sstevel@tonic-gate 		 */
15047c478bd9Sstevel@tonic-gate 		for (mtype = m; mtype >= n; mtype--) {
15057c478bd9Sstevel@tonic-gate 			if (fullrange != 0) {
15067c478bd9Sstevel@tonic-gate 				pp = page_get_mnode_freelist(mnode,
15077c478bd9Sstevel@tonic-gate 				    bin, mtype, szc, flags);
15087c478bd9Sstevel@tonic-gate 				if (pp == NULL) {
15097c478bd9Sstevel@tonic-gate 					pp = page_get_mnode_cachelist(
15107c478bd9Sstevel@tonic-gate 						bin, flags, mnode, mtype);
15117c478bd9Sstevel@tonic-gate 				}
15127c478bd9Sstevel@tonic-gate 			} else {
15137c478bd9Sstevel@tonic-gate 				pp = page_get_mnode_anylist(bin, szc,
15147c478bd9Sstevel@tonic-gate 				    flags, mnode, mtype, dma_attr);
15157c478bd9Sstevel@tonic-gate 			}
15167c478bd9Sstevel@tonic-gate 			if (pp != NULL) {
15177c478bd9Sstevel@tonic-gate 				VM_STAT_ADD(pga_vmstats.pga_allocok);
15187c478bd9Sstevel@tonic-gate 				check_dma(dma_attr, pp, 1);
15197c478bd9Sstevel@tonic-gate 				return (pp);
15207c478bd9Sstevel@tonic-gate 			}
15217c478bd9Sstevel@tonic-gate 		}
15227c478bd9Sstevel@tonic-gate 		if (!local_failed_stat) {
15237c478bd9Sstevel@tonic-gate 			lgrp_stat_add(lgrp->lgrp_id, LGRP_NUM_ALLOC_FAIL, 1);
15247c478bd9Sstevel@tonic-gate 			local_failed_stat = 1;
15257c478bd9Sstevel@tonic-gate 		}
15267c478bd9Sstevel@tonic-gate 	}
15277c478bd9Sstevel@tonic-gate 	VM_STAT_ADD(pga_vmstats.pga_allocfailed);
15287c478bd9Sstevel@tonic-gate 
15297c478bd9Sstevel@tonic-gate 	return (NULL);
15307c478bd9Sstevel@tonic-gate }
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate /*
15337c478bd9Sstevel@tonic-gate  * page_create_io()
15347c478bd9Sstevel@tonic-gate  *
15357c478bd9Sstevel@tonic-gate  * This function is a copy of page_create_va() with an additional
15367c478bd9Sstevel@tonic-gate  * argument 'mattr' that specifies DMA memory requirements to
15377c478bd9Sstevel@tonic-gate  * the page list functions. This function is used by the segkmem
15387c478bd9Sstevel@tonic-gate  * allocator so it is only to create new pages (i.e PG_EXCL is
15397c478bd9Sstevel@tonic-gate  * set).
15407c478bd9Sstevel@tonic-gate  *
15417c478bd9Sstevel@tonic-gate  * Note: This interface is currently used by x86 PSM only and is
15427c478bd9Sstevel@tonic-gate  *	 not fully specified so the commitment level is only for
15437c478bd9Sstevel@tonic-gate  *	 private interface specific to x86. This interface uses PSM
15447c478bd9Sstevel@tonic-gate  *	 specific page_get_anylist() interface.
15457c478bd9Sstevel@tonic-gate  */
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate #define	PAGE_HASH_SEARCH(index, pp, vp, off) { \
15487c478bd9Sstevel@tonic-gate 	for ((pp) = page_hash[(index)]; (pp); (pp) = (pp)->p_hash) { \
15497c478bd9Sstevel@tonic-gate 		if ((pp)->p_vnode == (vp) && (pp)->p_offset == (off)) \
15507c478bd9Sstevel@tonic-gate 			break; \
15517c478bd9Sstevel@tonic-gate 	} \
15527c478bd9Sstevel@tonic-gate }
15537c478bd9Sstevel@tonic-gate 
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate page_t *
15567c478bd9Sstevel@tonic-gate page_create_io(
15577c478bd9Sstevel@tonic-gate 	struct vnode	*vp,
15587c478bd9Sstevel@tonic-gate 	u_offset_t	off,
15597c478bd9Sstevel@tonic-gate 	uint_t		bytes,
15607c478bd9Sstevel@tonic-gate 	uint_t		flags,
15617c478bd9Sstevel@tonic-gate 	struct as	*as,
15627c478bd9Sstevel@tonic-gate 	caddr_t		vaddr,
15637c478bd9Sstevel@tonic-gate 	ddi_dma_attr_t	*mattr)	/* DMA memory attributes if any */
15647c478bd9Sstevel@tonic-gate {
15657c478bd9Sstevel@tonic-gate 	page_t		*plist = NULL;
15667c478bd9Sstevel@tonic-gate 	uint_t		plist_len = 0;
15677c478bd9Sstevel@tonic-gate 	pgcnt_t		npages;
15687c478bd9Sstevel@tonic-gate 	page_t		*npp = NULL;
15697c478bd9Sstevel@tonic-gate 	uint_t		pages_req;
15707c478bd9Sstevel@tonic-gate 	page_t		*pp;
15717c478bd9Sstevel@tonic-gate 	kmutex_t	*phm = NULL;
15727c478bd9Sstevel@tonic-gate 	uint_t		index;
15737c478bd9Sstevel@tonic-gate 
15747c478bd9Sstevel@tonic-gate 	TRACE_4(TR_FAC_VM, TR_PAGE_CREATE_START,
15757c478bd9Sstevel@tonic-gate 		"page_create_start:vp %p off %llx bytes %u flags %x",
15767c478bd9Sstevel@tonic-gate 		vp, off, bytes, flags);
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate 	ASSERT((flags & ~(PG_EXCL | PG_WAIT | PG_PHYSCONTIG)) == 0);
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate 	pages_req = npages = mmu_btopr(bytes);
15817c478bd9Sstevel@tonic-gate 
15827c478bd9Sstevel@tonic-gate 	/*
15837c478bd9Sstevel@tonic-gate 	 * Do the freemem and pcf accounting.
15847c478bd9Sstevel@tonic-gate 	 */
15857c478bd9Sstevel@tonic-gate 	if (!page_create_wait(npages, flags)) {
15867c478bd9Sstevel@tonic-gate 		return (NULL);
15877c478bd9Sstevel@tonic-gate 	}
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_VM, TR_PAGE_CREATE_SUCCESS,
15907c478bd9Sstevel@tonic-gate 		"page_create_success:vp %p off %llx",
15917c478bd9Sstevel@tonic-gate 		vp, off);
15927c478bd9Sstevel@tonic-gate 
15937c478bd9Sstevel@tonic-gate 	/*
15947c478bd9Sstevel@tonic-gate 	 * If satisfying this request has left us with too little
15957c478bd9Sstevel@tonic-gate 	 * memory, start the wheels turning to get some back.  The
15967c478bd9Sstevel@tonic-gate 	 * first clause of the test prevents waking up the pageout
15977c478bd9Sstevel@tonic-gate 	 * daemon in situations where it would decide that there's
15987c478bd9Sstevel@tonic-gate 	 * nothing to do.
15997c478bd9Sstevel@tonic-gate 	 */
16007c478bd9Sstevel@tonic-gate 	if (nscan < desscan && freemem < minfree) {
16017c478bd9Sstevel@tonic-gate 		TRACE_1(TR_FAC_VM, TR_PAGEOUT_CV_SIGNAL,
16027c478bd9Sstevel@tonic-gate 			"pageout_cv_signal:freemem %ld", freemem);
16037c478bd9Sstevel@tonic-gate 		cv_signal(&proc_pageout->p_cv);
16047c478bd9Sstevel@tonic-gate 	}
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate 	if (flags & PG_PHYSCONTIG) {
16077c478bd9Sstevel@tonic-gate 
16087c478bd9Sstevel@tonic-gate 		plist = page_get_contigpage(&npages, mattr, 1);
16097c478bd9Sstevel@tonic-gate 		if (plist == NULL) {
16107c478bd9Sstevel@tonic-gate 			page_create_putback(npages);
16117c478bd9Sstevel@tonic-gate 			return (NULL);
16127c478bd9Sstevel@tonic-gate 		}
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate 		pp = plist;
16157c478bd9Sstevel@tonic-gate 
16167c478bd9Sstevel@tonic-gate 		do {
16177c478bd9Sstevel@tonic-gate 			if (!page_hashin(pp, vp, off, NULL)) {
16187c478bd9Sstevel@tonic-gate 				panic("pg_creat_io: hashin failed %p %p %llx",
16197c478bd9Sstevel@tonic-gate 				    (void *)pp, (void *)vp, off);
16207c478bd9Sstevel@tonic-gate 			}
16217c478bd9Sstevel@tonic-gate 			VM_STAT_ADD(page_create_new);
16227c478bd9Sstevel@tonic-gate 			off += MMU_PAGESIZE;
16237c478bd9Sstevel@tonic-gate 			PP_CLRFREE(pp);
16247c478bd9Sstevel@tonic-gate 			PP_CLRAGED(pp);
16257c478bd9Sstevel@tonic-gate 			page_set_props(pp, P_REF);
16267c478bd9Sstevel@tonic-gate 			pp = pp->p_next;
16277c478bd9Sstevel@tonic-gate 		} while (pp != plist);
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate 		if (!npages) {
16307c478bd9Sstevel@tonic-gate 			check_dma(mattr, plist, pages_req);
16317c478bd9Sstevel@tonic-gate 			return (plist);
16327c478bd9Sstevel@tonic-gate 		} else {
16337c478bd9Sstevel@tonic-gate 			vaddr += (pages_req - npages) << MMU_PAGESHIFT;
16347c478bd9Sstevel@tonic-gate 		}
16357c478bd9Sstevel@tonic-gate 
16367c478bd9Sstevel@tonic-gate 		/*
16377c478bd9Sstevel@tonic-gate 		 * fall-thru:
16387c478bd9Sstevel@tonic-gate 		 *
16397c478bd9Sstevel@tonic-gate 		 * page_get_contigpage returns when npages <= sgllen.
16407c478bd9Sstevel@tonic-gate 		 * Grab the rest of the non-contig pages below from anylist.
16417c478bd9Sstevel@tonic-gate 		 */
16427c478bd9Sstevel@tonic-gate 	}
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate 	/*
16457c478bd9Sstevel@tonic-gate 	 * Loop around collecting the requested number of pages.
16467c478bd9Sstevel@tonic-gate 	 * Most of the time, we have to `create' a new page. With
16477c478bd9Sstevel@tonic-gate 	 * this in mind, pull the page off the free list before
16487c478bd9Sstevel@tonic-gate 	 * getting the hash lock.  This will minimize the hash
16497c478bd9Sstevel@tonic-gate 	 * lock hold time, nesting, and the like.  If it turns
16507c478bd9Sstevel@tonic-gate 	 * out we don't need the page, we put it back at the end.
16517c478bd9Sstevel@tonic-gate 	 */
16527c478bd9Sstevel@tonic-gate 	while (npages--) {
16537c478bd9Sstevel@tonic-gate 		phm = NULL;
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 		index = PAGE_HASH_FUNC(vp, off);
16567c478bd9Sstevel@tonic-gate top:
16577c478bd9Sstevel@tonic-gate 		ASSERT(phm == NULL);
16587c478bd9Sstevel@tonic-gate 		ASSERT(index == PAGE_HASH_FUNC(vp, off));
16597c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp)));
16607c478bd9Sstevel@tonic-gate 
16617c478bd9Sstevel@tonic-gate 		if (npp == NULL) {
16627c478bd9Sstevel@tonic-gate 			/*
16637c478bd9Sstevel@tonic-gate 			 * Try to get the page of any color either from
16647c478bd9Sstevel@tonic-gate 			 * the freelist or from the cache list.
16657c478bd9Sstevel@tonic-gate 			 */
16667c478bd9Sstevel@tonic-gate 			npp = page_get_anylist(vp, off, as, vaddr, MMU_PAGESIZE,
16677c478bd9Sstevel@tonic-gate 			    flags & ~PG_MATCH_COLOR, mattr, NULL);
16687c478bd9Sstevel@tonic-gate 			if (npp == NULL) {
16697c478bd9Sstevel@tonic-gate 				if (mattr == NULL) {
16707c478bd9Sstevel@tonic-gate 					/*
16717c478bd9Sstevel@tonic-gate 					 * Not looking for a special page;
16727c478bd9Sstevel@tonic-gate 					 * panic!
16737c478bd9Sstevel@tonic-gate 					 */
16747c478bd9Sstevel@tonic-gate 					panic("no page found %d", (int)npages);
16757c478bd9Sstevel@tonic-gate 				}
16767c478bd9Sstevel@tonic-gate 				/*
16777c478bd9Sstevel@tonic-gate 				 * No page found! This can happen
16787c478bd9Sstevel@tonic-gate 				 * if we are looking for a page
16797c478bd9Sstevel@tonic-gate 				 * within a specific memory range
16807c478bd9Sstevel@tonic-gate 				 * for DMA purposes. If PG_WAIT is
16817c478bd9Sstevel@tonic-gate 				 * specified then we wait for a
16827c478bd9Sstevel@tonic-gate 				 * while and then try again. The
16837c478bd9Sstevel@tonic-gate 				 * wait could be forever if we
16847c478bd9Sstevel@tonic-gate 				 * don't get the page(s) we need.
16857c478bd9Sstevel@tonic-gate 				 *
16867c478bd9Sstevel@tonic-gate 				 * Note: XXX We really need a mechanism
16877c478bd9Sstevel@tonic-gate 				 * to wait for pages in the desired
16887c478bd9Sstevel@tonic-gate 				 * range. For now, we wait for any
16897c478bd9Sstevel@tonic-gate 				 * pages and see if we can use it.
16907c478bd9Sstevel@tonic-gate 				 */
16917c478bd9Sstevel@tonic-gate 
16927c478bd9Sstevel@tonic-gate 				if ((mattr != NULL) && (flags & PG_WAIT)) {
16937c478bd9Sstevel@tonic-gate 					delay(10);
16947c478bd9Sstevel@tonic-gate 					goto top;
16957c478bd9Sstevel@tonic-gate 				}
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate 				goto fail; /* undo accounting stuff */
16987c478bd9Sstevel@tonic-gate 			}
16997c478bd9Sstevel@tonic-gate 
17007c478bd9Sstevel@tonic-gate 			if (PP_ISAGED(npp) == 0) {
17017c478bd9Sstevel@tonic-gate 				/*
17027c478bd9Sstevel@tonic-gate 				 * Since this page came from the
17037c478bd9Sstevel@tonic-gate 				 * cachelist, we must destroy the
17047c478bd9Sstevel@tonic-gate 				 * old vnode association.
17057c478bd9Sstevel@tonic-gate 				 */
17067c478bd9Sstevel@tonic-gate 				page_hashout(npp, (kmutex_t *)NULL);
17077c478bd9Sstevel@tonic-gate 			}
17087c478bd9Sstevel@tonic-gate 		}
17097c478bd9Sstevel@tonic-gate 
17107c478bd9Sstevel@tonic-gate 		/*
17117c478bd9Sstevel@tonic-gate 		 * We own this page!
17127c478bd9Sstevel@tonic-gate 		 */
17137c478bd9Sstevel@tonic-gate 		ASSERT(PAGE_EXCL(npp));
17147c478bd9Sstevel@tonic-gate 		ASSERT(npp->p_vnode == NULL);
17157c478bd9Sstevel@tonic-gate 		ASSERT(!hat_page_is_mapped(npp));
17167c478bd9Sstevel@tonic-gate 		PP_CLRFREE(npp);
17177c478bd9Sstevel@tonic-gate 		PP_CLRAGED(npp);
17187c478bd9Sstevel@tonic-gate 
17197c478bd9Sstevel@tonic-gate 		/*
17207c478bd9Sstevel@tonic-gate 		 * Here we have a page in our hot little mits and are
17217c478bd9Sstevel@tonic-gate 		 * just waiting to stuff it on the appropriate lists.
17227c478bd9Sstevel@tonic-gate 		 * Get the mutex and check to see if it really does
17237c478bd9Sstevel@tonic-gate 		 * not exist.
17247c478bd9Sstevel@tonic-gate 		 */
17257c478bd9Sstevel@tonic-gate 		phm = PAGE_HASH_MUTEX(index);
17267c478bd9Sstevel@tonic-gate 		mutex_enter(phm);
17277c478bd9Sstevel@tonic-gate 		PAGE_HASH_SEARCH(index, pp, vp, off);
17287c478bd9Sstevel@tonic-gate 		if (pp == NULL) {
17297c478bd9Sstevel@tonic-gate 			VM_STAT_ADD(page_create_new);
17307c478bd9Sstevel@tonic-gate 			pp = npp;
17317c478bd9Sstevel@tonic-gate 			npp = NULL;
17327c478bd9Sstevel@tonic-gate 			if (!page_hashin(pp, vp, off, phm)) {
17337c478bd9Sstevel@tonic-gate 				/*
17347c478bd9Sstevel@tonic-gate 				 * Since we hold the page hash mutex and
17357c478bd9Sstevel@tonic-gate 				 * just searched for this page, page_hashin
17367c478bd9Sstevel@tonic-gate 				 * had better not fail.  If it does, that
17377c478bd9Sstevel@tonic-gate 				 * means somethread did not follow the
17387c478bd9Sstevel@tonic-gate 				 * page hash mutex rules.  Panic now and
17397c478bd9Sstevel@tonic-gate 				 * get it over with.  As usual, go down
17407c478bd9Sstevel@tonic-gate 				 * holding all the locks.
17417c478bd9Sstevel@tonic-gate 				 */
17427c478bd9Sstevel@tonic-gate 				ASSERT(MUTEX_HELD(phm));
17437c478bd9Sstevel@tonic-gate 				panic("page_create: hashin fail %p %p %llx %p",
17447c478bd9Sstevel@tonic-gate 				    (void *)pp, (void *)vp, off, (void *)phm);
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate 			}
17477c478bd9Sstevel@tonic-gate 			ASSERT(MUTEX_HELD(phm));
17487c478bd9Sstevel@tonic-gate 			mutex_exit(phm);
17497c478bd9Sstevel@tonic-gate 			phm = NULL;
17507c478bd9Sstevel@tonic-gate 
17517c478bd9Sstevel@tonic-gate 			/*
17527c478bd9Sstevel@tonic-gate 			 * Hat layer locking need not be done to set
17537c478bd9Sstevel@tonic-gate 			 * the following bits since the page is not hashed
17547c478bd9Sstevel@tonic-gate 			 * and was on the free list (i.e., had no mappings).
17557c478bd9Sstevel@tonic-gate 			 *
17567c478bd9Sstevel@tonic-gate 			 * Set the reference bit to protect
17577c478bd9Sstevel@tonic-gate 			 * against immediate pageout
17587c478bd9Sstevel@tonic-gate 			 *
17597c478bd9Sstevel@tonic-gate 			 * XXXmh modify freelist code to set reference
17607c478bd9Sstevel@tonic-gate 			 * bit so we don't have to do it here.
17617c478bd9Sstevel@tonic-gate 			 */
17627c478bd9Sstevel@tonic-gate 			page_set_props(pp, P_REF);
17637c478bd9Sstevel@tonic-gate 		} else {
17647c478bd9Sstevel@tonic-gate 			ASSERT(MUTEX_HELD(phm));
17657c478bd9Sstevel@tonic-gate 			mutex_exit(phm);
17667c478bd9Sstevel@tonic-gate 			phm = NULL;
17677c478bd9Sstevel@tonic-gate 			/*
17687c478bd9Sstevel@tonic-gate 			 * NOTE: This should not happen for pages associated
17697c478bd9Sstevel@tonic-gate 			 *	 with kernel vnode 'kvp'.
17707c478bd9Sstevel@tonic-gate 			 */
17717c478bd9Sstevel@tonic-gate 			/* XX64 - to debug why this happens! */
17727c478bd9Sstevel@tonic-gate 			ASSERT(vp != &kvp);
17737c478bd9Sstevel@tonic-gate 			if (vp == &kvp)
17747c478bd9Sstevel@tonic-gate 				cmn_err(CE_NOTE,
17757c478bd9Sstevel@tonic-gate 				    "page_create: page not expected "
17767c478bd9Sstevel@tonic-gate 				    "in hash list for kernel vnode - pp 0x%p",
17777c478bd9Sstevel@tonic-gate 				    (void *)pp);
17787c478bd9Sstevel@tonic-gate 			VM_STAT_ADD(page_create_exists);
17797c478bd9Sstevel@tonic-gate 			goto fail;
17807c478bd9Sstevel@tonic-gate 		}
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate 		/*
17837c478bd9Sstevel@tonic-gate 		 * Got a page!  It is locked.  Acquire the i/o
17847c478bd9Sstevel@tonic-gate 		 * lock since we are going to use the p_next and
17857c478bd9Sstevel@tonic-gate 		 * p_prev fields to link the requested pages together.
17867c478bd9Sstevel@tonic-gate 		 */
17877c478bd9Sstevel@tonic-gate 		page_io_lock(pp);
17887c478bd9Sstevel@tonic-gate 		page_add(&plist, pp);
17897c478bd9Sstevel@tonic-gate 		plist = plist->p_next;
17907c478bd9Sstevel@tonic-gate 		off += MMU_PAGESIZE;
17917c478bd9Sstevel@tonic-gate 		vaddr += MMU_PAGESIZE;
17927c478bd9Sstevel@tonic-gate 	}
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate 	check_dma(mattr, plist, pages_req);
17957c478bd9Sstevel@tonic-gate 	return (plist);
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate fail:
17987c478bd9Sstevel@tonic-gate 	if (npp != NULL) {
17997c478bd9Sstevel@tonic-gate 		/*
18007c478bd9Sstevel@tonic-gate 		 * Did not need this page after all.
18017c478bd9Sstevel@tonic-gate 		 * Put it back on the free list.
18027c478bd9Sstevel@tonic-gate 		 */
18037c478bd9Sstevel@tonic-gate 		VM_STAT_ADD(page_create_putbacks);
18047c478bd9Sstevel@tonic-gate 		PP_SETFREE(npp);
18057c478bd9Sstevel@tonic-gate 		PP_SETAGED(npp);
18067c478bd9Sstevel@tonic-gate 		npp->p_offset = (u_offset_t)-1;
18077c478bd9Sstevel@tonic-gate 		page_list_add(npp, PG_FREE_LIST | PG_LIST_TAIL);
18087c478bd9Sstevel@tonic-gate 		page_unlock(npp);
18097c478bd9Sstevel@tonic-gate 	}
18107c478bd9Sstevel@tonic-gate 
18117c478bd9Sstevel@tonic-gate 	/*
18127c478bd9Sstevel@tonic-gate 	 * Give up the pages we already got.
18137c478bd9Sstevel@tonic-gate 	 */
18147c478bd9Sstevel@tonic-gate 	while (plist != NULL) {
18157c478bd9Sstevel@tonic-gate 		pp = plist;
18167c478bd9Sstevel@tonic-gate 		page_sub(&plist, pp);
18177c478bd9Sstevel@tonic-gate 		page_io_unlock(pp);
18187c478bd9Sstevel@tonic-gate 		plist_len++;
18197c478bd9Sstevel@tonic-gate 		/*LINTED: constant in conditional ctx*/
18207c478bd9Sstevel@tonic-gate 		VN_DISPOSE(pp, B_INVAL, 0, kcred);
18217c478bd9Sstevel@tonic-gate 	}
18227c478bd9Sstevel@tonic-gate 
18237c478bd9Sstevel@tonic-gate 	/*
18247c478bd9Sstevel@tonic-gate 	 * VN_DISPOSE does freemem accounting for the pages in plist
18257c478bd9Sstevel@tonic-gate 	 * by calling page_free. So, we need to undo the pcf accounting
18267c478bd9Sstevel@tonic-gate 	 * for only the remaining pages.
18277c478bd9Sstevel@tonic-gate 	 */
18287c478bd9Sstevel@tonic-gate 	VM_STAT_ADD(page_create_putbacks);
18297c478bd9Sstevel@tonic-gate 	page_create_putback(pages_req - plist_len);
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate 	return (NULL);
18327c478bd9Sstevel@tonic-gate }
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate 
18357c478bd9Sstevel@tonic-gate /*
18367c478bd9Sstevel@tonic-gate  * Copy the data from the physical page represented by "frompp" to
18377c478bd9Sstevel@tonic-gate  * that represented by "topp". ppcopy uses CPU->cpu_caddr1 and
18387c478bd9Sstevel@tonic-gate  * CPU->cpu_caddr2.  It assumes that no one uses either map at interrupt
18397c478bd9Sstevel@tonic-gate  * level and no one sleeps with an active mapping there.
18407c478bd9Sstevel@tonic-gate  *
18417c478bd9Sstevel@tonic-gate  * Note that the ref/mod bits in the page_t's are not affected by
18427c478bd9Sstevel@tonic-gate  * this operation, hence it is up to the caller to update them appropriately.
18437c478bd9Sstevel@tonic-gate  */
18447c478bd9Sstevel@tonic-gate void
18457c478bd9Sstevel@tonic-gate ppcopy(page_t *frompp, page_t *topp)
18467c478bd9Sstevel@tonic-gate {
18477c478bd9Sstevel@tonic-gate 	caddr_t		pp_addr1;
18487c478bd9Sstevel@tonic-gate 	caddr_t		pp_addr2;
18497c478bd9Sstevel@tonic-gate 	void		*pte1;
18507c478bd9Sstevel@tonic-gate 	void		*pte2;
18517c478bd9Sstevel@tonic-gate 	kmutex_t	*ppaddr_mutex;
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate 	ASSERT_STACK_ALIGNED();
18547c478bd9Sstevel@tonic-gate 	ASSERT(PAGE_LOCKED(frompp));
18557c478bd9Sstevel@tonic-gate 	ASSERT(PAGE_LOCKED(topp));
18567c478bd9Sstevel@tonic-gate 
18577c478bd9Sstevel@tonic-gate 	if (kpm_enable) {
18587c478bd9Sstevel@tonic-gate 		pp_addr1 = hat_kpm_page2va(frompp, 0);
18597c478bd9Sstevel@tonic-gate 		pp_addr2 = hat_kpm_page2va(topp, 0);
18607c478bd9Sstevel@tonic-gate 		kpreempt_disable();
18617c478bd9Sstevel@tonic-gate 	} else {
18627c478bd9Sstevel@tonic-gate 		/*
18637c478bd9Sstevel@tonic-gate 		 * disable pre-emption so that CPU can't change
18647c478bd9Sstevel@tonic-gate 		 */
18657c478bd9Sstevel@tonic-gate 		kpreempt_disable();
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate 		pp_addr1 = CPU->cpu_caddr1;
18687c478bd9Sstevel@tonic-gate 		pp_addr2 = CPU->cpu_caddr2;
18697c478bd9Sstevel@tonic-gate 		pte1 = (void *)CPU->cpu_caddr1pte;
18707c478bd9Sstevel@tonic-gate 		pte2 = (void *)CPU->cpu_caddr2pte;
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate 		ppaddr_mutex = &CPU->cpu_ppaddr_mutex;
18737c478bd9Sstevel@tonic-gate 		mutex_enter(ppaddr_mutex);
18747c478bd9Sstevel@tonic-gate 
18757c478bd9Sstevel@tonic-gate 		hat_mempte_remap(page_pptonum(frompp), pp_addr1, pte1,
18767c478bd9Sstevel@tonic-gate 		    PROT_READ | HAT_STORECACHING_OK, HAT_LOAD_NOCONSIST);
18777c478bd9Sstevel@tonic-gate 		hat_mempte_remap(page_pptonum(topp), pp_addr2, pte2,
18787c478bd9Sstevel@tonic-gate 		    PROT_READ | PROT_WRITE | HAT_STORECACHING_OK,
18797c478bd9Sstevel@tonic-gate 		    HAT_LOAD_NOCONSIST);
18807c478bd9Sstevel@tonic-gate 	}
18817c478bd9Sstevel@tonic-gate 
18827c478bd9Sstevel@tonic-gate 	if (use_sse_pagecopy)
18837c478bd9Sstevel@tonic-gate 		hwblkpagecopy(pp_addr1, pp_addr2);
18847c478bd9Sstevel@tonic-gate 	else
18857c478bd9Sstevel@tonic-gate 		bcopy(pp_addr1, pp_addr2, PAGESIZE);
18867c478bd9Sstevel@tonic-gate 
18877c478bd9Sstevel@tonic-gate 	if (!kpm_enable)
18887c478bd9Sstevel@tonic-gate 		mutex_exit(ppaddr_mutex);
18897c478bd9Sstevel@tonic-gate 	kpreempt_enable();
18907c478bd9Sstevel@tonic-gate }
18917c478bd9Sstevel@tonic-gate 
18927c478bd9Sstevel@tonic-gate /*
18937c478bd9Sstevel@tonic-gate  * Zero the physical page from off to off + len given by `pp'
18947c478bd9Sstevel@tonic-gate  * without changing the reference and modified bits of page.
18957c478bd9Sstevel@tonic-gate  *
18967c478bd9Sstevel@tonic-gate  * We use this using CPU private page address #2, see ppcopy() for more info.
18977c478bd9Sstevel@tonic-gate  * pagezero() must not be called at interrupt level.
18987c478bd9Sstevel@tonic-gate  */
18997c478bd9Sstevel@tonic-gate void
19007c478bd9Sstevel@tonic-gate pagezero(page_t *pp, uint_t off, uint_t len)
19017c478bd9Sstevel@tonic-gate {
19027c478bd9Sstevel@tonic-gate 	caddr_t		pp_addr2;
19037c478bd9Sstevel@tonic-gate 	void		*pte2;
19047c478bd9Sstevel@tonic-gate 	kmutex_t	*ppaddr_mutex;
19057c478bd9Sstevel@tonic-gate 
19067c478bd9Sstevel@tonic-gate 	ASSERT_STACK_ALIGNED();
19077c478bd9Sstevel@tonic-gate 	ASSERT(len <= MMU_PAGESIZE);
19087c478bd9Sstevel@tonic-gate 	ASSERT(off <= MMU_PAGESIZE);
19097c478bd9Sstevel@tonic-gate 	ASSERT(off + len <= MMU_PAGESIZE);
19107c478bd9Sstevel@tonic-gate 	ASSERT(PAGE_LOCKED(pp));
19117c478bd9Sstevel@tonic-gate 
19127c478bd9Sstevel@tonic-gate 	if (kpm_enable) {
19137c478bd9Sstevel@tonic-gate 		pp_addr2 = hat_kpm_page2va(pp, 0);
19147c478bd9Sstevel@tonic-gate 		kpreempt_disable();
19157c478bd9Sstevel@tonic-gate 	} else {
19167c478bd9Sstevel@tonic-gate 		kpreempt_disable();
19177c478bd9Sstevel@tonic-gate 
19187c478bd9Sstevel@tonic-gate 		pp_addr2 = CPU->cpu_caddr2;
19197c478bd9Sstevel@tonic-gate 		pte2 = (void *)CPU->cpu_caddr2pte;
19207c478bd9Sstevel@tonic-gate 
19217c478bd9Sstevel@tonic-gate 		ppaddr_mutex = &CPU->cpu_ppaddr_mutex;
19227c478bd9Sstevel@tonic-gate 		mutex_enter(ppaddr_mutex);
19237c478bd9Sstevel@tonic-gate 
19247c478bd9Sstevel@tonic-gate 		hat_mempte_remap(page_pptonum(pp), pp_addr2, pte2,
19257c478bd9Sstevel@tonic-gate 		    PROT_READ | PROT_WRITE | HAT_STORECACHING_OK,
19267c478bd9Sstevel@tonic-gate 		    HAT_LOAD_NOCONSIST);
19277c478bd9Sstevel@tonic-gate 	}
19287c478bd9Sstevel@tonic-gate 
19297c478bd9Sstevel@tonic-gate 	if (use_sse_pagezero)
19307c478bd9Sstevel@tonic-gate 		hwblkclr(pp_addr2 + off, len);
19317c478bd9Sstevel@tonic-gate 	else
19327c478bd9Sstevel@tonic-gate 		bzero(pp_addr2 + off, len);
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate 	if (!kpm_enable)
19357c478bd9Sstevel@tonic-gate 		mutex_exit(ppaddr_mutex);
19367c478bd9Sstevel@tonic-gate 	kpreempt_enable();
19377c478bd9Sstevel@tonic-gate }
19387c478bd9Sstevel@tonic-gate 
19397c478bd9Sstevel@tonic-gate /*
19407c478bd9Sstevel@tonic-gate  * Platform-dependent page scrub call.
19417c478bd9Sstevel@tonic-gate  */
19427c478bd9Sstevel@tonic-gate void
19437c478bd9Sstevel@tonic-gate pagescrub(page_t *pp, uint_t off, uint_t len)
19447c478bd9Sstevel@tonic-gate {
19457c478bd9Sstevel@tonic-gate 	/*
19467c478bd9Sstevel@tonic-gate 	 * For now, we rely on the fact that pagezero() will
19477c478bd9Sstevel@tonic-gate 	 * always clear UEs.
19487c478bd9Sstevel@tonic-gate 	 */
19497c478bd9Sstevel@tonic-gate 	pagezero(pp, off, len);
19507c478bd9Sstevel@tonic-gate }
19517c478bd9Sstevel@tonic-gate 
19527c478bd9Sstevel@tonic-gate /*
19537c478bd9Sstevel@tonic-gate  * set up two private addresses for use on a given CPU for use in ppcopy()
19547c478bd9Sstevel@tonic-gate  */
19557c478bd9Sstevel@tonic-gate void
19567c478bd9Sstevel@tonic-gate setup_vaddr_for_ppcopy(struct cpu *cpup)
19577c478bd9Sstevel@tonic-gate {
19587c478bd9Sstevel@tonic-gate 	void *addr;
19597c478bd9Sstevel@tonic-gate 	void *pte;
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate 	addr = vmem_alloc(heap_arena, mmu_ptob(1), VM_SLEEP);
19627c478bd9Sstevel@tonic-gate 	pte = hat_mempte_setup(addr);
19637c478bd9Sstevel@tonic-gate 	cpup->cpu_caddr1 = addr;
19647c478bd9Sstevel@tonic-gate 	cpup->cpu_caddr1pte = (pteptr_t)pte;
19657c478bd9Sstevel@tonic-gate 
19667c478bd9Sstevel@tonic-gate 	addr = vmem_alloc(heap_arena, mmu_ptob(1), VM_SLEEP);
19677c478bd9Sstevel@tonic-gate 	pte = hat_mempte_setup(addr);
19687c478bd9Sstevel@tonic-gate 	cpup->cpu_caddr2 = addr;
19697c478bd9Sstevel@tonic-gate 	cpup->cpu_caddr2pte = (pteptr_t)pte;
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate 	mutex_init(&cpup->cpu_ppaddr_mutex, NULL, MUTEX_DEFAULT, NULL);
19727c478bd9Sstevel@tonic-gate }
19737c478bd9Sstevel@tonic-gate 
19747c478bd9Sstevel@tonic-gate 
19757c478bd9Sstevel@tonic-gate /*
19767c478bd9Sstevel@tonic-gate  * Create the pageout scanner thread. The thread has to
19777c478bd9Sstevel@tonic-gate  * start at procedure with process pp and priority pri.
19787c478bd9Sstevel@tonic-gate  */
19797c478bd9Sstevel@tonic-gate void
19807c478bd9Sstevel@tonic-gate pageout_init(void (*procedure)(), proc_t *pp, pri_t pri)
19817c478bd9Sstevel@tonic-gate {
19827c478bd9Sstevel@tonic-gate 	(void) thread_create(NULL, 0, procedure, NULL, 0, pp, TS_RUN, pri);
19837c478bd9Sstevel@tonic-gate }
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate /*
19867c478bd9Sstevel@tonic-gate  * Function for flushing D-cache when performing module relocations
19877c478bd9Sstevel@tonic-gate  * to an alternate mapping.  Unnecessary on Intel / AMD platforms.
19887c478bd9Sstevel@tonic-gate  */
19897c478bd9Sstevel@tonic-gate void
19907c478bd9Sstevel@tonic-gate dcache_flushall()
19917c478bd9Sstevel@tonic-gate {}
1992