2 * Win32 virtual memory functions
4 * Copyright 1997, 2002 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
34 #include <sys/types.h>
35 #ifdef HAVE_SYS_SOCKET_H
36 # include <sys/socket.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_SYS_SYSINFO_H
45 # include <sys/sysinfo.h>
47 #ifdef HAVE_VALGRIND_VALGRIND_H
48 # include <valgrind/valgrind.h>
52 #define WIN32_NO_STATUS
53 #define NONAMELESSUNION
56 #include "wine/library.h"
57 #include "wine/server.h"
58 #include "wine/exception.h"
59 #include "wine/rbtree.h"
60 #include "wine/debug.h"
61 #include "ntdll_misc.h"
63 WINE_DEFAULT_DEBUG_CHANNEL(virtual);
64 WINE_DECLARE_DEBUG_CHANNEL(module
);
67 #define MAP_NORESERVE 0
73 struct wine_rb_entry entry
; /* entry in global view tree */
74 void *base
; /* base address */
75 size_t size
; /* size in bytes */
76 unsigned int protect
; /* protection for all pages at allocation time and SEC_* flags */
79 /* per-page protection flags */
80 #define VPROT_READ 0x01
81 #define VPROT_WRITE 0x02
82 #define VPROT_EXEC 0x04
83 #define VPROT_WRITECOPY 0x08
84 #define VPROT_GUARD 0x10
85 #define VPROT_COMMITTED 0x20
86 #define VPROT_WRITEWATCH 0x40
87 /* per-mapping protection flags */
88 #define VPROT_SYSTEM 0x0200 /* system view (underlying mmap not under our control) */
90 /* Conversion from VPROT_* to Win32 flags */
91 static const BYTE VIRTUAL_Win32Flags
[16] =
93 PAGE_NOACCESS
, /* 0 */
94 PAGE_READONLY
, /* READ */
95 PAGE_READWRITE
, /* WRITE */
96 PAGE_READWRITE
, /* READ | WRITE */
97 PAGE_EXECUTE
, /* EXEC */
98 PAGE_EXECUTE_READ
, /* READ | EXEC */
99 PAGE_EXECUTE_READWRITE
, /* WRITE | EXEC */
100 PAGE_EXECUTE_READWRITE
, /* READ | WRITE | EXEC */
101 PAGE_WRITECOPY
, /* WRITECOPY */
102 PAGE_WRITECOPY
, /* READ | WRITECOPY */
103 PAGE_WRITECOPY
, /* WRITE | WRITECOPY */
104 PAGE_WRITECOPY
, /* READ | WRITE | WRITECOPY */
105 PAGE_EXECUTE_WRITECOPY
, /* EXEC | WRITECOPY */
106 PAGE_EXECUTE_WRITECOPY
, /* READ | EXEC | WRITECOPY */
107 PAGE_EXECUTE_WRITECOPY
, /* WRITE | EXEC | WRITECOPY */
108 PAGE_EXECUTE_WRITECOPY
/* READ | WRITE | EXEC | WRITECOPY */
111 static struct wine_rb_tree views_tree
;
113 static RTL_CRITICAL_SECTION csVirtual
;
114 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
117 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
118 0, 0, { (DWORD_PTR
)(__FILE__
": csVirtual") }
120 static RTL_CRITICAL_SECTION csVirtual
= { &critsect_debug
, -1, 0, 0, 0, 0 };
123 static const UINT page_shift
= 12;
124 static const UINT_PTR page_mask
= 0xfff;
125 /* Note: these are Windows limits, you cannot change them. */
126 static void *address_space_limit
= (void *)0xc0000000; /* top of the total available address space */
127 static void *user_space_limit
= (void *)0x7fff0000; /* top of the user address space */
128 static void *working_set_limit
= (void *)0x7fff0000; /* top of the current working set */
129 static void *address_space_start
= (void *)0x110000; /* keep DOS area clear */
130 #elif defined(__x86_64__)
131 static const UINT page_shift
= 12;
132 static const UINT_PTR page_mask
= 0xfff;
133 static void *address_space_limit
= (void *)0x7fffffff0000;
134 static void *user_space_limit
= (void *)0x7fffffff0000;
135 static void *working_set_limit
= (void *)0x7fffffff0000;
136 static void *address_space_start
= (void *)0x10000;
138 UINT_PTR page_size
= 0;
139 static UINT page_shift
;
140 static UINT_PTR page_mask
;
141 static void *address_space_limit
;
142 static void *user_space_limit
;
143 static void *working_set_limit
;
144 static void *address_space_start
= (void *)0x10000;
145 #endif /* __i386__ */
146 static const BOOL is_win64
= (sizeof(void *) > sizeof(int));
148 #define ROUND_ADDR(addr,mask) \
149 ((void *)((UINT_PTR)(addr) & ~(UINT_PTR)(mask)))
151 #define ROUND_SIZE(addr,size) \
152 (((SIZE_T)(size) + ((UINT_PTR)(addr) & page_mask) + page_mask) & ~page_mask)
154 #define VIRTUAL_DEBUG_DUMP_VIEW(view) \
155 do { if (TRACE_ON(virtual)) VIRTUAL_DumpView(view); } while (0)
157 #ifdef _WIN64 /* on 64-bit the page protection bytes use a 2-level table */
158 static const size_t pages_vprot_shift
= 20;
159 static const size_t pages_vprot_mask
= (1 << 20) - 1;
160 static size_t pages_vprot_size
;
161 static BYTE
**pages_vprot
;
162 #else /* on 32-bit we use a simple array with one byte per page */
163 static BYTE
*pages_vprot
;
166 static struct file_view
*view_block_start
, *view_block_end
, *next_free_view
;
167 static const size_t view_block_size
= 0x100000;
168 static void *preload_reserve_start
;
169 static void *preload_reserve_end
;
170 static BOOL use_locks
;
171 static BOOL force_exec_prot
; /* whether to force PROT_EXEC on all PROT_READ mmaps */
173 static inline int is_view_valloc( const struct file_view
*view
)
175 return !(view
->protect
& (SEC_FILE
| SEC_RESERVE
| SEC_COMMIT
));
178 /***********************************************************************
181 * Return the page protection byte.
183 static BYTE
get_page_vprot( const void *addr
)
185 size_t idx
= (size_t)addr
>> page_shift
;
188 if ((idx
>> pages_vprot_shift
) >= pages_vprot_size
) return 0;
189 if (!pages_vprot
[idx
>> pages_vprot_shift
]) return 0;
190 return pages_vprot
[idx
>> pages_vprot_shift
][idx
& pages_vprot_mask
];
192 return pages_vprot
[idx
];
197 /***********************************************************************
200 * Set a range of page protection bytes.
202 static void set_page_vprot( const void *addr
, size_t size
, BYTE vprot
)
204 size_t idx
= (size_t)addr
>> page_shift
;
205 size_t end
= ((size_t)addr
+ size
+ page_mask
) >> page_shift
;
208 while (idx
>> pages_vprot_shift
!= end
>> pages_vprot_shift
)
210 size_t dir_size
= pages_vprot_mask
+ 1 - (idx
& pages_vprot_mask
);
211 memset( pages_vprot
[idx
>> pages_vprot_shift
] + (idx
& pages_vprot_mask
), vprot
, dir_size
);
214 memset( pages_vprot
[idx
>> pages_vprot_shift
] + (idx
& pages_vprot_mask
), vprot
, end
- idx
);
216 memset( pages_vprot
+ idx
, vprot
, end
- idx
);
221 /***********************************************************************
222 * set_page_vprot_bits
224 * Set or clear bits in a range of page protection bytes.
226 static void set_page_vprot_bits( const void *addr
, size_t size
, BYTE set
, BYTE clear
)
228 size_t idx
= (size_t)addr
>> page_shift
;
229 size_t end
= ((size_t)addr
+ size
+ page_mask
) >> page_shift
;
232 for ( ; idx
< end
; idx
++)
234 BYTE
*ptr
= pages_vprot
[idx
>> pages_vprot_shift
] + (idx
& pages_vprot_mask
);
235 *ptr
= (*ptr
& ~clear
) | set
;
238 for ( ; idx
< end
; idx
++) pages_vprot
[idx
] = (pages_vprot
[idx
] & ~clear
) | set
;
243 /***********************************************************************
246 * Allocate the page protection bytes for a given range.
248 static BOOL
alloc_pages_vprot( const void *addr
, size_t size
)
251 size_t idx
= (size_t)addr
>> page_shift
;
252 size_t end
= ((size_t)addr
+ size
+ page_mask
) >> page_shift
;
256 assert( end
<= pages_vprot_size
<< pages_vprot_shift
);
257 for (i
= idx
>> pages_vprot_shift
; i
< (end
+ pages_vprot_mask
) >> pages_vprot_shift
; i
++)
259 if (pages_vprot
[i
]) continue;
260 if ((ptr
= wine_anon_mmap( NULL
, pages_vprot_mask
+ 1, PROT_READ
| PROT_WRITE
, 0 )) == (void *)-1)
262 pages_vprot
[i
] = ptr
;
269 /***********************************************************************
272 * View comparison function used for the rb tree.
274 static int compare_view( const void *addr
, const struct wine_rb_entry
*entry
)
276 struct file_view
*view
= WINE_RB_ENTRY_VALUE( entry
, struct file_view
, entry
);
278 if (addr
< view
->base
) return -1;
279 if (addr
> view
->base
) return 1;
284 /***********************************************************************
287 static const char *VIRTUAL_GetProtStr( BYTE prot
)
289 static char buffer
[6];
290 buffer
[0] = (prot
& VPROT_COMMITTED
) ? 'c' : '-';
291 buffer
[1] = (prot
& VPROT_GUARD
) ? 'g' : ((prot
& VPROT_WRITEWATCH
) ? 'H' : '-');
292 buffer
[2] = (prot
& VPROT_READ
) ? 'r' : '-';
293 buffer
[3] = (prot
& VPROT_WRITECOPY
) ? 'W' : ((prot
& VPROT_WRITE
) ? 'w' : '-');
294 buffer
[4] = (prot
& VPROT_EXEC
) ? 'x' : '-';
300 /***********************************************************************
301 * VIRTUAL_GetUnixProt
303 * Convert page protections to protection for mmap/mprotect.
305 static int VIRTUAL_GetUnixProt( BYTE vprot
)
308 if ((vprot
& VPROT_COMMITTED
) && !(vprot
& VPROT_GUARD
))
310 if (vprot
& VPROT_READ
) prot
|= PROT_READ
;
311 if (vprot
& VPROT_WRITE
) prot
|= PROT_WRITE
| PROT_READ
;
312 if (vprot
& VPROT_WRITECOPY
) prot
|= PROT_WRITE
| PROT_READ
;
313 if (vprot
& VPROT_EXEC
) prot
|= PROT_EXEC
| PROT_READ
;
314 if (vprot
& VPROT_WRITEWATCH
) prot
&= ~PROT_WRITE
;
316 if (!prot
) prot
= PROT_NONE
;
321 /***********************************************************************
324 static void VIRTUAL_DumpView( struct file_view
*view
)
327 char *addr
= view
->base
;
328 BYTE prot
= get_page_vprot( addr
);
330 TRACE( "View: %p - %p", addr
, addr
+ view
->size
- 1 );
331 if (view
->protect
& VPROT_SYSTEM
)
332 TRACE( " (builtin image)\n" );
333 else if (view
->protect
& SEC_IMAGE
)
334 TRACE( " (image)\n" );
335 else if (view
->protect
& SEC_FILE
)
336 TRACE( " (file)\n" );
337 else if (view
->protect
& (SEC_RESERVE
| SEC_COMMIT
))
338 TRACE( " (anonymous)\n" );
340 TRACE( " (valloc)\n");
342 for (count
= i
= 1; i
< view
->size
>> page_shift
; i
++, count
++)
344 BYTE next
= get_page_vprot( addr
+ (count
<< page_shift
) );
345 if (next
== prot
) continue;
346 TRACE( " %p - %p %s\n",
347 addr
, addr
+ (count
<< page_shift
) - 1, VIRTUAL_GetProtStr(prot
) );
348 addr
+= (count
<< page_shift
);
353 TRACE( " %p - %p %s\n",
354 addr
, addr
+ (count
<< page_shift
) - 1, VIRTUAL_GetProtStr(prot
) );
358 /***********************************************************************
362 static void VIRTUAL_Dump(void)
365 struct file_view
*view
;
367 TRACE( "Dump of all virtual memory views:\n" );
368 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
369 WINE_RB_FOR_EACH_ENTRY( view
, &views_tree
, struct file_view
, entry
)
371 VIRTUAL_DumpView( view
);
373 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
378 /***********************************************************************
381 * Find the view containing a given address. The csVirtual section must be held by caller.
390 static struct file_view
*VIRTUAL_FindView( const void *addr
, size_t size
)
392 struct wine_rb_entry
*ptr
= views_tree
.root
;
394 if ((const char *)addr
+ size
< (const char *)addr
) return NULL
; /* overflow */
398 struct file_view
*view
= WINE_RB_ENTRY_VALUE( ptr
, struct file_view
, entry
);
400 if (view
->base
> addr
) ptr
= ptr
->left
;
401 else if ((const char *)view
->base
+ view
->size
<= (const char *)addr
) ptr
= ptr
->right
;
402 else if ((const char *)view
->base
+ view
->size
< (const char *)addr
+ size
) break; /* size too large */
409 /***********************************************************************
412 static inline UINT_PTR
get_mask( ULONG zero_bits
)
414 if (!zero_bits
) return 0xffff; /* allocations are aligned to 64K by default */
415 if (zero_bits
< page_shift
) zero_bits
= page_shift
;
416 if (zero_bits
> 21) return 0;
417 return (1 << zero_bits
) - 1;
421 /***********************************************************************
422 * is_write_watch_range
424 static inline BOOL
is_write_watch_range( const void *addr
, size_t size
)
426 struct file_view
*view
= VIRTUAL_FindView( addr
, size
);
427 return view
&& (view
->protect
& VPROT_WRITEWATCH
);
431 /***********************************************************************
434 * Find the first view overlapping at least part of the specified range.
435 * The csVirtual section must be held by caller.
437 static struct file_view
*find_view_range( const void *addr
, size_t size
)
439 struct wine_rb_entry
*ptr
= views_tree
.root
;
443 struct file_view
*view
= WINE_RB_ENTRY_VALUE( ptr
, struct file_view
, entry
);
445 if ((const char *)view
->base
>= (const char *)addr
+ size
) ptr
= ptr
->left
;
446 else if ((const char *)view
->base
+ view
->size
<= (const char *)addr
) ptr
= ptr
->right
;
453 /***********************************************************************
456 * Find a free area between views inside the specified range.
457 * The csVirtual section must be held by caller.
459 static void *find_free_area( void *base
, void *end
, size_t size
, size_t mask
, int top_down
)
461 struct wine_rb_entry
*first
= NULL
, *ptr
= views_tree
.root
;
464 /* find the first (resp. last) view inside the range */
467 struct file_view
*view
= WINE_RB_ENTRY_VALUE( ptr
, struct file_view
, entry
);
468 if ((char *)view
->base
+ view
->size
>= (char *)end
)
470 end
= min( end
, view
->base
);
473 else if (view
->base
<= base
)
475 base
= max( (char *)base
, (char *)view
->base
+ view
->size
);
481 ptr
= top_down
? ptr
->right
: ptr
->left
;
487 start
= ROUND_ADDR( (char *)end
- size
, mask
);
488 if (start
>= end
|| start
< base
) return NULL
;
492 struct file_view
*view
= WINE_RB_ENTRY_VALUE( first
, struct file_view
, entry
);
494 if ((char *)view
->base
+ view
->size
<= (char *)start
) break;
495 start
= ROUND_ADDR( (char *)view
->base
- size
, mask
);
496 /* stop if remaining space is not large enough */
497 if (!start
|| start
>= end
|| start
< base
) return NULL
;
498 first
= wine_rb_prev( first
);
503 start
= ROUND_ADDR( (char *)base
+ mask
, mask
);
504 if (!start
|| start
>= end
|| (char *)end
- (char *)start
< size
) return NULL
;
508 struct file_view
*view
= WINE_RB_ENTRY_VALUE( first
, struct file_view
, entry
);
510 if ((char *)view
->base
>= (char *)start
+ size
) break;
511 start
= ROUND_ADDR( (char *)view
->base
+ view
->size
+ mask
, mask
);
512 /* stop if remaining space is not large enough */
513 if (!start
|| start
>= end
|| (char *)end
- (char *)start
< size
) return NULL
;
514 first
= wine_rb_next( first
);
521 /***********************************************************************
524 * Add a reserved area to the list maintained by libwine.
525 * The csVirtual section must be held by caller.
527 static void add_reserved_area( void *addr
, size_t size
)
529 TRACE( "adding %p-%p\n", addr
, (char *)addr
+ size
);
531 if (addr
< user_space_limit
)
533 /* unmap the part of the area that is below the limit */
534 assert( (char *)addr
+ size
> (char *)user_space_limit
);
535 munmap( addr
, (char *)user_space_limit
- (char *)addr
);
536 size
-= (char *)user_space_limit
- (char *)addr
;
537 addr
= user_space_limit
;
539 /* blow away existing mappings */
540 wine_anon_mmap( addr
, size
, PROT_NONE
, MAP_NORESERVE
| MAP_FIXED
);
541 wine_mmap_add_reserved_area( addr
, size
);
545 /***********************************************************************
546 * remove_reserved_area
548 * Remove a reserved area from the list maintained by libwine.
549 * The csVirtual section must be held by caller.
551 static void remove_reserved_area( void *addr
, size_t size
)
553 struct file_view
*view
;
555 TRACE( "removing %p-%p\n", addr
, (char *)addr
+ size
);
556 wine_mmap_remove_reserved_area( addr
, size
, 0 );
558 /* unmap areas not covered by an existing view */
559 WINE_RB_FOR_EACH_ENTRY( view
, &views_tree
, struct file_view
, entry
)
561 if ((char *)view
->base
>= (char *)addr
+ size
) break;
562 if ((char *)view
->base
+ view
->size
<= (char *)addr
) continue;
563 if (view
->base
> addr
) munmap( addr
, (char *)view
->base
- (char *)addr
);
564 if ((char *)view
->base
+ view
->size
> (char *)addr
+ size
) return;
565 size
= (char *)addr
+ size
- ((char *)view
->base
+ view
->size
);
566 addr
= (char *)view
->base
+ view
->size
;
568 munmap( addr
, size
);
579 /***********************************************************************
580 * get_area_boundary_callback
582 * Get lowest boundary address between reserved area and non-reserved area
583 * in the specified region. If no boundaries are found, result is NULL.
584 * The csVirtual section must be held by caller.
586 static int get_area_boundary_callback( void *start
, size_t size
, void *arg
)
588 struct area_boundary
*area
= arg
;
589 void *end
= (char *)start
+ size
;
591 area
->boundary
= NULL
;
592 if (area
->base
>= end
) return 0;
593 if ((char *)start
>= (char *)area
->base
+ area
->size
) return 1;
594 if (area
->base
>= start
)
596 if ((char *)area
->base
+ area
->size
> (char *)end
)
598 area
->boundary
= end
;
603 area
->boundary
= start
;
608 /***********************************************************************
611 * Check if an address range goes beyond a given limit.
613 static inline BOOL
is_beyond_limit( const void *addr
, size_t size
, const void *limit
)
615 return (addr
>= limit
|| (const char *)addr
+ size
> (const char *)limit
);
619 /***********************************************************************
622 * Unmap an area, or simply replace it by an empty mapping if it is
623 * in a reserved area. The csVirtual section must be held by caller.
625 static inline void unmap_area( void *addr
, size_t size
)
627 switch (wine_mmap_is_in_reserved_area( addr
, size
))
629 case -1: /* partially in a reserved area */
631 struct area_boundary area
;
635 wine_mmap_enum_reserved_areas( get_area_boundary_callback
, &area
, 0 );
636 assert( area
.boundary
);
637 lower_size
= (char *)area
.boundary
- (char *)addr
;
638 unmap_area( addr
, lower_size
);
639 unmap_area( area
.boundary
, size
- lower_size
);
642 case 1: /* in a reserved area */
643 wine_anon_mmap( addr
, size
, PROT_NONE
, MAP_NORESERVE
| MAP_FIXED
);
646 case 0: /* not in a reserved area */
647 if (is_beyond_limit( addr
, size
, user_space_limit
))
648 add_reserved_area( addr
, size
);
650 munmap( addr
, size
);
656 /***********************************************************************
659 * Allocate a new view. The csVirtual section must be held by caller.
661 static struct file_view
*alloc_view(void)
665 struct file_view
*ret
= next_free_view
;
666 next_free_view
= *(struct file_view
**)ret
;
669 if (view_block_start
== view_block_end
)
671 void *ptr
= wine_anon_mmap( NULL
, view_block_size
, PROT_READ
| PROT_WRITE
, 0 );
672 if (ptr
== (void *)-1) return NULL
;
673 view_block_start
= ptr
;
674 view_block_end
= view_block_start
+ view_block_size
/ sizeof(*view_block_start
);
676 return view_block_start
++;
680 /***********************************************************************
683 * Deletes a view. The csVirtual section must be held by caller.
685 static void delete_view( struct file_view
*view
) /* [in] View */
687 if (!(view
->protect
& VPROT_SYSTEM
)) unmap_area( view
->base
, view
->size
);
688 set_page_vprot( view
->base
, view
->size
, 0 );
689 wine_rb_remove( &views_tree
, &view
->entry
);
690 *(struct file_view
**)view
= next_free_view
;
691 next_free_view
= view
;
695 /***********************************************************************
698 * Create a view. The csVirtual section must be held by caller.
700 static NTSTATUS
create_view( struct file_view
**view_ret
, void *base
, size_t size
, unsigned int vprot
)
702 struct file_view
*view
;
703 int unix_prot
= VIRTUAL_GetUnixProt( vprot
);
705 assert( !((UINT_PTR
)base
& page_mask
) );
706 assert( !(size
& page_mask
) );
708 /* Check for overlapping views. This can happen if the previous view
709 * was a system view that got unmapped behind our back. In that case
710 * we recover by simply deleting it. */
712 while ((view
= find_view_range( base
, size
)))
714 TRACE( "overlapping view %p-%p for %p-%p\n",
715 view
->base
, (char *)view
->base
+ view
->size
, base
, (char *)base
+ size
);
716 assert( view
->protect
& VPROT_SYSTEM
);
720 if (!alloc_pages_vprot( base
, size
)) return STATUS_NO_MEMORY
;
722 /* Create the view structure */
724 if (!(view
= alloc_view()))
726 FIXME( "out of memory for %p-%p\n", base
, (char *)base
+ size
);
727 return STATUS_NO_MEMORY
;
732 view
->protect
= vprot
;
733 set_page_vprot( base
, size
, vprot
);
735 wine_rb_put( &views_tree
, view
->base
, &view
->entry
);
739 if (force_exec_prot
&& (unix_prot
& PROT_READ
) && !(unix_prot
& PROT_EXEC
))
741 TRACE( "forcing exec permission on %p-%p\n", base
, (char *)base
+ size
- 1 );
742 mprotect( base
, size
, unix_prot
| PROT_EXEC
);
744 return STATUS_SUCCESS
;
748 /***********************************************************************
749 * VIRTUAL_GetWin32Prot
751 * Convert page protections to Win32 flags.
753 static DWORD
VIRTUAL_GetWin32Prot( BYTE vprot
, unsigned int map_prot
)
755 DWORD ret
= VIRTUAL_Win32Flags
[vprot
& 0x0f];
756 if (vprot
& VPROT_GUARD
) ret
|= PAGE_GUARD
;
757 if (map_prot
& SEC_NOCACHE
) ret
|= PAGE_NOCACHE
;
762 /***********************************************************************
765 * Build page protections from Win32 flags.
768 * protect [I] Win32 protection flags
771 * Value of page protection flags
773 static NTSTATUS
get_vprot_flags( DWORD protect
, unsigned int *vprot
, BOOL image
)
775 switch(protect
& 0xff)
782 *vprot
= VPROT_READ
| VPROT_WRITECOPY
;
784 *vprot
= VPROT_READ
| VPROT_WRITE
;
787 *vprot
= VPROT_READ
| VPROT_WRITECOPY
;
792 case PAGE_EXECUTE_READ
:
793 *vprot
= VPROT_EXEC
| VPROT_READ
;
795 case PAGE_EXECUTE_READWRITE
:
797 *vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITECOPY
;
799 *vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITE
;
801 case PAGE_EXECUTE_WRITECOPY
:
802 *vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITECOPY
;
808 return STATUS_INVALID_PAGE_PROTECTION
;
810 if (protect
& PAGE_GUARD
) *vprot
|= VPROT_GUARD
;
811 return STATUS_SUCCESS
;
815 /***********************************************************************
818 * Wrapper for mprotect, adds PROT_EXEC if forced by force_exec_prot
820 static inline int mprotect_exec( void *base
, size_t size
, int unix_prot
)
822 if (force_exec_prot
&& (unix_prot
& PROT_READ
) && !(unix_prot
& PROT_EXEC
))
824 TRACE( "forcing exec permission on %p-%p\n", base
, (char *)base
+ size
- 1 );
825 if (!mprotect( base
, size
, unix_prot
| PROT_EXEC
)) return 0;
826 /* exec + write may legitimately fail, in that case fall back to write only */
827 if (!(unix_prot
& PROT_WRITE
)) return -1;
830 return mprotect( base
, size
, unix_prot
);
834 /***********************************************************************
837 * Call mprotect on a page range, applying the protections from the per-page byte.
839 static void mprotect_range( void *base
, size_t size
, BYTE set
, BYTE clear
)
842 char *addr
= ROUND_ADDR( base
, page_mask
);
845 size
= ROUND_SIZE( base
, size
);
846 prot
= VIRTUAL_GetUnixProt( (get_page_vprot( addr
) & ~clear
) | set
);
847 for (count
= i
= 1; i
< size
>> page_shift
; i
++, count
++)
849 next
= VIRTUAL_GetUnixProt( (get_page_vprot( addr
+ (count
<< page_shift
) ) & ~clear
) | set
);
850 if (next
== prot
) continue;
851 mprotect_exec( addr
, count
<< page_shift
, prot
);
852 addr
+= count
<< page_shift
;
856 if (count
) mprotect_exec( addr
, count
<< page_shift
, prot
);
860 /***********************************************************************
863 * Change the protection of a range of pages.
869 static BOOL
VIRTUAL_SetProt( struct file_view
*view
, /* [in] Pointer to view */
870 void *base
, /* [in] Starting address */
871 size_t size
, /* [in] Size in bytes */
872 BYTE vprot
) /* [in] Protections to use */
874 int unix_prot
= VIRTUAL_GetUnixProt(vprot
);
876 if (view
->protect
& VPROT_WRITEWATCH
)
878 /* each page may need different protections depending on write watch flag */
879 set_page_vprot_bits( base
, size
, vprot
& ~VPROT_WRITEWATCH
, ~vprot
& ~VPROT_WRITEWATCH
);
880 mprotect_range( base
, size
, 0, 0 );
884 /* if setting stack guard pages, store the permissions first, as the guard may be
885 * triggered at any point after mprotect and change the permissions again */
886 if ((vprot
& VPROT_GUARD
) &&
887 (base
>= NtCurrentTeb()->DeallocationStack
) &&
888 (base
< NtCurrentTeb()->Tib
.StackBase
))
890 set_page_vprot( base
, size
, vprot
);
891 mprotect( base
, size
, unix_prot
);
895 if (mprotect_exec( base
, size
, unix_prot
)) /* FIXME: last error */
898 set_page_vprot( base
, size
, vprot
);
903 /***********************************************************************
906 * Set page protections on a range of pages
908 static NTSTATUS
set_protection( struct file_view
*view
, void *base
, SIZE_T size
, ULONG protect
)
913 if ((status
= get_vprot_flags( protect
, &vprot
, view
->protect
& SEC_IMAGE
))) return status
;
914 if (is_view_valloc( view
))
916 if (vprot
& VPROT_WRITECOPY
) return STATUS_INVALID_PAGE_PROTECTION
;
920 BYTE access
= vprot
& (VPROT_READ
| VPROT_WRITE
| VPROT_EXEC
);
921 if ((view
->protect
& access
) != access
) return STATUS_INVALID_PAGE_PROTECTION
;
924 if (!VIRTUAL_SetProt( view
, base
, size
, vprot
| VPROT_COMMITTED
)) return STATUS_ACCESS_DENIED
;
925 return STATUS_SUCCESS
;
929 /***********************************************************************
930 * update_write_watches
932 static void update_write_watches( void *base
, size_t size
, size_t accessed_size
)
934 TRACE( "updating watch %p-%p-%p\n", base
, (char *)base
+ accessed_size
, (char *)base
+ size
);
935 /* clear write watch flag on accessed pages */
936 set_page_vprot_bits( base
, accessed_size
, 0, VPROT_WRITEWATCH
);
937 /* restore page protections on the entire range */
938 mprotect_range( base
, size
, 0, 0 );
942 /***********************************************************************
943 * reset_write_watches
945 * Reset write watches in a memory range.
947 static void reset_write_watches( void *base
, SIZE_T size
)
949 set_page_vprot_bits( base
, size
, VPROT_WRITEWATCH
, 0 );
950 mprotect_range( base
, size
, 0, 0 );
954 /***********************************************************************
957 * Release the extra memory while keeping the range starting on the granularity boundary.
959 static inline void *unmap_extra_space( void *ptr
, size_t total_size
, size_t wanted_size
, size_t mask
)
961 if ((ULONG_PTR
)ptr
& mask
)
963 size_t extra
= mask
+ 1 - ((ULONG_PTR
)ptr
& mask
);
964 munmap( ptr
, extra
);
965 ptr
= (char *)ptr
+ extra
;
968 if (total_size
> wanted_size
)
969 munmap( (char *)ptr
+ wanted_size
, total_size
- wanted_size
);
983 /***********************************************************************
984 * alloc_reserved_area_callback
986 * Try to map some space inside a reserved area. Callback for wine_mmap_enum_reserved_areas.
988 static int alloc_reserved_area_callback( void *start
, size_t size
, void *arg
)
990 struct alloc_area
*alloc
= arg
;
991 void *end
= (char *)start
+ size
;
993 if (start
< address_space_start
) start
= address_space_start
;
994 if (is_beyond_limit( start
, size
, alloc
->limit
)) end
= alloc
->limit
;
995 if (start
>= end
) return 0;
997 /* make sure we don't touch the preloader reserved range */
998 if (preload_reserve_end
>= start
)
1000 if (preload_reserve_end
>= end
)
1002 if (preload_reserve_start
<= start
) return 0; /* no space in that area */
1003 if (preload_reserve_start
< end
) end
= preload_reserve_start
;
1005 else if (preload_reserve_start
<= start
) start
= preload_reserve_end
;
1008 /* range is split in two by the preloader reservation, try first part */
1009 if ((alloc
->result
= find_free_area( start
, preload_reserve_start
, alloc
->size
,
1010 alloc
->mask
, alloc
->top_down
)))
1012 /* then fall through to try second part */
1013 start
= preload_reserve_end
;
1016 if ((alloc
->result
= find_free_area( start
, end
, alloc
->size
, alloc
->mask
, alloc
->top_down
)))
1022 /***********************************************************************
1025 * mmap the fixed memory area.
1026 * The csVirtual section must be held by caller.
1028 static NTSTATUS
map_fixed_area( void *base
, size_t size
, unsigned int vprot
)
1032 switch (wine_mmap_is_in_reserved_area( base
, size
))
1034 case -1: /* partially in a reserved area */
1037 struct area_boundary area
;
1041 wine_mmap_enum_reserved_areas( get_area_boundary_callback
, &area
, 0 );
1042 assert( area
.boundary
);
1043 lower_size
= (char *)area
.boundary
- (char *)base
;
1044 status
= map_fixed_area( base
, lower_size
, vprot
);
1045 if (status
== STATUS_SUCCESS
)
1047 status
= map_fixed_area( area
.boundary
, size
- lower_size
, vprot
);
1048 if (status
!= STATUS_SUCCESS
) unmap_area( base
, lower_size
);
1052 case 0: /* not in a reserved area, do a normal allocation */
1053 if ((ptr
= wine_anon_mmap( base
, size
, VIRTUAL_GetUnixProt(vprot
), 0 )) == (void *)-1)
1055 if (errno
== ENOMEM
) return STATUS_NO_MEMORY
;
1056 return STATUS_INVALID_PARAMETER
;
1060 /* We couldn't get the address we wanted */
1061 if (is_beyond_limit( ptr
, size
, user_space_limit
)) add_reserved_area( ptr
, size
);
1062 else munmap( ptr
, size
);
1063 return STATUS_CONFLICTING_ADDRESSES
;
1068 case 1: /* in a reserved area, make sure the address is available */
1069 if (find_view_range( base
, size
)) return STATUS_CONFLICTING_ADDRESSES
;
1070 /* replace the reserved area by our mapping */
1071 if ((ptr
= wine_anon_mmap( base
, size
, VIRTUAL_GetUnixProt(vprot
), MAP_FIXED
)) != base
)
1072 return STATUS_INVALID_PARAMETER
;
1075 if (is_beyond_limit( ptr
, size
, working_set_limit
)) working_set_limit
= address_space_limit
;
1076 return STATUS_SUCCESS
;
1079 /***********************************************************************
1082 * Create a view and mmap the corresponding memory area.
1083 * The csVirtual section must be held by caller.
1085 static NTSTATUS
map_view( struct file_view
**view_ret
, void *base
, size_t size
, size_t mask
,
1086 int top_down
, unsigned int vprot
)
1093 if (is_beyond_limit( base
, size
, address_space_limit
))
1094 return STATUS_WORKING_SET_LIMIT_RANGE
;
1095 status
= map_fixed_area( base
, size
, vprot
);
1096 if (status
!= STATUS_SUCCESS
) return status
;
1101 size_t view_size
= size
+ mask
+ 1;
1102 struct alloc_area alloc
;
1106 alloc
.top_down
= top_down
;
1107 alloc
.limit
= user_space_limit
;
1108 if (wine_mmap_enum_reserved_areas( alloc_reserved_area_callback
, &alloc
, top_down
))
1111 TRACE( "got mem in reserved area %p-%p\n", ptr
, (char *)ptr
+ size
);
1112 if (wine_anon_mmap( ptr
, size
, VIRTUAL_GetUnixProt(vprot
), MAP_FIXED
) != ptr
)
1113 return STATUS_INVALID_PARAMETER
;
1119 if ((ptr
= wine_anon_mmap( NULL
, view_size
, VIRTUAL_GetUnixProt(vprot
), 0 )) == (void *)-1)
1121 if (errno
== ENOMEM
) return STATUS_NO_MEMORY
;
1122 return STATUS_INVALID_PARAMETER
;
1124 TRACE( "got mem with anon mmap %p-%p\n", ptr
, (char *)ptr
+ size
);
1125 /* if we got something beyond the user limit, unmap it and retry */
1126 if (is_beyond_limit( ptr
, view_size
, user_space_limit
)) add_reserved_area( ptr
, view_size
);
1129 ptr
= unmap_extra_space( ptr
, view_size
, size
, mask
);
1132 status
= create_view( view_ret
, ptr
, size
, vprot
);
1133 if (status
!= STATUS_SUCCESS
) unmap_area( ptr
, size
);
1138 /***********************************************************************
1139 * map_file_into_view
1141 * Wrapper for mmap() to map a file into a view, falling back to read if mmap fails.
1142 * The csVirtual section must be held by caller.
1144 static NTSTATUS
map_file_into_view( struct file_view
*view
, int fd
, size_t start
, size_t size
,
1145 off_t offset
, unsigned int vprot
, BOOL removable
)
1148 int prot
= VIRTUAL_GetUnixProt( vprot
| VPROT_COMMITTED
/* make sure it is accessible */ );
1149 unsigned int flags
= MAP_FIXED
| ((vprot
& VPROT_WRITECOPY
) ? MAP_PRIVATE
: MAP_SHARED
);
1151 assert( start
< view
->size
);
1152 assert( start
+ size
<= view
->size
);
1154 if (force_exec_prot
&& (vprot
& VPROT_READ
))
1156 TRACE( "forcing exec permission on mapping %p-%p\n",
1157 (char *)view
->base
+ start
, (char *)view
->base
+ start
+ size
- 1 );
1161 /* only try mmap if media is not removable (or if we require write access) */
1162 if (!removable
|| (flags
& MAP_SHARED
))
1164 if (mmap( (char *)view
->base
+ start
, size
, prot
, flags
, fd
, offset
) != (void *)-1)
1167 if ((errno
== EPERM
) && (prot
& PROT_EXEC
))
1168 ERR( "failed to set %08x protection on file map, noexec filesystem?\n", prot
);
1170 /* mmap() failed; if this is because the file offset is not */
1171 /* page-aligned (EINVAL), or because the underlying filesystem */
1172 /* does not support mmap() (ENOEXEC,ENODEV), we do it by hand. */
1173 if ((errno
!= ENOEXEC
) && (errno
!= EINVAL
) && (errno
!= ENODEV
)) return FILE_GetNtStatus();
1174 if (flags
& MAP_SHARED
) /* we cannot fake shared mappings */
1176 if (errno
== EINVAL
) return STATUS_INVALID_PARAMETER
;
1177 ERR( "shared writable mmap not supported, broken filesystem?\n" );
1178 return STATUS_NOT_SUPPORTED
;
1182 /* Reserve the memory with an anonymous mmap */
1183 ptr
= wine_anon_mmap( (char *)view
->base
+ start
, size
, PROT_READ
| PROT_WRITE
, MAP_FIXED
);
1184 if (ptr
== (void *)-1) return FILE_GetNtStatus();
1185 /* Now read in the file */
1186 pread( fd
, ptr
, size
, offset
);
1187 if (prot
!= (PROT_READ
|PROT_WRITE
)) mprotect( ptr
, size
, prot
); /* Set the right protection */
1189 set_page_vprot( (char *)view
->base
+ start
, size
, vprot
);
1190 return STATUS_SUCCESS
;
1194 /***********************************************************************
1195 * get_committed_size
1197 * Get the size of the committed range starting at base.
1198 * Also return the protections for the first page.
1200 static SIZE_T
get_committed_size( struct file_view
*view
, void *base
, BYTE
*vprot
)
1204 start
= ((char *)base
- (char *)view
->base
) >> page_shift
;
1205 *vprot
= get_page_vprot( base
);
1207 if (view
->protect
& SEC_RESERVE
)
1210 SERVER_START_REQ( get_mapping_committed_range
)
1212 req
->base
= wine_server_client_ptr( view
->base
);
1213 req
->offset
= start
<< page_shift
;
1214 if (!wine_server_call( req
))
1217 if (reply
->committed
)
1219 *vprot
|= VPROT_COMMITTED
;
1220 set_page_vprot_bits( base
, ret
, VPROT_COMMITTED
, 0 );
1227 for (i
= start
+ 1; i
< view
->size
>> page_shift
; i
++)
1228 if ((*vprot
^ get_page_vprot( (char *)view
->base
+ (i
<< page_shift
) )) & VPROT_COMMITTED
) break;
1229 return (i
- start
) << page_shift
;
1233 /***********************************************************************
1236 * Decommit some pages of a given view.
1237 * The csVirtual section must be held by caller.
1239 static NTSTATUS
decommit_pages( struct file_view
*view
, size_t start
, size_t size
)
1241 if (wine_anon_mmap( (char *)view
->base
+ start
, size
, PROT_NONE
, MAP_FIXED
) != (void *)-1)
1243 set_page_vprot_bits( (char *)view
->base
+ start
, size
, 0, VPROT_COMMITTED
);
1244 return STATUS_SUCCESS
;
1246 return FILE_GetNtStatus();
1250 /***********************************************************************
1251 * allocate_dos_memory
1253 * Allocate the DOS memory range.
1255 static NTSTATUS
allocate_dos_memory( struct file_view
**view
, unsigned int vprot
)
1259 void * const low_64k
= (void *)0x10000;
1260 const size_t dosmem_size
= 0x110000;
1261 int unix_prot
= VIRTUAL_GetUnixProt( vprot
);
1263 /* check for existing view */
1265 if (find_view_range( 0, dosmem_size
)) return STATUS_CONFLICTING_ADDRESSES
;
1267 /* check without the first 64K */
1269 if (wine_mmap_is_in_reserved_area( low_64k
, dosmem_size
- 0x10000 ) != 1)
1271 addr
= wine_anon_mmap( low_64k
, dosmem_size
- 0x10000, unix_prot
, 0 );
1272 if (addr
!= low_64k
)
1274 if (addr
!= (void *)-1) munmap( addr
, dosmem_size
- 0x10000 );
1275 return map_view( view
, NULL
, dosmem_size
, 0xffff, 0, vprot
);
1279 /* now try to allocate the low 64K too */
1281 if (wine_mmap_is_in_reserved_area( NULL
, 0x10000 ) != 1)
1283 addr
= wine_anon_mmap( (void *)page_size
, 0x10000 - page_size
, unix_prot
, 0 );
1284 if (addr
== (void *)page_size
)
1286 if (!wine_anon_mmap( NULL
, page_size
, unix_prot
, MAP_FIXED
))
1289 TRACE( "successfully mapped low 64K range\n" );
1291 else TRACE( "failed to map page 0\n" );
1295 if (addr
!= (void *)-1) munmap( addr
, 0x10000 - page_size
);
1297 TRACE( "failed to map low 64K range\n" );
1301 /* now reserve the whole range */
1303 size
= (char *)dosmem_size
- (char *)addr
;
1304 wine_anon_mmap( addr
, size
, unix_prot
, MAP_FIXED
);
1305 return create_view( view
, addr
, size
, vprot
);
1309 /***********************************************************************
1312 * Map the header of a PE file into memory.
1314 static NTSTATUS
map_pe_header( void *ptr
, size_t size
, int fd
, BOOL
*removable
)
1316 if (!size
) return STATUS_INVALID_IMAGE_FORMAT
;
1320 if (mmap( ptr
, size
, PROT_READ
|PROT_WRITE
|PROT_EXEC
, MAP_FIXED
|MAP_PRIVATE
, fd
, 0 ) != (void *)-1)
1321 return STATUS_SUCCESS
;
1326 WARN( "noexec file system, falling back to read\n" );
1330 WARN( "file system doesn't support mmap, falling back to read\n" );
1333 return FILE_GetNtStatus();
1337 pread( fd
, ptr
, size
, 0 );
1338 return STATUS_SUCCESS
; /* page protections will be updated later */
1342 /***********************************************************************
1345 * Map an executable (PE format) image into memory.
1347 static NTSTATUS
map_image( HANDLE hmapping
, ACCESS_MASK access
, int fd
, char *base
, SIZE_T total_size
,
1348 SIZE_T mask
, SIZE_T header_size
, int shared_fd
, BOOL removable
, PVOID
*addr_ptr
)
1350 IMAGE_DOS_HEADER
*dos
;
1351 IMAGE_NT_HEADERS
*nt
;
1352 IMAGE_SECTION_HEADER sections
[96];
1353 IMAGE_SECTION_HEADER
*sec
;
1354 IMAGE_DATA_DIRECTORY
*imports
;
1355 NTSTATUS status
= STATUS_CONFLICTING_ADDRESSES
;
1360 struct file_view
*view
= NULL
;
1361 char *ptr
, *header_end
, *header_start
;
1363 /* zero-map the whole range */
1365 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1367 if (base
>= (char *)address_space_start
) /* make sure the DOS area remains free */
1368 status
= map_view( &view
, base
, total_size
, mask
, FALSE
, SEC_IMAGE
| SEC_FILE
|
1369 VPROT_COMMITTED
| VPROT_READ
| VPROT_EXEC
| VPROT_WRITECOPY
);
1371 if (status
!= STATUS_SUCCESS
)
1372 status
= map_view( &view
, NULL
, total_size
, mask
, FALSE
, SEC_IMAGE
| SEC_FILE
|
1373 VPROT_COMMITTED
| VPROT_READ
| VPROT_EXEC
| VPROT_WRITECOPY
);
1375 if (status
!= STATUS_SUCCESS
) goto error
;
1378 TRACE_(module
)( "mapped PE file at %p-%p\n", ptr
, ptr
+ total_size
);
1380 /* map the header */
1382 if (fstat( fd
, &st
) == -1)
1384 status
= FILE_GetNtStatus();
1387 header_size
= min( header_size
, st
.st_size
);
1388 if ((status
= map_pe_header( view
->base
, header_size
, fd
, &removable
)) != STATUS_SUCCESS
) goto error
;
1390 status
= STATUS_INVALID_IMAGE_FORMAT
; /* generic error */
1391 dos
= (IMAGE_DOS_HEADER
*)ptr
;
1392 nt
= (IMAGE_NT_HEADERS
*)(ptr
+ dos
->e_lfanew
);
1393 header_end
= ptr
+ ROUND_SIZE( 0, header_size
);
1394 memset( ptr
+ header_size
, 0, header_end
- (ptr
+ header_size
) );
1395 if ((char *)(nt
+ 1) > header_end
) goto error
;
1396 header_start
= (char*)&nt
->OptionalHeader
+nt
->FileHeader
.SizeOfOptionalHeader
;
1397 if (nt
->FileHeader
.NumberOfSections
> sizeof(sections
)/sizeof(*sections
)) goto error
;
1398 if (header_start
+ sizeof(*sections
) * nt
->FileHeader
.NumberOfSections
> header_end
) goto error
;
1399 /* Some applications (e.g. the Steam version of Borderlands) map over the top of the section headers,
1400 * copying the headers into local memory is necessary to properly load such applications. */
1401 memcpy(sections
, header_start
, sizeof(*sections
) * nt
->FileHeader
.NumberOfSections
);
1404 imports
= nt
->OptionalHeader
.DataDirectory
+ IMAGE_DIRECTORY_ENTRY_IMPORT
;
1405 if (!imports
->Size
|| !imports
->VirtualAddress
) imports
= NULL
;
1407 /* check for non page-aligned binary */
1409 if (nt
->OptionalHeader
.SectionAlignment
<= page_mask
)
1411 /* unaligned sections, this happens for native subsystem binaries */
1412 /* in that case Windows simply maps in the whole file */
1414 if (map_file_into_view( view
, fd
, 0, total_size
, 0, VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
,
1415 removable
) != STATUS_SUCCESS
) goto error
;
1417 /* check that all sections are loaded at the right offset */
1418 if (nt
->OptionalHeader
.FileAlignment
!= nt
->OptionalHeader
.SectionAlignment
) goto error
;
1419 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++)
1421 if (sec
[i
].VirtualAddress
!= sec
[i
].PointerToRawData
)
1422 goto error
; /* Windows refuses to load in that case too */
1425 /* set the image protections */
1426 VIRTUAL_SetProt( view
, ptr
, total_size
,
1427 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
| VPROT_EXEC
);
1429 /* no relocations are performed on non page-aligned binaries */
1434 /* map all the sections */
1436 for (i
= pos
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
1438 static const SIZE_T sector_align
= 0x1ff;
1439 SIZE_T map_size
, file_start
, file_size
, end
;
1441 if (!sec
->Misc
.VirtualSize
)
1442 map_size
= ROUND_SIZE( 0, sec
->SizeOfRawData
);
1444 map_size
= ROUND_SIZE( 0, sec
->Misc
.VirtualSize
);
1446 /* file positions are rounded to sector boundaries regardless of OptionalHeader.FileAlignment */
1447 file_start
= sec
->PointerToRawData
& ~sector_align
;
1448 file_size
= (sec
->SizeOfRawData
+ (sec
->PointerToRawData
& sector_align
) + sector_align
) & ~sector_align
;
1449 if (file_size
> map_size
) file_size
= map_size
;
1451 /* a few sanity checks */
1452 end
= sec
->VirtualAddress
+ ROUND_SIZE( sec
->VirtualAddress
, map_size
);
1453 if (sec
->VirtualAddress
> total_size
|| end
> total_size
|| end
< sec
->VirtualAddress
)
1455 WARN_(module
)( "Section %.8s too large (%x+%lx/%lx)\n",
1456 sec
->Name
, sec
->VirtualAddress
, map_size
, total_size
);
1460 if ((sec
->Characteristics
& IMAGE_SCN_MEM_SHARED
) &&
1461 (sec
->Characteristics
& IMAGE_SCN_MEM_WRITE
))
1463 TRACE_(module
)( "mapping shared section %.8s at %p off %x (%x) size %lx (%lx) flags %x\n",
1464 sec
->Name
, ptr
+ sec
->VirtualAddress
,
1465 sec
->PointerToRawData
, (int)pos
, file_size
, map_size
,
1466 sec
->Characteristics
);
1467 if (map_file_into_view( view
, shared_fd
, sec
->VirtualAddress
, map_size
, pos
,
1468 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITE
, FALSE
) != STATUS_SUCCESS
)
1470 ERR_(module
)( "Could not map shared section %.8s\n", sec
->Name
);
1474 /* check if the import directory falls inside this section */
1475 if (imports
&& imports
->VirtualAddress
>= sec
->VirtualAddress
&&
1476 imports
->VirtualAddress
< sec
->VirtualAddress
+ map_size
)
1478 UINT_PTR base
= imports
->VirtualAddress
& ~page_mask
;
1479 UINT_PTR end
= base
+ ROUND_SIZE( imports
->VirtualAddress
, imports
->Size
);
1480 if (end
> sec
->VirtualAddress
+ map_size
) end
= sec
->VirtualAddress
+ map_size
;
1482 map_file_into_view( view
, shared_fd
, base
, end
- base
,
1483 pos
+ (base
- sec
->VirtualAddress
),
1484 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
, FALSE
);
1490 TRACE_(module
)( "mapping section %.8s at %p off %x size %x virt %x flags %x\n",
1491 sec
->Name
, ptr
+ sec
->VirtualAddress
,
1492 sec
->PointerToRawData
, sec
->SizeOfRawData
,
1493 sec
->Misc
.VirtualSize
, sec
->Characteristics
);
1495 if (!sec
->PointerToRawData
|| !file_size
) continue;
1497 /* Note: if the section is not aligned properly map_file_into_view will magically
1498 * fall back to read(), so we don't need to check anything here.
1500 end
= file_start
+ file_size
;
1501 if (sec
->PointerToRawData
>= st
.st_size
||
1502 end
> ((st
.st_size
+ sector_align
) & ~sector_align
) ||
1504 map_file_into_view( view
, fd
, sec
->VirtualAddress
, file_size
, file_start
,
1505 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
,
1506 removable
) != STATUS_SUCCESS
)
1508 ERR_(module
)( "Could not map section %.8s, file probably truncated\n", sec
->Name
);
1512 if (file_size
& page_mask
)
1514 end
= ROUND_SIZE( 0, file_size
);
1515 if (end
> map_size
) end
= map_size
;
1516 TRACE_(module
)("clearing %p - %p\n",
1517 ptr
+ sec
->VirtualAddress
+ file_size
,
1518 ptr
+ sec
->VirtualAddress
+ end
);
1519 memset( ptr
+ sec
->VirtualAddress
+ file_size
, 0, end
- file_size
);
1523 /* set the image protections */
1525 VIRTUAL_SetProt( view
, ptr
, ROUND_SIZE( 0, header_size
), VPROT_COMMITTED
| VPROT_READ
);
1528 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
1531 BYTE vprot
= VPROT_COMMITTED
;
1533 if (sec
->Misc
.VirtualSize
)
1534 size
= ROUND_SIZE( sec
->VirtualAddress
, sec
->Misc
.VirtualSize
);
1536 size
= ROUND_SIZE( sec
->VirtualAddress
, sec
->SizeOfRawData
);
1538 if (sec
->Characteristics
& IMAGE_SCN_MEM_READ
) vprot
|= VPROT_READ
;
1539 if (sec
->Characteristics
& IMAGE_SCN_MEM_WRITE
) vprot
|= VPROT_WRITECOPY
;
1540 if (sec
->Characteristics
& IMAGE_SCN_MEM_EXECUTE
) vprot
|= VPROT_EXEC
;
1542 /* Dumb game crack lets the AOEP point into a data section. Adjust. */
1543 if ((nt
->OptionalHeader
.AddressOfEntryPoint
>= sec
->VirtualAddress
) &&
1544 (nt
->OptionalHeader
.AddressOfEntryPoint
< sec
->VirtualAddress
+ size
))
1545 vprot
|= VPROT_EXEC
;
1547 if (!VIRTUAL_SetProt( view
, ptr
+ sec
->VirtualAddress
, size
, vprot
) && (vprot
& VPROT_EXEC
))
1548 ERR( "failed to set %08x protection on section %.8s, noexec filesystem?\n",
1549 sec
->Characteristics
, sec
->Name
);
1554 SERVER_START_REQ( map_view
)
1556 req
->mapping
= wine_server_obj_handle( hmapping
);
1557 req
->access
= access
;
1558 req
->base
= wine_server_client_ptr( view
->base
);
1559 req
->size
= view
->size
;
1561 status
= wine_server_call( req
);
1564 if (status
) goto error
;
1566 VIRTUAL_DEBUG_DUMP_VIEW( view
);
1567 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1570 #ifdef VALGRIND_LOAD_PDB_DEBUGINFO
1571 VALGRIND_LOAD_PDB_DEBUGINFO(fd
, ptr
, total_size
, ptr
- base
);
1573 if (ptr
!= base
) return STATUS_IMAGE_NOT_AT_BASE
;
1574 return STATUS_SUCCESS
;
1577 if (view
) delete_view( view
);
1578 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1583 struct alloc_virtual_heap
1589 /* callback for wine_mmap_enum_reserved_areas to allocate space for the virtual heap */
1590 static int alloc_virtual_heap( void *base
, size_t size
, void *arg
)
1592 struct alloc_virtual_heap
*alloc
= arg
;
1594 if (is_beyond_limit( base
, size
, address_space_limit
)) address_space_limit
= (char *)base
+ size
;
1595 if (size
< alloc
->size
) return 0;
1596 if (is_win64
&& base
< (void *)0x80000000) return 0;
1597 alloc
->base
= wine_anon_mmap( (char *)base
+ size
- alloc
->size
, alloc
->size
,
1598 PROT_READ
|PROT_WRITE
, MAP_FIXED
);
1599 return (alloc
->base
!= (void *)-1);
1602 /***********************************************************************
1605 void virtual_init(void)
1607 const char *preload
;
1608 struct alloc_virtual_heap alloc_views
;
1611 #if !defined(__i386__) && !defined(__x86_64__)
1612 page_size
= sysconf( _SC_PAGESIZE
);
1613 page_mask
= page_size
- 1;
1614 /* Make sure we have a power of 2 */
1615 assert( !(page_size
& page_mask
) );
1617 while ((1 << page_shift
) != page_size
) page_shift
++;
1619 address_space_limit
= (void *)(((1UL << 47) - 1) & ~page_mask
);
1621 address_space_limit
= (void *)~page_mask
;
1623 user_space_limit
= working_set_limit
= address_space_limit
;
1625 if ((preload
= getenv("WINEPRELOADRESERVE")))
1627 unsigned long start
, end
;
1628 if (sscanf( preload
, "%lx-%lx", &start
, &end
) == 2)
1630 preload_reserve_start
= (void *)start
;
1631 preload_reserve_end
= (void *)end
;
1632 /* some apps start inside the DOS area */
1633 if (preload_reserve_start
)
1634 address_space_start
= min( address_space_start
, preload_reserve_start
);
1638 /* try to find space in a reserved area for the views and pages protection table */
1640 pages_vprot_size
= ((size_t)address_space_limit
>> page_shift
>> pages_vprot_shift
) + 1;
1641 alloc_views
.size
= view_block_size
+ pages_vprot_size
* sizeof(*pages_vprot
);
1643 alloc_views
.size
= view_block_size
+ (1U << (32 - page_shift
));
1645 if (wine_mmap_enum_reserved_areas( alloc_virtual_heap
, &alloc_views
, 1 ))
1646 wine_mmap_remove_reserved_area( alloc_views
.base
, alloc_views
.size
, 0 );
1648 alloc_views
.base
= wine_anon_mmap( NULL
, alloc_views
.size
, PROT_READ
| PROT_WRITE
, 0 );
1650 assert( alloc_views
.base
!= (void *)-1 );
1651 view_block_start
= alloc_views
.base
;
1652 view_block_end
= view_block_start
+ view_block_size
/ sizeof(*view_block_start
);
1653 pages_vprot
= (void *)((char *)alloc_views
.base
+ view_block_size
);
1654 wine_rb_init( &views_tree
, compare_view
);
1656 /* make the DOS area accessible (except the low 64K) to hide bugs in broken apps like Excel 2003 */
1657 size
= (char *)address_space_start
- (char *)0x10000;
1658 if (size
&& wine_mmap_is_in_reserved_area( (void*)0x10000, size
) == 1)
1659 wine_anon_mmap( (void *)0x10000, size
, PROT_READ
| PROT_WRITE
, MAP_FIXED
);
1663 /***********************************************************************
1664 * virtual_init_threading
1666 void virtual_init_threading(void)
1672 /***********************************************************************
1673 * virtual_get_system_info
1675 void virtual_get_system_info( SYSTEM_BASIC_INFORMATION
*info
)
1678 struct sysinfo sinfo
;
1682 info
->KeMaximumIncrement
= 0; /* FIXME */
1683 info
->PageSize
= page_size
;
1684 info
->MmLowestPhysicalPage
= 1;
1685 info
->MmHighestPhysicalPage
= 0x7fffffff / page_size
;
1687 if (!sysinfo(&sinfo
))
1689 ULONG64 total
= (ULONG64
)sinfo
.totalram
* sinfo
.mem_unit
;
1690 info
->MmHighestPhysicalPage
= max(1, total
/ page_size
);
1693 info
->MmNumberOfPhysicalPages
= info
->MmHighestPhysicalPage
- info
->MmLowestPhysicalPage
;
1694 info
->AllocationGranularity
= get_mask(0) + 1;
1695 info
->LowestUserAddress
= (void *)0x10000;
1696 info
->HighestUserAddress
= (char *)user_space_limit
- 1;
1697 info
->ActiveProcessorsAffinityMask
= get_system_affinity_mask();
1698 info
->NumberOfProcessors
= NtCurrentTeb()->Peb
->NumberOfProcessors
;
1702 /***********************************************************************
1703 * virtual_create_builtin_view
1705 NTSTATUS
virtual_create_builtin_view( void *module
)
1709 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( module
);
1710 SIZE_T size
= nt
->OptionalHeader
.SizeOfImage
;
1711 IMAGE_SECTION_HEADER
*sec
;
1712 struct file_view
*view
;
1716 size
= ROUND_SIZE( module
, size
);
1717 base
= ROUND_ADDR( module
, page_mask
);
1718 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1719 status
= create_view( &view
, base
, size
, SEC_IMAGE
| SEC_FILE
| VPROT_SYSTEM
|
1720 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
| VPROT_EXEC
);
1723 TRACE( "created %p-%p\n", base
, (char *)base
+ size
);
1725 /* The PE header is always read-only, no write, no execute. */
1726 set_page_vprot( base
, page_size
, VPROT_COMMITTED
| VPROT_READ
);
1728 sec
= (IMAGE_SECTION_HEADER
*)((char *)&nt
->OptionalHeader
+ nt
->FileHeader
.SizeOfOptionalHeader
);
1729 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++)
1731 BYTE flags
= VPROT_COMMITTED
;
1733 if (sec
[i
].Characteristics
& IMAGE_SCN_MEM_EXECUTE
) flags
|= VPROT_EXEC
;
1734 if (sec
[i
].Characteristics
& IMAGE_SCN_MEM_READ
) flags
|= VPROT_READ
;
1735 if (sec
[i
].Characteristics
& IMAGE_SCN_MEM_WRITE
) flags
|= VPROT_WRITE
;
1736 set_page_vprot( (char *)base
+ sec
[i
].VirtualAddress
, sec
[i
].Misc
.VirtualSize
, flags
);
1738 VIRTUAL_DEBUG_DUMP_VIEW( view
);
1740 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1745 /***********************************************************************
1746 * virtual_alloc_thread_stack
1748 NTSTATUS
virtual_alloc_thread_stack( TEB
*teb
, SIZE_T reserve_size
, SIZE_T commit_size
)
1750 struct file_view
*view
;
1755 if (!reserve_size
|| !commit_size
)
1757 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( NtCurrentTeb()->Peb
->ImageBaseAddress
);
1758 if (!reserve_size
) reserve_size
= nt
->OptionalHeader
.SizeOfStackReserve
;
1759 if (!commit_size
) commit_size
= nt
->OptionalHeader
.SizeOfStackCommit
;
1762 size
= max( reserve_size
, commit_size
);
1763 if (size
< 1024 * 1024) size
= 1024 * 1024; /* Xlib needs a large stack */
1764 size
= (size
+ 0xffff) & ~0xffff; /* round to 64K boundary */
1766 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1768 if ((status
= map_view( &view
, NULL
, size
, 0xffff, 0,
1769 VPROT_READ
| VPROT_WRITE
| VPROT_COMMITTED
)) != STATUS_SUCCESS
)
1772 #ifdef VALGRIND_STACK_REGISTER
1773 VALGRIND_STACK_REGISTER( view
->base
, (char *)view
->base
+ view
->size
);
1776 /* setup no access guard page */
1777 set_page_vprot( view
->base
, page_size
, VPROT_COMMITTED
);
1778 set_page_vprot( (char *)view
->base
+ page_size
, page_size
,
1779 VPROT_READ
| VPROT_WRITE
| VPROT_COMMITTED
| VPROT_GUARD
);
1780 mprotect_range( view
->base
, 2 * page_size
, 0, 0 );
1781 VIRTUAL_DEBUG_DUMP_VIEW( view
);
1783 /* note: limit is lower than base since the stack grows down */
1784 teb
->DeallocationStack
= view
->base
;
1785 teb
->Tib
.StackBase
= (char *)view
->base
+ view
->size
;
1786 teb
->Tib
.StackLimit
= (char *)view
->base
+ 2 * page_size
;
1788 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1793 /***********************************************************************
1794 * virtual_clear_thread_stack
1796 * Clear the stack contents before calling the main entry point, some broken apps need that.
1798 void virtual_clear_thread_stack(void)
1800 void *stack
= NtCurrentTeb()->Tib
.StackLimit
;
1801 size_t size
= (char *)NtCurrentTeb()->Tib
.StackBase
- (char *)NtCurrentTeb()->Tib
.StackLimit
;
1803 wine_anon_mmap( stack
, size
- page_size
, PROT_READ
| PROT_WRITE
, MAP_FIXED
);
1804 if (force_exec_prot
) mprotect( stack
, size
- page_size
, PROT_READ
| PROT_WRITE
| PROT_EXEC
);
1808 /***********************************************************************
1809 * virtual_handle_fault
1811 NTSTATUS
virtual_handle_fault( LPCVOID addr
, DWORD err
, BOOL on_signal_stack
)
1813 NTSTATUS ret
= STATUS_ACCESS_VIOLATION
;
1814 void *page
= ROUND_ADDR( addr
, page_mask
);
1818 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1819 vprot
= get_page_vprot( page
);
1820 if (!on_signal_stack
&& (vprot
& VPROT_GUARD
))
1822 set_page_vprot_bits( page
, page_size
, 0, VPROT_GUARD
);
1823 mprotect_range( page
, page_size
, 0, 0 );
1824 ret
= STATUS_GUARD_PAGE_VIOLATION
;
1826 else if (err
& EXCEPTION_WRITE_FAULT
)
1828 if (vprot
& VPROT_WRITEWATCH
)
1830 set_page_vprot_bits( page
, page_size
, 0, VPROT_WRITEWATCH
);
1831 mprotect_range( page
, page_size
, 0, 0 );
1833 /* ignore fault if page is writable now */
1834 if (VIRTUAL_GetUnixProt( get_page_vprot( page
)) & PROT_WRITE
)
1836 if ((vprot
& VPROT_WRITEWATCH
) || is_write_watch_range( page
, page_size
))
1837 ret
= STATUS_SUCCESS
;
1840 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1845 /***********************************************************************
1846 * check_write_access
1848 * Check if the memory range is writable, temporarily disabling write watches if necessary.
1850 static NTSTATUS
check_write_access( void *base
, size_t size
, BOOL
*has_write_watch
)
1853 char *addr
= ROUND_ADDR( base
, page_mask
);
1855 size
= ROUND_SIZE( base
, size
);
1856 for (i
= 0; i
< size
; i
+= page_size
)
1858 BYTE vprot
= get_page_vprot( addr
+ i
);
1859 if (vprot
& VPROT_WRITEWATCH
) *has_write_watch
= TRUE
;
1860 if (!(VIRTUAL_GetUnixProt( vprot
& ~VPROT_WRITEWATCH
) & PROT_WRITE
))
1861 return STATUS_INVALID_USER_BUFFER
;
1863 if (*has_write_watch
)
1864 mprotect_range( addr
, size
, 0, VPROT_WRITEWATCH
); /* temporarily enable write access */
1865 return STATUS_SUCCESS
;
1869 /***********************************************************************
1870 * virtual_locked_server_call
1872 unsigned int virtual_locked_server_call( void *req_ptr
)
1874 struct __server_request_info
* const req
= req_ptr
;
1876 void *addr
= req
->reply_data
;
1877 data_size_t size
= req
->u
.req
.request_header
.reply_size
;
1878 BOOL has_write_watch
= FALSE
;
1879 unsigned int ret
= STATUS_ACCESS_VIOLATION
;
1881 if (!size
) return wine_server_call( req_ptr
);
1883 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1884 if (!(ret
= check_write_access( addr
, size
, &has_write_watch
)))
1886 ret
= server_call_unlocked( req
);
1887 if (has_write_watch
) update_write_watches( addr
, size
, wine_server_reply_size( req
));
1889 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1894 /***********************************************************************
1895 * virtual_locked_read
1897 ssize_t
virtual_locked_read( int fd
, void *addr
, size_t size
)
1900 BOOL has_write_watch
= FALSE
;
1903 ssize_t ret
= read( fd
, addr
, size
);
1904 if (ret
!= -1 || errno
!= EFAULT
) return ret
;
1906 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1907 if (!check_write_access( addr
, size
, &has_write_watch
))
1909 ret
= read( fd
, addr
, size
);
1911 if (has_write_watch
) update_write_watches( addr
, size
, max( 0, ret
));
1913 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1919 /***********************************************************************
1920 * virtual_locked_pread
1922 ssize_t
virtual_locked_pread( int fd
, void *addr
, size_t size
, off_t offset
)
1925 BOOL has_write_watch
= FALSE
;
1928 ssize_t ret
= pread( fd
, addr
, size
, offset
);
1929 if (ret
!= -1 || errno
!= EFAULT
) return ret
;
1931 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1932 if (!check_write_access( addr
, size
, &has_write_watch
))
1934 ret
= pread( fd
, addr
, size
, offset
);
1936 if (has_write_watch
) update_write_watches( addr
, size
, max( 0, ret
));
1938 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1944 /***********************************************************************
1945 * __wine_locked_recvmsg
1947 ssize_t CDECL
__wine_locked_recvmsg( int fd
, struct msghdr
*hdr
, int flags
)
1951 BOOL has_write_watch
= FALSE
;
1954 ssize_t ret
= recvmsg( fd
, hdr
, flags
);
1955 if (ret
!= -1 || errno
!= EFAULT
) return ret
;
1957 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1958 for (i
= 0; i
< hdr
->msg_iovlen
; i
++)
1959 if (check_write_access( hdr
->msg_iov
[i
].iov_base
, hdr
->msg_iov
[i
].iov_len
, &has_write_watch
))
1961 if (i
== hdr
->msg_iovlen
)
1963 ret
= recvmsg( fd
, hdr
, flags
);
1966 if (has_write_watch
)
1967 while (i
--) update_write_watches( hdr
->msg_iov
[i
].iov_base
, hdr
->msg_iov
[i
].iov_len
, 0 );
1969 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1975 /***********************************************************************
1976 * virtual_is_valid_code_address
1978 BOOL
virtual_is_valid_code_address( const void *addr
, SIZE_T size
)
1980 struct file_view
*view
;
1984 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1985 if ((view
= VIRTUAL_FindView( addr
, size
)))
1986 ret
= !(view
->protect
& VPROT_SYSTEM
); /* system views are not visible to the app */
1987 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1992 /***********************************************************************
1993 * virtual_handle_stack_fault
1995 * Handle an access fault inside the current thread stack.
1996 * Called from inside a signal handler.
1998 BOOL
virtual_handle_stack_fault( void *addr
)
2002 RtlEnterCriticalSection( &csVirtual
); /* no need for signal masking inside signal handler */
2003 if (get_page_vprot( addr
) & VPROT_GUARD
)
2005 char *page
= ROUND_ADDR( addr
, page_mask
);
2006 set_page_vprot_bits( page
, page_size
, 0, VPROT_GUARD
);
2007 mprotect_range( page
, page_size
, 0, 0 );
2008 NtCurrentTeb()->Tib
.StackLimit
= page
;
2009 if (page
>= (char *)NtCurrentTeb()->DeallocationStack
+ 2*page_size
)
2012 set_page_vprot_bits( page
, page_size
, VPROT_COMMITTED
| VPROT_GUARD
, 0 );
2013 mprotect_range( page
, page_size
, 0, 0 );
2017 RtlLeaveCriticalSection( &csVirtual
);
2022 /***********************************************************************
2023 * virtual_check_buffer_for_read
2025 * Check if a memory buffer can be read, triggering page faults if needed for DIB section access.
2027 BOOL
virtual_check_buffer_for_read( const void *ptr
, SIZE_T size
)
2029 if (!size
) return TRUE
;
2030 if (!ptr
) return FALSE
;
2034 volatile const char *p
= ptr
;
2035 char dummy
__attribute__((unused
));
2036 SIZE_T count
= size
;
2038 while (count
> page_size
)
2045 dummy
= p
[count
- 1];
2056 /***********************************************************************
2057 * virtual_check_buffer_for_write
2059 * Check if a memory buffer can be written to, triggering page faults if needed for write watches.
2061 BOOL
virtual_check_buffer_for_write( void *ptr
, SIZE_T size
)
2063 if (!size
) return TRUE
;
2064 if (!ptr
) return FALSE
;
2068 volatile char *p
= ptr
;
2069 SIZE_T count
= size
;
2071 while (count
> page_size
)
2089 /***********************************************************************
2090 * virtual_uninterrupted_read_memory
2092 * Similar to NtReadVirtualMemory, but without wineserver calls. Moreover
2093 * permissions are checked before accessing each page, to ensure that no
2094 * exceptions can happen.
2096 SIZE_T
virtual_uninterrupted_read_memory( const void *addr
, void *buffer
, SIZE_T size
)
2098 struct file_view
*view
;
2100 SIZE_T bytes_read
= 0;
2102 if (!size
) return 0;
2104 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2105 if ((view
= VIRTUAL_FindView( addr
, size
)))
2107 if (!(view
->protect
& VPROT_SYSTEM
))
2109 char *page
= ROUND_ADDR( addr
, page_mask
);
2111 while (bytes_read
< size
&& (VIRTUAL_GetUnixProt( get_page_vprot( page
)) & PROT_READ
))
2113 SIZE_T block_size
= min( size
, page_size
- ((UINT_PTR
)addr
& page_mask
) );
2114 memcpy( buffer
, addr
, block_size
);
2116 addr
= (const void *)((const char *)addr
+ block_size
);
2117 buffer
= (void *)((char *)buffer
+ block_size
);
2118 bytes_read
+= block_size
;
2123 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2128 /***********************************************************************
2129 * virtual_uninterrupted_write_memory
2131 * Similar to NtWriteVirtualMemory, but without wineserver calls. Moreover
2132 * permissions are checked before accessing each page, to ensure that no
2133 * exceptions can happen.
2135 NTSTATUS
virtual_uninterrupted_write_memory( void *addr
, const void *buffer
, SIZE_T size
)
2137 BOOL has_write_watch
= FALSE
;
2141 if (!size
) return STATUS_SUCCESS
;
2143 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2144 if (!(ret
= check_write_access( addr
, size
, &has_write_watch
)))
2146 memcpy( addr
, buffer
, size
);
2147 if (has_write_watch
) update_write_watches( addr
, size
, size
);
2149 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2154 /***********************************************************************
2155 * VIRTUAL_SetForceExec
2157 * Whether to force exec prot on all views.
2159 void VIRTUAL_SetForceExec( BOOL enable
)
2161 struct file_view
*view
;
2164 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2165 if (!force_exec_prot
!= !enable
) /* change all existing views */
2167 force_exec_prot
= enable
;
2169 WINE_RB_FOR_EACH_ENTRY( view
, &views_tree
, struct file_view
, entry
)
2171 /* file mappings are always accessible */
2172 BYTE commit
= is_view_valloc( view
) ? 0 : VPROT_COMMITTED
;
2174 mprotect_range( view
->base
, view
->size
, commit
, 0 );
2177 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2186 /* free reserved areas above the limit; callback for wine_mmap_enum_reserved_areas */
2187 static int free_reserved_memory( void *base
, size_t size
, void *arg
)
2189 struct free_range
*range
= arg
;
2191 if ((char *)base
>= range
->limit
) return 0;
2192 if ((char *)base
+ size
<= range
->base
) return 0;
2193 if ((char *)base
< range
->base
)
2195 size
-= range
->base
- (char *)base
;
2198 if ((char *)base
+ size
> range
->limit
) size
= range
->limit
- (char *)base
;
2199 remove_reserved_area( base
, size
);
2200 return 1; /* stop enumeration since the list has changed */
2203 /***********************************************************************
2204 * virtual_release_address_space
2206 * Release some address space once we have loaded and initialized the app.
2208 void virtual_release_address_space(void)
2210 struct free_range range
;
2213 if (is_win64
) return;
2215 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2217 range
.base
= (char *)0x82000000;
2218 range
.limit
= user_space_limit
;
2220 if (range
.limit
> range
.base
)
2222 while (wine_mmap_enum_reserved_areas( free_reserved_memory
, &range
, 1 )) /* nothing */;
2226 #ifndef __APPLE__ /* dyld doesn't support parts of the WINE_DOS segment being unmapped */
2227 range
.base
= (char *)0x20000000;
2228 range
.limit
= (char *)0x7f000000;
2229 while (wine_mmap_enum_reserved_areas( free_reserved_memory
, &range
, 0 )) /* nothing */;
2233 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2237 /***********************************************************************
2238 * virtual_set_large_address_space
2240 * Enable use of a large address space when allowed by the application.
2242 void virtual_set_large_address_space(void)
2244 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( NtCurrentTeb()->Peb
->ImageBaseAddress
);
2246 if (!(nt
->FileHeader
.Characteristics
& IMAGE_FILE_LARGE_ADDRESS_AWARE
)) return;
2247 /* no large address space on win9x */
2248 if (NtCurrentTeb()->Peb
->OSPlatformId
!= VER_PLATFORM_WIN32_NT
) return;
2250 user_space_limit
= working_set_limit
= address_space_limit
;
2254 /***********************************************************************
2255 * NtAllocateVirtualMemory (NTDLL.@)
2256 * ZwAllocateVirtualMemory (NTDLL.@)
2258 NTSTATUS WINAPI
NtAllocateVirtualMemory( HANDLE process
, PVOID
*ret
, ULONG zero_bits
,
2259 SIZE_T
*size_ptr
, ULONG type
, ULONG protect
)
2263 SIZE_T size
= *size_ptr
;
2264 SIZE_T mask
= get_mask( zero_bits
);
2265 NTSTATUS status
= STATUS_SUCCESS
;
2266 BOOL is_dos_memory
= FALSE
;
2267 struct file_view
*view
;
2270 TRACE("%p %p %08lx %x %08x\n", process
, *ret
, size
, type
, protect
);
2272 if (!size
) return STATUS_INVALID_PARAMETER
;
2273 if (!mask
) return STATUS_INVALID_PARAMETER_3
;
2275 if (process
!= NtCurrentProcess())
2278 apc_result_t result
;
2280 memset( &call
, 0, sizeof(call
) );
2282 call
.virtual_alloc
.type
= APC_VIRTUAL_ALLOC
;
2283 call
.virtual_alloc
.addr
= wine_server_client_ptr( *ret
);
2284 call
.virtual_alloc
.size
= *size_ptr
;
2285 call
.virtual_alloc
.zero_bits
= zero_bits
;
2286 call
.virtual_alloc
.op_type
= type
;
2287 call
.virtual_alloc
.prot
= protect
;
2288 status
= server_queue_process_apc( process
, &call
, &result
);
2289 if (status
!= STATUS_SUCCESS
) return status
;
2291 if (result
.virtual_alloc
.status
== STATUS_SUCCESS
)
2293 *ret
= wine_server_get_ptr( result
.virtual_alloc
.addr
);
2294 *size_ptr
= result
.virtual_alloc
.size
;
2296 return result
.virtual_alloc
.status
;
2299 /* Round parameters to a page boundary */
2301 if (is_beyond_limit( 0, size
, working_set_limit
)) return STATUS_WORKING_SET_LIMIT_RANGE
;
2305 if (type
& MEM_RESERVE
) /* Round down to 64k boundary */
2306 base
= ROUND_ADDR( *ret
, mask
);
2308 base
= ROUND_ADDR( *ret
, page_mask
);
2309 size
= (((UINT_PTR
)*ret
+ size
+ page_mask
) & ~page_mask
) - (UINT_PTR
)base
;
2311 /* disallow low 64k, wrap-around and kernel space */
2312 if (((char *)base
< (char *)0x10000) ||
2313 ((char *)base
+ size
< (char *)base
) ||
2314 is_beyond_limit( base
, size
, address_space_limit
))
2316 /* address 1 is magic to mean DOS area */
2317 if (!base
&& *ret
== (void *)1 && size
== 0x110000) is_dos_memory
= TRUE
;
2318 else return STATUS_INVALID_PARAMETER
;
2324 size
= (size
+ page_mask
) & ~page_mask
;
2327 /* Compute the alloc type flags */
2329 if (!(type
& (MEM_COMMIT
| MEM_RESERVE
| MEM_RESET
)) ||
2330 (type
& ~(MEM_COMMIT
| MEM_RESERVE
| MEM_TOP_DOWN
| MEM_WRITE_WATCH
| MEM_RESET
)))
2332 WARN("called with wrong alloc type flags (%08x) !\n", type
);
2333 return STATUS_INVALID_PARAMETER
;
2336 /* Reserve the memory */
2338 if (use_locks
) server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2340 if ((type
& MEM_RESERVE
) || !base
)
2342 if (!(status
= get_vprot_flags( protect
, &vprot
, FALSE
)))
2344 if (type
& MEM_COMMIT
) vprot
|= VPROT_COMMITTED
;
2345 if (type
& MEM_WRITE_WATCH
) vprot
|= VPROT_WRITEWATCH
;
2346 if (protect
& PAGE_NOCACHE
) vprot
|= SEC_NOCACHE
;
2348 if (vprot
& VPROT_WRITECOPY
) status
= STATUS_INVALID_PAGE_PROTECTION
;
2349 else if (is_dos_memory
) status
= allocate_dos_memory( &view
, vprot
);
2350 else status
= map_view( &view
, base
, size
, mask
, type
& MEM_TOP_DOWN
, vprot
);
2352 if (status
== STATUS_SUCCESS
) base
= view
->base
;
2355 else if (type
& MEM_RESET
)
2357 if (!(view
= VIRTUAL_FindView( base
, size
))) status
= STATUS_NOT_MAPPED_VIEW
;
2358 else madvise( base
, size
, MADV_DONTNEED
);
2360 else /* commit the pages */
2362 if (!(view
= VIRTUAL_FindView( base
, size
))) status
= STATUS_NOT_MAPPED_VIEW
;
2363 else if (view
->protect
& SEC_FILE
) status
= STATUS_ALREADY_COMMITTED
;
2364 else if (!(status
= set_protection( view
, base
, size
, protect
)) && (view
->protect
& SEC_RESERVE
))
2366 SERVER_START_REQ( add_mapping_committed_range
)
2368 req
->base
= wine_server_client_ptr( view
->base
);
2369 req
->offset
= (char *)base
- (char *)view
->base
;
2371 wine_server_call( req
);
2377 if (!status
) VIRTUAL_DEBUG_DUMP_VIEW( view
);
2379 if (use_locks
) server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2381 if (status
== STATUS_SUCCESS
)
2390 /***********************************************************************
2391 * NtFreeVirtualMemory (NTDLL.@)
2392 * ZwFreeVirtualMemory (NTDLL.@)
2394 NTSTATUS WINAPI
NtFreeVirtualMemory( HANDLE process
, PVOID
*addr_ptr
, SIZE_T
*size_ptr
, ULONG type
)
2396 struct file_view
*view
;
2399 NTSTATUS status
= STATUS_SUCCESS
;
2400 LPVOID addr
= *addr_ptr
;
2401 SIZE_T size
= *size_ptr
;
2403 TRACE("%p %p %08lx %x\n", process
, addr
, size
, type
);
2405 if (process
!= NtCurrentProcess())
2408 apc_result_t result
;
2410 memset( &call
, 0, sizeof(call
) );
2412 call
.virtual_free
.type
= APC_VIRTUAL_FREE
;
2413 call
.virtual_free
.addr
= wine_server_client_ptr( addr
);
2414 call
.virtual_free
.size
= size
;
2415 call
.virtual_free
.op_type
= type
;
2416 status
= server_queue_process_apc( process
, &call
, &result
);
2417 if (status
!= STATUS_SUCCESS
) return status
;
2419 if (result
.virtual_free
.status
== STATUS_SUCCESS
)
2421 *addr_ptr
= wine_server_get_ptr( result
.virtual_free
.addr
);
2422 *size_ptr
= result
.virtual_free
.size
;
2424 return result
.virtual_free
.status
;
2427 /* Fix the parameters */
2429 size
= ROUND_SIZE( addr
, size
);
2430 base
= ROUND_ADDR( addr
, page_mask
);
2432 /* avoid freeing the DOS area when a broken app passes a NULL pointer */
2433 if (!base
) return STATUS_INVALID_PARAMETER
;
2435 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2437 if (!(view
= VIRTUAL_FindView( base
, size
)) || !is_view_valloc( view
))
2439 status
= STATUS_INVALID_PARAMETER
;
2441 else if (type
== MEM_RELEASE
)
2443 /* Free the pages */
2445 if (size
|| (base
!= view
->base
)) status
= STATUS_INVALID_PARAMETER
;
2448 delete_view( view
);
2453 else if (type
== MEM_DECOMMIT
)
2455 status
= decommit_pages( view
, base
- (char *)view
->base
, size
);
2456 if (status
== STATUS_SUCCESS
)
2464 WARN("called with wrong free type flags (%08x) !\n", type
);
2465 status
= STATUS_INVALID_PARAMETER
;
2468 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2473 /***********************************************************************
2474 * NtProtectVirtualMemory (NTDLL.@)
2475 * ZwProtectVirtualMemory (NTDLL.@)
2477 NTSTATUS WINAPI
NtProtectVirtualMemory( HANDLE process
, PVOID
*addr_ptr
, SIZE_T
*size_ptr
,
2478 ULONG new_prot
, ULONG
*old_prot
)
2480 struct file_view
*view
;
2482 NTSTATUS status
= STATUS_SUCCESS
;
2485 SIZE_T size
= *size_ptr
;
2486 LPVOID addr
= *addr_ptr
;
2489 TRACE("%p %p %08lx %08x\n", process
, addr
, size
, new_prot
);
2492 return STATUS_ACCESS_VIOLATION
;
2494 if (process
!= NtCurrentProcess())
2497 apc_result_t result
;
2499 memset( &call
, 0, sizeof(call
) );
2501 call
.virtual_protect
.type
= APC_VIRTUAL_PROTECT
;
2502 call
.virtual_protect
.addr
= wine_server_client_ptr( addr
);
2503 call
.virtual_protect
.size
= size
;
2504 call
.virtual_protect
.prot
= new_prot
;
2505 status
= server_queue_process_apc( process
, &call
, &result
);
2506 if (status
!= STATUS_SUCCESS
) return status
;
2508 if (result
.virtual_protect
.status
== STATUS_SUCCESS
)
2510 *addr_ptr
= wine_server_get_ptr( result
.virtual_protect
.addr
);
2511 *size_ptr
= result
.virtual_protect
.size
;
2512 if (old_prot
) *old_prot
= result
.virtual_protect
.prot
;
2514 return result
.virtual_protect
.status
;
2517 /* Fix the parameters */
2519 size
= ROUND_SIZE( addr
, size
);
2520 base
= ROUND_ADDR( addr
, page_mask
);
2522 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2524 if ((view
= VIRTUAL_FindView( base
, size
)))
2526 /* Make sure all the pages are committed */
2527 if (get_committed_size( view
, base
, &vprot
) >= size
&& (vprot
& VPROT_COMMITTED
))
2529 old
= VIRTUAL_GetWin32Prot( vprot
, view
->protect
);
2530 status
= set_protection( view
, base
, size
, new_prot
);
2532 else status
= STATUS_NOT_COMMITTED
;
2534 else status
= STATUS_INVALID_PARAMETER
;
2536 if (!status
) VIRTUAL_DEBUG_DUMP_VIEW( view
);
2538 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2540 if (status
== STATUS_SUCCESS
)
2550 /* retrieve state for a free memory area; callback for wine_mmap_enum_reserved_areas */
2551 static int get_free_mem_state_callback( void *start
, size_t size
, void *arg
)
2553 MEMORY_BASIC_INFORMATION
*info
= arg
;
2554 void *end
= (char *)start
+ size
;
2556 if ((char *)info
->BaseAddress
+ info
->RegionSize
< (char *)start
) return 0;
2558 if (info
->BaseAddress
>= end
)
2560 if (info
->AllocationBase
< end
) info
->AllocationBase
= end
;
2564 if (info
->BaseAddress
>= start
|| start
<= address_space_start
)
2566 /* it's a real free area */
2567 info
->State
= MEM_FREE
;
2568 info
->Protect
= PAGE_NOACCESS
;
2569 info
->AllocationBase
= 0;
2570 info
->AllocationProtect
= 0;
2572 if ((char *)info
->BaseAddress
+ info
->RegionSize
> (char *)end
)
2573 info
->RegionSize
= (char *)end
- (char *)info
->BaseAddress
;
2575 else /* outside of the reserved area, pretend it's allocated */
2577 info
->RegionSize
= (char *)start
- (char *)info
->BaseAddress
;
2578 info
->State
= MEM_RESERVE
;
2579 info
->Protect
= PAGE_NOACCESS
;
2580 info
->AllocationProtect
= PAGE_NOACCESS
;
2581 info
->Type
= MEM_PRIVATE
;
2586 #define UNIMPLEMENTED_INFO_CLASS(c) \
2588 FIXME("(process=%p,addr=%p) Unimplemented information class: " #c "\n", process, addr); \
2589 return STATUS_INVALID_INFO_CLASS
2591 /***********************************************************************
2592 * NtQueryVirtualMemory (NTDLL.@)
2593 * ZwQueryVirtualMemory (NTDLL.@)
2595 NTSTATUS WINAPI
NtQueryVirtualMemory( HANDLE process
, LPCVOID addr
,
2596 MEMORY_INFORMATION_CLASS info_class
, PVOID buffer
,
2597 SIZE_T len
, SIZE_T
*res_len
)
2599 struct file_view
*view
;
2600 char *base
, *alloc_base
= 0, *alloc_end
= working_set_limit
;
2601 struct wine_rb_entry
*ptr
;
2602 MEMORY_BASIC_INFORMATION
*info
= buffer
;
2605 if (info_class
!= MemoryBasicInformation
)
2609 UNIMPLEMENTED_INFO_CLASS(MemoryWorkingSetList
);
2610 UNIMPLEMENTED_INFO_CLASS(MemorySectionName
);
2611 UNIMPLEMENTED_INFO_CLASS(MemoryBasicVlmInformation
);
2614 FIXME("(%p,%p,info_class=%d,%p,%ld,%p) Unknown information class\n",
2615 process
, addr
, info_class
, buffer
, len
, res_len
);
2616 return STATUS_INVALID_INFO_CLASS
;
2620 if (process
!= NtCurrentProcess())
2624 apc_result_t result
;
2626 memset( &call
, 0, sizeof(call
) );
2628 call
.virtual_query
.type
= APC_VIRTUAL_QUERY
;
2629 call
.virtual_query
.addr
= wine_server_client_ptr( addr
);
2630 status
= server_queue_process_apc( process
, &call
, &result
);
2631 if (status
!= STATUS_SUCCESS
) return status
;
2633 if (result
.virtual_query
.status
== STATUS_SUCCESS
)
2635 info
->BaseAddress
= wine_server_get_ptr( result
.virtual_query
.base
);
2636 info
->AllocationBase
= wine_server_get_ptr( result
.virtual_query
.alloc_base
);
2637 info
->RegionSize
= result
.virtual_query
.size
;
2638 info
->Protect
= result
.virtual_query
.prot
;
2639 info
->AllocationProtect
= result
.virtual_query
.alloc_prot
;
2640 info
->State
= (DWORD
)result
.virtual_query
.state
<< 12;
2641 info
->Type
= (DWORD
)result
.virtual_query
.alloc_type
<< 16;
2642 if (info
->RegionSize
!= result
.virtual_query
.size
) /* truncated */
2643 return STATUS_INVALID_PARAMETER
; /* FIXME */
2644 if (res_len
) *res_len
= sizeof(*info
);
2646 return result
.virtual_query
.status
;
2649 base
= ROUND_ADDR( addr
, page_mask
);
2651 if (is_beyond_limit( base
, 1, working_set_limit
)) return STATUS_WORKING_SET_LIMIT_RANGE
;
2653 /* Find the view containing the address */
2655 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2656 ptr
= views_tree
.root
;
2659 view
= WINE_RB_ENTRY_VALUE( ptr
, struct file_view
, entry
);
2660 if ((char *)view
->base
> base
)
2662 alloc_end
= view
->base
;
2665 else if ((char *)view
->base
+ view
->size
<= base
)
2667 alloc_base
= (char *)view
->base
+ view
->size
;
2672 alloc_base
= view
->base
;
2673 alloc_end
= (char *)view
->base
+ view
->size
;
2678 /* Fill the info structure */
2680 info
->AllocationBase
= alloc_base
;
2681 info
->BaseAddress
= base
;
2682 info
->RegionSize
= alloc_end
- base
;
2686 if (!wine_mmap_enum_reserved_areas( get_free_mem_state_callback
, info
, 0 ))
2688 /* not in a reserved area at all, pretend it's allocated */
2690 if (base
>= (char *)address_space_start
)
2692 info
->State
= MEM_RESERVE
;
2693 info
->Protect
= PAGE_NOACCESS
;
2694 info
->AllocationProtect
= PAGE_NOACCESS
;
2695 info
->Type
= MEM_PRIVATE
;
2700 info
->State
= MEM_FREE
;
2701 info
->Protect
= PAGE_NOACCESS
;
2702 info
->AllocationBase
= 0;
2703 info
->AllocationProtect
= 0;
2712 SIZE_T range_size
= get_committed_size( view
, base
, &vprot
);
2714 info
->State
= (vprot
& VPROT_COMMITTED
) ? MEM_COMMIT
: MEM_RESERVE
;
2715 info
->Protect
= (vprot
& VPROT_COMMITTED
) ? VIRTUAL_GetWin32Prot( vprot
, view
->protect
) : 0;
2716 info
->AllocationProtect
= VIRTUAL_GetWin32Prot( view
->protect
, view
->protect
);
2717 if (view
->protect
& SEC_IMAGE
) info
->Type
= MEM_IMAGE
;
2718 else if (view
->protect
& (SEC_FILE
| SEC_RESERVE
| SEC_COMMIT
)) info
->Type
= MEM_MAPPED
;
2719 else info
->Type
= MEM_PRIVATE
;
2720 for (ptr
= base
; ptr
< base
+ range_size
; ptr
+= page_size
)
2721 if ((get_page_vprot( ptr
) ^ vprot
) & ~VPROT_WRITEWATCH
) break;
2722 info
->RegionSize
= ptr
- base
;
2724 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2726 if (res_len
) *res_len
= sizeof(*info
);
2727 return STATUS_SUCCESS
;
2731 /***********************************************************************
2732 * NtLockVirtualMemory (NTDLL.@)
2733 * ZwLockVirtualMemory (NTDLL.@)
2735 NTSTATUS WINAPI
NtLockVirtualMemory( HANDLE process
, PVOID
*addr
, SIZE_T
*size
, ULONG unknown
)
2737 NTSTATUS status
= STATUS_SUCCESS
;
2739 if (process
!= NtCurrentProcess())
2742 apc_result_t result
;
2744 memset( &call
, 0, sizeof(call
) );
2746 call
.virtual_lock
.type
= APC_VIRTUAL_LOCK
;
2747 call
.virtual_lock
.addr
= wine_server_client_ptr( *addr
);
2748 call
.virtual_lock
.size
= *size
;
2749 status
= server_queue_process_apc( process
, &call
, &result
);
2750 if (status
!= STATUS_SUCCESS
) return status
;
2752 if (result
.virtual_lock
.status
== STATUS_SUCCESS
)
2754 *addr
= wine_server_get_ptr( result
.virtual_lock
.addr
);
2755 *size
= result
.virtual_lock
.size
;
2757 return result
.virtual_lock
.status
;
2760 *size
= ROUND_SIZE( *addr
, *size
);
2761 *addr
= ROUND_ADDR( *addr
, page_mask
);
2763 if (mlock( *addr
, *size
)) status
= STATUS_ACCESS_DENIED
;
2768 /***********************************************************************
2769 * NtUnlockVirtualMemory (NTDLL.@)
2770 * ZwUnlockVirtualMemory (NTDLL.@)
2772 NTSTATUS WINAPI
NtUnlockVirtualMemory( HANDLE process
, PVOID
*addr
, SIZE_T
*size
, ULONG unknown
)
2774 NTSTATUS status
= STATUS_SUCCESS
;
2776 if (process
!= NtCurrentProcess())
2779 apc_result_t result
;
2781 memset( &call
, 0, sizeof(call
) );
2783 call
.virtual_unlock
.type
= APC_VIRTUAL_UNLOCK
;
2784 call
.virtual_unlock
.addr
= wine_server_client_ptr( *addr
);
2785 call
.virtual_unlock
.size
= *size
;
2786 status
= server_queue_process_apc( process
, &call
, &result
);
2787 if (status
!= STATUS_SUCCESS
) return status
;
2789 if (result
.virtual_unlock
.status
== STATUS_SUCCESS
)
2791 *addr
= wine_server_get_ptr( result
.virtual_unlock
.addr
);
2792 *size
= result
.virtual_unlock
.size
;
2794 return result
.virtual_unlock
.status
;
2797 *size
= ROUND_SIZE( *addr
, *size
);
2798 *addr
= ROUND_ADDR( *addr
, page_mask
);
2800 if (munlock( *addr
, *size
)) status
= STATUS_ACCESS_DENIED
;
2805 /***********************************************************************
2806 * NtCreateSection (NTDLL.@)
2807 * ZwCreateSection (NTDLL.@)
2809 NTSTATUS WINAPI
NtCreateSection( HANDLE
*handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
,
2810 const LARGE_INTEGER
*size
, ULONG protect
,
2811 ULONG sec_flags
, HANDLE file
)
2814 unsigned int vprot
, file_access
= 0;
2816 struct object_attributes
*objattr
;
2818 if ((ret
= get_vprot_flags( protect
, &vprot
, sec_flags
& SEC_IMAGE
))) return ret
;
2819 if ((ret
= alloc_object_attributes( attr
, &objattr
, &len
))) return ret
;
2821 if (vprot
& VPROT_READ
) file_access
|= FILE_READ_DATA
;
2822 if (vprot
& VPROT_WRITE
) file_access
|= FILE_WRITE_DATA
;
2824 SERVER_START_REQ( create_mapping
)
2826 req
->access
= access
;
2827 req
->flags
= sec_flags
;
2828 req
->file_handle
= wine_server_obj_handle( file
);
2829 req
->file_access
= file_access
;
2830 req
->size
= size
? size
->QuadPart
: 0;
2831 wine_server_add_data( req
, objattr
, len
);
2832 ret
= wine_server_call( req
);
2833 *handle
= wine_server_ptr_handle( reply
->handle
);
2837 RtlFreeHeap( GetProcessHeap(), 0, objattr
);
2842 /***********************************************************************
2843 * NtOpenSection (NTDLL.@)
2844 * ZwOpenSection (NTDLL.@)
2846 NTSTATUS WINAPI
NtOpenSection( HANDLE
*handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
)
2850 if ((ret
= validate_open_object_attributes( attr
))) return ret
;
2852 SERVER_START_REQ( open_mapping
)
2854 req
->access
= access
;
2855 req
->attributes
= attr
->Attributes
;
2856 req
->rootdir
= wine_server_obj_handle( attr
->RootDirectory
);
2857 if (attr
->ObjectName
)
2858 wine_server_add_data( req
, attr
->ObjectName
->Buffer
, attr
->ObjectName
->Length
);
2859 ret
= wine_server_call( req
);
2860 *handle
= wine_server_ptr_handle( reply
->handle
);
2867 /***********************************************************************
2868 * NtMapViewOfSection (NTDLL.@)
2869 * ZwMapViewOfSection (NTDLL.@)
2871 NTSTATUS WINAPI
NtMapViewOfSection( HANDLE handle
, HANDLE process
, PVOID
*addr_ptr
, ULONG zero_bits
,
2872 SIZE_T commit_size
, const LARGE_INTEGER
*offset_ptr
, SIZE_T
*size_ptr
,
2873 SECTION_INHERIT inherit
, ULONG alloc_type
, ULONG protect
)
2876 mem_size_t full_size
;
2878 SIZE_T size
, mask
= get_mask( zero_bits
);
2879 int unix_handle
= -1, needs_close
;
2880 unsigned int vprot
, sec_flags
;
2881 struct file_view
*view
;
2882 pe_image_info_t image_info
;
2884 LARGE_INTEGER offset
;
2887 offset
.QuadPart
= offset_ptr
? offset_ptr
->QuadPart
: 0;
2889 TRACE("handle=%p process=%p addr=%p off=%x%08x size=%lx access=%x\n",
2890 handle
, process
, *addr_ptr
, offset
.u
.HighPart
, offset
.u
.LowPart
, *size_ptr
, protect
);
2892 /* Check parameters */
2894 if ((*addr_ptr
&& zero_bits
) || !mask
)
2895 return STATUS_INVALID_PARAMETER_4
;
2898 if (!is_wow64
&& (alloc_type
& AT_ROUND_TO_PAGE
))
2900 *addr_ptr
= ROUND_ADDR( *addr_ptr
, page_mask
);
2905 if ((offset
.u
.LowPart
& mask
) || (*addr_ptr
&& ((UINT_PTR
)*addr_ptr
& mask
)))
2906 return STATUS_MAPPED_ALIGNMENT
;
2912 case PAGE_WRITECOPY
:
2913 access
= SECTION_MAP_READ
;
2915 case PAGE_READWRITE
:
2916 access
= SECTION_MAP_WRITE
;
2919 case PAGE_EXECUTE_READ
:
2920 case PAGE_EXECUTE_WRITECOPY
:
2921 access
= SECTION_MAP_READ
| SECTION_MAP_EXECUTE
;
2923 case PAGE_EXECUTE_READWRITE
:
2924 access
= SECTION_MAP_WRITE
| SECTION_MAP_EXECUTE
;
2927 return STATUS_INVALID_PAGE_PROTECTION
;
2930 if (process
!= NtCurrentProcess())
2933 apc_result_t result
;
2935 memset( &call
, 0, sizeof(call
) );
2937 call
.map_view
.type
= APC_MAP_VIEW
;
2938 call
.map_view
.handle
= wine_server_obj_handle( handle
);
2939 call
.map_view
.addr
= wine_server_client_ptr( *addr_ptr
);
2940 call
.map_view
.size
= *size_ptr
;
2941 call
.map_view
.offset
= offset
.QuadPart
;
2942 call
.map_view
.zero_bits
= zero_bits
;
2943 call
.map_view
.alloc_type
= alloc_type
;
2944 call
.map_view
.prot
= protect
;
2945 res
= server_queue_process_apc( process
, &call
, &result
);
2946 if (res
!= STATUS_SUCCESS
) return res
;
2948 if ((NTSTATUS
)result
.map_view
.status
>= 0)
2950 *addr_ptr
= wine_server_get_ptr( result
.map_view
.addr
);
2951 *size_ptr
= result
.map_view
.size
;
2953 return result
.map_view
.status
;
2956 SERVER_START_REQ( get_mapping_info
)
2958 req
->handle
= wine_server_obj_handle( handle
);
2959 req
->access
= access
;
2960 wine_server_set_reply( req
, &image_info
, sizeof(image_info
) );
2961 res
= wine_server_call( req
);
2962 sec_flags
= reply
->flags
;
2963 full_size
= reply
->size
;
2964 shared_file
= wine_server_ptr_handle( reply
->shared_file
);
2967 if (res
) return res
;
2969 if ((res
= server_get_unix_fd( handle
, 0, &unix_handle
, &needs_close
, NULL
, NULL
))) goto done
;
2971 if (sec_flags
& SEC_IMAGE
)
2973 void *base
= wine_server_get_ptr( image_info
.base
);
2975 if ((ULONG_PTR
)base
!= image_info
.base
) base
= NULL
;
2976 size
= image_info
.map_size
;
2977 if (size
!= image_info
.map_size
) /* truncated */
2979 WARN( "Modules larger than 4Gb (%s) not supported\n",
2980 wine_dbgstr_longlong(image_info
.map_size
) );
2981 res
= STATUS_INVALID_PARAMETER
;
2986 int shared_fd
, shared_needs_close
;
2988 if ((res
= server_get_unix_fd( shared_file
, FILE_READ_DATA
|FILE_WRITE_DATA
,
2989 &shared_fd
, &shared_needs_close
, NULL
, NULL
))) goto done
;
2990 res
= map_image( handle
, access
, unix_handle
, base
, size
, mask
, image_info
.header_size
,
2991 shared_fd
, needs_close
, addr_ptr
);
2992 if (shared_needs_close
) close( shared_fd
);
2993 close_handle( shared_file
);
2997 res
= map_image( handle
, access
, unix_handle
, base
, size
, mask
, image_info
.header_size
,
2998 -1, needs_close
, addr_ptr
);
3000 if (needs_close
) close( unix_handle
);
3001 if (res
>= 0) *size_ptr
= size
;
3005 res
= STATUS_INVALID_PARAMETER
;
3006 if (offset
.QuadPart
>= full_size
) goto done
;
3010 if (size
> full_size
- offset
.QuadPart
)
3012 res
= STATUS_INVALID_VIEW_SIZE
;
3018 size
= full_size
- offset
.QuadPart
;
3019 if (size
!= full_size
- offset
.QuadPart
) /* truncated */
3021 WARN( "Files larger than 4Gb (%s) not supported on this platform\n",
3022 wine_dbgstr_longlong(full_size
) );
3026 if (!(size
= ROUND_SIZE( 0, size
))) goto done
; /* wrap-around */
3028 /* Reserve a properly aligned area */
3030 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
3032 get_vprot_flags( protect
, &vprot
, sec_flags
& SEC_IMAGE
);
3034 if (!(sec_flags
& SEC_RESERVE
)) vprot
|= VPROT_COMMITTED
;
3035 res
= map_view( &view
, *addr_ptr
, size
, mask
, FALSE
, vprot
);
3038 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
3044 TRACE("handle=%p size=%lx offset=%x%08x\n",
3045 handle
, size
, offset
.u
.HighPart
, offset
.u
.LowPart
);
3047 res
= map_file_into_view( view
, unix_handle
, 0, size
, offset
.QuadPart
, vprot
, needs_close
);
3048 if (res
== STATUS_SUCCESS
)
3050 SERVER_START_REQ( map_view
)
3052 req
->mapping
= wine_server_obj_handle( handle
);
3053 req
->access
= access
;
3054 req
->base
= wine_server_client_ptr( view
->base
);
3056 req
->start
= offset
.QuadPart
;
3057 res
= wine_server_call( req
);
3062 if (res
== STATUS_SUCCESS
)
3064 *addr_ptr
= view
->base
;
3066 VIRTUAL_DEBUG_DUMP_VIEW( view
);
3070 ERR( "map_file_into_view %p %lx %x%08x failed\n",
3071 view
->base
, size
, offset
.u
.HighPart
, offset
.u
.LowPart
);
3072 delete_view( view
);
3075 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
3078 if (needs_close
) close( unix_handle
);
3083 /***********************************************************************
3084 * NtUnmapViewOfSection (NTDLL.@)
3085 * ZwUnmapViewOfSection (NTDLL.@)
3087 NTSTATUS WINAPI
NtUnmapViewOfSection( HANDLE process
, PVOID addr
)
3089 struct file_view
*view
;
3090 NTSTATUS status
= STATUS_NOT_MAPPED_VIEW
;
3093 if (process
!= NtCurrentProcess())
3096 apc_result_t result
;
3098 memset( &call
, 0, sizeof(call
) );
3100 call
.unmap_view
.type
= APC_UNMAP_VIEW
;
3101 call
.unmap_view
.addr
= wine_server_client_ptr( addr
);
3102 status
= server_queue_process_apc( process
, &call
, &result
);
3103 if (status
== STATUS_SUCCESS
) status
= result
.unmap_view
.status
;
3107 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
3108 if ((view
= VIRTUAL_FindView( addr
, 0 )) && !is_view_valloc( view
))
3110 if (!(view
->protect
& VPROT_SYSTEM
))
3112 SERVER_START_REQ( unmap_view
)
3114 req
->base
= wine_server_client_ptr( view
->base
);
3115 status
= wine_server_call( req
);
3118 if (!status
) delete_view( view
);
3119 else FIXME( "failed to unmap %p %x\n", view
->base
, status
);
3121 else delete_view( view
);
3123 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
3128 /******************************************************************************
3129 * NtQuerySection (NTDLL.@)
3130 * ZwQuerySection (NTDLL.@)
3132 NTSTATUS WINAPI
NtQuerySection( HANDLE handle
, SECTION_INFORMATION_CLASS
class, void *ptr
,
3133 ULONG size
, ULONG
*ret_size
)
3136 pe_image_info_t image_info
;
3140 case SectionBasicInformation
:
3141 if (size
< sizeof(SECTION_BASIC_INFORMATION
)) return STATUS_INFO_LENGTH_MISMATCH
;
3143 case SectionImageInformation
:
3144 if (size
< sizeof(SECTION_IMAGE_INFORMATION
)) return STATUS_INFO_LENGTH_MISMATCH
;
3147 FIXME( "class %u not implemented\n", class );
3148 return STATUS_NOT_IMPLEMENTED
;
3150 if (!ptr
) return STATUS_ACCESS_VIOLATION
;
3152 SERVER_START_REQ( get_mapping_info
)
3154 req
->handle
= wine_server_obj_handle( handle
);
3155 req
->access
= SECTION_QUERY
;
3156 wine_server_set_reply( req
, &image_info
, sizeof(image_info
) );
3157 if (!(status
= wine_server_call( req
)))
3159 if (class == SectionBasicInformation
)
3161 SECTION_BASIC_INFORMATION
*info
= ptr
;
3162 info
->Attributes
= reply
->flags
;
3163 info
->BaseAddress
= NULL
;
3164 info
->Size
.QuadPart
= reply
->size
;
3165 if (ret_size
) *ret_size
= sizeof(*info
);
3167 else if (reply
->flags
& SEC_IMAGE
)
3169 SECTION_IMAGE_INFORMATION
*info
= ptr
;
3170 info
->TransferAddress
= wine_server_get_ptr( image_info
.entry_point
);
3171 info
->ZeroBits
= image_info
.zerobits
;
3172 info
->MaximumStackSize
= image_info
.stack_size
;
3173 info
->CommittedStackSize
= image_info
.stack_commit
;
3174 info
->SubSystemType
= image_info
.subsystem
;
3175 info
->SubsystemVersionLow
= image_info
.subsystem_low
;
3176 info
->SubsystemVersionHigh
= image_info
.subsystem_high
;
3177 info
->GpValue
= image_info
.gp
;
3178 info
->ImageCharacteristics
= image_info
.image_charact
;
3179 info
->DllCharacteristics
= image_info
.dll_charact
;
3180 info
->Machine
= image_info
.machine
;
3181 info
->ImageContainsCode
= image_info
.contains_code
;
3182 info
->ImageFlags
= image_info
.image_flags
;
3183 info
->LoaderFlags
= image_info
.loader_flags
;
3184 info
->ImageFileSize
= image_info
.file_size
;
3185 info
->CheckSum
= image_info
.checksum
;
3186 if (ret_size
) *ret_size
= sizeof(*info
);
3188 else status
= STATUS_SECTION_NOT_IMAGE
;
3197 /***********************************************************************
3198 * NtFlushVirtualMemory (NTDLL.@)
3199 * ZwFlushVirtualMemory (NTDLL.@)
3201 NTSTATUS WINAPI
NtFlushVirtualMemory( HANDLE process
, LPCVOID
*addr_ptr
,
3202 SIZE_T
*size_ptr
, ULONG unknown
)
3204 struct file_view
*view
;
3205 NTSTATUS status
= STATUS_SUCCESS
;
3207 void *addr
= ROUND_ADDR( *addr_ptr
, page_mask
);
3209 if (process
!= NtCurrentProcess())
3212 apc_result_t result
;
3214 memset( &call
, 0, sizeof(call
) );
3216 call
.virtual_flush
.type
= APC_VIRTUAL_FLUSH
;
3217 call
.virtual_flush
.addr
= wine_server_client_ptr( addr
);
3218 call
.virtual_flush
.size
= *size_ptr
;
3219 status
= server_queue_process_apc( process
, &call
, &result
);
3220 if (status
!= STATUS_SUCCESS
) return status
;
3222 if (result
.virtual_flush
.status
== STATUS_SUCCESS
)
3224 *addr_ptr
= wine_server_get_ptr( result
.virtual_flush
.addr
);
3225 *size_ptr
= result
.virtual_flush
.size
;
3227 return result
.virtual_flush
.status
;
3230 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
3231 if (!(view
= VIRTUAL_FindView( addr
, *size_ptr
))) status
= STATUS_INVALID_PARAMETER
;
3234 if (!*size_ptr
) *size_ptr
= view
->size
;
3237 if (msync( addr
, *size_ptr
, MS_ASYNC
)) status
= STATUS_NOT_MAPPED_DATA
;
3240 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
3245 /***********************************************************************
3246 * NtGetWriteWatch (NTDLL.@)
3247 * ZwGetWriteWatch (NTDLL.@)
3249 NTSTATUS WINAPI
NtGetWriteWatch( HANDLE process
, ULONG flags
, PVOID base
, SIZE_T size
, PVOID
*addresses
,
3250 ULONG_PTR
*count
, ULONG
*granularity
)
3252 NTSTATUS status
= STATUS_SUCCESS
;
3255 size
= ROUND_SIZE( base
, size
);
3256 base
= ROUND_ADDR( base
, page_mask
);
3258 if (!count
|| !granularity
) return STATUS_ACCESS_VIOLATION
;
3259 if (!*count
|| !size
) return STATUS_INVALID_PARAMETER
;
3260 if (flags
& ~WRITE_WATCH_FLAG_RESET
) return STATUS_INVALID_PARAMETER
;
3262 if (!addresses
) return STATUS_ACCESS_VIOLATION
;
3264 TRACE( "%p %x %p-%p %p %lu\n", process
, flags
, base
, (char *)base
+ size
,
3265 addresses
, *count
);
3267 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
3269 if (is_write_watch_range( base
, size
))
3273 char *end
= addr
+ size
;
3275 while (pos
< *count
&& addr
< end
)
3277 if (!(get_page_vprot( addr
) & VPROT_WRITEWATCH
)) addresses
[pos
++] = addr
;
3280 if (flags
& WRITE_WATCH_FLAG_RESET
) reset_write_watches( base
, addr
- (char *)base
);
3282 *granularity
= page_size
;
3284 else status
= STATUS_INVALID_PARAMETER
;
3286 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
3291 /***********************************************************************
3292 * NtResetWriteWatch (NTDLL.@)
3293 * ZwResetWriteWatch (NTDLL.@)
3295 NTSTATUS WINAPI
NtResetWriteWatch( HANDLE process
, PVOID base
, SIZE_T size
)
3297 NTSTATUS status
= STATUS_SUCCESS
;
3300 size
= ROUND_SIZE( base
, size
);
3301 base
= ROUND_ADDR( base
, page_mask
);
3303 TRACE( "%p %p-%p\n", process
, base
, (char *)base
+ size
);
3305 if (!size
) return STATUS_INVALID_PARAMETER
;
3307 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
3309 if (is_write_watch_range( base
, size
))
3310 reset_write_watches( base
, size
);
3312 status
= STATUS_INVALID_PARAMETER
;
3314 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
3319 /***********************************************************************
3320 * NtReadVirtualMemory (NTDLL.@)
3321 * ZwReadVirtualMemory (NTDLL.@)
3323 NTSTATUS WINAPI
NtReadVirtualMemory( HANDLE process
, const void *addr
, void *buffer
,
3324 SIZE_T size
, SIZE_T
*bytes_read
)
3328 if (virtual_check_buffer_for_write( buffer
, size
))
3330 SERVER_START_REQ( read_process_memory
)
3332 req
->handle
= wine_server_obj_handle( process
);
3333 req
->addr
= wine_server_client_ptr( addr
);
3334 wine_server_set_reply( req
, buffer
, size
);
3335 if ((status
= wine_server_call( req
))) size
= 0;
3341 status
= STATUS_ACCESS_VIOLATION
;
3344 if (bytes_read
) *bytes_read
= size
;
3349 /***********************************************************************
3350 * NtWriteVirtualMemory (NTDLL.@)
3351 * ZwWriteVirtualMemory (NTDLL.@)
3353 NTSTATUS WINAPI
NtWriteVirtualMemory( HANDLE process
, void *addr
, const void *buffer
,
3354 SIZE_T size
, SIZE_T
*bytes_written
)
3358 if (virtual_check_buffer_for_read( buffer
, size
))
3360 SERVER_START_REQ( write_process_memory
)
3362 req
->handle
= wine_server_obj_handle( process
);
3363 req
->addr
= wine_server_client_ptr( addr
);
3364 wine_server_add_data( req
, buffer
, size
);
3365 if ((status
= wine_server_call( req
))) size
= 0;
3371 status
= STATUS_PARTIAL_COPY
;
3374 if (bytes_written
) *bytes_written
= size
;
3379 /***********************************************************************
3380 * NtAreMappedFilesTheSame (NTDLL.@)
3381 * ZwAreMappedFilesTheSame (NTDLL.@)
3383 NTSTATUS WINAPI
NtAreMappedFilesTheSame(PVOID addr1
, PVOID addr2
)
3385 struct file_view
*view1
, *view2
;
3389 TRACE("%p %p\n", addr1
, addr2
);
3391 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
3393 view1
= VIRTUAL_FindView( addr1
, 0 );
3394 view2
= VIRTUAL_FindView( addr2
, 0 );
3396 if (!view1
|| !view2
)
3397 status
= STATUS_INVALID_ADDRESS
;
3398 else if (is_view_valloc( view1
) || is_view_valloc( view2
))
3399 status
= STATUS_CONFLICTING_ADDRESSES
;
3400 else if (view1
== view2
)
3401 status
= STATUS_SUCCESS
;
3402 else if ((view1
->protect
& VPROT_SYSTEM
) || (view2
->protect
& VPROT_SYSTEM
))
3403 status
= STATUS_NOT_SAME_DEVICE
;
3406 SERVER_START_REQ( is_same_mapping
)
3408 req
->base1
= wine_server_client_ptr( view1
->base
);
3409 req
->base2
= wine_server_client_ptr( view2
->base
);
3410 status
= wine_server_call( req
);
3415 server_leave_uninterrupted_section( &csVirtual
, &sigset
);