18212SMichael.Corcoran@Sun.COM /*
28212SMichael.Corcoran@Sun.COM * CDDL HEADER START
38212SMichael.Corcoran@Sun.COM *
48212SMichael.Corcoran@Sun.COM * The contents of this file are subject to the terms of the
58212SMichael.Corcoran@Sun.COM * Common Development and Distribution License (the "License").
68212SMichael.Corcoran@Sun.COM * You may not use this file except in compliance with the License.
78212SMichael.Corcoran@Sun.COM *
88212SMichael.Corcoran@Sun.COM * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
98212SMichael.Corcoran@Sun.COM * or http://www.opensolaris.org/os/licensing.
108212SMichael.Corcoran@Sun.COM * See the License for the specific language governing permissions
118212SMichael.Corcoran@Sun.COM * and limitations under the License.
128212SMichael.Corcoran@Sun.COM *
138212SMichael.Corcoran@Sun.COM * When distributing Covered Code, include this CDDL HEADER in each
148212SMichael.Corcoran@Sun.COM * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
158212SMichael.Corcoran@Sun.COM * If applicable, add the following below this CDDL HEADER, with the
168212SMichael.Corcoran@Sun.COM * fields enclosed by brackets "[]" replaced with your own identifying
178212SMichael.Corcoran@Sun.COM * information: Portions Copyright [yyyy] [name of copyright owner]
188212SMichael.Corcoran@Sun.COM *
198212SMichael.Corcoran@Sun.COM * CDDL HEADER END
208212SMichael.Corcoran@Sun.COM */
218212SMichael.Corcoran@Sun.COM /*
228578SMichael.Corcoran@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
238212SMichael.Corcoran@Sun.COM * Use is subject to license terms.
248212SMichael.Corcoran@Sun.COM */
258212SMichael.Corcoran@Sun.COM
268212SMichael.Corcoran@Sun.COM #include <sys/types.h>
278212SMichael.Corcoran@Sun.COM #include <sys/sysmacros.h>
288212SMichael.Corcoran@Sun.COM #include <sys/kmem.h>
298212SMichael.Corcoran@Sun.COM #include <sys/param.h>
308212SMichael.Corcoran@Sun.COM #include <sys/systm.h>
318212SMichael.Corcoran@Sun.COM #include <sys/errno.h>
328212SMichael.Corcoran@Sun.COM #include <sys/mman.h>
338212SMichael.Corcoran@Sun.COM #include <sys/cmn_err.h>
348212SMichael.Corcoran@Sun.COM #include <sys/cred.h>
358212SMichael.Corcoran@Sun.COM #include <sys/vmsystm.h>
368684SMichael.Corcoran@Sun.COM #include <sys/machsystm.h>
378212SMichael.Corcoran@Sun.COM #include <sys/debug.h>
388212SMichael.Corcoran@Sun.COM #include <vm/as.h>
398212SMichael.Corcoran@Sun.COM #include <vm/seg.h>
408212SMichael.Corcoran@Sun.COM #include <sys/vmparam.h>
418212SMichael.Corcoran@Sun.COM #include <sys/vfs.h>
428212SMichael.Corcoran@Sun.COM #include <sys/elf.h>
438212SMichael.Corcoran@Sun.COM #include <sys/machelf.h>
448212SMichael.Corcoran@Sun.COM #include <sys/corectl.h>
458212SMichael.Corcoran@Sun.COM #include <sys/exec.h>
468212SMichael.Corcoran@Sun.COM #include <sys/exechdr.h>
478212SMichael.Corcoran@Sun.COM #include <sys/autoconf.h>
488212SMichael.Corcoran@Sun.COM #include <sys/mem.h>
498212SMichael.Corcoran@Sun.COM #include <vm/seg_dev.h>
508212SMichael.Corcoran@Sun.COM #include <sys/vmparam.h>
518212SMichael.Corcoran@Sun.COM #include <sys/mmapobj.h>
528212SMichael.Corcoran@Sun.COM #include <sys/atomic.h>
538212SMichael.Corcoran@Sun.COM
548212SMichael.Corcoran@Sun.COM /*
558212SMichael.Corcoran@Sun.COM * Theory statement:
568212SMichael.Corcoran@Sun.COM *
578212SMichael.Corcoran@Sun.COM * The main driving force behind mmapobj is to interpret and map ELF files
588212SMichael.Corcoran@Sun.COM * inside of the kernel instead of having the linker be responsible for this.
598212SMichael.Corcoran@Sun.COM *
608212SMichael.Corcoran@Sun.COM * mmapobj also supports the AOUT 4.x binary format as well as flat files in
618212SMichael.Corcoran@Sun.COM * a read only manner.
628212SMichael.Corcoran@Sun.COM *
638212SMichael.Corcoran@Sun.COM * When interpreting and mapping an ELF file, mmapobj will map each PT_LOAD
648212SMichael.Corcoran@Sun.COM * or PT_SUNWBSS segment according to the ELF standard. Refer to the "Linker
658212SMichael.Corcoran@Sun.COM * and Libraries Guide" for more information about the standard and mapping
668212SMichael.Corcoran@Sun.COM * rules.
678212SMichael.Corcoran@Sun.COM *
688212SMichael.Corcoran@Sun.COM * Having mmapobj interpret and map objects will allow the kernel to make the
698212SMichael.Corcoran@Sun.COM * best decision for where to place the mappings for said objects. Thus, we
708212SMichael.Corcoran@Sun.COM * can make optimizations inside of the kernel for specific platforms or
718212SMichael.Corcoran@Sun.COM * cache mapping information to make mapping objects faster.
728212SMichael.Corcoran@Sun.COM *
738212SMichael.Corcoran@Sun.COM * The lib_va_hash will be one such optimization. For each ELF object that
748212SMichael.Corcoran@Sun.COM * mmapobj is asked to interpret, we will attempt to cache the information
758212SMichael.Corcoran@Sun.COM * about the PT_LOAD and PT_SUNWBSS sections to speed up future mappings of
768212SMichael.Corcoran@Sun.COM * the same objects. We will cache up to LIBVA_CACHED_SEGS (see below) program
778212SMichael.Corcoran@Sun.COM * headers which should cover a majority of the libraries out there without
788212SMichael.Corcoran@Sun.COM * wasting space. In order to make sure that the cached information is valid,
798212SMichael.Corcoran@Sun.COM * we check the passed in vnode's mtime and ctime to make sure the vnode
808212SMichael.Corcoran@Sun.COM * has not been modified since the last time we used it.
818212SMichael.Corcoran@Sun.COM *
828212SMichael.Corcoran@Sun.COM * In addition, the lib_va_hash may contain a preferred starting VA for the
838212SMichael.Corcoran@Sun.COM * object which can be useful for platforms which support a shared context.
848212SMichael.Corcoran@Sun.COM * This will increase the likelyhood that library text can be shared among
858212SMichael.Corcoran@Sun.COM * many different processes. We limit the reserved VA space for 32 bit objects
868212SMichael.Corcoran@Sun.COM * in order to minimize fragmenting the processes address space.
878212SMichael.Corcoran@Sun.COM *
888212SMichael.Corcoran@Sun.COM * In addition to the above, the mmapobj interface allows for padding to be
898212SMichael.Corcoran@Sun.COM * requested before the first mapping and after the last mapping created.
908212SMichael.Corcoran@Sun.COM * When padding is requested, no additional optimizations will be made for
918212SMichael.Corcoran@Sun.COM * that request.
928212SMichael.Corcoran@Sun.COM */
938212SMichael.Corcoran@Sun.COM
948212SMichael.Corcoran@Sun.COM /*
958212SMichael.Corcoran@Sun.COM * Threshold to prevent allocating too much kernel memory to read in the
968212SMichael.Corcoran@Sun.COM * program headers for an object. If it requires more than below,
978212SMichael.Corcoran@Sun.COM * we will use a KM_NOSLEEP allocation to allocate memory to hold all of the
988212SMichael.Corcoran@Sun.COM * program headers which could possibly fail. If less memory than below is
998212SMichael.Corcoran@Sun.COM * needed, then we use a KM_SLEEP allocation and are willing to wait for the
1008212SMichael.Corcoran@Sun.COM * memory if we need to.
1018212SMichael.Corcoran@Sun.COM */
1028212SMichael.Corcoran@Sun.COM size_t mmapobj_alloc_threshold = 65536;
1038212SMichael.Corcoran@Sun.COM
1048212SMichael.Corcoran@Sun.COM /* Debug stats for test coverage */
1058212SMichael.Corcoran@Sun.COM #ifdef DEBUG
1068212SMichael.Corcoran@Sun.COM struct mobj_stats {
1078212SMichael.Corcoran@Sun.COM uint_t mobjs_unmap_called;
1088212SMichael.Corcoran@Sun.COM uint_t mobjs_remap_devnull;
1098212SMichael.Corcoran@Sun.COM uint_t mobjs_lookup_start;
1108212SMichael.Corcoran@Sun.COM uint_t mobjs_alloc_start;
1118212SMichael.Corcoran@Sun.COM uint_t mobjs_alloc_vmem;
1128212SMichael.Corcoran@Sun.COM uint_t mobjs_add_collision;
1138212SMichael.Corcoran@Sun.COM uint_t mobjs_get_addr;
1148212SMichael.Corcoran@Sun.COM uint_t mobjs_map_flat_no_padding;
1158212SMichael.Corcoran@Sun.COM uint_t mobjs_map_flat_padding;
1168212SMichael.Corcoran@Sun.COM uint_t mobjs_map_ptload_text;
1178212SMichael.Corcoran@Sun.COM uint_t mobjs_map_ptload_initdata;
1188212SMichael.Corcoran@Sun.COM uint_t mobjs_map_ptload_preread;
1198212SMichael.Corcoran@Sun.COM uint_t mobjs_map_ptload_unaligned_text;
1208212SMichael.Corcoran@Sun.COM uint_t mobjs_map_ptload_unaligned_map_fail;
1218212SMichael.Corcoran@Sun.COM uint_t mobjs_map_ptload_unaligned_read_fail;
1228212SMichael.Corcoran@Sun.COM uint_t mobjs_zfoddiff;
1238212SMichael.Corcoran@Sun.COM uint_t mobjs_zfoddiff_nowrite;
1248212SMichael.Corcoran@Sun.COM uint_t mobjs_zfodextra;
1258212SMichael.Corcoran@Sun.COM uint_t mobjs_ptload_failed;
1268212SMichael.Corcoran@Sun.COM uint_t mobjs_map_elf_no_holes;
1278212SMichael.Corcoran@Sun.COM uint_t mobjs_unmap_hole;
1288212SMichael.Corcoran@Sun.COM uint_t mobjs_nomem_header;
1298578SMichael.Corcoran@Sun.COM uint_t mobjs_inval_header;
1308212SMichael.Corcoran@Sun.COM uint_t mobjs_overlap_header;
1318212SMichael.Corcoran@Sun.COM uint_t mobjs_np2_align;
1328212SMichael.Corcoran@Sun.COM uint_t mobjs_np2_align_overflow;
1338212SMichael.Corcoran@Sun.COM uint_t mobjs_exec_padding;
1348212SMichael.Corcoran@Sun.COM uint_t mobjs_exec_addr_mapped;
1358212SMichael.Corcoran@Sun.COM uint_t mobjs_exec_addr_devnull;
1368212SMichael.Corcoran@Sun.COM uint_t mobjs_exec_addr_in_use;
1378212SMichael.Corcoran@Sun.COM uint_t mobjs_lvp_found;
1388212SMichael.Corcoran@Sun.COM uint_t mobjs_no_loadable_yet;
1398212SMichael.Corcoran@Sun.COM uint_t mobjs_nothing_to_map;
1408212SMichael.Corcoran@Sun.COM uint_t mobjs_e2big;
1418212SMichael.Corcoran@Sun.COM uint_t mobjs_dyn_pad_align;
1428212SMichael.Corcoran@Sun.COM uint_t mobjs_dyn_pad_noalign;
1438212SMichael.Corcoran@Sun.COM uint_t mobjs_alloc_start_fail;
1448212SMichael.Corcoran@Sun.COM uint_t mobjs_lvp_nocache;
1458212SMichael.Corcoran@Sun.COM uint_t mobjs_extra_padding;
1468212SMichael.Corcoran@Sun.COM uint_t mobjs_lvp_not_needed;
1478212SMichael.Corcoran@Sun.COM uint_t mobjs_no_mem_map_sz;
1488212SMichael.Corcoran@Sun.COM uint_t mobjs_check_exec_failed;
1498212SMichael.Corcoran@Sun.COM uint_t mobjs_lvp_used;
1508212SMichael.Corcoran@Sun.COM uint_t mobjs_wrong_model;
1518212SMichael.Corcoran@Sun.COM uint_t mobjs_noexec_fs;
1528212SMichael.Corcoran@Sun.COM uint_t mobjs_e2big_et_rel;
1538212SMichael.Corcoran@Sun.COM uint_t mobjs_et_rel_mapped;
1548212SMichael.Corcoran@Sun.COM uint_t mobjs_unknown_elf_type;
1558212SMichael.Corcoran@Sun.COM uint_t mobjs_phent32_too_small;
1568212SMichael.Corcoran@Sun.COM uint_t mobjs_phent64_too_small;
1578212SMichael.Corcoran@Sun.COM uint_t mobjs_inval_elf_class;
1588212SMichael.Corcoran@Sun.COM uint_t mobjs_too_many_phdrs;
1598212SMichael.Corcoran@Sun.COM uint_t mobjs_no_phsize;
1608212SMichael.Corcoran@Sun.COM uint_t mobjs_phsize_large;
1618212SMichael.Corcoran@Sun.COM uint_t mobjs_phsize_xtralarge;
1628212SMichael.Corcoran@Sun.COM uint_t mobjs_fast_wrong_model;
1638212SMichael.Corcoran@Sun.COM uint_t mobjs_fast_e2big;
1648212SMichael.Corcoran@Sun.COM uint_t mobjs_fast;
1658212SMichael.Corcoran@Sun.COM uint_t mobjs_fast_success;
1668212SMichael.Corcoran@Sun.COM uint_t mobjs_fast_not_now;
1678212SMichael.Corcoran@Sun.COM uint_t mobjs_small_file;
1688212SMichael.Corcoran@Sun.COM uint_t mobjs_read_error;
1698212SMichael.Corcoran@Sun.COM uint_t mobjs_unsupported;
1708212SMichael.Corcoran@Sun.COM uint_t mobjs_flat_e2big;
1718212SMichael.Corcoran@Sun.COM uint_t mobjs_phent_align32;
1728212SMichael.Corcoran@Sun.COM uint_t mobjs_phent_align64;
1738212SMichael.Corcoran@Sun.COM uint_t mobjs_lib_va_find_hit;
1748212SMichael.Corcoran@Sun.COM uint_t mobjs_lib_va_find_delay_delete;
1758212SMichael.Corcoran@Sun.COM uint_t mobjs_lib_va_find_delete;
1768212SMichael.Corcoran@Sun.COM uint_t mobjs_lib_va_add_delay_delete;
1778212SMichael.Corcoran@Sun.COM uint_t mobjs_lib_va_add_delete;
1788684SMichael.Corcoran@Sun.COM uint_t mobjs_lib_va_create_failure;
1798362SMichael.Corcoran@Sun.COM uint_t mobjs_min_align;
1808212SMichael.Corcoran@Sun.COM #if defined(__sparc)
1818212SMichael.Corcoran@Sun.COM uint_t mobjs_aout_uzero_fault;
1828212SMichael.Corcoran@Sun.COM uint_t mobjs_aout_64bit_try;
1838212SMichael.Corcoran@Sun.COM uint_t mobjs_aout_noexec;
1848212SMichael.Corcoran@Sun.COM uint_t mobjs_aout_e2big;
1858212SMichael.Corcoran@Sun.COM uint_t mobjs_aout_lib;
1868212SMichael.Corcoran@Sun.COM uint_t mobjs_aout_fixed;
1878212SMichael.Corcoran@Sun.COM uint_t mobjs_aout_zfoddiff;
1888212SMichael.Corcoran@Sun.COM uint_t mobjs_aout_map_bss;
1898212SMichael.Corcoran@Sun.COM uint_t mobjs_aout_bss_fail;
1908212SMichael.Corcoran@Sun.COM uint_t mobjs_aout_nlist;
1918212SMichael.Corcoran@Sun.COM uint_t mobjs_aout_addr_in_use;
1928212SMichael.Corcoran@Sun.COM #endif
1938212SMichael.Corcoran@Sun.COM } mobj_stats;
1948212SMichael.Corcoran@Sun.COM
1958212SMichael.Corcoran@Sun.COM #define MOBJ_STAT_ADD(stat) ((mobj_stats.mobjs_##stat)++)
1968212SMichael.Corcoran@Sun.COM #else
1978212SMichael.Corcoran@Sun.COM #define MOBJ_STAT_ADD(stat)
1988212SMichael.Corcoran@Sun.COM #endif
1998212SMichael.Corcoran@Sun.COM
200*8947SMichael.Corcoran@Sun.COM /*
201*8947SMichael.Corcoran@Sun.COM * Check if addr is at or above the address space reserved for the stack.
202*8947SMichael.Corcoran@Sun.COM * The stack is at the top of the address space for all sparc processes
203*8947SMichael.Corcoran@Sun.COM * and 64 bit x86 processes. For 32 bit x86, the stack is not at the top
204*8947SMichael.Corcoran@Sun.COM * of the address space and thus this check wil always return false for
205*8947SMichael.Corcoran@Sun.COM * 32 bit x86 processes.
206*8947SMichael.Corcoran@Sun.COM */
207*8947SMichael.Corcoran@Sun.COM #if defined(__sparc)
208*8947SMichael.Corcoran@Sun.COM #define OVERLAPS_STACK(addr, p) \
209*8947SMichael.Corcoran@Sun.COM (addr >= (p->p_usrstack - ((p->p_stk_ctl + PAGEOFFSET) & PAGEMASK)))
210*8947SMichael.Corcoran@Sun.COM #elif defined(__amd64)
211*8947SMichael.Corcoran@Sun.COM #define OVERLAPS_STACK(addr, p) \
212*8947SMichael.Corcoran@Sun.COM ((p->p_model == DATAMODEL_LP64) && \
213*8947SMichael.Corcoran@Sun.COM (addr >= (p->p_usrstack - ((p->p_stk_ctl + PAGEOFFSET) & PAGEMASK))))
214*8947SMichael.Corcoran@Sun.COM #elif defined(__i386)
215*8947SMichael.Corcoran@Sun.COM #define OVERLAPS_STACK(addr, p) 0
216*8947SMichael.Corcoran@Sun.COM #endif
217*8947SMichael.Corcoran@Sun.COM
2188212SMichael.Corcoran@Sun.COM /* lv_flags values - bitmap */
2198212SMichael.Corcoran@Sun.COM #define LV_ELF32 0x1 /* 32 bit ELF file */
2208212SMichael.Corcoran@Sun.COM #define LV_ELF64 0x2 /* 64 bit ELF file */
2218212SMichael.Corcoran@Sun.COM #define LV_DEL 0x4 /* delete when lv_refcnt hits zero */
2228212SMichael.Corcoran@Sun.COM
2238212SMichael.Corcoran@Sun.COM /*
2248212SMichael.Corcoran@Sun.COM * Note: lv_num_segs will denote how many segments this file has and will
2258212SMichael.Corcoran@Sun.COM * only be set after the lv_mps array has been filled out.
2268212SMichael.Corcoran@Sun.COM * lv_mps can only be valid if lv_num_segs is non-zero.
2278212SMichael.Corcoran@Sun.COM */
2288212SMichael.Corcoran@Sun.COM struct lib_va {
2298212SMichael.Corcoran@Sun.COM struct lib_va *lv_next;
2308212SMichael.Corcoran@Sun.COM caddr_t lv_base_va; /* start va for library */
2318212SMichael.Corcoran@Sun.COM ssize_t lv_len; /* total va span of library */
2328212SMichael.Corcoran@Sun.COM size_t lv_align; /* minimum alignment */
2338212SMichael.Corcoran@Sun.COM uint64_t lv_nodeid; /* filesystem node id */
2348212SMichael.Corcoran@Sun.COM uint64_t lv_fsid; /* filesystem id */
2358212SMichael.Corcoran@Sun.COM timestruc_t lv_ctime; /* last time file was changed */
2368212SMichael.Corcoran@Sun.COM timestruc_t lv_mtime; /* or modified */
2378212SMichael.Corcoran@Sun.COM mmapobj_result_t lv_mps[LIBVA_CACHED_SEGS]; /* cached pheaders */
2388212SMichael.Corcoran@Sun.COM int lv_num_segs; /* # segs for this file */
2398212SMichael.Corcoran@Sun.COM int lv_flags;
2408212SMichael.Corcoran@Sun.COM uint_t lv_refcnt; /* number of holds on struct */
2418212SMichael.Corcoran@Sun.COM };
2428212SMichael.Corcoran@Sun.COM
2438212SMichael.Corcoran@Sun.COM #define LIB_VA_SIZE 1024
2448212SMichael.Corcoran@Sun.COM #define LIB_VA_MASK (LIB_VA_SIZE - 1)
2458212SMichael.Corcoran@Sun.COM #define LIB_VA_MUTEX_SHIFT 3
2468212SMichael.Corcoran@Sun.COM
2478212SMichael.Corcoran@Sun.COM #if (LIB_VA_SIZE & (LIB_VA_SIZE - 1))
2488212SMichael.Corcoran@Sun.COM #error "LIB_VA_SIZE is not a power of 2"
2498212SMichael.Corcoran@Sun.COM #endif
2508212SMichael.Corcoran@Sun.COM
2518212SMichael.Corcoran@Sun.COM static struct lib_va *lib_va_hash[LIB_VA_SIZE];
2528212SMichael.Corcoran@Sun.COM static kmutex_t lib_va_hash_mutex[LIB_VA_SIZE >> LIB_VA_MUTEX_SHIFT];
2538212SMichael.Corcoran@Sun.COM
2548212SMichael.Corcoran@Sun.COM #define LIB_VA_HASH_MUTEX(index) \
2558212SMichael.Corcoran@Sun.COM (&lib_va_hash_mutex[index >> LIB_VA_MUTEX_SHIFT])
2568212SMichael.Corcoran@Sun.COM
2578212SMichael.Corcoran@Sun.COM #define LIB_VA_HASH(nodeid) \
2588212SMichael.Corcoran@Sun.COM (((nodeid) ^ ((nodeid) << 7) ^ ((nodeid) << 13)) & LIB_VA_MASK)
2598212SMichael.Corcoran@Sun.COM
2608212SMichael.Corcoran@Sun.COM #define LIB_VA_MATCH_ID(arg1, arg2) \
2618212SMichael.Corcoran@Sun.COM ((arg1)->lv_nodeid == (arg2)->va_nodeid && \
2628212SMichael.Corcoran@Sun.COM (arg1)->lv_fsid == (arg2)->va_fsid)
2638212SMichael.Corcoran@Sun.COM
2648212SMichael.Corcoran@Sun.COM #define LIB_VA_MATCH_TIME(arg1, arg2) \
2658212SMichael.Corcoran@Sun.COM ((arg1)->lv_ctime.tv_sec == (arg2)->va_ctime.tv_sec && \
2668212SMichael.Corcoran@Sun.COM (arg1)->lv_mtime.tv_sec == (arg2)->va_mtime.tv_sec && \
2678212SMichael.Corcoran@Sun.COM (arg1)->lv_ctime.tv_nsec == (arg2)->va_ctime.tv_nsec && \
2688212SMichael.Corcoran@Sun.COM (arg1)->lv_mtime.tv_nsec == (arg2)->va_mtime.tv_nsec)
2698212SMichael.Corcoran@Sun.COM
2708212SMichael.Corcoran@Sun.COM #define LIB_VA_MATCH(arg1, arg2) \
2718212SMichael.Corcoran@Sun.COM (LIB_VA_MATCH_ID(arg1, arg2) && LIB_VA_MATCH_TIME(arg1, arg2))
2728212SMichael.Corcoran@Sun.COM
2738212SMichael.Corcoran@Sun.COM /*
2748684SMichael.Corcoran@Sun.COM * lib_va will be used for optimized allocation of address ranges for
2758684SMichael.Corcoran@Sun.COM * libraries, such that subsequent mappings of the same library will attempt
2768684SMichael.Corcoran@Sun.COM * to use the same VA as previous mappings of that library.
2778212SMichael.Corcoran@Sun.COM * In order to map libraries at the same VA in many processes, we need to carve
2788212SMichael.Corcoran@Sun.COM * out our own address space for them which is unique across many processes.
2798212SMichael.Corcoran@Sun.COM * We use different arenas for 32 bit and 64 bit libraries.
2808212SMichael.Corcoran@Sun.COM *
2818212SMichael.Corcoran@Sun.COM * Since the 32 bit address space is relatively small, we limit the number of
2828212SMichael.Corcoran@Sun.COM * libraries which try to use consistent virtual addresses to lib_threshold.
2838212SMichael.Corcoran@Sun.COM * For 64 bit libraries there is no such limit since the address space is large.
2848212SMichael.Corcoran@Sun.COM */
2858212SMichael.Corcoran@Sun.COM static vmem_t *lib_va_32_arena;
2868212SMichael.Corcoran@Sun.COM static vmem_t *lib_va_64_arena;
2878212SMichael.Corcoran@Sun.COM uint_t lib_threshold = 20; /* modifiable via /etc/system */
2888212SMichael.Corcoran@Sun.COM
2898684SMichael.Corcoran@Sun.COM static kmutex_t lib_va_init_mutex; /* no need to initialize */
2908684SMichael.Corcoran@Sun.COM
2918212SMichael.Corcoran@Sun.COM /*
2928212SMichael.Corcoran@Sun.COM * Number of 32 bit and 64 bit libraries in lib_va hash.
2938212SMichael.Corcoran@Sun.COM */
2948212SMichael.Corcoran@Sun.COM static uint_t libs_mapped_32 = 0;
2958212SMichael.Corcoran@Sun.COM static uint_t libs_mapped_64 = 0;
2968212SMichael.Corcoran@Sun.COM
2978212SMichael.Corcoran@Sun.COM /*
2988212SMichael.Corcoran@Sun.COM * Free up the resources associated with lvp as well as lvp itself.
2998212SMichael.Corcoran@Sun.COM * We also decrement the number of libraries mapped via a lib_va
3008212SMichael.Corcoran@Sun.COM * cached virtual address.
3018212SMichael.Corcoran@Sun.COM */
3028212SMichael.Corcoran@Sun.COM void
lib_va_free(struct lib_va * lvp)3038212SMichael.Corcoran@Sun.COM lib_va_free(struct lib_va *lvp)
3048212SMichael.Corcoran@Sun.COM {
3058212SMichael.Corcoran@Sun.COM int is_64bit = lvp->lv_flags & LV_ELF64;
3068212SMichael.Corcoran@Sun.COM ASSERT(lvp->lv_refcnt == 0);
3078212SMichael.Corcoran@Sun.COM
3088212SMichael.Corcoran@Sun.COM if (lvp->lv_base_va != NULL) {
3098212SMichael.Corcoran@Sun.COM vmem_xfree(is_64bit ? lib_va_64_arena : lib_va_32_arena,
3108212SMichael.Corcoran@Sun.COM lvp->lv_base_va, lvp->lv_len);
3118212SMichael.Corcoran@Sun.COM if (is_64bit) {
3128212SMichael.Corcoran@Sun.COM atomic_add_32(&libs_mapped_64, -1);
3138212SMichael.Corcoran@Sun.COM } else {
3148212SMichael.Corcoran@Sun.COM atomic_add_32(&libs_mapped_32, -1);
3158212SMichael.Corcoran@Sun.COM }
3168212SMichael.Corcoran@Sun.COM }
3178212SMichael.Corcoran@Sun.COM kmem_free(lvp, sizeof (struct lib_va));
3188212SMichael.Corcoran@Sun.COM }
3198212SMichael.Corcoran@Sun.COM
3208212SMichael.Corcoran@Sun.COM /*
3218212SMichael.Corcoran@Sun.COM * See if the file associated with the vap passed in is in the lib_va hash.
3228212SMichael.Corcoran@Sun.COM * If it is and the file has not been modified since last use, then
3238212SMichael.Corcoran@Sun.COM * return a pointer to that data. Otherwise, return NULL if the file has
3248212SMichael.Corcoran@Sun.COM * changed or the file was not found in the hash.
3258212SMichael.Corcoran@Sun.COM */
3268212SMichael.Corcoran@Sun.COM static struct lib_va *
lib_va_find(vattr_t * vap)3278212SMichael.Corcoran@Sun.COM lib_va_find(vattr_t *vap)
3288212SMichael.Corcoran@Sun.COM {
3298212SMichael.Corcoran@Sun.COM struct lib_va *lvp;
3308212SMichael.Corcoran@Sun.COM struct lib_va *del = NULL;
3318212SMichael.Corcoran@Sun.COM struct lib_va **tmp;
3328212SMichael.Corcoran@Sun.COM uint_t index;
3338212SMichael.Corcoran@Sun.COM index = LIB_VA_HASH(vap->va_nodeid);
3348212SMichael.Corcoran@Sun.COM
3358212SMichael.Corcoran@Sun.COM mutex_enter(LIB_VA_HASH_MUTEX(index));
3368212SMichael.Corcoran@Sun.COM tmp = &lib_va_hash[index];
3378212SMichael.Corcoran@Sun.COM while (*tmp != NULL) {
3388212SMichael.Corcoran@Sun.COM lvp = *tmp;
3398212SMichael.Corcoran@Sun.COM if (LIB_VA_MATCH_ID(lvp, vap)) {
3408212SMichael.Corcoran@Sun.COM if (LIB_VA_MATCH_TIME(lvp, vap)) {
3418212SMichael.Corcoran@Sun.COM ASSERT((lvp->lv_flags & LV_DEL) == 0);
3428212SMichael.Corcoran@Sun.COM lvp->lv_refcnt++;
3438212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(lib_va_find_hit);
3448212SMichael.Corcoran@Sun.COM } else {
3458212SMichael.Corcoran@Sun.COM /*
3468212SMichael.Corcoran@Sun.COM * file was updated since last use.
3478212SMichael.Corcoran@Sun.COM * need to remove it from list.
3488212SMichael.Corcoran@Sun.COM */
3498212SMichael.Corcoran@Sun.COM del = lvp;
3508212SMichael.Corcoran@Sun.COM *tmp = del->lv_next;
3518212SMichael.Corcoran@Sun.COM del->lv_next = NULL;
3528212SMichael.Corcoran@Sun.COM /*
3538212SMichael.Corcoran@Sun.COM * If we can't delete it now, mark it for later
3548212SMichael.Corcoran@Sun.COM */
3558212SMichael.Corcoran@Sun.COM if (del->lv_refcnt) {
3568212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(lib_va_find_delay_delete);
3578212SMichael.Corcoran@Sun.COM del->lv_flags |= LV_DEL;
3588212SMichael.Corcoran@Sun.COM del = NULL;
3598212SMichael.Corcoran@Sun.COM }
3608212SMichael.Corcoran@Sun.COM lvp = NULL;
3618212SMichael.Corcoran@Sun.COM }
3628212SMichael.Corcoran@Sun.COM mutex_exit(LIB_VA_HASH_MUTEX(index));
3638212SMichael.Corcoran@Sun.COM if (del) {
3648212SMichael.Corcoran@Sun.COM ASSERT(del->lv_refcnt == 0);
3658212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(lib_va_find_delete);
3668212SMichael.Corcoran@Sun.COM lib_va_free(del);
3678212SMichael.Corcoran@Sun.COM }
3688212SMichael.Corcoran@Sun.COM return (lvp);
3698212SMichael.Corcoran@Sun.COM }
3708212SMichael.Corcoran@Sun.COM tmp = &lvp->lv_next;
3718212SMichael.Corcoran@Sun.COM }
3728212SMichael.Corcoran@Sun.COM mutex_exit(LIB_VA_HASH_MUTEX(index));
3738212SMichael.Corcoran@Sun.COM return (NULL);
3748212SMichael.Corcoran@Sun.COM }
3758212SMichael.Corcoran@Sun.COM
3768212SMichael.Corcoran@Sun.COM /*
3778212SMichael.Corcoran@Sun.COM * Add a new entry to the lib_va hash.
3788212SMichael.Corcoran@Sun.COM * Search the hash while holding the appropriate mutex to make sure that the
3798212SMichael.Corcoran@Sun.COM * data is not already in the cache. If we find data that is in the cache
3808212SMichael.Corcoran@Sun.COM * already and has not been modified since last use, we return NULL. If it
3818212SMichael.Corcoran@Sun.COM * has been modified since last use, we will remove that entry from
3828212SMichael.Corcoran@Sun.COM * the hash and it will be deleted once it's reference count reaches zero.
3838212SMichael.Corcoran@Sun.COM * If there is no current entry in the hash we will add the new entry and
3848212SMichael.Corcoran@Sun.COM * return it to the caller who is responsible for calling lib_va_release to
3858212SMichael.Corcoran@Sun.COM * drop their reference count on it.
3868212SMichael.Corcoran@Sun.COM *
3878212SMichael.Corcoran@Sun.COM * lv_num_segs will be set to zero since the caller needs to add that
3888212SMichael.Corcoran@Sun.COM * information to the data structure.
3898212SMichael.Corcoran@Sun.COM */
3908212SMichael.Corcoran@Sun.COM static struct lib_va *
lib_va_add_hash(caddr_t base_va,ssize_t len,size_t align,vattr_t * vap)3918212SMichael.Corcoran@Sun.COM lib_va_add_hash(caddr_t base_va, ssize_t len, size_t align, vattr_t *vap)
3928212SMichael.Corcoran@Sun.COM {
3938212SMichael.Corcoran@Sun.COM struct lib_va *lvp;
3948212SMichael.Corcoran@Sun.COM uint_t index;
3958212SMichael.Corcoran@Sun.COM model_t model;
3968212SMichael.Corcoran@Sun.COM struct lib_va **tmp;
3978212SMichael.Corcoran@Sun.COM struct lib_va *del = NULL;
3988212SMichael.Corcoran@Sun.COM
3998212SMichael.Corcoran@Sun.COM model = get_udatamodel();
4008212SMichael.Corcoran@Sun.COM index = LIB_VA_HASH(vap->va_nodeid);
4018212SMichael.Corcoran@Sun.COM
4028212SMichael.Corcoran@Sun.COM lvp = kmem_alloc(sizeof (struct lib_va), KM_SLEEP);
4038212SMichael.Corcoran@Sun.COM
4048212SMichael.Corcoran@Sun.COM mutex_enter(LIB_VA_HASH_MUTEX(index));
4058212SMichael.Corcoran@Sun.COM
4068212SMichael.Corcoran@Sun.COM /*
4078212SMichael.Corcoran@Sun.COM * Make sure not adding same data a second time.
4088212SMichael.Corcoran@Sun.COM * The hash chains should be relatively short and adding
4098212SMichael.Corcoran@Sun.COM * is a relatively rare event, so it's worth the check.
4108212SMichael.Corcoran@Sun.COM */
4118212SMichael.Corcoran@Sun.COM tmp = &lib_va_hash[index];
4128212SMichael.Corcoran@Sun.COM while (*tmp != NULL) {
4138212SMichael.Corcoran@Sun.COM if (LIB_VA_MATCH_ID(*tmp, vap)) {
4148212SMichael.Corcoran@Sun.COM if (LIB_VA_MATCH_TIME(*tmp, vap)) {
4158212SMichael.Corcoran@Sun.COM mutex_exit(LIB_VA_HASH_MUTEX(index));
4168212SMichael.Corcoran@Sun.COM kmem_free(lvp, sizeof (struct lib_va));
4178212SMichael.Corcoran@Sun.COM return (NULL);
4188212SMichael.Corcoran@Sun.COM }
4198212SMichael.Corcoran@Sun.COM
4208212SMichael.Corcoran@Sun.COM /*
4218212SMichael.Corcoran@Sun.COM * We have the same nodeid and fsid but the file has
4228212SMichael.Corcoran@Sun.COM * been modified since we last saw it.
4238212SMichael.Corcoran@Sun.COM * Need to remove the old node and add this new
4248212SMichael.Corcoran@Sun.COM * one.
4258212SMichael.Corcoran@Sun.COM * Could probably use a callback mechanism to make
4268212SMichael.Corcoran@Sun.COM * this cleaner.
4278212SMichael.Corcoran@Sun.COM */
4288212SMichael.Corcoran@Sun.COM ASSERT(del == NULL);
4298212SMichael.Corcoran@Sun.COM del = *tmp;
4308212SMichael.Corcoran@Sun.COM *tmp = del->lv_next;
4318212SMichael.Corcoran@Sun.COM del->lv_next = NULL;
4328212SMichael.Corcoran@Sun.COM
4338212SMichael.Corcoran@Sun.COM /*
4348212SMichael.Corcoran@Sun.COM * Check to see if we can free it. If lv_refcnt
4358212SMichael.Corcoran@Sun.COM * is greater than zero, than some other thread
4368212SMichael.Corcoran@Sun.COM * has a reference to the one we want to delete
4378212SMichael.Corcoran@Sun.COM * and we can not delete it. All of this is done
4388212SMichael.Corcoran@Sun.COM * under the lib_va_hash_mutex lock so it is atomic.
4398212SMichael.Corcoran@Sun.COM */
4408212SMichael.Corcoran@Sun.COM if (del->lv_refcnt) {
4418212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(lib_va_add_delay_delete);
4428212SMichael.Corcoran@Sun.COM del->lv_flags |= LV_DEL;
4438212SMichael.Corcoran@Sun.COM del = NULL;
4448212SMichael.Corcoran@Sun.COM }
4458212SMichael.Corcoran@Sun.COM /* tmp is already advanced */
4468212SMichael.Corcoran@Sun.COM continue;
4478212SMichael.Corcoran@Sun.COM }
4488212SMichael.Corcoran@Sun.COM tmp = &((*tmp)->lv_next);
4498212SMichael.Corcoran@Sun.COM }
4508212SMichael.Corcoran@Sun.COM
4518212SMichael.Corcoran@Sun.COM lvp->lv_base_va = base_va;
4528212SMichael.Corcoran@Sun.COM lvp->lv_len = len;
4538212SMichael.Corcoran@Sun.COM lvp->lv_align = align;
4548212SMichael.Corcoran@Sun.COM lvp->lv_nodeid = vap->va_nodeid;
4558212SMichael.Corcoran@Sun.COM lvp->lv_fsid = vap->va_fsid;
4568212SMichael.Corcoran@Sun.COM lvp->lv_ctime.tv_sec = vap->va_ctime.tv_sec;
4578212SMichael.Corcoran@Sun.COM lvp->lv_ctime.tv_nsec = vap->va_ctime.tv_nsec;
4588212SMichael.Corcoran@Sun.COM lvp->lv_mtime.tv_sec = vap->va_mtime.tv_sec;
4598212SMichael.Corcoran@Sun.COM lvp->lv_mtime.tv_nsec = vap->va_mtime.tv_nsec;
4608212SMichael.Corcoran@Sun.COM lvp->lv_next = NULL;
4618212SMichael.Corcoran@Sun.COM lvp->lv_refcnt = 1;
4628212SMichael.Corcoran@Sun.COM
4638212SMichael.Corcoran@Sun.COM /* Caller responsible for filling this and lv_mps out */
4648212SMichael.Corcoran@Sun.COM lvp->lv_num_segs = 0;
4658212SMichael.Corcoran@Sun.COM
4668212SMichael.Corcoran@Sun.COM if (model == DATAMODEL_LP64) {
4678212SMichael.Corcoran@Sun.COM lvp->lv_flags = LV_ELF64;
4688212SMichael.Corcoran@Sun.COM } else {
4698212SMichael.Corcoran@Sun.COM ASSERT(model == DATAMODEL_ILP32);
4708212SMichael.Corcoran@Sun.COM lvp->lv_flags = LV_ELF32;
4718212SMichael.Corcoran@Sun.COM }
4728212SMichael.Corcoran@Sun.COM
4738212SMichael.Corcoran@Sun.COM if (base_va != NULL) {
4748212SMichael.Corcoran@Sun.COM if (model == DATAMODEL_LP64) {
4758212SMichael.Corcoran@Sun.COM atomic_add_32(&libs_mapped_64, 1);
4768212SMichael.Corcoran@Sun.COM } else {
4778212SMichael.Corcoran@Sun.COM ASSERT(model == DATAMODEL_ILP32);
4788212SMichael.Corcoran@Sun.COM atomic_add_32(&libs_mapped_32, 1);
4798212SMichael.Corcoran@Sun.COM }
4808212SMichael.Corcoran@Sun.COM }
4818212SMichael.Corcoran@Sun.COM ASSERT(*tmp == NULL);
4828212SMichael.Corcoran@Sun.COM *tmp = lvp;
4838212SMichael.Corcoran@Sun.COM mutex_exit(LIB_VA_HASH_MUTEX(index));
4848212SMichael.Corcoran@Sun.COM if (del) {
4858212SMichael.Corcoran@Sun.COM ASSERT(del->lv_refcnt == 0);
4868212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(lib_va_add_delete);
4878212SMichael.Corcoran@Sun.COM lib_va_free(del);
4888212SMichael.Corcoran@Sun.COM }
4898212SMichael.Corcoran@Sun.COM return (lvp);
4908212SMichael.Corcoran@Sun.COM }
4918212SMichael.Corcoran@Sun.COM
4928212SMichael.Corcoran@Sun.COM /*
4938212SMichael.Corcoran@Sun.COM * Release the hold on lvp which was acquired by lib_va_find or lib_va_add_hash.
4948212SMichael.Corcoran@Sun.COM * In addition, if this is the last hold and lvp is marked for deletion,
4958212SMichael.Corcoran@Sun.COM * free up it's reserved address space and free the structure.
4968212SMichael.Corcoran@Sun.COM */
4978212SMichael.Corcoran@Sun.COM static void
lib_va_release(struct lib_va * lvp)4988212SMichael.Corcoran@Sun.COM lib_va_release(struct lib_va *lvp)
4998212SMichael.Corcoran@Sun.COM {
5008212SMichael.Corcoran@Sun.COM uint_t index;
5018212SMichael.Corcoran@Sun.COM int to_del = 0;
5028212SMichael.Corcoran@Sun.COM
5038212SMichael.Corcoran@Sun.COM ASSERT(lvp->lv_refcnt > 0);
5048212SMichael.Corcoran@Sun.COM
5058212SMichael.Corcoran@Sun.COM index = LIB_VA_HASH(lvp->lv_nodeid);
5068212SMichael.Corcoran@Sun.COM mutex_enter(LIB_VA_HASH_MUTEX(index));
5078212SMichael.Corcoran@Sun.COM if (--lvp->lv_refcnt == 0 && (lvp->lv_flags & LV_DEL)) {
5088212SMichael.Corcoran@Sun.COM to_del = 1;
5098212SMichael.Corcoran@Sun.COM }
5108212SMichael.Corcoran@Sun.COM mutex_exit(LIB_VA_HASH_MUTEX(index));
5118212SMichael.Corcoran@Sun.COM if (to_del) {
5128212SMichael.Corcoran@Sun.COM ASSERT(lvp->lv_next == 0);
5138212SMichael.Corcoran@Sun.COM lib_va_free(lvp);
5148212SMichael.Corcoran@Sun.COM }
5158212SMichael.Corcoran@Sun.COM }
5168212SMichael.Corcoran@Sun.COM
5178212SMichael.Corcoran@Sun.COM /*
5188212SMichael.Corcoran@Sun.COM * Dummy function for mapping through /dev/null
5198212SMichael.Corcoran@Sun.COM * Normally I would have used mmmmap in common/io/mem.c
5208212SMichael.Corcoran@Sun.COM * but that is a static function, and for /dev/null, it
5218212SMichael.Corcoran@Sun.COM * just returns -1.
5228212SMichael.Corcoran@Sun.COM */
5238212SMichael.Corcoran@Sun.COM /* ARGSUSED */
5248212SMichael.Corcoran@Sun.COM static int
mmapobj_dummy(dev_t dev,off_t off,int prot)5258212SMichael.Corcoran@Sun.COM mmapobj_dummy(dev_t dev, off_t off, int prot)
5268212SMichael.Corcoran@Sun.COM {
5278212SMichael.Corcoran@Sun.COM return (-1);
5288212SMichael.Corcoran@Sun.COM }
5298212SMichael.Corcoran@Sun.COM
5308212SMichael.Corcoran@Sun.COM /*
5318212SMichael.Corcoran@Sun.COM * Called when an error occurred which requires mmapobj to return failure.
5328212SMichael.Corcoran@Sun.COM * All mapped objects will be unmapped and /dev/null mappings will be
5338212SMichael.Corcoran@Sun.COM * reclaimed if necessary.
5348212SMichael.Corcoran@Sun.COM * num_mapped is the number of elements of mrp which have been mapped, and
5358212SMichael.Corcoran@Sun.COM * num_segs is the total number of elements in mrp.
5368212SMichael.Corcoran@Sun.COM * For e_type ET_EXEC, we need to unmap all of the elements in mrp since
5378212SMichael.Corcoran@Sun.COM * we had already made reservations for them.
5388212SMichael.Corcoran@Sun.COM * If num_mapped equals num_segs, then we know that we had fully mapped
5398212SMichael.Corcoran@Sun.COM * the file and only need to clean up the segments described.
5408212SMichael.Corcoran@Sun.COM * If they are not equal, then for ET_DYN we will unmap the range from the
5418212SMichael.Corcoran@Sun.COM * end of the last mapped segment to the end of the last segment in mrp
5428212SMichael.Corcoran@Sun.COM * since we would have made a reservation for that memory earlier.
5438212SMichael.Corcoran@Sun.COM * If e_type is passed in as zero, num_mapped must equal num_segs.
5448212SMichael.Corcoran@Sun.COM */
5458212SMichael.Corcoran@Sun.COM void
mmapobj_unmap(mmapobj_result_t * mrp,int num_mapped,int num_segs,ushort_t e_type)5468212SMichael.Corcoran@Sun.COM mmapobj_unmap(mmapobj_result_t *mrp, int num_mapped, int num_segs,
5478212SMichael.Corcoran@Sun.COM ushort_t e_type)
5488212SMichael.Corcoran@Sun.COM {
5498212SMichael.Corcoran@Sun.COM int i;
5508212SMichael.Corcoran@Sun.COM struct as *as = curproc->p_as;
5518212SMichael.Corcoran@Sun.COM caddr_t addr;
5528212SMichael.Corcoran@Sun.COM size_t size;
5538212SMichael.Corcoran@Sun.COM
5548212SMichael.Corcoran@Sun.COM if (e_type == ET_EXEC) {
5558212SMichael.Corcoran@Sun.COM num_mapped = num_segs;
5568212SMichael.Corcoran@Sun.COM }
5578212SMichael.Corcoran@Sun.COM #ifdef DEBUG
5588212SMichael.Corcoran@Sun.COM if (e_type == 0) {
5598212SMichael.Corcoran@Sun.COM ASSERT(num_mapped == num_segs);
5608212SMichael.Corcoran@Sun.COM }
5618212SMichael.Corcoran@Sun.COM #endif
5628212SMichael.Corcoran@Sun.COM
5638212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(unmap_called);
5648212SMichael.Corcoran@Sun.COM for (i = 0; i < num_mapped; i++) {
5658212SMichael.Corcoran@Sun.COM
5668212SMichael.Corcoran@Sun.COM /*
5678212SMichael.Corcoran@Sun.COM * If we are going to have to create a mapping we need to
5688212SMichael.Corcoran@Sun.COM * make sure that no one else will use the address we
5698212SMichael.Corcoran@Sun.COM * need to remap between the time it is unmapped and
5708212SMichael.Corcoran@Sun.COM * mapped below.
5718212SMichael.Corcoran@Sun.COM */
5728212SMichael.Corcoran@Sun.COM if (mrp[i].mr_flags & MR_RESV) {
5738212SMichael.Corcoran@Sun.COM as_rangelock(as);
5748212SMichael.Corcoran@Sun.COM }
5758212SMichael.Corcoran@Sun.COM /* Always need to unmap what we mapped */
5768212SMichael.Corcoran@Sun.COM (void) as_unmap(as, mrp[i].mr_addr, mrp[i].mr_msize);
5778212SMichael.Corcoran@Sun.COM
5788212SMichael.Corcoran@Sun.COM /* Need to reclaim /dev/null reservation from earlier */
5798212SMichael.Corcoran@Sun.COM if (mrp[i].mr_flags & MR_RESV) {
5808212SMichael.Corcoran@Sun.COM struct segdev_crargs dev_a;
5818212SMichael.Corcoran@Sun.COM
5828212SMichael.Corcoran@Sun.COM ASSERT(e_type != ET_DYN);
5838212SMichael.Corcoran@Sun.COM /*
5848212SMichael.Corcoran@Sun.COM * Use seg_dev segment driver for /dev/null mapping.
5858212SMichael.Corcoran@Sun.COM */
5868212SMichael.Corcoran@Sun.COM dev_a.mapfunc = mmapobj_dummy;
5878212SMichael.Corcoran@Sun.COM dev_a.dev = makedevice(mm_major, M_NULL);
5888212SMichael.Corcoran@Sun.COM dev_a.offset = 0;
5898212SMichael.Corcoran@Sun.COM dev_a.type = 0; /* neither PRIVATE nor SHARED */
5908212SMichael.Corcoran@Sun.COM dev_a.prot = dev_a.maxprot = (uchar_t)PROT_NONE;
5918212SMichael.Corcoran@Sun.COM dev_a.hat_attr = 0;
5928212SMichael.Corcoran@Sun.COM dev_a.hat_flags = 0;
5938212SMichael.Corcoran@Sun.COM
5948212SMichael.Corcoran@Sun.COM (void) as_map(as, mrp[i].mr_addr, mrp[i].mr_msize,
5958212SMichael.Corcoran@Sun.COM segdev_create, &dev_a);
5968212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(remap_devnull);
5978212SMichael.Corcoran@Sun.COM as_rangeunlock(as);
5988212SMichael.Corcoran@Sun.COM }
5998212SMichael.Corcoran@Sun.COM }
6008212SMichael.Corcoran@Sun.COM
6018212SMichael.Corcoran@Sun.COM if (num_mapped != num_segs) {
6028212SMichael.Corcoran@Sun.COM ASSERT(e_type == ET_DYN);
6038212SMichael.Corcoran@Sun.COM /* Need to unmap any reservation made after last mapped seg */
6048212SMichael.Corcoran@Sun.COM if (num_mapped == 0) {
6058212SMichael.Corcoran@Sun.COM addr = mrp[0].mr_addr;
6068212SMichael.Corcoran@Sun.COM } else {
6078212SMichael.Corcoran@Sun.COM addr = mrp[num_mapped - 1].mr_addr +
6088212SMichael.Corcoran@Sun.COM mrp[num_mapped - 1].mr_msize;
6098212SMichael.Corcoran@Sun.COM }
6108212SMichael.Corcoran@Sun.COM size = (size_t)mrp[num_segs - 1].mr_addr +
6118212SMichael.Corcoran@Sun.COM mrp[num_segs - 1].mr_msize - (size_t)addr;
6128212SMichael.Corcoran@Sun.COM (void) as_unmap(as, addr, size);
6138212SMichael.Corcoran@Sun.COM
6148212SMichael.Corcoran@Sun.COM /*
6158212SMichael.Corcoran@Sun.COM * Now we need to unmap the holes between mapped segs.
6168212SMichael.Corcoran@Sun.COM * Note that we have not mapped all of the segments and thus
6178212SMichael.Corcoran@Sun.COM * the holes between segments would not have been unmapped
6188212SMichael.Corcoran@Sun.COM * yet. If num_mapped == num_segs, then all of the holes
6198212SMichael.Corcoran@Sun.COM * between segments would have already been unmapped.
6208212SMichael.Corcoran@Sun.COM */
6218212SMichael.Corcoran@Sun.COM
6228212SMichael.Corcoran@Sun.COM for (i = 1; i < num_mapped; i++) {
6238212SMichael.Corcoran@Sun.COM addr = mrp[i - 1].mr_addr + mrp[i - 1].mr_msize;
6248212SMichael.Corcoran@Sun.COM size = mrp[i].mr_addr - addr;
6258212SMichael.Corcoran@Sun.COM (void) as_unmap(as, addr, size);
6268212SMichael.Corcoran@Sun.COM }
6278212SMichael.Corcoran@Sun.COM }
6288212SMichael.Corcoran@Sun.COM }
6298212SMichael.Corcoran@Sun.COM
6308212SMichael.Corcoran@Sun.COM /*
6318212SMichael.Corcoran@Sun.COM * We need to add the start address into mrp so that the unmap function
6328212SMichael.Corcoran@Sun.COM * has absolute addresses to use.
6338212SMichael.Corcoran@Sun.COM */
6348212SMichael.Corcoran@Sun.COM static void
mmapobj_unmap_exec(mmapobj_result_t * mrp,int num_mapped,caddr_t start_addr)6358212SMichael.Corcoran@Sun.COM mmapobj_unmap_exec(mmapobj_result_t *mrp, int num_mapped, caddr_t start_addr)
6368212SMichael.Corcoran@Sun.COM {
6378212SMichael.Corcoran@Sun.COM int i;
6388212SMichael.Corcoran@Sun.COM
6398212SMichael.Corcoran@Sun.COM for (i = 0; i < num_mapped; i++) {
6408212SMichael.Corcoran@Sun.COM mrp[i].mr_addr += (size_t)start_addr;
6418212SMichael.Corcoran@Sun.COM }
6428212SMichael.Corcoran@Sun.COM mmapobj_unmap(mrp, num_mapped, num_mapped, ET_EXEC);
6438212SMichael.Corcoran@Sun.COM }
6448212SMichael.Corcoran@Sun.COM
6458212SMichael.Corcoran@Sun.COM static caddr_t
mmapobj_lookup_start_addr(struct lib_va * lvp)6468212SMichael.Corcoran@Sun.COM mmapobj_lookup_start_addr(struct lib_va *lvp)
6478212SMichael.Corcoran@Sun.COM {
648*8947SMichael.Corcoran@Sun.COM proc_t *p = curproc;
649*8947SMichael.Corcoran@Sun.COM struct as *as = p->p_as;
6508212SMichael.Corcoran@Sun.COM struct segvn_crargs crargs = SEGVN_ZFOD_ARGS(PROT_USER, PROT_ALL);
6518212SMichael.Corcoran@Sun.COM int error;
6528212SMichael.Corcoran@Sun.COM uint_t ma_flags = _MAP_LOW32;
6538212SMichael.Corcoran@Sun.COM caddr_t base = NULL;
6548212SMichael.Corcoran@Sun.COM size_t len;
6558212SMichael.Corcoran@Sun.COM size_t align;
6568212SMichael.Corcoran@Sun.COM
6578212SMichael.Corcoran@Sun.COM ASSERT(lvp != NULL);
6588212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(lookup_start);
6598212SMichael.Corcoran@Sun.COM
6608212SMichael.Corcoran@Sun.COM as_rangelock(as);
6618212SMichael.Corcoran@Sun.COM
6628212SMichael.Corcoran@Sun.COM base = lvp->lv_base_va;
6638212SMichael.Corcoran@Sun.COM len = lvp->lv_len;
6648212SMichael.Corcoran@Sun.COM
6658212SMichael.Corcoran@Sun.COM /*
6668212SMichael.Corcoran@Sun.COM * If we don't have an expected base address, or the one that we want
6678578SMichael.Corcoran@Sun.COM * to use is not available or acceptable, go get an acceptable
6688578SMichael.Corcoran@Sun.COM * address range.
6698212SMichael.Corcoran@Sun.COM */
6708578SMichael.Corcoran@Sun.COM if (base == NULL || as_gap(as, len, &base, &len, 0, NULL) ||
6718578SMichael.Corcoran@Sun.COM valid_usr_range(base, len, PROT_ALL, as, as->a_userlimit) !=
672*8947SMichael.Corcoran@Sun.COM RANGE_OKAY || OVERLAPS_STACK(base + len, p)) {
6738212SMichael.Corcoran@Sun.COM if (lvp->lv_flags & LV_ELF64) {
6748212SMichael.Corcoran@Sun.COM ma_flags = 0;
6758212SMichael.Corcoran@Sun.COM }
6768212SMichael.Corcoran@Sun.COM
6778212SMichael.Corcoran@Sun.COM align = lvp->lv_align;
6788212SMichael.Corcoran@Sun.COM if (align > 1) {
6798212SMichael.Corcoran@Sun.COM ma_flags |= MAP_ALIGN;
6808212SMichael.Corcoran@Sun.COM }
6818212SMichael.Corcoran@Sun.COM
6828212SMichael.Corcoran@Sun.COM base = (caddr_t)align;
6838212SMichael.Corcoran@Sun.COM map_addr(&base, len, 0, 1, ma_flags);
6848212SMichael.Corcoran@Sun.COM }
6858212SMichael.Corcoran@Sun.COM
6868212SMichael.Corcoran@Sun.COM /*
6878212SMichael.Corcoran@Sun.COM * Need to reserve the address space we're going to use.
6888212SMichael.Corcoran@Sun.COM * Don't reserve swap space since we'll be mapping over this.
6898212SMichael.Corcoran@Sun.COM */
6908212SMichael.Corcoran@Sun.COM if (base != NULL) {
6918212SMichael.Corcoran@Sun.COM crargs.flags |= MAP_NORESERVE;
6928212SMichael.Corcoran@Sun.COM error = as_map(as, base, len, segvn_create, &crargs);
6938212SMichael.Corcoran@Sun.COM if (error) {
6948212SMichael.Corcoran@Sun.COM base = NULL;
6958212SMichael.Corcoran@Sun.COM }
6968212SMichael.Corcoran@Sun.COM }
6978212SMichael.Corcoran@Sun.COM
6988212SMichael.Corcoran@Sun.COM as_rangeunlock(as);
6998212SMichael.Corcoran@Sun.COM return (base);
7008212SMichael.Corcoran@Sun.COM }
7018212SMichael.Corcoran@Sun.COM
7028212SMichael.Corcoran@Sun.COM /*
7038212SMichael.Corcoran@Sun.COM * Get the starting address for a given file to be mapped and return it
7048212SMichael.Corcoran@Sun.COM * to the caller. If we're using lib_va and we need to allocate an address,
7058212SMichael.Corcoran@Sun.COM * we will attempt to allocate it from the global reserved pool such that the
7068212SMichael.Corcoran@Sun.COM * same address can be used in the future for this file. If we can't use the
7078212SMichael.Corcoran@Sun.COM * reserved address then we just get one that will fit in our address space.
7088212SMichael.Corcoran@Sun.COM *
7098212SMichael.Corcoran@Sun.COM * Returns the starting virtual address for the range to be mapped or NULL
7108212SMichael.Corcoran@Sun.COM * if an error is encountered. If we successfully insert the requested info
7118212SMichael.Corcoran@Sun.COM * into the lib_va hash, then *lvpp will be set to point to this lib_va
7128212SMichael.Corcoran@Sun.COM * structure. The structure will have a hold on it and thus lib_va_release
7138212SMichael.Corcoran@Sun.COM * needs to be called on it by the caller. This function will not fill out
7148212SMichael.Corcoran@Sun.COM * lv_mps or lv_num_segs since it does not have enough information to do so.
7158212SMichael.Corcoran@Sun.COM * The caller is responsible for doing this making sure that any modifications
7168212SMichael.Corcoran@Sun.COM * to lv_mps are visible before setting lv_num_segs.
7178212SMichael.Corcoran@Sun.COM */
7188212SMichael.Corcoran@Sun.COM static caddr_t
mmapobj_alloc_start_addr(struct lib_va ** lvpp,size_t len,int use_lib_va,size_t align,vattr_t * vap)7198212SMichael.Corcoran@Sun.COM mmapobj_alloc_start_addr(struct lib_va **lvpp, size_t len, int use_lib_va,
7208212SMichael.Corcoran@Sun.COM size_t align, vattr_t *vap)
7218212SMichael.Corcoran@Sun.COM {
722*8947SMichael.Corcoran@Sun.COM proc_t *p = curproc;
723*8947SMichael.Corcoran@Sun.COM struct as *as = p->p_as;
7248212SMichael.Corcoran@Sun.COM struct segvn_crargs crargs = SEGVN_ZFOD_ARGS(PROT_USER, PROT_ALL);
7258212SMichael.Corcoran@Sun.COM int error;
7268212SMichael.Corcoran@Sun.COM model_t model;
7278212SMichael.Corcoran@Sun.COM uint_t ma_flags = _MAP_LOW32;
7288212SMichael.Corcoran@Sun.COM caddr_t base = NULL;
7298212SMichael.Corcoran@Sun.COM vmem_t *model_vmem;
7308684SMichael.Corcoran@Sun.COM size_t lib_va_start;
7318684SMichael.Corcoran@Sun.COM size_t lib_va_end;
7328684SMichael.Corcoran@Sun.COM size_t lib_va_len;
7338212SMichael.Corcoran@Sun.COM
7348212SMichael.Corcoran@Sun.COM ASSERT(lvpp != NULL);
7358212SMichael.Corcoran@Sun.COM
7368212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(alloc_start);
7378212SMichael.Corcoran@Sun.COM model = get_udatamodel();
7388212SMichael.Corcoran@Sun.COM
7398212SMichael.Corcoran@Sun.COM if (model == DATAMODEL_LP64) {
7408212SMichael.Corcoran@Sun.COM ma_flags = 0;
7418212SMichael.Corcoran@Sun.COM model_vmem = lib_va_64_arena;
7428212SMichael.Corcoran@Sun.COM } else {
7438212SMichael.Corcoran@Sun.COM ASSERT(model == DATAMODEL_ILP32);
7448212SMichael.Corcoran@Sun.COM model_vmem = lib_va_32_arena;
7458212SMichael.Corcoran@Sun.COM }
7468212SMichael.Corcoran@Sun.COM
7478212SMichael.Corcoran@Sun.COM if (align > 1) {
7488212SMichael.Corcoran@Sun.COM ma_flags |= MAP_ALIGN;
7498212SMichael.Corcoran@Sun.COM }
7508212SMichael.Corcoran@Sun.COM if (use_lib_va) {
7518684SMichael.Corcoran@Sun.COM /*
7528684SMichael.Corcoran@Sun.COM * The first time through, we need to setup the lib_va arenas.
7538684SMichael.Corcoran@Sun.COM * We call map_addr to find a suitable range of memory to map
7548684SMichael.Corcoran@Sun.COM * the given library, and we will set the highest address
7558684SMichael.Corcoran@Sun.COM * in our vmem arena to the end of this adddress range.
7568684SMichael.Corcoran@Sun.COM * We allow up to half of the address space to be used
7578684SMichael.Corcoran@Sun.COM * for lib_va addresses but we do not prevent any allocations
7588684SMichael.Corcoran@Sun.COM * in this range from other allocation paths.
7598684SMichael.Corcoran@Sun.COM */
7608684SMichael.Corcoran@Sun.COM if (lib_va_64_arena == NULL && model == DATAMODEL_LP64) {
7618684SMichael.Corcoran@Sun.COM mutex_enter(&lib_va_init_mutex);
7628684SMichael.Corcoran@Sun.COM if (lib_va_64_arena == NULL) {
7638684SMichael.Corcoran@Sun.COM base = (caddr_t)align;
7648684SMichael.Corcoran@Sun.COM as_rangelock(as);
7658684SMichael.Corcoran@Sun.COM map_addr(&base, len, 0, 1, ma_flags);
7668684SMichael.Corcoran@Sun.COM as_rangeunlock(as);
7678684SMichael.Corcoran@Sun.COM if (base == NULL) {
7688684SMichael.Corcoran@Sun.COM mutex_exit(&lib_va_init_mutex);
7698684SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(lib_va_create_failure);
7708684SMichael.Corcoran@Sun.COM goto nolibva;
7718684SMichael.Corcoran@Sun.COM }
7728684SMichael.Corcoran@Sun.COM lib_va_end = (size_t)base + len;
7738684SMichael.Corcoran@Sun.COM lib_va_len = lib_va_end >> 1;
7748684SMichael.Corcoran@Sun.COM lib_va_len = P2ROUNDUP(lib_va_len, PAGESIZE);
7758684SMichael.Corcoran@Sun.COM lib_va_start = lib_va_end - lib_va_len;
7768684SMichael.Corcoran@Sun.COM
7778684SMichael.Corcoran@Sun.COM /*
778*8947SMichael.Corcoran@Sun.COM * Need to make sure we avoid the address hole.
7798684SMichael.Corcoran@Sun.COM * We know lib_va_end is valid but we need to
7808684SMichael.Corcoran@Sun.COM * make sure lib_va_start is as well.
7818684SMichael.Corcoran@Sun.COM */
7828684SMichael.Corcoran@Sun.COM if ((lib_va_end > (size_t)hole_end) &&
7838684SMichael.Corcoran@Sun.COM (lib_va_start < (size_t)hole_end)) {
7848684SMichael.Corcoran@Sun.COM lib_va_start = P2ROUNDUP(
785*8947SMichael.Corcoran@Sun.COM (size_t)hole_end, PAGESIZE);
7868684SMichael.Corcoran@Sun.COM lib_va_len = lib_va_end - lib_va_start;
7878684SMichael.Corcoran@Sun.COM }
7888684SMichael.Corcoran@Sun.COM lib_va_64_arena = vmem_create("lib_va_64",
7898684SMichael.Corcoran@Sun.COM (void *)lib_va_start, lib_va_len, PAGESIZE,
7908684SMichael.Corcoran@Sun.COM NULL, NULL, NULL, 0,
7918684SMichael.Corcoran@Sun.COM VM_NOSLEEP | VMC_IDENTIFIER);
7928684SMichael.Corcoran@Sun.COM if (lib_va_64_arena == NULL) {
7938684SMichael.Corcoran@Sun.COM mutex_exit(&lib_va_init_mutex);
7948684SMichael.Corcoran@Sun.COM goto nolibva;
7958684SMichael.Corcoran@Sun.COM }
7968684SMichael.Corcoran@Sun.COM }
7978684SMichael.Corcoran@Sun.COM model_vmem = lib_va_64_arena;
7988684SMichael.Corcoran@Sun.COM mutex_exit(&lib_va_init_mutex);
7998684SMichael.Corcoran@Sun.COM } else if (lib_va_32_arena == NULL &&
8008684SMichael.Corcoran@Sun.COM model == DATAMODEL_ILP32) {
8018684SMichael.Corcoran@Sun.COM mutex_enter(&lib_va_init_mutex);
8028684SMichael.Corcoran@Sun.COM if (lib_va_32_arena == NULL) {
8038684SMichael.Corcoran@Sun.COM base = (caddr_t)align;
8048684SMichael.Corcoran@Sun.COM as_rangelock(as);
8058684SMichael.Corcoran@Sun.COM map_addr(&base, len, 0, 1, ma_flags);
8068684SMichael.Corcoran@Sun.COM as_rangeunlock(as);
8078684SMichael.Corcoran@Sun.COM if (base == NULL) {
8088684SMichael.Corcoran@Sun.COM mutex_exit(&lib_va_init_mutex);
8098684SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(lib_va_create_failure);
8108684SMichael.Corcoran@Sun.COM goto nolibva;
8118684SMichael.Corcoran@Sun.COM }
8128684SMichael.Corcoran@Sun.COM lib_va_end = (size_t)base + len;
8138684SMichael.Corcoran@Sun.COM lib_va_len = lib_va_end >> 1;
8148684SMichael.Corcoran@Sun.COM lib_va_len = P2ROUNDUP(lib_va_len, PAGESIZE);
8158684SMichael.Corcoran@Sun.COM lib_va_start = lib_va_end - lib_va_len;
8168684SMichael.Corcoran@Sun.COM lib_va_32_arena = vmem_create("lib_va_32",
8178684SMichael.Corcoran@Sun.COM (void *)lib_va_start, lib_va_len, PAGESIZE,
8188684SMichael.Corcoran@Sun.COM NULL, NULL, NULL, 0,
8198684SMichael.Corcoran@Sun.COM VM_NOSLEEP | VMC_IDENTIFIER);
8208684SMichael.Corcoran@Sun.COM if (lib_va_32_arena == NULL) {
8218684SMichael.Corcoran@Sun.COM mutex_exit(&lib_va_init_mutex);
8228684SMichael.Corcoran@Sun.COM goto nolibva;
8238684SMichael.Corcoran@Sun.COM }
8248684SMichael.Corcoran@Sun.COM }
8258684SMichael.Corcoran@Sun.COM model_vmem = lib_va_32_arena;
8268684SMichael.Corcoran@Sun.COM mutex_exit(&lib_va_init_mutex);
8278684SMichael.Corcoran@Sun.COM }
8288684SMichael.Corcoran@Sun.COM
8298212SMichael.Corcoran@Sun.COM if (model == DATAMODEL_LP64 || libs_mapped_32 < lib_threshold) {
8308212SMichael.Corcoran@Sun.COM base = vmem_xalloc(model_vmem, len, align, 0, 0, NULL,
8318212SMichael.Corcoran@Sun.COM NULL, VM_NOSLEEP | VM_ENDALLOC);
8328212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(alloc_vmem);
8338212SMichael.Corcoran@Sun.COM }
8348684SMichael.Corcoran@Sun.COM
8358212SMichael.Corcoran@Sun.COM /*
8368212SMichael.Corcoran@Sun.COM * Even if the address fails to fit in our address space,
8378212SMichael.Corcoran@Sun.COM * or we can't use a reserved address,
8388212SMichael.Corcoran@Sun.COM * we should still save it off in lib_va_hash.
8398212SMichael.Corcoran@Sun.COM */
8408212SMichael.Corcoran@Sun.COM *lvpp = lib_va_add_hash(base, len, align, vap);
8418212SMichael.Corcoran@Sun.COM
8428212SMichael.Corcoran@Sun.COM /*
8438212SMichael.Corcoran@Sun.COM * Check for collision on insertion and free up our VA space.
8448212SMichael.Corcoran@Sun.COM * This is expected to be rare, so we'll just reset base to
8458212SMichael.Corcoran@Sun.COM * NULL instead of looking it up in the lib_va hash.
8468212SMichael.Corcoran@Sun.COM */
8478212SMichael.Corcoran@Sun.COM if (*lvpp == NULL) {
8488212SMichael.Corcoran@Sun.COM if (base != NULL) {
8498212SMichael.Corcoran@Sun.COM vmem_xfree(model_vmem, base, len);
8508212SMichael.Corcoran@Sun.COM base = NULL;
8518212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(add_collision);
8528212SMichael.Corcoran@Sun.COM }
8538212SMichael.Corcoran@Sun.COM }
8548212SMichael.Corcoran@Sun.COM }
8558212SMichael.Corcoran@Sun.COM
8568684SMichael.Corcoran@Sun.COM nolibva:
8578212SMichael.Corcoran@Sun.COM as_rangelock(as);
8588212SMichael.Corcoran@Sun.COM
8598212SMichael.Corcoran@Sun.COM /*
8608212SMichael.Corcoran@Sun.COM * If we don't have an expected base address, or the one that we want
8618578SMichael.Corcoran@Sun.COM * to use is not available or acceptable, go get an acceptable
8628578SMichael.Corcoran@Sun.COM * address range.
8638212SMichael.Corcoran@Sun.COM */
8648578SMichael.Corcoran@Sun.COM if (base == NULL || as_gap(as, len, &base, &len, 0, NULL) ||
8658578SMichael.Corcoran@Sun.COM valid_usr_range(base, len, PROT_ALL, as, as->a_userlimit) !=
866*8947SMichael.Corcoran@Sun.COM RANGE_OKAY || OVERLAPS_STACK(base + len, p)) {
8678212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(get_addr);
8688212SMichael.Corcoran@Sun.COM base = (caddr_t)align;
8698212SMichael.Corcoran@Sun.COM map_addr(&base, len, 0, 1, ma_flags);
8708212SMichael.Corcoran@Sun.COM }
8718212SMichael.Corcoran@Sun.COM
8728212SMichael.Corcoran@Sun.COM /*
8738212SMichael.Corcoran@Sun.COM * Need to reserve the address space we're going to use.
8748212SMichael.Corcoran@Sun.COM * Don't reserve swap space since we'll be mapping over this.
8758212SMichael.Corcoran@Sun.COM */
8768212SMichael.Corcoran@Sun.COM if (base != NULL) {
8778212SMichael.Corcoran@Sun.COM /* Don't reserve swap space since we'll be mapping over this */
8788212SMichael.Corcoran@Sun.COM crargs.flags |= MAP_NORESERVE;
8798212SMichael.Corcoran@Sun.COM error = as_map(as, base, len, segvn_create, &crargs);
8808212SMichael.Corcoran@Sun.COM if (error) {
8818212SMichael.Corcoran@Sun.COM base = NULL;
8828212SMichael.Corcoran@Sun.COM }
8838212SMichael.Corcoran@Sun.COM }
8848212SMichael.Corcoran@Sun.COM
8858212SMichael.Corcoran@Sun.COM as_rangeunlock(as);
8868212SMichael.Corcoran@Sun.COM return (base);
8878212SMichael.Corcoran@Sun.COM }
8888212SMichael.Corcoran@Sun.COM
8898212SMichael.Corcoran@Sun.COM /*
8908212SMichael.Corcoran@Sun.COM * Map the file associated with vp into the address space as a single
8918212SMichael.Corcoran@Sun.COM * read only private mapping.
8928212SMichael.Corcoran@Sun.COM * Returns 0 for success, and non-zero for failure to map the file.
8938212SMichael.Corcoran@Sun.COM */
8948212SMichael.Corcoran@Sun.COM static int
mmapobj_map_flat(vnode_t * vp,mmapobj_result_t * mrp,size_t padding,cred_t * fcred)8958212SMichael.Corcoran@Sun.COM mmapobj_map_flat(vnode_t *vp, mmapobj_result_t *mrp, size_t padding,
8968212SMichael.Corcoran@Sun.COM cred_t *fcred)
8978212SMichael.Corcoran@Sun.COM {
8988212SMichael.Corcoran@Sun.COM int error = 0;
8998212SMichael.Corcoran@Sun.COM struct as *as = curproc->p_as;
9008212SMichael.Corcoran@Sun.COM caddr_t addr = NULL;
9018212SMichael.Corcoran@Sun.COM caddr_t start_addr;
9028212SMichael.Corcoran@Sun.COM size_t len;
9038212SMichael.Corcoran@Sun.COM size_t pad_len;
9048212SMichael.Corcoran@Sun.COM int prot = PROT_USER | PROT_READ;
9058212SMichael.Corcoran@Sun.COM uint_t ma_flags = _MAP_LOW32;
9068212SMichael.Corcoran@Sun.COM vattr_t vattr;
9078212SMichael.Corcoran@Sun.COM struct segvn_crargs crargs = SEGVN_ZFOD_ARGS(PROT_USER, PROT_ALL);
9088212SMichael.Corcoran@Sun.COM
9098212SMichael.Corcoran@Sun.COM if (get_udatamodel() == DATAMODEL_LP64) {
9108212SMichael.Corcoran@Sun.COM ma_flags = 0;
9118212SMichael.Corcoran@Sun.COM }
9128212SMichael.Corcoran@Sun.COM
9138212SMichael.Corcoran@Sun.COM vattr.va_mask = AT_SIZE;
9148212SMichael.Corcoran@Sun.COM error = VOP_GETATTR(vp, &vattr, 0, fcred, NULL);
9158212SMichael.Corcoran@Sun.COM if (error) {
9168212SMichael.Corcoran@Sun.COM return (error);
9178212SMichael.Corcoran@Sun.COM }
9188212SMichael.Corcoran@Sun.COM
9198212SMichael.Corcoran@Sun.COM len = vattr.va_size;
9208212SMichael.Corcoran@Sun.COM
9218212SMichael.Corcoran@Sun.COM ma_flags |= MAP_PRIVATE;
9228212SMichael.Corcoran@Sun.COM if (padding == 0) {
9238212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(map_flat_no_padding);
9248212SMichael.Corcoran@Sun.COM error = VOP_MAP(vp, 0, as, &addr, len, prot, PROT_ALL,
9258212SMichael.Corcoran@Sun.COM ma_flags, fcred, NULL);
9268212SMichael.Corcoran@Sun.COM if (error == 0) {
9278212SMichael.Corcoran@Sun.COM mrp[0].mr_addr = addr;
9288212SMichael.Corcoran@Sun.COM mrp[0].mr_msize = len;
9298212SMichael.Corcoran@Sun.COM mrp[0].mr_fsize = len;
9308212SMichael.Corcoran@Sun.COM mrp[0].mr_offset = 0;
9318212SMichael.Corcoran@Sun.COM mrp[0].mr_prot = prot;
9328212SMichael.Corcoran@Sun.COM mrp[0].mr_flags = 0;
9338212SMichael.Corcoran@Sun.COM }
9348212SMichael.Corcoran@Sun.COM return (error);
9358212SMichael.Corcoran@Sun.COM }
9368212SMichael.Corcoran@Sun.COM
9378212SMichael.Corcoran@Sun.COM /* padding was requested so there's more work to be done */
9388212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(map_flat_padding);
9398212SMichael.Corcoran@Sun.COM
9408212SMichael.Corcoran@Sun.COM /* No need to reserve swap space now since it will be reserved later */
9418212SMichael.Corcoran@Sun.COM crargs.flags |= MAP_NORESERVE;
9428212SMichael.Corcoran@Sun.COM
9438212SMichael.Corcoran@Sun.COM /* Need to setup padding which can only be in PAGESIZE increments. */
9448212SMichael.Corcoran@Sun.COM ASSERT((padding & PAGEOFFSET) == 0);
9458212SMichael.Corcoran@Sun.COM pad_len = len + (2 * padding);
9468212SMichael.Corcoran@Sun.COM
9478212SMichael.Corcoran@Sun.COM as_rangelock(as);
9488212SMichael.Corcoran@Sun.COM map_addr(&addr, pad_len, 0, 1, ma_flags);
9498212SMichael.Corcoran@Sun.COM error = as_map(as, addr, pad_len, segvn_create, &crargs);
9508212SMichael.Corcoran@Sun.COM as_rangeunlock(as);
9518212SMichael.Corcoran@Sun.COM if (error) {
9528212SMichael.Corcoran@Sun.COM return (error);
9538212SMichael.Corcoran@Sun.COM }
9548212SMichael.Corcoran@Sun.COM start_addr = addr;
9558212SMichael.Corcoran@Sun.COM addr += padding;
9568212SMichael.Corcoran@Sun.COM ma_flags |= MAP_FIXED;
9578212SMichael.Corcoran@Sun.COM error = VOP_MAP(vp, 0, as, &addr, len, prot, PROT_ALL, ma_flags,
9588212SMichael.Corcoran@Sun.COM fcred, NULL);
9598212SMichael.Corcoran@Sun.COM if (error == 0) {
9608212SMichael.Corcoran@Sun.COM mrp[0].mr_addr = start_addr;
9618212SMichael.Corcoran@Sun.COM mrp[0].mr_msize = padding;
9628212SMichael.Corcoran@Sun.COM mrp[0].mr_fsize = 0;
9638212SMichael.Corcoran@Sun.COM mrp[0].mr_offset = 0;
9648212SMichael.Corcoran@Sun.COM mrp[0].mr_prot = 0;
9658212SMichael.Corcoran@Sun.COM mrp[0].mr_flags = MR_PADDING;
9668212SMichael.Corcoran@Sun.COM
9678212SMichael.Corcoran@Sun.COM mrp[1].mr_addr = addr;
9688212SMichael.Corcoran@Sun.COM mrp[1].mr_msize = len;
9698212SMichael.Corcoran@Sun.COM mrp[1].mr_fsize = len;
9708212SMichael.Corcoran@Sun.COM mrp[1].mr_offset = 0;
9718212SMichael.Corcoran@Sun.COM mrp[1].mr_prot = prot;
9728212SMichael.Corcoran@Sun.COM mrp[1].mr_flags = 0;
9738212SMichael.Corcoran@Sun.COM
9748212SMichael.Corcoran@Sun.COM mrp[2].mr_addr = addr + P2ROUNDUP(len, PAGESIZE);
9758212SMichael.Corcoran@Sun.COM mrp[2].mr_msize = padding;
9768212SMichael.Corcoran@Sun.COM mrp[2].mr_fsize = 0;
9778212SMichael.Corcoran@Sun.COM mrp[2].mr_offset = 0;
9788212SMichael.Corcoran@Sun.COM mrp[2].mr_prot = 0;
9798212SMichael.Corcoran@Sun.COM mrp[2].mr_flags = MR_PADDING;
9808212SMichael.Corcoran@Sun.COM } else {
9818212SMichael.Corcoran@Sun.COM /* Need to cleanup the as_map from earlier */
9828212SMichael.Corcoran@Sun.COM (void) as_unmap(as, start_addr, pad_len);
9838212SMichael.Corcoran@Sun.COM }
9848212SMichael.Corcoran@Sun.COM return (error);
9858212SMichael.Corcoran@Sun.COM }
9868212SMichael.Corcoran@Sun.COM
9878212SMichael.Corcoran@Sun.COM /*
9888212SMichael.Corcoran@Sun.COM * Map a PT_LOAD or PT_SUNWBSS section of an executable file into the user's
9898212SMichael.Corcoran@Sun.COM * address space.
9908212SMichael.Corcoran@Sun.COM * vp - vnode to be mapped in
9918212SMichael.Corcoran@Sun.COM * addr - start address
9928212SMichael.Corcoran@Sun.COM * len - length of vp to be mapped
9938212SMichael.Corcoran@Sun.COM * zfodlen - length of zero filled memory after len above
9948212SMichael.Corcoran@Sun.COM * offset - offset into file where mapping should start
9958212SMichael.Corcoran@Sun.COM * prot - protections for this mapping
9968212SMichael.Corcoran@Sun.COM * fcred - credentials for the file associated with vp at open time.
9978212SMichael.Corcoran@Sun.COM */
9988212SMichael.Corcoran@Sun.COM static int
mmapobj_map_ptload(struct vnode * vp,caddr_t addr,size_t len,size_t zfodlen,off_t offset,int prot,cred_t * fcred)9998212SMichael.Corcoran@Sun.COM mmapobj_map_ptload(struct vnode *vp, caddr_t addr, size_t len, size_t zfodlen,
10008212SMichael.Corcoran@Sun.COM off_t offset, int prot, cred_t *fcred)
10018212SMichael.Corcoran@Sun.COM {
10028212SMichael.Corcoran@Sun.COM int error = 0;
10038212SMichael.Corcoran@Sun.COM caddr_t zfodbase, oldaddr;
10048212SMichael.Corcoran@Sun.COM size_t oldlen;
10058212SMichael.Corcoran@Sun.COM size_t end;
10068212SMichael.Corcoran@Sun.COM size_t zfoddiff;
10078212SMichael.Corcoran@Sun.COM label_t ljb;
10088212SMichael.Corcoran@Sun.COM struct as *as = curproc->p_as;
10098212SMichael.Corcoran@Sun.COM model_t model;
10108212SMichael.Corcoran@Sun.COM int full_page;
10118212SMichael.Corcoran@Sun.COM
10128212SMichael.Corcoran@Sun.COM /*
10138212SMichael.Corcoran@Sun.COM * See if addr and offset are aligned such that we can map in
10148212SMichael.Corcoran@Sun.COM * full pages instead of partial pages.
10158212SMichael.Corcoran@Sun.COM */
10168212SMichael.Corcoran@Sun.COM full_page = (((uintptr_t)addr & PAGEOFFSET) ==
10178212SMichael.Corcoran@Sun.COM ((uintptr_t)offset & PAGEOFFSET));
10188212SMichael.Corcoran@Sun.COM
10198212SMichael.Corcoran@Sun.COM model = get_udatamodel();
10208212SMichael.Corcoran@Sun.COM
10218212SMichael.Corcoran@Sun.COM oldaddr = addr;
10228212SMichael.Corcoran@Sun.COM addr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
10238212SMichael.Corcoran@Sun.COM if (len) {
10248212SMichael.Corcoran@Sun.COM spgcnt_t availm, npages;
10258212SMichael.Corcoran@Sun.COM int preread;
10268212SMichael.Corcoran@Sun.COM uint_t mflag = MAP_PRIVATE | MAP_FIXED;
10278212SMichael.Corcoran@Sun.COM
10288212SMichael.Corcoran@Sun.COM if (model == DATAMODEL_ILP32) {
10298212SMichael.Corcoran@Sun.COM mflag |= _MAP_LOW32;
10308212SMichael.Corcoran@Sun.COM }
10318212SMichael.Corcoran@Sun.COM /* We may need to map in extra bytes */
10328212SMichael.Corcoran@Sun.COM oldlen = len;
10338212SMichael.Corcoran@Sun.COM len += ((size_t)oldaddr & PAGEOFFSET);
10348212SMichael.Corcoran@Sun.COM
10358212SMichael.Corcoran@Sun.COM if (full_page) {
10368212SMichael.Corcoran@Sun.COM offset = (off_t)((uintptr_t)offset & PAGEMASK);
10378212SMichael.Corcoran@Sun.COM if ((prot & (PROT_WRITE | PROT_EXEC)) == PROT_EXEC) {
10388212SMichael.Corcoran@Sun.COM mflag |= MAP_TEXT;
10398212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(map_ptload_text);
10408212SMichael.Corcoran@Sun.COM } else {
10418212SMichael.Corcoran@Sun.COM mflag |= MAP_INITDATA;
10428212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(map_ptload_initdata);
10438212SMichael.Corcoran@Sun.COM }
10448212SMichael.Corcoran@Sun.COM
10458212SMichael.Corcoran@Sun.COM /*
10468212SMichael.Corcoran@Sun.COM * maxprot is passed as PROT_ALL so that mdb can
10478212SMichael.Corcoran@Sun.COM * write to this segment.
10488212SMichael.Corcoran@Sun.COM */
10498212SMichael.Corcoran@Sun.COM if (error = VOP_MAP(vp, (offset_t)offset, as, &addr,
10508212SMichael.Corcoran@Sun.COM len, prot, PROT_ALL, mflag, fcred, NULL)) {
10518212SMichael.Corcoran@Sun.COM return (error);
10528212SMichael.Corcoran@Sun.COM }
10538212SMichael.Corcoran@Sun.COM
10548212SMichael.Corcoran@Sun.COM /*
10558212SMichael.Corcoran@Sun.COM * If the segment can fit and is relatively small, then
10568212SMichael.Corcoran@Sun.COM * we prefault the entire segment in. This is based
10578212SMichael.Corcoran@Sun.COM * on the model that says the best working set of a
10588212SMichael.Corcoran@Sun.COM * small program is all of its pages.
10598212SMichael.Corcoran@Sun.COM * We only do this if freemem will not drop below
10608212SMichael.Corcoran@Sun.COM * lotsfree since we don't want to induce paging.
10618212SMichael.Corcoran@Sun.COM */
10628212SMichael.Corcoran@Sun.COM npages = (spgcnt_t)btopr(len);
10638212SMichael.Corcoran@Sun.COM availm = freemem - lotsfree;
10648212SMichael.Corcoran@Sun.COM preread = (npages < availm && len < PGTHRESH) ? 1 : 0;
10658212SMichael.Corcoran@Sun.COM
10668212SMichael.Corcoran@Sun.COM /*
10678212SMichael.Corcoran@Sun.COM * If we aren't prefaulting the segment,
10688212SMichael.Corcoran@Sun.COM * increment "deficit", if necessary to ensure
10698212SMichael.Corcoran@Sun.COM * that pages will become available when this
10708212SMichael.Corcoran@Sun.COM * process starts executing.
10718212SMichael.Corcoran@Sun.COM */
10728212SMichael.Corcoran@Sun.COM if (preread == 0 && npages > availm &&
10738212SMichael.Corcoran@Sun.COM deficit < lotsfree) {
10748212SMichael.Corcoran@Sun.COM deficit += MIN((pgcnt_t)(npages - availm),
10758212SMichael.Corcoran@Sun.COM lotsfree - deficit);
10768212SMichael.Corcoran@Sun.COM }
10778212SMichael.Corcoran@Sun.COM
10788212SMichael.Corcoran@Sun.COM if (preread) {
10798212SMichael.Corcoran@Sun.COM (void) as_faulta(as, addr, len);
10808212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(map_ptload_preread);
10818212SMichael.Corcoran@Sun.COM }
10828212SMichael.Corcoran@Sun.COM } else {
10838212SMichael.Corcoran@Sun.COM /*
10848212SMichael.Corcoran@Sun.COM * addr and offset were not aligned such that we could
10858212SMichael.Corcoran@Sun.COM * use VOP_MAP, thus we need to as_map the memory we
10868212SMichael.Corcoran@Sun.COM * need and then read the data in from disk.
10878212SMichael.Corcoran@Sun.COM * This code path is a corner case which should never
10888212SMichael.Corcoran@Sun.COM * be taken, but hand crafted binaries could trigger
10898212SMichael.Corcoran@Sun.COM * this logic and it needs to work correctly.
10908212SMichael.Corcoran@Sun.COM */
10918212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(map_ptload_unaligned_text);
10928212SMichael.Corcoran@Sun.COM as_rangelock(as);
10938212SMichael.Corcoran@Sun.COM (void) as_unmap(as, addr, len);
10948212SMichael.Corcoran@Sun.COM
10958212SMichael.Corcoran@Sun.COM /*
10968212SMichael.Corcoran@Sun.COM * We use zfod_argsp because we need to be able to
10978212SMichael.Corcoran@Sun.COM * write to the mapping and then we'll change the
10988212SMichael.Corcoran@Sun.COM * protections later if they are incorrect.
10998212SMichael.Corcoran@Sun.COM */
11008212SMichael.Corcoran@Sun.COM error = as_map(as, addr, len, segvn_create, zfod_argsp);
11018212SMichael.Corcoran@Sun.COM as_rangeunlock(as);
11028212SMichael.Corcoran@Sun.COM if (error) {
11038212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(map_ptload_unaligned_map_fail);
11048212SMichael.Corcoran@Sun.COM return (error);
11058212SMichael.Corcoran@Sun.COM }
11068212SMichael.Corcoran@Sun.COM
11078212SMichael.Corcoran@Sun.COM /* Now read in the data from disk */
11088212SMichael.Corcoran@Sun.COM error = vn_rdwr(UIO_READ, vp, oldaddr, oldlen, offset,
11098212SMichael.Corcoran@Sun.COM UIO_USERSPACE, 0, (rlim64_t)0, fcred, NULL);
11108212SMichael.Corcoran@Sun.COM if (error) {
11118212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(map_ptload_unaligned_read_fail);
11128212SMichael.Corcoran@Sun.COM return (error);
11138212SMichael.Corcoran@Sun.COM }
11148212SMichael.Corcoran@Sun.COM
11158212SMichael.Corcoran@Sun.COM /*
11168212SMichael.Corcoran@Sun.COM * Now set protections.
11178212SMichael.Corcoran@Sun.COM */
11188212SMichael.Corcoran@Sun.COM if (prot != PROT_ZFOD) {
11198212SMichael.Corcoran@Sun.COM (void) as_setprot(as, addr, len, prot);
11208212SMichael.Corcoran@Sun.COM }
11218212SMichael.Corcoran@Sun.COM }
11228212SMichael.Corcoran@Sun.COM }
11238212SMichael.Corcoran@Sun.COM
11248212SMichael.Corcoran@Sun.COM if (zfodlen) {
11258212SMichael.Corcoran@Sun.COM end = (size_t)addr + len;
11268212SMichael.Corcoran@Sun.COM zfodbase = (caddr_t)P2ROUNDUP(end, PAGESIZE);
11278212SMichael.Corcoran@Sun.COM zfoddiff = (uintptr_t)zfodbase - end;
11288212SMichael.Corcoran@Sun.COM if (zfoddiff) {
11298212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(zfoddiff);
11308212SMichael.Corcoran@Sun.COM if ((prot & PROT_WRITE) == 0) {
11318212SMichael.Corcoran@Sun.COM (void) as_setprot(as, (caddr_t)end,
11328212SMichael.Corcoran@Sun.COM zfoddiff, prot | PROT_WRITE);
11338212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(zfoddiff_nowrite);
11348212SMichael.Corcoran@Sun.COM }
11358212SMichael.Corcoran@Sun.COM if (on_fault(&ljb)) {
11368212SMichael.Corcoran@Sun.COM no_fault();
11378212SMichael.Corcoran@Sun.COM if ((prot & PROT_WRITE) == 0) {
11388212SMichael.Corcoran@Sun.COM (void) as_setprot(as, (caddr_t)end,
11398212SMichael.Corcoran@Sun.COM zfoddiff, prot);
11408212SMichael.Corcoran@Sun.COM }
11418212SMichael.Corcoran@Sun.COM return (EFAULT);
11428212SMichael.Corcoran@Sun.COM }
11438212SMichael.Corcoran@Sun.COM uzero((void *)end, zfoddiff);
11448212SMichael.Corcoran@Sun.COM no_fault();
11458212SMichael.Corcoran@Sun.COM
11468212SMichael.Corcoran@Sun.COM /*
11478212SMichael.Corcoran@Sun.COM * Remove write protection to return to original state
11488212SMichael.Corcoran@Sun.COM */
11498212SMichael.Corcoran@Sun.COM if ((prot & PROT_WRITE) == 0) {
11508212SMichael.Corcoran@Sun.COM (void) as_setprot(as, (caddr_t)end,
11518212SMichael.Corcoran@Sun.COM zfoddiff, prot);
11528212SMichael.Corcoran@Sun.COM }
11538212SMichael.Corcoran@Sun.COM }
11548212SMichael.Corcoran@Sun.COM if (zfodlen > zfoddiff) {
11558212SMichael.Corcoran@Sun.COM struct segvn_crargs crargs =
11568212SMichael.Corcoran@Sun.COM SEGVN_ZFOD_ARGS(prot, PROT_ALL);
11578212SMichael.Corcoran@Sun.COM
11588212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(zfodextra);
11598212SMichael.Corcoran@Sun.COM zfodlen -= zfoddiff;
11608212SMichael.Corcoran@Sun.COM crargs.szc = AS_MAP_NO_LPOOB;
11618212SMichael.Corcoran@Sun.COM
11628212SMichael.Corcoran@Sun.COM
11638212SMichael.Corcoran@Sun.COM as_rangelock(as);
11648212SMichael.Corcoran@Sun.COM (void) as_unmap(as, (caddr_t)zfodbase, zfodlen);
11658212SMichael.Corcoran@Sun.COM error = as_map(as, (caddr_t)zfodbase,
11668212SMichael.Corcoran@Sun.COM zfodlen, segvn_create, &crargs);
11678212SMichael.Corcoran@Sun.COM as_rangeunlock(as);
11688212SMichael.Corcoran@Sun.COM if (error) {
11698212SMichael.Corcoran@Sun.COM return (error);
11708212SMichael.Corcoran@Sun.COM }
11718212SMichael.Corcoran@Sun.COM }
11728212SMichael.Corcoran@Sun.COM }
11738212SMichael.Corcoran@Sun.COM return (0);
11748212SMichael.Corcoran@Sun.COM }
11758212SMichael.Corcoran@Sun.COM
11768212SMichael.Corcoran@Sun.COM /*
11778212SMichael.Corcoran@Sun.COM * Map the ELF file represented by vp into the users address space. The
11788212SMichael.Corcoran@Sun.COM * first mapping will start at start_addr and there will be num_elements
11798212SMichael.Corcoran@Sun.COM * mappings. The mappings are described by the data in mrp which may be
11808212SMichael.Corcoran@Sun.COM * modified upon returning from this function.
11818212SMichael.Corcoran@Sun.COM * Returns 0 for success or errno for failure.
11828212SMichael.Corcoran@Sun.COM */
11838212SMichael.Corcoran@Sun.COM static int
mmapobj_map_elf(struct vnode * vp,caddr_t start_addr,mmapobj_result_t * mrp,int num_elements,cred_t * fcred,ushort_t e_type)11848212SMichael.Corcoran@Sun.COM mmapobj_map_elf(struct vnode *vp, caddr_t start_addr, mmapobj_result_t *mrp,
11858212SMichael.Corcoran@Sun.COM int num_elements, cred_t *fcred, ushort_t e_type)
11868212SMichael.Corcoran@Sun.COM {
11878212SMichael.Corcoran@Sun.COM int i;
11888212SMichael.Corcoran@Sun.COM int ret;
11898212SMichael.Corcoran@Sun.COM caddr_t lo;
11908212SMichael.Corcoran@Sun.COM caddr_t hi;
11918212SMichael.Corcoran@Sun.COM struct as *as = curproc->p_as;
11928212SMichael.Corcoran@Sun.COM
11938212SMichael.Corcoran@Sun.COM for (i = 0; i < num_elements; i++) {
11948212SMichael.Corcoran@Sun.COM caddr_t addr;
11958212SMichael.Corcoran@Sun.COM size_t p_memsz;
11968212SMichael.Corcoran@Sun.COM size_t p_filesz;
11978212SMichael.Corcoran@Sun.COM size_t zfodlen;
11988212SMichael.Corcoran@Sun.COM offset_t p_offset;
11998212SMichael.Corcoran@Sun.COM size_t dif;
12008212SMichael.Corcoran@Sun.COM int prot;
12018212SMichael.Corcoran@Sun.COM
12028212SMichael.Corcoran@Sun.COM /* Always need to adjust mr_addr */
12038212SMichael.Corcoran@Sun.COM addr = start_addr + (size_t)(mrp[i].mr_addr);
12048212SMichael.Corcoran@Sun.COM mrp[i].mr_addr =
12058212SMichael.Corcoran@Sun.COM (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
12068212SMichael.Corcoran@Sun.COM
12078212SMichael.Corcoran@Sun.COM /* Padding has already been mapped */
12088212SMichael.Corcoran@Sun.COM if (MR_GET_TYPE(mrp[i].mr_flags) == MR_PADDING) {
12098212SMichael.Corcoran@Sun.COM continue;
12108212SMichael.Corcoran@Sun.COM }
12118212SMichael.Corcoran@Sun.COM p_memsz = mrp[i].mr_msize;
12128212SMichael.Corcoran@Sun.COM p_filesz = mrp[i].mr_fsize;
12138212SMichael.Corcoran@Sun.COM zfodlen = p_memsz - p_filesz;
12148212SMichael.Corcoran@Sun.COM p_offset = mrp[i].mr_offset;
12158212SMichael.Corcoran@Sun.COM dif = (uintptr_t)(addr) & PAGEOFFSET;
12168212SMichael.Corcoran@Sun.COM prot = mrp[i].mr_prot | PROT_USER;
12178212SMichael.Corcoran@Sun.COM ret = mmapobj_map_ptload(vp, addr, p_filesz, zfodlen,
12188212SMichael.Corcoran@Sun.COM p_offset, prot, fcred);
12198212SMichael.Corcoran@Sun.COM if (ret != 0) {
12208212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(ptload_failed);
12218212SMichael.Corcoran@Sun.COM mmapobj_unmap(mrp, i, num_elements, e_type);
12228212SMichael.Corcoran@Sun.COM return (ret);
12238212SMichael.Corcoran@Sun.COM }
12248212SMichael.Corcoran@Sun.COM
12258212SMichael.Corcoran@Sun.COM /* Need to cleanup mrp to reflect the actual values used */
12268212SMichael.Corcoran@Sun.COM mrp[i].mr_msize += dif;
12278212SMichael.Corcoran@Sun.COM mrp[i].mr_offset = (size_t)addr & PAGEOFFSET;
12288212SMichael.Corcoran@Sun.COM }
12298212SMichael.Corcoran@Sun.COM
12308212SMichael.Corcoran@Sun.COM /* Also need to unmap any holes created above */
12318212SMichael.Corcoran@Sun.COM if (num_elements == 1) {
12328212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(map_elf_no_holes);
12338212SMichael.Corcoran@Sun.COM return (0);
12348212SMichael.Corcoran@Sun.COM }
12358212SMichael.Corcoran@Sun.COM if (e_type == ET_EXEC) {
12368212SMichael.Corcoran@Sun.COM return (0);
12378212SMichael.Corcoran@Sun.COM }
12388212SMichael.Corcoran@Sun.COM
12398212SMichael.Corcoran@Sun.COM as_rangelock(as);
12408212SMichael.Corcoran@Sun.COM lo = start_addr;
12418212SMichael.Corcoran@Sun.COM hi = mrp[0].mr_addr;
12428212SMichael.Corcoran@Sun.COM
12438212SMichael.Corcoran@Sun.COM /* Remove holes made by the rest of the segments */
12448212SMichael.Corcoran@Sun.COM for (i = 0; i < num_elements - 1; i++) {
12458212SMichael.Corcoran@Sun.COM lo = (caddr_t)P2ROUNDUP((size_t)(mrp[i].mr_addr) +
12468212SMichael.Corcoran@Sun.COM mrp[i].mr_msize, PAGESIZE);
12478212SMichael.Corcoran@Sun.COM hi = mrp[i + 1].mr_addr;
12488212SMichael.Corcoran@Sun.COM if (lo < hi) {
12498212SMichael.Corcoran@Sun.COM /*
12508212SMichael.Corcoran@Sun.COM * If as_unmap fails we just use up a bit of extra
12518212SMichael.Corcoran@Sun.COM * space
12528212SMichael.Corcoran@Sun.COM */
12538212SMichael.Corcoran@Sun.COM (void) as_unmap(as, (caddr_t)lo,
12548212SMichael.Corcoran@Sun.COM (size_t)hi - (size_t)lo);
12558212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(unmap_hole);
12568212SMichael.Corcoran@Sun.COM }
12578212SMichael.Corcoran@Sun.COM }
12588212SMichael.Corcoran@Sun.COM as_rangeunlock(as);
12598212SMichael.Corcoran@Sun.COM
12608212SMichael.Corcoran@Sun.COM return (0);
12618212SMichael.Corcoran@Sun.COM }
12628212SMichael.Corcoran@Sun.COM
12638212SMichael.Corcoran@Sun.COM /* Ugly hack to get STRUCT_* macros to work below */
12648212SMichael.Corcoran@Sun.COM struct myphdr {
12658212SMichael.Corcoran@Sun.COM Phdr x; /* native version */
12668212SMichael.Corcoran@Sun.COM };
12678212SMichael.Corcoran@Sun.COM
12688212SMichael.Corcoran@Sun.COM struct myphdr32 {
12698212SMichael.Corcoran@Sun.COM Elf32_Phdr x;
12708212SMichael.Corcoran@Sun.COM };
12718212SMichael.Corcoran@Sun.COM
12728212SMichael.Corcoran@Sun.COM /*
12738212SMichael.Corcoran@Sun.COM * Calculate and return the number of loadable segments in the ELF Phdr
12748212SMichael.Corcoran@Sun.COM * represented by phdrbase as well as the len of the total mapping and
12758212SMichael.Corcoran@Sun.COM * the max alignment that is needed for a given segment. On success,
12768212SMichael.Corcoran@Sun.COM * 0 is returned, and *len, *loadable and *align have been filled out.
12778212SMichael.Corcoran@Sun.COM * On failure, errno will be returned, which in this case is ENOTSUP
12788212SMichael.Corcoran@Sun.COM * if we were passed an ELF file with overlapping segments.
12798212SMichael.Corcoran@Sun.COM */
12808212SMichael.Corcoran@Sun.COM static int
calc_loadable(Ehdr * ehdrp,caddr_t phdrbase,int nphdrs,size_t * len,int * loadable,size_t * align)12818212SMichael.Corcoran@Sun.COM calc_loadable(Ehdr *ehdrp, caddr_t phdrbase, int nphdrs, size_t *len,
12828212SMichael.Corcoran@Sun.COM int *loadable, size_t *align)
12838212SMichael.Corcoran@Sun.COM {
12848212SMichael.Corcoran@Sun.COM int i;
12858212SMichael.Corcoran@Sun.COM int hsize;
12868212SMichael.Corcoran@Sun.COM model_t model;
12878578SMichael.Corcoran@Sun.COM ushort_t e_type = ehdrp->e_type; /* same offset 32 and 64 bit */
12888212SMichael.Corcoran@Sun.COM uint_t p_type;
12898212SMichael.Corcoran@Sun.COM offset_t p_offset;
12908212SMichael.Corcoran@Sun.COM size_t p_memsz;
12918212SMichael.Corcoran@Sun.COM size_t p_align;
12928212SMichael.Corcoran@Sun.COM caddr_t vaddr;
12938212SMichael.Corcoran@Sun.COM int num_segs = 0;
12948212SMichael.Corcoran@Sun.COM caddr_t start_addr = NULL;
12958212SMichael.Corcoran@Sun.COM caddr_t p_end = NULL;
12968212SMichael.Corcoran@Sun.COM size_t max_align = 0;
12978362SMichael.Corcoran@Sun.COM size_t min_align = PAGESIZE; /* needed for vmem_xalloc */
12988212SMichael.Corcoran@Sun.COM STRUCT_HANDLE(myphdr, mph);
12998212SMichael.Corcoran@Sun.COM #if defined(__sparc)
13008212SMichael.Corcoran@Sun.COM extern int vac_size;
13018362SMichael.Corcoran@Sun.COM
13028362SMichael.Corcoran@Sun.COM /*
13038362SMichael.Corcoran@Sun.COM * Want to prevent aliasing by making the start address at least be
13048362SMichael.Corcoran@Sun.COM * aligned to vac_size.
13058362SMichael.Corcoran@Sun.COM */
13068362SMichael.Corcoran@Sun.COM min_align = MAX(PAGESIZE, vac_size);
13078212SMichael.Corcoran@Sun.COM #endif
13088212SMichael.Corcoran@Sun.COM
13098212SMichael.Corcoran@Sun.COM model = get_udatamodel();
13108212SMichael.Corcoran@Sun.COM STRUCT_SET_HANDLE(mph, model, (struct myphdr *)phdrbase);
13118212SMichael.Corcoran@Sun.COM
13128212SMichael.Corcoran@Sun.COM /* hsize alignment should have been checked before calling this func */
13138212SMichael.Corcoran@Sun.COM if (model == DATAMODEL_LP64) {
13148212SMichael.Corcoran@Sun.COM hsize = ehdrp->e_phentsize;
13158212SMichael.Corcoran@Sun.COM if (hsize & 7) {
13168212SMichael.Corcoran@Sun.COM return (ENOTSUP);
13178212SMichael.Corcoran@Sun.COM }
13188212SMichael.Corcoran@Sun.COM } else {
13198212SMichael.Corcoran@Sun.COM ASSERT(model == DATAMODEL_ILP32);
13208212SMichael.Corcoran@Sun.COM hsize = ((Elf32_Ehdr *)ehdrp)->e_phentsize;
13218212SMichael.Corcoran@Sun.COM if (hsize & 3) {
13228212SMichael.Corcoran@Sun.COM return (ENOTSUP);
13238212SMichael.Corcoran@Sun.COM }
13248212SMichael.Corcoran@Sun.COM }
13258212SMichael.Corcoran@Sun.COM
13268212SMichael.Corcoran@Sun.COM /*
13278212SMichael.Corcoran@Sun.COM * Determine the span of all loadable segments and calculate the
13288212SMichael.Corcoran@Sun.COM * number of loadable segments.
13298212SMichael.Corcoran@Sun.COM */
13308212SMichael.Corcoran@Sun.COM for (i = 0; i < nphdrs; i++) {
13318212SMichael.Corcoran@Sun.COM p_type = STRUCT_FGET(mph, x.p_type);
13328212SMichael.Corcoran@Sun.COM if (p_type == PT_LOAD || p_type == PT_SUNWBSS) {
13338212SMichael.Corcoran@Sun.COM vaddr = (caddr_t)(uintptr_t)STRUCT_FGET(mph, x.p_vaddr);
13348212SMichael.Corcoran@Sun.COM p_memsz = STRUCT_FGET(mph, x.p_memsz);
13358212SMichael.Corcoran@Sun.COM
13368212SMichael.Corcoran@Sun.COM /*
13378212SMichael.Corcoran@Sun.COM * Skip this header if it requests no memory to be
13388212SMichael.Corcoran@Sun.COM * mapped.
13398212SMichael.Corcoran@Sun.COM */
13408212SMichael.Corcoran@Sun.COM if (p_memsz == 0) {
13418212SMichael.Corcoran@Sun.COM STRUCT_SET_HANDLE(mph, model,
13428212SMichael.Corcoran@Sun.COM (struct myphdr *)((size_t)STRUCT_BUF(mph) +
13438212SMichael.Corcoran@Sun.COM hsize));
13448212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(nomem_header);
13458212SMichael.Corcoran@Sun.COM continue;
13468212SMichael.Corcoran@Sun.COM }
13478212SMichael.Corcoran@Sun.COM if (num_segs++ == 0) {
13488578SMichael.Corcoran@Sun.COM /*
13498578SMichael.Corcoran@Sun.COM * The p_vaddr of the first PT_LOAD segment
13508578SMichael.Corcoran@Sun.COM * must either be NULL or within the first
13518578SMichael.Corcoran@Sun.COM * page in order to be interpreted.
13528578SMichael.Corcoran@Sun.COM * Otherwise, its an invalid file.
13538578SMichael.Corcoran@Sun.COM */
13548578SMichael.Corcoran@Sun.COM if (e_type == ET_DYN &&
13558578SMichael.Corcoran@Sun.COM ((caddr_t)((uintptr_t)vaddr &
13568578SMichael.Corcoran@Sun.COM (uintptr_t)PAGEMASK) != NULL)) {
13578578SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(inval_header);
13588578SMichael.Corcoran@Sun.COM return (ENOTSUP);
13598578SMichael.Corcoran@Sun.COM }
13608212SMichael.Corcoran@Sun.COM start_addr = vaddr;
13618212SMichael.Corcoran@Sun.COM /*
13628212SMichael.Corcoran@Sun.COM * For the first segment, we need to map from
13638212SMichael.Corcoran@Sun.COM * the beginning of the file, so we will
13648212SMichael.Corcoran@Sun.COM * adjust the size of the mapping to include
13658212SMichael.Corcoran@Sun.COM * this memory.
13668212SMichael.Corcoran@Sun.COM */
13678212SMichael.Corcoran@Sun.COM p_offset = STRUCT_FGET(mph, x.p_offset);
13688212SMichael.Corcoran@Sun.COM } else {
13698212SMichael.Corcoran@Sun.COM p_offset = 0;
13708212SMichael.Corcoran@Sun.COM }
13718212SMichael.Corcoran@Sun.COM /*
13728212SMichael.Corcoran@Sun.COM * Check to make sure that this mapping wouldn't
13738212SMichael.Corcoran@Sun.COM * overlap a previous mapping.
13748212SMichael.Corcoran@Sun.COM */
13758212SMichael.Corcoran@Sun.COM if (vaddr < p_end) {
13768212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(overlap_header);
13778212SMichael.Corcoran@Sun.COM return (ENOTSUP);
13788212SMichael.Corcoran@Sun.COM }
13798212SMichael.Corcoran@Sun.COM
13808212SMichael.Corcoran@Sun.COM p_end = vaddr + p_memsz + p_offset;
13818212SMichael.Corcoran@Sun.COM p_end = (caddr_t)P2ROUNDUP((size_t)p_end, PAGESIZE);
13828212SMichael.Corcoran@Sun.COM
13838212SMichael.Corcoran@Sun.COM p_align = STRUCT_FGET(mph, x.p_align);
13848212SMichael.Corcoran@Sun.COM if (p_align > 1 && p_align > max_align) {
13858212SMichael.Corcoran@Sun.COM max_align = p_align;
13868362SMichael.Corcoran@Sun.COM if (max_align < min_align) {
13878362SMichael.Corcoran@Sun.COM max_align = min_align;
13888362SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(min_align);
13898212SMichael.Corcoran@Sun.COM }
13908212SMichael.Corcoran@Sun.COM }
13918212SMichael.Corcoran@Sun.COM }
13928212SMichael.Corcoran@Sun.COM STRUCT_SET_HANDLE(mph, model,
13938212SMichael.Corcoran@Sun.COM (struct myphdr *)((size_t)STRUCT_BUF(mph) + hsize));
13948212SMichael.Corcoran@Sun.COM }
13958212SMichael.Corcoran@Sun.COM
13968212SMichael.Corcoran@Sun.COM /*
13978212SMichael.Corcoran@Sun.COM * The alignment should be a power of 2, if it isn't we forgive it
13988212SMichael.Corcoran@Sun.COM * and round up. On overflow, we'll set the alignment to max_align
13998212SMichael.Corcoran@Sun.COM * rounded down to the nearest power of 2.
14008212SMichael.Corcoran@Sun.COM */
14018212SMichael.Corcoran@Sun.COM if (max_align > 0 && !ISP2(max_align)) {
14028212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(np2_align);
14038212SMichael.Corcoran@Sun.COM *align = 2 * (1L << (highbit(max_align) - 1));
14048212SMichael.Corcoran@Sun.COM if (*align < max_align ||
14058212SMichael.Corcoran@Sun.COM (*align > UINT_MAX && model == DATAMODEL_ILP32)) {
14068212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(np2_align_overflow);
14078212SMichael.Corcoran@Sun.COM *align = 1L << (highbit(max_align) - 1);
14088212SMichael.Corcoran@Sun.COM }
14098212SMichael.Corcoran@Sun.COM } else {
14108212SMichael.Corcoran@Sun.COM *align = max_align;
14118212SMichael.Corcoran@Sun.COM }
14128212SMichael.Corcoran@Sun.COM
14138362SMichael.Corcoran@Sun.COM ASSERT(*align >= PAGESIZE || *align == 0);
14148362SMichael.Corcoran@Sun.COM
14158212SMichael.Corcoran@Sun.COM *loadable = num_segs;
14168212SMichael.Corcoran@Sun.COM *len = p_end - start_addr;
14178212SMichael.Corcoran@Sun.COM return (0);
14188212SMichael.Corcoran@Sun.COM }
14198212SMichael.Corcoran@Sun.COM
14208212SMichael.Corcoran@Sun.COM /*
14218212SMichael.Corcoran@Sun.COM * Check the address space to see if the virtual addresses to be used are
14228212SMichael.Corcoran@Sun.COM * available. If they are not, return errno for failure. On success, 0
14238212SMichael.Corcoran@Sun.COM * will be returned, and the virtual addresses for each mmapobj_result_t
14248212SMichael.Corcoran@Sun.COM * will be reserved. Note that a reservation could have earlier been made
14258212SMichael.Corcoran@Sun.COM * for a given segment via a /dev/null mapping. If that is the case, then
14268212SMichael.Corcoran@Sun.COM * we can use that VA space for our mappings.
14278212SMichael.Corcoran@Sun.COM * Note: this function will only be used for ET_EXEC binaries.
14288212SMichael.Corcoran@Sun.COM */
14298212SMichael.Corcoran@Sun.COM int
check_exec_addrs(int loadable,mmapobj_result_t * mrp,caddr_t start_addr)14308212SMichael.Corcoran@Sun.COM check_exec_addrs(int loadable, mmapobj_result_t *mrp, caddr_t start_addr)
14318212SMichael.Corcoran@Sun.COM {
14328212SMichael.Corcoran@Sun.COM int i;
14338212SMichael.Corcoran@Sun.COM struct as *as = curproc->p_as;
14348212SMichael.Corcoran@Sun.COM struct segvn_crargs crargs = SEGVN_ZFOD_ARGS(PROT_ZFOD, PROT_ALL);
14358212SMichael.Corcoran@Sun.COM int ret;
14368212SMichael.Corcoran@Sun.COM caddr_t myaddr;
14378212SMichael.Corcoran@Sun.COM size_t mylen;
14388212SMichael.Corcoran@Sun.COM struct seg *seg;
14398212SMichael.Corcoran@Sun.COM
14408212SMichael.Corcoran@Sun.COM /* No need to reserve swap space now since it will be reserved later */
14418212SMichael.Corcoran@Sun.COM crargs.flags |= MAP_NORESERVE;
14428212SMichael.Corcoran@Sun.COM as_rangelock(as);
14438212SMichael.Corcoran@Sun.COM for (i = 0; i < loadable; i++) {
14448212SMichael.Corcoran@Sun.COM
14458212SMichael.Corcoran@Sun.COM myaddr = start_addr + (size_t)mrp[i].mr_addr;
14468212SMichael.Corcoran@Sun.COM mylen = mrp[i].mr_msize;
14478212SMichael.Corcoran@Sun.COM
14488212SMichael.Corcoran@Sun.COM /* See if there is a hole in the as for this range */
14498212SMichael.Corcoran@Sun.COM if (as_gap(as, mylen, &myaddr, &mylen, 0, NULL) == 0) {
14508212SMichael.Corcoran@Sun.COM ASSERT(myaddr == start_addr + (size_t)mrp[i].mr_addr);
14518212SMichael.Corcoran@Sun.COM ASSERT(mylen == mrp[i].mr_msize);
14528212SMichael.Corcoran@Sun.COM
14538212SMichael.Corcoran@Sun.COM #ifdef DEBUG
14548212SMichael.Corcoran@Sun.COM if (MR_GET_TYPE(mrp[i].mr_flags) == MR_PADDING) {
14558212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(exec_padding);
14568212SMichael.Corcoran@Sun.COM }
14578212SMichael.Corcoran@Sun.COM #endif
14588212SMichael.Corcoran@Sun.COM ret = as_map(as, myaddr, mylen, segvn_create, &crargs);
14598212SMichael.Corcoran@Sun.COM if (ret) {
14608212SMichael.Corcoran@Sun.COM as_rangeunlock(as);
14618212SMichael.Corcoran@Sun.COM mmapobj_unmap_exec(mrp, i, start_addr);
14628212SMichael.Corcoran@Sun.COM return (ret);
14638212SMichael.Corcoran@Sun.COM }
14648212SMichael.Corcoran@Sun.COM } else {
14658212SMichael.Corcoran@Sun.COM /*
14668212SMichael.Corcoran@Sun.COM * There is a mapping that exists in the range
14678212SMichael.Corcoran@Sun.COM * so check to see if it was a "reservation"
14688212SMichael.Corcoran@Sun.COM * from /dev/null. The mapping is from
14698212SMichael.Corcoran@Sun.COM * /dev/null if the mapping comes from
14708212SMichael.Corcoran@Sun.COM * segdev and the type is neither MAP_SHARED
14718212SMichael.Corcoran@Sun.COM * nor MAP_PRIVATE.
14728212SMichael.Corcoran@Sun.COM */
14738212SMichael.Corcoran@Sun.COM AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
14748212SMichael.Corcoran@Sun.COM seg = as_findseg(as, myaddr, 0);
14758212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(exec_addr_mapped);
14768212SMichael.Corcoran@Sun.COM if (seg && seg->s_ops == &segdev_ops &&
14778212SMichael.Corcoran@Sun.COM ((SEGOP_GETTYPE(seg, myaddr) &
14788212SMichael.Corcoran@Sun.COM (MAP_SHARED | MAP_PRIVATE)) == 0) &&
14798212SMichael.Corcoran@Sun.COM myaddr >= seg->s_base &&
14808212SMichael.Corcoran@Sun.COM myaddr + mylen <=
14818212SMichael.Corcoran@Sun.COM seg->s_base + seg->s_size) {
14828212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(exec_addr_devnull);
14838212SMichael.Corcoran@Sun.COM AS_LOCK_EXIT(as, &as->a_lock);
14848212SMichael.Corcoran@Sun.COM (void) as_unmap(as, myaddr, mylen);
14858212SMichael.Corcoran@Sun.COM ret = as_map(as, myaddr, mylen, segvn_create,
14868212SMichael.Corcoran@Sun.COM &crargs);
14878212SMichael.Corcoran@Sun.COM mrp[i].mr_flags |= MR_RESV;
14888212SMichael.Corcoran@Sun.COM if (ret) {
14898212SMichael.Corcoran@Sun.COM as_rangeunlock(as);
14908212SMichael.Corcoran@Sun.COM /* Need to remap what we unmapped */
14918212SMichael.Corcoran@Sun.COM mmapobj_unmap_exec(mrp, i + 1,
14928212SMichael.Corcoran@Sun.COM start_addr);
14938212SMichael.Corcoran@Sun.COM return (ret);
14948212SMichael.Corcoran@Sun.COM }
14958212SMichael.Corcoran@Sun.COM } else {
14968212SMichael.Corcoran@Sun.COM AS_LOCK_EXIT(as, &as->a_lock);
14978212SMichael.Corcoran@Sun.COM as_rangeunlock(as);
14988212SMichael.Corcoran@Sun.COM mmapobj_unmap_exec(mrp, i, start_addr);
14998212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(exec_addr_in_use);
15008212SMichael.Corcoran@Sun.COM return (EADDRINUSE);
15018212SMichael.Corcoran@Sun.COM }
15028212SMichael.Corcoran@Sun.COM }
15038212SMichael.Corcoran@Sun.COM }
15048212SMichael.Corcoran@Sun.COM as_rangeunlock(as);
15058212SMichael.Corcoran@Sun.COM return (0);
15068212SMichael.Corcoran@Sun.COM }
15078212SMichael.Corcoran@Sun.COM
15088212SMichael.Corcoran@Sun.COM /*
15098212SMichael.Corcoran@Sun.COM * Walk through the ELF program headers and extract all useful information
15108212SMichael.Corcoran@Sun.COM * for PT_LOAD and PT_SUNWBSS segments into mrp.
15118212SMichael.Corcoran@Sun.COM * Return 0 on success or error on failure.
15128212SMichael.Corcoran@Sun.COM */
15138212SMichael.Corcoran@Sun.COM static int
process_phdr(Ehdr * ehdrp,caddr_t phdrbase,int nphdrs,mmapobj_result_t * mrp,vnode_t * vp,uint_t * num_mapped,size_t padding,cred_t * fcred)15148212SMichael.Corcoran@Sun.COM process_phdr(Ehdr *ehdrp, caddr_t phdrbase, int nphdrs, mmapobj_result_t *mrp,
15158212SMichael.Corcoran@Sun.COM vnode_t *vp, uint_t *num_mapped, size_t padding, cred_t *fcred)
15168212SMichael.Corcoran@Sun.COM {
15178212SMichael.Corcoran@Sun.COM int i;
15188212SMichael.Corcoran@Sun.COM caddr_t start_addr = NULL;
15198212SMichael.Corcoran@Sun.COM caddr_t vaddr;
15208212SMichael.Corcoran@Sun.COM size_t len = 0;
15218212SMichael.Corcoran@Sun.COM size_t lib_len = 0;
15228212SMichael.Corcoran@Sun.COM int ret;
15238212SMichael.Corcoran@Sun.COM int prot;
15248212SMichael.Corcoran@Sun.COM struct lib_va *lvp = NULL;
15258212SMichael.Corcoran@Sun.COM vattr_t vattr;
15268212SMichael.Corcoran@Sun.COM struct as *as = curproc->p_as;
15278212SMichael.Corcoran@Sun.COM int error;
15288212SMichael.Corcoran@Sun.COM int loadable = 0;
15298212SMichael.Corcoran@Sun.COM int current = 0;
15308212SMichael.Corcoran@Sun.COM int use_lib_va = 1;
15318212SMichael.Corcoran@Sun.COM size_t align = 0;
15328212SMichael.Corcoran@Sun.COM size_t add_pad = 0;
15338212SMichael.Corcoran@Sun.COM int hdr_seen = 0;
15348212SMichael.Corcoran@Sun.COM ushort_t e_type = ehdrp->e_type; /* same offset 32 and 64 bit */
15358212SMichael.Corcoran@Sun.COM uint_t p_type;
15368212SMichael.Corcoran@Sun.COM offset_t p_offset;
15378212SMichael.Corcoran@Sun.COM size_t p_memsz;
15388212SMichael.Corcoran@Sun.COM size_t p_filesz;
15398212SMichael.Corcoran@Sun.COM uint_t p_flags;
15408212SMichael.Corcoran@Sun.COM int hsize;
15418212SMichael.Corcoran@Sun.COM model_t model;
15428212SMichael.Corcoran@Sun.COM STRUCT_HANDLE(myphdr, mph);
15438212SMichael.Corcoran@Sun.COM
15448212SMichael.Corcoran@Sun.COM model = get_udatamodel();
15458212SMichael.Corcoran@Sun.COM STRUCT_SET_HANDLE(mph, model, (struct myphdr *)phdrbase);
15468212SMichael.Corcoran@Sun.COM
15478212SMichael.Corcoran@Sun.COM /*
15488212SMichael.Corcoran@Sun.COM * Need to make sure that hsize is aligned properly.
15498212SMichael.Corcoran@Sun.COM * For 32bit processes, 4 byte alignment is required.
15508212SMichael.Corcoran@Sun.COM * For 64bit processes, 8 byte alignment is required.
15518212SMichael.Corcoran@Sun.COM * If the alignment isn't correct, we need to return failure
15528212SMichael.Corcoran@Sun.COM * since it could cause an alignment error panic while walking
15538212SMichael.Corcoran@Sun.COM * the phdr array.
15548212SMichael.Corcoran@Sun.COM */
15558212SMichael.Corcoran@Sun.COM if (model == DATAMODEL_LP64) {
15568212SMichael.Corcoran@Sun.COM hsize = ehdrp->e_phentsize;
15578212SMichael.Corcoran@Sun.COM if (hsize & 7) {
15588212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(phent_align64);
15598212SMichael.Corcoran@Sun.COM return (ENOTSUP);
15608212SMichael.Corcoran@Sun.COM }
15618212SMichael.Corcoran@Sun.COM } else {
15628212SMichael.Corcoran@Sun.COM ASSERT(model == DATAMODEL_ILP32);
15638212SMichael.Corcoran@Sun.COM hsize = ((Elf32_Ehdr *)ehdrp)->e_phentsize;
15648212SMichael.Corcoran@Sun.COM if (hsize & 3) {
15658212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(phent_align32);
15668212SMichael.Corcoran@Sun.COM return (ENOTSUP);
15678212SMichael.Corcoran@Sun.COM }
15688212SMichael.Corcoran@Sun.COM }
15698212SMichael.Corcoran@Sun.COM
15708212SMichael.Corcoran@Sun.COM if (padding != 0) {
15718212SMichael.Corcoran@Sun.COM use_lib_va = 0;
15728212SMichael.Corcoran@Sun.COM }
15738212SMichael.Corcoran@Sun.COM if (e_type == ET_DYN) {
15748212SMichael.Corcoran@Sun.COM vattr.va_mask = AT_FSID | AT_NODEID | AT_CTIME | AT_MTIME;
15758212SMichael.Corcoran@Sun.COM error = VOP_GETATTR(vp, &vattr, 0, fcred, NULL);
15768212SMichael.Corcoran@Sun.COM if (error) {
15778212SMichael.Corcoran@Sun.COM return (error);
15788212SMichael.Corcoran@Sun.COM }
15798212SMichael.Corcoran@Sun.COM /* Check to see if we already have a description for this lib */
15808212SMichael.Corcoran@Sun.COM lvp = lib_va_find(&vattr);
15818212SMichael.Corcoran@Sun.COM
15828212SMichael.Corcoran@Sun.COM if (lvp != NULL) {
15838212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(lvp_found);
15848212SMichael.Corcoran@Sun.COM if (use_lib_va) {
15858212SMichael.Corcoran@Sun.COM start_addr = mmapobj_lookup_start_addr(lvp);
15868212SMichael.Corcoran@Sun.COM if (start_addr == NULL) {
15878212SMichael.Corcoran@Sun.COM lib_va_release(lvp);
15888212SMichael.Corcoran@Sun.COM return (ENOMEM);
15898212SMichael.Corcoran@Sun.COM }
15908212SMichael.Corcoran@Sun.COM }
15918212SMichael.Corcoran@Sun.COM
15928212SMichael.Corcoran@Sun.COM /*
15938212SMichael.Corcoran@Sun.COM * loadable may be zero if the original allocator
15948212SMichael.Corcoran@Sun.COM * of lvp hasn't finished setting it up but the rest
15958212SMichael.Corcoran@Sun.COM * of the fields will be accurate.
15968212SMichael.Corcoran@Sun.COM */
15978212SMichael.Corcoran@Sun.COM loadable = lvp->lv_num_segs;
15988212SMichael.Corcoran@Sun.COM len = lvp->lv_len;
15998212SMichael.Corcoran@Sun.COM align = lvp->lv_align;
16008212SMichael.Corcoran@Sun.COM }
16018212SMichael.Corcoran@Sun.COM }
16028212SMichael.Corcoran@Sun.COM
16038212SMichael.Corcoran@Sun.COM /*
16048212SMichael.Corcoran@Sun.COM * Determine the span of all loadable segments and calculate the
16058212SMichael.Corcoran@Sun.COM * number of loadable segments, the total len spanned by the mappings
16068212SMichael.Corcoran@Sun.COM * and the max alignment, if we didn't get them above.
16078212SMichael.Corcoran@Sun.COM */
16088212SMichael.Corcoran@Sun.COM if (loadable == 0) {
16098212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(no_loadable_yet);
16108212SMichael.Corcoran@Sun.COM ret = calc_loadable(ehdrp, phdrbase, nphdrs, &len,
16118212SMichael.Corcoran@Sun.COM &loadable, &align);
16128212SMichael.Corcoran@Sun.COM if (ret != 0) {
16138212SMichael.Corcoran@Sun.COM /*
16148212SMichael.Corcoran@Sun.COM * Since it'd be an invalid file, we shouldn't have
16158212SMichael.Corcoran@Sun.COM * cached it previously.
16168212SMichael.Corcoran@Sun.COM */
16178212SMichael.Corcoran@Sun.COM ASSERT(lvp == NULL);
16188212SMichael.Corcoran@Sun.COM return (ret);
16198212SMichael.Corcoran@Sun.COM }
16208212SMichael.Corcoran@Sun.COM #ifdef DEBUG
16218212SMichael.Corcoran@Sun.COM if (lvp) {
16228212SMichael.Corcoran@Sun.COM ASSERT(len == lvp->lv_len);
16238212SMichael.Corcoran@Sun.COM ASSERT(align == lvp->lv_align);
16248212SMichael.Corcoran@Sun.COM }
16258212SMichael.Corcoran@Sun.COM #endif
16268212SMichael.Corcoran@Sun.COM }
16278212SMichael.Corcoran@Sun.COM
16288212SMichael.Corcoran@Sun.COM /* Make sure there's something to map. */
16298212SMichael.Corcoran@Sun.COM if (len == 0 || loadable == 0) {
16308212SMichael.Corcoran@Sun.COM /*
16318212SMichael.Corcoran@Sun.COM * Since it'd be an invalid file, we shouldn't have
16328212SMichael.Corcoran@Sun.COM * cached it previously.
16338212SMichael.Corcoran@Sun.COM */
16348212SMichael.Corcoran@Sun.COM ASSERT(lvp == NULL);
16358212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(nothing_to_map);
16368212SMichael.Corcoran@Sun.COM return (ENOTSUP);
16378212SMichael.Corcoran@Sun.COM }
16388212SMichael.Corcoran@Sun.COM
16398212SMichael.Corcoran@Sun.COM lib_len = len;
16408212SMichael.Corcoran@Sun.COM if (padding != 0) {
16418212SMichael.Corcoran@Sun.COM loadable += 2;
16428212SMichael.Corcoran@Sun.COM }
16438212SMichael.Corcoran@Sun.COM if (loadable > *num_mapped) {
16448212SMichael.Corcoran@Sun.COM *num_mapped = loadable;
16458212SMichael.Corcoran@Sun.COM /* cleanup previous reservation */
16468212SMichael.Corcoran@Sun.COM if (start_addr) {
16478212SMichael.Corcoran@Sun.COM (void) as_unmap(as, start_addr, lib_len);
16488212SMichael.Corcoran@Sun.COM }
16498212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(e2big);
16508212SMichael.Corcoran@Sun.COM if (lvp) {
16518212SMichael.Corcoran@Sun.COM lib_va_release(lvp);
16528212SMichael.Corcoran@Sun.COM }
16538212SMichael.Corcoran@Sun.COM return (E2BIG);
16548212SMichael.Corcoran@Sun.COM }
16558212SMichael.Corcoran@Sun.COM
16568212SMichael.Corcoran@Sun.COM /*
16578212SMichael.Corcoran@Sun.COM * We now know the size of the object to map and now we need to
16588212SMichael.Corcoran@Sun.COM * get the start address to map it at. It's possible we already
16598212SMichael.Corcoran@Sun.COM * have it if we found all the info we need in the lib_va cache.
16608212SMichael.Corcoran@Sun.COM */
16618212SMichael.Corcoran@Sun.COM if (e_type == ET_DYN && start_addr == NULL) {
16628212SMichael.Corcoran@Sun.COM /*
16638212SMichael.Corcoran@Sun.COM * Need to make sure padding does not throw off
16648212SMichael.Corcoran@Sun.COM * required alignment. We can only specify an
16658212SMichael.Corcoran@Sun.COM * alignment for the starting address to be mapped,
16668212SMichael.Corcoran@Sun.COM * so we round padding up to the alignment and map
16678212SMichael.Corcoran@Sun.COM * from there and then throw out the extra later.
16688212SMichael.Corcoran@Sun.COM */
16698212SMichael.Corcoran@Sun.COM if (padding != 0) {
16708212SMichael.Corcoran@Sun.COM if (align > 1) {
16718212SMichael.Corcoran@Sun.COM add_pad = P2ROUNDUP(padding, align);
16728212SMichael.Corcoran@Sun.COM len += add_pad;
16738212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(dyn_pad_align);
16748212SMichael.Corcoran@Sun.COM } else {
16758212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(dyn_pad_noalign);
16768212SMichael.Corcoran@Sun.COM len += padding; /* at beginning */
16778212SMichael.Corcoran@Sun.COM }
16788212SMichael.Corcoran@Sun.COM len += padding; /* at end of mapping */
16798212SMichael.Corcoran@Sun.COM }
16808212SMichael.Corcoran@Sun.COM /*
16818212SMichael.Corcoran@Sun.COM * At this point, if lvp is non-NULL, then above we
16828212SMichael.Corcoran@Sun.COM * already found it in the cache but did not get
16838212SMichael.Corcoran@Sun.COM * the start address since we were not going to use lib_va.
16848212SMichael.Corcoran@Sun.COM * Since we know that lib_va will not be used, it's safe
16858212SMichael.Corcoran@Sun.COM * to call mmapobj_alloc_start_addr and know that lvp
16868212SMichael.Corcoran@Sun.COM * will not be modified.
16878212SMichael.Corcoran@Sun.COM */
16888212SMichael.Corcoran@Sun.COM ASSERT(lvp ? use_lib_va == 0 : 1);
16898212SMichael.Corcoran@Sun.COM start_addr = mmapobj_alloc_start_addr(&lvp, len,
16908212SMichael.Corcoran@Sun.COM use_lib_va, align, &vattr);
16918212SMichael.Corcoran@Sun.COM if (start_addr == NULL) {
16928212SMichael.Corcoran@Sun.COM if (lvp) {
16938212SMichael.Corcoran@Sun.COM lib_va_release(lvp);
16948212SMichael.Corcoran@Sun.COM }
16958212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(alloc_start_fail);
16968212SMichael.Corcoran@Sun.COM return (ENOMEM);
16978212SMichael.Corcoran@Sun.COM }
16988212SMichael.Corcoran@Sun.COM /*
16998212SMichael.Corcoran@Sun.COM * If we can't cache it, no need to hang on to it.
17008212SMichael.Corcoran@Sun.COM * Setting lv_num_segs to non-zero will make that
17018212SMichael.Corcoran@Sun.COM * field active and since there are too many segments
17028212SMichael.Corcoran@Sun.COM * to cache, all future users will not try to use lv_mps.
17038212SMichael.Corcoran@Sun.COM */
17048212SMichael.Corcoran@Sun.COM if (lvp != NULL && loadable > LIBVA_CACHED_SEGS && use_lib_va) {
17058212SMichael.Corcoran@Sun.COM lvp->lv_num_segs = loadable;
17068212SMichael.Corcoran@Sun.COM lib_va_release(lvp);
17078212SMichael.Corcoran@Sun.COM lvp = NULL;
17088212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(lvp_nocache);
17098212SMichael.Corcoran@Sun.COM }
17108212SMichael.Corcoran@Sun.COM /*
17118212SMichael.Corcoran@Sun.COM * Free the beginning of the mapping if the padding
17128212SMichael.Corcoran@Sun.COM * was not aligned correctly.
17138212SMichael.Corcoran@Sun.COM */
17148212SMichael.Corcoran@Sun.COM if (padding != 0 && add_pad != padding) {
17158212SMichael.Corcoran@Sun.COM (void) as_unmap(as, start_addr,
17168212SMichael.Corcoran@Sun.COM add_pad - padding);
17178212SMichael.Corcoran@Sun.COM start_addr += (add_pad - padding);
17188212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(extra_padding);
17198212SMichael.Corcoran@Sun.COM }
17208212SMichael.Corcoran@Sun.COM }
17218212SMichael.Corcoran@Sun.COM
17228212SMichael.Corcoran@Sun.COM /*
17238212SMichael.Corcoran@Sun.COM * At this point, we have reserved the virtual address space
17248212SMichael.Corcoran@Sun.COM * for our mappings. Now we need to start filling out the mrp
17258212SMichael.Corcoran@Sun.COM * array to describe all of the individual mappings we are going
17268212SMichael.Corcoran@Sun.COM * to return.
17278212SMichael.Corcoran@Sun.COM * For ET_EXEC there has been no memory reservation since we are
17288212SMichael.Corcoran@Sun.COM * using fixed addresses. While filling in the mrp array below,
17298212SMichael.Corcoran@Sun.COM * we will have the first segment biased to start at addr 0
17308212SMichael.Corcoran@Sun.COM * and the rest will be biased by this same amount. Thus if there
17318212SMichael.Corcoran@Sun.COM * is padding, the first padding will start at addr 0, and the next
17328212SMichael.Corcoran@Sun.COM * segment will start at the value of padding.
17338212SMichael.Corcoran@Sun.COM */
17348212SMichael.Corcoran@Sun.COM
17358212SMichael.Corcoran@Sun.COM /* We'll fill out padding later, so start filling in mrp at index 1 */
17368212SMichael.Corcoran@Sun.COM if (padding != 0) {
17378212SMichael.Corcoran@Sun.COM current = 1;
17388212SMichael.Corcoran@Sun.COM }
17398212SMichael.Corcoran@Sun.COM
17408212SMichael.Corcoran@Sun.COM /* If we have no more need for lvp let it go now */
17418212SMichael.Corcoran@Sun.COM if (lvp != NULL && use_lib_va == 0) {
17428212SMichael.Corcoran@Sun.COM lib_va_release(lvp);
17438212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(lvp_not_needed);
17448212SMichael.Corcoran@Sun.COM lvp = NULL;
17458212SMichael.Corcoran@Sun.COM }
17468212SMichael.Corcoran@Sun.COM
17478212SMichael.Corcoran@Sun.COM /* Now fill out the mrp structs from the program headers */
17488212SMichael.Corcoran@Sun.COM STRUCT_SET_HANDLE(mph, model, (struct myphdr *)phdrbase);
17498212SMichael.Corcoran@Sun.COM for (i = 0; i < nphdrs; i++) {
17508212SMichael.Corcoran@Sun.COM p_type = STRUCT_FGET(mph, x.p_type);
17518212SMichael.Corcoran@Sun.COM if (p_type == PT_LOAD || p_type == PT_SUNWBSS) {
17528212SMichael.Corcoran@Sun.COM vaddr = (caddr_t)(uintptr_t)STRUCT_FGET(mph, x.p_vaddr);
17538212SMichael.Corcoran@Sun.COM p_memsz = STRUCT_FGET(mph, x.p_memsz);
17548212SMichael.Corcoran@Sun.COM p_filesz = STRUCT_FGET(mph, x.p_filesz);
17558212SMichael.Corcoran@Sun.COM p_offset = STRUCT_FGET(mph, x.p_offset);
17568212SMichael.Corcoran@Sun.COM p_flags = STRUCT_FGET(mph, x.p_flags);
17578212SMichael.Corcoran@Sun.COM
17588212SMichael.Corcoran@Sun.COM /*
17598212SMichael.Corcoran@Sun.COM * Skip this header if it requests no memory to be
17608212SMichael.Corcoran@Sun.COM * mapped.
17618212SMichael.Corcoran@Sun.COM */
17628212SMichael.Corcoran@Sun.COM if (p_memsz == 0) {
17638212SMichael.Corcoran@Sun.COM STRUCT_SET_HANDLE(mph, model,
17648212SMichael.Corcoran@Sun.COM (struct myphdr *)((size_t)STRUCT_BUF(mph) +
17658212SMichael.Corcoran@Sun.COM hsize));
17668212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(no_mem_map_sz);
17678212SMichael.Corcoran@Sun.COM continue;
17688212SMichael.Corcoran@Sun.COM }
17698212SMichael.Corcoran@Sun.COM
17708212SMichael.Corcoran@Sun.COM prot = 0;
17718212SMichael.Corcoran@Sun.COM if (p_flags & PF_R)
17728212SMichael.Corcoran@Sun.COM prot |= PROT_READ;
17738212SMichael.Corcoran@Sun.COM if (p_flags & PF_W)
17748212SMichael.Corcoran@Sun.COM prot |= PROT_WRITE;
17758212SMichael.Corcoran@Sun.COM if (p_flags & PF_X)
17768212SMichael.Corcoran@Sun.COM prot |= PROT_EXEC;
17778212SMichael.Corcoran@Sun.COM
17788212SMichael.Corcoran@Sun.COM ASSERT(current < loadable);
17798212SMichael.Corcoran@Sun.COM mrp[current].mr_msize = p_memsz;
17808212SMichael.Corcoran@Sun.COM mrp[current].mr_fsize = p_filesz;
17818212SMichael.Corcoran@Sun.COM mrp[current].mr_offset = p_offset;
17828212SMichael.Corcoran@Sun.COM mrp[current].mr_prot = prot;
17838212SMichael.Corcoran@Sun.COM
17848212SMichael.Corcoran@Sun.COM if (hdr_seen == 0 && p_filesz != 0) {
17858212SMichael.Corcoran@Sun.COM mrp[current].mr_flags = MR_HDR_ELF;
17868212SMichael.Corcoran@Sun.COM /*
17878578SMichael.Corcoran@Sun.COM * We modify mr_offset because we
17888212SMichael.Corcoran@Sun.COM * need to map the ELF header as well, and if
17898212SMichael.Corcoran@Sun.COM * we didn't then the header could be left out
17908212SMichael.Corcoran@Sun.COM * of the mapping that we will create later.
17918212SMichael.Corcoran@Sun.COM * Since we're removing the offset, we need to
17928212SMichael.Corcoran@Sun.COM * account for that in the other fields as well
17938212SMichael.Corcoran@Sun.COM * since we will be mapping the memory from 0
17948212SMichael.Corcoran@Sun.COM * to p_offset.
17958212SMichael.Corcoran@Sun.COM */
17968212SMichael.Corcoran@Sun.COM if (e_type == ET_DYN) {
17978212SMichael.Corcoran@Sun.COM mrp[current].mr_offset = 0;
17988212SMichael.Corcoran@Sun.COM mrp[current].mr_msize += p_offset;
17998212SMichael.Corcoran@Sun.COM mrp[current].mr_fsize += p_offset;
18008212SMichael.Corcoran@Sun.COM } else {
18018212SMichael.Corcoran@Sun.COM ASSERT(e_type == ET_EXEC);
18028212SMichael.Corcoran@Sun.COM /*
18038212SMichael.Corcoran@Sun.COM * Save off the start addr which will be
18048212SMichael.Corcoran@Sun.COM * our bias for the rest of the
18058212SMichael.Corcoran@Sun.COM * ET_EXEC mappings.
18068212SMichael.Corcoran@Sun.COM */
18078212SMichael.Corcoran@Sun.COM start_addr = vaddr - padding;
18088212SMichael.Corcoran@Sun.COM }
18098212SMichael.Corcoran@Sun.COM mrp[current].mr_addr = (caddr_t)padding;
18108212SMichael.Corcoran@Sun.COM hdr_seen = 1;
18118212SMichael.Corcoran@Sun.COM } else {
18128212SMichael.Corcoran@Sun.COM if (e_type == ET_EXEC) {
18138212SMichael.Corcoran@Sun.COM /* bias mr_addr */
18148212SMichael.Corcoran@Sun.COM mrp[current].mr_addr =
18158212SMichael.Corcoran@Sun.COM vaddr - (size_t)start_addr;
18168212SMichael.Corcoran@Sun.COM } else {
18178212SMichael.Corcoran@Sun.COM mrp[current].mr_addr = vaddr + padding;
18188212SMichael.Corcoran@Sun.COM }
18198212SMichael.Corcoran@Sun.COM mrp[current].mr_flags = 0;
18208212SMichael.Corcoran@Sun.COM }
18218212SMichael.Corcoran@Sun.COM current++;
18228212SMichael.Corcoran@Sun.COM }
18238212SMichael.Corcoran@Sun.COM
18248212SMichael.Corcoran@Sun.COM /* Move to next phdr */
18258212SMichael.Corcoran@Sun.COM STRUCT_SET_HANDLE(mph, model,
18268212SMichael.Corcoran@Sun.COM (struct myphdr *)((size_t)STRUCT_BUF(mph) +
18278212SMichael.Corcoran@Sun.COM hsize));
18288212SMichael.Corcoran@Sun.COM }
18298212SMichael.Corcoran@Sun.COM
18308212SMichael.Corcoran@Sun.COM /* Now fill out the padding segments */
18318212SMichael.Corcoran@Sun.COM if (padding != 0) {
18328212SMichael.Corcoran@Sun.COM mrp[0].mr_addr = NULL;
18338212SMichael.Corcoran@Sun.COM mrp[0].mr_msize = padding;
18348212SMichael.Corcoran@Sun.COM mrp[0].mr_fsize = 0;
18358212SMichael.Corcoran@Sun.COM mrp[0].mr_offset = 0;
18368212SMichael.Corcoran@Sun.COM mrp[0].mr_prot = 0;
18378212SMichael.Corcoran@Sun.COM mrp[0].mr_flags = MR_PADDING;
18388212SMichael.Corcoran@Sun.COM
18398212SMichael.Corcoran@Sun.COM /* Setup padding for the last segment */
18408212SMichael.Corcoran@Sun.COM ASSERT(current == loadable - 1);
18418212SMichael.Corcoran@Sun.COM mrp[current].mr_addr = (caddr_t)lib_len + padding;
18428212SMichael.Corcoran@Sun.COM mrp[current].mr_msize = padding;
18438212SMichael.Corcoran@Sun.COM mrp[current].mr_fsize = 0;
18448212SMichael.Corcoran@Sun.COM mrp[current].mr_offset = 0;
18458212SMichael.Corcoran@Sun.COM mrp[current].mr_prot = 0;
18468212SMichael.Corcoran@Sun.COM mrp[current].mr_flags = MR_PADDING;
18478212SMichael.Corcoran@Sun.COM }
18488212SMichael.Corcoran@Sun.COM
18498212SMichael.Corcoran@Sun.COM /*
18508212SMichael.Corcoran@Sun.COM * Need to make sure address ranges desired are not in use or
18518212SMichael.Corcoran@Sun.COM * are previously allocated reservations from /dev/null. For
18528212SMichael.Corcoran@Sun.COM * ET_DYN, we already made sure our address range was free.
18538212SMichael.Corcoran@Sun.COM */
18548212SMichael.Corcoran@Sun.COM if (e_type == ET_EXEC) {
18558212SMichael.Corcoran@Sun.COM ret = check_exec_addrs(loadable, mrp, start_addr);
18568212SMichael.Corcoran@Sun.COM if (ret != 0) {
18578212SMichael.Corcoran@Sun.COM ASSERT(lvp == NULL);
18588212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(check_exec_failed);
18598212SMichael.Corcoran@Sun.COM return (ret);
18608212SMichael.Corcoran@Sun.COM }
18618212SMichael.Corcoran@Sun.COM }
18628212SMichael.Corcoran@Sun.COM
18638212SMichael.Corcoran@Sun.COM /* Finish up our business with lvp. */
18648212SMichael.Corcoran@Sun.COM if (lvp) {
18658212SMichael.Corcoran@Sun.COM ASSERT(e_type == ET_DYN);
18668212SMichael.Corcoran@Sun.COM if (lvp->lv_num_segs == 0 && loadable <= LIBVA_CACHED_SEGS) {
18678212SMichael.Corcoran@Sun.COM bcopy(mrp, lvp->lv_mps,
18688212SMichael.Corcoran@Sun.COM loadable * sizeof (mmapobj_result_t));
18698212SMichael.Corcoran@Sun.COM membar_producer();
18708212SMichael.Corcoran@Sun.COM }
18718212SMichael.Corcoran@Sun.COM /*
18728212SMichael.Corcoran@Sun.COM * Setting lv_num_segs to a non-zero value indicates that
18738212SMichael.Corcoran@Sun.COM * lv_mps is now valid and can be used by other threads.
18748212SMichael.Corcoran@Sun.COM * So, the above stores need to finish before lv_num_segs
18758212SMichael.Corcoran@Sun.COM * is updated. lv_mps is only valid if lv_num_segs is
18768212SMichael.Corcoran@Sun.COM * greater than LIBVA_CACHED_SEGS.
18778212SMichael.Corcoran@Sun.COM */
18788212SMichael.Corcoran@Sun.COM lvp->lv_num_segs = loadable;
18798212SMichael.Corcoran@Sun.COM lib_va_release(lvp);
18808212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(lvp_used);
18818212SMichael.Corcoran@Sun.COM }
18828212SMichael.Corcoran@Sun.COM
18838212SMichael.Corcoran@Sun.COM /* Now that we have mrp completely filled out go map it */
18848212SMichael.Corcoran@Sun.COM ret = mmapobj_map_elf(vp, start_addr, mrp, loadable, fcred, e_type);
18858212SMichael.Corcoran@Sun.COM if (ret == 0) {
18868212SMichael.Corcoran@Sun.COM *num_mapped = loadable;
18878212SMichael.Corcoran@Sun.COM }
18888212SMichael.Corcoran@Sun.COM
18898212SMichael.Corcoran@Sun.COM return (ret);
18908212SMichael.Corcoran@Sun.COM }
18918212SMichael.Corcoran@Sun.COM
18928212SMichael.Corcoran@Sun.COM /*
18938212SMichael.Corcoran@Sun.COM * Take the ELF file passed in, and do the work of mapping it.
18948212SMichael.Corcoran@Sun.COM * num_mapped in - # elements in user buffer
18958212SMichael.Corcoran@Sun.COM * num_mapped out - # sections mapped and length of mrp array if
18968212SMichael.Corcoran@Sun.COM * no errors.
18978212SMichael.Corcoran@Sun.COM */
18988212SMichael.Corcoran@Sun.COM static int
doelfwork(Ehdr * ehdrp,vnode_t * vp,mmapobj_result_t * mrp,uint_t * num_mapped,size_t padding,cred_t * fcred)18998212SMichael.Corcoran@Sun.COM doelfwork(Ehdr *ehdrp, vnode_t *vp, mmapobj_result_t *mrp,
19008212SMichael.Corcoran@Sun.COM uint_t *num_mapped, size_t padding, cred_t *fcred)
19018212SMichael.Corcoran@Sun.COM {
19028212SMichael.Corcoran@Sun.COM int error;
19038212SMichael.Corcoran@Sun.COM offset_t phoff;
19048212SMichael.Corcoran@Sun.COM int nphdrs;
19058212SMichael.Corcoran@Sun.COM unsigned char ei_class;
19068212SMichael.Corcoran@Sun.COM unsigned short phentsize;
19078212SMichael.Corcoran@Sun.COM ssize_t phsizep;
19088212SMichael.Corcoran@Sun.COM caddr_t phbasep;
19098212SMichael.Corcoran@Sun.COM int to_map;
19108212SMichael.Corcoran@Sun.COM model_t model;
19118212SMichael.Corcoran@Sun.COM
19128212SMichael.Corcoran@Sun.COM ei_class = ehdrp->e_ident[EI_CLASS];
19138212SMichael.Corcoran@Sun.COM model = get_udatamodel();
19148212SMichael.Corcoran@Sun.COM if ((model == DATAMODEL_ILP32 && ei_class == ELFCLASS64) ||
19158212SMichael.Corcoran@Sun.COM (model == DATAMODEL_LP64 && ei_class == ELFCLASS32)) {
19168212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(wrong_model);
19178212SMichael.Corcoran@Sun.COM return (ENOTSUP);
19188212SMichael.Corcoran@Sun.COM }
19198212SMichael.Corcoran@Sun.COM
19208212SMichael.Corcoran@Sun.COM /* Can't execute code from "noexec" mounted filesystem. */
19218212SMichael.Corcoran@Sun.COM if (ehdrp->e_type == ET_EXEC &&
19228212SMichael.Corcoran@Sun.COM (vp->v_vfsp->vfs_flag & VFS_NOEXEC) != 0) {
19238212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(noexec_fs);
19248212SMichael.Corcoran@Sun.COM return (EACCES);
19258212SMichael.Corcoran@Sun.COM }
19268212SMichael.Corcoran@Sun.COM
19278212SMichael.Corcoran@Sun.COM /*
19288212SMichael.Corcoran@Sun.COM * Relocatable and core files are mapped as a single flat file
19298212SMichael.Corcoran@Sun.COM * since no interpretation is done on them by mmapobj.
19308212SMichael.Corcoran@Sun.COM */
19318212SMichael.Corcoran@Sun.COM if (ehdrp->e_type == ET_REL || ehdrp->e_type == ET_CORE) {
19328212SMichael.Corcoran@Sun.COM to_map = padding ? 3 : 1;
19338212SMichael.Corcoran@Sun.COM if (*num_mapped < to_map) {
19348212SMichael.Corcoran@Sun.COM *num_mapped = to_map;
19358212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(e2big_et_rel);
19368212SMichael.Corcoran@Sun.COM return (E2BIG);
19378212SMichael.Corcoran@Sun.COM }
19388212SMichael.Corcoran@Sun.COM error = mmapobj_map_flat(vp, mrp, padding, fcred);
19398212SMichael.Corcoran@Sun.COM if (error == 0) {
19408212SMichael.Corcoran@Sun.COM *num_mapped = to_map;
19418212SMichael.Corcoran@Sun.COM mrp[padding ? 1 : 0].mr_flags = MR_HDR_ELF;
19428212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(et_rel_mapped);
19438212SMichael.Corcoran@Sun.COM }
19448212SMichael.Corcoran@Sun.COM return (error);
19458212SMichael.Corcoran@Sun.COM }
19468212SMichael.Corcoran@Sun.COM
19478212SMichael.Corcoran@Sun.COM /* Check for an unknown ELF type */
19488212SMichael.Corcoran@Sun.COM if (ehdrp->e_type != ET_EXEC && ehdrp->e_type != ET_DYN) {
19498212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(unknown_elf_type);
19508212SMichael.Corcoran@Sun.COM return (ENOTSUP);
19518212SMichael.Corcoran@Sun.COM }
19528212SMichael.Corcoran@Sun.COM
19538212SMichael.Corcoran@Sun.COM if (ei_class == ELFCLASS32) {
19548212SMichael.Corcoran@Sun.COM Elf32_Ehdr *e32hdr = (Elf32_Ehdr *)ehdrp;
19558212SMichael.Corcoran@Sun.COM ASSERT(model == DATAMODEL_ILP32);
19568212SMichael.Corcoran@Sun.COM nphdrs = e32hdr->e_phnum;
19578212SMichael.Corcoran@Sun.COM phentsize = e32hdr->e_phentsize;
19588212SMichael.Corcoran@Sun.COM if (phentsize < sizeof (Elf32_Phdr)) {
19598212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(phent32_too_small);
19608212SMichael.Corcoran@Sun.COM return (ENOTSUP);
19618212SMichael.Corcoran@Sun.COM }
19628212SMichael.Corcoran@Sun.COM phoff = e32hdr->e_phoff;
19638212SMichael.Corcoran@Sun.COM } else if (ei_class == ELFCLASS64) {
19648212SMichael.Corcoran@Sun.COM Elf64_Ehdr *e64hdr = (Elf64_Ehdr *)ehdrp;
19658212SMichael.Corcoran@Sun.COM ASSERT(model == DATAMODEL_LP64);
19668212SMichael.Corcoran@Sun.COM nphdrs = e64hdr->e_phnum;
19678212SMichael.Corcoran@Sun.COM phentsize = e64hdr->e_phentsize;
19688212SMichael.Corcoran@Sun.COM if (phentsize < sizeof (Elf64_Phdr)) {
19698212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(phent64_too_small);
19708212SMichael.Corcoran@Sun.COM return (ENOTSUP);
19718212SMichael.Corcoran@Sun.COM }
19728212SMichael.Corcoran@Sun.COM phoff = e64hdr->e_phoff;
19738212SMichael.Corcoran@Sun.COM } else {
19748212SMichael.Corcoran@Sun.COM /* fallthrough case for an invalid ELF class */
19758212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(inval_elf_class);
19768212SMichael.Corcoran@Sun.COM return (ENOTSUP);
19778212SMichael.Corcoran@Sun.COM }
19788212SMichael.Corcoran@Sun.COM
19798212SMichael.Corcoran@Sun.COM /*
19808212SMichael.Corcoran@Sun.COM * nphdrs should only have this value for core files which are handled
19818212SMichael.Corcoran@Sun.COM * above as a single mapping. If other file types ever use this
19828212SMichael.Corcoran@Sun.COM * sentinel, then we'll add the support needed to handle this here.
19838212SMichael.Corcoran@Sun.COM */
19848212SMichael.Corcoran@Sun.COM if (nphdrs == PN_XNUM) {
19858212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(too_many_phdrs);
19868212SMichael.Corcoran@Sun.COM return (ENOTSUP);
19878212SMichael.Corcoran@Sun.COM }
19888212SMichael.Corcoran@Sun.COM
19898212SMichael.Corcoran@Sun.COM phsizep = nphdrs * phentsize;
19908212SMichael.Corcoran@Sun.COM
19918212SMichael.Corcoran@Sun.COM if (phsizep == 0) {
19928212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(no_phsize);
19938212SMichael.Corcoran@Sun.COM return (ENOTSUP);
19948212SMichael.Corcoran@Sun.COM }
19958212SMichael.Corcoran@Sun.COM
19968212SMichael.Corcoran@Sun.COM /* Make sure we only wait for memory if it's a reasonable request */
19978212SMichael.Corcoran@Sun.COM if (phsizep > mmapobj_alloc_threshold) {
19988212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(phsize_large);
19998212SMichael.Corcoran@Sun.COM if ((phbasep = kmem_alloc(phsizep, KM_NOSLEEP)) == NULL) {
20008212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(phsize_xtralarge);
20018212SMichael.Corcoran@Sun.COM return (ENOMEM);
20028212SMichael.Corcoran@Sun.COM }
20038212SMichael.Corcoran@Sun.COM } else {
20048212SMichael.Corcoran@Sun.COM phbasep = kmem_alloc(phsizep, KM_SLEEP);
20058212SMichael.Corcoran@Sun.COM }
20068212SMichael.Corcoran@Sun.COM
20078212SMichael.Corcoran@Sun.COM if ((error = vn_rdwr(UIO_READ, vp, phbasep, phsizep,
20088212SMichael.Corcoran@Sun.COM (offset_t)phoff, UIO_SYSSPACE, 0, (rlim64_t)0,
20098212SMichael.Corcoran@Sun.COM fcred, NULL)) != 0) {
20108212SMichael.Corcoran@Sun.COM kmem_free(phbasep, phsizep);
20118212SMichael.Corcoran@Sun.COM return (error);
20128212SMichael.Corcoran@Sun.COM }
20138212SMichael.Corcoran@Sun.COM
20148212SMichael.Corcoran@Sun.COM /* Now process the phdr's */
20158212SMichael.Corcoran@Sun.COM error = process_phdr(ehdrp, phbasep, nphdrs, mrp, vp, num_mapped,
20168212SMichael.Corcoran@Sun.COM padding, fcred);
20178212SMichael.Corcoran@Sun.COM kmem_free(phbasep, phsizep);
20188212SMichael.Corcoran@Sun.COM return (error);
20198212SMichael.Corcoran@Sun.COM }
20208212SMichael.Corcoran@Sun.COM
20218212SMichael.Corcoran@Sun.COM #if defined(__sparc)
20228212SMichael.Corcoran@Sun.COM /*
20238212SMichael.Corcoran@Sun.COM * Hack to support 64 bit kernels running AOUT 4.x programs.
20248212SMichael.Corcoran@Sun.COM * This is the sizeof (struct nlist) for a 32 bit kernel.
20258212SMichael.Corcoran@Sun.COM * Since AOUT programs are 32 bit only, they will never use the 64 bit
20268212SMichael.Corcoran@Sun.COM * sizeof (struct nlist) and thus creating a #define is the simplest
20278212SMichael.Corcoran@Sun.COM * way around this since this is a format which is not being updated.
20288212SMichael.Corcoran@Sun.COM * This will be used in the place of sizeof (struct nlist) below.
20298212SMichael.Corcoran@Sun.COM */
20308212SMichael.Corcoran@Sun.COM #define NLIST_SIZE (0xC)
20318212SMichael.Corcoran@Sun.COM
20328212SMichael.Corcoran@Sun.COM static int
doaoutwork(vnode_t * vp,mmapobj_result_t * mrp,uint_t * num_mapped,struct exec * hdr,cred_t * fcred)20338212SMichael.Corcoran@Sun.COM doaoutwork(vnode_t *vp, mmapobj_result_t *mrp,
20348212SMichael.Corcoran@Sun.COM uint_t *num_mapped, struct exec *hdr, cred_t *fcred)
20358212SMichael.Corcoran@Sun.COM {
20368212SMichael.Corcoran@Sun.COM int error;
20378212SMichael.Corcoran@Sun.COM size_t size;
20388212SMichael.Corcoran@Sun.COM size_t osize;
20398212SMichael.Corcoran@Sun.COM size_t nsize; /* nlist size */
20408212SMichael.Corcoran@Sun.COM size_t msize;
20418212SMichael.Corcoran@Sun.COM size_t zfoddiff;
20428212SMichael.Corcoran@Sun.COM caddr_t addr;
20438212SMichael.Corcoran@Sun.COM caddr_t start_addr;
20448212SMichael.Corcoran@Sun.COM struct as *as = curproc->p_as;
20458212SMichael.Corcoran@Sun.COM int prot = PROT_USER | PROT_READ | PROT_EXEC;
20468212SMichael.Corcoran@Sun.COM uint_t mflag = MAP_PRIVATE | _MAP_LOW32;
20478212SMichael.Corcoran@Sun.COM offset_t off = 0;
20488212SMichael.Corcoran@Sun.COM int segnum = 0;
20498212SMichael.Corcoran@Sun.COM uint_t to_map;
20508212SMichael.Corcoran@Sun.COM int is_library = 0;
20518212SMichael.Corcoran@Sun.COM struct segvn_crargs crargs = SEGVN_ZFOD_ARGS(PROT_ZFOD, PROT_ALL);
20528212SMichael.Corcoran@Sun.COM
20538212SMichael.Corcoran@Sun.COM /* Only 32bit apps supported by this file format */
20548212SMichael.Corcoran@Sun.COM if (get_udatamodel() != DATAMODEL_ILP32) {
20558212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(aout_64bit_try);
20568212SMichael.Corcoran@Sun.COM return (ENOTSUP);
20578212SMichael.Corcoran@Sun.COM }
20588212SMichael.Corcoran@Sun.COM
20598212SMichael.Corcoran@Sun.COM /* Check to see if this is a library */
20608212SMichael.Corcoran@Sun.COM if (hdr->a_magic == ZMAGIC && hdr->a_entry < PAGESIZE) {
20618212SMichael.Corcoran@Sun.COM is_library = 1;
20628212SMichael.Corcoran@Sun.COM }
20638212SMichael.Corcoran@Sun.COM
20648212SMichael.Corcoran@Sun.COM /* Can't execute code from "noexec" mounted filesystem. */
20658212SMichael.Corcoran@Sun.COM if (((vp->v_vfsp->vfs_flag & VFS_NOEXEC) != 0) && (is_library == 0)) {
20668212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(aout_noexec);
20678212SMichael.Corcoran@Sun.COM return (EACCES);
20688212SMichael.Corcoran@Sun.COM }
20698212SMichael.Corcoran@Sun.COM
20708212SMichael.Corcoran@Sun.COM /*
20718212SMichael.Corcoran@Sun.COM * There are 2 ways to calculate the mapped size of executable:
20728212SMichael.Corcoran@Sun.COM * 1) rounded text size + data size + bss size.
20738212SMichael.Corcoran@Sun.COM * 2) starting offset for text + text size + data size + text relocation
20748212SMichael.Corcoran@Sun.COM * size + data relocation size + room for nlist data structure.
20758212SMichael.Corcoran@Sun.COM *
20768212SMichael.Corcoran@Sun.COM * The larger of the two sizes will be used to map this binary.
20778212SMichael.Corcoran@Sun.COM */
20788212SMichael.Corcoran@Sun.COM osize = P2ROUNDUP(hdr->a_text, PAGESIZE) + hdr->a_data + hdr->a_bss;
20798212SMichael.Corcoran@Sun.COM
20808212SMichael.Corcoran@Sun.COM off = hdr->a_magic == ZMAGIC ? 0 : sizeof (struct exec);
20818212SMichael.Corcoran@Sun.COM
20828212SMichael.Corcoran@Sun.COM nsize = off + hdr->a_text + hdr->a_data + hdr->a_trsize +
20838212SMichael.Corcoran@Sun.COM hdr->a_drsize + NLIST_SIZE;
20848212SMichael.Corcoran@Sun.COM
20858212SMichael.Corcoran@Sun.COM size = MAX(osize, nsize);
20868212SMichael.Corcoran@Sun.COM if (size != nsize) {
20878212SMichael.Corcoran@Sun.COM nsize = 0;
20888212SMichael.Corcoran@Sun.COM }
20898212SMichael.Corcoran@Sun.COM
20908212SMichael.Corcoran@Sun.COM /*
20918212SMichael.Corcoran@Sun.COM * 1 seg for text and 1 seg for initialized data.
20928212SMichael.Corcoran@Sun.COM * 1 seg for bss (if can't fit in leftover space of init data)
20938212SMichael.Corcoran@Sun.COM * 1 seg for nlist if needed.
20948212SMichael.Corcoran@Sun.COM */
20958212SMichael.Corcoran@Sun.COM to_map = 2 + (nsize ? 1 : 0) +
20968212SMichael.Corcoran@Sun.COM (hdr->a_bss > PAGESIZE - P2PHASE(hdr->a_data, PAGESIZE) ? 1 : 0);
20978212SMichael.Corcoran@Sun.COM if (*num_mapped < to_map) {
20988212SMichael.Corcoran@Sun.COM *num_mapped = to_map;
20998212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(aout_e2big);
21008212SMichael.Corcoran@Sun.COM return (E2BIG);
21018212SMichael.Corcoran@Sun.COM }
21028212SMichael.Corcoran@Sun.COM
21038212SMichael.Corcoran@Sun.COM /* Reserve address space for the whole mapping */
21048212SMichael.Corcoran@Sun.COM if (is_library) {
21058212SMichael.Corcoran@Sun.COM /* We'll let VOP_MAP below pick our address for us */
21068212SMichael.Corcoran@Sun.COM addr = NULL;
21078212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(aout_lib);
21088212SMichael.Corcoran@Sun.COM } else {
21098212SMichael.Corcoran@Sun.COM /*
21108212SMichael.Corcoran@Sun.COM * default start address for fixed binaries from AOUT 4.x
21118212SMichael.Corcoran@Sun.COM * standard.
21128212SMichael.Corcoran@Sun.COM */
21138212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(aout_fixed);
21148212SMichael.Corcoran@Sun.COM mflag |= MAP_FIXED;
21158212SMichael.Corcoran@Sun.COM addr = (caddr_t)0x2000;
21168212SMichael.Corcoran@Sun.COM as_rangelock(as);
21178212SMichael.Corcoran@Sun.COM if (as_gap(as, size, &addr, &size, 0, NULL) != 0) {
21188212SMichael.Corcoran@Sun.COM as_rangeunlock(as);
21198212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(aout_addr_in_use);
21208212SMichael.Corcoran@Sun.COM return (EADDRINUSE);
21218212SMichael.Corcoran@Sun.COM }
21228212SMichael.Corcoran@Sun.COM crargs.flags |= MAP_NORESERVE;
21238212SMichael.Corcoran@Sun.COM error = as_map(as, addr, size, segvn_create, &crargs);
21248212SMichael.Corcoran@Sun.COM ASSERT(addr == (caddr_t)0x2000);
21258212SMichael.Corcoran@Sun.COM as_rangeunlock(as);
21268212SMichael.Corcoran@Sun.COM }
21278212SMichael.Corcoran@Sun.COM
21288212SMichael.Corcoran@Sun.COM start_addr = addr;
21298212SMichael.Corcoran@Sun.COM osize = size;
21308212SMichael.Corcoran@Sun.COM
21318212SMichael.Corcoran@Sun.COM /*
21328212SMichael.Corcoran@Sun.COM * Map as large as we need, backed by file, this will be text, and
21338212SMichael.Corcoran@Sun.COM * possibly the nlist segment. We map over this mapping for bss and
21348212SMichael.Corcoran@Sun.COM * initialized data segments.
21358212SMichael.Corcoran@Sun.COM */
21368212SMichael.Corcoran@Sun.COM error = VOP_MAP(vp, off, as, &addr, size, prot, PROT_ALL,
21378212SMichael.Corcoran@Sun.COM mflag, fcred, NULL);
21388212SMichael.Corcoran@Sun.COM if (error) {
21398212SMichael.Corcoran@Sun.COM if (!is_library) {
21408212SMichael.Corcoran@Sun.COM (void) as_unmap(as, start_addr, osize);
21418212SMichael.Corcoran@Sun.COM }
21428212SMichael.Corcoran@Sun.COM return (error);
21438212SMichael.Corcoran@Sun.COM }
21448212SMichael.Corcoran@Sun.COM
21458212SMichael.Corcoran@Sun.COM /* pickup the value of start_addr and osize for libraries */
21468212SMichael.Corcoran@Sun.COM start_addr = addr;
21478212SMichael.Corcoran@Sun.COM osize = size;
21488212SMichael.Corcoran@Sun.COM
21498212SMichael.Corcoran@Sun.COM /*
21508212SMichael.Corcoran@Sun.COM * We have our initial reservation/allocation so we need to use fixed
21518212SMichael.Corcoran@Sun.COM * addresses from now on.
21528212SMichael.Corcoran@Sun.COM */
21538212SMichael.Corcoran@Sun.COM mflag |= MAP_FIXED;
21548212SMichael.Corcoran@Sun.COM
21558212SMichael.Corcoran@Sun.COM mrp[0].mr_addr = addr;
21568212SMichael.Corcoran@Sun.COM mrp[0].mr_msize = hdr->a_text;
21578212SMichael.Corcoran@Sun.COM mrp[0].mr_fsize = hdr->a_text;
21588212SMichael.Corcoran@Sun.COM mrp[0].mr_offset = 0;
21598212SMichael.Corcoran@Sun.COM mrp[0].mr_prot = PROT_READ | PROT_EXEC;
21608212SMichael.Corcoran@Sun.COM mrp[0].mr_flags = MR_HDR_AOUT;
21618212SMichael.Corcoran@Sun.COM
21628212SMichael.Corcoran@Sun.COM
21638212SMichael.Corcoran@Sun.COM /*
21648212SMichael.Corcoran@Sun.COM * Map initialized data. We are mapping over a portion of the
21658212SMichael.Corcoran@Sun.COM * previous mapping which will be unmapped in VOP_MAP below.
21668212SMichael.Corcoran@Sun.COM */
21678212SMichael.Corcoran@Sun.COM off = P2ROUNDUP((offset_t)(hdr->a_text), PAGESIZE);
21688212SMichael.Corcoran@Sun.COM msize = off;
21698212SMichael.Corcoran@Sun.COM addr += off;
21708212SMichael.Corcoran@Sun.COM size = hdr->a_data;
21718212SMichael.Corcoran@Sun.COM error = VOP_MAP(vp, off, as, &addr, size, PROT_ALL, PROT_ALL,
21728212SMichael.Corcoran@Sun.COM mflag, fcred, NULL);
21738212SMichael.Corcoran@Sun.COM if (error) {
21748212SMichael.Corcoran@Sun.COM (void) as_unmap(as, start_addr, osize);
21758212SMichael.Corcoran@Sun.COM return (error);
21768212SMichael.Corcoran@Sun.COM }
21778212SMichael.Corcoran@Sun.COM msize += size;
21788212SMichael.Corcoran@Sun.COM mrp[1].mr_addr = addr;
21798212SMichael.Corcoran@Sun.COM mrp[1].mr_msize = size;
21808212SMichael.Corcoran@Sun.COM mrp[1].mr_fsize = size;
21818212SMichael.Corcoran@Sun.COM mrp[1].mr_offset = 0;
21828212SMichael.Corcoran@Sun.COM mrp[1].mr_prot = PROT_READ | PROT_WRITE | PROT_EXEC;
21838212SMichael.Corcoran@Sun.COM mrp[1].mr_flags = 0;
21848212SMichael.Corcoran@Sun.COM
21858212SMichael.Corcoran@Sun.COM /* Need to zero out remainder of page */
21868212SMichael.Corcoran@Sun.COM addr += hdr->a_data;
21878212SMichael.Corcoran@Sun.COM zfoddiff = P2PHASE((size_t)addr, PAGESIZE);
21888212SMichael.Corcoran@Sun.COM if (zfoddiff) {
21898212SMichael.Corcoran@Sun.COM label_t ljb;
21908212SMichael.Corcoran@Sun.COM
21918212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(aout_zfoddiff);
21928212SMichael.Corcoran@Sun.COM zfoddiff = PAGESIZE - zfoddiff;
21938212SMichael.Corcoran@Sun.COM if (on_fault(&ljb)) {
21948212SMichael.Corcoran@Sun.COM no_fault();
21958212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(aout_uzero_fault);
21968212SMichael.Corcoran@Sun.COM (void) as_unmap(as, start_addr, osize);
21978212SMichael.Corcoran@Sun.COM return (EFAULT);
21988212SMichael.Corcoran@Sun.COM }
21998212SMichael.Corcoran@Sun.COM uzero(addr, zfoddiff);
22008212SMichael.Corcoran@Sun.COM no_fault();
22018212SMichael.Corcoran@Sun.COM }
22028212SMichael.Corcoran@Sun.COM msize += zfoddiff;
22038212SMichael.Corcoran@Sun.COM segnum = 2;
22048212SMichael.Corcoran@Sun.COM
22058212SMichael.Corcoran@Sun.COM /* Map bss */
22068212SMichael.Corcoran@Sun.COM if (hdr->a_bss > zfoddiff) {
22078212SMichael.Corcoran@Sun.COM struct segvn_crargs crargs =
22088212SMichael.Corcoran@Sun.COM SEGVN_ZFOD_ARGS(PROT_ZFOD, PROT_ALL);
22098212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(aout_map_bss);
22108212SMichael.Corcoran@Sun.COM addr += zfoddiff;
22118212SMichael.Corcoran@Sun.COM size = hdr->a_bss - zfoddiff;
22128212SMichael.Corcoran@Sun.COM as_rangelock(as);
22138212SMichael.Corcoran@Sun.COM (void) as_unmap(as, addr, size);
22148212SMichael.Corcoran@Sun.COM error = as_map(as, addr, size, segvn_create, &crargs);
22158212SMichael.Corcoran@Sun.COM as_rangeunlock(as);
22168212SMichael.Corcoran@Sun.COM msize += size;
22178212SMichael.Corcoran@Sun.COM
22188212SMichael.Corcoran@Sun.COM if (error) {
22198212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(aout_bss_fail);
22208212SMichael.Corcoran@Sun.COM (void) as_unmap(as, start_addr, osize);
22218212SMichael.Corcoran@Sun.COM return (error);
22228212SMichael.Corcoran@Sun.COM }
22238212SMichael.Corcoran@Sun.COM mrp[2].mr_addr = addr;
22248212SMichael.Corcoran@Sun.COM mrp[2].mr_msize = size;
22258212SMichael.Corcoran@Sun.COM mrp[2].mr_fsize = 0;
22268212SMichael.Corcoran@Sun.COM mrp[2].mr_offset = 0;
22278212SMichael.Corcoran@Sun.COM mrp[2].mr_prot = PROT_READ | PROT_WRITE | PROT_EXEC;
22288212SMichael.Corcoran@Sun.COM mrp[2].mr_flags = 0;
22298212SMichael.Corcoran@Sun.COM
22308212SMichael.Corcoran@Sun.COM addr += size;
22318212SMichael.Corcoran@Sun.COM segnum = 3;
22328212SMichael.Corcoran@Sun.COM }
22338212SMichael.Corcoran@Sun.COM
22348212SMichael.Corcoran@Sun.COM /*
22358212SMichael.Corcoran@Sun.COM * If we have extra bits left over, we need to include that in how
22368212SMichael.Corcoran@Sun.COM * much we mapped to make sure the nlist logic is correct
22378212SMichael.Corcoran@Sun.COM */
22388212SMichael.Corcoran@Sun.COM msize = P2ROUNDUP(msize, PAGESIZE);
22398212SMichael.Corcoran@Sun.COM
22408212SMichael.Corcoran@Sun.COM if (nsize && msize < nsize) {
22418212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(aout_nlist);
22428212SMichael.Corcoran@Sun.COM mrp[segnum].mr_addr = addr;
22438212SMichael.Corcoran@Sun.COM mrp[segnum].mr_msize = nsize - msize;
22448212SMichael.Corcoran@Sun.COM mrp[segnum].mr_fsize = 0;
22458212SMichael.Corcoran@Sun.COM mrp[segnum].mr_offset = 0;
22468212SMichael.Corcoran@Sun.COM mrp[segnum].mr_prot = PROT_READ | PROT_EXEC;
22478212SMichael.Corcoran@Sun.COM mrp[segnum].mr_flags = 0;
22488212SMichael.Corcoran@Sun.COM }
22498212SMichael.Corcoran@Sun.COM
22508212SMichael.Corcoran@Sun.COM *num_mapped = to_map;
22518212SMichael.Corcoran@Sun.COM return (0);
22528212SMichael.Corcoran@Sun.COM }
22538212SMichael.Corcoran@Sun.COM #endif
22548212SMichael.Corcoran@Sun.COM
22558212SMichael.Corcoran@Sun.COM /*
22568212SMichael.Corcoran@Sun.COM * These are the two types of files that we can interpret and we want to read
22578212SMichael.Corcoran@Sun.COM * in enough info to cover both types when looking at the initial header.
22588212SMichael.Corcoran@Sun.COM */
22598212SMichael.Corcoran@Sun.COM #define MAX_HEADER_SIZE (MAX(sizeof (Ehdr), sizeof (struct exec)))
22608212SMichael.Corcoran@Sun.COM
22618212SMichael.Corcoran@Sun.COM /*
22628212SMichael.Corcoran@Sun.COM * Map vp passed in in an interpreted manner. ELF and AOUT files will be
22638212SMichael.Corcoran@Sun.COM * interpreted and mapped appropriately for execution.
22648212SMichael.Corcoran@Sun.COM * num_mapped in - # elements in mrp
22658212SMichael.Corcoran@Sun.COM * num_mapped out - # sections mapped and length of mrp array if
22668212SMichael.Corcoran@Sun.COM * no errors or E2BIG returned.
22678212SMichael.Corcoran@Sun.COM *
22688212SMichael.Corcoran@Sun.COM * Returns 0 on success, errno value on failure.
22698212SMichael.Corcoran@Sun.COM */
22708212SMichael.Corcoran@Sun.COM static int
mmapobj_map_interpret(vnode_t * vp,mmapobj_result_t * mrp,uint_t * num_mapped,size_t padding,cred_t * fcred)22718212SMichael.Corcoran@Sun.COM mmapobj_map_interpret(vnode_t *vp, mmapobj_result_t *mrp,
22728212SMichael.Corcoran@Sun.COM uint_t *num_mapped, size_t padding, cred_t *fcred)
22738212SMichael.Corcoran@Sun.COM {
22748212SMichael.Corcoran@Sun.COM int error = 0;
22758212SMichael.Corcoran@Sun.COM vattr_t vattr;
22768212SMichael.Corcoran@Sun.COM struct lib_va *lvp;
22778212SMichael.Corcoran@Sun.COM caddr_t start_addr;
22788212SMichael.Corcoran@Sun.COM model_t model;
22798212SMichael.Corcoran@Sun.COM
22808212SMichael.Corcoran@Sun.COM /*
22818212SMichael.Corcoran@Sun.COM * header has to be aligned to the native size of ulong_t in order
22828212SMichael.Corcoran@Sun.COM * to avoid an unaligned access when dereferencing the header as
22838212SMichael.Corcoran@Sun.COM * a ulong_t. Thus we allocate our array on the stack of type
22848212SMichael.Corcoran@Sun.COM * ulong_t and then have header, which we dereference later as a char
22858212SMichael.Corcoran@Sun.COM * array point at lheader.
22868212SMichael.Corcoran@Sun.COM */
22878212SMichael.Corcoran@Sun.COM ulong_t lheader[(MAX_HEADER_SIZE / (sizeof (ulong_t))) + 1];
22888212SMichael.Corcoran@Sun.COM caddr_t header = (caddr_t)&lheader;
22898212SMichael.Corcoran@Sun.COM
22908212SMichael.Corcoran@Sun.COM vattr.va_mask = AT_FSID | AT_NODEID | AT_CTIME | AT_MTIME | AT_SIZE;
22918212SMichael.Corcoran@Sun.COM error = VOP_GETATTR(vp, &vattr, 0, fcred, NULL);
22928212SMichael.Corcoran@Sun.COM if (error) {
22938212SMichael.Corcoran@Sun.COM return (error);
22948212SMichael.Corcoran@Sun.COM }
22958212SMichael.Corcoran@Sun.COM
22968212SMichael.Corcoran@Sun.COM /*
22978212SMichael.Corcoran@Sun.COM * Check lib_va to see if we already have a full description
22988212SMichael.Corcoran@Sun.COM * for this library. This is the fast path and only used for
22998212SMichael.Corcoran@Sun.COM * ET_DYN ELF files (dynamic libraries).
23008212SMichael.Corcoran@Sun.COM */
23018212SMichael.Corcoran@Sun.COM if (padding == 0 && (lvp = lib_va_find(&vattr)) != NULL) {
23028212SMichael.Corcoran@Sun.COM int num_segs;
23038212SMichael.Corcoran@Sun.COM
23048212SMichael.Corcoran@Sun.COM model = get_udatamodel();
23058212SMichael.Corcoran@Sun.COM if ((model == DATAMODEL_ILP32 &&
23068212SMichael.Corcoran@Sun.COM lvp->lv_flags & LV_ELF64) ||
23078212SMichael.Corcoran@Sun.COM (model == DATAMODEL_LP64 &&
23088212SMichael.Corcoran@Sun.COM lvp->lv_flags & LV_ELF32)) {
23098212SMichael.Corcoran@Sun.COM lib_va_release(lvp);
23108212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(fast_wrong_model);
23118212SMichael.Corcoran@Sun.COM return (ENOTSUP);
23128212SMichael.Corcoran@Sun.COM }
23138212SMichael.Corcoran@Sun.COM num_segs = lvp->lv_num_segs;
23148212SMichael.Corcoran@Sun.COM if (*num_mapped < num_segs) {
23158212SMichael.Corcoran@Sun.COM *num_mapped = num_segs;
23168212SMichael.Corcoran@Sun.COM lib_va_release(lvp);
23178212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(fast_e2big);
23188212SMichael.Corcoran@Sun.COM return (E2BIG);
23198212SMichael.Corcoran@Sun.COM }
23208212SMichael.Corcoran@Sun.COM
23218212SMichael.Corcoran@Sun.COM /*
23228212SMichael.Corcoran@Sun.COM * Check to see if we have all the mappable program headers
23238212SMichael.Corcoran@Sun.COM * cached.
23248212SMichael.Corcoran@Sun.COM */
23258212SMichael.Corcoran@Sun.COM if (num_segs <= LIBVA_CACHED_SEGS && num_segs != 0) {
23268212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(fast);
23278212SMichael.Corcoran@Sun.COM start_addr = mmapobj_lookup_start_addr(lvp);
23288212SMichael.Corcoran@Sun.COM if (start_addr == NULL) {
23298212SMichael.Corcoran@Sun.COM lib_va_release(lvp);
23308212SMichael.Corcoran@Sun.COM return (ENOMEM);
23318212SMichael.Corcoran@Sun.COM }
23328212SMichael.Corcoran@Sun.COM
23338212SMichael.Corcoran@Sun.COM bcopy(lvp->lv_mps, mrp,
23348212SMichael.Corcoran@Sun.COM num_segs * sizeof (mmapobj_result_t));
23358212SMichael.Corcoran@Sun.COM
23368212SMichael.Corcoran@Sun.COM error = mmapobj_map_elf(vp, start_addr, mrp,
23378212SMichael.Corcoran@Sun.COM num_segs, fcred, ET_DYN);
23388212SMichael.Corcoran@Sun.COM
23398212SMichael.Corcoran@Sun.COM lib_va_release(lvp);
23408212SMichael.Corcoran@Sun.COM if (error == 0) {
23418212SMichael.Corcoran@Sun.COM *num_mapped = num_segs;
23428212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(fast_success);
23438212SMichael.Corcoran@Sun.COM }
23448212SMichael.Corcoran@Sun.COM return (error);
23458212SMichael.Corcoran@Sun.COM }
23468212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(fast_not_now);
23478212SMichael.Corcoran@Sun.COM
23488212SMichael.Corcoran@Sun.COM /* Release it for now since we'll look it up below */
23498212SMichael.Corcoran@Sun.COM lib_va_release(lvp);
23508212SMichael.Corcoran@Sun.COM }
23518212SMichael.Corcoran@Sun.COM
23528212SMichael.Corcoran@Sun.COM /*
23538212SMichael.Corcoran@Sun.COM * Time to see if this is a file we can interpret. If it's smaller
23548212SMichael.Corcoran@Sun.COM * than this, then we can't interpret it.
23558212SMichael.Corcoran@Sun.COM */
23568212SMichael.Corcoran@Sun.COM if (vattr.va_size < MAX_HEADER_SIZE) {
23578212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(small_file);
23588212SMichael.Corcoran@Sun.COM return (ENOTSUP);
23598212SMichael.Corcoran@Sun.COM }
23608212SMichael.Corcoran@Sun.COM
23618212SMichael.Corcoran@Sun.COM if ((error = vn_rdwr(UIO_READ, vp, header, MAX_HEADER_SIZE, 0,
23628212SMichael.Corcoran@Sun.COM UIO_SYSSPACE, 0, (rlim64_t)0, fcred, NULL)) != 0) {
23638212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(read_error);
23648212SMichael.Corcoran@Sun.COM return (error);
23658212SMichael.Corcoran@Sun.COM }
23668212SMichael.Corcoran@Sun.COM
23678212SMichael.Corcoran@Sun.COM /* Verify file type */
23688212SMichael.Corcoran@Sun.COM if (header[EI_MAG0] == ELFMAG0 && header[EI_MAG1] == ELFMAG1 &&
23698212SMichael.Corcoran@Sun.COM header[EI_MAG2] == ELFMAG2 && header[EI_MAG3] == ELFMAG3) {
23708212SMichael.Corcoran@Sun.COM return (doelfwork((Ehdr *)lheader, vp, mrp, num_mapped,
23718212SMichael.Corcoran@Sun.COM padding, fcred));
23728212SMichael.Corcoran@Sun.COM }
23738212SMichael.Corcoran@Sun.COM
23748212SMichael.Corcoran@Sun.COM #if defined(__sparc)
23758212SMichael.Corcoran@Sun.COM /* On sparc, check for 4.X AOUT format */
23768212SMichael.Corcoran@Sun.COM switch (((struct exec *)header)->a_magic) {
23778212SMichael.Corcoran@Sun.COM case OMAGIC:
23788212SMichael.Corcoran@Sun.COM case ZMAGIC:
23798212SMichael.Corcoran@Sun.COM case NMAGIC:
23808212SMichael.Corcoran@Sun.COM return (doaoutwork(vp, mrp, num_mapped,
23818212SMichael.Corcoran@Sun.COM (struct exec *)lheader, fcred));
23828212SMichael.Corcoran@Sun.COM }
23838212SMichael.Corcoran@Sun.COM #endif
23848212SMichael.Corcoran@Sun.COM
23858212SMichael.Corcoran@Sun.COM /* Unsupported type */
23868212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(unsupported);
23878212SMichael.Corcoran@Sun.COM return (ENOTSUP);
23888212SMichael.Corcoran@Sun.COM }
23898212SMichael.Corcoran@Sun.COM
23908212SMichael.Corcoran@Sun.COM /*
23918212SMichael.Corcoran@Sun.COM * Given a vnode, map it as either a flat file or interpret it and map
23928212SMichael.Corcoran@Sun.COM * it according to the rules of the file type.
23938212SMichael.Corcoran@Sun.COM * *num_mapped will contain the size of the mmapobj_result_t array passed in.
23948212SMichael.Corcoran@Sun.COM * If padding is non-zero, the mappings will be padded by that amount
23958212SMichael.Corcoran@Sun.COM * rounded up to the nearest pagesize.
23968212SMichael.Corcoran@Sun.COM * If the mapping is successful, *num_mapped will contain the number of
23978212SMichael.Corcoran@Sun.COM * distinct mappings created, and mrp will point to the array of
23988212SMichael.Corcoran@Sun.COM * mmapobj_result_t's which describe these mappings.
23998212SMichael.Corcoran@Sun.COM *
24008212SMichael.Corcoran@Sun.COM * On error, -1 is returned and errno is set appropriately.
24018212SMichael.Corcoran@Sun.COM * A special error case will set errno to E2BIG when there are more than
24028212SMichael.Corcoran@Sun.COM * *num_mapped mappings to be created and *num_mapped will be set to the
24038212SMichael.Corcoran@Sun.COM * number of mappings needed.
24048212SMichael.Corcoran@Sun.COM */
24058212SMichael.Corcoran@Sun.COM int
mmapobj(vnode_t * vp,uint_t flags,mmapobj_result_t * mrp,uint_t * num_mapped,size_t padding,cred_t * fcred)24068212SMichael.Corcoran@Sun.COM mmapobj(vnode_t *vp, uint_t flags, mmapobj_result_t *mrp,
24078212SMichael.Corcoran@Sun.COM uint_t *num_mapped, size_t padding, cred_t *fcred)
24088212SMichael.Corcoran@Sun.COM {
24098212SMichael.Corcoran@Sun.COM int to_map;
24108212SMichael.Corcoran@Sun.COM int error = 0;
24118212SMichael.Corcoran@Sun.COM
24128212SMichael.Corcoran@Sun.COM ASSERT((padding & PAGEOFFSET) == 0);
24138212SMichael.Corcoran@Sun.COM ASSERT((flags & ~MMOBJ_ALL_FLAGS) == 0);
24148212SMichael.Corcoran@Sun.COM ASSERT(num_mapped != NULL);
24158212SMichael.Corcoran@Sun.COM ASSERT((flags & MMOBJ_PADDING) ? padding != 0 : padding == 0);
24168212SMichael.Corcoran@Sun.COM
24178212SMichael.Corcoran@Sun.COM if ((flags & MMOBJ_INTERPRET) == 0) {
24188212SMichael.Corcoran@Sun.COM to_map = padding ? 3 : 1;
24198212SMichael.Corcoran@Sun.COM if (*num_mapped < to_map) {
24208212SMichael.Corcoran@Sun.COM *num_mapped = to_map;
24218212SMichael.Corcoran@Sun.COM MOBJ_STAT_ADD(flat_e2big);
24228212SMichael.Corcoran@Sun.COM return (E2BIG);
24238212SMichael.Corcoran@Sun.COM }
24248212SMichael.Corcoran@Sun.COM error = mmapobj_map_flat(vp, mrp, padding, fcred);
24258212SMichael.Corcoran@Sun.COM
24268212SMichael.Corcoran@Sun.COM if (error) {
24278212SMichael.Corcoran@Sun.COM return (error);
24288212SMichael.Corcoran@Sun.COM }
24298212SMichael.Corcoran@Sun.COM *num_mapped = to_map;
24308212SMichael.Corcoran@Sun.COM return (0);
24318212SMichael.Corcoran@Sun.COM }
24328212SMichael.Corcoran@Sun.COM
24338212SMichael.Corcoran@Sun.COM error = mmapobj_map_interpret(vp, mrp, num_mapped, padding, fcred);
24348212SMichael.Corcoran@Sun.COM return (error);
24358212SMichael.Corcoran@Sun.COM }
2436