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