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
46 #include "wine/library.h"
47 #include "wine/server.h"
48 #include "wine/debug.h"
49 #include "ntdll_misc.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(virtual);
52 WINE_DECLARE_DEBUG_CHANNEL(module);
61 struct _FV *next; /* Next view */
62 struct _FV *prev; /* Prev view */
63 void *base; /* Base address */
64 UINT size; /* Size in bytes */
65 UINT flags; /* Allocation flags */
66 HANDLE mapping; /* Handle to the file mapping */
67 HANDLERPROC handlerProc; /* Fault handler */
68 LPVOID handlerArg; /* Fault handler argument */
69 BYTE protect; /* Protection for all pages at allocation time */
70 BYTE prot[1]; /* Protection byte for each page */
74 #define VFLAG_SYSTEM 0x01
75 #define VFLAG_VALLOC 0x02 /* allocated by VirtualAlloc */
77 /* Conversion from VPROT_* to Win32 flags */
78 static const BYTE VIRTUAL_Win32Flags[16] =
80 PAGE_NOACCESS, /* 0 */
81 PAGE_READONLY, /* READ */
82 PAGE_READWRITE, /* WRITE */
83 PAGE_READWRITE, /* READ | WRITE */
84 PAGE_EXECUTE, /* EXEC */
85 PAGE_EXECUTE_READ, /* READ | EXEC */
86 PAGE_EXECUTE_READWRITE, /* WRITE | EXEC */
87 PAGE_EXECUTE_READWRITE, /* READ | WRITE | EXEC */
88 PAGE_WRITECOPY, /* WRITECOPY */
89 PAGE_WRITECOPY, /* READ | WRITECOPY */
90 PAGE_WRITECOPY, /* WRITE | WRITECOPY */
91 PAGE_WRITECOPY, /* READ | WRITE | WRITECOPY */
92 PAGE_EXECUTE_WRITECOPY, /* EXEC | WRITECOPY */
93 PAGE_EXECUTE_WRITECOPY, /* READ | EXEC | WRITECOPY */
94 PAGE_EXECUTE_WRITECOPY, /* WRITE | EXEC | WRITECOPY */
95 PAGE_EXECUTE_WRITECOPY /* READ | WRITE | EXEC | WRITECOPY */
99 static FILE_VIEW *VIRTUAL_FirstView;
101 static CRITICAL_SECTION csVirtual;
102 static CRITICAL_SECTION_DEBUG critsect_debug =
105 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
106 0, 0, { 0, (DWORD)(__FILE__ ": csVirtual") }
108 static CRITICAL_SECTION csVirtual = { &critsect_debug, -1, 0, 0, 0, 0 };
111 /* These are always the same on an i386, and it will be faster this way */
112 # define page_mask 0xfff
113 # define page_shift 12
114 # define page_size 0x1000
115 /* Note: ADDRESS_SPACE_LIMIT is a Windows limit, you cannot change it.
116 * If you are on Solaris you need to find a way to avoid having the system
117 * allocate things above 0xc000000. Don't touch that define.
119 # define ADDRESS_SPACE_LIMIT ((void *)0xc0000000) /* top of the user address space */
121 static UINT page_shift;
122 static UINT page_mask;
123 static UINT page_size;
124 # define ADDRESS_SPACE_LIMIT 0 /* no limit needed on other platforms */
125 #endif /* __i386__ */
126 #define granularity_mask 0xffff /* Allocation granularity (usually 64k) */
128 #define ROUND_ADDR(addr,mask) \
129 ((void *)((UINT_PTR)(addr) & ~(mask)))
131 #define ROUND_SIZE(addr,size) \
132 (((UINT)(size) + ((UINT_PTR)(addr) & page_mask) + page_mask) & ~page_mask)
134 #define VIRTUAL_DEBUG_DUMP_VIEW(view) \
135 if (!TRACE_ON(virtual)); else VIRTUAL_DumpView(view)
137 static LPVOID VIRTUAL_mmap( int fd, LPVOID start, DWORD size, DWORD offset_low,
138 DWORD offset_high, int prot, int flags, BOOL *removable );
141 /***********************************************************************
144 static const char *VIRTUAL_GetProtStr( BYTE prot )
146 static char buffer[6];
147 buffer[0] = (prot & VPROT_COMMITTED) ? 'c' : '-';
148 buffer[1] = (prot & VPROT_GUARD) ? 'g' : '-';
149 buffer[2] = (prot & VPROT_READ) ? 'r' : '-';
150 buffer[3] = (prot & VPROT_WRITE) ?
151 ((prot & VPROT_WRITECOPY) ? 'w' : 'W') : '-';
152 buffer[4] = (prot & VPROT_EXEC) ? 'x' : '-';
158 /***********************************************************************
161 static void VIRTUAL_DumpView( FILE_VIEW *view )
164 char *addr = view->base;
165 BYTE prot = view->prot[0];
167 DPRINTF( "View: %p - %p", addr, addr + view->size - 1 );
168 if (view->flags & VFLAG_SYSTEM)
169 DPRINTF( " (system)\n" );
170 else if (view->flags & VFLAG_VALLOC)
171 DPRINTF( " (valloc)\n" );
172 else if (view->mapping)
173 DPRINTF( " %p\n", view->mapping );
175 DPRINTF( " (anonymous)\n");
177 for (count = i = 1; i < view->size >> page_shift; i++, count++)
179 if (view->prot[i] == prot) continue;
180 DPRINTF( " %p - %p %s\n",
181 addr, addr + (count << page_shift) - 1, VIRTUAL_GetProtStr(prot) );
182 addr += (count << page_shift);
183 prot = view->prot[i];
187 DPRINTF( " %p - %p %s\n",
188 addr, addr + (count << page_shift) - 1, VIRTUAL_GetProtStr(prot) );
192 /***********************************************************************
195 void VIRTUAL_Dump(void)
198 DPRINTF( "\nDump of all virtual memory views:\n\n" );
199 RtlEnterCriticalSection(&csVirtual);
200 view = VIRTUAL_FirstView;
203 VIRTUAL_DumpView( view );
206 RtlLeaveCriticalSection(&csVirtual);
210 /***********************************************************************
213 * Find the view containing a given address.
219 static FILE_VIEW *VIRTUAL_FindView( const void *addr ) /* [in] Address */
223 RtlEnterCriticalSection(&csVirtual);
224 view = VIRTUAL_FirstView;
227 if (view->base > addr)
232 if ((char*)view->base + view->size > (char*)addr) break;
235 RtlLeaveCriticalSection(&csVirtual);
240 /***********************************************************************
247 static void VIRTUAL_DeleteView( FILE_VIEW *view ) /* [in] View */
249 RtlEnterCriticalSection(&csVirtual);
250 if (!(view->flags & VFLAG_SYSTEM))
251 munmap( (void *)view->base, view->size );
252 if (view->next) view->next->prev = view->prev;
253 if (view->prev) view->prev->next = view->next;
254 else VIRTUAL_FirstView = view->next;
255 RtlLeaveCriticalSection(&csVirtual);
256 if (view->mapping) NtClose( view->mapping );
261 /***********************************************************************
264 * Create a new view and add it in the linked list.
266 static FILE_VIEW *VIRTUAL_CreateView( void *base, UINT size, UINT flags,
267 BYTE vprot, HANDLE mapping )
269 FILE_VIEW *view, *prev;
271 /* Create the view structure */
273 assert( !((unsigned int)base & page_mask) );
274 assert( !(size & page_mask) );
276 if (!(view = (FILE_VIEW *)malloc( sizeof(*view) + size - 1 ))) return NULL;
278 view->size = size << page_shift;
280 view->mapping = mapping;
281 view->protect = vprot;
282 view->handlerProc = NULL;
283 memset( view->prot, vprot, size );
285 /* Duplicate the mapping handle */
288 NtDuplicateObject( GetCurrentProcess(), view->mapping,
289 GetCurrentProcess(), &view->mapping,
290 0, 0, DUPLICATE_SAME_ACCESS ))
296 /* Insert it in the linked list */
298 RtlEnterCriticalSection(&csVirtual);
299 if (!VIRTUAL_FirstView || (VIRTUAL_FirstView->base > base))
301 view->next = VIRTUAL_FirstView;
303 if (view->next) view->next->prev = view;
304 VIRTUAL_FirstView = view;
308 prev = VIRTUAL_FirstView;
309 while (prev->next && (prev->next->base < base)) prev = prev->next;
310 view->next = prev->next;
312 if (view->next) view->next->prev = view;
315 /* Check for overlapping views. This can happen if the previous view
316 * was a system view that got unmapped behind our back. In that case
317 * we recover by simply deleting it. */
318 if ((char *)prev->base + prev->size > (char *)base)
320 TRACE( "overlapping prev view %p-%p for %p-%p\n",
321 prev->base, (char *)prev->base + prev->size,
322 base, (char *)base + view->size );
323 assert( view->prev->flags & VFLAG_SYSTEM );
324 VIRTUAL_DeleteView( view->prev );
327 /* check for overlap with next too */
328 if (view->next && (char *)base + view->size > (char *)view->next->base)
330 TRACE( "overlapping next view %p-%p for %p-%p\n",
331 view->next->base, (char *)view->next->base + view->next->size,
332 base, (char *)base + view->size );
333 assert( view->next->flags & VFLAG_SYSTEM );
334 VIRTUAL_DeleteView( view->next );
336 RtlLeaveCriticalSection(&csVirtual);
337 VIRTUAL_DEBUG_DUMP_VIEW( view );
342 /***********************************************************************
343 * VIRTUAL_GetUnixProt
345 * Convert page protections to protection for mmap/mprotect.
347 static int VIRTUAL_GetUnixProt( BYTE vprot )
350 if ((vprot & VPROT_COMMITTED) && !(vprot & VPROT_GUARD))
352 if (vprot & VPROT_READ) prot |= PROT_READ;
353 if (vprot & VPROT_WRITE) prot |= PROT_WRITE;
354 if (vprot & VPROT_WRITECOPY) prot |= PROT_WRITE;
355 if (vprot & VPROT_EXEC) prot |= PROT_EXEC;
361 /***********************************************************************
362 * VIRTUAL_GetWin32Prot
364 * Convert page protections to Win32 flags.
369 static void VIRTUAL_GetWin32Prot(
370 BYTE vprot, /* [in] Page protection flags */
371 DWORD *protect, /* [out] Location to store Win32 protection flags */
372 DWORD *state ) /* [out] Location to store mem state flag */
375 *protect = VIRTUAL_Win32Flags[vprot & 0x0f];
376 /* if (vprot & VPROT_GUARD) *protect |= PAGE_GUARD;*/
377 if (vprot & VPROT_NOCACHE) *protect |= PAGE_NOCACHE;
379 if (vprot & VPROT_GUARD) *protect = PAGE_NOACCESS;
382 if (state) *state = (vprot & VPROT_COMMITTED) ? MEM_COMMIT : MEM_RESERVE;
386 /***********************************************************************
389 * Build page protections from Win32 flags.
392 * Value of page protection flags
394 static BYTE VIRTUAL_GetProt( DWORD protect ) /* [in] Win32 protection flags */
398 switch(protect & 0xff)
404 vprot = VPROT_READ | VPROT_WRITE;
407 /* MSDN CreateFileMapping() states that if PAGE_WRITECOPY is given,
408 * that the hFile must have been opened with GENERIC_READ and
409 * GENERIC_WRITE access. This is WRONG as tests show that you
410 * only need GENERIC_READ access (at least for Win9x,
411 * FIXME: what about NT?). Thus, we don't put VPROT_WRITE in
412 * PAGE_WRITECOPY and PAGE_EXECUTE_WRITECOPY.
414 vprot = VPROT_READ | VPROT_WRITECOPY;
419 case PAGE_EXECUTE_READ:
420 vprot = VPROT_EXEC | VPROT_READ;
422 case PAGE_EXECUTE_READWRITE:
423 vprot = VPROT_EXEC | VPROT_READ | VPROT_WRITE;
425 case PAGE_EXECUTE_WRITECOPY:
426 /* See comment for PAGE_WRITECOPY above */
427 vprot = VPROT_EXEC | VPROT_READ | VPROT_WRITECOPY;
434 if (protect & PAGE_GUARD) vprot |= VPROT_GUARD;
435 if (protect & PAGE_NOCACHE) vprot |= VPROT_NOCACHE;
440 /***********************************************************************
443 * Change the protection of a range of pages.
449 static BOOL VIRTUAL_SetProt( FILE_VIEW *view, /* [in] Pointer to view */
450 void *base, /* [in] Starting address */
451 UINT size, /* [in] Size in bytes */
452 BYTE vprot ) /* [in] Protections to use */
455 base, (char *)base + size - 1, VIRTUAL_GetProtStr( vprot ) );
457 if (mprotect( base, size, VIRTUAL_GetUnixProt(vprot) ))
458 return FALSE; /* FIXME: last error */
460 memset( view->prot + (((char *)base - (char *)view->base) >> page_shift),
461 vprot, size >> page_shift );
462 VIRTUAL_DEBUG_DUMP_VIEW( view );
467 /***********************************************************************
470 * Create an anonymous mapping aligned to the allocation granularity.
472 static NTSTATUS anon_mmap_aligned( void **addr, unsigned int size, int prot, int flags )
474 void *ptr, *base = *addr;
475 unsigned int view_size = size + (base ? 0 : granularity_mask + 1);
477 if ((ptr = wine_anon_mmap( base, view_size, prot, flags )) == (void *)-1)
479 if (errno == ENOMEM) return STATUS_NO_MEMORY;
480 return STATUS_INVALID_PARAMETER;
485 /* Release the extra memory while keeping the range
486 * starting on the granularity boundary. */
487 if ((unsigned int)ptr & granularity_mask)
489 unsigned int extra = granularity_mask + 1 - ((unsigned int)ptr & granularity_mask);
490 munmap( ptr, extra );
491 ptr = (char *)ptr + extra;
494 if (view_size > size)
495 munmap( (char *)ptr + size, view_size - size );
497 else if (ptr != base)
499 /* We couldn't get the address we wanted */
500 munmap( ptr, view_size );
501 return STATUS_CONFLICTING_ADDRESSES;
504 return STATUS_SUCCESS;
508 /***********************************************************************
511 * Apply the relocations to a mapped PE image
513 static int do_relocations( char *base, const IMAGE_DATA_DIRECTORY *dir,
514 int delta, DWORD total_size )
516 IMAGE_BASE_RELOCATION *rel;
518 TRACE_(module)( "relocating from %p-%p to %p-%p\n",
519 base - delta, base - delta + total_size, base, base + total_size );
521 for (rel = (IMAGE_BASE_RELOCATION *)(base + dir->VirtualAddress);
522 ((char *)rel < base + dir->VirtualAddress + dir->Size) && rel->SizeOfBlock;
523 rel = (IMAGE_BASE_RELOCATION*)((char*)rel + rel->SizeOfBlock) )
525 char *page = base + rel->VirtualAddress;
526 WORD *TypeOffset = (WORD *)(rel + 1);
527 int i, count = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(*TypeOffset);
529 if (!count) continue;
532 if ((char *)rel + rel->SizeOfBlock > base + dir->VirtualAddress + dir->Size ||
533 page > base + total_size)
535 ERR_(module)("invalid relocation %p,%lx,%ld at %p,%lx,%lx\n",
536 rel, rel->VirtualAddress, rel->SizeOfBlock,
537 base, dir->VirtualAddress, dir->Size );
541 TRACE_(module)("%ld relocations for page %lx\n", rel->SizeOfBlock, rel->VirtualAddress);
543 /* patching in reverse order */
544 for (i = 0 ; i < count; i++)
546 int offset = TypeOffset[i] & 0xFFF;
547 int type = TypeOffset[i] >> 12;
550 case IMAGE_REL_BASED_ABSOLUTE:
552 case IMAGE_REL_BASED_HIGH:
553 *(short*)(page+offset) += HIWORD(delta);
555 case IMAGE_REL_BASED_LOW:
556 *(short*)(page+offset) += LOWORD(delta);
558 case IMAGE_REL_BASED_HIGHLOW:
559 *(int*)(page+offset) += delta;
560 /* FIXME: if this is an exported address, fire up enhanced logic */
563 FIXME_(module)("Unknown/unsupported fixup type %d.\n", type);
572 /***********************************************************************
575 * Map an executable (PE format) image into memory.
577 static NTSTATUS map_image( HANDLE hmapping, int fd, char *base, DWORD total_size,
578 DWORD header_size, int shared_fd, BOOL removable, PVOID *addr_ptr )
580 IMAGE_DOS_HEADER *dos;
581 IMAGE_NT_HEADERS *nt;
582 IMAGE_SECTION_HEADER *sec;
583 IMAGE_DATA_DIRECTORY *imports;
584 NTSTATUS status = STATUS_INVALID_IMAGE_FORMAT; /* generic error (FIXME) */
589 /* zero-map the whole range */
591 if (base < (char *)0x110000 || /* make sure the DOS area remains free */
592 (ptr = wine_anon_mmap( base, total_size,
593 PROT_READ | PROT_WRITE | PROT_EXEC, 0 )) == (char *)-1)
595 ptr = wine_anon_mmap( NULL, total_size,
596 PROT_READ | PROT_WRITE | PROT_EXEC, 0 );
597 if (ptr == (char *)-1)
599 ERR_(module)("Not enough memory for module (%ld bytes)\n", total_size);
603 TRACE_(module)( "mapped PE file at %p-%p\n", ptr, ptr + total_size );
607 if (VIRTUAL_mmap( fd, ptr, header_size, 0, 0, PROT_READ,
608 MAP_PRIVATE | MAP_FIXED, &removable ) == (char *)-1) goto error;
609 dos = (IMAGE_DOS_HEADER *)ptr;
610 nt = (IMAGE_NT_HEADERS *)(ptr + dos->e_lfanew);
611 if ((char *)(nt + 1) > ptr + header_size) goto error;
613 sec = (IMAGE_SECTION_HEADER*)((char*)&nt->OptionalHeader+nt->FileHeader.SizeOfOptionalHeader);
614 if ((char *)(sec + nt->FileHeader.NumberOfSections) > ptr + header_size) goto error;
616 imports = nt->OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_IMPORT;
617 if (!imports->Size || !imports->VirtualAddress) imports = NULL;
619 /* check the architecture */
621 if (nt->FileHeader.Machine != IMAGE_FILE_MACHINE_I386)
623 MESSAGE("Trying to load PE image for unsupported architecture (");
624 switch (nt->FileHeader.Machine)
626 case IMAGE_FILE_MACHINE_UNKNOWN: MESSAGE("Unknown"); break;
627 case IMAGE_FILE_MACHINE_I860: MESSAGE("I860"); break;
628 case IMAGE_FILE_MACHINE_R3000: MESSAGE("R3000"); break;
629 case IMAGE_FILE_MACHINE_R4000: MESSAGE("R4000"); break;
630 case IMAGE_FILE_MACHINE_R10000: MESSAGE("R10000"); break;
631 case IMAGE_FILE_MACHINE_ALPHA: MESSAGE("Alpha"); break;
632 case IMAGE_FILE_MACHINE_POWERPC: MESSAGE("PowerPC"); break;
633 default: MESSAGE("Unknown-%04x", nt->FileHeader.Machine); break;
639 /* map all the sections */
641 for (i = pos = 0; i < nt->FileHeader.NumberOfSections; i++, sec++)
645 /* a few sanity checks */
646 size = sec->VirtualAddress + ROUND_SIZE( sec->VirtualAddress, sec->Misc.VirtualSize );
647 if (sec->VirtualAddress > total_size || size > total_size || size < sec->VirtualAddress)
649 ERR_(module)( "Section %.8s too large (%lx+%lx/%lx)\n",
650 sec->Name, sec->VirtualAddress, sec->Misc.VirtualSize, total_size );
654 if ((sec->Characteristics & IMAGE_SCN_MEM_SHARED) &&
655 (sec->Characteristics & IMAGE_SCN_MEM_WRITE))
657 size = ROUND_SIZE( 0, sec->Misc.VirtualSize );
658 TRACE_(module)( "mapping shared section %.8s at %p off %lx (%x) size %lx (%lx) flags %lx\n",
659 sec->Name, ptr + sec->VirtualAddress,
660 sec->PointerToRawData, pos, sec->SizeOfRawData,
661 size, sec->Characteristics );
662 if (VIRTUAL_mmap( shared_fd, ptr + sec->VirtualAddress, size,
663 pos, 0, PROT_READ|PROT_WRITE|PROT_EXEC,
664 MAP_SHARED|MAP_FIXED, NULL ) == (void *)-1)
666 ERR_(module)( "Could not map shared section %.8s\n", sec->Name );
670 /* check if the import directory falls inside this section */
671 if (imports && imports->VirtualAddress >= sec->VirtualAddress &&
672 imports->VirtualAddress < sec->VirtualAddress + size)
674 UINT_PTR base = imports->VirtualAddress & ~page_mask;
675 UINT_PTR end = base + ROUND_SIZE( imports->VirtualAddress, imports->Size );
676 if (end > sec->VirtualAddress + size) end = sec->VirtualAddress + size;
677 if (end > base) VIRTUAL_mmap( shared_fd, ptr + base, end - base,
678 pos + (base - sec->VirtualAddress), 0,
679 PROT_READ|PROT_WRITE|PROT_EXEC,
680 MAP_PRIVATE|MAP_FIXED, NULL );
686 TRACE_(module)( "mapping section %.8s at %p off %lx size %lx flags %lx\n",
687 sec->Name, ptr + sec->VirtualAddress,
688 sec->PointerToRawData, sec->SizeOfRawData,
689 sec->Characteristics );
691 if ((sec->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA) &&
692 !(sec->Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)) continue;
693 if (!sec->PointerToRawData || !sec->SizeOfRawData) continue;
695 /* Note: if the section is not aligned properly VIRTUAL_mmap will magically
696 * fall back to read(), so we don't need to check anything here.
698 if (VIRTUAL_mmap( fd, ptr + sec->VirtualAddress, sec->SizeOfRawData,
699 sec->PointerToRawData, 0, PROT_READ|PROT_WRITE|PROT_EXEC,
700 MAP_PRIVATE | MAP_FIXED, &removable ) == (void *)-1)
702 ERR_(module)( "Could not map section %.8s, file probably truncated\n", sec->Name );
706 if ((sec->SizeOfRawData < sec->Misc.VirtualSize) && (sec->SizeOfRawData & page_mask))
708 DWORD end = ROUND_SIZE( 0, sec->SizeOfRawData );
709 if (end > sec->Misc.VirtualSize) end = sec->Misc.VirtualSize;
710 TRACE_(module)("clearing %p - %p\n",
711 ptr + sec->VirtualAddress + sec->SizeOfRawData,
712 ptr + sec->VirtualAddress + end );
713 memset( ptr + sec->VirtualAddress + sec->SizeOfRawData, 0,
714 end - sec->SizeOfRawData );
719 /* perform base relocation, if necessary */
723 const IMAGE_DATA_DIRECTORY *relocs;
725 relocs = &nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC];
726 if (!relocs->VirtualAddress || !relocs->Size)
728 if (nt->OptionalHeader.ImageBase == 0x400000)
729 ERR("Standard load address for a Win32 program (0x00400000) not available - security-patched kernel ?\n");
731 ERR( "FATAL: Need to relocate module from addr %lx, but there are no relocation records\n",
732 nt->OptionalHeader.ImageBase );
736 /* FIXME: If we need to relocate a system DLL (base > 2GB) we should
737 * really make sure that the *new* base address is also > 2GB.
738 * Some DLLs really check the MSB of the module handle :-/
740 if ((nt->OptionalHeader.ImageBase & 0x80000000) && !((DWORD)base & 0x80000000))
741 ERR( "Forced to relocate system DLL (base > 2GB). This is not good.\n" );
743 if (!do_relocations( ptr, relocs, ptr - base, total_size ))
749 if (removable) hmapping = 0; /* don't keep handle open on removable media */
750 if (!(view = VIRTUAL_CreateView( ptr, total_size, 0,
751 VPROT_COMMITTED | VPROT_READ | VPROT_WRITE |
752 VPROT_EXEC | VPROT_WRITECOPY | VPROT_IMAGE, hmapping )))
754 status = STATUS_NO_MEMORY;
758 /* set the image protections */
760 VIRTUAL_SetProt( view, ptr, header_size, VPROT_COMMITTED | VPROT_READ );
761 sec = (IMAGE_SECTION_HEADER*)((char *)&nt->OptionalHeader+nt->FileHeader.SizeOfOptionalHeader);
762 for (i = 0; i < nt->FileHeader.NumberOfSections; i++, sec++)
764 DWORD size = ROUND_SIZE( sec->VirtualAddress, sec->Misc.VirtualSize );
765 BYTE vprot = VPROT_COMMITTED;
766 if (sec->Characteristics & IMAGE_SCN_MEM_READ) vprot |= VPROT_READ;
767 if (sec->Characteristics & IMAGE_SCN_MEM_WRITE) vprot |= VPROT_WRITE|VPROT_WRITECOPY;
768 if (sec->Characteristics & IMAGE_SCN_MEM_EXECUTE) vprot |= VPROT_EXEC;
770 /* make sure the import directory is writable */
771 if (imports && imports->VirtualAddress >= sec->VirtualAddress &&
772 imports->VirtualAddress < sec->VirtualAddress + size)
773 vprot |= VPROT_READ|VPROT_WRITE|VPROT_WRITECOPY;
775 VIRTUAL_SetProt( view, ptr + sec->VirtualAddress, size, vprot );
779 return STATUS_SUCCESS;
782 if (ptr != (char *)-1) munmap( ptr, total_size );
787 /***********************************************************************
790 * Check whether a process handle is for the current process.
792 static BOOL is_current_process( HANDLE handle )
796 if (handle == GetCurrentProcess()) return TRUE;
797 SERVER_START_REQ( get_process_info )
799 req->handle = handle;
800 if (!wine_server_call( req ))
801 ret = ((DWORD)reply->pid == GetCurrentProcessId());
808 /***********************************************************************
811 void virtual_init(void)
814 page_size = getpagesize();
815 page_mask = page_size - 1;
816 /* Make sure we have a power of 2 */
817 assert( !(page_size & page_mask) );
819 while ((1 << page_shift) != page_size) page_shift++;
820 #endif /* page_mask */
824 /***********************************************************************
825 * VIRTUAL_SetFaultHandler
827 BOOL VIRTUAL_SetFaultHandler( LPCVOID addr, HANDLERPROC proc, LPVOID arg )
831 if (!(view = VIRTUAL_FindView( addr ))) return FALSE;
832 view->handlerProc = proc;
833 view->handlerArg = arg;
837 /***********************************************************************
838 * VIRTUAL_HandleFault
840 DWORD VIRTUAL_HandleFault( LPCVOID addr )
842 FILE_VIEW *view = VIRTUAL_FindView( addr );
843 DWORD ret = EXCEPTION_ACCESS_VIOLATION;
847 if (view->handlerProc)
849 if (view->handlerProc(view->handlerArg, addr)) ret = 0; /* handled */
853 BYTE vprot = view->prot[((char *)addr - (char *)view->base) >> page_shift];
854 void *page = (void *)((UINT_PTR)addr & ~page_mask);
855 char *stack = NtCurrentTeb()->Tib.StackLimit;
856 if (vprot & VPROT_GUARD)
858 VIRTUAL_SetProt( view, page, page_mask + 1, vprot & ~VPROT_GUARD );
859 ret = STATUS_GUARD_PAGE_VIOLATION;
861 /* is it inside the stack guard page? */
862 if (((char *)addr >= stack) && ((char *)addr < stack + (page_mask+1)))
863 ret = STATUS_STACK_OVERFLOW;
871 /***********************************************************************
874 * Linux kernels before 2.4.x can support non page-aligned offsets, as
875 * long as the offset is aligned to the filesystem block size. This is
876 * a big performance gain so we want to take advantage of it.
878 * However, when we use 64-bit file support this doesn't work because
879 * glibc rejects unaligned offsets. Also glibc 2.1.3 mmap64 is broken
880 * in that it rounds unaligned offsets down to a page boundary. For
881 * these reasons we do a direct system call here.
883 static void *unaligned_mmap( void *addr, size_t length, unsigned int prot,
884 unsigned int flags, int fd, unsigned int offset_low,
885 unsigned int offset_high )
887 #if defined(linux) && defined(__i386__) && defined(__GNUC__)
888 if (!offset_high && (offset_low & page_mask))
903 args.length = length;
907 args.offset = offset_low;
909 __asm__ __volatile__("push %%ebx\n\t"
914 : "0" (90), /* SYS_mmap */
916 if (ret < 0 && ret > -4096)
924 return mmap( addr, length, prot, flags, fd, ((off_t)offset_high << 32) | offset_low );
928 /***********************************************************************
931 * Wrapper for mmap() that handles anonymous mappings portably,
932 * and falls back to read if mmap of a file fails.
934 static LPVOID VIRTUAL_mmap( int fd, LPVOID start, DWORD size,
935 DWORD offset_low, DWORD offset_high,
936 int prot, int flags, BOOL *removable )
940 BOOL is_shared_write = FALSE;
942 if (fd == -1) return wine_anon_mmap( start, size, prot, flags );
944 if (prot & PROT_WRITE)
947 if (flags & MAP_SHARED) is_shared_write = TRUE;
950 if (!(flags & MAP_PRIVATE)) is_shared_write = TRUE;
954 if (removable && *removable)
956 /* if on removable media, try using read instead of mmap */
957 if (!is_shared_write) goto fake_mmap;
961 if ((ret = unaligned_mmap( start, size, prot, flags, fd,
962 offset_low, offset_high )) != (LPVOID)-1) return ret;
964 /* mmap() failed; if this is because the file offset is not */
965 /* page-aligned (EINVAL), or because the underlying filesystem */
966 /* does not support mmap() (ENOEXEC,ENODEV), we do it by hand. */
968 if ((errno != ENOEXEC) && (errno != EINVAL) && (errno != ENODEV)) return ret;
969 if (is_shared_write) return ret; /* we cannot fake shared write mappings */
972 /* Reserve the memory with an anonymous mmap */
973 ret = wine_anon_mmap( start, size, PROT_READ | PROT_WRITE, flags );
974 if (ret == (LPVOID)-1) return ret;
975 /* Now read in the file */
976 offset = ((off_t)offset_high << 32) | offset_low;
977 pread( fd, ret, size, offset );
978 mprotect( ret, size, prot ); /* Set the right protection */
983 /***********************************************************************
984 * NtAllocateVirtualMemory (NTDLL.@)
985 * ZwAllocateVirtualMemory (NTDLL.@)
987 NTSTATUS WINAPI NtAllocateVirtualMemory( HANDLE process, PVOID *ret, PVOID addr,
988 ULONG *size_ptr, ULONG type, ULONG protect )
993 DWORD size = *size_ptr;
995 if (!is_current_process( process ))
997 ERR("Unsupported on other process\n");
998 return STATUS_ACCESS_DENIED;
1001 TRACE("%p %08lx %lx %08lx\n", addr, size, type, protect );
1003 /* Round parameters to a page boundary */
1005 if (size > 0x7fc00000) return STATUS_WORKING_SET_LIMIT_RANGE; /* 2Gb - 4Mb */
1009 if (type & MEM_RESERVE) /* Round down to 64k boundary */
1010 base = ROUND_ADDR( addr, granularity_mask );
1012 base = ROUND_ADDR( addr, page_mask );
1013 size = (((UINT_PTR)addr + size + page_mask) & ~page_mask) - (UINT_PTR)base;
1015 /* disallow low 64k, wrap-around and kernel space */
1016 if (((char *)base <= (char *)granularity_mask) ||
1017 ((char *)base + size < (char *)base) ||
1018 (ADDRESS_SPACE_LIMIT && ((char *)base + size > (char *)ADDRESS_SPACE_LIMIT)))
1019 return STATUS_INVALID_PARAMETER;
1024 size = (size + page_mask) & ~page_mask;
1027 if (type & MEM_TOP_DOWN) {
1028 /* FIXME: MEM_TOP_DOWN allocates the largest possible address.
1029 * Is there _ANY_ way to do it with UNIX mmap()?
1031 WARN("MEM_TOP_DOWN ignored\n");
1032 type &= ~MEM_TOP_DOWN;
1035 /* Compute the alloc type flags */
1037 if (type & MEM_SYSTEM)
1039 vprot = VIRTUAL_GetProt( protect ) | VPROT_COMMITTED;
1040 if (type & MEM_IMAGE) vprot |= VPROT_IMAGE;
1044 if (!(type & (MEM_COMMIT | MEM_RESERVE)) || (type & ~(MEM_COMMIT | MEM_RESERVE)))
1046 WARN("called with wrong alloc type flags (%08lx) !\n", type);
1047 return STATUS_INVALID_PARAMETER;
1049 if (type & MEM_COMMIT)
1050 vprot = VIRTUAL_GetProt( protect ) | VPROT_COMMITTED;
1055 /* Reserve the memory */
1057 if (type & MEM_SYSTEM)
1059 if (!(view = VIRTUAL_CreateView( base, size, VFLAG_VALLOC | VFLAG_SYSTEM, vprot, 0 )))
1060 return STATUS_NO_MEMORY;
1062 else if ((type & MEM_RESERVE) || !base)
1064 NTSTATUS res = anon_mmap_aligned( &base, size, VIRTUAL_GetUnixProt( vprot ), 0 );
1065 if (res) return res;
1067 if (!(view = VIRTUAL_CreateView( base, size, VFLAG_VALLOC, vprot, 0 )))
1069 munmap( base, size );
1070 return STATUS_NO_MEMORY;
1075 /* Commit the pages */
1077 if (!(view = VIRTUAL_FindView( base )) ||
1078 ((char *)base + size > (char *)view->base + view->size)) return STATUS_NOT_MAPPED_VIEW;
1080 if (!VIRTUAL_SetProt( view, base, size, vprot )) return STATUS_ACCESS_DENIED;
1085 return STATUS_SUCCESS;
1089 /***********************************************************************
1090 * NtFreeVirtualMemory (NTDLL.@)
1091 * ZwFreeVirtualMemory (NTDLL.@)
1093 NTSTATUS WINAPI NtFreeVirtualMemory( HANDLE process, PVOID *addr_ptr, ULONG *size_ptr, ULONG type )
1097 LPVOID addr = *addr_ptr;
1098 DWORD size = *size_ptr;
1100 if (!is_current_process( process ))
1102 ERR("Unsupported on other process\n");
1103 return STATUS_ACCESS_DENIED;
1106 TRACE("%p %08lx %lx\n", addr, size, type );
1108 /* Fix the parameters */
1110 size = ROUND_SIZE( addr, size );
1111 base = ROUND_ADDR( addr, page_mask );
1113 if (!(view = VIRTUAL_FindView( base )) ||
1114 (base + size > (char *)view->base + view->size) ||
1115 !(view->flags & VFLAG_VALLOC))
1116 return STATUS_INVALID_PARAMETER;
1118 /* Check the type */
1120 if (type & MEM_SYSTEM)
1122 /* return the values that the caller should use to unmap the area */
1123 *addr_ptr = view->base;
1124 *size_ptr = view->size;
1125 view->flags |= VFLAG_SYSTEM;
1126 VIRTUAL_DeleteView( view );
1127 return STATUS_SUCCESS;
1130 if ((type != MEM_DECOMMIT) && (type != MEM_RELEASE))
1132 ERR("called with wrong free type flags (%08lx) !\n", type);
1133 return STATUS_INVALID_PARAMETER;
1136 /* Free the pages */
1138 if (type == MEM_RELEASE)
1140 if (size || (base != view->base)) return STATUS_INVALID_PARAMETER;
1141 VIRTUAL_DeleteView( view );
1145 /* Decommit the pages by remapping zero-pages instead */
1147 if (wine_anon_mmap( (LPVOID)base, size, VIRTUAL_GetUnixProt(0), MAP_FIXED ) != (LPVOID)base)
1148 ERR( "Could not remap pages, expect trouble\n" );
1149 if (!VIRTUAL_SetProt( view, base, size, 0 )) return STATUS_ACCESS_DENIED; /* FIXME */
1154 return STATUS_SUCCESS;
1158 /***********************************************************************
1159 * NtProtectVirtualMemory (NTDLL.@)
1160 * ZwProtectVirtualMemory (NTDLL.@)
1162 NTSTATUS WINAPI NtProtectVirtualMemory( HANDLE process, PVOID *addr_ptr, ULONG *size_ptr,
1163 ULONG new_prot, ULONG *old_prot )
1169 DWORD prot, size = *size_ptr;
1170 LPVOID addr = *addr_ptr;
1172 if (!is_current_process( process ))
1174 ERR("Unsupported on other process\n");
1175 return STATUS_ACCESS_DENIED;
1178 TRACE("%p %08lx %08lx\n", addr, size, new_prot );
1180 /* Fix the parameters */
1182 size = ROUND_SIZE( addr, size );
1183 base = ROUND_ADDR( addr, page_mask );
1185 if (!(view = VIRTUAL_FindView( base )) ||
1186 (base + size > (char *)view->base + view->size))
1187 return STATUS_INVALID_PARAMETER;
1189 /* Make sure all the pages are committed */
1191 p = view->prot + ((base - (char *)view->base) >> page_shift);
1192 VIRTUAL_GetWin32Prot( *p, &prot, NULL );
1193 for (i = size >> page_shift; i; i--, p++)
1195 if (!(*p & VPROT_COMMITTED)) return STATUS_INVALID_PARAMETER;
1198 if (old_prot) *old_prot = prot;
1199 vprot = VIRTUAL_GetProt( new_prot ) | VPROT_COMMITTED;
1200 if (!VIRTUAL_SetProt( view, base, size, vprot )) return STATUS_ACCESS_DENIED;
1204 return STATUS_SUCCESS;
1208 /***********************************************************************
1209 * NtQueryVirtualMemory (NTDLL.@)
1210 * ZwQueryVirtualMemory (NTDLL.@)
1212 NTSTATUS WINAPI NtQueryVirtualMemory( HANDLE process, LPCVOID addr,
1213 MEMORY_INFORMATION_CLASS info_class, PVOID buffer,
1214 ULONG len, ULONG *res_len )
1217 char *base, *alloc_base = 0;
1219 MEMORY_BASIC_INFORMATION *info = buffer;
1221 if (info_class != MemoryBasicInformation) return STATUS_INVALID_INFO_CLASS;
1222 if (ADDRESS_SPACE_LIMIT && addr >= ADDRESS_SPACE_LIMIT)
1223 return STATUS_WORKING_SET_LIMIT_RANGE; /* FIXME */
1225 if (!is_current_process( process ))
1227 ERR("Unsupported on other process\n");
1228 return STATUS_ACCESS_DENIED;
1231 base = ROUND_ADDR( addr, page_mask );
1233 /* Find the view containing the address */
1235 RtlEnterCriticalSection(&csVirtual);
1236 view = VIRTUAL_FirstView;
1241 size = (char *)ADDRESS_SPACE_LIMIT - alloc_base;
1244 if ((char *)view->base > base)
1246 size = (char *)view->base - alloc_base;
1250 if ((char *)view->base + view->size > base)
1252 alloc_base = view->base;
1256 alloc_base = (char *)view->base + view->size;
1259 RtlLeaveCriticalSection(&csVirtual);
1261 /* Fill the info structure */
1265 info->State = MEM_FREE;
1267 info->AllocationProtect = 0;
1272 BYTE vprot = view->prot[(base - alloc_base) >> page_shift];
1273 VIRTUAL_GetWin32Prot( vprot, &info->Protect, &info->State );
1274 for (size = base - alloc_base; size < view->size; size += page_mask+1)
1275 if (view->prot[size >> page_shift] != vprot) break;
1276 VIRTUAL_GetWin32Prot( view->protect, &info->AllocationProtect, NULL );
1277 if (view->protect & VPROT_IMAGE) info->Type = MEM_IMAGE;
1278 else if (view->flags & VFLAG_VALLOC) info->Type = MEM_PRIVATE;
1279 else info->Type = MEM_MAPPED;
1282 info->BaseAddress = (LPVOID)base;
1283 info->AllocationBase = (LPVOID)alloc_base;
1284 info->RegionSize = size - (base - alloc_base);
1285 if (res_len) *res_len = sizeof(*info);
1286 return STATUS_SUCCESS;
1290 /***********************************************************************
1291 * NtLockVirtualMemory (NTDLL.@)
1292 * ZwLockVirtualMemory (NTDLL.@)
1294 NTSTATUS WINAPI NtLockVirtualMemory( HANDLE process, PVOID *addr, ULONG *size, ULONG unknown )
1296 if (!is_current_process( process ))
1298 ERR("Unsupported on other process\n");
1299 return STATUS_ACCESS_DENIED;
1301 return STATUS_SUCCESS;
1305 /***********************************************************************
1306 * NtUnlockVirtualMemory (NTDLL.@)
1307 * ZwUnlockVirtualMemory (NTDLL.@)
1309 NTSTATUS WINAPI NtUnlockVirtualMemory( HANDLE process, PVOID *addr, ULONG *size, ULONG unknown )
1311 if (!is_current_process( process ))
1313 ERR("Unsupported on other process\n");
1314 return STATUS_ACCESS_DENIED;
1316 return STATUS_SUCCESS;
1320 /***********************************************************************
1321 * NtCreateSection (NTDLL.@)
1322 * ZwCreateSection (NTDLL.@)
1324 NTSTATUS WINAPI NtCreateSection( HANDLE *handle, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr,
1325 const LARGE_INTEGER *size, ULONG protect,
1326 ULONG sec_flags, HANDLE file )
1330 DWORD len = attr->ObjectName ? attr->ObjectName->Length : 0;
1332 /* Check parameters */
1334 if (len > MAX_PATH*sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
1336 vprot = VIRTUAL_GetProt( protect );
1337 if (sec_flags & SEC_RESERVE)
1339 if (file) return STATUS_INVALID_PARAMETER;
1341 else vprot |= VPROT_COMMITTED;
1342 if (sec_flags & SEC_NOCACHE) vprot |= VPROT_NOCACHE;
1343 if (sec_flags & SEC_IMAGE) vprot |= VPROT_IMAGE;
1345 /* Create the server object */
1347 SERVER_START_REQ( create_mapping )
1349 req->file_handle = file;
1350 req->size_high = size ? size->s.HighPart : 0;
1351 req->size_low = size ? size->s.LowPart : 0;
1352 req->protect = vprot;
1353 req->access = access;
1354 req->inherit = (attr->Attributes & OBJ_INHERIT) != 0;
1355 if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
1356 ret = wine_server_call( req );
1357 *handle = reply->handle;
1364 /***********************************************************************
1365 * NtOpenSection (NTDLL.@)
1366 * ZwOpenSection (NTDLL.@)
1368 NTSTATUS WINAPI NtOpenSection( HANDLE *handle, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr )
1371 DWORD len = attr->ObjectName->Length;
1373 if (len > MAX_PATH*sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
1375 SERVER_START_REQ( open_mapping )
1377 req->access = access;
1378 req->inherit = (attr->Attributes & OBJ_INHERIT) != 0;
1379 wine_server_add_data( req, attr->ObjectName->Buffer, len );
1380 if (!(ret = wine_server_call( req ))) *handle = reply->handle;
1387 /***********************************************************************
1388 * NtMapViewOfSection (NTDLL.@)
1389 * ZwMapViewOfSection (NTDLL.@)
1391 NTSTATUS WINAPI NtMapViewOfSection( HANDLE handle, HANDLE process, PVOID *addr_ptr, ULONG zero_bits,
1392 ULONG commit_size, const LARGE_INTEGER *offset, ULONG *size_ptr,
1393 SECTION_INHERIT inherit, ULONG alloc_type, ULONG protect )
1398 int flags = MAP_PRIVATE;
1399 int unix_handle = -1;
1401 void *base, *ptr = (void *)-1, *ret;
1402 DWORD size_low, size_high, header_size, shared_size;
1406 if (!is_current_process( process ))
1408 ERR("Unsupported on other process\n");
1409 return STATUS_ACCESS_DENIED;
1412 TRACE("handle=%p addr=%p off=%lx%08lx size=%x access=%lx\n",
1413 handle, *addr_ptr, offset->s.HighPart, offset->s.LowPart, size, protect );
1415 /* Check parameters */
1417 if ((offset->s.LowPart & granularity_mask) ||
1418 (*addr_ptr && ((UINT_PTR)*addr_ptr & granularity_mask)))
1419 return STATUS_INVALID_PARAMETER;
1421 SERVER_START_REQ( get_mapping_info )
1423 req->handle = handle;
1424 res = wine_server_call( req );
1425 prot = reply->protect;
1427 size_low = reply->size_low;
1428 size_high = reply->size_high;
1429 header_size = reply->header_size;
1430 shared_file = reply->shared_file;
1431 shared_size = reply->shared_size;
1432 removable = reply->removable;
1435 if (res) return res;
1437 if ((res = wine_server_handle_to_fd( handle, 0, &unix_handle, NULL, NULL ))) return res;
1439 if (prot & VPROT_IMAGE)
1445 if ((res = wine_server_handle_to_fd( shared_file, GENERIC_READ, &shared_fd,
1446 NULL, NULL ))) goto error;
1447 res = map_image( handle, unix_handle, base, size_low, header_size,
1448 shared_fd, removable, addr_ptr );
1449 wine_server_release_fd( shared_file, shared_fd );
1450 NtClose( shared_file );
1454 res = map_image( handle, unix_handle, base, size_low, header_size,
1455 -1, removable, addr_ptr );
1457 wine_server_release_fd( handle, unix_handle );
1458 if (!res) *size_ptr = size_low;
1463 ERR("Sizes larger than 4Gb not supported\n");
1465 if ((offset->s.LowPart >= size_low) ||
1466 (*size_ptr > size_low - offset->s.LowPart))
1468 res = STATUS_INVALID_PARAMETER;
1471 if (*size_ptr) size = ROUND_SIZE( offset->s.LowPart, *size_ptr );
1472 else size = size_low - offset->s.LowPart;
1478 case PAGE_READWRITE:
1479 case PAGE_EXECUTE_READWRITE:
1480 if (!(prot & VPROT_WRITE))
1482 res = STATUS_INVALID_PARAMETER;
1488 case PAGE_WRITECOPY:
1490 case PAGE_EXECUTE_READ:
1491 case PAGE_EXECUTE_WRITECOPY:
1492 if (prot & VPROT_READ) break;
1495 res = STATUS_INVALID_PARAMETER;
1499 /* FIXME: If a mapping is created with SEC_RESERVE and a process,
1500 * which has a view of this mapping commits some pages, they will
1501 * appear commited in all other processes, which have the same
1502 * view created. Since we don`t support this yet, we create the
1503 * whole mapping commited.
1505 prot |= VPROT_COMMITTED;
1507 /* Reserve a properly aligned area */
1509 if ((res = anon_mmap_aligned( addr_ptr, size, PROT_NONE, 0 ))) goto error;
1514 TRACE("handle=%p size=%x offset=%lx\n", handle, size, offset->s.LowPart );
1516 ret = VIRTUAL_mmap( unix_handle, ptr, size, offset->s.LowPart, offset->s.HighPart,
1517 VIRTUAL_GetUnixProt( prot ), flags | MAP_FIXED, &removable );
1520 ERR( "VIRTUAL_mmap %p %x %lx%08lx failed\n",
1521 ptr, size, offset->s.HighPart, offset->s.LowPart );
1522 res = STATUS_NO_MEMORY; /* FIXME */
1525 if (removable) handle = 0; /* don't keep handle open on removable media */
1527 if (!(view = VIRTUAL_CreateView( ptr, size, 0, prot, handle )))
1529 res = STATUS_NO_MEMORY;
1532 wine_server_release_fd( handle, unix_handle );
1534 return STATUS_SUCCESS;
1537 wine_server_release_fd( handle, unix_handle );
1538 if (ptr != (void *)-1) munmap( ptr, size );
1543 /***********************************************************************
1544 * NtUnmapViewOfSection (NTDLL.@)
1545 * ZwUnmapViewOfSection (NTDLL.@)
1547 NTSTATUS WINAPI NtUnmapViewOfSection( HANDLE process, PVOID addr )
1550 void *base = ROUND_ADDR( addr, page_mask );
1552 if (!is_current_process( process ))
1554 ERR("Unsupported on other process\n");
1555 return STATUS_ACCESS_DENIED;
1557 if (!(view = VIRTUAL_FindView( base )) || (base != view->base)) return STATUS_INVALID_PARAMETER;
1558 VIRTUAL_DeleteView( view );
1559 return STATUS_SUCCESS;
1563 /***********************************************************************
1564 * NtFlushVirtualMemory (NTDLL.@)
1565 * ZwFlushVirtualMemory (NTDLL.@)
1567 NTSTATUS WINAPI NtFlushVirtualMemory( HANDLE process, LPCVOID *addr_ptr,
1568 ULONG *size_ptr, ULONG unknown )
1571 void *addr = ROUND_ADDR( *addr_ptr, page_mask );
1573 if (!is_current_process( process ))
1575 ERR("Unsupported on other process\n");
1576 return STATUS_ACCESS_DENIED;
1578 if (!(view = VIRTUAL_FindView( addr ))) return STATUS_INVALID_PARAMETER;
1579 if (!*size_ptr) *size_ptr = view->size;
1581 if (!msync( addr, *size_ptr, MS_SYNC )) return STATUS_SUCCESS;
1582 return STATUS_NOT_MAPPED_DATA;
1586 /***********************************************************************
1587 * NtReadVirtualMemory (NTDLL.@)
1588 * ZwReadVirtualMemory (NTDLL.@)
1590 NTSTATUS WINAPI NtReadVirtualMemory( HANDLE process, const void *addr, void *buffer,
1591 SIZE_T size, SIZE_T *bytes_read )
1595 SERVER_START_REQ( read_process_memory )
1597 req->handle = process;
1598 req->addr = (void *)addr;
1599 wine_server_set_reply( req, buffer, size );
1600 if ((status = wine_server_call( req ))) size = 0;
1603 if (bytes_read) *bytes_read = size;
1608 /***********************************************************************
1609 * NtWriteVirtualMemory (NTDLL.@)
1610 * ZwWriteVirtualMemory (NTDLL.@)
1612 NTSTATUS WINAPI NtWriteVirtualMemory( HANDLE process, void *addr, const void *buffer,
1613 SIZE_T size, SIZE_T *bytes_written )
1615 static const unsigned int zero;
1616 unsigned int first_offset, last_offset, first_mask, last_mask;
1619 if (!size) return STATUS_INVALID_PARAMETER;
1621 /* compute the mask for the first int */
1623 first_offset = (unsigned int)addr % sizeof(int);
1624 memset( &first_mask, 0, first_offset );
1626 /* compute the mask for the last int */
1627 last_offset = (size + first_offset) % sizeof(int);
1629 memset( &last_mask, 0xff, last_offset ? last_offset : sizeof(int) );
1631 SERVER_START_REQ( write_process_memory )
1633 req->handle = process;
1634 req->addr = (char *)addr - first_offset;
1635 req->first_mask = first_mask;
1636 req->last_mask = last_mask;
1637 if (first_offset) wine_server_add_data( req, &zero, first_offset );
1638 wine_server_add_data( req, buffer, size );
1639 if (last_offset) wine_server_add_data( req, &zero, sizeof(int) - last_offset );
1641 if ((status = wine_server_call( req ))) size = 0;
1644 if (bytes_written) *bytes_written = size;