xref: /illumos-gate/usr/src/uts/common/os/mmapobj.c (revision 9174bfaa)
10616c1c3SMichael Corcoran /*
20616c1c3SMichael Corcoran  * CDDL HEADER START
30616c1c3SMichael Corcoran  *
40616c1c3SMichael Corcoran  * The contents of this file are subject to the terms of the
50616c1c3SMichael Corcoran  * Common Development and Distribution License (the "License").
60616c1c3SMichael Corcoran  * You may not use this file except in compliance with the License.
70616c1c3SMichael Corcoran  *
80616c1c3SMichael Corcoran  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90616c1c3SMichael Corcoran  * or http://www.opensolaris.org/os/licensing.
100616c1c3SMichael Corcoran  * See the License for the specific language governing permissions
110616c1c3SMichael Corcoran  * and limitations under the License.
120616c1c3SMichael Corcoran  *
130616c1c3SMichael Corcoran  * When distributing Covered Code, include this CDDL HEADER in each
140616c1c3SMichael Corcoran  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150616c1c3SMichael Corcoran  * If applicable, add the following below this CDDL HEADER, with the
160616c1c3SMichael Corcoran  * fields enclosed by brackets "[]" replaced with your own identifying
170616c1c3SMichael Corcoran  * information: Portions Copyright [yyyy] [name of copyright owner]
180616c1c3SMichael Corcoran  *
190616c1c3SMichael Corcoran  * CDDL HEADER END
200616c1c3SMichael Corcoran  */
210616c1c3SMichael Corcoran /*
22980cf3cfSMichael Corcoran  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230616c1c3SMichael Corcoran  * Use is subject to license terms.
24386e9c9eSJerry Jelinek  * Copyright 2014 Joyent, Inc.  All rights reserved.
25*9174bfaaSGarrett D'Amore  * Copyright 2022 Garrett D'Amore <garrett@damore.org>
260616c1c3SMichael Corcoran  */
270616c1c3SMichael Corcoran 
280616c1c3SMichael Corcoran #include <sys/types.h>
290616c1c3SMichael Corcoran #include <sys/sysmacros.h>
300616c1c3SMichael Corcoran #include <sys/kmem.h>
310616c1c3SMichael Corcoran #include <sys/param.h>
320616c1c3SMichael Corcoran #include <sys/systm.h>
330616c1c3SMichael Corcoran #include <sys/errno.h>
340616c1c3SMichael Corcoran #include <sys/mman.h>
350616c1c3SMichael Corcoran #include <sys/cmn_err.h>
360616c1c3SMichael Corcoran #include <sys/cred.h>
370616c1c3SMichael Corcoran #include <sys/vmsystm.h>
3879b24695SMichael Corcoran #include <sys/machsystm.h>
390616c1c3SMichael Corcoran #include <sys/debug.h>
400616c1c3SMichael Corcoran #include <vm/as.h>
410616c1c3SMichael Corcoran #include <vm/seg.h>
420616c1c3SMichael Corcoran #include <sys/vmparam.h>
430616c1c3SMichael Corcoran #include <sys/vfs.h>
440616c1c3SMichael Corcoran #include <sys/elf.h>
450616c1c3SMichael Corcoran #include <sys/machelf.h>
460616c1c3SMichael Corcoran #include <sys/corectl.h>
470616c1c3SMichael Corcoran #include <sys/exec.h>
480616c1c3SMichael Corcoran #include <sys/exechdr.h>
490616c1c3SMichael Corcoran #include <sys/autoconf.h>
500616c1c3SMichael Corcoran #include <sys/mem.h>
510616c1c3SMichael Corcoran #include <vm/seg_dev.h>
520616c1c3SMichael Corcoran #include <sys/vmparam.h>
530616c1c3SMichael Corcoran #include <sys/mmapobj.h>
540616c1c3SMichael Corcoran #include <sys/atomic.h>
550616c1c3SMichael Corcoran 
560616c1c3SMichael Corcoran /*
570616c1c3SMichael Corcoran  * Theory statement:
580616c1c3SMichael Corcoran  *
590616c1c3SMichael Corcoran  * The main driving force behind mmapobj is to interpret and map ELF files
600616c1c3SMichael Corcoran  * inside of the kernel instead of having the linker be responsible for this.
610616c1c3SMichael Corcoran  *
620616c1c3SMichael Corcoran  * mmapobj also supports the AOUT 4.x binary format as well as flat files in
630616c1c3SMichael Corcoran  * a read only manner.
640616c1c3SMichael Corcoran  *
650616c1c3SMichael Corcoran  * When interpreting and mapping an ELF file, mmapobj will map each PT_LOAD
660616c1c3SMichael Corcoran  * or PT_SUNWBSS segment according to the ELF standard.  Refer to the "Linker
670616c1c3SMichael Corcoran  * and Libraries Guide" for more information about the standard and mapping
680616c1c3SMichael Corcoran  * rules.
690616c1c3SMichael Corcoran  *
700616c1c3SMichael Corcoran  * Having mmapobj interpret and map objects will allow the kernel to make the
710616c1c3SMichael Corcoran  * best decision for where to place the mappings for said objects.  Thus, we
72d2a70789SRichard Lowe  * can make optimizations inside of the kernel for specific platforms or cache
73d2a70789SRichard Lowe  * mapping information to make mapping objects faster.  The cache is ignored
74d2a70789SRichard Lowe  * if ASLR is enabled.
750616c1c3SMichael Corcoran  *
760616c1c3SMichael Corcoran  * The lib_va_hash will be one such optimization.  For each ELF object that
770616c1c3SMichael Corcoran  * mmapobj is asked to interpret, we will attempt to cache the information
780616c1c3SMichael Corcoran  * about the PT_LOAD and PT_SUNWBSS sections to speed up future mappings of
790616c1c3SMichael Corcoran  * the same objects.  We will cache up to LIBVA_CACHED_SEGS (see below) program
800616c1c3SMichael Corcoran  * headers which should cover a majority of the libraries out there without
810616c1c3SMichael Corcoran  * wasting space.  In order to make sure that the cached information is valid,
820616c1c3SMichael Corcoran  * we check the passed in vnode's mtime and ctime to make sure the vnode
830616c1c3SMichael Corcoran  * has not been modified since the last time we used it.
840616c1c3SMichael Corcoran  *
850616c1c3SMichael Corcoran  * In addition, the lib_va_hash may contain a preferred starting VA for the
860616c1c3SMichael Corcoran  * object which can be useful for platforms which support a shared context.
870616c1c3SMichael Corcoran  * This will increase the likelyhood that library text can be shared among
880616c1c3SMichael Corcoran  * many different processes.  We limit the reserved VA space for 32 bit objects
890616c1c3SMichael Corcoran  * in order to minimize fragmenting the processes address space.
900616c1c3SMichael Corcoran  *
910616c1c3SMichael Corcoran  * In addition to the above, the mmapobj interface allows for padding to be
920616c1c3SMichael Corcoran  * requested before the first mapping and after the last mapping created.
930616c1c3SMichael Corcoran  * When padding is requested, no additional optimizations will be made for
940616c1c3SMichael Corcoran  * that request.
950616c1c3SMichael Corcoran  */
960616c1c3SMichael Corcoran 
970616c1c3SMichael Corcoran /*
980616c1c3SMichael Corcoran  * Threshold to prevent allocating too much kernel memory to read in the
990616c1c3SMichael Corcoran  * program headers for an object.  If it requires more than below,
1000616c1c3SMichael Corcoran  * we will use a KM_NOSLEEP allocation to allocate memory to hold all of the
1010616c1c3SMichael Corcoran  * program headers which could possibly fail.  If less memory than below is
1020616c1c3SMichael Corcoran  * needed, then we use a KM_SLEEP allocation and are willing to wait for the
1030616c1c3SMichael Corcoran  * memory if we need to.
1040616c1c3SMichael Corcoran  */
1050616c1c3SMichael Corcoran size_t mmapobj_alloc_threshold = 65536;
1060616c1c3SMichael Corcoran 
1070616c1c3SMichael Corcoran /* Debug stats for test coverage */
1080616c1c3SMichael Corcoran #ifdef DEBUG
1090616c1c3SMichael Corcoran struct mobj_stats {
1100616c1c3SMichael Corcoran 	uint_t	mobjs_unmap_called;
1110616c1c3SMichael Corcoran 	uint_t	mobjs_remap_devnull;
1120616c1c3SMichael Corcoran 	uint_t	mobjs_lookup_start;
1130616c1c3SMichael Corcoran 	uint_t	mobjs_alloc_start;
1140616c1c3SMichael Corcoran 	uint_t	mobjs_alloc_vmem;
1150616c1c3SMichael Corcoran 	uint_t	mobjs_add_collision;
1160616c1c3SMichael Corcoran 	uint_t	mobjs_get_addr;
1170616c1c3SMichael Corcoran 	uint_t	mobjs_map_flat_no_padding;
1180616c1c3SMichael Corcoran 	uint_t	mobjs_map_flat_padding;
1190616c1c3SMichael Corcoran 	uint_t	mobjs_map_ptload_text;
1200616c1c3SMichael Corcoran 	uint_t	mobjs_map_ptload_initdata;
1210616c1c3SMichael Corcoran 	uint_t	mobjs_map_ptload_preread;
1220616c1c3SMichael Corcoran 	uint_t	mobjs_map_ptload_unaligned_text;
1230616c1c3SMichael Corcoran 	uint_t	mobjs_map_ptload_unaligned_map_fail;
1240616c1c3SMichael Corcoran 	uint_t	mobjs_map_ptload_unaligned_read_fail;
1250616c1c3SMichael Corcoran 	uint_t	mobjs_zfoddiff;
1260616c1c3SMichael Corcoran 	uint_t	mobjs_zfoddiff_nowrite;
1270616c1c3SMichael Corcoran 	uint_t	mobjs_zfodextra;
1280616c1c3SMichael Corcoran 	uint_t	mobjs_ptload_failed;
1290616c1c3SMichael Corcoran 	uint_t	mobjs_map_elf_no_holes;
1300616c1c3SMichael Corcoran 	uint_t	mobjs_unmap_hole;
1310616c1c3SMichael Corcoran 	uint_t	mobjs_nomem_header;
132980cf3cfSMichael Corcoran 	uint_t	mobjs_inval_header;
1330616c1c3SMichael Corcoran 	uint_t	mobjs_overlap_header;
1340616c1c3SMichael Corcoran 	uint_t	mobjs_np2_align;
1350616c1c3SMichael Corcoran 	uint_t	mobjs_np2_align_overflow;
1360616c1c3SMichael Corcoran 	uint_t	mobjs_exec_padding;
1370616c1c3SMichael Corcoran 	uint_t	mobjs_exec_addr_mapped;
1380616c1c3SMichael Corcoran 	uint_t	mobjs_exec_addr_devnull;
1390616c1c3SMichael Corcoran 	uint_t	mobjs_exec_addr_in_use;
1400616c1c3SMichael Corcoran 	uint_t	mobjs_lvp_found;
1410616c1c3SMichael Corcoran 	uint_t	mobjs_no_loadable_yet;
1420616c1c3SMichael Corcoran 	uint_t	mobjs_nothing_to_map;
1430616c1c3SMichael Corcoran 	uint_t	mobjs_e2big;
1440616c1c3SMichael Corcoran 	uint_t	mobjs_dyn_pad_align;
1450616c1c3SMichael Corcoran 	uint_t	mobjs_dyn_pad_noalign;
1460616c1c3SMichael Corcoran 	uint_t	mobjs_alloc_start_fail;
1470616c1c3SMichael Corcoran 	uint_t	mobjs_lvp_nocache;
1480616c1c3SMichael Corcoran 	uint_t	mobjs_extra_padding;
1490616c1c3SMichael Corcoran 	uint_t	mobjs_lvp_not_needed;
1500616c1c3SMichael Corcoran 	uint_t	mobjs_no_mem_map_sz;
1510616c1c3SMichael Corcoran 	uint_t	mobjs_check_exec_failed;
1520616c1c3SMichael Corcoran 	uint_t	mobjs_lvp_used;
1530616c1c3SMichael Corcoran 	uint_t	mobjs_wrong_model;
1540616c1c3SMichael Corcoran 	uint_t	mobjs_noexec_fs;
1550616c1c3SMichael Corcoran 	uint_t	mobjs_e2big_et_rel;
1560616c1c3SMichael Corcoran 	uint_t	mobjs_et_rel_mapped;
1570616c1c3SMichael Corcoran 	uint_t	mobjs_unknown_elf_type;
1580616c1c3SMichael Corcoran 	uint_t	mobjs_phent32_too_small;
1590616c1c3SMichael Corcoran 	uint_t	mobjs_phent64_too_small;
1600616c1c3SMichael Corcoran 	uint_t	mobjs_inval_elf_class;
1610616c1c3SMichael Corcoran 	uint_t	mobjs_too_many_phdrs;
1620616c1c3SMichael Corcoran 	uint_t	mobjs_no_phsize;
1630616c1c3SMichael Corcoran 	uint_t	mobjs_phsize_large;
1640616c1c3SMichael Corcoran 	uint_t	mobjs_phsize_xtralarge;
1650616c1c3SMichael Corcoran 	uint_t	mobjs_fast_wrong_model;
1660616c1c3SMichael Corcoran 	uint_t	mobjs_fast_e2big;
1670616c1c3SMichael Corcoran 	uint_t	mobjs_fast;
1680616c1c3SMichael Corcoran 	uint_t	mobjs_fast_success;
1690616c1c3SMichael Corcoran 	uint_t	mobjs_fast_not_now;
1700616c1c3SMichael Corcoran 	uint_t	mobjs_small_file;
1710616c1c3SMichael Corcoran 	uint_t	mobjs_read_error;
1720616c1c3SMichael Corcoran 	uint_t	mobjs_unsupported;
1730616c1c3SMichael Corcoran 	uint_t	mobjs_flat_e2big;
1740616c1c3SMichael Corcoran 	uint_t	mobjs_phent_align32;
1750616c1c3SMichael Corcoran 	uint_t	mobjs_phent_align64;
1760616c1c3SMichael Corcoran 	uint_t	mobjs_lib_va_find_hit;
1770616c1c3SMichael Corcoran 	uint_t	mobjs_lib_va_find_delay_delete;
1780616c1c3SMichael Corcoran 	uint_t	mobjs_lib_va_find_delete;
1790616c1c3SMichael Corcoran 	uint_t	mobjs_lib_va_add_delay_delete;
1800616c1c3SMichael Corcoran 	uint_t	mobjs_lib_va_add_delete;
18179b24695SMichael Corcoran 	uint_t	mobjs_lib_va_create_failure;
182c1106aa8SMichael Corcoran 	uint_t	mobjs_min_align;
1830616c1c3SMichael Corcoran } mobj_stats;
1840616c1c3SMichael Corcoran 
1850616c1c3SMichael Corcoran #define	MOBJ_STAT_ADD(stat)		((mobj_stats.mobjs_##stat)++)
1860616c1c3SMichael Corcoran #else
1870616c1c3SMichael Corcoran #define	MOBJ_STAT_ADD(stat)
1880616c1c3SMichael Corcoran #endif
1890616c1c3SMichael Corcoran 
1901e1e1eecSMichael Corcoran /*
1911e1e1eecSMichael Corcoran  * Check if addr is at or above the address space reserved for the stack.
1921e1e1eecSMichael Corcoran  * The stack is at the top of the address space for all sparc processes
1931e1e1eecSMichael Corcoran  * and 64 bit x86 processes.  For 32 bit x86, the stack is not at the top
1941e1e1eecSMichael Corcoran  * of the address space and thus this check wil always return false for
1951e1e1eecSMichael Corcoran  * 32 bit x86 processes.
1961e1e1eecSMichael Corcoran  */
1971e1e1eecSMichael Corcoran #if defined(__sparc)
1981e1e1eecSMichael Corcoran #define	OVERLAPS_STACK(addr, p)						\
1991e1e1eecSMichael Corcoran 	(addr >= (p->p_usrstack - ((p->p_stk_ctl + PAGEOFFSET) & PAGEMASK)))
2001e1e1eecSMichael Corcoran #elif defined(__amd64)
2011e1e1eecSMichael Corcoran #define	OVERLAPS_STACK(addr, p)						\
2021e1e1eecSMichael Corcoran 	((p->p_model == DATAMODEL_LP64) &&				\
2031e1e1eecSMichael Corcoran 	(addr >= (p->p_usrstack - ((p->p_stk_ctl + PAGEOFFSET) & PAGEMASK))))
2041e1e1eecSMichael Corcoran #endif
2051e1e1eecSMichael Corcoran 
2060616c1c3SMichael Corcoran /* lv_flags values - bitmap */
2070616c1c3SMichael Corcoran #define	LV_ELF32	0x1		/* 32 bit ELF file */
2080616c1c3SMichael Corcoran #define	LV_ELF64	0x2		/* 64 bit ELF file */
2090616c1c3SMichael Corcoran #define	LV_DEL		0x4		/* delete when lv_refcnt hits zero */
2100616c1c3SMichael Corcoran 
2110616c1c3SMichael Corcoran /*
2120616c1c3SMichael Corcoran  * Note: lv_num_segs will denote how many segments this file has and will
2130616c1c3SMichael Corcoran  * only be set after the lv_mps array has been filled out.
2140616c1c3SMichael Corcoran  * lv_mps can only be valid if lv_num_segs is non-zero.
2150616c1c3SMichael Corcoran  */
2160616c1c3SMichael Corcoran struct lib_va {
2170616c1c3SMichael Corcoran 	struct lib_va		*lv_next;
2180616c1c3SMichael Corcoran 	caddr_t			lv_base_va;	/* start va for library */
2190616c1c3SMichael Corcoran 	ssize_t			lv_len;		/* total va span of library */
2200616c1c3SMichael Corcoran 	size_t			lv_align;	/* minimum alignment */
2210616c1c3SMichael Corcoran 	uint64_t		lv_nodeid;	/* filesystem node id */
2220616c1c3SMichael Corcoran 	uint64_t		lv_fsid;	/* filesystem id */
2230616c1c3SMichael Corcoran 	timestruc_t		lv_ctime;	/* last time file was changed */
2240616c1c3SMichael Corcoran 	timestruc_t		lv_mtime;	/* or modified */
2250616c1c3SMichael Corcoran 	mmapobj_result_t	lv_mps[LIBVA_CACHED_SEGS]; /* cached pheaders */
2260616c1c3SMichael Corcoran 	int			lv_num_segs;	/* # segs for this file */
2270616c1c3SMichael Corcoran 	int			lv_flags;
2280616c1c3SMichael Corcoran 	uint_t			lv_refcnt;	/* number of holds on struct */
2290616c1c3SMichael Corcoran };
2300616c1c3SMichael Corcoran 
2310616c1c3SMichael Corcoran #define	LIB_VA_SIZE	1024
2320616c1c3SMichael Corcoran #define	LIB_VA_MASK	(LIB_VA_SIZE - 1)
2330616c1c3SMichael Corcoran #define	LIB_VA_MUTEX_SHIFT	3
2340616c1c3SMichael Corcoran 
2350616c1c3SMichael Corcoran #if (LIB_VA_SIZE & (LIB_VA_SIZE - 1))
2360616c1c3SMichael Corcoran #error	"LIB_VA_SIZE is not a power of 2"
2370616c1c3SMichael Corcoran #endif
2380616c1c3SMichael Corcoran 
2390616c1c3SMichael Corcoran static struct lib_va *lib_va_hash[LIB_VA_SIZE];
2400616c1c3SMichael Corcoran static kmutex_t lib_va_hash_mutex[LIB_VA_SIZE >> LIB_VA_MUTEX_SHIFT];
2410616c1c3SMichael Corcoran 
2420616c1c3SMichael Corcoran #define	LIB_VA_HASH_MUTEX(index)					\
2430616c1c3SMichael Corcoran 	(&lib_va_hash_mutex[index >> LIB_VA_MUTEX_SHIFT])
2440616c1c3SMichael Corcoran 
2450616c1c3SMichael Corcoran #define	LIB_VA_HASH(nodeid)						\
2460616c1c3SMichael Corcoran 	(((nodeid) ^ ((nodeid) << 7) ^ ((nodeid) << 13)) & LIB_VA_MASK)
2470616c1c3SMichael Corcoran 
2480616c1c3SMichael Corcoran #define	LIB_VA_MATCH_ID(arg1, arg2)					\
2490616c1c3SMichael Corcoran 	((arg1)->lv_nodeid == (arg2)->va_nodeid &&			\
2500616c1c3SMichael Corcoran 	(arg1)->lv_fsid == (arg2)->va_fsid)
2510616c1c3SMichael Corcoran 
2520616c1c3SMichael Corcoran #define	LIB_VA_MATCH_TIME(arg1, arg2)					\
2530616c1c3SMichael Corcoran 	((arg1)->lv_ctime.tv_sec == (arg2)->va_ctime.tv_sec &&		\
2540616c1c3SMichael Corcoran 	(arg1)->lv_mtime.tv_sec == (arg2)->va_mtime.tv_sec &&		\
2550616c1c3SMichael Corcoran 	(arg1)->lv_ctime.tv_nsec == (arg2)->va_ctime.tv_nsec &&		\
2560616c1c3SMichael Corcoran 	(arg1)->lv_mtime.tv_nsec == (arg2)->va_mtime.tv_nsec)
2570616c1c3SMichael Corcoran 
2580616c1c3SMichael Corcoran #define	LIB_VA_MATCH(arg1, arg2)					\
2590616c1c3SMichael Corcoran 	(LIB_VA_MATCH_ID(arg1, arg2) && LIB_VA_MATCH_TIME(arg1, arg2))
2600616c1c3SMichael Corcoran 
2610616c1c3SMichael Corcoran /*
26279b24695SMichael Corcoran  * lib_va will be used for optimized allocation of address ranges for
26379b24695SMichael Corcoran  * libraries, such that subsequent mappings of the same library will attempt
26479b24695SMichael Corcoran  * to use the same VA as previous mappings of that library.
2650616c1c3SMichael Corcoran  * In order to map libraries at the same VA in many processes, we need to carve
2660616c1c3SMichael Corcoran  * out our own address space for them which is unique across many processes.
2670616c1c3SMichael Corcoran  * We use different arenas for 32 bit and 64 bit libraries.
2680616c1c3SMichael Corcoran  *
2690616c1c3SMichael Corcoran  * Since the 32 bit address space is relatively small, we limit the number of
2700616c1c3SMichael Corcoran  * libraries which try to use consistent virtual addresses to lib_threshold.
2710616c1c3SMichael Corcoran  * For 64 bit libraries there is no such limit since the address space is large.
2720616c1c3SMichael Corcoran  */
2730616c1c3SMichael Corcoran static vmem_t *lib_va_32_arena;
2740616c1c3SMichael Corcoran static vmem_t *lib_va_64_arena;
2750616c1c3SMichael Corcoran uint_t lib_threshold = 20;	/* modifiable via /etc/system */
2760616c1c3SMichael Corcoran 
27779b24695SMichael Corcoran static kmutex_t lib_va_init_mutex;	/* no need to initialize */
27879b24695SMichael Corcoran 
2790616c1c3SMichael Corcoran /*
2800616c1c3SMichael Corcoran  * Number of 32 bit and 64 bit libraries in lib_va hash.
2810616c1c3SMichael Corcoran  */
2820616c1c3SMichael Corcoran static uint_t libs_mapped_32 = 0;
2830616c1c3SMichael Corcoran static uint_t libs_mapped_64 = 0;
2840616c1c3SMichael Corcoran 
2850616c1c3SMichael Corcoran /*
2860616c1c3SMichael Corcoran  * Free up the resources associated with lvp as well as lvp itself.
2870616c1c3SMichael Corcoran  * We also decrement the number of libraries mapped via a lib_va
2880616c1c3SMichael Corcoran  * cached virtual address.
2890616c1c3SMichael Corcoran  */
2900616c1c3SMichael Corcoran void
lib_va_free(struct lib_va * lvp)2910616c1c3SMichael Corcoran lib_va_free(struct lib_va *lvp)
2920616c1c3SMichael Corcoran {
2930616c1c3SMichael Corcoran 	int is_64bit = lvp->lv_flags & LV_ELF64;
2940616c1c3SMichael Corcoran 	ASSERT(lvp->lv_refcnt == 0);
2950616c1c3SMichael Corcoran 
2960616c1c3SMichael Corcoran 	if (lvp->lv_base_va != NULL) {
2970616c1c3SMichael Corcoran 		vmem_xfree(is_64bit ? lib_va_64_arena : lib_va_32_arena,
2980616c1c3SMichael Corcoran 		    lvp->lv_base_va, lvp->lv_len);
2990616c1c3SMichael Corcoran 		if (is_64bit) {
3001a5e258fSJosef 'Jeff' Sipek 			atomic_dec_32(&libs_mapped_64);
3010616c1c3SMichael Corcoran 		} else {
3021a5e258fSJosef 'Jeff' Sipek 			atomic_dec_32(&libs_mapped_32);
3030616c1c3SMichael Corcoran 		}
3040616c1c3SMichael Corcoran 	}
3050616c1c3SMichael Corcoran 	kmem_free(lvp, sizeof (struct lib_va));
3060616c1c3SMichael Corcoran }
3070616c1c3SMichael Corcoran 
3080616c1c3SMichael Corcoran /*
3090616c1c3SMichael Corcoran  * See if the file associated with the vap passed in is in the lib_va hash.
3100616c1c3SMichael Corcoran  * If it is and the file has not been modified since last use, then
3110616c1c3SMichael Corcoran  * return a pointer to that data.  Otherwise, return NULL if the file has
3120616c1c3SMichael Corcoran  * changed or the file was not found in the hash.
3130616c1c3SMichael Corcoran  */
3140616c1c3SMichael Corcoran static struct lib_va *
lib_va_find(vattr_t * vap)3150616c1c3SMichael Corcoran lib_va_find(vattr_t *vap)
3160616c1c3SMichael Corcoran {
3170616c1c3SMichael Corcoran 	struct lib_va *lvp;
3180616c1c3SMichael Corcoran 	struct lib_va *del = NULL;
3190616c1c3SMichael Corcoran 	struct lib_va **tmp;
3200616c1c3SMichael Corcoran 	uint_t index;
3210616c1c3SMichael Corcoran 	index = LIB_VA_HASH(vap->va_nodeid);
3220616c1c3SMichael Corcoran 
3230616c1c3SMichael Corcoran 	mutex_enter(LIB_VA_HASH_MUTEX(index));
3240616c1c3SMichael Corcoran 	tmp = &lib_va_hash[index];
3250616c1c3SMichael Corcoran 	while (*tmp != NULL) {
3260616c1c3SMichael Corcoran 		lvp = *tmp;
3270616c1c3SMichael Corcoran 		if (LIB_VA_MATCH_ID(lvp, vap)) {
3280616c1c3SMichael Corcoran 			if (LIB_VA_MATCH_TIME(lvp, vap)) {
3290616c1c3SMichael Corcoran 				ASSERT((lvp->lv_flags & LV_DEL) == 0);
3300616c1c3SMichael Corcoran 				lvp->lv_refcnt++;
3310616c1c3SMichael Corcoran 				MOBJ_STAT_ADD(lib_va_find_hit);
3320616c1c3SMichael Corcoran 			} else {
3330616c1c3SMichael Corcoran 				/*
3340616c1c3SMichael Corcoran 				 * file was updated since last use.
3350616c1c3SMichael Corcoran 				 * need to remove it from list.
3360616c1c3SMichael Corcoran 				 */
3370616c1c3SMichael Corcoran 				del = lvp;
3380616c1c3SMichael Corcoran 				*tmp = del->lv_next;
3390616c1c3SMichael Corcoran 				del->lv_next = NULL;
3400616c1c3SMichael Corcoran 				/*
3410616c1c3SMichael Corcoran 				 * If we can't delete it now, mark it for later
3420616c1c3SMichael Corcoran 				 */
3430616c1c3SMichael Corcoran 				if (del->lv_refcnt) {
3440616c1c3SMichael Corcoran 					MOBJ_STAT_ADD(lib_va_find_delay_delete);
3450616c1c3SMichael Corcoran 					del->lv_flags |= LV_DEL;
3460616c1c3SMichael Corcoran 					del = NULL;
3470616c1c3SMichael Corcoran 				}
3480616c1c3SMichael Corcoran 				lvp = NULL;
3490616c1c3SMichael Corcoran 			}
3500616c1c3SMichael Corcoran 			mutex_exit(LIB_VA_HASH_MUTEX(index));
3510616c1c3SMichael Corcoran 			if (del) {
3520616c1c3SMichael Corcoran 				ASSERT(del->lv_refcnt == 0);
3530616c1c3SMichael Corcoran 				MOBJ_STAT_ADD(lib_va_find_delete);
3540616c1c3SMichael Corcoran 				lib_va_free(del);
3550616c1c3SMichael Corcoran 			}
3560616c1c3SMichael Corcoran 			return (lvp);
3570616c1c3SMichael Corcoran 		}
3580616c1c3SMichael Corcoran 		tmp = &lvp->lv_next;
3590616c1c3SMichael Corcoran 	}
3600616c1c3SMichael Corcoran 	mutex_exit(LIB_VA_HASH_MUTEX(index));
3610616c1c3SMichael Corcoran 	return (NULL);
3620616c1c3SMichael Corcoran }
3630616c1c3SMichael Corcoran 
3640616c1c3SMichael Corcoran /*
3650616c1c3SMichael Corcoran  * Add a new entry to the lib_va hash.
3660616c1c3SMichael Corcoran  * Search the hash while holding the appropriate mutex to make sure that the
3670616c1c3SMichael Corcoran  * data is not already in the cache.  If we find data that is in the cache
3680616c1c3SMichael Corcoran  * already and has not been modified since last use, we return NULL.  If it
3690616c1c3SMichael Corcoran  * has been modified since last use, we will remove that entry from
3700616c1c3SMichael Corcoran  * the hash and it will be deleted once it's reference count reaches zero.
3710616c1c3SMichael Corcoran  * If there is no current entry in the hash we will add the new entry and
3720616c1c3SMichael Corcoran  * return it to the caller who is responsible for calling lib_va_release to
3730616c1c3SMichael Corcoran  * drop their reference count on it.
3740616c1c3SMichael Corcoran  *
3750616c1c3SMichael Corcoran  * lv_num_segs will be set to zero since the caller needs to add that
3760616c1c3SMichael Corcoran  * information to the data structure.
3770616c1c3SMichael Corcoran  */
3780616c1c3SMichael Corcoran static struct lib_va *
lib_va_add_hash(caddr_t base_va,ssize_t len,size_t align,vattr_t * vap)3790616c1c3SMichael Corcoran lib_va_add_hash(caddr_t base_va, ssize_t len, size_t align, vattr_t *vap)
3800616c1c3SMichael Corcoran {
3810616c1c3SMichael Corcoran 	struct lib_va *lvp;
3820616c1c3SMichael Corcoran 	uint_t index;
3830616c1c3SMichael Corcoran 	model_t model;
3840616c1c3SMichael Corcoran 	struct lib_va **tmp;
3850616c1c3SMichael Corcoran 	struct lib_va *del = NULL;
3860616c1c3SMichael Corcoran 
3870616c1c3SMichael Corcoran 	model = get_udatamodel();
3880616c1c3SMichael Corcoran 	index = LIB_VA_HASH(vap->va_nodeid);
3890616c1c3SMichael Corcoran 
3900616c1c3SMichael Corcoran 	lvp = kmem_alloc(sizeof (struct lib_va), KM_SLEEP);
3910616c1c3SMichael Corcoran 
3920616c1c3SMichael Corcoran 	mutex_enter(LIB_VA_HASH_MUTEX(index));
3930616c1c3SMichael Corcoran 
3940616c1c3SMichael Corcoran 	/*
3950616c1c3SMichael Corcoran 	 * Make sure not adding same data a second time.
3960616c1c3SMichael Corcoran 	 * The hash chains should be relatively short and adding
3970616c1c3SMichael Corcoran 	 * is a relatively rare event, so it's worth the check.
3980616c1c3SMichael Corcoran 	 */
3990616c1c3SMichael Corcoran 	tmp = &lib_va_hash[index];
4000616c1c3SMichael Corcoran 	while (*tmp != NULL) {
4010616c1c3SMichael Corcoran 		if (LIB_VA_MATCH_ID(*tmp, vap)) {
4020616c1c3SMichael Corcoran 			if (LIB_VA_MATCH_TIME(*tmp, vap)) {
4030616c1c3SMichael Corcoran 				mutex_exit(LIB_VA_HASH_MUTEX(index));
4040616c1c3SMichael Corcoran 				kmem_free(lvp, sizeof (struct lib_va));
4050616c1c3SMichael Corcoran 				return (NULL);
4060616c1c3SMichael Corcoran 			}
4070616c1c3SMichael Corcoran 
4080616c1c3SMichael Corcoran 			/*
4090616c1c3SMichael Corcoran 			 * We have the same nodeid and fsid but the file has
4100616c1c3SMichael Corcoran 			 * been modified since we last saw it.
4110616c1c3SMichael Corcoran 			 * Need to remove the old node and add this new
4120616c1c3SMichael Corcoran 			 * one.
4130616c1c3SMichael Corcoran 			 * Could probably use a callback mechanism to make
4140616c1c3SMichael Corcoran 			 * this cleaner.
4150616c1c3SMichael Corcoran 			 */
4160616c1c3SMichael Corcoran 			ASSERT(del == NULL);
4170616c1c3SMichael Corcoran 			del = *tmp;
4180616c1c3SMichael Corcoran 			*tmp = del->lv_next;
4190616c1c3SMichael Corcoran 			del->lv_next = NULL;
4200616c1c3SMichael Corcoran 
4210616c1c3SMichael Corcoran 			/*
4220616c1c3SMichael Corcoran 			 * Check to see if we can free it.  If lv_refcnt
4230616c1c3SMichael Corcoran 			 * is greater than zero, than some other thread
4240616c1c3SMichael Corcoran 			 * has a reference to the one we want to delete
4250616c1c3SMichael Corcoran 			 * and we can not delete it.  All of this is done
4260616c1c3SMichael Corcoran 			 * under the lib_va_hash_mutex lock so it is atomic.
4270616c1c3SMichael Corcoran 			 */
4280616c1c3SMichael Corcoran 			if (del->lv_refcnt) {
4290616c1c3SMichael Corcoran 				MOBJ_STAT_ADD(lib_va_add_delay_delete);
4300616c1c3SMichael Corcoran 				del->lv_flags |= LV_DEL;
4310616c1c3SMichael Corcoran 				del = NULL;
4320616c1c3SMichael Corcoran 			}
4330616c1c3SMichael Corcoran 			/* tmp is already advanced */
4340616c1c3SMichael Corcoran 			continue;
4350616c1c3SMichael Corcoran 		}
4360616c1c3SMichael Corcoran 		tmp = &((*tmp)->lv_next);
4370616c1c3SMichael Corcoran 	}
4380616c1c3SMichael Corcoran 
4390616c1c3SMichael Corcoran 	lvp->lv_base_va = base_va;
4400616c1c3SMichael Corcoran 	lvp->lv_len = len;
4410616c1c3SMichael Corcoran 	lvp->lv_align = align;
4420616c1c3SMichael Corcoran 	lvp->lv_nodeid = vap->va_nodeid;
4430616c1c3SMichael Corcoran 	lvp->lv_fsid = vap->va_fsid;
4440616c1c3SMichael Corcoran 	lvp->lv_ctime.tv_sec = vap->va_ctime.tv_sec;
4450616c1c3SMichael Corcoran 	lvp->lv_ctime.tv_nsec = vap->va_ctime.tv_nsec;
4460616c1c3SMichael Corcoran 	lvp->lv_mtime.tv_sec = vap->va_mtime.tv_sec;
4470616c1c3SMichael Corcoran 	lvp->lv_mtime.tv_nsec = vap->va_mtime.tv_nsec;
4480616c1c3SMichael Corcoran 	lvp->lv_next = NULL;
4490616c1c3SMichael Corcoran 	lvp->lv_refcnt = 1;
4500616c1c3SMichael Corcoran 
4510616c1c3SMichael Corcoran 	/* Caller responsible for filling this and lv_mps out */
4520616c1c3SMichael Corcoran 	lvp->lv_num_segs = 0;
4530616c1c3SMichael Corcoran 
4540616c1c3SMichael Corcoran 	if (model == DATAMODEL_LP64) {
4550616c1c3SMichael Corcoran 		lvp->lv_flags = LV_ELF64;
4560616c1c3SMichael Corcoran 	} else {
4570616c1c3SMichael Corcoran 		ASSERT(model == DATAMODEL_ILP32);
4580616c1c3SMichael Corcoran 		lvp->lv_flags = LV_ELF32;
4590616c1c3SMichael Corcoran 	}
4600616c1c3SMichael Corcoran 
4610616c1c3SMichael Corcoran 	if (base_va != NULL) {
4620616c1c3SMichael Corcoran 		if (model == DATAMODEL_LP64) {
4631a5e258fSJosef 'Jeff' Sipek 			atomic_inc_32(&libs_mapped_64);
4640616c1c3SMichael Corcoran 		} else {
4650616c1c3SMichael Corcoran 			ASSERT(model == DATAMODEL_ILP32);
4661a5e258fSJosef 'Jeff' Sipek 			atomic_inc_32(&libs_mapped_32);
4670616c1c3SMichael Corcoran 		}
4680616c1c3SMichael Corcoran 	}
4690616c1c3SMichael Corcoran 	ASSERT(*tmp == NULL);
4700616c1c3SMichael Corcoran 	*tmp = lvp;
4710616c1c3SMichael Corcoran 	mutex_exit(LIB_VA_HASH_MUTEX(index));
4720616c1c3SMichael Corcoran 	if (del) {
4730616c1c3SMichael Corcoran 		ASSERT(del->lv_refcnt == 0);
4740616c1c3SMichael Corcoran 		MOBJ_STAT_ADD(lib_va_add_delete);
4750616c1c3SMichael Corcoran 		lib_va_free(del);
4760616c1c3SMichael Corcoran 	}
4770616c1c3SMichael Corcoran 	return (lvp);
4780616c1c3SMichael Corcoran }
4790616c1c3SMichael Corcoran 
4800616c1c3SMichael Corcoran /*
4810616c1c3SMichael Corcoran  * Release the hold on lvp which was acquired by lib_va_find or lib_va_add_hash.
4820616c1c3SMichael Corcoran  * In addition, if this is the last hold and lvp is marked for deletion,
4830616c1c3SMichael Corcoran  * free up it's reserved address space and free the structure.
4840616c1c3SMichael Corcoran  */
4850616c1c3SMichael Corcoran static void
lib_va_release(struct lib_va * lvp)4860616c1c3SMichael Corcoran lib_va_release(struct lib_va *lvp)
4870616c1c3SMichael Corcoran {
4880616c1c3SMichael Corcoran 	uint_t index;
4890616c1c3SMichael Corcoran 	int to_del = 0;
4900616c1c3SMichael Corcoran 
4910616c1c3SMichael Corcoran 	ASSERT(lvp->lv_refcnt > 0);
4920616c1c3SMichael Corcoran 
4930616c1c3SMichael Corcoran 	index = LIB_VA_HASH(lvp->lv_nodeid);
4940616c1c3SMichael Corcoran 	mutex_enter(LIB_VA_HASH_MUTEX(index));
4950616c1c3SMichael Corcoran 	if (--lvp->lv_refcnt == 0 && (lvp->lv_flags & LV_DEL)) {
4960616c1c3SMichael Corcoran 		to_del = 1;
4970616c1c3SMichael Corcoran 	}
4980616c1c3SMichael Corcoran 	mutex_exit(LIB_VA_HASH_MUTEX(index));
4990616c1c3SMichael Corcoran 	if (to_del) {
5000616c1c3SMichael Corcoran 		ASSERT(lvp->lv_next == 0);
5010616c1c3SMichael Corcoran 		lib_va_free(lvp);
5020616c1c3SMichael Corcoran 	}
5030616c1c3SMichael Corcoran }
5040616c1c3SMichael Corcoran 
5050616c1c3SMichael Corcoran /*
5060616c1c3SMichael Corcoran  * Dummy function for mapping through /dev/null
5070616c1c3SMichael Corcoran  * Normally I would have used mmmmap in common/io/mem.c
5080616c1c3SMichael Corcoran  * but that is a static function, and for /dev/null, it
5090616c1c3SMichael Corcoran  * just returns -1.
5100616c1c3SMichael Corcoran  */
5110616c1c3SMichael Corcoran /* ARGSUSED */
5120616c1c3SMichael Corcoran static int
mmapobj_dummy(dev_t dev,off_t off,int prot)5130616c1c3SMichael Corcoran mmapobj_dummy(dev_t dev, off_t off, int prot)
5140616c1c3SMichael Corcoran {
5150616c1c3SMichael Corcoran 	return (-1);
5160616c1c3SMichael Corcoran }
5170616c1c3SMichael Corcoran 
5180616c1c3SMichael Corcoran /*
5190616c1c3SMichael Corcoran  * Called when an error occurred which requires mmapobj to return failure.
5200616c1c3SMichael Corcoran  * All mapped objects will be unmapped and /dev/null mappings will be
5210616c1c3SMichael Corcoran  * reclaimed if necessary.
5220616c1c3SMichael Corcoran  * num_mapped is the number of elements of mrp which have been mapped, and
5230616c1c3SMichael Corcoran  * num_segs is the total number of elements in mrp.
5240616c1c3SMichael Corcoran  * For e_type ET_EXEC, we need to unmap all of the elements in mrp since
5250616c1c3SMichael Corcoran  * we had already made reservations for them.
5260616c1c3SMichael Corcoran  * If num_mapped equals num_segs, then we know that we had fully mapped
5270616c1c3SMichael Corcoran  * the file and only need to clean up the segments described.
5280616c1c3SMichael Corcoran  * If they are not equal, then for ET_DYN we will unmap the range from the
5290616c1c3SMichael Corcoran  * end of the last mapped segment to the end of the last segment in mrp
5300616c1c3SMichael Corcoran  * since we would have made a reservation for that memory earlier.
5310616c1c3SMichael Corcoran  * If e_type is passed in as zero, num_mapped must equal num_segs.
5320616c1c3SMichael Corcoran  */
5330616c1c3SMichael Corcoran void
mmapobj_unmap(mmapobj_result_t * mrp,int num_mapped,int num_segs,ushort_t e_type)5340616c1c3SMichael Corcoran mmapobj_unmap(mmapobj_result_t *mrp, int num_mapped, int num_segs,
5350616c1c3SMichael Corcoran     ushort_t e_type)
5360616c1c3SMichael Corcoran {
5370616c1c3SMichael Corcoran 	int i;
5380616c1c3SMichael Corcoran 	struct as *as = curproc->p_as;
5390616c1c3SMichael Corcoran 	caddr_t addr;
5400616c1c3SMichael Corcoran 	size_t size;
5410616c1c3SMichael Corcoran 
5420616c1c3SMichael Corcoran 	if (e_type == ET_EXEC) {
5430616c1c3SMichael Corcoran 		num_mapped = num_segs;
5440616c1c3SMichael Corcoran 	}
5450616c1c3SMichael Corcoran #ifdef DEBUG
5460616c1c3SMichael Corcoran 	if (e_type == 0) {
5470616c1c3SMichael Corcoran 		ASSERT(num_mapped == num_segs);
5480616c1c3SMichael Corcoran 	}
5490616c1c3SMichael Corcoran #endif
5500616c1c3SMichael Corcoran 
5510616c1c3SMichael Corcoran 	MOBJ_STAT_ADD(unmap_called);
5520616c1c3SMichael Corcoran 	for (i = 0; i < num_mapped; i++) {
5530616c1c3SMichael Corcoran 
5540616c1c3SMichael Corcoran 		/*
5550616c1c3SMichael Corcoran 		 * If we are going to have to create a mapping we need to
5560616c1c3SMichael Corcoran 		 * make sure that no one else will use the address we
5570616c1c3SMichael Corcoran 		 * need to remap between the time it is unmapped and
5580616c1c3SMichael Corcoran 		 * mapped below.
5590616c1c3SMichael Corcoran 		 */
5600616c1c3SMichael Corcoran 		if (mrp[i].mr_flags & MR_RESV) {
5610616c1c3SMichael Corcoran 			as_rangelock(as);
5620616c1c3SMichael Corcoran 		}
5630616c1c3SMichael Corcoran 		/* Always need to unmap what we mapped */
5640616c1c3SMichael Corcoran 		(void) as_unmap(as, mrp[i].mr_addr, mrp[i].mr_msize);
5650616c1c3SMichael Corcoran 
5660616c1c3SMichael Corcoran 		/* Need to reclaim /dev/null reservation from earlier */
5670616c1c3SMichael Corcoran 		if (mrp[i].mr_flags & MR_RESV) {
5680616c1c3SMichael Corcoran 			struct segdev_crargs dev_a;
5690616c1c3SMichael Corcoran 
5700616c1c3SMichael Corcoran 			ASSERT(e_type != ET_DYN);
5710616c1c3SMichael Corcoran 			/*
5720616c1c3SMichael Corcoran 			 * Use seg_dev segment driver for /dev/null mapping.
5730616c1c3SMichael Corcoran 			 */
5740616c1c3SMichael Corcoran 			dev_a.mapfunc = mmapobj_dummy;
5750616c1c3SMichael Corcoran 			dev_a.dev = makedevice(mm_major, M_NULL);
5760616c1c3SMichael Corcoran 			dev_a.offset = 0;
5770616c1c3SMichael Corcoran 			dev_a.type = 0;		/* neither PRIVATE nor SHARED */
5780616c1c3SMichael Corcoran 			dev_a.prot = dev_a.maxprot = (uchar_t)PROT_NONE;
5790616c1c3SMichael Corcoran 			dev_a.hat_attr = 0;
5800616c1c3SMichael Corcoran 			dev_a.hat_flags = 0;
5810616c1c3SMichael Corcoran 
5820616c1c3SMichael Corcoran 			(void) as_map(as, mrp[i].mr_addr, mrp[i].mr_msize,
5830616c1c3SMichael Corcoran 			    segdev_create, &dev_a);
5840616c1c3SMichael Corcoran 			MOBJ_STAT_ADD(remap_devnull);
5850616c1c3SMichael Corcoran 			as_rangeunlock(as);
5860616c1c3SMichael Corcoran 		}
5870616c1c3SMichael Corcoran 	}
5880616c1c3SMichael Corcoran 
5890616c1c3SMichael Corcoran 	if (num_mapped != num_segs) {
5900616c1c3SMichael Corcoran 		ASSERT(e_type == ET_DYN);
5910616c1c3SMichael Corcoran 		/* Need to unmap any reservation made after last mapped seg */
5920616c1c3SMichael Corcoran 		if (num_mapped == 0) {
5930616c1c3SMichael Corcoran 			addr = mrp[0].mr_addr;
5940616c1c3SMichael Corcoran 		} else {
5950616c1c3SMichael Corcoran 			addr = mrp[num_mapped - 1].mr_addr +
5960616c1c3SMichael Corcoran 			    mrp[num_mapped - 1].mr_msize;
5970616c1c3SMichael Corcoran 		}
5980616c1c3SMichael Corcoran 		size = (size_t)mrp[num_segs - 1].mr_addr +
5990616c1c3SMichael Corcoran 		    mrp[num_segs - 1].mr_msize - (size_t)addr;
6000616c1c3SMichael Corcoran 		(void) as_unmap(as, addr, size);
6010616c1c3SMichael Corcoran 
6020616c1c3SMichael Corcoran 		/*
6030616c1c3SMichael Corcoran 		 * Now we need to unmap the holes between mapped segs.
6040616c1c3SMichael Corcoran 		 * Note that we have not mapped all of the segments and thus
6050616c1c3SMichael Corcoran 		 * the holes between segments would not have been unmapped
6060616c1c3SMichael Corcoran 		 * yet.  If num_mapped == num_segs, then all of the holes
6070616c1c3SMichael Corcoran 		 * between segments would have already been unmapped.
6080616c1c3SMichael Corcoran 		 */
6090616c1c3SMichael Corcoran 
6100616c1c3SMichael Corcoran 		for (i = 1; i < num_mapped; i++) {
6110616c1c3SMichael Corcoran 			addr = mrp[i - 1].mr_addr + mrp[i - 1].mr_msize;
6120616c1c3SMichael Corcoran 			size = mrp[i].mr_addr - addr;
6130616c1c3SMichael Corcoran 			(void) as_unmap(as, addr, size);
6140616c1c3SMichael Corcoran 		}
6150616c1c3SMichael Corcoran 	}
6160616c1c3SMichael Corcoran }
6170616c1c3SMichael Corcoran 
6180616c1c3SMichael Corcoran /*
6190616c1c3SMichael Corcoran  * We need to add the start address into mrp so that the unmap function
6200616c1c3SMichael Corcoran  * has absolute addresses to use.
6210616c1c3SMichael Corcoran  */
6220616c1c3SMichael Corcoran static void
mmapobj_unmap_exec(mmapobj_result_t * mrp,int num_mapped,caddr_t start_addr)6230616c1c3SMichael Corcoran mmapobj_unmap_exec(mmapobj_result_t *mrp, int num_mapped, caddr_t start_addr)
6240616c1c3SMichael Corcoran {
6250616c1c3SMichael Corcoran 	int i;
6260616c1c3SMichael Corcoran 
6270616c1c3SMichael Corcoran 	for (i = 0; i < num_mapped; i++) {
6280616c1c3SMichael Corcoran 		mrp[i].mr_addr += (size_t)start_addr;
6290616c1c3SMichael Corcoran 	}
6300616c1c3SMichael Corcoran 	mmapobj_unmap(mrp, num_mapped, num_mapped, ET_EXEC);
6310616c1c3SMichael Corcoran }
6320616c1c3SMichael Corcoran 
6330616c1c3SMichael Corcoran static caddr_t
mmapobj_lookup_start_addr(struct lib_va * lvp)6340616c1c3SMichael Corcoran mmapobj_lookup_start_addr(struct lib_va *lvp)
6350616c1c3SMichael Corcoran {
6361e1e1eecSMichael Corcoran 	proc_t *p = curproc;
6371e1e1eecSMichael Corcoran 	struct as *as = p->p_as;
6380616c1c3SMichael Corcoran 	struct segvn_crargs crargs = SEGVN_ZFOD_ARGS(PROT_USER, PROT_ALL);
6390616c1c3SMichael Corcoran 	int error;
6400616c1c3SMichael Corcoran 	uint_t ma_flags = _MAP_LOW32;
6410616c1c3SMichael Corcoran 	caddr_t base = NULL;
6420616c1c3SMichael Corcoran 	size_t len;
6430616c1c3SMichael Corcoran 	size_t align;
6440616c1c3SMichael Corcoran 
6450616c1c3SMichael Corcoran 	ASSERT(lvp != NULL);
6460616c1c3SMichael Corcoran 	MOBJ_STAT_ADD(lookup_start);
6470616c1c3SMichael Corcoran 
6480616c1c3SMichael Corcoran 	as_rangelock(as);
6490616c1c3SMichael Corcoran 
6500616c1c3SMichael Corcoran 	base = lvp->lv_base_va;
6510616c1c3SMichael Corcoran 	len = lvp->lv_len;
6520616c1c3SMichael Corcoran 
6530616c1c3SMichael Corcoran 	/*
6540616c1c3SMichael Corcoran 	 * If we don't have an expected base address, or the one that we want
655980cf3cfSMichael Corcoran 	 * to use is not available or acceptable, go get an acceptable
656980cf3cfSMichael Corcoran 	 * address range.
6570616c1c3SMichael Corcoran 	 */
658980cf3cfSMichael Corcoran 	if (base == NULL || as_gap(as, len, &base, &len, 0, NULL) ||
659980cf3cfSMichael Corcoran 	    valid_usr_range(base, len, PROT_ALL, as, as->a_userlimit) !=
6601e1e1eecSMichael Corcoran 	    RANGE_OKAY || OVERLAPS_STACK(base + len, p)) {
6610616c1c3SMichael Corcoran 		if (lvp->lv_flags & LV_ELF64) {
6620616c1c3SMichael Corcoran 			ma_flags = 0;
6630616c1c3SMichael Corcoran 		}
6640616c1c3SMichael Corcoran 
6650616c1c3SMichael Corcoran 		align = lvp->lv_align;
6660616c1c3SMichael Corcoran 		if (align > 1) {
6670616c1c3SMichael Corcoran 			ma_flags |= MAP_ALIGN;
6680616c1c3SMichael Corcoran 		}
6690616c1c3SMichael Corcoran 
6700616c1c3SMichael Corcoran 		base = (caddr_t)align;
6710616c1c3SMichael Corcoran 		map_addr(&base, len, 0, 1, ma_flags);
6720616c1c3SMichael Corcoran 	}
6730616c1c3SMichael Corcoran 
6740616c1c3SMichael Corcoran 	/*
6750616c1c3SMichael Corcoran 	 * Need to reserve the address space we're going to use.
6760616c1c3SMichael Corcoran 	 * Don't reserve swap space since we'll be mapping over this.
6770616c1c3SMichael Corcoran 	 */
6780616c1c3SMichael Corcoran 	if (base != NULL) {
6790616c1c3SMichael Corcoran 		crargs.flags |= MAP_NORESERVE;
6800616c1c3SMichael Corcoran 		error = as_map(as, base, len, segvn_create, &crargs);
6810616c1c3SMichael Corcoran 		if (error) {
6820616c1c3SMichael Corcoran 			base = NULL;
6830616c1c3SMichael Corcoran 		}
6840616c1c3SMichael Corcoran 	}
6850616c1c3SMichael Corcoran 
6860616c1c3SMichael Corcoran 	as_rangeunlock(as);
6870616c1c3SMichael Corcoran 	return (base);
6880616c1c3SMichael Corcoran }
6890616c1c3SMichael Corcoran 
6900616c1c3SMichael Corcoran /*
6910616c1c3SMichael Corcoran  * Get the starting address for a given file to be mapped and return it
6920616c1c3SMichael Corcoran  * to the caller.  If we're using lib_va and we need to allocate an address,
6930616c1c3SMichael Corcoran  * we will attempt to allocate it from the global reserved pool such that the
6940616c1c3SMichael Corcoran  * same address can be used in the future for this file.  If we can't use the
6950616c1c3SMichael Corcoran  * reserved address then we just get one that will fit in our address space.
6960616c1c3SMichael Corcoran  *
6970616c1c3SMichael Corcoran  * Returns the starting virtual address for the range to be mapped or NULL
6980616c1c3SMichael Corcoran  * if an error is encountered. If we successfully insert the requested info
6990616c1c3SMichael Corcoran  * into the lib_va hash, then *lvpp will be set to point to this lib_va
7000616c1c3SMichael Corcoran  * structure.  The structure will have a hold on it and thus lib_va_release
7010616c1c3SMichael Corcoran  * needs to be called on it by the caller.  This function will not fill out
7020616c1c3SMichael Corcoran  * lv_mps or lv_num_segs since it does not have enough information to do so.
7030616c1c3SMichael Corcoran  * The caller is responsible for doing this making sure that any modifications
7040616c1c3SMichael Corcoran  * to lv_mps are visible before setting lv_num_segs.
7050616c1c3SMichael Corcoran  */
7060616c1c3SMichael Corcoran static caddr_t
mmapobj_alloc_start_addr(struct lib_va ** lvpp,size_t len,int use_lib_va,int randomize,size_t align,vattr_t * vap)7070616c1c3SMichael Corcoran mmapobj_alloc_start_addr(struct lib_va **lvpp, size_t len, int use_lib_va,
708d2a70789SRichard Lowe     int randomize, size_t align, vattr_t *vap)
7090616c1c3SMichael Corcoran {
7101e1e1eecSMichael Corcoran 	proc_t *p = curproc;
7111e1e1eecSMichael Corcoran 	struct as *as = p->p_as;
7120616c1c3SMichael Corcoran 	struct segvn_crargs crargs = SEGVN_ZFOD_ARGS(PROT_USER, PROT_ALL);
7130616c1c3SMichael Corcoran 	int error;
7140616c1c3SMichael Corcoran 	model_t model;
7150616c1c3SMichael Corcoran 	uint_t ma_flags = _MAP_LOW32;
7160616c1c3SMichael Corcoran 	caddr_t base = NULL;
7170616c1c3SMichael Corcoran 	vmem_t *model_vmem;
71879b24695SMichael Corcoran 	size_t lib_va_start;
71979b24695SMichael Corcoran 	size_t lib_va_end;
72079b24695SMichael Corcoran 	size_t lib_va_len;
7210616c1c3SMichael Corcoran 
7220616c1c3SMichael Corcoran 	ASSERT(lvpp != NULL);
723d2a70789SRichard Lowe 	ASSERT((randomize & use_lib_va) != 1);
7240616c1c3SMichael Corcoran 
7250616c1c3SMichael Corcoran 	MOBJ_STAT_ADD(alloc_start);
7260616c1c3SMichael Corcoran 	model = get_udatamodel();
7270616c1c3SMichael Corcoran 
7280616c1c3SMichael Corcoran 	if (model == DATAMODEL_LP64) {
7290616c1c3SMichael Corcoran 		ma_flags = 0;
7300616c1c3SMichael Corcoran 		model_vmem = lib_va_64_arena;
7310616c1c3SMichael Corcoran 	} else {
7320616c1c3SMichael Corcoran 		ASSERT(model == DATAMODEL_ILP32);
7330616c1c3SMichael Corcoran 		model_vmem = lib_va_32_arena;
7340616c1c3SMichael Corcoran 	}
7350616c1c3SMichael Corcoran 
7360616c1c3SMichael Corcoran 	if (align > 1) {
7370616c1c3SMichael Corcoran 		ma_flags |= MAP_ALIGN;
7380616c1c3SMichael Corcoran 	}
739d2a70789SRichard Lowe 
740d2a70789SRichard Lowe 	if (randomize != 0)
741d2a70789SRichard Lowe 		ma_flags |= _MAP_RANDOMIZE;
742d2a70789SRichard Lowe 
7430616c1c3SMichael Corcoran 	if (use_lib_va) {
74479b24695SMichael Corcoran 		/*
74579b24695SMichael Corcoran 		 * The first time through, we need to setup the lib_va arenas.
74679b24695SMichael Corcoran 		 * We call map_addr to find a suitable range of memory to map
74779b24695SMichael Corcoran 		 * the given library, and we will set the highest address
74879b24695SMichael Corcoran 		 * in our vmem arena to the end of this adddress range.
74979b24695SMichael Corcoran 		 * We allow up to half of the address space to be used
75079b24695SMichael Corcoran 		 * for lib_va addresses but we do not prevent any allocations
75179b24695SMichael Corcoran 		 * in this range from other allocation paths.
75279b24695SMichael Corcoran 		 */
75379b24695SMichael Corcoran 		if (lib_va_64_arena == NULL && model == DATAMODEL_LP64) {
75479b24695SMichael Corcoran 			mutex_enter(&lib_va_init_mutex);
75579b24695SMichael Corcoran 			if (lib_va_64_arena == NULL) {
75679b24695SMichael Corcoran 				base = (caddr_t)align;
75779b24695SMichael Corcoran 				as_rangelock(as);
75879b24695SMichael Corcoran 				map_addr(&base, len, 0, 1, ma_flags);
75979b24695SMichael Corcoran 				as_rangeunlock(as);
76079b24695SMichael Corcoran 				if (base == NULL) {
76179b24695SMichael Corcoran 					mutex_exit(&lib_va_init_mutex);
76279b24695SMichael Corcoran 					MOBJ_STAT_ADD(lib_va_create_failure);
76379b24695SMichael Corcoran 					goto nolibva;
76479b24695SMichael Corcoran 				}
76579b24695SMichael Corcoran 				lib_va_end = (size_t)base + len;
76679b24695SMichael Corcoran 				lib_va_len = lib_va_end >> 1;
76779b24695SMichael Corcoran 				lib_va_len = P2ROUNDUP(lib_va_len, PAGESIZE);
76879b24695SMichael Corcoran 				lib_va_start = lib_va_end - lib_va_len;
76979b24695SMichael Corcoran 
77079b24695SMichael Corcoran 				/*
7711e1e1eecSMichael Corcoran 				 * Need to make sure we avoid the address hole.
77279b24695SMichael Corcoran 				 * We know lib_va_end is valid but we need to
77379b24695SMichael Corcoran 				 * make sure lib_va_start is as well.
77479b24695SMichael Corcoran 				 */
77579b24695SMichael Corcoran 				if ((lib_va_end > (size_t)hole_end) &&
77679b24695SMichael Corcoran 				    (lib_va_start < (size_t)hole_end)) {
77779b24695SMichael Corcoran 					lib_va_start = P2ROUNDUP(
7781e1e1eecSMichael Corcoran 					    (size_t)hole_end, PAGESIZE);
77979b24695SMichael Corcoran 					lib_va_len = lib_va_end - lib_va_start;
78079b24695SMichael Corcoran 				}
78179b24695SMichael Corcoran 				lib_va_64_arena = vmem_create("lib_va_64",
78279b24695SMichael Corcoran 				    (void *)lib_va_start, lib_va_len, PAGESIZE,
78379b24695SMichael Corcoran 				    NULL, NULL, NULL, 0,
78479b24695SMichael Corcoran 				    VM_NOSLEEP | VMC_IDENTIFIER);
78579b24695SMichael Corcoran 				if (lib_va_64_arena == NULL) {
78679b24695SMichael Corcoran 					mutex_exit(&lib_va_init_mutex);
78779b24695SMichael Corcoran 					goto nolibva;
78879b24695SMichael Corcoran 				}
78979b24695SMichael Corcoran 			}
79079b24695SMichael Corcoran 			model_vmem = lib_va_64_arena;
79179b24695SMichael Corcoran 			mutex_exit(&lib_va_init_mutex);
79279b24695SMichael Corcoran 		} else if (lib_va_32_arena == NULL &&
79379b24695SMichael Corcoran 		    model == DATAMODEL_ILP32) {
79479b24695SMichael Corcoran 			mutex_enter(&lib_va_init_mutex);
79579b24695SMichael Corcoran 			if (lib_va_32_arena == NULL) {
79679b24695SMichael Corcoran 				base = (caddr_t)align;
79779b24695SMichael Corcoran 				as_rangelock(as);
79879b24695SMichael Corcoran 				map_addr(&base, len, 0, 1, ma_flags);
79979b24695SMichael Corcoran 				as_rangeunlock(as);
80079b24695SMichael Corcoran 				if (base == NULL) {
80179b24695SMichael Corcoran 					mutex_exit(&lib_va_init_mutex);
80279b24695SMichael Corcoran 					MOBJ_STAT_ADD(lib_va_create_failure);
80379b24695SMichael Corcoran 					goto nolibva;
80479b24695SMichael Corcoran 				}
80579b24695SMichael Corcoran 				lib_va_end = (size_t)base + len;
80679b24695SMichael Corcoran 				lib_va_len = lib_va_end >> 1;
80779b24695SMichael Corcoran 				lib_va_len = P2ROUNDUP(lib_va_len, PAGESIZE);
80879b24695SMichael Corcoran 				lib_va_start = lib_va_end - lib_va_len;
80979b24695SMichael Corcoran 				lib_va_32_arena = vmem_create("lib_va_32",
81079b24695SMichael Corcoran 				    (void *)lib_va_start, lib_va_len, PAGESIZE,
81179b24695SMichael Corcoran 				    NULL, NULL, NULL, 0,
81279b24695SMichael Corcoran 				    VM_NOSLEEP | VMC_IDENTIFIER);
81379b24695SMichael Corcoran 				if (lib_va_32_arena == NULL) {
81479b24695SMichael Corcoran 					mutex_exit(&lib_va_init_mutex);
81579b24695SMichael Corcoran 					goto nolibva;
81679b24695SMichael Corcoran 				}
81779b24695SMichael Corcoran 			}
81879b24695SMichael Corcoran 			model_vmem = lib_va_32_arena;
81979b24695SMichael Corcoran 			mutex_exit(&lib_va_init_mutex);
82079b24695SMichael Corcoran 		}
82179b24695SMichael Corcoran 
8220616c1c3SMichael Corcoran 		if (model == DATAMODEL_LP64 || libs_mapped_32 < lib_threshold) {
8230616c1c3SMichael Corcoran 			base = vmem_xalloc(model_vmem, len, align, 0, 0, NULL,
8240616c1c3SMichael Corcoran 			    NULL, VM_NOSLEEP | VM_ENDALLOC);
8250616c1c3SMichael Corcoran 			MOBJ_STAT_ADD(alloc_vmem);
8260616c1c3SMichael Corcoran 		}
82779b24695SMichael Corcoran 
8280616c1c3SMichael Corcoran 		/*
8290616c1c3SMichael Corcoran 		 * Even if the address fails to fit in our address space,
8300616c1c3SMichael Corcoran 		 * or we can't use a reserved address,
8310616c1c3SMichael Corcoran 		 * we should still save it off in lib_va_hash.
8320616c1c3SMichael Corcoran 		 */
8330616c1c3SMichael Corcoran 		*lvpp = lib_va_add_hash(base, len, align, vap);
8340616c1c3SMichael Corcoran 
8350616c1c3SMichael Corcoran 		/*
8360616c1c3SMichael Corcoran 		 * Check for collision on insertion and free up our VA space.
8370616c1c3SMichael Corcoran 		 * This is expected to be rare, so we'll just reset base to
8380616c1c3SMichael Corcoran 		 * NULL instead of looking it up in the lib_va hash.
8390616c1c3SMichael Corcoran 		 */
8400616c1c3SMichael Corcoran 		if (*lvpp == NULL) {
8410616c1c3SMichael Corcoran 			if (base != NULL) {
8420616c1c3SMichael Corcoran 				vmem_xfree(model_vmem, base, len);
8430616c1c3SMichael Corcoran 				base = NULL;
8440616c1c3SMichael Corcoran 				MOBJ_STAT_ADD(add_collision);
8450616c1c3SMichael Corcoran 			}
8460616c1c3SMichael Corcoran 		}
8470616c1c3SMichael Corcoran 	}
8480616c1c3SMichael Corcoran 
84979b24695SMichael Corcoran nolibva:
8500616c1c3SMichael Corcoran 	as_rangelock(as);
8510616c1c3SMichael Corcoran 
8520616c1c3SMichael Corcoran 	/*
8530616c1c3SMichael Corcoran 	 * If we don't have an expected base address, or the one that we want
854980cf3cfSMichael Corcoran 	 * to use is not available or acceptable, go get an acceptable
855980cf3cfSMichael Corcoran 	 * address range.
856d2a70789SRichard Lowe 	 *
857d2a70789SRichard Lowe 	 * If ASLR is enabled, we should never have used the cache, and should
858d2a70789SRichard Lowe 	 * also start our real work here, in the consequent of the next
859d2a70789SRichard Lowe 	 * condition.
8600616c1c3SMichael Corcoran 	 */
861d2a70789SRichard Lowe 	if (randomize != 0)
862d2a70789SRichard Lowe 		ASSERT(base == NULL);
863d2a70789SRichard Lowe 
864980cf3cfSMichael Corcoran 	if (base == NULL || as_gap(as, len, &base, &len, 0, NULL) ||
865980cf3cfSMichael Corcoran 	    valid_usr_range(base, len, PROT_ALL, as, as->a_userlimit) !=
8661e1e1eecSMichael Corcoran 	    RANGE_OKAY || OVERLAPS_STACK(base + len, p)) {
8670616c1c3SMichael Corcoran 		MOBJ_STAT_ADD(get_addr);
8680616c1c3SMichael Corcoran 		base = (caddr_t)align;
8690616c1c3SMichael Corcoran 		map_addr(&base, len, 0, 1, ma_flags);
8700616c1c3SMichael Corcoran 	}
8710616c1c3SMichael Corcoran 
8720616c1c3SMichael Corcoran 	/*
8730616c1c3SMichael Corcoran 	 * Need to reserve the address space we're going to use.
8740616c1c3SMichael Corcoran 	 * Don't reserve swap space since we'll be mapping over this.
8750616c1c3SMichael Corcoran 	 */
8760616c1c3SMichael Corcoran 	if (base != NULL) {
8770616c1c3SMichael Corcoran 		/* Don't reserve swap space since we'll be mapping over this */
8780616c1c3SMichael Corcoran 		crargs.flags |= MAP_NORESERVE;
8790616c1c3SMichael Corcoran 		error = as_map(as, base, len, segvn_create, &crargs);
8800616c1c3SMichael Corcoran 		if (error) {
8810616c1c3SMichael Corcoran 			base = NULL;
8820616c1c3SMichael Corcoran 		}
8830616c1c3SMichael Corcoran 	}
8840616c1c3SMichael Corcoran 
8850616c1c3SMichael Corcoran 	as_rangeunlock(as);
8860616c1c3SMichael Corcoran 	return (base);
8870616c1c3SMichael Corcoran }
8880616c1c3SMichael Corcoran 
8890616c1c3SMichael Corcoran /*
8900616c1c3SMichael Corcoran  * Map the file associated with vp into the address space as a single
8910616c1c3SMichael Corcoran  * read only private mapping.
8920616c1c3SMichael Corcoran  * Returns 0 for success, and non-zero for failure to map the file.
8930616c1c3SMichael Corcoran  */
8940616c1c3SMichael Corcoran static int
mmapobj_map_flat(vnode_t * vp,mmapobj_result_t * mrp,size_t padding,cred_t * fcred)8950616c1c3SMichael Corcoran mmapobj_map_flat(vnode_t *vp, mmapobj_result_t *mrp, size_t padding,
8960616c1c3SMichael Corcoran     cred_t *fcred)
8970616c1c3SMichael Corcoran {
8980616c1c3SMichael Corcoran 	int error = 0;
8990616c1c3SMichael Corcoran 	struct as *as = curproc->p_as;
9000616c1c3SMichael Corcoran 	caddr_t addr = NULL;
9010616c1c3SMichael Corcoran 	caddr_t start_addr;
9020616c1c3SMichael Corcoran 	size_t len;
9030616c1c3SMichael Corcoran 	size_t pad_len;
9040616c1c3SMichael Corcoran 	int prot = PROT_USER | PROT_READ;
9050616c1c3SMichael Corcoran 	uint_t ma_flags = _MAP_LOW32;
9060616c1c3SMichael Corcoran 	vattr_t vattr;
9070616c1c3SMichael Corcoran 	struct segvn_crargs crargs = SEGVN_ZFOD_ARGS(PROT_USER, PROT_ALL);
9080616c1c3SMichael Corcoran 
9090616c1c3SMichael Corcoran 	if (get_udatamodel() == DATAMODEL_LP64) {
9100616c1c3SMichael Corcoran 		ma_flags = 0;
9110616c1c3SMichael Corcoran 	}
9120616c1c3SMichael Corcoran 
9130616c1c3SMichael Corcoran 	vattr.va_mask = AT_SIZE;
9140616c1c3SMichael Corcoran 	error = VOP_GETATTR(vp, &vattr, 0, fcred, NULL);
9150616c1c3SMichael Corcoran 	if (error) {
9160616c1c3SMichael Corcoran 		return (error);
9170616c1c3SMichael Corcoran 	}
9180616c1c3SMichael Corcoran 
9190616c1c3SMichael Corcoran 	len = vattr.va_size;
9200616c1c3SMichael Corcoran 
9210616c1c3SMichael Corcoran 	ma_flags |= MAP_PRIVATE;
9220616c1c3SMichael Corcoran 	if (padding == 0) {
9230616c1c3SMichael Corcoran 		MOBJ_STAT_ADD(map_flat_no_padding);
9240616c1c3SMichael Corcoran 		error = VOP_MAP(vp, 0, as, &addr, len, prot, PROT_ALL,
9250616c1c3SMichael Corcoran 		    ma_flags, fcred, NULL);
9260616c1c3SMichael Corcoran 		if (error == 0) {
9270616c1c3SMichael Corcoran 			mrp[0].mr_addr = addr;
9280616c1c3SMichael Corcoran 			mrp[0].mr_msize = len;
9290616c1c3SMichael Corcoran 			mrp[0].mr_fsize = len;
9300616c1c3SMichael Corcoran 			mrp[0].mr_offset = 0;
9310616c1c3SMichael Corcoran 			mrp[0].mr_prot = prot;
9320616c1c3SMichael Corcoran 			mrp[0].mr_flags = 0;
9330616c1c3SMichael Corcoran 		}
9340616c1c3SMichael Corcoran 		return (error);
9350616c1c3SMichael Corcoran 	}
9360616c1c3SMichael Corcoran 
9370616c1c3SMichael Corcoran 	/* padding was requested so there's more work to be done */
9380616c1c3SMichael Corcoran 	MOBJ_STAT_ADD(map_flat_padding);
9390616c1c3SMichael Corcoran 
9400616c1c3SMichael Corcoran 	/* No need to reserve swap space now since it will be reserved later */
9410616c1c3SMichael Corcoran 	crargs.flags |= MAP_NORESERVE;
9420616c1c3SMichael Corcoran 
9430616c1c3SMichael Corcoran 	/* Need to setup padding which can only be in PAGESIZE increments. */
9440616c1c3SMichael Corcoran 	ASSERT((padding & PAGEOFFSET) == 0);
9450616c1c3SMichael Corcoran 	pad_len = len + (2 * padding);
9460616c1c3SMichael Corcoran 
9470616c1c3SMichael Corcoran 	as_rangelock(as);
9480616c1c3SMichael Corcoran 	map_addr(&addr, pad_len, 0, 1, ma_flags);
9490616c1c3SMichael Corcoran 	error = as_map(as, addr, pad_len, segvn_create, &crargs);
9500616c1c3SMichael Corcoran 	as_rangeunlock(as);
9510616c1c3SMichael Corcoran 	if (error) {
9520616c1c3SMichael Corcoran 		return (error);
9530616c1c3SMichael Corcoran 	}
9540616c1c3SMichael Corcoran 	start_addr = addr;
9550616c1c3SMichael Corcoran 	addr += padding;
9560616c1c3SMichael Corcoran 	ma_flags |= MAP_FIXED;
9570616c1c3SMichael Corcoran 	error = VOP_MAP(vp, 0, as, &addr, len, prot, PROT_ALL, ma_flags,
9580616c1c3SMichael Corcoran 	    fcred, NULL);
9590616c1c3SMichael Corcoran 	if (error == 0) {
9600616c1c3SMichael Corcoran 		mrp[0].mr_addr = start_addr;
9610616c1c3SMichael Corcoran 		mrp[0].mr_msize = padding;
9620616c1c3SMichael Corcoran 		mrp[0].mr_fsize = 0;
9630616c1c3SMichael Corcoran 		mrp[0].mr_offset = 0;
9640616c1c3SMichael Corcoran 		mrp[0].mr_prot = 0;
9650616c1c3SMichael Corcoran 		mrp[0].mr_flags = MR_PADDING;
9660616c1c3SMichael Corcoran 
9670616c1c3SMichael Corcoran 		mrp[1].mr_addr = addr;
9680616c1c3SMichael Corcoran 		mrp[1].mr_msize = len;
9690616c1c3SMichael Corcoran 		mrp[1].mr_fsize = len;
9700616c1c3SMichael Corcoran 		mrp[1].mr_offset = 0;
9710616c1c3SMichael Corcoran 		mrp[1].mr_prot = prot;
9720616c1c3SMichael Corcoran 		mrp[1].mr_flags = 0;
9730616c1c3SMichael Corcoran 
9740616c1c3SMichael Corcoran 		mrp[2].mr_addr = addr + P2ROUNDUP(len, PAGESIZE);
9750616c1c3SMichael Corcoran 		mrp[2].mr_msize = padding;
9760616c1c3SMichael Corcoran 		mrp[2].mr_fsize = 0;
9770616c1c3SMichael Corcoran 		mrp[2].mr_offset = 0;
9780616c1c3SMichael Corcoran 		mrp[2].mr_prot = 0;
9790616c1c3SMichael Corcoran 		mrp[2].mr_flags = MR_PADDING;
9800616c1c3SMichael Corcoran 	} else {
9810616c1c3SMichael Corcoran 		/* Need to cleanup the as_map from earlier */
9820616c1c3SMichael Corcoran 		(void) as_unmap(as, start_addr, pad_len);
9830616c1c3SMichael Corcoran 	}
9840616c1c3SMichael Corcoran 	return (error);
9850616c1c3SMichael Corcoran }
9860616c1c3SMichael Corcoran 
9870616c1c3SMichael Corcoran /*
9880616c1c3SMichael Corcoran  * Map a PT_LOAD or PT_SUNWBSS section of an executable file into the user's
9890616c1c3SMichael Corcoran  * address space.
9900616c1c3SMichael Corcoran  * vp - vnode to be mapped in
9910616c1c3SMichael Corcoran  * addr - start address
9920616c1c3SMichael Corcoran  * len - length of vp to be mapped
9930616c1c3SMichael Corcoran  * zfodlen - length of zero filled memory after len above
9940616c1c3SMichael Corcoran  * offset - offset into file where mapping should start
9950616c1c3SMichael Corcoran  * prot - protections for this mapping
9960616c1c3SMichael Corcoran  * fcred - credentials for the file associated with vp at open time.
9970616c1c3SMichael Corcoran  */
9980616c1c3SMichael Corcoran static int
mmapobj_map_ptload(struct vnode * vp,caddr_t addr,size_t len,volatile size_t zfodlen,off_t offset,int prot,cred_t * fcred)9993df2e8b2SRobert Mustacchi mmapobj_map_ptload(struct vnode *vp, caddr_t addr, size_t len,
10003df2e8b2SRobert Mustacchi     volatile size_t zfodlen, off_t offset, int prot, cred_t *fcred)
10010616c1c3SMichael Corcoran {
10020616c1c3SMichael Corcoran 	int error = 0;
10030616c1c3SMichael Corcoran 	caddr_t zfodbase, oldaddr;
10040616c1c3SMichael Corcoran 	size_t oldlen;
10050616c1c3SMichael Corcoran 	size_t end;
10060616c1c3SMichael Corcoran 	size_t zfoddiff;
10070616c1c3SMichael Corcoran 	label_t ljb;
10080616c1c3SMichael Corcoran 	struct as *as = curproc->p_as;
10090616c1c3SMichael Corcoran 	model_t model;
10100616c1c3SMichael Corcoran 	int full_page;
10110616c1c3SMichael Corcoran 
10120616c1c3SMichael Corcoran 	/*
10130616c1c3SMichael Corcoran 	 * See if addr and offset are aligned such that we can map in
10140616c1c3SMichael Corcoran 	 * full pages instead of partial pages.
10150616c1c3SMichael Corcoran 	 */
10160616c1c3SMichael Corcoran 	full_page = (((uintptr_t)addr & PAGEOFFSET) ==
10170616c1c3SMichael Corcoran 	    ((uintptr_t)offset & PAGEOFFSET));
10180616c1c3SMichael Corcoran 
10190616c1c3SMichael Corcoran 	model = get_udatamodel();
10200616c1c3SMichael Corcoran 
10210616c1c3SMichael Corcoran 	oldaddr = addr;
10220616c1c3SMichael Corcoran 	addr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
10230616c1c3SMichael Corcoran 	if (len) {
10240616c1c3SMichael Corcoran 		spgcnt_t availm, npages;
10250616c1c3SMichael Corcoran 		int preread;
10260616c1c3SMichael Corcoran 		uint_t mflag = MAP_PRIVATE | MAP_FIXED;
10270616c1c3SMichael Corcoran 
10280616c1c3SMichael Corcoran 		if (model == DATAMODEL_ILP32) {
10290616c1c3SMichael Corcoran 			mflag |= _MAP_LOW32;
10300616c1c3SMichael Corcoran 		}
10310616c1c3SMichael Corcoran 		/* We may need to map in extra bytes */
10320616c1c3SMichael Corcoran 		oldlen = len;
10330616c1c3SMichael Corcoran 		len += ((size_t)oldaddr & PAGEOFFSET);
10340616c1c3SMichael Corcoran 
10350616c1c3SMichael Corcoran 		if (full_page) {
10360616c1c3SMichael Corcoran 			offset = (off_t)((uintptr_t)offset & PAGEMASK);
10370616c1c3SMichael Corcoran 			if ((prot & (PROT_WRITE | PROT_EXEC)) == PROT_EXEC) {
10380616c1c3SMichael Corcoran 				mflag |= MAP_TEXT;
10390616c1c3SMichael Corcoran 				MOBJ_STAT_ADD(map_ptload_text);
10400616c1c3SMichael Corcoran 			} else {
10410616c1c3SMichael Corcoran 				mflag |= MAP_INITDATA;
10420616c1c3SMichael Corcoran 				MOBJ_STAT_ADD(map_ptload_initdata);
10430616c1c3SMichael Corcoran 			}
10440616c1c3SMichael Corcoran 
10450616c1c3SMichael Corcoran 			/*
10460616c1c3SMichael Corcoran 			 * maxprot is passed as PROT_ALL so that mdb can
10470616c1c3SMichael Corcoran 			 * write to this segment.
10480616c1c3SMichael Corcoran 			 */
10493df2e8b2SRobert Mustacchi 			if ((error = VOP_MAP(vp, (offset_t)offset, as, &addr,
10503df2e8b2SRobert Mustacchi 			    len, prot, PROT_ALL, mflag, fcred, NULL)) != 0) {
10510616c1c3SMichael Corcoran 				return (error);
10520616c1c3SMichael Corcoran 			}
10530616c1c3SMichael Corcoran 
10540616c1c3SMichael Corcoran 			/*
10550616c1c3SMichael Corcoran 			 * If the segment can fit and is relatively small, then
10560616c1c3SMichael Corcoran 			 * we prefault the entire segment in.  This is based
10570616c1c3SMichael Corcoran 			 * on the model that says the best working set of a
10580616c1c3SMichael Corcoran 			 * small program is all of its pages.
10590616c1c3SMichael Corcoran 			 * We only do this if freemem will not drop below
10600616c1c3SMichael Corcoran 			 * lotsfree since we don't want to induce paging.
10610616c1c3SMichael Corcoran 			 */
10620616c1c3SMichael Corcoran 			npages = (spgcnt_t)btopr(len);
10630616c1c3SMichael Corcoran 			availm = freemem - lotsfree;
10640616c1c3SMichael Corcoran 			preread = (npages < availm && len < PGTHRESH) ? 1 : 0;
10650616c1c3SMichael Corcoran 
10660616c1c3SMichael Corcoran 			/*
10670616c1c3SMichael Corcoran 			 * If we aren't prefaulting the segment,
10680616c1c3SMichael Corcoran 			 * increment "deficit", if necessary to ensure
10690616c1c3SMichael Corcoran 			 * that pages will become available when this
10700616c1c3SMichael Corcoran 			 * process starts executing.
10710616c1c3SMichael Corcoran 			 */
10720616c1c3SMichael Corcoran 			if (preread == 0 && npages > availm &&
10730616c1c3SMichael Corcoran 			    deficit < lotsfree) {
10740616c1c3SMichael Corcoran 				deficit += MIN((pgcnt_t)(npages - availm),
10750616c1c3SMichael Corcoran 				    lotsfree - deficit);
10760616c1c3SMichael Corcoran 			}
10770616c1c3SMichael Corcoran 
10780616c1c3SMichael Corcoran 			if (preread) {
10790616c1c3SMichael Corcoran 				(void) as_faulta(as, addr, len);
10800616c1c3SMichael Corcoran 				MOBJ_STAT_ADD(map_ptload_preread);
10810616c1c3SMichael Corcoran 			}
10820616c1c3SMichael Corcoran 		} else {
10830616c1c3SMichael Corcoran 			/*
10840616c1c3SMichael Corcoran 			 * addr and offset were not aligned such that we could
10850616c1c3SMichael Corcoran 			 * use VOP_MAP, thus we need to as_map the memory we
10860616c1c3SMichael Corcoran 			 * need and then read the data in from disk.
10870616c1c3SMichael Corcoran 			 * This code path is a corner case which should never
10880616c1c3SMichael Corcoran 			 * be taken, but hand crafted binaries could trigger
10890616c1c3SMichael Corcoran 			 * this logic and it needs to work correctly.
10900616c1c3SMichael Corcoran 			 */
10910616c1c3SMichael Corcoran 			MOBJ_STAT_ADD(map_ptload_unaligned_text);
10920616c1c3SMichael Corcoran 			as_rangelock(as);
10930616c1c3SMichael Corcoran 			(void) as_unmap(as, addr, len);
10940616c1c3SMichael Corcoran 
10950616c1c3SMichael Corcoran 			/*
10960616c1c3SMichael Corcoran 			 * We use zfod_argsp because we need to be able to
10970616c1c3SMichael Corcoran 			 * write to the mapping and then we'll change the
10980616c1c3SMichael Corcoran 			 * protections later if they are incorrect.
10990616c1c3SMichael Corcoran 			 */
11000616c1c3SMichael Corcoran 			error = as_map(as, addr, len, segvn_create, zfod_argsp);
11010616c1c3SMichael Corcoran 			as_rangeunlock(as);
11020616c1c3SMichael Corcoran 			if (error) {
11030616c1c3SMichael Corcoran 				MOBJ_STAT_ADD(map_ptload_unaligned_map_fail);
11040616c1c3SMichael Corcoran 				return (error);
11050616c1c3SMichael Corcoran 			}
11060616c1c3SMichael Corcoran 
11070616c1c3SMichael Corcoran 			/* Now read in the data from disk */
11080616c1c3SMichael Corcoran 			error = vn_rdwr(UIO_READ, vp, oldaddr, oldlen, offset,
11090616c1c3SMichael Corcoran 			    UIO_USERSPACE, 0, (rlim64_t)0, fcred, NULL);
11100616c1c3SMichael Corcoran 			if (error) {
11110616c1c3SMichael Corcoran 				MOBJ_STAT_ADD(map_ptload_unaligned_read_fail);
11120616c1c3SMichael Corcoran 				return (error);
11130616c1c3SMichael Corcoran 			}
11140616c1c3SMichael Corcoran 
11150616c1c3SMichael Corcoran 			/*
11160616c1c3SMichael Corcoran 			 * Now set protections.
11170616c1c3SMichael Corcoran 			 */
11180616c1c3SMichael Corcoran 			if (prot != PROT_ZFOD) {
11190616c1c3SMichael Corcoran 				(void) as_setprot(as, addr, len, prot);
11200616c1c3SMichael Corcoran 			}
11210616c1c3SMichael Corcoran 		}
11220616c1c3SMichael Corcoran 	}
11230616c1c3SMichael Corcoran 
11240616c1c3SMichael Corcoran 	if (zfodlen) {
11250616c1c3SMichael Corcoran 		end = (size_t)addr + len;
11260616c1c3SMichael Corcoran 		zfodbase = (caddr_t)P2ROUNDUP(end, PAGESIZE);
11270616c1c3SMichael Corcoran 		zfoddiff = (uintptr_t)zfodbase - end;
11280616c1c3SMichael Corcoran 		if (zfoddiff) {
1129386e9c9eSJerry Jelinek 			/*
1130386e9c9eSJerry Jelinek 			 * Before we go to zero the remaining space on the last
1131386e9c9eSJerry Jelinek 			 * page, make sure we have write permission.
1132386e9c9eSJerry Jelinek 			 *
1133386e9c9eSJerry Jelinek 			 * We need to be careful how we zero-fill the last page
1134386e9c9eSJerry Jelinek 			 * if the protection does not include PROT_WRITE. Using
1135386e9c9eSJerry Jelinek 			 * as_setprot() can cause the VM segment code to call
1136386e9c9eSJerry Jelinek 			 * segvn_vpage(), which must allocate a page struct for
1137386e9c9eSJerry Jelinek 			 * each page in the segment. If we have a very large
1138386e9c9eSJerry Jelinek 			 * segment, this may fail, so we check for that, even
1139386e9c9eSJerry Jelinek 			 * though we ignore other return values from as_setprot.
1140386e9c9eSJerry Jelinek 			 */
11410616c1c3SMichael Corcoran 			MOBJ_STAT_ADD(zfoddiff);
11420616c1c3SMichael Corcoran 			if ((prot & PROT_WRITE) == 0) {
1143386e9c9eSJerry Jelinek 				if (as_setprot(as, (caddr_t)end, zfoddiff,
1144386e9c9eSJerry Jelinek 				    prot | PROT_WRITE) == ENOMEM)
1145386e9c9eSJerry Jelinek 					return (ENOMEM);
11460616c1c3SMichael Corcoran 				MOBJ_STAT_ADD(zfoddiff_nowrite);
11470616c1c3SMichael Corcoran 			}
11480616c1c3SMichael Corcoran 			if (on_fault(&ljb)) {
11490616c1c3SMichael Corcoran 				no_fault();
11500616c1c3SMichael Corcoran 				if ((prot & PROT_WRITE) == 0) {
11510616c1c3SMichael Corcoran 					(void) as_setprot(as, (caddr_t)end,
11520616c1c3SMichael Corcoran 					    zfoddiff, prot);
11530616c1c3SMichael Corcoran 				}
11540616c1c3SMichael Corcoran 				return (EFAULT);
11550616c1c3SMichael Corcoran 			}
11560616c1c3SMichael Corcoran 			uzero((void *)end, zfoddiff);
11570616c1c3SMichael Corcoran 			no_fault();
11580616c1c3SMichael Corcoran 
11590616c1c3SMichael Corcoran 			/*
11600616c1c3SMichael Corcoran 			 * Remove write protection to return to original state
11610616c1c3SMichael Corcoran 			 */
11620616c1c3SMichael Corcoran 			if ((prot & PROT_WRITE) == 0) {
11630616c1c3SMichael Corcoran 				(void) as_setprot(as, (caddr_t)end,
11640616c1c3SMichael Corcoran 				    zfoddiff, prot);
11650616c1c3SMichael Corcoran 			}
11660616c1c3SMichael Corcoran 		}
11670616c1c3SMichael Corcoran 		if (zfodlen > zfoddiff) {
11680616c1c3SMichael Corcoran 			struct segvn_crargs crargs =
11690616c1c3SMichael Corcoran 			    SEGVN_ZFOD_ARGS(prot, PROT_ALL);
11700616c1c3SMichael Corcoran 
11710616c1c3SMichael Corcoran 			MOBJ_STAT_ADD(zfodextra);
11720616c1c3SMichael Corcoran 			zfodlen -= zfoddiff;
11730616c1c3SMichael Corcoran 			crargs.szc = AS_MAP_NO_LPOOB;
11740616c1c3SMichael Corcoran 
11750616c1c3SMichael Corcoran 
11760616c1c3SMichael Corcoran 			as_rangelock(as);
11770616c1c3SMichael Corcoran 			(void) as_unmap(as, (caddr_t)zfodbase, zfodlen);
11780616c1c3SMichael Corcoran 			error = as_map(as, (caddr_t)zfodbase,
11790616c1c3SMichael Corcoran 			    zfodlen, segvn_create, &crargs);
11800616c1c3SMichael Corcoran 			as_rangeunlock(as);
11810616c1c3SMichael Corcoran 			if (error) {
11820616c1c3SMichael Corcoran 				return (error);
11830616c1c3SMichael Corcoran 			}
11840616c1c3SMichael Corcoran 		}
11850616c1c3SMichael Corcoran 	}
11860616c1c3SMichael Corcoran 	return (0);
11870616c1c3SMichael Corcoran }
11880616c1c3SMichael Corcoran 
11890616c1c3SMichael Corcoran /*
11900616c1c3SMichael Corcoran  * Map the ELF file represented by vp into the users address space.  The
11910616c1c3SMichael Corcoran  * first mapping will start at start_addr and there will be num_elements
11920616c1c3SMichael Corcoran  * mappings.  The mappings are described by the data in mrp which may be
11930616c1c3SMichael Corcoran  * modified upon returning from this function.
11940616c1c3SMichael Corcoran  * Returns 0 for success or errno for failure.
11950616c1c3SMichael Corcoran  */
11960616c1c3SMichael Corcoran static int
mmapobj_map_elf(struct vnode * vp,caddr_t start_addr,mmapobj_result_t * mrp,int num_elements,cred_t * fcred,ushort_t e_type)11970616c1c3SMichael Corcoran mmapobj_map_elf(struct vnode *vp, caddr_t start_addr, mmapobj_result_t *mrp,
11980616c1c3SMichael Corcoran     int num_elements, cred_t *fcred, ushort_t e_type)
11990616c1c3SMichael Corcoran {
12000616c1c3SMichael Corcoran 	int i;
12010616c1c3SMichael Corcoran 	int ret;
12020616c1c3SMichael Corcoran 	caddr_t lo;
12030616c1c3SMichael Corcoran 	caddr_t hi;
12040616c1c3SMichael Corcoran 	struct as *as = curproc->p_as;
12050616c1c3SMichael Corcoran 
12060616c1c3SMichael Corcoran 	for (i = 0; i < num_elements; i++) {
12070616c1c3SMichael Corcoran 		caddr_t addr;
12080616c1c3SMichael Corcoran 		size_t p_memsz;
12090616c1c3SMichael Corcoran 		size_t p_filesz;
12100616c1c3SMichael Corcoran 		size_t zfodlen;
12110616c1c3SMichael Corcoran 		offset_t p_offset;
12120616c1c3SMichael Corcoran 		size_t dif;
12130616c1c3SMichael Corcoran 		int prot;
12140616c1c3SMichael Corcoran 
12150616c1c3SMichael Corcoran 		/* Always need to adjust mr_addr */
12160616c1c3SMichael Corcoran 		addr = start_addr + (size_t)(mrp[i].mr_addr);
12170616c1c3SMichael Corcoran 		mrp[i].mr_addr =
12180616c1c3SMichael Corcoran 		    (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
12190616c1c3SMichael Corcoran 
12200616c1c3SMichael Corcoran 		/* Padding has already been mapped */
12210616c1c3SMichael Corcoran 		if (MR_GET_TYPE(mrp[i].mr_flags) == MR_PADDING) {
12220616c1c3SMichael Corcoran 			continue;
12230616c1c3SMichael Corcoran 		}
122487aa58a7SRichard Lowe 
122587aa58a7SRichard Lowe 		/* Can't execute code from "noexec" mounted filesystem. */
122687aa58a7SRichard Lowe 		if (((vp->v_vfsp->vfs_flag & VFS_NOEXEC) != 0) &&
122787aa58a7SRichard Lowe 		    ((mrp[i].mr_prot & PROT_EXEC) != 0)) {
122887aa58a7SRichard Lowe 			MOBJ_STAT_ADD(noexec_fs);
122987aa58a7SRichard Lowe 			return (EACCES);
123087aa58a7SRichard Lowe 		}
123187aa58a7SRichard Lowe 
12320616c1c3SMichael Corcoran 		p_memsz = mrp[i].mr_msize;
12330616c1c3SMichael Corcoran 		p_filesz = mrp[i].mr_fsize;
12340616c1c3SMichael Corcoran 		zfodlen = p_memsz - p_filesz;
12350616c1c3SMichael Corcoran 		p_offset = mrp[i].mr_offset;
12360616c1c3SMichael Corcoran 		dif = (uintptr_t)(addr) & PAGEOFFSET;
12370616c1c3SMichael Corcoran 		prot = mrp[i].mr_prot | PROT_USER;
12380616c1c3SMichael Corcoran 		ret = mmapobj_map_ptload(vp, addr, p_filesz, zfodlen,
12390616c1c3SMichael Corcoran 		    p_offset, prot, fcred);
12400616c1c3SMichael Corcoran 		if (ret != 0) {
12410616c1c3SMichael Corcoran 			MOBJ_STAT_ADD(ptload_failed);
12420616c1c3SMichael Corcoran 			mmapobj_unmap(mrp, i, num_elements, e_type);
12430616c1c3SMichael Corcoran 			return (ret);
12440616c1c3SMichael Corcoran 		}
12450616c1c3SMichael Corcoran 
12460616c1c3SMichael Corcoran 		/* Need to cleanup mrp to reflect the actual values used */
12470616c1c3SMichael Corcoran 		mrp[i].mr_msize += dif;
12480616c1c3SMichael Corcoran 		mrp[i].mr_offset = (size_t)addr & PAGEOFFSET;
12490616c1c3SMichael Corcoran 	}
12500616c1c3SMichael Corcoran 
12510616c1c3SMichael Corcoran 	/* Also need to unmap any holes created above */
12520616c1c3SMichael Corcoran 	if (num_elements == 1) {
12530616c1c3SMichael Corcoran 		MOBJ_STAT_ADD(map_elf_no_holes);
12540616c1c3SMichael Corcoran 		return (0);
12550616c1c3SMichael Corcoran 	}
12560616c1c3SMichael Corcoran 	if (e_type == ET_EXEC) {
12570616c1c3SMichael Corcoran 		return (0);
12580616c1c3SMichael Corcoran 	}
12590616c1c3SMichael Corcoran 
12600616c1c3SMichael Corcoran 	as_rangelock(as);
12610616c1c3SMichael Corcoran 	lo = start_addr;
12620616c1c3SMichael Corcoran 	hi = mrp[0].mr_addr;
12630616c1c3SMichael Corcoran 
12640616c1c3SMichael Corcoran 	/* Remove holes made by the rest of the segments */
12650616c1c3SMichael Corcoran 	for (i = 0; i < num_elements - 1; i++) {
12660616c1c3SMichael Corcoran 		lo = (caddr_t)P2ROUNDUP((size_t)(mrp[i].mr_addr) +
12670616c1c3SMichael Corcoran 		    mrp[i].mr_msize, PAGESIZE);
12680616c1c3SMichael Corcoran 		hi = mrp[i + 1].mr_addr;
12690616c1c3SMichael Corcoran 		if (lo < hi) {
12700616c1c3SMichael Corcoran 			/*
12710616c1c3SMichael Corcoran 			 * If as_unmap fails we just use up a bit of extra
12720616c1c3SMichael Corcoran 			 * space
12730616c1c3SMichael Corcoran 			 */
12740616c1c3SMichael Corcoran 			(void) as_unmap(as, (caddr_t)lo,
12750616c1c3SMichael Corcoran 			    (size_t)hi - (size_t)lo);
12760616c1c3SMichael Corcoran 			MOBJ_STAT_ADD(unmap_hole);
12770616c1c3SMichael Corcoran 		}
12780616c1c3SMichael Corcoran 	}
12790616c1c3SMichael Corcoran 	as_rangeunlock(as);
12800616c1c3SMichael Corcoran 
12810616c1c3SMichael Corcoran 	return (0);
12820616c1c3SMichael Corcoran }
12830616c1c3SMichael Corcoran 
12840616c1c3SMichael Corcoran /* Ugly hack to get STRUCT_* macros to work below */
12850616c1c3SMichael Corcoran struct myphdr {
12860616c1c3SMichael Corcoran 	Phdr		x;	/* native version */
12870616c1c3SMichael Corcoran };
12880616c1c3SMichael Corcoran 
12890616c1c3SMichael Corcoran struct myphdr32 {
12900616c1c3SMichael Corcoran 	Elf32_Phdr	x;
12910616c1c3SMichael Corcoran };
12920616c1c3SMichael Corcoran 
12930616c1c3SMichael Corcoran /*
12940616c1c3SMichael Corcoran  * Calculate and return the number of loadable segments in the ELF Phdr
12950616c1c3SMichael Corcoran  * represented by phdrbase as well as the len of the total mapping and
12960616c1c3SMichael Corcoran  * the max alignment that is needed for a given segment.  On success,
12970616c1c3SMichael Corcoran  * 0 is returned, and *len, *loadable and *align have been filled out.
12980616c1c3SMichael Corcoran  * On failure, errno will be returned, which in this case is ENOTSUP
12990616c1c3SMichael Corcoran  * if we were passed an ELF file with overlapping segments.
13000616c1c3SMichael Corcoran  */
13010616c1c3SMichael Corcoran static int
calc_loadable(Ehdr * ehdrp,caddr_t phdrbase,int nphdrs,size_t * len,int * loadable,size_t * align)13020616c1c3SMichael Corcoran calc_loadable(Ehdr *ehdrp, caddr_t phdrbase, int nphdrs, size_t *len,
13030616c1c3SMichael Corcoran     int *loadable, size_t *align)
13040616c1c3SMichael Corcoran {
13050616c1c3SMichael Corcoran 	int i;
13060616c1c3SMichael Corcoran 	int hsize;
13070616c1c3SMichael Corcoran 	model_t model;
1308980cf3cfSMichael Corcoran 	ushort_t e_type = ehdrp->e_type;	/* same offset 32 and 64 bit */
13090616c1c3SMichael Corcoran 	uint_t p_type;
13100616c1c3SMichael Corcoran 	offset_t p_offset;
13110616c1c3SMichael Corcoran 	size_t p_memsz;
13120616c1c3SMichael Corcoran 	size_t p_align;
13130616c1c3SMichael Corcoran 	caddr_t vaddr;
13140616c1c3SMichael Corcoran 	int num_segs = 0;
13150616c1c3SMichael Corcoran 	caddr_t start_addr = NULL;
13160616c1c3SMichael Corcoran 	caddr_t p_end = NULL;
13170616c1c3SMichael Corcoran 	size_t max_align = 0;
1318c1106aa8SMichael Corcoran 	size_t min_align = PAGESIZE;	/* needed for vmem_xalloc */
13190616c1c3SMichael Corcoran 	STRUCT_HANDLE(myphdr, mph);
13200616c1c3SMichael Corcoran #if defined(__sparc)
13210616c1c3SMichael Corcoran 	extern int vac_size;
1322c1106aa8SMichael Corcoran 
1323c1106aa8SMichael Corcoran 	/*
1324c1106aa8SMichael Corcoran 	 * Want to prevent aliasing by making the start address at least be
1325c1106aa8SMichael Corcoran 	 * aligned to vac_size.
1326c1106aa8SMichael Corcoran 	 */
1327c1106aa8SMichael Corcoran 	min_align = MAX(PAGESIZE, vac_size);
13280616c1c3SMichael Corcoran #endif
13290616c1c3SMichael Corcoran 
13300616c1c3SMichael Corcoran 	model = get_udatamodel();
13310616c1c3SMichael Corcoran 	STRUCT_SET_HANDLE(mph, model, (struct myphdr *)phdrbase);
13320616c1c3SMichael Corcoran 
13330616c1c3SMichael Corcoran 	/* hsize alignment should have been checked before calling this func */
13340616c1c3SMichael Corcoran 	if (model == DATAMODEL_LP64) {
13350616c1c3SMichael Corcoran 		hsize = ehdrp->e_phentsize;
13360616c1c3SMichael Corcoran 		if (hsize & 7) {
13370616c1c3SMichael Corcoran 			return (ENOTSUP);
13380616c1c3SMichael Corcoran 		}
13390616c1c3SMichael Corcoran 	} else {
13400616c1c3SMichael Corcoran 		ASSERT(model == DATAMODEL_ILP32);
13410616c1c3SMichael Corcoran 		hsize = ((Elf32_Ehdr *)ehdrp)->e_phentsize;
13420616c1c3SMichael Corcoran 		if (hsize & 3) {
13430616c1c3SMichael Corcoran 			return (ENOTSUP);
13440616c1c3SMichael Corcoran 		}
13450616c1c3SMichael Corcoran 	}
13460616c1c3SMichael Corcoran 
13470616c1c3SMichael Corcoran 	/*
13480616c1c3SMichael Corcoran 	 * Determine the span of all loadable segments and calculate the
13490616c1c3SMichael Corcoran 	 * number of loadable segments.
13500616c1c3SMichael Corcoran 	 */
13510616c1c3SMichael Corcoran 	for (i = 0; i < nphdrs; i++) {
13520616c1c3SMichael Corcoran 		p_type = STRUCT_FGET(mph, x.p_type);
13530616c1c3SMichael Corcoran 		if (p_type == PT_LOAD || p_type == PT_SUNWBSS) {
13540616c1c3SMichael Corcoran 			vaddr = (caddr_t)(uintptr_t)STRUCT_FGET(mph, x.p_vaddr);
13550616c1c3SMichael Corcoran 			p_memsz = STRUCT_FGET(mph, x.p_memsz);
13560616c1c3SMichael Corcoran 
13570616c1c3SMichael Corcoran 			/*
13580616c1c3SMichael Corcoran 			 * Skip this header if it requests no memory to be
13590616c1c3SMichael Corcoran 			 * mapped.
13600616c1c3SMichael Corcoran 			 */
13610616c1c3SMichael Corcoran 			if (p_memsz == 0) {
13620616c1c3SMichael Corcoran 				STRUCT_SET_HANDLE(mph, model,
13630616c1c3SMichael Corcoran 				    (struct myphdr *)((size_t)STRUCT_BUF(mph) +
13640616c1c3SMichael Corcoran 				    hsize));
13650616c1c3SMichael Corcoran 				MOBJ_STAT_ADD(nomem_header);
13660616c1c3SMichael Corcoran 				continue;
13670616c1c3SMichael Corcoran 			}
13680616c1c3SMichael Corcoran 			if (num_segs++ == 0) {
1369980cf3cfSMichael Corcoran 				/*
1370980cf3cfSMichael Corcoran 				 * The p_vaddr of the first PT_LOAD segment
1371980cf3cfSMichael Corcoran 				 * must either be NULL or within the first
1372980cf3cfSMichael Corcoran 				 * page in order to be interpreted.
1373980cf3cfSMichael Corcoran 				 * Otherwise, its an invalid file.
1374980cf3cfSMichael Corcoran 				 */
1375980cf3cfSMichael Corcoran 				if (e_type == ET_DYN &&
1376980cf3cfSMichael Corcoran 				    ((caddr_t)((uintptr_t)vaddr &
1377980cf3cfSMichael Corcoran 				    (uintptr_t)PAGEMASK) != NULL)) {
1378980cf3cfSMichael Corcoran 					MOBJ_STAT_ADD(inval_header);
1379980cf3cfSMichael Corcoran 					return (ENOTSUP);
1380980cf3cfSMichael Corcoran 				}
13810616c1c3SMichael Corcoran 				start_addr = vaddr;
13820616c1c3SMichael Corcoran 				/*
13830616c1c3SMichael Corcoran 				 * For the first segment, we need to map from
13840616c1c3SMichael Corcoran 				 * the beginning of the file, so we will
13850616c1c3SMichael Corcoran 				 * adjust the size of the mapping to include
13860616c1c3SMichael Corcoran 				 * this memory.
13870616c1c3SMichael Corcoran 				 */
13880616c1c3SMichael Corcoran 				p_offset = STRUCT_FGET(mph, x.p_offset);
13890616c1c3SMichael Corcoran 			} else {
13900616c1c3SMichael Corcoran 				p_offset = 0;
13910616c1c3SMichael Corcoran 			}
13920616c1c3SMichael Corcoran 			/*
13930616c1c3SMichael Corcoran 			 * Check to make sure that this mapping wouldn't
13940616c1c3SMichael Corcoran 			 * overlap a previous mapping.
13950616c1c3SMichael Corcoran 			 */
13960616c1c3SMichael Corcoran 			if (vaddr < p_end) {
13970616c1c3SMichael Corcoran 				MOBJ_STAT_ADD(overlap_header);
13980616c1c3SMichael Corcoran 				return (ENOTSUP);
13990616c1c3SMichael Corcoran 			}
14000616c1c3SMichael Corcoran 
14010616c1c3SMichael Corcoran 			p_end = vaddr + p_memsz + p_offset;
14020616c1c3SMichael Corcoran 			p_end = (caddr_t)P2ROUNDUP((size_t)p_end, PAGESIZE);
14030616c1c3SMichael Corcoran 
14040616c1c3SMichael Corcoran 			p_align = STRUCT_FGET(mph, x.p_align);
14050616c1c3SMichael Corcoran 			if (p_align > 1 && p_align > max_align) {
14060616c1c3SMichael Corcoran 				max_align = p_align;
1407c1106aa8SMichael Corcoran 				if (max_align < min_align) {
1408c1106aa8SMichael Corcoran 					max_align = min_align;
1409c1106aa8SMichael Corcoran 					MOBJ_STAT_ADD(min_align);
14100616c1c3SMichael Corcoran 				}
14110616c1c3SMichael Corcoran 			}
14120616c1c3SMichael Corcoran 		}
14130616c1c3SMichael Corcoran 		STRUCT_SET_HANDLE(mph, model,
14140616c1c3SMichael Corcoran 		    (struct myphdr *)((size_t)STRUCT_BUF(mph) + hsize));
14150616c1c3SMichael Corcoran 	}
14160616c1c3SMichael Corcoran 
14170616c1c3SMichael Corcoran 	/*
14180616c1c3SMichael Corcoran 	 * The alignment should be a power of 2, if it isn't we forgive it
14190616c1c3SMichael Corcoran 	 * and round up.  On overflow, we'll set the alignment to max_align
14200616c1c3SMichael Corcoran 	 * rounded down to the nearest power of 2.
14210616c1c3SMichael Corcoran 	 */
14220616c1c3SMichael Corcoran 	if (max_align > 0 && !ISP2(max_align)) {
14230616c1c3SMichael Corcoran 		MOBJ_STAT_ADD(np2_align);
14240616c1c3SMichael Corcoran 		*align = 2 * (1L << (highbit(max_align) - 1));
14250616c1c3SMichael Corcoran 		if (*align < max_align ||
14260616c1c3SMichael Corcoran 		    (*align > UINT_MAX && model == DATAMODEL_ILP32)) {
14270616c1c3SMichael Corcoran 			MOBJ_STAT_ADD(np2_align_overflow);
14280616c1c3SMichael Corcoran 			*align = 1L << (highbit(max_align) - 1);
14290616c1c3SMichael Corcoran 		}
14300616c1c3SMichael Corcoran 	} else {
14310616c1c3SMichael Corcoran 		*align = max_align;
14320616c1c3SMichael Corcoran 	}
14330616c1c3SMichael Corcoran 
1434c1106aa8SMichael Corcoran 	ASSERT(*align >= PAGESIZE || *align == 0);
1435c1106aa8SMichael Corcoran 
14360616c1c3SMichael Corcoran 	*loadable = num_segs;
14370616c1c3SMichael Corcoran 	*len = p_end - start_addr;
14380616c1c3SMichael Corcoran 	return (0);
14390616c1c3SMichael Corcoran }
14400616c1c3SMichael Corcoran 
14410616c1c3SMichael Corcoran /*
14420616c1c3SMichael Corcoran  * Check the address space to see if the virtual addresses to be used are
14430616c1c3SMichael Corcoran  * available.  If they are not, return errno for failure.  On success, 0
14440616c1c3SMichael Corcoran  * will be returned, and the virtual addresses for each mmapobj_result_t
14450616c1c3SMichael Corcoran  * will be reserved.  Note that a reservation could have earlier been made
14460616c1c3SMichael Corcoran  * for a given segment via a /dev/null mapping.  If that is the case, then
14470616c1c3SMichael Corcoran  * we can use that VA space for our mappings.
14480616c1c3SMichael Corcoran  * Note: this function will only be used for ET_EXEC binaries.
14490616c1c3SMichael Corcoran  */
14500616c1c3SMichael Corcoran int
check_exec_addrs(int loadable,mmapobj_result_t * mrp,caddr_t start_addr)14510616c1c3SMichael Corcoran check_exec_addrs(int loadable, mmapobj_result_t *mrp, caddr_t start_addr)
14520616c1c3SMichael Corcoran {
14530616c1c3SMichael Corcoran 	int i;
14540616c1c3SMichael Corcoran 	struct as *as = curproc->p_as;
14550616c1c3SMichael Corcoran 	struct segvn_crargs crargs = SEGVN_ZFOD_ARGS(PROT_ZFOD, PROT_ALL);
14560616c1c3SMichael Corcoran 	int ret;
14570616c1c3SMichael Corcoran 	caddr_t myaddr;
14580616c1c3SMichael Corcoran 	size_t mylen;
14590616c1c3SMichael Corcoran 	struct seg *seg;
14600616c1c3SMichael Corcoran 
14610616c1c3SMichael Corcoran 	/* No need to reserve swap space now since it will be reserved later */
14620616c1c3SMichael Corcoran 	crargs.flags |= MAP_NORESERVE;
14630616c1c3SMichael Corcoran 	as_rangelock(as);
14640616c1c3SMichael Corcoran 	for (i = 0; i < loadable; i++) {
14650616c1c3SMichael Corcoran 
14660616c1c3SMichael Corcoran 		myaddr = start_addr + (size_t)mrp[i].mr_addr;
14670616c1c3SMichael Corcoran 		mylen = mrp[i].mr_msize;
14680616c1c3SMichael Corcoran 
14690616c1c3SMichael Corcoran 		/* See if there is a hole in the as for this range */
14700616c1c3SMichael Corcoran 		if (as_gap(as, mylen, &myaddr, &mylen, 0, NULL) == 0) {
14710616c1c3SMichael Corcoran 			ASSERT(myaddr == start_addr + (size_t)mrp[i].mr_addr);
14720616c1c3SMichael Corcoran 			ASSERT(mylen == mrp[i].mr_msize);
14730616c1c3SMichael Corcoran 
14740616c1c3SMichael Corcoran #ifdef DEBUG
14750616c1c3SMichael Corcoran 			if (MR_GET_TYPE(mrp[i].mr_flags) == MR_PADDING) {
14760616c1c3SMichael Corcoran 				MOBJ_STAT_ADD(exec_padding);
14770616c1c3SMichael Corcoran 			}
14780616c1c3SMichael Corcoran #endif
14790616c1c3SMichael Corcoran 			ret = as_map(as, myaddr, mylen, segvn_create, &crargs);
14800616c1c3SMichael Corcoran 			if (ret) {
14810616c1c3SMichael Corcoran 				as_rangeunlock(as);
14820616c1c3SMichael Corcoran 				mmapobj_unmap_exec(mrp, i, start_addr);
14830616c1c3SMichael Corcoran 				return (ret);
14840616c1c3SMichael Corcoran 			}
14850616c1c3SMichael Corcoran 		} else {
14860616c1c3SMichael Corcoran 			/*
14870616c1c3SMichael Corcoran 			 * There is a mapping that exists in the range
14880616c1c3SMichael Corcoran 			 * so check to see if it was a "reservation"
14890616c1c3SMichael Corcoran 			 * from /dev/null.  The mapping is from
14900616c1c3SMichael Corcoran 			 * /dev/null if the mapping comes from
14910616c1c3SMichael Corcoran 			 * segdev and the type is neither MAP_SHARED
14920616c1c3SMichael Corcoran 			 * nor MAP_PRIVATE.
14930616c1c3SMichael Corcoran 			 */
1494dc32d872SJosef 'Jeff' Sipek 			AS_LOCK_ENTER(as, RW_READER);
14950616c1c3SMichael Corcoran 			seg = as_findseg(as, myaddr, 0);
14960616c1c3SMichael Corcoran 			MOBJ_STAT_ADD(exec_addr_mapped);
14970616c1c3SMichael Corcoran 			if (seg && seg->s_ops == &segdev_ops &&
14980616c1c3SMichael Corcoran 			    ((SEGOP_GETTYPE(seg, myaddr) &
14990616c1c3SMichael Corcoran 			    (MAP_SHARED | MAP_PRIVATE)) == 0) &&
15000616c1c3SMichael Corcoran 			    myaddr >= seg->s_base &&
15010616c1c3SMichael Corcoran 			    myaddr + mylen <=
15020616c1c3SMichael Corcoran 			    seg->s_base + seg->s_size) {
15030616c1c3SMichael Corcoran 				MOBJ_STAT_ADD(exec_addr_devnull);
1504dc32d872SJosef 'Jeff' Sipek 				AS_LOCK_EXIT(as);
15050616c1c3SMichael Corcoran 				(void) as_unmap(as, myaddr, mylen);
15060616c1c3SMichael Corcoran 				ret = as_map(as, myaddr, mylen, segvn_create,
15070616c1c3SMichael Corcoran 				    &crargs);
15080616c1c3SMichael Corcoran 				mrp[i].mr_flags |= MR_RESV;
15090616c1c3SMichael Corcoran 				if (ret) {
15100616c1c3SMichael Corcoran 					as_rangeunlock(as);
15110616c1c3SMichael Corcoran 					/* Need to remap what we unmapped */
15120616c1c3SMichael Corcoran 					mmapobj_unmap_exec(mrp, i + 1,
15130616c1c3SMichael Corcoran 					    start_addr);
15140616c1c3SMichael Corcoran 					return (ret);
15150616c1c3SMichael Corcoran 				}
15160616c1c3SMichael Corcoran 			} else {
1517dc32d872SJosef 'Jeff' Sipek 				AS_LOCK_EXIT(as);
15180616c1c3SMichael Corcoran 				as_rangeunlock(as);
15190616c1c3SMichael Corcoran 				mmapobj_unmap_exec(mrp, i, start_addr);
15200616c1c3SMichael Corcoran 				MOBJ_STAT_ADD(exec_addr_in_use);
15210616c1c3SMichael Corcoran 				return (EADDRINUSE);
15220616c1c3SMichael Corcoran 			}
15230616c1c3SMichael Corcoran 		}
15240616c1c3SMichael Corcoran 	}
15250616c1c3SMichael Corcoran 	as_rangeunlock(as);
15260616c1c3SMichael Corcoran 	return (0);
15270616c1c3SMichael Corcoran }
15280616c1c3SMichael Corcoran 
15290616c1c3SMichael Corcoran /*
15300616c1c3SMichael Corcoran  * Walk through the ELF program headers and extract all useful information
15310616c1c3SMichael Corcoran  * for PT_LOAD and PT_SUNWBSS segments into mrp.
15320616c1c3SMichael Corcoran  * Return 0 on success or error on failure.
15330616c1c3SMichael Corcoran  */
15340616c1c3SMichael Corcoran static int
process_phdrs(Ehdr * ehdrp,caddr_t phdrbase,int nphdrs,mmapobj_result_t * mrp,vnode_t * vp,uint_t * num_mapped,size_t padding,cred_t * fcred)1535d2a70789SRichard Lowe process_phdrs(Ehdr *ehdrp, caddr_t phdrbase, int nphdrs, mmapobj_result_t *mrp,
15360616c1c3SMichael Corcoran     vnode_t *vp, uint_t *num_mapped, size_t padding, cred_t *fcred)
15370616c1c3SMichael Corcoran {
15380616c1c3SMichael Corcoran 	int i;
15390616c1c3SMichael Corcoran 	caddr_t start_addr = NULL;
15400616c1c3SMichael Corcoran 	caddr_t vaddr;
15410616c1c3SMichael Corcoran 	size_t len = 0;
15420616c1c3SMichael Corcoran 	size_t lib_len = 0;
15430616c1c3SMichael Corcoran 	int ret;
15440616c1c3SMichael Corcoran 	int prot;
15450616c1c3SMichael Corcoran 	struct lib_va *lvp = NULL;
15460616c1c3SMichael Corcoran 	vattr_t vattr;
15470616c1c3SMichael Corcoran 	struct as *as = curproc->p_as;
15480616c1c3SMichael Corcoran 	int error;
15490616c1c3SMichael Corcoran 	int loadable = 0;
15500616c1c3SMichael Corcoran 	int current = 0;
15510616c1c3SMichael Corcoran 	int use_lib_va = 1;
15520616c1c3SMichael Corcoran 	size_t align = 0;
15530616c1c3SMichael Corcoran 	size_t add_pad = 0;
15540616c1c3SMichael Corcoran 	int hdr_seen = 0;
15550616c1c3SMichael Corcoran 	ushort_t e_type = ehdrp->e_type;	/* same offset 32 and 64 bit */
15560616c1c3SMichael Corcoran 	uint_t p_type;
15570616c1c3SMichael Corcoran 	offset_t p_offset;
15580616c1c3SMichael Corcoran 	size_t p_memsz;
15590616c1c3SMichael Corcoran 	size_t p_filesz;
15600616c1c3SMichael Corcoran 	uint_t p_flags;
15610616c1c3SMichael Corcoran 	int hsize;
15620616c1c3SMichael Corcoran 	model_t model;
15630616c1c3SMichael Corcoran 	STRUCT_HANDLE(myphdr, mph);
15640616c1c3SMichael Corcoran 
15650616c1c3SMichael Corcoran 	model = get_udatamodel();
15660616c1c3SMichael Corcoran 	STRUCT_SET_HANDLE(mph, model, (struct myphdr *)phdrbase);
15670616c1c3SMichael Corcoran 
15680616c1c3SMichael Corcoran 	/*
15690616c1c3SMichael Corcoran 	 * Need to make sure that hsize is aligned properly.
15700616c1c3SMichael Corcoran 	 * For 32bit processes, 4 byte alignment is required.
15710616c1c3SMichael Corcoran 	 * For 64bit processes, 8 byte alignment is required.
15720616c1c3SMichael Corcoran 	 * If the alignment isn't correct, we need to return failure
15730616c1c3SMichael Corcoran 	 * since it could cause an alignment error panic while walking
15740616c1c3SMichael Corcoran 	 * the phdr array.
15750616c1c3SMichael Corcoran 	 */
15760616c1c3SMichael Corcoran 	if (model == DATAMODEL_LP64) {
15770616c1c3SMichael Corcoran 		hsize = ehdrp->e_phentsize;
15780616c1c3SMichael Corcoran 		if (hsize & 7) {
15790616c1c3SMichael Corcoran 			MOBJ_STAT_ADD(phent_align64);
15800616c1c3SMichael Corcoran 			return (ENOTSUP);
15810616c1c3SMichael Corcoran 		}
15820616c1c3SMichael Corcoran 	} else {
15830616c1c3SMichael Corcoran 		ASSERT(model == DATAMODEL_ILP32);
15840616c1c3SMichael Corcoran 		hsize = ((Elf32_Ehdr *)ehdrp)->e_phentsize;
15850616c1c3SMichael Corcoran 		if (hsize & 3) {
15860616c1c3SMichael Corcoran 			MOBJ_STAT_ADD(phent_align32);
15870616c1c3SMichael Corcoran 			return (ENOTSUP);
15880616c1c3SMichael Corcoran 		}
15890616c1c3SMichael Corcoran 	}
15900616c1c3SMichael Corcoran 
1591d2a70789SRichard Lowe 	if ((padding != 0) || secflag_enabled(curproc, PROC_SEC_ASLR)) {
15920616c1c3SMichael Corcoran 		use_lib_va = 0;
15930616c1c3SMichael Corcoran 	}
15940616c1c3SMichael Corcoran 	if (e_type == ET_DYN) {
15950616c1c3SMichael Corcoran 		vattr.va_mask = AT_FSID | AT_NODEID | AT_CTIME | AT_MTIME;
15960616c1c3SMichael Corcoran 		error = VOP_GETATTR(vp, &vattr, 0, fcred, NULL);
15970616c1c3SMichael Corcoran 		if (error) {
15980616c1c3SMichael Corcoran 			return (error);
15990616c1c3SMichael Corcoran 		}
16000616c1c3SMichael Corcoran 		/* Check to see if we already have a description for this lib */
1601d2a70789SRichard Lowe 		if (!secflag_enabled(curproc, PROC_SEC_ASLR))
1602d2a70789SRichard Lowe 			lvp = lib_va_find(&vattr);
16030616c1c3SMichael Corcoran 
16040616c1c3SMichael Corcoran 		if (lvp != NULL) {
16050616c1c3SMichael Corcoran 			MOBJ_STAT_ADD(lvp_found);
16060616c1c3SMichael Corcoran 			if (use_lib_va) {
16070616c1c3SMichael Corcoran 				start_addr = mmapobj_lookup_start_addr(lvp);
16080616c1c3SMichael Corcoran 				if (start_addr == NULL) {
16090616c1c3SMichael Corcoran 					lib_va_release(lvp);
16100616c1c3SMichael Corcoran 					return (ENOMEM);
16110616c1c3SMichael Corcoran 				}
16120616c1c3SMichael Corcoran 			}
16130616c1c3SMichael Corcoran 
16140616c1c3SMichael Corcoran 			/*
16150616c1c3SMichael Corcoran 			 * loadable may be zero if the original allocator
16160616c1c3SMichael Corcoran 			 * of lvp hasn't finished setting it up but the rest
16170616c1c3SMichael Corcoran 			 * of the fields will be accurate.
16180616c1c3SMichael Corcoran 			 */
16190616c1c3SMichael Corcoran 			loadable = lvp->lv_num_segs;
16200616c1c3SMichael Corcoran 			len = lvp->lv_len;
16210616c1c3SMichael Corcoran 			align = lvp->lv_align;
16220616c1c3SMichael Corcoran 		}
16230616c1c3SMichael Corcoran 	}
16240616c1c3SMichael Corcoran 
16250616c1c3SMichael Corcoran 	/*
16260616c1c3SMichael Corcoran 	 * Determine the span of all loadable segments and calculate the
16270616c1c3SMichael Corcoran 	 * number of loadable segments, the total len spanned by the mappings
16280616c1c3SMichael Corcoran 	 * and the max alignment, if we didn't get them above.
16290616c1c3SMichael Corcoran 	 */
16300616c1c3SMichael Corcoran 	if (loadable == 0) {
16310616c1c3SMichael Corcoran 		MOBJ_STAT_ADD(no_loadable_yet);
16320616c1c3SMichael Corcoran 		ret = calc_loadable(ehdrp, phdrbase, nphdrs, &len,
16330616c1c3SMichael Corcoran 		    &loadable, &align);
16340616c1c3SMichael Corcoran 		if (ret != 0) {
16350616c1c3SMichael Corcoran 			/*
16360616c1c3SMichael Corcoran 			 * Since it'd be an invalid file, we shouldn't have
16370616c1c3SMichael Corcoran 			 * cached it previously.
16380616c1c3SMichael Corcoran 			 */
16390616c1c3SMichael Corcoran 			ASSERT(lvp == NULL);
16400616c1c3SMichael Corcoran 			return (ret);
16410616c1c3SMichael Corcoran 		}
16420616c1c3SMichael Corcoran #ifdef DEBUG
16430616c1c3SMichael Corcoran 		if (lvp) {
16440616c1c3SMichael Corcoran 			ASSERT(len == lvp->lv_len);
16450616c1c3SMichael Corcoran 			ASSERT(align == lvp->lv_align);
16460616c1c3SMichael Corcoran 		}
16470616c1c3SMichael Corcoran #endif
16480616c1c3SMichael Corcoran 	}
16490616c1c3SMichael Corcoran 
16500616c1c3SMichael Corcoran 	/* Make sure there's something to map. */
16510616c1c3SMichael Corcoran 	if (len == 0 || loadable == 0) {
16520616c1c3SMichael Corcoran 		/*
16530616c1c3SMichael Corcoran 		 * Since it'd be an invalid file, we shouldn't have
16540616c1c3SMichael Corcoran 		 * cached it previously.
16550616c1c3SMichael Corcoran 		 */
16560616c1c3SMichael Corcoran 		ASSERT(lvp == NULL);
16570616c1c3SMichael Corcoran 		MOBJ_STAT_ADD(nothing_to_map);
16580616c1c3SMichael Corcoran 		return (ENOTSUP);
16590616c1c3SMichael Corcoran 	}
16600616c1c3SMichael Corcoran 
16610616c1c3SMichael Corcoran 	lib_len = len;
16620616c1c3SMichael Corcoran 	if (padding != 0) {
16630616c1c3SMichael Corcoran 		loadable += 2;
16640616c1c3SMichael Corcoran 	}
16650616c1c3SMichael Corcoran 	if (loadable > *num_mapped) {
16660616c1c3SMichael Corcoran 		*num_mapped = loadable;
16670616c1c3SMichael Corcoran 		/* cleanup previous reservation */
16680616c1c3SMichael Corcoran 		if (start_addr) {
16690616c1c3SMichael Corcoran 			(void) as_unmap(as, start_addr, lib_len);
16700616c1c3SMichael Corcoran 		}
16710616c1c3SMichael Corcoran 		MOBJ_STAT_ADD(e2big);
16720616c1c3SMichael Corcoran 		if (lvp) {
16730616c1c3SMichael Corcoran 			lib_va_release(lvp);
16740616c1c3SMichael Corcoran 		}
16750616c1c3SMichael Corcoran 		return (E2BIG);
16760616c1c3SMichael Corcoran 	}
16770616c1c3SMichael Corcoran 
16780616c1c3SMichael Corcoran 	/*
16790616c1c3SMichael Corcoran 	 * We now know the size of the object to map and now we need to
16800616c1c3SMichael Corcoran 	 * get the start address to map it at.  It's possible we already
16810616c1c3SMichael Corcoran 	 * have it if we found all the info we need in the lib_va cache.
16820616c1c3SMichael Corcoran 	 */
16830616c1c3SMichael Corcoran 	if (e_type == ET_DYN && start_addr == NULL) {
16840616c1c3SMichael Corcoran 		/*
16850616c1c3SMichael Corcoran 		 * Need to make sure padding does not throw off
16860616c1c3SMichael Corcoran 		 * required alignment.  We can only specify an
16870616c1c3SMichael Corcoran 		 * alignment for the starting address to be mapped,
16880616c1c3SMichael Corcoran 		 * so we round padding up to the alignment and map
16890616c1c3SMichael Corcoran 		 * from there and then throw out the extra later.
16900616c1c3SMichael Corcoran 		 */
16910616c1c3SMichael Corcoran 		if (padding != 0) {
16920616c1c3SMichael Corcoran 			if (align > 1) {
16930616c1c3SMichael Corcoran 				add_pad = P2ROUNDUP(padding, align);
16940616c1c3SMichael Corcoran 				len += add_pad;
16950616c1c3SMichael Corcoran 				MOBJ_STAT_ADD(dyn_pad_align);
16960616c1c3SMichael Corcoran 			} else {
16970616c1c3SMichael Corcoran 				MOBJ_STAT_ADD(dyn_pad_noalign);
16980616c1c3SMichael Corcoran 				len += padding;	/* at beginning */
16990616c1c3SMichael Corcoran 			}
17000616c1c3SMichael Corcoran 			len += padding;	/* at end of mapping */
17010616c1c3SMichael Corcoran 		}
17020616c1c3SMichael Corcoran 		/*
17030616c1c3SMichael Corcoran 		 * At this point, if lvp is non-NULL, then above we
17040616c1c3SMichael Corcoran 		 * already found it in the cache but did not get
17050616c1c3SMichael Corcoran 		 * the start address since we were not going to use lib_va.
17060616c1c3SMichael Corcoran 		 * Since we know that lib_va will not be used, it's safe
17070616c1c3SMichael Corcoran 		 * to call mmapobj_alloc_start_addr and know that lvp
17080616c1c3SMichael Corcoran 		 * will not be modified.
17090616c1c3SMichael Corcoran 		 */
17100616c1c3SMichael Corcoran 		ASSERT(lvp ? use_lib_va == 0 : 1);
17110616c1c3SMichael Corcoran 		start_addr = mmapobj_alloc_start_addr(&lvp, len,
1712d2a70789SRichard Lowe 		    use_lib_va,
1713d2a70789SRichard Lowe 		    secflag_enabled(curproc, PROC_SEC_ASLR),
1714d2a70789SRichard Lowe 		    align, &vattr);
17150616c1c3SMichael Corcoran 		if (start_addr == NULL) {
17160616c1c3SMichael Corcoran 			if (lvp) {
17170616c1c3SMichael Corcoran 				lib_va_release(lvp);
17180616c1c3SMichael Corcoran 			}
17190616c1c3SMichael Corcoran 			MOBJ_STAT_ADD(alloc_start_fail);
17200616c1c3SMichael Corcoran 			return (ENOMEM);
17210616c1c3SMichael Corcoran 		}
17220616c1c3SMichael Corcoran 		/*
17230616c1c3SMichael Corcoran 		 * If we can't cache it, no need to hang on to it.
17240616c1c3SMichael Corcoran 		 * Setting lv_num_segs to non-zero will make that
17250616c1c3SMichael Corcoran 		 * field active and since there are too many segments
17260616c1c3SMichael Corcoran 		 * to cache, all future users will not try to use lv_mps.
17270616c1c3SMichael Corcoran 		 */
17280616c1c3SMichael Corcoran 		if (lvp != NULL && loadable > LIBVA_CACHED_SEGS && use_lib_va) {
17290616c1c3SMichael Corcoran 			lvp->lv_num_segs = loadable;
17300616c1c3SMichael Corcoran 			lib_va_release(lvp);
17310616c1c3SMichael Corcoran 			lvp = NULL;
17320616c1c3SMichael Corcoran 			MOBJ_STAT_ADD(lvp_nocache);
17330616c1c3SMichael Corcoran 		}
17340616c1c3SMichael Corcoran 		/*
17350616c1c3SMichael Corcoran 		 * Free the beginning of the mapping if the padding
17360616c1c3SMichael Corcoran 		 * was not aligned correctly.
17370616c1c3SMichael Corcoran 		 */
17380616c1c3SMichael Corcoran 		if (padding != 0 && add_pad != padding) {
17390616c1c3SMichael Corcoran 			(void) as_unmap(as, start_addr,
17400616c1c3SMichael Corcoran 			    add_pad - padding);
17410616c1c3SMichael Corcoran 			start_addr += (add_pad - padding);
17420616c1c3SMichael Corcoran 			MOBJ_STAT_ADD(extra_padding);
17430616c1c3SMichael Corcoran 		}
17440616c1c3SMichael Corcoran 	}
17450616c1c3SMichael Corcoran 
17460616c1c3SMichael Corcoran 	/*
17470616c1c3SMichael Corcoran 	 * At this point, we have reserved the virtual address space
17480616c1c3SMichael Corcoran 	 * for our mappings.  Now we need to start filling out the mrp
17490616c1c3SMichael Corcoran 	 * array to describe all of the individual mappings we are going
17500616c1c3SMichael Corcoran 	 * to return.
17510616c1c3SMichael Corcoran 	 * For ET_EXEC there has been no memory reservation since we are
17520616c1c3SMichael Corcoran 	 * using fixed addresses.  While filling in the mrp array below,
17530616c1c3SMichael Corcoran 	 * we will have the first segment biased to start at addr 0
17540616c1c3SMichael Corcoran 	 * and the rest will be biased by this same amount.  Thus if there
17550616c1c3SMichael Corcoran 	 * is padding, the first padding will start at addr 0, and the next
17560616c1c3SMichael Corcoran 	 * segment will start at the value of padding.
17570616c1c3SMichael Corcoran 	 */
17580616c1c3SMichael Corcoran 
17590616c1c3SMichael Corcoran 	/* We'll fill out padding later, so start filling in mrp at index 1 */
17600616c1c3SMichael Corcoran 	if (padding != 0) {
17610616c1c3SMichael Corcoran 		current = 1;
17620616c1c3SMichael Corcoran 	}
17630616c1c3SMichael Corcoran 
17640616c1c3SMichael Corcoran 	/* If we have no more need for lvp let it go now */
17650616c1c3SMichael Corcoran 	if (lvp != NULL && use_lib_va == 0) {
17660616c1c3SMichael Corcoran 		lib_va_release(lvp);
17670616c1c3SMichael Corcoran 		MOBJ_STAT_ADD(lvp_not_needed);
17680616c1c3SMichael Corcoran 		lvp = NULL;
17690616c1c3SMichael Corcoran 	}
17700616c1c3SMichael Corcoran 
17710616c1c3SMichael Corcoran 	/* Now fill out the mrp structs from the program headers */
17720616c1c3SMichael Corcoran 	STRUCT_SET_HANDLE(mph, model, (struct myphdr *)phdrbase);
17730616c1c3SMichael Corcoran 	for (i = 0; i < nphdrs; i++) {
17740616c1c3SMichael Corcoran 		p_type = STRUCT_FGET(mph, x.p_type);
17750616c1c3SMichael Corcoran 		if (p_type == PT_LOAD || p_type == PT_SUNWBSS) {
17760616c1c3SMichael Corcoran 			vaddr = (caddr_t)(uintptr_t)STRUCT_FGET(mph, x.p_vaddr);
17770616c1c3SMichael Corcoran 			p_memsz = STRUCT_FGET(mph, x.p_memsz);
17780616c1c3SMichael Corcoran 			p_filesz = STRUCT_FGET(mph, x.p_filesz);
17790616c1c3SMichael Corcoran 			p_offset = STRUCT_FGET(mph, x.p_offset);
17800616c1c3SMichael Corcoran 			p_flags = STRUCT_FGET(mph, x.p_flags);
17810616c1c3SMichael Corcoran 
17820616c1c3SMichael Corcoran 			/*
17830616c1c3SMichael Corcoran 			 * Skip this header if it requests no memory to be
17840616c1c3SMichael Corcoran 			 * mapped.
17850616c1c3SMichael Corcoran 			 */
17860616c1c3SMichael Corcoran 			if (p_memsz == 0) {
17870616c1c3SMichael Corcoran 				STRUCT_SET_HANDLE(mph, model,
17880616c1c3SMichael Corcoran 				    (struct myphdr *)((size_t)STRUCT_BUF(mph) +
17890616c1c3SMichael Corcoran 				    hsize));
17900616c1c3SMichael Corcoran 				MOBJ_STAT_ADD(no_mem_map_sz);
17910616c1c3SMichael Corcoran 				continue;
17920616c1c3SMichael Corcoran 			}
17930616c1c3SMichael Corcoran 
17940616c1c3SMichael Corcoran 			prot = 0;
17950616c1c3SMichael Corcoran 			if (p_flags & PF_R)
17960616c1c3SMichael Corcoran 				prot |= PROT_READ;
17970616c1c3SMichael Corcoran 			if (p_flags & PF_W)
17980616c1c3SMichael Corcoran 				prot |= PROT_WRITE;
17990616c1c3SMichael Corcoran 			if (p_flags & PF_X)
18000616c1c3SMichael Corcoran 				prot |= PROT_EXEC;
18010616c1c3SMichael Corcoran 
18020616c1c3SMichael Corcoran 			ASSERT(current < loadable);
18030616c1c3SMichael Corcoran 			mrp[current].mr_msize = p_memsz;
18040616c1c3SMichael Corcoran 			mrp[current].mr_fsize = p_filesz;
18050616c1c3SMichael Corcoran 			mrp[current].mr_offset = p_offset;
18060616c1c3SMichael Corcoran 			mrp[current].mr_prot = prot;
18070616c1c3SMichael Corcoran 
18080616c1c3SMichael Corcoran 			if (hdr_seen == 0 && p_filesz != 0) {
18090616c1c3SMichael Corcoran 				mrp[current].mr_flags = MR_HDR_ELF;
18100616c1c3SMichael Corcoran 				/*
1811980cf3cfSMichael Corcoran 				 * We modify mr_offset because we
18120616c1c3SMichael Corcoran 				 * need to map the ELF header as well, and if
18130616c1c3SMichael Corcoran 				 * we didn't then the header could be left out
18140616c1c3SMichael Corcoran 				 * of the mapping that we will create later.
18150616c1c3SMichael Corcoran 				 * Since we're removing the offset, we need to
18160616c1c3SMichael Corcoran 				 * account for that in the other fields as well
18170616c1c3SMichael Corcoran 				 * since we will be mapping the memory from 0
18180616c1c3SMichael Corcoran 				 * to p_offset.
18190616c1c3SMichael Corcoran 				 */
18200616c1c3SMichael Corcoran 				if (e_type == ET_DYN) {
18210616c1c3SMichael Corcoran 					mrp[current].mr_offset = 0;
18220616c1c3SMichael Corcoran 					mrp[current].mr_msize += p_offset;
18230616c1c3SMichael Corcoran 					mrp[current].mr_fsize += p_offset;
18240616c1c3SMichael Corcoran 				} else {
18250616c1c3SMichael Corcoran 					ASSERT(e_type == ET_EXEC);
18260616c1c3SMichael Corcoran 					/*
18270616c1c3SMichael Corcoran 					 * Save off the start addr which will be
18280616c1c3SMichael Corcoran 					 * our bias for the rest of the
18290616c1c3SMichael Corcoran 					 * ET_EXEC mappings.
18300616c1c3SMichael Corcoran 					 */
18310616c1c3SMichael Corcoran 					start_addr = vaddr - padding;
18320616c1c3SMichael Corcoran 				}
18330616c1c3SMichael Corcoran 				mrp[current].mr_addr = (caddr_t)padding;
18340616c1c3SMichael Corcoran 				hdr_seen = 1;
18350616c1c3SMichael Corcoran 			} else {
18360616c1c3SMichael Corcoran 				if (e_type == ET_EXEC) {
18370616c1c3SMichael Corcoran 					/* bias mr_addr */
18380616c1c3SMichael Corcoran 					mrp[current].mr_addr =
18390616c1c3SMichael Corcoran 					    vaddr - (size_t)start_addr;
18400616c1c3SMichael Corcoran 				} else {
18410616c1c3SMichael Corcoran 					mrp[current].mr_addr = vaddr + padding;
18420616c1c3SMichael Corcoran 				}
18430616c1c3SMichael Corcoran 				mrp[current].mr_flags = 0;
18440616c1c3SMichael Corcoran 			}
18450616c1c3SMichael Corcoran 			current++;
18460616c1c3SMichael Corcoran 		}
18470616c1c3SMichael Corcoran 
18480616c1c3SMichael Corcoran 		/* Move to next phdr */
18490616c1c3SMichael Corcoran 		STRUCT_SET_HANDLE(mph, model,
18500616c1c3SMichael Corcoran 		    (struct myphdr *)((size_t)STRUCT_BUF(mph) +
18510616c1c3SMichael Corcoran 		    hsize));
18520616c1c3SMichael Corcoran 	}
18530616c1c3SMichael Corcoran 
18540616c1c3SMichael Corcoran 	/* Now fill out the padding segments */
18550616c1c3SMichael Corcoran 	if (padding != 0) {
18560616c1c3SMichael Corcoran 		mrp[0].mr_addr = NULL;
18570616c1c3SMichael Corcoran 		mrp[0].mr_msize = padding;
18580616c1c3SMichael Corcoran 		mrp[0].mr_fsize = 0;
18590616c1c3SMichael Corcoran 		mrp[0].mr_offset = 0;
18600616c1c3SMichael Corcoran 		mrp[0].mr_prot = 0;
18610616c1c3SMichael Corcoran 		mrp[0].mr_flags = MR_PADDING;
18620616c1c3SMichael Corcoran 
18630616c1c3SMichael Corcoran 		/* Setup padding for the last segment */
18640616c1c3SMichael Corcoran 		ASSERT(current == loadable - 1);
18650616c1c3SMichael Corcoran 		mrp[current].mr_addr = (caddr_t)lib_len + padding;
18660616c1c3SMichael Corcoran 		mrp[current].mr_msize = padding;
18670616c1c3SMichael Corcoran 		mrp[current].mr_fsize = 0;
18680616c1c3SMichael Corcoran 		mrp[current].mr_offset = 0;
18690616c1c3SMichael Corcoran 		mrp[current].mr_prot = 0;
18700616c1c3SMichael Corcoran 		mrp[current].mr_flags = MR_PADDING;
18710616c1c3SMichael Corcoran 	}
18720616c1c3SMichael Corcoran 
18730616c1c3SMichael Corcoran 	/*
18740616c1c3SMichael Corcoran 	 * Need to make sure address ranges desired are not in use or
18750616c1c3SMichael Corcoran 	 * are previously allocated reservations from /dev/null.  For
18760616c1c3SMichael Corcoran 	 * ET_DYN, we already made sure our address range was free.
18770616c1c3SMichael Corcoran 	 */
18780616c1c3SMichael Corcoran 	if (e_type == ET_EXEC) {
18790616c1c3SMichael Corcoran 		ret = check_exec_addrs(loadable, mrp, start_addr);
18800616c1c3SMichael Corcoran 		if (ret != 0) {
18810616c1c3SMichael Corcoran 			ASSERT(lvp == NULL);
18820616c1c3SMichael Corcoran 			MOBJ_STAT_ADD(check_exec_failed);
18830616c1c3SMichael Corcoran 			return (ret);
18840616c1c3SMichael Corcoran 		}
18850616c1c3SMichael Corcoran 	}
18860616c1c3SMichael Corcoran 
18870616c1c3SMichael Corcoran 	/* Finish up our business with lvp. */
18880616c1c3SMichael Corcoran 	if (lvp) {
18890616c1c3SMichael Corcoran 		ASSERT(e_type == ET_DYN);
18900616c1c3SMichael Corcoran 		if (lvp->lv_num_segs == 0 && loadable <= LIBVA_CACHED_SEGS) {
18910616c1c3SMichael Corcoran 			bcopy(mrp, lvp->lv_mps,
18920616c1c3SMichael Corcoran 			    loadable * sizeof (mmapobj_result_t));
18930616c1c3SMichael Corcoran 			membar_producer();
18940616c1c3SMichael Corcoran 		}
18950616c1c3SMichael Corcoran 		/*
18960616c1c3SMichael Corcoran 		 * Setting lv_num_segs to a non-zero value indicates that
18970616c1c3SMichael Corcoran 		 * lv_mps is now valid and can be used by other threads.
18980616c1c3SMichael Corcoran 		 * So, the above stores need to finish before lv_num_segs
18990616c1c3SMichael Corcoran 		 * is updated. lv_mps is only valid if lv_num_segs is
19000616c1c3SMichael Corcoran 		 * greater than LIBVA_CACHED_SEGS.
19010616c1c3SMichael Corcoran 		 */
19020616c1c3SMichael Corcoran 		lvp->lv_num_segs = loadable;
19030616c1c3SMichael Corcoran 		lib_va_release(lvp);
19040616c1c3SMichael Corcoran 		MOBJ_STAT_ADD(lvp_used);
19050616c1c3SMichael Corcoran 	}
19060616c1c3SMichael Corcoran 
19070616c1c3SMichael Corcoran 	/* Now that we have mrp completely filled out go map it */
19080616c1c3SMichael Corcoran 	ret = mmapobj_map_elf(vp, start_addr, mrp, loadable, fcred, e_type);
19090616c1c3SMichael Corcoran 	if (ret == 0) {
19100616c1c3SMichael Corcoran 		*num_mapped = loadable;
19110616c1c3SMichael Corcoran 	}
19120616c1c3SMichael Corcoran 
19130616c1c3SMichael Corcoran 	return (ret);
19140616c1c3SMichael Corcoran }
19150616c1c3SMichael Corcoran 
19160616c1c3SMichael Corcoran /*
19170616c1c3SMichael Corcoran  * Take the ELF file passed in, and do the work of mapping it.
19180616c1c3SMichael Corcoran  * num_mapped in - # elements in user buffer
19190616c1c3SMichael Corcoran  * num_mapped out - # sections mapped and length of mrp array if
19200616c1c3SMichael Corcoran  *			no errors.
19210616c1c3SMichael Corcoran  */
19220616c1c3SMichael Corcoran static int
doelfwork(Ehdr * ehdrp,vnode_t * vp,mmapobj_result_t * mrp,uint_t * num_mapped,size_t padding,cred_t * fcred)19230616c1c3SMichael Corcoran doelfwork(Ehdr *ehdrp, vnode_t *vp, mmapobj_result_t *mrp,
19240616c1c3SMichael Corcoran     uint_t *num_mapped, size_t padding, cred_t *fcred)
19250616c1c3SMichael Corcoran {
19260616c1c3SMichael Corcoran 	int error;
19270616c1c3SMichael Corcoran 	offset_t phoff;
19280616c1c3SMichael Corcoran 	int nphdrs;
19290616c1c3SMichael Corcoran 	unsigned char ei_class;
19300616c1c3SMichael Corcoran 	unsigned short phentsize;
19310616c1c3SMichael Corcoran 	ssize_t phsizep;
19320616c1c3SMichael Corcoran 	caddr_t phbasep;
19330616c1c3SMichael Corcoran 	int to_map;
19340616c1c3SMichael Corcoran 	model_t model;
19350616c1c3SMichael Corcoran 
19360616c1c3SMichael Corcoran 	ei_class = ehdrp->e_ident[EI_CLASS];
19370616c1c3SMichael Corcoran 	model = get_udatamodel();
19380616c1c3SMichael Corcoran 	if ((model == DATAMODEL_ILP32 && ei_class == ELFCLASS64) ||
19390616c1c3SMichael Corcoran 	    (model == DATAMODEL_LP64 && ei_class == ELFCLASS32)) {
19400616c1c3SMichael Corcoran 		MOBJ_STAT_ADD(wrong_model);
19410616c1c3SMichael Corcoran 		return (ENOTSUP);
19420616c1c3SMichael Corcoran 	}
19430616c1c3SMichael Corcoran 
19440616c1c3SMichael Corcoran 	/* Can't execute code from "noexec" mounted filesystem. */
19450616c1c3SMichael Corcoran 	if (ehdrp->e_type == ET_EXEC &&
19460616c1c3SMichael Corcoran 	    (vp->v_vfsp->vfs_flag & VFS_NOEXEC) != 0) {
19470616c1c3SMichael Corcoran 		MOBJ_STAT_ADD(noexec_fs);
19480616c1c3SMichael Corcoran 		return (EACCES);
19490616c1c3SMichael Corcoran 	}
19500616c1c3SMichael Corcoran 
19510616c1c3SMichael Corcoran 	/*
19520616c1c3SMichael Corcoran 	 * Relocatable and core files are mapped as a single flat file
19530616c1c3SMichael Corcoran 	 * since no interpretation is done on them by mmapobj.
19540616c1c3SMichael Corcoran 	 */
19550616c1c3SMichael Corcoran 	if (ehdrp->e_type == ET_REL || ehdrp->e_type == ET_CORE) {
19560616c1c3SMichael Corcoran 		to_map = padding ? 3 : 1;
19570616c1c3SMichael Corcoran 		if (*num_mapped < to_map) {
19580616c1c3SMichael Corcoran 			*num_mapped = to_map;
19590616c1c3SMichael Corcoran 			MOBJ_STAT_ADD(e2big_et_rel);
19600616c1c3SMichael Corcoran 			return (E2BIG);
19610616c1c3SMichael Corcoran 		}
19620616c1c3SMichael Corcoran 		error = mmapobj_map_flat(vp, mrp, padding, fcred);
19630616c1c3SMichael Corcoran 		if (error == 0) {
19640616c1c3SMichael Corcoran 			*num_mapped = to_map;
19650616c1c3SMichael Corcoran 			mrp[padding ? 1 : 0].mr_flags = MR_HDR_ELF;
19660616c1c3SMichael Corcoran 			MOBJ_STAT_ADD(et_rel_mapped);
19670616c1c3SMichael Corcoran 		}
19680616c1c3SMichael Corcoran 		return (error);
19690616c1c3SMichael Corcoran 	}
19700616c1c3SMichael Corcoran 
19710616c1c3SMichael Corcoran 	/* Check for an unknown ELF type */
19720616c1c3SMichael Corcoran 	if (ehdrp->e_type != ET_EXEC && ehdrp->e_type != ET_DYN) {
19730616c1c3SMichael Corcoran 		MOBJ_STAT_ADD(unknown_elf_type);
19740616c1c3SMichael Corcoran 		return (ENOTSUP);
19750616c1c3SMichael Corcoran 	}
19760616c1c3SMichael Corcoran 
19770616c1c3SMichael Corcoran 	if (ei_class == ELFCLASS32) {
19780616c1c3SMichael Corcoran 		Elf32_Ehdr *e32hdr = (Elf32_Ehdr *)ehdrp;
19790616c1c3SMichael Corcoran 		ASSERT(model == DATAMODEL_ILP32);
19800616c1c3SMichael Corcoran 		nphdrs = e32hdr->e_phnum;
19810616c1c3SMichael Corcoran 		phentsize = e32hdr->e_phentsize;
19820616c1c3SMichael Corcoran 		if (phentsize < sizeof (Elf32_Phdr)) {
19830616c1c3SMichael Corcoran 			MOBJ_STAT_ADD(phent32_too_small);
19840616c1c3SMichael Corcoran 			return (ENOTSUP);
19850616c1c3SMichael Corcoran 		}
19860616c1c3SMichael Corcoran 		phoff = e32hdr->e_phoff;
19870616c1c3SMichael Corcoran 	} else if (ei_class == ELFCLASS64) {
19880616c1c3SMichael Corcoran 		Elf64_Ehdr *e64hdr = (Elf64_Ehdr *)ehdrp;
19890616c1c3SMichael Corcoran 		ASSERT(model == DATAMODEL_LP64);
19900616c1c3SMichael Corcoran 		nphdrs = e64hdr->e_phnum;
19910616c1c3SMichael Corcoran 		phentsize = e64hdr->e_phentsize;
19920616c1c3SMichael Corcoran 		if (phentsize < sizeof (Elf64_Phdr)) {
19930616c1c3SMichael Corcoran 			MOBJ_STAT_ADD(phent64_too_small);
19940616c1c3SMichael Corcoran 			return (ENOTSUP);
19950616c1c3SMichael Corcoran 		}
19960616c1c3SMichael Corcoran 		phoff = e64hdr->e_phoff;
19970616c1c3SMichael Corcoran 	} else {
19980616c1c3SMichael Corcoran 		/* fallthrough case for an invalid ELF class */
19990616c1c3SMichael Corcoran 		MOBJ_STAT_ADD(inval_elf_class);
20000616c1c3SMichael Corcoran 		return (ENOTSUP);
20010616c1c3SMichael Corcoran 	}
20020616c1c3SMichael Corcoran 
20030616c1c3SMichael Corcoran 	/*
20040616c1c3SMichael Corcoran 	 * nphdrs should only have this value for core files which are handled
20050616c1c3SMichael Corcoran 	 * above as a single mapping.  If other file types ever use this
20060616c1c3SMichael Corcoran 	 * sentinel, then we'll add the support needed to handle this here.
20070616c1c3SMichael Corcoran 	 */
20080616c1c3SMichael Corcoran 	if (nphdrs == PN_XNUM) {
20090616c1c3SMichael Corcoran 		MOBJ_STAT_ADD(too_many_phdrs);
20100616c1c3SMichael Corcoran 		return (ENOTSUP);
20110616c1c3SMichael Corcoran 	}
20120616c1c3SMichael Corcoran 
20130616c1c3SMichael Corcoran 	phsizep = nphdrs * phentsize;
20140616c1c3SMichael Corcoran 
20150616c1c3SMichael Corcoran 	if (phsizep == 0) {
20160616c1c3SMichael Corcoran 		MOBJ_STAT_ADD(no_phsize);
20170616c1c3SMichael Corcoran 		return (ENOTSUP);
20180616c1c3SMichael Corcoran 	}
20190616c1c3SMichael Corcoran 
20200616c1c3SMichael Corcoran 	/* Make sure we only wait for memory if it's a reasonable request */
20210616c1c3SMichael Corcoran 	if (phsizep > mmapobj_alloc_threshold) {
20220616c1c3SMichael Corcoran 		MOBJ_STAT_ADD(phsize_large);
20230616c1c3SMichael Corcoran 		if ((phbasep = kmem_alloc(phsizep, KM_NOSLEEP)) == NULL) {
20240616c1c3SMichael Corcoran 			MOBJ_STAT_ADD(phsize_xtralarge);
20250616c1c3SMichael Corcoran 			return (ENOMEM);
20260616c1c3SMichael Corcoran 		}
20270616c1c3SMichael Corcoran 	} else {
20280616c1c3SMichael Corcoran 		phbasep = kmem_alloc(phsizep, KM_SLEEP);
20290616c1c3SMichael Corcoran 	}
20300616c1c3SMichael Corcoran 
20310616c1c3SMichael Corcoran 	if ((error = vn_rdwr(UIO_READ, vp, phbasep, phsizep,
20320616c1c3SMichael Corcoran 	    (offset_t)phoff, UIO_SYSSPACE, 0, (rlim64_t)0,
20330616c1c3SMichael Corcoran 	    fcred, NULL)) != 0) {
20340616c1c3SMichael Corcoran 		kmem_free(phbasep, phsizep);
20350616c1c3SMichael Corcoran 		return (error);
20360616c1c3SMichael Corcoran 	}
20370616c1c3SMichael Corcoran 
20380616c1c3SMichael Corcoran 	/* Now process the phdr's */
2039d2a70789SRichard Lowe 	error = process_phdrs(ehdrp, phbasep, nphdrs, mrp, vp, num_mapped,
20400616c1c3SMichael Corcoran 	    padding, fcred);
20410616c1c3SMichael Corcoran 	kmem_free(phbasep, phsizep);
20420616c1c3SMichael Corcoran 	return (error);
20430616c1c3SMichael Corcoran }
20440616c1c3SMichael Corcoran 
20450616c1c3SMichael Corcoran /*
20460616c1c3SMichael Corcoran  * These are the two types of files that we can interpret and we want to read
20470616c1c3SMichael Corcoran  * in enough info to cover both types when looking at the initial header.
20480616c1c3SMichael Corcoran  */
20490616c1c3SMichael Corcoran #define	MAX_HEADER_SIZE	(MAX(sizeof (Ehdr), sizeof (struct exec)))
20500616c1c3SMichael Corcoran 
20510616c1c3SMichael Corcoran /*
20520616c1c3SMichael Corcoran  * Map vp passed in in an interpreted manner.  ELF and AOUT files will be
20530616c1c3SMichael Corcoran  * interpreted and mapped appropriately for execution.
20540616c1c3SMichael Corcoran  * num_mapped in - # elements in mrp
20550616c1c3SMichael Corcoran  * num_mapped out - # sections mapped and length of mrp array if
20560616c1c3SMichael Corcoran  *		    no errors or E2BIG returned.
20570616c1c3SMichael Corcoran  *
20580616c1c3SMichael Corcoran  * Returns 0 on success, errno value on failure.
20590616c1c3SMichael Corcoran  */
20600616c1c3SMichael Corcoran static int
mmapobj_map_interpret(vnode_t * vp,mmapobj_result_t * mrp,uint_t * num_mapped,size_t padding,cred_t * fcred)20610616c1c3SMichael Corcoran mmapobj_map_interpret(vnode_t *vp, mmapobj_result_t *mrp,
20620616c1c3SMichael Corcoran     uint_t *num_mapped, size_t padding, cred_t *fcred)
20630616c1c3SMichael Corcoran {
20640616c1c3SMichael Corcoran 	int error = 0;
20650616c1c3SMichael Corcoran 	vattr_t vattr;
20660616c1c3SMichael Corcoran 	struct lib_va *lvp;
20670616c1c3SMichael Corcoran 	caddr_t start_addr;
20680616c1c3SMichael Corcoran 	model_t model;
20690616c1c3SMichael Corcoran 
20700616c1c3SMichael Corcoran 	/*
20710616c1c3SMichael Corcoran 	 * header has to be aligned to the native size of ulong_t in order
20720616c1c3SMichael Corcoran 	 * to avoid an unaligned access when dereferencing the header as
20730616c1c3SMichael Corcoran 	 * a ulong_t.  Thus we allocate our array on the stack of type
20740616c1c3SMichael Corcoran 	 * ulong_t and then have header, which we dereference later as a char
20750616c1c3SMichael Corcoran 	 * array point at lheader.
20760616c1c3SMichael Corcoran 	 */
20770616c1c3SMichael Corcoran 	ulong_t lheader[(MAX_HEADER_SIZE / (sizeof (ulong_t))) + 1];
20780616c1c3SMichael Corcoran 	caddr_t header = (caddr_t)&lheader;
20790616c1c3SMichael Corcoran 
20800616c1c3SMichael Corcoran 	vattr.va_mask = AT_FSID | AT_NODEID | AT_CTIME | AT_MTIME | AT_SIZE;
20810616c1c3SMichael Corcoran 	error = VOP_GETATTR(vp, &vattr, 0, fcred, NULL);
20820616c1c3SMichael Corcoran 	if (error) {
20830616c1c3SMichael Corcoran 		return (error);
20840616c1c3SMichael Corcoran 	}
20850616c1c3SMichael Corcoran 
20860616c1c3SMichael Corcoran 	/*
20870616c1c3SMichael Corcoran 	 * Check lib_va to see if we already have a full description
20880616c1c3SMichael Corcoran 	 * for this library.  This is the fast path and only used for
20890616c1c3SMichael Corcoran 	 * ET_DYN ELF files (dynamic libraries).
20900616c1c3SMichael Corcoran 	 */
2091d2a70789SRichard Lowe 	if (padding == 0 && !secflag_enabled(curproc, PROC_SEC_ASLR) &&
2092d2a70789SRichard Lowe 	    ((lvp = lib_va_find(&vattr)) != NULL)) {
20930616c1c3SMichael Corcoran 		int num_segs;
20940616c1c3SMichael Corcoran 
20950616c1c3SMichael Corcoran 		model = get_udatamodel();
20960616c1c3SMichael Corcoran 		if ((model == DATAMODEL_ILP32 &&
20970616c1c3SMichael Corcoran 		    lvp->lv_flags & LV_ELF64) ||
20980616c1c3SMichael Corcoran 		    (model == DATAMODEL_LP64 &&
20990616c1c3SMichael Corcoran 		    lvp->lv_flags & LV_ELF32)) {
21000616c1c3SMichael Corcoran 			lib_va_release(lvp);
21010616c1c3SMichael Corcoran 			MOBJ_STAT_ADD(fast_wrong_model);
21020616c1c3SMichael Corcoran 			return (ENOTSUP);
21030616c1c3SMichael Corcoran 		}
21040616c1c3SMichael Corcoran 		num_segs = lvp->lv_num_segs;
21050616c1c3SMichael Corcoran 		if (*num_mapped < num_segs) {
21060616c1c3SMichael Corcoran 			*num_mapped = num_segs;
21070616c1c3SMichael Corcoran 			lib_va_release(lvp);
21080616c1c3SMichael Corcoran 			MOBJ_STAT_ADD(fast_e2big);
21090616c1c3SMichael Corcoran 			return (E2BIG);
21100616c1c3SMichael Corcoran 		}
21110616c1c3SMichael Corcoran 
21120616c1c3SMichael Corcoran 		/*
21130616c1c3SMichael Corcoran 		 * Check to see if we have all the mappable program headers
21140616c1c3SMichael Corcoran 		 * cached.
21150616c1c3SMichael Corcoran 		 */
21160616c1c3SMichael Corcoran 		if (num_segs <= LIBVA_CACHED_SEGS && num_segs != 0) {
21170616c1c3SMichael Corcoran 			MOBJ_STAT_ADD(fast);
21180616c1c3SMichael Corcoran 			start_addr = mmapobj_lookup_start_addr(lvp);
21190616c1c3SMichael Corcoran 			if (start_addr == NULL) {
21200616c1c3SMichael Corcoran 				lib_va_release(lvp);
21210616c1c3SMichael Corcoran 				return (ENOMEM);
21220616c1c3SMichael Corcoran 			}
21230616c1c3SMichael Corcoran 
21240616c1c3SMichael Corcoran 			bcopy(lvp->lv_mps, mrp,
21250616c1c3SMichael Corcoran 			    num_segs * sizeof (mmapobj_result_t));
21260616c1c3SMichael Corcoran 
21270616c1c3SMichael Corcoran 			error = mmapobj_map_elf(vp, start_addr, mrp,
21280616c1c3SMichael Corcoran 			    num_segs, fcred, ET_DYN);
21290616c1c3SMichael Corcoran 
21300616c1c3SMichael Corcoran 			lib_va_release(lvp);
21310616c1c3SMichael Corcoran 			if (error == 0) {
21320616c1c3SMichael Corcoran 				*num_mapped = num_segs;
21330616c1c3SMichael Corcoran 				MOBJ_STAT_ADD(fast_success);
21340616c1c3SMichael Corcoran 			}
21350616c1c3SMichael Corcoran 			return (error);
21360616c1c3SMichael Corcoran 		}
21370616c1c3SMichael Corcoran 		MOBJ_STAT_ADD(fast_not_now);
21380616c1c3SMichael Corcoran 
21390616c1c3SMichael Corcoran 		/* Release it for now since we'll look it up below */
21400616c1c3SMichael Corcoran 		lib_va_release(lvp);
21410616c1c3SMichael Corcoran 	}
21420616c1c3SMichael Corcoran 
21430616c1c3SMichael Corcoran 	/*
21440616c1c3SMichael Corcoran 	 * Time to see if this is a file we can interpret.  If it's smaller
21450616c1c3SMichael Corcoran 	 * than this, then we can't interpret it.
21460616c1c3SMichael Corcoran 	 */
21470616c1c3SMichael Corcoran 	if (vattr.va_size < MAX_HEADER_SIZE) {
21480616c1c3SMichael Corcoran 		MOBJ_STAT_ADD(small_file);
21490616c1c3SMichael Corcoran 		return (ENOTSUP);
21500616c1c3SMichael Corcoran 	}
21510616c1c3SMichael Corcoran 
21520616c1c3SMichael Corcoran 	if ((error = vn_rdwr(UIO_READ, vp, header, MAX_HEADER_SIZE, 0,
21530616c1c3SMichael Corcoran 	    UIO_SYSSPACE, 0, (rlim64_t)0, fcred, NULL)) != 0) {
21540616c1c3SMichael Corcoran 		MOBJ_STAT_ADD(read_error);
21550616c1c3SMichael Corcoran 		return (error);
21560616c1c3SMichael Corcoran 	}
21570616c1c3SMichael Corcoran 
21580616c1c3SMichael Corcoran 	/* Verify file type */
21590616c1c3SMichael Corcoran 	if (header[EI_MAG0] == ELFMAG0 && header[EI_MAG1] == ELFMAG1 &&
21600616c1c3SMichael Corcoran 	    header[EI_MAG2] == ELFMAG2 && header[EI_MAG3] == ELFMAG3) {
21610616c1c3SMichael Corcoran 		return (doelfwork((Ehdr *)lheader, vp, mrp, num_mapped,
21620616c1c3SMichael Corcoran 		    padding, fcred));
21630616c1c3SMichael Corcoran 	}
21640616c1c3SMichael Corcoran 
21650616c1c3SMichael Corcoran 	/* Unsupported type */
21660616c1c3SMichael Corcoran 	MOBJ_STAT_ADD(unsupported);
21670616c1c3SMichael Corcoran 	return (ENOTSUP);
21680616c1c3SMichael Corcoran }
21690616c1c3SMichael Corcoran 
21700616c1c3SMichael Corcoran /*
21710616c1c3SMichael Corcoran  * Given a vnode, map it as either a flat file or interpret it and map
21720616c1c3SMichael Corcoran  * it according to the rules of the file type.
21730616c1c3SMichael Corcoran  * *num_mapped will contain the size of the mmapobj_result_t array passed in.
21740616c1c3SMichael Corcoran  * If padding is non-zero, the mappings will be padded by that amount
21750616c1c3SMichael Corcoran  * rounded up to the nearest pagesize.
21760616c1c3SMichael Corcoran  * If the mapping is successful, *num_mapped will contain the number of
21770616c1c3SMichael Corcoran  * distinct mappings created, and mrp will point to the array of
21780616c1c3SMichael Corcoran  * mmapobj_result_t's which describe these mappings.
21790616c1c3SMichael Corcoran  *
21800616c1c3SMichael Corcoran  * On error, -1 is returned and errno is set appropriately.
21810616c1c3SMichael Corcoran  * A special error case will set errno to E2BIG when there are more than
21820616c1c3SMichael Corcoran  * *num_mapped mappings to be created and *num_mapped will be set to the
21830616c1c3SMichael Corcoran  * number of mappings needed.
21840616c1c3SMichael Corcoran  */
21850616c1c3SMichael Corcoran int
mmapobj(vnode_t * vp,uint_t flags,mmapobj_result_t * mrp,uint_t * num_mapped,size_t padding,cred_t * fcred)21860616c1c3SMichael Corcoran mmapobj(vnode_t *vp, uint_t flags, mmapobj_result_t *mrp,
21870616c1c3SMichael Corcoran     uint_t *num_mapped, size_t padding, cred_t *fcred)
21880616c1c3SMichael Corcoran {
21890616c1c3SMichael Corcoran 	int to_map;
21900616c1c3SMichael Corcoran 	int error = 0;
21910616c1c3SMichael Corcoran 
21920616c1c3SMichael Corcoran 	ASSERT((padding & PAGEOFFSET) == 0);
21930616c1c3SMichael Corcoran 	ASSERT((flags & ~MMOBJ_ALL_FLAGS) == 0);
21940616c1c3SMichael Corcoran 	ASSERT(num_mapped != NULL);
21950616c1c3SMichael Corcoran 	ASSERT((flags & MMOBJ_PADDING) ? padding != 0 : padding == 0);
21960616c1c3SMichael Corcoran 
21970616c1c3SMichael Corcoran 	if ((flags & MMOBJ_INTERPRET) == 0) {
21980616c1c3SMichael Corcoran 		to_map = padding ? 3 : 1;
21990616c1c3SMichael Corcoran 		if (*num_mapped < to_map) {
22000616c1c3SMichael Corcoran 			*num_mapped = to_map;
22010616c1c3SMichael Corcoran 			MOBJ_STAT_ADD(flat_e2big);
22020616c1c3SMichael Corcoran 			return (E2BIG);
22030616c1c3SMichael Corcoran 		}
22040616c1c3SMichael Corcoran 		error = mmapobj_map_flat(vp, mrp, padding, fcred);
22050616c1c3SMichael Corcoran 
22060616c1c3SMichael Corcoran 		if (error) {
22070616c1c3SMichael Corcoran 			return (error);
22080616c1c3SMichael Corcoran 		}
22090616c1c3SMichael Corcoran 		*num_mapped = to_map;
22100616c1c3SMichael Corcoran 		return (0);
22110616c1c3SMichael Corcoran 	}
22120616c1c3SMichael Corcoran 
22130616c1c3SMichael Corcoran 	error = mmapobj_map_interpret(vp, mrp, num_mapped, padding, fcred);
22140616c1c3SMichael Corcoran 	return (error);
22150616c1c3SMichael Corcoran }
2216