2002-09-17 20:54:42 +02:00
|
|
|
/*
|
|
|
|
* Win32 virtual memory functions
|
|
|
|
*
|
|
|
|
* Copyright 1997, 2002 Alexandre Julliard
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* 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
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2002-09-17 20:54:42 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "wine/port.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#ifdef HAVE_SYS_ERRNO_H
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#endif
|
|
|
|
#include <fcntl.h>
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
2005-06-06 22:13:08 +02:00
|
|
|
#include <stdarg.h>
|
2002-09-17 20:54:42 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
2006-02-07 21:17:45 +01:00
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
|
# include <sys/stat.h>
|
|
|
|
#endif
|
2002-09-17 20:54:42 +02:00
|
|
|
#ifdef HAVE_SYS_MMAN_H
|
2006-02-07 21:17:45 +01:00
|
|
|
# include <sys/mman.h>
|
2002-09-17 20:54:42 +02:00
|
|
|
#endif
|
2008-07-24 19:21:33 +02:00
|
|
|
#ifdef HAVE_VALGRIND_VALGRIND_H
|
|
|
|
# include <valgrind/valgrind.h>
|
2008-04-01 14:11:44 +02:00
|
|
|
#endif
|
2002-09-17 20:54:42 +02:00
|
|
|
|
2003-01-07 21:36:20 +01:00
|
|
|
#define NONAMELESSUNION
|
|
|
|
#define NONAMELESSSTRUCT
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "ntstatus.h"
|
2005-11-28 17:32:54 +01:00
|
|
|
#define WIN32_NO_STATUS
|
2005-06-06 22:13:08 +02:00
|
|
|
#include "windef.h"
|
2002-09-17 20:54:42 +02:00
|
|
|
#include "winternl.h"
|
|
|
|
#include "wine/library.h"
|
|
|
|
#include "wine/server.h"
|
2009-01-14 20:17:52 +01:00
|
|
|
#include "wine/exception.h"
|
2004-05-21 22:58:44 +02:00
|
|
|
#include "wine/list.h"
|
2002-09-17 20:54:42 +02:00
|
|
|
#include "wine/debug.h"
|
2003-10-08 21:11:08 +02:00
|
|
|
#include "ntdll_misc.h"
|
2002-09-17 20:54:42 +02:00
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(virtual);
|
|
|
|
WINE_DECLARE_DEBUG_CHANNEL(module);
|
|
|
|
|
|
|
|
#ifndef MS_SYNC
|
|
|
|
#define MS_SYNC 0
|
|
|
|
#endif
|
|
|
|
|
2004-05-25 03:29:24 +02:00
|
|
|
#ifndef MAP_NORESERVE
|
|
|
|
#define MAP_NORESERVE 0
|
|
|
|
#endif
|
|
|
|
|
2002-09-17 20:54:42 +02:00
|
|
|
/* File view */
|
2004-05-21 22:58:44 +02:00
|
|
|
typedef struct file_view
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2004-05-21 22:58:44 +02:00
|
|
|
struct list entry; /* Entry in global view list */
|
2002-09-17 20:54:42 +02:00
|
|
|
void *base; /* Base address */
|
2005-07-15 12:01:30 +02:00
|
|
|
size_t size; /* Size in bytes */
|
2002-09-17 20:54:42 +02:00
|
|
|
HANDLE mapping; /* Handle to the file mapping */
|
2008-11-04 13:05:32 +01:00
|
|
|
unsigned int protect; /* Protection for all pages at allocation time */
|
2002-09-17 20:54:42 +02:00
|
|
|
BYTE prot[1]; /* Protection byte for each page */
|
|
|
|
} FILE_VIEW;
|
|
|
|
|
|
|
|
|
|
|
|
/* Conversion from VPROT_* to Win32 flags */
|
|
|
|
static const BYTE VIRTUAL_Win32Flags[16] =
|
|
|
|
{
|
|
|
|
PAGE_NOACCESS, /* 0 */
|
|
|
|
PAGE_READONLY, /* READ */
|
|
|
|
PAGE_READWRITE, /* WRITE */
|
|
|
|
PAGE_READWRITE, /* READ | WRITE */
|
|
|
|
PAGE_EXECUTE, /* EXEC */
|
|
|
|
PAGE_EXECUTE_READ, /* READ | EXEC */
|
|
|
|
PAGE_EXECUTE_READWRITE, /* WRITE | EXEC */
|
|
|
|
PAGE_EXECUTE_READWRITE, /* READ | WRITE | EXEC */
|
|
|
|
PAGE_WRITECOPY, /* WRITECOPY */
|
|
|
|
PAGE_WRITECOPY, /* READ | WRITECOPY */
|
|
|
|
PAGE_WRITECOPY, /* WRITE | WRITECOPY */
|
|
|
|
PAGE_WRITECOPY, /* READ | WRITE | WRITECOPY */
|
|
|
|
PAGE_EXECUTE_WRITECOPY, /* EXEC | WRITECOPY */
|
|
|
|
PAGE_EXECUTE_WRITECOPY, /* READ | EXEC | WRITECOPY */
|
|
|
|
PAGE_EXECUTE_WRITECOPY, /* WRITE | EXEC | WRITECOPY */
|
|
|
|
PAGE_EXECUTE_WRITECOPY /* READ | WRITE | EXEC | WRITECOPY */
|
|
|
|
};
|
|
|
|
|
2004-05-21 22:58:44 +02:00
|
|
|
static struct list views_list = LIST_INIT(views_list);
|
2003-08-13 01:50:54 +02:00
|
|
|
|
2005-06-25 20:00:57 +02:00
|
|
|
static RTL_CRITICAL_SECTION csVirtual;
|
|
|
|
static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
|
2003-08-13 01:50:54 +02:00
|
|
|
{
|
|
|
|
0, 0, &csVirtual,
|
|
|
|
{ &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
|
2005-09-09 12:19:44 +02:00
|
|
|
0, 0, { (DWORD_PTR)(__FILE__ ": csVirtual") }
|
2003-08-13 01:50:54 +02:00
|
|
|
};
|
2005-06-25 20:00:57 +02:00
|
|
|
static RTL_CRITICAL_SECTION csVirtual = { &critsect_debug, -1, 0, 0, 0, 0 };
|
2002-09-17 20:54:42 +02:00
|
|
|
|
|
|
|
#ifdef __i386__
|
|
|
|
/* These are always the same on an i386, and it will be faster this way */
|
|
|
|
# define page_mask 0xfff
|
|
|
|
# define page_shift 12
|
|
|
|
# define page_size 0x1000
|
2004-05-25 03:29:24 +02:00
|
|
|
/* Note: these are Windows limits, you cannot change them. */
|
2008-10-28 13:51:36 +01:00
|
|
|
static void *address_space_limit = (void *)0xc0000000; /* top of the total available address space */
|
|
|
|
static void *user_space_limit = (void *)0x7fff0000; /* top of the user address space */
|
2008-11-06 11:42:58 +01:00
|
|
|
static void *working_set_limit = (void *)0x7fff0000; /* top of the current working set */
|
2002-09-17 20:54:42 +02:00
|
|
|
#else
|
|
|
|
static UINT page_shift;
|
2009-01-08 17:28:45 +01:00
|
|
|
static UINT_PTR page_size;
|
2005-09-16 20:54:19 +02:00
|
|
|
static UINT_PTR page_mask;
|
2008-11-07 11:04:07 +01:00
|
|
|
static void *address_space_limit;
|
|
|
|
static void *user_space_limit;
|
|
|
|
static void *working_set_limit;
|
2002-09-17 20:54:42 +02:00
|
|
|
#endif /* __i386__ */
|
|
|
|
|
|
|
|
#define ROUND_ADDR(addr,mask) \
|
2005-09-16 20:54:19 +02:00
|
|
|
((void *)((UINT_PTR)(addr) & ~(UINT_PTR)(mask)))
|
2002-09-17 20:54:42 +02:00
|
|
|
|
|
|
|
#define ROUND_SIZE(addr,size) \
|
2008-11-14 17:40:54 +01:00
|
|
|
(((SIZE_T)(size) + ((UINT_PTR)(addr) & page_mask) + page_mask) & ~page_mask)
|
2002-09-17 20:54:42 +02:00
|
|
|
|
|
|
|
#define VIRTUAL_DEBUG_DUMP_VIEW(view) \
|
2005-09-26 11:57:38 +02:00
|
|
|
do { if (TRACE_ON(virtual)) VIRTUAL_DumpView(view); } while (0)
|
2002-09-17 20:54:42 +02:00
|
|
|
|
2008-10-31 13:00:59 +01:00
|
|
|
#define VIRTUAL_HEAP_SIZE (4*1024*1024)
|
|
|
|
|
|
|
|
static HANDLE virtual_heap;
|
2006-07-24 14:00:19 +02:00
|
|
|
static void *preload_reserve_start;
|
|
|
|
static void *preload_reserve_end;
|
2006-07-24 15:19:32 +02:00
|
|
|
static int use_locks;
|
2006-12-05 15:42:29 +01:00
|
|
|
static int force_exec_prot; /* whether to force PROT_EXEC on all PROT_READ mmaps */
|
2004-06-18 02:26:57 +02:00
|
|
|
|
2002-09-17 20:54:42 +02:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* VIRTUAL_GetProtStr
|
|
|
|
*/
|
|
|
|
static const char *VIRTUAL_GetProtStr( BYTE prot )
|
|
|
|
{
|
|
|
|
static char buffer[6];
|
|
|
|
buffer[0] = (prot & VPROT_COMMITTED) ? 'c' : '-';
|
|
|
|
buffer[1] = (prot & VPROT_GUARD) ? 'g' : '-';
|
|
|
|
buffer[2] = (prot & VPROT_READ) ? 'r' : '-';
|
2004-05-21 22:58:44 +02:00
|
|
|
buffer[3] = (prot & VPROT_WRITECOPY) ? 'W' : ((prot & VPROT_WRITE) ? 'w' : '-');
|
2002-09-17 20:54:42 +02:00
|
|
|
buffer[4] = (prot & VPROT_EXEC) ? 'x' : '-';
|
|
|
|
buffer[5] = 0;
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-05 15:42:29 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* VIRTUAL_GetUnixProt
|
|
|
|
*
|
|
|
|
* Convert page protections to protection for mmap/mprotect.
|
|
|
|
*/
|
|
|
|
static int VIRTUAL_GetUnixProt( BYTE vprot )
|
|
|
|
{
|
|
|
|
int prot = 0;
|
|
|
|
if ((vprot & VPROT_COMMITTED) && !(vprot & VPROT_GUARD))
|
|
|
|
{
|
|
|
|
if (vprot & VPROT_READ) prot |= PROT_READ;
|
|
|
|
if (vprot & VPROT_WRITE) prot |= PROT_WRITE;
|
|
|
|
if (vprot & VPROT_WRITECOPY) prot |= PROT_WRITE;
|
|
|
|
if (vprot & VPROT_EXEC) prot |= PROT_EXEC;
|
2008-11-25 12:07:35 +01:00
|
|
|
if (vprot & VPROT_WRITEWATCH) prot &= ~PROT_WRITE;
|
2006-12-05 15:42:29 +01:00
|
|
|
}
|
|
|
|
if (!prot) prot = PROT_NONE;
|
|
|
|
return prot;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-17 20:54:42 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* VIRTUAL_DumpView
|
|
|
|
*/
|
|
|
|
static void VIRTUAL_DumpView( FILE_VIEW *view )
|
|
|
|
{
|
|
|
|
UINT i, count;
|
|
|
|
char *addr = view->base;
|
|
|
|
BYTE prot = view->prot[0];
|
|
|
|
|
2005-09-25 17:23:21 +02:00
|
|
|
TRACE( "View: %p - %p", addr, addr + view->size - 1 );
|
2008-11-04 13:05:32 +01:00
|
|
|
if (view->protect & VPROT_SYSTEM)
|
2005-09-25 17:23:21 +02:00
|
|
|
TRACE( " (system)\n" );
|
2008-11-04 13:05:32 +01:00
|
|
|
else if (view->protect & VPROT_VALLOC)
|
2005-09-25 17:23:21 +02:00
|
|
|
TRACE( " (valloc)\n" );
|
2002-09-17 20:54:42 +02:00
|
|
|
else if (view->mapping)
|
2005-09-25 17:23:21 +02:00
|
|
|
TRACE( " %p\n", view->mapping );
|
2002-09-17 20:54:42 +02:00
|
|
|
else
|
2005-09-25 17:23:21 +02:00
|
|
|
TRACE( " (anonymous)\n");
|
2002-09-17 20:54:42 +02:00
|
|
|
|
|
|
|
for (count = i = 1; i < view->size >> page_shift; i++, count++)
|
|
|
|
{
|
|
|
|
if (view->prot[i] == prot) continue;
|
2005-09-25 17:23:21 +02:00
|
|
|
TRACE( " %p - %p %s\n",
|
2002-09-17 20:54:42 +02:00
|
|
|
addr, addr + (count << page_shift) - 1, VIRTUAL_GetProtStr(prot) );
|
|
|
|
addr += (count << page_shift);
|
|
|
|
prot = view->prot[i];
|
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
if (count)
|
2005-09-25 17:23:21 +02:00
|
|
|
TRACE( " %p - %p %s\n",
|
2002-09-17 20:54:42 +02:00
|
|
|
addr, addr + (count << page_shift) - 1, VIRTUAL_GetProtStr(prot) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* VIRTUAL_Dump
|
|
|
|
*/
|
2006-06-20 18:53:45 +02:00
|
|
|
#if WINE_VM_DEBUG
|
|
|
|
static void VIRTUAL_Dump(void)
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2007-01-12 20:15:52 +01:00
|
|
|
sigset_t sigset;
|
2005-05-23 11:51:02 +02:00
|
|
|
struct file_view *view;
|
2004-05-21 22:58:44 +02:00
|
|
|
|
2005-09-25 17:23:21 +02:00
|
|
|
TRACE( "Dump of all virtual memory views:\n" );
|
2007-01-12 20:15:52 +01:00
|
|
|
server_enter_uninterrupted_section( &csVirtual, &sigset );
|
2005-05-23 11:51:02 +02:00
|
|
|
LIST_FOR_EACH_ENTRY( view, &views_list, FILE_VIEW, entry )
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2005-05-23 11:51:02 +02:00
|
|
|
VIRTUAL_DumpView( view );
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
2007-01-12 20:15:52 +01:00
|
|
|
server_leave_uninterrupted_section( &csVirtual, &sigset );
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
2006-06-20 18:53:45 +02:00
|
|
|
#endif
|
2002-09-17 20:54:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* VIRTUAL_FindView
|
|
|
|
*
|
2004-04-06 22:16:51 +02:00
|
|
|
* Find the view containing a given address. The csVirtual section must be held by caller.
|
2002-09-17 20:54:42 +02:00
|
|
|
*
|
2005-05-18 11:45:12 +02:00
|
|
|
* PARAMS
|
|
|
|
* addr [I] Address
|
|
|
|
*
|
2002-09-17 20:54:42 +02:00
|
|
|
* RETURNS
|
|
|
|
* View: Success
|
|
|
|
* NULL: Failure
|
|
|
|
*/
|
2008-11-25 11:58:50 +01:00
|
|
|
static struct file_view *VIRTUAL_FindView( const void *addr, size_t size )
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2005-05-23 11:51:02 +02:00
|
|
|
struct file_view *view;
|
2002-09-17 20:54:42 +02:00
|
|
|
|
2005-05-23 11:51:02 +02:00
|
|
|
LIST_FOR_EACH_ENTRY( view, &views_list, struct file_view, entry )
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2008-11-25 11:58:50 +01:00
|
|
|
if (view->base > addr) break; /* no matching view */
|
|
|
|
if ((const char *)view->base + view->size <= (const char *)addr) continue;
|
|
|
|
if ((const char *)view->base + view->size < (const char *)addr + size) break; /* size too large */
|
|
|
|
if ((const char *)addr + size < (const char *)addr) break; /* overflow */
|
|
|
|
return view;
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
2004-05-21 22:58:44 +02:00
|
|
|
return NULL;
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-13 20:36:41 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* get_mask
|
|
|
|
*/
|
|
|
|
static inline UINT_PTR get_mask( ULONG zero_bits )
|
|
|
|
{
|
|
|
|
if (!zero_bits) return 0xffff; /* allocations are aligned to 64K by default */
|
|
|
|
if (zero_bits < page_shift) zero_bits = page_shift;
|
|
|
|
return (1 << zero_bits) - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-16 05:52:17 +01:00
|
|
|
/***********************************************************************
|
2004-05-25 03:29:24 +02:00
|
|
|
* find_view_range
|
2004-04-06 22:16:51 +02:00
|
|
|
*
|
2004-05-25 03:29:24 +02:00
|
|
|
* Find the first view overlapping at least part of the specified range.
|
|
|
|
* The csVirtual section must be held by caller.
|
|
|
|
*/
|
|
|
|
static struct file_view *find_view_range( const void *addr, size_t size )
|
|
|
|
{
|
2005-05-23 11:51:02 +02:00
|
|
|
struct file_view *view;
|
2004-05-25 03:29:24 +02:00
|
|
|
|
2005-05-23 11:51:02 +02:00
|
|
|
LIST_FOR_EACH_ENTRY( view, &views_list, struct file_view, entry )
|
2004-05-25 03:29:24 +02:00
|
|
|
{
|
2004-06-15 02:47:00 +02:00
|
|
|
if ((const char *)view->base >= (const char *)addr + size) break;
|
|
|
|
if ((const char *)view->base + view->size > (const char *)addr) return view;
|
2004-05-25 03:29:24 +02:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-24 14:00:19 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* find_free_area
|
|
|
|
*
|
|
|
|
* Find a free area between views inside the specified range.
|
|
|
|
* The csVirtual section must be held by caller.
|
|
|
|
*/
|
|
|
|
static void *find_free_area( void *base, void *end, size_t size, size_t mask, int top_down )
|
|
|
|
{
|
|
|
|
struct list *ptr;
|
|
|
|
void *start;
|
|
|
|
|
|
|
|
if (top_down)
|
|
|
|
{
|
|
|
|
start = ROUND_ADDR( (char *)end - size, mask );
|
|
|
|
if (start >= end || start < base) return NULL;
|
|
|
|
|
|
|
|
for (ptr = views_list.prev; ptr != &views_list; ptr = ptr->prev)
|
|
|
|
{
|
|
|
|
struct file_view *view = LIST_ENTRY( ptr, struct file_view, entry );
|
|
|
|
|
|
|
|
if ((char *)view->base + view->size <= (char *)start) break;
|
|
|
|
if ((char *)view->base >= (char *)start + size) continue;
|
|
|
|
start = ROUND_ADDR( (char *)view->base - size, mask );
|
|
|
|
/* stop if remaining space is not large enough */
|
|
|
|
if (!start || start >= end || start < base) return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
start = ROUND_ADDR( (char *)base + mask, mask );
|
|
|
|
if (start >= end || (char *)end - (char *)start < size) return NULL;
|
|
|
|
|
|
|
|
for (ptr = views_list.next; ptr != &views_list; ptr = ptr->next)
|
|
|
|
{
|
|
|
|
struct file_view *view = LIST_ENTRY( ptr, struct file_view, entry );
|
|
|
|
|
|
|
|
if ((char *)view->base >= (char *)start + size) break;
|
|
|
|
if ((char *)view->base + view->size <= (char *)start) continue;
|
|
|
|
start = ROUND_ADDR( (char *)view->base + view->size + mask, mask );
|
|
|
|
/* stop if remaining space is not large enough */
|
|
|
|
if (!start || start >= end || (char *)end - (char *)start < size) return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-25 03:29:24 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* add_reserved_area
|
2004-01-16 05:52:17 +01:00
|
|
|
*
|
2004-05-25 03:29:24 +02:00
|
|
|
* Add a reserved area to the list maintained by libwine.
|
|
|
|
* The csVirtual section must be held by caller.
|
|
|
|
*/
|
|
|
|
static void add_reserved_area( void *addr, size_t size )
|
|
|
|
{
|
|
|
|
TRACE( "adding %p-%p\n", addr, (char *)addr + size );
|
|
|
|
|
2004-06-18 02:26:57 +02:00
|
|
|
if (addr < user_space_limit)
|
2004-05-25 03:29:24 +02:00
|
|
|
{
|
|
|
|
/* unmap the part of the area that is below the limit */
|
2004-06-18 02:26:57 +02:00
|
|
|
assert( (char *)addr + size > (char *)user_space_limit );
|
|
|
|
munmap( addr, (char *)user_space_limit - (char *)addr );
|
|
|
|
size -= (char *)user_space_limit - (char *)addr;
|
|
|
|
addr = user_space_limit;
|
2004-05-25 03:29:24 +02:00
|
|
|
}
|
2004-05-28 05:48:09 +02:00
|
|
|
/* blow away existing mappings */
|
|
|
|
wine_anon_mmap( addr, size, PROT_NONE, MAP_NORESERVE | MAP_FIXED );
|
2004-05-25 03:29:24 +02:00
|
|
|
wine_mmap_add_reserved_area( addr, size );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* is_beyond_limit
|
|
|
|
*
|
|
|
|
* Check if an address range goes beyond a given limit.
|
|
|
|
*/
|
2007-06-27 21:59:51 +02:00
|
|
|
static inline int is_beyond_limit( const void *addr, size_t size, const void *limit )
|
2004-05-25 03:29:24 +02:00
|
|
|
{
|
2008-11-07 11:04:07 +01:00
|
|
|
return (addr >= limit || (const char *)addr + size > (const char *)limit);
|
2004-05-25 03:29:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* unmap_area
|
|
|
|
*
|
|
|
|
* Unmap an area, or simply replace it by an empty mapping if it is
|
|
|
|
* in a reserved area. The csVirtual section must be held by caller.
|
|
|
|
*/
|
|
|
|
static inline void unmap_area( void *addr, size_t size )
|
|
|
|
{
|
|
|
|
if (wine_mmap_is_in_reserved_area( addr, size ))
|
|
|
|
wine_anon_mmap( addr, size, PROT_NONE, MAP_NORESERVE | MAP_FIXED );
|
2005-12-01 18:44:04 +01:00
|
|
|
else if (is_beyond_limit( addr, size, user_space_limit ))
|
|
|
|
add_reserved_area( addr, size );
|
2004-05-25 03:29:24 +02:00
|
|
|
else
|
|
|
|
munmap( addr, size );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* delete_view
|
|
|
|
*
|
|
|
|
* Deletes a view. The csVirtual section must be held by caller.
|
2004-01-16 05:52:17 +01:00
|
|
|
*/
|
2004-05-25 03:29:24 +02:00
|
|
|
static void delete_view( struct file_view *view ) /* [in] View */
|
2004-01-16 05:52:17 +01:00
|
|
|
{
|
2008-11-04 13:05:32 +01:00
|
|
|
if (!(view->protect & VPROT_SYSTEM)) unmap_area( view->base, view->size );
|
2004-05-21 22:58:44 +02:00
|
|
|
list_remove( &view->entry );
|
2004-01-16 05:52:17 +01:00
|
|
|
if (view->mapping) NtClose( view->mapping );
|
2008-10-31 13:00:59 +01:00
|
|
|
RtlFreeHeap( virtual_heap, 0, view );
|
2004-01-16 05:52:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-17 20:54:42 +02:00
|
|
|
/***********************************************************************
|
2004-05-21 22:58:44 +02:00
|
|
|
* create_view
|
2002-09-17 20:54:42 +02:00
|
|
|
*
|
2004-05-21 22:58:44 +02:00
|
|
|
* Create a view. The csVirtual section must be held by caller.
|
2002-09-17 20:54:42 +02:00
|
|
|
*/
|
2008-11-04 13:05:32 +01:00
|
|
|
static NTSTATUS create_view( struct file_view **view_ret, void *base, size_t size, unsigned int vprot )
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2004-05-21 22:58:44 +02:00
|
|
|
struct file_view *view;
|
|
|
|
struct list *ptr;
|
2006-12-05 15:42:29 +01:00
|
|
|
int unix_prot = VIRTUAL_GetUnixProt( vprot );
|
2002-09-17 20:54:42 +02:00
|
|
|
|
2005-09-12 17:14:06 +02:00
|
|
|
assert( !((UINT_PTR)base & page_mask) );
|
2002-09-17 20:54:42 +02:00
|
|
|
assert( !(size & page_mask) );
|
2004-05-21 22:58:44 +02:00
|
|
|
|
|
|
|
/* Create the view structure */
|
|
|
|
|
2008-10-31 13:00:59 +01:00
|
|
|
if (!(view = RtlAllocateHeap( virtual_heap, 0, sizeof(*view) + (size >> page_shift) - 1 )))
|
|
|
|
{
|
|
|
|
FIXME( "out of memory in virtual heap for %p-%p\n", base, (char *)base + size );
|
|
|
|
return STATUS_NO_MEMORY;
|
|
|
|
}
|
2004-05-21 22:58:44 +02:00
|
|
|
|
2002-09-17 20:54:42 +02:00
|
|
|
view->base = base;
|
2004-05-21 22:58:44 +02:00
|
|
|
view->size = size;
|
|
|
|
view->mapping = 0;
|
2002-09-17 20:54:42 +02:00
|
|
|
view->protect = vprot;
|
2008-11-04 13:05:32 +01:00
|
|
|
memset( view->prot, vprot, size >> page_shift );
|
2002-09-17 20:54:42 +02:00
|
|
|
|
2004-05-21 22:58:44 +02:00
|
|
|
/* Insert it in the linked list */
|
2002-09-17 20:54:42 +02:00
|
|
|
|
2004-05-21 22:58:44 +02:00
|
|
|
LIST_FOR_EACH( ptr, &views_list )
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2004-05-21 22:58:44 +02:00
|
|
|
struct file_view *next = LIST_ENTRY( ptr, struct file_view, entry );
|
|
|
|
if (next->base > base) break;
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
2004-05-21 22:58:44 +02:00
|
|
|
list_add_before( ptr, &view->entry );
|
2002-09-17 20:54:42 +02:00
|
|
|
|
2004-05-21 22:58:44 +02:00
|
|
|
/* Check for overlapping views. This can happen if the previous view
|
|
|
|
* was a system view that got unmapped behind our back. In that case
|
|
|
|
* we recover by simply deleting it. */
|
2002-09-17 20:54:42 +02:00
|
|
|
|
2004-05-21 22:58:44 +02:00
|
|
|
if ((ptr = list_prev( &views_list, &view->entry )) != NULL)
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2004-05-21 22:58:44 +02:00
|
|
|
struct file_view *prev = LIST_ENTRY( ptr, struct file_view, entry );
|
2004-01-16 05:52:17 +01:00
|
|
|
if ((char *)prev->base + prev->size > (char *)base)
|
|
|
|
{
|
|
|
|
TRACE( "overlapping prev view %p-%p for %p-%p\n",
|
|
|
|
prev->base, (char *)prev->base + prev->size,
|
|
|
|
base, (char *)base + view->size );
|
2008-11-04 13:05:32 +01:00
|
|
|
assert( prev->protect & VPROT_SYSTEM );
|
2004-05-25 03:29:24 +02:00
|
|
|
delete_view( prev );
|
2004-01-16 05:52:17 +01:00
|
|
|
}
|
|
|
|
}
|
2004-05-21 22:58:44 +02:00
|
|
|
if ((ptr = list_next( &views_list, &view->entry )) != NULL)
|
2004-01-16 05:52:17 +01:00
|
|
|
{
|
2004-05-21 22:58:44 +02:00
|
|
|
struct file_view *next = LIST_ENTRY( ptr, struct file_view, entry );
|
|
|
|
if ((char *)base + view->size > (char *)next->base)
|
|
|
|
{
|
|
|
|
TRACE( "overlapping next view %p-%p for %p-%p\n",
|
|
|
|
next->base, (char *)next->base + next->size,
|
|
|
|
base, (char *)base + view->size );
|
2008-11-04 13:05:32 +01:00
|
|
|
assert( next->protect & VPROT_SYSTEM );
|
2004-05-25 03:29:24 +02:00
|
|
|
delete_view( next );
|
2004-05-21 22:58:44 +02:00
|
|
|
}
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
2004-05-21 22:58:44 +02:00
|
|
|
|
|
|
|
*view_ret = view;
|
2002-09-17 20:54:42 +02:00
|
|
|
VIRTUAL_DEBUG_DUMP_VIEW( view );
|
|
|
|
|
2008-11-04 13:16:01 +01:00
|
|
|
if (force_exec_prot && !(vprot & VPROT_NOEXEC) && (unix_prot & PROT_READ) && !(unix_prot & PROT_EXEC))
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2006-12-05 15:42:29 +01:00
|
|
|
TRACE( "forcing exec permission on %p-%p\n", base, (char *)base + size - 1 );
|
|
|
|
mprotect( base, size, unix_prot | PROT_EXEC );
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
2006-12-05 15:42:29 +01:00
|
|
|
return STATUS_SUCCESS;
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* VIRTUAL_GetWin32Prot
|
|
|
|
*
|
|
|
|
* Convert page protections to Win32 flags.
|
|
|
|
*/
|
2005-12-14 12:17:59 +01:00
|
|
|
static DWORD VIRTUAL_GetWin32Prot( BYTE vprot )
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2005-12-14 12:17:59 +01:00
|
|
|
DWORD ret = VIRTUAL_Win32Flags[vprot & 0x0f];
|
|
|
|
if (vprot & VPROT_NOCACHE) ret |= PAGE_NOCACHE;
|
|
|
|
if (vprot & VPROT_GUARD) ret |= PAGE_GUARD;
|
|
|
|
return ret;
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
2008-12-11 14:05:42 +01:00
|
|
|
* get_vprot_flags
|
2002-09-17 20:54:42 +02:00
|
|
|
*
|
|
|
|
* Build page protections from Win32 flags.
|
|
|
|
*
|
2005-05-18 11:45:12 +02:00
|
|
|
* PARAMS
|
|
|
|
* protect [I] Win32 protection flags
|
|
|
|
*
|
2002-09-17 20:54:42 +02:00
|
|
|
* RETURNS
|
|
|
|
* Value of page protection flags
|
|
|
|
*/
|
2008-12-11 14:05:42 +01:00
|
|
|
static NTSTATUS get_vprot_flags( DWORD protect, unsigned int *vprot )
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
|
|
|
switch(protect & 0xff)
|
|
|
|
{
|
|
|
|
case PAGE_READONLY:
|
2008-12-11 14:05:42 +01:00
|
|
|
*vprot = VPROT_READ;
|
2002-09-17 20:54:42 +02:00
|
|
|
break;
|
|
|
|
case PAGE_READWRITE:
|
2008-12-11 14:05:42 +01:00
|
|
|
*vprot = VPROT_READ | VPROT_WRITE;
|
2002-09-17 20:54:42 +02:00
|
|
|
break;
|
|
|
|
case PAGE_WRITECOPY:
|
2008-12-11 14:05:42 +01:00
|
|
|
*vprot = VPROT_READ | VPROT_WRITECOPY;
|
2002-09-17 20:54:42 +02:00
|
|
|
break;
|
|
|
|
case PAGE_EXECUTE:
|
2008-12-11 14:05:42 +01:00
|
|
|
*vprot = VPROT_EXEC;
|
2002-09-17 20:54:42 +02:00
|
|
|
break;
|
|
|
|
case PAGE_EXECUTE_READ:
|
2008-12-11 14:05:42 +01:00
|
|
|
*vprot = VPROT_EXEC | VPROT_READ;
|
2002-09-17 20:54:42 +02:00
|
|
|
break;
|
|
|
|
case PAGE_EXECUTE_READWRITE:
|
2008-12-11 14:05:42 +01:00
|
|
|
*vprot = VPROT_EXEC | VPROT_READ | VPROT_WRITE;
|
2002-09-17 20:54:42 +02:00
|
|
|
break;
|
|
|
|
case PAGE_EXECUTE_WRITECOPY:
|
2008-12-11 14:05:42 +01:00
|
|
|
*vprot = VPROT_EXEC | VPROT_READ | VPROT_WRITECOPY;
|
2002-09-17 20:54:42 +02:00
|
|
|
break;
|
|
|
|
case PAGE_NOACCESS:
|
2008-12-11 14:05:42 +01:00
|
|
|
*vprot = 0;
|
2002-09-17 20:54:42 +02:00
|
|
|
break;
|
2008-12-11 14:05:42 +01:00
|
|
|
default:
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
2008-12-11 14:05:42 +01:00
|
|
|
if (protect & PAGE_GUARD) *vprot |= VPROT_GUARD;
|
|
|
|
if (protect & PAGE_NOCACHE) *vprot |= VPROT_NOCACHE;
|
|
|
|
return STATUS_SUCCESS;
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* VIRTUAL_SetProt
|
|
|
|
*
|
|
|
|
* Change the protection of a range of pages.
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* TRUE: Success
|
|
|
|
* FALSE: Failure
|
|
|
|
*/
|
|
|
|
static BOOL VIRTUAL_SetProt( FILE_VIEW *view, /* [in] Pointer to view */
|
|
|
|
void *base, /* [in] Starting address */
|
2005-07-15 12:01:30 +02:00
|
|
|
size_t size, /* [in] Size in bytes */
|
2002-09-17 20:54:42 +02:00
|
|
|
BYTE vprot ) /* [in] Protections to use */
|
|
|
|
{
|
2006-12-05 15:42:29 +01:00
|
|
|
int unix_prot = VIRTUAL_GetUnixProt(vprot);
|
2008-11-25 12:07:35 +01:00
|
|
|
BYTE *p = view->prot + (((char *)base - (char *)view->base) >> page_shift);
|
2006-12-05 15:42:29 +01:00
|
|
|
|
2002-09-17 20:54:42 +02:00
|
|
|
TRACE("%p-%p %s\n",
|
|
|
|
base, (char *)base + size - 1, VIRTUAL_GetProtStr( vprot ) );
|
|
|
|
|
2008-11-25 12:07:35 +01:00
|
|
|
if (view->protect & VPROT_WRITEWATCH)
|
|
|
|
{
|
|
|
|
/* each page may need different protections depending on write watch flag */
|
|
|
|
UINT i, count;
|
|
|
|
char *addr = base;
|
|
|
|
int prot;
|
|
|
|
|
|
|
|
p[0] = vprot | (p[0] & VPROT_WRITEWATCH);
|
|
|
|
unix_prot = VIRTUAL_GetUnixProt( p[0] );
|
|
|
|
for (count = i = 1; i < size >> page_shift; i++, count++)
|
|
|
|
{
|
|
|
|
p[i] = vprot | (p[i] & VPROT_WRITEWATCH);
|
|
|
|
prot = VIRTUAL_GetUnixProt( p[i] );
|
|
|
|
if (prot == unix_prot) continue;
|
|
|
|
mprotect( addr, count << page_shift, unix_prot );
|
|
|
|
addr += count << page_shift;
|
|
|
|
unix_prot = prot;
|
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
if (count) mprotect( addr, count << page_shift, unix_prot );
|
|
|
|
VIRTUAL_DEBUG_DUMP_VIEW( view );
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2008-04-01 17:37:17 +02:00
|
|
|
/* if setting stack guard pages, store the permissions first, as the guard may be
|
|
|
|
* triggered at any point after mprotect and change the permissions again */
|
|
|
|
if ((vprot & VPROT_GUARD) &&
|
|
|
|
((char *)base >= (char *)NtCurrentTeb()->DeallocationStack) &&
|
|
|
|
((char *)base < (char *)NtCurrentTeb()->Tib.StackBase))
|
|
|
|
{
|
2008-11-25 12:07:35 +01:00
|
|
|
memset( p, vprot, size >> page_shift );
|
2008-04-01 17:37:17 +02:00
|
|
|
mprotect( base, size, unix_prot );
|
|
|
|
VIRTUAL_DEBUG_DUMP_VIEW( view );
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2008-11-04 13:16:01 +01:00
|
|
|
if (force_exec_prot && !(view->protect & VPROT_NOEXEC) &&
|
|
|
|
(unix_prot & PROT_READ) && !(unix_prot & PROT_EXEC))
|
2006-12-05 15:42:29 +01:00
|
|
|
{
|
|
|
|
TRACE( "forcing exec permission on %p-%p\n", base, (char *)base + size - 1 );
|
|
|
|
if (!mprotect( base, size, unix_prot | PROT_EXEC )) goto done;
|
|
|
|
/* exec + write may legitimately fail, in that case fall back to write only */
|
|
|
|
if (!(unix_prot & PROT_WRITE)) return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mprotect( base, size, unix_prot )) return FALSE; /* FIXME: last error */
|
2002-09-17 20:54:42 +02:00
|
|
|
|
2006-12-05 15:42:29 +01:00
|
|
|
done:
|
2008-11-25 12:07:35 +01:00
|
|
|
memset( p, vprot, size >> page_shift );
|
2002-09-17 20:54:42 +02:00
|
|
|
VIRTUAL_DEBUG_DUMP_VIEW( view );
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-25 12:07:35 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* reset_write_watches
|
|
|
|
*
|
|
|
|
* Reset write watches in a memory range.
|
|
|
|
*/
|
|
|
|
static void reset_write_watches( struct file_view *view, void *base, SIZE_T size )
|
|
|
|
{
|
|
|
|
SIZE_T i, count;
|
|
|
|
int prot, unix_prot;
|
|
|
|
char *addr = base;
|
|
|
|
BYTE *p = view->prot + ((addr - (char *)view->base) >> page_shift);
|
|
|
|
|
|
|
|
p[0] |= VPROT_WRITEWATCH;
|
|
|
|
unix_prot = VIRTUAL_GetUnixProt( p[0] );
|
|
|
|
for (count = i = 1; i < size >> page_shift; i++, count++)
|
|
|
|
{
|
|
|
|
p[i] |= VPROT_WRITEWATCH;
|
|
|
|
prot = VIRTUAL_GetUnixProt( p[i] );
|
|
|
|
if (prot == unix_prot) continue;
|
|
|
|
mprotect( addr, count << page_shift, unix_prot );
|
|
|
|
addr += count << page_shift;
|
|
|
|
unix_prot = prot;
|
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
if (count) mprotect( addr, count << page_shift, unix_prot );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-09-14 12:36:58 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* unmap_extra_space
|
|
|
|
*
|
|
|
|
* Release the extra memory while keeping the range starting on the granularity boundary.
|
|
|
|
*/
|
|
|
|
static inline void *unmap_extra_space( void *ptr, size_t total_size, size_t wanted_size, size_t mask )
|
|
|
|
{
|
|
|
|
if ((ULONG_PTR)ptr & mask)
|
|
|
|
{
|
|
|
|
size_t extra = mask + 1 - ((ULONG_PTR)ptr & mask);
|
|
|
|
munmap( ptr, extra );
|
|
|
|
ptr = (char *)ptr + extra;
|
|
|
|
total_size -= extra;
|
|
|
|
}
|
|
|
|
if (total_size > wanted_size)
|
|
|
|
munmap( (char *)ptr + wanted_size, total_size - wanted_size );
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-24 14:00:19 +02:00
|
|
|
struct alloc_area
|
|
|
|
{
|
|
|
|
size_t size;
|
|
|
|
size_t mask;
|
|
|
|
int top_down;
|
2008-11-07 11:04:07 +01:00
|
|
|
void *limit;
|
2006-07-24 14:00:19 +02:00
|
|
|
void *result;
|
|
|
|
};
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* alloc_reserved_area_callback
|
|
|
|
*
|
|
|
|
* Try to map some space inside a reserved area. Callback for wine_mmap_enum_reserved_areas.
|
|
|
|
*/
|
|
|
|
static int alloc_reserved_area_callback( void *start, size_t size, void *arg )
|
|
|
|
{
|
|
|
|
static void * const address_space_start = (void *)0x110000;
|
|
|
|
struct alloc_area *alloc = arg;
|
|
|
|
void *end = (char *)start + size;
|
|
|
|
|
|
|
|
if (start < address_space_start) start = address_space_start;
|
2008-11-07 11:04:07 +01:00
|
|
|
if (is_beyond_limit( start, size, alloc->limit )) end = alloc->limit;
|
2006-07-24 14:00:19 +02:00
|
|
|
if (start >= end) return 0;
|
|
|
|
|
|
|
|
/* make sure we don't touch the preloader reserved range */
|
|
|
|
if (preload_reserve_end >= start)
|
|
|
|
{
|
|
|
|
if (preload_reserve_end >= end)
|
|
|
|
{
|
|
|
|
if (preload_reserve_start <= start) return 0; /* no space in that area */
|
|
|
|
if (preload_reserve_start < end) end = preload_reserve_start;
|
|
|
|
}
|
|
|
|
else if (preload_reserve_start <= start) start = preload_reserve_end;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* range is split in two by the preloader reservation, try first part */
|
|
|
|
if ((alloc->result = find_free_area( start, preload_reserve_start, alloc->size,
|
|
|
|
alloc->mask, alloc->top_down )))
|
|
|
|
return 1;
|
|
|
|
/* then fall through to try second part */
|
|
|
|
start = preload_reserve_end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((alloc->result = find_free_area( start, end, alloc->size, alloc->mask, alloc->top_down )))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-17 20:54:42 +02:00
|
|
|
/***********************************************************************
|
2004-05-21 22:58:44 +02:00
|
|
|
* map_view
|
2002-09-17 20:54:42 +02:00
|
|
|
*
|
2004-05-21 22:58:44 +02:00
|
|
|
* Create a view and mmap the corresponding memory area.
|
|
|
|
* The csVirtual section must be held by caller.
|
2002-09-17 20:54:42 +02:00
|
|
|
*/
|
2006-07-13 20:36:41 +02:00
|
|
|
static NTSTATUS map_view( struct file_view **view_ret, void *base, size_t size, size_t mask,
|
2008-11-04 13:05:32 +01:00
|
|
|
int top_down, unsigned int vprot )
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2004-05-21 22:58:44 +02:00
|
|
|
void *ptr;
|
|
|
|
NTSTATUS status;
|
2002-09-17 20:54:42 +02:00
|
|
|
|
2004-05-21 22:58:44 +02:00
|
|
|
if (base)
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2008-10-28 13:51:36 +01:00
|
|
|
if (is_beyond_limit( base, size, address_space_limit ))
|
2004-05-25 03:29:24 +02:00
|
|
|
return STATUS_WORKING_SET_LIMIT_RANGE;
|
|
|
|
|
|
|
|
switch (wine_mmap_is_in_reserved_area( base, size ))
|
2004-05-21 22:58:44 +02:00
|
|
|
{
|
2004-05-25 03:29:24 +02:00
|
|
|
case -1: /* partially in a reserved area */
|
2004-05-21 22:58:44 +02:00
|
|
|
return STATUS_CONFLICTING_ADDRESSES;
|
2004-05-25 03:29:24 +02:00
|
|
|
|
|
|
|
case 0: /* not in a reserved area, do a normal allocation */
|
|
|
|
if ((ptr = wine_anon_mmap( base, size, VIRTUAL_GetUnixProt(vprot), 0 )) == (void *)-1)
|
|
|
|
{
|
|
|
|
if (errno == ENOMEM) return STATUS_NO_MEMORY;
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
if (ptr != base)
|
|
|
|
{
|
|
|
|
/* We couldn't get the address we wanted */
|
2004-06-18 02:26:57 +02:00
|
|
|
if (is_beyond_limit( ptr, size, user_space_limit )) add_reserved_area( ptr, size );
|
2004-05-25 03:29:24 +02:00
|
|
|
else munmap( ptr, size );
|
|
|
|
return STATUS_CONFLICTING_ADDRESSES;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
case 1: /* in a reserved area, make sure the address is available */
|
|
|
|
if (find_view_range( base, size )) return STATUS_CONFLICTING_ADDRESSES;
|
|
|
|
/* replace the reserved area by our mapping */
|
|
|
|
if ((ptr = wine_anon_mmap( base, size, VIRTUAL_GetUnixProt(vprot), MAP_FIXED )) != base)
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
break;
|
2004-05-21 22:58:44 +02:00
|
|
|
}
|
2008-11-06 11:42:58 +01:00
|
|
|
if (is_beyond_limit( ptr, size, working_set_limit )) working_set_limit = address_space_limit;
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
2004-05-21 22:58:44 +02:00
|
|
|
else
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2006-07-13 20:36:41 +02:00
|
|
|
size_t view_size = size + mask + 1;
|
2006-07-24 14:00:19 +02:00
|
|
|
struct alloc_area alloc;
|
|
|
|
|
|
|
|
alloc.size = size;
|
|
|
|
alloc.mask = mask;
|
|
|
|
alloc.top_down = top_down;
|
2008-11-07 11:04:07 +01:00
|
|
|
alloc.limit = user_space_limit;
|
2006-07-24 14:00:19 +02:00
|
|
|
if (wine_mmap_enum_reserved_areas( alloc_reserved_area_callback, &alloc, top_down ))
|
|
|
|
{
|
|
|
|
ptr = alloc.result;
|
|
|
|
TRACE( "got mem in reserved area %p-%p\n", ptr, (char *)ptr + size );
|
|
|
|
if (wine_anon_mmap( ptr, size, VIRTUAL_GetUnixProt(vprot), MAP_FIXED ) != ptr)
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
goto done;
|
|
|
|
}
|
2004-05-21 22:58:44 +02:00
|
|
|
|
2004-05-25 03:29:24 +02:00
|
|
|
for (;;)
|
2004-05-21 22:58:44 +02:00
|
|
|
{
|
2004-05-25 03:29:24 +02:00
|
|
|
if ((ptr = wine_anon_mmap( NULL, view_size, VIRTUAL_GetUnixProt(vprot), 0 )) == (void *)-1)
|
|
|
|
{
|
|
|
|
if (errno == ENOMEM) return STATUS_NO_MEMORY;
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
2006-07-24 14:00:19 +02:00
|
|
|
TRACE( "got mem with anon mmap %p-%p\n", ptr, (char *)ptr + size );
|
2004-05-25 03:29:24 +02:00
|
|
|
/* if we got something beyond the user limit, unmap it and retry */
|
2004-06-18 02:26:57 +02:00
|
|
|
if (is_beyond_limit( ptr, view_size, user_space_limit )) add_reserved_area( ptr, view_size );
|
2004-05-25 03:29:24 +02:00
|
|
|
else break;
|
2004-05-21 22:58:44 +02:00
|
|
|
}
|
2006-07-13 20:36:41 +02:00
|
|
|
ptr = unmap_extra_space( ptr, view_size, size, mask );
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
2006-07-24 14:00:19 +02:00
|
|
|
done:
|
2004-05-21 22:58:44 +02:00
|
|
|
status = create_view( view_ret, ptr, size, vprot );
|
2004-05-25 03:29:24 +02:00
|
|
|
if (status != STATUS_SUCCESS) unmap_area( ptr, size );
|
2004-05-21 22:58:44 +02:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* map_file_into_view
|
|
|
|
*
|
|
|
|
* Wrapper for mmap() to map a file into a view, falling back to read if mmap fails.
|
|
|
|
* The csVirtual section must be held by caller.
|
|
|
|
*/
|
|
|
|
static NTSTATUS map_file_into_view( struct file_view *view, int fd, size_t start, size_t size,
|
2008-11-04 13:05:32 +01:00
|
|
|
off_t offset, unsigned int vprot, BOOL removable )
|
2004-05-21 22:58:44 +02:00
|
|
|
{
|
|
|
|
void *ptr;
|
2008-11-05 12:31:49 +01:00
|
|
|
int prot = VIRTUAL_GetUnixProt( vprot | VPROT_COMMITTED /* make sure it is accessible */ );
|
2004-05-21 22:58:44 +02:00
|
|
|
BOOL shared_write = (vprot & VPROT_WRITE) != 0;
|
|
|
|
|
|
|
|
assert( start < view->size );
|
|
|
|
assert( start + size <= view->size );
|
|
|
|
|
|
|
|
/* only try mmap if media is not removable (or if we require write access) */
|
|
|
|
if (!removable || shared_write)
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2004-05-21 22:58:44 +02:00
|
|
|
int flags = MAP_FIXED | (shared_write ? MAP_SHARED : MAP_PRIVATE);
|
|
|
|
|
2008-11-17 20:34:19 +01:00
|
|
|
if (mmap( (char *)view->base + start, size, prot, flags, fd, offset ) != (void *)-1)
|
2004-05-21 22:58:44 +02:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* mmap() failed; if this is because the file offset is not */
|
|
|
|
/* page-aligned (EINVAL), or because the underlying filesystem */
|
|
|
|
/* does not support mmap() (ENOEXEC,ENODEV), we do it by hand. */
|
|
|
|
if ((errno != ENOEXEC) && (errno != EINVAL) && (errno != ENODEV)) return FILE_GetNtStatus();
|
2008-01-03 13:20:03 +01:00
|
|
|
if (shared_write) /* we cannot fake shared write mappings */
|
|
|
|
{
|
|
|
|
if (errno == EINVAL) return STATUS_INVALID_PARAMETER;
|
|
|
|
ERR( "shared writable mmap not supported, broken filesystem?\n" );
|
|
|
|
return STATUS_NOT_SUPPORTED;
|
|
|
|
}
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
2004-05-21 22:58:44 +02:00
|
|
|
|
|
|
|
/* Reserve the memory with an anonymous mmap */
|
|
|
|
ptr = wine_anon_mmap( (char *)view->base + start, size, PROT_READ | PROT_WRITE, MAP_FIXED );
|
|
|
|
if (ptr == (void *)-1) return FILE_GetNtStatus();
|
|
|
|
/* Now read in the file */
|
|
|
|
pread( fd, ptr, size, offset );
|
|
|
|
if (prot != (PROT_READ|PROT_WRITE)) mprotect( ptr, size, prot ); /* Set the right protection */
|
|
|
|
done:
|
2006-01-11 20:59:07 +01:00
|
|
|
memset( view->prot + (start >> page_shift), vprot, ROUND_SIZE(start,size) >> page_shift );
|
2002-09-17 20:54:42 +02:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-05 12:24:05 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* get_committed_size
|
|
|
|
*
|
|
|
|
* Get the size of the committed range starting at base.
|
|
|
|
* Also return the protections for the first page.
|
|
|
|
*/
|
|
|
|
static SIZE_T get_committed_size( struct file_view *view, void *base, BYTE *vprot )
|
|
|
|
{
|
|
|
|
SIZE_T i, start;
|
|
|
|
|
|
|
|
start = ((char *)base - (char *)view->base) >> page_shift;
|
|
|
|
*vprot = view->prot[start];
|
|
|
|
|
|
|
|
if (view->mapping && !(view->protect & VPROT_COMMITTED))
|
|
|
|
{
|
|
|
|
SIZE_T ret = 0;
|
|
|
|
SERVER_START_REQ( get_mapping_committed_range )
|
|
|
|
{
|
2008-12-08 16:05:17 +01:00
|
|
|
req->handle = wine_server_obj_handle( view->mapping );
|
2008-11-05 12:24:05 +01:00
|
|
|
req->offset = start << page_shift;
|
|
|
|
if (!wine_server_call( req ))
|
|
|
|
{
|
|
|
|
ret = reply->size;
|
|
|
|
if (reply->committed)
|
|
|
|
{
|
|
|
|
*vprot |= VPROT_COMMITTED;
|
|
|
|
for (i = 0; i < ret >> page_shift; i++) view->prot[start+i] |= VPROT_COMMITTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SERVER_END_REQ;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
for (i = start + 1; i < view->size >> page_shift; i++)
|
|
|
|
if ((*vprot ^ view->prot[i]) & VPROT_COMMITTED) break;
|
|
|
|
return (i - start) << page_shift;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-21 22:58:44 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* decommit_view
|
|
|
|
*
|
|
|
|
* Decommit some pages of a given view.
|
|
|
|
* The csVirtual section must be held by caller.
|
|
|
|
*/
|
|
|
|
static NTSTATUS decommit_pages( struct file_view *view, size_t start, size_t size )
|
|
|
|
{
|
|
|
|
if (wine_anon_mmap( (char *)view->base + start, size, PROT_NONE, MAP_FIXED ) != (void *)-1)
|
|
|
|
{
|
|
|
|
BYTE *p = view->prot + (start >> page_shift);
|
|
|
|
size >>= page_shift;
|
|
|
|
while (size--) *p++ &= ~VPROT_COMMITTED;
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
return FILE_GetNtStatus();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-18 20:11:49 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* allocate_dos_memory
|
|
|
|
*
|
|
|
|
* Allocate the DOS memory range.
|
|
|
|
*/
|
|
|
|
static NTSTATUS allocate_dos_memory( struct file_view **view, unsigned int vprot )
|
|
|
|
{
|
|
|
|
size_t size;
|
|
|
|
void *addr = NULL;
|
|
|
|
void * const low_64k = (void *)0x10000;
|
|
|
|
const size_t dosmem_size = 0x110000;
|
|
|
|
int unix_prot = VIRTUAL_GetUnixProt( vprot );
|
|
|
|
struct list *ptr;
|
|
|
|
|
|
|
|
/* check for existing view */
|
|
|
|
|
|
|
|
if ((ptr = list_head( &views_list )))
|
|
|
|
{
|
|
|
|
struct file_view *first_view = LIST_ENTRY( ptr, struct file_view, entry );
|
|
|
|
if (first_view->base < (void *)dosmem_size) return STATUS_CONFLICTING_ADDRESSES;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check without the first 64K */
|
|
|
|
|
|
|
|
if (wine_mmap_is_in_reserved_area( low_64k, dosmem_size - 0x10000 ) != 1)
|
|
|
|
{
|
|
|
|
addr = wine_anon_mmap( low_64k, dosmem_size - 0x10000, unix_prot, 0 );
|
|
|
|
if (addr != low_64k)
|
|
|
|
{
|
|
|
|
if (addr != (void *)-1) munmap( addr, dosmem_size - 0x10000 );
|
|
|
|
return map_view( view, NULL, dosmem_size, 0xffff, 0, vprot );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now try to allocate the low 64K too */
|
|
|
|
|
|
|
|
if (wine_mmap_is_in_reserved_area( NULL, 0x10000 ) != 1)
|
|
|
|
{
|
|
|
|
addr = wine_anon_mmap( (void *)page_size, 0x10000 - page_size, unix_prot, 0 );
|
|
|
|
if (addr == (void *)page_size)
|
|
|
|
{
|
|
|
|
addr = NULL;
|
|
|
|
TRACE( "successfully mapped low 64K range\n" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (addr != (void *)-1) munmap( addr, 0x10000 - page_size );
|
|
|
|
addr = low_64k;
|
|
|
|
TRACE( "failed to map low 64K range\n" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now reserve the whole range */
|
|
|
|
|
|
|
|
size = (char *)dosmem_size - (char *)addr;
|
|
|
|
wine_anon_mmap( addr, size, unix_prot, MAP_FIXED );
|
|
|
|
return create_view( view, addr, size, vprot );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-17 20:54:42 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* map_image
|
|
|
|
*
|
|
|
|
* Map an executable (PE format) image into memory.
|
|
|
|
*/
|
2006-07-13 20:36:41 +02:00
|
|
|
static NTSTATUS map_image( HANDLE hmapping, int fd, char *base, SIZE_T total_size, SIZE_T mask,
|
2007-01-12 14:55:31 +01:00
|
|
|
SIZE_T header_size, int shared_fd, HANDLE dup_mapping, PVOID *addr_ptr )
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
|
|
|
IMAGE_DOS_HEADER *dos;
|
|
|
|
IMAGE_NT_HEADERS *nt;
|
|
|
|
IMAGE_SECTION_HEADER *sec;
|
|
|
|
IMAGE_DATA_DIRECTORY *imports;
|
2004-05-21 22:58:44 +02:00
|
|
|
NTSTATUS status = STATUS_CONFLICTING_ADDRESSES;
|
|
|
|
int i;
|
|
|
|
off_t pos;
|
2007-01-12 20:15:52 +01:00
|
|
|
sigset_t sigset;
|
2006-01-03 17:39:23 +01:00
|
|
|
struct stat st;
|
2004-05-21 22:58:44 +02:00
|
|
|
struct file_view *view = NULL;
|
2006-01-13 13:52:07 +01:00
|
|
|
char *ptr, *header_end;
|
2008-12-22 00:07:58 +01:00
|
|
|
INT_PTR delta = 0;
|
2002-09-17 20:54:42 +02:00
|
|
|
|
|
|
|
/* zero-map the whole range */
|
|
|
|
|
2007-01-12 20:15:52 +01:00
|
|
|
server_enter_uninterrupted_section( &csVirtual, &sigset );
|
2004-05-21 22:58:44 +02:00
|
|
|
|
|
|
|
if (base >= (char *)0x110000) /* make sure the DOS area remains free */
|
2006-07-24 13:59:44 +02:00
|
|
|
status = map_view( &view, base, total_size, mask, FALSE,
|
2004-05-21 22:58:44 +02:00
|
|
|
VPROT_COMMITTED | VPROT_READ | VPROT_EXEC | VPROT_WRITECOPY | VPROT_IMAGE );
|
|
|
|
|
|
|
|
if (status == STATUS_CONFLICTING_ADDRESSES)
|
2006-07-24 13:59:44 +02:00
|
|
|
status = map_view( &view, NULL, total_size, mask, FALSE,
|
2004-05-21 22:58:44 +02:00
|
|
|
VPROT_COMMITTED | VPROT_READ | VPROT_EXEC | VPROT_WRITECOPY | VPROT_IMAGE );
|
|
|
|
|
|
|
|
if (status != STATUS_SUCCESS) goto error;
|
|
|
|
|
|
|
|
ptr = view->base;
|
2002-09-17 20:54:42 +02:00
|
|
|
TRACE_(module)( "mapped PE file at %p-%p\n", ptr, ptr + total_size );
|
|
|
|
|
|
|
|
/* map the header */
|
|
|
|
|
2006-01-03 17:39:23 +01:00
|
|
|
if (fstat( fd, &st ) == -1)
|
|
|
|
{
|
|
|
|
status = FILE_GetNtStatus();
|
|
|
|
goto error;
|
|
|
|
}
|
2004-05-21 22:58:44 +02:00
|
|
|
status = STATUS_INVALID_IMAGE_FORMAT; /* generic error */
|
2006-03-27 21:29:46 +02:00
|
|
|
if (!st.st_size) goto error;
|
|
|
|
header_size = min( header_size, st.st_size );
|
2006-12-13 13:06:12 +01:00
|
|
|
if (map_file_into_view( view, fd, 0, header_size, 0, VPROT_COMMITTED | VPROT_READ | VPROT_WRITECOPY,
|
2007-01-12 14:55:31 +01:00
|
|
|
!dup_mapping ) != STATUS_SUCCESS) goto error;
|
2002-09-17 20:54:42 +02:00
|
|
|
dos = (IMAGE_DOS_HEADER *)ptr;
|
|
|
|
nt = (IMAGE_NT_HEADERS *)(ptr + dos->e_lfanew);
|
2006-01-13 13:52:07 +01:00
|
|
|
header_end = ptr + ROUND_SIZE( 0, header_size );
|
2006-12-13 13:06:12 +01:00
|
|
|
memset( ptr + header_size, 0, header_end - (ptr + header_size) );
|
2006-01-13 13:52:07 +01:00
|
|
|
if ((char *)(nt + 1) > header_end) goto error;
|
2002-09-17 20:54:42 +02:00
|
|
|
sec = (IMAGE_SECTION_HEADER*)((char*)&nt->OptionalHeader+nt->FileHeader.SizeOfOptionalHeader);
|
2006-01-13 13:52:07 +01:00
|
|
|
if ((char *)(sec + nt->FileHeader.NumberOfSections) > header_end) goto error;
|
2002-09-17 20:54:42 +02:00
|
|
|
|
|
|
|
imports = nt->OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_IMPORT;
|
|
|
|
if (!imports->Size || !imports->VirtualAddress) imports = NULL;
|
|
|
|
|
|
|
|
/* check the architecture */
|
|
|
|
|
2008-11-25 09:30:17 +01:00
|
|
|
#ifdef __x86_64__
|
|
|
|
if (nt->FileHeader.Machine != IMAGE_FILE_MACHINE_AMD64)
|
|
|
|
#else
|
2002-09-17 20:54:42 +02:00
|
|
|
if (nt->FileHeader.Machine != IMAGE_FILE_MACHINE_I386)
|
2008-11-25 09:30:17 +01:00
|
|
|
#endif
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
|
|
|
MESSAGE("Trying to load PE image for unsupported architecture (");
|
|
|
|
switch (nt->FileHeader.Machine)
|
|
|
|
{
|
|
|
|
case IMAGE_FILE_MACHINE_UNKNOWN: MESSAGE("Unknown"); break;
|
|
|
|
case IMAGE_FILE_MACHINE_I860: MESSAGE("I860"); break;
|
2008-11-25 09:30:17 +01:00
|
|
|
case IMAGE_FILE_MACHINE_I386: MESSAGE("I386"); break;
|
2002-09-17 20:54:42 +02:00
|
|
|
case IMAGE_FILE_MACHINE_R3000: MESSAGE("R3000"); break;
|
|
|
|
case IMAGE_FILE_MACHINE_R4000: MESSAGE("R4000"); break;
|
|
|
|
case IMAGE_FILE_MACHINE_R10000: MESSAGE("R10000"); break;
|
|
|
|
case IMAGE_FILE_MACHINE_ALPHA: MESSAGE("Alpha"); break;
|
|
|
|
case IMAGE_FILE_MACHINE_POWERPC: MESSAGE("PowerPC"); break;
|
2005-09-09 12:42:42 +02:00
|
|
|
case IMAGE_FILE_MACHINE_IA64: MESSAGE("IA-64"); break;
|
|
|
|
case IMAGE_FILE_MACHINE_ALPHA64: MESSAGE("Alpha-64"); break;
|
|
|
|
case IMAGE_FILE_MACHINE_AMD64: MESSAGE("AMD-64"); break;
|
2006-04-07 23:00:03 +02:00
|
|
|
case IMAGE_FILE_MACHINE_ARM: MESSAGE("ARM"); break;
|
2002-09-17 20:54:42 +02:00
|
|
|
default: MESSAGE("Unknown-%04x", nt->FileHeader.Machine); break;
|
|
|
|
}
|
|
|
|
MESSAGE(")\n");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2005-01-04 13:04:06 +01:00
|
|
|
/* check for non page-aligned binary */
|
|
|
|
|
|
|
|
if (nt->OptionalHeader.SectionAlignment <= page_mask)
|
|
|
|
{
|
|
|
|
/* unaligned sections, this happens for native subsystem binaries */
|
|
|
|
/* in that case Windows simply maps in the whole file */
|
|
|
|
|
|
|
|
if (map_file_into_view( view, fd, 0, total_size, 0, VPROT_COMMITTED | VPROT_READ,
|
2007-01-12 14:55:31 +01:00
|
|
|
!dup_mapping ) != STATUS_SUCCESS) goto error;
|
2005-01-04 13:04:06 +01:00
|
|
|
|
|
|
|
/* check that all sections are loaded at the right offset */
|
2006-12-07 15:34:36 +01:00
|
|
|
if (nt->OptionalHeader.FileAlignment != nt->OptionalHeader.SectionAlignment) goto error;
|
2005-01-04 13:04:06 +01:00
|
|
|
for (i = 0; i < nt->FileHeader.NumberOfSections; i++)
|
|
|
|
{
|
|
|
|
if (sec[i].VirtualAddress != sec[i].PointerToRawData)
|
|
|
|
goto error; /* Windows refuses to load in that case too */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set the image protections */
|
|
|
|
VIRTUAL_SetProt( view, ptr, total_size,
|
|
|
|
VPROT_COMMITTED | VPROT_READ | VPROT_WRITECOPY | VPROT_EXEC );
|
|
|
|
|
2008-04-02 20:39:22 +02:00
|
|
|
/* no relocations are performed on non page-aligned binaries */
|
2005-01-04 13:04:06 +01:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-17 20:54:42 +02:00
|
|
|
/* map all the sections */
|
|
|
|
|
|
|
|
for (i = pos = 0; i < nt->FileHeader.NumberOfSections; i++, sec++)
|
|
|
|
{
|
2006-12-21 12:34:44 +01:00
|
|
|
static const SIZE_T sector_align = 0x1ff;
|
|
|
|
SIZE_T map_size, file_start, file_size, end;
|
2005-08-15 16:50:06 +02:00
|
|
|
|
|
|
|
if (!sec->Misc.VirtualSize)
|
2006-12-21 12:34:44 +01:00
|
|
|
map_size = ROUND_SIZE( 0, sec->SizeOfRawData );
|
2005-08-15 16:50:06 +02:00
|
|
|
else
|
|
|
|
map_size = ROUND_SIZE( 0, sec->Misc.VirtualSize );
|
2006-12-21 12:34:44 +01:00
|
|
|
|
|
|
|
/* file positions are rounded to sector boundaries regardless of OptionalHeader.FileAlignment */
|
|
|
|
file_start = sec->PointerToRawData & ~sector_align;
|
|
|
|
file_size = (sec->SizeOfRawData + (sec->PointerToRawData & sector_align) + sector_align) & ~sector_align;
|
|
|
|
if (file_size > map_size) file_size = map_size;
|
2002-09-17 20:54:42 +02:00
|
|
|
|
|
|
|
/* a few sanity checks */
|
2005-08-15 16:50:06 +02:00
|
|
|
end = sec->VirtualAddress + ROUND_SIZE( sec->VirtualAddress, map_size );
|
|
|
|
if (sec->VirtualAddress > total_size || end > total_size || end < sec->VirtualAddress)
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2006-12-13 13:06:12 +01:00
|
|
|
WARN_(module)( "Section %.8s too large (%x+%lx/%lx)\n",
|
|
|
|
sec->Name, sec->VirtualAddress, map_size, total_size );
|
2002-09-17 20:54:42 +02:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((sec->Characteristics & IMAGE_SCN_MEM_SHARED) &&
|
|
|
|
(sec->Characteristics & IMAGE_SCN_MEM_WRITE))
|
|
|
|
{
|
2006-10-16 13:49:06 +02:00
|
|
|
TRACE_(module)( "mapping shared section %.8s at %p off %x (%x) size %lx (%lx) flags %x\n",
|
2005-08-15 16:50:06 +02:00
|
|
|
sec->Name, ptr + sec->VirtualAddress,
|
|
|
|
sec->PointerToRawData, (int)pos, file_size, map_size,
|
|
|
|
sec->Characteristics );
|
|
|
|
if (map_file_into_view( view, shared_fd, sec->VirtualAddress, map_size, pos,
|
2006-12-20 12:31:20 +01:00
|
|
|
VPROT_COMMITTED | VPROT_READ | VPROT_WRITE,
|
2004-05-21 22:58:44 +02:00
|
|
|
FALSE ) != STATUS_SUCCESS)
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
|
|
|
ERR_(module)( "Could not map shared section %.8s\n", sec->Name );
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if the import directory falls inside this section */
|
|
|
|
if (imports && imports->VirtualAddress >= sec->VirtualAddress &&
|
2005-08-15 16:50:06 +02:00
|
|
|
imports->VirtualAddress < sec->VirtualAddress + map_size)
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
|
|
|
UINT_PTR base = imports->VirtualAddress & ~page_mask;
|
|
|
|
UINT_PTR end = base + ROUND_SIZE( imports->VirtualAddress, imports->Size );
|
2005-08-15 16:50:06 +02:00
|
|
|
if (end > sec->VirtualAddress + map_size) end = sec->VirtualAddress + map_size;
|
2004-05-21 22:58:44 +02:00
|
|
|
if (end > base)
|
|
|
|
map_file_into_view( view, shared_fd, base, end - base,
|
|
|
|
pos + (base - sec->VirtualAddress),
|
|
|
|
VPROT_COMMITTED | VPROT_READ | VPROT_WRITECOPY,
|
|
|
|
FALSE );
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
2005-08-15 16:50:06 +02:00
|
|
|
pos += map_size;
|
2002-09-17 20:54:42 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-10-16 13:49:06 +02:00
|
|
|
TRACE_(module)( "mapping section %.8s at %p off %x size %x virt %x flags %x\n",
|
2002-09-17 20:54:42 +02:00
|
|
|
sec->Name, ptr + sec->VirtualAddress,
|
|
|
|
sec->PointerToRawData, sec->SizeOfRawData,
|
2005-08-15 16:50:06 +02:00
|
|
|
sec->Misc.VirtualSize, sec->Characteristics );
|
2002-09-17 20:54:42 +02:00
|
|
|
|
2005-08-15 16:50:06 +02:00
|
|
|
if (!sec->PointerToRawData || !file_size) continue;
|
2002-09-17 20:54:42 +02:00
|
|
|
|
2004-05-21 22:58:44 +02:00
|
|
|
/* Note: if the section is not aligned properly map_file_into_view will magically
|
2002-09-17 20:54:42 +02:00
|
|
|
* fall back to read(), so we don't need to check anything here.
|
|
|
|
*/
|
2006-12-21 12:34:44 +01:00
|
|
|
end = file_start + file_size;
|
|
|
|
if (sec->PointerToRawData >= st.st_size ||
|
|
|
|
end > ((st.st_size + sector_align) & ~sector_align) ||
|
|
|
|
end < file_start ||
|
|
|
|
map_file_into_view( view, fd, sec->VirtualAddress, file_size, file_start,
|
2004-05-21 22:58:44 +02:00
|
|
|
VPROT_COMMITTED | VPROT_READ | VPROT_WRITECOPY,
|
2007-01-12 14:55:31 +01:00
|
|
|
!dup_mapping ) != STATUS_SUCCESS)
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
|
|
|
ERR_(module)( "Could not map section %.8s, file probably truncated\n", sec->Name );
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2005-08-15 16:50:06 +02:00
|
|
|
if (file_size & page_mask)
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2005-08-15 16:50:06 +02:00
|
|
|
end = ROUND_SIZE( 0, file_size );
|
|
|
|
if (end > map_size) end = map_size;
|
2002-09-17 20:54:42 +02:00
|
|
|
TRACE_(module)("clearing %p - %p\n",
|
2005-08-15 16:50:06 +02:00
|
|
|
ptr + sec->VirtualAddress + file_size,
|
2002-09-17 20:54:42 +02:00
|
|
|
ptr + sec->VirtualAddress + end );
|
2005-08-15 16:50:06 +02:00
|
|
|
memset( ptr + sec->VirtualAddress + file_size, 0, end - file_size );
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* perform base relocation, if necessary */
|
|
|
|
|
2008-01-17 14:05:25 +01:00
|
|
|
if (ptr != base &&
|
|
|
|
((nt->FileHeader.Characteristics & IMAGE_FILE_DLL) ||
|
|
|
|
!NtCurrentTeb()->Peb->ImageBaseAddress) )
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2008-04-02 20:39:22 +02:00
|
|
|
IMAGE_BASE_RELOCATION *rel, *end;
|
2002-09-17 20:54:42 +02:00
|
|
|
const IMAGE_DATA_DIRECTORY *relocs;
|
|
|
|
|
2006-06-14 03:38:34 +02:00
|
|
|
if (nt->FileHeader.Characteristics & IMAGE_FILE_RELOCS_STRIPPED)
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2008-04-02 20:39:22 +02:00
|
|
|
WARN_(module)( "Need to relocate module from %p to %p, but there are no relocation records\n",
|
|
|
|
base, ptr );
|
2006-07-19 14:12:58 +02:00
|
|
|
status = STATUS_CONFLICTING_ADDRESSES;
|
2002-09-17 20:54:42 +02:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2008-04-02 20:39:22 +02:00
|
|
|
TRACE_(module)( "relocating from %p-%p to %p-%p\n",
|
|
|
|
base, base + total_size, ptr, ptr + total_size );
|
|
|
|
|
|
|
|
relocs = &nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC];
|
|
|
|
rel = (IMAGE_BASE_RELOCATION *)(ptr + relocs->VirtualAddress);
|
|
|
|
end = (IMAGE_BASE_RELOCATION *)(ptr + relocs->VirtualAddress + relocs->Size);
|
2008-07-24 19:21:33 +02:00
|
|
|
delta = ptr - base;
|
2002-09-17 20:54:42 +02:00
|
|
|
|
2008-10-27 13:19:30 +01:00
|
|
|
while (rel < end - 1 && rel->SizeOfBlock)
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2008-06-30 12:51:35 +02:00
|
|
|
if (rel->VirtualAddress >= total_size)
|
|
|
|
{
|
|
|
|
WARN_(module)( "invalid address %p in relocation %p\n", ptr + rel->VirtualAddress, rel );
|
|
|
|
status = STATUS_ACCESS_VIOLATION;
|
|
|
|
goto error;
|
|
|
|
}
|
2008-04-02 20:39:22 +02:00
|
|
|
rel = LdrProcessRelocationBlock( ptr + rel->VirtualAddress,
|
|
|
|
(rel->SizeOfBlock - sizeof(*rel)) / sizeof(USHORT),
|
|
|
|
(USHORT *)(rel + 1), delta );
|
|
|
|
if (!rel) goto error;
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set the image protections */
|
|
|
|
|
2006-12-13 13:06:12 +01:00
|
|
|
VIRTUAL_SetProt( view, ptr, ROUND_SIZE( 0, header_size ), VPROT_COMMITTED | VPROT_READ );
|
|
|
|
|
2002-09-17 20:54:42 +02:00
|
|
|
sec = (IMAGE_SECTION_HEADER*)((char *)&nt->OptionalHeader+nt->FileHeader.SizeOfOptionalHeader);
|
|
|
|
for (i = 0; i < nt->FileHeader.NumberOfSections; i++, sec++)
|
|
|
|
{
|
2006-07-30 09:59:46 +02:00
|
|
|
SIZE_T size;
|
2002-09-17 20:54:42 +02:00
|
|
|
BYTE vprot = VPROT_COMMITTED;
|
2006-07-30 09:59:46 +02:00
|
|
|
|
|
|
|
if (sec->Misc.VirtualSize)
|
|
|
|
size = ROUND_SIZE( sec->VirtualAddress, sec->Misc.VirtualSize );
|
|
|
|
else
|
|
|
|
size = ROUND_SIZE( sec->VirtualAddress, sec->SizeOfRawData );
|
|
|
|
|
2002-09-17 20:54:42 +02:00
|
|
|
if (sec->Characteristics & IMAGE_SCN_MEM_READ) vprot |= VPROT_READ;
|
2004-05-21 22:58:44 +02:00
|
|
|
if (sec->Characteristics & IMAGE_SCN_MEM_WRITE) vprot |= VPROT_READ|VPROT_WRITECOPY;
|
2002-09-17 20:54:42 +02:00
|
|
|
if (sec->Characteristics & IMAGE_SCN_MEM_EXECUTE) vprot |= VPROT_EXEC;
|
2006-05-06 15:16:39 +02:00
|
|
|
|
|
|
|
/* Dumb game crack lets the AOEP point into a data section. Adjust. */
|
|
|
|
if ((nt->OptionalHeader.AddressOfEntryPoint >= sec->VirtualAddress) &&
|
|
|
|
(nt->OptionalHeader.AddressOfEntryPoint < sec->VirtualAddress + size))
|
|
|
|
vprot |= VPROT_EXEC;
|
|
|
|
|
2002-09-17 20:54:42 +02:00
|
|
|
VIRTUAL_SetProt( view, ptr + sec->VirtualAddress, size, vprot );
|
|
|
|
}
|
2005-01-04 13:04:06 +01:00
|
|
|
|
|
|
|
done:
|
2007-01-12 14:55:31 +01:00
|
|
|
view->mapping = dup_mapping;
|
2007-01-12 20:15:52 +01:00
|
|
|
server_leave_uninterrupted_section( &csVirtual, &sigset );
|
2002-09-17 20:54:42 +02:00
|
|
|
|
|
|
|
*addr_ptr = ptr;
|
2008-07-24 19:21:33 +02:00
|
|
|
#ifdef VALGRIND_LOAD_PDB_DEBUGINFO
|
|
|
|
VALGRIND_LOAD_PDB_DEBUGINFO(fd, ptr, total_size, delta);
|
|
|
|
#endif
|
2002-09-17 20:54:42 +02:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
|
|
|
|
error:
|
2004-05-25 03:29:24 +02:00
|
|
|
if (view) delete_view( view );
|
2007-01-12 20:15:52 +01:00
|
|
|
server_leave_uninterrupted_section( &csVirtual, &sigset );
|
2007-01-12 14:55:31 +01:00
|
|
|
if (dup_mapping) NtClose( dup_mapping );
|
2002-09-17 20:54:42 +02:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-31 13:00:59 +01:00
|
|
|
/* callback for wine_mmap_enum_reserved_areas to allocate space for the virtual heap */
|
|
|
|
static int alloc_virtual_heap( void *base, size_t size, void *arg )
|
2008-10-28 13:51:36 +01:00
|
|
|
{
|
2008-10-31 13:00:59 +01:00
|
|
|
void **heap_base = arg;
|
|
|
|
|
2008-11-07 11:04:07 +01:00
|
|
|
if (is_beyond_limit( base, size, address_space_limit )) address_space_limit = (char *)base + size;
|
2008-10-31 13:00:59 +01:00
|
|
|
if (size < VIRTUAL_HEAP_SIZE) return 0;
|
|
|
|
*heap_base = wine_anon_mmap( (char *)base + size - VIRTUAL_HEAP_SIZE,
|
|
|
|
VIRTUAL_HEAP_SIZE, PROT_READ|PROT_WRITE, MAP_FIXED );
|
|
|
|
return (*heap_base != (void *)-1);
|
2008-10-28 13:51:36 +01:00
|
|
|
}
|
|
|
|
|
2002-09-17 20:54:42 +02:00
|
|
|
/***********************************************************************
|
2004-01-07 05:50:11 +01:00
|
|
|
* virtual_init
|
2002-09-17 20:54:42 +02:00
|
|
|
*/
|
2006-07-24 15:19:32 +02:00
|
|
|
void virtual_init(void)
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2006-07-24 14:00:19 +02:00
|
|
|
const char *preload;
|
2008-10-31 13:00:59 +01:00
|
|
|
void *heap_base;
|
|
|
|
struct file_view *heap_view;
|
|
|
|
|
2004-01-07 05:50:11 +01:00
|
|
|
#ifndef page_mask
|
2002-09-17 20:54:42 +02:00
|
|
|
page_size = getpagesize();
|
|
|
|
page_mask = page_size - 1;
|
|
|
|
/* Make sure we have a power of 2 */
|
|
|
|
assert( !(page_size & page_mask) );
|
|
|
|
page_shift = 0;
|
|
|
|
while ((1 << page_shift) != page_size) page_shift++;
|
2008-11-07 11:04:07 +01:00
|
|
|
user_space_limit = working_set_limit = address_space_limit = (void *)~page_mask;
|
2002-09-17 20:54:42 +02:00
|
|
|
#endif /* page_mask */
|
2006-07-24 14:00:19 +02:00
|
|
|
if ((preload = getenv("WINEPRELOADRESERVE")))
|
|
|
|
{
|
|
|
|
unsigned long start, end;
|
|
|
|
if (sscanf( preload, "%lx-%lx", &start, &end ) == 2)
|
|
|
|
{
|
|
|
|
preload_reserve_start = (void *)start;
|
|
|
|
preload_reserve_end = (void *)end;
|
|
|
|
}
|
|
|
|
}
|
2008-10-31 13:00:59 +01:00
|
|
|
|
|
|
|
/* try to find space in a reserved area for the virtual heap */
|
|
|
|
if (!wine_mmap_enum_reserved_areas( alloc_virtual_heap, &heap_base, 1 ))
|
|
|
|
heap_base = wine_anon_mmap( NULL, VIRTUAL_HEAP_SIZE, PROT_READ|PROT_WRITE, 0 );
|
|
|
|
|
|
|
|
assert( heap_base != (void *)-1 );
|
|
|
|
virtual_heap = RtlCreateHeap( HEAP_NO_SERIALIZE, heap_base, VIRTUAL_HEAP_SIZE,
|
|
|
|
VIRTUAL_HEAP_SIZE, NULL, NULL );
|
|
|
|
create_view( &heap_view, heap_base, VIRTUAL_HEAP_SIZE, VPROT_COMMITTED | VPROT_READ | VPROT_WRITE );
|
2004-01-07 05:50:11 +01:00
|
|
|
}
|
2002-09-17 20:54:42 +02:00
|
|
|
|
|
|
|
|
2005-08-09 12:43:47 +02:00
|
|
|
/***********************************************************************
|
2006-07-24 15:19:32 +02:00
|
|
|
* virtual_init_threading
|
2005-08-09 12:43:47 +02:00
|
|
|
*/
|
2006-07-24 15:19:32 +02:00
|
|
|
void virtual_init_threading(void)
|
2005-08-09 12:43:47 +02:00
|
|
|
{
|
2006-07-24 15:19:32 +02:00
|
|
|
use_locks = 1;
|
2005-08-09 12:43:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-03 13:23:48 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* virtual_get_system_info
|
|
|
|
*/
|
|
|
|
void virtual_get_system_info( SYSTEM_BASIC_INFORMATION *info )
|
|
|
|
{
|
|
|
|
info->dwUnknown1 = 0;
|
|
|
|
info->uKeMaximumIncrement = 0; /* FIXME */
|
|
|
|
info->uPageSize = page_size;
|
|
|
|
info->uMmLowestPhysicalPage = 1;
|
|
|
|
info->uMmHighestPhysicalPage = 0x7fffffff / page_size;
|
|
|
|
info->uMmNumberOfPhysicalPages = info->uMmHighestPhysicalPage - info->uMmLowestPhysicalPage;
|
|
|
|
info->uAllocationGranularity = get_mask(0) + 1;
|
|
|
|
info->pLowestUserAddress = (void *)0x10000;
|
|
|
|
info->pMmHighestUserAddress = (char *)user_space_limit - 1;
|
|
|
|
info->uKeActiveProcessors = NtCurrentTeb()->Peb->NumberOfProcessors;
|
|
|
|
info->bKeNumberProcessors = info->uKeActiveProcessors;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-14 17:40:54 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* virtual_create_system_view
|
|
|
|
*/
|
|
|
|
NTSTATUS virtual_create_system_view( void *base, SIZE_T size, DWORD vprot )
|
|
|
|
{
|
|
|
|
FILE_VIEW *view;
|
|
|
|
NTSTATUS status;
|
|
|
|
sigset_t sigset;
|
|
|
|
|
|
|
|
size = ROUND_SIZE( base, size );
|
|
|
|
base = ROUND_ADDR( base, page_mask );
|
|
|
|
server_enter_uninterrupted_section( &csVirtual, &sigset );
|
|
|
|
status = create_view( &view, base, size, vprot );
|
|
|
|
if (!status) TRACE( "created %p-%p\n", base, (char *)base + size );
|
|
|
|
server_leave_uninterrupted_section( &csVirtual, &sigset );
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* virtual_free_system_view
|
|
|
|
*/
|
|
|
|
SIZE_T virtual_free_system_view( PVOID *addr_ptr )
|
|
|
|
{
|
|
|
|
FILE_VIEW *view;
|
|
|
|
sigset_t sigset;
|
|
|
|
SIZE_T size = 0;
|
|
|
|
char *base = ROUND_ADDR( *addr_ptr, page_mask );
|
|
|
|
|
|
|
|
server_enter_uninterrupted_section( &csVirtual, &sigset );
|
2008-11-25 11:58:50 +01:00
|
|
|
if ((view = VIRTUAL_FindView( base, 0 )))
|
2008-11-14 17:40:54 +01:00
|
|
|
{
|
|
|
|
TRACE( "freeing %p-%p\n", view->base, (char *)view->base + view->size );
|
|
|
|
/* return the values that the caller should use to unmap the area */
|
|
|
|
*addr_ptr = view->base;
|
|
|
|
/* make sure we don't munmap anything from a reserved area */
|
|
|
|
if (!wine_mmap_is_in_reserved_area( view->base, view->size )) size = view->size;
|
|
|
|
view->protect |= VPROT_SYSTEM;
|
|
|
|
delete_view( view );
|
|
|
|
}
|
|
|
|
server_leave_uninterrupted_section( &csVirtual, &sigset );
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-01 14:11:44 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* virtual_alloc_thread_stack
|
|
|
|
*/
|
|
|
|
NTSTATUS virtual_alloc_thread_stack( void *base, SIZE_T size )
|
|
|
|
{
|
|
|
|
FILE_VIEW *view;
|
|
|
|
NTSTATUS status;
|
|
|
|
sigset_t sigset;
|
|
|
|
|
|
|
|
server_enter_uninterrupted_section( &csVirtual, &sigset );
|
|
|
|
|
|
|
|
if (base) /* already allocated, create a system view */
|
|
|
|
{
|
|
|
|
size = ROUND_SIZE( base, size );
|
|
|
|
base = ROUND_ADDR( base, page_mask );
|
|
|
|
if ((status = create_view( &view, base, size,
|
2008-11-04 13:05:32 +01:00
|
|
|
VPROT_READ | VPROT_WRITE | VPROT_COMMITTED | VPROT_VALLOC | VPROT_SYSTEM )) != STATUS_SUCCESS)
|
2008-04-01 14:11:44 +02:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size = (size + 0xffff) & ~0xffff; /* round to 64K boundary */
|
|
|
|
if ((status = map_view( &view, NULL, size, 0xffff, 0,
|
2008-11-04 13:05:32 +01:00
|
|
|
VPROT_READ | VPROT_WRITE | VPROT_COMMITTED | VPROT_VALLOC )) != STATUS_SUCCESS)
|
2008-04-01 14:11:44 +02:00
|
|
|
goto done;
|
|
|
|
#ifdef VALGRIND_STACK_REGISTER
|
|
|
|
/* no need to de-register the stack as it's the one of the main thread */
|
|
|
|
VALGRIND_STACK_REGISTER( view->base, (char *)view->base + view->size );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* setup no access guard page */
|
|
|
|
VIRTUAL_SetProt( view, view->base, page_size, VPROT_COMMITTED );
|
2008-04-01 17:40:37 +02:00
|
|
|
VIRTUAL_SetProt( view, (char *)view->base + page_size, page_size,
|
|
|
|
VPROT_READ | VPROT_WRITE | VPROT_COMMITTED | VPROT_GUARD );
|
2008-04-01 14:11:44 +02:00
|
|
|
|
|
|
|
/* note: limit is lower than base since the stack grows down */
|
|
|
|
NtCurrentTeb()->DeallocationStack = view->base;
|
|
|
|
NtCurrentTeb()->Tib.StackBase = (char *)view->base + view->size;
|
2008-04-01 17:40:37 +02:00
|
|
|
NtCurrentTeb()->Tib.StackLimit = (char *)view->base + 2 * page_size;
|
2008-04-01 14:11:44 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
server_leave_uninterrupted_section( &csVirtual, &sigset );
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-26 21:10:57 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* virtual_clear_thread_stack
|
|
|
|
*
|
|
|
|
* Clear the stack contents before calling the main entry point, some broken apps need that.
|
|
|
|
*/
|
|
|
|
void virtual_clear_thread_stack(void)
|
|
|
|
{
|
|
|
|
void *stack = NtCurrentTeb()->Tib.StackLimit;
|
|
|
|
size_t size = (char *)NtCurrentTeb()->Tib.StackBase - (char *)NtCurrentTeb()->Tib.StackLimit;
|
|
|
|
|
|
|
|
wine_anon_mmap( stack, size, PROT_READ | PROT_WRITE, MAP_FIXED );
|
|
|
|
if (force_exec_prot) mprotect( stack, size, PROT_READ | PROT_WRITE | PROT_EXEC );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-17 20:54:42 +02:00
|
|
|
/***********************************************************************
|
2008-11-25 12:02:16 +01:00
|
|
|
* virtual_handle_fault
|
2002-09-17 20:54:42 +02:00
|
|
|
*/
|
2008-11-25 12:02:16 +01:00
|
|
|
NTSTATUS virtual_handle_fault( LPCVOID addr, DWORD err )
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2004-04-06 22:16:51 +02:00
|
|
|
FILE_VIEW *view;
|
2005-07-15 13:43:09 +02:00
|
|
|
NTSTATUS ret = STATUS_ACCESS_VIOLATION;
|
2007-01-12 20:15:52 +01:00
|
|
|
sigset_t sigset;
|
2002-09-17 20:54:42 +02:00
|
|
|
|
2007-01-12 20:15:52 +01:00
|
|
|
server_enter_uninterrupted_section( &csVirtual, &sigset );
|
2008-11-25 11:58:50 +01:00
|
|
|
if ((view = VIRTUAL_FindView( addr, 0 )))
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2005-12-17 18:47:03 +01:00
|
|
|
void *page = ROUND_ADDR( addr, page_mask );
|
2008-11-25 12:07:35 +01:00
|
|
|
BYTE *vprot = &view->prot[((const char *)page - (const char *)view->base) >> page_shift];
|
|
|
|
if (*vprot & VPROT_GUARD)
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2008-11-25 12:07:35 +01:00
|
|
|
VIRTUAL_SetProt( view, page, page_size, *vprot & ~VPROT_GUARD );
|
2005-04-20 18:26:23 +02:00
|
|
|
ret = STATUS_GUARD_PAGE_VIOLATION;
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
2008-11-25 12:07:35 +01:00
|
|
|
if ((err & EXCEPTION_WRITE_FAULT) && (*vprot & VPROT_WRITEWATCH))
|
|
|
|
{
|
|
|
|
*vprot &= ~VPROT_WRITEWATCH;
|
|
|
|
VIRTUAL_SetProt( view, page, page_size, *vprot );
|
|
|
|
ret = STATUS_SUCCESS;
|
|
|
|
}
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
2007-01-12 20:15:52 +01:00
|
|
|
server_leave_uninterrupted_section( &csVirtual, &sigset );
|
2002-09-17 20:54:42 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-01 17:37:17 +02:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* virtual_handle_stack_fault
|
|
|
|
*
|
|
|
|
* Handle an access fault inside the current thread stack.
|
|
|
|
* Called from inside a signal handler.
|
|
|
|
*/
|
|
|
|
BOOL virtual_handle_stack_fault( void *addr )
|
|
|
|
{
|
|
|
|
FILE_VIEW *view;
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
RtlEnterCriticalSection( &csVirtual ); /* no need for signal masking inside signal handler */
|
2008-11-25 11:58:50 +01:00
|
|
|
if ((view = VIRTUAL_FindView( addr, 0 )))
|
2008-04-01 17:37:17 +02:00
|
|
|
{
|
|
|
|
void *page = ROUND_ADDR( addr, page_mask );
|
|
|
|
BYTE vprot = view->prot[((const char *)page - (const char *)view->base) >> page_shift];
|
|
|
|
if (vprot & VPROT_GUARD)
|
|
|
|
{
|
|
|
|
VIRTUAL_SetProt( view, page, page_size, vprot & ~VPROT_GUARD );
|
|
|
|
if ((char *)page + page_size == NtCurrentTeb()->Tib.StackLimit)
|
|
|
|
NtCurrentTeb()->Tib.StackLimit = page;
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RtlLeaveCriticalSection( &csVirtual );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-14 20:17:52 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* virtual_check_buffer_for_read
|
|
|
|
*
|
|
|
|
* Check if a memory buffer can be read, triggering page faults if needed for DIB section access.
|
|
|
|
*/
|
|
|
|
BOOL virtual_check_buffer_for_read( const void *ptr, SIZE_T size )
|
|
|
|
{
|
|
|
|
if (!size) return TRUE;
|
|
|
|
if (!ptr) return FALSE;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
volatile const char *p = ptr;
|
|
|
|
char dummy;
|
|
|
|
SIZE_T count = size;
|
|
|
|
|
|
|
|
while (count > page_size)
|
|
|
|
{
|
|
|
|
dummy = *p;
|
|
|
|
p += page_size;
|
|
|
|
count -= page_size;
|
|
|
|
}
|
|
|
|
dummy = p[0];
|
|
|
|
dummy = p[count - 1];
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-14 20:17:52 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* virtual_check_buffer_for_write
|
|
|
|
*
|
|
|
|
* Check if a memory buffer can be written to, triggering page faults if needed for write watches.
|
|
|
|
*/
|
|
|
|
BOOL virtual_check_buffer_for_write( void *ptr, SIZE_T size )
|
|
|
|
{
|
|
|
|
if (!size) return TRUE;
|
|
|
|
if (!ptr) return FALSE;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
volatile char *p = ptr;
|
|
|
|
SIZE_T count = size;
|
|
|
|
|
|
|
|
while (count > page_size)
|
|
|
|
{
|
|
|
|
*p |= 0;
|
|
|
|
p += page_size;
|
|
|
|
count -= page_size;
|
|
|
|
}
|
|
|
|
p[0] |= 0;
|
|
|
|
p[count - 1] |= 0;
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-05 15:42:29 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* VIRTUAL_SetForceExec
|
|
|
|
*
|
|
|
|
* Whether to force exec prot on all views.
|
|
|
|
*/
|
|
|
|
void VIRTUAL_SetForceExec( BOOL enable )
|
|
|
|
{
|
|
|
|
struct file_view *view;
|
2007-01-12 20:15:52 +01:00
|
|
|
sigset_t sigset;
|
2006-12-05 15:42:29 +01:00
|
|
|
|
2007-01-12 20:15:52 +01:00
|
|
|
server_enter_uninterrupted_section( &csVirtual, &sigset );
|
2006-12-05 15:42:29 +01:00
|
|
|
if (!force_exec_prot != !enable) /* change all existing views */
|
|
|
|
{
|
|
|
|
force_exec_prot = enable;
|
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY( view, &views_list, struct file_view, entry )
|
|
|
|
{
|
|
|
|
UINT i, count;
|
|
|
|
char *addr = view->base;
|
2008-11-05 12:31:49 +01:00
|
|
|
BYTE commit = view->mapping ? VPROT_COMMITTED : 0; /* file mappings are always accessible */
|
|
|
|
int unix_prot = VIRTUAL_GetUnixProt( view->prot[0] | commit );
|
2006-12-05 15:42:29 +01:00
|
|
|
|
2008-11-04 13:16:01 +01:00
|
|
|
if (view->protect & VPROT_NOEXEC) continue;
|
2006-12-05 15:42:29 +01:00
|
|
|
for (count = i = 1; i < view->size >> page_shift; i++, count++)
|
|
|
|
{
|
2008-11-05 12:31:49 +01:00
|
|
|
int prot = VIRTUAL_GetUnixProt( view->prot[i] | commit );
|
|
|
|
if (prot == unix_prot) continue;
|
2006-12-05 15:42:29 +01:00
|
|
|
if ((unix_prot & PROT_READ) && !(unix_prot & PROT_EXEC))
|
|
|
|
{
|
|
|
|
TRACE( "%s exec prot for %p-%p\n",
|
|
|
|
force_exec_prot ? "enabling" : "disabling",
|
|
|
|
addr, addr + (count << page_shift) - 1 );
|
|
|
|
mprotect( addr, count << page_shift,
|
|
|
|
unix_prot | (force_exec_prot ? PROT_EXEC : 0) );
|
|
|
|
}
|
|
|
|
addr += (count << page_shift);
|
2008-11-05 12:31:49 +01:00
|
|
|
unix_prot = prot;
|
2006-12-05 15:42:29 +01:00
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
if (count)
|
|
|
|
{
|
|
|
|
if ((unix_prot & PROT_READ) && !(unix_prot & PROT_EXEC))
|
|
|
|
{
|
|
|
|
TRACE( "%s exec prot for %p-%p\n",
|
|
|
|
force_exec_prot ? "enabling" : "disabling",
|
|
|
|
addr, addr + (count << page_shift) - 1 );
|
|
|
|
mprotect( addr, count << page_shift,
|
|
|
|
unix_prot | (force_exec_prot ? PROT_EXEC : 0) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-01-12 20:15:52 +01:00
|
|
|
server_leave_uninterrupted_section( &csVirtual, &sigset );
|
2006-12-05 15:42:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-18 02:26:57 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* VIRTUAL_UseLargeAddressSpace
|
|
|
|
*
|
|
|
|
* Increase the address space size for apps that support it.
|
|
|
|
*/
|
|
|
|
void VIRTUAL_UseLargeAddressSpace(void)
|
|
|
|
{
|
2005-12-01 18:42:39 +01:00
|
|
|
/* no large address space on win9x */
|
|
|
|
if (NtCurrentTeb()->Peb->OSPlatformId != VER_PLATFORM_WIN32_NT) return;
|
2008-11-06 11:42:58 +01:00
|
|
|
user_space_limit = working_set_limit = address_space_limit;
|
2004-06-18 02:26:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-17 20:54:42 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* NtAllocateVirtualMemory (NTDLL.@)
|
|
|
|
* ZwAllocateVirtualMemory (NTDLL.@)
|
|
|
|
*/
|
2004-10-11 22:59:06 +02:00
|
|
|
NTSTATUS WINAPI NtAllocateVirtualMemory( HANDLE process, PVOID *ret, ULONG zero_bits,
|
2005-07-15 12:01:30 +02:00
|
|
|
SIZE_T *size_ptr, ULONG type, ULONG protect )
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
|
|
|
void *base;
|
2008-11-04 13:05:32 +01:00
|
|
|
unsigned int vprot;
|
2005-07-15 12:01:30 +02:00
|
|
|
SIZE_T size = *size_ptr;
|
2006-07-13 20:36:41 +02:00
|
|
|
SIZE_T mask = get_mask( zero_bits );
|
2004-05-21 22:58:44 +02:00
|
|
|
NTSTATUS status = STATUS_SUCCESS;
|
|
|
|
struct file_view *view;
|
2007-01-12 20:15:52 +01:00
|
|
|
sigset_t sigset;
|
2002-09-17 20:54:42 +02:00
|
|
|
|
2006-10-16 13:49:06 +02:00
|
|
|
TRACE("%p %p %08lx %x %08x\n", process, *ret, size, type, protect );
|
2004-09-22 06:03:10 +02:00
|
|
|
|
|
|
|
if (!size) return STATUS_INVALID_PARAMETER;
|
|
|
|
|
2007-01-15 22:27:40 +01:00
|
|
|
if (process != NtCurrentProcess())
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2007-01-15 22:27:40 +01:00
|
|
|
apc_call_t call;
|
|
|
|
apc_result_t result;
|
|
|
|
|
2007-12-11 15:07:42 +01:00
|
|
|
memset( &call, 0, sizeof(call) );
|
|
|
|
|
2007-01-15 22:27:40 +01:00
|
|
|
call.virtual_alloc.type = APC_VIRTUAL_ALLOC;
|
2008-12-30 15:05:38 +01:00
|
|
|
call.virtual_alloc.addr = wine_server_client_ptr( *ret );
|
2007-01-15 22:27:40 +01:00
|
|
|
call.virtual_alloc.size = *size_ptr;
|
|
|
|
call.virtual_alloc.zero_bits = zero_bits;
|
|
|
|
call.virtual_alloc.op_type = type;
|
|
|
|
call.virtual_alloc.prot = protect;
|
|
|
|
status = NTDLL_queue_process_apc( process, &call, &result );
|
|
|
|
if (status != STATUS_SUCCESS) return status;
|
|
|
|
|
|
|
|
if (result.virtual_alloc.status == STATUS_SUCCESS)
|
|
|
|
{
|
2008-12-30 15:05:38 +01:00
|
|
|
*ret = wine_server_get_ptr( result.virtual_alloc.addr );
|
2007-01-15 22:27:40 +01:00
|
|
|
*size_ptr = result.virtual_alloc.size;
|
|
|
|
}
|
|
|
|
return result.virtual_alloc.status;
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Round parameters to a page boundary */
|
|
|
|
|
2008-11-06 11:42:58 +01:00
|
|
|
if (is_beyond_limit( 0, size, working_set_limit )) return STATUS_WORKING_SET_LIMIT_RANGE;
|
2002-09-17 20:54:42 +02:00
|
|
|
|
2008-12-11 14:05:42 +01:00
|
|
|
if ((status = get_vprot_flags( protect, &vprot ))) return status;
|
|
|
|
vprot |= VPROT_VALLOC;
|
2008-11-18 20:11:49 +01:00
|
|
|
if (type & MEM_COMMIT) vprot |= VPROT_COMMITTED;
|
|
|
|
|
2004-10-11 22:59:06 +02:00
|
|
|
if (*ret)
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
|
|
|
if (type & MEM_RESERVE) /* Round down to 64k boundary */
|
2006-07-13 20:36:41 +02:00
|
|
|
base = ROUND_ADDR( *ret, mask );
|
2002-09-17 20:54:42 +02:00
|
|
|
else
|
2004-10-11 22:59:06 +02:00
|
|
|
base = ROUND_ADDR( *ret, page_mask );
|
|
|
|
size = (((UINT_PTR)*ret + size + page_mask) & ~page_mask) - (UINT_PTR)base;
|
2002-09-17 20:54:42 +02:00
|
|
|
|
2008-11-18 20:11:49 +01:00
|
|
|
/* address 1 is magic to mean DOS area */
|
|
|
|
if (!base && *ret == (void *)1 && size == 0x110000)
|
|
|
|
{
|
|
|
|
server_enter_uninterrupted_section( &csVirtual, &sigset );
|
|
|
|
status = allocate_dos_memory( &view, vprot );
|
|
|
|
if (status == STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
*ret = view->base;
|
|
|
|
*size_ptr = view->size;
|
|
|
|
}
|
|
|
|
server_leave_uninterrupted_section( &csVirtual, &sigset );
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2002-09-17 20:54:42 +02:00
|
|
|
/* disallow low 64k, wrap-around and kernel space */
|
2006-07-13 20:36:41 +02:00
|
|
|
if (((char *)base < (char *)0x10000) ||
|
2002-09-17 20:54:42 +02:00
|
|
|
((char *)base + size < (char *)base) ||
|
2008-10-28 13:51:36 +01:00
|
|
|
is_beyond_limit( base, size, address_space_limit ))
|
2002-09-17 20:54:42 +02:00
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
base = NULL;
|
|
|
|
size = (size + page_mask) & ~page_mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute the alloc type flags */
|
|
|
|
|
2008-11-18 20:14:46 +01:00
|
|
|
if (!(type & (MEM_COMMIT | MEM_RESERVE)) ||
|
|
|
|
(type & ~(MEM_COMMIT | MEM_RESERVE | MEM_TOP_DOWN | MEM_WRITE_WATCH | MEM_RESET)))
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2008-11-18 20:14:46 +01:00
|
|
|
WARN("called with wrong alloc type flags (%08x) !\n", type);
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
2002-09-17 20:54:42 +02:00
|
|
|
|
2003-11-03 23:21:55 +01:00
|
|
|
/* Reserve the memory */
|
|
|
|
|
2007-01-12 20:15:52 +01:00
|
|
|
if (use_locks) server_enter_uninterrupted_section( &csVirtual, &sigset );
|
2004-05-21 22:58:44 +02:00
|
|
|
|
2008-11-18 20:14:46 +01:00
|
|
|
if ((type & MEM_RESERVE) || !base)
|
2003-11-03 23:21:55 +01:00
|
|
|
{
|
2008-11-25 12:07:35 +01:00
|
|
|
if (type & MEM_WRITE_WATCH) vprot |= VPROT_WRITEWATCH;
|
2006-07-24 13:59:44 +02:00
|
|
|
status = map_view( &view, base, size, mask, type & MEM_TOP_DOWN, vprot );
|
2008-11-04 13:05:32 +01:00
|
|
|
if (status == STATUS_SUCCESS) base = view->base;
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
2004-05-21 22:58:44 +02:00
|
|
|
else /* commit the pages */
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2008-11-25 11:58:50 +01:00
|
|
|
if (!(view = VIRTUAL_FindView( base, size ))) status = STATUS_NOT_MAPPED_VIEW;
|
2004-04-06 22:16:51 +02:00
|
|
|
else if (!VIRTUAL_SetProt( view, base, size, vprot )) status = STATUS_ACCESS_DENIED;
|
2008-11-05 12:24:05 +01:00
|
|
|
else if (view->mapping && !(view->protect & VPROT_COMMITTED))
|
|
|
|
{
|
|
|
|
SERVER_START_REQ( add_mapping_committed_range )
|
|
|
|
{
|
2008-12-08 16:05:17 +01:00
|
|
|
req->handle = wine_server_obj_handle( view->mapping );
|
2008-11-05 12:24:05 +01:00
|
|
|
req->offset = (char *)base - (char *)view->base;
|
|
|
|
req->size = size;
|
|
|
|
wine_server_call( req );
|
|
|
|
}
|
|
|
|
SERVER_END_REQ;
|
|
|
|
}
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
|
|
|
|
2007-01-12 20:15:52 +01:00
|
|
|
if (use_locks) server_leave_uninterrupted_section( &csVirtual, &sigset );
|
2004-05-21 22:58:44 +02:00
|
|
|
|
|
|
|
if (status == STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
*ret = base;
|
|
|
|
*size_ptr = size;
|
|
|
|
}
|
|
|
|
return status;
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* NtFreeVirtualMemory (NTDLL.@)
|
|
|
|
* ZwFreeVirtualMemory (NTDLL.@)
|
|
|
|
*/
|
2005-07-15 12:01:30 +02:00
|
|
|
NTSTATUS WINAPI NtFreeVirtualMemory( HANDLE process, PVOID *addr_ptr, SIZE_T *size_ptr, ULONG type )
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
|
|
|
FILE_VIEW *view;
|
|
|
|
char *base;
|
2007-01-12 20:15:52 +01:00
|
|
|
sigset_t sigset;
|
2004-04-06 22:16:51 +02:00
|
|
|
NTSTATUS status = STATUS_SUCCESS;
|
2002-09-17 20:54:42 +02:00
|
|
|
LPVOID addr = *addr_ptr;
|
2005-07-15 12:01:30 +02:00
|
|
|
SIZE_T size = *size_ptr;
|
2002-09-17 20:54:42 +02:00
|
|
|
|
2006-10-16 13:49:06 +02:00
|
|
|
TRACE("%p %p %08lx %x\n", process, addr, size, type );
|
2004-09-22 06:03:10 +02:00
|
|
|
|
2007-01-15 22:27:40 +01:00
|
|
|
if (process != NtCurrentProcess())
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2007-01-15 22:27:40 +01:00
|
|
|
apc_call_t call;
|
|
|
|
apc_result_t result;
|
|
|
|
|
2007-12-11 15:07:42 +01:00
|
|
|
memset( &call, 0, sizeof(call) );
|
|
|
|
|
2007-01-15 22:27:40 +01:00
|
|
|
call.virtual_free.type = APC_VIRTUAL_FREE;
|
2008-12-30 15:05:38 +01:00
|
|
|
call.virtual_free.addr = wine_server_client_ptr( addr );
|
2007-01-15 22:27:40 +01:00
|
|
|
call.virtual_free.size = size;
|
|
|
|
call.virtual_free.op_type = type;
|
|
|
|
status = NTDLL_queue_process_apc( process, &call, &result );
|
|
|
|
if (status != STATUS_SUCCESS) return status;
|
|
|
|
|
|
|
|
if (result.virtual_free.status == STATUS_SUCCESS)
|
|
|
|
{
|
2008-12-30 15:05:38 +01:00
|
|
|
*addr_ptr = wine_server_get_ptr( result.virtual_free.addr );
|
2007-01-15 22:27:40 +01:00
|
|
|
*size_ptr = result.virtual_free.size;
|
|
|
|
}
|
|
|
|
return result.virtual_free.status;
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Fix the parameters */
|
|
|
|
|
|
|
|
size = ROUND_SIZE( addr, size );
|
|
|
|
base = ROUND_ADDR( addr, page_mask );
|
|
|
|
|
2006-12-01 11:38:52 +01:00
|
|
|
/* avoid freeing the DOS area when a broken app passes a NULL pointer */
|
2008-11-18 20:14:46 +01:00
|
|
|
if (!base) return STATUS_INVALID_PARAMETER;
|
2006-12-01 11:38:52 +01:00
|
|
|
|
2007-01-12 20:15:52 +01:00
|
|
|
server_enter_uninterrupted_section( &csVirtual, &sigset );
|
2004-04-06 22:16:51 +02:00
|
|
|
|
2008-11-25 11:58:50 +01:00
|
|
|
if (!(view = VIRTUAL_FindView( base, size )) || !(view->protect & VPROT_VALLOC))
|
2004-04-06 22:16:51 +02:00
|
|
|
{
|
|
|
|
status = STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
else if (type == MEM_RELEASE)
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2004-02-12 00:56:52 +01:00
|
|
|
/* Free the pages */
|
|
|
|
|
2004-04-06 22:16:51 +02:00
|
|
|
if (size || (base != view->base)) status = STATUS_INVALID_PARAMETER;
|
|
|
|
else
|
|
|
|
{
|
2004-05-25 03:29:24 +02:00
|
|
|
delete_view( view );
|
2004-04-06 22:16:51 +02:00
|
|
|
*addr_ptr = base;
|
|
|
|
*size_ptr = size;
|
|
|
|
}
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
2004-02-12 00:56:52 +01:00
|
|
|
else if (type == MEM_DECOMMIT)
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2004-05-21 22:58:44 +02:00
|
|
|
status = decommit_pages( view, base - (char *)view->base, size );
|
|
|
|
if (status == STATUS_SUCCESS)
|
2004-04-06 22:16:51 +02:00
|
|
|
{
|
|
|
|
*addr_ptr = base;
|
|
|
|
*size_ptr = size;
|
|
|
|
}
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
2004-02-12 00:56:52 +01:00
|
|
|
else
|
|
|
|
{
|
2006-10-16 13:49:06 +02:00
|
|
|
WARN("called with wrong free type flags (%08x) !\n", type);
|
2004-04-06 22:16:51 +02:00
|
|
|
status = STATUS_INVALID_PARAMETER;
|
2004-02-12 00:56:52 +01:00
|
|
|
}
|
2002-09-17 20:54:42 +02:00
|
|
|
|
2007-01-12 20:15:52 +01:00
|
|
|
server_leave_uninterrupted_section( &csVirtual, &sigset );
|
2004-04-06 22:16:51 +02:00
|
|
|
return status;
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* NtProtectVirtualMemory (NTDLL.@)
|
|
|
|
* ZwProtectVirtualMemory (NTDLL.@)
|
|
|
|
*/
|
2005-07-15 12:01:30 +02:00
|
|
|
NTSTATUS WINAPI NtProtectVirtualMemory( HANDLE process, PVOID *addr_ptr, SIZE_T *size_ptr,
|
2002-09-17 20:54:42 +02:00
|
|
|
ULONG new_prot, ULONG *old_prot )
|
|
|
|
{
|
|
|
|
FILE_VIEW *view;
|
2007-01-12 20:15:52 +01:00
|
|
|
sigset_t sigset;
|
2004-04-06 22:16:51 +02:00
|
|
|
NTSTATUS status = STATUS_SUCCESS;
|
2002-09-17 20:54:42 +02:00
|
|
|
char *base;
|
2008-11-05 12:24:05 +01:00
|
|
|
BYTE vprot;
|
2008-12-11 14:05:42 +01:00
|
|
|
unsigned int new_vprot;
|
2005-07-15 12:01:30 +02:00
|
|
|
SIZE_T size = *size_ptr;
|
2002-09-17 20:54:42 +02:00
|
|
|
LPVOID addr = *addr_ptr;
|
|
|
|
|
2006-10-16 13:49:06 +02:00
|
|
|
TRACE("%p %p %08lx %08x\n", process, addr, size, new_prot );
|
2004-09-22 06:03:10 +02:00
|
|
|
|
2007-01-15 22:30:04 +01:00
|
|
|
if (process != NtCurrentProcess())
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2007-01-15 22:30:04 +01:00
|
|
|
apc_call_t call;
|
|
|
|
apc_result_t result;
|
|
|
|
|
2007-12-11 15:07:42 +01:00
|
|
|
memset( &call, 0, sizeof(call) );
|
|
|
|
|
2007-01-15 22:30:04 +01:00
|
|
|
call.virtual_protect.type = APC_VIRTUAL_PROTECT;
|
2008-12-30 15:05:38 +01:00
|
|
|
call.virtual_protect.addr = wine_server_client_ptr( addr );
|
2007-01-15 22:30:04 +01:00
|
|
|
call.virtual_protect.size = size;
|
|
|
|
call.virtual_protect.prot = new_prot;
|
|
|
|
status = NTDLL_queue_process_apc( process, &call, &result );
|
|
|
|
if (status != STATUS_SUCCESS) return status;
|
|
|
|
|
|
|
|
if (result.virtual_protect.status == STATUS_SUCCESS)
|
|
|
|
{
|
2008-12-30 15:05:38 +01:00
|
|
|
*addr_ptr = wine_server_get_ptr( result.virtual_protect.addr );
|
2007-01-15 22:30:04 +01:00
|
|
|
*size_ptr = result.virtual_protect.size;
|
|
|
|
if (old_prot) *old_prot = result.virtual_protect.prot;
|
|
|
|
}
|
|
|
|
return result.virtual_protect.status;
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Fix the parameters */
|
|
|
|
|
|
|
|
size = ROUND_SIZE( addr, size );
|
|
|
|
base = ROUND_ADDR( addr, page_mask );
|
2008-12-11 14:05:42 +01:00
|
|
|
if ((status = get_vprot_flags( new_prot, &new_vprot ))) return status;
|
|
|
|
new_vprot |= VPROT_COMMITTED;
|
2002-09-17 20:54:42 +02:00
|
|
|
|
2007-01-12 20:15:52 +01:00
|
|
|
server_enter_uninterrupted_section( &csVirtual, &sigset );
|
2002-09-17 20:54:42 +02:00
|
|
|
|
2008-11-25 11:58:50 +01:00
|
|
|
if (!(view = VIRTUAL_FindView( base, size )))
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2004-04-06 22:16:51 +02:00
|
|
|
status = STATUS_INVALID_PARAMETER;
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
2004-04-06 22:16:51 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Make sure all the pages are committed */
|
2008-11-05 12:24:05 +01:00
|
|
|
if (get_committed_size( view, base, &vprot ) >= size && (vprot & VPROT_COMMITTED))
|
2004-04-06 22:16:51 +02:00
|
|
|
{
|
2008-11-05 12:24:05 +01:00
|
|
|
if (old_prot) *old_prot = VIRTUAL_GetWin32Prot( vprot );
|
2008-12-11 14:05:42 +01:00
|
|
|
if (!VIRTUAL_SetProt( view, base, size, new_vprot )) status = STATUS_ACCESS_DENIED;
|
2004-04-06 22:16:51 +02:00
|
|
|
}
|
2008-11-05 12:24:05 +01:00
|
|
|
else status = STATUS_NOT_COMMITTED;
|
2004-04-06 22:16:51 +02:00
|
|
|
}
|
2007-01-12 20:15:52 +01:00
|
|
|
server_leave_uninterrupted_section( &csVirtual, &sigset );
|
2002-09-17 20:54:42 +02:00
|
|
|
|
2004-04-06 22:16:51 +02:00
|
|
|
if (status == STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
*addr_ptr = base;
|
|
|
|
*size_ptr = size;
|
|
|
|
}
|
|
|
|
return status;
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
|
|
|
|
2008-11-06 15:00:58 +01:00
|
|
|
|
|
|
|
/* retrieve state for a free memory area; callback for wine_mmap_enum_reserved_areas */
|
|
|
|
static int get_free_mem_state_callback( void *start, size_t size, void *arg )
|
|
|
|
{
|
|
|
|
MEMORY_BASIC_INFORMATION *info = arg;
|
|
|
|
void *end = (char *)start + size;
|
|
|
|
|
|
|
|
if ((char *)info->BaseAddress + info->RegionSize < (char *)start) return 0;
|
|
|
|
|
|
|
|
if (info->BaseAddress >= end)
|
|
|
|
{
|
|
|
|
if (info->AllocationBase < end) info->AllocationBase = end;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info->BaseAddress >= start)
|
|
|
|
{
|
|
|
|
/* it's a real free area */
|
|
|
|
info->State = MEM_FREE;
|
|
|
|
info->Protect = PAGE_NOACCESS;
|
|
|
|
info->AllocationBase = 0;
|
|
|
|
info->AllocationProtect = 0;
|
|
|
|
info->Type = 0;
|
|
|
|
if ((char *)info->BaseAddress + info->RegionSize > (char *)end)
|
|
|
|
info->RegionSize = (char *)end - (char *)info->BaseAddress;
|
|
|
|
}
|
|
|
|
else /* outside of the reserved area, pretend it's allocated */
|
|
|
|
{
|
|
|
|
info->RegionSize = (char *)start - (char *)info->BaseAddress;
|
|
|
|
info->State = MEM_RESERVE;
|
|
|
|
info->Protect = PAGE_NOACCESS;
|
|
|
|
info->AllocationProtect = PAGE_NOACCESS;
|
|
|
|
info->Type = MEM_PRIVATE;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-07-05 16:05:58 +02:00
|
|
|
#define UNIMPLEMENTED_INFO_CLASS(c) \
|
|
|
|
case c: \
|
|
|
|
FIXME("(process=%p,addr=%p) Unimplemented information class: " #c "\n", process, addr); \
|
|
|
|
return STATUS_INVALID_INFO_CLASS
|
2002-09-17 20:54:42 +02:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* NtQueryVirtualMemory (NTDLL.@)
|
|
|
|
* ZwQueryVirtualMemory (NTDLL.@)
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI NtQueryVirtualMemory( HANDLE process, LPCVOID addr,
|
|
|
|
MEMORY_INFORMATION_CLASS info_class, PVOID buffer,
|
2005-07-15 12:01:30 +02:00
|
|
|
SIZE_T len, SIZE_T *res_len )
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
|
|
|
FILE_VIEW *view;
|
|
|
|
char *base, *alloc_base = 0;
|
2004-05-21 22:58:44 +02:00
|
|
|
struct list *ptr;
|
2005-07-15 12:01:30 +02:00
|
|
|
SIZE_T size = 0;
|
2002-09-17 20:54:42 +02:00
|
|
|
MEMORY_BASIC_INFORMATION *info = buffer;
|
2007-01-12 20:15:52 +01:00
|
|
|
sigset_t sigset;
|
2002-09-17 20:54:42 +02:00
|
|
|
|
2005-01-20 20:59:48 +01:00
|
|
|
if (info_class != MemoryBasicInformation)
|
|
|
|
{
|
2005-07-05 16:05:58 +02:00
|
|
|
switch(info_class)
|
|
|
|
{
|
|
|
|
UNIMPLEMENTED_INFO_CLASS(MemoryWorkingSetList);
|
|
|
|
UNIMPLEMENTED_INFO_CLASS(MemorySectionName);
|
|
|
|
UNIMPLEMENTED_INFO_CLASS(MemoryBasicVlmInformation);
|
|
|
|
|
|
|
|
default:
|
|
|
|
FIXME("(%p,%p,info_class=%d,%p,%ld,%p) Unknown information class\n",
|
|
|
|
process, addr, info_class, buffer, len, res_len);
|
|
|
|
return STATUS_INVALID_INFO_CLASS;
|
|
|
|
}
|
2005-01-20 20:59:48 +01:00
|
|
|
}
|
2002-09-17 20:54:42 +02:00
|
|
|
|
2007-01-15 22:28:42 +01:00
|
|
|
if (process != NtCurrentProcess())
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2007-01-15 22:28:42 +01:00
|
|
|
NTSTATUS status;
|
|
|
|
apc_call_t call;
|
|
|
|
apc_result_t result;
|
|
|
|
|
2007-12-11 15:07:42 +01:00
|
|
|
memset( &call, 0, sizeof(call) );
|
|
|
|
|
2007-01-15 22:28:42 +01:00
|
|
|
call.virtual_query.type = APC_VIRTUAL_QUERY;
|
2008-12-30 15:05:38 +01:00
|
|
|
call.virtual_query.addr = wine_server_client_ptr( addr );
|
2007-01-15 22:28:42 +01:00
|
|
|
status = NTDLL_queue_process_apc( process, &call, &result );
|
|
|
|
if (status != STATUS_SUCCESS) return status;
|
|
|
|
|
|
|
|
if (result.virtual_query.status == STATUS_SUCCESS)
|
|
|
|
{
|
2008-12-30 15:05:38 +01:00
|
|
|
info->BaseAddress = wine_server_get_ptr( result.virtual_query.base );
|
|
|
|
info->AllocationBase = wine_server_get_ptr( result.virtual_query.alloc_base );
|
2007-01-15 22:28:42 +01:00
|
|
|
info->RegionSize = result.virtual_query.size;
|
|
|
|
info->Protect = result.virtual_query.prot;
|
|
|
|
info->AllocationProtect = result.virtual_query.alloc_prot;
|
2008-12-30 15:22:45 +01:00
|
|
|
info->State = (DWORD)result.virtual_query.state << 12;
|
|
|
|
info->Type = (DWORD)result.virtual_query.alloc_type << 16;
|
2008-12-17 19:25:09 +01:00
|
|
|
if (info->RegionSize != result.virtual_query.size) /* truncated */
|
|
|
|
return STATUS_INVALID_PARAMETER; /* FIXME */
|
2007-01-15 22:28:42 +01:00
|
|
|
if (res_len) *res_len = sizeof(*info);
|
|
|
|
}
|
|
|
|
return result.virtual_query.status;
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
base = ROUND_ADDR( addr, page_mask );
|
|
|
|
|
2008-11-06 11:42:58 +01:00
|
|
|
if (is_beyond_limit( base, 1, working_set_limit )) return STATUS_WORKING_SET_LIMIT_RANGE;
|
|
|
|
|
2002-09-17 20:54:42 +02:00
|
|
|
/* Find the view containing the address */
|
|
|
|
|
2007-01-12 20:15:52 +01:00
|
|
|
server_enter_uninterrupted_section( &csVirtual, &sigset );
|
2004-05-21 22:58:44 +02:00
|
|
|
ptr = list_head( &views_list );
|
2002-09-17 20:54:42 +02:00
|
|
|
for (;;)
|
|
|
|
{
|
2004-05-21 22:58:44 +02:00
|
|
|
if (!ptr)
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2008-11-06 11:42:58 +01:00
|
|
|
size = (char *)working_set_limit - alloc_base;
|
2004-05-21 22:58:44 +02:00
|
|
|
view = NULL;
|
2002-09-17 20:54:42 +02:00
|
|
|
break;
|
|
|
|
}
|
2004-05-21 22:58:44 +02:00
|
|
|
view = LIST_ENTRY( ptr, struct file_view, entry );
|
2002-09-17 20:54:42 +02:00
|
|
|
if ((char *)view->base > base)
|
|
|
|
{
|
|
|
|
size = (char *)view->base - alloc_base;
|
|
|
|
view = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((char *)view->base + view->size > base)
|
|
|
|
{
|
|
|
|
alloc_base = view->base;
|
|
|
|
size = view->size;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
alloc_base = (char *)view->base + view->size;
|
2004-05-21 22:58:44 +02:00
|
|
|
ptr = list_next( &views_list, ptr );
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Fill the info structure */
|
|
|
|
|
2008-11-06 15:00:58 +01:00
|
|
|
info->AllocationBase = alloc_base;
|
|
|
|
info->BaseAddress = base;
|
|
|
|
info->RegionSize = size - (base - alloc_base);
|
|
|
|
|
2002-09-17 20:54:42 +02:00
|
|
|
if (!view)
|
|
|
|
{
|
2008-11-06 15:00:58 +01:00
|
|
|
if (!wine_mmap_enum_reserved_areas( get_free_mem_state_callback, info, 0 ))
|
|
|
|
{
|
|
|
|
/* not in a reserved area at all, pretend it's allocated */
|
|
|
|
info->State = MEM_RESERVE;
|
|
|
|
info->Protect = PAGE_NOACCESS;
|
|
|
|
info->AllocationProtect = PAGE_NOACCESS;
|
|
|
|
info->Type = MEM_PRIVATE;
|
|
|
|
}
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-11-05 12:24:05 +01:00
|
|
|
BYTE vprot;
|
|
|
|
SIZE_T range_size = get_committed_size( view, base, &vprot );
|
|
|
|
|
2005-12-14 12:17:59 +01:00
|
|
|
info->State = (vprot & VPROT_COMMITTED) ? MEM_COMMIT : MEM_RESERVE;
|
2008-11-05 12:24:05 +01:00
|
|
|
info->Protect = (vprot & VPROT_COMMITTED) ? VIRTUAL_GetWin32Prot( vprot ) : 0;
|
2006-12-07 15:34:36 +01:00
|
|
|
info->AllocationBase = alloc_base;
|
2005-12-14 12:17:59 +01:00
|
|
|
info->AllocationProtect = VIRTUAL_GetWin32Prot( view->protect );
|
2003-11-03 23:21:55 +01:00
|
|
|
if (view->protect & VPROT_IMAGE) info->Type = MEM_IMAGE;
|
2008-11-04 13:05:32 +01:00
|
|
|
else if (view->protect & VPROT_VALLOC) info->Type = MEM_PRIVATE;
|
2003-11-03 23:21:55 +01:00
|
|
|
else info->Type = MEM_MAPPED;
|
2008-11-05 12:24:05 +01:00
|
|
|
for (size = base - alloc_base; size < base + range_size - alloc_base; size += page_size)
|
2008-11-25 12:07:35 +01:00
|
|
|
if ((view->prot[size >> page_shift] ^ vprot) & ~VPROT_WRITEWATCH) break;
|
2008-11-06 15:00:58 +01:00
|
|
|
info->RegionSize = size - (base - alloc_base);
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
2007-01-12 20:15:52 +01:00
|
|
|
server_leave_uninterrupted_section( &csVirtual, &sigset );
|
2002-09-17 20:54:42 +02:00
|
|
|
|
2003-05-08 05:50:32 +02:00
|
|
|
if (res_len) *res_len = sizeof(*info);
|
2002-09-17 20:54:42 +02:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* NtLockVirtualMemory (NTDLL.@)
|
|
|
|
* ZwLockVirtualMemory (NTDLL.@)
|
|
|
|
*/
|
2005-07-15 12:01:30 +02:00
|
|
|
NTSTATUS WINAPI NtLockVirtualMemory( HANDLE process, PVOID *addr, SIZE_T *size, ULONG unknown )
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2007-01-16 09:50:08 +01:00
|
|
|
NTSTATUS status = STATUS_SUCCESS;
|
|
|
|
|
|
|
|
if (process != NtCurrentProcess())
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2007-01-16 09:50:08 +01:00
|
|
|
apc_call_t call;
|
|
|
|
apc_result_t result;
|
|
|
|
|
2007-12-11 15:07:42 +01:00
|
|
|
memset( &call, 0, sizeof(call) );
|
|
|
|
|
2007-01-16 09:50:08 +01:00
|
|
|
call.virtual_lock.type = APC_VIRTUAL_LOCK;
|
2008-12-30 15:05:38 +01:00
|
|
|
call.virtual_lock.addr = wine_server_client_ptr( *addr );
|
2007-01-16 09:50:08 +01:00
|
|
|
call.virtual_lock.size = *size;
|
|
|
|
status = NTDLL_queue_process_apc( process, &call, &result );
|
|
|
|
if (status != STATUS_SUCCESS) return status;
|
|
|
|
|
|
|
|
if (result.virtual_lock.status == STATUS_SUCCESS)
|
|
|
|
{
|
2008-12-30 15:05:38 +01:00
|
|
|
*addr = wine_server_get_ptr( result.virtual_lock.addr );
|
2007-01-16 09:50:08 +01:00
|
|
|
*size = result.virtual_lock.size;
|
|
|
|
}
|
|
|
|
return result.virtual_lock.status;
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
2007-01-16 09:50:08 +01:00
|
|
|
|
|
|
|
*size = ROUND_SIZE( *addr, *size );
|
|
|
|
*addr = ROUND_ADDR( *addr, page_mask );
|
|
|
|
|
|
|
|
if (mlock( *addr, *size )) status = STATUS_ACCESS_DENIED;
|
|
|
|
return status;
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* NtUnlockVirtualMemory (NTDLL.@)
|
|
|
|
* ZwUnlockVirtualMemory (NTDLL.@)
|
|
|
|
*/
|
2005-07-15 12:01:30 +02:00
|
|
|
NTSTATUS WINAPI NtUnlockVirtualMemory( HANDLE process, PVOID *addr, SIZE_T *size, ULONG unknown )
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2007-01-16 09:50:08 +01:00
|
|
|
NTSTATUS status = STATUS_SUCCESS;
|
|
|
|
|
|
|
|
if (process != NtCurrentProcess())
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2007-01-16 09:50:08 +01:00
|
|
|
apc_call_t call;
|
|
|
|
apc_result_t result;
|
|
|
|
|
2007-12-11 15:07:42 +01:00
|
|
|
memset( &call, 0, sizeof(call) );
|
|
|
|
|
2007-01-16 09:50:08 +01:00
|
|
|
call.virtual_unlock.type = APC_VIRTUAL_UNLOCK;
|
2008-12-30 15:05:38 +01:00
|
|
|
call.virtual_unlock.addr = wine_server_client_ptr( *addr );
|
2007-01-16 09:50:08 +01:00
|
|
|
call.virtual_unlock.size = *size;
|
|
|
|
status = NTDLL_queue_process_apc( process, &call, &result );
|
|
|
|
if (status != STATUS_SUCCESS) return status;
|
|
|
|
|
|
|
|
if (result.virtual_unlock.status == STATUS_SUCCESS)
|
|
|
|
{
|
2008-12-30 15:05:38 +01:00
|
|
|
*addr = wine_server_get_ptr( result.virtual_unlock.addr );
|
2007-01-16 09:50:08 +01:00
|
|
|
*size = result.virtual_unlock.size;
|
|
|
|
}
|
|
|
|
return result.virtual_unlock.status;
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
2007-01-16 09:50:08 +01:00
|
|
|
|
|
|
|
*size = ROUND_SIZE( *addr, *size );
|
|
|
|
*addr = ROUND_ADDR( *addr, page_mask );
|
|
|
|
|
|
|
|
if (munlock( *addr, *size )) status = STATUS_ACCESS_DENIED;
|
|
|
|
return status;
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* NtCreateSection (NTDLL.@)
|
|
|
|
* ZwCreateSection (NTDLL.@)
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI NtCreateSection( HANDLE *handle, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr,
|
|
|
|
const LARGE_INTEGER *size, ULONG protect,
|
|
|
|
ULONG sec_flags, HANDLE file )
|
|
|
|
{
|
|
|
|
NTSTATUS ret;
|
2008-11-04 13:05:32 +01:00
|
|
|
unsigned int vprot;
|
2005-04-19 12:31:28 +02:00
|
|
|
DWORD len = (attr && attr->ObjectName) ? attr->ObjectName->Length : 0;
|
2007-10-25 20:09:54 +02:00
|
|
|
struct security_descriptor *sd = NULL;
|
|
|
|
struct object_attributes objattr;
|
2002-09-17 20:54:42 +02:00
|
|
|
|
|
|
|
/* Check parameters */
|
|
|
|
|
|
|
|
if (len > MAX_PATH*sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
|
|
|
|
|
2008-12-11 14:05:42 +01:00
|
|
|
if ((ret = get_vprot_flags( protect, &vprot ))) return ret;
|
|
|
|
|
2008-12-08 16:05:17 +01:00
|
|
|
objattr.rootdir = wine_server_obj_handle( attr ? attr->RootDirectory : 0 );
|
2007-10-25 20:09:54 +02:00
|
|
|
objattr.sd_len = 0;
|
2007-10-26 18:01:33 +02:00
|
|
|
objattr.name_len = len;
|
2007-10-25 20:09:54 +02:00
|
|
|
if (attr)
|
|
|
|
{
|
|
|
|
ret = NTDLL_create_struct_sd( attr->SecurityDescriptor, &sd, &objattr.sd_len );
|
|
|
|
if (ret != STATUS_SUCCESS) return ret;
|
|
|
|
}
|
|
|
|
|
2008-11-05 12:31:49 +01:00
|
|
|
if (!(sec_flags & SEC_RESERVE)) vprot |= VPROT_COMMITTED;
|
2002-09-17 20:54:42 +02:00
|
|
|
if (sec_flags & SEC_NOCACHE) vprot |= VPROT_NOCACHE;
|
|
|
|
if (sec_flags & SEC_IMAGE) vprot |= VPROT_IMAGE;
|
|
|
|
|
|
|
|
/* Create the server object */
|
|
|
|
|
|
|
|
SERVER_START_REQ( create_mapping )
|
|
|
|
{
|
2005-10-27 20:30:37 +02:00
|
|
|
req->access = access;
|
|
|
|
req->attributes = (attr) ? attr->Attributes : 0;
|
2008-12-08 16:05:17 +01:00
|
|
|
req->file_handle = wine_server_obj_handle( file );
|
2007-10-10 14:06:25 +02:00
|
|
|
req->size = size ? size->QuadPart : 0;
|
2002-09-17 20:54:42 +02:00
|
|
|
req->protect = vprot;
|
2007-10-25 20:09:54 +02:00
|
|
|
wine_server_add_data( req, &objattr, sizeof(objattr) );
|
|
|
|
if (objattr.sd_len) wine_server_add_data( req, sd, objattr.sd_len );
|
2002-09-17 20:54:42 +02:00
|
|
|
if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
|
|
|
|
ret = wine_server_call( req );
|
2008-12-08 16:05:17 +01:00
|
|
|
*handle = wine_server_ptr_handle( reply->handle );
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
|
|
|
SERVER_END_REQ;
|
2007-10-25 20:09:54 +02:00
|
|
|
|
|
|
|
NTDLL_free_struct_sd( sd );
|
|
|
|
|
2002-09-17 20:54:42 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* NtOpenSection (NTDLL.@)
|
|
|
|
* ZwOpenSection (NTDLL.@)
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI NtOpenSection( HANDLE *handle, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr )
|
|
|
|
{
|
|
|
|
NTSTATUS ret;
|
|
|
|
DWORD len = attr->ObjectName->Length;
|
|
|
|
|
|
|
|
if (len > MAX_PATH*sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
|
|
|
|
|
|
|
|
SERVER_START_REQ( open_mapping )
|
|
|
|
{
|
|
|
|
req->access = access;
|
2008-05-20 07:48:29 +02:00
|
|
|
req->attributes = attr->Attributes;
|
2008-12-08 16:05:17 +01:00
|
|
|
req->rootdir = wine_server_obj_handle( attr->RootDirectory );
|
2002-09-17 20:54:42 +02:00
|
|
|
wine_server_add_data( req, attr->ObjectName->Buffer, len );
|
2008-12-08 16:05:17 +01:00
|
|
|
if (!(ret = wine_server_call( req ))) *handle = wine_server_ptr_handle( reply->handle );
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
|
|
|
SERVER_END_REQ;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* NtMapViewOfSection (NTDLL.@)
|
|
|
|
* ZwMapViewOfSection (NTDLL.@)
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI NtMapViewOfSection( HANDLE handle, HANDLE process, PVOID *addr_ptr, ULONG zero_bits,
|
2005-07-15 12:01:30 +02:00
|
|
|
SIZE_T commit_size, const LARGE_INTEGER *offset_ptr, SIZE_T *size_ptr,
|
2002-09-17 20:54:42 +02:00
|
|
|
SECTION_INHERIT inherit, ULONG alloc_type, ULONG protect )
|
|
|
|
{
|
|
|
|
NTSTATUS res;
|
2008-12-17 19:25:49 +01:00
|
|
|
mem_size_t full_size;
|
2008-11-05 20:32:32 +01:00
|
|
|
ACCESS_MASK access;
|
2005-07-15 12:01:30 +02:00
|
|
|
SIZE_T size = 0;
|
2006-07-13 20:36:41 +02:00
|
|
|
SIZE_T mask = get_mask( zero_bits );
|
2007-01-12 14:55:31 +01:00
|
|
|
int unix_handle = -1, needs_close;
|
2008-11-05 20:32:32 +01:00
|
|
|
unsigned int map_vprot, vprot;
|
2004-05-21 22:58:44 +02:00
|
|
|
void *base;
|
|
|
|
struct file_view *view;
|
2007-10-10 14:06:25 +02:00
|
|
|
DWORD header_size;
|
2007-01-12 14:55:31 +01:00
|
|
|
HANDLE dup_mapping, shared_file;
|
2005-04-19 12:31:28 +02:00
|
|
|
LARGE_INTEGER offset;
|
2007-01-12 20:15:52 +01:00
|
|
|
sigset_t sigset;
|
2005-04-19 12:31:28 +02:00
|
|
|
|
|
|
|
offset.QuadPart = offset_ptr ? offset_ptr->QuadPart : 0;
|
2002-09-17 20:54:42 +02:00
|
|
|
|
2006-10-16 13:49:06 +02:00
|
|
|
TRACE("handle=%p process=%p addr=%p off=%x%08x size=%lx access=%x\n",
|
2005-04-19 12:31:28 +02:00
|
|
|
handle, process, *addr_ptr, offset.u.HighPart, offset.u.LowPart, size, protect );
|
2004-09-22 06:03:10 +02:00
|
|
|
|
2002-09-17 20:54:42 +02:00
|
|
|
/* Check parameters */
|
|
|
|
|
2006-07-13 20:36:41 +02:00
|
|
|
if ((offset.u.LowPart & mask) || (*addr_ptr && ((UINT_PTR)*addr_ptr & mask)))
|
2002-09-17 20:54:42 +02:00
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
|
2008-12-30 15:22:45 +01:00
|
|
|
switch(protect)
|
|
|
|
{
|
|
|
|
case PAGE_NOACCESS:
|
|
|
|
access = SECTION_QUERY;
|
|
|
|
break;
|
|
|
|
case PAGE_READWRITE:
|
|
|
|
case PAGE_EXECUTE_READWRITE:
|
|
|
|
access = SECTION_QUERY | SECTION_MAP_WRITE;
|
|
|
|
break;
|
|
|
|
case PAGE_READONLY:
|
|
|
|
case PAGE_WRITECOPY:
|
|
|
|
case PAGE_EXECUTE:
|
|
|
|
case PAGE_EXECUTE_READ:
|
|
|
|
case PAGE_EXECUTE_WRITECOPY:
|
|
|
|
access = SECTION_QUERY | SECTION_MAP_READ;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
2007-01-18 15:17:51 +01:00
|
|
|
if (process != NtCurrentProcess())
|
|
|
|
{
|
|
|
|
apc_call_t call;
|
|
|
|
apc_result_t result;
|
|
|
|
|
2007-12-11 15:07:42 +01:00
|
|
|
memset( &call, 0, sizeof(call) );
|
|
|
|
|
2007-01-18 15:17:51 +01:00
|
|
|
call.map_view.type = APC_MAP_VIEW;
|
2008-12-08 16:05:17 +01:00
|
|
|
call.map_view.handle = wine_server_obj_handle( handle );
|
2008-12-30 15:05:38 +01:00
|
|
|
call.map_view.addr = wine_server_client_ptr( *addr_ptr );
|
2007-01-18 15:17:51 +01:00
|
|
|
call.map_view.size = *size_ptr;
|
2007-10-10 14:06:25 +02:00
|
|
|
call.map_view.offset = offset.QuadPart;
|
2007-01-18 15:17:51 +01:00
|
|
|
call.map_view.zero_bits = zero_bits;
|
|
|
|
call.map_view.alloc_type = alloc_type;
|
|
|
|
call.map_view.prot = protect;
|
|
|
|
res = NTDLL_queue_process_apc( process, &call, &result );
|
|
|
|
if (res != STATUS_SUCCESS) return res;
|
|
|
|
|
|
|
|
if (result.map_view.status == STATUS_SUCCESS)
|
|
|
|
{
|
2008-12-30 15:05:38 +01:00
|
|
|
*addr_ptr = wine_server_get_ptr( result.map_view.addr );
|
2007-01-18 15:17:51 +01:00
|
|
|
*size_ptr = result.map_view.size;
|
|
|
|
}
|
|
|
|
return result.map_view.status;
|
|
|
|
}
|
|
|
|
|
2002-09-17 20:54:42 +02:00
|
|
|
SERVER_START_REQ( get_mapping_info )
|
|
|
|
{
|
2008-12-08 16:05:17 +01:00
|
|
|
req->handle = wine_server_obj_handle( handle );
|
2008-11-05 20:32:32 +01:00
|
|
|
req->access = access;
|
2002-09-17 20:54:42 +02:00
|
|
|
res = wine_server_call( req );
|
2008-11-05 20:32:32 +01:00
|
|
|
map_vprot = reply->protect;
|
2008-12-30 14:18:21 +01:00
|
|
|
base = wine_server_get_ptr( reply->base );
|
2007-10-10 14:06:25 +02:00
|
|
|
full_size = reply->size;
|
2002-09-17 20:54:42 +02:00
|
|
|
header_size = reply->header_size;
|
2008-12-08 16:05:17 +01:00
|
|
|
dup_mapping = wine_server_ptr_handle( reply->mapping );
|
|
|
|
shared_file = wine_server_ptr_handle( reply->shared_file );
|
2008-12-30 14:18:21 +01:00
|
|
|
if ((ULONG_PTR)base != reply->base) base = NULL;
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
|
|
|
SERVER_END_REQ;
|
2003-12-02 00:01:12 +01:00
|
|
|
if (res) return res;
|
2002-09-17 20:54:42 +02:00
|
|
|
|
2007-10-10 14:06:25 +02:00
|
|
|
size = full_size;
|
2008-12-17 19:25:49 +01:00
|
|
|
if (size != full_size)
|
|
|
|
{
|
|
|
|
WARN( "Sizes larger than 4Gb (%s) not supported on this platform\n",
|
|
|
|
wine_dbgstr_longlong(full_size) );
|
|
|
|
if (dup_mapping) NtClose( dup_mapping );
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
2007-01-12 15:01:44 +01:00
|
|
|
|
2007-01-12 14:55:31 +01:00
|
|
|
if ((res = server_get_unix_fd( handle, 0, &unix_handle, &needs_close, NULL, NULL ))) goto done;
|
2004-04-07 01:13:47 +02:00
|
|
|
|
2008-11-05 20:32:32 +01:00
|
|
|
if (map_vprot & VPROT_IMAGE)
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
|
|
|
if (shared_file)
|
|
|
|
{
|
2006-11-03 11:38:44 +01:00
|
|
|
int shared_fd, shared_needs_close;
|
2003-12-02 00:01:12 +01:00
|
|
|
|
2006-11-03 11:38:44 +01:00
|
|
|
if ((res = server_get_unix_fd( shared_file, FILE_READ_DATA|FILE_WRITE_DATA,
|
2006-11-20 14:15:06 +01:00
|
|
|
&shared_fd, &shared_needs_close, NULL, NULL ))) goto done;
|
2007-01-12 15:01:44 +01:00
|
|
|
res = map_image( handle, unix_handle, base, size, mask, header_size,
|
2007-01-12 14:55:31 +01:00
|
|
|
shared_fd, dup_mapping, addr_ptr );
|
2006-11-03 11:38:44 +01:00
|
|
|
if (shared_needs_close) close( shared_fd );
|
2003-12-02 00:01:12 +01:00
|
|
|
NtClose( shared_file );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-01-12 15:01:44 +01:00
|
|
|
res = map_image( handle, unix_handle, base, size, mask, header_size,
|
2007-01-12 14:55:31 +01:00
|
|
|
-1, dup_mapping, addr_ptr );
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
2006-11-03 11:38:44 +01:00
|
|
|
if (needs_close) close( unix_handle );
|
2007-01-12 15:01:44 +01:00
|
|
|
if (!res) *size_ptr = size;
|
2002-09-17 20:54:42 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2007-01-12 15:01:44 +01:00
|
|
|
if ((offset.QuadPart >= size) || (*size_ptr > size - offset.QuadPart))
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
|
|
|
res = STATUS_INVALID_PARAMETER;
|
2004-05-21 22:58:44 +02:00
|
|
|
goto done;
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
2005-04-19 12:31:28 +02:00
|
|
|
if (*size_ptr) size = ROUND_SIZE( offset.u.LowPart, *size_ptr );
|
2007-01-12 15:01:44 +01:00
|
|
|
else size = size - offset.QuadPart;
|
2002-09-17 20:54:42 +02:00
|
|
|
|
|
|
|
/* Reserve a properly aligned area */
|
|
|
|
|
2007-01-12 20:15:52 +01:00
|
|
|
server_enter_uninterrupted_section( &csVirtual, &sigset );
|
2004-05-21 22:58:44 +02:00
|
|
|
|
2008-12-11 14:05:42 +01:00
|
|
|
get_vprot_flags( protect, &vprot );
|
|
|
|
vprot |= (map_vprot & VPROT_COMMITTED);
|
2008-11-05 20:32:32 +01:00
|
|
|
res = map_view( &view, *addr_ptr, size, mask, FALSE, vprot );
|
2004-05-21 22:58:44 +02:00
|
|
|
if (res)
|
|
|
|
{
|
2007-01-12 20:15:52 +01:00
|
|
|
server_leave_uninterrupted_section( &csVirtual, &sigset );
|
2004-05-21 22:58:44 +02:00
|
|
|
goto done;
|
|
|
|
}
|
2002-09-17 20:54:42 +02:00
|
|
|
|
|
|
|
/* Map the file */
|
|
|
|
|
2006-10-16 13:49:06 +02:00
|
|
|
TRACE("handle=%p size=%lx offset=%x%08x\n",
|
2005-04-19 12:31:28 +02:00
|
|
|
handle, size, offset.u.HighPart, offset.u.LowPart );
|
2002-09-17 20:54:42 +02:00
|
|
|
|
2008-11-05 20:32:32 +01:00
|
|
|
res = map_file_into_view( view, unix_handle, 0, size, offset.QuadPart, vprot, !dup_mapping );
|
2004-05-21 22:58:44 +02:00
|
|
|
if (res == STATUS_SUCCESS)
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2004-05-21 22:58:44 +02:00
|
|
|
*addr_ptr = view->base;
|
|
|
|
*size_ptr = size;
|
2007-01-12 14:55:31 +01:00
|
|
|
view->mapping = dup_mapping;
|
|
|
|
dup_mapping = 0; /* don't close it */
|
2004-05-21 22:58:44 +02:00
|
|
|
}
|
|
|
|
else
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2006-10-16 13:49:06 +02:00
|
|
|
ERR( "map_file_into_view %p %lx %x%08x failed\n",
|
2005-04-19 12:31:28 +02:00
|
|
|
view->base, size, offset.u.HighPart, offset.u.LowPart );
|
2004-05-25 03:29:24 +02:00
|
|
|
delete_view( view );
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
|
|
|
|
2007-01-12 20:15:52 +01:00
|
|
|
server_leave_uninterrupted_section( &csVirtual, &sigset );
|
2004-05-21 22:58:44 +02:00
|
|
|
|
|
|
|
done:
|
2007-01-12 14:55:31 +01:00
|
|
|
if (dup_mapping) NtClose( dup_mapping );
|
2006-11-03 11:38:44 +01:00
|
|
|
if (needs_close) close( unix_handle );
|
2002-09-17 20:54:42 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* NtUnmapViewOfSection (NTDLL.@)
|
|
|
|
* ZwUnmapViewOfSection (NTDLL.@)
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI NtUnmapViewOfSection( HANDLE process, PVOID addr )
|
|
|
|
{
|
|
|
|
FILE_VIEW *view;
|
2004-04-06 22:16:51 +02:00
|
|
|
NTSTATUS status = STATUS_INVALID_PARAMETER;
|
2007-01-12 20:15:52 +01:00
|
|
|
sigset_t sigset;
|
2002-09-17 20:54:42 +02:00
|
|
|
void *base = ROUND_ADDR( addr, page_mask );
|
|
|
|
|
2007-01-18 15:17:51 +01:00
|
|
|
if (process != NtCurrentProcess())
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2007-01-18 15:17:51 +01:00
|
|
|
apc_call_t call;
|
|
|
|
apc_result_t result;
|
|
|
|
|
2007-12-11 15:07:42 +01:00
|
|
|
memset( &call, 0, sizeof(call) );
|
|
|
|
|
2007-01-18 15:17:51 +01:00
|
|
|
call.unmap_view.type = APC_UNMAP_VIEW;
|
2008-12-30 15:05:38 +01:00
|
|
|
call.unmap_view.addr = wine_server_client_ptr( addr );
|
2007-01-18 15:17:51 +01:00
|
|
|
status = NTDLL_queue_process_apc( process, &call, &result );
|
|
|
|
if (status == STATUS_SUCCESS) status = result.unmap_view.status;
|
|
|
|
return status;
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
2007-01-18 15:17:51 +01:00
|
|
|
|
2007-01-12 20:15:52 +01:00
|
|
|
server_enter_uninterrupted_section( &csVirtual, &sigset );
|
2008-11-25 11:58:50 +01:00
|
|
|
if ((view = VIRTUAL_FindView( base, 0 )) && (base == view->base))
|
2004-04-06 22:16:51 +02:00
|
|
|
{
|
2004-05-25 03:29:24 +02:00
|
|
|
delete_view( view );
|
2004-04-06 22:16:51 +02:00
|
|
|
status = STATUS_SUCCESS;
|
|
|
|
}
|
2007-01-12 20:15:52 +01:00
|
|
|
server_leave_uninterrupted_section( &csVirtual, &sigset );
|
2004-04-06 22:16:51 +02:00
|
|
|
return status;
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* NtFlushVirtualMemory (NTDLL.@)
|
|
|
|
* ZwFlushVirtualMemory (NTDLL.@)
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI NtFlushVirtualMemory( HANDLE process, LPCVOID *addr_ptr,
|
2005-07-15 12:01:30 +02:00
|
|
|
SIZE_T *size_ptr, ULONG unknown )
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
|
|
|
FILE_VIEW *view;
|
2004-04-06 22:16:51 +02:00
|
|
|
NTSTATUS status = STATUS_SUCCESS;
|
2007-01-12 20:15:52 +01:00
|
|
|
sigset_t sigset;
|
2002-09-17 20:54:42 +02:00
|
|
|
void *addr = ROUND_ADDR( *addr_ptr, page_mask );
|
|
|
|
|
2007-01-15 22:31:07 +01:00
|
|
|
if (process != NtCurrentProcess())
|
2002-09-17 20:54:42 +02:00
|
|
|
{
|
2007-01-15 22:31:07 +01:00
|
|
|
apc_call_t call;
|
|
|
|
apc_result_t result;
|
|
|
|
|
2007-12-11 15:07:42 +01:00
|
|
|
memset( &call, 0, sizeof(call) );
|
|
|
|
|
2007-01-15 22:31:07 +01:00
|
|
|
call.virtual_flush.type = APC_VIRTUAL_FLUSH;
|
2008-12-30 15:05:38 +01:00
|
|
|
call.virtual_flush.addr = wine_server_client_ptr( addr );
|
2007-01-15 22:31:07 +01:00
|
|
|
call.virtual_flush.size = *size_ptr;
|
|
|
|
status = NTDLL_queue_process_apc( process, &call, &result );
|
|
|
|
if (status != STATUS_SUCCESS) return status;
|
|
|
|
|
|
|
|
if (result.virtual_flush.status == STATUS_SUCCESS)
|
|
|
|
{
|
2008-12-30 15:05:38 +01:00
|
|
|
*addr_ptr = wine_server_get_ptr( result.virtual_flush.addr );
|
2007-01-15 22:31:07 +01:00
|
|
|
*size_ptr = result.virtual_flush.size;
|
|
|
|
}
|
|
|
|
return result.virtual_flush.status;
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
2007-01-15 22:31:07 +01:00
|
|
|
|
2007-01-12 20:15:52 +01:00
|
|
|
server_enter_uninterrupted_section( &csVirtual, &sigset );
|
2008-11-25 11:58:50 +01:00
|
|
|
if (!(view = VIRTUAL_FindView( addr, *size_ptr ))) status = STATUS_INVALID_PARAMETER;
|
2004-04-06 22:16:51 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!*size_ptr) *size_ptr = view->size;
|
|
|
|
*addr_ptr = addr;
|
|
|
|
if (msync( addr, *size_ptr, MS_SYNC )) status = STATUS_NOT_MAPPED_DATA;
|
|
|
|
}
|
2007-01-12 20:15:52 +01:00
|
|
|
server_leave_uninterrupted_section( &csVirtual, &sigset );
|
2004-04-06 22:16:51 +02:00
|
|
|
return status;
|
2002-09-17 20:54:42 +02:00
|
|
|
}
|
2003-07-08 23:18:45 +02:00
|
|
|
|
|
|
|
|
2008-11-14 10:49:02 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* NtGetWriteWatch (NTDLL.@)
|
|
|
|
* ZwGetWriteWatch (NTDLL.@)
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI NtGetWriteWatch( HANDLE process, ULONG flags, PVOID base, SIZE_T size, PVOID *addresses,
|
|
|
|
ULONG_PTR *count, ULONG *granularity )
|
|
|
|
{
|
2008-11-25 12:07:35 +01:00
|
|
|
struct file_view *view;
|
|
|
|
NTSTATUS status = STATUS_SUCCESS;
|
|
|
|
sigset_t sigset;
|
|
|
|
|
|
|
|
size = ROUND_SIZE( base, size );
|
|
|
|
base = ROUND_ADDR( base, page_mask );
|
|
|
|
|
|
|
|
if (!count || !granularity) return STATUS_ACCESS_VIOLATION;
|
|
|
|
if (!*count || !size) return STATUS_INVALID_PARAMETER;
|
|
|
|
if (flags & ~WRITE_WATCH_FLAG_RESET) return STATUS_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
if (!addresses) return STATUS_ACCESS_VIOLATION;
|
|
|
|
|
|
|
|
TRACE( "%p %x %p-%p %p %lu\n", process, flags, base, (char *)base + size,
|
|
|
|
addresses, count ? *count : 0 );
|
|
|
|
|
|
|
|
server_enter_uninterrupted_section( &csVirtual, &sigset );
|
|
|
|
|
|
|
|
if ((view = VIRTUAL_FindView( base, size )) && (view->protect & VPROT_WRITEWATCH))
|
|
|
|
{
|
|
|
|
ULONG_PTR pos = 0;
|
|
|
|
char *addr = base;
|
|
|
|
char *end = addr + size;
|
|
|
|
|
|
|
|
while (pos < *count && addr < end)
|
|
|
|
{
|
|
|
|
BYTE prot = view->prot[(addr - (char *)view->base) >> page_shift];
|
|
|
|
if (!(prot & VPROT_WRITEWATCH)) addresses[pos++] = addr;
|
|
|
|
addr += page_size;
|
|
|
|
}
|
|
|
|
if (flags & WRITE_WATCH_FLAG_RESET) reset_write_watches( view, base, addr - (char *)base );
|
|
|
|
*count = pos;
|
|
|
|
*granularity = page_size;
|
|
|
|
}
|
|
|
|
else status = STATUS_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
server_leave_uninterrupted_section( &csVirtual, &sigset );
|
|
|
|
return status;
|
2008-11-14 10:49:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* NtResetWriteWatch (NTDLL.@)
|
|
|
|
* ZwResetWriteWatch (NTDLL.@)
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI NtResetWriteWatch( HANDLE process, PVOID base, SIZE_T size )
|
|
|
|
{
|
2008-11-25 12:07:35 +01:00
|
|
|
struct file_view *view;
|
|
|
|
NTSTATUS status = STATUS_SUCCESS;
|
|
|
|
sigset_t sigset;
|
|
|
|
|
|
|
|
size = ROUND_SIZE( base, size );
|
|
|
|
base = ROUND_ADDR( base, page_mask );
|
|
|
|
|
|
|
|
TRACE( "%p %p-%p\n", process, base, (char *)base + size );
|
|
|
|
|
|
|
|
if (!size) return STATUS_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
server_enter_uninterrupted_section( &csVirtual, &sigset );
|
|
|
|
|
|
|
|
if ((view = VIRTUAL_FindView( base, size )) && (view->protect & VPROT_WRITEWATCH))
|
|
|
|
reset_write_watches( view, base, size );
|
|
|
|
else
|
|
|
|
status = STATUS_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
server_leave_uninterrupted_section( &csVirtual, &sigset );
|
|
|
|
return status;
|
2008-11-14 10:49:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-08 23:18:45 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* NtReadVirtualMemory (NTDLL.@)
|
|
|
|
* ZwReadVirtualMemory (NTDLL.@)
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI NtReadVirtualMemory( HANDLE process, const void *addr, void *buffer,
|
|
|
|
SIZE_T size, SIZE_T *bytes_read )
|
|
|
|
{
|
|
|
|
NTSTATUS status;
|
|
|
|
|
2009-01-14 20:36:27 +01:00
|
|
|
if (virtual_check_buffer_for_write( buffer, size ))
|
2003-07-08 23:18:45 +02:00
|
|
|
{
|
2009-01-14 20:36:27 +01:00
|
|
|
SERVER_START_REQ( read_process_memory )
|
|
|
|
{
|
|
|
|
req->handle = wine_server_obj_handle( process );
|
|
|
|
req->addr = wine_server_client_ptr( addr );
|
|
|
|
wine_server_set_reply( req, buffer, size );
|
|
|
|
if ((status = wine_server_call( req ))) size = 0;
|
|
|
|
}
|
|
|
|
SERVER_END_REQ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = STATUS_ACCESS_VIOLATION;
|
|
|
|
size = 0;
|
2003-07-08 23:18:45 +02:00
|
|
|
}
|
|
|
|
if (bytes_read) *bytes_read = size;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* NtWriteVirtualMemory (NTDLL.@)
|
|
|
|
* ZwWriteVirtualMemory (NTDLL.@)
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI NtWriteVirtualMemory( HANDLE process, void *addr, const void *buffer,
|
|
|
|
SIZE_T size, SIZE_T *bytes_written )
|
|
|
|
{
|
|
|
|
NTSTATUS status;
|
|
|
|
|
2009-01-14 20:36:27 +01:00
|
|
|
if (virtual_check_buffer_for_read( buffer, size ))
|
2003-07-08 23:18:45 +02:00
|
|
|
{
|
2009-01-14 20:36:27 +01:00
|
|
|
SERVER_START_REQ( write_process_memory )
|
|
|
|
{
|
|
|
|
req->handle = wine_server_obj_handle( process );
|
|
|
|
req->addr = wine_server_client_ptr( addr );
|
|
|
|
wine_server_add_data( req, buffer, size );
|
|
|
|
if ((status = wine_server_call( req ))) size = 0;
|
|
|
|
}
|
|
|
|
SERVER_END_REQ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = STATUS_PARTIAL_COPY;
|
|
|
|
size = 0;
|
2003-07-08 23:18:45 +02:00
|
|
|
}
|
|
|
|
if (bytes_written) *bytes_written = size;
|
|
|
|
return status;
|
|
|
|
}
|
2007-09-26 20:50:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* NtAreMappedFilesTheSame (NTDLL.@)
|
|
|
|
* ZwAreMappedFilesTheSame (NTDLL.@)
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI NtAreMappedFilesTheSame(PVOID addr1, PVOID addr2)
|
|
|
|
{
|
|
|
|
TRACE("%p %p\n", addr1, addr2);
|
|
|
|
|
|
|
|
return STATUS_NOT_SAME_DEVICE;
|
|
|
|
}
|