2 * mmap support for qemu
4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
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., 51 Franklin Street - Fifth Floor, Boston,
33 /* NOTE: all the constants are the HOST ones */
34 int target_mprotect(unsigned long start, unsigned long len, int prot)
36 unsigned long end, host_start, host_end, addr;
40 printf("mprotect: start=0x%lx len=0x%lx prot=%c%c%c\n", start, len,
41 prot & PROT_READ ? 'r' : '-',
42 prot & PROT_WRITE ? 'w' : '-',
43 prot & PROT_EXEC ? 'x' : '-');
46 if ((start & ~TARGET_PAGE_MASK) != 0)
48 len = TARGET_PAGE_ALIGN(len);
52 if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
57 host_start = start & qemu_host_page_mask;
58 host_end = HOST_PAGE_ALIGN(end);
59 if (start > host_start) {
60 /* handle host page containing start */
62 for(addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {
63 prot1 |= page_get_flags(addr);
65 if (host_end == host_start + qemu_host_page_size) {
66 for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
67 prot1 |= page_get_flags(addr);
71 ret = mprotect((void *)host_start, qemu_host_page_size, prot1 & PAGE_BITS);
74 host_start += qemu_host_page_size;
78 for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
79 prot1 |= page_get_flags(addr);
81 ret = mprotect((void *)(host_end - qemu_host_page_size), qemu_host_page_size,
85 host_end -= qemu_host_page_size;
88 /* handle the pages in the middle */
89 if (host_start < host_end) {
90 ret = mprotect((void *)host_start, host_end - host_start, prot);
94 page_set_flags(start, start + len, prot | PAGE_VALID);
98 /* map an incomplete host page */
99 int mmap_frag(unsigned long host_start,
100 unsigned long start, unsigned long end,
101 int prot, int flags, int fd, unsigned long offset)
103 unsigned long host_end, ret, addr;
106 host_end = host_start + qemu_host_page_size;
108 /* get the protection of the target pages outside the mapping */
110 for(addr = host_start; addr < host_end; addr++) {
111 if (addr < start || addr >= end)
112 prot1 |= page_get_flags(addr);
116 /* no page was there, so we allocate one */
117 ret = (long)mmap((void *)host_start, qemu_host_page_size, prot,
118 flags | MAP_ANONYMOUS, -1, 0);
124 prot_new = prot | prot1;
125 if (!(flags & MAP_ANONYMOUS)) {
126 /* msync() won't work here, so we return an error if write is
127 possible while it is a shared mapping */
129 if ((flags & MAP_TYPE) == MAP_SHARED &&
131 if ((flags & MAP_SHARED) &&
136 /* adjust protection to be able to read */
137 if (!(prot1 & PROT_WRITE))
138 mprotect((void *)host_start, qemu_host_page_size, prot1 | PROT_WRITE);
140 /* read the corresponding file data */
141 pread(fd, (void *)start, end - start, offset);
143 /* put final protection */
144 if (prot_new != (prot1 | PROT_WRITE))
145 mprotect((void *)host_start, qemu_host_page_size, prot_new);
147 /* just update the protection */
148 if (prot_new != prot1) {
149 mprotect((void *)host_start, qemu_host_page_size, prot_new);
155 /* NOTE: all the constants are the HOST ones */
156 long target_mmap(unsigned long start, unsigned long len, int prot,
157 int flags, int fd, unsigned long offset)
159 unsigned long ret, end, host_start, host_end, retaddr, host_offset, host_len;
160 #if defined(__alpha__) || defined(__sparc__) || defined(__x86_64__)
161 static unsigned long last_start = 0x40000000;
166 printf("mmap: start=0x%lx len=0x%lx prot=%c%c%c flags=",
168 prot & PROT_READ ? 'r' : '-',
169 prot & PROT_WRITE ? 'w' : '-',
170 prot & PROT_EXEC ? 'x' : '-');
171 if (flags & MAP_FIXED)
172 printf("MAP_FIXED ");
173 if (flags & MAP_ANONYMOUS)
176 # define MAP_TYPE 0x3
178 switch(flags & MAP_TYPE) {
180 printf("MAP_PRIVATE ");
183 printf("MAP_SHARED ");
186 printf("[MAP_TYPE=0x%x] ", flags & MAP_TYPE);
189 printf("fd=%d offset=%lx\n", fd, offset);
193 if (offset & ~TARGET_PAGE_MASK)
196 len = TARGET_PAGE_ALIGN(len);
199 host_start = start & qemu_host_page_mask;
201 if (!(flags & MAP_FIXED)) {
202 #if defined(__alpha__) || defined(__sparc__) || defined(__x86_64__)
203 /* tell the kernel to search at the same place as i386 */
204 if (host_start == 0) {
205 host_start = last_start;
206 last_start += HOST_PAGE_ALIGN(len);
209 if (qemu_host_page_size != qemu_real_host_page_size) {
210 /* NOTE: this code is only for debugging with '-p' option */
211 /* reserve a memory area */
212 host_len = HOST_PAGE_ALIGN(len) + qemu_host_page_size - TARGET_PAGE_SIZE;
213 host_start = (long)mmap((void *)host_start, host_len, PROT_NONE,
214 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
215 if (host_start == -1)
217 host_end = host_start + host_len;
218 start = HOST_PAGE_ALIGN(host_start);
219 end = start + HOST_PAGE_ALIGN(len);
220 if (start > host_start)
221 munmap((void *)host_start, start - host_start);
223 munmap((void *)end, host_end - end);
224 /* use it as a fixed mapping */
227 /* if not fixed, no need to do anything */
228 host_offset = offset & qemu_host_page_mask;
229 host_len = len + offset - host_offset;
230 start = (long)mmap((void *)host_start, host_len,
231 prot, flags, fd, host_offset);
234 /* update start so that it points to the file position at 'offset' */
235 if (!(flags & MAP_ANONYMOUS))
236 start += offset - host_offset;
241 if (start & ~TARGET_PAGE_MASK)
244 host_end = HOST_PAGE_ALIGN(end);
246 /* worst case: we cannot map the file because the offset is not
247 aligned, so we read it */
248 if (!(flags & MAP_ANONYMOUS) &&
249 (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) {
250 /* msync() won't work here, so we return an error if write is
251 possible while it is a shared mapping */
253 if ((flags & MAP_TYPE) == MAP_SHARED &&
255 if ((flags & MAP_SHARED) &&
259 retaddr = target_mmap(start, len, prot | PROT_WRITE,
260 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS,
264 pread(fd, (void *)start, len, offset);
265 if (!(prot & PROT_WRITE)) {
266 ret = target_mprotect(start, len, prot);
273 /* handle the start of the mapping */
274 if (start > host_start) {
275 if (host_end == host_start + qemu_host_page_size) {
276 /* one single host page */
277 ret = mmap_frag(host_start, start, end,
278 prot, flags, fd, offset);
283 ret = mmap_frag(host_start, start, host_start + qemu_host_page_size,
284 prot, flags, fd, offset);
287 host_start += qemu_host_page_size;
289 /* handle the end of the mapping */
290 if (end < host_end) {
291 ret = mmap_frag(host_end - qemu_host_page_size,
292 host_end - qemu_host_page_size, host_end,
294 offset + host_end - qemu_host_page_size - start);
297 host_end -= qemu_host_page_size;
300 /* map the middle (easier) */
301 if (host_start < host_end) {
302 unsigned long offset1;
303 if (flags & MAP_ANONYMOUS)
306 offset1 = offset + host_start - start;
307 ret = (long)mmap((void *)host_start, host_end - host_start,
308 prot, flags, fd, offset1);
313 page_set_flags(start, start + len, prot | PAGE_VALID);
316 printf("target_mmap: ret=0x%lx\n", (long)start);
323 int target_munmap(unsigned long start, unsigned long len)
325 unsigned long end, host_start, host_end, addr;
329 printf("munmap: start=0x%lx len=0x%lx\n", start, len);
331 if (start & ~TARGET_PAGE_MASK)
333 len = TARGET_PAGE_ALIGN(len);
337 host_start = start & qemu_host_page_mask;
338 host_end = HOST_PAGE_ALIGN(end);
340 if (start > host_start) {
341 /* handle host page containing start */
343 for(addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {
344 prot |= page_get_flags(addr);
346 if (host_end == host_start + qemu_host_page_size) {
347 for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
348 prot |= page_get_flags(addr);
353 host_start += qemu_host_page_size;
355 if (end < host_end) {
357 for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
358 prot |= page_get_flags(addr);
361 host_end -= qemu_host_page_size;
364 /* unmap what we can */
365 if (host_start < host_end) {
366 ret = munmap((void *)host_start, host_end - host_start);
371 page_set_flags(start, start + len, 0);
375 /* XXX: currently, we only handle MAP_ANONYMOUS and not MAP_FIXED
376 blocks which have been allocated starting on a host page */
377 long target_mremap(unsigned long old_addr, unsigned long old_size,
378 unsigned long new_size, unsigned long flags,
379 unsigned long new_addr)
382 /* XXX: use 5 args syscall */
383 new_addr = (long)mremap((void *)old_addr, old_size, new_size, flags);
386 prot = page_get_flags(old_addr);
387 page_set_flags(old_addr, old_addr + old_size, 0);
388 page_set_flags(new_addr, new_addr + new_size, prot | PAGE_VALID);
391 qerror("target_mremap: unsupported\n");
396 int target_msync(unsigned long start, unsigned long len, int flags)
400 if (start & ~TARGET_PAGE_MASK)
402 len = TARGET_PAGE_ALIGN(len);
409 start &= qemu_host_page_mask;
410 return msync((void *)start, end - start, flags);