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