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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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_MMAN_H
42 #define NONAMELESSUNION
43 #define NONAMELESSSTRUCT
48 #include "wine/library.h"
49 #include "wine/server.h"
50 #include "wine/list.h"
51 #include "wine/debug.h"
52 #include "ntdll_misc.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(virtual);
55 WINE_DECLARE_DEBUG_CHANNEL(module
);
62 #define MAP_NORESERVE 0
66 typedef struct file_view
68 struct list entry
; /* Entry in global view list */
69 void *base
; /* Base address */
70 size_t size
; /* Size in bytes */
71 HANDLE mapping
; /* Handle to the file mapping */
72 BYTE flags
; /* Allocation flags (VFLAG_*) */
73 BYTE protect
; /* Protection for all pages at allocation time */
74 BYTE prot
[1]; /* Protection byte for each page */
78 #define VFLAG_SYSTEM 0x01 /* system view (underlying mmap not under our control) */
79 #define VFLAG_VALLOC 0x02 /* allocated by VirtualAlloc */
81 /* Conversion from VPROT_* to Win32 flags */
82 static const BYTE VIRTUAL_Win32Flags
[16] =
84 PAGE_NOACCESS
, /* 0 */
85 PAGE_READONLY
, /* READ */
86 PAGE_READWRITE
, /* WRITE */
87 PAGE_READWRITE
, /* READ | WRITE */
88 PAGE_EXECUTE
, /* EXEC */
89 PAGE_EXECUTE_READ
, /* READ | EXEC */
90 PAGE_EXECUTE_READWRITE
, /* WRITE | EXEC */
91 PAGE_EXECUTE_READWRITE
, /* READ | WRITE | EXEC */
92 PAGE_WRITECOPY
, /* WRITECOPY */
93 PAGE_WRITECOPY
, /* READ | WRITECOPY */
94 PAGE_WRITECOPY
, /* WRITE | WRITECOPY */
95 PAGE_WRITECOPY
, /* READ | WRITE | WRITECOPY */
96 PAGE_EXECUTE_WRITECOPY
, /* EXEC | WRITECOPY */
97 PAGE_EXECUTE_WRITECOPY
, /* READ | EXEC | WRITECOPY */
98 PAGE_EXECUTE_WRITECOPY
, /* WRITE | EXEC | WRITECOPY */
99 PAGE_EXECUTE_WRITECOPY
/* READ | WRITE | EXEC | WRITECOPY */
102 static struct list views_list
= LIST_INIT(views_list
);
104 static RTL_CRITICAL_SECTION csVirtual
;
105 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
108 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
109 0, 0, { (DWORD_PTR
)(__FILE__
": csVirtual") }
111 static RTL_CRITICAL_SECTION csVirtual
= { &critsect_debug
, -1, 0, 0, 0, 0 };
114 /* These are always the same on an i386, and it will be faster this way */
115 # define page_mask 0xfff
116 # define page_shift 12
117 # define page_size 0x1000
118 /* Note: these are Windows limits, you cannot change them. */
119 # define ADDRESS_SPACE_LIMIT ((void *)0xc0000000) /* top of the total available address space */
120 # define USER_SPACE_LIMIT ((void *)0x80000000) /* top of the user address space */
122 static UINT page_shift
;
123 static UINT page_size
;
124 static UINT_PTR page_mask
;
125 # define ADDRESS_SPACE_LIMIT 0 /* no limit needed on other platforms */
126 # define USER_SPACE_LIMIT 0 /* no limit needed on other platforms */
127 #endif /* __i386__ */
128 static const UINT_PTR granularity_mask
= 0xffff; /* Allocation granularity (usually 64k) */
130 #define ROUND_ADDR(addr,mask) \
131 ((void *)((UINT_PTR)(addr) & ~(UINT_PTR)(mask)))
133 #define ROUND_SIZE(addr,size) \
134 (((UINT)(size) + ((UINT_PTR)(addr) & page_mask) + page_mask) & ~page_mask)
136 #define VIRTUAL_DEBUG_DUMP_VIEW(view) \
137 if (!TRACE_ON(virtual)); else VIRTUAL_DumpView(view)
139 static void *user_space_limit
= USER_SPACE_LIMIT
;
142 /***********************************************************************
145 static const char *VIRTUAL_GetProtStr( BYTE prot
)
147 static char buffer
[6];
148 buffer
[0] = (prot
& VPROT_COMMITTED
) ? 'c' : '-';
149 buffer
[1] = (prot
& VPROT_GUARD
) ? 'g' : '-';
150 buffer
[2] = (prot
& VPROT_READ
) ? 'r' : '-';
151 buffer
[3] = (prot
& VPROT_WRITECOPY
) ? 'W' : ((prot
& VPROT_WRITE
) ? 'w' : '-');
152 buffer
[4] = (prot
& VPROT_EXEC
) ? 'x' : '-';
158 /***********************************************************************
161 static void VIRTUAL_DumpView( FILE_VIEW
*view
)
164 char *addr
= view
->base
;
165 BYTE prot
= view
->prot
[0];
167 DPRINTF( "View: %p - %p", addr
, addr
+ view
->size
- 1 );
168 if (view
->flags
& VFLAG_SYSTEM
)
169 DPRINTF( " (system)\n" );
170 else if (view
->flags
& VFLAG_VALLOC
)
171 DPRINTF( " (valloc)\n" );
172 else if (view
->mapping
)
173 DPRINTF( " %p\n", view
->mapping
);
175 DPRINTF( " (anonymous)\n");
177 for (count
= i
= 1; i
< view
->size
>> page_shift
; i
++, count
++)
179 if (view
->prot
[i
] == prot
) continue;
180 DPRINTF( " %p - %p %s\n",
181 addr
, addr
+ (count
<< page_shift
) - 1, VIRTUAL_GetProtStr(prot
) );
182 addr
+= (count
<< page_shift
);
183 prot
= view
->prot
[i
];
187 DPRINTF( " %p - %p %s\n",
188 addr
, addr
+ (count
<< page_shift
) - 1, VIRTUAL_GetProtStr(prot
) );
192 /***********************************************************************
195 void VIRTUAL_Dump(void)
197 struct file_view
*view
;
199 DPRINTF( "\nDump of all virtual memory views:\n\n" );
200 RtlEnterCriticalSection(&csVirtual
);
201 LIST_FOR_EACH_ENTRY( view
, &views_list
, FILE_VIEW
, entry
)
203 VIRTUAL_DumpView( view
);
205 RtlLeaveCriticalSection(&csVirtual
);
209 /***********************************************************************
212 * Find the view containing a given address. The csVirtual section must be held by caller.
221 static struct file_view
*VIRTUAL_FindView( const void *addr
)
223 struct file_view
*view
;
225 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
227 if (view
->base
> addr
) break;
228 if ((const char*)view
->base
+ view
->size
> (const char*)addr
) return view
;
234 /***********************************************************************
237 * Find the first view overlapping at least part of the specified range.
238 * The csVirtual section must be held by caller.
240 static struct file_view
*find_view_range( const void *addr
, size_t size
)
242 struct file_view
*view
;
244 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
246 if ((const char *)view
->base
>= (const char *)addr
+ size
) break;
247 if ((const char *)view
->base
+ view
->size
> (const char *)addr
) return view
;
253 /***********************************************************************
256 * Add a reserved area to the list maintained by libwine.
257 * The csVirtual section must be held by caller.
259 static void add_reserved_area( void *addr
, size_t size
)
261 TRACE( "adding %p-%p\n", addr
, (char *)addr
+ size
);
263 if (addr
< user_space_limit
)
265 /* unmap the part of the area that is below the limit */
266 assert( (char *)addr
+ size
> (char *)user_space_limit
);
267 munmap( addr
, (char *)user_space_limit
- (char *)addr
);
268 size
-= (char *)user_space_limit
- (char *)addr
;
269 addr
= user_space_limit
;
271 /* blow away existing mappings */
272 wine_anon_mmap( addr
, size
, PROT_NONE
, MAP_NORESERVE
| MAP_FIXED
);
273 wine_mmap_add_reserved_area( addr
, size
);
277 /***********************************************************************
278 * remove_reserved_area
280 * Remove a reserved area from the list maintained by libwine.
281 * The csVirtual section must be held by caller.
283 static void remove_reserved_area( void *addr
, size_t size
)
285 struct file_view
*view
;
287 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
289 if ((char *)view
->base
>= (char *)addr
+ size
) break;
290 if ((char *)view
->base
+ view
->size
<= (char *)addr
) continue;
291 /* now we have an overlapping view */
292 if (view
->base
> addr
)
294 wine_mmap_remove_reserved_area( addr
, (char *)view
->base
- (char *)addr
, TRUE
);
295 size
-= (char *)view
->base
- (char *)addr
;
298 if ((char *)view
->base
+ view
->size
>= (char *)addr
+ size
)
300 /* view covers all the remaining area */
301 wine_mmap_remove_reserved_area( addr
, size
, FALSE
);
305 else /* view covers only part of the area */
307 wine_mmap_remove_reserved_area( addr
, (char *)view
->base
+ view
->size
- (char *)addr
, FALSE
);
308 size
-= (char *)view
->base
+ view
->size
- (char *)addr
;
309 addr
= (char *)view
->base
+ view
->size
;
312 /* remove remaining space */
313 if (size
) wine_mmap_remove_reserved_area( addr
, size
, TRUE
);
317 /***********************************************************************
320 * Check if an address range goes beyond a given limit.
322 static inline int is_beyond_limit( void *addr
, size_t size
, void *limit
)
324 return (limit
&& (addr
>= limit
|| (char *)addr
+ size
> (char *)limit
));
328 /***********************************************************************
331 * Unmap an area, or simply replace it by an empty mapping if it is
332 * in a reserved area. The csVirtual section must be held by caller.
334 static inline void unmap_area( void *addr
, size_t size
)
336 if (wine_mmap_is_in_reserved_area( addr
, size
))
337 wine_anon_mmap( addr
, size
, PROT_NONE
, MAP_NORESERVE
| MAP_FIXED
);
339 munmap( addr
, size
);
343 /***********************************************************************
346 * Deletes a view. The csVirtual section must be held by caller.
348 static void delete_view( struct file_view
*view
) /* [in] View */
350 if (!(view
->flags
& VFLAG_SYSTEM
)) unmap_area( view
->base
, view
->size
);
351 list_remove( &view
->entry
);
352 if (view
->mapping
) NtClose( view
->mapping
);
357 /***********************************************************************
360 * Create a view. The csVirtual section must be held by caller.
362 static NTSTATUS
create_view( struct file_view
**view_ret
, void *base
, size_t size
, BYTE vprot
)
364 struct file_view
*view
;
367 assert( !((UINT_PTR
)base
& page_mask
) );
368 assert( !(size
& page_mask
) );
370 /* Create the view structure */
372 if (!(view
= malloc( sizeof(*view
) + (size
>> page_shift
) - 1 ))) return STATUS_NO_MEMORY
;
378 view
->protect
= vprot
;
379 memset( view
->prot
, vprot
, size
>> page_shift
);
381 /* Insert it in the linked list */
383 LIST_FOR_EACH( ptr
, &views_list
)
385 struct file_view
*next
= LIST_ENTRY( ptr
, struct file_view
, entry
);
386 if (next
->base
> base
) break;
388 list_add_before( ptr
, &view
->entry
);
390 /* Check for overlapping views. This can happen if the previous view
391 * was a system view that got unmapped behind our back. In that case
392 * we recover by simply deleting it. */
394 if ((ptr
= list_prev( &views_list
, &view
->entry
)) != NULL
)
396 struct file_view
*prev
= LIST_ENTRY( ptr
, struct file_view
, entry
);
397 if ((char *)prev
->base
+ prev
->size
> (char *)base
)
399 TRACE( "overlapping prev view %p-%p for %p-%p\n",
400 prev
->base
, (char *)prev
->base
+ prev
->size
,
401 base
, (char *)base
+ view
->size
);
402 assert( prev
->flags
& VFLAG_SYSTEM
);
406 if ((ptr
= list_next( &views_list
, &view
->entry
)) != NULL
)
408 struct file_view
*next
= LIST_ENTRY( ptr
, struct file_view
, entry
);
409 if ((char *)base
+ view
->size
> (char *)next
->base
)
411 TRACE( "overlapping next view %p-%p for %p-%p\n",
412 next
->base
, (char *)next
->base
+ next
->size
,
413 base
, (char *)base
+ view
->size
);
414 assert( next
->flags
& VFLAG_SYSTEM
);
420 VIRTUAL_DEBUG_DUMP_VIEW( view
);
421 return STATUS_SUCCESS
;
425 /***********************************************************************
426 * VIRTUAL_GetUnixProt
428 * Convert page protections to protection for mmap/mprotect.
430 static int VIRTUAL_GetUnixProt( BYTE vprot
)
433 if ((vprot
& VPROT_COMMITTED
) && !(vprot
& VPROT_GUARD
))
435 if (vprot
& VPROT_READ
) prot
|= PROT_READ
;
436 if (vprot
& VPROT_WRITE
) prot
|= PROT_WRITE
;
437 if (vprot
& VPROT_WRITECOPY
) prot
|= PROT_WRITE
;
438 if (vprot
& VPROT_EXEC
) prot
|= PROT_EXEC
;
444 /***********************************************************************
445 * VIRTUAL_GetWin32Prot
447 * Convert page protections to Win32 flags.
452 static void VIRTUAL_GetWin32Prot(
453 BYTE vprot
, /* [in] Page protection flags */
454 DWORD
*protect
, /* [out] Location to store Win32 protection flags */
455 DWORD
*state
) /* [out] Location to store mem state flag */
458 *protect
= VIRTUAL_Win32Flags
[vprot
& 0x0f];
459 if (vprot
& VPROT_NOCACHE
) *protect
|= PAGE_NOCACHE
;
460 if (vprot
& VPROT_GUARD
) *protect
= PAGE_NOACCESS
| PAGE_GUARD
;
463 if (state
) *state
= (vprot
& VPROT_COMMITTED
) ? MEM_COMMIT
: MEM_RESERVE
;
467 /***********************************************************************
470 * Build page protections from Win32 flags.
473 * protect [I] Win32 protection flags
476 * Value of page protection flags
478 static BYTE
VIRTUAL_GetProt( DWORD protect
)
482 switch(protect
& 0xff)
488 vprot
= VPROT_READ
| VPROT_WRITE
;
491 /* MSDN CreateFileMapping() states that if PAGE_WRITECOPY is given,
492 * that the hFile must have been opened with GENERIC_READ and
493 * GENERIC_WRITE access. This is WRONG as tests show that you
494 * only need GENERIC_READ access (at least for Win9x,
495 * FIXME: what about NT?). Thus, we don't put VPROT_WRITE in
496 * PAGE_WRITECOPY and PAGE_EXECUTE_WRITECOPY.
498 vprot
= VPROT_READ
| VPROT_WRITECOPY
;
503 case PAGE_EXECUTE_READ
:
504 vprot
= VPROT_EXEC
| VPROT_READ
;
506 case PAGE_EXECUTE_READWRITE
:
507 vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITE
;
509 case PAGE_EXECUTE_WRITECOPY
:
510 /* See comment for PAGE_WRITECOPY above */
511 vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITECOPY
;
518 if (protect
& PAGE_GUARD
) vprot
|= VPROT_GUARD
;
519 if (protect
& PAGE_NOCACHE
) vprot
|= VPROT_NOCACHE
;
524 /***********************************************************************
527 * Change the protection of a range of pages.
533 static BOOL
VIRTUAL_SetProt( FILE_VIEW
*view
, /* [in] Pointer to view */
534 void *base
, /* [in] Starting address */
535 size_t size
, /* [in] Size in bytes */
536 BYTE vprot
) /* [in] Protections to use */
539 base
, (char *)base
+ size
- 1, VIRTUAL_GetProtStr( vprot
) );
541 if (mprotect( base
, size
, VIRTUAL_GetUnixProt(vprot
) ))
542 return FALSE
; /* FIXME: last error */
544 memset( view
->prot
+ (((char *)base
- (char *)view
->base
) >> page_shift
),
545 vprot
, size
>> page_shift
);
546 VIRTUAL_DEBUG_DUMP_VIEW( view
);
551 /***********************************************************************
554 * Release the extra memory while keeping the range starting on the granularity boundary.
556 static inline void *unmap_extra_space( void *ptr
, size_t total_size
, size_t wanted_size
, size_t mask
)
558 if ((ULONG_PTR
)ptr
& mask
)
560 size_t extra
= mask
+ 1 - ((ULONG_PTR
)ptr
& mask
);
561 munmap( ptr
, extra
);
562 ptr
= (char *)ptr
+ extra
;
565 if (total_size
> wanted_size
)
566 munmap( (char *)ptr
+ wanted_size
, total_size
- wanted_size
);
571 /***********************************************************************
574 * Create a view and mmap the corresponding memory area.
575 * The csVirtual section must be held by caller.
577 static NTSTATUS
map_view( struct file_view
**view_ret
, void *base
, size_t size
, BYTE vprot
)
584 if (is_beyond_limit( base
, size
, ADDRESS_SPACE_LIMIT
))
585 return STATUS_WORKING_SET_LIMIT_RANGE
;
587 switch (wine_mmap_is_in_reserved_area( base
, size
))
589 case -1: /* partially in a reserved area */
590 return STATUS_CONFLICTING_ADDRESSES
;
592 case 0: /* not in a reserved area, do a normal allocation */
593 if ((ptr
= wine_anon_mmap( base
, size
, VIRTUAL_GetUnixProt(vprot
), 0 )) == (void *)-1)
595 if (errno
== ENOMEM
) return STATUS_NO_MEMORY
;
596 return STATUS_INVALID_PARAMETER
;
600 /* We couldn't get the address we wanted */
601 if (is_beyond_limit( ptr
, size
, user_space_limit
)) add_reserved_area( ptr
, size
);
602 else munmap( ptr
, size
);
603 return STATUS_CONFLICTING_ADDRESSES
;
608 case 1: /* in a reserved area, make sure the address is available */
609 if (find_view_range( base
, size
)) return STATUS_CONFLICTING_ADDRESSES
;
610 /* replace the reserved area by our mapping */
611 if ((ptr
= wine_anon_mmap( base
, size
, VIRTUAL_GetUnixProt(vprot
), MAP_FIXED
)) != base
)
612 return STATUS_INVALID_PARAMETER
;
618 size_t view_size
= size
+ granularity_mask
+ 1;
622 if ((ptr
= wine_anon_mmap( NULL
, view_size
, VIRTUAL_GetUnixProt(vprot
), 0 )) == (void *)-1)
624 if (errno
== ENOMEM
) return STATUS_NO_MEMORY
;
625 return STATUS_INVALID_PARAMETER
;
627 /* if we got something beyond the user limit, unmap it and retry */
628 if (is_beyond_limit( ptr
, view_size
, user_space_limit
)) add_reserved_area( ptr
, view_size
);
631 ptr
= unmap_extra_space( ptr
, view_size
, size
, granularity_mask
);
634 status
= create_view( view_ret
, ptr
, size
, vprot
);
635 if (status
!= STATUS_SUCCESS
) unmap_area( ptr
, size
);
640 /***********************************************************************
643 * Linux kernels before 2.4.x can support non page-aligned offsets, as
644 * long as the offset is aligned to the filesystem block size. This is
645 * a big performance gain so we want to take advantage of it.
647 * However, when we use 64-bit file support this doesn't work because
648 * glibc rejects unaligned offsets. Also glibc 2.1.3 mmap64 is broken
649 * in that it rounds unaligned offsets down to a page boundary. For
650 * these reasons we do a direct system call here.
652 static void *unaligned_mmap( void *addr
, size_t length
, unsigned int prot
,
653 unsigned int flags
, int fd
, off_t offset
)
655 #if defined(linux) && defined(__i386__) && defined(__GNUC__)
656 if (!(offset
>> 32) && (offset
& page_mask
))
671 args
.length
= length
;
675 args
.offset
= offset
;
677 __asm__
__volatile__("push %%ebx\n\t"
682 : "0" (90), /* SYS_mmap */
685 if (ret
< 0 && ret
> -4096)
693 return mmap( addr
, length
, prot
, flags
, fd
, offset
);
697 /***********************************************************************
700 * Wrapper for mmap() to map a file into a view, falling back to read if mmap fails.
701 * The csVirtual section must be held by caller.
703 static NTSTATUS
map_file_into_view( struct file_view
*view
, int fd
, size_t start
, size_t size
,
704 off_t offset
, BYTE vprot
, BOOL removable
)
707 int prot
= VIRTUAL_GetUnixProt( vprot
);
708 BOOL shared_write
= (vprot
& VPROT_WRITE
) != 0;
710 assert( start
< view
->size
);
711 assert( start
+ size
<= view
->size
);
713 /* only try mmap if media is not removable (or if we require write access) */
714 if (!removable
|| shared_write
)
716 int flags
= MAP_FIXED
| (shared_write
? MAP_SHARED
: MAP_PRIVATE
);
718 if (unaligned_mmap( (char *)view
->base
+ start
, size
, prot
, flags
, fd
, offset
) != (void *)-1)
721 /* mmap() failed; if this is because the file offset is not */
722 /* page-aligned (EINVAL), or because the underlying filesystem */
723 /* does not support mmap() (ENOEXEC,ENODEV), we do it by hand. */
724 if ((errno
!= ENOEXEC
) && (errno
!= EINVAL
) && (errno
!= ENODEV
)) return FILE_GetNtStatus();
725 if (shared_write
) return FILE_GetNtStatus(); /* we cannot fake shared write mappings */
728 /* Reserve the memory with an anonymous mmap */
729 ptr
= wine_anon_mmap( (char *)view
->base
+ start
, size
, PROT_READ
| PROT_WRITE
, MAP_FIXED
);
730 if (ptr
== (void *)-1) return FILE_GetNtStatus();
731 /* Now read in the file */
732 pread( fd
, ptr
, size
, offset
);
733 if (prot
!= (PROT_READ
|PROT_WRITE
)) mprotect( ptr
, size
, prot
); /* Set the right protection */
735 memset( view
->prot
+ (start
>> page_shift
), vprot
, size
>> page_shift
);
736 return STATUS_SUCCESS
;
740 /***********************************************************************
743 * Decommit some pages of a given view.
744 * The csVirtual section must be held by caller.
746 static NTSTATUS
decommit_pages( struct file_view
*view
, size_t start
, size_t size
)
748 if (wine_anon_mmap( (char *)view
->base
+ start
, size
, PROT_NONE
, MAP_FIXED
) != (void *)-1)
750 BYTE
*p
= view
->prot
+ (start
>> page_shift
);
752 while (size
--) *p
++ &= ~VPROT_COMMITTED
;
753 return STATUS_SUCCESS
;
755 return FILE_GetNtStatus();
759 /***********************************************************************
762 * Apply the relocations to a mapped PE image
764 static int do_relocations( char *base
, const IMAGE_DATA_DIRECTORY
*dir
,
765 int delta
, SIZE_T total_size
)
767 IMAGE_BASE_RELOCATION
*rel
;
769 TRACE_(module
)( "relocating from %p-%p to %p-%p\n",
770 base
- delta
, base
- delta
+ total_size
, base
, base
+ total_size
);
772 for (rel
= (IMAGE_BASE_RELOCATION
*)(base
+ dir
->VirtualAddress
);
773 ((char *)rel
< base
+ dir
->VirtualAddress
+ dir
->Size
) && rel
->SizeOfBlock
;
774 rel
= (IMAGE_BASE_RELOCATION
*)((char*)rel
+ rel
->SizeOfBlock
) )
776 char *page
= base
+ rel
->VirtualAddress
;
777 WORD
*TypeOffset
= (WORD
*)(rel
+ 1);
778 int i
, count
= (rel
->SizeOfBlock
- sizeof(*rel
)) / sizeof(*TypeOffset
);
780 if (!count
) continue;
783 if ((char *)rel
+ rel
->SizeOfBlock
> base
+ dir
->VirtualAddress
+ dir
->Size
||
784 page
> base
+ total_size
)
786 ERR_(module
)("invalid relocation %p,%lx,%ld at %p,%lx,%lx\n",
787 rel
, rel
->VirtualAddress
, rel
->SizeOfBlock
,
788 base
, dir
->VirtualAddress
, dir
->Size
);
792 TRACE_(module
)("%ld relocations for page %lx\n", rel
->SizeOfBlock
, rel
->VirtualAddress
);
794 /* patching in reverse order */
795 for (i
= 0 ; i
< count
; i
++)
797 int offset
= TypeOffset
[i
] & 0xFFF;
798 int type
= TypeOffset
[i
] >> 12;
801 case IMAGE_REL_BASED_ABSOLUTE
:
803 case IMAGE_REL_BASED_HIGH
:
804 *(short*)(page
+offset
) += HIWORD(delta
);
806 case IMAGE_REL_BASED_LOW
:
807 *(short*)(page
+offset
) += LOWORD(delta
);
809 case IMAGE_REL_BASED_HIGHLOW
:
810 *(int*)(page
+offset
) += delta
;
811 /* FIXME: if this is an exported address, fire up enhanced logic */
814 FIXME_(module
)("Unknown/unsupported fixup type %d.\n", type
);
823 /***********************************************************************
826 * Map an executable (PE format) image into memory.
828 static NTSTATUS
map_image( HANDLE hmapping
, int fd
, char *base
, SIZE_T total_size
,
829 SIZE_T header_size
, int shared_fd
, BOOL removable
, PVOID
*addr_ptr
)
831 IMAGE_DOS_HEADER
*dos
;
832 IMAGE_NT_HEADERS
*nt
;
833 IMAGE_SECTION_HEADER
*sec
;
834 IMAGE_DATA_DIRECTORY
*imports
;
835 NTSTATUS status
= STATUS_CONFLICTING_ADDRESSES
;
838 struct file_view
*view
= NULL
;
841 /* zero-map the whole range */
843 RtlEnterCriticalSection( &csVirtual
);
845 if (base
>= (char *)0x110000) /* make sure the DOS area remains free */
846 status
= map_view( &view
, base
, total_size
,
847 VPROT_COMMITTED
| VPROT_READ
| VPROT_EXEC
| VPROT_WRITECOPY
| VPROT_IMAGE
);
849 if (status
== STATUS_CONFLICTING_ADDRESSES
)
850 status
= map_view( &view
, NULL
, total_size
,
851 VPROT_COMMITTED
| VPROT_READ
| VPROT_EXEC
| VPROT_WRITECOPY
| VPROT_IMAGE
);
853 if (status
!= STATUS_SUCCESS
) goto error
;
856 TRACE_(module
)( "mapped PE file at %p-%p\n", ptr
, ptr
+ total_size
);
860 status
= STATUS_INVALID_IMAGE_FORMAT
; /* generic error */
861 if (map_file_into_view( view
, fd
, 0, header_size
, 0, VPROT_COMMITTED
| VPROT_READ
,
862 removable
) != STATUS_SUCCESS
) goto error
;
863 dos
= (IMAGE_DOS_HEADER
*)ptr
;
864 nt
= (IMAGE_NT_HEADERS
*)(ptr
+ dos
->e_lfanew
);
865 if ((char *)(nt
+ 1) > ptr
+ header_size
) goto error
;
867 sec
= (IMAGE_SECTION_HEADER
*)((char*)&nt
->OptionalHeader
+nt
->FileHeader
.SizeOfOptionalHeader
);
868 if ((char *)(sec
+ nt
->FileHeader
.NumberOfSections
) > ptr
+ header_size
) goto error
;
870 imports
= nt
->OptionalHeader
.DataDirectory
+ IMAGE_DIRECTORY_ENTRY_IMPORT
;
871 if (!imports
->Size
|| !imports
->VirtualAddress
) imports
= NULL
;
873 /* check the architecture */
875 if (nt
->FileHeader
.Machine
!= IMAGE_FILE_MACHINE_I386
)
877 MESSAGE("Trying to load PE image for unsupported architecture (");
878 switch (nt
->FileHeader
.Machine
)
880 case IMAGE_FILE_MACHINE_UNKNOWN
: MESSAGE("Unknown"); break;
881 case IMAGE_FILE_MACHINE_I860
: MESSAGE("I860"); break;
882 case IMAGE_FILE_MACHINE_R3000
: MESSAGE("R3000"); break;
883 case IMAGE_FILE_MACHINE_R4000
: MESSAGE("R4000"); break;
884 case IMAGE_FILE_MACHINE_R10000
: MESSAGE("R10000"); break;
885 case IMAGE_FILE_MACHINE_ALPHA
: MESSAGE("Alpha"); break;
886 case IMAGE_FILE_MACHINE_POWERPC
: MESSAGE("PowerPC"); break;
887 case IMAGE_FILE_MACHINE_IA64
: MESSAGE("IA-64"); break;
888 case IMAGE_FILE_MACHINE_ALPHA64
: MESSAGE("Alpha-64"); break;
889 case IMAGE_FILE_MACHINE_AMD64
: MESSAGE("AMD-64"); break;
890 default: MESSAGE("Unknown-%04x", nt
->FileHeader
.Machine
); break;
896 /* check for non page-aligned binary */
898 if (nt
->OptionalHeader
.SectionAlignment
<= page_mask
)
900 /* unaligned sections, this happens for native subsystem binaries */
901 /* in that case Windows simply maps in the whole file */
903 if (map_file_into_view( view
, fd
, 0, total_size
, 0, VPROT_COMMITTED
| VPROT_READ
,
904 removable
) != STATUS_SUCCESS
) goto error
;
906 /* check that all sections are loaded at the right offset */
907 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++)
909 if (sec
[i
].VirtualAddress
!= sec
[i
].PointerToRawData
)
910 goto error
; /* Windows refuses to load in that case too */
913 /* set the image protections */
914 VIRTUAL_SetProt( view
, ptr
, total_size
,
915 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
| VPROT_EXEC
);
917 /* perform relocations if necessary */
918 /* FIXME: not 100% compatible, Windows doesn't do this for non page-aligned binaries */
921 const IMAGE_DATA_DIRECTORY
*relocs
;
922 relocs
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
];
923 if (relocs
->VirtualAddress
&& relocs
->Size
)
924 do_relocations( ptr
, relocs
, ptr
- base
, total_size
);
931 /* map all the sections */
933 for (i
= pos
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
935 SIZE_T map_size
, file_size
, end
;
937 if (!sec
->Misc
.VirtualSize
)
939 file_size
= sec
->SizeOfRawData
;
940 map_size
= ROUND_SIZE( 0, file_size
);
944 map_size
= ROUND_SIZE( 0, sec
->Misc
.VirtualSize
);
945 file_size
= min( sec
->SizeOfRawData
, map_size
);
948 /* a few sanity checks */
949 end
= sec
->VirtualAddress
+ ROUND_SIZE( sec
->VirtualAddress
, map_size
);
950 if (sec
->VirtualAddress
> total_size
|| end
> total_size
|| end
< sec
->VirtualAddress
)
952 ERR_(module
)( "Section %.8s too large (%lx+%lx/%lx)\n",
953 sec
->Name
, sec
->VirtualAddress
, map_size
, total_size
);
957 if ((sec
->Characteristics
& IMAGE_SCN_MEM_SHARED
) &&
958 (sec
->Characteristics
& IMAGE_SCN_MEM_WRITE
))
960 TRACE_(module
)( "mapping shared section %.8s at %p off %lx (%x) size %lx (%lx) flags %lx\n",
961 sec
->Name
, ptr
+ sec
->VirtualAddress
,
962 sec
->PointerToRawData
, (int)pos
, file_size
, map_size
,
963 sec
->Characteristics
);
964 if (map_file_into_view( view
, shared_fd
, sec
->VirtualAddress
, map_size
, pos
,
965 VPROT_COMMITTED
| VPROT_READ
| PROT_WRITE
,
966 FALSE
) != STATUS_SUCCESS
)
968 ERR_(module
)( "Could not map shared section %.8s\n", sec
->Name
);
972 /* check if the import directory falls inside this section */
973 if (imports
&& imports
->VirtualAddress
>= sec
->VirtualAddress
&&
974 imports
->VirtualAddress
< sec
->VirtualAddress
+ map_size
)
976 UINT_PTR base
= imports
->VirtualAddress
& ~page_mask
;
977 UINT_PTR end
= base
+ ROUND_SIZE( imports
->VirtualAddress
, imports
->Size
);
978 if (end
> sec
->VirtualAddress
+ map_size
) end
= sec
->VirtualAddress
+ map_size
;
980 map_file_into_view( view
, shared_fd
, base
, end
- base
,
981 pos
+ (base
- sec
->VirtualAddress
),
982 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
,
989 TRACE_(module
)( "mapping section %.8s at %p off %lx size %lx virt %lx flags %lx\n",
990 sec
->Name
, ptr
+ sec
->VirtualAddress
,
991 sec
->PointerToRawData
, sec
->SizeOfRawData
,
992 sec
->Misc
.VirtualSize
, sec
->Characteristics
);
994 if (!sec
->PointerToRawData
|| !file_size
) continue;
996 /* Note: if the section is not aligned properly map_file_into_view will magically
997 * fall back to read(), so we don't need to check anything here.
999 if (map_file_into_view( view
, fd
, sec
->VirtualAddress
, file_size
, sec
->PointerToRawData
,
1000 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
,
1001 removable
) != STATUS_SUCCESS
)
1003 ERR_(module
)( "Could not map section %.8s, file probably truncated\n", sec
->Name
);
1007 if (file_size
& page_mask
)
1009 end
= ROUND_SIZE( 0, file_size
);
1010 if (end
> map_size
) end
= map_size
;
1011 TRACE_(module
)("clearing %p - %p\n",
1012 ptr
+ sec
->VirtualAddress
+ file_size
,
1013 ptr
+ sec
->VirtualAddress
+ end
);
1014 memset( ptr
+ sec
->VirtualAddress
+ file_size
, 0, end
- file_size
);
1019 /* perform base relocation, if necessary */
1023 const IMAGE_DATA_DIRECTORY
*relocs
;
1025 relocs
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1026 if (!relocs
->VirtualAddress
|| !relocs
->Size
)
1028 if (nt
->OptionalHeader
.ImageBase
== 0x400000) {
1029 ERR("Image was mapped at %p: standard load address for a Win32 program (0x00400000) not available\n", ptr
);
1030 ERR("Do you have exec-shield or prelink active?\n");
1032 ERR( "FATAL: Need to relocate module from addr %lx, but there are no relocation records\n",
1033 nt
->OptionalHeader
.ImageBase
);
1037 /* FIXME: If we need to relocate a system DLL (base > 2GB) we should
1038 * really make sure that the *new* base address is also > 2GB.
1039 * Some DLLs really check the MSB of the module handle :-/
1041 if ((nt
->OptionalHeader
.ImageBase
& 0x80000000) && !((ULONG_PTR
)base
& 0x80000000))
1042 ERR( "Forced to relocate system DLL (base > 2GB). This is not good.\n" );
1044 if (!do_relocations( ptr
, relocs
, ptr
- base
, total_size
))
1050 /* set the image protections */
1052 sec
= (IMAGE_SECTION_HEADER
*)((char *)&nt
->OptionalHeader
+nt
->FileHeader
.SizeOfOptionalHeader
);
1053 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
1055 SIZE_T size
= ROUND_SIZE( sec
->VirtualAddress
, sec
->Misc
.VirtualSize
);
1056 BYTE vprot
= VPROT_COMMITTED
;
1057 if (sec
->Characteristics
& IMAGE_SCN_MEM_READ
) vprot
|= VPROT_READ
;
1058 if (sec
->Characteristics
& IMAGE_SCN_MEM_WRITE
) vprot
|= VPROT_READ
|VPROT_WRITECOPY
;
1059 if (sec
->Characteristics
& IMAGE_SCN_MEM_EXECUTE
) vprot
|= VPROT_EXEC
;
1060 VIRTUAL_SetProt( view
, ptr
+ sec
->VirtualAddress
, size
, vprot
);
1064 if (!removable
) /* don't keep handle open on removable media */
1065 NtDuplicateObject( NtCurrentProcess(), hmapping
,
1066 NtCurrentProcess(), &view
->mapping
,
1067 0, 0, DUPLICATE_SAME_ACCESS
);
1069 RtlLeaveCriticalSection( &csVirtual
);
1072 return STATUS_SUCCESS
;
1075 if (view
) delete_view( view
);
1076 RtlLeaveCriticalSection( &csVirtual
);
1081 /***********************************************************************
1082 * is_current_process
1084 * Check whether a process handle is for the current process.
1086 BOOL
is_current_process( HANDLE handle
)
1090 if (handle
== NtCurrentProcess()) return TRUE
;
1091 SERVER_START_REQ( get_process_info
)
1093 req
->handle
= handle
;
1094 if (!wine_server_call( req
))
1095 ret
= ((DWORD
)reply
->pid
== GetCurrentProcessId());
1102 /***********************************************************************
1105 static inline void virtual_init(void)
1108 page_size
= getpagesize();
1109 page_mask
= page_size
- 1;
1110 /* Make sure we have a power of 2 */
1111 assert( !(page_size
& page_mask
) );
1113 while ((1 << page_shift
) != page_size
) page_shift
++;
1114 #endif /* page_mask */
1118 /***********************************************************************
1121 * Allocate a memory view for a new TEB, properly aligned to a multiple of the size.
1123 NTSTATUS
VIRTUAL_alloc_teb( void **ret
, size_t size
, BOOL first
)
1127 struct file_view
*view
;
1129 BYTE vprot
= VPROT_READ
| VPROT_WRITE
| VPROT_COMMITTED
;
1131 if (first
) virtual_init();
1134 size
= ROUND_SIZE( 0, size
);
1135 align_size
= page_size
;
1136 while (align_size
< size
) align_size
*= 2;
1140 if ((ptr
= wine_anon_mmap( NULL
, 2 * align_size
, VIRTUAL_GetUnixProt(vprot
), 0 )) == (void *)-1)
1142 if (errno
== ENOMEM
) return STATUS_NO_MEMORY
;
1143 return STATUS_INVALID_PARAMETER
;
1145 if (!is_beyond_limit( ptr
, 2 * align_size
, user_space_limit
))
1147 ptr
= unmap_extra_space( ptr
, 2 * align_size
, align_size
, align_size
- 1 );
1150 /* if we got something beyond the user limit, unmap it and retry */
1151 add_reserved_area( ptr
, 2 * align_size
);
1154 if (!first
) RtlEnterCriticalSection( &csVirtual
);
1156 status
= create_view( &view
, ptr
, size
, vprot
);
1157 if (status
== STATUS_SUCCESS
)
1159 view
->flags
|= VFLAG_VALLOC
;
1162 else unmap_area( ptr
, size
);
1164 if (!first
) RtlLeaveCriticalSection( &csVirtual
);
1170 /***********************************************************************
1171 * VIRTUAL_HandleFault
1173 NTSTATUS
VIRTUAL_HandleFault( LPCVOID addr
)
1176 NTSTATUS ret
= STATUS_ACCESS_VIOLATION
;
1178 RtlEnterCriticalSection( &csVirtual
);
1179 if ((view
= VIRTUAL_FindView( addr
)))
1181 BYTE vprot
= view
->prot
[((const char *)addr
- (const char *)view
->base
) >> page_shift
];
1182 void *page
= (void *)((UINT_PTR
)addr
& ~page_mask
);
1183 char *stack
= NtCurrentTeb()->Tib
.StackLimit
;
1184 if (vprot
& VPROT_GUARD
)
1186 VIRTUAL_SetProt( view
, page
, page_mask
+ 1, vprot
& ~VPROT_GUARD
);
1187 ret
= STATUS_GUARD_PAGE_VIOLATION
;
1189 /* is it inside the stack guard page? */
1190 if (((const char *)addr
>= stack
) && ((const char *)addr
< stack
+ (page_mask
+1)))
1191 ret
= STATUS_STACK_OVERFLOW
;
1193 RtlLeaveCriticalSection( &csVirtual
);
1197 /***********************************************************************
1198 * VIRTUAL_HasMapping
1200 * Check if the specified view has an associated file mapping.
1202 BOOL
VIRTUAL_HasMapping( LPCVOID addr
)
1207 RtlEnterCriticalSection( &csVirtual
);
1208 if ((view
= VIRTUAL_FindView( addr
))) ret
= (view
->mapping
!= 0);
1209 RtlLeaveCriticalSection( &csVirtual
);
1214 /***********************************************************************
1215 * VIRTUAL_UseLargeAddressSpace
1217 * Increase the address space size for apps that support it.
1219 void VIRTUAL_UseLargeAddressSpace(void)
1221 if (user_space_limit
>= ADDRESS_SPACE_LIMIT
) return;
1222 RtlEnterCriticalSection( &csVirtual
);
1223 remove_reserved_area( user_space_limit
, (char *)ADDRESS_SPACE_LIMIT
- (char *)user_space_limit
);
1224 user_space_limit
= ADDRESS_SPACE_LIMIT
;
1225 RtlLeaveCriticalSection( &csVirtual
);
1229 /***********************************************************************
1230 * NtAllocateVirtualMemory (NTDLL.@)
1231 * ZwAllocateVirtualMemory (NTDLL.@)
1233 NTSTATUS WINAPI
NtAllocateVirtualMemory( HANDLE process
, PVOID
*ret
, ULONG zero_bits
,
1234 SIZE_T
*size_ptr
, ULONG type
, ULONG protect
)
1238 SIZE_T size
= *size_ptr
;
1239 NTSTATUS status
= STATUS_SUCCESS
;
1240 struct file_view
*view
;
1242 TRACE("%p %p %08lx %lx %08lx\n", process
, *ret
, size
, type
, protect
);
1244 if (!size
) return STATUS_INVALID_PARAMETER
;
1246 if (!is_current_process( process
))
1248 ERR("Unsupported on other process\n");
1249 return STATUS_ACCESS_DENIED
;
1252 /* Round parameters to a page boundary */
1254 if (size
> 0x7fc00000) return STATUS_WORKING_SET_LIMIT_RANGE
; /* 2Gb - 4Mb */
1258 if (type
& MEM_RESERVE
) /* Round down to 64k boundary */
1259 base
= ROUND_ADDR( *ret
, granularity_mask
);
1261 base
= ROUND_ADDR( *ret
, page_mask
);
1262 size
= (((UINT_PTR
)*ret
+ size
+ page_mask
) & ~page_mask
) - (UINT_PTR
)base
;
1264 /* disallow low 64k, wrap-around and kernel space */
1265 if (((char *)base
<= (char *)granularity_mask
) ||
1266 ((char *)base
+ size
< (char *)base
) ||
1267 is_beyond_limit( base
, size
, ADDRESS_SPACE_LIMIT
))
1268 return STATUS_INVALID_PARAMETER
;
1273 size
= (size
+ page_mask
) & ~page_mask
;
1276 if (type
& MEM_TOP_DOWN
) {
1277 /* FIXME: MEM_TOP_DOWN allocates the largest possible address. */
1278 WARN("MEM_TOP_DOWN ignored\n");
1279 type
&= ~MEM_TOP_DOWN
;
1283 WARN("zero_bits %lu ignored\n", zero_bits
);
1285 /* Compute the alloc type flags */
1287 if (!(type
& MEM_SYSTEM
))
1289 if (!(type
& (MEM_COMMIT
| MEM_RESERVE
)) || (type
& ~(MEM_COMMIT
| MEM_RESERVE
)))
1291 WARN("called with wrong alloc type flags (%08lx) !\n", type
);
1292 return STATUS_INVALID_PARAMETER
;
1295 vprot
= VIRTUAL_GetProt( protect
);
1296 if (type
& MEM_COMMIT
) vprot
|= VPROT_COMMITTED
;
1298 /* Reserve the memory */
1300 RtlEnterCriticalSection( &csVirtual
);
1302 if (type
& MEM_SYSTEM
)
1304 if (type
& MEM_IMAGE
) vprot
|= VPROT_IMAGE
;
1305 status
= create_view( &view
, base
, size
, vprot
| VPROT_COMMITTED
);
1306 if (status
== STATUS_SUCCESS
)
1308 view
->flags
|= VFLAG_VALLOC
| VFLAG_SYSTEM
;
1312 else if ((type
& MEM_RESERVE
) || !base
)
1314 status
= map_view( &view
, base
, size
, vprot
);
1315 if (status
== STATUS_SUCCESS
)
1317 view
->flags
|= VFLAG_VALLOC
;
1321 else /* commit the pages */
1323 if (!(view
= VIRTUAL_FindView( base
)) ||
1324 ((char *)base
+ size
> (char *)view
->base
+ view
->size
)) status
= STATUS_NOT_MAPPED_VIEW
;
1325 else if (!VIRTUAL_SetProt( view
, base
, size
, vprot
)) status
= STATUS_ACCESS_DENIED
;
1328 RtlLeaveCriticalSection( &csVirtual
);
1330 if (status
== STATUS_SUCCESS
)
1339 /***********************************************************************
1340 * NtFreeVirtualMemory (NTDLL.@)
1341 * ZwFreeVirtualMemory (NTDLL.@)
1343 NTSTATUS WINAPI
NtFreeVirtualMemory( HANDLE process
, PVOID
*addr_ptr
, SIZE_T
*size_ptr
, ULONG type
)
1347 NTSTATUS status
= STATUS_SUCCESS
;
1348 LPVOID addr
= *addr_ptr
;
1349 SIZE_T size
= *size_ptr
;
1351 TRACE("%p %p %08lx %lx\n", process
, addr
, size
, type
);
1353 if (!is_current_process( process
))
1355 ERR("Unsupported on other process\n");
1356 return STATUS_ACCESS_DENIED
;
1359 /* Fix the parameters */
1361 size
= ROUND_SIZE( addr
, size
);
1362 base
= ROUND_ADDR( addr
, page_mask
);
1364 RtlEnterCriticalSection(&csVirtual
);
1366 if (!(view
= VIRTUAL_FindView( base
)) ||
1367 (base
+ size
> (char *)view
->base
+ view
->size
) ||
1368 !(view
->flags
& VFLAG_VALLOC
))
1370 status
= STATUS_INVALID_PARAMETER
;
1372 else if (type
& MEM_SYSTEM
)
1374 /* return the values that the caller should use to unmap the area */
1375 *addr_ptr
= view
->base
;
1376 *size_ptr
= view
->size
;
1377 view
->flags
|= VFLAG_SYSTEM
;
1378 delete_view( view
);
1380 else if (type
== MEM_RELEASE
)
1382 /* Free the pages */
1384 if (size
|| (base
!= view
->base
)) status
= STATUS_INVALID_PARAMETER
;
1387 delete_view( view
);
1392 else if (type
== MEM_DECOMMIT
)
1394 status
= decommit_pages( view
, base
- (char *)view
->base
, size
);
1395 if (status
== STATUS_SUCCESS
)
1403 WARN("called with wrong free type flags (%08lx) !\n", type
);
1404 status
= STATUS_INVALID_PARAMETER
;
1407 RtlLeaveCriticalSection(&csVirtual
);
1412 /***********************************************************************
1413 * NtProtectVirtualMemory (NTDLL.@)
1414 * ZwProtectVirtualMemory (NTDLL.@)
1416 NTSTATUS WINAPI
NtProtectVirtualMemory( HANDLE process
, PVOID
*addr_ptr
, SIZE_T
*size_ptr
,
1417 ULONG new_prot
, ULONG
*old_prot
)
1420 NTSTATUS status
= STATUS_SUCCESS
;
1425 SIZE_T size
= *size_ptr
;
1426 LPVOID addr
= *addr_ptr
;
1428 TRACE("%p %p %08lx %08lx\n", process
, addr
, size
, new_prot
);
1430 if (!is_current_process( process
))
1432 ERR("Unsupported on other process\n");
1433 return STATUS_ACCESS_DENIED
;
1436 /* Fix the parameters */
1438 size
= ROUND_SIZE( addr
, size
);
1439 base
= ROUND_ADDR( addr
, page_mask
);
1441 RtlEnterCriticalSection( &csVirtual
);
1443 if (!(view
= VIRTUAL_FindView( base
)) || (base
+ size
> (char *)view
->base
+ view
->size
))
1445 status
= STATUS_INVALID_PARAMETER
;
1449 /* Make sure all the pages are committed */
1451 p
= view
->prot
+ ((base
- (char *)view
->base
) >> page_shift
);
1452 VIRTUAL_GetWin32Prot( *p
, &prot
, NULL
);
1453 for (i
= size
>> page_shift
; i
; i
--, p
++)
1455 if (!(*p
& VPROT_COMMITTED
))
1457 status
= STATUS_NOT_COMMITTED
;
1463 if (old_prot
) *old_prot
= prot
;
1464 vprot
= VIRTUAL_GetProt( new_prot
) | VPROT_COMMITTED
;
1465 if (!VIRTUAL_SetProt( view
, base
, size
, vprot
)) status
= STATUS_ACCESS_DENIED
;
1468 RtlLeaveCriticalSection( &csVirtual
);
1470 if (status
== STATUS_SUCCESS
)
1478 #define UNIMPLEMENTED_INFO_CLASS(c) \
1480 FIXME("(process=%p,addr=%p) Unimplemented information class: " #c "\n", process, addr); \
1481 return STATUS_INVALID_INFO_CLASS
1483 /***********************************************************************
1484 * NtQueryVirtualMemory (NTDLL.@)
1485 * ZwQueryVirtualMemory (NTDLL.@)
1487 NTSTATUS WINAPI
NtQueryVirtualMemory( HANDLE process
, LPCVOID addr
,
1488 MEMORY_INFORMATION_CLASS info_class
, PVOID buffer
,
1489 SIZE_T len
, SIZE_T
*res_len
)
1492 char *base
, *alloc_base
= 0;
1495 MEMORY_BASIC_INFORMATION
*info
= buffer
;
1497 if (info_class
!= MemoryBasicInformation
)
1501 UNIMPLEMENTED_INFO_CLASS(MemoryWorkingSetList
);
1502 UNIMPLEMENTED_INFO_CLASS(MemorySectionName
);
1503 UNIMPLEMENTED_INFO_CLASS(MemoryBasicVlmInformation
);
1506 FIXME("(%p,%p,info_class=%d,%p,%ld,%p) Unknown information class\n",
1507 process
, addr
, info_class
, buffer
, len
, res_len
);
1508 return STATUS_INVALID_INFO_CLASS
;
1511 if (ADDRESS_SPACE_LIMIT
&& addr
>= ADDRESS_SPACE_LIMIT
)
1512 return STATUS_WORKING_SET_LIMIT_RANGE
;
1514 if (!is_current_process( process
))
1516 ERR("Unsupported on other process\n");
1517 return STATUS_ACCESS_DENIED
;
1520 base
= ROUND_ADDR( addr
, page_mask
);
1522 /* Find the view containing the address */
1524 RtlEnterCriticalSection(&csVirtual
);
1525 ptr
= list_head( &views_list
);
1530 /* make the address space end at the user limit, except if
1531 * the last view was mapped beyond that */
1532 if (alloc_base
<= (char *)user_space_limit
)
1534 if (user_space_limit
&& base
>= (char *)user_space_limit
)
1536 RtlLeaveCriticalSection( &csVirtual
);
1537 return STATUS_WORKING_SET_LIMIT_RANGE
;
1539 size
= (char *)user_space_limit
- alloc_base
;
1541 else size
= (char *)ADDRESS_SPACE_LIMIT
- alloc_base
;
1545 view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
1546 if ((char *)view
->base
> base
)
1548 size
= (char *)view
->base
- alloc_base
;
1552 if ((char *)view
->base
+ view
->size
> base
)
1554 alloc_base
= view
->base
;
1558 alloc_base
= (char *)view
->base
+ view
->size
;
1559 ptr
= list_next( &views_list
, ptr
);
1562 /* Fill the info structure */
1566 info
->State
= MEM_FREE
;
1568 info
->AllocationProtect
= 0;
1573 BYTE vprot
= view
->prot
[(base
- alloc_base
) >> page_shift
];
1574 VIRTUAL_GetWin32Prot( vprot
, &info
->Protect
, &info
->State
);
1575 for (size
= base
- alloc_base
; size
< view
->size
; size
+= page_mask
+1)
1576 if (view
->prot
[size
>> page_shift
] != vprot
) break;
1577 VIRTUAL_GetWin32Prot( view
->protect
, &info
->AllocationProtect
, NULL
);
1578 if (view
->protect
& VPROT_IMAGE
) info
->Type
= MEM_IMAGE
;
1579 else if (view
->flags
& VFLAG_VALLOC
) info
->Type
= MEM_PRIVATE
;
1580 else info
->Type
= MEM_MAPPED
;
1582 RtlLeaveCriticalSection(&csVirtual
);
1584 info
->BaseAddress
= (LPVOID
)base
;
1585 info
->AllocationBase
= (LPVOID
)alloc_base
;
1586 info
->RegionSize
= size
- (base
- alloc_base
);
1587 if (res_len
) *res_len
= sizeof(*info
);
1588 return STATUS_SUCCESS
;
1592 /***********************************************************************
1593 * NtLockVirtualMemory (NTDLL.@)
1594 * ZwLockVirtualMemory (NTDLL.@)
1596 NTSTATUS WINAPI
NtLockVirtualMemory( HANDLE process
, PVOID
*addr
, SIZE_T
*size
, ULONG unknown
)
1598 if (!is_current_process( process
))
1600 ERR("Unsupported on other process\n");
1601 return STATUS_ACCESS_DENIED
;
1603 return STATUS_SUCCESS
;
1607 /***********************************************************************
1608 * NtUnlockVirtualMemory (NTDLL.@)
1609 * ZwUnlockVirtualMemory (NTDLL.@)
1611 NTSTATUS WINAPI
NtUnlockVirtualMemory( HANDLE process
, PVOID
*addr
, SIZE_T
*size
, ULONG unknown
)
1613 if (!is_current_process( process
))
1615 ERR("Unsupported on other process\n");
1616 return STATUS_ACCESS_DENIED
;
1618 return STATUS_SUCCESS
;
1622 /***********************************************************************
1623 * NtCreateSection (NTDLL.@)
1624 * ZwCreateSection (NTDLL.@)
1626 NTSTATUS WINAPI
NtCreateSection( HANDLE
*handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
,
1627 const LARGE_INTEGER
*size
, ULONG protect
,
1628 ULONG sec_flags
, HANDLE file
)
1632 DWORD len
= (attr
&& attr
->ObjectName
) ? attr
->ObjectName
->Length
: 0;
1634 /* Check parameters */
1636 if (len
> MAX_PATH
*sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
1638 vprot
= VIRTUAL_GetProt( protect
);
1639 if (sec_flags
& SEC_RESERVE
)
1641 if (file
) return STATUS_INVALID_PARAMETER
;
1643 else vprot
|= VPROT_COMMITTED
;
1644 if (sec_flags
& SEC_NOCACHE
) vprot
|= VPROT_NOCACHE
;
1645 if (sec_flags
& SEC_IMAGE
) vprot
|= VPROT_IMAGE
;
1647 /* Create the server object */
1649 SERVER_START_REQ( create_mapping
)
1651 req
->file_handle
= file
;
1652 req
->size_high
= size
? size
->u
.HighPart
: 0;
1653 req
->size_low
= size
? size
->u
.LowPart
: 0;
1654 req
->protect
= vprot
;
1655 req
->access
= access
;
1656 req
->inherit
= (attr
&& (attr
->Attributes
& OBJ_INHERIT
) != 0);
1657 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
1658 ret
= wine_server_call( req
);
1659 *handle
= reply
->handle
;
1666 /***********************************************************************
1667 * NtOpenSection (NTDLL.@)
1668 * ZwOpenSection (NTDLL.@)
1670 NTSTATUS WINAPI
NtOpenSection( HANDLE
*handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
)
1673 DWORD len
= attr
->ObjectName
->Length
;
1675 if (len
> MAX_PATH
*sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
1677 SERVER_START_REQ( open_mapping
)
1679 req
->access
= access
;
1680 req
->inherit
= (attr
->Attributes
& OBJ_INHERIT
) != 0;
1681 wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
1682 if (!(ret
= wine_server_call( req
))) *handle
= reply
->handle
;
1689 /***********************************************************************
1690 * NtMapViewOfSection (NTDLL.@)
1691 * ZwMapViewOfSection (NTDLL.@)
1693 NTSTATUS WINAPI
NtMapViewOfSection( HANDLE handle
, HANDLE process
, PVOID
*addr_ptr
, ULONG zero_bits
,
1694 SIZE_T commit_size
, const LARGE_INTEGER
*offset_ptr
, SIZE_T
*size_ptr
,
1695 SECTION_INHERIT inherit
, ULONG alloc_type
, ULONG protect
)
1697 FILE_FS_DEVICE_INFORMATION device_info
;
1700 int unix_handle
= -1;
1703 struct file_view
*view
;
1704 DWORD size_low
, size_high
, header_size
, shared_size
;
1706 BOOL removable
= FALSE
;
1707 LARGE_INTEGER offset
;
1709 offset
.QuadPart
= offset_ptr
? offset_ptr
->QuadPart
: 0;
1711 TRACE("handle=%p process=%p addr=%p off=%lx%08lx size=%lx access=%lx\n",
1712 handle
, process
, *addr_ptr
, offset
.u
.HighPart
, offset
.u
.LowPart
, size
, protect
);
1714 if (!is_current_process( process
))
1716 ERR("Unsupported on other process\n");
1717 return STATUS_ACCESS_DENIED
;
1720 /* Check parameters */
1722 if ((offset
.u
.LowPart
& granularity_mask
) ||
1723 (*addr_ptr
&& ((UINT_PTR
)*addr_ptr
& granularity_mask
)))
1724 return STATUS_INVALID_PARAMETER
;
1726 SERVER_START_REQ( get_mapping_info
)
1728 req
->handle
= handle
;
1729 res
= wine_server_call( req
);
1730 prot
= reply
->protect
;
1732 size_low
= reply
->size_low
;
1733 size_high
= reply
->size_high
;
1734 header_size
= reply
->header_size
;
1735 shared_file
= reply
->shared_file
;
1736 shared_size
= reply
->shared_size
;
1739 if (res
) return res
;
1741 if ((res
= wine_server_handle_to_fd( handle
, 0, &unix_handle
, NULL
))) return res
;
1743 if (FILE_GetDeviceInfo( unix_handle
, &device_info
) == STATUS_SUCCESS
)
1744 removable
= device_info
.Characteristics
& FILE_REMOVABLE_MEDIA
;
1746 if (prot
& VPROT_IMAGE
)
1752 if ((res
= wine_server_handle_to_fd( shared_file
, GENERIC_READ
, &shared_fd
,
1754 res
= map_image( handle
, unix_handle
, base
, size_low
, header_size
,
1755 shared_fd
, removable
, addr_ptr
);
1756 wine_server_release_fd( shared_file
, shared_fd
);
1757 NtClose( shared_file
);
1761 res
= map_image( handle
, unix_handle
, base
, size_low
, header_size
,
1762 -1, removable
, addr_ptr
);
1764 wine_server_release_fd( handle
, unix_handle
);
1765 if (!res
) *size_ptr
= size_low
;
1770 ERR("Sizes larger than 4Gb not supported\n");
1772 if ((offset
.u
.LowPart
>= size_low
) ||
1773 (*size_ptr
> size_low
- offset
.u
.LowPart
))
1775 res
= STATUS_INVALID_PARAMETER
;
1778 if (*size_ptr
) size
= ROUND_SIZE( offset
.u
.LowPart
, *size_ptr
);
1779 else size
= size_low
- offset
.u
.LowPart
;
1785 case PAGE_READWRITE
:
1786 case PAGE_EXECUTE_READWRITE
:
1787 if (!(prot
& VPROT_WRITE
))
1789 res
= STATUS_INVALID_PARAMETER
;
1795 case PAGE_WRITECOPY
:
1797 case PAGE_EXECUTE_READ
:
1798 case PAGE_EXECUTE_WRITECOPY
:
1799 if (prot
& VPROT_READ
) break;
1802 res
= STATUS_INVALID_PARAMETER
;
1806 /* FIXME: If a mapping is created with SEC_RESERVE and a process,
1807 * which has a view of this mapping commits some pages, they will
1808 * appear commited in all other processes, which have the same
1809 * view created. Since we don`t support this yet, we create the
1810 * whole mapping commited.
1812 prot
|= VPROT_COMMITTED
;
1814 /* Reserve a properly aligned area */
1816 RtlEnterCriticalSection( &csVirtual
);
1818 res
= map_view( &view
, *addr_ptr
, size
, prot
);
1821 RtlLeaveCriticalSection( &csVirtual
);
1827 TRACE("handle=%p size=%lx offset=%lx%08lx\n",
1828 handle
, size
, offset
.u
.HighPart
, offset
.u
.LowPart
);
1830 res
= map_file_into_view( view
, unix_handle
, 0, size
, offset
.QuadPart
, prot
, removable
);
1831 if (res
== STATUS_SUCCESS
)
1833 if (!removable
) /* don't keep handle open on removable media */
1834 NtDuplicateObject( NtCurrentProcess(), handle
,
1835 NtCurrentProcess(), &view
->mapping
,
1836 0, 0, DUPLICATE_SAME_ACCESS
);
1838 *addr_ptr
= view
->base
;
1843 ERR( "map_file_into_view %p %lx %lx%08lx failed\n",
1844 view
->base
, size
, offset
.u
.HighPart
, offset
.u
.LowPart
);
1845 delete_view( view
);
1848 RtlLeaveCriticalSection( &csVirtual
);
1851 wine_server_release_fd( handle
, unix_handle
);
1856 /***********************************************************************
1857 * NtUnmapViewOfSection (NTDLL.@)
1858 * ZwUnmapViewOfSection (NTDLL.@)
1860 NTSTATUS WINAPI
NtUnmapViewOfSection( HANDLE process
, PVOID addr
)
1863 NTSTATUS status
= STATUS_INVALID_PARAMETER
;
1864 void *base
= ROUND_ADDR( addr
, page_mask
);
1866 if (!is_current_process( process
))
1868 ERR("Unsupported on other process\n");
1869 return STATUS_ACCESS_DENIED
;
1871 RtlEnterCriticalSection( &csVirtual
);
1872 if ((view
= VIRTUAL_FindView( base
)) && (base
== view
->base
))
1874 delete_view( view
);
1875 status
= STATUS_SUCCESS
;
1877 RtlLeaveCriticalSection( &csVirtual
);
1882 /***********************************************************************
1883 * NtFlushVirtualMemory (NTDLL.@)
1884 * ZwFlushVirtualMemory (NTDLL.@)
1886 NTSTATUS WINAPI
NtFlushVirtualMemory( HANDLE process
, LPCVOID
*addr_ptr
,
1887 SIZE_T
*size_ptr
, ULONG unknown
)
1890 NTSTATUS status
= STATUS_SUCCESS
;
1891 void *addr
= ROUND_ADDR( *addr_ptr
, page_mask
);
1893 if (!is_current_process( process
))
1895 ERR("Unsupported on other process\n");
1896 return STATUS_ACCESS_DENIED
;
1898 RtlEnterCriticalSection( &csVirtual
);
1899 if (!(view
= VIRTUAL_FindView( addr
))) status
= STATUS_INVALID_PARAMETER
;
1902 if (!*size_ptr
) *size_ptr
= view
->size
;
1904 if (msync( addr
, *size_ptr
, MS_SYNC
)) status
= STATUS_NOT_MAPPED_DATA
;
1906 RtlLeaveCriticalSection( &csVirtual
);
1911 /***********************************************************************
1912 * NtReadVirtualMemory (NTDLL.@)
1913 * ZwReadVirtualMemory (NTDLL.@)
1915 NTSTATUS WINAPI
NtReadVirtualMemory( HANDLE process
, const void *addr
, void *buffer
,
1916 SIZE_T size
, SIZE_T
*bytes_read
)
1920 SERVER_START_REQ( read_process_memory
)
1922 req
->handle
= process
;
1923 req
->addr
= (void *)addr
;
1924 wine_server_set_reply( req
, buffer
, size
);
1925 if ((status
= wine_server_call( req
))) size
= 0;
1928 if (bytes_read
) *bytes_read
= size
;
1933 /***********************************************************************
1934 * NtWriteVirtualMemory (NTDLL.@)
1935 * ZwWriteVirtualMemory (NTDLL.@)
1937 NTSTATUS WINAPI
NtWriteVirtualMemory( HANDLE process
, void *addr
, const void *buffer
,
1938 SIZE_T size
, SIZE_T
*bytes_written
)
1940 static const unsigned int zero
;
1941 SIZE_T first_offset
, last_offset
, first_mask
, last_mask
;
1944 if (!size
) return STATUS_INVALID_PARAMETER
;
1946 /* compute the mask for the first int */
1948 first_offset
= (ULONG_PTR
)addr
% sizeof(int);
1949 memset( &first_mask
, 0, first_offset
);
1951 /* compute the mask for the last int */
1952 last_offset
= (size
+ first_offset
) % sizeof(int);
1954 memset( &last_mask
, 0xff, last_offset
? last_offset
: sizeof(int) );
1956 SERVER_START_REQ( write_process_memory
)
1958 req
->handle
= process
;
1959 req
->addr
= (char *)addr
- first_offset
;
1960 req
->first_mask
= first_mask
;
1961 req
->last_mask
= last_mask
;
1962 if (first_offset
) wine_server_add_data( req
, &zero
, first_offset
);
1963 wine_server_add_data( req
, buffer
, size
);
1964 if (last_offset
) wine_server_add_data( req
, &zero
, sizeof(int) - last_offset
);
1966 if ((status
= wine_server_call( req
))) size
= 0;
1969 if (bytes_written
) *bytes_written
= size
;