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.
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.
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]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 * Copyright 2014 Joyent, Inc. All rights reserved.
27 #include <sys/types.h>
28 #include <sys/sysmacros.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/errno.h>
34 #include <sys/cmn_err.h>
36 #include <sys/vmsystm.h>
37 #include <sys/machsystm.h>
38 #include <sys/debug.h>
41 #include <sys/vmparam.h>
44 #include <sys/machelf.h>
45 #include <sys/corectl.h>
47 #include <sys/exechdr.h>
48 #include <sys/autoconf.h>
50 #include <vm/seg_dev.h>
51 #include <sys/vmparam.h>
52 #include <sys/mmapobj.h>
53 #include <sys/atomic.h>
58 * The main driving force behind mmapobj is to interpret and map ELF files
59 * inside of the kernel instead of having the linker be responsible for this.
61 * mmapobj also supports the AOUT 4.x binary format as well as flat files in
64 * When interpreting and mapping an ELF file, mmapobj will map each PT_LOAD
65 * or PT_SUNWBSS segment according to the ELF standard. Refer to the "Linker
66 * and Libraries Guide" for more information about the standard and mapping
69 * Having mmapobj interpret and map objects will allow the kernel to make the
70 * best decision for where to place the mappings for said objects. Thus, we
71 * can make optimizations inside of the kernel for specific platforms or cache
72 * mapping information to make mapping objects faster. The cache is ignored
75 * The lib_va_hash will be one such optimization. For each ELF object that
76 * mmapobj is asked to interpret, we will attempt to cache the information
77 * about the PT_LOAD and PT_SUNWBSS sections to speed up future mappings of
78 * the same objects. We will cache up to LIBVA_CACHED_SEGS (see below) program
79 * headers which should cover a majority of the libraries out there without
80 * wasting space. In order to make sure that the cached information is valid,
81 * we check the passed in vnode's mtime and ctime to make sure the vnode
82 * has not been modified since the last time we used it.
84 * In addition, the lib_va_hash may contain a preferred starting VA for the
85 * object which can be useful for platforms which support a shared context.
86 * This will increase the likelyhood that library text can be shared among
87 * many different processes. We limit the reserved VA space for 32 bit objects
88 * in order to minimize fragmenting the processes address space.
90 * In addition to the above, the mmapobj interface allows for padding to be
91 * requested before the first mapping and after the last mapping created.
92 * When padding is requested, no additional optimizations will be made for
97 * Threshold to prevent allocating too much kernel memory to read in the
98 * program headers for an object. If it requires more than below,
99 * we will use a KM_NOSLEEP allocation to allocate memory to hold all of the
100 * program headers which could possibly fail. If less memory than below is
101 * needed, then we use a KM_SLEEP allocation and are willing to wait for the
102 * memory if we need to.
104 size_t mmapobj_alloc_threshold
= 65536;
106 /* Debug stats for test coverage */
109 uint_t mobjs_unmap_called
;
110 uint_t mobjs_remap_devnull
;
111 uint_t mobjs_lookup_start
;
112 uint_t mobjs_alloc_start
;
113 uint_t mobjs_alloc_vmem
;
114 uint_t mobjs_add_collision
;
115 uint_t mobjs_get_addr
;
116 uint_t mobjs_map_flat_no_padding
;
117 uint_t mobjs_map_flat_padding
;
118 uint_t mobjs_map_ptload_text
;
119 uint_t mobjs_map_ptload_initdata
;
120 uint_t mobjs_map_ptload_preread
;
121 uint_t mobjs_map_ptload_unaligned_text
;
122 uint_t mobjs_map_ptload_unaligned_map_fail
;
123 uint_t mobjs_map_ptload_unaligned_read_fail
;
124 uint_t mobjs_zfoddiff
;
125 uint_t mobjs_zfoddiff_nowrite
;
126 uint_t mobjs_zfodextra
;
127 uint_t mobjs_ptload_failed
;
128 uint_t mobjs_map_elf_no_holes
;
129 uint_t mobjs_unmap_hole
;
130 uint_t mobjs_nomem_header
;
131 uint_t mobjs_inval_header
;
132 uint_t mobjs_overlap_header
;
133 uint_t mobjs_np2_align
;
134 uint_t mobjs_np2_align_overflow
;
135 uint_t mobjs_exec_padding
;
136 uint_t mobjs_exec_addr_mapped
;
137 uint_t mobjs_exec_addr_devnull
;
138 uint_t mobjs_exec_addr_in_use
;
139 uint_t mobjs_lvp_found
;
140 uint_t mobjs_no_loadable_yet
;
141 uint_t mobjs_nothing_to_map
;
143 uint_t mobjs_dyn_pad_align
;
144 uint_t mobjs_dyn_pad_noalign
;
145 uint_t mobjs_alloc_start_fail
;
146 uint_t mobjs_lvp_nocache
;
147 uint_t mobjs_extra_padding
;
148 uint_t mobjs_lvp_not_needed
;
149 uint_t mobjs_no_mem_map_sz
;
150 uint_t mobjs_check_exec_failed
;
151 uint_t mobjs_lvp_used
;
152 uint_t mobjs_wrong_model
;
153 uint_t mobjs_noexec_fs
;
154 uint_t mobjs_e2big_et_rel
;
155 uint_t mobjs_et_rel_mapped
;
156 uint_t mobjs_unknown_elf_type
;
157 uint_t mobjs_phent32_too_small
;
158 uint_t mobjs_phent64_too_small
;
159 uint_t mobjs_inval_elf_class
;
160 uint_t mobjs_too_many_phdrs
;
161 uint_t mobjs_no_phsize
;
162 uint_t mobjs_phsize_large
;
163 uint_t mobjs_phsize_xtralarge
;
164 uint_t mobjs_fast_wrong_model
;
165 uint_t mobjs_fast_e2big
;
167 uint_t mobjs_fast_success
;
168 uint_t mobjs_fast_not_now
;
169 uint_t mobjs_small_file
;
170 uint_t mobjs_read_error
;
171 uint_t mobjs_unsupported
;
172 uint_t mobjs_flat_e2big
;
173 uint_t mobjs_phent_align32
;
174 uint_t mobjs_phent_align64
;
175 uint_t mobjs_lib_va_find_hit
;
176 uint_t mobjs_lib_va_find_delay_delete
;
177 uint_t mobjs_lib_va_find_delete
;
178 uint_t mobjs_lib_va_add_delay_delete
;
179 uint_t mobjs_lib_va_add_delete
;
180 uint_t mobjs_lib_va_create_failure
;
181 uint_t mobjs_min_align
;
184 #define MOBJ_STAT_ADD(stat) ((mobj_stats.mobjs_##stat)++)
186 #define MOBJ_STAT_ADD(stat)
190 * Check if addr is at or above the address space reserved for the stack.
191 * The stack is at the top of the address space for all sparc processes
192 * and 64 bit x86 processes. For 32 bit x86, the stack is not at the top
193 * of the address space and thus this check wil always return false for
194 * 32 bit x86 processes.
197 #define OVERLAPS_STACK(addr, p) \
198 (addr >= (p->p_usrstack - ((p->p_stk_ctl + PAGEOFFSET) & PAGEMASK)))
199 #elif defined(__amd64)
200 #define OVERLAPS_STACK(addr, p) \
201 ((p->p_model == DATAMODEL_LP64) && \
202 (addr >= (p->p_usrstack - ((p->p_stk_ctl + PAGEOFFSET) & PAGEMASK))))
203 #elif defined(__i386)
204 #define OVERLAPS_STACK(addr, p) 0
207 /* lv_flags values - bitmap */
208 #define LV_ELF32 0x1 /* 32 bit ELF file */
209 #define LV_ELF64 0x2 /* 64 bit ELF file */
210 #define LV_DEL 0x4 /* delete when lv_refcnt hits zero */
213 * Note: lv_num_segs will denote how many segments this file has and will
214 * only be set after the lv_mps array has been filled out.
215 * lv_mps can only be valid if lv_num_segs is non-zero.
218 struct lib_va
*lv_next
;
219 caddr_t lv_base_va
; /* start va for library */
220 ssize_t lv_len
; /* total va span of library */
221 size_t lv_align
; /* minimum alignment */
222 uint64_t lv_nodeid
; /* filesystem node id */
223 uint64_t lv_fsid
; /* filesystem id */
224 timestruc_t lv_ctime
; /* last time file was changed */
225 timestruc_t lv_mtime
; /* or modified */
226 mmapobj_result_t lv_mps
[LIBVA_CACHED_SEGS
]; /* cached pheaders */
227 int lv_num_segs
; /* # segs for this file */
229 uint_t lv_refcnt
; /* number of holds on struct */
232 #define LIB_VA_SIZE 1024
233 #define LIB_VA_MASK (LIB_VA_SIZE - 1)
234 #define LIB_VA_MUTEX_SHIFT 3
236 #if (LIB_VA_SIZE & (LIB_VA_SIZE - 1))
237 #error "LIB_VA_SIZE is not a power of 2"
240 static struct lib_va
*lib_va_hash
[LIB_VA_SIZE
];
241 static kmutex_t lib_va_hash_mutex
[LIB_VA_SIZE
>> LIB_VA_MUTEX_SHIFT
];
243 #define LIB_VA_HASH_MUTEX(index) \
244 (&lib_va_hash_mutex[index >> LIB_VA_MUTEX_SHIFT])
246 #define LIB_VA_HASH(nodeid) \
247 (((nodeid) ^ ((nodeid) << 7) ^ ((nodeid) << 13)) & LIB_VA_MASK)
249 #define LIB_VA_MATCH_ID(arg1, arg2) \
250 ((arg1)->lv_nodeid == (arg2)->va_nodeid && \
251 (arg1)->lv_fsid == (arg2)->va_fsid)
253 #define LIB_VA_MATCH_TIME(arg1, arg2) \
254 ((arg1)->lv_ctime.tv_sec == (arg2)->va_ctime.tv_sec && \
255 (arg1)->lv_mtime.tv_sec == (arg2)->va_mtime.tv_sec && \
256 (arg1)->lv_ctime.tv_nsec == (arg2)->va_ctime.tv_nsec && \
257 (arg1)->lv_mtime.tv_nsec == (arg2)->va_mtime.tv_nsec)
259 #define LIB_VA_MATCH(arg1, arg2) \
260 (LIB_VA_MATCH_ID(arg1, arg2) && LIB_VA_MATCH_TIME(arg1, arg2))
263 * lib_va will be used for optimized allocation of address ranges for
264 * libraries, such that subsequent mappings of the same library will attempt
265 * to use the same VA as previous mappings of that library.
266 * In order to map libraries at the same VA in many processes, we need to carve
267 * out our own address space for them which is unique across many processes.
268 * We use different arenas for 32 bit and 64 bit libraries.
270 * Since the 32 bit address space is relatively small, we limit the number of
271 * libraries which try to use consistent virtual addresses to lib_threshold.
272 * For 64 bit libraries there is no such limit since the address space is large.
274 static vmem_t
*lib_va_32_arena
;
275 static vmem_t
*lib_va_64_arena
;
276 uint_t lib_threshold
= 20; /* modifiable via /etc/system */
278 static kmutex_t lib_va_init_mutex
; /* no need to initialize */
281 * Number of 32 bit and 64 bit libraries in lib_va hash.
283 static uint_t libs_mapped_32
= 0;
284 static uint_t libs_mapped_64
= 0;
287 * Free up the resources associated with lvp as well as lvp itself.
288 * We also decrement the number of libraries mapped via a lib_va
289 * cached virtual address.
292 lib_va_free(struct lib_va
*lvp
)
294 int is_64bit
= lvp
->lv_flags
& LV_ELF64
;
295 ASSERT(lvp
->lv_refcnt
== 0);
297 if (lvp
->lv_base_va
!= NULL
) {
298 vmem_xfree(is_64bit
? lib_va_64_arena
: lib_va_32_arena
,
299 lvp
->lv_base_va
, lvp
->lv_len
);
301 atomic_dec_32(&libs_mapped_64
);
303 atomic_dec_32(&libs_mapped_32
);
306 kmem_free(lvp
, sizeof (struct lib_va
));
310 * See if the file associated with the vap passed in is in the lib_va hash.
311 * If it is and the file has not been modified since last use, then
312 * return a pointer to that data. Otherwise, return NULL if the file has
313 * changed or the file was not found in the hash.
315 static struct lib_va
*
316 lib_va_find(vattr_t
*vap
)
319 struct lib_va
*del
= NULL
;
322 index
= LIB_VA_HASH(vap
->va_nodeid
);
324 mutex_enter(LIB_VA_HASH_MUTEX(index
));
325 tmp
= &lib_va_hash
[index
];
326 while (*tmp
!= NULL
) {
328 if (LIB_VA_MATCH_ID(lvp
, vap
)) {
329 if (LIB_VA_MATCH_TIME(lvp
, vap
)) {
330 ASSERT((lvp
->lv_flags
& LV_DEL
) == 0);
332 MOBJ_STAT_ADD(lib_va_find_hit
);
335 * file was updated since last use.
336 * need to remove it from list.
342 * If we can't delete it now, mark it for later
344 if (del
->lv_refcnt
) {
345 MOBJ_STAT_ADD(lib_va_find_delay_delete
);
346 del
->lv_flags
|= LV_DEL
;
351 mutex_exit(LIB_VA_HASH_MUTEX(index
));
353 ASSERT(del
->lv_refcnt
== 0);
354 MOBJ_STAT_ADD(lib_va_find_delete
);
361 mutex_exit(LIB_VA_HASH_MUTEX(index
));
366 * Add a new entry to the lib_va hash.
367 * Search the hash while holding the appropriate mutex to make sure that the
368 * data is not already in the cache. If we find data that is in the cache
369 * already and has not been modified since last use, we return NULL. If it
370 * has been modified since last use, we will remove that entry from
371 * the hash and it will be deleted once it's reference count reaches zero.
372 * If there is no current entry in the hash we will add the new entry and
373 * return it to the caller who is responsible for calling lib_va_release to
374 * drop their reference count on it.
376 * lv_num_segs will be set to zero since the caller needs to add that
377 * information to the data structure.
379 static struct lib_va
*
380 lib_va_add_hash(caddr_t base_va
, ssize_t len
, size_t align
, vattr_t
*vap
)
386 struct lib_va
*del
= NULL
;
388 model
= get_udatamodel();
389 index
= LIB_VA_HASH(vap
->va_nodeid
);
391 lvp
= kmem_alloc(sizeof (struct lib_va
), KM_SLEEP
);
393 mutex_enter(LIB_VA_HASH_MUTEX(index
));
396 * Make sure not adding same data a second time.
397 * The hash chains should be relatively short and adding
398 * is a relatively rare event, so it's worth the check.
400 tmp
= &lib_va_hash
[index
];
401 while (*tmp
!= NULL
) {
402 if (LIB_VA_MATCH_ID(*tmp
, vap
)) {
403 if (LIB_VA_MATCH_TIME(*tmp
, vap
)) {
404 mutex_exit(LIB_VA_HASH_MUTEX(index
));
405 kmem_free(lvp
, sizeof (struct lib_va
));
410 * We have the same nodeid and fsid but the file has
411 * been modified since we last saw it.
412 * Need to remove the old node and add this new
414 * Could probably use a callback mechanism to make
423 * Check to see if we can free it. If lv_refcnt
424 * is greater than zero, than some other thread
425 * has a reference to the one we want to delete
426 * and we can not delete it. All of this is done
427 * under the lib_va_hash_mutex lock so it is atomic.
429 if (del
->lv_refcnt
) {
430 MOBJ_STAT_ADD(lib_va_add_delay_delete
);
431 del
->lv_flags
|= LV_DEL
;
434 /* tmp is already advanced */
437 tmp
= &((*tmp
)->lv_next
);
440 lvp
->lv_base_va
= base_va
;
442 lvp
->lv_align
= align
;
443 lvp
->lv_nodeid
= vap
->va_nodeid
;
444 lvp
->lv_fsid
= vap
->va_fsid
;
445 lvp
->lv_ctime
.tv_sec
= vap
->va_ctime
.tv_sec
;
446 lvp
->lv_ctime
.tv_nsec
= vap
->va_ctime
.tv_nsec
;
447 lvp
->lv_mtime
.tv_sec
= vap
->va_mtime
.tv_sec
;
448 lvp
->lv_mtime
.tv_nsec
= vap
->va_mtime
.tv_nsec
;
452 /* Caller responsible for filling this and lv_mps out */
453 lvp
->lv_num_segs
= 0;
455 if (model
== DATAMODEL_LP64
) {
456 lvp
->lv_flags
= LV_ELF64
;
458 ASSERT(model
== DATAMODEL_ILP32
);
459 lvp
->lv_flags
= LV_ELF32
;
462 if (base_va
!= NULL
) {
463 if (model
== DATAMODEL_LP64
) {
464 atomic_inc_32(&libs_mapped_64
);
466 ASSERT(model
== DATAMODEL_ILP32
);
467 atomic_inc_32(&libs_mapped_32
);
470 ASSERT(*tmp
== NULL
);
472 mutex_exit(LIB_VA_HASH_MUTEX(index
));
474 ASSERT(del
->lv_refcnt
== 0);
475 MOBJ_STAT_ADD(lib_va_add_delete
);
482 * Release the hold on lvp which was acquired by lib_va_find or lib_va_add_hash.
483 * In addition, if this is the last hold and lvp is marked for deletion,
484 * free up it's reserved address space and free the structure.
487 lib_va_release(struct lib_va
*lvp
)
492 ASSERT(lvp
->lv_refcnt
> 0);
494 index
= LIB_VA_HASH(lvp
->lv_nodeid
);
495 mutex_enter(LIB_VA_HASH_MUTEX(index
));
496 if (--lvp
->lv_refcnt
== 0 && (lvp
->lv_flags
& LV_DEL
)) {
499 mutex_exit(LIB_VA_HASH_MUTEX(index
));
501 ASSERT(lvp
->lv_next
== 0);
507 * Dummy function for mapping through /dev/null
508 * Normally I would have used mmmmap in common/io/mem.c
509 * but that is a static function, and for /dev/null, it
514 mmapobj_dummy(dev_t dev
, off_t off
, int prot
)
520 * Called when an error occurred which requires mmapobj to return failure.
521 * All mapped objects will be unmapped and /dev/null mappings will be
522 * reclaimed if necessary.
523 * num_mapped is the number of elements of mrp which have been mapped, and
524 * num_segs is the total number of elements in mrp.
525 * For e_type ET_EXEC, we need to unmap all of the elements in mrp since
526 * we had already made reservations for them.
527 * If num_mapped equals num_segs, then we know that we had fully mapped
528 * the file and only need to clean up the segments described.
529 * If they are not equal, then for ET_DYN we will unmap the range from the
530 * end of the last mapped segment to the end of the last segment in mrp
531 * since we would have made a reservation for that memory earlier.
532 * If e_type is passed in as zero, num_mapped must equal num_segs.
535 mmapobj_unmap(mmapobj_result_t
*mrp
, int num_mapped
, int num_segs
,
539 struct as
*as
= curproc
->p_as
;
543 if (e_type
== ET_EXEC
) {
544 num_mapped
= num_segs
;
548 ASSERT(num_mapped
== num_segs
);
552 MOBJ_STAT_ADD(unmap_called
);
553 for (i
= 0; i
< num_mapped
; i
++) {
556 * If we are going to have to create a mapping we need to
557 * make sure that no one else will use the address we
558 * need to remap between the time it is unmapped and
561 if (mrp
[i
].mr_flags
& MR_RESV
) {
564 /* Always need to unmap what we mapped */
565 (void) as_unmap(as
, mrp
[i
].mr_addr
, mrp
[i
].mr_msize
);
567 /* Need to reclaim /dev/null reservation from earlier */
568 if (mrp
[i
].mr_flags
& MR_RESV
) {
569 struct segdev_crargs dev_a
;
571 ASSERT(e_type
!= ET_DYN
);
573 * Use seg_dev segment driver for /dev/null mapping.
575 dev_a
.mapfunc
= mmapobj_dummy
;
576 dev_a
.dev
= makedevice(mm_major
, M_NULL
);
578 dev_a
.type
= 0; /* neither PRIVATE nor SHARED */
579 dev_a
.prot
= dev_a
.maxprot
= (uchar_t
)PROT_NONE
;
583 (void) as_map(as
, mrp
[i
].mr_addr
, mrp
[i
].mr_msize
,
584 segdev_create
, &dev_a
);
585 MOBJ_STAT_ADD(remap_devnull
);
590 if (num_mapped
!= num_segs
) {
591 ASSERT(e_type
== ET_DYN
);
592 /* Need to unmap any reservation made after last mapped seg */
593 if (num_mapped
== 0) {
594 addr
= mrp
[0].mr_addr
;
596 addr
= mrp
[num_mapped
- 1].mr_addr
+
597 mrp
[num_mapped
- 1].mr_msize
;
599 size
= (size_t)mrp
[num_segs
- 1].mr_addr
+
600 mrp
[num_segs
- 1].mr_msize
- (size_t)addr
;
601 (void) as_unmap(as
, addr
, size
);
604 * Now we need to unmap the holes between mapped segs.
605 * Note that we have not mapped all of the segments and thus
606 * the holes between segments would not have been unmapped
607 * yet. If num_mapped == num_segs, then all of the holes
608 * between segments would have already been unmapped.
611 for (i
= 1; i
< num_mapped
; i
++) {
612 addr
= mrp
[i
- 1].mr_addr
+ mrp
[i
- 1].mr_msize
;
613 size
= mrp
[i
].mr_addr
- addr
;
614 (void) as_unmap(as
, addr
, size
);
620 * We need to add the start address into mrp so that the unmap function
621 * has absolute addresses to use.
624 mmapobj_unmap_exec(mmapobj_result_t
*mrp
, int num_mapped
, caddr_t start_addr
)
628 for (i
= 0; i
< num_mapped
; i
++) {
629 mrp
[i
].mr_addr
+= (size_t)start_addr
;
631 mmapobj_unmap(mrp
, num_mapped
, num_mapped
, ET_EXEC
);
635 mmapobj_lookup_start_addr(struct lib_va
*lvp
)
638 struct as
*as
= p
->p_as
;
639 struct segvn_crargs crargs
= SEGVN_ZFOD_ARGS(PROT_USER
, PROT_ALL
);
641 uint_t ma_flags
= _MAP_LOW32
;
647 MOBJ_STAT_ADD(lookup_start
);
651 base
= lvp
->lv_base_va
;
655 * If we don't have an expected base address, or the one that we want
656 * to use is not available or acceptable, go get an acceptable
659 if (base
== NULL
|| as_gap(as
, len
, &base
, &len
, 0, NULL
) ||
660 valid_usr_range(base
, len
, PROT_ALL
, as
, as
->a_userlimit
) !=
661 RANGE_OKAY
|| OVERLAPS_STACK(base
+ len
, p
)) {
662 if (lvp
->lv_flags
& LV_ELF64
) {
666 align
= lvp
->lv_align
;
668 ma_flags
|= MAP_ALIGN
;
671 base
= (caddr_t
)align
;
672 map_addr(&base
, len
, 0, 1, ma_flags
);
676 * Need to reserve the address space we're going to use.
677 * Don't reserve swap space since we'll be mapping over this.
680 crargs
.flags
|= MAP_NORESERVE
;
681 error
= as_map(as
, base
, len
, segvn_create
, &crargs
);
692 * Get the starting address for a given file to be mapped and return it
693 * to the caller. If we're using lib_va and we need to allocate an address,
694 * we will attempt to allocate it from the global reserved pool such that the
695 * same address can be used in the future for this file. If we can't use the
696 * reserved address then we just get one that will fit in our address space.
698 * Returns the starting virtual address for the range to be mapped or NULL
699 * if an error is encountered. If we successfully insert the requested info
700 * into the lib_va hash, then *lvpp will be set to point to this lib_va
701 * structure. The structure will have a hold on it and thus lib_va_release
702 * needs to be called on it by the caller. This function will not fill out
703 * lv_mps or lv_num_segs since it does not have enough information to do so.
704 * The caller is responsible for doing this making sure that any modifications
705 * to lv_mps are visible before setting lv_num_segs.
708 mmapobj_alloc_start_addr(struct lib_va
**lvpp
, size_t len
, int use_lib_va
,
709 int randomize
, size_t align
, vattr_t
*vap
)
712 struct as
*as
= p
->p_as
;
713 struct segvn_crargs crargs
= SEGVN_ZFOD_ARGS(PROT_USER
, PROT_ALL
);
716 uint_t ma_flags
= _MAP_LOW32
;
723 ASSERT(lvpp
!= NULL
);
724 ASSERT((randomize
& use_lib_va
) != 1);
726 MOBJ_STAT_ADD(alloc_start
);
727 model
= get_udatamodel();
729 if (model
== DATAMODEL_LP64
) {
731 model_vmem
= lib_va_64_arena
;
733 ASSERT(model
== DATAMODEL_ILP32
);
734 model_vmem
= lib_va_32_arena
;
738 ma_flags
|= MAP_ALIGN
;
742 ma_flags
|= _MAP_RANDOMIZE
;
746 * The first time through, we need to setup the lib_va arenas.
747 * We call map_addr to find a suitable range of memory to map
748 * the given library, and we will set the highest address
749 * in our vmem arena to the end of this adddress range.
750 * We allow up to half of the address space to be used
751 * for lib_va addresses but we do not prevent any allocations
752 * in this range from other allocation paths.
754 if (lib_va_64_arena
== NULL
&& model
== DATAMODEL_LP64
) {
755 mutex_enter(&lib_va_init_mutex
);
756 if (lib_va_64_arena
== NULL
) {
757 base
= (caddr_t
)align
;
759 map_addr(&base
, len
, 0, 1, ma_flags
);
762 mutex_exit(&lib_va_init_mutex
);
763 MOBJ_STAT_ADD(lib_va_create_failure
);
766 lib_va_end
= (size_t)base
+ len
;
767 lib_va_len
= lib_va_end
>> 1;
768 lib_va_len
= P2ROUNDUP(lib_va_len
, PAGESIZE
);
769 lib_va_start
= lib_va_end
- lib_va_len
;
772 * Need to make sure we avoid the address hole.
773 * We know lib_va_end is valid but we need to
774 * make sure lib_va_start is as well.
776 if ((lib_va_end
> (size_t)hole_end
) &&
777 (lib_va_start
< (size_t)hole_end
)) {
778 lib_va_start
= P2ROUNDUP(
779 (size_t)hole_end
, PAGESIZE
);
780 lib_va_len
= lib_va_end
- lib_va_start
;
782 lib_va_64_arena
= vmem_create("lib_va_64",
783 (void *)lib_va_start
, lib_va_len
, PAGESIZE
,
785 VM_NOSLEEP
| VMC_IDENTIFIER
);
786 if (lib_va_64_arena
== NULL
) {
787 mutex_exit(&lib_va_init_mutex
);
791 model_vmem
= lib_va_64_arena
;
792 mutex_exit(&lib_va_init_mutex
);
793 } else if (lib_va_32_arena
== NULL
&&
794 model
== DATAMODEL_ILP32
) {
795 mutex_enter(&lib_va_init_mutex
);
796 if (lib_va_32_arena
== NULL
) {
797 base
= (caddr_t
)align
;
799 map_addr(&base
, len
, 0, 1, ma_flags
);
802 mutex_exit(&lib_va_init_mutex
);
803 MOBJ_STAT_ADD(lib_va_create_failure
);
806 lib_va_end
= (size_t)base
+ len
;
807 lib_va_len
= lib_va_end
>> 1;
808 lib_va_len
= P2ROUNDUP(lib_va_len
, PAGESIZE
);
809 lib_va_start
= lib_va_end
- lib_va_len
;
810 lib_va_32_arena
= vmem_create("lib_va_32",
811 (void *)lib_va_start
, lib_va_len
, PAGESIZE
,
813 VM_NOSLEEP
| VMC_IDENTIFIER
);
814 if (lib_va_32_arena
== NULL
) {
815 mutex_exit(&lib_va_init_mutex
);
819 model_vmem
= lib_va_32_arena
;
820 mutex_exit(&lib_va_init_mutex
);
823 if (model
== DATAMODEL_LP64
|| libs_mapped_32
< lib_threshold
) {
824 base
= vmem_xalloc(model_vmem
, len
, align
, 0, 0, NULL
,
825 NULL
, VM_NOSLEEP
| VM_ENDALLOC
);
826 MOBJ_STAT_ADD(alloc_vmem
);
830 * Even if the address fails to fit in our address space,
831 * or we can't use a reserved address,
832 * we should still save it off in lib_va_hash.
834 *lvpp
= lib_va_add_hash(base
, len
, align
, vap
);
837 * Check for collision on insertion and free up our VA space.
838 * This is expected to be rare, so we'll just reset base to
839 * NULL instead of looking it up in the lib_va hash.
843 vmem_xfree(model_vmem
, base
, len
);
845 MOBJ_STAT_ADD(add_collision
);
854 * If we don't have an expected base address, or the one that we want
855 * to use is not available or acceptable, go get an acceptable
858 * If ASLR is enabled, we should never have used the cache, and should
859 * also start our real work here, in the consequent of the next
863 ASSERT(base
== NULL
);
865 if (base
== NULL
|| as_gap(as
, len
, &base
, &len
, 0, NULL
) ||
866 valid_usr_range(base
, len
, PROT_ALL
, as
, as
->a_userlimit
) !=
867 RANGE_OKAY
|| OVERLAPS_STACK(base
+ len
, p
)) {
868 MOBJ_STAT_ADD(get_addr
);
869 base
= (caddr_t
)align
;
870 map_addr(&base
, len
, 0, 1, ma_flags
);
874 * Need to reserve the address space we're going to use.
875 * Don't reserve swap space since we'll be mapping over this.
878 /* Don't reserve swap space since we'll be mapping over this */
879 crargs
.flags
|= MAP_NORESERVE
;
880 error
= as_map(as
, base
, len
, segvn_create
, &crargs
);
891 * Map the file associated with vp into the address space as a single
892 * read only private mapping.
893 * Returns 0 for success, and non-zero for failure to map the file.
896 mmapobj_map_flat(vnode_t
*vp
, mmapobj_result_t
*mrp
, size_t padding
,
900 struct as
*as
= curproc
->p_as
;
905 int prot
= PROT_USER
| PROT_READ
;
906 uint_t ma_flags
= _MAP_LOW32
;
908 struct segvn_crargs crargs
= SEGVN_ZFOD_ARGS(PROT_USER
, PROT_ALL
);
910 if (get_udatamodel() == DATAMODEL_LP64
) {
914 vattr
.va_mask
= AT_SIZE
;
915 error
= VOP_GETATTR(vp
, &vattr
, 0, fcred
, NULL
);
922 ma_flags
|= MAP_PRIVATE
;
924 MOBJ_STAT_ADD(map_flat_no_padding
);
925 error
= VOP_MAP(vp
, 0, as
, &addr
, len
, prot
, PROT_ALL
,
926 ma_flags
, fcred
, NULL
);
928 mrp
[0].mr_addr
= addr
;
929 mrp
[0].mr_msize
= len
;
930 mrp
[0].mr_fsize
= len
;
931 mrp
[0].mr_offset
= 0;
932 mrp
[0].mr_prot
= prot
;
938 /* padding was requested so there's more work to be done */
939 MOBJ_STAT_ADD(map_flat_padding
);
941 /* No need to reserve swap space now since it will be reserved later */
942 crargs
.flags
|= MAP_NORESERVE
;
944 /* Need to setup padding which can only be in PAGESIZE increments. */
945 ASSERT((padding
& PAGEOFFSET
) == 0);
946 pad_len
= len
+ (2 * padding
);
949 map_addr(&addr
, pad_len
, 0, 1, ma_flags
);
950 error
= as_map(as
, addr
, pad_len
, segvn_create
, &crargs
);
957 ma_flags
|= MAP_FIXED
;
958 error
= VOP_MAP(vp
, 0, as
, &addr
, len
, prot
, PROT_ALL
, ma_flags
,
961 mrp
[0].mr_addr
= start_addr
;
962 mrp
[0].mr_msize
= padding
;
964 mrp
[0].mr_offset
= 0;
966 mrp
[0].mr_flags
= MR_PADDING
;
968 mrp
[1].mr_addr
= addr
;
969 mrp
[1].mr_msize
= len
;
970 mrp
[1].mr_fsize
= len
;
971 mrp
[1].mr_offset
= 0;
972 mrp
[1].mr_prot
= prot
;
975 mrp
[2].mr_addr
= addr
+ P2ROUNDUP(len
, PAGESIZE
);
976 mrp
[2].mr_msize
= padding
;
978 mrp
[2].mr_offset
= 0;
980 mrp
[2].mr_flags
= MR_PADDING
;
982 /* Need to cleanup the as_map from earlier */
983 (void) as_unmap(as
, start_addr
, pad_len
);
989 * Map a PT_LOAD or PT_SUNWBSS section of an executable file into the user's
991 * vp - vnode to be mapped in
992 * addr - start address
993 * len - length of vp to be mapped
994 * zfodlen - length of zero filled memory after len above
995 * offset - offset into file where mapping should start
996 * prot - protections for this mapping
997 * fcred - credentials for the file associated with vp at open time.
1000 mmapobj_map_ptload(struct vnode
*vp
, caddr_t addr
, size_t len
, size_t zfodlen
,
1001 off_t offset
, int prot
, cred_t
*fcred
)
1004 caddr_t zfodbase
, oldaddr
;
1009 struct as
*as
= curproc
->p_as
;
1014 * See if addr and offset are aligned such that we can map in
1015 * full pages instead of partial pages.
1017 full_page
= (((uintptr_t)addr
& PAGEOFFSET
) ==
1018 ((uintptr_t)offset
& PAGEOFFSET
));
1020 model
= get_udatamodel();
1023 addr
= (caddr_t
)((uintptr_t)addr
& (uintptr_t)PAGEMASK
);
1025 spgcnt_t availm
, npages
;
1027 uint_t mflag
= MAP_PRIVATE
| MAP_FIXED
;
1029 if (model
== DATAMODEL_ILP32
) {
1030 mflag
|= _MAP_LOW32
;
1032 /* We may need to map in extra bytes */
1034 len
+= ((size_t)oldaddr
& PAGEOFFSET
);
1037 offset
= (off_t
)((uintptr_t)offset
& PAGEMASK
);
1038 if ((prot
& (PROT_WRITE
| PROT_EXEC
)) == PROT_EXEC
) {
1040 MOBJ_STAT_ADD(map_ptload_text
);
1042 mflag
|= MAP_INITDATA
;
1043 MOBJ_STAT_ADD(map_ptload_initdata
);
1047 * maxprot is passed as PROT_ALL so that mdb can
1048 * write to this segment.
1050 if (error
= VOP_MAP(vp
, (offset_t
)offset
, as
, &addr
,
1051 len
, prot
, PROT_ALL
, mflag
, fcred
, NULL
)) {
1056 * If the segment can fit and is relatively small, then
1057 * we prefault the entire segment in. This is based
1058 * on the model that says the best working set of a
1059 * small program is all of its pages.
1060 * We only do this if freemem will not drop below
1061 * lotsfree since we don't want to induce paging.
1063 npages
= (spgcnt_t
)btopr(len
);
1064 availm
= freemem
- lotsfree
;
1065 preread
= (npages
< availm
&& len
< PGTHRESH
) ? 1 : 0;
1068 * If we aren't prefaulting the segment,
1069 * increment "deficit", if necessary to ensure
1070 * that pages will become available when this
1071 * process starts executing.
1073 if (preread
== 0 && npages
> availm
&&
1074 deficit
< lotsfree
) {
1075 deficit
+= MIN((pgcnt_t
)(npages
- availm
),
1076 lotsfree
- deficit
);
1080 (void) as_faulta(as
, addr
, len
);
1081 MOBJ_STAT_ADD(map_ptload_preread
);
1085 * addr and offset were not aligned such that we could
1086 * use VOP_MAP, thus we need to as_map the memory we
1087 * need and then read the data in from disk.
1088 * This code path is a corner case which should never
1089 * be taken, but hand crafted binaries could trigger
1090 * this logic and it needs to work correctly.
1092 MOBJ_STAT_ADD(map_ptload_unaligned_text
);
1094 (void) as_unmap(as
, addr
, len
);
1097 * We use zfod_argsp because we need to be able to
1098 * write to the mapping and then we'll change the
1099 * protections later if they are incorrect.
1101 error
= as_map(as
, addr
, len
, segvn_create
, zfod_argsp
);
1104 MOBJ_STAT_ADD(map_ptload_unaligned_map_fail
);
1108 /* Now read in the data from disk */
1109 error
= vn_rdwr(UIO_READ
, vp
, oldaddr
, oldlen
, offset
,
1110 UIO_USERSPACE
, 0, (rlim64_t
)0, fcred
, NULL
);
1112 MOBJ_STAT_ADD(map_ptload_unaligned_read_fail
);
1117 * Now set protections.
1119 if (prot
!= PROT_ZFOD
) {
1120 (void) as_setprot(as
, addr
, len
, prot
);
1126 end
= (size_t)addr
+ len
;
1127 zfodbase
= (caddr_t
)P2ROUNDUP(end
, PAGESIZE
);
1128 zfoddiff
= (uintptr_t)zfodbase
- end
;
1131 * Before we go to zero the remaining space on the last
1132 * page, make sure we have write permission.
1134 * We need to be careful how we zero-fill the last page
1135 * if the protection does not include PROT_WRITE. Using
1136 * as_setprot() can cause the VM segment code to call
1137 * segvn_vpage(), which must allocate a page struct for
1138 * each page in the segment. If we have a very large
1139 * segment, this may fail, so we check for that, even
1140 * though we ignore other return values from as_setprot.
1142 MOBJ_STAT_ADD(zfoddiff
);
1143 if ((prot
& PROT_WRITE
) == 0) {
1144 if (as_setprot(as
, (caddr_t
)end
, zfoddiff
,
1145 prot
| PROT_WRITE
) == ENOMEM
)
1147 MOBJ_STAT_ADD(zfoddiff_nowrite
);
1149 if (on_fault(&ljb
)) {
1151 if ((prot
& PROT_WRITE
) == 0) {
1152 (void) as_setprot(as
, (caddr_t
)end
,
1157 uzero((void *)end
, zfoddiff
);
1161 * Remove write protection to return to original state
1163 if ((prot
& PROT_WRITE
) == 0) {
1164 (void) as_setprot(as
, (caddr_t
)end
,
1168 if (zfodlen
> zfoddiff
) {
1169 struct segvn_crargs crargs
=
1170 SEGVN_ZFOD_ARGS(prot
, PROT_ALL
);
1172 MOBJ_STAT_ADD(zfodextra
);
1173 zfodlen
-= zfoddiff
;
1174 crargs
.szc
= AS_MAP_NO_LPOOB
;
1178 (void) as_unmap(as
, (caddr_t
)zfodbase
, zfodlen
);
1179 error
= as_map(as
, (caddr_t
)zfodbase
,
1180 zfodlen
, segvn_create
, &crargs
);
1191 * Map the ELF file represented by vp into the users address space. The
1192 * first mapping will start at start_addr and there will be num_elements
1193 * mappings. The mappings are described by the data in mrp which may be
1194 * modified upon returning from this function.
1195 * Returns 0 for success or errno for failure.
1198 mmapobj_map_elf(struct vnode
*vp
, caddr_t start_addr
, mmapobj_result_t
*mrp
,
1199 int num_elements
, cred_t
*fcred
, ushort_t e_type
)
1205 struct as
*as
= curproc
->p_as
;
1207 for (i
= 0; i
< num_elements
; i
++) {
1216 /* Always need to adjust mr_addr */
1217 addr
= start_addr
+ (size_t)(mrp
[i
].mr_addr
);
1219 (caddr_t
)((uintptr_t)addr
& (uintptr_t)PAGEMASK
);
1221 /* Padding has already been mapped */
1222 if (MR_GET_TYPE(mrp
[i
].mr_flags
) == MR_PADDING
) {
1226 /* Can't execute code from "noexec" mounted filesystem. */
1227 if (((vp
->v_vfsp
->vfs_flag
& VFS_NOEXEC
) != 0) &&
1228 ((mrp
[i
].mr_prot
& PROT_EXEC
) != 0)) {
1229 MOBJ_STAT_ADD(noexec_fs
);
1233 p_memsz
= mrp
[i
].mr_msize
;
1234 p_filesz
= mrp
[i
].mr_fsize
;
1235 zfodlen
= p_memsz
- p_filesz
;
1236 p_offset
= mrp
[i
].mr_offset
;
1237 dif
= (uintptr_t)(addr
) & PAGEOFFSET
;
1238 prot
= mrp
[i
].mr_prot
| PROT_USER
;
1239 ret
= mmapobj_map_ptload(vp
, addr
, p_filesz
, zfodlen
,
1240 p_offset
, prot
, fcred
);
1242 MOBJ_STAT_ADD(ptload_failed
);
1243 mmapobj_unmap(mrp
, i
, num_elements
, e_type
);
1247 /* Need to cleanup mrp to reflect the actual values used */
1248 mrp
[i
].mr_msize
+= dif
;
1249 mrp
[i
].mr_offset
= (size_t)addr
& PAGEOFFSET
;
1252 /* Also need to unmap any holes created above */
1253 if (num_elements
== 1) {
1254 MOBJ_STAT_ADD(map_elf_no_holes
);
1257 if (e_type
== ET_EXEC
) {
1263 hi
= mrp
[0].mr_addr
;
1265 /* Remove holes made by the rest of the segments */
1266 for (i
= 0; i
< num_elements
- 1; i
++) {
1267 lo
= (caddr_t
)P2ROUNDUP((size_t)(mrp
[i
].mr_addr
) +
1268 mrp
[i
].mr_msize
, PAGESIZE
);
1269 hi
= mrp
[i
+ 1].mr_addr
;
1272 * If as_unmap fails we just use up a bit of extra
1275 (void) as_unmap(as
, (caddr_t
)lo
,
1276 (size_t)hi
- (size_t)lo
);
1277 MOBJ_STAT_ADD(unmap_hole
);
1285 /* Ugly hack to get STRUCT_* macros to work below */
1287 Phdr x
; /* native version */
1295 * Calculate and return the number of loadable segments in the ELF Phdr
1296 * represented by phdrbase as well as the len of the total mapping and
1297 * the max alignment that is needed for a given segment. On success,
1298 * 0 is returned, and *len, *loadable and *align have been filled out.
1299 * On failure, errno will be returned, which in this case is ENOTSUP
1300 * if we were passed an ELF file with overlapping segments.
1303 calc_loadable(Ehdr
*ehdrp
, caddr_t phdrbase
, int nphdrs
, size_t *len
,
1304 int *loadable
, size_t *align
)
1309 ushort_t e_type
= ehdrp
->e_type
; /* same offset 32 and 64 bit */
1316 caddr_t start_addr
= NULL
;
1317 caddr_t p_end
= NULL
;
1318 size_t max_align
= 0;
1319 size_t min_align
= PAGESIZE
; /* needed for vmem_xalloc */
1320 STRUCT_HANDLE(myphdr
, mph
);
1321 #if defined(__sparc)
1322 extern int vac_size
;
1325 * Want to prevent aliasing by making the start address at least be
1326 * aligned to vac_size.
1328 min_align
= MAX(PAGESIZE
, vac_size
);
1331 model
= get_udatamodel();
1332 STRUCT_SET_HANDLE(mph
, model
, (struct myphdr
*)phdrbase
);
1334 /* hsize alignment should have been checked before calling this func */
1335 if (model
== DATAMODEL_LP64
) {
1336 hsize
= ehdrp
->e_phentsize
;
1341 ASSERT(model
== DATAMODEL_ILP32
);
1342 hsize
= ((Elf32_Ehdr
*)ehdrp
)->e_phentsize
;
1349 * Determine the span of all loadable segments and calculate the
1350 * number of loadable segments.
1352 for (i
= 0; i
< nphdrs
; i
++) {
1353 p_type
= STRUCT_FGET(mph
, x
.p_type
);
1354 if (p_type
== PT_LOAD
|| p_type
== PT_SUNWBSS
) {
1355 vaddr
= (caddr_t
)(uintptr_t)STRUCT_FGET(mph
, x
.p_vaddr
);
1356 p_memsz
= STRUCT_FGET(mph
, x
.p_memsz
);
1359 * Skip this header if it requests no memory to be
1363 STRUCT_SET_HANDLE(mph
, model
,
1364 (struct myphdr
*)((size_t)STRUCT_BUF(mph
) +
1366 MOBJ_STAT_ADD(nomem_header
);
1369 if (num_segs
++ == 0) {
1371 * The p_vaddr of the first PT_LOAD segment
1372 * must either be NULL or within the first
1373 * page in order to be interpreted.
1374 * Otherwise, its an invalid file.
1376 if (e_type
== ET_DYN
&&
1377 ((caddr_t
)((uintptr_t)vaddr
&
1378 (uintptr_t)PAGEMASK
) != NULL
)) {
1379 MOBJ_STAT_ADD(inval_header
);
1384 * For the first segment, we need to map from
1385 * the beginning of the file, so we will
1386 * adjust the size of the mapping to include
1389 p_offset
= STRUCT_FGET(mph
, x
.p_offset
);
1394 * Check to make sure that this mapping wouldn't
1395 * overlap a previous mapping.
1397 if (vaddr
< p_end
) {
1398 MOBJ_STAT_ADD(overlap_header
);
1402 p_end
= vaddr
+ p_memsz
+ p_offset
;
1403 p_end
= (caddr_t
)P2ROUNDUP((size_t)p_end
, PAGESIZE
);
1405 p_align
= STRUCT_FGET(mph
, x
.p_align
);
1406 if (p_align
> 1 && p_align
> max_align
) {
1407 max_align
= p_align
;
1408 if (max_align
< min_align
) {
1409 max_align
= min_align
;
1410 MOBJ_STAT_ADD(min_align
);
1414 STRUCT_SET_HANDLE(mph
, model
,
1415 (struct myphdr
*)((size_t)STRUCT_BUF(mph
) + hsize
));
1419 * The alignment should be a power of 2, if it isn't we forgive it
1420 * and round up. On overflow, we'll set the alignment to max_align
1421 * rounded down to the nearest power of 2.
1423 if (max_align
> 0 && !ISP2(max_align
)) {
1424 MOBJ_STAT_ADD(np2_align
);
1425 *align
= 2 * (1L << (highbit(max_align
) - 1));
1426 if (*align
< max_align
||
1427 (*align
> UINT_MAX
&& model
== DATAMODEL_ILP32
)) {
1428 MOBJ_STAT_ADD(np2_align_overflow
);
1429 *align
= 1L << (highbit(max_align
) - 1);
1435 ASSERT(*align
>= PAGESIZE
|| *align
== 0);
1437 *loadable
= num_segs
;
1438 *len
= p_end
- start_addr
;
1443 * Check the address space to see if the virtual addresses to be used are
1444 * available. If they are not, return errno for failure. On success, 0
1445 * will be returned, and the virtual addresses for each mmapobj_result_t
1446 * will be reserved. Note that a reservation could have earlier been made
1447 * for a given segment via a /dev/null mapping. If that is the case, then
1448 * we can use that VA space for our mappings.
1449 * Note: this function will only be used for ET_EXEC binaries.
1452 check_exec_addrs(int loadable
, mmapobj_result_t
*mrp
, caddr_t start_addr
)
1455 struct as
*as
= curproc
->p_as
;
1456 struct segvn_crargs crargs
= SEGVN_ZFOD_ARGS(PROT_ZFOD
, PROT_ALL
);
1462 /* No need to reserve swap space now since it will be reserved later */
1463 crargs
.flags
|= MAP_NORESERVE
;
1465 for (i
= 0; i
< loadable
; i
++) {
1467 myaddr
= start_addr
+ (size_t)mrp
[i
].mr_addr
;
1468 mylen
= mrp
[i
].mr_msize
;
1470 /* See if there is a hole in the as for this range */
1471 if (as_gap(as
, mylen
, &myaddr
, &mylen
, 0, NULL
) == 0) {
1472 ASSERT(myaddr
== start_addr
+ (size_t)mrp
[i
].mr_addr
);
1473 ASSERT(mylen
== mrp
[i
].mr_msize
);
1476 if (MR_GET_TYPE(mrp
[i
].mr_flags
) == MR_PADDING
) {
1477 MOBJ_STAT_ADD(exec_padding
);
1480 ret
= as_map(as
, myaddr
, mylen
, segvn_create
, &crargs
);
1483 mmapobj_unmap_exec(mrp
, i
, start_addr
);
1488 * There is a mapping that exists in the range
1489 * so check to see if it was a "reservation"
1490 * from /dev/null. The mapping is from
1491 * /dev/null if the mapping comes from
1492 * segdev and the type is neither MAP_SHARED
1495 AS_LOCK_ENTER(as
, RW_READER
);
1496 seg
= as_findseg(as
, myaddr
, 0);
1497 MOBJ_STAT_ADD(exec_addr_mapped
);
1498 if (seg
&& seg
->s_ops
== &segdev_ops
&&
1499 ((segop_gettype(seg
, myaddr
) &
1500 (MAP_SHARED
| MAP_PRIVATE
)) == 0) &&
1501 myaddr
>= seg
->s_base
&&
1503 seg
->s_base
+ seg
->s_size
) {
1504 MOBJ_STAT_ADD(exec_addr_devnull
);
1506 (void) as_unmap(as
, myaddr
, mylen
);
1507 ret
= as_map(as
, myaddr
, mylen
, segvn_create
,
1509 mrp
[i
].mr_flags
|= MR_RESV
;
1512 /* Need to remap what we unmapped */
1513 mmapobj_unmap_exec(mrp
, i
+ 1,
1520 mmapobj_unmap_exec(mrp
, i
, start_addr
);
1521 MOBJ_STAT_ADD(exec_addr_in_use
);
1522 return (EADDRINUSE
);
1531 * Walk through the ELF program headers and extract all useful information
1532 * for PT_LOAD and PT_SUNWBSS segments into mrp.
1533 * Return 0 on success or error on failure.
1536 process_phdrs(Ehdr
*ehdrp
, caddr_t phdrbase
, int nphdrs
, mmapobj_result_t
*mrp
,
1537 vnode_t
*vp
, uint_t
*num_mapped
, size_t padding
, cred_t
*fcred
)
1540 caddr_t start_addr
= NULL
;
1546 struct lib_va
*lvp
= NULL
;
1548 struct as
*as
= curproc
->p_as
;
1556 ushort_t e_type
= ehdrp
->e_type
; /* same offset 32 and 64 bit */
1564 STRUCT_HANDLE(myphdr
, mph
);
1566 model
= get_udatamodel();
1567 STRUCT_SET_HANDLE(mph
, model
, (struct myphdr
*)phdrbase
);
1570 * Need to make sure that hsize is aligned properly.
1571 * For 32bit processes, 4 byte alignment is required.
1572 * For 64bit processes, 8 byte alignment is required.
1573 * If the alignment isn't correct, we need to return failure
1574 * since it could cause an alignment error panic while walking
1577 if (model
== DATAMODEL_LP64
) {
1578 hsize
= ehdrp
->e_phentsize
;
1580 MOBJ_STAT_ADD(phent_align64
);
1584 ASSERT(model
== DATAMODEL_ILP32
);
1585 hsize
= ((Elf32_Ehdr
*)ehdrp
)->e_phentsize
;
1587 MOBJ_STAT_ADD(phent_align32
);
1592 if ((padding
!= 0) || secflag_enabled(curproc
, PROC_SEC_ASLR
)) {
1595 if (e_type
== ET_DYN
) {
1596 vattr
.va_mask
= AT_FSID
| AT_NODEID
| AT_CTIME
| AT_MTIME
;
1597 error
= VOP_GETATTR(vp
, &vattr
, 0, fcred
, NULL
);
1601 /* Check to see if we already have a description for this lib */
1602 if (!secflag_enabled(curproc
, PROC_SEC_ASLR
))
1603 lvp
= lib_va_find(&vattr
);
1606 MOBJ_STAT_ADD(lvp_found
);
1608 start_addr
= mmapobj_lookup_start_addr(lvp
);
1609 if (start_addr
== NULL
) {
1610 lib_va_release(lvp
);
1616 * loadable may be zero if the original allocator
1617 * of lvp hasn't finished setting it up but the rest
1618 * of the fields will be accurate.
1620 loadable
= lvp
->lv_num_segs
;
1622 align
= lvp
->lv_align
;
1627 * Determine the span of all loadable segments and calculate the
1628 * number of loadable segments, the total len spanned by the mappings
1629 * and the max alignment, if we didn't get them above.
1631 if (loadable
== 0) {
1632 MOBJ_STAT_ADD(no_loadable_yet
);
1633 ret
= calc_loadable(ehdrp
, phdrbase
, nphdrs
, &len
,
1637 * Since it'd be an invalid file, we shouldn't have
1638 * cached it previously.
1640 ASSERT(lvp
== NULL
);
1645 ASSERT(len
== lvp
->lv_len
);
1646 ASSERT(align
== lvp
->lv_align
);
1651 /* Make sure there's something to map. */
1652 if (len
== 0 || loadable
== 0) {
1654 * Since it'd be an invalid file, we shouldn't have
1655 * cached it previously.
1657 ASSERT(lvp
== NULL
);
1658 MOBJ_STAT_ADD(nothing_to_map
);
1666 if (loadable
> *num_mapped
) {
1667 *num_mapped
= loadable
;
1668 /* cleanup previous reservation */
1670 (void) as_unmap(as
, start_addr
, lib_len
);
1672 MOBJ_STAT_ADD(e2big
);
1674 lib_va_release(lvp
);
1680 * We now know the size of the object to map and now we need to
1681 * get the start address to map it at. It's possible we already
1682 * have it if we found all the info we need in the lib_va cache.
1684 if (e_type
== ET_DYN
&& start_addr
== NULL
) {
1686 * Need to make sure padding does not throw off
1687 * required alignment. We can only specify an
1688 * alignment for the starting address to be mapped,
1689 * so we round padding up to the alignment and map
1690 * from there and then throw out the extra later.
1694 add_pad
= P2ROUNDUP(padding
, align
);
1696 MOBJ_STAT_ADD(dyn_pad_align
);
1698 MOBJ_STAT_ADD(dyn_pad_noalign
);
1699 len
+= padding
; /* at beginning */
1701 len
+= padding
; /* at end of mapping */
1704 * At this point, if lvp is non-NULL, then above we
1705 * already found it in the cache but did not get
1706 * the start address since we were not going to use lib_va.
1707 * Since we know that lib_va will not be used, it's safe
1708 * to call mmapobj_alloc_start_addr and know that lvp
1709 * will not be modified.
1711 ASSERT(lvp
? use_lib_va
== 0 : 1);
1712 start_addr
= mmapobj_alloc_start_addr(&lvp
, len
,
1714 secflag_enabled(curproc
, PROC_SEC_ASLR
),
1716 if (start_addr
== NULL
) {
1718 lib_va_release(lvp
);
1720 MOBJ_STAT_ADD(alloc_start_fail
);
1724 * If we can't cache it, no need to hang on to it.
1725 * Setting lv_num_segs to non-zero will make that
1726 * field active and since there are too many segments
1727 * to cache, all future users will not try to use lv_mps.
1729 if (lvp
!= NULL
&& loadable
> LIBVA_CACHED_SEGS
&& use_lib_va
) {
1730 lvp
->lv_num_segs
= loadable
;
1731 lib_va_release(lvp
);
1733 MOBJ_STAT_ADD(lvp_nocache
);
1736 * Free the beginning of the mapping if the padding
1737 * was not aligned correctly.
1739 if (padding
!= 0 && add_pad
!= padding
) {
1740 (void) as_unmap(as
, start_addr
,
1742 start_addr
+= (add_pad
- padding
);
1743 MOBJ_STAT_ADD(extra_padding
);
1748 * At this point, we have reserved the virtual address space
1749 * for our mappings. Now we need to start filling out the mrp
1750 * array to describe all of the individual mappings we are going
1752 * For ET_EXEC there has been no memory reservation since we are
1753 * using fixed addresses. While filling in the mrp array below,
1754 * we will have the first segment biased to start at addr 0
1755 * and the rest will be biased by this same amount. Thus if there
1756 * is padding, the first padding will start at addr 0, and the next
1757 * segment will start at the value of padding.
1760 /* We'll fill out padding later, so start filling in mrp at index 1 */
1765 /* If we have no more need for lvp let it go now */
1766 if (lvp
!= NULL
&& use_lib_va
== 0) {
1767 lib_va_release(lvp
);
1768 MOBJ_STAT_ADD(lvp_not_needed
);
1772 /* Now fill out the mrp structs from the program headers */
1773 STRUCT_SET_HANDLE(mph
, model
, (struct myphdr
*)phdrbase
);
1774 for (i
= 0; i
< nphdrs
; i
++) {
1775 p_type
= STRUCT_FGET(mph
, x
.p_type
);
1776 if (p_type
== PT_LOAD
|| p_type
== PT_SUNWBSS
) {
1777 vaddr
= (caddr_t
)(uintptr_t)STRUCT_FGET(mph
, x
.p_vaddr
);
1778 p_memsz
= STRUCT_FGET(mph
, x
.p_memsz
);
1779 p_filesz
= STRUCT_FGET(mph
, x
.p_filesz
);
1780 p_offset
= STRUCT_FGET(mph
, x
.p_offset
);
1781 p_flags
= STRUCT_FGET(mph
, x
.p_flags
);
1784 * Skip this header if it requests no memory to be
1788 STRUCT_SET_HANDLE(mph
, model
,
1789 (struct myphdr
*)((size_t)STRUCT_BUF(mph
) +
1791 MOBJ_STAT_ADD(no_mem_map_sz
);
1803 ASSERT(current
< loadable
);
1804 mrp
[current
].mr_msize
= p_memsz
;
1805 mrp
[current
].mr_fsize
= p_filesz
;
1806 mrp
[current
].mr_offset
= p_offset
;
1807 mrp
[current
].mr_prot
= prot
;
1809 if (hdr_seen
== 0 && p_filesz
!= 0) {
1810 mrp
[current
].mr_flags
= MR_HDR_ELF
;
1812 * We modify mr_offset because we
1813 * need to map the ELF header as well, and if
1814 * we didn't then the header could be left out
1815 * of the mapping that we will create later.
1816 * Since we're removing the offset, we need to
1817 * account for that in the other fields as well
1818 * since we will be mapping the memory from 0
1821 if (e_type
== ET_DYN
) {
1822 mrp
[current
].mr_offset
= 0;
1823 mrp
[current
].mr_msize
+= p_offset
;
1824 mrp
[current
].mr_fsize
+= p_offset
;
1826 ASSERT(e_type
== ET_EXEC
);
1828 * Save off the start addr which will be
1829 * our bias for the rest of the
1832 start_addr
= vaddr
- padding
;
1834 mrp
[current
].mr_addr
= (caddr_t
)padding
;
1837 if (e_type
== ET_EXEC
) {
1839 mrp
[current
].mr_addr
=
1840 vaddr
- (size_t)start_addr
;
1842 mrp
[current
].mr_addr
= vaddr
+ padding
;
1844 mrp
[current
].mr_flags
= 0;
1849 /* Move to next phdr */
1850 STRUCT_SET_HANDLE(mph
, model
,
1851 (struct myphdr
*)((size_t)STRUCT_BUF(mph
) +
1855 /* Now fill out the padding segments */
1857 mrp
[0].mr_addr
= NULL
;
1858 mrp
[0].mr_msize
= padding
;
1859 mrp
[0].mr_fsize
= 0;
1860 mrp
[0].mr_offset
= 0;
1862 mrp
[0].mr_flags
= MR_PADDING
;
1864 /* Setup padding for the last segment */
1865 ASSERT(current
== loadable
- 1);
1866 mrp
[current
].mr_addr
= (caddr_t
)lib_len
+ padding
;
1867 mrp
[current
].mr_msize
= padding
;
1868 mrp
[current
].mr_fsize
= 0;
1869 mrp
[current
].mr_offset
= 0;
1870 mrp
[current
].mr_prot
= 0;
1871 mrp
[current
].mr_flags
= MR_PADDING
;
1875 * Need to make sure address ranges desired are not in use or
1876 * are previously allocated reservations from /dev/null. For
1877 * ET_DYN, we already made sure our address range was free.
1879 if (e_type
== ET_EXEC
) {
1880 ret
= check_exec_addrs(loadable
, mrp
, start_addr
);
1882 ASSERT(lvp
== NULL
);
1883 MOBJ_STAT_ADD(check_exec_failed
);
1888 /* Finish up our business with lvp. */
1890 ASSERT(e_type
== ET_DYN
);
1891 if (lvp
->lv_num_segs
== 0 && loadable
<= LIBVA_CACHED_SEGS
) {
1892 bcopy(mrp
, lvp
->lv_mps
,
1893 loadable
* sizeof (mmapobj_result_t
));
1897 * Setting lv_num_segs to a non-zero value indicates that
1898 * lv_mps is now valid and can be used by other threads.
1899 * So, the above stores need to finish before lv_num_segs
1900 * is updated. lv_mps is only valid if lv_num_segs is
1901 * greater than LIBVA_CACHED_SEGS.
1903 lvp
->lv_num_segs
= loadable
;
1904 lib_va_release(lvp
);
1905 MOBJ_STAT_ADD(lvp_used
);
1908 /* Now that we have mrp completely filled out go map it */
1909 ret
= mmapobj_map_elf(vp
, start_addr
, mrp
, loadable
, fcred
, e_type
);
1911 *num_mapped
= loadable
;
1918 * Take the ELF file passed in, and do the work of mapping it.
1919 * num_mapped in - # elements in user buffer
1920 * num_mapped out - # sections mapped and length of mrp array if
1924 doelfwork(Ehdr
*ehdrp
, vnode_t
*vp
, mmapobj_result_t
*mrp
,
1925 uint_t
*num_mapped
, size_t padding
, cred_t
*fcred
)
1930 unsigned char ei_class
;
1931 unsigned short phentsize
;
1937 ei_class
= ehdrp
->e_ident
[EI_CLASS
];
1938 model
= get_udatamodel();
1939 if ((model
== DATAMODEL_ILP32
&& ei_class
== ELFCLASS64
) ||
1940 (model
== DATAMODEL_LP64
&& ei_class
== ELFCLASS32
)) {
1941 MOBJ_STAT_ADD(wrong_model
);
1945 /* Can't execute code from "noexec" mounted filesystem. */
1946 if (ehdrp
->e_type
== ET_EXEC
&&
1947 (vp
->v_vfsp
->vfs_flag
& VFS_NOEXEC
) != 0) {
1948 MOBJ_STAT_ADD(noexec_fs
);
1953 * Relocatable and core files are mapped as a single flat file
1954 * since no interpretation is done on them by mmapobj.
1956 if (ehdrp
->e_type
== ET_REL
|| ehdrp
->e_type
== ET_CORE
) {
1957 to_map
= padding
? 3 : 1;
1958 if (*num_mapped
< to_map
) {
1959 *num_mapped
= to_map
;
1960 MOBJ_STAT_ADD(e2big_et_rel
);
1963 error
= mmapobj_map_flat(vp
, mrp
, padding
, fcred
);
1965 *num_mapped
= to_map
;
1966 mrp
[padding
? 1 : 0].mr_flags
= MR_HDR_ELF
;
1967 MOBJ_STAT_ADD(et_rel_mapped
);
1972 /* Check for an unknown ELF type */
1973 if (ehdrp
->e_type
!= ET_EXEC
&& ehdrp
->e_type
!= ET_DYN
) {
1974 MOBJ_STAT_ADD(unknown_elf_type
);
1978 if (ei_class
== ELFCLASS32
) {
1979 Elf32_Ehdr
*e32hdr
= (Elf32_Ehdr
*)ehdrp
;
1980 ASSERT(model
== DATAMODEL_ILP32
);
1981 nphdrs
= e32hdr
->e_phnum
;
1982 phentsize
= e32hdr
->e_phentsize
;
1983 if (phentsize
< sizeof (Elf32_Phdr
)) {
1984 MOBJ_STAT_ADD(phent32_too_small
);
1987 phoff
= e32hdr
->e_phoff
;
1988 } else if (ei_class
== ELFCLASS64
) {
1989 Elf64_Ehdr
*e64hdr
= (Elf64_Ehdr
*)ehdrp
;
1990 ASSERT(model
== DATAMODEL_LP64
);
1991 nphdrs
= e64hdr
->e_phnum
;
1992 phentsize
= e64hdr
->e_phentsize
;
1993 if (phentsize
< sizeof (Elf64_Phdr
)) {
1994 MOBJ_STAT_ADD(phent64_too_small
);
1997 phoff
= e64hdr
->e_phoff
;
1999 /* fallthrough case for an invalid ELF class */
2000 MOBJ_STAT_ADD(inval_elf_class
);
2005 * nphdrs should only have this value for core files which are handled
2006 * above as a single mapping. If other file types ever use this
2007 * sentinel, then we'll add the support needed to handle this here.
2009 if (nphdrs
== PN_XNUM
) {
2010 MOBJ_STAT_ADD(too_many_phdrs
);
2014 phsizep
= nphdrs
* phentsize
;
2017 MOBJ_STAT_ADD(no_phsize
);
2021 /* Make sure we only wait for memory if it's a reasonable request */
2022 if (phsizep
> mmapobj_alloc_threshold
) {
2023 MOBJ_STAT_ADD(phsize_large
);
2024 if ((phbasep
= kmem_alloc(phsizep
, KM_NOSLEEP
)) == NULL
) {
2025 MOBJ_STAT_ADD(phsize_xtralarge
);
2029 phbasep
= kmem_alloc(phsizep
, KM_SLEEP
);
2032 if ((error
= vn_rdwr(UIO_READ
, vp
, phbasep
, phsizep
,
2033 (offset_t
)phoff
, UIO_SYSSPACE
, 0, (rlim64_t
)0,
2034 fcred
, NULL
)) != 0) {
2035 kmem_free(phbasep
, phsizep
);
2039 /* Now process the phdr's */
2040 error
= process_phdrs(ehdrp
, phbasep
, nphdrs
, mrp
, vp
, num_mapped
,
2042 kmem_free(phbasep
, phsizep
);
2047 * These are the two types of files that we can interpret and we want to read
2048 * in enough info to cover both types when looking at the initial header.
2050 #define MAX_HEADER_SIZE (MAX(sizeof (Ehdr), sizeof (struct exec)))
2053 * Map vp passed in in an interpreted manner. ELF and AOUT files will be
2054 * interpreted and mapped appropriately for execution.
2055 * num_mapped in - # elements in mrp
2056 * num_mapped out - # sections mapped and length of mrp array if
2057 * no errors or E2BIG returned.
2059 * Returns 0 on success, errno value on failure.
2062 mmapobj_map_interpret(vnode_t
*vp
, mmapobj_result_t
*mrp
,
2063 uint_t
*num_mapped
, size_t padding
, cred_t
*fcred
)
2072 * header has to be aligned to the native size of ulong_t in order
2073 * to avoid an unaligned access when dereferencing the header as
2074 * a ulong_t. Thus we allocate our array on the stack of type
2075 * ulong_t and then have header, which we dereference later as a char
2076 * array point at lheader.
2078 ulong_t lheader
[(MAX_HEADER_SIZE
/ (sizeof (ulong_t
))) + 1];
2079 caddr_t header
= (caddr_t
)&lheader
;
2081 vattr
.va_mask
= AT_FSID
| AT_NODEID
| AT_CTIME
| AT_MTIME
| AT_SIZE
;
2082 error
= VOP_GETATTR(vp
, &vattr
, 0, fcred
, NULL
);
2088 * Check lib_va to see if we already have a full description
2089 * for this library. This is the fast path and only used for
2090 * ET_DYN ELF files (dynamic libraries).
2092 if (padding
== 0 && !secflag_enabled(curproc
, PROC_SEC_ASLR
) &&
2093 ((lvp
= lib_va_find(&vattr
)) != NULL
)) {
2096 model
= get_udatamodel();
2097 if ((model
== DATAMODEL_ILP32
&&
2098 lvp
->lv_flags
& LV_ELF64
) ||
2099 (model
== DATAMODEL_LP64
&&
2100 lvp
->lv_flags
& LV_ELF32
)) {
2101 lib_va_release(lvp
);
2102 MOBJ_STAT_ADD(fast_wrong_model
);
2105 num_segs
= lvp
->lv_num_segs
;
2106 if (*num_mapped
< num_segs
) {
2107 *num_mapped
= num_segs
;
2108 lib_va_release(lvp
);
2109 MOBJ_STAT_ADD(fast_e2big
);
2114 * Check to see if we have all the mappable program headers
2117 if (num_segs
<= LIBVA_CACHED_SEGS
&& num_segs
!= 0) {
2118 MOBJ_STAT_ADD(fast
);
2119 start_addr
= mmapobj_lookup_start_addr(lvp
);
2120 if (start_addr
== NULL
) {
2121 lib_va_release(lvp
);
2125 bcopy(lvp
->lv_mps
, mrp
,
2126 num_segs
* sizeof (mmapobj_result_t
));
2128 error
= mmapobj_map_elf(vp
, start_addr
, mrp
,
2129 num_segs
, fcred
, ET_DYN
);
2131 lib_va_release(lvp
);
2133 *num_mapped
= num_segs
;
2134 MOBJ_STAT_ADD(fast_success
);
2138 MOBJ_STAT_ADD(fast_not_now
);
2140 /* Release it for now since we'll look it up below */
2141 lib_va_release(lvp
);
2145 * Time to see if this is a file we can interpret. If it's smaller
2146 * than this, then we can't interpret it.
2148 if (vattr
.va_size
< MAX_HEADER_SIZE
) {
2149 MOBJ_STAT_ADD(small_file
);
2153 if ((error
= vn_rdwr(UIO_READ
, vp
, header
, MAX_HEADER_SIZE
, 0,
2154 UIO_SYSSPACE
, 0, (rlim64_t
)0, fcred
, NULL
)) != 0) {
2155 MOBJ_STAT_ADD(read_error
);
2159 /* Verify file type */
2160 if (header
[EI_MAG0
] == ELFMAG0
&& header
[EI_MAG1
] == ELFMAG1
&&
2161 header
[EI_MAG2
] == ELFMAG2
&& header
[EI_MAG3
] == ELFMAG3
) {
2162 return (doelfwork((Ehdr
*)lheader
, vp
, mrp
, num_mapped
,
2166 /* Unsupported type */
2167 MOBJ_STAT_ADD(unsupported
);
2172 * Given a vnode, map it as either a flat file or interpret it and map
2173 * it according to the rules of the file type.
2174 * *num_mapped will contain the size of the mmapobj_result_t array passed in.
2175 * If padding is non-zero, the mappings will be padded by that amount
2176 * rounded up to the nearest pagesize.
2177 * If the mapping is successful, *num_mapped will contain the number of
2178 * distinct mappings created, and mrp will point to the array of
2179 * mmapobj_result_t's which describe these mappings.
2181 * On error, -1 is returned and errno is set appropriately.
2182 * A special error case will set errno to E2BIG when there are more than
2183 * *num_mapped mappings to be created and *num_mapped will be set to the
2184 * number of mappings needed.
2187 mmapobj(vnode_t
*vp
, uint_t flags
, mmapobj_result_t
*mrp
,
2188 uint_t
*num_mapped
, size_t padding
, cred_t
*fcred
)
2193 ASSERT((padding
& PAGEOFFSET
) == 0);
2194 ASSERT((flags
& ~MMOBJ_ALL_FLAGS
) == 0);
2195 ASSERT(num_mapped
!= NULL
);
2196 ASSERT((flags
& MMOBJ_PADDING
) ? padding
!= 0 : padding
== 0);
2198 if ((flags
& MMOBJ_INTERPRET
) == 0) {
2199 to_map
= padding
? 3 : 1;
2200 if (*num_mapped
< to_map
) {
2201 *num_mapped
= to_map
;
2202 MOBJ_STAT_ADD(flat_e2big
);
2205 error
= mmapobj_map_flat(vp
, mrp
, padding
, fcred
);
2210 *num_mapped
= to_map
;
2214 error
= mmapobj_map_interpret(vp
, mrp
, num_mapped
, padding
, fcred
);