Revision 53a5960a linux-user/mmap.c

b/linux-user/mmap.c
29 29

  
30 30
//#define DEBUG_MMAP
31 31

  
32
/* NOTE: all the constants are the HOST ones */
33
int target_mprotect(unsigned long start, unsigned long len, int prot)
32
/* NOTE: all the constants are the HOST ones, but addresses are target. */
33
int target_mprotect(target_ulong start, target_ulong len, int prot)
34 34
{
35
    unsigned long end, host_start, host_end, addr;
35
    target_ulong end, host_start, host_end, addr;
36 36
    int prot1, ret;
37 37

  
38 38
#ifdef DEBUG_MMAP
......
67 67
            }
68 68
            end = host_end;
69 69
        }
70
        ret = mprotect((void *)host_start, qemu_host_page_size, prot1 & PAGE_BITS);
70
        ret = mprotect(g2h(host_start), qemu_host_page_size, prot1 & PAGE_BITS);
71 71
        if (ret != 0)
72 72
            return ret;
73 73
        host_start += qemu_host_page_size;
......
77 77
        for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
78 78
            prot1 |= page_get_flags(addr);
79 79
        }
80
        ret = mprotect((void *)(host_end - qemu_host_page_size), qemu_host_page_size, 
80
        ret = mprotect(g2h(host_end - qemu_host_page_size), qemu_host_page_size, 
81 81
                       prot1 & PAGE_BITS);
82 82
        if (ret != 0)
83 83
            return ret;
......
86 86
    
87 87
    /* handle the pages in the middle */
88 88
    if (host_start < host_end) {
89
        ret = mprotect((void *)host_start, host_end - host_start, prot);
89
        ret = mprotect(g2h(host_start), host_end - host_start, prot);
90 90
        if (ret != 0)
91 91
            return ret;
92 92
    }
......
95 95
}
96 96

  
97 97
/* map an incomplete host page */
98
int mmap_frag(unsigned long host_start, 
99
               unsigned long start, unsigned long end, 
100
               int prot, int flags, int fd, unsigned long offset)
98
static int mmap_frag(target_ulong real_start, 
99
                     target_ulong start, target_ulong end, 
100
                     int prot, int flags, int fd, target_ulong offset)
