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
507b65a64Saguzovsk  * Common Development and Distribution License (the "License").
607b65a64Saguzovsk  * 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 /*
2207b65a64Saguzovsk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
249d12795fSRobert Mustacchi  * Copyright (c) 2015 Joyent, Inc.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
337c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
347c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
357c478bd9Sstevel@tonic-gate #include <sys/param.h>
367c478bd9Sstevel@tonic-gate #include <sys/systm.h>
377c478bd9Sstevel@tonic-gate #include <sys/user.h>
387c478bd9Sstevel@tonic-gate #include <sys/unistd.h>
397c478bd9Sstevel@tonic-gate #include <sys/errno.h>
407c478bd9Sstevel@tonic-gate #include <sys/proc.h>
417c478bd9Sstevel@tonic-gate #include <sys/mman.h>
427c478bd9Sstevel@tonic-gate #include <sys/tuneable.h>
437c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
447c478bd9Sstevel@tonic-gate #include <sys/cred.h>
457c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
467c478bd9Sstevel@tonic-gate #include <sys/debug.h>
477c478bd9Sstevel@tonic-gate #include <sys/policy.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #include <vm/as.h>
507c478bd9Sstevel@tonic-gate #include <vm/seg.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate static uint_t mem_getpgszc(size_t);
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /*
557c478bd9Sstevel@tonic-gate  * Memory control operations
567c478bd9Sstevel@tonic-gate  */
577c478bd9Sstevel@tonic-gate int
memcntl(caddr_t addr,size_t len,int cmd,caddr_t arg,int attr,int mask)587c478bd9Sstevel@tonic-gate memcntl(caddr_t addr, size_t len, int cmd, caddr_t arg, int attr, int mask)
597c478bd9Sstevel@tonic-gate {
607c478bd9Sstevel@tonic-gate 	struct as *as = ttoproc(curthread)->p_as;
617c478bd9Sstevel@tonic-gate 	struct proc *p = ttoproc(curthread);
627c478bd9Sstevel@tonic-gate 	size_t pgsz;
637c478bd9Sstevel@tonic-gate 	uint_t szc, oszc, pgcmd;
647c478bd9Sstevel@tonic-gate 	int error = 0;
657c478bd9Sstevel@tonic-gate 	faultcode_t fc;
667c478bd9Sstevel@tonic-gate 	uintptr_t iarg;
677c478bd9Sstevel@tonic-gate 	STRUCT_DECL(memcntl_mha, mha);
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	if (mask)
707c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
717c478bd9Sstevel@tonic-gate 	if ((cmd == MC_LOCKAS) || (cmd == MC_UNLOCKAS)) {
727c478bd9Sstevel@tonic-gate 		if ((addr != 0) || (len != 0)) {
737c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
747c478bd9Sstevel@tonic-gate 		}
757c478bd9Sstevel@tonic-gate 	} else if (cmd != MC_HAT_ADVISE) {
767c478bd9Sstevel@tonic-gate 		if (((uintptr_t)addr & PAGEOFFSET) != 0 || len == 0) {
777c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
787c478bd9Sstevel@tonic-gate 		}
797c478bd9Sstevel@tonic-gate 		/*
807c478bd9Sstevel@tonic-gate 		 * We're only concerned with the address range
817c478bd9Sstevel@tonic-gate 		 * here, not the protections.  The protections
827c478bd9Sstevel@tonic-gate 		 * are only used as a "filter" in this code,
837c478bd9Sstevel@tonic-gate 		 * they aren't set or modified here.
847c478bd9Sstevel@tonic-gate 		 */
857c478bd9Sstevel@tonic-gate 		if (valid_usr_range(addr, len, 0, as,
867c478bd9Sstevel@tonic-gate 		    as->a_userlimit) != RANGE_OKAY) {
877c478bd9Sstevel@tonic-gate 			return (set_errno(ENOMEM));
887c478bd9Sstevel@tonic-gate 		}
897c478bd9Sstevel@tonic-gate 	}
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	if (cmd == MC_HAT_ADVISE) {
927c478bd9Sstevel@tonic-gate 		if (attr != 0 || mask != 0) {
937c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
947c478bd9Sstevel@tonic-gate 		}
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	} else {
977c478bd9Sstevel@tonic-gate 		if ((VALID_ATTR & attr) != attr) {
987c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
997c478bd9Sstevel@tonic-gate 		}
1007c478bd9Sstevel@tonic-gate 		if ((attr & SHARED) && (attr & PRIVATE)) {
1017c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
1027c478bd9Sstevel@tonic-gate 		}
1037c478bd9Sstevel@tonic-gate 		if (((cmd == MC_LOCKAS) || (cmd == MC_LOCK) ||
1047c478bd9Sstevel@tonic-gate 		    (cmd == MC_UNLOCKAS) || (cmd == MC_UNLOCK)) &&
1057c478bd9Sstevel@tonic-gate 		    (error = secpolicy_lock_memory(CRED())) != 0)
1067c478bd9Sstevel@tonic-gate 			return (set_errno(error));
1077c478bd9Sstevel@tonic-gate 	}
1087c478bd9Sstevel@tonic-gate 	if (attr) {
1097c478bd9Sstevel@tonic-gate 		attr |= PROT_USER;
1107c478bd9Sstevel@tonic-gate 	}
1117c478bd9Sstevel@tonic-gate 
112*c6f039c7SToomas Soome 	oszc = 0;
1137c478bd9Sstevel@tonic-gate 	switch (cmd) {
1147c478bd9Sstevel@tonic-gate 	case MC_SYNC:
1157c478bd9Sstevel@tonic-gate 		/*
1167c478bd9Sstevel@tonic-gate 		 * MS_SYNC used to be defined to be zero but is now non-zero.
1177c478bd9Sstevel@tonic-gate 		 * For binary compatibility we still accept zero
1187c478bd9Sstevel@tonic-gate 		 * (the absence of MS_ASYNC) to mean the same thing.
1197c478bd9Sstevel@tonic-gate 		 */
1207c478bd9Sstevel@tonic-gate 		iarg = (uintptr_t)arg;
1217c478bd9Sstevel@tonic-gate 		if ((iarg & ~MS_INVALIDATE) == 0)
1227c478bd9Sstevel@tonic-gate 			iarg |= MS_SYNC;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 		if (((iarg & ~(MS_SYNC|MS_ASYNC|MS_INVALIDATE)) != 0) ||
1259d12795fSRobert Mustacchi 		    ((iarg & (MS_SYNC|MS_ASYNC)) == (MS_SYNC|MS_ASYNC))) {
1267c478bd9Sstevel@tonic-gate 			error = set_errno(EINVAL);
1277c478bd9Sstevel@tonic-gate 		} else {
1287c478bd9Sstevel@tonic-gate 			error = as_ctl(as, addr, len, cmd, attr, iarg, NULL, 0);
1297c478bd9Sstevel@tonic-gate 			if (error) {
1307c478bd9Sstevel@tonic-gate 				(void) set_errno(error);
1317c478bd9Sstevel@tonic-gate 			}
1327c478bd9Sstevel@tonic-gate 		}
1337c478bd9Sstevel@tonic-gate 		return (error);
1347c478bd9Sstevel@tonic-gate 	case MC_LOCKAS:
1357c478bd9Sstevel@tonic-gate 		if ((uintptr_t)arg & ~(MCL_FUTURE|MCL_CURRENT) ||
1367c478bd9Sstevel@tonic-gate 		    (uintptr_t)arg == 0) {
1377c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
1387c478bd9Sstevel@tonic-gate 		}
1397c478bd9Sstevel@tonic-gate 		break;
1407c478bd9Sstevel@tonic-gate 	case MC_LOCK:
1417c478bd9Sstevel@tonic-gate 	case MC_UNLOCKAS:
1427c478bd9Sstevel@tonic-gate 	case MC_UNLOCK:
1437c478bd9Sstevel@tonic-gate 		break;
1447c478bd9Sstevel@tonic-gate 	case MC_HAT_ADVISE:
1457c478bd9Sstevel@tonic-gate 		/*
1467c478bd9Sstevel@tonic-gate 		 * Set prefered page size.
1477c478bd9Sstevel@tonic-gate 		 */
1487c478bd9Sstevel@tonic-gate 		STRUCT_INIT(mha, get_udatamodel());
1497c478bd9Sstevel@tonic-gate 		if (copyin(arg, STRUCT_BUF(mha), STRUCT_SIZE(mha))) {
1507c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
1517c478bd9Sstevel@tonic-gate 		}
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 		pgcmd = STRUCT_FGET(mha, mha_cmd);
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 		/*
1567c478bd9Sstevel@tonic-gate 		 * Currently only MHA_MAPSIZE_VA, MHA_MAPSIZE_STACK
1577c478bd9Sstevel@tonic-gate 		 * and MHA_MAPSIZE_BSSBRK are supported. Only one
1587c478bd9Sstevel@tonic-gate 		 * command may be specified at a time.
1597c478bd9Sstevel@tonic-gate 		 */
1607c478bd9Sstevel@tonic-gate 		if ((~(MHA_MAPSIZE_VA|MHA_MAPSIZE_STACK|MHA_MAPSIZE_BSSBRK) &
1617c478bd9Sstevel@tonic-gate 		    pgcmd) || pgcmd == 0 || !ISP2(pgcmd) ||
1627c478bd9Sstevel@tonic-gate 		    STRUCT_FGET(mha, mha_flags))
1637c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 		pgsz = STRUCT_FGET(mha, mha_pagesize);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 		/*
1687c478bd9Sstevel@tonic-gate 		 * call platform specific map_pgsz() routine to get the
1697c478bd9Sstevel@tonic-gate 		 * optimal pgsz if pgsz is 0.
1707c478bd9Sstevel@tonic-gate 		 *
1717c478bd9Sstevel@tonic-gate 		 * For stack and heap operations addr and len must be zero.
1727c478bd9Sstevel@tonic-gate 		 */
1737c478bd9Sstevel@tonic-gate 		if ((pgcmd & (MHA_MAPSIZE_BSSBRK|MHA_MAPSIZE_STACK)) != 0) {
1747c478bd9Sstevel@tonic-gate 			if (addr != NULL || len != 0) {
1757c478bd9Sstevel@tonic-gate 				return (set_errno(EINVAL));
1767c478bd9Sstevel@tonic-gate 			}
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 			/*
1797c478bd9Sstevel@tonic-gate 			 * Disable autompss for this process unless pgsz == 0,
1807c478bd9Sstevel@tonic-gate 			 * which means the system should pick.  In the
1817c478bd9Sstevel@tonic-gate 			 * pgsz == 0 case, leave the SAUTOLPG setting alone, as
1827c478bd9Sstevel@tonic-gate 			 * we don't want to enable it when someone has
1837c478bd9Sstevel@tonic-gate 			 * disabled automatic large page selection for the
1847c478bd9Sstevel@tonic-gate 			 * whole system.
1857c478bd9Sstevel@tonic-gate 			 */
1867c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
1877c478bd9Sstevel@tonic-gate 			if (pgsz != 0) {
1887c478bd9Sstevel@tonic-gate 				p->p_flag &= ~SAUTOLPG;
1897c478bd9Sstevel@tonic-gate 			}
1907c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 			as_rangelock(as);
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 			if (pgsz == 0) {
1957c478bd9Sstevel@tonic-gate 				int	type;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 				if (pgcmd == MHA_MAPSIZE_BSSBRK)
1987c478bd9Sstevel@tonic-gate 					type = MAPPGSZ_HEAP;
1997c478bd9Sstevel@tonic-gate 				else
2007c478bd9Sstevel@tonic-gate 					type = MAPPGSZ_STK;
2017c478bd9Sstevel@tonic-gate 
202ec25b48fSsusans 				pgsz = map_pgsz(type, p, 0, 0, 1);
2037c478bd9Sstevel@tonic-gate 			}
2047c478bd9Sstevel@tonic-gate 		} else {
205ec25b48fSsusans 			/*
206ec25b48fSsusans 			 * addr and len must be valid for range specified.
207ec25b48fSsusans 			 */
208ec25b48fSsusans 			if (valid_usr_range(addr, len, 0, as,
209ec25b48fSsusans 			    as->a_userlimit) != RANGE_OKAY) {
210ec25b48fSsusans 				return (set_errno(ENOMEM));
211ec25b48fSsusans 			}
2127c478bd9Sstevel@tonic-gate 			/*
2137c478bd9Sstevel@tonic-gate 			 * Note that we don't disable automatic large page
2147c478bd9Sstevel@tonic-gate 			 * selection for anon segments based on use of
2157c478bd9Sstevel@tonic-gate 			 * memcntl().
2167c478bd9Sstevel@tonic-gate 			 */
2177c478bd9Sstevel@tonic-gate 			if (pgsz == 0) {
218ec25b48fSsusans 				error = as_set_default_lpsize(as, addr, len);
219ec25b48fSsusans 				if (error) {
220ec25b48fSsusans 					(void) set_errno(error);
221ec25b48fSsusans 				}
222ec25b48fSsusans 				return (error);
2237c478bd9Sstevel@tonic-gate 			}
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 			/*
2267c478bd9Sstevel@tonic-gate 			 * addr and len must be prefered page size aligned
2277c478bd9Sstevel@tonic-gate 			 */
2287c478bd9Sstevel@tonic-gate 			if (!IS_P2ALIGNED(addr, pgsz) ||
2297c478bd9Sstevel@tonic-gate 			    !IS_P2ALIGNED(len, pgsz)) {
2307c478bd9Sstevel@tonic-gate 				return (set_errno(EINVAL));
2317c478bd9Sstevel@tonic-gate 			}
2327c478bd9Sstevel@tonic-gate 		}
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 		szc = mem_getpgszc(pgsz);
2357c478bd9Sstevel@tonic-gate 		if (szc == (uint_t)-1) {
2367c478bd9Sstevel@tonic-gate 			if ((pgcmd & (MHA_MAPSIZE_BSSBRK|MHA_MAPSIZE_STACK))
2377c478bd9Sstevel@tonic-gate 			    != 0) {
2387c478bd9Sstevel@tonic-gate 				as_rangeunlock(as);
2397c478bd9Sstevel@tonic-gate 			}
2407c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
2417c478bd9Sstevel@tonic-gate 		}
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 		/*
2447c478bd9Sstevel@tonic-gate 		 * For stack and heap operations we first need to pad
2457c478bd9Sstevel@tonic-gate 		 * out existing range (create new mappings) to the new
2467c478bd9Sstevel@tonic-gate 		 * prefered page size boundary. Also the start of the
2477c478bd9Sstevel@tonic-gate 		 * .bss for the heap or user's stack base may not be on
2487c478bd9Sstevel@tonic-gate 		 * the new prefered page size boundary. For these cases
2497c478bd9Sstevel@tonic-gate 		 * we align the base of the request on the new prefered
2507c478bd9Sstevel@tonic-gate 		 * page size.
2517c478bd9Sstevel@tonic-gate 		 */
2527c478bd9Sstevel@tonic-gate 		if (pgcmd & MHA_MAPSIZE_BSSBRK) {
2537c478bd9Sstevel@tonic-gate 			if (szc == p->p_brkpageszc) {
2547c478bd9Sstevel@tonic-gate 				as_rangeunlock(as);
2557c478bd9Sstevel@tonic-gate 				return (0);
2567c478bd9Sstevel@tonic-gate 			}
2577c478bd9Sstevel@tonic-gate 			if (szc > p->p_brkpageszc) {
2587c478bd9Sstevel@tonic-gate 				error = brk_internal(p->p_brkbase
2597c478bd9Sstevel@tonic-gate 				    + p->p_brksize, szc);
2607c478bd9Sstevel@tonic-gate 				if (error) {
2617c478bd9Sstevel@tonic-gate 					as_rangeunlock(as);
2627c478bd9Sstevel@tonic-gate 					return (set_errno(error));
2637c478bd9Sstevel@tonic-gate 				}
2647c478bd9Sstevel@tonic-gate 			}
265ec25b48fSsusans 			/*
266ec25b48fSsusans 			 * It is possible for brk_internal to silently fail to
267ec25b48fSsusans 			 * promote the heap size, so don't panic or ASSERT.
268ec25b48fSsusans 			 */
269ec25b48fSsusans 			if (!IS_P2ALIGNED(p->p_brkbase + p->p_brksize, pgsz)) {
270ec25b48fSsusans 				as_rangeunlock(as);
271ec25b48fSsusans 				return (set_errno(ENOMEM));
272ec25b48fSsusans 			}
2737c478bd9Sstevel@tonic-gate 			oszc = p->p_brkpageszc;
2747c478bd9Sstevel@tonic-gate 			p->p_brkpageszc = szc;
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 			addr = (caddr_t)P2ROUNDUP((uintptr_t)p->p_bssbase,
2777c478bd9Sstevel@tonic-gate 			    pgsz);
2787c478bd9Sstevel@tonic-gate 			len = (p->p_brkbase + p->p_brksize) - addr;
2797c478bd9Sstevel@tonic-gate 			ASSERT(IS_P2ALIGNED(len, pgsz));
2807c478bd9Sstevel@tonic-gate 			/*
2817c478bd9Sstevel@tonic-gate 			 * Perhaps no existing pages to promote.
2827c478bd9Sstevel@tonic-gate 			 */
2837c478bd9Sstevel@tonic-gate 			if (len == 0) {
2847c478bd9Sstevel@tonic-gate 				as_rangeunlock(as);
2857c478bd9Sstevel@tonic-gate 				return (0);
2867c478bd9Sstevel@tonic-gate 			}
2877c478bd9Sstevel@tonic-gate 		}
2887c478bd9Sstevel@tonic-gate 		/*
2897c478bd9Sstevel@tonic-gate 		 * The code below, as does grow.c, assumes stacks always grow
2907c478bd9Sstevel@tonic-gate 		 * downward.
2917c478bd9Sstevel@tonic-gate 		 */
2927c478bd9Sstevel@tonic-gate 		if (pgcmd & MHA_MAPSIZE_STACK) {
293e7f81dc7Ssusans 			if (szc == p->p_stkpageszc) {
2947c478bd9Sstevel@tonic-gate 				as_rangeunlock(as);
2957c478bd9Sstevel@tonic-gate 				return (0);
2967c478bd9Sstevel@tonic-gate 			}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 			if (szc > p->p_stkpageszc) {
299ec25b48fSsusans 				error = grow_internal(p->p_usrstack -
300ec25b48fSsusans 				    p->p_stksize, szc);
3017c478bd9Sstevel@tonic-gate 				if (error) {
3027c478bd9Sstevel@tonic-gate 					as_rangeunlock(as);
3037c478bd9Sstevel@tonic-gate 					return (set_errno(error));
3047c478bd9Sstevel@tonic-gate 				}
3057c478bd9Sstevel@tonic-gate 			}
306ec25b48fSsusans 			/*
307ec25b48fSsusans 			 * It is possible for grow_internal to silently fail to
308ec25b48fSsusans 			 * promote the stack size, so don't panic or ASSERT.
309ec25b48fSsusans 			 */
310ec25b48fSsusans 			if (!IS_P2ALIGNED(p->p_usrstack - p->p_stksize, pgsz)) {
311ec25b48fSsusans 				as_rangeunlock(as);
312ec25b48fSsusans 				return (set_errno(ENOMEM));
313ec25b48fSsusans 			}
3147c478bd9Sstevel@tonic-gate 			oszc = p->p_stkpageszc;
3157c478bd9Sstevel@tonic-gate 			p->p_stkpageszc = szc;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 			addr = p->p_usrstack - p->p_stksize;
318e7f81dc7Ssusans 			len = P2ALIGN(p->p_stksize, pgsz);
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 			/*
321e7f81dc7Ssusans 			 * Perhaps nothing to promote.
3227c478bd9Sstevel@tonic-gate 			 */
323e7f81dc7Ssusans 			if (len == 0 || addr >= p->p_usrstack ||
324e7f81dc7Ssusans 			    (addr + len) < addr) {
3257c478bd9Sstevel@tonic-gate 				as_rangeunlock(as);
3267c478bd9Sstevel@tonic-gate 				return (0);
3277c478bd9Sstevel@tonic-gate 			}
3287c478bd9Sstevel@tonic-gate 		}
3297c478bd9Sstevel@tonic-gate 		ASSERT(IS_P2ALIGNED(addr, pgsz));
3307c478bd9Sstevel@tonic-gate 		ASSERT(IS_P2ALIGNED(len, pgsz));
3317c478bd9Sstevel@tonic-gate 		error = as_setpagesize(as, addr, len, szc, B_TRUE);
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 		/*
3347c478bd9Sstevel@tonic-gate 		 * On stack or heap failures restore original
3357c478bd9Sstevel@tonic-gate 		 * pg size code.
3367c478bd9Sstevel@tonic-gate 		 */
3377c478bd9Sstevel@tonic-gate 		if (error) {
3387c478bd9Sstevel@tonic-gate 			if ((pgcmd & MHA_MAPSIZE_BSSBRK) != 0) {
3397c478bd9Sstevel@tonic-gate 				p->p_brkpageszc = oszc;
3407c478bd9Sstevel@tonic-gate 			}
3417c478bd9Sstevel@tonic-gate 			if ((pgcmd & MHA_MAPSIZE_STACK) != 0) {
3427c478bd9Sstevel@tonic-gate 				p->p_stkpageszc = oszc;
3437c478bd9Sstevel@tonic-gate 			}
3447c478bd9Sstevel@tonic-gate 			(void) set_errno(error);
3457c478bd9Sstevel@tonic-gate 		}
3467c478bd9Sstevel@tonic-gate 		if ((pgcmd & (MHA_MAPSIZE_BSSBRK|MHA_MAPSIZE_STACK)) != 0) {
3477c478bd9Sstevel@tonic-gate 			as_rangeunlock(as);
3487c478bd9Sstevel@tonic-gate 		}
3497c478bd9Sstevel@tonic-gate 		return (error);
3507c478bd9Sstevel@tonic-gate 	case MC_ADVISE:
3518905f42cSBryan Cantrill 		if ((uintptr_t)arg == MADV_FREE ||
3528905f42cSBryan Cantrill 		    (uintptr_t)arg == MADV_PURGE) {
35307b65a64Saguzovsk 			len &= PAGEMASK;
35407b65a64Saguzovsk 		}
3557c478bd9Sstevel@tonic-gate 		switch ((uintptr_t)arg) {
3567c478bd9Sstevel@tonic-gate 		case MADV_WILLNEED:
3577c478bd9Sstevel@tonic-gate 			fc = as_faulta(as, addr, len);
3587c478bd9Sstevel@tonic-gate 			if (fc) {
3597c478bd9Sstevel@tonic-gate 				if (FC_CODE(fc) == FC_OBJERR)
3607c478bd9Sstevel@tonic-gate 					error = set_errno(FC_ERRNO(fc));
3617c478bd9Sstevel@tonic-gate 				else if (FC_CODE(fc) == FC_NOMAP)
3627c478bd9Sstevel@tonic-gate 					error = set_errno(ENOMEM);
3637c478bd9Sstevel@tonic-gate 				else
3647c478bd9Sstevel@tonic-gate 					error = set_errno(EINVAL);
3657c478bd9Sstevel@tonic-gate 				return (error);
3667c478bd9Sstevel@tonic-gate 			}
3677c478bd9Sstevel@tonic-gate 			break;
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 		case MADV_DONTNEED:
3707c478bd9Sstevel@tonic-gate 			/*
3717c478bd9Sstevel@tonic-gate 			 * For now, don't need is turned into an as_ctl(MC_SYNC)
3727c478bd9Sstevel@tonic-gate 			 * operation flagged for async invalidate.
3737c478bd9Sstevel@tonic-gate 			 */
3747c478bd9Sstevel@tonic-gate 			error = as_ctl(as, addr, len, MC_SYNC, attr,
3757c478bd9Sstevel@tonic-gate 			    MS_ASYNC | MS_INVALIDATE, NULL, 0);
3767c478bd9Sstevel@tonic-gate 			if (error)
3777c478bd9Sstevel@tonic-gate 				(void) set_errno(error);
3787c478bd9Sstevel@tonic-gate 			return (error);
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 		default:
3817c478bd9Sstevel@tonic-gate 			error = as_ctl(as, addr, len, cmd, attr,
3827c478bd9Sstevel@tonic-gate 			    (uintptr_t)arg, NULL, 0);
3837c478bd9Sstevel@tonic-gate 			if (error)
3847c478bd9Sstevel@tonic-gate 				(void) set_errno(error);
3857c478bd9Sstevel@tonic-gate 			return (error);
3867c478bd9Sstevel@tonic-gate 		}
3877c478bd9Sstevel@tonic-gate 		break;
3889d12795fSRobert Mustacchi 	case MC_INHERIT_ZERO:
389ede2438eSRichard PALO 		if (arg != 0 || attr != 0 || mask != 0)
3909d12795fSRobert Mustacchi 			return (set_errno(EINVAL));
3919d12795fSRobert Mustacchi 		break;
3927c478bd9Sstevel@tonic-gate 	default:
3937c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
3947c478bd9Sstevel@tonic-gate 	}
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	error = as_ctl(as, addr, len, cmd, attr, (uintptr_t)arg, NULL, 0);
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	if (error)
3997c478bd9Sstevel@tonic-gate 		(void) set_errno(error);
4007c478bd9Sstevel@tonic-gate 	return (error);
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate /*
4047c478bd9Sstevel@tonic-gate  * Return page size code for page size passed in. If
4054abce959Smec  * matching page size not found or supported, return -1.
4067c478bd9Sstevel@tonic-gate  */
4077c478bd9Sstevel@tonic-gate static uint_t
mem_getpgszc(size_t pgsz)4087c478bd9Sstevel@tonic-gate mem_getpgszc(size_t pgsz) {
4094abce959Smec 	return ((uint_t)page_szc_user_filtered(pgsz));
4107c478bd9Sstevel@tonic-gate }
411