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__ */
132 static const UINT_PTR granularity_mask
= 0xffff; /* Allocation granularity (usually 64k) */
134 #define ROUND_ADDR(addr,mask) \
135 ((void *)((UINT_PTR)(addr) & ~(UINT_PTR)(mask)))
137 #define ROUND_SIZE(addr,size) \
138 (((UINT)(size) + ((UINT_PTR)(addr) & page_mask) + page_mask) & ~page_mask)
140 #define VIRTUAL_DEBUG_DUMP_VIEW(view) \
141 do { if (TRACE_ON(virtual)) VIRTUAL_DumpView(view); } while (0)
143 static void *user_space_limit
= USER_SPACE_LIMIT
;
146 /***********************************************************************
149 static const char *VIRTUAL_GetProtStr( BYTE prot
)
151 static char buffer
[6];
152 buffer
[0] = (prot
& VPROT_COMMITTED
) ? 'c' : '-';
153 buffer
[1] = (prot
& VPROT_GUARD
) ? 'g' : '-';
154 buffer
[2] = (prot
& VPROT_READ
) ? 'r' : '-';
155 buffer
[3] = (prot
& VPROT_WRITECOPY
) ? 'W' : ((prot
& VPROT_WRITE
) ? 'w' : '-');
156 buffer
[4] = (prot
& VPROT_EXEC
) ? 'x' : '-';
162 /***********************************************************************
165 static void VIRTUAL_DumpView( FILE_VIEW
*view
)
168 char *addr
= view
->base
;
169 BYTE prot
= view
->prot
[0];
171 TRACE( "View: %p - %p", addr
, addr
+ view
->size
- 1 );
172 if (view
->flags
& VFLAG_SYSTEM
)
173 TRACE( " (system)\n" );
174 else if (view
->flags
& VFLAG_VALLOC
)
175 TRACE( " (valloc)\n" );
176 else if (view
->mapping
)
177 TRACE( " %p\n", view
->mapping
);
179 TRACE( " (anonymous)\n");
181 for (count
= i
= 1; i
< view
->size
>> page_shift
; i
++, count
++)
183 if (view
->prot
[i
] == prot
) continue;
184 TRACE( " %p - %p %s\n",
185 addr
, addr
+ (count
<< page_shift
) - 1, VIRTUAL_GetProtStr(prot
) );
186 addr
+= (count
<< page_shift
);
187 prot
= view
->prot
[i
];
191 TRACE( " %p - %p %s\n",
192 addr
, addr
+ (count
<< page_shift
) - 1, VIRTUAL_GetProtStr(prot
) );
196 /***********************************************************************
199 void VIRTUAL_Dump(void)
201 struct file_view
*view
;
203 TRACE( "Dump of all virtual memory views:\n" );
204 RtlEnterCriticalSection(&csVirtual
);
205 LIST_FOR_EACH_ENTRY( view
, &views_list
, FILE_VIEW
, entry
)
207 VIRTUAL_DumpView( view
);
209 RtlLeaveCriticalSection(&csVirtual
);
213 /***********************************************************************
216 * Find the view containing a given address. The csVirtual section must be held by caller.
225 static struct file_view
*VIRTUAL_FindView( const void *addr
)
227 struct file_view
*view
;
229 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
231 if (view
->base
> addr
) break;
232 if ((const char*)view
->base
+ view
->size
> (const char*)addr
) return view
;
238 /***********************************************************************
241 * Find the first view overlapping at least part of the specified range.
242 * The csVirtual section must be held by caller.
244 static struct file_view
*find_view_range( const void *addr
, size_t size
)
246 struct file_view
*view
;
248 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
250 if ((const char *)view
->base
>= (const char *)addr
+ size
) break;
251 if ((const char *)view
->base
+ view
->size
> (const char *)addr
) return view
;
257 /***********************************************************************
260 * Add a reserved area to the list maintained by libwine.
261 * The csVirtual section must be held by caller.
263 static void add_reserved_area( void *addr
, size_t size
)
265 TRACE( "adding %p-%p\n", addr
, (char *)addr
+ size
);
267 if (addr
< user_space_limit
)
269 /* unmap the part of the area that is below the limit */
270 assert( (char *)addr
+ size
> (char *)user_space_limit
);
271 munmap( addr
, (char *)user_space_limit
- (char *)addr
);
272 size
-= (char *)user_space_limit
- (char *)addr
;
273 addr
= user_space_limit
;
275 /* blow away existing mappings */
276 wine_anon_mmap( addr
, size
, PROT_NONE
, MAP_NORESERVE
| MAP_FIXED
);
277 wine_mmap_add_reserved_area( addr
, size
);
281 /***********************************************************************
282 * remove_reserved_area
284 * Remove a reserved area from the list maintained by libwine.
285 * The csVirtual section must be held by caller.
287 static void remove_reserved_area( void *addr
, size_t size
)
289 struct file_view
*view
;
291 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
293 if ((char *)view
->base
>= (char *)addr
+ size
) break;
294 if ((char *)view
->base
+ view
->size
<= (char *)addr
) continue;
295 /* now we have an overlapping view */
296 if (view
->base
> addr
)
298 wine_mmap_remove_reserved_area( addr
, (char *)view
->base
- (char *)addr
, TRUE
);
299 size
-= (char *)view
->base
- (char *)addr
;
302 if ((char *)view
->base
+ view
->size
>= (char *)addr
+ size
)
304 /* view covers all the remaining area */
305 wine_mmap_remove_reserved_area( addr
, size
, FALSE
);
309 else /* view covers only part of the area */
311 wine_mmap_remove_reserved_area( addr
, (char *)view
->base
+ view
->size
- (char *)addr
, FALSE
);
312 size
-= (char *)view
->base
+ view
->size
- (char *)addr
;
313 addr
= (char *)view
->base
+ view
->size
;
316 /* remove remaining space */
317 if (size
) wine_mmap_remove_reserved_area( addr
, size
, TRUE
);
321 /***********************************************************************
324 * Check if an address range goes beyond a given limit.
326 static inline int is_beyond_limit( void *addr
, size_t size
, void *limit
)
328 return (limit
&& (addr
>= limit
|| (char *)addr
+ size
> (char *)limit
));
332 /***********************************************************************
335 * Unmap an area, or simply replace it by an empty mapping if it is
336 * in a reserved area. The csVirtual section must be held by caller.
338 static inline void unmap_area( void *addr
, size_t size
)
340 if (wine_mmap_is_in_reserved_area( addr
, size
))
341 wine_anon_mmap( addr
, size
, PROT_NONE
, MAP_NORESERVE
| MAP_FIXED
);
342 else if (is_beyond_limit( addr
, size
, user_space_limit
))
343 add_reserved_area( addr
, size
);
345 munmap( addr
, size
);
349 /***********************************************************************
352 * Deletes a view. The csVirtual section must be held by caller.
354 static void delete_view( struct file_view
*view
) /* [in] View */
356 if (!(view
->flags
& VFLAG_SYSTEM
)) unmap_area( view
->base
, view
->size
);
357 list_remove( &view
->entry
);
358 if (view
->mapping
) NtClose( view
->mapping
);
363 /***********************************************************************
366 * Create a view. The csVirtual section must be held by caller.
368 static NTSTATUS
create_view( struct file_view
**view_ret
, void *base
, size_t size
, BYTE vprot
)
370 struct file_view
*view
;
373 assert( !((UINT_PTR
)base
& page_mask
) );
374 assert( !(size
& page_mask
) );
376 /* Create the view structure */
378 if (!(view
= malloc( sizeof(*view
) + (size
>> page_shift
) - 1 ))) return STATUS_NO_MEMORY
;
384 view
->protect
= vprot
;
385 memset( view
->prot
, vprot
& ~VPROT_IMAGE
, size
>> page_shift
);
387 /* Insert it in the linked list */
389 LIST_FOR_EACH( ptr
, &views_list
)
391 struct file_view
*next
= LIST_ENTRY( ptr
, struct file_view
, entry
);
392 if (next
->base
> base
) break;
394 list_add_before( ptr
, &view
->entry
);
396 /* Check for overlapping views. This can happen if the previous view
397 * was a system view that got unmapped behind our back. In that case
398 * we recover by simply deleting it. */
400 if ((ptr
= list_prev( &views_list
, &view
->entry
)) != NULL
)
402 struct file_view
*prev
= LIST_ENTRY( ptr
, struct file_view
, entry
);
403 if ((char *)prev
->base
+ prev
->size
> (char *)base
)
405 TRACE( "overlapping prev view %p-%p for %p-%p\n",
406 prev
->base
, (char *)prev
->base
+ prev
->size
,
407 base
, (char *)base
+ view
->size
);
408 assert( prev
->flags
& VFLAG_SYSTEM
);
412 if ((ptr
= list_next( &views_list
, &view
->entry
)) != NULL
)
414 struct file_view
*next
= LIST_ENTRY( ptr
, struct file_view
, entry
);
415 if ((char *)base
+ view
->size
> (char *)next
->base
)
417 TRACE( "overlapping next view %p-%p for %p-%p\n",
418 next
->base
, (char *)next
->base
+ next
->size
,
419 base
, (char *)base
+ view
->size
);
420 assert( next
->flags
& VFLAG_SYSTEM
);
426 VIRTUAL_DEBUG_DUMP_VIEW( view
);
427 return STATUS_SUCCESS
;
431 /***********************************************************************
432 * VIRTUAL_GetUnixProt
434 * Convert page protections to protection for mmap/mprotect.
436 static int VIRTUAL_GetUnixProt( BYTE vprot
)
439 if ((vprot
& VPROT_COMMITTED
) && !(vprot
& VPROT_GUARD
))
441 if (vprot
& VPROT_READ
) prot
|= PROT_READ
;
442 if (vprot
& VPROT_WRITE
) prot
|= PROT_WRITE
;
443 if (vprot
& VPROT_WRITECOPY
) prot
|= PROT_WRITE
;
444 if (vprot
& VPROT_EXEC
) prot
|= PROT_EXEC
;
446 if (!prot
) prot
= PROT_NONE
;
451 /***********************************************************************
452 * VIRTUAL_GetWin32Prot
454 * Convert page protections to Win32 flags.
456 static DWORD
VIRTUAL_GetWin32Prot( BYTE vprot
)
458 DWORD ret
= VIRTUAL_Win32Flags
[vprot
& 0x0f];
459 if (vprot
& VPROT_NOCACHE
) ret
|= PAGE_NOCACHE
;
460 if (vprot
& VPROT_GUARD
) ret
|= PAGE_GUARD
;
465 /***********************************************************************
468 * Build page protections from Win32 flags.
471 * protect [I] Win32 protection flags
474 * Value of page protection flags
476 static BYTE
VIRTUAL_GetProt( DWORD protect
)
480 switch(protect
& 0xff)
486 vprot
= VPROT_READ
| VPROT_WRITE
;
489 /* MSDN CreateFileMapping() states that if PAGE_WRITECOPY is given,
490 * that the hFile must have been opened with GENERIC_READ and
491 * GENERIC_WRITE access. This is WRONG as tests show that you
492 * only need GENERIC_READ access (at least for Win9x,
493 * FIXME: what about NT?). Thus, we don't put VPROT_WRITE in
494 * PAGE_WRITECOPY and PAGE_EXECUTE_WRITECOPY.
496 vprot
= VPROT_READ
| VPROT_WRITECOPY
;
501 case PAGE_EXECUTE_READ
:
502 vprot
= VPROT_EXEC
| VPROT_READ
;
504 case PAGE_EXECUTE_READWRITE
:
505 vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITE
;
507 case PAGE_EXECUTE_WRITECOPY
:
508 /* See comment for PAGE_WRITECOPY above */
509 vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITECOPY
;
516 if (protect
& PAGE_GUARD
) vprot
|= VPROT_GUARD
;
517 if (protect
& PAGE_NOCACHE
) vprot
|= VPROT_NOCACHE
;
522 /***********************************************************************
525 * Change the protection of a range of pages.
531 static BOOL
VIRTUAL_SetProt( FILE_VIEW
*view
, /* [in] Pointer to view */
532 void *base
, /* [in] Starting address */
533 size_t size
, /* [in] Size in bytes */
534 BYTE vprot
) /* [in] Protections to use */
537 base
, (char *)base
+ size
- 1, VIRTUAL_GetProtStr( vprot
) );
539 if (mprotect( base
, size
, VIRTUAL_GetUnixProt(vprot
) ))
540 return FALSE
; /* FIXME: last error */
542 memset( view
->prot
+ (((char *)base
- (char *)view
->base
) >> page_shift
),
543 vprot
, size
>> page_shift
);
544 VIRTUAL_DEBUG_DUMP_VIEW( view
);
549 /***********************************************************************
552 * Release the extra memory while keeping the range starting on the granularity boundary.
554 static inline void *unmap_extra_space( void *ptr
, size_t total_size
, size_t wanted_size
, size_t mask
)
556 if ((ULONG_PTR
)ptr
& mask
)
558 size_t extra
= mask
+ 1 - ((ULONG_PTR
)ptr
& mask
);
559 munmap( ptr
, extra
);
560 ptr
= (char *)ptr
+ extra
;
563 if (total_size
> wanted_size
)
564 munmap( (char *)ptr
+ wanted_size
, total_size
- wanted_size
);
569 /***********************************************************************
572 * Create a view and mmap the corresponding memory area.
573 * The csVirtual section must be held by caller.
575 static NTSTATUS
map_view( struct file_view
**view_ret
, void *base
, size_t size
, BYTE vprot
)
582 if (is_beyond_limit( base
, size
, ADDRESS_SPACE_LIMIT
))
583 return STATUS_WORKING_SET_LIMIT_RANGE
;
585 switch (wine_mmap_is_in_reserved_area( base
, size
))
587 case -1: /* partially in a reserved area */
588 return STATUS_CONFLICTING_ADDRESSES
;
590 case 0: /* not in a reserved area, do a normal allocation */
591 if ((ptr
= wine_anon_mmap( base
, size
, VIRTUAL_GetUnixProt(vprot
), 0 )) == (void *)-1)
593 if (errno
== ENOMEM
) return STATUS_NO_MEMORY
;
594 return STATUS_INVALID_PARAMETER
;
598 /* We couldn't get the address we wanted */
599 if (is_beyond_limit( ptr
, size
, user_space_limit
)) add_reserved_area( ptr
, size
);
600 else munmap( ptr
, size
);
601 return STATUS_CONFLICTING_ADDRESSES
;
606 case 1: /* in a reserved area, make sure the address is available */
607 if (find_view_range( base
, size
)) return STATUS_CONFLICTING_ADDRESSES
;
608 /* replace the reserved area by our mapping */
609 if ((ptr
= wine_anon_mmap( base
, size
, VIRTUAL_GetUnixProt(vprot
), MAP_FIXED
)) != base
)
610 return STATUS_INVALID_PARAMETER
;
616 size_t view_size
= size
+ granularity_mask
+ 1;
620 if ((ptr
= wine_anon_mmap( NULL
, view_size
, VIRTUAL_GetUnixProt(vprot
), 0 )) == (void *)-1)
622 if (errno
== ENOMEM
) return STATUS_NO_MEMORY
;
623 return STATUS_INVALID_PARAMETER
;
625 /* if we got something beyond the user limit, unmap it and retry */
626 if (is_beyond_limit( ptr
, view_size
, user_space_limit
)) add_reserved_area( ptr
, view_size
);
629 ptr
= unmap_extra_space( ptr
, view_size
, size
, granularity_mask
);
632 status
= create_view( view_ret
, ptr
, size
, vprot
);
633 if (status
!= STATUS_SUCCESS
) unmap_area( ptr
, size
);
638 /***********************************************************************
641 * Linux kernels before 2.4.x can support non page-aligned offsets, as
642 * long as the offset is aligned to the filesystem block size. This is
643 * a big performance gain so we want to take advantage of it.
645 * However, when we use 64-bit file support this doesn't work because
646 * glibc rejects unaligned offsets. Also glibc 2.1.3 mmap64 is broken
647 * in that it rounds unaligned offsets down to a page boundary. For
648 * these reasons we do a direct system call here.
650 static void *unaligned_mmap( void *addr
, size_t length
, unsigned int prot
,
651 unsigned int flags
, int fd
, off_t offset
)
653 #if defined(linux) && defined(__i386__) && defined(__GNUC__)
654 if (!(offset
>> 32) && (offset
& page_mask
))
669 args
.length
= length
;
673 args
.offset
= offset
;
675 __asm__
__volatile__("push %%ebx\n\t"
680 : "0" (90), /* SYS_mmap */
683 if (ret
< 0 && ret
> -4096)
691 return mmap( addr
, length
, prot
, flags
, fd
, offset
);
695 /***********************************************************************
698 * Wrapper for mmap() to map a file into a view, falling back to read if mmap fails.
699 * The csVirtual section must be held by caller.
701 static NTSTATUS
map_file_into_view( struct file_view
*view
, int fd
, size_t start
, size_t size
,
702 off_t offset
, BYTE vprot
, BOOL removable
)
705 int prot
= VIRTUAL_GetUnixProt( vprot
);
706 BOOL shared_write
= (vprot
& VPROT_WRITE
) != 0;
708 assert( start
< view
->size
);
709 assert( start
+ size
<= view
->size
);
711 /* only try mmap if media is not removable (or if we require write access) */
712 if (!removable
|| shared_write
)
714 int flags
= MAP_FIXED
| (shared_write
? MAP_SHARED
: MAP_PRIVATE
);
716 if (unaligned_mmap( (char *)view
->base
+ start
, size
, prot
, flags
, fd
, offset
) != (void *)-1)
719 /* mmap() failed; if this is because the file offset is not */
720 /* page-aligned (EINVAL), or because the underlying filesystem */
721 /* does not support mmap() (ENOEXEC,ENODEV), we do it by hand. */
722 if ((errno
!= ENOEXEC
) && (errno
!= EINVAL
) && (errno
!= ENODEV
)) return FILE_GetNtStatus();
723 if (shared_write
) return FILE_GetNtStatus(); /* we cannot fake shared write mappings */
726 /* Reserve the memory with an anonymous mmap */
727 ptr
= wine_anon_mmap( (char *)view
->base
+ start
, size
, PROT_READ
| PROT_WRITE
, MAP_FIXED
);
728 if (ptr
== (void *)-1) return FILE_GetNtStatus();
729 /* Now read in the file */
730 pread( fd
, ptr
, size
, offset
);
731 if (prot
!= (PROT_READ
|PROT_WRITE
)) mprotect( ptr
, size
, prot
); /* Set the right protection */
733 memset( view
->prot
+ (start
>> page_shift
), vprot
, ROUND_SIZE(start
,size
) >> page_shift
);
734 return STATUS_SUCCESS
;
738 /***********************************************************************
741 * Decommit some pages of a given view.
742 * The csVirtual section must be held by caller.
744 static NTSTATUS
decommit_pages( struct file_view
*view
, size_t start
, size_t size
)
746 if (wine_anon_mmap( (char *)view
->base
+ start
, size
, PROT_NONE
, MAP_FIXED
) != (void *)-1)
748 BYTE
*p
= view
->prot
+ (start
>> page_shift
);
750 while (size
--) *p
++ &= ~VPROT_COMMITTED
;
751 return STATUS_SUCCESS
;
753 return FILE_GetNtStatus();
757 /***********************************************************************
760 * Apply the relocations to a mapped PE image
762 static int do_relocations( char *base
, const IMAGE_DATA_DIRECTORY
*dir
,
763 int delta
, SIZE_T total_size
)
765 IMAGE_BASE_RELOCATION
*rel
;
767 TRACE_(module
)( "relocating from %p-%p to %p-%p\n",
768 base
- delta
, base
- delta
+ total_size
, base
, base
+ total_size
);
770 for (rel
= (IMAGE_BASE_RELOCATION
*)(base
+ dir
->VirtualAddress
);
771 ((char *)rel
< base
+ dir
->VirtualAddress
+ dir
->Size
) && rel
->SizeOfBlock
;
772 rel
= (IMAGE_BASE_RELOCATION
*)((char*)rel
+ rel
->SizeOfBlock
) )
774 char *page
= base
+ rel
->VirtualAddress
;
775 WORD
*TypeOffset
= (WORD
*)(rel
+ 1);
776 int i
, count
= (rel
->SizeOfBlock
- sizeof(*rel
)) / sizeof(*TypeOffset
);
778 if (!count
) continue;
781 if ((char *)rel
+ rel
->SizeOfBlock
> base
+ dir
->VirtualAddress
+ dir
->Size
)
783 ERR_(module
)("invalid relocation %p,%lx,%ld at %p,%lx,%lx\n",
784 rel
, rel
->VirtualAddress
, rel
->SizeOfBlock
,
785 base
, dir
->VirtualAddress
, dir
->Size
);
789 if (page
> base
+ total_size
)
791 WARN_(module
)("skipping %d relocations for page %p beyond module %p-%p\n",
792 count
, page
, base
, base
+ total_size
);
796 TRACE_(module
)("%d relocations for page %lx\n", count
, rel
->VirtualAddress
);
798 /* patching in reverse order */
799 for (i
= 0 ; i
< count
; i
++)
801 int offset
= TypeOffset
[i
] & 0xFFF;
802 int type
= TypeOffset
[i
] >> 12;
805 case IMAGE_REL_BASED_ABSOLUTE
:
807 case IMAGE_REL_BASED_HIGH
:
808 *(short*)(page
+offset
) += HIWORD(delta
);
810 case IMAGE_REL_BASED_LOW
:
811 *(short*)(page
+offset
) += LOWORD(delta
);
813 case IMAGE_REL_BASED_HIGHLOW
:
814 *(int*)(page
+offset
) += delta
;
815 /* FIXME: if this is an exported address, fire up enhanced logic */
818 FIXME_(module
)("Unknown/unsupported fixup type %d.\n", type
);
827 /***********************************************************************
830 * Map an executable (PE format) image into memory.
832 static NTSTATUS
map_image( HANDLE hmapping
, int fd
, char *base
, SIZE_T total_size
,
833 SIZE_T header_size
, int shared_fd
, BOOL removable
, PVOID
*addr_ptr
)
835 IMAGE_DOS_HEADER
*dos
;
836 IMAGE_NT_HEADERS
*nt
;
837 IMAGE_SECTION_HEADER
*sec
;
838 IMAGE_DATA_DIRECTORY
*imports
;
839 NTSTATUS status
= STATUS_CONFLICTING_ADDRESSES
;
843 struct file_view
*view
= NULL
;
844 char *ptr
, *header_end
;
846 /* zero-map the whole range */
848 RtlEnterCriticalSection( &csVirtual
);
850 if (base
>= (char *)0x110000) /* make sure the DOS area remains free */
851 status
= map_view( &view
, base
, total_size
,
852 VPROT_COMMITTED
| VPROT_READ
| VPROT_EXEC
| VPROT_WRITECOPY
| VPROT_IMAGE
);
854 if (status
== STATUS_CONFLICTING_ADDRESSES
)
855 status
= map_view( &view
, NULL
, total_size
,
856 VPROT_COMMITTED
| VPROT_READ
| VPROT_EXEC
| VPROT_WRITECOPY
| VPROT_IMAGE
);
858 if (status
!= STATUS_SUCCESS
) goto error
;
861 TRACE_(module
)( "mapped PE file at %p-%p\n", ptr
, ptr
+ total_size
);
865 if (fstat( fd
, &st
) == -1)
867 status
= FILE_GetNtStatus();
870 status
= STATUS_INVALID_IMAGE_FORMAT
; /* generic error */
871 if (!st
.st_size
) goto error
;
872 header_size
= min( header_size
, st
.st_size
);
873 if (map_file_into_view( view
, fd
, 0, header_size
, 0, VPROT_COMMITTED
| VPROT_READ
,
874 removable
) != STATUS_SUCCESS
) goto error
;
875 dos
= (IMAGE_DOS_HEADER
*)ptr
;
876 nt
= (IMAGE_NT_HEADERS
*)(ptr
+ dos
->e_lfanew
);
877 header_end
= ptr
+ ROUND_SIZE( 0, header_size
);
878 if ((char *)(nt
+ 1) > header_end
) goto error
;
879 sec
= (IMAGE_SECTION_HEADER
*)((char*)&nt
->OptionalHeader
+nt
->FileHeader
.SizeOfOptionalHeader
);
880 if ((char *)(sec
+ nt
->FileHeader
.NumberOfSections
) > header_end
) goto error
;
882 imports
= nt
->OptionalHeader
.DataDirectory
+ IMAGE_DIRECTORY_ENTRY_IMPORT
;
883 if (!imports
->Size
|| !imports
->VirtualAddress
) imports
= NULL
;
885 /* check the architecture */
887 if (nt
->FileHeader
.Machine
!= IMAGE_FILE_MACHINE_I386
)
889 MESSAGE("Trying to load PE image for unsupported architecture (");
890 switch (nt
->FileHeader
.Machine
)
892 case IMAGE_FILE_MACHINE_UNKNOWN
: MESSAGE("Unknown"); break;
893 case IMAGE_FILE_MACHINE_I860
: MESSAGE("I860"); break;
894 case IMAGE_FILE_MACHINE_R3000
: MESSAGE("R3000"); break;
895 case IMAGE_FILE_MACHINE_R4000
: MESSAGE("R4000"); break;
896 case IMAGE_FILE_MACHINE_R10000
: MESSAGE("R10000"); break;
897 case IMAGE_FILE_MACHINE_ALPHA
: MESSAGE("Alpha"); break;
898 case IMAGE_FILE_MACHINE_POWERPC
: MESSAGE("PowerPC"); break;
899 case IMAGE_FILE_MACHINE_IA64
: MESSAGE("IA-64"); break;
900 case IMAGE_FILE_MACHINE_ALPHA64
: MESSAGE("Alpha-64"); break;
901 case IMAGE_FILE_MACHINE_AMD64
: MESSAGE("AMD-64"); break;
902 case IMAGE_FILE_MACHINE_ARM
: MESSAGE("ARM"); break;
903 default: MESSAGE("Unknown-%04x", nt
->FileHeader
.Machine
); break;
909 /* check for non page-aligned binary */
911 if (nt
->OptionalHeader
.SectionAlignment
<= page_mask
)
913 /* unaligned sections, this happens for native subsystem binaries */
914 /* in that case Windows simply maps in the whole file */
916 if (map_file_into_view( view
, fd
, 0, total_size
, 0, VPROT_COMMITTED
| VPROT_READ
,
917 removable
) != STATUS_SUCCESS
) goto error
;
919 /* check that all sections are loaded at the right offset */
920 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++)
922 if (sec
[i
].VirtualAddress
!= sec
[i
].PointerToRawData
)
923 goto error
; /* Windows refuses to load in that case too */
926 /* set the image protections */
927 VIRTUAL_SetProt( view
, ptr
, total_size
,
928 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
| VPROT_EXEC
);
930 /* perform relocations if necessary */
931 /* FIXME: not 100% compatible, Windows doesn't do this for non page-aligned binaries */
934 const IMAGE_DATA_DIRECTORY
*relocs
;
935 relocs
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
];
936 if (relocs
->VirtualAddress
&& relocs
->Size
)
937 do_relocations( ptr
, relocs
, ptr
- base
, total_size
);
944 /* map all the sections */
946 for (i
= pos
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
948 SIZE_T map_size
, file_size
, end
;
950 if (!sec
->Misc
.VirtualSize
)
952 file_size
= sec
->SizeOfRawData
;
953 map_size
= ROUND_SIZE( 0, file_size
);
957 map_size
= ROUND_SIZE( 0, sec
->Misc
.VirtualSize
);
958 file_size
= min( sec
->SizeOfRawData
, map_size
);
961 /* a few sanity checks */
962 end
= sec
->VirtualAddress
+ ROUND_SIZE( sec
->VirtualAddress
, map_size
);
963 if (sec
->VirtualAddress
> total_size
|| end
> total_size
|| end
< sec
->VirtualAddress
)
965 ERR_(module
)( "Section %.8s too large (%lx+%lx/%lx)\n",
966 sec
->Name
, sec
->VirtualAddress
, map_size
, total_size
);
970 if ((sec
->Characteristics
& IMAGE_SCN_MEM_SHARED
) &&
971 (sec
->Characteristics
& IMAGE_SCN_MEM_WRITE
))
973 TRACE_(module
)( "mapping shared section %.8s at %p off %lx (%x) size %lx (%lx) flags %lx\n",
974 sec
->Name
, ptr
+ sec
->VirtualAddress
,
975 sec
->PointerToRawData
, (int)pos
, file_size
, map_size
,
976 sec
->Characteristics
);
977 if (map_file_into_view( view
, shared_fd
, sec
->VirtualAddress
, map_size
, pos
,
978 VPROT_COMMITTED
| VPROT_READ
| PROT_WRITE
,
979 FALSE
) != STATUS_SUCCESS
)
981 ERR_(module
)( "Could not map shared section %.8s\n", sec
->Name
);
985 /* check if the import directory falls inside this section */
986 if (imports
&& imports
->VirtualAddress
>= sec
->VirtualAddress
&&
987 imports
->VirtualAddress
< sec
->VirtualAddress
+ map_size
)
989 UINT_PTR base
= imports
->VirtualAddress
& ~page_mask
;
990 UINT_PTR end
= base
+ ROUND_SIZE( imports
->VirtualAddress
, imports
->Size
);
991 if (end
> sec
->VirtualAddress
+ map_size
) end
= sec
->VirtualAddress
+ map_size
;
993 map_file_into_view( view
, shared_fd
, base
, end
- base
,
994 pos
+ (base
- sec
->VirtualAddress
),
995 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
,
1002 TRACE_(module
)( "mapping section %.8s at %p off %lx size %lx virt %lx flags %lx\n",
1003 sec
->Name
, ptr
+ sec
->VirtualAddress
,
1004 sec
->PointerToRawData
, sec
->SizeOfRawData
,
1005 sec
->Misc
.VirtualSize
, sec
->Characteristics
);
1007 if (!sec
->PointerToRawData
|| !file_size
) continue;
1009 /* Note: if the section is not aligned properly map_file_into_view will magically
1010 * fall back to read(), so we don't need to check anything here.
1012 end
= sec
->PointerToRawData
+ file_size
;
1013 if (sec
->PointerToRawData
>= st
.st_size
|| end
> st
.st_size
|| end
< sec
->PointerToRawData
||
1014 map_file_into_view( view
, fd
, sec
->VirtualAddress
, file_size
, sec
->PointerToRawData
,
1015 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
,
1016 removable
) != STATUS_SUCCESS
)
1018 ERR_(module
)( "Could not map section %.8s, file probably truncated\n", sec
->Name
);
1022 if (file_size
& page_mask
)
1024 end
= ROUND_SIZE( 0, file_size
);
1025 if (end
> map_size
) end
= map_size
;
1026 TRACE_(module
)("clearing %p - %p\n",
1027 ptr
+ sec
->VirtualAddress
+ file_size
,
1028 ptr
+ sec
->VirtualAddress
+ end
);
1029 memset( ptr
+ sec
->VirtualAddress
+ file_size
, 0, end
- file_size
);
1034 /* perform base relocation, if necessary */
1038 const IMAGE_DATA_DIRECTORY
*relocs
;
1040 relocs
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1041 if (nt
->FileHeader
.Characteristics
& IMAGE_FILE_RELOCS_STRIPPED
)
1043 if (nt
->OptionalHeader
.ImageBase
== 0x400000) {
1044 ERR("Image was mapped at %p: standard load address for a Win32 program (0x00400000) not available\n", ptr
);
1045 ERR("Do you have exec-shield or prelink active?\n");
1047 ERR( "FATAL: Need to relocate module from addr %lx, but there are no relocation records\n",
1048 nt
->OptionalHeader
.ImageBase
);
1052 /* FIXME: If we need to relocate a system DLL (base > 2GB) we should
1053 * really make sure that the *new* base address is also > 2GB.
1054 * Some DLLs really check the MSB of the module handle :-/
1056 if ((nt
->OptionalHeader
.ImageBase
& 0x80000000) && !((ULONG_PTR
)base
& 0x80000000))
1057 ERR( "Forced to relocate system DLL (base > 2GB). This is not good.\n" );
1059 if (!do_relocations( ptr
, relocs
, ptr
- base
, total_size
))
1065 /* set the image protections */
1067 sec
= (IMAGE_SECTION_HEADER
*)((char *)&nt
->OptionalHeader
+nt
->FileHeader
.SizeOfOptionalHeader
);
1068 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
1070 SIZE_T size
= ROUND_SIZE( sec
->VirtualAddress
, sec
->Misc
.VirtualSize
);
1071 BYTE vprot
= VPROT_COMMITTED
;
1072 if (sec
->Characteristics
& IMAGE_SCN_MEM_READ
) vprot
|= VPROT_READ
;
1073 if (sec
->Characteristics
& IMAGE_SCN_MEM_WRITE
) vprot
|= VPROT_READ
|VPROT_WRITECOPY
;
1074 if (sec
->Characteristics
& IMAGE_SCN_MEM_EXECUTE
) vprot
|= VPROT_EXEC
;
1076 /* Dumb game crack lets the AOEP point into a data section. Adjust. */
1077 if ((nt
->OptionalHeader
.AddressOfEntryPoint
>= sec
->VirtualAddress
) &&
1078 (nt
->OptionalHeader
.AddressOfEntryPoint
< sec
->VirtualAddress
+ size
))
1079 vprot
|= VPROT_EXEC
;
1081 VIRTUAL_SetProt( view
, ptr
+ sec
->VirtualAddress
, size
, vprot
);
1085 if (!removable
) /* don't keep handle open on removable media */
1086 NtDuplicateObject( NtCurrentProcess(), hmapping
,
1087 NtCurrentProcess(), &view
->mapping
,
1088 0, 0, DUPLICATE_SAME_ACCESS
);
1090 RtlLeaveCriticalSection( &csVirtual
);
1093 return STATUS_SUCCESS
;
1096 if (view
) delete_view( view
);
1097 RtlLeaveCriticalSection( &csVirtual
);
1102 /***********************************************************************
1103 * is_current_process
1105 * Check whether a process handle is for the current process.
1107 BOOL
is_current_process( HANDLE handle
)
1111 if (handle
== NtCurrentProcess()) return TRUE
;
1112 SERVER_START_REQ( get_process_info
)
1114 req
->handle
= handle
;
1115 if (!wine_server_call( req
))
1116 ret
= ((DWORD
)reply
->pid
== GetCurrentProcessId());
1123 /***********************************************************************
1126 static inline void virtual_init(void)
1129 page_size
= getpagesize();
1130 page_mask
= page_size
- 1;
1131 /* Make sure we have a power of 2 */
1132 assert( !(page_size
& page_mask
) );
1134 while ((1 << page_shift
) != page_size
) page_shift
++;
1135 #endif /* page_mask */
1139 /***********************************************************************
1142 * Allocate a memory view for a new TEB, properly aligned to a multiple of the size.
1144 NTSTATUS
VIRTUAL_alloc_teb( void **ret
, size_t size
, BOOL first
)
1148 struct file_view
*view
;
1150 BYTE vprot
= VPROT_READ
| VPROT_WRITE
| VPROT_COMMITTED
;
1152 if (first
) virtual_init();
1155 size
= ROUND_SIZE( 0, size
);
1156 align_size
= page_size
;
1157 while (align_size
< size
) align_size
*= 2;
1161 if ((ptr
= wine_anon_mmap( NULL
, 2 * align_size
, VIRTUAL_GetUnixProt(vprot
), 0 )) == (void *)-1)
1163 if (errno
== ENOMEM
) return STATUS_NO_MEMORY
;
1164 return STATUS_INVALID_PARAMETER
;
1166 if (!is_beyond_limit( ptr
, 2 * align_size
, user_space_limit
))
1168 ptr
= unmap_extra_space( ptr
, 2 * align_size
, align_size
, align_size
- 1 );
1171 /* if we got something beyond the user limit, unmap it and retry */
1172 add_reserved_area( ptr
, 2 * align_size
);
1175 if (!first
) RtlEnterCriticalSection( &csVirtual
);
1177 status
= create_view( &view
, ptr
, size
, vprot
);
1178 if (status
== STATUS_SUCCESS
)
1180 view
->flags
|= VFLAG_VALLOC
;
1183 else unmap_area( ptr
, size
);
1185 if (!first
) RtlLeaveCriticalSection( &csVirtual
);
1191 /***********************************************************************
1192 * VIRTUAL_HandleFault
1194 NTSTATUS
VIRTUAL_HandleFault( LPCVOID addr
)
1197 NTSTATUS ret
= STATUS_ACCESS_VIOLATION
;
1199 RtlEnterCriticalSection( &csVirtual
);
1200 if ((view
= VIRTUAL_FindView( addr
)))
1202 void *page
= ROUND_ADDR( addr
, page_mask
);
1203 BYTE vprot
= view
->prot
[((const char *)page
- (const char *)view
->base
) >> page_shift
];
1204 if (vprot
& VPROT_GUARD
)
1206 VIRTUAL_SetProt( view
, page
, page_size
, vprot
& ~VPROT_GUARD
);
1207 ret
= STATUS_GUARD_PAGE_VIOLATION
;
1210 RtlLeaveCriticalSection( &csVirtual
);
1214 /***********************************************************************
1215 * VIRTUAL_HasMapping
1217 * Check if the specified view has an associated file mapping.
1219 BOOL
VIRTUAL_HasMapping( LPCVOID addr
)
1224 RtlEnterCriticalSection( &csVirtual
);
1225 if ((view
= VIRTUAL_FindView( addr
))) ret
= (view
->mapping
!= 0);
1226 RtlLeaveCriticalSection( &csVirtual
);
1231 /***********************************************************************
1232 * VIRTUAL_UseLargeAddressSpace
1234 * Increase the address space size for apps that support it.
1236 void VIRTUAL_UseLargeAddressSpace(void)
1238 if (user_space_limit
>= ADDRESS_SPACE_LIMIT
) return;
1239 /* no large address space on win9x */
1240 if (NtCurrentTeb()->Peb
->OSPlatformId
!= VER_PLATFORM_WIN32_NT
) return;
1242 RtlEnterCriticalSection( &csVirtual
);
1243 remove_reserved_area( user_space_limit
, (char *)ADDRESS_SPACE_LIMIT
- (char *)user_space_limit
);
1244 user_space_limit
= ADDRESS_SPACE_LIMIT
;
1245 RtlLeaveCriticalSection( &csVirtual
);
1249 /***********************************************************************
1250 * NtAllocateVirtualMemory (NTDLL.@)
1251 * ZwAllocateVirtualMemory (NTDLL.@)
1253 NTSTATUS WINAPI
NtAllocateVirtualMemory( HANDLE process
, PVOID
*ret
, ULONG zero_bits
,
1254 SIZE_T
*size_ptr
, ULONG type
, ULONG protect
)
1258 SIZE_T size
= *size_ptr
;
1259 NTSTATUS status
= STATUS_SUCCESS
;
1260 struct file_view
*view
;
1262 TRACE("%p %p %08lx %lx %08lx\n", process
, *ret
, size
, type
, protect
);
1264 if (!size
) return STATUS_INVALID_PARAMETER
;
1266 if (!is_current_process( process
))
1268 ERR("Unsupported on other process\n");
1269 return STATUS_ACCESS_DENIED
;
1272 /* Round parameters to a page boundary */
1274 if (size
> 0x7fc00000) return STATUS_WORKING_SET_LIMIT_RANGE
; /* 2Gb - 4Mb */
1278 if (type
& MEM_RESERVE
) /* Round down to 64k boundary */
1279 base
= ROUND_ADDR( *ret
, granularity_mask
);
1281 base
= ROUND_ADDR( *ret
, page_mask
);
1282 size
= (((UINT_PTR
)*ret
+ size
+ page_mask
) & ~page_mask
) - (UINT_PTR
)base
;
1284 /* disallow low 64k, wrap-around and kernel space */
1285 if (((char *)base
<= (char *)granularity_mask
) ||
1286 ((char *)base
+ size
< (char *)base
) ||
1287 is_beyond_limit( base
, size
, ADDRESS_SPACE_LIMIT
))
1288 return STATUS_INVALID_PARAMETER
;
1293 size
= (size
+ page_mask
) & ~page_mask
;
1296 if (type
& MEM_TOP_DOWN
) {
1297 /* FIXME: MEM_TOP_DOWN allocates the largest possible address. */
1298 WARN("MEM_TOP_DOWN ignored\n");
1299 type
&= ~MEM_TOP_DOWN
;
1303 WARN("zero_bits %lu ignored\n", zero_bits
);
1305 /* Compute the alloc type flags */
1307 if (!(type
& MEM_SYSTEM
))
1309 if (!(type
& (MEM_COMMIT
| MEM_RESERVE
)) || (type
& ~(MEM_COMMIT
| MEM_RESERVE
)))
1311 WARN("called with wrong alloc type flags (%08lx) !\n", type
);
1312 return STATUS_INVALID_PARAMETER
;
1315 vprot
= VIRTUAL_GetProt( protect
);
1316 if (type
& MEM_COMMIT
) vprot
|= VPROT_COMMITTED
;
1318 /* Reserve the memory */
1320 RtlEnterCriticalSection( &csVirtual
);
1322 if (type
& MEM_SYSTEM
)
1324 if (type
& MEM_IMAGE
) vprot
|= VPROT_IMAGE
;
1325 status
= create_view( &view
, base
, size
, vprot
| VPROT_COMMITTED
);
1326 if (status
== STATUS_SUCCESS
)
1328 view
->flags
|= VFLAG_VALLOC
| VFLAG_SYSTEM
;
1332 else if ((type
& MEM_RESERVE
) || !base
)
1334 status
= map_view( &view
, base
, size
, vprot
);
1335 if (status
== STATUS_SUCCESS
)
1337 view
->flags
|= VFLAG_VALLOC
;
1341 else /* commit the pages */
1343 if (!(view
= VIRTUAL_FindView( base
)) ||
1344 ((char *)base
+ size
> (char *)view
->base
+ view
->size
)) status
= STATUS_NOT_MAPPED_VIEW
;
1345 else if (!VIRTUAL_SetProt( view
, base
, size
, vprot
)) status
= STATUS_ACCESS_DENIED
;
1348 RtlLeaveCriticalSection( &csVirtual
);
1350 if (status
== STATUS_SUCCESS
)
1359 /***********************************************************************
1360 * NtFreeVirtualMemory (NTDLL.@)
1361 * ZwFreeVirtualMemory (NTDLL.@)
1363 NTSTATUS WINAPI
NtFreeVirtualMemory( HANDLE process
, PVOID
*addr_ptr
, SIZE_T
*size_ptr
, ULONG type
)
1367 NTSTATUS status
= STATUS_SUCCESS
;
1368 LPVOID addr
= *addr_ptr
;
1369 SIZE_T size
= *size_ptr
;
1371 TRACE("%p %p %08lx %lx\n", process
, addr
, size
, type
);
1373 if (!is_current_process( process
))
1375 ERR("Unsupported on other process\n");
1376 return STATUS_ACCESS_DENIED
;
1379 /* Fix the parameters */
1381 size
= ROUND_SIZE( addr
, size
);
1382 base
= ROUND_ADDR( addr
, page_mask
);
1384 RtlEnterCriticalSection(&csVirtual
);
1386 if (!(view
= VIRTUAL_FindView( base
)) ||
1387 (base
+ size
> (char *)view
->base
+ view
->size
) ||
1388 !(view
->flags
& VFLAG_VALLOC
))
1390 status
= STATUS_INVALID_PARAMETER
;
1392 else if (type
& MEM_SYSTEM
)
1394 /* return the values that the caller should use to unmap the area */
1395 *addr_ptr
= view
->base
;
1396 *size_ptr
= view
->size
;
1397 view
->flags
|= VFLAG_SYSTEM
;
1398 delete_view( view
);
1400 else if (type
== MEM_RELEASE
)
1402 /* Free the pages */
1404 if (size
|| (base
!= view
->base
)) status
= STATUS_INVALID_PARAMETER
;
1407 delete_view( view
);
1412 else if (type
== MEM_DECOMMIT
)
1414 status
= decommit_pages( view
, base
- (char *)view
->base
, size
);
1415 if (status
== STATUS_SUCCESS
)
1423 WARN("called with wrong free type flags (%08lx) !\n", type
);
1424 status
= STATUS_INVALID_PARAMETER
;
1427 RtlLeaveCriticalSection(&csVirtual
);
1432 /***********************************************************************
1433 * NtProtectVirtualMemory (NTDLL.@)
1434 * ZwProtectVirtualMemory (NTDLL.@)
1436 NTSTATUS WINAPI
NtProtectVirtualMemory( HANDLE process
, PVOID
*addr_ptr
, SIZE_T
*size_ptr
,
1437 ULONG new_prot
, ULONG
*old_prot
)
1440 NTSTATUS status
= STATUS_SUCCESS
;
1445 SIZE_T size
= *size_ptr
;
1446 LPVOID addr
= *addr_ptr
;
1448 TRACE("%p %p %08lx %08lx\n", process
, addr
, size
, new_prot
);
1450 if (!is_current_process( process
))
1452 ERR("Unsupported on other process\n");
1453 return STATUS_ACCESS_DENIED
;
1456 /* Fix the parameters */
1458 size
= ROUND_SIZE( addr
, size
);
1459 base
= ROUND_ADDR( addr
, page_mask
);
1461 RtlEnterCriticalSection( &csVirtual
);
1463 if (!(view
= VIRTUAL_FindView( base
)) || (base
+ size
> (char *)view
->base
+ view
->size
))
1465 status
= STATUS_INVALID_PARAMETER
;
1469 /* Make sure all the pages are committed */
1471 p
= view
->prot
+ ((base
- (char *)view
->base
) >> page_shift
);
1472 prot
= VIRTUAL_GetWin32Prot( *p
);
1473 for (i
= size
>> page_shift
; i
; i
--, p
++)
1475 if (!(*p
& VPROT_COMMITTED
))
1477 status
= STATUS_NOT_COMMITTED
;
1483 if (old_prot
) *old_prot
= prot
;
1484 vprot
= VIRTUAL_GetProt( new_prot
) | VPROT_COMMITTED
;
1485 if (!VIRTUAL_SetProt( view
, base
, size
, vprot
)) status
= STATUS_ACCESS_DENIED
;
1488 RtlLeaveCriticalSection( &csVirtual
);
1490 if (status
== STATUS_SUCCESS
)
1498 #define UNIMPLEMENTED_INFO_CLASS(c) \
1500 FIXME("(process=%p,addr=%p) Unimplemented information class: " #c "\n", process, addr); \
1501 return STATUS_INVALID_INFO_CLASS
1503 /***********************************************************************
1504 * NtQueryVirtualMemory (NTDLL.@)
1505 * ZwQueryVirtualMemory (NTDLL.@)
1507 NTSTATUS WINAPI
NtQueryVirtualMemory( HANDLE process
, LPCVOID addr
,
1508 MEMORY_INFORMATION_CLASS info_class
, PVOID buffer
,
1509 SIZE_T len
, SIZE_T
*res_len
)
1512 char *base
, *alloc_base
= 0;
1515 MEMORY_BASIC_INFORMATION
*info
= buffer
;
1517 if (info_class
!= MemoryBasicInformation
)
1521 UNIMPLEMENTED_INFO_CLASS(MemoryWorkingSetList
);
1522 UNIMPLEMENTED_INFO_CLASS(MemorySectionName
);
1523 UNIMPLEMENTED_INFO_CLASS(MemoryBasicVlmInformation
);
1526 FIXME("(%p,%p,info_class=%d,%p,%ld,%p) Unknown information class\n",
1527 process
, addr
, info_class
, buffer
, len
, res_len
);
1528 return STATUS_INVALID_INFO_CLASS
;
1531 if (ADDRESS_SPACE_LIMIT
&& addr
>= ADDRESS_SPACE_LIMIT
)
1532 return STATUS_WORKING_SET_LIMIT_RANGE
;
1534 if (!is_current_process( process
))
1536 ERR("Unsupported on other process\n");
1537 return STATUS_ACCESS_DENIED
;
1540 base
= ROUND_ADDR( addr
, page_mask
);
1542 /* Find the view containing the address */
1544 RtlEnterCriticalSection(&csVirtual
);
1545 ptr
= list_head( &views_list
);
1550 /* make the address space end at the user limit, except if
1551 * the last view was mapped beyond that */
1552 if (alloc_base
<= (char *)user_space_limit
)
1554 if (user_space_limit
&& base
>= (char *)user_space_limit
)
1556 RtlLeaveCriticalSection( &csVirtual
);
1557 return STATUS_WORKING_SET_LIMIT_RANGE
;
1559 size
= (char *)user_space_limit
- alloc_base
;
1561 else size
= (char *)ADDRESS_SPACE_LIMIT
- alloc_base
;
1565 view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
1566 if ((char *)view
->base
> base
)
1568 size
= (char *)view
->base
- alloc_base
;
1572 if ((char *)view
->base
+ view
->size
> base
)
1574 alloc_base
= view
->base
;
1578 alloc_base
= (char *)view
->base
+ view
->size
;
1579 ptr
= list_next( &views_list
, ptr
);
1582 /* Fill the info structure */
1586 info
->State
= MEM_FREE
;
1588 info
->AllocationProtect
= 0;
1593 BYTE vprot
= view
->prot
[(base
- alloc_base
) >> page_shift
];
1594 info
->State
= (vprot
& VPROT_COMMITTED
) ? MEM_COMMIT
: MEM_RESERVE
;
1595 info
->Protect
= VIRTUAL_GetWin32Prot( vprot
);
1596 info
->AllocationProtect
= VIRTUAL_GetWin32Prot( view
->protect
);
1597 if (view
->protect
& VPROT_IMAGE
) info
->Type
= MEM_IMAGE
;
1598 else if (view
->flags
& VFLAG_VALLOC
) info
->Type
= MEM_PRIVATE
;
1599 else info
->Type
= MEM_MAPPED
;
1600 for (size
= base
- alloc_base
; size
< view
->size
; size
+= page_size
)
1601 if (view
->prot
[size
>> page_shift
] != vprot
) break;
1603 RtlLeaveCriticalSection(&csVirtual
);
1605 info
->BaseAddress
= (LPVOID
)base
;
1606 info
->AllocationBase
= (LPVOID
)alloc_base
;
1607 info
->RegionSize
= size
- (base
- alloc_base
);
1608 if (res_len
) *res_len
= sizeof(*info
);
1609 return STATUS_SUCCESS
;
1613 /***********************************************************************
1614 * NtLockVirtualMemory (NTDLL.@)
1615 * ZwLockVirtualMemory (NTDLL.@)
1617 NTSTATUS WINAPI
NtLockVirtualMemory( HANDLE process
, PVOID
*addr
, SIZE_T
*size
, ULONG unknown
)
1619 if (!is_current_process( process
))
1621 ERR("Unsupported on other process\n");
1622 return STATUS_ACCESS_DENIED
;
1624 return STATUS_SUCCESS
;
1628 /***********************************************************************
1629 * NtUnlockVirtualMemory (NTDLL.@)
1630 * ZwUnlockVirtualMemory (NTDLL.@)
1632 NTSTATUS WINAPI
NtUnlockVirtualMemory( HANDLE process
, PVOID
*addr
, SIZE_T
*size
, ULONG unknown
)
1634 if (!is_current_process( process
))
1636 ERR("Unsupported on other process\n");
1637 return STATUS_ACCESS_DENIED
;
1639 return STATUS_SUCCESS
;
1643 /***********************************************************************
1644 * NtCreateSection (NTDLL.@)
1645 * ZwCreateSection (NTDLL.@)
1647 NTSTATUS WINAPI
NtCreateSection( HANDLE
*handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
,
1648 const LARGE_INTEGER
*size
, ULONG protect
,
1649 ULONG sec_flags
, HANDLE file
)
1653 DWORD len
= (attr
&& attr
->ObjectName
) ? attr
->ObjectName
->Length
: 0;
1655 /* Check parameters */
1657 if (len
> MAX_PATH
*sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
1659 vprot
= VIRTUAL_GetProt( protect
);
1660 if (sec_flags
& SEC_RESERVE
)
1662 if (file
) return STATUS_INVALID_PARAMETER
;
1664 else vprot
|= VPROT_COMMITTED
;
1665 if (sec_flags
& SEC_NOCACHE
) vprot
|= VPROT_NOCACHE
;
1666 if (sec_flags
& SEC_IMAGE
) vprot
|= VPROT_IMAGE
;
1668 /* Create the server object */
1670 SERVER_START_REQ( create_mapping
)
1672 req
->access
= access
;
1673 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
1674 req
->rootdir
= attr
? attr
->RootDirectory
: 0;
1675 req
->file_handle
= file
;
1676 req
->size_high
= size
? size
->u
.HighPart
: 0;
1677 req
->size_low
= size
? size
->u
.LowPart
: 0;
1678 req
->protect
= vprot
;
1679 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
1680 ret
= wine_server_call( req
);
1681 *handle
= reply
->handle
;
1688 /***********************************************************************
1689 * NtOpenSection (NTDLL.@)
1690 * ZwOpenSection (NTDLL.@)
1692 NTSTATUS WINAPI
NtOpenSection( HANDLE
*handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
)
1695 DWORD len
= attr
->ObjectName
->Length
;
1697 if (len
> MAX_PATH
*sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
1699 SERVER_START_REQ( open_mapping
)
1701 req
->access
= access
;
1702 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
1703 req
->rootdir
= attr
? attr
->RootDirectory
: 0;
1704 wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
1705 if (!(ret
= wine_server_call( req
))) *handle
= reply
->handle
;
1712 /***********************************************************************
1713 * NtMapViewOfSection (NTDLL.@)
1714 * ZwMapViewOfSection (NTDLL.@)
1716 NTSTATUS WINAPI
NtMapViewOfSection( HANDLE handle
, HANDLE process
, PVOID
*addr_ptr
, ULONG zero_bits
,
1717 SIZE_T commit_size
, const LARGE_INTEGER
*offset_ptr
, SIZE_T
*size_ptr
,
1718 SECTION_INHERIT inherit
, ULONG alloc_type
, ULONG protect
)
1720 FILE_FS_DEVICE_INFORMATION device_info
;
1723 int unix_handle
= -1;
1726 struct file_view
*view
;
1727 DWORD size_low
, size_high
, header_size
, shared_size
;
1729 BOOL removable
= FALSE
;
1730 LARGE_INTEGER offset
;
1732 offset
.QuadPart
= offset_ptr
? offset_ptr
->QuadPart
: 0;
1734 TRACE("handle=%p process=%p addr=%p off=%lx%08lx size=%lx access=%lx\n",
1735 handle
, process
, *addr_ptr
, offset
.u
.HighPart
, offset
.u
.LowPart
, size
, protect
);
1737 if (!is_current_process( process
))
1739 ERR("Unsupported on other process\n");
1740 return STATUS_ACCESS_DENIED
;
1743 /* Check parameters */
1745 if ((offset
.u
.LowPart
& granularity_mask
) ||
1746 (*addr_ptr
&& ((UINT_PTR
)*addr_ptr
& granularity_mask
)))
1747 return STATUS_INVALID_PARAMETER
;
1749 SERVER_START_REQ( get_mapping_info
)
1751 req
->handle
= handle
;
1752 res
= wine_server_call( req
);
1753 prot
= reply
->protect
;
1755 size_low
= reply
->size_low
;
1756 size_high
= reply
->size_high
;
1757 header_size
= reply
->header_size
;
1758 shared_file
= reply
->shared_file
;
1759 shared_size
= reply
->shared_size
;
1762 if (res
) return res
;
1764 if ((res
= wine_server_handle_to_fd( handle
, 0, &unix_handle
, NULL
))) return res
;
1766 if (FILE_GetDeviceInfo( unix_handle
, &device_info
) == STATUS_SUCCESS
)
1767 removable
= device_info
.Characteristics
& FILE_REMOVABLE_MEDIA
;
1769 if (prot
& VPROT_IMAGE
)
1775 if ((res
= wine_server_handle_to_fd( shared_file
, FILE_READ_DATA
|FILE_WRITE_DATA
,
1776 &shared_fd
, NULL
))) goto done
;
1777 res
= map_image( handle
, unix_handle
, base
, size_low
, header_size
,
1778 shared_fd
, removable
, addr_ptr
);
1779 wine_server_release_fd( shared_file
, shared_fd
);
1780 NtClose( shared_file
);
1784 res
= map_image( handle
, unix_handle
, base
, size_low
, header_size
,
1785 -1, removable
, addr_ptr
);
1787 wine_server_release_fd( handle
, unix_handle
);
1788 if (!res
) *size_ptr
= size_low
;
1793 ERR("Sizes larger than 4Gb not supported\n");
1795 if ((offset
.u
.LowPart
>= size_low
) ||
1796 (*size_ptr
> size_low
- offset
.u
.LowPart
))
1798 res
= STATUS_INVALID_PARAMETER
;
1801 if (*size_ptr
) size
= ROUND_SIZE( offset
.u
.LowPart
, *size_ptr
);
1802 else size
= size_low
- offset
.u
.LowPart
;
1808 case PAGE_READWRITE
:
1809 case PAGE_EXECUTE_READWRITE
:
1810 if (!(prot
& VPROT_WRITE
))
1812 res
= STATUS_INVALID_PARAMETER
;
1818 case PAGE_WRITECOPY
:
1820 case PAGE_EXECUTE_READ
:
1821 case PAGE_EXECUTE_WRITECOPY
:
1822 if (prot
& VPROT_READ
) break;
1825 res
= STATUS_INVALID_PARAMETER
;
1829 /* FIXME: If a mapping is created with SEC_RESERVE and a process,
1830 * which has a view of this mapping commits some pages, they will
1831 * appear commited in all other processes, which have the same
1832 * view created. Since we don`t support this yet, we create the
1833 * whole mapping commited.
1835 prot
|= VPROT_COMMITTED
;
1837 /* Reserve a properly aligned area */
1839 RtlEnterCriticalSection( &csVirtual
);
1841 res
= map_view( &view
, *addr_ptr
, size
, prot
);
1844 RtlLeaveCriticalSection( &csVirtual
);
1850 TRACE("handle=%p size=%lx offset=%lx%08lx\n",
1851 handle
, size
, offset
.u
.HighPart
, offset
.u
.LowPart
);
1853 res
= map_file_into_view( view
, unix_handle
, 0, size
, offset
.QuadPart
, prot
, removable
);
1854 if (res
== STATUS_SUCCESS
)
1856 if (!removable
) /* don't keep handle open on removable media */
1857 NtDuplicateObject( NtCurrentProcess(), handle
,
1858 NtCurrentProcess(), &view
->mapping
,
1859 0, 0, DUPLICATE_SAME_ACCESS
);
1861 *addr_ptr
= view
->base
;
1866 ERR( "map_file_into_view %p %lx %lx%08lx failed\n",
1867 view
->base
, size
, offset
.u
.HighPart
, offset
.u
.LowPart
);
1868 delete_view( view
);
1871 RtlLeaveCriticalSection( &csVirtual
);
1874 wine_server_release_fd( handle
, unix_handle
);
1879 /***********************************************************************
1880 * NtUnmapViewOfSection (NTDLL.@)
1881 * ZwUnmapViewOfSection (NTDLL.@)
1883 NTSTATUS WINAPI
NtUnmapViewOfSection( HANDLE process
, PVOID addr
)
1886 NTSTATUS status
= STATUS_INVALID_PARAMETER
;
1887 void *base
= ROUND_ADDR( addr
, page_mask
);
1889 if (!is_current_process( process
))
1891 ERR("Unsupported on other process\n");
1892 return STATUS_ACCESS_DENIED
;
1894 RtlEnterCriticalSection( &csVirtual
);
1895 if ((view
= VIRTUAL_FindView( base
)) && (base
== view
->base
))
1897 delete_view( view
);
1898 status
= STATUS_SUCCESS
;
1900 RtlLeaveCriticalSection( &csVirtual
);
1905 /***********************************************************************
1906 * NtFlushVirtualMemory (NTDLL.@)
1907 * ZwFlushVirtualMemory (NTDLL.@)
1909 NTSTATUS WINAPI
NtFlushVirtualMemory( HANDLE process
, LPCVOID
*addr_ptr
,
1910 SIZE_T
*size_ptr
, ULONG unknown
)
1913 NTSTATUS status
= STATUS_SUCCESS
;
1914 void *addr
= ROUND_ADDR( *addr_ptr
, page_mask
);
1916 if (!is_current_process( process
))
1918 ERR("Unsupported on other process\n");
1919 return STATUS_ACCESS_DENIED
;
1921 RtlEnterCriticalSection( &csVirtual
);
1922 if (!(view
= VIRTUAL_FindView( addr
))) status
= STATUS_INVALID_PARAMETER
;
1925 if (!*size_ptr
) *size_ptr
= view
->size
;
1927 if (msync( addr
, *size_ptr
, MS_SYNC
)) status
= STATUS_NOT_MAPPED_DATA
;
1929 RtlLeaveCriticalSection( &csVirtual
);
1934 /***********************************************************************
1935 * NtReadVirtualMemory (NTDLL.@)
1936 * ZwReadVirtualMemory (NTDLL.@)
1938 NTSTATUS WINAPI
NtReadVirtualMemory( HANDLE process
, const void *addr
, void *buffer
,
1939 SIZE_T size
, SIZE_T
*bytes_read
)
1943 SERVER_START_REQ( read_process_memory
)
1945 req
->handle
= process
;
1946 req
->addr
= (void *)addr
;
1947 wine_server_set_reply( req
, buffer
, size
);
1948 if ((status
= wine_server_call( req
))) size
= 0;
1951 if (bytes_read
) *bytes_read
= size
;
1956 /***********************************************************************
1957 * NtWriteVirtualMemory (NTDLL.@)
1958 * ZwWriteVirtualMemory (NTDLL.@)
1960 NTSTATUS WINAPI
NtWriteVirtualMemory( HANDLE process
, void *addr
, const void *buffer
,
1961 SIZE_T size
, SIZE_T
*bytes_written
)
1965 SERVER_START_REQ( write_process_memory
)
1967 req
->handle
= process
;
1969 wine_server_add_data( req
, buffer
, size
);
1970 if ((status
= wine_server_call( req
))) size
= 0;
1973 if (bytes_written
) *bytes_written
= size
;