101 101
{
102
    unsigned long host_end, ret, addr;
102
    target_ulong real_end, ret, addr;
103
    void *host_start;
103 104
    int prot1, prot_new;
104 105

  
105
    host_end = host_start + qemu_host_page_size;
106
    real_end = real_start + qemu_host_page_size;
107
    host_start = g2h(real_start);
106 108

  
107 109
    /* get the protection of the target pages outside the mapping */
108 110
    prot1 = 0;
109
    for(addr = host_start; addr < host_end; addr++) {
111
    for(addr = real_start; addr < real_end; addr++) {
110 112
        if (addr < start || addr >= end)
111 113
            prot1 |= page_get_flags(addr);
112 114
    }
113 115
    
114 116
    if (prot1 == 0) {
115 117
        /* no page was there, so we allocate one */
116
        ret = (long)mmap((void *)host_start, qemu_host_page_size, prot, 
118
        ret = (long)mmap(host_start, qemu_host_page_size, prot, 
117 119
                         flags | MAP_ANONYMOUS, -1, 0);
118 120
        if (ret == -1)
119 121
            return ret;
122
        prot1 = prot;
120 123
    }
121 124
    prot1 &= PAGE_BITS;
122 125

  
......
130 133

  
131 134
        /* adjust protection to be able to read */
132 135
        if (!(prot1 & PROT_WRITE))
133
            mprotect((void *)host_start, qemu_host_page_size, prot1 | PROT_WRITE);
136
            mprotect(host_start, qemu_host_page_size, prot1 | PROT_WRITE);
134 137
        
135 138
        /* read the corresponding file data */
136
        pread(fd, (void *)start, end - start, offset);
139
        pread(fd, g2h(start), end - start, offset);
137 140
        
138 141
        /* put final protection */
139 142
        if (prot_new != (prot1 | PROT_WRITE))
140
            mprotect((void *)host_start, qemu_host_page_size, prot_new);
143
            mprotect(host_start, qemu_host_page_size, prot_new);
141 144
    } else {
142 145
        /* just update the protection */
143 146
        if (prot_new != prot1) {
144
            mprotect((void *)host_start, qemu_host_page_size, prot_new);
147
            mprotect(host_start, qemu_host_page_size, prot_new);
145 148
        }
146 149
    }
147 150
    return 0;
148 151
}
149 152

  
150 153
/* NOTE: all the constants are the HOST ones */
151
long target_mmap(unsigned long start, unsigned long len, int prot, 
152
                 int flags, int fd, unsigned long offset)
154
long target_mmap(target_ulong start, target_ulong len, int prot, 
155
                 int flags, int fd, target_ulong offset)
153 156
{
154
    unsigned long ret, end, host_start, host_end, retaddr, host_offset, host_len;
157
    target_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len;
158
    long host_start;
155 159
#if defined(__alpha__) || defined(__sparc__) || defined(__x86_64__) || \
156 160
    defined(__ia64)
157
    static unsigned long last_start = 0x40000000;
161
    static target_ulong last_start = 0x40000000;
162
#elif defined(__CYGWIN__)
163
    /* Cygwin doesn't have a whole lot of address space.  */
164
    static target_ulong last_start = 0x18000000;
158 165
#endif
159 166

  
160 167
#ifdef DEBUG_MMAP
......
191 198
    len = TARGET_PAGE_ALIGN(len);
192 199
    if (len == 0)
193 200
        return start;
194
    host_start = start & qemu_host_page_mask;
201
    real_start = start & qemu_host_page_mask;
195 202

  
196 203
    if (!(flags & MAP_FIXED)) {
197 204
#if defined(__alpha__) || defined(__sparc__) || defined(__x86_64__) || \
198
    defined(__ia64)
205
    defined(__ia64) || defined(__CYGWIN__)
199 206
        /* tell the kenel to search at the same place as i386 */
200
        if (host_start == 0) {
201
            host_start = last_start;
207
        if (real_start == 0) {
208
            real_start = last_start;
202 209
            last_start += HOST_PAGE_ALIGN(len);
203 210
        }
204 211
#endif
205 212
        if (qemu_host_page_size != qemu_real_host_page_size) {
206 213
            /* NOTE: this code is only for debugging with '-p' option */
214
            /* ??? Can also occur when TARGET_PAGE_SIZE > host page size.  */
207 215
            /* reserve a memory area */
216
            /* ??? This needs fixing for remapping.  */
217
abort();
208 218
            host_len = HOST_PAGE_ALIGN(len) + qemu_host_page_size - TARGET_PAGE_SIZE;
209
            host_start = (long)mmap((void *)host_start, host_len, PROT_NONE, 
219
            real_start = (long)mmap(g2h(real_start), host_len, PROT_NONE, 
210 220
                                    MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
211
            if (host_start == -1)
212
                return host_start;
213
            host_end = host_start + host_len;
214
            start = HOST_PAGE_ALIGN(host_start);
221
            if (real_start == -1)
222
                return real_start;
223
            real_end = real_start + host_len;
224
            start = HOST_PAGE_ALIGN(real_start);
215 225
            end = start + HOST_PAGE_ALIGN(len);
216
            if (start > host_start)
217
                munmap((void *)host_start, start - host_start);
218
            if (end < host_end)
219
                munmap((void *)end, host_end - end);
226
            if (start > real_start)
227
                munmap((void *)real_start, start - real_start);
228
            if (end < real_end)
229
                munmap((void *)end, real_end - end);
220 230
            /* use it as a fixed mapping */
221 231
            flags |= MAP_FIXED;
222 232
        } else {
223 233
            /* if not fixed, no need to do anything */
224 234
            host_offset = offset & qemu_host_page_mask;
225 235
            host_len = len + offset - host_offset;
226
            start = (long)mmap((void *)host_start, host_len, 
227
                               prot, flags, fd, host_offset);
228
            if (start == -1)
229
                return start;
236
            host_start = (long)mmap(real_start ? g2h(real_start) : NULL,
237
                                    host_len, prot, flags, fd, host_offset);
238
            if (host_start == -1)
239
                return host_start;
230 240
            /* update start so that it points to the file position at 'offset' */
231 241
            if (!(flags & MAP_ANONYMOUS)) 
232
                start += offset - host_offset;
242
                host_start += offset - host_offset;
243
            start = h2g(host_start);
233 244
            goto the_end1;
234 245
        }
235 246
    }
......
239 250
        return -1;
240 251
    }
241 252
    end = start + len;
242
    host_end = HOST_PAGE_ALIGN(end);
253
    real_end = HOST_PAGE_ALIGN(end);
243 254

  
244 255
    /* worst case: we cannot map the file because the offset is not
245 256
       aligned, so we read it */
......
257 268
                              -1, 0);
258 269
        if (retaddr == -1)
259 270
            return retaddr;
260
        pread(fd, (void *)start, len, offset);
271
        pread(fd, g2h(start), len, offset);
261 272
        if (!(prot & PROT_WRITE)) {
262 273
            ret = target_mprotect(start, len, prot);
263 274
            if (ret != 0)
......
267 278
    }
268 279

  
269 280
    /* handle the start of the mapping */
270
    if (start > host_start) {
271
        if (host_end == host_start + qemu_host_page_size) {
281
    if (start > real_start) {
282
        if (real_end == real_start + qemu_host_page_size) {
272 283
            /* one single host page */
273
            ret = mmap_frag(host_start, start, end,
284
            ret = mmap_frag(real_start, start, end,
274 285
                            prot, flags, fd, offset);
275 286
            if (ret == -1)
276 287
                return ret;
277 288
            goto the_end1;
278 289
        }
279
        ret = mmap_frag(host_start, start, host_start + qemu_host_page_size,
290
        ret = mmap_frag(real_start, start, real_start + qemu_host_page_size,
280 291
                        prot, flags, fd, offset);
281 292
        if (ret == -1)
282 293
            return ret;
283
        host_start += qemu_host_page_size;
294
        real_start += qemu_host_page_size;
284 295
    }
285 296
    /* handle the end of the mapping */
286
    if (end < host_end) {
287
        ret = mmap_frag(host_end - qemu_host_page_size, 
288
                        host_end - qemu_host_page_size, host_end,
297
    if (end < real_end) {
298
        ret = mmap_frag(real_end - qemu_host_page_size, 
299
                        real_end - qemu_host_page_size, real_end,
289 300
                        prot, flags, fd, 
290
                        offset + host_end - qemu_host_page_size - start);
301
                        offset + real_end - qemu_host_page_size - start);
291 302
        if (ret == -1)
292 303
            return ret;
293
        host_end -= qemu_host_page_size;
304
        real_end -= qemu_host_page_size;
294 305
    }
295 306
    
296 307
    /* map the middle (easier) */
297
    if (host_start < host_end) {
308
    if (real_start < real_end) {
298 309
        unsigned long offset1;
299 310
	if (flags & MAP_ANONYMOUS)
300 311
	  offset1 = 0;
301 312
	else
302
	  offset1 = offset + host_start - start;
303
        ret = (long)mmap((void *)host_start, host_end - host_start, 
313
	  offset1 = offset + real_start - start;
314
        ret = (long)mmap(g2h(real_start), real_end - real_start, 
304 315
                         prot, flags, fd, offset1);
305 316
        if (ret == -1)
306 317
            return ret;
......
316 327
    return start;
317 328
}
318 329

  
319
int target_munmap(unsigned long start, unsigned long len)
330
int target_munmap(target_ulong start, target_ulong len)
320 331
{
321
    unsigned long end, host_start, host_end, addr;
332
    target_ulong end, real_start, real_end, addr;
322 333
    int prot, ret;
323 334

  
324 335
#ifdef DEBUG_MMAP
......
330 341
    if (len == 0)
331 342
        return -EINVAL;
332 343
    end = start + len;
333
    host_start = start & qemu_host_page_mask;
334
    host_end = HOST_PAGE_ALIGN(end);
344
    real_start = start & qemu_host_page_mask;
345
    real_end = HOST_PAGE_ALIGN(end);
335 346

  
336
    if (start > host_start) {
347
    if (start > real_start) {
337 348
        /* handle host page containing start */
338 349
        prot = 0;
339
        for(addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {
350
        for(addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) {
340 351
            prot |= page_get_flags(addr);
341 352
        }
342
        if (host_end == host_start + qemu_host_page_size) {
343
            for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
353
        if (real_end == real_start + qemu_host_page_size) {
354
            for(addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
344 355
                prot |= page_get_flags(addr);
345 356
            }
346
            end = host_end;
357
            end = real_end;
347 358
        }
348 359
        if (prot != 0)
349
            host_start += qemu_host_page_size;
360
            real_start += qemu_host_page_size;
350 361
    }
351
    if (end < host_end) {
362
    if (end < real_end) {
352 363
        prot = 0;
353
        for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
364
        for(addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
354 365
            prot |= page_get_flags(addr);
355 366
        }
356 367
        if (prot != 0)
357
            host_end -= qemu_host_page_size;
368
            real_end -= qemu_host_page_size;
358 369
    }
359 370
    
360 371
    /* unmap what we can */
361
    if (host_start < host_end) {
362
        ret = munmap((void *)host_start, host_end - host_start);
372
    if (real_start < real_end) {
373
        ret = munmap((void *)real_start, real_end - real_start);
363 374
        if (ret != 0)
364 375
            return ret;
365 376
    }
......
370 381

  
371 382
/* XXX: currently, we only handle MAP_ANONYMOUS and not MAP_FIXED
372 383
   blocks which have been allocated starting on a host page */
373
long target_mremap(unsigned long old_addr, unsigned long old_size, 
374
                   unsigned long new_size, unsigned long flags,
375
                   unsigned long new_addr)
384
long target_mremap(target_ulong old_addr, target_ulong old_size, 
385
                   target_ulong new_size, unsigned long flags,
386
                   target_ulong new_addr)
376 387
{
377 388
    int prot;
378 389

  
379 390
    /* XXX: use 5 args syscall */
380
    new_addr = (long)mremap((void *)old_addr, old_size, new_size, flags);
391
    new_addr = (long)mremap(g2h(old_addr), old_size, new_size, flags);
381 392
    if (new_addr == -1)
382 393
        return new_addr;
394
    new_addr = h2g(new_addr);
383 395
    prot = page_get_flags(old_addr);
384 396
    page_set_flags(old_addr, old_addr + old_size, 0);
385 397
    page_set_flags(new_addr, new_addr + new_size, prot | PAGE_VALID);
386 398
    return new_addr;
387 399
}
388 400

  
389
int target_msync(unsigned long start, unsigned long len, int flags)
401
int target_msync(target_ulong start, target_ulong len, int flags)
390 402
{
391
    unsigned long end;
403
    target_ulong end;
392 404

  
393 405
    if (start & ~TARGET_PAGE_MASK)
394 406
        return -EINVAL;
......
400 412
        return 0;
401 413
    
402 414
    start &= qemu_host_page_mask;
403
    return msync((void *)start, end - start, flags);
415
    return msync(g2h(start), end - start, flags);
404 416
}
405 417

  

Also available in: Unified diff