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
73 typedef struct file_view
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 */
126 static UINT page_shift
;
127 static UINT_PTR page_size
;
128 static UINT_PTR page_mask
;
129 static void *address_space_limit
;
130 static void *user_space_limit
;
131 static void *working_set_limit
;
132 #endif /* __i386__ */
134 #define ROUND_ADDR(addr,mask) \
135 ((void *)((UINT_PTR)(addr) & ~(UINT_PTR)(mask)))
137 #define ROUND_SIZE(addr,size) \
138 (((SIZE_T)(size) + ((UINT_PTR)(addr) & page_mask) + page_mask) & ~page_mask)
140 #define VIRTUAL_DEBUG_DUMP_VIEW(view) \
141 do { if (TRACE_ON(virtual)) VIRTUAL_DumpView(view); } while (0)
143 #define VIRTUAL_HEAP_SIZE (4*1024*1024)
145 static HANDLE virtual_heap
;
146 static void *preload_reserve_start
;
147 static void *preload_reserve_end
;
148 static int use_locks
;
149 static int force_exec_prot
; /* whether to force PROT_EXEC on all PROT_READ mmaps */
152 /***********************************************************************
155 static const char *VIRTUAL_GetProtStr( BYTE prot
)
157 static char buffer
[6];
158 buffer
[0] = (prot
& VPROT_COMMITTED
) ? 'c' : '-';
159 buffer
[1] = (prot
& VPROT_GUARD
) ? 'g' : ((prot
& VPROT_WRITEWATCH
) ? 'H' : '-');
160 buffer
[2] = (prot
& VPROT_READ
) ? 'r' : '-';
161 buffer
[3] = (prot
& VPROT_WRITECOPY
) ? 'W' : ((prot
& VPROT_WRITE
) ? 'w' : '-');
162 buffer
[4] = (prot
& VPROT_EXEC
) ? 'x' : '-';
168 /***********************************************************************
169 * VIRTUAL_GetUnixProt
171 * Convert page protections to protection for mmap/mprotect.
173 static int VIRTUAL_GetUnixProt( BYTE vprot
)
176 if ((vprot
& VPROT_COMMITTED
) && !(vprot
& VPROT_GUARD
))
178 if (vprot
& VPROT_READ
) prot
|= PROT_READ
;
179 if (vprot
& VPROT_WRITE
) prot
|= PROT_WRITE
;
180 if (vprot
& VPROT_WRITECOPY
) prot
|= PROT_WRITE
;
181 if (vprot
& VPROT_EXEC
) prot
|= PROT_EXEC
;
182 if (vprot
& VPROT_WRITEWATCH
) prot
&= ~PROT_WRITE
;
184 if (!prot
) prot
= PROT_NONE
;
189 /***********************************************************************
192 static void VIRTUAL_DumpView( FILE_VIEW
*view
)
195 char *addr
= view
->base
;
196 BYTE prot
= view
->prot
[0];
198 TRACE( "View: %p - %p", addr
, addr
+ view
->size
- 1 );
199 if (view
->protect
& VPROT_SYSTEM
)
200 TRACE( " (system)\n" );
201 else if (view
->protect
& VPROT_VALLOC
)
202 TRACE( " (valloc)\n" );
203 else if (view
->mapping
)
204 TRACE( " %p\n", view
->mapping
);
206 TRACE( " (anonymous)\n");
208 for (count
= i
= 1; i
< view
->size
>> page_shift
; i
++, count
++)
210 if (view
->prot
[i
] == prot
) continue;
211 TRACE( " %p - %p %s\n",
212 addr
, addr
+ (count
<< page_shift
) - 1, VIRTUAL_GetProtStr(prot
) );
213 addr
+= (count
<< page_shift
);
214 prot
= view
->prot
[i
];
218 TRACE( " %p - %p %s\n",
219 addr
, addr
+ (count
<< page_shift
) - 1, VIRTUAL_GetProtStr(prot
) );
223 /***********************************************************************
227 static void VIRTUAL_Dump(void)
230 struct file_view
*view
;
232 TRACE( "Dump of all virtual memory views:\n" );
233 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
234 LIST_FOR_EACH_ENTRY( view
, &views_list
, FILE_VIEW
, entry
)
236 VIRTUAL_DumpView( view
);
238 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
243 /***********************************************************************
246 * Find the view containing a given address. The csVirtual section must be held by caller.
255 static struct file_view
*VIRTUAL_FindView( const void *addr
, size_t size
)
257 struct file_view
*view
;
259 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
261 if (view
->base
> addr
) break; /* no matching view */
262 if ((const char *)view
->base
+ view
->size
<= (const char *)addr
) continue;
263 if ((const char *)view
->base
+ view
->size
< (const char *)addr
+ size
) break; /* size too large */
264 if ((const char *)addr
+ size
< (const char *)addr
) break; /* overflow */
271 /***********************************************************************
274 static inline UINT_PTR
get_mask( ULONG zero_bits
)
276 if (!zero_bits
) return 0xffff; /* allocations are aligned to 64K by default */
277 if (zero_bits
< page_shift
) zero_bits
= page_shift
;
278 return (1 << zero_bits
) - 1;
282 /***********************************************************************
285 * Find the first view overlapping at least part of the specified range.
286 * The csVirtual section must be held by caller.
288 static struct file_view
*find_view_range( const void *addr
, size_t size
)
290 struct file_view
*view
;
292 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
294 if ((const char *)view
->base
>= (const char *)addr
+ size
) break;
295 if ((const char *)view
->base
+ view
->size
> (const char *)addr
) return view
;
301 /***********************************************************************
304 * Find a free area between views inside the specified range.
305 * The csVirtual section must be held by caller.
307 static void *find_free_area( void *base
, void *end
, size_t size
, size_t mask
, int top_down
)
314 start
= ROUND_ADDR( (char *)end
- size
, mask
);
315 if (start
>= end
|| start
< base
) return NULL
;
317 for (ptr
= views_list
.prev
; ptr
!= &views_list
; ptr
= ptr
->prev
)
319 struct file_view
*view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
321 if ((char *)view
->base
+ view
->size
<= (char *)start
) break;
322 if ((char *)view
->base
>= (char *)start
+ size
) continue;
323 start
= ROUND_ADDR( (char *)view
->base
- size
, mask
);
324 /* stop if remaining space is not large enough */
325 if (!start
|| start
>= end
|| start
< base
) return NULL
;
330 start
= ROUND_ADDR( (char *)base
+ mask
, mask
);
331 if (start
>= end
|| (char *)end
- (char *)start
< size
) return NULL
;
333 for (ptr
= views_list
.next
; ptr
!= &views_list
; ptr
= ptr
->next
)
335 struct file_view
*view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
337 if ((char *)view
->base
>= (char *)start
+ size
) break;
338 if ((char *)view
->base
+ view
->size
<= (char *)start
) continue;
339 start
= ROUND_ADDR( (char *)view
->base
+ view
->size
+ mask
, mask
);
340 /* stop if remaining space is not large enough */
341 if (!start
|| start
>= end
|| (char *)end
- (char *)start
< size
) return NULL
;
348 /***********************************************************************
351 * Add a reserved area to the list maintained by libwine.
352 * The csVirtual section must be held by caller.
354 static void add_reserved_area( void *addr
, size_t size
)
356 TRACE( "adding %p-%p\n", addr
, (char *)addr
+ size
);
358 if (addr
< user_space_limit
)
360 /* unmap the part of the area that is below the limit */
361 assert( (char *)addr
+ size
> (char *)user_space_limit
);
362 munmap( addr
, (char *)user_space_limit
- (char *)addr
);
363 size
-= (char *)user_space_limit
- (char *)addr
;
364 addr
= user_space_limit
;
366 /* blow away existing mappings */
367 wine_anon_mmap( addr
, size
, PROT_NONE
, MAP_NORESERVE
| MAP_FIXED
);
368 wine_mmap_add_reserved_area( addr
, size
);
372 /***********************************************************************
373 * remove_reserved_area
375 * Remove a reserved area from the list maintained by libwine.
376 * The csVirtual section must be held by caller.
378 static void remove_reserved_area( void *addr
, size_t size
)
380 struct file_view
*view
;
382 TRACE( "removing %p-%p\n", addr
, (char *)addr
+ size
);
383 wine_mmap_remove_reserved_area( addr
, size
, 0 );
385 /* unmap areas not covered by an existing view */
386 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
388 if ((char *)view
->base
>= (char *)addr
+ size
)
390 munmap( addr
, size
);
393 if ((char *)view
->base
+ view
->size
<= (char *)addr
) continue;
394 if (view
->base
> addr
) munmap( addr
, (char *)view
->base
- (char *)addr
);
395 if ((char *)view
->base
+ view
->size
> (char *)addr
+ size
) break;
396 size
= (char *)addr
+ size
- ((char *)view
->base
+ view
->size
);
397 addr
= (char *)view
->base
+ view
->size
;
402 /***********************************************************************
405 * Check if an address range goes beyond a given limit.
407 static inline int is_beyond_limit( const void *addr
, size_t size
, const void *limit
)
409 return (addr
>= limit
|| (const char *)addr
+ size
> (const char *)limit
);
413 /***********************************************************************
416 * Unmap an area, or simply replace it by an empty mapping if it is
417 * in a reserved area. The csVirtual section must be held by caller.
419 static inline void unmap_area( void *addr
, size_t size
)
421 if (wine_mmap_is_in_reserved_area( addr
, size
))
422 wine_anon_mmap( addr
, size
, PROT_NONE
, MAP_NORESERVE
| MAP_FIXED
);
423 else if (is_beyond_limit( addr
, size
, user_space_limit
))
424 add_reserved_area( addr
, size
);
426 munmap( addr
, size
);
430 /***********************************************************************
433 * Deletes a view. The csVirtual section must be held by caller.
435 static void delete_view( struct file_view
*view
) /* [in] View */
437 if (!(view
->protect
& VPROT_SYSTEM
)) unmap_area( view
->base
, view
->size
);
438 list_remove( &view
->entry
);
439 if (view
->mapping
) NtClose( view
->mapping
);
440 RtlFreeHeap( virtual_heap
, 0, view
);
444 /***********************************************************************
447 * Create a view. The csVirtual section must be held by caller.
449 static NTSTATUS
create_view( struct file_view
**view_ret
, void *base
, size_t size
, unsigned int vprot
)
451 struct file_view
*view
;
453 int unix_prot
= VIRTUAL_GetUnixProt( vprot
);
455 assert( !((UINT_PTR
)base
& page_mask
) );
456 assert( !(size
& page_mask
) );
458 /* Create the view structure */
460 if (!(view
= RtlAllocateHeap( virtual_heap
, 0, sizeof(*view
) + (size
>> page_shift
) - 1 )))
462 FIXME( "out of memory in virtual heap for %p-%p\n", base
, (char *)base
+ size
);
463 return STATUS_NO_MEMORY
;
469 view
->protect
= vprot
;
470 memset( view
->prot
, vprot
, size
>> page_shift
);
472 /* Insert it in the linked list */
474 LIST_FOR_EACH( ptr
, &views_list
)
476 struct file_view
*next
= LIST_ENTRY( ptr
, struct file_view
, entry
);
477 if (next
->base
> base
) break;
479 list_add_before( ptr
, &view
->entry
);
481 /* Check for overlapping views. This can happen if the previous view
482 * was a system view that got unmapped behind our back. In that case
483 * we recover by simply deleting it. */
485 if ((ptr
= list_prev( &views_list
, &view
->entry
)) != NULL
)
487 struct file_view
*prev
= LIST_ENTRY( ptr
, struct file_view
, entry
);
488 if ((char *)prev
->base
+ prev
->size
> (char *)base
)
490 TRACE( "overlapping prev view %p-%p for %p-%p\n",
491 prev
->base
, (char *)prev
->base
+ prev
->size
,
492 base
, (char *)base
+ view
->size
);
493 assert( prev
->protect
& VPROT_SYSTEM
);
497 if ((ptr
= list_next( &views_list
, &view
->entry
)) != NULL
)
499 struct file_view
*next
= LIST_ENTRY( ptr
, struct file_view
, entry
);
500 if ((char *)base
+ view
->size
> (char *)next
->base
)
502 TRACE( "overlapping next view %p-%p for %p-%p\n",
503 next
->base
, (char *)next
->base
+ next
->size
,
504 base
, (char *)base
+ view
->size
);
505 assert( next
->protect
& VPROT_SYSTEM
);
511 VIRTUAL_DEBUG_DUMP_VIEW( view
);
513 if (force_exec_prot
&& !(vprot
& VPROT_NOEXEC
) && (unix_prot
& PROT_READ
) && !(unix_prot
& PROT_EXEC
))
515 TRACE( "forcing exec permission on %p-%p\n", base
, (char *)base
+ size
- 1 );
516 mprotect( base
, size
, unix_prot
| PROT_EXEC
);
518 return STATUS_SUCCESS
;
522 /***********************************************************************
523 * VIRTUAL_GetWin32Prot
525 * Convert page protections to Win32 flags.
527 static DWORD
VIRTUAL_GetWin32Prot( BYTE vprot
)
529 DWORD ret
= VIRTUAL_Win32Flags
[vprot
& 0x0f];
530 if (vprot
& VPROT_NOCACHE
) ret
|= PAGE_NOCACHE
;
531 if (vprot
& VPROT_GUARD
) ret
|= PAGE_GUARD
;
536 /***********************************************************************
539 * Build page protections from Win32 flags.
542 * protect [I] Win32 protection flags
545 * Value of page protection flags
547 static NTSTATUS
get_vprot_flags( DWORD protect
, unsigned int *vprot
)
549 switch(protect
& 0xff)
555 *vprot
= VPROT_READ
| VPROT_WRITE
;
558 *vprot
= VPROT_READ
| VPROT_WRITECOPY
;
563 case PAGE_EXECUTE_READ
:
564 *vprot
= VPROT_EXEC
| VPROT_READ
;
566 case PAGE_EXECUTE_READWRITE
:
567 *vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITE
;
569 case PAGE_EXECUTE_WRITECOPY
:
570 *vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITECOPY
;
576 return STATUS_INVALID_PARAMETER
;
578 if (protect
& PAGE_GUARD
) *vprot
|= VPROT_GUARD
;
579 if (protect
& PAGE_NOCACHE
) *vprot
|= VPROT_NOCACHE
;
580 return STATUS_SUCCESS
;
584 /***********************************************************************
587 * Change the protection of a range of pages.
593 static BOOL
VIRTUAL_SetProt( FILE_VIEW
*view
, /* [in] Pointer to view */
594 void *base
, /* [in] Starting address */
595 size_t size
, /* [in] Size in bytes */
596 BYTE vprot
) /* [in] Protections to use */
598 int unix_prot
= VIRTUAL_GetUnixProt(vprot
);
599 BYTE
*p
= view
->prot
+ (((char *)base
- (char *)view
->base
) >> page_shift
);
602 base
, (char *)base
+ size
- 1, VIRTUAL_GetProtStr( vprot
) );
604 if (view
->protect
& VPROT_WRITEWATCH
)
606 /* each page may need different protections depending on write watch flag */
611 p
[0] = vprot
| (p
[0] & VPROT_WRITEWATCH
);
612 unix_prot
= VIRTUAL_GetUnixProt( p
[0] );
613 for (count
= i
= 1; i
< size
>> page_shift
; i
++, count
++)
615 p
[i
] = vprot
| (p
[i
] & VPROT_WRITEWATCH
);
616 prot
= VIRTUAL_GetUnixProt( p
[i
] );
617 if (prot
== unix_prot
) continue;
618 mprotect( addr
, count
<< page_shift
, unix_prot
);
619 addr
+= count
<< page_shift
;
623 if (count
) mprotect( addr
, count
<< page_shift
, unix_prot
);
624 VIRTUAL_DEBUG_DUMP_VIEW( view
);
628 /* if setting stack guard pages, store the permissions first, as the guard may be
629 * triggered at any point after mprotect and change the permissions again */
630 if ((vprot
& VPROT_GUARD
) &&
631 (base
>= NtCurrentTeb()->DeallocationStack
) &&
632 (base
< NtCurrentTeb()->Tib
.StackBase
))
634 memset( p
, vprot
, size
>> page_shift
);
635 mprotect( base
, size
, unix_prot
);
636 VIRTUAL_DEBUG_DUMP_VIEW( view
);
640 if (force_exec_prot
&& !(view
->protect
& VPROT_NOEXEC
) &&
641 (unix_prot
& PROT_READ
) && !(unix_prot
& PROT_EXEC
))
643 TRACE( "forcing exec permission on %p-%p\n", base
, (char *)base
+ size
- 1 );
644 if (!mprotect( base
, size
, unix_prot
| PROT_EXEC
)) goto done
;
645 /* exec + write may legitimately fail, in that case fall back to write only */
646 if (!(unix_prot
& PROT_WRITE
)) return FALSE
;
649 if (mprotect( base
, size
, unix_prot
)) return FALSE
; /* FIXME: last error */
652 memset( p
, vprot
, size
>> page_shift
);
653 VIRTUAL_DEBUG_DUMP_VIEW( view
);
658 /***********************************************************************
659 * reset_write_watches
661 * Reset write watches in a memory range.
663 static void reset_write_watches( struct file_view
*view
, void *base
, SIZE_T size
)
668 BYTE
*p
= view
->prot
+ ((addr
- (char *)view
->base
) >> page_shift
);
670 p
[0] |= VPROT_WRITEWATCH
;
671 unix_prot
= VIRTUAL_GetUnixProt( p
[0] );
672 for (count
= i
= 1; i
< size
>> page_shift
; i
++, count
++)
674 p
[i
] |= VPROT_WRITEWATCH
;
675 prot
= VIRTUAL_GetUnixProt( p
[i
] );
676 if (prot
== unix_prot
) continue;
677 mprotect( addr
, count
<< page_shift
, unix_prot
);
678 addr
+= count
<< page_shift
;
682 if (count
) mprotect( addr
, count
<< page_shift
, unix_prot
);
686 /***********************************************************************
689 * Release the extra memory while keeping the range starting on the granularity boundary.
691 static inline void *unmap_extra_space( void *ptr
, size_t total_size
, size_t wanted_size
, size_t mask
)
693 if ((ULONG_PTR
)ptr
& mask
)
695 size_t extra
= mask
+ 1 - ((ULONG_PTR
)ptr
& mask
);
696 munmap( ptr
, extra
);
697 ptr
= (char *)ptr
+ extra
;
700 if (total_size
> wanted_size
)
701 munmap( (char *)ptr
+ wanted_size
, total_size
- wanted_size
);
715 /***********************************************************************
716 * alloc_reserved_area_callback
718 * Try to map some space inside a reserved area. Callback for wine_mmap_enum_reserved_areas.
720 static int alloc_reserved_area_callback( void *start
, size_t size
, void *arg
)
722 static void * const address_space_start
= (void *)0x110000;
723 struct alloc_area
*alloc
= arg
;
724 void *end
= (char *)start
+ size
;
726 if (start
< address_space_start
) start
= address_space_start
;
727 if (is_beyond_limit( start
, size
, alloc
->limit
)) end
= alloc
->limit
;
728 if (start
>= end
) return 0;
730 /* make sure we don't touch the preloader reserved range */
731 if (preload_reserve_end
>= start
)
733 if (preload_reserve_end
>= end
)
735 if (preload_reserve_start
<= start
) return 0; /* no space in that area */
736 if (preload_reserve_start
< end
) end
= preload_reserve_start
;
738 else if (preload_reserve_start
<= start
) start
= preload_reserve_end
;
741 /* range is split in two by the preloader reservation, try first part */
742 if ((alloc
->result
= find_free_area( start
, preload_reserve_start
, alloc
->size
,
743 alloc
->mask
, alloc
->top_down
)))
745 /* then fall through to try second part */
746 start
= preload_reserve_end
;
749 if ((alloc
->result
= find_free_area( start
, end
, alloc
->size
, alloc
->mask
, alloc
->top_down
)))
756 /***********************************************************************
759 * Create a view and mmap the corresponding memory area.
760 * The csVirtual section must be held by caller.
762 static NTSTATUS
map_view( struct file_view
**view_ret
, void *base
, size_t size
, size_t mask
,
763 int top_down
, unsigned int vprot
)
770 if (is_beyond_limit( base
, size
, address_space_limit
))
771 return STATUS_WORKING_SET_LIMIT_RANGE
;
773 switch (wine_mmap_is_in_reserved_area( base
, size
))
775 case -1: /* partially in a reserved area */
776 return STATUS_CONFLICTING_ADDRESSES
;
778 case 0: /* not in a reserved area, do a normal allocation */
779 if ((ptr
= wine_anon_mmap( base
, size
, VIRTUAL_GetUnixProt(vprot
), 0 )) == (void *)-1)
781 if (errno
== ENOMEM
) return STATUS_NO_MEMORY
;
782 return STATUS_INVALID_PARAMETER
;
786 /* We couldn't get the address we wanted */
787 if (is_beyond_limit( ptr
, size
, user_space_limit
)) add_reserved_area( ptr
, size
);
788 else munmap( ptr
, size
);
789 return STATUS_CONFLICTING_ADDRESSES
;
794 case 1: /* in a reserved area, make sure the address is available */
795 if (find_view_range( base
, size
)) return STATUS_CONFLICTING_ADDRESSES
;
796 /* replace the reserved area by our mapping */
797 if ((ptr
= wine_anon_mmap( base
, size
, VIRTUAL_GetUnixProt(vprot
), MAP_FIXED
)) != base
)
798 return STATUS_INVALID_PARAMETER
;
801 if (is_beyond_limit( ptr
, size
, working_set_limit
)) working_set_limit
= address_space_limit
;
805 size_t view_size
= size
+ mask
+ 1;
806 struct alloc_area alloc
;
810 alloc
.top_down
= top_down
;
811 alloc
.limit
= user_space_limit
;
812 if (wine_mmap_enum_reserved_areas( alloc_reserved_area_callback
, &alloc
, top_down
))
815 TRACE( "got mem in reserved area %p-%p\n", ptr
, (char *)ptr
+ size
);
816 if (wine_anon_mmap( ptr
, size
, VIRTUAL_GetUnixProt(vprot
), MAP_FIXED
) != ptr
)
817 return STATUS_INVALID_PARAMETER
;
823 if ((ptr
= wine_anon_mmap( NULL
, view_size
, VIRTUAL_GetUnixProt(vprot
), 0 )) == (void *)-1)
825 if (errno
== ENOMEM
) return STATUS_NO_MEMORY
;
826 return STATUS_INVALID_PARAMETER
;
828 TRACE( "got mem with anon mmap %p-%p\n", ptr
, (char *)ptr
+ size
);
829 /* if we got something beyond the user limit, unmap it and retry */
830 if (is_beyond_limit( ptr
, view_size
, user_space_limit
)) add_reserved_area( ptr
, view_size
);
833 ptr
= unmap_extra_space( ptr
, view_size
, size
, mask
);
836 status
= create_view( view_ret
, ptr
, size
, vprot
);
837 if (status
!= STATUS_SUCCESS
) unmap_area( ptr
, size
);
842 /***********************************************************************
845 * Wrapper for mmap() to map a file into a view, falling back to read if mmap fails.
846 * The csVirtual section must be held by caller.
848 static NTSTATUS
map_file_into_view( struct file_view
*view
, int fd
, size_t start
, size_t size
,
849 off_t offset
, unsigned int vprot
, BOOL removable
)
852 int prot
= VIRTUAL_GetUnixProt( vprot
| VPROT_COMMITTED
/* make sure it is accessible */ );
853 BOOL shared_write
= (vprot
& VPROT_WRITE
) != 0;
855 assert( start
< view
->size
);
856 assert( start
+ size
<= view
->size
);
858 /* only try mmap if media is not removable (or if we require write access) */
859 if (!removable
|| shared_write
)
861 int flags
= MAP_FIXED
| (shared_write
? MAP_SHARED
: MAP_PRIVATE
);
863 if (mmap( (char *)view
->base
+ start
, size
, prot
, flags
, fd
, offset
) != (void *)-1)
866 /* mmap() failed; if this is because the file offset is not */
867 /* page-aligned (EINVAL), or because the underlying filesystem */
868 /* does not support mmap() (ENOEXEC,ENODEV), we do it by hand. */
869 if ((errno
!= ENOEXEC
) && (errno
!= EINVAL
) && (errno
!= ENODEV
)) return FILE_GetNtStatus();
870 if (shared_write
) /* we cannot fake shared write mappings */
872 if (errno
== EINVAL
) return STATUS_INVALID_PARAMETER
;
873 ERR( "shared writable mmap not supported, broken filesystem?\n" );
874 return STATUS_NOT_SUPPORTED
;
878 /* Reserve the memory with an anonymous mmap */
879 ptr
= wine_anon_mmap( (char *)view
->base
+ start
, size
, PROT_READ
| PROT_WRITE
, MAP_FIXED
);
880 if (ptr
== (void *)-1) return FILE_GetNtStatus();
881 /* Now read in the file */
882 pread( fd
, ptr
, size
, offset
);
883 if (prot
!= (PROT_READ
|PROT_WRITE
)) mprotect( ptr
, size
, prot
); /* Set the right protection */
885 memset( view
->prot
+ (start
>> page_shift
), vprot
, ROUND_SIZE(start
,size
) >> page_shift
);
886 return STATUS_SUCCESS
;
890 /***********************************************************************
893 * Get the size of the committed range starting at base.
894 * Also return the protections for the first page.
896 static SIZE_T
get_committed_size( struct file_view
*view
, void *base
, BYTE
*vprot
)
900 start
= ((char *)base
- (char *)view
->base
) >> page_shift
;
901 *vprot
= view
->prot
[start
];
903 if (view
->mapping
&& !(view
->protect
& VPROT_COMMITTED
))
906 SERVER_START_REQ( get_mapping_committed_range
)
908 req
->handle
= wine_server_obj_handle( view
->mapping
);
909 req
->offset
= start
<< page_shift
;
910 if (!wine_server_call( req
))
913 if (reply
->committed
)
915 *vprot
|= VPROT_COMMITTED
;
916 for (i
= 0; i
< ret
>> page_shift
; i
++) view
->prot
[start
+i
] |= VPROT_COMMITTED
;
923 for (i
= start
+ 1; i
< view
->size
>> page_shift
; i
++)
924 if ((*vprot
^ view
->prot
[i
]) & VPROT_COMMITTED
) break;
925 return (i
- start
) << page_shift
;
929 /***********************************************************************
932 * Decommit some pages of a given view.
933 * The csVirtual section must be held by caller.
935 static NTSTATUS
decommit_pages( struct file_view
*view
, size_t start
, size_t size
)
937 if (wine_anon_mmap( (char *)view
->base
+ start
, size
, PROT_NONE
, MAP_FIXED
) != (void *)-1)
939 BYTE
*p
= view
->prot
+ (start
>> page_shift
);
941 while (size
--) *p
++ &= ~VPROT_COMMITTED
;
942 return STATUS_SUCCESS
;
944 return FILE_GetNtStatus();
948 /***********************************************************************
949 * allocate_dos_memory
951 * Allocate the DOS memory range.
953 static NTSTATUS
allocate_dos_memory( struct file_view
**view
, unsigned int vprot
)
957 void * const low_64k
= (void *)0x10000;
958 const size_t dosmem_size
= 0x110000;
959 int unix_prot
= VIRTUAL_GetUnixProt( vprot
);
962 /* check for existing view */
964 if ((ptr
= list_head( &views_list
)))
966 struct file_view
*first_view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
967 if (first_view
->base
< (void *)dosmem_size
) return STATUS_CONFLICTING_ADDRESSES
;
970 /* check without the first 64K */
972 if (wine_mmap_is_in_reserved_area( low_64k
, dosmem_size
- 0x10000 ) != 1)
974 addr
= wine_anon_mmap( low_64k
, dosmem_size
- 0x10000, unix_prot
, 0 );
977 if (addr
!= (void *)-1) munmap( addr
, dosmem_size
- 0x10000 );
978 return map_view( view
, NULL
, dosmem_size
, 0xffff, 0, vprot
);
982 /* now try to allocate the low 64K too */
984 if (wine_mmap_is_in_reserved_area( NULL
, 0x10000 ) != 1)
986 addr
= wine_anon_mmap( (void *)page_size
, 0x10000 - page_size
, unix_prot
, 0 );
987 if (addr
== (void *)page_size
)
990 TRACE( "successfully mapped low 64K range\n" );
994 if (addr
!= (void *)-1) munmap( addr
, 0x10000 - page_size
);
996 TRACE( "failed to map low 64K range\n" );
1000 /* now reserve the whole range */
1002 size
= (char *)dosmem_size
- (char *)addr
;
1003 wine_anon_mmap( addr
, size
, unix_prot
, MAP_FIXED
);
1004 return create_view( view
, addr
, size
, vprot
);
1008 /***********************************************************************
1011 * Map an executable (PE format) image into memory.
1013 static NTSTATUS
map_image( HANDLE hmapping
, int fd
, char *base
, SIZE_T total_size
, SIZE_T mask
,
1014 SIZE_T header_size
, int shared_fd
, HANDLE dup_mapping
, PVOID
*addr_ptr
)
1016 IMAGE_DOS_HEADER
*dos
;
1017 IMAGE_NT_HEADERS
*nt
;
1018 IMAGE_SECTION_HEADER
*sec
;
1019 IMAGE_DATA_DIRECTORY
*imports
;
1020 NTSTATUS status
= STATUS_CONFLICTING_ADDRESSES
;
1025 struct file_view
*view
= NULL
;
1026 char *ptr
, *header_end
;
1029 /* zero-map the whole range */
1031 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1033 if (base
>= (char *)0x110000) /* make sure the DOS area remains free */
1034 status
= map_view( &view
, base
, total_size
, mask
, FALSE
,
1035 VPROT_COMMITTED
| VPROT_READ
| VPROT_EXEC
| VPROT_WRITECOPY
| VPROT_IMAGE
);
1037 if (status
!= STATUS_SUCCESS
)
1038 status
= map_view( &view
, NULL
, total_size
, mask
, FALSE
,
1039 VPROT_COMMITTED
| VPROT_READ
| VPROT_EXEC
| VPROT_WRITECOPY
| VPROT_IMAGE
);
1041 if (status
!= STATUS_SUCCESS
) goto error
;
1044 TRACE_(module
)( "mapped PE file at %p-%p\n", ptr
, ptr
+ total_size
);
1046 /* map the header */
1048 if (fstat( fd
, &st
) == -1)
1050 status
= FILE_GetNtStatus();
1053 status
= STATUS_INVALID_IMAGE_FORMAT
; /* generic error */
1054 if (!st
.st_size
) goto error
;
1055 header_size
= min( header_size
, st
.st_size
);
1056 if (map_file_into_view( view
, fd
, 0, header_size
, 0, VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
,
1057 !dup_mapping
) != STATUS_SUCCESS
) goto error
;
1058 dos
= (IMAGE_DOS_HEADER
*)ptr
;
1059 nt
= (IMAGE_NT_HEADERS
*)(ptr
+ dos
->e_lfanew
);
1060 header_end
= ptr
+ ROUND_SIZE( 0, header_size
);
1061 memset( ptr
+ header_size
, 0, header_end
- (ptr
+ header_size
) );
1062 if ((char *)(nt
+ 1) > header_end
) goto error
;
1063 sec
= (IMAGE_SECTION_HEADER
*)((char*)&nt
->OptionalHeader
+nt
->FileHeader
.SizeOfOptionalHeader
);
1064 if ((char *)(sec
+ nt
->FileHeader
.NumberOfSections
) > header_end
) goto error
;
1066 imports
= nt
->OptionalHeader
.DataDirectory
+ IMAGE_DIRECTORY_ENTRY_IMPORT
;
1067 if (!imports
->Size
|| !imports
->VirtualAddress
) imports
= NULL
;
1069 /* check the architecture */
1072 if (nt
->FileHeader
.Machine
!= IMAGE_FILE_MACHINE_AMD64
)
1074 if (nt
->FileHeader
.Machine
!= IMAGE_FILE_MACHINE_I386
)
1077 MESSAGE("Trying to load PE image for unsupported architecture (");
1078 switch (nt
->FileHeader
.Machine
)
1080 case IMAGE_FILE_MACHINE_UNKNOWN
: MESSAGE("Unknown"); break;
1081 case IMAGE_FILE_MACHINE_I860
: MESSAGE("I860"); break;
1082 case IMAGE_FILE_MACHINE_I386
: MESSAGE("I386"); break;
1083 case IMAGE_FILE_MACHINE_R3000
: MESSAGE("R3000"); break;
1084 case IMAGE_FILE_MACHINE_R4000
: MESSAGE("R4000"); break;
1085 case IMAGE_FILE_MACHINE_R10000
: MESSAGE("R10000"); break;
1086 case IMAGE_FILE_MACHINE_ALPHA
: MESSAGE("Alpha"); break;
1087 case IMAGE_FILE_MACHINE_POWERPC
: MESSAGE("PowerPC"); break;
1088 case IMAGE_FILE_MACHINE_IA64
: MESSAGE("IA-64"); break;
1089 case IMAGE_FILE_MACHINE_ALPHA64
: MESSAGE("Alpha-64"); break;
1090 case IMAGE_FILE_MACHINE_AMD64
: MESSAGE("AMD-64"); break;
1091 case IMAGE_FILE_MACHINE_ARM
: MESSAGE("ARM"); break;
1092 default: MESSAGE("Unknown-%04x", nt
->FileHeader
.Machine
); break;
1098 /* check for non page-aligned binary */
1100 if (nt
->OptionalHeader
.SectionAlignment
<= page_mask
)
1102 /* unaligned sections, this happens for native subsystem binaries */
1103 /* in that case Windows simply maps in the whole file */
1105 if (map_file_into_view( view
, fd
, 0, total_size
, 0, VPROT_COMMITTED
| VPROT_READ
,
1106 !dup_mapping
) != STATUS_SUCCESS
) goto error
;
1108 /* check that all sections are loaded at the right offset */
1109 if (nt
->OptionalHeader
.FileAlignment
!= nt
->OptionalHeader
.SectionAlignment
) goto error
;
1110 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++)
1112 if (sec
[i
].VirtualAddress
!= sec
[i
].PointerToRawData
)
1113 goto error
; /* Windows refuses to load in that case too */
1116 /* set the image protections */
1117 VIRTUAL_SetProt( view
, ptr
, total_size
,
1118 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
| VPROT_EXEC
);
1120 /* no relocations are performed on non page-aligned binaries */
1125 /* map all the sections */
1127 for (i
= pos
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
1129 static const SIZE_T sector_align
= 0x1ff;
1130 SIZE_T map_size
, file_start
, file_size
, end
;
1132 if (!sec
->Misc
.VirtualSize
)
1133 map_size
= ROUND_SIZE( 0, sec
->SizeOfRawData
);
1135 map_size
= ROUND_SIZE( 0, sec
->Misc
.VirtualSize
);
1137 /* file positions are rounded to sector boundaries regardless of OptionalHeader.FileAlignment */
1138 file_start
= sec
->PointerToRawData
& ~sector_align
;
1139 file_size
= (sec
->SizeOfRawData
+ (sec
->PointerToRawData
& sector_align
) + sector_align
) & ~sector_align
;
1140 if (file_size
> map_size
) file_size
= map_size
;
1142 /* a few sanity checks */
1143 end
= sec
->VirtualAddress
+ ROUND_SIZE( sec
->VirtualAddress
, map_size
);
1144 if (sec
->VirtualAddress
> total_size
|| end
> total_size
|| end
< sec
->VirtualAddress
)
1146 WARN_(module
)( "Section %.8s too large (%x+%lx/%lx)\n",
1147 sec
->Name
, sec
->VirtualAddress
, map_size
, total_size
);
1151 if ((sec
->Characteristics
& IMAGE_SCN_MEM_SHARED
) &&
1152 (sec
->Characteristics
& IMAGE_SCN_MEM_WRITE
))
1154 TRACE_(module
)( "mapping shared section %.8s at %p off %x (%x) size %lx (%lx) flags %x\n",
1155 sec
->Name
, ptr
+ sec
->VirtualAddress
,
1156 sec
->PointerToRawData
, (int)pos
, file_size
, map_size
,
1157 sec
->Characteristics
);
1158 if (map_file_into_view( view
, shared_fd
, sec
->VirtualAddress
, map_size
, pos
,
1159 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITE
,
1160 FALSE
) != STATUS_SUCCESS
)
1162 ERR_(module
)( "Could not map shared section %.8s\n", sec
->Name
);
1166 /* check if the import directory falls inside this section */
1167 if (imports
&& imports
->VirtualAddress
>= sec
->VirtualAddress
&&
1168 imports
->VirtualAddress
< sec
->VirtualAddress
+ map_size
)
1170 UINT_PTR base
= imports
->VirtualAddress
& ~page_mask
;
1171 UINT_PTR end
= base
+ ROUND_SIZE( imports
->VirtualAddress
, imports
->Size
);
1172 if (end
> sec
->VirtualAddress
+ map_size
) end
= sec
->VirtualAddress
+ map_size
;
1174 map_file_into_view( view
, shared_fd
, base
, end
- base
,
1175 pos
+ (base
- sec
->VirtualAddress
),
1176 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
,
1183 TRACE_(module
)( "mapping section %.8s at %p off %x size %x virt %x flags %x\n",
1184 sec
->Name
, ptr
+ sec
->VirtualAddress
,
1185 sec
->PointerToRawData
, sec
->SizeOfRawData
,
1186 sec
->Misc
.VirtualSize
, sec
->Characteristics
);
1188 if (!sec
->PointerToRawData
|| !file_size
) continue;
1190 /* Note: if the section is not aligned properly map_file_into_view will magically
1191 * fall back to read(), so we don't need to check anything here.
1193 end
= file_start
+ file_size
;
1194 if (sec
->PointerToRawData
>= st
.st_size
||
1195 end
> ((st
.st_size
+ sector_align
) & ~sector_align
) ||
1197 map_file_into_view( view
, fd
, sec
->VirtualAddress
, file_size
, file_start
,
1198 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
,
1199 !dup_mapping
) != STATUS_SUCCESS
)
1201 ERR_(module
)( "Could not map section %.8s, file probably truncated\n", sec
->Name
);
1205 if (file_size
& page_mask
)
1207 end
= ROUND_SIZE( 0, file_size
);
1208 if (end
> map_size
) end
= map_size
;
1209 TRACE_(module
)("clearing %p - %p\n",
1210 ptr
+ sec
->VirtualAddress
+ file_size
,
1211 ptr
+ sec
->VirtualAddress
+ end
);
1212 memset( ptr
+ sec
->VirtualAddress
+ file_size
, 0, end
- file_size
);
1217 /* perform base relocation, if necessary */
1220 ((nt
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
) ||
1221 !NtCurrentTeb()->Peb
->ImageBaseAddress
) )
1223 IMAGE_BASE_RELOCATION
*rel
, *end
;
1224 const IMAGE_DATA_DIRECTORY
*relocs
;
1226 if (nt
->FileHeader
.Characteristics
& IMAGE_FILE_RELOCS_STRIPPED
)
1228 WARN_(module
)( "Need to relocate module from %p to %p, but there are no relocation records\n",
1230 status
= STATUS_CONFLICTING_ADDRESSES
;
1234 TRACE_(module
)( "relocating from %p-%p to %p-%p\n",
1235 base
, base
+ total_size
, ptr
, ptr
+ total_size
);
1237 relocs
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1238 rel
= (IMAGE_BASE_RELOCATION
*)(ptr
+ relocs
->VirtualAddress
);
1239 end
= (IMAGE_BASE_RELOCATION
*)(ptr
+ relocs
->VirtualAddress
+ relocs
->Size
);
1242 while (rel
< end
- 1 && rel
->SizeOfBlock
)
1244 if (rel
->VirtualAddress
>= total_size
)
1246 WARN_(module
)( "invalid address %p in relocation %p\n", ptr
+ rel
->VirtualAddress
, rel
);
1247 status
= STATUS_ACCESS_VIOLATION
;
1250 rel
= LdrProcessRelocationBlock( ptr
+ rel
->VirtualAddress
,
1251 (rel
->SizeOfBlock
- sizeof(*rel
)) / sizeof(USHORT
),
1252 (USHORT
*)(rel
+ 1), delta
);
1253 if (!rel
) goto error
;
1257 /* set the image protections */
1259 VIRTUAL_SetProt( view
, ptr
, ROUND_SIZE( 0, header_size
), VPROT_COMMITTED
| VPROT_READ
);
1261 sec
= (IMAGE_SECTION_HEADER
*)((char *)&nt
->OptionalHeader
+nt
->FileHeader
.SizeOfOptionalHeader
);
1262 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
1265 BYTE vprot
= VPROT_COMMITTED
;
1267 if (sec
->Misc
.VirtualSize
)
1268 size
= ROUND_SIZE( sec
->VirtualAddress
, sec
->Misc
.VirtualSize
);
1270 size
= ROUND_SIZE( sec
->VirtualAddress
, sec
->SizeOfRawData
);
1272 if (sec
->Characteristics
& IMAGE_SCN_MEM_READ
) vprot
|= VPROT_READ
;
1273 if (sec
->Characteristics
& IMAGE_SCN_MEM_WRITE
) vprot
|= VPROT_READ
|VPROT_WRITECOPY
;
1274 if (sec
->Characteristics
& IMAGE_SCN_MEM_EXECUTE
) vprot
|= VPROT_EXEC
;
1276 /* Dumb game crack lets the AOEP point into a data section. Adjust. */
1277 if ((nt
->OptionalHeader
.AddressOfEntryPoint
>= sec
->VirtualAddress
) &&
1278 (nt
->OptionalHeader
.AddressOfEntryPoint
< sec
->VirtualAddress
+ size
))
1279 vprot
|= VPROT_EXEC
;
1281 VIRTUAL_SetProt( view
, ptr
+ sec
->VirtualAddress
, size
, vprot
);
1285 view
->mapping
= dup_mapping
;
1286 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1289 #ifdef VALGRIND_LOAD_PDB_DEBUGINFO
1290 VALGRIND_LOAD_PDB_DEBUGINFO(fd
, ptr
, total_size
, delta
);
1292 return STATUS_SUCCESS
;
1295 if (view
) delete_view( view
);
1296 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1297 if (dup_mapping
) NtClose( dup_mapping
);
1302 /* callback for wine_mmap_enum_reserved_areas to allocate space for the virtual heap */
1303 static int alloc_virtual_heap( void *base
, size_t size
, void *arg
)
1305 void **heap_base
= arg
;
1307 if (is_beyond_limit( base
, size
, address_space_limit
)) address_space_limit
= (char *)base
+ size
;
1308 if (size
< VIRTUAL_HEAP_SIZE
) return 0;
1309 *heap_base
= wine_anon_mmap( (char *)base
+ size
- VIRTUAL_HEAP_SIZE
,
1310 VIRTUAL_HEAP_SIZE
, PROT_READ
|PROT_WRITE
, MAP_FIXED
);
1311 return (*heap_base
!= (void *)-1);
1314 /***********************************************************************
1317 void virtual_init(void)
1319 const char *preload
;
1321 struct file_view
*heap_view
;
1324 page_size
= getpagesize();
1325 page_mask
= page_size
- 1;
1326 /* Make sure we have a power of 2 */
1327 assert( !(page_size
& page_mask
) );
1329 while ((1 << page_shift
) != page_size
) page_shift
++;
1330 user_space_limit
= working_set_limit
= address_space_limit
= (void *)~page_mask
;
1331 #endif /* page_mask */
1332 if ((preload
= getenv("WINEPRELOADRESERVE")))
1334 unsigned long start
, end
;
1335 if (sscanf( preload
, "%lx-%lx", &start
, &end
) == 2)
1337 preload_reserve_start
= (void *)start
;
1338 preload_reserve_end
= (void *)end
;
1342 /* try to find space in a reserved area for the virtual heap */
1343 if (!wine_mmap_enum_reserved_areas( alloc_virtual_heap
, &heap_base
, 1 ))
1344 heap_base
= wine_anon_mmap( NULL
, VIRTUAL_HEAP_SIZE
, PROT_READ
|PROT_WRITE
, 0 );
1346 assert( heap_base
!= (void *)-1 );
1347 virtual_heap
= RtlCreateHeap( HEAP_NO_SERIALIZE
, heap_base
, VIRTUAL_HEAP_SIZE
,
1348 VIRTUAL_HEAP_SIZE
, NULL
, NULL
);
1349 create_view( &heap_view
, heap_base
, VIRTUAL_HEAP_SIZE
, VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITE
);
1353 /***********************************************************************
1354 * virtual_init_threading
1356 void virtual_init_threading(void)
1362 /***********************************************************************
1363 * virtual_get_system_info
1365 void virtual_get_system_info( SYSTEM_BASIC_INFORMATION
*info
)
1367 info
->dwUnknown1
= 0;
1368 info
->uKeMaximumIncrement
= 0; /* FIXME */
1369 info
->uPageSize
= page_size
;
1370 info
->uMmLowestPhysicalPage
= 1;
1371 info
->uMmHighestPhysicalPage
= 0x7fffffff / page_size
;
1372 info
->uMmNumberOfPhysicalPages
= info
->uMmHighestPhysicalPage
- info
->uMmLowestPhysicalPage
;
1373 info
->uAllocationGranularity
= get_mask(0) + 1;
1374 info
->pLowestUserAddress
= (void *)0x10000;
1375 info
->pMmHighestUserAddress
= (char *)user_space_limit
- 1;
1376 info
->uKeActiveProcessors
= NtCurrentTeb()->Peb
->NumberOfProcessors
;
1377 info
->bKeNumberProcessors
= info
->uKeActiveProcessors
;
1381 /***********************************************************************
1382 * virtual_create_system_view
1384 NTSTATUS
virtual_create_system_view( void *base
, SIZE_T size
, DWORD vprot
)
1390 size
= ROUND_SIZE( base
, size
);
1391 base
= ROUND_ADDR( base
, page_mask
);
1392 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1393 status
= create_view( &view
, base
, size
, vprot
);
1394 if (!status
) TRACE( "created %p-%p\n", base
, (char *)base
+ size
);
1395 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1400 /***********************************************************************
1401 * virtual_alloc_thread_stack
1403 NTSTATUS
virtual_alloc_thread_stack( TEB
*teb
, SIZE_T reserve_size
, SIZE_T commit_size
)
1410 if (!reserve_size
|| !commit_size
)
1412 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( NtCurrentTeb()->Peb
->ImageBaseAddress
);
1413 if (!reserve_size
) reserve_size
= nt
->OptionalHeader
.SizeOfStackReserve
;
1414 if (!commit_size
) commit_size
= nt
->OptionalHeader
.SizeOfStackCommit
;
1417 size
= max( reserve_size
, commit_size
);
1418 if (size
< 1024 * 1024) size
= 1024 * 1024; /* Xlib needs a large stack */
1419 size
= (size
+ 0xffff) & ~0xffff; /* round to 64K boundary */
1421 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1423 if ((status
= map_view( &view
, NULL
, size
, 0xffff, 0,
1424 VPROT_READ
| VPROT_WRITE
| VPROT_COMMITTED
| VPROT_VALLOC
)) != STATUS_SUCCESS
)
1427 #ifdef VALGRIND_STACK_REGISTER
1428 VALGRIND_STACK_REGISTER( view
->base
, (char *)view
->base
+ view
->size
);
1431 /* setup no access guard page */
1432 VIRTUAL_SetProt( view
, view
->base
, page_size
, VPROT_COMMITTED
);
1433 VIRTUAL_SetProt( view
, (char *)view
->base
+ page_size
, page_size
,
1434 VPROT_READ
| VPROT_WRITE
| VPROT_COMMITTED
| VPROT_GUARD
);
1436 /* note: limit is lower than base since the stack grows down */
1437 teb
->DeallocationStack
= view
->base
;
1438 teb
->Tib
.StackBase
= (char *)view
->base
+ view
->size
;
1439 teb
->Tib
.StackLimit
= (char *)view
->base
+ 2 * page_size
;
1441 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1446 /***********************************************************************
1447 * virtual_clear_thread_stack
1449 * Clear the stack contents before calling the main entry point, some broken apps need that.
1451 void virtual_clear_thread_stack(void)
1453 void *stack
= NtCurrentTeb()->Tib
.StackLimit
;
1454 size_t size
= (char *)NtCurrentTeb()->Tib
.StackBase
- (char *)NtCurrentTeb()->Tib
.StackLimit
;
1456 wine_anon_mmap( stack
, size
, PROT_READ
| PROT_WRITE
, MAP_FIXED
);
1457 if (force_exec_prot
) mprotect( stack
, size
, PROT_READ
| PROT_WRITE
| PROT_EXEC
);
1461 /***********************************************************************
1462 * virtual_handle_fault
1464 NTSTATUS
virtual_handle_fault( LPCVOID addr
, DWORD err
)
1467 NTSTATUS ret
= STATUS_ACCESS_VIOLATION
;
1470 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1471 if ((view
= VIRTUAL_FindView( addr
, 0 )))
1473 void *page
= ROUND_ADDR( addr
, page_mask
);
1474 BYTE
*vprot
= &view
->prot
[((const char *)page
- (const char *)view
->base
) >> page_shift
];
1475 if (*vprot
& VPROT_GUARD
)
1477 VIRTUAL_SetProt( view
, page
, page_size
, *vprot
& ~VPROT_GUARD
);
1478 ret
= STATUS_GUARD_PAGE_VIOLATION
;
1480 if ((err
& EXCEPTION_WRITE_FAULT
) && (view
->protect
& VPROT_WRITEWATCH
))
1482 if (*vprot
& VPROT_WRITEWATCH
)
1484 *vprot
&= ~VPROT_WRITEWATCH
;
1485 VIRTUAL_SetProt( view
, page
, page_size
, *vprot
);
1487 /* ignore fault if page is writable now */
1488 if (VIRTUAL_GetUnixProt( *vprot
) & PROT_WRITE
) ret
= STATUS_SUCCESS
;
1491 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1497 /***********************************************************************
1498 * virtual_handle_stack_fault
1500 * Handle an access fault inside the current thread stack.
1501 * Called from inside a signal handler.
1503 BOOL
virtual_handle_stack_fault( void *addr
)
1508 RtlEnterCriticalSection( &csVirtual
); /* no need for signal masking inside signal handler */
1509 if ((view
= VIRTUAL_FindView( addr
, 0 )))
1511 void *page
= ROUND_ADDR( addr
, page_mask
);
1512 BYTE vprot
= view
->prot
[((const char *)page
- (const char *)view
->base
) >> page_shift
];
1513 if (vprot
& VPROT_GUARD
)
1515 VIRTUAL_SetProt( view
, page
, page_size
, vprot
& ~VPROT_GUARD
);
1516 if ((char *)page
+ page_size
== NtCurrentTeb()->Tib
.StackLimit
)
1517 NtCurrentTeb()->Tib
.StackLimit
= page
;
1521 RtlLeaveCriticalSection( &csVirtual
);
1526 /***********************************************************************
1527 * virtual_check_buffer_for_read
1529 * Check if a memory buffer can be read, triggering page faults if needed for DIB section access.
1531 BOOL
virtual_check_buffer_for_read( const void *ptr
, SIZE_T size
)
1533 if (!size
) return TRUE
;
1534 if (!ptr
) return FALSE
;
1538 volatile const char *p
= ptr
;
1540 SIZE_T count
= size
;
1542 while (count
> page_size
)
1549 dummy
= p
[count
- 1];
1560 /***********************************************************************
1561 * virtual_check_buffer_for_write
1563 * Check if a memory buffer can be written to, triggering page faults if needed for write watches.
1565 BOOL
virtual_check_buffer_for_write( void *ptr
, SIZE_T size
)
1567 if (!size
) return TRUE
;
1568 if (!ptr
) return FALSE
;
1572 volatile char *p
= ptr
;
1573 SIZE_T count
= size
;
1575 while (count
> page_size
)
1593 /***********************************************************************
1594 * VIRTUAL_SetForceExec
1596 * Whether to force exec prot on all views.
1598 void VIRTUAL_SetForceExec( BOOL enable
)
1600 struct file_view
*view
;
1603 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1604 if (!force_exec_prot
!= !enable
) /* change all existing views */
1606 force_exec_prot
= enable
;
1608 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
1611 char *addr
= view
->base
;
1612 BYTE commit
= view
->mapping
? VPROT_COMMITTED
: 0; /* file mappings are always accessible */
1613 int unix_prot
= VIRTUAL_GetUnixProt( view
->prot
[0] | commit
);
1615 if (view
->protect
& VPROT_NOEXEC
) continue;
1616 for (count
= i
= 1; i
< view
->size
>> page_shift
; i
++, count
++)
1618 int prot
= VIRTUAL_GetUnixProt( view
->prot
[i
] | commit
);
1619 if (prot
== unix_prot
) continue;
1620 if ((unix_prot
& PROT_READ
) && !(unix_prot
& PROT_EXEC
))
1622 TRACE( "%s exec prot for %p-%p\n",
1623 force_exec_prot
? "enabling" : "disabling",
1624 addr
, addr
+ (count
<< page_shift
) - 1 );
1625 mprotect( addr
, count
<< page_shift
,
1626 unix_prot
| (force_exec_prot
? PROT_EXEC
: 0) );
1628 addr
+= (count
<< page_shift
);
1634 if ((unix_prot
& PROT_READ
) && !(unix_prot
& PROT_EXEC
))
1636 TRACE( "%s exec prot for %p-%p\n",
1637 force_exec_prot
? "enabling" : "disabling",
1638 addr
, addr
+ (count
<< page_shift
) - 1 );
1639 mprotect( addr
, count
<< page_shift
,
1640 unix_prot
| (force_exec_prot
? PROT_EXEC
: 0) );
1645 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1654 /* free reserved areas above the limit; callback for wine_mmap_enum_reserved_areas */
1655 static int free_reserved_memory( void *base
, size_t size
, void *arg
)
1657 struct free_range
*range
= arg
;
1659 if ((char *)base
>= range
->limit
) return 0;
1660 if ((char *)base
+ size
<= range
->base
) return 0;
1661 if ((char *)base
< range
->base
)
1663 size
-= range
->base
- (char *)base
;
1666 if ((char *)base
+ size
> range
->limit
) size
= range
->limit
- (char *)base
;
1667 remove_reserved_area( base
, size
);
1668 return 1; /* stop enumeration since the list has changed */
1671 /***********************************************************************
1672 * virtual_release_address_space
1674 * Release some address space once we have loaded and initialized the app.
1676 void virtual_release_address_space( BOOL free_high_mem
)
1679 struct free_range range
;
1682 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1684 #ifndef __APPLE__ /* dyld doesn't support parts of the WINE_DOS segment being unmapped */
1685 range
.base
= (char *)0x20000000;
1686 range
.limit
= (char *)0x7f000000;
1687 while (wine_mmap_enum_reserved_areas( free_reserved_memory
, &range
, 0 )) /* nothing */;
1690 /* no large address space on win9x */
1691 if (free_high_mem
&& NtCurrentTeb()->Peb
->OSPlatformId
== VER_PLATFORM_WIN32_NT
)
1693 range
.base
= (char *)0x82000000;
1694 range
.limit
= address_space_limit
;
1695 while (wine_mmap_enum_reserved_areas( free_reserved_memory
, &range
, 1 )) /* nothing */;
1696 user_space_limit
= working_set_limit
= address_space_limit
;
1698 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1703 /***********************************************************************
1704 * NtAllocateVirtualMemory (NTDLL.@)
1705 * ZwAllocateVirtualMemory (NTDLL.@)
1707 NTSTATUS WINAPI
NtAllocateVirtualMemory( HANDLE process
, PVOID
*ret
, ULONG zero_bits
,
1708 SIZE_T
*size_ptr
, ULONG type
, ULONG protect
)
1712 SIZE_T size
= *size_ptr
;
1713 SIZE_T mask
= get_mask( zero_bits
);
1714 NTSTATUS status
= STATUS_SUCCESS
;
1715 struct file_view
*view
;
1718 TRACE("%p %p %08lx %x %08x\n", process
, *ret
, size
, type
, protect
);
1720 if (!size
) return STATUS_INVALID_PARAMETER
;
1722 if (process
!= NtCurrentProcess())
1725 apc_result_t result
;
1727 memset( &call
, 0, sizeof(call
) );
1729 call
.virtual_alloc
.type
= APC_VIRTUAL_ALLOC
;
1730 call
.virtual_alloc
.addr
= wine_server_client_ptr( *ret
);
1731 call
.virtual_alloc
.size
= *size_ptr
;
1732 call
.virtual_alloc
.zero_bits
= zero_bits
;
1733 call
.virtual_alloc
.op_type
= type
;
1734 call
.virtual_alloc
.prot
= protect
;
1735 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
1736 if (status
!= STATUS_SUCCESS
) return status
;
1738 if (result
.virtual_alloc
.status
== STATUS_SUCCESS
)
1740 *ret
= wine_server_get_ptr( result
.virtual_alloc
.addr
);
1741 *size_ptr
= result
.virtual_alloc
.size
;
1743 return result
.virtual_alloc
.status
;
1746 /* Round parameters to a page boundary */
1748 if (is_beyond_limit( 0, size
, working_set_limit
)) return STATUS_WORKING_SET_LIMIT_RANGE
;
1750 if ((status
= get_vprot_flags( protect
, &vprot
))) return status
;
1751 vprot
|= VPROT_VALLOC
;
1752 if (type
& MEM_COMMIT
) vprot
|= VPROT_COMMITTED
;
1756 if (type
& MEM_RESERVE
) /* Round down to 64k boundary */
1757 base
= ROUND_ADDR( *ret
, mask
);
1759 base
= ROUND_ADDR( *ret
, page_mask
);
1760 size
= (((UINT_PTR
)*ret
+ size
+ page_mask
) & ~page_mask
) - (UINT_PTR
)base
;
1762 /* address 1 is magic to mean DOS area */
1763 if (!base
&& *ret
== (void *)1 && size
== 0x110000)
1765 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1766 status
= allocate_dos_memory( &view
, vprot
);
1767 if (status
== STATUS_SUCCESS
)
1770 *size_ptr
= view
->size
;
1772 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1776 /* disallow low 64k, wrap-around and kernel space */
1777 if (((char *)base
< (char *)0x10000) ||
1778 ((char *)base
+ size
< (char *)base
) ||
1779 is_beyond_limit( base
, size
, address_space_limit
))
1780 return STATUS_INVALID_PARAMETER
;
1785 size
= (size
+ page_mask
) & ~page_mask
;
1788 /* Compute the alloc type flags */
1790 if (!(type
& (MEM_COMMIT
| MEM_RESERVE
| MEM_RESET
)) ||
1791 (type
& ~(MEM_COMMIT
| MEM_RESERVE
| MEM_TOP_DOWN
| MEM_WRITE_WATCH
| MEM_RESET
)))
1793 WARN("called with wrong alloc type flags (%08x) !\n", type
);
1794 return STATUS_INVALID_PARAMETER
;
1797 /* Reserve the memory */
1799 if (use_locks
) server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1801 if ((type
& MEM_RESERVE
) || !base
)
1803 if (type
& MEM_WRITE_WATCH
) vprot
|= VPROT_WRITEWATCH
;
1804 status
= map_view( &view
, base
, size
, mask
, type
& MEM_TOP_DOWN
, vprot
);
1805 if (status
== STATUS_SUCCESS
) base
= view
->base
;
1807 else if (type
& MEM_RESET
)
1809 if (!(view
= VIRTUAL_FindView( base
, size
))) status
= STATUS_NOT_MAPPED_VIEW
;
1810 else madvise( base
, size
, MADV_DONTNEED
);
1812 else /* commit the pages */
1814 if (!(view
= VIRTUAL_FindView( base
, size
))) status
= STATUS_NOT_MAPPED_VIEW
;
1815 else if (!VIRTUAL_SetProt( view
, base
, size
, vprot
)) status
= STATUS_ACCESS_DENIED
;
1816 else if (view
->mapping
&& !(view
->protect
& VPROT_COMMITTED
))
1818 SERVER_START_REQ( add_mapping_committed_range
)
1820 req
->handle
= wine_server_obj_handle( view
->mapping
);
1821 req
->offset
= (char *)base
- (char *)view
->base
;
1823 wine_server_call( req
);
1829 if (use_locks
) server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1831 if (status
== STATUS_SUCCESS
)
1840 /***********************************************************************
1841 * NtFreeVirtualMemory (NTDLL.@)
1842 * ZwFreeVirtualMemory (NTDLL.@)
1844 NTSTATUS WINAPI
NtFreeVirtualMemory( HANDLE process
, PVOID
*addr_ptr
, SIZE_T
*size_ptr
, ULONG type
)
1849 NTSTATUS status
= STATUS_SUCCESS
;
1850 LPVOID addr
= *addr_ptr
;
1851 SIZE_T size
= *size_ptr
;
1853 TRACE("%p %p %08lx %x\n", process
, addr
, size
, type
);
1855 if (process
!= NtCurrentProcess())
1858 apc_result_t result
;
1860 memset( &call
, 0, sizeof(call
) );
1862 call
.virtual_free
.type
= APC_VIRTUAL_FREE
;
1863 call
.virtual_free
.addr
= wine_server_client_ptr( addr
);
1864 call
.virtual_free
.size
= size
;
1865 call
.virtual_free
.op_type
= type
;
1866 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
1867 if (status
!= STATUS_SUCCESS
) return status
;
1869 if (result
.virtual_free
.status
== STATUS_SUCCESS
)
1871 *addr_ptr
= wine_server_get_ptr( result
.virtual_free
.addr
);
1872 *size_ptr
= result
.virtual_free
.size
;
1874 return result
.virtual_free
.status
;
1877 /* Fix the parameters */
1879 size
= ROUND_SIZE( addr
, size
);
1880 base
= ROUND_ADDR( addr
, page_mask
);
1882 /* avoid freeing the DOS area when a broken app passes a NULL pointer */
1883 if (!base
) return STATUS_INVALID_PARAMETER
;
1885 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1887 if (!(view
= VIRTUAL_FindView( base
, size
)) || !(view
->protect
& VPROT_VALLOC
))
1889 status
= STATUS_INVALID_PARAMETER
;
1891 else if (type
== MEM_RELEASE
)
1893 /* Free the pages */
1895 if (size
|| (base
!= view
->base
)) status
= STATUS_INVALID_PARAMETER
;
1898 delete_view( view
);
1903 else if (type
== MEM_DECOMMIT
)
1905 status
= decommit_pages( view
, base
- (char *)view
->base
, size
);
1906 if (status
== STATUS_SUCCESS
)
1914 WARN("called with wrong free type flags (%08x) !\n", type
);
1915 status
= STATUS_INVALID_PARAMETER
;
1918 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1923 /***********************************************************************
1924 * NtProtectVirtualMemory (NTDLL.@)
1925 * ZwProtectVirtualMemory (NTDLL.@)
1927 NTSTATUS WINAPI
NtProtectVirtualMemory( HANDLE process
, PVOID
*addr_ptr
, SIZE_T
*size_ptr
,
1928 ULONG new_prot
, ULONG
*old_prot
)
1932 NTSTATUS status
= STATUS_SUCCESS
;
1935 unsigned int new_vprot
;
1936 SIZE_T size
= *size_ptr
;
1937 LPVOID addr
= *addr_ptr
;
1939 TRACE("%p %p %08lx %08x\n", process
, addr
, size
, new_prot
);
1941 if (process
!= NtCurrentProcess())
1944 apc_result_t result
;
1946 memset( &call
, 0, sizeof(call
) );
1948 call
.virtual_protect
.type
= APC_VIRTUAL_PROTECT
;
1949 call
.virtual_protect
.addr
= wine_server_client_ptr( addr
);
1950 call
.virtual_protect
.size
= size
;
1951 call
.virtual_protect
.prot
= new_prot
;
1952 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
1953 if (status
!= STATUS_SUCCESS
) return status
;
1955 if (result
.virtual_protect
.status
== STATUS_SUCCESS
)
1957 *addr_ptr
= wine_server_get_ptr( result
.virtual_protect
.addr
);
1958 *size_ptr
= result
.virtual_protect
.size
;
1959 if (old_prot
) *old_prot
= result
.virtual_protect
.prot
;
1961 return result
.virtual_protect
.status
;
1964 /* Fix the parameters */
1966 size
= ROUND_SIZE( addr
, size
);
1967 base
= ROUND_ADDR( addr
, page_mask
);
1968 if ((status
= get_vprot_flags( new_prot
, &new_vprot
))) return status
;
1969 new_vprot
|= VPROT_COMMITTED
;
1971 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1973 if (!(view
= VIRTUAL_FindView( base
, size
)))
1975 status
= STATUS_INVALID_PARAMETER
;
1979 /* Make sure all the pages are committed */
1980 if (get_committed_size( view
, base
, &vprot
) >= size
&& (vprot
& VPROT_COMMITTED
))
1982 if (old_prot
) *old_prot
= VIRTUAL_GetWin32Prot( vprot
);
1983 if (!VIRTUAL_SetProt( view
, base
, size
, new_vprot
)) status
= STATUS_ACCESS_DENIED
;
1985 else status
= STATUS_NOT_COMMITTED
;
1987 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1989 if (status
== STATUS_SUCCESS
)
1998 /* retrieve state for a free memory area; callback for wine_mmap_enum_reserved_areas */
1999 static int get_free_mem_state_callback( void *start
, size_t size
, void *arg
)
2001 MEMORY_BASIC_INFORMATION
*info
= arg
;
2002 void *end
= (char *)start
+ size
;
2004 if ((char *)info
->BaseAddress
+ info
->RegionSize
< (char *)start
) return 0;
2006 if (info
->BaseAddress
>= end
)
2008 if (info
->AllocationBase
< end
) info
->AllocationBase
= end
;
2012 if (info
->BaseAddress
>= start
)
2014 /* it's a real free area */
2015 info
->State
= MEM_FREE
;
2016 info
->Protect
= PAGE_NOACCESS
;
2017 info
->AllocationBase
= 0;
2018 info
->AllocationProtect
= 0;
2020 if ((char *)info
->BaseAddress
+ info
->RegionSize
> (char *)end
)
2021 info
->RegionSize
= (char *)end
- (char *)info
->BaseAddress
;
2023 else /* outside of the reserved area, pretend it's allocated */
2025 info
->RegionSize
= (char *)start
- (char *)info
->BaseAddress
;
2026 info
->State
= MEM_RESERVE
;
2027 info
->Protect
= PAGE_NOACCESS
;
2028 info
->AllocationProtect
= PAGE_NOACCESS
;
2029 info
->Type
= MEM_PRIVATE
;
2034 #define UNIMPLEMENTED_INFO_CLASS(c) \
2036 FIXME("(process=%p,addr=%p) Unimplemented information class: " #c "\n", process, addr); \
2037 return STATUS_INVALID_INFO_CLASS
2039 /***********************************************************************
2040 * NtQueryVirtualMemory (NTDLL.@)
2041 * ZwQueryVirtualMemory (NTDLL.@)
2043 NTSTATUS WINAPI
NtQueryVirtualMemory( HANDLE process
, LPCVOID addr
,
2044 MEMORY_INFORMATION_CLASS info_class
, PVOID buffer
,
2045 SIZE_T len
, SIZE_T
*res_len
)
2048 char *base
, *alloc_base
= 0;
2051 MEMORY_BASIC_INFORMATION
*info
= buffer
;
2054 if (info_class
!= MemoryBasicInformation
)
2058 UNIMPLEMENTED_INFO_CLASS(MemoryWorkingSetList
);
2059 UNIMPLEMENTED_INFO_CLASS(MemorySectionName
);
2060 UNIMPLEMENTED_INFO_CLASS(MemoryBasicVlmInformation
);
2063 FIXME("(%p,%p,info_class=%d,%p,%ld,%p) Unknown information class\n",
2064 process
, addr
, info_class
, buffer
, len
, res_len
);
2065 return STATUS_INVALID_INFO_CLASS
;
2069 if (process
!= NtCurrentProcess())
2073 apc_result_t result
;
2075 memset( &call
, 0, sizeof(call
) );
2077 call
.virtual_query
.type
= APC_VIRTUAL_QUERY
;
2078 call
.virtual_query
.addr
= wine_server_client_ptr( addr
);
2079 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
2080 if (status
!= STATUS_SUCCESS
) return status
;
2082 if (result
.virtual_query
.status
== STATUS_SUCCESS
)
2084 info
->BaseAddress
= wine_server_get_ptr( result
.virtual_query
.base
);
2085 info
->AllocationBase
= wine_server_get_ptr( result
.virtual_query
.alloc_base
);
2086 info
->RegionSize
= result
.virtual_query
.size
;
2087 info
->Protect
= result
.virtual_query
.prot
;
2088 info
->AllocationProtect
= result
.virtual_query
.alloc_prot
;
2089 info
->State
= (DWORD
)result
.virtual_query
.state
<< 12;
2090 info
->Type
= (DWORD
)result
.virtual_query
.alloc_type
<< 16;
2091 if (info
->RegionSize
!= result
.virtual_query
.size
) /* truncated */
2092 return STATUS_INVALID_PARAMETER
; /* FIXME */
2093 if (res_len
) *res_len
= sizeof(*info
);
2095 return result
.virtual_query
.status
;
2098 base
= ROUND_ADDR( addr
, page_mask
);
2100 if (is_beyond_limit( base
, 1, working_set_limit
)) return STATUS_WORKING_SET_LIMIT_RANGE
;
2102 /* Find the view containing the address */
2104 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2105 ptr
= list_head( &views_list
);
2110 size
= (char *)working_set_limit
- alloc_base
;
2114 view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
2115 if ((char *)view
->base
> base
)
2117 size
= (char *)view
->base
- alloc_base
;
2121 if ((char *)view
->base
+ view
->size
> base
)
2123 alloc_base
= view
->base
;
2127 alloc_base
= (char *)view
->base
+ view
->size
;
2128 ptr
= list_next( &views_list
, ptr
);
2131 /* Fill the info structure */
2133 info
->AllocationBase
= alloc_base
;
2134 info
->BaseAddress
= base
;
2135 info
->RegionSize
= size
- (base
- alloc_base
);
2139 if (!wine_mmap_enum_reserved_areas( get_free_mem_state_callback
, info
, 0 ))
2141 /* not in a reserved area at all, pretend it's allocated */
2143 info
->State
= MEM_RESERVE
;
2144 info
->Protect
= PAGE_NOACCESS
;
2145 info
->AllocationProtect
= PAGE_NOACCESS
;
2146 info
->Type
= MEM_PRIVATE
;
2148 info
->State
= MEM_FREE
;
2149 info
->Protect
= PAGE_NOACCESS
;
2150 info
->AllocationBase
= 0;
2151 info
->AllocationProtect
= 0;
2159 SIZE_T range_size
= get_committed_size( view
, base
, &vprot
);
2161 info
->State
= (vprot
& VPROT_COMMITTED
) ? MEM_COMMIT
: MEM_RESERVE
;
2162 info
->Protect
= (vprot
& VPROT_COMMITTED
) ? VIRTUAL_GetWin32Prot( vprot
) : 0;
2163 info
->AllocationBase
= alloc_base
;
2164 info
->AllocationProtect
= VIRTUAL_GetWin32Prot( view
->protect
);
2165 if (view
->protect
& VPROT_IMAGE
) info
->Type
= MEM_IMAGE
;
2166 else if (view
->protect
& VPROT_VALLOC
) info
->Type
= MEM_PRIVATE
;
2167 else info
->Type
= MEM_MAPPED
;
2168 for (size
= base
- alloc_base
; size
< base
+ range_size
- alloc_base
; size
+= page_size
)
2169 if ((view
->prot
[size
>> page_shift
] ^ vprot
) & ~VPROT_WRITEWATCH
) break;
2170 info
->RegionSize
= size
- (base
- alloc_base
);
2172 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2174 if (res_len
) *res_len
= sizeof(*info
);
2175 return STATUS_SUCCESS
;
2179 /***********************************************************************
2180 * NtLockVirtualMemory (NTDLL.@)
2181 * ZwLockVirtualMemory (NTDLL.@)
2183 NTSTATUS WINAPI
NtLockVirtualMemory( HANDLE process
, PVOID
*addr
, SIZE_T
*size
, ULONG unknown
)
2185 NTSTATUS status
= STATUS_SUCCESS
;
2187 if (process
!= NtCurrentProcess())
2190 apc_result_t result
;
2192 memset( &call
, 0, sizeof(call
) );
2194 call
.virtual_lock
.type
= APC_VIRTUAL_LOCK
;
2195 call
.virtual_lock
.addr
= wine_server_client_ptr( *addr
);
2196 call
.virtual_lock
.size
= *size
;
2197 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
2198 if (status
!= STATUS_SUCCESS
) return status
;
2200 if (result
.virtual_lock
.status
== STATUS_SUCCESS
)
2202 *addr
= wine_server_get_ptr( result
.virtual_lock
.addr
);
2203 *size
= result
.virtual_lock
.size
;
2205 return result
.virtual_lock
.status
;
2208 *size
= ROUND_SIZE( *addr
, *size
);
2209 *addr
= ROUND_ADDR( *addr
, page_mask
);
2211 if (mlock( *addr
, *size
)) status
= STATUS_ACCESS_DENIED
;
2216 /***********************************************************************
2217 * NtUnlockVirtualMemory (NTDLL.@)
2218 * ZwUnlockVirtualMemory (NTDLL.@)
2220 NTSTATUS WINAPI
NtUnlockVirtualMemory( HANDLE process
, PVOID
*addr
, SIZE_T
*size
, ULONG unknown
)
2222 NTSTATUS status
= STATUS_SUCCESS
;
2224 if (process
!= NtCurrentProcess())
2227 apc_result_t result
;
2229 memset( &call
, 0, sizeof(call
) );
2231 call
.virtual_unlock
.type
= APC_VIRTUAL_UNLOCK
;
2232 call
.virtual_unlock
.addr
= wine_server_client_ptr( *addr
);
2233 call
.virtual_unlock
.size
= *size
;
2234 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
2235 if (status
!= STATUS_SUCCESS
) return status
;
2237 if (result
.virtual_unlock
.status
== STATUS_SUCCESS
)
2239 *addr
= wine_server_get_ptr( result
.virtual_unlock
.addr
);
2240 *size
= result
.virtual_unlock
.size
;
2242 return result
.virtual_unlock
.status
;
2245 *size
= ROUND_SIZE( *addr
, *size
);
2246 *addr
= ROUND_ADDR( *addr
, page_mask
);
2248 if (munlock( *addr
, *size
)) status
= STATUS_ACCESS_DENIED
;
2253 /***********************************************************************
2254 * NtCreateSection (NTDLL.@)
2255 * ZwCreateSection (NTDLL.@)
2257 NTSTATUS WINAPI
NtCreateSection( HANDLE
*handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
,
2258 const LARGE_INTEGER
*size
, ULONG protect
,
2259 ULONG sec_flags
, HANDLE file
)
2263 DWORD len
= (attr
&& attr
->ObjectName
) ? attr
->ObjectName
->Length
: 0;
2264 struct security_descriptor
*sd
= NULL
;
2265 struct object_attributes objattr
;
2267 /* Check parameters */
2269 if (len
> MAX_PATH
*sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
2271 if ((ret
= get_vprot_flags( protect
, &vprot
))) return ret
;
2273 objattr
.rootdir
= wine_server_obj_handle( attr
? attr
->RootDirectory
: 0 );
2275 objattr
.name_len
= len
;
2278 ret
= NTDLL_create_struct_sd( attr
->SecurityDescriptor
, &sd
, &objattr
.sd_len
);
2279 if (ret
!= STATUS_SUCCESS
) return ret
;
2282 if (!(sec_flags
& SEC_RESERVE
)) vprot
|= VPROT_COMMITTED
;
2283 if (sec_flags
& SEC_NOCACHE
) vprot
|= VPROT_NOCACHE
;
2284 if (sec_flags
& SEC_IMAGE
) vprot
|= VPROT_IMAGE
;
2286 /* Create the server object */
2288 SERVER_START_REQ( create_mapping
)
2290 req
->access
= access
;
2291 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
2292 req
->file_handle
= wine_server_obj_handle( file
);
2293 req
->size
= size
? size
->QuadPart
: 0;
2294 req
->protect
= vprot
;
2295 wine_server_add_data( req
, &objattr
, sizeof(objattr
) );
2296 if (objattr
.sd_len
) wine_server_add_data( req
, sd
, objattr
.sd_len
);
2297 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
2298 ret
= wine_server_call( req
);
2299 *handle
= wine_server_ptr_handle( reply
->handle
);
2303 NTDLL_free_struct_sd( sd
);
2309 /***********************************************************************
2310 * NtOpenSection (NTDLL.@)
2311 * ZwOpenSection (NTDLL.@)
2313 NTSTATUS WINAPI
NtOpenSection( HANDLE
*handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
)
2316 DWORD len
= attr
->ObjectName
->Length
;
2318 if (len
> MAX_PATH
*sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
2320 SERVER_START_REQ( open_mapping
)
2322 req
->access
= access
;
2323 req
->attributes
= attr
->Attributes
;
2324 req
->rootdir
= wine_server_obj_handle( attr
->RootDirectory
);
2325 wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
2326 if (!(ret
= wine_server_call( req
))) *handle
= wine_server_ptr_handle( reply
->handle
);
2333 /***********************************************************************
2334 * NtMapViewOfSection (NTDLL.@)
2335 * ZwMapViewOfSection (NTDLL.@)
2337 NTSTATUS WINAPI
NtMapViewOfSection( HANDLE handle
, HANDLE process
, PVOID
*addr_ptr
, ULONG zero_bits
,
2338 SIZE_T commit_size
, const LARGE_INTEGER
*offset_ptr
, SIZE_T
*size_ptr
,
2339 SECTION_INHERIT inherit
, ULONG alloc_type
, ULONG protect
)
2342 mem_size_t full_size
;
2344 SIZE_T size
, mask
= get_mask( zero_bits
);
2345 int unix_handle
= -1, needs_close
;
2346 unsigned int map_vprot
, vprot
;
2348 struct file_view
*view
;
2350 HANDLE dup_mapping
, shared_file
;
2351 LARGE_INTEGER offset
;
2354 offset
.QuadPart
= offset_ptr
? offset_ptr
->QuadPart
: 0;
2356 TRACE("handle=%p process=%p addr=%p off=%x%08x size=%lx access=%x\n",
2357 handle
, process
, *addr_ptr
, offset
.u
.HighPart
, offset
.u
.LowPart
, *size_ptr
, protect
);
2359 /* Check parameters */
2361 if ((offset
.u
.LowPart
& mask
) || (*addr_ptr
&& ((UINT_PTR
)*addr_ptr
& mask
)))
2362 return STATUS_INVALID_PARAMETER
;
2369 case PAGE_READWRITE
:
2370 case PAGE_EXECUTE_READWRITE
:
2371 access
= SECTION_MAP_WRITE
;
2374 case PAGE_WRITECOPY
:
2376 case PAGE_EXECUTE_READ
:
2377 case PAGE_EXECUTE_WRITECOPY
:
2378 access
= SECTION_MAP_READ
;
2381 return STATUS_INVALID_PARAMETER
;
2384 if (process
!= NtCurrentProcess())
2387 apc_result_t result
;
2389 memset( &call
, 0, sizeof(call
) );
2391 call
.map_view
.type
= APC_MAP_VIEW
;
2392 call
.map_view
.handle
= wine_server_obj_handle( handle
);
2393 call
.map_view
.addr
= wine_server_client_ptr( *addr_ptr
);
2394 call
.map_view
.size
= *size_ptr
;
2395 call
.map_view
.offset
= offset
.QuadPart
;
2396 call
.map_view
.zero_bits
= zero_bits
;
2397 call
.map_view
.alloc_type
= alloc_type
;
2398 call
.map_view
.prot
= protect
;
2399 res
= NTDLL_queue_process_apc( process
, &call
, &result
);
2400 if (res
!= STATUS_SUCCESS
) return res
;
2402 if (result
.map_view
.status
== STATUS_SUCCESS
)
2404 *addr_ptr
= wine_server_get_ptr( result
.map_view
.addr
);
2405 *size_ptr
= result
.map_view
.size
;
2407 return result
.map_view
.status
;
2410 SERVER_START_REQ( get_mapping_info
)
2412 req
->handle
= wine_server_obj_handle( handle
);
2413 req
->access
= access
;
2414 res
= wine_server_call( req
);
2415 map_vprot
= reply
->protect
;
2416 base
= wine_server_get_ptr( reply
->base
);
2417 full_size
= reply
->size
;
2418 header_size
= reply
->header_size
;
2419 dup_mapping
= wine_server_ptr_handle( reply
->mapping
);
2420 shared_file
= wine_server_ptr_handle( reply
->shared_file
);
2421 if ((ULONG_PTR
)base
!= reply
->base
) base
= NULL
;
2424 if (res
) return res
;
2426 if ((res
= server_get_unix_fd( handle
, 0, &unix_handle
, &needs_close
, NULL
, NULL
))) goto done
;
2428 if (map_vprot
& VPROT_IMAGE
)
2431 if (size
!= full_size
) /* truncated */
2433 WARN( "Modules larger than 4Gb (%s) not supported\n", wine_dbgstr_longlong(full_size
) );
2434 res
= STATUS_INVALID_PARAMETER
;
2439 int shared_fd
, shared_needs_close
;
2441 if ((res
= server_get_unix_fd( shared_file
, FILE_READ_DATA
|FILE_WRITE_DATA
,
2442 &shared_fd
, &shared_needs_close
, NULL
, NULL
))) goto done
;
2443 res
= map_image( handle
, unix_handle
, base
, size
, mask
, header_size
,
2444 shared_fd
, dup_mapping
, addr_ptr
);
2445 if (shared_needs_close
) close( shared_fd
);
2446 NtClose( shared_file
);
2450 res
= map_image( handle
, unix_handle
, base
, size
, mask
, header_size
,
2451 -1, dup_mapping
, addr_ptr
);
2453 if (needs_close
) close( unix_handle
);
2454 if (!res
) *size_ptr
= size
;
2458 res
= STATUS_INVALID_PARAMETER
;
2459 if (offset
.QuadPart
>= full_size
) goto done
;
2462 if (*size_ptr
> full_size
- offset
.QuadPart
) goto done
;
2463 size
= ROUND_SIZE( offset
.u
.LowPart
, *size_ptr
);
2464 if (size
< *size_ptr
) goto done
; /* wrap-around */
2468 size
= full_size
- offset
.QuadPart
;
2469 if (size
!= full_size
- offset
.QuadPart
) /* truncated */
2471 WARN( "Files larger than 4Gb (%s) not supported on this platform\n",
2472 wine_dbgstr_longlong(full_size
) );
2477 /* Reserve a properly aligned area */
2479 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2481 get_vprot_flags( protect
, &vprot
);
2482 vprot
|= (map_vprot
& VPROT_COMMITTED
);
2483 res
= map_view( &view
, *addr_ptr
, size
, mask
, FALSE
, vprot
);
2486 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2492 TRACE("handle=%p size=%lx offset=%x%08x\n",
2493 handle
, size
, offset
.u
.HighPart
, offset
.u
.LowPart
);
2495 res
= map_file_into_view( view
, unix_handle
, 0, size
, offset
.QuadPart
, vprot
, !dup_mapping
);
2496 if (res
== STATUS_SUCCESS
)
2498 *addr_ptr
= view
->base
;
2500 view
->mapping
= dup_mapping
;
2501 dup_mapping
= 0; /* don't close it */
2505 ERR( "map_file_into_view %p %lx %x%08x failed\n",
2506 view
->base
, size
, offset
.u
.HighPart
, offset
.u
.LowPart
);
2507 delete_view( view
);
2510 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2513 if (dup_mapping
) NtClose( dup_mapping
);
2514 if (needs_close
) close( unix_handle
);
2519 /***********************************************************************
2520 * NtUnmapViewOfSection (NTDLL.@)
2521 * ZwUnmapViewOfSection (NTDLL.@)
2523 NTSTATUS WINAPI
NtUnmapViewOfSection( HANDLE process
, PVOID addr
)
2526 NTSTATUS status
= STATUS_INVALID_PARAMETER
;
2528 void *base
= ROUND_ADDR( addr
, page_mask
);
2530 if (process
!= NtCurrentProcess())
2533 apc_result_t result
;
2535 memset( &call
, 0, sizeof(call
) );
2537 call
.unmap_view
.type
= APC_UNMAP_VIEW
;
2538 call
.unmap_view
.addr
= wine_server_client_ptr( addr
);
2539 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
2540 if (status
== STATUS_SUCCESS
) status
= result
.unmap_view
.status
;
2544 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2545 if ((view
= VIRTUAL_FindView( base
, 0 )) && (base
== view
->base
))
2547 delete_view( view
);
2548 status
= STATUS_SUCCESS
;
2550 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2555 /***********************************************************************
2556 * NtFlushVirtualMemory (NTDLL.@)
2557 * ZwFlushVirtualMemory (NTDLL.@)
2559 NTSTATUS WINAPI
NtFlushVirtualMemory( HANDLE process
, LPCVOID
*addr_ptr
,
2560 SIZE_T
*size_ptr
, ULONG unknown
)
2563 NTSTATUS status
= STATUS_SUCCESS
;
2565 void *addr
= ROUND_ADDR( *addr_ptr
, page_mask
);
2567 if (process
!= NtCurrentProcess())
2570 apc_result_t result
;
2572 memset( &call
, 0, sizeof(call
) );
2574 call
.virtual_flush
.type
= APC_VIRTUAL_FLUSH
;
2575 call
.virtual_flush
.addr
= wine_server_client_ptr( addr
);
2576 call
.virtual_flush
.size
= *size_ptr
;
2577 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
2578 if (status
!= STATUS_SUCCESS
) return status
;
2580 if (result
.virtual_flush
.status
== STATUS_SUCCESS
)
2582 *addr_ptr
= wine_server_get_ptr( result
.virtual_flush
.addr
);
2583 *size_ptr
= result
.virtual_flush
.size
;
2585 return result
.virtual_flush
.status
;
2588 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2589 if (!(view
= VIRTUAL_FindView( addr
, *size_ptr
))) status
= STATUS_INVALID_PARAMETER
;
2592 if (!*size_ptr
) *size_ptr
= view
->size
;
2594 if (msync( addr
, *size_ptr
, MS_SYNC
)) status
= STATUS_NOT_MAPPED_DATA
;
2596 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2601 /***********************************************************************
2602 * NtGetWriteWatch (NTDLL.@)
2603 * ZwGetWriteWatch (NTDLL.@)
2605 NTSTATUS WINAPI
NtGetWriteWatch( HANDLE process
, ULONG flags
, PVOID base
, SIZE_T size
, PVOID
*addresses
,
2606 ULONG_PTR
*count
, ULONG
*granularity
)
2608 struct file_view
*view
;
2609 NTSTATUS status
= STATUS_SUCCESS
;
2612 size
= ROUND_SIZE( base
, size
);
2613 base
= ROUND_ADDR( base
, page_mask
);
2615 if (!count
|| !granularity
) return STATUS_ACCESS_VIOLATION
;
2616 if (!*count
|| !size
) return STATUS_INVALID_PARAMETER
;
2617 if (flags
& ~WRITE_WATCH_FLAG_RESET
) return STATUS_INVALID_PARAMETER
;
2619 if (!addresses
) return STATUS_ACCESS_VIOLATION
;
2621 TRACE( "%p %x %p-%p %p %lu\n", process
, flags
, base
, (char *)base
+ size
,
2622 addresses
, count
? *count
: 0 );
2624 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2626 if ((view
= VIRTUAL_FindView( base
, size
)) && (view
->protect
& VPROT_WRITEWATCH
))
2630 char *end
= addr
+ size
;
2632 while (pos
< *count
&& addr
< end
)
2634 BYTE prot
= view
->prot
[(addr
- (char *)view
->base
) >> page_shift
];
2635 if (!(prot
& VPROT_WRITEWATCH
)) addresses
[pos
++] = addr
;
2638 if (flags
& WRITE_WATCH_FLAG_RESET
) reset_write_watches( view
, base
, addr
- (char *)base
);
2640 *granularity
= page_size
;
2642 else status
= STATUS_INVALID_PARAMETER
;
2644 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2649 /***********************************************************************
2650 * NtResetWriteWatch (NTDLL.@)
2651 * ZwResetWriteWatch (NTDLL.@)
2653 NTSTATUS WINAPI
NtResetWriteWatch( HANDLE process
, PVOID base
, SIZE_T size
)
2655 struct file_view
*view
;
2656 NTSTATUS status
= STATUS_SUCCESS
;
2659 size
= ROUND_SIZE( base
, size
);
2660 base
= ROUND_ADDR( base
, page_mask
);
2662 TRACE( "%p %p-%p\n", process
, base
, (char *)base
+ size
);
2664 if (!size
) return STATUS_INVALID_PARAMETER
;
2666 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2668 if ((view
= VIRTUAL_FindView( base
, size
)) && (view
->protect
& VPROT_WRITEWATCH
))
2669 reset_write_watches( view
, base
, size
);
2671 status
= STATUS_INVALID_PARAMETER
;
2673 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2678 /***********************************************************************
2679 * NtReadVirtualMemory (NTDLL.@)
2680 * ZwReadVirtualMemory (NTDLL.@)
2682 NTSTATUS WINAPI
NtReadVirtualMemory( HANDLE process
, const void *addr
, void *buffer
,
2683 SIZE_T size
, SIZE_T
*bytes_read
)
2687 if (virtual_check_buffer_for_write( buffer
, size
))
2689 SERVER_START_REQ( read_process_memory
)
2691 req
->handle
= wine_server_obj_handle( process
);
2692 req
->addr
= wine_server_client_ptr( addr
);
2693 wine_server_set_reply( req
, buffer
, size
);
2694 if ((status
= wine_server_call( req
))) size
= 0;
2700 status
= STATUS_ACCESS_VIOLATION
;
2703 if (bytes_read
) *bytes_read
= size
;
2708 /***********************************************************************
2709 * NtWriteVirtualMemory (NTDLL.@)
2710 * ZwWriteVirtualMemory (NTDLL.@)
2712 NTSTATUS WINAPI
NtWriteVirtualMemory( HANDLE process
, void *addr
, const void *buffer
,
2713 SIZE_T size
, SIZE_T
*bytes_written
)
2717 if (virtual_check_buffer_for_read( buffer
, size
))
2719 SERVER_START_REQ( write_process_memory
)
2721 req
->handle
= wine_server_obj_handle( process
);
2722 req
->addr
= wine_server_client_ptr( addr
);
2723 wine_server_add_data( req
, buffer
, size
);
2724 if ((status
= wine_server_call( req
))) size
= 0;
2730 status
= STATUS_PARTIAL_COPY
;
2733 if (bytes_written
) *bytes_written
= size
;
2738 /***********************************************************************
2739 * NtAreMappedFilesTheSame (NTDLL.@)
2740 * ZwAreMappedFilesTheSame (NTDLL.@)
2742 NTSTATUS WINAPI
NtAreMappedFilesTheSame(PVOID addr1
, PVOID addr2
)
2744 TRACE("%p %p\n", addr1
, addr2
);
2746 return STATUS_NOT_SAME_DEVICE
;