ntdll: Support loading binaries that start inside the DOS area.
[wine.git] / dlls / ntdll / virtual.c
blobd0d25c7c3c9bc4552e9f52b83f98635daa0ae060
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_STAT_H
36 # include <sys/stat.h>
37 #endif
38 #ifdef HAVE_SYS_MMAN_H
39 # include <sys/mman.h>
40 #endif
41 #ifdef HAVE_SYS_SYSINFO_H
42 # include <sys/sysinfo.h>
43 #endif
44 #ifdef HAVE_VALGRIND_VALGRIND_H
45 # include <valgrind/valgrind.h>
46 #endif
48 #include "ntstatus.h"
49 #define WIN32_NO_STATUS
50 #define NONAMELESSUNION
51 #include "windef.h"
52 #include "winternl.h"
53 #include "wine/library.h"
54 #include "wine/server.h"
55 #include "wine/exception.h"
56 #include "wine/rbtree.h"
57 #include "wine/debug.h"
58 #include "ntdll_misc.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(virtual);
61 WINE_DECLARE_DEBUG_CHANNEL(module);
63 #ifndef MAP_NORESERVE
64 #define MAP_NORESERVE 0
65 #endif
67 /* File view */
68 struct file_view
70 struct wine_rb_entry entry; /* entry in global view tree */
71 void *base; /* base address */
72 size_t size; /* size in bytes */
73 unsigned int protect; /* protection for all pages at allocation time and SEC_* flags */
76 /* per-page protection flags */
77 #define VPROT_READ 0x01
78 #define VPROT_WRITE 0x02
79 #define VPROT_EXEC 0x04
80 #define VPROT_WRITECOPY 0x08
81 #define VPROT_GUARD 0x10
82 #define VPROT_COMMITTED 0x20
83 #define VPROT_WRITEWATCH 0x40
84 /* per-mapping protection flags */
85 #define VPROT_SYSTEM 0x0200 /* system view (underlying mmap not under our control) */
87 /* Conversion from VPROT_* to Win32 flags */
88 static const BYTE VIRTUAL_Win32Flags[16] =
90 PAGE_NOACCESS, /* 0 */
91 PAGE_READONLY, /* READ */
92 PAGE_READWRITE, /* WRITE */
93 PAGE_READWRITE, /* READ | WRITE */
94 PAGE_EXECUTE, /* EXEC */
95 PAGE_EXECUTE_READ, /* READ | EXEC */
96 PAGE_EXECUTE_READWRITE, /* WRITE | EXEC */
97 PAGE_EXECUTE_READWRITE, /* READ | WRITE | EXEC */
98 PAGE_WRITECOPY, /* WRITECOPY */
99 PAGE_WRITECOPY, /* READ | WRITECOPY */
100 PAGE_WRITECOPY, /* WRITE | WRITECOPY */
101 PAGE_WRITECOPY, /* READ | WRITE | WRITECOPY */
102 PAGE_EXECUTE_WRITECOPY, /* EXEC | WRITECOPY */
103 PAGE_EXECUTE_WRITECOPY, /* READ | EXEC | WRITECOPY */
104 PAGE_EXECUTE_WRITECOPY, /* WRITE | EXEC | WRITECOPY */
105 PAGE_EXECUTE_WRITECOPY /* READ | WRITE | EXEC | WRITECOPY */
108 static struct wine_rb_tree views_tree;
110 static RTL_CRITICAL_SECTION csVirtual;
111 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
113 0, 0, &csVirtual,
114 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
115 0, 0, { (DWORD_PTR)(__FILE__ ": csVirtual") }
117 static RTL_CRITICAL_SECTION csVirtual = { &critsect_debug, -1, 0, 0, 0, 0 };
119 #ifdef __i386__
120 static const UINT page_shift = 12;
121 static const UINT_PTR page_mask = 0xfff;
122 /* Note: these are Windows limits, you cannot change them. */
123 static void *address_space_limit = (void *)0xc0000000; /* top of the total available address space */
124 static void *user_space_limit = (void *)0x7fff0000; /* top of the user address space */
125 static void *working_set_limit = (void *)0x7fff0000; /* top of the current working set */
126 static void *address_space_start = (void *)0x110000; /* keep DOS area clear */
127 #elif defined(__x86_64__)
128 static const UINT page_shift = 12;
129 static const UINT_PTR page_mask = 0xfff;
130 static void *address_space_limit = (void *)0x7fffffff0000;
131 static void *user_space_limit = (void *)0x7fffffff0000;
132 static void *working_set_limit = (void *)0x7fffffff0000;
133 static void *address_space_start = (void *)0x10000;
134 #else
135 UINT_PTR page_size = 0;
136 static UINT page_shift;
137 static UINT_PTR page_mask;
138 static void *address_space_limit;
139 static void *user_space_limit;
140 static void *working_set_limit;
141 static void *address_space_start = (void *)0x10000;
142 #endif /* __i386__ */
143 static const BOOL is_win64 = (sizeof(void *) > sizeof(int));
145 #define ROUND_ADDR(addr,mask) \
146 ((void *)((UINT_PTR)(addr) & ~(UINT_PTR)(mask)))
148 #define ROUND_SIZE(addr,size) \
149 (((SIZE_T)(size) + ((UINT_PTR)(addr) & page_mask) + page_mask) & ~page_mask)
151 #define VIRTUAL_DEBUG_DUMP_VIEW(view) \
152 do { if (TRACE_ON(virtual)) VIRTUAL_DumpView(view); } while (0)
154 #ifdef _WIN64 /* on 64-bit the page protection bytes use a 2-level table */
155 static const size_t pages_vprot_shift = 20;
156 static const size_t pages_vprot_mask = (1 << 20) - 1;
157 static size_t pages_vprot_size;
158 static BYTE **pages_vprot;
159 #else /* on 32-bit we use a simple array with one byte per page */
160 static BYTE *pages_vprot;
161 #endif
163 static struct file_view *view_block_start, *view_block_end, *next_free_view;
164 static const size_t view_block_size = 0x100000;
165 static void *preload_reserve_start;
166 static void *preload_reserve_end;
167 static BOOL use_locks;
168 static BOOL force_exec_prot; /* whether to force PROT_EXEC on all PROT_READ mmaps */
170 static inline int is_view_valloc( const struct file_view *view )
172 return !(view->protect & (SEC_FILE | SEC_RESERVE | SEC_COMMIT));
175 /***********************************************************************
176 * get_page_vprot
178 * Return the page protection byte.
180 static BYTE get_page_vprot( const void *addr )
182 size_t idx = (size_t)addr >> page_shift;
184 #ifdef _WIN64
185 if (!pages_vprot[idx >> pages_vprot_shift]) return 0;
186 return pages_vprot[idx >> pages_vprot_shift][idx & pages_vprot_mask];
187 #else
188 return pages_vprot[idx];
189 #endif
193 /***********************************************************************
194 * set_page_vprot
196 * Set a range of page protection bytes.
198 static void set_page_vprot( const void *addr, size_t size, BYTE vprot )
200 size_t idx = (size_t)addr >> page_shift;
201 size_t end = ((size_t)addr + size + page_mask) >> page_shift;
203 #ifdef _WIN64
204 while (idx >> pages_vprot_shift != end >> pages_vprot_shift)
206 size_t dir_size = pages_vprot_mask + 1 - (idx & pages_vprot_mask);
207 memset( pages_vprot[idx >> pages_vprot_shift] + (idx & pages_vprot_mask), vprot, dir_size );
208 idx += dir_size;
210 memset( pages_vprot[idx >> pages_vprot_shift] + (idx & pages_vprot_mask), vprot, end - idx );
211 #else
212 memset( pages_vprot + idx, vprot, end - idx );
213 #endif
217 /***********************************************************************
218 * set_page_vprot_bits
220 * Set or clear bits in a range of page protection bytes.
222 static void set_page_vprot_bits( const void *addr, size_t size, BYTE set, BYTE clear )
224 size_t idx = (size_t)addr >> page_shift;
225 size_t end = ((size_t)addr + size + page_mask) >> page_shift;
227 #ifdef _WIN64
228 for ( ; idx < end; idx++)
230 BYTE *ptr = pages_vprot[idx >> pages_vprot_shift] + (idx & pages_vprot_mask);
231 *ptr = (*ptr & ~clear) | set;
233 #else
234 for ( ; idx < end; idx++) pages_vprot[idx] = (pages_vprot[idx] & ~clear) | set;
235 #endif
239 /***********************************************************************
240 * alloc_pages_vprot
242 * Allocate the page protection bytes for a given range.
244 static BOOL alloc_pages_vprot( const void *addr, size_t size )
246 #ifdef _WIN64
247 size_t idx = (size_t)addr >> page_shift;
248 size_t end = ((size_t)addr + size + page_mask) >> page_shift;
249 size_t i;
250 void *ptr;
252 assert( end <= pages_vprot_size << pages_vprot_shift );
253 for (i = idx >> pages_vprot_shift; i < (end + pages_vprot_mask) >> pages_vprot_shift; i++)
255 if (pages_vprot[i]) continue;
256 if ((ptr = wine_anon_mmap( NULL, pages_vprot_mask + 1, PROT_READ | PROT_WRITE, 0 )) == (void *)-1)
257 return FALSE;
258 pages_vprot[i] = ptr;
260 #endif
261 return TRUE;
265 /***********************************************************************
266 * compare_view
268 * View comparison function used for the rb tree.
270 static int compare_view( const void *addr, const struct wine_rb_entry *entry )
272 struct file_view *view = WINE_RB_ENTRY_VALUE( entry, struct file_view, entry );
274 if (addr < view->base) return -1;
275 if (addr > view->base) return 1;
276 return 0;
280 /***********************************************************************
281 * VIRTUAL_GetProtStr
283 static const char *VIRTUAL_GetProtStr( BYTE prot )
285 static char buffer[6];
286 buffer[0] = (prot & VPROT_COMMITTED) ? 'c' : '-';
287 buffer[1] = (prot & VPROT_GUARD) ? 'g' : ((prot & VPROT_WRITEWATCH) ? 'H' : '-');
288 buffer[2] = (prot & VPROT_READ) ? 'r' : '-';
289 buffer[3] = (prot & VPROT_WRITECOPY) ? 'W' : ((prot & VPROT_WRITE) ? 'w' : '-');
290 buffer[4] = (prot & VPROT_EXEC) ? 'x' : '-';
291 buffer[5] = 0;
292 return buffer;
296 /***********************************************************************
297 * VIRTUAL_GetUnixProt
299 * Convert page protections to protection for mmap/mprotect.
301 static int VIRTUAL_GetUnixProt( BYTE vprot )
303 int prot = 0;
304 if ((vprot & VPROT_COMMITTED) && !(vprot & VPROT_GUARD))
306 if (vprot & VPROT_READ) prot |= PROT_READ;
307 if (vprot & VPROT_WRITE) prot |= PROT_WRITE | PROT_READ;
308 if (vprot & VPROT_WRITECOPY) prot |= PROT_WRITE | PROT_READ;
309 if (vprot & VPROT_EXEC) prot |= PROT_EXEC | PROT_READ;
310 if (vprot & VPROT_WRITEWATCH) prot &= ~PROT_WRITE;
312 if (!prot) prot = PROT_NONE;
313 return prot;
317 /***********************************************************************
318 * VIRTUAL_DumpView
320 static void VIRTUAL_DumpView( struct file_view *view )
322 UINT i, count;
323 char *addr = view->base;
324 BYTE prot = get_page_vprot( addr );
326 TRACE( "View: %p - %p", addr, addr + view->size - 1 );
327 if (view->protect & VPROT_SYSTEM)
328 TRACE( " (builtin image)\n" );
329 else if (view->protect & SEC_IMAGE)
330 TRACE( " (image)\n" );
331 else if (view->protect & SEC_FILE)
332 TRACE( " (file)\n" );
333 else if (view->protect & (SEC_RESERVE | SEC_COMMIT))
334 TRACE( " (anonymous)\n" );
335 else
336 TRACE( " (valloc)\n");
338 for (count = i = 1; i < view->size >> page_shift; i++, count++)
340 BYTE next = get_page_vprot( addr + (count << page_shift) );
341 if (next == prot) continue;
342 TRACE( " %p - %p %s\n",
343 addr, addr + (count << page_shift) - 1, VIRTUAL_GetProtStr(prot) );
344 addr += (count << page_shift);
345 prot = next;
346 count = 0;
348 if (count)
349 TRACE( " %p - %p %s\n",
350 addr, addr + (count << page_shift) - 1, VIRTUAL_GetProtStr(prot) );
354 /***********************************************************************
355 * VIRTUAL_Dump
357 #ifdef WINE_VM_DEBUG
358 static void VIRTUAL_Dump(void)
360 sigset_t sigset;
361 struct file_view *view;
363 TRACE( "Dump of all virtual memory views:\n" );
364 server_enter_uninterrupted_section( &csVirtual, &sigset );
365 WINE_RB_FOR_EACH_ENTRY( view, &views_tree, struct file_view, entry )
367 VIRTUAL_DumpView( view );
369 server_leave_uninterrupted_section( &csVirtual, &sigset );
371 #endif
374 /***********************************************************************
375 * VIRTUAL_FindView
377 * Find the view containing a given address. The csVirtual section must be held by caller.
379 * PARAMS
380 * addr [I] Address
382 * RETURNS
383 * View: Success
384 * NULL: Failure
386 static struct file_view *VIRTUAL_FindView( const void *addr, size_t size )
388 struct wine_rb_entry *ptr = views_tree.root;
390 if ((const char *)addr + size < (const char *)addr) return NULL; /* overflow */
392 while (ptr)
394 struct file_view *view = WINE_RB_ENTRY_VALUE( ptr, struct file_view, entry );
396 if (view->base > addr) ptr = ptr->left;
397 else if ((const char *)view->base + view->size <= (const char *)addr) ptr = ptr->right;
398 else if ((const char *)view->base + view->size < (const char *)addr + size) break; /* size too large */
399 else return view;
401 return NULL;
405 /***********************************************************************
406 * get_mask
408 static inline UINT_PTR get_mask( ULONG zero_bits )
410 if (!zero_bits) return 0xffff; /* allocations are aligned to 64K by default */
411 if (zero_bits < page_shift) zero_bits = page_shift;
412 if (zero_bits > 21) return 0;
413 return (1 << zero_bits) - 1;
417 /***********************************************************************
418 * is_write_watch_range
420 static inline BOOL is_write_watch_range( const void *addr, size_t size )
422 struct file_view *view = VIRTUAL_FindView( addr, size );
423 return view && (view->protect & VPROT_WRITEWATCH);
427 /***********************************************************************
428 * find_view_range
430 * Find the first view overlapping at least part of the specified range.
431 * The csVirtual section must be held by caller.
433 static struct file_view *find_view_range( const void *addr, size_t size )
435 struct wine_rb_entry *ptr = views_tree.root;
437 while (ptr)
439 struct file_view *view = WINE_RB_ENTRY_VALUE( ptr, struct file_view, entry );
441 if ((const char *)view->base >= (const char *)addr + size) ptr = ptr->left;
442 else if ((const char *)view->base + view->size <= (const char *)addr) ptr = ptr->right;
443 else return view;
445 return NULL;
449 /***********************************************************************
450 * find_free_area
452 * Find a free area between views inside the specified range.
453 * The csVirtual section must be held by caller.
455 static void *find_free_area( void *base, void *end, size_t size, size_t mask, int top_down )
457 struct wine_rb_entry *first = NULL, *ptr = views_tree.root;
458 void *start;
460 /* find the first (resp. last) view inside the range */
461 while (ptr)
463 struct file_view *view = WINE_RB_ENTRY_VALUE( ptr, struct file_view, entry );
464 if ((char *)view->base + view->size >= (char *)end)
466 end = min( end, view->base );
467 ptr = ptr->left;
469 else if (view->base <= base)
471 base = max( (char *)base, (char *)view->base + view->size );
472 ptr = ptr->right;
474 else
476 first = ptr;
477 ptr = top_down ? ptr->right : ptr->left;
481 if (top_down)
483 start = ROUND_ADDR( (char *)end - size, mask );
484 if (start >= end || start < base) return NULL;
486 while (first)
488 struct file_view *view = WINE_RB_ENTRY_VALUE( first, struct file_view, entry );
490 if ((char *)view->base + view->size <= (char *)start) break;
491 start = ROUND_ADDR( (char *)view->base - size, mask );
492 /* stop if remaining space is not large enough */
493 if (!start || start >= end || start < base) return NULL;
494 first = wine_rb_prev( first );
497 else
499 start = ROUND_ADDR( (char *)base + mask, mask );
500 if (!start || start >= end || (char *)end - (char *)start < size) return NULL;
502 while (first)
504 struct file_view *view = WINE_RB_ENTRY_VALUE( first, struct file_view, entry );
506 if ((char *)view->base >= (char *)start + size) break;
507 start = ROUND_ADDR( (char *)view->base + view->size + mask, mask );
508 /* stop if remaining space is not large enough */
509 if (!start || start >= end || (char *)end - (char *)start < size) return NULL;
510 first = wine_rb_next( first );
513 return start;
517 /***********************************************************************
518 * add_reserved_area
520 * Add a reserved area to the list maintained by libwine.
521 * The csVirtual section must be held by caller.
523 static void add_reserved_area( void *addr, size_t size )
525 TRACE( "adding %p-%p\n", addr, (char *)addr + size );
527 if (addr < user_space_limit)
529 /* unmap the part of the area that is below the limit */
530 assert( (char *)addr + size > (char *)user_space_limit );
531 munmap( addr, (char *)user_space_limit - (char *)addr );
532 size -= (char *)user_space_limit - (char *)addr;
533 addr = user_space_limit;
535 /* blow away existing mappings */
536 wine_anon_mmap( addr, size, PROT_NONE, MAP_NORESERVE | MAP_FIXED );
537 wine_mmap_add_reserved_area( addr, size );
541 /***********************************************************************
542 * remove_reserved_area
544 * Remove a reserved area from the list maintained by libwine.
545 * The csVirtual section must be held by caller.
547 static void remove_reserved_area( void *addr, size_t size )
549 struct file_view *view;
551 TRACE( "removing %p-%p\n", addr, (char *)addr + size );
552 wine_mmap_remove_reserved_area( addr, size, 0 );
554 /* unmap areas not covered by an existing view */
555 WINE_RB_FOR_EACH_ENTRY( view, &views_tree, struct file_view, entry )
557 if ((char *)view->base >= (char *)addr + size) break;
558 if ((char *)view->base + view->size <= (char *)addr) continue;
559 if (view->base > addr) munmap( addr, (char *)view->base - (char *)addr );
560 if ((char *)view->base + view->size > (char *)addr + size) return;
561 size = (char *)addr + size - ((char *)view->base + view->size);
562 addr = (char *)view->base + view->size;
564 munmap( addr, size );
568 struct area_boundary
570 void *base;
571 size_t size;
572 void *boundary;
575 /***********************************************************************
576 * get_area_boundary_callback
578 * Get lowest boundary address between reserved area and non-reserved area
579 * in the specified region. If no boundaries are found, result is NULL.
580 * The csVirtual section must be held by caller.
582 static int get_area_boundary_callback( void *start, size_t size, void *arg )
584 struct area_boundary *area = arg;
585 void *end = (char *)start + size;
587 area->boundary = NULL;
588 if (area->base >= end) return 0;
589 if ((char *)start >= (char *)area->base + area->size) return 1;
590 if (area->base >= start)
592 if ((char *)area->base + area->size > (char *)end)
594 area->boundary = end;
595 return 1;
597 return 0;
599 area->boundary = start;
600 return 1;
604 /***********************************************************************
605 * is_beyond_limit
607 * Check if an address range goes beyond a given limit.
609 static inline BOOL is_beyond_limit( const void *addr, size_t size, const void *limit )
611 return (addr >= limit || (const char *)addr + size > (const char *)limit);
615 /***********************************************************************
616 * unmap_area
618 * Unmap an area, or simply replace it by an empty mapping if it is
619 * in a reserved area. The csVirtual section must be held by caller.
621 static inline void unmap_area( void *addr, size_t size )
623 switch (wine_mmap_is_in_reserved_area( addr, size ))
625 case -1: /* partially in a reserved area */
627 struct area_boundary area;
628 size_t lower_size;
629 area.base = addr;
630 area.size = size;
631 wine_mmap_enum_reserved_areas( get_area_boundary_callback, &area, 0 );
632 assert( area.boundary );
633 lower_size = (char *)area.boundary - (char *)addr;
634 unmap_area( addr, lower_size );
635 unmap_area( area.boundary, size - lower_size );
636 break;
638 case 1: /* in a reserved area */
639 wine_anon_mmap( addr, size, PROT_NONE, MAP_NORESERVE | MAP_FIXED );
640 break;
641 default:
642 case 0: /* not in a reserved area */
643 if (is_beyond_limit( addr, size, user_space_limit ))
644 add_reserved_area( addr, size );
645 else
646 munmap( addr, size );
647 break;
652 /***********************************************************************
653 * alloc_view
655 * Allocate a new view. The csVirtual section must be held by caller.
657 static struct file_view *alloc_view(void)
659 if (next_free_view)
661 struct file_view *ret = next_free_view;
662 next_free_view = *(struct file_view **)ret;
663 return ret;
665 if (view_block_start == view_block_end)
667 void *ptr = wine_anon_mmap( NULL, view_block_size, PROT_READ | PROT_WRITE, 0 );
668 if (ptr == (void *)-1) return NULL;
669 view_block_start = ptr;
670 view_block_end = view_block_start + view_block_size / sizeof(*view_block_start);
672 return view_block_start++;
676 /***********************************************************************
677 * delete_view
679 * Deletes a view. The csVirtual section must be held by caller.
681 static void delete_view( struct file_view *view ) /* [in] View */
683 if (!(view->protect & VPROT_SYSTEM)) unmap_area( view->base, view->size );
684 set_page_vprot( view->base, view->size, 0 );
685 wine_rb_remove( &views_tree, &view->entry );
686 *(struct file_view **)view = next_free_view;
687 next_free_view = view;
691 /***********************************************************************
692 * create_view
694 * Create a view. The csVirtual section must be held by caller.
696 static NTSTATUS create_view( struct file_view **view_ret, void *base, size_t size, unsigned int vprot )
698 struct file_view *view;
699 int unix_prot = VIRTUAL_GetUnixProt( vprot );
701 assert( !((UINT_PTR)base & page_mask) );
702 assert( !(size & page_mask) );
704 /* Check for overlapping views. This can happen if the previous view
705 * was a system view that got unmapped behind our back. In that case
706 * we recover by simply deleting it. */
708 while ((view = find_view_range( base, size )))
710 TRACE( "overlapping view %p-%p for %p-%p\n",
711 view->base, (char *)view->base + view->size, base, (char *)base + size );
712 assert( view->protect & VPROT_SYSTEM );
713 delete_view( view );
716 if (!alloc_pages_vprot( base, size )) return STATUS_NO_MEMORY;
718 /* Create the view structure */
720 if (!(view = alloc_view()))
722 FIXME( "out of memory for %p-%p\n", base, (char *)base + size );
723 return STATUS_NO_MEMORY;
726 view->base = base;
727 view->size = size;
728 view->protect = vprot;
729 set_page_vprot( base, size, vprot );
731 wine_rb_put( &views_tree, view->base, &view->entry );
733 *view_ret = view;
735 if (force_exec_prot && (unix_prot & PROT_READ) && !(unix_prot & PROT_EXEC))
737 TRACE( "forcing exec permission on %p-%p\n", base, (char *)base + size - 1 );
738 mprotect( base, size, unix_prot | PROT_EXEC );
740 return STATUS_SUCCESS;
744 /***********************************************************************
745 * VIRTUAL_GetWin32Prot
747 * Convert page protections to Win32 flags.
749 static DWORD VIRTUAL_GetWin32Prot( BYTE vprot, unsigned int map_prot )
751 DWORD ret = VIRTUAL_Win32Flags[vprot & 0x0f];
752 if (vprot & VPROT_GUARD) ret |= PAGE_GUARD;
753 if (map_prot & SEC_NOCACHE) ret |= PAGE_NOCACHE;
754 return ret;
758 /***********************************************************************
759 * get_vprot_flags
761 * Build page protections from Win32 flags.
763 * PARAMS
764 * protect [I] Win32 protection flags
766 * RETURNS
767 * Value of page protection flags
769 static NTSTATUS get_vprot_flags( DWORD protect, unsigned int *vprot, BOOL image )
771 switch(protect & 0xff)
773 case PAGE_READONLY:
774 *vprot = VPROT_READ;
775 break;
776 case PAGE_READWRITE:
777 if (image)
778 *vprot = VPROT_READ | VPROT_WRITECOPY;
779 else
780 *vprot = VPROT_READ | VPROT_WRITE;
781 break;
782 case PAGE_WRITECOPY:
783 *vprot = VPROT_READ | VPROT_WRITECOPY;
784 break;
785 case PAGE_EXECUTE:
786 *vprot = VPROT_EXEC;
787 break;
788 case PAGE_EXECUTE_READ:
789 *vprot = VPROT_EXEC | VPROT_READ;
790 break;
791 case PAGE_EXECUTE_READWRITE:
792 if (image)
793 *vprot = VPROT_EXEC | VPROT_READ | VPROT_WRITECOPY;
794 else
795 *vprot = VPROT_EXEC | VPROT_READ | VPROT_WRITE;
796 break;
797 case PAGE_EXECUTE_WRITECOPY:
798 *vprot = VPROT_EXEC | VPROT_READ | VPROT_WRITECOPY;
799 break;
800 case PAGE_NOACCESS:
801 *vprot = 0;
802 break;
803 default:
804 return STATUS_INVALID_PAGE_PROTECTION;
806 if (protect & PAGE_GUARD) *vprot |= VPROT_GUARD;
807 return STATUS_SUCCESS;
811 /***********************************************************************
812 * mprotect_exec
814 * Wrapper for mprotect, adds PROT_EXEC if forced by force_exec_prot
816 static inline int mprotect_exec( void *base, size_t size, int unix_prot )
818 if (force_exec_prot && (unix_prot & PROT_READ) && !(unix_prot & PROT_EXEC))
820 TRACE( "forcing exec permission on %p-%p\n", base, (char *)base + size - 1 );
821 if (!mprotect( base, size, unix_prot | PROT_EXEC )) return 0;
822 /* exec + write may legitimately fail, in that case fall back to write only */
823 if (!(unix_prot & PROT_WRITE)) return -1;
826 return mprotect( base, size, unix_prot );
830 /***********************************************************************
831 * mprotect_range
833 * Call mprotect on a page range, applying the protections from the per-page byte.
835 static void mprotect_range( void *base, size_t size, BYTE set, BYTE clear )
837 size_t i, count;
838 char *addr = ROUND_ADDR( base, page_mask );
839 int prot, next;
841 size = ROUND_SIZE( base, size );
842 prot = VIRTUAL_GetUnixProt( (get_page_vprot( addr ) & ~clear ) | set );
843 for (count = i = 1; i < size >> page_shift; i++, count++)
845 next = VIRTUAL_GetUnixProt( (get_page_vprot( addr + (count << page_shift) ) & ~clear) | set );
846 if (next == prot) continue;
847 mprotect_exec( addr, count << page_shift, prot );
848 addr += count << page_shift;
849 prot = next;
850 count = 0;
852 if (count) mprotect_exec( addr, count << page_shift, prot );
856 /***********************************************************************
857 * VIRTUAL_SetProt
859 * Change the protection of a range of pages.
861 * RETURNS
862 * TRUE: Success
863 * FALSE: Failure
865 static BOOL VIRTUAL_SetProt( struct file_view *view, /* [in] Pointer to view */
866 void *base, /* [in] Starting address */
867 size_t size, /* [in] Size in bytes */
868 BYTE vprot ) /* [in] Protections to use */
870 int unix_prot = VIRTUAL_GetUnixProt(vprot);
872 if (view->protect & VPROT_WRITEWATCH)
874 /* each page may need different protections depending on write watch flag */
875 set_page_vprot_bits( base, size, vprot & ~VPROT_WRITEWATCH, ~vprot & ~VPROT_WRITEWATCH );
876 mprotect_range( base, size, 0, 0 );
877 return TRUE;
880 /* if setting stack guard pages, store the permissions first, as the guard may be
881 * triggered at any point after mprotect and change the permissions again */
882 if ((vprot & VPROT_GUARD) &&
883 (base >= NtCurrentTeb()->DeallocationStack) &&
884 (base < NtCurrentTeb()->Tib.StackBase))
886 set_page_vprot( base, size, vprot );
887 mprotect( base, size, unix_prot );
888 return TRUE;
891 if (mprotect_exec( base, size, unix_prot )) /* FIXME: last error */
892 return FALSE;
894 set_page_vprot( base, size, vprot );
895 return TRUE;
899 /***********************************************************************
900 * set_protection
902 * Set page protections on a range of pages
904 static NTSTATUS set_protection( struct file_view *view, void *base, SIZE_T size, ULONG protect )
906 unsigned int vprot;
907 NTSTATUS status;
909 if ((status = get_vprot_flags( protect, &vprot, view->protect & SEC_IMAGE ))) return status;
910 if (is_view_valloc( view ))
912 if (vprot & VPROT_WRITECOPY) return STATUS_INVALID_PAGE_PROTECTION;
914 else
916 BYTE access = vprot & (VPROT_READ | VPROT_WRITE | VPROT_EXEC);
917 if ((view->protect & access) != access) return STATUS_INVALID_PAGE_PROTECTION;
920 if (!VIRTUAL_SetProt( view, base, size, vprot | VPROT_COMMITTED )) return STATUS_ACCESS_DENIED;
921 return STATUS_SUCCESS;
925 /***********************************************************************
926 * update_write_watches
928 static void update_write_watches( void *base, size_t size, size_t accessed_size )
930 TRACE( "updating watch %p-%p-%p\n", base, (char *)base + accessed_size, (char *)base + size );
931 /* clear write watch flag on accessed pages */
932 set_page_vprot_bits( base, accessed_size, 0, VPROT_WRITEWATCH );
933 /* restore page protections on the entire range */
934 mprotect_range( base, size, 0, 0 );
938 /***********************************************************************
939 * reset_write_watches
941 * Reset write watches in a memory range.
943 static void reset_write_watches( void *base, SIZE_T size )
945 set_page_vprot_bits( base, size, VPROT_WRITEWATCH, 0 );
946 mprotect_range( base, size, 0, 0 );
950 /***********************************************************************
951 * unmap_extra_space
953 * Release the extra memory while keeping the range starting on the granularity boundary.
955 static inline void *unmap_extra_space( void *ptr, size_t total_size, size_t wanted_size, size_t mask )
957 if ((ULONG_PTR)ptr & mask)
959 size_t extra = mask + 1 - ((ULONG_PTR)ptr & mask);
960 munmap( ptr, extra );
961 ptr = (char *)ptr + extra;
962 total_size -= extra;
964 if (total_size > wanted_size)
965 munmap( (char *)ptr + wanted_size, total_size - wanted_size );
966 return ptr;
970 struct alloc_area
972 size_t size;
973 size_t mask;
974 int top_down;
975 void *limit;
976 void *result;
979 /***********************************************************************
980 * alloc_reserved_area_callback
982 * Try to map some space inside a reserved area. Callback for wine_mmap_enum_reserved_areas.
984 static int alloc_reserved_area_callback( void *start, size_t size, void *arg )
986 struct alloc_area *alloc = arg;
987 void *end = (char *)start + size;
989 if (start < address_space_start) start = address_space_start;
990 if (is_beyond_limit( start, size, alloc->limit )) end = alloc->limit;
991 if (start >= end) return 0;
993 /* make sure we don't touch the preloader reserved range */
994 if (preload_reserve_end >= start)
996 if (preload_reserve_end >= end)
998 if (preload_reserve_start <= start) return 0; /* no space in that area */
999 if (preload_reserve_start < end) end = preload_reserve_start;
1001 else if (preload_reserve_start <= start) start = preload_reserve_end;
1002 else
1004 /* range is split in two by the preloader reservation, try first part */
1005 if ((alloc->result = find_free_area( start, preload_reserve_start, alloc->size,
1006 alloc->mask, alloc->top_down )))
1007 return 1;
1008 /* then fall through to try second part */
1009 start = preload_reserve_end;
1012 if ((alloc->result = find_free_area( start, end, alloc->size, alloc->mask, alloc->top_down )))
1013 return 1;
1015 return 0;
1018 /***********************************************************************
1019 * map_fixed_area
1021 * mmap the fixed memory area.
1022 * The csVirtual section must be held by caller.
1024 static NTSTATUS map_fixed_area( void *base, size_t size, unsigned int vprot )
1026 void *ptr;
1028 switch (wine_mmap_is_in_reserved_area( base, size ))
1030 case -1: /* partially in a reserved area */
1032 NTSTATUS status;
1033 struct area_boundary area;
1034 size_t lower_size;
1035 area.base = base;
1036 area.size = size;
1037 wine_mmap_enum_reserved_areas( get_area_boundary_callback, &area, 0 );
1038 assert( area.boundary );
1039 lower_size = (char *)area.boundary - (char *)base;
1040 status = map_fixed_area( base, lower_size, vprot );
1041 if (status == STATUS_SUCCESS)
1043 status = map_fixed_area( area.boundary, size - lower_size, vprot);
1044 if (status != STATUS_SUCCESS) unmap_area( base, lower_size );
1046 return status;
1048 case 0: /* not in a reserved area, do a normal allocation */
1049 if ((ptr = wine_anon_mmap( base, size, VIRTUAL_GetUnixProt(vprot), 0 )) == (void *)-1)
1051 if (errno == ENOMEM) return STATUS_NO_MEMORY;
1052 return STATUS_INVALID_PARAMETER;
1054 if (ptr != base)
1056 /* We couldn't get the address we wanted */
1057 if (is_beyond_limit( ptr, size, user_space_limit )) add_reserved_area( ptr, size );
1058 else munmap( ptr, size );
1059 return STATUS_CONFLICTING_ADDRESSES;
1061 break;
1063 default:
1064 case 1: /* in a reserved area, make sure the address is available */
1065 if (find_view_range( base, size )) return STATUS_CONFLICTING_ADDRESSES;
1066 /* replace the reserved area by our mapping */
1067 if ((ptr = wine_anon_mmap( base, size, VIRTUAL_GetUnixProt(vprot), MAP_FIXED )) != base)
1068 return STATUS_INVALID_PARAMETER;
1069 break;
1071 if (is_beyond_limit( ptr, size, working_set_limit )) working_set_limit = address_space_limit;
1072 return STATUS_SUCCESS;
1075 /***********************************************************************
1076 * map_view
1078 * Create a view and mmap the corresponding memory area.
1079 * The csVirtual section must be held by caller.
1081 static NTSTATUS map_view( struct file_view **view_ret, void *base, size_t size, size_t mask,
1082 int top_down, unsigned int vprot )
1084 void *ptr;
1085 NTSTATUS status;
1087 if (base)
1089 if (is_beyond_limit( base, size, address_space_limit ))
1090 return STATUS_WORKING_SET_LIMIT_RANGE;
1091 status = map_fixed_area( base, size, vprot );
1092 if (status != STATUS_SUCCESS) return status;
1093 ptr = base;
1095 else
1097 size_t view_size = size + mask + 1;
1098 struct alloc_area alloc;
1100 alloc.size = size;
1101 alloc.mask = mask;
1102 alloc.top_down = top_down;
1103 alloc.limit = user_space_limit;
1104 if (wine_mmap_enum_reserved_areas( alloc_reserved_area_callback, &alloc, top_down ))
1106 ptr = alloc.result;
1107 TRACE( "got mem in reserved area %p-%p\n", ptr, (char *)ptr + size );
1108 if (wine_anon_mmap( ptr, size, VIRTUAL_GetUnixProt(vprot), MAP_FIXED ) != ptr)
1109 return STATUS_INVALID_PARAMETER;
1110 goto done;
1113 for (;;)
1115 if ((ptr = wine_anon_mmap( NULL, view_size, VIRTUAL_GetUnixProt(vprot), 0 )) == (void *)-1)
1117 if (errno == ENOMEM) return STATUS_NO_MEMORY;
1118 return STATUS_INVALID_PARAMETER;
1120 TRACE( "got mem with anon mmap %p-%p\n", ptr, (char *)ptr + size );
1121 /* if we got something beyond the user limit, unmap it and retry */
1122 if (is_beyond_limit( ptr, view_size, user_space_limit )) add_reserved_area( ptr, view_size );
1123 else break;
1125 ptr = unmap_extra_space( ptr, view_size, size, mask );
1127 done:
1128 status = create_view( view_ret, ptr, size, vprot );
1129 if (status != STATUS_SUCCESS) unmap_area( ptr, size );
1130 return status;
1134 /***********************************************************************
1135 * map_file_into_view
1137 * Wrapper for mmap() to map a file into a view, falling back to read if mmap fails.
1138 * The csVirtual section must be held by caller.
1140 static NTSTATUS map_file_into_view( struct file_view *view, int fd, size_t start, size_t size,
1141 off_t offset, unsigned int vprot, BOOL removable )
1143 void *ptr;
1144 int prot = VIRTUAL_GetUnixProt( vprot | VPROT_COMMITTED /* make sure it is accessible */ );
1145 unsigned int flags = MAP_FIXED | ((vprot & VPROT_WRITECOPY) ? MAP_PRIVATE : MAP_SHARED);
1147 assert( start < view->size );
1148 assert( start + size <= view->size );
1150 if (force_exec_prot && (vprot & VPROT_READ))
1152 TRACE( "forcing exec permission on mapping %p-%p\n",
1153 (char *)view->base + start, (char *)view->base + start + size - 1 );
1154 prot |= PROT_EXEC;
1157 /* only try mmap if media is not removable (or if we require write access) */
1158 if (!removable || (flags & MAP_SHARED))
1160 if (mmap( (char *)view->base + start, size, prot, flags, fd, offset ) != (void *)-1)
1161 goto done;
1163 if ((errno == EPERM) && (prot & PROT_EXEC))
1164 ERR( "failed to set %08x protection on file map, noexec filesystem?\n", prot );
1166 /* mmap() failed; if this is because the file offset is not */
1167 /* page-aligned (EINVAL), or because the underlying filesystem */
1168 /* does not support mmap() (ENOEXEC,ENODEV), we do it by hand. */
1169 if ((errno != ENOEXEC) && (errno != EINVAL) && (errno != ENODEV)) return FILE_GetNtStatus();
1170 if (flags & MAP_SHARED) /* we cannot fake shared mappings */
1172 if (errno == EINVAL) return STATUS_INVALID_PARAMETER;
1173 ERR( "shared writable mmap not supported, broken filesystem?\n" );
1174 return STATUS_NOT_SUPPORTED;
1178 /* Reserve the memory with an anonymous mmap */
1179 ptr = wine_anon_mmap( (char *)view->base + start, size, PROT_READ | PROT_WRITE, MAP_FIXED );
1180 if (ptr == (void *)-1) return FILE_GetNtStatus();
1181 /* Now read in the file */
1182 pread( fd, ptr, size, offset );
1183 if (prot != (PROT_READ|PROT_WRITE)) mprotect( ptr, size, prot ); /* Set the right protection */
1184 done:
1185 set_page_vprot( (char *)view->base + start, size, vprot );
1186 return STATUS_SUCCESS;
1190 /***********************************************************************
1191 * get_committed_size
1193 * Get the size of the committed range starting at base.
1194 * Also return the protections for the first page.
1196 static SIZE_T get_committed_size( struct file_view *view, void *base, BYTE *vprot )
1198 SIZE_T i, start;
1200 start = ((char *)base - (char *)view->base) >> page_shift;
1201 *vprot = get_page_vprot( base );
1203 if (view->protect & SEC_RESERVE)
1205 SIZE_T ret = 0;
1206 SERVER_START_REQ( get_mapping_committed_range )
1208 req->base = wine_server_client_ptr( view->base );
1209 req->offset = start << page_shift;
1210 if (!wine_server_call( req ))
1212 ret = reply->size;
1213 if (reply->committed)
1215 *vprot |= VPROT_COMMITTED;
1216 set_page_vprot_bits( base, ret, VPROT_COMMITTED, 0 );
1220 SERVER_END_REQ;
1221 return ret;
1223 for (i = start + 1; i < view->size >> page_shift; i++)
1224 if ((*vprot ^ get_page_vprot( (char *)view->base + (i << page_shift) )) & VPROT_COMMITTED) break;
1225 return (i - start) << page_shift;
1229 /***********************************************************************
1230 * decommit_view
1232 * Decommit some pages of a given view.
1233 * The csVirtual section must be held by caller.
1235 static NTSTATUS decommit_pages( struct file_view *view, size_t start, size_t size )
1237 if (wine_anon_mmap( (char *)view->base + start, size, PROT_NONE, MAP_FIXED ) != (void *)-1)
1239 set_page_vprot_bits( (char *)view->base + start, size, 0, VPROT_COMMITTED );
1240 return STATUS_SUCCESS;
1242 return FILE_GetNtStatus();
1246 /***********************************************************************
1247 * allocate_dos_memory
1249 * Allocate the DOS memory range.
1251 static NTSTATUS allocate_dos_memory( struct file_view **view, unsigned int vprot )
1253 size_t size;
1254 void *addr = NULL;
1255 void * const low_64k = (void *)0x10000;
1256 const size_t dosmem_size = 0x110000;
1257 int unix_prot = VIRTUAL_GetUnixProt( vprot );
1259 /* check for existing view */
1261 if (find_view_range( 0, dosmem_size )) return STATUS_CONFLICTING_ADDRESSES;
1263 /* check without the first 64K */
1265 if (wine_mmap_is_in_reserved_area( low_64k, dosmem_size - 0x10000 ) != 1)
1267 addr = wine_anon_mmap( low_64k, dosmem_size - 0x10000, unix_prot, 0 );
1268 if (addr != low_64k)
1270 if (addr != (void *)-1) munmap( addr, dosmem_size - 0x10000 );
1271 return map_view( view, NULL, dosmem_size, 0xffff, 0, vprot );
1275 /* now try to allocate the low 64K too */
1277 if (wine_mmap_is_in_reserved_area( NULL, 0x10000 ) != 1)
1279 addr = wine_anon_mmap( (void *)page_size, 0x10000 - page_size, unix_prot, 0 );
1280 if (addr == (void *)page_size)
1282 if (!wine_anon_mmap( NULL, page_size, unix_prot, MAP_FIXED ))
1284 addr = NULL;
1285 TRACE( "successfully mapped low 64K range\n" );
1287 else TRACE( "failed to map page 0\n" );
1289 else
1291 if (addr != (void *)-1) munmap( addr, 0x10000 - page_size );
1292 addr = low_64k;
1293 TRACE( "failed to map low 64K range\n" );
1297 /* now reserve the whole range */
1299 size = (char *)dosmem_size - (char *)addr;
1300 wine_anon_mmap( addr, size, unix_prot, MAP_FIXED );
1301 return create_view( view, addr, size, vprot );
1305 /***********************************************************************
1306 * map_image
1308 * Map an executable (PE format) image into memory.
1310 static NTSTATUS map_image( HANDLE hmapping, ACCESS_MASK access, int fd, char *base, SIZE_T total_size,
1311 SIZE_T mask, SIZE_T header_size, int shared_fd, BOOL removable, PVOID *addr_ptr )
1313 IMAGE_DOS_HEADER *dos;
1314 IMAGE_NT_HEADERS *nt;
1315 IMAGE_SECTION_HEADER sections[96];
1316 IMAGE_SECTION_HEADER *sec;
1317 IMAGE_DATA_DIRECTORY *imports;
1318 NTSTATUS status = STATUS_CONFLICTING_ADDRESSES;
1319 int i;
1320 off_t pos;
1321 sigset_t sigset;
1322 struct stat st;
1323 struct file_view *view = NULL;
1324 char *ptr, *header_end, *header_start;
1326 /* zero-map the whole range */
1328 server_enter_uninterrupted_section( &csVirtual, &sigset );
1330 if (base >= (char *)address_space_start) /* make sure the DOS area remains free */
1331 status = map_view( &view, base, total_size, mask, FALSE, SEC_IMAGE | SEC_FILE |
1332 VPROT_COMMITTED | VPROT_READ | VPROT_EXEC | VPROT_WRITECOPY );
1334 if (status != STATUS_SUCCESS)
1335 status = map_view( &view, NULL, total_size, mask, FALSE, SEC_IMAGE | SEC_FILE |
1336 VPROT_COMMITTED | VPROT_READ | VPROT_EXEC | VPROT_WRITECOPY );
1338 if (status != STATUS_SUCCESS) goto error;
1340 ptr = view->base;
1341 TRACE_(module)( "mapped PE file at %p-%p\n", ptr, ptr + total_size );
1343 /* map the header */
1345 if (fstat( fd, &st ) == -1)
1347 status = FILE_GetNtStatus();
1348 goto error;
1350 status = STATUS_INVALID_IMAGE_FORMAT; /* generic error */
1351 if (!st.st_size) goto error;
1352 header_size = min( header_size, st.st_size );
1353 if (map_file_into_view( view, fd, 0, header_size, 0, VPROT_COMMITTED | VPROT_READ | VPROT_WRITECOPY,
1354 removable ) != STATUS_SUCCESS) goto error;
1355 dos = (IMAGE_DOS_HEADER *)ptr;
1356 nt = (IMAGE_NT_HEADERS *)(ptr + dos->e_lfanew);
1357 header_end = ptr + ROUND_SIZE( 0, header_size );
1358 memset( ptr + header_size, 0, header_end - (ptr + header_size) );
1359 if ((char *)(nt + 1) > header_end) goto error;
1360 header_start = (char*)&nt->OptionalHeader+nt->FileHeader.SizeOfOptionalHeader;
1361 if (nt->FileHeader.NumberOfSections > sizeof(sections)/sizeof(*sections)) goto error;
1362 if (header_start + sizeof(*sections) * nt->FileHeader.NumberOfSections > header_end) goto error;
1363 /* Some applications (e.g. the Steam version of Borderlands) map over the top of the section headers,
1364 * copying the headers into local memory is necessary to properly load such applications. */
1365 memcpy(sections, header_start, sizeof(*sections) * nt->FileHeader.NumberOfSections);
1366 sec = sections;
1368 imports = nt->OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_IMPORT;
1369 if (!imports->Size || !imports->VirtualAddress) imports = NULL;
1371 /* check for non page-aligned binary */
1373 if (nt->OptionalHeader.SectionAlignment <= page_mask)
1375 /* unaligned sections, this happens for native subsystem binaries */
1376 /* in that case Windows simply maps in the whole file */
1378 if (map_file_into_view( view, fd, 0, total_size, 0, VPROT_COMMITTED | VPROT_READ | VPROT_WRITECOPY,
1379 removable ) != STATUS_SUCCESS) goto error;
1381 /* check that all sections are loaded at the right offset */
1382 if (nt->OptionalHeader.FileAlignment != nt->OptionalHeader.SectionAlignment) goto error;
1383 for (i = 0; i < nt->FileHeader.NumberOfSections; i++)
1385 if (sec[i].VirtualAddress != sec[i].PointerToRawData)
1386 goto error; /* Windows refuses to load in that case too */
1389 /* set the image protections */
1390 VIRTUAL_SetProt( view, ptr, total_size,
1391 VPROT_COMMITTED | VPROT_READ | VPROT_WRITECOPY | VPROT_EXEC );
1393 /* no relocations are performed on non page-aligned binaries */
1394 goto done;
1398 /* map all the sections */
1400 for (i = pos = 0; i < nt->FileHeader.NumberOfSections; i++, sec++)
1402 static const SIZE_T sector_align = 0x1ff;
1403 SIZE_T map_size, file_start, file_size, end;
1405 if (!sec->Misc.VirtualSize)
1406 map_size = ROUND_SIZE( 0, sec->SizeOfRawData );
1407 else
1408 map_size = ROUND_SIZE( 0, sec->Misc.VirtualSize );
1410 /* file positions are rounded to sector boundaries regardless of OptionalHeader.FileAlignment */
1411 file_start = sec->PointerToRawData & ~sector_align;
1412 file_size = (sec->SizeOfRawData + (sec->PointerToRawData & sector_align) + sector_align) & ~sector_align;
1413 if (file_size > map_size) file_size = map_size;
1415 /* a few sanity checks */
1416 end = sec->VirtualAddress + ROUND_SIZE( sec->VirtualAddress, map_size );
1417 if (sec->VirtualAddress > total_size || end > total_size || end < sec->VirtualAddress)
1419 WARN_(module)( "Section %.8s too large (%x+%lx/%lx)\n",
1420 sec->Name, sec->VirtualAddress, map_size, total_size );
1421 goto error;
1424 if ((sec->Characteristics & IMAGE_SCN_MEM_SHARED) &&
1425 (sec->Characteristics & IMAGE_SCN_MEM_WRITE))
1427 TRACE_(module)( "mapping shared section %.8s at %p off %x (%x) size %lx (%lx) flags %x\n",
1428 sec->Name, ptr + sec->VirtualAddress,
1429 sec->PointerToRawData, (int)pos, file_size, map_size,
1430 sec->Characteristics );
1431 if (map_file_into_view( view, shared_fd, sec->VirtualAddress, map_size, pos,
1432 VPROT_COMMITTED | VPROT_READ | VPROT_WRITE, FALSE ) != STATUS_SUCCESS)
1434 ERR_(module)( "Could not map shared section %.8s\n", sec->Name );
1435 goto error;
1438 /* check if the import directory falls inside this section */
1439 if (imports && imports->VirtualAddress >= sec->VirtualAddress &&
1440 imports->VirtualAddress < sec->VirtualAddress + map_size)
1442 UINT_PTR base = imports->VirtualAddress & ~page_mask;
1443 UINT_PTR end = base + ROUND_SIZE( imports->VirtualAddress, imports->Size );
1444 if (end > sec->VirtualAddress + map_size) end = sec->VirtualAddress + map_size;
1445 if (end > base)
1446 map_file_into_view( view, shared_fd, base, end - base,
1447 pos + (base - sec->VirtualAddress),
1448 VPROT_COMMITTED | VPROT_READ | VPROT_WRITECOPY, FALSE );
1450 pos += map_size;
1451 continue;
1454 TRACE_(module)( "mapping section %.8s at %p off %x size %x virt %x flags %x\n",
1455 sec->Name, ptr + sec->VirtualAddress,
1456 sec->PointerToRawData, sec->SizeOfRawData,
1457 sec->Misc.VirtualSize, sec->Characteristics );
1459 if (!sec->PointerToRawData || !file_size) continue;
1461 /* Note: if the section is not aligned properly map_file_into_view will magically
1462 * fall back to read(), so we don't need to check anything here.
1464 end = file_start + file_size;
1465 if (sec->PointerToRawData >= st.st_size ||
1466 end > ((st.st_size + sector_align) & ~sector_align) ||
1467 end < file_start ||
1468 map_file_into_view( view, fd, sec->VirtualAddress, file_size, file_start,
1469 VPROT_COMMITTED | VPROT_READ | VPROT_WRITECOPY,
1470 removable ) != STATUS_SUCCESS)
1472 ERR_(module)( "Could not map section %.8s, file probably truncated\n", sec->Name );
1473 goto error;
1476 if (file_size & page_mask)
1478 end = ROUND_SIZE( 0, file_size );
1479 if (end > map_size) end = map_size;
1480 TRACE_(module)("clearing %p - %p\n",
1481 ptr + sec->VirtualAddress + file_size,
1482 ptr + sec->VirtualAddress + end );
1483 memset( ptr + sec->VirtualAddress + file_size, 0, end - file_size );
1487 /* set the image protections */
1489 VIRTUAL_SetProt( view, ptr, ROUND_SIZE( 0, header_size ), VPROT_COMMITTED | VPROT_READ );
1491 sec = sections;
1492 for (i = 0; i < nt->FileHeader.NumberOfSections; i++, sec++)
1494 SIZE_T size;
1495 BYTE vprot = VPROT_COMMITTED;
1497 if (sec->Misc.VirtualSize)
1498 size = ROUND_SIZE( sec->VirtualAddress, sec->Misc.VirtualSize );
1499 else
1500 size = ROUND_SIZE( sec->VirtualAddress, sec->SizeOfRawData );
1502 if (sec->Characteristics & IMAGE_SCN_MEM_READ) vprot |= VPROT_READ;
1503 if (sec->Characteristics & IMAGE_SCN_MEM_WRITE) vprot |= VPROT_WRITECOPY;
1504 if (sec->Characteristics & IMAGE_SCN_MEM_EXECUTE) vprot |= VPROT_EXEC;
1506 /* Dumb game crack lets the AOEP point into a data section. Adjust. */
1507 if ((nt->OptionalHeader.AddressOfEntryPoint >= sec->VirtualAddress) &&
1508 (nt->OptionalHeader.AddressOfEntryPoint < sec->VirtualAddress + size))
1509 vprot |= VPROT_EXEC;
1511 if (!VIRTUAL_SetProt( view, ptr + sec->VirtualAddress, size, vprot ) && (vprot & VPROT_EXEC))
1512 ERR( "failed to set %08x protection on section %.8s, noexec filesystem?\n",
1513 sec->Characteristics, sec->Name );
1516 done:
1518 SERVER_START_REQ( map_view )
1520 req->mapping = wine_server_obj_handle( hmapping );
1521 req->access = access;
1522 req->base = wine_server_client_ptr( view->base );
1523 req->size = view->size;
1524 req->start = 0;
1525 status = wine_server_call( req );
1527 SERVER_END_REQ;
1528 if (status) goto error;
1530 VIRTUAL_DEBUG_DUMP_VIEW( view );
1531 server_leave_uninterrupted_section( &csVirtual, &sigset );
1533 *addr_ptr = ptr;
1534 #ifdef VALGRIND_LOAD_PDB_DEBUGINFO
1535 VALGRIND_LOAD_PDB_DEBUGINFO(fd, ptr, total_size, ptr - base);
1536 #endif
1537 if (ptr != base) return STATUS_IMAGE_NOT_AT_BASE;
1538 return STATUS_SUCCESS;
1540 error:
1541 if (view) delete_view( view );
1542 server_leave_uninterrupted_section( &csVirtual, &sigset );
1543 return status;
1547 struct alloc_virtual_heap
1549 void *base;
1550 size_t size;
1553 /* callback for wine_mmap_enum_reserved_areas to allocate space for the virtual heap */
1554 static int alloc_virtual_heap( void *base, size_t size, void *arg )
1556 struct alloc_virtual_heap *alloc = arg;
1558 if (is_beyond_limit( base, size, address_space_limit )) address_space_limit = (char *)base + size;
1559 if (size < alloc->size) return 0;
1560 if (is_win64 && base < (void *)0x80000000) return 0;
1561 alloc->base = wine_anon_mmap( (char *)base + size - alloc->size, alloc->size,
1562 PROT_READ|PROT_WRITE, MAP_FIXED );
1563 return (alloc->base != (void *)-1);
1566 /***********************************************************************
1567 * virtual_init
1569 void virtual_init(void)
1571 const char *preload;
1572 struct alloc_virtual_heap alloc_views;
1573 size_t size;
1575 #if !defined(__i386__) && !defined(__x86_64__)
1576 page_size = sysconf( _SC_PAGESIZE );
1577 page_mask = page_size - 1;
1578 /* Make sure we have a power of 2 */
1579 assert( !(page_size & page_mask) );
1580 page_shift = 0;
1581 while ((1 << page_shift) != page_size) page_shift++;
1582 #ifdef _WIN64
1583 address_space_limit = (void *)(((1UL << 47) - 1) & ~page_mask);
1584 #else
1585 address_space_limit = (void *)~page_mask;
1586 #endif
1587 user_space_limit = working_set_limit = address_space_limit;
1588 #endif
1589 if ((preload = getenv("WINEPRELOADRESERVE")))
1591 unsigned long start, end;
1592 if (sscanf( preload, "%lx-%lx", &start, &end ) == 2)
1594 preload_reserve_start = (void *)start;
1595 preload_reserve_end = (void *)end;
1596 /* some apps start inside the DOS area */
1597 address_space_start = min( address_space_start, preload_reserve_start );
1601 /* try to find space in a reserved area for the views and pages protection table */
1602 #ifdef _WIN64
1603 pages_vprot_size = ((size_t)address_space_limit >> page_shift >> pages_vprot_shift) + 1;
1604 alloc_views.size = view_block_size + pages_vprot_size * sizeof(*pages_vprot);
1605 #else
1606 alloc_views.size = view_block_size + (1U << (32 - page_shift));
1607 #endif
1608 if (wine_mmap_enum_reserved_areas( alloc_virtual_heap, &alloc_views, 1 ))
1609 wine_mmap_remove_reserved_area( alloc_views.base, alloc_views.size, 0 );
1610 else
1611 alloc_views.base = wine_anon_mmap( NULL, alloc_views.size, PROT_READ | PROT_WRITE, 0 );
1613 assert( alloc_views.base != (void *)-1 );
1614 view_block_start = alloc_views.base;
1615 view_block_end = view_block_start + view_block_size / sizeof(*view_block_start);
1616 pages_vprot = (void *)((char *)alloc_views.base + view_block_size);
1617 wine_rb_init( &views_tree, compare_view );
1619 /* make the DOS area accessible (except the low 64K) to hide bugs in broken apps like Excel 2003 */
1620 size = (char *)address_space_start - (char *)0x10000;
1621 if (size && wine_mmap_is_in_reserved_area( (void*)0x10000, size ) == 1)
1622 wine_anon_mmap( (void *)0x10000, size, PROT_READ | PROT_WRITE, MAP_FIXED );
1626 /***********************************************************************
1627 * virtual_init_threading
1629 void virtual_init_threading(void)
1631 use_locks = TRUE;
1635 /***********************************************************************
1636 * virtual_get_system_info
1638 void virtual_get_system_info( SYSTEM_BASIC_INFORMATION *info )
1640 #ifdef HAVE_SYSINFO
1641 struct sysinfo sinfo;
1642 #endif
1644 info->unknown = 0;
1645 info->KeMaximumIncrement = 0; /* FIXME */
1646 info->PageSize = page_size;
1647 info->MmLowestPhysicalPage = 1;
1648 info->MmHighestPhysicalPage = 0x7fffffff / page_size;
1649 #ifdef HAVE_SYSINFO
1650 if (!sysinfo(&sinfo))
1652 ULONG64 total = (ULONG64)sinfo.totalram * sinfo.mem_unit;
1653 info->MmHighestPhysicalPage = max(1, total / page_size);
1655 #endif
1656 info->MmNumberOfPhysicalPages = info->MmHighestPhysicalPage - info->MmLowestPhysicalPage;
1657 info->AllocationGranularity = get_mask(0) + 1;
1658 info->LowestUserAddress = (void *)0x10000;
1659 info->HighestUserAddress = (char *)user_space_limit - 1;
1660 info->ActiveProcessorsAffinityMask = get_system_affinity_mask();
1661 info->NumberOfProcessors = NtCurrentTeb()->Peb->NumberOfProcessors;
1665 /***********************************************************************
1666 * virtual_create_builtin_view
1668 NTSTATUS virtual_create_builtin_view( void *module )
1670 NTSTATUS status;
1671 sigset_t sigset;
1672 IMAGE_NT_HEADERS *nt = RtlImageNtHeader( module );
1673 SIZE_T size = nt->OptionalHeader.SizeOfImage;
1674 IMAGE_SECTION_HEADER *sec;
1675 struct file_view *view;
1676 void *base;
1677 int i;
1679 size = ROUND_SIZE( module, size );
1680 base = ROUND_ADDR( module, page_mask );
1681 server_enter_uninterrupted_section( &csVirtual, &sigset );
1682 status = create_view( &view, base, size, SEC_IMAGE | SEC_FILE | VPROT_SYSTEM |
1683 VPROT_COMMITTED | VPROT_READ | VPROT_WRITECOPY | VPROT_EXEC );
1684 if (!status)
1686 TRACE( "created %p-%p\n", base, (char *)base + size );
1688 /* The PE header is always read-only, no write, no execute. */
1689 set_page_vprot( base, page_size, VPROT_COMMITTED | VPROT_READ );
1691 sec = (IMAGE_SECTION_HEADER *)((char *)&nt->OptionalHeader + nt->FileHeader.SizeOfOptionalHeader);
1692 for (i = 0; i < nt->FileHeader.NumberOfSections; i++)
1694 BYTE flags = VPROT_COMMITTED;
1696 if (sec[i].Characteristics & IMAGE_SCN_MEM_EXECUTE) flags |= VPROT_EXEC;
1697 if (sec[i].Characteristics & IMAGE_SCN_MEM_READ) flags |= VPROT_READ;
1698 if (sec[i].Characteristics & IMAGE_SCN_MEM_WRITE) flags |= VPROT_WRITE;
1699 set_page_vprot( (char *)base + sec[i].VirtualAddress, sec[i].Misc.VirtualSize, flags );
1701 VIRTUAL_DEBUG_DUMP_VIEW( view );
1703 server_leave_uninterrupted_section( &csVirtual, &sigset );
1704 return status;
1708 /***********************************************************************
1709 * virtual_alloc_thread_stack
1711 NTSTATUS virtual_alloc_thread_stack( TEB *teb, SIZE_T reserve_size, SIZE_T commit_size )
1713 struct file_view *view;
1714 NTSTATUS status;
1715 sigset_t sigset;
1716 SIZE_T size;
1718 if (!reserve_size || !commit_size)
1720 IMAGE_NT_HEADERS *nt = RtlImageNtHeader( NtCurrentTeb()->Peb->ImageBaseAddress );
1721 if (!reserve_size) reserve_size = nt->OptionalHeader.SizeOfStackReserve;
1722 if (!commit_size) commit_size = nt->OptionalHeader.SizeOfStackCommit;
1725 size = max( reserve_size, commit_size );
1726 if (size < 1024 * 1024) size = 1024 * 1024; /* Xlib needs a large stack */
1727 size = (size + 0xffff) & ~0xffff; /* round to 64K boundary */
1729 server_enter_uninterrupted_section( &csVirtual, &sigset );
1731 if ((status = map_view( &view, NULL, size, 0xffff, 0,
1732 VPROT_READ | VPROT_WRITE | VPROT_COMMITTED )) != STATUS_SUCCESS)
1733 goto done;
1735 #ifdef VALGRIND_STACK_REGISTER
1736 VALGRIND_STACK_REGISTER( view->base, (char *)view->base + view->size );
1737 #endif
1739 /* setup no access guard page */
1740 set_page_vprot( view->base, page_size, VPROT_COMMITTED );
1741 set_page_vprot( (char *)view->base + page_size, page_size,
1742 VPROT_READ | VPROT_WRITE | VPROT_COMMITTED | VPROT_GUARD );
1743 mprotect_range( view->base, 2 * page_size, 0, 0 );
1744 VIRTUAL_DEBUG_DUMP_VIEW( view );
1746 /* note: limit is lower than base since the stack grows down */
1747 teb->DeallocationStack = view->base;
1748 teb->Tib.StackBase = (char *)view->base + view->size;
1749 teb->Tib.StackLimit = (char *)view->base + 2 * page_size;
1750 done:
1751 server_leave_uninterrupted_section( &csVirtual, &sigset );
1752 return status;
1756 /***********************************************************************
1757 * virtual_clear_thread_stack
1759 * Clear the stack contents before calling the main entry point, some broken apps need that.
1761 void virtual_clear_thread_stack(void)
1763 void *stack = NtCurrentTeb()->Tib.StackLimit;
1764 size_t size = (char *)NtCurrentTeb()->Tib.StackBase - (char *)NtCurrentTeb()->Tib.StackLimit;
1766 wine_anon_mmap( stack, size - page_size, PROT_READ | PROT_WRITE, MAP_FIXED );
1767 if (force_exec_prot) mprotect( stack, size - page_size, PROT_READ | PROT_WRITE | PROT_EXEC );
1771 /***********************************************************************
1772 * virtual_handle_fault
1774 NTSTATUS virtual_handle_fault( LPCVOID addr, DWORD err, BOOL on_signal_stack )
1776 NTSTATUS ret = STATUS_ACCESS_VIOLATION;
1777 void *page = ROUND_ADDR( addr, page_mask );
1778 sigset_t sigset;
1779 BYTE vprot;
1781 server_enter_uninterrupted_section( &csVirtual, &sigset );
1782 vprot = get_page_vprot( page );
1783 if (!on_signal_stack && (vprot & VPROT_GUARD))
1785 set_page_vprot_bits( page, page_size, 0, VPROT_GUARD );
1786 mprotect_range( page, page_size, 0, 0 );
1787 ret = STATUS_GUARD_PAGE_VIOLATION;
1789 else if (err & EXCEPTION_WRITE_FAULT)
1791 if (vprot & VPROT_WRITEWATCH)
1793 set_page_vprot_bits( page, page_size, 0, VPROT_WRITEWATCH );
1794 mprotect_range( page, page_size, 0, 0 );
1796 /* ignore fault if page is writable now */
1797 if (VIRTUAL_GetUnixProt( get_page_vprot( page )) & PROT_WRITE)
1799 if ((vprot & VPROT_WRITEWATCH) || is_write_watch_range( page, page_size ))
1800 ret = STATUS_SUCCESS;
1803 server_leave_uninterrupted_section( &csVirtual, &sigset );
1804 return ret;
1808 /***********************************************************************
1809 * check_write_access
1811 * Check if the memory range is writable, temporarily disabling write watches if necessary.
1813 static NTSTATUS check_write_access( void *base, size_t size, BOOL *has_write_watch )
1815 size_t i;
1816 char *addr = ROUND_ADDR( base, page_mask );
1818 size = ROUND_SIZE( base, size );
1819 for (i = 0; i < size; i += page_size)
1821 BYTE vprot = get_page_vprot( addr + i );
1822 if (vprot & VPROT_WRITEWATCH) *has_write_watch = TRUE;
1823 if (!(VIRTUAL_GetUnixProt( vprot & ~VPROT_WRITEWATCH ) & PROT_WRITE))
1824 return STATUS_INVALID_USER_BUFFER;
1826 if (*has_write_watch)
1827 mprotect_range( addr, size, 0, VPROT_WRITEWATCH ); /* temporarily enable write access */
1828 return STATUS_SUCCESS;
1832 /***********************************************************************
1833 * virtual_locked_server_call
1835 unsigned int virtual_locked_server_call( void *req_ptr )
1837 struct __server_request_info * const req = req_ptr;
1838 sigset_t sigset;
1839 void *addr = req->reply_data;
1840 data_size_t size = req->u.req.request_header.reply_size;
1841 BOOL has_write_watch = FALSE;
1842 unsigned int ret = STATUS_ACCESS_VIOLATION;
1844 if (!size) return wine_server_call( req_ptr );
1846 server_enter_uninterrupted_section( &csVirtual, &sigset );
1847 if (!(ret = check_write_access( addr, size, &has_write_watch )))
1849 ret = server_call_unlocked( req );
1850 if (has_write_watch) update_write_watches( addr, size, wine_server_reply_size( req ));
1852 server_leave_uninterrupted_section( &csVirtual, &sigset );
1853 return ret;
1857 /***********************************************************************
1858 * virtual_locked_read
1860 ssize_t virtual_locked_read( int fd, void *addr, size_t size )
1862 sigset_t sigset;
1863 BOOL has_write_watch = FALSE;
1864 int err = EFAULT;
1866 ssize_t ret = read( fd, addr, size );
1867 if (ret != -1 || errno != EFAULT) return ret;
1869 server_enter_uninterrupted_section( &csVirtual, &sigset );
1870 if (!check_write_access( addr, size, &has_write_watch ))
1872 ret = read( fd, addr, size );
1873 err = errno;
1874 if (has_write_watch) update_write_watches( addr, size, max( 0, ret ));
1876 server_leave_uninterrupted_section( &csVirtual, &sigset );
1877 errno = err;
1878 return ret;
1882 /***********************************************************************
1883 * virtual_locked_pread
1885 ssize_t virtual_locked_pread( int fd, void *addr, size_t size, off_t offset )
1887 sigset_t sigset;
1888 BOOL has_write_watch = FALSE;
1889 int err = EFAULT;
1891 ssize_t ret = pread( fd, addr, size, offset );
1892 if (ret != -1 || errno != EFAULT) return ret;
1894 server_enter_uninterrupted_section( &csVirtual, &sigset );
1895 if (!check_write_access( addr, size, &has_write_watch ))
1897 ret = pread( fd, addr, size, offset );
1898 err = errno;
1899 if (has_write_watch) update_write_watches( addr, size, max( 0, ret ));
1901 server_leave_uninterrupted_section( &csVirtual, &sigset );
1902 errno = err;
1903 return ret;
1908 /***********************************************************************
1909 * virtual_is_valid_code_address
1911 BOOL virtual_is_valid_code_address( const void *addr, SIZE_T size )
1913 struct file_view *view;
1914 BOOL ret = FALSE;
1915 sigset_t sigset;
1917 server_enter_uninterrupted_section( &csVirtual, &sigset );
1918 if ((view = VIRTUAL_FindView( addr, size )))
1919 ret = !(view->protect & VPROT_SYSTEM); /* system views are not visible to the app */
1920 server_leave_uninterrupted_section( &csVirtual, &sigset );
1921 return ret;
1925 /***********************************************************************
1926 * virtual_handle_stack_fault
1928 * Handle an access fault inside the current thread stack.
1929 * Called from inside a signal handler.
1931 BOOL virtual_handle_stack_fault( void *addr )
1933 BOOL ret = FALSE;
1935 RtlEnterCriticalSection( &csVirtual ); /* no need for signal masking inside signal handler */
1936 if (get_page_vprot( addr ) & VPROT_GUARD)
1938 char *page = ROUND_ADDR( addr, page_mask );
1939 set_page_vprot_bits( page, page_size, 0, VPROT_GUARD );
1940 mprotect_range( page, page_size, 0, 0 );
1941 NtCurrentTeb()->Tib.StackLimit = page;
1942 if (page >= (char *)NtCurrentTeb()->DeallocationStack + 2*page_size)
1944 page -= page_size;
1945 set_page_vprot_bits( page, page_size, VPROT_COMMITTED | VPROT_GUARD, 0 );
1946 mprotect_range( page, page_size, 0, 0 );
1948 ret = TRUE;
1950 RtlLeaveCriticalSection( &csVirtual );
1951 return ret;
1955 /***********************************************************************
1956 * virtual_check_buffer_for_read
1958 * Check if a memory buffer can be read, triggering page faults if needed for DIB section access.
1960 BOOL virtual_check_buffer_for_read( const void *ptr, SIZE_T size )
1962 if (!size) return TRUE;
1963 if (!ptr) return FALSE;
1965 __TRY
1967 volatile const char *p = ptr;
1968 char dummy __attribute__((unused));
1969 SIZE_T count = size;
1971 while (count > page_size)
1973 dummy = *p;
1974 p += page_size;
1975 count -= page_size;
1977 dummy = p[0];
1978 dummy = p[count - 1];
1980 __EXCEPT_PAGE_FAULT
1982 return FALSE;
1984 __ENDTRY
1985 return TRUE;
1989 /***********************************************************************
1990 * virtual_check_buffer_for_write
1992 * Check if a memory buffer can be written to, triggering page faults if needed for write watches.
1994 BOOL virtual_check_buffer_for_write( void *ptr, SIZE_T size )
1996 if (!size) return TRUE;
1997 if (!ptr) return FALSE;
1999 __TRY
2001 volatile char *p = ptr;
2002 SIZE_T count = size;
2004 while (count > page_size)
2006 *p |= 0;
2007 p += page_size;
2008 count -= page_size;
2010 p[0] |= 0;
2011 p[count - 1] |= 0;
2013 __EXCEPT_PAGE_FAULT
2015 return FALSE;
2017 __ENDTRY
2018 return TRUE;
2022 /***********************************************************************
2023 * virtual_uninterrupted_read_memory
2025 * Similar to NtReadVirtualMemory, but without wineserver calls. Moreover
2026 * permissions are checked before accessing each page, to ensure that no
2027 * exceptions can happen.
2029 SIZE_T virtual_uninterrupted_read_memory( const void *addr, void *buffer, SIZE_T size )
2031 struct file_view *view;
2032 sigset_t sigset;
2033 SIZE_T bytes_read = 0;
2035 if (!size) return 0;
2037 server_enter_uninterrupted_section( &csVirtual, &sigset );
2038 if ((view = VIRTUAL_FindView( addr, size )))
2040 if (!(view->protect & VPROT_SYSTEM))
2042 char *page = ROUND_ADDR( addr, page_mask );
2044 while (bytes_read < size && (VIRTUAL_GetUnixProt( get_page_vprot( page )) & PROT_READ))
2046 SIZE_T block_size = min( size, page_size - ((UINT_PTR)addr & page_mask) );
2047 memcpy( buffer, addr, block_size );
2049 addr = (const void *)((const char *)addr + block_size);
2050 buffer = (void *)((char *)buffer + block_size);
2051 bytes_read += block_size;
2052 page += page_size;
2056 server_leave_uninterrupted_section( &csVirtual, &sigset );
2057 return bytes_read;
2061 /***********************************************************************
2062 * virtual_uninterrupted_write_memory
2064 * Similar to NtWriteVirtualMemory, but without wineserver calls. Moreover
2065 * permissions are checked before accessing each page, to ensure that no
2066 * exceptions can happen.
2068 NTSTATUS virtual_uninterrupted_write_memory( void *addr, const void *buffer, SIZE_T size )
2070 BOOL has_write_watch = FALSE;
2071 sigset_t sigset;
2072 NTSTATUS ret;
2074 if (!size) return STATUS_SUCCESS;
2076 server_enter_uninterrupted_section( &csVirtual, &sigset );
2077 if (!(ret = check_write_access( addr, size, &has_write_watch )))
2079 memcpy( addr, buffer, size );
2080 if (has_write_watch) update_write_watches( addr, size, size );
2082 server_leave_uninterrupted_section( &csVirtual, &sigset );
2083 return ret;
2087 /***********************************************************************
2088 * VIRTUAL_SetForceExec
2090 * Whether to force exec prot on all views.
2092 void VIRTUAL_SetForceExec( BOOL enable )
2094 struct file_view *view;
2095 sigset_t sigset;
2097 server_enter_uninterrupted_section( &csVirtual, &sigset );
2098 if (!force_exec_prot != !enable) /* change all existing views */
2100 force_exec_prot = enable;
2102 WINE_RB_FOR_EACH_ENTRY( view, &views_tree, struct file_view, entry )
2104 /* file mappings are always accessible */
2105 BYTE commit = is_view_valloc( view ) ? 0 : VPROT_COMMITTED;
2107 mprotect_range( view->base, view->size, commit, 0 );
2110 server_leave_uninterrupted_section( &csVirtual, &sigset );
2113 struct free_range
2115 char *base;
2116 char *limit;
2119 /* free reserved areas above the limit; callback for wine_mmap_enum_reserved_areas */
2120 static int free_reserved_memory( void *base, size_t size, void *arg )
2122 struct free_range *range = arg;
2124 if ((char *)base >= range->limit) return 0;
2125 if ((char *)base + size <= range->base) return 0;
2126 if ((char *)base < range->base)
2128 size -= range->base - (char *)base;
2129 base = range->base;
2131 if ((char *)base + size > range->limit) size = range->limit - (char *)base;
2132 remove_reserved_area( base, size );
2133 return 1; /* stop enumeration since the list has changed */
2136 /***********************************************************************
2137 * virtual_release_address_space
2139 * Release some address space once we have loaded and initialized the app.
2141 void virtual_release_address_space(void)
2143 struct free_range range;
2144 sigset_t sigset;
2146 if (is_win64) return;
2148 server_enter_uninterrupted_section( &csVirtual, &sigset );
2150 range.base = (char *)0x82000000;
2151 range.limit = user_space_limit;
2153 if (range.limit > range.base)
2155 while (wine_mmap_enum_reserved_areas( free_reserved_memory, &range, 1 )) /* nothing */;
2157 else
2159 #ifndef __APPLE__ /* dyld doesn't support parts of the WINE_DOS segment being unmapped */
2160 range.base = (char *)0x20000000;
2161 range.limit = (char *)0x7f000000;
2162 while (wine_mmap_enum_reserved_areas( free_reserved_memory, &range, 0 )) /* nothing */;
2163 #endif
2166 server_leave_uninterrupted_section( &csVirtual, &sigset );
2170 /***********************************************************************
2171 * virtual_set_large_address_space
2173 * Enable use of a large address space when allowed by the application.
2175 void virtual_set_large_address_space(void)
2177 IMAGE_NT_HEADERS *nt = RtlImageNtHeader( NtCurrentTeb()->Peb->ImageBaseAddress );
2179 if (!(nt->FileHeader.Characteristics & IMAGE_FILE_LARGE_ADDRESS_AWARE)) return;
2180 /* no large address space on win9x */
2181 if (NtCurrentTeb()->Peb->OSPlatformId != VER_PLATFORM_WIN32_NT) return;
2183 user_space_limit = working_set_limit = address_space_limit;
2187 /***********************************************************************
2188 * NtAllocateVirtualMemory (NTDLL.@)
2189 * ZwAllocateVirtualMemory (NTDLL.@)
2191 NTSTATUS WINAPI NtAllocateVirtualMemory( HANDLE process, PVOID *ret, ULONG zero_bits,
2192 SIZE_T *size_ptr, ULONG type, ULONG protect )
2194 void *base;
2195 unsigned int vprot;
2196 SIZE_T size = *size_ptr;
2197 SIZE_T mask = get_mask( zero_bits );
2198 NTSTATUS status = STATUS_SUCCESS;
2199 BOOL is_dos_memory = FALSE;
2200 struct file_view *view;
2201 sigset_t sigset;
2203 TRACE("%p %p %08lx %x %08x\n", process, *ret, size, type, protect );
2205 if (!size) return STATUS_INVALID_PARAMETER;
2206 if (!mask) return STATUS_INVALID_PARAMETER_3;
2208 if (process != NtCurrentProcess())
2210 apc_call_t call;
2211 apc_result_t result;
2213 memset( &call, 0, sizeof(call) );
2215 call.virtual_alloc.type = APC_VIRTUAL_ALLOC;
2216 call.virtual_alloc.addr = wine_server_client_ptr( *ret );
2217 call.virtual_alloc.size = *size_ptr;
2218 call.virtual_alloc.zero_bits = zero_bits;
2219 call.virtual_alloc.op_type = type;
2220 call.virtual_alloc.prot = protect;
2221 status = server_queue_process_apc( process, &call, &result );
2222 if (status != STATUS_SUCCESS) return status;
2224 if (result.virtual_alloc.status == STATUS_SUCCESS)
2226 *ret = wine_server_get_ptr( result.virtual_alloc.addr );
2227 *size_ptr = result.virtual_alloc.size;
2229 return result.virtual_alloc.status;
2232 /* Round parameters to a page boundary */
2234 if (is_beyond_limit( 0, size, working_set_limit )) return STATUS_WORKING_SET_LIMIT_RANGE;
2236 if (*ret)
2238 if (type & MEM_RESERVE) /* Round down to 64k boundary */
2239 base = ROUND_ADDR( *ret, mask );
2240 else
2241 base = ROUND_ADDR( *ret, page_mask );
2242 size = (((UINT_PTR)*ret + size + page_mask) & ~page_mask) - (UINT_PTR)base;
2244 /* disallow low 64k, wrap-around and kernel space */
2245 if (((char *)base < (char *)0x10000) ||
2246 ((char *)base + size < (char *)base) ||
2247 is_beyond_limit( base, size, address_space_limit ))
2249 /* address 1 is magic to mean DOS area */
2250 if (!base && *ret == (void *)1 && size == 0x110000) is_dos_memory = TRUE;
2251 else return STATUS_INVALID_PARAMETER;
2254 else
2256 base = NULL;
2257 size = (size + page_mask) & ~page_mask;
2260 /* Compute the alloc type flags */
2262 if (!(type & (MEM_COMMIT | MEM_RESERVE | MEM_RESET)) ||
2263 (type & ~(MEM_COMMIT | MEM_RESERVE | MEM_TOP_DOWN | MEM_WRITE_WATCH | MEM_RESET)))
2265 WARN("called with wrong alloc type flags (%08x) !\n", type);
2266 return STATUS_INVALID_PARAMETER;
2269 /* Reserve the memory */
2271 if (use_locks) server_enter_uninterrupted_section( &csVirtual, &sigset );
2273 if ((type & MEM_RESERVE) || !base)
2275 if (!(status = get_vprot_flags( protect, &vprot, FALSE )))
2277 if (type & MEM_COMMIT) vprot |= VPROT_COMMITTED;
2278 if (type & MEM_WRITE_WATCH) vprot |= VPROT_WRITEWATCH;
2279 if (protect & PAGE_NOCACHE) vprot |= SEC_NOCACHE;
2281 if (vprot & VPROT_WRITECOPY) status = STATUS_INVALID_PAGE_PROTECTION;
2282 else if (is_dos_memory) status = allocate_dos_memory( &view, vprot );
2283 else status = map_view( &view, base, size, mask, type & MEM_TOP_DOWN, vprot );
2285 if (status == STATUS_SUCCESS) base = view->base;
2288 else if (type & MEM_RESET)
2290 if (!(view = VIRTUAL_FindView( base, size ))) status = STATUS_NOT_MAPPED_VIEW;
2291 else madvise( base, size, MADV_DONTNEED );
2293 else /* commit the pages */
2295 if (!(view = VIRTUAL_FindView( base, size ))) status = STATUS_NOT_MAPPED_VIEW;
2296 else if (view->protect & SEC_FILE) status = STATUS_ALREADY_COMMITTED;
2297 else if (!(status = set_protection( view, base, size, protect )) && (view->protect & SEC_RESERVE))
2299 SERVER_START_REQ( add_mapping_committed_range )
2301 req->base = wine_server_client_ptr( view->base );
2302 req->offset = (char *)base - (char *)view->base;
2303 req->size = size;
2304 wine_server_call( req );
2306 SERVER_END_REQ;
2310 if (!status) VIRTUAL_DEBUG_DUMP_VIEW( view );
2312 if (use_locks) server_leave_uninterrupted_section( &csVirtual, &sigset );
2314 if (status == STATUS_SUCCESS)
2316 *ret = base;
2317 *size_ptr = size;
2319 return status;
2323 /***********************************************************************
2324 * NtFreeVirtualMemory (NTDLL.@)
2325 * ZwFreeVirtualMemory (NTDLL.@)
2327 NTSTATUS WINAPI NtFreeVirtualMemory( HANDLE process, PVOID *addr_ptr, SIZE_T *size_ptr, ULONG type )
2329 struct file_view *view;
2330 char *base;
2331 sigset_t sigset;
2332 NTSTATUS status = STATUS_SUCCESS;
2333 LPVOID addr = *addr_ptr;
2334 SIZE_T size = *size_ptr;
2336 TRACE("%p %p %08lx %x\n", process, addr, size, type );
2338 if (process != NtCurrentProcess())
2340 apc_call_t call;
2341 apc_result_t result;
2343 memset( &call, 0, sizeof(call) );
2345 call.virtual_free.type = APC_VIRTUAL_FREE;
2346 call.virtual_free.addr = wine_server_client_ptr( addr );
2347 call.virtual_free.size = size;
2348 call.virtual_free.op_type = type;
2349 status = server_queue_process_apc( process, &call, &result );
2350 if (status != STATUS_SUCCESS) return status;
2352 if (result.virtual_free.status == STATUS_SUCCESS)
2354 *addr_ptr = wine_server_get_ptr( result.virtual_free.addr );
2355 *size_ptr = result.virtual_free.size;
2357 return result.virtual_free.status;
2360 /* Fix the parameters */
2362 size = ROUND_SIZE( addr, size );
2363 base = ROUND_ADDR( addr, page_mask );
2365 /* avoid freeing the DOS area when a broken app passes a NULL pointer */
2366 if (!base) return STATUS_INVALID_PARAMETER;
2368 server_enter_uninterrupted_section( &csVirtual, &sigset );
2370 if (!(view = VIRTUAL_FindView( base, size )) || !is_view_valloc( view ))
2372 status = STATUS_INVALID_PARAMETER;
2374 else if (type == MEM_RELEASE)
2376 /* Free the pages */
2378 if (size || (base != view->base)) status = STATUS_INVALID_PARAMETER;
2379 else
2381 delete_view( view );
2382 *addr_ptr = base;
2383 *size_ptr = size;
2386 else if (type == MEM_DECOMMIT)
2388 status = decommit_pages( view, base - (char *)view->base, size );
2389 if (status == STATUS_SUCCESS)
2391 *addr_ptr = base;
2392 *size_ptr = size;
2395 else
2397 WARN("called with wrong free type flags (%08x) !\n", type);
2398 status = STATUS_INVALID_PARAMETER;
2401 server_leave_uninterrupted_section( &csVirtual, &sigset );
2402 return status;
2406 /***********************************************************************
2407 * NtProtectVirtualMemory (NTDLL.@)
2408 * ZwProtectVirtualMemory (NTDLL.@)
2410 NTSTATUS WINAPI NtProtectVirtualMemory( HANDLE process, PVOID *addr_ptr, SIZE_T *size_ptr,
2411 ULONG new_prot, ULONG *old_prot )
2413 struct file_view *view;
2414 sigset_t sigset;
2415 NTSTATUS status = STATUS_SUCCESS;
2416 char *base;
2417 BYTE vprot;
2418 SIZE_T size = *size_ptr;
2419 LPVOID addr = *addr_ptr;
2420 DWORD old;
2422 TRACE("%p %p %08lx %08x\n", process, addr, size, new_prot );
2424 if (!old_prot)
2425 return STATUS_ACCESS_VIOLATION;
2427 if (process != NtCurrentProcess())
2429 apc_call_t call;
2430 apc_result_t result;
2432 memset( &call, 0, sizeof(call) );
2434 call.virtual_protect.type = APC_VIRTUAL_PROTECT;
2435 call.virtual_protect.addr = wine_server_client_ptr( addr );
2436 call.virtual_protect.size = size;
2437 call.virtual_protect.prot = new_prot;
2438 status = server_queue_process_apc( process, &call, &result );
2439 if (status != STATUS_SUCCESS) return status;
2441 if (result.virtual_protect.status == STATUS_SUCCESS)
2443 *addr_ptr = wine_server_get_ptr( result.virtual_protect.addr );
2444 *size_ptr = result.virtual_protect.size;
2445 if (old_prot) *old_prot = result.virtual_protect.prot;
2447 return result.virtual_protect.status;
2450 /* Fix the parameters */
2452 size = ROUND_SIZE( addr, size );
2453 base = ROUND_ADDR( addr, page_mask );
2455 server_enter_uninterrupted_section( &csVirtual, &sigset );
2457 if ((view = VIRTUAL_FindView( base, size )))
2459 /* Make sure all the pages are committed */
2460 if (get_committed_size( view, base, &vprot ) >= size && (vprot & VPROT_COMMITTED))
2462 old = VIRTUAL_GetWin32Prot( vprot, view->protect );
2463 status = set_protection( view, base, size, new_prot );
2465 else status = STATUS_NOT_COMMITTED;
2467 else status = STATUS_INVALID_PARAMETER;
2469 if (!status) VIRTUAL_DEBUG_DUMP_VIEW( view );
2471 server_leave_uninterrupted_section( &csVirtual, &sigset );
2473 if (status == STATUS_SUCCESS)
2475 *addr_ptr = base;
2476 *size_ptr = size;
2477 *old_prot = old;
2479 return status;
2483 /* retrieve state for a free memory area; callback for wine_mmap_enum_reserved_areas */
2484 static int get_free_mem_state_callback( void *start, size_t size, void *arg )
2486 MEMORY_BASIC_INFORMATION *info = arg;
2487 void *end = (char *)start + size;
2489 if ((char *)info->BaseAddress + info->RegionSize < (char *)start) return 0;
2491 if (info->BaseAddress >= end)
2493 if (info->AllocationBase < end) info->AllocationBase = end;
2494 return 0;
2497 if (info->BaseAddress >= start || start <= address_space_start)
2499 /* it's a real free area */
2500 info->State = MEM_FREE;
2501 info->Protect = PAGE_NOACCESS;
2502 info->AllocationBase = 0;
2503 info->AllocationProtect = 0;
2504 info->Type = 0;
2505 if ((char *)info->BaseAddress + info->RegionSize > (char *)end)
2506 info->RegionSize = (char *)end - (char *)info->BaseAddress;
2508 else /* outside of the reserved area, pretend it's allocated */
2510 info->RegionSize = (char *)start - (char *)info->BaseAddress;
2511 info->State = MEM_RESERVE;
2512 info->Protect = PAGE_NOACCESS;
2513 info->AllocationProtect = PAGE_NOACCESS;
2514 info->Type = MEM_PRIVATE;
2516 return 1;
2519 #define UNIMPLEMENTED_INFO_CLASS(c) \
2520 case c: \
2521 FIXME("(process=%p,addr=%p) Unimplemented information class: " #c "\n", process, addr); \
2522 return STATUS_INVALID_INFO_CLASS
2524 /***********************************************************************
2525 * NtQueryVirtualMemory (NTDLL.@)
2526 * ZwQueryVirtualMemory (NTDLL.@)
2528 NTSTATUS WINAPI NtQueryVirtualMemory( HANDLE process, LPCVOID addr,
2529 MEMORY_INFORMATION_CLASS info_class, PVOID buffer,
2530 SIZE_T len, SIZE_T *res_len )
2532 struct file_view *view;
2533 char *base, *alloc_base = 0, *alloc_end = working_set_limit;
2534 struct wine_rb_entry *ptr;
2535 MEMORY_BASIC_INFORMATION *info = buffer;
2536 sigset_t sigset;
2538 if (info_class != MemoryBasicInformation)
2540 switch(info_class)
2542 UNIMPLEMENTED_INFO_CLASS(MemoryWorkingSetList);
2543 UNIMPLEMENTED_INFO_CLASS(MemorySectionName);
2544 UNIMPLEMENTED_INFO_CLASS(MemoryBasicVlmInformation);
2546 default:
2547 FIXME("(%p,%p,info_class=%d,%p,%ld,%p) Unknown information class\n",
2548 process, addr, info_class, buffer, len, res_len);
2549 return STATUS_INVALID_INFO_CLASS;
2553 if (process != NtCurrentProcess())
2555 NTSTATUS status;
2556 apc_call_t call;
2557 apc_result_t result;
2559 memset( &call, 0, sizeof(call) );
2561 call.virtual_query.type = APC_VIRTUAL_QUERY;
2562 call.virtual_query.addr = wine_server_client_ptr( addr );
2563 status = server_queue_process_apc( process, &call, &result );
2564 if (status != STATUS_SUCCESS) return status;
2566 if (result.virtual_query.status == STATUS_SUCCESS)
2568 info->BaseAddress = wine_server_get_ptr( result.virtual_query.base );
2569 info->AllocationBase = wine_server_get_ptr( result.virtual_query.alloc_base );
2570 info->RegionSize = result.virtual_query.size;
2571 info->Protect = result.virtual_query.prot;
2572 info->AllocationProtect = result.virtual_query.alloc_prot;
2573 info->State = (DWORD)result.virtual_query.state << 12;
2574 info->Type = (DWORD)result.virtual_query.alloc_type << 16;
2575 if (info->RegionSize != result.virtual_query.size) /* truncated */
2576 return STATUS_INVALID_PARAMETER; /* FIXME */
2577 if (res_len) *res_len = sizeof(*info);
2579 return result.virtual_query.status;
2582 base = ROUND_ADDR( addr, page_mask );
2584 if (is_beyond_limit( base, 1, working_set_limit )) return STATUS_WORKING_SET_LIMIT_RANGE;
2586 /* Find the view containing the address */
2588 server_enter_uninterrupted_section( &csVirtual, &sigset );
2589 ptr = views_tree.root;
2590 while (ptr)
2592 view = WINE_RB_ENTRY_VALUE( ptr, struct file_view, entry );
2593 if ((char *)view->base > base)
2595 alloc_end = view->base;
2596 ptr = ptr->left;
2598 else if ((char *)view->base + view->size <= base)
2600 alloc_base = (char *)view->base + view->size;
2601 ptr = ptr->right;
2603 else
2605 alloc_base = view->base;
2606 alloc_end = (char *)view->base + view->size;
2607 break;
2611 /* Fill the info structure */
2613 info->AllocationBase = alloc_base;
2614 info->BaseAddress = base;
2615 info->RegionSize = alloc_end - base;
2617 if (!ptr)
2619 if (!wine_mmap_enum_reserved_areas( get_free_mem_state_callback, info, 0 ))
2621 /* not in a reserved area at all, pretend it's allocated */
2622 #ifdef __i386__
2623 if (base >= (char *)address_space_start)
2625 info->State = MEM_RESERVE;
2626 info->Protect = PAGE_NOACCESS;
2627 info->AllocationProtect = PAGE_NOACCESS;
2628 info->Type = MEM_PRIVATE;
2630 else
2631 #endif
2633 info->State = MEM_FREE;
2634 info->Protect = PAGE_NOACCESS;
2635 info->AllocationBase = 0;
2636 info->AllocationProtect = 0;
2637 info->Type = 0;
2641 else
2643 BYTE vprot;
2644 char *ptr;
2645 SIZE_T range_size = get_committed_size( view, base, &vprot );
2647 info->State = (vprot & VPROT_COMMITTED) ? MEM_COMMIT : MEM_RESERVE;
2648 info->Protect = (vprot & VPROT_COMMITTED) ? VIRTUAL_GetWin32Prot( vprot, view->protect ) : 0;
2649 info->AllocationProtect = VIRTUAL_GetWin32Prot( view->protect, view->protect );
2650 if (view->protect & SEC_IMAGE) info->Type = MEM_IMAGE;
2651 else if (view->protect & (SEC_FILE | SEC_RESERVE | SEC_COMMIT)) info->Type = MEM_MAPPED;
2652 else info->Type = MEM_PRIVATE;
2653 for (ptr = base; ptr < base + range_size; ptr += page_size)
2654 if ((get_page_vprot( ptr ) ^ vprot) & ~VPROT_WRITEWATCH) break;
2655 info->RegionSize = ptr - base;
2657 server_leave_uninterrupted_section( &csVirtual, &sigset );
2659 if (res_len) *res_len = sizeof(*info);
2660 return STATUS_SUCCESS;
2664 /***********************************************************************
2665 * NtLockVirtualMemory (NTDLL.@)
2666 * ZwLockVirtualMemory (NTDLL.@)
2668 NTSTATUS WINAPI NtLockVirtualMemory( HANDLE process, PVOID *addr, SIZE_T *size, ULONG unknown )
2670 NTSTATUS status = STATUS_SUCCESS;
2672 if (process != NtCurrentProcess())
2674 apc_call_t call;
2675 apc_result_t result;
2677 memset( &call, 0, sizeof(call) );
2679 call.virtual_lock.type = APC_VIRTUAL_LOCK;
2680 call.virtual_lock.addr = wine_server_client_ptr( *addr );
2681 call.virtual_lock.size = *size;
2682 status = server_queue_process_apc( process, &call, &result );
2683 if (status != STATUS_SUCCESS) return status;
2685 if (result.virtual_lock.status == STATUS_SUCCESS)
2687 *addr = wine_server_get_ptr( result.virtual_lock.addr );
2688 *size = result.virtual_lock.size;
2690 return result.virtual_lock.status;
2693 *size = ROUND_SIZE( *addr, *size );
2694 *addr = ROUND_ADDR( *addr, page_mask );
2696 if (mlock( *addr, *size )) status = STATUS_ACCESS_DENIED;
2697 return status;
2701 /***********************************************************************
2702 * NtUnlockVirtualMemory (NTDLL.@)
2703 * ZwUnlockVirtualMemory (NTDLL.@)
2705 NTSTATUS WINAPI NtUnlockVirtualMemory( HANDLE process, PVOID *addr, SIZE_T *size, ULONG unknown )
2707 NTSTATUS status = STATUS_SUCCESS;
2709 if (process != NtCurrentProcess())
2711 apc_call_t call;
2712 apc_result_t result;
2714 memset( &call, 0, sizeof(call) );
2716 call.virtual_unlock.type = APC_VIRTUAL_UNLOCK;
2717 call.virtual_unlock.addr = wine_server_client_ptr( *addr );
2718 call.virtual_unlock.size = *size;
2719 status = server_queue_process_apc( process, &call, &result );
2720 if (status != STATUS_SUCCESS) return status;
2722 if (result.virtual_unlock.status == STATUS_SUCCESS)
2724 *addr = wine_server_get_ptr( result.virtual_unlock.addr );
2725 *size = result.virtual_unlock.size;
2727 return result.virtual_unlock.status;
2730 *size = ROUND_SIZE( *addr, *size );
2731 *addr = ROUND_ADDR( *addr, page_mask );
2733 if (munlock( *addr, *size )) status = STATUS_ACCESS_DENIED;
2734 return status;
2738 /***********************************************************************
2739 * NtCreateSection (NTDLL.@)
2740 * ZwCreateSection (NTDLL.@)
2742 NTSTATUS WINAPI NtCreateSection( HANDLE *handle, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr,
2743 const LARGE_INTEGER *size, ULONG protect,
2744 ULONG sec_flags, HANDLE file )
2746 NTSTATUS ret;
2747 unsigned int vprot, file_access = 0;
2748 data_size_t len;
2749 struct object_attributes *objattr;
2751 if ((ret = get_vprot_flags( protect, &vprot, sec_flags & SEC_IMAGE ))) return ret;
2752 if ((ret = alloc_object_attributes( attr, &objattr, &len ))) return ret;
2754 if (vprot & VPROT_READ) file_access |= FILE_READ_DATA;
2755 if (vprot & VPROT_WRITE) file_access |= FILE_WRITE_DATA;
2757 SERVER_START_REQ( create_mapping )
2759 req->access = access;
2760 req->flags = sec_flags;
2761 req->file_handle = wine_server_obj_handle( file );
2762 req->file_access = file_access;
2763 req->size = size ? size->QuadPart : 0;
2764 wine_server_add_data( req, objattr, len );
2765 ret = wine_server_call( req );
2766 *handle = wine_server_ptr_handle( reply->handle );
2768 SERVER_END_REQ;
2770 RtlFreeHeap( GetProcessHeap(), 0, objattr );
2771 return ret;
2775 /***********************************************************************
2776 * NtOpenSection (NTDLL.@)
2777 * ZwOpenSection (NTDLL.@)
2779 NTSTATUS WINAPI NtOpenSection( HANDLE *handle, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr )
2781 NTSTATUS ret;
2783 if ((ret = validate_open_object_attributes( attr ))) return ret;
2785 SERVER_START_REQ( open_mapping )
2787 req->access = access;
2788 req->attributes = attr->Attributes;
2789 req->rootdir = wine_server_obj_handle( attr->RootDirectory );
2790 if (attr->ObjectName)
2791 wine_server_add_data( req, attr->ObjectName->Buffer, attr->ObjectName->Length );
2792 ret = wine_server_call( req );
2793 *handle = wine_server_ptr_handle( reply->handle );
2795 SERVER_END_REQ;
2796 return ret;
2800 /***********************************************************************
2801 * NtMapViewOfSection (NTDLL.@)
2802 * ZwMapViewOfSection (NTDLL.@)
2804 NTSTATUS WINAPI NtMapViewOfSection( HANDLE handle, HANDLE process, PVOID *addr_ptr, ULONG zero_bits,
2805 SIZE_T commit_size, const LARGE_INTEGER *offset_ptr, SIZE_T *size_ptr,
2806 SECTION_INHERIT inherit, ULONG alloc_type, ULONG protect )
2808 NTSTATUS res;
2809 mem_size_t full_size;
2810 ACCESS_MASK access;
2811 SIZE_T size, mask = get_mask( zero_bits );
2812 int unix_handle = -1, needs_close;
2813 unsigned int vprot, sec_flags;
2814 struct file_view *view;
2815 pe_image_info_t image_info;
2816 HANDLE shared_file;
2817 LARGE_INTEGER offset;
2818 sigset_t sigset;
2820 offset.QuadPart = offset_ptr ? offset_ptr->QuadPart : 0;
2822 TRACE("handle=%p process=%p addr=%p off=%x%08x size=%lx access=%x\n",
2823 handle, process, *addr_ptr, offset.u.HighPart, offset.u.LowPart, *size_ptr, protect );
2825 /* Check parameters */
2827 if ((*addr_ptr && zero_bits) || !mask)
2828 return STATUS_INVALID_PARAMETER_4;
2830 #ifndef _WIN64
2831 if (!is_wow64 && (alloc_type & AT_ROUND_TO_PAGE))
2833 *addr_ptr = ROUND_ADDR( *addr_ptr, page_mask );
2834 mask = page_mask;
2836 #endif
2838 if ((offset.u.LowPart & mask) || (*addr_ptr && ((UINT_PTR)*addr_ptr & mask)))
2839 return STATUS_MAPPED_ALIGNMENT;
2841 switch(protect)
2843 case PAGE_NOACCESS:
2844 case PAGE_READONLY:
2845 case PAGE_WRITECOPY:
2846 access = SECTION_MAP_READ;
2847 break;
2848 case PAGE_READWRITE:
2849 access = SECTION_MAP_WRITE;
2850 break;
2851 case PAGE_EXECUTE:
2852 case PAGE_EXECUTE_READ:
2853 case PAGE_EXECUTE_WRITECOPY:
2854 access = SECTION_MAP_READ | SECTION_MAP_EXECUTE;
2855 break;
2856 case PAGE_EXECUTE_READWRITE:
2857 access = SECTION_MAP_WRITE | SECTION_MAP_EXECUTE;
2858 break;
2859 default:
2860 return STATUS_INVALID_PAGE_PROTECTION;
2863 if (process != NtCurrentProcess())
2865 apc_call_t call;
2866 apc_result_t result;
2868 memset( &call, 0, sizeof(call) );
2870 call.map_view.type = APC_MAP_VIEW;
2871 call.map_view.handle = wine_server_obj_handle( handle );
2872 call.map_view.addr = wine_server_client_ptr( *addr_ptr );
2873 call.map_view.size = *size_ptr;
2874 call.map_view.offset = offset.QuadPart;
2875 call.map_view.zero_bits = zero_bits;
2876 call.map_view.alloc_type = alloc_type;
2877 call.map_view.prot = protect;
2878 res = server_queue_process_apc( process, &call, &result );
2879 if (res != STATUS_SUCCESS) return res;
2881 if ((NTSTATUS)result.map_view.status >= 0)
2883 *addr_ptr = wine_server_get_ptr( result.map_view.addr );
2884 *size_ptr = result.map_view.size;
2886 return result.map_view.status;
2889 SERVER_START_REQ( get_mapping_info )
2891 req->handle = wine_server_obj_handle( handle );
2892 req->access = access;
2893 wine_server_set_reply( req, &image_info, sizeof(image_info) );
2894 res = wine_server_call( req );
2895 sec_flags = reply->flags;
2896 full_size = reply->size;
2897 shared_file = wine_server_ptr_handle( reply->shared_file );
2899 SERVER_END_REQ;
2900 if (res) return res;
2902 if ((res = server_get_unix_fd( handle, 0, &unix_handle, &needs_close, NULL, NULL ))) goto done;
2904 if (sec_flags & SEC_IMAGE)
2906 void *base = wine_server_get_ptr( image_info.base );
2908 if ((ULONG_PTR)base != image_info.base) base = NULL;
2909 size = image_info.map_size;
2910 if (size != image_info.map_size) /* truncated */
2912 WARN( "Modules larger than 4Gb (%s) not supported\n",
2913 wine_dbgstr_longlong(image_info.map_size) );
2914 res = STATUS_INVALID_PARAMETER;
2915 goto done;
2917 if (shared_file)
2919 int shared_fd, shared_needs_close;
2921 if ((res = server_get_unix_fd( shared_file, FILE_READ_DATA|FILE_WRITE_DATA,
2922 &shared_fd, &shared_needs_close, NULL, NULL ))) goto done;
2923 res = map_image( handle, access, unix_handle, base, size, mask, image_info.header_size,
2924 shared_fd, needs_close, addr_ptr );
2925 if (shared_needs_close) close( shared_fd );
2926 close_handle( shared_file );
2928 else
2930 res = map_image( handle, access, unix_handle, base, size, mask, image_info.header_size,
2931 -1, needs_close, addr_ptr );
2933 if (needs_close) close( unix_handle );
2934 if (res >= 0) *size_ptr = size;
2935 return res;
2938 res = STATUS_INVALID_PARAMETER;
2939 if (offset.QuadPart >= full_size) goto done;
2940 if (*size_ptr)
2942 size = *size_ptr;
2943 if (size > full_size - offset.QuadPart)
2945 res = STATUS_INVALID_VIEW_SIZE;
2946 goto done;
2949 else
2951 size = full_size - offset.QuadPart;
2952 if (size != full_size - offset.QuadPart) /* truncated */
2954 WARN( "Files larger than 4Gb (%s) not supported on this platform\n",
2955 wine_dbgstr_longlong(full_size) );
2956 goto done;
2959 if (!(size = ROUND_SIZE( 0, size ))) goto done; /* wrap-around */
2961 /* Reserve a properly aligned area */
2963 server_enter_uninterrupted_section( &csVirtual, &sigset );
2965 get_vprot_flags( protect, &vprot, sec_flags & SEC_IMAGE );
2966 vprot |= sec_flags;
2967 if (!(sec_flags & SEC_RESERVE)) vprot |= VPROT_COMMITTED;
2968 res = map_view( &view, *addr_ptr, size, mask, FALSE, vprot );
2969 if (res)
2971 server_leave_uninterrupted_section( &csVirtual, &sigset );
2972 goto done;
2975 /* Map the file */
2977 TRACE("handle=%p size=%lx offset=%x%08x\n",
2978 handle, size, offset.u.HighPart, offset.u.LowPart );
2980 res = map_file_into_view( view, unix_handle, 0, size, offset.QuadPart, vprot, needs_close );
2981 if (res == STATUS_SUCCESS)
2983 SERVER_START_REQ( map_view )
2985 req->mapping = wine_server_obj_handle( handle );
2986 req->access = access;
2987 req->base = wine_server_client_ptr( view->base );
2988 req->size = size;
2989 req->start = offset.QuadPart;
2990 res = wine_server_call( req );
2992 SERVER_END_REQ;
2995 if (res == STATUS_SUCCESS)
2997 *addr_ptr = view->base;
2998 *size_ptr = size;
2999 VIRTUAL_DEBUG_DUMP_VIEW( view );
3001 else
3003 ERR( "map_file_into_view %p %lx %x%08x failed\n",
3004 view->base, size, offset.u.HighPart, offset.u.LowPart );
3005 delete_view( view );
3008 server_leave_uninterrupted_section( &csVirtual, &sigset );
3010 done:
3011 if (needs_close) close( unix_handle );
3012 return res;
3016 /***********************************************************************
3017 * NtUnmapViewOfSection (NTDLL.@)
3018 * ZwUnmapViewOfSection (NTDLL.@)
3020 NTSTATUS WINAPI NtUnmapViewOfSection( HANDLE process, PVOID addr )
3022 struct file_view *view;
3023 NTSTATUS status = STATUS_NOT_MAPPED_VIEW;
3024 sigset_t sigset;
3026 if (process != NtCurrentProcess())
3028 apc_call_t call;
3029 apc_result_t result;
3031 memset( &call, 0, sizeof(call) );
3033 call.unmap_view.type = APC_UNMAP_VIEW;
3034 call.unmap_view.addr = wine_server_client_ptr( addr );
3035 status = server_queue_process_apc( process, &call, &result );
3036 if (status == STATUS_SUCCESS) status = result.unmap_view.status;
3037 return status;
3040 server_enter_uninterrupted_section( &csVirtual, &sigset );
3041 if ((view = VIRTUAL_FindView( addr, 0 )) && !is_view_valloc( view ))
3043 SERVER_START_REQ( unmap_view )
3045 req->base = wine_server_client_ptr( view->base );
3046 status = wine_server_call( req );
3048 SERVER_END_REQ;
3049 if (!status) delete_view( view );
3051 server_leave_uninterrupted_section( &csVirtual, &sigset );
3052 return status;
3056 /******************************************************************************
3057 * NtQuerySection (NTDLL.@)
3058 * ZwQuerySection (NTDLL.@)
3060 NTSTATUS WINAPI NtQuerySection( HANDLE handle, SECTION_INFORMATION_CLASS class, void *ptr,
3061 ULONG size, ULONG *ret_size )
3063 NTSTATUS status;
3064 pe_image_info_t image_info;
3066 switch (class)
3068 case SectionBasicInformation:
3069 if (size < sizeof(SECTION_BASIC_INFORMATION)) return STATUS_INFO_LENGTH_MISMATCH;
3070 break;
3071 case SectionImageInformation:
3072 if (size < sizeof(SECTION_IMAGE_INFORMATION)) return STATUS_INFO_LENGTH_MISMATCH;
3073 break;
3074 default:
3075 FIXME( "class %u not implemented\n", class );
3076 return STATUS_NOT_IMPLEMENTED;
3078 if (!ptr) return STATUS_ACCESS_VIOLATION;
3080 SERVER_START_REQ( get_mapping_info )
3082 req->handle = wine_server_obj_handle( handle );
3083 req->access = SECTION_QUERY;
3084 wine_server_set_reply( req, &image_info, sizeof(image_info) );
3085 if (!(status = wine_server_call( req )))
3087 if (class == SectionBasicInformation)
3089 SECTION_BASIC_INFORMATION *info = ptr;
3090 info->Attributes = reply->flags;
3091 info->BaseAddress = NULL;
3092 info->Size.QuadPart = reply->size;
3093 if (ret_size) *ret_size = sizeof(*info);
3095 else if (reply->flags & SEC_IMAGE)
3097 SECTION_IMAGE_INFORMATION *info = ptr;
3098 info->TransferAddress = wine_server_get_ptr( image_info.entry_point );
3099 info->ZeroBits = image_info.zerobits;
3100 info->MaximumStackSize = image_info.stack_size;
3101 info->CommittedStackSize = image_info.stack_commit;
3102 info->SubSystemType = image_info.subsystem;
3103 info->SubsystemVersionLow = image_info.subsystem_low;
3104 info->SubsystemVersionHigh = image_info.subsystem_high;
3105 info->GpValue = image_info.gp;
3106 info->ImageCharacteristics = image_info.image_charact;
3107 info->DllCharacteristics = image_info.dll_charact;
3108 info->Machine = image_info.machine;
3109 info->ImageContainsCode = image_info.contains_code;
3110 info->ImageFlags = image_info.image_flags;
3111 info->LoaderFlags = image_info.loader_flags;
3112 info->ImageFileSize = image_info.file_size;
3113 info->CheckSum = image_info.checksum;
3114 if (ret_size) *ret_size = sizeof(*info);
3116 else status = STATUS_SECTION_NOT_IMAGE;
3119 SERVER_END_REQ;
3121 return status;
3125 /***********************************************************************
3126 * NtFlushVirtualMemory (NTDLL.@)
3127 * ZwFlushVirtualMemory (NTDLL.@)
3129 NTSTATUS WINAPI NtFlushVirtualMemory( HANDLE process, LPCVOID *addr_ptr,
3130 SIZE_T *size_ptr, ULONG unknown )
3132 struct file_view *view;
3133 NTSTATUS status = STATUS_SUCCESS;
3134 sigset_t sigset;
3135 void *addr = ROUND_ADDR( *addr_ptr, page_mask );
3137 if (process != NtCurrentProcess())
3139 apc_call_t call;
3140 apc_result_t result;
3142 memset( &call, 0, sizeof(call) );
3144 call.virtual_flush.type = APC_VIRTUAL_FLUSH;
3145 call.virtual_flush.addr = wine_server_client_ptr( addr );
3146 call.virtual_flush.size = *size_ptr;
3147 status = server_queue_process_apc( process, &call, &result );
3148 if (status != STATUS_SUCCESS) return status;
3150 if (result.virtual_flush.status == STATUS_SUCCESS)
3152 *addr_ptr = wine_server_get_ptr( result.virtual_flush.addr );
3153 *size_ptr = result.virtual_flush.size;
3155 return result.virtual_flush.status;
3158 server_enter_uninterrupted_section( &csVirtual, &sigset );
3159 if (!(view = VIRTUAL_FindView( addr, *size_ptr ))) status = STATUS_INVALID_PARAMETER;
3160 else
3162 if (!*size_ptr) *size_ptr = view->size;
3163 *addr_ptr = addr;
3164 #ifdef MS_ASYNC
3165 if (msync( addr, *size_ptr, MS_ASYNC )) status = STATUS_NOT_MAPPED_DATA;
3166 #endif
3168 server_leave_uninterrupted_section( &csVirtual, &sigset );
3169 return status;
3173 /***********************************************************************
3174 * NtGetWriteWatch (NTDLL.@)
3175 * ZwGetWriteWatch (NTDLL.@)
3177 NTSTATUS WINAPI NtGetWriteWatch( HANDLE process, ULONG flags, PVOID base, SIZE_T size, PVOID *addresses,
3178 ULONG_PTR *count, ULONG *granularity )
3180 NTSTATUS status = STATUS_SUCCESS;
3181 sigset_t sigset;
3183 size = ROUND_SIZE( base, size );
3184 base = ROUND_ADDR( base, page_mask );
3186 if (!count || !granularity) return STATUS_ACCESS_VIOLATION;
3187 if (!*count || !size) return STATUS_INVALID_PARAMETER;
3188 if (flags & ~WRITE_WATCH_FLAG_RESET) return STATUS_INVALID_PARAMETER;
3190 if (!addresses) return STATUS_ACCESS_VIOLATION;
3192 TRACE( "%p %x %p-%p %p %lu\n", process, flags, base, (char *)base + size,
3193 addresses, *count );
3195 server_enter_uninterrupted_section( &csVirtual, &sigset );
3197 if (is_write_watch_range( base, size ))
3199 ULONG_PTR pos = 0;
3200 char *addr = base;
3201 char *end = addr + size;
3203 while (pos < *count && addr < end)
3205 if (!(get_page_vprot( addr ) & VPROT_WRITEWATCH)) addresses[pos++] = addr;
3206 addr += page_size;
3208 if (flags & WRITE_WATCH_FLAG_RESET) reset_write_watches( base, addr - (char *)base );
3209 *count = pos;
3210 *granularity = page_size;
3212 else status = STATUS_INVALID_PARAMETER;
3214 server_leave_uninterrupted_section( &csVirtual, &sigset );
3215 return status;
3219 /***********************************************************************
3220 * NtResetWriteWatch (NTDLL.@)
3221 * ZwResetWriteWatch (NTDLL.@)
3223 NTSTATUS WINAPI NtResetWriteWatch( HANDLE process, PVOID base, SIZE_T size )
3225 NTSTATUS status = STATUS_SUCCESS;
3226 sigset_t sigset;
3228 size = ROUND_SIZE( base, size );
3229 base = ROUND_ADDR( base, page_mask );
3231 TRACE( "%p %p-%p\n", process, base, (char *)base + size );
3233 if (!size) return STATUS_INVALID_PARAMETER;
3235 server_enter_uninterrupted_section( &csVirtual, &sigset );
3237 if (is_write_watch_range( base, size ))
3238 reset_write_watches( base, size );
3239 else
3240 status = STATUS_INVALID_PARAMETER;
3242 server_leave_uninterrupted_section( &csVirtual, &sigset );
3243 return status;
3247 /***********************************************************************
3248 * NtReadVirtualMemory (NTDLL.@)
3249 * ZwReadVirtualMemory (NTDLL.@)
3251 NTSTATUS WINAPI NtReadVirtualMemory( HANDLE process, const void *addr, void *buffer,
3252 SIZE_T size, SIZE_T *bytes_read )
3254 NTSTATUS status;
3256 if (virtual_check_buffer_for_write( buffer, size ))
3258 SERVER_START_REQ( read_process_memory )
3260 req->handle = wine_server_obj_handle( process );
3261 req->addr = wine_server_client_ptr( addr );
3262 wine_server_set_reply( req, buffer, size );
3263 if ((status = wine_server_call( req ))) size = 0;
3265 SERVER_END_REQ;
3267 else
3269 status = STATUS_ACCESS_VIOLATION;
3270 size = 0;
3272 if (bytes_read) *bytes_read = size;
3273 return status;
3277 /***********************************************************************
3278 * NtWriteVirtualMemory (NTDLL.@)
3279 * ZwWriteVirtualMemory (NTDLL.@)
3281 NTSTATUS WINAPI NtWriteVirtualMemory( HANDLE process, void *addr, const void *buffer,
3282 SIZE_T size, SIZE_T *bytes_written )
3284 NTSTATUS status;
3286 if (virtual_check_buffer_for_read( buffer, size ))
3288 SERVER_START_REQ( write_process_memory )
3290 req->handle = wine_server_obj_handle( process );
3291 req->addr = wine_server_client_ptr( addr );
3292 wine_server_add_data( req, buffer, size );
3293 if ((status = wine_server_call( req ))) size = 0;
3295 SERVER_END_REQ;
3297 else
3299 status = STATUS_PARTIAL_COPY;
3300 size = 0;
3302 if (bytes_written) *bytes_written = size;
3303 return status;
3307 /***********************************************************************
3308 * NtAreMappedFilesTheSame (NTDLL.@)
3309 * ZwAreMappedFilesTheSame (NTDLL.@)
3311 NTSTATUS WINAPI NtAreMappedFilesTheSame(PVOID addr1, PVOID addr2)
3313 struct file_view *view1, *view2;
3314 NTSTATUS status;
3315 sigset_t sigset;
3317 TRACE("%p %p\n", addr1, addr2);
3319 server_enter_uninterrupted_section( &csVirtual, &sigset );
3321 view1 = VIRTUAL_FindView( addr1, 0 );
3322 view2 = VIRTUAL_FindView( addr2, 0 );
3324 if (!view1 || !view2)
3325 status = STATUS_INVALID_ADDRESS;
3326 else if (is_view_valloc( view1 ) || is_view_valloc( view2 ))
3327 status = STATUS_CONFLICTING_ADDRESSES;
3328 else if (view1 == view2)
3329 status = STATUS_SUCCESS;
3330 else if ((view1->protect & VPROT_SYSTEM) || (view2->protect & VPROT_SYSTEM))
3331 status = STATUS_NOT_SAME_DEVICE;
3332 else
3334 SERVER_START_REQ( is_same_mapping )
3336 req->base1 = wine_server_client_ptr( view1->base );
3337 req->base2 = wine_server_client_ptr( view2->base );
3338 status = wine_server_call( req );
3340 SERVER_END_REQ;
3343 server_leave_uninterrupted_section( &csVirtual, &sigset );
3344 return status;