ntdll: Use MAP_SHARED also for read-only mappings.
[wine.git] / dlls / ntdll / virtual.c
blob426af011532a37d72c6562c295384cf2dd9173f5
1 /*
2 * Win32 virtual memory functions
4 * Copyright 1997, 2002 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <stdarg.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <sys/types.h>
35 #ifdef HAVE_SYS_SOCKET_H
36 # include <sys/socket.h>
37 #endif
38 #ifdef HAVE_SYS_STAT_H
39 # include <sys/stat.h>
40 #endif
41 #ifdef HAVE_SYS_MMAN_H
42 # include <sys/mman.h>
43 #endif
44 #ifdef HAVE_SYS_SYSINFO_H
45 # include <sys/sysinfo.h>
46 #endif
47 #ifdef HAVE_VALGRIND_VALGRIND_H
48 # include <valgrind/valgrind.h>
49 #endif
51 #include "ntstatus.h"
52 #define WIN32_NO_STATUS
53 #define NONAMELESSUNION
54 #include "windef.h"
55 #include "winternl.h"
56 #include "wine/library.h"
57 #include "wine/server.h"
58 #include "wine/exception.h"
59 #include "wine/rbtree.h"
60 #include "wine/debug.h"
61 #include "ntdll_misc.h"
63 WINE_DEFAULT_DEBUG_CHANNEL(virtual);
64 WINE_DECLARE_DEBUG_CHANNEL(module);
66 #ifndef MAP_NORESERVE
67 #define MAP_NORESERVE 0
68 #endif
70 /* File view */
71 struct file_view
73 struct wine_rb_entry entry; /* entry in global view tree */
74 void *base; /* base address */
75 size_t size; /* size in bytes */
76 unsigned int protect; /* protection for all pages at allocation time and SEC_* flags */
79 /* per-page protection flags */
80 #define VPROT_READ 0x01
81 #define VPROT_WRITE 0x02
82 #define VPROT_EXEC 0x04
83 #define VPROT_WRITECOPY 0x08
84 #define VPROT_GUARD 0x10
85 #define VPROT_COMMITTED 0x20
86 #define VPROT_WRITEWATCH 0x40
87 /* per-mapping protection flags */
88 #define VPROT_SYSTEM 0x0200 /* system view (underlying mmap not under our control) */
90 /* Conversion from VPROT_* to Win32 flags */
91 static const BYTE VIRTUAL_Win32Flags[16] =
93 PAGE_NOACCESS, /* 0 */
94 PAGE_READONLY, /* READ */
95 PAGE_READWRITE, /* WRITE */
96 PAGE_READWRITE, /* READ | WRITE */
97 PAGE_EXECUTE, /* EXEC */
98 PAGE_EXECUTE_READ, /* READ | EXEC */
99 PAGE_EXECUTE_READWRITE, /* WRITE | EXEC */
100 PAGE_EXECUTE_READWRITE, /* READ | WRITE | EXEC */
101 PAGE_WRITECOPY, /* WRITECOPY */
102 PAGE_WRITECOPY, /* READ | WRITECOPY */
103 PAGE_WRITECOPY, /* WRITE | WRITECOPY */
104 PAGE_WRITECOPY, /* READ | WRITE | WRITECOPY */
105 PAGE_EXECUTE_WRITECOPY, /* EXEC | WRITECOPY */
106 PAGE_EXECUTE_WRITECOPY, /* READ | EXEC | WRITECOPY */
107 PAGE_EXECUTE_WRITECOPY, /* WRITE | EXEC | WRITECOPY */
108 PAGE_EXECUTE_WRITECOPY /* READ | WRITE | EXEC | WRITECOPY */
111 static struct wine_rb_tree views_tree;
113 static RTL_CRITICAL_SECTION csVirtual;
114 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
116 0, 0, &csVirtual,
117 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
118 0, 0, { (DWORD_PTR)(__FILE__ ": csVirtual") }
120 static RTL_CRITICAL_SECTION csVirtual = { &critsect_debug, -1, 0, 0, 0, 0 };
122 #ifdef __i386__
123 static const UINT page_shift = 12;
124 static const UINT_PTR page_mask = 0xfff;
125 /* Note: these are Windows limits, you cannot change them. */
126 static void *address_space_limit = (void *)0xc0000000; /* top of the total available address space */
127 static void *user_space_limit = (void *)0x7fff0000; /* top of the user address space */
128 static void *working_set_limit = (void *)0x7fff0000; /* top of the current working set */
129 static void *address_space_start = (void *)0x110000; /* keep DOS area clear */
130 #elif defined(__x86_64__)
131 static const UINT page_shift = 12;
132 static const UINT_PTR page_mask = 0xfff;
133 static void *address_space_limit = (void *)0x7fffffff0000;
134 static void *user_space_limit = (void *)0x7fffffff0000;
135 static void *working_set_limit = (void *)0x7fffffff0000;
136 static void *address_space_start = (void *)0x10000;
137 #else
138 UINT_PTR page_size = 0;
139 static UINT page_shift;
140 static UINT_PTR page_mask;
141 static void *address_space_limit;
142 static void *user_space_limit;
143 static void *working_set_limit;
144 static void *address_space_start = (void *)0x10000;
145 #endif /* __i386__ */
146 static const BOOL is_win64 = (sizeof(void *) > sizeof(int));
148 #define ROUND_ADDR(addr,mask) \
149 ((void *)((UINT_PTR)(addr) & ~(UINT_PTR)(mask)))
151 #define ROUND_SIZE(addr,size) \
152 (((SIZE_T)(size) + ((UINT_PTR)(addr) & page_mask) + page_mask) & ~page_mask)
154 #define VIRTUAL_DEBUG_DUMP_VIEW(view) \
155 do { if (TRACE_ON(virtual)) VIRTUAL_DumpView(view); } while (0)
157 #ifdef _WIN64 /* on 64-bit the page protection bytes use a 2-level table */
158 static const size_t pages_vprot_shift = 20;
159 static const size_t pages_vprot_mask = (1 << 20) - 1;
160 static size_t pages_vprot_size;
161 static BYTE **pages_vprot;
162 #else /* on 32-bit we use a simple array with one byte per page */
163 static BYTE *pages_vprot;
164 #endif
166 static struct file_view *view_block_start, *view_block_end, *next_free_view;
167 static const size_t view_block_size = 0x100000;
168 static void *preload_reserve_start;
169 static void *preload_reserve_end;
170 static BOOL use_locks;
171 static BOOL force_exec_prot; /* whether to force PROT_EXEC on all PROT_READ mmaps */
173 static inline int is_view_valloc( const struct file_view *view )
175 return !(view->protect & (SEC_FILE | SEC_RESERVE | SEC_COMMIT));
178 /***********************************************************************
179 * get_page_vprot
181 * Return the page protection byte.
183 static BYTE get_page_vprot( const void *addr )
185 size_t idx = (size_t)addr >> page_shift;
187 #ifdef _WIN64
188 if ((idx >> pages_vprot_shift) >= pages_vprot_size) return 0;
189 if (!pages_vprot[idx >> pages_vprot_shift]) return 0;
190 return pages_vprot[idx >> pages_vprot_shift][idx & pages_vprot_mask];
191 #else
192 return pages_vprot[idx];
193 #endif
197 /***********************************************************************
198 * set_page_vprot
200 * Set a range of page protection bytes.
202 static void set_page_vprot( const void *addr, size_t size, BYTE vprot )
204 size_t idx = (size_t)addr >> page_shift;
205 size_t end = ((size_t)addr + size + page_mask) >> page_shift;
207 #ifdef _WIN64
208 while (idx >> pages_vprot_shift != end >> pages_vprot_shift)
210 size_t dir_size = pages_vprot_mask + 1 - (idx & pages_vprot_mask);
211 memset( pages_vprot[idx >> pages_vprot_shift] + (idx & pages_vprot_mask), vprot, dir_size );
212 idx += dir_size;
214 memset( pages_vprot[idx >> pages_vprot_shift] + (idx & pages_vprot_mask), vprot, end - idx );
215 #else
216 memset( pages_vprot + idx, vprot, end - idx );
217 #endif
221 /***********************************************************************
222 * set_page_vprot_bits
224 * Set or clear bits in a range of page protection bytes.
226 static void set_page_vprot_bits( const void *addr, size_t size, BYTE set, BYTE clear )
228 size_t idx = (size_t)addr >> page_shift;
229 size_t end = ((size_t)addr + size + page_mask) >> page_shift;
231 #ifdef _WIN64
232 for ( ; idx < end; idx++)
234 BYTE *ptr = pages_vprot[idx >> pages_vprot_shift] + (idx & pages_vprot_mask);
235 *ptr = (*ptr & ~clear) | set;
237 #else
238 for ( ; idx < end; idx++) pages_vprot[idx] = (pages_vprot[idx] & ~clear) | set;
239 #endif
243 /***********************************************************************
244 * alloc_pages_vprot
246 * Allocate the page protection bytes for a given range.
248 static BOOL alloc_pages_vprot( const void *addr, size_t size )
250 #ifdef _WIN64
251 size_t idx = (size_t)addr >> page_shift;
252 size_t end = ((size_t)addr + size + page_mask) >> page_shift;
253 size_t i;
254 void *ptr;
256 assert( end <= pages_vprot_size << pages_vprot_shift );
257 for (i = idx >> pages_vprot_shift; i < (end + pages_vprot_mask) >> pages_vprot_shift; i++)
259 if (pages_vprot[i]) continue;
260 if ((ptr = wine_anon_mmap( NULL, pages_vprot_mask + 1, PROT_READ | PROT_WRITE, 0 )) == (void *)-1)
261 return FALSE;
262 pages_vprot[i] = ptr;
264 #endif
265 return TRUE;
269 /***********************************************************************
270 * compare_view
272 * View comparison function used for the rb tree.
274 static int compare_view( const void *addr, const struct wine_rb_entry *entry )
276 struct file_view *view = WINE_RB_ENTRY_VALUE( entry, struct file_view, entry );
278 if (addr < view->base) return -1;
279 if (addr > view->base) return 1;
280 return 0;
284 /***********************************************************************
285 * VIRTUAL_GetProtStr
287 static const char *VIRTUAL_GetProtStr( BYTE prot )
289 static char buffer[6];
290 buffer[0] = (prot & VPROT_COMMITTED) ? 'c' : '-';
291 buffer[1] = (prot & VPROT_GUARD) ? 'g' : ((prot & VPROT_WRITEWATCH) ? 'H' : '-');
292 buffer[2] = (prot & VPROT_READ) ? 'r' : '-';
293 buffer[3] = (prot & VPROT_WRITECOPY) ? 'W' : ((prot & VPROT_WRITE) ? 'w' : '-');
294 buffer[4] = (prot & VPROT_EXEC) ? 'x' : '-';
295 buffer[5] = 0;
296 return buffer;
300 /***********************************************************************
301 * VIRTUAL_GetUnixProt
303 * Convert page protections to protection for mmap/mprotect.
305 static int VIRTUAL_GetUnixProt( BYTE vprot )
307 int prot = 0;
308 if ((vprot & VPROT_COMMITTED) && !(vprot & VPROT_GUARD))
310 if (vprot & VPROT_READ) prot |= PROT_READ;
311 if (vprot & VPROT_WRITE) prot |= PROT_WRITE | PROT_READ;
312 if (vprot & VPROT_WRITECOPY) prot |= PROT_WRITE | PROT_READ;
313 if (vprot & VPROT_EXEC) prot |= PROT_EXEC | PROT_READ;
314 if (vprot & VPROT_WRITEWATCH) prot &= ~PROT_WRITE;
316 if (!prot) prot = PROT_NONE;
317 return prot;
321 /***********************************************************************
322 * VIRTUAL_DumpView
324 static void VIRTUAL_DumpView( struct file_view *view )
326 UINT i, count;
327 char *addr = view->base;
328 BYTE prot = get_page_vprot( addr );
330 TRACE( "View: %p - %p", addr, addr + view->size - 1 );
331 if (view->protect & VPROT_SYSTEM)
332 TRACE( " (builtin image)\n" );
333 else if (view->protect & SEC_IMAGE)
334 TRACE( " (image)\n" );
335 else if (view->protect & SEC_FILE)
336 TRACE( " (file)\n" );
337 else if (view->protect & (SEC_RESERVE | SEC_COMMIT))
338 TRACE( " (anonymous)\n" );
339 else
340 TRACE( " (valloc)\n");
342 for (count = i = 1; i < view->size >> page_shift; i++, count++)
344 BYTE next = get_page_vprot( addr + (count << page_shift) );
345 if (next == prot) continue;
346 TRACE( " %p - %p %s\n",
347 addr, addr + (count << page_shift) - 1, VIRTUAL_GetProtStr(prot) );
348 addr += (count << page_shift);
349 prot = next;
350 count = 0;
352 if (count)
353 TRACE( " %p - %p %s\n",
354 addr, addr + (count << page_shift) - 1, VIRTUAL_GetProtStr(prot) );
358 /***********************************************************************
359 * VIRTUAL_Dump
361 #ifdef WINE_VM_DEBUG
362 static void VIRTUAL_Dump(void)
364 sigset_t sigset;
365 struct file_view *view;
367 TRACE( "Dump of all virtual memory views:\n" );
368 server_enter_uninterrupted_section( &csVirtual, &sigset );
369 WINE_RB_FOR_EACH_ENTRY( view, &views_tree, struct file_view, entry )
371 VIRTUAL_DumpView( view );
373 server_leave_uninterrupted_section( &csVirtual, &sigset );
375 #endif
378 /***********************************************************************
379 * VIRTUAL_FindView
381 * Find the view containing a given address. The csVirtual section must be held by caller.
383 * PARAMS
384 * addr [I] Address
386 * RETURNS
387 * View: Success
388 * NULL: Failure
390 static struct file_view *VIRTUAL_FindView( const void *addr, size_t size )
392 struct wine_rb_entry *ptr = views_tree.root;
394 if ((const char *)addr + size < (const char *)addr) return NULL; /* overflow */
396 while (ptr)
398 struct file_view *view = WINE_RB_ENTRY_VALUE( ptr, struct file_view, entry );
400 if (view->base > addr) ptr = ptr->left;
401 else if ((const char *)view->base + view->size <= (const char *)addr) ptr = ptr->right;
402 else if ((const char *)view->base + view->size < (const char *)addr + size) break; /* size too large */
403 else return view;
405 return NULL;
409 /***********************************************************************
410 * get_mask
412 static inline UINT_PTR get_mask( ULONG zero_bits )
414 if (!zero_bits) return 0xffff; /* allocations are aligned to 64K by default */
415 if (zero_bits < page_shift) zero_bits = page_shift;
416 if (zero_bits > 21) return 0;
417 return (1 << zero_bits) - 1;
421 /***********************************************************************
422 * is_write_watch_range
424 static inline BOOL is_write_watch_range( const void *addr, size_t size )
426 struct file_view *view = VIRTUAL_FindView( addr, size );
427 return view && (view->protect & VPROT_WRITEWATCH);
431 /***********************************************************************
432 * find_view_range
434 * Find the first view overlapping at least part of the specified range.
435 * The csVirtual section must be held by caller.
437 static struct file_view *find_view_range( const void *addr, size_t size )
439 struct wine_rb_entry *ptr = views_tree.root;
441 while (ptr)
443 struct file_view *view = WINE_RB_ENTRY_VALUE( ptr, struct file_view, entry );
445 if ((const char *)view->base >= (const char *)addr + size) ptr = ptr->left;
446 else if ((const char *)view->base + view->size <= (const char *)addr) ptr = ptr->right;
447 else return view;
449 return NULL;
453 /***********************************************************************
454 * find_free_area
456 * Find a free area between views inside the specified range.
457 * The csVirtual section must be held by caller.
459 static void *find_free_area( void *base, void *end, size_t size, size_t mask, int top_down )
461 struct wine_rb_entry *first = NULL, *ptr = views_tree.root;
462 void *start;
464 /* find the first (resp. last) view inside the range */
465 while (ptr)
467 struct file_view *view = WINE_RB_ENTRY_VALUE( ptr, struct file_view, entry );
468 if ((char *)view->base + view->size >= (char *)end)
470 end = min( end, view->base );
471 ptr = ptr->left;
473 else if (view->base <= base)
475 base = max( (char *)base, (char *)view->base + view->size );
476 ptr = ptr->right;
478 else
480 first = ptr;
481 ptr = top_down ? ptr->right : ptr->left;
485 if (top_down)
487 start = ROUND_ADDR( (char *)end - size, mask );
488 if (start >= end || start < base) return NULL;
490 while (first)
492 struct file_view *view = WINE_RB_ENTRY_VALUE( first, struct file_view, entry );
494 if ((char *)view->base + view->size <= (char *)start) break;
495 start = ROUND_ADDR( (char *)view->base - size, mask );
496 /* stop if remaining space is not large enough */
497 if (!start || start >= end || start < base) return NULL;
498 first = wine_rb_prev( first );
501 else
503 start = ROUND_ADDR( (char *)base + mask, mask );
504 if (!start || start >= end || (char *)end - (char *)start < size) return NULL;
506 while (first)
508 struct file_view *view = WINE_RB_ENTRY_VALUE( first, struct file_view, entry );
510 if ((char *)view->base >= (char *)start + size) break;
511 start = ROUND_ADDR( (char *)view->base + view->size + mask, mask );
512 /* stop if remaining space is not large enough */
513 if (!start || start >= end || (char *)end - (char *)start < size) return NULL;
514 first = wine_rb_next( first );
517 return start;
521 /***********************************************************************
522 * add_reserved_area
524 * Add a reserved area to the list maintained by libwine.
525 * The csVirtual section must be held by caller.
527 static void add_reserved_area( void *addr, size_t size )
529 TRACE( "adding %p-%p\n", addr, (char *)addr + size );
531 if (addr < user_space_limit)
533 /* unmap the part of the area that is below the limit */
534 assert( (char *)addr + size > (char *)user_space_limit );
535 munmap( addr, (char *)user_space_limit - (char *)addr );
536 size -= (char *)user_space_limit - (char *)addr;
537 addr = user_space_limit;
539 /* blow away existing mappings */
540 wine_anon_mmap( addr, size, PROT_NONE, MAP_NORESERVE | MAP_FIXED );
541 wine_mmap_add_reserved_area( addr, size );
545 /***********************************************************************
546 * remove_reserved_area
548 * Remove a reserved area from the list maintained by libwine.
549 * The csVirtual section must be held by caller.
551 static void remove_reserved_area( void *addr, size_t size )
553 struct file_view *view;
555 TRACE( "removing %p-%p\n", addr, (char *)addr + size );
556 wine_mmap_remove_reserved_area( addr, size, 0 );
558 /* unmap areas not covered by an existing view */
559 WINE_RB_FOR_EACH_ENTRY( view, &views_tree, struct file_view, entry )
561 if ((char *)view->base >= (char *)addr + size) break;
562 if ((char *)view->base + view->size <= (char *)addr) continue;
563 if (view->base > addr) munmap( addr, (char *)view->base - (char *)addr );
564 if ((char *)view->base + view->size > (char *)addr + size) return;
565 size = (char *)addr + size - ((char *)view->base + view->size);
566 addr = (char *)view->base + view->size;
568 munmap( addr, size );
572 struct area_boundary
574 void *base;
575 size_t size;
576 void *boundary;
579 /***********************************************************************
580 * get_area_boundary_callback
582 * Get lowest boundary address between reserved area and non-reserved area
583 * in the specified region. If no boundaries are found, result is NULL.
584 * The csVirtual section must be held by caller.
586 static int get_area_boundary_callback( void *start, size_t size, void *arg )
588 struct area_boundary *area = arg;
589 void *end = (char *)start + size;
591 area->boundary = NULL;
592 if (area->base >= end) return 0;
593 if ((char *)start >= (char *)area->base + area->size) return 1;
594 if (area->base >= start)
596 if ((char *)area->base + area->size > (char *)end)
598 area->boundary = end;
599 return 1;
601 return 0;
603 area->boundary = start;
604 return 1;
608 /***********************************************************************
609 * is_beyond_limit
611 * Check if an address range goes beyond a given limit.
613 static inline BOOL is_beyond_limit( const void *addr, size_t size, const void *limit )
615 return (addr >= limit || (const char *)addr + size > (const char *)limit);
619 /***********************************************************************
620 * unmap_area
622 * Unmap an area, or simply replace it by an empty mapping if it is
623 * in a reserved area. The csVirtual section must be held by caller.
625 static inline void unmap_area( void *addr, size_t size )
627 switch (wine_mmap_is_in_reserved_area( addr, size ))
629 case -1: /* partially in a reserved area */
631 struct area_boundary area;
632 size_t lower_size;
633 area.base = addr;
634 area.size = size;
635 wine_mmap_enum_reserved_areas( get_area_boundary_callback, &area, 0 );
636 assert( area.boundary );
637 lower_size = (char *)area.boundary - (char *)addr;
638 unmap_area( addr, lower_size );
639 unmap_area( area.boundary, size - lower_size );
640 break;
642 case 1: /* in a reserved area */
643 wine_anon_mmap( addr, size, PROT_NONE, MAP_NORESERVE | MAP_FIXED );
644 break;
645 default:
646 case 0: /* not in a reserved area */
647 if (is_beyond_limit( addr, size, user_space_limit ))
648 add_reserved_area( addr, size );
649 else
650 munmap( addr, size );
651 break;
656 /***********************************************************************
657 * alloc_view
659 * Allocate a new view. The csVirtual section must be held by caller.
661 static struct file_view *alloc_view(void)
663 if (next_free_view)
665 struct file_view *ret = next_free_view;
666 next_free_view = *(struct file_view **)ret;
667 return ret;
669 if (view_block_start == view_block_end)
671 void *ptr = wine_anon_mmap( NULL, view_block_size, PROT_READ | PROT_WRITE, 0 );
672 if (ptr == (void *)-1) return NULL;
673 view_block_start = ptr;
674 view_block_end = view_block_start + view_block_size / sizeof(*view_block_start);
676 return view_block_start++;
680 /***********************************************************************
681 * delete_view
683 * Deletes a view. The csVirtual section must be held by caller.
685 static void delete_view( struct file_view *view ) /* [in] View */
687 if (!(view->protect & VPROT_SYSTEM)) unmap_area( view->base, view->size );
688 set_page_vprot( view->base, view->size, 0 );
689 wine_rb_remove( &views_tree, &view->entry );
690 *(struct file_view **)view = next_free_view;
691 next_free_view = view;
695 /***********************************************************************
696 * create_view
698 * Create a view. The csVirtual section must be held by caller.
700 static NTSTATUS create_view( struct file_view **view_ret, void *base, size_t size, unsigned int vprot )
702 struct file_view *view;
703 int unix_prot = VIRTUAL_GetUnixProt( vprot );
705 assert( !((UINT_PTR)base & page_mask) );
706 assert( !(size & page_mask) );
708 /* Check for overlapping views. This can happen if the previous view
709 * was a system view that got unmapped behind our back. In that case
710 * we recover by simply deleting it. */
712 while ((view = find_view_range( base, size )))
714 TRACE( "overlapping view %p-%p for %p-%p\n",
715 view->base, (char *)view->base + view->size, base, (char *)base + size );
716 assert( view->protect & VPROT_SYSTEM );
717 delete_view( view );
720 if (!alloc_pages_vprot( base, size )) return STATUS_NO_MEMORY;
722 /* Create the view structure */
724 if (!(view = alloc_view()))
726 FIXME( "out of memory for %p-%p\n", base, (char *)base + size );
727 return STATUS_NO_MEMORY;
730 view->base = base;
731 view->size = size;
732 view->protect = vprot;
733 set_page_vprot( base, size, vprot );
735 wine_rb_put( &views_tree, view->base, &view->entry );
737 *view_ret = view;
739 if (force_exec_prot && (unix_prot & PROT_READ) && !(unix_prot & PROT_EXEC))
741 TRACE( "forcing exec permission on %p-%p\n", base, (char *)base + size - 1 );
742 mprotect( base, size, unix_prot | PROT_EXEC );
744 return STATUS_SUCCESS;
748 /***********************************************************************
749 * VIRTUAL_GetWin32Prot
751 * Convert page protections to Win32 flags.
753 static DWORD VIRTUAL_GetWin32Prot( BYTE vprot, unsigned int map_prot )
755 DWORD ret = VIRTUAL_Win32Flags[vprot & 0x0f];
756 if (vprot & VPROT_GUARD) ret |= PAGE_GUARD;
757 if (map_prot & SEC_NOCACHE) ret |= PAGE_NOCACHE;
758 return ret;
762 /***********************************************************************
763 * get_vprot_flags
765 * Build page protections from Win32 flags.
767 * PARAMS
768 * protect [I] Win32 protection flags
770 * RETURNS
771 * Value of page protection flags
773 static NTSTATUS get_vprot_flags( DWORD protect, unsigned int *vprot, BOOL image )
775 switch(protect & 0xff)
777 case PAGE_READONLY:
778 *vprot = VPROT_READ;
779 break;
780 case PAGE_READWRITE:
781 if (image)
782 *vprot = VPROT_READ | VPROT_WRITECOPY;
783 else
784 *vprot = VPROT_READ | VPROT_WRITE;
785 break;
786 case PAGE_WRITECOPY:
787 *vprot = VPROT_READ | VPROT_WRITECOPY;
788 break;
789 case PAGE_EXECUTE:
790 *vprot = VPROT_EXEC;
791 break;
792 case PAGE_EXECUTE_READ:
793 *vprot = VPROT_EXEC | VPROT_READ;
794 break;
795 case PAGE_EXECUTE_READWRITE:
796 if (image)
797 *vprot = VPROT_EXEC | VPROT_READ | VPROT_WRITECOPY;
798 else
799 *vprot = VPROT_EXEC | VPROT_READ | VPROT_WRITE;
800 break;
801 case PAGE_EXECUTE_WRITECOPY:
802 *vprot = VPROT_EXEC | VPROT_READ | VPROT_WRITECOPY;
803 break;
804 case PAGE_NOACCESS:
805 *vprot = 0;
806 break;
807 default:
808 return STATUS_INVALID_PAGE_PROTECTION;
810 if (protect & PAGE_GUARD) *vprot |= VPROT_GUARD;
811 return STATUS_SUCCESS;
815 /***********************************************************************
816 * mprotect_exec
818 * Wrapper for mprotect, adds PROT_EXEC if forced by force_exec_prot
820 static inline int mprotect_exec( void *base, size_t size, int unix_prot )
822 if (force_exec_prot && (unix_prot & PROT_READ) && !(unix_prot & PROT_EXEC))
824 TRACE( "forcing exec permission on %p-%p\n", base, (char *)base + size - 1 );
825 if (!mprotect( base, size, unix_prot | PROT_EXEC )) return 0;
826 /* exec + write may legitimately fail, in that case fall back to write only */
827 if (!(unix_prot & PROT_WRITE)) return -1;
830 return mprotect( base, size, unix_prot );
834 /***********************************************************************
835 * mprotect_range
837 * Call mprotect on a page range, applying the protections from the per-page byte.
839 static void mprotect_range( void *base, size_t size, BYTE set, BYTE clear )
841 size_t i, count;
842 char *addr = ROUND_ADDR( base, page_mask );
843 int prot, next;
845 size = ROUND_SIZE( base, size );
846 prot = VIRTUAL_GetUnixProt( (get_page_vprot( addr ) & ~clear ) | set );
847 for (count = i = 1; i < size >> page_shift; i++, count++)
849 next = VIRTUAL_GetUnixProt( (get_page_vprot( addr + (count << page_shift) ) & ~clear) | set );
850 if (next == prot) continue;
851 mprotect_exec( addr, count << page_shift, prot );
852 addr += count << page_shift;
853 prot = next;
854 count = 0;
856 if (count) mprotect_exec( addr, count << page_shift, prot );
860 /***********************************************************************
861 * VIRTUAL_SetProt
863 * Change the protection of a range of pages.
865 * RETURNS
866 * TRUE: Success
867 * FALSE: Failure
869 static BOOL VIRTUAL_SetProt( struct file_view *view, /* [in] Pointer to view */
870 void *base, /* [in] Starting address */
871 size_t size, /* [in] Size in bytes */
872 BYTE vprot ) /* [in] Protections to use */
874 int unix_prot = VIRTUAL_GetUnixProt(vprot);
876 if (view->protect & VPROT_WRITEWATCH)
878 /* each page may need different protections depending on write watch flag */
879 set_page_vprot_bits( base, size, vprot & ~VPROT_WRITEWATCH, ~vprot & ~VPROT_WRITEWATCH );
880 mprotect_range( base, size, 0, 0 );
881 return TRUE;
884 /* if setting stack guard pages, store the permissions first, as the guard may be
885 * triggered at any point after mprotect and change the permissions again */
886 if ((vprot & VPROT_GUARD) &&
887 (base >= NtCurrentTeb()->DeallocationStack) &&
888 (base < NtCurrentTeb()->Tib.StackBase))
890 set_page_vprot( base, size, vprot );
891 mprotect( base, size, unix_prot );
892 return TRUE;
895 if (mprotect_exec( base, size, unix_prot )) /* FIXME: last error */
896 return FALSE;
898 set_page_vprot( base, size, vprot );
899 return TRUE;
903 /***********************************************************************
904 * set_protection
906 * Set page protections on a range of pages
908 static NTSTATUS set_protection( struct file_view *view, void *base, SIZE_T size, ULONG protect )
910 unsigned int vprot;
911 NTSTATUS status;
913 if ((status = get_vprot_flags( protect, &vprot, view->protect & SEC_IMAGE ))) return status;
914 if (is_view_valloc( view ))
916 if (vprot & VPROT_WRITECOPY) return STATUS_INVALID_PAGE_PROTECTION;
918 else
920 BYTE access = vprot & (VPROT_READ | VPROT_WRITE | VPROT_EXEC);
921 if ((view->protect & access) != access) return STATUS_INVALID_PAGE_PROTECTION;
924 if (!VIRTUAL_SetProt( view, base, size, vprot | VPROT_COMMITTED )) return STATUS_ACCESS_DENIED;
925 return STATUS_SUCCESS;
929 /***********************************************************************
930 * update_write_watches
932 static void update_write_watches( void *base, size_t size, size_t accessed_size )
934 TRACE( "updating watch %p-%p-%p\n", base, (char *)base + accessed_size, (char *)base + size );
935 /* clear write watch flag on accessed pages */
936 set_page_vprot_bits( base, accessed_size, 0, VPROT_WRITEWATCH );
937 /* restore page protections on the entire range */
938 mprotect_range( base, size, 0, 0 );
942 /***********************************************************************
943 * reset_write_watches
945 * Reset write watches in a memory range.
947 static void reset_write_watches( void *base, SIZE_T size )
949 set_page_vprot_bits( base, size, VPROT_WRITEWATCH, 0 );
950 mprotect_range( base, size, 0, 0 );
954 /***********************************************************************
955 * unmap_extra_space
957 * Release the extra memory while keeping the range starting on the granularity boundary.
959 static inline void *unmap_extra_space( void *ptr, size_t total_size, size_t wanted_size, size_t mask )
961 if ((ULONG_PTR)ptr & mask)
963 size_t extra = mask + 1 - ((ULONG_PTR)ptr & mask);
964 munmap( ptr, extra );
965 ptr = (char *)ptr + extra;
966 total_size -= extra;
968 if (total_size > wanted_size)
969 munmap( (char *)ptr + wanted_size, total_size - wanted_size );
970 return ptr;
974 struct alloc_area
976 size_t size;
977 size_t mask;
978 int top_down;
979 void *limit;
980 void *result;
983 /***********************************************************************
984 * alloc_reserved_area_callback
986 * Try to map some space inside a reserved area. Callback for wine_mmap_enum_reserved_areas.
988 static int alloc_reserved_area_callback( void *start, size_t size, void *arg )
990 struct alloc_area *alloc = arg;
991 void *end = (char *)start + size;
993 if (start < address_space_start) start = address_space_start;
994 if (is_beyond_limit( start, size, alloc->limit )) end = alloc->limit;
995 if (start >= end) return 0;
997 /* make sure we don't touch the preloader reserved range */
998 if (preload_reserve_end >= start)
1000 if (preload_reserve_end >= end)
1002 if (preload_reserve_start <= start) return 0; /* no space in that area */
1003 if (preload_reserve_start < end) end = preload_reserve_start;
1005 else if (preload_reserve_start <= start) start = preload_reserve_end;
1006 else
1008 /* range is split in two by the preloader reservation, try first part */
1009 if ((alloc->result = find_free_area( start, preload_reserve_start, alloc->size,
1010 alloc->mask, alloc->top_down )))
1011 return 1;
1012 /* then fall through to try second part */
1013 start = preload_reserve_end;
1016 if ((alloc->result = find_free_area( start, end, alloc->size, alloc->mask, alloc->top_down )))
1017 return 1;
1019 return 0;
1022 /***********************************************************************
1023 * map_fixed_area
1025 * mmap the fixed memory area.
1026 * The csVirtual section must be held by caller.
1028 static NTSTATUS map_fixed_area( void *base, size_t size, unsigned int vprot )
1030 void *ptr;
1032 switch (wine_mmap_is_in_reserved_area( base, size ))
1034 case -1: /* partially in a reserved area */
1036 NTSTATUS status;
1037 struct area_boundary area;
1038 size_t lower_size;
1039 area.base = base;
1040 area.size = size;
1041 wine_mmap_enum_reserved_areas( get_area_boundary_callback, &area, 0 );
1042 assert( area.boundary );
1043 lower_size = (char *)area.boundary - (char *)base;
1044 status = map_fixed_area( base, lower_size, vprot );
1045 if (status == STATUS_SUCCESS)
1047 status = map_fixed_area( area.boundary, size - lower_size, vprot);
1048 if (status != STATUS_SUCCESS) unmap_area( base, lower_size );
1050 return status;
1052 case 0: /* not in a reserved area, do a normal allocation */
1053 if ((ptr = wine_anon_mmap( base, size, VIRTUAL_GetUnixProt(vprot), 0 )) == (void *)-1)
1055 if (errno == ENOMEM) return STATUS_NO_MEMORY;
1056 return STATUS_INVALID_PARAMETER;
1058 if (ptr != base)
1060 /* We couldn't get the address we wanted */
1061 if (is_beyond_limit( ptr, size, user_space_limit )) add_reserved_area( ptr, size );
1062 else munmap( ptr, size );
1063 return STATUS_CONFLICTING_ADDRESSES;
1065 break;
1067 default:
1068 case 1: /* in a reserved area, make sure the address is available */
1069 if (find_view_range( base, size )) return STATUS_CONFLICTING_ADDRESSES;
1070 /* replace the reserved area by our mapping */
1071 if ((ptr = wine_anon_mmap( base, size, VIRTUAL_GetUnixProt(vprot), MAP_FIXED )) != base)
1072 return STATUS_INVALID_PARAMETER;
1073 break;
1075 if (is_beyond_limit( ptr, size, working_set_limit )) working_set_limit = address_space_limit;
1076 return STATUS_SUCCESS;
1079 /***********************************************************************
1080 * map_view
1082 * Create a view and mmap the corresponding memory area.
1083 * The csVirtual section must be held by caller.
1085 static NTSTATUS map_view( struct file_view **view_ret, void *base, size_t size, size_t mask,
1086 int top_down, unsigned int vprot )
1088 void *ptr;
1089 NTSTATUS status;
1091 if (base)
1093 if (is_beyond_limit( base, size, address_space_limit ))
1094 return STATUS_WORKING_SET_LIMIT_RANGE;
1095 status = map_fixed_area( base, size, vprot );
1096 if (status != STATUS_SUCCESS) return status;
1097 ptr = base;
1099 else
1101 size_t view_size = size + mask + 1;
1102 struct alloc_area alloc;
1104 alloc.size = size;
1105 alloc.mask = mask;
1106 alloc.top_down = top_down;
1107 alloc.limit = user_space_limit;
1108 if (wine_mmap_enum_reserved_areas( alloc_reserved_area_callback, &alloc, top_down ))
1110 ptr = alloc.result;
1111 TRACE( "got mem in reserved area %p-%p\n", ptr, (char *)ptr + size );
1112 if (wine_anon_mmap( ptr, size, VIRTUAL_GetUnixProt(vprot), MAP_FIXED ) != ptr)
1113 return STATUS_INVALID_PARAMETER;
1114 goto done;
1117 for (;;)
1119 if ((ptr = wine_anon_mmap( NULL, view_size, VIRTUAL_GetUnixProt(vprot), 0 )) == (void *)-1)
1121 if (errno == ENOMEM) return STATUS_NO_MEMORY;
1122 return STATUS_INVALID_PARAMETER;
1124 TRACE( "got mem with anon mmap %p-%p\n", ptr, (char *)ptr + size );
1125 /* if we got something beyond the user limit, unmap it and retry */
1126 if (is_beyond_limit( ptr, view_size, user_space_limit )) add_reserved_area( ptr, view_size );
1127 else break;
1129 ptr = unmap_extra_space( ptr, view_size, size, mask );
1131 done:
1132 status = create_view( view_ret, ptr, size, vprot );
1133 if (status != STATUS_SUCCESS) unmap_area( ptr, size );
1134 return status;
1138 /***********************************************************************
1139 * map_file_into_view
1141 * Wrapper for mmap() to map a file into a view, falling back to read if mmap fails.
1142 * The csVirtual section must be held by caller.
1144 static NTSTATUS map_file_into_view( struct file_view *view, int fd, size_t start, size_t size,
1145 off_t offset, unsigned int vprot, BOOL removable )
1147 void *ptr;
1148 int prot = VIRTUAL_GetUnixProt( vprot | VPROT_COMMITTED /* make sure it is accessible */ );
1149 unsigned int flags = MAP_FIXED | ((vprot & VPROT_WRITECOPY) ? MAP_PRIVATE : MAP_SHARED);
1151 assert( start < view->size );
1152 assert( start + size <= view->size );
1154 if (force_exec_prot && (vprot & VPROT_READ))
1156 TRACE( "forcing exec permission on mapping %p-%p\n",
1157 (char *)view->base + start, (char *)view->base + start + size - 1 );
1158 prot |= PROT_EXEC;
1161 /* only try mmap if media is not removable (or if we require write access) */
1162 if (!removable || (flags & MAP_SHARED))
1164 if (mmap( (char *)view->base + start, size, prot, flags, fd, offset ) != (void *)-1)
1165 goto done;
1167 switch (errno)
1169 case EINVAL: /* file offset is not page-aligned, fall back to read() */
1170 if (flags & MAP_SHARED) return STATUS_INVALID_PARAMETER;
1171 break;
1172 case ENOEXEC:
1173 case ENODEV: /* filesystem doesn't support mmap(), fall back to read() */
1174 if (vprot & VPROT_WRITE)
1176 ERR( "shared writable mmap not supported, broken filesystem?\n" );
1177 return STATUS_NOT_SUPPORTED;
1179 break;
1180 case EACCES:
1181 case EPERM: /* noexec filesystem, fall back to read() */
1182 if (flags & MAP_SHARED)
1184 if (prot & PROT_EXEC) ERR( "failed to set PROT_EXEC on file map, noexec filesystem?\n" );
1185 return STATUS_ACCESS_DENIED;
1187 if (prot & PROT_EXEC) WARN( "failed to set PROT_EXEC on file map, noexec filesystem?\n" );
1188 break;
1189 default:
1190 return FILE_GetNtStatus();
1194 /* Reserve the memory with an anonymous mmap */
1195 ptr = wine_anon_mmap( (char *)view->base + start, size, PROT_READ | PROT_WRITE, MAP_FIXED );
1196 if (ptr == (void *)-1) return FILE_GetNtStatus();
1197 /* Now read in the file */
1198 pread( fd, ptr, size, offset );
1199 if (prot != (PROT_READ|PROT_WRITE)) mprotect( ptr, size, prot ); /* Set the right protection */
1200 done:
1201 set_page_vprot( (char *)view->base + start, size, vprot );
1202 return STATUS_SUCCESS;
1206 /***********************************************************************
1207 * get_committed_size
1209 * Get the size of the committed range starting at base.
1210 * Also return the protections for the first page.
1212 static SIZE_T get_committed_size( struct file_view *view, void *base, BYTE *vprot )
1214 SIZE_T i, start;
1216 start = ((char *)base - (char *)view->base) >> page_shift;
1217 *vprot = get_page_vprot( base );
1219 if (view->protect & SEC_RESERVE)
1221 SIZE_T ret = 0;
1222 SERVER_START_REQ( get_mapping_committed_range )
1224 req->base = wine_server_client_ptr( view->base );
1225 req->offset = start << page_shift;
1226 if (!wine_server_call( req ))
1228 ret = reply->size;
1229 if (reply->committed)
1231 *vprot |= VPROT_COMMITTED;
1232 set_page_vprot_bits( base, ret, VPROT_COMMITTED, 0 );
1236 SERVER_END_REQ;
1237 return ret;
1239 for (i = start + 1; i < view->size >> page_shift; i++)
1240 if ((*vprot ^ get_page_vprot( (char *)view->base + (i << page_shift) )) & VPROT_COMMITTED) break;
1241 return (i - start) << page_shift;
1245 /***********************************************************************
1246 * decommit_view
1248 * Decommit some pages of a given view.
1249 * The csVirtual section must be held by caller.
1251 static NTSTATUS decommit_pages( struct file_view *view, size_t start, size_t size )
1253 if (wine_anon_mmap( (char *)view->base + start, size, PROT_NONE, MAP_FIXED ) != (void *)-1)
1255 set_page_vprot_bits( (char *)view->base + start, size, 0, VPROT_COMMITTED );
1256 return STATUS_SUCCESS;
1258 return FILE_GetNtStatus();
1262 /***********************************************************************
1263 * allocate_dos_memory
1265 * Allocate the DOS memory range.
1267 static NTSTATUS allocate_dos_memory( struct file_view **view, unsigned int vprot )
1269 size_t size;
1270 void *addr = NULL;
1271 void * const low_64k = (void *)0x10000;
1272 const size_t dosmem_size = 0x110000;
1273 int unix_prot = VIRTUAL_GetUnixProt( vprot );
1275 /* check for existing view */
1277 if (find_view_range( 0, dosmem_size )) return STATUS_CONFLICTING_ADDRESSES;
1279 /* check without the first 64K */
1281 if (wine_mmap_is_in_reserved_area( low_64k, dosmem_size - 0x10000 ) != 1)
1283 addr = wine_anon_mmap( low_64k, dosmem_size - 0x10000, unix_prot, 0 );
1284 if (addr != low_64k)
1286 if (addr != (void *)-1) munmap( addr, dosmem_size - 0x10000 );
1287 return map_view( view, NULL, dosmem_size, 0xffff, 0, vprot );
1291 /* now try to allocate the low 64K too */
1293 if (wine_mmap_is_in_reserved_area( NULL, 0x10000 ) != 1)
1295 addr = wine_anon_mmap( (void *)page_size, 0x10000 - page_size, unix_prot, 0 );
1296 if (addr == (void *)page_size)
1298 if (!wine_anon_mmap( NULL, page_size, unix_prot, MAP_FIXED ))
1300 addr = NULL;
1301 TRACE( "successfully mapped low 64K range\n" );
1303 else TRACE( "failed to map page 0\n" );
1305 else
1307 if (addr != (void *)-1) munmap( addr, 0x10000 - page_size );
1308 addr = low_64k;
1309 TRACE( "failed to map low 64K range\n" );
1313 /* now reserve the whole range */
1315 size = (char *)dosmem_size - (char *)addr;
1316 wine_anon_mmap( addr, size, unix_prot, MAP_FIXED );
1317 return create_view( view, addr, size, vprot );
1321 /***********************************************************************
1322 * map_pe_header
1324 * Map the header of a PE file into memory.
1326 static NTSTATUS map_pe_header( void *ptr, size_t size, int fd, BOOL *removable )
1328 if (!size) return STATUS_INVALID_IMAGE_FORMAT;
1330 if (!*removable)
1332 if (mmap( ptr, size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_FIXED|MAP_PRIVATE, fd, 0 ) != (void *)-1)
1333 return STATUS_SUCCESS;
1335 switch (errno)
1337 case EPERM:
1338 case EACCES:
1339 WARN( "noexec file system, falling back to read\n" );
1340 break;
1341 case ENOEXEC:
1342 case ENODEV:
1343 WARN( "file system doesn't support mmap, falling back to read\n" );
1344 break;
1345 default:
1346 return FILE_GetNtStatus();
1348 *removable = TRUE;
1350 pread( fd, ptr, size, 0 );
1351 return STATUS_SUCCESS; /* page protections will be updated later */
1355 /***********************************************************************
1356 * map_image
1358 * Map an executable (PE format) image into memory.
1360 static NTSTATUS map_image( HANDLE hmapping, ACCESS_MASK access, int fd, char *base, SIZE_T total_size,
1361 SIZE_T mask, SIZE_T header_size, int shared_fd, BOOL removable, PVOID *addr_ptr )
1363 IMAGE_DOS_HEADER *dos;
1364 IMAGE_NT_HEADERS *nt;
1365 IMAGE_SECTION_HEADER sections[96];
1366 IMAGE_SECTION_HEADER *sec;
1367 IMAGE_DATA_DIRECTORY *imports;
1368 NTSTATUS status = STATUS_CONFLICTING_ADDRESSES;
1369 int i;
1370 off_t pos;
1371 sigset_t sigset;
1372 struct stat st;
1373 struct file_view *view = NULL;
1374 char *ptr, *header_end, *header_start;
1376 /* zero-map the whole range */
1378 server_enter_uninterrupted_section( &csVirtual, &sigset );
1380 if (base >= (char *)address_space_start) /* make sure the DOS area remains free */
1381 status = map_view( &view, base, total_size, mask, FALSE, SEC_IMAGE | SEC_FILE |
1382 VPROT_COMMITTED | VPROT_READ | VPROT_EXEC | VPROT_WRITECOPY );
1384 if (status != STATUS_SUCCESS)
1385 status = map_view( &view, NULL, total_size, mask, FALSE, SEC_IMAGE | SEC_FILE |
1386 VPROT_COMMITTED | VPROT_READ | VPROT_EXEC | VPROT_WRITECOPY );
1388 if (status != STATUS_SUCCESS) goto error;
1390 ptr = view->base;
1391 TRACE_(module)( "mapped PE file at %p-%p\n", ptr, ptr + total_size );
1393 /* map the header */
1395 if (fstat( fd, &st ) == -1)
1397 status = FILE_GetNtStatus();
1398 goto error;
1400 header_size = min( header_size, st.st_size );
1401 if ((status = map_pe_header( view->base, header_size, fd, &removable )) != STATUS_SUCCESS) goto error;
1403 status = STATUS_INVALID_IMAGE_FORMAT; /* generic error */
1404 dos = (IMAGE_DOS_HEADER *)ptr;
1405 nt = (IMAGE_NT_HEADERS *)(ptr + dos->e_lfanew);
1406 header_end = ptr + ROUND_SIZE( 0, header_size );
1407 memset( ptr + header_size, 0, header_end - (ptr + header_size) );
1408 if ((char *)(nt + 1) > header_end) goto error;
1409 header_start = (char*)&nt->OptionalHeader+nt->FileHeader.SizeOfOptionalHeader;
1410 if (nt->FileHeader.NumberOfSections > sizeof(sections)/sizeof(*sections)) goto error;
1411 if (header_start + sizeof(*sections) * nt->FileHeader.NumberOfSections > header_end) goto error;
1412 /* Some applications (e.g. the Steam version of Borderlands) map over the top of the section headers,
1413 * copying the headers into local memory is necessary to properly load such applications. */
1414 memcpy(sections, header_start, sizeof(*sections) * nt->FileHeader.NumberOfSections);
1415 sec = sections;
1417 imports = nt->OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_IMPORT;
1418 if (!imports->Size || !imports->VirtualAddress) imports = NULL;
1420 /* check for non page-aligned binary */
1422 if (nt->OptionalHeader.SectionAlignment <= page_mask)
1424 /* unaligned sections, this happens for native subsystem binaries */
1425 /* in that case Windows simply maps in the whole file */
1427 if (map_file_into_view( view, fd, 0, total_size, 0, VPROT_COMMITTED | VPROT_READ | VPROT_WRITECOPY,
1428 removable ) != STATUS_SUCCESS) goto error;
1430 /* check that all sections are loaded at the right offset */
1431 if (nt->OptionalHeader.FileAlignment != nt->OptionalHeader.SectionAlignment) goto error;
1432 for (i = 0; i < nt->FileHeader.NumberOfSections; i++)
1434 if (sec[i].VirtualAddress != sec[i].PointerToRawData)
1435 goto error; /* Windows refuses to load in that case too */
1438 /* set the image protections */
1439 VIRTUAL_SetProt( view, ptr, total_size,
1440 VPROT_COMMITTED | VPROT_READ | VPROT_WRITECOPY | VPROT_EXEC );
1442 /* no relocations are performed on non page-aligned binaries */
1443 goto done;
1447 /* map all the sections */
1449 for (i = pos = 0; i < nt->FileHeader.NumberOfSections; i++, sec++)
1451 static const SIZE_T sector_align = 0x1ff;
1452 SIZE_T map_size, file_start, file_size, end;
1454 if (!sec->Misc.VirtualSize)
1455 map_size = ROUND_SIZE( 0, sec->SizeOfRawData );
1456 else
1457 map_size = ROUND_SIZE( 0, sec->Misc.VirtualSize );
1459 /* file positions are rounded to sector boundaries regardless of OptionalHeader.FileAlignment */
1460 file_start = sec->PointerToRawData & ~sector_align;
1461 file_size = (sec->SizeOfRawData + (sec->PointerToRawData & sector_align) + sector_align) & ~sector_align;
1462 if (file_size > map_size) file_size = map_size;
1464 /* a few sanity checks */
1465 end = sec->VirtualAddress + ROUND_SIZE( sec->VirtualAddress, map_size );
1466 if (sec->VirtualAddress > total_size || end > total_size || end < sec->VirtualAddress)
1468 WARN_(module)( "Section %.8s too large (%x+%lx/%lx)\n",
1469 sec->Name, sec->VirtualAddress, map_size, total_size );
1470 goto error;
1473 if ((sec->Characteristics & IMAGE_SCN_MEM_SHARED) &&
1474 (sec->Characteristics & IMAGE_SCN_MEM_WRITE))
1476 TRACE_(module)( "mapping shared section %.8s at %p off %x (%x) size %lx (%lx) flags %x\n",
1477 sec->Name, ptr + sec->VirtualAddress,
1478 sec->PointerToRawData, (int)pos, file_size, map_size,
1479 sec->Characteristics );
1480 if (map_file_into_view( view, shared_fd, sec->VirtualAddress, map_size, pos,
1481 VPROT_COMMITTED | VPROT_READ | VPROT_WRITE, FALSE ) != STATUS_SUCCESS)
1483 ERR_(module)( "Could not map shared section %.8s\n", sec->Name );
1484 goto error;
1487 /* check if the import directory falls inside this section */
1488 if (imports && imports->VirtualAddress >= sec->VirtualAddress &&
1489 imports->VirtualAddress < sec->VirtualAddress + map_size)
1491 UINT_PTR base = imports->VirtualAddress & ~page_mask;
1492 UINT_PTR end = base + ROUND_SIZE( imports->VirtualAddress, imports->Size );
1493 if (end > sec->VirtualAddress + map_size) end = sec->VirtualAddress + map_size;
1494 if (end > base)
1495 map_file_into_view( view, shared_fd, base, end - base,
1496 pos + (base - sec->VirtualAddress),
1497 VPROT_COMMITTED | VPROT_READ | VPROT_WRITECOPY, FALSE );
1499 pos += map_size;
1500 continue;
1503 TRACE_(module)( "mapping section %.8s at %p off %x size %x virt %x flags %x\n",
1504 sec->Name, ptr + sec->VirtualAddress,
1505 sec->PointerToRawData, sec->SizeOfRawData,
1506 sec->Misc.VirtualSize, sec->Characteristics );
1508 if (!sec->PointerToRawData || !file_size) continue;
1510 /* Note: if the section is not aligned properly map_file_into_view will magically
1511 * fall back to read(), so we don't need to check anything here.
1513 end = file_start + file_size;
1514 if (sec->PointerToRawData >= st.st_size ||
1515 end > ((st.st_size + sector_align) & ~sector_align) ||
1516 end < file_start ||
1517 map_file_into_view( view, fd, sec->VirtualAddress, file_size, file_start,
1518 VPROT_COMMITTED | VPROT_READ | VPROT_WRITECOPY,
1519 removable ) != STATUS_SUCCESS)
1521 ERR_(module)( "Could not map section %.8s, file probably truncated\n", sec->Name );
1522 goto error;
1525 if (file_size & page_mask)
1527 end = ROUND_SIZE( 0, file_size );
1528 if (end > map_size) end = map_size;
1529 TRACE_(module)("clearing %p - %p\n",
1530 ptr + sec->VirtualAddress + file_size,
1531 ptr + sec->VirtualAddress + end );
1532 memset( ptr + sec->VirtualAddress + file_size, 0, end - file_size );
1536 /* set the image protections */
1538 VIRTUAL_SetProt( view, ptr, ROUND_SIZE( 0, header_size ), VPROT_COMMITTED | VPROT_READ );
1540 sec = sections;
1541 for (i = 0; i < nt->FileHeader.NumberOfSections; i++, sec++)
1543 SIZE_T size;
1544 BYTE vprot = VPROT_COMMITTED;
1546 if (sec->Misc.VirtualSize)
1547 size = ROUND_SIZE( sec->VirtualAddress, sec->Misc.VirtualSize );
1548 else
1549 size = ROUND_SIZE( sec->VirtualAddress, sec->SizeOfRawData );
1551 if (sec->Characteristics & IMAGE_SCN_MEM_READ) vprot |= VPROT_READ;
1552 if (sec->Characteristics & IMAGE_SCN_MEM_WRITE) vprot |= VPROT_WRITECOPY;
1553 if (sec->Characteristics & IMAGE_SCN_MEM_EXECUTE) vprot |= VPROT_EXEC;
1555 /* Dumb game crack lets the AOEP point into a data section. Adjust. */
1556 if ((nt->OptionalHeader.AddressOfEntryPoint >= sec->VirtualAddress) &&
1557 (nt->OptionalHeader.AddressOfEntryPoint < sec->VirtualAddress + size))
1558 vprot |= VPROT_EXEC;
1560 if (!VIRTUAL_SetProt( view, ptr + sec->VirtualAddress, size, vprot ) && (vprot & VPROT_EXEC))
1561 ERR( "failed to set %08x protection on section %.8s, noexec filesystem?\n",
1562 sec->Characteristics, sec->Name );
1565 done:
1567 SERVER_START_REQ( map_view )
1569 req->mapping = wine_server_obj_handle( hmapping );
1570 req->access = access;
1571 req->base = wine_server_client_ptr( view->base );
1572 req->size = view->size;
1573 req->start = 0;
1574 status = wine_server_call( req );
1576 SERVER_END_REQ;
1577 if (status) goto error;
1579 VIRTUAL_DEBUG_DUMP_VIEW( view );
1580 server_leave_uninterrupted_section( &csVirtual, &sigset );
1582 *addr_ptr = ptr;
1583 #ifdef VALGRIND_LOAD_PDB_DEBUGINFO
1584 VALGRIND_LOAD_PDB_DEBUGINFO(fd, ptr, total_size, ptr - base);
1585 #endif
1586 if (ptr != base) return STATUS_IMAGE_NOT_AT_BASE;
1587 return STATUS_SUCCESS;
1589 error:
1590 if (view) delete_view( view );
1591 server_leave_uninterrupted_section( &csVirtual, &sigset );
1592 return status;
1596 /***********************************************************************
1597 * virtual_map_section
1599 * Map a file section into memory.
1601 NTSTATUS virtual_map_section( HANDLE handle, PVOID *addr_ptr, ULONG zero_bits, SIZE_T commit_size,
1602 const LARGE_INTEGER *offset_ptr, SIZE_T *size_ptr, ULONG protect,
1603 pe_image_info_t *image_info )
1605 NTSTATUS res;
1606 mem_size_t full_size;
1607 ACCESS_MASK access;
1608 SIZE_T size, mask = get_mask( zero_bits );
1609 int unix_handle = -1, needs_close;
1610 unsigned int vprot, sec_flags;
1611 struct file_view *view;
1612 HANDLE shared_file;
1613 LARGE_INTEGER offset;
1614 sigset_t sigset;
1616 offset.QuadPart = offset_ptr ? offset_ptr->QuadPart : 0;
1618 switch(protect)
1620 case PAGE_NOACCESS:
1621 case PAGE_READONLY:
1622 case PAGE_WRITECOPY:
1623 access = SECTION_MAP_READ;
1624 break;
1625 case PAGE_READWRITE:
1626 access = SECTION_MAP_WRITE;
1627 break;
1628 case PAGE_EXECUTE:
1629 case PAGE_EXECUTE_READ:
1630 case PAGE_EXECUTE_WRITECOPY:
1631 access = SECTION_MAP_READ | SECTION_MAP_EXECUTE;
1632 break;
1633 case PAGE_EXECUTE_READWRITE:
1634 access = SECTION_MAP_WRITE | SECTION_MAP_EXECUTE;
1635 break;
1636 default:
1637 return STATUS_INVALID_PAGE_PROTECTION;
1640 SERVER_START_REQ( get_mapping_info )
1642 req->handle = wine_server_obj_handle( handle );
1643 req->access = access;
1644 wine_server_set_reply( req, image_info, sizeof(*image_info) );
1645 res = wine_server_call( req );
1646 sec_flags = reply->flags;
1647 full_size = reply->size;
1648 shared_file = wine_server_ptr_handle( reply->shared_file );
1650 SERVER_END_REQ;
1651 if (res) return res;
1653 if ((res = server_get_unix_fd( handle, 0, &unix_handle, &needs_close, NULL, NULL ))) goto done;
1655 if (sec_flags & SEC_IMAGE)
1657 void *base = wine_server_get_ptr( image_info->base );
1659 if ((ULONG_PTR)base != image_info->base) base = NULL;
1660 size = image_info->map_size;
1661 if (size != image_info->map_size) /* truncated */
1663 WARN( "Modules larger than 4Gb (%s) not supported\n",
1664 wine_dbgstr_longlong(image_info->map_size) );
1665 res = STATUS_INVALID_PARAMETER;
1666 goto done;
1668 if (shared_file)
1670 int shared_fd, shared_needs_close;
1672 if ((res = server_get_unix_fd( shared_file, FILE_READ_DATA|FILE_WRITE_DATA,
1673 &shared_fd, &shared_needs_close, NULL, NULL ))) goto done;
1674 res = map_image( handle, access, unix_handle, base, size, mask, image_info->header_size,
1675 shared_fd, needs_close, addr_ptr );
1676 if (shared_needs_close) close( shared_fd );
1677 close_handle( shared_file );
1679 else
1681 res = map_image( handle, access, unix_handle, base, size, mask, image_info->header_size,
1682 -1, needs_close, addr_ptr );
1684 if (needs_close) close( unix_handle );
1685 if (res >= 0) *size_ptr = size;
1686 return res;
1689 res = STATUS_INVALID_PARAMETER;
1690 if (offset.QuadPart >= full_size) goto done;
1691 if (*size_ptr)
1693 size = *size_ptr;
1694 if (size > full_size - offset.QuadPart)
1696 res = STATUS_INVALID_VIEW_SIZE;
1697 goto done;
1700 else
1702 size = full_size - offset.QuadPart;
1703 if (size != full_size - offset.QuadPart) /* truncated */
1705 WARN( "Files larger than 4Gb (%s) not supported on this platform\n",
1706 wine_dbgstr_longlong(full_size) );
1707 goto done;
1710 if (!(size = ROUND_SIZE( 0, size ))) goto done; /* wrap-around */
1712 /* Reserve a properly aligned area */
1714 server_enter_uninterrupted_section( &csVirtual, &sigset );
1716 get_vprot_flags( protect, &vprot, sec_flags & SEC_IMAGE );
1717 vprot |= sec_flags;
1718 if (!(sec_flags & SEC_RESERVE)) vprot |= VPROT_COMMITTED;
1719 res = map_view( &view, *addr_ptr, size, mask, FALSE, vprot );
1720 if (res)
1722 server_leave_uninterrupted_section( &csVirtual, &sigset );
1723 goto done;
1726 /* Map the file */
1728 TRACE( "handle=%p size=%lx offset=%x%08x\n", handle, size, offset.u.HighPart, offset.u.LowPart );
1730 res = map_file_into_view( view, unix_handle, 0, size, offset.QuadPart, vprot, needs_close );
1731 if (res == STATUS_SUCCESS)
1733 SERVER_START_REQ( map_view )
1735 req->mapping = wine_server_obj_handle( handle );
1736 req->access = access;
1737 req->base = wine_server_client_ptr( view->base );
1738 req->size = size;
1739 req->start = offset.QuadPart;
1740 res = wine_server_call( req );
1742 SERVER_END_REQ;
1745 if (res == STATUS_SUCCESS)
1747 *addr_ptr = view->base;
1748 *size_ptr = size;
1749 VIRTUAL_DEBUG_DUMP_VIEW( view );
1751 else
1753 ERR( "mapping %p %lx %x%08x failed\n", view->base, size, offset.u.HighPart, offset.u.LowPart );
1754 delete_view( view );
1757 server_leave_uninterrupted_section( &csVirtual, &sigset );
1759 done:
1760 if (needs_close) close( unix_handle );
1761 return res;
1765 struct alloc_virtual_heap
1767 void *base;
1768 size_t size;
1771 /* callback for wine_mmap_enum_reserved_areas to allocate space for the virtual heap */
1772 static int alloc_virtual_heap( void *base, size_t size, void *arg )
1774 struct alloc_virtual_heap *alloc = arg;
1776 if (is_beyond_limit( base, size, address_space_limit )) address_space_limit = (char *)base + size;
1777 if (size < alloc->size) return 0;
1778 if (is_win64 && base < (void *)0x80000000) return 0;
1779 alloc->base = wine_anon_mmap( (char *)base + size - alloc->size, alloc->size,
1780 PROT_READ|PROT_WRITE, MAP_FIXED );
1781 return (alloc->base != (void *)-1);
1784 /***********************************************************************
1785 * virtual_init
1787 void virtual_init(void)
1789 const char *preload;
1790 struct alloc_virtual_heap alloc_views;
1791 size_t size;
1793 #if !defined(__i386__) && !defined(__x86_64__)
1794 page_size = sysconf( _SC_PAGESIZE );
1795 page_mask = page_size - 1;
1796 /* Make sure we have a power of 2 */
1797 assert( !(page_size & page_mask) );
1798 page_shift = 0;
1799 while ((1 << page_shift) != page_size) page_shift++;
1800 #ifdef _WIN64
1801 address_space_limit = (void *)(((1UL << 47) - 1) & ~page_mask);
1802 #else
1803 address_space_limit = (void *)~page_mask;
1804 #endif
1805 user_space_limit = working_set_limit = address_space_limit;
1806 #endif
1807 if ((preload = getenv("WINEPRELOADRESERVE")))
1809 unsigned long start, end;
1810 if (sscanf( preload, "%lx-%lx", &start, &end ) == 2)
1812 preload_reserve_start = (void *)start;
1813 preload_reserve_end = (void *)end;
1814 /* some apps start inside the DOS area */
1815 if (preload_reserve_start)
1816 address_space_start = min( address_space_start, preload_reserve_start );
1820 /* try to find space in a reserved area for the views and pages protection table */
1821 #ifdef _WIN64
1822 pages_vprot_size = ((size_t)address_space_limit >> page_shift >> pages_vprot_shift) + 1;
1823 alloc_views.size = view_block_size + pages_vprot_size * sizeof(*pages_vprot);
1824 #else
1825 alloc_views.size = view_block_size + (1U << (32 - page_shift));
1826 #endif
1827 if (wine_mmap_enum_reserved_areas( alloc_virtual_heap, &alloc_views, 1 ))
1828 wine_mmap_remove_reserved_area( alloc_views.base, alloc_views.size, 0 );
1829 else
1830 alloc_views.base = wine_anon_mmap( NULL, alloc_views.size, PROT_READ | PROT_WRITE, 0 );
1832 assert( alloc_views.base != (void *)-1 );
1833 view_block_start = alloc_views.base;
1834 view_block_end = view_block_start + view_block_size / sizeof(*view_block_start);
1835 pages_vprot = (void *)((char *)alloc_views.base + view_block_size);
1836 wine_rb_init( &views_tree, compare_view );
1838 /* make the DOS area accessible (except the low 64K) to hide bugs in broken apps like Excel 2003 */
1839 size = (char *)address_space_start - (char *)0x10000;
1840 if (size && wine_mmap_is_in_reserved_area( (void*)0x10000, size ) == 1)
1841 wine_anon_mmap( (void *)0x10000, size, PROT_READ | PROT_WRITE, MAP_FIXED );
1845 /***********************************************************************
1846 * virtual_init_threading
1848 void virtual_init_threading(void)
1850 use_locks = TRUE;
1854 /***********************************************************************
1855 * virtual_get_system_info
1857 void virtual_get_system_info( SYSTEM_BASIC_INFORMATION *info )
1859 #ifdef HAVE_SYSINFO
1860 struct sysinfo sinfo;
1861 #endif
1863 info->unknown = 0;
1864 info->KeMaximumIncrement = 0; /* FIXME */
1865 info->PageSize = page_size;
1866 info->MmLowestPhysicalPage = 1;
1867 info->MmHighestPhysicalPage = 0x7fffffff / page_size;
1868 #ifdef HAVE_SYSINFO
1869 if (!sysinfo(&sinfo))
1871 ULONG64 total = (ULONG64)sinfo.totalram * sinfo.mem_unit;
1872 info->MmHighestPhysicalPage = max(1, total / page_size);
1874 #endif
1875 info->MmNumberOfPhysicalPages = info->MmHighestPhysicalPage - info->MmLowestPhysicalPage;
1876 info->AllocationGranularity = get_mask(0) + 1;
1877 info->LowestUserAddress = (void *)0x10000;
1878 info->HighestUserAddress = (char *)user_space_limit - 1;
1879 info->ActiveProcessorsAffinityMask = get_system_affinity_mask();
1880 info->NumberOfProcessors = NtCurrentTeb()->Peb->NumberOfProcessors;
1884 /***********************************************************************
1885 * virtual_create_builtin_view
1887 NTSTATUS virtual_create_builtin_view( void *module )
1889 NTSTATUS status;
1890 sigset_t sigset;
1891 IMAGE_NT_HEADERS *nt = RtlImageNtHeader( module );
1892 SIZE_T size = nt->OptionalHeader.SizeOfImage;
1893 IMAGE_SECTION_HEADER *sec;
1894 struct file_view *view;
1895 void *base;
1896 int i;
1898 size = ROUND_SIZE( module, size );
1899 base = ROUND_ADDR( module, page_mask );
1900 server_enter_uninterrupted_section( &csVirtual, &sigset );
1901 status = create_view( &view, base, size, SEC_IMAGE | SEC_FILE | VPROT_SYSTEM |
1902 VPROT_COMMITTED | VPROT_READ | VPROT_WRITECOPY | VPROT_EXEC );
1903 if (!status)
1905 TRACE( "created %p-%p\n", base, (char *)base + size );
1907 /* The PE header is always read-only, no write, no execute. */
1908 set_page_vprot( base, page_size, VPROT_COMMITTED | VPROT_READ );
1910 sec = (IMAGE_SECTION_HEADER *)((char *)&nt->OptionalHeader + nt->FileHeader.SizeOfOptionalHeader);
1911 for (i = 0; i < nt->FileHeader.NumberOfSections; i++)
1913 BYTE flags = VPROT_COMMITTED;
1915 if (sec[i].Characteristics & IMAGE_SCN_MEM_EXECUTE) flags |= VPROT_EXEC;
1916 if (sec[i].Characteristics & IMAGE_SCN_MEM_READ) flags |= VPROT_READ;
1917 if (sec[i].Characteristics & IMAGE_SCN_MEM_WRITE) flags |= VPROT_WRITE;
1918 set_page_vprot( (char *)base + sec[i].VirtualAddress, sec[i].Misc.VirtualSize, flags );
1920 VIRTUAL_DEBUG_DUMP_VIEW( view );
1922 server_leave_uninterrupted_section( &csVirtual, &sigset );
1923 return status;
1927 /***********************************************************************
1928 * virtual_alloc_thread_stack
1930 NTSTATUS virtual_alloc_thread_stack( TEB *teb, SIZE_T reserve_size, SIZE_T commit_size, SIZE_T *pthread_size )
1932 struct file_view *view;
1933 NTSTATUS status;
1934 sigset_t sigset;
1935 SIZE_T size, extra_size = 0;
1937 if (!reserve_size || !commit_size)
1939 IMAGE_NT_HEADERS *nt = RtlImageNtHeader( NtCurrentTeb()->Peb->ImageBaseAddress );
1940 if (!reserve_size) reserve_size = nt->OptionalHeader.SizeOfStackReserve;
1941 if (!commit_size) commit_size = nt->OptionalHeader.SizeOfStackCommit;
1944 size = max( reserve_size, commit_size );
1945 if (size < 1024 * 1024) size = 1024 * 1024; /* Xlib needs a large stack */
1946 size = (size + 0xffff) & ~0xffff; /* round to 64K boundary */
1947 if (pthread_size) *pthread_size = extra_size = max( page_size, ROUND_SIZE( 0, *pthread_size ));
1949 server_enter_uninterrupted_section( &csVirtual, &sigset );
1951 if ((status = map_view( &view, NULL, size + extra_size, 0xffff, 0,
1952 VPROT_READ | VPROT_WRITE | VPROT_COMMITTED )) != STATUS_SUCCESS)
1953 goto done;
1955 #ifdef VALGRIND_STACK_REGISTER
1956 VALGRIND_STACK_REGISTER( view->base, (char *)view->base + view->size );
1957 #endif
1959 /* setup no access guard page */
1960 set_page_vprot( view->base, page_size, VPROT_COMMITTED );
1961 set_page_vprot( (char *)view->base + page_size, page_size,
1962 VPROT_READ | VPROT_WRITE | VPROT_COMMITTED | VPROT_GUARD );
1963 mprotect_range( view->base, 2 * page_size, 0, 0 );
1964 VIRTUAL_DEBUG_DUMP_VIEW( view );
1966 if (extra_size)
1968 struct file_view *extra_view;
1970 /* shrink the first view and create a second one for the extra size */
1971 /* this allows the app to free the stack without freeing the thread start portion */
1972 view->size -= extra_size;
1973 status = create_view( &extra_view, (char *)view->base + view->size, extra_size,
1974 VPROT_READ | VPROT_WRITE | VPROT_COMMITTED );
1975 if (status != STATUS_SUCCESS)
1977 unmap_area( (char *)view->base + view->size, extra_size );
1978 delete_view( view );
1979 goto done;
1983 /* note: limit is lower than base since the stack grows down */
1984 teb->DeallocationStack = view->base;
1985 teb->Tib.StackBase = (char *)view->base + view->size;
1986 teb->Tib.StackLimit = (char *)view->base + 2 * page_size;
1987 done:
1988 server_leave_uninterrupted_section( &csVirtual, &sigset );
1989 return status;
1993 /***********************************************************************
1994 * virtual_clear_thread_stack
1996 * Clear the stack contents before calling the main entry point, some broken apps need that.
1998 void virtual_clear_thread_stack( void *stack_end )
2000 void *stack = NtCurrentTeb()->Tib.StackLimit;
2001 size_t size = (char *)stack_end - (char *)stack;
2003 wine_anon_mmap( stack, size, PROT_READ | PROT_WRITE, MAP_FIXED );
2004 if (force_exec_prot) mprotect( stack, size, PROT_READ | PROT_WRITE | PROT_EXEC );
2008 /***********************************************************************
2009 * virtual_handle_fault
2011 NTSTATUS virtual_handle_fault( LPCVOID addr, DWORD err, BOOL on_signal_stack )
2013 NTSTATUS ret = STATUS_ACCESS_VIOLATION;
2014 void *page = ROUND_ADDR( addr, page_mask );
2015 sigset_t sigset;
2016 BYTE vprot;
2018 server_enter_uninterrupted_section( &csVirtual, &sigset );
2019 vprot = get_page_vprot( page );
2020 if (!on_signal_stack && (vprot & VPROT_GUARD))
2022 set_page_vprot_bits( page, page_size, 0, VPROT_GUARD );
2023 mprotect_range( page, page_size, 0, 0 );
2024 ret = STATUS_GUARD_PAGE_VIOLATION;
2026 else if (err & EXCEPTION_WRITE_FAULT)
2028 if (vprot & VPROT_WRITEWATCH)
2030 set_page_vprot_bits( page, page_size, 0, VPROT_WRITEWATCH );
2031 mprotect_range( page, page_size, 0, 0 );
2033 /* ignore fault if page is writable now */
2034 if (VIRTUAL_GetUnixProt( get_page_vprot( page )) & PROT_WRITE)
2036 if ((vprot & VPROT_WRITEWATCH) || is_write_watch_range( page, page_size ))
2037 ret = STATUS_SUCCESS;
2040 server_leave_uninterrupted_section( &csVirtual, &sigset );
2041 return ret;
2045 /***********************************************************************
2046 * check_write_access
2048 * Check if the memory range is writable, temporarily disabling write watches if necessary.
2050 static NTSTATUS check_write_access( void *base, size_t size, BOOL *has_write_watch )
2052 size_t i;
2053 char *addr = ROUND_ADDR( base, page_mask );
2055 size = ROUND_SIZE( base, size );
2056 for (i = 0; i < size; i += page_size)
2058 BYTE vprot = get_page_vprot( addr + i );
2059 if (vprot & VPROT_WRITEWATCH) *has_write_watch = TRUE;
2060 if (!(VIRTUAL_GetUnixProt( vprot & ~VPROT_WRITEWATCH ) & PROT_WRITE))
2061 return STATUS_INVALID_USER_BUFFER;
2063 if (*has_write_watch)
2064 mprotect_range( addr, size, 0, VPROT_WRITEWATCH ); /* temporarily enable write access */
2065 return STATUS_SUCCESS;
2069 /***********************************************************************
2070 * virtual_locked_server_call
2072 unsigned int virtual_locked_server_call( void *req_ptr )
2074 struct __server_request_info * const req = req_ptr;
2075 sigset_t sigset;
2076 void *addr = req->reply_data;
2077 data_size_t size = req->u.req.request_header.reply_size;
2078 BOOL has_write_watch = FALSE;
2079 unsigned int ret = STATUS_ACCESS_VIOLATION;
2081 if (!size) return wine_server_call( req_ptr );
2083 server_enter_uninterrupted_section( &csVirtual, &sigset );
2084 if (!(ret = check_write_access( addr, size, &has_write_watch )))
2086 ret = server_call_unlocked( req );
2087 if (has_write_watch) update_write_watches( addr, size, wine_server_reply_size( req ));
2089 server_leave_uninterrupted_section( &csVirtual, &sigset );
2090 return ret;
2094 /***********************************************************************
2095 * virtual_locked_read
2097 ssize_t virtual_locked_read( int fd, void *addr, size_t size )
2099 sigset_t sigset;
2100 BOOL has_write_watch = FALSE;
2101 int err = EFAULT;
2103 ssize_t ret = read( fd, addr, size );
2104 if (ret != -1 || errno != EFAULT) return ret;
2106 server_enter_uninterrupted_section( &csVirtual, &sigset );
2107 if (!check_write_access( addr, size, &has_write_watch ))
2109 ret = read( fd, addr, size );
2110 err = errno;
2111 if (has_write_watch) update_write_watches( addr, size, max( 0, ret ));
2113 server_leave_uninterrupted_section( &csVirtual, &sigset );
2114 errno = err;
2115 return ret;
2119 /***********************************************************************
2120 * virtual_locked_pread
2122 ssize_t virtual_locked_pread( int fd, void *addr, size_t size, off_t offset )
2124 sigset_t sigset;
2125 BOOL has_write_watch = FALSE;
2126 int err = EFAULT;
2128 ssize_t ret = pread( fd, addr, size, offset );
2129 if (ret != -1 || errno != EFAULT) return ret;
2131 server_enter_uninterrupted_section( &csVirtual, &sigset );
2132 if (!check_write_access( addr, size, &has_write_watch ))
2134 ret = pread( fd, addr, size, offset );
2135 err = errno;
2136 if (has_write_watch) update_write_watches( addr, size, max( 0, ret ));
2138 server_leave_uninterrupted_section( &csVirtual, &sigset );
2139 errno = err;
2140 return ret;
2144 /***********************************************************************
2145 * __wine_locked_recvmsg
2147 ssize_t CDECL __wine_locked_recvmsg( int fd, struct msghdr *hdr, int flags )
2149 sigset_t sigset;
2150 size_t i;
2151 BOOL has_write_watch = FALSE;
2152 int err = EFAULT;
2154 ssize_t ret = recvmsg( fd, hdr, flags );
2155 if (ret != -1 || errno != EFAULT) return ret;
2157 server_enter_uninterrupted_section( &csVirtual, &sigset );
2158 for (i = 0; i < hdr->msg_iovlen; i++)
2159 if (check_write_access( hdr->msg_iov[i].iov_base, hdr->msg_iov[i].iov_len, &has_write_watch ))
2160 break;
2161 if (i == hdr->msg_iovlen)
2163 ret = recvmsg( fd, hdr, flags );
2164 err = errno;
2166 if (has_write_watch)
2167 while (i--) update_write_watches( hdr->msg_iov[i].iov_base, hdr->msg_iov[i].iov_len, 0 );
2169 server_leave_uninterrupted_section( &csVirtual, &sigset );
2170 errno = err;
2171 return ret;
2175 /***********************************************************************
2176 * virtual_is_valid_code_address
2178 BOOL virtual_is_valid_code_address( const void *addr, SIZE_T size )
2180 struct file_view *view;
2181 BOOL ret = FALSE;
2182 sigset_t sigset;
2184 server_enter_uninterrupted_section( &csVirtual, &sigset );
2185 if ((view = VIRTUAL_FindView( addr, size )))
2186 ret = !(view->protect & VPROT_SYSTEM); /* system views are not visible to the app */
2187 server_leave_uninterrupted_section( &csVirtual, &sigset );
2188 return ret;
2192 /***********************************************************************
2193 * virtual_handle_stack_fault
2195 * Handle an access fault inside the current thread stack.
2196 * Called from inside a signal handler.
2198 BOOL virtual_handle_stack_fault( void *addr )
2200 BOOL ret = FALSE;
2202 RtlEnterCriticalSection( &csVirtual ); /* no need for signal masking inside signal handler */
2203 if (get_page_vprot( addr ) & VPROT_GUARD)
2205 char *page = ROUND_ADDR( addr, page_mask );
2206 set_page_vprot_bits( page, page_size, 0, VPROT_GUARD );
2207 mprotect_range( page, page_size, 0, 0 );
2208 NtCurrentTeb()->Tib.StackLimit = page;
2209 if (page >= (char *)NtCurrentTeb()->DeallocationStack + 2*page_size)
2211 page -= page_size;
2212 set_page_vprot_bits( page, page_size, VPROT_COMMITTED | VPROT_GUARD, 0 );
2213 mprotect_range( page, page_size, 0, 0 );
2215 ret = TRUE;
2217 RtlLeaveCriticalSection( &csVirtual );
2218 return ret;
2222 /***********************************************************************
2223 * virtual_check_buffer_for_read
2225 * Check if a memory buffer can be read, triggering page faults if needed for DIB section access.
2227 BOOL virtual_check_buffer_for_read( const void *ptr, SIZE_T size )
2229 if (!size) return TRUE;
2230 if (!ptr) return FALSE;
2232 __TRY
2234 volatile const char *p = ptr;
2235 char dummy __attribute__((unused));
2236 SIZE_T count = size;
2238 while (count > page_size)
2240 dummy = *p;
2241 p += page_size;
2242 count -= page_size;
2244 dummy = p[0];
2245 dummy = p[count - 1];
2247 __EXCEPT_PAGE_FAULT
2249 return FALSE;
2251 __ENDTRY
2252 return TRUE;
2256 /***********************************************************************
2257 * virtual_check_buffer_for_write
2259 * Check if a memory buffer can be written to, triggering page faults if needed for write watches.
2261 BOOL virtual_check_buffer_for_write( void *ptr, SIZE_T size )
2263 if (!size) return TRUE;
2264 if (!ptr) return FALSE;
2266 __TRY
2268 volatile char *p = ptr;
2269 SIZE_T count = size;
2271 while (count > page_size)
2273 *p |= 0;
2274 p += page_size;
2275 count -= page_size;
2277 p[0] |= 0;
2278 p[count - 1] |= 0;
2280 __EXCEPT_PAGE_FAULT
2282 return FALSE;
2284 __ENDTRY
2285 return TRUE;
2289 /***********************************************************************
2290 * virtual_uninterrupted_read_memory
2292 * Similar to NtReadVirtualMemory, but without wineserver calls. Moreover
2293 * permissions are checked before accessing each page, to ensure that no
2294 * exceptions can happen.
2296 SIZE_T virtual_uninterrupted_read_memory( const void *addr, void *buffer, SIZE_T size )
2298 struct file_view *view;
2299 sigset_t sigset;
2300 SIZE_T bytes_read = 0;
2302 if (!size) return 0;
2304 server_enter_uninterrupted_section( &csVirtual, &sigset );
2305 if ((view = VIRTUAL_FindView( addr, size )))
2307 if (!(view->protect & VPROT_SYSTEM))
2309 char *page = ROUND_ADDR( addr, page_mask );
2311 while (bytes_read < size && (VIRTUAL_GetUnixProt( get_page_vprot( page )) & PROT_READ))
2313 SIZE_T block_size = min( size, page_size - ((UINT_PTR)addr & page_mask) );
2314 memcpy( buffer, addr, block_size );
2316 addr = (const void *)((const char *)addr + block_size);
2317 buffer = (void *)((char *)buffer + block_size);
2318 bytes_read += block_size;
2319 page += page_size;
2323 server_leave_uninterrupted_section( &csVirtual, &sigset );
2324 return bytes_read;
2328 /***********************************************************************
2329 * virtual_uninterrupted_write_memory
2331 * Similar to NtWriteVirtualMemory, but without wineserver calls. Moreover
2332 * permissions are checked before accessing each page, to ensure that no
2333 * exceptions can happen.
2335 NTSTATUS virtual_uninterrupted_write_memory( void *addr, const void *buffer, SIZE_T size )
2337 BOOL has_write_watch = FALSE;
2338 sigset_t sigset;
2339 NTSTATUS ret;
2341 if (!size) return STATUS_SUCCESS;
2343 server_enter_uninterrupted_section( &csVirtual, &sigset );
2344 if (!(ret = check_write_access( addr, size, &has_write_watch )))
2346 memcpy( addr, buffer, size );
2347 if (has_write_watch) update_write_watches( addr, size, size );
2349 server_leave_uninterrupted_section( &csVirtual, &sigset );
2350 return ret;
2354 /***********************************************************************
2355 * VIRTUAL_SetForceExec
2357 * Whether to force exec prot on all views.
2359 void VIRTUAL_SetForceExec( BOOL enable )
2361 struct file_view *view;
2362 sigset_t sigset;
2364 server_enter_uninterrupted_section( &csVirtual, &sigset );
2365 if (!force_exec_prot != !enable) /* change all existing views */
2367 force_exec_prot = enable;
2369 WINE_RB_FOR_EACH_ENTRY( view, &views_tree, struct file_view, entry )
2371 /* file mappings are always accessible */
2372 BYTE commit = is_view_valloc( view ) ? 0 : VPROT_COMMITTED;
2374 mprotect_range( view->base, view->size, commit, 0 );
2377 server_leave_uninterrupted_section( &csVirtual, &sigset );
2380 struct free_range
2382 char *base;
2383 char *limit;
2386 /* free reserved areas above the limit; callback for wine_mmap_enum_reserved_areas */
2387 static int free_reserved_memory( void *base, size_t size, void *arg )
2389 struct free_range *range = arg;
2391 if ((char *)base >= range->limit) return 0;
2392 if ((char *)base + size <= range->base) return 0;
2393 if ((char *)base < range->base)
2395 size -= range->base - (char *)base;
2396 base = range->base;
2398 if ((char *)base + size > range->limit) size = range->limit - (char *)base;
2399 remove_reserved_area( base, size );
2400 return 1; /* stop enumeration since the list has changed */
2403 /***********************************************************************
2404 * virtual_release_address_space
2406 * Release some address space once we have loaded and initialized the app.
2408 void virtual_release_address_space(void)
2410 struct free_range range;
2411 sigset_t sigset;
2413 if (is_win64) return;
2415 server_enter_uninterrupted_section( &csVirtual, &sigset );
2417 range.base = (char *)0x82000000;
2418 range.limit = user_space_limit;
2420 if (range.limit > range.base)
2422 while (wine_mmap_enum_reserved_areas( free_reserved_memory, &range, 1 )) /* nothing */;
2424 else
2426 #ifndef __APPLE__ /* dyld doesn't support parts of the WINE_DOS segment being unmapped */
2427 range.base = (char *)0x20000000;
2428 range.limit = (char *)0x7f000000;
2429 while (wine_mmap_enum_reserved_areas( free_reserved_memory, &range, 0 )) /* nothing */;
2430 #endif
2433 server_leave_uninterrupted_section( &csVirtual, &sigset );
2437 /***********************************************************************
2438 * virtual_set_large_address_space
2440 * Enable use of a large address space when allowed by the application.
2442 void virtual_set_large_address_space(void)
2444 IMAGE_NT_HEADERS *nt = RtlImageNtHeader( NtCurrentTeb()->Peb->ImageBaseAddress );
2446 if (!(nt->FileHeader.Characteristics & IMAGE_FILE_LARGE_ADDRESS_AWARE)) return;
2447 /* no large address space on win9x */
2448 if (NtCurrentTeb()->Peb->OSPlatformId != VER_PLATFORM_WIN32_NT) return;
2450 user_space_limit = working_set_limit = address_space_limit;
2454 /***********************************************************************
2455 * NtAllocateVirtualMemory (NTDLL.@)
2456 * ZwAllocateVirtualMemory (NTDLL.@)
2458 NTSTATUS WINAPI NtAllocateVirtualMemory( HANDLE process, PVOID *ret, ULONG zero_bits,
2459 SIZE_T *size_ptr, ULONG type, ULONG protect )
2461 void *base;
2462 unsigned int vprot;
2463 SIZE_T size = *size_ptr;
2464 SIZE_T mask = get_mask( zero_bits );
2465 NTSTATUS status = STATUS_SUCCESS;
2466 BOOL is_dos_memory = FALSE;
2467 struct file_view *view;
2468 sigset_t sigset;
2470 TRACE("%p %p %08lx %x %08x\n", process, *ret, size, type, protect );
2472 if (!size) return STATUS_INVALID_PARAMETER;
2473 if (!mask) return STATUS_INVALID_PARAMETER_3;
2475 if (process != NtCurrentProcess())
2477 apc_call_t call;
2478 apc_result_t result;
2480 memset( &call, 0, sizeof(call) );
2482 call.virtual_alloc.type = APC_VIRTUAL_ALLOC;
2483 call.virtual_alloc.addr = wine_server_client_ptr( *ret );
2484 call.virtual_alloc.size = *size_ptr;
2485 call.virtual_alloc.zero_bits = zero_bits;
2486 call.virtual_alloc.op_type = type;
2487 call.virtual_alloc.prot = protect;
2488 status = server_queue_process_apc( process, &call, &result );
2489 if (status != STATUS_SUCCESS) return status;
2491 if (result.virtual_alloc.status == STATUS_SUCCESS)
2493 *ret = wine_server_get_ptr( result.virtual_alloc.addr );
2494 *size_ptr = result.virtual_alloc.size;
2496 return result.virtual_alloc.status;
2499 /* Round parameters to a page boundary */
2501 if (is_beyond_limit( 0, size, working_set_limit )) return STATUS_WORKING_SET_LIMIT_RANGE;
2503 if (*ret)
2505 if (type & MEM_RESERVE) /* Round down to 64k boundary */
2506 base = ROUND_ADDR( *ret, mask );
2507 else
2508 base = ROUND_ADDR( *ret, page_mask );
2509 size = (((UINT_PTR)*ret + size + page_mask) & ~page_mask) - (UINT_PTR)base;
2511 /* disallow low 64k, wrap-around and kernel space */
2512 if (((char *)base < (char *)0x10000) ||
2513 ((char *)base + size < (char *)base) ||
2514 is_beyond_limit( base, size, address_space_limit ))
2516 /* address 1 is magic to mean DOS area */
2517 if (!base && *ret == (void *)1 && size == 0x110000) is_dos_memory = TRUE;
2518 else return STATUS_INVALID_PARAMETER;
2521 else
2523 base = NULL;
2524 size = (size + page_mask) & ~page_mask;
2527 /* Compute the alloc type flags */
2529 if (!(type & (MEM_COMMIT | MEM_RESERVE | MEM_RESET)) ||
2530 (type & ~(MEM_COMMIT | MEM_RESERVE | MEM_TOP_DOWN | MEM_WRITE_WATCH | MEM_RESET)))
2532 WARN("called with wrong alloc type flags (%08x) !\n", type);
2533 return STATUS_INVALID_PARAMETER;
2536 /* Reserve the memory */
2538 if (use_locks) server_enter_uninterrupted_section( &csVirtual, &sigset );
2540 if ((type & MEM_RESERVE) || !base)
2542 if (!(status = get_vprot_flags( protect, &vprot, FALSE )))
2544 if (type & MEM_COMMIT) vprot |= VPROT_COMMITTED;
2545 if (type & MEM_WRITE_WATCH) vprot |= VPROT_WRITEWATCH;
2546 if (protect & PAGE_NOCACHE) vprot |= SEC_NOCACHE;
2548 if (vprot & VPROT_WRITECOPY) status = STATUS_INVALID_PAGE_PROTECTION;
2549 else if (is_dos_memory) status = allocate_dos_memory( &view, vprot );
2550 else status = map_view( &view, base, size, mask, type & MEM_TOP_DOWN, vprot );
2552 if (status == STATUS_SUCCESS) base = view->base;
2555 else if (type & MEM_RESET)
2557 if (!(view = VIRTUAL_FindView( base, size ))) status = STATUS_NOT_MAPPED_VIEW;
2558 else madvise( base, size, MADV_DONTNEED );
2560 else /* commit the pages */
2562 if (!(view = VIRTUAL_FindView( base, size ))) status = STATUS_NOT_MAPPED_VIEW;
2563 else if (view->protect & SEC_FILE) status = STATUS_ALREADY_COMMITTED;
2564 else if (!(status = set_protection( view, base, size, protect )) && (view->protect & SEC_RESERVE))
2566 SERVER_START_REQ( add_mapping_committed_range )
2568 req->base = wine_server_client_ptr( view->base );
2569 req->offset = (char *)base - (char *)view->base;
2570 req->size = size;
2571 wine_server_call( req );
2573 SERVER_END_REQ;
2577 if (!status) VIRTUAL_DEBUG_DUMP_VIEW( view );
2579 if (use_locks) server_leave_uninterrupted_section( &csVirtual, &sigset );
2581 if (status == STATUS_SUCCESS)
2583 *ret = base;
2584 *size_ptr = size;
2586 return status;
2590 /***********************************************************************
2591 * NtFreeVirtualMemory (NTDLL.@)
2592 * ZwFreeVirtualMemory (NTDLL.@)
2594 NTSTATUS WINAPI NtFreeVirtualMemory( HANDLE process, PVOID *addr_ptr, SIZE_T *size_ptr, ULONG type )
2596 struct file_view *view;
2597 char *base;
2598 sigset_t sigset;
2599 NTSTATUS status = STATUS_SUCCESS;
2600 LPVOID addr = *addr_ptr;
2601 SIZE_T size = *size_ptr;
2603 TRACE("%p %p %08lx %x\n", process, addr, size, type );
2605 if (process != NtCurrentProcess())
2607 apc_call_t call;
2608 apc_result_t result;
2610 memset( &call, 0, sizeof(call) );
2612 call.virtual_free.type = APC_VIRTUAL_FREE;
2613 call.virtual_free.addr = wine_server_client_ptr( addr );
2614 call.virtual_free.size = size;
2615 call.virtual_free.op_type = type;
2616 status = server_queue_process_apc( process, &call, &result );
2617 if (status != STATUS_SUCCESS) return status;
2619 if (result.virtual_free.status == STATUS_SUCCESS)
2621 *addr_ptr = wine_server_get_ptr( result.virtual_free.addr );
2622 *size_ptr = result.virtual_free.size;
2624 return result.virtual_free.status;
2627 /* Fix the parameters */
2629 size = ROUND_SIZE( addr, size );
2630 base = ROUND_ADDR( addr, page_mask );
2632 /* avoid freeing the DOS area when a broken app passes a NULL pointer */
2633 if (!base) return STATUS_INVALID_PARAMETER;
2635 server_enter_uninterrupted_section( &csVirtual, &sigset );
2637 if (!(view = VIRTUAL_FindView( base, size )) || !is_view_valloc( view ))
2639 status = STATUS_INVALID_PARAMETER;
2641 else if (type == MEM_RELEASE)
2643 /* Free the pages */
2645 if (size || (base != view->base)) status = STATUS_INVALID_PARAMETER;
2646 else
2648 delete_view( view );
2649 *addr_ptr = base;
2650 *size_ptr = size;
2653 else if (type == MEM_DECOMMIT)
2655 status = decommit_pages( view, base - (char *)view->base, size );
2656 if (status == STATUS_SUCCESS)
2658 *addr_ptr = base;
2659 *size_ptr = size;
2662 else
2664 WARN("called with wrong free type flags (%08x) !\n", type);
2665 status = STATUS_INVALID_PARAMETER;
2668 server_leave_uninterrupted_section( &csVirtual, &sigset );
2669 return status;
2673 /***********************************************************************
2674 * NtProtectVirtualMemory (NTDLL.@)
2675 * ZwProtectVirtualMemory (NTDLL.@)
2677 NTSTATUS WINAPI NtProtectVirtualMemory( HANDLE process, PVOID *addr_ptr, SIZE_T *size_ptr,
2678 ULONG new_prot, ULONG *old_prot )
2680 struct file_view *view;
2681 sigset_t sigset;
2682 NTSTATUS status = STATUS_SUCCESS;
2683 char *base;
2684 BYTE vprot;
2685 SIZE_T size = *size_ptr;
2686 LPVOID addr = *addr_ptr;
2687 DWORD old;
2689 TRACE("%p %p %08lx %08x\n", process, addr, size, new_prot );
2691 if (!old_prot)
2692 return STATUS_ACCESS_VIOLATION;
2694 if (process != NtCurrentProcess())
2696 apc_call_t call;
2697 apc_result_t result;
2699 memset( &call, 0, sizeof(call) );
2701 call.virtual_protect.type = APC_VIRTUAL_PROTECT;
2702 call.virtual_protect.addr = wine_server_client_ptr( addr );
2703 call.virtual_protect.size = size;
2704 call.virtual_protect.prot = new_prot;
2705 status = server_queue_process_apc( process, &call, &result );
2706 if (status != STATUS_SUCCESS) return status;
2708 if (result.virtual_protect.status == STATUS_SUCCESS)
2710 *addr_ptr = wine_server_get_ptr( result.virtual_protect.addr );
2711 *size_ptr = result.virtual_protect.size;
2712 if (old_prot) *old_prot = result.virtual_protect.prot;
2714 return result.virtual_protect.status;
2717 /* Fix the parameters */
2719 size = ROUND_SIZE( addr, size );
2720 base = ROUND_ADDR( addr, page_mask );
2722 server_enter_uninterrupted_section( &csVirtual, &sigset );
2724 if ((view = VIRTUAL_FindView( base, size )))
2726 /* Make sure all the pages are committed */
2727 if (get_committed_size( view, base, &vprot ) >= size && (vprot & VPROT_COMMITTED))
2729 old = VIRTUAL_GetWin32Prot( vprot, view->protect );
2730 status = set_protection( view, base, size, new_prot );
2732 else status = STATUS_NOT_COMMITTED;
2734 else status = STATUS_INVALID_PARAMETER;
2736 if (!status) VIRTUAL_DEBUG_DUMP_VIEW( view );
2738 server_leave_uninterrupted_section( &csVirtual, &sigset );
2740 if (status == STATUS_SUCCESS)
2742 *addr_ptr = base;
2743 *size_ptr = size;
2744 *old_prot = old;
2746 return status;
2750 /* retrieve state for a free memory area; callback for wine_mmap_enum_reserved_areas */
2751 static int get_free_mem_state_callback( void *start, size_t size, void *arg )
2753 MEMORY_BASIC_INFORMATION *info = arg;
2754 void *end = (char *)start + size;
2756 if ((char *)info->BaseAddress + info->RegionSize < (char *)start) return 0;
2758 if (info->BaseAddress >= end)
2760 if (info->AllocationBase < end) info->AllocationBase = end;
2761 return 0;
2764 if (info->BaseAddress >= start || start <= address_space_start)
2766 /* it's a real free area */
2767 info->State = MEM_FREE;
2768 info->Protect = PAGE_NOACCESS;
2769 info->AllocationBase = 0;
2770 info->AllocationProtect = 0;
2771 info->Type = 0;
2772 if ((char *)info->BaseAddress + info->RegionSize > (char *)end)
2773 info->RegionSize = (char *)end - (char *)info->BaseAddress;
2775 else /* outside of the reserved area, pretend it's allocated */
2777 info->RegionSize = (char *)start - (char *)info->BaseAddress;
2778 info->State = MEM_RESERVE;
2779 info->Protect = PAGE_NOACCESS;
2780 info->AllocationProtect = PAGE_NOACCESS;
2781 info->Type = MEM_PRIVATE;
2783 return 1;
2786 #define UNIMPLEMENTED_INFO_CLASS(c) \
2787 case c: \
2788 FIXME("(process=%p,addr=%p) Unimplemented information class: " #c "\n", process, addr); \
2789 return STATUS_INVALID_INFO_CLASS
2791 /***********************************************************************
2792 * NtQueryVirtualMemory (NTDLL.@)
2793 * ZwQueryVirtualMemory (NTDLL.@)
2795 NTSTATUS WINAPI NtQueryVirtualMemory( HANDLE process, LPCVOID addr,
2796 MEMORY_INFORMATION_CLASS info_class, PVOID buffer,
2797 SIZE_T len, SIZE_T *res_len )
2799 struct file_view *view;
2800 char *base, *alloc_base = 0, *alloc_end = working_set_limit;
2801 struct wine_rb_entry *ptr;
2802 MEMORY_BASIC_INFORMATION *info = buffer;
2803 sigset_t sigset;
2805 if (info_class != MemoryBasicInformation)
2807 switch(info_class)
2809 UNIMPLEMENTED_INFO_CLASS(MemoryWorkingSetList);
2810 UNIMPLEMENTED_INFO_CLASS(MemorySectionName);
2811 UNIMPLEMENTED_INFO_CLASS(MemoryBasicVlmInformation);
2813 default:
2814 FIXME("(%p,%p,info_class=%d,%p,%ld,%p) Unknown information class\n",
2815 process, addr, info_class, buffer, len, res_len);
2816 return STATUS_INVALID_INFO_CLASS;
2820 if (process != NtCurrentProcess())
2822 NTSTATUS status;
2823 apc_call_t call;
2824 apc_result_t result;
2826 memset( &call, 0, sizeof(call) );
2828 call.virtual_query.type = APC_VIRTUAL_QUERY;
2829 call.virtual_query.addr = wine_server_client_ptr( addr );
2830 status = server_queue_process_apc( process, &call, &result );
2831 if (status != STATUS_SUCCESS) return status;
2833 if (result.virtual_query.status == STATUS_SUCCESS)
2835 info->BaseAddress = wine_server_get_ptr( result.virtual_query.base );
2836 info->AllocationBase = wine_server_get_ptr( result.virtual_query.alloc_base );
2837 info->RegionSize = result.virtual_query.size;
2838 info->Protect = result.virtual_query.prot;
2839 info->AllocationProtect = result.virtual_query.alloc_prot;
2840 info->State = (DWORD)result.virtual_query.state << 12;
2841 info->Type = (DWORD)result.virtual_query.alloc_type << 16;
2842 if (info->RegionSize != result.virtual_query.size) /* truncated */
2843 return STATUS_INVALID_PARAMETER; /* FIXME */
2844 if (res_len) *res_len = sizeof(*info);
2846 return result.virtual_query.status;
2849 base = ROUND_ADDR( addr, page_mask );
2851 if (is_beyond_limit( base, 1, working_set_limit )) return STATUS_WORKING_SET_LIMIT_RANGE;
2853 /* Find the view containing the address */
2855 server_enter_uninterrupted_section( &csVirtual, &sigset );
2856 ptr = views_tree.root;
2857 while (ptr)
2859 view = WINE_RB_ENTRY_VALUE( ptr, struct file_view, entry );
2860 if ((char *)view->base > base)
2862 alloc_end = view->base;
2863 ptr = ptr->left;
2865 else if ((char *)view->base + view->size <= base)
2867 alloc_base = (char *)view->base + view->size;
2868 ptr = ptr->right;
2870 else
2872 alloc_base = view->base;
2873 alloc_end = (char *)view->base + view->size;
2874 break;
2878 /* Fill the info structure */
2880 info->AllocationBase = alloc_base;
2881 info->BaseAddress = base;
2882 info->RegionSize = alloc_end - base;
2884 if (!ptr)
2886 if (!wine_mmap_enum_reserved_areas( get_free_mem_state_callback, info, 0 ))
2888 /* not in a reserved area at all, pretend it's allocated */
2889 #ifdef __i386__
2890 if (base >= (char *)address_space_start)
2892 info->State = MEM_RESERVE;
2893 info->Protect = PAGE_NOACCESS;
2894 info->AllocationProtect = PAGE_NOACCESS;
2895 info->Type = MEM_PRIVATE;
2897 else
2898 #endif
2900 info->State = MEM_FREE;
2901 info->Protect = PAGE_NOACCESS;
2902 info->AllocationBase = 0;
2903 info->AllocationProtect = 0;
2904 info->Type = 0;
2908 else
2910 BYTE vprot;
2911 char *ptr;
2912 SIZE_T range_size = get_committed_size( view, base, &vprot );
2914 info->State = (vprot & VPROT_COMMITTED) ? MEM_COMMIT : MEM_RESERVE;
2915 info->Protect = (vprot & VPROT_COMMITTED) ? VIRTUAL_GetWin32Prot( vprot, view->protect ) : 0;
2916 info->AllocationProtect = VIRTUAL_GetWin32Prot( view->protect, view->protect );
2917 if (view->protect & SEC_IMAGE) info->Type = MEM_IMAGE;
2918 else if (view->protect & (SEC_FILE | SEC_RESERVE | SEC_COMMIT)) info->Type = MEM_MAPPED;
2919 else info->Type = MEM_PRIVATE;
2920 for (ptr = base; ptr < base + range_size; ptr += page_size)
2921 if ((get_page_vprot( ptr ) ^ vprot) & ~VPROT_WRITEWATCH) break;
2922 info->RegionSize = ptr - base;
2924 server_leave_uninterrupted_section( &csVirtual, &sigset );
2926 if (res_len) *res_len = sizeof(*info);
2927 return STATUS_SUCCESS;
2931 /***********************************************************************
2932 * NtLockVirtualMemory (NTDLL.@)
2933 * ZwLockVirtualMemory (NTDLL.@)
2935 NTSTATUS WINAPI NtLockVirtualMemory( HANDLE process, PVOID *addr, SIZE_T *size, ULONG unknown )
2937 NTSTATUS status = STATUS_SUCCESS;
2939 if (process != NtCurrentProcess())
2941 apc_call_t call;
2942 apc_result_t result;
2944 memset( &call, 0, sizeof(call) );
2946 call.virtual_lock.type = APC_VIRTUAL_LOCK;
2947 call.virtual_lock.addr = wine_server_client_ptr( *addr );
2948 call.virtual_lock.size = *size;
2949 status = server_queue_process_apc( process, &call, &result );
2950 if (status != STATUS_SUCCESS) return status;
2952 if (result.virtual_lock.status == STATUS_SUCCESS)
2954 *addr = wine_server_get_ptr( result.virtual_lock.addr );
2955 *size = result.virtual_lock.size;
2957 return result.virtual_lock.status;
2960 *size = ROUND_SIZE( *addr, *size );
2961 *addr = ROUND_ADDR( *addr, page_mask );
2963 if (mlock( *addr, *size )) status = STATUS_ACCESS_DENIED;
2964 return status;
2968 /***********************************************************************
2969 * NtUnlockVirtualMemory (NTDLL.@)
2970 * ZwUnlockVirtualMemory (NTDLL.@)
2972 NTSTATUS WINAPI NtUnlockVirtualMemory( HANDLE process, PVOID *addr, SIZE_T *size, ULONG unknown )
2974 NTSTATUS status = STATUS_SUCCESS;
2976 if (process != NtCurrentProcess())
2978 apc_call_t call;
2979 apc_result_t result;
2981 memset( &call, 0, sizeof(call) );
2983 call.virtual_unlock.type = APC_VIRTUAL_UNLOCK;
2984 call.virtual_unlock.addr = wine_server_client_ptr( *addr );
2985 call.virtual_unlock.size = *size;
2986 status = server_queue_process_apc( process, &call, &result );
2987 if (status != STATUS_SUCCESS) return status;
2989 if (result.virtual_unlock.status == STATUS_SUCCESS)
2991 *addr = wine_server_get_ptr( result.virtual_unlock.addr );
2992 *size = result.virtual_unlock.size;
2994 return result.virtual_unlock.status;
2997 *size = ROUND_SIZE( *addr, *size );
2998 *addr = ROUND_ADDR( *addr, page_mask );
3000 if (munlock( *addr, *size )) status = STATUS_ACCESS_DENIED;
3001 return status;
3005 /***********************************************************************
3006 * NtCreateSection (NTDLL.@)
3007 * ZwCreateSection (NTDLL.@)
3009 NTSTATUS WINAPI NtCreateSection( HANDLE *handle, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr,
3010 const LARGE_INTEGER *size, ULONG protect,
3011 ULONG sec_flags, HANDLE file )
3013 NTSTATUS ret;
3014 unsigned int vprot, file_access = 0;
3015 data_size_t len;
3016 struct object_attributes *objattr;
3018 if ((ret = get_vprot_flags( protect, &vprot, sec_flags & SEC_IMAGE ))) return ret;
3019 if ((ret = alloc_object_attributes( attr, &objattr, &len ))) return ret;
3021 if (vprot & VPROT_READ) file_access |= FILE_READ_DATA;
3022 if (vprot & VPROT_WRITE) file_access |= FILE_WRITE_DATA;
3024 SERVER_START_REQ( create_mapping )
3026 req->access = access;
3027 req->flags = sec_flags;
3028 req->file_handle = wine_server_obj_handle( file );
3029 req->file_access = file_access;
3030 req->size = size ? size->QuadPart : 0;
3031 wine_server_add_data( req, objattr, len );
3032 ret = wine_server_call( req );
3033 *handle = wine_server_ptr_handle( reply->handle );
3035 SERVER_END_REQ;
3037 RtlFreeHeap( GetProcessHeap(), 0, objattr );
3038 return ret;
3042 /***********************************************************************
3043 * NtOpenSection (NTDLL.@)
3044 * ZwOpenSection (NTDLL.@)
3046 NTSTATUS WINAPI NtOpenSection( HANDLE *handle, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr )
3048 NTSTATUS ret;
3050 if ((ret = validate_open_object_attributes( attr ))) return ret;
3052 SERVER_START_REQ( open_mapping )
3054 req->access = access;
3055 req->attributes = attr->Attributes;
3056 req->rootdir = wine_server_obj_handle( attr->RootDirectory );
3057 if (attr->ObjectName)
3058 wine_server_add_data( req, attr->ObjectName->Buffer, attr->ObjectName->Length );
3059 ret = wine_server_call( req );
3060 *handle = wine_server_ptr_handle( reply->handle );
3062 SERVER_END_REQ;
3063 return ret;
3067 /***********************************************************************
3068 * NtMapViewOfSection (NTDLL.@)
3069 * ZwMapViewOfSection (NTDLL.@)
3071 NTSTATUS WINAPI NtMapViewOfSection( HANDLE handle, HANDLE process, PVOID *addr_ptr, ULONG zero_bits,
3072 SIZE_T commit_size, const LARGE_INTEGER *offset_ptr, SIZE_T *size_ptr,
3073 SECTION_INHERIT inherit, ULONG alloc_type, ULONG protect )
3075 NTSTATUS res;
3076 SIZE_T mask = get_mask( zero_bits );
3077 pe_image_info_t image_info;
3078 LARGE_INTEGER offset;
3080 offset.QuadPart = offset_ptr ? offset_ptr->QuadPart : 0;
3082 TRACE("handle=%p process=%p addr=%p off=%x%08x size=%lx access=%x\n",
3083 handle, process, *addr_ptr, offset.u.HighPart, offset.u.LowPart, *size_ptr, protect );
3085 /* Check parameters */
3087 if ((*addr_ptr && zero_bits) || !mask)
3088 return STATUS_INVALID_PARAMETER_4;
3090 #ifndef _WIN64
3091 if (!is_wow64 && (alloc_type & AT_ROUND_TO_PAGE))
3093 *addr_ptr = ROUND_ADDR( *addr_ptr, page_mask );
3094 mask = page_mask;
3096 #endif
3098 if ((offset.u.LowPart & mask) || (*addr_ptr && ((UINT_PTR)*addr_ptr & mask)))
3099 return STATUS_MAPPED_ALIGNMENT;
3101 if (process != NtCurrentProcess())
3103 apc_call_t call;
3104 apc_result_t result;
3106 memset( &call, 0, sizeof(call) );
3108 call.map_view.type = APC_MAP_VIEW;
3109 call.map_view.handle = wine_server_obj_handle( handle );
3110 call.map_view.addr = wine_server_client_ptr( *addr_ptr );
3111 call.map_view.size = *size_ptr;
3112 call.map_view.offset = offset.QuadPart;
3113 call.map_view.zero_bits = zero_bits;
3114 call.map_view.alloc_type = alloc_type;
3115 call.map_view.prot = protect;
3116 res = server_queue_process_apc( process, &call, &result );
3117 if (res != STATUS_SUCCESS) return res;
3119 if ((NTSTATUS)result.map_view.status >= 0)
3121 *addr_ptr = wine_server_get_ptr( result.map_view.addr );
3122 *size_ptr = result.map_view.size;
3124 return result.map_view.status;
3127 return virtual_map_section( handle, addr_ptr, zero_bits, commit_size,
3128 offset_ptr, size_ptr, protect, &image_info );
3132 /***********************************************************************
3133 * NtUnmapViewOfSection (NTDLL.@)
3134 * ZwUnmapViewOfSection (NTDLL.@)
3136 NTSTATUS WINAPI NtUnmapViewOfSection( HANDLE process, PVOID addr )
3138 struct file_view *view;
3139 NTSTATUS status = STATUS_NOT_MAPPED_VIEW;
3140 sigset_t sigset;
3142 if (process != NtCurrentProcess())
3144 apc_call_t call;
3145 apc_result_t result;
3147 memset( &call, 0, sizeof(call) );
3149 call.unmap_view.type = APC_UNMAP_VIEW;
3150 call.unmap_view.addr = wine_server_client_ptr( addr );
3151 status = server_queue_process_apc( process, &call, &result );
3152 if (status == STATUS_SUCCESS) status = result.unmap_view.status;
3153 return status;
3156 server_enter_uninterrupted_section( &csVirtual, &sigset );
3157 if ((view = VIRTUAL_FindView( addr, 0 )) && !is_view_valloc( view ))
3159 if (!(view->protect & VPROT_SYSTEM))
3161 SERVER_START_REQ( unmap_view )
3163 req->base = wine_server_client_ptr( view->base );
3164 status = wine_server_call( req );
3166 SERVER_END_REQ;
3167 if (!status) delete_view( view );
3168 else FIXME( "failed to unmap %p %x\n", view->base, status );
3170 else delete_view( view );
3172 server_leave_uninterrupted_section( &csVirtual, &sigset );
3173 return status;
3177 /******************************************************************************
3178 * NtQuerySection (NTDLL.@)
3179 * ZwQuerySection (NTDLL.@)
3181 NTSTATUS WINAPI NtQuerySection( HANDLE handle, SECTION_INFORMATION_CLASS class, void *ptr,
3182 SIZE_T size, SIZE_T *ret_size )
3184 NTSTATUS status;
3185 pe_image_info_t image_info;
3187 switch (class)
3189 case SectionBasicInformation:
3190 if (size < sizeof(SECTION_BASIC_INFORMATION)) return STATUS_INFO_LENGTH_MISMATCH;
3191 break;
3192 case SectionImageInformation:
3193 if (size < sizeof(SECTION_IMAGE_INFORMATION)) return STATUS_INFO_LENGTH_MISMATCH;
3194 break;
3195 default:
3196 FIXME( "class %u not implemented\n", class );
3197 return STATUS_NOT_IMPLEMENTED;
3199 if (!ptr) return STATUS_ACCESS_VIOLATION;
3201 SERVER_START_REQ( get_mapping_info )
3203 req->handle = wine_server_obj_handle( handle );
3204 req->access = SECTION_QUERY;
3205 wine_server_set_reply( req, &image_info, sizeof(image_info) );
3206 if (!(status = wine_server_call( req )))
3208 if (class == SectionBasicInformation)
3210 SECTION_BASIC_INFORMATION *info = ptr;
3211 info->Attributes = reply->flags;
3212 info->BaseAddress = NULL;
3213 info->Size.QuadPart = reply->size;
3214 if (ret_size) *ret_size = sizeof(*info);
3216 else if (reply->flags & SEC_IMAGE)
3218 SECTION_IMAGE_INFORMATION *info = ptr;
3219 info->TransferAddress = wine_server_get_ptr( image_info.entry_point );
3220 info->ZeroBits = image_info.zerobits;
3221 info->MaximumStackSize = image_info.stack_size;
3222 info->CommittedStackSize = image_info.stack_commit;
3223 info->SubSystemType = image_info.subsystem;
3224 info->SubsystemVersionLow = image_info.subsystem_low;
3225 info->SubsystemVersionHigh = image_info.subsystem_high;
3226 info->GpValue = image_info.gp;
3227 info->ImageCharacteristics = image_info.image_charact;
3228 info->DllCharacteristics = image_info.dll_charact;
3229 info->Machine = image_info.machine;
3230 info->ImageContainsCode = image_info.contains_code;
3231 info->ImageFlags = image_info.image_flags;
3232 info->LoaderFlags = image_info.loader_flags;
3233 info->ImageFileSize = image_info.file_size;
3234 info->CheckSum = image_info.checksum;
3235 if (ret_size) *ret_size = sizeof(*info);
3236 #ifndef _WIN64 /* don't return 64-bit values to 32-bit processes */
3237 if (image_info.machine == IMAGE_FILE_MACHINE_AMD64 ||
3238 image_info.machine == IMAGE_FILE_MACHINE_ARM64)
3240 info->TransferAddress = (void *)0x81231234; /* sic */
3241 info->MaximumStackSize = 0x100000;
3242 info->CommittedStackSize = 0x10000;
3244 #endif
3246 else status = STATUS_SECTION_NOT_IMAGE;
3249 SERVER_END_REQ;
3251 return status;
3255 /***********************************************************************
3256 * NtFlushVirtualMemory (NTDLL.@)
3257 * ZwFlushVirtualMemory (NTDLL.@)
3259 NTSTATUS WINAPI NtFlushVirtualMemory( HANDLE process, LPCVOID *addr_ptr,
3260 SIZE_T *size_ptr, ULONG unknown )
3262 struct file_view *view;
3263 NTSTATUS status = STATUS_SUCCESS;
3264 sigset_t sigset;
3265 void *addr = ROUND_ADDR( *addr_ptr, page_mask );
3267 if (process != NtCurrentProcess())
3269 apc_call_t call;
3270 apc_result_t result;
3272 memset( &call, 0, sizeof(call) );
3274 call.virtual_flush.type = APC_VIRTUAL_FLUSH;
3275 call.virtual_flush.addr = wine_server_client_ptr( addr );
3276 call.virtual_flush.size = *size_ptr;
3277 status = server_queue_process_apc( process, &call, &result );
3278 if (status != STATUS_SUCCESS) return status;
3280 if (result.virtual_flush.status == STATUS_SUCCESS)
3282 *addr_ptr = wine_server_get_ptr( result.virtual_flush.addr );
3283 *size_ptr = result.virtual_flush.size;
3285 return result.virtual_flush.status;
3288 server_enter_uninterrupted_section( &csVirtual, &sigset );
3289 if (!(view = VIRTUAL_FindView( addr, *size_ptr ))) status = STATUS_INVALID_PARAMETER;
3290 else
3292 if (!*size_ptr) *size_ptr = view->size;
3293 *addr_ptr = addr;
3294 #ifdef MS_ASYNC
3295 if (msync( addr, *size_ptr, MS_ASYNC )) status = STATUS_NOT_MAPPED_DATA;
3296 #endif
3298 server_leave_uninterrupted_section( &csVirtual, &sigset );
3299 return status;
3303 /***********************************************************************
3304 * NtGetWriteWatch (NTDLL.@)
3305 * ZwGetWriteWatch (NTDLL.@)
3307 NTSTATUS WINAPI NtGetWriteWatch( HANDLE process, ULONG flags, PVOID base, SIZE_T size, PVOID *addresses,
3308 ULONG_PTR *count, ULONG *granularity )
3310 NTSTATUS status = STATUS_SUCCESS;
3311 sigset_t sigset;
3313 size = ROUND_SIZE( base, size );
3314 base = ROUND_ADDR( base, page_mask );
3316 if (!count || !granularity) return STATUS_ACCESS_VIOLATION;
3317 if (!*count || !size) return STATUS_INVALID_PARAMETER;
3318 if (flags & ~WRITE_WATCH_FLAG_RESET) return STATUS_INVALID_PARAMETER;
3320 if (!addresses) return STATUS_ACCESS_VIOLATION;
3322 TRACE( "%p %x %p-%p %p %lu\n", process, flags, base, (char *)base + size,
3323 addresses, *count );
3325 server_enter_uninterrupted_section( &csVirtual, &sigset );
3327 if (is_write_watch_range( base, size ))
3329 ULONG_PTR pos = 0;
3330 char *addr = base;
3331 char *end = addr + size;
3333 while (pos < *count && addr < end)
3335 if (!(get_page_vprot( addr ) & VPROT_WRITEWATCH)) addresses[pos++] = addr;
3336 addr += page_size;
3338 if (flags & WRITE_WATCH_FLAG_RESET) reset_write_watches( base, addr - (char *)base );
3339 *count = pos;
3340 *granularity = page_size;
3342 else status = STATUS_INVALID_PARAMETER;
3344 server_leave_uninterrupted_section( &csVirtual, &sigset );
3345 return status;
3349 /***********************************************************************
3350 * NtResetWriteWatch (NTDLL.@)
3351 * ZwResetWriteWatch (NTDLL.@)
3353 NTSTATUS WINAPI NtResetWriteWatch( HANDLE process, PVOID base, SIZE_T size )
3355 NTSTATUS status = STATUS_SUCCESS;
3356 sigset_t sigset;
3358 size = ROUND_SIZE( base, size );
3359 base = ROUND_ADDR( base, page_mask );
3361 TRACE( "%p %p-%p\n", process, base, (char *)base + size );
3363 if (!size) return STATUS_INVALID_PARAMETER;
3365 server_enter_uninterrupted_section( &csVirtual, &sigset );
3367 if (is_write_watch_range( base, size ))
3368 reset_write_watches( base, size );
3369 else
3370 status = STATUS_INVALID_PARAMETER;
3372 server_leave_uninterrupted_section( &csVirtual, &sigset );
3373 return status;
3377 /***********************************************************************
3378 * NtReadVirtualMemory (NTDLL.@)
3379 * ZwReadVirtualMemory (NTDLL.@)
3381 NTSTATUS WINAPI NtReadVirtualMemory( HANDLE process, const void *addr, void *buffer,
3382 SIZE_T size, SIZE_T *bytes_read )
3384 NTSTATUS status;
3386 if (virtual_check_buffer_for_write( buffer, size ))
3388 SERVER_START_REQ( read_process_memory )
3390 req->handle = wine_server_obj_handle( process );
3391 req->addr = wine_server_client_ptr( addr );
3392 wine_server_set_reply( req, buffer, size );
3393 if ((status = wine_server_call( req ))) size = 0;
3395 SERVER_END_REQ;
3397 else
3399 status = STATUS_ACCESS_VIOLATION;
3400 size = 0;
3402 if (bytes_read) *bytes_read = size;
3403 return status;
3407 /***********************************************************************
3408 * NtWriteVirtualMemory (NTDLL.@)
3409 * ZwWriteVirtualMemory (NTDLL.@)
3411 NTSTATUS WINAPI NtWriteVirtualMemory( HANDLE process, void *addr, const void *buffer,
3412 SIZE_T size, SIZE_T *bytes_written )
3414 NTSTATUS status;
3416 if (virtual_check_buffer_for_read( buffer, size ))
3418 SERVER_START_REQ( write_process_memory )
3420 req->handle = wine_server_obj_handle( process );
3421 req->addr = wine_server_client_ptr( addr );
3422 wine_server_add_data( req, buffer, size );
3423 if ((status = wine_server_call( req ))) size = 0;
3425 SERVER_END_REQ;
3427 else
3429 status = STATUS_PARTIAL_COPY;
3430 size = 0;
3432 if (bytes_written) *bytes_written = size;
3433 return status;
3437 /***********************************************************************
3438 * NtAreMappedFilesTheSame (NTDLL.@)
3439 * ZwAreMappedFilesTheSame (NTDLL.@)
3441 NTSTATUS WINAPI NtAreMappedFilesTheSame(PVOID addr1, PVOID addr2)
3443 struct file_view *view1, *view2;
3444 NTSTATUS status;
3445 sigset_t sigset;
3447 TRACE("%p %p\n", addr1, addr2);
3449 server_enter_uninterrupted_section( &csVirtual, &sigset );
3451 view1 = VIRTUAL_FindView( addr1, 0 );
3452 view2 = VIRTUAL_FindView( addr2, 0 );
3454 if (!view1 || !view2)
3455 status = STATUS_INVALID_ADDRESS;
3456 else if (is_view_valloc( view1 ) || is_view_valloc( view2 ))
3457 status = STATUS_CONFLICTING_ADDRESSES;
3458 else if (view1 == view2)
3459 status = STATUS_SUCCESS;
3460 else if ((view1->protect & VPROT_SYSTEM) || (view2->protect & VPROT_SYSTEM))
3461 status = STATUS_NOT_SAME_DEVICE;
3462 else
3464 SERVER_START_REQ( is_same_mapping )
3466 req->base1 = wine_server_client_ptr( view1->base );
3467 req->base2 = wine_server_client_ptr( view2->base );
3468 status = wine_server_call( req );
3470 SERVER_END_REQ;
3473 server_leave_uninterrupted_section( &csVirtual, &sigset );
3474 return status;