mmap.c revision 589fd26cedb2b4ebbed14f2964cad03cc8ebbca2
/*
* Wine memory mappings support
*
* Copyright 2000, 2004 Alexandre Julliard
*
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
/*
* Sun LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
* other than GPL or LGPL is available it will apply instead, Sun elects to use only
* the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
* a choice of LGPL license versions is made available with the language indicating
* that LGPLv2 or any later version may be used, or where a choice of which version
* of the LGPL is applied is otherwise unspecified.
*/
#include "config.h"
#include <assert.h>
#include <ctype.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef HAVE_SYS_MMAN_H
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef HAVE_STDINT_H
# include <stdint.h>
#endif
#ifdef HAVE_MMAP
struct reserved_area
{
void *base;
};
#ifndef MAP_NORESERVE
#define MAP_NORESERVE 0
#endif
#ifndef MAP_PRIVATE
#define MAP_PRIVATE 0
#endif
#ifndef MAP_ANON
#define MAP_ANON 0
#endif
static inline int get_fdzero(void)
{
static int fd = -1;
{
{
exit(1);
}
}
return fd;
}
/***********************************************************************
* try_mmap_fixed
*
* The purpose of this routine is to emulate the behaviour of
* the Linux mmap() routine if a non-NULL address is passed,
* but the MAP_FIXED flag is not set. Linux in this case tries
* to place the mapping at the specified address, *unless* the
* range is already in use. Solaris, however, completely ignores
* the address argument in this case.
*
* As Wine code occasionally relies on the Linux behaviour, e.g. to
* be able to map non-relocatable PE executables to their proper
* start addresses, or to map the DOS memory to 0, this routine
* emulates the Linux behaviour by checking whether the desired
* address range is still available, and placing the mapping there
* using MAP_FIXED if so.
*/
{
int pagesize = getpagesize();
/* We only try to map to a fixed address if
addr is non-NULL and properly aligned,
and MAP_FIXED isn't already specified. */
if ( !addr )
return 0;
return 0;
return 0;
/* We use vfork() to freeze all threads of the
current process. This allows us to check without
race condition whether the desired memory range is
already in use. Note that because vfork() shares
the address spaces between parent and child, we
can actually perform the mapping in the child. */
{
perror("try_mmap_fixed: vfork");
exit(1);
}
if ( pid == 0 )
{
int i;
char vec;
/* We call mincore() for every page in the desired range.
If any of these calls succeeds, the page is already
mapped and we must fail. */
_exit(1);
/* Perform the mapping with MAP_FIXED set. This is safe
now, as none of the pages is currently in use. */
_exit(0);
_exit(1);
}
/* vfork() lets the parent continue only after the child
has exited. Furthermore, Wine sets SIGCHLD to SIG_IGN,
so we don't need to wait for the child. */
}
#include <mach/mach_init.h>
/*
* On Darwin, we can use the Mach call vm_allocate to allocate
* anonymous memory at the specified address, and then use mmap with
* MAP_FIXED to replace the mapping.
*/
{
{
return 1;
}
return 0;
}
#endif /* (__svr4__ || __NetBSD__) && !MAP_TRYFIXED */
/***********************************************************************
* wine_anon_mmap
*
* Portable wrapper for anonymous mmaps
*/
{
#ifdef MAP_SHARED
flags &= ~MAP_SHARED;
#endif
/* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
{
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
/* Even FreeBSD 5.3 does not properly support NULL here. */
#endif
#ifdef MAP_TRYFIXED
/* If available, this will attempt a fixed mapping in-kernel */
flags |= MAP_TRYFIXED;
return start;
#endif
}
}
/***********************************************************************
* mmap_reserve
*
* mmap wrapper used for reservations, only maps the specified address
*/
{
void *ptr;
#ifdef MAP_TRYFIXED
flags |= MAP_TRYFIXED;
#endif
}
/***********************************************************************
* reserve_area
*
* Reserve as much memory as possible in the given area.
*/
#ifdef __i386__
{
/* try_mmap_fixed is inefficient when using vfork, so we need a different algorithm here */
/* we assume no other thread is running at this point */
char vec;
while (size)
{
i &= ~granularity_mask;
get_fdzero(), 0 ) != (void *)-1)
wine_mmap_add_reserved_area( addr, i );
i += granularity_mask + 1;
}
#else
if (!size) return;
{
return;
}
{
}
#endif
}
/***********************************************************************
* reserve_malloc_space
*
* Solaris malloc is not smart enough to obtain space through mmap(), so try to make
* sure that there is some available sbrk() space before we reserve other things.
*/
{
#ifdef __sun
if (!ptrs) return;
if (i--) /* free everything except the last one */
#endif
}
#endif /* __i386__ */
/***********************************************************************
* reserve_dos_area
*
* Reserve the DOS area (0x00000000-0x00110000).
*/
static void reserve_dos_area(void)
{
void *ptr;
/* first page has to be handled specially */
{
return;
}
/* now add first page with MAP_FIXED */
}
/***********************************************************************
* mmap_init
*/
void mmap_init(void)
{
struct reserved_area *area;
#ifdef __i386__
char stack;
char *user_space_limit = (char *)0x7ffe0000;
/* check for a reserved area starting at the user space limit */
/* to avoid wasting time trying to allocate it again */
{
{
break;
}
}
if (stack_ptr >= user_space_limit)
{
char *end = 0;
#if defined(linux) || defined(__FreeBSD__)
/* Heuristic: assume the stack is near the end of the address */
/* space, this avoids a lot of futile allocation attempts */
#endif
}
else reserve_area( user_space_limit, 0 );
#endif /* __i386__ */
/* reserve the DOS area if not already done */
if (ptr)
{
}
}
/***********************************************************************
* wine_mmap_add_reserved_area
*
* Add an address range to the list of reserved areas.
* Caller must have made sure the range is not used by anything else.
*
* Note: the reserved areas functions are not reentrant, caller is
* responsible for proper locking.
*/
{
struct reserved_area *area;
{
{
/* try to merge with the next one */
{
return;
}
break;
}
{
/* merge with the previous one */
/* try to merge with the next one too */
{
{
}
}
return;
}
}
{
}
}
/***********************************************************************
* wine_mmap_remove_reserved_area
*
* Remove an address range from the list of reserved areas.
* If 'unmap' is non-zero the range is unmapped too.
*
* Note: the reserved areas functions are not reentrant, caller is
* responsible for proper locking.
*/
{
struct reserved_area *area;
/* find the first area covering address */
while (ptr)
{
{
{
{
/* range overlaps beginning of area only -> shrink area */
break;
}
else
{
/* range contains the whole area -> remove area completely */
continue;
}
}
else
{
{
/* range is in the middle of area -> split area in two */
if (new_area)
{
}
break;
}
else
{
/* range overlaps end of area only -> shrink area */
}
}
}
}
}
/***********************************************************************
* wine_mmap_is_in_reserved_area
*
* Check if the specified range is included in a reserved area.
* Returns 1 if range is fully included, 0 if range is not included
* at all, and -1 if it is only partially included.
*
* Note: the reserved areas functions are not reentrant, caller is
* responsible for proper locking.
*/
{
struct reserved_area *area;
{
/* area must contain block completely */
return 1;
}
return 0;
}
/***********************************************************************
* wine_mmap_enum_reserved_areas
*
* Enumerate the list of reserved areas, sorted by addresses.
* If enum_func returns a non-zero value, enumeration is stopped and the value is returned.
*
* Note: the reserved areas functions are not reentrant, caller is
* responsible for proper locking.
*/
int top_down )
{
int ret = 0;
if (top_down)
{
{
}
}
else
{
{
}
}
return ret;
}
#else /* HAVE_MMAP */
void mmap_init(void)
{
}
#endif