2 * Win32 virtual memory functions
4 * Copyright 1997, 2002 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
34 #include <sys/types.h>
35 #ifdef HAVE_SYS_STAT_H
36 # include <sys/stat.h>
38 #ifdef HAVE_SYS_MMAN_H
39 # include <sys/mman.h>
41 #ifdef HAVE_VALGRIND_VALGRIND_H
42 # include <valgrind/valgrind.h>
45 #define NONAMELESSUNION
46 #define NONAMELESSSTRUCT
48 #define WIN32_NO_STATUS
51 #include "wine/library.h"
52 #include "wine/server.h"
53 #include "wine/exception.h"
54 #include "wine/list.h"
55 #include "wine/debug.h"
56 #include "ntdll_misc.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(virtual);
59 WINE_DECLARE_DEBUG_CHANNEL(module
);
66 #define MAP_NORESERVE 0
72 struct list entry
; /* Entry in global view list */
73 void *base
; /* Base address */
74 size_t size
; /* Size in bytes */
75 HANDLE mapping
; /* Handle to the file mapping */
76 unsigned int protect
; /* Protection for all pages at allocation time */
77 BYTE prot
[1]; /* Protection byte for each page */
81 /* Conversion from VPROT_* to Win32 flags */
82 static const BYTE VIRTUAL_Win32Flags
[16] =
84 PAGE_NOACCESS
, /* 0 */
85 PAGE_READONLY
, /* READ */
86 PAGE_READWRITE
, /* WRITE */
87 PAGE_READWRITE
, /* READ | WRITE */
88 PAGE_EXECUTE
, /* EXEC */
89 PAGE_EXECUTE_READ
, /* READ | EXEC */
90 PAGE_EXECUTE_READWRITE
, /* WRITE | EXEC */
91 PAGE_EXECUTE_READWRITE
, /* READ | WRITE | EXEC */
92 PAGE_WRITECOPY
, /* WRITECOPY */
93 PAGE_WRITECOPY
, /* READ | WRITECOPY */
94 PAGE_WRITECOPY
, /* WRITE | WRITECOPY */
95 PAGE_WRITECOPY
, /* READ | WRITE | WRITECOPY */
96 PAGE_EXECUTE_WRITECOPY
, /* EXEC | WRITECOPY */
97 PAGE_EXECUTE_WRITECOPY
, /* READ | EXEC | WRITECOPY */
98 PAGE_EXECUTE_WRITECOPY
, /* WRITE | EXEC | WRITECOPY */
99 PAGE_EXECUTE_WRITECOPY
/* READ | WRITE | EXEC | WRITECOPY */
102 static struct list views_list
= LIST_INIT(views_list
);
104 static RTL_CRITICAL_SECTION csVirtual
;
105 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
108 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
109 0, 0, { (DWORD_PTR
)(__FILE__
": csVirtual") }
111 static RTL_CRITICAL_SECTION csVirtual
= { &critsect_debug
, -1, 0, 0, 0, 0 };
114 /* These are always the same on an i386, and it will be faster this way */
115 # define page_mask 0xfff
116 # define page_shift 12
117 # define page_size 0x1000
118 /* Note: these are Windows limits, you cannot change them. */
119 static void *address_space_limit
= (void *)0xc0000000; /* top of the total available address space */
120 static void *user_space_limit
= (void *)0x7fff0000; /* top of the user address space */
121 static void *working_set_limit
= (void *)0x7fff0000; /* top of the current working set */
122 static void *address_space_start
= (void *)0x110000; /* keep DOS area clear */
123 #elif defined(__x86_64__)
124 # define page_mask 0xfff
125 # define page_shift 12
126 # define page_size 0x1000
127 static void *address_space_limit
= (void *)0x7fffffff0000;
128 static void *user_space_limit
= (void *)0x7fffffff0000;
129 static void *working_set_limit
= (void *)0x7fffffff0000;
130 static void *address_space_start
= (void *)0x10000;
132 static UINT page_shift
;
133 static UINT_PTR page_size
;
134 static UINT_PTR page_mask
;
135 static void *address_space_limit
;
136 static void *user_space_limit
;
137 static void *working_set_limit
;
138 static void *address_space_start
= (void *)0x10000;
139 #endif /* __i386__ */
140 static const int is_win64
= (sizeof(void *) > sizeof(int));
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
| PROT_READ
;
188 if (vprot
& VPROT_WRITECOPY
) prot
|= PROT_WRITE
| PROT_READ
;
189 if (vprot
& VPROT_EXEC
) prot
|= PROT_EXEC
| PROT_READ
;
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
) close_handle( 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_PAGE_PROTECTION
;
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 if (force_exec_prot
&& !(vprot
& VPROT_NOEXEC
) && (vprot
& VPROT_READ
))
867 TRACE( "forcing exec permission on mapping %p-%p\n",
868 (char *)view
->base
+ start
, (char *)view
->base
+ start
+ size
- 1 );
872 /* only try mmap if media is not removable (or if we require write access) */
873 if (!removable
|| shared_write
)
875 int flags
= MAP_FIXED
| (shared_write
? MAP_SHARED
: MAP_PRIVATE
);
877 if (mmap( (char *)view
->base
+ start
, size
, prot
, flags
, fd
, offset
) != (void *)-1)
880 if ((errno
== EPERM
) && (prot
& PROT_EXEC
))
881 ERR( "failed to set %08x protection on file map, noexec filesystem?\n", prot
);
883 /* mmap() failed; if this is because the file offset is not */
884 /* page-aligned (EINVAL), or because the underlying filesystem */
885 /* does not support mmap() (ENOEXEC,ENODEV), we do it by hand. */
886 if ((errno
!= ENOEXEC
) && (errno
!= EINVAL
) && (errno
!= ENODEV
)) return FILE_GetNtStatus();
887 if (shared_write
) /* we cannot fake shared write mappings */
889 if (errno
== EINVAL
) return STATUS_INVALID_PARAMETER
;
890 ERR( "shared writable mmap not supported, broken filesystem?\n" );
891 return STATUS_NOT_SUPPORTED
;
895 /* Reserve the memory with an anonymous mmap */
896 ptr
= wine_anon_mmap( (char *)view
->base
+ start
, size
, PROT_READ
| PROT_WRITE
, MAP_FIXED
);
897 if (ptr
== (void *)-1) return FILE_GetNtStatus();
898 /* Now read in the file */
899 pread( fd
, ptr
, size
, offset
);
900 if (prot
!= (PROT_READ
|PROT_WRITE
)) mprotect( ptr
, size
, prot
); /* Set the right protection */
902 memset( view
->prot
+ (start
>> page_shift
), vprot
, ROUND_SIZE(start
,size
) >> page_shift
);
903 return STATUS_SUCCESS
;
907 /***********************************************************************
910 * Get the size of the committed range starting at base.
911 * Also return the protections for the first page.
913 static SIZE_T
get_committed_size( struct file_view
*view
, void *base
, BYTE
*vprot
)
917 start
= ((char *)base
- (char *)view
->base
) >> page_shift
;
918 *vprot
= view
->prot
[start
];
920 if (view
->mapping
&& !(view
->protect
& VPROT_COMMITTED
))
923 SERVER_START_REQ( get_mapping_committed_range
)
925 req
->handle
= wine_server_obj_handle( view
->mapping
);
926 req
->offset
= start
<< page_shift
;
927 if (!wine_server_call( req
))
930 if (reply
->committed
)
932 *vprot
|= VPROT_COMMITTED
;
933 for (i
= 0; i
< ret
>> page_shift
; i
++) view
->prot
[start
+i
] |= VPROT_COMMITTED
;
940 for (i
= start
+ 1; i
< view
->size
>> page_shift
; i
++)
941 if ((*vprot
^ view
->prot
[i
]) & VPROT_COMMITTED
) break;
942 return (i
- start
) << page_shift
;
946 /***********************************************************************
949 * Decommit some pages of a given view.
950 * The csVirtual section must be held by caller.
952 static NTSTATUS
decommit_pages( struct file_view
*view
, size_t start
, size_t size
)
954 if (wine_anon_mmap( (char *)view
->base
+ start
, size
, PROT_NONE
, MAP_FIXED
) != (void *)-1)
956 BYTE
*p
= view
->prot
+ (start
>> page_shift
);
958 while (size
--) *p
++ &= ~VPROT_COMMITTED
;
959 return STATUS_SUCCESS
;
961 return FILE_GetNtStatus();
965 /***********************************************************************
966 * allocate_dos_memory
968 * Allocate the DOS memory range.
970 static NTSTATUS
allocate_dos_memory( struct file_view
**view
, unsigned int vprot
)
974 void * const low_64k
= (void *)0x10000;
975 const size_t dosmem_size
= 0x110000;
976 int unix_prot
= VIRTUAL_GetUnixProt( vprot
);
979 /* check for existing view */
981 if ((ptr
= list_head( &views_list
)))
983 struct file_view
*first_view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
984 if (first_view
->base
< (void *)dosmem_size
) return STATUS_CONFLICTING_ADDRESSES
;
987 /* check without the first 64K */
989 if (wine_mmap_is_in_reserved_area( low_64k
, dosmem_size
- 0x10000 ) != 1)
991 addr
= wine_anon_mmap( low_64k
, dosmem_size
- 0x10000, unix_prot
, 0 );
994 if (addr
!= (void *)-1) munmap( addr
, dosmem_size
- 0x10000 );
995 return map_view( view
, NULL
, dosmem_size
, 0xffff, 0, vprot
);
999 /* now try to allocate the low 64K too */
1001 if (wine_mmap_is_in_reserved_area( NULL
, 0x10000 ) != 1)
1003 addr
= wine_anon_mmap( (void *)page_size
, 0x10000 - page_size
, unix_prot
, 0 );
1004 if (addr
== (void *)page_size
)
1006 if (!wine_anon_mmap( NULL
, page_size
, unix_prot
, MAP_FIXED
))
1009 TRACE( "successfully mapped low 64K range\n" );
1011 else TRACE( "failed to map page 0\n" );
1015 if (addr
!= (void *)-1) munmap( addr
, 0x10000 - page_size
);
1017 TRACE( "failed to map low 64K range\n" );
1021 /* now reserve the whole range */
1023 size
= (char *)dosmem_size
- (char *)addr
;
1024 wine_anon_mmap( addr
, size
, unix_prot
, MAP_FIXED
);
1025 return create_view( view
, addr
, size
, vprot
);
1029 /***********************************************************************
1030 * check_architecture
1032 * Check the architecture of a PE binary.
1034 static NTSTATUS
check_architecture( const IMAGE_NT_HEADERS
*nt
)
1036 static const char *arch
;
1039 if (nt
->FileHeader
.Machine
== IMAGE_FILE_MACHINE_I386
) return STATUS_SUCCESS
;
1040 if (nt
->FileHeader
.Machine
== IMAGE_FILE_MACHINE_AMD64
)
1042 if (nt
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
) /* don't warn for a 64-bit exe */
1043 WARN( "loading amd64 dll in 32-bit mode will fail\n" );
1044 return STATUS_INVALID_IMAGE_FORMAT
;
1046 #elif defined(__x86_64__)
1047 if (nt
->FileHeader
.Machine
== IMAGE_FILE_MACHINE_AMD64
) return STATUS_SUCCESS
;
1048 if (nt
->FileHeader
.Machine
== IMAGE_FILE_MACHINE_I386
)
1050 if (nt
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
) /* don't warn for a 32-bit exe */
1051 WARN( "loading 32-bit dll in 64-bit mode will fail\n" );
1052 return STATUS_INVALID_IMAGE_FORMAT
;
1054 #elif defined(__arm__) && !defined(__ARMEB__)
1055 if (nt
->FileHeader
.Machine
== IMAGE_FILE_MACHINE_ARM
||
1056 #if defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__)
1057 nt
->FileHeader
.Machine
== IMAGE_FILE_MACHINE_ARMV7
||
1059 nt
->FileHeader
.Machine
== IMAGE_FILE_MACHINE_THUMB
)
1060 return STATUS_SUCCESS
;
1063 switch (nt
->FileHeader
.Machine
)
1065 case IMAGE_FILE_MACHINE_UNKNOWN
: arch
= "Unknown"; break;
1066 case IMAGE_FILE_MACHINE_I860
: arch
= "I860"; break;
1067 case IMAGE_FILE_MACHINE_I386
: arch
= "I386"; break;
1068 case IMAGE_FILE_MACHINE_R3000
: arch
= "R3000"; break;
1069 case IMAGE_FILE_MACHINE_R4000
: arch
= "R4000"; break;
1070 case IMAGE_FILE_MACHINE_R10000
: arch
= "R10000"; break;
1071 case IMAGE_FILE_MACHINE_ALPHA
: arch
= "Alpha"; break;
1072 case IMAGE_FILE_MACHINE_POWERPC
: arch
= "PowerPC"; break;
1073 case IMAGE_FILE_MACHINE_IA64
: arch
= "IA-64"; break;
1074 case IMAGE_FILE_MACHINE_ALPHA64
: arch
= "Alpha-64"; break;
1075 case IMAGE_FILE_MACHINE_AMD64
: arch
= "AMD-64"; break;
1076 case IMAGE_FILE_MACHINE_ARM
: arch
= "ARM"; break;
1077 case IMAGE_FILE_MACHINE_ARMV7
: arch
= "ARMv7"; break;
1078 case IMAGE_FILE_MACHINE_THUMB
: arch
= "ARM Thumb"; break;
1079 case IMAGE_FILE_MACHINE_SPARC
: arch
= "SPARC"; break;
1080 default: arch
= wine_dbg_sprintf( "Unknown-%04x", nt
->FileHeader
.Machine
); break;
1082 ERR( "Trying to load PE image for unsupported architecture %s\n", arch
);
1083 return STATUS_INVALID_IMAGE_FORMAT
;
1087 /***********************************************************************
1090 * Stat the underlying file for a memory view.
1092 static NTSTATUS
stat_mapping_file( struct file_view
*view
, struct stat
*st
)
1095 int unix_fd
, needs_close
;
1097 if (!view
->mapping
) return STATUS_NOT_MAPPED_VIEW
;
1098 if (!(status
= server_get_unix_fd( view
->mapping
, 0, &unix_fd
, &needs_close
, NULL
, NULL
)))
1100 if (fstat( unix_fd
, st
) == -1) status
= FILE_GetNtStatus();
1101 if (needs_close
) close( unix_fd
);
1107 /***********************************************************************
1110 * Map an executable (PE format) image into memory.
1112 static NTSTATUS
map_image( HANDLE hmapping
, int fd
, char *base
, SIZE_T total_size
, SIZE_T mask
,
1113 SIZE_T header_size
, int shared_fd
, HANDLE dup_mapping
, PVOID
*addr_ptr
)
1115 IMAGE_DOS_HEADER
*dos
;
1116 IMAGE_NT_HEADERS
*nt
;
1117 IMAGE_SECTION_HEADER
*sec
;
1118 IMAGE_DATA_DIRECTORY
*imports
;
1119 NTSTATUS status
= STATUS_CONFLICTING_ADDRESSES
;
1124 struct file_view
*view
= NULL
;
1125 char *ptr
, *header_end
;
1128 /* zero-map the whole range */
1130 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1132 if (base
>= (char *)address_space_start
) /* make sure the DOS area remains free */
1133 status
= map_view( &view
, base
, total_size
, mask
, FALSE
,
1134 VPROT_COMMITTED
| VPROT_READ
| VPROT_EXEC
| VPROT_WRITECOPY
| VPROT_IMAGE
);
1136 if (status
!= STATUS_SUCCESS
)
1137 status
= map_view( &view
, NULL
, total_size
, mask
, FALSE
,
1138 VPROT_COMMITTED
| VPROT_READ
| VPROT_EXEC
| VPROT_WRITECOPY
| VPROT_IMAGE
);
1140 if (status
!= STATUS_SUCCESS
) goto error
;
1143 TRACE_(module
)( "mapped PE file at %p-%p\n", ptr
, ptr
+ total_size
);
1145 /* map the header */
1147 if (fstat( fd
, &st
) == -1)
1149 status
= FILE_GetNtStatus();
1152 status
= STATUS_INVALID_IMAGE_FORMAT
; /* generic error */
1153 if (!st
.st_size
) goto error
;
1154 header_size
= min( header_size
, st
.st_size
);
1155 if (map_file_into_view( view
, fd
, 0, header_size
, 0, VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
,
1156 !dup_mapping
) != STATUS_SUCCESS
) goto error
;
1157 dos
= (IMAGE_DOS_HEADER
*)ptr
;
1158 nt
= (IMAGE_NT_HEADERS
*)(ptr
+ dos
->e_lfanew
);
1159 header_end
= ptr
+ ROUND_SIZE( 0, header_size
);
1160 memset( ptr
+ header_size
, 0, header_end
- (ptr
+ header_size
) );
1161 if ((char *)(nt
+ 1) > header_end
) goto error
;
1162 sec
= (IMAGE_SECTION_HEADER
*)((char*)&nt
->OptionalHeader
+nt
->FileHeader
.SizeOfOptionalHeader
);
1163 if ((char *)(sec
+ nt
->FileHeader
.NumberOfSections
) > header_end
) goto error
;
1165 imports
= nt
->OptionalHeader
.DataDirectory
+ IMAGE_DIRECTORY_ENTRY_IMPORT
;
1166 if (!imports
->Size
|| !imports
->VirtualAddress
) imports
= NULL
;
1168 if (check_architecture( nt
)) goto error
;
1170 /* check for non page-aligned binary */
1172 if (nt
->OptionalHeader
.SectionAlignment
<= page_mask
)
1174 /* unaligned sections, this happens for native subsystem binaries */
1175 /* in that case Windows simply maps in the whole file */
1177 if (map_file_into_view( view
, fd
, 0, total_size
, 0, VPROT_COMMITTED
| VPROT_READ
,
1178 !dup_mapping
) != STATUS_SUCCESS
) goto error
;
1180 /* check that all sections are loaded at the right offset */
1181 if (nt
->OptionalHeader
.FileAlignment
!= nt
->OptionalHeader
.SectionAlignment
) goto error
;
1182 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++)
1184 if (sec
[i
].VirtualAddress
!= sec
[i
].PointerToRawData
)
1185 goto error
; /* Windows refuses to load in that case too */
1188 /* set the image protections */
1189 VIRTUAL_SetProt( view
, ptr
, total_size
,
1190 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
| VPROT_EXEC
);
1192 /* no relocations are performed on non page-aligned binaries */
1197 /* map all the sections */
1199 for (i
= pos
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
1201 static const SIZE_T sector_align
= 0x1ff;
1202 SIZE_T map_size
, file_start
, file_size
, end
;
1204 if (!sec
->Misc
.VirtualSize
)
1205 map_size
= ROUND_SIZE( 0, sec
->SizeOfRawData
);
1207 map_size
= ROUND_SIZE( 0, sec
->Misc
.VirtualSize
);
1209 /* file positions are rounded to sector boundaries regardless of OptionalHeader.FileAlignment */
1210 file_start
= sec
->PointerToRawData
& ~sector_align
;
1211 file_size
= (sec
->SizeOfRawData
+ (sec
->PointerToRawData
& sector_align
) + sector_align
) & ~sector_align
;
1212 if (file_size
> map_size
) file_size
= map_size
;
1214 /* a few sanity checks */
1215 end
= sec
->VirtualAddress
+ ROUND_SIZE( sec
->VirtualAddress
, map_size
);
1216 if (sec
->VirtualAddress
> total_size
|| end
> total_size
|| end
< sec
->VirtualAddress
)
1218 WARN_(module
)( "Section %.8s too large (%x+%lx/%lx)\n",
1219 sec
->Name
, sec
->VirtualAddress
, map_size
, total_size
);
1223 if ((sec
->Characteristics
& IMAGE_SCN_MEM_SHARED
) &&
1224 (sec
->Characteristics
& IMAGE_SCN_MEM_WRITE
))
1226 TRACE_(module
)( "mapping shared section %.8s at %p off %x (%x) size %lx (%lx) flags %x\n",
1227 sec
->Name
, ptr
+ sec
->VirtualAddress
,
1228 sec
->PointerToRawData
, (int)pos
, file_size
, map_size
,
1229 sec
->Characteristics
);
1230 if (map_file_into_view( view
, shared_fd
, sec
->VirtualAddress
, map_size
, pos
,
1231 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITE
,
1232 FALSE
) != STATUS_SUCCESS
)
1234 ERR_(module
)( "Could not map shared section %.8s\n", sec
->Name
);
1238 /* check if the import directory falls inside this section */
1239 if (imports
&& imports
->VirtualAddress
>= sec
->VirtualAddress
&&
1240 imports
->VirtualAddress
< sec
->VirtualAddress
+ map_size
)
1242 UINT_PTR base
= imports
->VirtualAddress
& ~page_mask
;
1243 UINT_PTR end
= base
+ ROUND_SIZE( imports
->VirtualAddress
, imports
->Size
);
1244 if (end
> sec
->VirtualAddress
+ map_size
) end
= sec
->VirtualAddress
+ map_size
;
1246 map_file_into_view( view
, shared_fd
, base
, end
- base
,
1247 pos
+ (base
- sec
->VirtualAddress
),
1248 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
,
1255 TRACE_(module
)( "mapping section %.8s at %p off %x size %x virt %x flags %x\n",
1256 sec
->Name
, ptr
+ sec
->VirtualAddress
,
1257 sec
->PointerToRawData
, sec
->SizeOfRawData
,
1258 sec
->Misc
.VirtualSize
, sec
->Characteristics
);
1260 if (!sec
->PointerToRawData
|| !file_size
) continue;
1262 /* Note: if the section is not aligned properly map_file_into_view will magically
1263 * fall back to read(), so we don't need to check anything here.
1265 end
= file_start
+ file_size
;
1266 if (sec
->PointerToRawData
>= st
.st_size
||
1267 end
> ((st
.st_size
+ sector_align
) & ~sector_align
) ||
1269 map_file_into_view( view
, fd
, sec
->VirtualAddress
, file_size
, file_start
,
1270 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
,
1271 !dup_mapping
) != STATUS_SUCCESS
)
1273 ERR_(module
)( "Could not map section %.8s, file probably truncated\n", sec
->Name
);
1277 if (file_size
& page_mask
)
1279 end
= ROUND_SIZE( 0, file_size
);
1280 if (end
> map_size
) end
= map_size
;
1281 TRACE_(module
)("clearing %p - %p\n",
1282 ptr
+ sec
->VirtualAddress
+ file_size
,
1283 ptr
+ sec
->VirtualAddress
+ end
);
1284 memset( ptr
+ sec
->VirtualAddress
+ file_size
, 0, end
- file_size
);
1289 /* perform base relocation, if necessary */
1292 ((nt
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
) ||
1293 !NtCurrentTeb()->Peb
->ImageBaseAddress
) )
1295 IMAGE_BASE_RELOCATION
*rel
, *end
;
1296 const IMAGE_DATA_DIRECTORY
*relocs
;
1298 if (nt
->FileHeader
.Characteristics
& IMAGE_FILE_RELOCS_STRIPPED
)
1300 WARN_(module
)( "Need to relocate module from %p to %p, but there are no relocation records\n",
1302 status
= STATUS_CONFLICTING_ADDRESSES
;
1306 TRACE_(module
)( "relocating from %p-%p to %p-%p\n",
1307 base
, base
+ total_size
, ptr
, ptr
+ total_size
);
1309 relocs
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1310 rel
= (IMAGE_BASE_RELOCATION
*)(ptr
+ relocs
->VirtualAddress
);
1311 end
= (IMAGE_BASE_RELOCATION
*)(ptr
+ relocs
->VirtualAddress
+ relocs
->Size
);
1314 while (rel
< end
- 1 && rel
->SizeOfBlock
)
1316 if (rel
->VirtualAddress
>= total_size
)
1318 WARN_(module
)( "invalid address %p in relocation %p\n", ptr
+ rel
->VirtualAddress
, rel
);
1319 status
= STATUS_ACCESS_VIOLATION
;
1322 rel
= LdrProcessRelocationBlock( ptr
+ rel
->VirtualAddress
,
1323 (rel
->SizeOfBlock
- sizeof(*rel
)) / sizeof(USHORT
),
1324 (USHORT
*)(rel
+ 1), delta
);
1325 if (!rel
) goto error
;
1329 /* set the image protections */
1331 VIRTUAL_SetProt( view
, ptr
, ROUND_SIZE( 0, header_size
), VPROT_COMMITTED
| VPROT_READ
);
1333 sec
= (IMAGE_SECTION_HEADER
*)((char *)&nt
->OptionalHeader
+nt
->FileHeader
.SizeOfOptionalHeader
);
1334 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
1337 BYTE vprot
= VPROT_COMMITTED
;
1339 if (sec
->Misc
.VirtualSize
)
1340 size
= ROUND_SIZE( sec
->VirtualAddress
, sec
->Misc
.VirtualSize
);
1342 size
= ROUND_SIZE( sec
->VirtualAddress
, sec
->SizeOfRawData
);
1344 if (sec
->Characteristics
& IMAGE_SCN_MEM_READ
) vprot
|= VPROT_READ
;
1345 if (sec
->Characteristics
& IMAGE_SCN_MEM_WRITE
) vprot
|= VPROT_WRITECOPY
;
1346 if (sec
->Characteristics
& IMAGE_SCN_MEM_EXECUTE
) vprot
|= VPROT_EXEC
;
1348 /* Dumb game crack lets the AOEP point into a data section. Adjust. */
1349 if ((nt
->OptionalHeader
.AddressOfEntryPoint
>= sec
->VirtualAddress
) &&
1350 (nt
->OptionalHeader
.AddressOfEntryPoint
< sec
->VirtualAddress
+ size
))
1351 vprot
|= VPROT_EXEC
;
1353 if (!VIRTUAL_SetProt( view
, ptr
+ sec
->VirtualAddress
, size
, vprot
) && (vprot
& VPROT_EXEC
))
1354 ERR( "failed to set %08x protection on section %.8s, noexec filesystem?\n",
1355 sec
->Characteristics
, sec
->Name
);
1359 view
->mapping
= dup_mapping
;
1360 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1363 #ifdef VALGRIND_LOAD_PDB_DEBUGINFO
1364 VALGRIND_LOAD_PDB_DEBUGINFO(fd
, ptr
, total_size
, delta
);
1366 if (ptr
!= base
) return STATUS_IMAGE_NOT_AT_BASE
;
1367 return STATUS_SUCCESS
;
1370 if (view
) delete_view( view
);
1371 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1372 if (dup_mapping
) NtClose( dup_mapping
);
1377 /* callback for wine_mmap_enum_reserved_areas to allocate space for the virtual heap */
1378 static int alloc_virtual_heap( void *base
, size_t size
, void *arg
)
1380 void **heap_base
= arg
;
1382 if (is_beyond_limit( base
, size
, address_space_limit
)) address_space_limit
= (char *)base
+ size
;
1383 if (size
< VIRTUAL_HEAP_SIZE
) return 0;
1384 if (is_win64
&& base
< (void *)0x80000000) return 0;
1385 *heap_base
= wine_anon_mmap( (char *)base
+ size
- VIRTUAL_HEAP_SIZE
,
1386 VIRTUAL_HEAP_SIZE
, PROT_READ
|PROT_WRITE
, MAP_FIXED
);
1387 return (*heap_base
!= (void *)-1);
1390 /***********************************************************************
1393 void virtual_init(void)
1395 const char *preload
;
1398 struct file_view
*heap_view
;
1401 page_size
= getpagesize();
1402 page_mask
= page_size
- 1;
1403 /* Make sure we have a power of 2 */
1404 assert( !(page_size
& page_mask
) );
1406 while ((1 << page_shift
) != page_size
) page_shift
++;
1407 user_space_limit
= working_set_limit
= address_space_limit
= (void *)~page_mask
;
1408 #endif /* page_mask */
1409 if ((preload
= getenv("WINEPRELOADRESERVE")))
1411 unsigned long start
, end
;
1412 if (sscanf( preload
, "%lx-%lx", &start
, &end
) == 2)
1414 preload_reserve_start
= (void *)start
;
1415 preload_reserve_end
= (void *)end
;
1419 /* try to find space in a reserved area for the virtual heap */
1420 if (!wine_mmap_enum_reserved_areas( alloc_virtual_heap
, &heap_base
, 1 ))
1421 heap_base
= wine_anon_mmap( NULL
, VIRTUAL_HEAP_SIZE
, PROT_READ
|PROT_WRITE
, 0 );
1423 assert( heap_base
!= (void *)-1 );
1424 virtual_heap
= RtlCreateHeap( HEAP_NO_SERIALIZE
, heap_base
, VIRTUAL_HEAP_SIZE
,
1425 VIRTUAL_HEAP_SIZE
, NULL
, NULL
);
1426 create_view( &heap_view
, heap_base
, VIRTUAL_HEAP_SIZE
, VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITE
);
1428 /* make the DOS area accessible (except the low 64K) to hide bugs in broken apps like Excel 2003 */
1429 size
= (char *)address_space_start
- (char *)0x10000;
1430 if (size
&& wine_mmap_is_in_reserved_area( (void*)0x10000, size
) == 1)
1431 wine_anon_mmap( (void *)0x10000, size
, PROT_READ
| PROT_WRITE
, MAP_FIXED
);
1435 /***********************************************************************
1436 * virtual_init_threading
1438 void virtual_init_threading(void)
1444 /***********************************************************************
1445 * virtual_get_system_info
1447 void virtual_get_system_info( SYSTEM_BASIC_INFORMATION
*info
)
1450 info
->KeMaximumIncrement
= 0; /* FIXME */
1451 info
->PageSize
= page_size
;
1452 info
->MmLowestPhysicalPage
= 1;
1453 info
->MmHighestPhysicalPage
= 0x7fffffff / page_size
;
1454 info
->MmNumberOfPhysicalPages
= info
->MmHighestPhysicalPage
- info
->MmLowestPhysicalPage
;
1455 info
->AllocationGranularity
= get_mask(0) + 1;
1456 info
->LowestUserAddress
= (void *)0x10000;
1457 info
->HighestUserAddress
= (char *)user_space_limit
- 1;
1458 info
->ActiveProcessorsAffinityMask
= (1 << NtCurrentTeb()->Peb
->NumberOfProcessors
) - 1;
1459 info
->NumberOfProcessors
= NtCurrentTeb()->Peb
->NumberOfProcessors
;
1463 /***********************************************************************
1464 * virtual_create_builtin_view
1466 NTSTATUS
virtual_create_builtin_view( void *module
)
1470 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( module
);
1471 SIZE_T size
= nt
->OptionalHeader
.SizeOfImage
;
1472 IMAGE_SECTION_HEADER
*sec
;
1473 struct file_view
*view
;
1477 size
= ROUND_SIZE( module
, size
);
1478 base
= ROUND_ADDR( module
, page_mask
);
1479 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1480 status
= create_view( &view
, base
, size
, VPROT_SYSTEM
| VPROT_IMAGE
|
1481 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
| VPROT_EXEC
);
1482 if (!status
) TRACE( "created %p-%p\n", base
, (char *)base
+ size
);
1483 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1485 if (status
) return status
;
1487 /* The PE header is always read-only, no write, no execute. */
1488 view
->prot
[0] = VPROT_COMMITTED
| VPROT_READ
;
1490 sec
= (IMAGE_SECTION_HEADER
*)((char *)&nt
->OptionalHeader
+ nt
->FileHeader
.SizeOfOptionalHeader
);
1491 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++)
1493 BYTE flags
= VPROT_COMMITTED
;
1495 if (sec
[i
].Characteristics
& IMAGE_SCN_MEM_EXECUTE
) flags
|= VPROT_EXEC
;
1496 if (sec
[i
].Characteristics
& IMAGE_SCN_MEM_READ
) flags
|= VPROT_READ
;
1497 if (sec
[i
].Characteristics
& IMAGE_SCN_MEM_WRITE
) flags
|= VPROT_WRITE
;
1498 memset (view
->prot
+ (sec
[i
].VirtualAddress
>> page_shift
), flags
,
1499 ROUND_SIZE( sec
[i
].VirtualAddress
, sec
[i
].Misc
.VirtualSize
) >> page_shift
);
1506 /***********************************************************************
1507 * virtual_alloc_thread_stack
1509 NTSTATUS
virtual_alloc_thread_stack( TEB
*teb
, SIZE_T reserve_size
, SIZE_T commit_size
)
1511 struct file_view
*view
;
1516 if (!reserve_size
|| !commit_size
)
1518 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( NtCurrentTeb()->Peb
->ImageBaseAddress
);
1519 if (!reserve_size
) reserve_size
= nt
->OptionalHeader
.SizeOfStackReserve
;
1520 if (!commit_size
) commit_size
= nt
->OptionalHeader
.SizeOfStackCommit
;
1523 size
= max( reserve_size
, commit_size
);
1524 if (size
< 1024 * 1024) size
= 1024 * 1024; /* Xlib needs a large stack */
1525 size
= (size
+ 0xffff) & ~0xffff; /* round to 64K boundary */
1527 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1529 if ((status
= map_view( &view
, NULL
, size
, 0xffff, 0,
1530 VPROT_READ
| VPROT_WRITE
| VPROT_COMMITTED
| VPROT_VALLOC
)) != STATUS_SUCCESS
)
1533 #ifdef VALGRIND_STACK_REGISTER
1534 VALGRIND_STACK_REGISTER( view
->base
, (char *)view
->base
+ view
->size
);
1537 /* setup no access guard page */
1538 VIRTUAL_SetProt( view
, view
->base
, page_size
, VPROT_COMMITTED
);
1539 VIRTUAL_SetProt( view
, (char *)view
->base
+ page_size
, page_size
,
1540 VPROT_READ
| VPROT_WRITE
| VPROT_COMMITTED
| VPROT_GUARD
);
1542 /* note: limit is lower than base since the stack grows down */
1543 teb
->DeallocationStack
= view
->base
;
1544 teb
->Tib
.StackBase
= (char *)view
->base
+ view
->size
;
1545 teb
->Tib
.StackLimit
= (char *)view
->base
+ 2 * page_size
;
1547 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1552 /***********************************************************************
1553 * virtual_clear_thread_stack
1555 * Clear the stack contents before calling the main entry point, some broken apps need that.
1557 void virtual_clear_thread_stack(void)
1559 void *stack
= NtCurrentTeb()->Tib
.StackLimit
;
1560 size_t size
= (char *)NtCurrentTeb()->Tib
.StackBase
- (char *)NtCurrentTeb()->Tib
.StackLimit
;
1562 wine_anon_mmap( stack
, size
, PROT_READ
| PROT_WRITE
, MAP_FIXED
);
1563 if (force_exec_prot
) mprotect( stack
, size
, PROT_READ
| PROT_WRITE
| PROT_EXEC
);
1567 /***********************************************************************
1568 * virtual_handle_fault
1570 NTSTATUS
virtual_handle_fault( LPCVOID addr
, DWORD err
)
1572 struct file_view
*view
;
1573 NTSTATUS ret
= STATUS_ACCESS_VIOLATION
;
1576 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1577 if ((view
= VIRTUAL_FindView( addr
, 0 )))
1579 void *page
= ROUND_ADDR( addr
, page_mask
);
1580 BYTE
*vprot
= &view
->prot
[((const char *)page
- (const char *)view
->base
) >> page_shift
];
1581 if (*vprot
& VPROT_GUARD
)
1583 VIRTUAL_SetProt( view
, page
, page_size
, *vprot
& ~VPROT_GUARD
);
1584 ret
= STATUS_GUARD_PAGE_VIOLATION
;
1586 if ((err
& EXCEPTION_WRITE_FAULT
) && (view
->protect
& VPROT_WRITEWATCH
))
1588 if (*vprot
& VPROT_WRITEWATCH
)
1590 *vprot
&= ~VPROT_WRITEWATCH
;
1591 VIRTUAL_SetProt( view
, page
, page_size
, *vprot
);
1593 /* ignore fault if page is writable now */
1594 if (VIRTUAL_GetUnixProt( *vprot
) & PROT_WRITE
) ret
= STATUS_SUCCESS
;
1597 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1603 /***********************************************************************
1604 * virtual_handle_stack_fault
1606 * Handle an access fault inside the current thread stack.
1607 * Called from inside a signal handler.
1609 BOOL
virtual_handle_stack_fault( void *addr
)
1611 struct file_view
*view
;
1614 RtlEnterCriticalSection( &csVirtual
); /* no need for signal masking inside signal handler */
1615 if ((view
= VIRTUAL_FindView( addr
, 0 )))
1617 void *page
= ROUND_ADDR( addr
, page_mask
);
1618 BYTE vprot
= view
->prot
[((const char *)page
- (const char *)view
->base
) >> page_shift
];
1619 if (vprot
& VPROT_GUARD
)
1621 VIRTUAL_SetProt( view
, page
, page_size
, vprot
& ~VPROT_GUARD
);
1622 NtCurrentTeb()->Tib
.StackLimit
= page
;
1623 if ((char *)page
>= (char *)NtCurrentTeb()->DeallocationStack
+ 2*page_size
)
1625 vprot
= view
->prot
[((char *)page
- page_size
- (char *)view
->base
) >> page_shift
];
1626 VIRTUAL_SetProt( view
, (char *)page
- page_size
, page_size
, vprot
| VPROT_GUARD
);
1631 RtlLeaveCriticalSection( &csVirtual
);
1636 /***********************************************************************
1637 * virtual_check_buffer_for_read
1639 * Check if a memory buffer can be read, triggering page faults if needed for DIB section access.
1641 BOOL
virtual_check_buffer_for_read( const void *ptr
, SIZE_T size
)
1643 if (!size
) return TRUE
;
1644 if (!ptr
) return FALSE
;
1648 volatile const char *p
= ptr
;
1649 char dummy
__attribute__((unused
));
1650 SIZE_T count
= size
;
1652 while (count
> page_size
)
1659 dummy
= p
[count
- 1];
1670 /***********************************************************************
1671 * virtual_check_buffer_for_write
1673 * Check if a memory buffer can be written to, triggering page faults if needed for write watches.
1675 BOOL
virtual_check_buffer_for_write( void *ptr
, SIZE_T size
)
1677 if (!size
) return TRUE
;
1678 if (!ptr
) return FALSE
;
1682 volatile char *p
= ptr
;
1683 SIZE_T count
= size
;
1685 while (count
> page_size
)
1703 /***********************************************************************
1704 * VIRTUAL_SetForceExec
1706 * Whether to force exec prot on all views.
1708 void VIRTUAL_SetForceExec( BOOL enable
)
1710 struct file_view
*view
;
1713 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1714 if (!force_exec_prot
!= !enable
) /* change all existing views */
1716 force_exec_prot
= enable
;
1718 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
1721 char *addr
= view
->base
;
1722 BYTE commit
= view
->mapping
? VPROT_COMMITTED
: 0; /* file mappings are always accessible */
1723 int unix_prot
= VIRTUAL_GetUnixProt( view
->prot
[0] | commit
);
1725 if (view
->protect
& VPROT_NOEXEC
) continue;
1726 for (count
= i
= 1; i
< view
->size
>> page_shift
; i
++, count
++)
1728 int prot
= VIRTUAL_GetUnixProt( view
->prot
[i
] | commit
);
1729 if (prot
== unix_prot
) continue;
1730 if ((unix_prot
& PROT_READ
) && !(unix_prot
& PROT_EXEC
))
1732 TRACE( "%s exec prot for %p-%p\n",
1733 force_exec_prot
? "enabling" : "disabling",
1734 addr
, addr
+ (count
<< page_shift
) - 1 );
1735 mprotect( addr
, count
<< page_shift
,
1736 unix_prot
| (force_exec_prot
? PROT_EXEC
: 0) );
1738 addr
+= (count
<< page_shift
);
1744 if ((unix_prot
& PROT_READ
) && !(unix_prot
& PROT_EXEC
))
1746 TRACE( "%s exec prot for %p-%p\n",
1747 force_exec_prot
? "enabling" : "disabling",
1748 addr
, addr
+ (count
<< page_shift
) - 1 );
1749 mprotect( addr
, count
<< page_shift
,
1750 unix_prot
| (force_exec_prot
? PROT_EXEC
: 0) );
1755 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1764 /* free reserved areas above the limit; callback for wine_mmap_enum_reserved_areas */
1765 static int free_reserved_memory( void *base
, size_t size
, void *arg
)
1767 struct free_range
*range
= arg
;
1769 if ((char *)base
>= range
->limit
) return 0;
1770 if ((char *)base
+ size
<= range
->base
) return 0;
1771 if ((char *)base
< range
->base
)
1773 size
-= range
->base
- (char *)base
;
1776 if ((char *)base
+ size
> range
->limit
) size
= range
->limit
- (char *)base
;
1777 remove_reserved_area( base
, size
);
1778 return 1; /* stop enumeration since the list has changed */
1781 /***********************************************************************
1782 * virtual_release_address_space
1784 * Release some address space once we have loaded and initialized the app.
1786 void virtual_release_address_space( BOOL free_high_mem
)
1788 struct free_range range
;
1791 if (user_space_limit
== address_space_limit
) return; /* no need to free anything */
1793 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1795 /* no large address space on win9x */
1796 if (free_high_mem
&& NtCurrentTeb()->Peb
->OSPlatformId
== VER_PLATFORM_WIN32_NT
)
1798 range
.base
= (char *)0x82000000;
1799 range
.limit
= address_space_limit
;
1800 while (wine_mmap_enum_reserved_areas( free_reserved_memory
, &range
, 1 )) /* nothing */;
1801 user_space_limit
= working_set_limit
= address_space_limit
;
1805 #ifndef __APPLE__ /* dyld doesn't support parts of the WINE_DOS segment being unmapped */
1806 range
.base
= (char *)0x20000000;
1807 range
.limit
= (char *)0x7f000000;
1808 while (wine_mmap_enum_reserved_areas( free_reserved_memory
, &range
, 0 )) /* nothing */;
1812 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1816 /***********************************************************************
1817 * NtAllocateVirtualMemory (NTDLL.@)
1818 * ZwAllocateVirtualMemory (NTDLL.@)
1820 NTSTATUS WINAPI
NtAllocateVirtualMemory( HANDLE process
, PVOID
*ret
, ULONG zero_bits
,
1821 SIZE_T
*size_ptr
, ULONG type
, ULONG protect
)
1825 SIZE_T size
= *size_ptr
;
1826 SIZE_T mask
= get_mask( zero_bits
);
1827 NTSTATUS status
= STATUS_SUCCESS
;
1828 struct file_view
*view
;
1831 TRACE("%p %p %08lx %x %08x\n", process
, *ret
, size
, type
, protect
);
1833 if (!size
) return STATUS_INVALID_PARAMETER
;
1835 if (process
!= NtCurrentProcess())
1838 apc_result_t result
;
1840 memset( &call
, 0, sizeof(call
) );
1842 call
.virtual_alloc
.type
= APC_VIRTUAL_ALLOC
;
1843 call
.virtual_alloc
.addr
= wine_server_client_ptr( *ret
);
1844 call
.virtual_alloc
.size
= *size_ptr
;
1845 call
.virtual_alloc
.zero_bits
= zero_bits
;
1846 call
.virtual_alloc
.op_type
= type
;
1847 call
.virtual_alloc
.prot
= protect
;
1848 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
1849 if (status
!= STATUS_SUCCESS
) return status
;
1851 if (result
.virtual_alloc
.status
== STATUS_SUCCESS
)
1853 *ret
= wine_server_get_ptr( result
.virtual_alloc
.addr
);
1854 *size_ptr
= result
.virtual_alloc
.size
;
1856 return result
.virtual_alloc
.status
;
1859 /* Round parameters to a page boundary */
1861 if (is_beyond_limit( 0, size
, working_set_limit
)) return STATUS_WORKING_SET_LIMIT_RANGE
;
1863 if ((status
= get_vprot_flags( protect
, &vprot
))) return status
;
1864 if (vprot
& VPROT_WRITECOPY
) return STATUS_INVALID_PAGE_PROTECTION
;
1865 vprot
|= VPROT_VALLOC
;
1866 if (type
& MEM_COMMIT
) vprot
|= VPROT_COMMITTED
;
1870 if (type
& MEM_RESERVE
) /* Round down to 64k boundary */
1871 base
= ROUND_ADDR( *ret
, mask
);
1873 base
= ROUND_ADDR( *ret
, page_mask
);
1874 size
= (((UINT_PTR
)*ret
+ size
+ page_mask
) & ~page_mask
) - (UINT_PTR
)base
;
1876 /* address 1 is magic to mean DOS area */
1877 if (!base
&& *ret
== (void *)1 && size
== 0x110000)
1879 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1880 status
= allocate_dos_memory( &view
, vprot
);
1881 if (status
== STATUS_SUCCESS
)
1884 *size_ptr
= view
->size
;
1886 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1890 /* disallow low 64k, wrap-around and kernel space */
1891 if (((char *)base
< (char *)0x10000) ||
1892 ((char *)base
+ size
< (char *)base
) ||
1893 is_beyond_limit( base
, size
, address_space_limit
))
1894 return STATUS_INVALID_PARAMETER
;
1899 size
= (size
+ page_mask
) & ~page_mask
;
1902 /* Compute the alloc type flags */
1904 if (!(type
& (MEM_COMMIT
| MEM_RESERVE
| MEM_RESET
)) ||
1905 (type
& ~(MEM_COMMIT
| MEM_RESERVE
| MEM_TOP_DOWN
| MEM_WRITE_WATCH
| MEM_RESET
)))
1907 WARN("called with wrong alloc type flags (%08x) !\n", type
);
1908 return STATUS_INVALID_PARAMETER
;
1911 /* Reserve the memory */
1913 if (use_locks
) server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1915 if ((type
& MEM_RESERVE
) || !base
)
1917 if (type
& MEM_WRITE_WATCH
) vprot
|= VPROT_WRITEWATCH
;
1918 status
= map_view( &view
, base
, size
, mask
, type
& MEM_TOP_DOWN
, vprot
);
1919 if (status
== STATUS_SUCCESS
) base
= view
->base
;
1921 else if (type
& MEM_RESET
)
1923 if (!(view
= VIRTUAL_FindView( base
, size
))) status
= STATUS_NOT_MAPPED_VIEW
;
1924 else madvise( base
, size
, MADV_DONTNEED
);
1926 else /* commit the pages */
1928 if (!(view
= VIRTUAL_FindView( base
, size
))) status
= STATUS_NOT_MAPPED_VIEW
;
1929 else if (!VIRTUAL_SetProt( view
, base
, size
, vprot
)) status
= STATUS_ACCESS_DENIED
;
1930 else if (view
->mapping
&& !(view
->protect
& VPROT_COMMITTED
))
1932 SERVER_START_REQ( add_mapping_committed_range
)
1934 req
->handle
= wine_server_obj_handle( view
->mapping
);
1935 req
->offset
= (char *)base
- (char *)view
->base
;
1937 wine_server_call( req
);
1943 if (use_locks
) server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1945 if (status
== STATUS_SUCCESS
)
1954 /***********************************************************************
1955 * NtFreeVirtualMemory (NTDLL.@)
1956 * ZwFreeVirtualMemory (NTDLL.@)
1958 NTSTATUS WINAPI
NtFreeVirtualMemory( HANDLE process
, PVOID
*addr_ptr
, SIZE_T
*size_ptr
, ULONG type
)
1960 struct file_view
*view
;
1963 NTSTATUS status
= STATUS_SUCCESS
;
1964 LPVOID addr
= *addr_ptr
;
1965 SIZE_T size
= *size_ptr
;
1967 TRACE("%p %p %08lx %x\n", process
, addr
, size
, type
);
1969 if (process
!= NtCurrentProcess())
1972 apc_result_t result
;
1974 memset( &call
, 0, sizeof(call
) );
1976 call
.virtual_free
.type
= APC_VIRTUAL_FREE
;
1977 call
.virtual_free
.addr
= wine_server_client_ptr( addr
);
1978 call
.virtual_free
.size
= size
;
1979 call
.virtual_free
.op_type
= type
;
1980 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
1981 if (status
!= STATUS_SUCCESS
) return status
;
1983 if (result
.virtual_free
.status
== STATUS_SUCCESS
)
1985 *addr_ptr
= wine_server_get_ptr( result
.virtual_free
.addr
);
1986 *size_ptr
= result
.virtual_free
.size
;
1988 return result
.virtual_free
.status
;
1991 /* Fix the parameters */
1993 size
= ROUND_SIZE( addr
, size
);
1994 base
= ROUND_ADDR( addr
, page_mask
);
1996 /* avoid freeing the DOS area when a broken app passes a NULL pointer */
1997 if (!base
) return STATUS_INVALID_PARAMETER
;
1999 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2001 if (!(view
= VIRTUAL_FindView( base
, size
)) || !(view
->protect
& VPROT_VALLOC
))
2003 status
= STATUS_INVALID_PARAMETER
;
2005 else if (type
== MEM_RELEASE
)
2007 /* Free the pages */
2009 if (size
|| (base
!= view
->base
)) status
= STATUS_INVALID_PARAMETER
;
2012 delete_view( view
);
2017 else if (type
== MEM_DECOMMIT
)
2019 status
= decommit_pages( view
, base
- (char *)view
->base
, size
);
2020 if (status
== STATUS_SUCCESS
)
2028 WARN("called with wrong free type flags (%08x) !\n", type
);
2029 status
= STATUS_INVALID_PARAMETER
;
2032 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2037 /***********************************************************************
2038 * NtProtectVirtualMemory (NTDLL.@)
2039 * ZwProtectVirtualMemory (NTDLL.@)
2041 NTSTATUS WINAPI
NtProtectVirtualMemory( HANDLE process
, PVOID
*addr_ptr
, SIZE_T
*size_ptr
,
2042 ULONG new_prot
, ULONG
*old_prot
)
2044 struct file_view
*view
;
2046 NTSTATUS status
= STATUS_SUCCESS
;
2049 unsigned int new_vprot
;
2050 SIZE_T size
= *size_ptr
;
2051 LPVOID addr
= *addr_ptr
;
2053 TRACE("%p %p %08lx %08x\n", process
, addr
, size
, new_prot
);
2055 if (process
!= NtCurrentProcess())
2058 apc_result_t result
;
2060 memset( &call
, 0, sizeof(call
) );
2062 call
.virtual_protect
.type
= APC_VIRTUAL_PROTECT
;
2063 call
.virtual_protect
.addr
= wine_server_client_ptr( addr
);
2064 call
.virtual_protect
.size
= size
;
2065 call
.virtual_protect
.prot
= new_prot
;
2066 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
2067 if (status
!= STATUS_SUCCESS
) return status
;
2069 if (result
.virtual_protect
.status
== STATUS_SUCCESS
)
2071 *addr_ptr
= wine_server_get_ptr( result
.virtual_protect
.addr
);
2072 *size_ptr
= result
.virtual_protect
.size
;
2073 if (old_prot
) *old_prot
= result
.virtual_protect
.prot
;
2075 return result
.virtual_protect
.status
;
2078 /* Fix the parameters */
2080 size
= ROUND_SIZE( addr
, size
);
2081 base
= ROUND_ADDR( addr
, page_mask
);
2082 if ((status
= get_vprot_flags( new_prot
, &new_vprot
))) return status
;
2083 if (new_vprot
& VPROT_WRITECOPY
) return STATUS_INVALID_PAGE_PROTECTION
;
2084 new_vprot
|= VPROT_COMMITTED
;
2086 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2088 if (!(view
= VIRTUAL_FindView( base
, size
)))
2090 status
= STATUS_INVALID_PARAMETER
;
2094 /* Make sure all the pages are committed */
2095 if (get_committed_size( view
, base
, &vprot
) >= size
&& (vprot
& VPROT_COMMITTED
))
2097 if (old_prot
) *old_prot
= VIRTUAL_GetWin32Prot( vprot
);
2098 if (!VIRTUAL_SetProt( view
, base
, size
, new_vprot
)) status
= STATUS_ACCESS_DENIED
;
2100 else status
= STATUS_NOT_COMMITTED
;
2102 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2104 if (status
== STATUS_SUCCESS
)
2113 /* retrieve state for a free memory area; callback for wine_mmap_enum_reserved_areas */
2114 static int get_free_mem_state_callback( void *start
, size_t size
, void *arg
)
2116 MEMORY_BASIC_INFORMATION
*info
= arg
;
2117 void *end
= (char *)start
+ size
;
2119 if ((char *)info
->BaseAddress
+ info
->RegionSize
< (char *)start
) return 0;
2121 if (info
->BaseAddress
>= end
)
2123 if (info
->AllocationBase
< end
) info
->AllocationBase
= end
;
2127 if (info
->BaseAddress
>= start
|| start
<= address_space_start
)
2129 /* it's a real free area */
2130 info
->State
= MEM_FREE
;
2131 info
->Protect
= PAGE_NOACCESS
;
2132 info
->AllocationBase
= 0;
2133 info
->AllocationProtect
= 0;
2135 if ((char *)info
->BaseAddress
+ info
->RegionSize
> (char *)end
)
2136 info
->RegionSize
= (char *)end
- (char *)info
->BaseAddress
;
2138 else /* outside of the reserved area, pretend it's allocated */
2140 info
->RegionSize
= (char *)start
- (char *)info
->BaseAddress
;
2141 info
->State
= MEM_RESERVE
;
2142 info
->Protect
= PAGE_NOACCESS
;
2143 info
->AllocationProtect
= PAGE_NOACCESS
;
2144 info
->Type
= MEM_PRIVATE
;
2149 #define UNIMPLEMENTED_INFO_CLASS(c) \
2151 FIXME("(process=%p,addr=%p) Unimplemented information class: " #c "\n", process, addr); \
2152 return STATUS_INVALID_INFO_CLASS
2154 /***********************************************************************
2155 * NtQueryVirtualMemory (NTDLL.@)
2156 * ZwQueryVirtualMemory (NTDLL.@)
2158 NTSTATUS WINAPI
NtQueryVirtualMemory( HANDLE process
, LPCVOID addr
,
2159 MEMORY_INFORMATION_CLASS info_class
, PVOID buffer
,
2160 SIZE_T len
, SIZE_T
*res_len
)
2162 struct file_view
*view
;
2163 char *base
, *alloc_base
= 0;
2166 MEMORY_BASIC_INFORMATION
*info
= buffer
;
2169 if (info_class
!= MemoryBasicInformation
)
2173 UNIMPLEMENTED_INFO_CLASS(MemoryWorkingSetList
);
2174 UNIMPLEMENTED_INFO_CLASS(MemorySectionName
);
2175 UNIMPLEMENTED_INFO_CLASS(MemoryBasicVlmInformation
);
2178 FIXME("(%p,%p,info_class=%d,%p,%ld,%p) Unknown information class\n",
2179 process
, addr
, info_class
, buffer
, len
, res_len
);
2180 return STATUS_INVALID_INFO_CLASS
;
2184 if (process
!= NtCurrentProcess())
2188 apc_result_t result
;
2190 memset( &call
, 0, sizeof(call
) );
2192 call
.virtual_query
.type
= APC_VIRTUAL_QUERY
;
2193 call
.virtual_query
.addr
= wine_server_client_ptr( addr
);
2194 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
2195 if (status
!= STATUS_SUCCESS
) return status
;
2197 if (result
.virtual_query
.status
== STATUS_SUCCESS
)
2199 info
->BaseAddress
= wine_server_get_ptr( result
.virtual_query
.base
);
2200 info
->AllocationBase
= wine_server_get_ptr( result
.virtual_query
.alloc_base
);
2201 info
->RegionSize
= result
.virtual_query
.size
;
2202 info
->Protect
= result
.virtual_query
.prot
;
2203 info
->AllocationProtect
= result
.virtual_query
.alloc_prot
;
2204 info
->State
= (DWORD
)result
.virtual_query
.state
<< 12;
2205 info
->Type
= (DWORD
)result
.virtual_query
.alloc_type
<< 16;
2206 if (info
->RegionSize
!= result
.virtual_query
.size
) /* truncated */
2207 return STATUS_INVALID_PARAMETER
; /* FIXME */
2208 if (res_len
) *res_len
= sizeof(*info
);
2210 return result
.virtual_query
.status
;
2213 base
= ROUND_ADDR( addr
, page_mask
);
2215 if (is_beyond_limit( base
, 1, working_set_limit
)) return STATUS_WORKING_SET_LIMIT_RANGE
;
2217 /* Find the view containing the address */
2219 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2220 ptr
= list_head( &views_list
);
2225 size
= (char *)working_set_limit
- alloc_base
;
2229 view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
2230 if ((char *)view
->base
> base
)
2232 size
= (char *)view
->base
- alloc_base
;
2236 if ((char *)view
->base
+ view
->size
> base
)
2238 alloc_base
= view
->base
;
2242 alloc_base
= (char *)view
->base
+ view
->size
;
2243 ptr
= list_next( &views_list
, ptr
);
2246 /* Fill the info structure */
2248 info
->AllocationBase
= alloc_base
;
2249 info
->BaseAddress
= base
;
2250 info
->RegionSize
= size
- (base
- alloc_base
);
2254 if (!wine_mmap_enum_reserved_areas( get_free_mem_state_callback
, info
, 0 ))
2256 /* not in a reserved area at all, pretend it's allocated */
2258 if (base
>= (char *)address_space_start
)
2260 info
->State
= MEM_RESERVE
;
2261 info
->Protect
= PAGE_NOACCESS
;
2262 info
->AllocationProtect
= PAGE_NOACCESS
;
2263 info
->Type
= MEM_PRIVATE
;
2268 info
->State
= MEM_FREE
;
2269 info
->Protect
= PAGE_NOACCESS
;
2270 info
->AllocationBase
= 0;
2271 info
->AllocationProtect
= 0;
2279 SIZE_T range_size
= get_committed_size( view
, base
, &vprot
);
2281 info
->State
= (vprot
& VPROT_COMMITTED
) ? MEM_COMMIT
: MEM_RESERVE
;
2282 info
->Protect
= (vprot
& VPROT_COMMITTED
) ? VIRTUAL_GetWin32Prot( vprot
) : 0;
2283 info
->AllocationBase
= alloc_base
;
2284 info
->AllocationProtect
= VIRTUAL_GetWin32Prot( view
->protect
);
2285 if (view
->protect
& VPROT_IMAGE
) info
->Type
= MEM_IMAGE
;
2286 else if (view
->protect
& VPROT_VALLOC
) info
->Type
= MEM_PRIVATE
;
2287 else info
->Type
= MEM_MAPPED
;
2288 for (size
= base
- alloc_base
; size
< base
+ range_size
- alloc_base
; size
+= page_size
)
2289 if ((view
->prot
[size
>> page_shift
] ^ vprot
) & ~VPROT_WRITEWATCH
) break;
2290 info
->RegionSize
= size
- (base
- alloc_base
);
2292 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2294 if (res_len
) *res_len
= sizeof(*info
);
2295 return STATUS_SUCCESS
;
2299 /***********************************************************************
2300 * NtLockVirtualMemory (NTDLL.@)
2301 * ZwLockVirtualMemory (NTDLL.@)
2303 NTSTATUS WINAPI
NtLockVirtualMemory( HANDLE process
, PVOID
*addr
, SIZE_T
*size
, ULONG unknown
)
2305 NTSTATUS status
= STATUS_SUCCESS
;
2307 if (process
!= NtCurrentProcess())
2310 apc_result_t result
;
2312 memset( &call
, 0, sizeof(call
) );
2314 call
.virtual_lock
.type
= APC_VIRTUAL_LOCK
;
2315 call
.virtual_lock
.addr
= wine_server_client_ptr( *addr
);
2316 call
.virtual_lock
.size
= *size
;
2317 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
2318 if (status
!= STATUS_SUCCESS
) return status
;
2320 if (result
.virtual_lock
.status
== STATUS_SUCCESS
)
2322 *addr
= wine_server_get_ptr( result
.virtual_lock
.addr
);
2323 *size
= result
.virtual_lock
.size
;
2325 return result
.virtual_lock
.status
;
2328 *size
= ROUND_SIZE( *addr
, *size
);
2329 *addr
= ROUND_ADDR( *addr
, page_mask
);
2331 if (mlock( *addr
, *size
)) status
= STATUS_ACCESS_DENIED
;
2336 /***********************************************************************
2337 * NtUnlockVirtualMemory (NTDLL.@)
2338 * ZwUnlockVirtualMemory (NTDLL.@)
2340 NTSTATUS WINAPI
NtUnlockVirtualMemory( HANDLE process
, PVOID
*addr
, SIZE_T
*size
, ULONG unknown
)
2342 NTSTATUS status
= STATUS_SUCCESS
;
2344 if (process
!= NtCurrentProcess())
2347 apc_result_t result
;
2349 memset( &call
, 0, sizeof(call
) );
2351 call
.virtual_unlock
.type
= APC_VIRTUAL_UNLOCK
;
2352 call
.virtual_unlock
.addr
= wine_server_client_ptr( *addr
);
2353 call
.virtual_unlock
.size
= *size
;
2354 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
2355 if (status
!= STATUS_SUCCESS
) return status
;
2357 if (result
.virtual_unlock
.status
== STATUS_SUCCESS
)
2359 *addr
= wine_server_get_ptr( result
.virtual_unlock
.addr
);
2360 *size
= result
.virtual_unlock
.size
;
2362 return result
.virtual_unlock
.status
;
2365 *size
= ROUND_SIZE( *addr
, *size
);
2366 *addr
= ROUND_ADDR( *addr
, page_mask
);
2368 if (munlock( *addr
, *size
)) status
= STATUS_ACCESS_DENIED
;
2373 /***********************************************************************
2374 * NtCreateSection (NTDLL.@)
2375 * ZwCreateSection (NTDLL.@)
2377 NTSTATUS WINAPI
NtCreateSection( HANDLE
*handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
,
2378 const LARGE_INTEGER
*size
, ULONG protect
,
2379 ULONG sec_flags
, HANDLE file
)
2383 DWORD len
= (attr
&& attr
->ObjectName
) ? attr
->ObjectName
->Length
: 0;
2384 struct security_descriptor
*sd
= NULL
;
2385 struct object_attributes objattr
;
2387 /* Check parameters */
2389 if (len
> MAX_PATH
*sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
2391 if ((ret
= get_vprot_flags( protect
, &vprot
))) return ret
;
2393 objattr
.rootdir
= wine_server_obj_handle( attr
? attr
->RootDirectory
: 0 );
2395 objattr
.name_len
= len
;
2398 ret
= NTDLL_create_struct_sd( attr
->SecurityDescriptor
, &sd
, &objattr
.sd_len
);
2399 if (ret
!= STATUS_SUCCESS
) return ret
;
2402 if (!(sec_flags
& SEC_RESERVE
)) vprot
|= VPROT_COMMITTED
;
2403 if (sec_flags
& SEC_NOCACHE
) vprot
|= VPROT_NOCACHE
;
2404 if (sec_flags
& SEC_IMAGE
) vprot
|= VPROT_IMAGE
;
2406 /* Create the server object */
2408 SERVER_START_REQ( create_mapping
)
2410 req
->access
= access
;
2411 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
2412 req
->file_handle
= wine_server_obj_handle( file
);
2413 req
->size
= size
? size
->QuadPart
: 0;
2414 req
->protect
= vprot
;
2415 wine_server_add_data( req
, &objattr
, sizeof(objattr
) );
2416 if (objattr
.sd_len
) wine_server_add_data( req
, sd
, objattr
.sd_len
);
2417 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
2418 ret
= wine_server_call( req
);
2419 *handle
= wine_server_ptr_handle( reply
->handle
);
2423 NTDLL_free_struct_sd( sd
);
2429 /***********************************************************************
2430 * NtOpenSection (NTDLL.@)
2431 * ZwOpenSection (NTDLL.@)
2433 NTSTATUS WINAPI
NtOpenSection( HANDLE
*handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
)
2436 DWORD len
= attr
->ObjectName
->Length
;
2438 if (len
> MAX_PATH
*sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
2440 SERVER_START_REQ( open_mapping
)
2442 req
->access
= access
;
2443 req
->attributes
= attr
->Attributes
;
2444 req
->rootdir
= wine_server_obj_handle( attr
->RootDirectory
);
2445 wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
2446 if (!(ret
= wine_server_call( req
))) *handle
= wine_server_ptr_handle( reply
->handle
);
2453 /***********************************************************************
2454 * NtMapViewOfSection (NTDLL.@)
2455 * ZwMapViewOfSection (NTDLL.@)
2457 NTSTATUS WINAPI
NtMapViewOfSection( HANDLE handle
, HANDLE process
, PVOID
*addr_ptr
, ULONG zero_bits
,
2458 SIZE_T commit_size
, const LARGE_INTEGER
*offset_ptr
, SIZE_T
*size_ptr
,
2459 SECTION_INHERIT inherit
, ULONG alloc_type
, ULONG protect
)
2462 mem_size_t full_size
;
2464 SIZE_T size
, mask
= get_mask( zero_bits
);
2465 int unix_handle
= -1, needs_close
;
2466 unsigned int map_vprot
, vprot
;
2468 struct file_view
*view
;
2470 HANDLE dup_mapping
, shared_file
;
2471 LARGE_INTEGER offset
;
2474 offset
.QuadPart
= offset_ptr
? offset_ptr
->QuadPart
: 0;
2476 TRACE("handle=%p process=%p addr=%p off=%x%08x size=%lx access=%x\n",
2477 handle
, process
, *addr_ptr
, offset
.u
.HighPart
, offset
.u
.LowPart
, *size_ptr
, protect
);
2479 /* Check parameters */
2481 if ((offset
.u
.LowPart
& mask
) || (*addr_ptr
&& ((UINT_PTR
)*addr_ptr
& mask
)))
2482 return STATUS_INVALID_PARAMETER
;
2489 case PAGE_READWRITE
:
2490 case PAGE_EXECUTE_READWRITE
:
2491 access
= SECTION_MAP_WRITE
;
2494 case PAGE_WRITECOPY
:
2496 case PAGE_EXECUTE_READ
:
2497 case PAGE_EXECUTE_WRITECOPY
:
2498 access
= SECTION_MAP_READ
;
2501 return STATUS_INVALID_PAGE_PROTECTION
;
2504 if (process
!= NtCurrentProcess())
2507 apc_result_t result
;
2509 memset( &call
, 0, sizeof(call
) );
2511 call
.map_view
.type
= APC_MAP_VIEW
;
2512 call
.map_view
.handle
= wine_server_obj_handle( handle
);
2513 call
.map_view
.addr
= wine_server_client_ptr( *addr_ptr
);
2514 call
.map_view
.size
= *size_ptr
;
2515 call
.map_view
.offset
= offset
.QuadPart
;
2516 call
.map_view
.zero_bits
= zero_bits
;
2517 call
.map_view
.alloc_type
= alloc_type
;
2518 call
.map_view
.prot
= protect
;
2519 res
= NTDLL_queue_process_apc( process
, &call
, &result
);
2520 if (res
!= STATUS_SUCCESS
) return res
;
2522 if ((NTSTATUS
)result
.map_view
.status
>= 0)
2524 *addr_ptr
= wine_server_get_ptr( result
.map_view
.addr
);
2525 *size_ptr
= result
.map_view
.size
;
2527 return result
.map_view
.status
;
2530 SERVER_START_REQ( get_mapping_info
)
2532 req
->handle
= wine_server_obj_handle( handle
);
2533 req
->access
= access
;
2534 res
= wine_server_call( req
);
2535 map_vprot
= reply
->protect
;
2536 base
= wine_server_get_ptr( reply
->base
);
2537 full_size
= reply
->size
;
2538 header_size
= reply
->header_size
;
2539 dup_mapping
= wine_server_ptr_handle( reply
->mapping
);
2540 shared_file
= wine_server_ptr_handle( reply
->shared_file
);
2541 if ((ULONG_PTR
)base
!= reply
->base
) base
= NULL
;
2544 if (res
) return res
;
2546 if ((res
= server_get_unix_fd( handle
, 0, &unix_handle
, &needs_close
, NULL
, NULL
))) goto done
;
2548 if (map_vprot
& VPROT_IMAGE
)
2551 if (size
!= full_size
) /* truncated */
2553 WARN( "Modules larger than 4Gb (%s) not supported\n", wine_dbgstr_longlong(full_size
) );
2554 res
= STATUS_INVALID_PARAMETER
;
2559 int shared_fd
, shared_needs_close
;
2561 if ((res
= server_get_unix_fd( shared_file
, FILE_READ_DATA
|FILE_WRITE_DATA
,
2562 &shared_fd
, &shared_needs_close
, NULL
, NULL
))) goto done
;
2563 res
= map_image( handle
, unix_handle
, base
, size
, mask
, header_size
,
2564 shared_fd
, dup_mapping
, addr_ptr
);
2565 if (shared_needs_close
) close( shared_fd
);
2566 NtClose( shared_file
);
2570 res
= map_image( handle
, unix_handle
, base
, size
, mask
, header_size
,
2571 -1, dup_mapping
, addr_ptr
);
2573 if (needs_close
) close( unix_handle
);
2574 if (res
>= 0) *size_ptr
= size
;
2578 res
= STATUS_INVALID_PARAMETER
;
2579 if (offset
.QuadPart
>= full_size
) goto done
;
2582 if (*size_ptr
> full_size
- offset
.QuadPart
) goto done
;
2583 size
= ROUND_SIZE( offset
.u
.LowPart
, *size_ptr
);
2584 if (size
< *size_ptr
) goto done
; /* wrap-around */
2588 size
= full_size
- offset
.QuadPart
;
2589 if (size
!= full_size
- offset
.QuadPart
) /* truncated */
2591 WARN( "Files larger than 4Gb (%s) not supported on this platform\n",
2592 wine_dbgstr_longlong(full_size
) );
2597 /* Reserve a properly aligned area */
2599 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2601 get_vprot_flags( protect
, &vprot
);
2602 vprot
|= (map_vprot
& VPROT_COMMITTED
);
2603 res
= map_view( &view
, *addr_ptr
, size
, mask
, FALSE
, vprot
);
2606 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2612 TRACE("handle=%p size=%lx offset=%x%08x\n",
2613 handle
, size
, offset
.u
.HighPart
, offset
.u
.LowPart
);
2615 res
= map_file_into_view( view
, unix_handle
, 0, size
, offset
.QuadPart
, vprot
, !dup_mapping
);
2616 if (res
== STATUS_SUCCESS
)
2618 *addr_ptr
= view
->base
;
2620 view
->mapping
= dup_mapping
;
2621 dup_mapping
= 0; /* don't close it */
2625 ERR( "map_file_into_view %p %lx %x%08x failed\n",
2626 view
->base
, size
, offset
.u
.HighPart
, offset
.u
.LowPart
);
2627 delete_view( view
);
2630 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2633 if (dup_mapping
) NtClose( dup_mapping
);
2634 if (needs_close
) close( unix_handle
);
2639 /***********************************************************************
2640 * NtUnmapViewOfSection (NTDLL.@)
2641 * ZwUnmapViewOfSection (NTDLL.@)
2643 NTSTATUS WINAPI
NtUnmapViewOfSection( HANDLE process
, PVOID addr
)
2645 struct file_view
*view
;
2646 NTSTATUS status
= STATUS_NOT_MAPPED_VIEW
;
2648 void *base
= ROUND_ADDR( addr
, page_mask
);
2650 if (process
!= NtCurrentProcess())
2653 apc_result_t result
;
2655 memset( &call
, 0, sizeof(call
) );
2657 call
.unmap_view
.type
= APC_UNMAP_VIEW
;
2658 call
.unmap_view
.addr
= wine_server_client_ptr( addr
);
2659 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
2660 if (status
== STATUS_SUCCESS
) status
= result
.unmap_view
.status
;
2664 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2665 if ((view
= VIRTUAL_FindView( base
, 0 )) && (base
== view
->base
) && !(view
->protect
& VPROT_VALLOC
))
2667 delete_view( view
);
2668 status
= STATUS_SUCCESS
;
2670 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2675 /***********************************************************************
2676 * NtFlushVirtualMemory (NTDLL.@)
2677 * ZwFlushVirtualMemory (NTDLL.@)
2679 NTSTATUS WINAPI
NtFlushVirtualMemory( HANDLE process
, LPCVOID
*addr_ptr
,
2680 SIZE_T
*size_ptr
, ULONG unknown
)
2682 struct file_view
*view
;
2683 NTSTATUS status
= STATUS_SUCCESS
;
2685 void *addr
= ROUND_ADDR( *addr_ptr
, page_mask
);
2687 if (process
!= NtCurrentProcess())
2690 apc_result_t result
;
2692 memset( &call
, 0, sizeof(call
) );
2694 call
.virtual_flush
.type
= APC_VIRTUAL_FLUSH
;
2695 call
.virtual_flush
.addr
= wine_server_client_ptr( addr
);
2696 call
.virtual_flush
.size
= *size_ptr
;
2697 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
2698 if (status
!= STATUS_SUCCESS
) return status
;
2700 if (result
.virtual_flush
.status
== STATUS_SUCCESS
)
2702 *addr_ptr
= wine_server_get_ptr( result
.virtual_flush
.addr
);
2703 *size_ptr
= result
.virtual_flush
.size
;
2705 return result
.virtual_flush
.status
;
2708 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2709 if (!(view
= VIRTUAL_FindView( addr
, *size_ptr
))) status
= STATUS_INVALID_PARAMETER
;
2712 if (!*size_ptr
) *size_ptr
= view
->size
;
2714 if (msync( addr
, *size_ptr
, MS_SYNC
)) status
= STATUS_NOT_MAPPED_DATA
;
2716 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2721 /***********************************************************************
2722 * NtGetWriteWatch (NTDLL.@)
2723 * ZwGetWriteWatch (NTDLL.@)
2725 NTSTATUS WINAPI
NtGetWriteWatch( HANDLE process
, ULONG flags
, PVOID base
, SIZE_T size
, PVOID
*addresses
,
2726 ULONG_PTR
*count
, ULONG
*granularity
)
2728 struct file_view
*view
;
2729 NTSTATUS status
= STATUS_SUCCESS
;
2732 size
= ROUND_SIZE( base
, size
);
2733 base
= ROUND_ADDR( base
, page_mask
);
2735 if (!count
|| !granularity
) return STATUS_ACCESS_VIOLATION
;
2736 if (!*count
|| !size
) return STATUS_INVALID_PARAMETER
;
2737 if (flags
& ~WRITE_WATCH_FLAG_RESET
) return STATUS_INVALID_PARAMETER
;
2739 if (!addresses
) return STATUS_ACCESS_VIOLATION
;
2741 TRACE( "%p %x %p-%p %p %lu\n", process
, flags
, base
, (char *)base
+ size
,
2742 addresses
, *count
);
2744 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2746 if ((view
= VIRTUAL_FindView( base
, size
)) && (view
->protect
& VPROT_WRITEWATCH
))
2750 char *end
= addr
+ size
;
2752 while (pos
< *count
&& addr
< end
)
2754 BYTE prot
= view
->prot
[(addr
- (char *)view
->base
) >> page_shift
];
2755 if (!(prot
& VPROT_WRITEWATCH
)) addresses
[pos
++] = addr
;
2758 if (flags
& WRITE_WATCH_FLAG_RESET
) reset_write_watches( view
, base
, addr
- (char *)base
);
2760 *granularity
= page_size
;
2762 else status
= STATUS_INVALID_PARAMETER
;
2764 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2769 /***********************************************************************
2770 * NtResetWriteWatch (NTDLL.@)
2771 * ZwResetWriteWatch (NTDLL.@)
2773 NTSTATUS WINAPI
NtResetWriteWatch( HANDLE process
, PVOID base
, SIZE_T size
)
2775 struct file_view
*view
;
2776 NTSTATUS status
= STATUS_SUCCESS
;
2779 size
= ROUND_SIZE( base
, size
);
2780 base
= ROUND_ADDR( base
, page_mask
);
2782 TRACE( "%p %p-%p\n", process
, base
, (char *)base
+ size
);
2784 if (!size
) return STATUS_INVALID_PARAMETER
;
2786 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2788 if ((view
= VIRTUAL_FindView( base
, size
)) && (view
->protect
& VPROT_WRITEWATCH
))
2789 reset_write_watches( view
, base
, size
);
2791 status
= STATUS_INVALID_PARAMETER
;
2793 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2798 /***********************************************************************
2799 * NtReadVirtualMemory (NTDLL.@)
2800 * ZwReadVirtualMemory (NTDLL.@)
2802 NTSTATUS WINAPI
NtReadVirtualMemory( HANDLE process
, const void *addr
, void *buffer
,
2803 SIZE_T size
, SIZE_T
*bytes_read
)
2807 if (virtual_check_buffer_for_write( buffer
, size
))
2809 SERVER_START_REQ( read_process_memory
)
2811 req
->handle
= wine_server_obj_handle( process
);
2812 req
->addr
= wine_server_client_ptr( addr
);
2813 wine_server_set_reply( req
, buffer
, size
);
2814 if ((status
= wine_server_call( req
))) size
= 0;
2820 status
= STATUS_ACCESS_VIOLATION
;
2823 if (bytes_read
) *bytes_read
= size
;
2828 /***********************************************************************
2829 * NtWriteVirtualMemory (NTDLL.@)
2830 * ZwWriteVirtualMemory (NTDLL.@)
2832 NTSTATUS WINAPI
NtWriteVirtualMemory( HANDLE process
, void *addr
, const void *buffer
,
2833 SIZE_T size
, SIZE_T
*bytes_written
)
2837 if (virtual_check_buffer_for_read( buffer
, size
))
2839 SERVER_START_REQ( write_process_memory
)
2841 req
->handle
= wine_server_obj_handle( process
);
2842 req
->addr
= wine_server_client_ptr( addr
);
2843 wine_server_add_data( req
, buffer
, size
);
2844 if ((status
= wine_server_call( req
))) size
= 0;
2850 status
= STATUS_PARTIAL_COPY
;
2853 if (bytes_written
) *bytes_written
= size
;
2858 /***********************************************************************
2859 * NtAreMappedFilesTheSame (NTDLL.@)
2860 * ZwAreMappedFilesTheSame (NTDLL.@)
2862 NTSTATUS WINAPI
NtAreMappedFilesTheSame(PVOID addr1
, PVOID addr2
)
2864 struct file_view
*view1
, *view2
;
2865 struct stat st1
, st2
;
2869 TRACE("%p %p\n", addr1
, addr2
);
2871 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2873 view1
= VIRTUAL_FindView( addr1
, 0 );
2874 view2
= VIRTUAL_FindView( addr2
, 0 );
2876 if (!view1
|| !view2
)
2877 status
= STATUS_INVALID_ADDRESS
;
2878 else if ((view1
->protect
& VPROT_VALLOC
) || (view2
->protect
& VPROT_VALLOC
))
2879 status
= STATUS_CONFLICTING_ADDRESSES
;
2880 else if (!(view1
->protect
& VPROT_IMAGE
) || !(view2
->protect
& VPROT_IMAGE
))
2881 status
= STATUS_NOT_SAME_DEVICE
;
2882 else if (view1
== view2
)
2883 status
= STATUS_SUCCESS
;
2884 else if (!stat_mapping_file( view1
, &st1
) && !stat_mapping_file( view2
, &st2
) &&
2885 st1
.st_dev
== st2
.st_dev
&& st1
.st_ino
== st2
.st_ino
)
2886 status
= STATUS_SUCCESS
;
2888 status
= STATUS_NOT_SAME_DEVICE
;
2890 server_leave_uninterrupted_section( &csVirtual
, &sigset
);