libwine: Better memory reservation algorithm for platforms that use try_mmap_fixed.
[wine] / libs / wine / mmap.c
1 /*
2  * Wine memory mappings support
3  *
4  * Copyright 2000, 2004 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <ctype.h>
26 #include <fcntl.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #ifdef HAVE_SYS_MMAN_H
32 #include <sys/mman.h>
33 #endif
34 #ifdef HAVE_UNISTD_H
35 # include <unistd.h>
36 #endif
37 #ifdef HAVE_STDINT_H
38 # include <stdint.h>
39 #endif
40
41 #include "wine/library.h"
42 #include "wine/list.h"
43
44 struct reserved_area
45 {
46     struct list entry;
47     void       *base;
48     size_t      size;
49 };
50
51 static struct list reserved_areas = LIST_INIT(reserved_areas);
52 static const int granularity_mask = 0xffff;  /* reserved areas have 64k granularity */
53
54 #ifdef HAVE_MMAP
55
56 #ifndef MAP_NORESERVE
57 #define MAP_NORESERVE 0
58 #endif
59 #ifndef MAP_PRIVATE
60 #define MAP_PRIVATE 0
61 #endif
62 #ifndef MAP_ANON
63 #define MAP_ANON 0
64 #endif
65
66 static inline int get_fdzero(void)
67 {
68     static int fd = -1;
69
70     if (MAP_ANON == 0 && fd == -1)
71     {
72         if ((fd = open( "/dev/zero", O_RDONLY )) == -1)
73         {
74             perror( "/dev/zero: open" );
75             exit(1);
76         }
77     }
78     return fd;
79 }
80
81 #if (defined(__svr4__) || defined(__NetBSD__)) && !defined(MAP_TRYFIXED)
82 /***********************************************************************
83  *             try_mmap_fixed
84  *
85  * The purpose of this routine is to emulate the behaviour of
86  * the Linux mmap() routine if a non-NULL address is passed,
87  * but the MAP_FIXED flag is not set.  Linux in this case tries
88  * to place the mapping at the specified address, *unless* the
89  * range is already in use.  Solaris, however, completely ignores
90  * the address argument in this case.
91  *
92  * As Wine code occasionally relies on the Linux behaviour, e.g. to
93  * be able to map non-relocateable PE executables to their proper
94  * start addresses, or to map the DOS memory to 0, this routine
95  * emulates the Linux behaviour by checking whether the desired
96  * address range is still available, and placing the mapping there
97  * using MAP_FIXED if so.
98  */
99 static int try_mmap_fixed (void *addr, size_t len, int prot, int flags,
100                            int fildes, off_t off)
101 {
102     char * volatile result = NULL;
103     int pagesize = getpagesize();
104     pid_t pid;
105
106     /* We only try to map to a fixed address if
107        addr is non-NULL and properly aligned,
108        and MAP_FIXED isn't already specified. */
109
110     if ( !addr )
111         return 0;
112     if ( (uintptr_t)addr & (pagesize-1) )
113         return 0;
114     if ( flags & MAP_FIXED )
115         return 0;
116
117     /* We use vfork() to freeze all threads of the
118        current process.  This allows us to check without
119        race condition whether the desired memory range is
120        already in use.  Note that because vfork() shares
121        the address spaces between parent and child, we
122        can actually perform the mapping in the child. */
123
124     if ( (pid = vfork()) == -1 )
125     {
126         perror("try_mmap_fixed: vfork");
127         exit(1);
128     }
129     if ( pid == 0 )
130     {
131         int i;
132         char vec;
133
134         /* We call mincore() for every page in the desired range.
135            If any of these calls succeeds, the page is already
136            mapped and we must fail. */
137         for ( i = 0; i < len; i += pagesize )
138             if ( mincore( (caddr_t)addr + i, pagesize, &vec ) != -1 )
139                _exit(1);
140
141         /* Perform the mapping with MAP_FIXED set.  This is safe
142            now, as none of the pages is currently in use. */
143         result = mmap( addr, len, prot, flags | MAP_FIXED, fildes, off );
144         if ( result == addr )
145             _exit(0);
146
147         if ( result != (void *) -1 ) /* This should never happen ... */
148             munmap( result, len );
149
150        _exit(1);
151     }
152
153     /* vfork() lets the parent continue only after the child
154        has exited.  Furthermore, Wine sets SIGCHLD to SIG_IGN,
155        so we don't need to wait for the child. */
156
157     return result == addr;
158 }
159
160 #elif defined(__APPLE__)
161
162 #include <mach/mach_init.h>
163 #include <mach/vm_map.h>
164
165 /*
166  * On Darwin, we can use the Mach call vm_allocate to allocate
167  * anonymous memory at the specified address, and then use mmap with
168  * MAP_FIXED to replace the mapping.
169  */
170 static int try_mmap_fixed (void *addr, size_t len, int prot, int flags,
171                            int fildes, off_t off)
172 {
173     vm_address_t result = (vm_address_t)addr;
174
175     if (!vm_allocate(mach_task_self(),&result,len,0))
176     {
177         if (mmap( (void *)result, len, prot, flags | MAP_FIXED, fildes, off ) != MAP_FAILED)
178             return 1;
179         vm_deallocate(mach_task_self(),result,len);
180     }
181     return 0;
182 }
183
184 #endif  /* (__svr4__ || __NetBSD__) && !MAP_TRYFIXED */
185
186
187 /***********************************************************************
188  *              wine_anon_mmap
189  *
190  * Portable wrapper for anonymous mmaps
191  */
192 void *wine_anon_mmap( void *start, size_t size, int prot, int flags )
193 {
194 #ifdef MAP_SHARED
195     flags &= ~MAP_SHARED;
196 #endif
197
198     /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
199     flags |= MAP_PRIVATE | MAP_ANON;
200
201     if (!(flags & MAP_FIXED))
202     {
203 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
204         /* Even FreeBSD 5.3 does not properly support NULL here. */
205         if( start == NULL ) start = (void *)0x110000;
206 #endif
207
208 #ifdef MAP_TRYFIXED
209         /* If available, this will attempt a fixed mapping in-kernel */
210         flags |= MAP_TRYFIXED;
211 #elif defined(__svr4__) || defined(__NetBSD__) || defined(__APPLE__)
212         if ( try_mmap_fixed( start, size, prot, flags, get_fdzero(), 0 ) )
213             return start;
214 #endif
215     }
216     return mmap( start, size, prot, flags, get_fdzero(), 0 );
217 }
218
219
220 /***********************************************************************
221  *              mmap_reserve
222  *
223  * mmap wrapper used for reservations, only maps the specified address
224  */
225 static inline int mmap_reserve( void *addr, size_t size )
226 {
227     void *ptr;
228     int flags = MAP_PRIVATE | MAP_ANON | MAP_NORESERVE;
229
230 #ifdef MAP_TRYFIXED
231     flags |= MAP_TRYFIXED;
232 #elif defined(__APPLE__)
233     return try_mmap_fixed( addr, size, PROT_NONE, flags, get_fdzero(), 0 );
234 #endif
235     ptr = mmap( addr, size, PROT_NONE, flags, get_fdzero(), 0 );
236     if (ptr != addr && ptr != (void *)-1)  munmap( ptr, size );
237     return (ptr == addr);
238 }
239
240
241 /***********************************************************************
242  *           reserve_area
243  *
244  * Reserve as much memory as possible in the given area.
245  */
246 static void reserve_area( void *addr, void *end )
247 {
248     size_t size = (char *)end - (char *)addr;
249
250 #if (defined(__svr4__) || defined(__NetBSD__)) && !defined(MAP_TRYFIXED)
251     /* try_mmap_fixed is inefficient when using vfork, so we need a different algorithm here */
252     /* we assume no other thread is running at this point */
253     size_t i, pagesize = getpagesize();
254     char vec;
255
256     while (size)
257     {
258         for (i = 0; i < size; i += pagesize)
259             if (mincore( (caddr_t)addr + i, pagesize, &vec ) != -1) break;
260
261         i &= ~granularity_mask;
262         if (i && mmap( addr, i, PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
263                        get_fdzero(), 0 ) != (void *)-1)
264             wine_mmap_add_reserved_area( addr, i );
265
266         i += granularity_mask + 1;
267         if ((char *)addr + i < (char *)addr) break;  /* overflow */
268         addr = (char *)addr + i;
269         if (addr >= end) break;
270         size = (char *)end - (char *)addr;
271     }
272 #else
273     if (!size) return;
274
275     if (mmap_reserve( addr, size ))
276     {
277         wine_mmap_add_reserved_area( addr, size );
278         return;
279     }
280     if (size > granularity_mask + 1)
281     {
282         size_t new_size = (size / 2) & ~granularity_mask;
283         reserve_area( addr, (char *)addr + new_size );
284         reserve_area( (char *)addr + new_size, end );
285     }
286 #endif
287 }
288
289
290 /***********************************************************************
291  *           reserve_dos_area
292  *
293  * Reserve the DOS area (0x00000000-0x00110000).
294  */
295 static void reserve_dos_area(void)
296 {
297     const size_t page_size = getpagesize();
298     const size_t dos_area_size = 0x110000;
299     void *ptr;
300
301     /* first page has to be handled specially */
302     ptr = wine_anon_mmap( (void *)page_size, dos_area_size - page_size, PROT_NONE, MAP_NORESERVE );
303     if (ptr != (void *)page_size)
304     {
305         if (ptr != (void *)-1) munmap( ptr, dos_area_size - page_size );
306         return;
307     }
308     /* now add first page with MAP_FIXED */
309     wine_anon_mmap( NULL, page_size, PROT_NONE, MAP_NORESERVE|MAP_FIXED );
310     wine_mmap_add_reserved_area( NULL, dos_area_size );
311 }
312
313
314 /***********************************************************************
315  *           mmap_init
316  */
317 void mmap_init(void)
318 {
319     struct reserved_area *area;
320     struct list *ptr;
321 #if defined(__i386__) && !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__)  /* commented out until FreeBSD gets fixed */
322     char stack;
323     char * const stack_ptr = &stack;
324     char *user_space_limit = (char *)0x7ffe0000;
325
326     /* check for a reserved area starting at the user space limit */
327     /* to avoid wasting time trying to allocate it again */
328     LIST_FOR_EACH( ptr, &reserved_areas )
329     {
330         area = LIST_ENTRY( ptr, struct reserved_area, entry );
331         if ((char *)area->base > user_space_limit) break;
332         if ((char *)area->base + area->size > user_space_limit)
333         {
334             user_space_limit = (char *)area->base + area->size;
335             break;
336         }
337     }
338
339     if (stack_ptr >= user_space_limit)
340     {
341         char *base = stack_ptr - ((unsigned int)stack_ptr & granularity_mask) - (granularity_mask + 1);
342         if (base > user_space_limit) reserve_area( user_space_limit, base );
343         base = stack_ptr - ((unsigned int)stack_ptr & granularity_mask) + (granularity_mask + 1);
344 #ifdef linux
345         /* Linux heuristic: if the stack top is at c0000000, assume the address space */
346         /* ends there, this avoids a lot of futile allocation attempts */
347         if (base != (char *)0xc0000000)
348 #endif
349             reserve_area( base, 0 );
350     }
351     else reserve_area( user_space_limit, 0 );
352 #endif /* __i386__ */
353
354     /* reserve the DOS area if not already done */
355
356     ptr = list_head( &reserved_areas );
357     if (ptr)
358     {
359         area = LIST_ENTRY( ptr, struct reserved_area, entry );
360         if (!area->base) return;  /* already reserved */
361     }
362     reserve_dos_area();
363 }
364
365 #else /* HAVE_MMAP */
366
367 void *wine_anon_mmap( void *start, size_t size, int prot, int flags )
368 {
369     return (void *)-1;
370 }
371
372 static inline int munmap( void *ptr, size_t size )
373 {
374     return 0;
375 }
376
377 void mmap_init(void)
378 {
379 }
380
381 #endif
382
383 /***********************************************************************
384  *           wine_mmap_add_reserved_area
385  *
386  * Add an address range to the list of reserved areas.
387  * Caller must have made sure the range is not used by anything else.
388  *
389  * Note: the reserved areas functions are not reentrant, caller is
390  * responsible for proper locking.
391  */
392 void wine_mmap_add_reserved_area( void *addr, size_t size )
393 {
394     struct reserved_area *area;
395     struct list *ptr;
396
397     if (!((char *)addr + size)) size--;  /* avoid wrap-around */
398
399     LIST_FOR_EACH( ptr, &reserved_areas )
400     {
401         area = LIST_ENTRY( ptr, struct reserved_area, entry );
402         if (area->base > addr)
403         {
404             /* try to merge with the next one */
405             if ((char *)addr + size == (char *)area->base)
406             {
407                 area->base = addr;
408                 area->size += size;
409                 return;
410             }
411             break;
412         }
413         else if ((char *)area->base + area->size == (char *)addr)
414         {
415             /* merge with the previous one */
416             area->size += size;
417
418             /* try to merge with the next one too */
419             if ((ptr = list_next( &reserved_areas, ptr )))
420             {
421                 struct reserved_area *next = LIST_ENTRY( ptr, struct reserved_area, entry );
422                 if ((char *)addr + size == (char *)next->base)
423                 {
424                     area->size += next->size;
425                     list_remove( &next->entry );
426                     free( next );
427                 }
428             }
429             return;
430         }
431     }
432
433     if ((area = malloc( sizeof(*area) )))
434     {
435         area->base = addr;
436         area->size = size;
437         list_add_before( ptr, &area->entry );
438     }
439 }
440
441
442 /***********************************************************************
443  *           wine_mmap_remove_reserved_area
444  *
445  * Remove an address range from the list of reserved areas.
446  * If 'unmap' is non-zero the range is unmapped too.
447  *
448  * Note: the reserved areas functions are not reentrant, caller is
449  * responsible for proper locking.
450  */
451 void wine_mmap_remove_reserved_area( void *addr, size_t size, int unmap )
452 {
453     struct reserved_area *area;
454     struct list *ptr;
455
456     if (!((char *)addr + size)) size--;  /* avoid wrap-around */
457
458     ptr = list_head( &reserved_areas );
459     /* find the first area covering address */
460     while (ptr)
461     {
462         area = LIST_ENTRY( ptr, struct reserved_area, entry );
463         if ((char *)area->base >= (char *)addr + size) break;  /* outside the range */
464         if ((char *)area->base + area->size > (char *)addr)  /* overlaps range */
465         {
466             if (area->base >= addr)
467             {
468                 if ((char *)area->base + area->size > (char *)addr + size)
469                 {
470                     /* range overlaps beginning of area only -> shrink area */
471                     if (unmap) munmap( area->base, (char *)addr + size - (char *)area->base );
472                     area->size -= (char *)addr + size - (char *)area->base;
473                     area->base = (char *)addr + size;
474                     break;
475                 }
476                 else
477                 {
478                     /* range contains the whole area -> remove area completely */
479                     ptr = list_next( &reserved_areas, ptr );
480                     if (unmap) munmap( area->base, area->size );
481                     list_remove( &area->entry );
482                     free( area );
483                     continue;
484                 }
485             }
486             else
487             {
488                 if ((char *)area->base + area->size > (char *)addr + size)
489                 {
490                     /* range is in the middle of area -> split area in two */
491                     struct reserved_area *new_area = malloc( sizeof(*new_area) );
492                     if (new_area)
493                     {
494                         new_area->base = (char *)addr + size;
495                         new_area->size = (char *)area->base + area->size - (char *)new_area->base;
496                         list_add_after( ptr, &new_area->entry );
497                     }
498                     else size = (char *)area->base + area->size - (char *)addr;
499                     area->size = (char *)addr - (char *)area->base;
500                     if (unmap) munmap( addr, size );
501                     break;
502                 }
503                 else
504                 {
505                     /* range overlaps end of area only -> shrink area */
506                     if (unmap) munmap( addr, (char *)area->base + area->size - (char *)addr );
507                     area->size = (char *)addr - (char *)area->base;
508                 }
509             }
510         }
511         ptr = list_next( &reserved_areas, ptr );
512     }
513 }
514
515
516 /***********************************************************************
517  *           wine_mmap_is_in_reserved_area
518  *
519  * Check if the specified range is included in a reserved area.
520  * Returns 1 if range is fully included, 0 if range is not included
521  * at all, and -1 if it is only partially included.
522  *
523  * Note: the reserved areas functions are not reentrant, caller is
524  * responsible for proper locking.
525  */
526 int wine_mmap_is_in_reserved_area( void *addr, size_t size )
527 {
528     struct reserved_area *area;
529     struct list *ptr;
530
531     LIST_FOR_EACH( ptr, &reserved_areas )
532     {
533         area = LIST_ENTRY( ptr, struct reserved_area, entry );
534         if (area->base > addr) break;
535         if ((char *)area->base + area->size <= (char *)addr) continue;
536         /* area must contain block completely */
537         if ((char *)area->base + area->size < (char *)addr + size) return -1;
538         return 1;
539     }
540     return 0;
541 }