root / linux-user / mmap.c @ 459a4017
History | View | Annotate | Download (14.2 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 | 54936004 | bellard | * along with this program; if not, write to the Free Software
|
18 | 54936004 | bellard | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
19 | 54936004 | bellard | */
|
20 | 54936004 | bellard | #include <stdlib.h> |
21 | 54936004 | bellard | #include <stdio.h> |
22 | 54936004 | bellard | #include <stdarg.h> |
23 | 54936004 | bellard | #include <string.h> |
24 | 54936004 | bellard | #include <unistd.h> |
25 | 54936004 | bellard | #include <errno.h> |
26 | 54936004 | bellard | #include <sys/mman.h> |
27 | 54936004 | bellard | |
28 | 54936004 | bellard | #include "qemu.h" |
29 | 54936004 | bellard | |
30 | 54936004 | bellard | //#define DEBUG_MMAP
|
31 | 54936004 | bellard | |
32 | 53a5960a | pbrook | /* NOTE: all the constants are the HOST ones, but addresses are target. */
|
33 | 992f48a0 | blueswir1 | int target_mprotect(abi_ulong start, abi_ulong len, int prot) |
34 | 54936004 | bellard | { |
35 | 992f48a0 | blueswir1 | abi_ulong end, host_start, host_end, addr; |
36 | 54936004 | bellard | int prot1, ret;
|
37 | 54936004 | bellard | |
38 | 54936004 | bellard | #ifdef DEBUG_MMAP
|
39 | a5b85f79 | ths | printf("mprotect: start=0x" TARGET_FMT_lx
|
40 | 80210bcd | ths | "len=0x" TARGET_FMT_lx " prot=%c%c%c\n", start, len, |
41 | 54936004 | bellard | prot & PROT_READ ? 'r' : '-', |
42 | 54936004 | bellard | prot & PROT_WRITE ? 'w' : '-', |
43 | 54936004 | bellard | prot & PROT_EXEC ? 'x' : '-'); |
44 | 54936004 | bellard | #endif
|
45 | 54936004 | bellard | |
46 | 54936004 | bellard | if ((start & ~TARGET_PAGE_MASK) != 0) |
47 | 54936004 | bellard | return -EINVAL;
|
48 | 54936004 | bellard | len = TARGET_PAGE_ALIGN(len); |
49 | 54936004 | bellard | end = start + len; |
50 | 54936004 | bellard | if (end < start)
|
51 | 54936004 | bellard | return -EINVAL;
|
52 | 54936004 | bellard | if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
|
53 | 54936004 | bellard | return -EINVAL;
|
54 | 54936004 | bellard | if (len == 0) |
55 | 54936004 | bellard | return 0; |
56 | 3b46e624 | ths | |
57 | 83fb7adf | bellard | host_start = start & qemu_host_page_mask; |
58 | 54936004 | bellard | host_end = HOST_PAGE_ALIGN(end); |
59 | 54936004 | bellard | if (start > host_start) {
|
60 | 54936004 | bellard | /* handle host page containing start */
|
61 | 54936004 | bellard | prot1 = prot; |
62 | 54936004 | bellard | for(addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {
|
63 | 54936004 | bellard | prot1 |= page_get_flags(addr); |
64 | 54936004 | bellard | } |
65 | 83fb7adf | bellard | if (host_end == host_start + qemu_host_page_size) {
|
66 | d418c81e | bellard | for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
|
67 | d418c81e | bellard | prot1 |= page_get_flags(addr); |
68 | d418c81e | bellard | } |
69 | d418c81e | bellard | end = host_end; |
70 | d418c81e | bellard | } |
71 | 53a5960a | pbrook | ret = mprotect(g2h(host_start), qemu_host_page_size, prot1 & PAGE_BITS); |
72 | 54936004 | bellard | if (ret != 0) |
73 | 54936004 | bellard | return ret;
|
74 | 83fb7adf | bellard | host_start += qemu_host_page_size; |
75 | 54936004 | bellard | } |
76 | 54936004 | bellard | if (end < host_end) {
|
77 | 54936004 | bellard | prot1 = prot; |
78 | 54936004 | bellard | for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
|
79 | 54936004 | bellard | prot1 |= page_get_flags(addr); |
80 | 54936004 | bellard | } |
81 | 5fafdf24 | ths | ret = mprotect(g2h(host_end - qemu_host_page_size), qemu_host_page_size, |
82 | 54936004 | bellard | prot1 & PAGE_BITS); |
83 | 54936004 | bellard | if (ret != 0) |
84 | 54936004 | bellard | return ret;
|
85 | 83fb7adf | bellard | host_end -= qemu_host_page_size; |
86 | 54936004 | bellard | } |
87 | 3b46e624 | ths | |
88 | 54936004 | bellard | /* handle the pages in the middle */
|
89 | 54936004 | bellard | if (host_start < host_end) {
|
90 | 53a5960a | pbrook | ret = mprotect(g2h(host_start), host_end - host_start, prot); |
91 | 54936004 | bellard | if (ret != 0) |
92 | 54936004 | bellard | return ret;
|
93 | 54936004 | bellard | } |
94 | 54936004 | bellard | page_set_flags(start, start + len, prot | PAGE_VALID); |
95 | 54936004 | bellard | return 0; |
96 | 54936004 | bellard | } |
97 | 54936004 | bellard | |
98 | 54936004 | bellard | /* map an incomplete host page */
|
99 | 992f48a0 | blueswir1 | static int mmap_frag(abi_ulong real_start, |
100 | 992f48a0 | blueswir1 | abi_ulong start, abi_ulong end, |
101 | 992f48a0 | blueswir1 | int prot, int flags, int fd, abi_ulong offset) |
102 | 54936004 | bellard | { |
103 | 80210bcd | ths | abi_ulong real_end, addr; |
104 | 53a5960a | pbrook | void *host_start;
|
105 | 54936004 | bellard | int prot1, prot_new;
|
106 | 54936004 | bellard | |
107 | 53a5960a | pbrook | real_end = real_start + qemu_host_page_size; |
108 | 53a5960a | pbrook | host_start = g2h(real_start); |
109 | 54936004 | bellard | |
110 | 54936004 | bellard | /* get the protection of the target pages outside the mapping */
|
111 | 54936004 | bellard | prot1 = 0;
|
112 | 53a5960a | pbrook | for(addr = real_start; addr < real_end; addr++) {
|
113 | 54936004 | bellard | if (addr < start || addr >= end)
|
114 | 54936004 | bellard | prot1 |= page_get_flags(addr); |
115 | 54936004 | bellard | } |
116 | 3b46e624 | ths | |
117 | 54936004 | bellard | if (prot1 == 0) { |
118 | 54936004 | bellard | /* no page was there, so we allocate one */
|
119 | 80210bcd | ths | void *p = mmap(host_start, qemu_host_page_size, prot,
|
120 | 80210bcd | ths | flags | MAP_ANONYMOUS, -1, 0); |
121 | 80210bcd | ths | if (p == MAP_FAILED)
|
122 | 80210bcd | ths | return -1; |
123 | 53a5960a | pbrook | prot1 = prot; |
124 | 54936004 | bellard | } |
125 | 54936004 | bellard | prot1 &= PAGE_BITS; |
126 | 54936004 | bellard | |
127 | 54936004 | bellard | prot_new = prot | prot1; |
128 | 54936004 | bellard | if (!(flags & MAP_ANONYMOUS)) {
|
129 | 54936004 | bellard | /* msync() won't work here, so we return an error if write is
|
130 | 54936004 | bellard | possible while it is a shared mapping */
|
131 | 54936004 | bellard | if ((flags & MAP_TYPE) == MAP_SHARED &&
|
132 | 54936004 | bellard | (prot & PROT_WRITE)) |
133 | 54936004 | bellard | return -EINVAL;
|
134 | 54936004 | bellard | |
135 | 54936004 | bellard | /* adjust protection to be able to read */
|
136 | 54936004 | bellard | if (!(prot1 & PROT_WRITE))
|
137 | 53a5960a | pbrook | mprotect(host_start, qemu_host_page_size, prot1 | PROT_WRITE); |
138 | 3b46e624 | ths | |
139 | 54936004 | bellard | /* read the corresponding file data */
|
140 | 53a5960a | pbrook | pread(fd, g2h(start), end - start, offset); |
141 | 3b46e624 | ths | |
142 | 54936004 | bellard | /* put final protection */
|
143 | 54936004 | bellard | if (prot_new != (prot1 | PROT_WRITE))
|
144 | 53a5960a | pbrook | mprotect(host_start, qemu_host_page_size, prot_new); |
145 | 54936004 | bellard | } else {
|
146 | 54936004 | bellard | /* just update the protection */
|
147 | 54936004 | bellard | if (prot_new != prot1) {
|
148 | 53a5960a | pbrook | mprotect(host_start, qemu_host_page_size, prot_new); |
149 | 54936004 | bellard | } |
150 | 54936004 | bellard | } |
151 | 54936004 | bellard | return 0; |
152 | 54936004 | bellard | } |
153 | 54936004 | bellard | |
154 | 54936004 | bellard | /* NOTE: all the constants are the HOST ones */
|
155 | 992f48a0 | blueswir1 | abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
|
156 | 992f48a0 | blueswir1 | int flags, int fd, abi_ulong offset) |
157 | 54936004 | bellard | { |
158 | 992f48a0 | blueswir1 | abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len; |
159 | a5b85f79 | ths | unsigned long host_start; |
160 | b8076a74 | bellard | #if defined(__alpha__) || defined(__sparc__) || defined(__x86_64__) || \
|
161 | 6b078dfd | ths | defined(__ia64) || defined(__mips__) |
162 | 992f48a0 | blueswir1 | static abi_ulong last_start = 0x40000000; |
163 | 53a5960a | pbrook | #elif defined(__CYGWIN__)
|
164 | 53a5960a | pbrook | /* Cygwin doesn't have a whole lot of address space. */
|
165 | 992f48a0 | blueswir1 | static abi_ulong last_start = 0x18000000; |
166 | 4f2ac237 | bellard | #endif
|
167 | 54936004 | bellard | |
168 | 54936004 | bellard | #ifdef DEBUG_MMAP
|
169 | 54936004 | bellard | { |
170 | a5b85f79 | ths | printf("mmap: start=0x" TARGET_FMT_lx
|
171 | 80210bcd | ths | " len=0x" TARGET_FMT_lx " prot=%c%c%c flags=", |
172 | 5fafdf24 | ths | start, len, |
173 | 54936004 | bellard | prot & PROT_READ ? 'r' : '-', |
174 | 54936004 | bellard | prot & PROT_WRITE ? 'w' : '-', |
175 | 54936004 | bellard | prot & PROT_EXEC ? 'x' : '-'); |
176 | 54936004 | bellard | if (flags & MAP_FIXED)
|
177 | 54936004 | bellard | printf("MAP_FIXED ");
|
178 | 54936004 | bellard | if (flags & MAP_ANONYMOUS)
|
179 | 54936004 | bellard | printf("MAP_ANON ");
|
180 | 54936004 | bellard | switch(flags & MAP_TYPE) {
|
181 | 54936004 | bellard | case MAP_PRIVATE:
|
182 | 54936004 | bellard | printf("MAP_PRIVATE ");
|
183 | 54936004 | bellard | break;
|
184 | 54936004 | bellard | case MAP_SHARED:
|
185 | 54936004 | bellard | printf("MAP_SHARED ");
|
186 | 54936004 | bellard | break;
|
187 | 54936004 | bellard | default:
|
188 | 54936004 | bellard | printf("[MAP_TYPE=0x%x] ", flags & MAP_TYPE);
|
189 | 54936004 | bellard | break;
|
190 | 54936004 | bellard | } |
191 | a5b85f79 | ths | printf("fd=%d offset=" TARGET_FMT_lx "\n", fd, offset); |
192 | 54936004 | bellard | } |
193 | 54936004 | bellard | #endif
|
194 | 54936004 | bellard | |
195 | e89f07d3 | pbrook | if (offset & ~TARGET_PAGE_MASK) {
|
196 | e89f07d3 | pbrook | errno = EINVAL; |
197 | e89f07d3 | pbrook | return -1; |
198 | e89f07d3 | pbrook | } |
199 | 54936004 | bellard | |
200 | 54936004 | bellard | len = TARGET_PAGE_ALIGN(len); |
201 | 54936004 | bellard | if (len == 0) |
202 | 54936004 | bellard | return start;
|
203 | 53a5960a | pbrook | real_start = start & qemu_host_page_mask; |
204 | 54936004 | bellard | |
205 | 54936004 | bellard | if (!(flags & MAP_FIXED)) {
|
206 | b8076a74 | bellard | #if defined(__alpha__) || defined(__sparc__) || defined(__x86_64__) || \
|
207 | 6b078dfd | ths | defined(__ia64) || defined(__mips__) || defined(__CYGWIN__) |
208 | 6b078dfd | ths | /* tell the kernel to search at the same place as i386 */
|
209 | 53a5960a | pbrook | if (real_start == 0) { |
210 | 53a5960a | pbrook | real_start = last_start; |
211 | 4f2ac237 | bellard | last_start += HOST_PAGE_ALIGN(len); |
212 | 4f2ac237 | bellard | } |
213 | 917f95fd | bellard | #endif
|
214 | 83fb7adf | bellard | host_offset = offset & qemu_host_page_mask; |
215 | 54936004 | bellard | host_len = len + offset - host_offset; |
216 | a5b85f79 | ths | |
217 | a5b85f79 | ths | if (qemu_host_page_size > qemu_real_host_page_size) {
|
218 | a5b85f79 | ths | /*
|
219 | a5b85f79 | ths | * The guest expects to see mmapped areas aligned to it's pagesize.
|
220 | a5b85f79 | ths | * If the host's real page size is smaller than the guest's, we need
|
221 | a5b85f79 | ths | * to fixup the maps. It is done by allocating a larger area,
|
222 | a5b85f79 | ths | * displacing the map (if needed) and finally chopping off the spare
|
223 | a5b85f79 | ths | * room at the edges.
|
224 | a5b85f79 | ths | */
|
225 | a5b85f79 | ths | |
226 | a5b85f79 | ths | /*
|
227 | a5b85f79 | ths | * We assume qemu_host_page_size is always the same as
|
228 | a5b85f79 | ths | * TARGET_PAGE_SIZE, see exec.c. qemu_real_host_page_size is the
|
229 | a5b85f79 | ths | * hosts real page size.
|
230 | a5b85f79 | ths | */
|
231 | 992f48a0 | blueswir1 | abi_ulong host_end; |
232 | a5b85f79 | ths | unsigned long host_aligned_start; |
233 | 80210bcd | ths | void *p;
|
234 | a5b85f79 | ths | |
235 | a5b85f79 | ths | host_len = HOST_PAGE_ALIGN(host_len + qemu_host_page_size |
236 | a5b85f79 | ths | - qemu_real_host_page_size); |
237 | 80210bcd | ths | p = mmap(real_start ? g2h(real_start) : NULL,
|
238 | 80210bcd | ths | host_len, prot, flags, fd, host_offset); |
239 | 80210bcd | ths | if (p == MAP_FAILED)
|
240 | a5b85f79 | ths | return -1; |
241 | a5b85f79 | ths | |
242 | 80210bcd | ths | host_start = (unsigned long)p; |
243 | a5b85f79 | ths | host_end = host_start + host_len; |
244 | a5b85f79 | ths | |
245 | a5b85f79 | ths | /* Find start and end, aligned to the targets pagesize with-in the
|
246 | a5b85f79 | ths | large mmaped area. */
|
247 | a5b85f79 | ths | host_aligned_start = TARGET_PAGE_ALIGN(host_start); |
248 | a5b85f79 | ths | if (!(flags & MAP_ANONYMOUS))
|
249 | a5b85f79 | ths | host_aligned_start += offset - host_offset; |
250 | a5b85f79 | ths | |
251 | a5b85f79 | ths | start = h2g(host_aligned_start); |
252 | a5b85f79 | ths | end = start + TARGET_PAGE_ALIGN(len); |
253 | a5b85f79 | ths | |
254 | a5b85f79 | ths | /* Chop off the leftovers, if any. */
|
255 | a5b85f79 | ths | if (host_aligned_start > host_start)
|
256 | a5b85f79 | ths | munmap((void *)host_start, host_aligned_start - host_start);
|
257 | a5b85f79 | ths | if (end < host_end)
|
258 | a5b85f79 | ths | munmap((void *)g2h(end), host_end - end);
|
259 | a5b85f79 | ths | |
260 | a5b85f79 | ths | goto the_end1;
|
261 | a5b85f79 | ths | } else {
|
262 | a5b85f79 | ths | /* if not fixed, no need to do anything */
|
263 | 80210bcd | ths | void *p = mmap(real_start ? g2h(real_start) : NULL, |
264 | 53a5960a | pbrook | host_len, prot, flags, fd, host_offset); |
265 | 80210bcd | ths | if (p == MAP_FAILED)
|
266 | a5b85f79 | ths | return -1; |
267 | 54936004 | bellard | /* update start so that it points to the file position at 'offset' */
|
268 | 80210bcd | ths | host_start = (unsigned long)p; |
269 | 5fafdf24 | ths | if (!(flags & MAP_ANONYMOUS))
|
270 | 53a5960a | pbrook | host_start += offset - host_offset; |
271 | 53a5960a | pbrook | start = h2g(host_start); |
272 | 54936004 | bellard | goto the_end1;
|
273 | 54936004 | bellard | } |
274 | 54936004 | bellard | } |
275 | 3b46e624 | ths | |
276 | e89f07d3 | pbrook | if (start & ~TARGET_PAGE_MASK) {
|
277 | e89f07d3 | pbrook | errno = EINVAL; |
278 | e89f07d3 | pbrook | return -1; |
279 | e89f07d3 | pbrook | } |
280 | 54936004 | bellard | end = start + len; |
281 | 53a5960a | pbrook | real_end = HOST_PAGE_ALIGN(end); |
282 | 54936004 | bellard | |
283 | 54936004 | bellard | /* worst case: we cannot map the file because the offset is not
|
284 | 54936004 | bellard | aligned, so we read it */
|
285 | 54936004 | bellard | if (!(flags & MAP_ANONYMOUS) &&
|
286 | 83fb7adf | bellard | (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) { |
287 | 54936004 | bellard | /* msync() won't work here, so we return an error if write is
|
288 | 54936004 | bellard | possible while it is a shared mapping */
|
289 | 54936004 | bellard | if ((flags & MAP_TYPE) == MAP_SHARED &&
|
290 | e89f07d3 | pbrook | (prot & PROT_WRITE)) { |
291 | e89f07d3 | pbrook | errno = EINVAL; |
292 | e89f07d3 | pbrook | return -1; |
293 | e89f07d3 | pbrook | } |
294 | 5fafdf24 | ths | retaddr = target_mmap(start, len, prot | PROT_WRITE, |
295 | 5fafdf24 | ths | MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, |
296 | 54936004 | bellard | -1, 0); |
297 | 54936004 | bellard | if (retaddr == -1) |
298 | a5b85f79 | ths | return -1; |
299 | 53a5960a | pbrook | pread(fd, g2h(start), len, offset); |
300 | 54936004 | bellard | if (!(prot & PROT_WRITE)) {
|
301 | 54936004 | bellard | ret = target_mprotect(start, len, prot); |
302 | 54936004 | bellard | if (ret != 0) |
303 | 54936004 | bellard | return ret;
|
304 | 54936004 | bellard | } |
305 | 54936004 | bellard | goto the_end;
|
306 | 54936004 | bellard | } |
307 | 54936004 | bellard | |
308 | 54936004 | bellard | /* handle the start of the mapping */
|
309 | 53a5960a | pbrook | if (start > real_start) {
|
310 | 53a5960a | pbrook | if (real_end == real_start + qemu_host_page_size) {
|
311 | 54936004 | bellard | /* one single host page */
|
312 | 53a5960a | pbrook | ret = mmap_frag(real_start, start, end, |
313 | 54936004 | bellard | prot, flags, fd, offset); |
314 | 54936004 | bellard | if (ret == -1) |
315 | 54936004 | bellard | return ret;
|
316 | 54936004 | bellard | goto the_end1;
|
317 | 54936004 | bellard | } |
318 | 53a5960a | pbrook | ret = mmap_frag(real_start, start, real_start + qemu_host_page_size, |
319 | 54936004 | bellard | prot, flags, fd, offset); |
320 | 54936004 | bellard | if (ret == -1) |
321 | 54936004 | bellard | return ret;
|
322 | 53a5960a | pbrook | real_start += qemu_host_page_size; |
323 | 54936004 | bellard | } |
324 | 54936004 | bellard | /* handle the end of the mapping */
|
325 | 53a5960a | pbrook | if (end < real_end) {
|
326 | 5fafdf24 | ths | ret = mmap_frag(real_end - qemu_host_page_size, |
327 | 53a5960a | pbrook | real_end - qemu_host_page_size, real_end, |
328 | 5fafdf24 | ths | prot, flags, fd, |
329 | 53a5960a | pbrook | offset + real_end - qemu_host_page_size - start); |
330 | 54936004 | bellard | if (ret == -1) |
331 | a5b85f79 | ths | return -1; |
332 | 53a5960a | pbrook | real_end -= qemu_host_page_size; |
333 | 54936004 | bellard | } |
334 | 3b46e624 | ths | |
335 | 54936004 | bellard | /* map the middle (easier) */
|
336 | 53a5960a | pbrook | if (real_start < real_end) {
|
337 | 80210bcd | ths | void *p;
|
338 | 4a585ccb | bellard | unsigned long offset1; |
339 | 80210bcd | ths | if (flags & MAP_ANONYMOUS)
|
340 | 80210bcd | ths | offset1 = 0;
|
341 | 80210bcd | ths | else
|
342 | 80210bcd | ths | offset1 = offset + real_start - start; |
343 | 80210bcd | ths | p = mmap(g2h(real_start), real_end - real_start, |
344 | 80210bcd | ths | prot, flags, fd, offset1); |
345 | 80210bcd | ths | if (p == MAP_FAILED)
|
346 | a5b85f79 | ths | return -1; |
347 | 54936004 | bellard | } |
348 | 54936004 | bellard | the_end1:
|
349 | 54936004 | bellard | page_set_flags(start, start + len, prot | PAGE_VALID); |
350 | 54936004 | bellard | the_end:
|
351 | 54936004 | bellard | #ifdef DEBUG_MMAP
|
352 | a5b85f79 | ths | printf("ret=0x%llx\n", start);
|
353 | 54936004 | bellard | page_dump(stdout); |
354 | 54936004 | bellard | printf("\n");
|
355 | 54936004 | bellard | #endif
|
356 | 54936004 | bellard | return start;
|
357 | 54936004 | bellard | } |
358 | 54936004 | bellard | |
359 | 992f48a0 | blueswir1 | int target_munmap(abi_ulong start, abi_ulong len)
|
360 | 54936004 | bellard | { |
361 | 992f48a0 | blueswir1 | abi_ulong end, real_start, real_end, addr; |
362 | 54936004 | bellard | int prot, ret;
|
363 | 54936004 | bellard | |
364 | 54936004 | bellard | #ifdef DEBUG_MMAP
|
365 | 54936004 | bellard | printf("munmap: start=0x%lx len=0x%lx\n", start, len);
|
366 | 54936004 | bellard | #endif
|
367 | 54936004 | bellard | if (start & ~TARGET_PAGE_MASK)
|
368 | 54936004 | bellard | return -EINVAL;
|
369 | 54936004 | bellard | len = TARGET_PAGE_ALIGN(len); |
370 | 54936004 | bellard | if (len == 0) |
371 | 54936004 | bellard | return -EINVAL;
|
372 | 54936004 | bellard | end = start + len; |
373 | 53a5960a | pbrook | real_start = start & qemu_host_page_mask; |
374 | 53a5960a | pbrook | real_end = HOST_PAGE_ALIGN(end); |
375 | 54936004 | bellard | |
376 | 53a5960a | pbrook | if (start > real_start) {
|
377 | 54936004 | bellard | /* handle host page containing start */
|
378 | 54936004 | bellard | prot = 0;
|
379 | 53a5960a | pbrook | for(addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) {
|
380 | 54936004 | bellard | prot |= page_get_flags(addr); |
381 | 54936004 | bellard | } |
382 | 53a5960a | pbrook | if (real_end == real_start + qemu_host_page_size) {
|
383 | 53a5960a | pbrook | for(addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
|
384 | d418c81e | bellard | prot |= page_get_flags(addr); |
385 | d418c81e | bellard | } |
386 | 53a5960a | pbrook | end = real_end; |
387 | d418c81e | bellard | } |
388 | 54936004 | bellard | if (prot != 0) |
389 | 53a5960a | pbrook | real_start += qemu_host_page_size; |
390 | 54936004 | bellard | } |
391 | 53a5960a | pbrook | if (end < real_end) {
|
392 | 54936004 | bellard | prot = 0;
|
393 | 53a5960a | pbrook | for(addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
|
394 | 54936004 | bellard | prot |= page_get_flags(addr); |
395 | 54936004 | bellard | } |
396 | 54936004 | bellard | if (prot != 0) |
397 | 53a5960a | pbrook | real_end -= qemu_host_page_size; |
398 | 54936004 | bellard | } |
399 | 3b46e624 | ths | |
400 | 54936004 | bellard | /* unmap what we can */
|
401 | 53a5960a | pbrook | if (real_start < real_end) {
|
402 | 4118a970 | j_mayer | ret = munmap(g2h(real_start), real_end - real_start); |
403 | 54936004 | bellard | if (ret != 0) |
404 | 54936004 | bellard | return ret;
|
405 | 54936004 | bellard | } |
406 | 54936004 | bellard | |
407 | 54936004 | bellard | page_set_flags(start, start + len, 0);
|
408 | 54936004 | bellard | return 0; |
409 | 54936004 | bellard | } |
410 | 54936004 | bellard | |
411 | 54936004 | bellard | /* XXX: currently, we only handle MAP_ANONYMOUS and not MAP_FIXED
|
412 | 54936004 | bellard | blocks which have been allocated starting on a host page */
|
413 | 992f48a0 | blueswir1 | abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, |
414 | 992f48a0 | blueswir1 | abi_ulong new_size, unsigned long flags, |
415 | 992f48a0 | blueswir1 | abi_ulong new_addr) |
416 | 54936004 | bellard | { |
417 | 54936004 | bellard | int prot;
|
418 | a5b85f79 | ths | unsigned long host_addr; |
419 | 54936004 | bellard | |
420 | 54936004 | bellard | /* XXX: use 5 args syscall */
|
421 | a5b85f79 | ths | host_addr = (long)mremap(g2h(old_addr), old_size, new_size, flags);
|
422 | a5b85f79 | ths | if (host_addr == -1) |
423 | a5b85f79 | ths | return -1; |
424 | a5b85f79 | ths | new_addr = h2g(host_addr); |
425 | 54936004 | bellard | prot = page_get_flags(old_addr); |
426 | 54936004 | bellard | page_set_flags(old_addr, old_addr + old_size, 0);
|
427 | 54936004 | bellard | page_set_flags(new_addr, new_addr + new_size, prot | PAGE_VALID); |
428 | 54936004 | bellard | return new_addr;
|
429 | 54936004 | bellard | } |
430 | 54936004 | bellard | |
431 | 992f48a0 | blueswir1 | int target_msync(abi_ulong start, abi_ulong len, int flags) |
432 | 54936004 | bellard | { |
433 | 992f48a0 | blueswir1 | abi_ulong end; |
434 | 54936004 | bellard | |
435 | 54936004 | bellard | if (start & ~TARGET_PAGE_MASK)
|
436 | 54936004 | bellard | return -EINVAL;
|
437 | 54936004 | bellard | len = TARGET_PAGE_ALIGN(len); |
438 | 54936004 | bellard | end = start + len; |
439 | d418c81e | bellard | if (end < start)
|
440 | d418c81e | bellard | return -EINVAL;
|
441 | d418c81e | bellard | if (end == start)
|
442 | d418c81e | bellard | return 0; |
443 | 3b46e624 | ths | |
444 | 83fb7adf | bellard | start &= qemu_host_page_mask; |
445 | 53a5960a | pbrook | return msync(g2h(start), end - start, flags);
|
446 | 54936004 | bellard | } |