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
45 #include "wine/library.h"
46 #include "wine/server.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(virtual);
50 WINE_DECLARE_DEBUG_CHANNEL(module
);
59 struct _FV
*next
; /* Next view */
60 struct _FV
*prev
; /* Prev view */
61 void *base
; /* Base address */
62 UINT size
; /* Size in bytes */
63 UINT flags
; /* Allocation flags */
64 HANDLE mapping
; /* Handle to the file mapping */
65 HANDLERPROC handlerProc
; /* Fault handler */
66 LPVOID handlerArg
; /* Fault handler argument */
67 BYTE protect
; /* Protection for all pages at allocation time */
68 BYTE prot
[1]; /* Protection byte for each page */
72 #define VFLAG_SYSTEM 0x01
73 #define VFLAG_VALLOC 0x02 /* allocated by VirtualAlloc */
75 /* Conversion from VPROT_* to Win32 flags */
76 static const BYTE VIRTUAL_Win32Flags
[16] =
78 PAGE_NOACCESS
, /* 0 */
79 PAGE_READONLY
, /* READ */
80 PAGE_READWRITE
, /* WRITE */
81 PAGE_READWRITE
, /* READ | WRITE */
82 PAGE_EXECUTE
, /* EXEC */
83 PAGE_EXECUTE_READ
, /* READ | EXEC */
84 PAGE_EXECUTE_READWRITE
, /* WRITE | EXEC */
85 PAGE_EXECUTE_READWRITE
, /* READ | WRITE | EXEC */
86 PAGE_WRITECOPY
, /* WRITECOPY */
87 PAGE_WRITECOPY
, /* READ | WRITECOPY */
88 PAGE_WRITECOPY
, /* WRITE | WRITECOPY */
89 PAGE_WRITECOPY
, /* READ | WRITE | WRITECOPY */
90 PAGE_EXECUTE_WRITECOPY
, /* EXEC | WRITECOPY */
91 PAGE_EXECUTE_WRITECOPY
, /* READ | EXEC | WRITECOPY */
92 PAGE_EXECUTE_WRITECOPY
, /* WRITE | EXEC | WRITECOPY */
93 PAGE_EXECUTE_WRITECOPY
/* READ | WRITE | EXEC | WRITECOPY */
97 static FILE_VIEW
*VIRTUAL_FirstView
;
99 static CRITICAL_SECTION csVirtual
;
100 static CRITICAL_SECTION_DEBUG critsect_debug
=
103 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
104 0, 0, { 0, (DWORD
)(__FILE__
": csVirtual") }
106 static CRITICAL_SECTION csVirtual
= { &critsect_debug
, -1, 0, 0, 0, 0 };
109 /* These are always the same on an i386, and it will be faster this way */
110 # define page_mask 0xfff
111 # define page_shift 12
112 # define page_size 0x1000
113 /* Note: ADDRESS_SPACE_LIMIT is a Windows limit, you cannot change it.
114 * If you are on Solaris you need to find a way to avoid having the system
115 * allocate things above 0xc000000. Don't touch that define.
117 # define ADDRESS_SPACE_LIMIT ((void *)0xc0000000) /* top of the user address space */
119 static UINT page_shift
;
120 static UINT page_mask
;
121 static UINT page_size
;
122 # define ADDRESS_SPACE_LIMIT 0 /* no limit needed on other platforms */
123 #endif /* __i386__ */
124 #define granularity_mask 0xffff /* Allocation granularity (usually 64k) */
126 #define ROUND_ADDR(addr,mask) \
127 ((void *)((UINT_PTR)(addr) & ~(mask)))
129 #define ROUND_SIZE(addr,size) \
130 (((UINT)(size) + ((UINT_PTR)(addr) & page_mask) + page_mask) & ~page_mask)
132 #define VIRTUAL_DEBUG_DUMP_VIEW(view) \
133 if (!TRACE_ON(virtual)); else VIRTUAL_DumpView(view)
135 static LPVOID
VIRTUAL_mmap( int fd
, LPVOID start
, DWORD size
, DWORD offset_low
,
136 DWORD offset_high
, int prot
, int flags
, BOOL
*removable
);
139 /***********************************************************************
142 static const char *VIRTUAL_GetProtStr( BYTE prot
)
144 static char buffer
[6];
145 buffer
[0] = (prot
& VPROT_COMMITTED
) ? 'c' : '-';
146 buffer
[1] = (prot
& VPROT_GUARD
) ? 'g' : '-';
147 buffer
[2] = (prot
& VPROT_READ
) ? 'r' : '-';
148 buffer
[3] = (prot
& VPROT_WRITE
) ?
149 ((prot
& VPROT_WRITECOPY
) ? 'w' : 'W') : '-';
150 buffer
[4] = (prot
& VPROT_EXEC
) ? 'x' : '-';
156 /***********************************************************************
159 static void VIRTUAL_DumpView( FILE_VIEW
*view
)
162 char *addr
= view
->base
;
163 BYTE prot
= view
->prot
[0];
165 DPRINTF( "View: %p - %p", addr
, addr
+ view
->size
- 1 );
166 if (view
->flags
& VFLAG_SYSTEM
)
167 DPRINTF( " (system)\n" );
168 else if (view
->flags
& VFLAG_VALLOC
)
169 DPRINTF( " (valloc)\n" );
170 else if (view
->mapping
)
171 DPRINTF( " %p\n", view
->mapping
);
173 DPRINTF( " (anonymous)\n");
175 for (count
= i
= 1; i
< view
->size
>> page_shift
; i
++, count
++)
177 if (view
->prot
[i
] == prot
) continue;
178 DPRINTF( " %p - %p %s\n",
179 addr
, addr
+ (count
<< page_shift
) - 1, VIRTUAL_GetProtStr(prot
) );
180 addr
+= (count
<< page_shift
);
181 prot
= view
->prot
[i
];
185 DPRINTF( " %p - %p %s\n",
186 addr
, addr
+ (count
<< page_shift
) - 1, VIRTUAL_GetProtStr(prot
) );
190 /***********************************************************************
193 void VIRTUAL_Dump(void)
196 DPRINTF( "\nDump of all virtual memory views:\n\n" );
197 RtlEnterCriticalSection(&csVirtual
);
198 view
= VIRTUAL_FirstView
;
201 VIRTUAL_DumpView( view
);
204 RtlLeaveCriticalSection(&csVirtual
);
208 /***********************************************************************
211 * Find the view containing a given address.
217 static FILE_VIEW
*VIRTUAL_FindView( const void *addr
) /* [in] Address */
221 RtlEnterCriticalSection(&csVirtual
);
222 view
= VIRTUAL_FirstView
;
225 if (view
->base
> addr
)
230 if ((char*)view
->base
+ view
->size
> (char*)addr
) break;
233 RtlLeaveCriticalSection(&csVirtual
);
238 /***********************************************************************
241 * Create a new view and add it in the linked list.
243 static FILE_VIEW
*VIRTUAL_CreateView( void *base
, UINT size
, UINT flags
,
244 BYTE vprot
, HANDLE mapping
)
246 FILE_VIEW
*view
, *prev
;
248 /* Create the view structure */
250 assert( !((unsigned int)base
& page_mask
) );
251 assert( !(size
& page_mask
) );
253 if (!(view
= (FILE_VIEW
*)malloc( sizeof(*view
) + size
- 1 ))) return NULL
;
255 view
->size
= size
<< page_shift
;
257 view
->mapping
= mapping
;
258 view
->protect
= vprot
;
259 view
->handlerProc
= NULL
;
260 memset( view
->prot
, vprot
, size
);
262 /* Duplicate the mapping handle */
265 NtDuplicateObject( GetCurrentProcess(), view
->mapping
,
266 GetCurrentProcess(), &view
->mapping
,
267 0, 0, DUPLICATE_SAME_ACCESS
))
273 /* Insert it in the linked list */
275 RtlEnterCriticalSection(&csVirtual
);
276 if (!VIRTUAL_FirstView
|| (VIRTUAL_FirstView
->base
> base
))
278 view
->next
= VIRTUAL_FirstView
;
280 if (view
->next
) view
->next
->prev
= view
;
281 VIRTUAL_FirstView
= view
;
285 prev
= VIRTUAL_FirstView
;
286 while (prev
->next
&& (prev
->next
->base
< base
)) prev
= prev
->next
;
287 view
->next
= prev
->next
;
289 if (view
->next
) view
->next
->prev
= view
;
292 RtlLeaveCriticalSection(&csVirtual
);
293 VIRTUAL_DEBUG_DUMP_VIEW( view
);
298 /***********************************************************************
305 static void VIRTUAL_DeleteView( FILE_VIEW
*view
) /* [in] View */
307 if (!(view
->flags
& VFLAG_SYSTEM
))
308 munmap( (void *)view
->base
, view
->size
);
309 RtlEnterCriticalSection(&csVirtual
);
310 if (view
->next
) view
->next
->prev
= view
->prev
;
311 if (view
->prev
) view
->prev
->next
= view
->next
;
312 else VIRTUAL_FirstView
= view
->next
;
313 RtlLeaveCriticalSection(&csVirtual
);
314 if (view
->mapping
) NtClose( view
->mapping
);
319 /***********************************************************************
320 * VIRTUAL_GetUnixProt
322 * Convert page protections to protection for mmap/mprotect.
324 static int VIRTUAL_GetUnixProt( BYTE vprot
)
327 if ((vprot
& VPROT_COMMITTED
) && !(vprot
& VPROT_GUARD
))
329 if (vprot
& VPROT_READ
) prot
|= PROT_READ
;
330 if (vprot
& VPROT_WRITE
) prot
|= PROT_WRITE
;
331 if (vprot
& VPROT_WRITECOPY
) prot
|= PROT_WRITE
;
332 if (vprot
& VPROT_EXEC
) prot
|= PROT_EXEC
;
338 /***********************************************************************
339 * VIRTUAL_GetWin32Prot
341 * Convert page protections to Win32 flags.
346 static void VIRTUAL_GetWin32Prot(
347 BYTE vprot
, /* [in] Page protection flags */
348 DWORD
*protect
, /* [out] Location to store Win32 protection flags */
349 DWORD
*state
) /* [out] Location to store mem state flag */
352 *protect
= VIRTUAL_Win32Flags
[vprot
& 0x0f];
353 /* if (vprot & VPROT_GUARD) *protect |= PAGE_GUARD;*/
354 if (vprot
& VPROT_NOCACHE
) *protect
|= PAGE_NOCACHE
;
356 if (vprot
& VPROT_GUARD
) *protect
= PAGE_NOACCESS
;
359 if (state
) *state
= (vprot
& VPROT_COMMITTED
) ? MEM_COMMIT
: MEM_RESERVE
;
363 /***********************************************************************
366 * Build page protections from Win32 flags.
369 * Value of page protection flags
371 static BYTE
VIRTUAL_GetProt( DWORD protect
) /* [in] Win32 protection flags */
375 switch(protect
& 0xff)
381 vprot
= VPROT_READ
| VPROT_WRITE
;
384 /* MSDN CreateFileMapping() states that if PAGE_WRITECOPY is given,
385 * that the hFile must have been opened with GENERIC_READ and
386 * GENERIC_WRITE access. This is WRONG as tests show that you
387 * only need GENERIC_READ access (at least for Win9x,
388 * FIXME: what about NT?). Thus, we don't put VPROT_WRITE in
389 * PAGE_WRITECOPY and PAGE_EXECUTE_WRITECOPY.
391 vprot
= VPROT_READ
| VPROT_WRITECOPY
;
396 case PAGE_EXECUTE_READ
:
397 vprot
= VPROT_EXEC
| VPROT_READ
;
399 case PAGE_EXECUTE_READWRITE
:
400 vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITE
;
402 case PAGE_EXECUTE_WRITECOPY
:
403 /* See comment for PAGE_WRITECOPY above */
404 vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITECOPY
;
411 if (protect
& PAGE_GUARD
) vprot
|= VPROT_GUARD
;
412 if (protect
& PAGE_NOCACHE
) vprot
|= VPROT_NOCACHE
;
417 /***********************************************************************
420 * Change the protection of a range of pages.
426 static BOOL
VIRTUAL_SetProt( FILE_VIEW
*view
, /* [in] Pointer to view */
427 void *base
, /* [in] Starting address */
428 UINT size
, /* [in] Size in bytes */
429 BYTE vprot
) /* [in] Protections to use */
432 base
, (char *)base
+ size
- 1, VIRTUAL_GetProtStr( vprot
) );
434 if (mprotect( base
, size
, VIRTUAL_GetUnixProt(vprot
) ))
435 return FALSE
; /* FIXME: last error */
437 memset( view
->prot
+ (((char *)base
- (char *)view
->base
) >> page_shift
),
438 vprot
, size
>> page_shift
);
439 VIRTUAL_DEBUG_DUMP_VIEW( view
);
444 /***********************************************************************
447 * Create an anonymous mapping aligned to the allocation granularity.
449 static NTSTATUS
anon_mmap_aligned( void **addr
, unsigned int size
, int prot
, int flags
)
451 void *ptr
, *base
= *addr
;
452 unsigned int view_size
= size
+ (base
? 0 : granularity_mask
+ 1);
454 if ((ptr
= wine_anon_mmap( base
, view_size
, prot
, flags
)) == (void *)-1)
456 if (errno
== ENOMEM
) return STATUS_NO_MEMORY
;
457 return STATUS_INVALID_PARAMETER
;
462 /* Release the extra memory while keeping the range
463 * starting on the granularity boundary. */
464 if ((unsigned int)ptr
& granularity_mask
)
466 unsigned int extra
= granularity_mask
+ 1 - ((unsigned int)ptr
& granularity_mask
);
467 munmap( ptr
, extra
);
468 ptr
= (char *)ptr
+ extra
;
471 if (view_size
> size
)
472 munmap( (char *)ptr
+ size
, view_size
- size
);
474 else if (ptr
!= base
)
476 /* We couldn't get the address we wanted */
477 munmap( ptr
, view_size
);
478 return STATUS_CONFLICTING_ADDRESSES
;
481 return STATUS_SUCCESS
;
485 /***********************************************************************
488 * Apply the relocations to a mapped PE image
490 static int do_relocations( char *base
, const IMAGE_DATA_DIRECTORY
*dir
,
491 int delta
, DWORD total_size
)
493 IMAGE_BASE_RELOCATION
*rel
;
495 TRACE_(module
)( "relocating from %p-%p to %p-%p\n",
496 base
- delta
, base
- delta
+ total_size
, base
, base
+ total_size
);
498 for (rel
= (IMAGE_BASE_RELOCATION
*)(base
+ dir
->VirtualAddress
);
499 ((char *)rel
< base
+ dir
->VirtualAddress
+ dir
->Size
) && rel
->SizeOfBlock
;
500 rel
= (IMAGE_BASE_RELOCATION
*)((char*)rel
+ rel
->SizeOfBlock
) )
502 char *page
= base
+ rel
->VirtualAddress
;
503 WORD
*TypeOffset
= (WORD
*)(rel
+ 1);
504 int i
, count
= (rel
->SizeOfBlock
- sizeof(*rel
)) / sizeof(*TypeOffset
);
506 if (!count
) continue;
509 if ((char *)rel
+ rel
->SizeOfBlock
> base
+ dir
->VirtualAddress
+ dir
->Size
||
510 page
> base
+ total_size
)
512 ERR_(module
)("invalid relocation %p,%lx,%ld at %p,%lx,%lx\n",
513 rel
, rel
->VirtualAddress
, rel
->SizeOfBlock
,
514 base
, dir
->VirtualAddress
, dir
->Size
);
518 TRACE_(module
)("%ld relocations for page %lx\n", rel
->SizeOfBlock
, rel
->VirtualAddress
);
520 /* patching in reverse order */
521 for (i
= 0 ; i
< count
; i
++)
523 int offset
= TypeOffset
[i
] & 0xFFF;
524 int type
= TypeOffset
[i
] >> 12;
527 case IMAGE_REL_BASED_ABSOLUTE
:
529 case IMAGE_REL_BASED_HIGH
:
530 *(short*)(page
+offset
) += HIWORD(delta
);
532 case IMAGE_REL_BASED_LOW
:
533 *(short*)(page
+offset
) += LOWORD(delta
);
535 case IMAGE_REL_BASED_HIGHLOW
:
536 *(int*)(page
+offset
) += delta
;
537 /* FIXME: if this is an exported address, fire up enhanced logic */
540 FIXME_(module
)("Unknown/unsupported fixup type %d.\n", type
);
549 /***********************************************************************
552 * Map an executable (PE format) image into memory.
554 static NTSTATUS
map_image( HANDLE hmapping
, int fd
, char *base
, DWORD total_size
,
555 DWORD header_size
, int shared_fd
, DWORD shared_size
,
556 BOOL removable
, PVOID
*addr_ptr
)
558 IMAGE_DOS_HEADER
*dos
;
559 IMAGE_NT_HEADERS
*nt
;
560 IMAGE_SECTION_HEADER
*sec
;
561 IMAGE_DATA_DIRECTORY
*imports
;
562 NTSTATUS status
= STATUS_INVALID_IMAGE_FORMAT
; /* generic error (FIXME) */
567 /* zero-map the whole range */
569 if (base
< (char *)0x110000 || /* make sure the DOS area remains free */
570 (ptr
= wine_anon_mmap( base
, total_size
,
571 PROT_READ
| PROT_WRITE
| PROT_EXEC
, 0 )) == (char *)-1)
573 ptr
= wine_anon_mmap( NULL
, total_size
,
574 PROT_READ
| PROT_WRITE
| PROT_EXEC
, 0 );
575 if (ptr
== (char *)-1)
577 ERR_(module
)("Not enough memory for module (%ld bytes)\n", total_size
);
581 TRACE_(module
)( "mapped PE file at %p-%p\n", ptr
, ptr
+ total_size
);
585 if (VIRTUAL_mmap( fd
, ptr
, header_size
, 0, 0, PROT_READ
,
586 MAP_PRIVATE
| MAP_FIXED
, &removable
) == (char *)-1) goto error
;
587 dos
= (IMAGE_DOS_HEADER
*)ptr
;
588 nt
= (IMAGE_NT_HEADERS
*)(ptr
+ dos
->e_lfanew
);
589 if ((char *)(nt
+ 1) > ptr
+ header_size
) goto error
;
591 sec
= (IMAGE_SECTION_HEADER
*)((char*)&nt
->OptionalHeader
+nt
->FileHeader
.SizeOfOptionalHeader
);
592 if ((char *)(sec
+ nt
->FileHeader
.NumberOfSections
) > ptr
+ header_size
) goto error
;
594 imports
= nt
->OptionalHeader
.DataDirectory
+ IMAGE_DIRECTORY_ENTRY_IMPORT
;
595 if (!imports
->Size
|| !imports
->VirtualAddress
) imports
= NULL
;
597 /* check the architecture */
599 if (nt
->FileHeader
.Machine
!= IMAGE_FILE_MACHINE_I386
)
601 MESSAGE("Trying to load PE image for unsupported architecture (");
602 switch (nt
->FileHeader
.Machine
)
604 case IMAGE_FILE_MACHINE_UNKNOWN
: MESSAGE("Unknown"); break;
605 case IMAGE_FILE_MACHINE_I860
: MESSAGE("I860"); break;
606 case IMAGE_FILE_MACHINE_R3000
: MESSAGE("R3000"); break;
607 case IMAGE_FILE_MACHINE_R4000
: MESSAGE("R4000"); break;
608 case IMAGE_FILE_MACHINE_R10000
: MESSAGE("R10000"); break;
609 case IMAGE_FILE_MACHINE_ALPHA
: MESSAGE("Alpha"); break;
610 case IMAGE_FILE_MACHINE_POWERPC
: MESSAGE("PowerPC"); break;
611 default: MESSAGE("Unknown-%04x", nt
->FileHeader
.Machine
); break;
617 /* map all the sections */
619 for (i
= pos
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
623 /* a few sanity checks */
624 size
= sec
->VirtualAddress
+ ROUND_SIZE( sec
->VirtualAddress
, sec
->Misc
.VirtualSize
);
625 if (sec
->VirtualAddress
> total_size
|| size
> total_size
|| size
< sec
->VirtualAddress
)
627 ERR_(module
)( "Section %.8s too large (%lx+%lx/%lx)\n",
628 sec
->Name
, sec
->VirtualAddress
, sec
->Misc
.VirtualSize
, total_size
);
632 if ((sec
->Characteristics
& IMAGE_SCN_MEM_SHARED
) &&
633 (sec
->Characteristics
& IMAGE_SCN_MEM_WRITE
))
635 size
= ROUND_SIZE( 0, sec
->Misc
.VirtualSize
);
636 TRACE_(module
)( "mapping shared section %.8s at %p off %lx (%x) size %lx (%lx) flags %lx\n",
637 sec
->Name
, ptr
+ sec
->VirtualAddress
,
638 sec
->PointerToRawData
, pos
, sec
->SizeOfRawData
,
639 size
, sec
->Characteristics
);
640 if (VIRTUAL_mmap( shared_fd
, ptr
+ sec
->VirtualAddress
, size
,
641 pos
, 0, PROT_READ
|PROT_WRITE
|PROT_EXEC
,
642 MAP_SHARED
|MAP_FIXED
, NULL
) == (void *)-1)
644 ERR_(module
)( "Could not map shared section %.8s\n", sec
->Name
);
648 /* check if the import directory falls inside this section */
649 if (imports
&& imports
->VirtualAddress
>= sec
->VirtualAddress
&&
650 imports
->VirtualAddress
< sec
->VirtualAddress
+ size
)
652 UINT_PTR base
= imports
->VirtualAddress
& ~page_mask
;
653 UINT_PTR end
= base
+ ROUND_SIZE( imports
->VirtualAddress
, imports
->Size
);
654 if (end
> sec
->VirtualAddress
+ size
) end
= sec
->VirtualAddress
+ size
;
655 if (end
> base
) VIRTUAL_mmap( shared_fd
, ptr
+ base
, end
- base
,
656 pos
+ (base
- sec
->VirtualAddress
), 0,
657 PROT_READ
|PROT_WRITE
|PROT_EXEC
,
658 MAP_PRIVATE
|MAP_FIXED
, NULL
);
664 TRACE_(module
)( "mapping section %.8s at %p off %lx size %lx flags %lx\n",
665 sec
->Name
, ptr
+ sec
->VirtualAddress
,
666 sec
->PointerToRawData
, sec
->SizeOfRawData
,
667 sec
->Characteristics
);
669 if ((sec
->Characteristics
& IMAGE_SCN_CNT_UNINITIALIZED_DATA
) &&
670 !(sec
->Characteristics
& IMAGE_SCN_CNT_INITIALIZED_DATA
)) continue;
671 if (!sec
->PointerToRawData
|| !sec
->SizeOfRawData
) continue;
673 /* Note: if the section is not aligned properly VIRTUAL_mmap will magically
674 * fall back to read(), so we don't need to check anything here.
676 if (VIRTUAL_mmap( fd
, ptr
+ sec
->VirtualAddress
, sec
->SizeOfRawData
,
677 sec
->PointerToRawData
, 0, PROT_READ
|PROT_WRITE
|PROT_EXEC
,
678 MAP_PRIVATE
| MAP_FIXED
, &removable
) == (void *)-1)
680 ERR_(module
)( "Could not map section %.8s, file probably truncated\n", sec
->Name
);
684 if ((sec
->SizeOfRawData
< sec
->Misc
.VirtualSize
) && (sec
->SizeOfRawData
& page_mask
))
686 DWORD end
= ROUND_SIZE( 0, sec
->SizeOfRawData
);
687 if (end
> sec
->Misc
.VirtualSize
) end
= sec
->Misc
.VirtualSize
;
688 TRACE_(module
)("clearing %p - %p\n",
689 ptr
+ sec
->VirtualAddress
+ sec
->SizeOfRawData
,
690 ptr
+ sec
->VirtualAddress
+ end
);
691 memset( ptr
+ sec
->VirtualAddress
+ sec
->SizeOfRawData
, 0,
692 end
- sec
->SizeOfRawData
);
697 /* perform base relocation, if necessary */
701 const IMAGE_DATA_DIRECTORY
*relocs
;
703 relocs
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
];
704 if (!relocs
->VirtualAddress
|| !relocs
->Size
)
706 if (nt
->OptionalHeader
.ImageBase
== 0x400000)
707 ERR("Standard load address for a Win32 program (0x00400000) not available - security-patched kernel ?\n");
709 ERR( "FATAL: Need to relocate module from addr %lx, but there are no relocation records\n",
710 nt
->OptionalHeader
.ImageBase
);
714 /* FIXME: If we need to relocate a system DLL (base > 2GB) we should
715 * really make sure that the *new* base address is also > 2GB.
716 * Some DLLs really check the MSB of the module handle :-/
718 if ((nt
->OptionalHeader
.ImageBase
& 0x80000000) && !((DWORD
)base
& 0x80000000))
719 ERR( "Forced to relocate system DLL (base > 2GB). This is not good.\n" );
721 if (!do_relocations( ptr
, relocs
, ptr
- base
, total_size
))
727 if (removable
) hmapping
= 0; /* don't keep handle open on removable media */
728 if (!(view
= VIRTUAL_CreateView( ptr
, total_size
, 0, VPROT_COMMITTED
|VPROT_READ
, hmapping
)))
730 status
= STATUS_NO_MEMORY
;
734 /* set the image protections */
736 sec
= (IMAGE_SECTION_HEADER
*)((char *)&nt
->OptionalHeader
+nt
->FileHeader
.SizeOfOptionalHeader
);
737 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
739 DWORD size
= ROUND_SIZE( sec
->VirtualAddress
, sec
->Misc
.VirtualSize
);
740 BYTE vprot
= VPROT_COMMITTED
;
741 if (sec
->Characteristics
& IMAGE_SCN_MEM_READ
) vprot
|= VPROT_READ
;
742 if (sec
->Characteristics
& IMAGE_SCN_MEM_WRITE
) vprot
|= VPROT_WRITE
|VPROT_WRITECOPY
;
743 if (sec
->Characteristics
& IMAGE_SCN_MEM_EXECUTE
) vprot
|= VPROT_EXEC
;
745 /* make sure the import directory is writable */
746 if (imports
&& imports
->VirtualAddress
>= sec
->VirtualAddress
&&
747 imports
->VirtualAddress
< sec
->VirtualAddress
+ size
)
748 vprot
|= VPROT_READ
|VPROT_WRITE
|VPROT_WRITECOPY
;
750 VIRTUAL_SetProt( view
, ptr
+ sec
->VirtualAddress
, size
, vprot
);
755 return STATUS_SUCCESS
;
758 if (ptr
!= (char *)-1) munmap( ptr
, total_size
);
764 /***********************************************************************
767 * Check whether a process handle is for the current process.
769 static BOOL
is_current_process( HANDLE handle
)
773 if (handle
== GetCurrentProcess()) return TRUE
;
774 SERVER_START_REQ( get_process_info
)
776 req
->handle
= handle
;
777 if (!wine_server_call( req
))
778 ret
= ((DWORD
)reply
->pid
== GetCurrentProcessId());
785 /***********************************************************************
789 DECL_GLOBAL_CONSTRUCTOR(VIRTUAL_Init
)
791 page_size
= getpagesize();
792 page_mask
= page_size
- 1;
793 /* Make sure we have a power of 2 */
794 assert( !(page_size
& page_mask
) );
796 while ((1 << page_shift
) != page_size
) page_shift
++;
798 #endif /* page_mask */
801 /***********************************************************************
802 * VIRTUAL_SetFaultHandler
804 BOOL
VIRTUAL_SetFaultHandler( LPCVOID addr
, HANDLERPROC proc
, LPVOID arg
)
808 if (!(view
= VIRTUAL_FindView( addr
))) return FALSE
;
809 view
->handlerProc
= proc
;
810 view
->handlerArg
= arg
;
814 /***********************************************************************
815 * VIRTUAL_HandleFault
817 DWORD
VIRTUAL_HandleFault( LPCVOID addr
)
819 FILE_VIEW
*view
= VIRTUAL_FindView( addr
);
820 DWORD ret
= EXCEPTION_ACCESS_VIOLATION
;
824 if (view
->handlerProc
)
826 if (view
->handlerProc(view
->handlerArg
, addr
)) ret
= 0; /* handled */
830 BYTE vprot
= view
->prot
[((char *)addr
- (char *)view
->base
) >> page_shift
];
831 void *page
= (void *)((UINT_PTR
)addr
& ~page_mask
);
832 char *stack
= (char *)NtCurrentTeb()->stack_base
+ SIGNAL_STACK_SIZE
+ page_mask
+ 1;
833 if (vprot
& VPROT_GUARD
)
835 VIRTUAL_SetProt( view
, page
, page_mask
+ 1, vprot
& ~VPROT_GUARD
);
836 ret
= STATUS_GUARD_PAGE_VIOLATION
;
838 /* is it inside the stack guard pages? */
839 if (((char *)addr
>= stack
) && ((char *)addr
< stack
+ 2*(page_mask
+1)))
840 ret
= STATUS_STACK_OVERFLOW
;
848 /***********************************************************************
851 * Linux kernels before 2.4.x can support non page-aligned offsets, as
852 * long as the offset is aligned to the filesystem block size. This is
853 * a big performance gain so we want to take advantage of it.
855 * However, when we use 64-bit file support this doesn't work because
856 * glibc rejects unaligned offsets. Also glibc 2.1.3 mmap64 is broken
857 * in that it rounds unaligned offsets down to a page boundary. For
858 * these reasons we do a direct system call here.
860 static void *unaligned_mmap( void *addr
, size_t length
, unsigned int prot
,
861 unsigned int flags
, int fd
, unsigned int offset_low
,
862 unsigned int offset_high
)
864 #if defined(linux) && defined(__i386__) && defined(__GNUC__)
865 if (!offset_high
&& (offset_low
& page_mask
))
880 args
.length
= length
;
884 args
.offset
= offset_low
;
886 __asm__
__volatile__("push %%ebx\n\t"
891 : "0" (90), /* SYS_mmap */
893 if (ret
< 0 && ret
> -4096)
901 return mmap( addr
, length
, prot
, flags
, fd
, ((off_t
)offset_high
<< 32) | offset_low
);
905 /***********************************************************************
908 * Wrapper for mmap() that handles anonymous mappings portably,
909 * and falls back to read if mmap of a file fails.
911 static LPVOID
VIRTUAL_mmap( int fd
, LPVOID start
, DWORD size
,
912 DWORD offset_low
, DWORD offset_high
,
913 int prot
, int flags
, BOOL
*removable
)
918 BOOL is_shared_write
= FALSE
;
920 if (fd
== -1) return wine_anon_mmap( start
, size
, prot
, flags
);
922 if (prot
& PROT_WRITE
)
925 if (flags
& MAP_SHARED
) is_shared_write
= TRUE
;
928 if (!(flags
& MAP_PRIVATE
)) is_shared_write
= TRUE
;
932 if (removable
&& *removable
)
934 /* if on removable media, try using read instead of mmap */
935 if (!is_shared_write
) goto fake_mmap
;
939 if ((ret
= unaligned_mmap( start
, size
, prot
, flags
, fd
,
940 offset_low
, offset_high
)) != (LPVOID
)-1) return ret
;
942 /* mmap() failed; if this is because the file offset is not */
943 /* page-aligned (EINVAL), or because the underlying filesystem */
944 /* does not support mmap() (ENOEXEC,ENODEV), we do it by hand. */
946 if ((errno
!= ENOEXEC
) && (errno
!= EINVAL
) && (errno
!= ENODEV
)) return ret
;
947 if (is_shared_write
) return ret
; /* we cannot fake shared write mappings */
950 /* Reserve the memory with an anonymous mmap */
951 ret
= wine_anon_mmap( start
, size
, PROT_READ
| PROT_WRITE
, flags
);
952 if (ret
== (LPVOID
)-1) return ret
;
953 /* Now read in the file */
954 offset
= ((off_t
)offset_high
<< 32) | offset_low
;
955 if ((pos
= lseek( fd
, offset
, SEEK_SET
)) == -1)
960 read( fd
, ret
, size
);
961 lseek( fd
, pos
, SEEK_SET
); /* Restore the file pointer */
962 mprotect( ret
, size
, prot
); /* Set the right protection */
967 /***********************************************************************
968 * NtAllocateVirtualMemory (NTDLL.@)
969 * ZwAllocateVirtualMemory (NTDLL.@)
971 NTSTATUS WINAPI
NtAllocateVirtualMemory( HANDLE process
, PVOID
*ret
, PVOID addr
,
972 ULONG
*size_ptr
, ULONG type
, ULONG protect
)
977 DWORD size
= *size_ptr
;
979 if (!is_current_process( process
))
981 ERR("Unsupported on other process\n");
982 return STATUS_ACCESS_DENIED
;
985 TRACE("%p %08lx %lx %08lx\n", addr
, size
, type
, protect
);
987 /* Round parameters to a page boundary */
989 if (size
> 0x7fc00000) return STATUS_WORKING_SET_LIMIT_RANGE
; /* 2Gb - 4Mb */
993 if (type
& MEM_RESERVE
) /* Round down to 64k boundary */
994 base
= ROUND_ADDR( addr
, granularity_mask
);
996 base
= ROUND_ADDR( addr
, page_mask
);
997 size
= (((UINT_PTR
)addr
+ size
+ page_mask
) & ~page_mask
) - (UINT_PTR
)base
;
999 /* disallow low 64k, wrap-around and kernel space */
1000 if (((char *)base
<= (char *)granularity_mask
) ||
1001 ((char *)base
+ size
< (char *)base
) ||
1002 (ADDRESS_SPACE_LIMIT
&& ((char *)base
+ size
> (char *)ADDRESS_SPACE_LIMIT
)))
1003 return STATUS_INVALID_PARAMETER
;
1008 size
= (size
+ page_mask
) & ~page_mask
;
1011 if (type
& MEM_TOP_DOWN
) {
1012 /* FIXME: MEM_TOP_DOWN allocates the largest possible address.
1013 * Is there _ANY_ way to do it with UNIX mmap()?
1015 WARN("MEM_TOP_DOWN ignored\n");
1016 type
&= ~MEM_TOP_DOWN
;
1019 /* Compute the alloc type flags */
1021 if (!(type
& (MEM_COMMIT
| MEM_RESERVE
| MEM_SYSTEM
)) ||
1022 (type
& ~(MEM_COMMIT
| MEM_RESERVE
| MEM_SYSTEM
)))
1024 ERR("called with wrong alloc type flags (%08lx) !\n", type
);
1025 return STATUS_INVALID_PARAMETER
;
1027 if (type
& (MEM_COMMIT
| MEM_SYSTEM
))
1028 vprot
= VIRTUAL_GetProt( protect
) | VPROT_COMMITTED
;
1031 /* Reserve the memory */
1033 if ((type
& MEM_RESERVE
) || !base
)
1035 if (type
& MEM_SYSTEM
)
1037 if (!(view
= VIRTUAL_CreateView( base
, size
, VFLAG_VALLOC
| VFLAG_SYSTEM
, vprot
, 0 )))
1038 return STATUS_NO_MEMORY
;
1042 NTSTATUS res
= anon_mmap_aligned( &base
, size
, VIRTUAL_GetUnixProt( vprot
), 0 );
1043 if (res
) return res
;
1045 if (!(view
= VIRTUAL_CreateView( base
, size
, VFLAG_VALLOC
, vprot
, 0 )))
1047 munmap( base
, size
);
1048 return STATUS_NO_MEMORY
;
1054 /* Commit the pages */
1056 if (!(view
= VIRTUAL_FindView( base
)) ||
1057 ((char *)base
+ size
> (char *)view
->base
+ view
->size
)) return STATUS_NOT_MAPPED_VIEW
;
1059 if (!VIRTUAL_SetProt( view
, base
, size
, vprot
)) return STATUS_ACCESS_DENIED
;
1064 return STATUS_SUCCESS
;
1068 /***********************************************************************
1069 * NtFreeVirtualMemory (NTDLL.@)
1070 * ZwFreeVirtualMemory (NTDLL.@)
1072 NTSTATUS WINAPI
NtFreeVirtualMemory( HANDLE process
, PVOID
*addr_ptr
, ULONG
*size_ptr
, ULONG type
)
1076 LPVOID addr
= *addr_ptr
;
1077 DWORD size
= *size_ptr
;
1079 if (!is_current_process( process
))
1081 ERR("Unsupported on other process\n");
1082 return STATUS_ACCESS_DENIED
;
1085 TRACE("%p %08lx %lx\n", addr
, size
, type
);
1087 /* Fix the parameters */
1089 size
= ROUND_SIZE( addr
, size
);
1090 base
= ROUND_ADDR( addr
, page_mask
);
1092 if (!(view
= VIRTUAL_FindView( base
)) ||
1093 (base
+ size
> (char *)view
->base
+ view
->size
) ||
1094 !(view
->flags
& VFLAG_VALLOC
))
1095 return STATUS_INVALID_PARAMETER
;
1097 /* Check the type */
1099 if (type
& MEM_SYSTEM
)
1101 view
->flags
|= VFLAG_SYSTEM
;
1102 type
&= ~MEM_SYSTEM
;
1105 if ((type
!= MEM_DECOMMIT
) && (type
!= MEM_RELEASE
))
1107 ERR("called with wrong free type flags (%08lx) !\n", type
);
1108 return STATUS_INVALID_PARAMETER
;
1111 /* Free the pages */
1113 if (type
== MEM_RELEASE
)
1115 if (size
|| (base
!= view
->base
)) return STATUS_INVALID_PARAMETER
;
1116 VIRTUAL_DeleteView( view
);
1120 /* Decommit the pages by remapping zero-pages instead */
1122 if (wine_anon_mmap( (LPVOID
)base
, size
, VIRTUAL_GetUnixProt(0), MAP_FIXED
) != (LPVOID
)base
)
1123 ERR( "Could not remap pages, expect trouble\n" );
1124 if (!VIRTUAL_SetProt( view
, base
, size
, 0 )) return STATUS_ACCESS_DENIED
; /* FIXME */
1129 return STATUS_SUCCESS
;
1133 /***********************************************************************
1134 * NtProtectVirtualMemory (NTDLL.@)
1135 * ZwProtectVirtualMemory (NTDLL.@)
1137 NTSTATUS WINAPI
NtProtectVirtualMemory( HANDLE process
, PVOID
*addr_ptr
, ULONG
*size_ptr
,
1138 ULONG new_prot
, ULONG
*old_prot
)
1144 DWORD prot
, size
= *size_ptr
;
1145 LPVOID addr
= *addr_ptr
;
1147 if (!is_current_process( process
))
1149 ERR("Unsupported on other process\n");
1150 return STATUS_ACCESS_DENIED
;
1153 TRACE("%p %08lx %08lx\n", addr
, size
, new_prot
);
1155 /* Fix the parameters */
1157 size
= ROUND_SIZE( addr
, size
);
1158 base
= ROUND_ADDR( addr
, page_mask
);
1160 if (!(view
= VIRTUAL_FindView( base
)) ||
1161 (base
+ size
> (char *)view
->base
+ view
->size
))
1162 return STATUS_INVALID_PARAMETER
;
1164 /* Make sure all the pages are committed */
1166 p
= view
->prot
+ ((base
- (char *)view
->base
) >> page_shift
);
1167 VIRTUAL_GetWin32Prot( *p
, &prot
, NULL
);
1168 for (i
= size
>> page_shift
; i
; i
--, p
++)
1170 if (!(*p
& VPROT_COMMITTED
)) return STATUS_INVALID_PARAMETER
;
1173 if (old_prot
) *old_prot
= prot
;
1174 vprot
= VIRTUAL_GetProt( new_prot
) | VPROT_COMMITTED
;
1175 if (!VIRTUAL_SetProt( view
, base
, size
, vprot
)) return STATUS_ACCESS_DENIED
;
1179 return STATUS_SUCCESS
;
1183 /***********************************************************************
1184 * NtQueryVirtualMemory (NTDLL.@)
1185 * ZwQueryVirtualMemory (NTDLL.@)
1187 NTSTATUS WINAPI
NtQueryVirtualMemory( HANDLE process
, LPCVOID addr
,
1188 MEMORY_INFORMATION_CLASS info_class
, PVOID buffer
,
1189 ULONG len
, ULONG
*res_len
)
1192 char *base
, *alloc_base
= 0;
1194 MEMORY_BASIC_INFORMATION
*info
= buffer
;
1196 if (info_class
!= MemoryBasicInformation
) return STATUS_INVALID_INFO_CLASS
;
1197 if (ADDRESS_SPACE_LIMIT
&& addr
>= ADDRESS_SPACE_LIMIT
)
1198 return STATUS_WORKING_SET_LIMIT_RANGE
; /* FIXME */
1200 if (!is_current_process( process
))
1202 ERR("Unsupported on other process\n");
1203 return STATUS_ACCESS_DENIED
;
1206 base
= ROUND_ADDR( addr
, page_mask
);
1208 /* Find the view containing the address */
1210 RtlEnterCriticalSection(&csVirtual
);
1211 view
= VIRTUAL_FirstView
;
1216 size
= (char *)ADDRESS_SPACE_LIMIT
- alloc_base
;
1219 if ((char *)view
->base
> base
)
1221 size
= (char *)view
->base
- alloc_base
;
1225 if ((char *)view
->base
+ view
->size
> base
)
1227 alloc_base
= view
->base
;
1231 alloc_base
= (char *)view
->base
+ view
->size
;
1234 RtlLeaveCriticalSection(&csVirtual
);
1236 /* Fill the info structure */
1240 info
->State
= MEM_FREE
;
1242 info
->AllocationProtect
= 0;
1247 BYTE vprot
= view
->prot
[(base
- alloc_base
) >> page_shift
];
1248 VIRTUAL_GetWin32Prot( vprot
, &info
->Protect
, &info
->State
);
1249 for (size
= base
- alloc_base
; size
< view
->size
; size
+= page_mask
+1)
1250 if (view
->prot
[size
>> page_shift
] != vprot
) break;
1251 info
->AllocationProtect
= view
->protect
;
1252 info
->Type
= MEM_PRIVATE
; /* FIXME */
1255 info
->BaseAddress
= (LPVOID
)base
;
1256 info
->AllocationBase
= (LPVOID
)alloc_base
;
1257 info
->RegionSize
= size
- (base
- alloc_base
);
1258 if (res_len
) *res_len
= sizeof(*info
);
1259 return STATUS_SUCCESS
;
1263 /***********************************************************************
1264 * NtLockVirtualMemory (NTDLL.@)
1265 * ZwLockVirtualMemory (NTDLL.@)
1267 NTSTATUS WINAPI
NtLockVirtualMemory( HANDLE process
, PVOID
*addr
, ULONG
*size
, ULONG unknown
)
1269 if (!is_current_process( process
))
1271 ERR("Unsupported on other process\n");
1272 return STATUS_ACCESS_DENIED
;
1274 return STATUS_SUCCESS
;
1278 /***********************************************************************
1279 * NtUnlockVirtualMemory (NTDLL.@)
1280 * ZwUnlockVirtualMemory (NTDLL.@)
1282 NTSTATUS WINAPI
NtUnlockVirtualMemory( HANDLE process
, PVOID
*addr
, ULONG
*size
, ULONG unknown
)
1284 if (!is_current_process( process
))
1286 ERR("Unsupported on other process\n");
1287 return STATUS_ACCESS_DENIED
;
1289 return STATUS_SUCCESS
;
1293 /***********************************************************************
1294 * NtCreateSection (NTDLL.@)
1295 * ZwCreateSection (NTDLL.@)
1297 NTSTATUS WINAPI
NtCreateSection( HANDLE
*handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
,
1298 const LARGE_INTEGER
*size
, ULONG protect
,
1299 ULONG sec_flags
, HANDLE file
)
1303 DWORD len
= attr
->ObjectName
? attr
->ObjectName
->Length
: 0;
1305 /* Check parameters */
1307 if (len
> MAX_PATH
*sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
1309 vprot
= VIRTUAL_GetProt( protect
);
1310 if (sec_flags
& SEC_RESERVE
)
1312 if (file
) return STATUS_INVALID_PARAMETER
;
1314 else vprot
|= VPROT_COMMITTED
;
1315 if (sec_flags
& SEC_NOCACHE
) vprot
|= VPROT_NOCACHE
;
1316 if (sec_flags
& SEC_IMAGE
) vprot
|= VPROT_IMAGE
;
1318 /* Create the server object */
1320 SERVER_START_REQ( create_mapping
)
1322 req
->file_handle
= file
;
1323 req
->size_high
= size
? size
->s
.HighPart
: 0;
1324 req
->size_low
= size
? size
->s
.LowPart
: 0;
1325 req
->protect
= vprot
;
1326 req
->access
= access
;
1327 req
->inherit
= (attr
->Attributes
& OBJ_INHERIT
) != 0;
1328 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
1329 ret
= wine_server_call( req
);
1330 *handle
= reply
->handle
;
1337 /***********************************************************************
1338 * NtOpenSection (NTDLL.@)
1339 * ZwOpenSection (NTDLL.@)
1341 NTSTATUS WINAPI
NtOpenSection( HANDLE
*handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
)
1344 DWORD len
= attr
->ObjectName
->Length
;
1346 if (len
> MAX_PATH
*sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
1348 SERVER_START_REQ( open_mapping
)
1350 req
->access
= access
;
1351 req
->inherit
= (attr
->Attributes
& OBJ_INHERIT
) != 0;
1352 wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
1353 if (!(ret
= wine_server_call( req
))) *handle
= reply
->handle
;
1360 /***********************************************************************
1361 * NtMapViewOfSection (NTDLL.@)
1362 * ZwMapViewOfSection (NTDLL.@)
1364 NTSTATUS WINAPI
NtMapViewOfSection( HANDLE handle
, HANDLE process
, PVOID
*addr_ptr
, ULONG zero_bits
,
1365 ULONG commit_size
, const LARGE_INTEGER
*offset
, ULONG
*size_ptr
,
1366 SECTION_INHERIT inherit
, ULONG alloc_type
, ULONG protect
)
1371 int flags
= MAP_PRIVATE
;
1372 int unix_handle
= -1;
1374 void *base
, *ptr
= (void *)-1, *ret
;
1375 DWORD size_low
, size_high
, header_size
, shared_size
;
1379 if (!is_current_process( process
))
1381 ERR("Unsupported on other process\n");
1382 return STATUS_ACCESS_DENIED
;
1385 TRACE("handle=%p addr=%p off=%lx%08lx size=%x access=%lx\n",
1386 handle
, *addr_ptr
, offset
->s
.HighPart
, offset
->s
.LowPart
, size
, protect
);
1388 /* Check parameters */
1390 if ((offset
->s
.LowPart
& granularity_mask
) ||
1391 (*addr_ptr
&& ((UINT_PTR
)*addr_ptr
& granularity_mask
)))
1392 return STATUS_INVALID_PARAMETER
;
1394 SERVER_START_REQ( get_mapping_info
)
1396 req
->handle
= handle
;
1397 res
= wine_server_call( req
);
1398 prot
= reply
->protect
;
1400 size_low
= reply
->size_low
;
1401 size_high
= reply
->size_high
;
1402 header_size
= reply
->header_size
;
1403 shared_file
= reply
->shared_file
;
1404 shared_size
= reply
->shared_size
;
1405 removable
= (reply
->drive_type
== DRIVE_REMOVABLE
||
1406 reply
->drive_type
== DRIVE_CDROM
);
1409 if (res
) goto error
;
1411 if ((res
= wine_server_handle_to_fd( handle
, 0, &unix_handle
, NULL
, NULL
))) goto error
;
1413 if (prot
& VPROT_IMAGE
)
1419 if ((res
= wine_server_handle_to_fd( shared_file
, GENERIC_READ
, &shared_fd
,
1420 NULL
, NULL
))) goto error
;
1421 NtClose( shared_file
); /* we no longer need it */
1423 res
= map_image( handle
, unix_handle
, base
, size_low
, header_size
,
1424 shared_fd
, shared_size
, removable
, addr_ptr
);
1425 if (shared_fd
!= -1) close( shared_fd
);
1426 if (!res
) *size_ptr
= size_low
;
1432 ERR("Sizes larger than 4Gb not supported\n");
1434 if ((offset
->s
.LowPart
>= size_low
) ||
1435 (*size_ptr
> size_low
- offset
->s
.LowPart
))
1437 res
= STATUS_INVALID_PARAMETER
;
1440 if (*size_ptr
) size
= ROUND_SIZE( offset
->s
.LowPart
, *size_ptr
);
1441 else size
= size_low
- offset
->s
.LowPart
;
1447 case PAGE_READWRITE
:
1448 case PAGE_EXECUTE_READWRITE
:
1449 if (!(prot
& VPROT_WRITE
))
1451 res
= STATUS_INVALID_PARAMETER
;
1457 case PAGE_WRITECOPY
:
1459 case PAGE_EXECUTE_READ
:
1460 case PAGE_EXECUTE_WRITECOPY
:
1461 if (prot
& VPROT_READ
) break;
1464 res
= STATUS_INVALID_PARAMETER
;
1468 /* FIXME: If a mapping is created with SEC_RESERVE and a process,
1469 * which has a view of this mapping commits some pages, they will
1470 * appear commited in all other processes, which have the same
1471 * view created. Since we don`t support this yet, we create the
1472 * whole mapping commited.
1474 prot
|= VPROT_COMMITTED
;
1476 /* Reserve a properly aligned area */
1478 if ((res
= anon_mmap_aligned( addr_ptr
, size
, PROT_NONE
, 0 ))) goto error
;
1483 TRACE("handle=%p size=%x offset=%lx\n", handle
, size
, offset
->s
.LowPart
);
1485 ret
= VIRTUAL_mmap( unix_handle
, ptr
, size
, offset
->s
.LowPart
, offset
->s
.HighPart
,
1486 VIRTUAL_GetUnixProt( prot
), flags
| MAP_FIXED
, &removable
);
1489 ERR( "VIRTUAL_mmap %p %x %lx%08lx failed\n",
1490 ptr
, size
, offset
->s
.HighPart
, offset
->s
.LowPart
);
1491 res
= STATUS_NO_MEMORY
; /* FIXME */
1494 if (removable
) handle
= 0; /* don't keep handle open on removable media */
1496 if (!(view
= VIRTUAL_CreateView( ptr
, size
, 0, prot
, handle
)))
1498 res
= STATUS_NO_MEMORY
;
1501 if (unix_handle
!= -1) close( unix_handle
);
1503 return STATUS_SUCCESS
;
1506 if (unix_handle
!= -1) close( unix_handle
);
1507 if (ptr
!= (void *)-1) munmap( ptr
, size
);
1512 /***********************************************************************
1513 * NtUnmapViewOfSection (NTDLL.@)
1514 * ZwUnmapViewOfSection (NTDLL.@)
1516 NTSTATUS WINAPI
NtUnmapViewOfSection( HANDLE process
, PVOID addr
)
1519 void *base
= ROUND_ADDR( addr
, page_mask
);
1521 if (!is_current_process( process
))
1523 ERR("Unsupported on other process\n");
1524 return STATUS_ACCESS_DENIED
;
1526 if (!(view
= VIRTUAL_FindView( base
)) || (base
!= view
->base
)) return STATUS_INVALID_PARAMETER
;
1527 VIRTUAL_DeleteView( view
);
1528 return STATUS_SUCCESS
;
1532 /***********************************************************************
1533 * NtFlushVirtualMemory (NTDLL.@)
1534 * ZwFlushVirtualMemory (NTDLL.@)
1536 NTSTATUS WINAPI
NtFlushVirtualMemory( HANDLE process
, LPCVOID
*addr_ptr
,
1537 ULONG
*size_ptr
, ULONG unknown
)
1540 void *addr
= ROUND_ADDR( *addr_ptr
, page_mask
);
1542 if (!is_current_process( process
))
1544 ERR("Unsupported on other process\n");
1545 return STATUS_ACCESS_DENIED
;
1547 if (!(view
= VIRTUAL_FindView( addr
))) return STATUS_INVALID_PARAMETER
;
1548 if (!*size_ptr
) *size_ptr
= view
->size
;
1550 if (!msync( addr
, *size_ptr
, MS_SYNC
)) return STATUS_SUCCESS
;
1551 return STATUS_NOT_MAPPED_DATA
;
1555 /***********************************************************************
1556 * NtReadVirtualMemory (NTDLL.@)
1557 * ZwReadVirtualMemory (NTDLL.@)
1559 NTSTATUS WINAPI
NtReadVirtualMemory( HANDLE process
, const void *addr
, void *buffer
,
1560 SIZE_T size
, SIZE_T
*bytes_read
)
1564 SERVER_START_REQ( read_process_memory
)
1566 req
->handle
= process
;
1567 req
->addr
= (void *)addr
;
1568 wine_server_set_reply( req
, buffer
, size
);
1569 if ((status
= wine_server_call( req
))) size
= 0;
1572 if (bytes_read
) *bytes_read
= size
;
1577 /***********************************************************************
1578 * NtWriteVirtualMemory (NTDLL.@)
1579 * ZwWriteVirtualMemory (NTDLL.@)
1581 NTSTATUS WINAPI
NtWriteVirtualMemory( HANDLE process
, void *addr
, const void *buffer
,
1582 SIZE_T size
, SIZE_T
*bytes_written
)
1584 static const unsigned int zero
;
1585 unsigned int first_offset
, last_offset
, first_mask
, last_mask
;
1588 if (!size
) return STATUS_INVALID_PARAMETER
;
1590 /* compute the mask for the first int */
1592 first_offset
= (unsigned int)addr
% sizeof(int);
1593 memset( &first_mask
, 0, first_offset
);
1595 /* compute the mask for the last int */
1596 last_offset
= (size
+ first_offset
) % sizeof(int);
1598 memset( &last_mask
, 0xff, last_offset
? last_offset
: sizeof(int) );
1600 SERVER_START_REQ( write_process_memory
)
1602 req
->handle
= process
;
1603 req
->addr
= (char *)addr
- first_offset
;
1604 req
->first_mask
= first_mask
;
1605 req
->last_mask
= last_mask
;
1606 if (first_offset
) wine_server_add_data( req
, &zero
, first_offset
);
1607 wine_server_add_data( req
, buffer
, size
);
1608 if (last_offset
) wine_server_add_data( req
, &zero
, sizeof(int) - last_offset
);
1610 if ((status
= wine_server_call( req
))) size
= 0;
1613 if (bytes_written
) *bytes_written
= size
;