xref: /titanic_44/usr/src/uts/common/os/mmapobj.c (revision a1e9eea083a8f257157edb8a1efb5bbd300eb4bf)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/types.h>
27 #include <sys/sysmacros.h>
28 #include <sys/kmem.h>
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/errno.h>
32 #include <sys/mman.h>
33 #include <sys/cmn_err.h>
34 #include <sys/cred.h>
35 #include <sys/vmsystm.h>
36 #include <sys/machsystm.h>
37 #include <sys/debug.h>
38 #include <vm/as.h>
39 #include <vm/seg.h>
40 #include <sys/vmparam.h>
41 #include <sys/vfs.h>
42 #include <sys/elf.h>
43 #include <sys/machelf.h>
44 #include <sys/corectl.h>
45 #include <sys/exec.h>
46 #include <sys/exechdr.h>
47 #include <sys/autoconf.h>
48 #include <sys/mem.h>
49 #include <vm/seg_dev.h>
50 #include <sys/vmparam.h>
51 #include <sys/mmapobj.h>
52 #include <sys/atomic.h>
53 
54 /*
55  * Theory statement:
56  *
57  * The main driving force behind mmapobj is to interpret and map ELF files
58  * inside of the kernel instead of having the linker be responsible for this.
59  *
60  * mmapobj also supports the AOUT 4.x binary format as well as flat files in
61  * a read only manner.
62  *
63  * When interpreting and mapping an ELF file, mmapobj will map each PT_LOAD
64  * or PT_SUNWBSS segment according to the ELF standard.  Refer to the "Linker
65  * and Libraries Guide" for more information about the standard and mapping
66  * rules.
67  *
68  * Having mmapobj interpret and map objects will allow the kernel to make the
69  * best decision for where to place the mappings for said objects.  Thus, we
70  * can make optimizations inside of the kernel for specific platforms or
71  * cache mapping information to make mapping objects faster.
72  *
73  * The lib_va_hash will be one such optimization.  For each ELF object that
74  * mmapobj is asked to interpret, we will attempt to cache the information
75  * about the PT_LOAD and PT_SUNWBSS sections to speed up future mappings of
76  * the same objects.  We will cache up to LIBVA_CACHED_SEGS (see below) program
77  * headers which should cover a majority of the libraries out there without
78  * wasting space.  In order to make sure that the cached information is valid,
79  * we check the passed in vnode's mtime and ctime to make sure the vnode
80  * has not been modified since the last time we used it.
81  *
82  * In addition, the lib_va_hash may contain a preferred starting VA for the
83  * object which can be useful for platforms which support a shared context.
84  * This will increase the likelyhood that library text can be shared among
85  * many different processes.  We limit the reserved VA space for 32 bit objects
86  * in order to minimize fragmenting the processes address space.
87  *
88  * In addition to the above, the mmapobj interface allows for padding to be
89  * requested before the first mapping and after the last mapping created.
90  * When padding is requested, no additional optimizations will be made for
91  * that request.
92  */
93 
94 /*
95  * Threshold to prevent allocating too much kernel memory to read in the
96  * program headers for an object.  If it requires more than below,
97  * we will use a KM_NOSLEEP allocation to allocate memory to hold all of the
98  * program headers which could possibly fail.  If less memory than below is
99  * needed, then we use a KM_SLEEP allocation and are willing to wait for the
100  * memory if we need to.
101  */
102 size_t mmapobj_alloc_threshold = 65536;
103 
104 /* Debug stats for test coverage */
105 #ifdef DEBUG
106 struct mobj_stats {
107 	uint_t	mobjs_unmap_called;
108 	uint_t	mobjs_remap_devnull;
109 	uint_t	mobjs_lookup_start;
110 	uint_t	mobjs_alloc_start;
111 	uint_t	mobjs_alloc_vmem;
112 	uint_t	mobjs_add_collision;
113 	uint_t	mobjs_get_addr;
114 	uint_t	mobjs_map_flat_no_padding;
115 	uint_t	mobjs_map_flat_padding;
116 	uint_t	mobjs_map_ptload_text;
117 	uint_t	mobjs_map_ptload_initdata;
118 	uint_t	mobjs_map_ptload_preread;
119 	uint_t	mobjs_map_ptload_unaligned_text;
120 	uint_t	mobjs_map_ptload_unaligned_map_fail;
121 	uint_t	mobjs_map_ptload_unaligned_read_fail;
122 	uint_t	mobjs_zfoddiff;
123 	uint_t	mobjs_zfoddiff_nowrite;
124 	uint_t	mobjs_zfodextra;
125 	uint_t	mobjs_ptload_failed;
126 	uint_t	mobjs_map_elf_no_holes;
127 	uint_t	mobjs_unmap_hole;
128 	uint_t	mobjs_nomem_header;
129 	uint_t	mobjs_inval_header;
130 	uint_t	mobjs_overlap_header;
131 	uint_t	mobjs_np2_align;
132 	uint_t	mobjs_np2_align_overflow;
133 	uint_t	mobjs_exec_padding;
134 	uint_t	mobjs_exec_addr_mapped;
135 	uint_t	mobjs_exec_addr_devnull;
136 	uint_t	mobjs_exec_addr_in_use;
137 	uint_t	mobjs_lvp_found;
138 	uint_t	mobjs_no_loadable_yet;
139 	uint_t	mobjs_nothing_to_map;
140 	uint_t	mobjs_e2big;
141 	uint_t	mobjs_dyn_pad_align;
142 	uint_t	mobjs_dyn_pad_noalign;
143 	uint_t	mobjs_alloc_start_fail;
144 	uint_t	mobjs_lvp_nocache;
145 	uint_t	mobjs_extra_padding;
146 	uint_t	mobjs_lvp_not_needed;
147 	uint_t	mobjs_no_mem_map_sz;
148 	uint_t	mobjs_check_exec_failed;
149 	uint_t	mobjs_lvp_used;
150 	uint_t	mobjs_wrong_model;
151 	uint_t	mobjs_noexec_fs;
152 	uint_t	mobjs_e2big_et_rel;
153 	uint_t	mobjs_et_rel_mapped;
154 	uint_t	mobjs_unknown_elf_type;
155 	uint_t	mobjs_phent32_too_small;
156 	uint_t	mobjs_phent64_too_small;
157 	uint_t	mobjs_inval_elf_class;
158 	uint_t	mobjs_too_many_phdrs;
159 	uint_t	mobjs_no_phsize;
160 	uint_t	mobjs_phsize_large;
161 	uint_t	mobjs_phsize_xtralarge;
162 	uint_t	mobjs_fast_wrong_model;
163 	uint_t	mobjs_fast_e2big;
164 	uint_t	mobjs_fast;
165 	uint_t	mobjs_fast_success;
166 	uint_t	mobjs_fast_not_now;
167 	uint_t	mobjs_small_file;
168 	uint_t	mobjs_read_error;
169 	uint_t	mobjs_unsupported;
170 	uint_t	mobjs_flat_e2big;
171 	uint_t	mobjs_phent_align32;
172 	uint_t	mobjs_phent_align64;
173 	uint_t	mobjs_lib_va_find_hit;
174 	uint_t	mobjs_lib_va_find_delay_delete;
175 	uint_t	mobjs_lib_va_find_delete;
176 	uint_t	mobjs_lib_va_add_delay_delete;
177 	uint_t	mobjs_lib_va_add_delete;
178 	uint_t	mobjs_lib_va_create_failure;
179 	uint_t	mobjs_min_align;
180 #if defined(__sparc)
181 	uint_t	mobjs_aout_uzero_fault;
182 	uint_t	mobjs_aout_64bit_try;
183 	uint_t	mobjs_aout_noexec;
184 	uint_t	mobjs_aout_e2big;
185 	uint_t	mobjs_aout_lib;
186 	uint_t	mobjs_aout_fixed;
187 	uint_t	mobjs_aout_zfoddiff;
188 	uint_t	mobjs_aout_map_bss;
189 	uint_t	mobjs_aout_bss_fail;
190 	uint_t	mobjs_aout_nlist;
191 	uint_t	mobjs_aout_addr_in_use;
192 #endif
193 } mobj_stats;
194 
195 #define	MOBJ_STAT_ADD(stat)		((mobj_stats.mobjs_##stat)++)
196 #else
197 #define	MOBJ_STAT_ADD(stat)
198 #endif
199 
200 /* lv_flags values - bitmap */
201 #define	LV_ELF32	0x1		/* 32 bit ELF file */
202 #define	LV_ELF64	0x2		/* 64 bit ELF file */
203 #define	LV_DEL		0x4		/* delete when lv_refcnt hits zero */
204 
205 /*
206  * Note: lv_num_segs will denote how many segments this file has and will
207  * only be set after the lv_mps array has been filled out.
208  * lv_mps can only be valid if lv_num_segs is non-zero.
209  */
210 struct lib_va {
211 	struct lib_va		*lv_next;
212 	caddr_t			lv_base_va;	/* start va for library */
213 	ssize_t			lv_len;		/* total va span of library */
214 	size_t			lv_align;	/* minimum alignment */
215 	uint64_t		lv_nodeid;	/* filesystem node id */
216 	uint64_t		lv_fsid;	/* filesystem id */
217 	timestruc_t		lv_ctime;	/* last time file was changed */
218 	timestruc_t		lv_mtime;	/* or modified */
219 	mmapobj_result_t	lv_mps[LIBVA_CACHED_SEGS]; /* cached pheaders */
220 	int			lv_num_segs;	/* # segs for this file */
221 	int			lv_flags;
222 	uint_t			lv_refcnt;	/* number of holds on struct */
223 };
224 
225 #define	LIB_VA_SIZE	1024
226 #define	LIB_VA_MASK	(LIB_VA_SIZE - 1)
227 #define	LIB_VA_MUTEX_SHIFT	3
228 
229 #if (LIB_VA_SIZE & (LIB_VA_SIZE - 1))
230 #error	"LIB_VA_SIZE is not a power of 2"
231 #endif
232 
233 static struct lib_va *lib_va_hash[LIB_VA_SIZE];
234 static kmutex_t lib_va_hash_mutex[LIB_VA_SIZE >> LIB_VA_MUTEX_SHIFT];
235 
236 #define	LIB_VA_HASH_MUTEX(index)					\
237 	(&lib_va_hash_mutex[index >> LIB_VA_MUTEX_SHIFT])
238 
239 #define	LIB_VA_HASH(nodeid)						\
240 	(((nodeid) ^ ((nodeid) << 7) ^ ((nodeid) << 13)) & LIB_VA_MASK)
241 
242 #define	LIB_VA_MATCH_ID(arg1, arg2)					\
243 	((arg1)->lv_nodeid == (arg2)->va_nodeid &&			\
244 	(arg1)->lv_fsid == (arg2)->va_fsid)
245 
246 #define	LIB_VA_MATCH_TIME(arg1, arg2)					\
247 	((arg1)->lv_ctime.tv_sec == (arg2)->va_ctime.tv_sec &&		\
248 	(arg1)->lv_mtime.tv_sec == (arg2)->va_mtime.tv_sec &&		\
249 	(arg1)->lv_ctime.tv_nsec == (arg2)->va_ctime.tv_nsec &&		\
250 	(arg1)->lv_mtime.tv_nsec == (arg2)->va_mtime.tv_nsec)
251 
252 #define	LIB_VA_MATCH(arg1, arg2)					\
253 	(LIB_VA_MATCH_ID(arg1, arg2) && LIB_VA_MATCH_TIME(arg1, arg2))
254 
255 /*
256  * lib_va will be used for optimized allocation of address ranges for
257  * libraries, such that subsequent mappings of the same library will attempt
258  * to use the same VA as previous mappings of that library.
259  * In order to map libraries at the same VA in many processes, we need to carve
260  * out our own address space for them which is unique across many processes.
261  * We use different arenas for 32 bit and 64 bit libraries.
262  *
263  * Since the 32 bit address space is relatively small, we limit the number of
264  * libraries which try to use consistent virtual addresses to lib_threshold.
265  * For 64 bit libraries there is no such limit since the address space is large.
266  */
267 static vmem_t *lib_va_32_arena;
268 static vmem_t *lib_va_64_arena;
269 uint_t lib_threshold = 20;	/* modifiable via /etc/system */
270 
271 static kmutex_t lib_va_init_mutex;	/* no need to initialize */
272 
273 /*
274  * Number of 32 bit and 64 bit libraries in lib_va hash.
275  */
276 static uint_t libs_mapped_32 = 0;
277 static uint_t libs_mapped_64 = 0;
278 
279 /*
280  * Free up the resources associated with lvp as well as lvp itself.
281  * We also decrement the number of libraries mapped via a lib_va
282  * cached virtual address.
283  */
284 void
285 lib_va_free(struct lib_va *lvp)
286 {
287 	int is_64bit = lvp->lv_flags & LV_ELF64;
288 	ASSERT(lvp->lv_refcnt == 0);
289 
290 	if (lvp->lv_base_va != NULL) {
291 		vmem_xfree(is_64bit ? lib_va_64_arena : lib_va_32_arena,
292 		    lvp->lv_base_va, lvp->lv_len);
293 		if (is_64bit) {
294 			atomic_add_32(&libs_mapped_64, -1);
295 		} else {
296 			atomic_add_32(&libs_mapped_32, -1);
297 		}
298 	}
299 	kmem_free(lvp, sizeof (struct lib_va));
300 }
301 
302 /*
303  * See if the file associated with the vap passed in is in the lib_va hash.
304  * If it is and the file has not been modified since last use, then
305  * return a pointer to that data.  Otherwise, return NULL if the file has
306  * changed or the file was not found in the hash.
307  */
308 static struct lib_va *
309 lib_va_find(vattr_t *vap)
310 {
311 	struct lib_va *lvp;
312 	struct lib_va *del = NULL;
313 	struct lib_va **tmp;
314 	uint_t index;
315 	index = LIB_VA_HASH(vap->va_nodeid);
316 
317 	mutex_enter(LIB_VA_HASH_MUTEX(index));
318 	tmp = &lib_va_hash[index];
319 	while (*tmp != NULL) {
320 		lvp = *tmp;
321 		if (LIB_VA_MATCH_ID(lvp, vap)) {
322 			if (LIB_VA_MATCH_TIME(lvp, vap)) {
323 				ASSERT((lvp->lv_flags & LV_DEL) == 0);
324 				lvp->lv_refcnt++;
325 				MOBJ_STAT_ADD(lib_va_find_hit);
326 			} else {
327 				/*
328 				 * file was updated since last use.
329 				 * need to remove it from list.
330 				 */
331 				del = lvp;
332 				*tmp = del->lv_next;
333 				del->lv_next = NULL;
334 				/*
335 				 * If we can't delete it now, mark it for later
336 				 */
337 				if (del->lv_refcnt) {
338 					MOBJ_STAT_ADD(lib_va_find_delay_delete);
339 					del->lv_flags |= LV_DEL;
340 					del = NULL;
341 				}
342 				lvp = NULL;
343 			}
344 			mutex_exit(LIB_VA_HASH_MUTEX(index));
345 			if (del) {
346 				ASSERT(del->lv_refcnt == 0);
347 				MOBJ_STAT_ADD(lib_va_find_delete);
348 				lib_va_free(del);
349 			}
350 			return (lvp);
351 		}
352 		tmp = &lvp->lv_next;
353 	}
354 	mutex_exit(LIB_VA_HASH_MUTEX(index));
355 	return (NULL);
356 }
357 
358 /*
359  * Add a new entry to the lib_va hash.
360  * Search the hash while holding the appropriate mutex to make sure that the
361  * data is not already in the cache.  If we find data that is in the cache
362  * already and has not been modified since last use, we return NULL.  If it
363  * has been modified since last use, we will remove that entry from
364  * the hash and it will be deleted once it's reference count reaches zero.
365  * If there is no current entry in the hash we will add the new entry and
366  * return it to the caller who is responsible for calling lib_va_release to
367  * drop their reference count on it.
368  *
369  * lv_num_segs will be set to zero since the caller needs to add that
370  * information to the data structure.
371  */
372 static struct lib_va *
373 lib_va_add_hash(caddr_t base_va, ssize_t len, size_t align, vattr_t *vap)
374 {
375 	struct lib_va *lvp;
376 	uint_t index;
377 	model_t model;
378 	struct lib_va **tmp;
379 	struct lib_va *del = NULL;
380 
381 	model = get_udatamodel();
382 	index = LIB_VA_HASH(vap->va_nodeid);
383 
384 	lvp = kmem_alloc(sizeof (struct lib_va), KM_SLEEP);
385 
386 	mutex_enter(LIB_VA_HASH_MUTEX(index));
387 
388 	/*
389 	 * Make sure not adding same data a second time.
390 	 * The hash chains should be relatively short and adding
391 	 * is a relatively rare event, so it's worth the check.
392 	 */
393 	tmp = &lib_va_hash[index];
394 	while (*tmp != NULL) {
395 		if (LIB_VA_MATCH_ID(*tmp, vap)) {
396 			if (LIB_VA_MATCH_TIME(*tmp, vap)) {
397 				mutex_exit(LIB_VA_HASH_MUTEX(index));
398 				kmem_free(lvp, sizeof (struct lib_va));
399 				return (NULL);
400 			}
401 
402 			/*
403 			 * We have the same nodeid and fsid but the file has
404 			 * been modified since we last saw it.
405 			 * Need to remove the old node and add this new
406 			 * one.
407 			 * Could probably use a callback mechanism to make
408 			 * this cleaner.
409 			 */
410 			ASSERT(del == NULL);
411 			del = *tmp;
412 			*tmp = del->lv_next;
413 			del->lv_next = NULL;
414 
415 			/*
416 			 * Check to see if we can free it.  If lv_refcnt
417 			 * is greater than zero, than some other thread
418 			 * has a reference to the one we want to delete
419 			 * and we can not delete it.  All of this is done
420 			 * under the lib_va_hash_mutex lock so it is atomic.
421 			 */
422 			if (del->lv_refcnt) {
423 				MOBJ_STAT_ADD(lib_va_add_delay_delete);
424 				del->lv_flags |= LV_DEL;
425 				del = NULL;
426 			}
427 			/* tmp is already advanced */
428 			continue;
429 		}
430 		tmp = &((*tmp)->lv_next);
431 	}
432 
433 	lvp->lv_base_va = base_va;
434 	lvp->lv_len = len;
435 	lvp->lv_align = align;
436 	lvp->lv_nodeid = vap->va_nodeid;
437 	lvp->lv_fsid = vap->va_fsid;
438 	lvp->lv_ctime.tv_sec = vap->va_ctime.tv_sec;
439 	lvp->lv_ctime.tv_nsec = vap->va_ctime.tv_nsec;
440 	lvp->lv_mtime.tv_sec = vap->va_mtime.tv_sec;
441 	lvp->lv_mtime.tv_nsec = vap->va_mtime.tv_nsec;
442 	lvp->lv_next = NULL;
443 	lvp->lv_refcnt = 1;
444 
445 	/* Caller responsible for filling this and lv_mps out */
446 	lvp->lv_num_segs = 0;
447 
448 	if (model == DATAMODEL_LP64) {
449 		lvp->lv_flags = LV_ELF64;
450 	} else {
451 		ASSERT(model == DATAMODEL_ILP32);
452 		lvp->lv_flags = LV_ELF32;
453 	}
454 
455 	if (base_va != NULL) {
456 		if (model == DATAMODEL_LP64) {
457 			atomic_add_32(&libs_mapped_64, 1);
458 		} else {
459 			ASSERT(model == DATAMODEL_ILP32);
460 			atomic_add_32(&libs_mapped_32, 1);
461 		}
462 	}
463 	ASSERT(*tmp == NULL);
464 	*tmp = lvp;
465 	mutex_exit(LIB_VA_HASH_MUTEX(index));
466 	if (del) {
467 		ASSERT(del->lv_refcnt == 0);
468 		MOBJ_STAT_ADD(lib_va_add_delete);
469 		lib_va_free(del);
470 	}
471 	return (lvp);
472 }
473 
474 /*
475  * Release the hold on lvp which was acquired by lib_va_find or lib_va_add_hash.
476  * In addition, if this is the last hold and lvp is marked for deletion,
477  * free up it's reserved address space and free the structure.
478  */
479 static void
480 lib_va_release(struct lib_va *lvp)
481 {
482 	uint_t index;
483 	int to_del = 0;
484 
485 	ASSERT(lvp->lv_refcnt > 0);
486 
487 	index = LIB_VA_HASH(lvp->lv_nodeid);
488 	mutex_enter(LIB_VA_HASH_MUTEX(index));
489 	if (--lvp->lv_refcnt == 0 && (lvp->lv_flags & LV_DEL)) {
490 		to_del = 1;
491 	}
492 	mutex_exit(LIB_VA_HASH_MUTEX(index));
493 	if (to_del) {
494 		ASSERT(lvp->lv_next == 0);
495 		lib_va_free(lvp);
496 	}
497 }
498 
499 /*
500  * Dummy function for mapping through /dev/null
501  * Normally I would have used mmmmap in common/io/mem.c
502  * but that is a static function, and for /dev/null, it
503  * just returns -1.
504  */
505 /* ARGSUSED */
506 static int
507 mmapobj_dummy(dev_t dev, off_t off, int prot)
508 {
509 	return (-1);
510 }
511 
512 /*
513  * Called when an error occurred which requires mmapobj to return failure.
514  * All mapped objects will be unmapped and /dev/null mappings will be
515  * reclaimed if necessary.
516  * num_mapped is the number of elements of mrp which have been mapped, and
517  * num_segs is the total number of elements in mrp.
518  * For e_type ET_EXEC, we need to unmap all of the elements in mrp since
519  * we had already made reservations for them.
520  * If num_mapped equals num_segs, then we know that we had fully mapped
521  * the file and only need to clean up the segments described.
522  * If they are not equal, then for ET_DYN we will unmap the range from the
523  * end of the last mapped segment to the end of the last segment in mrp
524  * since we would have made a reservation for that memory earlier.
525  * If e_type is passed in as zero, num_mapped must equal num_segs.
526  */
527 void
528 mmapobj_unmap(mmapobj_result_t *mrp, int num_mapped, int num_segs,
529     ushort_t e_type)
530 {
531 	int i;
532 	struct as *as = curproc->p_as;
533 	caddr_t addr;
534 	size_t size;
535 
536 	if (e_type == ET_EXEC) {
537 		num_mapped = num_segs;
538 	}
539 #ifdef DEBUG
540 	if (e_type == 0) {
541 		ASSERT(num_mapped == num_segs);
542 	}
543 #endif
544 
545 	MOBJ_STAT_ADD(unmap_called);
546 	for (i = 0; i < num_mapped; i++) {
547 
548 		/*
549 		 * If we are going to have to create a mapping we need to
550 		 * make sure that no one else will use the address we
551 		 * need to remap between the time it is unmapped and
552 		 * mapped below.
553 		 */
554 		if (mrp[i].mr_flags & MR_RESV) {
555 			as_rangelock(as);
556 		}
557 		/* Always need to unmap what we mapped */
558 		(void) as_unmap(as, mrp[i].mr_addr, mrp[i].mr_msize);
559 
560 		/* Need to reclaim /dev/null reservation from earlier */
561 		if (mrp[i].mr_flags & MR_RESV) {
562 			struct segdev_crargs dev_a;
563 
564 			ASSERT(e_type != ET_DYN);
565 			/*
566 			 * Use seg_dev segment driver for /dev/null mapping.
567 			 */
568 			dev_a.mapfunc = mmapobj_dummy;
569 			dev_a.dev = makedevice(mm_major, M_NULL);
570 			dev_a.offset = 0;
571 			dev_a.type = 0;		/* neither PRIVATE nor SHARED */
572 			dev_a.prot = dev_a.maxprot = (uchar_t)PROT_NONE;
573 			dev_a.hat_attr = 0;
574 			dev_a.hat_flags = 0;
575 
576 			(void) as_map(as, mrp[i].mr_addr, mrp[i].mr_msize,
577 			    segdev_create, &dev_a);
578 			MOBJ_STAT_ADD(remap_devnull);
579 			as_rangeunlock(as);
580 		}
581 	}
582 
583 	if (num_mapped != num_segs) {
584 		ASSERT(e_type == ET_DYN);
585 		/* Need to unmap any reservation made after last mapped seg */
586 		if (num_mapped == 0) {
587 			addr = mrp[0].mr_addr;
588 		} else {
589 			addr = mrp[num_mapped - 1].mr_addr +
590 			    mrp[num_mapped - 1].mr_msize;
591 		}
592 		size = (size_t)mrp[num_segs - 1].mr_addr +
593 		    mrp[num_segs - 1].mr_msize - (size_t)addr;
594 		(void) as_unmap(as, addr, size);
595 
596 		/*
597 		 * Now we need to unmap the holes between mapped segs.
598 		 * Note that we have not mapped all of the segments and thus
599 		 * the holes between segments would not have been unmapped
600 		 * yet.  If num_mapped == num_segs, then all of the holes
601 		 * between segments would have already been unmapped.
602 		 */
603 
604 		for (i = 1; i < num_mapped; i++) {
605 			addr = mrp[i - 1].mr_addr + mrp[i - 1].mr_msize;
606 			size = mrp[i].mr_addr - addr;
607 			(void) as_unmap(as, addr, size);
608 		}
609 	}
610 }
611 
612 /*
613  * We need to add the start address into mrp so that the unmap function
614  * has absolute addresses to use.
615  */
616 static void
617 mmapobj_unmap_exec(mmapobj_result_t *mrp, int num_mapped, caddr_t start_addr)
618 {
619 	int i;
620 
621 	for (i = 0; i < num_mapped; i++) {
622 		mrp[i].mr_addr += (size_t)start_addr;
623 	}
624 	mmapobj_unmap(mrp, num_mapped, num_mapped, ET_EXEC);
625 }
626 
627 static caddr_t
628 mmapobj_lookup_start_addr(struct lib_va *lvp)
629 {
630 	struct as *as = curproc->p_as;
631 	struct segvn_crargs crargs = SEGVN_ZFOD_ARGS(PROT_USER, PROT_ALL);
632 	int error;
633 	uint_t ma_flags = _MAP_LOW32;
634 	caddr_t base = NULL;
635 	size_t len;
636 	size_t align;
637 
638 	ASSERT(lvp != NULL);
639 	MOBJ_STAT_ADD(lookup_start);
640 
641 	as_rangelock(as);
642 
643 	base = lvp->lv_base_va;
644 	len = lvp->lv_len;
645 
646 	/*
647 	 * If we don't have an expected base address, or the one that we want
648 	 * to use is not available or acceptable, go get an acceptable
649 	 * address range.
650 	 */
651 	if (base == NULL || as_gap(as, len, &base, &len, 0, NULL) ||
652 	    valid_usr_range(base, len, PROT_ALL, as, as->a_userlimit) !=
653 	    RANGE_OKAY) {
654 
655 		if (lvp->lv_flags & LV_ELF64) {
656 			ma_flags = 0;
657 		}
658 
659 		align = lvp->lv_align;
660 		if (align > 1) {
661 			ma_flags |= MAP_ALIGN;
662 		}
663 
664 		base = (caddr_t)align;
665 		map_addr(&base, len, 0, 1, ma_flags);
666 	}
667 
668 	/*
669 	 * Need to reserve the address space we're going to use.
670 	 * Don't reserve swap space since we'll be mapping over this.
671 	 */
672 	if (base != NULL) {
673 		crargs.flags |= MAP_NORESERVE;
674 		error = as_map(as, base, len, segvn_create, &crargs);
675 		if (error) {
676 			base = NULL;
677 		}
678 	}
679 
680 	as_rangeunlock(as);
681 	return (base);
682 }
683 
684 /*
685  * Get the starting address for a given file to be mapped and return it
686  * to the caller.  If we're using lib_va and we need to allocate an address,
687  * we will attempt to allocate it from the global reserved pool such that the
688  * same address can be used in the future for this file.  If we can't use the
689  * reserved address then we just get one that will fit in our address space.
690  *
691  * Returns the starting virtual address for the range to be mapped or NULL
692  * if an error is encountered. If we successfully insert the requested info
693  * into the lib_va hash, then *lvpp will be set to point to this lib_va
694  * structure.  The structure will have a hold on it and thus lib_va_release
695  * needs to be called on it by the caller.  This function will not fill out
696  * lv_mps or lv_num_segs since it does not have enough information to do so.
697  * The caller is responsible for doing this making sure that any modifications
698  * to lv_mps are visible before setting lv_num_segs.
699  */
700 static caddr_t
701 mmapobj_alloc_start_addr(struct lib_va **lvpp, size_t len, int use_lib_va,
702     size_t align, vattr_t *vap)
703 {
704 	struct as *as = curproc->p_as;
705 	struct segvn_crargs crargs = SEGVN_ZFOD_ARGS(PROT_USER, PROT_ALL);
706 	int error;
707 	model_t model;
708 	uint_t ma_flags = _MAP_LOW32;
709 	caddr_t base = NULL;
710 	vmem_t *model_vmem;
711 	size_t lib_va_start;
712 	size_t lib_va_end;
713 	size_t lib_va_len;
714 
715 	ASSERT(lvpp != NULL);
716 
717 	MOBJ_STAT_ADD(alloc_start);
718 	model = get_udatamodel();
719 
720 	if (model == DATAMODEL_LP64) {
721 		ma_flags = 0;
722 		model_vmem = lib_va_64_arena;
723 	} else {
724 		ASSERT(model == DATAMODEL_ILP32);
725 		model_vmem = lib_va_32_arena;
726 	}
727 
728 	if (align > 1) {
729 		ma_flags |= MAP_ALIGN;
730 	}
731 	if (use_lib_va) {
732 		/*
733 		 * The first time through, we need to setup the lib_va arenas.
734 		 * We call map_addr to find a suitable range of memory to map
735 		 * the given library, and we will set the highest address
736 		 * in our vmem arena to the end of this adddress range.
737 		 * We allow up to half of the address space to be used
738 		 * for lib_va addresses but we do not prevent any allocations
739 		 * in this range from other allocation paths.
740 		 */
741 		if (lib_va_64_arena == NULL && model == DATAMODEL_LP64) {
742 			mutex_enter(&lib_va_init_mutex);
743 			if (lib_va_64_arena == NULL) {
744 				base = (caddr_t)align;
745 				as_rangelock(as);
746 				map_addr(&base, len, 0, 1, ma_flags);
747 				as_rangeunlock(as);
748 				if (base == NULL) {
749 					mutex_exit(&lib_va_init_mutex);
750 					MOBJ_STAT_ADD(lib_va_create_failure);
751 					goto nolibva;
752 				}
753 				lib_va_end = (size_t)base + len;
754 				lib_va_len = lib_va_end >> 1;
755 				lib_va_len = P2ROUNDUP(lib_va_len, PAGESIZE);
756 				lib_va_start = lib_va_end - lib_va_len;
757 
758 				/*
759 			 	 * Need to make sure we avoid the address hole.
760 				 * We know lib_va_end is valid but we need to
761 				 * make sure lib_va_start is as well.
762 				 */
763 				if ((lib_va_end > (size_t)hole_end) &&
764 				    (lib_va_start < (size_t)hole_end)) {
765 					lib_va_start = P2ROUNDUP(
766 					   (size_t)hole_end, PAGESIZE);
767 					lib_va_len = lib_va_end - lib_va_start;
768 				}
769 				lib_va_64_arena = vmem_create("lib_va_64",
770 				    (void *)lib_va_start, lib_va_len, PAGESIZE,
771 				    NULL, NULL, NULL, 0,
772 				    VM_NOSLEEP | VMC_IDENTIFIER);
773 				if (lib_va_64_arena == NULL) {
774 					mutex_exit(&lib_va_init_mutex);
775 					goto nolibva;
776 				}
777 			}
778 			model_vmem = lib_va_64_arena;
779 			mutex_exit(&lib_va_init_mutex);
780 		} else if (lib_va_32_arena == NULL &&
781 		    model == DATAMODEL_ILP32) {
782 			mutex_enter(&lib_va_init_mutex);
783 			if (lib_va_32_arena == NULL) {
784 				base = (caddr_t)align;
785 				as_rangelock(as);
786 				map_addr(&base, len, 0, 1, ma_flags);
787 				as_rangeunlock(as);
788 				if (base == NULL) {
789 					mutex_exit(&lib_va_init_mutex);
790 					MOBJ_STAT_ADD(lib_va_create_failure);
791 					goto nolibva;
792 				}
793 				lib_va_end = (size_t)base + len;
794 				lib_va_len = lib_va_end >> 1;
795 				lib_va_len = P2ROUNDUP(lib_va_len, PAGESIZE);
796 				lib_va_start = lib_va_end - lib_va_len;
797 				lib_va_32_arena = vmem_create("lib_va_32",
798 				    (void *)lib_va_start, lib_va_len, PAGESIZE,
799 				    NULL, NULL, NULL, 0,
800 				    VM_NOSLEEP | VMC_IDENTIFIER);
801 				if (lib_va_32_arena == NULL) {
802 					mutex_exit(&lib_va_init_mutex);
803 					goto nolibva;
804 				}
805 			}
806 			model_vmem = lib_va_32_arena;
807 			mutex_exit(&lib_va_init_mutex);
808 		}
809 
810 		if (model == DATAMODEL_LP64 || libs_mapped_32 < lib_threshold) {
811 			base = vmem_xalloc(model_vmem, len, align, 0, 0, NULL,
812 			    NULL, VM_NOSLEEP | VM_ENDALLOC);
813 			MOBJ_STAT_ADD(alloc_vmem);
814 		}
815 
816 		/*
817 		 * Even if the address fails to fit in our address space,
818 		 * or we can't use a reserved address,
819 		 * we should still save it off in lib_va_hash.
820 		 */
821 		*lvpp = lib_va_add_hash(base, len, align, vap);
822 
823 		/*
824 		 * Check for collision on insertion and free up our VA space.
825 		 * This is expected to be rare, so we'll just reset base to
826 		 * NULL instead of looking it up in the lib_va hash.
827 		 */
828 		if (*lvpp == NULL) {
829 			if (base != NULL) {
830 				vmem_xfree(model_vmem, base, len);
831 				base = NULL;
832 				MOBJ_STAT_ADD(add_collision);
833 			}
834 		}
835 	}
836 
837 nolibva:
838 	as_rangelock(as);
839 
840 	/*
841 	 * If we don't have an expected base address, or the one that we want
842 	 * to use is not available or acceptable, go get an acceptable
843 	 * address range.
844 	 */
845 	if (base == NULL || as_gap(as, len, &base, &len, 0, NULL) ||
846 	    valid_usr_range(base, len, PROT_ALL, as, as->a_userlimit) !=
847 	    RANGE_OKAY) {
848 		MOBJ_STAT_ADD(get_addr);
849 		base = (caddr_t)align;
850 		map_addr(&base, len, 0, 1, ma_flags);
851 	}
852 
853 	/*
854 	 * Need to reserve the address space we're going to use.
855 	 * Don't reserve swap space since we'll be mapping over this.
856 	 */
857 	if (base != NULL) {
858 		/* Don't reserve swap space since we'll be mapping over this */
859 		crargs.flags |= MAP_NORESERVE;
860 		error = as_map(as, base, len, segvn_create, &crargs);
861 		if (error) {
862 			base = NULL;
863 		}
864 	}
865 
866 	as_rangeunlock(as);
867 	return (base);
868 }
869 
870 /*
871  * Map the file associated with vp into the address space as a single
872  * read only private mapping.
873  * Returns 0 for success, and non-zero for failure to map the file.
874  */
875 static int
876 mmapobj_map_flat(vnode_t *vp, mmapobj_result_t *mrp, size_t padding,
877     cred_t *fcred)
878 {
879 	int error = 0;
880 	struct as *as = curproc->p_as;
881 	caddr_t addr = NULL;
882 	caddr_t start_addr;
883 	size_t len;
884 	size_t pad_len;
885 	int prot = PROT_USER | PROT_READ;
886 	uint_t ma_flags = _MAP_LOW32;
887 	vattr_t vattr;
888 	struct segvn_crargs crargs = SEGVN_ZFOD_ARGS(PROT_USER, PROT_ALL);
889 
890 	if (get_udatamodel() == DATAMODEL_LP64) {
891 		ma_flags = 0;
892 	}
893 
894 	vattr.va_mask = AT_SIZE;
895 	error = VOP_GETATTR(vp, &vattr, 0, fcred, NULL);
896 	if (error) {
897 		return (error);
898 	}
899 
900 	len = vattr.va_size;
901 
902 	ma_flags |= MAP_PRIVATE;
903 	if (padding == 0) {
904 		MOBJ_STAT_ADD(map_flat_no_padding);
905 		error = VOP_MAP(vp, 0, as, &addr, len, prot, PROT_ALL,
906 		    ma_flags, fcred, NULL);
907 		if (error == 0) {
908 			mrp[0].mr_addr = addr;
909 			mrp[0].mr_msize = len;
910 			mrp[0].mr_fsize = len;
911 			mrp[0].mr_offset = 0;
912 			mrp[0].mr_prot = prot;
913 			mrp[0].mr_flags = 0;
914 		}
915 		return (error);
916 	}
917 
918 	/* padding was requested so there's more work to be done */
919 	MOBJ_STAT_ADD(map_flat_padding);
920 
921 	/* No need to reserve swap space now since it will be reserved later */
922 	crargs.flags |= MAP_NORESERVE;
923 
924 	/* Need to setup padding which can only be in PAGESIZE increments. */
925 	ASSERT((padding & PAGEOFFSET) == 0);
926 	pad_len = len + (2 * padding);
927 
928 	as_rangelock(as);
929 	map_addr(&addr, pad_len, 0, 1, ma_flags);
930 	error = as_map(as, addr, pad_len, segvn_create, &crargs);
931 	as_rangeunlock(as);
932 	if (error) {
933 		return (error);
934 	}
935 	start_addr = addr;
936 	addr += padding;
937 	ma_flags |= MAP_FIXED;
938 	error = VOP_MAP(vp, 0, as, &addr, len, prot, PROT_ALL, ma_flags,
939 	    fcred, NULL);
940 	if (error == 0) {
941 		mrp[0].mr_addr = start_addr;
942 		mrp[0].mr_msize = padding;
943 		mrp[0].mr_fsize = 0;
944 		mrp[0].mr_offset = 0;
945 		mrp[0].mr_prot = 0;
946 		mrp[0].mr_flags = MR_PADDING;
947 
948 		mrp[1].mr_addr = addr;
949 		mrp[1].mr_msize = len;
950 		mrp[1].mr_fsize = len;
951 		mrp[1].mr_offset = 0;
952 		mrp[1].mr_prot = prot;
953 		mrp[1].mr_flags = 0;
954 
955 		mrp[2].mr_addr = addr + P2ROUNDUP(len, PAGESIZE);
956 		mrp[2].mr_msize = padding;
957 		mrp[2].mr_fsize = 0;
958 		mrp[2].mr_offset = 0;
959 		mrp[2].mr_prot = 0;
960 		mrp[2].mr_flags = MR_PADDING;
961 	} else {
962 		/* Need to cleanup the as_map from earlier */
963 		(void) as_unmap(as, start_addr, pad_len);
964 	}
965 	return (error);
966 }
967 
968 /*
969  * Map a PT_LOAD or PT_SUNWBSS section of an executable file into the user's
970  * address space.
971  * vp - vnode to be mapped in
972  * addr - start address
973  * len - length of vp to be mapped
974  * zfodlen - length of zero filled memory after len above
975  * offset - offset into file where mapping should start
976  * prot - protections for this mapping
977  * fcred - credentials for the file associated with vp at open time.
978  */
979 static int
980 mmapobj_map_ptload(struct vnode *vp, caddr_t addr, size_t len, size_t zfodlen,
981     off_t offset, int prot, cred_t *fcred)
982 {
983 	int error = 0;
984 	caddr_t zfodbase, oldaddr;
985 	size_t oldlen;
986 	size_t end;
987 	size_t zfoddiff;
988 	label_t ljb;
989 	struct as *as = curproc->p_as;
990 	model_t model;
991 	int full_page;
992 
993 	/*
994 	 * See if addr and offset are aligned such that we can map in
995 	 * full pages instead of partial pages.
996 	 */
997 	full_page = (((uintptr_t)addr & PAGEOFFSET) ==
998 	    ((uintptr_t)offset & PAGEOFFSET));
999 
1000 	model = get_udatamodel();
1001 
1002 	oldaddr = addr;
1003 	addr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
1004 	if (len) {
1005 		spgcnt_t availm, npages;
1006 		int preread;
1007 		uint_t mflag = MAP_PRIVATE | MAP_FIXED;
1008 
1009 		if (model == DATAMODEL_ILP32) {
1010 			mflag |= _MAP_LOW32;
1011 		}
1012 		/* We may need to map in extra bytes */
1013 		oldlen = len;
1014 		len += ((size_t)oldaddr & PAGEOFFSET);
1015 
1016 		if (full_page) {
1017 			offset = (off_t)((uintptr_t)offset & PAGEMASK);
1018 			if ((prot & (PROT_WRITE | PROT_EXEC)) == PROT_EXEC) {
1019 				mflag |= MAP_TEXT;
1020 				MOBJ_STAT_ADD(map_ptload_text);
1021 			} else {
1022 				mflag |= MAP_INITDATA;
1023 				MOBJ_STAT_ADD(map_ptload_initdata);
1024 			}
1025 
1026 			/*
1027 			 * maxprot is passed as PROT_ALL so that mdb can
1028 			 * write to this segment.
1029 			 */
1030 			if (error = VOP_MAP(vp, (offset_t)offset, as, &addr,
1031 			    len, prot, PROT_ALL, mflag, fcred, NULL)) {
1032 				return (error);
1033 			}
1034 
1035 			/*
1036 			 * If the segment can fit and is relatively small, then
1037 			 * we prefault the entire segment in.  This is based
1038 			 * on the model that says the best working set of a
1039 			 * small program is all of its pages.
1040 			 * We only do this if freemem will not drop below
1041 			 * lotsfree since we don't want to induce paging.
1042 			 */
1043 			npages = (spgcnt_t)btopr(len);
1044 			availm = freemem - lotsfree;
1045 			preread = (npages < availm && len < PGTHRESH) ? 1 : 0;
1046 
1047 			/*
1048 			 * If we aren't prefaulting the segment,
1049 			 * increment "deficit", if necessary to ensure
1050 			 * that pages will become available when this
1051 			 * process starts executing.
1052 			 */
1053 			if (preread == 0 && npages > availm &&
1054 			    deficit < lotsfree) {
1055 				deficit += MIN((pgcnt_t)(npages - availm),
1056 				    lotsfree - deficit);
1057 			}
1058 
1059 			if (preread) {
1060 				(void) as_faulta(as, addr, len);
1061 				MOBJ_STAT_ADD(map_ptload_preread);
1062 			}
1063 		} else {
1064 			/*
1065 			 * addr and offset were not aligned such that we could
1066 			 * use VOP_MAP, thus we need to as_map the memory we
1067 			 * need and then read the data in from disk.
1068 			 * This code path is a corner case which should never
1069 			 * be taken, but hand crafted binaries could trigger
1070 			 * this logic and it needs to work correctly.
1071 			 */
1072 			MOBJ_STAT_ADD(map_ptload_unaligned_text);
1073 			as_rangelock(as);
1074 			(void) as_unmap(as, addr, len);
1075 
1076 			/*
1077 			 * We use zfod_argsp because we need to be able to
1078 			 * write to the mapping and then we'll change the
1079 			 * protections later if they are incorrect.
1080 			 */
1081 			error = as_map(as, addr, len, segvn_create, zfod_argsp);
1082 			as_rangeunlock(as);
1083 			if (error) {
1084 				MOBJ_STAT_ADD(map_ptload_unaligned_map_fail);
1085 				return (error);
1086 			}
1087 
1088 			/* Now read in the data from disk */
1089 			error = vn_rdwr(UIO_READ, vp, oldaddr, oldlen, offset,
1090 			    UIO_USERSPACE, 0, (rlim64_t)0, fcred, NULL);
1091 			if (error) {
1092 				MOBJ_STAT_ADD(map_ptload_unaligned_read_fail);
1093 				return (error);
1094 			}
1095 
1096 			/*
1097 			 * Now set protections.
1098 			 */
1099 			if (prot != PROT_ZFOD) {
1100 				(void) as_setprot(as, addr, len, prot);
1101 			}
1102 		}
1103 	}
1104 
1105 	if (zfodlen) {
1106 		end = (size_t)addr + len;
1107 		zfodbase = (caddr_t)P2ROUNDUP(end, PAGESIZE);
1108 		zfoddiff = (uintptr_t)zfodbase - end;
1109 		if (zfoddiff) {
1110 			MOBJ_STAT_ADD(zfoddiff);
1111 			if ((prot & PROT_WRITE) == 0) {
1112 				(void) as_setprot(as, (caddr_t)end,
1113 				    zfoddiff, prot | PROT_WRITE);
1114 				MOBJ_STAT_ADD(zfoddiff_nowrite);
1115 			}
1116 			if (on_fault(&ljb)) {
1117 				no_fault();
1118 				if ((prot & PROT_WRITE) == 0) {
1119 					(void) as_setprot(as, (caddr_t)end,
1120 					    zfoddiff, prot);
1121 				}
1122 				return (EFAULT);
1123 			}
1124 			uzero((void *)end, zfoddiff);
1125 			no_fault();
1126 
1127 			/*
1128 			 * Remove write protection to return to original state
1129 			 */
1130 			if ((prot & PROT_WRITE) == 0) {
1131 				(void) as_setprot(as, (caddr_t)end,
1132 				    zfoddiff, prot);
1133 			}
1134 		}
1135 		if (zfodlen > zfoddiff) {
1136 			struct segvn_crargs crargs =
1137 			    SEGVN_ZFOD_ARGS(prot, PROT_ALL);
1138 
1139 			MOBJ_STAT_ADD(zfodextra);
1140 			zfodlen -= zfoddiff;
1141 			crargs.szc = AS_MAP_NO_LPOOB;
1142 
1143 
1144 			as_rangelock(as);
1145 			(void) as_unmap(as, (caddr_t)zfodbase, zfodlen);
1146 			error = as_map(as, (caddr_t)zfodbase,
1147 			    zfodlen, segvn_create, &crargs);
1148 			as_rangeunlock(as);
1149 			if (error) {
1150 				return (error);
1151 			}
1152 		}
1153 	}
1154 	return (0);
1155 }
1156 
1157 /*
1158  * Map the ELF file represented by vp into the users address space.  The
1159  * first mapping will start at start_addr and there will be num_elements
1160  * mappings.  The mappings are described by the data in mrp which may be
1161  * modified upon returning from this function.
1162  * Returns 0 for success or errno for failure.
1163  */
1164 static int
1165 mmapobj_map_elf(struct vnode *vp, caddr_t start_addr, mmapobj_result_t *mrp,
1166     int num_elements, cred_t *fcred, ushort_t e_type)
1167 {
1168 	int i;
1169 	int ret;
1170 	caddr_t lo;
1171 	caddr_t hi;
1172 	struct as *as = curproc->p_as;
1173 
1174 	for (i = 0; i < num_elements; i++) {
1175 		caddr_t addr;
1176 		size_t p_memsz;
1177 		size_t p_filesz;
1178 		size_t zfodlen;
1179 		offset_t p_offset;
1180 		size_t dif;
1181 		int prot;
1182 
1183 		/* Always need to adjust mr_addr */
1184 		addr = start_addr + (size_t)(mrp[i].mr_addr);
1185 		mrp[i].mr_addr =
1186 		    (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
1187 
1188 		/* Padding has already been mapped */
1189 		if (MR_GET_TYPE(mrp[i].mr_flags) == MR_PADDING) {
1190 			continue;
1191 		}
1192 		p_memsz = mrp[i].mr_msize;
1193 		p_filesz = mrp[i].mr_fsize;
1194 		zfodlen = p_memsz - p_filesz;
1195 		p_offset = mrp[i].mr_offset;
1196 		dif = (uintptr_t)(addr) & PAGEOFFSET;
1197 		prot = mrp[i].mr_prot | PROT_USER;
1198 		ret = mmapobj_map_ptload(vp, addr, p_filesz, zfodlen,
1199 		    p_offset, prot, fcred);
1200 		if (ret != 0) {
1201 			MOBJ_STAT_ADD(ptload_failed);
1202 			mmapobj_unmap(mrp, i, num_elements, e_type);
1203 			return (ret);
1204 		}
1205 
1206 		/* Need to cleanup mrp to reflect the actual values used */
1207 		mrp[i].mr_msize += dif;
1208 		mrp[i].mr_offset = (size_t)addr & PAGEOFFSET;
1209 	}
1210 
1211 	/* Also need to unmap any holes created above */
1212 	if (num_elements == 1) {
1213 		MOBJ_STAT_ADD(map_elf_no_holes);
1214 		return (0);
1215 	}
1216 	if (e_type == ET_EXEC) {
1217 		return (0);
1218 	}
1219 
1220 	as_rangelock(as);
1221 	lo = start_addr;
1222 	hi = mrp[0].mr_addr;
1223 
1224 	/* Remove holes made by the rest of the segments */
1225 	for (i = 0; i < num_elements - 1; i++) {
1226 		lo = (caddr_t)P2ROUNDUP((size_t)(mrp[i].mr_addr) +
1227 		    mrp[i].mr_msize, PAGESIZE);
1228 		hi = mrp[i + 1].mr_addr;
1229 		if (lo < hi) {
1230 			/*
1231 			 * If as_unmap fails we just use up a bit of extra
1232 			 * space
1233 			 */
1234 			(void) as_unmap(as, (caddr_t)lo,
1235 			    (size_t)hi - (size_t)lo);
1236 			MOBJ_STAT_ADD(unmap_hole);
1237 		}
1238 	}
1239 	as_rangeunlock(as);
1240 
1241 	return (0);
1242 }
1243 
1244 /* Ugly hack to get STRUCT_* macros to work below */
1245 struct myphdr {
1246 	Phdr		x;	/* native version */
1247 };
1248 
1249 struct myphdr32 {
1250 	Elf32_Phdr	x;
1251 };
1252 
1253 /*
1254  * Calculate and return the number of loadable segments in the ELF Phdr
1255  * represented by phdrbase as well as the len of the total mapping and
1256  * the max alignment that is needed for a given segment.  On success,
1257  * 0 is returned, and *len, *loadable and *align have been filled out.
1258  * On failure, errno will be returned, which in this case is ENOTSUP
1259  * if we were passed an ELF file with overlapping segments.
1260  */
1261 static int
1262 calc_loadable(Ehdr *ehdrp, caddr_t phdrbase, int nphdrs, size_t *len,
1263     int *loadable, size_t *align)
1264 {
1265 	int i;
1266 	int hsize;
1267 	model_t model;
1268 	ushort_t e_type = ehdrp->e_type;	/* same offset 32 and 64 bit */
1269 	uint_t p_type;
1270 	offset_t p_offset;
1271 	size_t p_memsz;
1272 	size_t p_align;
1273 	caddr_t vaddr;
1274 	int num_segs = 0;
1275 	caddr_t start_addr = NULL;
1276 	caddr_t p_end = NULL;
1277 	size_t max_align = 0;
1278 	size_t min_align = PAGESIZE;	/* needed for vmem_xalloc */
1279 	STRUCT_HANDLE(myphdr, mph);
1280 #if defined(__sparc)
1281 	extern int vac_size;
1282 
1283 	/*
1284 	 * Want to prevent aliasing by making the start address at least be
1285 	 * aligned to vac_size.
1286 	 */
1287 	min_align = MAX(PAGESIZE, vac_size);
1288 #endif
1289 
1290 	model = get_udatamodel();
1291 	STRUCT_SET_HANDLE(mph, model, (struct myphdr *)phdrbase);
1292 
1293 	/* hsize alignment should have been checked before calling this func */
1294 	if (model == DATAMODEL_LP64) {
1295 		hsize = ehdrp->e_phentsize;
1296 		if (hsize & 7) {
1297 			return (ENOTSUP);
1298 		}
1299 	} else {
1300 		ASSERT(model == DATAMODEL_ILP32);
1301 		hsize = ((Elf32_Ehdr *)ehdrp)->e_phentsize;
1302 		if (hsize & 3) {
1303 			return (ENOTSUP);
1304 		}
1305 	}
1306 
1307 	/*
1308 	 * Determine the span of all loadable segments and calculate the
1309 	 * number of loadable segments.
1310 	 */
1311 	for (i = 0; i < nphdrs; i++) {
1312 		p_type = STRUCT_FGET(mph, x.p_type);
1313 		if (p_type == PT_LOAD || p_type == PT_SUNWBSS) {
1314 			vaddr = (caddr_t)(uintptr_t)STRUCT_FGET(mph, x.p_vaddr);
1315 			p_memsz = STRUCT_FGET(mph, x.p_memsz);
1316 
1317 			/*
1318 			 * Skip this header if it requests no memory to be
1319 			 * mapped.
1320 			 */
1321 			if (p_memsz == 0) {
1322 				STRUCT_SET_HANDLE(mph, model,
1323 				    (struct myphdr *)((size_t)STRUCT_BUF(mph) +
1324 				    hsize));
1325 				MOBJ_STAT_ADD(nomem_header);
1326 				continue;
1327 			}
1328 			if (num_segs++ == 0) {
1329 				/*
1330 				 * The p_vaddr of the first PT_LOAD segment
1331 				 * must either be NULL or within the first
1332 				 * page in order to be interpreted.
1333 				 * Otherwise, its an invalid file.
1334 				 */
1335 				if (e_type == ET_DYN &&
1336 				    ((caddr_t)((uintptr_t)vaddr &
1337 				    (uintptr_t)PAGEMASK) != NULL)) {
1338 					MOBJ_STAT_ADD(inval_header);
1339 					return (ENOTSUP);
1340 				}
1341 				start_addr = vaddr;
1342 				/*
1343 				 * For the first segment, we need to map from
1344 				 * the beginning of the file, so we will
1345 				 * adjust the size of the mapping to include
1346 				 * this memory.
1347 				 */
1348 				p_offset = STRUCT_FGET(mph, x.p_offset);
1349 			} else {
1350 				p_offset = 0;
1351 			}
1352 			/*
1353 			 * Check to make sure that this mapping wouldn't
1354 			 * overlap a previous mapping.
1355 			 */
1356 			if (vaddr < p_end) {
1357 				MOBJ_STAT_ADD(overlap_header);
1358 				return (ENOTSUP);
1359 			}
1360 
1361 			p_end = vaddr + p_memsz + p_offset;
1362 			p_end = (caddr_t)P2ROUNDUP((size_t)p_end, PAGESIZE);
1363 
1364 			p_align = STRUCT_FGET(mph, x.p_align);
1365 			if (p_align > 1 && p_align > max_align) {
1366 				max_align = p_align;
1367 				if (max_align < min_align) {
1368 					max_align = min_align;
1369 					MOBJ_STAT_ADD(min_align);
1370 				}
1371 			}
1372 		}
1373 		STRUCT_SET_HANDLE(mph, model,
1374 		    (struct myphdr *)((size_t)STRUCT_BUF(mph) + hsize));
1375 	}
1376 
1377 	/*
1378 	 * The alignment should be a power of 2, if it isn't we forgive it
1379 	 * and round up.  On overflow, we'll set the alignment to max_align
1380 	 * rounded down to the nearest power of 2.
1381 	 */
1382 	if (max_align > 0 && !ISP2(max_align)) {
1383 		MOBJ_STAT_ADD(np2_align);
1384 		*align = 2 * (1L << (highbit(max_align) - 1));
1385 		if (*align < max_align ||
1386 		    (*align > UINT_MAX && model == DATAMODEL_ILP32)) {
1387 			MOBJ_STAT_ADD(np2_align_overflow);
1388 			*align = 1L << (highbit(max_align) - 1);
1389 		}
1390 	} else {
1391 		*align = max_align;
1392 	}
1393 
1394 	ASSERT(*align >= PAGESIZE || *align == 0);
1395 
1396 	*loadable = num_segs;
1397 	*len = p_end - start_addr;
1398 	return (0);
1399 }
1400 
1401 /*
1402  * Check the address space to see if the virtual addresses to be used are
1403  * available.  If they are not, return errno for failure.  On success, 0
1404  * will be returned, and the virtual addresses for each mmapobj_result_t
1405  * will be reserved.  Note that a reservation could have earlier been made
1406  * for a given segment via a /dev/null mapping.  If that is the case, then
1407  * we can use that VA space for our mappings.
1408  * Note: this function will only be used for ET_EXEC binaries.
1409  */
1410 int
1411 check_exec_addrs(int loadable, mmapobj_result_t *mrp, caddr_t start_addr)
1412 {
1413 	int i;
1414 	struct as *as = curproc->p_as;
1415 	struct segvn_crargs crargs = SEGVN_ZFOD_ARGS(PROT_ZFOD, PROT_ALL);
1416 	int ret;
1417 	caddr_t myaddr;
1418 	size_t mylen;
1419 	struct seg *seg;
1420 
1421 	/* No need to reserve swap space now since it will be reserved later */
1422 	crargs.flags |= MAP_NORESERVE;
1423 	as_rangelock(as);
1424 	for (i = 0; i < loadable; i++) {
1425 
1426 		myaddr = start_addr + (size_t)mrp[i].mr_addr;
1427 		mylen = mrp[i].mr_msize;
1428 
1429 		/* See if there is a hole in the as for this range */
1430 		if (as_gap(as, mylen, &myaddr, &mylen, 0, NULL) == 0) {
1431 			ASSERT(myaddr == start_addr + (size_t)mrp[i].mr_addr);
1432 			ASSERT(mylen == mrp[i].mr_msize);
1433 
1434 #ifdef DEBUG
1435 			if (MR_GET_TYPE(mrp[i].mr_flags) == MR_PADDING) {
1436 				MOBJ_STAT_ADD(exec_padding);
1437 			}
1438 #endif
1439 			ret = as_map(as, myaddr, mylen, segvn_create, &crargs);
1440 			if (ret) {
1441 				as_rangeunlock(as);
1442 				mmapobj_unmap_exec(mrp, i, start_addr);
1443 				return (ret);
1444 			}
1445 		} else {
1446 			/*
1447 			 * There is a mapping that exists in the range
1448 			 * so check to see if it was a "reservation"
1449 			 * from /dev/null.  The mapping is from
1450 			 * /dev/null if the mapping comes from
1451 			 * segdev and the type is neither MAP_SHARED
1452 			 * nor MAP_PRIVATE.
1453 			 */
1454 			AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
1455 			seg = as_findseg(as, myaddr, 0);
1456 			MOBJ_STAT_ADD(exec_addr_mapped);
1457 			if (seg && seg->s_ops == &segdev_ops &&
1458 			    ((SEGOP_GETTYPE(seg, myaddr) &
1459 			    (MAP_SHARED | MAP_PRIVATE)) == 0) &&
1460 			    myaddr >= seg->s_base &&
1461 			    myaddr + mylen <=
1462 			    seg->s_base + seg->s_size) {
1463 				MOBJ_STAT_ADD(exec_addr_devnull);
1464 				AS_LOCK_EXIT(as, &as->a_lock);
1465 				(void) as_unmap(as, myaddr, mylen);
1466 				ret = as_map(as, myaddr, mylen, segvn_create,
1467 				    &crargs);
1468 				mrp[i].mr_flags |= MR_RESV;
1469 				if (ret) {
1470 					as_rangeunlock(as);
1471 					/* Need to remap what we unmapped */
1472 					mmapobj_unmap_exec(mrp, i + 1,
1473 					    start_addr);
1474 					return (ret);
1475 				}
1476 			} else {
1477 				AS_LOCK_EXIT(as, &as->a_lock);
1478 				as_rangeunlock(as);
1479 				mmapobj_unmap_exec(mrp, i, start_addr);
1480 				MOBJ_STAT_ADD(exec_addr_in_use);
1481 				return (EADDRINUSE);
1482 			}
1483 		}
1484 	}
1485 	as_rangeunlock(as);
1486 	return (0);
1487 }
1488 
1489 /*
1490  * Walk through the ELF program headers and extract all useful information
1491  * for PT_LOAD and PT_SUNWBSS segments into mrp.
1492  * Return 0 on success or error on failure.
1493  */
1494 static int
1495 process_phdr(Ehdr *ehdrp, caddr_t phdrbase, int nphdrs, mmapobj_result_t *mrp,
1496     vnode_t *vp, uint_t *num_mapped, size_t padding, cred_t *fcred)
1497 {
1498 	int i;
1499 	caddr_t start_addr = NULL;
1500 	caddr_t vaddr;
1501 	size_t len = 0;
1502 	size_t lib_len = 0;
1503 	int ret;
1504 	int prot;
1505 	struct lib_va *lvp = NULL;
1506 	vattr_t vattr;
1507 	struct as *as = curproc->p_as;
1508 	int error;
1509 	int loadable = 0;
1510 	int current = 0;
1511 	int use_lib_va = 1;
1512 	size_t align = 0;
1513 	size_t add_pad = 0;
1514 	int hdr_seen = 0;
1515 	ushort_t e_type = ehdrp->e_type;	/* same offset 32 and 64 bit */
1516 	uint_t p_type;
1517 	offset_t p_offset;
1518 	size_t p_memsz;
1519 	size_t p_filesz;
1520 	uint_t p_flags;
1521 	int hsize;
1522 	model_t model;
1523 	STRUCT_HANDLE(myphdr, mph);
1524 
1525 	model = get_udatamodel();
1526 	STRUCT_SET_HANDLE(mph, model, (struct myphdr *)phdrbase);
1527 
1528 	/*
1529 	 * Need to make sure that hsize is aligned properly.
1530 	 * For 32bit processes, 4 byte alignment is required.
1531 	 * For 64bit processes, 8 byte alignment is required.
1532 	 * If the alignment isn't correct, we need to return failure
1533 	 * since it could cause an alignment error panic while walking
1534 	 * the phdr array.
1535 	 */
1536 	if (model == DATAMODEL_LP64) {
1537 		hsize = ehdrp->e_phentsize;
1538 		if (hsize & 7) {
1539 			MOBJ_STAT_ADD(phent_align64);
1540 			return (ENOTSUP);
1541 		}
1542 	} else {
1543 		ASSERT(model == DATAMODEL_ILP32);
1544 		hsize = ((Elf32_Ehdr *)ehdrp)->e_phentsize;
1545 		if (hsize & 3) {
1546 			MOBJ_STAT_ADD(phent_align32);
1547 			return (ENOTSUP);
1548 		}
1549 	}
1550 
1551 	if (padding != 0) {
1552 		use_lib_va = 0;
1553 	}
1554 	if (e_type == ET_DYN) {
1555 		vattr.va_mask = AT_FSID | AT_NODEID | AT_CTIME | AT_MTIME;
1556 		error = VOP_GETATTR(vp, &vattr, 0, fcred, NULL);
1557 		if (error) {
1558 			return (error);
1559 		}
1560 		/* Check to see if we already have a description for this lib */
1561 		lvp = lib_va_find(&vattr);
1562 
1563 		if (lvp != NULL) {
1564 			MOBJ_STAT_ADD(lvp_found);
1565 			if (use_lib_va) {
1566 				start_addr = mmapobj_lookup_start_addr(lvp);
1567 				if (start_addr == NULL) {
1568 					lib_va_release(lvp);
1569 					return (ENOMEM);
1570 				}
1571 			}
1572 
1573 			/*
1574 			 * loadable may be zero if the original allocator
1575 			 * of lvp hasn't finished setting it up but the rest
1576 			 * of the fields will be accurate.
1577 			 */
1578 			loadable = lvp->lv_num_segs;
1579 			len = lvp->lv_len;
1580 			align = lvp->lv_align;
1581 		}
1582 	}
1583 
1584 	/*
1585 	 * Determine the span of all loadable segments and calculate the
1586 	 * number of loadable segments, the total len spanned by the mappings
1587 	 * and the max alignment, if we didn't get them above.
1588 	 */
1589 	if (loadable == 0) {
1590 		MOBJ_STAT_ADD(no_loadable_yet);
1591 		ret = calc_loadable(ehdrp, phdrbase, nphdrs, &len,
1592 		    &loadable, &align);
1593 		if (ret != 0) {
1594 			/*
1595 			 * Since it'd be an invalid file, we shouldn't have
1596 			 * cached it previously.
1597 			 */
1598 			ASSERT(lvp == NULL);
1599 			return (ret);
1600 		}
1601 #ifdef DEBUG
1602 		if (lvp) {
1603 			ASSERT(len == lvp->lv_len);
1604 			ASSERT(align == lvp->lv_align);
1605 		}
1606 #endif
1607 	}
1608 
1609 	/* Make sure there's something to map. */
1610 	if (len == 0 || loadable == 0) {
1611 		/*
1612 		 * Since it'd be an invalid file, we shouldn't have
1613 		 * cached it previously.
1614 		 */
1615 		ASSERT(lvp == NULL);
1616 		MOBJ_STAT_ADD(nothing_to_map);
1617 		return (ENOTSUP);
1618 	}
1619 
1620 	lib_len = len;
1621 	if (padding != 0) {
1622 		loadable += 2;
1623 	}
1624 	if (loadable > *num_mapped) {
1625 		*num_mapped = loadable;
1626 		/* cleanup previous reservation */
1627 		if (start_addr) {
1628 			(void) as_unmap(as, start_addr, lib_len);
1629 		}
1630 		MOBJ_STAT_ADD(e2big);
1631 		if (lvp) {
1632 			lib_va_release(lvp);
1633 		}
1634 		return (E2BIG);
1635 	}
1636 
1637 	/*
1638 	 * We now know the size of the object to map and now we need to
1639 	 * get the start address to map it at.  It's possible we already
1640 	 * have it if we found all the info we need in the lib_va cache.
1641 	 */
1642 	if (e_type == ET_DYN && start_addr == NULL) {
1643 		/*
1644 		 * Need to make sure padding does not throw off
1645 		 * required alignment.  We can only specify an
1646 		 * alignment for the starting address to be mapped,
1647 		 * so we round padding up to the alignment and map
1648 		 * from there and then throw out the extra later.
1649 		 */
1650 		if (padding != 0) {
1651 			if (align > 1) {
1652 				add_pad = P2ROUNDUP(padding, align);
1653 				len += add_pad;
1654 				MOBJ_STAT_ADD(dyn_pad_align);
1655 			} else {
1656 				MOBJ_STAT_ADD(dyn_pad_noalign);
1657 				len += padding;	/* at beginning */
1658 			}
1659 			len += padding;	/* at end of mapping */
1660 		}
1661 		/*
1662 		 * At this point, if lvp is non-NULL, then above we
1663 		 * already found it in the cache but did not get
1664 		 * the start address since we were not going to use lib_va.
1665 		 * Since we know that lib_va will not be used, it's safe
1666 		 * to call mmapobj_alloc_start_addr and know that lvp
1667 		 * will not be modified.
1668 		 */
1669 		ASSERT(lvp ? use_lib_va == 0 : 1);
1670 		start_addr = mmapobj_alloc_start_addr(&lvp, len,
1671 		    use_lib_va, align, &vattr);
1672 		if (start_addr == NULL) {
1673 			if (lvp) {
1674 				lib_va_release(lvp);
1675 			}
1676 			MOBJ_STAT_ADD(alloc_start_fail);
1677 			return (ENOMEM);
1678 		}
1679 		/*
1680 		 * If we can't cache it, no need to hang on to it.
1681 		 * Setting lv_num_segs to non-zero will make that
1682 		 * field active and since there are too many segments
1683 		 * to cache, all future users will not try to use lv_mps.
1684 		 */
1685 		if (lvp != NULL && loadable > LIBVA_CACHED_SEGS && use_lib_va) {
1686 			lvp->lv_num_segs = loadable;
1687 			lib_va_release(lvp);
1688 			lvp = NULL;
1689 			MOBJ_STAT_ADD(lvp_nocache);
1690 		}
1691 		/*
1692 		 * Free the beginning of the mapping if the padding
1693 		 * was not aligned correctly.
1694 		 */
1695 		if (padding != 0 && add_pad != padding) {
1696 			(void) as_unmap(as, start_addr,
1697 			    add_pad - padding);
1698 			start_addr += (add_pad - padding);
1699 			MOBJ_STAT_ADD(extra_padding);
1700 		}
1701 	}
1702 
1703 	/*
1704 	 * At this point, we have reserved the virtual address space
1705 	 * for our mappings.  Now we need to start filling out the mrp
1706 	 * array to describe all of the individual mappings we are going
1707 	 * to return.
1708 	 * For ET_EXEC there has been no memory reservation since we are
1709 	 * using fixed addresses.  While filling in the mrp array below,
1710 	 * we will have the first segment biased to start at addr 0
1711 	 * and the rest will be biased by this same amount.  Thus if there
1712 	 * is padding, the first padding will start at addr 0, and the next
1713 	 * segment will start at the value of padding.
1714 	 */
1715 
1716 	/* We'll fill out padding later, so start filling in mrp at index 1 */
1717 	if (padding != 0) {
1718 		current = 1;
1719 	}
1720 
1721 	/* If we have no more need for lvp let it go now */
1722 	if (lvp != NULL && use_lib_va == 0) {
1723 		lib_va_release(lvp);
1724 		MOBJ_STAT_ADD(lvp_not_needed);
1725 		lvp = NULL;
1726 	}
1727 
1728 	/* Now fill out the mrp structs from the program headers */
1729 	STRUCT_SET_HANDLE(mph, model, (struct myphdr *)phdrbase);
1730 	for (i = 0; i < nphdrs; i++) {
1731 		p_type = STRUCT_FGET(mph, x.p_type);
1732 		if (p_type == PT_LOAD || p_type == PT_SUNWBSS) {
1733 			vaddr = (caddr_t)(uintptr_t)STRUCT_FGET(mph, x.p_vaddr);
1734 			p_memsz = STRUCT_FGET(mph, x.p_memsz);
1735 			p_filesz = STRUCT_FGET(mph, x.p_filesz);
1736 			p_offset = STRUCT_FGET(mph, x.p_offset);
1737 			p_flags = STRUCT_FGET(mph, x.p_flags);
1738 
1739 			/*
1740 			 * Skip this header if it requests no memory to be
1741 			 * mapped.
1742 			 */
1743 			if (p_memsz == 0) {
1744 				STRUCT_SET_HANDLE(mph, model,
1745 				    (struct myphdr *)((size_t)STRUCT_BUF(mph) +
1746 				    hsize));
1747 				MOBJ_STAT_ADD(no_mem_map_sz);
1748 				continue;
1749 			}
1750 
1751 			prot = 0;
1752 			if (p_flags & PF_R)
1753 				prot |= PROT_READ;
1754 			if (p_flags & PF_W)
1755 				prot |= PROT_WRITE;
1756 			if (p_flags & PF_X)
1757 				prot |= PROT_EXEC;
1758 
1759 			ASSERT(current < loadable);
1760 			mrp[current].mr_msize = p_memsz;
1761 			mrp[current].mr_fsize = p_filesz;
1762 			mrp[current].mr_offset = p_offset;
1763 			mrp[current].mr_prot = prot;
1764 
1765 			if (hdr_seen == 0 && p_filesz != 0) {
1766 				mrp[current].mr_flags = MR_HDR_ELF;
1767 				/*
1768 				 * We modify mr_offset because we
1769 				 * need to map the ELF header as well, and if
1770 				 * we didn't then the header could be left out
1771 				 * of the mapping that we will create later.
1772 				 * Since we're removing the offset, we need to
1773 				 * account for that in the other fields as well
1774 				 * since we will be mapping the memory from 0
1775 				 * to p_offset.
1776 				 */
1777 				if (e_type == ET_DYN) {
1778 					mrp[current].mr_offset = 0;
1779 					mrp[current].mr_msize += p_offset;
1780 					mrp[current].mr_fsize += p_offset;
1781 				} else {
1782 					ASSERT(e_type == ET_EXEC);
1783 					/*
1784 					 * Save off the start addr which will be
1785 					 * our bias for the rest of the
1786 					 * ET_EXEC mappings.
1787 					 */
1788 					start_addr = vaddr - padding;
1789 				}
1790 				mrp[current].mr_addr = (caddr_t)padding;
1791 				hdr_seen = 1;
1792 			} else {
1793 				if (e_type == ET_EXEC) {
1794 					/* bias mr_addr */
1795 					mrp[current].mr_addr =
1796 					    vaddr - (size_t)start_addr;
1797 				} else {
1798 					mrp[current].mr_addr = vaddr + padding;
1799 				}
1800 				mrp[current].mr_flags = 0;
1801 			}
1802 			current++;
1803 		}
1804 
1805 		/* Move to next phdr */
1806 		STRUCT_SET_HANDLE(mph, model,
1807 		    (struct myphdr *)((size_t)STRUCT_BUF(mph) +
1808 		    hsize));
1809 	}
1810 
1811 	/* Now fill out the padding segments */
1812 	if (padding != 0) {
1813 		mrp[0].mr_addr = NULL;
1814 		mrp[0].mr_msize = padding;
1815 		mrp[0].mr_fsize = 0;
1816 		mrp[0].mr_offset = 0;
1817 		mrp[0].mr_prot = 0;
1818 		mrp[0].mr_flags = MR_PADDING;
1819 
1820 		/* Setup padding for the last segment */
1821 		ASSERT(current == loadable - 1);
1822 		mrp[current].mr_addr = (caddr_t)lib_len + padding;
1823 		mrp[current].mr_msize = padding;
1824 		mrp[current].mr_fsize = 0;
1825 		mrp[current].mr_offset = 0;
1826 		mrp[current].mr_prot = 0;
1827 		mrp[current].mr_flags = MR_PADDING;
1828 	}
1829 
1830 	/*
1831 	 * Need to make sure address ranges desired are not in use or
1832 	 * are previously allocated reservations from /dev/null.  For
1833 	 * ET_DYN, we already made sure our address range was free.
1834 	 */
1835 	if (e_type == ET_EXEC) {
1836 		ret = check_exec_addrs(loadable, mrp, start_addr);
1837 		if (ret != 0) {
1838 			ASSERT(lvp == NULL);
1839 			MOBJ_STAT_ADD(check_exec_failed);
1840 			return (ret);
1841 		}
1842 	}
1843 
1844 	/* Finish up our business with lvp. */
1845 	if (lvp) {
1846 		ASSERT(e_type == ET_DYN);
1847 		if (lvp->lv_num_segs == 0 && loadable <= LIBVA_CACHED_SEGS) {
1848 			bcopy(mrp, lvp->lv_mps,
1849 			    loadable * sizeof (mmapobj_result_t));
1850 			membar_producer();
1851 		}
1852 		/*
1853 		 * Setting lv_num_segs to a non-zero value indicates that
1854 		 * lv_mps is now valid and can be used by other threads.
1855 		 * So, the above stores need to finish before lv_num_segs
1856 		 * is updated. lv_mps is only valid if lv_num_segs is
1857 		 * greater than LIBVA_CACHED_SEGS.
1858 		 */
1859 		lvp->lv_num_segs = loadable;
1860 		lib_va_release(lvp);
1861 		MOBJ_STAT_ADD(lvp_used);
1862 	}
1863 
1864 	/* Now that we have mrp completely filled out go map it */
1865 	ret = mmapobj_map_elf(vp, start_addr, mrp, loadable, fcred, e_type);
1866 	if (ret == 0) {
1867 		*num_mapped = loadable;
1868 	}
1869 
1870 	return (ret);
1871 }
1872 
1873 /*
1874  * Take the ELF file passed in, and do the work of mapping it.
1875  * num_mapped in - # elements in user buffer
1876  * num_mapped out - # sections mapped and length of mrp array if
1877  *			no errors.
1878  */
1879 static int
1880 doelfwork(Ehdr *ehdrp, vnode_t *vp, mmapobj_result_t *mrp,
1881     uint_t *num_mapped, size_t padding, cred_t *fcred)
1882 {
1883 	int error;
1884 	offset_t phoff;
1885 	int nphdrs;
1886 	unsigned char ei_class;
1887 	unsigned short phentsize;
1888 	ssize_t phsizep;
1889 	caddr_t phbasep;
1890 	int to_map;
1891 	model_t model;
1892 
1893 	ei_class = ehdrp->e_ident[EI_CLASS];
1894 	model = get_udatamodel();
1895 	if ((model == DATAMODEL_ILP32 && ei_class == ELFCLASS64) ||
1896 	    (model == DATAMODEL_LP64 && ei_class == ELFCLASS32)) {
1897 		MOBJ_STAT_ADD(wrong_model);
1898 		return (ENOTSUP);
1899 	}
1900 
1901 	/* Can't execute code from "noexec" mounted filesystem. */
1902 	if (ehdrp->e_type == ET_EXEC &&
1903 	    (vp->v_vfsp->vfs_flag & VFS_NOEXEC) != 0) {
1904 		MOBJ_STAT_ADD(noexec_fs);
1905 		return (EACCES);
1906 	}
1907 
1908 	/*
1909 	 * Relocatable and core files are mapped as a single flat file
1910 	 * since no interpretation is done on them by mmapobj.
1911 	 */
1912 	if (ehdrp->e_type == ET_REL || ehdrp->e_type == ET_CORE) {
1913 		to_map = padding ? 3 : 1;
1914 		if (*num_mapped < to_map) {
1915 			*num_mapped = to_map;
1916 			MOBJ_STAT_ADD(e2big_et_rel);
1917 			return (E2BIG);
1918 		}
1919 		error = mmapobj_map_flat(vp, mrp, padding, fcred);
1920 		if (error == 0) {
1921 			*num_mapped = to_map;
1922 			mrp[padding ? 1 : 0].mr_flags = MR_HDR_ELF;
1923 			MOBJ_STAT_ADD(et_rel_mapped);
1924 		}
1925 		return (error);
1926 	}
1927 
1928 	/* Check for an unknown ELF type */
1929 	if (ehdrp->e_type != ET_EXEC && ehdrp->e_type != ET_DYN) {
1930 		MOBJ_STAT_ADD(unknown_elf_type);
1931 		return (ENOTSUP);
1932 	}
1933 
1934 	if (ei_class == ELFCLASS32) {
1935 		Elf32_Ehdr *e32hdr = (Elf32_Ehdr *)ehdrp;
1936 		ASSERT(model == DATAMODEL_ILP32);
1937 		nphdrs = e32hdr->e_phnum;
1938 		phentsize = e32hdr->e_phentsize;
1939 		if (phentsize < sizeof (Elf32_Phdr)) {
1940 			MOBJ_STAT_ADD(phent32_too_small);
1941 			return (ENOTSUP);
1942 		}
1943 		phoff = e32hdr->e_phoff;
1944 	} else if (ei_class == ELFCLASS64) {
1945 		Elf64_Ehdr *e64hdr = (Elf64_Ehdr *)ehdrp;
1946 		ASSERT(model == DATAMODEL_LP64);
1947 		nphdrs = e64hdr->e_phnum;
1948 		phentsize = e64hdr->e_phentsize;
1949 		if (phentsize < sizeof (Elf64_Phdr)) {
1950 			MOBJ_STAT_ADD(phent64_too_small);
1951 			return (ENOTSUP);
1952 		}
1953 		phoff = e64hdr->e_phoff;
1954 	} else {
1955 		/* fallthrough case for an invalid ELF class */
1956 		MOBJ_STAT_ADD(inval_elf_class);
1957 		return (ENOTSUP);
1958 	}
1959 
1960 	/*
1961 	 * nphdrs should only have this value for core files which are handled
1962 	 * above as a single mapping.  If other file types ever use this
1963 	 * sentinel, then we'll add the support needed to handle this here.
1964 	 */
1965 	if (nphdrs == PN_XNUM) {
1966 		MOBJ_STAT_ADD(too_many_phdrs);
1967 		return (ENOTSUP);
1968 	}
1969 
1970 	phsizep = nphdrs * phentsize;
1971 
1972 	if (phsizep == 0) {
1973 		MOBJ_STAT_ADD(no_phsize);
1974 		return (ENOTSUP);
1975 	}
1976 
1977 	/* Make sure we only wait for memory if it's a reasonable request */
1978 	if (phsizep > mmapobj_alloc_threshold) {
1979 		MOBJ_STAT_ADD(phsize_large);
1980 		if ((phbasep = kmem_alloc(phsizep, KM_NOSLEEP)) == NULL) {
1981 			MOBJ_STAT_ADD(phsize_xtralarge);
1982 			return (ENOMEM);
1983 		}
1984 	} else {
1985 		phbasep = kmem_alloc(phsizep, KM_SLEEP);
1986 	}
1987 
1988 	if ((error = vn_rdwr(UIO_READ, vp, phbasep, phsizep,
1989 	    (offset_t)phoff, UIO_SYSSPACE, 0, (rlim64_t)0,
1990 	    fcred, NULL)) != 0) {
1991 		kmem_free(phbasep, phsizep);
1992 		return (error);
1993 	}
1994 
1995 	/* Now process the phdr's */
1996 	error = process_phdr(ehdrp, phbasep, nphdrs, mrp, vp, num_mapped,
1997 	    padding, fcred);
1998 	kmem_free(phbasep, phsizep);
1999 	return (error);
2000 }
2001 
2002 #if defined(__sparc)
2003 /*
2004  * Hack to support 64 bit kernels running AOUT 4.x programs.
2005  * This is the sizeof (struct nlist) for a 32 bit kernel.
2006  * Since AOUT programs are 32 bit only, they will never use the 64 bit
2007  * sizeof (struct nlist) and thus creating a #define is the simplest
2008  * way around this since this is a format which is not being updated.
2009  * This will be used in the place of sizeof (struct nlist) below.
2010  */
2011 #define	NLIST_SIZE	(0xC)
2012 
2013 static int
2014 doaoutwork(vnode_t *vp, mmapobj_result_t *mrp,
2015     uint_t *num_mapped, struct exec *hdr, cred_t *fcred)
2016 {
2017 	int error;
2018 	size_t size;
2019 	size_t osize;
2020 	size_t nsize;	/* nlist size */
2021 	size_t msize;
2022 	size_t zfoddiff;
2023 	caddr_t addr;
2024 	caddr_t start_addr;
2025 	struct as *as = curproc->p_as;
2026 	int prot = PROT_USER | PROT_READ | PROT_EXEC;
2027 	uint_t mflag = MAP_PRIVATE | _MAP_LOW32;
2028 	offset_t off = 0;
2029 	int segnum = 0;
2030 	uint_t to_map;
2031 	int is_library = 0;
2032 	struct segvn_crargs crargs = SEGVN_ZFOD_ARGS(PROT_ZFOD, PROT_ALL);
2033 
2034 	/* Only 32bit apps supported by this file format */
2035 	if (get_udatamodel() != DATAMODEL_ILP32) {
2036 		MOBJ_STAT_ADD(aout_64bit_try);
2037 		return (ENOTSUP);
2038 	}
2039 
2040 	/* Check to see if this is a library */
2041 	if (hdr->a_magic == ZMAGIC && hdr->a_entry < PAGESIZE) {
2042 		is_library = 1;
2043 	}
2044 
2045 	/* Can't execute code from "noexec" mounted filesystem. */
2046 	if (((vp->v_vfsp->vfs_flag & VFS_NOEXEC) != 0) && (is_library == 0)) {
2047 		MOBJ_STAT_ADD(aout_noexec);
2048 		return (EACCES);
2049 	}
2050 
2051 	/*
2052 	 * There are 2 ways to calculate the mapped size of executable:
2053 	 * 1) rounded text size + data size + bss size.
2054 	 * 2) starting offset for text + text size + data size + text relocation
2055 	 *    size + data relocation size + room for nlist data structure.
2056 	 *
2057 	 * The larger of the two sizes will be used to map this binary.
2058 	 */
2059 	osize = P2ROUNDUP(hdr->a_text, PAGESIZE) + hdr->a_data + hdr->a_bss;
2060 
2061 	off = hdr->a_magic == ZMAGIC ? 0 : sizeof (struct exec);
2062 
2063 	nsize = off + hdr->a_text + hdr->a_data + hdr->a_trsize +
2064 	    hdr->a_drsize + NLIST_SIZE;
2065 
2066 	size = MAX(osize, nsize);
2067 	if (size != nsize) {
2068 		nsize = 0;
2069 	}
2070 
2071 	/*
2072 	 * 1 seg for text and 1 seg for initialized data.
2073 	 * 1 seg for bss (if can't fit in leftover space of init data)
2074 	 * 1 seg for nlist if needed.
2075 	 */
2076 	to_map = 2 + (nsize ? 1 : 0) +
2077 	    (hdr->a_bss > PAGESIZE - P2PHASE(hdr->a_data, PAGESIZE) ? 1 : 0);
2078 	if (*num_mapped < to_map) {
2079 		*num_mapped = to_map;
2080 		MOBJ_STAT_ADD(aout_e2big);
2081 		return (E2BIG);
2082 	}
2083 
2084 	/* Reserve address space for the whole mapping */
2085 	if (is_library) {
2086 		/* We'll let VOP_MAP below pick our address for us */
2087 		addr = NULL;
2088 		MOBJ_STAT_ADD(aout_lib);
2089 	} else {
2090 		/*
2091 		 * default start address for fixed binaries from AOUT 4.x
2092 		 * standard.
2093 		 */
2094 		MOBJ_STAT_ADD(aout_fixed);
2095 		mflag |= MAP_FIXED;
2096 		addr = (caddr_t)0x2000;
2097 		as_rangelock(as);
2098 		if (as_gap(as, size, &addr, &size, 0, NULL) != 0) {
2099 			as_rangeunlock(as);
2100 			MOBJ_STAT_ADD(aout_addr_in_use);
2101 			return (EADDRINUSE);
2102 		}
2103 		crargs.flags |= MAP_NORESERVE;
2104 		error = as_map(as, addr, size, segvn_create, &crargs);
2105 		ASSERT(addr == (caddr_t)0x2000);
2106 		as_rangeunlock(as);
2107 	}
2108 
2109 	start_addr = addr;
2110 	osize = size;
2111 
2112 	/*
2113 	 * Map as large as we need, backed by file, this will be text, and
2114 	 * possibly the nlist segment.  We map over this mapping for bss and
2115 	 * initialized data segments.
2116 	 */
2117 	error = VOP_MAP(vp, off, as, &addr, size, prot, PROT_ALL,
2118 	    mflag, fcred, NULL);
2119 	if (error) {
2120 		if (!is_library) {
2121 			(void) as_unmap(as, start_addr, osize);
2122 		}
2123 		return (error);
2124 	}
2125 
2126 	/* pickup the value of start_addr and osize for libraries */
2127 	start_addr = addr;
2128 	osize = size;
2129 
2130 	/*
2131 	 * We have our initial reservation/allocation so we need to use fixed
2132 	 * addresses from now on.
2133 	 */
2134 	mflag |= MAP_FIXED;
2135 
2136 	mrp[0].mr_addr = addr;
2137 	mrp[0].mr_msize = hdr->a_text;
2138 	mrp[0].mr_fsize = hdr->a_text;
2139 	mrp[0].mr_offset = 0;
2140 	mrp[0].mr_prot = PROT_READ | PROT_EXEC;
2141 	mrp[0].mr_flags = MR_HDR_AOUT;
2142 
2143 
2144 	/*
2145 	 * Map initialized data. We are mapping over a portion of the
2146 	 * previous mapping which will be unmapped in VOP_MAP below.
2147 	 */
2148 	off = P2ROUNDUP((offset_t)(hdr->a_text), PAGESIZE);
2149 	msize = off;
2150 	addr += off;
2151 	size = hdr->a_data;
2152 	error = VOP_MAP(vp, off, as, &addr, size, PROT_ALL, PROT_ALL,
2153 	    mflag, fcred, NULL);
2154 	if (error) {
2155 		(void) as_unmap(as, start_addr, osize);
2156 		return (error);
2157 	}
2158 	msize += size;
2159 	mrp[1].mr_addr = addr;
2160 	mrp[1].mr_msize = size;
2161 	mrp[1].mr_fsize = size;
2162 	mrp[1].mr_offset = 0;
2163 	mrp[1].mr_prot = PROT_READ | PROT_WRITE | PROT_EXEC;
2164 	mrp[1].mr_flags = 0;
2165 
2166 	/* Need to zero out remainder of page */
2167 	addr += hdr->a_data;
2168 	zfoddiff = P2PHASE((size_t)addr, PAGESIZE);
2169 	if (zfoddiff) {
2170 		label_t ljb;
2171 
2172 		MOBJ_STAT_ADD(aout_zfoddiff);
2173 		zfoddiff = PAGESIZE - zfoddiff;
2174 		if (on_fault(&ljb)) {
2175 			no_fault();
2176 			MOBJ_STAT_ADD(aout_uzero_fault);
2177 			(void) as_unmap(as, start_addr, osize);
2178 			return (EFAULT);
2179 		}
2180 		uzero(addr, zfoddiff);
2181 		no_fault();
2182 	}
2183 	msize += zfoddiff;
2184 	segnum = 2;
2185 
2186 	/* Map bss */
2187 	if (hdr->a_bss > zfoddiff) {
2188 		struct segvn_crargs crargs =
2189 		    SEGVN_ZFOD_ARGS(PROT_ZFOD, PROT_ALL);
2190 		MOBJ_STAT_ADD(aout_map_bss);
2191 		addr += zfoddiff;
2192 		size = hdr->a_bss - zfoddiff;
2193 		as_rangelock(as);
2194 		(void) as_unmap(as, addr, size);
2195 		error = as_map(as, addr, size, segvn_create, &crargs);
2196 		as_rangeunlock(as);
2197 		msize += size;
2198 
2199 		if (error) {
2200 			MOBJ_STAT_ADD(aout_bss_fail);
2201 			(void) as_unmap(as, start_addr, osize);
2202 			return (error);
2203 		}
2204 		mrp[2].mr_addr = addr;
2205 		mrp[2].mr_msize = size;
2206 		mrp[2].mr_fsize = 0;
2207 		mrp[2].mr_offset = 0;
2208 		mrp[2].mr_prot = PROT_READ | PROT_WRITE | PROT_EXEC;
2209 		mrp[2].mr_flags = 0;
2210 
2211 		addr += size;
2212 		segnum = 3;
2213 	}
2214 
2215 	/*
2216 	 * If we have extra bits left over, we need to include that in how
2217 	 * much we mapped to make sure the nlist logic is correct
2218 	 */
2219 	msize = P2ROUNDUP(msize, PAGESIZE);
2220 
2221 	if (nsize && msize < nsize) {
2222 		MOBJ_STAT_ADD(aout_nlist);
2223 		mrp[segnum].mr_addr = addr;
2224 		mrp[segnum].mr_msize = nsize - msize;
2225 		mrp[segnum].mr_fsize = 0;
2226 		mrp[segnum].mr_offset = 0;
2227 		mrp[segnum].mr_prot = PROT_READ | PROT_EXEC;
2228 		mrp[segnum].mr_flags = 0;
2229 	}
2230 
2231 	*num_mapped = to_map;
2232 	return (0);
2233 }
2234 #endif
2235 
2236 /*
2237  * These are the two types of files that we can interpret and we want to read
2238  * in enough info to cover both types when looking at the initial header.
2239  */
2240 #define	MAX_HEADER_SIZE	(MAX(sizeof (Ehdr), sizeof (struct exec)))
2241 
2242 /*
2243  * Map vp passed in in an interpreted manner.  ELF and AOUT files will be
2244  * interpreted and mapped appropriately for execution.
2245  * num_mapped in - # elements in mrp
2246  * num_mapped out - # sections mapped and length of mrp array if
2247  *		    no errors or E2BIG returned.
2248  *
2249  * Returns 0 on success, errno value on failure.
2250  */
2251 static int
2252 mmapobj_map_interpret(vnode_t *vp, mmapobj_result_t *mrp,
2253     uint_t *num_mapped, size_t padding, cred_t *fcred)
2254 {
2255 	int error = 0;
2256 	vattr_t vattr;
2257 	struct lib_va *lvp;
2258 	caddr_t start_addr;
2259 	model_t model;
2260 
2261 	/*
2262 	 * header has to be aligned to the native size of ulong_t in order
2263 	 * to avoid an unaligned access when dereferencing the header as
2264 	 * a ulong_t.  Thus we allocate our array on the stack of type
2265 	 * ulong_t and then have header, which we dereference later as a char
2266 	 * array point at lheader.
2267 	 */
2268 	ulong_t lheader[(MAX_HEADER_SIZE / (sizeof (ulong_t))) + 1];
2269 	caddr_t header = (caddr_t)&lheader;
2270 
2271 	vattr.va_mask = AT_FSID | AT_NODEID | AT_CTIME | AT_MTIME | AT_SIZE;
2272 	error = VOP_GETATTR(vp, &vattr, 0, fcred, NULL);
2273 	if (error) {
2274 		return (error);
2275 	}
2276 
2277 	/*
2278 	 * Check lib_va to see if we already have a full description
2279 	 * for this library.  This is the fast path and only used for
2280 	 * ET_DYN ELF files (dynamic libraries).
2281 	 */
2282 	if (padding == 0 && (lvp = lib_va_find(&vattr)) != NULL) {
2283 		int num_segs;
2284 
2285 		model = get_udatamodel();
2286 		if ((model == DATAMODEL_ILP32 &&
2287 		    lvp->lv_flags & LV_ELF64) ||
2288 		    (model == DATAMODEL_LP64 &&
2289 		    lvp->lv_flags & LV_ELF32)) {
2290 			lib_va_release(lvp);
2291 			MOBJ_STAT_ADD(fast_wrong_model);
2292 			return (ENOTSUP);
2293 		}
2294 		num_segs = lvp->lv_num_segs;
2295 		if (*num_mapped < num_segs) {
2296 			*num_mapped = num_segs;
2297 			lib_va_release(lvp);
2298 			MOBJ_STAT_ADD(fast_e2big);
2299 			return (E2BIG);
2300 		}
2301 
2302 		/*
2303 		 * Check to see if we have all the mappable program headers
2304 		 * cached.
2305 		 */
2306 		if (num_segs <= LIBVA_CACHED_SEGS && num_segs != 0) {
2307 			MOBJ_STAT_ADD(fast);
2308 			start_addr = mmapobj_lookup_start_addr(lvp);
2309 			if (start_addr == NULL) {
2310 				lib_va_release(lvp);
2311 				return (ENOMEM);
2312 			}
2313 
2314 			bcopy(lvp->lv_mps, mrp,
2315 			    num_segs * sizeof (mmapobj_result_t));
2316 
2317 			error = mmapobj_map_elf(vp, start_addr, mrp,
2318 			    num_segs, fcred, ET_DYN);
2319 
2320 			lib_va_release(lvp);
2321 			if (error == 0) {
2322 				*num_mapped = num_segs;
2323 				MOBJ_STAT_ADD(fast_success);
2324 			}
2325 			return (error);
2326 		}
2327 		MOBJ_STAT_ADD(fast_not_now);
2328 
2329 		/* Release it for now since we'll look it up below */
2330 		lib_va_release(lvp);
2331 	}
2332 
2333 	/*
2334 	 * Time to see if this is a file we can interpret.  If it's smaller
2335 	 * than this, then we can't interpret it.
2336 	 */
2337 	if (vattr.va_size < MAX_HEADER_SIZE) {
2338 		MOBJ_STAT_ADD(small_file);
2339 		return (ENOTSUP);
2340 	}
2341 
2342 	if ((error = vn_rdwr(UIO_READ, vp, header, MAX_HEADER_SIZE, 0,
2343 	    UIO_SYSSPACE, 0, (rlim64_t)0, fcred, NULL)) != 0) {
2344 		MOBJ_STAT_ADD(read_error);
2345 		return (error);
2346 	}
2347 
2348 	/* Verify file type */
2349 	if (header[EI_MAG0] == ELFMAG0 && header[EI_MAG1] == ELFMAG1 &&
2350 	    header[EI_MAG2] == ELFMAG2 && header[EI_MAG3] == ELFMAG3) {
2351 		return (doelfwork((Ehdr *)lheader, vp, mrp, num_mapped,
2352 		    padding, fcred));
2353 	}
2354 
2355 #if defined(__sparc)
2356 	/* On sparc, check for 4.X AOUT format */
2357 	switch (((struct exec *)header)->a_magic) {
2358 	case OMAGIC:
2359 	case ZMAGIC:
2360 	case NMAGIC:
2361 		return (doaoutwork(vp, mrp, num_mapped,
2362 		    (struct exec *)lheader, fcred));
2363 	}
2364 #endif
2365 
2366 	/* Unsupported type */
2367 	MOBJ_STAT_ADD(unsupported);
2368 	return (ENOTSUP);
2369 }
2370 
2371 /*
2372  * Given a vnode, map it as either a flat file or interpret it and map
2373  * it according to the rules of the file type.
2374  * *num_mapped will contain the size of the mmapobj_result_t array passed in.
2375  * If padding is non-zero, the mappings will be padded by that amount
2376  * rounded up to the nearest pagesize.
2377  * If the mapping is successful, *num_mapped will contain the number of
2378  * distinct mappings created, and mrp will point to the array of
2379  * mmapobj_result_t's which describe these mappings.
2380  *
2381  * On error, -1 is returned and errno is set appropriately.
2382  * A special error case will set errno to E2BIG when there are more than
2383  * *num_mapped mappings to be created and *num_mapped will be set to the
2384  * number of mappings needed.
2385  */
2386 int
2387 mmapobj(vnode_t *vp, uint_t flags, mmapobj_result_t *mrp,
2388     uint_t *num_mapped, size_t padding, cred_t *fcred)
2389 {
2390 	int to_map;
2391 	int error = 0;
2392 
2393 	ASSERT((padding & PAGEOFFSET) == 0);
2394 	ASSERT((flags & ~MMOBJ_ALL_FLAGS) == 0);
2395 	ASSERT(num_mapped != NULL);
2396 	ASSERT((flags & MMOBJ_PADDING) ? padding != 0 : padding == 0);
2397 
2398 	if ((flags & MMOBJ_INTERPRET) == 0) {
2399 		to_map = padding ? 3 : 1;
2400 		if (*num_mapped < to_map) {
2401 			*num_mapped = to_map;
2402 			MOBJ_STAT_ADD(flat_e2big);
2403 			return (E2BIG);
2404 		}
2405 		error = mmapobj_map_flat(vp, mrp, padding, fcred);
2406 
2407 		if (error) {
2408 			return (error);
2409 		}
2410 		*num_mapped = to_map;
2411 		return (0);
2412 	}
2413 
2414 	error = mmapobj_map_interpret(vp, mrp, num_mapped, padding, fcred);
2415 	return (error);
2416 }
2417