~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/dlls/ntdll/virtual.c

Version: ~ [ wine-1.0-rc1 ] ~ [ wine-0.9.61 ] ~ [ wine-0.9.60 ] ~ [ wine-0.9.59 ] ~ [ wine-0.9.58 ] ~ [ wine-0.9.57 ] ~ [ wine-0.9.56 ] ~ [ wine-0.9.55 ] ~ [ wine-0.9.54 ] ~ [ wine-0.9.53 ] ~ [ wine-0.9.52 ] ~ [ wine-0.9.51 ] ~ [ wine-0.9.50 ] ~ [ wine-0.9.49 ] ~ [ wine-0.9.48 ] ~ [ wine-0.9.47 ] ~ [ wine-0.9.46 ] ~ [ wine-0.9.45 ] ~ [ wine-0.9.44 ] ~ [ wine-0.9.43 ] ~ [ wine-0.9.42 ] ~ [ wine-0.9.41 ] ~ [ wine-0.9.40 ] ~ [ wine-0.9.39 ] ~ [ wine-0.9.38 ] ~ [ wine-0.9.37 ] ~ [ wine-0.9.36 ] ~ [ wine-0.9.35 ] ~ [ wine-0.9.34 ] ~ [ wine-0.9.33 ] ~ [ wine-0.9.32 ] ~ [ wine-0.9.31 ] ~ [ wine-0.9.30 ] ~ [ wine-0.9.29 ] ~ [ wine-0.9.28 ] ~ [ wine-0.9.27 ] ~ [ wine-0.9.26 ] ~ [ wine-0.9.25 ] ~ [ wine-0.9.24 ] ~ [ wine-0.9.23 ] ~ [ wine-0.9.22 ] ~ [ wine-0.9.21 ] ~ [ wine-0.9.20 ] ~ [ wine-0.9.19 ] ~ [ wine-0.9.18 ] ~ [ wine-0.9.17 ] ~ [ wine-0.9.16 ] ~ [ wine-0.9.15 ] ~ [ wine-0.9.14 ] ~ [ wine-0.9.13 ] ~ [ wine-0.9.12 ] ~ [ wine-0.9.11 ] ~ [ wine-0.9.10 ] ~ [ wine-0.9.9 ] ~ [ wine-0.9.8 ] ~ [ wine-0.9.7 ] ~ [ wine-0.9.6 ] ~ [ wine-0.9.5 ] ~ [ wine-0.9.4 ] ~ [ wine-0.9.3 ] ~ [ wine-0.9.2 ] ~ [ wine-0.9.1 ] ~ [ wine-0.9 ] ~ [ wine20050930 ] ~ [ wine20050830 ] ~ [ wine20050725 ] ~ [ wine20050628 ] ~ [ wine20050524 ] ~ [ wine20050419 ] ~ [ wine20050310 ] ~ [ wine20050211 ] ~ [ wine20050111 ] ~ [ wine20041201 ] ~ [ wine20041019 ] ~ [ wine20040914 ] ~ [ wine20040813 ] ~ [ wine20040716 ] ~ [ wine20040615 ] ~ [ wine20040505 ] ~ [ wine20040408 ] ~ [ wine20040309 ] ~ [ wine20040213 ] ~ [ wine20040121 ] ~ [ wine20031212 ] ~ [ wine20031118 ] ~ [ wine20031016 ] ~ [ wine20030911 ] ~ [ wine20030813 ] ~ [ wine20030709 ] ~ [ wine20030618 ] ~ [ wine20030508 ] ~ [ wine20030408 ] ~ [ wine20030318 ] ~ [ wine20030219 ] ~ [ wine20030115 ] ~ [ wine20021219 ] ~ [ wine20021125 ] ~ [ wine20021031 ] ~ [ wine20021007 ] ~ [ wine20020904 ] ~ [ wine20020804 ] ~ [ wine20020710 ] ~ [ wine20020605 ] ~ [ wine20020509 ] ~ [ wine20020411 ] ~ [ wine20020310 ] ~ [ wine20020228 ] ~ [ wine20011226 ] ~ [ wine20011108 ] ~ [ wine20011004 ] ~ [ wine20010824 ] ~ [ wine20010731 ] ~ [ wine20010629 ] ~ [ wine20010510 ] ~ [ wine20010418 ] ~ [ wine20010326 ] ~ [ wine20010305 ] ~ [ wine20010216 ] ~ [ wine20010112 ] ~ [ wine20001222 ] ~ [ wine20001202 ] ~ [ wine20001026 ] ~ [ wine20001002 ] ~ [ wine20000909 ] ~ [ wine20000821 ] ~ [ wine20000801 ] ~ [ wine20000716 ] ~ [ wine20000326 ] ~ [ wine20000227 ] ~ [ wine20000130 ] ~ [ wine20000109 ] ~

  1 /*
  2  * Win32 virtual memory functions
  3  *
  4  * Copyright 1997, 2002 Alexandre Julliard
  5  *
  6  * This library is free software; you can redistribute it and/or
  7  * modify it under the terms of the GNU Lesser General Public
  8  * License as published by the Free Software Foundation; either
  9  * version 2.1 of the License, or (at your option) any later version.
 10  *
 11  * This library is distributed in the hope that it will be useful,
 12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 14  * Lesser General Public License for more details.
 15  *
 16  * You should have received a copy of the GNU Lesser General Public
 17  * License along with this library; if not, write to the Free Software
 18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 19  */
 20 
 21 #include "config.h"
 22 #include "wine/port.h"
 23 
 24 #include <assert.h>
 25 #include <errno.h>
 26 #ifdef HAVE_SYS_ERRNO_H
 27 #include <sys/errno.h>
 28 #endif
 29 #include <fcntl.h>
 30 #ifdef HAVE_UNISTD_H
 31 # include <unistd.h>
 32 #endif
 33 #include <stdarg.h>
 34 #include <stdlib.h>
 35 #include <stdio.h>
 36 #include <string.h>
 37 #include <sys/types.h>
 38 #ifdef HAVE_SYS_STAT_H
 39 # include <sys/stat.h>
 40 #endif
 41 #ifdef HAVE_SYS_MMAN_H
 42 # include <sys/mman.h>
 43 #endif
 44 #ifdef HAVE_VALGRIND_MEMCHECK_H
 45 # include <valgrind/memcheck.h>
 46 #endif
 47 
 48 #define NONAMELESSUNION
 49 #define NONAMELESSSTRUCT
 50 #include "ntstatus.h"
 51 #define WIN32_NO_STATUS
 52 #include "windef.h"
 53 #include "winternl.h"
 54 #include "wine/library.h"
 55 #include "wine/server.h"
 56 #include "wine/list.h"
 57 #include "wine/debug.h"
 58 #include "ntdll_misc.h"
 59 
 60 WINE_DEFAULT_DEBUG_CHANNEL(virtual);
 61 WINE_DECLARE_DEBUG_CHANNEL(module);
 62 
 63 #ifndef MS_SYNC
 64 #define MS_SYNC 0
 65 #endif
 66 
 67 #ifndef MAP_NORESERVE
 68 #define MAP_NORESERVE 0
 69 #endif
 70 
 71 /* File view */
 72 typedef struct file_view
 73 {
 74     struct list   entry;       /* Entry in global view list */
 75     void         *base;        /* Base address */
 76     size_t        size;        /* Size in bytes */
 77     HANDLE        mapping;     /* Handle to the file mapping */
 78     BYTE          flags;       /* Allocation flags (VFLAG_*) */
 79     BYTE          protect;     /* Protection for all pages at allocation time */
 80     BYTE          prot[1];     /* Protection byte for each page */
 81 } FILE_VIEW;
 82 
 83 /* Per-view flags */
 84 #define VFLAG_SYSTEM     0x01  /* system view (underlying mmap not under our control) */
 85 #define VFLAG_VALLOC     0x02  /* allocated by VirtualAlloc */
 86 
 87 /* Conversion from VPROT_* to Win32 flags */
 88 static const BYTE VIRTUAL_Win32Flags[16] =
 89 {
 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 */
106 };
107 
108 static struct list views_list = LIST_INIT(views_list);
109 
110 static RTL_CRITICAL_SECTION csVirtual;
111 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
112 {
113     0, 0, &csVirtual,
114     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
115       0, 0, { (DWORD_PTR)(__FILE__ ": csVirtual") }
116 };
117 static RTL_CRITICAL_SECTION csVirtual = { &critsect_debug, -1, 0, 0, 0, 0 };
118 
119 #ifdef __i386__
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 # define ADDRESS_SPACE_LIMIT  ((void *)0xc0000000)  /* top of the total available address space */
126 # define USER_SPACE_LIMIT     ((void *)0x7fff0000)  /* top of the user address space */
127 #else
128 static UINT page_shift;
129 static UINT page_size;
130 static UINT_PTR page_mask;
131 # define ADDRESS_SPACE_LIMIT  0   /* no limit needed on other platforms */
132 # define USER_SPACE_LIMIT     0   /* no limit needed on other platforms */
133 #endif  /* __i386__ */
134 
135 #define ROUND_ADDR(addr,mask) \
136    ((void *)((UINT_PTR)(addr) & ~(UINT_PTR)(mask)))
137 
138 #define ROUND_SIZE(addr,size) \
139    (((UINT)(size) + ((UINT_PTR)(addr) & page_mask) + page_mask) & ~page_mask)
140 
141 #define VIRTUAL_DEBUG_DUMP_VIEW(view) \
142     do { if (TRACE_ON(virtual)) VIRTUAL_DumpView(view); } while (0)
143 
144 static void *user_space_limit = USER_SPACE_LIMIT;
145 static void *preload_reserve_start;
146 static void *preload_reserve_end;
147 static int use_locks;
148 static int force_exec_prot;  /* whether to force PROT_EXEC on all PROT_READ mmaps */
149 
150 
151 /***********************************************************************
152  *           VIRTUAL_GetProtStr
153  */
154 static const char *VIRTUAL_GetProtStr( BYTE prot )
155 {
156     static char buffer[6];
157     buffer[0] = (prot & VPROT_COMMITTED) ? 'c' : '-';
158     buffer[1] = (prot & VPROT_GUARD) ? 'g' : '-';
159     buffer[2] = (prot & VPROT_READ) ? 'r' : '-';
160     buffer[3] = (prot & VPROT_WRITECOPY) ? 'W' : ((prot & VPROT_WRITE) ? 'w' : '-');
161     buffer[4] = (prot & VPROT_EXEC) ? 'x' : '-';
162     buffer[5] = 0;
163     return buffer;
164 }
165 
166 
167 /***********************************************************************
168  *           VIRTUAL_GetUnixProt
169  *
170  * Convert page protections to protection for mmap/mprotect.
171  */
172 static int VIRTUAL_GetUnixProt( BYTE vprot )
173 {
174     int prot = 0;
175     if ((vprot & VPROT_COMMITTED) && !(vprot & VPROT_GUARD))
176     {
177         if (vprot & VPROT_READ) prot |= PROT_READ;
178         if (vprot & VPROT_WRITE) prot |= PROT_WRITE;
179         if (vprot & VPROT_WRITECOPY) prot |= PROT_WRITE;
180         if (vprot & VPROT_EXEC) prot |= PROT_EXEC;
181     }
182     if (!prot) prot = PROT_NONE;
183     return prot;
184 }
185 
186 
187 /***********************************************************************
188  *           VIRTUAL_DumpView
189  */
190 static void VIRTUAL_DumpView( FILE_VIEW *view )
191 {
192     UINT i, count;
193     char *addr = view->base;
194     BYTE prot = view->prot[0];
195 
196     TRACE( "View: %p - %p", addr, addr + view->size - 1 );
197     if (view->flags & VFLAG_SYSTEM)
198         TRACE( " (system)\n" );
199     else if (view->flags & VFLAG_VALLOC)
200         TRACE( " (valloc)\n" );
201     else if (view->mapping)
202         TRACE( " %p\n", view->mapping );
203     else
204         TRACE( " (anonymous)\n");
205 
206     for (count = i = 1; i < view->size >> page_shift; i++, count++)
207     {
208         if (view->prot[i] == prot) continue;
209         TRACE( "      %p - %p %s\n",
210                  addr, addr + (count << page_shift) - 1, VIRTUAL_GetProtStr(prot) );
211         addr += (count << page_shift);
212         prot = view->prot[i];
213         count = 0;
214     }
215     if (count)
216         TRACE( "      %p - %p %s\n",
217                  addr, addr + (count << page_shift) - 1, VIRTUAL_GetProtStr(prot) );
218 }
219 
220 
221 /***********************************************************************
222  *           VIRTUAL_Dump
223  */
224 #if WINE_VM_DEBUG
225 static void VIRTUAL_Dump(void)
226 {
227     sigset_t sigset;
228     struct file_view *view;
229 
230     TRACE( "Dump of all virtual memory views:\n" );
231     server_enter_uninterrupted_section( &csVirtual, &sigset );
232     LIST_FOR_EACH_ENTRY( view, &views_list, FILE_VIEW, entry )
233     {
234         VIRTUAL_DumpView( view );
235     }
236     server_leave_uninterrupted_section( &csVirtual, &sigset );
237 }
238 #endif
239 
240 
241 /***********************************************************************
242  *           VIRTUAL_FindView
243  *
244  * Find the view containing a given address. The csVirtual section must be held by caller.
245  *
246  * PARAMS
247  *      addr  [I] Address
248  *
249  * RETURNS
250  *      View: Success
251  *      NULL: Failure
252  */
253 static struct file_view *VIRTUAL_FindView( const void *addr )
254 {
255     struct file_view *view;
256 
257     LIST_FOR_EACH_ENTRY( view, &views_list, struct file_view, entry )
258     {
259         if (view->base > addr) break;
260         if ((const char*)view->base + view->size > (const char*)addr) return view;
261     }
262     return NULL;
263 }
264 
265 
266 /***********************************************************************
267  *           get_mask
268  */
269 static inline UINT_PTR get_mask( ULONG zero_bits )
270 {
271     if (!zero_bits) return 0xffff;  /* allocations are aligned to 64K by default */
272     if (zero_bits < page_shift) zero_bits = page_shift;
273     return (1 << zero_bits) - 1;
274 }
275 
276 
277 /***********************************************************************
278  *           find_view_range
279  *
280  * Find the first view overlapping at least part of the specified range.
281  * The csVirtual section must be held by caller.
282  */
283 static struct file_view *find_view_range( const void *addr, size_t size )
284 {
285     struct file_view *view;
286 
287     LIST_FOR_EACH_ENTRY( view, &views_list, struct file_view, entry )
288     {
289         if ((const char *)view->base >= (const char *)addr + size) break;
290         if ((const char *)view->base + view->size > (const char *)addr) return view;
291     }
292     return NULL;
293 }
294 
295 
296 /***********************************************************************
297  *           find_free_area
298  *
299  * Find a free area between views inside the specified range.
300  * The csVirtual section must be held by caller.
301  */
302 static void *find_free_area( void *base, void *end, size_t size, size_t mask, int top_down )
303 {
304     struct list *ptr;
305     void *start;
306 
307     if (top_down)
308     {
309         start = ROUND_ADDR( (char *)end - size, mask );
310         if (start >= end || start < base) return NULL;
311 
312         for (ptr = views_list.prev; ptr != &views_list; ptr = ptr->prev)
313         {
314             struct file_view *view = LIST_ENTRY( ptr, struct file_view, entry );
315 
316             if ((char *)view->base + view->size <= (char *)start) break;
317             if ((char *)view->base >= (char *)start + size) continue;
318             start = ROUND_ADDR( (char *)view->base - size, mask );
319             /* stop if remaining space is not large enough */
320             if (!start || start >= end || start < base) return NULL;
321         }
322     }
323     else
324     {
325         start = ROUND_ADDR( (char *)base + mask, mask );
326         if (start >= end || (char *)end - (char *)start < size) return NULL;
327 
328         for (ptr = views_list.next; ptr != &views_list; ptr = ptr->next)
329         {
330             struct file_view *view = LIST_ENTRY( ptr, struct file_view, entry );
331 
332             if ((char *)view->base >= (char *)start + size) break;
333             if ((char *)view->base + view->size <= (char *)start) continue;
334             start = ROUND_ADDR( (char *)view->base + view->size + mask, mask );
335             /* stop if remaining space is not large enough */
336             if (!start || start >= end || (char *)end - (char *)start < size) return NULL;
337         }
338     }
339     return start;
340 }
341 
342 
343 /***********************************************************************
344  *           add_reserved_area
345  *
346  * Add a reserved area to the list maintained by libwine.
347  * The csVirtual section must be held by caller.
348  */
349 static void add_reserved_area( void *addr, size_t size )
350 {
351     TRACE( "adding %p-%p\n", addr, (char *)addr + size );
352 
353     if (addr < user_space_limit)
354     {
355         /* unmap the part of the area that is below the limit */
356         assert( (char *)addr + size > (char *)user_space_limit );
357         munmap( addr, (char *)user_space_limit - (char *)addr );
358         size -= (char *)user_space_limit - (char *)addr;
359         addr = user_space_limit;
360     }
361     /* blow away existing mappings */
362     wine_anon_mmap( addr, size, PROT_NONE, MAP_NORESERVE | MAP_FIXED );
363     wine_mmap_add_reserved_area( addr, size );
364 }
365 
366 
367 /***********************************************************************
368  *           is_beyond_limit
369  *
370  * Check if an address range goes beyond a given limit.
371  */
372 static inline int is_beyond_limit( const void *addr, size_t size, const void *limit )
373 {
374     return (limit && (addr >= limit || (const char *)addr + size > (const char *)limit));
375 }
376 
377 
378 /***********************************************************************
379  *           unmap_area
380  *
381  * Unmap an area, or simply replace it by an empty mapping if it is
382  * in a reserved area. The csVirtual section must be held by caller.
383  */
384 static inline void unmap_area( void *addr, size_t size )
385 {
386     if (wine_mmap_is_in_reserved_area( addr, size ))
387         wine_anon_mmap( addr, size, PROT_NONE, MAP_NORESERVE | MAP_FIXED );
388     else if (is_beyond_limit( addr, size, user_space_limit ))
389         add_reserved_area( addr, size );
390     else
391         munmap( addr, size );
392 }
393 
394 
395 /***********************************************************************
396  *           delete_view
397  *
398  * Deletes a view. The csVirtual section must be held by caller.
399  */
400 static void delete_view( struct file_view *view ) /* [in] View */
401 {
402     if (!(view->flags & VFLAG_SYSTEM)) unmap_area( view->base, view->size );
403     list_remove( &view->entry );
404     if (view->mapping) NtClose( view->mapping );
405     free( view );
406 }
407 
408 
409 /***********************************************************************
410  *           create_view
411  *
412  * Create a view. The csVirtual section must be held by caller.
413  */
414 static NTSTATUS create_view( struct file_view **view_ret, void *base, size_t size, BYTE vprot )
415 {
416     struct file_view *view;
417     struct list *ptr;
418     int unix_prot = VIRTUAL_GetUnixProt( vprot );
419 
420     assert( !((UINT_PTR)base & page_mask) );
421     assert( !(size & page_mask) );
422 
423     /* Create the view structure */
424 
425     if (!(view = malloc( sizeof(*view) + (size >> page_shift) - 1 ))) return STATUS_NO_MEMORY;
426 
427     view->base    = base;
428     view->size    = size;
429     view->flags   = 0;
430     view->mapping = 0;
431     view->protect = vprot;
432     memset( view->prot, vprot & ~VPROT_IMAGE, size >> page_shift );
433 
434     /* Insert it in the linked list */
435 
436     LIST_FOR_EACH( ptr, &views_list )
437     {
438         struct file_view *next = LIST_ENTRY( ptr, struct file_view, entry );
439         if (next->base > base) break;
440     }
441     list_add_before( ptr, &view->entry );
442 
443     /* Check for overlapping views. This can happen if the previous view
444      * was a system view that got unmapped behind our back. In that case
445      * we recover by simply deleting it. */
446 
447     if ((ptr = list_prev( &views_list, &view->entry )) != NULL)
448     {
449         struct file_view *prev = LIST_ENTRY( ptr, struct file_view, entry );
450         if ((char *)prev->base + prev->size > (char *)base)
451         {
452             TRACE( "overlapping prev view %p-%p for %p-%p\n",
453                    prev->base, (char *)prev->base + prev->size,
454                    base, (char *)base + view->size );
455             assert( prev->flags & VFLAG_SYSTEM );
456             delete_view( prev );
457         }
458     }
459     if ((ptr = list_next( &views_list, &view->entry )) != NULL)
460     {
461         struct file_view *next = LIST_ENTRY( ptr, struct file_view, entry );
462         if ((char *)base + view->size > (char *)next->base)
463         {
464             TRACE( "overlapping next view %p-%p for %p-%p\n",
465                    next->base, (char *)next->base + next->size,
466                    base, (char *)base + view->size );
467             assert( next->flags & VFLAG_SYSTEM );
468             delete_view( next );
469         }
470     }
471 
472     *view_ret = view;
473     VIRTUAL_DEBUG_DUMP_VIEW( view );
474 
475     if (force_exec_prot && (unix_prot & PROT_READ) && !(unix_prot & PROT_EXEC))
476     {
477         TRACE( "forcing exec permission on %p-%p\n", base, (char *)base + size - 1 );
478         mprotect( base, size, unix_prot | PROT_EXEC );
479     }
480     return STATUS_SUCCESS;
481 }
482 
483 
484 /***********************************************************************
485  *           VIRTUAL_GetWin32Prot
486  *
487  * Convert page protections to Win32 flags.
488  */
489 static DWORD VIRTUAL_GetWin32Prot( BYTE vprot )
490 {
491     DWORD ret = VIRTUAL_Win32Flags[vprot & 0x0f];
492     if (vprot & VPROT_NOCACHE) ret |= PAGE_NOCACHE;
493     if (vprot & VPROT_GUARD) ret |= PAGE_GUARD;
494     return ret;
495 }
496 
497 
498 /***********************************************************************
499  *           VIRTUAL_GetProt
500  *
501  * Build page protections from Win32 flags.
502  *
503  * PARAMS
504  *      protect [I] Win32 protection flags
505  *
506  * RETURNS
507  *      Value of page protection flags
508  */
509 static BYTE VIRTUAL_GetProt( DWORD protect )
510 {
511     BYTE vprot;
512 
513     switch(protect & 0xff)
514     {
515     case PAGE_READONLY:
516         vprot = VPROT_READ;
517         break;
518     case PAGE_READWRITE:
519         vprot = VPROT_READ | VPROT_WRITE;
520         break;
521     case PAGE_WRITECOPY:
522         /* MSDN CreateFileMapping() states that if PAGE_WRITECOPY is given,
523          * that the hFile must have been opened with GENERIC_READ and
524          * GENERIC_WRITE access.  This is WRONG as tests show that you
525          * only need GENERIC_READ access (at least for Win9x,
526          * FIXME: what about NT?).  Thus, we don't put VPROT_WRITE in
527          * PAGE_WRITECOPY and PAGE_EXECUTE_WRITECOPY.
528          */
529         vprot = VPROT_READ | VPROT_WRITECOPY;
530         break;
531     case PAGE_EXECUTE:
532         vprot = VPROT_EXEC;
533         break;
534     case PAGE_EXECUTE_READ:
535         vprot = VPROT_EXEC | VPROT_READ;
536         break;
537     case PAGE_EXECUTE_READWRITE:
538         vprot = VPROT_EXEC | VPROT_READ | VPROT_WRITE;
539         break;
540     case PAGE_EXECUTE_WRITECOPY:
541         /* See comment for PAGE_WRITECOPY above */
542         vprot = VPROT_EXEC | VPROT_READ | VPROT_WRITECOPY;
543         break;
544     case PAGE_NOACCESS:
545     default:
546         vprot = 0;
547         break;
548     }
549     if (protect & PAGE_GUARD) vprot |= VPROT_GUARD;
550     if (protect & PAGE_NOCACHE) vprot |= VPROT_NOCACHE;
551     return vprot;
552 }
553 
554 
555 /***********************************************************************
556  *           VIRTUAL_SetProt
557  *
558  * Change the protection of a range of pages.
559  *
560  * RETURNS
561  *      TRUE: Success
562  *      FALSE: Failure
563  */
564 static BOOL VIRTUAL_SetProt( FILE_VIEW *view, /* [in] Pointer to view */
565                              void *base,      /* [in] Starting address */
566                              size_t size,     /* [in] Size in bytes */
567                              BYTE vprot )     /* [in] Protections to use */
568 {
569     int unix_prot = VIRTUAL_GetUnixProt(vprot);
570 
571     TRACE("%p-%p %s\n",
572           base, (char *)base + size - 1, VIRTUAL_GetProtStr( vprot ) );
573 
574     /* if setting stack guard pages, store the permissions first, as the guard may be
575      * triggered at any point after mprotect and change the permissions again */
576     if ((vprot & VPROT_GUARD) &&
577         ((char *)base >= (char *)NtCurrentTeb()->DeallocationStack) &&
578         ((char *)base < (char *)NtCurrentTeb()->Tib.StackBase))
579     {
580         memset( view->prot + (((char *)base - (char *)view->base) >> page_shift),
581                 vprot, size >> page_shift );
582         mprotect( base, size, unix_prot );
583         VIRTUAL_DEBUG_DUMP_VIEW( view );
584         return TRUE;
585     }
586 
587     if (force_exec_prot && (unix_prot & PROT_READ) && !(unix_prot & PROT_EXEC))
588     {
589         TRACE( "forcing exec permission on %p-%p\n", base, (char *)base + size - 1 );
590         if (!mprotect( base, size, unix_prot | PROT_EXEC )) goto done;
591         /* exec + write may legitimately fail, in that case fall back to write only */
592         if (!(unix_prot & PROT_WRITE)) return FALSE;
593     }
594 
595     if (mprotect( base, size, unix_prot )) return FALSE;  /* FIXME: last error */
596 
597 done:
598     memset( view->prot + (((char *)base - (char *)view->base) >> page_shift),
599             vprot, size >> page_shift );
600     VIRTUAL_DEBUG_DUMP_VIEW( view );
601     return TRUE;
602 }
603 
604 
605 /***********************************************************************
606  *           unmap_extra_space
607  *
608  * Release the extra memory while keeping the range starting on the granularity boundary.
609  */
610 static inline void *unmap_extra_space( void *ptr, size_t total_size, size_t wanted_size, size_t mask )
611 {
612     if ((ULONG_PTR)ptr & mask)
613     {
614         size_t extra = mask + 1 - ((ULONG_PTR)ptr & mask);
615         munmap( ptr, extra );
616         ptr = (char *)ptr + extra;
617         total_size -= extra;
618     }
619     if (total_size > wanted_size)
620         munmap( (char *)ptr + wanted_size, total_size - wanted_size );
621     return ptr;
622 }
623 
624 
625 struct alloc_area
626 {
627     size_t size;
628     size_t mask;
629     int    top_down;
630     void  *result;
631 };
632 
633 /***********************************************************************
634  *           alloc_reserved_area_callback
635  *
636  * Try to map some space inside a reserved area. Callback for wine_mmap_enum_reserved_areas.
637  */
638 static int alloc_reserved_area_callback( void *start, size_t size, void *arg )
639 {
640     static void * const address_space_start = (void *)0x110000;
641     struct alloc_area *alloc = arg;
642     void *end = (char *)start + size;
643 
644     if (start < address_space_start) start = address_space_start;
645     if (user_space_limit && end > user_space_limit) end = user_space_limit;
646     if (start >= end) return 0;
647 
648     /* make sure we don't touch the preloader reserved range */
649     if (preload_reserve_end >= start)
650     {
651         if (preload_reserve_end >= end)
652         {
653             if (preload_reserve_start <= start) return 0;  /* no space in that area */
654             if (preload_reserve_start < end) end = preload_reserve_start;
655         }
656         else if (preload_reserve_start <= start) start = preload_reserve_end;
657         else
658         {
659             /* range is split in two by the preloader reservation, try first part */
660             if ((alloc->result = find_free_area( start, preload_reserve_start, alloc->size,
661                                                  alloc->mask, alloc->top_down )))
662                 return 1;
663             /* then fall through to try second part */
664             start = preload_reserve_end;
665         }
666     }
667     if ((alloc->result = find_free_area( start, end, alloc->size, alloc->mask, alloc->top_down )))
668         return 1;
669 
670     return 0;
671 }
672 
673 
674 /***********************************************************************
675  *           map_view
676  *
677  * Create a view and mmap the corresponding memory area.
678  * The csVirtual section must be held by caller.
679  */
680 static NTSTATUS map_view( struct file_view **view_ret, void *base, size_t size, size_t mask,
681                           int top_down, BYTE vprot )
682 {
683     void *ptr;
684     NTSTATUS status;
685 
686     if (base)
687     {
688         if (is_beyond_limit( base, size, ADDRESS_SPACE_LIMIT ))
689             return STATUS_WORKING_SET_LIMIT_RANGE;
690 
691         switch (wine_mmap_is_in_reserved_area( base, size ))
692         {
693         case -1: /* partially in a reserved area */
694             return STATUS_CONFLICTING_ADDRESSES;
695 
696         case 0:  /* not in a reserved area, do a normal allocation */
697             if ((ptr = wine_anon_mmap( base, size, VIRTUAL_GetUnixProt(vprot), 0 )) == (void *)-1)
698             {
699                 if (errno == ENOMEM) return STATUS_NO_MEMORY;
700                 return STATUS_INVALID_PARAMETER;
701             }
702             if (ptr != base)
703             {
704                 /* We couldn't get the address we wanted */
705                 if (is_beyond_limit( ptr, size, user_space_limit )) add_reserved_area( ptr, size );
706                 else munmap( ptr, size );
707                 return STATUS_CONFLICTING_ADDRESSES;
708             }
709             break;
710 
711         default:
712         case 1:  /* in a reserved area, make sure the address is available */
713             if (find_view_range( base, size )) return STATUS_CONFLICTING_ADDRESSES;
714             /* replace the reserved area by our mapping */
715             if ((ptr = wine_anon_mmap( base, size, VIRTUAL_GetUnixProt(vprot), MAP_FIXED )) != base)
716                 return STATUS_INVALID_PARAMETER;
717             break;
718         }
719     }
720     else
721     {
722         size_t view_size = size + mask + 1;
723         struct alloc_area alloc;
724 
725         alloc.size = size;
726         alloc.mask = mask;
727         alloc.top_down = top_down;
728         if (wine_mmap_enum_reserved_areas( alloc_reserved_area_callback, &alloc, top_down ))
729         {
730             ptr = alloc.result;
731             TRACE( "got mem in reserved area %p-%p\n", ptr, (char *)ptr + size );
732             if (wine_anon_mmap( ptr, size, VIRTUAL_GetUnixProt(vprot), MAP_FIXED ) != ptr)
733                 return STATUS_INVALID_PARAMETER;
734             goto done;
735         }
736 
737         for (;;)
738         {
739             if ((ptr = wine_anon_mmap( NULL, view_size, VIRTUAL_GetUnixProt(vprot), 0 )) == (void *)-1)
740             {
741                 if (errno == ENOMEM) return STATUS_NO_MEMORY;
742                 return STATUS_INVALID_PARAMETER;
743             }
744             TRACE( "got mem with anon mmap %p-%p\n", ptr, (char *)ptr + size );
745             /* if we got something beyond the user limit, unmap it and retry */
746             if (is_beyond_limit( ptr, view_size, user_space_limit )) add_reserved_area( ptr, view_size );
747             else break;
748         }
749         ptr = unmap_extra_space( ptr, view_size, size, mask );
750     }
751 done:
752     status = create_view( view_ret, ptr, size, vprot );
753     if (status != STATUS_SUCCESS) unmap_area( ptr, size );
754     return status;
755 }
756 
757 
758 /***********************************************************************
759  *           unaligned_mmap
760  *
761  * Linux kernels before 2.4.x can support non page-aligned offsets, as
762  * long as the offset is aligned to the filesystem block size. This is
763  * a big performance gain so we want to take advantage of it.
764  *
765  * However, when we use 64-bit file support this doesn't work because
766  * glibc rejects unaligned offsets. Also glibc 2.1.3 mmap64 is broken
767  * in that it rounds unaligned offsets down to a page boundary. For
768  * these reasons we do a direct system call here.
769  */
770 static void *unaligned_mmap( void *addr, size_t length, unsigned int prot,
771                              unsigned int flags, int fd, off_t offset )
772 {
773 #if defined(linux) && defined(__i386__) && defined(__GNUC__)
774     if (!(offset >> 32) && (offset & page_mask))
775     {
776         int ret;
777 
778         struct
779         {
780             void        *addr;
781             unsigned int length;
782             unsigned int prot;
783             unsigned int flags;
784             unsigned int fd;
785             unsigned int offset;
786         } args;
787 
788         args.addr   = addr;
789         args.length = length;
790         args.prot   = prot;
791         args.flags  = flags;
792         args.fd     = fd;
793         args.offset = offset;
794 
795         __asm__ __volatile__("push %%ebx\n\t"
796                              "movl %2,%%ebx\n\t"
797                              "int $0x80\n\t"
798                              "popl %%ebx"
799                              : "=a" (ret)
800                              : "" (90), /* SYS_mmap */
801                                "q" (&args)
802                              : "memory" );
803         if (ret < 0 && ret > -4096)
804         {
805             errno = -ret;
806             ret = -1;
807         }
808         return (void *)ret;
809     }
810 #endif
811     return mmap( addr, length, prot, flags, fd, offset );
812 }
813 
814 
815 /***********************************************************************
816  *           map_file_into_view
817  *
818  * Wrapper for mmap() to map a file into a view, falling back to read if mmap fails.
819  * The csVirtual section must be held by caller.
820  */
821 static NTSTATUS map_file_into_view( struct file_view *view, int fd, size_t start, size_t size,
822                                     off_t offset, BYTE vprot, BOOL removable )
823 {
824     void *ptr;
825     int prot = VIRTUAL_GetUnixProt( vprot );
826     BOOL shared_write = (vprot & VPROT_WRITE) != 0;
827 
828     assert( start < view->size );
829     assert( start + size <= view->size );
830 
831     /* only try mmap if media is not removable (or if we require write access) */
832     if (!removable || shared_write)
833     {
834         int flags = MAP_FIXED | (shared_write ? MAP_SHARED : MAP_PRIVATE);
835 
836         if (unaligned_mmap( (char *)view->base + start, size, prot, flags, fd, offset ) != (void *)-1)
837             goto done;
838 
839         /* mmap() failed; if this is because the file offset is not    */
840         /* page-aligned (EINVAL), or because the underlying filesystem */
841         /* does not support mmap() (ENOEXEC,ENODEV), we do it by hand. */
842         if ((errno != ENOEXEC) && (errno != EINVAL) && (errno != ENODEV)) return FILE_GetNtStatus();
843         if (shared_write)  /* we cannot fake shared write mappings */
844         {
845             if (errno == EINVAL) return STATUS_INVALID_PARAMETER;
846             ERR( "shared writable mmap not supported, broken filesystem?\n" );
847             return STATUS_NOT_SUPPORTED;
848         }
849     }
850 
851     /* Reserve the memory with an anonymous mmap */
852     ptr = wine_anon_mmap( (char *)view->base + start, size, PROT_READ | PROT_WRITE, MAP_FIXED );
853     if (ptr == (void *)-1) return FILE_GetNtStatus();
854     /* Now read in the file */
855     pread( fd, ptr, size, offset );
856     if (prot != (PROT_READ|PROT_WRITE)) mprotect( ptr, size, prot );  /* Set the right protection */
857 done:
858     memset( view->prot + (start >> page_shift), vprot, ROUND_SIZE(start,size) >> page_shift );
859     return STATUS_SUCCESS;
860 }
861 
862 
863 /***********************************************************************
864  *           decommit_view
865  *
866  * Decommit some pages of a given view.
867  * The csVirtual section must be held by caller.
868  */
869 static NTSTATUS decommit_pages( struct file_view *view, size_t start, size_t size )
870 {
871     if (wine_anon_mmap( (char *)view->base + start, size, PROT_NONE, MAP_FIXED ) != (void *)-1)
872     {
873         BYTE *p = view->prot + (start >> page_shift);
874         size >>= page_shift;
875         while (size--) *p++ &= ~VPROT_COMMITTED;
876         return STATUS_SUCCESS;
877     }
878     return FILE_GetNtStatus();
879 }
880 
881 
882 /***********************************************************************
883  *           map_image
884  *
885  * Map an executable (PE format) image into memory.
886  */
887 static NTSTATUS map_image( HANDLE hmapping, int fd, char *base, SIZE_T total_size, SIZE_T mask,
888                            SIZE_T header_size, int shared_fd, HANDLE dup_mapping, PVOID *addr_ptr )
889 {
890     IMAGE_DOS_HEADER *dos;
891     IMAGE_NT_HEADERS *nt;
892     IMAGE_SECTION_HEADER *sec;
893     IMAGE_DATA_DIRECTORY *imports;
894     NTSTATUS status = STATUS_CONFLICTING_ADDRESSES;
895     int i;
896     off_t pos;
897     sigset_t sigset;
898     struct stat st;
899     struct file_view *view = NULL;
900     char *ptr, *header_end;
901 
902     /* zero-map the whole range */
903 
904     server_enter_uninterrupted_section( &csVirtual, &sigset );
905 
906     if (base >= (char *)0x110000)  /* make sure the DOS area remains free */
907         status = map_view( &view, base, total_size, mask, FALSE,
908                            VPROT_COMMITTED | VPROT_READ | VPROT_EXEC | VPROT_WRITECOPY | VPROT_IMAGE );
909 
910     if (status == STATUS_CONFLICTING_ADDRESSES)
911         status = map_view( &view, NULL, total_size, mask, FALSE,
912                            VPROT_COMMITTED | VPROT_READ | VPROT_EXEC | VPROT_WRITECOPY | VPROT_IMAGE );
913 
914     if (status != STATUS_SUCCESS) goto error;
915 
916     ptr = view->base;
917     TRACE_(module)( "mapped PE file at %p-%p\n", ptr, ptr + tota