Sparc64: split DPRINTF into CPUIRQ and EBUS versions
[qemu.git] / darwin-user / mmap.c
blob8ba61fb9747826b7fac94e51490007a38823ff86
1 /*
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, see <http://www.gnu.org/licenses/>.
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <stdarg.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <sys/mman.h>
27 #include "qemu.h"
29 //#define DEBUG_MMAP
31 /* NOTE: all the constants are the HOST ones */
32 int target_mprotect(unsigned long start, unsigned long len, int prot)
34 unsigned long end, host_start, host_end, addr;
35 int prot1, ret;
37 #ifdef DEBUG_MMAP
38 printf("mprotect: start=0x%lx len=0x%lx prot=%c%c%c\n", start, len,
39 prot & PROT_READ ? 'r' : '-',
40 prot & PROT_WRITE ? 'w' : '-',
41 prot & PROT_EXEC ? 'x' : '-');
42 #endif
44 if ((start & ~TARGET_PAGE_MASK) != 0)
45 return -EINVAL;
46 len = TARGET_PAGE_ALIGN(len);
47 end = start + len;
48 if (end < start)
49 return -EINVAL;
50 if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
51 return -EINVAL;
52 if (len == 0)
53 return 0;
55 host_start = start & qemu_host_page_mask;
56 host_end = HOST_PAGE_ALIGN(end);
57 if (start > host_start) {
58 /* handle host page containing start */
59 prot1 = prot;
60 for(addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {
61 prot1 |= page_get_flags(addr);
63 if (host_end == host_start + qemu_host_page_size) {
64 for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
65 prot1 |= page_get_flags(addr);
67 end = host_end;
69 ret = mprotect((void *)host_start, qemu_host_page_size, prot1 & PAGE_BITS);
70 if (ret != 0)
71 return ret;
72 host_start += qemu_host_page_size;
74 if (end < host_end) {
75 prot1 = prot;
76 for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
77 prot1 |= page_get_flags(addr);
79 ret = mprotect((void *)(host_end - qemu_host_page_size), qemu_host_page_size,
80 prot1 & PAGE_BITS);
81 if (ret != 0)
82 return ret;
83 host_end -= qemu_host_page_size;
86 /* handle the pages in the middle */
87 if (host_start < host_end) {
88 ret = mprotect((void *)host_start, host_end - host_start, prot);
89 if (ret != 0)
90 return ret;
92 page_set_flags(start, start + len, prot | PAGE_VALID);
93 return 0;
96 /* map an incomplete host page */
97 int mmap_frag(unsigned long host_start,
98 unsigned long start, unsigned long end,
99 int prot, int flags, int fd, unsigned long offset)
101 unsigned long host_end, ret, addr;
102 int prot1, prot_new;
104 host_end = host_start + qemu_host_page_size;
106 /* get the protection of the target pages outside the mapping */
107 prot1 = 0;
108 for(addr = host_start; addr < host_end; addr++) {
109 if (addr < start || addr >= end)
110 prot1 |= page_get_flags(addr);
113 if (prot1 == 0) {
114 /* no page was there, so we allocate one */
115 ret = (long)mmap((void *)host_start, qemu_host_page_size, prot,
116 flags | MAP_ANONYMOUS, -1, 0);
117 if (ret == -1)
118 return ret;
120 prot1 &= PAGE_BITS;
122 prot_new = prot | prot1;
123 if (!(flags & MAP_ANONYMOUS)) {
124 /* msync() won't work here, so we return an error if write is
125 possible while it is a shared mapping */
126 #ifndef __APPLE__
127 if ((flags & MAP_TYPE) == MAP_SHARED &&
128 #else
129 if ((flags & MAP_SHARED) &&
130 #endif
131 (prot & PROT_WRITE))
132 return -EINVAL;
134 /* adjust protection to be able to read */
135 if (!(prot1 & PROT_WRITE))
136 mprotect((void *)host_start, qemu_host_page_size, prot1 | PROT_WRITE);
138 /* read the corresponding file data */
139 pread(fd, (void *)start, end - start, offset);
141 /* put final protection */
142 if (prot_new != (prot1 | PROT_WRITE))
143 mprotect((void *)host_start, qemu_host_page_size, prot_new);
144 } else {
145 /* just update the protection */
146 if (prot_new != prot1) {
147 mprotect((void *)host_start, qemu_host_page_size, prot_new);
150 return 0;
153 /* NOTE: all the constants are the HOST ones */
154 long target_mmap(unsigned long start, unsigned long len, int prot,
155 int flags, int fd, unsigned long offset)
157 unsigned long ret, end, host_start, host_end, retaddr, host_offset, host_len;
158 #if defined(__alpha__) || defined(__sparc__) || defined(__x86_64__)
159 static unsigned long last_start = 0x40000000;
160 #endif
162 #ifdef DEBUG_MMAP
164 printf("mmap: start=0x%lx len=0x%lx prot=%c%c%c flags=",
165 start, len,
166 prot & PROT_READ ? 'r' : '-',
167 prot & PROT_WRITE ? 'w' : '-',
168 prot & PROT_EXEC ? 'x' : '-');
169 if (flags & MAP_FIXED)
170 printf("MAP_FIXED ");
171 if (flags & MAP_ANONYMOUS)
172 printf("MAP_ANON ");
173 #ifndef MAP_TYPE
174 # define MAP_TYPE 0x3
175 #endif
176 switch(flags & MAP_TYPE) {
177 case MAP_PRIVATE:
178 printf("MAP_PRIVATE ");
179 break;
180 case MAP_SHARED:
181 printf("MAP_SHARED ");
182 break;
183 default:
184 printf("[MAP_TYPE=0x%x] ", flags & MAP_TYPE);
185 break;
187 printf("fd=%d offset=%lx\n", fd, offset);
189 #endif
191 if (offset & ~TARGET_PAGE_MASK)
192 return -EINVAL;
194 len = TARGET_PAGE_ALIGN(len);
195 if (len == 0)
196 return start;
197 host_start = start & qemu_host_page_mask;
199 if (!(flags & MAP_FIXED)) {
200 #if defined(__alpha__) || defined(__sparc__) || defined(__x86_64__)
201 /* tell the kernel to search at the same place as i386 */
202 if (host_start == 0) {
203 host_start = last_start;
204 last_start += HOST_PAGE_ALIGN(len);
206 #endif
207 if (qemu_host_page_size != qemu_real_host_page_size) {
208 /* NOTE: this code is only for debugging with '-p' option */
209 /* reserve a memory area */
210 host_len = HOST_PAGE_ALIGN(len) + qemu_host_page_size - TARGET_PAGE_SIZE;
211 host_start = (long)mmap((void *)host_start, host_len, PROT_NONE,
212 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
213 if (host_start == -1)
214 return host_start;
215 host_end = host_start + host_len;
216 start = HOST_PAGE_ALIGN(host_start);
217 end = start + HOST_PAGE_ALIGN(len);
218 if (start > host_start)
219 munmap((void *)host_start, start - host_start);
220 if (end < host_end)
221 munmap((void *)end, host_end - end);
222 /* use it as a fixed mapping */
223 flags |= MAP_FIXED;
224 } else {
225 /* if not fixed, no need to do anything */
226 host_offset = offset & qemu_host_page_mask;
227 host_len = len + offset - host_offset;
228 start = (long)mmap((void *)host_start, host_len,
229 prot, flags, fd, host_offset);
230 if (start == -1)
231 return start;
232 /* update start so that it points to the file position at 'offset' */
233 if (!(flags & MAP_ANONYMOUS))
234 start += offset - host_offset;
235 goto the_end1;
239 if (start & ~TARGET_PAGE_MASK)
240 return -EINVAL;
241 end = start + len;
242 host_end = HOST_PAGE_ALIGN(end);
244 /* worst case: we cannot map the file because the offset is not
245 aligned, so we read it */
246 if (!(flags & MAP_ANONYMOUS) &&
247 (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) {
248 /* msync() won't work here, so we return an error if write is
249 possible while it is a shared mapping */
250 #ifndef __APPLE__
251 if ((flags & MAP_TYPE) == MAP_SHARED &&
252 #else
253 if ((flags & MAP_SHARED) &&
254 #endif
255 (prot & PROT_WRITE))
256 return -EINVAL;
257 retaddr = target_mmap(start, len, prot | PROT_WRITE,
258 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS,
259 -1, 0);
260 if (retaddr == -1)
261 return retaddr;
262 pread(fd, (void *)start, len, offset);
263 if (!(prot & PROT_WRITE)) {
264 ret = target_mprotect(start, len, prot);
265 if (ret != 0)
266 return ret;
268 goto the_end;
271 /* handle the start of the mapping */
272 if (start > host_start) {
273 if (host_end == host_start + qemu_host_page_size) {
274 /* one single host page */
275 ret = mmap_frag(host_start, start, end,
276 prot, flags, fd, offset);
277 if (ret == -1)
278 return ret;
279 goto the_end1;
281 ret = mmap_frag(host_start, start, host_start + qemu_host_page_size,
282 prot, flags, fd, offset);
283 if (ret == -1)
284 return ret;
285 host_start += qemu_host_page_size;
287 /* handle the end of the mapping */
288 if (end < host_end) {
289 ret = mmap_frag(host_end - qemu_host_page_size,
290 host_end - qemu_host_page_size, host_end,
291 prot, flags, fd,
292 offset + host_end - qemu_host_page_size - start);
293 if (ret == -1)
294 return ret;
295 host_end -= qemu_host_page_size;
298 /* map the middle (easier) */
299 if (host_start < host_end) {
300 unsigned long offset1;
301 if (flags & MAP_ANONYMOUS)
302 offset1 = 0;
303 else
304 offset1 = offset + host_start - start;
305 ret = (long)mmap((void *)host_start, host_end - host_start,
306 prot, flags, fd, offset1);
307 if (ret == -1)
308 return ret;
310 the_end1:
311 page_set_flags(start, start + len, prot | PAGE_VALID);
312 the_end:
313 #ifdef DEBUG_MMAP
314 printf("target_mmap: ret=0x%lx\n", (long)start);
315 page_dump(stdout);
316 printf("\n");
317 #endif
318 return start;
321 int target_munmap(unsigned long start, unsigned long len)
323 unsigned long end, host_start, host_end, addr;
324 int prot, ret;
326 #ifdef DEBUG_MMAP
327 printf("munmap: start=0x%lx len=0x%lx\n", start, len);
328 #endif
329 if (start & ~TARGET_PAGE_MASK)
330 return -EINVAL;
331 len = TARGET_PAGE_ALIGN(len);
332 if (len == 0)
333 return -EINVAL;
334 end = start + len;
335 host_start = start & qemu_host_page_mask;
336 host_end = HOST_PAGE_ALIGN(end);
338 if (start > host_start) {
339 /* handle host page containing start */
340 prot = 0;
341 for(addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {
342 prot |= page_get_flags(addr);
344 if (host_end == host_start + qemu_host_page_size) {
345 for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
346 prot |= page_get_flags(addr);
348 end = host_end;
350 if (prot != 0)
351 host_start += qemu_host_page_size;
353 if (end < host_end) {
354 prot = 0;
355 for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
356 prot |= page_get_flags(addr);
358 if (prot != 0)
359 host_end -= qemu_host_page_size;
362 /* unmap what we can */
363 if (host_start < host_end) {
364 ret = munmap((void *)host_start, host_end - host_start);
365 if (ret != 0)
366 return ret;
369 page_set_flags(start, start + len, 0);
370 return 0;
373 /* XXX: currently, we only handle MAP_ANONYMOUS and not MAP_FIXED
374 blocks which have been allocated starting on a host page */
375 long target_mremap(unsigned long old_addr, unsigned long old_size,
376 unsigned long new_size, unsigned long flags,
377 unsigned long new_addr)
379 #ifndef __APPLE__
380 /* XXX: use 5 args syscall */
381 new_addr = (long)mremap((void *)old_addr, old_size, new_size, flags);
382 if (new_addr == -1)
383 return new_addr;
384 prot = page_get_flags(old_addr);
385 page_set_flags(old_addr, old_addr + old_size, 0);
386 page_set_flags(new_addr, new_addr + new_size, prot | PAGE_VALID);
387 return new_addr;
388 #else
389 qerror("target_mremap: unsupported\n");
390 #endif
394 int target_msync(unsigned long start, unsigned long len, int flags)
396 unsigned long end;
398 if (start & ~TARGET_PAGE_MASK)
399 return -EINVAL;
400 len = TARGET_PAGE_ALIGN(len);
401 end = start + len;
402 if (end < start)
403 return -EINVAL;
404 if (end == start)
405 return 0;
407 start &= qemu_host_page_mask;
408 return msync((void *)start, end - start, flags);