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>
45 #define NONAMELESSUNION
46 #define NONAMELESSSTRUCT
48 #define WIN32_NO_STATUS
52 #include "wine/library.h"
53 #include "wine/server.h"
54 #include "wine/list.h"
55 #include "wine/debug.h"
56 #include "ntdll_misc.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(virtual);
59 WINE_DECLARE_DEBUG_CHANNEL(module
);
66 #define MAP_NORESERVE 0
70 typedef struct file_view
72 struct list entry
; /* Entry in global view list */
73 void *base
; /* Base address */
74 size_t size
; /* Size in bytes */
75 HANDLE mapping
; /* Handle to the file mapping */
76 BYTE flags
; /* Allocation flags (VFLAG_*) */
77 BYTE protect
; /* Protection for all pages at allocation time */
78 BYTE prot
[1]; /* Protection byte for each page */
82 #define VFLAG_SYSTEM 0x01 /* system view (underlying mmap not under our control) */
83 #define VFLAG_VALLOC 0x02 /* allocated by VirtualAlloc */
85 /* Conversion from VPROT_* to Win32 flags */
86 static const BYTE VIRTUAL_Win32Flags
[16] =
88 PAGE_NOACCESS
, /* 0 */
89 PAGE_READONLY
, /* READ */
90 PAGE_READWRITE
, /* WRITE */
91 PAGE_READWRITE
, /* READ | WRITE */
92 PAGE_EXECUTE
, /* EXEC */
93 PAGE_EXECUTE_READ
, /* READ | EXEC */
94 PAGE_EXECUTE_READWRITE
, /* WRITE | EXEC */
95 PAGE_EXECUTE_READWRITE
, /* READ | WRITE | EXEC */
96 PAGE_WRITECOPY
, /* WRITECOPY */
97 PAGE_WRITECOPY
, /* READ | WRITECOPY */
98 PAGE_WRITECOPY
, /* WRITE | WRITECOPY */
99 PAGE_WRITECOPY
, /* READ | WRITE | WRITECOPY */
100 PAGE_EXECUTE_WRITECOPY
, /* EXEC | WRITECOPY */
101 PAGE_EXECUTE_WRITECOPY
, /* READ | EXEC | WRITECOPY */
102 PAGE_EXECUTE_WRITECOPY
, /* WRITE | EXEC | WRITECOPY */
103 PAGE_EXECUTE_WRITECOPY
/* READ | WRITE | EXEC | WRITECOPY */
106 static struct list views_list
= LIST_INIT(views_list
);
108 static RTL_CRITICAL_SECTION csVirtual
;
109 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
112 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
113 0, 0, { (DWORD_PTR
)(__FILE__
": csVirtual") }
115 static RTL_CRITICAL_SECTION csVirtual
= { &critsect_debug
, -1, 0, 0, 0, 0 };
118 /* These are always the same on an i386, and it will be faster this way */
119 # define page_mask 0xfff
120 # define page_shift 12
121 # define page_size 0x1000
122 /* Note: these are Windows limits, you cannot change them. */
123 # define ADDRESS_SPACE_LIMIT ((void *)0xc0000000) /* top of the total available address space */
124 # define USER_SPACE_LIMIT ((void *)0x7fff0000) /* top of the user address space */
126 static UINT page_shift
;
127 static UINT page_size
;
128 static UINT_PTR page_mask
;
129 # define ADDRESS_SPACE_LIMIT 0 /* no limit needed on other platforms */
130 # define USER_SPACE_LIMIT 0 /* no limit needed on other platforms */
131 #endif /* __i386__ */
133 #define ROUND_ADDR(addr,mask) \
134 ((void *)((UINT_PTR)(addr) & ~(UINT_PTR)(mask)))
136 #define ROUND_SIZE(addr,size) \
137 (((UINT)(size) + ((UINT_PTR)(addr) & page_mask) + page_mask) & ~page_mask)
139 #define VIRTUAL_DEBUG_DUMP_VIEW(view) \
140 do { if (TRACE_ON(virtual)) VIRTUAL_DumpView(view); } while (0)
142 static void *user_space_limit
= USER_SPACE_LIMIT
;
143 static void *preload_reserve_start
;
144 static void *preload_reserve_end
;
145 static int use_locks
;
148 /***********************************************************************
151 static const char *VIRTUAL_GetProtStr( BYTE prot
)
153 static char buffer
[6];
154 buffer
[0] = (prot
& VPROT_COMMITTED
) ? 'c' : '-';
155 buffer
[1] = (prot
& VPROT_GUARD
) ? 'g' : '-';
156 buffer
[2] = (prot
& VPROT_READ
) ? 'r' : '-';
157 buffer
[3] = (prot
& VPROT_WRITECOPY
) ? 'W' : ((prot
& VPROT_WRITE
) ? 'w' : '-');
158 buffer
[4] = (prot
& VPROT_EXEC
) ? 'x' : '-';
164 /***********************************************************************
167 static void VIRTUAL_DumpView( FILE_VIEW
*view
)
170 char *addr
= view
->base
;
171 BYTE prot
= view
->prot
[0];
173 TRACE( "View: %p - %p", addr
, addr
+ view
->size
- 1 );
174 if (view
->flags
& VFLAG_SYSTEM
)
175 TRACE( " (system)\n" );
176 else if (view
->flags
& VFLAG_VALLOC
)
177 TRACE( " (valloc)\n" );
178 else if (view
->mapping
)
179 TRACE( " %p\n", view
->mapping
);
181 TRACE( " (anonymous)\n");
183 for (count
= i
= 1; i
< view
->size
>> page_shift
; i
++, count
++)
185 if (view
->prot
[i
] == prot
) continue;
186 TRACE( " %p - %p %s\n",
187 addr
, addr
+ (count
<< page_shift
) - 1, VIRTUAL_GetProtStr(prot
) );
188 addr
+= (count
<< page_shift
);
189 prot
= view
->prot
[i
];
193 TRACE( " %p - %p %s\n",
194 addr
, addr
+ (count
<< page_shift
) - 1, VIRTUAL_GetProtStr(prot
) );
198 /***********************************************************************
202 static void VIRTUAL_Dump(void)
204 struct file_view
*view
;
206 TRACE( "Dump of all virtual memory views:\n" );
207 RtlEnterCriticalSection(&csVirtual
);
208 LIST_FOR_EACH_ENTRY( view
, &views_list
, FILE_VIEW
, entry
)
210 VIRTUAL_DumpView( view
);
212 RtlLeaveCriticalSection(&csVirtual
);
217 /***********************************************************************
220 * Find the view containing a given address. The csVirtual section must be held by caller.
229 static struct file_view
*VIRTUAL_FindView( const void *addr
)
231 struct file_view
*view
;
233 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
235 if (view
->base
> addr
) break;
236 if ((const char*)view
->base
+ view
->size
> (const char*)addr
) return view
;
242 /***********************************************************************
245 static inline UINT_PTR
get_mask( ULONG zero_bits
)
247 if (!zero_bits
) return 0xffff; /* allocations are aligned to 64K by default */
248 if (zero_bits
< page_shift
) zero_bits
= page_shift
;
249 return (1 << zero_bits
) - 1;
253 /***********************************************************************
256 * Find the first view overlapping at least part of the specified range.
257 * The csVirtual section must be held by caller.
259 static struct file_view
*find_view_range( const void *addr
, size_t size
)
261 struct file_view
*view
;
263 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
265 if ((const char *)view
->base
>= (const char *)addr
+ size
) break;
266 if ((const char *)view
->base
+ view
->size
> (const char *)addr
) return view
;
272 /***********************************************************************
275 * Find a free area between views inside the specified range.
276 * The csVirtual section must be held by caller.
278 static void *find_free_area( void *base
, void *end
, size_t size
, size_t mask
, int top_down
)
285 start
= ROUND_ADDR( (char *)end
- size
, mask
);
286 if (start
>= end
|| start
< base
) return NULL
;
288 for (ptr
= views_list
.prev
; ptr
!= &views_list
; ptr
= ptr
->prev
)
290 struct file_view
*view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
292 if ((char *)view
->base
+ view
->size
<= (char *)start
) break;
293 if ((char *)view
->base
>= (char *)start
+ size
) continue;
294 start
= ROUND_ADDR( (char *)view
->base
- size
, mask
);
295 /* stop if remaining space is not large enough */
296 if (!start
|| start
>= end
|| start
< base
) return NULL
;
301 start
= ROUND_ADDR( (char *)base
+ mask
, mask
);
302 if (start
>= end
|| (char *)end
- (char *)start
< size
) return NULL
;
304 for (ptr
= views_list
.next
; ptr
!= &views_list
; ptr
= ptr
->next
)
306 struct file_view
*view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
308 if ((char *)view
->base
>= (char *)start
+ size
) break;
309 if ((char *)view
->base
+ view
->size
<= (char *)start
) continue;
310 start
= ROUND_ADDR( (char *)view
->base
+ view
->size
+ mask
, mask
);
311 /* stop if remaining space is not large enough */
312 if (!start
|| start
>= end
|| (char *)end
- (char *)start
< size
) return NULL
;
319 /***********************************************************************
322 * Add a reserved area to the list maintained by libwine.
323 * The csVirtual section must be held by caller.
325 static void add_reserved_area( void *addr
, size_t size
)
327 TRACE( "adding %p-%p\n", addr
, (char *)addr
+ size
);
329 if (addr
< user_space_limit
)
331 /* unmap the part of the area that is below the limit */
332 assert( (char *)addr
+ size
> (char *)user_space_limit
);
333 munmap( addr
, (char *)user_space_limit
- (char *)addr
);
334 size
-= (char *)user_space_limit
- (char *)addr
;
335 addr
= user_space_limit
;
337 /* blow away existing mappings */
338 wine_anon_mmap( addr
, size
, PROT_NONE
, MAP_NORESERVE
| MAP_FIXED
);
339 wine_mmap_add_reserved_area( addr
, size
);
343 /***********************************************************************
346 * Check if an address range goes beyond a given limit.
348 static inline int is_beyond_limit( void *addr
, size_t size
, void *limit
)
350 return (limit
&& (addr
>= limit
|| (char *)addr
+ size
> (char *)limit
));
354 /***********************************************************************
357 * Unmap an area, or simply replace it by an empty mapping if it is
358 * in a reserved area. The csVirtual section must be held by caller.
360 static inline void unmap_area( void *addr
, size_t size
)
362 if (wine_mmap_is_in_reserved_area( addr
, size
))
363 wine_anon_mmap( addr
, size
, PROT_NONE
, MAP_NORESERVE
| MAP_FIXED
);
364 else if (is_beyond_limit( addr
, size
, user_space_limit
))
365 add_reserved_area( addr
, size
);
367 munmap( addr
, size
);
371 /***********************************************************************
374 * Deletes a view. The csVirtual section must be held by caller.
376 static void delete_view( struct file_view
*view
) /* [in] View */
378 if (!(view
->flags
& VFLAG_SYSTEM
)) unmap_area( view
->base
, view
->size
);
379 list_remove( &view
->entry
);
380 if (view
->mapping
) NtClose( view
->mapping
);
385 /***********************************************************************
388 * Create a view. The csVirtual section must be held by caller.
390 static NTSTATUS
create_view( struct file_view
**view_ret
, void *base
, size_t size
, BYTE vprot
)
392 struct file_view
*view
;
395 assert( !((UINT_PTR
)base
& page_mask
) );
396 assert( !(size
& page_mask
) );
398 /* Create the view structure */
400 if (!(view
= malloc( sizeof(*view
) + (size
>> page_shift
) - 1 ))) return STATUS_NO_MEMORY
;
406 view
->protect
= vprot
;
407 memset( view
->prot
, vprot
& ~VPROT_IMAGE
, size
>> page_shift
);
409 /* Insert it in the linked list */
411 LIST_FOR_EACH( ptr
, &views_list
)
413 struct file_view
*next
= LIST_ENTRY( ptr
, struct file_view
, entry
);
414 if (next
->base
> base
) break;
416 list_add_before( ptr
, &view
->entry
);
418 /* Check for overlapping views. This can happen if the previous view
419 * was a system view that got unmapped behind our back. In that case
420 * we recover by simply deleting it. */
422 if ((ptr
= list_prev( &views_list
, &view
->entry
)) != NULL
)
424 struct file_view
*prev
= LIST_ENTRY( ptr
, struct file_view
, entry
);
425 if ((char *)prev
->base
+ prev
->size
> (char *)base
)
427 TRACE( "overlapping prev view %p-%p for %p-%p\n",
428 prev
->base
, (char *)prev
->base
+ prev
->size
,
429 base
, (char *)base
+ view
->size
);
430 assert( prev
->flags
& VFLAG_SYSTEM
);
434 if ((ptr
= list_next( &views_list
, &view
->entry
)) != NULL
)
436 struct file_view
*next
= LIST_ENTRY( ptr
, struct file_view
, entry
);
437 if ((char *)base
+ view
->size
> (char *)next
->base
)
439 TRACE( "overlapping next view %p-%p for %p-%p\n",
440 next
->base
, (char *)next
->base
+ next
->size
,
441 base
, (char *)base
+ view
->size
);
442 assert( next
->flags
& VFLAG_SYSTEM
);
448 VIRTUAL_DEBUG_DUMP_VIEW( view
);
449 return STATUS_SUCCESS
;
453 /***********************************************************************
454 * VIRTUAL_GetUnixProt
456 * Convert page protections to protection for mmap/mprotect.
458 static int VIRTUAL_GetUnixProt( BYTE vprot
)
461 if ((vprot
& VPROT_COMMITTED
) && !(vprot
& VPROT_GUARD
))
463 if (vprot
& VPROT_READ
) prot
|= PROT_READ
;
464 if (vprot
& VPROT_WRITE
) prot
|= PROT_WRITE
;
465 if (vprot
& VPROT_WRITECOPY
) prot
|= PROT_WRITE
;
466 if (vprot
& VPROT_EXEC
) prot
|= PROT_EXEC
;
468 if (!prot
) prot
= PROT_NONE
;
473 /***********************************************************************
474 * VIRTUAL_GetWin32Prot
476 * Convert page protections to Win32 flags.
478 static DWORD
VIRTUAL_GetWin32Prot( BYTE vprot
)
480 DWORD ret
= VIRTUAL_Win32Flags
[vprot
& 0x0f];
481 if (vprot
& VPROT_NOCACHE
) ret
|= PAGE_NOCACHE
;
482 if (vprot
& VPROT_GUARD
) ret
|= PAGE_GUARD
;
487 /***********************************************************************
490 * Build page protections from Win32 flags.
493 * protect [I] Win32 protection flags
496 * Value of page protection flags
498 static BYTE
VIRTUAL_GetProt( DWORD protect
)
502 switch(protect
& 0xff)
508 vprot
= VPROT_READ
| VPROT_WRITE
;
511 /* MSDN CreateFileMapping() states that if PAGE_WRITECOPY is given,
512 * that the hFile must have been opened with GENERIC_READ and
513 * GENERIC_WRITE access. This is WRONG as tests show that you
514 * only need GENERIC_READ access (at least for Win9x,
515 * FIXME: what about NT?). Thus, we don't put VPROT_WRITE in
516 * PAGE_WRITECOPY and PAGE_EXECUTE_WRITECOPY.
518 vprot
= VPROT_READ
| VPROT_WRITECOPY
;
523 case PAGE_EXECUTE_READ
:
524 vprot
= VPROT_EXEC
| VPROT_READ
;
526 case PAGE_EXECUTE_READWRITE
:
527 vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITE
;
529 case PAGE_EXECUTE_WRITECOPY
:
530 /* See comment for PAGE_WRITECOPY above */
531 vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITECOPY
;
538 if (protect
& PAGE_GUARD
) vprot
|= VPROT_GUARD
;
539 if (protect
& PAGE_NOCACHE
) vprot
|= VPROT_NOCACHE
;
544 /***********************************************************************
547 * Change the protection of a range of pages.
553 static BOOL
VIRTUAL_SetProt( FILE_VIEW
*view
, /* [in] Pointer to view */
554 void *base
, /* [in] Starting address */
555 size_t size
, /* [in] Size in bytes */
556 BYTE vprot
) /* [in] Protections to use */
559 base
, (char *)base
+ size
- 1, VIRTUAL_GetProtStr( vprot
) );
561 if (mprotect( base
, size
, VIRTUAL_GetUnixProt(vprot
) ))
562 return FALSE
; /* FIXME: last error */
564 memset( view
->prot
+ (((char *)base
- (char *)view
->base
) >> page_shift
),
565 vprot
, size
>> page_shift
);
566 VIRTUAL_DEBUG_DUMP_VIEW( view
);
571 /***********************************************************************
574 * Release the extra memory while keeping the range starting on the granularity boundary.
576 static inline void *unmap_extra_space( void *ptr
, size_t total_size
, size_t wanted_size
, size_t mask
)
578 if ((ULONG_PTR
)ptr
& mask
)
580 size_t extra
= mask
+ 1 - ((ULONG_PTR
)ptr
& mask
);
581 munmap( ptr
, extra
);
582 ptr
= (char *)ptr
+ extra
;
585 if (total_size
> wanted_size
)
586 munmap( (char *)ptr
+ wanted_size
, total_size
- wanted_size
);
599 /***********************************************************************
600 * alloc_reserved_area_callback
602 * Try to map some space inside a reserved area. Callback for wine_mmap_enum_reserved_areas.
604 static int alloc_reserved_area_callback( void *start
, size_t size
, void *arg
)
606 static void * const address_space_start
= (void *)0x110000;
607 struct alloc_area
*alloc
= arg
;
608 void *end
= (char *)start
+ size
;
610 if (start
< address_space_start
) start
= address_space_start
;
611 if (user_space_limit
&& end
> user_space_limit
) end
= user_space_limit
;
612 if (start
>= end
) return 0;
614 /* make sure we don't touch the preloader reserved range */
615 if (preload_reserve_end
>= start
)
617 if (preload_reserve_end
>= end
)
619 if (preload_reserve_start
<= start
) return 0; /* no space in that area */
620 if (preload_reserve_start
< end
) end
= preload_reserve_start
;
622 else if (preload_reserve_start
<= start
) start
= preload_reserve_end
;
625 /* range is split in two by the preloader reservation, try first part */
626 if ((alloc
->result
= find_free_area( start
, preload_reserve_start
, alloc
->size
,
627 alloc
->mask
, alloc
->top_down
)))
629 /* then fall through to try second part */
630 start
= preload_reserve_end
;
633 if ((alloc
->result
= find_free_area( start
, end
, alloc
->size
, alloc
->mask
, alloc
->top_down
)))
640 /***********************************************************************
643 * Create a view and mmap the corresponding memory area.
644 * The csVirtual section must be held by caller.
646 static NTSTATUS
map_view( struct file_view
**view_ret
, void *base
, size_t size
, size_t mask
,
647 int top_down
, BYTE vprot
)
654 if (is_beyond_limit( base
, size
, ADDRESS_SPACE_LIMIT
))
655 return STATUS_WORKING_SET_LIMIT_RANGE
;
657 switch (wine_mmap_is_in_reserved_area( base
, size
))
659 case -1: /* partially in a reserved area */
660 return STATUS_CONFLICTING_ADDRESSES
;
662 case 0: /* not in a reserved area, do a normal allocation */
663 if ((ptr
= wine_anon_mmap( base
, size
, VIRTUAL_GetUnixProt(vprot
), 0 )) == (void *)-1)
665 if (errno
== ENOMEM
) return STATUS_NO_MEMORY
;
666 return STATUS_INVALID_PARAMETER
;
670 /* We couldn't get the address we wanted */
671 if (is_beyond_limit( ptr
, size
, user_space_limit
)) add_reserved_area( ptr
, size
);
672 else munmap( ptr
, size
);
673 return STATUS_CONFLICTING_ADDRESSES
;
678 case 1: /* in a reserved area, make sure the address is available */
679 if (find_view_range( base
, size
)) return STATUS_CONFLICTING_ADDRESSES
;
680 /* replace the reserved area by our mapping */
681 if ((ptr
= wine_anon_mmap( base
, size
, VIRTUAL_GetUnixProt(vprot
), MAP_FIXED
)) != base
)
682 return STATUS_INVALID_PARAMETER
;
688 size_t view_size
= size
+ mask
+ 1;
689 struct alloc_area alloc
;
693 alloc
.top_down
= top_down
;
694 if (wine_mmap_enum_reserved_areas( alloc_reserved_area_callback
, &alloc
, top_down
))
697 TRACE( "got mem in reserved area %p-%p\n", ptr
, (char *)ptr
+ size
);
698 if (wine_anon_mmap( ptr
, size
, VIRTUAL_GetUnixProt(vprot
), MAP_FIXED
) != ptr
)
699 return STATUS_INVALID_PARAMETER
;
705 if ((ptr
= wine_anon_mmap( NULL
, view_size
, VIRTUAL_GetUnixProt(vprot
), 0 )) == (void *)-1)
707 if (errno
== ENOMEM
) return STATUS_NO_MEMORY
;
708 return STATUS_INVALID_PARAMETER
;
710 TRACE( "got mem with anon mmap %p-%p\n", ptr
, (char *)ptr
+ size
);
711 /* if we got something beyond the user limit, unmap it and retry */
712 if (is_beyond_limit( ptr
, view_size
, user_space_limit
)) add_reserved_area( ptr
, view_size
);
715 ptr
= unmap_extra_space( ptr
, view_size
, size
, mask
);
718 status
= create_view( view_ret
, ptr
, size
, vprot
);
719 if (status
!= STATUS_SUCCESS
) unmap_area( ptr
, size
);
724 /***********************************************************************
727 * Linux kernels before 2.4.x can support non page-aligned offsets, as
728 * long as the offset is aligned to the filesystem block size. This is
729 * a big performance gain so we want to take advantage of it.
731 * However, when we use 64-bit file support this doesn't work because
732 * glibc rejects unaligned offsets. Also glibc 2.1.3 mmap64 is broken
733 * in that it rounds unaligned offsets down to a page boundary. For
734 * these reasons we do a direct system call here.
736 static void *unaligned_mmap( void *addr
, size_t length
, unsigned int prot
,
737 unsigned int flags
, int fd
, off_t offset
)
739 #if defined(linux) && defined(__i386__) && defined(__GNUC__)
740 if (!(offset
>> 32) && (offset
& page_mask
))
755 args
.length
= length
;
759 args
.offset
= offset
;
761 __asm__
__volatile__("push %%ebx\n\t"
766 : "0" (90), /* SYS_mmap */
769 if (ret
< 0 && ret
> -4096)
777 return mmap( addr
, length
, prot
, flags
, fd
, offset
);
781 /***********************************************************************
784 * Wrapper for mmap() to map a file into a view, falling back to read if mmap fails.
785 * The csVirtual section must be held by caller.
787 static NTSTATUS
map_file_into_view( struct file_view
*view
, int fd
, size_t start
, size_t size
,
788 off_t offset
, BYTE vprot
, BOOL removable
)
791 int prot
= VIRTUAL_GetUnixProt( vprot
);
792 BOOL shared_write
= (vprot
& VPROT_WRITE
) != 0;
794 assert( start
< view
->size
);
795 assert( start
+ size
<= view
->size
);
797 /* only try mmap if media is not removable (or if we require write access) */
798 if (!removable
|| shared_write
)
800 int flags
= MAP_FIXED
| (shared_write
? MAP_SHARED
: MAP_PRIVATE
);
802 if (unaligned_mmap( (char *)view
->base
+ start
, size
, prot
, flags
, fd
, offset
) != (void *)-1)
805 /* mmap() failed; if this is because the file offset is not */
806 /* page-aligned (EINVAL), or because the underlying filesystem */
807 /* does not support mmap() (ENOEXEC,ENODEV), we do it by hand. */
808 if ((errno
!= ENOEXEC
) && (errno
!= EINVAL
) && (errno
!= ENODEV
)) return FILE_GetNtStatus();
809 if (shared_write
) return FILE_GetNtStatus(); /* we cannot fake shared write mappings */
812 /* Reserve the memory with an anonymous mmap */
813 ptr
= wine_anon_mmap( (char *)view
->base
+ start
, size
, PROT_READ
| PROT_WRITE
, MAP_FIXED
);
814 if (ptr
== (void *)-1) return FILE_GetNtStatus();
815 /* Now read in the file */
816 pread( fd
, ptr
, size
, offset
);
817 if (prot
!= (PROT_READ
|PROT_WRITE
)) mprotect( ptr
, size
, prot
); /* Set the right protection */
819 memset( view
->prot
+ (start
>> page_shift
), vprot
, ROUND_SIZE(start
,size
) >> page_shift
);
820 return STATUS_SUCCESS
;
824 /***********************************************************************
827 * Decommit some pages of a given view.
828 * The csVirtual section must be held by caller.
830 static NTSTATUS
decommit_pages( struct file_view
*view
, size_t start
, size_t size
)
832 if (wine_anon_mmap( (char *)view
->base
+ start
, size
, PROT_NONE
, MAP_FIXED
) != (void *)-1)
834 BYTE
*p
= view
->prot
+ (start
>> page_shift
);
836 while (size
--) *p
++ &= ~VPROT_COMMITTED
;
837 return STATUS_SUCCESS
;
839 return FILE_GetNtStatus();
843 /***********************************************************************
846 * Apply the relocations to a mapped PE image
848 static int do_relocations( char *base
, const IMAGE_DATA_DIRECTORY
*dir
,
849 int delta
, SIZE_T total_size
)
851 IMAGE_BASE_RELOCATION
*rel
;
853 TRACE_(module
)( "relocating from %p-%p to %p-%p\n",
854 base
- delta
, base
- delta
+ total_size
, base
, base
+ total_size
);
856 for (rel
= (IMAGE_BASE_RELOCATION
*)(base
+ dir
->VirtualAddress
);
857 ((char *)rel
< base
+ dir
->VirtualAddress
+ dir
->Size
) && rel
->SizeOfBlock
;
858 rel
= (IMAGE_BASE_RELOCATION
*)((char*)rel
+ rel
->SizeOfBlock
) )
860 char *page
= base
+ rel
->VirtualAddress
;
861 WORD
*TypeOffset
= (WORD
*)(rel
+ 1);
862 int i
, count
= (rel
->SizeOfBlock
- sizeof(*rel
)) / sizeof(*TypeOffset
);
864 if (!count
) continue;
867 if ((char *)rel
+ rel
->SizeOfBlock
> base
+ dir
->VirtualAddress
+ dir
->Size
)
869 ERR_(module
)("invalid relocation %p,%x,%d at %p,%x,%x\n",
870 rel
, rel
->VirtualAddress
, rel
->SizeOfBlock
,
871 base
, dir
->VirtualAddress
, dir
->Size
);
875 if (page
> base
+ total_size
)
877 WARN_(module
)("skipping %d relocations for page %p beyond module %p-%p\n",
878 count
, page
, base
, base
+ total_size
);
882 TRACE_(module
)("%d relocations for page %x\n", count
, rel
->VirtualAddress
);
884 /* patching in reverse order */
885 for (i
= 0 ; i
< count
; i
++)
887 int offset
= TypeOffset
[i
] & 0xFFF;
888 int type
= TypeOffset
[i
] >> 12;
891 case IMAGE_REL_BASED_ABSOLUTE
:
893 case IMAGE_REL_BASED_HIGH
:
894 *(short*)(page
+offset
) += HIWORD(delta
);
896 case IMAGE_REL_BASED_LOW
:
897 *(short*)(page
+offset
) += LOWORD(delta
);
899 case IMAGE_REL_BASED_HIGHLOW
:
900 *(int*)(page
+offset
) += delta
;
901 /* FIXME: if this is an exported address, fire up enhanced logic */
904 FIXME_(module
)("Unknown/unsupported fixup type %d.\n", type
);
913 /***********************************************************************
916 * Map an executable (PE format) image into memory.
918 static NTSTATUS
map_image( HANDLE hmapping
, int fd
, char *base
, SIZE_T total_size
, SIZE_T mask
,
919 SIZE_T header_size
, int shared_fd
, BOOL removable
, PVOID
*addr_ptr
)
921 IMAGE_DOS_HEADER
*dos
;
922 IMAGE_NT_HEADERS
*nt
;
923 IMAGE_SECTION_HEADER
*sec
;
924 IMAGE_DATA_DIRECTORY
*imports
;
925 NTSTATUS status
= STATUS_CONFLICTING_ADDRESSES
;
929 struct file_view
*view
= NULL
;
930 char *ptr
, *header_end
;
932 /* zero-map the whole range */
934 RtlEnterCriticalSection( &csVirtual
);
936 if (base
>= (char *)0x110000) /* make sure the DOS area remains free */
937 status
= map_view( &view
, base
, total_size
, mask
, FALSE
,
938 VPROT_COMMITTED
| VPROT_READ
| VPROT_EXEC
| VPROT_WRITECOPY
| VPROT_IMAGE
);
940 if (status
== STATUS_CONFLICTING_ADDRESSES
)
941 status
= map_view( &view
, NULL
, total_size
, mask
, FALSE
,
942 VPROT_COMMITTED
| VPROT_READ
| VPROT_EXEC
| VPROT_WRITECOPY
| VPROT_IMAGE
);
944 if (status
!= STATUS_SUCCESS
) goto error
;
947 TRACE_(module
)( "mapped PE file at %p-%p\n", ptr
, ptr
+ total_size
);
951 if (fstat( fd
, &st
) == -1)
953 status
= FILE_GetNtStatus();
956 status
= STATUS_INVALID_IMAGE_FORMAT
; /* generic error */
957 if (!st
.st_size
) goto error
;
958 header_size
= min( header_size
, st
.st_size
);
959 if (map_file_into_view( view
, fd
, 0, header_size
, 0, VPROT_COMMITTED
| VPROT_READ
,
960 removable
) != STATUS_SUCCESS
) goto error
;
961 dos
= (IMAGE_DOS_HEADER
*)ptr
;
962 nt
= (IMAGE_NT_HEADERS
*)(ptr
+ dos
->e_lfanew
);
963 header_end
= ptr
+ ROUND_SIZE( 0, header_size
);
964 if ((char *)(nt
+ 1) > header_end
) goto error
;
965 sec
= (IMAGE_SECTION_HEADER
*)((char*)&nt
->OptionalHeader
+nt
->FileHeader
.SizeOfOptionalHeader
);
966 if ((char *)(sec
+ nt
->FileHeader
.NumberOfSections
) > header_end
) goto error
;
968 imports
= nt
->OptionalHeader
.DataDirectory
+ IMAGE_DIRECTORY_ENTRY_IMPORT
;
969 if (!imports
->Size
|| !imports
->VirtualAddress
) imports
= NULL
;
971 /* check the architecture */
973 if (nt
->FileHeader
.Machine
!= IMAGE_FILE_MACHINE_I386
)
975 MESSAGE("Trying to load PE image for unsupported architecture (");
976 switch (nt
->FileHeader
.Machine
)
978 case IMAGE_FILE_MACHINE_UNKNOWN
: MESSAGE("Unknown"); break;
979 case IMAGE_FILE_MACHINE_I860
: MESSAGE("I860"); break;
980 case IMAGE_FILE_MACHINE_R3000
: MESSAGE("R3000"); break;
981 case IMAGE_FILE_MACHINE_R4000
: MESSAGE("R4000"); break;
982 case IMAGE_FILE_MACHINE_R10000
: MESSAGE("R10000"); break;
983 case IMAGE_FILE_MACHINE_ALPHA
: MESSAGE("Alpha"); break;
984 case IMAGE_FILE_MACHINE_POWERPC
: MESSAGE("PowerPC"); break;
985 case IMAGE_FILE_MACHINE_IA64
: MESSAGE("IA-64"); break;
986 case IMAGE_FILE_MACHINE_ALPHA64
: MESSAGE("Alpha-64"); break;
987 case IMAGE_FILE_MACHINE_AMD64
: MESSAGE("AMD-64"); break;
988 case IMAGE_FILE_MACHINE_ARM
: MESSAGE("ARM"); break;
989 default: MESSAGE("Unknown-%04x", nt
->FileHeader
.Machine
); break;
995 /* check for non page-aligned binary */
997 if (nt
->OptionalHeader
.SectionAlignment
<= page_mask
)
999 /* unaligned sections, this happens for native subsystem binaries */
1000 /* in that case Windows simply maps in the whole file */
1002 if (map_file_into_view( view
, fd
, 0, total_size
, 0, VPROT_COMMITTED
| VPROT_READ
,
1003 removable
) != STATUS_SUCCESS
) goto error
;
1005 /* check that all sections are loaded at the right offset */
1006 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++)
1008 if (sec
[i
].VirtualAddress
!= sec
[i
].PointerToRawData
)
1009 goto error
; /* Windows refuses to load in that case too */
1012 /* set the image protections */
1013 VIRTUAL_SetProt( view
, ptr
, total_size
,
1014 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
| VPROT_EXEC
);
1016 /* perform relocations if necessary */
1017 /* FIXME: not 100% compatible, Windows doesn't do this for non page-aligned binaries */
1020 const IMAGE_DATA_DIRECTORY
*relocs
;
1021 relocs
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1022 if (relocs
->VirtualAddress
&& relocs
->Size
)
1023 do_relocations( ptr
, relocs
, ptr
- base
, total_size
);
1030 /* map all the sections */
1032 for (i
= pos
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
1034 SIZE_T map_size
, file_size
, end
;
1036 if (!sec
->Misc
.VirtualSize
)
1038 file_size
= sec
->SizeOfRawData
;
1039 map_size
= ROUND_SIZE( 0, file_size
);
1043 map_size
= ROUND_SIZE( 0, sec
->Misc
.VirtualSize
);
1044 file_size
= min( sec
->SizeOfRawData
, map_size
);
1047 /* a few sanity checks */
1048 end
= sec
->VirtualAddress
+ ROUND_SIZE( sec
->VirtualAddress
, map_size
);
1049 if (sec
->VirtualAddress
> total_size
|| end
> total_size
|| end
< sec
->VirtualAddress
)
1051 ERR_(module
)( "Section %.8s too large (%x+%lx/%lx)\n",
1052 sec
->Name
, sec
->VirtualAddress
, map_size
, total_size
);
1056 if ((sec
->Characteristics
& IMAGE_SCN_MEM_SHARED
) &&
1057 (sec
->Characteristics
& IMAGE_SCN_MEM_WRITE
))
1059 TRACE_(module
)( "mapping shared section %.8s at %p off %x (%x) size %lx (%lx) flags %x\n",
1060 sec
->Name
, ptr
+ sec
->VirtualAddress
,
1061 sec
->PointerToRawData
, (int)pos
, file_size
, map_size
,
1062 sec
->Characteristics
);
1063 if (map_file_into_view( view
, shared_fd
, sec
->VirtualAddress
, map_size
, pos
,
1064 VPROT_COMMITTED
| VPROT_READ
| PROT_WRITE
,
1065 FALSE
) != STATUS_SUCCESS
)
1067 ERR_(module
)( "Could not map shared section %.8s\n", sec
->Name
);
1071 /* check if the import directory falls inside this section */
1072 if (imports
&& imports
->VirtualAddress
>= sec
->VirtualAddress
&&
1073 imports
->VirtualAddress
< sec
->VirtualAddress
+ map_size
)
1075 UINT_PTR base
= imports
->VirtualAddress
& ~page_mask
;
1076 UINT_PTR end
= base
+ ROUND_SIZE( imports
->VirtualAddress
, imports
->Size
);
1077 if (end
> sec
->VirtualAddress
+ map_size
) end
= sec
->VirtualAddress
+ map_size
;
1079 map_file_into_view( view
, shared_fd
, base
, end
- base
,
1080 pos
+ (base
- sec
->VirtualAddress
),
1081 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
,
1088 TRACE_(module
)( "mapping section %.8s at %p off %x size %x virt %x flags %x\n",
1089 sec
->Name
, ptr
+ sec
->VirtualAddress
,
1090 sec
->PointerToRawData
, sec
->SizeOfRawData
,
1091 sec
->Misc
.VirtualSize
, sec
->Characteristics
);
1093 if (!sec
->PointerToRawData
|| !file_size
) continue;
1095 /* Note: if the section is not aligned properly map_file_into_view will magically
1096 * fall back to read(), so we don't need to check anything here.
1098 end
= sec
->PointerToRawData
+ file_size
;
1099 if (sec
->PointerToRawData
>= st
.st_size
|| end
> st
.st_size
|| end
< sec
->PointerToRawData
||
1100 map_file_into_view( view
, fd
, sec
->VirtualAddress
, file_size
, sec
->PointerToRawData
,
1101 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
,
1102 removable
) != STATUS_SUCCESS
)
1104 ERR_(module
)( "Could not map section %.8s, file probably truncated\n", sec
->Name
);
1108 if (file_size
& page_mask
)
1110 end
= ROUND_SIZE( 0, file_size
);
1111 if (end
> map_size
) end
= map_size
;
1112 TRACE_(module
)("clearing %p - %p\n",
1113 ptr
+ sec
->VirtualAddress
+ file_size
,
1114 ptr
+ sec
->VirtualAddress
+ end
);
1115 memset( ptr
+ sec
->VirtualAddress
+ file_size
, 0, end
- file_size
);
1120 /* perform base relocation, if necessary */
1124 const IMAGE_DATA_DIRECTORY
*relocs
;
1126 relocs
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1127 if (nt
->FileHeader
.Characteristics
& IMAGE_FILE_RELOCS_STRIPPED
)
1129 WARN( "Need to relocate module from addr %x, but there are no relocation records\n",
1130 nt
->OptionalHeader
.ImageBase
);
1131 status
= STATUS_CONFLICTING_ADDRESSES
;
1135 /* FIXME: If we need to relocate a system DLL (base > 2GB) we should
1136 * really make sure that the *new* base address is also > 2GB.
1137 * Some DLLs really check the MSB of the module handle :-/
1139 if ((nt
->OptionalHeader
.ImageBase
& 0x80000000) && !((ULONG_PTR
)base
& 0x80000000))
1140 ERR( "Forced to relocate system DLL (base > 2GB). This is not good.\n" );
1142 if (!do_relocations( ptr
, relocs
, ptr
- base
, total_size
))
1148 /* set the image protections */
1150 sec
= (IMAGE_SECTION_HEADER
*)((char *)&nt
->OptionalHeader
+nt
->FileHeader
.SizeOfOptionalHeader
);
1151 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
1154 BYTE vprot
= VPROT_COMMITTED
;
1156 if (sec
->Misc
.VirtualSize
)
1157 size
= ROUND_SIZE( sec
->VirtualAddress
, sec
->Misc
.VirtualSize
);
1159 size
= ROUND_SIZE( sec
->VirtualAddress
, sec
->SizeOfRawData
);
1161 if (sec
->Characteristics
& IMAGE_SCN_MEM_READ
) vprot
|= VPROT_READ
;
1162 if (sec
->Characteristics
& IMAGE_SCN_MEM_WRITE
) vprot
|= VPROT_READ
|VPROT_WRITECOPY
;
1163 if (sec
->Characteristics
& IMAGE_SCN_MEM_EXECUTE
) vprot
|= VPROT_EXEC
;
1165 /* Dumb game crack lets the AOEP point into a data section. Adjust. */
1166 if ((nt
->OptionalHeader
.AddressOfEntryPoint
>= sec
->VirtualAddress
) &&
1167 (nt
->OptionalHeader
.AddressOfEntryPoint
< sec
->VirtualAddress
+ size
))
1168 vprot
|= VPROT_EXEC
;
1170 VIRTUAL_SetProt( view
, ptr
+ sec
->VirtualAddress
, size
, vprot
);
1174 if (!removable
) /* don't keep handle open on removable media */
1175 NtDuplicateObject( NtCurrentProcess(), hmapping
,
1176 NtCurrentProcess(), &view
->mapping
,
1177 0, 0, DUPLICATE_SAME_ACCESS
);
1179 RtlLeaveCriticalSection( &csVirtual
);
1182 return STATUS_SUCCESS
;
1185 if (view
) delete_view( view
);
1186 RtlLeaveCriticalSection( &csVirtual
);
1191 /***********************************************************************
1192 * is_current_process
1194 * Check whether a process handle is for the current process.
1196 BOOL
is_current_process( HANDLE handle
)
1200 if (handle
== NtCurrentProcess()) return TRUE
;
1201 SERVER_START_REQ( get_process_info
)
1203 req
->handle
= handle
;
1204 if (!wine_server_call( req
))
1205 ret
= ((DWORD
)reply
->pid
== GetCurrentProcessId());
1212 /***********************************************************************
1215 void virtual_init(void)
1217 const char *preload
;
1219 page_size
= getpagesize();
1220 page_mask
= page_size
- 1;
1221 /* Make sure we have a power of 2 */
1222 assert( !(page_size
& page_mask
) );
1224 while ((1 << page_shift
) != page_size
) page_shift
++;
1225 #endif /* page_mask */
1226 if ((preload
= getenv("WINEPRELOADRESERVE")))
1228 unsigned long start
, end
;
1229 if (sscanf( preload
, "%lx-%lx", &start
, &end
) == 2)
1231 preload_reserve_start
= (void *)start
;
1232 preload_reserve_end
= (void *)end
;
1238 /***********************************************************************
1239 * virtual_init_threading
1241 void virtual_init_threading(void)
1247 /***********************************************************************
1248 * VIRTUAL_HandleFault
1250 NTSTATUS
VIRTUAL_HandleFault( LPCVOID addr
)
1253 NTSTATUS ret
= STATUS_ACCESS_VIOLATION
;
1255 RtlEnterCriticalSection( &csVirtual
);
1256 if ((view
= VIRTUAL_FindView( addr
)))
1258 void *page
= ROUND_ADDR( addr
, page_mask
);
1259 BYTE vprot
= view
->prot
[((const char *)page
- (const char *)view
->base
) >> page_shift
];
1260 if (vprot
& VPROT_GUARD
)
1262 VIRTUAL_SetProt( view
, page
, page_size
, vprot
& ~VPROT_GUARD
);
1263 ret
= STATUS_GUARD_PAGE_VIOLATION
;
1266 RtlLeaveCriticalSection( &csVirtual
);
1270 /***********************************************************************
1271 * VIRTUAL_HasMapping
1273 * Check if the specified view has an associated file mapping.
1275 BOOL
VIRTUAL_HasMapping( LPCVOID addr
)
1280 RtlEnterCriticalSection( &csVirtual
);
1281 if ((view
= VIRTUAL_FindView( addr
))) ret
= (view
->mapping
!= 0);
1282 RtlLeaveCriticalSection( &csVirtual
);
1287 /***********************************************************************
1288 * VIRTUAL_UseLargeAddressSpace
1290 * Increase the address space size for apps that support it.
1292 void VIRTUAL_UseLargeAddressSpace(void)
1294 /* no large address space on win9x */
1295 if (NtCurrentTeb()->Peb
->OSPlatformId
!= VER_PLATFORM_WIN32_NT
) return;
1296 user_space_limit
= ADDRESS_SPACE_LIMIT
;
1300 /***********************************************************************
1301 * NtAllocateVirtualMemory (NTDLL.@)
1302 * ZwAllocateVirtualMemory (NTDLL.@)
1304 NTSTATUS WINAPI
NtAllocateVirtualMemory( HANDLE process
, PVOID
*ret
, ULONG zero_bits
,
1305 SIZE_T
*size_ptr
, ULONG type
, ULONG protect
)
1309 SIZE_T size
= *size_ptr
;
1310 SIZE_T mask
= get_mask( zero_bits
);
1311 NTSTATUS status
= STATUS_SUCCESS
;
1312 struct file_view
*view
;
1314 TRACE("%p %p %08lx %x %08x\n", process
, *ret
, size
, type
, protect
);
1316 if (!size
) return STATUS_INVALID_PARAMETER
;
1318 if (!is_current_process( process
))
1320 ERR("Unsupported on other process\n");
1321 return STATUS_ACCESS_DENIED
;
1324 /* Round parameters to a page boundary */
1326 if (size
> 0x7fc00000) return STATUS_WORKING_SET_LIMIT_RANGE
; /* 2Gb - 4Mb */
1330 if (type
& MEM_RESERVE
) /* Round down to 64k boundary */
1331 base
= ROUND_ADDR( *ret
, mask
);
1333 base
= ROUND_ADDR( *ret
, page_mask
);
1334 size
= (((UINT_PTR
)*ret
+ size
+ page_mask
) & ~page_mask
) - (UINT_PTR
)base
;
1336 /* disallow low 64k, wrap-around and kernel space */
1337 if (((char *)base
< (char *)0x10000) ||
1338 ((char *)base
+ size
< (char *)base
) ||
1339 is_beyond_limit( base
, size
, ADDRESS_SPACE_LIMIT
))
1340 return STATUS_INVALID_PARAMETER
;
1345 size
= (size
+ page_mask
) & ~page_mask
;
1348 /* Compute the alloc type flags */
1350 if (!(type
& MEM_SYSTEM
))
1352 if (!(type
& (MEM_COMMIT
| MEM_RESERVE
)) ||
1353 (type
& ~(MEM_COMMIT
| MEM_RESERVE
| MEM_TOP_DOWN
| MEM_WRITE_WATCH
| MEM_RESET
)))
1355 WARN("called with wrong alloc type flags (%08x) !\n", type
);
1356 return STATUS_INVALID_PARAMETER
;
1358 if (type
& MEM_WRITE_WATCH
)
1360 FIXME("MEM_WRITE_WATCH type not supported\n");
1361 return STATUS_NOT_SUPPORTED
;
1364 vprot
= VIRTUAL_GetProt( protect
);
1365 if (type
& MEM_COMMIT
) vprot
|= VPROT_COMMITTED
;
1367 /* Reserve the memory */
1369 if (use_locks
) RtlEnterCriticalSection( &csVirtual
);
1371 if (type
& MEM_SYSTEM
)
1373 if (type
& MEM_IMAGE
) vprot
|= VPROT_IMAGE
;
1374 status
= create_view( &view
, base
, size
, vprot
| VPROT_COMMITTED
);
1375 if (status
== STATUS_SUCCESS
)
1377 view
->flags
|= VFLAG_VALLOC
| VFLAG_SYSTEM
;
1381 else if ((type
& MEM_RESERVE
) || !base
)
1383 status
= map_view( &view
, base
, size
, mask
, type
& MEM_TOP_DOWN
, vprot
);
1384 if (status
== STATUS_SUCCESS
)
1386 view
->flags
|= VFLAG_VALLOC
;
1390 else /* commit the pages */
1392 if (!(view
= VIRTUAL_FindView( base
)) ||
1393 ((char *)base
+ size
> (char *)view
->base
+ view
->size
)) status
= STATUS_NOT_MAPPED_VIEW
;
1394 else if (!VIRTUAL_SetProt( view
, base
, size
, vprot
)) status
= STATUS_ACCESS_DENIED
;
1397 if (use_locks
) RtlLeaveCriticalSection( &csVirtual
);
1399 if (status
== STATUS_SUCCESS
)
1408 /***********************************************************************
1409 * NtFreeVirtualMemory (NTDLL.@)
1410 * ZwFreeVirtualMemory (NTDLL.@)
1412 NTSTATUS WINAPI
NtFreeVirtualMemory( HANDLE process
, PVOID
*addr_ptr
, SIZE_T
*size_ptr
, ULONG type
)
1416 NTSTATUS status
= STATUS_SUCCESS
;
1417 LPVOID addr
= *addr_ptr
;
1418 SIZE_T size
= *size_ptr
;
1420 TRACE("%p %p %08lx %x\n", process
, addr
, size
, type
);
1422 if (!is_current_process( process
))
1424 ERR("Unsupported on other process\n");
1425 return STATUS_ACCESS_DENIED
;
1428 /* Fix the parameters */
1430 size
= ROUND_SIZE( addr
, size
);
1431 base
= ROUND_ADDR( addr
, page_mask
);
1433 RtlEnterCriticalSection(&csVirtual
);
1435 if (!(view
= VIRTUAL_FindView( base
)) ||
1436 (base
+ size
> (char *)view
->base
+ view
->size
) ||
1437 !(view
->flags
& VFLAG_VALLOC
))
1439 status
= STATUS_INVALID_PARAMETER
;
1441 else if (type
& MEM_SYSTEM
)
1443 /* return the values that the caller should use to unmap the area */
1444 *addr_ptr
= view
->base
;
1445 if (!wine_mmap_is_in_reserved_area( view
->base
, view
->size
)) *size_ptr
= view
->size
;
1446 else *size_ptr
= 0; /* make sure we don't munmap anything from a reserved area */
1447 view
->flags
|= VFLAG_SYSTEM
;
1448 delete_view( view
);
1450 else if (type
== MEM_RELEASE
)
1452 /* Free the pages */
1454 if (size
|| (base
!= view
->base
)) status
= STATUS_INVALID_PARAMETER
;
1457 delete_view( view
);
1462 else if (type
== MEM_DECOMMIT
)
1464 status
= decommit_pages( view
, base
- (char *)view
->base
, size
);
1465 if (status
== STATUS_SUCCESS
)
1473 WARN("called with wrong free type flags (%08x) !\n", type
);
1474 status
= STATUS_INVALID_PARAMETER
;
1477 RtlLeaveCriticalSection(&csVirtual
);
1482 /***********************************************************************
1483 * NtProtectVirtualMemory (NTDLL.@)
1484 * ZwProtectVirtualMemory (NTDLL.@)
1486 NTSTATUS WINAPI
NtProtectVirtualMemory( HANDLE process
, PVOID
*addr_ptr
, SIZE_T
*size_ptr
,
1487 ULONG new_prot
, ULONG
*old_prot
)
1490 NTSTATUS status
= STATUS_SUCCESS
;
1495 SIZE_T size
= *size_ptr
;
1496 LPVOID addr
= *addr_ptr
;
1498 TRACE("%p %p %08lx %08x\n", process
, addr
, size
, new_prot
);
1500 if (!is_current_process( process
))
1502 ERR("Unsupported on other process\n");
1503 return STATUS_ACCESS_DENIED
;
1506 /* Fix the parameters */
1508 size
= ROUND_SIZE( addr
, size
);
1509 base
= ROUND_ADDR( addr
, page_mask
);
1511 RtlEnterCriticalSection( &csVirtual
);
1513 if (!(view
= VIRTUAL_FindView( base
)) || (base
+ size
> (char *)view
->base
+ view
->size
))
1515 status
= STATUS_INVALID_PARAMETER
;
1519 /* Make sure all the pages are committed */
1521 p
= view
->prot
+ ((base
- (char *)view
->base
) >> page_shift
);
1522 prot
= VIRTUAL_GetWin32Prot( *p
);
1523 for (i
= size
>> page_shift
; i
; i
--, p
++)
1525 if (!(*p
& VPROT_COMMITTED
))
1527 status
= STATUS_NOT_COMMITTED
;
1533 if (old_prot
) *old_prot
= prot
;
1534 vprot
= VIRTUAL_GetProt( new_prot
) | VPROT_COMMITTED
;
1535 if (!VIRTUAL_SetProt( view
, base
, size
, vprot
)) status
= STATUS_ACCESS_DENIED
;
1538 RtlLeaveCriticalSection( &csVirtual
);
1540 if (status
== STATUS_SUCCESS
)
1548 #define UNIMPLEMENTED_INFO_CLASS(c) \
1550 FIXME("(process=%p,addr=%p) Unimplemented information class: " #c "\n", process, addr); \
1551 return STATUS_INVALID_INFO_CLASS
1553 /***********************************************************************
1554 * NtQueryVirtualMemory (NTDLL.@)
1555 * ZwQueryVirtualMemory (NTDLL.@)
1557 NTSTATUS WINAPI
NtQueryVirtualMemory( HANDLE process
, LPCVOID addr
,
1558 MEMORY_INFORMATION_CLASS info_class
, PVOID buffer
,
1559 SIZE_T len
, SIZE_T
*res_len
)
1562 char *base
, *alloc_base
= 0;
1565 MEMORY_BASIC_INFORMATION
*info
= buffer
;
1567 if (info_class
!= MemoryBasicInformation
)
1571 UNIMPLEMENTED_INFO_CLASS(MemoryWorkingSetList
);
1572 UNIMPLEMENTED_INFO_CLASS(MemorySectionName
);
1573 UNIMPLEMENTED_INFO_CLASS(MemoryBasicVlmInformation
);
1576 FIXME("(%p,%p,info_class=%d,%p,%ld,%p) Unknown information class\n",
1577 process
, addr
, info_class
, buffer
, len
, res_len
);
1578 return STATUS_INVALID_INFO_CLASS
;
1581 if (ADDRESS_SPACE_LIMIT
&& addr
>= ADDRESS_SPACE_LIMIT
)
1582 return STATUS_WORKING_SET_LIMIT_RANGE
;
1584 if (!is_current_process( process
))
1586 ERR("Unsupported on other process\n");
1587 return STATUS_ACCESS_DENIED
;
1590 base
= ROUND_ADDR( addr
, page_mask
);
1592 /* Find the view containing the address */
1594 RtlEnterCriticalSection(&csVirtual
);
1595 ptr
= list_head( &views_list
);
1600 /* make the address space end at the user limit, except if
1601 * the last view was mapped beyond that */
1602 if (alloc_base
<= (char *)user_space_limit
)
1604 if (user_space_limit
&& base
>= (char *)user_space_limit
)
1606 RtlLeaveCriticalSection( &csVirtual
);
1607 return STATUS_WORKING_SET_LIMIT_RANGE
;
1609 size
= (char *)user_space_limit
- alloc_base
;
1611 else size
= (char *)ADDRESS_SPACE_LIMIT
- alloc_base
;
1615 view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
1616 if ((char *)view
->base
> base
)
1618 size
= (char *)view
->base
- alloc_base
;
1622 if ((char *)view
->base
+ view
->size
> base
)
1624 alloc_base
= view
->base
;
1628 alloc_base
= (char *)view
->base
+ view
->size
;
1629 ptr
= list_next( &views_list
, ptr
);
1632 /* Fill the info structure */
1636 info
->State
= MEM_FREE
;
1638 info
->AllocationProtect
= 0;
1643 BYTE vprot
= view
->prot
[(base
- alloc_base
) >> page_shift
];
1644 info
->State
= (vprot
& VPROT_COMMITTED
) ? MEM_COMMIT
: MEM_RESERVE
;
1645 info
->Protect
= VIRTUAL_GetWin32Prot( vprot
);
1646 info
->AllocationProtect
= VIRTUAL_GetWin32Prot( view
->protect
);
1647 if (view
->protect
& VPROT_IMAGE
) info
->Type
= MEM_IMAGE
;
1648 else if (view
->flags
& VFLAG_VALLOC
) info
->Type
= MEM_PRIVATE
;
1649 else info
->Type
= MEM_MAPPED
;
1650 for (size
= base
- alloc_base
; size
< view
->size
; size
+= page_size
)
1651 if (view
->prot
[size
>> page_shift
] != vprot
) break;
1653 RtlLeaveCriticalSection(&csVirtual
);
1655 info
->BaseAddress
= (LPVOID
)base
;
1656 info
->AllocationBase
= (LPVOID
)alloc_base
;
1657 info
->RegionSize
= size
- (base
- alloc_base
);
1658 if (res_len
) *res_len
= sizeof(*info
);
1659 return STATUS_SUCCESS
;
1663 /***********************************************************************
1664 * NtLockVirtualMemory (NTDLL.@)
1665 * ZwLockVirtualMemory (NTDLL.@)
1667 NTSTATUS WINAPI
NtLockVirtualMemory( HANDLE process
, PVOID
*addr
, SIZE_T
*size
, ULONG unknown
)
1669 if (!is_current_process( process
))
1671 ERR("Unsupported on other process\n");
1672 return STATUS_ACCESS_DENIED
;
1674 return STATUS_SUCCESS
;
1678 /***********************************************************************
1679 * NtUnlockVirtualMemory (NTDLL.@)
1680 * ZwUnlockVirtualMemory (NTDLL.@)
1682 NTSTATUS WINAPI
NtUnlockVirtualMemory( HANDLE process
, PVOID
*addr
, SIZE_T
*size
, ULONG unknown
)
1684 if (!is_current_process( process
))
1686 ERR("Unsupported on other process\n");
1687 return STATUS_ACCESS_DENIED
;
1689 return STATUS_SUCCESS
;
1693 /***********************************************************************
1694 * NtCreateSection (NTDLL.@)
1695 * ZwCreateSection (NTDLL.@)
1697 NTSTATUS WINAPI
NtCreateSection( HANDLE
*handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
,
1698 const LARGE_INTEGER
*size
, ULONG protect
,
1699 ULONG sec_flags
, HANDLE file
)
1703 DWORD len
= (attr
&& attr
->ObjectName
) ? attr
->ObjectName
->Length
: 0;
1705 /* Check parameters */
1707 if (len
> MAX_PATH
*sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
1709 vprot
= VIRTUAL_GetProt( protect
);
1710 if (sec_flags
& SEC_RESERVE
)
1712 if (file
) return STATUS_INVALID_PARAMETER
;
1714 else vprot
|= VPROT_COMMITTED
;
1715 if (sec_flags
& SEC_NOCACHE
) vprot
|= VPROT_NOCACHE
;
1716 if (sec_flags
& SEC_IMAGE
) vprot
|= VPROT_IMAGE
;
1718 /* Create the server object */
1720 SERVER_START_REQ( create_mapping
)
1722 req
->access
= access
;
1723 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
1724 req
->rootdir
= attr
? attr
->RootDirectory
: 0;
1725 req
->file_handle
= file
;
1726 req
->size_high
= size
? size
->u
.HighPart
: 0;
1727 req
->size_low
= size
? size
->u
.LowPart
: 0;
1728 req
->protect
= vprot
;
1729 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
1730 ret
= wine_server_call( req
);
1731 *handle
= reply
->handle
;
1738 /***********************************************************************
1739 * NtOpenSection (NTDLL.@)
1740 * ZwOpenSection (NTDLL.@)
1742 NTSTATUS WINAPI
NtOpenSection( HANDLE
*handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
)
1745 DWORD len
= attr
->ObjectName
->Length
;
1747 if (len
> MAX_PATH
*sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
1749 SERVER_START_REQ( open_mapping
)
1751 req
->access
= access
;
1752 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
1753 req
->rootdir
= attr
? attr
->RootDirectory
: 0;
1754 wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
1755 if (!(ret
= wine_server_call( req
))) *handle
= reply
->handle
;
1762 /***********************************************************************
1763 * NtMapViewOfSection (NTDLL.@)
1764 * ZwMapViewOfSection (NTDLL.@)
1766 NTSTATUS WINAPI
NtMapViewOfSection( HANDLE handle
, HANDLE process
, PVOID
*addr_ptr
, ULONG zero_bits
,
1767 SIZE_T commit_size
, const LARGE_INTEGER
*offset_ptr
, SIZE_T
*size_ptr
,
1768 SECTION_INHERIT inherit
, ULONG alloc_type
, ULONG protect
)
1770 FILE_FS_DEVICE_INFORMATION device_info
;
1773 SIZE_T mask
= get_mask( zero_bits
);
1774 int unix_handle
= -1;
1777 struct file_view
*view
;
1778 DWORD size_low
, size_high
, header_size
, shared_size
;
1780 BOOL removable
= FALSE
;
1781 LARGE_INTEGER offset
;
1783 offset
.QuadPart
= offset_ptr
? offset_ptr
->QuadPart
: 0;
1785 TRACE("handle=%p process=%p addr=%p off=%x%08x size=%lx access=%x\n",
1786 handle
, process
, *addr_ptr
, offset
.u
.HighPart
, offset
.u
.LowPart
, size
, protect
);
1788 if (!is_current_process( process
))
1790 ERR("Unsupported on other process\n");
1791 return STATUS_ACCESS_DENIED
;
1794 /* Check parameters */
1796 if ((offset
.u
.LowPart
& mask
) || (*addr_ptr
&& ((UINT_PTR
)*addr_ptr
& mask
)))
1797 return STATUS_INVALID_PARAMETER
;
1799 SERVER_START_REQ( get_mapping_info
)
1801 req
->handle
= handle
;
1802 res
= wine_server_call( req
);
1803 prot
= reply
->protect
;
1805 size_low
= reply
->size_low
;
1806 size_high
= reply
->size_high
;
1807 header_size
= reply
->header_size
;
1808 shared_file
= reply
->shared_file
;
1809 shared_size
= reply
->shared_size
;
1812 if (res
) return res
;
1814 if ((res
= wine_server_handle_to_fd( handle
, 0, &unix_handle
, NULL
))) return res
;
1816 if (FILE_GetDeviceInfo( unix_handle
, &device_info
) == STATUS_SUCCESS
)
1817 removable
= device_info
.Characteristics
& FILE_REMOVABLE_MEDIA
;
1819 if (prot
& VPROT_IMAGE
)
1825 if ((res
= wine_server_handle_to_fd( shared_file
, FILE_READ_DATA
|FILE_WRITE_DATA
,
1826 &shared_fd
, NULL
))) goto done
;
1827 res
= map_image( handle
, unix_handle
, base
, size_low
, mask
, header_size
,
1828 shared_fd
, removable
, addr_ptr
);
1829 wine_server_release_fd( shared_file
, shared_fd
);
1830 NtClose( shared_file
);
1834 res
= map_image( handle
, unix_handle
, base
, size_low
, mask
, header_size
,
1835 -1, removable
, addr_ptr
);
1837 wine_server_release_fd( handle
, unix_handle
);
1838 if (!res
) *size_ptr
= size_low
;
1843 ERR("Sizes larger than 4Gb not supported\n");
1845 if ((offset
.u
.LowPart
>= size_low
) ||
1846 (*size_ptr
> size_low
- offset
.u
.LowPart
))
1848 res
= STATUS_INVALID_PARAMETER
;
1851 if (*size_ptr
) size
= ROUND_SIZE( offset
.u
.LowPart
, *size_ptr
);
1852 else size
= size_low
- offset
.u
.LowPart
;
1858 case PAGE_READWRITE
:
1859 case PAGE_EXECUTE_READWRITE
:
1860 if (!(prot
& VPROT_WRITE
))
1862 res
= STATUS_INVALID_PARAMETER
;
1868 case PAGE_WRITECOPY
:
1870 case PAGE_EXECUTE_READ
:
1871 case PAGE_EXECUTE_WRITECOPY
:
1872 if (prot
& VPROT_READ
) break;
1875 res
= STATUS_INVALID_PARAMETER
;
1879 /* FIXME: If a mapping is created with SEC_RESERVE and a process,
1880 * which has a view of this mapping commits some pages, they will
1881 * appear commited in all other processes, which have the same
1882 * view created. Since we don`t support this yet, we create the
1883 * whole mapping commited.
1885 prot
|= VPROT_COMMITTED
;
1887 /* Reserve a properly aligned area */
1889 RtlEnterCriticalSection( &csVirtual
);
1891 res
= map_view( &view
, *addr_ptr
, size
, mask
, FALSE
, prot
);
1894 RtlLeaveCriticalSection( &csVirtual
);
1900 TRACE("handle=%p size=%lx offset=%x%08x\n",
1901 handle
, size
, offset
.u
.HighPart
, offset
.u
.LowPart
);
1903 res
= map_file_into_view( view
, unix_handle
, 0, size
, offset
.QuadPart
, prot
, removable
);
1904 if (res
== STATUS_SUCCESS
)
1906 if (!removable
) /* don't keep handle open on removable media */
1907 NtDuplicateObject( NtCurrentProcess(), handle
,
1908 NtCurrentProcess(), &view
->mapping
,
1909 0, 0, DUPLICATE_SAME_ACCESS
);
1911 *addr_ptr
= view
->base
;
1916 ERR( "map_file_into_view %p %lx %x%08x failed\n",
1917 view
->base
, size
, offset
.u
.HighPart
, offset
.u
.LowPart
);
1918 delete_view( view
);
1921 RtlLeaveCriticalSection( &csVirtual
);
1924 wine_server_release_fd( handle
, unix_handle
);
1929 /***********************************************************************
1930 * NtUnmapViewOfSection (NTDLL.@)
1931 * ZwUnmapViewOfSection (NTDLL.@)
1933 NTSTATUS WINAPI
NtUnmapViewOfSection( HANDLE process
, PVOID addr
)
1936 NTSTATUS status
= STATUS_INVALID_PARAMETER
;
1937 void *base
= ROUND_ADDR( addr
, page_mask
);
1939 if (!is_current_process( process
))
1941 ERR("Unsupported on other process\n");
1942 return STATUS_ACCESS_DENIED
;
1944 RtlEnterCriticalSection( &csVirtual
);
1945 if ((view
= VIRTUAL_FindView( base
)) && (base
== view
->base
))
1947 delete_view( view
);
1948 status
= STATUS_SUCCESS
;
1950 RtlLeaveCriticalSection( &csVirtual
);
1955 /***********************************************************************
1956 * NtFlushVirtualMemory (NTDLL.@)
1957 * ZwFlushVirtualMemory (NTDLL.@)
1959 NTSTATUS WINAPI
NtFlushVirtualMemory( HANDLE process
, LPCVOID
*addr_ptr
,
1960 SIZE_T
*size_ptr
, ULONG unknown
)
1963 NTSTATUS status
= STATUS_SUCCESS
;
1964 void *addr
= ROUND_ADDR( *addr_ptr
, page_mask
);
1966 if (!is_current_process( process
))
1968 ERR("Unsupported on other process\n");
1969 return STATUS_ACCESS_DENIED
;
1971 RtlEnterCriticalSection( &csVirtual
);
1972 if (!(view
= VIRTUAL_FindView( addr
))) status
= STATUS_INVALID_PARAMETER
;
1975 if (!*size_ptr
) *size_ptr
= view
->size
;
1977 if (msync( addr
, *size_ptr
, MS_SYNC
)) status
= STATUS_NOT_MAPPED_DATA
;
1979 RtlLeaveCriticalSection( &csVirtual
);
1984 /***********************************************************************
1985 * NtReadVirtualMemory (NTDLL.@)
1986 * ZwReadVirtualMemory (NTDLL.@)
1988 NTSTATUS WINAPI
NtReadVirtualMemory( HANDLE process
, const void *addr
, void *buffer
,
1989 SIZE_T size
, SIZE_T
*bytes_read
)
1993 SERVER_START_REQ( read_process_memory
)
1995 req
->handle
= process
;
1996 req
->addr
= (void *)addr
;
1997 wine_server_set_reply( req
, buffer
, size
);
1998 if ((status
= wine_server_call( req
))) size
= 0;
2001 if (bytes_read
) *bytes_read
= size
;
2006 /***********************************************************************
2007 * NtWriteVirtualMemory (NTDLL.@)
2008 * ZwWriteVirtualMemory (NTDLL.@)
2010 NTSTATUS WINAPI
NtWriteVirtualMemory( HANDLE process
, void *addr
, const void *buffer
,
2011 SIZE_T size
, SIZE_T
*bytes_written
)
2015 SERVER_START_REQ( write_process_memory
)
2017 req
->handle
= process
;
2019 wine_server_add_data( req
, buffer
, size
);
2020 if ((status
= wine_server_call( req
))) size
= 0;
2023 if (bytes_written
) *bytes_written
= size
;