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