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,
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, MAP_PRIVATE | MAP_FIXED, NULL );
685 TRACE_(module)( "mapping section %.8s at %p off %lx size %lx flags %lx\n",
686 sec->Name, ptr + sec->VirtualAddress,
687 sec->PointerToRawData, sec->SizeOfRawData,
688 sec->Characteristics );
690 if ((sec->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA) &&
691 !(sec->Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)) continue;
692 if (!sec->PointerToRawData || !sec->SizeOfRawData) continue;
694 /* Note: if the section is not aligned properly VIRTUAL_mmap will magically
695 * fall back to read(), so we don't need to check anything here.
697 if (VIRTUAL_mmap( fd, ptr + sec->VirtualAddress, sec->SizeOfRawData,
698 sec->PointerToRawData, 0, PROT_READ | PROT_WRITE,
699 MAP_PRIVATE | MAP_FIXED, &removable ) == (void *)-1)
701 ERR_(module)( "Could not map section %.8s, file probably truncated\n", sec->Name );
705 if ((sec->SizeOfRawData < sec->Misc.VirtualSize) && (sec->SizeOfRawData & page_mask))
707 DWORD end = ROUND_SIZE( 0, sec->SizeOfRawData );
708 if (end > sec->Misc.VirtualSize) end = sec->Misc.VirtualSize;
709 TRACE_(module)("clearing %p - %p\n",
710 ptr + sec->VirtualAddress + sec->SizeOfRawData,
711 ptr + sec->VirtualAddress + end );
712 memset( ptr + sec->VirtualAddress + sec->SizeOfRawData, 0,
713 end - sec->SizeOfRawData );
718 /* perform base relocation, if necessary */
722 const IMAGE_DATA_DIRECTORY *relocs;
724 relocs = &nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC];
725 if (!relocs->VirtualAddress || !relocs->Size)
727 if (nt->OptionalHeader.ImageBase == 0x400000) {
728 ERR("Image was mapped at %p: standard load address for a Win32 program (0x00400000) not available\n", ptr);
729 ERR("Do you have exec-shield or prelink active?\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_READ|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 */
917 if (ret < 0 && ret > -4096)
925 return mmap( addr, length, prot, flags, fd, ((off_t)offset_high << 32) | offset_low );
929 /***********************************************************************
932 * Wrapper for mmap() that handles anonymous mappings portably,
933 * and falls back to read if mmap of a file fails.
935 static LPVOID VIRTUAL_mmap( int fd, LPVOID start, DWORD size,
936 DWORD offset_low, DWORD offset_high,
937 int prot, int flags, BOOL *removable )
941 BOOL is_shared_write = FALSE;
943 if (fd == -1) return wine_anon_mmap( start, size, prot, flags );
945 if (prot & PROT_WRITE)
948 if (flags & MAP_SHARED) is_shared_write = TRUE;
951 if (!(flags & MAP_PRIVATE)) is_shared_write = TRUE;
955 if (removable && *removable)
957 /* if on removable media, try using read instead of mmap */
958 if (!is_shared_write) goto fake_mmap;
962 if ((ret = unaligned_mmap( start, size, prot, flags, fd,
963 offset_low, offset_high )) != (LPVOID)-1) return ret;
965 /* mmap() failed; if this is because the file offset is not */
966 /* page-aligned (EINVAL), or because the underlying filesystem */
967 /* does not support mmap() (ENOEXEC,ENODEV), we do it by hand. */
969 if ((errno != ENOEXEC) && (errno != EINVAL) && (errno != ENODEV)) return ret;
970 if (is_shared_write) return ret; /* we cannot fake shared write mappings */
973 /* Reserve the memory with an anonymous mmap */
974 ret = wine_anon_mmap( start, size, PROT_READ | PROT_WRITE, flags );
975 if (ret == (LPVOID)-1) return ret;
976 /* Now read in the file */
977 offset = ((off_t)offset_high << 32) | offset_low;
978 pread( fd, ret, size, offset );
979 mprotect( ret, size, prot ); /* Set the right protection */
984 /***********************************************************************
985 * NtAllocateVirtualMemory (NTDLL.@)
986 * ZwAllocateVirtualMemory (NTDLL.@)
988 NTSTATUS WINAPI NtAllocateVirtualMemory( HANDLE process, PVOID *ret, PVOID addr,
989 ULONG *size_ptr, ULONG type, ULONG protect )
994 DWORD size = *size_ptr;
996 if (!is_current_process( process ))
998 ERR("Unsupported on other process\n");
999 return STATUS_ACCESS_DENIED;
1002 TRACE("%p %08lx %lx %08lx\n", addr, size, type, protect );
1004 if (!size) return STATUS_INVALID_PARAMETER;
1006 /* Round parameters to a page boundary */
1008 if (size > 0x7fc00000) return STATUS_WORKING_SET_LIMIT_RANGE; /* 2Gb - 4Mb */
1012 if (type & MEM_RESERVE) /* Round down to 64k boundary */
1013 base = ROUND_ADDR( addr, granularity_mask );
1015 base = ROUND_ADDR( addr, page_mask );
1016 size = (((UINT_PTR)addr + size + page_mask) & ~page_mask) - (UINT_PTR)base;
1018 /* disallow low 64k, wrap-around and kernel space */
1019 if (((char *)base <= (char *)granularity_mask) ||
1020 ((char *)base + size < (char *)base) ||
1021 (ADDRESS_SPACE_LIMIT && ((char *)base + size > (char *)ADDRESS_SPACE_LIMIT)))
1022 return STATUS_INVALID_PARAMETER;
1027 size = (size + page_mask) & ~page_mask;
1030 if (type & MEM_TOP_DOWN) {
1031 /* FIXME: MEM_TOP_DOWN allocates the largest possible address.
1032 * Is there _ANY_ way to do it with UNIX mmap()?
1034 WARN("MEM_TOP_DOWN ignored\n");
1035 type &= ~MEM_TOP_DOWN;
1038 /* Compute the alloc type flags */
1040 if (type & MEM_SYSTEM)
1042 vprot = VIRTUAL_GetProt( protect ) | VPROT_COMMITTED;
1043 if (type & MEM_IMAGE) vprot |= VPROT_IMAGE;
1047 if (!(type & (MEM_COMMIT | MEM_RESERVE)) || (type & ~(MEM_COMMIT | MEM_RESERVE)))
1049 WARN("called with wrong alloc type flags (%08lx) !\n", type);
1050 return STATUS_INVALID_PARAMETER;
1052 if (type & MEM_COMMIT)
1053 vprot = VIRTUAL_GetProt( protect ) | VPROT_COMMITTED;
1058 /* Reserve the memory */
1060 if (type & MEM_SYSTEM)
1062 if (!(view = VIRTUAL_CreateView( base, size, VFLAG_VALLOC | VFLAG_SYSTEM, vprot, 0 )))
1063 return STATUS_NO_MEMORY;
1065 else if ((type & MEM_RESERVE) || !base)
1067 NTSTATUS res = anon_mmap_aligned( &base, size, VIRTUAL_GetUnixProt( vprot ), 0 );
1068 if (res) return res;
1070 if (!(view = VIRTUAL_CreateView( base, size, VFLAG_VALLOC, vprot, 0 )))
1072 munmap( base, size );
1073 return STATUS_NO_MEMORY;
1078 /* Commit the pages */
1080 if (!(view = VIRTUAL_FindView( base )) ||
1081 ((char *)base + size > (char *)view->base + view->size)) return STATUS_NOT_MAPPED_VIEW;
1083 if (!VIRTUAL_SetProt( view, base, size, vprot )) return STATUS_ACCESS_DENIED;
1088 return STATUS_SUCCESS;
1092 /***********************************************************************
1093 * NtFreeVirtualMemory (NTDLL.@)
1094 * ZwFreeVirtualMemory (NTDLL.@)
1096 NTSTATUS WINAPI NtFreeVirtualMemory( HANDLE process, PVOID *addr_ptr, ULONG *size_ptr, ULONG type )
1100 LPVOID addr = *addr_ptr;
1101 DWORD size = *size_ptr;
1103 if (!is_current_process( process ))
1105 ERR("Unsupported on other process\n");
1106 return STATUS_ACCESS_DENIED;
1109 TRACE("%p %08lx %lx\n", addr, size, type );
1111 /* Fix the parameters */
1113 size = ROUND_SIZE( addr, size );
1114 base = ROUND_ADDR( addr, page_mask );
1116 if (!(view = VIRTUAL_FindView( base )) ||
1117 (base + size > (char *)view->base + view->size) ||
1118 !(view->flags & VFLAG_VALLOC))
1119 return STATUS_INVALID_PARAMETER;
1121 /* Check the type */
1123 if (type & MEM_SYSTEM)
1125 /* return the values that the caller should use to unmap the area */
1126 *addr_ptr = view->base;
1127 *size_ptr = view->size;
1128 view->flags |= VFLAG_SYSTEM;
1129 VIRTUAL_DeleteView( view );
1130 return STATUS_SUCCESS;
1133 if (type == MEM_RELEASE)
1135 /* Free the pages */
1137 if (size || (base != view->base)) return STATUS_INVALID_PARAMETER;
1138 VIRTUAL_DeleteView( view );
1140 else if (type == MEM_DECOMMIT)
1142 /* Decommit the pages by remapping zero-pages instead */
1144 if (wine_anon_mmap( (LPVOID)base, size, VIRTUAL_GetUnixProt(0), MAP_FIXED ) != (LPVOID)base)
1145 ERR( "Could not remap pages, expect trouble\n" );
1146 if (!VIRTUAL_SetProt( view, base, size, 0 )) return STATUS_ACCESS_DENIED; /* FIXME */
1150 WARN("called with wrong free type flags (%08lx) !\n", type);
1151 return STATUS_INVALID_PARAMETER;
1156 return STATUS_SUCCESS;
1160 /***********************************************************************
1161 * NtProtectVirtualMemory (NTDLL.@)
1162 * ZwProtectVirtualMemory (NTDLL.@)
1164 NTSTATUS WINAPI NtProtectVirtualMemory( HANDLE process, PVOID *addr_ptr, ULONG *size_ptr,
1165 ULONG new_prot, ULONG *old_prot )
1171 DWORD prot, size = *size_ptr;
1172 LPVOID addr = *addr_ptr;
1174 if (!is_current_process( process ))
1176 ERR("Unsupported on other process\n");
1177 return STATUS_ACCESS_DENIED;
1180 TRACE("%p %08lx %08lx\n", addr, size, new_prot );
1182 /* Fix the parameters */
1184 size = ROUND_SIZE( addr, size );
1185 base = ROUND_ADDR( addr, page_mask );
1187 if (!(view = VIRTUAL_FindView( base )) ||
1188 (base + size > (char *)view->base + view->size))
1189 return STATUS_INVALID_PARAMETER;
1191 /* Make sure all the pages are committed */
1193 p = view->prot + ((base - (char *)view->base) >> page_shift);
1194 VIRTUAL_GetWin32Prot( *p, &prot, NULL );
1195 for (i = size >> page_shift; i; i--, p++)
1197 if (!(*p & VPROT_COMMITTED)) return STATUS_NOT_COMMITTED;
1200 if (old_prot) *old_prot = prot;
1201 vprot = VIRTUAL_GetProt( new_prot ) | VPROT_COMMITTED;
1202 if (!VIRTUAL_SetProt( view, base, size, vprot )) return STATUS_ACCESS_DENIED;
1206 return STATUS_SUCCESS;
1210 /***********************************************************************
1211 * NtQueryVirtualMemory (NTDLL.@)
1212 * ZwQueryVirtualMemory (NTDLL.@)
1214 NTSTATUS WINAPI NtQueryVirtualMemory( HANDLE process, LPCVOID addr,
1215 MEMORY_INFORMATION_CLASS info_class, PVOID buffer,
1216 ULONG len, ULONG *res_len )
1219 char *base, *alloc_base = 0;
1221 MEMORY_BASIC_INFORMATION *info = buffer;
1223 if (info_class != MemoryBasicInformation) return STATUS_INVALID_INFO_CLASS;
1224 if (ADDRESS_SPACE_LIMIT && addr >= ADDRESS_SPACE_LIMIT)
1225 return STATUS_WORKING_SET_LIMIT_RANGE; /* FIXME */
1227 if (!is_current_process( process ))
1229 ERR("Unsupported on other process\n");
1230 return STATUS_ACCESS_DENIED;
1233 base = ROUND_ADDR( addr, page_mask );
1235 /* Find the view containing the address */
1237 RtlEnterCriticalSection(&csVirtual);
1238 view = VIRTUAL_FirstView;
1243 size = (char *)ADDRESS_SPACE_LIMIT - alloc_base;
1246 if ((char *)view->base > base)
1248 size = (char *)view->base - alloc_base;
1252 if ((char *)view->base + view->size > base)
1254 alloc_base = view->base;
1258 alloc_base = (char *)view->base + view->size;
1261 RtlLeaveCriticalSection(&csVirtual);
1263 /* Fill the info structure */
1267 info->State = MEM_FREE;
1269 info->AllocationProtect = 0;
1274 BYTE vprot = view->prot[(base - alloc_base) >> page_shift];
1275 VIRTUAL_GetWin32Prot( vprot, &info->Protect, &info->State );
1276 for (size = base - alloc_base; size < view->size; size += page_mask+1)
1277 if (view->prot[size >> page_shift] != vprot) break;
1278 VIRTUAL_GetWin32Prot( view->protect, &info->AllocationProtect, NULL );
1279 if (view->protect & VPROT_IMAGE) info->Type = MEM_IMAGE;
1280 else if (view->flags & VFLAG_VALLOC) info->Type = MEM_PRIVATE;
1281 else info->Type = MEM_MAPPED;
1284 info->BaseAddress = (LPVOID)base;
1285 info->AllocationBase = (LPVOID)alloc_base;
1286 info->RegionSize = size - (base - alloc_base);
1287 if (res_len) *res_len = sizeof(*info);
1288 return STATUS_SUCCESS;
1292 /***********************************************************************
1293 * NtLockVirtualMemory (NTDLL.@)
1294 * ZwLockVirtualMemory (NTDLL.@)
1296 NTSTATUS WINAPI NtLockVirtualMemory( HANDLE process, PVOID *addr, ULONG *size, ULONG unknown )
1298 if (!is_current_process( process ))
1300 ERR("Unsupported on other process\n");
1301 return STATUS_ACCESS_DENIED;
1303 return STATUS_SUCCESS;
1307 /***********************************************************************
1308 * NtUnlockVirtualMemory (NTDLL.@)
1309 * ZwUnlockVirtualMemory (NTDLL.@)
1311 NTSTATUS WINAPI NtUnlockVirtualMemory( HANDLE process, PVOID *addr, ULONG *size, ULONG unknown )
1313 if (!is_current_process( process ))
1315 ERR("Unsupported on other process\n");
1316 return STATUS_ACCESS_DENIED;
1318 return STATUS_SUCCESS;
1322 /***********************************************************************
1323 * NtCreateSection (NTDLL.@)
1324 * ZwCreateSection (NTDLL.@)
1326 NTSTATUS WINAPI NtCreateSection( HANDLE *handle, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr,
1327 const LARGE_INTEGER *size, ULONG protect,
1328 ULONG sec_flags, HANDLE file )
1332 DWORD len = attr->ObjectName ? attr->ObjectName->Length : 0;
1334 /* Check parameters */
1336 if (len > MAX_PATH*sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
1338 vprot = VIRTUAL_GetProt( protect );
1339 if (sec_flags & SEC_RESERVE)
1341 if (file) return STATUS_INVALID_PARAMETER;
1343 else vprot |= VPROT_COMMITTED;
1344 if (sec_flags & SEC_NOCACHE) vprot |= VPROT_NOCACHE;
1345 if (sec_flags & SEC_IMAGE) vprot |= VPROT_IMAGE;
1347 /* Create the server object */
1349 SERVER_START_REQ( create_mapping )
1351 req->file_handle = file;
1352 req->size_high = size ? size->u.HighPart : 0;
1353 req->size_low = size ? size->u.LowPart : 0;
1354 req->protect = vprot;
1355 req->access = access;
1356 req->inherit = (attr->Attributes & OBJ_INHERIT) != 0;
1357 if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
1358 ret = wine_server_call( req );
1359 *handle = reply->handle;
1366 /***********************************************************************
1367 * NtOpenSection (NTDLL.@)
1368 * ZwOpenSection (NTDLL.@)
1370 NTSTATUS WINAPI NtOpenSection( HANDLE *handle, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr )
1373 DWORD len = attr->ObjectName->Length;
1375 if (len > MAX_PATH*sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
1377 SERVER_START_REQ( open_mapping )
1379 req->access = access;
1380 req->inherit = (attr->Attributes & OBJ_INHERIT) != 0;
1381 wine_server_add_data( req, attr->ObjectName->Buffer, len );
1382 if (!(ret = wine_server_call( req ))) *handle = reply->handle;
1389 /***********************************************************************
1390 * NtMapViewOfSection (NTDLL.@)
1391 * ZwMapViewOfSection (NTDLL.@)
1393 NTSTATUS WINAPI NtMapViewOfSection( HANDLE handle, HANDLE process, PVOID *addr_ptr, ULONG zero_bits,
1394 ULONG commit_size, const LARGE_INTEGER *offset, ULONG *size_ptr,
1395 SECTION_INHERIT inherit, ULONG alloc_type, ULONG protect )
1400 int flags = MAP_PRIVATE;
1401 int unix_handle = -1;
1403 void *base, *ptr = (void *)-1, *ret;
1404 DWORD size_low, size_high, header_size, shared_size;
1408 if (!is_current_process( process ))
1410 ERR("Unsupported on other process\n");
1411 return STATUS_ACCESS_DENIED;
1414 TRACE("handle=%p addr=%p off=%lx%08lx size=%x access=%lx\n",
1415 handle, *addr_ptr, offset->u.HighPart, offset->u.LowPart, size, protect );
1417 /* Check parameters */
1419 if ((offset->u.LowPart & granularity_mask) ||
1420 (*addr_ptr && ((UINT_PTR)*addr_ptr & granularity_mask)))
1421 return STATUS_INVALID_PARAMETER;
1423 SERVER_START_REQ( get_mapping_info )
1425 req->handle = handle;
1426 res = wine_server_call( req );
1427 prot = reply->protect;
1429 size_low = reply->size_low;
1430 size_high = reply->size_high;
1431 header_size = reply->header_size;
1432 shared_file = reply->shared_file;
1433 shared_size = reply->shared_size;
1434 removable = reply->removable;
1437 if (res) return res;
1439 if ((res = wine_server_handle_to_fd( handle, 0, &unix_handle, NULL, NULL ))) return res;
1441 if (prot & VPROT_IMAGE)
1447 if ((res = wine_server_handle_to_fd( shared_file, GENERIC_READ, &shared_fd,
1448 NULL, NULL ))) goto error;
1449 res = map_image( handle, unix_handle, base, size_low, header_size,
1450 shared_fd, removable, addr_ptr );
1451 wine_server_release_fd( shared_file, shared_fd );
1452 NtClose( shared_file );
1456 res = map_image( handle, unix_handle, base, size_low, header_size,
1457 -1, removable, addr_ptr );
1459 wine_server_release_fd( handle, unix_handle );
1460 if (!res) *size_ptr = size_low;
1465 ERR("Sizes larger than 4Gb not supported\n");
1467 if ((offset->u.LowPart >= size_low) ||
1468 (*size_ptr > size_low - offset->u.LowPart))
1470 res = STATUS_INVALID_PARAMETER;
1473 if (*size_ptr) size = ROUND_SIZE( offset->u.LowPart, *size_ptr );
1474 else size = size_low - offset->u.LowPart;
1480 case PAGE_READWRITE:
1481 case PAGE_EXECUTE_READWRITE:
1482 if (!(prot & VPROT_WRITE))
1484 res = STATUS_INVALID_PARAMETER;
1490 case PAGE_WRITECOPY:
1492 case PAGE_EXECUTE_READ:
1493 case PAGE_EXECUTE_WRITECOPY:
1494 if (prot & VPROT_READ) break;
1497 res = STATUS_INVALID_PARAMETER;
1501 /* FIXME: If a mapping is created with SEC_RESERVE and a process,
1502 * which has a view of this mapping commits some pages, they will
1503 * appear commited in all other processes, which have the same
1504 * view created. Since we don`t support this yet, we create the
1505 * whole mapping commited.
1507 prot |= VPROT_COMMITTED;
1509 /* Reserve a properly aligned area */
1511 if ((res = anon_mmap_aligned( addr_ptr, size, PROT_NONE, 0 ))) goto error;
1516 TRACE("handle=%p size=%x offset=%lx\n", handle, size, offset->u.LowPart );
1518 ret = VIRTUAL_mmap( unix_handle, ptr, size, offset->u.LowPart, offset->u.HighPart,
1519 VIRTUAL_GetUnixProt( prot ), flags | MAP_FIXED, &removable );
1522 ERR( "VIRTUAL_mmap %p %x %lx%08lx failed\n",
1523 ptr, size, offset->u.HighPart, offset->u.LowPart );
1524 res = STATUS_NO_MEMORY; /* FIXME */
1527 if (removable) handle = 0; /* don't keep handle open on removable media */
1529 if (!(view = VIRTUAL_CreateView( ptr, size, 0, prot, handle )))
1531 res = STATUS_NO_MEMORY;
1534 wine_server_release_fd( handle, unix_handle );
1536 return STATUS_SUCCESS;
1539 wine_server_release_fd( handle, unix_handle );
1540 if (ptr != (void *)-1) munmap( ptr, size );
1545 /***********************************************************************
1546 * NtUnmapViewOfSection (NTDLL.@)
1547 * ZwUnmapViewOfSection (NTDLL.@)
1549 NTSTATUS WINAPI NtUnmapViewOfSection( HANDLE process, PVOID addr )
1552 void *base = ROUND_ADDR( addr, page_mask );
1554 if (!is_current_process( process ))
1556 ERR("Unsupported on other process\n");
1557 return STATUS_ACCESS_DENIED;
1559 if (!(view = VIRTUAL_FindView( base )) || (base != view->base)) return STATUS_INVALID_PARAMETER;
1560 VIRTUAL_DeleteView( view );
1561 return STATUS_SUCCESS;
1565 /***********************************************************************
1566 * NtFlushVirtualMemory (NTDLL.@)
1567 * ZwFlushVirtualMemory (NTDLL.@)
1569 NTSTATUS WINAPI NtFlushVirtualMemory( HANDLE process, LPCVOID *addr_ptr,
1570 ULONG *size_ptr, ULONG unknown )
1573 void *addr = ROUND_ADDR( *addr_ptr, page_mask );
1575 if (!is_current_process( process ))
1577 ERR("Unsupported on other process\n");
1578 return STATUS_ACCESS_DENIED;
1580 if (!(view = VIRTUAL_FindView( addr ))) return STATUS_INVALID_PARAMETER;
1581 if (!*size_ptr) *size_ptr = view->size;
1583 if (!msync( addr, *size_ptr, MS_SYNC )) return STATUS_SUCCESS;
1584 return STATUS_NOT_MAPPED_DATA;
1588 /***********************************************************************
1589 * NtReadVirtualMemory (NTDLL.@)
1590 * ZwReadVirtualMemory (NTDLL.@)
1592 NTSTATUS WINAPI NtReadVirtualMemory( HANDLE process, const void *addr, void *buffer,
1593 SIZE_T size, SIZE_T *bytes_read )
1597 SERVER_START_REQ( read_process_memory )
1599 req->handle = process;
1600 req->addr = (void *)addr;
1601 wine_server_set_reply( req, buffer, size );
1602 if ((status = wine_server_call( req ))) size = 0;
1605 if (bytes_read) *bytes_read = size;
1610 /***********************************************************************
1611 * NtWriteVirtualMemory (NTDLL.@)
1612 * ZwWriteVirtualMemory (NTDLL.@)
1614 NTSTATUS WINAPI NtWriteVirtualMemory( HANDLE process, void *addr, const void *buffer,
1615 SIZE_T size, SIZE_T *bytes_written )
1617 static const unsigned int zero;
1618 unsigned int first_offset, last_offset, first_mask, last_mask;
1621 if (!size) return STATUS_INVALID_PARAMETER;
1623 /* compute the mask for the first int */
1625 first_offset = (unsigned int)addr % sizeof(int);
1626 memset( &first_mask, 0, first_offset );
1628 /* compute the mask for the last int */
1629 last_offset = (size + first_offset) % sizeof(int);
1631 memset( &last_mask, 0xff, last_offset ? last_offset : sizeof(int) );
1633 SERVER_START_REQ( write_process_memory )
1635 req->handle = process;
1636 req->addr = (char *)addr - first_offset;
1637 req->first_mask = first_mask;
1638 req->last_mask = last_mask;
1639 if (first_offset) wine_server_add_data( req, &zero, first_offset );
1640 wine_server_add_data( req, buffer, size );
1641 if (last_offset) wine_server_add_data( req, &zero, sizeof(int) - last_offset );
1643 if ((status = wine_server_call( req ))) size = 0;
1646 if (bytes_written) *bytes_written = size;