root / linux-user / mmap.c @ 1e6eec8b
History | View | Annotate | Download (19.4 kB)
1 | 54936004 | bellard | /*
|
---|---|---|---|
2 | 54936004 | bellard | * mmap support for qemu
|
3 | 5fafdf24 | ths | *
|
4 | 54936004 | bellard | * Copyright (c) 2003 Fabrice Bellard
|
5 | 54936004 | bellard | *
|
6 | 54936004 | bellard | * This program is free software; you can redistribute it and/or modify
|
7 | 54936004 | bellard | * it under the terms of the GNU General Public License as published by
|
8 | 54936004 | bellard | * the Free Software Foundation; either version 2 of the License, or
|
9 | 54936004 | bellard | * (at your option) any later version.
|
10 | 54936004 | bellard | *
|
11 | 54936004 | bellard | * This program is distributed in the hope that it will be useful,
|
12 | 54936004 | bellard | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 54936004 | bellard | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 | 54936004 | bellard | * GNU General Public License for more details.
|
15 | 54936004 | bellard | *
|
16 | 54936004 | bellard | * You should have received a copy of the GNU General Public License
|
17 | 8167ee88 | Blue Swirl | * along with this program; if not, see <http://www.gnu.org/licenses/>.
|
18 | 54936004 | bellard | */
|
19 | 54936004 | bellard | #include <stdlib.h> |
20 | 54936004 | bellard | #include <stdio.h> |
21 | 54936004 | bellard | #include <stdarg.h> |
22 | 54936004 | bellard | #include <string.h> |
23 | 54936004 | bellard | #include <unistd.h> |
24 | 54936004 | bellard | #include <errno.h> |
25 | 54c5a2ae | edgar_igl | #include <sys/types.h> |
26 | 54c5a2ae | edgar_igl | #include <sys/stat.h> |
27 | 54936004 | bellard | #include <sys/mman.h> |
28 | 3af72a4d | blueswir1 | #include <linux/mman.h> |
29 | 3af72a4d | blueswir1 | #include <linux/unistd.h> |
30 | 54936004 | bellard | |
31 | 54936004 | bellard | #include "qemu.h" |
32 | 78f5bf1e | blueswir1 | #include "qemu-common.h" |
33 | 54936004 | bellard | |
34 | 54936004 | bellard | //#define DEBUG_MMAP
|
35 | 54936004 | bellard | |
36 | 2f7bb878 | Juan Quintela | #if defined(CONFIG_USE_NPTL)
|
37 | 1e6eec8b | Blue Swirl | static pthread_mutex_t mmap_mutex = PTHREAD_MUTEX_INITIALIZER;
|
38 | c8a706fe | pbrook | static int __thread mmap_lock_count; |
39 | c8a706fe | pbrook | |
40 | c8a706fe | pbrook | void mmap_lock(void) |
41 | c8a706fe | pbrook | { |
42 | c8a706fe | pbrook | if (mmap_lock_count++ == 0) { |
43 | c8a706fe | pbrook | pthread_mutex_lock(&mmap_mutex); |
44 | c8a706fe | pbrook | } |
45 | c8a706fe | pbrook | } |
46 | c8a706fe | pbrook | |
47 | c8a706fe | pbrook | void mmap_unlock(void) |
48 | c8a706fe | pbrook | { |
49 | c8a706fe | pbrook | if (--mmap_lock_count == 0) { |
50 | c8a706fe | pbrook | pthread_mutex_unlock(&mmap_mutex); |
51 | c8a706fe | pbrook | } |
52 | c8a706fe | pbrook | } |
53 | d5975363 | pbrook | |
54 | d5975363 | pbrook | /* Grab lock to make sure things are in a consistent state after fork(). */
|
55 | d5975363 | pbrook | void mmap_fork_start(void) |
56 | d5975363 | pbrook | { |
57 | d5975363 | pbrook | if (mmap_lock_count)
|
58 | d5975363 | pbrook | abort(); |
59 | d5975363 | pbrook | pthread_mutex_lock(&mmap_mutex); |
60 | d5975363 | pbrook | } |
61 | d5975363 | pbrook | |
62 | d5975363 | pbrook | void mmap_fork_end(int child) |
63 | d5975363 | pbrook | { |
64 | d5975363 | pbrook | if (child)
|
65 | d5975363 | pbrook | pthread_mutex_init(&mmap_mutex, NULL);
|
66 | d5975363 | pbrook | else
|
67 | d5975363 | pbrook | pthread_mutex_unlock(&mmap_mutex); |
68 | d5975363 | pbrook | } |
69 | c8a706fe | pbrook | #else
|
70 | c8a706fe | pbrook | /* We aren't threadsafe to start with, so no need to worry about locking. */
|
71 | c8a706fe | pbrook | void mmap_lock(void) |
72 | c8a706fe | pbrook | { |
73 | c8a706fe | pbrook | } |
74 | c8a706fe | pbrook | |
75 | c8a706fe | pbrook | void mmap_unlock(void) |
76 | c8a706fe | pbrook | { |
77 | c8a706fe | pbrook | } |
78 | c8a706fe | pbrook | #endif
|
79 | c8a706fe | pbrook | |
80 | 17e2377a | pbrook | void *qemu_vmalloc(size_t size)
|
81 | 17e2377a | pbrook | { |
82 | 17e2377a | pbrook | void *p;
|
83 | 17e2377a | pbrook | unsigned long addr; |
84 | 17e2377a | pbrook | mmap_lock(); |
85 | 17e2377a | pbrook | /* Use map and mark the pages as used. */
|
86 | 17e2377a | pbrook | p = mmap(NULL, size, PROT_READ | PROT_WRITE,
|
87 | 17e2377a | pbrook | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
88 | 17e2377a | pbrook | |
89 | 17e2377a | pbrook | addr = (unsigned long)p; |
90 | 17e2377a | pbrook | if (addr == (target_ulong) addr) {
|
91 | 17e2377a | pbrook | /* Allocated region overlaps guest address space.
|
92 | 17e2377a | pbrook | This may recurse. */
|
93 | 17e2377a | pbrook | page_set_flags(addr & TARGET_PAGE_MASK, TARGET_PAGE_ALIGN(addr + size), |
94 | 17e2377a | pbrook | PAGE_RESERVED); |
95 | 17e2377a | pbrook | } |
96 | 17e2377a | pbrook | |
97 | 17e2377a | pbrook | mmap_unlock(); |
98 | 17e2377a | pbrook | return p;
|
99 | 17e2377a | pbrook | } |
100 | 17e2377a | pbrook | |
101 | 17e2377a | pbrook | void *qemu_malloc(size_t size)
|
102 | 17e2377a | pbrook | { |
103 | 17e2377a | pbrook | char * p;
|
104 | 17e2377a | pbrook | size += 16;
|
105 | 17e2377a | pbrook | p = qemu_vmalloc(size); |
106 | 17e2377a | pbrook | *(size_t *)p = size; |
107 | 17e2377a | pbrook | return p + 16; |
108 | 17e2377a | pbrook | } |
109 | 17e2377a | pbrook | |
110 | 17e2377a | pbrook | /* We use map, which is always zero initialized. */
|
111 | 17e2377a | pbrook | void * qemu_mallocz(size_t size)
|
112 | 17e2377a | pbrook | { |
113 | 17e2377a | pbrook | return qemu_malloc(size);
|
114 | 17e2377a | pbrook | } |
115 | 17e2377a | pbrook | |
116 | 17e2377a | pbrook | void qemu_free(void *ptr) |
117 | 17e2377a | pbrook | { |
118 | 17e2377a | pbrook | /* FIXME: We should unmark the reserved pages here. However this gets
|
119 | 17e2377a | pbrook | complicated when one target page spans multiple host pages, so we
|
120 | 17e2377a | pbrook | don't bother. */
|
121 | 17e2377a | pbrook | size_t *p; |
122 | 17e2377a | pbrook | p = (size_t *)((char *)ptr - 16); |
123 | 17e2377a | pbrook | munmap(p, *p); |
124 | 17e2377a | pbrook | } |
125 | 17e2377a | pbrook | |
126 | 1a6f0dbc | aliguori | void *qemu_realloc(void *ptr, size_t size) |
127 | 1a6f0dbc | aliguori | { |
128 | 1a6f0dbc | aliguori | size_t old_size, copy; |
129 | 1a6f0dbc | aliguori | void *new_ptr;
|
130 | 1a6f0dbc | aliguori | |
131 | baa8c602 | malc | if (!ptr)
|
132 | baa8c602 | malc | return qemu_malloc(size);
|
133 | 1a6f0dbc | aliguori | old_size = *(size_t *)((char *)ptr - 16); |
134 | 1a6f0dbc | aliguori | copy = old_size < size ? old_size : size; |
135 | 1a6f0dbc | aliguori | new_ptr = qemu_malloc(size); |
136 | 1a6f0dbc | aliguori | memcpy(new_ptr, ptr, copy); |
137 | 1a6f0dbc | aliguori | qemu_free(ptr); |
138 | 1a6f0dbc | aliguori | return new_ptr;
|
139 | 1a6f0dbc | aliguori | } |
140 | 1a6f0dbc | aliguori | |
141 | 53a5960a | pbrook | /* NOTE: all the constants are the HOST ones, but addresses are target. */
|
142 | 992f48a0 | blueswir1 | int target_mprotect(abi_ulong start, abi_ulong len, int prot) |
143 | 54936004 | bellard | { |
144 | 992f48a0 | blueswir1 | abi_ulong end, host_start, host_end, addr; |
145 | 54936004 | bellard | int prot1, ret;
|
146 | 54936004 | bellard | |
147 | 54936004 | bellard | #ifdef DEBUG_MMAP
|
148 | 0bf9e31a | Blue Swirl | printf("mprotect: start=0x" TARGET_ABI_FMT_lx
|
149 | 0bf9e31a | Blue Swirl | "len=0x" TARGET_ABI_FMT_lx " prot=%c%c%c\n", start, len, |
150 | 54936004 | bellard | prot & PROT_READ ? 'r' : '-', |
151 | 54936004 | bellard | prot & PROT_WRITE ? 'w' : '-', |
152 | 54936004 | bellard | prot & PROT_EXEC ? 'x' : '-'); |
153 | 54936004 | bellard | #endif
|
154 | 54936004 | bellard | |
155 | 54936004 | bellard | if ((start & ~TARGET_PAGE_MASK) != 0) |
156 | 54936004 | bellard | return -EINVAL;
|
157 | 54936004 | bellard | len = TARGET_PAGE_ALIGN(len); |
158 | 54936004 | bellard | end = start + len; |
159 | 54936004 | bellard | if (end < start)
|
160 | 54936004 | bellard | return -EINVAL;
|
161 | 171cd1cd | balrog | prot &= PROT_READ | PROT_WRITE | PROT_EXEC; |
162 | 54936004 | bellard | if (len == 0) |
163 | 54936004 | bellard | return 0; |
164 | 3b46e624 | ths | |
165 | c8a706fe | pbrook | mmap_lock(); |
166 | 83fb7adf | bellard | host_start = start & qemu_host_page_mask; |
167 | 54936004 | bellard | host_end = HOST_PAGE_ALIGN(end); |
168 | 54936004 | bellard | if (start > host_start) {
|
169 | 54936004 | bellard | /* handle host page containing start */
|
170 | 54936004 | bellard | prot1 = prot; |
171 | 54936004 | bellard | for(addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {
|
172 | 54936004 | bellard | prot1 |= page_get_flags(addr); |
173 | 54936004 | bellard | } |
174 | 83fb7adf | bellard | if (host_end == host_start + qemu_host_page_size) {
|
175 | d418c81e | bellard | for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
|
176 | d418c81e | bellard | prot1 |= page_get_flags(addr); |
177 | d418c81e | bellard | } |
178 | d418c81e | bellard | end = host_end; |
179 | d418c81e | bellard | } |
180 | 53a5960a | pbrook | ret = mprotect(g2h(host_start), qemu_host_page_size, prot1 & PAGE_BITS); |
181 | 54936004 | bellard | if (ret != 0) |
182 | c8a706fe | pbrook | goto error;
|
183 | 83fb7adf | bellard | host_start += qemu_host_page_size; |
184 | 54936004 | bellard | } |
185 | 54936004 | bellard | if (end < host_end) {
|
186 | 54936004 | bellard | prot1 = prot; |
187 | 54936004 | bellard | for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
|
188 | 54936004 | bellard | prot1 |= page_get_flags(addr); |
189 | 54936004 | bellard | } |
190 | 5fafdf24 | ths | ret = mprotect(g2h(host_end - qemu_host_page_size), qemu_host_page_size, |
191 | 54936004 | bellard | prot1 & PAGE_BITS); |
192 | 54936004 | bellard | if (ret != 0) |
193 | c8a706fe | pbrook | goto error;
|
194 | 83fb7adf | bellard | host_end -= qemu_host_page_size; |
195 | 54936004 | bellard | } |
196 | 3b46e624 | ths | |
197 | 54936004 | bellard | /* handle the pages in the middle */
|
198 | 54936004 | bellard | if (host_start < host_end) {
|
199 | 53a5960a | pbrook | ret = mprotect(g2h(host_start), host_end - host_start, prot); |
200 | 54936004 | bellard | if (ret != 0) |
201 | c8a706fe | pbrook | goto error;
|
202 | 54936004 | bellard | } |
203 | 54936004 | bellard | page_set_flags(start, start + len, prot | PAGE_VALID); |
204 | c8a706fe | pbrook | mmap_unlock(); |
205 | 54936004 | bellard | return 0; |
206 | c8a706fe | pbrook | error:
|
207 | c8a706fe | pbrook | mmap_unlock(); |
208 | c8a706fe | pbrook | return ret;
|
209 | 54936004 | bellard | } |
210 | 54936004 | bellard | |
211 | 54936004 | bellard | /* map an incomplete host page */
|
212 | 992f48a0 | blueswir1 | static int mmap_frag(abi_ulong real_start, |
213 | 992f48a0 | blueswir1 | abi_ulong start, abi_ulong end, |
214 | 992f48a0 | blueswir1 | int prot, int flags, int fd, abi_ulong offset) |
215 | 54936004 | bellard | { |
216 | 80210bcd | ths | abi_ulong real_end, addr; |
217 | 53a5960a | pbrook | void *host_start;
|
218 | 54936004 | bellard | int prot1, prot_new;
|
219 | 54936004 | bellard | |
220 | 53a5960a | pbrook | real_end = real_start + qemu_host_page_size; |
221 | 53a5960a | pbrook | host_start = g2h(real_start); |
222 | 54936004 | bellard | |
223 | 54936004 | bellard | /* get the protection of the target pages outside the mapping */
|
224 | 54936004 | bellard | prot1 = 0;
|
225 | 53a5960a | pbrook | for(addr = real_start; addr < real_end; addr++) {
|
226 | 54936004 | bellard | if (addr < start || addr >= end)
|
227 | 54936004 | bellard | prot1 |= page_get_flags(addr); |
228 | 54936004 | bellard | } |
229 | 3b46e624 | ths | |
230 | 54936004 | bellard | if (prot1 == 0) { |
231 | 54936004 | bellard | /* no page was there, so we allocate one */
|
232 | 80210bcd | ths | void *p = mmap(host_start, qemu_host_page_size, prot,
|
233 | 80210bcd | ths | flags | MAP_ANONYMOUS, -1, 0); |
234 | 80210bcd | ths | if (p == MAP_FAILED)
|
235 | 80210bcd | ths | return -1; |
236 | 53a5960a | pbrook | prot1 = prot; |
237 | 54936004 | bellard | } |
238 | 54936004 | bellard | prot1 &= PAGE_BITS; |
239 | 54936004 | bellard | |
240 | 54936004 | bellard | prot_new = prot | prot1; |
241 | 54936004 | bellard | if (!(flags & MAP_ANONYMOUS)) {
|
242 | 54936004 | bellard | /* msync() won't work here, so we return an error if write is
|
243 | 54936004 | bellard | possible while it is a shared mapping */
|
244 | 54936004 | bellard | if ((flags & MAP_TYPE) == MAP_SHARED &&
|
245 | 54936004 | bellard | (prot & PROT_WRITE)) |
246 | 54936004 | bellard | return -EINVAL;
|
247 | 54936004 | bellard | |
248 | 54936004 | bellard | /* adjust protection to be able to read */
|
249 | 54936004 | bellard | if (!(prot1 & PROT_WRITE))
|
250 | 53a5960a | pbrook | mprotect(host_start, qemu_host_page_size, prot1 | PROT_WRITE); |
251 | 3b46e624 | ths | |
252 | 54936004 | bellard | /* read the corresponding file data */
|
253 | 53a5960a | pbrook | pread(fd, g2h(start), end - start, offset); |
254 | 3b46e624 | ths | |
255 | 54936004 | bellard | /* put final protection */
|
256 | 54936004 | bellard | if (prot_new != (prot1 | PROT_WRITE))
|
257 | 53a5960a | pbrook | mprotect(host_start, qemu_host_page_size, prot_new); |
258 | 54936004 | bellard | } else {
|
259 | 54936004 | bellard | /* just update the protection */
|
260 | 54936004 | bellard | if (prot_new != prot1) {
|
261 | 53a5960a | pbrook | mprotect(host_start, qemu_host_page_size, prot_new); |
262 | 54936004 | bellard | } |
263 | 54936004 | bellard | } |
264 | 54936004 | bellard | return 0; |
265 | 54936004 | bellard | } |
266 | 54936004 | bellard | |
267 | a03e2d42 | bellard | #if defined(__CYGWIN__)
|
268 | a03e2d42 | bellard | /* Cygwin doesn't have a whole lot of address space. */
|
269 | a03e2d42 | bellard | static abi_ulong mmap_next_start = 0x18000000; |
270 | a03e2d42 | bellard | #else
|
271 | a03e2d42 | bellard | static abi_ulong mmap_next_start = 0x40000000; |
272 | a03e2d42 | bellard | #endif
|
273 | a03e2d42 | bellard | |
274 | 0776590d | pbrook | unsigned long last_brk; |
275 | 0776590d | pbrook | |
276 | fe3b4152 | Kirill A. Shutemov | /*
|
277 | fe3b4152 | Kirill A. Shutemov | * Find and reserve a free memory area of size 'size'. The search
|
278 | fe3b4152 | Kirill A. Shutemov | * starts at 'start'.
|
279 | fe3b4152 | Kirill A. Shutemov | * It must be called with mmap_lock() held.
|
280 | fe3b4152 | Kirill A. Shutemov | * Return -1 if error.
|
281 | fe3b4152 | Kirill A. Shutemov | */
|
282 | 9ad197d9 | Riku Voipio | abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size) |
283 | a03e2d42 | bellard | { |
284 | fe3b4152 | Kirill A. Shutemov | void *ptr;
|
285 | fe3b4152 | Kirill A. Shutemov | abi_ulong addr; |
286 | a03e2d42 | bellard | |
287 | a03e2d42 | bellard | size = HOST_PAGE_ALIGN(size); |
288 | fe3b4152 | Kirill A. Shutemov | start &= qemu_host_page_mask; |
289 | fe3b4152 | Kirill A. Shutemov | |
290 | fe3b4152 | Kirill A. Shutemov | /* If 'start' == 0, then a default start address is used. */
|
291 | fe3b4152 | Kirill A. Shutemov | if (start == 0) |
292 | fe3b4152 | Kirill A. Shutemov | start = mmap_next_start; |
293 | fe3b4152 | Kirill A. Shutemov | |
294 | a03e2d42 | bellard | addr = start; |
295 | fe3b4152 | Kirill A. Shutemov | |
296 | a03e2d42 | bellard | for(;;) {
|
297 | fe3b4152 | Kirill A. Shutemov | /*
|
298 | fe3b4152 | Kirill A. Shutemov | * Reserve needed memory area to avoid a race.
|
299 | fe3b4152 | Kirill A. Shutemov | * It should be discarded using:
|
300 | fe3b4152 | Kirill A. Shutemov | * - mmap() with MAP_FIXED flag
|
301 | fe3b4152 | Kirill A. Shutemov | * - mremap() with MREMAP_FIXED flag
|
302 | fe3b4152 | Kirill A. Shutemov | * - shmat() with SHM_REMAP flag
|
303 | fe3b4152 | Kirill A. Shutemov | */
|
304 | fe3b4152 | Kirill A. Shutemov | ptr = mmap((void *)(unsigned long)addr, size, PROT_NONE, |
305 | fe3b4152 | Kirill A. Shutemov | MAP_ANONYMOUS|MAP_PRIVATE|MAP_NORESERVE, -1, 0); |
306 | fe3b4152 | Kirill A. Shutemov | |
307 | fe3b4152 | Kirill A. Shutemov | /* ENOMEM, if host address space has no memory */
|
308 | fe3b4152 | Kirill A. Shutemov | if (ptr == MAP_FAILED)
|
309 | fe3b4152 | Kirill A. Shutemov | return (abi_ulong)-1; |
310 | fe3b4152 | Kirill A. Shutemov | |
311 | fe3b4152 | Kirill A. Shutemov | /* If address fits target address space we've found what we need */
|
312 | fe3b4152 | Kirill A. Shutemov | if ((unsigned long)ptr + size - 1 <= (abi_ulong)-1) |
313 | a03e2d42 | bellard | break;
|
314 | fe3b4152 | Kirill A. Shutemov | |
315 | fe3b4152 | Kirill A. Shutemov | /* Unmap and try again with new page */
|
316 | fe3b4152 | Kirill A. Shutemov | munmap(ptr, size); |
317 | a03e2d42 | bellard | addr += qemu_host_page_size; |
318 | fe3b4152 | Kirill A. Shutemov | |
319 | fe3b4152 | Kirill A. Shutemov | /* ENOMEM if we check whole of target address space */
|
320 | fe3b4152 | Kirill A. Shutemov | if (addr == start)
|
321 | a03e2d42 | bellard | return (abi_ulong)-1; |
322 | a03e2d42 | bellard | } |
323 | fe3b4152 | Kirill A. Shutemov | |
324 | fe3b4152 | Kirill A. Shutemov | /* Update default start address */
|
325 | fe3b4152 | Kirill A. Shutemov | if (start == mmap_next_start)
|
326 | fe3b4152 | Kirill A. Shutemov | mmap_next_start = (unsigned long)ptr + size; |
327 | fe3b4152 | Kirill A. Shutemov | |
328 | fe3b4152 | Kirill A. Shutemov | return h2g(ptr);
|
329 | a03e2d42 | bellard | } |
330 | a03e2d42 | bellard | |
331 | 54936004 | bellard | /* NOTE: all the constants are the HOST ones */
|
332 | 992f48a0 | blueswir1 | abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
|
333 | 992f48a0 | blueswir1 | int flags, int fd, abi_ulong offset) |
334 | 54936004 | bellard | { |
335 | 992f48a0 | blueswir1 | abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len; |
336 | a5b85f79 | ths | unsigned long host_start; |
337 | 54936004 | bellard | |
338 | c8a706fe | pbrook | mmap_lock(); |
339 | 54936004 | bellard | #ifdef DEBUG_MMAP
|
340 | 54936004 | bellard | { |
341 | 0bf9e31a | Blue Swirl | printf("mmap: start=0x" TARGET_ABI_FMT_lx
|
342 | 0bf9e31a | Blue Swirl | " len=0x" TARGET_ABI_FMT_lx " prot=%c%c%c flags=", |
343 | 5fafdf24 | ths | start, len, |
344 | 54936004 | bellard | prot & PROT_READ ? 'r' : '-', |
345 | 54936004 | bellard | prot & PROT_WRITE ? 'w' : '-', |
346 | 54936004 | bellard | prot & PROT_EXEC ? 'x' : '-'); |
347 | 54936004 | bellard | if (flags & MAP_FIXED)
|
348 | 54936004 | bellard | printf("MAP_FIXED ");
|
349 | 54936004 | bellard | if (flags & MAP_ANONYMOUS)
|
350 | 54936004 | bellard | printf("MAP_ANON ");
|
351 | 54936004 | bellard | switch(flags & MAP_TYPE) {
|
352 | 54936004 | bellard | case MAP_PRIVATE:
|
353 | 54936004 | bellard | printf("MAP_PRIVATE ");
|
354 | 54936004 | bellard | break;
|
355 | 54936004 | bellard | case MAP_SHARED:
|
356 | 54936004 | bellard | printf("MAP_SHARED ");
|
357 | 54936004 | bellard | break;
|
358 | 54936004 | bellard | default:
|
359 | 54936004 | bellard | printf("[MAP_TYPE=0x%x] ", flags & MAP_TYPE);
|
360 | 54936004 | bellard | break;
|
361 | 54936004 | bellard | } |
362 | 0bf9e31a | Blue Swirl | printf("fd=%d offset=" TARGET_ABI_FMT_lx "\n", fd, offset); |
363 | 54936004 | bellard | } |
364 | 54936004 | bellard | #endif
|
365 | 54936004 | bellard | |
366 | e89f07d3 | pbrook | if (offset & ~TARGET_PAGE_MASK) {
|
367 | e89f07d3 | pbrook | errno = EINVAL; |
368 | c8a706fe | pbrook | goto fail;
|
369 | e89f07d3 | pbrook | } |
370 | 54936004 | bellard | |
371 | 54936004 | bellard | len = TARGET_PAGE_ALIGN(len); |
372 | 54936004 | bellard | if (len == 0) |
373 | c8a706fe | pbrook | goto the_end;
|
374 | 53a5960a | pbrook | real_start = start & qemu_host_page_mask; |
375 | 54936004 | bellard | |
376 | 54c5a2ae | edgar_igl | /* When mapping files into a memory area larger than the file, accesses
|
377 | 54c5a2ae | edgar_igl | to pages beyond the file size will cause a SIGBUS.
|
378 | 54c5a2ae | edgar_igl | |
379 | 54c5a2ae | edgar_igl | For example, if mmaping a file of 100 bytes on a host with 4K pages
|
380 | 54c5a2ae | edgar_igl | emulating a target with 8K pages, the target expects to be able to
|
381 | 54c5a2ae | edgar_igl | access the first 8K. But the host will trap us on any access beyond
|
382 | 54c5a2ae | edgar_igl | 4K.
|
383 | 54c5a2ae | edgar_igl | |
384 | 54c5a2ae | edgar_igl | When emulating a target with a larger page-size than the hosts, we
|
385 | 54c5a2ae | edgar_igl | may need to truncate file maps at EOF and add extra anonymous pages
|
386 | 54c5a2ae | edgar_igl | up to the targets page boundary. */
|
387 | 54c5a2ae | edgar_igl | |
388 | 54c5a2ae | edgar_igl | if ((qemu_real_host_page_size < TARGET_PAGE_SIZE)
|
389 | 54c5a2ae | edgar_igl | && !(flags & MAP_ANONYMOUS)) { |
390 | 54c5a2ae | edgar_igl | struct stat sb;
|
391 | 54c5a2ae | edgar_igl | |
392 | 54c5a2ae | edgar_igl | if (fstat (fd, &sb) == -1) |
393 | 54c5a2ae | edgar_igl | goto fail;
|
394 | 54c5a2ae | edgar_igl | |
395 | 54c5a2ae | edgar_igl | /* Are we trying to create a map beyond EOF?. */
|
396 | 54c5a2ae | edgar_igl | if (offset + len > sb.st_size) {
|
397 | 54c5a2ae | edgar_igl | /* If so, truncate the file map at eof aligned with
|
398 | 54c5a2ae | edgar_igl | the hosts real pagesize. Additional anonymous maps
|
399 | 54c5a2ae | edgar_igl | will be created beyond EOF. */
|
400 | 54c5a2ae | edgar_igl | len = (sb.st_size - offset); |
401 | 54c5a2ae | edgar_igl | len += qemu_real_host_page_size - 1;
|
402 | 54c5a2ae | edgar_igl | len &= ~(qemu_real_host_page_size - 1);
|
403 | 54c5a2ae | edgar_igl | } |
404 | 54c5a2ae | edgar_igl | } |
405 | 54c5a2ae | edgar_igl | |
406 | 54936004 | bellard | if (!(flags & MAP_FIXED)) {
|
407 | a03e2d42 | bellard | abi_ulong mmap_start; |
408 | a03e2d42 | bellard | void *p;
|
409 | a03e2d42 | bellard | host_offset = offset & qemu_host_page_mask; |
410 | a03e2d42 | bellard | host_len = len + offset - host_offset; |
411 | a03e2d42 | bellard | host_len = HOST_PAGE_ALIGN(host_len); |
412 | a03e2d42 | bellard | mmap_start = mmap_find_vma(real_start, host_len); |
413 | a03e2d42 | bellard | if (mmap_start == (abi_ulong)-1) { |
414 | a03e2d42 | bellard | errno = ENOMEM; |
415 | c8a706fe | pbrook | goto fail;
|
416 | 54936004 | bellard | } |
417 | a03e2d42 | bellard | /* Note: we prefer to control the mapping address. It is
|
418 | a03e2d42 | bellard | especially important if qemu_host_page_size >
|
419 | a03e2d42 | bellard | qemu_real_host_page_size */
|
420 | a03e2d42 | bellard | p = mmap(g2h(mmap_start), |
421 | 54c5a2ae | edgar_igl | host_len, prot, flags | MAP_FIXED | MAP_ANONYMOUS, -1, 0); |
422 | a03e2d42 | bellard | if (p == MAP_FAILED)
|
423 | c8a706fe | pbrook | goto fail;
|
424 | a03e2d42 | bellard | /* update start so that it points to the file position at 'offset' */
|
425 | a03e2d42 | bellard | host_start = (unsigned long)p; |
426 | 54c5a2ae | edgar_igl | if (!(flags & MAP_ANONYMOUS)) {
|
427 | 54c5a2ae | edgar_igl | p = mmap(g2h(mmap_start), len, prot, |
428 | 54c5a2ae | edgar_igl | flags | MAP_FIXED, fd, host_offset); |
429 | a03e2d42 | bellard | host_start += offset - host_offset; |
430 | 54c5a2ae | edgar_igl | } |
431 | a03e2d42 | bellard | start = h2g(host_start); |
432 | a03e2d42 | bellard | } else {
|
433 | 7ab240ad | balrog | int flg;
|
434 | 7ab240ad | balrog | target_ulong addr; |
435 | 7ab240ad | balrog | |
436 | a03e2d42 | bellard | if (start & ~TARGET_PAGE_MASK) {
|
437 | e89f07d3 | pbrook | errno = EINVAL; |
438 | c8a706fe | pbrook | goto fail;
|
439 | e89f07d3 | pbrook | } |
440 | a03e2d42 | bellard | end = start + len; |
441 | a03e2d42 | bellard | real_end = HOST_PAGE_ALIGN(end); |
442 | 7ab240ad | balrog | |
443 | 45bc1f52 | aurel32 | /*
|
444 | 45bc1f52 | aurel32 | * Test if requested memory area fits target address space
|
445 | 45bc1f52 | aurel32 | * It can fail only on 64-bit host with 32-bit target.
|
446 | 45bc1f52 | aurel32 | * On any other target/host host mmap() handles this error correctly.
|
447 | 45bc1f52 | aurel32 | */
|
448 | 45bc1f52 | aurel32 | if ((unsigned long)start + len - 1 > (abi_ulong) -1) { |
449 | 45bc1f52 | aurel32 | errno = EINVAL; |
450 | 45bc1f52 | aurel32 | goto fail;
|
451 | 45bc1f52 | aurel32 | } |
452 | 45bc1f52 | aurel32 | |
453 | 7ab240ad | balrog | for(addr = real_start; addr < real_end; addr += TARGET_PAGE_SIZE) {
|
454 | 7ab240ad | balrog | flg = page_get_flags(addr); |
455 | 7ab240ad | balrog | if (flg & PAGE_RESERVED) {
|
456 | 7ab240ad | balrog | errno = ENXIO; |
457 | c8a706fe | pbrook | goto fail;
|
458 | 7ab240ad | balrog | } |
459 | 7ab240ad | balrog | } |
460 | 7ab240ad | balrog | |
461 | a03e2d42 | bellard | /* worst case: we cannot map the file because the offset is not
|
462 | a03e2d42 | bellard | aligned, so we read it */
|
463 | a03e2d42 | bellard | if (!(flags & MAP_ANONYMOUS) &&
|
464 | a03e2d42 | bellard | (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) { |
465 | a03e2d42 | bellard | /* msync() won't work here, so we return an error if write is
|
466 | a03e2d42 | bellard | possible while it is a shared mapping */
|
467 | a03e2d42 | bellard | if ((flags & MAP_TYPE) == MAP_SHARED &&
|
468 | a03e2d42 | bellard | (prot & PROT_WRITE)) { |
469 | a03e2d42 | bellard | errno = EINVAL; |
470 | c8a706fe | pbrook | goto fail;
|
471 | a03e2d42 | bellard | } |
472 | a03e2d42 | bellard | retaddr = target_mmap(start, len, prot | PROT_WRITE, |
473 | a03e2d42 | bellard | MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, |
474 | a03e2d42 | bellard | -1, 0); |
475 | a03e2d42 | bellard | if (retaddr == -1) |
476 | c8a706fe | pbrook | goto fail;
|
477 | a03e2d42 | bellard | pread(fd, g2h(start), len, offset); |
478 | a03e2d42 | bellard | if (!(prot & PROT_WRITE)) {
|
479 | a03e2d42 | bellard | ret = target_mprotect(start, len, prot); |
480 | c8a706fe | pbrook | if (ret != 0) { |
481 | c8a706fe | pbrook | start = ret; |
482 | c8a706fe | pbrook | goto the_end;
|
483 | c8a706fe | pbrook | } |
484 | a03e2d42 | bellard | } |
485 | a03e2d42 | bellard | goto the_end;
|
486 | 54936004 | bellard | } |
487 | a03e2d42 | bellard | |
488 | a03e2d42 | bellard | /* handle the start of the mapping */
|
489 | a03e2d42 | bellard | if (start > real_start) {
|
490 | a03e2d42 | bellard | if (real_end == real_start + qemu_host_page_size) {
|
491 | a03e2d42 | bellard | /* one single host page */
|
492 | a03e2d42 | bellard | ret = mmap_frag(real_start, start, end, |
493 | a03e2d42 | bellard | prot, flags, fd, offset); |
494 | a03e2d42 | bellard | if (ret == -1) |
495 | c8a706fe | pbrook | goto fail;
|
496 | a03e2d42 | bellard | goto the_end1;
|
497 | a03e2d42 | bellard | } |
498 | a03e2d42 | bellard | ret = mmap_frag(real_start, start, real_start + qemu_host_page_size, |
499 | 54936004 | bellard | prot, flags, fd, offset); |
500 | 54936004 | bellard | if (ret == -1) |
501 | c8a706fe | pbrook | goto fail;
|
502 | a03e2d42 | bellard | real_start += qemu_host_page_size; |
503 | a03e2d42 | bellard | } |
504 | a03e2d42 | bellard | /* handle the end of the mapping */
|
505 | a03e2d42 | bellard | if (end < real_end) {
|
506 | a03e2d42 | bellard | ret = mmap_frag(real_end - qemu_host_page_size, |
507 | a03e2d42 | bellard | real_end - qemu_host_page_size, real_end, |
508 | a03e2d42 | bellard | prot, flags, fd, |
509 | a03e2d42 | bellard | offset + real_end - qemu_host_page_size - start); |
510 | a03e2d42 | bellard | if (ret == -1) |
511 | c8a706fe | pbrook | goto fail;
|
512 | a03e2d42 | bellard | real_end -= qemu_host_page_size; |
513 | 54936004 | bellard | } |
514 | 3b46e624 | ths | |
515 | a03e2d42 | bellard | /* map the middle (easier) */
|
516 | a03e2d42 | bellard | if (real_start < real_end) {
|
517 | a03e2d42 | bellard | void *p;
|
518 | a03e2d42 | bellard | unsigned long offset1; |
519 | a03e2d42 | bellard | if (flags & MAP_ANONYMOUS)
|
520 | a03e2d42 | bellard | offset1 = 0;
|
521 | a03e2d42 | bellard | else
|
522 | a03e2d42 | bellard | offset1 = offset + real_start - start; |
523 | a03e2d42 | bellard | p = mmap(g2h(real_start), real_end - real_start, |
524 | a03e2d42 | bellard | prot, flags, fd, offset1); |
525 | a03e2d42 | bellard | if (p == MAP_FAILED)
|
526 | c8a706fe | pbrook | goto fail;
|
527 | a03e2d42 | bellard | } |
528 | 54936004 | bellard | } |
529 | 54936004 | bellard | the_end1:
|
530 | 54936004 | bellard | page_set_flags(start, start + len, prot | PAGE_VALID); |
531 | 54936004 | bellard | the_end:
|
532 | 54936004 | bellard | #ifdef DEBUG_MMAP
|
533 | 0bf9e31a | Blue Swirl | printf("ret=0x" TARGET_ABI_FMT_lx "\n", start); |
534 | 54936004 | bellard | page_dump(stdout); |
535 | 54936004 | bellard | printf("\n");
|
536 | 54936004 | bellard | #endif
|
537 | c8a706fe | pbrook | mmap_unlock(); |
538 | 54936004 | bellard | return start;
|
539 | c8a706fe | pbrook | fail:
|
540 | c8a706fe | pbrook | mmap_unlock(); |
541 | c8a706fe | pbrook | return -1; |
542 | 54936004 | bellard | } |
543 | 54936004 | bellard | |
544 | 992f48a0 | blueswir1 | int target_munmap(abi_ulong start, abi_ulong len)
|
545 | 54936004 | bellard | { |
546 | 992f48a0 | blueswir1 | abi_ulong end, real_start, real_end, addr; |
547 | 54936004 | bellard | int prot, ret;
|
548 | 54936004 | bellard | |
549 | 54936004 | bellard | #ifdef DEBUG_MMAP
|
550 | 0bf9e31a | Blue Swirl | printf("munmap: start=0x" TARGET_ABI_FMT_lx " len=0x" |
551 | 0bf9e31a | Blue Swirl | TARGET_ABI_FMT_lx "\n",
|
552 | 0bf9e31a | Blue Swirl | start, len); |
553 | 54936004 | bellard | #endif
|
554 | 54936004 | bellard | if (start & ~TARGET_PAGE_MASK)
|
555 | 54936004 | bellard | return -EINVAL;
|
556 | 54936004 | bellard | len = TARGET_PAGE_ALIGN(len); |
557 | 54936004 | bellard | if (len == 0) |
558 | 54936004 | bellard | return -EINVAL;
|
559 | c8a706fe | pbrook | mmap_lock(); |
560 | 54936004 | bellard | end = start + len; |
561 | 53a5960a | pbrook | real_start = start & qemu_host_page_mask; |
562 | 53a5960a | pbrook | real_end = HOST_PAGE_ALIGN(end); |
563 | 54936004 | bellard | |
564 | 53a5960a | pbrook | if (start > real_start) {
|
565 | 54936004 | bellard | /* handle host page containing start */
|
566 | 54936004 | bellard | prot = 0;
|
567 | 53a5960a | pbrook | for(addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) {
|
568 | 54936004 | bellard | prot |= page_get_flags(addr); |
569 | 54936004 | bellard | } |
570 | 53a5960a | pbrook | if (real_end == real_start + qemu_host_page_size) {
|
571 | 53a5960a | pbrook | for(addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
|
572 | d418c81e | bellard | prot |= page_get_flags(addr); |
573 | d418c81e | bellard | } |
574 | 53a5960a | pbrook | end = real_end; |
575 | d418c81e | bellard | } |
576 | 54936004 | bellard | if (prot != 0) |
577 | 53a5960a | pbrook | real_start += qemu_host_page_size; |
578 | 54936004 | bellard | } |
579 | 53a5960a | pbrook | if (end < real_end) {
|
580 | 54936004 | bellard | prot = 0;
|
581 | 53a5960a | pbrook | for(addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
|
582 | 54936004 | bellard | prot |= page_get_flags(addr); |
583 | 54936004 | bellard | } |
584 | 54936004 | bellard | if (prot != 0) |
585 | 53a5960a | pbrook | real_end -= qemu_host_page_size; |
586 | 54936004 | bellard | } |
587 | 3b46e624 | ths | |
588 | c8a706fe | pbrook | ret = 0;
|
589 | 54936004 | bellard | /* unmap what we can */
|
590 | 53a5960a | pbrook | if (real_start < real_end) {
|
591 | 4118a970 | j_mayer | ret = munmap(g2h(real_start), real_end - real_start); |
592 | 54936004 | bellard | } |
593 | 54936004 | bellard | |
594 | c8a706fe | pbrook | if (ret == 0) |
595 | c8a706fe | pbrook | page_set_flags(start, start + len, 0);
|
596 | c8a706fe | pbrook | mmap_unlock(); |
597 | c8a706fe | pbrook | return ret;
|
598 | 54936004 | bellard | } |
599 | 54936004 | bellard | |
600 | 992f48a0 | blueswir1 | abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, |
601 | 992f48a0 | blueswir1 | abi_ulong new_size, unsigned long flags, |
602 | 992f48a0 | blueswir1 | abi_ulong new_addr) |
603 | 54936004 | bellard | { |
604 | 54936004 | bellard | int prot;
|
605 | f19412a2 | aurel32 | void *host_addr;
|
606 | 54936004 | bellard | |
607 | c8a706fe | pbrook | mmap_lock(); |
608 | f19412a2 | aurel32 | |
609 | f19412a2 | aurel32 | if (flags & MREMAP_FIXED)
|
610 | 3af72a4d | blueswir1 | host_addr = (void *) syscall(__NR_mremap, g2h(old_addr),
|
611 | 3af72a4d | blueswir1 | old_size, new_size, |
612 | 3af72a4d | blueswir1 | flags, |
613 | 3af72a4d | blueswir1 | new_addr); |
614 | f19412a2 | aurel32 | else if (flags & MREMAP_MAYMOVE) { |
615 | f19412a2 | aurel32 | abi_ulong mmap_start; |
616 | f19412a2 | aurel32 | |
617 | f19412a2 | aurel32 | mmap_start = mmap_find_vma(0, new_size);
|
618 | f19412a2 | aurel32 | |
619 | f19412a2 | aurel32 | if (mmap_start == -1) { |
620 | f19412a2 | aurel32 | errno = ENOMEM; |
621 | f19412a2 | aurel32 | host_addr = MAP_FAILED; |
622 | f19412a2 | aurel32 | } else
|
623 | 3af72a4d | blueswir1 | host_addr = (void *) syscall(__NR_mremap, g2h(old_addr),
|
624 | 3af72a4d | blueswir1 | old_size, new_size, |
625 | 3af72a4d | blueswir1 | flags | MREMAP_FIXED, |
626 | 3af72a4d | blueswir1 | g2h(mmap_start)); |
627 | 3af72a4d | blueswir1 | } else {
|
628 | f19412a2 | aurel32 | host_addr = mremap(g2h(old_addr), old_size, new_size, flags); |
629 | f19412a2 | aurel32 | /* Check if address fits target address space */
|
630 | f19412a2 | aurel32 | if ((unsigned long)host_addr + new_size > (abi_ulong)-1) { |
631 | f19412a2 | aurel32 | /* Revert mremap() changes */
|
632 | f19412a2 | aurel32 | host_addr = mremap(g2h(old_addr), new_size, old_size, flags); |
633 | f19412a2 | aurel32 | errno = ENOMEM; |
634 | f19412a2 | aurel32 | host_addr = MAP_FAILED; |
635 | f19412a2 | aurel32 | } |
636 | f19412a2 | aurel32 | } |
637 | f19412a2 | aurel32 | |
638 | f19412a2 | aurel32 | if (host_addr == MAP_FAILED) {
|
639 | c8a706fe | pbrook | new_addr = -1;
|
640 | c8a706fe | pbrook | } else {
|
641 | c8a706fe | pbrook | new_addr = h2g(host_addr); |
642 | c8a706fe | pbrook | prot = page_get_flags(old_addr); |
643 | c8a706fe | pbrook | page_set_flags(old_addr, old_addr + old_size, 0);
|
644 | c8a706fe | pbrook | page_set_flags(new_addr, new_addr + new_size, prot | PAGE_VALID); |
645 | c8a706fe | pbrook | } |
646 | c8a706fe | pbrook | mmap_unlock(); |
647 | 54936004 | bellard | return new_addr;
|
648 | 54936004 | bellard | } |
649 | 54936004 | bellard | |
650 | 992f48a0 | blueswir1 | int target_msync(abi_ulong start, abi_ulong len, int flags) |
651 | 54936004 | bellard | { |
652 | 992f48a0 | blueswir1 | abi_ulong end; |
653 | 54936004 | bellard | |
654 | 54936004 | bellard | if (start & ~TARGET_PAGE_MASK)
|
655 | 54936004 | bellard | return -EINVAL;
|
656 | 54936004 | bellard | len = TARGET_PAGE_ALIGN(len); |
657 | 54936004 | bellard | end = start + len; |
658 | d418c81e | bellard | if (end < start)
|
659 | d418c81e | bellard | return -EINVAL;
|
660 | d418c81e | bellard | if (end == start)
|
661 | d418c81e | bellard | return 0; |
662 | 3b46e624 | ths | |
663 | 83fb7adf | bellard | start &= qemu_host_page_mask; |
664 | 53a5960a | pbrook | return msync(g2h(start), end - start, flags);
|
665 | 54936004 | bellard | } |