2 * Win32 virtual memory functions
4 * Copyright 1997, 2002 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
26 #ifdef HAVE_SYS_ERRNO_H
27 #include <sys/errno.h>
37 #include <sys/types.h>
38 #ifdef HAVE_SYS_STAT_H
39 # include <sys/stat.h>
41 #ifdef HAVE_SYS_MMAN_H
42 # include <sys/mman.h>
44 #ifdef HAVE_VALGRIND_VALGRIND_H
45 # include <valgrind/valgrind.h>
48 #define NONAMELESSUNION
49 #define NONAMELESSSTRUCT
51 #define WIN32_NO_STATUS
54 #include "wine/library.h"
55 #include "wine/server.h"
56 #include "wine/list.h"
57 #include "wine/debug.h"
58 #include "ntdll_misc.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(virtual);
61 WINE_DECLARE_DEBUG_CHANNEL(module
);
68 #define MAP_NORESERVE 0
72 typedef struct file_view
74 struct list entry
; /* Entry in global view list */
75 void *base
; /* Base address */
76 size_t size
; /* Size in bytes */
77 HANDLE mapping
; /* Handle to the file mapping */
78 BYTE flags
; /* Allocation flags (VFLAG_*) */
79 BYTE protect
; /* Protection for all pages at allocation time */
80 BYTE prot
[1]; /* Protection byte for each page */
84 #define VFLAG_SYSTEM 0x01 /* system view (underlying mmap not under our control) */
85 #define VFLAG_VALLOC 0x02 /* allocated by VirtualAlloc */
87 /* Conversion from VPROT_* to Win32 flags */
88 static const BYTE VIRTUAL_Win32Flags
[16] =
90 PAGE_NOACCESS
, /* 0 */
91 PAGE_READONLY
, /* READ */
92 PAGE_READWRITE
, /* WRITE */
93 PAGE_READWRITE
, /* READ | WRITE */
94 PAGE_EXECUTE
, /* EXEC */
95 PAGE_EXECUTE_READ
, /* READ | EXEC */
96 PAGE_EXECUTE_READWRITE
, /* WRITE | EXEC */
97 PAGE_EXECUTE_READWRITE
, /* READ | WRITE | EXEC */
98 PAGE_WRITECOPY
, /* WRITECOPY */
99 PAGE_WRITECOPY
, /* READ | WRITECOPY */
100 PAGE_WRITECOPY
, /* WRITE | WRITECOPY */
101 PAGE_WRITECOPY
, /* READ | WRITE | WRITECOPY */
102 PAGE_EXECUTE_WRITECOPY
, /* EXEC | WRITECOPY */
103 PAGE_EXECUTE_WRITECOPY
, /* READ | EXEC | WRITECOPY */
104 PAGE_EXECUTE_WRITECOPY
, /* WRITE | EXEC | WRITECOPY */
105 PAGE_EXECUTE_WRITECOPY
/* READ | WRITE | EXEC | WRITECOPY */
108 static struct list views_list
= LIST_INIT(views_list
);
110 static RTL_CRITICAL_SECTION csVirtual
;
111 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
114 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
115 0, 0, { (DWORD_PTR
)(__FILE__
": csVirtual") }
117 static RTL_CRITICAL_SECTION csVirtual
= { &critsect_debug
, -1, 0, 0, 0, 0 };
120 /* These are always the same on an i386, and it will be faster this way */
121 # define page_mask 0xfff
122 # define page_shift 12
123 # define page_size 0x1000
124 /* Note: these are Windows limits, you cannot change them. */
125 static void *address_space_limit
= (void *)0xc0000000; /* top of the total available address space */
126 static void *user_space_limit
= (void *)0x7fff0000; /* top of the user address space */
128 static UINT page_shift
;
129 static UINT page_size
;
130 static UINT_PTR page_mask
;
131 static void * const address_space_limit
= 0; /* no limit needed on other platforms */
132 static void * const user_space_limit
= 0; /* no limit needed on other platforms */
133 #endif /* __i386__ */
135 #define ROUND_ADDR(addr,mask) \
136 ((void *)((UINT_PTR)(addr) & ~(UINT_PTR)(mask)))
138 #define ROUND_SIZE(addr,size) \
139 (((UINT)(size) + ((UINT_PTR)(addr) & page_mask) + page_mask) & ~page_mask)
141 #define VIRTUAL_DEBUG_DUMP_VIEW(view) \
142 do { if (TRACE_ON(virtual)) VIRTUAL_DumpView(view); } while (0)
144 #define VIRTUAL_HEAP_SIZE (4*1024*1024)
146 static HANDLE virtual_heap
;
147 static void *preload_reserve_start
;
148 static void *preload_reserve_end
;
149 static int use_locks
;
150 static int force_exec_prot
; /* whether to force PROT_EXEC on all PROT_READ mmaps */
153 /***********************************************************************
156 static const char *VIRTUAL_GetProtStr( BYTE prot
)
158 static char buffer
[6];
159 buffer
[0] = (prot
& VPROT_COMMITTED
) ? 'c' : '-';
160 buffer
[1] = (prot
& VPROT_GUARD
) ? 'g' : '-';
161 buffer
[2] = (prot
& VPROT_READ
) ? 'r' : '-';
162 buffer
[3] = (prot
& VPROT_WRITECOPY
) ? 'W' : ((prot
& VPROT_WRITE
) ? 'w' : '-');
163 buffer
[4] = (prot
& VPROT_EXEC
) ? 'x' : '-';
169 /***********************************************************************
170 * VIRTUAL_GetUnixProt
172 * Convert page protections to protection for mmap/mprotect.
174 static int VIRTUAL_GetUnixProt( BYTE vprot
)
177 if ((vprot
& VPROT_COMMITTED
) && !(vprot
& VPROT_GUARD
))
179 if (vprot
& VPROT_READ
) prot
|= PROT_READ
;
180 if (vprot
& VPROT_WRITE
) prot
|= PROT_WRITE
;
181 if (vprot
& VPROT_WRITECOPY
) prot
|= PROT_WRITE
;
182 if (vprot
& VPROT_EXEC
) prot
|= PROT_EXEC
;
184 if (!prot
) prot
= PROT_NONE
;
189 /***********************************************************************
192 static void VIRTUAL_DumpView( FILE_VIEW
*view
)
195 char *addr
= view
->base
;
196 BYTE prot
= view
->prot
[0];
198 TRACE( "View: %p - %p", addr
, addr
+ view
->size
- 1 );
199 if (view
->flags
& VFLAG_SYSTEM
)
200 TRACE( " (system)\n" );
201 else if (view
->flags
& VFLAG_VALLOC
)
202 TRACE( " (valloc)\n" );
203 else if (view
->mapping
)
204 TRACE( " %p\n", view
->mapping
);
206 TRACE( " (anonymous)\n");
208 for (count
= i
= 1; i
< view
->size
>> page_shift
; i
++, count
++)
210 if (view
->prot
[i
] == prot
) continue;
211 TRACE( " %p - %p %s\n",
212 addr
, addr
+ (count
<< page_shift
) - 1, VIRTUAL_GetProtStr(prot
) );
213 addr
+= (count
<< page_shift
);
214 prot
= view
->prot
[i
];
218 TRACE( " %p - %p %s\n",
219 addr
, addr
+ (count
<< page_shift
) - 1, VIRTUAL_GetProtStr(prot
) );
223 /***********************************************************************
227 static void VIRTUAL_Dump(void)
230 struct file_view
*view
;
232 TRACE( "Dump of all virtual memory views:\n" );
233 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
234 LIST_FOR_EACH_ENTRY( view
, &views_list
, FILE_VIEW
, entry
)
236 VIRTUAL_DumpView( view
);
238 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
243 /***********************************************************************
246 * Find the view containing a given address. The csVirtual section must be held by caller.
255 static struct file_view
*VIRTUAL_FindView( const void *addr
)
257 struct file_view
*view
;
259 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
261 if (view
->base
> addr
) break;
262 if ((const char*)view
->base
+ view
->size
> (const char*)addr
) return view
;
268 /***********************************************************************
271 static inline UINT_PTR
get_mask( ULONG zero_bits
)
273 if (!zero_bits
) return 0xffff; /* allocations are aligned to 64K by default */
274 if (zero_bits
< page_shift
) zero_bits
= page_shift
;
275 return (1 << zero_bits
) - 1;
279 /***********************************************************************
282 * Find the first view overlapping at least part of the specified range.
283 * The csVirtual section must be held by caller.
285 static struct file_view
*find_view_range( const void *addr
, size_t size
)
287 struct file_view
*view
;
289 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
291 if ((const char *)view
->base
>= (const char *)addr
+ size
) break;
292 if ((const char *)view
->base
+ view
->size
> (const char *)addr
) return view
;
298 /***********************************************************************
301 * Find a free area between views inside the specified range.
302 * The csVirtual section must be held by caller.
304 static void *find_free_area( void *base
, void *end
, size_t size
, size_t mask
, int top_down
)
311 start
= ROUND_ADDR( (char *)end
- size
, mask
);
312 if (start
>= end
|| start
< base
) return NULL
;
314 for (ptr
= views_list
.prev
; ptr
!= &views_list
; ptr
= ptr
->prev
)
316 struct file_view
*view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
318 if ((char *)view
->base
+ view
->size
<= (char *)start
) break;
319 if ((char *)view
->base
>= (char *)start
+ size
) continue;
320 start
= ROUND_ADDR( (char *)view
->base
- size
, mask
);
321 /* stop if remaining space is not large enough */
322 if (!start
|| start
>= end
|| start
< base
) return NULL
;
327 start
= ROUND_ADDR( (char *)base
+ mask
, mask
);
328 if (start
>= end
|| (char *)end
- (char *)start
< size
) return NULL
;
330 for (ptr
= views_list
.next
; ptr
!= &views_list
; ptr
= ptr
->next
)
332 struct file_view
*view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
334 if ((char *)view
->base
>= (char *)start
+ size
) break;
335 if ((char *)view
->base
+ view
->size
<= (char *)start
) continue;
336 start
= ROUND_ADDR( (char *)view
->base
+ view
->size
+ mask
, mask
);
337 /* stop if remaining space is not large enough */
338 if (!start
|| start
>= end
|| (char *)end
- (char *)start
< size
) return NULL
;
345 /***********************************************************************
348 * Add a reserved area to the list maintained by libwine.
349 * The csVirtual section must be held by caller.
351 static void add_reserved_area( void *addr
, size_t size
)
353 TRACE( "adding %p-%p\n", addr
, (char *)addr
+ size
);
355 if (addr
< user_space_limit
)
357 /* unmap the part of the area that is below the limit */
358 assert( (char *)addr
+ size
> (char *)user_space_limit
);
359 munmap( addr
, (char *)user_space_limit
- (char *)addr
);
360 size
-= (char *)user_space_limit
- (char *)addr
;
361 addr
= user_space_limit
;
363 /* blow away existing mappings */
364 wine_anon_mmap( addr
, size
, PROT_NONE
, MAP_NORESERVE
| MAP_FIXED
);
365 wine_mmap_add_reserved_area( addr
, size
);
369 /***********************************************************************
372 * Check if an address range goes beyond a given limit.
374 static inline int is_beyond_limit( const void *addr
, size_t size
, const void *limit
)
376 return (limit
&& (addr
>= limit
|| (const char *)addr
+ size
> (const char *)limit
));
380 /***********************************************************************
383 * Unmap an area, or simply replace it by an empty mapping if it is
384 * in a reserved area. The csVirtual section must be held by caller.
386 static inline void unmap_area( void *addr
, size_t size
)
388 if (wine_mmap_is_in_reserved_area( addr
, size
))
389 wine_anon_mmap( addr
, size
, PROT_NONE
, MAP_NORESERVE
| MAP_FIXED
);
390 else if (is_beyond_limit( addr
, size
, user_space_limit
))
391 add_reserved_area( addr
, size
);
393 munmap( addr
, size
);
397 /***********************************************************************
400 * Deletes a view. The csVirtual section must be held by caller.
402 static void delete_view( struct file_view
*view
) /* [in] View */
404 if (!(view
->flags
& VFLAG_SYSTEM
)) unmap_area( view
->base
, view
->size
);
405 list_remove( &view
->entry
);
406 if (view
->mapping
) NtClose( view
->mapping
);
407 RtlFreeHeap( virtual_heap
, 0, view
);
411 /***********************************************************************
414 * Create a view. The csVirtual section must be held by caller.
416 static NTSTATUS
create_view( struct file_view
**view_ret
, void *base
, size_t size
, BYTE vprot
)
418 struct file_view
*view
;
420 int unix_prot
= VIRTUAL_GetUnixProt( vprot
);
422 assert( !((UINT_PTR
)base
& page_mask
) );
423 assert( !(size
& page_mask
) );
425 /* Create the view structure */
427 if (!(view
= RtlAllocateHeap( virtual_heap
, 0, sizeof(*view
) + (size
>> page_shift
) - 1 )))
429 FIXME( "out of memory in virtual heap for %p-%p\n", base
, (char *)base
+ size
);
430 return STATUS_NO_MEMORY
;
437 view
->protect
= vprot
;
438 memset( view
->prot
, vprot
& ~VPROT_IMAGE
, size
>> page_shift
);
440 /* Insert it in the linked list */
442 LIST_FOR_EACH( ptr
, &views_list
)
444 struct file_view
*next
= LIST_ENTRY( ptr
, struct file_view
, entry
);
445 if (next
->base
> base
) break;
447 list_add_before( ptr
, &view
->entry
);
449 /* Check for overlapping views. This can happen if the previous view
450 * was a system view that got unmapped behind our back. In that case
451 * we recover by simply deleting it. */
453 if ((ptr
= list_prev( &views_list
, &view
->entry
)) != NULL
)
455 struct file_view
*prev
= LIST_ENTRY( ptr
, struct file_view
, entry
);
456 if ((char *)prev
->base
+ prev
->size
> (char *)base
)
458 TRACE( "overlapping prev view %p-%p for %p-%p\n",
459 prev
->base
, (char *)prev
->base
+ prev
->size
,
460 base
, (char *)base
+ view
->size
);
461 assert( prev
->flags
& VFLAG_SYSTEM
);
465 if ((ptr
= list_next( &views_list
, &view
->entry
)) != NULL
)
467 struct file_view
*next
= LIST_ENTRY( ptr
, struct file_view
, entry
);
468 if ((char *)base
+ view
->size
> (char *)next
->base
)
470 TRACE( "overlapping next view %p-%p for %p-%p\n",
471 next
->base
, (char *)next
->base
+ next
->size
,
472 base
, (char *)base
+ view
->size
);
473 assert( next
->flags
& VFLAG_SYSTEM
);
479 VIRTUAL_DEBUG_DUMP_VIEW( view
);
481 if (force_exec_prot
&& (unix_prot
& PROT_READ
) && !(unix_prot
& PROT_EXEC
))
483 TRACE( "forcing exec permission on %p-%p\n", base
, (char *)base
+ size
- 1 );
484 mprotect( base
, size
, unix_prot
| PROT_EXEC
);
486 return STATUS_SUCCESS
;
490 /***********************************************************************
491 * VIRTUAL_GetWin32Prot
493 * Convert page protections to Win32 flags.
495 static DWORD
VIRTUAL_GetWin32Prot( BYTE vprot
)
497 DWORD ret
= VIRTUAL_Win32Flags
[vprot
& 0x0f];
498 if (vprot
& VPROT_NOCACHE
) ret
|= PAGE_NOCACHE
;
499 if (vprot
& VPROT_GUARD
) ret
|= PAGE_GUARD
;
504 /***********************************************************************
507 * Build page protections from Win32 flags.
510 * protect [I] Win32 protection flags
513 * Value of page protection flags
515 static BYTE
VIRTUAL_GetProt( DWORD protect
)
519 switch(protect
& 0xff)
525 vprot
= VPROT_READ
| VPROT_WRITE
;
528 /* MSDN CreateFileMapping() states that if PAGE_WRITECOPY is given,
529 * that the hFile must have been opened with GENERIC_READ and
530 * GENERIC_WRITE access. This is WRONG as tests show that you
531 * only need GENERIC_READ access (at least for Win9x,
532 * FIXME: what about NT?). Thus, we don't put VPROT_WRITE in
533 * PAGE_WRITECOPY and PAGE_EXECUTE_WRITECOPY.
535 vprot
= VPROT_READ
| VPROT_WRITECOPY
;
540 case PAGE_EXECUTE_READ
:
541 vprot
= VPROT_EXEC
| VPROT_READ
;
543 case PAGE_EXECUTE_READWRITE
:
544 vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITE
;
546 case PAGE_EXECUTE_WRITECOPY
:
547 /* See comment for PAGE_WRITECOPY above */
548 vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITECOPY
;
555 if (protect
& PAGE_GUARD
) vprot
|= VPROT_GUARD
;
556 if (protect
& PAGE_NOCACHE
) vprot
|= VPROT_NOCACHE
;
561 /***********************************************************************
564 * Change the protection of a range of pages.
570 static BOOL
VIRTUAL_SetProt( FILE_VIEW
*view
, /* [in] Pointer to view */
571 void *base
, /* [in] Starting address */
572 size_t size
, /* [in] Size in bytes */
573 BYTE vprot
) /* [in] Protections to use */
575 int unix_prot
= VIRTUAL_GetUnixProt(vprot
);
578 base
, (char *)base
+ size
- 1, VIRTUAL_GetProtStr( vprot
) );
580 /* if setting stack guard pages, store the permissions first, as the guard may be
581 * triggered at any point after mprotect and change the permissions again */
582 if ((vprot
& VPROT_GUARD
) &&
583 ((char *)base
>= (char *)NtCurrentTeb()->DeallocationStack
) &&
584 ((char *)base
< (char *)NtCurrentTeb()->Tib
.StackBase
))
586 memset( view
->prot
+ (((char *)base
- (char *)view
->base
) >> page_shift
),
587 vprot
, size
>> page_shift
);
588 mprotect( base
, size
, unix_prot
);
589 VIRTUAL_DEBUG_DUMP_VIEW( view
);
593 if (force_exec_prot
&& (unix_prot
& PROT_READ
) && !(unix_prot
& PROT_EXEC
))
595 TRACE( "forcing exec permission on %p-%p\n", base
, (char *)base
+ size
- 1 );
596 if (!mprotect( base
, size
, unix_prot
| PROT_EXEC
)) goto done
;
597 /* exec + write may legitimately fail, in that case fall back to write only */
598 if (!(unix_prot
& PROT_WRITE
)) return FALSE
;
601 if (mprotect( base
, size
, unix_prot
)) return FALSE
; /* FIXME: last error */
604 memset( view
->prot
+ (((char *)base
- (char *)view
->base
) >> page_shift
),
605 vprot
, size
>> page_shift
);
606 VIRTUAL_DEBUG_DUMP_VIEW( view
);
611 /***********************************************************************
614 * Release the extra memory while keeping the range starting on the granularity boundary.
616 static inline void *unmap_extra_space( void *ptr
, size_t total_size
, size_t wanted_size
, size_t mask
)
618 if ((ULONG_PTR
)ptr
& mask
)
620 size_t extra
= mask
+ 1 - ((ULONG_PTR
)ptr
& mask
);
621 munmap( ptr
, extra
);
622 ptr
= (char *)ptr
+ extra
;
625 if (total_size
> wanted_size
)
626 munmap( (char *)ptr
+ wanted_size
, total_size
- wanted_size
);
639 /***********************************************************************
640 * alloc_reserved_area_callback
642 * Try to map some space inside a reserved area. Callback for wine_mmap_enum_reserved_areas.
644 static int alloc_reserved_area_callback( void *start
, size_t size
, void *arg
)
646 static void * const address_space_start
= (void *)0x110000;
647 struct alloc_area
*alloc
= arg
;
648 void *end
= (char *)start
+ size
;
650 if (start
< address_space_start
) start
= address_space_start
;
651 if (user_space_limit
&& end
> user_space_limit
) end
= user_space_limit
;
652 if (start
>= end
) return 0;
654 /* make sure we don't touch the preloader reserved range */
655 if (preload_reserve_end
>= start
)
657 if (preload_reserve_end
>= end
)
659 if (preload_reserve_start
<= start
) return 0; /* no space in that area */
660 if (preload_reserve_start
< end
) end
= preload_reserve_start
;
662 else if (preload_reserve_start
<= start
) start
= preload_reserve_end
;
665 /* range is split in two by the preloader reservation, try first part */
666 if ((alloc
->result
= find_free_area( start
, preload_reserve_start
, alloc
->size
,
667 alloc
->mask
, alloc
->top_down
)))
669 /* then fall through to try second part */
670 start
= preload_reserve_end
;
673 if ((alloc
->result
= find_free_area( start
, end
, alloc
->size
, alloc
->mask
, alloc
->top_down
)))
680 /***********************************************************************
683 * Create a view and mmap the corresponding memory area.
684 * The csVirtual section must be held by caller.
686 static NTSTATUS
map_view( struct file_view
**view_ret
, void *base
, size_t size
, size_t mask
,
687 int top_down
, BYTE vprot
)
694 if (is_beyond_limit( base
, size
, address_space_limit
))
695 return STATUS_WORKING_SET_LIMIT_RANGE
;
697 switch (wine_mmap_is_in_reserved_area( base
, size
))
699 case -1: /* partially in a reserved area */
700 return STATUS_CONFLICTING_ADDRESSES
;
702 case 0: /* not in a reserved area, do a normal allocation */
703 if ((ptr
= wine_anon_mmap( base
, size
, VIRTUAL_GetUnixProt(vprot
), 0 )) == (void *)-1)
705 if (errno
== ENOMEM
) return STATUS_NO_MEMORY
;
706 return STATUS_INVALID_PARAMETER
;
710 /* We couldn't get the address we wanted */
711 if (is_beyond_limit( ptr
, size
, user_space_limit
)) add_reserved_area( ptr
, size
);
712 else munmap( ptr
, size
);
713 return STATUS_CONFLICTING_ADDRESSES
;
718 case 1: /* in a reserved area, make sure the address is available */
719 if (find_view_range( base
, size
)) return STATUS_CONFLICTING_ADDRESSES
;
720 /* replace the reserved area by our mapping */
721 if ((ptr
= wine_anon_mmap( base
, size
, VIRTUAL_GetUnixProt(vprot
), MAP_FIXED
)) != base
)
722 return STATUS_INVALID_PARAMETER
;
728 size_t view_size
= size
+ mask
+ 1;
729 struct alloc_area alloc
;
733 alloc
.top_down
= top_down
;
734 if (wine_mmap_enum_reserved_areas( alloc_reserved_area_callback
, &alloc
, top_down
))
737 TRACE( "got mem in reserved area %p-%p\n", ptr
, (char *)ptr
+ size
);
738 if (wine_anon_mmap( ptr
, size
, VIRTUAL_GetUnixProt(vprot
), MAP_FIXED
) != ptr
)
739 return STATUS_INVALID_PARAMETER
;
745 if ((ptr
= wine_anon_mmap( NULL
, view_size
, VIRTUAL_GetUnixProt(vprot
), 0 )) == (void *)-1)
747 if (errno
== ENOMEM
) return STATUS_NO_MEMORY
;
748 return STATUS_INVALID_PARAMETER
;
750 TRACE( "got mem with anon mmap %p-%p\n", ptr
, (char *)ptr
+ size
);
751 /* if we got something beyond the user limit, unmap it and retry */
752 if (is_beyond_limit( ptr
, view_size
, user_space_limit
)) add_reserved_area( ptr
, view_size
);
755 ptr
= unmap_extra_space( ptr
, view_size
, size
, mask
);
758 status
= create_view( view_ret
, ptr
, size
, vprot
);
759 if (status
!= STATUS_SUCCESS
) unmap_area( ptr
, size
);
764 /***********************************************************************
767 * Linux kernels before 2.4.x can support non page-aligned offsets, as
768 * long as the offset is aligned to the filesystem block size. This is
769 * a big performance gain so we want to take advantage of it.
771 * However, when we use 64-bit file support this doesn't work because
772 * glibc rejects unaligned offsets. Also glibc 2.1.3 mmap64 is broken
773 * in that it rounds unaligned offsets down to a page boundary. For
774 * these reasons we do a direct system call here.
776 static void *unaligned_mmap( void *addr
, size_t length
, unsigned int prot
,
777 unsigned int flags
, int fd
, off_t offset
)
779 #if defined(linux) && defined(__i386__) && defined(__GNUC__)
780 if (!(offset
>> 32) && (offset
& page_mask
))
795 args
.length
= length
;
799 args
.offset
= offset
;
801 __asm__
__volatile__("push %%ebx\n\t"
806 : "0" (90), /* SYS_mmap */
809 if (ret
< 0 && ret
> -4096)
817 return mmap( addr
, length
, prot
, flags
, fd
, offset
);
821 /***********************************************************************
824 * Wrapper for mmap() to map a file into a view, falling back to read if mmap fails.
825 * The csVirtual section must be held by caller.
827 static NTSTATUS
map_file_into_view( struct file_view
*view
, int fd
, size_t start
, size_t size
,
828 off_t offset
, BYTE vprot
, BOOL removable
)
831 int prot
= VIRTUAL_GetUnixProt( vprot
);
832 BOOL shared_write
= (vprot
& VPROT_WRITE
) != 0;
834 assert( start
< view
->size
);
835 assert( start
+ size
<= view
->size
);
837 /* only try mmap if media is not removable (or if we require write access) */
838 if (!removable
|| shared_write
)
840 int flags
= MAP_FIXED
| (shared_write
? MAP_SHARED
: MAP_PRIVATE
);
842 if (unaligned_mmap( (char *)view
->base
+ start
, size
, prot
, flags
, fd
, offset
) != (void *)-1)
845 /* mmap() failed; if this is because the file offset is not */
846 /* page-aligned (EINVAL), or because the underlying filesystem */
847 /* does not support mmap() (ENOEXEC,ENODEV), we do it by hand. */
848 if ((errno
!= ENOEXEC
) && (errno
!= EINVAL
) && (errno
!= ENODEV
)) return FILE_GetNtStatus();
849 if (shared_write
) /* we cannot fake shared write mappings */
851 if (errno
== EINVAL
) return STATUS_INVALID_PARAMETER
;
852 ERR( "shared writable mmap not supported, broken filesystem?\n" );
853 return STATUS_NOT_SUPPORTED
;
857 /* Reserve the memory with an anonymous mmap */
858 ptr
= wine_anon_mmap( (char *)view
->base
+ start
, size
, PROT_READ
| PROT_WRITE
, MAP_FIXED
);
859 if (ptr
== (void *)-1) return FILE_GetNtStatus();
860 /* Now read in the file */
861 pread( fd
, ptr
, size
, offset
);
862 if (prot
!= (PROT_READ
|PROT_WRITE
)) mprotect( ptr
, size
, prot
); /* Set the right protection */
864 memset( view
->prot
+ (start
>> page_shift
), vprot
, ROUND_SIZE(start
,size
) >> page_shift
);
865 return STATUS_SUCCESS
;
869 /***********************************************************************
872 * Decommit some pages of a given view.
873 * The csVirtual section must be held by caller.
875 static NTSTATUS
decommit_pages( struct file_view
*view
, size_t start
, size_t size
)
877 if (wine_anon_mmap( (char *)view
->base
+ start
, size
, PROT_NONE
, MAP_FIXED
) != (void *)-1)
879 BYTE
*p
= view
->prot
+ (start
>> page_shift
);
881 while (size
--) *p
++ &= ~VPROT_COMMITTED
;
882 return STATUS_SUCCESS
;
884 return FILE_GetNtStatus();
888 /***********************************************************************
891 * Map an executable (PE format) image into memory.
893 static NTSTATUS
map_image( HANDLE hmapping
, int fd
, char *base
, SIZE_T total_size
, SIZE_T mask
,
894 SIZE_T header_size
, int shared_fd
, HANDLE dup_mapping
, PVOID
*addr_ptr
)
896 IMAGE_DOS_HEADER
*dos
;
897 IMAGE_NT_HEADERS
*nt
;
898 IMAGE_SECTION_HEADER
*sec
;
899 IMAGE_DATA_DIRECTORY
*imports
;
900 NTSTATUS status
= STATUS_CONFLICTING_ADDRESSES
;
905 struct file_view
*view
= NULL
;
906 char *ptr
, *header_end
;
909 /* zero-map the whole range */
911 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
913 if (base
>= (char *)0x110000) /* make sure the DOS area remains free */
914 status
= map_view( &view
, base
, total_size
, mask
, FALSE
,
915 VPROT_COMMITTED
| VPROT_READ
| VPROT_EXEC
| VPROT_WRITECOPY
| VPROT_IMAGE
);
917 if (status
== STATUS_CONFLICTING_ADDRESSES
)
918 status
= map_view( &view
, NULL
, total_size
, mask
, FALSE
,
919 VPROT_COMMITTED
| VPROT_READ
| VPROT_EXEC
| VPROT_WRITECOPY
| VPROT_IMAGE
);
921 if (status
!= STATUS_SUCCESS
) goto error
;
924 TRACE_(module
)( "mapped PE file at %p-%p\n", ptr
, ptr
+ total_size
);
928 if (fstat( fd
, &st
) == -1)
930 status
= FILE_GetNtStatus();
933 status
= STATUS_INVALID_IMAGE_FORMAT
; /* generic error */
934 if (!st
.st_size
) goto error
;
935 header_size
= min( header_size
, st
.st_size
);
936 if (map_file_into_view( view
, fd
, 0, header_size
, 0, VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
,
937 !dup_mapping
) != STATUS_SUCCESS
) goto error
;
938 dos
= (IMAGE_DOS_HEADER
*)ptr
;
939 nt
= (IMAGE_NT_HEADERS
*)(ptr
+ dos
->e_lfanew
);
940 header_end
= ptr
+ ROUND_SIZE( 0, header_size
);
941 memset( ptr
+ header_size
, 0, header_end
- (ptr
+ header_size
) );
942 if ((char *)(nt
+ 1) > header_end
) goto error
;
943 sec
= (IMAGE_SECTION_HEADER
*)((char*)&nt
->OptionalHeader
+nt
->FileHeader
.SizeOfOptionalHeader
);
944 if ((char *)(sec
+ nt
->FileHeader
.NumberOfSections
) > header_end
) goto error
;
946 imports
= nt
->OptionalHeader
.DataDirectory
+ IMAGE_DIRECTORY_ENTRY_IMPORT
;
947 if (!imports
->Size
|| !imports
->VirtualAddress
) imports
= NULL
;
949 /* check the architecture */
951 if (nt
->FileHeader
.Machine
!= IMAGE_FILE_MACHINE_I386
)
953 MESSAGE("Trying to load PE image for unsupported architecture (");
954 switch (nt
->FileHeader
.Machine
)
956 case IMAGE_FILE_MACHINE_UNKNOWN
: MESSAGE("Unknown"); break;
957 case IMAGE_FILE_MACHINE_I860
: MESSAGE("I860"); break;
958 case IMAGE_FILE_MACHINE_R3000
: MESSAGE("R3000"); break;
959 case IMAGE_FILE_MACHINE_R4000
: MESSAGE("R4000"); break;
960 case IMAGE_FILE_MACHINE_R10000
: MESSAGE("R10000"); break;
961 case IMAGE_FILE_MACHINE_ALPHA
: MESSAGE("Alpha"); break;
962 case IMAGE_FILE_MACHINE_POWERPC
: MESSAGE("PowerPC"); break;
963 case IMAGE_FILE_MACHINE_IA64
: MESSAGE("IA-64"); break;
964 case IMAGE_FILE_MACHINE_ALPHA64
: MESSAGE("Alpha-64"); break;
965 case IMAGE_FILE_MACHINE_AMD64
: MESSAGE("AMD-64"); break;
966 case IMAGE_FILE_MACHINE_ARM
: MESSAGE("ARM"); break;
967 default: MESSAGE("Unknown-%04x", nt
->FileHeader
.Machine
); break;
973 /* check for non page-aligned binary */
975 if (nt
->OptionalHeader
.SectionAlignment
<= page_mask
)
977 /* unaligned sections, this happens for native subsystem binaries */
978 /* in that case Windows simply maps in the whole file */
980 if (map_file_into_view( view
, fd
, 0, total_size
, 0, VPROT_COMMITTED
| VPROT_READ
,
981 !dup_mapping
) != STATUS_SUCCESS
) goto error
;
983 /* check that all sections are loaded at the right offset */
984 if (nt
->OptionalHeader
.FileAlignment
!= nt
->OptionalHeader
.SectionAlignment
) goto error
;
985 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++)
987 if (sec
[i
].VirtualAddress
!= sec
[i
].PointerToRawData
)
988 goto error
; /* Windows refuses to load in that case too */
991 /* set the image protections */
992 VIRTUAL_SetProt( view
, ptr
, total_size
,
993 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
| VPROT_EXEC
);
995 /* no relocations are performed on non page-aligned binaries */
1000 /* map all the sections */
1002 for (i
= pos
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
1004 static const SIZE_T sector_align
= 0x1ff;
1005 SIZE_T map_size
, file_start
, file_size
, end
;
1007 if (!sec
->Misc
.VirtualSize
)
1008 map_size
= ROUND_SIZE( 0, sec
->SizeOfRawData
);
1010 map_size
= ROUND_SIZE( 0, sec
->Misc
.VirtualSize
);
1012 /* file positions are rounded to sector boundaries regardless of OptionalHeader.FileAlignment */
1013 file_start
= sec
->PointerToRawData
& ~sector_align
;
1014 file_size
= (sec
->SizeOfRawData
+ (sec
->PointerToRawData
& sector_align
) + sector_align
) & ~sector_align
;
1015 if (file_size
> map_size
) file_size
= map_size
;
1017 /* a few sanity checks */
1018 end
= sec
->VirtualAddress
+ ROUND_SIZE( sec
->VirtualAddress
, map_size
);
1019 if (sec
->VirtualAddress
> total_size
|| end
> total_size
|| end
< sec
->VirtualAddress
)
1021 WARN_(module
)( "Section %.8s too large (%x+%lx/%lx)\n",
1022 sec
->Name
, sec
->VirtualAddress
, map_size
, total_size
);
1026 if ((sec
->Characteristics
& IMAGE_SCN_MEM_SHARED
) &&
1027 (sec
->Characteristics
& IMAGE_SCN_MEM_WRITE
))
1029 TRACE_(module
)( "mapping shared section %.8s at %p off %x (%x) size %lx (%lx) flags %x\n",
1030 sec
->Name
, ptr
+ sec
->VirtualAddress
,
1031 sec
->PointerToRawData
, (int)pos
, file_size
, map_size
,
1032 sec
->Characteristics
);
1033 if (map_file_into_view( view
, shared_fd
, sec
->VirtualAddress
, map_size
, pos
,
1034 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITE
,
1035 FALSE
) != STATUS_SUCCESS
)
1037 ERR_(module
)( "Could not map shared section %.8s\n", sec
->Name
);
1041 /* check if the import directory falls inside this section */
1042 if (imports
&& imports
->VirtualAddress
>= sec
->VirtualAddress
&&
1043 imports
->VirtualAddress
< sec
->VirtualAddress
+ map_size
)
1045 UINT_PTR base
= imports
->VirtualAddress
& ~page_mask
;
1046 UINT_PTR end
= base
+ ROUND_SIZE( imports
->VirtualAddress
, imports
->Size
);
1047 if (end
> sec
->VirtualAddress
+ map_size
) end
= sec
->VirtualAddress
+ map_size
;
1049 map_file_into_view( view
, shared_fd
, base
, end
- base
,
1050 pos
+ (base
- sec
->VirtualAddress
),
1051 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
,
1058 TRACE_(module
)( "mapping section %.8s at %p off %x size %x virt %x flags %x\n",
1059 sec
->Name
, ptr
+ sec
->VirtualAddress
,
1060 sec
->PointerToRawData
, sec
->SizeOfRawData
,
1061 sec
->Misc
.VirtualSize
, sec
->Characteristics
);
1063 if (!sec
->PointerToRawData
|| !file_size
) continue;
1065 /* Note: if the section is not aligned properly map_file_into_view will magically
1066 * fall back to read(), so we don't need to check anything here.
1068 end
= file_start
+ file_size
;
1069 if (sec
->PointerToRawData
>= st
.st_size
||
1070 end
> ((st
.st_size
+ sector_align
) & ~sector_align
) ||
1072 map_file_into_view( view
, fd
, sec
->VirtualAddress
, file_size
, file_start
,
1073 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
,
1074 !dup_mapping
) != STATUS_SUCCESS
)
1076 ERR_(module
)( "Could not map section %.8s, file probably truncated\n", sec
->Name
);
1080 if (file_size
& page_mask
)
1082 end
= ROUND_SIZE( 0, file_size
);
1083 if (end
> map_size
) end
= map_size
;
1084 TRACE_(module
)("clearing %p - %p\n",
1085 ptr
+ sec
->VirtualAddress
+ file_size
,
1086 ptr
+ sec
->VirtualAddress
+ end
);
1087 memset( ptr
+ sec
->VirtualAddress
+ file_size
, 0, end
- file_size
);
1092 /* perform base relocation, if necessary */
1095 ((nt
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
) ||
1096 !NtCurrentTeb()->Peb
->ImageBaseAddress
) )
1098 IMAGE_BASE_RELOCATION
*rel
, *end
;
1099 const IMAGE_DATA_DIRECTORY
*relocs
;
1101 if (nt
->FileHeader
.Characteristics
& IMAGE_FILE_RELOCS_STRIPPED
)
1103 WARN_(module
)( "Need to relocate module from %p to %p, but there are no relocation records\n",
1105 status
= STATUS_CONFLICTING_ADDRESSES
;
1109 TRACE_(module
)( "relocating from %p-%p to %p-%p\n",
1110 base
, base
+ total_size
, ptr
, ptr
+ total_size
);
1112 relocs
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1113 rel
= (IMAGE_BASE_RELOCATION
*)(ptr
+ relocs
->VirtualAddress
);
1114 end
= (IMAGE_BASE_RELOCATION
*)(ptr
+ relocs
->VirtualAddress
+ relocs
->Size
);
1117 while (rel
< end
- 1 && rel
->SizeOfBlock
)
1119 if (rel
->VirtualAddress
>= total_size
)
1121 WARN_(module
)( "invalid address %p in relocation %p\n", ptr
+ rel
->VirtualAddress
, rel
);
1122 status
= STATUS_ACCESS_VIOLATION
;
1125 rel
= LdrProcessRelocationBlock( ptr
+ rel
->VirtualAddress
,
1126 (rel
->SizeOfBlock
- sizeof(*rel
)) / sizeof(USHORT
),
1127 (USHORT
*)(rel
+ 1), delta
);
1128 if (!rel
) goto error
;
1132 /* set the image protections */
1134 VIRTUAL_SetProt( view
, ptr
, ROUND_SIZE( 0, header_size
), VPROT_COMMITTED
| VPROT_READ
);
1136 sec
= (IMAGE_SECTION_HEADER
*)((char *)&nt
->OptionalHeader
+nt
->FileHeader
.SizeOfOptionalHeader
);
1137 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
1140 BYTE vprot
= VPROT_COMMITTED
;
1142 if (sec
->Misc
.VirtualSize
)
1143 size
= ROUND_SIZE( sec
->VirtualAddress
, sec
->Misc
.VirtualSize
);
1145 size
= ROUND_SIZE( sec
->VirtualAddress
, sec
->SizeOfRawData
);
1147 if (sec
->Characteristics
& IMAGE_SCN_MEM_READ
) vprot
|= VPROT_READ
;
1148 if (sec
->Characteristics
& IMAGE_SCN_MEM_WRITE
) vprot
|= VPROT_READ
|VPROT_WRITECOPY
;
1149 if (sec
->Characteristics
& IMAGE_SCN_MEM_EXECUTE
) vprot
|= VPROT_EXEC
;
1151 /* Dumb game crack lets the AOEP point into a data section. Adjust. */
1152 if ((nt
->OptionalHeader
.AddressOfEntryPoint
>= sec
->VirtualAddress
) &&
1153 (nt
->OptionalHeader
.AddressOfEntryPoint
< sec
->VirtualAddress
+ size
))
1154 vprot
|= VPROT_EXEC
;
1156 VIRTUAL_SetProt( view
, ptr
+ sec
->VirtualAddress
, size
, vprot
);
1160 view
->mapping
= dup_mapping
;
1161 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1164 #ifdef VALGRIND_LOAD_PDB_DEBUGINFO
1165 VALGRIND_LOAD_PDB_DEBUGINFO(fd
, ptr
, total_size
, delta
);
1167 return STATUS_SUCCESS
;
1170 if (view
) delete_view( view
);
1171 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1172 if (dup_mapping
) NtClose( dup_mapping
);
1177 /* callback for wine_mmap_enum_reserved_areas to allocate space for the virtual heap */
1178 static int alloc_virtual_heap( void *base
, size_t size
, void *arg
)
1180 void **heap_base
= arg
;
1182 if (address_space_limit
) address_space_limit
= max( (char *)address_space_limit
, (char *)base
+ size
);
1183 if (size
< VIRTUAL_HEAP_SIZE
) return 0;
1184 *heap_base
= wine_anon_mmap( (char *)base
+ size
- VIRTUAL_HEAP_SIZE
,
1185 VIRTUAL_HEAP_SIZE
, PROT_READ
|PROT_WRITE
, MAP_FIXED
);
1186 return (*heap_base
!= (void *)-1);
1189 /***********************************************************************
1192 void virtual_init(void)
1194 const char *preload
;
1196 struct file_view
*heap_view
;
1199 page_size
= getpagesize();
1200 page_mask
= page_size
- 1;
1201 /* Make sure we have a power of 2 */
1202 assert( !(page_size
& page_mask
) );
1204 while ((1 << page_shift
) != page_size
) page_shift
++;
1205 #endif /* page_mask */
1206 if ((preload
= getenv("WINEPRELOADRESERVE")))
1208 unsigned long start
, end
;
1209 if (sscanf( preload
, "%lx-%lx", &start
, &end
) == 2)
1211 preload_reserve_start
= (void *)start
;
1212 preload_reserve_end
= (void *)end
;
1216 /* try to find space in a reserved area for the virtual heap */
1217 if (!wine_mmap_enum_reserved_areas( alloc_virtual_heap
, &heap_base
, 1 ))
1218 heap_base
= wine_anon_mmap( NULL
, VIRTUAL_HEAP_SIZE
, PROT_READ
|PROT_WRITE
, 0 );
1220 assert( heap_base
!= (void *)-1 );
1221 virtual_heap
= RtlCreateHeap( HEAP_NO_SERIALIZE
, heap_base
, VIRTUAL_HEAP_SIZE
,
1222 VIRTUAL_HEAP_SIZE
, NULL
, NULL
);
1223 create_view( &heap_view
, heap_base
, VIRTUAL_HEAP_SIZE
, VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITE
);
1227 /***********************************************************************
1228 * virtual_init_threading
1230 void virtual_init_threading(void)
1236 /***********************************************************************
1237 * virtual_alloc_thread_stack
1239 NTSTATUS
virtual_alloc_thread_stack( void *base
, SIZE_T size
)
1245 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1247 if (base
) /* already allocated, create a system view */
1249 size
= ROUND_SIZE( base
, size
);
1250 base
= ROUND_ADDR( base
, page_mask
);
1251 if ((status
= create_view( &view
, base
, size
,
1252 VPROT_READ
| VPROT_WRITE
| VPROT_COMMITTED
)) != STATUS_SUCCESS
)
1254 view
->flags
|= VFLAG_VALLOC
| VFLAG_SYSTEM
;
1258 size
= (size
+ 0xffff) & ~0xffff; /* round to 64K boundary */
1259 if ((status
= map_view( &view
, NULL
, size
, 0xffff, 0,
1260 VPROT_READ
| VPROT_WRITE
| VPROT_COMMITTED
)) != STATUS_SUCCESS
)
1262 view
->flags
|= VFLAG_VALLOC
;
1263 #ifdef VALGRIND_STACK_REGISTER
1264 /* no need to de-register the stack as it's the one of the main thread */
1265 VALGRIND_STACK_REGISTER( view
->base
, (char *)view
->base
+ view
->size
);
1269 /* setup no access guard page */
1270 VIRTUAL_SetProt( view
, view
->base
, page_size
, VPROT_COMMITTED
);
1271 VIRTUAL_SetProt( view
, (char *)view
->base
+ page_size
, page_size
,
1272 VPROT_READ
| VPROT_WRITE
| VPROT_COMMITTED
| VPROT_GUARD
);
1274 /* note: limit is lower than base since the stack grows down */
1275 NtCurrentTeb()->DeallocationStack
= view
->base
;
1276 NtCurrentTeb()->Tib
.StackBase
= (char *)view
->base
+ view
->size
;
1277 NtCurrentTeb()->Tib
.StackLimit
= (char *)view
->base
+ 2 * page_size
;
1280 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1285 /***********************************************************************
1286 * virtual_clear_thread_stack
1288 * Clear the stack contents before calling the main entry point, some broken apps need that.
1290 void virtual_clear_thread_stack(void)
1292 void *stack
= NtCurrentTeb()->Tib
.StackLimit
;
1293 size_t size
= (char *)NtCurrentTeb()->Tib
.StackBase
- (char *)NtCurrentTeb()->Tib
.StackLimit
;
1295 wine_anon_mmap( stack
, size
, PROT_READ
| PROT_WRITE
, MAP_FIXED
);
1296 if (force_exec_prot
) mprotect( stack
, size
, PROT_READ
| PROT_WRITE
| PROT_EXEC
);
1300 /***********************************************************************
1301 * VIRTUAL_HandleFault
1303 NTSTATUS
VIRTUAL_HandleFault( LPCVOID addr
)
1306 NTSTATUS ret
= STATUS_ACCESS_VIOLATION
;
1309 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1310 if ((view
= VIRTUAL_FindView( addr
)))
1312 void *page
= ROUND_ADDR( addr
, page_mask
);
1313 BYTE vprot
= view
->prot
[((const char *)page
- (const char *)view
->base
) >> page_shift
];
1314 if (vprot
& VPROT_GUARD
)
1316 VIRTUAL_SetProt( view
, page
, page_size
, vprot
& ~VPROT_GUARD
);
1317 ret
= STATUS_GUARD_PAGE_VIOLATION
;
1320 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1326 /***********************************************************************
1327 * virtual_handle_stack_fault
1329 * Handle an access fault inside the current thread stack.
1330 * Called from inside a signal handler.
1332 BOOL
virtual_handle_stack_fault( void *addr
)
1337 RtlEnterCriticalSection( &csVirtual
); /* no need for signal masking inside signal handler */
1338 if ((view
= VIRTUAL_FindView( addr
)))
1340 void *page
= ROUND_ADDR( addr
, page_mask
);
1341 BYTE vprot
= view
->prot
[((const char *)page
- (const char *)view
->base
) >> page_shift
];
1342 if (vprot
& VPROT_GUARD
)
1344 VIRTUAL_SetProt( view
, page
, page_size
, vprot
& ~VPROT_GUARD
);
1345 if ((char *)page
+ page_size
== NtCurrentTeb()->Tib
.StackLimit
)
1346 NtCurrentTeb()->Tib
.StackLimit
= page
;
1350 RtlLeaveCriticalSection( &csVirtual
);
1355 /***********************************************************************
1356 * VIRTUAL_SetForceExec
1358 * Whether to force exec prot on all views.
1360 void VIRTUAL_SetForceExec( BOOL enable
)
1362 struct file_view
*view
;
1365 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1366 if (!force_exec_prot
!= !enable
) /* change all existing views */
1368 force_exec_prot
= enable
;
1370 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
1374 char *addr
= view
->base
;
1375 BYTE prot
= view
->prot
[0];
1377 for (count
= i
= 1; i
< view
->size
>> page_shift
; i
++, count
++)
1379 if (view
->prot
[i
] == prot
) continue;
1380 unix_prot
= VIRTUAL_GetUnixProt( prot
);
1381 if ((unix_prot
& PROT_READ
) && !(unix_prot
& PROT_EXEC
))
1383 TRACE( "%s exec prot for %p-%p\n",
1384 force_exec_prot
? "enabling" : "disabling",
1385 addr
, addr
+ (count
<< page_shift
) - 1 );
1386 mprotect( addr
, count
<< page_shift
,
1387 unix_prot
| (force_exec_prot
? PROT_EXEC
: 0) );
1389 addr
+= (count
<< page_shift
);
1390 prot
= view
->prot
[i
];
1395 unix_prot
= VIRTUAL_GetUnixProt( prot
);
1396 if ((unix_prot
& PROT_READ
) && !(unix_prot
& PROT_EXEC
))
1398 TRACE( "%s exec prot for %p-%p\n",
1399 force_exec_prot
? "enabling" : "disabling",
1400 addr
, addr
+ (count
<< page_shift
) - 1 );
1401 mprotect( addr
, count
<< page_shift
,
1402 unix_prot
| (force_exec_prot
? PROT_EXEC
: 0) );
1407 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1411 /***********************************************************************
1412 * VIRTUAL_UseLargeAddressSpace
1414 * Increase the address space size for apps that support it.
1416 void VIRTUAL_UseLargeAddressSpace(void)
1418 /* no large address space on win9x */
1419 if (NtCurrentTeb()->Peb
->OSPlatformId
!= VER_PLATFORM_WIN32_NT
) return;
1420 user_space_limit
= address_space_limit
;
1424 /***********************************************************************
1425 * NtAllocateVirtualMemory (NTDLL.@)
1426 * ZwAllocateVirtualMemory (NTDLL.@)
1428 NTSTATUS WINAPI
NtAllocateVirtualMemory( HANDLE process
, PVOID
*ret
, ULONG zero_bits
,
1429 SIZE_T
*size_ptr
, ULONG type
, ULONG protect
)
1433 SIZE_T size
= *size_ptr
;
1434 SIZE_T mask
= get_mask( zero_bits
);
1435 NTSTATUS status
= STATUS_SUCCESS
;
1436 struct file_view
*view
;
1439 TRACE("%p %p %08lx %x %08x\n", process
, *ret
, size
, type
, protect
);
1441 if (!size
) return STATUS_INVALID_PARAMETER
;
1443 if (process
!= NtCurrentProcess())
1446 apc_result_t result
;
1448 memset( &call
, 0, sizeof(call
) );
1450 call
.virtual_alloc
.type
= APC_VIRTUAL_ALLOC
;
1451 call
.virtual_alloc
.addr
= *ret
;
1452 call
.virtual_alloc
.size
= *size_ptr
;
1453 call
.virtual_alloc
.zero_bits
= zero_bits
;
1454 call
.virtual_alloc
.op_type
= type
;
1455 call
.virtual_alloc
.prot
= protect
;
1456 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
1457 if (status
!= STATUS_SUCCESS
) return status
;
1459 if (result
.virtual_alloc
.status
== STATUS_SUCCESS
)
1461 *ret
= result
.virtual_alloc
.addr
;
1462 *size_ptr
= result
.virtual_alloc
.size
;
1464 return result
.virtual_alloc
.status
;
1467 /* Round parameters to a page boundary */
1469 if (size
> 0x7fc00000) return STATUS_WORKING_SET_LIMIT_RANGE
; /* 2Gb - 4Mb */
1473 if (type
& MEM_RESERVE
) /* Round down to 64k boundary */
1474 base
= ROUND_ADDR( *ret
, mask
);
1476 base
= ROUND_ADDR( *ret
, page_mask
);
1477 size
= (((UINT_PTR
)*ret
+ size
+ page_mask
) & ~page_mask
) - (UINT_PTR
)base
;
1479 /* disallow low 64k, wrap-around and kernel space */
1480 if (((char *)base
< (char *)0x10000) ||
1481 ((char *)base
+ size
< (char *)base
) ||
1482 is_beyond_limit( base
, size
, address_space_limit
))
1483 return STATUS_INVALID_PARAMETER
;
1488 size
= (size
+ page_mask
) & ~page_mask
;
1491 /* Compute the alloc type flags */
1493 if (!(type
& MEM_SYSTEM
))
1495 if (!(type
& (MEM_COMMIT
| MEM_RESERVE
)) ||
1496 (type
& ~(MEM_COMMIT
| MEM_RESERVE
| MEM_TOP_DOWN
| MEM_WRITE_WATCH
| MEM_RESET
)))
1498 WARN("called with wrong alloc type flags (%08x) !\n", type
);
1499 return STATUS_INVALID_PARAMETER
;
1501 if (type
& MEM_WRITE_WATCH
)
1503 FIXME("MEM_WRITE_WATCH type not supported\n");
1504 return STATUS_NOT_SUPPORTED
;
1507 vprot
= VIRTUAL_GetProt( protect
);
1508 if (type
& MEM_COMMIT
) vprot
|= VPROT_COMMITTED
;
1510 /* Reserve the memory */
1512 if (use_locks
) server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1514 if (type
& MEM_SYSTEM
)
1516 if (type
& MEM_IMAGE
) vprot
|= VPROT_IMAGE
;
1517 status
= create_view( &view
, base
, size
, vprot
| VPROT_COMMITTED
);
1518 if (status
== STATUS_SUCCESS
)
1520 view
->flags
|= VFLAG_VALLOC
| VFLAG_SYSTEM
;
1524 else if ((type
& MEM_RESERVE
) || !base
)
1526 status
= map_view( &view
, base
, size
, mask
, type
& MEM_TOP_DOWN
, vprot
);
1527 if (status
== STATUS_SUCCESS
)
1529 view
->flags
|= VFLAG_VALLOC
;
1533 else /* commit the pages */
1535 if (!(view
= VIRTUAL_FindView( base
)) ||
1536 ((char *)base
+ size
> (char *)view
->base
+ view
->size
)) status
= STATUS_NOT_MAPPED_VIEW
;
1537 else if (!VIRTUAL_SetProt( view
, base
, size
, vprot
)) status
= STATUS_ACCESS_DENIED
;
1540 if (use_locks
) server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1542 if (status
== STATUS_SUCCESS
)
1551 /***********************************************************************
1552 * NtFreeVirtualMemory (NTDLL.@)
1553 * ZwFreeVirtualMemory (NTDLL.@)
1555 NTSTATUS WINAPI
NtFreeVirtualMemory( HANDLE process
, PVOID
*addr_ptr
, SIZE_T
*size_ptr
, ULONG type
)
1560 NTSTATUS status
= STATUS_SUCCESS
;
1561 LPVOID addr
= *addr_ptr
;
1562 SIZE_T size
= *size_ptr
;
1564 TRACE("%p %p %08lx %x\n", process
, addr
, size
, type
);
1566 if (process
!= NtCurrentProcess())
1569 apc_result_t result
;
1571 memset( &call
, 0, sizeof(call
) );
1573 call
.virtual_free
.type
= APC_VIRTUAL_FREE
;
1574 call
.virtual_free
.addr
= addr
;
1575 call
.virtual_free
.size
= size
;
1576 call
.virtual_free
.op_type
= type
;
1577 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
1578 if (status
!= STATUS_SUCCESS
) return status
;
1580 if (result
.virtual_free
.status
== STATUS_SUCCESS
)
1582 *addr_ptr
= result
.virtual_free
.addr
;
1583 *size_ptr
= result
.virtual_free
.size
;
1585 return result
.virtual_free
.status
;
1588 /* Fix the parameters */
1590 size
= ROUND_SIZE( addr
, size
);
1591 base
= ROUND_ADDR( addr
, page_mask
);
1593 /* avoid freeing the DOS area when a broken app passes a NULL pointer */
1594 if (!base
&& !(type
& MEM_SYSTEM
)) return STATUS_INVALID_PARAMETER
;
1596 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1598 if (!(view
= VIRTUAL_FindView( base
)) ||
1599 (base
+ size
> (char *)view
->base
+ view
->size
) ||
1600 !(view
->flags
& VFLAG_VALLOC
))
1602 status
= STATUS_INVALID_PARAMETER
;
1604 else if (type
& MEM_SYSTEM
)
1606 /* return the values that the caller should use to unmap the area */
1607 *addr_ptr
= view
->base
;
1608 if (!wine_mmap_is_in_reserved_area( view
->base
, view
->size
)) *size_ptr
= view
->size
;
1609 else *size_ptr
= 0; /* make sure we don't munmap anything from a reserved area */
1610 view
->flags
|= VFLAG_SYSTEM
;
1611 delete_view( view
);
1613 else if (type
== MEM_RELEASE
)
1615 /* Free the pages */
1617 if (size
|| (base
!= view
->base
)) status
= STATUS_INVALID_PARAMETER
;
1620 delete_view( view
);
1625 else if (type
== MEM_DECOMMIT
)
1627 status
= decommit_pages( view
, base
- (char *)view
->base
, size
);
1628 if (status
== STATUS_SUCCESS
)
1636 WARN("called with wrong free type flags (%08x) !\n", type
);
1637 status
= STATUS_INVALID_PARAMETER
;
1640 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1645 /***********************************************************************
1646 * NtProtectVirtualMemory (NTDLL.@)
1647 * ZwProtectVirtualMemory (NTDLL.@)
1649 NTSTATUS WINAPI
NtProtectVirtualMemory( HANDLE process
, PVOID
*addr_ptr
, SIZE_T
*size_ptr
,
1650 ULONG new_prot
, ULONG
*old_prot
)
1654 NTSTATUS status
= STATUS_SUCCESS
;
1659 SIZE_T size
= *size_ptr
;
1660 LPVOID addr
= *addr_ptr
;
1662 TRACE("%p %p %08lx %08x\n", process
, addr
, size
, new_prot
);
1664 if (process
!= NtCurrentProcess())
1667 apc_result_t result
;
1669 memset( &call
, 0, sizeof(call
) );
1671 call
.virtual_protect
.type
= APC_VIRTUAL_PROTECT
;
1672 call
.virtual_protect
.addr
= addr
;
1673 call
.virtual_protect
.size
= size
;
1674 call
.virtual_protect
.prot
= new_prot
;
1675 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
1676 if (status
!= STATUS_SUCCESS
) return status
;
1678 if (result
.virtual_protect
.status
== STATUS_SUCCESS
)
1680 *addr_ptr
= result
.virtual_protect
.addr
;
1681 *size_ptr
= result
.virtual_protect
.size
;
1682 if (old_prot
) *old_prot
= result
.virtual_protect
.prot
;
1684 return result
.virtual_protect
.status
;
1687 /* Fix the parameters */
1689 size
= ROUND_SIZE( addr
, size
);
1690 base
= ROUND_ADDR( addr
, page_mask
);
1692 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1694 if (!(view
= VIRTUAL_FindView( base
)) || (base
+ size
> (char *)view
->base
+ view
->size
))
1696 status
= STATUS_INVALID_PARAMETER
;
1700 /* Make sure all the pages are committed */
1702 p
= view
->prot
+ ((base
- (char *)view
->base
) >> page_shift
);
1703 prot
= VIRTUAL_GetWin32Prot( *p
);
1704 for (i
= size
>> page_shift
; i
; i
--, p
++)
1706 if (!(*p
& VPROT_COMMITTED
))
1708 status
= STATUS_NOT_COMMITTED
;
1714 if (old_prot
) *old_prot
= prot
;
1715 vprot
= VIRTUAL_GetProt( new_prot
) | VPROT_COMMITTED
;
1716 if (!VIRTUAL_SetProt( view
, base
, size
, vprot
)) status
= STATUS_ACCESS_DENIED
;
1719 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1721 if (status
== STATUS_SUCCESS
)
1729 #define UNIMPLEMENTED_INFO_CLASS(c) \
1731 FIXME("(process=%p,addr=%p) Unimplemented information class: " #c "\n", process, addr); \
1732 return STATUS_INVALID_INFO_CLASS
1734 /***********************************************************************
1735 * NtQueryVirtualMemory (NTDLL.@)
1736 * ZwQueryVirtualMemory (NTDLL.@)
1738 NTSTATUS WINAPI
NtQueryVirtualMemory( HANDLE process
, LPCVOID addr
,
1739 MEMORY_INFORMATION_CLASS info_class
, PVOID buffer
,
1740 SIZE_T len
, SIZE_T
*res_len
)
1743 char *base
, *alloc_base
= 0;
1746 MEMORY_BASIC_INFORMATION
*info
= buffer
;
1749 if (info_class
!= MemoryBasicInformation
)
1753 UNIMPLEMENTED_INFO_CLASS(MemoryWorkingSetList
);
1754 UNIMPLEMENTED_INFO_CLASS(MemorySectionName
);
1755 UNIMPLEMENTED_INFO_CLASS(MemoryBasicVlmInformation
);
1758 FIXME("(%p,%p,info_class=%d,%p,%ld,%p) Unknown information class\n",
1759 process
, addr
, info_class
, buffer
, len
, res_len
);
1760 return STATUS_INVALID_INFO_CLASS
;
1763 if (address_space_limit
&& addr
>= address_space_limit
)
1764 return STATUS_WORKING_SET_LIMIT_RANGE
;
1766 if (process
!= NtCurrentProcess())
1770 apc_result_t result
;
1772 memset( &call
, 0, sizeof(call
) );
1774 call
.virtual_query
.type
= APC_VIRTUAL_QUERY
;
1775 call
.virtual_query
.addr
= addr
;
1776 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
1777 if (status
!= STATUS_SUCCESS
) return status
;
1779 if (result
.virtual_query
.status
== STATUS_SUCCESS
)
1781 info
->BaseAddress
= result
.virtual_query
.base
;
1782 info
->AllocationBase
= result
.virtual_query
.alloc_base
;
1783 info
->RegionSize
= result
.virtual_query
.size
;
1784 info
->State
= result
.virtual_query
.state
;
1785 info
->Protect
= result
.virtual_query
.prot
;
1786 info
->AllocationProtect
= result
.virtual_query
.alloc_prot
;
1787 info
->Type
= result
.virtual_query
.alloc_type
;
1788 if (res_len
) *res_len
= sizeof(*info
);
1790 return result
.virtual_query
.status
;
1793 base
= ROUND_ADDR( addr
, page_mask
);
1795 /* Find the view containing the address */
1797 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1798 ptr
= list_head( &views_list
);
1803 /* make the address space end at the user limit, except if
1804 * the last view was mapped beyond that */
1805 if (alloc_base
<= (char *)user_space_limit
)
1807 if (user_space_limit
&& base
>= (char *)user_space_limit
)
1809 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1810 return STATUS_WORKING_SET_LIMIT_RANGE
;
1812 size
= (char *)user_space_limit
- alloc_base
;
1814 else size
= (char *)address_space_limit
- alloc_base
;
1818 view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
1819 if ((char *)view
->base
> base
)
1821 size
= (char *)view
->base
- alloc_base
;
1825 if ((char *)view
->base
+ view
->size
> base
)
1827 alloc_base
= view
->base
;
1831 alloc_base
= (char *)view
->base
+ view
->size
;
1832 ptr
= list_next( &views_list
, ptr
);
1835 /* Fill the info structure */
1839 info
->State
= MEM_FREE
;
1840 info
->Protect
= PAGE_NOACCESS
;
1841 info
->AllocationBase
= 0;
1842 info
->AllocationProtect
= 0;
1847 BYTE vprot
= view
->prot
[(base
- alloc_base
) >> page_shift
];
1848 info
->State
= (vprot
& VPROT_COMMITTED
) ? MEM_COMMIT
: MEM_RESERVE
;
1849 info
->Protect
= VIRTUAL_GetWin32Prot( vprot
);
1850 info
->AllocationBase
= alloc_base
;
1851 info
->AllocationProtect
= VIRTUAL_GetWin32Prot( view
->protect
);
1852 if (view
->protect
& VPROT_IMAGE
) info
->Type
= MEM_IMAGE
;
1853 else if (view
->flags
& VFLAG_VALLOC
) info
->Type
= MEM_PRIVATE
;
1854 else info
->Type
= MEM_MAPPED
;
1855 for (size
= base
- alloc_base
; size
< view
->size
; size
+= page_size
)
1856 if (view
->prot
[size
>> page_shift
] != vprot
) break;
1858 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1860 info
->BaseAddress
= base
;
1861 info
->RegionSize
= size
- (base
- alloc_base
);
1862 if (res_len
) *res_len
= sizeof(*info
);
1863 return STATUS_SUCCESS
;
1867 /***********************************************************************
1868 * NtLockVirtualMemory (NTDLL.@)
1869 * ZwLockVirtualMemory (NTDLL.@)
1871 NTSTATUS WINAPI
NtLockVirtualMemory( HANDLE process
, PVOID
*addr
, SIZE_T
*size
, ULONG unknown
)
1873 NTSTATUS status
= STATUS_SUCCESS
;
1875 if (process
!= NtCurrentProcess())
1878 apc_result_t result
;
1880 memset( &call
, 0, sizeof(call
) );
1882 call
.virtual_lock
.type
= APC_VIRTUAL_LOCK
;
1883 call
.virtual_lock
.addr
= *addr
;
1884 call
.virtual_lock
.size
= *size
;
1885 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
1886 if (status
!= STATUS_SUCCESS
) return status
;
1888 if (result
.virtual_lock
.status
== STATUS_SUCCESS
)
1890 *addr
= result
.virtual_lock
.addr
;
1891 *size
= result
.virtual_lock
.size
;
1893 return result
.virtual_lock
.status
;
1896 *size
= ROUND_SIZE( *addr
, *size
);
1897 *addr
= ROUND_ADDR( *addr
, page_mask
);
1899 if (mlock( *addr
, *size
)) status
= STATUS_ACCESS_DENIED
;
1904 /***********************************************************************
1905 * NtUnlockVirtualMemory (NTDLL.@)
1906 * ZwUnlockVirtualMemory (NTDLL.@)
1908 NTSTATUS WINAPI
NtUnlockVirtualMemory( HANDLE process
, PVOID
*addr
, SIZE_T
*size
, ULONG unknown
)
1910 NTSTATUS status
= STATUS_SUCCESS
;
1912 if (process
!= NtCurrentProcess())
1915 apc_result_t result
;
1917 memset( &call
, 0, sizeof(call
) );
1919 call
.virtual_unlock
.type
= APC_VIRTUAL_UNLOCK
;
1920 call
.virtual_unlock
.addr
= *addr
;
1921 call
.virtual_unlock
.size
= *size
;
1922 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
1923 if (status
!= STATUS_SUCCESS
) return status
;
1925 if (result
.virtual_unlock
.status
== STATUS_SUCCESS
)
1927 *addr
= result
.virtual_unlock
.addr
;
1928 *size
= result
.virtual_unlock
.size
;
1930 return result
.virtual_unlock
.status
;
1933 *size
= ROUND_SIZE( *addr
, *size
);
1934 *addr
= ROUND_ADDR( *addr
, page_mask
);
1936 if (munlock( *addr
, *size
)) status
= STATUS_ACCESS_DENIED
;
1941 /***********************************************************************
1942 * NtCreateSection (NTDLL.@)
1943 * ZwCreateSection (NTDLL.@)
1945 NTSTATUS WINAPI
NtCreateSection( HANDLE
*handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
,
1946 const LARGE_INTEGER
*size
, ULONG protect
,
1947 ULONG sec_flags
, HANDLE file
)
1951 DWORD len
= (attr
&& attr
->ObjectName
) ? attr
->ObjectName
->Length
: 0;
1952 struct security_descriptor
*sd
= NULL
;
1953 struct object_attributes objattr
;
1955 /* Check parameters */
1957 if (len
> MAX_PATH
*sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
1959 objattr
.rootdir
= attr
? attr
->RootDirectory
: 0;
1961 objattr
.name_len
= len
;
1964 ret
= NTDLL_create_struct_sd( attr
->SecurityDescriptor
, &sd
, &objattr
.sd_len
);
1965 if (ret
!= STATUS_SUCCESS
) return ret
;
1968 vprot
= VIRTUAL_GetProt( protect
);
1969 if (sec_flags
& SEC_RESERVE
)
1971 if (file
) return STATUS_INVALID_PARAMETER
;
1973 else vprot
|= VPROT_COMMITTED
;
1974 if (sec_flags
& SEC_NOCACHE
) vprot
|= VPROT_NOCACHE
;
1975 if (sec_flags
& SEC_IMAGE
) vprot
|= VPROT_IMAGE
;
1977 /* Create the server object */
1979 SERVER_START_REQ( create_mapping
)
1981 req
->access
= access
;
1982 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
1983 req
->file_handle
= file
;
1984 req
->size
= size
? size
->QuadPart
: 0;
1985 req
->protect
= vprot
;
1986 wine_server_add_data( req
, &objattr
, sizeof(objattr
) );
1987 if (objattr
.sd_len
) wine_server_add_data( req
, sd
, objattr
.sd_len
);
1988 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
1989 ret
= wine_server_call( req
);
1990 *handle
= reply
->handle
;
1994 NTDLL_free_struct_sd( sd
);
2000 /***********************************************************************
2001 * NtOpenSection (NTDLL.@)
2002 * ZwOpenSection (NTDLL.@)
2004 NTSTATUS WINAPI
NtOpenSection( HANDLE
*handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
)
2007 DWORD len
= attr
->ObjectName
->Length
;
2009 if (len
> MAX_PATH
*sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
2011 SERVER_START_REQ( open_mapping
)
2013 req
->access
= access
;
2014 req
->attributes
= attr
->Attributes
;
2015 req
->rootdir
= attr
->RootDirectory
;
2016 wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
2017 if (!(ret
= wine_server_call( req
))) *handle
= reply
->handle
;
2024 /***********************************************************************
2025 * NtMapViewOfSection (NTDLL.@)
2026 * ZwMapViewOfSection (NTDLL.@)
2028 NTSTATUS WINAPI
NtMapViewOfSection( HANDLE handle
, HANDLE process
, PVOID
*addr_ptr
, ULONG zero_bits
,
2029 SIZE_T commit_size
, const LARGE_INTEGER
*offset_ptr
, SIZE_T
*size_ptr
,
2030 SECTION_INHERIT inherit
, ULONG alloc_type
, ULONG protect
)
2033 ULONGLONG full_size
;
2035 SIZE_T mask
= get_mask( zero_bits
);
2036 int unix_handle
= -1, needs_close
;
2039 struct file_view
*view
;
2041 HANDLE dup_mapping
, shared_file
;
2042 LARGE_INTEGER offset
;
2045 offset
.QuadPart
= offset_ptr
? offset_ptr
->QuadPart
: 0;
2047 TRACE("handle=%p process=%p addr=%p off=%x%08x size=%lx access=%x\n",
2048 handle
, process
, *addr_ptr
, offset
.u
.HighPart
, offset
.u
.LowPart
, size
, protect
);
2050 /* Check parameters */
2052 if ((offset
.u
.LowPart
& mask
) || (*addr_ptr
&& ((UINT_PTR
)*addr_ptr
& mask
)))
2053 return STATUS_INVALID_PARAMETER
;
2055 if (process
!= NtCurrentProcess())
2058 apc_result_t result
;
2060 memset( &call
, 0, sizeof(call
) );
2062 call
.map_view
.type
= APC_MAP_VIEW
;
2063 call
.map_view
.handle
= handle
;
2064 call
.map_view
.addr
= *addr_ptr
;
2065 call
.map_view
.size
= *size_ptr
;
2066 call
.map_view
.offset
= offset
.QuadPart
;
2067 call
.map_view
.zero_bits
= zero_bits
;
2068 call
.map_view
.alloc_type
= alloc_type
;
2069 call
.map_view
.prot
= protect
;
2070 res
= NTDLL_queue_process_apc( process
, &call
, &result
);
2071 if (res
!= STATUS_SUCCESS
) return res
;
2073 if (result
.map_view
.status
== STATUS_SUCCESS
)
2075 *addr_ptr
= result
.map_view
.addr
;
2076 *size_ptr
= result
.map_view
.size
;
2078 return result
.map_view
.status
;
2081 SERVER_START_REQ( get_mapping_info
)
2083 req
->handle
= handle
;
2084 res
= wine_server_call( req
);
2085 prot
= reply
->protect
;
2087 full_size
= reply
->size
;
2088 header_size
= reply
->header_size
;
2089 dup_mapping
= reply
->mapping
;
2090 shared_file
= reply
->shared_file
;
2093 if (res
) return res
;
2096 if (sizeof(size
) < sizeof(full_size
) && (size
!= full_size
))
2097 ERR( "Sizes larger than 4Gb (%x%08x) not supported on this platform\n",
2098 (DWORD
)(full_size
>> 32), (DWORD
)full_size
);
2100 if ((res
= server_get_unix_fd( handle
, 0, &unix_handle
, &needs_close
, NULL
, NULL
))) goto done
;
2102 if (prot
& VPROT_IMAGE
)
2106 int shared_fd
, shared_needs_close
;
2108 if ((res
= server_get_unix_fd( shared_file
, FILE_READ_DATA
|FILE_WRITE_DATA
,
2109 &shared_fd
, &shared_needs_close
, NULL
, NULL
))) goto done
;
2110 res
= map_image( handle
, unix_handle
, base
, size
, mask
, header_size
,
2111 shared_fd
, dup_mapping
, addr_ptr
);
2112 if (shared_needs_close
) close( shared_fd
);
2113 NtClose( shared_file
);
2117 res
= map_image( handle
, unix_handle
, base
, size
, mask
, header_size
,
2118 -1, dup_mapping
, addr_ptr
);
2120 if (needs_close
) close( unix_handle
);
2121 if (!res
) *size_ptr
= size
;
2125 if ((offset
.QuadPart
>= size
) || (*size_ptr
> size
- offset
.QuadPart
))
2127 res
= STATUS_INVALID_PARAMETER
;
2130 if (*size_ptr
) size
= ROUND_SIZE( offset
.u
.LowPart
, *size_ptr
);
2131 else size
= size
- offset
.QuadPart
;
2137 case PAGE_READWRITE
:
2138 case PAGE_EXECUTE_READWRITE
:
2139 if (!(prot
& VPROT_WRITE
))
2141 res
= STATUS_INVALID_PARAMETER
;
2146 case PAGE_WRITECOPY
:
2148 case PAGE_EXECUTE_READ
:
2149 case PAGE_EXECUTE_WRITECOPY
:
2150 if (prot
& VPROT_READ
) break;
2153 res
= STATUS_INVALID_PARAMETER
;
2157 /* FIXME: If a mapping is created with SEC_RESERVE and a process,
2158 * which has a view of this mapping commits some pages, they will
2159 * appear committed in all other processes, which have the same
2160 * view created. Since we don't support this yet, we create the
2161 * whole mapping committed.
2163 prot
|= VPROT_COMMITTED
;
2165 /* Reserve a properly aligned area */
2167 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2169 res
= map_view( &view
, *addr_ptr
, size
, mask
, FALSE
, prot
);
2172 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2178 TRACE("handle=%p size=%lx offset=%x%08x\n",
2179 handle
, size
, offset
.u
.HighPart
, offset
.u
.LowPart
);
2181 res
= map_file_into_view( view
, unix_handle
, 0, size
, offset
.QuadPart
, prot
, !dup_mapping
);
2182 if (res
== STATUS_SUCCESS
)
2184 *addr_ptr
= view
->base
;
2186 view
->mapping
= dup_mapping
;
2187 dup_mapping
= 0; /* don't close it */
2191 ERR( "map_file_into_view %p %lx %x%08x failed\n",
2192 view
->base
, size
, offset
.u
.HighPart
, offset
.u
.LowPart
);
2193 delete_view( view
);
2196 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2199 if (dup_mapping
) NtClose( dup_mapping
);
2200 if (needs_close
) close( unix_handle
);
2205 /***********************************************************************
2206 * NtUnmapViewOfSection (NTDLL.@)
2207 * ZwUnmapViewOfSection (NTDLL.@)
2209 NTSTATUS WINAPI
NtUnmapViewOfSection( HANDLE process
, PVOID addr
)
2212 NTSTATUS status
= STATUS_INVALID_PARAMETER
;
2214 void *base
= ROUND_ADDR( addr
, page_mask
);
2216 if (process
!= NtCurrentProcess())
2219 apc_result_t result
;
2221 memset( &call
, 0, sizeof(call
) );
2223 call
.unmap_view
.type
= APC_UNMAP_VIEW
;
2224 call
.unmap_view
.addr
= addr
;
2225 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
2226 if (status
== STATUS_SUCCESS
) status
= result
.unmap_view
.status
;
2230 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2231 if ((view
= VIRTUAL_FindView( base
)) && (base
== view
->base
))
2233 delete_view( view
);
2234 status
= STATUS_SUCCESS
;
2236 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2241 /***********************************************************************
2242 * NtFlushVirtualMemory (NTDLL.@)
2243 * ZwFlushVirtualMemory (NTDLL.@)
2245 NTSTATUS WINAPI
NtFlushVirtualMemory( HANDLE process
, LPCVOID
*addr_ptr
,
2246 SIZE_T
*size_ptr
, ULONG unknown
)
2249 NTSTATUS status
= STATUS_SUCCESS
;
2251 void *addr
= ROUND_ADDR( *addr_ptr
, page_mask
);
2253 if (process
!= NtCurrentProcess())
2256 apc_result_t result
;
2258 memset( &call
, 0, sizeof(call
) );
2260 call
.virtual_flush
.type
= APC_VIRTUAL_FLUSH
;
2261 call
.virtual_flush
.addr
= addr
;
2262 call
.virtual_flush
.size
= *size_ptr
;
2263 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
2264 if (status
!= STATUS_SUCCESS
) return status
;
2266 if (result
.virtual_flush
.status
== STATUS_SUCCESS
)
2268 *addr_ptr
= result
.virtual_flush
.addr
;
2269 *size_ptr
= result
.virtual_flush
.size
;
2271 return result
.virtual_flush
.status
;
2274 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2275 if (!(view
= VIRTUAL_FindView( addr
))) status
= STATUS_INVALID_PARAMETER
;
2278 if (!*size_ptr
) *size_ptr
= view
->size
;
2280 if (msync( addr
, *size_ptr
, MS_SYNC
)) status
= STATUS_NOT_MAPPED_DATA
;
2282 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2287 /***********************************************************************
2288 * NtReadVirtualMemory (NTDLL.@)
2289 * ZwReadVirtualMemory (NTDLL.@)
2291 NTSTATUS WINAPI
NtReadVirtualMemory( HANDLE process
, const void *addr
, void *buffer
,
2292 SIZE_T size
, SIZE_T
*bytes_read
)
2296 SERVER_START_REQ( read_process_memory
)
2298 req
->handle
= process
;
2299 req
->addr
= (void *)addr
;
2300 wine_server_set_reply( req
, buffer
, size
);
2301 if ((status
= wine_server_call( req
))) size
= 0;
2304 if (bytes_read
) *bytes_read
= size
;
2309 /***********************************************************************
2310 * NtWriteVirtualMemory (NTDLL.@)
2311 * ZwWriteVirtualMemory (NTDLL.@)
2313 NTSTATUS WINAPI
NtWriteVirtualMemory( HANDLE process
, void *addr
, const void *buffer
,
2314 SIZE_T size
, SIZE_T
*bytes_written
)
2318 SERVER_START_REQ( write_process_memory
)
2320 req
->handle
= process
;
2322 wine_server_add_data( req
, buffer
, size
);
2323 if ((status
= wine_server_call( req
))) size
= 0;
2326 if (bytes_written
) *bytes_written
= size
;
2331 /***********************************************************************
2332 * NtAreMappedFilesTheSame (NTDLL.@)
2333 * ZwAreMappedFilesTheSame (NTDLL.@)
2335 NTSTATUS WINAPI
NtAreMappedFilesTheSame(PVOID addr1
, PVOID addr2
)
2337 TRACE("%p %p\n", addr1
, addr2
);
2339 return STATUS_NOT_SAME_DEVICE
;