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"
26 #ifdef HAVE_SYS_ERRNO_H
27 #include <sys/errno.h>
37 #include <sys/types.h>
38 #ifdef HAVE_SYS_STAT_H
39 # include <sys/stat.h>
41 #ifdef HAVE_SYS_MMAN_H
42 # include <sys/mman.h>
44 #ifdef HAVE_VALGRIND_VALGRIND_H
45 # include <valgrind/valgrind.h>
48 #define NONAMELESSUNION
49 #define NONAMELESSSTRUCT
51 #define WIN32_NO_STATUS
54 #include "wine/library.h"
55 #include "wine/server.h"
56 #include "wine/exception.h"
57 #include "wine/list.h"
58 #include "wine/debug.h"
59 #include "ntdll_misc.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(virtual);
62 WINE_DECLARE_DEBUG_CHANNEL(module
);
69 #define MAP_NORESERVE 0
75 struct list entry
; /* Entry in global view list */
76 void *base
; /* Base address */
77 size_t size
; /* Size in bytes */
78 HANDLE mapping
; /* Handle to the file mapping */
79 unsigned int protect
; /* Protection for all pages at allocation time */
80 BYTE prot
[1]; /* Protection byte for each page */
84 /* Conversion from VPROT_* to Win32 flags */
85 static const BYTE VIRTUAL_Win32Flags
[16] =
87 PAGE_NOACCESS
, /* 0 */
88 PAGE_READONLY
, /* READ */
89 PAGE_READWRITE
, /* WRITE */
90 PAGE_READWRITE
, /* READ | WRITE */
91 PAGE_EXECUTE
, /* EXEC */
92 PAGE_EXECUTE_READ
, /* READ | EXEC */
93 PAGE_EXECUTE_READWRITE
, /* WRITE | EXEC */
94 PAGE_EXECUTE_READWRITE
, /* READ | WRITE | EXEC */
95 PAGE_WRITECOPY
, /* WRITECOPY */
96 PAGE_WRITECOPY
, /* READ | WRITECOPY */
97 PAGE_WRITECOPY
, /* WRITE | WRITECOPY */
98 PAGE_WRITECOPY
, /* READ | WRITE | WRITECOPY */
99 PAGE_EXECUTE_WRITECOPY
, /* EXEC | WRITECOPY */
100 PAGE_EXECUTE_WRITECOPY
, /* READ | EXEC | WRITECOPY */
101 PAGE_EXECUTE_WRITECOPY
, /* WRITE | EXEC | WRITECOPY */
102 PAGE_EXECUTE_WRITECOPY
/* READ | WRITE | EXEC | WRITECOPY */
105 static struct list views_list
= LIST_INIT(views_list
);
107 static RTL_CRITICAL_SECTION csVirtual
;
108 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
111 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
112 0, 0, { (DWORD_PTR
)(__FILE__
": csVirtual") }
114 static RTL_CRITICAL_SECTION csVirtual
= { &critsect_debug
, -1, 0, 0, 0, 0 };
117 /* These are always the same on an i386, and it will be faster this way */
118 # define page_mask 0xfff
119 # define page_shift 12
120 # define page_size 0x1000
121 /* Note: these are Windows limits, you cannot change them. */
122 static void *address_space_limit
= (void *)0xc0000000; /* top of the total available address space */
123 static void *user_space_limit
= (void *)0x7fff0000; /* top of the user address space */
124 static void *working_set_limit
= (void *)0x7fff0000; /* top of the current working set */
125 #elif defined(__x86_64__)
126 # define page_mask 0xfff
127 # define page_shift 12
128 # define page_size 0x1000
129 static void *address_space_limit
= (void *)0x7fffffff0000;
130 static void *user_space_limit
= (void *)0x7fffffff0000;
131 static void *working_set_limit
= (void *)0x7fffffff0000;
133 static UINT page_shift
;
134 static UINT_PTR page_size
;
135 static UINT_PTR page_mask
;
136 static void *address_space_limit
;
137 static void *user_space_limit
;
138 static void *working_set_limit
;
139 #endif /* __i386__ */
140 static void * const address_space_start
= (void *)0x110000;
142 #define ROUND_ADDR(addr,mask) \
143 ((void *)((UINT_PTR)(addr) & ~(UINT_PTR)(mask)))
145 #define ROUND_SIZE(addr,size) \
146 (((SIZE_T)(size) + ((UINT_PTR)(addr) & page_mask) + page_mask) & ~page_mask)
148 #define VIRTUAL_DEBUG_DUMP_VIEW(view) \
149 do { if (TRACE_ON(virtual)) VIRTUAL_DumpView(view); } while (0)
151 #define VIRTUAL_HEAP_SIZE (4*1024*1024)
153 static HANDLE virtual_heap
;
154 static void *preload_reserve_start
;
155 static void *preload_reserve_end
;
156 static int use_locks
;
157 static int force_exec_prot
; /* whether to force PROT_EXEC on all PROT_READ mmaps */
160 /***********************************************************************
163 static const char *VIRTUAL_GetProtStr( BYTE prot
)
165 static char buffer
[6];
166 buffer
[0] = (prot
& VPROT_COMMITTED
) ? 'c' : '-';
167 buffer
[1] = (prot
& VPROT_GUARD
) ? 'g' : ((prot
& VPROT_WRITEWATCH
) ? 'H' : '-');
168 buffer
[2] = (prot
& VPROT_READ
) ? 'r' : '-';
169 buffer
[3] = (prot
& VPROT_WRITECOPY
) ? 'W' : ((prot
& VPROT_WRITE
) ? 'w' : '-');
170 buffer
[4] = (prot
& VPROT_EXEC
) ? 'x' : '-';
176 /***********************************************************************
177 * VIRTUAL_GetUnixProt
179 * Convert page protections to protection for mmap/mprotect.
181 static int VIRTUAL_GetUnixProt( BYTE vprot
)
184 if ((vprot
& VPROT_COMMITTED
) && !(vprot
& VPROT_GUARD
))
186 if (vprot
& VPROT_READ
) prot
|= PROT_READ
;
187 if (vprot
& VPROT_WRITE
) prot
|= PROT_WRITE
;
188 if (vprot
& VPROT_WRITECOPY
) prot
|= PROT_WRITE
;
189 if (vprot
& VPROT_EXEC
) prot
|= PROT_EXEC
;
190 if (vprot
& VPROT_WRITEWATCH
) prot
&= ~PROT_WRITE
;
192 if (!prot
) prot
= PROT_NONE
;
197 /***********************************************************************
200 static void VIRTUAL_DumpView( struct file_view
*view
)
203 char *addr
= view
->base
;
204 BYTE prot
= view
->prot
[0];
206 TRACE( "View: %p - %p", addr
, addr
+ view
->size
- 1 );
207 if (view
->protect
& VPROT_SYSTEM
)
208 TRACE( " (system)\n" );
209 else if (view
->protect
& VPROT_VALLOC
)
210 TRACE( " (valloc)\n" );
211 else if (view
->mapping
)
212 TRACE( " %p\n", view
->mapping
);
214 TRACE( " (anonymous)\n");
216 for (count
= i
= 1; i
< view
->size
>> page_shift
; i
++, count
++)
218 if (view
->prot
[i
] == prot
) continue;
219 TRACE( " %p - %p %s\n",
220 addr
, addr
+ (count
<< page_shift
) - 1, VIRTUAL_GetProtStr(prot
) );
221 addr
+= (count
<< page_shift
);
222 prot
= view
->prot
[i
];
226 TRACE( " %p - %p %s\n",
227 addr
, addr
+ (count
<< page_shift
) - 1, VIRTUAL_GetProtStr(prot
) );
231 /***********************************************************************
235 static void VIRTUAL_Dump(void)
238 struct file_view
*view
;
240 TRACE( "Dump of all virtual memory views:\n" );
241 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
242 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
244 VIRTUAL_DumpView( view
);
246 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
251 /***********************************************************************
254 * Find the view containing a given address. The csVirtual section must be held by caller.
263 static struct file_view
*VIRTUAL_FindView( const void *addr
, size_t size
)
265 struct file_view
*view
;
267 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
269 if (view
->base
> addr
) break; /* no matching view */
270 if ((const char *)view
->base
+ view
->size
<= (const char *)addr
) continue;
271 if ((const char *)view
->base
+ view
->size
< (const char *)addr
+ size
) break; /* size too large */
272 if ((const char *)addr
+ size
< (const char *)addr
) break; /* overflow */
279 /***********************************************************************
282 static inline UINT_PTR
get_mask( ULONG zero_bits
)
284 if (!zero_bits
) return 0xffff; /* allocations are aligned to 64K by default */
285 if (zero_bits
< page_shift
) zero_bits
= page_shift
;
286 return (1 << zero_bits
) - 1;
290 /***********************************************************************
293 * Find the first view overlapping at least part of the specified range.
294 * The csVirtual section must be held by caller.
296 static struct file_view
*find_view_range( const void *addr
, size_t size
)
298 struct file_view
*view
;
300 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
302 if ((const char *)view
->base
>= (const char *)addr
+ size
) break;
303 if ((const char *)view
->base
+ view
->size
> (const char *)addr
) return view
;
309 /***********************************************************************
312 * Find a free area between views inside the specified range.
313 * The csVirtual section must be held by caller.
315 static void *find_free_area( void *base
, void *end
, size_t size
, size_t mask
, int top_down
)
322 start
= ROUND_ADDR( (char *)end
- size
, mask
);
323 if (start
>= end
|| start
< base
) return NULL
;
325 for (ptr
= views_list
.prev
; ptr
!= &views_list
; ptr
= ptr
->prev
)
327 struct file_view
*view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
329 if ((char *)view
->base
+ view
->size
<= (char *)start
) break;
330 if ((char *)view
->base
>= (char *)start
+ size
) continue;
331 start
= ROUND_ADDR( (char *)view
->base
- size
, mask
);
332 /* stop if remaining space is not large enough */
333 if (!start
|| start
>= end
|| start
< base
) return NULL
;
338 start
= ROUND_ADDR( (char *)base
+ mask
, mask
);
339 if (start
>= end
|| (char *)end
- (char *)start
< size
) return NULL
;
341 for (ptr
= views_list
.next
; ptr
!= &views_list
; ptr
= ptr
->next
)
343 struct file_view
*view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
345 if ((char *)view
->base
>= (char *)start
+ size
) break;
346 if ((char *)view
->base
+ view
->size
<= (char *)start
) continue;
347 start
= ROUND_ADDR( (char *)view
->base
+ view
->size
+ mask
, mask
);
348 /* stop if remaining space is not large enough */
349 if (!start
|| start
>= end
|| (char *)end
- (char *)start
< size
) return NULL
;
356 /***********************************************************************
359 * Add a reserved area to the list maintained by libwine.
360 * The csVirtual section must be held by caller.
362 static void add_reserved_area( void *addr
, size_t size
)
364 TRACE( "adding %p-%p\n", addr
, (char *)addr
+ size
);
366 if (addr
< user_space_limit
)
368 /* unmap the part of the area that is below the limit */
369 assert( (char *)addr
+ size
> (char *)user_space_limit
);
370 munmap( addr
, (char *)user_space_limit
- (char *)addr
);
371 size
-= (char *)user_space_limit
- (char *)addr
;
372 addr
= user_space_limit
;
374 /* blow away existing mappings */
375 wine_anon_mmap( addr
, size
, PROT_NONE
, MAP_NORESERVE
| MAP_FIXED
);
376 wine_mmap_add_reserved_area( addr
, size
);
380 /***********************************************************************
381 * remove_reserved_area
383 * Remove a reserved area from the list maintained by libwine.
384 * The csVirtual section must be held by caller.
386 static void remove_reserved_area( void *addr
, size_t size
)
388 struct file_view
*view
;
390 TRACE( "removing %p-%p\n", addr
, (char *)addr
+ size
);
391 wine_mmap_remove_reserved_area( addr
, size
, 0 );
393 /* unmap areas not covered by an existing view */
394 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
396 if ((char *)view
->base
>= (char *)addr
+ size
)
398 munmap( addr
, size
);
401 if ((char *)view
->base
+ view
->size
<= (char *)addr
) continue;
402 if (view
->base
> addr
) munmap( addr
, (char *)view
->base
- (char *)addr
);
403 if ((char *)view
->base
+ view
->size
> (char *)addr
+ size
) break;
404 size
= (char *)addr
+ size
- ((char *)view
->base
+ view
->size
);
405 addr
= (char *)view
->base
+ view
->size
;
410 /***********************************************************************
413 * Check if an address range goes beyond a given limit.
415 static inline int is_beyond_limit( const void *addr
, size_t size
, const void *limit
)
417 return (addr
>= limit
|| (const char *)addr
+ size
> (const char *)limit
);
421 /***********************************************************************
424 * Unmap an area, or simply replace it by an empty mapping if it is
425 * in a reserved area. The csVirtual section must be held by caller.
427 static inline void unmap_area( void *addr
, size_t size
)
429 if (wine_mmap_is_in_reserved_area( addr
, size
))
430 wine_anon_mmap( addr
, size
, PROT_NONE
, MAP_NORESERVE
| MAP_FIXED
);
431 else if (is_beyond_limit( addr
, size
, user_space_limit
))
432 add_reserved_area( addr
, size
);
434 munmap( addr
, size
);
438 /***********************************************************************
441 * Deletes a view. The csVirtual section must be held by caller.
443 static void delete_view( struct file_view
*view
) /* [in] View */
445 if (!(view
->protect
& VPROT_SYSTEM
)) unmap_area( view
->base
, view
->size
);
446 list_remove( &view
->entry
);
447 if (view
->mapping
) NtClose( view
->mapping
);
448 RtlFreeHeap( virtual_heap
, 0, view
);
452 /***********************************************************************
455 * Create a view. The csVirtual section must be held by caller.
457 static NTSTATUS
create_view( struct file_view
**view_ret
, void *base
, size_t size
, unsigned int vprot
)
459 struct file_view
*view
;
461 int unix_prot
= VIRTUAL_GetUnixProt( vprot
);
463 assert( !((UINT_PTR
)base
& page_mask
) );
464 assert( !(size
& page_mask
) );
466 /* Create the view structure */
468 if (!(view
= RtlAllocateHeap( virtual_heap
, 0, sizeof(*view
) + (size
>> page_shift
) - 1 )))
470 FIXME( "out of memory in virtual heap for %p-%p\n", base
, (char *)base
+ size
);
471 return STATUS_NO_MEMORY
;
477 view
->protect
= vprot
;
478 memset( view
->prot
, vprot
, size
>> page_shift
);
480 /* Insert it in the linked list */
482 LIST_FOR_EACH( ptr
, &views_list
)
484 struct file_view
*next
= LIST_ENTRY( ptr
, struct file_view
, entry
);
485 if (next
->base
> base
) break;
487 list_add_before( ptr
, &view
->entry
);
489 /* Check for overlapping views. This can happen if the previous view
490 * was a system view that got unmapped behind our back. In that case
491 * we recover by simply deleting it. */
493 if ((ptr
= list_prev( &views_list
, &view
->entry
)) != NULL
)
495 struct file_view
*prev
= LIST_ENTRY( ptr
, struct file_view
, entry
);
496 if ((char *)prev
->base
+ prev
->size
> (char *)base
)
498 TRACE( "overlapping prev view %p-%p for %p-%p\n",
499 prev
->base
, (char *)prev
->base
+ prev
->size
,
500 base
, (char *)base
+ view
->size
);
501 assert( prev
->protect
& VPROT_SYSTEM
);
505 if ((ptr
= list_next( &views_list
, &view
->entry
)) != NULL
)
507 struct file_view
*next
= LIST_ENTRY( ptr
, struct file_view
, entry
);
508 if ((char *)base
+ view
->size
> (char *)next
->base
)
510 TRACE( "overlapping next view %p-%p for %p-%p\n",
511 next
->base
, (char *)next
->base
+ next
->size
,
512 base
, (char *)base
+ view
->size
);
513 assert( next
->protect
& VPROT_SYSTEM
);
519 VIRTUAL_DEBUG_DUMP_VIEW( view
);
521 if (force_exec_prot
&& !(vprot
& VPROT_NOEXEC
) && (unix_prot
& PROT_READ
) && !(unix_prot
& PROT_EXEC
))
523 TRACE( "forcing exec permission on %p-%p\n", base
, (char *)base
+ size
- 1 );
524 mprotect( base
, size
, unix_prot
| PROT_EXEC
);
526 return STATUS_SUCCESS
;
530 /***********************************************************************
531 * VIRTUAL_GetWin32Prot
533 * Convert page protections to Win32 flags.
535 static DWORD
VIRTUAL_GetWin32Prot( BYTE vprot
)
537 DWORD ret
= VIRTUAL_Win32Flags
[vprot
& 0x0f];
538 if (vprot
& VPROT_NOCACHE
) ret
|= PAGE_NOCACHE
;
539 if (vprot
& VPROT_GUARD
) ret
|= PAGE_GUARD
;
544 /***********************************************************************
547 * Build page protections from Win32 flags.
550 * protect [I] Win32 protection flags
553 * Value of page protection flags
555 static NTSTATUS
get_vprot_flags( DWORD protect
, unsigned int *vprot
)
557 switch(protect
& 0xff)
563 *vprot
= VPROT_READ
| VPROT_WRITE
;
566 *vprot
= VPROT_READ
| VPROT_WRITECOPY
;
571 case PAGE_EXECUTE_READ
:
572 *vprot
= VPROT_EXEC
| VPROT_READ
;
574 case PAGE_EXECUTE_READWRITE
:
575 *vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITE
;
577 case PAGE_EXECUTE_WRITECOPY
:
578 *vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITECOPY
;
584 return STATUS_INVALID_PARAMETER
;
586 if (protect
& PAGE_GUARD
) *vprot
|= VPROT_GUARD
;
587 if (protect
& PAGE_NOCACHE
) *vprot
|= VPROT_NOCACHE
;
588 return STATUS_SUCCESS
;
592 /***********************************************************************
595 * Change the protection of a range of pages.
601 static BOOL
VIRTUAL_SetProt( struct file_view
*view
, /* [in] Pointer to view */
602 void *base
, /* [in] Starting address */
603 size_t size
, /* [in] Size in bytes */
604 BYTE vprot
) /* [in] Protections to use */
606 int unix_prot
= VIRTUAL_GetUnixProt(vprot
);
607 BYTE
*p
= view
->prot
+ (((char *)base
- (char *)view
->base
) >> page_shift
);
610 base
, (char *)base
+ size
- 1, VIRTUAL_GetProtStr( vprot
) );
612 if (view
->protect
& VPROT_WRITEWATCH
)
614 /* each page may need different protections depending on write watch flag */
619 p
[0] = vprot
| (p
[0] & VPROT_WRITEWATCH
);
620 unix_prot
= VIRTUAL_GetUnixProt( p
[0] );
621 for (count
= i
= 1; i
< size
>> page_shift
; i
++, count
++)
623 p
[i
] = vprot
| (p
[i
] & VPROT_WRITEWATCH
);
624 prot
= VIRTUAL_GetUnixProt( p
[i
] );
625 if (prot
== unix_prot
) continue;
626 mprotect( addr
, count
<< page_shift
, unix_prot
);
627 addr
+= count
<< page_shift
;
631 if (count
) mprotect( addr
, count
<< page_shift
, unix_prot
);
632 VIRTUAL_DEBUG_DUMP_VIEW( view
);
636 /* if setting stack guard pages, store the permissions first, as the guard may be
637 * triggered at any point after mprotect and change the permissions again */
638 if ((vprot
& VPROT_GUARD
) &&
639 (base
>= NtCurrentTeb()->DeallocationStack
) &&
640 (base
< NtCurrentTeb()->Tib
.StackBase
))
642 memset( p
, vprot
, size
>> page_shift
);
643 mprotect( base
, size
, unix_prot
);
644 VIRTUAL_DEBUG_DUMP_VIEW( view
);
648 if (force_exec_prot
&& !(view
->protect
& VPROT_NOEXEC
) &&
649 (unix_prot
& PROT_READ
) && !(unix_prot
& PROT_EXEC
))
651 TRACE( "forcing exec permission on %p-%p\n", base
, (char *)base
+ size
- 1 );
652 if (!mprotect( base
, size
, unix_prot
| PROT_EXEC
)) goto done
;
653 /* exec + write may legitimately fail, in that case fall back to write only */
654 if (!(unix_prot
& PROT_WRITE
)) return FALSE
;
657 if (mprotect( base
, size
, unix_prot
)) return FALSE
; /* FIXME: last error */
660 memset( p
, vprot
, size
>> page_shift
);
661 VIRTUAL_DEBUG_DUMP_VIEW( view
);
666 /***********************************************************************
667 * reset_write_watches
669 * Reset write watches in a memory range.
671 static void reset_write_watches( struct file_view
*view
, void *base
, SIZE_T size
)
676 BYTE
*p
= view
->prot
+ ((addr
- (char *)view
->base
) >> page_shift
);
678 p
[0] |= VPROT_WRITEWATCH
;
679 unix_prot
= VIRTUAL_GetUnixProt( p
[0] );
680 for (count
= i
= 1; i
< size
>> page_shift
; i
++, count
++)
682 p
[i
] |= VPROT_WRITEWATCH
;
683 prot
= VIRTUAL_GetUnixProt( p
[i
] );
684 if (prot
== unix_prot
) continue;
685 mprotect( addr
, count
<< page_shift
, unix_prot
);
686 addr
+= count
<< page_shift
;
690 if (count
) mprotect( addr
, count
<< page_shift
, unix_prot
);
694 /***********************************************************************
697 * Release the extra memory while keeping the range starting on the granularity boundary.
699 static inline void *unmap_extra_space( void *ptr
, size_t total_size
, size_t wanted_size
, size_t mask
)
701 if ((ULONG_PTR
)ptr
& mask
)
703 size_t extra
= mask
+ 1 - ((ULONG_PTR
)ptr
& mask
);
704 munmap( ptr
, extra
);
705 ptr
= (char *)ptr
+ extra
;
708 if (total_size
> wanted_size
)
709 munmap( (char *)ptr
+ wanted_size
, total_size
- wanted_size
);
723 /***********************************************************************
724 * alloc_reserved_area_callback
726 * Try to map some space inside a reserved area. Callback for wine_mmap_enum_reserved_areas.
728 static int alloc_reserved_area_callback( void *start
, size_t size
, void *arg
)
730 struct alloc_area
*alloc
= arg
;
731 void *end
= (char *)start
+ size
;
733 if (start
< address_space_start
) start
= address_space_start
;
734 if (is_beyond_limit( start
, size
, alloc
->limit
)) end
= alloc
->limit
;
735 if (start
>= end
) return 0;
737 /* make sure we don't touch the preloader reserved range */
738 if (preload_reserve_end
>= start
)
740 if (preload_reserve_end
>= end
)
742 if (preload_reserve_start
<= start
) return 0; /* no space in that area */
743 if (preload_reserve_start
< end
) end
= preload_reserve_start
;
745 else if (preload_reserve_start
<= start
) start
= preload_reserve_end
;
748 /* range is split in two by the preloader reservation, try first part */
749 if ((alloc
->result
= find_free_area( start
, preload_reserve_start
, alloc
->size
,
750 alloc
->mask
, alloc
->top_down
)))
752 /* then fall through to try second part */
753 start
= preload_reserve_end
;
756 if ((alloc
->result
= find_free_area( start
, end
, alloc
->size
, alloc
->mask
, alloc
->top_down
)))
763 /***********************************************************************
766 * Create a view and mmap the corresponding memory area.
767 * The csVirtual section must be held by caller.
769 static NTSTATUS
map_view( struct file_view
**view_ret
, void *base
, size_t size
, size_t mask
,
770 int top_down
, unsigned int vprot
)
777 if (is_beyond_limit( base
, size
, address_space_limit
))
778 return STATUS_WORKING_SET_LIMIT_RANGE
;
780 switch (wine_mmap_is_in_reserved_area( base
, size
))
782 case -1: /* partially in a reserved area */
783 return STATUS_CONFLICTING_ADDRESSES
;
785 case 0: /* not in a reserved area, do a normal allocation */
786 if ((ptr
= wine_anon_mmap( base
, size
, VIRTUAL_GetUnixProt(vprot
), 0 )) == (void *)-1)
788 if (errno
== ENOMEM
) return STATUS_NO_MEMORY
;
789 return STATUS_INVALID_PARAMETER
;
793 /* We couldn't get the address we wanted */
794 if (is_beyond_limit( ptr
, size
, user_space_limit
)) add_reserved_area( ptr
, size
);
795 else munmap( ptr
, size
);
796 return STATUS_CONFLICTING_ADDRESSES
;
801 case 1: /* in a reserved area, make sure the address is available */
802 if (find_view_range( base
, size
)) return STATUS_CONFLICTING_ADDRESSES
;
803 /* replace the reserved area by our mapping */
804 if ((ptr
= wine_anon_mmap( base
, size
, VIRTUAL_GetUnixProt(vprot
), MAP_FIXED
)) != base
)
805 return STATUS_INVALID_PARAMETER
;
808 if (is_beyond_limit( ptr
, size
, working_set_limit
)) working_set_limit
= address_space_limit
;
812 size_t view_size
= size
+ mask
+ 1;
813 struct alloc_area alloc
;
817 alloc
.top_down
= top_down
;
818 alloc
.limit
= user_space_limit
;
819 if (wine_mmap_enum_reserved_areas( alloc_reserved_area_callback
, &alloc
, top_down
))
822 TRACE( "got mem in reserved area %p-%p\n", ptr
, (char *)ptr
+ size
);
823 if (wine_anon_mmap( ptr
, size
, VIRTUAL_GetUnixProt(vprot
), MAP_FIXED
) != ptr
)
824 return STATUS_INVALID_PARAMETER
;
830 if ((ptr
= wine_anon_mmap( NULL
, view_size
, VIRTUAL_GetUnixProt(vprot
), 0 )) == (void *)-1)
832 if (errno
== ENOMEM
) return STATUS_NO_MEMORY
;
833 return STATUS_INVALID_PARAMETER
;
835 TRACE( "got mem with anon mmap %p-%p\n", ptr
, (char *)ptr
+ size
);
836 /* if we got something beyond the user limit, unmap it and retry */
837 if (is_beyond_limit( ptr
, view_size
, user_space_limit
)) add_reserved_area( ptr
, view_size
);
840 ptr
= unmap_extra_space( ptr
, view_size
, size
, mask
);
843 status
= create_view( view_ret
, ptr
, size
, vprot
);
844 if (status
!= STATUS_SUCCESS
) unmap_area( ptr
, size
);
849 /***********************************************************************
852 * Wrapper for mmap() to map a file into a view, falling back to read if mmap fails.
853 * The csVirtual section must be held by caller.
855 static NTSTATUS
map_file_into_view( struct file_view
*view
, int fd
, size_t start
, size_t size
,
856 off_t offset
, unsigned int vprot
, BOOL removable
)
859 int prot
= VIRTUAL_GetUnixProt( vprot
| VPROT_COMMITTED
/* make sure it is accessible */ );
860 BOOL shared_write
= (vprot
& VPROT_WRITE
) != 0;
862 assert( start
< view
->size
);
863 assert( start
+ size
<= view
->size
);
865 /* only try mmap if media is not removable (or if we require write access) */
866 if (!removable
|| shared_write
)
868 int flags
= MAP_FIXED
| (shared_write
? MAP_SHARED
: MAP_PRIVATE
);
870 if (mmap( (char *)view
->base
+ start
, size
, prot
, flags
, fd
, offset
) != (void *)-1)
873 /* mmap() failed; if this is because the file offset is not */
874 /* page-aligned (EINVAL), or because the underlying filesystem */
875 /* does not support mmap() (ENOEXEC,ENODEV), we do it by hand. */
876 if ((errno
!= ENOEXEC
) && (errno
!= EINVAL
) && (errno
!= ENODEV
)) return FILE_GetNtStatus();
877 if (shared_write
) /* we cannot fake shared write mappings */
879 if (errno
== EINVAL
) return STATUS_INVALID_PARAMETER
;
880 ERR( "shared writable mmap not supported, broken filesystem?\n" );
881 return STATUS_NOT_SUPPORTED
;
885 /* Reserve the memory with an anonymous mmap */
886 ptr
= wine_anon_mmap( (char *)view
->base
+ start
, size
, PROT_READ
| PROT_WRITE
, MAP_FIXED
);
887 if (ptr
== (void *)-1) return FILE_GetNtStatus();
888 /* Now read in the file */
889 pread( fd
, ptr
, size
, offset
);
890 if (prot
!= (PROT_READ
|PROT_WRITE
)) mprotect( ptr
, size
, prot
); /* Set the right protection */
892 memset( view
->prot
+ (start
>> page_shift
), vprot
, ROUND_SIZE(start
,size
) >> page_shift
);
893 return STATUS_SUCCESS
;
897 /***********************************************************************
900 * Get the size of the committed range starting at base.
901 * Also return the protections for the first page.
903 static SIZE_T
get_committed_size( struct file_view
*view
, void *base
, BYTE
*vprot
)
907 start
= ((char *)base
- (char *)view
->base
) >> page_shift
;
908 *vprot
= view
->prot
[start
];
910 if (view
->mapping
&& !(view
->protect
& VPROT_COMMITTED
))
913 SERVER_START_REQ( get_mapping_committed_range
)
915 req
->handle
= wine_server_obj_handle( view
->mapping
);
916 req
->offset
= start
<< page_shift
;
917 if (!wine_server_call( req
))
920 if (reply
->committed
)
922 *vprot
|= VPROT_COMMITTED
;
923 for (i
= 0; i
< ret
>> page_shift
; i
++) view
->prot
[start
+i
] |= VPROT_COMMITTED
;
930 for (i
= start
+ 1; i
< view
->size
>> page_shift
; i
++)
931 if ((*vprot
^ view
->prot
[i
]) & VPROT_COMMITTED
) break;
932 return (i
- start
) << page_shift
;
936 /***********************************************************************
939 * Decommit some pages of a given view.
940 * The csVirtual section must be held by caller.
942 static NTSTATUS
decommit_pages( struct file_view
*view
, size_t start
, size_t size
)
944 if (wine_anon_mmap( (char *)view
->base
+ start
, size
, PROT_NONE
, MAP_FIXED
) != (void *)-1)
946 BYTE
*p
= view
->prot
+ (start
>> page_shift
);
948 while (size
--) *p
++ &= ~VPROT_COMMITTED
;
949 return STATUS_SUCCESS
;
951 return FILE_GetNtStatus();
955 /***********************************************************************
956 * allocate_dos_memory
958 * Allocate the DOS memory range.
960 static NTSTATUS
allocate_dos_memory( struct file_view
**view
, unsigned int vprot
)
964 void * const low_64k
= (void *)0x10000;
965 const size_t dosmem_size
= 0x110000;
966 int unix_prot
= VIRTUAL_GetUnixProt( vprot
);
969 /* check for existing view */
971 if ((ptr
= list_head( &views_list
)))
973 struct file_view
*first_view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
974 if (first_view
->base
< (void *)dosmem_size
) return STATUS_CONFLICTING_ADDRESSES
;
977 /* check without the first 64K */
979 if (wine_mmap_is_in_reserved_area( low_64k
, dosmem_size
- 0x10000 ) != 1)
981 addr
= wine_anon_mmap( low_64k
, dosmem_size
- 0x10000, unix_prot
, 0 );
984 if (addr
!= (void *)-1) munmap( addr
, dosmem_size
- 0x10000 );
985 return map_view( view
, NULL
, dosmem_size
, 0xffff, 0, vprot
);
989 /* now try to allocate the low 64K too */
991 if (wine_mmap_is_in_reserved_area( NULL
, 0x10000 ) != 1)
993 addr
= wine_anon_mmap( (void *)page_size
, 0x10000 - page_size
, unix_prot
, 0 );
994 if (addr
== (void *)page_size
)
996 if (!wine_anon_mmap( NULL
, page_size
, unix_prot
, MAP_FIXED
))
999 TRACE( "successfully mapped low 64K range\n" );
1001 else TRACE( "failed to map page 0\n" );
1005 if (addr
!= (void *)-1) munmap( addr
, 0x10000 - page_size
);
1007 TRACE( "failed to map low 64K range\n" );
1011 /* now reserve the whole range */
1013 size
= (char *)dosmem_size
- (char *)addr
;
1014 wine_anon_mmap( addr
, size
, unix_prot
, MAP_FIXED
);
1015 return create_view( view
, addr
, size
, vprot
);
1019 /***********************************************************************
1022 * Stat the underlying file for a memory view.
1024 static NTSTATUS
stat_mapping_file( struct file_view
*view
, struct stat
*st
)
1027 int unix_fd
, needs_close
;
1029 if (!view
->mapping
) return STATUS_NOT_MAPPED_VIEW
;
1030 if (!(status
= server_get_unix_fd( view
->mapping
, 0, &unix_fd
, &needs_close
, NULL
, NULL
)))
1032 if (fstat( unix_fd
, st
) == -1) status
= FILE_GetNtStatus();
1033 if (needs_close
) close( unix_fd
);
1039 /***********************************************************************
1042 * Map an executable (PE format) image into memory.
1044 static NTSTATUS
map_image( HANDLE hmapping
, int fd
, char *base
, SIZE_T total_size
, SIZE_T mask
,
1045 SIZE_T header_size
, int shared_fd
, HANDLE dup_mapping
, PVOID
*addr_ptr
)
1047 IMAGE_DOS_HEADER
*dos
;
1048 IMAGE_NT_HEADERS
*nt
;
1049 IMAGE_SECTION_HEADER
*sec
;
1050 IMAGE_DATA_DIRECTORY
*imports
;
1051 NTSTATUS status
= STATUS_CONFLICTING_ADDRESSES
;
1056 struct file_view
*view
= NULL
;
1057 char *ptr
, *header_end
;
1060 /* zero-map the whole range */
1062 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1064 if (base
>= (char *)0x110000) /* make sure the DOS area remains free */
1065 status
= map_view( &view
, base
, total_size
, mask
, FALSE
,
1066 VPROT_COMMITTED
| VPROT_READ
| VPROT_EXEC
| VPROT_WRITECOPY
| VPROT_IMAGE
);
1068 if (status
!= STATUS_SUCCESS
)
1069 status
= map_view( &view
, NULL
, total_size
, mask
, FALSE
,
1070 VPROT_COMMITTED
| VPROT_READ
| VPROT_EXEC
| VPROT_WRITECOPY
| VPROT_IMAGE
);
1072 if (status
!= STATUS_SUCCESS
) goto error
;
1075 TRACE_(module
)( "mapped PE file at %p-%p\n", ptr
, ptr
+ total_size
);
1077 /* map the header */
1079 if (fstat( fd
, &st
) == -1)
1081 status
= FILE_GetNtStatus();
1084 status
= STATUS_INVALID_IMAGE_FORMAT
; /* generic error */
1085 if (!st
.st_size
) goto error
;
1086 header_size
= min( header_size
, st
.st_size
);
1087 if (map_file_into_view( view
, fd
, 0, header_size
, 0, VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
,
1088 !dup_mapping
) != STATUS_SUCCESS
) goto error
;
1089 dos
= (IMAGE_DOS_HEADER
*)ptr
;
1090 nt
= (IMAGE_NT_HEADERS
*)(ptr
+ dos
->e_lfanew
);
1091 header_end
= ptr
+ ROUND_SIZE( 0, header_size
);
1092 memset( ptr
+ header_size
, 0, header_end
- (ptr
+ header_size
) );
1093 if ((char *)(nt
+ 1) > header_end
) goto error
;
1094 sec
= (IMAGE_SECTION_HEADER
*)((char*)&nt
->OptionalHeader
+nt
->FileHeader
.SizeOfOptionalHeader
);
1095 if ((char *)(sec
+ nt
->FileHeader
.NumberOfSections
) > header_end
) goto error
;
1097 imports
= nt
->OptionalHeader
.DataDirectory
+ IMAGE_DIRECTORY_ENTRY_IMPORT
;
1098 if (!imports
->Size
|| !imports
->VirtualAddress
) imports
= NULL
;
1100 /* check the architecture */
1103 if (nt
->FileHeader
.Machine
!= IMAGE_FILE_MACHINE_AMD64
)
1104 #elif defined(__ARMEL__)
1105 if (nt
->FileHeader
.Machine
!= IMAGE_FILE_MACHINE_ARM
&&
1106 nt
->FileHeader
.Machine
!= IMAGE_FILE_MACHINE_THUMB
)
1108 if (nt
->FileHeader
.Machine
!= IMAGE_FILE_MACHINE_I386
)
1111 MESSAGE("Trying to load PE image for unsupported architecture (");
1112 switch (nt
->FileHeader
.Machine
)
1114 case IMAGE_FILE_MACHINE_UNKNOWN
: MESSAGE("Unknown"); break;
1115 case IMAGE_FILE_MACHINE_I860
: MESSAGE("I860"); break;
1116 case IMAGE_FILE_MACHINE_I386
: MESSAGE("I386"); break;
1117 case IMAGE_FILE_MACHINE_R3000
: MESSAGE("R3000"); break;
1118 case IMAGE_FILE_MACHINE_R4000
: MESSAGE("R4000"); break;
1119 case IMAGE_FILE_MACHINE_R10000
: MESSAGE("R10000"); break;
1120 case IMAGE_FILE_MACHINE_ALPHA
: MESSAGE("Alpha"); break;
1121 case IMAGE_FILE_MACHINE_POWERPC
: MESSAGE("PowerPC"); break;
1122 case IMAGE_FILE_MACHINE_IA64
: MESSAGE("IA-64"); break;
1123 case IMAGE_FILE_MACHINE_ALPHA64
: MESSAGE("Alpha-64"); break;
1124 case IMAGE_FILE_MACHINE_AMD64
: MESSAGE("AMD-64"); break;
1125 case IMAGE_FILE_MACHINE_ARM
: MESSAGE("ARM"); break;
1126 case IMAGE_FILE_MACHINE_THUMB
: MESSAGE("ARM Thumb"); break;
1127 case IMAGE_FILE_MACHINE_SPARC
: MESSAGE("SPARC"); break;
1128 default: MESSAGE("Unknown-%04x", nt
->FileHeader
.Machine
); break;
1134 /* check for non page-aligned binary */
1136 if (nt
->OptionalHeader
.SectionAlignment
<= page_mask
)
1138 /* unaligned sections, this happens for native subsystem binaries */
1139 /* in that case Windows simply maps in the whole file */
1141 if (map_file_into_view( view
, fd
, 0, total_size
, 0, VPROT_COMMITTED
| VPROT_READ
,
1142 !dup_mapping
) != STATUS_SUCCESS
) goto error
;
1144 /* check that all sections are loaded at the right offset */
1145 if (nt
->OptionalHeader
.FileAlignment
!= nt
->OptionalHeader
.SectionAlignment
) goto error
;
1146 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++)
1148 if (sec
[i
].VirtualAddress
!= sec
[i
].PointerToRawData
)
1149 goto error
; /* Windows refuses to load in that case too */
1152 /* set the image protections */
1153 VIRTUAL_SetProt( view
, ptr
, total_size
,
1154 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
| VPROT_EXEC
);
1156 /* no relocations are performed on non page-aligned binaries */
1161 /* map all the sections */
1163 for (i
= pos
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
1165 static const SIZE_T sector_align
= 0x1ff;
1166 SIZE_T map_size
, file_start
, file_size
, end
;
1168 if (!sec
->Misc
.VirtualSize
)
1169 map_size
= ROUND_SIZE( 0, sec
->SizeOfRawData
);
1171 map_size
= ROUND_SIZE( 0, sec
->Misc
.VirtualSize
);
1173 /* file positions are rounded to sector boundaries regardless of OptionalHeader.FileAlignment */
1174 file_start
= sec
->PointerToRawData
& ~sector_align
;
1175 file_size
= (sec
->SizeOfRawData
+ (sec
->PointerToRawData
& sector_align
) + sector_align
) & ~sector_align
;
1176 if (file_size
> map_size
) file_size
= map_size
;
1178 /* a few sanity checks */
1179 end
= sec
->VirtualAddress
+ ROUND_SIZE( sec
->VirtualAddress
, map_size
);
1180 if (sec
->VirtualAddress
> total_size
|| end
> total_size
|| end
< sec
->VirtualAddress
)
1182 WARN_(module
)( "Section %.8s too large (%x+%lx/%lx)\n",
1183 sec
->Name
, sec
->VirtualAddress
, map_size
, total_size
);
1187 if ((sec
->Characteristics
& IMAGE_SCN_MEM_SHARED
) &&
1188 (sec
->Characteristics
& IMAGE_SCN_MEM_WRITE
))
1190 TRACE_(module
)( "mapping shared section %.8s at %p off %x (%x) size %lx (%lx) flags %x\n",
1191 sec
->Name
, ptr
+ sec
->VirtualAddress
,
1192 sec
->PointerToRawData
, (int)pos
, file_size
, map_size
,
1193 sec
->Characteristics
);
1194 if (map_file_into_view( view
, shared_fd
, sec
->VirtualAddress
, map_size
, pos
,
1195 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITE
,
1196 FALSE
) != STATUS_SUCCESS
)
1198 ERR_(module
)( "Could not map shared section %.8s\n", sec
->Name
);
1202 /* check if the import directory falls inside this section */
1203 if (imports
&& imports
->VirtualAddress
>= sec
->VirtualAddress
&&
1204 imports
->VirtualAddress
< sec
->VirtualAddress
+ map_size
)
1206 UINT_PTR base
= imports
->VirtualAddress
& ~page_mask
;
1207 UINT_PTR end
= base
+ ROUND_SIZE( imports
->VirtualAddress
, imports
->Size
);
1208 if (end
> sec
->VirtualAddress
+ map_size
) end
= sec
->VirtualAddress
+ map_size
;
1210 map_file_into_view( view
, shared_fd
, base
, end
- base
,
1211 pos
+ (base
- sec
->VirtualAddress
),
1212 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
,
1219 TRACE_(module
)( "mapping section %.8s at %p off %x size %x virt %x flags %x\n",
1220 sec
->Name
, ptr
+ sec
->VirtualAddress
,
1221 sec
->PointerToRawData
, sec
->SizeOfRawData
,
1222 sec
->Misc
.VirtualSize
, sec
->Characteristics
);
1224 if (!sec
->PointerToRawData
|| !file_size
) continue;
1226 /* Note: if the section is not aligned properly map_file_into_view will magically
1227 * fall back to read(), so we don't need to check anything here.
1229 end
= file_start
+ file_size
;
1230 if (sec
->PointerToRawData
>= st
.st_size
||
1231 end
> ((st
.st_size
+ sector_align
) & ~sector_align
) ||
1233 map_file_into_view( view
, fd
, sec
->VirtualAddress
, file_size
, file_start
,
1234 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
,
1235 !dup_mapping
) != STATUS_SUCCESS
)
1237 ERR_(module
)( "Could not map section %.8s, file probably truncated\n", sec
->Name
);
1241 if (file_size
& page_mask
)
1243 end
= ROUND_SIZE( 0, file_size
);
1244 if (end
> map_size
) end
= map_size
;
1245 TRACE_(module
)("clearing %p - %p\n",
1246 ptr
+ sec
->VirtualAddress
+ file_size
,
1247 ptr
+ sec
->VirtualAddress
+ end
);
1248 memset( ptr
+ sec
->VirtualAddress
+ file_size
, 0, end
- file_size
);
1253 /* perform base relocation, if necessary */
1256 ((nt
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
) ||
1257 !NtCurrentTeb()->Peb
->ImageBaseAddress
) )
1259 IMAGE_BASE_RELOCATION
*rel
, *end
;
1260 const IMAGE_DATA_DIRECTORY
*relocs
;
1262 if (nt
->FileHeader
.Characteristics
& IMAGE_FILE_RELOCS_STRIPPED
)
1264 WARN_(module
)( "Need to relocate module from %p to %p, but there are no relocation records\n",
1266 status
= STATUS_CONFLICTING_ADDRESSES
;
1270 TRACE_(module
)( "relocating from %p-%p to %p-%p\n",
1271 base
, base
+ total_size
, ptr
, ptr
+ total_size
);
1273 relocs
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1274 rel
= (IMAGE_BASE_RELOCATION
*)(ptr
+ relocs
->VirtualAddress
);
1275 end
= (IMAGE_BASE_RELOCATION
*)(ptr
+ relocs
->VirtualAddress
+ relocs
->Size
);
1278 while (rel
< end
- 1 && rel
->SizeOfBlock
)
1280 if (rel
->VirtualAddress
>= total_size
)
1282 WARN_(module
)( "invalid address %p in relocation %p\n", ptr
+ rel
->VirtualAddress
, rel
);
1283 status
= STATUS_ACCESS_VIOLATION
;
1286 rel
= LdrProcessRelocationBlock( ptr
+ rel
->VirtualAddress
,
1287 (rel
->SizeOfBlock
- sizeof(*rel
)) / sizeof(USHORT
),
1288 (USHORT
*)(rel
+ 1), delta
);
1289 if (!rel
) goto error
;
1293 /* set the image protections */
1295 VIRTUAL_SetProt( view
, ptr
, ROUND_SIZE( 0, header_size
), VPROT_COMMITTED
| VPROT_READ
);
1297 sec
= (IMAGE_SECTION_HEADER
*)((char *)&nt
->OptionalHeader
+nt
->FileHeader
.SizeOfOptionalHeader
);
1298 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
1301 BYTE vprot
= VPROT_COMMITTED
;
1303 if (sec
->Misc
.VirtualSize
)
1304 size
= ROUND_SIZE( sec
->VirtualAddress
, sec
->Misc
.VirtualSize
);
1306 size
= ROUND_SIZE( sec
->VirtualAddress
, sec
->SizeOfRawData
);
1308 if (sec
->Characteristics
& IMAGE_SCN_MEM_READ
) vprot
|= VPROT_READ
;
1309 if (sec
->Characteristics
& IMAGE_SCN_MEM_WRITE
) vprot
|= VPROT_READ
|VPROT_WRITE
;
1310 if (sec
->Characteristics
& IMAGE_SCN_MEM_EXECUTE
) vprot
|= VPROT_EXEC
;
1312 /* Dumb game crack lets the AOEP point into a data section. Adjust. */
1313 if ((nt
->OptionalHeader
.AddressOfEntryPoint
>= sec
->VirtualAddress
) &&
1314 (nt
->OptionalHeader
.AddressOfEntryPoint
< sec
->VirtualAddress
+ size
))
1315 vprot
|= VPROT_EXEC
;
1317 if (!VIRTUAL_SetProt( view
, ptr
+ sec
->VirtualAddress
, size
, vprot
) && (vprot
& VPROT_EXEC
))
1318 ERR( "failed to set %08x protection on section %.8s, noexec filesystem?\n",
1319 sec
->Characteristics
, sec
->Name
);
1323 view
->mapping
= dup_mapping
;
1324 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1327 #ifdef VALGRIND_LOAD_PDB_DEBUGINFO
1328 VALGRIND_LOAD_PDB_DEBUGINFO(fd
, ptr
, total_size
, delta
);
1330 if (ptr
!= base
) return STATUS_IMAGE_NOT_AT_BASE
;
1331 return STATUS_SUCCESS
;
1334 if (view
) delete_view( view
);
1335 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1336 if (dup_mapping
) NtClose( dup_mapping
);
1341 /* callback for wine_mmap_enum_reserved_areas to allocate space for the virtual heap */
1342 static int alloc_virtual_heap( void *base
, size_t size
, void *arg
)
1344 void **heap_base
= arg
;
1346 if (is_beyond_limit( base
, size
, address_space_limit
)) address_space_limit
= (char *)base
+ size
;
1347 if (size
< VIRTUAL_HEAP_SIZE
) return 0;
1348 *heap_base
= wine_anon_mmap( (char *)base
+ size
- VIRTUAL_HEAP_SIZE
,
1349 VIRTUAL_HEAP_SIZE
, PROT_READ
|PROT_WRITE
, MAP_FIXED
);
1350 return (*heap_base
!= (void *)-1);
1353 /***********************************************************************
1356 void virtual_init(void)
1358 const char *preload
;
1360 struct file_view
*heap_view
;
1363 page_size
= getpagesize();
1364 page_mask
= page_size
- 1;
1365 /* Make sure we have a power of 2 */
1366 assert( !(page_size
& page_mask
) );
1368 while ((1 << page_shift
) != page_size
) page_shift
++;
1369 user_space_limit
= working_set_limit
= address_space_limit
= (void *)~page_mask
;
1370 #endif /* page_mask */
1371 if ((preload
= getenv("WINEPRELOADRESERVE")))
1373 unsigned long start
, end
;
1374 if (sscanf( preload
, "%lx-%lx", &start
, &end
) == 2)
1376 preload_reserve_start
= (void *)start
;
1377 preload_reserve_end
= (void *)end
;
1381 /* try to find space in a reserved area for the virtual heap */
1382 if (!wine_mmap_enum_reserved_areas( alloc_virtual_heap
, &heap_base
, 1 ))
1383 heap_base
= wine_anon_mmap( NULL
, VIRTUAL_HEAP_SIZE
, PROT_READ
|PROT_WRITE
, 0 );
1385 assert( heap_base
!= (void *)-1 );
1386 virtual_heap
= RtlCreateHeap( HEAP_NO_SERIALIZE
, heap_base
, VIRTUAL_HEAP_SIZE
,
1387 VIRTUAL_HEAP_SIZE
, NULL
, NULL
);
1388 create_view( &heap_view
, heap_base
, VIRTUAL_HEAP_SIZE
, VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITE
);
1390 /* make the DOS area accessible to hide bugs in broken apps like Excel 2003 */
1391 if (wine_mmap_is_in_reserved_area( (void *)0x10000, 0x100000 ) == 1)
1392 wine_anon_mmap( (void *)0x10000, 0x100000, PROT_READ
| PROT_WRITE
, MAP_FIXED
);
1396 /***********************************************************************
1397 * virtual_init_threading
1399 void virtual_init_threading(void)
1405 /***********************************************************************
1406 * virtual_get_system_info
1408 void virtual_get_system_info( SYSTEM_BASIC_INFORMATION
*info
)
1411 info
->KeMaximumIncrement
= 0; /* FIXME */
1412 info
->PageSize
= page_size
;
1413 info
->MmLowestPhysicalPage
= 1;
1414 info
->MmHighestPhysicalPage
= 0x7fffffff / page_size
;
1415 info
->MmNumberOfPhysicalPages
= info
->MmHighestPhysicalPage
- info
->MmLowestPhysicalPage
;
1416 info
->AllocationGranularity
= get_mask(0) + 1;
1417 info
->LowestUserAddress
= (void *)0x10000;
1418 info
->HighestUserAddress
= (char *)user_space_limit
- 1;
1419 info
->ActiveProcessorsAffinityMask
= (1 << NtCurrentTeb()->Peb
->NumberOfProcessors
) - 1;
1420 info
->NumberOfProcessors
= NtCurrentTeb()->Peb
->NumberOfProcessors
;
1424 /***********************************************************************
1425 * virtual_create_system_view
1427 NTSTATUS
virtual_create_builtin_view( void *module
)
1431 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( module
);
1432 SIZE_T size
= nt
->OptionalHeader
.SizeOfImage
;
1433 IMAGE_SECTION_HEADER
*sec
;
1434 struct file_view
*view
;
1438 size
= ROUND_SIZE( module
, size
);
1439 base
= ROUND_ADDR( module
, page_mask
);
1440 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1441 status
= create_view( &view
, base
, size
, VPROT_SYSTEM
| VPROT_IMAGE
|
1442 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
| VPROT_EXEC
);
1443 if (!status
) TRACE( "created %p-%p\n", base
, (char *)base
+ size
);
1444 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1446 if (status
) return status
;
1448 /* The PE header is always read-only, no write, no execute. */
1449 view
->prot
[0] = VPROT_COMMITTED
| VPROT_READ
;
1451 sec
= (IMAGE_SECTION_HEADER
*)((char *)&nt
->OptionalHeader
+ nt
->FileHeader
.SizeOfOptionalHeader
);
1452 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++)
1454 BYTE flags
= VPROT_COMMITTED
;
1456 if (sec
[i
].Characteristics
& IMAGE_SCN_MEM_EXECUTE
) flags
|= VPROT_EXEC
;
1457 if (sec
[i
].Characteristics
& IMAGE_SCN_MEM_READ
) flags
|= VPROT_READ
;
1458 if (sec
[i
].Characteristics
& IMAGE_SCN_MEM_WRITE
) flags
|= VPROT_WRITE
;
1459 memset (view
->prot
+ (sec
[i
].VirtualAddress
>> page_shift
), flags
,
1460 ROUND_SIZE( sec
[i
].VirtualAddress
, sec
[i
].Misc
.VirtualSize
) >> page_shift
);
1467 /***********************************************************************
1468 * virtual_alloc_thread_stack
1470 NTSTATUS
virtual_alloc_thread_stack( TEB
*teb
, SIZE_T reserve_size
, SIZE_T commit_size
)
1472 struct file_view
*view
;
1477 if (!reserve_size
|| !commit_size
)
1479 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( NtCurrentTeb()->Peb
->ImageBaseAddress
);
1480 if (!reserve_size
) reserve_size
= nt
->OptionalHeader
.SizeOfStackReserve
;
1481 if (!commit_size
) commit_size
= nt
->OptionalHeader
.SizeOfStackCommit
;
1484 size
= max( reserve_size
, commit_size
);
1485 if (size
< 1024 * 1024) size
= 1024 * 1024; /* Xlib needs a large stack */
1486 size
= (size
+ 0xffff) & ~0xffff; /* round to 64K boundary */
1488 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1490 if ((status
= map_view( &view
, NULL
, size
, 0xffff, 0,
1491 VPROT_READ
| VPROT_WRITE
| VPROT_COMMITTED
| VPROT_VALLOC
)) != STATUS_SUCCESS
)
1494 #ifdef VALGRIND_STACK_REGISTER
1495 VALGRIND_STACK_REGISTER( view
->base
, (char *)view
->base
+ view
->size
);
1498 /* setup no access guard page */
1499 VIRTUAL_SetProt( view
, view
->base
, page_size
, VPROT_COMMITTED
);
1500 VIRTUAL_SetProt( view
, (char *)view
->base
+ page_size
, page_size
,
1501 VPROT_READ
| VPROT_WRITE
| VPROT_COMMITTED
| VPROT_GUARD
);
1503 /* note: limit is lower than base since the stack grows down */
1504 teb
->DeallocationStack
= view
->base
;
1505 teb
->Tib
.StackBase
= (char *)view
->base
+ view
->size
;
1506 teb
->Tib
.StackLimit
= (char *)view
->base
+ 2 * page_size
;
1508 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1513 /***********************************************************************
1514 * virtual_clear_thread_stack
1516 * Clear the stack contents before calling the main entry point, some broken apps need that.
1518 void virtual_clear_thread_stack(void)
1520 void *stack
= NtCurrentTeb()->Tib
.StackLimit
;
1521 size_t size
= (char *)NtCurrentTeb()->Tib
.StackBase
- (char *)NtCurrentTeb()->Tib
.StackLimit
;
1523 wine_anon_mmap( stack
, size
, PROT_READ
| PROT_WRITE
, MAP_FIXED
);
1524 if (force_exec_prot
) mprotect( stack
, size
, PROT_READ
| PROT_WRITE
| PROT_EXEC
);
1528 /***********************************************************************
1529 * virtual_handle_fault
1531 NTSTATUS
virtual_handle_fault( LPCVOID addr
, DWORD err
)
1533 struct file_view
*view
;
1534 NTSTATUS ret
= STATUS_ACCESS_VIOLATION
;
1537 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1538 if ((view
= VIRTUAL_FindView( addr
, 0 )))
1540 void *page
= ROUND_ADDR( addr
, page_mask
);
1541 BYTE
*vprot
= &view
->prot
[((const char *)page
- (const char *)view
->base
) >> page_shift
];
1542 if (*vprot
& VPROT_GUARD
)
1544 VIRTUAL_SetProt( view
, page
, page_size
, *vprot
& ~VPROT_GUARD
);
1545 ret
= STATUS_GUARD_PAGE_VIOLATION
;
1547 if ((err
& EXCEPTION_WRITE_FAULT
) && (view
->protect
& VPROT_WRITEWATCH
))
1549 if (*vprot
& VPROT_WRITEWATCH
)
1551 *vprot
&= ~VPROT_WRITEWATCH
;
1552 VIRTUAL_SetProt( view
, page
, page_size
, *vprot
);
1554 /* ignore fault if page is writable now */
1555 if (VIRTUAL_GetUnixProt( *vprot
) & PROT_WRITE
) ret
= STATUS_SUCCESS
;
1558 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 if ((char *)page
+ page_size
== NtCurrentTeb()->Tib
.StackLimit
)
1584 NtCurrentTeb()->Tib
.StackLimit
= page
;
1588 RtlLeaveCriticalSection( &csVirtual
);
1593 /***********************************************************************
1594 * virtual_check_buffer_for_read
1596 * Check if a memory buffer can be read, triggering page faults if needed for DIB section access.
1598 BOOL
virtual_check_buffer_for_read( const void *ptr
, SIZE_T size
)
1600 if (!size
) return TRUE
;
1601 if (!ptr
) return FALSE
;
1605 volatile const char *p
= ptr
;
1607 SIZE_T count
= size
;
1609 while (count
> page_size
)
1616 dummy
= p
[count
- 1];
1627 /***********************************************************************
1628 * virtual_check_buffer_for_write
1630 * Check if a memory buffer can be written to, triggering page faults if needed for write watches.
1632 BOOL
virtual_check_buffer_for_write( void *ptr
, SIZE_T size
)
1634 if (!size
) return TRUE
;
1635 if (!ptr
) return FALSE
;
1639 volatile char *p
= ptr
;
1640 SIZE_T count
= size
;
1642 while (count
> page_size
)
1660 /***********************************************************************
1661 * VIRTUAL_SetForceExec
1663 * Whether to force exec prot on all views.
1665 void VIRTUAL_SetForceExec( BOOL enable
)
1667 struct file_view
*view
;
1670 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1671 if (!force_exec_prot
!= !enable
) /* change all existing views */
1673 force_exec_prot
= enable
;
1675 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
1678 char *addr
= view
->base
;
1679 BYTE commit
= view
->mapping
? VPROT_COMMITTED
: 0; /* file mappings are always accessible */
1680 int unix_prot
= VIRTUAL_GetUnixProt( view
->prot
[0] | commit
);
1682 if (view
->protect
& VPROT_NOEXEC
) continue;
1683 for (count
= i
= 1; i
< view
->size
>> page_shift
; i
++, count
++)
1685 int prot
= VIRTUAL_GetUnixProt( view
->prot
[i
] | commit
);
1686 if (prot
== unix_prot
) continue;
1687 if ((unix_prot
& PROT_READ
) && !(unix_prot
& PROT_EXEC
))
1689 TRACE( "%s exec prot for %p-%p\n",
1690 force_exec_prot
? "enabling" : "disabling",
1691 addr
, addr
+ (count
<< page_shift
) - 1 );
1692 mprotect( addr
, count
<< page_shift
,
1693 unix_prot
| (force_exec_prot
? PROT_EXEC
: 0) );
1695 addr
+= (count
<< page_shift
);
1701 if ((unix_prot
& PROT_READ
) && !(unix_prot
& PROT_EXEC
))
1703 TRACE( "%s exec prot for %p-%p\n",
1704 force_exec_prot
? "enabling" : "disabling",
1705 addr
, addr
+ (count
<< page_shift
) - 1 );
1706 mprotect( addr
, count
<< page_shift
,
1707 unix_prot
| (force_exec_prot
? PROT_EXEC
: 0) );
1712 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1721 /* free reserved areas above the limit; callback for wine_mmap_enum_reserved_areas */
1722 static int free_reserved_memory( void *base
, size_t size
, void *arg
)
1724 struct free_range
*range
= arg
;
1726 if ((char *)base
>= range
->limit
) return 0;
1727 if ((char *)base
+ size
<= range
->base
) return 0;
1728 if ((char *)base
< range
->base
)
1730 size
-= range
->base
- (char *)base
;
1733 if ((char *)base
+ size
> range
->limit
) size
= range
->limit
- (char *)base
;
1734 remove_reserved_area( base
, size
);
1735 return 1; /* stop enumeration since the list has changed */
1738 /***********************************************************************
1739 * virtual_release_address_space
1741 * Release some address space once we have loaded and initialized the app.
1743 void virtual_release_address_space( BOOL free_high_mem
)
1745 struct free_range range
;
1748 if (user_space_limit
== address_space_limit
) return; /* no need to free anything */
1750 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1752 /* no large address space on win9x */
1753 if (free_high_mem
&& NtCurrentTeb()->Peb
->OSPlatformId
== VER_PLATFORM_WIN32_NT
)
1755 range
.base
= (char *)0x82000000;
1756 range
.limit
= address_space_limit
;
1757 while (wine_mmap_enum_reserved_areas( free_reserved_memory
, &range
, 1 )) /* nothing */;
1758 user_space_limit
= working_set_limit
= address_space_limit
;
1762 #ifndef __APPLE__ /* dyld doesn't support parts of the WINE_DOS segment being unmapped */
1763 range
.base
= (char *)0x20000000;
1764 range
.limit
= (char *)0x7f000000;
1765 while (wine_mmap_enum_reserved_areas( free_reserved_memory
, &range
, 0 )) /* nothing */;
1769 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1773 /***********************************************************************
1774 * NtAllocateVirtualMemory (NTDLL.@)
1775 * ZwAllocateVirtualMemory (NTDLL.@)
1777 NTSTATUS WINAPI
NtAllocateVirtualMemory( HANDLE process
, PVOID
*ret
, ULONG zero_bits
,
1778 SIZE_T
*size_ptr
, ULONG type
, ULONG protect
)
1782 SIZE_T size
= *size_ptr
;
1783 SIZE_T mask
= get_mask( zero_bits
);
1784 NTSTATUS status
= STATUS_SUCCESS
;
1785 struct file_view
*view
;
1788 TRACE("%p %p %08lx %x %08x\n", process
, *ret
, size
, type
, protect
);
1790 if (!size
) return STATUS_INVALID_PARAMETER
;
1792 if (process
!= NtCurrentProcess())
1795 apc_result_t result
;
1797 memset( &call
, 0, sizeof(call
) );
1799 call
.virtual_alloc
.type
= APC_VIRTUAL_ALLOC
;
1800 call
.virtual_alloc
.addr
= wine_server_client_ptr( *ret
);
1801 call
.virtual_alloc
.size
= *size_ptr
;
1802 call
.virtual_alloc
.zero_bits
= zero_bits
;
1803 call
.virtual_alloc
.op_type
= type
;
1804 call
.virtual_alloc
.prot
= protect
;
1805 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
1806 if (status
!= STATUS_SUCCESS
) return status
;
1808 if (result
.virtual_alloc
.status
== STATUS_SUCCESS
)
1810 *ret
= wine_server_get_ptr( result
.virtual_alloc
.addr
);
1811 *size_ptr
= result
.virtual_alloc
.size
;
1813 return result
.virtual_alloc
.status
;
1816 /* Round parameters to a page boundary */
1818 if (is_beyond_limit( 0, size
, working_set_limit
)) return STATUS_WORKING_SET_LIMIT_RANGE
;
1820 if ((status
= get_vprot_flags( protect
, &vprot
))) return status
;
1821 vprot
|= VPROT_VALLOC
;
1822 if (type
& MEM_COMMIT
) vprot
|= VPROT_COMMITTED
;
1826 if (type
& MEM_RESERVE
) /* Round down to 64k boundary */
1827 base
= ROUND_ADDR( *ret
, mask
);
1829 base
= ROUND_ADDR( *ret
, page_mask
);
1830 size
= (((UINT_PTR
)*ret
+ size
+ page_mask
) & ~page_mask
) - (UINT_PTR
)base
;
1832 /* address 1 is magic to mean DOS area */
1833 if (!base
&& *ret
== (void *)1 && size
== 0x110000)
1835 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1836 status
= allocate_dos_memory( &view
, vprot
);
1837 if (status
== STATUS_SUCCESS
)
1840 *size_ptr
= view
->size
;
1842 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1846 /* disallow low 64k, wrap-around and kernel space */
1847 if (((char *)base
< (char *)0x10000) ||
1848 ((char *)base
+ size
< (char *)base
) ||
1849 is_beyond_limit( base
, size
, address_space_limit
))
1850 return STATUS_INVALID_PARAMETER
;
1855 size
= (size
+ page_mask
) & ~page_mask
;
1858 /* Compute the alloc type flags */
1860 if (!(type
& (MEM_COMMIT
| MEM_RESERVE
| MEM_RESET
)) ||
1861 (type
& ~(MEM_COMMIT
| MEM_RESERVE
| MEM_TOP_DOWN
| MEM_WRITE_WATCH
| MEM_RESET
)))
1863 WARN("called with wrong alloc type flags (%08x) !\n", type
);
1864 return STATUS_INVALID_PARAMETER
;
1867 /* Reserve the memory */
1869 if (use_locks
) server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1871 if ((type
& MEM_RESERVE
) || !base
)
1873 if (type
& MEM_WRITE_WATCH
) vprot
|= VPROT_WRITEWATCH
;
1874 status
= map_view( &view
, base
, size
, mask
, type
& MEM_TOP_DOWN
, vprot
);
1875 if (status
== STATUS_SUCCESS
) base
= view
->base
;
1877 else if (type
& MEM_RESET
)
1879 if (!(view
= VIRTUAL_FindView( base
, size
))) status
= STATUS_NOT_MAPPED_VIEW
;
1880 else madvise( base
, size
, MADV_DONTNEED
);
1882 else /* commit the pages */
1884 if (!(view
= VIRTUAL_FindView( base
, size
))) status
= STATUS_NOT_MAPPED_VIEW
;
1885 else if (!VIRTUAL_SetProt( view
, base
, size
, vprot
)) status
= STATUS_ACCESS_DENIED
;
1886 else if (view
->mapping
&& !(view
->protect
& VPROT_COMMITTED
))
1888 SERVER_START_REQ( add_mapping_committed_range
)
1890 req
->handle
= wine_server_obj_handle( view
->mapping
);
1891 req
->offset
= (char *)base
- (char *)view
->base
;
1893 wine_server_call( req
);
1899 if (use_locks
) server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1901 if (status
== STATUS_SUCCESS
)
1910 /***********************************************************************
1911 * NtFreeVirtualMemory (NTDLL.@)
1912 * ZwFreeVirtualMemory (NTDLL.@)
1914 NTSTATUS WINAPI
NtFreeVirtualMemory( HANDLE process
, PVOID
*addr_ptr
, SIZE_T
*size_ptr
, ULONG type
)
1916 struct file_view
*view
;
1919 NTSTATUS status
= STATUS_SUCCESS
;
1920 LPVOID addr
= *addr_ptr
;
1921 SIZE_T size
= *size_ptr
;
1923 TRACE("%p %p %08lx %x\n", process
, addr
, size
, type
);
1925 if (process
!= NtCurrentProcess())
1928 apc_result_t result
;
1930 memset( &call
, 0, sizeof(call
) );
1932 call
.virtual_free
.type
= APC_VIRTUAL_FREE
;
1933 call
.virtual_free
.addr
= wine_server_client_ptr( addr
);
1934 call
.virtual_free
.size
= size
;
1935 call
.virtual_free
.op_type
= type
;
1936 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
1937 if (status
!= STATUS_SUCCESS
) return status
;
1939 if (result
.virtual_free
.status
== STATUS_SUCCESS
)
1941 *addr_ptr
= wine_server_get_ptr( result
.virtual_free
.addr
);
1942 *size_ptr
= result
.virtual_free
.size
;
1944 return result
.virtual_free
.status
;
1947 /* Fix the parameters */
1949 size
= ROUND_SIZE( addr
, size
);
1950 base
= ROUND_ADDR( addr
, page_mask
);
1952 /* avoid freeing the DOS area when a broken app passes a NULL pointer */
1953 if (!base
) return STATUS_INVALID_PARAMETER
;
1955 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1957 if (!(view
= VIRTUAL_FindView( base
, size
)) || !(view
->protect
& VPROT_VALLOC
))
1959 status
= STATUS_INVALID_PARAMETER
;
1961 else if (type
== MEM_RELEASE
)
1963 /* Free the pages */
1965 if (size
|| (base
!= view
->base
)) status
= STATUS_INVALID_PARAMETER
;
1968 delete_view( view
);
1973 else if (type
== MEM_DECOMMIT
)
1975 status
= decommit_pages( view
, base
- (char *)view
->base
, size
);
1976 if (status
== STATUS_SUCCESS
)
1984 WARN("called with wrong free type flags (%08x) !\n", type
);
1985 status
= STATUS_INVALID_PARAMETER
;
1988 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1993 /***********************************************************************
1994 * NtProtectVirtualMemory (NTDLL.@)
1995 * ZwProtectVirtualMemory (NTDLL.@)
1997 NTSTATUS WINAPI
NtProtectVirtualMemory( HANDLE process
, PVOID
*addr_ptr
, SIZE_T
*size_ptr
,
1998 ULONG new_prot
, ULONG
*old_prot
)
2000 struct file_view
*view
;
2002 NTSTATUS status
= STATUS_SUCCESS
;
2005 unsigned int new_vprot
;
2006 SIZE_T size
= *size_ptr
;
2007 LPVOID addr
= *addr_ptr
;
2009 TRACE("%p %p %08lx %08x\n", process
, addr
, size
, new_prot
);
2011 if (process
!= NtCurrentProcess())
2014 apc_result_t result
;
2016 memset( &call
, 0, sizeof(call
) );
2018 call
.virtual_protect
.type
= APC_VIRTUAL_PROTECT
;
2019 call
.virtual_protect
.addr
= wine_server_client_ptr( addr
);
2020 call
.virtual_protect
.size
= size
;
2021 call
.virtual_protect
.prot
= new_prot
;
2022 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
2023 if (status
!= STATUS_SUCCESS
) return status
;
2025 if (result
.virtual_protect
.status
== STATUS_SUCCESS
)
2027 *addr_ptr
= wine_server_get_ptr( result
.virtual_protect
.addr
);
2028 *size_ptr
= result
.virtual_protect
.size
;
2029 if (old_prot
) *old_prot
= result
.virtual_protect
.prot
;
2031 return result
.virtual_protect
.status
;
2034 /* Fix the parameters */
2036 size
= ROUND_SIZE( addr
, size
);
2037 base
= ROUND_ADDR( addr
, page_mask
);
2038 if ((status
= get_vprot_flags( new_prot
, &new_vprot
))) return status
;
2039 new_vprot
|= VPROT_COMMITTED
;
2041 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2043 if (!(view
= VIRTUAL_FindView( base
, size
)))
2045 status
= STATUS_INVALID_PARAMETER
;
2049 /* Make sure all the pages are committed */
2050 if (get_committed_size( view
, base
, &vprot
) >= size
&& (vprot
& VPROT_COMMITTED
))
2052 if (old_prot
) *old_prot
= VIRTUAL_GetWin32Prot( vprot
);
2053 if (!VIRTUAL_SetProt( view
, base
, size
, new_vprot
)) status
= STATUS_ACCESS_DENIED
;
2055 else status
= STATUS_NOT_COMMITTED
;
2057 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2059 if (status
== STATUS_SUCCESS
)
2068 /* retrieve state for a free memory area; callback for wine_mmap_enum_reserved_areas */
2069 static int get_free_mem_state_callback( void *start
, size_t size
, void *arg
)
2071 MEMORY_BASIC_INFORMATION
*info
= arg
;
2072 void *end
= (char *)start
+ size
;
2074 if ((char *)info
->BaseAddress
+ info
->RegionSize
< (char *)start
) return 0;
2076 if (info
->BaseAddress
>= end
)
2078 if (info
->AllocationBase
< end
) info
->AllocationBase
= end
;
2082 if (info
->BaseAddress
>= start
|| start
<= address_space_start
)
2084 /* it's a real free area */
2085 info
->State
= MEM_FREE
;
2086 info
->Protect
= PAGE_NOACCESS
;
2087 info
->AllocationBase
= 0;
2088 info
->AllocationProtect
= 0;
2090 if ((char *)info
->BaseAddress
+ info
->RegionSize
> (char *)end
)
2091 info
->RegionSize
= (char *)end
- (char *)info
->BaseAddress
;
2093 else /* outside of the reserved area, pretend it's allocated */
2095 info
->RegionSize
= (char *)start
- (char *)info
->BaseAddress
;
2096 info
->State
= MEM_RESERVE
;
2097 info
->Protect
= PAGE_NOACCESS
;
2098 info
->AllocationProtect
= PAGE_NOACCESS
;
2099 info
->Type
= MEM_PRIVATE
;
2104 #define UNIMPLEMENTED_INFO_CLASS(c) \
2106 FIXME("(process=%p,addr=%p) Unimplemented information class: " #c "\n", process, addr); \
2107 return STATUS_INVALID_INFO_CLASS
2109 /***********************************************************************
2110 * NtQueryVirtualMemory (NTDLL.@)
2111 * ZwQueryVirtualMemory (NTDLL.@)
2113 NTSTATUS WINAPI
NtQueryVirtualMemory( HANDLE process
, LPCVOID addr
,
2114 MEMORY_INFORMATION_CLASS info_class
, PVOID buffer
,
2115 SIZE_T len
, SIZE_T
*res_len
)
2117 struct file_view
*view
;
2118 char *base
, *alloc_base
= 0;
2121 MEMORY_BASIC_INFORMATION
*info
= buffer
;
2124 if (info_class
!= MemoryBasicInformation
)
2128 UNIMPLEMENTED_INFO_CLASS(MemoryWorkingSetList
);
2129 UNIMPLEMENTED_INFO_CLASS(MemorySectionName
);
2130 UNIMPLEMENTED_INFO_CLASS(MemoryBasicVlmInformation
);
2133 FIXME("(%p,%p,info_class=%d,%p,%ld,%p) Unknown information class\n",
2134 process
, addr
, info_class
, buffer
, len
, res_len
);
2135 return STATUS_INVALID_INFO_CLASS
;
2139 if (process
!= NtCurrentProcess())
2143 apc_result_t result
;
2145 memset( &call
, 0, sizeof(call
) );
2147 call
.virtual_query
.type
= APC_VIRTUAL_QUERY
;
2148 call
.virtual_query
.addr
= wine_server_client_ptr( addr
);
2149 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
2150 if (status
!= STATUS_SUCCESS
) return status
;
2152 if (result
.virtual_query
.status
== STATUS_SUCCESS
)
2154 info
->BaseAddress
= wine_server_get_ptr( result
.virtual_query
.base
);
2155 info
->AllocationBase
= wine_server_get_ptr( result
.virtual_query
.alloc_base
);
2156 info
->RegionSize
= result
.virtual_query
.size
;
2157 info
->Protect
= result
.virtual_query
.prot
;
2158 info
->AllocationProtect
= result
.virtual_query
.alloc_prot
;
2159 info
->State
= (DWORD
)result
.virtual_query
.state
<< 12;
2160 info
->Type
= (DWORD
)result
.virtual_query
.alloc_type
<< 16;
2161 if (info
->RegionSize
!= result
.virtual_query
.size
) /* truncated */
2162 return STATUS_INVALID_PARAMETER
; /* FIXME */
2163 if (res_len
) *res_len
= sizeof(*info
);
2165 return result
.virtual_query
.status
;
2168 base
= ROUND_ADDR( addr
, page_mask
);
2170 if (is_beyond_limit( base
, 1, working_set_limit
)) return STATUS_WORKING_SET_LIMIT_RANGE
;
2172 /* Find the view containing the address */
2174 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2175 ptr
= list_head( &views_list
);
2180 size
= (char *)working_set_limit
- alloc_base
;
2184 view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
2185 if ((char *)view
->base
> base
)
2187 size
= (char *)view
->base
- alloc_base
;
2191 if ((char *)view
->base
+ view
->size
> base
)
2193 alloc_base
= view
->base
;
2197 alloc_base
= (char *)view
->base
+ view
->size
;
2198 ptr
= list_next( &views_list
, ptr
);
2201 /* Fill the info structure */
2203 info
->AllocationBase
= alloc_base
;
2204 info
->BaseAddress
= base
;
2205 info
->RegionSize
= size
- (base
- alloc_base
);
2209 if (!wine_mmap_enum_reserved_areas( get_free_mem_state_callback
, info
, 0 ))
2211 /* not in a reserved area at all, pretend it's allocated */
2213 if (base
>= (char *)address_space_start
)
2215 info
->State
= MEM_RESERVE
;
2216 info
->Protect
= PAGE_NOACCESS
;
2217 info
->AllocationProtect
= PAGE_NOACCESS
;
2218 info
->Type
= MEM_PRIVATE
;
2223 info
->State
= MEM_FREE
;
2224 info
->Protect
= PAGE_NOACCESS
;
2225 info
->AllocationBase
= 0;
2226 info
->AllocationProtect
= 0;
2234 SIZE_T range_size
= get_committed_size( view
, base
, &vprot
);
2236 info
->State
= (vprot
& VPROT_COMMITTED
) ? MEM_COMMIT
: MEM_RESERVE
;
2237 info
->Protect
= (vprot
& VPROT_COMMITTED
) ? VIRTUAL_GetWin32Prot( vprot
) : 0;
2238 info
->AllocationBase
= alloc_base
;
2239 info
->AllocationProtect
= VIRTUAL_GetWin32Prot( view
->protect
);
2240 if (view
->protect
& VPROT_IMAGE
) info
->Type
= MEM_IMAGE
;
2241 else if (view
->protect
& VPROT_VALLOC
) info
->Type
= MEM_PRIVATE
;
2242 else info
->Type
= MEM_MAPPED
;
2243 for (size
= base
- alloc_base
; size
< base
+ range_size
- alloc_base
; size
+= page_size
)
2244 if ((view
->prot
[size
>> page_shift
] ^ vprot
) & ~VPROT_WRITEWATCH
) break;
2245 info
->RegionSize
= size
- (base
- alloc_base
);
2247 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2249 if (res_len
) *res_len
= sizeof(*info
);
2250 return STATUS_SUCCESS
;
2254 /***********************************************************************
2255 * NtLockVirtualMemory (NTDLL.@)
2256 * ZwLockVirtualMemory (NTDLL.@)
2258 NTSTATUS WINAPI
NtLockVirtualMemory( HANDLE process
, PVOID
*addr
, SIZE_T
*size
, ULONG unknown
)
2260 NTSTATUS status
= STATUS_SUCCESS
;
2262 if (process
!= NtCurrentProcess())
2265 apc_result_t result
;
2267 memset( &call
, 0, sizeof(call
) );
2269 call
.virtual_lock
.type
= APC_VIRTUAL_LOCK
;
2270 call
.virtual_lock
.addr
= wine_server_client_ptr( *addr
);
2271 call
.virtual_lock
.size
= *size
;
2272 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
2273 if (status
!= STATUS_SUCCESS
) return status
;
2275 if (result
.virtual_lock
.status
== STATUS_SUCCESS
)
2277 *addr
= wine_server_get_ptr( result
.virtual_lock
.addr
);
2278 *size
= result
.virtual_lock
.size
;
2280 return result
.virtual_lock
.status
;
2283 *size
= ROUND_SIZE( *addr
, *size
);
2284 *addr
= ROUND_ADDR( *addr
, page_mask
);
2286 if (mlock( *addr
, *size
)) status
= STATUS_ACCESS_DENIED
;
2291 /***********************************************************************
2292 * NtUnlockVirtualMemory (NTDLL.@)
2293 * ZwUnlockVirtualMemory (NTDLL.@)
2295 NTSTATUS WINAPI
NtUnlockVirtualMemory( HANDLE process
, PVOID
*addr
, SIZE_T
*size
, ULONG unknown
)
2297 NTSTATUS status
= STATUS_SUCCESS
;
2299 if (process
!= NtCurrentProcess())
2302 apc_result_t result
;
2304 memset( &call
, 0, sizeof(call
) );
2306 call
.virtual_unlock
.type
= APC_VIRTUAL_UNLOCK
;
2307 call
.virtual_unlock
.addr
= wine_server_client_ptr( *addr
);
2308 call
.virtual_unlock
.size
= *size
;
2309 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
2310 if (status
!= STATUS_SUCCESS
) return status
;
2312 if (result
.virtual_unlock
.status
== STATUS_SUCCESS
)
2314 *addr
= wine_server_get_ptr( result
.virtual_unlock
.addr
);
2315 *size
= result
.virtual_unlock
.size
;
2317 return result
.virtual_unlock
.status
;
2320 *size
= ROUND_SIZE( *addr
, *size
);
2321 *addr
= ROUND_ADDR( *addr
, page_mask
);
2323 if (munlock( *addr
, *size
)) status
= STATUS_ACCESS_DENIED
;
2328 /***********************************************************************
2329 * NtCreateSection (NTDLL.@)
2330 * ZwCreateSection (NTDLL.@)
2332 NTSTATUS WINAPI
NtCreateSection( HANDLE
*handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
,
2333 const LARGE_INTEGER
*size
, ULONG protect
,
2334 ULONG sec_flags
, HANDLE file
)
2338 DWORD len
= (attr
&& attr
->ObjectName
) ? attr
->ObjectName
->Length
: 0;
2339 struct security_descriptor
*sd
= NULL
;
2340 struct object_attributes objattr
;
2342 /* Check parameters */
2344 if (len
> MAX_PATH
*sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
2346 if ((ret
= get_vprot_flags( protect
, &vprot
))) return ret
;
2348 objattr
.rootdir
= wine_server_obj_handle( attr
? attr
->RootDirectory
: 0 );
2350 objattr
.name_len
= len
;
2353 ret
= NTDLL_create_struct_sd( attr
->SecurityDescriptor
, &sd
, &objattr
.sd_len
);
2354 if (ret
!= STATUS_SUCCESS
) return ret
;
2357 if (!(sec_flags
& SEC_RESERVE
)) vprot
|= VPROT_COMMITTED
;
2358 if (sec_flags
& SEC_NOCACHE
) vprot
|= VPROT_NOCACHE
;
2359 if (sec_flags
& SEC_IMAGE
) vprot
|= VPROT_IMAGE
;
2361 /* Create the server object */
2363 SERVER_START_REQ( create_mapping
)
2365 req
->access
= access
;
2366 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
2367 req
->file_handle
= wine_server_obj_handle( file
);
2368 req
->size
= size
? size
->QuadPart
: 0;
2369 req
->protect
= vprot
;
2370 wine_server_add_data( req
, &objattr
, sizeof(objattr
) );
2371 if (objattr
.sd_len
) wine_server_add_data( req
, sd
, objattr
.sd_len
);
2372 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
2373 ret
= wine_server_call( req
);
2374 *handle
= wine_server_ptr_handle( reply
->handle
);
2378 NTDLL_free_struct_sd( sd
);
2384 /***********************************************************************
2385 * NtOpenSection (NTDLL.@)
2386 * ZwOpenSection (NTDLL.@)
2388 NTSTATUS WINAPI
NtOpenSection( HANDLE
*handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
)
2391 DWORD len
= attr
->ObjectName
->Length
;
2393 if (len
> MAX_PATH
*sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
2395 SERVER_START_REQ( open_mapping
)
2397 req
->access
= access
;
2398 req
->attributes
= attr
->Attributes
;
2399 req
->rootdir
= wine_server_obj_handle( attr
->RootDirectory
);
2400 wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
2401 if (!(ret
= wine_server_call( req
))) *handle
= wine_server_ptr_handle( reply
->handle
);
2408 /***********************************************************************
2409 * NtMapViewOfSection (NTDLL.@)
2410 * ZwMapViewOfSection (NTDLL.@)
2412 NTSTATUS WINAPI
NtMapViewOfSection( HANDLE handle
, HANDLE process
, PVOID
*addr_ptr
, ULONG zero_bits
,
2413 SIZE_T commit_size
, const LARGE_INTEGER
*offset_ptr
, SIZE_T
*size_ptr
,
2414 SECTION_INHERIT inherit
, ULONG alloc_type
, ULONG protect
)
2417 mem_size_t full_size
;
2419 SIZE_T size
, mask
= get_mask( zero_bits
);
2420 int unix_handle
= -1, needs_close
;
2421 unsigned int map_vprot
, vprot
;
2423 struct file_view
*view
;
2425 HANDLE dup_mapping
, shared_file
;
2426 LARGE_INTEGER offset
;
2429 offset
.QuadPart
= offset_ptr
? offset_ptr
->QuadPart
: 0;
2431 TRACE("handle=%p process=%p addr=%p off=%x%08x size=%lx access=%x\n",
2432 handle
, process
, *addr_ptr
, offset
.u
.HighPart
, offset
.u
.LowPart
, *size_ptr
, protect
);
2434 /* Check parameters */
2436 if ((offset
.u
.LowPart
& mask
) || (*addr_ptr
&& ((UINT_PTR
)*addr_ptr
& mask
)))
2437 return STATUS_INVALID_PARAMETER
;
2444 case PAGE_READWRITE
:
2445 case PAGE_EXECUTE_READWRITE
:
2446 access
= SECTION_MAP_WRITE
;
2449 case PAGE_WRITECOPY
:
2451 case PAGE_EXECUTE_READ
:
2452 case PAGE_EXECUTE_WRITECOPY
:
2453 access
= SECTION_MAP_READ
;
2456 return STATUS_INVALID_PARAMETER
;
2459 if (process
!= NtCurrentProcess())
2462 apc_result_t result
;
2464 memset( &call
, 0, sizeof(call
) );
2466 call
.map_view
.type
= APC_MAP_VIEW
;
2467 call
.map_view
.handle
= wine_server_obj_handle( handle
);
2468 call
.map_view
.addr
= wine_server_client_ptr( *addr_ptr
);
2469 call
.map_view
.size
= *size_ptr
;
2470 call
.map_view
.offset
= offset
.QuadPart
;
2471 call
.map_view
.zero_bits
= zero_bits
;
2472 call
.map_view
.alloc_type
= alloc_type
;
2473 call
.map_view
.prot
= protect
;
2474 res
= NTDLL_queue_process_apc( process
, &call
, &result
);
2475 if (res
!= STATUS_SUCCESS
) return res
;
2477 if ((NTSTATUS
)result
.map_view
.status
>= 0)
2479 *addr_ptr
= wine_server_get_ptr( result
.map_view
.addr
);
2480 *size_ptr
= result
.map_view
.size
;
2482 return result
.map_view
.status
;
2485 SERVER_START_REQ( get_mapping_info
)
2487 req
->handle
= wine_server_obj_handle( handle
);
2488 req
->access
= access
;
2489 res
= wine_server_call( req
);
2490 map_vprot
= reply
->protect
;
2491 base
= wine_server_get_ptr( reply
->base
);
2492 full_size
= reply
->size
;
2493 header_size
= reply
->header_size
;
2494 dup_mapping
= wine_server_ptr_handle( reply
->mapping
);
2495 shared_file
= wine_server_ptr_handle( reply
->shared_file
);
2496 if ((ULONG_PTR
)base
!= reply
->base
) base
= NULL
;
2499 if (res
) return res
;
2501 if ((res
= server_get_unix_fd( handle
, 0, &unix_handle
, &needs_close
, NULL
, NULL
))) goto done
;
2503 if (map_vprot
& VPROT_IMAGE
)
2506 if (size
!= full_size
) /* truncated */
2508 WARN( "Modules larger than 4Gb (%s) not supported\n", wine_dbgstr_longlong(full_size
) );
2509 res
= STATUS_INVALID_PARAMETER
;
2514 int shared_fd
, shared_needs_close
;
2516 if ((res
= server_get_unix_fd( shared_file
, FILE_READ_DATA
|FILE_WRITE_DATA
,
2517 &shared_fd
, &shared_needs_close
, NULL
, NULL
))) goto done
;
2518 res
= map_image( handle
, unix_handle
, base
, size
, mask
, header_size
,
2519 shared_fd
, dup_mapping
, addr_ptr
);
2520 if (shared_needs_close
) close( shared_fd
);
2521 NtClose( shared_file
);
2525 res
= map_image( handle
, unix_handle
, base
, size
, mask
, header_size
,
2526 -1, dup_mapping
, addr_ptr
);
2528 if (needs_close
) close( unix_handle
);
2529 if (res
>= 0) *size_ptr
= size
;
2533 res
= STATUS_INVALID_PARAMETER
;
2534 if (offset
.QuadPart
>= full_size
) goto done
;
2537 if (*size_ptr
> full_size
- offset
.QuadPart
) goto done
;
2538 size
= ROUND_SIZE( offset
.u
.LowPart
, *size_ptr
);
2539 if (size
< *size_ptr
) goto done
; /* wrap-around */
2543 size
= full_size
- offset
.QuadPart
;
2544 if (size
!= full_size
- offset
.QuadPart
) /* truncated */
2546 WARN( "Files larger than 4Gb (%s) not supported on this platform\n",
2547 wine_dbgstr_longlong(full_size
) );
2552 /* Reserve a properly aligned area */
2554 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2556 get_vprot_flags( protect
, &vprot
);
2557 vprot
|= (map_vprot
& VPROT_COMMITTED
);
2558 res
= map_view( &view
, *addr_ptr
, size
, mask
, FALSE
, vprot
);
2561 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2567 TRACE("handle=%p size=%lx offset=%x%08x\n",
2568 handle
, size
, offset
.u
.HighPart
, offset
.u
.LowPart
);
2570 res
= map_file_into_view( view
, unix_handle
, 0, size
, offset
.QuadPart
, vprot
, !dup_mapping
);
2571 if (res
== STATUS_SUCCESS
)
2573 *addr_ptr
= view
->base
;
2575 view
->mapping
= dup_mapping
;
2576 dup_mapping
= 0; /* don't close it */
2580 ERR( "map_file_into_view %p %lx %x%08x failed\n",
2581 view
->base
, size
, offset
.u
.HighPart
, offset
.u
.LowPart
);
2582 delete_view( view
);
2585 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2588 if (dup_mapping
) NtClose( dup_mapping
);
2589 if (needs_close
) close( unix_handle
);
2594 /***********************************************************************
2595 * NtUnmapViewOfSection (NTDLL.@)
2596 * ZwUnmapViewOfSection (NTDLL.@)
2598 NTSTATUS WINAPI
NtUnmapViewOfSection( HANDLE process
, PVOID addr
)
2600 struct file_view
*view
;
2601 NTSTATUS status
= STATUS_NOT_MAPPED_VIEW
;
2603 void *base
= ROUND_ADDR( addr
, page_mask
);
2605 if (process
!= NtCurrentProcess())
2608 apc_result_t result
;
2610 memset( &call
, 0, sizeof(call
) );
2612 call
.unmap_view
.type
= APC_UNMAP_VIEW
;
2613 call
.unmap_view
.addr
= wine_server_client_ptr( addr
);
2614 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
2615 if (status
== STATUS_SUCCESS
) status
= result
.unmap_view
.status
;
2619 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2620 if ((view
= VIRTUAL_FindView( base
, 0 )) && (base
== view
->base
) && !(view
->protect
& VPROT_VALLOC
))
2622 delete_view( view
);
2623 status
= STATUS_SUCCESS
;
2625 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2630 /***********************************************************************
2631 * NtFlushVirtualMemory (NTDLL.@)
2632 * ZwFlushVirtualMemory (NTDLL.@)
2634 NTSTATUS WINAPI
NtFlushVirtualMemory( HANDLE process
, LPCVOID
*addr_ptr
,
2635 SIZE_T
*size_ptr
, ULONG unknown
)
2637 struct file_view
*view
;
2638 NTSTATUS status
= STATUS_SUCCESS
;
2640 void *addr
= ROUND_ADDR( *addr_ptr
, page_mask
);
2642 if (process
!= NtCurrentProcess())
2645 apc_result_t result
;
2647 memset( &call
, 0, sizeof(call
) );
2649 call
.virtual_flush
.type
= APC_VIRTUAL_FLUSH
;
2650 call
.virtual_flush
.addr
= wine_server_client_ptr( addr
);
2651 call
.virtual_flush
.size
= *size_ptr
;
2652 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
2653 if (status
!= STATUS_SUCCESS
) return status
;
2655 if (result
.virtual_flush
.status
== STATUS_SUCCESS
)
2657 *addr_ptr
= wine_server_get_ptr( result
.virtual_flush
.addr
);
2658 *size_ptr
= result
.virtual_flush
.size
;
2660 return result
.virtual_flush
.status
;
2663 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2664 if (!(view
= VIRTUAL_FindView( addr
, *size_ptr
))) status
= STATUS_INVALID_PARAMETER
;
2667 if (!*size_ptr
) *size_ptr
= view
->size
;
2669 if (msync( addr
, *size_ptr
, MS_SYNC
)) status
= STATUS_NOT_MAPPED_DATA
;
2671 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2676 /***********************************************************************
2677 * NtGetWriteWatch (NTDLL.@)
2678 * ZwGetWriteWatch (NTDLL.@)
2680 NTSTATUS WINAPI
NtGetWriteWatch( HANDLE process
, ULONG flags
, PVOID base
, SIZE_T size
, PVOID
*addresses
,
2681 ULONG_PTR
*count
, ULONG
*granularity
)
2683 struct file_view
*view
;
2684 NTSTATUS status
= STATUS_SUCCESS
;
2687 size
= ROUND_SIZE( base
, size
);
2688 base
= ROUND_ADDR( base
, page_mask
);
2690 if (!count
|| !granularity
) return STATUS_ACCESS_VIOLATION
;
2691 if (!*count
|| !size
) return STATUS_INVALID_PARAMETER
;
2692 if (flags
& ~WRITE_WATCH_FLAG_RESET
) return STATUS_INVALID_PARAMETER
;
2694 if (!addresses
) return STATUS_ACCESS_VIOLATION
;
2696 TRACE( "%p %x %p-%p %p %lu\n", process
, flags
, base
, (char *)base
+ size
,
2697 addresses
, *count
);
2699 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2701 if ((view
= VIRTUAL_FindView( base
, size
)) && (view
->protect
& VPROT_WRITEWATCH
))
2705 char *end
= addr
+ size
;
2707 while (pos
< *count
&& addr
< end
)
2709 BYTE prot
= view
->prot
[(addr
- (char *)view
->base
) >> page_shift
];
2710 if (!(prot
& VPROT_WRITEWATCH
)) addresses
[pos
++] = addr
;
2713 if (flags
& WRITE_WATCH_FLAG_RESET
) reset_write_watches( view
, base
, addr
- (char *)base
);
2715 *granularity
= page_size
;
2717 else status
= STATUS_INVALID_PARAMETER
;
2719 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2724 /***********************************************************************
2725 * NtResetWriteWatch (NTDLL.@)
2726 * ZwResetWriteWatch (NTDLL.@)
2728 NTSTATUS WINAPI
NtResetWriteWatch( HANDLE process
, PVOID base
, SIZE_T size
)
2730 struct file_view
*view
;
2731 NTSTATUS status
= STATUS_SUCCESS
;
2734 size
= ROUND_SIZE( base
, size
);
2735 base
= ROUND_ADDR( base
, page_mask
);
2737 TRACE( "%p %p-%p\n", process
, base
, (char *)base
+ size
);
2739 if (!size
) return STATUS_INVALID_PARAMETER
;
2741 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2743 if ((view
= VIRTUAL_FindView( base
, size
)) && (view
->protect
& VPROT_WRITEWATCH
))
2744 reset_write_watches( view
, base
, size
);
2746 status
= STATUS_INVALID_PARAMETER
;
2748 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2753 /***********************************************************************
2754 * NtReadVirtualMemory (NTDLL.@)
2755 * ZwReadVirtualMemory (NTDLL.@)
2757 NTSTATUS WINAPI
NtReadVirtualMemory( HANDLE process
, const void *addr
, void *buffer
,
2758 SIZE_T size
, SIZE_T
*bytes_read
)
2762 if (virtual_check_buffer_for_write( buffer
, size
))
2764 SERVER_START_REQ( read_process_memory
)
2766 req
->handle
= wine_server_obj_handle( process
);
2767 req
->addr
= wine_server_client_ptr( addr
);
2768 wine_server_set_reply( req
, buffer
, size
);
2769 if ((status
= wine_server_call( req
))) size
= 0;
2775 status
= STATUS_ACCESS_VIOLATION
;
2778 if (bytes_read
) *bytes_read
= size
;
2783 /***********************************************************************
2784 * NtWriteVirtualMemory (NTDLL.@)
2785 * ZwWriteVirtualMemory (NTDLL.@)
2787 NTSTATUS WINAPI
NtWriteVirtualMemory( HANDLE process
, void *addr
, const void *buffer
,
2788 SIZE_T size
, SIZE_T
*bytes_written
)
2792 if (virtual_check_buffer_for_read( buffer
, size
))
2794 SERVER_START_REQ( write_process_memory
)
2796 req
->handle
= wine_server_obj_handle( process
);
2797 req
->addr
= wine_server_client_ptr( addr
);
2798 wine_server_add_data( req
, buffer
, size
);
2799 if ((status
= wine_server_call( req
))) size
= 0;
2805 status
= STATUS_PARTIAL_COPY
;
2808 if (bytes_written
) *bytes_written
= size
;
2813 /***********************************************************************
2814 * NtAreMappedFilesTheSame (NTDLL.@)
2815 * ZwAreMappedFilesTheSame (NTDLL.@)
2817 NTSTATUS WINAPI
NtAreMappedFilesTheSame(PVOID addr1
, PVOID addr2
)
2819 struct file_view
*view1
, *view2
;
2820 struct stat st1
, st2
;
2824 TRACE("%p %p\n", addr1
, addr2
);
2826 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2828 view1
= VIRTUAL_FindView( addr1
, 0 );
2829 view2
= VIRTUAL_FindView( addr2
, 0 );
2831 if (!view1
|| !view2
)
2832 status
= STATUS_INVALID_ADDRESS
;
2833 else if ((view1
->protect
& VPROT_VALLOC
) || (view2
->protect
& VPROT_VALLOC
))
2834 status
= STATUS_CONFLICTING_ADDRESSES
;
2835 else if (!(view1
->protect
& VPROT_IMAGE
) || !(view2
->protect
& VPROT_IMAGE
))
2836 status
= STATUS_NOT_SAME_DEVICE
;
2837 else if (view1
== view2
)
2838 status
= STATUS_SUCCESS
;
2839 else if (!stat_mapping_file( view1
, &st1
) && !stat_mapping_file( view2
, &st2
) &&
2840 st1
.st_dev
== st2
.st_dev
&& st1
.st_ino
== st2
.st_ino
)
2841 status
= STATUS_SUCCESS
;
2843 status
= STATUS_NOT_SAME_DEVICE
;
2845 server_leave_uninterrupted_section( &csVirtual
, &sigset
);