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
51 #include "wine/library.h"
52 #include "wine/server.h"
53 #include "wine/list.h"
54 #include "wine/debug.h"
55 #include "ntdll_misc.h"
57 WINE_DEFAULT_DEBUG_CHANNEL(virtual);
58 WINE_DECLARE_DEBUG_CHANNEL(module
);
65 #define MAP_NORESERVE 0
69 typedef struct file_view
71 struct list entry
; /* Entry in global view list */
72 void *base
; /* Base address */
73 size_t size
; /* Size in bytes */
74 HANDLE mapping
; /* Handle to the file mapping */
75 BYTE flags
; /* Allocation flags (VFLAG_*) */
76 BYTE protect
; /* Protection for all pages at allocation time */
77 BYTE prot
[1]; /* Protection byte for each page */
81 #define VFLAG_SYSTEM 0x01 /* system view (underlying mmap not under our control) */
82 #define VFLAG_VALLOC 0x02 /* allocated by VirtualAlloc */
84 /* Conversion from VPROT_* to Win32 flags */
85 static const BYTE VIRTUAL_Win32Flags
[16] =
87 PAGE_NOACCESS
, /* 0 */
88 PAGE_READONLY
, /* READ */
89 PAGE_READWRITE
, /* WRITE */
90 PAGE_READWRITE
, /* READ | WRITE */
91 PAGE_EXECUTE
, /* EXEC */
92 PAGE_EXECUTE_READ
, /* READ | EXEC */
93 PAGE_EXECUTE_READWRITE
, /* WRITE | EXEC */
94 PAGE_EXECUTE_READWRITE
, /* READ | WRITE | EXEC */
95 PAGE_WRITECOPY
, /* WRITECOPY */
96 PAGE_WRITECOPY
, /* READ | WRITECOPY */
97 PAGE_WRITECOPY
, /* WRITE | WRITECOPY */
98 PAGE_WRITECOPY
, /* READ | WRITE | WRITECOPY */
99 PAGE_EXECUTE_WRITECOPY
, /* EXEC | WRITECOPY */
100 PAGE_EXECUTE_WRITECOPY
, /* READ | EXEC | WRITECOPY */
101 PAGE_EXECUTE_WRITECOPY
, /* WRITE | EXEC | WRITECOPY */
102 PAGE_EXECUTE_WRITECOPY
/* READ | WRITE | EXEC | WRITECOPY */
105 static struct list views_list
= LIST_INIT(views_list
);
107 static RTL_CRITICAL_SECTION csVirtual
;
108 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
111 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
112 0, 0, { (DWORD_PTR
)(__FILE__
": csVirtual") }
114 static RTL_CRITICAL_SECTION csVirtual
= { &critsect_debug
, -1, 0, 0, 0, 0 };
117 /* These are always the same on an i386, and it will be faster this way */
118 # define page_mask 0xfff
119 # define page_shift 12
120 # define page_size 0x1000
121 /* Note: these are Windows limits, you cannot change them. */
122 # define ADDRESS_SPACE_LIMIT ((void *)0xc0000000) /* top of the total available address space */
123 # define USER_SPACE_LIMIT ((void *)0x7fff0000) /* top of the user address space */
125 static UINT page_shift
;
126 static UINT page_size
;
127 static UINT_PTR page_mask
;
128 # define ADDRESS_SPACE_LIMIT 0 /* no limit needed on other platforms */
129 # define USER_SPACE_LIMIT 0 /* no limit needed on other platforms */
130 #endif /* __i386__ */
132 #define ROUND_ADDR(addr,mask) \
133 ((void *)((UINT_PTR)(addr) & ~(UINT_PTR)(mask)))
135 #define ROUND_SIZE(addr,size) \
136 (((UINT)(size) + ((UINT_PTR)(addr) & page_mask) + page_mask) & ~page_mask)
138 #define VIRTUAL_DEBUG_DUMP_VIEW(view) \
139 do { if (TRACE_ON(virtual)) VIRTUAL_DumpView(view); } while (0)
141 static void *user_space_limit
= USER_SPACE_LIMIT
;
142 static void *preload_reserve_start
;
143 static void *preload_reserve_end
;
144 static int use_locks
;
145 static int force_exec_prot
; /* whether to force PROT_EXEC on all PROT_READ mmaps */
148 /***********************************************************************
151 static const char *VIRTUAL_GetProtStr( BYTE prot
)
153 static char buffer
[6];
154 buffer
[0] = (prot
& VPROT_COMMITTED
) ? 'c' : '-';
155 buffer
[1] = (prot
& VPROT_GUARD
) ? 'g' : '-';
156 buffer
[2] = (prot
& VPROT_READ
) ? 'r' : '-';
157 buffer
[3] = (prot
& VPROT_WRITECOPY
) ? 'W' : ((prot
& VPROT_WRITE
) ? 'w' : '-');
158 buffer
[4] = (prot
& VPROT_EXEC
) ? 'x' : '-';
164 /***********************************************************************
165 * VIRTUAL_GetUnixProt
167 * Convert page protections to protection for mmap/mprotect.
169 static int VIRTUAL_GetUnixProt( BYTE vprot
)
172 if ((vprot
& VPROT_COMMITTED
) && !(vprot
& VPROT_GUARD
))
174 if (vprot
& VPROT_READ
) prot
|= PROT_READ
;
175 if (vprot
& VPROT_WRITE
) prot
|= PROT_WRITE
;
176 if (vprot
& VPROT_WRITECOPY
) prot
|= PROT_WRITE
;
177 if (vprot
& VPROT_EXEC
) prot
|= PROT_EXEC
;
179 if (!prot
) prot
= PROT_NONE
;
184 /***********************************************************************
187 static void VIRTUAL_DumpView( FILE_VIEW
*view
)
190 char *addr
= view
->base
;
191 BYTE prot
= view
->prot
[0];
193 TRACE( "View: %p - %p", addr
, addr
+ view
->size
- 1 );
194 if (view
->flags
& VFLAG_SYSTEM
)
195 TRACE( " (system)\n" );
196 else if (view
->flags
& VFLAG_VALLOC
)
197 TRACE( " (valloc)\n" );
198 else if (view
->mapping
)
199 TRACE( " %p\n", view
->mapping
);
201 TRACE( " (anonymous)\n");
203 for (count
= i
= 1; i
< view
->size
>> page_shift
; i
++, count
++)
205 if (view
->prot
[i
] == prot
) continue;
206 TRACE( " %p - %p %s\n",
207 addr
, addr
+ (count
<< page_shift
) - 1, VIRTUAL_GetProtStr(prot
) );
208 addr
+= (count
<< page_shift
);
209 prot
= view
->prot
[i
];
213 TRACE( " %p - %p %s\n",
214 addr
, addr
+ (count
<< page_shift
) - 1, VIRTUAL_GetProtStr(prot
) );
218 /***********************************************************************
222 static void VIRTUAL_Dump(void)
225 struct file_view
*view
;
227 TRACE( "Dump of all virtual memory views:\n" );
228 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
229 LIST_FOR_EACH_ENTRY( view
, &views_list
, FILE_VIEW
, entry
)
231 VIRTUAL_DumpView( view
);
233 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
238 /***********************************************************************
241 * Find the view containing a given address. The csVirtual section must be held by caller.
250 static struct file_view
*VIRTUAL_FindView( const void *addr
)
252 struct file_view
*view
;
254 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
256 if (view
->base
> addr
) break;
257 if ((const char*)view
->base
+ view
->size
> (const char*)addr
) return view
;
263 /***********************************************************************
266 static inline UINT_PTR
get_mask( ULONG zero_bits
)
268 if (!zero_bits
) return 0xffff; /* allocations are aligned to 64K by default */
269 if (zero_bits
< page_shift
) zero_bits
= page_shift
;
270 return (1 << zero_bits
) - 1;
274 /***********************************************************************
277 * Find the first view overlapping at least part of the specified range.
278 * The csVirtual section must be held by caller.
280 static struct file_view
*find_view_range( const void *addr
, size_t size
)
282 struct file_view
*view
;
284 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
286 if ((const char *)view
->base
>= (const char *)addr
+ size
) break;
287 if ((const char *)view
->base
+ view
->size
> (const char *)addr
) return view
;
293 /***********************************************************************
296 * Find a free area between views inside the specified range.
297 * The csVirtual section must be held by caller.
299 static void *find_free_area( void *base
, void *end
, size_t size
, size_t mask
, int top_down
)
306 start
= ROUND_ADDR( (char *)end
- size
, mask
);
307 if (start
>= end
|| start
< base
) return NULL
;
309 for (ptr
= views_list
.prev
; ptr
!= &views_list
; ptr
= ptr
->prev
)
311 struct file_view
*view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
313 if ((char *)view
->base
+ view
->size
<= (char *)start
) break;
314 if ((char *)view
->base
>= (char *)start
+ size
) continue;
315 start
= ROUND_ADDR( (char *)view
->base
- size
, mask
);
316 /* stop if remaining space is not large enough */
317 if (!start
|| start
>= end
|| start
< base
) return NULL
;
322 start
= ROUND_ADDR( (char *)base
+ mask
, mask
);
323 if (start
>= end
|| (char *)end
- (char *)start
< size
) return NULL
;
325 for (ptr
= views_list
.next
; ptr
!= &views_list
; ptr
= ptr
->next
)
327 struct file_view
*view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
329 if ((char *)view
->base
>= (char *)start
+ size
) break;
330 if ((char *)view
->base
+ view
->size
<= (char *)start
) continue;
331 start
= ROUND_ADDR( (char *)view
->base
+ view
->size
+ mask
, mask
);
332 /* stop if remaining space is not large enough */
333 if (!start
|| start
>= end
|| (char *)end
- (char *)start
< size
) return NULL
;
340 /***********************************************************************
343 * Add a reserved area to the list maintained by libwine.
344 * The csVirtual section must be held by caller.
346 static void add_reserved_area( void *addr
, size_t size
)
348 TRACE( "adding %p-%p\n", addr
, (char *)addr
+ size
);
350 if (addr
< user_space_limit
)
352 /* unmap the part of the area that is below the limit */
353 assert( (char *)addr
+ size
> (char *)user_space_limit
);
354 munmap( addr
, (char *)user_space_limit
- (char *)addr
);
355 size
-= (char *)user_space_limit
- (char *)addr
;
356 addr
= user_space_limit
;
358 /* blow away existing mappings */
359 wine_anon_mmap( addr
, size
, PROT_NONE
, MAP_NORESERVE
| MAP_FIXED
);
360 wine_mmap_add_reserved_area( addr
, size
);
364 /***********************************************************************
367 * Check if an address range goes beyond a given limit.
369 static inline int is_beyond_limit( const void *addr
, size_t size
, const void *limit
)
371 return (limit
&& (addr
>= limit
|| (const char *)addr
+ size
> (const char *)limit
));
375 /***********************************************************************
378 * Unmap an area, or simply replace it by an empty mapping if it is
379 * in a reserved area. The csVirtual section must be held by caller.
381 static inline void unmap_area( void *addr
, size_t size
)
383 if (wine_mmap_is_in_reserved_area( addr
, size
))
384 wine_anon_mmap( addr
, size
, PROT_NONE
, MAP_NORESERVE
| MAP_FIXED
);
385 else if (is_beyond_limit( addr
, size
, user_space_limit
))
386 add_reserved_area( addr
, size
);
388 munmap( addr
, size
);
392 /***********************************************************************
395 * Deletes a view. The csVirtual section must be held by caller.
397 static void delete_view( struct file_view
*view
) /* [in] View */
399 if (!(view
->flags
& VFLAG_SYSTEM
)) unmap_area( view
->base
, view
->size
);
400 list_remove( &view
->entry
);
401 if (view
->mapping
) NtClose( view
->mapping
);
406 /***********************************************************************
409 * Create a view. The csVirtual section must be held by caller.
411 static NTSTATUS
create_view( struct file_view
**view_ret
, void *base
, size_t size
, BYTE vprot
)
413 struct file_view
*view
;
415 int unix_prot
= VIRTUAL_GetUnixProt( vprot
);
417 assert( !((UINT_PTR
)base
& page_mask
) );
418 assert( !(size
& page_mask
) );
420 /* Create the view structure */
422 if (!(view
= malloc( sizeof(*view
) + (size
>> page_shift
) - 1 ))) return STATUS_NO_MEMORY
;
428 view
->protect
= vprot
;
429 memset( view
->prot
, vprot
& ~VPROT_IMAGE
, size
>> page_shift
);
431 /* Insert it in the linked list */
433 LIST_FOR_EACH( ptr
, &views_list
)
435 struct file_view
*next
= LIST_ENTRY( ptr
, struct file_view
, entry
);
436 if (next
->base
> base
) break;
438 list_add_before( ptr
, &view
->entry
);
440 /* Check for overlapping views. This can happen if the previous view
441 * was a system view that got unmapped behind our back. In that case
442 * we recover by simply deleting it. */
444 if ((ptr
= list_prev( &views_list
, &view
->entry
)) != NULL
)
446 struct file_view
*prev
= LIST_ENTRY( ptr
, struct file_view
, entry
);
447 if ((char *)prev
->base
+ prev
->size
> (char *)base
)
449 TRACE( "overlapping prev view %p-%p for %p-%p\n",
450 prev
->base
, (char *)prev
->base
+ prev
->size
,
451 base
, (char *)base
+ view
->size
);
452 assert( prev
->flags
& VFLAG_SYSTEM
);
456 if ((ptr
= list_next( &views_list
, &view
->entry
)) != NULL
)
458 struct file_view
*next
= LIST_ENTRY( ptr
, struct file_view
, entry
);
459 if ((char *)base
+ view
->size
> (char *)next
->base
)
461 TRACE( "overlapping next view %p-%p for %p-%p\n",
462 next
->base
, (char *)next
->base
+ next
->size
,
463 base
, (char *)base
+ view
->size
);
464 assert( next
->flags
& VFLAG_SYSTEM
);
470 VIRTUAL_DEBUG_DUMP_VIEW( view
);
472 if (force_exec_prot
&& (unix_prot
& PROT_READ
) && !(unix_prot
& PROT_EXEC
))
474 TRACE( "forcing exec permission on %p-%p\n", base
, (char *)base
+ size
- 1 );
475 mprotect( base
, size
, unix_prot
| PROT_EXEC
);
477 return STATUS_SUCCESS
;
481 /***********************************************************************
482 * VIRTUAL_GetWin32Prot
484 * Convert page protections to Win32 flags.
486 static DWORD
VIRTUAL_GetWin32Prot( BYTE vprot
)
488 DWORD ret
= VIRTUAL_Win32Flags
[vprot
& 0x0f];
489 if (vprot
& VPROT_NOCACHE
) ret
|= PAGE_NOCACHE
;
490 if (vprot
& VPROT_GUARD
) ret
|= PAGE_GUARD
;
495 /***********************************************************************
498 * Build page protections from Win32 flags.
501 * protect [I] Win32 protection flags
504 * Value of page protection flags
506 static BYTE
VIRTUAL_GetProt( DWORD protect
)
510 switch(protect
& 0xff)
516 vprot
= VPROT_READ
| VPROT_WRITE
;
519 /* MSDN CreateFileMapping() states that if PAGE_WRITECOPY is given,
520 * that the hFile must have been opened with GENERIC_READ and
521 * GENERIC_WRITE access. This is WRONG as tests show that you
522 * only need GENERIC_READ access (at least for Win9x,
523 * FIXME: what about NT?). Thus, we don't put VPROT_WRITE in
524 * PAGE_WRITECOPY and PAGE_EXECUTE_WRITECOPY.
526 vprot
= VPROT_READ
| VPROT_WRITECOPY
;
531 case PAGE_EXECUTE_READ
:
532 vprot
= VPROT_EXEC
| VPROT_READ
;
534 case PAGE_EXECUTE_READWRITE
:
535 vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITE
;
537 case PAGE_EXECUTE_WRITECOPY
:
538 /* See comment for PAGE_WRITECOPY above */
539 vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITECOPY
;
546 if (protect
& PAGE_GUARD
) vprot
|= VPROT_GUARD
;
547 if (protect
& PAGE_NOCACHE
) vprot
|= VPROT_NOCACHE
;
552 /***********************************************************************
555 * Change the protection of a range of pages.
561 static BOOL
VIRTUAL_SetProt( FILE_VIEW
*view
, /* [in] Pointer to view */
562 void *base
, /* [in] Starting address */
563 size_t size
, /* [in] Size in bytes */
564 BYTE vprot
) /* [in] Protections to use */
566 int unix_prot
= VIRTUAL_GetUnixProt(vprot
);
569 base
, (char *)base
+ size
- 1, VIRTUAL_GetProtStr( vprot
) );
571 if (force_exec_prot
&& (unix_prot
& PROT_READ
) && !(unix_prot
& PROT_EXEC
))
573 TRACE( "forcing exec permission on %p-%p\n", base
, (char *)base
+ size
- 1 );
574 if (!mprotect( base
, size
, unix_prot
| PROT_EXEC
)) goto done
;
575 /* exec + write may legitimately fail, in that case fall back to write only */
576 if (!(unix_prot
& PROT_WRITE
)) return FALSE
;
579 if (mprotect( base
, size
, unix_prot
)) return FALSE
; /* FIXME: last error */
582 memset( view
->prot
+ (((char *)base
- (char *)view
->base
) >> page_shift
),
583 vprot
, size
>> page_shift
);
584 VIRTUAL_DEBUG_DUMP_VIEW( view
);
589 /***********************************************************************
592 * Release the extra memory while keeping the range starting on the granularity boundary.
594 static inline void *unmap_extra_space( void *ptr
, size_t total_size
, size_t wanted_size
, size_t mask
)
596 if ((ULONG_PTR
)ptr
& mask
)
598 size_t extra
= mask
+ 1 - ((ULONG_PTR
)ptr
& mask
);
599 munmap( ptr
, extra
);
600 ptr
= (char *)ptr
+ extra
;
603 if (total_size
> wanted_size
)
604 munmap( (char *)ptr
+ wanted_size
, total_size
- wanted_size
);
617 /***********************************************************************
618 * alloc_reserved_area_callback
620 * Try to map some space inside a reserved area. Callback for wine_mmap_enum_reserved_areas.
622 static int alloc_reserved_area_callback( void *start
, size_t size
, void *arg
)
624 static void * const address_space_start
= (void *)0x110000;
625 struct alloc_area
*alloc
= arg
;
626 void *end
= (char *)start
+ size
;
628 if (start
< address_space_start
) start
= address_space_start
;
629 if (user_space_limit
&& end
> user_space_limit
) end
= user_space_limit
;
630 if (start
>= end
) return 0;
632 /* make sure we don't touch the preloader reserved range */
633 if (preload_reserve_end
>= start
)
635 if (preload_reserve_end
>= end
)
637 if (preload_reserve_start
<= start
) return 0; /* no space in that area */
638 if (preload_reserve_start
< end
) end
= preload_reserve_start
;
640 else if (preload_reserve_start
<= start
) start
= preload_reserve_end
;
643 /* range is split in two by the preloader reservation, try first part */
644 if ((alloc
->result
= find_free_area( start
, preload_reserve_start
, alloc
->size
,
645 alloc
->mask
, alloc
->top_down
)))
647 /* then fall through to try second part */
648 start
= preload_reserve_end
;
651 if ((alloc
->result
= find_free_area( start
, end
, alloc
->size
, alloc
->mask
, alloc
->top_down
)))
658 /***********************************************************************
661 * Create a view and mmap the corresponding memory area.
662 * The csVirtual section must be held by caller.
664 static NTSTATUS
map_view( struct file_view
**view_ret
, void *base
, size_t size
, size_t mask
,
665 int top_down
, BYTE vprot
)
672 if (is_beyond_limit( base
, size
, ADDRESS_SPACE_LIMIT
))
673 return STATUS_WORKING_SET_LIMIT_RANGE
;
675 switch (wine_mmap_is_in_reserved_area( base
, size
))
677 case -1: /* partially in a reserved area */
678 return STATUS_CONFLICTING_ADDRESSES
;
680 case 0: /* not in a reserved area, do a normal allocation */
681 if ((ptr
= wine_anon_mmap( base
, size
, VIRTUAL_GetUnixProt(vprot
), 0 )) == (void *)-1)
683 if (errno
== ENOMEM
) return STATUS_NO_MEMORY
;
684 return STATUS_INVALID_PARAMETER
;
688 /* We couldn't get the address we wanted */
689 if (is_beyond_limit( ptr
, size
, user_space_limit
)) add_reserved_area( ptr
, size
);
690 else munmap( ptr
, size
);
691 return STATUS_CONFLICTING_ADDRESSES
;
696 case 1: /* in a reserved area, make sure the address is available */
697 if (find_view_range( base
, size
)) return STATUS_CONFLICTING_ADDRESSES
;
698 /* replace the reserved area by our mapping */
699 if ((ptr
= wine_anon_mmap( base
, size
, VIRTUAL_GetUnixProt(vprot
), MAP_FIXED
)) != base
)
700 return STATUS_INVALID_PARAMETER
;
706 size_t view_size
= size
+ mask
+ 1;
707 struct alloc_area alloc
;
711 alloc
.top_down
= top_down
;
712 if (wine_mmap_enum_reserved_areas( alloc_reserved_area_callback
, &alloc
, top_down
))
715 TRACE( "got mem in reserved area %p-%p\n", ptr
, (char *)ptr
+ size
);
716 if (wine_anon_mmap( ptr
, size
, VIRTUAL_GetUnixProt(vprot
), MAP_FIXED
) != ptr
)
717 return STATUS_INVALID_PARAMETER
;
723 if ((ptr
= wine_anon_mmap( NULL
, view_size
, VIRTUAL_GetUnixProt(vprot
), 0 )) == (void *)-1)
725 if (errno
== ENOMEM
) return STATUS_NO_MEMORY
;
726 return STATUS_INVALID_PARAMETER
;
728 TRACE( "got mem with anon mmap %p-%p\n", ptr
, (char *)ptr
+ size
);
729 /* if we got something beyond the user limit, unmap it and retry */
730 if (is_beyond_limit( ptr
, view_size
, user_space_limit
)) add_reserved_area( ptr
, view_size
);
733 ptr
= unmap_extra_space( ptr
, view_size
, size
, mask
);
736 status
= create_view( view_ret
, ptr
, size
, vprot
);
737 if (status
!= STATUS_SUCCESS
) unmap_area( ptr
, size
);
742 /***********************************************************************
745 * Linux kernels before 2.4.x can support non page-aligned offsets, as
746 * long as the offset is aligned to the filesystem block size. This is
747 * a big performance gain so we want to take advantage of it.
749 * However, when we use 64-bit file support this doesn't work because
750 * glibc rejects unaligned offsets. Also glibc 2.1.3 mmap64 is broken
751 * in that it rounds unaligned offsets down to a page boundary. For
752 * these reasons we do a direct system call here.
754 static void *unaligned_mmap( void *addr
, size_t length
, unsigned int prot
,
755 unsigned int flags
, int fd
, off_t offset
)
757 #if defined(linux) && defined(__i386__) && defined(__GNUC__)
758 if (!(offset
>> 32) && (offset
& page_mask
))
773 args
.length
= length
;
777 args
.offset
= offset
;
779 __asm__
__volatile__("push %%ebx\n\t"
784 : "0" (90), /* SYS_mmap */
787 if (ret
< 0 && ret
> -4096)
795 return mmap( addr
, length
, prot
, flags
, fd
, offset
);
799 /***********************************************************************
802 * Wrapper for mmap() to map a file into a view, falling back to read if mmap fails.
803 * The csVirtual section must be held by caller.
805 static NTSTATUS
map_file_into_view( struct file_view
*view
, int fd
, size_t start
, size_t size
,
806 off_t offset
, BYTE vprot
, BOOL removable
)
809 int prot
= VIRTUAL_GetUnixProt( vprot
);
810 BOOL shared_write
= (vprot
& VPROT_WRITE
) != 0;
812 assert( start
< view
->size
);
813 assert( start
+ size
<= view
->size
);
815 /* only try mmap if media is not removable (or if we require write access) */
816 if (!removable
|| shared_write
)
818 int flags
= MAP_FIXED
| (shared_write
? MAP_SHARED
: MAP_PRIVATE
);
820 if (unaligned_mmap( (char *)view
->base
+ start
, size
, prot
, flags
, fd
, offset
) != (void *)-1)
823 /* mmap() failed; if this is because the file offset is not */
824 /* page-aligned (EINVAL), or because the underlying filesystem */
825 /* does not support mmap() (ENOEXEC,ENODEV), we do it by hand. */
826 if ((errno
!= ENOEXEC
) && (errno
!= EINVAL
) && (errno
!= ENODEV
)) return FILE_GetNtStatus();
827 if (shared_write
) return FILE_GetNtStatus(); /* we cannot fake shared write mappings */
830 /* Reserve the memory with an anonymous mmap */
831 ptr
= wine_anon_mmap( (char *)view
->base
+ start
, size
, PROT_READ
| PROT_WRITE
, MAP_FIXED
);
832 if (ptr
== (void *)-1) return FILE_GetNtStatus();
833 /* Now read in the file */
834 pread( fd
, ptr
, size
, offset
);
835 if (prot
!= (PROT_READ
|PROT_WRITE
)) mprotect( ptr
, size
, prot
); /* Set the right protection */
837 memset( view
->prot
+ (start
>> page_shift
), vprot
, ROUND_SIZE(start
,size
) >> page_shift
);
838 return STATUS_SUCCESS
;
842 /***********************************************************************
845 * Decommit some pages of a given view.
846 * The csVirtual section must be held by caller.
848 static NTSTATUS
decommit_pages( struct file_view
*view
, size_t start
, size_t size
)
850 if (wine_anon_mmap( (char *)view
->base
+ start
, size
, PROT_NONE
, MAP_FIXED
) != (void *)-1)
852 BYTE
*p
= view
->prot
+ (start
>> page_shift
);
854 while (size
--) *p
++ &= ~VPROT_COMMITTED
;
855 return STATUS_SUCCESS
;
857 return FILE_GetNtStatus();
861 /***********************************************************************
864 * Apply the relocations to a mapped PE image
866 static int do_relocations( char *base
, const IMAGE_DATA_DIRECTORY
*dir
,
867 int delta
, SIZE_T total_size
)
869 IMAGE_BASE_RELOCATION
*rel
;
871 TRACE_(module
)( "relocating from %p-%p to %p-%p\n",
872 base
- delta
, base
- delta
+ total_size
, base
, base
+ total_size
);
874 for (rel
= (IMAGE_BASE_RELOCATION
*)(base
+ dir
->VirtualAddress
);
875 ((char *)rel
< base
+ dir
->VirtualAddress
+ dir
->Size
) && rel
->SizeOfBlock
;
876 rel
= (IMAGE_BASE_RELOCATION
*)((char*)rel
+ rel
->SizeOfBlock
) )
878 char *page
= base
+ rel
->VirtualAddress
;
879 WORD
*TypeOffset
= (WORD
*)(rel
+ 1);
880 int i
, count
= (rel
->SizeOfBlock
- sizeof(*rel
)) / sizeof(*TypeOffset
);
882 if (!count
) continue;
885 if ((char *)rel
+ rel
->SizeOfBlock
> base
+ dir
->VirtualAddress
+ dir
->Size
)
887 ERR_(module
)("invalid relocation %p,%x,%d at %p,%x,%x\n",
888 rel
, rel
->VirtualAddress
, rel
->SizeOfBlock
,
889 base
, dir
->VirtualAddress
, dir
->Size
);
893 if (page
> base
+ total_size
)
895 WARN_(module
)("skipping %d relocations for page %p beyond module %p-%p\n",
896 count
, page
, base
, base
+ total_size
);
900 TRACE_(module
)("%d relocations for page %x\n", count
, rel
->VirtualAddress
);
902 /* patching in reverse order */
903 for (i
= 0 ; i
< count
; i
++)
905 int offset
= TypeOffset
[i
] & 0xFFF;
906 int type
= TypeOffset
[i
] >> 12;
909 case IMAGE_REL_BASED_ABSOLUTE
:
911 case IMAGE_REL_BASED_HIGH
:
912 *(short*)(page
+offset
) += HIWORD(delta
);
914 case IMAGE_REL_BASED_LOW
:
915 *(short*)(page
+offset
) += LOWORD(delta
);
917 case IMAGE_REL_BASED_HIGHLOW
:
918 *(int*)(page
+offset
) += delta
;
919 /* FIXME: if this is an exported address, fire up enhanced logic */
922 FIXME_(module
)("Unknown/unsupported fixup type %d.\n", type
);
931 /***********************************************************************
934 * Map an executable (PE format) image into memory.
936 static NTSTATUS
map_image( HANDLE hmapping
, int fd
, char *base
, SIZE_T total_size
, SIZE_T mask
,
937 SIZE_T header_size
, int shared_fd
, HANDLE dup_mapping
, PVOID
*addr_ptr
)
939 IMAGE_DOS_HEADER
*dos
;
940 IMAGE_NT_HEADERS
*nt
;
941 IMAGE_SECTION_HEADER
*sec
;
942 IMAGE_DATA_DIRECTORY
*imports
;
943 NTSTATUS status
= STATUS_CONFLICTING_ADDRESSES
;
948 struct file_view
*view
= NULL
;
949 char *ptr
, *header_end
;
951 /* zero-map the whole range */
953 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
955 if (base
>= (char *)0x110000) /* make sure the DOS area remains free */
956 status
= map_view( &view
, base
, total_size
, mask
, FALSE
,
957 VPROT_COMMITTED
| VPROT_READ
| VPROT_EXEC
| VPROT_WRITECOPY
| VPROT_IMAGE
);
959 if (status
== STATUS_CONFLICTING_ADDRESSES
)
960 status
= map_view( &view
, NULL
, total_size
, mask
, FALSE
,
961 VPROT_COMMITTED
| VPROT_READ
| VPROT_EXEC
| VPROT_WRITECOPY
| VPROT_IMAGE
);
963 if (status
!= STATUS_SUCCESS
) goto error
;
966 TRACE_(module
)( "mapped PE file at %p-%p\n", ptr
, ptr
+ total_size
);
970 if (fstat( fd
, &st
) == -1)
972 status
= FILE_GetNtStatus();
975 status
= STATUS_INVALID_IMAGE_FORMAT
; /* generic error */
976 if (!st
.st_size
) goto error
;
977 header_size
= min( header_size
, st
.st_size
);
978 if (map_file_into_view( view
, fd
, 0, header_size
, 0, VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
,
979 !dup_mapping
) != STATUS_SUCCESS
) goto error
;
980 dos
= (IMAGE_DOS_HEADER
*)ptr
;
981 nt
= (IMAGE_NT_HEADERS
*)(ptr
+ dos
->e_lfanew
);
982 header_end
= ptr
+ ROUND_SIZE( 0, header_size
);
983 memset( ptr
+ header_size
, 0, header_end
- (ptr
+ header_size
) );
984 if ((char *)(nt
+ 1) > header_end
) goto error
;
985 sec
= (IMAGE_SECTION_HEADER
*)((char*)&nt
->OptionalHeader
+nt
->FileHeader
.SizeOfOptionalHeader
);
986 if ((char *)(sec
+ nt
->FileHeader
.NumberOfSections
) > header_end
) goto error
;
988 imports
= nt
->OptionalHeader
.DataDirectory
+ IMAGE_DIRECTORY_ENTRY_IMPORT
;
989 if (!imports
->Size
|| !imports
->VirtualAddress
) imports
= NULL
;
991 /* check the architecture */
993 if (nt
->FileHeader
.Machine
!= IMAGE_FILE_MACHINE_I386
)
995 MESSAGE("Trying to load PE image for unsupported architecture (");
996 switch (nt
->FileHeader
.Machine
)
998 case IMAGE_FILE_MACHINE_UNKNOWN
: MESSAGE("Unknown"); break;
999 case IMAGE_FILE_MACHINE_I860
: MESSAGE("I860"); break;
1000 case IMAGE_FILE_MACHINE_R3000
: MESSAGE("R3000"); break;
1001 case IMAGE_FILE_MACHINE_R4000
: MESSAGE("R4000"); break;
1002 case IMAGE_FILE_MACHINE_R10000
: MESSAGE("R10000"); break;
1003 case IMAGE_FILE_MACHINE_ALPHA
: MESSAGE("Alpha"); break;
1004 case IMAGE_FILE_MACHINE_POWERPC
: MESSAGE("PowerPC"); break;
1005 case IMAGE_FILE_MACHINE_IA64
: MESSAGE("IA-64"); break;
1006 case IMAGE_FILE_MACHINE_ALPHA64
: MESSAGE("Alpha-64"); break;
1007 case IMAGE_FILE_MACHINE_AMD64
: MESSAGE("AMD-64"); break;
1008 case IMAGE_FILE_MACHINE_ARM
: MESSAGE("ARM"); break;
1009 default: MESSAGE("Unknown-%04x", nt
->FileHeader
.Machine
); break;
1015 /* check for non page-aligned binary */
1017 if (nt
->OptionalHeader
.SectionAlignment
<= page_mask
)
1019 /* unaligned sections, this happens for native subsystem binaries */
1020 /* in that case Windows simply maps in the whole file */
1022 if (map_file_into_view( view
, fd
, 0, total_size
, 0, VPROT_COMMITTED
| VPROT_READ
,
1023 !dup_mapping
) != STATUS_SUCCESS
) goto error
;
1025 /* check that all sections are loaded at the right offset */
1026 if (nt
->OptionalHeader
.FileAlignment
!= nt
->OptionalHeader
.SectionAlignment
) goto error
;
1027 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++)
1029 if (sec
[i
].VirtualAddress
!= sec
[i
].PointerToRawData
)
1030 goto error
; /* Windows refuses to load in that case too */
1033 /* set the image protections */
1034 VIRTUAL_SetProt( view
, ptr
, total_size
,
1035 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
| VPROT_EXEC
);
1037 /* perform relocations if necessary */
1038 /* FIXME: not 100% compatible, Windows doesn't do this for non page-aligned binaries */
1041 const IMAGE_DATA_DIRECTORY
*relocs
;
1042 relocs
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1043 if (relocs
->VirtualAddress
&& relocs
->Size
)
1044 do_relocations( ptr
, relocs
, ptr
- base
, total_size
);
1051 /* map all the sections */
1053 for (i
= pos
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
1055 static const SIZE_T sector_align
= 0x1ff;
1056 SIZE_T map_size
, file_start
, file_size
, end
;
1058 if (!sec
->Misc
.VirtualSize
)
1059 map_size
= ROUND_SIZE( 0, sec
->SizeOfRawData
);
1061 map_size
= ROUND_SIZE( 0, sec
->Misc
.VirtualSize
);
1063 /* file positions are rounded to sector boundaries regardless of OptionalHeader.FileAlignment */
1064 file_start
= sec
->PointerToRawData
& ~sector_align
;
1065 file_size
= (sec
->SizeOfRawData
+ (sec
->PointerToRawData
& sector_align
) + sector_align
) & ~sector_align
;
1066 if (file_size
> map_size
) file_size
= map_size
;
1068 /* a few sanity checks */
1069 end
= sec
->VirtualAddress
+ ROUND_SIZE( sec
->VirtualAddress
, map_size
);
1070 if (sec
->VirtualAddress
> total_size
|| end
> total_size
|| end
< sec
->VirtualAddress
)
1072 WARN_(module
)( "Section %.8s too large (%x+%lx/%lx)\n",
1073 sec
->Name
, sec
->VirtualAddress
, map_size
, total_size
);
1077 if ((sec
->Characteristics
& IMAGE_SCN_MEM_SHARED
) &&
1078 (sec
->Characteristics
& IMAGE_SCN_MEM_WRITE
))
1080 TRACE_(module
)( "mapping shared section %.8s at %p off %x (%x) size %lx (%lx) flags %x\n",
1081 sec
->Name
, ptr
+ sec
->VirtualAddress
,
1082 sec
->PointerToRawData
, (int)pos
, file_size
, map_size
,
1083 sec
->Characteristics
);
1084 if (map_file_into_view( view
, shared_fd
, sec
->VirtualAddress
, map_size
, pos
,
1085 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITE
,
1086 FALSE
) != STATUS_SUCCESS
)
1088 ERR_(module
)( "Could not map shared section %.8s\n", sec
->Name
);
1092 /* check if the import directory falls inside this section */
1093 if (imports
&& imports
->VirtualAddress
>= sec
->VirtualAddress
&&
1094 imports
->VirtualAddress
< sec
->VirtualAddress
+ map_size
)
1096 UINT_PTR base
= imports
->VirtualAddress
& ~page_mask
;
1097 UINT_PTR end
= base
+ ROUND_SIZE( imports
->VirtualAddress
, imports
->Size
);
1098 if (end
> sec
->VirtualAddress
+ map_size
) end
= sec
->VirtualAddress
+ map_size
;
1100 map_file_into_view( view
, shared_fd
, base
, end
- base
,
1101 pos
+ (base
- sec
->VirtualAddress
),
1102 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
,
1109 TRACE_(module
)( "mapping section %.8s at %p off %x size %x virt %x flags %x\n",
1110 sec
->Name
, ptr
+ sec
->VirtualAddress
,
1111 sec
->PointerToRawData
, sec
->SizeOfRawData
,
1112 sec
->Misc
.VirtualSize
, sec
->Characteristics
);
1114 if (!sec
->PointerToRawData
|| !file_size
) continue;
1116 /* Note: if the section is not aligned properly map_file_into_view will magically
1117 * fall back to read(), so we don't need to check anything here.
1119 end
= file_start
+ file_size
;
1120 if (sec
->PointerToRawData
>= st
.st_size
||
1121 end
> ((st
.st_size
+ sector_align
) & ~sector_align
) ||
1123 map_file_into_view( view
, fd
, sec
->VirtualAddress
, file_size
, file_start
,
1124 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
,
1125 !dup_mapping
) != STATUS_SUCCESS
)
1127 ERR_(module
)( "Could not map section %.8s, file probably truncated\n", sec
->Name
);
1131 if (file_size
& page_mask
)
1133 end
= ROUND_SIZE( 0, file_size
);
1134 if (end
> map_size
) end
= map_size
;
1135 TRACE_(module
)("clearing %p - %p\n",
1136 ptr
+ sec
->VirtualAddress
+ file_size
,
1137 ptr
+ sec
->VirtualAddress
+ end
);
1138 memset( ptr
+ sec
->VirtualAddress
+ file_size
, 0, end
- file_size
);
1143 /* perform base relocation, if necessary */
1147 const IMAGE_DATA_DIRECTORY
*relocs
;
1149 relocs
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1150 if (nt
->FileHeader
.Characteristics
& IMAGE_FILE_RELOCS_STRIPPED
)
1152 WARN( "Need to relocate module from addr %lx, but there are no relocation records\n",
1153 (ULONG_PTR
)nt
->OptionalHeader
.ImageBase
);
1154 status
= STATUS_CONFLICTING_ADDRESSES
;
1158 /* FIXME: If we need to relocate a system DLL (base > 2GB) we should
1159 * really make sure that the *new* base address is also > 2GB.
1160 * Some DLLs really check the MSB of the module handle :-/
1162 if ((nt
->OptionalHeader
.ImageBase
& 0x80000000) && !((ULONG_PTR
)base
& 0x80000000))
1163 ERR( "Forced to relocate system DLL (base > 2GB). This is not good.\n" );
1165 if (!do_relocations( ptr
, relocs
, ptr
- base
, total_size
))
1171 /* set the image protections */
1173 VIRTUAL_SetProt( view
, ptr
, ROUND_SIZE( 0, header_size
), VPROT_COMMITTED
| VPROT_READ
);
1175 sec
= (IMAGE_SECTION_HEADER
*)((char *)&nt
->OptionalHeader
+nt
->FileHeader
.SizeOfOptionalHeader
);
1176 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
1179 BYTE vprot
= VPROT_COMMITTED
;
1181 if (sec
->Misc
.VirtualSize
)
1182 size
= ROUND_SIZE( sec
->VirtualAddress
, sec
->Misc
.VirtualSize
);
1184 size
= ROUND_SIZE( sec
->VirtualAddress
, sec
->SizeOfRawData
);
1186 if (sec
->Characteristics
& IMAGE_SCN_MEM_READ
) vprot
|= VPROT_READ
;
1187 if (sec
->Characteristics
& IMAGE_SCN_MEM_WRITE
) vprot
|= VPROT_READ
|VPROT_WRITECOPY
;
1188 if (sec
->Characteristics
& IMAGE_SCN_MEM_EXECUTE
) vprot
|= VPROT_EXEC
;
1190 /* Dumb game crack lets the AOEP point into a data section. Adjust. */
1191 if ((nt
->OptionalHeader
.AddressOfEntryPoint
>= sec
->VirtualAddress
) &&
1192 (nt
->OptionalHeader
.AddressOfEntryPoint
< sec
->VirtualAddress
+ size
))
1193 vprot
|= VPROT_EXEC
;
1195 VIRTUAL_SetProt( view
, ptr
+ sec
->VirtualAddress
, size
, vprot
);
1199 view
->mapping
= dup_mapping
;
1200 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1203 return STATUS_SUCCESS
;
1206 if (view
) delete_view( view
);
1207 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1208 if (dup_mapping
) NtClose( dup_mapping
);
1213 /***********************************************************************
1216 void virtual_init(void)
1218 const char *preload
;
1220 page_size
= getpagesize();
1221 page_mask
= page_size
- 1;
1222 /* Make sure we have a power of 2 */
1223 assert( !(page_size
& page_mask
) );
1225 while ((1 << page_shift
) != page_size
) page_shift
++;
1226 #endif /* page_mask */
1227 if ((preload
= getenv("WINEPRELOADRESERVE")))
1229 unsigned long start
, end
;
1230 if (sscanf( preload
, "%lx-%lx", &start
, &end
) == 2)
1232 preload_reserve_start
= (void *)start
;
1233 preload_reserve_end
= (void *)end
;
1239 /***********************************************************************
1240 * virtual_init_threading
1242 void virtual_init_threading(void)
1248 /***********************************************************************
1249 * VIRTUAL_HandleFault
1251 NTSTATUS
VIRTUAL_HandleFault( LPCVOID addr
)
1254 NTSTATUS ret
= STATUS_ACCESS_VIOLATION
;
1257 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1258 if ((view
= VIRTUAL_FindView( addr
)))
1260 void *page
= ROUND_ADDR( addr
, page_mask
);
1261 BYTE vprot
= view
->prot
[((const char *)page
- (const char *)view
->base
) >> page_shift
];
1262 if (vprot
& VPROT_GUARD
)
1264 VIRTUAL_SetProt( view
, page
, page_size
, vprot
& ~VPROT_GUARD
);
1265 ret
= STATUS_GUARD_PAGE_VIOLATION
;
1268 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1273 /***********************************************************************
1274 * VIRTUAL_SetForceExec
1276 * Whether to force exec prot on all views.
1278 void VIRTUAL_SetForceExec( BOOL enable
)
1280 struct file_view
*view
;
1283 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1284 if (!force_exec_prot
!= !enable
) /* change all existing views */
1286 force_exec_prot
= enable
;
1288 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
1292 char *addr
= view
->base
;
1293 BYTE prot
= view
->prot
[0];
1295 for (count
= i
= 1; i
< view
->size
>> page_shift
; i
++, count
++)
1297 if (view
->prot
[i
] == prot
) continue;
1298 unix_prot
= VIRTUAL_GetUnixProt( prot
);
1299 if ((unix_prot
& PROT_READ
) && !(unix_prot
& PROT_EXEC
))
1301 TRACE( "%s exec prot for %p-%p\n",
1302 force_exec_prot
? "enabling" : "disabling",
1303 addr
, addr
+ (count
<< page_shift
) - 1 );
1304 mprotect( addr
, count
<< page_shift
,
1305 unix_prot
| (force_exec_prot
? PROT_EXEC
: 0) );
1307 addr
+= (count
<< page_shift
);
1308 prot
= view
->prot
[i
];
1313 unix_prot
= VIRTUAL_GetUnixProt( prot
);
1314 if ((unix_prot
& PROT_READ
) && !(unix_prot
& PROT_EXEC
))
1316 TRACE( "%s exec prot for %p-%p\n",
1317 force_exec_prot
? "enabling" : "disabling",
1318 addr
, addr
+ (count
<< page_shift
) - 1 );
1319 mprotect( addr
, count
<< page_shift
,
1320 unix_prot
| (force_exec_prot
? PROT_EXEC
: 0) );
1325 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1329 /***********************************************************************
1330 * VIRTUAL_UseLargeAddressSpace
1332 * Increase the address space size for apps that support it.
1334 void VIRTUAL_UseLargeAddressSpace(void)
1336 /* no large address space on win9x */
1337 if (NtCurrentTeb()->Peb
->OSPlatformId
!= VER_PLATFORM_WIN32_NT
) return;
1338 user_space_limit
= ADDRESS_SPACE_LIMIT
;
1342 /***********************************************************************
1343 * NtAllocateVirtualMemory (NTDLL.@)
1344 * ZwAllocateVirtualMemory (NTDLL.@)
1346 NTSTATUS WINAPI
NtAllocateVirtualMemory( HANDLE process
, PVOID
*ret
, ULONG zero_bits
,
1347 SIZE_T
*size_ptr
, ULONG type
, ULONG protect
)
1351 SIZE_T size
= *size_ptr
;
1352 SIZE_T mask
= get_mask( zero_bits
);
1353 NTSTATUS status
= STATUS_SUCCESS
;
1354 struct file_view
*view
;
1357 TRACE("%p %p %08lx %x %08x\n", process
, *ret
, size
, type
, protect
);
1359 if (!size
) return STATUS_INVALID_PARAMETER
;
1361 if (process
!= NtCurrentProcess())
1364 apc_result_t result
;
1366 memset( &call
, 0, sizeof(call
) );
1368 call
.virtual_alloc
.type
= APC_VIRTUAL_ALLOC
;
1369 call
.virtual_alloc
.addr
= *ret
;
1370 call
.virtual_alloc
.size
= *size_ptr
;
1371 call
.virtual_alloc
.zero_bits
= zero_bits
;
1372 call
.virtual_alloc
.op_type
= type
;
1373 call
.virtual_alloc
.prot
= protect
;
1374 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
1375 if (status
!= STATUS_SUCCESS
) return status
;
1377 if (result
.virtual_alloc
.status
== STATUS_SUCCESS
)
1379 *ret
= result
.virtual_alloc
.addr
;
1380 *size_ptr
= result
.virtual_alloc
.size
;
1382 return result
.virtual_alloc
.status
;
1385 /* Round parameters to a page boundary */
1387 if (size
> 0x7fc00000) return STATUS_WORKING_SET_LIMIT_RANGE
; /* 2Gb - 4Mb */
1391 if (type
& MEM_RESERVE
) /* Round down to 64k boundary */
1392 base
= ROUND_ADDR( *ret
, mask
);
1394 base
= ROUND_ADDR( *ret
, page_mask
);
1395 size
= (((UINT_PTR
)*ret
+ size
+ page_mask
) & ~page_mask
) - (UINT_PTR
)base
;
1397 /* disallow low 64k, wrap-around and kernel space */
1398 if (((char *)base
< (char *)0x10000) ||
1399 ((char *)base
+ size
< (char *)base
) ||
1400 is_beyond_limit( base
, size
, ADDRESS_SPACE_LIMIT
))
1401 return STATUS_INVALID_PARAMETER
;
1406 size
= (size
+ page_mask
) & ~page_mask
;
1409 /* Compute the alloc type flags */
1411 if (!(type
& MEM_SYSTEM
))
1413 if (!(type
& (MEM_COMMIT
| MEM_RESERVE
)) ||
1414 (type
& ~(MEM_COMMIT
| MEM_RESERVE
| MEM_TOP_DOWN
| MEM_WRITE_WATCH
| MEM_RESET
)))
1416 WARN("called with wrong alloc type flags (%08x) !\n", type
);
1417 return STATUS_INVALID_PARAMETER
;
1419 if (type
& MEM_WRITE_WATCH
)
1421 FIXME("MEM_WRITE_WATCH type not supported\n");
1422 return STATUS_NOT_SUPPORTED
;
1425 vprot
= VIRTUAL_GetProt( protect
);
1426 if (type
& MEM_COMMIT
) vprot
|= VPROT_COMMITTED
;
1428 /* Reserve the memory */
1430 if (use_locks
) server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1432 if (type
& MEM_SYSTEM
)
1434 if (type
& MEM_IMAGE
) vprot
|= VPROT_IMAGE
;
1435 status
= create_view( &view
, base
, size
, vprot
| VPROT_COMMITTED
);
1436 if (status
== STATUS_SUCCESS
)
1438 view
->flags
|= VFLAG_VALLOC
| VFLAG_SYSTEM
;
1442 else if ((type
& MEM_RESERVE
) || !base
)
1444 status
= map_view( &view
, base
, size
, mask
, type
& MEM_TOP_DOWN
, vprot
);
1445 if (status
== STATUS_SUCCESS
)
1447 view
->flags
|= VFLAG_VALLOC
;
1451 else /* commit the pages */
1453 if (!(view
= VIRTUAL_FindView( base
)) ||
1454 ((char *)base
+ size
> (char *)view
->base
+ view
->size
)) status
= STATUS_NOT_MAPPED_VIEW
;
1455 else if (!VIRTUAL_SetProt( view
, base
, size
, vprot
)) status
= STATUS_ACCESS_DENIED
;
1458 if (use_locks
) server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1460 if (status
== STATUS_SUCCESS
)
1469 /***********************************************************************
1470 * NtFreeVirtualMemory (NTDLL.@)
1471 * ZwFreeVirtualMemory (NTDLL.@)
1473 NTSTATUS WINAPI
NtFreeVirtualMemory( HANDLE process
, PVOID
*addr_ptr
, SIZE_T
*size_ptr
, ULONG type
)
1478 NTSTATUS status
= STATUS_SUCCESS
;
1479 LPVOID addr
= *addr_ptr
;
1480 SIZE_T size
= *size_ptr
;
1482 TRACE("%p %p %08lx %x\n", process
, addr
, size
, type
);
1484 if (process
!= NtCurrentProcess())
1487 apc_result_t result
;
1489 memset( &call
, 0, sizeof(call
) );
1491 call
.virtual_free
.type
= APC_VIRTUAL_FREE
;
1492 call
.virtual_free
.addr
= addr
;
1493 call
.virtual_free
.size
= size
;
1494 call
.virtual_free
.op_type
= type
;
1495 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
1496 if (status
!= STATUS_SUCCESS
) return status
;
1498 if (result
.virtual_free
.status
== STATUS_SUCCESS
)
1500 *addr_ptr
= result
.virtual_free
.addr
;
1501 *size_ptr
= result
.virtual_free
.size
;
1503 return result
.virtual_free
.status
;
1506 /* Fix the parameters */
1508 size
= ROUND_SIZE( addr
, size
);
1509 base
= ROUND_ADDR( addr
, page_mask
);
1511 /* avoid freeing the DOS area when a broken app passes a NULL pointer */
1512 if (!base
&& !(type
& MEM_SYSTEM
)) return STATUS_INVALID_PARAMETER
;
1514 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1516 if (!(view
= VIRTUAL_FindView( base
)) ||
1517 (base
+ size
> (char *)view
->base
+ view
->size
) ||
1518 !(view
->flags
& VFLAG_VALLOC
))
1520 status
= STATUS_INVALID_PARAMETER
;
1522 else if (type
& MEM_SYSTEM
)
1524 /* return the values that the caller should use to unmap the area */
1525 *addr_ptr
= view
->base
;
1526 if (!wine_mmap_is_in_reserved_area( view
->base
, view
->size
)) *size_ptr
= view
->size
;
1527 else *size_ptr
= 0; /* make sure we don't munmap anything from a reserved area */
1528 view
->flags
|= VFLAG_SYSTEM
;
1529 delete_view( view
);
1531 else if (type
== MEM_RELEASE
)
1533 /* Free the pages */
1535 if (size
|| (base
!= view
->base
)) status
= STATUS_INVALID_PARAMETER
;
1538 delete_view( view
);
1543 else if (type
== MEM_DECOMMIT
)
1545 status
= decommit_pages( view
, base
- (char *)view
->base
, size
);
1546 if (status
== STATUS_SUCCESS
)
1554 WARN("called with wrong free type flags (%08x) !\n", type
);
1555 status
= STATUS_INVALID_PARAMETER
;
1558 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1563 /***********************************************************************
1564 * NtProtectVirtualMemory (NTDLL.@)
1565 * ZwProtectVirtualMemory (NTDLL.@)
1567 NTSTATUS WINAPI
NtProtectVirtualMemory( HANDLE process
, PVOID
*addr_ptr
, SIZE_T
*size_ptr
,
1568 ULONG new_prot
, ULONG
*old_prot
)
1572 NTSTATUS status
= STATUS_SUCCESS
;
1577 SIZE_T size
= *size_ptr
;
1578 LPVOID addr
= *addr_ptr
;
1580 TRACE("%p %p %08lx %08x\n", process
, addr
, size
, new_prot
);
1582 if (process
!= NtCurrentProcess())
1585 apc_result_t result
;
1587 memset( &call
, 0, sizeof(call
) );
1589 call
.virtual_protect
.type
= APC_VIRTUAL_PROTECT
;
1590 call
.virtual_protect
.addr
= addr
;
1591 call
.virtual_protect
.size
= size
;
1592 call
.virtual_protect
.prot
= new_prot
;
1593 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
1594 if (status
!= STATUS_SUCCESS
) return status
;
1596 if (result
.virtual_protect
.status
== STATUS_SUCCESS
)
1598 *addr_ptr
= result
.virtual_protect
.addr
;
1599 *size_ptr
= result
.virtual_protect
.size
;
1600 if (old_prot
) *old_prot
= result
.virtual_protect
.prot
;
1602 return result
.virtual_protect
.status
;
1605 /* Fix the parameters */
1607 size
= ROUND_SIZE( addr
, size
);
1608 base
= ROUND_ADDR( addr
, page_mask
);
1610 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1612 if (!(view
= VIRTUAL_FindView( base
)) || (base
+ size
> (char *)view
->base
+ view
->size
))
1614 status
= STATUS_INVALID_PARAMETER
;
1618 /* Make sure all the pages are committed */
1620 p
= view
->prot
+ ((base
- (char *)view
->base
) >> page_shift
);
1621 prot
= VIRTUAL_GetWin32Prot( *p
);
1622 for (i
= size
>> page_shift
; i
; i
--, p
++)
1624 if (!(*p
& VPROT_COMMITTED
))
1626 status
= STATUS_NOT_COMMITTED
;
1632 if (old_prot
) *old_prot
= prot
;
1633 vprot
= VIRTUAL_GetProt( new_prot
) | VPROT_COMMITTED
;
1634 if (!VIRTUAL_SetProt( view
, base
, size
, vprot
)) status
= STATUS_ACCESS_DENIED
;
1637 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1639 if (status
== STATUS_SUCCESS
)
1647 #define UNIMPLEMENTED_INFO_CLASS(c) \
1649 FIXME("(process=%p,addr=%p) Unimplemented information class: " #c "\n", process, addr); \
1650 return STATUS_INVALID_INFO_CLASS
1652 /***********************************************************************
1653 * NtQueryVirtualMemory (NTDLL.@)
1654 * ZwQueryVirtualMemory (NTDLL.@)
1656 NTSTATUS WINAPI
NtQueryVirtualMemory( HANDLE process
, LPCVOID addr
,
1657 MEMORY_INFORMATION_CLASS info_class
, PVOID buffer
,
1658 SIZE_T len
, SIZE_T
*res_len
)
1661 char *base
, *alloc_base
= 0;
1664 MEMORY_BASIC_INFORMATION
*info
= buffer
;
1667 if (info_class
!= MemoryBasicInformation
)
1671 UNIMPLEMENTED_INFO_CLASS(MemoryWorkingSetList
);
1672 UNIMPLEMENTED_INFO_CLASS(MemorySectionName
);
1673 UNIMPLEMENTED_INFO_CLASS(MemoryBasicVlmInformation
);
1676 FIXME("(%p,%p,info_class=%d,%p,%ld,%p) Unknown information class\n",
1677 process
, addr
, info_class
, buffer
, len
, res_len
);
1678 return STATUS_INVALID_INFO_CLASS
;
1681 if (ADDRESS_SPACE_LIMIT
&& addr
>= ADDRESS_SPACE_LIMIT
)
1682 return STATUS_WORKING_SET_LIMIT_RANGE
;
1684 if (process
!= NtCurrentProcess())
1688 apc_result_t result
;
1690 memset( &call
, 0, sizeof(call
) );
1692 call
.virtual_query
.type
= APC_VIRTUAL_QUERY
;
1693 call
.virtual_query
.addr
= addr
;
1694 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
1695 if (status
!= STATUS_SUCCESS
) return status
;
1697 if (result
.virtual_query
.status
== STATUS_SUCCESS
)
1699 info
->BaseAddress
= result
.virtual_query
.base
;
1700 info
->AllocationBase
= result
.virtual_query
.alloc_base
;
1701 info
->RegionSize
= result
.virtual_query
.size
;
1702 info
->State
= result
.virtual_query
.state
;
1703 info
->Protect
= result
.virtual_query
.prot
;
1704 info
->AllocationProtect
= result
.virtual_query
.alloc_prot
;
1705 info
->Type
= result
.virtual_query
.alloc_type
;
1706 if (res_len
) *res_len
= sizeof(*info
);
1708 return result
.virtual_query
.status
;
1711 base
= ROUND_ADDR( addr
, page_mask
);
1713 /* Find the view containing the address */
1715 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1716 ptr
= list_head( &views_list
);
1721 /* make the address space end at the user limit, except if
1722 * the last view was mapped beyond that */
1723 if (alloc_base
<= (char *)user_space_limit
)
1725 if (user_space_limit
&& base
>= (char *)user_space_limit
)
1727 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1728 return STATUS_WORKING_SET_LIMIT_RANGE
;
1730 size
= (char *)user_space_limit
- alloc_base
;
1732 else size
= (char *)ADDRESS_SPACE_LIMIT
- alloc_base
;
1736 view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
1737 if ((char *)view
->base
> base
)
1739 size
= (char *)view
->base
- alloc_base
;
1743 if ((char *)view
->base
+ view
->size
> base
)
1745 alloc_base
= view
->base
;
1749 alloc_base
= (char *)view
->base
+ view
->size
;
1750 ptr
= list_next( &views_list
, ptr
);
1753 /* Fill the info structure */
1757 info
->State
= MEM_FREE
;
1758 info
->Protect
= PAGE_NOACCESS
;
1759 info
->AllocationBase
= 0;
1760 info
->AllocationProtect
= 0;
1765 BYTE vprot
= view
->prot
[(base
- alloc_base
) >> page_shift
];
1766 info
->State
= (vprot
& VPROT_COMMITTED
) ? MEM_COMMIT
: MEM_RESERVE
;
1767 info
->Protect
= VIRTUAL_GetWin32Prot( vprot
);
1768 info
->AllocationBase
= alloc_base
;
1769 info
->AllocationProtect
= VIRTUAL_GetWin32Prot( view
->protect
);
1770 if (view
->protect
& VPROT_IMAGE
) info
->Type
= MEM_IMAGE
;
1771 else if (view
->flags
& VFLAG_VALLOC
) info
->Type
= MEM_PRIVATE
;
1772 else info
->Type
= MEM_MAPPED
;
1773 for (size
= base
- alloc_base
; size
< view
->size
; size
+= page_size
)
1774 if (view
->prot
[size
>> page_shift
] != vprot
) break;
1776 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1778 info
->BaseAddress
= base
;
1779 info
->RegionSize
= size
- (base
- alloc_base
);
1780 if (res_len
) *res_len
= sizeof(*info
);
1781 return STATUS_SUCCESS
;
1785 /***********************************************************************
1786 * NtLockVirtualMemory (NTDLL.@)
1787 * ZwLockVirtualMemory (NTDLL.@)
1789 NTSTATUS WINAPI
NtLockVirtualMemory( HANDLE process
, PVOID
*addr
, SIZE_T
*size
, ULONG unknown
)
1791 NTSTATUS status
= STATUS_SUCCESS
;
1793 if (process
!= NtCurrentProcess())
1796 apc_result_t result
;
1798 memset( &call
, 0, sizeof(call
) );
1800 call
.virtual_lock
.type
= APC_VIRTUAL_LOCK
;
1801 call
.virtual_lock
.addr
= *addr
;
1802 call
.virtual_lock
.size
= *size
;
1803 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
1804 if (status
!= STATUS_SUCCESS
) return status
;
1806 if (result
.virtual_lock
.status
== STATUS_SUCCESS
)
1808 *addr
= result
.virtual_lock
.addr
;
1809 *size
= result
.virtual_lock
.size
;
1811 return result
.virtual_lock
.status
;
1814 *size
= ROUND_SIZE( *addr
, *size
);
1815 *addr
= ROUND_ADDR( *addr
, page_mask
);
1817 if (mlock( *addr
, *size
)) status
= STATUS_ACCESS_DENIED
;
1822 /***********************************************************************
1823 * NtUnlockVirtualMemory (NTDLL.@)
1824 * ZwUnlockVirtualMemory (NTDLL.@)
1826 NTSTATUS WINAPI
NtUnlockVirtualMemory( HANDLE process
, PVOID
*addr
, SIZE_T
*size
, ULONG unknown
)
1828 NTSTATUS status
= STATUS_SUCCESS
;
1830 if (process
!= NtCurrentProcess())
1833 apc_result_t result
;
1835 memset( &call
, 0, sizeof(call
) );
1837 call
.virtual_unlock
.type
= APC_VIRTUAL_UNLOCK
;
1838 call
.virtual_unlock
.addr
= *addr
;
1839 call
.virtual_unlock
.size
= *size
;
1840 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
1841 if (status
!= STATUS_SUCCESS
) return status
;
1843 if (result
.virtual_unlock
.status
== STATUS_SUCCESS
)
1845 *addr
= result
.virtual_unlock
.addr
;
1846 *size
= result
.virtual_unlock
.size
;
1848 return result
.virtual_unlock
.status
;
1851 *size
= ROUND_SIZE( *addr
, *size
);
1852 *addr
= ROUND_ADDR( *addr
, page_mask
);
1854 if (munlock( *addr
, *size
)) status
= STATUS_ACCESS_DENIED
;
1859 /***********************************************************************
1860 * NtCreateSection (NTDLL.@)
1861 * ZwCreateSection (NTDLL.@)
1863 NTSTATUS WINAPI
NtCreateSection( HANDLE
*handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
,
1864 const LARGE_INTEGER
*size
, ULONG protect
,
1865 ULONG sec_flags
, HANDLE file
)
1869 DWORD len
= (attr
&& attr
->ObjectName
) ? attr
->ObjectName
->Length
: 0;
1870 struct security_descriptor
*sd
= NULL
;
1871 struct object_attributes objattr
;
1873 /* Check parameters */
1875 if (len
> MAX_PATH
*sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
1877 objattr
.rootdir
= attr
? attr
->RootDirectory
: 0;
1879 objattr
.name_len
= len
;
1882 ret
= NTDLL_create_struct_sd( attr
->SecurityDescriptor
, &sd
, &objattr
.sd_len
);
1883 if (ret
!= STATUS_SUCCESS
) return ret
;
1886 vprot
= VIRTUAL_GetProt( protect
);
1887 if (sec_flags
& SEC_RESERVE
)
1889 if (file
) return STATUS_INVALID_PARAMETER
;
1891 else vprot
|= VPROT_COMMITTED
;
1892 if (sec_flags
& SEC_NOCACHE
) vprot
|= VPROT_NOCACHE
;
1893 if (sec_flags
& SEC_IMAGE
) vprot
|= VPROT_IMAGE
;
1895 /* Create the server object */
1897 SERVER_START_REQ( create_mapping
)
1899 req
->access
= access
;
1900 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
1901 req
->file_handle
= file
;
1902 req
->size
= size
? size
->QuadPart
: 0;
1903 req
->protect
= vprot
;
1904 wine_server_add_data( req
, &objattr
, sizeof(objattr
) );
1905 if (objattr
.sd_len
) wine_server_add_data( req
, sd
, objattr
.sd_len
);
1906 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
1907 ret
= wine_server_call( req
);
1908 *handle
= reply
->handle
;
1912 NTDLL_free_struct_sd( sd
);
1918 /***********************************************************************
1919 * NtOpenSection (NTDLL.@)
1920 * ZwOpenSection (NTDLL.@)
1922 NTSTATUS WINAPI
NtOpenSection( HANDLE
*handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
)
1925 DWORD len
= attr
->ObjectName
->Length
;
1927 if (len
> MAX_PATH
*sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
1929 SERVER_START_REQ( open_mapping
)
1931 req
->access
= access
;
1932 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
1933 req
->rootdir
= attr
? attr
->RootDirectory
: 0;
1934 wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
1935 if (!(ret
= wine_server_call( req
))) *handle
= reply
->handle
;
1942 /***********************************************************************
1943 * NtMapViewOfSection (NTDLL.@)
1944 * ZwMapViewOfSection (NTDLL.@)
1946 NTSTATUS WINAPI
NtMapViewOfSection( HANDLE handle
, HANDLE process
, PVOID
*addr_ptr
, ULONG zero_bits
,
1947 SIZE_T commit_size
, const LARGE_INTEGER
*offset_ptr
, SIZE_T
*size_ptr
,
1948 SECTION_INHERIT inherit
, ULONG alloc_type
, ULONG protect
)
1951 ULONGLONG full_size
;
1953 SIZE_T mask
= get_mask( zero_bits
);
1954 int unix_handle
= -1, needs_close
;
1957 struct file_view
*view
;
1959 HANDLE dup_mapping
, shared_file
;
1960 LARGE_INTEGER offset
;
1963 offset
.QuadPart
= offset_ptr
? offset_ptr
->QuadPart
: 0;
1965 TRACE("handle=%p process=%p addr=%p off=%x%08x size=%lx access=%x\n",
1966 handle
, process
, *addr_ptr
, offset
.u
.HighPart
, offset
.u
.LowPart
, size
, protect
);
1968 /* Check parameters */
1970 if ((offset
.u
.LowPart
& mask
) || (*addr_ptr
&& ((UINT_PTR
)*addr_ptr
& mask
)))
1971 return STATUS_INVALID_PARAMETER
;
1973 if (process
!= NtCurrentProcess())
1976 apc_result_t result
;
1978 memset( &call
, 0, sizeof(call
) );
1980 call
.map_view
.type
= APC_MAP_VIEW
;
1981 call
.map_view
.handle
= handle
;
1982 call
.map_view
.addr
= *addr_ptr
;
1983 call
.map_view
.size
= *size_ptr
;
1984 call
.map_view
.offset
= offset
.QuadPart
;
1985 call
.map_view
.zero_bits
= zero_bits
;
1986 call
.map_view
.alloc_type
= alloc_type
;
1987 call
.map_view
.prot
= protect
;
1988 res
= NTDLL_queue_process_apc( process
, &call
, &result
);
1989 if (res
!= STATUS_SUCCESS
) return res
;
1991 if (result
.map_view
.status
== STATUS_SUCCESS
)
1993 *addr_ptr
= result
.map_view
.addr
;
1994 *size_ptr
= result
.map_view
.size
;
1996 return result
.map_view
.status
;
1999 SERVER_START_REQ( get_mapping_info
)
2001 req
->handle
= handle
;
2002 res
= wine_server_call( req
);
2003 prot
= reply
->protect
;
2005 full_size
= reply
->size
;
2006 header_size
= reply
->header_size
;
2007 dup_mapping
= reply
->mapping
;
2008 shared_file
= reply
->shared_file
;
2011 if (res
) return res
;
2014 if (sizeof(size
) < sizeof(full_size
) && (size
!= full_size
))
2015 ERR( "Sizes larger than 4Gb (%x%08x) not supported on this platform\n",
2016 (DWORD
)(full_size
>> 32), (DWORD
)full_size
);
2018 if ((res
= server_get_unix_fd( handle
, 0, &unix_handle
, &needs_close
, NULL
, NULL
))) goto done
;
2020 if (prot
& VPROT_IMAGE
)
2024 int shared_fd
, shared_needs_close
;
2026 if ((res
= server_get_unix_fd( shared_file
, FILE_READ_DATA
|FILE_WRITE_DATA
,
2027 &shared_fd
, &shared_needs_close
, NULL
, NULL
))) goto done
;
2028 res
= map_image( handle
, unix_handle
, base
, size
, mask
, header_size
,
2029 shared_fd
, dup_mapping
, addr_ptr
);
2030 if (shared_needs_close
) close( shared_fd
);
2031 NtClose( shared_file
);
2035 res
= map_image( handle
, unix_handle
, base
, size
, mask
, header_size
,
2036 -1, dup_mapping
, addr_ptr
);
2038 if (needs_close
) close( unix_handle
);
2039 if (!res
) *size_ptr
= size
;
2043 if ((offset
.QuadPart
>= size
) || (*size_ptr
> size
- offset
.QuadPart
))
2045 res
= STATUS_INVALID_PARAMETER
;
2048 if (*size_ptr
) size
= ROUND_SIZE( offset
.u
.LowPart
, *size_ptr
);
2049 else size
= size
- offset
.QuadPart
;
2055 case PAGE_READWRITE
:
2056 case PAGE_EXECUTE_READWRITE
:
2057 if (!(prot
& VPROT_WRITE
))
2059 res
= STATUS_INVALID_PARAMETER
;
2064 case PAGE_WRITECOPY
:
2066 case PAGE_EXECUTE_READ
:
2067 case PAGE_EXECUTE_WRITECOPY
:
2068 if (prot
& VPROT_READ
) break;
2071 res
= STATUS_INVALID_PARAMETER
;
2075 /* FIXME: If a mapping is created with SEC_RESERVE and a process,
2076 * which has a view of this mapping commits some pages, they will
2077 * appear committed in all other processes, which have the same
2078 * view created. Since we don't support this yet, we create the
2079 * whole mapping committed.
2081 prot
|= VPROT_COMMITTED
;
2083 /* Reserve a properly aligned area */
2085 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2087 res
= map_view( &view
, *addr_ptr
, size
, mask
, FALSE
, prot
);
2090 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2096 TRACE("handle=%p size=%lx offset=%x%08x\n",
2097 handle
, size
, offset
.u
.HighPart
, offset
.u
.LowPart
);
2099 res
= map_file_into_view( view
, unix_handle
, 0, size
, offset
.QuadPart
, prot
, !dup_mapping
);
2100 if (res
== STATUS_SUCCESS
)
2102 *addr_ptr
= view
->base
;
2104 view
->mapping
= dup_mapping
;
2105 dup_mapping
= 0; /* don't close it */
2109 ERR( "map_file_into_view %p %lx %x%08x failed\n",
2110 view
->base
, size
, offset
.u
.HighPart
, offset
.u
.LowPart
);
2111 delete_view( view
);
2114 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2117 if (dup_mapping
) NtClose( dup_mapping
);
2118 if (needs_close
) close( unix_handle
);
2123 /***********************************************************************
2124 * NtUnmapViewOfSection (NTDLL.@)
2125 * ZwUnmapViewOfSection (NTDLL.@)
2127 NTSTATUS WINAPI
NtUnmapViewOfSection( HANDLE process
, PVOID addr
)
2130 NTSTATUS status
= STATUS_INVALID_PARAMETER
;
2132 void *base
= ROUND_ADDR( addr
, page_mask
);
2134 if (process
!= NtCurrentProcess())
2137 apc_result_t result
;
2139 memset( &call
, 0, sizeof(call
) );
2141 call
.unmap_view
.type
= APC_UNMAP_VIEW
;
2142 call
.unmap_view
.addr
= addr
;
2143 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
2144 if (status
== STATUS_SUCCESS
) status
= result
.unmap_view
.status
;
2148 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2149 if ((view
= VIRTUAL_FindView( base
)) && (base
== view
->base
))
2151 delete_view( view
);
2152 status
= STATUS_SUCCESS
;
2154 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2159 /***********************************************************************
2160 * NtFlushVirtualMemory (NTDLL.@)
2161 * ZwFlushVirtualMemory (NTDLL.@)
2163 NTSTATUS WINAPI
NtFlushVirtualMemory( HANDLE process
, LPCVOID
*addr_ptr
,
2164 SIZE_T
*size_ptr
, ULONG unknown
)
2167 NTSTATUS status
= STATUS_SUCCESS
;
2169 void *addr
= ROUND_ADDR( *addr_ptr
, page_mask
);
2171 if (process
!= NtCurrentProcess())
2174 apc_result_t result
;
2176 memset( &call
, 0, sizeof(call
) );
2178 call
.virtual_flush
.type
= APC_VIRTUAL_FLUSH
;
2179 call
.virtual_flush
.addr
= addr
;
2180 call
.virtual_flush
.size
= *size_ptr
;
2181 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
2182 if (status
!= STATUS_SUCCESS
) return status
;
2184 if (result
.virtual_flush
.status
== STATUS_SUCCESS
)
2186 *addr_ptr
= result
.virtual_flush
.addr
;
2187 *size_ptr
= result
.virtual_flush
.size
;
2189 return result
.virtual_flush
.status
;
2192 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2193 if (!(view
= VIRTUAL_FindView( addr
))) status
= STATUS_INVALID_PARAMETER
;
2196 if (!*size_ptr
) *size_ptr
= view
->size
;
2198 if (msync( addr
, *size_ptr
, MS_SYNC
)) status
= STATUS_NOT_MAPPED_DATA
;
2200 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2205 /***********************************************************************
2206 * NtReadVirtualMemory (NTDLL.@)
2207 * ZwReadVirtualMemory (NTDLL.@)
2209 NTSTATUS WINAPI
NtReadVirtualMemory( HANDLE process
, const void *addr
, void *buffer
,
2210 SIZE_T size
, SIZE_T
*bytes_read
)
2214 SERVER_START_REQ( read_process_memory
)
2216 req
->handle
= process
;
2217 req
->addr
= (void *)addr
;
2218 wine_server_set_reply( req
, buffer
, size
);
2219 if ((status
= wine_server_call( req
))) size
= 0;
2222 if (bytes_read
) *bytes_read
= size
;
2227 /***********************************************************************
2228 * NtWriteVirtualMemory (NTDLL.@)
2229 * ZwWriteVirtualMemory (NTDLL.@)
2231 NTSTATUS WINAPI
NtWriteVirtualMemory( HANDLE process
, void *addr
, const void *buffer
,
2232 SIZE_T size
, SIZE_T
*bytes_written
)
2236 SERVER_START_REQ( write_process_memory
)
2238 req
->handle
= process
;
2240 wine_server_add_data( req
, buffer
, size
);
2241 if ((status
= wine_server_call( req
))) size
= 0;
2244 if (bytes_written
) *bytes_written
= size
;
2249 /***********************************************************************
2250 * NtAreMappedFilesTheSame (NTDLL.@)
2251 * ZwAreMappedFilesTheSame (NTDLL.@)
2253 NTSTATUS WINAPI
NtAreMappedFilesTheSame(PVOID addr1
, PVOID addr2
)
2255 TRACE("%p %p\n", addr1
, addr2
);
2257 return STATUS_NOT_SAME_DEVICE
;