2 * Win32 virtual memory functions
4 * Copyright 1997, 2002 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
26 #ifdef HAVE_SYS_ERRNO_H
27 #include <sys/errno.h>
36 #include <sys/types.h>
37 #ifdef HAVE_SYS_MMAN_H
41 #define NONAMELESSUNION
42 #define NONAMELESSSTRUCT
47 #include "wine/library.h"
48 #include "wine/server.h"
49 #include "wine/debug.h"
50 #include "ntdll_misc.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(virtual);
53 WINE_DECLARE_DEBUG_CHANNEL(module);
62 struct _FV *next; /* Next view */
63 struct _FV *prev; /* Prev view */
64 void *base; /* Base address */
65 UINT size; /* Size in bytes */
66 UINT flags; /* Allocation flags */
67 HANDLE mapping; /* Handle to the file mapping */
68 HANDLERPROC handlerProc; /* Fault handler */
69 LPVOID handlerArg; /* Fault handler argument */
70 BYTE protect; /* Protection for all pages at allocation time */
71 BYTE prot[1]; /* Protection byte for each page */
75 #define VFLAG_SYSTEM 0x01
76 #define VFLAG_VALLOC 0x02 /* allocated by VirtualAlloc */
78 /* Conversion from VPROT_* to Win32 flags */
79 static const BYTE VIRTUAL_Win32Flags[16] =
81 PAGE_NOACCESS, /* 0 */
82 PAGE_READONLY, /* READ */
83 PAGE_READWRITE, /* WRITE */
84 PAGE_READWRITE, /* READ | WRITE */
85 PAGE_EXECUTE, /* EXEC */
86 PAGE_EXECUTE_READ, /* READ | EXEC */
87 PAGE_EXECUTE_READWRITE, /* WRITE | EXEC */
88 PAGE_EXECUTE_READWRITE, /* READ | WRITE | EXEC */
89 PAGE_WRITECOPY, /* WRITECOPY */
90 PAGE_WRITECOPY, /* READ | WRITECOPY */
91 PAGE_WRITECOPY, /* WRITE | WRITECOPY */
92 PAGE_WRITECOPY, /* READ | WRITE | WRITECOPY */
93 PAGE_EXECUTE_WRITECOPY, /* EXEC | WRITECOPY */
94 PAGE_EXECUTE_WRITECOPY, /* READ | EXEC | WRITECOPY */
95 PAGE_EXECUTE_WRITECOPY, /* WRITE | EXEC | WRITECOPY */
96 PAGE_EXECUTE_WRITECOPY /* READ | WRITE | EXEC | WRITECOPY */
100 static FILE_VIEW *VIRTUAL_FirstView;
102 static CRITICAL_SECTION csVirtual;
103 static CRITICAL_SECTION_DEBUG critsect_debug =
106 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
107 0, 0, { 0, (DWORD)(__FILE__ ": csVirtual") }
109 static CRITICAL_SECTION csVirtual = { &critsect_debug, -1, 0, 0, 0, 0 };
112 /* These are always the same on an i386, and it will be faster this way */
113 # define page_mask 0xfff
114 # define page_shift 12
115 # define page_size 0x1000
116 /* Note: ADDRESS_SPACE_LIMIT is a Windows limit, you cannot change it.
117 * If you are on Solaris you need to find a way to avoid having the system
118 * allocate things above 0xc000000. Don't touch that define.
120 # define ADDRESS_SPACE_LIMIT ((void *)0xc0000000) /* top of the user address space */
122 static UINT page_shift;
123 static UINT page_mask;
124 static UINT page_size;
125 # define ADDRESS_SPACE_LIMIT 0 /* no limit needed on other platforms */
126 #endif /* __i386__ */
127 #define granularity_mask 0xffff /* Allocation granularity (usually 64k) */
129 #define ROUND_ADDR(addr,mask) \
130 ((void *)((UINT_PTR)(addr) & ~(mask)))
132 #define ROUND_SIZE(addr,size) \
133 (((UINT)(size) + ((UINT_PTR)(addr) & page_mask) + page_mask) & ~page_mask)
135 #define VIRTUAL_DEBUG_DUMP_VIEW(view) \
136 if (!TRACE_ON(virtual)); else VIRTUAL_DumpView(view)
138 static LPVOID VIRTUAL_mmap( int fd, LPVOID start, DWORD size, DWORD offset_low,
139 DWORD offset_high, int prot, int flags, BOOL *removable );
142 /***********************************************************************
145 static const char *VIRTUAL_GetProtStr( BYTE prot )
147 static char buffer[6];
148 buffer[0] = (prot & VPROT_COMMITTED) ? 'c' : '-';
149 buffer[1] = (prot & VPROT_GUARD) ? 'g' : '-';
150 buffer[2] = (prot & VPROT_READ) ? 'r' : '-';
151 buffer[3] = (prot & VPROT_WRITE) ?
152 ((prot & VPROT_WRITECOPY) ? 'w' : 'W') : '-';
153 buffer[4] = (prot & VPROT_EXEC) ? 'x' : '-';
159 /***********************************************************************
162 static void VIRTUAL_DumpView( FILE_VIEW *view )
165 char *addr = view->base;
166 BYTE prot = view->prot[0];
168 DPRINTF( "View: %p - %p", addr, addr + view->size - 1 );
169 if (view->flags & VFLAG_SYSTEM)
170 DPRINTF( " (system)\n" );
171 else if (view->flags & VFLAG_VALLOC)
172 DPRINTF( " (valloc)\n" );
173 else if (view->mapping)
174 DPRINTF( " %p\n", view->mapping );
176 DPRINTF( " (anonymous)\n");
178 for (count = i = 1; i < view->size >> page_shift; i++, count++)
180 if (view->prot[i] == prot) continue;
181 DPRINTF( " %p - %p %s\n",
182 addr, addr + (count << page_shift) - 1, VIRTUAL_GetProtStr(prot) );
183 addr += (count << page_shift);
184 prot = view->prot[i];
188 DPRINTF( " %p - %p %s\n",
189 addr, addr + (count << page_shift) - 1, VIRTUAL_GetProtStr(prot) );
193 /***********************************************************************
196 void VIRTUAL_Dump(void)
199 DPRINTF( "\nDump of all virtual memory views:\n\n" );
200 RtlEnterCriticalSection(&csVirtual);
201 view = VIRTUAL_FirstView;
204 VIRTUAL_DumpView( view );
207 RtlLeaveCriticalSection(&csVirtual);
211 /***********************************************************************
214 * Find the view containing a given address.
220 static FILE_VIEW *VIRTUAL_FindView( const void *addr ) /* [in] Address */
224 RtlEnterCriticalSection(&csVirtual);
225 view = VIRTUAL_FirstView;
228 if (view->base > addr)
233 if ((char*)view->base + view->size > (char*)addr) break;
236 RtlLeaveCriticalSection(&csVirtual);
241 /***********************************************************************
244 * Create a new view and add it in the linked list.
246 static FILE_VIEW *VIRTUAL_CreateView( void *base, UINT size, UINT flags,
247 BYTE vprot, HANDLE mapping )
249 FILE_VIEW *view, *prev;
251 /* Create the view structure */
253 assert( !((unsigned int)base & page_mask) );
254 assert( !(size & page_mask) );
256 if (!(view = (FILE_VIEW *)malloc( sizeof(*view) + size - 1 ))) return NULL;
258 view->size = size << page_shift;
260 view->mapping = mapping;
261 view->protect = vprot;
262 view->handlerProc = NULL;
263 memset( view->prot, vprot, size );
265 /* Duplicate the mapping handle */
268 NtDuplicateObject( GetCurrentProcess(), view->mapping,
269 GetCurrentProcess(), &view->mapping,
270 0, 0, DUPLICATE_SAME_ACCESS ))
276 /* Insert it in the linked list */
278 RtlEnterCriticalSection(&csVirtual);
279 if (!VIRTUAL_FirstView || (VIRTUAL_FirstView->base > base))
281 view->next = VIRTUAL_FirstView;
283 if (view->next) view->next->prev = view;
284 VIRTUAL_FirstView = view;
288 prev = VIRTUAL_FirstView;
289 while (prev->next && (prev->next->base < base)) prev = prev->next;
290 view->next = prev->next;
292 if (view->next) view->next->prev = view;
295 RtlLeaveCriticalSection(&csVirtual);
296 VIRTUAL_DEBUG_DUMP_VIEW( view );
301 /***********************************************************************
308 static void VIRTUAL_DeleteView( FILE_VIEW *view ) /* [in] View */
310 if (!(view->flags & VFLAG_SYSTEM))
311 munmap( (void *)view->base, view->size );
312 RtlEnterCriticalSection(&csVirtual);
313 if (view->next) view->next->prev = view->prev;
314 if (view->prev) view->prev->next = view->next;
315 else VIRTUAL_FirstView = view->next;
316 RtlLeaveCriticalSection(&csVirtual);
317 if (view->mapping) NtClose( view->mapping );
322 /***********************************************************************
323 * VIRTUAL_GetUnixProt
325 * Convert page protections to protection for mmap/mprotect.
327 static int VIRTUAL_GetUnixProt( BYTE vprot )
330 if ((vprot & VPROT_COMMITTED) && !(vprot & VPROT_GUARD))
332 if (vprot & VPROT_READ) prot |= PROT_READ;
333 if (vprot & VPROT_WRITE) prot |= PROT_WRITE;
334 if (vprot & VPROT_WRITECOPY) prot |= PROT_WRITE;
335 if (vprot & VPROT_EXEC) prot |= PROT_EXEC;
341 /***********************************************************************
342 * VIRTUAL_GetWin32Prot
344 * Convert page protections to Win32 flags.
349 static void VIRTUAL_GetWin32Prot(
350 BYTE vprot, /* [in] Page protection flags */
351 DWORD *protect, /* [out] Location to store Win32 protection flags */
352 DWORD *state ) /* [out] Location to store mem state flag */
355 *protect = VIRTUAL_Win32Flags[vprot & 0x0f];
356 /* if (vprot & VPROT_GUARD) *protect |= PAGE_GUARD;*/
357 if (vprot & VPROT_NOCACHE) *protect |= PAGE_NOCACHE;
359 if (vprot & VPROT_GUARD) *protect = PAGE_NOACCESS;
362 if (state) *state = (vprot & VPROT_COMMITTED) ? MEM_COMMIT : MEM_RESERVE;
366 /***********************************************************************
369 * Build page protections from Win32 flags.
372 * Value of page protection flags
374 static BYTE VIRTUAL_GetProt( DWORD protect ) /* [in] Win32 protection flags */
378 switch(protect & 0xff)
384 vprot = VPROT_READ | VPROT_WRITE;
387 /* MSDN CreateFileMapping() states that if PAGE_WRITECOPY is given,
388 * that the hFile must have been opened with GENERIC_READ and
389 * GENERIC_WRITE access. This is WRONG as tests show that you
390 * only need GENERIC_READ access (at least for Win9x,
391 * FIXME: what about NT?). Thus, we don't put VPROT_WRITE in
392 * PAGE_WRITECOPY and PAGE_EXECUTE_WRITECOPY.
394 vprot = VPROT_READ | VPROT_WRITECOPY;
399 case PAGE_EXECUTE_READ:
400 vprot = VPROT_EXEC | VPROT_READ;
402 case PAGE_EXECUTE_READWRITE:
403 vprot = VPROT_EXEC | VPROT_READ | VPROT_WRITE;
405 case PAGE_EXECUTE_WRITECOPY:
406 /* See comment for PAGE_WRITECOPY above */
407 vprot = VPROT_EXEC | VPROT_READ | VPROT_WRITECOPY;
414 if (protect & PAGE_GUARD) vprot |= VPROT_GUARD;
415 if (protect & PAGE_NOCACHE) vprot |= VPROT_NOCACHE;
420 /***********************************************************************
423 * Change the protection of a range of pages.
429 static BOOL VIRTUAL_SetProt( FILE_VIEW *view, /* [in] Pointer to view */
430 void *base, /* [in] Starting address */
431 UINT size, /* [in] Size in bytes */
432 BYTE vprot ) /* [in] Protections to use */
435 base, (char *)base + size - 1, VIRTUAL_GetProtStr( vprot ) );
437 if (mprotect( base, size, VIRTUAL_GetUnixProt(vprot) ))
438 return FALSE; /* FIXME: last error */
440 memset( view->prot + (((char *)base - (char *)view->base) >> page_shift),
441 vprot, size >> page_shift );
442 VIRTUAL_DEBUG_DUMP_VIEW( view );
447 /***********************************************************************
450 * Create an anonymous mapping aligned to the allocation granularity.
452 static NTSTATUS anon_mmap_aligned( void **addr, unsigned int size, int prot, int flags )
454 void *ptr, *base = *addr;
455 unsigned int view_size = size + (base ? 0 : granularity_mask + 1);
457 if ((ptr = wine_anon_mmap( base, view_size, prot, flags )) == (void *)-1)
459 if (errno == ENOMEM) return STATUS_NO_MEMORY;
460 return STATUS_INVALID_PARAMETER;
465 /* Release the extra memory while keeping the range
466 * starting on the granularity boundary. */
467 if ((unsigned int)ptr & granularity_mask)
469 unsigned int extra = granularity_mask + 1 - ((unsigned int)ptr & granularity_mask);
470 munmap( ptr, extra );
471 ptr = (char *)ptr + extra;
474 if (view_size > size)
475 munmap( (char *)ptr + size, view_size - size );
477 else if (ptr != base)
479 /* We couldn't get the address we wanted */
480 munmap( ptr, view_size );
481 return STATUS_CONFLICTING_ADDRESSES;
484 return STATUS_SUCCESS;
488 /***********************************************************************
491 * Apply the relocations to a mapped PE image
493 static int do_relocations( char *base, const IMAGE_DATA_DIRECTORY *dir,
494 int delta, DWORD total_size )
496 IMAGE_BASE_RELOCATION *rel;
498 TRACE_(module)( "relocating from %p-%p to %p-%p\n",
499 base - delta, base - delta + total_size, base, base + total_size );
501 for (rel = (IMAGE_BASE_RELOCATION *)(base + dir->VirtualAddress);
502 ((char *)rel < base + dir->VirtualAddress + dir->Size) && rel->SizeOfBlock;
503 rel = (IMAGE_BASE_RELOCATION*)((char*)rel + rel->SizeOfBlock) )
505 char *page = base + rel->VirtualAddress;
506 WORD *TypeOffset = (WORD *)(rel + 1);
507 int i, count = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(*TypeOffset);
509 if (!count) continue;
512 if ((char *)rel + rel->SizeOfBlock > base + dir->VirtualAddress + dir->Size ||
513 page > base + total_size)
515 ERR_(module)("invalid relocation %p,%lx,%ld at %p,%lx,%lx\n",
516 rel, rel->VirtualAddress, rel->SizeOfBlock,
517 base, dir->VirtualAddress, dir->Size );
521 TRACE_(module)("%ld relocations for page %lx\n", rel->SizeOfBlock, rel->VirtualAddress);
523 /* patching in reverse order */
524 for (i = 0 ; i < count; i++)
526 int offset = TypeOffset[i] & 0xFFF;
527 int type = TypeOffset[i] >> 12;
530 case IMAGE_REL_BASED_ABSOLUTE:
532 case IMAGE_REL_BASED_HIGH:
533 *(short*)(page+offset) += HIWORD(delta);
535 case IMAGE_REL_BASED_LOW:
536 *(short*)(page+offset) += LOWORD(delta);
538 case IMAGE_REL_BASED_HIGHLOW:
539 *(int*)(page+offset) += delta;
540 /* FIXME: if this is an exported address, fire up enhanced logic */
543 FIXME_(module)("Unknown/unsupported fixup type %d.\n", type);
552 /***********************************************************************
555 * Map an executable (PE format) image into memory.
557 static NTSTATUS map_image( HANDLE hmapping, int fd, char *base, DWORD total_size,
558 DWORD header_size, int shared_fd, DWORD shared_size,
559 BOOL removable, PVOID *addr_ptr )
561 IMAGE_DOS_HEADER *dos;
562 IMAGE_NT_HEADERS *nt;
563 IMAGE_SECTION_HEADER *sec;
564 IMAGE_DATA_DIRECTORY *imports;
565 NTSTATUS status = STATUS_INVALID_IMAGE_FORMAT; /* generic error (FIXME) */
570 /* zero-map the whole range */
572 if (base < (char *)0x110000 || /* make sure the DOS area remains free */
573 (ptr = wine_anon_mmap( base, total_size,
574 PROT_READ | PROT_WRITE | PROT_EXEC, 0 )) == (char *)-1)
576 ptr = wine_anon_mmap( NULL, total_size,
577 PROT_READ | PROT_WRITE | PROT_EXEC, 0 );
578 if (ptr == (char *)-1)
580 ERR_(module)("Not enough memory for module (%ld bytes)\n", total_size);
584 TRACE_(module)( "mapped PE file at %p-%p\n", ptr, ptr + total_size );
588 if (VIRTUAL_mmap( fd, ptr, header_size, 0, 0, PROT_READ,
589 MAP_PRIVATE | MAP_FIXED, &removable ) == (char *)-1) goto error;
590 dos = (IMAGE_DOS_HEADER *)ptr;
591 nt = (IMAGE_NT_HEADERS *)(ptr + dos->e_lfanew);
592 if ((char *)(nt + 1) > ptr + header_size) goto error;
594 sec = (IMAGE_SECTION_HEADER*)((char*)&nt->OptionalHeader+nt->FileHeader.SizeOfOptionalHeader);
595 if ((char *)(sec + nt->FileHeader.NumberOfSections) > ptr + header_size) goto error;
597 imports = nt->OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_IMPORT;
598 if (!imports->Size || !imports->VirtualAddress) imports = NULL;
600 /* check the architecture */
602 if (nt->FileHeader.Machine != IMAGE_FILE_MACHINE_I386)
604 MESSAGE("Trying to load PE image for unsupported architecture (");
605 switch (nt->FileHeader.Machine)
607 case IMAGE_FILE_MACHINE_UNKNOWN: MESSAGE("Unknown"); break;
608 case IMAGE_FILE_MACHINE_I860: MESSAGE("I860"); break;
609 case IMAGE_FILE_MACHINE_R3000: MESSAGE("R3000"); break;
610 case IMAGE_FILE_MACHINE_R4000: MESSAGE("R4000"); break;
611 case IMAGE_FILE_MACHINE_R10000: MESSAGE("R10000"); break;
612 case IMAGE_FILE_MACHINE_ALPHA: MESSAGE("Alpha"); break;
613 case IMAGE_FILE_MACHINE_POWERPC: MESSAGE("PowerPC"); break;
614 default: MESSAGE("Unknown-%04x", nt->FileHeader.Machine); break;
620 /* map all the sections */
622 for (i = pos = 0; i < nt->FileHeader.NumberOfSections; i++, sec++)
626 /* a few sanity checks */
627 size = sec->VirtualAddress + ROUND_SIZE( sec->VirtualAddress, sec->Misc.VirtualSize );
628 if (sec->VirtualAddress > total_size || size > total_size || size < sec->VirtualAddress)
630 ERR_(module)( "Section %.8s too large (%lx+%lx/%lx)\n",
631 sec->Name, sec->VirtualAddress, sec->Misc.VirtualSize, total_size );
635 if ((sec->Characteristics & IMAGE_SCN_MEM_SHARED) &&
636 (sec->Characteristics & IMAGE_SCN_MEM_WRITE))
638 size = ROUND_SIZE( 0, sec->Misc.VirtualSize );
639 TRACE_(module)( "mapping shared section %.8s at %p off %lx (%x) size %lx (%lx) flags %lx\n",
640 sec->Name, ptr + sec->VirtualAddress,
641 sec->PointerToRawData, pos, sec->SizeOfRawData,
642 size, sec->Characteristics );
643 if (VIRTUAL_mmap( shared_fd, ptr + sec->VirtualAddress, size,
644 pos, 0, PROT_READ|PROT_WRITE|PROT_EXEC,
645 MAP_SHARED|MAP_FIXED, NULL ) == (void *)-1)
647 ERR_(module)( "Could not map shared section %.8s\n", sec->Name );
651 /* check if the import directory falls inside this section */
652 if (imports && imports->VirtualAddress >= sec->VirtualAddress &&
653 imports->VirtualAddress < sec->VirtualAddress + size)
655 UINT_PTR base = imports->VirtualAddress & ~page_mask;
656 UINT_PTR end = base + ROUND_SIZE( imports->VirtualAddress, imports->Size );
657 if (end > sec->VirtualAddress + size) end = sec->VirtualAddress + size;
658 if (end > base) VIRTUAL_mmap( shared_fd, ptr + base, end - base,
659 pos + (base - sec->VirtualAddress), 0,
660 PROT_READ|PROT_WRITE|PROT_EXEC,
661 MAP_PRIVATE|MAP_FIXED, NULL );
667 TRACE_(module)( "mapping section %.8s at %p off %lx size %lx flags %lx\n",
668 sec->Name, ptr + sec->VirtualAddress,
669 sec->PointerToRawData, sec->SizeOfRawData,
670 sec->Characteristics );
672 if ((sec->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA) &&
673 !(sec->Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)) continue;
674 if (!sec->PointerToRawData || !sec->SizeOfRawData) continue;
676 /* Note: if the section is not aligned properly VIRTUAL_mmap will magically
677 * fall back to read(), so we don't need to check anything here.
679 if (VIRTUAL_mmap( fd, ptr + sec->VirtualAddress, sec->SizeOfRawData,
680 sec->PointerToRawData, 0, PROT_READ|PROT_WRITE|PROT_EXEC,
681 MAP_PRIVATE | MAP_FIXED, &removable ) == (void *)-1)
683 ERR_(module)( "Could not map section %.8s, file probably truncated\n", sec->Name );
687 if ((sec->SizeOfRawData < sec->Misc.VirtualSize) && (sec->SizeOfRawData & page_mask))
689 DWORD end = ROUND_SIZE( 0, sec->SizeOfRawData );
690 if (end > sec->Misc.VirtualSize) end = sec->Misc.VirtualSize;
691 TRACE_(module)("clearing %p - %p\n",
692 ptr + sec->VirtualAddress + sec->SizeOfRawData,
693 ptr + sec->VirtualAddress + end );
694 memset( ptr + sec->VirtualAddress + sec->SizeOfRawData, 0,
695 end - sec->SizeOfRawData );
700 /* perform base relocation, if necessary */
704 const IMAGE_DATA_DIRECTORY *relocs;
706 relocs = &nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC];
707 if (!relocs->VirtualAddress || !relocs->Size)
709 if (nt->OptionalHeader.ImageBase == 0x400000)
710 ERR("Standard load address for a Win32 program (0x00400000) not available - security-patched kernel ?\n");
712 ERR( "FATAL: Need to relocate module from addr %lx, but there are no relocation records\n",
713 nt->OptionalHeader.ImageBase );
717 /* FIXME: If we need to relocate a system DLL (base > 2GB) we should
718 * really make sure that the *new* base address is also > 2GB.
719 * Some DLLs really check the MSB of the module handle :-/
721 if ((nt->OptionalHeader.ImageBase & 0x80000000) && !((DWORD)base & 0x80000000))
722 ERR( "Forced to relocate system DLL (base > 2GB). This is not good.\n" );
724 if (!do_relocations( ptr, relocs, ptr - base, total_size ))
730 if (removable) hmapping = 0; /* don't keep handle open on removable media */
731 if (!(view = VIRTUAL_CreateView( ptr, total_size, 0, VPROT_COMMITTED|VPROT_READ, hmapping )))
733 status = STATUS_NO_MEMORY;
737 /* set the image protections */
739 sec = (IMAGE_SECTION_HEADER*)((char *)&nt->OptionalHeader+nt->FileHeader.SizeOfOptionalHeader);
740 for (i = 0; i < nt->FileHeader.NumberOfSections; i++, sec++)
742 DWORD size = ROUND_SIZE( sec->VirtualAddress, sec->Misc.VirtualSize );
743 BYTE vprot = VPROT_COMMITTED;
744 if (sec->Characteristics & IMAGE_SCN_MEM_READ) vprot |= VPROT_READ;
745 if (sec->Characteristics & IMAGE_SCN_MEM_WRITE) vprot |= VPROT_WRITE|VPROT_WRITECOPY;
746 if (sec->Characteristics & IMAGE_SCN_MEM_EXECUTE) vprot |= VPROT_EXEC;
748 /* make sure the import directory is writable */
749 if (imports && imports->VirtualAddress >= sec->VirtualAddress &&
750 imports->VirtualAddress < sec->VirtualAddress + size)
751 vprot |= VPROT_READ|VPROT_WRITE|VPROT_WRITECOPY;
753 VIRTUAL_SetProt( view, ptr + sec->VirtualAddress, size, vprot );
758 return STATUS_SUCCESS;
761 if (ptr != (char *)-1) munmap( ptr, total_size );
767 /***********************************************************************
770 * Check whether a process handle is for the current process.
772 static BOOL is_current_process( HANDLE handle )
776 if (handle == GetCurrentProcess()) return TRUE;
777 SERVER_START_REQ( get_process_info )
779 req->handle = handle;
780 if (!wine_server_call( req ))
781 ret = ((DWORD)reply->pid == GetCurrentProcessId());
788 /***********************************************************************
792 DECL_GLOBAL_CONSTRUCTOR(VIRTUAL_Init)
794 page_size = getpagesize();
795 page_mask = page_size - 1;
796 /* Make sure we have a power of 2 */
797 assert( !(page_size & page_mask) );
799 while ((1 << page_shift) != page_size) page_shift++;
801 #endif /* page_mask */
804 /***********************************************************************
805 * VIRTUAL_SetFaultHandler
807 BOOL VIRTUAL_SetFaultHandler( LPCVOID addr, HANDLERPROC proc, LPVOID arg )
811 if (!(view = VIRTUAL_FindView( addr ))) return FALSE;
812 view->handlerProc = proc;
813 view->handlerArg = arg;
817 /***********************************************************************
818 * VIRTUAL_HandleFault
820 DWORD VIRTUAL_HandleFault( LPCVOID addr )
822 FILE_VIEW *view = VIRTUAL_FindView( addr );
823 DWORD ret = EXCEPTION_ACCESS_VIOLATION;
827 if (view->handlerProc)
829 if (view->handlerProc(view->handlerArg, addr)) ret = 0; /* handled */
833 BYTE vprot = view->prot[((char *)addr - (char *)view->base) >> page_shift];
834 void *page = (void *)((UINT_PTR)addr & ~page_mask);
835 char *stack = (char *)NtCurrentTeb()->DeallocationStack + SIGNAL_STACK_SIZE;
836 if (vprot & VPROT_GUARD)
838 VIRTUAL_SetProt( view, page, page_mask + 1, vprot & ~VPROT_GUARD );
839 ret = STATUS_GUARD_PAGE_VIOLATION;
841 /* is it inside the stack guard page? */
842 if (((char *)addr >= stack) && ((char *)addr < stack + (page_mask+1)))
843 ret = STATUS_STACK_OVERFLOW;
851 /***********************************************************************
854 * Linux kernels before 2.4.x can support non page-aligned offsets, as
855 * long as the offset is aligned to the filesystem block size. This is
856 * a big performance gain so we want to take advantage of it.
858 * However, when we use 64-bit file support this doesn't work because
859 * glibc rejects unaligned offsets. Also glibc 2.1.3 mmap64 is broken
860 * in that it rounds unaligned offsets down to a page boundary. For
861 * these reasons we do a direct system call here.
863 static void *unaligned_mmap( void *addr, size_t length, unsigned int prot,
864 unsigned int flags, int fd, unsigned int offset_low,
865 unsigned int offset_high )
867 #if defined(linux) && defined(__i386__) && defined(__GNUC__)
868 if (!offset_high && (offset_low & page_mask))
883 args.length = length;
887 args.offset = offset_low;
889 __asm__ __volatile__("push %%ebx\n\t"
894 : "0" (90), /* SYS_mmap */
896 if (ret < 0 && ret > -4096)
904 return mmap( addr, length, prot, flags, fd, ((off_t)offset_high << 32) | offset_low );
908 /***********************************************************************
911 * Wrapper for mmap() that handles anonymous mappings portably,
912 * and falls back to read if mmap of a file fails.
914 static LPVOID VIRTUAL_mmap( int fd, LPVOID start, DWORD size,
915 DWORD offset_low, DWORD offset_high,
916 int prot, int flags, BOOL *removable )
921 BOOL is_shared_write = FALSE;
923 if (fd == -1) return wine_anon_mmap( start, size, prot, flags );
925 if (prot & PROT_WRITE)
928 if (flags & MAP_SHARED) is_shared_write = TRUE;
931 if (!(flags & MAP_PRIVATE)) is_shared_write = TRUE;
935 if (removable && *removable)
937 /* if on removable media, try using read instead of mmap */
938 if (!is_shared_write) goto fake_mmap;
942 if ((ret = unaligned_mmap( start, size, prot, flags, fd,
943 offset_low, offset_high )) != (LPVOID)-1) return ret;
945 /* mmap() failed; if this is because the file offset is not */
946 /* page-aligned (EINVAL), or because the underlying filesystem */
947 /* does not support mmap() (ENOEXEC,ENODEV), we do it by hand. */
949 if ((errno != ENOEXEC) && (errno != EINVAL) && (errno != ENODEV)) return ret;
950 if (is_shared_write) return ret; /* we cannot fake shared write mappings */
953 /* Reserve the memory with an anonymous mmap */
954 ret = wine_anon_mmap( start, size, PROT_READ | PROT_WRITE, flags );
955 if (ret == (LPVOID)-1) return ret;
956 /* Now read in the file */
957 offset = ((off_t)offset_high << 32) | offset_low;
958 if ((pos = lseek( fd, offset, SEEK_SET )) == -1)
963 read( fd, ret, size );
964 lseek( fd, pos, SEEK_SET ); /* Restore the file pointer */
965 mprotect( ret, size, prot ); /* Set the right protection */
970 /***********************************************************************
971 * NtAllocateVirtualMemory (NTDLL.@)
972 * ZwAllocateVirtualMemory (NTDLL.@)
974 NTSTATUS WINAPI NtAllocateVirtualMemory( HANDLE process, PVOID *ret, PVOID addr,
975 ULONG *size_ptr, ULONG type, ULONG protect )
980 DWORD size = *size_ptr;
982 if (!is_current_process( process ))
984 ERR("Unsupported on other process\n");
985 return STATUS_ACCESS_DENIED;
988 TRACE("%p %08lx %lx %08lx\n", addr, size, type, protect );
990 /* Round parameters to a page boundary */
992 if (size > 0x7fc00000) return STATUS_WORKING_SET_LIMIT_RANGE; /* 2Gb - 4Mb */
996 if (type & MEM_RESERVE) /* Round down to 64k boundary */
997 base = ROUND_ADDR( addr, granularity_mask );
999 base = ROUND_ADDR( addr, page_mask );
1000 size = (((UINT_PTR)addr + size + page_mask) & ~page_mask) - (UINT_PTR)base;
1002 /* disallow low 64k, wrap-around and kernel space */
1003 if (((char *)base <= (char *)granularity_mask) ||
1004 ((char *)base + size < (char *)base) ||
1005 (ADDRESS_SPACE_LIMIT && ((char *)base + size > (char *)ADDRESS_SPACE_LIMIT)))
1006 return STATUS_INVALID_PARAMETER;
1011 size = (size + page_mask) & ~page_mask;
1014 if (type & MEM_TOP_DOWN) {
1015 /* FIXME: MEM_TOP_DOWN allocates the largest possible address.
1016 * Is there _ANY_ way to do it with UNIX mmap()?
1018 WARN("MEM_TOP_DOWN ignored\n");
1019 type &= ~MEM_TOP_DOWN;
1022 /* Compute the alloc type flags */
1024 if (!(type & (MEM_COMMIT | MEM_RESERVE | MEM_SYSTEM)) ||
1025 (type & ~(MEM_COMMIT | MEM_RESERVE | MEM_SYSTEM)))
1027 ERR("called with wrong alloc type flags (%08lx) !\n", type);
1028 return STATUS_INVALID_PARAMETER;
1030 if (type & (MEM_COMMIT | MEM_SYSTEM))
1031 vprot = VIRTUAL_GetProt( protect ) | VPROT_COMMITTED;
1034 /* Reserve the memory */
1036 if ((type & MEM_RESERVE) || !base)
1038 if (type & MEM_SYSTEM)
1040 if (!(view = VIRTUAL_CreateView( base, size, VFLAG_VALLOC | VFLAG_SYSTEM, vprot, 0 )))
1041 return STATUS_NO_MEMORY;
1045 NTSTATUS res = anon_mmap_aligned( &base, size, VIRTUAL_GetUnixProt( vprot ), 0 );
1046 if (res) return res;
1048 if (!(view = VIRTUAL_CreateView( base, size, VFLAG_VALLOC, vprot, 0 )))
1050 munmap( base, size );
1051 return STATUS_NO_MEMORY;
1057 /* Commit the pages */
1059 if (!(view = VIRTUAL_FindView( base )) ||
1060 ((char *)base + size > (char *)view->base + view->size)) return STATUS_NOT_MAPPED_VIEW;
1062 if (!VIRTUAL_SetProt( view, base, size, vprot )) return STATUS_ACCESS_DENIED;
1067 return STATUS_SUCCESS;
1071 /***********************************************************************
1072 * NtFreeVirtualMemory (NTDLL.@)
1073 * ZwFreeVirtualMemory (NTDLL.@)
1075 NTSTATUS WINAPI NtFreeVirtualMemory( HANDLE process, PVOID *addr_ptr, ULONG *size_ptr, ULONG type )
1079 LPVOID addr = *addr_ptr;
1080 DWORD size = *size_ptr;
1082 if (!is_current_process( process ))
1084 ERR("Unsupported on other process\n");
1085 return STATUS_ACCESS_DENIED;
1088 TRACE("%p %08lx %lx\n", addr, size, type );
1090 /* Fix the parameters */
1092 size = ROUND_SIZE( addr, size );
1093 base = ROUND_ADDR( addr, page_mask );
1095 if (!(view = VIRTUAL_FindView( base )) ||
1096 (base + size > (char *)view->base + view->size) ||
1097 !(view->flags & VFLAG_VALLOC))
1098 return STATUS_INVALID_PARAMETER;
1100 /* Check the type */
1102 if (type & MEM_SYSTEM)
1104 view->flags |= VFLAG_SYSTEM;
1105 type &= ~MEM_SYSTEM;
1108 if ((type != MEM_DECOMMIT) && (type != MEM_RELEASE))
1110 ERR("called with wrong free type flags (%08lx) !\n", type);
1111 return STATUS_INVALID_PARAMETER;
1114 /* Free the pages */
1116 if (type == MEM_RELEASE)
1118 if (size || (base != view->base)) return STATUS_INVALID_PARAMETER;
1119 VIRTUAL_DeleteView( view );
1123 /* Decommit the pages by remapping zero-pages instead */
1125 if (wine_anon_mmap( (LPVOID)base, size, VIRTUAL_GetUnixProt(0), MAP_FIXED ) != (LPVOID)base)
1126 ERR( "Could not remap pages, expect trouble\n" );
1127 if (!VIRTUAL_SetProt( view, base, size, 0 )) return STATUS_ACCESS_DENIED; /* FIXME */
1132 return STATUS_SUCCESS;
1136 /***********************************************************************
1137 * NtProtectVirtualMemory (NTDLL.@)
1138 * ZwProtectVirtualMemory (NTDLL.@)
1140 NTSTATUS WINAPI NtProtectVirtualMemory( HANDLE process, PVOID *addr_ptr, ULONG *size_ptr,
1141 ULONG new_prot, ULONG *old_prot )
1147 DWORD prot, size = *size_ptr;
1148 LPVOID addr = *addr_ptr;
1150 if (!is_current_process( process ))
1152 ERR("Unsupported on other process\n");
1153 return STATUS_ACCESS_DENIED;
1156 TRACE("%p %08lx %08lx\n", addr, size, new_prot );
1158 /* Fix the parameters */
1160 size = ROUND_SIZE( addr, size );
1161 base = ROUND_ADDR( addr, page_mask );
1163 if (!(view = VIRTUAL_FindView( base )) ||
1164 (base + size > (char *)view->base + view->size))
1165 return STATUS_INVALID_PARAMETER;
1167 /* Make sure all the pages are committed */
1169 p = view->prot + ((base - (char *)view->base) >> page_shift);
1170 VIRTUAL_GetWin32Prot( *p, &prot, NULL );
1171 for (i = size >> page_shift; i; i--, p++)
1173 if (!(*p & VPROT_COMMITTED)) return STATUS_INVALID_PARAMETER;
1176 if (old_prot) *old_prot = prot;
1177 vprot = VIRTUAL_GetProt( new_prot ) | VPROT_COMMITTED;
1178 if (!VIRTUAL_SetProt( view, base, size, vprot )) return STATUS_ACCESS_DENIED;
1182 return STATUS_SUCCESS;
1186 /***********************************************************************
1187 * NtQueryVirtualMemory (NTDLL.@)
1188 * ZwQueryVirtualMemory (NTDLL.@)
1190 NTSTATUS WINAPI NtQueryVirtualMemory( HANDLE process, LPCVOID addr,
1191 MEMORY_INFORMATION_CLASS info_class, PVOID buffer,
1192 ULONG len, ULONG *res_len )
1195 char *base, *alloc_base = 0;
1197 MEMORY_BASIC_INFORMATION *info = buffer;
1199 if (info_class != MemoryBasicInformation) return STATUS_INVALID_INFO_CLASS;
1200 if (ADDRESS_SPACE_LIMIT && addr >= ADDRESS_SPACE_LIMIT)
1201 return STATUS_WORKING_SET_LIMIT_RANGE; /* FIXME */
1203 if (!is_current_process( process ))
1205 ERR("Unsupported on other process\n");
1206 return STATUS_ACCESS_DENIED;
1209 base = ROUND_ADDR( addr, page_mask );
1211 /* Find the view containing the address */
1213 RtlEnterCriticalSection(&csVirtual);
1214 view = VIRTUAL_FirstView;
1219 size = (char *)ADDRESS_SPACE_LIMIT - alloc_base;
1222 if ((char *)view->base > base)
1224 size = (char *)view->base - alloc_base;
1228 if ((char *)view->base + view->size > base)
1230 alloc_base = view->base;
1234 alloc_base = (char *)view->base + view->size;
1237 RtlLeaveCriticalSection(&csVirtual);
1239 /* Fill the info structure */
1243 info->State = MEM_FREE;
1245 info->AllocationProtect = 0;
1250 BYTE vprot = view->prot[(base - alloc_base) >> page_shift];
1251 VIRTUAL_GetWin32Prot( vprot, &info->Protect, &info->State );
1252 for (size = base - alloc_base; size < view->size; size += page_mask+1)
1253 if (view->prot[size >> page_shift] != vprot) break;
1254 info->AllocationProtect = view->protect;
1255 info->Type = MEM_PRIVATE; /* FIXME */
1258 info->BaseAddress = (LPVOID)base;
1259 info->AllocationBase = (LPVOID)alloc_base;
1260 info->RegionSize = size - (base - alloc_base);
1261 if (res_len) *res_len = sizeof(*info);
1262 return STATUS_SUCCESS;
1266 /***********************************************************************
1267 * NtLockVirtualMemory (NTDLL.@)
1268 * ZwLockVirtualMemory (NTDLL.@)
1270 NTSTATUS WINAPI NtLockVirtualMemory( HANDLE process, PVOID *addr, ULONG *size, ULONG unknown )
1272 if (!is_current_process( process ))
1274 ERR("Unsupported on other process\n");
1275 return STATUS_ACCESS_DENIED;
1277 return STATUS_SUCCESS;
1281 /***********************************************************************
1282 * NtUnlockVirtualMemory (NTDLL.@)
1283 * ZwUnlockVirtualMemory (NTDLL.@)
1285 NTSTATUS WINAPI NtUnlockVirtualMemory( HANDLE process, PVOID *addr, ULONG *size, ULONG unknown )
1287 if (!is_current_process( process ))
1289 ERR("Unsupported on other process\n");
1290 return STATUS_ACCESS_DENIED;
1292 return STATUS_SUCCESS;
1296 /***********************************************************************
1297 * NtCreateSection (NTDLL.@)
1298 * ZwCreateSection (NTDLL.@)
1300 NTSTATUS WINAPI NtCreateSection( HANDLE *handle, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr,
1301 const LARGE_INTEGER *size, ULONG protect,
1302 ULONG sec_flags, HANDLE file )
1306 DWORD len = attr->ObjectName ? attr->ObjectName->Length : 0;
1308 /* Check parameters */
1310 if (len > MAX_PATH*sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
1312 vprot = VIRTUAL_GetProt( protect );
1313 if (sec_flags & SEC_RESERVE)
1315 if (file) return STATUS_INVALID_PARAMETER;
1317 else vprot |= VPROT_COMMITTED;
1318 if (sec_flags & SEC_NOCACHE) vprot |= VPROT_NOCACHE;
1319 if (sec_flags & SEC_IMAGE) vprot |= VPROT_IMAGE;
1321 /* Create the server object */
1323 SERVER_START_REQ( create_mapping )
1325 req->file_handle = file;
1326 req->size_high = size ? size->s.HighPart : 0;
1327 req->size_low = size ? size->s.LowPart : 0;
1328 req->protect = vprot;
1329 req->access = access;
1330 req->inherit = (attr->Attributes & OBJ_INHERIT) != 0;
1331 if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
1332 ret = wine_server_call( req );
1333 *handle = reply->handle;
1340 /***********************************************************************
1341 * NtOpenSection (NTDLL.@)
1342 * ZwOpenSection (NTDLL.@)
1344 NTSTATUS WINAPI NtOpenSection( HANDLE *handle, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr )
1347 DWORD len = attr->ObjectName->Length;
1349 if (len > MAX_PATH*sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
1351 SERVER_START_REQ( open_mapping )
1353 req->access = access;
1354 req->inherit = (attr->Attributes & OBJ_INHERIT) != 0;
1355 wine_server_add_data( req, attr->ObjectName->Buffer, len );
1356 if (!(ret = wine_server_call( req ))) *handle = reply->handle;
1363 /***********************************************************************
1364 * NtMapViewOfSection (NTDLL.@)
1365 * ZwMapViewOfSection (NTDLL.@)
1367 NTSTATUS WINAPI NtMapViewOfSection( HANDLE handle, HANDLE process, PVOID *addr_ptr, ULONG zero_bits,
1368 ULONG commit_size, const LARGE_INTEGER *offset, ULONG *size_ptr,
1369 SECTION_INHERIT inherit, ULONG alloc_type, ULONG protect )
1374 int flags = MAP_PRIVATE;
1375 int unix_handle = -1;
1377 void *base, *ptr = (void *)-1, *ret;
1378 DWORD size_low, size_high, header_size, shared_size;
1382 if (!is_current_process( process ))
1384 ERR("Unsupported on other process\n");
1385 return STATUS_ACCESS_DENIED;
1388 TRACE("handle=%p addr=%p off=%lx%08lx size=%x access=%lx\n",
1389 handle, *addr_ptr, offset->s.HighPart, offset->s.LowPart, size, protect );
1391 /* Check parameters */
1393 if ((offset->s.LowPart & granularity_mask) ||
1394 (*addr_ptr && ((UINT_PTR)*addr_ptr & granularity_mask)))
1395 return STATUS_INVALID_PARAMETER;
1397 SERVER_START_REQ( get_mapping_info )
1399 req->handle = handle;
1400 res = wine_server_call( req );
1401 prot = reply->protect;
1403 size_low = reply->size_low;
1404 size_high = reply->size_high;
1405 header_size = reply->header_size;
1406 shared_file = reply->shared_file;
1407 shared_size = reply->shared_size;
1408 removable = reply->removable;
1411 if (res) goto error;
1413 if ((res = wine_server_handle_to_fd( handle, 0, &unix_handle, NULL, NULL ))) goto error;
1415 if (prot & VPROT_IMAGE)
1421 if ((res = wine_server_handle_to_fd( shared_file, GENERIC_READ, &shared_fd,
1422 NULL, NULL ))) goto error;
1423 NtClose( shared_file ); /* we no longer need it */
1425 res = map_image( handle, unix_handle, base, size_low, header_size,
1426 shared_fd, shared_size, removable, addr_ptr );
1427 if (shared_fd != -1) close( shared_fd );
1428 if (!res) *size_ptr = size_low;
1434 ERR("Sizes larger than 4Gb not supported\n");
1436 if ((offset->s.LowPart >= size_low) ||
1437 (*size_ptr > size_low - offset->s.LowPart))
1439 res = STATUS_INVALID_PARAMETER;
1442 if (*size_ptr) size = ROUND_SIZE( offset->s.LowPart, *size_ptr );
1443 else size = size_low - offset->s.LowPart;
1449 case PAGE_READWRITE:
1450 case PAGE_EXECUTE_READWRITE:
1451 if (!(prot & VPROT_WRITE))
1453 res = STATUS_INVALID_PARAMETER;
1459 case PAGE_WRITECOPY:
1461 case PAGE_EXECUTE_READ:
1462 case PAGE_EXECUTE_WRITECOPY:
1463 if (prot & VPROT_READ) break;
1466 res = STATUS_INVALID_PARAMETER;
1470 /* FIXME: If a mapping is created with SEC_RESERVE and a process,
1471 * which has a view of this mapping commits some pages, they will
1472 * appear commited in all other processes, which have the same
1473 * view created. Since we don`t support this yet, we create the
1474 * whole mapping commited.
1476 prot |= VPROT_COMMITTED;
1478 /* Reserve a properly aligned area */
1480 if ((res = anon_mmap_aligned( addr_ptr, size, PROT_NONE, 0 ))) goto error;
1485 TRACE("handle=%p size=%x offset=%lx\n", handle, size, offset->s.LowPart );
1487 ret = VIRTUAL_mmap( unix_handle, ptr, size, offset->s.LowPart, offset->s.HighPart,
1488 VIRTUAL_GetUnixProt( prot ), flags | MAP_FIXED, &removable );
1491 ERR( "VIRTUAL_mmap %p %x %lx%08lx failed\n",
1492 ptr, size, offset->s.HighPart, offset->s.LowPart );
1493 res = STATUS_NO_MEMORY; /* FIXME */
1496 if (removable) handle = 0; /* don't keep handle open on removable media */
1498 if (!(view = VIRTUAL_CreateView( ptr, size, 0, prot, handle )))
1500 res = STATUS_NO_MEMORY;
1503 if (unix_handle != -1) close( unix_handle );
1505 return STATUS_SUCCESS;
1508 if (unix_handle != -1) close( unix_handle );
1509 if (ptr != (void *)-1) munmap( ptr, size );
1514 /***********************************************************************
1515 * NtUnmapViewOfSection (NTDLL.@)
1516 * ZwUnmapViewOfSection (NTDLL.@)
1518 NTSTATUS WINAPI NtUnmapViewOfSection( HANDLE process, PVOID addr )
1521 void *base = ROUND_ADDR( addr, page_mask );
1523 if (!is_current_process( process ))
1525 ERR("Unsupported on other process\n");
1526 return STATUS_ACCESS_DENIED;
1528 if (!(view = VIRTUAL_FindView( base )) || (base != view->base)) return STATUS_INVALID_PARAMETER;
1529 VIRTUAL_DeleteView( view );
1530 return STATUS_SUCCESS;
1534 /***********************************************************************
1535 * NtFlushVirtualMemory (NTDLL.@)
1536 * ZwFlushVirtualMemory (NTDLL.@)
1538 NTSTATUS WINAPI NtFlushVirtualMemory( HANDLE process, LPCVOID *addr_ptr,
1539 ULONG *size_ptr, ULONG unknown )
1542 void *addr = ROUND_ADDR( *addr_ptr, page_mask );
1544 if (!is_current_process( process ))
1546 ERR("Unsupported on other process\n");
1547 return STATUS_ACCESS_DENIED;
1549 if (!(view = VIRTUAL_FindView( addr ))) return STATUS_INVALID_PARAMETER;
1550 if (!*size_ptr) *size_ptr = view->size;
1552 if (!msync( addr, *size_ptr, MS_SYNC )) return STATUS_SUCCESS;
1553 return STATUS_NOT_MAPPED_DATA;
1557 /***********************************************************************
1558 * NtReadVirtualMemory (NTDLL.@)
1559 * ZwReadVirtualMemory (NTDLL.@)
1561 NTSTATUS WINAPI NtReadVirtualMemory( HANDLE process, const void *addr, void *buffer,
1562 SIZE_T size, SIZE_T *bytes_read )
1566 SERVER_START_REQ( read_process_memory )
1568 req->handle = process;
1569 req->addr = (void *)addr;
1570 wine_server_set_reply( req, buffer, size );
1571 if ((status = wine_server_call( req ))) size = 0;
1574 if (bytes_read) *bytes_read = size;
1579 /***********************************************************************
1580 * NtWriteVirtualMemory (NTDLL.@)
1581 * ZwWriteVirtualMemory (NTDLL.@)
1583 NTSTATUS WINAPI NtWriteVirtualMemory( HANDLE process, void *addr, const void *buffer,
1584 SIZE_T size, SIZE_T *bytes_written )
1586 static const unsigned int zero;
1587 unsigned int first_offset, last_offset, first_mask, last_mask;
1590 if (!size) return STATUS_INVALID_PARAMETER;
1592 /* compute the mask for the first int */
1594 first_offset = (unsigned int)addr % sizeof(int);
1595 memset( &first_mask, 0, first_offset );
1597 /* compute the mask for the last int */
1598 last_offset = (size + first_offset) % sizeof(int);
1600 memset( &last_mask, 0xff, last_offset ? last_offset : sizeof(int) );
1602 SERVER_START_REQ( write_process_memory )
1604 req->handle = process;
1605 req->addr = (char *)addr - first_offset;
1606 req->first_mask = first_mask;
1607 req->last_mask = last_mask;
1608 if (first_offset) wine_server_add_data( req, &zero, first_offset );
1609 wine_server_add_data( req, buffer, size );
1610 if (last_offset) wine_server_add_data( req, &zero, sizeof(int) - last_offset );
1612 if ((status = wine_server_call( req ))) size = 0;
1615 if (bytes_written) *bytes_written = size;