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
22 #include "wine/port.h"
34 #include <sys/types.h>
35 #ifdef HAVE_SYS_STAT_H
36 # include <sys/stat.h>
38 #ifdef HAVE_SYS_MMAN_H
39 # include <sys/mman.h>
41 #ifdef HAVE_VALGRIND_VALGRIND_H
42 # include <valgrind/valgrind.h>
45 #define NONAMELESSUNION
46 #define NONAMELESSSTRUCT
48 #define WIN32_NO_STATUS
51 #include "wine/library.h"
52 #include "wine/server.h"
53 #include "wine/exception.h"
54 #include "wine/list.h"
55 #include "wine/debug.h"
56 #include "ntdll_misc.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(virtual);
59 WINE_DECLARE_DEBUG_CHANNEL(module
);
62 #define MAP_NORESERVE 0
68 struct list entry
; /* Entry in global view list */
69 void *base
; /* Base address */
70 size_t size
; /* Size in bytes */
71 HANDLE mapping
; /* Handle to the file mapping */
72 unsigned int map_protect
; /* Mapping protection */
73 unsigned int protect
; /* Protection for all pages at allocation time */
74 BYTE prot
[1]; /* Protection byte for each page */
78 /* Conversion from VPROT_* to Win32 flags */
79 static const BYTE VIRTUAL_Win32Flags
[16] =
81 PAGE_NOACCESS
, /* 0 */
82 PAGE_READONLY
, /* READ */
83 PAGE_READWRITE
, /* WRITE */
84 PAGE_READWRITE
, /* READ | WRITE */
85 PAGE_EXECUTE
, /* EXEC */
86 PAGE_EXECUTE_READ
, /* READ | EXEC */
87 PAGE_EXECUTE_READWRITE
, /* WRITE | EXEC */
88 PAGE_EXECUTE_READWRITE
, /* READ | WRITE | EXEC */
89 PAGE_WRITECOPY
, /* WRITECOPY */
90 PAGE_WRITECOPY
, /* READ | WRITECOPY */
91 PAGE_WRITECOPY
, /* WRITE | WRITECOPY */
92 PAGE_WRITECOPY
, /* READ | WRITE | WRITECOPY */
93 PAGE_EXECUTE_WRITECOPY
, /* EXEC | WRITECOPY */
94 PAGE_EXECUTE_WRITECOPY
, /* READ | EXEC | WRITECOPY */
95 PAGE_EXECUTE_WRITECOPY
, /* WRITE | EXEC | WRITECOPY */
96 PAGE_EXECUTE_WRITECOPY
/* READ | WRITE | EXEC | WRITECOPY */
99 static struct list views_list
= LIST_INIT(views_list
);
101 static RTL_CRITICAL_SECTION csVirtual
;
102 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
105 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
106 0, 0, { (DWORD_PTR
)(__FILE__
": csVirtual") }
108 static RTL_CRITICAL_SECTION csVirtual
= { &critsect_debug
, -1, 0, 0, 0, 0 };
111 static const UINT page_shift
= 12;
112 static const UINT_PTR page_mask
= 0xfff;
113 /* Note: these are Windows limits, you cannot change them. */
114 static void *address_space_limit
= (void *)0xc0000000; /* top of the total available address space */
115 static void *user_space_limit
= (void *)0x7fff0000; /* top of the user address space */
116 static void *working_set_limit
= (void *)0x7fff0000; /* top of the current working set */
117 static void *address_space_start
= (void *)0x110000; /* keep DOS area clear */
118 #elif defined(__x86_64__)
119 static const UINT page_shift
= 12;
120 static const UINT_PTR page_mask
= 0xfff;
121 static void *address_space_limit
= (void *)0x7fffffff0000;
122 static void *user_space_limit
= (void *)0x7fffffff0000;
123 static void *working_set_limit
= (void *)0x7fffffff0000;
124 static void *address_space_start
= (void *)0x10000;
126 UINT_PTR page_size
= 0;
127 static UINT page_shift
;
128 static UINT_PTR page_mask
;
129 static void *address_space_limit
;
130 static void *user_space_limit
;
131 static void *working_set_limit
;
132 static void *address_space_start
= (void *)0x10000;
133 #endif /* __i386__ */
134 static const BOOL is_win64
= (sizeof(void *) > sizeof(int));
136 #define ROUND_ADDR(addr,mask) \
137 ((void *)((UINT_PTR)(addr) & ~(UINT_PTR)(mask)))
139 #define ROUND_SIZE(addr,size) \
140 (((SIZE_T)(size) + ((UINT_PTR)(addr) & page_mask) + page_mask) & ~page_mask)
142 #define VIRTUAL_DEBUG_DUMP_VIEW(view) \
143 do { if (TRACE_ON(virtual)) VIRTUAL_DumpView(view); } while (0)
145 #define VIRTUAL_HEAP_SIZE (sizeof(void*)*1024*1024)
147 static HANDLE virtual_heap
;
148 static void *preload_reserve_start
;
149 static void *preload_reserve_end
;
150 static BOOL use_locks
;
151 static BOOL force_exec_prot
; /* whether to force PROT_EXEC on all PROT_READ mmaps */
154 /***********************************************************************
157 static const char *VIRTUAL_GetProtStr( BYTE prot
)
159 static char buffer
[6];
160 buffer
[0] = (prot
& VPROT_COMMITTED
) ? 'c' : '-';
161 buffer
[1] = (prot
& VPROT_GUARD
) ? 'g' : ((prot
& VPROT_WRITEWATCH
) ? 'H' : '-');
162 buffer
[2] = (prot
& VPROT_READ
) ? 'r' : '-';
163 buffer
[3] = (prot
& VPROT_WRITECOPY
) ? 'W' : ((prot
& VPROT_WRITE
) ? 'w' : '-');
164 buffer
[4] = (prot
& VPROT_EXEC
) ? 'x' : '-';
170 /***********************************************************************
171 * VIRTUAL_GetUnixProt
173 * Convert page protections to protection for mmap/mprotect.
175 static int VIRTUAL_GetUnixProt( BYTE vprot
)
178 if ((vprot
& VPROT_COMMITTED
) && !(vprot
& VPROT_GUARD
))
180 if (vprot
& VPROT_READ
) prot
|= PROT_READ
;
181 if (vprot
& VPROT_WRITE
) prot
|= PROT_WRITE
| PROT_READ
;
182 if (vprot
& VPROT_WRITECOPY
) prot
|= PROT_WRITE
| PROT_READ
;
183 if (vprot
& VPROT_EXEC
) prot
|= PROT_EXEC
| PROT_READ
;
184 if (vprot
& VPROT_WRITEWATCH
) prot
&= ~PROT_WRITE
;
186 if (!prot
) prot
= PROT_NONE
;
191 /***********************************************************************
194 static void VIRTUAL_DumpView( struct file_view
*view
)
197 char *addr
= view
->base
;
198 BYTE prot
= view
->prot
[0];
200 TRACE( "View: %p - %p", addr
, addr
+ view
->size
- 1 );
201 if (view
->protect
& VPROT_SYSTEM
)
202 TRACE( " (system)\n" );
203 else if (view
->protect
& VPROT_VALLOC
)
204 TRACE( " (valloc)\n" );
205 else if (view
->mapping
)
206 TRACE( " %p\n", view
->mapping
);
208 TRACE( " (anonymous)\n");
210 for (count
= i
= 1; i
< view
->size
>> page_shift
; i
++, count
++)
212 if (view
->prot
[i
] == prot
) continue;
213 TRACE( " %p - %p %s\n",
214 addr
, addr
+ (count
<< page_shift
) - 1, VIRTUAL_GetProtStr(prot
) );
215 addr
+= (count
<< page_shift
);
216 prot
= view
->prot
[i
];
220 TRACE( " %p - %p %s\n",
221 addr
, addr
+ (count
<< page_shift
) - 1, VIRTUAL_GetProtStr(prot
) );
225 /***********************************************************************
229 static void VIRTUAL_Dump(void)
232 struct file_view
*view
;
234 TRACE( "Dump of all virtual memory views:\n" );
235 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
236 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
238 VIRTUAL_DumpView( view
);
240 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
245 /***********************************************************************
248 * Find the view containing a given address. The csVirtual section must be held by caller.
257 static struct file_view
*VIRTUAL_FindView( const void *addr
, size_t size
)
259 struct file_view
*view
;
261 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
263 if (view
->base
> addr
) break; /* no matching view */
264 if ((const char *)view
->base
+ view
->size
<= (const char *)addr
) continue;
265 if ((const char *)view
->base
+ view
->size
< (const char *)addr
+ size
) break; /* size too large */
266 if ((const char *)addr
+ size
< (const char *)addr
) break; /* overflow */
273 /***********************************************************************
276 static inline UINT_PTR
get_mask( ULONG zero_bits
)
278 if (!zero_bits
) return 0xffff; /* allocations are aligned to 64K by default */
279 if (zero_bits
< page_shift
) zero_bits
= page_shift
;
280 return (1 << zero_bits
) - 1;
284 /***********************************************************************
287 * Find the first view overlapping at least part of the specified range.
288 * The csVirtual section must be held by caller.
290 static struct file_view
*find_view_range( const void *addr
, size_t size
)
292 struct file_view
*view
;
294 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
296 if ((const char *)view
->base
>= (const char *)addr
+ size
) break;
297 if ((const char *)view
->base
+ view
->size
> (const char *)addr
) return view
;
303 /***********************************************************************
306 * Find a free area between views inside the specified range.
307 * The csVirtual section must be held by caller.
309 static void *find_free_area( void *base
, void *end
, size_t size
, size_t mask
, int top_down
)
316 start
= ROUND_ADDR( (char *)end
- size
, mask
);
317 if (start
>= end
|| start
< base
) return NULL
;
319 for (ptr
= views_list
.prev
; ptr
!= &views_list
; ptr
= ptr
->prev
)
321 struct file_view
*view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
323 if ((char *)view
->base
+ view
->size
<= (char *)start
) break;
324 if ((char *)view
->base
>= (char *)start
+ size
) continue;
325 start
= ROUND_ADDR( (char *)view
->base
- size
, mask
);
326 /* stop if remaining space is not large enough */
327 if (!start
|| start
>= end
|| start
< base
) return NULL
;
332 start
= ROUND_ADDR( (char *)base
+ mask
, mask
);
333 if (start
>= end
|| (char *)end
- (char *)start
< size
) return NULL
;
335 for (ptr
= views_list
.next
; ptr
!= &views_list
; ptr
= ptr
->next
)
337 struct file_view
*view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
339 if ((char *)view
->base
>= (char *)start
+ size
) break;
340 if ((char *)view
->base
+ view
->size
<= (char *)start
) continue;
341 start
= ROUND_ADDR( (char *)view
->base
+ view
->size
+ mask
, mask
);
342 /* stop if remaining space is not large enough */
343 if (!start
|| start
>= end
|| (char *)end
- (char *)start
< size
) return NULL
;
350 /***********************************************************************
353 * Add a reserved area to the list maintained by libwine.
354 * The csVirtual section must be held by caller.
356 static void add_reserved_area( void *addr
, size_t size
)
358 TRACE( "adding %p-%p\n", addr
, (char *)addr
+ size
);
360 if (addr
< user_space_limit
)
362 /* unmap the part of the area that is below the limit */
363 assert( (char *)addr
+ size
> (char *)user_space_limit
);
364 munmap( addr
, (char *)user_space_limit
- (char *)addr
);
365 size
-= (char *)user_space_limit
- (char *)addr
;
366 addr
= user_space_limit
;
368 /* blow away existing mappings */
369 wine_anon_mmap( addr
, size
, PROT_NONE
, MAP_NORESERVE
| MAP_FIXED
);
370 wine_mmap_add_reserved_area( addr
, size
);
374 /***********************************************************************
375 * remove_reserved_area
377 * Remove a reserved area from the list maintained by libwine.
378 * The csVirtual section must be held by caller.
380 static void remove_reserved_area( void *addr
, size_t size
)
382 struct file_view
*view
;
384 TRACE( "removing %p-%p\n", addr
, (char *)addr
+ size
);
385 wine_mmap_remove_reserved_area( addr
, size
, 0 );
387 /* unmap areas not covered by an existing view */
388 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
390 if ((char *)view
->base
>= (char *)addr
+ size
)
392 munmap( addr
, size
);
395 if ((char *)view
->base
+ view
->size
<= (char *)addr
) continue;
396 if (view
->base
> addr
) munmap( addr
, (char *)view
->base
- (char *)addr
);
397 if ((char *)view
->base
+ view
->size
> (char *)addr
+ size
) break;
398 size
= (char *)addr
+ size
- ((char *)view
->base
+ view
->size
);
399 addr
= (char *)view
->base
+ view
->size
;
404 /***********************************************************************
407 * Check if an address range goes beyond a given limit.
409 static inline BOOL
is_beyond_limit( const void *addr
, size_t size
, const void *limit
)
411 return (addr
>= limit
|| (const char *)addr
+ size
> (const char *)limit
);
415 /***********************************************************************
418 * Unmap an area, or simply replace it by an empty mapping if it is
419 * in a reserved area. The csVirtual section must be held by caller.
421 static inline void unmap_area( void *addr
, size_t size
)
423 if (wine_mmap_is_in_reserved_area( addr
, size
))
424 wine_anon_mmap( addr
, size
, PROT_NONE
, MAP_NORESERVE
| MAP_FIXED
);
425 else if (is_beyond_limit( addr
, size
, user_space_limit
))
426 add_reserved_area( addr
, size
);
428 munmap( addr
, size
);
432 /***********************************************************************
435 * Deletes a view. The csVirtual section must be held by caller.
437 static void delete_view( struct file_view
*view
) /* [in] View */
439 if (!(view
->protect
& VPROT_SYSTEM
)) unmap_area( view
->base
, view
->size
);
440 list_remove( &view
->entry
);
441 if (view
->mapping
) close_handle( view
->mapping
);
442 RtlFreeHeap( virtual_heap
, 0, view
);
446 /***********************************************************************
449 * Create a view. The csVirtual section must be held by caller.
451 static NTSTATUS
create_view( struct file_view
**view_ret
, void *base
, size_t size
, unsigned int vprot
)
453 struct file_view
*view
;
455 int unix_prot
= VIRTUAL_GetUnixProt( vprot
);
457 assert( !((UINT_PTR
)base
& page_mask
) );
458 assert( !(size
& page_mask
) );
460 /* Create the view structure */
462 if (!(view
= RtlAllocateHeap( virtual_heap
, 0, sizeof(*view
) + (size
>> page_shift
) - 1 )))
464 FIXME( "out of memory in virtual heap for %p-%p\n", base
, (char *)base
+ size
);
465 return STATUS_NO_MEMORY
;
471 view
->map_protect
= 0;
472 view
->protect
= vprot
;
473 memset( view
->prot
, vprot
, size
>> page_shift
);
475 /* Insert it in the linked list */
477 LIST_FOR_EACH( ptr
, &views_list
)
479 struct file_view
*next
= LIST_ENTRY( ptr
, struct file_view
, entry
);
480 if (next
->base
> base
) break;
482 list_add_before( ptr
, &view
->entry
);
484 /* Check for overlapping views. This can happen if the previous view
485 * was a system view that got unmapped behind our back. In that case
486 * we recover by simply deleting it. */
488 if ((ptr
= list_prev( &views_list
, &view
->entry
)) != NULL
)
490 struct file_view
*prev
= LIST_ENTRY( ptr
, struct file_view
, entry
);
491 if ((char *)prev
->base
+ prev
->size
> (char *)base
)
493 TRACE( "overlapping prev view %p-%p for %p-%p\n",
494 prev
->base
, (char *)prev
->base
+ prev
->size
,
495 base
, (char *)base
+ view
->size
);
496 assert( prev
->protect
& VPROT_SYSTEM
);
500 if ((ptr
= list_next( &views_list
, &view
->entry
)) != NULL
)
502 struct file_view
*next
= LIST_ENTRY( ptr
, struct file_view
, entry
);
503 if ((char *)base
+ view
->size
> (char *)next
->base
)
505 TRACE( "overlapping next view %p-%p for %p-%p\n",
506 next
->base
, (char *)next
->base
+ next
->size
,
507 base
, (char *)base
+ view
->size
);
508 assert( next
->protect
& VPROT_SYSTEM
);
514 VIRTUAL_DEBUG_DUMP_VIEW( view
);
516 if (force_exec_prot
&& !(vprot
& VPROT_NOEXEC
) && (unix_prot
& PROT_READ
) && !(unix_prot
& PROT_EXEC
))
518 TRACE( "forcing exec permission on %p-%p\n", base
, (char *)base
+ size
- 1 );
519 mprotect( base
, size
, unix_prot
| PROT_EXEC
);
521 return STATUS_SUCCESS
;
525 /***********************************************************************
526 * VIRTUAL_GetWin32Prot
528 * Convert page protections to Win32 flags.
530 static DWORD
VIRTUAL_GetWin32Prot( BYTE vprot
)
532 DWORD ret
= VIRTUAL_Win32Flags
[vprot
& 0x0f];
533 if (vprot
& VPROT_NOCACHE
) ret
|= PAGE_NOCACHE
;
534 if (vprot
& VPROT_GUARD
) ret
|= PAGE_GUARD
;
539 /***********************************************************************
542 * Build page protections from Win32 flags.
545 * protect [I] Win32 protection flags
548 * Value of page protection flags
550 static NTSTATUS
get_vprot_flags( DWORD protect
, unsigned int *vprot
, BOOL image
)
552 switch(protect
& 0xff)
559 *vprot
= VPROT_READ
| VPROT_WRITECOPY
;
561 *vprot
= VPROT_READ
| VPROT_WRITE
;
564 *vprot
= VPROT_READ
| VPROT_WRITECOPY
;
569 case PAGE_EXECUTE_READ
:
570 *vprot
= VPROT_EXEC
| VPROT_READ
;
572 case PAGE_EXECUTE_READWRITE
:
574 *vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITECOPY
;
576 *vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITE
;
578 case PAGE_EXECUTE_WRITECOPY
:
579 *vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITECOPY
;
585 return STATUS_INVALID_PAGE_PROTECTION
;
587 if (protect
& PAGE_GUARD
) *vprot
|= VPROT_GUARD
;
588 if (protect
& PAGE_NOCACHE
) *vprot
|= VPROT_NOCACHE
;
589 return STATUS_SUCCESS
;
593 /***********************************************************************
596 * Change the protection of a range of pages.
602 static BOOL
VIRTUAL_SetProt( struct file_view
*view
, /* [in] Pointer to view */
603 void *base
, /* [in] Starting address */
604 size_t size
, /* [in] Size in bytes */
605 BYTE vprot
) /* [in] Protections to use */
607 int unix_prot
= VIRTUAL_GetUnixProt(vprot
);
608 BYTE
*p
= view
->prot
+ (((char *)base
- (char *)view
->base
) >> page_shift
);
611 base
, (char *)base
+ size
- 1, VIRTUAL_GetProtStr( vprot
) );
613 if (view
->protect
& VPROT_WRITEWATCH
)
615 /* each page may need different protections depending on write watch flag */
620 p
[0] = vprot
| (p
[0] & VPROT_WRITEWATCH
);
621 unix_prot
= VIRTUAL_GetUnixProt( p
[0] );
622 for (count
= i
= 1; i
< size
>> page_shift
; i
++, count
++)
624 p
[i
] = vprot
| (p
[i
] & VPROT_WRITEWATCH
);
625 prot
= VIRTUAL_GetUnixProt( p
[i
] );
626 if (prot
== unix_prot
) continue;
627 mprotect( addr
, count
<< page_shift
, unix_prot
);
628 addr
+= count
<< page_shift
;
632 if (count
) mprotect( addr
, count
<< page_shift
, unix_prot
);
633 VIRTUAL_DEBUG_DUMP_VIEW( view
);
637 /* if setting stack guard pages, store the permissions first, as the guard may be
638 * triggered at any point after mprotect and change the permissions again */
639 if ((vprot
& VPROT_GUARD
) &&
640 (base
>= NtCurrentTeb()->DeallocationStack
) &&
641 (base
< NtCurrentTeb()->Tib
.StackBase
))
643 memset( p
, vprot
, size
>> page_shift
);
644 mprotect( base
, size
, unix_prot
);
645 VIRTUAL_DEBUG_DUMP_VIEW( view
);
649 if (force_exec_prot
&& !(view
->protect
& VPROT_NOEXEC
) &&
650 (unix_prot
& PROT_READ
) && !(unix_prot
& PROT_EXEC
))
652 TRACE( "forcing exec permission on %p-%p\n", base
, (char *)base
+ size
- 1 );
653 if (!mprotect( base
, size
, unix_prot
| PROT_EXEC
)) goto done
;
654 /* exec + write may legitimately fail, in that case fall back to write only */
655 if (!(unix_prot
& PROT_WRITE
)) return FALSE
;
658 if (mprotect( base
, size
, unix_prot
)) return FALSE
; /* FIXME: last error */
661 memset( p
, vprot
, size
>> page_shift
);
662 VIRTUAL_DEBUG_DUMP_VIEW( view
);
667 /***********************************************************************
668 * reset_write_watches
670 * Reset write watches in a memory range.
672 static void reset_write_watches( struct file_view
*view
, void *base
, SIZE_T size
)
677 BYTE
*p
= view
->prot
+ ((addr
- (char *)view
->base
) >> page_shift
);
679 p
[0] |= VPROT_WRITEWATCH
;
680 unix_prot
= VIRTUAL_GetUnixProt( p
[0] );
681 for (count
= i
= 1; i
< size
>> page_shift
; i
++, count
++)
683 p
[i
] |= VPROT_WRITEWATCH
;
684 prot
= VIRTUAL_GetUnixProt( p
[i
] );
685 if (prot
== unix_prot
) continue;
686 mprotect( addr
, count
<< page_shift
, unix_prot
);
687 addr
+= count
<< page_shift
;
691 if (count
) mprotect( addr
, count
<< page_shift
, unix_prot
);
695 /***********************************************************************
698 * Release the extra memory while keeping the range starting on the granularity boundary.
700 static inline void *unmap_extra_space( void *ptr
, size_t total_size
, size_t wanted_size
, size_t mask
)
702 if ((ULONG_PTR
)ptr
& mask
)
704 size_t extra
= mask
+ 1 - ((ULONG_PTR
)ptr
& mask
);
705 munmap( ptr
, extra
);
706 ptr
= (char *)ptr
+ extra
;
709 if (total_size
> wanted_size
)
710 munmap( (char *)ptr
+ wanted_size
, total_size
- wanted_size
);
724 /***********************************************************************
725 * alloc_reserved_area_callback
727 * Try to map some space inside a reserved area. Callback for wine_mmap_enum_reserved_areas.
729 static int alloc_reserved_area_callback( void *start
, size_t size
, void *arg
)
731 struct alloc_area
*alloc
= arg
;
732 void *end
= (char *)start
+ size
;
734 if (start
< address_space_start
) start
= address_space_start
;
735 if (is_beyond_limit( start
, size
, alloc
->limit
)) end
= alloc
->limit
;
736 if (start
>= end
) return 0;
738 /* make sure we don't touch the preloader reserved range */
739 if (preload_reserve_end
>= start
)
741 if (preload_reserve_end
>= end
)
743 if (preload_reserve_start
<= start
) return 0; /* no space in that area */
744 if (preload_reserve_start
< end
) end
= preload_reserve_start
;
746 else if (preload_reserve_start
<= start
) start
= preload_reserve_end
;
749 /* range is split in two by the preloader reservation, try first part */
750 if ((alloc
->result
= find_free_area( start
, preload_reserve_start
, alloc
->size
,
751 alloc
->mask
, alloc
->top_down
)))
753 /* then fall through to try second part */
754 start
= preload_reserve_end
;
757 if ((alloc
->result
= find_free_area( start
, end
, alloc
->size
, alloc
->mask
, alloc
->top_down
)))
764 /***********************************************************************
767 * Create a view and mmap the corresponding memory area.
768 * The csVirtual section must be held by caller.
770 static NTSTATUS
map_view( struct file_view
**view_ret
, void *base
, size_t size
, size_t mask
,
771 int top_down
, unsigned int vprot
)
778 if (is_beyond_limit( base
, size
, address_space_limit
))
779 return STATUS_WORKING_SET_LIMIT_RANGE
;
781 switch (wine_mmap_is_in_reserved_area( base
, size
))
783 case -1: /* partially in a reserved area */
784 return STATUS_CONFLICTING_ADDRESSES
;
786 case 0: /* not in a reserved area, do a normal allocation */
787 if ((ptr
= wine_anon_mmap( base
, size
, VIRTUAL_GetUnixProt(vprot
), 0 )) == (void *)-1)
789 if (errno
== ENOMEM
) return STATUS_NO_MEMORY
;
790 return STATUS_INVALID_PARAMETER
;
794 /* We couldn't get the address we wanted */
795 if (is_beyond_limit( ptr
, size
, user_space_limit
)) add_reserved_area( ptr
, size
);
796 else munmap( ptr
, size
);
797 return STATUS_CONFLICTING_ADDRESSES
;
802 case 1: /* in a reserved area, make sure the address is available */
803 if (find_view_range( base
, size
)) return STATUS_CONFLICTING_ADDRESSES
;
804 /* replace the reserved area by our mapping */
805 if ((ptr
= wine_anon_mmap( base
, size
, VIRTUAL_GetUnixProt(vprot
), MAP_FIXED
)) != base
)
806 return STATUS_INVALID_PARAMETER
;
809 if (is_beyond_limit( ptr
, size
, working_set_limit
)) working_set_limit
= address_space_limit
;
813 size_t view_size
= size
+ mask
+ 1;
814 struct alloc_area alloc
;
818 alloc
.top_down
= top_down
;
819 alloc
.limit
= user_space_limit
;
820 if (wine_mmap_enum_reserved_areas( alloc_reserved_area_callback
, &alloc
, top_down
))
823 TRACE( "got mem in reserved area %p-%p\n", ptr
, (char *)ptr
+ size
);
824 if (wine_anon_mmap( ptr
, size
, VIRTUAL_GetUnixProt(vprot
), MAP_FIXED
) != ptr
)
825 return STATUS_INVALID_PARAMETER
;
831 if ((ptr
= wine_anon_mmap( NULL
, view_size
, VIRTUAL_GetUnixProt(vprot
), 0 )) == (void *)-1)
833 if (errno
== ENOMEM
) return STATUS_NO_MEMORY
;
834 return STATUS_INVALID_PARAMETER
;
836 TRACE( "got mem with anon mmap %p-%p\n", ptr
, (char *)ptr
+ size
);
837 /* if we got something beyond the user limit, unmap it and retry */
838 if (is_beyond_limit( ptr
, view_size
, user_space_limit
)) add_reserved_area( ptr
, view_size
);
841 ptr
= unmap_extra_space( ptr
, view_size
, size
, mask
);
844 status
= create_view( view_ret
, ptr
, size
, vprot
);
845 if (status
!= STATUS_SUCCESS
) unmap_area( ptr
, size
);
850 /***********************************************************************
853 * Wrapper for mmap() to map a file into a view, falling back to read if mmap fails.
854 * The csVirtual section must be held by caller.
856 static NTSTATUS
map_file_into_view( struct file_view
*view
, int fd
, size_t start
, size_t size
,
857 off_t offset
, unsigned int vprot
, BOOL removable
)
860 int prot
= VIRTUAL_GetUnixProt( vprot
| VPROT_COMMITTED
/* make sure it is accessible */ );
861 unsigned int flags
= MAP_FIXED
| ((vprot
& VPROT_WRITECOPY
) ? MAP_PRIVATE
: MAP_SHARED
);
863 assert( start
< view
->size
);
864 assert( start
+ size
<= view
->size
);
866 if (force_exec_prot
&& !(vprot
& VPROT_NOEXEC
) && (vprot
& VPROT_READ
))
868 TRACE( "forcing exec permission on mapping %p-%p\n",
869 (char *)view
->base
+ start
, (char *)view
->base
+ start
+ size
- 1 );
873 /* only try mmap if media is not removable (or if we require write access) */
874 if (!removable
|| (flags
& MAP_SHARED
))
876 if (mmap( (char *)view
->base
+ start
, size
, prot
, flags
, fd
, offset
) != (void *)-1)
879 if ((errno
== EPERM
) && (prot
& PROT_EXEC
))
880 ERR( "failed to set %08x protection on file map, noexec filesystem?\n", prot
);
882 /* mmap() failed; if this is because the file offset is not */
883 /* page-aligned (EINVAL), or because the underlying filesystem */
884 /* does not support mmap() (ENOEXEC,ENODEV), we do it by hand. */
885 if ((errno
!= ENOEXEC
) && (errno
!= EINVAL
) && (errno
!= ENODEV
)) return FILE_GetNtStatus();
886 if (flags
& MAP_SHARED
) /* we cannot fake shared mappings */
888 if (errno
== EINVAL
) return STATUS_INVALID_PARAMETER
;
889 ERR( "shared writable mmap not supported, broken filesystem?\n" );
890 return STATUS_NOT_SUPPORTED
;
894 /* Reserve the memory with an anonymous mmap */
895 ptr
= wine_anon_mmap( (char *)view
->base
+ start
, size
, PROT_READ
| PROT_WRITE
, MAP_FIXED
);
896 if (ptr
== (void *)-1) return FILE_GetNtStatus();
897 /* Now read in the file */
898 pread( fd
, ptr
, size
, offset
);
899 if (prot
!= (PROT_READ
|PROT_WRITE
)) mprotect( ptr
, size
, prot
); /* Set the right protection */
901 memset( view
->prot
+ (start
>> page_shift
), vprot
, ROUND_SIZE(start
,size
) >> page_shift
);
902 return STATUS_SUCCESS
;
906 /***********************************************************************
909 * Get the size of the committed range starting at base.
910 * Also return the protections for the first page.
912 static SIZE_T
get_committed_size( struct file_view
*view
, void *base
, BYTE
*vprot
)
916 start
= ((char *)base
- (char *)view
->base
) >> page_shift
;
917 *vprot
= view
->prot
[start
];
919 if (view
->mapping
&& !(view
->protect
& VPROT_COMMITTED
))
922 SERVER_START_REQ( get_mapping_committed_range
)
924 req
->handle
= wine_server_obj_handle( view
->mapping
);
925 req
->offset
= start
<< page_shift
;
926 if (!wine_server_call( req
))
929 if (reply
->committed
)
931 *vprot
|= VPROT_COMMITTED
;
932 for (i
= 0; i
< ret
>> page_shift
; i
++) view
->prot
[start
+i
] |= VPROT_COMMITTED
;
939 for (i
= start
+ 1; i
< view
->size
>> page_shift
; i
++)
940 if ((*vprot
^ view
->prot
[i
]) & VPROT_COMMITTED
) break;
941 return (i
- start
) << page_shift
;
945 /***********************************************************************
948 * Decommit some pages of a given view.
949 * The csVirtual section must be held by caller.
951 static NTSTATUS
decommit_pages( struct file_view
*view
, size_t start
, size_t size
)
953 if (wine_anon_mmap( (char *)view
->base
+ start
, size
, PROT_NONE
, MAP_FIXED
) != (void *)-1)
955 BYTE
*p
= view
->prot
+ (start
>> page_shift
);
957 while (size
--) *p
++ &= ~VPROT_COMMITTED
;
958 return STATUS_SUCCESS
;
960 return FILE_GetNtStatus();
964 /***********************************************************************
965 * allocate_dos_memory
967 * Allocate the DOS memory range.
969 static NTSTATUS
allocate_dos_memory( struct file_view
**view
, unsigned int vprot
)
973 void * const low_64k
= (void *)0x10000;
974 const size_t dosmem_size
= 0x110000;
975 int unix_prot
= VIRTUAL_GetUnixProt( vprot
);
978 /* check for existing view */
980 if ((ptr
= list_head( &views_list
)))
982 struct file_view
*first_view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
983 if (first_view
->base
< (void *)dosmem_size
) return STATUS_CONFLICTING_ADDRESSES
;
986 /* check without the first 64K */
988 if (wine_mmap_is_in_reserved_area( low_64k
, dosmem_size
- 0x10000 ) != 1)
990 addr
= wine_anon_mmap( low_64k
, dosmem_size
- 0x10000, unix_prot
, 0 );
993 if (addr
!= (void *)-1) munmap( addr
, dosmem_size
- 0x10000 );
994 return map_view( view
, NULL
, dosmem_size
, 0xffff, 0, vprot
);
998 /* now try to allocate the low 64K too */
1000 if (wine_mmap_is_in_reserved_area( NULL
, 0x10000 ) != 1)
1002 addr
= wine_anon_mmap( (void *)page_size
, 0x10000 - page_size
, unix_prot
, 0 );
1003 if (addr
== (void *)page_size
)
1005 if (!wine_anon_mmap( NULL
, page_size
, unix_prot
, MAP_FIXED
))
1008 TRACE( "successfully mapped low 64K range\n" );
1010 else TRACE( "failed to map page 0\n" );
1014 if (addr
!= (void *)-1) munmap( addr
, 0x10000 - page_size
);
1016 TRACE( "failed to map low 64K range\n" );
1020 /* now reserve the whole range */
1022 size
= (char *)dosmem_size
- (char *)addr
;
1023 wine_anon_mmap( addr
, size
, unix_prot
, MAP_FIXED
);
1024 return create_view( view
, addr
, size
, vprot
);
1028 /***********************************************************************
1031 * Stat the underlying file for a memory view.
1033 static NTSTATUS
stat_mapping_file( struct file_view
*view
, struct stat
*st
)
1036 int unix_fd
, needs_close
;
1038 if (!view
->mapping
) return STATUS_NOT_MAPPED_VIEW
;
1039 if (!(status
= server_get_unix_fd( view
->mapping
, 0, &unix_fd
, &needs_close
, NULL
, NULL
)))
1041 if (fstat( unix_fd
, st
) == -1) status
= FILE_GetNtStatus();
1042 if (needs_close
) close( unix_fd
);
1048 /***********************************************************************
1051 * Map an executable (PE format) image into memory.
1053 static NTSTATUS
map_image( HANDLE hmapping
, int fd
, char *base
, SIZE_T total_size
, SIZE_T mask
,
1054 SIZE_T header_size
, int shared_fd
, HANDLE dup_mapping
, unsigned int map_vprot
, PVOID
*addr_ptr
)
1056 IMAGE_DOS_HEADER
*dos
;
1057 IMAGE_NT_HEADERS
*nt
;
1058 IMAGE_SECTION_HEADER sections
[96];
1059 IMAGE_SECTION_HEADER
*sec
;
1060 IMAGE_DATA_DIRECTORY
*imports
;
1061 NTSTATUS status
= STATUS_CONFLICTING_ADDRESSES
;
1066 struct file_view
*view
= NULL
;
1067 char *ptr
, *header_end
, *header_start
;
1070 /* zero-map the whole range */
1072 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1074 if (base
>= (char *)address_space_start
) /* make sure the DOS area remains free */
1075 status
= map_view( &view
, base
, total_size
, mask
, FALSE
,
1076 VPROT_COMMITTED
| VPROT_READ
| VPROT_EXEC
| VPROT_WRITECOPY
| VPROT_IMAGE
);
1078 if (status
!= STATUS_SUCCESS
)
1079 status
= map_view( &view
, NULL
, total_size
, mask
, FALSE
,
1080 VPROT_COMMITTED
| VPROT_READ
| VPROT_EXEC
| VPROT_WRITECOPY
| VPROT_IMAGE
);
1082 if (status
!= STATUS_SUCCESS
) goto error
;
1085 TRACE_(module
)( "mapped PE file at %p-%p\n", ptr
, ptr
+ total_size
);
1087 /* map the header */
1089 if (fstat( fd
, &st
) == -1)
1091 status
= FILE_GetNtStatus();
1094 status
= STATUS_INVALID_IMAGE_FORMAT
; /* generic error */
1095 if (!st
.st_size
) goto error
;
1096 header_size
= min( header_size
, st
.st_size
);
1097 if (map_file_into_view( view
, fd
, 0, header_size
, 0, VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
,
1098 !dup_mapping
) != STATUS_SUCCESS
) goto error
;
1099 dos
= (IMAGE_DOS_HEADER
*)ptr
;
1100 nt
= (IMAGE_NT_HEADERS
*)(ptr
+ dos
->e_lfanew
);
1101 header_end
= ptr
+ ROUND_SIZE( 0, header_size
);
1102 memset( ptr
+ header_size
, 0, header_end
- (ptr
+ header_size
) );
1103 if ((char *)(nt
+ 1) > header_end
) goto error
;
1104 header_start
= (char*)&nt
->OptionalHeader
+nt
->FileHeader
.SizeOfOptionalHeader
;
1105 if (nt
->FileHeader
.NumberOfSections
> sizeof(sections
)/sizeof(*sections
)) goto error
;
1106 if (header_start
+ sizeof(*sections
) * nt
->FileHeader
.NumberOfSections
> header_end
) goto error
;
1107 /* Some applications (e.g. the Steam version of Borderlands) map over the top of the section headers,
1108 * copying the headers into local memory is necessary to properly load such applications. */
1109 memcpy(sections
, header_start
, sizeof(*sections
) * nt
->FileHeader
.NumberOfSections
);
1112 imports
= nt
->OptionalHeader
.DataDirectory
+ IMAGE_DIRECTORY_ENTRY_IMPORT
;
1113 if (!imports
->Size
|| !imports
->VirtualAddress
) imports
= NULL
;
1115 /* check for non page-aligned binary */
1117 if (nt
->OptionalHeader
.SectionAlignment
<= page_mask
)
1119 /* unaligned sections, this happens for native subsystem binaries */
1120 /* in that case Windows simply maps in the whole file */
1122 if (map_file_into_view( view
, fd
, 0, total_size
, 0, VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
,
1123 !dup_mapping
) != STATUS_SUCCESS
) goto error
;
1125 /* check that all sections are loaded at the right offset */
1126 if (nt
->OptionalHeader
.FileAlignment
!= nt
->OptionalHeader
.SectionAlignment
) goto error
;
1127 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++)
1129 if (sec
[i
].VirtualAddress
!= sec
[i
].PointerToRawData
)
1130 goto error
; /* Windows refuses to load in that case too */
1133 /* set the image protections */
1134 VIRTUAL_SetProt( view
, ptr
, total_size
,
1135 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
| VPROT_EXEC
);
1137 /* no relocations are performed on non page-aligned binaries */
1142 /* map all the sections */
1144 for (i
= pos
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
1146 static const SIZE_T sector_align
= 0x1ff;
1147 SIZE_T map_size
, file_start
, file_size
, end
;
1149 if (!sec
->Misc
.VirtualSize
)
1150 map_size
= ROUND_SIZE( 0, sec
->SizeOfRawData
);
1152 map_size
= ROUND_SIZE( 0, sec
->Misc
.VirtualSize
);
1154 /* file positions are rounded to sector boundaries regardless of OptionalHeader.FileAlignment */
1155 file_start
= sec
->PointerToRawData
& ~sector_align
;
1156 file_size
= (sec
->SizeOfRawData
+ (sec
->PointerToRawData
& sector_align
) + sector_align
) & ~sector_align
;
1157 if (file_size
> map_size
) file_size
= map_size
;
1159 /* a few sanity checks */
1160 end
= sec
->VirtualAddress
+ ROUND_SIZE( sec
->VirtualAddress
, map_size
);
1161 if (sec
->VirtualAddress
> total_size
|| end
> total_size
|| end
< sec
->VirtualAddress
)
1163 WARN_(module
)( "Section %.8s too large (%x+%lx/%lx)\n",
1164 sec
->Name
, sec
->VirtualAddress
, map_size
, total_size
);
1168 if ((sec
->Characteristics
& IMAGE_SCN_MEM_SHARED
) &&
1169 (sec
->Characteristics
& IMAGE_SCN_MEM_WRITE
))
1171 TRACE_(module
)( "mapping shared section %.8s at %p off %x (%x) size %lx (%lx) flags %x\n",
1172 sec
->Name
, ptr
+ sec
->VirtualAddress
,
1173 sec
->PointerToRawData
, (int)pos
, file_size
, map_size
,
1174 sec
->Characteristics
);
1175 if (map_file_into_view( view
, shared_fd
, sec
->VirtualAddress
, map_size
, pos
,
1176 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITE
, FALSE
) != STATUS_SUCCESS
)
1178 ERR_(module
)( "Could not map shared section %.8s\n", sec
->Name
);
1182 /* check if the import directory falls inside this section */
1183 if (imports
&& imports
->VirtualAddress
>= sec
->VirtualAddress
&&
1184 imports
->VirtualAddress
< sec
->VirtualAddress
+ map_size
)
1186 UINT_PTR base
= imports
->VirtualAddress
& ~page_mask
;
1187 UINT_PTR end
= base
+ ROUND_SIZE( imports
->VirtualAddress
, imports
->Size
);
1188 if (end
> sec
->VirtualAddress
+ map_size
) end
= sec
->VirtualAddress
+ map_size
;
1190 map_file_into_view( view
, shared_fd
, base
, end
- base
,
1191 pos
+ (base
- sec
->VirtualAddress
),
1192 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
, FALSE
);
1198 TRACE_(module
)( "mapping section %.8s at %p off %x size %x virt %x flags %x\n",
1199 sec
->Name
, ptr
+ sec
->VirtualAddress
,
1200 sec
->PointerToRawData
, sec
->SizeOfRawData
,
1201 sec
->Misc
.VirtualSize
, sec
->Characteristics
);
1203 if (!sec
->PointerToRawData
|| !file_size
) continue;
1205 /* Note: if the section is not aligned properly map_file_into_view will magically
1206 * fall back to read(), so we don't need to check anything here.
1208 end
= file_start
+ file_size
;
1209 if (sec
->PointerToRawData
>= st
.st_size
||
1210 end
> ((st
.st_size
+ sector_align
) & ~sector_align
) ||
1212 map_file_into_view( view
, fd
, sec
->VirtualAddress
, file_size
, file_start
,
1213 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
,
1214 !dup_mapping
) != STATUS_SUCCESS
)
1216 ERR_(module
)( "Could not map section %.8s, file probably truncated\n", sec
->Name
);
1220 if (file_size
& page_mask
)
1222 end
= ROUND_SIZE( 0, file_size
);
1223 if (end
> map_size
) end
= map_size
;
1224 TRACE_(module
)("clearing %p - %p\n",
1225 ptr
+ sec
->VirtualAddress
+ file_size
,
1226 ptr
+ sec
->VirtualAddress
+ end
);
1227 memset( ptr
+ sec
->VirtualAddress
+ file_size
, 0, end
- file_size
);
1232 /* perform base relocation, if necessary */
1235 ((nt
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
) ||
1236 !NtCurrentTeb()->Peb
->ImageBaseAddress
) )
1238 IMAGE_BASE_RELOCATION
*rel
, *end
;
1239 const IMAGE_DATA_DIRECTORY
*relocs
;
1241 if (nt
->FileHeader
.Characteristics
& IMAGE_FILE_RELOCS_STRIPPED
)
1243 WARN_(module
)( "Need to relocate module from %p to %p, but there are no relocation records\n",
1245 status
= STATUS_CONFLICTING_ADDRESSES
;
1249 TRACE_(module
)( "relocating from %p-%p to %p-%p\n",
1250 base
, base
+ total_size
, ptr
, ptr
+ total_size
);
1252 relocs
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1253 rel
= (IMAGE_BASE_RELOCATION
*)(ptr
+ relocs
->VirtualAddress
);
1254 end
= (IMAGE_BASE_RELOCATION
*)(ptr
+ relocs
->VirtualAddress
+ relocs
->Size
);
1257 while (rel
< end
- 1 && rel
->SizeOfBlock
)
1259 if (rel
->VirtualAddress
>= total_size
)
1261 WARN_(module
)( "invalid address %p in relocation %p\n", ptr
+ rel
->VirtualAddress
, rel
);
1262 status
= STATUS_ACCESS_VIOLATION
;
1265 rel
= LdrProcessRelocationBlock( ptr
+ rel
->VirtualAddress
,
1266 (rel
->SizeOfBlock
- sizeof(*rel
)) / sizeof(USHORT
),
1267 (USHORT
*)(rel
+ 1), delta
);
1268 if (!rel
) goto error
;
1272 /* set the image protections */
1274 VIRTUAL_SetProt( view
, ptr
, ROUND_SIZE( 0, header_size
), VPROT_COMMITTED
| VPROT_READ
);
1277 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
1280 BYTE vprot
= VPROT_COMMITTED
;
1282 if (sec
->Misc
.VirtualSize
)
1283 size
= ROUND_SIZE( sec
->VirtualAddress
, sec
->Misc
.VirtualSize
);
1285 size
= ROUND_SIZE( sec
->VirtualAddress
, sec
->SizeOfRawData
);
1287 if (sec
->Characteristics
& IMAGE_SCN_MEM_READ
) vprot
|= VPROT_READ
;
1288 if (sec
->Characteristics
& IMAGE_SCN_MEM_WRITE
) vprot
|= VPROT_WRITECOPY
;
1289 if (sec
->Characteristics
& IMAGE_SCN_MEM_EXECUTE
) vprot
|= VPROT_EXEC
;
1291 /* Dumb game crack lets the AOEP point into a data section. Adjust. */
1292 if ((nt
->OptionalHeader
.AddressOfEntryPoint
>= sec
->VirtualAddress
) &&
1293 (nt
->OptionalHeader
.AddressOfEntryPoint
< sec
->VirtualAddress
+ size
))
1294 vprot
|= VPROT_EXEC
;
1296 if (!VIRTUAL_SetProt( view
, ptr
+ sec
->VirtualAddress
, size
, vprot
) && (vprot
& VPROT_EXEC
))
1297 ERR( "failed to set %08x protection on section %.8s, noexec filesystem?\n",
1298 sec
->Characteristics
, sec
->Name
);
1302 view
->mapping
= dup_mapping
;
1303 view
->map_protect
= map_vprot
;
1304 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1307 #ifdef VALGRIND_LOAD_PDB_DEBUGINFO
1308 VALGRIND_LOAD_PDB_DEBUGINFO(fd
, ptr
, total_size
, delta
);
1310 if (ptr
!= base
) return STATUS_IMAGE_NOT_AT_BASE
;
1311 return STATUS_SUCCESS
;
1314 if (view
) delete_view( view
);
1315 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1316 if (dup_mapping
) NtClose( dup_mapping
);
1321 /* callback for wine_mmap_enum_reserved_areas to allocate space for the virtual heap */
1322 static int alloc_virtual_heap( void *base
, size_t size
, void *arg
)
1324 void **heap_base
= arg
;
1326 if (is_beyond_limit( base
, size
, address_space_limit
)) address_space_limit
= (char *)base
+ size
;
1327 if (size
< VIRTUAL_HEAP_SIZE
) return 0;
1328 if (is_win64
&& base
< (void *)0x80000000) return 0;
1329 *heap_base
= wine_anon_mmap( (char *)base
+ size
- VIRTUAL_HEAP_SIZE
,
1330 VIRTUAL_HEAP_SIZE
, PROT_READ
|PROT_WRITE
, MAP_FIXED
);
1331 return (*heap_base
!= (void *)-1);
1334 /***********************************************************************
1337 void virtual_init(void)
1339 const char *preload
;
1342 struct file_view
*heap_view
;
1344 #if !defined(__i386__) && !defined(__x86_64__)
1345 page_size
= sysconf( _SC_PAGESIZE
);
1346 page_mask
= page_size
- 1;
1347 /* Make sure we have a power of 2 */
1348 assert( !(page_size
& page_mask
) );
1350 while ((1 << page_shift
) != page_size
) page_shift
++;
1351 user_space_limit
= working_set_limit
= address_space_limit
= (void *)~page_mask
;
1352 #endif /* page_mask */
1353 if ((preload
= getenv("WINEPRELOADRESERVE")))
1355 unsigned long start
, end
;
1356 if (sscanf( preload
, "%lx-%lx", &start
, &end
) == 2)
1358 preload_reserve_start
= (void *)start
;
1359 preload_reserve_end
= (void *)end
;
1363 /* try to find space in a reserved area for the virtual heap */
1364 if (!wine_mmap_enum_reserved_areas( alloc_virtual_heap
, &heap_base
, 1 ))
1365 heap_base
= wine_anon_mmap( NULL
, VIRTUAL_HEAP_SIZE
, PROT_READ
|PROT_WRITE
, 0 );
1367 assert( heap_base
!= (void *)-1 );
1368 virtual_heap
= RtlCreateHeap( HEAP_NO_SERIALIZE
, heap_base
, VIRTUAL_HEAP_SIZE
,
1369 VIRTUAL_HEAP_SIZE
, NULL
, NULL
);
1370 create_view( &heap_view
, heap_base
, VIRTUAL_HEAP_SIZE
, VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITE
);
1372 /* make the DOS area accessible (except the low 64K) to hide bugs in broken apps like Excel 2003 */
1373 size
= (char *)address_space_start
- (char *)0x10000;
1374 if (size
&& wine_mmap_is_in_reserved_area( (void*)0x10000, size
) == 1)
1375 wine_anon_mmap( (void *)0x10000, size
, PROT_READ
| PROT_WRITE
, MAP_FIXED
);
1379 /***********************************************************************
1380 * virtual_init_threading
1382 void virtual_init_threading(void)
1388 /***********************************************************************
1389 * virtual_get_system_info
1391 void virtual_get_system_info( SYSTEM_BASIC_INFORMATION
*info
)
1394 info
->KeMaximumIncrement
= 0; /* FIXME */
1395 info
->PageSize
= page_size
;
1396 info
->MmLowestPhysicalPage
= 1;
1397 info
->MmHighestPhysicalPage
= 0x7fffffff / page_size
;
1398 info
->MmNumberOfPhysicalPages
= info
->MmHighestPhysicalPage
- info
->MmLowestPhysicalPage
;
1399 info
->AllocationGranularity
= get_mask(0) + 1;
1400 info
->LowestUserAddress
= (void *)0x10000;
1401 info
->HighestUserAddress
= (char *)user_space_limit
- 1;
1402 info
->ActiveProcessorsAffinityMask
= (1 << NtCurrentTeb()->Peb
->NumberOfProcessors
) - 1;
1403 info
->NumberOfProcessors
= NtCurrentTeb()->Peb
->NumberOfProcessors
;
1407 /***********************************************************************
1408 * virtual_create_builtin_view
1410 NTSTATUS
virtual_create_builtin_view( void *module
)
1414 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( module
);
1415 SIZE_T size
= nt
->OptionalHeader
.SizeOfImage
;
1416 IMAGE_SECTION_HEADER
*sec
;
1417 struct file_view
*view
;
1421 size
= ROUND_SIZE( module
, size
);
1422 base
= ROUND_ADDR( module
, page_mask
);
1423 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1424 status
= create_view( &view
, base
, size
, VPROT_SYSTEM
| VPROT_IMAGE
|
1425 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
| VPROT_EXEC
);
1426 if (!status
) TRACE( "created %p-%p\n", base
, (char *)base
+ size
);
1427 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1429 if (status
) return status
;
1431 /* The PE header is always read-only, no write, no execute. */
1432 view
->prot
[0] = VPROT_COMMITTED
| VPROT_READ
;
1434 sec
= (IMAGE_SECTION_HEADER
*)((char *)&nt
->OptionalHeader
+ nt
->FileHeader
.SizeOfOptionalHeader
);
1435 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++)
1437 BYTE flags
= VPROT_COMMITTED
;
1439 if (sec
[i
].Characteristics
& IMAGE_SCN_MEM_EXECUTE
) flags
|= VPROT_EXEC
;
1440 if (sec
[i
].Characteristics
& IMAGE_SCN_MEM_READ
) flags
|= VPROT_READ
;
1441 if (sec
[i
].Characteristics
& IMAGE_SCN_MEM_WRITE
) flags
|= VPROT_WRITE
;
1442 memset (view
->prot
+ (sec
[i
].VirtualAddress
>> page_shift
), flags
,
1443 ROUND_SIZE( sec
[i
].VirtualAddress
, sec
[i
].Misc
.VirtualSize
) >> page_shift
);
1450 /***********************************************************************
1451 * virtual_alloc_thread_stack
1453 NTSTATUS
virtual_alloc_thread_stack( TEB
*teb
, SIZE_T reserve_size
, SIZE_T commit_size
)
1455 struct file_view
*view
;
1460 if (!reserve_size
|| !commit_size
)
1462 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( NtCurrentTeb()->Peb
->ImageBaseAddress
);
1463 if (!reserve_size
) reserve_size
= nt
->OptionalHeader
.SizeOfStackReserve
;
1464 if (!commit_size
) commit_size
= nt
->OptionalHeader
.SizeOfStackCommit
;
1467 size
= max( reserve_size
, commit_size
);
1468 if (size
< 1024 * 1024) size
= 1024 * 1024; /* Xlib needs a large stack */
1469 size
= (size
+ 0xffff) & ~0xffff; /* round to 64K boundary */
1471 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1473 if ((status
= map_view( &view
, NULL
, size
, 0xffff, 0,
1474 VPROT_READ
| VPROT_WRITE
| VPROT_COMMITTED
| VPROT_VALLOC
)) != STATUS_SUCCESS
)
1477 #ifdef VALGRIND_STACK_REGISTER
1478 VALGRIND_STACK_REGISTER( view
->base
, (char *)view
->base
+ view
->size
);
1481 /* setup no access guard page */
1482 VIRTUAL_SetProt( view
, view
->base
, page_size
, VPROT_COMMITTED
);
1483 VIRTUAL_SetProt( view
, (char *)view
->base
+ page_size
, page_size
,
1484 VPROT_READ
| VPROT_WRITE
| VPROT_COMMITTED
| VPROT_GUARD
);
1486 /* note: limit is lower than base since the stack grows down */
1487 teb
->DeallocationStack
= view
->base
;
1488 teb
->Tib
.StackBase
= (char *)view
->base
+ view
->size
;
1489 teb
->Tib
.StackLimit
= (char *)view
->base
+ 2 * page_size
;
1491 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1496 /***********************************************************************
1497 * virtual_clear_thread_stack
1499 * Clear the stack contents before calling the main entry point, some broken apps need that.
1501 void virtual_clear_thread_stack(void)
1503 void *stack
= NtCurrentTeb()->Tib
.StackLimit
;
1504 size_t size
= (char *)NtCurrentTeb()->Tib
.StackBase
- (char *)NtCurrentTeb()->Tib
.StackLimit
;
1506 wine_anon_mmap( stack
, size
, PROT_READ
| PROT_WRITE
, MAP_FIXED
);
1507 if (force_exec_prot
) mprotect( stack
, size
, PROT_READ
| PROT_WRITE
| PROT_EXEC
);
1511 /***********************************************************************
1512 * virtual_handle_fault
1514 NTSTATUS
virtual_handle_fault( LPCVOID addr
, DWORD err
)
1516 struct file_view
*view
;
1517 NTSTATUS ret
= STATUS_ACCESS_VIOLATION
;
1520 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1521 if ((view
= VIRTUAL_FindView( addr
, 0 )))
1523 void *page
= ROUND_ADDR( addr
, page_mask
);
1524 BYTE
*vprot
= &view
->prot
[((const char *)page
- (const char *)view
->base
) >> page_shift
];
1525 if (*vprot
& VPROT_GUARD
)
1527 VIRTUAL_SetProt( view
, page
, page_size
, *vprot
& ~VPROT_GUARD
);
1528 ret
= STATUS_GUARD_PAGE_VIOLATION
;
1530 if ((err
& EXCEPTION_WRITE_FAULT
) && (view
->protect
& VPROT_WRITEWATCH
))
1532 if (*vprot
& VPROT_WRITEWATCH
)
1534 *vprot
&= ~VPROT_WRITEWATCH
;
1535 VIRTUAL_SetProt( view
, page
, page_size
, *vprot
);
1537 /* ignore fault if page is writable now */
1538 if (VIRTUAL_GetUnixProt( *vprot
) & PROT_WRITE
) ret
= STATUS_SUCCESS
;
1541 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1547 /***********************************************************************
1548 * virtual_is_valid_code_address
1550 BOOL
virtual_is_valid_code_address( const void *addr
, SIZE_T size
)
1552 struct file_view
*view
;
1556 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1557 if ((view
= VIRTUAL_FindView( addr
, size
)))
1558 ret
= !(view
->protect
& VPROT_SYSTEM
); /* system views are not visible to the app */
1559 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1564 /***********************************************************************
1565 * virtual_handle_stack_fault
1567 * Handle an access fault inside the current thread stack.
1568 * Called from inside a signal handler.
1570 BOOL
virtual_handle_stack_fault( void *addr
)
1572 struct file_view
*view
;
1575 RtlEnterCriticalSection( &csVirtual
); /* no need for signal masking inside signal handler */
1576 if ((view
= VIRTUAL_FindView( addr
, 0 )))
1578 void *page
= ROUND_ADDR( addr
, page_mask
);
1579 BYTE vprot
= view
->prot
[((const char *)page
- (const char *)view
->base
) >> page_shift
];
1580 if (vprot
& VPROT_GUARD
)
1582 VIRTUAL_SetProt( view
, page
, page_size
, vprot
& ~VPROT_GUARD
);
1583 NtCurrentTeb()->Tib
.StackLimit
= page
;
1584 if ((char *)page
>= (char *)NtCurrentTeb()->DeallocationStack
+ 2*page_size
)
1586 vprot
= view
->prot
[((char *)page
- page_size
- (char *)view
->base
) >> page_shift
];
1587 VIRTUAL_SetProt( view
, (char *)page
- page_size
, page_size
, vprot
| VPROT_GUARD
);
1592 RtlLeaveCriticalSection( &csVirtual
);
1597 /***********************************************************************
1598 * virtual_check_buffer_for_read
1600 * Check if a memory buffer can be read, triggering page faults if needed for DIB section access.
1602 BOOL
virtual_check_buffer_for_read( const void *ptr
, SIZE_T size
)
1604 if (!size
) return TRUE
;
1605 if (!ptr
) return FALSE
;
1609 volatile const char *p
= ptr
;
1610 char dummy
__attribute__((unused
));
1611 SIZE_T count
= size
;
1613 while (count
> page_size
)
1620 dummy
= p
[count
- 1];
1631 /***********************************************************************
1632 * virtual_check_buffer_for_write
1634 * Check if a memory buffer can be written to, triggering page faults if needed for write watches.
1636 BOOL
virtual_check_buffer_for_write( void *ptr
, SIZE_T size
)
1638 if (!size
) return TRUE
;
1639 if (!ptr
) return FALSE
;
1643 volatile char *p
= ptr
;
1644 SIZE_T count
= size
;
1646 while (count
> page_size
)
1664 /***********************************************************************
1665 * VIRTUAL_SetForceExec
1667 * Whether to force exec prot on all views.
1669 void VIRTUAL_SetForceExec( BOOL enable
)
1671 struct file_view
*view
;
1674 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1675 if (!force_exec_prot
!= !enable
) /* change all existing views */
1677 force_exec_prot
= enable
;
1679 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
1682 char *addr
= view
->base
;
1683 BYTE commit
= view
->mapping
? VPROT_COMMITTED
: 0; /* file mappings are always accessible */
1684 int unix_prot
= VIRTUAL_GetUnixProt( view
->prot
[0] | commit
);
1686 if (view
->protect
& VPROT_NOEXEC
) continue;
1687 for (count
= i
= 1; i
< view
->size
>> page_shift
; i
++, count
++)
1689 int prot
= VIRTUAL_GetUnixProt( view
->prot
[i
] | commit
);
1690 if (prot
== unix_prot
) continue;
1691 if ((unix_prot
& PROT_READ
) && !(unix_prot
& PROT_EXEC
))
1693 TRACE( "%s exec prot for %p-%p\n",
1694 force_exec_prot
? "enabling" : "disabling",
1695 addr
, addr
+ (count
<< page_shift
) - 1 );
1696 mprotect( addr
, count
<< page_shift
,
1697 unix_prot
| (force_exec_prot
? PROT_EXEC
: 0) );
1699 addr
+= (count
<< page_shift
);
1705 if ((unix_prot
& PROT_READ
) && !(unix_prot
& PROT_EXEC
))
1707 TRACE( "%s exec prot for %p-%p\n",
1708 force_exec_prot
? "enabling" : "disabling",
1709 addr
, addr
+ (count
<< page_shift
) - 1 );
1710 mprotect( addr
, count
<< page_shift
,
1711 unix_prot
| (force_exec_prot
? PROT_EXEC
: 0) );
1716 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1725 /* free reserved areas above the limit; callback for wine_mmap_enum_reserved_areas */
1726 static int free_reserved_memory( void *base
, size_t size
, void *arg
)
1728 struct free_range
*range
= arg
;
1730 if ((char *)base
>= range
->limit
) return 0;
1731 if ((char *)base
+ size
<= range
->base
) return 0;
1732 if ((char *)base
< range
->base
)
1734 size
-= range
->base
- (char *)base
;
1737 if ((char *)base
+ size
> range
->limit
) size
= range
->limit
- (char *)base
;
1738 remove_reserved_area( base
, size
);
1739 return 1; /* stop enumeration since the list has changed */
1742 /***********************************************************************
1743 * virtual_release_address_space
1745 * Release some address space once we have loaded and initialized the app.
1747 void virtual_release_address_space(void)
1749 struct free_range range
;
1752 if (is_win64
) return;
1754 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1756 range
.base
= (char *)0x82000000;
1757 range
.limit
= user_space_limit
;
1759 if (range
.limit
> range
.base
)
1761 while (wine_mmap_enum_reserved_areas( free_reserved_memory
, &range
, 1 )) /* nothing */;
1765 #ifndef __APPLE__ /* dyld doesn't support parts of the WINE_DOS segment being unmapped */
1766 range
.base
= (char *)0x20000000;
1767 range
.limit
= (char *)0x7f000000;
1768 while (wine_mmap_enum_reserved_areas( free_reserved_memory
, &range
, 0 )) /* nothing */;
1772 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1776 /***********************************************************************
1777 * virtual_set_large_address_space
1779 * Enable use of a large address space when allowed by the application.
1781 void virtual_set_large_address_space(void)
1783 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( NtCurrentTeb()->Peb
->ImageBaseAddress
);
1785 if (!(nt
->FileHeader
.Characteristics
& IMAGE_FILE_LARGE_ADDRESS_AWARE
)) return;
1786 /* no large address space on win9x */
1787 if (NtCurrentTeb()->Peb
->OSPlatformId
!= VER_PLATFORM_WIN32_NT
) return;
1789 user_space_limit
= working_set_limit
= address_space_limit
;
1793 /***********************************************************************
1794 * NtAllocateVirtualMemory (NTDLL.@)
1795 * ZwAllocateVirtualMemory (NTDLL.@)
1797 NTSTATUS WINAPI
NtAllocateVirtualMemory( HANDLE process
, PVOID
*ret
, ULONG zero_bits
,
1798 SIZE_T
*size_ptr
, ULONG type
, ULONG protect
)
1802 SIZE_T size
= *size_ptr
;
1803 SIZE_T mask
= get_mask( zero_bits
);
1804 NTSTATUS status
= STATUS_SUCCESS
;
1805 struct file_view
*view
;
1808 TRACE("%p %p %08lx %x %08x\n", process
, *ret
, size
, type
, protect
);
1810 if (!size
) return STATUS_INVALID_PARAMETER
;
1812 if (process
!= NtCurrentProcess())
1815 apc_result_t result
;
1817 memset( &call
, 0, sizeof(call
) );
1819 call
.virtual_alloc
.type
= APC_VIRTUAL_ALLOC
;
1820 call
.virtual_alloc
.addr
= wine_server_client_ptr( *ret
);
1821 call
.virtual_alloc
.size
= *size_ptr
;
1822 call
.virtual_alloc
.zero_bits
= zero_bits
;
1823 call
.virtual_alloc
.op_type
= type
;
1824 call
.virtual_alloc
.prot
= protect
;
1825 status
= server_queue_process_apc( process
, &call
, &result
);
1826 if (status
!= STATUS_SUCCESS
) return status
;
1828 if (result
.virtual_alloc
.status
== STATUS_SUCCESS
)
1830 *ret
= wine_server_get_ptr( result
.virtual_alloc
.addr
);
1831 *size_ptr
= result
.virtual_alloc
.size
;
1833 return result
.virtual_alloc
.status
;
1836 /* Round parameters to a page boundary */
1838 if (is_beyond_limit( 0, size
, working_set_limit
)) return STATUS_WORKING_SET_LIMIT_RANGE
;
1840 if ((status
= get_vprot_flags( protect
, &vprot
, FALSE
))) return status
;
1841 if (vprot
& VPROT_WRITECOPY
) return STATUS_INVALID_PAGE_PROTECTION
;
1842 vprot
|= VPROT_VALLOC
;
1843 if (type
& MEM_COMMIT
) vprot
|= VPROT_COMMITTED
;
1847 if (type
& MEM_RESERVE
) /* Round down to 64k boundary */
1848 base
= ROUND_ADDR( *ret
, mask
);
1850 base
= ROUND_ADDR( *ret
, page_mask
);
1851 size
= (((UINT_PTR
)*ret
+ size
+ page_mask
) & ~page_mask
) - (UINT_PTR
)base
;
1853 /* address 1 is magic to mean DOS area */
1854 if (!base
&& *ret
== (void *)1 && size
== 0x110000)
1856 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1857 status
= allocate_dos_memory( &view
, vprot
);
1858 if (status
== STATUS_SUCCESS
)
1861 *size_ptr
= view
->size
;
1863 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1867 /* disallow low 64k, wrap-around and kernel space */
1868 if (((char *)base
< (char *)0x10000) ||
1869 ((char *)base
+ size
< (char *)base
) ||
1870 is_beyond_limit( base
, size
, address_space_limit
))
1871 return STATUS_INVALID_PARAMETER
;
1876 size
= (size
+ page_mask
) & ~page_mask
;
1879 /* Compute the alloc type flags */
1881 if (!(type
& (MEM_COMMIT
| MEM_RESERVE
| MEM_RESET
)) ||
1882 (type
& ~(MEM_COMMIT
| MEM_RESERVE
| MEM_TOP_DOWN
| MEM_WRITE_WATCH
| MEM_RESET
)))
1884 WARN("called with wrong alloc type flags (%08x) !\n", type
);
1885 return STATUS_INVALID_PARAMETER
;
1888 /* Reserve the memory */
1890 if (use_locks
) server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1892 if ((type
& MEM_RESERVE
) || !base
)
1894 if (type
& MEM_WRITE_WATCH
) vprot
|= VPROT_WRITEWATCH
;
1895 status
= map_view( &view
, base
, size
, mask
, type
& MEM_TOP_DOWN
, vprot
);
1896 if (status
== STATUS_SUCCESS
) base
= view
->base
;
1898 else if (type
& MEM_RESET
)
1900 if (!(view
= VIRTUAL_FindView( base
, size
))) status
= STATUS_NOT_MAPPED_VIEW
;
1901 else madvise( base
, size
, MADV_DONTNEED
);
1903 else /* commit the pages */
1905 if (!(view
= VIRTUAL_FindView( base
, size
))) status
= STATUS_NOT_MAPPED_VIEW
;
1906 else if (view
->mapping
&& (view
->protect
& VPROT_COMMITTED
)) status
= STATUS_ALREADY_COMMITTED
;
1907 else if (!VIRTUAL_SetProt( view
, base
, size
, vprot
)) status
= STATUS_ACCESS_DENIED
;
1908 else if (view
->mapping
&& !(view
->protect
& VPROT_COMMITTED
))
1910 SERVER_START_REQ( add_mapping_committed_range
)
1912 req
->handle
= wine_server_obj_handle( view
->mapping
);
1913 req
->offset
= (char *)base
- (char *)view
->base
;
1915 wine_server_call( req
);
1921 if (use_locks
) server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1923 if (status
== STATUS_SUCCESS
)
1932 /***********************************************************************
1933 * NtFreeVirtualMemory (NTDLL.@)
1934 * ZwFreeVirtualMemory (NTDLL.@)
1936 NTSTATUS WINAPI
NtFreeVirtualMemory( HANDLE process
, PVOID
*addr_ptr
, SIZE_T
*size_ptr
, ULONG type
)
1938 struct file_view
*view
;
1941 NTSTATUS status
= STATUS_SUCCESS
;
1942 LPVOID addr
= *addr_ptr
;
1943 SIZE_T size
= *size_ptr
;
1945 TRACE("%p %p %08lx %x\n", process
, addr
, size
, type
);
1947 if (process
!= NtCurrentProcess())
1950 apc_result_t result
;
1952 memset( &call
, 0, sizeof(call
) );
1954 call
.virtual_free
.type
= APC_VIRTUAL_FREE
;
1955 call
.virtual_free
.addr
= wine_server_client_ptr( addr
);
1956 call
.virtual_free
.size
= size
;
1957 call
.virtual_free
.op_type
= type
;
1958 status
= server_queue_process_apc( process
, &call
, &result
);
1959 if (status
!= STATUS_SUCCESS
) return status
;
1961 if (result
.virtual_free
.status
== STATUS_SUCCESS
)
1963 *addr_ptr
= wine_server_get_ptr( result
.virtual_free
.addr
);
1964 *size_ptr
= result
.virtual_free
.size
;
1966 return result
.virtual_free
.status
;
1969 /* Fix the parameters */
1971 size
= ROUND_SIZE( addr
, size
);
1972 base
= ROUND_ADDR( addr
, page_mask
);
1974 /* avoid freeing the DOS area when a broken app passes a NULL pointer */
1975 if (!base
) return STATUS_INVALID_PARAMETER
;
1977 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1979 if (!(view
= VIRTUAL_FindView( base
, size
)) || !(view
->protect
& VPROT_VALLOC
))
1981 status
= STATUS_INVALID_PARAMETER
;
1983 else if (type
== MEM_RELEASE
)
1985 /* Free the pages */
1987 if (size
|| (base
!= view
->base
)) status
= STATUS_INVALID_PARAMETER
;
1990 delete_view( view
);
1995 else if (type
== MEM_DECOMMIT
)
1997 status
= decommit_pages( view
, base
- (char *)view
->base
, size
);
1998 if (status
== STATUS_SUCCESS
)
2006 WARN("called with wrong free type flags (%08x) !\n", type
);
2007 status
= STATUS_INVALID_PARAMETER
;
2010 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2014 static ULONG
map_protection_to_access( ULONG vprot
)
2016 vprot
&= VPROT_READ
| VPROT_WRITE
| VPROT_EXEC
| VPROT_WRITECOPY
;
2017 if (vprot
& VPROT_EXEC
)
2019 if (vprot
& VPROT_WRITE
) vprot
|= VPROT_WRITECOPY
;
2021 else vprot
&= ~VPROT_WRITECOPY
;
2025 static BOOL
is_compatible_protection( const struct file_view
*view
, ULONG new_prot
)
2027 ULONG view_prot
, map_prot
;
2029 view_prot
= map_protection_to_access( view
->protect
);
2030 new_prot
= map_protection_to_access( new_prot
);
2032 if (view_prot
== new_prot
) return TRUE
;
2033 if (!view_prot
) return FALSE
;
2035 if ((view_prot
& new_prot
) != new_prot
) return FALSE
;
2037 map_prot
= map_protection_to_access( view
->map_protect
);
2038 if ((map_prot
& new_prot
) == new_prot
) return TRUE
;
2043 /***********************************************************************
2044 * NtProtectVirtualMemory (NTDLL.@)
2045 * ZwProtectVirtualMemory (NTDLL.@)
2047 NTSTATUS WINAPI
NtProtectVirtualMemory( HANDLE process
, PVOID
*addr_ptr
, SIZE_T
*size_ptr
,
2048 ULONG new_prot
, ULONG
*old_prot
)
2050 struct file_view
*view
;
2052 NTSTATUS status
= STATUS_SUCCESS
;
2055 unsigned int new_vprot
;
2056 SIZE_T size
= *size_ptr
;
2057 LPVOID addr
= *addr_ptr
;
2059 TRACE("%p %p %08lx %08x\n", process
, addr
, size
, new_prot
);
2061 if (process
!= NtCurrentProcess())
2064 apc_result_t result
;
2066 memset( &call
, 0, sizeof(call
) );
2068 call
.virtual_protect
.type
= APC_VIRTUAL_PROTECT
;
2069 call
.virtual_protect
.addr
= wine_server_client_ptr( addr
);
2070 call
.virtual_protect
.size
= size
;
2071 call
.virtual_protect
.prot
= new_prot
;
2072 status
= server_queue_process_apc( process
, &call
, &result
);
2073 if (status
!= STATUS_SUCCESS
) return status
;
2075 if (result
.virtual_protect
.status
== STATUS_SUCCESS
)
2077 *addr_ptr
= wine_server_get_ptr( result
.virtual_protect
.addr
);
2078 *size_ptr
= result
.virtual_protect
.size
;
2079 if (old_prot
) *old_prot
= result
.virtual_protect
.prot
;
2081 return result
.virtual_protect
.status
;
2084 /* Fix the parameters */
2086 size
= ROUND_SIZE( addr
, size
);
2087 base
= ROUND_ADDR( addr
, page_mask
);
2089 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2091 if ((view
= VIRTUAL_FindView( base
, size
)))
2093 /* Make sure all the pages are committed */
2094 if (get_committed_size( view
, base
, &vprot
) >= size
&& (vprot
& VPROT_COMMITTED
))
2096 if (!(status
= get_vprot_flags( new_prot
, &new_vprot
, view
->protect
& VPROT_IMAGE
)))
2098 if ((new_vprot
& VPROT_WRITECOPY
) && (view
->protect
& VPROT_VALLOC
))
2099 status
= STATUS_INVALID_PAGE_PROTECTION
;
2102 if (!view
->mapping
|| is_compatible_protection( view
, new_vprot
))
2104 new_vprot
|= VPROT_COMMITTED
;
2105 if (old_prot
) *old_prot
= VIRTUAL_GetWin32Prot( vprot
);
2106 if (!VIRTUAL_SetProt( view
, base
, size
, new_vprot
)) status
= STATUS_ACCESS_DENIED
;
2108 else status
= STATUS_INVALID_PAGE_PROTECTION
;
2112 else status
= STATUS_NOT_COMMITTED
;
2114 else status
= STATUS_INVALID_PARAMETER
;
2116 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2118 if (status
== STATUS_SUCCESS
)
2127 /* retrieve state for a free memory area; callback for wine_mmap_enum_reserved_areas */
2128 static int get_free_mem_state_callback( void *start
, size_t size
, void *arg
)
2130 MEMORY_BASIC_INFORMATION
*info
= arg
;
2131 void *end
= (char *)start
+ size
;
2133 if ((char *)info
->BaseAddress
+ info
->RegionSize
< (char *)start
) return 0;
2135 if (info
->BaseAddress
>= end
)
2137 if (info
->AllocationBase
< end
) info
->AllocationBase
= end
;
2141 if (info
->BaseAddress
>= start
|| start
<= address_space_start
)
2143 /* it's a real free area */
2144 info
->State
= MEM_FREE
;
2145 info
->Protect
= PAGE_NOACCESS
;
2146 info
->AllocationBase
= 0;
2147 info
->AllocationProtect
= 0;
2149 if ((char *)info
->BaseAddress
+ info
->RegionSize
> (char *)end
)
2150 info
->RegionSize
= (char *)end
- (char *)info
->BaseAddress
;
2152 else /* outside of the reserved area, pretend it's allocated */
2154 info
->RegionSize
= (char *)start
- (char *)info
->BaseAddress
;
2155 info
->State
= MEM_RESERVE
;
2156 info
->Protect
= PAGE_NOACCESS
;
2157 info
->AllocationProtect
= PAGE_NOACCESS
;
2158 info
->Type
= MEM_PRIVATE
;
2163 #define UNIMPLEMENTED_INFO_CLASS(c) \
2165 FIXME("(process=%p,addr=%p) Unimplemented information class: " #c "\n", process, addr); \
2166 return STATUS_INVALID_INFO_CLASS
2168 /***********************************************************************
2169 * NtQueryVirtualMemory (NTDLL.@)
2170 * ZwQueryVirtualMemory (NTDLL.@)
2172 NTSTATUS WINAPI
NtQueryVirtualMemory( HANDLE process
, LPCVOID addr
,
2173 MEMORY_INFORMATION_CLASS info_class
, PVOID buffer
,
2174 SIZE_T len
, SIZE_T
*res_len
)
2176 struct file_view
*view
;
2177 char *base
, *alloc_base
= 0;
2180 MEMORY_BASIC_INFORMATION
*info
= buffer
;
2183 if (info_class
!= MemoryBasicInformation
)
2187 UNIMPLEMENTED_INFO_CLASS(MemoryWorkingSetList
);
2188 UNIMPLEMENTED_INFO_CLASS(MemorySectionName
);
2189 UNIMPLEMENTED_INFO_CLASS(MemoryBasicVlmInformation
);
2192 FIXME("(%p,%p,info_class=%d,%p,%ld,%p) Unknown information class\n",
2193 process
, addr
, info_class
, buffer
, len
, res_len
);
2194 return STATUS_INVALID_INFO_CLASS
;
2198 if (process
!= NtCurrentProcess())
2202 apc_result_t result
;
2204 memset( &call
, 0, sizeof(call
) );
2206 call
.virtual_query
.type
= APC_VIRTUAL_QUERY
;
2207 call
.virtual_query
.addr
= wine_server_client_ptr( addr
);
2208 status
= server_queue_process_apc( process
, &call
, &result
);
2209 if (status
!= STATUS_SUCCESS
) return status
;
2211 if (result
.virtual_query
.status
== STATUS_SUCCESS
)
2213 info
->BaseAddress
= wine_server_get_ptr( result
.virtual_query
.base
);
2214 info
->AllocationBase
= wine_server_get_ptr( result
.virtual_query
.alloc_base
);
2215 info
->RegionSize
= result
.virtual_query
.size
;
2216 info
->Protect
= result
.virtual_query
.prot
;
2217 info
->AllocationProtect
= result
.virtual_query
.alloc_prot
;
2218 info
->State
= (DWORD
)result
.virtual_query
.state
<< 12;
2219 info
->Type
= (DWORD
)result
.virtual_query
.alloc_type
<< 16;
2220 if (info
->RegionSize
!= result
.virtual_query
.size
) /* truncated */
2221 return STATUS_INVALID_PARAMETER
; /* FIXME */
2222 if (res_len
) *res_len
= sizeof(*info
);
2224 return result
.virtual_query
.status
;
2227 base
= ROUND_ADDR( addr
, page_mask
);
2229 if (is_beyond_limit( base
, 1, working_set_limit
)) return STATUS_WORKING_SET_LIMIT_RANGE
;
2231 /* Find the view containing the address */
2233 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2234 ptr
= list_head( &views_list
);
2239 size
= (char *)working_set_limit
- alloc_base
;
2243 view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
2244 if ((char *)view
->base
> base
)
2246 size
= (char *)view
->base
- alloc_base
;
2250 if ((char *)view
->base
+ view
->size
> base
)
2252 alloc_base
= view
->base
;
2256 alloc_base
= (char *)view
->base
+ view
->size
;
2257 ptr
= list_next( &views_list
, ptr
);
2260 /* Fill the info structure */
2262 info
->AllocationBase
= alloc_base
;
2263 info
->BaseAddress
= base
;
2264 info
->RegionSize
= size
- (base
- alloc_base
);
2268 if (!wine_mmap_enum_reserved_areas( get_free_mem_state_callback
, info
, 0 ))
2270 /* not in a reserved area at all, pretend it's allocated */
2272 if (base
>= (char *)address_space_start
)
2274 info
->State
= MEM_RESERVE
;
2275 info
->Protect
= PAGE_NOACCESS
;
2276 info
->AllocationProtect
= PAGE_NOACCESS
;
2277 info
->Type
= MEM_PRIVATE
;
2282 info
->State
= MEM_FREE
;
2283 info
->Protect
= PAGE_NOACCESS
;
2284 info
->AllocationBase
= 0;
2285 info
->AllocationProtect
= 0;
2293 SIZE_T range_size
= get_committed_size( view
, base
, &vprot
);
2295 info
->State
= (vprot
& VPROT_COMMITTED
) ? MEM_COMMIT
: MEM_RESERVE
;
2296 info
->Protect
= (vprot
& VPROT_COMMITTED
) ? VIRTUAL_GetWin32Prot( vprot
) : 0;
2297 info
->AllocationBase
= alloc_base
;
2298 info
->AllocationProtect
= VIRTUAL_GetWin32Prot( view
->protect
);
2299 if (view
->protect
& VPROT_IMAGE
) info
->Type
= MEM_IMAGE
;
2300 else if (view
->protect
& VPROT_VALLOC
) info
->Type
= MEM_PRIVATE
;
2301 else info
->Type
= MEM_MAPPED
;
2302 for (size
= base
- alloc_base
; size
< base
+ range_size
- alloc_base
; size
+= page_size
)
2303 if ((view
->prot
[size
>> page_shift
] ^ vprot
) & ~VPROT_WRITEWATCH
) break;
2304 info
->RegionSize
= size
- (base
- alloc_base
);
2306 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2308 if (res_len
) *res_len
= sizeof(*info
);
2309 return STATUS_SUCCESS
;
2313 /***********************************************************************
2314 * NtLockVirtualMemory (NTDLL.@)
2315 * ZwLockVirtualMemory (NTDLL.@)
2317 NTSTATUS WINAPI
NtLockVirtualMemory( HANDLE process
, PVOID
*addr
, SIZE_T
*size
, ULONG unknown
)
2319 NTSTATUS status
= STATUS_SUCCESS
;
2321 if (process
!= NtCurrentProcess())
2324 apc_result_t result
;
2326 memset( &call
, 0, sizeof(call
) );
2328 call
.virtual_lock
.type
= APC_VIRTUAL_LOCK
;
2329 call
.virtual_lock
.addr
= wine_server_client_ptr( *addr
);
2330 call
.virtual_lock
.size
= *size
;
2331 status
= server_queue_process_apc( process
, &call
, &result
);
2332 if (status
!= STATUS_SUCCESS
) return status
;
2334 if (result
.virtual_lock
.status
== STATUS_SUCCESS
)
2336 *addr
= wine_server_get_ptr( result
.virtual_lock
.addr
);
2337 *size
= result
.virtual_lock
.size
;
2339 return result
.virtual_lock
.status
;
2342 *size
= ROUND_SIZE( *addr
, *size
);
2343 *addr
= ROUND_ADDR( *addr
, page_mask
);
2345 if (mlock( *addr
, *size
)) status
= STATUS_ACCESS_DENIED
;
2350 /***********************************************************************
2351 * NtUnlockVirtualMemory (NTDLL.@)
2352 * ZwUnlockVirtualMemory (NTDLL.@)
2354 NTSTATUS WINAPI
NtUnlockVirtualMemory( HANDLE process
, PVOID
*addr
, SIZE_T
*size
, ULONG unknown
)
2356 NTSTATUS status
= STATUS_SUCCESS
;
2358 if (process
!= NtCurrentProcess())
2361 apc_result_t result
;
2363 memset( &call
, 0, sizeof(call
) );
2365 call
.virtual_unlock
.type
= APC_VIRTUAL_UNLOCK
;
2366 call
.virtual_unlock
.addr
= wine_server_client_ptr( *addr
);
2367 call
.virtual_unlock
.size
= *size
;
2368 status
= server_queue_process_apc( process
, &call
, &result
);
2369 if (status
!= STATUS_SUCCESS
) return status
;
2371 if (result
.virtual_unlock
.status
== STATUS_SUCCESS
)
2373 *addr
= wine_server_get_ptr( result
.virtual_unlock
.addr
);
2374 *size
= result
.virtual_unlock
.size
;
2376 return result
.virtual_unlock
.status
;
2379 *size
= ROUND_SIZE( *addr
, *size
);
2380 *addr
= ROUND_ADDR( *addr
, page_mask
);
2382 if (munlock( *addr
, *size
)) status
= STATUS_ACCESS_DENIED
;
2387 /***********************************************************************
2388 * NtCreateSection (NTDLL.@)
2389 * ZwCreateSection (NTDLL.@)
2391 NTSTATUS WINAPI
NtCreateSection( HANDLE
*handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
,
2392 const LARGE_INTEGER
*size
, ULONG protect
,
2393 ULONG sec_flags
, HANDLE file
)
2397 DWORD len
= (attr
&& attr
->ObjectName
) ? attr
->ObjectName
->Length
: 0;
2398 struct security_descriptor
*sd
= NULL
;
2399 struct object_attributes objattr
;
2401 /* Check parameters */
2403 if (len
> MAX_PATH
*sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
2405 if ((ret
= get_vprot_flags( protect
, &vprot
, sec_flags
& SEC_IMAGE
))) return ret
;
2407 objattr
.rootdir
= wine_server_obj_handle( attr
? attr
->RootDirectory
: 0 );
2409 objattr
.name_len
= len
;
2412 ret
= NTDLL_create_struct_sd( attr
->SecurityDescriptor
, &sd
, &objattr
.sd_len
);
2413 if (ret
!= STATUS_SUCCESS
) return ret
;
2416 if (!(sec_flags
& SEC_RESERVE
)) vprot
|= VPROT_COMMITTED
;
2417 if (sec_flags
& SEC_NOCACHE
) vprot
|= VPROT_NOCACHE
;
2418 if (sec_flags
& SEC_IMAGE
) vprot
|= VPROT_IMAGE
;
2420 /* Create the server object */
2422 SERVER_START_REQ( create_mapping
)
2424 req
->access
= access
;
2425 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
2426 req
->file_handle
= wine_server_obj_handle( file
);
2427 req
->size
= size
? size
->QuadPart
: 0;
2428 req
->protect
= vprot
;
2429 wine_server_add_data( req
, &objattr
, sizeof(objattr
) );
2430 if (objattr
.sd_len
) wine_server_add_data( req
, sd
, objattr
.sd_len
);
2431 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
2432 ret
= wine_server_call( req
);
2433 *handle
= wine_server_ptr_handle( reply
->handle
);
2437 NTDLL_free_struct_sd( sd
);
2443 /***********************************************************************
2444 * NtOpenSection (NTDLL.@)
2445 * ZwOpenSection (NTDLL.@)
2447 NTSTATUS WINAPI
NtOpenSection( HANDLE
*handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
)
2450 DWORD len
= attr
->ObjectName
->Length
;
2452 if (len
> MAX_PATH
*sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
2454 SERVER_START_REQ( open_mapping
)
2456 req
->access
= access
;
2457 req
->attributes
= attr
->Attributes
;
2458 req
->rootdir
= wine_server_obj_handle( attr
->RootDirectory
);
2459 wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
2460 if (!(ret
= wine_server_call( req
))) *handle
= wine_server_ptr_handle( reply
->handle
);
2467 /***********************************************************************
2468 * NtMapViewOfSection (NTDLL.@)
2469 * ZwMapViewOfSection (NTDLL.@)
2471 NTSTATUS WINAPI
NtMapViewOfSection( HANDLE handle
, HANDLE process
, PVOID
*addr_ptr
, ULONG zero_bits
,
2472 SIZE_T commit_size
, const LARGE_INTEGER
*offset_ptr
, SIZE_T
*size_ptr
,
2473 SECTION_INHERIT inherit
, ULONG alloc_type
, ULONG protect
)
2476 mem_size_t full_size
;
2478 SIZE_T size
, mask
= get_mask( zero_bits
);
2479 int unix_handle
= -1, needs_close
;
2480 unsigned int map_vprot
, vprot
;
2482 struct file_view
*view
;
2484 HANDLE dup_mapping
, shared_file
;
2485 LARGE_INTEGER offset
;
2488 offset
.QuadPart
= offset_ptr
? offset_ptr
->QuadPart
: 0;
2490 TRACE("handle=%p process=%p addr=%p off=%x%08x size=%lx access=%x\n",
2491 handle
, process
, *addr_ptr
, offset
.u
.HighPart
, offset
.u
.LowPart
, *size_ptr
, protect
);
2493 /* Check parameters */
2495 if ((offset
.u
.LowPart
& mask
) || (*addr_ptr
&& ((UINT_PTR
)*addr_ptr
& mask
)))
2496 return STATUS_INVALID_PARAMETER
;
2501 access
= SECTION_MAP_READ
;
2503 case PAGE_READWRITE
:
2504 case PAGE_EXECUTE_READWRITE
:
2505 access
= SECTION_MAP_WRITE
;
2508 case PAGE_WRITECOPY
:
2510 case PAGE_EXECUTE_READ
:
2511 case PAGE_EXECUTE_WRITECOPY
:
2512 access
= SECTION_MAP_READ
;
2515 return STATUS_INVALID_PAGE_PROTECTION
;
2518 if (process
!= NtCurrentProcess())
2521 apc_result_t result
;
2523 memset( &call
, 0, sizeof(call
) );
2525 call
.map_view
.type
= APC_MAP_VIEW
;
2526 call
.map_view
.handle
= wine_server_obj_handle( handle
);
2527 call
.map_view
.addr
= wine_server_client_ptr( *addr_ptr
);
2528 call
.map_view
.size
= *size_ptr
;
2529 call
.map_view
.offset
= offset
.QuadPart
;
2530 call
.map_view
.zero_bits
= zero_bits
;
2531 call
.map_view
.alloc_type
= alloc_type
;
2532 call
.map_view
.prot
= protect
;
2533 res
= server_queue_process_apc( process
, &call
, &result
);
2534 if (res
!= STATUS_SUCCESS
) return res
;
2536 if ((NTSTATUS
)result
.map_view
.status
>= 0)
2538 *addr_ptr
= wine_server_get_ptr( result
.map_view
.addr
);
2539 *size_ptr
= result
.map_view
.size
;
2541 return result
.map_view
.status
;
2544 SERVER_START_REQ( get_mapping_info
)
2546 req
->handle
= wine_server_obj_handle( handle
);
2547 req
->access
= access
;
2548 res
= wine_server_call( req
);
2549 map_vprot
= reply
->protect
;
2550 base
= wine_server_get_ptr( reply
->base
);
2551 full_size
= reply
->size
;
2552 header_size
= reply
->header_size
;
2553 dup_mapping
= wine_server_ptr_handle( reply
->mapping
);
2554 shared_file
= wine_server_ptr_handle( reply
->shared_file
);
2555 if ((ULONG_PTR
)base
!= reply
->base
) base
= NULL
;
2558 if (res
) return res
;
2560 if ((res
= server_get_unix_fd( handle
, 0, &unix_handle
, &needs_close
, NULL
, NULL
))) goto done
;
2562 if (map_vprot
& VPROT_IMAGE
)
2565 if (size
!= full_size
) /* truncated */
2567 WARN( "Modules larger than 4Gb (%s) not supported\n", wine_dbgstr_longlong(full_size
) );
2568 res
= STATUS_INVALID_PARAMETER
;
2573 int shared_fd
, shared_needs_close
;
2575 if ((res
= server_get_unix_fd( shared_file
, FILE_READ_DATA
|FILE_WRITE_DATA
,
2576 &shared_fd
, &shared_needs_close
, NULL
, NULL
))) goto done
;
2577 res
= map_image( handle
, unix_handle
, base
, size
, mask
, header_size
,
2578 shared_fd
, dup_mapping
, map_vprot
, addr_ptr
);
2579 if (shared_needs_close
) close( shared_fd
);
2580 NtClose( shared_file
);
2584 res
= map_image( handle
, unix_handle
, base
, size
, mask
, header_size
,
2585 -1, dup_mapping
, map_vprot
, addr_ptr
);
2587 if (needs_close
) close( unix_handle
);
2588 if (res
>= 0) *size_ptr
= size
;
2592 res
= STATUS_INVALID_PARAMETER
;
2593 if (offset
.QuadPart
>= full_size
) goto done
;
2596 if (*size_ptr
> full_size
- offset
.QuadPart
) goto done
;
2597 size
= ROUND_SIZE( offset
.u
.LowPart
, *size_ptr
);
2598 if (size
< *size_ptr
) goto done
; /* wrap-around */
2602 size
= full_size
- offset
.QuadPart
;
2603 if (size
!= full_size
- offset
.QuadPart
) /* truncated */
2605 WARN( "Files larger than 4Gb (%s) not supported on this platform\n",
2606 wine_dbgstr_longlong(full_size
) );
2611 /* Reserve a properly aligned area */
2613 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2615 get_vprot_flags( protect
, &vprot
, map_vprot
& VPROT_IMAGE
);
2616 vprot
|= (map_vprot
& VPROT_COMMITTED
);
2617 res
= map_view( &view
, *addr_ptr
, size
, mask
, FALSE
, vprot
);
2620 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2626 TRACE("handle=%p size=%lx offset=%x%08x\n",
2627 handle
, size
, offset
.u
.HighPart
, offset
.u
.LowPart
);
2629 res
= map_file_into_view( view
, unix_handle
, 0, size
, offset
.QuadPart
, vprot
, !dup_mapping
);
2630 if (res
== STATUS_SUCCESS
)
2632 *addr_ptr
= view
->base
;
2634 view
->mapping
= dup_mapping
;
2635 view
->map_protect
= map_vprot
;
2636 dup_mapping
= 0; /* don't close it */
2640 ERR( "map_file_into_view %p %lx %x%08x failed\n",
2641 view
->base
, size
, offset
.u
.HighPart
, offset
.u
.LowPart
);
2642 delete_view( view
);
2645 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2648 if (dup_mapping
) NtClose( dup_mapping
);
2649 if (needs_close
) close( unix_handle
);
2654 /***********************************************************************
2655 * NtUnmapViewOfSection (NTDLL.@)
2656 * ZwUnmapViewOfSection (NTDLL.@)
2658 NTSTATUS WINAPI
NtUnmapViewOfSection( HANDLE process
, PVOID addr
)
2660 struct file_view
*view
;
2661 NTSTATUS status
= STATUS_NOT_MAPPED_VIEW
;
2663 void *base
= ROUND_ADDR( addr
, page_mask
);
2665 if (process
!= NtCurrentProcess())
2668 apc_result_t result
;
2670 memset( &call
, 0, sizeof(call
) );
2672 call
.unmap_view
.type
= APC_UNMAP_VIEW
;
2673 call
.unmap_view
.addr
= wine_server_client_ptr( addr
);
2674 status
= server_queue_process_apc( process
, &call
, &result
);
2675 if (status
== STATUS_SUCCESS
) status
= result
.unmap_view
.status
;
2679 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2680 if ((view
= VIRTUAL_FindView( base
, 0 )) && (base
== view
->base
) && !(view
->protect
& VPROT_VALLOC
))
2682 delete_view( view
);
2683 status
= STATUS_SUCCESS
;
2685 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2690 /***********************************************************************
2691 * NtFlushVirtualMemory (NTDLL.@)
2692 * ZwFlushVirtualMemory (NTDLL.@)
2694 NTSTATUS WINAPI
NtFlushVirtualMemory( HANDLE process
, LPCVOID
*addr_ptr
,
2695 SIZE_T
*size_ptr
, ULONG unknown
)
2697 struct file_view
*view
;
2698 NTSTATUS status
= STATUS_SUCCESS
;
2700 void *addr
= ROUND_ADDR( *addr_ptr
, page_mask
);
2702 if (process
!= NtCurrentProcess())
2705 apc_result_t result
;
2707 memset( &call
, 0, sizeof(call
) );
2709 call
.virtual_flush
.type
= APC_VIRTUAL_FLUSH
;
2710 call
.virtual_flush
.addr
= wine_server_client_ptr( addr
);
2711 call
.virtual_flush
.size
= *size_ptr
;
2712 status
= server_queue_process_apc( process
, &call
, &result
);
2713 if (status
!= STATUS_SUCCESS
) return status
;
2715 if (result
.virtual_flush
.status
== STATUS_SUCCESS
)
2717 *addr_ptr
= wine_server_get_ptr( result
.virtual_flush
.addr
);
2718 *size_ptr
= result
.virtual_flush
.size
;
2720 return result
.virtual_flush
.status
;
2723 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2724 if (!(view
= VIRTUAL_FindView( addr
, *size_ptr
))) status
= STATUS_INVALID_PARAMETER
;
2727 if (!*size_ptr
) *size_ptr
= view
->size
;
2730 if (msync( addr
, *size_ptr
, MS_ASYNC
)) status
= STATUS_NOT_MAPPED_DATA
;
2733 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2738 /***********************************************************************
2739 * NtGetWriteWatch (NTDLL.@)
2740 * ZwGetWriteWatch (NTDLL.@)
2742 NTSTATUS WINAPI
NtGetWriteWatch( HANDLE process
, ULONG flags
, PVOID base
, SIZE_T size
, PVOID
*addresses
,
2743 ULONG_PTR
*count
, ULONG
*granularity
)
2745 struct file_view
*view
;
2746 NTSTATUS status
= STATUS_SUCCESS
;
2749 size
= ROUND_SIZE( base
, size
);
2750 base
= ROUND_ADDR( base
, page_mask
);
2752 if (!count
|| !granularity
) return STATUS_ACCESS_VIOLATION
;
2753 if (!*count
|| !size
) return STATUS_INVALID_PARAMETER
;
2754 if (flags
& ~WRITE_WATCH_FLAG_RESET
) return STATUS_INVALID_PARAMETER
;
2756 if (!addresses
) return STATUS_ACCESS_VIOLATION
;
2758 TRACE( "%p %x %p-%p %p %lu\n", process
, flags
, base
, (char *)base
+ size
,
2759 addresses
, *count
);
2761 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2763 if ((view
= VIRTUAL_FindView( base
, size
)) && (view
->protect
& VPROT_WRITEWATCH
))
2767 char *end
= addr
+ size
;
2769 while (pos
< *count
&& addr
< end
)
2771 BYTE prot
= view
->prot
[(addr
- (char *)view
->base
) >> page_shift
];
2772 if (!(prot
& VPROT_WRITEWATCH
)) addresses
[pos
++] = addr
;
2775 if (flags
& WRITE_WATCH_FLAG_RESET
) reset_write_watches( view
, base
, addr
- (char *)base
);
2777 *granularity
= page_size
;
2779 else status
= STATUS_INVALID_PARAMETER
;
2781 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2786 /***********************************************************************
2787 * NtResetWriteWatch (NTDLL.@)
2788 * ZwResetWriteWatch (NTDLL.@)
2790 NTSTATUS WINAPI
NtResetWriteWatch( HANDLE process
, PVOID base
, SIZE_T size
)
2792 struct file_view
*view
;
2793 NTSTATUS status
= STATUS_SUCCESS
;
2796 size
= ROUND_SIZE( base
, size
);
2797 base
= ROUND_ADDR( base
, page_mask
);
2799 TRACE( "%p %p-%p\n", process
, base
, (char *)base
+ size
);
2801 if (!size
) return STATUS_INVALID_PARAMETER
;
2803 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2805 if ((view
= VIRTUAL_FindView( base
, size
)) && (view
->protect
& VPROT_WRITEWATCH
))
2806 reset_write_watches( view
, base
, size
);
2808 status
= STATUS_INVALID_PARAMETER
;
2810 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2815 /***********************************************************************
2816 * NtReadVirtualMemory (NTDLL.@)
2817 * ZwReadVirtualMemory (NTDLL.@)
2819 NTSTATUS WINAPI
NtReadVirtualMemory( HANDLE process
, const void *addr
, void *buffer
,
2820 SIZE_T size
, SIZE_T
*bytes_read
)
2824 if (virtual_check_buffer_for_write( buffer
, size
))
2826 SERVER_START_REQ( read_process_memory
)
2828 req
->handle
= wine_server_obj_handle( process
);
2829 req
->addr
= wine_server_client_ptr( addr
);
2830 wine_server_set_reply( req
, buffer
, size
);
2831 if ((status
= wine_server_call( req
))) size
= 0;
2837 status
= STATUS_ACCESS_VIOLATION
;
2840 if (bytes_read
) *bytes_read
= size
;
2845 /***********************************************************************
2846 * NtWriteVirtualMemory (NTDLL.@)
2847 * ZwWriteVirtualMemory (NTDLL.@)
2849 NTSTATUS WINAPI
NtWriteVirtualMemory( HANDLE process
, void *addr
, const void *buffer
,
2850 SIZE_T size
, SIZE_T
*bytes_written
)
2854 if (virtual_check_buffer_for_read( buffer
, size
))
2856 SERVER_START_REQ( write_process_memory
)
2858 req
->handle
= wine_server_obj_handle( process
);
2859 req
->addr
= wine_server_client_ptr( addr
);
2860 wine_server_add_data( req
, buffer
, size
);
2861 if ((status
= wine_server_call( req
))) size
= 0;
2867 status
= STATUS_PARTIAL_COPY
;
2870 if (bytes_written
) *bytes_written
= size
;
2875 /***********************************************************************
2876 * NtAreMappedFilesTheSame (NTDLL.@)
2877 * ZwAreMappedFilesTheSame (NTDLL.@)
2879 NTSTATUS WINAPI
NtAreMappedFilesTheSame(PVOID addr1
, PVOID addr2
)
2881 struct file_view
*view1
, *view2
;
2882 struct stat st1
, st2
;
2886 TRACE("%p %p\n", addr1
, addr2
);
2888 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2890 view1
= VIRTUAL_FindView( addr1
, 0 );
2891 view2
= VIRTUAL_FindView( addr2
, 0 );
2893 if (!view1
|| !view2
)
2894 status
= STATUS_INVALID_ADDRESS
;
2895 else if ((view1
->protect
& VPROT_VALLOC
) || (view2
->protect
& VPROT_VALLOC
))
2896 status
= STATUS_CONFLICTING_ADDRESSES
;
2897 else if (view1
== view2
)
2898 status
= STATUS_SUCCESS
;
2899 else if (!(view1
->protect
& VPROT_IMAGE
) || !(view2
->protect
& VPROT_IMAGE
))
2900 status
= STATUS_NOT_SAME_DEVICE
;
2901 else if (!stat_mapping_file( view1
, &st1
) && !stat_mapping_file( view2
, &st2
) &&
2902 st1
.st_dev
== st2
.st_dev
&& st1
.st_ino
== st2
.st_ino
)
2903 status
= STATUS_SUCCESS
;
2905 status
= STATUS_NOT_SAME_DEVICE
;
2907 server_leave_uninterrupted_section( &csVirtual
, &sigset
);