root / linux-user / mmap.c @ fb7e378c
History | View | Annotate | Download (19.5 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 | dfd3f85c | Juan Quintela | static __thread int 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 | fb7e378c | Kirill A. Shutemov | if (pread(fd, g2h(start), end - start, offset) == -1) |
254 | fb7e378c | Kirill A. Shutemov | return -1; |
255 | 3b46e624 | ths | |
256 | 54936004 | bellard | /* put final protection */
|
257 | 54936004 | bellard | if (prot_new != (prot1 | PROT_WRITE))
|
258 | 53a5960a | pbrook | mprotect(host_start, qemu_host_page_size, prot_new); |
259 | 54936004 | bellard | } else {
|
260 | 54936004 | bellard | /* just update the protection */
|
261 | 54936004 | bellard | if (prot_new != prot1) {
|
262 | 53a5960a | pbrook | mprotect(host_start, qemu_host_page_size, prot_new); |
263 | 54936004 | bellard | } |
264 | 54936004 | bellard | } |
265 | 54936004 | bellard | return 0; |
266 | 54936004 | bellard | } |
267 | 54936004 | bellard | |
268 | a03e2d42 | bellard | #if defined(__CYGWIN__)
|
269 | a03e2d42 | bellard | /* Cygwin doesn't have a whole lot of address space. */
|
270 | a03e2d42 | bellard | static abi_ulong mmap_next_start = 0x18000000; |
271 | a03e2d42 | bellard | #else
|
272 | a03e2d42 | bellard | static abi_ulong mmap_next_start = 0x40000000; |
273 | a03e2d42 | bellard | #endif
|
274 | a03e2d42 | bellard | |
275 | 0776590d | pbrook | unsigned long last_brk; |
276 | 0776590d | pbrook | |
277 | fe3b4152 | Kirill A. Shutemov | /*
|
278 | fe3b4152 | Kirill A. Shutemov | * Find and reserve a free memory area of size 'size'. The search
|
279 | fe3b4152 | Kirill A. Shutemov | * starts at 'start'.
|
280 | fe3b4152 | Kirill A. Shutemov | * It must be called with mmap_lock() held.
|
281 | fe3b4152 | Kirill A. Shutemov | * Return -1 if error.
|
282 | fe3b4152 | Kirill A. Shutemov | */
|
283 | 9ad197d9 | Riku Voipio | abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size) |
284 | a03e2d42 | bellard | { |
285 | fe3b4152 | Kirill A. Shutemov | void *ptr;
|
286 | fe3b4152 | Kirill A. Shutemov | abi_ulong addr; |
287 | a03e2d42 | bellard | |
288 | a03e2d42 | bellard | size = HOST_PAGE_ALIGN(size); |
289 | fe3b4152 | Kirill A. Shutemov | start &= qemu_host_page_mask; |
290 | fe3b4152 | Kirill A. Shutemov | |
291 | fe3b4152 | Kirill A. Shutemov | /* If 'start' == 0, then a default start address is used. */
|
292 | fe3b4152 | Kirill A. Shutemov | if (start == 0) |
293 | fe3b4152 | Kirill A. Shutemov | start = mmap_next_start; |
294 | fe3b4152 | Kirill A. Shutemov | |
295 | a03e2d42 | bellard | addr = start; |
296 | fe3b4152 | Kirill A. Shutemov | |
297 | a03e2d42 | bellard | for(;;) {
|
298 | fe3b4152 | Kirill A. Shutemov | /*
|
299 | fe3b4152 | Kirill A. Shutemov | * Reserve needed memory area to avoid a race.
|
300 | fe3b4152 | Kirill A. Shutemov | * It should be discarded using:
|
301 | fe3b4152 | Kirill A. Shutemov | * - mmap() with MAP_FIXED flag
|
302 | fe3b4152 | Kirill A. Shutemov | * - mremap() with MREMAP_FIXED flag
|
303 | fe3b4152 | Kirill A. Shutemov | * - shmat() with SHM_REMAP flag
|
304 | fe3b4152 | Kirill A. Shutemov | */
|
305 | fe3b4152 | Kirill A. Shutemov | ptr = mmap((void *)(unsigned long)addr, size, PROT_NONE, |
306 | fe3b4152 | Kirill A. Shutemov | MAP_ANONYMOUS|MAP_PRIVATE|MAP_NORESERVE, -1, 0); |
307 | fe3b4152 | Kirill A. Shutemov | |
308 | fe3b4152 | Kirill A. Shutemov | /* ENOMEM, if host address space has no memory */
|
309 | fe3b4152 | Kirill A. Shutemov | if (ptr == MAP_FAILED)
|
310 | fe3b4152 | Kirill A. Shutemov | return (abi_ulong)-1; |
311 | fe3b4152 | Kirill A. Shutemov | |
312 | fe3b4152 | Kirill A. Shutemov | /* If address fits target address space we've found what we need */
|
313 | fe3b4152 | Kirill A. Shutemov | if ((unsigned long)ptr + size - 1 <= (abi_ulong)-1) |
314 | a03e2d42 | bellard | break;
|
315 | fe3b4152 | Kirill A. Shutemov | |
316 | fe3b4152 | Kirill A. Shutemov | /* Unmap and try again with new page */
|
317 | fe3b4152 | Kirill A. Shutemov | munmap(ptr, size); |
318 | a03e2d42 | bellard | addr += qemu_host_page_size; |
319 | fe3b4152 | Kirill A. Shutemov | |
320 | fe3b4152 | Kirill A. Shutemov | /* ENOMEM if we check whole of target address space */
|
321 | fe3b4152 | Kirill A. Shutemov | if (addr == start)
|
322 | a03e2d42 | bellard | return (abi_ulong)-1; |
323 | a03e2d42 | bellard | } |
324 | fe3b4152 | Kirill A. Shutemov | |
325 | fe3b4152 | Kirill A. Shutemov | /* Update default start address */
|
326 | fe3b4152 | Kirill A. Shutemov | if (start == mmap_next_start)
|
327 | fe3b4152 | Kirill A. Shutemov | mmap_next_start = (unsigned long)ptr + size; |
328 | fe3b4152 | Kirill A. Shutemov | |
329 | fe3b4152 | Kirill A. Shutemov | return h2g(ptr);
|
330 | a03e2d42 | bellard | } |
331 | a03e2d42 | bellard | |
332 | 54936004 | bellard | /* NOTE: all the constants are the HOST ones */
|
333 | 992f48a0 | blueswir1 | abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
|
334 | 992f48a0 | blueswir1 | int flags, int fd, abi_ulong offset) |
335 | 54936004 | bellard | { |
336 | 992f48a0 | blueswir1 | abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len; |
337 | a5b85f79 | ths | unsigned long host_start; |
338 | 54936004 | bellard | |
339 | c8a706fe | pbrook | mmap_lock(); |
340 | 54936004 | bellard | #ifdef DEBUG_MMAP
|
341 | 54936004 | bellard | { |
342 | 0bf9e31a | Blue Swirl | printf("mmap: start=0x" TARGET_ABI_FMT_lx
|
343 | 0bf9e31a | Blue Swirl | " len=0x" TARGET_ABI_FMT_lx " prot=%c%c%c flags=", |
344 | 5fafdf24 | ths | start, len, |
345 | 54936004 | bellard | prot & PROT_READ ? 'r' : '-', |
346 | 54936004 | bellard | prot & PROT_WRITE ? 'w' : '-', |
347 | 54936004 | bellard | prot & PROT_EXEC ? 'x' : '-'); |
348 | 54936004 | bellard | if (flags & MAP_FIXED)
|
349 | 54936004 | bellard | printf("MAP_FIXED ");
|
350 | 54936004 | bellard | if (flags & MAP_ANONYMOUS)
|
351 | 54936004 | bellard | printf("MAP_ANON ");
|
352 | 54936004 | bellard | switch(flags & MAP_TYPE) {
|
353 | 54936004 | bellard | case MAP_PRIVATE:
|
354 | 54936004 | bellard | printf("MAP_PRIVATE ");
|
355 | 54936004 | bellard | break;
|
356 | 54936004 | bellard | case MAP_SHARED:
|
357 | 54936004 | bellard | printf("MAP_SHARED ");
|
358 | 54936004 | bellard | break;
|
359 | 54936004 | bellard | default:
|
360 | 54936004 | bellard | printf("[MAP_TYPE=0x%x] ", flags & MAP_TYPE);
|
361 | 54936004 | bellard | break;
|
362 | 54936004 | bellard | } |
363 | 0bf9e31a | Blue Swirl | printf("fd=%d offset=" TARGET_ABI_FMT_lx "\n", fd, offset); |
364 | 54936004 | bellard | } |
365 | 54936004 | bellard | #endif
|
366 | 54936004 | bellard | |
367 | e89f07d3 | pbrook | if (offset & ~TARGET_PAGE_MASK) {
|
368 | e89f07d3 | pbrook | errno = EINVAL; |
369 | c8a706fe | pbrook | goto fail;
|
370 | e89f07d3 | pbrook | } |
371 | 54936004 | bellard | |
372 | 54936004 | bellard | len = TARGET_PAGE_ALIGN(len); |
373 | 54936004 | bellard | if (len == 0) |
374 | c8a706fe | pbrook | goto the_end;
|
375 | 53a5960a | pbrook | real_start = start & qemu_host_page_mask; |
376 | 54936004 | bellard | |
377 | 54c5a2ae | edgar_igl | /* When mapping files into a memory area larger than the file, accesses
|
378 | 54c5a2ae | edgar_igl | to pages beyond the file size will cause a SIGBUS.
|
379 | 54c5a2ae | edgar_igl | |
380 | 54c5a2ae | edgar_igl | For example, if mmaping a file of 100 bytes on a host with 4K pages
|
381 | 54c5a2ae | edgar_igl | emulating a target with 8K pages, the target expects to be able to
|
382 | 54c5a2ae | edgar_igl | access the first 8K. But the host will trap us on any access beyond
|
383 | 54c5a2ae | edgar_igl | 4K.
|
384 | 54c5a2ae | edgar_igl | |
385 | 54c5a2ae | edgar_igl | When emulating a target with a larger page-size than the hosts, we
|
386 | 54c5a2ae | edgar_igl | may need to truncate file maps at EOF and add extra anonymous pages
|
387 | 54c5a2ae | edgar_igl | up to the targets page boundary. */
|
388 | 54c5a2ae | edgar_igl | |
389 | 54c5a2ae | edgar_igl | if ((qemu_real_host_page_size < TARGET_PAGE_SIZE)
|
390 | 54c5a2ae | edgar_igl | && !(flags & MAP_ANONYMOUS)) { |
391 | 54c5a2ae | edgar_igl | struct stat sb;
|
392 | 54c5a2ae | edgar_igl | |
393 | 54c5a2ae | edgar_igl | if (fstat (fd, &sb) == -1) |
394 | 54c5a2ae | edgar_igl | goto fail;
|
395 | 54c5a2ae | edgar_igl | |
396 | 54c5a2ae | edgar_igl | /* Are we trying to create a map beyond EOF?. */
|
397 | 54c5a2ae | edgar_igl | if (offset + len > sb.st_size) {
|
398 | 54c5a2ae | edgar_igl | /* If so, truncate the file map at eof aligned with
|
399 | 54c5a2ae | edgar_igl | the hosts real pagesize. Additional anonymous maps
|
400 | 54c5a2ae | edgar_igl | will be created beyond EOF. */
|
401 | 54c5a2ae | edgar_igl | len = (sb.st_size - offset); |
402 | 54c5a2ae | edgar_igl | len += qemu_real_host_page_size - 1;
|
403 | 54c5a2ae | edgar_igl | len &= ~(qemu_real_host_page_size - 1);
|
404 | 54c5a2ae | edgar_igl | } |
405 | 54c5a2ae | edgar_igl | } |
406 | 54c5a2ae | edgar_igl | |
407 | 54936004 | bellard | if (!(flags & MAP_FIXED)) {
|
408 | a03e2d42 | bellard | abi_ulong mmap_start; |
409 | a03e2d42 | bellard | void *p;
|
410 | a03e2d42 | bellard | host_offset = offset & qemu_host_page_mask; |
411 | a03e2d42 | bellard | host_len = len + offset - host_offset; |
412 | a03e2d42 | bellard | host_len = HOST_PAGE_ALIGN(host_len); |
413 | a03e2d42 | bellard | mmap_start = mmap_find_vma(real_start, host_len); |
414 | a03e2d42 | bellard | if (mmap_start == (abi_ulong)-1) { |
415 | a03e2d42 | bellard | errno = ENOMEM; |
416 | c8a706fe | pbrook | goto fail;
|
417 | 54936004 | bellard | } |
418 | a03e2d42 | bellard | /* Note: we prefer to control the mapping address. It is
|
419 | a03e2d42 | bellard | especially important if qemu_host_page_size >
|
420 | a03e2d42 | bellard | qemu_real_host_page_size */
|
421 | a03e2d42 | bellard | p = mmap(g2h(mmap_start), |
422 | 54c5a2ae | edgar_igl | host_len, prot, flags | MAP_FIXED | MAP_ANONYMOUS, -1, 0); |
423 | a03e2d42 | bellard | if (p == MAP_FAILED)
|
424 | c8a706fe | pbrook | goto fail;
|
425 | a03e2d42 | bellard | /* update start so that it points to the file position at 'offset' */
|
426 | a03e2d42 | bellard | host_start = (unsigned long)p; |
427 | 54c5a2ae | edgar_igl | if (!(flags & MAP_ANONYMOUS)) {
|
428 | 54c5a2ae | edgar_igl | p = mmap(g2h(mmap_start), len, prot, |
429 | 54c5a2ae | edgar_igl | flags | MAP_FIXED, fd, host_offset); |
430 | a03e2d42 | bellard | host_start += offset - host_offset; |
431 | 54c5a2ae | edgar_igl | } |
432 | a03e2d42 | bellard | start = h2g(host_start); |
433 | a03e2d42 | bellard | } else {
|
434 | 7ab240ad | balrog | int flg;
|
435 | 7ab240ad | balrog | target_ulong addr; |
436 | 7ab240ad | balrog | |
437 | a03e2d42 | bellard | if (start & ~TARGET_PAGE_MASK) {
|
438 | e89f07d3 | pbrook | errno = EINVAL; |
439 | c8a706fe | pbrook | goto fail;
|
440 | e89f07d3 | pbrook | } |
441 | a03e2d42 | bellard | end = start + len; |
442 | a03e2d42 | bellard | real_end = HOST_PAGE_ALIGN(end); |
443 | 7ab240ad | balrog | |
444 | 45bc1f52 | aurel32 | /*
|
445 | 45bc1f52 | aurel32 | * Test if requested memory area fits target address space
|
446 | 45bc1f52 | aurel32 | * It can fail only on 64-bit host with 32-bit target.
|
447 | 45bc1f52 | aurel32 | * On any other target/host host mmap() handles this error correctly.
|
448 | 45bc1f52 | aurel32 | */
|
449 | 45bc1f52 | aurel32 | if ((unsigned long)start + len - 1 > (abi_ulong) -1) { |
450 | 45bc1f52 | aurel32 | errno = EINVAL; |
451 | 45bc1f52 | aurel32 | goto fail;
|
452 | 45bc1f52 | aurel32 | } |
453 | 45bc1f52 | aurel32 | |
454 | 7ab240ad | balrog | for(addr = real_start; addr < real_end; addr += TARGET_PAGE_SIZE) {
|
455 | 7ab240ad | balrog | flg = page_get_flags(addr); |
456 | 7ab240ad | balrog | if (flg & PAGE_RESERVED) {
|
457 | 7ab240ad | balrog | errno = ENXIO; |
458 | c8a706fe | pbrook | goto fail;
|
459 | 7ab240ad | balrog | } |
460 | 7ab240ad | balrog | } |
461 | 7ab240ad | balrog | |
462 | a03e2d42 | bellard | /* worst case: we cannot map the file because the offset is not
|
463 | a03e2d42 | bellard | aligned, so we read it */
|
464 | a03e2d42 | bellard | if (!(flags & MAP_ANONYMOUS) &&
|
465 | a03e2d42 | bellard | (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) { |
466 | a03e2d42 | bellard | /* msync() won't work here, so we return an error if write is
|
467 | a03e2d42 | bellard | possible while it is a shared mapping */
|
468 | a03e2d42 | bellard | if ((flags & MAP_TYPE) == MAP_SHARED &&
|
469 | a03e2d42 | bellard | (prot & PROT_WRITE)) { |
470 | a03e2d42 | bellard | errno = EINVAL; |
471 | c8a706fe | pbrook | goto fail;
|
472 | a03e2d42 | bellard | } |
473 | a03e2d42 | bellard | retaddr = target_mmap(start, len, prot | PROT_WRITE, |
474 | a03e2d42 | bellard | MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, |
475 | a03e2d42 | bellard | -1, 0); |
476 | a03e2d42 | bellard | if (retaddr == -1) |
477 | c8a706fe | pbrook | goto fail;
|
478 | fb7e378c | Kirill A. Shutemov | if (pread(fd, g2h(start), len, offset) == -1) |
479 | fb7e378c | Kirill A. Shutemov | goto fail;
|
480 | a03e2d42 | bellard | if (!(prot & PROT_WRITE)) {
|
481 | a03e2d42 | bellard | ret = target_mprotect(start, len, prot); |
482 | c8a706fe | pbrook | if (ret != 0) { |
483 | c8a706fe | pbrook | start = ret; |
484 | c8a706fe | pbrook | goto the_end;
|
485 | c8a706fe | pbrook | } |
486 | a03e2d42 | bellard | } |
487 | a03e2d42 | bellard | goto the_end;
|
488 | 54936004 | bellard | } |
489 | a03e2d42 | bellard | |
490 | a03e2d42 | bellard | /* handle the start of the mapping */
|
491 | a03e2d42 | bellard | if (start > real_start) {
|
492 | a03e2d42 | bellard | if (real_end == real_start + qemu_host_page_size) {
|
493 | a03e2d42 | bellard | /* one single host page */
|
494 | a03e2d42 | bellard | ret = mmap_frag(real_start, start, end, |
495 | a03e2d42 | bellard | prot, flags, fd, offset); |
496 | a03e2d42 | bellard | if (ret == -1) |
497 | c8a706fe | pbrook | goto fail;
|
498 | a03e2d42 | bellard | goto the_end1;
|
499 | a03e2d42 | bellard | } |
500 | a03e2d42 | bellard | ret = mmap_frag(real_start, start, real_start + qemu_host_page_size, |
501 | 54936004 | bellard | prot, flags, fd, offset); |
502 | 54936004 | bellard | if (ret == -1) |
503 | c8a706fe | pbrook | goto fail;
|
504 | a03e2d42 | bellard | real_start += qemu_host_page_size; |
505 | a03e2d42 | bellard | } |
506 | a03e2d42 | bellard | /* handle the end of the mapping */
|
507 | a03e2d42 | bellard | if (end < real_end) {
|
508 | a03e2d42 | bellard | ret = mmap_frag(real_end - qemu_host_page_size, |
509 | a03e2d42 | bellard | real_end - qemu_host_page_size, real_end, |
510 | a03e2d42 | bellard | prot, flags, fd, |
511 | a03e2d42 | bellard | offset + real_end - qemu_host_page_size - start); |
512 | a03e2d42 | bellard | if (ret == -1) |
513 | c8a706fe | pbrook | goto fail;
|
514 | a03e2d42 | bellard | real_end -= qemu_host_page_size; |
515 | 54936004 | bellard | } |
516 | 3b46e624 | ths | |
517 | a03e2d42 | bellard | /* map the middle (easier) */
|
518 | a03e2d42 | bellard | if (real_start < real_end) {
|
519 | a03e2d42 | bellard | void *p;
|
520 | a03e2d42 | bellard | unsigned long offset1; |
521 | a03e2d42 | bellard | if (flags & MAP_ANONYMOUS)
|
522 | a03e2d42 | bellard | offset1 = 0;
|
523 | a03e2d42 | bellard | else
|
524 | a03e2d42 | bellard | offset1 = offset + real_start - start; |
525 | a03e2d42 | bellard | p = mmap(g2h(real_start), real_end - real_start, |
526 | a03e2d42 | bellard | prot, flags, fd, offset1); |
527 | a03e2d42 | bellard | if (p == MAP_FAILED)
|
528 | c8a706fe | pbrook | goto fail;
|
529 | a03e2d42 | bellard | } |
530 | 54936004 | bellard | } |
531 | 54936004 | bellard | the_end1:
|
532 | 54936004 | bellard | page_set_flags(start, start + len, prot | PAGE_VALID); |
533 | 54936004 | bellard | the_end:
|
534 | 54936004 | bellard | #ifdef DEBUG_MMAP
|
535 | 0bf9e31a | Blue Swirl | printf("ret=0x" TARGET_ABI_FMT_lx "\n", start); |
536 | 54936004 | bellard | page_dump(stdout); |
537 | 54936004 | bellard | printf("\n");
|
538 | 54936004 | bellard | #endif
|
539 | c8a706fe | pbrook | mmap_unlock(); |
540 | 54936004 | bellard | return start;
|
541 | c8a706fe | pbrook | fail:
|
542 | c8a706fe | pbrook | mmap_unlock(); |
543 | c8a706fe | pbrook | return -1; |
544 | 54936004 | bellard | } |
545 | 54936004 | bellard | |
546 | 992f48a0 | blueswir1 | int target_munmap(abi_ulong start, abi_ulong len)
|
547 | 54936004 | bellard | { |
548 | 992f48a0 | blueswir1 | abi_ulong end, real_start, real_end, addr; |
549 | 54936004 | bellard | int prot, ret;
|
550 | 54936004 | bellard | |
551 | 54936004 | bellard | #ifdef DEBUG_MMAP
|
552 | 0bf9e31a | Blue Swirl | printf("munmap: start=0x" TARGET_ABI_FMT_lx " len=0x" |
553 | 0bf9e31a | Blue Swirl | TARGET_ABI_FMT_lx "\n",
|
554 | 0bf9e31a | Blue Swirl | start, len); |
555 | 54936004 | bellard | #endif
|
556 | 54936004 | bellard | if (start & ~TARGET_PAGE_MASK)
|
557 | 54936004 | bellard | return -EINVAL;
|
558 | 54936004 | bellard | len = TARGET_PAGE_ALIGN(len); |
559 | 54936004 | bellard | if (len == 0) |
560 | 54936004 | bellard | return -EINVAL;
|
561 | c8a706fe | pbrook | mmap_lock(); |
562 | 54936004 | bellard | end = start + len; |
563 | 53a5960a | pbrook | real_start = start & qemu_host_page_mask; |
564 | 53a5960a | pbrook | real_end = HOST_PAGE_ALIGN(end); |
565 | 54936004 | bellard | |
566 | 53a5960a | pbrook | if (start > real_start) {
|
567 | 54936004 | bellard | /* handle host page containing start */
|
568 | 54936004 | bellard | prot = 0;
|
569 | 53a5960a | pbrook | for(addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) {
|
570 | 54936004 | bellard | prot |= page_get_flags(addr); |
571 | 54936004 | bellard | } |
572 | 53a5960a | pbrook | if (real_end == real_start + qemu_host_page_size) {
|
573 | 53a5960a | pbrook | for(addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
|
574 | d418c81e | bellard | prot |= page_get_flags(addr); |
575 | d418c81e | bellard | } |
576 | 53a5960a | pbrook | end = real_end; |
577 | d418c81e | bellard | } |
578 | 54936004 | bellard | if (prot != 0) |
579 | 53a5960a | pbrook | real_start += qemu_host_page_size; |
580 | 54936004 | bellard | } |
581 | 53a5960a | pbrook | if (end < real_end) {
|
582 | 54936004 | bellard | prot = 0;
|
583 | 53a5960a | pbrook | for(addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
|
584 | 54936004 | bellard | prot |= page_get_flags(addr); |
585 | 54936004 | bellard | } |
586 | 54936004 | bellard | if (prot != 0) |
587 | 53a5960a | pbrook | real_end -= qemu_host_page_size; |
588 | 54936004 | bellard | } |
589 | 3b46e624 | ths | |
590 | c8a706fe | pbrook | ret = 0;
|
591 | 54936004 | bellard | /* unmap what we can */
|
592 | 53a5960a | pbrook | if (real_start < real_end) {
|
593 | 4118a970 | j_mayer | ret = munmap(g2h(real_start), real_end - real_start); |
594 | 54936004 | bellard | } |
595 | 54936004 | bellard | |
596 | c8a706fe | pbrook | if (ret == 0) |
597 | c8a706fe | pbrook | page_set_flags(start, start + len, 0);
|
598 | c8a706fe | pbrook | mmap_unlock(); |
599 | c8a706fe | pbrook | return ret;
|
600 | 54936004 | bellard | } |
601 | 54936004 | bellard | |
602 | 992f48a0 | blueswir1 | abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, |
603 | 992f48a0 | blueswir1 | abi_ulong new_size, unsigned long flags, |
604 | 992f48a0 | blueswir1 | abi_ulong new_addr) |
605 | 54936004 | bellard | { |
606 | 54936004 | bellard | int prot;
|
607 | f19412a2 | aurel32 | void *host_addr;
|
608 | 54936004 | bellard | |
609 | c8a706fe | pbrook | mmap_lock(); |
610 | f19412a2 | aurel32 | |
611 | f19412a2 | aurel32 | if (flags & MREMAP_FIXED)
|
612 | 3af72a4d | blueswir1 | host_addr = (void *) syscall(__NR_mremap, g2h(old_addr),
|
613 | 3af72a4d | blueswir1 | old_size, new_size, |
614 | 3af72a4d | blueswir1 | flags, |
615 | 3af72a4d | blueswir1 | new_addr); |
616 | f19412a2 | aurel32 | else if (flags & MREMAP_MAYMOVE) { |
617 | f19412a2 | aurel32 | abi_ulong mmap_start; |
618 | f19412a2 | aurel32 | |
619 | f19412a2 | aurel32 | mmap_start = mmap_find_vma(0, new_size);
|
620 | f19412a2 | aurel32 | |
621 | f19412a2 | aurel32 | if (mmap_start == -1) { |
622 | f19412a2 | aurel32 | errno = ENOMEM; |
623 | f19412a2 | aurel32 | host_addr = MAP_FAILED; |
624 | f19412a2 | aurel32 | } else
|
625 | 3af72a4d | blueswir1 | host_addr = (void *) syscall(__NR_mremap, g2h(old_addr),
|
626 | 3af72a4d | blueswir1 | old_size, new_size, |
627 | 3af72a4d | blueswir1 | flags | MREMAP_FIXED, |
628 | 3af72a4d | blueswir1 | g2h(mmap_start)); |
629 | 3af72a4d | blueswir1 | } else {
|
630 | f19412a2 | aurel32 | host_addr = mremap(g2h(old_addr), old_size, new_size, flags); |
631 | f19412a2 | aurel32 | /* Check if address fits target address space */
|
632 | f19412a2 | aurel32 | if ((unsigned long)host_addr + new_size > (abi_ulong)-1) { |
633 | f19412a2 | aurel32 | /* Revert mremap() changes */
|
634 | f19412a2 | aurel32 | host_addr = mremap(g2h(old_addr), new_size, old_size, flags); |
635 | f19412a2 | aurel32 | errno = ENOMEM; |
636 | f19412a2 | aurel32 | host_addr = MAP_FAILED; |
637 | f19412a2 | aurel32 | } |
638 | f19412a2 | aurel32 | } |
639 | f19412a2 | aurel32 | |
640 | f19412a2 | aurel32 | if (host_addr == MAP_FAILED) {
|
641 | c8a706fe | pbrook | new_addr = -1;
|
642 | c8a706fe | pbrook | } else {
|
643 | c8a706fe | pbrook | new_addr = h2g(host_addr); |
644 | c8a706fe | pbrook | prot = page_get_flags(old_addr); |
645 | c8a706fe | pbrook | page_set_flags(old_addr, old_addr + old_size, 0);
|
646 | c8a706fe | pbrook | page_set_flags(new_addr, new_addr + new_size, prot | PAGE_VALID); |
647 | c8a706fe | pbrook | } |
648 | c8a706fe | pbrook | mmap_unlock(); |
649 | 54936004 | bellard | return new_addr;
|
650 | 54936004 | bellard | } |
651 | 54936004 | bellard | |
652 | 992f48a0 | blueswir1 | int target_msync(abi_ulong start, abi_ulong len, int flags) |
653 | 54936004 | bellard | { |
654 | 992f48a0 | blueswir1 | abi_ulong end; |
655 | 54936004 | bellard | |
656 | 54936004 | bellard | if (start & ~TARGET_PAGE_MASK)
|
657 | 54936004 | bellard | return -EINVAL;
|
658 | 54936004 | bellard | len = TARGET_PAGE_ALIGN(len); |
659 | 54936004 | bellard | end = start + len; |
660 | d418c81e | bellard | if (end < start)
|
661 | d418c81e | bellard | return -EINVAL;
|
662 | d418c81e | bellard | if (end == start)
|
663 | d418c81e | bellard | return 0; |
664 | 3b46e624 | ths | |
665 | 83fb7adf | bellard | start &= qemu_host_page_mask; |
666 | 53a5960a | pbrook | return msync(g2h(start), end - start, flags);
|
667 | 54936004 | bellard | } |