ntdll: Implementation of inter-process VirtualQueryEx.
[wine/wine-kai.git] / dlls / ntdll / virtual.c
blob0407a213078595c41701cedea2ae4c707671eace
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <errno.h>
26 #ifdef HAVE_SYS_ERRNO_H
27 #include <sys/errno.h>
28 #endif
29 #include <fcntl.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
33 #include <stdarg.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <sys/types.h>
38 #ifdef HAVE_SYS_STAT_H
39 # include <sys/stat.h>
40 #endif
41 #ifdef HAVE_SYS_MMAN_H
42 # include <sys/mman.h>
43 #endif
45 #define NONAMELESSUNION
46 #define NONAMELESSSTRUCT
47 #include "ntstatus.h"
48 #define WIN32_NO_STATUS
49 #include "windef.h"
50 #include "winternl.h"
51 #include "winioctl.h"
52 #include "wine/library.h"
53 #include "wine/server.h"
54 #include "wine/list.h"
55 #include "wine/debug.h"
56 #include "ntdll_misc.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(virtual);
59 WINE_DECLARE_DEBUG_CHANNEL(module);
61 #ifndef MS_SYNC
62 #define MS_SYNC 0
63 #endif
65 #ifndef MAP_NORESERVE
66 #define MAP_NORESERVE 0
67 #endif
69 /* File view */
70 typedef struct file_view
72 struct list entry; /* Entry in global view list */
73 void *base; /* Base address */
74 size_t size; /* Size in bytes */
75 HANDLE mapping; /* Handle to the file mapping */
76 BYTE flags; /* Allocation flags (VFLAG_*) */
77 BYTE protect; /* Protection for all pages at allocation time */
78 BYTE prot[1]; /* Protection byte for each page */
79 } FILE_VIEW;
81 /* Per-view flags */
82 #define VFLAG_SYSTEM 0x01 /* system view (underlying mmap not under our control) */
83 #define VFLAG_VALLOC 0x02 /* allocated by VirtualAlloc */
85 /* Conversion from VPROT_* to Win32 flags */
86 static const BYTE VIRTUAL_Win32Flags[16] =
88 PAGE_NOACCESS, /* 0 */
89 PAGE_READONLY, /* READ */
90 PAGE_READWRITE, /* WRITE */
91 PAGE_READWRITE, /* READ | WRITE */
92 PAGE_EXECUTE, /* EXEC */
93 PAGE_EXECUTE_READ, /* READ | EXEC */
94 PAGE_EXECUTE_READWRITE, /* WRITE | EXEC */
95 PAGE_EXECUTE_READWRITE, /* READ | WRITE | EXEC */
96 PAGE_WRITECOPY, /* WRITECOPY */
97 PAGE_WRITECOPY, /* READ | WRITECOPY */
98 PAGE_WRITECOPY, /* WRITE | WRITECOPY */
99 PAGE_WRITECOPY, /* READ | WRITE | WRITECOPY */
100 PAGE_EXECUTE_WRITECOPY, /* EXEC | WRITECOPY */
101 PAGE_EXECUTE_WRITECOPY, /* READ | EXEC | WRITECOPY */
102 PAGE_EXECUTE_WRITECOPY, /* WRITE | EXEC | WRITECOPY */
103 PAGE_EXECUTE_WRITECOPY /* READ | WRITE | EXEC | WRITECOPY */
106 static struct list views_list = LIST_INIT(views_list);
108 static RTL_CRITICAL_SECTION csVirtual;
109 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
111 0, 0, &csVirtual,
112 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
113 0, 0, { (DWORD_PTR)(__FILE__ ": csVirtual") }
115 static RTL_CRITICAL_SECTION csVirtual = { &critsect_debug, -1, 0, 0, 0, 0 };
117 #ifdef __i386__
118 /* These are always the same on an i386, and it will be faster this way */
119 # define page_mask 0xfff
120 # define page_shift 12
121 # define page_size 0x1000
122 /* Note: these are Windows limits, you cannot change them. */
123 # define ADDRESS_SPACE_LIMIT ((void *)0xc0000000) /* top of the total available address space */
124 # define USER_SPACE_LIMIT ((void *)0x7fff0000) /* top of the user address space */
125 #else
126 static UINT page_shift;
127 static UINT page_size;
128 static UINT_PTR page_mask;
129 # define ADDRESS_SPACE_LIMIT 0 /* no limit needed on other platforms */
130 # define USER_SPACE_LIMIT 0 /* no limit needed on other platforms */
131 #endif /* __i386__ */
133 #define ROUND_ADDR(addr,mask) \
134 ((void *)((UINT_PTR)(addr) & ~(UINT_PTR)(mask)))
136 #define ROUND_SIZE(addr,size) \
137 (((UINT)(size) + ((UINT_PTR)(addr) & page_mask) + page_mask) & ~page_mask)
139 #define VIRTUAL_DEBUG_DUMP_VIEW(view) \
140 do { if (TRACE_ON(virtual)) VIRTUAL_DumpView(view); } while (0)
142 static void *user_space_limit = USER_SPACE_LIMIT;
143 static void *preload_reserve_start;
144 static void *preload_reserve_end;
145 static int use_locks;
146 static int force_exec_prot; /* whether to force PROT_EXEC on all PROT_READ mmaps */
149 /***********************************************************************
150 * VIRTUAL_GetProtStr
152 static const char *VIRTUAL_GetProtStr( BYTE prot )
154 static char buffer[6];
155 buffer[0] = (prot & VPROT_COMMITTED) ? 'c' : '-';
156 buffer[1] = (prot & VPROT_GUARD) ? 'g' : '-';
157 buffer[2] = (prot & VPROT_READ) ? 'r' : '-';
158 buffer[3] = (prot & VPROT_WRITECOPY) ? 'W' : ((prot & VPROT_WRITE) ? 'w' : '-');
159 buffer[4] = (prot & VPROT_EXEC) ? 'x' : '-';
160 buffer[5] = 0;
161 return buffer;
165 /***********************************************************************
166 * VIRTUAL_GetUnixProt
168 * Convert page protections to protection for mmap/mprotect.
170 static int VIRTUAL_GetUnixProt( BYTE vprot )
172 int prot = 0;
173 if ((vprot & VPROT_COMMITTED) && !(vprot & VPROT_GUARD))
175 if (vprot & VPROT_READ) prot |= PROT_READ;
176 if (vprot & VPROT_WRITE) prot |= PROT_WRITE;
177 if (vprot & VPROT_WRITECOPY) prot |= PROT_WRITE;
178 if (vprot & VPROT_EXEC) prot |= PROT_EXEC;
180 if (!prot) prot = PROT_NONE;
181 return prot;
185 /***********************************************************************
186 * VIRTUAL_DumpView
188 static void VIRTUAL_DumpView( FILE_VIEW *view )
190 UINT i, count;
191 char *addr = view->base;
192 BYTE prot = view->prot[0];
194 TRACE( "View: %p - %p", addr, addr + view->size - 1 );
195 if (view->flags & VFLAG_SYSTEM)
196 TRACE( " (system)\n" );
197 else if (view->flags & VFLAG_VALLOC)
198 TRACE( " (valloc)\n" );
199 else if (view->mapping)
200 TRACE( " %p\n", view->mapping );
201 else
202 TRACE( " (anonymous)\n");
204 for (count = i = 1; i < view->size >> page_shift; i++, count++)
206 if (view->prot[i] == prot) continue;
207 TRACE( " %p - %p %s\n",
208 addr, addr + (count << page_shift) - 1, VIRTUAL_GetProtStr(prot) );
209 addr += (count << page_shift);
210 prot = view->prot[i];
211 count = 0;
213 if (count)
214 TRACE( " %p - %p %s\n",
215 addr, addr + (count << page_shift) - 1, VIRTUAL_GetProtStr(prot) );
219 /***********************************************************************
220 * VIRTUAL_Dump
222 #if WINE_VM_DEBUG
223 static void VIRTUAL_Dump(void)
225 sigset_t sigset;
226 struct file_view *view;
228 TRACE( "Dump of all virtual memory views:\n" );
229 server_enter_uninterrupted_section( &csVirtual, &sigset );
230 LIST_FOR_EACH_ENTRY( view, &views_list, FILE_VIEW, entry )
232 VIRTUAL_DumpView( view );
234 server_leave_uninterrupted_section( &csVirtual, &sigset );
236 #endif
239 /***********************************************************************
240 * VIRTUAL_FindView
242 * Find the view containing a given address. The csVirtual section must be held by caller.
244 * PARAMS
245 * addr [I] Address
247 * RETURNS
248 * View: Success
249 * NULL: Failure
251 static struct file_view *VIRTUAL_FindView( const void *addr )
253 struct file_view *view;
255 LIST_FOR_EACH_ENTRY( view, &views_list, struct file_view, entry )
257 if (view->base > addr) break;
258 if ((const char*)view->base + view->size > (const char*)addr) return view;
260 return NULL;
264 /***********************************************************************
265 * get_mask
267 static inline UINT_PTR get_mask( ULONG zero_bits )
269 if (!zero_bits) return 0xffff; /* allocations are aligned to 64K by default */
270 if (zero_bits < page_shift) zero_bits = page_shift;
271 return (1 << zero_bits) - 1;
275 /***********************************************************************
276 * find_view_range
278 * Find the first view overlapping at least part of the specified range.
279 * The csVirtual section must be held by caller.
281 static struct file_view *find_view_range( const void *addr, size_t size )
283 struct file_view *view;
285 LIST_FOR_EACH_ENTRY( view, &views_list, struct file_view, entry )
287 if ((const char *)view->base >= (const char *)addr + size) break;
288 if ((const char *)view->base + view->size > (const char *)addr) return view;
290 return NULL;
294 /***********************************************************************
295 * find_free_area
297 * Find a free area between views inside the specified range.
298 * The csVirtual section must be held by caller.
300 static void *find_free_area( void *base, void *end, size_t size, size_t mask, int top_down )
302 struct list *ptr;
303 void *start;
305 if (top_down)
307 start = ROUND_ADDR( (char *)end - size, mask );
308 if (start >= end || start < base) return NULL;
310 for (ptr = views_list.prev; ptr != &views_list; ptr = ptr->prev)
312 struct file_view *view = LIST_ENTRY( ptr, struct file_view, entry );
314 if ((char *)view->base + view->size <= (char *)start) break;
315 if ((char *)view->base >= (char *)start + size) continue;
316 start = ROUND_ADDR( (char *)view->base - size, mask );
317 /* stop if remaining space is not large enough */
318 if (!start || start >= end || start < base) return NULL;
321 else
323 start = ROUND_ADDR( (char *)base + mask, mask );
324 if (start >= end || (char *)end - (char *)start < size) return NULL;
326 for (ptr = views_list.next; ptr != &views_list; ptr = ptr->next)
328 struct file_view *view = LIST_ENTRY( ptr, struct file_view, entry );
330 if ((char *)view->base >= (char *)start + size) break;
331 if ((char *)view->base + view->size <= (char *)start) continue;
332 start = ROUND_ADDR( (char *)view->base + view->size + mask, mask );
333 /* stop if remaining space is not large enough */
334 if (!start || start >= end || (char *)end - (char *)start < size) return NULL;
337 return start;
341 /***********************************************************************
342 * add_reserved_area
344 * Add a reserved area to the list maintained by libwine.
345 * The csVirtual section must be held by caller.
347 static void add_reserved_area( void *addr, size_t size )
349 TRACE( "adding %p-%p\n", addr, (char *)addr + size );
351 if (addr < user_space_limit)
353 /* unmap the part of the area that is below the limit */
354 assert( (char *)addr + size > (char *)user_space_limit );
355 munmap( addr, (char *)user_space_limit - (char *)addr );
356 size -= (char *)user_space_limit - (char *)addr;
357 addr = user_space_limit;
359 /* blow away existing mappings */
360 wine_anon_mmap( addr, size, PROT_NONE, MAP_NORESERVE | MAP_FIXED );
361 wine_mmap_add_reserved_area( addr, size );
365 /***********************************************************************
366 * is_beyond_limit
368 * Check if an address range goes beyond a given limit.
370 static inline int is_beyond_limit( void *addr, size_t size, void *limit )
372 return (limit && (addr >= limit || (char *)addr + size > (char *)limit));
376 /***********************************************************************
377 * unmap_area
379 * Unmap an area, or simply replace it by an empty mapping if it is
380 * in a reserved area. The csVirtual section must be held by caller.
382 static inline void unmap_area( void *addr, size_t size )
384 if (wine_mmap_is_in_reserved_area( addr, size ))
385 wine_anon_mmap( addr, size, PROT_NONE, MAP_NORESERVE | MAP_FIXED );
386 else if (is_beyond_limit( addr, size, user_space_limit ))
387 add_reserved_area( addr, size );
388 else
389 munmap( addr, size );
393 /***********************************************************************
394 * delete_view
396 * Deletes a view. The csVirtual section must be held by caller.
398 static void delete_view( struct file_view *view ) /* [in] View */
400 if (!(view->flags & VFLAG_SYSTEM)) unmap_area( view->base, view->size );
401 list_remove( &view->entry );
402 if (view->mapping) NtClose( view->mapping );
403 free( view );
407 /***********************************************************************
408 * create_view
410 * Create a view. The csVirtual section must be held by caller.
412 static NTSTATUS create_view( struct file_view **view_ret, void *base, size_t size, BYTE vprot )
414 struct file_view *view;
415 struct list *ptr;
416 int unix_prot = VIRTUAL_GetUnixProt( vprot );
418 assert( !((UINT_PTR)base & page_mask) );
419 assert( !(size & page_mask) );
421 /* Create the view structure */
423 if (!(view = malloc( sizeof(*view) + (size >> page_shift) - 1 ))) return STATUS_NO_MEMORY;
425 view->base = base;
426 view->size = size;
427 view->flags = 0;
428 view->mapping = 0;
429 view->protect = vprot;
430 memset( view->prot, vprot & ~VPROT_IMAGE, size >> page_shift );
432 /* Insert it in the linked list */
434 LIST_FOR_EACH( ptr, &views_list )
436 struct file_view *next = LIST_ENTRY( ptr, struct file_view, entry );
437 if (next->base > base) break;
439 list_add_before( ptr, &view->entry );
441 /* Check for overlapping views. This can happen if the previous view
442 * was a system view that got unmapped behind our back. In that case
443 * we recover by simply deleting it. */
445 if ((ptr = list_prev( &views_list, &view->entry )) != NULL)
447 struct file_view *prev = LIST_ENTRY( ptr, struct file_view, entry );
448 if ((char *)prev->base + prev->size > (char *)base)
450 TRACE( "overlapping prev view %p-%p for %p-%p\n",
451 prev->base, (char *)prev->base + prev->size,
452 base, (char *)base + view->size );
453 assert( prev->flags & VFLAG_SYSTEM );
454 delete_view( prev );
457 if ((ptr = list_next( &views_list, &view->entry )) != NULL)
459 struct file_view *next = LIST_ENTRY( ptr, struct file_view, entry );
460 if ((char *)base + view->size > (char *)next->base)
462 TRACE( "overlapping next view %p-%p for %p-%p\n",
463 next->base, (char *)next->base + next->size,
464 base, (char *)base + view->size );
465 assert( next->flags & VFLAG_SYSTEM );
466 delete_view( next );
470 *view_ret = view;
471 VIRTUAL_DEBUG_DUMP_VIEW( view );
473 if (force_exec_prot && (unix_prot & PROT_READ) && !(unix_prot & PROT_EXEC))
475 TRACE( "forcing exec permission on %p-%p\n", base, (char *)base + size - 1 );
476 mprotect( base, size, unix_prot | PROT_EXEC );
478 return STATUS_SUCCESS;
482 /***********************************************************************
483 * VIRTUAL_GetWin32Prot
485 * Convert page protections to Win32 flags.
487 static DWORD VIRTUAL_GetWin32Prot( BYTE vprot )
489 DWORD ret = VIRTUAL_Win32Flags[vprot & 0x0f];
490 if (vprot & VPROT_NOCACHE) ret |= PAGE_NOCACHE;
491 if (vprot & VPROT_GUARD) ret |= PAGE_GUARD;
492 return ret;
496 /***********************************************************************
497 * VIRTUAL_GetProt
499 * Build page protections from Win32 flags.
501 * PARAMS
502 * protect [I] Win32 protection flags
504 * RETURNS
505 * Value of page protection flags
507 static BYTE VIRTUAL_GetProt( DWORD protect )
509 BYTE vprot;
511 switch(protect & 0xff)
513 case PAGE_READONLY:
514 vprot = VPROT_READ;
515 break;
516 case PAGE_READWRITE:
517 vprot = VPROT_READ | VPROT_WRITE;
518 break;
519 case PAGE_WRITECOPY:
520 /* MSDN CreateFileMapping() states that if PAGE_WRITECOPY is given,
521 * that the hFile must have been opened with GENERIC_READ and
522 * GENERIC_WRITE access. This is WRONG as tests show that you
523 * only need GENERIC_READ access (at least for Win9x,
524 * FIXME: what about NT?). Thus, we don't put VPROT_WRITE in
525 * PAGE_WRITECOPY and PAGE_EXECUTE_WRITECOPY.
527 vprot = VPROT_READ | VPROT_WRITECOPY;
528 break;
529 case PAGE_EXECUTE:
530 vprot = VPROT_EXEC;
531 break;
532 case PAGE_EXECUTE_READ:
533 vprot = VPROT_EXEC | VPROT_READ;
534 break;
535 case PAGE_EXECUTE_READWRITE:
536 vprot = VPROT_EXEC | VPROT_READ | VPROT_WRITE;
537 break;
538 case PAGE_EXECUTE_WRITECOPY:
539 /* See comment for PAGE_WRITECOPY above */
540 vprot = VPROT_EXEC | VPROT_READ | VPROT_WRITECOPY;
541 break;
542 case PAGE_NOACCESS:
543 default:
544 vprot = 0;
545 break;
547 if (protect & PAGE_GUARD) vprot |= VPROT_GUARD;
548 if (protect & PAGE_NOCACHE) vprot |= VPROT_NOCACHE;
549 return vprot;
553 /***********************************************************************
554 * VIRTUAL_SetProt
556 * Change the protection of a range of pages.
558 * RETURNS
559 * TRUE: Success
560 * FALSE: Failure
562 static BOOL VIRTUAL_SetProt( FILE_VIEW *view, /* [in] Pointer to view */
563 void *base, /* [in] Starting address */
564 size_t size, /* [in] Size in bytes */
565 BYTE vprot ) /* [in] Protections to use */
567 int unix_prot = VIRTUAL_GetUnixProt(vprot);
569 TRACE("%p-%p %s\n",
570 base, (char *)base + size - 1, VIRTUAL_GetProtStr( vprot ) );
572 if (force_exec_prot && (unix_prot & PROT_READ) && !(unix_prot & PROT_EXEC))
574 TRACE( "forcing exec permission on %p-%p\n", base, (char *)base + size - 1 );
575 if (!mprotect( base, size, unix_prot | PROT_EXEC )) goto done;
576 /* exec + write may legitimately fail, in that case fall back to write only */
577 if (!(unix_prot & PROT_WRITE)) return FALSE;
580 if (mprotect( base, size, unix_prot )) return FALSE; /* FIXME: last error */
582 done:
583 memset( view->prot + (((char *)base - (char *)view->base) >> page_shift),
584 vprot, size >> page_shift );
585 VIRTUAL_DEBUG_DUMP_VIEW( view );
586 return TRUE;
590 /***********************************************************************
591 * unmap_extra_space
593 * Release the extra memory while keeping the range starting on the granularity boundary.
595 static inline void *unmap_extra_space( void *ptr, size_t total_size, size_t wanted_size, size_t mask )
597 if ((ULONG_PTR)ptr & mask)
599 size_t extra = mask + 1 - ((ULONG_PTR)ptr & mask);
600 munmap( ptr, extra );
601 ptr = (char *)ptr + extra;
602 total_size -= extra;
604 if (total_size > wanted_size)
605 munmap( (char *)ptr + wanted_size, total_size - wanted_size );
606 return ptr;
610 struct alloc_area
612 size_t size;
613 size_t mask;
614 int top_down;
615 void *result;
618 /***********************************************************************
619 * alloc_reserved_area_callback
621 * Try to map some space inside a reserved area. Callback for wine_mmap_enum_reserved_areas.
623 static int alloc_reserved_area_callback( void *start, size_t size, void *arg )
625 static void * const address_space_start = (void *)0x110000;
626 struct alloc_area *alloc = arg;
627 void *end = (char *)start + size;
629 if (start < address_space_start) start = address_space_start;
630 if (user_space_limit && end > user_space_limit) end = user_space_limit;
631 if (start >= end) return 0;
633 /* make sure we don't touch the preloader reserved range */
634 if (preload_reserve_end >= start)
636 if (preload_reserve_end >= end)
638 if (preload_reserve_start <= start) return 0; /* no space in that area */
639 if (preload_reserve_start < end) end = preload_reserve_start;
641 else if (preload_reserve_start <= start) start = preload_reserve_end;
642 else
644 /* range is split in two by the preloader reservation, try first part */
645 if ((alloc->result = find_free_area( start, preload_reserve_start, alloc->size,
646 alloc->mask, alloc->top_down )))
647 return 1;
648 /* then fall through to try second part */
649 start = preload_reserve_end;
652 if ((alloc->result = find_free_area( start, end, alloc->size, alloc->mask, alloc->top_down )))
653 return 1;
655 return 0;
659 /***********************************************************************
660 * map_view
662 * Create a view and mmap the corresponding memory area.
663 * The csVirtual section must be held by caller.
665 static NTSTATUS map_view( struct file_view **view_ret, void *base, size_t size, size_t mask,
666 int top_down, BYTE vprot )
668 void *ptr;
669 NTSTATUS status;
671 if (base)
673 if (is_beyond_limit( base, size, ADDRESS_SPACE_LIMIT ))
674 return STATUS_WORKING_SET_LIMIT_RANGE;
676 switch (wine_mmap_is_in_reserved_area( base, size ))
678 case -1: /* partially in a reserved area */
679 return STATUS_CONFLICTING_ADDRESSES;
681 case 0: /* not in a reserved area, do a normal allocation */
682 if ((ptr = wine_anon_mmap( base, size, VIRTUAL_GetUnixProt(vprot), 0 )) == (void *)-1)
684 if (errno == ENOMEM) return STATUS_NO_MEMORY;
685 return STATUS_INVALID_PARAMETER;
687 if (ptr != base)
689 /* We couldn't get the address we wanted */
690 if (is_beyond_limit( ptr, size, user_space_limit )) add_reserved_area( ptr, size );
691 else munmap( ptr, size );
692 return STATUS_CONFLICTING_ADDRESSES;
694 break;
696 default:
697 case 1: /* in a reserved area, make sure the address is available */
698 if (find_view_range( base, size )) return STATUS_CONFLICTING_ADDRESSES;
699 /* replace the reserved area by our mapping */
700 if ((ptr = wine_anon_mmap( base, size, VIRTUAL_GetUnixProt(vprot), MAP_FIXED )) != base)
701 return STATUS_INVALID_PARAMETER;
702 break;
705 else
707 size_t view_size = size + mask + 1;
708 struct alloc_area alloc;
710 alloc.size = size;
711 alloc.mask = mask;
712 alloc.top_down = top_down;
713 if (wine_mmap_enum_reserved_areas( alloc_reserved_area_callback, &alloc, top_down ))
715 ptr = alloc.result;
716 TRACE( "got mem in reserved area %p-%p\n", ptr, (char *)ptr + size );
717 if (wine_anon_mmap( ptr, size, VIRTUAL_GetUnixProt(vprot), MAP_FIXED ) != ptr)
718 return STATUS_INVALID_PARAMETER;
719 goto done;
722 for (;;)
724 if ((ptr = wine_anon_mmap( NULL, view_size, VIRTUAL_GetUnixProt(vprot), 0 )) == (void *)-1)
726 if (errno == ENOMEM) return STATUS_NO_MEMORY;
727 return STATUS_INVALID_PARAMETER;
729 TRACE( "got mem with anon mmap %p-%p\n", ptr, (char *)ptr + size );
730 /* if we got something beyond the user limit, unmap it and retry */
731 if (is_beyond_limit( ptr, view_size, user_space_limit )) add_reserved_area( ptr, view_size );
732 else break;
734 ptr = unmap_extra_space( ptr, view_size, size, mask );
736 done:
737 status = create_view( view_ret, ptr, size, vprot );
738 if (status != STATUS_SUCCESS) unmap_area( ptr, size );
739 return status;
743 /***********************************************************************
744 * unaligned_mmap
746 * Linux kernels before 2.4.x can support non page-aligned offsets, as
747 * long as the offset is aligned to the filesystem block size. This is
748 * a big performance gain so we want to take advantage of it.
750 * However, when we use 64-bit file support this doesn't work because
751 * glibc rejects unaligned offsets. Also glibc 2.1.3 mmap64 is broken
752 * in that it rounds unaligned offsets down to a page boundary. For
753 * these reasons we do a direct system call here.
755 static void *unaligned_mmap( void *addr, size_t length, unsigned int prot,
756 unsigned int flags, int fd, off_t offset )
758 #if defined(linux) && defined(__i386__) && defined(__GNUC__)
759 if (!(offset >> 32) && (offset & page_mask))
761 int ret;
763 struct
765 void *addr;
766 unsigned int length;
767 unsigned int prot;
768 unsigned int flags;
769 unsigned int fd;
770 unsigned int offset;
771 } args;
773 args.addr = addr;
774 args.length = length;
775 args.prot = prot;
776 args.flags = flags;
777 args.fd = fd;
778 args.offset = offset;
780 __asm__ __volatile__("push %%ebx\n\t"
781 "movl %2,%%ebx\n\t"
782 "int $0x80\n\t"
783 "popl %%ebx"
784 : "=a" (ret)
785 : "0" (90), /* SYS_mmap */
786 "q" (&args)
787 : "memory" );
788 if (ret < 0 && ret > -4096)
790 errno = -ret;
791 ret = -1;
793 return (void *)ret;
795 #endif
796 return mmap( addr, length, prot, flags, fd, offset );
800 /***********************************************************************
801 * map_file_into_view
803 * Wrapper for mmap() to map a file into a view, falling back to read if mmap fails.
804 * The csVirtual section must be held by caller.
806 static NTSTATUS map_file_into_view( struct file_view *view, int fd, size_t start, size_t size,
807 off_t offset, BYTE vprot, BOOL removable )
809 void *ptr;
810 int prot = VIRTUAL_GetUnixProt( vprot );
811 BOOL shared_write = (vprot & VPROT_WRITE) != 0;
813 assert( start < view->size );
814 assert( start + size <= view->size );
816 /* only try mmap if media is not removable (or if we require write access) */
817 if (!removable || shared_write)
819 int flags = MAP_FIXED | (shared_write ? MAP_SHARED : MAP_PRIVATE);
821 if (unaligned_mmap( (char *)view->base + start, size, prot, flags, fd, offset ) != (void *)-1)
822 goto done;
824 /* mmap() failed; if this is because the file offset is not */
825 /* page-aligned (EINVAL), or because the underlying filesystem */
826 /* does not support mmap() (ENOEXEC,ENODEV), we do it by hand. */
827 if ((errno != ENOEXEC) && (errno != EINVAL) && (errno != ENODEV)) return FILE_GetNtStatus();
828 if (shared_write) return FILE_GetNtStatus(); /* we cannot fake shared write mappings */
831 /* Reserve the memory with an anonymous mmap */
832 ptr = wine_anon_mmap( (char *)view->base + start, size, PROT_READ | PROT_WRITE, MAP_FIXED );
833 if (ptr == (void *)-1) return FILE_GetNtStatus();
834 /* Now read in the file */
835 pread( fd, ptr, size, offset );
836 if (prot != (PROT_READ|PROT_WRITE)) mprotect( ptr, size, prot ); /* Set the right protection */
837 done:
838 memset( view->prot + (start >> page_shift), vprot, ROUND_SIZE(start,size) >> page_shift );
839 return STATUS_SUCCESS;
843 /***********************************************************************
844 * decommit_view
846 * Decommit some pages of a given view.
847 * The csVirtual section must be held by caller.
849 static NTSTATUS decommit_pages( struct file_view *view, size_t start, size_t size )
851 if (wine_anon_mmap( (char *)view->base + start, size, PROT_NONE, MAP_FIXED ) != (void *)-1)
853 BYTE *p = view->prot + (start >> page_shift);
854 size >>= page_shift;
855 while (size--) *p++ &= ~VPROT_COMMITTED;
856 return STATUS_SUCCESS;
858 return FILE_GetNtStatus();
862 /***********************************************************************
863 * do_relocations
865 * Apply the relocations to a mapped PE image
867 static int do_relocations( char *base, const IMAGE_DATA_DIRECTORY *dir,
868 int delta, SIZE_T total_size )
870 IMAGE_BASE_RELOCATION *rel;
872 TRACE_(module)( "relocating from %p-%p to %p-%p\n",
873 base - delta, base - delta + total_size, base, base + total_size );
875 for (rel = (IMAGE_BASE_RELOCATION *)(base + dir->VirtualAddress);
876 ((char *)rel < base + dir->VirtualAddress + dir->Size) && rel->SizeOfBlock;
877 rel = (IMAGE_BASE_RELOCATION*)((char*)rel + rel->SizeOfBlock) )
879 char *page = base + rel->VirtualAddress;
880 WORD *TypeOffset = (WORD *)(rel + 1);
881 int i, count = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(*TypeOffset);
883 if (!count) continue;
885 /* sanity checks */
886 if ((char *)rel + rel->SizeOfBlock > base + dir->VirtualAddress + dir->Size)
888 ERR_(module)("invalid relocation %p,%x,%d at %p,%x,%x\n",
889 rel, rel->VirtualAddress, rel->SizeOfBlock,
890 base, dir->VirtualAddress, dir->Size );
891 return 0;
894 if (page > base + total_size)
896 WARN_(module)("skipping %d relocations for page %p beyond module %p-%p\n",
897 count, page, base, base + total_size );
898 continue;
901 TRACE_(module)("%d relocations for page %x\n", count, rel->VirtualAddress);
903 /* patching in reverse order */
904 for (i = 0 ; i < count; i++)
906 int offset = TypeOffset[i] & 0xFFF;
907 int type = TypeOffset[i] >> 12;
908 switch(type)
910 case IMAGE_REL_BASED_ABSOLUTE:
911 break;
912 case IMAGE_REL_BASED_HIGH:
913 *(short*)(page+offset) += HIWORD(delta);
914 break;
915 case IMAGE_REL_BASED_LOW:
916 *(short*)(page+offset) += LOWORD(delta);
917 break;
918 case IMAGE_REL_BASED_HIGHLOW:
919 *(int*)(page+offset) += delta;
920 /* FIXME: if this is an exported address, fire up enhanced logic */
921 break;
922 default:
923 FIXME_(module)("Unknown/unsupported fixup type %d.\n", type);
924 break;
928 return 1;
932 /***********************************************************************
933 * map_image
935 * Map an executable (PE format) image into memory.
937 static NTSTATUS map_image( HANDLE hmapping, int fd, char *base, SIZE_T total_size, SIZE_T mask,
938 SIZE_T header_size, int shared_fd, HANDLE dup_mapping, PVOID *addr_ptr )
940 IMAGE_DOS_HEADER *dos;
941 IMAGE_NT_HEADERS *nt;
942 IMAGE_SECTION_HEADER *sec;
943 IMAGE_DATA_DIRECTORY *imports;
944 NTSTATUS status = STATUS_CONFLICTING_ADDRESSES;
945 int i;
946 off_t pos;
947 sigset_t sigset;
948 struct stat st;
949 struct file_view *view = NULL;
950 char *ptr, *header_end;
952 /* zero-map the whole range */
954 server_enter_uninterrupted_section( &csVirtual, &sigset );
956 if (base >= (char *)0x110000) /* make sure the DOS area remains free */
957 status = map_view( &view, base, total_size, mask, FALSE,
958 VPROT_COMMITTED | VPROT_READ | VPROT_EXEC | VPROT_WRITECOPY | VPROT_IMAGE );
960 if (status == STATUS_CONFLICTING_ADDRESSES)
961 status = map_view( &view, NULL, total_size, mask, FALSE,
962 VPROT_COMMITTED | VPROT_READ | VPROT_EXEC | VPROT_WRITECOPY | VPROT_IMAGE );
964 if (status != STATUS_SUCCESS) goto error;
966 ptr = view->base;
967 TRACE_(module)( "mapped PE file at %p-%p\n", ptr, ptr + total_size );
969 /* map the header */
971 if (fstat( fd, &st ) == -1)
973 status = FILE_GetNtStatus();
974 goto error;
976 status = STATUS_INVALID_IMAGE_FORMAT; /* generic error */
977 if (!st.st_size) goto error;
978 header_size = min( header_size, st.st_size );
979 if (map_file_into_view( view, fd, 0, header_size, 0, VPROT_COMMITTED | VPROT_READ | VPROT_WRITECOPY,
980 !dup_mapping ) != STATUS_SUCCESS) goto error;
981 dos = (IMAGE_DOS_HEADER *)ptr;
982 nt = (IMAGE_NT_HEADERS *)(ptr + dos->e_lfanew);
983 header_end = ptr + ROUND_SIZE( 0, header_size );
984 memset( ptr + header_size, 0, header_end - (ptr + header_size) );
985 if ((char *)(nt + 1) > header_end) goto error;
986 sec = (IMAGE_SECTION_HEADER*)((char*)&nt->OptionalHeader+nt->FileHeader.SizeOfOptionalHeader);
987 if ((char *)(sec + nt->FileHeader.NumberOfSections) > header_end) goto error;
989 imports = nt->OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_IMPORT;
990 if (!imports->Size || !imports->VirtualAddress) imports = NULL;
992 /* check the architecture */
994 if (nt->FileHeader.Machine != IMAGE_FILE_MACHINE_I386)
996 MESSAGE("Trying to load PE image for unsupported architecture (");
997 switch (nt->FileHeader.Machine)
999 case IMAGE_FILE_MACHINE_UNKNOWN: MESSAGE("Unknown"); break;
1000 case IMAGE_FILE_MACHINE_I860: MESSAGE("I860"); break;
1001 case IMAGE_FILE_MACHINE_R3000: MESSAGE("R3000"); break;
1002 case IMAGE_FILE_MACHINE_R4000: MESSAGE("R4000"); break;
1003 case IMAGE_FILE_MACHINE_R10000: MESSAGE("R10000"); break;
1004 case IMAGE_FILE_MACHINE_ALPHA: MESSAGE("Alpha"); break;
1005 case IMAGE_FILE_MACHINE_POWERPC: MESSAGE("PowerPC"); break;
1006 case IMAGE_FILE_MACHINE_IA64: MESSAGE("IA-64"); break;
1007 case IMAGE_FILE_MACHINE_ALPHA64: MESSAGE("Alpha-64"); break;
1008 case IMAGE_FILE_MACHINE_AMD64: MESSAGE("AMD-64"); break;
1009 case IMAGE_FILE_MACHINE_ARM: MESSAGE("ARM"); break;
1010 default: MESSAGE("Unknown-%04x", nt->FileHeader.Machine); break;
1012 MESSAGE(")\n");
1013 goto error;
1016 /* check for non page-aligned binary */
1018 if (nt->OptionalHeader.SectionAlignment <= page_mask)
1020 /* unaligned sections, this happens for native subsystem binaries */
1021 /* in that case Windows simply maps in the whole file */
1023 if (map_file_into_view( view, fd, 0, total_size, 0, VPROT_COMMITTED | VPROT_READ,
1024 !dup_mapping ) != STATUS_SUCCESS) goto error;
1026 /* check that all sections are loaded at the right offset */
1027 if (nt->OptionalHeader.FileAlignment != nt->OptionalHeader.SectionAlignment) goto error;
1028 for (i = 0; i < nt->FileHeader.NumberOfSections; i++)
1030 if (sec[i].VirtualAddress != sec[i].PointerToRawData)
1031 goto error; /* Windows refuses to load in that case too */
1034 /* set the image protections */
1035 VIRTUAL_SetProt( view, ptr, total_size,
1036 VPROT_COMMITTED | VPROT_READ | VPROT_WRITECOPY | VPROT_EXEC );
1038 /* perform relocations if necessary */
1039 /* FIXME: not 100% compatible, Windows doesn't do this for non page-aligned binaries */
1040 if (ptr != base)
1042 const IMAGE_DATA_DIRECTORY *relocs;
1043 relocs = &nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC];
1044 if (relocs->VirtualAddress && relocs->Size)
1045 do_relocations( ptr, relocs, ptr - base, total_size );
1048 goto done;
1052 /* map all the sections */
1054 for (i = pos = 0; i < nt->FileHeader.NumberOfSections; i++, sec++)
1056 static const SIZE_T sector_align = 0x1ff;
1057 SIZE_T map_size, file_start, file_size, end;
1059 if (!sec->Misc.VirtualSize)
1060 map_size = ROUND_SIZE( 0, sec->SizeOfRawData );
1061 else
1062 map_size = ROUND_SIZE( 0, sec->Misc.VirtualSize );
1064 /* file positions are rounded to sector boundaries regardless of OptionalHeader.FileAlignment */
1065 file_start = sec->PointerToRawData & ~sector_align;
1066 file_size = (sec->SizeOfRawData + (sec->PointerToRawData & sector_align) + sector_align) & ~sector_align;
1067 if (file_size > map_size) file_size = map_size;
1069 /* a few sanity checks */
1070 end = sec->VirtualAddress + ROUND_SIZE( sec->VirtualAddress, map_size );
1071 if (sec->VirtualAddress > total_size || end > total_size || end < sec->VirtualAddress)
1073 WARN_(module)( "Section %.8s too large (%x+%lx/%lx)\n",
1074 sec->Name, sec->VirtualAddress, map_size, total_size );
1075 goto error;
1078 if ((sec->Characteristics & IMAGE_SCN_MEM_SHARED) &&
1079 (sec->Characteristics & IMAGE_SCN_MEM_WRITE))
1081 TRACE_(module)( "mapping shared section %.8s at %p off %x (%x) size %lx (%lx) flags %x\n",
1082 sec->Name, ptr + sec->VirtualAddress,
1083 sec->PointerToRawData, (int)pos, file_size, map_size,
1084 sec->Characteristics );
1085 if (map_file_into_view( view, shared_fd, sec->VirtualAddress, map_size, pos,
1086 VPROT_COMMITTED | VPROT_READ | VPROT_WRITE,
1087 FALSE ) != STATUS_SUCCESS)
1089 ERR_(module)( "Could not map shared section %.8s\n", sec->Name );
1090 goto error;
1093 /* check if the import directory falls inside this section */
1094 if (imports && imports->VirtualAddress >= sec->VirtualAddress &&
1095 imports->VirtualAddress < sec->VirtualAddress + map_size)
1097 UINT_PTR base = imports->VirtualAddress & ~page_mask;
1098 UINT_PTR end = base + ROUND_SIZE( imports->VirtualAddress, imports->Size );
1099 if (end > sec->VirtualAddress + map_size) end = sec->VirtualAddress + map_size;
1100 if (end > base)
1101 map_file_into_view( view, shared_fd, base, end - base,
1102 pos + (base - sec->VirtualAddress),
1103 VPROT_COMMITTED | VPROT_READ | VPROT_WRITECOPY,
1104 FALSE );
1106 pos += map_size;
1107 continue;
1110 TRACE_(module)( "mapping section %.8s at %p off %x size %x virt %x flags %x\n",
1111 sec->Name, ptr + sec->VirtualAddress,
1112 sec->PointerToRawData, sec->SizeOfRawData,
1113 sec->Misc.VirtualSize, sec->Characteristics );
1115 if (!sec->PointerToRawData || !file_size) continue;
1117 /* Note: if the section is not aligned properly map_file_into_view will magically
1118 * fall back to read(), so we don't need to check anything here.
1120 end = file_start + file_size;
1121 if (sec->PointerToRawData >= st.st_size ||
1122 end > ((st.st_size + sector_align) & ~sector_align) ||
1123 end < file_start ||
1124 map_file_into_view( view, fd, sec->VirtualAddress, file_size, file_start,
1125 VPROT_COMMITTED | VPROT_READ | VPROT_WRITECOPY,
1126 !dup_mapping ) != STATUS_SUCCESS)
1128 ERR_(module)( "Could not map section %.8s, file probably truncated\n", sec->Name );
1129 goto error;
1132 if (file_size & page_mask)
1134 end = ROUND_SIZE( 0, file_size );
1135 if (end > map_size) end = map_size;
1136 TRACE_(module)("clearing %p - %p\n",
1137 ptr + sec->VirtualAddress + file_size,
1138 ptr + sec->VirtualAddress + end );
1139 memset( ptr + sec->VirtualAddress + file_size, 0, end - file_size );
1144 /* perform base relocation, if necessary */
1146 if (ptr != base)
1148 const IMAGE_DATA_DIRECTORY *relocs;
1150 relocs = &nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC];
1151 if (nt->FileHeader.Characteristics & IMAGE_FILE_RELOCS_STRIPPED)
1153 WARN( "Need to relocate module from addr %x, but there are no relocation records\n",
1154 nt->OptionalHeader.ImageBase );
1155 status = STATUS_CONFLICTING_ADDRESSES;
1156 goto error;
1159 /* FIXME: If we need to relocate a system DLL (base > 2GB) we should
1160 * really make sure that the *new* base address is also > 2GB.
1161 * Some DLLs really check the MSB of the module handle :-/
1163 if ((nt->OptionalHeader.ImageBase & 0x80000000) && !((ULONG_PTR)base & 0x80000000))
1164 ERR( "Forced to relocate system DLL (base > 2GB). This is not good.\n" );
1166 if (!do_relocations( ptr, relocs, ptr - base, total_size ))
1168 goto error;
1172 /* set the image protections */
1174 VIRTUAL_SetProt( view, ptr, ROUND_SIZE( 0, header_size ), VPROT_COMMITTED | VPROT_READ );
1176 sec = (IMAGE_SECTION_HEADER*)((char *)&nt->OptionalHeader+nt->FileHeader.SizeOfOptionalHeader);
1177 for (i = 0; i < nt->FileHeader.NumberOfSections; i++, sec++)
1179 SIZE_T size;
1180 BYTE vprot = VPROT_COMMITTED;
1182 if (sec->Misc.VirtualSize)
1183 size = ROUND_SIZE( sec->VirtualAddress, sec->Misc.VirtualSize );
1184 else
1185 size = ROUND_SIZE( sec->VirtualAddress, sec->SizeOfRawData );
1187 if (sec->Characteristics & IMAGE_SCN_MEM_READ) vprot |= VPROT_READ;
1188 if (sec->Characteristics & IMAGE_SCN_MEM_WRITE) vprot |= VPROT_READ|VPROT_WRITECOPY;
1189 if (sec->Characteristics & IMAGE_SCN_MEM_EXECUTE) vprot |= VPROT_EXEC;
1191 /* Dumb game crack lets the AOEP point into a data section. Adjust. */
1192 if ((nt->OptionalHeader.AddressOfEntryPoint >= sec->VirtualAddress) &&
1193 (nt->OptionalHeader.AddressOfEntryPoint < sec->VirtualAddress + size))
1194 vprot |= VPROT_EXEC;
1196 VIRTUAL_SetProt( view, ptr + sec->VirtualAddress, size, vprot );
1199 done:
1200 view->mapping = dup_mapping;
1201 server_leave_uninterrupted_section( &csVirtual, &sigset );
1203 *addr_ptr = ptr;
1204 return STATUS_SUCCESS;
1206 error:
1207 if (view) delete_view( view );
1208 server_leave_uninterrupted_section( &csVirtual, &sigset );
1209 if (dup_mapping) NtClose( dup_mapping );
1210 return status;
1214 /***********************************************************************
1215 * is_current_process
1217 * Check whether a process handle is for the current process.
1219 BOOL is_current_process( HANDLE handle )
1221 BOOL ret = FALSE;
1223 if (handle == NtCurrentProcess()) return TRUE;
1224 SERVER_START_REQ( get_process_info )
1226 req->handle = handle;
1227 if (!wine_server_call( req ))
1228 ret = ((DWORD)reply->pid == GetCurrentProcessId());
1230 SERVER_END_REQ;
1231 return ret;
1235 /***********************************************************************
1236 * virtual_init
1238 void virtual_init(void)
1240 const char *preload;
1241 #ifndef page_mask
1242 page_size = getpagesize();
1243 page_mask = page_size - 1;
1244 /* Make sure we have a power of 2 */
1245 assert( !(page_size & page_mask) );
1246 page_shift = 0;
1247 while ((1 << page_shift) != page_size) page_shift++;
1248 #endif /* page_mask */
1249 if ((preload = getenv("WINEPRELOADRESERVE")))
1251 unsigned long start, end;
1252 if (sscanf( preload, "%lx-%lx", &start, &end ) == 2)
1254 preload_reserve_start = (void *)start;
1255 preload_reserve_end = (void *)end;
1261 /***********************************************************************
1262 * virtual_init_threading
1264 void virtual_init_threading(void)
1266 use_locks = 1;
1270 /***********************************************************************
1271 * VIRTUAL_HandleFault
1273 NTSTATUS VIRTUAL_HandleFault( LPCVOID addr )
1275 FILE_VIEW *view;
1276 NTSTATUS ret = STATUS_ACCESS_VIOLATION;
1277 sigset_t sigset;
1279 server_enter_uninterrupted_section( &csVirtual, &sigset );
1280 if ((view = VIRTUAL_FindView( addr )))
1282 void *page = ROUND_ADDR( addr, page_mask );
1283 BYTE vprot = view->prot[((const char *)page - (const char *)view->base) >> page_shift];
1284 if (vprot & VPROT_GUARD)
1286 VIRTUAL_SetProt( view, page, page_size, vprot & ~VPROT_GUARD );
1287 ret = STATUS_GUARD_PAGE_VIOLATION;
1290 server_leave_uninterrupted_section( &csVirtual, &sigset );
1291 return ret;
1295 /***********************************************************************
1296 * VIRTUAL_SetForceExec
1298 * Whether to force exec prot on all views.
1300 void VIRTUAL_SetForceExec( BOOL enable )
1302 struct file_view *view;
1303 sigset_t sigset;
1305 server_enter_uninterrupted_section( &csVirtual, &sigset );
1306 if (!force_exec_prot != !enable) /* change all existing views */
1308 force_exec_prot = enable;
1310 LIST_FOR_EACH_ENTRY( view, &views_list, struct file_view, entry )
1312 UINT i, count;
1313 int unix_prot;
1314 char *addr = view->base;
1315 BYTE prot = view->prot[0];
1317 for (count = i = 1; i < view->size >> page_shift; i++, count++)
1319 if (view->prot[i] == prot) continue;
1320 unix_prot = VIRTUAL_GetUnixProt( prot );
1321 if ((unix_prot & PROT_READ) && !(unix_prot & PROT_EXEC))
1323 TRACE( "%s exec prot for %p-%p\n",
1324 force_exec_prot ? "enabling" : "disabling",
1325 addr, addr + (count << page_shift) - 1 );
1326 mprotect( addr, count << page_shift,
1327 unix_prot | (force_exec_prot ? PROT_EXEC : 0) );
1329 addr += (count << page_shift);
1330 prot = view->prot[i];
1331 count = 0;
1333 if (count)
1335 unix_prot = VIRTUAL_GetUnixProt( prot );
1336 if ((unix_prot & PROT_READ) && !(unix_prot & PROT_EXEC))
1338 TRACE( "%s exec prot for %p-%p\n",
1339 force_exec_prot ? "enabling" : "disabling",
1340 addr, addr + (count << page_shift) - 1 );
1341 mprotect( addr, count << page_shift,
1342 unix_prot | (force_exec_prot ? PROT_EXEC : 0) );
1347 server_leave_uninterrupted_section( &csVirtual, &sigset );
1351 /***********************************************************************
1352 * VIRTUAL_UseLargeAddressSpace
1354 * Increase the address space size for apps that support it.
1356 void VIRTUAL_UseLargeAddressSpace(void)
1358 /* no large address space on win9x */
1359 if (NtCurrentTeb()->Peb->OSPlatformId != VER_PLATFORM_WIN32_NT) return;
1360 user_space_limit = ADDRESS_SPACE_LIMIT;
1364 /***********************************************************************
1365 * NtAllocateVirtualMemory (NTDLL.@)
1366 * ZwAllocateVirtualMemory (NTDLL.@)
1368 NTSTATUS WINAPI NtAllocateVirtualMemory( HANDLE process, PVOID *ret, ULONG zero_bits,
1369 SIZE_T *size_ptr, ULONG type, ULONG protect )
1371 void *base;
1372 BYTE vprot;
1373 SIZE_T size = *size_ptr;
1374 SIZE_T mask = get_mask( zero_bits );
1375 NTSTATUS status = STATUS_SUCCESS;
1376 struct file_view *view;
1377 sigset_t sigset;
1379 TRACE("%p %p %08lx %x %08x\n", process, *ret, size, type, protect );
1381 if (!size) return STATUS_INVALID_PARAMETER;
1383 if (process != NtCurrentProcess())
1385 apc_call_t call;
1386 apc_result_t result;
1388 call.virtual_alloc.type = APC_VIRTUAL_ALLOC;
1389 call.virtual_alloc.addr = *ret;
1390 call.virtual_alloc.size = *size_ptr;
1391 call.virtual_alloc.zero_bits = zero_bits;
1392 call.virtual_alloc.op_type = type;
1393 call.virtual_alloc.prot = protect;
1394 status = NTDLL_queue_process_apc( process, &call, &result );
1395 if (status != STATUS_SUCCESS) return status;
1397 if (result.virtual_alloc.status == STATUS_SUCCESS)
1399 *ret = result.virtual_alloc.addr;
1400 *size_ptr = result.virtual_alloc.size;
1402 return result.virtual_alloc.status;
1405 /* Round parameters to a page boundary */
1407 if (size > 0x7fc00000) return STATUS_WORKING_SET_LIMIT_RANGE; /* 2Gb - 4Mb */
1409 if (*ret)
1411 if (type & MEM_RESERVE) /* Round down to 64k boundary */
1412 base = ROUND_ADDR( *ret, mask );
1413 else
1414 base = ROUND_ADDR( *ret, page_mask );
1415 size = (((UINT_PTR)*ret + size + page_mask) & ~page_mask) - (UINT_PTR)base;
1417 /* disallow low 64k, wrap-around and kernel space */
1418 if (((char *)base < (char *)0x10000) ||
1419 ((char *)base + size < (char *)base) ||
1420 is_beyond_limit( base, size, ADDRESS_SPACE_LIMIT ))
1421 return STATUS_INVALID_PARAMETER;
1423 else
1425 base = NULL;
1426 size = (size + page_mask) & ~page_mask;
1429 /* Compute the alloc type flags */
1431 if (!(type & MEM_SYSTEM))
1433 if (!(type & (MEM_COMMIT | MEM_RESERVE)) ||
1434 (type & ~(MEM_COMMIT | MEM_RESERVE | MEM_TOP_DOWN | MEM_WRITE_WATCH | MEM_RESET)))
1436 WARN("called with wrong alloc type flags (%08x) !\n", type);
1437 return STATUS_INVALID_PARAMETER;
1439 if (type & MEM_WRITE_WATCH)
1441 FIXME("MEM_WRITE_WATCH type not supported\n");
1442 return STATUS_NOT_SUPPORTED;
1445 vprot = VIRTUAL_GetProt( protect );
1446 if (type & MEM_COMMIT) vprot |= VPROT_COMMITTED;
1448 /* Reserve the memory */
1450 if (use_locks) server_enter_uninterrupted_section( &csVirtual, &sigset );
1452 if (type & MEM_SYSTEM)
1454 if (type & MEM_IMAGE) vprot |= VPROT_IMAGE;
1455 status = create_view( &view, base, size, vprot | VPROT_COMMITTED );
1456 if (status == STATUS_SUCCESS)
1458 view->flags |= VFLAG_VALLOC | VFLAG_SYSTEM;
1459 base = view->base;
1462 else if ((type & MEM_RESERVE) || !base)
1464 status = map_view( &view, base, size, mask, type & MEM_TOP_DOWN, vprot );
1465 if (status == STATUS_SUCCESS)
1467 view->flags |= VFLAG_VALLOC;
1468 base = view->base;
1471 else /* commit the pages */
1473 if (!(view = VIRTUAL_FindView( base )) ||
1474 ((char *)base + size > (char *)view->base + view->size)) status = STATUS_NOT_MAPPED_VIEW;
1475 else if (!VIRTUAL_SetProt( view, base, size, vprot )) status = STATUS_ACCESS_DENIED;
1478 if (use_locks) server_leave_uninterrupted_section( &csVirtual, &sigset );
1480 if (status == STATUS_SUCCESS)
1482 *ret = base;
1483 *size_ptr = size;
1485 return status;
1489 /***********************************************************************
1490 * NtFreeVirtualMemory (NTDLL.@)
1491 * ZwFreeVirtualMemory (NTDLL.@)
1493 NTSTATUS WINAPI NtFreeVirtualMemory( HANDLE process, PVOID *addr_ptr, SIZE_T *size_ptr, ULONG type )
1495 FILE_VIEW *view;
1496 char *base;
1497 sigset_t sigset;
1498 NTSTATUS status = STATUS_SUCCESS;
1499 LPVOID addr = *addr_ptr;
1500 SIZE_T size = *size_ptr;
1502 TRACE("%p %p %08lx %x\n", process, addr, size, type );
1504 if (process != NtCurrentProcess())
1506 apc_call_t call;
1507 apc_result_t result;
1509 call.virtual_free.type = APC_VIRTUAL_FREE;
1510 call.virtual_free.addr = addr;
1511 call.virtual_free.size = size;
1512 call.virtual_free.op_type = type;
1513 status = NTDLL_queue_process_apc( process, &call, &result );
1514 if (status != STATUS_SUCCESS) return status;
1516 if (result.virtual_free.status == STATUS_SUCCESS)
1518 *addr_ptr = result.virtual_free.addr;
1519 *size_ptr = result.virtual_free.size;
1521 return result.virtual_free.status;
1524 /* Fix the parameters */
1526 size = ROUND_SIZE( addr, size );
1527 base = ROUND_ADDR( addr, page_mask );
1529 /* avoid freeing the DOS area when a broken app passes a NULL pointer */
1530 if (!base && !(type & MEM_SYSTEM)) return STATUS_INVALID_PARAMETER;
1532 server_enter_uninterrupted_section( &csVirtual, &sigset );
1534 if (!(view = VIRTUAL_FindView( base )) ||
1535 (base + size > (char *)view->base + view->size) ||
1536 !(view->flags & VFLAG_VALLOC))
1538 status = STATUS_INVALID_PARAMETER;
1540 else if (type & MEM_SYSTEM)
1542 /* return the values that the caller should use to unmap the area */
1543 *addr_ptr = view->base;
1544 if (!wine_mmap_is_in_reserved_area( view->base, view->size )) *size_ptr = view->size;
1545 else *size_ptr = 0; /* make sure we don't munmap anything from a reserved area */
1546 view->flags |= VFLAG_SYSTEM;
1547 delete_view( view );
1549 else if (type == MEM_RELEASE)
1551 /* Free the pages */
1553 if (size || (base != view->base)) status = STATUS_INVALID_PARAMETER;
1554 else
1556 delete_view( view );
1557 *addr_ptr = base;
1558 *size_ptr = size;
1561 else if (type == MEM_DECOMMIT)
1563 status = decommit_pages( view, base - (char *)view->base, size );
1564 if (status == STATUS_SUCCESS)
1566 *addr_ptr = base;
1567 *size_ptr = size;
1570 else
1572 WARN("called with wrong free type flags (%08x) !\n", type);
1573 status = STATUS_INVALID_PARAMETER;
1576 server_leave_uninterrupted_section( &csVirtual, &sigset );
1577 return status;
1581 /***********************************************************************
1582 * NtProtectVirtualMemory (NTDLL.@)
1583 * ZwProtectVirtualMemory (NTDLL.@)
1585 NTSTATUS WINAPI NtProtectVirtualMemory( HANDLE process, PVOID *addr_ptr, SIZE_T *size_ptr,
1586 ULONG new_prot, ULONG *old_prot )
1588 FILE_VIEW *view;
1589 sigset_t sigset;
1590 NTSTATUS status = STATUS_SUCCESS;
1591 char *base;
1592 UINT i;
1593 BYTE vprot, *p;
1594 ULONG prot;
1595 SIZE_T size = *size_ptr;
1596 LPVOID addr = *addr_ptr;
1598 TRACE("%p %p %08lx %08x\n", process, addr, size, new_prot );
1600 if (!is_current_process( process ))
1602 ERR("Unsupported on other process\n");
1603 return STATUS_ACCESS_DENIED;
1606 /* Fix the parameters */
1608 size = ROUND_SIZE( addr, size );
1609 base = ROUND_ADDR( addr, page_mask );
1611 server_enter_uninterrupted_section( &csVirtual, &sigset );
1613 if (!(view = VIRTUAL_FindView( base )) || (base + size > (char *)view->base + view->size))
1615 status = STATUS_INVALID_PARAMETER;
1617 else
1619 /* Make sure all the pages are committed */
1621 p = view->prot + ((base - (char *)view->base) >> page_shift);
1622 prot = VIRTUAL_GetWin32Prot( *p );
1623 for (i = size >> page_shift; i; i--, p++)
1625 if (!(*p & VPROT_COMMITTED))
1627 status = STATUS_NOT_COMMITTED;
1628 break;
1631 if (!i)
1633 if (old_prot) *old_prot = prot;
1634 vprot = VIRTUAL_GetProt( new_prot ) | VPROT_COMMITTED;
1635 if (!VIRTUAL_SetProt( view, base, size, vprot )) status = STATUS_ACCESS_DENIED;
1638 server_leave_uninterrupted_section( &csVirtual, &sigset );
1640 if (status == STATUS_SUCCESS)
1642 *addr_ptr = base;
1643 *size_ptr = size;
1645 return status;
1648 #define UNIMPLEMENTED_INFO_CLASS(c) \
1649 case c: \
1650 FIXME("(process=%p,addr=%p) Unimplemented information class: " #c "\n", process, addr); \
1651 return STATUS_INVALID_INFO_CLASS
1653 /***********************************************************************
1654 * NtQueryVirtualMemory (NTDLL.@)
1655 * ZwQueryVirtualMemory (NTDLL.@)
1657 NTSTATUS WINAPI NtQueryVirtualMemory( HANDLE process, LPCVOID addr,
1658 MEMORY_INFORMATION_CLASS info_class, PVOID buffer,
1659 SIZE_T len, SIZE_T *res_len )
1661 FILE_VIEW *view;
1662 char *base, *alloc_base = 0;
1663 struct list *ptr;
1664 SIZE_T size = 0;
1665 MEMORY_BASIC_INFORMATION *info = buffer;
1666 sigset_t sigset;
1668 if (info_class != MemoryBasicInformation)
1670 switch(info_class)
1672 UNIMPLEMENTED_INFO_CLASS(MemoryWorkingSetList);
1673 UNIMPLEMENTED_INFO_CLASS(MemorySectionName);
1674 UNIMPLEMENTED_INFO_CLASS(MemoryBasicVlmInformation);
1676 default:
1677 FIXME("(%p,%p,info_class=%d,%p,%ld,%p) Unknown information class\n",
1678 process, addr, info_class, buffer, len, res_len);
1679 return STATUS_INVALID_INFO_CLASS;
1682 if (ADDRESS_SPACE_LIMIT && addr >= ADDRESS_SPACE_LIMIT)
1683 return STATUS_WORKING_SET_LIMIT_RANGE;
1685 if (process != NtCurrentProcess())
1687 NTSTATUS status;
1688 apc_call_t call;
1689 apc_result_t result;
1691 call.virtual_query.type = APC_VIRTUAL_QUERY;
1692 call.virtual_query.addr = addr;
1693 status = NTDLL_queue_process_apc( process, &call, &result );
1694 if (status != STATUS_SUCCESS) return status;
1696 if (result.virtual_query.status == STATUS_SUCCESS)
1698 info->BaseAddress = result.virtual_query.base;
1699 info->AllocationBase = result.virtual_query.alloc_base;
1700 info->RegionSize = result.virtual_query.size;
1701 info->State = result.virtual_query.state;
1702 info->Protect = result.virtual_query.prot;
1703 info->AllocationProtect = result.virtual_query.alloc_prot;
1704 info->Type = result.virtual_query.alloc_type;
1705 if (res_len) *res_len = sizeof(*info);
1707 return result.virtual_query.status;
1710 base = ROUND_ADDR( addr, page_mask );
1712 /* Find the view containing the address */
1714 server_enter_uninterrupted_section( &csVirtual, &sigset );
1715 ptr = list_head( &views_list );
1716 for (;;)
1718 if (!ptr)
1720 /* make the address space end at the user limit, except if
1721 * the last view was mapped beyond that */
1722 if (alloc_base <= (char *)user_space_limit)
1724 if (user_space_limit && base >= (char *)user_space_limit)
1726 server_leave_uninterrupted_section( &csVirtual, &sigset );
1727 return STATUS_WORKING_SET_LIMIT_RANGE;
1729 size = (char *)user_space_limit - alloc_base;
1731 else size = (char *)ADDRESS_SPACE_LIMIT - alloc_base;
1732 view = NULL;
1733 break;
1735 view = LIST_ENTRY( ptr, struct file_view, entry );
1736 if ((char *)view->base > base)
1738 size = (char *)view->base - alloc_base;
1739 view = NULL;
1740 break;
1742 if ((char *)view->base + view->size > base)
1744 alloc_base = view->base;
1745 size = view->size;
1746 break;
1748 alloc_base = (char *)view->base + view->size;
1749 ptr = list_next( &views_list, ptr );
1752 /* Fill the info structure */
1754 if (!view)
1756 info->State = MEM_FREE;
1757 info->Protect = PAGE_NOACCESS;
1758 info->AllocationBase = 0;
1759 info->AllocationProtect = 0;
1760 info->Type = 0;
1762 else
1764 BYTE vprot = view->prot[(base - alloc_base) >> page_shift];
1765 info->State = (vprot & VPROT_COMMITTED) ? MEM_COMMIT : MEM_RESERVE;
1766 info->Protect = VIRTUAL_GetWin32Prot( vprot );
1767 info->AllocationBase = alloc_base;
1768 info->AllocationProtect = VIRTUAL_GetWin32Prot( view->protect );
1769 if (view->protect & VPROT_IMAGE) info->Type = MEM_IMAGE;
1770 else if (view->flags & VFLAG_VALLOC) info->Type = MEM_PRIVATE;
1771 else info->Type = MEM_MAPPED;
1772 for (size = base - alloc_base; size < view->size; size += page_size)
1773 if (view->prot[size >> page_shift] != vprot) break;
1775 server_leave_uninterrupted_section( &csVirtual, &sigset );
1777 info->BaseAddress = base;
1778 info->RegionSize = size - (base - alloc_base);
1779 if (res_len) *res_len = sizeof(*info);
1780 return STATUS_SUCCESS;
1784 /***********************************************************************
1785 * NtLockVirtualMemory (NTDLL.@)
1786 * ZwLockVirtualMemory (NTDLL.@)
1788 NTSTATUS WINAPI NtLockVirtualMemory( HANDLE process, PVOID *addr, SIZE_T *size, ULONG unknown )
1790 if (!is_current_process( process ))
1792 ERR("Unsupported on other process\n");
1793 return STATUS_ACCESS_DENIED;
1795 return STATUS_SUCCESS;
1799 /***********************************************************************
1800 * NtUnlockVirtualMemory (NTDLL.@)
1801 * ZwUnlockVirtualMemory (NTDLL.@)
1803 NTSTATUS WINAPI NtUnlockVirtualMemory( HANDLE process, PVOID *addr, SIZE_T *size, ULONG unknown )
1805 if (!is_current_process( process ))
1807 ERR("Unsupported on other process\n");
1808 return STATUS_ACCESS_DENIED;
1810 return STATUS_SUCCESS;
1814 /***********************************************************************
1815 * NtCreateSection (NTDLL.@)
1816 * ZwCreateSection (NTDLL.@)
1818 NTSTATUS WINAPI NtCreateSection( HANDLE *handle, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr,
1819 const LARGE_INTEGER *size, ULONG protect,
1820 ULONG sec_flags, HANDLE file )
1822 NTSTATUS ret;
1823 BYTE vprot;
1824 DWORD len = (attr && attr->ObjectName) ? attr->ObjectName->Length : 0;
1826 /* Check parameters */
1828 if (len > MAX_PATH*sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
1830 vprot = VIRTUAL_GetProt( protect );
1831 if (sec_flags & SEC_RESERVE)
1833 if (file) return STATUS_INVALID_PARAMETER;
1835 else vprot |= VPROT_COMMITTED;
1836 if (sec_flags & SEC_NOCACHE) vprot |= VPROT_NOCACHE;
1837 if (sec_flags & SEC_IMAGE) vprot |= VPROT_IMAGE;
1839 /* Create the server object */
1841 SERVER_START_REQ( create_mapping )
1843 req->access = access;
1844 req->attributes = (attr) ? attr->Attributes : 0;
1845 req->rootdir = attr ? attr->RootDirectory : 0;
1846 req->file_handle = file;
1847 req->size_high = size ? size->u.HighPart : 0;
1848 req->size_low = size ? size->u.LowPart : 0;
1849 req->protect = vprot;
1850 if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
1851 ret = wine_server_call( req );
1852 *handle = reply->handle;
1854 SERVER_END_REQ;
1855 return ret;
1859 /***********************************************************************
1860 * NtOpenSection (NTDLL.@)
1861 * ZwOpenSection (NTDLL.@)
1863 NTSTATUS WINAPI NtOpenSection( HANDLE *handle, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr )
1865 NTSTATUS ret;
1866 DWORD len = attr->ObjectName->Length;
1868 if (len > MAX_PATH*sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
1870 SERVER_START_REQ( open_mapping )
1872 req->access = access;
1873 req->attributes = (attr) ? attr->Attributes : 0;
1874 req->rootdir = attr ? attr->RootDirectory : 0;
1875 wine_server_add_data( req, attr->ObjectName->Buffer, len );
1876 if (!(ret = wine_server_call( req ))) *handle = reply->handle;
1878 SERVER_END_REQ;
1879 return ret;
1883 /***********************************************************************
1884 * NtMapViewOfSection (NTDLL.@)
1885 * ZwMapViewOfSection (NTDLL.@)
1887 NTSTATUS WINAPI NtMapViewOfSection( HANDLE handle, HANDLE process, PVOID *addr_ptr, ULONG zero_bits,
1888 SIZE_T commit_size, const LARGE_INTEGER *offset_ptr, SIZE_T *size_ptr,
1889 SECTION_INHERIT inherit, ULONG alloc_type, ULONG protect )
1891 NTSTATUS res;
1892 SIZE_T size = 0;
1893 SIZE_T mask = get_mask( zero_bits );
1894 int unix_handle = -1, needs_close;
1895 int prot;
1896 void *base;
1897 struct file_view *view;
1898 DWORD size_low, size_high, header_size, shared_size;
1899 HANDLE dup_mapping, shared_file;
1900 LARGE_INTEGER offset;
1901 sigset_t sigset;
1903 offset.QuadPart = offset_ptr ? offset_ptr->QuadPart : 0;
1905 TRACE("handle=%p process=%p addr=%p off=%x%08x size=%lx access=%x\n",
1906 handle, process, *addr_ptr, offset.u.HighPart, offset.u.LowPart, size, protect );
1908 if (!is_current_process( process ))
1910 ERR("Unsupported on other process\n");
1911 return STATUS_ACCESS_DENIED;
1914 /* Check parameters */
1916 if ((offset.u.LowPart & mask) || (*addr_ptr && ((UINT_PTR)*addr_ptr & mask)))
1917 return STATUS_INVALID_PARAMETER;
1919 SERVER_START_REQ( get_mapping_info )
1921 req->handle = handle;
1922 res = wine_server_call( req );
1923 prot = reply->protect;
1924 base = reply->base;
1925 size_low = reply->size_low;
1926 size_high = reply->size_high;
1927 header_size = reply->header_size;
1928 dup_mapping = reply->mapping;
1929 shared_file = reply->shared_file;
1930 shared_size = reply->shared_size;
1932 SERVER_END_REQ;
1933 if (res) return res;
1935 size = ((ULONGLONG)size_high << 32) | size_low;
1936 if (sizeof(size) == sizeof(size_low) && size_high)
1937 ERR( "Sizes larger than 4Gb (%x%08x) not supported on this platform\n", size_high, size_low );
1939 if ((res = server_get_unix_fd( handle, 0, &unix_handle, &needs_close, NULL, NULL ))) goto done;
1941 if (prot & VPROT_IMAGE)
1943 if (shared_file)
1945 int shared_fd, shared_needs_close;
1947 if ((res = server_get_unix_fd( shared_file, FILE_READ_DATA|FILE_WRITE_DATA,
1948 &shared_fd, &shared_needs_close, NULL, NULL ))) goto done;
1949 res = map_image( handle, unix_handle, base, size, mask, header_size,
1950 shared_fd, dup_mapping, addr_ptr );
1951 if (shared_needs_close) close( shared_fd );
1952 NtClose( shared_file );
1954 else
1956 res = map_image( handle, unix_handle, base, size, mask, header_size,
1957 -1, dup_mapping, addr_ptr );
1959 if (needs_close) close( unix_handle );
1960 if (!res) *size_ptr = size;
1961 return res;
1964 if ((offset.QuadPart >= size) || (*size_ptr > size - offset.QuadPart))
1966 res = STATUS_INVALID_PARAMETER;
1967 goto done;
1969 if (*size_ptr) size = ROUND_SIZE( offset.u.LowPart, *size_ptr );
1970 else size = size - offset.QuadPart;
1972 switch(protect)
1974 case PAGE_NOACCESS:
1975 break;
1976 case PAGE_READWRITE:
1977 case PAGE_EXECUTE_READWRITE:
1978 if (!(prot & VPROT_WRITE))
1980 res = STATUS_INVALID_PARAMETER;
1981 goto done;
1983 /* fall through */
1984 case PAGE_READONLY:
1985 case PAGE_WRITECOPY:
1986 case PAGE_EXECUTE:
1987 case PAGE_EXECUTE_READ:
1988 case PAGE_EXECUTE_WRITECOPY:
1989 if (prot & VPROT_READ) break;
1990 /* fall through */
1991 default:
1992 res = STATUS_INVALID_PARAMETER;
1993 goto done;
1996 /* FIXME: If a mapping is created with SEC_RESERVE and a process,
1997 * which has a view of this mapping commits some pages, they will
1998 * appear committed in all other processes, which have the same
1999 * view created. Since we don't support this yet, we create the
2000 * whole mapping committed.
2002 prot |= VPROT_COMMITTED;
2004 /* Reserve a properly aligned area */
2006 server_enter_uninterrupted_section( &csVirtual, &sigset );
2008 res = map_view( &view, *addr_ptr, size, mask, FALSE, prot );
2009 if (res)
2011 server_leave_uninterrupted_section( &csVirtual, &sigset );
2012 goto done;
2015 /* Map the file */
2017 TRACE("handle=%p size=%lx offset=%x%08x\n",
2018 handle, size, offset.u.HighPart, offset.u.LowPart );
2020 res = map_file_into_view( view, unix_handle, 0, size, offset.QuadPart, prot, !dup_mapping );
2021 if (res == STATUS_SUCCESS)
2023 *addr_ptr = view->base;
2024 *size_ptr = size;
2025 view->mapping = dup_mapping;
2026 dup_mapping = 0; /* don't close it */
2028 else
2030 ERR( "map_file_into_view %p %lx %x%08x failed\n",
2031 view->base, size, offset.u.HighPart, offset.u.LowPart );
2032 delete_view( view );
2035 server_leave_uninterrupted_section( &csVirtual, &sigset );
2037 done:
2038 if (dup_mapping) NtClose( dup_mapping );
2039 if (needs_close) close( unix_handle );
2040 return res;
2044 /***********************************************************************
2045 * NtUnmapViewOfSection (NTDLL.@)
2046 * ZwUnmapViewOfSection (NTDLL.@)
2048 NTSTATUS WINAPI NtUnmapViewOfSection( HANDLE process, PVOID addr )
2050 FILE_VIEW *view;
2051 NTSTATUS status = STATUS_INVALID_PARAMETER;
2052 sigset_t sigset;
2053 void *base = ROUND_ADDR( addr, page_mask );
2055 if (!is_current_process( process ))
2057 ERR("Unsupported on other process\n");
2058 return STATUS_ACCESS_DENIED;
2060 server_enter_uninterrupted_section( &csVirtual, &sigset );
2061 if ((view = VIRTUAL_FindView( base )) && (base == view->base))
2063 delete_view( view );
2064 status = STATUS_SUCCESS;
2066 server_leave_uninterrupted_section( &csVirtual, &sigset );
2067 return status;
2071 /***********************************************************************
2072 * NtFlushVirtualMemory (NTDLL.@)
2073 * ZwFlushVirtualMemory (NTDLL.@)
2075 NTSTATUS WINAPI NtFlushVirtualMemory( HANDLE process, LPCVOID *addr_ptr,
2076 SIZE_T *size_ptr, ULONG unknown )
2078 FILE_VIEW *view;
2079 NTSTATUS status = STATUS_SUCCESS;
2080 sigset_t sigset;
2081 void *addr = ROUND_ADDR( *addr_ptr, page_mask );
2083 if (!is_current_process( process ))
2085 ERR("Unsupported on other process\n");
2086 return STATUS_ACCESS_DENIED;
2088 server_enter_uninterrupted_section( &csVirtual, &sigset );
2089 if (!(view = VIRTUAL_FindView( addr ))) status = STATUS_INVALID_PARAMETER;
2090 else
2092 if (!*size_ptr) *size_ptr = view->size;
2093 *addr_ptr = addr;
2094 if (msync( addr, *size_ptr, MS_SYNC )) status = STATUS_NOT_MAPPED_DATA;
2096 server_leave_uninterrupted_section( &csVirtual, &sigset );
2097 return status;
2101 /***********************************************************************
2102 * NtReadVirtualMemory (NTDLL.@)
2103 * ZwReadVirtualMemory (NTDLL.@)
2105 NTSTATUS WINAPI NtReadVirtualMemory( HANDLE process, const void *addr, void *buffer,
2106 SIZE_T size, SIZE_T *bytes_read )
2108 NTSTATUS status;
2110 SERVER_START_REQ( read_process_memory )
2112 req->handle = process;
2113 req->addr = (void *)addr;
2114 wine_server_set_reply( req, buffer, size );
2115 if ((status = wine_server_call( req ))) size = 0;
2117 SERVER_END_REQ;
2118 if (bytes_read) *bytes_read = size;
2119 return status;
2123 /***********************************************************************
2124 * NtWriteVirtualMemory (NTDLL.@)
2125 * ZwWriteVirtualMemory (NTDLL.@)
2127 NTSTATUS WINAPI NtWriteVirtualMemory( HANDLE process, void *addr, const void *buffer,
2128 SIZE_T size, SIZE_T *bytes_written )
2130 NTSTATUS status;
2132 SERVER_START_REQ( write_process_memory )
2134 req->handle = process;
2135 req->addr = addr;
2136 wine_server_add_data( req, buffer, size );
2137 if ((status = wine_server_call( req ))) size = 0;
2139 SERVER_END_REQ;
2140 if (bytes_written) *bytes_written = size;
2141 return status;