]>
Commit | Line | Data |
---|---|---|
54936004 FB |
1 | /* |
2 | * mmap support for qemu | |
3 | * | |
4 | * Copyright (c) 2003 Fabrice Bellard | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
19 | */ | |
20 | #include <stdlib.h> | |
21 | #include <stdio.h> | |
22 | #include <stdarg.h> | |
23 | #include <string.h> | |
24 | #include <unistd.h> | |
25 | #include <errno.h> | |
26 | #include <sys/mman.h> | |
27 | ||
28 | #include "qemu.h" | |
29 | ||
30 | //#define DEBUG_MMAP | |
31 | ||
32 | /* NOTE: all the constants are the HOST ones */ | |
33 | int target_mprotect(unsigned long start, unsigned long len, int prot) | |
34 | { | |
35 | unsigned long end, host_start, host_end, addr; | |
36 | int prot1, ret; | |
37 | ||
38 | #ifdef DEBUG_MMAP | |
39 | printf("mprotect: start=0x%lx len=0x%lx prot=%c%c%c\n", start, len, | |
40 | prot & PROT_READ ? 'r' : '-', | |
41 | prot & PROT_WRITE ? 'w' : '-', | |
42 | prot & PROT_EXEC ? 'x' : '-'); | |
43 | #endif | |
44 | ||
45 | if ((start & ~TARGET_PAGE_MASK) != 0) | |
46 | return -EINVAL; | |
47 | len = TARGET_PAGE_ALIGN(len); | |
48 | end = start + len; | |
49 | if (end < start) | |
50 | return -EINVAL; | |
51 | if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC)) | |
52 | return -EINVAL; | |
53 | if (len == 0) | |
54 | return 0; | |
55 | ||
56 | host_start = start & host_page_mask; | |
57 | host_end = HOST_PAGE_ALIGN(end); | |
58 | if (start > host_start) { | |
59 | /* handle host page containing start */ | |
60 | prot1 = prot; | |
61 | for(addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) { | |
62 | prot1 |= page_get_flags(addr); | |
63 | } | |
64 | ret = mprotect((void *)host_start, host_page_size, prot1 & PAGE_BITS); | |
65 | if (ret != 0) | |
66 | return ret; | |
67 | host_start += host_page_size; | |
68 | } | |
69 | if (end < host_end) { | |
70 | /* handle host page containing end (can be the same as first page) */ | |
71 | prot1 = prot; | |
72 | for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) { | |
73 | prot1 |= page_get_flags(addr); | |
74 | } | |
75 | ret = mprotect((void *)(host_end - host_page_size), host_page_size, | |
76 | prot1 & PAGE_BITS); | |
77 | if (ret != 0) | |
78 | return ret; | |
79 | host_end -= host_page_size; | |
80 | } | |
81 | ||
82 | /* handle the pages in the middle */ | |
83 | if (host_start < host_end) { | |
84 | ret = mprotect((void *)host_start, host_end - host_start, prot); | |
85 | if (ret != 0) | |
86 | return ret; | |
87 | } | |
88 | ||
89 | page_set_flags(start, start + len, prot | PAGE_VALID); | |
90 | return 0; | |
91 | } | |
92 | ||
93 | /* map an incomplete host page */ | |
94 | int mmap_frag(unsigned long host_start, | |
95 | unsigned long start, unsigned long end, | |
96 | int prot, int flags, int fd, unsigned long offset) | |
97 | { | |
98 | unsigned long host_end, ret, addr; | |
99 | int prot1, prot_new; | |
100 | ||
101 | host_end = host_start + host_page_size; | |
102 | ||
103 | /* get the protection of the target pages outside the mapping */ | |
104 | prot1 = 0; | |
105 | for(addr = host_start; addr < host_end; addr++) { | |
106 | if (addr < start || addr >= end) | |
107 | prot1 |= page_get_flags(addr); | |
108 | } | |
109 | ||
110 | if (prot1 == 0) { | |
111 | /* no page was there, so we allocate one */ | |
112 | ret = (long)mmap((void *)host_start, host_page_size, prot, | |
113 | flags | MAP_ANONYMOUS, -1, 0); | |
114 | if (ret == -1) | |
115 | return ret; | |
116 | } | |
117 | prot1 &= PAGE_BITS; | |
118 | ||
119 | prot_new = prot | prot1; | |
120 | if (!(flags & MAP_ANONYMOUS)) { | |
121 | /* msync() won't work here, so we return an error if write is | |
122 | possible while it is a shared mapping */ | |
123 | if ((flags & MAP_TYPE) == MAP_SHARED && | |
124 | (prot & PROT_WRITE)) | |
125 | return -EINVAL; | |
126 | ||
127 | /* adjust protection to be able to read */ | |
128 | if (!(prot1 & PROT_WRITE)) | |
129 | mprotect((void *)host_start, host_page_size, prot1 | PROT_WRITE); | |
130 | ||
131 | /* read the corresponding file data */ | |
132 | pread(fd, (void *)start, end - start, offset); | |
133 | ||
134 | /* put final protection */ | |
135 | if (prot_new != (prot1 | PROT_WRITE)) | |
136 | mprotect((void *)host_start, host_page_size, prot_new); | |
137 | } else { | |
138 | /* just update the protection */ | |
139 | if (prot_new != prot1) { | |
140 | mprotect((void *)host_start, host_page_size, prot_new); | |
141 | } | |
142 | } | |
143 | return 0; | |
144 | } | |
145 | ||
146 | /* NOTE: all the constants are the HOST ones */ | |
147 | long target_mmap(unsigned long start, unsigned long len, int prot, | |
148 | int flags, int fd, unsigned long offset) | |
149 | { | |
150 | unsigned long ret, end, host_start, host_end, retaddr, host_offset, host_len; | |
151 | ||
152 | #ifdef DEBUG_MMAP | |
153 | { | |
154 | printf("mmap: start=0x%lx len=0x%lx prot=%c%c%c flags=", | |
155 | start, len, | |
156 | prot & PROT_READ ? 'r' : '-', | |
157 | prot & PROT_WRITE ? 'w' : '-', | |
158 | prot & PROT_EXEC ? 'x' : '-'); | |
159 | if (flags & MAP_FIXED) | |
160 | printf("MAP_FIXED "); | |
161 | if (flags & MAP_ANONYMOUS) | |
162 | printf("MAP_ANON "); | |
163 | switch(flags & MAP_TYPE) { | |
164 | case MAP_PRIVATE: | |
165 | printf("MAP_PRIVATE "); | |
166 | break; | |
167 | case MAP_SHARED: | |
168 | printf("MAP_SHARED "); | |
169 | break; | |
170 | default: | |
171 | printf("[MAP_TYPE=0x%x] ", flags & MAP_TYPE); | |
172 | break; | |
173 | } | |
174 | printf("fd=%d offset=%lx\n", fd, offset); | |
175 | } | |
176 | #endif | |
177 | ||
178 | if (offset & ~TARGET_PAGE_MASK) | |
179 | return -EINVAL; | |
180 | ||
181 | len = TARGET_PAGE_ALIGN(len); | |
182 | if (len == 0) | |
183 | return start; | |
184 | host_start = start & host_page_mask; | |
185 | ||
186 | if (!(flags & MAP_FIXED)) { | |
187 | if (host_page_size != real_host_page_size) { | |
188 | /* NOTE: this code is only for debugging with '-p' option */ | |
189 | /* reserve a memory area */ | |
190 | host_len = HOST_PAGE_ALIGN(len) + host_page_size - TARGET_PAGE_SIZE; | |
191 | host_start = (long)mmap((void *)host_start, host_len, PROT_NONE, | |
192 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |
193 | if (host_start == -1) | |
194 | return host_start; | |
195 | host_end = host_start + host_len; | |
196 | start = HOST_PAGE_ALIGN(host_start); | |
197 | end = start + HOST_PAGE_ALIGN(len); | |
198 | if (start > host_start) | |
199 | munmap((void *)host_start, start - host_start); | |
200 | if (end < host_end) | |
201 | munmap((void *)end, host_end - end); | |
202 | /* use it as a fixed mapping */ | |
203 | flags |= MAP_FIXED; | |
204 | } else { | |
205 | /* if not fixed, no need to do anything */ | |
206 | host_offset = offset & host_page_mask; | |
207 | host_len = len + offset - host_offset; | |
208 | start = (long)mmap((void *)host_start, host_len, | |
209 | prot, flags, fd, host_offset); | |
210 | if (start == -1) | |
211 | return start; | |
212 | /* update start so that it points to the file position at 'offset' */ | |
213 | if (!(flags & MAP_ANONYMOUS)) | |
214 | start += offset - host_offset; | |
215 | goto the_end1; | |
216 | } | |
217 | } | |
218 | ||
219 | if (start & ~TARGET_PAGE_MASK) | |
220 | return -EINVAL; | |
221 | end = start + len; | |
222 | host_end = HOST_PAGE_ALIGN(end); | |
223 | ||
224 | /* worst case: we cannot map the file because the offset is not | |
225 | aligned, so we read it */ | |
226 | if (!(flags & MAP_ANONYMOUS) && | |
227 | (offset & ~host_page_mask) != (start & ~host_page_mask)) { | |
228 | /* msync() won't work here, so we return an error if write is | |
229 | possible while it is a shared mapping */ | |
230 | if ((flags & MAP_TYPE) == MAP_SHARED && | |
231 | (prot & PROT_WRITE)) | |
232 | return -EINVAL; | |
233 | retaddr = target_mmap(start, len, prot | PROT_WRITE, | |
234 | MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, | |
235 | -1, 0); | |
236 | if (retaddr == -1) | |
237 | return retaddr; | |
238 | pread(fd, (void *)start, len, offset); | |
239 | if (!(prot & PROT_WRITE)) { | |
240 | ret = target_mprotect(start, len, prot); | |
241 | if (ret != 0) | |
242 | return ret; | |
243 | } | |
244 | goto the_end; | |
245 | } | |
246 | ||
247 | /* handle the start of the mapping */ | |
248 | if (start > host_start) { | |
249 | if (host_end == host_start + host_page_size) { | |
250 | /* one single host page */ | |
251 | ret = mmap_frag(host_start, start, end, | |
252 | prot, flags, fd, offset); | |
253 | if (ret == -1) | |
254 | return ret; | |
255 | goto the_end1; | |
256 | } | |
257 | ret = mmap_frag(host_start, start, host_start + host_page_size, | |
258 | prot, flags, fd, offset); | |
259 | if (ret == -1) | |
260 | return ret; | |
261 | host_start += host_page_size; | |
262 | } | |
263 | /* handle the end of the mapping */ | |
264 | if (end < host_end) { | |
265 | ret = mmap_frag(host_end - host_page_size, | |
266 | host_end - host_page_size, host_end, | |
267 | prot, flags, fd, | |
268 | offset + host_end - host_page_size - start); | |
269 | if (ret == -1) | |
270 | return ret; | |
271 | host_end -= host_page_size; | |
272 | } | |
273 | ||
274 | /* map the middle (easier) */ | |
275 | if (host_start < host_end) { | |
276 | ret = (long)mmap((void *)host_start, host_end - host_start, | |
277 | prot, flags, fd, offset + host_start - start); | |
278 | if (ret == -1) | |
279 | return ret; | |
280 | } | |
281 | the_end1: | |
282 | page_set_flags(start, start + len, prot | PAGE_VALID); | |
283 | the_end: | |
284 | #ifdef DEBUG_MMAP | |
285 | page_dump(stdout); | |
286 | printf("\n"); | |
287 | #endif | |
288 | return start; | |
289 | } | |
290 | ||
291 | int target_munmap(unsigned long start, unsigned long len) | |
292 | { | |
293 | unsigned long end, host_start, host_end, addr; | |
294 | int prot, ret; | |
295 | ||
296 | #ifdef DEBUG_MMAP | |
297 | printf("munmap: start=0x%lx len=0x%lx\n", start, len); | |
298 | #endif | |
299 | if (start & ~TARGET_PAGE_MASK) | |
300 | return -EINVAL; | |
301 | len = TARGET_PAGE_ALIGN(len); | |
302 | if (len == 0) | |
303 | return -EINVAL; | |
304 | end = start + len; | |
305 | host_start = start & host_page_mask; | |
306 | host_end = HOST_PAGE_ALIGN(end); | |
307 | ||
308 | if (start > host_start) { | |
309 | /* handle host page containing start */ | |
310 | prot = 0; | |
311 | for(addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) { | |
312 | prot |= page_get_flags(addr); | |
313 | } | |
314 | if (prot != 0) | |
315 | host_start += host_page_size; | |
316 | } | |
317 | if (end < host_end) { | |
318 | /* handle host page containing end (can be the same as first page) */ | |
319 | prot = 0; | |
320 | for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) { | |
321 | prot |= page_get_flags(addr); | |
322 | } | |
323 | if (prot != 0) | |
324 | host_end -= host_page_size; | |
325 | } | |
326 | ||
327 | /* unmap what we can */ | |
328 | if (host_start < host_end) { | |
329 | ret = munmap((void *)host_start, host_end - host_start); | |
330 | if (ret != 0) | |
331 | return ret; | |
332 | } | |
333 | ||
334 | page_set_flags(start, start + len, 0); | |
335 | return 0; | |
336 | } | |
337 | ||
338 | /* XXX: currently, we only handle MAP_ANONYMOUS and not MAP_FIXED | |
339 | blocks which have been allocated starting on a host page */ | |
340 | long target_mremap(unsigned long old_addr, unsigned long old_size, | |
341 | unsigned long new_size, unsigned long flags, | |
342 | unsigned long new_addr) | |
343 | { | |
344 | int prot; | |
345 | ||
346 | /* XXX: use 5 args syscall */ | |
347 | new_addr = (long)mremap((void *)old_addr, old_size, new_size, flags); | |
348 | if (new_addr == -1) | |
349 | return new_addr; | |
350 | prot = page_get_flags(old_addr); | |
351 | page_set_flags(old_addr, old_addr + old_size, 0); | |
352 | page_set_flags(new_addr, new_addr + new_size, prot | PAGE_VALID); | |
353 | return new_addr; | |
354 | } | |
355 | ||
356 | int target_msync(unsigned long start, unsigned long len, int flags) | |
357 | { | |
358 | unsigned long end; | |
359 | ||
360 | if (start & ~TARGET_PAGE_MASK) | |
361 | return -EINVAL; | |
362 | len = TARGET_PAGE_ALIGN(len); | |
363 | if (len == 0) | |
364 | return 0; | |
365 | end = start + len; | |
366 | ||
367 | start &= host_page_mask; | |
368 | return msync((void *)start, len, flags); | |
369 | } | |
370 |