2 * Copyright (c) 2004 Tim J. Robbins
3 * Copyright (c) 2002 Doug Rabson
4 * Copyright (c) 2000 Marcel Moolenaar
5 * Copyright (c) 1994-1995 Søren Schmidt
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer
13 * in this position and unchanged.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/capsicum.h>
39 #include <sys/imgact.h>
43 #include <sys/resourcevar.h>
44 #include <sys/syscallsubr.h>
45 #include <sys/sysent.h>
46 #include <sys/sysproto.h>
49 #include <vm/vm_extern.h>
50 #include <vm/vm_map.h>
52 #include <compat/linux/linux_emul.h>
53 #include <compat/linux/linux_mmap.h>
54 #include <compat/linux/linux_persona.h>
55 #include <compat/linux/linux_util.h>
58 #define STACK_SIZE (2 * 1024 * 1024)
59 #define GUARD_SIZE (4 * PAGE_SIZE)
61 #if defined(__amd64__)
62 static void linux_fixup_prot(struct thread *td, int *prot);
66 linux_mmap_check_fp(struct file *fp, int flags, int prot, int maxprot)
69 /* Linux mmap() just fails for O_WRONLY files */
70 if ((fp->f_flag & FREAD) == 0)
77 linux_mmap_common(struct thread *td, uintptr_t addr, size_t len, int prot,
78 int flags, int fd, off_t pos)
80 struct mmap_req mr, mr_fixed;
81 struct proc *p = td->td_proc;
82 struct vmspace *vms = td->td_proc->p_vmspace;
86 LINUX_CTR6(mmap2, "0x%lx, %ld, %ld, 0x%08lx, %ld, 0x%lx",
87 addr, len, prot, flags, fd, pos);
95 * You must specify exactly one of MAP_SHARED and MAP_PRIVATE
97 if (!((flags & LINUX_MAP_SHARED) ^ (flags & LINUX_MAP_PRIVATE)))
100 if (flags & LINUX_MAP_SHARED)
101 bsd_flags |= MAP_SHARED;
102 if (flags & LINUX_MAP_PRIVATE)
103 bsd_flags |= MAP_PRIVATE;
104 if (flags & LINUX_MAP_FIXED)
105 bsd_flags |= MAP_FIXED;
106 if (flags & LINUX_MAP_ANON) {
107 /* Enforce pos to be on page boundary, then ignore. */
108 if ((pos & PAGE_MASK) != 0)
111 bsd_flags |= MAP_ANON;
113 bsd_flags |= MAP_NOSYNC;
114 if (flags & LINUX_MAP_GROWSDOWN)
115 bsd_flags |= MAP_STACK;
117 #if defined(__amd64__)
119 * According to the Linux mmap(2) man page, "MAP_32BIT flag
120 * is ignored when MAP_FIXED is set."
122 if ((flags & LINUX_MAP_32BIT) && (flags & LINUX_MAP_FIXED) == 0)
123 bsd_flags |= MAP_32BIT;
126 * PROT_READ, PROT_WRITE, or PROT_EXEC implies PROT_READ and PROT_EXEC
127 * on Linux/i386 if the binary requires executable stack.
128 * We do this only for IA32 emulation as on native i386 this is does not
129 * make sense without PAE.
131 * XXX. Linux checks that the file system is not mounted with noexec.
133 linux_fixup_prot(td, &prot);
136 /* Linux does not check file descriptor when MAP_ANONYMOUS is set. */
137 fd = (bsd_flags & MAP_ANON) ? -1 : fd;
138 if (flags & LINUX_MAP_GROWSDOWN) {
140 * The Linux MAP_GROWSDOWN option does not limit auto
141 * growth of the region. Linux mmap with this option
142 * takes as addr the initial BOS, and as len, the initial
143 * region size. It can then grow down from addr without
144 * limit. However, Linux threads has an implicit internal
145 * limit to stack size of STACK_SIZE. Its just not
146 * enforced explicitly in Linux. But, here we impose
147 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
148 * region, since we can do this with our mmap.
150 * Our mmap with MAP_STACK takes addr as the maximum
151 * downsize limit on BOS, and as len the max size of
152 * the region. It then maps the top SGROWSIZ bytes,
153 * and auto grows the region down, up to the limit
156 * If we don't use the MAP_STACK option, the effect
157 * of this code is to allocate a stack region of a
158 * fixed size of (STACK_SIZE - GUARD_SIZE).
161 if ((caddr_t)addr + len > vms->vm_maxsaddr) {
163 * Some Linux apps will attempt to mmap
164 * thread stacks near the top of their
165 * address space. If their TOS is greater
166 * than vm_maxsaddr, vm_map_growstack()
167 * will confuse the thread stack with the
168 * process stack and deliver a SEGV if they
169 * attempt to grow the thread stack past their
170 * current stacksize rlimit. To avoid this,
171 * adjust vm_maxsaddr upwards to reflect
172 * the current stacksize rlimit rather
173 * than the maximum possible stacksize.
174 * It would be better to adjust the
175 * mmap'ed region, but some apps do not check
176 * mmap's return value.
179 vms->vm_maxsaddr = (char *)p->p_sysent->sv_usrstack -
180 lim_cur_proc(p, RLIMIT_STACK);
185 * This gives us our maximum stack size and a new BOS.
186 * If we're using VM_STACK, then mmap will just map
187 * the top SGROWSIZ bytes, and let the stack grow down
188 * to the limit at BOS. If we're not using VM_STACK
189 * we map the full stack, since we don't have a way
192 if (len <= STACK_SIZE - GUARD_SIZE) {
193 addr = addr - (STACK_SIZE - GUARD_SIZE - len);
194 len = STACK_SIZE - GUARD_SIZE;
199 * FreeBSD is free to ignore the address hint if MAP_FIXED wasn't
200 * passed. However, some Linux applications, like the ART runtime,
201 * depend on the hint. If the MAP_FIXED wasn't passed, but the
202 * address is not zero, try with MAP_FIXED and MAP_EXCL first,
203 * and fall back to the normal behaviour if that fails.
205 mr = (struct mmap_req) {
209 .mr_flags = bsd_flags,
212 .mr_check_fp_fn = linux_mmap_check_fp,
214 if (addr != 0 && (bsd_flags & MAP_FIXED) == 0 &&
215 (bsd_flags & MAP_EXCL) == 0) {
217 mr_fixed.mr_flags |= MAP_FIXED | MAP_EXCL;
218 error = kern_mmap_req(td, &mr_fixed);
223 error = kern_mmap_req(td, &mr);
225 LINUX_CTR2(mmap2, "return: %d (%p)", error, td->td_retval[0]);
231 linux_mprotect_common(struct thread *td, uintptr_t addr, size_t len, int prot)
234 /* XXX Ignore PROT_GROWSDOWN and PROT_GROWSUP for now. */
235 prot &= ~(LINUX_PROT_GROWSDOWN | LINUX_PROT_GROWSUP);
236 if ((prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC)) != 0)
239 #if defined(__amd64__)
240 linux_fixup_prot(td, &prot);
242 return (kern_mprotect(td, addr, len, prot));
245 #if defined(__amd64__)
247 linux_fixup_prot(struct thread *td, int *prot)
249 struct linux_pemuldata *pem;
251 if (SV_PROC_FLAG(td->td_proc, SV_ILP32) && *prot & PROT_READ) {
252 pem = pem_find(td->td_proc);
253 if (pem->persona & LINUX_READ_IMPLIES_EXEC)