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
);
66 #define MAP_NORESERVE 0
72 struct list entry
; /* Entry in global view list */
73 void *base
; /* Base address */
74 size_t size
; /* Size in bytes */
75 HANDLE mapping
; /* Handle to the file mapping */
76 unsigned int map_protect
; /* Mapping protection */
77 unsigned int protect
; /* Protection for all pages at allocation time */
78 BYTE prot
[1]; /* Protection byte for each page */
82 /* Conversion from VPROT_* to Win32 flags */
83 static const BYTE VIRTUAL_Win32Flags
[16] =
85 PAGE_NOACCESS
, /* 0 */
86 PAGE_READONLY
, /* READ */
87 PAGE_READWRITE
, /* WRITE */
88 PAGE_READWRITE
, /* READ | WRITE */
89 PAGE_EXECUTE
, /* EXEC */
90 PAGE_EXECUTE_READ
, /* READ | EXEC */
91 PAGE_EXECUTE_READWRITE
, /* WRITE | EXEC */
92 PAGE_EXECUTE_READWRITE
, /* READ | WRITE | EXEC */
93 PAGE_WRITECOPY
, /* WRITECOPY */
94 PAGE_WRITECOPY
, /* READ | WRITECOPY */
95 PAGE_WRITECOPY
, /* WRITE | WRITECOPY */
96 PAGE_WRITECOPY
, /* READ | WRITE | WRITECOPY */
97 PAGE_EXECUTE_WRITECOPY
, /* EXEC | WRITECOPY */
98 PAGE_EXECUTE_WRITECOPY
, /* READ | EXEC | WRITECOPY */
99 PAGE_EXECUTE_WRITECOPY
, /* WRITE | EXEC | WRITECOPY */
100 PAGE_EXECUTE_WRITECOPY
/* READ | WRITE | EXEC | WRITECOPY */
103 static struct list views_list
= LIST_INIT(views_list
);
105 static RTL_CRITICAL_SECTION csVirtual
;
106 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
109 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
110 0, 0, { (DWORD_PTR
)(__FILE__
": csVirtual") }
112 static RTL_CRITICAL_SECTION csVirtual
= { &critsect_debug
, -1, 0, 0, 0, 0 };
115 static const UINT page_shift
= 12;
116 static const UINT_PTR page_mask
= 0xfff;
117 /* Note: these are Windows limits, you cannot change them. */
118 static void *address_space_limit
= (void *)0xc0000000; /* top of the total available address space */
119 static void *user_space_limit
= (void *)0x7fff0000; /* top of the user address space */
120 static void *working_set_limit
= (void *)0x7fff0000; /* top of the current working set */
121 static void *address_space_start
= (void *)0x110000; /* keep DOS area clear */
122 #elif defined(__x86_64__)
123 static const UINT page_shift
= 12;
124 static const UINT_PTR page_mask
= 0xfff;
125 static void *address_space_limit
= (void *)0x7fffffff0000;
126 static void *user_space_limit
= (void *)0x7fffffff0000;
127 static void *working_set_limit
= (void *)0x7fffffff0000;
128 static void *address_space_start
= (void *)0x10000;
130 UINT_PTR page_size
= 0;
131 static UINT page_shift
;
132 static UINT_PTR page_mask
;
133 static void *address_space_limit
;
134 static void *user_space_limit
;
135 static void *working_set_limit
;
136 static void *address_space_start
= (void *)0x10000;
137 #endif /* __i386__ */
138 static const BOOL is_win64
= (sizeof(void *) > sizeof(int));
140 #define ROUND_ADDR(addr,mask) \
141 ((void *)((UINT_PTR)(addr) & ~(UINT_PTR)(mask)))
143 #define ROUND_SIZE(addr,size) \
144 (((SIZE_T)(size) + ((UINT_PTR)(addr) & page_mask) + page_mask) & ~page_mask)
146 #define VIRTUAL_DEBUG_DUMP_VIEW(view) \
147 do { if (TRACE_ON(virtual)) VIRTUAL_DumpView(view); } while (0)
149 #define VIRTUAL_HEAP_SIZE (sizeof(void*)*1024*1024)
151 static HANDLE virtual_heap
;
152 static void *preload_reserve_start
;
153 static void *preload_reserve_end
;
154 static BOOL use_locks
;
155 static BOOL force_exec_prot
; /* whether to force PROT_EXEC on all PROT_READ mmaps */
158 /***********************************************************************
161 static const char *VIRTUAL_GetProtStr( BYTE prot
)
163 static char buffer
[6];
164 buffer
[0] = (prot
& VPROT_COMMITTED
) ? 'c' : '-';
165 buffer
[1] = (prot
& VPROT_GUARD
) ? 'g' : ((prot
& VPROT_WRITEWATCH
) ? 'H' : '-');
166 buffer
[2] = (prot
& VPROT_READ
) ? 'r' : '-';
167 buffer
[3] = (prot
& VPROT_WRITECOPY
) ? 'W' : ((prot
& VPROT_WRITE
) ? 'w' : '-');
168 buffer
[4] = (prot
& VPROT_EXEC
) ? 'x' : '-';
174 /***********************************************************************
175 * VIRTUAL_GetUnixProt
177 * Convert page protections to protection for mmap/mprotect.
179 static int VIRTUAL_GetUnixProt( BYTE vprot
)
182 if ((vprot
& VPROT_COMMITTED
) && !(vprot
& VPROT_GUARD
))
184 if (vprot
& VPROT_READ
) prot
|= PROT_READ
;
185 if (vprot
& VPROT_WRITE
) prot
|= PROT_WRITE
| PROT_READ
;
186 if (vprot
& VPROT_WRITECOPY
) prot
|= PROT_WRITE
| PROT_READ
;
187 if (vprot
& VPROT_EXEC
) prot
|= PROT_EXEC
| PROT_READ
;
188 if (vprot
& VPROT_WRITEWATCH
) prot
&= ~PROT_WRITE
;
190 if (!prot
) prot
= PROT_NONE
;
195 /***********************************************************************
198 static void VIRTUAL_DumpView( struct file_view
*view
)
201 char *addr
= view
->base
;
202 BYTE prot
= view
->prot
[0];
204 TRACE( "View: %p - %p", addr
, addr
+ view
->size
- 1 );
205 if (view
->protect
& VPROT_SYSTEM
)
206 TRACE( " (system)\n" );
207 else if (view
->protect
& VPROT_VALLOC
)
208 TRACE( " (valloc)\n" );
209 else if (view
->mapping
)
210 TRACE( " %p\n", view
->mapping
);
212 TRACE( " (anonymous)\n");
214 for (count
= i
= 1; i
< view
->size
>> page_shift
; i
++, count
++)
216 if (view
->prot
[i
] == prot
) continue;
217 TRACE( " %p - %p %s\n",
218 addr
, addr
+ (count
<< page_shift
) - 1, VIRTUAL_GetProtStr(prot
) );
219 addr
+= (count
<< page_shift
);
220 prot
= view
->prot
[i
];
224 TRACE( " %p - %p %s\n",
225 addr
, addr
+ (count
<< page_shift
) - 1, VIRTUAL_GetProtStr(prot
) );
229 /***********************************************************************
233 static void VIRTUAL_Dump(void)
236 struct file_view
*view
;
238 TRACE( "Dump of all virtual memory views:\n" );
239 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
240 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
242 VIRTUAL_DumpView( view
);
244 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
249 /***********************************************************************
252 * Find the view containing a given address. The csVirtual section must be held by caller.
261 static struct file_view
*VIRTUAL_FindView( const void *addr
, size_t size
)
263 struct file_view
*view
;
265 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
267 if (view
->base
> addr
) break; /* no matching view */
268 if ((const char *)view
->base
+ view
->size
<= (const char *)addr
) continue;
269 if ((const char *)view
->base
+ view
->size
< (const char *)addr
+ size
) break; /* size too large */
270 if ((const char *)addr
+ size
< (const char *)addr
) break; /* overflow */
277 /***********************************************************************
280 static inline UINT_PTR
get_mask( ULONG zero_bits
)
282 if (!zero_bits
) return 0xffff; /* allocations are aligned to 64K by default */
283 if (zero_bits
< page_shift
) zero_bits
= page_shift
;
284 return (1 << zero_bits
) - 1;
288 /***********************************************************************
291 * Find the first view overlapping at least part of the specified range.
292 * The csVirtual section must be held by caller.
294 static struct file_view
*find_view_range( const void *addr
, size_t size
)
296 struct file_view
*view
;
298 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
300 if ((const char *)view
->base
>= (const char *)addr
+ size
) break;
301 if ((const char *)view
->base
+ view
->size
> (const char *)addr
) return view
;
307 /***********************************************************************
310 * Find a free area between views inside the specified range.
311 * The csVirtual section must be held by caller.
313 static void *find_free_area( void *base
, void *end
, size_t size
, size_t mask
, int top_down
)
320 start
= ROUND_ADDR( (char *)end
- size
, mask
);
321 if (start
>= end
|| start
< base
) return NULL
;
323 for (ptr
= views_list
.prev
; ptr
!= &views_list
; ptr
= ptr
->prev
)
325 struct file_view
*view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
327 if ((char *)view
->base
+ view
->size
<= (char *)start
) break;
328 if ((char *)view
->base
>= (char *)start
+ size
) continue;
329 start
= ROUND_ADDR( (char *)view
->base
- size
, mask
);
330 /* stop if remaining space is not large enough */
331 if (!start
|| start
>= end
|| start
< base
) return NULL
;
336 start
= ROUND_ADDR( (char *)base
+ mask
, mask
);
337 if (start
>= end
|| (char *)end
- (char *)start
< size
) return NULL
;
339 for (ptr
= views_list
.next
; ptr
!= &views_list
; ptr
= ptr
->next
)
341 struct file_view
*view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
343 if ((char *)view
->base
>= (char *)start
+ size
) break;
344 if ((char *)view
->base
+ view
->size
<= (char *)start
) continue;
345 start
= ROUND_ADDR( (char *)view
->base
+ view
->size
+ mask
, mask
);
346 /* stop if remaining space is not large enough */
347 if (!start
|| start
>= end
|| (char *)end
- (char *)start
< size
) return NULL
;
354 /***********************************************************************
357 * Add a reserved area to the list maintained by libwine.
358 * The csVirtual section must be held by caller.
360 static void add_reserved_area( void *addr
, size_t size
)
362 TRACE( "adding %p-%p\n", addr
, (char *)addr
+ size
);
364 if (addr
< user_space_limit
)
366 /* unmap the part of the area that is below the limit */
367 assert( (char *)addr
+ size
> (char *)user_space_limit
);
368 munmap( addr
, (char *)user_space_limit
- (char *)addr
);
369 size
-= (char *)user_space_limit
- (char *)addr
;
370 addr
= user_space_limit
;
372 /* blow away existing mappings */
373 wine_anon_mmap( addr
, size
, PROT_NONE
, MAP_NORESERVE
| MAP_FIXED
);
374 wine_mmap_add_reserved_area( addr
, size
);
378 /***********************************************************************
379 * remove_reserved_area
381 * Remove a reserved area from the list maintained by libwine.
382 * The csVirtual section must be held by caller.
384 static void remove_reserved_area( void *addr
, size_t size
)
386 struct file_view
*view
;
388 TRACE( "removing %p-%p\n", addr
, (char *)addr
+ size
);
389 wine_mmap_remove_reserved_area( addr
, size
, 0 );
391 /* unmap areas not covered by an existing view */
392 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
394 if ((char *)view
->base
>= (char *)addr
+ size
)
396 munmap( addr
, size
);
399 if ((char *)view
->base
+ view
->size
<= (char *)addr
) continue;
400 if (view
->base
> addr
) munmap( addr
, (char *)view
->base
- (char *)addr
);
401 if ((char *)view
->base
+ view
->size
> (char *)addr
+ size
) break;
402 size
= (char *)addr
+ size
- ((char *)view
->base
+ view
->size
);
403 addr
= (char *)view
->base
+ view
->size
;
408 /***********************************************************************
411 * Check if an address range goes beyond a given limit.
413 static inline BOOL
is_beyond_limit( const void *addr
, size_t size
, const void *limit
)
415 return (addr
>= limit
|| (const char *)addr
+ size
> (const char *)limit
);
419 /***********************************************************************
422 * Unmap an area, or simply replace it by an empty mapping if it is
423 * in a reserved area. The csVirtual section must be held by caller.
425 static inline void unmap_area( void *addr
, size_t size
)
427 if (wine_mmap_is_in_reserved_area( addr
, size
))
428 wine_anon_mmap( addr
, size
, PROT_NONE
, MAP_NORESERVE
| MAP_FIXED
);
429 else if (is_beyond_limit( addr
, size
, user_space_limit
))
430 add_reserved_area( addr
, size
);
432 munmap( addr
, size
);
436 /***********************************************************************
439 * Deletes a view. The csVirtual section must be held by caller.
441 static void delete_view( struct file_view
*view
) /* [in] View */
443 if (!(view
->protect
& VPROT_SYSTEM
)) unmap_area( view
->base
, view
->size
);
444 list_remove( &view
->entry
);
445 if (view
->mapping
) close_handle( view
->mapping
);
446 RtlFreeHeap( virtual_heap
, 0, view
);
450 /***********************************************************************
453 * Create a view. The csVirtual section must be held by caller.
455 static NTSTATUS
create_view( struct file_view
**view_ret
, void *base
, size_t size
, unsigned int vprot
)
457 struct file_view
*view
;
459 int unix_prot
= VIRTUAL_GetUnixProt( vprot
);
461 assert( !((UINT_PTR
)base
& page_mask
) );
462 assert( !(size
& page_mask
) );
464 /* Create the view structure */
466 if (!(view
= RtlAllocateHeap( virtual_heap
, 0, sizeof(*view
) + (size
>> page_shift
) - 1 )))
468 FIXME( "out of memory in virtual heap for %p-%p\n", base
, (char *)base
+ size
);
469 return STATUS_NO_MEMORY
;
475 view
->map_protect
= 0;
476 view
->protect
= vprot
;
477 memset( view
->prot
, vprot
, size
>> page_shift
);
479 /* Insert it in the linked list */
481 LIST_FOR_EACH( ptr
, &views_list
)
483 struct file_view
*next
= LIST_ENTRY( ptr
, struct file_view
, entry
);
484 if (next
->base
> base
) break;
486 list_add_before( ptr
, &view
->entry
);
488 /* Check for overlapping views. This can happen if the previous view
489 * was a system view that got unmapped behind our back. In that case
490 * we recover by simply deleting it. */
492 if ((ptr
= list_prev( &views_list
, &view
->entry
)) != NULL
)
494 struct file_view
*prev
= LIST_ENTRY( ptr
, struct file_view
, entry
);
495 if ((char *)prev
->base
+ prev
->size
> (char *)base
)
497 TRACE( "overlapping prev view %p-%p for %p-%p\n",
498 prev
->base
, (char *)prev
->base
+ prev
->size
,
499 base
, (char *)base
+ view
->size
);
500 assert( prev
->protect
& VPROT_SYSTEM
);
504 if ((ptr
= list_next( &views_list
, &view
->entry
)) != NULL
)
506 struct file_view
*next
= LIST_ENTRY( ptr
, struct file_view
, entry
);
507 if ((char *)base
+ view
->size
> (char *)next
->base
)
509 TRACE( "overlapping next view %p-%p for %p-%p\n",
510 next
->base
, (char *)next
->base
+ next
->size
,
511 base
, (char *)base
+ view
->size
);
512 assert( next
->protect
& VPROT_SYSTEM
);
518 VIRTUAL_DEBUG_DUMP_VIEW( view
);
520 if (force_exec_prot
&& !(vprot
& VPROT_NOEXEC
) && (unix_prot
& PROT_READ
) && !(unix_prot
& PROT_EXEC
))
522 TRACE( "forcing exec permission on %p-%p\n", base
, (char *)base
+ size
- 1 );
523 mprotect( base
, size
, unix_prot
| PROT_EXEC
);
525 return STATUS_SUCCESS
;
529 /***********************************************************************
530 * VIRTUAL_GetWin32Prot
532 * Convert page protections to Win32 flags.
534 static DWORD
VIRTUAL_GetWin32Prot( BYTE vprot
)
536 DWORD ret
= VIRTUAL_Win32Flags
[vprot
& 0x0f];
537 if (vprot
& VPROT_NOCACHE
) ret
|= PAGE_NOCACHE
;
538 if (vprot
& VPROT_GUARD
) ret
|= PAGE_GUARD
;
543 /***********************************************************************
546 * Build page protections from Win32 flags.
549 * protect [I] Win32 protection flags
552 * Value of page protection flags
554 static NTSTATUS
get_vprot_flags( DWORD protect
, unsigned int *vprot
, BOOL image
)
556 switch(protect
& 0xff)
563 *vprot
= VPROT_READ
| VPROT_WRITECOPY
;
565 *vprot
= VPROT_READ
| VPROT_WRITE
;
568 *vprot
= VPROT_READ
| VPROT_WRITECOPY
;
573 case PAGE_EXECUTE_READ
:
574 *vprot
= VPROT_EXEC
| VPROT_READ
;
576 case PAGE_EXECUTE_READWRITE
:
578 *vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITECOPY
;
580 *vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITE
;
582 case PAGE_EXECUTE_WRITECOPY
:
583 *vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITECOPY
;
589 return STATUS_INVALID_PAGE_PROTECTION
;
591 if (protect
& PAGE_GUARD
) *vprot
|= VPROT_GUARD
;
592 if (protect
& PAGE_NOCACHE
) *vprot
|= VPROT_NOCACHE
;
593 return STATUS_SUCCESS
;
597 /***********************************************************************
600 * Change the protection of a range of pages.
606 static BOOL
VIRTUAL_SetProt( struct file_view
*view
, /* [in] Pointer to view */
607 void *base
, /* [in] Starting address */
608 size_t size
, /* [in] Size in bytes */
609 BYTE vprot
) /* [in] Protections to use */
611 int unix_prot
= VIRTUAL_GetUnixProt(vprot
);
612 BYTE
*p
= view
->prot
+ (((char *)base
- (char *)view
->base
) >> page_shift
);
615 base
, (char *)base
+ size
- 1, VIRTUAL_GetProtStr( vprot
) );
617 if (view
->protect
& VPROT_WRITEWATCH
)
619 /* each page may need different protections depending on write watch flag */
624 p
[0] = vprot
| (p
[0] & VPROT_WRITEWATCH
);
625 unix_prot
= VIRTUAL_GetUnixProt( p
[0] );
626 for (count
= i
= 1; i
< size
>> page_shift
; i
++, count
++)
628 p
[i
] = vprot
| (p
[i
] & VPROT_WRITEWATCH
);
629 prot
= VIRTUAL_GetUnixProt( p
[i
] );
630 if (prot
== unix_prot
) continue;
631 mprotect( addr
, count
<< page_shift
, unix_prot
);
632 addr
+= count
<< page_shift
;
636 if (count
) mprotect( addr
, count
<< page_shift
, unix_prot
);
637 VIRTUAL_DEBUG_DUMP_VIEW( view
);
641 /* if setting stack guard pages, store the permissions first, as the guard may be
642 * triggered at any point after mprotect and change the permissions again */
643 if ((vprot
& VPROT_GUARD
) &&
644 (base
>= NtCurrentTeb()->DeallocationStack
) &&
645 (base
< NtCurrentTeb()->Tib
.StackBase
))
647 memset( p
, vprot
, size
>> page_shift
);
648 mprotect( base
, size
, unix_prot
);
649 VIRTUAL_DEBUG_DUMP_VIEW( view
);
653 if (force_exec_prot
&& !(view
->protect
& VPROT_NOEXEC
) &&
654 (unix_prot
& PROT_READ
) && !(unix_prot
& PROT_EXEC
))
656 TRACE( "forcing exec permission on %p-%p\n", base
, (char *)base
+ size
- 1 );
657 if (!mprotect( base
, size
, unix_prot
| PROT_EXEC
)) goto done
;
658 /* exec + write may legitimately fail, in that case fall back to write only */
659 if (!(unix_prot
& PROT_WRITE
)) return FALSE
;
662 if (mprotect( base
, size
, unix_prot
)) return FALSE
; /* FIXME: last error */
665 memset( p
, vprot
, size
>> page_shift
);
666 VIRTUAL_DEBUG_DUMP_VIEW( view
);
671 /***********************************************************************
672 * reset_write_watches
674 * Reset write watches in a memory range.
676 static void reset_write_watches( struct file_view
*view
, void *base
, SIZE_T size
)
681 BYTE
*p
= view
->prot
+ ((addr
- (char *)view
->base
) >> page_shift
);
683 p
[0] |= VPROT_WRITEWATCH
;
684 unix_prot
= VIRTUAL_GetUnixProt( p
[0] );
685 for (count
= i
= 1; i
< size
>> page_shift
; i
++, count
++)
687 p
[i
] |= VPROT_WRITEWATCH
;
688 prot
= VIRTUAL_GetUnixProt( p
[i
] );
689 if (prot
== unix_prot
) continue;
690 mprotect( addr
, count
<< page_shift
, unix_prot
);
691 addr
+= count
<< page_shift
;
695 if (count
) mprotect( addr
, count
<< page_shift
, unix_prot
);
699 /***********************************************************************
702 * Release the extra memory while keeping the range starting on the granularity boundary.
704 static inline void *unmap_extra_space( void *ptr
, size_t total_size
, size_t wanted_size
, size_t mask
)
706 if ((ULONG_PTR
)ptr
& mask
)
708 size_t extra
= mask
+ 1 - ((ULONG_PTR
)ptr
& mask
);
709 munmap( ptr
, extra
);
710 ptr
= (char *)ptr
+ extra
;
713 if (total_size
> wanted_size
)
714 munmap( (char *)ptr
+ wanted_size
, total_size
- wanted_size
);
728 /***********************************************************************
729 * alloc_reserved_area_callback
731 * Try to map some space inside a reserved area. Callback for wine_mmap_enum_reserved_areas.
733 static int alloc_reserved_area_callback( void *start
, size_t size
, void *arg
)
735 struct alloc_area
*alloc
= arg
;
736 void *end
= (char *)start
+ size
;
738 if (start
< address_space_start
) start
= address_space_start
;
739 if (is_beyond_limit( start
, size
, alloc
->limit
)) end
= alloc
->limit
;
740 if (start
>= end
) return 0;
742 /* make sure we don't touch the preloader reserved range */
743 if (preload_reserve_end
>= start
)
745 if (preload_reserve_end
>= end
)
747 if (preload_reserve_start
<= start
) return 0; /* no space in that area */
748 if (preload_reserve_start
< end
) end
= preload_reserve_start
;
750 else if (preload_reserve_start
<= start
) start
= preload_reserve_end
;
753 /* range is split in two by the preloader reservation, try first part */
754 if ((alloc
->result
= find_free_area( start
, preload_reserve_start
, alloc
->size
,
755 alloc
->mask
, alloc
->top_down
)))
757 /* then fall through to try second part */
758 start
= preload_reserve_end
;
761 if ((alloc
->result
= find_free_area( start
, end
, alloc
->size
, alloc
->mask
, alloc
->top_down
)))
768 /***********************************************************************
771 * Create a view and mmap the corresponding memory area.
772 * The csVirtual section must be held by caller.
774 static NTSTATUS
map_view( struct file_view
**view_ret
, void *base
, size_t size
, size_t mask
,
775 int top_down
, unsigned int vprot
)
782 if (is_beyond_limit( base
, size
, address_space_limit
))
783 return STATUS_WORKING_SET_LIMIT_RANGE
;
785 switch (wine_mmap_is_in_reserved_area( base
, size
))
787 case -1: /* partially in a reserved area */
788 return STATUS_CONFLICTING_ADDRESSES
;
790 case 0: /* not in a reserved area, do a normal allocation */
791 if ((ptr
= wine_anon_mmap( base
, size
, VIRTUAL_GetUnixProt(vprot
), 0 )) == (void *)-1)
793 if (errno
== ENOMEM
) return STATUS_NO_MEMORY
;
794 return STATUS_INVALID_PARAMETER
;
798 /* We couldn't get the address we wanted */
799 if (is_beyond_limit( ptr
, size
, user_space_limit
)) add_reserved_area( ptr
, size
);
800 else munmap( ptr
, size
);
801 return STATUS_CONFLICTING_ADDRESSES
;
806 case 1: /* in a reserved area, make sure the address is available */
807 if (find_view_range( base
, size
)) return STATUS_CONFLICTING_ADDRESSES
;
808 /* replace the reserved area by our mapping */
809 if ((ptr
= wine_anon_mmap( base
, size
, VIRTUAL_GetUnixProt(vprot
), MAP_FIXED
)) != base
)
810 return STATUS_INVALID_PARAMETER
;
813 if (is_beyond_limit( ptr
, size
, working_set_limit
)) working_set_limit
= address_space_limit
;
817 size_t view_size
= size
+ mask
+ 1;
818 struct alloc_area alloc
;
822 alloc
.top_down
= top_down
;
823 alloc
.limit
= user_space_limit
;
824 if (wine_mmap_enum_reserved_areas( alloc_reserved_area_callback
, &alloc
, top_down
))
827 TRACE( "got mem in reserved area %p-%p\n", ptr
, (char *)ptr
+ size
);
828 if (wine_anon_mmap( ptr
, size
, VIRTUAL_GetUnixProt(vprot
), MAP_FIXED
) != ptr
)
829 return STATUS_INVALID_PARAMETER
;
835 if ((ptr
= wine_anon_mmap( NULL
, view_size
, VIRTUAL_GetUnixProt(vprot
), 0 )) == (void *)-1)
837 if (errno
== ENOMEM
) return STATUS_NO_MEMORY
;
838 return STATUS_INVALID_PARAMETER
;
840 TRACE( "got mem with anon mmap %p-%p\n", ptr
, (char *)ptr
+ size
);
841 /* if we got something beyond the user limit, unmap it and retry */
842 if (is_beyond_limit( ptr
, view_size
, user_space_limit
)) add_reserved_area( ptr
, view_size
);
845 ptr
= unmap_extra_space( ptr
, view_size
, size
, mask
);
848 status
= create_view( view_ret
, ptr
, size
, vprot
);
849 if (status
!= STATUS_SUCCESS
) unmap_area( ptr
, size
);
854 /***********************************************************************
857 * Wrapper for mmap() to map a file into a view, falling back to read if mmap fails.
858 * The csVirtual section must be held by caller.
860 static NTSTATUS
map_file_into_view( struct file_view
*view
, int fd
, size_t start
, size_t size
,
861 off_t offset
, unsigned int vprot
, BOOL removable
)
864 int prot
= VIRTUAL_GetUnixProt( vprot
| VPROT_COMMITTED
/* make sure it is accessible */ );
865 BOOL shared_write
= (vprot
& VPROT_WRITE
) != 0;
867 assert( start
< view
->size
);
868 assert( start
+ size
<= view
->size
);
870 if (force_exec_prot
&& !(vprot
& VPROT_NOEXEC
) && (vprot
& VPROT_READ
))
872 TRACE( "forcing exec permission on mapping %p-%p\n",
873 (char *)view
->base
+ start
, (char *)view
->base
+ start
+ size
- 1 );
877 /* only try mmap if media is not removable (or if we require write access) */
878 if (!removable
|| shared_write
)
880 int flags
= MAP_FIXED
| (shared_write
? MAP_SHARED
: MAP_PRIVATE
);
882 if (mmap( (char *)view
->base
+ start
, size
, prot
, flags
, fd
, offset
) != (void *)-1)
885 if ((errno
== EPERM
) && (prot
& PROT_EXEC
))
886 ERR( "failed to set %08x protection on file map, noexec filesystem?\n", prot
);
888 /* mmap() failed; if this is because the file offset is not */
889 /* page-aligned (EINVAL), or because the underlying filesystem */
890 /* does not support mmap() (ENOEXEC,ENODEV), we do it by hand. */
891 if ((errno
!= ENOEXEC
) && (errno
!= EINVAL
) && (errno
!= ENODEV
)) return FILE_GetNtStatus();
892 if (shared_write
) /* we cannot fake shared write mappings */
894 if (errno
== EINVAL
) return STATUS_INVALID_PARAMETER
;
895 ERR( "shared writable mmap not supported, broken filesystem?\n" );
896 return STATUS_NOT_SUPPORTED
;
900 /* Reserve the memory with an anonymous mmap */
901 ptr
= wine_anon_mmap( (char *)view
->base
+ start
, size
, PROT_READ
| PROT_WRITE
, MAP_FIXED
);
902 if (ptr
== (void *)-1) return FILE_GetNtStatus();
903 /* Now read in the file */
904 pread( fd
, ptr
, size
, offset
);
905 if (prot
!= (PROT_READ
|PROT_WRITE
)) mprotect( ptr
, size
, prot
); /* Set the right protection */
907 memset( view
->prot
+ (start
>> page_shift
), vprot
, ROUND_SIZE(start
,size
) >> page_shift
);
908 return STATUS_SUCCESS
;
912 /***********************************************************************
915 * Get the size of the committed range starting at base.
916 * Also return the protections for the first page.
918 static SIZE_T
get_committed_size( struct file_view
*view
, void *base
, BYTE
*vprot
)
922 start
= ((char *)base
- (char *)view
->base
) >> page_shift
;
923 *vprot
= view
->prot
[start
];
925 if (view
->mapping
&& !(view
->protect
& VPROT_COMMITTED
))
928 SERVER_START_REQ( get_mapping_committed_range
)
930 req
->handle
= wine_server_obj_handle( view
->mapping
);
931 req
->offset
= start
<< page_shift
;
932 if (!wine_server_call( req
))
935 if (reply
->committed
)
937 *vprot
|= VPROT_COMMITTED
;
938 for (i
= 0; i
< ret
>> page_shift
; i
++) view
->prot
[start
+i
] |= VPROT_COMMITTED
;
945 for (i
= start
+ 1; i
< view
->size
>> page_shift
; i
++)
946 if ((*vprot
^ view
->prot
[i
]) & VPROT_COMMITTED
) break;
947 return (i
- start
) << page_shift
;
951 /***********************************************************************
954 * Decommit some pages of a given view.
955 * The csVirtual section must be held by caller.
957 static NTSTATUS
decommit_pages( struct file_view
*view
, size_t start
, size_t size
)
959 if (wine_anon_mmap( (char *)view
->base
+ start
, size
, PROT_NONE
, MAP_FIXED
) != (void *)-1)
961 BYTE
*p
= view
->prot
+ (start
>> page_shift
);
963 while (size
--) *p
++ &= ~VPROT_COMMITTED
;
964 return STATUS_SUCCESS
;
966 return FILE_GetNtStatus();
970 /***********************************************************************
971 * allocate_dos_memory
973 * Allocate the DOS memory range.
975 static NTSTATUS
allocate_dos_memory( struct file_view
**view
, unsigned int vprot
)
979 void * const low_64k
= (void *)0x10000;
980 const size_t dosmem_size
= 0x110000;
981 int unix_prot
= VIRTUAL_GetUnixProt( vprot
);
984 /* check for existing view */
986 if ((ptr
= list_head( &views_list
)))
988 struct file_view
*first_view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
989 if (first_view
->base
< (void *)dosmem_size
) return STATUS_CONFLICTING_ADDRESSES
;
992 /* check without the first 64K */
994 if (wine_mmap_is_in_reserved_area( low_64k
, dosmem_size
- 0x10000 ) != 1)
996 addr
= wine_anon_mmap( low_64k
, dosmem_size
- 0x10000, unix_prot
, 0 );
999 if (addr
!= (void *)-1) munmap( addr
, dosmem_size
- 0x10000 );
1000 return map_view( view
, NULL
, dosmem_size
, 0xffff, 0, vprot
);
1004 /* now try to allocate the low 64K too */
1006 if (wine_mmap_is_in_reserved_area( NULL
, 0x10000 ) != 1)
1008 addr
= wine_anon_mmap( (void *)page_size
, 0x10000 - page_size
, unix_prot
, 0 );
1009 if (addr
== (void *)page_size
)
1011 if (!wine_anon_mmap( NULL
, page_size
, unix_prot
, MAP_FIXED
))
1014 TRACE( "successfully mapped low 64K range\n" );
1016 else TRACE( "failed to map page 0\n" );
1020 if (addr
!= (void *)-1) munmap( addr
, 0x10000 - page_size
);
1022 TRACE( "failed to map low 64K range\n" );
1026 /* now reserve the whole range */
1028 size
= (char *)dosmem_size
- (char *)addr
;
1029 wine_anon_mmap( addr
, size
, unix_prot
, MAP_FIXED
);
1030 return create_view( view
, addr
, size
, vprot
);
1034 /***********************************************************************
1037 * Stat the underlying file for a memory view.
1039 static NTSTATUS
stat_mapping_file( struct file_view
*view
, struct stat
*st
)
1042 int unix_fd
, needs_close
;
1044 if (!view
->mapping
) return STATUS_NOT_MAPPED_VIEW
;
1045 if (!(status
= server_get_unix_fd( view
->mapping
, 0, &unix_fd
, &needs_close
, NULL
, NULL
)))
1047 if (fstat( unix_fd
, st
) == -1) status
= FILE_GetNtStatus();
1048 if (needs_close
) close( unix_fd
);
1054 /***********************************************************************
1057 * Map an executable (PE format) image into memory.
1059 static NTSTATUS
map_image( HANDLE hmapping
, int fd
, char *base
, SIZE_T total_size
, SIZE_T mask
,
1060 SIZE_T header_size
, int shared_fd
, HANDLE dup_mapping
, unsigned int map_vprot
, PVOID
*addr_ptr
)
1062 IMAGE_DOS_HEADER
*dos
;
1063 IMAGE_NT_HEADERS
*nt
;
1064 IMAGE_SECTION_HEADER sections
[96];
1065 IMAGE_SECTION_HEADER
*sec
;
1066 IMAGE_DATA_DIRECTORY
*imports
;
1067 NTSTATUS status
= STATUS_CONFLICTING_ADDRESSES
;
1072 struct file_view
*view
= NULL
;
1073 char *ptr
, *header_end
, *header_start
;
1076 /* zero-map the whole range */
1078 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1080 if (base
>= (char *)address_space_start
) /* make sure the DOS area remains free */
1081 status
= map_view( &view
, base
, total_size
, mask
, FALSE
,
1082 VPROT_COMMITTED
| VPROT_READ
| VPROT_EXEC
| VPROT_WRITECOPY
| VPROT_IMAGE
);
1084 if (status
!= STATUS_SUCCESS
)
1085 status
= map_view( &view
, NULL
, total_size
, mask
, FALSE
,
1086 VPROT_COMMITTED
| VPROT_READ
| VPROT_EXEC
| VPROT_WRITECOPY
| VPROT_IMAGE
);
1088 if (status
!= STATUS_SUCCESS
) goto error
;
1091 TRACE_(module
)( "mapped PE file at %p-%p\n", ptr
, ptr
+ total_size
);
1093 /* map the header */
1095 if (fstat( fd
, &st
) == -1)
1097 status
= FILE_GetNtStatus();
1100 status
= STATUS_INVALID_IMAGE_FORMAT
; /* generic error */
1101 if (!st
.st_size
) goto error
;
1102 header_size
= min( header_size
, st
.st_size
);
1103 if (map_file_into_view( view
, fd
, 0, header_size
, 0, VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
,
1104 !dup_mapping
) != STATUS_SUCCESS
) goto error
;
1105 dos
= (IMAGE_DOS_HEADER
*)ptr
;
1106 nt
= (IMAGE_NT_HEADERS
*)(ptr
+ dos
->e_lfanew
);
1107 header_end
= ptr
+ ROUND_SIZE( 0, header_size
);
1108 memset( ptr
+ header_size
, 0, header_end
- (ptr
+ header_size
) );
1109 if ((char *)(nt
+ 1) > header_end
) goto error
;
1110 header_start
= (char*)&nt
->OptionalHeader
+nt
->FileHeader
.SizeOfOptionalHeader
;
1111 if (nt
->FileHeader
.NumberOfSections
> sizeof(sections
)/sizeof(*sections
)) goto error
;
1112 if (header_start
+ sizeof(*sections
) * nt
->FileHeader
.NumberOfSections
> header_end
) goto error
;
1113 /* Some applications (e.g. the Steam version of Borderlands) map over the top of the section headers,
1114 * copying the headers into local memory is necessary to properly load such applications. */
1115 memcpy(sections
, header_start
, sizeof(*sections
) * nt
->FileHeader
.NumberOfSections
);
1118 imports
= nt
->OptionalHeader
.DataDirectory
+ IMAGE_DIRECTORY_ENTRY_IMPORT
;
1119 if (!imports
->Size
|| !imports
->VirtualAddress
) imports
= NULL
;
1121 /* check for non page-aligned binary */
1123 if (nt
->OptionalHeader
.SectionAlignment
<= page_mask
)
1125 /* unaligned sections, this happens for native subsystem binaries */
1126 /* in that case Windows simply maps in the whole file */
1128 if (map_file_into_view( view
, fd
, 0, total_size
, 0, VPROT_COMMITTED
| VPROT_READ
,
1129 !dup_mapping
) != STATUS_SUCCESS
) goto error
;
1131 /* check that all sections are loaded at the right offset */
1132 if (nt
->OptionalHeader
.FileAlignment
!= nt
->OptionalHeader
.SectionAlignment
) goto error
;
1133 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++)
1135 if (sec
[i
].VirtualAddress
!= sec
[i
].PointerToRawData
)
1136 goto error
; /* Windows refuses to load in that case too */
1139 /* set the image protections */
1140 VIRTUAL_SetProt( view
, ptr
, total_size
,
1141 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
| VPROT_EXEC
);
1143 /* no relocations are performed on non page-aligned binaries */
1148 /* map all the sections */
1150 for (i
= pos
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
1152 static const SIZE_T sector_align
= 0x1ff;
1153 SIZE_T map_size
, file_start
, file_size
, end
;
1155 if (!sec
->Misc
.VirtualSize
)
1156 map_size
= ROUND_SIZE( 0, sec
->SizeOfRawData
);
1158 map_size
= ROUND_SIZE( 0, sec
->Misc
.VirtualSize
);
1160 /* file positions are rounded to sector boundaries regardless of OptionalHeader.FileAlignment */
1161 file_start
= sec
->PointerToRawData
& ~sector_align
;
1162 file_size
= (sec
->SizeOfRawData
+ (sec
->PointerToRawData
& sector_align
) + sector_align
) & ~sector_align
;
1163 if (file_size
> map_size
) file_size
= map_size
;
1165 /* a few sanity checks */
1166 end
= sec
->VirtualAddress
+ ROUND_SIZE( sec
->VirtualAddress
, map_size
);
1167 if (sec
->VirtualAddress
> total_size
|| end
> total_size
|| end
< sec
->VirtualAddress
)
1169 WARN_(module
)( "Section %.8s too large (%x+%lx/%lx)\n",
1170 sec
->Name
, sec
->VirtualAddress
, map_size
, total_size
);
1174 if ((sec
->Characteristics
& IMAGE_SCN_MEM_SHARED
) &&
1175 (sec
->Characteristics
& IMAGE_SCN_MEM_WRITE
))
1177 TRACE_(module
)( "mapping shared section %.8s at %p off %x (%x) size %lx (%lx) flags %x\n",
1178 sec
->Name
, ptr
+ sec
->VirtualAddress
,
1179 sec
->PointerToRawData
, (int)pos
, file_size
, map_size
,
1180 sec
->Characteristics
);
1181 if (map_file_into_view( view
, shared_fd
, sec
->VirtualAddress
, map_size
, pos
,
1182 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITE
,
1183 FALSE
) != STATUS_SUCCESS
)
1185 ERR_(module
)( "Could not map shared section %.8s\n", sec
->Name
);
1189 /* check if the import directory falls inside this section */
1190 if (imports
&& imports
->VirtualAddress
>= sec
->VirtualAddress
&&
1191 imports
->VirtualAddress
< sec
->VirtualAddress
+ map_size
)
1193 UINT_PTR base
= imports
->VirtualAddress
& ~page_mask
;
1194 UINT_PTR end
= base
+ ROUND_SIZE( imports
->VirtualAddress
, imports
->Size
);
1195 if (end
> sec
->VirtualAddress
+ map_size
) end
= sec
->VirtualAddress
+ map_size
;
1197 map_file_into_view( view
, shared_fd
, base
, end
- base
,
1198 pos
+ (base
- sec
->VirtualAddress
),
1199 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
,
1206 TRACE_(module
)( "mapping section %.8s at %p off %x size %x virt %x flags %x\n",
1207 sec
->Name
, ptr
+ sec
->VirtualAddress
,
1208 sec
->PointerToRawData
, sec
->SizeOfRawData
,
1209 sec
->Misc
.VirtualSize
, sec
->Characteristics
);
1211 if (!sec
->PointerToRawData
|| !file_size
) continue;
1213 /* Note: if the section is not aligned properly map_file_into_view will magically
1214 * fall back to read(), so we don't need to check anything here.
1216 end
= file_start
+ file_size
;
1217 if (sec
->PointerToRawData
>= st
.st_size
||
1218 end
> ((st
.st_size
+ sector_align
) & ~sector_align
) ||
1220 map_file_into_view( view
, fd
, sec
->VirtualAddress
, file_size
, file_start
,
1221 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
,
1222 !dup_mapping
) != STATUS_SUCCESS
)
1224 ERR_(module
)( "Could not map section %.8s, file probably truncated\n", sec
->Name
);
1228 if (file_size
& page_mask
)
1230 end
= ROUND_SIZE( 0, file_size
);
1231 if (end
> map_size
) end
= map_size
;
1232 TRACE_(module
)("clearing %p - %p\n",
1233 ptr
+ sec
->VirtualAddress
+ file_size
,
1234 ptr
+ sec
->VirtualAddress
+ end
);
1235 memset( ptr
+ sec
->VirtualAddress
+ file_size
, 0, end
- file_size
);
1240 /* perform base relocation, if necessary */
1243 ((nt
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
) ||
1244 !NtCurrentTeb()->Peb
->ImageBaseAddress
) )
1246 IMAGE_BASE_RELOCATION
*rel
, *end
;
1247 const IMAGE_DATA_DIRECTORY
*relocs
;
1249 if (nt
->FileHeader
.Characteristics
& IMAGE_FILE_RELOCS_STRIPPED
)
1251 WARN_(module
)( "Need to relocate module from %p to %p, but there are no relocation records\n",
1253 status
= STATUS_CONFLICTING_ADDRESSES
;
1257 TRACE_(module
)( "relocating from %p-%p to %p-%p\n",
1258 base
, base
+ total_size
, ptr
, ptr
+ total_size
);
1260 relocs
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1261 rel
= (IMAGE_BASE_RELOCATION
*)(ptr
+ relocs
->VirtualAddress
);
1262 end
= (IMAGE_BASE_RELOCATION
*)(ptr
+ relocs
->VirtualAddress
+ relocs
->Size
);
1265 while (rel
< end
- 1 && rel
->SizeOfBlock
)
1267 if (rel
->VirtualAddress
>= total_size
)
1269 WARN_(module
)( "invalid address %p in relocation %p\n", ptr
+ rel
->VirtualAddress
, rel
);
1270 status
= STATUS_ACCESS_VIOLATION
;
1273 rel
= LdrProcessRelocationBlock( ptr
+ rel
->VirtualAddress
,
1274 (rel
->SizeOfBlock
- sizeof(*rel
)) / sizeof(USHORT
),
1275 (USHORT
*)(rel
+ 1), delta
);
1276 if (!rel
) goto error
;
1280 /* set the image protections */
1282 VIRTUAL_SetProt( view
, ptr
, ROUND_SIZE( 0, header_size
), VPROT_COMMITTED
| VPROT_READ
);
1285 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
1288 BYTE vprot
= VPROT_COMMITTED
;
1290 if (sec
->Misc
.VirtualSize
)
1291 size
= ROUND_SIZE( sec
->VirtualAddress
, sec
->Misc
.VirtualSize
);
1293 size
= ROUND_SIZE( sec
->VirtualAddress
, sec
->SizeOfRawData
);
1295 if (sec
->Characteristics
& IMAGE_SCN_MEM_READ
) vprot
|= VPROT_READ
;
1296 if (sec
->Characteristics
& IMAGE_SCN_MEM_WRITE
) vprot
|= VPROT_WRITECOPY
;
1297 if (sec
->Characteristics
& IMAGE_SCN_MEM_EXECUTE
) vprot
|= VPROT_EXEC
;
1299 /* Dumb game crack lets the AOEP point into a data section. Adjust. */
1300 if ((nt
->OptionalHeader
.AddressOfEntryPoint
>= sec
->VirtualAddress
) &&
1301 (nt
->OptionalHeader
.AddressOfEntryPoint
< sec
->VirtualAddress
+ size
))
1302 vprot
|= VPROT_EXEC
;
1304 if (!VIRTUAL_SetProt( view
, ptr
+ sec
->VirtualAddress
, size
, vprot
) && (vprot
& VPROT_EXEC
))
1305 ERR( "failed to set %08x protection on section %.8s, noexec filesystem?\n",
1306 sec
->Characteristics
, sec
->Name
);
1310 view
->mapping
= dup_mapping
;
1311 view
->map_protect
= map_vprot
;
1312 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1315 #ifdef VALGRIND_LOAD_PDB_DEBUGINFO
1316 VALGRIND_LOAD_PDB_DEBUGINFO(fd
, ptr
, total_size
, delta
);
1318 if (ptr
!= base
) return STATUS_IMAGE_NOT_AT_BASE
;
1319 return STATUS_SUCCESS
;
1322 if (view
) delete_view( view
);
1323 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1324 if (dup_mapping
) NtClose( dup_mapping
);
1329 /* callback for wine_mmap_enum_reserved_areas to allocate space for the virtual heap */
1330 static int alloc_virtual_heap( void *base
, size_t size
, void *arg
)
1332 void **heap_base
= arg
;
1334 if (is_beyond_limit( base
, size
, address_space_limit
)) address_space_limit
= (char *)base
+ size
;
1335 if (size
< VIRTUAL_HEAP_SIZE
) return 0;
1336 if (is_win64
&& base
< (void *)0x80000000) return 0;
1337 *heap_base
= wine_anon_mmap( (char *)base
+ size
- VIRTUAL_HEAP_SIZE
,
1338 VIRTUAL_HEAP_SIZE
, PROT_READ
|PROT_WRITE
, MAP_FIXED
);
1339 return (*heap_base
!= (void *)-1);
1342 /***********************************************************************
1345 void virtual_init(void)
1347 const char *preload
;
1350 struct file_view
*heap_view
;
1352 #if !defined(__i386__) && !defined(__x86_64__)
1353 page_size
= sysconf( _SC_PAGESIZE
);
1354 page_mask
= page_size
- 1;
1355 /* Make sure we have a power of 2 */
1356 assert( !(page_size
& page_mask
) );
1358 while ((1 << page_shift
) != page_size
) page_shift
++;
1359 user_space_limit
= working_set_limit
= address_space_limit
= (void *)~page_mask
;
1360 #endif /* page_mask */
1361 if ((preload
= getenv("WINEPRELOADRESERVE")))
1363 unsigned long start
, end
;
1364 if (sscanf( preload
, "%lx-%lx", &start
, &end
) == 2)
1366 preload_reserve_start
= (void *)start
;
1367 preload_reserve_end
= (void *)end
;
1371 /* try to find space in a reserved area for the virtual heap */
1372 if (!wine_mmap_enum_reserved_areas( alloc_virtual_heap
, &heap_base
, 1 ))
1373 heap_base
= wine_anon_mmap( NULL
, VIRTUAL_HEAP_SIZE
, PROT_READ
|PROT_WRITE
, 0 );
1375 assert( heap_base
!= (void *)-1 );
1376 virtual_heap
= RtlCreateHeap( HEAP_NO_SERIALIZE
, heap_base
, VIRTUAL_HEAP_SIZE
,
1377 VIRTUAL_HEAP_SIZE
, NULL
, NULL
);
1378 create_view( &heap_view
, heap_base
, VIRTUAL_HEAP_SIZE
, VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITE
);
1380 /* make the DOS area accessible (except the low 64K) to hide bugs in broken apps like Excel 2003 */
1381 size
= (char *)address_space_start
- (char *)0x10000;
1382 if (size
&& wine_mmap_is_in_reserved_area( (void*)0x10000, size
) == 1)
1383 wine_anon_mmap( (void *)0x10000, size
, PROT_READ
| PROT_WRITE
, MAP_FIXED
);
1387 /***********************************************************************
1388 * virtual_init_threading
1390 void virtual_init_threading(void)
1396 /***********************************************************************
1397 * virtual_get_system_info
1399 void virtual_get_system_info( SYSTEM_BASIC_INFORMATION
*info
)
1402 info
->KeMaximumIncrement
= 0; /* FIXME */
1403 info
->PageSize
= page_size
;
1404 info
->MmLowestPhysicalPage
= 1;
1405 info
->MmHighestPhysicalPage
= 0x7fffffff / page_size
;
1406 info
->MmNumberOfPhysicalPages
= info
->MmHighestPhysicalPage
- info
->MmLowestPhysicalPage
;
1407 info
->AllocationGranularity
= get_mask(0) + 1;
1408 info
->LowestUserAddress
= (void *)0x10000;
1409 info
->HighestUserAddress
= (char *)user_space_limit
- 1;
1410 info
->ActiveProcessorsAffinityMask
= (1 << NtCurrentTeb()->Peb
->NumberOfProcessors
) - 1;
1411 info
->NumberOfProcessors
= NtCurrentTeb()->Peb
->NumberOfProcessors
;
1415 /***********************************************************************
1416 * virtual_create_builtin_view
1418 NTSTATUS
virtual_create_builtin_view( void *module
)
1422 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( module
);
1423 SIZE_T size
= nt
->OptionalHeader
.SizeOfImage
;
1424 IMAGE_SECTION_HEADER
*sec
;
1425 struct file_view
*view
;
1429 size
= ROUND_SIZE( module
, size
);
1430 base
= ROUND_ADDR( module
, page_mask
);
1431 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1432 status
= create_view( &view
, base
, size
, VPROT_SYSTEM
| VPROT_IMAGE
|
1433 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
| VPROT_EXEC
);
1434 if (!status
) TRACE( "created %p-%p\n", base
, (char *)base
+ size
);
1435 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1437 if (status
) return status
;
1439 /* The PE header is always read-only, no write, no execute. */
1440 view
->prot
[0] = VPROT_COMMITTED
| VPROT_READ
;
1442 sec
= (IMAGE_SECTION_HEADER
*)((char *)&nt
->OptionalHeader
+ nt
->FileHeader
.SizeOfOptionalHeader
);
1443 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++)
1445 BYTE flags
= VPROT_COMMITTED
;
1447 if (sec
[i
].Characteristics
& IMAGE_SCN_MEM_EXECUTE
) flags
|= VPROT_EXEC
;
1448 if (sec
[i
].Characteristics
& IMAGE_SCN_MEM_READ
) flags
|= VPROT_READ
;
1449 if (sec
[i
].Characteristics
& IMAGE_SCN_MEM_WRITE
) flags
|= VPROT_WRITE
;
1450 memset (view
->prot
+ (sec
[i
].VirtualAddress
>> page_shift
), flags
,
1451 ROUND_SIZE( sec
[i
].VirtualAddress
, sec
[i
].Misc
.VirtualSize
) >> page_shift
);
1458 /***********************************************************************
1459 * virtual_alloc_thread_stack
1461 NTSTATUS
virtual_alloc_thread_stack( TEB
*teb
, SIZE_T reserve_size
, SIZE_T commit_size
)
1463 struct file_view
*view
;
1468 if (!reserve_size
|| !commit_size
)
1470 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( NtCurrentTeb()->Peb
->ImageBaseAddress
);
1471 if (!reserve_size
) reserve_size
= nt
->OptionalHeader
.SizeOfStackReserve
;
1472 if (!commit_size
) commit_size
= nt
->OptionalHeader
.SizeOfStackCommit
;
1475 size
= max( reserve_size
, commit_size
);
1476 if (size
< 1024 * 1024) size
= 1024 * 1024; /* Xlib needs a large stack */
1477 size
= (size
+ 0xffff) & ~0xffff; /* round to 64K boundary */
1479 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1481 if ((status
= map_view( &view
, NULL
, size
, 0xffff, 0,
1482 VPROT_READ
| VPROT_WRITE
| VPROT_COMMITTED
| VPROT_VALLOC
)) != STATUS_SUCCESS
)
1485 #ifdef VALGRIND_STACK_REGISTER
1486 VALGRIND_STACK_REGISTER( view
->base
, (char *)view
->base
+ view
->size
);
1489 /* setup no access guard page */
1490 VIRTUAL_SetProt( view
, view
->base
, page_size
, VPROT_COMMITTED
);
1491 VIRTUAL_SetProt( view
, (char *)view
->base
+ page_size
, page_size
,
1492 VPROT_READ
| VPROT_WRITE
| VPROT_COMMITTED
| VPROT_GUARD
);
1494 /* note: limit is lower than base since the stack grows down */
1495 teb
->DeallocationStack
= view
->base
;
1496 teb
->Tib
.StackBase
= (char *)view
->base
+ view
->size
;
1497 teb
->Tib
.StackLimit
= (char *)view
->base
+ 2 * page_size
;
1499 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1504 /***********************************************************************
1505 * virtual_clear_thread_stack
1507 * Clear the stack contents before calling the main entry point, some broken apps need that.
1509 void virtual_clear_thread_stack(void)
1511 void *stack
= NtCurrentTeb()->Tib
.StackLimit
;
1512 size_t size
= (char *)NtCurrentTeb()->Tib
.StackBase
- (char *)NtCurrentTeb()->Tib
.StackLimit
;
1514 wine_anon_mmap( stack
, size
, PROT_READ
| PROT_WRITE
, MAP_FIXED
);
1515 if (force_exec_prot
) mprotect( stack
, size
, PROT_READ
| PROT_WRITE
| PROT_EXEC
);
1519 /***********************************************************************
1520 * virtual_handle_fault
1522 NTSTATUS
virtual_handle_fault( LPCVOID addr
, DWORD err
)
1524 struct file_view
*view
;
1525 NTSTATUS ret
= STATUS_ACCESS_VIOLATION
;
1528 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1529 if ((view
= VIRTUAL_FindView( addr
, 0 )))
1531 void *page
= ROUND_ADDR( addr
, page_mask
);
1532 BYTE
*vprot
= &view
->prot
[((const char *)page
- (const char *)view
->base
) >> page_shift
];
1533 if (*vprot
& VPROT_GUARD
)
1535 VIRTUAL_SetProt( view
, page
, page_size
, *vprot
& ~VPROT_GUARD
);
1536 ret
= STATUS_GUARD_PAGE_VIOLATION
;
1538 if ((err
& EXCEPTION_WRITE_FAULT
) && (view
->protect
& VPROT_WRITEWATCH
))
1540 if (*vprot
& VPROT_WRITEWATCH
)
1542 *vprot
&= ~VPROT_WRITEWATCH
;
1543 VIRTUAL_SetProt( view
, page
, page_size
, *vprot
);
1545 /* ignore fault if page is writable now */
1546 if (VIRTUAL_GetUnixProt( *vprot
) & PROT_WRITE
) ret
= STATUS_SUCCESS
;
1549 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1555 /***********************************************************************
1556 * virtual_is_valid_code_address
1558 BOOL
virtual_is_valid_code_address( const void *addr
, SIZE_T size
)
1560 struct file_view
*view
;
1564 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1565 if ((view
= VIRTUAL_FindView( addr
, size
)))
1566 ret
= !(view
->protect
& VPROT_SYSTEM
); /* system views are not visible to the app */
1567 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1572 /***********************************************************************
1573 * virtual_handle_stack_fault
1575 * Handle an access fault inside the current thread stack.
1576 * Called from inside a signal handler.
1578 BOOL
virtual_handle_stack_fault( void *addr
)
1580 struct file_view
*view
;
1583 RtlEnterCriticalSection( &csVirtual
); /* no need for signal masking inside signal handler */
1584 if ((view
= VIRTUAL_FindView( addr
, 0 )))
1586 void *page
= ROUND_ADDR( addr
, page_mask
);
1587 BYTE vprot
= view
->prot
[((const char *)page
- (const char *)view
->base
) >> page_shift
];
1588 if (vprot
& VPROT_GUARD
)
1590 VIRTUAL_SetProt( view
, page
, page_size
, vprot
& ~VPROT_GUARD
);
1591 NtCurrentTeb()->Tib
.StackLimit
= page
;
1592 if ((char *)page
>= (char *)NtCurrentTeb()->DeallocationStack
+ 2*page_size
)
1594 vprot
= view
->prot
[((char *)page
- page_size
- (char *)view
->base
) >> page_shift
];
1595 VIRTUAL_SetProt( view
, (char *)page
- page_size
, page_size
, vprot
| VPROT_GUARD
);
1600 RtlLeaveCriticalSection( &csVirtual
);
1605 /***********************************************************************
1606 * virtual_check_buffer_for_read
1608 * Check if a memory buffer can be read, triggering page faults if needed for DIB section access.
1610 BOOL
virtual_check_buffer_for_read( const void *ptr
, SIZE_T size
)
1612 if (!size
) return TRUE
;
1613 if (!ptr
) return FALSE
;
1617 volatile const char *p
= ptr
;
1618 char dummy
__attribute__((unused
));
1619 SIZE_T count
= size
;
1621 while (count
> page_size
)
1628 dummy
= p
[count
- 1];
1639 /***********************************************************************
1640 * virtual_check_buffer_for_write
1642 * Check if a memory buffer can be written to, triggering page faults if needed for write watches.
1644 BOOL
virtual_check_buffer_for_write( void *ptr
, SIZE_T size
)
1646 if (!size
) return TRUE
;
1647 if (!ptr
) return FALSE
;
1651 volatile char *p
= ptr
;
1652 SIZE_T count
= size
;
1654 while (count
> page_size
)
1672 /***********************************************************************
1673 * VIRTUAL_SetForceExec
1675 * Whether to force exec prot on all views.
1677 void VIRTUAL_SetForceExec( BOOL enable
)
1679 struct file_view
*view
;
1682 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1683 if (!force_exec_prot
!= !enable
) /* change all existing views */
1685 force_exec_prot
= enable
;
1687 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
1690 char *addr
= view
->base
;
1691 BYTE commit
= view
->mapping
? VPROT_COMMITTED
: 0; /* file mappings are always accessible */
1692 int unix_prot
= VIRTUAL_GetUnixProt( view
->prot
[0] | commit
);
1694 if (view
->protect
& VPROT_NOEXEC
) continue;
1695 for (count
= i
= 1; i
< view
->size
>> page_shift
; i
++, count
++)
1697 int prot
= VIRTUAL_GetUnixProt( view
->prot
[i
] | commit
);
1698 if (prot
== unix_prot
) continue;
1699 if ((unix_prot
& PROT_READ
) && !(unix_prot
& PROT_EXEC
))
1701 TRACE( "%s exec prot for %p-%p\n",
1702 force_exec_prot
? "enabling" : "disabling",
1703 addr
, addr
+ (count
<< page_shift
) - 1 );
1704 mprotect( addr
, count
<< page_shift
,
1705 unix_prot
| (force_exec_prot
? PROT_EXEC
: 0) );
1707 addr
+= (count
<< page_shift
);
1713 if ((unix_prot
& PROT_READ
) && !(unix_prot
& PROT_EXEC
))
1715 TRACE( "%s exec prot for %p-%p\n",
1716 force_exec_prot
? "enabling" : "disabling",
1717 addr
, addr
+ (count
<< page_shift
) - 1 );
1718 mprotect( addr
, count
<< page_shift
,
1719 unix_prot
| (force_exec_prot
? PROT_EXEC
: 0) );
1724 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1733 /* free reserved areas above the limit; callback for wine_mmap_enum_reserved_areas */
1734 static int free_reserved_memory( void *base
, size_t size
, void *arg
)
1736 struct free_range
*range
= arg
;
1738 if ((char *)base
>= range
->limit
) return 0;
1739 if ((char *)base
+ size
<= range
->base
) return 0;
1740 if ((char *)base
< range
->base
)
1742 size
-= range
->base
- (char *)base
;
1745 if ((char *)base
+ size
> range
->limit
) size
= range
->limit
- (char *)base
;
1746 remove_reserved_area( base
, size
);
1747 return 1; /* stop enumeration since the list has changed */
1750 /***********************************************************************
1751 * virtual_release_address_space
1753 * Release some address space once we have loaded and initialized the app.
1755 void virtual_release_address_space(void)
1757 struct free_range range
;
1760 if (is_win64
) return;
1762 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1764 range
.base
= (char *)0x82000000;
1765 range
.limit
= user_space_limit
;
1767 if (range
.limit
> range
.base
)
1769 while (wine_mmap_enum_reserved_areas( free_reserved_memory
, &range
, 1 )) /* nothing */;
1773 #ifndef __APPLE__ /* dyld doesn't support parts of the WINE_DOS segment being unmapped */
1774 range
.base
= (char *)0x20000000;
1775 range
.limit
= (char *)0x7f000000;
1776 while (wine_mmap_enum_reserved_areas( free_reserved_memory
, &range
, 0 )) /* nothing */;
1780 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1784 /***********************************************************************
1785 * virtual_set_large_address_space
1787 * Enable use of a large address space when allowed by the application.
1789 void virtual_set_large_address_space(void)
1791 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( NtCurrentTeb()->Peb
->ImageBaseAddress
);
1793 if (!(nt
->FileHeader
.Characteristics
& IMAGE_FILE_LARGE_ADDRESS_AWARE
)) return;
1794 /* no large address space on win9x */
1795 if (NtCurrentTeb()->Peb
->OSPlatformId
!= VER_PLATFORM_WIN32_NT
) return;
1797 user_space_limit
= working_set_limit
= address_space_limit
;
1801 /***********************************************************************
1802 * NtAllocateVirtualMemory (NTDLL.@)
1803 * ZwAllocateVirtualMemory (NTDLL.@)
1805 NTSTATUS WINAPI
NtAllocateVirtualMemory( HANDLE process
, PVOID
*ret
, ULONG zero_bits
,
1806 SIZE_T
*size_ptr
, ULONG type
, ULONG protect
)
1810 SIZE_T size
= *size_ptr
;
1811 SIZE_T mask
= get_mask( zero_bits
);
1812 NTSTATUS status
= STATUS_SUCCESS
;
1813 struct file_view
*view
;
1816 TRACE("%p %p %08lx %x %08x\n", process
, *ret
, size
, type
, protect
);
1818 if (!size
) return STATUS_INVALID_PARAMETER
;
1820 if (process
!= NtCurrentProcess())
1823 apc_result_t result
;
1825 memset( &call
, 0, sizeof(call
) );
1827 call
.virtual_alloc
.type
= APC_VIRTUAL_ALLOC
;
1828 call
.virtual_alloc
.addr
= wine_server_client_ptr( *ret
);
1829 call
.virtual_alloc
.size
= *size_ptr
;
1830 call
.virtual_alloc
.zero_bits
= zero_bits
;
1831 call
.virtual_alloc
.op_type
= type
;
1832 call
.virtual_alloc
.prot
= protect
;
1833 status
= server_queue_process_apc( process
, &call
, &result
);
1834 if (status
!= STATUS_SUCCESS
) return status
;
1836 if (result
.virtual_alloc
.status
== STATUS_SUCCESS
)
1838 *ret
= wine_server_get_ptr( result
.virtual_alloc
.addr
);
1839 *size_ptr
= result
.virtual_alloc
.size
;
1841 return result
.virtual_alloc
.status
;
1844 /* Round parameters to a page boundary */
1846 if (is_beyond_limit( 0, size
, working_set_limit
)) return STATUS_WORKING_SET_LIMIT_RANGE
;
1848 if ((status
= get_vprot_flags( protect
, &vprot
, FALSE
))) return status
;
1849 if (vprot
& VPROT_WRITECOPY
) return STATUS_INVALID_PAGE_PROTECTION
;
1850 vprot
|= VPROT_VALLOC
;
1851 if (type
& MEM_COMMIT
) vprot
|= VPROT_COMMITTED
;
1855 if (type
& MEM_RESERVE
) /* Round down to 64k boundary */
1856 base
= ROUND_ADDR( *ret
, mask
);
1858 base
= ROUND_ADDR( *ret
, page_mask
);
1859 size
= (((UINT_PTR
)*ret
+ size
+ page_mask
) & ~page_mask
) - (UINT_PTR
)base
;
1861 /* address 1 is magic to mean DOS area */
1862 if (!base
&& *ret
== (void *)1 && size
== 0x110000)
1864 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1865 status
= allocate_dos_memory( &view
, vprot
);
1866 if (status
== STATUS_SUCCESS
)
1869 *size_ptr
= view
->size
;
1871 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1875 /* disallow low 64k, wrap-around and kernel space */
1876 if (((char *)base
< (char *)0x10000) ||
1877 ((char *)base
+ size
< (char *)base
) ||
1878 is_beyond_limit( base
, size
, address_space_limit
))
1879 return STATUS_INVALID_PARAMETER
;
1884 size
= (size
+ page_mask
) & ~page_mask
;
1887 /* Compute the alloc type flags */
1889 if (!(type
& (MEM_COMMIT
| MEM_RESERVE
| MEM_RESET
)) ||
1890 (type
& ~(MEM_COMMIT
| MEM_RESERVE
| MEM_TOP_DOWN
| MEM_WRITE_WATCH
| MEM_RESET
)))
1892 WARN("called with wrong alloc type flags (%08x) !\n", type
);
1893 return STATUS_INVALID_PARAMETER
;
1896 /* Reserve the memory */
1898 if (use_locks
) server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1900 if ((type
& MEM_RESERVE
) || !base
)
1902 if (type
& MEM_WRITE_WATCH
) vprot
|= VPROT_WRITEWATCH
;
1903 status
= map_view( &view
, base
, size
, mask
, type
& MEM_TOP_DOWN
, vprot
);
1904 if (status
== STATUS_SUCCESS
) base
= view
->base
;
1906 else if (type
& MEM_RESET
)
1908 if (!(view
= VIRTUAL_FindView( base
, size
))) status
= STATUS_NOT_MAPPED_VIEW
;
1909 else madvise( base
, size
, MADV_DONTNEED
);
1911 else /* commit the pages */
1913 if (!(view
= VIRTUAL_FindView( base
, size
))) status
= STATUS_NOT_MAPPED_VIEW
;
1914 else if (view
->mapping
&& (view
->protect
& VPROT_COMMITTED
)) status
= STATUS_ALREADY_COMMITTED
;
1915 else if (!VIRTUAL_SetProt( view
, base
, size
, vprot
)) status
= STATUS_ACCESS_DENIED
;
1916 else if (view
->mapping
&& !(view
->protect
& VPROT_COMMITTED
))
1918 SERVER_START_REQ( add_mapping_committed_range
)
1920 req
->handle
= wine_server_obj_handle( view
->mapping
);
1921 req
->offset
= (char *)base
- (char *)view
->base
;
1923 wine_server_call( req
);
1929 if (use_locks
) server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1931 if (status
== STATUS_SUCCESS
)
1940 /***********************************************************************
1941 * NtFreeVirtualMemory (NTDLL.@)
1942 * ZwFreeVirtualMemory (NTDLL.@)
1944 NTSTATUS WINAPI
NtFreeVirtualMemory( HANDLE process
, PVOID
*addr_ptr
, SIZE_T
*size_ptr
, ULONG type
)
1946 struct file_view
*view
;
1949 NTSTATUS status
= STATUS_SUCCESS
;
1950 LPVOID addr
= *addr_ptr
;
1951 SIZE_T size
= *size_ptr
;
1953 TRACE("%p %p %08lx %x\n", process
, addr
, size
, type
);
1955 if (process
!= NtCurrentProcess())
1958 apc_result_t result
;
1960 memset( &call
, 0, sizeof(call
) );
1962 call
.virtual_free
.type
= APC_VIRTUAL_FREE
;
1963 call
.virtual_free
.addr
= wine_server_client_ptr( addr
);
1964 call
.virtual_free
.size
= size
;
1965 call
.virtual_free
.op_type
= type
;
1966 status
= server_queue_process_apc( process
, &call
, &result
);
1967 if (status
!= STATUS_SUCCESS
) return status
;
1969 if (result
.virtual_free
.status
== STATUS_SUCCESS
)
1971 *addr_ptr
= wine_server_get_ptr( result
.virtual_free
.addr
);
1972 *size_ptr
= result
.virtual_free
.size
;
1974 return result
.virtual_free
.status
;
1977 /* Fix the parameters */
1979 size
= ROUND_SIZE( addr
, size
);
1980 base
= ROUND_ADDR( addr
, page_mask
);
1982 /* avoid freeing the DOS area when a broken app passes a NULL pointer */
1983 if (!base
) return STATUS_INVALID_PARAMETER
;
1985 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1987 if (!(view
= VIRTUAL_FindView( base
, size
)) || !(view
->protect
& VPROT_VALLOC
))
1989 status
= STATUS_INVALID_PARAMETER
;
1991 else if (type
== MEM_RELEASE
)
1993 /* Free the pages */
1995 if (size
|| (base
!= view
->base
)) status
= STATUS_INVALID_PARAMETER
;
1998 delete_view( view
);
2003 else if (type
== MEM_DECOMMIT
)
2005 status
= decommit_pages( view
, base
- (char *)view
->base
, size
);
2006 if (status
== STATUS_SUCCESS
)
2014 WARN("called with wrong free type flags (%08x) !\n", type
);
2015 status
= STATUS_INVALID_PARAMETER
;
2018 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2022 static ULONG
map_protection_to_access( ULONG vprot
)
2024 vprot
&= VPROT_READ
| VPROT_WRITE
| VPROT_EXEC
| VPROT_WRITECOPY
;
2025 if (vprot
& VPROT_EXEC
)
2027 if (vprot
& VPROT_WRITE
) vprot
|= VPROT_WRITECOPY
;
2029 else vprot
&= ~VPROT_WRITECOPY
;
2033 static BOOL
is_compatible_protection( const struct file_view
*view
, ULONG new_prot
)
2035 ULONG view_prot
, map_prot
;
2037 view_prot
= map_protection_to_access( view
->protect
);
2038 new_prot
= map_protection_to_access( new_prot
);
2040 if (view_prot
== new_prot
) return TRUE
;
2041 if (!view_prot
) return FALSE
;
2043 if ((view_prot
& new_prot
) != new_prot
) return FALSE
;
2045 map_prot
= map_protection_to_access( view
->map_protect
);
2046 if ((map_prot
& new_prot
) == new_prot
) return TRUE
;
2051 /***********************************************************************
2052 * NtProtectVirtualMemory (NTDLL.@)
2053 * ZwProtectVirtualMemory (NTDLL.@)
2055 NTSTATUS WINAPI
NtProtectVirtualMemory( HANDLE process
, PVOID
*addr_ptr
, SIZE_T
*size_ptr
,
2056 ULONG new_prot
, ULONG
*old_prot
)
2058 struct file_view
*view
;
2060 NTSTATUS status
= STATUS_SUCCESS
;
2063 unsigned int new_vprot
;
2064 SIZE_T size
= *size_ptr
;
2065 LPVOID addr
= *addr_ptr
;
2067 TRACE("%p %p %08lx %08x\n", process
, addr
, size
, new_prot
);
2069 if (process
!= NtCurrentProcess())
2072 apc_result_t result
;
2074 memset( &call
, 0, sizeof(call
) );
2076 call
.virtual_protect
.type
= APC_VIRTUAL_PROTECT
;
2077 call
.virtual_protect
.addr
= wine_server_client_ptr( addr
);
2078 call
.virtual_protect
.size
= size
;
2079 call
.virtual_protect
.prot
= new_prot
;
2080 status
= server_queue_process_apc( process
, &call
, &result
);
2081 if (status
!= STATUS_SUCCESS
) return status
;
2083 if (result
.virtual_protect
.status
== STATUS_SUCCESS
)
2085 *addr_ptr
= wine_server_get_ptr( result
.virtual_protect
.addr
);
2086 *size_ptr
= result
.virtual_protect
.size
;
2087 if (old_prot
) *old_prot
= result
.virtual_protect
.prot
;
2089 return result
.virtual_protect
.status
;
2092 /* Fix the parameters */
2094 size
= ROUND_SIZE( addr
, size
);
2095 base
= ROUND_ADDR( addr
, page_mask
);
2097 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2099 if ((view
= VIRTUAL_FindView( base
, size
)))
2101 /* Make sure all the pages are committed */
2102 if (get_committed_size( view
, base
, &vprot
) >= size
&& (vprot
& VPROT_COMMITTED
))
2104 if (!(status
= get_vprot_flags( new_prot
, &new_vprot
, view
->protect
& VPROT_IMAGE
)))
2106 if ((new_vprot
& VPROT_WRITECOPY
) && (view
->protect
& VPROT_VALLOC
))
2107 status
= STATUS_INVALID_PAGE_PROTECTION
;
2110 if (!view
->mapping
|| is_compatible_protection( view
, new_vprot
))
2112 new_vprot
|= VPROT_COMMITTED
;
2113 if (old_prot
) *old_prot
= VIRTUAL_GetWin32Prot( vprot
);
2114 if (!VIRTUAL_SetProt( view
, base
, size
, new_vprot
)) status
= STATUS_ACCESS_DENIED
;
2116 else status
= STATUS_INVALID_PAGE_PROTECTION
;
2120 else status
= STATUS_NOT_COMMITTED
;
2122 else status
= STATUS_INVALID_PARAMETER
;
2124 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2126 if (status
== STATUS_SUCCESS
)
2135 /* retrieve state for a free memory area; callback for wine_mmap_enum_reserved_areas */
2136 static int get_free_mem_state_callback( void *start
, size_t size
, void *arg
)
2138 MEMORY_BASIC_INFORMATION
*info
= arg
;
2139 void *end
= (char *)start
+ size
;
2141 if ((char *)info
->BaseAddress
+ info
->RegionSize
< (char *)start
) return 0;
2143 if (info
->BaseAddress
>= end
)
2145 if (info
->AllocationBase
< end
) info
->AllocationBase
= end
;
2149 if (info
->BaseAddress
>= start
|| start
<= address_space_start
)
2151 /* it's a real free area */
2152 info
->State
= MEM_FREE
;
2153 info
->Protect
= PAGE_NOACCESS
;
2154 info
->AllocationBase
= 0;
2155 info
->AllocationProtect
= 0;
2157 if ((char *)info
->BaseAddress
+ info
->RegionSize
> (char *)end
)
2158 info
->RegionSize
= (char *)end
- (char *)info
->BaseAddress
;
2160 else /* outside of the reserved area, pretend it's allocated */
2162 info
->RegionSize
= (char *)start
- (char *)info
->BaseAddress
;
2163 info
->State
= MEM_RESERVE
;
2164 info
->Protect
= PAGE_NOACCESS
;
2165 info
->AllocationProtect
= PAGE_NOACCESS
;
2166 info
->Type
= MEM_PRIVATE
;
2171 #define UNIMPLEMENTED_INFO_CLASS(c) \
2173 FIXME("(process=%p,addr=%p) Unimplemented information class: " #c "\n", process, addr); \
2174 return STATUS_INVALID_INFO_CLASS
2176 /***********************************************************************
2177 * NtQueryVirtualMemory (NTDLL.@)
2178 * ZwQueryVirtualMemory (NTDLL.@)
2180 NTSTATUS WINAPI
NtQueryVirtualMemory( HANDLE process
, LPCVOID addr
,
2181 MEMORY_INFORMATION_CLASS info_class
, PVOID buffer
,
2182 SIZE_T len
, SIZE_T
*res_len
)
2184 struct file_view
*view
;
2185 char *base
, *alloc_base
= 0;
2188 MEMORY_BASIC_INFORMATION
*info
= buffer
;
2191 if (info_class
!= MemoryBasicInformation
)
2195 UNIMPLEMENTED_INFO_CLASS(MemoryWorkingSetList
);
2196 UNIMPLEMENTED_INFO_CLASS(MemorySectionName
);
2197 UNIMPLEMENTED_INFO_CLASS(MemoryBasicVlmInformation
);
2200 FIXME("(%p,%p,info_class=%d,%p,%ld,%p) Unknown information class\n",
2201 process
, addr
, info_class
, buffer
, len
, res_len
);
2202 return STATUS_INVALID_INFO_CLASS
;
2206 if (process
!= NtCurrentProcess())
2210 apc_result_t result
;
2212 memset( &call
, 0, sizeof(call
) );
2214 call
.virtual_query
.type
= APC_VIRTUAL_QUERY
;
2215 call
.virtual_query
.addr
= wine_server_client_ptr( addr
);
2216 status
= server_queue_process_apc( process
, &call
, &result
);
2217 if (status
!= STATUS_SUCCESS
) return status
;
2219 if (result
.virtual_query
.status
== STATUS_SUCCESS
)
2221 info
->BaseAddress
= wine_server_get_ptr( result
.virtual_query
.base
);
2222 info
->AllocationBase
= wine_server_get_ptr( result
.virtual_query
.alloc_base
);
2223 info
->RegionSize
= result
.virtual_query
.size
;
2224 info
->Protect
= result
.virtual_query
.prot
;
2225 info
->AllocationProtect
= result
.virtual_query
.alloc_prot
;
2226 info
->State
= (DWORD
)result
.virtual_query
.state
<< 12;
2227 info
->Type
= (DWORD
)result
.virtual_query
.alloc_type
<< 16;
2228 if (info
->RegionSize
!= result
.virtual_query
.size
) /* truncated */
2229 return STATUS_INVALID_PARAMETER
; /* FIXME */
2230 if (res_len
) *res_len
= sizeof(*info
);
2232 return result
.virtual_query
.status
;
2235 base
= ROUND_ADDR( addr
, page_mask
);
2237 if (is_beyond_limit( base
, 1, working_set_limit
)) return STATUS_WORKING_SET_LIMIT_RANGE
;
2239 /* Find the view containing the address */
2241 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2242 ptr
= list_head( &views_list
);
2247 size
= (char *)working_set_limit
- alloc_base
;
2251 view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
2252 if ((char *)view
->base
> base
)
2254 size
= (char *)view
->base
- alloc_base
;
2258 if ((char *)view
->base
+ view
->size
> base
)
2260 alloc_base
= view
->base
;
2264 alloc_base
= (char *)view
->base
+ view
->size
;
2265 ptr
= list_next( &views_list
, ptr
);
2268 /* Fill the info structure */
2270 info
->AllocationBase
= alloc_base
;
2271 info
->BaseAddress
= base
;
2272 info
->RegionSize
= size
- (base
- alloc_base
);
2276 if (!wine_mmap_enum_reserved_areas( get_free_mem_state_callback
, info
, 0 ))
2278 /* not in a reserved area at all, pretend it's allocated */
2280 if (base
>= (char *)address_space_start
)
2282 info
->State
= MEM_RESERVE
;
2283 info
->Protect
= PAGE_NOACCESS
;
2284 info
->AllocationProtect
= PAGE_NOACCESS
;
2285 info
->Type
= MEM_PRIVATE
;
2290 info
->State
= MEM_FREE
;
2291 info
->Protect
= PAGE_NOACCESS
;
2292 info
->AllocationBase
= 0;
2293 info
->AllocationProtect
= 0;
2301 SIZE_T range_size
= get_committed_size( view
, base
, &vprot
);
2303 info
->State
= (vprot
& VPROT_COMMITTED
) ? MEM_COMMIT
: MEM_RESERVE
;
2304 info
->Protect
= (vprot
& VPROT_COMMITTED
) ? VIRTUAL_GetWin32Prot( vprot
) : 0;
2305 info
->AllocationBase
= alloc_base
;
2306 info
->AllocationProtect
= VIRTUAL_GetWin32Prot( view
->protect
);
2307 if (view
->protect
& VPROT_IMAGE
) info
->Type
= MEM_IMAGE
;
2308 else if (view
->protect
& VPROT_VALLOC
) info
->Type
= MEM_PRIVATE
;
2309 else info
->Type
= MEM_MAPPED
;
2310 for (size
= base
- alloc_base
; size
< base
+ range_size
- alloc_base
; size
+= page_size
)
2311 if ((view
->prot
[size
>> page_shift
] ^ vprot
) & ~VPROT_WRITEWATCH
) break;
2312 info
->RegionSize
= size
- (base
- alloc_base
);
2314 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2316 if (res_len
) *res_len
= sizeof(*info
);
2317 return STATUS_SUCCESS
;
2321 /***********************************************************************
2322 * NtLockVirtualMemory (NTDLL.@)
2323 * ZwLockVirtualMemory (NTDLL.@)
2325 NTSTATUS WINAPI
NtLockVirtualMemory( HANDLE process
, PVOID
*addr
, SIZE_T
*size
, ULONG unknown
)
2327 NTSTATUS status
= STATUS_SUCCESS
;
2329 if (process
!= NtCurrentProcess())
2332 apc_result_t result
;
2334 memset( &call
, 0, sizeof(call
) );
2336 call
.virtual_lock
.type
= APC_VIRTUAL_LOCK
;
2337 call
.virtual_lock
.addr
= wine_server_client_ptr( *addr
);
2338 call
.virtual_lock
.size
= *size
;
2339 status
= server_queue_process_apc( process
, &call
, &result
);
2340 if (status
!= STATUS_SUCCESS
) return status
;
2342 if (result
.virtual_lock
.status
== STATUS_SUCCESS
)
2344 *addr
= wine_server_get_ptr( result
.virtual_lock
.addr
);
2345 *size
= result
.virtual_lock
.size
;
2347 return result
.virtual_lock
.status
;
2350 *size
= ROUND_SIZE( *addr
, *size
);
2351 *addr
= ROUND_ADDR( *addr
, page_mask
);
2353 if (mlock( *addr
, *size
)) status
= STATUS_ACCESS_DENIED
;
2358 /***********************************************************************
2359 * NtUnlockVirtualMemory (NTDLL.@)
2360 * ZwUnlockVirtualMemory (NTDLL.@)
2362 NTSTATUS WINAPI
NtUnlockVirtualMemory( HANDLE process
, PVOID
*addr
, SIZE_T
*size
, ULONG unknown
)
2364 NTSTATUS status
= STATUS_SUCCESS
;
2366 if (process
!= NtCurrentProcess())
2369 apc_result_t result
;
2371 memset( &call
, 0, sizeof(call
) );
2373 call
.virtual_unlock
.type
= APC_VIRTUAL_UNLOCK
;
2374 call
.virtual_unlock
.addr
= wine_server_client_ptr( *addr
);
2375 call
.virtual_unlock
.size
= *size
;
2376 status
= server_queue_process_apc( process
, &call
, &result
);
2377 if (status
!= STATUS_SUCCESS
) return status
;
2379 if (result
.virtual_unlock
.status
== STATUS_SUCCESS
)
2381 *addr
= wine_server_get_ptr( result
.virtual_unlock
.addr
);
2382 *size
= result
.virtual_unlock
.size
;
2384 return result
.virtual_unlock
.status
;
2387 *size
= ROUND_SIZE( *addr
, *size
);
2388 *addr
= ROUND_ADDR( *addr
, page_mask
);
2390 if (munlock( *addr
, *size
)) status
= STATUS_ACCESS_DENIED
;
2395 /***********************************************************************
2396 * NtCreateSection (NTDLL.@)
2397 * ZwCreateSection (NTDLL.@)
2399 NTSTATUS WINAPI
NtCreateSection( HANDLE
*handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
,
2400 const LARGE_INTEGER
*size
, ULONG protect
,
2401 ULONG sec_flags
, HANDLE file
)
2405 DWORD len
= (attr
&& attr
->ObjectName
) ? attr
->ObjectName
->Length
: 0;
2406 struct security_descriptor
*sd
= NULL
;
2407 struct object_attributes objattr
;
2409 /* Check parameters */
2411 if (len
> MAX_PATH
*sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
2413 if ((ret
= get_vprot_flags( protect
, &vprot
, sec_flags
& SEC_IMAGE
))) return ret
;
2415 objattr
.rootdir
= wine_server_obj_handle( attr
? attr
->RootDirectory
: 0 );
2417 objattr
.name_len
= len
;
2420 ret
= NTDLL_create_struct_sd( attr
->SecurityDescriptor
, &sd
, &objattr
.sd_len
);
2421 if (ret
!= STATUS_SUCCESS
) return ret
;
2424 if (!(sec_flags
& SEC_RESERVE
)) vprot
|= VPROT_COMMITTED
;
2425 if (sec_flags
& SEC_NOCACHE
) vprot
|= VPROT_NOCACHE
;
2426 if (sec_flags
& SEC_IMAGE
) vprot
|= VPROT_IMAGE
;
2428 /* Create the server object */
2430 SERVER_START_REQ( create_mapping
)
2432 req
->access
= access
;
2433 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
2434 req
->file_handle
= wine_server_obj_handle( file
);
2435 req
->size
= size
? size
->QuadPart
: 0;
2436 req
->protect
= vprot
;
2437 wine_server_add_data( req
, &objattr
, sizeof(objattr
) );
2438 if (objattr
.sd_len
) wine_server_add_data( req
, sd
, objattr
.sd_len
);
2439 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
2440 ret
= wine_server_call( req
);
2441 *handle
= wine_server_ptr_handle( reply
->handle
);
2445 NTDLL_free_struct_sd( sd
);
2451 /***********************************************************************
2452 * NtOpenSection (NTDLL.@)
2453 * ZwOpenSection (NTDLL.@)
2455 NTSTATUS WINAPI
NtOpenSection( HANDLE
*handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
)
2458 DWORD len
= attr
->ObjectName
->Length
;
2460 if (len
> MAX_PATH
*sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
2462 SERVER_START_REQ( open_mapping
)
2464 req
->access
= access
;
2465 req
->attributes
= attr
->Attributes
;
2466 req
->rootdir
= wine_server_obj_handle( attr
->RootDirectory
);
2467 wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
2468 if (!(ret
= wine_server_call( req
))) *handle
= wine_server_ptr_handle( reply
->handle
);
2475 /***********************************************************************
2476 * NtMapViewOfSection (NTDLL.@)
2477 * ZwMapViewOfSection (NTDLL.@)
2479 NTSTATUS WINAPI
NtMapViewOfSection( HANDLE handle
, HANDLE process
, PVOID
*addr_ptr
, ULONG zero_bits
,
2480 SIZE_T commit_size
, const LARGE_INTEGER
*offset_ptr
, SIZE_T
*size_ptr
,
2481 SECTION_INHERIT inherit
, ULONG alloc_type
, ULONG protect
)
2484 mem_size_t full_size
;
2486 SIZE_T size
, mask
= get_mask( zero_bits
);
2487 int unix_handle
= -1, needs_close
;
2488 unsigned int map_vprot
, vprot
;
2490 struct file_view
*view
;
2492 HANDLE dup_mapping
, shared_file
;
2493 LARGE_INTEGER offset
;
2496 offset
.QuadPart
= offset_ptr
? offset_ptr
->QuadPart
: 0;
2498 TRACE("handle=%p process=%p addr=%p off=%x%08x size=%lx access=%x\n",
2499 handle
, process
, *addr_ptr
, offset
.u
.HighPart
, offset
.u
.LowPart
, *size_ptr
, protect
);
2501 /* Check parameters */
2503 if ((offset
.u
.LowPart
& mask
) || (*addr_ptr
&& ((UINT_PTR
)*addr_ptr
& mask
)))
2504 return STATUS_INVALID_PARAMETER
;
2509 access
= SECTION_MAP_READ
;
2511 case PAGE_READWRITE
:
2512 case PAGE_EXECUTE_READWRITE
:
2513 access
= SECTION_MAP_WRITE
;
2516 case PAGE_WRITECOPY
:
2518 case PAGE_EXECUTE_READ
:
2519 case PAGE_EXECUTE_WRITECOPY
:
2520 access
= SECTION_MAP_READ
;
2523 return STATUS_INVALID_PAGE_PROTECTION
;
2526 if (process
!= NtCurrentProcess())
2529 apc_result_t result
;
2531 memset( &call
, 0, sizeof(call
) );
2533 call
.map_view
.type
= APC_MAP_VIEW
;
2534 call
.map_view
.handle
= wine_server_obj_handle( handle
);
2535 call
.map_view
.addr
= wine_server_client_ptr( *addr_ptr
);
2536 call
.map_view
.size
= *size_ptr
;
2537 call
.map_view
.offset
= offset
.QuadPart
;
2538 call
.map_view
.zero_bits
= zero_bits
;
2539 call
.map_view
.alloc_type
= alloc_type
;
2540 call
.map_view
.prot
= protect
;
2541 res
= server_queue_process_apc( process
, &call
, &result
);
2542 if (res
!= STATUS_SUCCESS
) return res
;
2544 if ((NTSTATUS
)result
.map_view
.status
>= 0)
2546 *addr_ptr
= wine_server_get_ptr( result
.map_view
.addr
);
2547 *size_ptr
= result
.map_view
.size
;
2549 return result
.map_view
.status
;
2552 SERVER_START_REQ( get_mapping_info
)
2554 req
->handle
= wine_server_obj_handle( handle
);
2555 req
->access
= access
;
2556 res
= wine_server_call( req
);
2557 map_vprot
= reply
->protect
;
2558 base
= wine_server_get_ptr( reply
->base
);
2559 full_size
= reply
->size
;
2560 header_size
= reply
->header_size
;
2561 dup_mapping
= wine_server_ptr_handle( reply
->mapping
);
2562 shared_file
= wine_server_ptr_handle( reply
->shared_file
);
2563 if ((ULONG_PTR
)base
!= reply
->base
) base
= NULL
;
2566 if (res
) return res
;
2568 if ((res
= server_get_unix_fd( handle
, 0, &unix_handle
, &needs_close
, NULL
, NULL
))) goto done
;
2570 if (map_vprot
& VPROT_IMAGE
)
2573 if (size
!= full_size
) /* truncated */
2575 WARN( "Modules larger than 4Gb (%s) not supported\n", wine_dbgstr_longlong(full_size
) );
2576 res
= STATUS_INVALID_PARAMETER
;
2581 int shared_fd
, shared_needs_close
;
2583 if ((res
= server_get_unix_fd( shared_file
, FILE_READ_DATA
|FILE_WRITE_DATA
,
2584 &shared_fd
, &shared_needs_close
, NULL
, NULL
))) goto done
;
2585 res
= map_image( handle
, unix_handle
, base
, size
, mask
, header_size
,
2586 shared_fd
, dup_mapping
, map_vprot
, addr_ptr
);
2587 if (shared_needs_close
) close( shared_fd
);
2588 NtClose( shared_file
);
2592 res
= map_image( handle
, unix_handle
, base
, size
, mask
, header_size
,
2593 -1, dup_mapping
, map_vprot
, addr_ptr
);
2595 if (needs_close
) close( unix_handle
);
2596 if (res
>= 0) *size_ptr
= size
;
2600 res
= STATUS_INVALID_PARAMETER
;
2601 if (offset
.QuadPart
>= full_size
) goto done
;
2604 if (*size_ptr
> full_size
- offset
.QuadPart
) goto done
;
2605 size
= ROUND_SIZE( offset
.u
.LowPart
, *size_ptr
);
2606 if (size
< *size_ptr
) goto done
; /* wrap-around */
2610 size
= full_size
- offset
.QuadPart
;
2611 if (size
!= full_size
- offset
.QuadPart
) /* truncated */
2613 WARN( "Files larger than 4Gb (%s) not supported on this platform\n",
2614 wine_dbgstr_longlong(full_size
) );
2619 /* Reserve a properly aligned area */
2621 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2623 get_vprot_flags( protect
, &vprot
, map_vprot
& VPROT_IMAGE
);
2624 vprot
|= (map_vprot
& VPROT_COMMITTED
);
2625 res
= map_view( &view
, *addr_ptr
, size
, mask
, FALSE
, vprot
);
2628 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2634 TRACE("handle=%p size=%lx offset=%x%08x\n",
2635 handle
, size
, offset
.u
.HighPart
, offset
.u
.LowPart
);
2637 res
= map_file_into_view( view
, unix_handle
, 0, size
, offset
.QuadPart
, vprot
, !dup_mapping
);
2638 if (res
== STATUS_SUCCESS
)
2640 *addr_ptr
= view
->base
;
2642 view
->mapping
= dup_mapping
;
2643 view
->map_protect
= map_vprot
;
2644 dup_mapping
= 0; /* don't close it */
2648 ERR( "map_file_into_view %p %lx %x%08x failed\n",
2649 view
->base
, size
, offset
.u
.HighPart
, offset
.u
.LowPart
);
2650 delete_view( view
);
2653 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2656 if (dup_mapping
) NtClose( dup_mapping
);
2657 if (needs_close
) close( unix_handle
);
2662 /***********************************************************************
2663 * NtUnmapViewOfSection (NTDLL.@)
2664 * ZwUnmapViewOfSection (NTDLL.@)
2666 NTSTATUS WINAPI
NtUnmapViewOfSection( HANDLE process
, PVOID addr
)
2668 struct file_view
*view
;
2669 NTSTATUS status
= STATUS_NOT_MAPPED_VIEW
;
2671 void *base
= ROUND_ADDR( addr
, page_mask
);
2673 if (process
!= NtCurrentProcess())
2676 apc_result_t result
;
2678 memset( &call
, 0, sizeof(call
) );
2680 call
.unmap_view
.type
= APC_UNMAP_VIEW
;
2681 call
.unmap_view
.addr
= wine_server_client_ptr( addr
);
2682 status
= server_queue_process_apc( process
, &call
, &result
);
2683 if (status
== STATUS_SUCCESS
) status
= result
.unmap_view
.status
;
2687 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2688 if ((view
= VIRTUAL_FindView( base
, 0 )) && (base
== view
->base
) && !(view
->protect
& VPROT_VALLOC
))
2690 delete_view( view
);
2691 status
= STATUS_SUCCESS
;
2693 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2698 /***********************************************************************
2699 * NtFlushVirtualMemory (NTDLL.@)
2700 * ZwFlushVirtualMemory (NTDLL.@)
2702 NTSTATUS WINAPI
NtFlushVirtualMemory( HANDLE process
, LPCVOID
*addr_ptr
,
2703 SIZE_T
*size_ptr
, ULONG unknown
)
2705 struct file_view
*view
;
2706 NTSTATUS status
= STATUS_SUCCESS
;
2708 void *addr
= ROUND_ADDR( *addr_ptr
, page_mask
);
2710 if (process
!= NtCurrentProcess())
2713 apc_result_t result
;
2715 memset( &call
, 0, sizeof(call
) );
2717 call
.virtual_flush
.type
= APC_VIRTUAL_FLUSH
;
2718 call
.virtual_flush
.addr
= wine_server_client_ptr( addr
);
2719 call
.virtual_flush
.size
= *size_ptr
;
2720 status
= server_queue_process_apc( process
, &call
, &result
);
2721 if (status
!= STATUS_SUCCESS
) return status
;
2723 if (result
.virtual_flush
.status
== STATUS_SUCCESS
)
2725 *addr_ptr
= wine_server_get_ptr( result
.virtual_flush
.addr
);
2726 *size_ptr
= result
.virtual_flush
.size
;
2728 return result
.virtual_flush
.status
;
2731 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2732 if (!(view
= VIRTUAL_FindView( addr
, *size_ptr
))) status
= STATUS_INVALID_PARAMETER
;
2735 if (!*size_ptr
) *size_ptr
= view
->size
;
2737 if (msync( addr
, *size_ptr
, MS_SYNC
)) status
= STATUS_NOT_MAPPED_DATA
;
2739 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2744 /***********************************************************************
2745 * NtGetWriteWatch (NTDLL.@)
2746 * ZwGetWriteWatch (NTDLL.@)
2748 NTSTATUS WINAPI
NtGetWriteWatch( HANDLE process
, ULONG flags
, PVOID base
, SIZE_T size
, PVOID
*addresses
,
2749 ULONG_PTR
*count
, ULONG
*granularity
)
2751 struct file_view
*view
;
2752 NTSTATUS status
= STATUS_SUCCESS
;
2755 size
= ROUND_SIZE( base
, size
);
2756 base
= ROUND_ADDR( base
, page_mask
);
2758 if (!count
|| !granularity
) return STATUS_ACCESS_VIOLATION
;
2759 if (!*count
|| !size
) return STATUS_INVALID_PARAMETER
;
2760 if (flags
& ~WRITE_WATCH_FLAG_RESET
) return STATUS_INVALID_PARAMETER
;
2762 if (!addresses
) return STATUS_ACCESS_VIOLATION
;
2764 TRACE( "%p %x %p-%p %p %lu\n", process
, flags
, base
, (char *)base
+ size
,
2765 addresses
, *count
);
2767 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2769 if ((view
= VIRTUAL_FindView( base
, size
)) && (view
->protect
& VPROT_WRITEWATCH
))
2773 char *end
= addr
+ size
;
2775 while (pos
< *count
&& addr
< end
)
2777 BYTE prot
= view
->prot
[(addr
- (char *)view
->base
) >> page_shift
];
2778 if (!(prot
& VPROT_WRITEWATCH
)) addresses
[pos
++] = addr
;
2781 if (flags
& WRITE_WATCH_FLAG_RESET
) reset_write_watches( view
, base
, addr
- (char *)base
);
2783 *granularity
= page_size
;
2785 else status
= STATUS_INVALID_PARAMETER
;
2787 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2792 /***********************************************************************
2793 * NtResetWriteWatch (NTDLL.@)
2794 * ZwResetWriteWatch (NTDLL.@)
2796 NTSTATUS WINAPI
NtResetWriteWatch( HANDLE process
, PVOID base
, SIZE_T size
)
2798 struct file_view
*view
;
2799 NTSTATUS status
= STATUS_SUCCESS
;
2802 size
= ROUND_SIZE( base
, size
);
2803 base
= ROUND_ADDR( base
, page_mask
);
2805 TRACE( "%p %p-%p\n", process
, base
, (char *)base
+ size
);
2807 if (!size
) return STATUS_INVALID_PARAMETER
;
2809 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2811 if ((view
= VIRTUAL_FindView( base
, size
)) && (view
->protect
& VPROT_WRITEWATCH
))
2812 reset_write_watches( view
, base
, size
);
2814 status
= STATUS_INVALID_PARAMETER
;
2816 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2821 /***********************************************************************
2822 * NtReadVirtualMemory (NTDLL.@)
2823 * ZwReadVirtualMemory (NTDLL.@)
2825 NTSTATUS WINAPI
NtReadVirtualMemory( HANDLE process
, const void *addr
, void *buffer
,
2826 SIZE_T size
, SIZE_T
*bytes_read
)
2830 if (virtual_check_buffer_for_write( buffer
, size
))
2832 SERVER_START_REQ( read_process_memory
)
2834 req
->handle
= wine_server_obj_handle( process
);
2835 req
->addr
= wine_server_client_ptr( addr
);
2836 wine_server_set_reply( req
, buffer
, size
);
2837 if ((status
= wine_server_call( req
))) size
= 0;
2843 status
= STATUS_ACCESS_VIOLATION
;
2846 if (bytes_read
) *bytes_read
= size
;
2851 /***********************************************************************
2852 * NtWriteVirtualMemory (NTDLL.@)
2853 * ZwWriteVirtualMemory (NTDLL.@)
2855 NTSTATUS WINAPI
NtWriteVirtualMemory( HANDLE process
, void *addr
, const void *buffer
,
2856 SIZE_T size
, SIZE_T
*bytes_written
)
2860 if (virtual_check_buffer_for_read( buffer
, size
))
2862 SERVER_START_REQ( write_process_memory
)
2864 req
->handle
= wine_server_obj_handle( process
);
2865 req
->addr
= wine_server_client_ptr( addr
);
2866 wine_server_add_data( req
, buffer
, size
);
2867 if ((status
= wine_server_call( req
))) size
= 0;
2873 status
= STATUS_PARTIAL_COPY
;
2876 if (bytes_written
) *bytes_written
= size
;
2881 /***********************************************************************
2882 * NtAreMappedFilesTheSame (NTDLL.@)
2883 * ZwAreMappedFilesTheSame (NTDLL.@)
2885 NTSTATUS WINAPI
NtAreMappedFilesTheSame(PVOID addr1
, PVOID addr2
)
2887 struct file_view
*view1
, *view2
;
2888 struct stat st1
, st2
;
2892 TRACE("%p %p\n", addr1
, addr2
);
2894 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2896 view1
= VIRTUAL_FindView( addr1
, 0 );
2897 view2
= VIRTUAL_FindView( addr2
, 0 );
2899 if (!view1
|| !view2
)
2900 status
= STATUS_INVALID_ADDRESS
;
2901 else if ((view1
->protect
& VPROT_VALLOC
) || (view2
->protect
& VPROT_VALLOC
))
2902 status
= STATUS_CONFLICTING_ADDRESSES
;
2903 else if (view1
== view2
)
2904 status
= STATUS_SUCCESS
;
2905 else if (!(view1
->protect
& VPROT_IMAGE
) || !(view2
->protect
& VPROT_IMAGE
))
2906 status
= STATUS_NOT_SAME_DEVICE
;
2907 else if (!stat_mapping_file( view1
, &st1
) && !stat_mapping_file( view2
, &st2
) &&
2908 st1
.st_dev
== st2
.st_dev
&& st1
.st_ino
== st2
.st_ino
)
2909 status
= STATUS_SUCCESS
;
2911 status
= STATUS_NOT_SAME_DEVICE
;
2913 server_leave_uninterrupted_section( &csVirtual
, &sigset
);