root / bsd-user / mmap.c @ 12381085
History | View | Annotate | Download (16 kB)
1 | 84778508 | blueswir1 | /*
|
---|---|---|---|
2 | 84778508 | blueswir1 | * mmap support for qemu
|
3 | 84778508 | blueswir1 | *
|
4 | 84778508 | blueswir1 | * Copyright (c) 2003 - 2008 Fabrice Bellard
|
5 | 84778508 | blueswir1 | *
|
6 | 84778508 | blueswir1 | * This program is free software; you can redistribute it and/or modify
|
7 | 84778508 | blueswir1 | * it under the terms of the GNU General Public License as published by
|
8 | 84778508 | blueswir1 | * the Free Software Foundation; either version 2 of the License, or
|
9 | 84778508 | blueswir1 | * (at your option) any later version.
|
10 | 84778508 | blueswir1 | *
|
11 | 84778508 | blueswir1 | * This program is distributed in the hope that it will be useful,
|
12 | 84778508 | blueswir1 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 84778508 | blueswir1 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 | 84778508 | blueswir1 | * GNU General Public License for more details.
|
15 | 84778508 | blueswir1 | *
|
16 | 84778508 | blueswir1 | * 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 | 84778508 | blueswir1 | */
|
19 | 84778508 | blueswir1 | #include <stdlib.h> |
20 | 84778508 | blueswir1 | #include <stdio.h> |
21 | 84778508 | blueswir1 | #include <stdarg.h> |
22 | 84778508 | blueswir1 | #include <string.h> |
23 | 84778508 | blueswir1 | #include <unistd.h> |
24 | 84778508 | blueswir1 | #include <errno.h> |
25 | 84778508 | blueswir1 | #include <sys/mman.h> |
26 | 84778508 | blueswir1 | |
27 | 84778508 | blueswir1 | #include "qemu.h" |
28 | 84778508 | blueswir1 | #include "qemu-common.h" |
29 | 6c173b3c | blueswir1 | #include "bsd-mman.h" |
30 | 84778508 | blueswir1 | |
31 | 84778508 | blueswir1 | //#define DEBUG_MMAP
|
32 | 84778508 | blueswir1 | |
33 | 2f7bb878 | Juan Quintela | #if defined(CONFIG_USE_NPTL)
|
34 | 84778508 | blueswir1 | pthread_mutex_t mmap_mutex; |
35 | 84778508 | blueswir1 | static int __thread mmap_lock_count; |
36 | 84778508 | blueswir1 | |
37 | 84778508 | blueswir1 | void mmap_lock(void) |
38 | 84778508 | blueswir1 | { |
39 | 84778508 | blueswir1 | if (mmap_lock_count++ == 0) { |
40 | 84778508 | blueswir1 | pthread_mutex_lock(&mmap_mutex); |
41 | 84778508 | blueswir1 | } |
42 | 84778508 | blueswir1 | } |
43 | 84778508 | blueswir1 | |
44 | 84778508 | blueswir1 | void mmap_unlock(void) |
45 | 84778508 | blueswir1 | { |
46 | 84778508 | blueswir1 | if (--mmap_lock_count == 0) { |
47 | 84778508 | blueswir1 | pthread_mutex_unlock(&mmap_mutex); |
48 | 84778508 | blueswir1 | } |
49 | 84778508 | blueswir1 | } |
50 | 84778508 | blueswir1 | |
51 | 84778508 | blueswir1 | /* Grab lock to make sure things are in a consistent state after fork(). */
|
52 | 84778508 | blueswir1 | void mmap_fork_start(void) |
53 | 84778508 | blueswir1 | { |
54 | 84778508 | blueswir1 | if (mmap_lock_count)
|
55 | 84778508 | blueswir1 | abort(); |
56 | 84778508 | blueswir1 | pthread_mutex_lock(&mmap_mutex); |
57 | 84778508 | blueswir1 | } |
58 | 84778508 | blueswir1 | |
59 | 84778508 | blueswir1 | void mmap_fork_end(int child) |
60 | 84778508 | blueswir1 | { |
61 | 84778508 | blueswir1 | if (child)
|
62 | 84778508 | blueswir1 | pthread_mutex_init(&mmap_mutex, NULL);
|
63 | 84778508 | blueswir1 | else
|
64 | 84778508 | blueswir1 | pthread_mutex_unlock(&mmap_mutex); |
65 | 84778508 | blueswir1 | } |
66 | 84778508 | blueswir1 | #else
|
67 | 84778508 | blueswir1 | /* We aren't threadsafe to start with, so no need to worry about locking. */
|
68 | 84778508 | blueswir1 | void mmap_lock(void) |
69 | 84778508 | blueswir1 | { |
70 | 84778508 | blueswir1 | } |
71 | 84778508 | blueswir1 | |
72 | 84778508 | blueswir1 | void mmap_unlock(void) |
73 | 84778508 | blueswir1 | { |
74 | 84778508 | blueswir1 | } |
75 | 84778508 | blueswir1 | #endif
|
76 | 84778508 | blueswir1 | |
77 | 84778508 | blueswir1 | void *qemu_vmalloc(size_t size)
|
78 | 84778508 | blueswir1 | { |
79 | 84778508 | blueswir1 | void *p;
|
80 | 84778508 | blueswir1 | unsigned long addr; |
81 | 84778508 | blueswir1 | mmap_lock(); |
82 | 84778508 | blueswir1 | /* Use map and mark the pages as used. */
|
83 | 84778508 | blueswir1 | p = mmap(NULL, size, PROT_READ | PROT_WRITE,
|
84 | 84778508 | blueswir1 | MAP_PRIVATE | MAP_ANON, -1, 0); |
85 | 84778508 | blueswir1 | |
86 | 84778508 | blueswir1 | addr = (unsigned long)p; |
87 | 84778508 | blueswir1 | if (addr == (target_ulong) addr) {
|
88 | 84778508 | blueswir1 | /* Allocated region overlaps guest address space.
|
89 | 84778508 | blueswir1 | This may recurse. */
|
90 | 84778508 | blueswir1 | page_set_flags(addr & TARGET_PAGE_MASK, TARGET_PAGE_ALIGN(addr + size), |
91 | 84778508 | blueswir1 | PAGE_RESERVED); |
92 | 84778508 | blueswir1 | } |
93 | 84778508 | blueswir1 | |
94 | 84778508 | blueswir1 | mmap_unlock(); |
95 | 84778508 | blueswir1 | return p;
|
96 | 84778508 | blueswir1 | } |
97 | 84778508 | blueswir1 | |
98 | 84778508 | blueswir1 | void *qemu_malloc(size_t size)
|
99 | 84778508 | blueswir1 | { |
100 | 84778508 | blueswir1 | char * p;
|
101 | 84778508 | blueswir1 | size += 16;
|
102 | 84778508 | blueswir1 | p = qemu_vmalloc(size); |
103 | 84778508 | blueswir1 | *(size_t *)p = size; |
104 | 84778508 | blueswir1 | return p + 16; |
105 | 84778508 | blueswir1 | } |
106 | 84778508 | blueswir1 | |
107 | 84778508 | blueswir1 | /* We use map, which is always zero initialized. */
|
108 | 84778508 | blueswir1 | void * qemu_mallocz(size_t size)
|
109 | 84778508 | blueswir1 | { |
110 | 84778508 | blueswir1 | return qemu_malloc(size);
|
111 | 84778508 | blueswir1 | } |
112 | 84778508 | blueswir1 | |
113 | 84778508 | blueswir1 | void qemu_free(void *ptr) |
114 | 84778508 | blueswir1 | { |
115 | 84778508 | blueswir1 | /* FIXME: We should unmark the reserved pages here. However this gets
|
116 | 84778508 | blueswir1 | complicated when one target page spans multiple host pages, so we
|
117 | 84778508 | blueswir1 | don't bother. */
|
118 | 84778508 | blueswir1 | size_t *p; |
119 | 84778508 | blueswir1 | p = (size_t *)((char *)ptr - 16); |
120 | 84778508 | blueswir1 | munmap(p, *p); |
121 | 84778508 | blueswir1 | } |
122 | 84778508 | blueswir1 | |
123 | 004c9ef4 | blueswir1 | void *qemu_realloc(void *ptr, size_t size) |
124 | 004c9ef4 | blueswir1 | { |
125 | 004c9ef4 | blueswir1 | size_t old_size, copy; |
126 | 004c9ef4 | blueswir1 | void *new_ptr;
|
127 | 004c9ef4 | blueswir1 | |
128 | baa8c602 | malc | if (!ptr)
|
129 | baa8c602 | malc | return qemu_malloc(size);
|
130 | 004c9ef4 | blueswir1 | old_size = *(size_t *)((char *)ptr - 16); |
131 | 004c9ef4 | blueswir1 | copy = old_size < size ? old_size : size; |
132 | 004c9ef4 | blueswir1 | new_ptr = qemu_malloc(size); |
133 | 004c9ef4 | blueswir1 | memcpy(new_ptr, ptr, copy); |
134 | 004c9ef4 | blueswir1 | qemu_free(ptr); |
135 | 004c9ef4 | blueswir1 | return new_ptr;
|
136 | 004c9ef4 | blueswir1 | } |
137 | 004c9ef4 | blueswir1 | |
138 | 84778508 | blueswir1 | /* NOTE: all the constants are the HOST ones, but addresses are target. */
|
139 | 84778508 | blueswir1 | int target_mprotect(abi_ulong start, abi_ulong len, int prot) |
140 | 84778508 | blueswir1 | { |
141 | 84778508 | blueswir1 | abi_ulong end, host_start, host_end, addr; |
142 | 84778508 | blueswir1 | int prot1, ret;
|
143 | 84778508 | blueswir1 | |
144 | 84778508 | blueswir1 | #ifdef DEBUG_MMAP
|
145 | 84778508 | blueswir1 | printf("mprotect: start=0x" TARGET_FMT_lx
|
146 | 84778508 | blueswir1 | " len=0x" TARGET_FMT_lx " prot=%c%c%c\n", start, len, |
147 | 84778508 | blueswir1 | prot & PROT_READ ? 'r' : '-', |
148 | 84778508 | blueswir1 | prot & PROT_WRITE ? 'w' : '-', |
149 | 84778508 | blueswir1 | prot & PROT_EXEC ? 'x' : '-'); |
150 | 84778508 | blueswir1 | #endif
|
151 | 84778508 | blueswir1 | |
152 | 84778508 | blueswir1 | if ((start & ~TARGET_PAGE_MASK) != 0) |
153 | 84778508 | blueswir1 | return -EINVAL;
|
154 | 84778508 | blueswir1 | len = TARGET_PAGE_ALIGN(len); |
155 | 84778508 | blueswir1 | end = start + len; |
156 | 84778508 | blueswir1 | if (end < start)
|
157 | 84778508 | blueswir1 | return -EINVAL;
|
158 | 84778508 | blueswir1 | prot &= PROT_READ | PROT_WRITE | PROT_EXEC; |
159 | 84778508 | blueswir1 | if (len == 0) |
160 | 84778508 | blueswir1 | return 0; |
161 | 84778508 | blueswir1 | |
162 | 84778508 | blueswir1 | mmap_lock(); |
163 | 84778508 | blueswir1 | host_start = start & qemu_host_page_mask; |
164 | 84778508 | blueswir1 | host_end = HOST_PAGE_ALIGN(end); |
165 | 84778508 | blueswir1 | if (start > host_start) {
|
166 | 84778508 | blueswir1 | /* handle host page containing start */
|
167 | 84778508 | blueswir1 | prot1 = prot; |
168 | 84778508 | blueswir1 | for(addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {
|
169 | 84778508 | blueswir1 | prot1 |= page_get_flags(addr); |
170 | 84778508 | blueswir1 | } |
171 | 84778508 | blueswir1 | if (host_end == host_start + qemu_host_page_size) {
|
172 | 84778508 | blueswir1 | for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
|
173 | 84778508 | blueswir1 | prot1 |= page_get_flags(addr); |
174 | 84778508 | blueswir1 | } |
175 | 84778508 | blueswir1 | end = host_end; |
176 | 84778508 | blueswir1 | } |
177 | 84778508 | blueswir1 | ret = mprotect(g2h(host_start), qemu_host_page_size, prot1 & PAGE_BITS); |
178 | 84778508 | blueswir1 | if (ret != 0) |
179 | 84778508 | blueswir1 | goto error;
|
180 | 84778508 | blueswir1 | host_start += qemu_host_page_size; |
181 | 84778508 | blueswir1 | } |
182 | 84778508 | blueswir1 | if (end < host_end) {
|
183 | 84778508 | blueswir1 | prot1 = prot; |
184 | 84778508 | blueswir1 | for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
|
185 | 84778508 | blueswir1 | prot1 |= page_get_flags(addr); |
186 | 84778508 | blueswir1 | } |
187 | 84778508 | blueswir1 | ret = mprotect(g2h(host_end - qemu_host_page_size), qemu_host_page_size, |
188 | 84778508 | blueswir1 | prot1 & PAGE_BITS); |
189 | 84778508 | blueswir1 | if (ret != 0) |
190 | 84778508 | blueswir1 | goto error;
|
191 | 84778508 | blueswir1 | host_end -= qemu_host_page_size; |
192 | 84778508 | blueswir1 | } |
193 | 84778508 | blueswir1 | |
194 | 84778508 | blueswir1 | /* handle the pages in the middle */
|
195 | 84778508 | blueswir1 | if (host_start < host_end) {
|
196 | 84778508 | blueswir1 | ret = mprotect(g2h(host_start), host_end - host_start, prot); |
197 | 84778508 | blueswir1 | if (ret != 0) |
198 | 84778508 | blueswir1 | goto error;
|
199 | 84778508 | blueswir1 | } |
200 | 84778508 | blueswir1 | page_set_flags(start, start + len, prot | PAGE_VALID); |
201 | 84778508 | blueswir1 | mmap_unlock(); |
202 | 84778508 | blueswir1 | return 0; |
203 | 84778508 | blueswir1 | error:
|
204 | 84778508 | blueswir1 | mmap_unlock(); |
205 | 84778508 | blueswir1 | return ret;
|
206 | 84778508 | blueswir1 | } |
207 | 84778508 | blueswir1 | |
208 | 84778508 | blueswir1 | /* map an incomplete host page */
|
209 | 84778508 | blueswir1 | static int mmap_frag(abi_ulong real_start, |
210 | 84778508 | blueswir1 | abi_ulong start, abi_ulong end, |
211 | 84778508 | blueswir1 | int prot, int flags, int fd, abi_ulong offset) |
212 | 84778508 | blueswir1 | { |
213 | 84778508 | blueswir1 | abi_ulong real_end, addr; |
214 | 84778508 | blueswir1 | void *host_start;
|
215 | 84778508 | blueswir1 | int prot1, prot_new;
|
216 | 84778508 | blueswir1 | |
217 | 84778508 | blueswir1 | real_end = real_start + qemu_host_page_size; |
218 | 84778508 | blueswir1 | host_start = g2h(real_start); |
219 | 84778508 | blueswir1 | |
220 | 84778508 | blueswir1 | /* get the protection of the target pages outside the mapping */
|
221 | 84778508 | blueswir1 | prot1 = 0;
|
222 | 84778508 | blueswir1 | for(addr = real_start; addr < real_end; addr++) {
|
223 | 84778508 | blueswir1 | if (addr < start || addr >= end)
|
224 | 84778508 | blueswir1 | prot1 |= page_get_flags(addr); |
225 | 84778508 | blueswir1 | } |
226 | 84778508 | blueswir1 | |
227 | 84778508 | blueswir1 | if (prot1 == 0) { |
228 | 84778508 | blueswir1 | /* no page was there, so we allocate one */
|
229 | 84778508 | blueswir1 | void *p = mmap(host_start, qemu_host_page_size, prot,
|
230 | 84778508 | blueswir1 | flags | MAP_ANON, -1, 0); |
231 | 84778508 | blueswir1 | if (p == MAP_FAILED)
|
232 | 84778508 | blueswir1 | return -1; |
233 | 84778508 | blueswir1 | prot1 = prot; |
234 | 84778508 | blueswir1 | } |
235 | 84778508 | blueswir1 | prot1 &= PAGE_BITS; |
236 | 84778508 | blueswir1 | |
237 | 84778508 | blueswir1 | prot_new = prot | prot1; |
238 | 84778508 | blueswir1 | if (!(flags & MAP_ANON)) {
|
239 | 84778508 | blueswir1 | /* msync() won't work here, so we return an error if write is
|
240 | 84778508 | blueswir1 | possible while it is a shared mapping */
|
241 | 6c173b3c | blueswir1 | if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED &&
|
242 | 84778508 | blueswir1 | (prot & PROT_WRITE)) |
243 | 84778508 | blueswir1 | return -EINVAL;
|
244 | 84778508 | blueswir1 | |
245 | 84778508 | blueswir1 | /* adjust protection to be able to read */
|
246 | 84778508 | blueswir1 | if (!(prot1 & PROT_WRITE))
|
247 | 84778508 | blueswir1 | mprotect(host_start, qemu_host_page_size, prot1 | PROT_WRITE); |
248 | 84778508 | blueswir1 | |
249 | 84778508 | blueswir1 | /* read the corresponding file data */
|
250 | 84778508 | blueswir1 | pread(fd, g2h(start), end - start, offset); |
251 | 84778508 | blueswir1 | |
252 | 84778508 | blueswir1 | /* put final protection */
|
253 | 84778508 | blueswir1 | if (prot_new != (prot1 | PROT_WRITE))
|
254 | 84778508 | blueswir1 | mprotect(host_start, qemu_host_page_size, prot_new); |
255 | 84778508 | blueswir1 | } else {
|
256 | 84778508 | blueswir1 | /* just update the protection */
|
257 | 84778508 | blueswir1 | if (prot_new != prot1) {
|
258 | 84778508 | blueswir1 | mprotect(host_start, qemu_host_page_size, prot_new); |
259 | 84778508 | blueswir1 | } |
260 | 84778508 | blueswir1 | } |
261 | 84778508 | blueswir1 | return 0; |
262 | 84778508 | blueswir1 | } |
263 | 84778508 | blueswir1 | |
264 | 84778508 | blueswir1 | #if defined(__CYGWIN__)
|
265 | 84778508 | blueswir1 | /* Cygwin doesn't have a whole lot of address space. */
|
266 | 84778508 | blueswir1 | static abi_ulong mmap_next_start = 0x18000000; |
267 | 84778508 | blueswir1 | #else
|
268 | 84778508 | blueswir1 | static abi_ulong mmap_next_start = 0x40000000; |
269 | 84778508 | blueswir1 | #endif
|
270 | 84778508 | blueswir1 | |
271 | 84778508 | blueswir1 | unsigned long last_brk; |
272 | 84778508 | blueswir1 | |
273 | 84778508 | blueswir1 | /* find a free memory area of size 'size'. The search starts at
|
274 | 84778508 | blueswir1 | 'start'. If 'start' == 0, then a default start address is used.
|
275 | 84778508 | blueswir1 | Return -1 if error.
|
276 | 84778508 | blueswir1 | */
|
277 | 84778508 | blueswir1 | /* page_init() marks pages used by the host as reserved to be sure not
|
278 | 84778508 | blueswir1 | to use them. */
|
279 | 84778508 | blueswir1 | static abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size)
|
280 | 84778508 | blueswir1 | { |
281 | 84778508 | blueswir1 | abi_ulong addr, addr1, addr_start; |
282 | 84778508 | blueswir1 | int prot;
|
283 | 84778508 | blueswir1 | unsigned long new_brk; |
284 | 84778508 | blueswir1 | |
285 | 84778508 | blueswir1 | new_brk = (unsigned long)sbrk(0); |
286 | 84778508 | blueswir1 | if (last_brk && last_brk < new_brk && last_brk == (target_ulong)last_brk) {
|
287 | 84778508 | blueswir1 | /* This is a hack to catch the host allocating memory with brk().
|
288 | 84778508 | blueswir1 | If it uses mmap then we loose.
|
289 | 84778508 | blueswir1 | FIXME: We really want to avoid the host allocating memory in
|
290 | 84778508 | blueswir1 | the first place, and maybe leave some slack to avoid switching
|
291 | 84778508 | blueswir1 | to mmap. */
|
292 | 84778508 | blueswir1 | page_set_flags(last_brk & TARGET_PAGE_MASK, |
293 | 84778508 | blueswir1 | TARGET_PAGE_ALIGN(new_brk), |
294 | 84778508 | blueswir1 | PAGE_RESERVED); |
295 | 84778508 | blueswir1 | } |
296 | 84778508 | blueswir1 | last_brk = new_brk; |
297 | 84778508 | blueswir1 | |
298 | 84778508 | blueswir1 | size = HOST_PAGE_ALIGN(size); |
299 | 84778508 | blueswir1 | start = start & qemu_host_page_mask; |
300 | 84778508 | blueswir1 | addr = start; |
301 | 84778508 | blueswir1 | if (addr == 0) |
302 | 84778508 | blueswir1 | addr = mmap_next_start; |
303 | 84778508 | blueswir1 | addr_start = addr; |
304 | 84778508 | blueswir1 | for(;;) {
|
305 | 84778508 | blueswir1 | prot = 0;
|
306 | 84778508 | blueswir1 | for(addr1 = addr; addr1 < (addr + size); addr1 += TARGET_PAGE_SIZE) {
|
307 | 84778508 | blueswir1 | prot |= page_get_flags(addr1); |
308 | 84778508 | blueswir1 | } |
309 | 84778508 | blueswir1 | if (prot == 0) |
310 | 84778508 | blueswir1 | break;
|
311 | 84778508 | blueswir1 | addr += qemu_host_page_size; |
312 | 84778508 | blueswir1 | /* we found nothing */
|
313 | 84778508 | blueswir1 | if (addr == addr_start)
|
314 | 84778508 | blueswir1 | return (abi_ulong)-1; |
315 | 84778508 | blueswir1 | } |
316 | 84778508 | blueswir1 | if (start == 0) |
317 | 84778508 | blueswir1 | mmap_next_start = addr + size; |
318 | 84778508 | blueswir1 | return addr;
|
319 | 84778508 | blueswir1 | } |
320 | 84778508 | blueswir1 | |
321 | 84778508 | blueswir1 | /* NOTE: all the constants are the HOST ones */
|
322 | 84778508 | blueswir1 | abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
|
323 | 84778508 | blueswir1 | int flags, int fd, abi_ulong offset) |
324 | 84778508 | blueswir1 | { |
325 | 84778508 | blueswir1 | abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len; |
326 | 84778508 | blueswir1 | unsigned long host_start; |
327 | 84778508 | blueswir1 | |
328 | 84778508 | blueswir1 | mmap_lock(); |
329 | 84778508 | blueswir1 | #ifdef DEBUG_MMAP
|
330 | 84778508 | blueswir1 | { |
331 | 84778508 | blueswir1 | printf("mmap: start=0x" TARGET_FMT_lx
|
332 | 84778508 | blueswir1 | " len=0x" TARGET_FMT_lx " prot=%c%c%c flags=", |
333 | 84778508 | blueswir1 | start, len, |
334 | 84778508 | blueswir1 | prot & PROT_READ ? 'r' : '-', |
335 | 84778508 | blueswir1 | prot & PROT_WRITE ? 'w' : '-', |
336 | 84778508 | blueswir1 | prot & PROT_EXEC ? 'x' : '-'); |
337 | 84778508 | blueswir1 | if (flags & MAP_FIXED)
|
338 | 84778508 | blueswir1 | printf("MAP_FIXED ");
|
339 | 84778508 | blueswir1 | if (flags & MAP_ANON)
|
340 | 84778508 | blueswir1 | printf("MAP_ANON ");
|
341 | 6c173b3c | blueswir1 | switch(flags & TARGET_BSD_MAP_FLAGMASK) {
|
342 | 84778508 | blueswir1 | case MAP_PRIVATE:
|
343 | 84778508 | blueswir1 | printf("MAP_PRIVATE ");
|
344 | 84778508 | blueswir1 | break;
|
345 | 84778508 | blueswir1 | case MAP_SHARED:
|
346 | 84778508 | blueswir1 | printf("MAP_SHARED ");
|
347 | 84778508 | blueswir1 | break;
|
348 | 84778508 | blueswir1 | default:
|
349 | 6c173b3c | blueswir1 | printf("[MAP_FLAGMASK=0x%x] ", flags & TARGET_BSD_MAP_FLAGMASK);
|
350 | 84778508 | blueswir1 | break;
|
351 | 84778508 | blueswir1 | } |
352 | 84778508 | blueswir1 | printf("fd=%d offset=" TARGET_FMT_lx "\n", fd, offset); |
353 | 84778508 | blueswir1 | } |
354 | 84778508 | blueswir1 | #endif
|
355 | 84778508 | blueswir1 | |
356 | 84778508 | blueswir1 | if (offset & ~TARGET_PAGE_MASK) {
|
357 | 84778508 | blueswir1 | errno = EINVAL; |
358 | 84778508 | blueswir1 | goto fail;
|
359 | 84778508 | blueswir1 | } |
360 | 84778508 | blueswir1 | |
361 | 84778508 | blueswir1 | len = TARGET_PAGE_ALIGN(len); |
362 | 84778508 | blueswir1 | if (len == 0) |
363 | 84778508 | blueswir1 | goto the_end;
|
364 | 84778508 | blueswir1 | real_start = start & qemu_host_page_mask; |
365 | 84778508 | blueswir1 | |
366 | 84778508 | blueswir1 | if (!(flags & MAP_FIXED)) {
|
367 | 84778508 | blueswir1 | abi_ulong mmap_start; |
368 | 84778508 | blueswir1 | void *p;
|
369 | 84778508 | blueswir1 | host_offset = offset & qemu_host_page_mask; |
370 | 84778508 | blueswir1 | host_len = len + offset - host_offset; |
371 | 84778508 | blueswir1 | host_len = HOST_PAGE_ALIGN(host_len); |
372 | 84778508 | blueswir1 | mmap_start = mmap_find_vma(real_start, host_len); |
373 | 84778508 | blueswir1 | if (mmap_start == (abi_ulong)-1) { |
374 | 84778508 | blueswir1 | errno = ENOMEM; |
375 | 84778508 | blueswir1 | goto fail;
|
376 | 84778508 | blueswir1 | } |
377 | 84778508 | blueswir1 | /* Note: we prefer to control the mapping address. It is
|
378 | 84778508 | blueswir1 | especially important if qemu_host_page_size >
|
379 | 84778508 | blueswir1 | qemu_real_host_page_size */
|
380 | 84778508 | blueswir1 | p = mmap(g2h(mmap_start), |
381 | 84778508 | blueswir1 | host_len, prot, flags | MAP_FIXED, fd, host_offset); |
382 | 84778508 | blueswir1 | if (p == MAP_FAILED)
|
383 | 84778508 | blueswir1 | goto fail;
|
384 | 84778508 | blueswir1 | /* update start so that it points to the file position at 'offset' */
|
385 | 84778508 | blueswir1 | host_start = (unsigned long)p; |
386 | 84778508 | blueswir1 | if (!(flags & MAP_ANON))
|
387 | 84778508 | blueswir1 | host_start += offset - host_offset; |
388 | 84778508 | blueswir1 | start = h2g(host_start); |
389 | 84778508 | blueswir1 | } else {
|
390 | 84778508 | blueswir1 | int flg;
|
391 | 84778508 | blueswir1 | target_ulong addr; |
392 | 84778508 | blueswir1 | |
393 | 84778508 | blueswir1 | if (start & ~TARGET_PAGE_MASK) {
|
394 | 84778508 | blueswir1 | errno = EINVAL; |
395 | 84778508 | blueswir1 | goto fail;
|
396 | 84778508 | blueswir1 | } |
397 | 84778508 | blueswir1 | end = start + len; |
398 | 84778508 | blueswir1 | real_end = HOST_PAGE_ALIGN(end); |
399 | 84778508 | blueswir1 | |
400 | 84778508 | blueswir1 | for(addr = real_start; addr < real_end; addr += TARGET_PAGE_SIZE) {
|
401 | 84778508 | blueswir1 | flg = page_get_flags(addr); |
402 | 84778508 | blueswir1 | if (flg & PAGE_RESERVED) {
|
403 | 84778508 | blueswir1 | errno = ENXIO; |
404 | 84778508 | blueswir1 | goto fail;
|
405 | 84778508 | blueswir1 | } |
406 | 84778508 | blueswir1 | } |
407 | 84778508 | blueswir1 | |
408 | 84778508 | blueswir1 | /* worst case: we cannot map the file because the offset is not
|
409 | 84778508 | blueswir1 | aligned, so we read it */
|
410 | 84778508 | blueswir1 | if (!(flags & MAP_ANON) &&
|
411 | 84778508 | blueswir1 | (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) { |
412 | 84778508 | blueswir1 | /* msync() won't work here, so we return an error if write is
|
413 | 84778508 | blueswir1 | possible while it is a shared mapping */
|
414 | 6c173b3c | blueswir1 | if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED &&
|
415 | 84778508 | blueswir1 | (prot & PROT_WRITE)) { |
416 | 84778508 | blueswir1 | errno = EINVAL; |
417 | 84778508 | blueswir1 | goto fail;
|
418 | 84778508 | blueswir1 | } |
419 | 84778508 | blueswir1 | retaddr = target_mmap(start, len, prot | PROT_WRITE, |
420 | 84778508 | blueswir1 | MAP_FIXED | MAP_PRIVATE | MAP_ANON, |
421 | 84778508 | blueswir1 | -1, 0); |
422 | 84778508 | blueswir1 | if (retaddr == -1) |
423 | 84778508 | blueswir1 | goto fail;
|
424 | 84778508 | blueswir1 | pread(fd, g2h(start), len, offset); |
425 | 84778508 | blueswir1 | if (!(prot & PROT_WRITE)) {
|
426 | 84778508 | blueswir1 | ret = target_mprotect(start, len, prot); |
427 | 84778508 | blueswir1 | if (ret != 0) { |
428 | 84778508 | blueswir1 | start = ret; |
429 | 84778508 | blueswir1 | goto the_end;
|
430 | 84778508 | blueswir1 | } |
431 | 84778508 | blueswir1 | } |
432 | 84778508 | blueswir1 | goto the_end;
|
433 | 84778508 | blueswir1 | } |
434 | 84778508 | blueswir1 | |
435 | 84778508 | blueswir1 | /* handle the start of the mapping */
|
436 | 84778508 | blueswir1 | if (start > real_start) {
|
437 | 84778508 | blueswir1 | if (real_end == real_start + qemu_host_page_size) {
|
438 | 84778508 | blueswir1 | /* one single host page */
|
439 | 84778508 | blueswir1 | ret = mmap_frag(real_start, start, end, |
440 | 84778508 | blueswir1 | prot, flags, fd, offset); |
441 | 84778508 | blueswir1 | if (ret == -1) |
442 | 84778508 | blueswir1 | goto fail;
|
443 | 84778508 | blueswir1 | goto the_end1;
|
444 | 84778508 | blueswir1 | } |
445 | 84778508 | blueswir1 | ret = mmap_frag(real_start, start, real_start + qemu_host_page_size, |
446 | 84778508 | blueswir1 | prot, flags, fd, offset); |
447 | 84778508 | blueswir1 | if (ret == -1) |
448 | 84778508 | blueswir1 | goto fail;
|
449 | 84778508 | blueswir1 | real_start += qemu_host_page_size; |
450 | 84778508 | blueswir1 | } |
451 | 84778508 | blueswir1 | /* handle the end of the mapping */
|
452 | 84778508 | blueswir1 | if (end < real_end) {
|
453 | 84778508 | blueswir1 | ret = mmap_frag(real_end - qemu_host_page_size, |
454 | 84778508 | blueswir1 | real_end - qemu_host_page_size, real_end, |
455 | 84778508 | blueswir1 | prot, flags, fd, |
456 | 84778508 | blueswir1 | offset + real_end - qemu_host_page_size - start); |
457 | 84778508 | blueswir1 | if (ret == -1) |
458 | 84778508 | blueswir1 | goto fail;
|
459 | 84778508 | blueswir1 | real_end -= qemu_host_page_size; |
460 | 84778508 | blueswir1 | } |
461 | 84778508 | blueswir1 | |
462 | 84778508 | blueswir1 | /* map the middle (easier) */
|
463 | 84778508 | blueswir1 | if (real_start < real_end) {
|
464 | 84778508 | blueswir1 | void *p;
|
465 | 84778508 | blueswir1 | unsigned long offset1; |
466 | 84778508 | blueswir1 | if (flags & MAP_ANON)
|
467 | 84778508 | blueswir1 | offset1 = 0;
|
468 | 84778508 | blueswir1 | else
|
469 | 84778508 | blueswir1 | offset1 = offset + real_start - start; |
470 | 84778508 | blueswir1 | p = mmap(g2h(real_start), real_end - real_start, |
471 | 84778508 | blueswir1 | prot, flags, fd, offset1); |
472 | 84778508 | blueswir1 | if (p == MAP_FAILED)
|
473 | 84778508 | blueswir1 | goto fail;
|
474 | 84778508 | blueswir1 | } |
475 | 84778508 | blueswir1 | } |
476 | 84778508 | blueswir1 | the_end1:
|
477 | 84778508 | blueswir1 | page_set_flags(start, start + len, prot | PAGE_VALID); |
478 | 84778508 | blueswir1 | the_end:
|
479 | 84778508 | blueswir1 | #ifdef DEBUG_MMAP
|
480 | 84778508 | blueswir1 | printf("ret=0x" TARGET_FMT_lx "\n", start); |
481 | 84778508 | blueswir1 | page_dump(stdout); |
482 | 84778508 | blueswir1 | printf("\n");
|
483 | 84778508 | blueswir1 | #endif
|
484 | 84778508 | blueswir1 | mmap_unlock(); |
485 | 84778508 | blueswir1 | return start;
|
486 | 84778508 | blueswir1 | fail:
|
487 | 84778508 | blueswir1 | mmap_unlock(); |
488 | 84778508 | blueswir1 | return -1; |
489 | 84778508 | blueswir1 | } |
490 | 84778508 | blueswir1 | |
491 | 84778508 | blueswir1 | int target_munmap(abi_ulong start, abi_ulong len)
|
492 | 84778508 | blueswir1 | { |
493 | 84778508 | blueswir1 | abi_ulong end, real_start, real_end, addr; |
494 | 84778508 | blueswir1 | int prot, ret;
|
495 | 84778508 | blueswir1 | |
496 | 84778508 | blueswir1 | #ifdef DEBUG_MMAP
|
497 | 84778508 | blueswir1 | printf("munmap: start=0x%lx len=0x%lx\n", start, len);
|
498 | 84778508 | blueswir1 | #endif
|
499 | 84778508 | blueswir1 | if (start & ~TARGET_PAGE_MASK)
|
500 | 84778508 | blueswir1 | return -EINVAL;
|
501 | 84778508 | blueswir1 | len = TARGET_PAGE_ALIGN(len); |
502 | 84778508 | blueswir1 | if (len == 0) |
503 | 84778508 | blueswir1 | return -EINVAL;
|
504 | 84778508 | blueswir1 | mmap_lock(); |
505 | 84778508 | blueswir1 | end = start + len; |
506 | 84778508 | blueswir1 | real_start = start & qemu_host_page_mask; |
507 | 84778508 | blueswir1 | real_end = HOST_PAGE_ALIGN(end); |
508 | 84778508 | blueswir1 | |
509 | 84778508 | blueswir1 | if (start > real_start) {
|
510 | 84778508 | blueswir1 | /* handle host page containing start */
|
511 | 84778508 | blueswir1 | prot = 0;
|
512 | 84778508 | blueswir1 | for(addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) {
|
513 | 84778508 | blueswir1 | prot |= page_get_flags(addr); |
514 | 84778508 | blueswir1 | } |
515 | 84778508 | blueswir1 | if (real_end == real_start + qemu_host_page_size) {
|
516 | 84778508 | blueswir1 | for(addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
|
517 | 84778508 | blueswir1 | prot |= page_get_flags(addr); |
518 | 84778508 | blueswir1 | } |
519 | 84778508 | blueswir1 | end = real_end; |
520 | 84778508 | blueswir1 | } |
521 | 84778508 | blueswir1 | if (prot != 0) |
522 | 84778508 | blueswir1 | real_start += qemu_host_page_size; |
523 | 84778508 | blueswir1 | } |
524 | 84778508 | blueswir1 | if (end < real_end) {
|
525 | 84778508 | blueswir1 | prot = 0;
|
526 | 84778508 | blueswir1 | for(addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
|
527 | 84778508 | blueswir1 | prot |= page_get_flags(addr); |
528 | 84778508 | blueswir1 | } |
529 | 84778508 | blueswir1 | if (prot != 0) |
530 | 84778508 | blueswir1 | real_end -= qemu_host_page_size; |
531 | 84778508 | blueswir1 | } |
532 | 84778508 | blueswir1 | |
533 | 84778508 | blueswir1 | ret = 0;
|
534 | 84778508 | blueswir1 | /* unmap what we can */
|
535 | 84778508 | blueswir1 | if (real_start < real_end) {
|
536 | 84778508 | blueswir1 | ret = munmap(g2h(real_start), real_end - real_start); |
537 | 84778508 | blueswir1 | } |
538 | 84778508 | blueswir1 | |
539 | 84778508 | blueswir1 | if (ret == 0) |
540 | 84778508 | blueswir1 | page_set_flags(start, start + len, 0);
|
541 | 84778508 | blueswir1 | mmap_unlock(); |
542 | 84778508 | blueswir1 | return ret;
|
543 | 84778508 | blueswir1 | } |
544 | 84778508 | blueswir1 | |
545 | 84778508 | blueswir1 | int target_msync(abi_ulong start, abi_ulong len, int flags) |
546 | 84778508 | blueswir1 | { |
547 | 84778508 | blueswir1 | abi_ulong end; |
548 | 84778508 | blueswir1 | |
549 | 84778508 | blueswir1 | if (start & ~TARGET_PAGE_MASK)
|
550 | 84778508 | blueswir1 | return -EINVAL;
|
551 | 84778508 | blueswir1 | len = TARGET_PAGE_ALIGN(len); |
552 | 84778508 | blueswir1 | end = start + len; |
553 | 84778508 | blueswir1 | if (end < start)
|
554 | 84778508 | blueswir1 | return -EINVAL;
|
555 | 84778508 | blueswir1 | if (end == start)
|
556 | 84778508 | blueswir1 | return 0; |
557 | 84778508 | blueswir1 | |
558 | 84778508 | blueswir1 | start &= qemu_host_page_mask; |
559 | 84778508 | blueswir1 | return msync(g2h(start), end - start, flags);
|
560 | 84778508 | blueswir1 | } |