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 static void *address_space_start
= (void *)0x110000; /* keep DOS area clear */
126 #elif defined(__x86_64__)
127 # define page_mask 0xfff
128 # define page_shift 12
129 # define page_size 0x1000
130 static void *address_space_limit
= (void *)0x7fffffff0000;
131 static void *user_space_limit
= (void *)0x7fffffff0000;
132 static void *working_set_limit
= (void *)0x7fffffff0000;
133 static void *address_space_start
= (void *)0x10000;
135 static UINT page_shift
;
136 static UINT_PTR page_size
;
137 static UINT_PTR page_mask
;
138 static void *address_space_limit
;
139 static void *user_space_limit
;
140 static void *working_set_limit
;
141 static void *address_space_start
= (void *)0x10000;
142 #endif /* __i386__ */
144 #define ROUND_ADDR(addr,mask) \
145 ((void *)((UINT_PTR)(addr) & ~(UINT_PTR)(mask)))
147 #define ROUND_SIZE(addr,size) \
148 (((SIZE_T)(size) + ((UINT_PTR)(addr) & page_mask) + page_mask) & ~page_mask)
150 #define VIRTUAL_DEBUG_DUMP_VIEW(view) \
151 do { if (TRACE_ON(virtual)) VIRTUAL_DumpView(view); } while (0)
153 #define VIRTUAL_HEAP_SIZE (4*1024*1024)
155 static HANDLE virtual_heap
;
156 static void *preload_reserve_start
;
157 static void *preload_reserve_end
;
158 static int use_locks
;
159 static int force_exec_prot
; /* whether to force PROT_EXEC on all PROT_READ mmaps */
162 /***********************************************************************
165 static const char *VIRTUAL_GetProtStr( BYTE prot
)
167 static char buffer
[6];
168 buffer
[0] = (prot
& VPROT_COMMITTED
) ? 'c' : '-';
169 buffer
[1] = (prot
& VPROT_GUARD
) ? 'g' : ((prot
& VPROT_WRITEWATCH
) ? 'H' : '-');
170 buffer
[2] = (prot
& VPROT_READ
) ? 'r' : '-';
171 buffer
[3] = (prot
& VPROT_WRITECOPY
) ? 'W' : ((prot
& VPROT_WRITE
) ? 'w' : '-');
172 buffer
[4] = (prot
& VPROT_EXEC
) ? 'x' : '-';
178 /***********************************************************************
179 * VIRTUAL_GetUnixProt
181 * Convert page protections to protection for mmap/mprotect.
183 static int VIRTUAL_GetUnixProt( BYTE vprot
)
186 if ((vprot
& VPROT_COMMITTED
) && !(vprot
& VPROT_GUARD
))
188 if (vprot
& VPROT_READ
) prot
|= PROT_READ
;
189 if (vprot
& VPROT_WRITE
) prot
|= PROT_WRITE
;
190 if (vprot
& VPROT_WRITECOPY
) prot
|= PROT_WRITE
;
191 if (vprot
& VPROT_EXEC
) prot
|= PROT_EXEC
;
192 if (vprot
& VPROT_WRITEWATCH
) prot
&= ~PROT_WRITE
;
194 if (!prot
) prot
= PROT_NONE
;
199 /***********************************************************************
202 static void VIRTUAL_DumpView( struct file_view
*view
)
205 char *addr
= view
->base
;
206 BYTE prot
= view
->prot
[0];
208 TRACE( "View: %p - %p", addr
, addr
+ view
->size
- 1 );
209 if (view
->protect
& VPROT_SYSTEM
)
210 TRACE( " (system)\n" );
211 else if (view
->protect
& VPROT_VALLOC
)
212 TRACE( " (valloc)\n" );
213 else if (view
->mapping
)
214 TRACE( " %p\n", view
->mapping
);
216 TRACE( " (anonymous)\n");
218 for (count
= i
= 1; i
< view
->size
>> page_shift
; i
++, count
++)
220 if (view
->prot
[i
] == prot
) continue;
221 TRACE( " %p - %p %s\n",
222 addr
, addr
+ (count
<< page_shift
) - 1, VIRTUAL_GetProtStr(prot
) );
223 addr
+= (count
<< page_shift
);
224 prot
= view
->prot
[i
];
228 TRACE( " %p - %p %s\n",
229 addr
, addr
+ (count
<< page_shift
) - 1, VIRTUAL_GetProtStr(prot
) );
233 /***********************************************************************
237 static void VIRTUAL_Dump(void)
240 struct file_view
*view
;
242 TRACE( "Dump of all virtual memory views:\n" );
243 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
244 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
246 VIRTUAL_DumpView( view
);
248 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
253 /***********************************************************************
256 * Find the view containing a given address. The csVirtual section must be held by caller.
265 static struct file_view
*VIRTUAL_FindView( const void *addr
, size_t size
)
267 struct file_view
*view
;
269 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
271 if (view
->base
> addr
) break; /* no matching view */
272 if ((const char *)view
->base
+ view
->size
<= (const char *)addr
) continue;
273 if ((const char *)view
->base
+ view
->size
< (const char *)addr
+ size
) break; /* size too large */
274 if ((const char *)addr
+ size
< (const char *)addr
) break; /* overflow */
281 /***********************************************************************
284 static inline UINT_PTR
get_mask( ULONG zero_bits
)
286 if (!zero_bits
) return 0xffff; /* allocations are aligned to 64K by default */
287 if (zero_bits
< page_shift
) zero_bits
= page_shift
;
288 return (1 << zero_bits
) - 1;
292 /***********************************************************************
295 * Find the first view overlapping at least part of the specified range.
296 * The csVirtual section must be held by caller.
298 static struct file_view
*find_view_range( const void *addr
, size_t size
)
300 struct file_view
*view
;
302 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
304 if ((const char *)view
->base
>= (const char *)addr
+ size
) break;
305 if ((const char *)view
->base
+ view
->size
> (const char *)addr
) return view
;
311 /***********************************************************************
314 * Find a free area between views inside the specified range.
315 * The csVirtual section must be held by caller.
317 static void *find_free_area( void *base
, void *end
, size_t size
, size_t mask
, int top_down
)
324 start
= ROUND_ADDR( (char *)end
- size
, mask
);
325 if (start
>= end
|| start
< base
) return NULL
;
327 for (ptr
= views_list
.prev
; ptr
!= &views_list
; ptr
= ptr
->prev
)
329 struct file_view
*view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
331 if ((char *)view
->base
+ view
->size
<= (char *)start
) break;
332 if ((char *)view
->base
>= (char *)start
+ size
) continue;
333 start
= ROUND_ADDR( (char *)view
->base
- size
, mask
);
334 /* stop if remaining space is not large enough */
335 if (!start
|| start
>= end
|| start
< base
) return NULL
;
340 start
= ROUND_ADDR( (char *)base
+ mask
, mask
);
341 if (start
>= end
|| (char *)end
- (char *)start
< size
) return NULL
;
343 for (ptr
= views_list
.next
; ptr
!= &views_list
; ptr
= ptr
->next
)
345 struct file_view
*view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
347 if ((char *)view
->base
>= (char *)start
+ size
) break;
348 if ((char *)view
->base
+ view
->size
<= (char *)start
) continue;
349 start
= ROUND_ADDR( (char *)view
->base
+ view
->size
+ mask
, mask
);
350 /* stop if remaining space is not large enough */
351 if (!start
|| start
>= end
|| (char *)end
- (char *)start
< size
) return NULL
;
358 /***********************************************************************
361 * Add a reserved area to the list maintained by libwine.
362 * The csVirtual section must be held by caller.
364 static void add_reserved_area( void *addr
, size_t size
)
366 TRACE( "adding %p-%p\n", addr
, (char *)addr
+ size
);
368 if (addr
< user_space_limit
)
370 /* unmap the part of the area that is below the limit */
371 assert( (char *)addr
+ size
> (char *)user_space_limit
);
372 munmap( addr
, (char *)user_space_limit
- (char *)addr
);
373 size
-= (char *)user_space_limit
- (char *)addr
;
374 addr
= user_space_limit
;
376 /* blow away existing mappings */
377 wine_anon_mmap( addr
, size
, PROT_NONE
, MAP_NORESERVE
| MAP_FIXED
);
378 wine_mmap_add_reserved_area( addr
, size
);
382 /***********************************************************************
383 * remove_reserved_area
385 * Remove a reserved area from the list maintained by libwine.
386 * The csVirtual section must be held by caller.
388 static void remove_reserved_area( void *addr
, size_t size
)
390 struct file_view
*view
;
392 TRACE( "removing %p-%p\n", addr
, (char *)addr
+ size
);
393 wine_mmap_remove_reserved_area( addr
, size
, 0 );
395 /* unmap areas not covered by an existing view */
396 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
398 if ((char *)view
->base
>= (char *)addr
+ size
)
400 munmap( addr
, size
);
403 if ((char *)view
->base
+ view
->size
<= (char *)addr
) continue;
404 if (view
->base
> addr
) munmap( addr
, (char *)view
->base
- (char *)addr
);
405 if ((char *)view
->base
+ view
->size
> (char *)addr
+ size
) break;
406 size
= (char *)addr
+ size
- ((char *)view
->base
+ view
->size
);
407 addr
= (char *)view
->base
+ view
->size
;
412 /***********************************************************************
415 * Check if an address range goes beyond a given limit.
417 static inline int is_beyond_limit( const void *addr
, size_t size
, const void *limit
)
419 return (addr
>= limit
|| (const char *)addr
+ size
> (const char *)limit
);
423 /***********************************************************************
426 * Unmap an area, or simply replace it by an empty mapping if it is
427 * in a reserved area. The csVirtual section must be held by caller.
429 static inline void unmap_area( void *addr
, size_t size
)
431 if (wine_mmap_is_in_reserved_area( addr
, size
))
432 wine_anon_mmap( addr
, size
, PROT_NONE
, MAP_NORESERVE
| MAP_FIXED
);
433 else if (is_beyond_limit( addr
, size
, user_space_limit
))
434 add_reserved_area( addr
, size
);
436 munmap( addr
, size
);
440 /***********************************************************************
443 * Deletes a view. The csVirtual section must be held by caller.
445 static void delete_view( struct file_view
*view
) /* [in] View */
447 if (!(view
->protect
& VPROT_SYSTEM
)) unmap_area( view
->base
, view
->size
);
448 list_remove( &view
->entry
);
449 if (view
->mapping
) close_handle( view
->mapping
);
450 RtlFreeHeap( virtual_heap
, 0, view
);
454 /***********************************************************************
457 * Create a view. The csVirtual section must be held by caller.
459 static NTSTATUS
create_view( struct file_view
**view_ret
, void *base
, size_t size
, unsigned int vprot
)
461 struct file_view
*view
;
463 int unix_prot
= VIRTUAL_GetUnixProt( vprot
);
465 assert( !((UINT_PTR
)base
& page_mask
) );
466 assert( !(size
& page_mask
) );
468 /* Create the view structure */
470 if (!(view
= RtlAllocateHeap( virtual_heap
, 0, sizeof(*view
) + (size
>> page_shift
) - 1 )))
472 FIXME( "out of memory in virtual heap for %p-%p\n", base
, (char *)base
+ size
);
473 return STATUS_NO_MEMORY
;
479 view
->protect
= vprot
;
480 memset( view
->prot
, vprot
, size
>> page_shift
);
482 /* Insert it in the linked list */
484 LIST_FOR_EACH( ptr
, &views_list
)
486 struct file_view
*next
= LIST_ENTRY( ptr
, struct file_view
, entry
);
487 if (next
->base
> base
) break;
489 list_add_before( ptr
, &view
->entry
);
491 /* Check for overlapping views. This can happen if the previous view
492 * was a system view that got unmapped behind our back. In that case
493 * we recover by simply deleting it. */
495 if ((ptr
= list_prev( &views_list
, &view
->entry
)) != NULL
)
497 struct file_view
*prev
= LIST_ENTRY( ptr
, struct file_view
, entry
);
498 if ((char *)prev
->base
+ prev
->size
> (char *)base
)
500 TRACE( "overlapping prev view %p-%p for %p-%p\n",
501 prev
->base
, (char *)prev
->base
+ prev
->size
,
502 base
, (char *)base
+ view
->size
);
503 assert( prev
->protect
& VPROT_SYSTEM
);
507 if ((ptr
= list_next( &views_list
, &view
->entry
)) != NULL
)
509 struct file_view
*next
= LIST_ENTRY( ptr
, struct file_view
, entry
);
510 if ((char *)base
+ view
->size
> (char *)next
->base
)
512 TRACE( "overlapping next view %p-%p for %p-%p\n",
513 next
->base
, (char *)next
->base
+ next
->size
,
514 base
, (char *)base
+ view
->size
);
515 assert( next
->protect
& VPROT_SYSTEM
);
521 VIRTUAL_DEBUG_DUMP_VIEW( view
);
523 if (force_exec_prot
&& !(vprot
& VPROT_NOEXEC
) && (unix_prot
& PROT_READ
) && !(unix_prot
& PROT_EXEC
))
525 TRACE( "forcing exec permission on %p-%p\n", base
, (char *)base
+ size
- 1 );
526 mprotect( base
, size
, unix_prot
| PROT_EXEC
);
528 return STATUS_SUCCESS
;
532 /***********************************************************************
533 * VIRTUAL_GetWin32Prot
535 * Convert page protections to Win32 flags.
537 static DWORD
VIRTUAL_GetWin32Prot( BYTE vprot
)
539 DWORD ret
= VIRTUAL_Win32Flags
[vprot
& 0x0f];
540 if (vprot
& VPROT_NOCACHE
) ret
|= PAGE_NOCACHE
;
541 if (vprot
& VPROT_GUARD
) ret
|= PAGE_GUARD
;
546 /***********************************************************************
549 * Build page protections from Win32 flags.
552 * protect [I] Win32 protection flags
555 * Value of page protection flags
557 static NTSTATUS
get_vprot_flags( DWORD protect
, unsigned int *vprot
)
559 switch(protect
& 0xff)
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
:
577 *vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITE
;
579 case PAGE_EXECUTE_WRITECOPY
:
580 *vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITECOPY
;
586 return STATUS_INVALID_PARAMETER
;
588 if (protect
& PAGE_GUARD
) *vprot
|= VPROT_GUARD
;
589 if (protect
& PAGE_NOCACHE
) *vprot
|= VPROT_NOCACHE
;
590 return STATUS_SUCCESS
;
594 /***********************************************************************
597 * Change the protection of a range of pages.
603 static BOOL
VIRTUAL_SetProt( struct file_view
*view
, /* [in] Pointer to view */
604 void *base
, /* [in] Starting address */
605 size_t size
, /* [in] Size in bytes */
606 BYTE vprot
) /* [in] Protections to use */
608 int unix_prot
= VIRTUAL_GetUnixProt(vprot
);
609 BYTE
*p
= view
->prot
+ (((char *)base
- (char *)view
->base
) >> page_shift
);
612 base
, (char *)base
+ size
- 1, VIRTUAL_GetProtStr( vprot
) );
614 if (view
->protect
& VPROT_WRITEWATCH
)
616 /* each page may need different protections depending on write watch flag */
621 p
[0] = vprot
| (p
[0] & VPROT_WRITEWATCH
);
622 unix_prot
= VIRTUAL_GetUnixProt( p
[0] );
623 for (count
= i
= 1; i
< size
>> page_shift
; i
++, count
++)
625 p
[i
] = vprot
| (p
[i
] & VPROT_WRITEWATCH
);
626 prot
= VIRTUAL_GetUnixProt( p
[i
] );
627 if (prot
== unix_prot
) continue;
628 mprotect( addr
, count
<< page_shift
, unix_prot
);
629 addr
+= count
<< page_shift
;
633 if (count
) mprotect( addr
, count
<< page_shift
, unix_prot
);
634 VIRTUAL_DEBUG_DUMP_VIEW( view
);
638 /* if setting stack guard pages, store the permissions first, as the guard may be
639 * triggered at any point after mprotect and change the permissions again */
640 if ((vprot
& VPROT_GUARD
) &&
641 (base
>= NtCurrentTeb()->DeallocationStack
) &&
642 (base
< NtCurrentTeb()->Tib
.StackBase
))
644 memset( p
, vprot
, size
>> page_shift
);
645 mprotect( base
, size
, unix_prot
);
646 VIRTUAL_DEBUG_DUMP_VIEW( view
);
650 if (force_exec_prot
&& !(view
->protect
& VPROT_NOEXEC
) &&
651 (unix_prot
& PROT_READ
) && !(unix_prot
& PROT_EXEC
))
653 TRACE( "forcing exec permission on %p-%p\n", base
, (char *)base
+ size
- 1 );
654 if (!mprotect( base
, size
, unix_prot
| PROT_EXEC
)) goto done
;
655 /* exec + write may legitimately fail, in that case fall back to write only */
656 if (!(unix_prot
& PROT_WRITE
)) return FALSE
;
659 if (mprotect( base
, size
, unix_prot
)) return FALSE
; /* FIXME: last error */
662 memset( p
, vprot
, size
>> page_shift
);
663 VIRTUAL_DEBUG_DUMP_VIEW( view
);
668 /***********************************************************************
669 * reset_write_watches
671 * Reset write watches in a memory range.
673 static void reset_write_watches( struct file_view
*view
, void *base
, SIZE_T size
)
678 BYTE
*p
= view
->prot
+ ((addr
- (char *)view
->base
) >> page_shift
);
680 p
[0] |= VPROT_WRITEWATCH
;
681 unix_prot
= VIRTUAL_GetUnixProt( p
[0] );
682 for (count
= i
= 1; i
< size
>> page_shift
; i
++, count
++)
684 p
[i
] |= VPROT_WRITEWATCH
;
685 prot
= VIRTUAL_GetUnixProt( p
[i
] );
686 if (prot
== unix_prot
) continue;
687 mprotect( addr
, count
<< page_shift
, unix_prot
);
688 addr
+= count
<< page_shift
;
692 if (count
) mprotect( addr
, count
<< page_shift
, unix_prot
);
696 /***********************************************************************
699 * Release the extra memory while keeping the range starting on the granularity boundary.
701 static inline void *unmap_extra_space( void *ptr
, size_t total_size
, size_t wanted_size
, size_t mask
)
703 if ((ULONG_PTR
)ptr
& mask
)
705 size_t extra
= mask
+ 1 - ((ULONG_PTR
)ptr
& mask
);
706 munmap( ptr
, extra
);
707 ptr
= (char *)ptr
+ extra
;
710 if (total_size
> wanted_size
)
711 munmap( (char *)ptr
+ wanted_size
, total_size
- wanted_size
);
725 /***********************************************************************
726 * alloc_reserved_area_callback
728 * Try to map some space inside a reserved area. Callback for wine_mmap_enum_reserved_areas.
730 static int alloc_reserved_area_callback( void *start
, size_t size
, void *arg
)
732 struct alloc_area
*alloc
= arg
;
733 void *end
= (char *)start
+ size
;
735 if (start
< address_space_start
) start
= address_space_start
;
736 if (is_beyond_limit( start
, size
, alloc
->limit
)) end
= alloc
->limit
;
737 if (start
>= end
) return 0;
739 /* make sure we don't touch the preloader reserved range */
740 if (preload_reserve_end
>= start
)
742 if (preload_reserve_end
>= end
)
744 if (preload_reserve_start
<= start
) return 0; /* no space in that area */
745 if (preload_reserve_start
< end
) end
= preload_reserve_start
;
747 else if (preload_reserve_start
<= start
) start
= preload_reserve_end
;
750 /* range is split in two by the preloader reservation, try first part */
751 if ((alloc
->result
= find_free_area( start
, preload_reserve_start
, alloc
->size
,
752 alloc
->mask
, alloc
->top_down
)))
754 /* then fall through to try second part */
755 start
= preload_reserve_end
;
758 if ((alloc
->result
= find_free_area( start
, end
, alloc
->size
, alloc
->mask
, alloc
->top_down
)))
765 /***********************************************************************
768 * Create a view and mmap the corresponding memory area.
769 * The csVirtual section must be held by caller.
771 static NTSTATUS
map_view( struct file_view
**view_ret
, void *base
, size_t size
, size_t mask
,
772 int top_down
, unsigned int vprot
)
779 if (is_beyond_limit( base
, size
, address_space_limit
))
780 return STATUS_WORKING_SET_LIMIT_RANGE
;
782 switch (wine_mmap_is_in_reserved_area( base
, size
))
784 case -1: /* partially in a reserved area */
785 return STATUS_CONFLICTING_ADDRESSES
;
787 case 0: /* not in a reserved area, do a normal allocation */
788 if ((ptr
= wine_anon_mmap( base
, size
, VIRTUAL_GetUnixProt(vprot
), 0 )) == (void *)-1)
790 if (errno
== ENOMEM
) return STATUS_NO_MEMORY
;
791 return STATUS_INVALID_PARAMETER
;
795 /* We couldn't get the address we wanted */
796 if (is_beyond_limit( ptr
, size
, user_space_limit
)) add_reserved_area( ptr
, size
);
797 else munmap( ptr
, size
);
798 return STATUS_CONFLICTING_ADDRESSES
;
803 case 1: /* in a reserved area, make sure the address is available */
804 if (find_view_range( base
, size
)) return STATUS_CONFLICTING_ADDRESSES
;
805 /* replace the reserved area by our mapping */
806 if ((ptr
= wine_anon_mmap( base
, size
, VIRTUAL_GetUnixProt(vprot
), MAP_FIXED
)) != base
)
807 return STATUS_INVALID_PARAMETER
;
810 if (is_beyond_limit( ptr
, size
, working_set_limit
)) working_set_limit
= address_space_limit
;
814 size_t view_size
= size
+ mask
+ 1;
815 struct alloc_area alloc
;
819 alloc
.top_down
= top_down
;
820 alloc
.limit
= user_space_limit
;
821 if (wine_mmap_enum_reserved_areas( alloc_reserved_area_callback
, &alloc
, top_down
))
824 TRACE( "got mem in reserved area %p-%p\n", ptr
, (char *)ptr
+ size
);
825 if (wine_anon_mmap( ptr
, size
, VIRTUAL_GetUnixProt(vprot
), MAP_FIXED
) != ptr
)
826 return STATUS_INVALID_PARAMETER
;
832 if ((ptr
= wine_anon_mmap( NULL
, view_size
, VIRTUAL_GetUnixProt(vprot
), 0 )) == (void *)-1)
834 if (errno
== ENOMEM
) return STATUS_NO_MEMORY
;
835 return STATUS_INVALID_PARAMETER
;
837 TRACE( "got mem with anon mmap %p-%p\n", ptr
, (char *)ptr
+ size
);
838 /* if we got something beyond the user limit, unmap it and retry */
839 if (is_beyond_limit( ptr
, view_size
, user_space_limit
)) add_reserved_area( ptr
, view_size
);
842 ptr
= unmap_extra_space( ptr
, view_size
, size
, mask
);
845 status
= create_view( view_ret
, ptr
, size
, vprot
);
846 if (status
!= STATUS_SUCCESS
) unmap_area( ptr
, size
);
851 /***********************************************************************
854 * Wrapper for mmap() to map a file into a view, falling back to read if mmap fails.
855 * The csVirtual section must be held by caller.
857 static NTSTATUS
map_file_into_view( struct file_view
*view
, int fd
, size_t start
, size_t size
,
858 off_t offset
, unsigned int vprot
, BOOL removable
)
861 int prot
= VIRTUAL_GetUnixProt( vprot
| VPROT_COMMITTED
/* make sure it is accessible */ );
862 BOOL shared_write
= (vprot
& VPROT_WRITE
) != 0;
864 assert( start
< view
->size
);
865 assert( start
+ size
<= view
->size
);
867 if (force_exec_prot
&& !(vprot
& VPROT_NOEXEC
) && (vprot
& VPROT_READ
))
869 TRACE( "forcing exec permission on mapping %p-%p\n",
870 (char *)view
->base
+ start
, (char *)view
->base
+ start
+ size
- 1 );
874 /* only try mmap if media is not removable (or if we require write access) */
875 if (!removable
|| shared_write
)
877 int flags
= MAP_FIXED
| (shared_write
? MAP_SHARED
: MAP_PRIVATE
);
879 if (mmap( (char *)view
->base
+ start
, size
, prot
, flags
, fd
, offset
) != (void *)-1)
882 if ((errno
== EPERM
) && (prot
& PROT_EXEC
))
883 ERR( "failed to set %08x protection on file map, noexec filesystem?\n", prot
);
885 /* mmap() failed; if this is because the file offset is not */
886 /* page-aligned (EINVAL), or because the underlying filesystem */
887 /* does not support mmap() (ENOEXEC,ENODEV), we do it by hand. */
888 if ((errno
!= ENOEXEC
) && (errno
!= EINVAL
) && (errno
!= ENODEV
)) return FILE_GetNtStatus();
889 if (shared_write
) /* we cannot fake shared write mappings */
891 if (errno
== EINVAL
) return STATUS_INVALID_PARAMETER
;
892 ERR( "shared writable mmap not supported, broken filesystem?\n" );
893 return STATUS_NOT_SUPPORTED
;
897 /* Reserve the memory with an anonymous mmap */
898 ptr
= wine_anon_mmap( (char *)view
->base
+ start
, size
, PROT_READ
| PROT_WRITE
, MAP_FIXED
);
899 if (ptr
== (void *)-1) return FILE_GetNtStatus();
900 /* Now read in the file */
901 pread( fd
, ptr
, size
, offset
);
902 if (prot
!= (PROT_READ
|PROT_WRITE
)) mprotect( ptr
, size
, prot
); /* Set the right protection */
904 memset( view
->prot
+ (start
>> page_shift
), vprot
, ROUND_SIZE(start
,size
) >> page_shift
);
905 return STATUS_SUCCESS
;
909 /***********************************************************************
912 * Get the size of the committed range starting at base.
913 * Also return the protections for the first page.
915 static SIZE_T
get_committed_size( struct file_view
*view
, void *base
, BYTE
*vprot
)
919 start
= ((char *)base
- (char *)view
->base
) >> page_shift
;
920 *vprot
= view
->prot
[start
];
922 if (view
->mapping
&& !(view
->protect
& VPROT_COMMITTED
))
925 SERVER_START_REQ( get_mapping_committed_range
)
927 req
->handle
= wine_server_obj_handle( view
->mapping
);
928 req
->offset
= start
<< page_shift
;
929 if (!wine_server_call( req
))
932 if (reply
->committed
)
934 *vprot
|= VPROT_COMMITTED
;
935 for (i
= 0; i
< ret
>> page_shift
; i
++) view
->prot
[start
+i
] |= VPROT_COMMITTED
;
942 for (i
= start
+ 1; i
< view
->size
>> page_shift
; i
++)
943 if ((*vprot
^ view
->prot
[i
]) & VPROT_COMMITTED
) break;
944 return (i
- start
) << page_shift
;
948 /***********************************************************************
951 * Decommit some pages of a given view.
952 * The csVirtual section must be held by caller.
954 static NTSTATUS
decommit_pages( struct file_view
*view
, size_t start
, size_t size
)
956 if (wine_anon_mmap( (char *)view
->base
+ start
, size
, PROT_NONE
, MAP_FIXED
) != (void *)-1)
958 BYTE
*p
= view
->prot
+ (start
>> page_shift
);
960 while (size
--) *p
++ &= ~VPROT_COMMITTED
;
961 return STATUS_SUCCESS
;
963 return FILE_GetNtStatus();
967 /***********************************************************************
968 * allocate_dos_memory
970 * Allocate the DOS memory range.
972 static NTSTATUS
allocate_dos_memory( struct file_view
**view
, unsigned int vprot
)
976 void * const low_64k
= (void *)0x10000;
977 const size_t dosmem_size
= 0x110000;
978 int unix_prot
= VIRTUAL_GetUnixProt( vprot
);
981 /* check for existing view */
983 if ((ptr
= list_head( &views_list
)))
985 struct file_view
*first_view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
986 if (first_view
->base
< (void *)dosmem_size
) return STATUS_CONFLICTING_ADDRESSES
;
989 /* check without the first 64K */
991 if (wine_mmap_is_in_reserved_area( low_64k
, dosmem_size
- 0x10000 ) != 1)
993 addr
= wine_anon_mmap( low_64k
, dosmem_size
- 0x10000, unix_prot
, 0 );
996 if (addr
!= (void *)-1) munmap( addr
, dosmem_size
- 0x10000 );
997 return map_view( view
, NULL
, dosmem_size
, 0xffff, 0, vprot
);
1001 /* now try to allocate the low 64K too */
1003 if (wine_mmap_is_in_reserved_area( NULL
, 0x10000 ) != 1)
1005 addr
= wine_anon_mmap( (void *)page_size
, 0x10000 - page_size
, unix_prot
, 0 );
1006 if (addr
== (void *)page_size
)
1008 if (!wine_anon_mmap( NULL
, page_size
, unix_prot
, MAP_FIXED
))
1011 TRACE( "successfully mapped low 64K range\n" );
1013 else TRACE( "failed to map page 0\n" );
1017 if (addr
!= (void *)-1) munmap( addr
, 0x10000 - page_size
);
1019 TRACE( "failed to map low 64K range\n" );
1023 /* now reserve the whole range */
1025 size
= (char *)dosmem_size
- (char *)addr
;
1026 wine_anon_mmap( addr
, size
, unix_prot
, MAP_FIXED
);
1027 return create_view( view
, addr
, size
, vprot
);
1031 /***********************************************************************
1034 * Stat the underlying file for a memory view.
1036 static NTSTATUS
stat_mapping_file( struct file_view
*view
, struct stat
*st
)
1039 int unix_fd
, needs_close
;
1041 if (!view
->mapping
) return STATUS_NOT_MAPPED_VIEW
;
1042 if (!(status
= server_get_unix_fd( view
->mapping
, 0, &unix_fd
, &needs_close
, NULL
, NULL
)))
1044 if (fstat( unix_fd
, st
) == -1) status
= FILE_GetNtStatus();
1045 if (needs_close
) close( unix_fd
);
1051 /***********************************************************************
1054 * Map an executable (PE format) image into memory.
1056 static NTSTATUS
map_image( HANDLE hmapping
, int fd
, char *base
, SIZE_T total_size
, SIZE_T mask
,
1057 SIZE_T header_size
, int shared_fd
, HANDLE dup_mapping
, PVOID
*addr_ptr
)
1059 IMAGE_DOS_HEADER
*dos
;
1060 IMAGE_NT_HEADERS
*nt
;
1061 IMAGE_SECTION_HEADER
*sec
;
1062 IMAGE_DATA_DIRECTORY
*imports
;
1063 NTSTATUS status
= STATUS_CONFLICTING_ADDRESSES
;
1068 struct file_view
*view
= NULL
;
1069 char *ptr
, *header_end
;
1072 /* zero-map the whole range */
1074 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1076 if (base
>= (char *)address_space_start
) /* make sure the DOS area remains free */
1077 status
= map_view( &view
, base
, total_size
, mask
, FALSE
,
1078 VPROT_COMMITTED
| VPROT_READ
| VPROT_EXEC
| VPROT_WRITECOPY
| VPROT_IMAGE
);
1080 if (status
!= STATUS_SUCCESS
)
1081 status
= map_view( &view
, NULL
, total_size
, mask
, FALSE
,
1082 VPROT_COMMITTED
| VPROT_READ
| VPROT_EXEC
| VPROT_WRITECOPY
| VPROT_IMAGE
);
1084 if (status
!= STATUS_SUCCESS
) goto error
;
1087 TRACE_(module
)( "mapped PE file at %p-%p\n", ptr
, ptr
+ total_size
);
1089 /* map the header */
1091 if (fstat( fd
, &st
) == -1)
1093 status
= FILE_GetNtStatus();
1096 status
= STATUS_INVALID_IMAGE_FORMAT
; /* generic error */
1097 if (!st
.st_size
) goto error
;
1098 header_size
= min( header_size
, st
.st_size
);
1099 if (map_file_into_view( view
, fd
, 0, header_size
, 0, VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
,
1100 !dup_mapping
) != STATUS_SUCCESS
) goto error
;
1101 dos
= (IMAGE_DOS_HEADER
*)ptr
;
1102 nt
= (IMAGE_NT_HEADERS
*)(ptr
+ dos
->e_lfanew
);
1103 header_end
= ptr
+ ROUND_SIZE( 0, header_size
);
1104 memset( ptr
+ header_size
, 0, header_end
- (ptr
+ header_size
) );
1105 if ((char *)(nt
+ 1) > header_end
) goto error
;
1106 sec
= (IMAGE_SECTION_HEADER
*)((char*)&nt
->OptionalHeader
+nt
->FileHeader
.SizeOfOptionalHeader
);
1107 if ((char *)(sec
+ nt
->FileHeader
.NumberOfSections
) > header_end
) goto error
;
1109 imports
= nt
->OptionalHeader
.DataDirectory
+ IMAGE_DIRECTORY_ENTRY_IMPORT
;
1110 if (!imports
->Size
|| !imports
->VirtualAddress
) imports
= NULL
;
1112 /* check the architecture */
1115 if (nt
->FileHeader
.Machine
!= IMAGE_FILE_MACHINE_AMD64
)
1116 #elif defined(__ARMEL__)
1117 if (nt
->FileHeader
.Machine
!= IMAGE_FILE_MACHINE_ARM
&&
1118 nt
->FileHeader
.Machine
!= IMAGE_FILE_MACHINE_THUMB
)
1120 if (nt
->FileHeader
.Machine
!= IMAGE_FILE_MACHINE_I386
)
1123 MESSAGE("Trying to load PE image for unsupported architecture (");
1124 switch (nt
->FileHeader
.Machine
)
1126 case IMAGE_FILE_MACHINE_UNKNOWN
: MESSAGE("Unknown"); break;
1127 case IMAGE_FILE_MACHINE_I860
: MESSAGE("I860"); break;
1128 case IMAGE_FILE_MACHINE_I386
: MESSAGE("I386"); break;
1129 case IMAGE_FILE_MACHINE_R3000
: MESSAGE("R3000"); break;
1130 case IMAGE_FILE_MACHINE_R4000
: MESSAGE("R4000"); break;
1131 case IMAGE_FILE_MACHINE_R10000
: MESSAGE("R10000"); break;
1132 case IMAGE_FILE_MACHINE_ALPHA
: MESSAGE("Alpha"); break;
1133 case IMAGE_FILE_MACHINE_POWERPC
: MESSAGE("PowerPC"); break;
1134 case IMAGE_FILE_MACHINE_IA64
: MESSAGE("IA-64"); break;
1135 case IMAGE_FILE_MACHINE_ALPHA64
: MESSAGE("Alpha-64"); break;
1136 case IMAGE_FILE_MACHINE_AMD64
: MESSAGE("AMD-64"); break;
1137 case IMAGE_FILE_MACHINE_ARM
: MESSAGE("ARM"); break;
1138 case IMAGE_FILE_MACHINE_THUMB
: MESSAGE("ARM Thumb"); break;
1139 case IMAGE_FILE_MACHINE_SPARC
: MESSAGE("SPARC"); break;
1140 default: MESSAGE("Unknown-%04x", nt
->FileHeader
.Machine
); break;
1146 /* check for non page-aligned binary */
1148 if (nt
->OptionalHeader
.SectionAlignment
<= page_mask
)
1150 /* unaligned sections, this happens for native subsystem binaries */
1151 /* in that case Windows simply maps in the whole file */
1153 if (map_file_into_view( view
, fd
, 0, total_size
, 0, VPROT_COMMITTED
| VPROT_READ
,
1154 !dup_mapping
) != STATUS_SUCCESS
) goto error
;
1156 /* check that all sections are loaded at the right offset */
1157 if (nt
->OptionalHeader
.FileAlignment
!= nt
->OptionalHeader
.SectionAlignment
) goto error
;
1158 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++)
1160 if (sec
[i
].VirtualAddress
!= sec
[i
].PointerToRawData
)
1161 goto error
; /* Windows refuses to load in that case too */
1164 /* set the image protections */
1165 VIRTUAL_SetProt( view
, ptr
, total_size
,
1166 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
| VPROT_EXEC
);
1168 /* no relocations are performed on non page-aligned binaries */
1173 /* map all the sections */
1175 for (i
= pos
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
1177 static const SIZE_T sector_align
= 0x1ff;
1178 SIZE_T map_size
, file_start
, file_size
, end
;
1180 if (!sec
->Misc
.VirtualSize
)
1181 map_size
= ROUND_SIZE( 0, sec
->SizeOfRawData
);
1183 map_size
= ROUND_SIZE( 0, sec
->Misc
.VirtualSize
);
1185 /* file positions are rounded to sector boundaries regardless of OptionalHeader.FileAlignment */
1186 file_start
= sec
->PointerToRawData
& ~sector_align
;
1187 file_size
= (sec
->SizeOfRawData
+ (sec
->PointerToRawData
& sector_align
) + sector_align
) & ~sector_align
;
1188 if (file_size
> map_size
) file_size
= map_size
;
1190 /* a few sanity checks */
1191 end
= sec
->VirtualAddress
+ ROUND_SIZE( sec
->VirtualAddress
, map_size
);
1192 if (sec
->VirtualAddress
> total_size
|| end
> total_size
|| end
< sec
->VirtualAddress
)
1194 WARN_(module
)( "Section %.8s too large (%x+%lx/%lx)\n",
1195 sec
->Name
, sec
->VirtualAddress
, map_size
, total_size
);
1199 if ((sec
->Characteristics
& IMAGE_SCN_MEM_SHARED
) &&
1200 (sec
->Characteristics
& IMAGE_SCN_MEM_WRITE
))
1202 TRACE_(module
)( "mapping shared section %.8s at %p off %x (%x) size %lx (%lx) flags %x\n",
1203 sec
->Name
, ptr
+ sec
->VirtualAddress
,
1204 sec
->PointerToRawData
, (int)pos
, file_size
, map_size
,
1205 sec
->Characteristics
);
1206 if (map_file_into_view( view
, shared_fd
, sec
->VirtualAddress
, map_size
, pos
,
1207 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITE
,
1208 FALSE
) != STATUS_SUCCESS
)
1210 ERR_(module
)( "Could not map shared section %.8s\n", sec
->Name
);
1214 /* check if the import directory falls inside this section */
1215 if (imports
&& imports
->VirtualAddress
>= sec
->VirtualAddress
&&
1216 imports
->VirtualAddress
< sec
->VirtualAddress
+ map_size
)
1218 UINT_PTR base
= imports
->VirtualAddress
& ~page_mask
;
1219 UINT_PTR end
= base
+ ROUND_SIZE( imports
->VirtualAddress
, imports
->Size
);
1220 if (end
> sec
->VirtualAddress
+ map_size
) end
= sec
->VirtualAddress
+ map_size
;
1222 map_file_into_view( view
, shared_fd
, base
, end
- base
,
1223 pos
+ (base
- sec
->VirtualAddress
),
1224 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
,
1231 TRACE_(module
)( "mapping section %.8s at %p off %x size %x virt %x flags %x\n",
1232 sec
->Name
, ptr
+ sec
->VirtualAddress
,
1233 sec
->PointerToRawData
, sec
->SizeOfRawData
,
1234 sec
->Misc
.VirtualSize
, sec
->Characteristics
);
1236 if (!sec
->PointerToRawData
|| !file_size
) continue;
1238 /* Note: if the section is not aligned properly map_file_into_view will magically
1239 * fall back to read(), so we don't need to check anything here.
1241 end
= file_start
+ file_size
;
1242 if (sec
->PointerToRawData
>= st
.st_size
||
1243 end
> ((st
.st_size
+ sector_align
) & ~sector_align
) ||
1245 map_file_into_view( view
, fd
, sec
->VirtualAddress
, file_size
, file_start
,
1246 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
,
1247 !dup_mapping
) != STATUS_SUCCESS
)
1249 ERR_(module
)( "Could not map section %.8s, file probably truncated\n", sec
->Name
);
1253 if (file_size
& page_mask
)
1255 end
= ROUND_SIZE( 0, file_size
);
1256 if (end
> map_size
) end
= map_size
;
1257 TRACE_(module
)("clearing %p - %p\n",
1258 ptr
+ sec
->VirtualAddress
+ file_size
,
1259 ptr
+ sec
->VirtualAddress
+ end
);
1260 memset( ptr
+ sec
->VirtualAddress
+ file_size
, 0, end
- file_size
);
1265 /* perform base relocation, if necessary */
1268 ((nt
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
) ||
1269 !NtCurrentTeb()->Peb
->ImageBaseAddress
) )
1271 IMAGE_BASE_RELOCATION
*rel
, *end
;
1272 const IMAGE_DATA_DIRECTORY
*relocs
;
1274 if (nt
->FileHeader
.Characteristics
& IMAGE_FILE_RELOCS_STRIPPED
)
1276 WARN_(module
)( "Need to relocate module from %p to %p, but there are no relocation records\n",
1278 status
= STATUS_CONFLICTING_ADDRESSES
;
1282 TRACE_(module
)( "relocating from %p-%p to %p-%p\n",
1283 base
, base
+ total_size
, ptr
, ptr
+ total_size
);
1285 relocs
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1286 rel
= (IMAGE_BASE_RELOCATION
*)(ptr
+ relocs
->VirtualAddress
);
1287 end
= (IMAGE_BASE_RELOCATION
*)(ptr
+ relocs
->VirtualAddress
+ relocs
->Size
);
1290 while (rel
< end
- 1 && rel
->SizeOfBlock
)
1292 if (rel
->VirtualAddress
>= total_size
)
1294 WARN_(module
)( "invalid address %p in relocation %p\n", ptr
+ rel
->VirtualAddress
, rel
);
1295 status
= STATUS_ACCESS_VIOLATION
;
1298 rel
= LdrProcessRelocationBlock( ptr
+ rel
->VirtualAddress
,
1299 (rel
->SizeOfBlock
- sizeof(*rel
)) / sizeof(USHORT
),
1300 (USHORT
*)(rel
+ 1), delta
);
1301 if (!rel
) goto error
;
1305 /* set the image protections */
1307 VIRTUAL_SetProt( view
, ptr
, ROUND_SIZE( 0, header_size
), VPROT_COMMITTED
| VPROT_READ
);
1309 sec
= (IMAGE_SECTION_HEADER
*)((char *)&nt
->OptionalHeader
+nt
->FileHeader
.SizeOfOptionalHeader
);
1310 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
1313 BYTE vprot
= VPROT_COMMITTED
;
1315 if (sec
->Misc
.VirtualSize
)
1316 size
= ROUND_SIZE( sec
->VirtualAddress
, sec
->Misc
.VirtualSize
);
1318 size
= ROUND_SIZE( sec
->VirtualAddress
, sec
->SizeOfRawData
);
1320 if (sec
->Characteristics
& IMAGE_SCN_MEM_READ
) vprot
|= VPROT_READ
;
1321 if (sec
->Characteristics
& IMAGE_SCN_MEM_WRITE
) vprot
|= VPROT_READ
|VPROT_WRITE
;
1322 if (sec
->Characteristics
& IMAGE_SCN_MEM_EXECUTE
) vprot
|= VPROT_EXEC
;
1324 /* Dumb game crack lets the AOEP point into a data section. Adjust. */
1325 if ((nt
->OptionalHeader
.AddressOfEntryPoint
>= sec
->VirtualAddress
) &&
1326 (nt
->OptionalHeader
.AddressOfEntryPoint
< sec
->VirtualAddress
+ size
))
1327 vprot
|= VPROT_EXEC
;
1329 if (!VIRTUAL_SetProt( view
, ptr
+ sec
->VirtualAddress
, size
, vprot
) && (vprot
& VPROT_EXEC
))
1330 ERR( "failed to set %08x protection on section %.8s, noexec filesystem?\n",
1331 sec
->Characteristics
, sec
->Name
);
1335 view
->mapping
= dup_mapping
;
1336 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1339 #ifdef VALGRIND_LOAD_PDB_DEBUGINFO
1340 VALGRIND_LOAD_PDB_DEBUGINFO(fd
, ptr
, total_size
, delta
);
1342 if (ptr
!= base
) return STATUS_IMAGE_NOT_AT_BASE
;
1343 return STATUS_SUCCESS
;
1346 if (view
) delete_view( view
);
1347 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1348 if (dup_mapping
) NtClose( dup_mapping
);
1353 /* callback for wine_mmap_enum_reserved_areas to allocate space for the virtual heap */
1354 static int alloc_virtual_heap( void *base
, size_t size
, void *arg
)
1356 void **heap_base
= arg
;
1358 if (is_beyond_limit( base
, size
, address_space_limit
)) address_space_limit
= (char *)base
+ size
;
1359 if (size
< VIRTUAL_HEAP_SIZE
) return 0;
1360 *heap_base
= wine_anon_mmap( (char *)base
+ size
- VIRTUAL_HEAP_SIZE
,
1361 VIRTUAL_HEAP_SIZE
, PROT_READ
|PROT_WRITE
, MAP_FIXED
);
1362 return (*heap_base
!= (void *)-1);
1365 /***********************************************************************
1368 void virtual_init(void)
1370 const char *preload
;
1373 struct file_view
*heap_view
;
1376 page_size
= getpagesize();
1377 page_mask
= page_size
- 1;
1378 /* Make sure we have a power of 2 */
1379 assert( !(page_size
& page_mask
) );
1381 while ((1 << page_shift
) != page_size
) page_shift
++;
1382 user_space_limit
= working_set_limit
= address_space_limit
= (void *)~page_mask
;
1383 #endif /* page_mask */
1384 if ((preload
= getenv("WINEPRELOADRESERVE")))
1386 unsigned long start
, end
;
1387 if (sscanf( preload
, "%lx-%lx", &start
, &end
) == 2)
1389 preload_reserve_start
= (void *)start
;
1390 preload_reserve_end
= (void *)end
;
1394 /* try to find space in a reserved area for the virtual heap */
1395 if (!wine_mmap_enum_reserved_areas( alloc_virtual_heap
, &heap_base
, 1 ))
1396 heap_base
= wine_anon_mmap( NULL
, VIRTUAL_HEAP_SIZE
, PROT_READ
|PROT_WRITE
, 0 );
1398 assert( heap_base
!= (void *)-1 );
1399 virtual_heap
= RtlCreateHeap( HEAP_NO_SERIALIZE
, heap_base
, VIRTUAL_HEAP_SIZE
,
1400 VIRTUAL_HEAP_SIZE
, NULL
, NULL
);
1401 create_view( &heap_view
, heap_base
, VIRTUAL_HEAP_SIZE
, VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITE
);
1403 /* make the DOS area accessible (except the low 64K) to hide bugs in broken apps like Excel 2003 */
1404 size
= (char *)address_space_start
- (char *)0x10000;
1405 if (size
&& wine_mmap_is_in_reserved_area( (void*)0x10000, size
) == 1)
1406 wine_anon_mmap( (void *)0x10000, size
, PROT_READ
| PROT_WRITE
, MAP_FIXED
);
1410 /***********************************************************************
1411 * virtual_init_threading
1413 void virtual_init_threading(void)
1419 /***********************************************************************
1420 * virtual_get_system_info
1422 void virtual_get_system_info( SYSTEM_BASIC_INFORMATION
*info
)
1425 info
->KeMaximumIncrement
= 0; /* FIXME */
1426 info
->PageSize
= page_size
;
1427 info
->MmLowestPhysicalPage
= 1;
1428 info
->MmHighestPhysicalPage
= 0x7fffffff / page_size
;
1429 info
->MmNumberOfPhysicalPages
= info
->MmHighestPhysicalPage
- info
->MmLowestPhysicalPage
;
1430 info
->AllocationGranularity
= get_mask(0) + 1;
1431 info
->LowestUserAddress
= (void *)0x10000;
1432 info
->HighestUserAddress
= (char *)user_space_limit
- 1;
1433 info
->ActiveProcessorsAffinityMask
= (1 << NtCurrentTeb()->Peb
->NumberOfProcessors
) - 1;
1434 info
->NumberOfProcessors
= NtCurrentTeb()->Peb
->NumberOfProcessors
;
1438 /***********************************************************************
1439 * virtual_create_system_view
1441 NTSTATUS
virtual_create_builtin_view( void *module
)
1445 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( module
);
1446 SIZE_T size
= nt
->OptionalHeader
.SizeOfImage
;
1447 IMAGE_SECTION_HEADER
*sec
;
1448 struct file_view
*view
;
1452 size
= ROUND_SIZE( module
, size
);
1453 base
= ROUND_ADDR( module
, page_mask
);
1454 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1455 status
= create_view( &view
, base
, size
, VPROT_SYSTEM
| VPROT_IMAGE
|
1456 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
| VPROT_EXEC
);
1457 if (!status
) TRACE( "created %p-%p\n", base
, (char *)base
+ size
);
1458 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1460 if (status
) return status
;
1462 /* The PE header is always read-only, no write, no execute. */
1463 view
->prot
[0] = VPROT_COMMITTED
| VPROT_READ
;
1465 sec
= (IMAGE_SECTION_HEADER
*)((char *)&nt
->OptionalHeader
+ nt
->FileHeader
.SizeOfOptionalHeader
);
1466 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++)
1468 BYTE flags
= VPROT_COMMITTED
;
1470 if (sec
[i
].Characteristics
& IMAGE_SCN_MEM_EXECUTE
) flags
|= VPROT_EXEC
;
1471 if (sec
[i
].Characteristics
& IMAGE_SCN_MEM_READ
) flags
|= VPROT_READ
;
1472 if (sec
[i
].Characteristics
& IMAGE_SCN_MEM_WRITE
) flags
|= VPROT_WRITE
;
1473 memset (view
->prot
+ (sec
[i
].VirtualAddress
>> page_shift
), flags
,
1474 ROUND_SIZE( sec
[i
].VirtualAddress
, sec
[i
].Misc
.VirtualSize
) >> page_shift
);
1481 /***********************************************************************
1482 * virtual_alloc_thread_stack
1484 NTSTATUS
virtual_alloc_thread_stack( TEB
*teb
, SIZE_T reserve_size
, SIZE_T commit_size
)
1486 struct file_view
*view
;
1491 if (!reserve_size
|| !commit_size
)
1493 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( NtCurrentTeb()->Peb
->ImageBaseAddress
);
1494 if (!reserve_size
) reserve_size
= nt
->OptionalHeader
.SizeOfStackReserve
;
1495 if (!commit_size
) commit_size
= nt
->OptionalHeader
.SizeOfStackCommit
;
1498 size
= max( reserve_size
, commit_size
);
1499 if (size
< 1024 * 1024) size
= 1024 * 1024; /* Xlib needs a large stack */
1500 size
= (size
+ 0xffff) & ~0xffff; /* round to 64K boundary */
1502 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1504 if ((status
= map_view( &view
, NULL
, size
, 0xffff, 0,
1505 VPROT_READ
| VPROT_WRITE
| VPROT_COMMITTED
| VPROT_VALLOC
)) != STATUS_SUCCESS
)
1508 #ifdef VALGRIND_STACK_REGISTER
1509 VALGRIND_STACK_REGISTER( view
->base
, (char *)view
->base
+ view
->size
);
1512 /* setup no access guard page */
1513 VIRTUAL_SetProt( view
, view
->base
, page_size
, VPROT_COMMITTED
);
1514 VIRTUAL_SetProt( view
, (char *)view
->base
+ page_size
, page_size
,
1515 VPROT_READ
| VPROT_WRITE
| VPROT_COMMITTED
| VPROT_GUARD
);
1517 /* note: limit is lower than base since the stack grows down */
1518 teb
->DeallocationStack
= view
->base
;
1519 teb
->Tib
.StackBase
= (char *)view
->base
+ view
->size
;
1520 teb
->Tib
.StackLimit
= (char *)view
->base
+ 2 * page_size
;
1522 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1527 /***********************************************************************
1528 * virtual_clear_thread_stack
1530 * Clear the stack contents before calling the main entry point, some broken apps need that.
1532 void virtual_clear_thread_stack(void)
1534 void *stack
= NtCurrentTeb()->Tib
.StackLimit
;
1535 size_t size
= (char *)NtCurrentTeb()->Tib
.StackBase
- (char *)NtCurrentTeb()->Tib
.StackLimit
;
1537 wine_anon_mmap( stack
, size
, PROT_READ
| PROT_WRITE
, MAP_FIXED
);
1538 if (force_exec_prot
) mprotect( stack
, size
, PROT_READ
| PROT_WRITE
| PROT_EXEC
);
1542 /***********************************************************************
1543 * virtual_handle_fault
1545 NTSTATUS
virtual_handle_fault( LPCVOID addr
, DWORD err
)
1547 struct file_view
*view
;
1548 NTSTATUS ret
= STATUS_ACCESS_VIOLATION
;
1551 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1552 if ((view
= VIRTUAL_FindView( addr
, 0 )))
1554 void *page
= ROUND_ADDR( addr
, page_mask
);
1555 BYTE
*vprot
= &view
->prot
[((const char *)page
- (const char *)view
->base
) >> page_shift
];
1556 if (*vprot
& VPROT_GUARD
)
1558 VIRTUAL_SetProt( view
, page
, page_size
, *vprot
& ~VPROT_GUARD
);
1559 ret
= STATUS_GUARD_PAGE_VIOLATION
;
1561 if ((err
& EXCEPTION_WRITE_FAULT
) && (view
->protect
& VPROT_WRITEWATCH
))
1563 if (*vprot
& VPROT_WRITEWATCH
)
1565 *vprot
&= ~VPROT_WRITEWATCH
;
1566 VIRTUAL_SetProt( view
, page
, page_size
, *vprot
);
1568 /* ignore fault if page is writable now */
1569 if (VIRTUAL_GetUnixProt( *vprot
) & PROT_WRITE
) ret
= STATUS_SUCCESS
;
1572 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1578 /***********************************************************************
1579 * virtual_handle_stack_fault
1581 * Handle an access fault inside the current thread stack.
1582 * Called from inside a signal handler.
1584 BOOL
virtual_handle_stack_fault( void *addr
)
1586 struct file_view
*view
;
1589 RtlEnterCriticalSection( &csVirtual
); /* no need for signal masking inside signal handler */
1590 if ((view
= VIRTUAL_FindView( addr
, 0 )))
1592 void *page
= ROUND_ADDR( addr
, page_mask
);
1593 BYTE vprot
= view
->prot
[((const char *)page
- (const char *)view
->base
) >> page_shift
];
1594 if (vprot
& VPROT_GUARD
)
1596 VIRTUAL_SetProt( view
, page
, page_size
, vprot
& ~VPROT_GUARD
);
1597 if ((char *)page
+ page_size
== NtCurrentTeb()->Tib
.StackLimit
)
1598 NtCurrentTeb()->Tib
.StackLimit
= page
;
1602 RtlLeaveCriticalSection( &csVirtual
);
1607 /***********************************************************************
1608 * virtual_check_buffer_for_read
1610 * Check if a memory buffer can be read, triggering page faults if needed for DIB section access.
1612 BOOL
virtual_check_buffer_for_read( const void *ptr
, SIZE_T size
)
1614 if (!size
) return TRUE
;
1615 if (!ptr
) return FALSE
;
1619 volatile const char *p
= ptr
;
1621 SIZE_T count
= size
;
1623 while (count
> page_size
)
1630 dummy
= p
[count
- 1];
1641 /***********************************************************************
1642 * virtual_check_buffer_for_write
1644 * Check if a memory buffer can be written to, triggering page faults if needed for write watches.
1646 BOOL
virtual_check_buffer_for_write( void *ptr
, SIZE_T size
)
1648 if (!size
) return TRUE
;
1649 if (!ptr
) return FALSE
;
1653 volatile char *p
= ptr
;
1654 SIZE_T count
= size
;
1656 while (count
> page_size
)
1674 /***********************************************************************
1675 * VIRTUAL_SetForceExec
1677 * Whether to force exec prot on all views.
1679 void VIRTUAL_SetForceExec( BOOL enable
)
1681 struct file_view
*view
;
1684 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1685 if (!force_exec_prot
!= !enable
) /* change all existing views */
1687 force_exec_prot
= enable
;
1689 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
1692 char *addr
= view
->base
;
1693 BYTE commit
= view
->mapping
? VPROT_COMMITTED
: 0; /* file mappings are always accessible */
1694 int unix_prot
= VIRTUAL_GetUnixProt( view
->prot
[0] | commit
);
1696 if (view
->protect
& VPROT_NOEXEC
) continue;
1697 for (count
= i
= 1; i
< view
->size
>> page_shift
; i
++, count
++)
1699 int prot
= VIRTUAL_GetUnixProt( view
->prot
[i
] | commit
);
1700 if (prot
== unix_prot
) continue;
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) );
1709 addr
+= (count
<< page_shift
);
1715 if ((unix_prot
& PROT_READ
) && !(unix_prot
& PROT_EXEC
))
1717 TRACE( "%s exec prot for %p-%p\n",
1718 force_exec_prot
? "enabling" : "disabling",
1719 addr
, addr
+ (count
<< page_shift
) - 1 );
1720 mprotect( addr
, count
<< page_shift
,
1721 unix_prot
| (force_exec_prot
? PROT_EXEC
: 0) );
1726 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1735 /* free reserved areas above the limit; callback for wine_mmap_enum_reserved_areas */
1736 static int free_reserved_memory( void *base
, size_t size
, void *arg
)
1738 struct free_range
*range
= arg
;
1740 if ((char *)base
>= range
->limit
) return 0;
1741 if ((char *)base
+ size
<= range
->base
) return 0;
1742 if ((char *)base
< range
->base
)
1744 size
-= range
->base
- (char *)base
;
1747 if ((char *)base
+ size
> range
->limit
) size
= range
->limit
- (char *)base
;
1748 remove_reserved_area( base
, size
);
1749 return 1; /* stop enumeration since the list has changed */
1752 /***********************************************************************
1753 * virtual_release_address_space
1755 * Release some address space once we have loaded and initialized the app.
1757 void virtual_release_address_space( BOOL free_high_mem
)
1759 struct free_range range
;
1762 if (user_space_limit
== address_space_limit
) return; /* no need to free anything */
1764 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1766 /* no large address space on win9x */
1767 if (free_high_mem
&& NtCurrentTeb()->Peb
->OSPlatformId
== VER_PLATFORM_WIN32_NT
)
1769 range
.base
= (char *)0x82000000;
1770 range
.limit
= address_space_limit
;
1771 while (wine_mmap_enum_reserved_areas( free_reserved_memory
, &range
, 1 )) /* nothing */;
1772 user_space_limit
= working_set_limit
= address_space_limit
;
1776 #ifndef __APPLE__ /* dyld doesn't support parts of the WINE_DOS segment being unmapped */
1777 range
.base
= (char *)0x20000000;
1778 range
.limit
= (char *)0x7f000000;
1779 while (wine_mmap_enum_reserved_areas( free_reserved_memory
, &range
, 0 )) /* nothing */;
1783 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1787 /***********************************************************************
1788 * NtAllocateVirtualMemory (NTDLL.@)
1789 * ZwAllocateVirtualMemory (NTDLL.@)
1791 NTSTATUS WINAPI
NtAllocateVirtualMemory( HANDLE process
, PVOID
*ret
, ULONG zero_bits
,
1792 SIZE_T
*size_ptr
, ULONG type
, ULONG protect
)
1796 SIZE_T size
= *size_ptr
;
1797 SIZE_T mask
= get_mask( zero_bits
);
1798 NTSTATUS status
= STATUS_SUCCESS
;
1799 struct file_view
*view
;
1802 TRACE("%p %p %08lx %x %08x\n", process
, *ret
, size
, type
, protect
);
1804 if (!size
) return STATUS_INVALID_PARAMETER
;
1806 if (process
!= NtCurrentProcess())
1809 apc_result_t result
;
1811 memset( &call
, 0, sizeof(call
) );
1813 call
.virtual_alloc
.type
= APC_VIRTUAL_ALLOC
;
1814 call
.virtual_alloc
.addr
= wine_server_client_ptr( *ret
);
1815 call
.virtual_alloc
.size
= *size_ptr
;
1816 call
.virtual_alloc
.zero_bits
= zero_bits
;
1817 call
.virtual_alloc
.op_type
= type
;
1818 call
.virtual_alloc
.prot
= protect
;
1819 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
1820 if (status
!= STATUS_SUCCESS
) return status
;
1822 if (result
.virtual_alloc
.status
== STATUS_SUCCESS
)
1824 *ret
= wine_server_get_ptr( result
.virtual_alloc
.addr
);
1825 *size_ptr
= result
.virtual_alloc
.size
;
1827 return result
.virtual_alloc
.status
;
1830 /* Round parameters to a page boundary */
1832 if (is_beyond_limit( 0, size
, working_set_limit
)) return STATUS_WORKING_SET_LIMIT_RANGE
;
1834 if ((status
= get_vprot_flags( protect
, &vprot
))) return status
;
1835 vprot
|= VPROT_VALLOC
;
1836 if (type
& MEM_COMMIT
) vprot
|= VPROT_COMMITTED
;
1840 if (type
& MEM_RESERVE
) /* Round down to 64k boundary */
1841 base
= ROUND_ADDR( *ret
, mask
);
1843 base
= ROUND_ADDR( *ret
, page_mask
);
1844 size
= (((UINT_PTR
)*ret
+ size
+ page_mask
) & ~page_mask
) - (UINT_PTR
)base
;
1846 /* address 1 is magic to mean DOS area */
1847 if (!base
&& *ret
== (void *)1 && size
== 0x110000)
1849 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1850 status
= allocate_dos_memory( &view
, vprot
);
1851 if (status
== STATUS_SUCCESS
)
1854 *size_ptr
= view
->size
;
1856 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1860 /* disallow low 64k, wrap-around and kernel space */
1861 if (((char *)base
< (char *)0x10000) ||
1862 ((char *)base
+ size
< (char *)base
) ||
1863 is_beyond_limit( base
, size
, address_space_limit
))
1864 return STATUS_INVALID_PARAMETER
;
1869 size
= (size
+ page_mask
) & ~page_mask
;
1872 /* Compute the alloc type flags */
1874 if (!(type
& (MEM_COMMIT
| MEM_RESERVE
| MEM_RESET
)) ||
1875 (type
& ~(MEM_COMMIT
| MEM_RESERVE
| MEM_TOP_DOWN
| MEM_WRITE_WATCH
| MEM_RESET
)))
1877 WARN("called with wrong alloc type flags (%08x) !\n", type
);
1878 return STATUS_INVALID_PARAMETER
;
1881 /* Reserve the memory */
1883 if (use_locks
) server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1885 if ((type
& MEM_RESERVE
) || !base
)
1887 if (type
& MEM_WRITE_WATCH
) vprot
|= VPROT_WRITEWATCH
;
1888 status
= map_view( &view
, base
, size
, mask
, type
& MEM_TOP_DOWN
, vprot
);
1889 if (status
== STATUS_SUCCESS
) base
= view
->base
;
1891 else if (type
& MEM_RESET
)
1893 if (!(view
= VIRTUAL_FindView( base
, size
))) status
= STATUS_NOT_MAPPED_VIEW
;
1894 else madvise( base
, size
, MADV_DONTNEED
);
1896 else /* commit the pages */
1898 if (!(view
= VIRTUAL_FindView( base
, size
))) status
= STATUS_NOT_MAPPED_VIEW
;
1899 else if (!VIRTUAL_SetProt( view
, base
, size
, vprot
)) status
= STATUS_ACCESS_DENIED
;
1900 else if (view
->mapping
&& !(view
->protect
& VPROT_COMMITTED
))
1902 SERVER_START_REQ( add_mapping_committed_range
)
1904 req
->handle
= wine_server_obj_handle( view
->mapping
);
1905 req
->offset
= (char *)base
- (char *)view
->base
;
1907 wine_server_call( req
);
1913 if (use_locks
) server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1915 if (status
== STATUS_SUCCESS
)
1924 /***********************************************************************
1925 * NtFreeVirtualMemory (NTDLL.@)
1926 * ZwFreeVirtualMemory (NTDLL.@)
1928 NTSTATUS WINAPI
NtFreeVirtualMemory( HANDLE process
, PVOID
*addr_ptr
, SIZE_T
*size_ptr
, ULONG type
)
1930 struct file_view
*view
;
1933 NTSTATUS status
= STATUS_SUCCESS
;
1934 LPVOID addr
= *addr_ptr
;
1935 SIZE_T size
= *size_ptr
;
1937 TRACE("%p %p %08lx %x\n", process
, addr
, size
, type
);
1939 if (process
!= NtCurrentProcess())
1942 apc_result_t result
;
1944 memset( &call
, 0, sizeof(call
) );
1946 call
.virtual_free
.type
= APC_VIRTUAL_FREE
;
1947 call
.virtual_free
.addr
= wine_server_client_ptr( addr
);
1948 call
.virtual_free
.size
= size
;
1949 call
.virtual_free
.op_type
= type
;
1950 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
1951 if (status
!= STATUS_SUCCESS
) return status
;
1953 if (result
.virtual_free
.status
== STATUS_SUCCESS
)
1955 *addr_ptr
= wine_server_get_ptr( result
.virtual_free
.addr
);
1956 *size_ptr
= result
.virtual_free
.size
;
1958 return result
.virtual_free
.status
;
1961 /* Fix the parameters */
1963 size
= ROUND_SIZE( addr
, size
);
1964 base
= ROUND_ADDR( addr
, page_mask
);
1966 /* avoid freeing the DOS area when a broken app passes a NULL pointer */
1967 if (!base
) return STATUS_INVALID_PARAMETER
;
1969 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1971 if (!(view
= VIRTUAL_FindView( base
, size
)) || !(view
->protect
& VPROT_VALLOC
))
1973 status
= STATUS_INVALID_PARAMETER
;
1975 else if (type
== MEM_RELEASE
)
1977 /* Free the pages */
1979 if (size
|| (base
!= view
->base
)) status
= STATUS_INVALID_PARAMETER
;
1982 delete_view( view
);
1987 else if (type
== MEM_DECOMMIT
)
1989 status
= decommit_pages( view
, base
- (char *)view
->base
, size
);
1990 if (status
== STATUS_SUCCESS
)
1998 WARN("called with wrong free type flags (%08x) !\n", type
);
1999 status
= STATUS_INVALID_PARAMETER
;
2002 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2007 /***********************************************************************
2008 * NtProtectVirtualMemory (NTDLL.@)
2009 * ZwProtectVirtualMemory (NTDLL.@)
2011 NTSTATUS WINAPI
NtProtectVirtualMemory( HANDLE process
, PVOID
*addr_ptr
, SIZE_T
*size_ptr
,
2012 ULONG new_prot
, ULONG
*old_prot
)
2014 struct file_view
*view
;
2016 NTSTATUS status
= STATUS_SUCCESS
;
2019 unsigned int new_vprot
;
2020 SIZE_T size
= *size_ptr
;
2021 LPVOID addr
= *addr_ptr
;
2023 TRACE("%p %p %08lx %08x\n", process
, addr
, size
, new_prot
);
2025 if (process
!= NtCurrentProcess())
2028 apc_result_t result
;
2030 memset( &call
, 0, sizeof(call
) );
2032 call
.virtual_protect
.type
= APC_VIRTUAL_PROTECT
;
2033 call
.virtual_protect
.addr
= wine_server_client_ptr( addr
);
2034 call
.virtual_protect
.size
= size
;
2035 call
.virtual_protect
.prot
= new_prot
;
2036 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
2037 if (status
!= STATUS_SUCCESS
) return status
;
2039 if (result
.virtual_protect
.status
== STATUS_SUCCESS
)
2041 *addr_ptr
= wine_server_get_ptr( result
.virtual_protect
.addr
);
2042 *size_ptr
= result
.virtual_protect
.size
;
2043 if (old_prot
) *old_prot
= result
.virtual_protect
.prot
;
2045 return result
.virtual_protect
.status
;
2048 /* Fix the parameters */
2050 size
= ROUND_SIZE( addr
, size
);
2051 base
= ROUND_ADDR( addr
, page_mask
);
2052 if ((status
= get_vprot_flags( new_prot
, &new_vprot
))) return status
;
2053 new_vprot
|= VPROT_COMMITTED
;
2055 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2057 if (!(view
= VIRTUAL_FindView( base
, size
)))
2059 status
= STATUS_INVALID_PARAMETER
;
2063 /* Make sure all the pages are committed */
2064 if (get_committed_size( view
, base
, &vprot
) >= size
&& (vprot
& VPROT_COMMITTED
))
2066 if (old_prot
) *old_prot
= VIRTUAL_GetWin32Prot( vprot
);
2067 if (!VIRTUAL_SetProt( view
, base
, size
, new_vprot
)) status
= STATUS_ACCESS_DENIED
;
2069 else status
= STATUS_NOT_COMMITTED
;
2071 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2073 if (status
== STATUS_SUCCESS
)
2082 /* retrieve state for a free memory area; callback for wine_mmap_enum_reserved_areas */
2083 static int get_free_mem_state_callback( void *start
, size_t size
, void *arg
)
2085 MEMORY_BASIC_INFORMATION
*info
= arg
;
2086 void *end
= (char *)start
+ size
;
2088 if ((char *)info
->BaseAddress
+ info
->RegionSize
< (char *)start
) return 0;
2090 if (info
->BaseAddress
>= end
)
2092 if (info
->AllocationBase
< end
) info
->AllocationBase
= end
;
2096 if (info
->BaseAddress
>= start
|| start
<= address_space_start
)
2098 /* it's a real free area */
2099 info
->State
= MEM_FREE
;
2100 info
->Protect
= PAGE_NOACCESS
;
2101 info
->AllocationBase
= 0;
2102 info
->AllocationProtect
= 0;
2104 if ((char *)info
->BaseAddress
+ info
->RegionSize
> (char *)end
)
2105 info
->RegionSize
= (char *)end
- (char *)info
->BaseAddress
;
2107 else /* outside of the reserved area, pretend it's allocated */
2109 info
->RegionSize
= (char *)start
- (char *)info
->BaseAddress
;
2110 info
->State
= MEM_RESERVE
;
2111 info
->Protect
= PAGE_NOACCESS
;
2112 info
->AllocationProtect
= PAGE_NOACCESS
;
2113 info
->Type
= MEM_PRIVATE
;
2118 #define UNIMPLEMENTED_INFO_CLASS(c) \
2120 FIXME("(process=%p,addr=%p) Unimplemented information class: " #c "\n", process, addr); \
2121 return STATUS_INVALID_INFO_CLASS
2123 /***********************************************************************
2124 * NtQueryVirtualMemory (NTDLL.@)
2125 * ZwQueryVirtualMemory (NTDLL.@)
2127 NTSTATUS WINAPI
NtQueryVirtualMemory( HANDLE process
, LPCVOID addr
,
2128 MEMORY_INFORMATION_CLASS info_class
, PVOID buffer
,
2129 SIZE_T len
, SIZE_T
*res_len
)
2131 struct file_view
*view
;
2132 char *base
, *alloc_base
= 0;
2135 MEMORY_BASIC_INFORMATION
*info
= buffer
;
2138 if (info_class
!= MemoryBasicInformation
)
2142 UNIMPLEMENTED_INFO_CLASS(MemoryWorkingSetList
);
2143 UNIMPLEMENTED_INFO_CLASS(MemorySectionName
);
2144 UNIMPLEMENTED_INFO_CLASS(MemoryBasicVlmInformation
);
2147 FIXME("(%p,%p,info_class=%d,%p,%ld,%p) Unknown information class\n",
2148 process
, addr
, info_class
, buffer
, len
, res_len
);
2149 return STATUS_INVALID_INFO_CLASS
;
2153 if (process
!= NtCurrentProcess())
2157 apc_result_t result
;
2159 memset( &call
, 0, sizeof(call
) );
2161 call
.virtual_query
.type
= APC_VIRTUAL_QUERY
;
2162 call
.virtual_query
.addr
= wine_server_client_ptr( addr
);
2163 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
2164 if (status
!= STATUS_SUCCESS
) return status
;
2166 if (result
.virtual_query
.status
== STATUS_SUCCESS
)
2168 info
->BaseAddress
= wine_server_get_ptr( result
.virtual_query
.base
);
2169 info
->AllocationBase
= wine_server_get_ptr( result
.virtual_query
.alloc_base
);
2170 info
->RegionSize
= result
.virtual_query
.size
;
2171 info
->Protect
= result
.virtual_query
.prot
;
2172 info
->AllocationProtect
= result
.virtual_query
.alloc_prot
;
2173 info
->State
= (DWORD
)result
.virtual_query
.state
<< 12;
2174 info
->Type
= (DWORD
)result
.virtual_query
.alloc_type
<< 16;
2175 if (info
->RegionSize
!= result
.virtual_query
.size
) /* truncated */
2176 return STATUS_INVALID_PARAMETER
; /* FIXME */
2177 if (res_len
) *res_len
= sizeof(*info
);
2179 return result
.virtual_query
.status
;
2182 base
= ROUND_ADDR( addr
, page_mask
);
2184 if (is_beyond_limit( base
, 1, working_set_limit
)) return STATUS_WORKING_SET_LIMIT_RANGE
;
2186 /* Find the view containing the address */
2188 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2189 ptr
= list_head( &views_list
);
2194 size
= (char *)working_set_limit
- alloc_base
;
2198 view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
2199 if ((char *)view
->base
> base
)
2201 size
= (char *)view
->base
- alloc_base
;
2205 if ((char *)view
->base
+ view
->size
> base
)
2207 alloc_base
= view
->base
;
2211 alloc_base
= (char *)view
->base
+ view
->size
;
2212 ptr
= list_next( &views_list
, ptr
);
2215 /* Fill the info structure */
2217 info
->AllocationBase
= alloc_base
;
2218 info
->BaseAddress
= base
;
2219 info
->RegionSize
= size
- (base
- alloc_base
);
2223 if (!wine_mmap_enum_reserved_areas( get_free_mem_state_callback
, info
, 0 ))
2225 /* not in a reserved area at all, pretend it's allocated */
2227 if (base
>= (char *)address_space_start
)
2229 info
->State
= MEM_RESERVE
;
2230 info
->Protect
= PAGE_NOACCESS
;
2231 info
->AllocationProtect
= PAGE_NOACCESS
;
2232 info
->Type
= MEM_PRIVATE
;
2237 info
->State
= MEM_FREE
;
2238 info
->Protect
= PAGE_NOACCESS
;
2239 info
->AllocationBase
= 0;
2240 info
->AllocationProtect
= 0;
2248 SIZE_T range_size
= get_committed_size( view
, base
, &vprot
);
2250 info
->State
= (vprot
& VPROT_COMMITTED
) ? MEM_COMMIT
: MEM_RESERVE
;
2251 info
->Protect
= (vprot
& VPROT_COMMITTED
) ? VIRTUAL_GetWin32Prot( vprot
) : 0;
2252 info
->AllocationBase
= alloc_base
;
2253 info
->AllocationProtect
= VIRTUAL_GetWin32Prot( view
->protect
);
2254 if (view
->protect
& VPROT_IMAGE
) info
->Type
= MEM_IMAGE
;
2255 else if (view
->protect
& VPROT_VALLOC
) info
->Type
= MEM_PRIVATE
;
2256 else info
->Type
= MEM_MAPPED
;
2257 for (size
= base
- alloc_base
; size
< base
+ range_size
- alloc_base
; size
+= page_size
)
2258 if ((view
->prot
[size
>> page_shift
] ^ vprot
) & ~VPROT_WRITEWATCH
) break;
2259 info
->RegionSize
= size
- (base
- alloc_base
);
2261 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2263 if (res_len
) *res_len
= sizeof(*info
);
2264 return STATUS_SUCCESS
;
2268 /***********************************************************************
2269 * NtLockVirtualMemory (NTDLL.@)
2270 * ZwLockVirtualMemory (NTDLL.@)
2272 NTSTATUS WINAPI
NtLockVirtualMemory( HANDLE process
, PVOID
*addr
, SIZE_T
*size
, ULONG unknown
)
2274 NTSTATUS status
= STATUS_SUCCESS
;
2276 if (process
!= NtCurrentProcess())
2279 apc_result_t result
;
2281 memset( &call
, 0, sizeof(call
) );
2283 call
.virtual_lock
.type
= APC_VIRTUAL_LOCK
;
2284 call
.virtual_lock
.addr
= wine_server_client_ptr( *addr
);
2285 call
.virtual_lock
.size
= *size
;
2286 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
2287 if (status
!= STATUS_SUCCESS
) return status
;
2289 if (result
.virtual_lock
.status
== STATUS_SUCCESS
)
2291 *addr
= wine_server_get_ptr( result
.virtual_lock
.addr
);
2292 *size
= result
.virtual_lock
.size
;
2294 return result
.virtual_lock
.status
;
2297 *size
= ROUND_SIZE( *addr
, *size
);
2298 *addr
= ROUND_ADDR( *addr
, page_mask
);
2300 if (mlock( *addr
, *size
)) status
= STATUS_ACCESS_DENIED
;
2305 /***********************************************************************
2306 * NtUnlockVirtualMemory (NTDLL.@)
2307 * ZwUnlockVirtualMemory (NTDLL.@)
2309 NTSTATUS WINAPI
NtUnlockVirtualMemory( HANDLE process
, PVOID
*addr
, SIZE_T
*size
, ULONG unknown
)
2311 NTSTATUS status
= STATUS_SUCCESS
;
2313 if (process
!= NtCurrentProcess())
2316 apc_result_t result
;
2318 memset( &call
, 0, sizeof(call
) );
2320 call
.virtual_unlock
.type
= APC_VIRTUAL_UNLOCK
;
2321 call
.virtual_unlock
.addr
= wine_server_client_ptr( *addr
);
2322 call
.virtual_unlock
.size
= *size
;
2323 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
2324 if (status
!= STATUS_SUCCESS
) return status
;
2326 if (result
.virtual_unlock
.status
== STATUS_SUCCESS
)
2328 *addr
= wine_server_get_ptr( result
.virtual_unlock
.addr
);
2329 *size
= result
.virtual_unlock
.size
;
2331 return result
.virtual_unlock
.status
;
2334 *size
= ROUND_SIZE( *addr
, *size
);
2335 *addr
= ROUND_ADDR( *addr
, page_mask
);
2337 if (munlock( *addr
, *size
)) status
= STATUS_ACCESS_DENIED
;
2342 /***********************************************************************
2343 * NtCreateSection (NTDLL.@)
2344 * ZwCreateSection (NTDLL.@)
2346 NTSTATUS WINAPI
NtCreateSection( HANDLE
*handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
,
2347 const LARGE_INTEGER
*size
, ULONG protect
,
2348 ULONG sec_flags
, HANDLE file
)
2352 DWORD len
= (attr
&& attr
->ObjectName
) ? attr
->ObjectName
->Length
: 0;
2353 struct security_descriptor
*sd
= NULL
;
2354 struct object_attributes objattr
;
2356 /* Check parameters */
2358 if (len
> MAX_PATH
*sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
2360 if ((ret
= get_vprot_flags( protect
, &vprot
))) return ret
;
2362 objattr
.rootdir
= wine_server_obj_handle( attr
? attr
->RootDirectory
: 0 );
2364 objattr
.name_len
= len
;
2367 ret
= NTDLL_create_struct_sd( attr
->SecurityDescriptor
, &sd
, &objattr
.sd_len
);
2368 if (ret
!= STATUS_SUCCESS
) return ret
;
2371 if (!(sec_flags
& SEC_RESERVE
)) vprot
|= VPROT_COMMITTED
;
2372 if (sec_flags
& SEC_NOCACHE
) vprot
|= VPROT_NOCACHE
;
2373 if (sec_flags
& SEC_IMAGE
) vprot
|= VPROT_IMAGE
;
2375 /* Create the server object */
2377 SERVER_START_REQ( create_mapping
)
2379 req
->access
= access
;
2380 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
2381 req
->file_handle
= wine_server_obj_handle( file
);
2382 req
->size
= size
? size
->QuadPart
: 0;
2383 req
->protect
= vprot
;
2384 wine_server_add_data( req
, &objattr
, sizeof(objattr
) );
2385 if (objattr
.sd_len
) wine_server_add_data( req
, sd
, objattr
.sd_len
);
2386 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
2387 ret
= wine_server_call( req
);
2388 *handle
= wine_server_ptr_handle( reply
->handle
);
2392 NTDLL_free_struct_sd( sd
);
2398 /***********************************************************************
2399 * NtOpenSection (NTDLL.@)
2400 * ZwOpenSection (NTDLL.@)
2402 NTSTATUS WINAPI
NtOpenSection( HANDLE
*handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
)
2405 DWORD len
= attr
->ObjectName
->Length
;
2407 if (len
> MAX_PATH
*sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
2409 SERVER_START_REQ( open_mapping
)
2411 req
->access
= access
;
2412 req
->attributes
= attr
->Attributes
;
2413 req
->rootdir
= wine_server_obj_handle( attr
->RootDirectory
);
2414 wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
2415 if (!(ret
= wine_server_call( req
))) *handle
= wine_server_ptr_handle( reply
->handle
);
2422 /***********************************************************************
2423 * NtMapViewOfSection (NTDLL.@)
2424 * ZwMapViewOfSection (NTDLL.@)
2426 NTSTATUS WINAPI
NtMapViewOfSection( HANDLE handle
, HANDLE process
, PVOID
*addr_ptr
, ULONG zero_bits
,
2427 SIZE_T commit_size
, const LARGE_INTEGER
*offset_ptr
, SIZE_T
*size_ptr
,
2428 SECTION_INHERIT inherit
, ULONG alloc_type
, ULONG protect
)
2431 mem_size_t full_size
;
2433 SIZE_T size
, mask
= get_mask( zero_bits
);
2434 int unix_handle
= -1, needs_close
;
2435 unsigned int map_vprot
, vprot
;
2437 struct file_view
*view
;
2439 HANDLE dup_mapping
, shared_file
;
2440 LARGE_INTEGER offset
;
2443 offset
.QuadPart
= offset_ptr
? offset_ptr
->QuadPart
: 0;
2445 TRACE("handle=%p process=%p addr=%p off=%x%08x size=%lx access=%x\n",
2446 handle
, process
, *addr_ptr
, offset
.u
.HighPart
, offset
.u
.LowPart
, *size_ptr
, protect
);
2448 /* Check parameters */
2450 if ((offset
.u
.LowPart
& mask
) || (*addr_ptr
&& ((UINT_PTR
)*addr_ptr
& mask
)))
2451 return STATUS_INVALID_PARAMETER
;
2458 case PAGE_READWRITE
:
2459 case PAGE_EXECUTE_READWRITE
:
2460 access
= SECTION_MAP_WRITE
;
2463 case PAGE_WRITECOPY
:
2465 case PAGE_EXECUTE_READ
:
2466 case PAGE_EXECUTE_WRITECOPY
:
2467 access
= SECTION_MAP_READ
;
2470 return STATUS_INVALID_PARAMETER
;
2473 if (process
!= NtCurrentProcess())
2476 apc_result_t result
;
2478 memset( &call
, 0, sizeof(call
) );
2480 call
.map_view
.type
= APC_MAP_VIEW
;
2481 call
.map_view
.handle
= wine_server_obj_handle( handle
);
2482 call
.map_view
.addr
= wine_server_client_ptr( *addr_ptr
);
2483 call
.map_view
.size
= *size_ptr
;
2484 call
.map_view
.offset
= offset
.QuadPart
;
2485 call
.map_view
.zero_bits
= zero_bits
;
2486 call
.map_view
.alloc_type
= alloc_type
;
2487 call
.map_view
.prot
= protect
;
2488 res
= NTDLL_queue_process_apc( process
, &call
, &result
);
2489 if (res
!= STATUS_SUCCESS
) return res
;
2491 if ((NTSTATUS
)result
.map_view
.status
>= 0)
2493 *addr_ptr
= wine_server_get_ptr( result
.map_view
.addr
);
2494 *size_ptr
= result
.map_view
.size
;
2496 return result
.map_view
.status
;
2499 SERVER_START_REQ( get_mapping_info
)
2501 req
->handle
= wine_server_obj_handle( handle
);
2502 req
->access
= access
;
2503 res
= wine_server_call( req
);
2504 map_vprot
= reply
->protect
;
2505 base
= wine_server_get_ptr( reply
->base
);
2506 full_size
= reply
->size
;
2507 header_size
= reply
->header_size
;
2508 dup_mapping
= wine_server_ptr_handle( reply
->mapping
);
2509 shared_file
= wine_server_ptr_handle( reply
->shared_file
);
2510 if ((ULONG_PTR
)base
!= reply
->base
) base
= NULL
;
2513 if (res
) return res
;
2515 if ((res
= server_get_unix_fd( handle
, 0, &unix_handle
, &needs_close
, NULL
, NULL
))) goto done
;
2517 if (map_vprot
& VPROT_IMAGE
)
2520 if (size
!= full_size
) /* truncated */
2522 WARN( "Modules larger than 4Gb (%s) not supported\n", wine_dbgstr_longlong(full_size
) );
2523 res
= STATUS_INVALID_PARAMETER
;
2528 int shared_fd
, shared_needs_close
;
2530 if ((res
= server_get_unix_fd( shared_file
, FILE_READ_DATA
|FILE_WRITE_DATA
,
2531 &shared_fd
, &shared_needs_close
, NULL
, NULL
))) goto done
;
2532 res
= map_image( handle
, unix_handle
, base
, size
, mask
, header_size
,
2533 shared_fd
, dup_mapping
, addr_ptr
);
2534 if (shared_needs_close
) close( shared_fd
);
2535 NtClose( shared_file
);
2539 res
= map_image( handle
, unix_handle
, base
, size
, mask
, header_size
,
2540 -1, dup_mapping
, addr_ptr
);
2542 if (needs_close
) close( unix_handle
);
2543 if (res
>= 0) *size_ptr
= size
;
2547 res
= STATUS_INVALID_PARAMETER
;
2548 if (offset
.QuadPart
>= full_size
) goto done
;
2551 if (*size_ptr
> full_size
- offset
.QuadPart
) goto done
;
2552 size
= ROUND_SIZE( offset
.u
.LowPart
, *size_ptr
);
2553 if (size
< *size_ptr
) goto done
; /* wrap-around */
2557 size
= full_size
- offset
.QuadPart
;
2558 if (size
!= full_size
- offset
.QuadPart
) /* truncated */
2560 WARN( "Files larger than 4Gb (%s) not supported on this platform\n",
2561 wine_dbgstr_longlong(full_size
) );
2566 /* Reserve a properly aligned area */
2568 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2570 get_vprot_flags( protect
, &vprot
);
2571 vprot
|= (map_vprot
& VPROT_COMMITTED
);
2572 res
= map_view( &view
, *addr_ptr
, size
, mask
, FALSE
, vprot
);
2575 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2581 TRACE("handle=%p size=%lx offset=%x%08x\n",
2582 handle
, size
, offset
.u
.HighPart
, offset
.u
.LowPart
);
2584 res
= map_file_into_view( view
, unix_handle
, 0, size
, offset
.QuadPart
, vprot
, !dup_mapping
);
2585 if (res
== STATUS_SUCCESS
)
2587 *addr_ptr
= view
->base
;
2589 view
->mapping
= dup_mapping
;
2590 dup_mapping
= 0; /* don't close it */
2594 ERR( "map_file_into_view %p %lx %x%08x failed\n",
2595 view
->base
, size
, offset
.u
.HighPart
, offset
.u
.LowPart
);
2596 delete_view( view
);
2599 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2602 if (dup_mapping
) NtClose( dup_mapping
);
2603 if (needs_close
) close( unix_handle
);
2608 /***********************************************************************
2609 * NtUnmapViewOfSection (NTDLL.@)
2610 * ZwUnmapViewOfSection (NTDLL.@)
2612 NTSTATUS WINAPI
NtUnmapViewOfSection( HANDLE process
, PVOID addr
)
2614 struct file_view
*view
;
2615 NTSTATUS status
= STATUS_NOT_MAPPED_VIEW
;
2617 void *base
= ROUND_ADDR( addr
, page_mask
);
2619 if (process
!= NtCurrentProcess())
2622 apc_result_t result
;
2624 memset( &call
, 0, sizeof(call
) );
2626 call
.unmap_view
.type
= APC_UNMAP_VIEW
;
2627 call
.unmap_view
.addr
= wine_server_client_ptr( addr
);
2628 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
2629 if (status
== STATUS_SUCCESS
) status
= result
.unmap_view
.status
;
2633 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2634 if ((view
= VIRTUAL_FindView( base
, 0 )) && (base
== view
->base
) && !(view
->protect
& VPROT_VALLOC
))
2636 delete_view( view
);
2637 status
= STATUS_SUCCESS
;
2639 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2644 /***********************************************************************
2645 * NtFlushVirtualMemory (NTDLL.@)
2646 * ZwFlushVirtualMemory (NTDLL.@)
2648 NTSTATUS WINAPI
NtFlushVirtualMemory( HANDLE process
, LPCVOID
*addr_ptr
,
2649 SIZE_T
*size_ptr
, ULONG unknown
)
2651 struct file_view
*view
;
2652 NTSTATUS status
= STATUS_SUCCESS
;
2654 void *addr
= ROUND_ADDR( *addr_ptr
, page_mask
);
2656 if (process
!= NtCurrentProcess())
2659 apc_result_t result
;
2661 memset( &call
, 0, sizeof(call
) );
2663 call
.virtual_flush
.type
= APC_VIRTUAL_FLUSH
;
2664 call
.virtual_flush
.addr
= wine_server_client_ptr( addr
);
2665 call
.virtual_flush
.size
= *size_ptr
;
2666 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
2667 if (status
!= STATUS_SUCCESS
) return status
;
2669 if (result
.virtual_flush
.status
== STATUS_SUCCESS
)
2671 *addr_ptr
= wine_server_get_ptr( result
.virtual_flush
.addr
);
2672 *size_ptr
= result
.virtual_flush
.size
;
2674 return result
.virtual_flush
.status
;
2677 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2678 if (!(view
= VIRTUAL_FindView( addr
, *size_ptr
))) status
= STATUS_INVALID_PARAMETER
;
2681 if (!*size_ptr
) *size_ptr
= view
->size
;
2683 if (msync( addr
, *size_ptr
, MS_SYNC
)) status
= STATUS_NOT_MAPPED_DATA
;
2685 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2690 /***********************************************************************
2691 * NtGetWriteWatch (NTDLL.@)
2692 * ZwGetWriteWatch (NTDLL.@)
2694 NTSTATUS WINAPI
NtGetWriteWatch( HANDLE process
, ULONG flags
, PVOID base
, SIZE_T size
, PVOID
*addresses
,
2695 ULONG_PTR
*count
, ULONG
*granularity
)
2697 struct file_view
*view
;
2698 NTSTATUS status
= STATUS_SUCCESS
;
2701 size
= ROUND_SIZE( base
, size
);
2702 base
= ROUND_ADDR( base
, page_mask
);
2704 if (!count
|| !granularity
) return STATUS_ACCESS_VIOLATION
;
2705 if (!*count
|| !size
) return STATUS_INVALID_PARAMETER
;
2706 if (flags
& ~WRITE_WATCH_FLAG_RESET
) return STATUS_INVALID_PARAMETER
;
2708 if (!addresses
) return STATUS_ACCESS_VIOLATION
;
2710 TRACE( "%p %x %p-%p %p %lu\n", process
, flags
, base
, (char *)base
+ size
,
2711 addresses
, *count
);
2713 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2715 if ((view
= VIRTUAL_FindView( base
, size
)) && (view
->protect
& VPROT_WRITEWATCH
))
2719 char *end
= addr
+ size
;
2721 while (pos
< *count
&& addr
< end
)
2723 BYTE prot
= view
->prot
[(addr
- (char *)view
->base
) >> page_shift
];
2724 if (!(prot
& VPROT_WRITEWATCH
)) addresses
[pos
++] = addr
;
2727 if (flags
& WRITE_WATCH_FLAG_RESET
) reset_write_watches( view
, base
, addr
- (char *)base
);
2729 *granularity
= page_size
;
2731 else status
= STATUS_INVALID_PARAMETER
;
2733 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2738 /***********************************************************************
2739 * NtResetWriteWatch (NTDLL.@)
2740 * ZwResetWriteWatch (NTDLL.@)
2742 NTSTATUS WINAPI
NtResetWriteWatch( HANDLE process
, PVOID base
, SIZE_T size
)
2744 struct file_view
*view
;
2745 NTSTATUS status
= STATUS_SUCCESS
;
2748 size
= ROUND_SIZE( base
, size
);
2749 base
= ROUND_ADDR( base
, page_mask
);
2751 TRACE( "%p %p-%p\n", process
, base
, (char *)base
+ size
);
2753 if (!size
) return STATUS_INVALID_PARAMETER
;
2755 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2757 if ((view
= VIRTUAL_FindView( base
, size
)) && (view
->protect
& VPROT_WRITEWATCH
))
2758 reset_write_watches( view
, base
, size
);
2760 status
= STATUS_INVALID_PARAMETER
;
2762 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2767 /***********************************************************************
2768 * NtReadVirtualMemory (NTDLL.@)
2769 * ZwReadVirtualMemory (NTDLL.@)
2771 NTSTATUS WINAPI
NtReadVirtualMemory( HANDLE process
, const void *addr
, void *buffer
,
2772 SIZE_T size
, SIZE_T
*bytes_read
)
2776 if (virtual_check_buffer_for_write( buffer
, size
))
2778 SERVER_START_REQ( read_process_memory
)
2780 req
->handle
= wine_server_obj_handle( process
);
2781 req
->addr
= wine_server_client_ptr( addr
);
2782 wine_server_set_reply( req
, buffer
, size
);
2783 if ((status
= wine_server_call( req
))) size
= 0;
2789 status
= STATUS_ACCESS_VIOLATION
;
2792 if (bytes_read
) *bytes_read
= size
;
2797 /***********************************************************************
2798 * NtWriteVirtualMemory (NTDLL.@)
2799 * ZwWriteVirtualMemory (NTDLL.@)
2801 NTSTATUS WINAPI
NtWriteVirtualMemory( HANDLE process
, void *addr
, const void *buffer
,
2802 SIZE_T size
, SIZE_T
*bytes_written
)
2806 if (virtual_check_buffer_for_read( buffer
, size
))
2808 SERVER_START_REQ( write_process_memory
)
2810 req
->handle
= wine_server_obj_handle( process
);
2811 req
->addr
= wine_server_client_ptr( addr
);
2812 wine_server_add_data( req
, buffer
, size
);
2813 if ((status
= wine_server_call( req
))) size
= 0;
2819 status
= STATUS_PARTIAL_COPY
;
2822 if (bytes_written
) *bytes_written
= size
;
2827 /***********************************************************************
2828 * NtAreMappedFilesTheSame (NTDLL.@)
2829 * ZwAreMappedFilesTheSame (NTDLL.@)
2831 NTSTATUS WINAPI
NtAreMappedFilesTheSame(PVOID addr1
, PVOID addr2
)
2833 struct file_view
*view1
, *view2
;
2834 struct stat st1
, st2
;
2838 TRACE("%p %p\n", addr1
, addr2
);
2840 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2842 view1
= VIRTUAL_FindView( addr1
, 0 );
2843 view2
= VIRTUAL_FindView( addr2
, 0 );
2845 if (!view1
|| !view2
)
2846 status
= STATUS_INVALID_ADDRESS
;
2847 else if ((view1
->protect
& VPROT_VALLOC
) || (view2
->protect
& VPROT_VALLOC
))
2848 status
= STATUS_CONFLICTING_ADDRESSES
;
2849 else if (!(view1
->protect
& VPROT_IMAGE
) || !(view2
->protect
& VPROT_IMAGE
))
2850 status
= STATUS_NOT_SAME_DEVICE
;
2851 else if (view1
== view2
)
2852 status
= STATUS_SUCCESS
;
2853 else if (!stat_mapping_file( view1
, &st1
) && !stat_mapping_file( view2
, &st2
) &&
2854 st1
.st_dev
== st2
.st_dev
&& st1
.st_ino
== st2
.st_ino
)
2855 status
= STATUS_SUCCESS
;
2857 status
= STATUS_NOT_SAME_DEVICE
;
2859 server_leave_uninterrupted_section( &csVirtual
, &sigset
);