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>
44 #ifdef HAVE_VALGRIND_VALGRIND_H
45 # include <valgrind/valgrind.h>
48 #define NONAMELESSUNION
49 #define NONAMELESSSTRUCT
51 #define WIN32_NO_STATUS
54 #include "wine/library.h"
55 #include "wine/server.h"
56 #include "wine/list.h"
57 #include "wine/debug.h"
58 #include "ntdll_misc.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(virtual);
61 WINE_DECLARE_DEBUG_CHANNEL(module
);
68 #define MAP_NORESERVE 0
72 typedef struct file_view
74 struct list entry
; /* Entry in global view list */
75 void *base
; /* Base address */
76 size_t size
; /* Size in bytes */
77 HANDLE mapping
; /* Handle to the file mapping */
78 BYTE flags
; /* Allocation flags (VFLAG_*) */
79 BYTE protect
; /* Protection for all pages at allocation time */
80 BYTE prot
[1]; /* Protection byte for each page */
84 #define VFLAG_SYSTEM 0x01 /* system view (underlying mmap not under our control) */
85 #define VFLAG_VALLOC 0x02 /* allocated by VirtualAlloc */
87 /* Conversion from VPROT_* to Win32 flags */
88 static const BYTE VIRTUAL_Win32Flags
[16] =
90 PAGE_NOACCESS
, /* 0 */
91 PAGE_READONLY
, /* READ */
92 PAGE_READWRITE
, /* WRITE */
93 PAGE_READWRITE
, /* READ | WRITE */
94 PAGE_EXECUTE
, /* EXEC */
95 PAGE_EXECUTE_READ
, /* READ | EXEC */
96 PAGE_EXECUTE_READWRITE
, /* WRITE | EXEC */
97 PAGE_EXECUTE_READWRITE
, /* READ | WRITE | EXEC */
98 PAGE_WRITECOPY
, /* WRITECOPY */
99 PAGE_WRITECOPY
, /* READ | WRITECOPY */
100 PAGE_WRITECOPY
, /* WRITE | WRITECOPY */
101 PAGE_WRITECOPY
, /* READ | WRITE | WRITECOPY */
102 PAGE_EXECUTE_WRITECOPY
, /* EXEC | WRITECOPY */
103 PAGE_EXECUTE_WRITECOPY
, /* READ | EXEC | WRITECOPY */
104 PAGE_EXECUTE_WRITECOPY
, /* WRITE | EXEC | WRITECOPY */
105 PAGE_EXECUTE_WRITECOPY
/* READ | WRITE | EXEC | WRITECOPY */
108 static struct list views_list
= LIST_INIT(views_list
);
110 static RTL_CRITICAL_SECTION csVirtual
;
111 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
114 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
115 0, 0, { (DWORD_PTR
)(__FILE__
": csVirtual") }
117 static RTL_CRITICAL_SECTION csVirtual
= { &critsect_debug
, -1, 0, 0, 0, 0 };
120 /* These are always the same on an i386, and it will be faster this way */
121 # define page_mask 0xfff
122 # define page_shift 12
123 # define page_size 0x1000
124 /* Note: these are Windows limits, you cannot change them. */
125 static void *address_space_limit
= (void *)0xc0000000; /* top of the total available address space */
126 static void *user_space_limit
= (void *)0x7fff0000; /* top of the user address space */
128 static UINT page_shift
;
129 static UINT page_size
;
130 static UINT_PTR page_mask
;
131 static void * const address_space_limit
= 0; /* no limit needed on other platforms */
132 static void * const user_space_limit
= 0; /* no limit needed on other platforms */
133 #endif /* __i386__ */
135 #define ROUND_ADDR(addr,mask) \
136 ((void *)((UINT_PTR)(addr) & ~(UINT_PTR)(mask)))
138 #define ROUND_SIZE(addr,size) \
139 (((UINT)(size) + ((UINT_PTR)(addr) & page_mask) + page_mask) & ~page_mask)
141 #define VIRTUAL_DEBUG_DUMP_VIEW(view) \
142 do { if (TRACE_ON(virtual)) VIRTUAL_DumpView(view); } while (0)
144 static void *preload_reserve_start
;
145 static void *preload_reserve_end
;
146 static int use_locks
;
147 static int force_exec_prot
; /* whether to force PROT_EXEC on all PROT_READ mmaps */
150 /***********************************************************************
153 static const char *VIRTUAL_GetProtStr( BYTE prot
)
155 static char buffer
[6];
156 buffer
[0] = (prot
& VPROT_COMMITTED
) ? 'c' : '-';
157 buffer
[1] = (prot
& VPROT_GUARD
) ? 'g' : '-';
158 buffer
[2] = (prot
& VPROT_READ
) ? 'r' : '-';
159 buffer
[3] = (prot
& VPROT_WRITECOPY
) ? 'W' : ((prot
& VPROT_WRITE
) ? 'w' : '-');
160 buffer
[4] = (prot
& VPROT_EXEC
) ? 'x' : '-';
166 /***********************************************************************
167 * VIRTUAL_GetUnixProt
169 * Convert page protections to protection for mmap/mprotect.
171 static int VIRTUAL_GetUnixProt( BYTE vprot
)
174 if ((vprot
& VPROT_COMMITTED
) && !(vprot
& VPROT_GUARD
))
176 if (vprot
& VPROT_READ
) prot
|= PROT_READ
;
177 if (vprot
& VPROT_WRITE
) prot
|= PROT_WRITE
;
178 if (vprot
& VPROT_WRITECOPY
) prot
|= PROT_WRITE
;
179 if (vprot
& VPROT_EXEC
) prot
|= PROT_EXEC
;
181 if (!prot
) prot
= PROT_NONE
;
186 /***********************************************************************
189 static void VIRTUAL_DumpView( FILE_VIEW
*view
)
192 char *addr
= view
->base
;
193 BYTE prot
= view
->prot
[0];
195 TRACE( "View: %p - %p", addr
, addr
+ view
->size
- 1 );
196 if (view
->flags
& VFLAG_SYSTEM
)
197 TRACE( " (system)\n" );
198 else if (view
->flags
& VFLAG_VALLOC
)
199 TRACE( " (valloc)\n" );
200 else if (view
->mapping
)
201 TRACE( " %p\n", view
->mapping
);
203 TRACE( " (anonymous)\n");
205 for (count
= i
= 1; i
< view
->size
>> page_shift
; i
++, count
++)
207 if (view
->prot
[i
] == prot
) continue;
208 TRACE( " %p - %p %s\n",
209 addr
, addr
+ (count
<< page_shift
) - 1, VIRTUAL_GetProtStr(prot
) );
210 addr
+= (count
<< page_shift
);
211 prot
= view
->prot
[i
];
215 TRACE( " %p - %p %s\n",
216 addr
, addr
+ (count
<< page_shift
) - 1, VIRTUAL_GetProtStr(prot
) );
220 /***********************************************************************
224 static void VIRTUAL_Dump(void)
227 struct file_view
*view
;
229 TRACE( "Dump of all virtual memory views:\n" );
230 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
231 LIST_FOR_EACH_ENTRY( view
, &views_list
, FILE_VIEW
, entry
)
233 VIRTUAL_DumpView( view
);
235 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
240 /***********************************************************************
243 * Find the view containing a given address. The csVirtual section must be held by caller.
252 static struct file_view
*VIRTUAL_FindView( const void *addr
)
254 struct file_view
*view
;
256 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
258 if (view
->base
> addr
) break;
259 if ((const char*)view
->base
+ view
->size
> (const char*)addr
) return view
;
265 /***********************************************************************
268 static inline UINT_PTR
get_mask( ULONG zero_bits
)
270 if (!zero_bits
) return 0xffff; /* allocations are aligned to 64K by default */
271 if (zero_bits
< page_shift
) zero_bits
= page_shift
;
272 return (1 << zero_bits
) - 1;
276 /***********************************************************************
279 * Find the first view overlapping at least part of the specified range.
280 * The csVirtual section must be held by caller.
282 static struct file_view
*find_view_range( const void *addr
, size_t size
)
284 struct file_view
*view
;
286 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
288 if ((const char *)view
->base
>= (const char *)addr
+ size
) break;
289 if ((const char *)view
->base
+ view
->size
> (const char *)addr
) return view
;
295 /***********************************************************************
298 * Find a free area between views inside the specified range.
299 * The csVirtual section must be held by caller.
301 static void *find_free_area( void *base
, void *end
, size_t size
, size_t mask
, int top_down
)
308 start
= ROUND_ADDR( (char *)end
- size
, mask
);
309 if (start
>= end
|| start
< base
) return NULL
;
311 for (ptr
= views_list
.prev
; ptr
!= &views_list
; ptr
= ptr
->prev
)
313 struct file_view
*view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
315 if ((char *)view
->base
+ view
->size
<= (char *)start
) break;
316 if ((char *)view
->base
>= (char *)start
+ size
) continue;
317 start
= ROUND_ADDR( (char *)view
->base
- size
, mask
);
318 /* stop if remaining space is not large enough */
319 if (!start
|| start
>= end
|| start
< base
) return NULL
;
324 start
= ROUND_ADDR( (char *)base
+ mask
, mask
);
325 if (start
>= end
|| (char *)end
- (char *)start
< size
) return NULL
;
327 for (ptr
= views_list
.next
; ptr
!= &views_list
; ptr
= ptr
->next
)
329 struct file_view
*view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
331 if ((char *)view
->base
>= (char *)start
+ size
) break;
332 if ((char *)view
->base
+ view
->size
<= (char *)start
) continue;
333 start
= ROUND_ADDR( (char *)view
->base
+ view
->size
+ mask
, mask
);
334 /* stop if remaining space is not large enough */
335 if (!start
|| start
>= end
|| (char *)end
- (char *)start
< size
) return NULL
;
342 /***********************************************************************
345 * Add a reserved area to the list maintained by libwine.
346 * The csVirtual section must be held by caller.
348 static void add_reserved_area( void *addr
, size_t size
)
350 TRACE( "adding %p-%p\n", addr
, (char *)addr
+ size
);
352 if (addr
< user_space_limit
)
354 /* unmap the part of the area that is below the limit */
355 assert( (char *)addr
+ size
> (char *)user_space_limit
);
356 munmap( addr
, (char *)user_space_limit
- (char *)addr
);
357 size
-= (char *)user_space_limit
- (char *)addr
;
358 addr
= user_space_limit
;
360 /* blow away existing mappings */
361 wine_anon_mmap( addr
, size
, PROT_NONE
, MAP_NORESERVE
| MAP_FIXED
);
362 wine_mmap_add_reserved_area( addr
, size
);
366 /***********************************************************************
369 * Check if an address range goes beyond a given limit.
371 static inline int is_beyond_limit( const void *addr
, size_t size
, const void *limit
)
373 return (limit
&& (addr
>= limit
|| (const char *)addr
+ size
> (const char *)limit
));
377 /***********************************************************************
380 * Unmap an area, or simply replace it by an empty mapping if it is
381 * in a reserved area. The csVirtual section must be held by caller.
383 static inline void unmap_area( void *addr
, size_t size
)
385 if (wine_mmap_is_in_reserved_area( addr
, size
))
386 wine_anon_mmap( addr
, size
, PROT_NONE
, MAP_NORESERVE
| MAP_FIXED
);
387 else if (is_beyond_limit( addr
, size
, user_space_limit
))
388 add_reserved_area( addr
, size
);
390 munmap( addr
, size
);
394 /***********************************************************************
397 * Deletes a view. The csVirtual section must be held by caller.
399 static void delete_view( struct file_view
*view
) /* [in] View */
401 if (!(view
->flags
& VFLAG_SYSTEM
)) unmap_area( view
->base
, view
->size
);
402 list_remove( &view
->entry
);
403 if (view
->mapping
) NtClose( view
->mapping
);
408 /***********************************************************************
411 * Create a view. The csVirtual section must be held by caller.
413 static NTSTATUS
create_view( struct file_view
**view_ret
, void *base
, size_t size
, BYTE vprot
)
415 struct file_view
*view
;
417 int unix_prot
= VIRTUAL_GetUnixProt( vprot
);
419 assert( !((UINT_PTR
)base
& page_mask
) );
420 assert( !(size
& page_mask
) );
422 /* Create the view structure */
424 if (!(view
= malloc( sizeof(*view
) + (size
>> page_shift
) - 1 ))) return STATUS_NO_MEMORY
;
430 view
->protect
= vprot
;
431 memset( view
->prot
, vprot
& ~VPROT_IMAGE
, size
>> page_shift
);
433 /* Insert it in the linked list */
435 LIST_FOR_EACH( ptr
, &views_list
)
437 struct file_view
*next
= LIST_ENTRY( ptr
, struct file_view
, entry
);
438 if (next
->base
> base
) break;
440 list_add_before( ptr
, &view
->entry
);
442 /* Check for overlapping views. This can happen if the previous view
443 * was a system view that got unmapped behind our back. In that case
444 * we recover by simply deleting it. */
446 if ((ptr
= list_prev( &views_list
, &view
->entry
)) != NULL
)
448 struct file_view
*prev
= LIST_ENTRY( ptr
, struct file_view
, entry
);
449 if ((char *)prev
->base
+ prev
->size
> (char *)base
)
451 TRACE( "overlapping prev view %p-%p for %p-%p\n",
452 prev
->base
, (char *)prev
->base
+ prev
->size
,
453 base
, (char *)base
+ view
->size
);
454 assert( prev
->flags
& VFLAG_SYSTEM
);
458 if ((ptr
= list_next( &views_list
, &view
->entry
)) != NULL
)
460 struct file_view
*next
= LIST_ENTRY( ptr
, struct file_view
, entry
);
461 if ((char *)base
+ view
->size
> (char *)next
->base
)
463 TRACE( "overlapping next view %p-%p for %p-%p\n",
464 next
->base
, (char *)next
->base
+ next
->size
,
465 base
, (char *)base
+ view
->size
);
466 assert( next
->flags
& VFLAG_SYSTEM
);
472 VIRTUAL_DEBUG_DUMP_VIEW( view
);
474 if (force_exec_prot
&& (unix_prot
& PROT_READ
) && !(unix_prot
& PROT_EXEC
))
476 TRACE( "forcing exec permission on %p-%p\n", base
, (char *)base
+ size
- 1 );
477 mprotect( base
, size
, unix_prot
| PROT_EXEC
);
479 return STATUS_SUCCESS
;
483 /***********************************************************************
484 * VIRTUAL_GetWin32Prot
486 * Convert page protections to Win32 flags.
488 static DWORD
VIRTUAL_GetWin32Prot( BYTE vprot
)
490 DWORD ret
= VIRTUAL_Win32Flags
[vprot
& 0x0f];
491 if (vprot
& VPROT_NOCACHE
) ret
|= PAGE_NOCACHE
;
492 if (vprot
& VPROT_GUARD
) ret
|= PAGE_GUARD
;
497 /***********************************************************************
500 * Build page protections from Win32 flags.
503 * protect [I] Win32 protection flags
506 * Value of page protection flags
508 static BYTE
VIRTUAL_GetProt( DWORD protect
)
512 switch(protect
& 0xff)
518 vprot
= VPROT_READ
| VPROT_WRITE
;
521 /* MSDN CreateFileMapping() states that if PAGE_WRITECOPY is given,
522 * that the hFile must have been opened with GENERIC_READ and
523 * GENERIC_WRITE access. This is WRONG as tests show that you
524 * only need GENERIC_READ access (at least for Win9x,
525 * FIXME: what about NT?). Thus, we don't put VPROT_WRITE in
526 * PAGE_WRITECOPY and PAGE_EXECUTE_WRITECOPY.
528 vprot
= VPROT_READ
| VPROT_WRITECOPY
;
533 case PAGE_EXECUTE_READ
:
534 vprot
= VPROT_EXEC
| VPROT_READ
;
536 case PAGE_EXECUTE_READWRITE
:
537 vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITE
;
539 case PAGE_EXECUTE_WRITECOPY
:
540 /* See comment for PAGE_WRITECOPY above */
541 vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITECOPY
;
548 if (protect
& PAGE_GUARD
) vprot
|= VPROT_GUARD
;
549 if (protect
& PAGE_NOCACHE
) vprot
|= VPROT_NOCACHE
;
554 /***********************************************************************
557 * Change the protection of a range of pages.
563 static BOOL
VIRTUAL_SetProt( FILE_VIEW
*view
, /* [in] Pointer to view */
564 void *base
, /* [in] Starting address */
565 size_t size
, /* [in] Size in bytes */
566 BYTE vprot
) /* [in] Protections to use */
568 int unix_prot
= VIRTUAL_GetUnixProt(vprot
);
571 base
, (char *)base
+ size
- 1, VIRTUAL_GetProtStr( vprot
) );
573 /* if setting stack guard pages, store the permissions first, as the guard may be
574 * triggered at any point after mprotect and change the permissions again */
575 if ((vprot
& VPROT_GUARD
) &&
576 ((char *)base
>= (char *)NtCurrentTeb()->DeallocationStack
) &&
577 ((char *)base
< (char *)NtCurrentTeb()->Tib
.StackBase
))
579 memset( view
->prot
+ (((char *)base
- (char *)view
->base
) >> page_shift
),
580 vprot
, size
>> page_shift
);
581 mprotect( base
, size
, unix_prot
);
582 VIRTUAL_DEBUG_DUMP_VIEW( view
);
586 if (force_exec_prot
&& (unix_prot
& PROT_READ
) && !(unix_prot
& PROT_EXEC
))
588 TRACE( "forcing exec permission on %p-%p\n", base
, (char *)base
+ size
- 1 );
589 if (!mprotect( base
, size
, unix_prot
| PROT_EXEC
)) goto done
;
590 /* exec + write may legitimately fail, in that case fall back to write only */
591 if (!(unix_prot
& PROT_WRITE
)) return FALSE
;
594 if (mprotect( base
, size
, unix_prot
)) return FALSE
; /* FIXME: last error */
597 memset( view
->prot
+ (((char *)base
- (char *)view
->base
) >> page_shift
),
598 vprot
, size
>> page_shift
);
599 VIRTUAL_DEBUG_DUMP_VIEW( view
);
604 /***********************************************************************
607 * Release the extra memory while keeping the range starting on the granularity boundary.
609 static inline void *unmap_extra_space( void *ptr
, size_t total_size
, size_t wanted_size
, size_t mask
)
611 if ((ULONG_PTR
)ptr
& mask
)
613 size_t extra
= mask
+ 1 - ((ULONG_PTR
)ptr
& mask
);
614 munmap( ptr
, extra
);
615 ptr
= (char *)ptr
+ extra
;
618 if (total_size
> wanted_size
)
619 munmap( (char *)ptr
+ wanted_size
, total_size
- wanted_size
);
632 /***********************************************************************
633 * alloc_reserved_area_callback
635 * Try to map some space inside a reserved area. Callback for wine_mmap_enum_reserved_areas.
637 static int alloc_reserved_area_callback( void *start
, size_t size
, void *arg
)
639 static void * const address_space_start
= (void *)0x110000;
640 struct alloc_area
*alloc
= arg
;
641 void *end
= (char *)start
+ size
;
643 if (start
< address_space_start
) start
= address_space_start
;
644 if (user_space_limit
&& end
> user_space_limit
) end
= user_space_limit
;
645 if (start
>= end
) return 0;
647 /* make sure we don't touch the preloader reserved range */
648 if (preload_reserve_end
>= start
)
650 if (preload_reserve_end
>= end
)
652 if (preload_reserve_start
<= start
) return 0; /* no space in that area */
653 if (preload_reserve_start
< end
) end
= preload_reserve_start
;
655 else if (preload_reserve_start
<= start
) start
= preload_reserve_end
;
658 /* range is split in two by the preloader reservation, try first part */
659 if ((alloc
->result
= find_free_area( start
, preload_reserve_start
, alloc
->size
,
660 alloc
->mask
, alloc
->top_down
)))
662 /* then fall through to try second part */
663 start
= preload_reserve_end
;
666 if ((alloc
->result
= find_free_area( start
, end
, alloc
->size
, alloc
->mask
, alloc
->top_down
)))
673 /***********************************************************************
676 * Create a view and mmap the corresponding memory area.
677 * The csVirtual section must be held by caller.
679 static NTSTATUS
map_view( struct file_view
**view_ret
, void *base
, size_t size
, size_t mask
,
680 int top_down
, BYTE vprot
)
687 if (is_beyond_limit( base
, size
, address_space_limit
))
688 return STATUS_WORKING_SET_LIMIT_RANGE
;
690 switch (wine_mmap_is_in_reserved_area( base
, size
))
692 case -1: /* partially in a reserved area */
693 return STATUS_CONFLICTING_ADDRESSES
;
695 case 0: /* not in a reserved area, do a normal allocation */
696 if ((ptr
= wine_anon_mmap( base
, size
, VIRTUAL_GetUnixProt(vprot
), 0 )) == (void *)-1)
698 if (errno
== ENOMEM
) return STATUS_NO_MEMORY
;
699 return STATUS_INVALID_PARAMETER
;
703 /* We couldn't get the address we wanted */
704 if (is_beyond_limit( ptr
, size
, user_space_limit
)) add_reserved_area( ptr
, size
);
705 else munmap( ptr
, size
);
706 return STATUS_CONFLICTING_ADDRESSES
;
711 case 1: /* in a reserved area, make sure the address is available */
712 if (find_view_range( base
, size
)) return STATUS_CONFLICTING_ADDRESSES
;
713 /* replace the reserved area by our mapping */
714 if ((ptr
= wine_anon_mmap( base
, size
, VIRTUAL_GetUnixProt(vprot
), MAP_FIXED
)) != base
)
715 return STATUS_INVALID_PARAMETER
;
721 size_t view_size
= size
+ mask
+ 1;
722 struct alloc_area alloc
;
726 alloc
.top_down
= top_down
;
727 if (wine_mmap_enum_reserved_areas( alloc_reserved_area_callback
, &alloc
, top_down
))
730 TRACE( "got mem in reserved area %p-%p\n", ptr
, (char *)ptr
+ size
);
731 if (wine_anon_mmap( ptr
, size
, VIRTUAL_GetUnixProt(vprot
), MAP_FIXED
) != ptr
)
732 return STATUS_INVALID_PARAMETER
;
738 if ((ptr
= wine_anon_mmap( NULL
, view_size
, VIRTUAL_GetUnixProt(vprot
), 0 )) == (void *)-1)
740 if (errno
== ENOMEM
) return STATUS_NO_MEMORY
;
741 return STATUS_INVALID_PARAMETER
;
743 TRACE( "got mem with anon mmap %p-%p\n", ptr
, (char *)ptr
+ size
);
744 /* if we got something beyond the user limit, unmap it and retry */
745 if (is_beyond_limit( ptr
, view_size
, user_space_limit
)) add_reserved_area( ptr
, view_size
);
748 ptr
= unmap_extra_space( ptr
, view_size
, size
, mask
);
751 status
= create_view( view_ret
, ptr
, size
, vprot
);
752 if (status
!= STATUS_SUCCESS
) unmap_area( ptr
, size
);
757 /***********************************************************************
760 * Linux kernels before 2.4.x can support non page-aligned offsets, as
761 * long as the offset is aligned to the filesystem block size. This is
762 * a big performance gain so we want to take advantage of it.
764 * However, when we use 64-bit file support this doesn't work because
765 * glibc rejects unaligned offsets. Also glibc 2.1.3 mmap64 is broken
766 * in that it rounds unaligned offsets down to a page boundary. For
767 * these reasons we do a direct system call here.
769 static void *unaligned_mmap( void *addr
, size_t length
, unsigned int prot
,
770 unsigned int flags
, int fd
, off_t offset
)
772 #if defined(linux) && defined(__i386__) && defined(__GNUC__)
773 if (!(offset
>> 32) && (offset
& page_mask
))
788 args
.length
= length
;
792 args
.offset
= offset
;
794 __asm__
__volatile__("push %%ebx\n\t"
799 : "0" (90), /* SYS_mmap */
802 if (ret
< 0 && ret
> -4096)
810 return mmap( addr
, length
, prot
, flags
, fd
, offset
);
814 /***********************************************************************
817 * Wrapper for mmap() to map a file into a view, falling back to read if mmap fails.
818 * The csVirtual section must be held by caller.
820 static NTSTATUS
map_file_into_view( struct file_view
*view
, int fd
, size_t start
, size_t size
,
821 off_t offset
, BYTE vprot
, BOOL removable
)
824 int prot
= VIRTUAL_GetUnixProt( vprot
);
825 BOOL shared_write
= (vprot
& VPROT_WRITE
) != 0;
827 assert( start
< view
->size
);
828 assert( start
+ size
<= view
->size
);
830 /* only try mmap if media is not removable (or if we require write access) */
831 if (!removable
|| shared_write
)
833 int flags
= MAP_FIXED
| (shared_write
? MAP_SHARED
: MAP_PRIVATE
);
835 if (unaligned_mmap( (char *)view
->base
+ start
, size
, prot
, flags
, fd
, offset
) != (void *)-1)
838 /* mmap() failed; if this is because the file offset is not */
839 /* page-aligned (EINVAL), or because the underlying filesystem */
840 /* does not support mmap() (ENOEXEC,ENODEV), we do it by hand. */
841 if ((errno
!= ENOEXEC
) && (errno
!= EINVAL
) && (errno
!= ENODEV
)) return FILE_GetNtStatus();
842 if (shared_write
) /* we cannot fake shared write mappings */
844 if (errno
== EINVAL
) return STATUS_INVALID_PARAMETER
;
845 ERR( "shared writable mmap not supported, broken filesystem?\n" );
846 return STATUS_NOT_SUPPORTED
;
850 /* Reserve the memory with an anonymous mmap */
851 ptr
= wine_anon_mmap( (char *)view
->base
+ start
, size
, PROT_READ
| PROT_WRITE
, MAP_FIXED
);
852 if (ptr
== (void *)-1) return FILE_GetNtStatus();
853 /* Now read in the file */
854 pread( fd
, ptr
, size
, offset
);
855 if (prot
!= (PROT_READ
|PROT_WRITE
)) mprotect( ptr
, size
, prot
); /* Set the right protection */
857 memset( view
->prot
+ (start
>> page_shift
), vprot
, ROUND_SIZE(start
,size
) >> page_shift
);
858 return STATUS_SUCCESS
;
862 /***********************************************************************
865 * Decommit some pages of a given view.
866 * The csVirtual section must be held by caller.
868 static NTSTATUS
decommit_pages( struct file_view
*view
, size_t start
, size_t size
)
870 if (wine_anon_mmap( (char *)view
->base
+ start
, size
, PROT_NONE
, MAP_FIXED
) != (void *)-1)
872 BYTE
*p
= view
->prot
+ (start
>> page_shift
);
874 while (size
--) *p
++ &= ~VPROT_COMMITTED
;
875 return STATUS_SUCCESS
;
877 return FILE_GetNtStatus();
881 /***********************************************************************
884 * Map an executable (PE format) image into memory.
886 static NTSTATUS
map_image( HANDLE hmapping
, int fd
, char *base
, SIZE_T total_size
, SIZE_T mask
,
887 SIZE_T header_size
, int shared_fd
, HANDLE dup_mapping
, PVOID
*addr_ptr
)
889 IMAGE_DOS_HEADER
*dos
;
890 IMAGE_NT_HEADERS
*nt
;
891 IMAGE_SECTION_HEADER
*sec
;
892 IMAGE_DATA_DIRECTORY
*imports
;
893 NTSTATUS status
= STATUS_CONFLICTING_ADDRESSES
;
898 struct file_view
*view
= NULL
;
899 char *ptr
, *header_end
;
902 /* zero-map the whole range */
904 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
906 if (base
>= (char *)0x110000) /* make sure the DOS area remains free */
907 status
= map_view( &view
, base
, total_size
, mask
, FALSE
,
908 VPROT_COMMITTED
| VPROT_READ
| VPROT_EXEC
| VPROT_WRITECOPY
| VPROT_IMAGE
);
910 if (status
== STATUS_CONFLICTING_ADDRESSES
)
911 status
= map_view( &view
, NULL
, total_size
, mask
, FALSE
,
912 VPROT_COMMITTED
| VPROT_READ
| VPROT_EXEC
| VPROT_WRITECOPY
| VPROT_IMAGE
);
914 if (status
!= STATUS_SUCCESS
) goto error
;
917 TRACE_(module
)( "mapped PE file at %p-%p\n", ptr
, ptr
+ total_size
);
921 if (fstat( fd
, &st
) == -1)
923 status
= FILE_GetNtStatus();
926 status
= STATUS_INVALID_IMAGE_FORMAT
; /* generic error */
927 if (!st
.st_size
) goto error
;
928 header_size
= min( header_size
, st
.st_size
);
929 if (map_file_into_view( view
, fd
, 0, header_size
, 0, VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
,
930 !dup_mapping
) != STATUS_SUCCESS
) goto error
;
931 dos
= (IMAGE_DOS_HEADER
*)ptr
;
932 nt
= (IMAGE_NT_HEADERS
*)(ptr
+ dos
->e_lfanew
);
933 header_end
= ptr
+ ROUND_SIZE( 0, header_size
);
934 memset( ptr
+ header_size
, 0, header_end
- (ptr
+ header_size
) );
935 if ((char *)(nt
+ 1) > header_end
) goto error
;
936 sec
= (IMAGE_SECTION_HEADER
*)((char*)&nt
->OptionalHeader
+nt
->FileHeader
.SizeOfOptionalHeader
);
937 if ((char *)(sec
+ nt
->FileHeader
.NumberOfSections
) > header_end
) goto error
;
939 imports
= nt
->OptionalHeader
.DataDirectory
+ IMAGE_DIRECTORY_ENTRY_IMPORT
;
940 if (!imports
->Size
|| !imports
->VirtualAddress
) imports
= NULL
;
942 /* check the architecture */
944 if (nt
->FileHeader
.Machine
!= IMAGE_FILE_MACHINE_I386
)
946 MESSAGE("Trying to load PE image for unsupported architecture (");
947 switch (nt
->FileHeader
.Machine
)
949 case IMAGE_FILE_MACHINE_UNKNOWN
: MESSAGE("Unknown"); break;
950 case IMAGE_FILE_MACHINE_I860
: MESSAGE("I860"); break;
951 case IMAGE_FILE_MACHINE_R3000
: MESSAGE("R3000"); break;
952 case IMAGE_FILE_MACHINE_R4000
: MESSAGE("R4000"); break;
953 case IMAGE_FILE_MACHINE_R10000
: MESSAGE("R10000"); break;
954 case IMAGE_FILE_MACHINE_ALPHA
: MESSAGE("Alpha"); break;
955 case IMAGE_FILE_MACHINE_POWERPC
: MESSAGE("PowerPC"); break;
956 case IMAGE_FILE_MACHINE_IA64
: MESSAGE("IA-64"); break;
957 case IMAGE_FILE_MACHINE_ALPHA64
: MESSAGE("Alpha-64"); break;
958 case IMAGE_FILE_MACHINE_AMD64
: MESSAGE("AMD-64"); break;
959 case IMAGE_FILE_MACHINE_ARM
: MESSAGE("ARM"); break;
960 default: MESSAGE("Unknown-%04x", nt
->FileHeader
.Machine
); break;
966 /* check for non page-aligned binary */
968 if (nt
->OptionalHeader
.SectionAlignment
<= page_mask
)
970 /* unaligned sections, this happens for native subsystem binaries */
971 /* in that case Windows simply maps in the whole file */
973 if (map_file_into_view( view
, fd
, 0, total_size
, 0, VPROT_COMMITTED
| VPROT_READ
,
974 !dup_mapping
) != STATUS_SUCCESS
) goto error
;
976 /* check that all sections are loaded at the right offset */
977 if (nt
->OptionalHeader
.FileAlignment
!= nt
->OptionalHeader
.SectionAlignment
) goto error
;
978 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++)
980 if (sec
[i
].VirtualAddress
!= sec
[i
].PointerToRawData
)
981 goto error
; /* Windows refuses to load in that case too */
984 /* set the image protections */
985 VIRTUAL_SetProt( view
, ptr
, total_size
,
986 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
| VPROT_EXEC
);
988 /* no relocations are performed on non page-aligned binaries */
993 /* map all the sections */
995 for (i
= pos
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
997 static const SIZE_T sector_align
= 0x1ff;
998 SIZE_T map_size
, file_start
, file_size
, end
;
1000 if (!sec
->Misc
.VirtualSize
)
1001 map_size
= ROUND_SIZE( 0, sec
->SizeOfRawData
);
1003 map_size
= ROUND_SIZE( 0, sec
->Misc
.VirtualSize
);
1005 /* file positions are rounded to sector boundaries regardless of OptionalHeader.FileAlignment */
1006 file_start
= sec
->PointerToRawData
& ~sector_align
;
1007 file_size
= (sec
->SizeOfRawData
+ (sec
->PointerToRawData
& sector_align
) + sector_align
) & ~sector_align
;
1008 if (file_size
> map_size
) file_size
= map_size
;
1010 /* a few sanity checks */
1011 end
= sec
->VirtualAddress
+ ROUND_SIZE( sec
->VirtualAddress
, map_size
);
1012 if (sec
->VirtualAddress
> total_size
|| end
> total_size
|| end
< sec
->VirtualAddress
)
1014 WARN_(module
)( "Section %.8s too large (%x+%lx/%lx)\n",
1015 sec
->Name
, sec
->VirtualAddress
, map_size
, total_size
);
1019 if ((sec
->Characteristics
& IMAGE_SCN_MEM_SHARED
) &&
1020 (sec
->Characteristics
& IMAGE_SCN_MEM_WRITE
))
1022 TRACE_(module
)( "mapping shared section %.8s at %p off %x (%x) size %lx (%lx) flags %x\n",
1023 sec
->Name
, ptr
+ sec
->VirtualAddress
,
1024 sec
->PointerToRawData
, (int)pos
, file_size
, map_size
,
1025 sec
->Characteristics
);
1026 if (map_file_into_view( view
, shared_fd
, sec
->VirtualAddress
, map_size
, pos
,
1027 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITE
,
1028 FALSE
) != STATUS_SUCCESS
)
1030 ERR_(module
)( "Could not map shared section %.8s\n", sec
->Name
);
1034 /* check if the import directory falls inside this section */
1035 if (imports
&& imports
->VirtualAddress
>= sec
->VirtualAddress
&&
1036 imports
->VirtualAddress
< sec
->VirtualAddress
+ map_size
)
1038 UINT_PTR base
= imports
->VirtualAddress
& ~page_mask
;
1039 UINT_PTR end
= base
+ ROUND_SIZE( imports
->VirtualAddress
, imports
->Size
);
1040 if (end
> sec
->VirtualAddress
+ map_size
) end
= sec
->VirtualAddress
+ map_size
;
1042 map_file_into_view( view
, shared_fd
, base
, end
- base
,
1043 pos
+ (base
- sec
->VirtualAddress
),
1044 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
,
1051 TRACE_(module
)( "mapping section %.8s at %p off %x size %x virt %x flags %x\n",
1052 sec
->Name
, ptr
+ sec
->VirtualAddress
,
1053 sec
->PointerToRawData
, sec
->SizeOfRawData
,
1054 sec
->Misc
.VirtualSize
, sec
->Characteristics
);
1056 if (!sec
->PointerToRawData
|| !file_size
) continue;
1058 /* Note: if the section is not aligned properly map_file_into_view will magically
1059 * fall back to read(), so we don't need to check anything here.
1061 end
= file_start
+ file_size
;
1062 if (sec
->PointerToRawData
>= st
.st_size
||
1063 end
> ((st
.st_size
+ sector_align
) & ~sector_align
) ||
1065 map_file_into_view( view
, fd
, sec
->VirtualAddress
, file_size
, file_start
,
1066 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITECOPY
,
1067 !dup_mapping
) != STATUS_SUCCESS
)
1069 ERR_(module
)( "Could not map section %.8s, file probably truncated\n", sec
->Name
);
1073 if (file_size
& page_mask
)
1075 end
= ROUND_SIZE( 0, file_size
);
1076 if (end
> map_size
) end
= map_size
;
1077 TRACE_(module
)("clearing %p - %p\n",
1078 ptr
+ sec
->VirtualAddress
+ file_size
,
1079 ptr
+ sec
->VirtualAddress
+ end
);
1080 memset( ptr
+ sec
->VirtualAddress
+ file_size
, 0, end
- file_size
);
1085 /* perform base relocation, if necessary */
1088 ((nt
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
) ||
1089 !NtCurrentTeb()->Peb
->ImageBaseAddress
) )
1091 IMAGE_BASE_RELOCATION
*rel
, *end
;
1092 const IMAGE_DATA_DIRECTORY
*relocs
;
1094 if (nt
->FileHeader
.Characteristics
& IMAGE_FILE_RELOCS_STRIPPED
)
1096 WARN_(module
)( "Need to relocate module from %p to %p, but there are no relocation records\n",
1098 status
= STATUS_CONFLICTING_ADDRESSES
;
1102 TRACE_(module
)( "relocating from %p-%p to %p-%p\n",
1103 base
, base
+ total_size
, ptr
, ptr
+ total_size
);
1105 relocs
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1106 rel
= (IMAGE_BASE_RELOCATION
*)(ptr
+ relocs
->VirtualAddress
);
1107 end
= (IMAGE_BASE_RELOCATION
*)(ptr
+ relocs
->VirtualAddress
+ relocs
->Size
);
1110 while (rel
< end
- 1 && rel
->SizeOfBlock
)
1112 if (rel
->VirtualAddress
>= total_size
)
1114 WARN_(module
)( "invalid address %p in relocation %p\n", ptr
+ rel
->VirtualAddress
, rel
);
1115 status
= STATUS_ACCESS_VIOLATION
;
1118 rel
= LdrProcessRelocationBlock( ptr
+ rel
->VirtualAddress
,
1119 (rel
->SizeOfBlock
- sizeof(*rel
)) / sizeof(USHORT
),
1120 (USHORT
*)(rel
+ 1), delta
);
1121 if (!rel
) goto error
;
1125 /* set the image protections */
1127 VIRTUAL_SetProt( view
, ptr
, ROUND_SIZE( 0, header_size
), VPROT_COMMITTED
| VPROT_READ
);
1129 sec
= (IMAGE_SECTION_HEADER
*)((char *)&nt
->OptionalHeader
+nt
->FileHeader
.SizeOfOptionalHeader
);
1130 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
1133 BYTE vprot
= VPROT_COMMITTED
;
1135 if (sec
->Misc
.VirtualSize
)
1136 size
= ROUND_SIZE( sec
->VirtualAddress
, sec
->Misc
.VirtualSize
);
1138 size
= ROUND_SIZE( sec
->VirtualAddress
, sec
->SizeOfRawData
);
1140 if (sec
->Characteristics
& IMAGE_SCN_MEM_READ
) vprot
|= VPROT_READ
;
1141 if (sec
->Characteristics
& IMAGE_SCN_MEM_WRITE
) vprot
|= VPROT_READ
|VPROT_WRITECOPY
;
1142 if (sec
->Characteristics
& IMAGE_SCN_MEM_EXECUTE
) vprot
|= VPROT_EXEC
;
1144 /* Dumb game crack lets the AOEP point into a data section. Adjust. */
1145 if ((nt
->OptionalHeader
.AddressOfEntryPoint
>= sec
->VirtualAddress
) &&
1146 (nt
->OptionalHeader
.AddressOfEntryPoint
< sec
->VirtualAddress
+ size
))
1147 vprot
|= VPROT_EXEC
;
1149 VIRTUAL_SetProt( view
, ptr
+ sec
->VirtualAddress
, size
, vprot
);
1153 view
->mapping
= dup_mapping
;
1154 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1157 #ifdef VALGRIND_LOAD_PDB_DEBUGINFO
1158 VALGRIND_LOAD_PDB_DEBUGINFO(fd
, ptr
, total_size
, delta
);
1160 return STATUS_SUCCESS
;
1163 if (view
) delete_view( view
);
1164 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1165 if (dup_mapping
) NtClose( dup_mapping
);
1170 static int get_address_space_limit( void *base
, size_t size
, void *arg
)
1173 if ((char *)base
+ size
> (char *)*limit
) *limit
= (char *)base
+ size
;
1177 /***********************************************************************
1180 void virtual_init(void)
1182 const char *preload
;
1184 page_size
= getpagesize();
1185 page_mask
= page_size
- 1;
1186 /* Make sure we have a power of 2 */
1187 assert( !(page_size
& page_mask
) );
1189 while ((1 << page_shift
) != page_size
) page_shift
++;
1190 #endif /* page_mask */
1191 if ((preload
= getenv("WINEPRELOADRESERVE")))
1193 unsigned long start
, end
;
1194 if (sscanf( preload
, "%lx-%lx", &start
, &end
) == 2)
1196 preload_reserve_start
= (void *)start
;
1197 preload_reserve_end
= (void *)end
;
1200 if (address_space_limit
)
1201 wine_mmap_enum_reserved_areas( get_address_space_limit
, &address_space_limit
, 1 );
1205 /***********************************************************************
1206 * virtual_init_threading
1208 void virtual_init_threading(void)
1214 /***********************************************************************
1215 * virtual_alloc_thread_stack
1217 NTSTATUS
virtual_alloc_thread_stack( void *base
, SIZE_T size
)
1223 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1225 if (base
) /* already allocated, create a system view */
1227 size
= ROUND_SIZE( base
, size
);
1228 base
= ROUND_ADDR( base
, page_mask
);
1229 if ((status
= create_view( &view
, base
, size
,
1230 VPROT_READ
| VPROT_WRITE
| VPROT_COMMITTED
)) != STATUS_SUCCESS
)
1232 view
->flags
|= VFLAG_VALLOC
| VFLAG_SYSTEM
;
1236 size
= (size
+ 0xffff) & ~0xffff; /* round to 64K boundary */
1237 if ((status
= map_view( &view
, NULL
, size
, 0xffff, 0,
1238 VPROT_READ
| VPROT_WRITE
| VPROT_COMMITTED
)) != STATUS_SUCCESS
)
1240 view
->flags
|= VFLAG_VALLOC
;
1241 #ifdef VALGRIND_STACK_REGISTER
1242 /* no need to de-register the stack as it's the one of the main thread */
1243 VALGRIND_STACK_REGISTER( view
->base
, (char *)view
->base
+ view
->size
);
1247 /* setup no access guard page */
1248 VIRTUAL_SetProt( view
, view
->base
, page_size
, VPROT_COMMITTED
);
1249 VIRTUAL_SetProt( view
, (char *)view
->base
+ page_size
, page_size
,
1250 VPROT_READ
| VPROT_WRITE
| VPROT_COMMITTED
| VPROT_GUARD
);
1252 /* note: limit is lower than base since the stack grows down */
1253 NtCurrentTeb()->DeallocationStack
= view
->base
;
1254 NtCurrentTeb()->Tib
.StackBase
= (char *)view
->base
+ view
->size
;
1255 NtCurrentTeb()->Tib
.StackLimit
= (char *)view
->base
+ 2 * page_size
;
1258 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1263 /***********************************************************************
1264 * virtual_clear_thread_stack
1266 * Clear the stack contents before calling the main entry point, some broken apps need that.
1268 void virtual_clear_thread_stack(void)
1270 void *stack
= NtCurrentTeb()->Tib
.StackLimit
;
1271 size_t size
= (char *)NtCurrentTeb()->Tib
.StackBase
- (char *)NtCurrentTeb()->Tib
.StackLimit
;
1273 wine_anon_mmap( stack
, size
, PROT_READ
| PROT_WRITE
, MAP_FIXED
);
1274 if (force_exec_prot
) mprotect( stack
, size
, PROT_READ
| PROT_WRITE
| PROT_EXEC
);
1278 /***********************************************************************
1279 * VIRTUAL_HandleFault
1281 NTSTATUS
VIRTUAL_HandleFault( LPCVOID addr
)
1284 NTSTATUS ret
= STATUS_ACCESS_VIOLATION
;
1287 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1288 if ((view
= VIRTUAL_FindView( addr
)))
1290 void *page
= ROUND_ADDR( addr
, page_mask
);
1291 BYTE vprot
= view
->prot
[((const char *)page
- (const char *)view
->base
) >> page_shift
];
1292 if (vprot
& VPROT_GUARD
)
1294 VIRTUAL_SetProt( view
, page
, page_size
, vprot
& ~VPROT_GUARD
);
1295 ret
= STATUS_GUARD_PAGE_VIOLATION
;
1298 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1304 /***********************************************************************
1305 * virtual_handle_stack_fault
1307 * Handle an access fault inside the current thread stack.
1308 * Called from inside a signal handler.
1310 BOOL
virtual_handle_stack_fault( void *addr
)
1315 RtlEnterCriticalSection( &csVirtual
); /* no need for signal masking inside signal handler */
1316 if ((view
= VIRTUAL_FindView( addr
)))
1318 void *page
= ROUND_ADDR( addr
, page_mask
);
1319 BYTE vprot
= view
->prot
[((const char *)page
- (const char *)view
->base
) >> page_shift
];
1320 if (vprot
& VPROT_GUARD
)
1322 VIRTUAL_SetProt( view
, page
, page_size
, vprot
& ~VPROT_GUARD
);
1323 if ((char *)page
+ page_size
== NtCurrentTeb()->Tib
.StackLimit
)
1324 NtCurrentTeb()->Tib
.StackLimit
= page
;
1328 RtlLeaveCriticalSection( &csVirtual
);
1333 /***********************************************************************
1334 * VIRTUAL_SetForceExec
1336 * Whether to force exec prot on all views.
1338 void VIRTUAL_SetForceExec( BOOL enable
)
1340 struct file_view
*view
;
1343 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1344 if (!force_exec_prot
!= !enable
) /* change all existing views */
1346 force_exec_prot
= enable
;
1348 LIST_FOR_EACH_ENTRY( view
, &views_list
, struct file_view
, entry
)
1352 char *addr
= view
->base
;
1353 BYTE prot
= view
->prot
[0];
1355 for (count
= i
= 1; i
< view
->size
>> page_shift
; i
++, count
++)
1357 if (view
->prot
[i
] == prot
) continue;
1358 unix_prot
= VIRTUAL_GetUnixProt( prot
);
1359 if ((unix_prot
& PROT_READ
) && !(unix_prot
& PROT_EXEC
))
1361 TRACE( "%s exec prot for %p-%p\n",
1362 force_exec_prot
? "enabling" : "disabling",
1363 addr
, addr
+ (count
<< page_shift
) - 1 );
1364 mprotect( addr
, count
<< page_shift
,
1365 unix_prot
| (force_exec_prot
? PROT_EXEC
: 0) );
1367 addr
+= (count
<< page_shift
);
1368 prot
= view
->prot
[i
];
1373 unix_prot
= VIRTUAL_GetUnixProt( prot
);
1374 if ((unix_prot
& PROT_READ
) && !(unix_prot
& PROT_EXEC
))
1376 TRACE( "%s exec prot for %p-%p\n",
1377 force_exec_prot
? "enabling" : "disabling",
1378 addr
, addr
+ (count
<< page_shift
) - 1 );
1379 mprotect( addr
, count
<< page_shift
,
1380 unix_prot
| (force_exec_prot
? PROT_EXEC
: 0) );
1385 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1389 /***********************************************************************
1390 * VIRTUAL_UseLargeAddressSpace
1392 * Increase the address space size for apps that support it.
1394 void VIRTUAL_UseLargeAddressSpace(void)
1396 /* no large address space on win9x */
1397 if (NtCurrentTeb()->Peb
->OSPlatformId
!= VER_PLATFORM_WIN32_NT
) return;
1398 user_space_limit
= address_space_limit
;
1402 /***********************************************************************
1403 * NtAllocateVirtualMemory (NTDLL.@)
1404 * ZwAllocateVirtualMemory (NTDLL.@)
1406 NTSTATUS WINAPI
NtAllocateVirtualMemory( HANDLE process
, PVOID
*ret
, ULONG zero_bits
,
1407 SIZE_T
*size_ptr
, ULONG type
, ULONG protect
)
1411 SIZE_T size
= *size_ptr
;
1412 SIZE_T mask
= get_mask( zero_bits
);
1413 NTSTATUS status
= STATUS_SUCCESS
;
1414 struct file_view
*view
;
1417 TRACE("%p %p %08lx %x %08x\n", process
, *ret
, size
, type
, protect
);
1419 if (!size
) return STATUS_INVALID_PARAMETER
;
1421 if (process
!= NtCurrentProcess())
1424 apc_result_t result
;
1426 memset( &call
, 0, sizeof(call
) );
1428 call
.virtual_alloc
.type
= APC_VIRTUAL_ALLOC
;
1429 call
.virtual_alloc
.addr
= *ret
;
1430 call
.virtual_alloc
.size
= *size_ptr
;
1431 call
.virtual_alloc
.zero_bits
= zero_bits
;
1432 call
.virtual_alloc
.op_type
= type
;
1433 call
.virtual_alloc
.prot
= protect
;
1434 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
1435 if (status
!= STATUS_SUCCESS
) return status
;
1437 if (result
.virtual_alloc
.status
== STATUS_SUCCESS
)
1439 *ret
= result
.virtual_alloc
.addr
;
1440 *size_ptr
= result
.virtual_alloc
.size
;
1442 return result
.virtual_alloc
.status
;
1445 /* Round parameters to a page boundary */
1447 if (size
> 0x7fc00000) return STATUS_WORKING_SET_LIMIT_RANGE
; /* 2Gb - 4Mb */
1451 if (type
& MEM_RESERVE
) /* Round down to 64k boundary */
1452 base
= ROUND_ADDR( *ret
, mask
);
1454 base
= ROUND_ADDR( *ret
, page_mask
);
1455 size
= (((UINT_PTR
)*ret
+ size
+ page_mask
) & ~page_mask
) - (UINT_PTR
)base
;
1457 /* disallow low 64k, wrap-around and kernel space */
1458 if (((char *)base
< (char *)0x10000) ||
1459 ((char *)base
+ size
< (char *)base
) ||
1460 is_beyond_limit( base
, size
, address_space_limit
))
1461 return STATUS_INVALID_PARAMETER
;
1466 size
= (size
+ page_mask
) & ~page_mask
;
1469 /* Compute the alloc type flags */
1471 if (!(type
& MEM_SYSTEM
))
1473 if (!(type
& (MEM_COMMIT
| MEM_RESERVE
)) ||
1474 (type
& ~(MEM_COMMIT
| MEM_RESERVE
| MEM_TOP_DOWN
| MEM_WRITE_WATCH
| MEM_RESET
)))
1476 WARN("called with wrong alloc type flags (%08x) !\n", type
);
1477 return STATUS_INVALID_PARAMETER
;
1479 if (type
& MEM_WRITE_WATCH
)
1481 FIXME("MEM_WRITE_WATCH type not supported\n");
1482 return STATUS_NOT_SUPPORTED
;
1485 vprot
= VIRTUAL_GetProt( protect
);
1486 if (type
& MEM_COMMIT
) vprot
|= VPROT_COMMITTED
;
1488 /* Reserve the memory */
1490 if (use_locks
) server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1492 if (type
& MEM_SYSTEM
)
1494 if (type
& MEM_IMAGE
) vprot
|= VPROT_IMAGE
;
1495 status
= create_view( &view
, base
, size
, vprot
| VPROT_COMMITTED
);
1496 if (status
== STATUS_SUCCESS
)
1498 view
->flags
|= VFLAG_VALLOC
| VFLAG_SYSTEM
;
1502 else if ((type
& MEM_RESERVE
) || !base
)
1504 status
= map_view( &view
, base
, size
, mask
, type
& MEM_TOP_DOWN
, vprot
);
1505 if (status
== STATUS_SUCCESS
)
1507 view
->flags
|= VFLAG_VALLOC
;
1511 else /* commit the pages */
1513 if (!(view
= VIRTUAL_FindView( base
)) ||
1514 ((char *)base
+ size
> (char *)view
->base
+ view
->size
)) status
= STATUS_NOT_MAPPED_VIEW
;
1515 else if (!VIRTUAL_SetProt( view
, base
, size
, vprot
)) status
= STATUS_ACCESS_DENIED
;
1518 if (use_locks
) server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1520 if (status
== STATUS_SUCCESS
)
1529 /***********************************************************************
1530 * NtFreeVirtualMemory (NTDLL.@)
1531 * ZwFreeVirtualMemory (NTDLL.@)
1533 NTSTATUS WINAPI
NtFreeVirtualMemory( HANDLE process
, PVOID
*addr_ptr
, SIZE_T
*size_ptr
, ULONG type
)
1538 NTSTATUS status
= STATUS_SUCCESS
;
1539 LPVOID addr
= *addr_ptr
;
1540 SIZE_T size
= *size_ptr
;
1542 TRACE("%p %p %08lx %x\n", process
, addr
, size
, type
);
1544 if (process
!= NtCurrentProcess())
1547 apc_result_t result
;
1549 memset( &call
, 0, sizeof(call
) );
1551 call
.virtual_free
.type
= APC_VIRTUAL_FREE
;
1552 call
.virtual_free
.addr
= addr
;
1553 call
.virtual_free
.size
= size
;
1554 call
.virtual_free
.op_type
= type
;
1555 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
1556 if (status
!= STATUS_SUCCESS
) return status
;
1558 if (result
.virtual_free
.status
== STATUS_SUCCESS
)
1560 *addr_ptr
= result
.virtual_free
.addr
;
1561 *size_ptr
= result
.virtual_free
.size
;
1563 return result
.virtual_free
.status
;
1566 /* Fix the parameters */
1568 size
= ROUND_SIZE( addr
, size
);
1569 base
= ROUND_ADDR( addr
, page_mask
);
1571 /* avoid freeing the DOS area when a broken app passes a NULL pointer */
1572 if (!base
&& !(type
& MEM_SYSTEM
)) return STATUS_INVALID_PARAMETER
;
1574 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1576 if (!(view
= VIRTUAL_FindView( base
)) ||
1577 (base
+ size
> (char *)view
->base
+ view
->size
) ||
1578 !(view
->flags
& VFLAG_VALLOC
))
1580 status
= STATUS_INVALID_PARAMETER
;
1582 else if (type
& MEM_SYSTEM
)
1584 /* return the values that the caller should use to unmap the area */
1585 *addr_ptr
= view
->base
;
1586 if (!wine_mmap_is_in_reserved_area( view
->base
, view
->size
)) *size_ptr
= view
->size
;
1587 else *size_ptr
= 0; /* make sure we don't munmap anything from a reserved area */
1588 view
->flags
|= VFLAG_SYSTEM
;
1589 delete_view( view
);
1591 else if (type
== MEM_RELEASE
)
1593 /* Free the pages */
1595 if (size
|| (base
!= view
->base
)) status
= STATUS_INVALID_PARAMETER
;
1598 delete_view( view
);
1603 else if (type
== MEM_DECOMMIT
)
1605 status
= decommit_pages( view
, base
- (char *)view
->base
, size
);
1606 if (status
== STATUS_SUCCESS
)
1614 WARN("called with wrong free type flags (%08x) !\n", type
);
1615 status
= STATUS_INVALID_PARAMETER
;
1618 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1623 /***********************************************************************
1624 * NtProtectVirtualMemory (NTDLL.@)
1625 * ZwProtectVirtualMemory (NTDLL.@)
1627 NTSTATUS WINAPI
NtProtectVirtualMemory( HANDLE process
, PVOID
*addr_ptr
, SIZE_T
*size_ptr
,
1628 ULONG new_prot
, ULONG
*old_prot
)
1632 NTSTATUS status
= STATUS_SUCCESS
;
1637 SIZE_T size
= *size_ptr
;
1638 LPVOID addr
= *addr_ptr
;
1640 TRACE("%p %p %08lx %08x\n", process
, addr
, size
, new_prot
);
1642 if (process
!= NtCurrentProcess())
1645 apc_result_t result
;
1647 memset( &call
, 0, sizeof(call
) );
1649 call
.virtual_protect
.type
= APC_VIRTUAL_PROTECT
;
1650 call
.virtual_protect
.addr
= addr
;
1651 call
.virtual_protect
.size
= size
;
1652 call
.virtual_protect
.prot
= new_prot
;
1653 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
1654 if (status
!= STATUS_SUCCESS
) return status
;
1656 if (result
.virtual_protect
.status
== STATUS_SUCCESS
)
1658 *addr_ptr
= result
.virtual_protect
.addr
;
1659 *size_ptr
= result
.virtual_protect
.size
;
1660 if (old_prot
) *old_prot
= result
.virtual_protect
.prot
;
1662 return result
.virtual_protect
.status
;
1665 /* Fix the parameters */
1667 size
= ROUND_SIZE( addr
, size
);
1668 base
= ROUND_ADDR( addr
, page_mask
);
1670 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1672 if (!(view
= VIRTUAL_FindView( base
)) || (base
+ size
> (char *)view
->base
+ view
->size
))
1674 status
= STATUS_INVALID_PARAMETER
;
1678 /* Make sure all the pages are committed */
1680 p
= view
->prot
+ ((base
- (char *)view
->base
) >> page_shift
);
1681 prot
= VIRTUAL_GetWin32Prot( *p
);
1682 for (i
= size
>> page_shift
; i
; i
--, p
++)
1684 if (!(*p
& VPROT_COMMITTED
))
1686 status
= STATUS_NOT_COMMITTED
;
1692 if (old_prot
) *old_prot
= prot
;
1693 vprot
= VIRTUAL_GetProt( new_prot
) | VPROT_COMMITTED
;
1694 if (!VIRTUAL_SetProt( view
, base
, size
, vprot
)) status
= STATUS_ACCESS_DENIED
;
1697 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1699 if (status
== STATUS_SUCCESS
)
1707 #define UNIMPLEMENTED_INFO_CLASS(c) \
1709 FIXME("(process=%p,addr=%p) Unimplemented information class: " #c "\n", process, addr); \
1710 return STATUS_INVALID_INFO_CLASS
1712 /***********************************************************************
1713 * NtQueryVirtualMemory (NTDLL.@)
1714 * ZwQueryVirtualMemory (NTDLL.@)
1716 NTSTATUS WINAPI
NtQueryVirtualMemory( HANDLE process
, LPCVOID addr
,
1717 MEMORY_INFORMATION_CLASS info_class
, PVOID buffer
,
1718 SIZE_T len
, SIZE_T
*res_len
)
1721 char *base
, *alloc_base
= 0;
1724 MEMORY_BASIC_INFORMATION
*info
= buffer
;
1727 if (info_class
!= MemoryBasicInformation
)
1731 UNIMPLEMENTED_INFO_CLASS(MemoryWorkingSetList
);
1732 UNIMPLEMENTED_INFO_CLASS(MemorySectionName
);
1733 UNIMPLEMENTED_INFO_CLASS(MemoryBasicVlmInformation
);
1736 FIXME("(%p,%p,info_class=%d,%p,%ld,%p) Unknown information class\n",
1737 process
, addr
, info_class
, buffer
, len
, res_len
);
1738 return STATUS_INVALID_INFO_CLASS
;
1741 if (address_space_limit
&& addr
>= address_space_limit
)
1742 return STATUS_WORKING_SET_LIMIT_RANGE
;
1744 if (process
!= NtCurrentProcess())
1748 apc_result_t result
;
1750 memset( &call
, 0, sizeof(call
) );
1752 call
.virtual_query
.type
= APC_VIRTUAL_QUERY
;
1753 call
.virtual_query
.addr
= addr
;
1754 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
1755 if (status
!= STATUS_SUCCESS
) return status
;
1757 if (result
.virtual_query
.status
== STATUS_SUCCESS
)
1759 info
->BaseAddress
= result
.virtual_query
.base
;
1760 info
->AllocationBase
= result
.virtual_query
.alloc_base
;
1761 info
->RegionSize
= result
.virtual_query
.size
;
1762 info
->State
= result
.virtual_query
.state
;
1763 info
->Protect
= result
.virtual_query
.prot
;
1764 info
->AllocationProtect
= result
.virtual_query
.alloc_prot
;
1765 info
->Type
= result
.virtual_query
.alloc_type
;
1766 if (res_len
) *res_len
= sizeof(*info
);
1768 return result
.virtual_query
.status
;
1771 base
= ROUND_ADDR( addr
, page_mask
);
1773 /* Find the view containing the address */
1775 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
1776 ptr
= list_head( &views_list
);
1781 /* make the address space end at the user limit, except if
1782 * the last view was mapped beyond that */
1783 if (alloc_base
<= (char *)user_space_limit
)
1785 if (user_space_limit
&& base
>= (char *)user_space_limit
)
1787 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1788 return STATUS_WORKING_SET_LIMIT_RANGE
;
1790 size
= (char *)user_space_limit
- alloc_base
;
1792 else size
= (char *)address_space_limit
- alloc_base
;
1796 view
= LIST_ENTRY( ptr
, struct file_view
, entry
);
1797 if ((char *)view
->base
> base
)
1799 size
= (char *)view
->base
- alloc_base
;
1803 if ((char *)view
->base
+ view
->size
> base
)
1805 alloc_base
= view
->base
;
1809 alloc_base
= (char *)view
->base
+ view
->size
;
1810 ptr
= list_next( &views_list
, ptr
);
1813 /* Fill the info structure */
1817 info
->State
= MEM_FREE
;
1818 info
->Protect
= PAGE_NOACCESS
;
1819 info
->AllocationBase
= 0;
1820 info
->AllocationProtect
= 0;
1825 BYTE vprot
= view
->prot
[(base
- alloc_base
) >> page_shift
];
1826 info
->State
= (vprot
& VPROT_COMMITTED
) ? MEM_COMMIT
: MEM_RESERVE
;
1827 info
->Protect
= VIRTUAL_GetWin32Prot( vprot
);
1828 info
->AllocationBase
= alloc_base
;
1829 info
->AllocationProtect
= VIRTUAL_GetWin32Prot( view
->protect
);
1830 if (view
->protect
& VPROT_IMAGE
) info
->Type
= MEM_IMAGE
;
1831 else if (view
->flags
& VFLAG_VALLOC
) info
->Type
= MEM_PRIVATE
;
1832 else info
->Type
= MEM_MAPPED
;
1833 for (size
= base
- alloc_base
; size
< view
->size
; size
+= page_size
)
1834 if (view
->prot
[size
>> page_shift
] != vprot
) break;
1836 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
1838 info
->BaseAddress
= base
;
1839 info
->RegionSize
= size
- (base
- alloc_base
);
1840 if (res_len
) *res_len
= sizeof(*info
);
1841 return STATUS_SUCCESS
;
1845 /***********************************************************************
1846 * NtLockVirtualMemory (NTDLL.@)
1847 * ZwLockVirtualMemory (NTDLL.@)
1849 NTSTATUS WINAPI
NtLockVirtualMemory( HANDLE process
, PVOID
*addr
, SIZE_T
*size
, ULONG unknown
)
1851 NTSTATUS status
= STATUS_SUCCESS
;
1853 if (process
!= NtCurrentProcess())
1856 apc_result_t result
;
1858 memset( &call
, 0, sizeof(call
) );
1860 call
.virtual_lock
.type
= APC_VIRTUAL_LOCK
;
1861 call
.virtual_lock
.addr
= *addr
;
1862 call
.virtual_lock
.size
= *size
;
1863 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
1864 if (status
!= STATUS_SUCCESS
) return status
;
1866 if (result
.virtual_lock
.status
== STATUS_SUCCESS
)
1868 *addr
= result
.virtual_lock
.addr
;
1869 *size
= result
.virtual_lock
.size
;
1871 return result
.virtual_lock
.status
;
1874 *size
= ROUND_SIZE( *addr
, *size
);
1875 *addr
= ROUND_ADDR( *addr
, page_mask
);
1877 if (mlock( *addr
, *size
)) status
= STATUS_ACCESS_DENIED
;
1882 /***********************************************************************
1883 * NtUnlockVirtualMemory (NTDLL.@)
1884 * ZwUnlockVirtualMemory (NTDLL.@)
1886 NTSTATUS WINAPI
NtUnlockVirtualMemory( HANDLE process
, PVOID
*addr
, SIZE_T
*size
, ULONG unknown
)
1888 NTSTATUS status
= STATUS_SUCCESS
;
1890 if (process
!= NtCurrentProcess())
1893 apc_result_t result
;
1895 memset( &call
, 0, sizeof(call
) );
1897 call
.virtual_unlock
.type
= APC_VIRTUAL_UNLOCK
;
1898 call
.virtual_unlock
.addr
= *addr
;
1899 call
.virtual_unlock
.size
= *size
;
1900 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
1901 if (status
!= STATUS_SUCCESS
) return status
;
1903 if (result
.virtual_unlock
.status
== STATUS_SUCCESS
)
1905 *addr
= result
.virtual_unlock
.addr
;
1906 *size
= result
.virtual_unlock
.size
;
1908 return result
.virtual_unlock
.status
;
1911 *size
= ROUND_SIZE( *addr
, *size
);
1912 *addr
= ROUND_ADDR( *addr
, page_mask
);
1914 if (munlock( *addr
, *size
)) status
= STATUS_ACCESS_DENIED
;
1919 /***********************************************************************
1920 * NtCreateSection (NTDLL.@)
1921 * ZwCreateSection (NTDLL.@)
1923 NTSTATUS WINAPI
NtCreateSection( HANDLE
*handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
,
1924 const LARGE_INTEGER
*size
, ULONG protect
,
1925 ULONG sec_flags
, HANDLE file
)
1929 DWORD len
= (attr
&& attr
->ObjectName
) ? attr
->ObjectName
->Length
: 0;
1930 struct security_descriptor
*sd
= NULL
;
1931 struct object_attributes objattr
;
1933 /* Check parameters */
1935 if (len
> MAX_PATH
*sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
1937 objattr
.rootdir
= attr
? attr
->RootDirectory
: 0;
1939 objattr
.name_len
= len
;
1942 ret
= NTDLL_create_struct_sd( attr
->SecurityDescriptor
, &sd
, &objattr
.sd_len
);
1943 if (ret
!= STATUS_SUCCESS
) return ret
;
1946 vprot
= VIRTUAL_GetProt( protect
);
1947 if (sec_flags
& SEC_RESERVE
)
1949 if (file
) return STATUS_INVALID_PARAMETER
;
1951 else vprot
|= VPROT_COMMITTED
;
1952 if (sec_flags
& SEC_NOCACHE
) vprot
|= VPROT_NOCACHE
;
1953 if (sec_flags
& SEC_IMAGE
) vprot
|= VPROT_IMAGE
;
1955 /* Create the server object */
1957 SERVER_START_REQ( create_mapping
)
1959 req
->access
= access
;
1960 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
1961 req
->file_handle
= file
;
1962 req
->size
= size
? size
->QuadPart
: 0;
1963 req
->protect
= vprot
;
1964 wine_server_add_data( req
, &objattr
, sizeof(objattr
) );
1965 if (objattr
.sd_len
) wine_server_add_data( req
, sd
, objattr
.sd_len
);
1966 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
1967 ret
= wine_server_call( req
);
1968 *handle
= reply
->handle
;
1972 NTDLL_free_struct_sd( sd
);
1978 /***********************************************************************
1979 * NtOpenSection (NTDLL.@)
1980 * ZwOpenSection (NTDLL.@)
1982 NTSTATUS WINAPI
NtOpenSection( HANDLE
*handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
)
1985 DWORD len
= attr
->ObjectName
->Length
;
1987 if (len
> MAX_PATH
*sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
1989 SERVER_START_REQ( open_mapping
)
1991 req
->access
= access
;
1992 req
->attributes
= attr
->Attributes
;
1993 req
->rootdir
= attr
->RootDirectory
;
1994 wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
1995 if (!(ret
= wine_server_call( req
))) *handle
= reply
->handle
;
2002 /***********************************************************************
2003 * NtMapViewOfSection (NTDLL.@)
2004 * ZwMapViewOfSection (NTDLL.@)
2006 NTSTATUS WINAPI
NtMapViewOfSection( HANDLE handle
, HANDLE process
, PVOID
*addr_ptr
, ULONG zero_bits
,
2007 SIZE_T commit_size
, const LARGE_INTEGER
*offset_ptr
, SIZE_T
*size_ptr
,
2008 SECTION_INHERIT inherit
, ULONG alloc_type
, ULONG protect
)
2011 ULONGLONG full_size
;
2013 SIZE_T mask
= get_mask( zero_bits
);
2014 int unix_handle
= -1, needs_close
;
2017 struct file_view
*view
;
2019 HANDLE dup_mapping
, shared_file
;
2020 LARGE_INTEGER offset
;
2023 offset
.QuadPart
= offset_ptr
? offset_ptr
->QuadPart
: 0;
2025 TRACE("handle=%p process=%p addr=%p off=%x%08x size=%lx access=%x\n",
2026 handle
, process
, *addr_ptr
, offset
.u
.HighPart
, offset
.u
.LowPart
, size
, protect
);
2028 /* Check parameters */
2030 if ((offset
.u
.LowPart
& mask
) || (*addr_ptr
&& ((UINT_PTR
)*addr_ptr
& mask
)))
2031 return STATUS_INVALID_PARAMETER
;
2033 if (process
!= NtCurrentProcess())
2036 apc_result_t result
;
2038 memset( &call
, 0, sizeof(call
) );
2040 call
.map_view
.type
= APC_MAP_VIEW
;
2041 call
.map_view
.handle
= handle
;
2042 call
.map_view
.addr
= *addr_ptr
;
2043 call
.map_view
.size
= *size_ptr
;
2044 call
.map_view
.offset
= offset
.QuadPart
;
2045 call
.map_view
.zero_bits
= zero_bits
;
2046 call
.map_view
.alloc_type
= alloc_type
;
2047 call
.map_view
.prot
= protect
;
2048 res
= NTDLL_queue_process_apc( process
, &call
, &result
);
2049 if (res
!= STATUS_SUCCESS
) return res
;
2051 if (result
.map_view
.status
== STATUS_SUCCESS
)
2053 *addr_ptr
= result
.map_view
.addr
;
2054 *size_ptr
= result
.map_view
.size
;
2056 return result
.map_view
.status
;
2059 SERVER_START_REQ( get_mapping_info
)
2061 req
->handle
= handle
;
2062 res
= wine_server_call( req
);
2063 prot
= reply
->protect
;
2065 full_size
= reply
->size
;
2066 header_size
= reply
->header_size
;
2067 dup_mapping
= reply
->mapping
;
2068 shared_file
= reply
->shared_file
;
2071 if (res
) return res
;
2074 if (sizeof(size
) < sizeof(full_size
) && (size
!= full_size
))
2075 ERR( "Sizes larger than 4Gb (%x%08x) not supported on this platform\n",
2076 (DWORD
)(full_size
>> 32), (DWORD
)full_size
);
2078 if ((res
= server_get_unix_fd( handle
, 0, &unix_handle
, &needs_close
, NULL
, NULL
))) goto done
;
2080 if (prot
& VPROT_IMAGE
)
2084 int shared_fd
, shared_needs_close
;
2086 if ((res
= server_get_unix_fd( shared_file
, FILE_READ_DATA
|FILE_WRITE_DATA
,
2087 &shared_fd
, &shared_needs_close
, NULL
, NULL
))) goto done
;
2088 res
= map_image( handle
, unix_handle
, base
, size
, mask
, header_size
,
2089 shared_fd
, dup_mapping
, addr_ptr
);
2090 if (shared_needs_close
) close( shared_fd
);
2091 NtClose( shared_file
);
2095 res
= map_image( handle
, unix_handle
, base
, size
, mask
, header_size
,
2096 -1, dup_mapping
, addr_ptr
);
2098 if (needs_close
) close( unix_handle
);
2099 if (!res
) *size_ptr
= size
;
2103 if ((offset
.QuadPart
>= size
) || (*size_ptr
> size
- offset
.QuadPart
))
2105 res
= STATUS_INVALID_PARAMETER
;
2108 if (*size_ptr
) size
= ROUND_SIZE( offset
.u
.LowPart
, *size_ptr
);
2109 else size
= size
- offset
.QuadPart
;
2115 case PAGE_READWRITE
:
2116 case PAGE_EXECUTE_READWRITE
:
2117 if (!(prot
& VPROT_WRITE
))
2119 res
= STATUS_INVALID_PARAMETER
;
2124 case PAGE_WRITECOPY
:
2126 case PAGE_EXECUTE_READ
:
2127 case PAGE_EXECUTE_WRITECOPY
:
2128 if (prot
& VPROT_READ
) break;
2131 res
= STATUS_INVALID_PARAMETER
;
2135 /* FIXME: If a mapping is created with SEC_RESERVE and a process,
2136 * which has a view of this mapping commits some pages, they will
2137 * appear committed in all other processes, which have the same
2138 * view created. Since we don't support this yet, we create the
2139 * whole mapping committed.
2141 prot
|= VPROT_COMMITTED
;
2143 /* Reserve a properly aligned area */
2145 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2147 res
= map_view( &view
, *addr_ptr
, size
, mask
, FALSE
, prot
);
2150 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2156 TRACE("handle=%p size=%lx offset=%x%08x\n",
2157 handle
, size
, offset
.u
.HighPart
, offset
.u
.LowPart
);
2159 res
= map_file_into_view( view
, unix_handle
, 0, size
, offset
.QuadPart
, prot
, !dup_mapping
);
2160 if (res
== STATUS_SUCCESS
)
2162 *addr_ptr
= view
->base
;
2164 view
->mapping
= dup_mapping
;
2165 dup_mapping
= 0; /* don't close it */
2169 ERR( "map_file_into_view %p %lx %x%08x failed\n",
2170 view
->base
, size
, offset
.u
.HighPart
, offset
.u
.LowPart
);
2171 delete_view( view
);
2174 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2177 if (dup_mapping
) NtClose( dup_mapping
);
2178 if (needs_close
) close( unix_handle
);
2183 /***********************************************************************
2184 * NtUnmapViewOfSection (NTDLL.@)
2185 * ZwUnmapViewOfSection (NTDLL.@)
2187 NTSTATUS WINAPI
NtUnmapViewOfSection( HANDLE process
, PVOID addr
)
2190 NTSTATUS status
= STATUS_INVALID_PARAMETER
;
2192 void *base
= ROUND_ADDR( addr
, page_mask
);
2194 if (process
!= NtCurrentProcess())
2197 apc_result_t result
;
2199 memset( &call
, 0, sizeof(call
) );
2201 call
.unmap_view
.type
= APC_UNMAP_VIEW
;
2202 call
.unmap_view
.addr
= addr
;
2203 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
2204 if (status
== STATUS_SUCCESS
) status
= result
.unmap_view
.status
;
2208 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2209 if ((view
= VIRTUAL_FindView( base
)) && (base
== view
->base
))
2211 delete_view( view
);
2212 status
= STATUS_SUCCESS
;
2214 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2219 /***********************************************************************
2220 * NtFlushVirtualMemory (NTDLL.@)
2221 * ZwFlushVirtualMemory (NTDLL.@)
2223 NTSTATUS WINAPI
NtFlushVirtualMemory( HANDLE process
, LPCVOID
*addr_ptr
,
2224 SIZE_T
*size_ptr
, ULONG unknown
)
2227 NTSTATUS status
= STATUS_SUCCESS
;
2229 void *addr
= ROUND_ADDR( *addr_ptr
, page_mask
);
2231 if (process
!= NtCurrentProcess())
2234 apc_result_t result
;
2236 memset( &call
, 0, sizeof(call
) );
2238 call
.virtual_flush
.type
= APC_VIRTUAL_FLUSH
;
2239 call
.virtual_flush
.addr
= addr
;
2240 call
.virtual_flush
.size
= *size_ptr
;
2241 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
2242 if (status
!= STATUS_SUCCESS
) return status
;
2244 if (result
.virtual_flush
.status
== STATUS_SUCCESS
)
2246 *addr_ptr
= result
.virtual_flush
.addr
;
2247 *size_ptr
= result
.virtual_flush
.size
;
2249 return result
.virtual_flush
.status
;
2252 server_enter_uninterrupted_section( &csVirtual
, &sigset
);
2253 if (!(view
= VIRTUAL_FindView( addr
))) status
= STATUS_INVALID_PARAMETER
;
2256 if (!*size_ptr
) *size_ptr
= view
->size
;
2258 if (msync( addr
, *size_ptr
, MS_SYNC
)) status
= STATUS_NOT_MAPPED_DATA
;
2260 server_leave_uninterrupted_section( &csVirtual
, &sigset
);
2265 /***********************************************************************
2266 * NtReadVirtualMemory (NTDLL.@)
2267 * ZwReadVirtualMemory (NTDLL.@)
2269 NTSTATUS WINAPI
NtReadVirtualMemory( HANDLE process
, const void *addr
, void *buffer
,
2270 SIZE_T size
, SIZE_T
*bytes_read
)
2274 SERVER_START_REQ( read_process_memory
)
2276 req
->handle
= process
;
2277 req
->addr
= (void *)addr
;
2278 wine_server_set_reply( req
, buffer
, size
);
2279 if ((status
= wine_server_call( req
))) size
= 0;
2282 if (bytes_read
) *bytes_read
= size
;
2287 /***********************************************************************
2288 * NtWriteVirtualMemory (NTDLL.@)
2289 * ZwWriteVirtualMemory (NTDLL.@)
2291 NTSTATUS WINAPI
NtWriteVirtualMemory( HANDLE process
, void *addr
, const void *buffer
,
2292 SIZE_T size
, SIZE_T
*bytes_written
)
2296 SERVER_START_REQ( write_process_memory
)
2298 req
->handle
= process
;
2300 wine_server_add_data( req
, buffer
, size
);
2301 if ((status
= wine_server_call( req
))) size
= 0;
2304 if (bytes_written
) *bytes_written
= size
;
2309 /***********************************************************************
2310 * NtAreMappedFilesTheSame (NTDLL.@)
2311 * ZwAreMappedFilesTheSame (NTDLL.@)
2313 NTSTATUS WINAPI
NtAreMappedFilesTheSame(PVOID addr1
, PVOID addr2
)
2315 TRACE("%p %p\n", addr1
, addr2
);
2317 return STATUS_NOT_SAME_DEVICE
;