xref: /openbmc/qemu/bsd-user/mmap.c (revision 45b8765e8f3001436c09cebcd9b8b281e6c55804)
184778508Sblueswir1 /*
284778508Sblueswir1  *  mmap support for qemu
384778508Sblueswir1  *
484778508Sblueswir1  *  Copyright (c) 2003 - 2008 Fabrice Bellard
584778508Sblueswir1  *
684778508Sblueswir1  *  This program is free software; you can redistribute it and/or modify
784778508Sblueswir1  *  it under the terms of the GNU General Public License as published by
884778508Sblueswir1  *  the Free Software Foundation; either version 2 of the License, or
984778508Sblueswir1  *  (at your option) any later version.
1084778508Sblueswir1  *
1184778508Sblueswir1  *  This program is distributed in the hope that it will be useful,
1284778508Sblueswir1  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
1384778508Sblueswir1  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1484778508Sblueswir1  *  GNU General Public License for more details.
1584778508Sblueswir1  *
1684778508Sblueswir1  *  You should have received a copy of the GNU General Public License
178167ee88SBlue Swirl  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
1884778508Sblueswir1  */
192231197cSPeter Maydell #include "qemu/osdep.h"
2084778508Sblueswir1 
2184778508Sblueswir1 #include "qemu.h"
2284778508Sblueswir1 #include "qemu-common.h"
2384778508Sblueswir1 
2495992b67SAlex Bennée static pthread_mutex_t mmap_mutex = PTHREAD_MUTEX_INITIALIZER;
2506943a62SPeter Maydell static __thread int mmap_lock_count;
2684778508Sblueswir1 
2784778508Sblueswir1 void mmap_lock(void)
2884778508Sblueswir1 {
2984778508Sblueswir1     if (mmap_lock_count++ == 0) {
3084778508Sblueswir1         pthread_mutex_lock(&mmap_mutex);
3184778508Sblueswir1     }
3284778508Sblueswir1 }
3384778508Sblueswir1 
3484778508Sblueswir1 void mmap_unlock(void)
3584778508Sblueswir1 {
3684778508Sblueswir1     if (--mmap_lock_count == 0) {
3784778508Sblueswir1         pthread_mutex_unlock(&mmap_mutex);
3884778508Sblueswir1     }
3984778508Sblueswir1 }
4084778508Sblueswir1 
41301e40edSAlex Bennée bool have_mmap_lock(void)
42301e40edSAlex Bennée {
43301e40edSAlex Bennée     return mmap_lock_count > 0 ? true : false;
44301e40edSAlex Bennée }
45301e40edSAlex Bennée 
4684778508Sblueswir1 /* Grab lock to make sure things are in a consistent state after fork().  */
4784778508Sblueswir1 void mmap_fork_start(void)
4884778508Sblueswir1 {
4984778508Sblueswir1     if (mmap_lock_count)
5084778508Sblueswir1         abort();
5184778508Sblueswir1     pthread_mutex_lock(&mmap_mutex);
5284778508Sblueswir1 }
5384778508Sblueswir1 
5484778508Sblueswir1 void mmap_fork_end(int child)
5584778508Sblueswir1 {
5684778508Sblueswir1     if (child)
5784778508Sblueswir1         pthread_mutex_init(&mmap_mutex, NULL);
5884778508Sblueswir1     else
5984778508Sblueswir1         pthread_mutex_unlock(&mmap_mutex);
6084778508Sblueswir1 }
6184778508Sblueswir1 
6284778508Sblueswir1 /* NOTE: all the constants are the HOST ones, but addresses are target. */
6384778508Sblueswir1 int target_mprotect(abi_ulong start, abi_ulong len, int prot)
6484778508Sblueswir1 {
6584778508Sblueswir1     abi_ulong end, host_start, host_end, addr;
6684778508Sblueswir1     int prot1, ret;
6784778508Sblueswir1 
68*45b8765eSWarner Losh     qemu_log_mask(CPU_LOG_PAGE, "mprotect: start=0x" TARGET_ABI_FMT_lx
696a3b9bfdSWarner Losh                   " len=0x" TARGET_ABI_FMT_lx " prot=%c%c%c\n", start, len,
7084778508Sblueswir1                   prot & PROT_READ ? 'r' : '-',
7184778508Sblueswir1                   prot & PROT_WRITE ? 'w' : '-',
7284778508Sblueswir1                   prot & PROT_EXEC ? 'x' : '-');
7384778508Sblueswir1     if ((start & ~TARGET_PAGE_MASK) != 0)
7484778508Sblueswir1         return -EINVAL;
7584778508Sblueswir1     len = TARGET_PAGE_ALIGN(len);
7684778508Sblueswir1     end = start + len;
7784778508Sblueswir1     if (end < start)
7884778508Sblueswir1         return -EINVAL;
7984778508Sblueswir1     prot &= PROT_READ | PROT_WRITE | PROT_EXEC;
8084778508Sblueswir1     if (len == 0)
8184778508Sblueswir1         return 0;
8284778508Sblueswir1 
8384778508Sblueswir1     mmap_lock();
8484778508Sblueswir1     host_start = start & qemu_host_page_mask;
8584778508Sblueswir1     host_end = HOST_PAGE_ALIGN(end);
8684778508Sblueswir1     if (start > host_start) {
8784778508Sblueswir1         /* handle host page containing start */
8884778508Sblueswir1         prot1 = prot;
8984778508Sblueswir1         for (addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {
9084778508Sblueswir1             prot1 |= page_get_flags(addr);
9184778508Sblueswir1         }
9284778508Sblueswir1         if (host_end == host_start + qemu_host_page_size) {
9384778508Sblueswir1             for (addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
9484778508Sblueswir1                 prot1 |= page_get_flags(addr);
9584778508Sblueswir1             }
9684778508Sblueswir1             end = host_end;
9784778508Sblueswir1         }
983e8f1628SRichard Henderson         ret = mprotect(g2h_untagged(host_start),
993e8f1628SRichard Henderson                        qemu_host_page_size, prot1 & PAGE_BITS);
10084778508Sblueswir1         if (ret != 0)
10184778508Sblueswir1             goto error;
10284778508Sblueswir1         host_start += qemu_host_page_size;
10384778508Sblueswir1     }
10484778508Sblueswir1     if (end < host_end) {
10584778508Sblueswir1         prot1 = prot;
10684778508Sblueswir1         for (addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
10784778508Sblueswir1             prot1 |= page_get_flags(addr);
10884778508Sblueswir1         }
1093e8f1628SRichard Henderson         ret = mprotect(g2h_untagged(host_end - qemu_host_page_size),
1103e8f1628SRichard Henderson                        qemu_host_page_size, prot1 & PAGE_BITS);
11184778508Sblueswir1         if (ret != 0)
11284778508Sblueswir1             goto error;
11384778508Sblueswir1         host_end -= qemu_host_page_size;
11484778508Sblueswir1     }
11584778508Sblueswir1 
11684778508Sblueswir1     /* handle the pages in the middle */
11784778508Sblueswir1     if (host_start < host_end) {
1183e8f1628SRichard Henderson         ret = mprotect(g2h_untagged(host_start), host_end - host_start, prot);
11984778508Sblueswir1         if (ret != 0)
12084778508Sblueswir1             goto error;
12184778508Sblueswir1     }
12284778508Sblueswir1     page_set_flags(start, start + len, prot | PAGE_VALID);
12384778508Sblueswir1     mmap_unlock();
12484778508Sblueswir1     return 0;
12584778508Sblueswir1 error:
12684778508Sblueswir1     mmap_unlock();
12784778508Sblueswir1     return ret;
12884778508Sblueswir1 }
12984778508Sblueswir1 
13084778508Sblueswir1 /* map an incomplete host page */
13184778508Sblueswir1 static int mmap_frag(abi_ulong real_start,
13284778508Sblueswir1                      abi_ulong start, abi_ulong end,
13384778508Sblueswir1                      int prot, int flags, int fd, abi_ulong offset)
13484778508Sblueswir1 {
13584778508Sblueswir1     abi_ulong real_end, addr;
13684778508Sblueswir1     void *host_start;
13784778508Sblueswir1     int prot1, prot_new;
13884778508Sblueswir1 
13984778508Sblueswir1     real_end = real_start + qemu_host_page_size;
1403e8f1628SRichard Henderson     host_start = g2h_untagged(real_start);
14184778508Sblueswir1 
14284778508Sblueswir1     /* get the protection of the target pages outside the mapping */
14384778508Sblueswir1     prot1 = 0;
14484778508Sblueswir1     for (addr = real_start; addr < real_end; addr++) {
14584778508Sblueswir1         if (addr < start || addr >= end)
14684778508Sblueswir1             prot1 |= page_get_flags(addr);
14784778508Sblueswir1     }
14884778508Sblueswir1 
14984778508Sblueswir1     if (prot1 == 0) {
15084778508Sblueswir1         /* no page was there, so we allocate one */
15184778508Sblueswir1         void *p = mmap(host_start, qemu_host_page_size, prot,
15284778508Sblueswir1                        flags | MAP_ANON, -1, 0);
15384778508Sblueswir1         if (p == MAP_FAILED)
15484778508Sblueswir1             return -1;
15584778508Sblueswir1         prot1 = prot;
15684778508Sblueswir1     }
15784778508Sblueswir1     prot1 &= PAGE_BITS;
15884778508Sblueswir1 
15984778508Sblueswir1     prot_new = prot | prot1;
16084778508Sblueswir1     if (!(flags & MAP_ANON)) {
16184778508Sblueswir1         /* msync() won't work here, so we return an error if write is
16284778508Sblueswir1            possible while it is a shared mapping */
1636c173b3cSblueswir1         if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED &&
16484778508Sblueswir1             (prot & PROT_WRITE))
165059bca46SBlue Swirl             return -1;
16684778508Sblueswir1 
16784778508Sblueswir1         /* adjust protection to be able to read */
16884778508Sblueswir1         if (!(prot1 & PROT_WRITE))
16984778508Sblueswir1             mprotect(host_start, qemu_host_page_size, prot1 | PROT_WRITE);
17084778508Sblueswir1 
17184778508Sblueswir1         /* read the corresponding file data */
17226778ac3SMikaël Urankar         if (pread(fd, g2h_untagged(start), end - start, offset) == -1) {
17326778ac3SMikaël Urankar             return -1;
17426778ac3SMikaël Urankar         }
17584778508Sblueswir1 
17684778508Sblueswir1         /* put final protection */
17784778508Sblueswir1         if (prot_new != (prot1 | PROT_WRITE))
17884778508Sblueswir1             mprotect(host_start, qemu_host_page_size, prot_new);
17984778508Sblueswir1     } else {
18084778508Sblueswir1         if (prot_new != prot1) {
18184778508Sblueswir1             mprotect(host_start, qemu_host_page_size, prot_new);
18284778508Sblueswir1         }
183948516a3SMikaël Urankar         if (prot_new & PROT_WRITE) {
184948516a3SMikaël Urankar             memset(g2h_untagged(start), 0, end - start);
185948516a3SMikaël Urankar         }
18684778508Sblueswir1     }
18784778508Sblueswir1     return 0;
18884778508Sblueswir1 }
18984778508Sblueswir1 
190be04f210SWarner Losh #if HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 64
191be04f210SWarner Losh # define TASK_UNMAPPED_BASE  (1ul << 38)
192be04f210SWarner Losh #else
193be04f210SWarner Losh # define TASK_UNMAPPED_BASE  0x40000000
194be04f210SWarner Losh #endif
195be04f210SWarner Losh abi_ulong mmap_next_start = TASK_UNMAPPED_BASE;
19684778508Sblueswir1 
19784778508Sblueswir1 unsigned long last_brk;
19884778508Sblueswir1 
199be04f210SWarner Losh /*
200be04f210SWarner Losh  * Subroutine of mmap_find_vma, used when we have pre-allocated a chunk of guest
201be04f210SWarner Losh  * address space.
20284778508Sblueswir1  */
203be04f210SWarner Losh static abi_ulong mmap_find_vma_reserved(abi_ulong start, abi_ulong size,
204be04f210SWarner Losh                                         abi_ulong alignment)
20584778508Sblueswir1 {
206be04f210SWarner Losh     abi_ulong addr;
207be04f210SWarner Losh     abi_ulong end_addr;
20884778508Sblueswir1     int prot;
209be04f210SWarner Losh     int looped = 0;
21084778508Sblueswir1 
211be04f210SWarner Losh     if (size > reserved_va) {
21284778508Sblueswir1         return (abi_ulong)-1;
21384778508Sblueswir1     }
214be04f210SWarner Losh 
215be04f210SWarner Losh     size = HOST_PAGE_ALIGN(size) + alignment;
216be04f210SWarner Losh     end_addr = start + size;
217be04f210SWarner Losh     if (end_addr > reserved_va) {
218be04f210SWarner Losh         end_addr = reserved_va;
219be04f210SWarner Losh     }
220be04f210SWarner Losh     addr = end_addr - qemu_host_page_size;
221be04f210SWarner Losh 
222be04f210SWarner Losh     while (1) {
223be04f210SWarner Losh         if (addr > end_addr) {
224be04f210SWarner Losh             if (looped) {
225be04f210SWarner Losh                 return (abi_ulong)-1;
226be04f210SWarner Losh             }
227be04f210SWarner Losh             end_addr = reserved_va;
228be04f210SWarner Losh             addr = end_addr - qemu_host_page_size;
229be04f210SWarner Losh             looped = 1;
230be04f210SWarner Losh             continue;
231be04f210SWarner Losh         }
232be04f210SWarner Losh         prot = page_get_flags(addr);
233be04f210SWarner Losh         if (prot) {
234be04f210SWarner Losh             end_addr = addr;
235be04f210SWarner Losh         }
236be04f210SWarner Losh         if (end_addr - addr >= size) {
237be04f210SWarner Losh             break;
238be04f210SWarner Losh         }
239be04f210SWarner Losh         addr -= qemu_host_page_size;
240be04f210SWarner Losh     }
241be04f210SWarner Losh 
242be04f210SWarner Losh     if (start == mmap_next_start) {
243be04f210SWarner Losh         mmap_next_start = addr;
244be04f210SWarner Losh     }
245be04f210SWarner Losh     /* addr is sufficiently low to align it up */
246be04f210SWarner Losh     if (alignment != 0) {
247be04f210SWarner Losh         addr = (addr + alignment) & ~(alignment - 1);
248be04f210SWarner Losh     }
24984778508Sblueswir1     return addr;
25084778508Sblueswir1 }
25184778508Sblueswir1 
252be04f210SWarner Losh /*
253be04f210SWarner Losh  * Find and reserve a free memory area of size 'size'. The search
254be04f210SWarner Losh  * starts at 'start'.
255be04f210SWarner Losh  * It must be called with mmap_lock() held.
256be04f210SWarner Losh  * Return -1 if error.
257be04f210SWarner Losh  */
258be04f210SWarner Losh static abi_ulong mmap_find_vma_aligned(abi_ulong start, abi_ulong size,
259be04f210SWarner Losh                                        abi_ulong alignment)
260be04f210SWarner Losh {
261be04f210SWarner Losh     void *ptr, *prev;
262be04f210SWarner Losh     abi_ulong addr;
263be04f210SWarner Losh     int flags;
264be04f210SWarner Losh     int wrapped, repeat;
265be04f210SWarner Losh 
266be04f210SWarner Losh     /* If 'start' == 0, then a default start address is used. */
267be04f210SWarner Losh     if (start == 0) {
268be04f210SWarner Losh         start = mmap_next_start;
269be04f210SWarner Losh     } else {
270be04f210SWarner Losh         start &= qemu_host_page_mask;
271be04f210SWarner Losh     }
272be04f210SWarner Losh 
273be04f210SWarner Losh     size = HOST_PAGE_ALIGN(size);
274be04f210SWarner Losh 
275be04f210SWarner Losh     if (reserved_va) {
276be04f210SWarner Losh         return mmap_find_vma_reserved(start, size,
277be04f210SWarner Losh             (alignment != 0 ? 1 << alignment : 0));
278be04f210SWarner Losh     }
279be04f210SWarner Losh 
280be04f210SWarner Losh     addr = start;
281be04f210SWarner Losh     wrapped = repeat = 0;
282be04f210SWarner Losh     prev = 0;
283953b69ccSWarner Losh     flags = MAP_ANON | MAP_PRIVATE;
284be04f210SWarner Losh     if (alignment != 0) {
285be04f210SWarner Losh         flags |= MAP_ALIGNED(alignment);
286be04f210SWarner Losh     }
287be04f210SWarner Losh 
288be04f210SWarner Losh     for (;; prev = ptr) {
289be04f210SWarner Losh         /*
290be04f210SWarner Losh          * Reserve needed memory area to avoid a race.
291be04f210SWarner Losh          * It should be discarded using:
292be04f210SWarner Losh          *  - mmap() with MAP_FIXED flag
293be04f210SWarner Losh          *  - mremap() with MREMAP_FIXED flag
294be04f210SWarner Losh          *  - shmat() with SHM_REMAP flag
295be04f210SWarner Losh          */
296be04f210SWarner Losh         ptr = mmap(g2h_untagged(addr), size, PROT_NONE,
297be04f210SWarner Losh                    flags, -1, 0);
298be04f210SWarner Losh 
299be04f210SWarner Losh         /* ENOMEM, if host address space has no memory */
300be04f210SWarner Losh         if (ptr == MAP_FAILED) {
301be04f210SWarner Losh             return (abi_ulong)-1;
302be04f210SWarner Losh         }
303be04f210SWarner Losh 
304be04f210SWarner Losh         /*
305be04f210SWarner Losh          * Count the number of sequential returns of the same address.
306be04f210SWarner Losh          * This is used to modify the search algorithm below.
307be04f210SWarner Losh          */
308be04f210SWarner Losh         repeat = (ptr == prev ? repeat + 1 : 0);
309be04f210SWarner Losh 
310be04f210SWarner Losh         if (h2g_valid(ptr + size - 1)) {
311be04f210SWarner Losh             addr = h2g(ptr);
312be04f210SWarner Losh 
313be04f210SWarner Losh             if ((addr & ~TARGET_PAGE_MASK) == 0) {
314be04f210SWarner Losh                 /* Success.  */
315be04f210SWarner Losh                 if (start == mmap_next_start && addr >= TASK_UNMAPPED_BASE) {
316be04f210SWarner Losh                     mmap_next_start = addr + size;
317be04f210SWarner Losh                 }
318be04f210SWarner Losh                 return addr;
319be04f210SWarner Losh             }
320be04f210SWarner Losh 
321be04f210SWarner Losh             /* The address is not properly aligned for the target.  */
322be04f210SWarner Losh             switch (repeat) {
323be04f210SWarner Losh             case 0:
324be04f210SWarner Losh                 /*
325be04f210SWarner Losh                  * Assume the result that the kernel gave us is the
326be04f210SWarner Losh                  * first with enough free space, so start again at the
327be04f210SWarner Losh                  * next higher target page.
328be04f210SWarner Losh                  */
329be04f210SWarner Losh                 addr = TARGET_PAGE_ALIGN(addr);
330be04f210SWarner Losh                 break;
331be04f210SWarner Losh             case 1:
332be04f210SWarner Losh                 /*
333be04f210SWarner Losh                  * Sometimes the kernel decides to perform the allocation
334be04f210SWarner Losh                  * at the top end of memory instead.
335be04f210SWarner Losh                  */
336be04f210SWarner Losh                 addr &= TARGET_PAGE_MASK;
337be04f210SWarner Losh                 break;
338be04f210SWarner Losh             case 2:
339be04f210SWarner Losh                 /* Start over at low memory.  */
340be04f210SWarner Losh                 addr = 0;
341be04f210SWarner Losh                 break;
342be04f210SWarner Losh             default:
343be04f210SWarner Losh                 /* Fail.  This unaligned block must the last.  */
344be04f210SWarner Losh                 addr = -1;
345be04f210SWarner Losh                 break;
346be04f210SWarner Losh             }
347be04f210SWarner Losh         } else {
348be04f210SWarner Losh             /*
349be04f210SWarner Losh              * Since the result the kernel gave didn't fit, start
350be04f210SWarner Losh              * again at low memory.  If any repetition, fail.
351be04f210SWarner Losh              */
352be04f210SWarner Losh             addr = (repeat ? -1 : 0);
353be04f210SWarner Losh         }
354be04f210SWarner Losh 
355be04f210SWarner Losh         /* Unmap and try again.  */
356be04f210SWarner Losh         munmap(ptr, size);
357be04f210SWarner Losh 
358be04f210SWarner Losh         /* ENOMEM if we checked the whole of the target address space.  */
359be04f210SWarner Losh         if (addr == (abi_ulong)-1) {
360be04f210SWarner Losh             return (abi_ulong)-1;
361be04f210SWarner Losh         } else if (addr == 0) {
362be04f210SWarner Losh             if (wrapped) {
363be04f210SWarner Losh                 return (abi_ulong)-1;
364be04f210SWarner Losh             }
365be04f210SWarner Losh             wrapped = 1;
366be04f210SWarner Losh             /*
367be04f210SWarner Losh              * Don't actually use 0 when wrapping, instead indicate
368be04f210SWarner Losh              * that we'd truly like an allocation in low memory.
369be04f210SWarner Losh              */
370be04f210SWarner Losh             addr = TARGET_PAGE_SIZE;
371be04f210SWarner Losh         } else if (wrapped && addr >= start) {
372be04f210SWarner Losh             return (abi_ulong)-1;
373be04f210SWarner Losh         }
374be04f210SWarner Losh     }
375be04f210SWarner Losh }
376be04f210SWarner Losh 
377be04f210SWarner Losh abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size)
378be04f210SWarner Losh {
379be04f210SWarner Losh     return mmap_find_vma_aligned(start, size, 0);
380be04f210SWarner Losh }
381be04f210SWarner Losh 
38284778508Sblueswir1 /* NOTE: all the constants are the HOST ones */
38384778508Sblueswir1 abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
384be04f210SWarner Losh                      int flags, int fd, off_t offset)
38584778508Sblueswir1 {
38684778508Sblueswir1     abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len;
38784778508Sblueswir1 
38884778508Sblueswir1     mmap_lock();
389*45b8765eSWarner Losh     if (qemu_loglevel_mask(CPU_LOG_PAGE)) {
390*45b8765eSWarner Losh         qemu_log("mmap: start=0x" TARGET_ABI_FMT_lx
3916a3b9bfdSWarner Losh                  " len=0x" TARGET_ABI_FMT_lx " prot=%c%c%c flags=",
39284778508Sblueswir1                  start, len,
39384778508Sblueswir1                  prot & PROT_READ ? 'r' : '-',
39484778508Sblueswir1                  prot & PROT_WRITE ? 'w' : '-',
39584778508Sblueswir1                  prot & PROT_EXEC ? 'x' : '-');
3966a3b9bfdSWarner Losh         if (flags & MAP_ALIGNMENT_MASK) {
397*45b8765eSWarner Losh             qemu_log("MAP_ALIGNED(%u) ",
398*45b8765eSWarner Losh                      (flags & MAP_ALIGNMENT_MASK) >> MAP_ALIGNMENT_SHIFT);
39984778508Sblueswir1         }
4006a3b9bfdSWarner Losh         if (flags & MAP_GUARD) {
401*45b8765eSWarner Losh             qemu_log("MAP_GUARD ");
4026a3b9bfdSWarner Losh         }
4036a3b9bfdSWarner Losh         if (flags & MAP_FIXED) {
404*45b8765eSWarner Losh             qemu_log("MAP_FIXED ");
4056a3b9bfdSWarner Losh         }
406953b69ccSWarner Losh         if (flags & MAP_ANON) {
407*45b8765eSWarner Losh             qemu_log("MAP_ANON ");
4086a3b9bfdSWarner Losh         }
4096a3b9bfdSWarner Losh         if (flags & MAP_EXCL) {
410*45b8765eSWarner Losh             qemu_log("MAP_EXCL ");
4116a3b9bfdSWarner Losh         }
4126a3b9bfdSWarner Losh         if (flags & MAP_PRIVATE) {
413*45b8765eSWarner Losh             qemu_log("MAP_PRIVATE ");
4146a3b9bfdSWarner Losh         }
4156a3b9bfdSWarner Losh         if (flags & MAP_SHARED) {
416*45b8765eSWarner Losh             qemu_log("MAP_SHARED ");
4176a3b9bfdSWarner Losh         }
4186a3b9bfdSWarner Losh         if (flags & MAP_NOCORE) {
419*45b8765eSWarner Losh             qemu_log("MAP_NOCORE ");
4206a3b9bfdSWarner Losh         }
4216a3b9bfdSWarner Losh         if (flags & MAP_STACK) {
422*45b8765eSWarner Losh             qemu_log("MAP_STACK ");
4236a3b9bfdSWarner Losh         }
424*45b8765eSWarner Losh         qemu_log("fd=%d offset=0x%lx\n", fd, offset);
42584778508Sblueswir1     }
42684778508Sblueswir1 
427953b69ccSWarner Losh     if ((flags & MAP_ANON) && fd != -1) {
428be04f210SWarner Losh         errno = EINVAL;
429be04f210SWarner Losh         goto fail;
430be04f210SWarner Losh     }
431be04f210SWarner Losh     if (flags & MAP_STACK) {
432be04f210SWarner Losh         if ((fd != -1) || ((prot & (PROT_READ | PROT_WRITE)) !=
433be04f210SWarner Losh                     (PROT_READ | PROT_WRITE))) {
434be04f210SWarner Losh             errno = EINVAL;
435be04f210SWarner Losh             goto fail;
436be04f210SWarner Losh         }
437be04f210SWarner Losh     }
438be04f210SWarner Losh     if ((flags & MAP_GUARD) && (prot != PROT_NONE || fd != -1 ||
439be04f210SWarner Losh         offset != 0 || (flags & (MAP_SHARED | MAP_PRIVATE |
440be04f210SWarner Losh         /* MAP_PREFAULT | */ /* MAP_PREFAULT not in mman.h */
441be04f210SWarner Losh         MAP_PREFAULT_READ | MAP_ANON | MAP_STACK)) != 0)) {
442be04f210SWarner Losh         errno = EINVAL;
443be04f210SWarner Losh         goto fail;
444be04f210SWarner Losh     }
445be04f210SWarner Losh 
44684778508Sblueswir1     if (offset & ~TARGET_PAGE_MASK) {
44784778508Sblueswir1         errno = EINVAL;
44884778508Sblueswir1         goto fail;
44984778508Sblueswir1     }
45084778508Sblueswir1 
451be04f210SWarner Losh     if (len == 0) {
452be04f210SWarner Losh         errno = EINVAL;
453be04f210SWarner Losh         goto fail;
454be04f210SWarner Losh     }
45514837a3fSWarner Losh 
45614837a3fSWarner Losh     /* Check for overflows */
45714837a3fSWarner Losh     len = TARGET_PAGE_ALIGN(len);
45814837a3fSWarner Losh     if (len == 0) {
45914837a3fSWarner Losh         errno = ENOMEM;
46014837a3fSWarner Losh         goto fail;
46114837a3fSWarner Losh     }
46214837a3fSWarner Losh 
46384778508Sblueswir1     real_start = start & qemu_host_page_mask;
46484778508Sblueswir1     host_offset = offset & qemu_host_page_mask;
465be04f210SWarner Losh 
466be04f210SWarner Losh     /*
467be04f210SWarner Losh      * If the user is asking for the kernel to find a location, do that
468be04f210SWarner Losh      * before we truncate the length for mapping files below.
469be04f210SWarner Losh      */
470be04f210SWarner Losh     if (!(flags & MAP_FIXED)) {
47184778508Sblueswir1         host_len = len + offset - host_offset;
47284778508Sblueswir1         host_len = HOST_PAGE_ALIGN(host_len);
473be04f210SWarner Losh         if ((flags & MAP_ALIGNMENT_MASK) != 0)
474be04f210SWarner Losh             start = mmap_find_vma_aligned(real_start, host_len,
475be04f210SWarner Losh                 (flags & MAP_ALIGNMENT_MASK) >> MAP_ALIGNMENT_SHIFT);
476be04f210SWarner Losh         else
477be04f210SWarner Losh             start = mmap_find_vma(real_start, host_len);
478be04f210SWarner Losh         if (start == (abi_ulong)-1) {
47984778508Sblueswir1             errno = ENOMEM;
48084778508Sblueswir1             goto fail;
48184778508Sblueswir1         }
482be04f210SWarner Losh     }
483be04f210SWarner Losh 
484be04f210SWarner Losh     /*
485be04f210SWarner Losh      * When mapping files into a memory area larger than the file, accesses
486be04f210SWarner Losh      * to pages beyond the file size will cause a SIGBUS.
487be04f210SWarner Losh      *
488be04f210SWarner Losh      * For example, if mmaping a file of 100 bytes on a host with 4K pages
489be04f210SWarner Losh      * emulating a target with 8K pages, the target expects to be able to
490be04f210SWarner Losh      * access the first 8K. But the host will trap us on any access beyond
491be04f210SWarner Losh      * 4K.
492be04f210SWarner Losh      *
493be04f210SWarner Losh      * When emulating a target with a larger page-size than the hosts, we
494be04f210SWarner Losh      * may need to truncate file maps at EOF and add extra anonymous pages
495be04f210SWarner Losh      * up to the targets page boundary.
496be04f210SWarner Losh      */
497be04f210SWarner Losh 
498be04f210SWarner Losh     if ((qemu_real_host_page_size < qemu_host_page_size) && fd != -1) {
499be04f210SWarner Losh         struct stat sb;
500be04f210SWarner Losh 
501be04f210SWarner Losh         if (fstat(fd, &sb) == -1) {
502be04f210SWarner Losh             goto fail;
503be04f210SWarner Losh         }
504be04f210SWarner Losh 
505be04f210SWarner Losh         /* Are we trying to create a map beyond EOF?.  */
506be04f210SWarner Losh         if (offset + len > sb.st_size) {
507be04f210SWarner Losh             /*
508be04f210SWarner Losh              * If so, truncate the file map at eof aligned with
509be04f210SWarner Losh              * the hosts real pagesize. Additional anonymous maps
510be04f210SWarner Losh              * will be created beyond EOF.
511be04f210SWarner Losh              */
512be04f210SWarner Losh             len = REAL_HOST_PAGE_ALIGN(sb.st_size - offset);
513be04f210SWarner Losh         }
514be04f210SWarner Losh     }
515be04f210SWarner Losh 
516be04f210SWarner Losh     if (!(flags & MAP_FIXED)) {
517be04f210SWarner Losh         unsigned long host_start;
518be04f210SWarner Losh         void *p;
519be04f210SWarner Losh 
520be04f210SWarner Losh         host_len = len + offset - host_offset;
521be04f210SWarner Losh         host_len = HOST_PAGE_ALIGN(host_len);
522be04f210SWarner Losh 
523be04f210SWarner Losh         /*
524be04f210SWarner Losh          * Note: we prefer to control the mapping address. It is
525be04f210SWarner Losh          * especially important if qemu_host_page_size >
526be04f210SWarner Losh          * qemu_real_host_page_size
527be04f210SWarner Losh          */
528be04f210SWarner Losh         p = mmap(g2h_untagged(start), host_len, prot,
529953b69ccSWarner Losh                  flags | MAP_FIXED | ((fd != -1) ? MAP_ANON : 0), -1, 0);
53084778508Sblueswir1         if (p == MAP_FAILED)
53184778508Sblueswir1             goto fail;
53284778508Sblueswir1         /* update start so that it points to the file position at 'offset' */
53384778508Sblueswir1         host_start = (unsigned long)p;
534be04f210SWarner Losh         if (fd != -1) {
535be04f210SWarner Losh             p = mmap(g2h_untagged(start), len, prot,
536be04f210SWarner Losh                      flags | MAP_FIXED, fd, host_offset);
537be04f210SWarner Losh             if (p == MAP_FAILED) {
538be04f210SWarner Losh                 munmap(g2h_untagged(start), host_len);
539be04f210SWarner Losh                 goto fail;
540be04f210SWarner Losh             }
54184778508Sblueswir1             host_start += offset - host_offset;
542be04f210SWarner Losh         }
54384778508Sblueswir1         start = h2g(host_start);
54484778508Sblueswir1     } else {
54584778508Sblueswir1         if (start & ~TARGET_PAGE_MASK) {
54684778508Sblueswir1             errno = EINVAL;
54784778508Sblueswir1             goto fail;
54884778508Sblueswir1         }
54984778508Sblueswir1         end = start + len;
55084778508Sblueswir1         real_end = HOST_PAGE_ALIGN(end);
55184778508Sblueswir1 
552be04f210SWarner Losh         /*
553be04f210SWarner Losh          * Test if requested memory area fits target address space
554be04f210SWarner Losh          * It can fail only on 64-bit host with 32-bit target.
555be04f210SWarner Losh          * On any other target/host host mmap() handles this error correctly.
556be04f210SWarner Losh          */
557be04f210SWarner Losh #if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
558be04f210SWarner Losh         if ((unsigned long)start + len - 1 > (abi_ulong) -1) {
559be04f210SWarner Losh             errno = EINVAL;
56084778508Sblueswir1             goto fail;
56184778508Sblueswir1         }
562be04f210SWarner Losh #endif
56384778508Sblueswir1 
564be04f210SWarner Losh         /*
565be04f210SWarner Losh          * worst case: we cannot map the file because the offset is not
566be04f210SWarner Losh          * aligned, so we read it
567be04f210SWarner Losh          */
56884778508Sblueswir1         if (!(flags & MAP_ANON) &&
56984778508Sblueswir1             (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) {
570be04f210SWarner Losh             /*
571be04f210SWarner Losh              * msync() won't work here, so we return an error if write is
572be04f210SWarner Losh              * possible while it is a shared mapping
573be04f210SWarner Losh              */
5746c173b3cSblueswir1             if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED &&
57584778508Sblueswir1                 (prot & PROT_WRITE)) {
57684778508Sblueswir1                 errno = EINVAL;
57784778508Sblueswir1                 goto fail;
57884778508Sblueswir1             }
57984778508Sblueswir1             retaddr = target_mmap(start, len, prot | PROT_WRITE,
58084778508Sblueswir1                                   MAP_FIXED | MAP_PRIVATE | MAP_ANON,
58184778508Sblueswir1                                   -1, 0);
58284778508Sblueswir1             if (retaddr == -1)
58384778508Sblueswir1                 goto fail;
58426778ac3SMikaël Urankar             if (pread(fd, g2h_untagged(start), len, offset) == -1) {
58526778ac3SMikaël Urankar                 goto fail;
58626778ac3SMikaël Urankar             }
58784778508Sblueswir1             if (!(prot & PROT_WRITE)) {
58884778508Sblueswir1                 ret = target_mprotect(start, len, prot);
58984778508Sblueswir1                 if (ret != 0) {
59084778508Sblueswir1                     start = ret;
59184778508Sblueswir1                     goto the_end;
59284778508Sblueswir1                 }
59384778508Sblueswir1             }
59484778508Sblueswir1             goto the_end;
59584778508Sblueswir1         }
59684778508Sblueswir1 
59784778508Sblueswir1         /* handle the start of the mapping */
59884778508Sblueswir1         if (start > real_start) {
59984778508Sblueswir1             if (real_end == real_start + qemu_host_page_size) {
60084778508Sblueswir1                 /* one single host page */
60184778508Sblueswir1                 ret = mmap_frag(real_start, start, end,
60284778508Sblueswir1                                 prot, flags, fd, offset);
60384778508Sblueswir1                 if (ret == -1)
60484778508Sblueswir1                     goto fail;
60584778508Sblueswir1                 goto the_end1;
60684778508Sblueswir1             }
60784778508Sblueswir1             ret = mmap_frag(real_start, start, real_start + qemu_host_page_size,
60884778508Sblueswir1                             prot, flags, fd, offset);
60984778508Sblueswir1             if (ret == -1)
61084778508Sblueswir1                 goto fail;
61184778508Sblueswir1             real_start += qemu_host_page_size;
61284778508Sblueswir1         }
61384778508Sblueswir1         /* handle the end of the mapping */
61484778508Sblueswir1         if (end < real_end) {
61584778508Sblueswir1             ret = mmap_frag(real_end - qemu_host_page_size,
616be04f210SWarner Losh                             real_end - qemu_host_page_size, end,
61784778508Sblueswir1                             prot, flags, fd,
61884778508Sblueswir1                             offset + real_end - qemu_host_page_size - start);
61984778508Sblueswir1             if (ret == -1)
62084778508Sblueswir1                 goto fail;
62184778508Sblueswir1             real_end -= qemu_host_page_size;
62284778508Sblueswir1         }
62384778508Sblueswir1 
62484778508Sblueswir1         /* map the middle (easier) */
62584778508Sblueswir1         if (real_start < real_end) {
62684778508Sblueswir1             void *p;
62784778508Sblueswir1             unsigned long offset1;
62884778508Sblueswir1             if (flags & MAP_ANON)
62984778508Sblueswir1                 offset1 = 0;
63084778508Sblueswir1             else
63184778508Sblueswir1                 offset1 = offset + real_start - start;
6323e8f1628SRichard Henderson             p = mmap(g2h_untagged(real_start), real_end - real_start,
63384778508Sblueswir1                      prot, flags, fd, offset1);
63484778508Sblueswir1             if (p == MAP_FAILED)
63584778508Sblueswir1                 goto fail;
63684778508Sblueswir1         }
63784778508Sblueswir1     }
63884778508Sblueswir1  the_end1:
63984778508Sblueswir1     page_set_flags(start, start + len, prot | PAGE_VALID);
64084778508Sblueswir1  the_end:
64184778508Sblueswir1 #ifdef DEBUG_MMAP
6426a3b9bfdSWarner Losh     printf("ret=0x" TARGET_ABI_FMT_lx "\n", start);
64384778508Sblueswir1     page_dump(stdout);
64484778508Sblueswir1     printf("\n");
64584778508Sblueswir1 #endif
646be04f210SWarner Losh     tb_invalidate_phys_range(start, start + len);
64784778508Sblueswir1     mmap_unlock();
64884778508Sblueswir1     return start;
64984778508Sblueswir1 fail:
65084778508Sblueswir1     mmap_unlock();
65184778508Sblueswir1     return -1;
65284778508Sblueswir1 }
65384778508Sblueswir1 
654be04f210SWarner Losh static void mmap_reserve(abi_ulong start, abi_ulong size)
655be04f210SWarner Losh {
656be04f210SWarner Losh     abi_ulong real_start;
657be04f210SWarner Losh     abi_ulong real_end;
658be04f210SWarner Losh     abi_ulong addr;
659be04f210SWarner Losh     abi_ulong end;
660be04f210SWarner Losh     int prot;
661be04f210SWarner Losh 
662be04f210SWarner Losh     real_start = start & qemu_host_page_mask;
663be04f210SWarner Losh     real_end = HOST_PAGE_ALIGN(start + size);
664be04f210SWarner Losh     end = start + size;
665be04f210SWarner Losh     if (start > real_start) {
666be04f210SWarner Losh         /* handle host page containing start */
667be04f210SWarner Losh         prot = 0;
668be04f210SWarner Losh         for (addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) {
669be04f210SWarner Losh             prot |= page_get_flags(addr);
670be04f210SWarner Losh         }
671be04f210SWarner Losh         if (real_end == real_start + qemu_host_page_size) {
672be04f210SWarner Losh             for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
673be04f210SWarner Losh                 prot |= page_get_flags(addr);
674be04f210SWarner Losh             }
675be04f210SWarner Losh             end = real_end;
676be04f210SWarner Losh         }
677be04f210SWarner Losh         if (prot != 0) {
678be04f210SWarner Losh             real_start += qemu_host_page_size;
679be04f210SWarner Losh         }
680be04f210SWarner Losh     }
681be04f210SWarner Losh     if (end < real_end) {
682be04f210SWarner Losh         prot = 0;
683be04f210SWarner Losh         for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
684be04f210SWarner Losh             prot |= page_get_flags(addr);
685be04f210SWarner Losh         }
686be04f210SWarner Losh         if (prot != 0) {
687be04f210SWarner Losh             real_end -= qemu_host_page_size;
688be04f210SWarner Losh         }
689be04f210SWarner Losh     }
690be04f210SWarner Losh     if (real_start != real_end) {
691be04f210SWarner Losh         mmap(g2h_untagged(real_start), real_end - real_start, PROT_NONE,
692953b69ccSWarner Losh                  MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);
693be04f210SWarner Losh     }
694be04f210SWarner Losh }
695be04f210SWarner Losh 
69684778508Sblueswir1 int target_munmap(abi_ulong start, abi_ulong len)
69784778508Sblueswir1 {
69884778508Sblueswir1     abi_ulong end, real_start, real_end, addr;
69984778508Sblueswir1     int prot, ret;
70084778508Sblueswir1 
70184778508Sblueswir1 #ifdef DEBUG_MMAP
7026a3b9bfdSWarner Losh     printf("munmap: start=0x" TARGET_ABI_FMT_lx " len=0x"
7036a3b9bfdSWarner Losh            TARGET_ABI_FMT_lx "\n",
7046a3b9bfdSWarner Losh            start, len);
70584778508Sblueswir1 #endif
70684778508Sblueswir1     if (start & ~TARGET_PAGE_MASK)
70784778508Sblueswir1         return -EINVAL;
70884778508Sblueswir1     len = TARGET_PAGE_ALIGN(len);
70984778508Sblueswir1     if (len == 0)
71084778508Sblueswir1         return -EINVAL;
71184778508Sblueswir1     mmap_lock();
71284778508Sblueswir1     end = start + len;
71384778508Sblueswir1     real_start = start & qemu_host_page_mask;
71484778508Sblueswir1     real_end = HOST_PAGE_ALIGN(end);
71584778508Sblueswir1 
71684778508Sblueswir1     if (start > real_start) {
71784778508Sblueswir1         /* handle host page containing start */
71884778508Sblueswir1         prot = 0;
71984778508Sblueswir1         for (addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) {
72084778508Sblueswir1             prot |= page_get_flags(addr);
72184778508Sblueswir1         }
72284778508Sblueswir1         if (real_end == real_start + qemu_host_page_size) {
72384778508Sblueswir1             for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
72484778508Sblueswir1                 prot |= page_get_flags(addr);
72584778508Sblueswir1             }
72684778508Sblueswir1             end = real_end;
72784778508Sblueswir1         }
72884778508Sblueswir1         if (prot != 0)
72984778508Sblueswir1             real_start += qemu_host_page_size;
73084778508Sblueswir1     }
73184778508Sblueswir1     if (end < real_end) {
73284778508Sblueswir1         prot = 0;
73384778508Sblueswir1         for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
73484778508Sblueswir1             prot |= page_get_flags(addr);
73584778508Sblueswir1         }
73684778508Sblueswir1         if (prot != 0)
73784778508Sblueswir1             real_end -= qemu_host_page_size;
73884778508Sblueswir1     }
73984778508Sblueswir1 
74084778508Sblueswir1     ret = 0;
74184778508Sblueswir1     /* unmap what we can */
74284778508Sblueswir1     if (real_start < real_end) {
743be04f210SWarner Losh         if (reserved_va) {
744be04f210SWarner Losh             mmap_reserve(real_start, real_end - real_start);
745be04f210SWarner Losh         } else {
7463e8f1628SRichard Henderson             ret = munmap(g2h_untagged(real_start), real_end - real_start);
74784778508Sblueswir1         }
748be04f210SWarner Losh     }
74984778508Sblueswir1 
750be04f210SWarner Losh     if (ret == 0) {
75184778508Sblueswir1         page_set_flags(start, start + len, 0);
752be04f210SWarner Losh         tb_invalidate_phys_range(start, start + len);
753be04f210SWarner Losh     }
75484778508Sblueswir1     mmap_unlock();
75584778508Sblueswir1     return ret;
75684778508Sblueswir1 }
75784778508Sblueswir1 
75884778508Sblueswir1 int target_msync(abi_ulong start, abi_ulong len, int flags)
75984778508Sblueswir1 {
76084778508Sblueswir1     abi_ulong end;
76184778508Sblueswir1 
76284778508Sblueswir1     if (start & ~TARGET_PAGE_MASK)
76384778508Sblueswir1         return -EINVAL;
76484778508Sblueswir1     len = TARGET_PAGE_ALIGN(len);
76584778508Sblueswir1     end = start + len;
76684778508Sblueswir1     if (end < start)
76784778508Sblueswir1         return -EINVAL;
76884778508Sblueswir1     if (end == start)
76984778508Sblueswir1         return 0;
77084778508Sblueswir1 
77184778508Sblueswir1     start &= qemu_host_page_mask;
7723e8f1628SRichard Henderson     return msync(g2h_untagged(start), end - start, flags);
77384778508Sblueswir1 }
774