1 /*
2 * Win32 virtual memory functions
3 *
4 * Copyright 1997, 2002 Alexandre Julliard
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <errno.h>
26 #ifdef HAVE_SYS_ERRNO_H
27 #include <sys/errno.h>
28 #endif
29 #include <fcntl.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
33 #include <stdarg.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <sys/types.h>
38 #ifdef HAVE_SYS_STAT_H
39 # include <sys/stat.h>
40 #endif
41 #ifdef HAVE_SYS_MMAN_H
42 # include <sys/mman.h>
43 #endif
44 #ifdef HAVE_VALGRIND_VALGRIND_H
45 # include <valgrind/valgrind.h>
46 #endif
47
48 #define NONAMELESSUNION
49 #define NONAMELESSSTRUCT
50 #include "ntstatus.h"
51 #define WIN32_NO_STATUS
52 #include "windef.h"
53 #include "winternl.h"
54 #include "wine/library.h"
55 #include "wine/server.h"
56 #include "wine/list.h"
57 #include "wine/debug.h"
58 #include "ntdll_misc.h"
59
60 WINE_DEFAULT_DEBUG_CHANNEL(virtual);
61 WINE_DECLARE_DEBUG_CHANNEL(module);
62
63 #ifndef MS_SYNC
64 #define MS_SYNC 0
65 #endif
66
67 #ifndef MAP_NORESERVE
68 #define MAP_NORESERVE 0
69 #endif
70
71 /* File view */
72 typedef struct file_view
73 {
74 struct list entry; /* Entry in global view list */
75 void *base; /* Base address */
76 size_t size; /* Size in bytes */
77 HANDLE mapping; /* Handle to the file mapping */
78 unsigned int protect; /* Protection for all pages at allocation time */
79 BYTE prot[1]; /* Protection byte for each page */
80 } FILE_VIEW;
81
82
83 /* Conversion from VPROT_* to Win32 flags */
84 static const BYTE VIRTUAL_Win32Flags[16] =
85 {
86 PAGE_NOACCESS, /* 0 */
87 PAGE_READONLY, /* READ */
88 PAGE_READWRITE, /* WRITE */
89 PAGE_READWRITE, /* READ | WRITE */
90 PAGE_EXECUTE, /* EXEC */
91 PAGE_EXECUTE_READ, /* READ | EXEC */
92 PAGE_EXECUTE_READWRITE, /* WRITE | EXEC */
93 PAGE_EXECUTE_READWRITE, /* READ | WRITE | EXEC */
94 PAGE_WRITECOPY, /* WRITECOPY */
95 PAGE_WRITECOPY, /* READ | WRITECOPY */
96 PAGE_WRITECOPY, /* WRITE | WRITECOPY */
97 PAGE_WRITECOPY, /* READ | WRITE | WRITECOPY */
98 PAGE_EXECUTE_WRITECOPY, /* EXEC | WRITECOPY */
99 PAGE_EXECUTE_WRITECOPY, /* READ | EXEC | WRITECOPY */
100 PAGE_EXECUTE_WRITECOPY, /* WRITE | EXEC | WRITECOPY */
101 PAGE_EXECUTE_WRITECOPY /* READ | WRITE | EXEC | WRITECOPY */
102 };
103
104 static struct list views_list = LIST_INIT(views_list);
105
106 static RTL_CRITICAL_SECTION csVirtual;
107 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
108 {
109 0, 0, &csVirtual,
110 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
111 0, 0, { (DWORD_PTR)(__FILE__ ": csVirtual") }
112 };
113 static RTL_CRITICAL_SECTION csVirtual = { &critsect_debug, -1, 0, 0, 0, 0 };
114
115 #ifdef __i386__
116 /* These are always the same on an i386, and it will be faster this way */
117 # define page_mask 0xfff
118 # define page_shift 12
119 # define page_size 0x1000
120 /* Note: these are Windows limits, you cannot change them. */
121 static void *address_space_limit = (void *)0xc0000000; /* top of the total available address space */
122 static void *user_space_limit = (void *)0x7fff0000; /* top of the user address space */
123 static void *working_set_limit = (void *)0x7fff0000; /* top of the current working set */
124 #else
125 static UINT page_shift;
126 static UINT page_size;
127 static UINT_PTR page_mask;
128 static void *address_space_limit;
129 static void *user_space_limit;
130 static void *working_set_limit;
131 #endif /* __i386__ */
132
133 #define ROUND_ADDR(addr,mask) \
134 ((void *)((UINT_PTR)(addr) & ~(UINT_PTR)(mask)))
135
136 #define ROUND_SIZE(addr,size) \
137 (((SIZE_T)(size) + ((UINT_PTR)(addr) & page_mask) + page_mask) & ~page_mask)
138
139 #define VIRTUAL_DEBUG_DUMP_VIEW(view) \
140 do { if (TRACE_ON(virtual)) VIRTUAL_DumpView(view); } while (0)
141
142 #define VIRTUAL_HEAP_SIZE (4*1024*1024)
143
144 static HANDLE virtual_heap;
145 static void *preload_reserve_start;
146 static void *preload_reserve_end;
147 static int use_locks;
148 static int force_exec_prot; /* whether to force PROT_EXEC on all PROT_READ mmaps */
149
150
151 /***********************************************************************
152 * VIRTUAL_GetProtStr
153 */
154 static const char *VIRTUAL_GetProtStr( BYTE prot )
155 {
156 static char buffer[6];
157 buffer[0] = (prot & VPROT_COMMITTED) ? 'c' : '-';
158 buffer[1] = (prot & VPROT_GUARD) ? 'g' : '-';
159 buffer[2] = (prot & VPROT_READ) ? 'r' : '-';
160 buffer[3] = (prot & VPROT_WRITECOPY) ? 'W' : ((prot & VPROT_WRITE) ? 'w' : '-');
161 buffer[4] = (prot & VPROT_EXEC) ? 'x' : '-';
162 buffer[5] = 0;
163 return buffer;
164 }
165
166
167 /***********************************************************************
168 * VIRTUAL_GetUnixProt
169 *
170 * Convert page protections to protection for mmap/mprotect.
171 */
172 static int VIRTUAL_GetUnixProt( BYTE vprot )
173 {
174 int prot = 0;
175 if ((vprot & VPROT_COMMITTED) && !(vprot & VPROT_GUARD))
176 {
177 if (vprot & VPROT_READ) prot |= PROT_READ;
178 if (vprot & VPROT_WRITE) prot |= PROT_WRITE;
179 if (vprot & VPROT_WRITECOPY) prot |= PROT_WRITE;
180 if (vprot & VPROT_EXEC) prot |= PROT_EXEC;
181 if (vprot & VPROT_WRITEWATCH) prot &= ~PROT_WRITE;
182 }
183 if (!prot) prot = PROT_NONE;
184 return prot;
185 }
186
187
188 /***********************************************************************
189 * VIRTUAL_DumpView
190 */
191 static void VIRTUAL_DumpView( FILE_VIEW *view )
192 {
193 UINT i, count;
194 char *addr = view->base;
195 BYTE prot = view->prot[0];
196
197 TRACE( "View: %p - %p", addr, addr + view->size - 1 );
198 if (view->protect & VPROT_SYSTEM)
199 TRACE( " (system)\n" );
200 else if (view->protect & VPROT_VALLOC)
201 TRACE( " (valloc)\n" );
202 else if (view->mapping)
203 TRACE( " %p\n", view->mapping );
204 else
205 TRACE( " (anonymous)\n");
206
207 for (count = i = 1; i < view->size >> page_shift; i++, count++)
208 {
209 if (view->prot[i] == prot) continue;
210 TRACE( " %p - %p %s\n",
211 addr, addr + (count << page_shift) - 1, VIRTUAL_GetProtStr(prot) );
212 addr += (count << page_shift);
213 prot = view->prot[i];
214 count = 0;
215 }
216 if (count)
217 TRACE( " %p - %p %s\n",
218 addr, addr + (count << page_shift) - 1, VIRTUAL_GetProtStr(prot) );
219 }
220
221
222 /***********************************************************************
223 * VIRTUAL_Dump
224 */
225 #if WINE_VM_DEBUG
226 static void VIRTUAL_Dump(void)
227 {
228 sigset_t sigset;
229 struct file_view *view;
230
231 TRACE( "Dump of all virtual memory views:\n" );
232 server_enter_uninterrupted_section( &csVirtual, &sigset );
233 LIST_FOR_EACH_ENTRY( view, &views_list, FILE_VIEW, entry )
234 {
235 VIRTUAL_DumpView( view );
236 }
237 server_leave_uninterrupted_section( &csVirtual, &sigset );
238 }
239 #endif
240
241
242 /***********************************************************************
243 * VIRTUAL_FindView
244 *
245 * Find the view containing a given address. The csVirtual section must be held by caller.
246 *
247 * PARAMS
248 * addr [I] Address
249 *
250 * RETURNS
251 * View: Success
252 * NULL: Failure
253 */
254 static struct file_view *VIRTUAL_FindView( const void *addr, size_t size )
255 {
256 struct file_view *view;
257
258 LIST_FOR_EACH_ENTRY( view, &views_list, struct file_view, entry )
259 {
260 if (view->base > addr) break; /* no matching view */
261 if ((const char *)view->base + view->size <= (const char *)addr) continue;
262 if ((const char *)view->base + view->size < (const char *)addr + size) break; /* size too large */
263 if ((const char *)addr + size < (const char *)addr) break; /* overflow */
264 return view;
265 }
266 return NULL;
267 }
268
269
270 /***********************************************************************
271 * get_mask
272 */
273 static inline UINT_PTR get_mask( ULONG zero_bits )
274 {
275 if (!zero_bits) return 0xffff; /* allocations are aligned to 64K by default */
276 if (zero_bits < page_shift) zero_bits = page_shift;
277 return (1 << zero_bits) - 1;
278 }
279
280
281 /***********************************************************************
282 * find_view_range
283 *
284 * Find the first view overlapping at least part of the specified range.
285 * The csVirtual section must be held by caller.
286 */
287 static struct file_view *find_view_range( const void *addr, size_t size )
288 {
289 struct file_view *view;
290
291 LIST_FOR_EACH_ENTRY( view, &views_list, struct file_view, entry )
292 {
293 if ((const char *)view->base >= (const char *)addr + size) break;
294 if ((const char *)view->base + view->size > (const char *)addr) return view;
295 }
296 return NULL;
297 }
298
299
300 /***********************************************************************
301 * find_free_area
302 *
303 * Find a free area between views inside the specified range.
304 * The csVirtual section must be held by caller.
305 */
306 static void *find_free_area( void *base, void *end, size_t size, size_t mask, int top_down )
307 {
308 struct list *ptr;
309 void *start;
310
311 if (top_down)
312 {
313 start = ROUND_ADDR( (char *)end - size, mask );
314 if (start >= end || start < base) return NULL;
315
316 for (ptr = views_list.prev; ptr != &views_list; ptr = ptr->prev)
317 {
318 struct file_view *view = LIST_ENTRY( ptr, struct file_view, entry );
319
320 if ((char *)view->base + view->size <= (char *)start) break;
321 if ((char *)view->base >= (char *)start + size) continue;
322 start = ROUND_ADDR( (char *)view->base - size, mask );
323 /* stop if remaining space is not large enough */
324 if (!start || start >= end || start < base) return NULL;
325 }
326 }
327 else
328 {
329 start = ROUND_ADDR( (char *)base + mask, mask );
330 if (start >= end || (char *)end - (char *)start < size) return NULL;
331
332 for (ptr = views_list.next; ptr != &views_list; ptr = ptr->next)
333 {
334 struct file_view *view = LIST_ENTRY( ptr, struct file_view, entry );
335
336 if ((char *)view->base >= (char *)start + size) break;
337 if ((char *)view->base + view->size <= (char *)start) continue;
338 start = ROUND_ADDR( (char *)view->base + view->size + mask, mask );
339 /* stop if remaining space is not large enough */
340 if (!start || start >= end || (char *)end - (char *)start < size) return NULL;
341 }
342 }
343 return start;
344 }
345
346
347 /***********************************************************************
348 * add_reserved_area
349 *
350 * Add a reserved area to the list maintained by libwine.
351 * The csVirtual section must be held by caller.
352 */
353 static void add_reserved_area( void *addr, size_t size )
354 {
355 TRACE( "adding %p-%p\n", addr, (char *)addr + size );
356
357 if (addr < user_space_limit)
358 {
359 /* unmap the part of the area that is below the limit */
360 assert( (char *)addr + size > (char *)user_space_limit );
361 munmap( addr, (char *)user_space_limit - (char *)addr );
362 size -= (char *)user_space_limit - (char *)addr;
363 addr = user_space_limit;
364 }
365 /* blow away existing mappings */
366 wine_anon_mmap( addr, size, PROT_NONE, MAP_NORESERVE | MAP_FIXED );
367 wine_mmap_add_reserved_area( addr, size );
368 }
369
370
371 /***********************************************************************
372 * is_beyond_limit
373 *
374 * Check if an address range goes beyond a given limit.
375 */
376 static inline int is_beyond_limit( const void *addr, size_t size, const void *limit )
377 {
378 return (addr >= limit || (const char *)addr + size > (const char *)limit);
379 }
380
381
382 /***********************************************************************
383 * unmap_area
384 *
385 * Unmap an area, or simply replace it by an empty mapping if it is
386 * in a reserved area. The csVirtual section must be held by caller.
387 */
388 static inline void unmap_area( void *addr, size_t size )
389 {
390 if (wine_mmap_is_in_reserved_area( addr, size ))
391 wine_anon_mmap( addr, size, PROT_NONE, MAP_NORESERVE | MAP_FIXED );
392 else if (is_beyond_limit( addr, size, user_space_limit ))
393 add_reserved_area( addr, size );
394 else
395 munmap( addr, size );
396 }
397
398
399 /***********************************************************************
400 * delete_view
401 *
402 * Deletes a view. The csVirtual section must be held by caller.
403 */
404 static void delete_view( struct file_view *view ) /* [in] View */
405 {
406 if (!(view->protect & VPROT_SYSTEM)) unmap_area( view->base, view->size );
407 list_remove( &view->entry );
408 if (view->mapping) NtClose( view->mapping );
409 RtlFreeHeap( virtual_heap, 0, view );
410 }
411
412
413 /***********************************************************************
414 * create_view
415 *
416 * Create a view. The csVirtual section must be held by caller.
417 */
418 static NTSTATUS create_view( struct file_view **view_ret, void *base, size_t size, unsigned int vprot )
419 {
420 struct file_view *view;
421 struct list *ptr;
422 int unix_prot = VIRTUAL_GetUnixProt( vprot );
423
424 assert( !((UINT_PTR)base & page_mask) );
425 assert( !(size & page_mask) );
426
427 /* Create the view structure */
428
429 if (!(view = RtlAllocateHeap( virtual_heap, 0, sizeof(*view) + (size >> page_shift) - 1 )))
430 {
431 FIXME( "out of memory in virtual heap for %p-%p\n", base, (char *)base + size );
432 return STATUS_NO_MEMORY;
433 }
434
435 view->base = base;
436 view->size = size;
437 view->mapping = 0;
438 view->protect = vprot;
439 memset( view->prot, vprot, size >> page_shift );
440
441 /* Insert it in the linked list */
442
443 LIST_FOR_EACH( ptr, &views_list )
444 {
445 struct file_view *next = LIST_ENTRY( ptr, struct file_view, entry );
446 if (next->base > base) break;
447 }
448 list_add_before( ptr, &view->entry );
449
450 /* Check for overlapping views. This can happen if the previous view
451 * was a system view that got unmapped behind our back. In that case
452 * we recover by simply deleting it. */
453
454 if ((ptr = list_prev( &views_list, &view->entry )) != NULL)
455 {
456 struct file_view *prev = LIST_ENTRY( ptr, struct file_view, entry );
457 if ((char *)prev->base + prev->size > (char *)base)
458 {
459 TRACE( "overlapping prev view %p-%p for %p-%p\n",
460 prev->base, (char *)prev->base + prev->size,
461 base, (char *)base + view->size );
462 assert( prev->protect & VPROT_SYSTEM );
463 delete_view( prev );
464 }
465 }
466 if ((ptr = list_next( &views_list, &view->entry )) != NULL)
467 {
468 struct file_view *next = LIST_ENTRY( ptr, struct file_view, entry );
469 if ((char *)base + view->size > (char *)next->base)
470 {
471 TRACE( "overlapping next view %p-%p for %p-%p\n",
472 next->base, (char *)next->base + next->size,
473 base, (char *)base + view->size );
474 assert( next->protect & VPROT_SYSTEM );
475 delete_view( next );
476 }
477 }
478
479 *view_ret = view;
480 VIRTUAL_DEBUG_DUMP_VIEW( view );
481
482 if (force_exec_prot && !(vprot & VPROT_NOEXEC) && (unix_prot & PROT_READ) && !(unix_prot & PROT_EXEC))
483 {
484 TRACE( "forcing exec permission on %p-%p\n", base, (char *)base + size - 1 );
485 mprotect( base, size, unix_prot | PROT_EXEC );
486 }
487 return STATUS_SUCCESS;
488 }
489
490
491 /***********************************************************************
492 * VIRTUAL_GetWin32Prot
493 *
494 * Convert page protections to Win32 flags.
495 */
496 static DWORD VIRTUAL_GetWin32Prot( BYTE vprot )
497 {
498 DWORD ret = VIRTUAL_Win32Flags[vprot & 0x0f];
499 if (vprot & VPROT_NOCACHE) ret |= PAGE_NOCACHE;
500 if (vprot & VPROT_GUARD) ret |= PAGE_GUARD;
501 return ret;
502 }
503
504
505 /***********************************************************************
506 * get_vprot_flags
507 *
508 * Build page protections from Win32 flags.
509 *
510 * PARAMS
511 * protect [I] Win32 protection flags
512 *
513 * RETURNS
514 * Value of page protection flags
515 */
516 static NTSTATUS get_vprot_flags( DWORD protect, unsigned int *vprot )
517 {
518 switch(protect & 0xff)
519 {
520 case PAGE_READONLY:
521 *vprot = VPROT_READ;
522 break;
523 case PAGE_READWRITE:
524 *vprot = VPROT_READ | VPROT_WRITE;
525 break;
526 case PAGE_WRITECOPY:
527 *vprot = VPROT_READ | VPROT_WRITECOPY;
528 break;
529 case PAGE_EXECUTE:
530 *vprot = VPROT_EXEC;
531 break;
532 case PAGE_EXECUTE_READ:
533 *vprot = VPROT_EXEC | VPROT_READ;
534 break;
535 case PAGE_EXECUTE_READWRITE:
536 *vprot = VPROT_EXEC | VPROT_READ | VPROT_WRITE;
537 break;
538 case PAGE_EXECUTE_WRITECOPY:
539 *vprot = VPROT_EXEC | VPROT_READ | VPROT_WRITECOPY;
540 break;
541 case PAGE_NOACCESS:
542 *vprot = 0;
543 break;
544 default:
545 return STATUS_INVALID_PARAMETER;
546 }
547 if (protect & PAGE_GUARD) *vprot |= VPROT_GUARD;
548 if (protect & PAGE_NOCACHE) *vprot |= VPROT_NOCACHE;
549 return STATUS_SUCCESS;
550 }
551
552
553 /***********************************************************************
554 * VIRTUAL_SetProt
555 *
556 * Change the protection of a range of pages.
557 *
558 * RETURNS
559 * TRUE: Success
560 * FALSE: Failure
561 */
562 static BOOL VIRTUAL_SetProt( FILE_VIEW *view, /* [in] Pointer to view */
563 void *base, /* [in] Starting address */
564 size_t size, /* [in] Size in bytes */
565 BYTE vprot ) /* [in] Protections to use */
566 {
567 int unix_prot = VIRTUAL_GetUnixProt(vprot);
568 BYTE *p = view->prot + (((char *)base - (char *)view->base) >> page_shift);
569
570 TRACE("%p-%p %s\n",
571 base, (char *)base + size - 1, VIRTUAL_GetProtStr( vprot ) );
572
573 if (view->protect & VPROT_WRITEWATCH)
574 {
575 /* each page may need different protections depending on write watch flag */
576 UINT i, count;
577 char *addr = base;
578 int prot;
579
580 p[0] = vprot | (p[0] & VPROT_WRITEWATCH);
581 unix_prot = VIRTUAL_GetUnixProt( p[0] );
582 for (count = i = 1; i < size >> page_shift; i++, count++)
583 {
584 p[i] = vprot | (p[i] & VPROT_WRITEWATCH);
585 prot = VIRTUAL_GetUnixProt( p[i] );
586 if (prot == unix_prot) continue;
587 mprotect( addr, count << page_shift, unix_prot );
588 addr += count << page_shift;
589 unix_prot = prot;
590 count = 0;
591 }
592 if (count) mprotect( addr, count << page_shift, unix_prot );
593 VIRTUAL_DEBUG_DUMP_VIEW( view );
594 return TRUE;
595 }
596
597 /* if setting stack guard pages, store the permissions first, as the guard may be
598 * triggered at any point after mprotect and change the permissions again */
599 if ((vprot & VPROT_GUARD) &&
600 ((char *)base >= (char *)NtCurrentTeb()->DeallocationStack) &&
601 ((char *)base < (char *)NtCurrentTeb()->Tib.StackBase))
602 {
603 memset( p, vprot, size >> page_shift );
604 mprotect( base, size, unix_prot );
605 VIRTUAL_DEBUG_DUMP_VIEW( view );
606 return TRUE;
607 }
608
609 if (force_exec_prot && !(view->protect & VPROT_NOEXEC) &&
610 (unix_prot & PROT_READ) && !(unix_prot & PROT_EXEC))
611 {
612 TRACE( "forcing exec permission on %p-%p\n", base, (char *)base + size - 1 );
613 if (!mprotect( base, size, unix_prot | PROT_EXEC )) goto done;
614 /* exec + write may legitimately fail, in that case fall back to write only */
615 if (!(unix_prot & PROT_WRITE)) return FALSE;
616 }
617
618 if (mprotect( base, size, unix_prot )) return FALSE; /* FIXME: last error */
619
620 done:
621 memset( p, vprot, size >> page_shift );
622 VIRTUAL_DEBUG_DUMP_VIEW( view );
623 return TRUE;
624 }
625
626
627 /***********************************************************************
628 * reset_write_watches
629 *
630 * Reset write watches in a memory range.
631 */
632 static void reset_write_watches( struct file_view *view, void *base, SIZE_T size )
633 {
634 SIZE_T i, count;
635 int prot, unix_prot;
636 char *addr = base;
637 BYTE *p = view->prot + ((addr - (char *)view->base) >> page_shift);
638
639 p[0] |= VPROT_WRITEWATCH;
640 unix_prot = VIRTUAL_GetUnixProt( p[0] );
641 for (count = i = 1; i < size >> page_shift; i++, count++)
642 {
643 p[i] |= VPROT_WRITEWATCH;
644 prot = VIRTUAL_GetUnixProt( p[i] );
645 if (prot == unix_prot) continue;
646 mprotect( addr, count << page_shift, unix_prot );
647 addr += count << page_shift;
648 unix_prot = prot;
649 count = 0;
650 }
651 if (count) mprotect( addr, count << page_shift, unix_prot );
652 }
653
654
655 /***********************************************************************
656 * unmap_extra_space
657 *
658 * Release the extra memory while keeping the range starting on the granularity boundary.
659 */
660 static inline void *unmap_extra_space( void *ptr, size_t total_size, size_t wanted_size, size_t mask )
661 {
662 if ((ULONG_PTR)ptr & mask)
663 {
664 size_t extra = mask + 1 - ((ULONG_PTR)ptr & mask);
665 munmap( ptr, extra );
666 ptr = (char *)ptr + extra;
667 total_size -= extra;
668 }
669 if (total_size > wanted_size)
670 munmap( (char *)ptr + wanted_size, total_size - wanted_size );
671 return ptr;
672 }
673
674
675 struct alloc_area
676 {
677 size_t size;
678 size_t mask;
679 int top_down;
680 void *limit;
681 void *result;
682 };
683
684 /***********************************************************************
685 * alloc_reserved_area_callback
686 *
687 * Try to map some space inside a reserved area. Callback for wine_mmap_enum_reserved_areas.
688 */
689 static int alloc_reserved_area_callback( void *start, size_t size, void *arg )
690 {
691 static void * const address_space_start = (void *)0x110000;
692 struct alloc_area *alloc = arg;
693 void *end = (char *)start + size;
694
695 if (start < address_space_start) start = address_space_start;
696 if (is_beyond_limit( start, size, alloc->limit )) end = alloc->limit;
697 if (start >= end) return 0;
698
699 /* make sure we don't touch the preloader reserved range */
700 if (preload_reserve_end >= start)
701 {
702 if (preload_reserve_end >= end)
703 {
704 if (preload_reserve_start <= start) return 0; /* no space in that area */
705 if (preload_reserve_start < end) end = preload_reserve_start;
706 }
707 else if (preload_reserve_start <= start) start = preload_reserve_end;
708 else
709 {
710 /* range is split in two by the preloader reservation, try first part */
711 if ((alloc->result = find_free_area( start, preload_reserve_start, alloc->size,
712 alloc->mask, alloc->top_down )))
713 return 1;
714 /* then fall through to try second part */
715 start = preload_reserve_end;
716 }
717 }
718 if ((alloc->result = find_free_area( start, end, alloc->size, alloc->mask, alloc->top_down )))
719 return 1;
720
721 return 0;
722 }
723
724
725 /***********************************************************************
726 * map_view
727 *
728 * Create a view and mmap the corresponding memory area.
729 * The csVirtual section must be held by caller.
730 */
731 static NTSTATUS map_view( struct file_view **view_ret, void *base, size_t size, size_t mask,
732 int top_down, unsigned int vprot )
733 {
734 void *ptr;
735 NTSTATUS status;
736
737 if (base)
738 {
739 if (is_beyond_limit( base, size, address_space_limit ))
740 return STATUS_WORKING_SET_LIMIT_RANGE;
741
742 switch (wine_mmap_is_in_reserved_area( base, size ))
743 {
744 case -1: /* partially in a reserved area */
745 return STATUS_CONFLICTING_ADDRESSES;
746
747 case 0: /* not in a reserved area, do a normal allocation */
748 if ((ptr = wine_anon_mmap( base, size, VIRTUAL_GetUnixProt(vprot), 0 )) == (void *)-1)
749 {
750 if (errno == ENOMEM) return STATUS_NO_MEMORY;
751 return STATUS_INVALID_PARAMETER;
752 }
753 if (ptr != base)
754 {
755 /* We couldn't get the address we wanted */
756 if (is_beyond_limit( ptr, size, user_space_limit )) add_reserved_area( ptr, size );
757 else munmap( ptr, size );
758 return STATUS_CONFLICTING_ADDRESSES;
759 }
760 break;
761
762 default:
763 case 1: /* in a reserved area, make sure the address is available */
764 if (find_view_range( base, size )) return STATUS_CONFLICTING_ADDRESSES;
765 /* replace the reserved area by our mapping */
766 if ((ptr = wine_anon_mmap( base, size, VIRTUAL_GetUnixProt(vprot), MAP_FIXED )) != base)
767 return STATUS_INVALID_PARAMETER;
768 break;
769 }
770 if (is_beyond_limit( ptr, size, working_set_limit )) working_set_limit = address_space_limit;
771 }
772 else
773 {
774 size_t view_size = size + mask + 1;
775 struct alloc_area alloc;
776
777 alloc.size = size;
778 alloc.mask = mask;
779 alloc.top_down = top_down;
780 alloc.limit = user_space_limit;
781 if (wine_mmap_enum_reserved_areas( alloc_reserved_area_callback, &alloc, top_down ))
782 {
783 ptr = alloc.result;
784 TRACE( "got mem in reserved area %p-%p\n", ptr, (char *)ptr + size );
785 if (wine_anon_mmap( ptr, size, VIRTUAL_GetUnixProt(vprot), MAP_FIXED ) != ptr)
786 return STATUS_INVALID_PARAMETER;
787 goto done;
788 }
789
790 for (;;)
791 {
792 if ((ptr = wine_anon_mmap( NULL, view_size, VIRTUAL_GetUnixProt(vprot), 0 )) == (void *)-1)
793 {
794 if (errno == ENOMEM) return STATUS_NO_MEMORY;
795 return STATUS_INVALID_PARAMETER;
796 }
797 TRACE( "got mem with anon mmap %p-%p\n", ptr, (char *)ptr + size );
798 /* if we got something beyond the user limit, unmap it and retry */
799 if (is_beyond_limit( ptr, view_size, user_space_limit )) add_reserved_area( ptr, view_size );
800 else break;
801 }
802 ptr = unmap_extra_space( ptr, view_size, size, mask );
803 }
804 done:
805 status = create_view( view_ret, ptr, size, vprot );
806 if (status != STATUS_SUCCESS) unmap_area( ptr, size );
807 return status;
808 }
809
810
811 /***********************************************************************
812 * map_file_into_view
813 *
814 * Wrapper for mmap() to map a file into a view, falling back to read if mmap fails.
815 * The csVirtual section must be held by caller.
816 */
817 static NTSTATUS map_file_into_view( struct file_view *view, int fd, size_t start, size_t size,
818 off_t offset, unsigned int vprot, BOOL removable )
819 {
820 void *ptr;
821 int prot = VIRTUAL_GetUnixProt( vprot | VPROT_COMMITTED /* make sure it is accessible */ );
822 BOOL shared_write = (vprot & VPROT_WRITE) != 0;
823
824 assert( start < view->size );
825 assert( start + size <= view->size );
826
827 /* only try mmap if media is not removable (or if we require write access) */
828 if (!removable || shared_write)
829 {
830 int flags = MAP_FIXED | (shared_write ? MAP_SHARED : MAP_PRIVATE);
831
832 if (mmap( (char *)view->base + start, size, prot, flags, fd, offset ) != (void *)-1)
833 goto done;
834
835 /* mmap() failed; if this is because the file offset is not */
836 /* page-aligned (EINVAL), or because the underlying filesystem */
837 /* does not support mmap() (ENOEXEC,ENODEV), we do it by hand. */
838 if ((errno != ENOEXEC) && (errno != EINVAL) && (errno != ENODEV)) return FILE_GetNtStatus();
839 if (shared_write) /* we cannot fake shared write mappings */
840 {
841 if (errno == EINVAL) return STATUS_INVALID_PARAMETER;
842 ERR( "shared writable mmap not supported, broken filesystem?\n" );
843 return STATUS_NOT_SUPPORTED;
844 }
845 }
846
847 /* Reserve the memory with an anonymous mmap */
848 ptr = wine_anon_mmap( (char *)view->base + start, size, PROT_READ | PROT_WRITE, MAP_FIXED );
849 if (ptr == (void *)-1) return FILE_GetNtStatus();
850 /* Now read in the file */
851 pread( fd, ptr, size, offset );
852 if (prot != (PROT_READ|PROT_WRITE)) mprotect( ptr, size, prot ); /* Set the right protection */
853 done:
854 memset( view->prot + (start >> page_shift), vprot, ROUND_SIZE(start,size) >> page_shift );
855 return STATUS_SUCCESS;
856 }
857
858
859 /***********************************************************************
860 * get_committed_size
861 *
862 * Get the size of the committed range starting at base.
863 * Also return the protections for the first page.
864 */
865 static SIZE_T get_committed_size( struct file_view *view, void *base, BYTE *vprot )
866 {
867 SIZE_T i, start;
868
869 start = ((char *)base - (char *)view->base) >> page_shift;
870 *vprot = view->prot[start];
871
872 if (view->mapping && !(view->protect & VPROT_COMMITTED))
873 {
874 SIZE_T ret = 0;
875 SERVER_START_REQ( get_mapping_committed_range )
876 {
877 req->handle = wine_server_obj_handle( view->mapping );
878 req->offset = start << page_shift;
879 if (!wine_server_call( req ))
880 {
881 ret = reply->size;
882 if (reply->committed)
883 {
884 *vprot |= VPROT_COMMITTED;
885 for (i = 0; i < ret >> page_shift; i++) view->prot[start+i] |= VPROT_COMMITTED;
886 }
887 }
888 }
889 SERVER_END_REQ;
890 return ret;
891 }
892 for (i = start + 1; i < view->size >> page_shift; i++)
893 if ((*vprot ^ view->prot[i]) & VPROT_COMMITTED) break;
894 return (i - start) << page_shift;
895 }
896
897
898 /***********************************************************************
899 * decommit_view
900 *
901 * Decommit some pages of a given view.
902 * The csVirtual section must be held by caller.
903 */
904 static NTSTATUS decommit_pages( struct file_view *view, size_t start, size_t size )
905 {
906 if (wine_anon_mmap( (char *)view->base + start, size, PROT_NONE, MAP_FIXED ) != (void *)-1)
907 {
908 BYTE *p = view->prot + (start >> page_shift);
909 size >>= page_shift;
910 while (size--) *p++ &= ~VPROT_COMMITTED;
911 return STATUS_SUCCESS;
912 }
913 return FILE_GetNtStatus();
914 }
915
916
917 /***********************************************************************
918 * allocate_dos_memory
919 *
920 * Allocate the DOS memory range.
921 */
922 static NTSTATUS allocate_dos_memory( struct file_view **view, unsigned int vprot )
923 {
924 size_t size;
925 void *addr = NULL;
926 void * const low_64k = (void *)0x10000;
927 const size_t dosmem_size = 0x110000;
928 int unix_prot = VIRTUAL_GetUnixProt( vprot );
929 struct list *ptr;
930
931 /* check for existing view */
932
933 if ((ptr = list_head( &views_list )))
934 {
935 struct file_view *first_view = LIST_ENTRY( ptr, struct file_view, entry );
936 if (first_view->base < (void *)dosmem_size) return STATUS_CONFLICTING_ADDRESSES;
937 }
938
939 /* check without the first 64K */
940
941 if (wine_mmap_is_in_reserved_area( low_64k, dosmem_size - 0x10000 ) != 1)
942 {
943 addr = wine_anon_mmap( low_64k, dosmem_size - 0x10000, unix_prot, 0 );
944 if (addr != low_64k)
945 {
946 if (addr != (void *)-1) munmap( addr, dosmem_size - 0x10000 );
947 return map_view( view, NULL, dosmem_size, 0xffff, 0, vprot );
948 }
949 }
950
951 /* now try to allocate the low 64K too */
952
953 if (wine_mmap_is_in_reserved_area( NULL, 0x10000 ) != 1)
954 {
955 addr = wine_anon_mmap( (void *)page_size, 0x10000 - page_size, unix_prot, 0 );
956 if (addr == (void *)page_size)
957 {
958 addr = NULL;
959 TRACE( "successfully mapped low 64K range\n" );
960 }
961 else
962 {
963 if (addr != (void *)-1) munmap( addr, 0x10000 - page_size );
964 addr = low_64k;
965 TRACE( "failed to map low 64K range\n" );
966 }
967 }
968
969 /* now reserve the whole range */
970
971 size = (char *)dosmem_size - (char *)addr;
972 wine_anon_mmap( addr, size, unix_prot, MAP_FIXED );
973 return create_view( view, addr, size, vprot );
974 }
975
976
977 /***********************************************************************
978 * map_image
979 *
980 * Map an executable (PE format) image into memory.
981 */
982 static NTSTATUS map_image( HANDLE hmapping, int fd, char *base, SIZE_T total_size, SIZE_T mask,
983 SIZE_T header_size, int shared_fd, HANDLE dup_mapping, PVOID *addr_ptr )
984 {
985 IMAGE_DOS_HEADER *dos;
986 IMAGE_NT_HEADERS *nt;
987 IMAGE_SECTION_HEADER *sec;
988 IMAGE_DATA_DIRECTORY *imports;
989 NTSTATUS status = STATUS_CONFLICTING_ADDRESSES;
990 int i;
991 off_t pos;
992 sigset_t sigset;
993 struct stat st;
994 struct file_view *view = NULL;
995 char *ptr, *header_end;
996 INT_PTR delta = 0;
997
998 /* zero-map the whole range */
999
1000 server_enter_uninterrupted_section( &csVirtual, &sigset );
1001
1002 if (base >= (char *)0x110000) /* make sure the DOS area remains free */
1003 status = map_view( &view, base, total_size, mask, FALSE,
1004 VPROT_COMMITTED | VPROT_READ | VPROT_EXEC | VPROT_WRITECOPY | VPROT_IMAGE );
1005
1006 if (status == STATUS_CONFLICTING_ADDRESSES)
1007 status = map_view( &view, NULL, total_size, mask, FALSE,
1008 VPROT_COMMITTED | VPROT_READ | VPROT_EXEC | VPROT_WRITECOPY | VPROT_IMAGE );
1009
1010 if (status != STATUS_SUCCESS) goto error;
1011
1012 ptr = view->base;
1013 TRACE_(module)( "mapped PE file at %p-%p\n", ptr, ptr + total_size );
1014
1015 /* map the header */
1016
1017 if (fstat( fd, &st ) == -1)
1018 {
1019 status = FILE_GetNtStatus();
1020 goto error;
1021 }
1022 status = STATUS_INVALID_IMAGE_FORMAT; /* generic error */
1023 if (!st.st_size) goto error;
1024 header_size = min( header_size, st.st_size );
1025 if (map_file_into_view( view, fd, 0, header_size, 0, VPROT_COMMITTED | VPROT_READ | VPROT_WRITECOPY,
1026 !dup_mapping ) != STATUS_SUCCESS) goto error;
1027 dos = (IMAGE_DOS_HEADER *)ptr;
1028 nt = (IMAGE_NT_HEADERS *)(ptr + dos->e_lfanew);
1029 header_end = ptr + ROUND_SIZE( 0, header_size );
1030 memset( ptr + header_size, 0, header_end - (ptr + header_size) );
1031 if ((char *)(nt + 1) > header_end) goto error;
1032 sec = (IMAGE_SECTION_HEADER*)((char*)&nt->OptionalHeader+nt->FileHeader.SizeOfOptionalHeader);
1033 if ((char *)(sec + nt->FileHeader.NumberOfSections) > header_end) goto error;
1034
1035 imports = nt->OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_IMPORT;
1036 if (!imports->Size || !imports->VirtualAddress) imports = NULL;
1037
1038 /* check the architecture */
1039
1040 #ifdef __x86_64__
1041 if (nt->FileHeader.Machine != IMAGE_FILE_MACHINE_AMD64)
1042 #else
1043 if (nt->FileHeader.Machine != IMAGE_FILE_MACHINE_I386)
1044 #endif
1045 {
1046 MESSAGE("Trying to load PE image for unsupported architecture (");
1047 switch (nt->FileHeader.Machine)
1048 {
1049 case IMAGE_FILE_MACHINE_UNKNOWN: MESSAGE("Unknown"); break;
1050 case IMAGE_FILE_MACHINE_I860: MESSAGE("I860"); break;
1051 case IMAGE_FILE_MACHINE_I386: MESSAGE("I386"); break;
1052 case IMAGE_FILE_MACHINE_R3000: MESSAGE("R3000"); break;
1053 case IMAGE_FILE_MACHINE_R4000: MESSAGE("R4000"); break;
1054 case IMAGE_FILE_MACHINE_R10000: MESSAGE("R10000"); break;
1055 case IMAGE_FILE_MACHINE_ALPHA: MESSAGE("Alpha"); break;
1056 case IMAGE_FILE_MACHINE_POWERPC: MESSAGE("PowerPC"); break;
1057 case IMAGE_FILE_MACHINE_IA64: MESSAGE("IA-64"); break;
1058 case IMAGE_FILE_MACHINE_ALPHA64: MESSAGE("Alpha-64"); break;
1059 case IMAGE_FILE_MACHINE_AMD64: MESSAGE("AMD-64"); break;
1060 case IMAGE_FILE_MACHINE_ARM: MESSAGE("ARM"); break;
1061 default: MESSAGE("Unknown-%04x", nt->FileHeader.Machine); break;
1062 }
1063 MESSAGE(")\n");
1064 goto error;
1065 }
1066
1067 /* check for non page-aligned binary */
1068
1069 if (nt->OptionalHeader.SectionAlignment <= page_mask)
1070 {
1071 /* unaligned sections, this happens for native subsystem binaries */
1072 /* in that case Windows simply maps in the whole file */
1073
1074 if (map_file_into_view( view, fd, 0, total_size, 0, VPROT_COMMITTED | VPROT_READ,
1075 !dup_mapping ) != STATUS_SUCCESS) goto error;
1076
1077 /* check that all sections are loaded at the right offset */
1078 if (nt->OptionalHeader.FileAlignment != nt->OptionalHeader.SectionAlignment) goto error;
1079 for (i = 0; i < nt->FileHeader.NumberOfSections; i++)
1080 {
1081 if (sec[i].VirtualAddress != sec[i].PointerToRawData)
1082 goto error; /* Windows refuses to load in that case too */
1083 }
1084
1085 /* set the image protections */
1086 VIRTUAL_SetProt( view, ptr, total_size,
1087 VPROT_COMMITTED | VPROT_READ | VPROT_WRITECOPY | VPROT_EXEC );
1088
1089 /* no relocations are performed on non page-aligned binaries */
1090 goto done;
1091 }
1092
1093
1094 /* map all the sections */
1095
1096 for (i = pos = 0; i < nt->FileHeader.NumberOfSections; i++, sec++)
1097 {
1098 static const SIZE_T sector_align = 0x1ff;
1099 SIZE_T map_size, file_start, file_size, end;
1100
1101 if (!sec->Misc.VirtualSize)
1102 map_size = ROUND_SIZE( 0, sec->SizeOfRawData );
1103 else
1104 map_size = ROUND_SIZE( 0, sec->Misc.VirtualSize );
1105
1106 /* file positions are rounded to sector boundaries regardless of OptionalHeader.FileAlignment */
1107 file_start = sec->PointerToRawData & ~sector_align;
1108 file_size = (sec->SizeOfRawData + (sec->PointerToRawData & sector_align) + sector_align) & ~sector_align;
1109 if (file_size > map_size) file_size = map_size;
1110
1111 /* a few sanity checks */
1112 end = sec->VirtualAddress + ROUND_SIZE( sec->VirtualAddress, map_size );
1113 if (sec->VirtualAddress > total_size || end > total_size || end < sec->VirtualAddress)
1114 {
1115 WARN_(module)( "Section %.8s too large (%x+%lx/%lx)\n",
1116 sec->Name, sec->VirtualAddress, map_size, total_size );
1117 goto error;
1118 }
1119
1120 if ((sec->Characteristics & IMAGE_SCN_MEM_SHARED) &&
1121 (sec->Characteristics & IMAGE_SCN_MEM_WRITE))
1122 {
1123 TRACE_(module)( "mapping shared section %.8s at %p off %x (%x) size %lx (%lx) flags %x\n",
1124 sec->Name, ptr + sec->VirtualAddress,
1125 sec->PointerToRawData, (int)pos, file_size, map_size,
1126 sec->Characteristics );
1127 if (map_file_into_view( view, shared_fd, sec->VirtualAddress, map_size, pos,
1128 VPROT_COMMITTED | VPROT_READ | VPROT_WRITE,
1129 FALSE ) != STATUS_SUCCESS)
1130 {
1131 ERR_(module)( "Could not map shared section %.8s\n", sec->Name );
1132 goto error;
1133 }
1134
1135 /* check if the import directory falls inside this section */
1136 if (imports && imports->VirtualAddress >= sec->VirtualAddress &&
1137 imports->VirtualAddress < sec->VirtualAddress + map_size)
1138 {
1139 UINT_PTR base = imports->VirtualAddress & ~page_mask;
1140 UINT_PTR end = base + ROUND_SIZE( imports->VirtualAddress, imports->Size );
1141 if (end > sec->VirtualAddress + map_size) end = sec->VirtualAddress + map_size;
1142 if (end > base)
1143 map_file_into_view( view, shared_fd, base, end - base,
1144 pos + (base - sec->VirtualAddress),
1145 VPROT_COMMITTED | VPROT_READ | VPROT_WRITECOPY,
1146 FALSE );
1147 }
1148 pos += map_size;
1149 continue;
1150 }
1151
1152 TRACE_(module)( "mapping section %.8s at %p off %x size %x virt %x flags %x\n",
1153 sec->Name, ptr + sec->VirtualAddress,
1154 sec->PointerToRawData, sec->SizeOfRawData,
1155 sec->Misc.VirtualSize, sec->Characteristics );
1156
1157 if (!sec->PointerToRawData || !file_size) continue;
1158
1159 /* Note: if the section is not aligned properly map_file_into_view will magically
1160 * fall back to read(), so we don't need to check anything here.
1161 */
1162 end = file_start + file_size;
1163 if (sec->PointerToRawData >= st.st_size ||
1164 end > ((st.st_size + sector_align) & ~sector_align) ||
1165 end < file_start ||
1166 map_file_into_view( view, fd, sec->VirtualAddress, file_size, file_start,
1167 VPROT_COMMITTED | VPROT_READ | VPROT_WRITECOPY,
1168 !dup_mapping ) != STATUS_SUCCESS)
1169 {
1170 ERR_(module)( "Could not map section %.8s, file probably truncated\n", sec->Name );
1171 goto error;
1172 }
1173
1174 if (file_size & page_mask)
1175 {
1176 end = ROUND_SIZE( 0, file_size );
1177 if (end > map_size) end = map_size;
1178 TRACE_(module)("clearing %p - %p\n",
1179 ptr + sec->VirtualAddress + file_size,
1180 ptr + sec->VirtualAddress + end );
1181 memset( ptr + sec->VirtualAddress + file_size, 0, end - file_size );
1182 }
1183 }
1184
1185
1186 /* perform base relocation, if necessary */
1187
1188 if (ptr != base &&
1189 ((nt->FileHeader.Characteristics & IMAGE_FILE_DLL) ||
1190 !NtCurrentTeb()->Peb->ImageBaseAddress) )
1191 {
1192 IMAGE_BASE_RELOCATION *rel, *end;
1193 const IMAGE_DATA_DIRECTORY *relocs;
1194
1195 if (nt->FileHeader.Characteristics & IMAGE_FILE_RELOCS_STRIPPED)
1196 {
1197 WARN_(module)( "Need to relocate module from %p to %p, but there are no relocation records\n",
1198 base, ptr );
1199 status = STATUS_CONFLICTING_ADDRESSES;
1200 goto error;
1201 }
1202
1203 TRACE_(module)( "relocating from %p-%p to %p-%p\n",
1204 base, base + total_size, ptr, ptr + total_size );
1205
1206 relocs = &nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC];
1207 rel = (IMAGE_BASE_RELOCATION *)(ptr + relocs->VirtualAddress);
1208 end = (IMAGE_BASE_RELOCATION *)(ptr + relocs->VirtualAddress + relocs->Size);
1209 delta = ptr - base;
1210
1211 while (rel < end - 1 && rel->SizeOfBlock)
1212 {
1213 if (rel->VirtualAddress >= total_size)
1214 {
1215 WARN_(module)( "invalid address %p in relocation %p\n", ptr + rel->VirtualAddress, rel );
1216 status = STATUS_ACCESS_VIOLATION;
1217 goto error;
1218 }
1219 rel = LdrProcessRelocationBlock( ptr + rel->VirtualAddress,
1220 (rel->SizeOfBlock - sizeof(*rel)) / sizeof(USHORT),
1221 (USHORT *)(rel + 1), delta );
1222 if (!rel) goto error;
1223 }
1224 }
1225
1226 /* set the image protections */
1227
1228 VIRTUAL_SetProt( view, ptr, ROUND_SIZE( 0, header_size ), VPROT_COMMITTED | VPROT_READ );
1229
1230 sec = (IMAGE_SECTION_HEADER*)((char *)&nt->OptionalHeader+nt->FileHeader.SizeOfOptionalHeader);
1231 for (i = 0; i < nt->FileHeader.NumberOfSections; i++, sec++)
1232 {
1233 SIZE_T size;
1234 BYTE vprot = VPROT_COMMITTED;
1235
1236 if (sec->Misc.VirtualSize)
1237 size = ROUND_SIZE( sec->VirtualAddress, sec->Misc.VirtualSize );
1238 else
1239 size = ROUND_SIZE( sec->VirtualAddress, sec->SizeOfRawData );
1240
1241 if (sec->Characteristics & IMAGE_SCN_MEM_READ) vprot |= VPROT_READ;
1242 if (sec->Characteristics & IMAGE_SCN_MEM_WRITE) vprot |= VPROT_READ|VPROT_WRITECOPY;
1243 if (sec->Characteristics & IMAGE_SCN_MEM_EXECUTE) vprot |= VPROT_EXEC;
1244
1245 /* Dumb game crack lets the AOEP point into a data section. Adjust. */
1246 if ((nt->OptionalHeader.AddressOfEntryPoint >= sec->VirtualAddress) &&
1247 (nt->OptionalHeader.AddressOfEntryPoint < sec->VirtualAddress + size))
1248 vprot |= VPROT_EXEC;
1249
1250 VIRTUAL_SetProt( view, ptr + sec->VirtualAddress, size, vprot );
1251 }
1252
1253 done:
1254 view->mapping = dup_mapping;
1255 server_leave_uninterrupted_section( &csVirtual, &sigset );
1256
1257 *addr_ptr = ptr;
1258 #ifdef VALGRIND_LOAD_PDB_DEBUGINFO
1259 VALGRIND_LOAD_PDB_DEBUGINFO(fd, ptr, total_size, delta);
1260 #endif
1261 return STATUS_SUCCESS;
1262
1263 error:
1264 if (view) delete_view( view );
1265 server_leave_uninterrupted_section( &csVirtual, &sigset );
1266 if (dup_mapping) NtClose( dup_mapping );
1267 return status;
1268 }
1269
1270
1271 /* callback for wine_mmap_enum_reserved_areas to allocate space for the virtual heap */
1272 static int alloc_virtual_heap( void *base, size_t size, void *arg )
1273 {
1274 void **heap_base = arg;
1275
1276 if (is_beyond_limit( base, size, address_space_limit )) address_space_limit = (char *)base + size;
1277 if (size < VIRTUAL_HEAP_SIZE) return 0;
1278 *heap_base = wine_anon_mmap( (char *)base + size - VIRTUAL_HEAP_SIZE,
1279 VIRTUAL_HEAP_SIZE, PROT_READ|PROT_WRITE, MAP_FIXED );
1280 return (*heap_base != (void *)-1);
1281 }
1282
1283 /***********************************************************************
1284 * virtual_init
1285 */
1286 void virtual_init(void)
1287 {
1288 const char *preload;
1289 void *heap_base;
1290 struct file_view *heap_view;
1291
1292 #ifndef page_mask
1293 page_size = getpagesize();
1294 page_mask = page_size - 1;
1295 /* Make sure we have a power of 2 */
1296 assert( !(page_size & page_mask) );
1297 page_shift = 0;
1298 while ((1 << page_shift) != page_size) page_shift++;
1299 user_space_limit = working_set_limit = address_space_limit = (void *)~page_mask;
1300 #endif /* page_mask */
1301 if ((preload = getenv("WINEPRELOADRESERVE")))
1302 {
1303 unsigned long start, end;
1304 if (sscanf( preload, "%lx-%lx", &start, &end ) == 2)
1305 {
1306 preload_reserve_start = (void *)start;
1307 preload_reserve_end = (void *)end;
1308 }
1309 }
1310
1311 /* try to find space in a reserved area for the virtual heap */
1312 if (!wine_mmap_enum_reserved_areas( alloc_virtual_heap, &heap_base, 1 ))
1313 heap_base = wine_anon_mmap( NULL, VIRTUAL_HEAP_SIZE, PROT_READ|PROT_WRITE, 0 );
1314
1315 assert( heap_base != (void *)-1 );
1316 virtual_heap = RtlCreateHeap( HEAP_NO_SERIALIZE, heap_base, VIRTUAL_HEAP_SIZE,
1317 VIRTUAL_HEAP_SIZE, NULL, NULL );
1318 create_view( &heap_view, heap_base, VIRTUAL_HEAP_SIZE, VPROT_COMMITTED | VPROT_READ | VPROT_WRITE );
1319 }
1320
1321
1322 /***********************************************************************
1323 * virtual_init_threading
1324 */
1325 void virtual_init_threading(void)
1326 {
1327 use_locks = 1;
1328 }
1329
1330
1331 /***********************************************************************
1332 * virtual_get_system_info
1333 */
1334 void virtual_get_system_info( SYSTEM_BASIC_INFORMATION *info )
1335 {
1336 info->dwUnknown1 = 0;
1337 info->uKeMaximumIncrement = 0; /* FIXME */
1338 info->uPageSize = page_size;
1339 info->uMmLowestPhysicalPage = 1;
1340 info->uMmHighestPhysicalPage = 0x7fffffff / page_size;
1341 info->uMmNumberOfPhysicalPages = info->uMmHighestPhysicalPage - info->uMmLowestPhysicalPage;
1342 info->uAllocationGranularity = get_mask(0) + 1;
1343 info->pLowestUserAddress = (void *)0x10000;
1344 info->pMmHighestUserAddress = (char *)user_space_limit - 1;
1345 info->uKeActiveProcessors = NtCurrentTeb()->Peb->NumberOfProcessors;
1346 info->bKeNumberProcessors = info->uKeActiveProcessors;
1347 }
1348
1349
1350 /***********************************************************************
1351 * virtual_create_system_view
1352 */
1353 NTSTATUS virtual_create_system_view( void *base, SIZE_T size, DWORD vprot )
1354 {
1355 FILE_VIEW *view;
1356 NTSTATUS status;
1357 sigset_t sigset;
1358
1359 size = ROUND_SIZE( base, size );
1360 base = ROUND_ADDR( base, page_mask );
1361 server_enter_uninterrupted_section( &csVirtual, &sigset );
1362 status = create_view( &view, base, size, vprot );
1363 if (!status) TRACE( "created %p-%p\n", base, (char *)base + size );
1364 server_leave_uninterrupted_section( &csVirtual, &sigset );
1365 return status;
1366 }
1367
1368
1369 /***********************************************************************
1370 * virtual_free_system_view
1371 */
1372 SIZE_T virtual_free_system_view( PVOID *addr_ptr )
1373 {
1374 FILE_VIEW *view;
1375 sigset_t sigset;
1376 SIZE_T size = 0;
1377 char *base = ROUND_ADDR( *addr_ptr, page_mask );
1378
1379 server_enter_uninterrupted_section( &csVirtual, &sigset );
1380 if ((view = VIRTUAL_FindView( base, 0 )))
1381 {
1382 TRACE( "freeing %p-%p\n", view->base, (char *)view->base + view->size );
1383 /* return the values that the caller should use to unmap the area */
1384 *addr_ptr = view->base;
1385 /* make sure we don't munmap anything from a reserved area */
1386 if (!wine_mmap_is_in_reserved_area( view->base, view->size )) size = view->size;
1387 view->protect |= VPROT_SYSTEM;
1388 delete_view( view );
1389 }
1390 server_leave_uninterrupted_section( &csVirtual, &sigset );
1391 return size;
1392 }
1393
1394
1395 /***********************************************************************
1396 * virtual_alloc_thread_stack
1397 */
1398 NTSTATUS virtual_alloc_thread_stack( void *base, SIZE_T size )
1399 {
1400 FILE_VIEW *view;
1401 NTSTATUS status;
1402 sigset_t sigset;
1403
1404 server_enter_uninterrupted_section( &csVirtual, &sigset );
1405
1406 if (base) /* already allocated, create a system view */
1407 {
1408 size = ROUND_SIZE( base, size );
1409 base = ROUND_ADDR( base, page_mask );
1410 if ((status = create_view( &view, base, size,
1411 VPROT_READ | VPROT_WRITE | VPROT_COMMITTED | VPROT_VALLOC | VPROT_SYSTEM )) != STATUS_SUCCESS)
1412 goto done;
1413 }
1414 else
1415 {
1416 size = (size + 0xffff) & ~0xffff; /* round to 64K boundary */
1417 if ((status = map_view( &view, NULL, size, 0xffff, 0,
1418 VPROT_READ | VPROT_WRITE | VPROT_COMMITTED | VPROT_VALLOC )) != STATUS_SUCCESS)
1419 goto done;
1420 #ifdef VALGRIND_STACK_REGISTER
1421 /* no need to de-register the stack as it's the one of the main thread */
1422 VALGRIND_STACK_REGISTER( view->base, (char *)view->base + view->size );
1423 #endif
1424 }
1425
1426 /* setup no access guard page */
1427 VIRTUAL_SetProt( view, view->base, page_size, VPROT_COMMITTED );
1428 VIRTUAL_SetProt( view, (char *)view->base + page_size, page_size,
1429 VPROT_READ | VPROT_WRITE | VPROT_COMMITTED | VPROT_GUARD );
1430
1431 /* note: limit is lower than base since the stack grows down */
1432 NtCurrentTeb()->DeallocationStack = view->base;
1433 NtCurrentTeb()->Tib.StackBase = (char *)view->base + view->size;
1434 NtCurrentTeb()->Tib.StackLimit = (char *)view->base + 2 * page_size;
1435
1436 done:
1437 server_leave_uninterrupted_section( &csVirtual, &sigset );
1438 return status;
1439 }
1440
1441
1442 /***********************************************************************
1443 * virtual_clear_thread_stack
1444 *
1445 * Clear the stack contents before calling the main entry point, some broken apps need that.
1446 */
1447 void virtual_clear_thread_stack(void)
1448 {
1449 void *stack = NtCurrentTeb()->Tib.StackLimit;
1450 size_t size = (char *)NtCurrentTeb()->Tib.StackBase - (char *)NtCurrentTeb()->Tib.StackLimit;
1451
1452 wine_anon_mmap( stack, size, PROT_READ | PROT_WRITE, MAP_FIXED );
1453 if (force_exec_prot) mprotect( stack, size, PROT_READ | PROT_WRITE | PROT_EXEC );
1454 }
1455
1456
1457 /***********************************************************************
1458 * virtual_handle_fault
1459 */
1460 NTSTATUS virtual_handle_fault( LPCVOID addr, DWORD err )
1461 {
1462 FILE_VIEW *view;
1463 NTSTATUS ret = STATUS_ACCESS_VIOLATION;
1464 sigset_t sigset;
1465
1466 server_enter_uninterrupted_section( &csVirtual, &sigset );
1467 if ((view = VIRTUAL_FindView( addr, 0 )))
1468 {
1469 void *page = ROUND_ADDR( addr, page_mask );
1470 BYTE *vprot = &view->prot[((const char *)page - (const char *)view->base) >> page_shift];
1471 if (*vprot & VPROT_GUARD)
1472 {
1473 VIRTUAL_SetProt( view, page, page_size, *vprot & ~VPROT_GUARD );
1474 ret = STATUS_GUARD_PAGE_VIOLATION;
1475 }
1476 if ((err & EXCEPTION_WRITE_FAULT) && (*vprot & VPROT_WRITEWATCH))
1477 {
1478 *vprot &= ~VPROT_WRITEWATCH;
1479 VIRTUAL_SetProt( view, page, page_size, *vprot );
1480 ret = STATUS_SUCCESS;
1481 }
1482 }
1483 server_leave_uninterrupted_section( &csVirtual, &sigset );
1484 return ret;
1485 }
1486
1487
1488
1489 /***********************************************************************
1490 * virtual_handle_stack_fault
1491 *
1492 * Handle an access fault inside the current thread stack.
1493 * Called from inside a signal handler.
1494 */
1495 BOOL virtual_handle_stack_fault( void *addr )
1496 {
1497 FILE_VIEW *view;
1498 BOOL ret = FALSE;
1499
1500 RtlEnterCriticalSection( &csVirtual ); /* no need for signal masking inside signal handler */
1501 if ((view = VIRTUAL_FindView( addr, 0 )))
1502 {
1503 void *page = ROUND_ADDR( addr, page_mask );
1504 BYTE vprot = view->prot[((const char *)page - (const char *)view->base) >> page_shift];
1505 if (vprot & VPROT_GUARD)
1506 {
1507 VIRTUAL_SetProt( view, page, page_size, vprot & ~VPROT_GUARD );
1508 if ((char *)page + page_size == NtCurrentTeb()->Tib.StackLimit)
1509 NtCurrentTeb()->Tib.StackLimit = page;
1510 ret = TRUE;
1511 }
1512 }
1513 RtlLeaveCriticalSection( &csVirtual );
1514 return ret;
1515 }
1516
1517
1518 /***********************************************************************
1519 * VIRTUAL_SetForceExec
1520 *
1521 * Whether to force exec prot on all views.
1522 */
1523 void VIRTUAL_SetForceExec( BOOL enable )
1524 {
1525 struct file_view *view;
1526 sigset_t sigset;
1527
1528 server_enter_uninterrupted_section( &csVirtual, &sigset );
1529 if (!force_exec_prot != !enable) /* change all existing views */
1530 {
1531 force_exec_prot = enable;
1532
1533 LIST_FOR_EACH_ENTRY( view, &views_list, struct file_view, entry )
1534 {
1535 UINT i, count;
1536 char *addr = view->base;
1537 BYTE commit = view->mapping ? VPROT_COMMITTED : 0; /* file mappings are always accessible */
1538 int unix_prot = VIRTUAL_GetUnixProt( view->prot[0] | commit );
1539
1540 if (view->protect & VPROT_NOEXEC) continue;
1541 for (count = i = 1; i < view->size >> page_shift; i++, count++)
1542 {
1543 int prot = VIRTUAL_GetUnixProt( view->prot[i] | commit );
1544 if (prot == unix_prot) continue;
1545 if ((unix_prot & PROT_READ) && !(unix_prot & PROT_EXEC))
1546 {
1547 TRACE( "%s exec prot for %p-%p\n",
1548 force_exec_prot ? "enabling" : "disabling",
1549 addr, addr + (count << page_shift) - 1 );
1550 mprotect( addr, count << page_shift,
1551 unix_prot | (force_exec_prot ? PROT_EXEC : 0) );
1552 }
1553 addr += (count << page_shift);
1554 unix_prot = prot;
1555 count = 0;
1556 }
1557 if (count)
1558 {
1559 if ((unix_prot & PROT_READ) && !(unix_prot & PROT_EXEC))
1560 {
1561 TRACE( "%s exec prot for %p-%p\n",
1562 force_exec_prot ? "enabling" : "disabling",
1563 addr, addr + (count << page_shift) - 1 );
1564 mprotect( addr, count << page_shift,
1565 unix_prot | (force_exec_prot ? PROT_EXEC : 0) );
1566 }
1567 }
1568 }
1569 }
1570 server_leave_uninterrupted_section( &csVirtual, &sigset );
1571 }
1572
1573
1574 /***********************************************************************
1575 * VIRTUAL_UseLargeAddressSpace
1576 *
1577 * Increase the address space size for apps that support it.
1578 */
1579 void VIRTUAL_UseLargeAddressSpace(void)
1580 {
1581 /* no large address space on win9x */
1582 if (NtCurrentTeb()->Peb->OSPlatformId != VER_PLATFORM_WIN32_NT) return;
1583 user_space_limit = working_set_limit = address_space_limit;
1584 }
1585
1586
1587 /***********************************************************************
1588 * NtAllocateVirtualMemory (NTDLL.@)
1589 * ZwAllocateVirtualMemory (NTDLL.@)
1590 */
1591 NTSTATUS WINAPI NtAllocateVirtualMemory( HANDLE process, PVOID *ret, ULONG zero_bits,
1592 SIZE_T *size_ptr, ULONG type, ULONG protect )
1593 {
1594 void *base;
1595 unsigned int vprot;
1596 SIZE_T size = *size_ptr;
1597 SIZE_T mask = get_mask( zero_bits );
1598 NTSTATUS status = STATUS_SUCCESS;
1599 struct file_view *view;
1600 sigset_t sigset;
1601
1602 TRACE("%p %p %08lx %x %08x\n", process, *ret, size, type, protect );
1603
1604 if (!size) return STATUS_INVALID_PARAMETER;
1605
1606 if (process != NtCurrentProcess())
1607 {
1608 apc_call_t call;
1609 apc_result_t result;
1610
1611 memset( &call, 0, sizeof(call) );
1612
1613 call.virtual_alloc.type = APC_VIRTUAL_ALLOC;
1614 call.virtual_alloc.addr = wine_server_client_ptr( *ret );
1615 call.virtual_alloc.size = *size_ptr;
1616 call.virtual_alloc.zero_bits = zero_bits;
1617 call.virtual_alloc.op_type = type;
1618 call.virtual_alloc.prot = protect;
1619 status = NTDLL_queue_process_apc( process, &call, &result );
1620 if (status != STATUS_SUCCESS) return status;
1621
1622 if (result.virtual_alloc.status == STATUS_SUCCESS)
1623 {
1624 *ret = wine_server_get_ptr( result.virtual_alloc.addr );
1625 *size_ptr = result.virtual_alloc.size;
1626 }
1627 return result.virtual_alloc.status;
1628 }
1629
1630 /* Round parameters to a page boundary */
1631
1632 if (is_beyond_limit( 0, size, working_set_limit )) return STATUS_WORKING_SET_LIMIT_RANGE;
1633
1634 if ((status = get_vprot_flags( protect, &vprot ))) return status;
1635 vprot |= VPROT_VALLOC;
1636 if (type & MEM_COMMIT) vprot |= VPROT_COMMITTED;
1637
1638 if (*ret)
1639 {
1640 if (type & MEM_RESERVE) /* Round down to 64k boundary */
1641 base = ROUND_ADDR( *ret, mask );
1642 else
1643 base = ROUND_ADDR( *ret, page_mask );
1644 size = (((UINT_PTR)*ret + size + page_mask) & ~page_mask) - (UINT_PTR)base;
1645
1646 /* address 1 is magic to mean DOS area */
1647 if (!base && *ret == (void *)1 && size == 0x110000)
1648 {
1649 server_enter_uninterrupted_section( &csVirtual, &sigset );
1650 status = allocate_dos_memory( &view, vprot );
1651 if (status == STATUS_SUCCESS)
1652 {
1653 *ret = view->base;
1654 *size_ptr = view->size;
1655 }
1656 server_leave_uninterrupted_section( &csVirtual, &sigset );
1657 return status;
1658 }
1659
1660 /* disallow low 64k, wrap-around and kernel space */
1661 if (((char *)base < (char *)0x10000) ||
1662 ((char *)base + size < (char *)base) ||
1663 is_beyond_limit( base, size, address_space_limit ))
1664 return STATUS_INVALID_PARAMETER;
1665 }
1666 else
1667 {
1668 base = NULL;
1669 size = (size + page_mask) & ~page_mask;
1670 }
1671
1672 /* Compute the alloc type flags */
1673
1674 if (!(type & (MEM_COMMIT | MEM_RESERVE)) ||
1675 (type & ~(MEM_COMMIT | MEM_RESERVE | MEM_TOP_DOWN | MEM_WRITE_WATCH | MEM_RESET)))
1676 {
1677 WARN("called with wrong alloc type flags (%08x) !\n", type);
1678 return STATUS_INVALID_PARAMETER;
1679 }
1680
1681 /* Reserve the memory */
1682
1683 if (use_locks) server_enter_uninterrupted_section( &csVirtual, &sigset );
1684
1685 if ((type & MEM_RESERVE) || !base)
1686 {
1687 if (type & MEM_WRITE_WATCH) vprot |= VPROT_WRITEWATCH;
1688 status = map_view( &view, base, size, mask, type & MEM_TOP_DOWN, vprot );
1689 if (status == STATUS_SUCCESS) base = view->base;
1690 }
1691 else /* commit the pages */
1692 {
1693 if (!(view = VIRTUAL_FindView( base, size ))) status = STATUS_NOT_MAPPED_VIEW;
1694 else if (!VIRTUAL_SetProt( view, base, size, vprot )) status = STATUS_ACCESS_DENIED;
1695 else if (view->mapping && !(view->protect & VPROT_COMMITTED))
1696 {
1697 SERVER_START_REQ( add_mapping_committed_range )
1698 {
1699 req->handle = wine_server_obj_handle( view->mapping );
1700 req->offset = (char *)base - (char *)view->base;
1701 req->size = size;
1702 wine_server_call( req );
1703 }
1704 SERVER_END_REQ;
1705 }
1706 }
1707
1708 if (use_locks) server_leave_uninterrupted_section( &csVirtual, &sigset );
1709
1710 if (status == STATUS_SUCCESS)
1711 {
1712 *ret = base;
1713 *size_ptr = size;
1714 }
1715 return status;
1716 }
1717
1718
1719 /***********************************************************************
1720 * NtFreeVirtualMemory (NTDLL.@)
1721 * ZwFreeVirtualMemory (NTDLL.@)
1722 */
1723 NTSTATUS WINAPI NtFreeVirtualMemory( HANDLE process, PVOID *addr_ptr, SIZE_T *size_ptr, ULONG type )
1724 {
1725 FILE_VIEW *view;
1726 char *base;
1727 sigset_t sigset;
1728 NTSTATUS status = STATUS_SUCCESS;
1729 LPVOID addr = *addr_ptr;
1730 SIZE_T size = *size_ptr;
1731
1732 TRACE("%p %p %08lx %x\n", process, addr, size, type );
1733
1734 if (process != NtCurrentProcess())
1735 {
1736 apc_call_t call;
1737 apc_result_t result;
1738
1739 memset( &call, 0, sizeof(call) );
1740
1741 call.virtual_free.type = APC_VIRTUAL_FREE;
1742 call.virtual_free.addr = wine_server_client_ptr( addr );
1743 call.virtual_free.size = size;
1744 call.virtual_free.op_type = type;
1745 status = NTDLL_queue_process_apc( process, &call, &result );
1746 if (status != STATUS_SUCCESS) return status;
1747
1748 if (result.virtual_free.status == STATUS_SUCCESS)
1749 {
1750 *addr_ptr = wine_server_get_ptr( result.virtual_free.addr );
1751 *size_ptr = result.virtual_free.size;
1752 }
1753 return result.virtual_free.status;
1754 }
1755
1756 /* Fix the parameters */
1757
1758 size = ROUND_SIZE( addr, size );
1759 base = ROUND_ADDR( addr, page_mask );
1760
1761 /* avoid freeing the DOS area when a broken app passes a NULL pointer */
1762 if (!base) return STATUS_INVALID_PARAMETER;
1763
1764 server_enter_uninterrupted_section( &csVirtual, &sigset );
1765
1766 if (!(view = VIRTUAL_FindView( base, size )) || !(view->protect & VPROT_VALLOC))
1767 {
1768 status = STATUS_INVALID_PARAMETER;
1769 }
1770 else if (type == MEM_RELEASE)
1771 {
1772 /* Free the pages */
1773
1774 if (size || (base != view->base)) status = STATUS_INVALID_PARAMETER;
1775 else
1776 {
1777 delete_view( view );
1778 *addr_ptr = base;
1779 *size_ptr = size;
1780 }
1781 }
1782 else if (type == MEM_DECOMMIT)
1783 {
1784 status = decommit_pages( view, base - (char *)view->base, size );
1785 if (status == STATUS_SUCCESS)
1786 {
1787 *addr_ptr = base;
1788 *size_ptr = size;
1789 }
1790 }
1791 else
1792 {
1793 WARN("called with wrong free type flags (%08x) !\n", type);
1794 status = STATUS_INVALID_PARAMETER;
1795 }
1796
1797 server_leave_uninterrupted_section( &csVirtual, &sigset );
1798 return status;
1799 }
1800
1801
1802 /***********************************************************************
1803 * NtProtectVirtualMemory (NTDLL.@)
1804 * ZwProtectVirtualMemory (NTDLL.@)
1805 */
1806 NTSTATUS WINAPI NtProtectVirtualMemory( HANDLE process, PVOID *addr_ptr, SIZE_T *size_ptr,
1807 ULONG new_prot, ULONG *old_prot )
1808 {
1809 FILE_VIEW *view;
1810 sigset_t sigset;
1811 NTSTATUS status = STATUS_SUCCESS;
1812 char *base;
1813 BYTE vprot;
1814 unsigned int new_vprot;
1815 SIZE_T size = *size_ptr;
1816 LPVOID addr = *addr_ptr;
1817
1818 TRACE("%p %p %08lx %08x\n", process, addr, size, new_prot );
1819
1820 if (process != NtCurrentProcess())
1821 {
1822 apc_call_t call;
1823 apc_result_t result;
1824
1825 memset( &call, 0, sizeof(call) );
1826
1827 call.virtual_protect.type = APC_VIRTUAL_PROTECT;
1828 call.virtual_protect.addr = wine_server_client_ptr( addr );
1829 call.virtual_protect.size = size;
1830 call.virtual_protect.prot = new_prot;
1831 status = NTDLL_queue_process_apc( process, &call, &result );
1832 if (status != STATUS_SUCCESS) return status;
1833
1834 if (result.virtual_protect.status == STATUS_SUCCESS)
1835 {
1836 *addr_ptr = wine_server_get_ptr( result.virtual_protect.addr );
1837 *size_ptr = result.virtual_protect.size;
1838 if (old_prot) *old_prot = result.virtual_protect.prot;
1839 }
1840 return result.virtual_protect.status;
1841 }
1842
1843 /* Fix the parameters */
1844
1845 size = ROUND_SIZE( addr, size );
1846 base = ROUND_ADDR( addr, page_mask );
1847 if ((status = get_vprot_flags( new_prot, &new_vprot ))) return status;
1848 new_vprot |= VPROT_COMMITTED;
1849
1850 server_enter_uninterrupted_section( &csVirtual, &sigset );
1851
1852 if (!(view = VIRTUAL_FindView( base, size )))
1853 {
1854 status = STATUS_INVALID_PARAMETER;
1855 }
1856 else
1857 {
1858 /* Make sure all the pages are committed */
1859 if (get_committed_size( view, base, &vprot ) >= size && (vprot & VPROT_COMMITTED))
1860 {
1861 if (old_prot) *old_prot = VIRTUAL_GetWin32Prot( vprot );
1862 if (!VIRTUAL_SetProt( view, base, size, new_vprot )) status = STATUS_ACCESS_DENIED;
1863 }
1864 else status = STATUS_NOT_COMMITTED;
1865 }
1866 server_leave_uninterrupted_section( &csVirtual, &sigset );
1867
1868 if (status == STATUS_SUCCESS)
1869 {
1870 *addr_ptr = base;
1871 *size_ptr = size;
1872 }
1873 return status;
1874 }
1875
1876
1877 /* retrieve state for a free memory area; callback for wine_mmap_enum_reserved_areas */
1878 static int get_free_mem_state_callback( void *start, size_t size, void *arg )
1879 {
1880 MEMORY_BASIC_INFORMATION *info = arg;
1881 void *end = (char *)start + size;
1882
1883 if ((char *)info->BaseAddress + info->RegionSize < (char *)start) return 0;
1884
1885 if (info->BaseAddress >= end)
1886 {
1887 if (info->AllocationBase < end) info->AllocationBase = end;
1888 return 0;
1889 }
1890
1891 if (info->BaseAddress >= start)
1892 {
1893 /* it's a real free area */
1894 info->State = MEM_FREE;
1895 info->Protect = PAGE_NOACCESS;
1896 info->AllocationBase = 0;
1897 info->AllocationProtect = 0;
1898 info->Type = 0;
1899 if ((char *)info->BaseAddress + info->RegionSize > (char *)end)
1900 info->RegionSize = (char *)end - (char *)info->BaseAddress;
1901 }
1902 else /* outside of the reserved area, pretend it's allocated */
1903 {
1904 info->RegionSize = (char *)start - (char *)info->BaseAddress;
1905 info->State = MEM_RESERVE;
1906 info->Protect = PAGE_NOACCESS;
1907 info->AllocationProtect = PAGE_NOACCESS;
1908 info->Type = MEM_PRIVATE;
1909 }
1910 return 1;
1911 }
1912
1913 #define UNIMPLEMENTED_INFO_CLASS(c) \
1914 case c: \
1915 FIXME("(process=%p,addr=%p) Unimplemented information class: " #c "\n", process, addr); \
1916 return STATUS_INVALID_INFO_CLASS
1917
1918 /***********************************************************************
1919 * NtQueryVirtualMemory (NTDLL.@)
1920 * ZwQueryVirtualMemory (NTDLL.@)
1921 */
1922 NTSTATUS WINAPI NtQueryVirtualMemory( HANDLE process, LPCVOID addr,
1923 MEMORY_INFORMATION_CLASS info_class, PVOID buffer,
1924 SIZE_T len, SIZE_T *res_len )
1925 {
1926 FILE_VIEW *view;
1927 char *base, *alloc_base = 0;
1928 struct list *ptr;
1929 SIZE_T size = 0;
1930 MEMORY_BASIC_INFORMATION *info = buffer;
1931 sigset_t sigset;
1932
1933 if (info_class != MemoryBasicInformation)
1934 {
1935 switch(info_class)
1936 {
1937 UNIMPLEMENTED_INFO_CLASS(MemoryWorkingSetList);
1938 UNIMPLEMENTED_INFO_CLASS(MemorySectionName);
1939 UNIMPLEMENTED_INFO_CLASS(MemoryBasicVlmInformation);
1940
1941 default:
1942 FIXME("(%p,%p,info_class=%d,%p,%ld,%p) Unknown information class\n",
1943 process, addr, info_class, buffer, len, res_len);
1944 return STATUS_INVALID_INFO_CLASS;
1945 }
1946 }
1947
1948 if (process != NtCurrentProcess())
1949 {
1950 NTSTATUS status;
1951 apc_call_t call;
1952 apc_result_t result;
1953
1954 memset( &call, 0, sizeof(call) );
1955
1956 call.virtual_query.type = APC_VIRTUAL_QUERY;
1957 call.virtual_query.addr = wine_server_client_ptr( addr );
1958 status = NTDLL_queue_process_apc( process, &call, &result );
1959 if (status != STATUS_SUCCESS) return status;
1960
1961 if (result.virtual_query.status == STATUS_SUCCESS)
1962 {
1963 info->BaseAddress = wine_server_get_ptr( result.virtual_query.base );
1964 info->AllocationBase = wine_server_get_ptr( result.virtual_query.alloc_base );
1965 info->RegionSize = result.virtual_query.size;
1966 info->Protect = result.virtual_query.prot;
1967 info->AllocationProtect = result.virtual_query.alloc_prot;
1968 info->State = (DWORD)result.virtual_query.state << 12;
1969 info->Type = (DWORD)result.virtual_query.alloc_type << 16;
1970 if (info->RegionSize != result.virtual_query.size) /* truncated */
1971 return STATUS_INVALID_PARAMETER; /* FIXME */
1972 if (res_len) *res_len = sizeof(*info);
1973 }
1974 return result.virtual_query.status;
1975 }
1976
1977 base = ROUND_ADDR( addr, page_mask );
1978
1979 if (is_beyond_limit( base, 1, working_set_limit )) return STATUS_WORKING_SET_LIMIT_RANGE;
1980
1981 /* Find the view containing the address */
1982
1983 server_enter_uninterrupted_section( &csVirtual, &sigset );
1984 ptr = list_head( &views_list );
1985 for (;;)
1986 {
1987 if (!ptr)
1988 {
1989 size = (char *)working_set_limit - alloc_base;
1990 view = NULL;
1991 break;
1992 }
1993 view = LIST_ENTRY( ptr, struct file_view, entry );
1994 if ((char *)view->base > base)
1995 {
1996 size = (char *)view->base - alloc_base;
1997 view = NULL;
1998 break;
1999 }
2000 if ((char *)view->base + view->size > base)
2001 {
2002 alloc_base = view->base;
2003 size = view->size;
2004 break;
2005 }
2006 alloc_base = (char *)view->base + view->size;
2007 ptr = list_next( &views_list, ptr );
2008 }
2009
2010 /* Fill the info structure */
2011
2012 info->AllocationBase = alloc_base;
2013 info->BaseAddress = base;
2014 info->RegionSize = size - (base - alloc_base);
2015
2016 if (!view)
2017 {
2018 if (!wine_mmap_enum_reserved_areas( get_free_mem_state_callback, info, 0 ))
2019 {
2020 /* not in a reserved area at all, pretend it's allocated */
2021 info->State = MEM_RESERVE;
2022 info->Protect = PAGE_NOACCESS;
2023 info->AllocationProtect = PAGE_NOACCESS;
2024 info->Type = MEM_PRIVATE;
2025 }
2026 }
2027 else
2028 {
2029 BYTE vprot;
2030 SIZE_T range_size = get_committed_size( view, base, &vprot );
2031
2032 info->State = (vprot & VPROT_COMMITTED) ? MEM_COMMIT : MEM_RESERVE;
2033 info->Protect = (vprot & VPROT_COMMITTED) ? VIRTUAL_GetWin32Prot( vprot ) : 0;
2034 info->AllocationBase = alloc_base;
2035 info->AllocationProtect = VIRTUAL_GetWin32Prot( view->protect );
2036 if (view->protect & VPROT_IMAGE) info->Type = MEM_IMAGE;
2037 else if (view->protect & VPROT_VALLOC) info->Type = MEM_PRIVATE;
2038 else info->Type = MEM_MAPPED;
2039 for (size = base - alloc_base; size < base + range_size - alloc_base; size += page_size)
2040 if ((view->prot[size >> page_shift] ^ vprot) & ~VPROT_WRITEWATCH) break;
2041 info->RegionSize = size - (base - alloc_base);
2042 }
2043 server_leave_uninterrupted_section( &csVirtual, &sigset );
2044
2045 if (res_len) *res_len = sizeof(*info);
2046 return STATUS_SUCCESS;
2047 }
2048
2049
2050 /***********************************************************************
2051 * NtLockVirtualMemory (NTDLL.@)
2052 * ZwLockVirtualMemory (NTDLL.@)
2053 */
2054 NTSTATUS WINAPI NtLockVirtualMemory( HANDLE process, PVOID *addr, SIZE_T *size, ULONG unknown )
2055 {
2056 NTSTATUS status = STATUS_SUCCESS;
2057
2058 if (process != NtCurrentProcess())
2059 {
2060 apc_call_t call;
2061 apc_result_t result;
2062
2063 memset( &call, 0, sizeof(call) );
2064
2065 call.virtual_lock.type = APC_VIRTUAL_LOCK;
2066 call.virtual_lock.addr = wine_server_client_ptr( *addr );
2067 call.virtual_lock.size = *size;
2068 status = NTDLL_queue_process_apc( process, &call, &result );
2069 if (status != STATUS_SUCCESS) return status;
2070
2071 if (result.virtual_lock.status == STATUS_SUCCESS)
2072 {
2073 *addr = wine_server_get_ptr( result.virtual_lock.addr );
2074 *size = result.virtual_lock.size;
2075 }
2076 return result.virtual_lock.status;
2077 }
2078
2079 *size = ROUND_SIZE( *addr, *size );
2080 *addr = ROUND_ADDR( *addr, page_mask );
2081
2082 if (mlock( *addr, *size )) status = STATUS_ACCESS_DENIED;
2083 return status;
2084 }
2085
2086
2087 /***********************************************************************
2088 * NtUnlockVirtualMemory (NTDLL.@)
2089 * ZwUnlockVirtualMemory (NTDLL.@)
2090 */
2091 NTSTATUS WINAPI NtUnlockVirtualMemory( HANDLE process, PVOID *addr, SIZE_T *size, ULONG unknown )
2092 {
2093 NTSTATUS status = STATUS_SUCCESS;
2094
2095 if (process != NtCurrentProcess())
2096 {
2097 apc_call_t call;
2098 apc_result_t result;
2099
2100 memset( &call, 0, sizeof(call) );
2101
2102 call.virtual_unlock.type = APC_VIRTUAL_UNLOCK;
2103 call.virtual_unlock.addr = wine_server_client_ptr( *addr );
2104 call.virtual_unlock.size = *size;
2105 status = NTDLL_queue_process_apc( process, &call, &result );
2106 if (status != STATUS_SUCCESS) return status;
2107
2108 if (result.virtual_unlock.status == STATUS_SUCCESS)
2109 {
2110 *addr = wine_server_get_ptr( result.virtual_unlock.addr );
2111 *size = result.virtual_unlock.size;
2112 }
2113 return result.virtual_unlock.status;
2114 }
2115
2116 *size = ROUND_SIZE( *addr, *size );
2117 *addr = ROUND_ADDR( *addr, page_mask );
2118
2119 if (munlock( *addr, *size )) status = STATUS_ACCESS_DENIED;
2120 return status;
2121 }
2122
2123
2124 /***********************************************************************
2125 * NtCreateSection (NTDLL.@)
2126 * ZwCreateSection (NTDLL.@)
2127 */
2128 NTSTATUS WINAPI NtCreateSection( HANDLE *handle, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr,
2129 const LARGE_INTEGER *size, ULONG protect,
2130 ULONG sec_flags, HANDLE file )
2131 {
2132 NTSTATUS ret;
2133 unsigned int vprot;
2134 DWORD len = (attr && attr->ObjectName) ? attr->ObjectName->Length : 0;
2135 struct security_descriptor *sd = NULL;
2136 struct object_attributes objattr;
2137
2138 /* Check parameters */
2139
2140 if (len > MAX_PATH*sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
2141
2142 if ((ret = get_vprot_flags( protect, &vprot ))) return ret;
2143
2144 objattr.rootdir = wine_server_obj_handle( attr ? attr->RootDirectory : 0 );
2145 objattr.sd_len = 0;
2146 objattr.name_len = len;
2147 if (attr)
2148 {
2149 ret = NTDLL_create_struct_sd( attr->SecurityDescriptor, &sd, &objattr.sd_len );
2150 if (ret != STATUS_SUCCESS) return ret;
2151 }
2152
2153 if (!(sec_flags & SEC_RESERVE)) vprot |= VPROT_COMMITTED;
2154 if (sec_flags & SEC_NOCACHE) vprot |= VPROT_NOCACHE;
2155 if (sec_flags & SEC_IMAGE) vprot |= VPROT_IMAGE;
2156
2157 /* Create the server object */
2158
2159 SERVER_START_REQ( create_mapping )
2160 {
2161 req->access = access;
2162 req->attributes = (attr) ? attr->Attributes : 0;
2163 req->file_handle = wine_server_obj_handle( file );
2164 req->size = size ? size->QuadPart : 0;
2165 req->protect = vprot;
2166 wine_server_add_data( req, &objattr, sizeof(objattr) );
2167 if (objattr.sd_len) wine_server_add_data( req, sd, objattr.sd_len );
2168 if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
2169 ret = wine_server_call( req );
2170 *handle = wine_server_ptr_handle( reply->handle );
2171 }
2172 SERVER_END_REQ;
2173
2174 NTDLL_free_struct_sd( sd );
2175
2176 return ret;
2177 }
2178
2179
2180 /***********************************************************************
2181 * NtOpenSection (NTDLL.@)
2182 * ZwOpenSection (NTDLL.@)
2183 */
2184 NTSTATUS WINAPI NtOpenSection( HANDLE *handle, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr )
2185 {
2186 NTSTATUS ret;
2187 DWORD len = attr->ObjectName->Length;
2188
2189 if (len > MAX_PATH*sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
2190
2191 SERVER_START_REQ( open_mapping )
2192 {
2193 req->access = access;
2194 req->attributes = attr->Attributes;
2195 req->rootdir = wine_server_obj_handle( attr->RootDirectory );
2196 wine_server_add_data( req, attr->ObjectName->Buffer, len );
2197 if (!(ret = wine_server_call( req ))) *handle = wine_server_ptr_handle( reply->handle );
2198 }
2199 SERVER_END_REQ;
2200 return ret;
2201 }
2202
2203
2204 /***********************************************************************
2205 * NtMapViewOfSection (NTDLL.@)
2206 * ZwMapViewOfSection (NTDLL.@)
2207 */
2208 NTSTATUS WINAPI NtMapViewOfSection( HANDLE handle, HANDLE process, PVOID *addr_ptr, ULONG zero_bits,
2209 SIZE_T commit_size, const LARGE_INTEGER *offset_ptr, SIZE_T *size_ptr,
2210 SECTION_INHERIT inherit, ULONG alloc_type, ULONG protect )
2211 {
2212 NTSTATUS res;
2213 mem_size_t full_size;
2214 ACCESS_MASK access;
2215 SIZE_T size = 0;
2216 SIZE_T mask = get_mask( zero_bits );
2217 int unix_handle = -1, needs_close;
2218 unsigned int map_vprot, vprot;
2219 void *base;
2220 struct file_view *view;
2221 DWORD header_size;
2222 HANDLE dup_mapping, shared_file;
2223 LARGE_INTEGER offset;
2224 sigset_t sigset;
2225
2226 offset.QuadPart = offset_ptr ? offset_ptr->QuadPart : 0;
2227
2228 TRACE("handle=%p process=%p addr=%p off=%x%08x size=%lx access=%x\n",
2229 handle, process, *addr_ptr, offset.u.HighPart, offset.u.LowPart, size, protect );
2230
2231 /* Check parameters */
2232
2233 if ((offset.u.LowPart & mask) || (*addr_ptr && ((UINT_PTR)*addr_ptr & mask)))
2234 return STATUS_INVALID_PARAMETER;
2235
2236 switch(protect)
2237 {
2238 case PAGE_NOACCESS:
2239 access = SECTION_QUERY;
2240 break;
2241 case PAGE_READWRITE:
2242 case PAGE_EXECUTE_READWRITE:
2243 access = SECTION_QUERY | SECTION_MAP_WRITE;
2244 break;
2245 case PAGE_READONLY:
2246 case PAGE_WRITECOPY:
2247 case PAGE_EXECUTE:
2248 case PAGE_EXECUTE_READ:
2249 case PAGE_EXECUTE_WRITECOPY:
2250 access = SECTION_QUERY | SECTION_MAP_READ;
2251 break;
2252 default:
2253 return STATUS_INVALID_PARAMETER;
2254 }
2255
2256 if (process != NtCurrentProcess())
2257 {
2258 apc_call_t call;
2259 apc_result_t result;
2260
2261 memset( &call, 0, sizeof(call) );
2262
2263 call.map_view.type = APC_MAP_VIEW;
2264 call.map_view.handle = wine_server_obj_handle( handle );
2265 call.map_view.addr = wine_server_client_ptr( *addr_ptr );
2266 call.map_view.size = *size_ptr;
2267 call.map_view.offset = offset.QuadPart;
2268 call.map_view.zero_bits = zero_bits;
2269 call.map_view.alloc_type = alloc_type;
2270 call.map_view.prot = protect;
2271 res = NTDLL_queue_process_apc( process, &call, &result );
2272 if (res != STATUS_SUCCESS) return res;
2273
2274 if (result.map_view.status == STATUS_SUCCESS)
2275 {
2276 *addr_ptr = wine_server_get_ptr( result.map_view.addr );
2277 *size_ptr = result.map_view.size;
2278 }
2279 return result.map_view.status;
2280 }
2281
2282 SERVER_START_REQ( get_mapping_info )
2283 {
2284 req->handle = wine_server_obj_handle( handle );
2285 req->access = access;
2286 res = wine_server_call( req );
2287 map_vprot = reply->protect;
2288 base = wine_server_get_ptr( reply->base );
2289 full_size = reply->size;
2290 header_size = reply->header_size;
2291 dup_mapping = wine_server_ptr_handle( reply->mapping );
2292 shared_file = wine_server_ptr_handle( reply->shared_file );
2293 if ((ULONG_PTR)base != reply->base) base = NULL;
2294 }
2295 SERVER_END_REQ;
2296 if (res) return res;
2297
2298 size = full_size;
2299 if (size != full_size)
2300 {
2301 WARN( "Sizes larger than 4Gb (%s) not supported on this platform\n",
2302 wine_dbgstr_longlong(full_size) );
2303 if (dup_mapping) NtClose( dup_mapping );
2304 return STATUS_INVALID_PARAMETER;
2305 }
2306
2307 if ((res = server_get_unix_fd( handle, 0, &unix_handle, &needs_close, NULL, NULL ))) goto done;
2308
2309 if (map_vprot & VPROT_IMAGE)
2310 {
2311 if (shared_file)
2312 {
2313 int shared_fd, shared_needs_close;
2314
2315 if ((res = server_get_unix_fd( shared_file, FILE_READ_DATA|FILE_WRITE_DATA,
2316 &shared_fd, &shared_needs_close, NULL, NULL ))) goto done;
2317 res = map_image( handle, unix_handle, base, size, mask, header_size,
2318 shared_fd, dup_mapping, addr_ptr );
2319 if (shared_needs_close) close( shared_fd );
2320 NtClose( shared_file );
2321 }
2322 else
2323 {
2324 res = map_image( handle, unix_handle, base, size, mask, header_size,
2325 -1, dup_mapping, addr_ptr );
2326 }
2327 if (needs_close) close( unix_handle );
2328 if (!res) *size_ptr = size;
2329 return res;
2330 }
2331
2332 if ((offset.QuadPart >= size) || (*size_ptr > size - offset.QuadPart))
2333 {
2334 res = STATUS_INVALID_PARAMETER;
2335 goto done;
2336 }
2337 if (*size_ptr) size = ROUND_SIZE( offset.u.LowPart, *size_ptr );
2338 else size = size - offset.QuadPart;
2339
2340 /* Reserve a properly aligned area */
2341
2342 server_enter_uninterrupted_section( &csVirtual, &sigset );
2343
2344 get_vprot_flags( protect, &vprot );
2345 vprot |= (map_vprot & VPROT_COMMITTED);
2346 res = map_view( &view, *addr_ptr, size, mask, FALSE, vprot );
2347 if (res)
2348 {
2349 server_leave_uninterrupted_section( &csVirtual, &sigset );
2350 goto done;
2351 }
2352
2353 /* Map the file */
2354
2355 TRACE("handle=%p size=%lx offset=%x%08x\n",
2356 handle, size, offset.u.HighPart, offset.u.LowPart );
2357
2358 res = map_file_into_view( view, unix_handle, 0, size, offset.QuadPart, vprot, !dup_mapping );
2359 if (res == STATUS_SUCCESS)
2360 {
2361 *addr_ptr = view->base;
2362 *size_ptr = size;
2363 view->mapping = dup_mapping;
2364 dup_mapping = 0; /* don't close it */
2365 }
2366 else
2367 {
2368 ERR( "map_file_into_view %p %lx %x%08x failed\n",
2369 view->base, size, offset.u.HighPart, offset.u.LowPart );
2370 delete_view( view );
2371 }
2372
2373 server_leave_uninterrupted_section( &csVirtual, &sigset );
2374
2375 done:
2376 if (dup_mapping) NtClose( dup_mapping );
2377 if (needs_close) close( unix_handle );
2378 return res;
2379 }
2380
2381
2382 /***********************************************************************
2383 * NtUnmapViewOfSection (NTDLL.@)
2384 * ZwUnmapViewOfSection (NTDLL.@)
2385 */
2386 NTSTATUS WINAPI NtUnmapViewOfSection( HANDLE process, PVOID addr )
2387 {
2388 FILE_VIEW *view;
2389 NTSTATUS status = STATUS_INVALID_PARAMETER;
2390 sigset_t sigset;
2391 void *base = ROUND_ADDR( addr, page_mask );
2392
2393 if (process != NtCurrentProcess())
2394 {
2395 apc_call_t call;
2396 apc_result_t result;
2397
2398 memset( &call, 0, sizeof(call) );
2399
2400 call.unmap_view.type = APC_UNMAP_VIEW;
2401 call.unmap_view.addr = wine_server_client_ptr( addr );
2402 status = NTDLL_queue_process_apc( process, &call, &result );
2403 if (status == STATUS_SUCCESS) status = result.unmap_view.status;
2404 return status;
2405 }
2406
2407 server_enter_uninterrupted_section( &csVirtual, &sigset );
2408 if ((view = VIRTUAL_FindView( base, 0 )) && (base == view->base))
2409 {
2410 delete_view( view );
2411 status = STATUS_SUCCESS;
2412 }
2413 server_leave_uninterrupted_section( &csVirtual, &sigset );
2414 return status;
2415 }
2416
2417
2418 /***********************************************************************
2419 * NtFlushVirtualMemory (NTDLL.@)
2420 * ZwFlushVirtualMemory (NTDLL.@)
2421 */
2422 NTSTATUS WINAPI NtFlushVirtualMemory( HANDLE process, LPCVOID *addr_ptr,
2423 SIZE_T *size_ptr, ULONG unknown )
2424 {
2425 FILE_VIEW *view;
2426 NTSTATUS status = STATUS_SUCCESS;
2427 sigset_t sigset;
2428 void *addr = ROUND_ADDR( *addr_ptr, page_mask );
2429
2430 if (process != NtCurrentProcess())
2431 {
2432 apc_call_t call;
2433 apc_result_t result;
2434
2435 memset( &call, 0, sizeof(call) );
2436
2437 call.virtual_flush.type = APC_VIRTUAL_FLUSH;
2438 call.virtual_flush.addr = wine_server_client_ptr( addr );
2439 call.virtual_flush.size = *size_ptr;
2440 status = NTDLL_queue_process_apc( process, &call, &result );
2441 if (status != STATUS_SUCCESS) return status;
2442
2443 if (result.virtual_flush.status == STATUS_SUCCESS)
2444 {
2445 *addr_ptr = wine_server_get_ptr( result.virtual_flush.addr );
2446 *size_ptr = result.virtual_flush.size;
2447 }
2448 return result.virtual_flush.status;
2449 }
2450
2451 server_enter_uninterrupted_section( &csVirtual, &sigset );
2452 if (!(view = VIRTUAL_FindView( addr, *size_ptr ))) status = STATUS_INVALID_PARAMETER;
2453 else
2454 {
2455 if (!*size_ptr) *size_ptr = view->size;
2456 *addr_ptr = addr;
2457 if (msync( addr, *size_ptr, MS_SYNC )) status = STATUS_NOT_MAPPED_DATA;
2458 }
2459 server_leave_uninterrupted_section( &csVirtual, &sigset );
2460 return status;
2461 }
2462
2463
2464 /***********************************************************************
2465 * NtGetWriteWatch (NTDLL.@)
2466 * ZwGetWriteWatch (NTDLL.@)
2467 */
2468 NTSTATUS WINAPI NtGetWriteWatch( HANDLE process, ULONG flags, PVOID base, SIZE_T size, PVOID *addresses,
2469 ULONG_PTR *count, ULONG *granularity )
2470 {
2471 struct file_view *view;
2472 NTSTATUS status = STATUS_SUCCESS;
2473 sigset_t sigset;
2474
2475 size = ROUND_SIZE( base, size );
2476 base = ROUND_ADDR( base, page_mask );
2477
2478 if (!count || !granularity) return STATUS_ACCESS_VIOLATION;
2479 if (!*count || !size) return STATUS_INVALID_PARAMETER;
2480 if (flags & ~WRITE_WATCH_FLAG_RESET) return STATUS_INVALID_PARAMETER;
2481
2482 if (!addresses) return STATUS_ACCESS_VIOLATION;
2483
2484 TRACE( "%p %x %p-%p %p %lu\n", process, flags, base, (char *)base + size,
2485 addresses, count ? *count : 0 );
2486
2487 server_enter_uninterrupted_section( &csVirtual, &sigset );
2488
2489 if ((view = VIRTUAL_FindView( base, size )) && (view->protect & VPROT_WRITEWATCH))
2490 {
2491 ULONG_PTR pos = 0;
2492 char *addr = base;
2493 char *end = addr + size;
2494
2495 while (pos < *count && addr < end)
2496 {
2497 BYTE prot = view->prot[(addr - (char *)view->base) >> page_shift];
2498 if (!(prot & VPROT_WRITEWATCH)) addresses[pos++] = addr;
2499 addr += page_size;
2500 }
2501 if (flags & WRITE_WATCH_FLAG_RESET) reset_write_watches( view, base, addr - (char *)base );
2502 *count = pos;
2503 *granularity = page_size;
2504 }
2505 else status = STATUS_INVALID_PARAMETER;
2506
2507 server_leave_uninterrupted_section( &csVirtual, &sigset );
2508 return status;
2509 }
2510
2511
2512 /***********************************************************************
2513 * NtResetWriteWatch (NTDLL.@)
2514 * ZwResetWriteWatch (NTDLL.@)
2515 */
2516 NTSTATUS WINAPI NtResetWriteWatch( HANDLE process, PVOID base, SIZE_T size )
2517 {
2518 struct file_view *view;
2519 NTSTATUS status = STATUS_SUCCESS;
2520 sigset_t sigset;
2521
2522 size = ROUND_SIZE( base, size );
2523 base = ROUND_ADDR( base, page_mask );
2524
2525 TRACE( "%p %p-%p\n", process, base, (char *)base + size );
2526
2527 if (!size) return STATUS_INVALID_PARAMETER;
2528
2529 server_enter_uninterrupted_section( &csVirtual, &sigset );
2530
2531 if ((view = VIRTUAL_FindView( base, size )) && (view->protect & VPROT_WRITEWATCH))
2532 reset_write_watches( view, base, size );
2533 else
2534 status = STATUS_INVALID_PARAMETER;
2535
2536 server_leave_uninterrupted_section( &csVirtual, &sigset );
2537 return status;
2538 }
2539
2540
2541 /***********************************************************************
2542 * NtReadVirtualMemory (NTDLL.@)
2543 * ZwReadVirtualMemory (NTDLL.@)
2544 */
2545 NTSTATUS WINAPI NtReadVirtualMemory( HANDLE process, const void *addr, void *buffer,
2546 SIZE_T size, SIZE_T *bytes_read )
2547 {
2548 NTSTATUS status;
2549
2550 SERVER_START_REQ( read_process_memory )
2551 {
2552 req->handle = wine_server_obj_handle( process );
2553 req->addr = wine_server_client_ptr( addr );
2554 wine_server_set_reply( req, buffer, size );
2555 if ((status = wine_server_call( req ))) size = 0;
2556 }
2557 SERVER_END_REQ;
2558 if (bytes_read) *bytes_read = size;
2559 return status;
2560 }
2561
2562
2563 /***********************************************************************
2564 * NtWriteVirtualMemory (NTDLL.@)
2565 * ZwWriteVirtualMemory (NTDLL.@)
2566 */
2567 NTSTATUS WINAPI NtWriteVirtualMemory( HANDLE process, void *addr, const void *buffer,
2568 SIZE_T size, SIZE_T *bytes_written )
2569 {
2570 NTSTATUS status;
2571
2572 SERVER_START_REQ( write_process_memory )
2573 {
2574 req->handle = wine_server_obj_handle( process );
2575 req->addr = wine_server_client_ptr( addr );
2576 wine_server_add_data( req, buffer, size );
2577 if ((status = wine_server_call( req ))) size = 0;
2578 }
2579 SERVER_END_REQ;
2580 if (bytes_written) *bytes_written = size;
2581 return status;
2582 }
2583
2584
2585 /***********************************************************************
2586 * NtAreMappedFilesTheSame (NTDLL.@)
2587 * ZwAreMappedFilesTheSame (NTDLL.@)
2588 */
2589 NTSTATUS WINAPI NtAreMappedFilesTheSame(PVOID addr1, PVOID addr2)
2590 {
2591 TRACE("%p %p\n", addr1, addr2);
2592
2593 return STATUS_NOT_SAME_DEVICE;
2594 }
2595
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.