xref: /titanic_54/usr/src/uts/common/os/grow.c (revision 49a63d68cccecc58bd9c500de15d916229dc0fca)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <sys/inttypes.h>
357c478bd9Sstevel@tonic-gate #include <sys/param.h>
367c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
377c478bd9Sstevel@tonic-gate #include <sys/systm.h>
387c478bd9Sstevel@tonic-gate #include <sys/signal.h>
397c478bd9Sstevel@tonic-gate #include <sys/user.h>
407c478bd9Sstevel@tonic-gate #include <sys/errno.h>
417c478bd9Sstevel@tonic-gate #include <sys/var.h>
427c478bd9Sstevel@tonic-gate #include <sys/proc.h>
437c478bd9Sstevel@tonic-gate #include <sys/tuneable.h>
447c478bd9Sstevel@tonic-gate #include <sys/debug.h>
457c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
467c478bd9Sstevel@tonic-gate #include <sys/cred.h>
477c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
487c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
497c478bd9Sstevel@tonic-gate #include <sys/vm.h>
507c478bd9Sstevel@tonic-gate #include <sys/file.h>
517c478bd9Sstevel@tonic-gate #include <sys/mman.h>
527c478bd9Sstevel@tonic-gate #include <sys/vmparam.h>
537c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
547c478bd9Sstevel@tonic-gate #include <sys/lwpchan_impl.h>
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #include <vm/hat.h>
577c478bd9Sstevel@tonic-gate #include <vm/as.h>
587c478bd9Sstevel@tonic-gate #include <vm/seg.h>
597c478bd9Sstevel@tonic-gate #include <vm/seg_dev.h>
607c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h>
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate int use_brk_lpg = 1;
637c478bd9Sstevel@tonic-gate int use_stk_lpg = 1;
647c478bd9Sstevel@tonic-gate int use_zmap_lpg = 1;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate static int brk_lpg(caddr_t nva);
677c478bd9Sstevel@tonic-gate static int grow_lpg(caddr_t sp);
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate int
707c478bd9Sstevel@tonic-gate brk(caddr_t nva)
717c478bd9Sstevel@tonic-gate {
727c478bd9Sstevel@tonic-gate 	int error;
737c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	/*
767c478bd9Sstevel@tonic-gate 	 * Serialize brk operations on an address space.
777c478bd9Sstevel@tonic-gate 	 * This also serves as the lock protecting p_brksize
787c478bd9Sstevel@tonic-gate 	 * and p_brkpageszc.
797c478bd9Sstevel@tonic-gate 	 */
807c478bd9Sstevel@tonic-gate 	as_rangelock(p->p_as);
817c478bd9Sstevel@tonic-gate 	if (use_brk_lpg && (p->p_flag & SAUTOLPG) != 0) {
827c478bd9Sstevel@tonic-gate 		error = brk_lpg(nva);
837c478bd9Sstevel@tonic-gate 	} else {
847c478bd9Sstevel@tonic-gate 		error = brk_internal(nva, p->p_brkpageszc);
857c478bd9Sstevel@tonic-gate 	}
867c478bd9Sstevel@tonic-gate 	as_rangeunlock(p->p_as);
877c478bd9Sstevel@tonic-gate 	return ((error != 0 ? set_errno(error) : 0));
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate  * Algorithm: call arch-specific map_pgsz to get best page size to use,
927c478bd9Sstevel@tonic-gate  * then call brk_internal().
937c478bd9Sstevel@tonic-gate  * Returns 0 on success.
947c478bd9Sstevel@tonic-gate  */
957c478bd9Sstevel@tonic-gate static int
967c478bd9Sstevel@tonic-gate brk_lpg(caddr_t nva)
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate 	struct proc *p = curproc;
997c478bd9Sstevel@tonic-gate 	size_t pgsz, len;
1007c478bd9Sstevel@tonic-gate 	caddr_t addr;
1017c478bd9Sstevel@tonic-gate 	caddr_t bssbase = p->p_bssbase;
1027c478bd9Sstevel@tonic-gate 	caddr_t brkbase = p->p_brkbase;
1037c478bd9Sstevel@tonic-gate 	int oszc, szc;
1047c478bd9Sstevel@tonic-gate 	int err;
1057c478bd9Sstevel@tonic-gate 	int remap = 0;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	oszc = p->p_brkpageszc;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	/*
1107c478bd9Sstevel@tonic-gate 	 * If p_brkbase has not yet been set, the first call
1117c478bd9Sstevel@tonic-gate 	 * to brk_internal() will initialize it.
1127c478bd9Sstevel@tonic-gate 	 */
1137c478bd9Sstevel@tonic-gate 	if (brkbase == 0) {
1147c478bd9Sstevel@tonic-gate 		return (brk_internal(nva, oszc));
1157c478bd9Sstevel@tonic-gate 	}
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	len = nva - bssbase;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	pgsz = map_pgsz(MAPPGSZ_HEAP, p, bssbase, len, &remap);
1207c478bd9Sstevel@tonic-gate 	szc = page_szc(pgsz);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	/*
1237c478bd9Sstevel@tonic-gate 	 * Covers two cases:
1247c478bd9Sstevel@tonic-gate 	 * 1. page_szc() returns -1 for invalid page size, so we want to
1257c478bd9Sstevel@tonic-gate 	 * ignore it in that case.
1267c478bd9Sstevel@tonic-gate 	 * 2. By design we never decrease page size, as it is more stable.
1277c478bd9Sstevel@tonic-gate 	 */
1287c478bd9Sstevel@tonic-gate 	if (szc <= oszc) {
1297c478bd9Sstevel@tonic-gate 		err = brk_internal(nva, oszc);
1307c478bd9Sstevel@tonic-gate 		/* If failed, back off to base page size. */
1317c478bd9Sstevel@tonic-gate 		if (err != 0 && oszc != 0) {
1327c478bd9Sstevel@tonic-gate 			err = brk_internal(nva, 0);
1337c478bd9Sstevel@tonic-gate 		}
1347c478bd9Sstevel@tonic-gate 		return (err);
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	if (remap == 0) {
1387c478bd9Sstevel@tonic-gate 		/*
1397c478bd9Sstevel@tonic-gate 		 * Map from the current brk end up to the new page size
1407c478bd9Sstevel@tonic-gate 		 * alignment using the current page size.
1417c478bd9Sstevel@tonic-gate 		 */
1427c478bd9Sstevel@tonic-gate 		addr = brkbase + p->p_brksize;
1437c478bd9Sstevel@tonic-gate 		addr = (caddr_t)P2ROUNDUP((uintptr_t)addr, pgsz);
1447c478bd9Sstevel@tonic-gate 		if (addr < nva) {
1457c478bd9Sstevel@tonic-gate 			err = brk_internal(addr, oszc);
1467c478bd9Sstevel@tonic-gate 			/*
1477c478bd9Sstevel@tonic-gate 			 * In failure case, try again if oszc is not base page
1487c478bd9Sstevel@tonic-gate 			 * size, then return err.
1497c478bd9Sstevel@tonic-gate 			 */
1507c478bd9Sstevel@tonic-gate 			if (err != 0) {
1517c478bd9Sstevel@tonic-gate 				if (oszc != 0) {
1527c478bd9Sstevel@tonic-gate 					err = brk_internal(nva, 0);
1537c478bd9Sstevel@tonic-gate 				}
1547c478bd9Sstevel@tonic-gate 				return (err);
1557c478bd9Sstevel@tonic-gate 			}
1567c478bd9Sstevel@tonic-gate 		}
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	err = brk_internal(nva, szc);
1607c478bd9Sstevel@tonic-gate 	/* If using szc failed, map with base page size and return. */
1617c478bd9Sstevel@tonic-gate 	if (err != 0) {
1627c478bd9Sstevel@tonic-gate 		if (szc != 0) {
1637c478bd9Sstevel@tonic-gate 			err = brk_internal(nva, 0);
1647c478bd9Sstevel@tonic-gate 		}
1657c478bd9Sstevel@tonic-gate 		return (err);
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	if (remap != 0) {
1697c478bd9Sstevel@tonic-gate 		/*
1707c478bd9Sstevel@tonic-gate 		 * Round up brk base to a large page boundary and remap
1717c478bd9Sstevel@tonic-gate 		 * anything in the segment already faulted in beyond that
1727c478bd9Sstevel@tonic-gate 		 * point.
1737c478bd9Sstevel@tonic-gate 		 */
1747c478bd9Sstevel@tonic-gate 		addr = (caddr_t)P2ROUNDUP((uintptr_t)p->p_bssbase, pgsz);
1757c478bd9Sstevel@tonic-gate 		len = (brkbase + p->p_brksize) - addr;
1767c478bd9Sstevel@tonic-gate 		/* advisory, so ignore errors */
1777c478bd9Sstevel@tonic-gate 		(void) as_setpagesize(p->p_as, addr, len, szc, B_FALSE);
1787c478bd9Sstevel@tonic-gate 	}
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	ASSERT(err == 0);
1817c478bd9Sstevel@tonic-gate 	return (err);		/* should always be 0 */
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate /*
1857c478bd9Sstevel@tonic-gate  * Returns 0 on success.
1867c478bd9Sstevel@tonic-gate  */
1877c478bd9Sstevel@tonic-gate int
1887c478bd9Sstevel@tonic-gate brk_internal(caddr_t nva, uint_t brkszc)
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate 	caddr_t ova;			/* current break address */
1917c478bd9Sstevel@tonic-gate 	size_t size;
1927c478bd9Sstevel@tonic-gate 	int	error;
1937c478bd9Sstevel@tonic-gate 	struct proc *p = curproc;
1947c478bd9Sstevel@tonic-gate 	struct as *as = p->p_as;
1957c478bd9Sstevel@tonic-gate 	size_t pgsz;
1967c478bd9Sstevel@tonic-gate 	uint_t szc;
1977c478bd9Sstevel@tonic-gate 	rctl_qty_t as_rctl;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	/*
2007c478bd9Sstevel@tonic-gate 	 * extend heap to brkszc alignment but use current p->p_brkpageszc
2017c478bd9Sstevel@tonic-gate 	 * for the newly created segment. This allows the new extension
2027c478bd9Sstevel@tonic-gate 	 * segment to be concatenated successfully with the existing brk
2037c478bd9Sstevel@tonic-gate 	 * segment.
2047c478bd9Sstevel@tonic-gate 	 */
2057c478bd9Sstevel@tonic-gate 	if ((szc = brkszc) != 0) {
2067c478bd9Sstevel@tonic-gate 		pgsz = page_get_pagesize(szc);
2077c478bd9Sstevel@tonic-gate 		ASSERT(pgsz > PAGESIZE);
2087c478bd9Sstevel@tonic-gate 	} else {
2097c478bd9Sstevel@tonic-gate 		pgsz = PAGESIZE;
2107c478bd9Sstevel@tonic-gate 	}
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
2137c478bd9Sstevel@tonic-gate 	as_rctl = rctl_enforced_value(rctlproc_legacy[RLIMIT_DATA],
2147c478bd9Sstevel@tonic-gate 	    p->p_rctls, p);
2157c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	/*
2187c478bd9Sstevel@tonic-gate 	 * If p_brkbase has not yet been set, the first call
2197c478bd9Sstevel@tonic-gate 	 * to brk() will initialize it.
2207c478bd9Sstevel@tonic-gate 	 */
2217c478bd9Sstevel@tonic-gate 	if (p->p_brkbase == 0)
2227c478bd9Sstevel@tonic-gate 		p->p_brkbase = nva;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	/*
2257c478bd9Sstevel@tonic-gate 	 * Before multiple page size support existed p_brksize was the value
2267c478bd9Sstevel@tonic-gate 	 * not rounded to the pagesize (i.e. it stored the exact user request
2277c478bd9Sstevel@tonic-gate 	 * for heap size). If pgsz is greater than PAGESIZE calculate the
2287c478bd9Sstevel@tonic-gate 	 * heap size as the real new heap size by rounding it up to pgsz.
2297c478bd9Sstevel@tonic-gate 	 * This is useful since we may want to know where the heap ends
2307c478bd9Sstevel@tonic-gate 	 * without knowing heap pagesize (e.g. some old code) and also if
2317c478bd9Sstevel@tonic-gate 	 * heap pagesize changes we can update p_brkpageszc but delay adding
2327c478bd9Sstevel@tonic-gate 	 * new mapping yet still know from p_brksize where the heap really
2337c478bd9Sstevel@tonic-gate 	 * ends. The user requested heap end is stored in libc variable.
2347c478bd9Sstevel@tonic-gate 	 */
2357c478bd9Sstevel@tonic-gate 	if (pgsz > PAGESIZE) {
2367c478bd9Sstevel@tonic-gate 		caddr_t tnva = (caddr_t)P2ROUNDUP((uintptr_t)nva, pgsz);
2377c478bd9Sstevel@tonic-gate 		size = tnva - p->p_brkbase;
2387c478bd9Sstevel@tonic-gate 		if (tnva < p->p_brkbase || (size > p->p_brksize &&
2397c478bd9Sstevel@tonic-gate 		    size > (size_t)as_rctl)) {
2407c478bd9Sstevel@tonic-gate 			szc = 0;
2417c478bd9Sstevel@tonic-gate 			pgsz = PAGESIZE;
2427c478bd9Sstevel@tonic-gate 			size = nva - p->p_brkbase;
2437c478bd9Sstevel@tonic-gate 		}
2447c478bd9Sstevel@tonic-gate 	} else {
2457c478bd9Sstevel@tonic-gate 		size = nva - p->p_brkbase;
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	/*
2497c478bd9Sstevel@tonic-gate 	 * use PAGESIZE to roundup ova because we want to know the real value
2507c478bd9Sstevel@tonic-gate 	 * of the current heap end in case p_brkpageszc changes since the last
2517c478bd9Sstevel@tonic-gate 	 * p_brksize was computed.
2527c478bd9Sstevel@tonic-gate 	 */
2537c478bd9Sstevel@tonic-gate 	nva = (caddr_t)P2ROUNDUP((uintptr_t)nva, pgsz);
2547c478bd9Sstevel@tonic-gate 	ova = (caddr_t)P2ROUNDUP((uintptr_t)(p->p_brkbase + p->p_brksize),
2557c478bd9Sstevel@tonic-gate 		PAGESIZE);
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	if ((nva < p->p_brkbase) || (size > p->p_brksize &&
2587c478bd9Sstevel@tonic-gate 	    size > as_rctl)) {
2597c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
2607c478bd9Sstevel@tonic-gate 		(void) rctl_action(rctlproc_legacy[RLIMIT_DATA], p->p_rctls, p,
2617c478bd9Sstevel@tonic-gate 		    RCA_SAFE);
2627c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
2637c478bd9Sstevel@tonic-gate 		return (ENOMEM);
2647c478bd9Sstevel@tonic-gate 	}
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	if (nva > ova) {
2677c478bd9Sstevel@tonic-gate 		struct segvn_crargs crargs =
2687c478bd9Sstevel@tonic-gate 		    SEGVN_ZFOD_ARGS(PROT_ZFOD, PROT_ALL);
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 		if (!(p->p_datprot & PROT_EXEC)) {
2717c478bd9Sstevel@tonic-gate 			crargs.prot &= ~PROT_EXEC;
2727c478bd9Sstevel@tonic-gate 		}
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 		/*
2757c478bd9Sstevel@tonic-gate 		 * Add new zfod mapping to extend UNIX data segment
2767c478bd9Sstevel@tonic-gate 		 */
2777c478bd9Sstevel@tonic-gate 		crargs.szc = szc;
2787c478bd9Sstevel@tonic-gate 		crargs.lgrp_mem_policy_flags = LGRP_MP_FLAG_EXTEND_UP;
2797c478bd9Sstevel@tonic-gate 		error = as_map(as, ova, (size_t)(nva - ova), segvn_create,
2807c478bd9Sstevel@tonic-gate 		    &crargs);
2817c478bd9Sstevel@tonic-gate 		if (error) {
2827c478bd9Sstevel@tonic-gate 			return (error);
2837c478bd9Sstevel@tonic-gate 		}
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	} else if (nva < ova) {
2867c478bd9Sstevel@tonic-gate 		/*
2877c478bd9Sstevel@tonic-gate 		 * Release mapping to shrink UNIX data segment.
2887c478bd9Sstevel@tonic-gate 		 */
2897c478bd9Sstevel@tonic-gate 		(void) as_unmap(as, nva, (size_t)(ova - nva));
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 	p->p_brksize = size;
2927c478bd9Sstevel@tonic-gate 	p->p_brkpageszc = szc;
2937c478bd9Sstevel@tonic-gate 	return (0);
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate /*
2977c478bd9Sstevel@tonic-gate  * Grow the stack to include sp.  Return 1 if successful, 0 otherwise.
2987c478bd9Sstevel@tonic-gate  * This routine assumes that the stack grows downward.
2997c478bd9Sstevel@tonic-gate  */
3007c478bd9Sstevel@tonic-gate int
3017c478bd9Sstevel@tonic-gate grow(caddr_t sp)
3027c478bd9Sstevel@tonic-gate {
3037c478bd9Sstevel@tonic-gate 	struct proc *p = curproc;
3047c478bd9Sstevel@tonic-gate 	int err;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	/*
3077c478bd9Sstevel@tonic-gate 	 * Serialize grow operations on an address space.
3087c478bd9Sstevel@tonic-gate 	 * This also serves as the lock protecting p_stksize
3097c478bd9Sstevel@tonic-gate 	 * and p_stkpageszc.
3107c478bd9Sstevel@tonic-gate 	 */
3117c478bd9Sstevel@tonic-gate 	as_rangelock(p->p_as);
3127c478bd9Sstevel@tonic-gate 	if (use_stk_lpg && (p->p_flag & SAUTOLPG) != 0) {
3137c478bd9Sstevel@tonic-gate 		err = grow_lpg(sp);
3147c478bd9Sstevel@tonic-gate 	} else {
3157c478bd9Sstevel@tonic-gate 		err = grow_internal(sp, p->p_stkpageszc);
3167c478bd9Sstevel@tonic-gate 	}
3177c478bd9Sstevel@tonic-gate 	as_rangeunlock(p->p_as);
3187c478bd9Sstevel@tonic-gate 	return ((err == 0 ? 1 : 0));
3197c478bd9Sstevel@tonic-gate }
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate /*
3227c478bd9Sstevel@tonic-gate  * Algorithm: call arch-specific map_pgsz to get best page size to use,
3237c478bd9Sstevel@tonic-gate  * then call grow_internal().
3247c478bd9Sstevel@tonic-gate  * Returns 0 on success.
3257c478bd9Sstevel@tonic-gate  */
3267c478bd9Sstevel@tonic-gate static int
3277c478bd9Sstevel@tonic-gate grow_lpg(caddr_t sp)
3287c478bd9Sstevel@tonic-gate {
3297c478bd9Sstevel@tonic-gate 	struct proc *p = curproc;
3307c478bd9Sstevel@tonic-gate 	size_t pgsz;
3317c478bd9Sstevel@tonic-gate 	size_t len, newsize;
3327c478bd9Sstevel@tonic-gate 	caddr_t addr, oldsp;
3337c478bd9Sstevel@tonic-gate 	int oszc, szc;
3347c478bd9Sstevel@tonic-gate 	int err;
3357c478bd9Sstevel@tonic-gate 	int remap = 0;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	newsize = p->p_usrstack - sp;
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	oszc = p->p_stkpageszc;
3407c478bd9Sstevel@tonic-gate 	pgsz = map_pgsz(MAPPGSZ_STK, p, sp, newsize, &remap);
3417c478bd9Sstevel@tonic-gate 	szc = page_szc(pgsz);
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	/*
3447c478bd9Sstevel@tonic-gate 	 * Covers two cases:
3457c478bd9Sstevel@tonic-gate 	 * 1. page_szc() returns -1 for invalid page size, so we want to
3467c478bd9Sstevel@tonic-gate 	 * ignore it in that case.
3477c478bd9Sstevel@tonic-gate 	 * 2. By design we never decrease page size, as it is more stable.
3487c478bd9Sstevel@tonic-gate 	 * This shouldn't happen as the stack never shrinks.
3497c478bd9Sstevel@tonic-gate 	 */
3507c478bd9Sstevel@tonic-gate 	if (szc <= oszc) {
3517c478bd9Sstevel@tonic-gate 		err = grow_internal(sp, oszc);
3527c478bd9Sstevel@tonic-gate 		/* failed, fall back to base page size */
3537c478bd9Sstevel@tonic-gate 		if (err != 0 && oszc != 0) {
3547c478bd9Sstevel@tonic-gate 			err = grow_internal(sp, 0);
3557c478bd9Sstevel@tonic-gate 		}
3567c478bd9Sstevel@tonic-gate 		return (err);
3577c478bd9Sstevel@tonic-gate 	}
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	/*
3607c478bd9Sstevel@tonic-gate 	 * We've grown sufficiently to switch to a new page size.
3617c478bd9Sstevel@tonic-gate 	 * If we're not going to remap the whole segment with the new
3627c478bd9Sstevel@tonic-gate 	 * page size, split the grow into two operations: map to the new
3637c478bd9Sstevel@tonic-gate 	 * page size alignment boundary with the existing page size, then
3647c478bd9Sstevel@tonic-gate 	 * map the rest with the new page size.
3657c478bd9Sstevel@tonic-gate 	 */
3667c478bd9Sstevel@tonic-gate 	err = 0;
3677c478bd9Sstevel@tonic-gate 	if (remap == 0) {
3687c478bd9Sstevel@tonic-gate 		oldsp = p->p_usrstack - p->p_stksize;
3697c478bd9Sstevel@tonic-gate 		addr = (caddr_t)P2ALIGN((uintptr_t)oldsp, pgsz);
3707c478bd9Sstevel@tonic-gate 		if (addr > sp) {
3717c478bd9Sstevel@tonic-gate 			err = grow_internal(addr, oszc);
3727c478bd9Sstevel@tonic-gate 			/*
3737c478bd9Sstevel@tonic-gate 			 * In this case, grow with oszc failed, so grow all the
3747c478bd9Sstevel@tonic-gate 			 * way to sp with base page size.
3757c478bd9Sstevel@tonic-gate 			 */
3767c478bd9Sstevel@tonic-gate 			if (err != 0) {
3777c478bd9Sstevel@tonic-gate 				if (oszc != 0) {
3787c478bd9Sstevel@tonic-gate 					err = grow_internal(sp, 0);
3797c478bd9Sstevel@tonic-gate 				}
3807c478bd9Sstevel@tonic-gate 				return (err);
3817c478bd9Sstevel@tonic-gate 			}
3827c478bd9Sstevel@tonic-gate 		}
3837c478bd9Sstevel@tonic-gate 	}
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	err = grow_internal(sp, szc);
3867c478bd9Sstevel@tonic-gate 	/* The grow with szc failed, so fall back to base page size. */
3877c478bd9Sstevel@tonic-gate 	if (err != 0) {
3887c478bd9Sstevel@tonic-gate 		if (szc != 0) {
3897c478bd9Sstevel@tonic-gate 			err = grow_internal(sp, 0);
3907c478bd9Sstevel@tonic-gate 		}
3917c478bd9Sstevel@tonic-gate 		return (err);
3927c478bd9Sstevel@tonic-gate 	}
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	if (remap) {
3957c478bd9Sstevel@tonic-gate 		/*
3967c478bd9Sstevel@tonic-gate 		 * Round up stack pointer to a large page boundary and remap
3977c478bd9Sstevel@tonic-gate 		 * any pgsz pages in the segment already faulted in beyond that
3987c478bd9Sstevel@tonic-gate 		 * point.
3997c478bd9Sstevel@tonic-gate 		 */
4007c478bd9Sstevel@tonic-gate 		addr = p->p_usrstack - p->p_stksize;
4017c478bd9Sstevel@tonic-gate 		addr = (caddr_t)P2ROUNDUP((uintptr_t)addr, pgsz);
4027c478bd9Sstevel@tonic-gate 		len = (caddr_t)P2ALIGN((uintptr_t)p->p_usrstack, pgsz) - addr;
4037c478bd9Sstevel@tonic-gate 		/* advisory, so ignore errors */
4047c478bd9Sstevel@tonic-gate 		(void) as_setpagesize(p->p_as, addr, len, szc, B_FALSE);
4057c478bd9Sstevel@tonic-gate 	}
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	/* Update page size code for stack. */
4087c478bd9Sstevel@tonic-gate 	p->p_stkpageszc = szc;
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	ASSERT(err == 0);
4117c478bd9Sstevel@tonic-gate 	return (err);		/* should always be 0 */
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate /*
4157c478bd9Sstevel@tonic-gate  * This routine assumes that the stack grows downward.
4167c478bd9Sstevel@tonic-gate  * Returns 0 on success, errno on failure.
4177c478bd9Sstevel@tonic-gate  */
4187c478bd9Sstevel@tonic-gate int
4197c478bd9Sstevel@tonic-gate grow_internal(caddr_t sp, uint_t growszc)
4207c478bd9Sstevel@tonic-gate {
4217c478bd9Sstevel@tonic-gate 	struct proc *p = curproc;
4227c478bd9Sstevel@tonic-gate 	struct as *as = p->p_as;
4237c478bd9Sstevel@tonic-gate 	size_t newsize = p->p_usrstack - sp;
4247c478bd9Sstevel@tonic-gate 	size_t oldsize;
4257c478bd9Sstevel@tonic-gate 	int    error;
4267c478bd9Sstevel@tonic-gate 	size_t pgsz;
4277c478bd9Sstevel@tonic-gate 	uint_t szc;
4287c478bd9Sstevel@tonic-gate 	struct segvn_crargs crargs = SEGVN_ZFOD_ARGS(PROT_ZFOD, PROT_ALL);
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	ASSERT(sp < p->p_usrstack);
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	/*
4337c478bd9Sstevel@tonic-gate 	 * grow to growszc alignment but use current p->p_stkpageszc for
4347c478bd9Sstevel@tonic-gate 	 * the segvn_crargs szc passed to segvn_create. For memcntl to
4357c478bd9Sstevel@tonic-gate 	 * increase the szc, this allows the new extension segment to be
4367c478bd9Sstevel@tonic-gate 	 * concatenated successfully with the existing stack segment.
4377c478bd9Sstevel@tonic-gate 	 */
4387c478bd9Sstevel@tonic-gate 	if ((szc = growszc) != 0) {
4397c478bd9Sstevel@tonic-gate 		pgsz = page_get_pagesize(szc);
4407c478bd9Sstevel@tonic-gate 		ASSERT(pgsz > PAGESIZE);
4417c478bd9Sstevel@tonic-gate 		newsize = P2ROUNDUP(newsize, pgsz);
4427c478bd9Sstevel@tonic-gate 		if (newsize > (size_t)p->p_stk_ctl) {
4437c478bd9Sstevel@tonic-gate 			szc = 0;
4447c478bd9Sstevel@tonic-gate 			pgsz = PAGESIZE;
4457c478bd9Sstevel@tonic-gate 			newsize = p->p_usrstack - sp;
4467c478bd9Sstevel@tonic-gate 		}
4477c478bd9Sstevel@tonic-gate 	} else {
4487c478bd9Sstevel@tonic-gate 		pgsz = PAGESIZE;
4497c478bd9Sstevel@tonic-gate 	}
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	if (newsize > (size_t)p->p_stk_ctl) {
4527c478bd9Sstevel@tonic-gate 		(void) rctl_action(rctlproc_legacy[RLIMIT_STACK], p->p_rctls, p,
4537c478bd9Sstevel@tonic-gate 		    RCA_UNSAFE_ALL);
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 		return (ENOMEM);
4567c478bd9Sstevel@tonic-gate 	}
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	oldsize = p->p_stksize;
4597c478bd9Sstevel@tonic-gate 	newsize = P2ROUNDUP(newsize, pgsz);
4607c478bd9Sstevel@tonic-gate 	ASSERT(P2PHASE(oldsize, PAGESIZE) == 0);
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	if (newsize <= oldsize) {	/* prevent the stack from shrinking */
4637c478bd9Sstevel@tonic-gate 		return (0);
4647c478bd9Sstevel@tonic-gate 	}
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	if (!(p->p_stkprot & PROT_EXEC)) {
4677c478bd9Sstevel@tonic-gate 		crargs.prot &= ~PROT_EXEC;
4687c478bd9Sstevel@tonic-gate 	}
4697c478bd9Sstevel@tonic-gate 	/*
4707c478bd9Sstevel@tonic-gate 	 * extend stack with the p_stkpageszc. growszc is different than
4717c478bd9Sstevel@tonic-gate 	 * p_stkpageszc only on a memcntl to increase the stack pagesize.
4727c478bd9Sstevel@tonic-gate 	 */
4737c478bd9Sstevel@tonic-gate 	crargs.szc = p->p_stkpageszc;
4747c478bd9Sstevel@tonic-gate 	crargs.lgrp_mem_policy_flags = LGRP_MP_FLAG_EXTEND_DOWN;
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	if ((error = as_map(as, p->p_usrstack - newsize, newsize - oldsize,
4777c478bd9Sstevel@tonic-gate 	    segvn_create, &crargs)) != 0) {
4787c478bd9Sstevel@tonic-gate 		if (error == EAGAIN) {
4797c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "Sorry, no swap space to grow stack "
4807c478bd9Sstevel@tonic-gate 			    "for pid %d (%s)", p->p_pid, u.u_comm);
4817c478bd9Sstevel@tonic-gate 		}
4827c478bd9Sstevel@tonic-gate 		return (error);
4837c478bd9Sstevel@tonic-gate 	}
4847c478bd9Sstevel@tonic-gate 	p->p_stksize = newsize;
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	/*
4887c478bd9Sstevel@tonic-gate 	 * Set up translations so the process doesn't have to fault in
4897c478bd9Sstevel@tonic-gate 	 * the stack pages we just gave it.
4907c478bd9Sstevel@tonic-gate 	 */
4917c478bd9Sstevel@tonic-gate 	(void) as_fault(as->a_hat, as,
4927c478bd9Sstevel@tonic-gate 	    p->p_usrstack - newsize, newsize - oldsize, F_INVAL, S_WRITE);
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	return (0);
4957c478bd9Sstevel@tonic-gate }
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate /*
4987c478bd9Sstevel@tonic-gate  * Used for MAP_ANON - fast way to get anonymous pages
4997c478bd9Sstevel@tonic-gate  */
5007c478bd9Sstevel@tonic-gate static int
5017c478bd9Sstevel@tonic-gate zmap(struct as *as, caddr_t *addrp, size_t len, uint_t uprot, int flags,
5027c478bd9Sstevel@tonic-gate     offset_t pos)
5037c478bd9Sstevel@tonic-gate {
5047c478bd9Sstevel@tonic-gate 	struct segvn_crargs a, b;
5057c478bd9Sstevel@tonic-gate 	struct proc *p = curproc;
5067c478bd9Sstevel@tonic-gate 	int err;
5077c478bd9Sstevel@tonic-gate 	size_t pgsz;
5087c478bd9Sstevel@tonic-gate 	size_t l0, l1, l2, l3, l4; /* 0th through 5th chunks */
5097c478bd9Sstevel@tonic-gate 	caddr_t ruaddr, ruaddr0; /* rounded up addresses */
5107c478bd9Sstevel@tonic-gate 	extern size_t auto_lpg_va_default;
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	if (((PROT_ALL & uprot) != uprot))
5137c478bd9Sstevel@tonic-gate 		return (EACCES);
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	if ((flags & MAP_FIXED) != 0) {
5167c478bd9Sstevel@tonic-gate 		caddr_t userlimit;
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 		/*
5197c478bd9Sstevel@tonic-gate 		 * Use the user address.  First verify that
5207c478bd9Sstevel@tonic-gate 		 * the address to be used is page aligned.
5217c478bd9Sstevel@tonic-gate 		 * Then make some simple bounds checks.
5227c478bd9Sstevel@tonic-gate 		 */
5237c478bd9Sstevel@tonic-gate 		if (((uintptr_t)*addrp & PAGEOFFSET) != 0)
5247c478bd9Sstevel@tonic-gate 			return (EINVAL);
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 		userlimit = flags & _MAP_LOW32 ?
5277c478bd9Sstevel@tonic-gate 		    (caddr_t)USERLIMIT32 : as->a_userlimit;
5287c478bd9Sstevel@tonic-gate 		switch (valid_usr_range(*addrp, len, uprot, as, userlimit)) {
5297c478bd9Sstevel@tonic-gate 		case RANGE_OKAY:
5307c478bd9Sstevel@tonic-gate 			break;
5317c478bd9Sstevel@tonic-gate 		case RANGE_BADPROT:
5327c478bd9Sstevel@tonic-gate 			return (ENOTSUP);
5337c478bd9Sstevel@tonic-gate 		case RANGE_BADADDR:
5347c478bd9Sstevel@tonic-gate 		default:
5357c478bd9Sstevel@tonic-gate 			return (ENOMEM);
5367c478bd9Sstevel@tonic-gate 		}
5377c478bd9Sstevel@tonic-gate 		(void) as_unmap(as, *addrp, len);
5387c478bd9Sstevel@tonic-gate 	} else {
5397c478bd9Sstevel@tonic-gate 		/*
5407c478bd9Sstevel@tonic-gate 		 * No need to worry about vac alignment for anonymous
5417c478bd9Sstevel@tonic-gate 		 * pages since this is a "clone" object that doesn't
5427c478bd9Sstevel@tonic-gate 		 * yet exist.
5437c478bd9Sstevel@tonic-gate 		 */
5447c478bd9Sstevel@tonic-gate 		map_addr(addrp, len, pos, 0, flags);
5457c478bd9Sstevel@tonic-gate 		if (*addrp == NULL)
5467c478bd9Sstevel@tonic-gate 			return (ENOMEM);
5477c478bd9Sstevel@tonic-gate 	}
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	/*
5507c478bd9Sstevel@tonic-gate 	 * Use the seg_vn segment driver; passing in the NULL amp
5517c478bd9Sstevel@tonic-gate 	 * gives the desired "cloning" effect.
5527c478bd9Sstevel@tonic-gate 	 */
5537c478bd9Sstevel@tonic-gate 	a.vp = NULL;
5547c478bd9Sstevel@tonic-gate 	a.offset = 0;
5557c478bd9Sstevel@tonic-gate 	a.type = flags & MAP_TYPE;
5567c478bd9Sstevel@tonic-gate 	a.prot = uprot;
5577c478bd9Sstevel@tonic-gate 	a.maxprot = PROT_ALL;
5587c478bd9Sstevel@tonic-gate 	a.flags = flags & ~MAP_TYPE;
5597c478bd9Sstevel@tonic-gate 	a.cred = CRED();
5607c478bd9Sstevel@tonic-gate 	a.amp = NULL;
5617c478bd9Sstevel@tonic-gate 	a.szc = 0;
5627c478bd9Sstevel@tonic-gate 	a.lgrp_mem_policy_flags = 0;
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	/*
5657c478bd9Sstevel@tonic-gate 	 * Call arch-specific map_pgsz routine to pick best page size to map
5667c478bd9Sstevel@tonic-gate 	 * this segment, and break the mapping up into parts if required.
5677c478bd9Sstevel@tonic-gate 	 *
5687c478bd9Sstevel@tonic-gate 	 * The parts work like this:
5697c478bd9Sstevel@tonic-gate 	 *
5707c478bd9Sstevel@tonic-gate 	 * addr		---------
5717c478bd9Sstevel@tonic-gate 	 *		|	| l0
5727c478bd9Sstevel@tonic-gate 	 *		---------
5737c478bd9Sstevel@tonic-gate 	 *		|	| l1
5747c478bd9Sstevel@tonic-gate 	 *		---------
5757c478bd9Sstevel@tonic-gate 	 *		|	| l2
5767c478bd9Sstevel@tonic-gate 	 *		---------
5777c478bd9Sstevel@tonic-gate 	 *		|	| l3
5787c478bd9Sstevel@tonic-gate 	 *		---------
5797c478bd9Sstevel@tonic-gate 	 *		|	| l4
5807c478bd9Sstevel@tonic-gate 	 *		---------
5817c478bd9Sstevel@tonic-gate 	 * addr+len
5827c478bd9Sstevel@tonic-gate 	 *
5837c478bd9Sstevel@tonic-gate 	 * Starting from the middle, l2 is the number of bytes mapped by the
5847c478bd9Sstevel@tonic-gate 	 * selected large page.  l1 and l3 are mapped by auto_lpg_va_default
5857c478bd9Sstevel@tonic-gate 	 * page size pages, and l0 and l4 are mapped by base page size pages.
5867c478bd9Sstevel@tonic-gate 	 * If auto_lpg_va_default is the base page size, then l0 == l4 == 0.
5877c478bd9Sstevel@tonic-gate 	 * If the requested address or length are aligned to the selected large
5887c478bd9Sstevel@tonic-gate 	 * page size, l1 or l3 may also be 0.
5897c478bd9Sstevel@tonic-gate 	 */
5907c478bd9Sstevel@tonic-gate 	if (use_zmap_lpg) {
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 		pgsz = map_pgsz(MAPPGSZ_VA, p, *addrp, len, NULL);
5937c478bd9Sstevel@tonic-gate 		if (pgsz <= PAGESIZE || len < pgsz) {
5947c478bd9Sstevel@tonic-gate 			return (as_map(as, *addrp, len, segvn_create, &a));
5957c478bd9Sstevel@tonic-gate 		}
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 		ruaddr = (caddr_t)P2ROUNDUP((uintptr_t)*addrp, pgsz);
5987c478bd9Sstevel@tonic-gate 		if (auto_lpg_va_default != MMU_PAGESIZE) {
5997c478bd9Sstevel@tonic-gate 			ruaddr0 = (caddr_t)P2ROUNDUP((uintptr_t)*addrp,
6007c478bd9Sstevel@tonic-gate 			    auto_lpg_va_default);
6017c478bd9Sstevel@tonic-gate 			l0 = ruaddr0 - *addrp;
6027c478bd9Sstevel@tonic-gate 		} else {
6037c478bd9Sstevel@tonic-gate 			l0 = 0;
6047c478bd9Sstevel@tonic-gate 			ruaddr0 = *addrp;
6057c478bd9Sstevel@tonic-gate 		}
6067c478bd9Sstevel@tonic-gate 		l1 = ruaddr - ruaddr0;
6077c478bd9Sstevel@tonic-gate 		l3 = P2PHASE(len - l0 - l1, pgsz);
6087c478bd9Sstevel@tonic-gate 		if (auto_lpg_va_default == MMU_PAGESIZE) {
6097c478bd9Sstevel@tonic-gate 			l4 = 0;
6107c478bd9Sstevel@tonic-gate 		} else {
6117c478bd9Sstevel@tonic-gate 			l4 = P2PHASE(l3, auto_lpg_va_default);
6127c478bd9Sstevel@tonic-gate 			l3 -= l4;
6137c478bd9Sstevel@tonic-gate 		}
6147c478bd9Sstevel@tonic-gate 		l2 = len - l0 - l1 - l3 - l4;
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 		if (l0) {
6177c478bd9Sstevel@tonic-gate 			b = a;
6187c478bd9Sstevel@tonic-gate 			err = as_map(as, *addrp, l0, segvn_create, &b);
6197c478bd9Sstevel@tonic-gate 			if (err) {
6207c478bd9Sstevel@tonic-gate 				return (err);
6217c478bd9Sstevel@tonic-gate 			}
6227c478bd9Sstevel@tonic-gate 		}
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 		if (l1) {
6257c478bd9Sstevel@tonic-gate 			b = a;
6267c478bd9Sstevel@tonic-gate 			b.szc = page_szc(auto_lpg_va_default);
6277c478bd9Sstevel@tonic-gate 			err = as_map(as, ruaddr0, l1, segvn_create, &b);
6287c478bd9Sstevel@tonic-gate 			if (err) {
6297c478bd9Sstevel@tonic-gate 				goto error1;
6307c478bd9Sstevel@tonic-gate 			}
6317c478bd9Sstevel@tonic-gate 		}
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 		if (l2) {
6347c478bd9Sstevel@tonic-gate 			b = a;
6357c478bd9Sstevel@tonic-gate 			b.szc = page_szc(pgsz);
6367c478bd9Sstevel@tonic-gate 			err = as_map(as, ruaddr, l2, segvn_create, &b);
6377c478bd9Sstevel@tonic-gate 			if (err) {
6387c478bd9Sstevel@tonic-gate 				goto error2;
6397c478bd9Sstevel@tonic-gate 			}
6407c478bd9Sstevel@tonic-gate 		}
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 		if (l3) {
6437c478bd9Sstevel@tonic-gate 			b = a;
6447c478bd9Sstevel@tonic-gate 			b.szc = page_szc(auto_lpg_va_default);
6457c478bd9Sstevel@tonic-gate 			err = as_map(as, ruaddr + l2, l3, segvn_create, &b);
6467c478bd9Sstevel@tonic-gate 			if (err) {
6477c478bd9Sstevel@tonic-gate 				goto error3;
6487c478bd9Sstevel@tonic-gate 			}
6497c478bd9Sstevel@tonic-gate 		}
6507c478bd9Sstevel@tonic-gate 		if (l4) {
6517c478bd9Sstevel@tonic-gate 			err = as_map(as, ruaddr + l2 + l3, l4, segvn_create,
6527c478bd9Sstevel@tonic-gate 			    &a);
6537c478bd9Sstevel@tonic-gate 			if (err) {
6547c478bd9Sstevel@tonic-gate error3:
6557c478bd9Sstevel@tonic-gate 				if (l3) {
6567c478bd9Sstevel@tonic-gate 					(void) as_unmap(as, ruaddr + l2, l3);
6577c478bd9Sstevel@tonic-gate 				}
6587c478bd9Sstevel@tonic-gate error2:
6597c478bd9Sstevel@tonic-gate 				if (l2) {
6607c478bd9Sstevel@tonic-gate 					(void) as_unmap(as, ruaddr, l2);
6617c478bd9Sstevel@tonic-gate 				}
6627c478bd9Sstevel@tonic-gate error1:
6637c478bd9Sstevel@tonic-gate 				if (l1) {
6647c478bd9Sstevel@tonic-gate 					(void) as_unmap(as, ruaddr0, l1);
6657c478bd9Sstevel@tonic-gate 				}
6667c478bd9Sstevel@tonic-gate 				if (l0) {
6677c478bd9Sstevel@tonic-gate 					(void) as_unmap(as, *addrp, l0);
6687c478bd9Sstevel@tonic-gate 				}
6697c478bd9Sstevel@tonic-gate 				return (err);
6707c478bd9Sstevel@tonic-gate 			}
6717c478bd9Sstevel@tonic-gate 		}
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 		return (0);
6747c478bd9Sstevel@tonic-gate 	}
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 	return (as_map(as, *addrp, len, segvn_create, &a));
6777c478bd9Sstevel@tonic-gate }
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate static int
6807c478bd9Sstevel@tonic-gate smmap_common(caddr_t *addrp, size_t len,
6817c478bd9Sstevel@tonic-gate     int prot, int flags, struct file *fp, offset_t pos)
6827c478bd9Sstevel@tonic-gate {
6837c478bd9Sstevel@tonic-gate 	struct vnode *vp;
6847c478bd9Sstevel@tonic-gate 	struct as *as = curproc->p_as;
6857c478bd9Sstevel@tonic-gate 	uint_t uprot, maxprot, type;
6867c478bd9Sstevel@tonic-gate 	int error;
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	if ((flags & ~(MAP_SHARED | MAP_PRIVATE | MAP_FIXED | _MAP_NEW |
6897c478bd9Sstevel@tonic-gate 	    _MAP_LOW32 | MAP_NORESERVE | MAP_ANON | MAP_ALIGN |
6907c478bd9Sstevel@tonic-gate 	    MAP_TEXT | MAP_INITDATA)) != 0) {
6917c478bd9Sstevel@tonic-gate 		/* | MAP_RENAME */	/* not implemented, let user know */
6927c478bd9Sstevel@tonic-gate 		return (EINVAL);
6937c478bd9Sstevel@tonic-gate 	}
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	if ((flags & MAP_TEXT) && !(prot & PROT_EXEC)) {
6967c478bd9Sstevel@tonic-gate 		return (EINVAL);
6977c478bd9Sstevel@tonic-gate 	}
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	if ((flags & (MAP_TEXT | MAP_INITDATA)) == (MAP_TEXT | MAP_INITDATA)) {
7007c478bd9Sstevel@tonic-gate 		return (EINVAL);
7017c478bd9Sstevel@tonic-gate 	}
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate #if defined(__sparc)
7047c478bd9Sstevel@tonic-gate 	/*
7057c478bd9Sstevel@tonic-gate 	 * See if this is an "old mmap call".  If so, remember this
7067c478bd9Sstevel@tonic-gate 	 * fact and convert the flags value given to mmap to indicate
7077c478bd9Sstevel@tonic-gate 	 * the specified address in the system call must be used.
7087c478bd9Sstevel@tonic-gate 	 * _MAP_NEW is turned set by all new uses of mmap.
7097c478bd9Sstevel@tonic-gate 	 */
7107c478bd9Sstevel@tonic-gate 	if ((flags & _MAP_NEW) == 0)
7117c478bd9Sstevel@tonic-gate 		flags |= MAP_FIXED;
7127c478bd9Sstevel@tonic-gate #endif
7137c478bd9Sstevel@tonic-gate 	flags &= ~_MAP_NEW;
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	type = flags & MAP_TYPE;
7167c478bd9Sstevel@tonic-gate 	if (type != MAP_PRIVATE && type != MAP_SHARED)
7177c478bd9Sstevel@tonic-gate 		return (EINVAL);
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 	if (flags & MAP_ALIGN) {
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 		if (flags & MAP_FIXED)
7237c478bd9Sstevel@tonic-gate 			return (EINVAL);
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 		/* alignment needs to be a power of 2 >= page size */
7267c478bd9Sstevel@tonic-gate 		if (((uintptr_t)*addrp < PAGESIZE && (uintptr_t)*addrp != 0) ||
7277c478bd9Sstevel@tonic-gate 			!ISP2((uintptr_t)*addrp))
7287c478bd9Sstevel@tonic-gate 			return (EINVAL);
7297c478bd9Sstevel@tonic-gate 	}
7307c478bd9Sstevel@tonic-gate 	/*
7317c478bd9Sstevel@tonic-gate 	 * Check for bad lengths and file position.
7327c478bd9Sstevel@tonic-gate 	 * We let the VOP_MAP routine check for negative lengths
7337c478bd9Sstevel@tonic-gate 	 * since on some vnode types this might be appropriate.
7347c478bd9Sstevel@tonic-gate 	 */
7357c478bd9Sstevel@tonic-gate 	if (len == 0 || (pos & (u_offset_t)PAGEOFFSET) != 0)
7367c478bd9Sstevel@tonic-gate 		return (EINVAL);
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 	maxprot = PROT_ALL;		/* start out allowing all accesses */
7397c478bd9Sstevel@tonic-gate 	uprot = prot | PROT_USER;
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
7427c478bd9Sstevel@tonic-gate 		ASSERT(flags & MAP_ANON);
7437c478bd9Sstevel@tonic-gate 		as_rangelock(as);
7447c478bd9Sstevel@tonic-gate 		error = zmap(as, addrp, len, uprot, flags, pos);
7457c478bd9Sstevel@tonic-gate 		as_rangeunlock(as);
7467c478bd9Sstevel@tonic-gate 		return (error);
7477c478bd9Sstevel@tonic-gate 	} else if ((flags & MAP_ANON) != 0)
7487c478bd9Sstevel@tonic-gate 		return (EINVAL);
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 	/* Can't execute code from "noexec" mounted filesystem. */
7537c478bd9Sstevel@tonic-gate 	if ((vp->v_vfsp->vfs_flag & VFS_NOEXEC) != 0)
7547c478bd9Sstevel@tonic-gate 		maxprot &= ~PROT_EXEC;
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	/*
7577c478bd9Sstevel@tonic-gate 	 * These checks were added as part of large files.
7587c478bd9Sstevel@tonic-gate 	 *
759*49a63d68Speterte 	 * Return ENXIO if the initial position is negative; return EOVERFLOW
7607c478bd9Sstevel@tonic-gate 	 * if (offset + len) would overflow the maximum allowed offset for the
7617c478bd9Sstevel@tonic-gate 	 * type of file descriptor being used.
7627c478bd9Sstevel@tonic-gate 	 */
7637c478bd9Sstevel@tonic-gate 	if (vp->v_type == VREG) {
764*49a63d68Speterte 		if (pos < 0)
765*49a63d68Speterte 			return (ENXIO);
7667c478bd9Sstevel@tonic-gate 		if ((offset_t)len > (OFFSET_MAX(fp) - pos))
7677c478bd9Sstevel@tonic-gate 			return (EOVERFLOW);
7687c478bd9Sstevel@tonic-gate 	}
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	if (type == MAP_SHARED && (fp->f_flag & FWRITE) == 0) {
7717c478bd9Sstevel@tonic-gate 		/* no write access allowed */
7727c478bd9Sstevel@tonic-gate 		maxprot &= ~PROT_WRITE;
7737c478bd9Sstevel@tonic-gate 	}
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	/*
7767c478bd9Sstevel@tonic-gate 	 * XXX - Do we also adjust maxprot based on protections
7777c478bd9Sstevel@tonic-gate 	 * of the vnode?  E.g. if no execute permission is given
7787c478bd9Sstevel@tonic-gate 	 * on the vnode for the current user, maxprot probably
7797c478bd9Sstevel@tonic-gate 	 * should disallow PROT_EXEC also?  This is different
7807c478bd9Sstevel@tonic-gate 	 * from the write access as this would be a per vnode
7817c478bd9Sstevel@tonic-gate 	 * test as opposed to a per fd test for writability.
7827c478bd9Sstevel@tonic-gate 	 */
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	/*
7857c478bd9Sstevel@tonic-gate 	 * Verify that the specified protections are not greater than
7867c478bd9Sstevel@tonic-gate 	 * the maximum allowable protections.  Also test to make sure
7877c478bd9Sstevel@tonic-gate 	 * that the file descriptor does allows for read access since
7887c478bd9Sstevel@tonic-gate 	 * "write only" mappings are hard to do since normally we do
7897c478bd9Sstevel@tonic-gate 	 * the read from the file before the page can be written.
7907c478bd9Sstevel@tonic-gate 	 */
7917c478bd9Sstevel@tonic-gate 	if (((maxprot & uprot) != uprot) || (fp->f_flag & FREAD) == 0)
7927c478bd9Sstevel@tonic-gate 		return (EACCES);
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	/*
7957c478bd9Sstevel@tonic-gate 	 * If the user specified an address, do some simple checks here
7967c478bd9Sstevel@tonic-gate 	 */
7977c478bd9Sstevel@tonic-gate 	if ((flags & MAP_FIXED) != 0) {
7987c478bd9Sstevel@tonic-gate 		caddr_t userlimit;
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 		/*
8017c478bd9Sstevel@tonic-gate 		 * Use the user address.  First verify that
8027c478bd9Sstevel@tonic-gate 		 * the address to be used is page aligned.
8037c478bd9Sstevel@tonic-gate 		 * Then make some simple bounds checks.
8047c478bd9Sstevel@tonic-gate 		 */
8057c478bd9Sstevel@tonic-gate 		if (((uintptr_t)*addrp & PAGEOFFSET) != 0)
8067c478bd9Sstevel@tonic-gate 			return (EINVAL);
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 		userlimit = flags & _MAP_LOW32 ?
8097c478bd9Sstevel@tonic-gate 		    (caddr_t)USERLIMIT32 : as->a_userlimit;
8107c478bd9Sstevel@tonic-gate 		switch (valid_usr_range(*addrp, len, uprot, as, userlimit)) {
8117c478bd9Sstevel@tonic-gate 		case RANGE_OKAY:
8127c478bd9Sstevel@tonic-gate 			break;
8137c478bd9Sstevel@tonic-gate 		case RANGE_BADPROT:
8147c478bd9Sstevel@tonic-gate 			return (ENOTSUP);
8157c478bd9Sstevel@tonic-gate 		case RANGE_BADADDR:
8167c478bd9Sstevel@tonic-gate 		default:
8177c478bd9Sstevel@tonic-gate 			return (ENOMEM);
8187c478bd9Sstevel@tonic-gate 		}
8197c478bd9Sstevel@tonic-gate 	}
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 	/*
8237c478bd9Sstevel@tonic-gate 	 * Ok, now let the vnode map routine do its thing to set things up.
8247c478bd9Sstevel@tonic-gate 	 */
8257c478bd9Sstevel@tonic-gate 	error = VOP_MAP(vp, pos, as,
8267c478bd9Sstevel@tonic-gate 	    addrp, len, uprot, maxprot, flags, fp->f_cred);
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	if (error == 0) {
8297c478bd9Sstevel@tonic-gate 		if (vp->v_type == VREG &&
8307c478bd9Sstevel@tonic-gate 		    (flags & (MAP_TEXT | MAP_INITDATA)) != 0) {
8317c478bd9Sstevel@tonic-gate 			/*
8327c478bd9Sstevel@tonic-gate 			 * Mark this as an executable vnode
8337c478bd9Sstevel@tonic-gate 			 */
8347c478bd9Sstevel@tonic-gate 			mutex_enter(&vp->v_lock);
8357c478bd9Sstevel@tonic-gate 			vp->v_flag |= VVMEXEC;
8367c478bd9Sstevel@tonic-gate 			mutex_exit(&vp->v_lock);
8377c478bd9Sstevel@tonic-gate 		}
8387c478bd9Sstevel@tonic-gate 	}
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 	return (error);
8417c478bd9Sstevel@tonic-gate }
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate #ifdef _LP64
8447c478bd9Sstevel@tonic-gate /*
8457c478bd9Sstevel@tonic-gate  * LP64 mmap(2) system call: 64-bit offset, 64-bit address.
8467c478bd9Sstevel@tonic-gate  *
8477c478bd9Sstevel@tonic-gate  * The "large file" mmap routine mmap64(2) is also mapped to this routine
8487c478bd9Sstevel@tonic-gate  * by the 64-bit version of libc.
8497c478bd9Sstevel@tonic-gate  *
8507c478bd9Sstevel@tonic-gate  * Eventually, this should be the only version, and have smmap_common()
8517c478bd9Sstevel@tonic-gate  * folded back into it again.  Some day.
8527c478bd9Sstevel@tonic-gate  */
8537c478bd9Sstevel@tonic-gate caddr_t
8547c478bd9Sstevel@tonic-gate smmap64(caddr_t addr, size_t len, int prot, int flags, int fd, off_t pos)
8557c478bd9Sstevel@tonic-gate {
8567c478bd9Sstevel@tonic-gate 	struct file *fp;
8577c478bd9Sstevel@tonic-gate 	int error;
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	if (flags & _MAP_LOW32)
8607c478bd9Sstevel@tonic-gate 		error = EINVAL;
8617c478bd9Sstevel@tonic-gate 	else if (fd == -1 && (flags & MAP_ANON) != 0)
8627c478bd9Sstevel@tonic-gate 		error = smmap_common(&addr, len, prot, flags,
8637c478bd9Sstevel@tonic-gate 		    NULL, (offset_t)pos);
8647c478bd9Sstevel@tonic-gate 	else if ((fp = getf(fd)) != NULL) {
8657c478bd9Sstevel@tonic-gate 		error = smmap_common(&addr, len, prot, flags,
8667c478bd9Sstevel@tonic-gate 		    fp, (offset_t)pos);
8677c478bd9Sstevel@tonic-gate 		releasef(fd);
8687c478bd9Sstevel@tonic-gate 	} else
8697c478bd9Sstevel@tonic-gate 		error = EBADF;
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 	return (error ? (caddr_t)(uintptr_t)set_errno(error) : addr);
8727c478bd9Sstevel@tonic-gate }
8737c478bd9Sstevel@tonic-gate #endif	/* _LP64 */
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL) || defined(_ILP32)
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate /*
8787c478bd9Sstevel@tonic-gate  * ILP32 mmap(2) system call: 32-bit offset, 32-bit address.
8797c478bd9Sstevel@tonic-gate  */
8807c478bd9Sstevel@tonic-gate caddr_t
8817c478bd9Sstevel@tonic-gate smmap32(caddr32_t addr, size32_t len, int prot, int flags, int fd, off32_t pos)
8827c478bd9Sstevel@tonic-gate {
8837c478bd9Sstevel@tonic-gate 	struct file *fp;
8847c478bd9Sstevel@tonic-gate 	int error;
8857c478bd9Sstevel@tonic-gate 	caddr_t a = (caddr_t)(uintptr_t)addr;
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	if (flags & _MAP_LOW32)
8887c478bd9Sstevel@tonic-gate 		error = EINVAL;
8897c478bd9Sstevel@tonic-gate 	else if (fd == -1 && (flags & MAP_ANON) != 0)
8907c478bd9Sstevel@tonic-gate 		error = smmap_common(&a, (size_t)len, prot,
8917c478bd9Sstevel@tonic-gate 		    flags | _MAP_LOW32, NULL, (offset_t)pos);
8927c478bd9Sstevel@tonic-gate 	else if ((fp = getf(fd)) != NULL) {
8937c478bd9Sstevel@tonic-gate 		error = smmap_common(&a, (size_t)len, prot,
8947c478bd9Sstevel@tonic-gate 		    flags | _MAP_LOW32, fp, (offset_t)pos);
8957c478bd9Sstevel@tonic-gate 		releasef(fd);
8967c478bd9Sstevel@tonic-gate 	} else
8977c478bd9Sstevel@tonic-gate 		error = EBADF;
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	ASSERT(error != 0 || (uintptr_t)(a + len) < (uintptr_t)UINT32_MAX);
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	return (error ? (caddr_t)(uintptr_t)set_errno(error) : a);
9027c478bd9Sstevel@tonic-gate }
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate /*
9057c478bd9Sstevel@tonic-gate  * ILP32 mmap64(2) system call: 64-bit offset, 32-bit address.
9067c478bd9Sstevel@tonic-gate  *
9077c478bd9Sstevel@tonic-gate  * Now things really get ugly because we can't use the C-style
9087c478bd9Sstevel@tonic-gate  * calling convention for more than 6 args, and 64-bit parameter
9097c478bd9Sstevel@tonic-gate  * passing on 32-bit systems is less than clean.
9107c478bd9Sstevel@tonic-gate  */
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate struct mmaplf32a {
9137c478bd9Sstevel@tonic-gate 	caddr_t addr;
9147c478bd9Sstevel@tonic-gate 	size_t len;
9157c478bd9Sstevel@tonic-gate #ifdef _LP64
9167c478bd9Sstevel@tonic-gate 	/*
9177c478bd9Sstevel@tonic-gate 	 * 32-bit contents, 64-bit cells
9187c478bd9Sstevel@tonic-gate 	 */
9197c478bd9Sstevel@tonic-gate 	uint64_t prot;
9207c478bd9Sstevel@tonic-gate 	uint64_t flags;
9217c478bd9Sstevel@tonic-gate 	uint64_t fd;
9227c478bd9Sstevel@tonic-gate 	uint64_t offhi;
9237c478bd9Sstevel@tonic-gate 	uint64_t offlo;
9247c478bd9Sstevel@tonic-gate #else
9257c478bd9Sstevel@tonic-gate 	/*
9267c478bd9Sstevel@tonic-gate 	 * 32-bit contents, 32-bit cells
9277c478bd9Sstevel@tonic-gate 	 */
9287c478bd9Sstevel@tonic-gate 	uint32_t prot;
9297c478bd9Sstevel@tonic-gate 	uint32_t flags;
9307c478bd9Sstevel@tonic-gate 	uint32_t fd;
9317c478bd9Sstevel@tonic-gate 	uint32_t offhi;
9327c478bd9Sstevel@tonic-gate 	uint32_t offlo;
9337c478bd9Sstevel@tonic-gate #endif
9347c478bd9Sstevel@tonic-gate };
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate int
9377c478bd9Sstevel@tonic-gate smmaplf32(struct mmaplf32a *uap, rval_t *rvp)
9387c478bd9Sstevel@tonic-gate {
9397c478bd9Sstevel@tonic-gate 	struct file *fp;
9407c478bd9Sstevel@tonic-gate 	int error;
9417c478bd9Sstevel@tonic-gate 	caddr_t a = uap->addr;
9427c478bd9Sstevel@tonic-gate 	int flags = (int)uap->flags;
9437c478bd9Sstevel@tonic-gate 	int fd = (int)uap->fd;
9447c478bd9Sstevel@tonic-gate #ifdef _BIG_ENDIAN
9457c478bd9Sstevel@tonic-gate 	offset_t off = ((u_offset_t)uap->offhi << 32) | (u_offset_t)uap->offlo;
9467c478bd9Sstevel@tonic-gate #else
9477c478bd9Sstevel@tonic-gate 	offset_t off = ((u_offset_t)uap->offlo << 32) | (u_offset_t)uap->offhi;
9487c478bd9Sstevel@tonic-gate #endif
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 	if (flags & _MAP_LOW32)
9517c478bd9Sstevel@tonic-gate 		error = EINVAL;
9527c478bd9Sstevel@tonic-gate 	else if (fd == -1 && (flags & MAP_ANON) != 0)
9537c478bd9Sstevel@tonic-gate 		error = smmap_common(&a, uap->len, (int)uap->prot,
9547c478bd9Sstevel@tonic-gate 		    flags | _MAP_LOW32, NULL, off);
9557c478bd9Sstevel@tonic-gate 	else if ((fp = getf(fd)) != NULL) {
9567c478bd9Sstevel@tonic-gate 		error = smmap_common(&a, uap->len, (int)uap->prot,
9577c478bd9Sstevel@tonic-gate 		    flags | _MAP_LOW32, fp, off);
9587c478bd9Sstevel@tonic-gate 		releasef(fd);
9597c478bd9Sstevel@tonic-gate 	} else
9607c478bd9Sstevel@tonic-gate 		error = EBADF;
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 	if (error == 0)
9637c478bd9Sstevel@tonic-gate 		rvp->r_val1 = (uintptr_t)a;
9647c478bd9Sstevel@tonic-gate 	return (error);
9657c478bd9Sstevel@tonic-gate }
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL || _ILP32 */
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate int
9707c478bd9Sstevel@tonic-gate munmap(caddr_t addr, size_t len)
9717c478bd9Sstevel@tonic-gate {
9727c478bd9Sstevel@tonic-gate 	struct proc *p = curproc;
9737c478bd9Sstevel@tonic-gate 	struct as *as = p->p_as;
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 	if (((uintptr_t)addr & PAGEOFFSET) != 0 || len == 0)
9767c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 	if (valid_usr_range(addr, len, 0, as, as->a_userlimit) != RANGE_OKAY)
9797c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 	/*
9827c478bd9Sstevel@tonic-gate 	 * Discard lwpchan mappings.
9837c478bd9Sstevel@tonic-gate 	 */
9847c478bd9Sstevel@tonic-gate 	if (p->p_lcp != NULL)
9857c478bd9Sstevel@tonic-gate 		lwpchan_delete_mapping(p, addr, addr + len);
9867c478bd9Sstevel@tonic-gate 	if (as_unmap(as, addr, len) != 0)
9877c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 	return (0);
9907c478bd9Sstevel@tonic-gate }
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate int
9937c478bd9Sstevel@tonic-gate mprotect(caddr_t addr, size_t len, int prot)
9947c478bd9Sstevel@tonic-gate {
9957c478bd9Sstevel@tonic-gate 	struct as *as = curproc->p_as;
9967c478bd9Sstevel@tonic-gate 	uint_t uprot = prot | PROT_USER;
9977c478bd9Sstevel@tonic-gate 	int error;
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 	if (((uintptr_t)addr & PAGEOFFSET) != 0 || len == 0)
10007c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 	switch (valid_usr_range(addr, len, prot, as, as->a_userlimit)) {
10037c478bd9Sstevel@tonic-gate 	case RANGE_OKAY:
10047c478bd9Sstevel@tonic-gate 		break;
10057c478bd9Sstevel@tonic-gate 	case RANGE_BADPROT:
10067c478bd9Sstevel@tonic-gate 		return (set_errno(ENOTSUP));
10077c478bd9Sstevel@tonic-gate 	case RANGE_BADADDR:
10087c478bd9Sstevel@tonic-gate 	default:
10097c478bd9Sstevel@tonic-gate 		return (set_errno(ENOMEM));
10107c478bd9Sstevel@tonic-gate 	}
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 	error = as_setprot(as, addr, len, uprot);
10137c478bd9Sstevel@tonic-gate 	if (error)
10147c478bd9Sstevel@tonic-gate 		return (set_errno(error));
10157c478bd9Sstevel@tonic-gate 	return (0);
10167c478bd9Sstevel@tonic-gate }
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate #define	MC_CACHE	128			/* internal result buffer */
10197c478bd9Sstevel@tonic-gate #define	MC_QUANTUM	(MC_CACHE * PAGESIZE)	/* addresses covered in loop */
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate int
10227c478bd9Sstevel@tonic-gate mincore(caddr_t addr, size_t len, char *vecp)
10237c478bd9Sstevel@tonic-gate {
10247c478bd9Sstevel@tonic-gate 	struct as *as = curproc->p_as;
10257c478bd9Sstevel@tonic-gate 	caddr_t ea;			/* end address of loop */
10267c478bd9Sstevel@tonic-gate 	size_t rl;			/* inner result length */
10277c478bd9Sstevel@tonic-gate 	char vec[MC_CACHE];		/* local vector cache */
10287c478bd9Sstevel@tonic-gate 	int error;
10297c478bd9Sstevel@tonic-gate 	model_t model;
10307c478bd9Sstevel@tonic-gate 	long	llen;
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 	model = get_udatamodel();
10337c478bd9Sstevel@tonic-gate 	/*
10347c478bd9Sstevel@tonic-gate 	 * Validate form of address parameters.
10357c478bd9Sstevel@tonic-gate 	 */
10367c478bd9Sstevel@tonic-gate 	if (model == DATAMODEL_NATIVE) {
10377c478bd9Sstevel@tonic-gate 		llen = (long)len;
10387c478bd9Sstevel@tonic-gate 	} else {
10397c478bd9Sstevel@tonic-gate 		llen = (int32_t)(size32_t)len;
10407c478bd9Sstevel@tonic-gate 	}
10417c478bd9Sstevel@tonic-gate 	if (((uintptr_t)addr & PAGEOFFSET) != 0 || llen <= 0)
10427c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 	if (valid_usr_range(addr, len, 0, as, as->a_userlimit) != RANGE_OKAY)
10457c478bd9Sstevel@tonic-gate 		return (set_errno(ENOMEM));
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 	/*
10487c478bd9Sstevel@tonic-gate 	 * Loop over subranges of interval [addr : addr + len), recovering
10497c478bd9Sstevel@tonic-gate 	 * results internally and then copying them out to caller.  Subrange
10507c478bd9Sstevel@tonic-gate 	 * is based on the size of MC_CACHE, defined above.
10517c478bd9Sstevel@tonic-gate 	 */
10527c478bd9Sstevel@tonic-gate 	for (ea = addr + len; addr < ea; addr += MC_QUANTUM) {
10537c478bd9Sstevel@tonic-gate 		error = as_incore(as, addr,
10547c478bd9Sstevel@tonic-gate 		    (size_t)MIN(MC_QUANTUM, ea - addr), vec, &rl);
10557c478bd9Sstevel@tonic-gate 		if (rl != 0) {
10567c478bd9Sstevel@tonic-gate 			rl = (rl + PAGESIZE - 1) / PAGESIZE;
10577c478bd9Sstevel@tonic-gate 			if (copyout(vec, vecp, rl) != 0)
10587c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
10597c478bd9Sstevel@tonic-gate 			vecp += rl;
10607c478bd9Sstevel@tonic-gate 		}
10617c478bd9Sstevel@tonic-gate 		if (error != 0)
10627c478bd9Sstevel@tonic-gate 			return (set_errno(ENOMEM));
10637c478bd9Sstevel@tonic-gate 	}
10647c478bd9Sstevel@tonic-gate 	return (0);
10657c478bd9Sstevel@tonic-gate }
1066