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 /***********************************************************************
243 * Create a new view and add it in the linked list.
245 static FILE_VIEW
*VIRTUAL_CreateView( void *base
, UINT size
, UINT flags
,
246 BYTE vprot
, HANDLE mapping
)
248 FILE_VIEW
*view
, *prev
;
250 /* Create the view structure */
252 assert( !((unsigned int)base
& page_mask
) );
253 assert( !(size
& page_mask
) );
255 if (!(view
= (FILE_VIEW
*)malloc( sizeof(*view
) + size
- 1 ))) return NULL
;
257 view
->size
= size
<< page_shift
;
259 view
->mapping
= mapping
;
260 view
->protect
= vprot
;
261 view
->handlerProc
= NULL
;
262 memset( view
->prot
, vprot
, size
);
264 /* Duplicate the mapping handle */
267 NtDuplicateObject( GetCurrentProcess(), view
->mapping
,
268 GetCurrentProcess(), &view
->mapping
,
269 0, 0, DUPLICATE_SAME_ACCESS
))
275 /* Insert it in the linked list */
277 RtlEnterCriticalSection(&csVirtual
);
278 if (!VIRTUAL_FirstView
|| (VIRTUAL_FirstView
->base
> base
))
280 view
->next
= VIRTUAL_FirstView
;
282 if (view
->next
) view
->next
->prev
= view
;
283 VIRTUAL_FirstView
= view
;
287 prev
= VIRTUAL_FirstView
;
288 while (prev
->next
&& (prev
->next
->base
< base
)) prev
= prev
->next
;
289 view
->next
= prev
->next
;
291 if (view
->next
) view
->next
->prev
= view
;
294 RtlLeaveCriticalSection(&csVirtual
);
295 VIRTUAL_DEBUG_DUMP_VIEW( view
);
300 /***********************************************************************
307 static void VIRTUAL_DeleteView( FILE_VIEW
*view
) /* [in] View */
309 if (!(view
->flags
& VFLAG_SYSTEM
))
310 munmap( (void *)view
->base
, view
->size
);
311 RtlEnterCriticalSection(&csVirtual
);
312 if (view
->next
) view
->next
->prev
= view
->prev
;
313 if (view
->prev
) view
->prev
->next
= view
->next
;
314 else VIRTUAL_FirstView
= view
->next
;
315 RtlLeaveCriticalSection(&csVirtual
);
316 if (view
->mapping
) NtClose( view
->mapping
);
321 /***********************************************************************
322 * VIRTUAL_GetUnixProt
324 * Convert page protections to protection for mmap/mprotect.
326 static int VIRTUAL_GetUnixProt( BYTE vprot
)
329 if ((vprot
& VPROT_COMMITTED
) && !(vprot
& VPROT_GUARD
))
331 if (vprot
& VPROT_READ
) prot
|= PROT_READ
;
332 if (vprot
& VPROT_WRITE
) prot
|= PROT_WRITE
;
333 if (vprot
& VPROT_WRITECOPY
) prot
|= PROT_WRITE
;
334 if (vprot
& VPROT_EXEC
) prot
|= PROT_EXEC
;
340 /***********************************************************************
341 * VIRTUAL_GetWin32Prot
343 * Convert page protections to Win32 flags.
348 static void VIRTUAL_GetWin32Prot(
349 BYTE vprot
, /* [in] Page protection flags */
350 DWORD
*protect
, /* [out] Location to store Win32 protection flags */
351 DWORD
*state
) /* [out] Location to store mem state flag */
354 *protect
= VIRTUAL_Win32Flags
[vprot
& 0x0f];
355 /* if (vprot & VPROT_GUARD) *protect |= PAGE_GUARD;*/
356 if (vprot
& VPROT_NOCACHE
) *protect
|= PAGE_NOCACHE
;
358 if (vprot
& VPROT_GUARD
) *protect
= PAGE_NOACCESS
;
361 if (state
) *state
= (vprot
& VPROT_COMMITTED
) ? MEM_COMMIT
: MEM_RESERVE
;
365 /***********************************************************************
368 * Build page protections from Win32 flags.
371 * Value of page protection flags
373 static BYTE
VIRTUAL_GetProt( DWORD protect
) /* [in] Win32 protection flags */
377 switch(protect
& 0xff)
383 vprot
= VPROT_READ
| VPROT_WRITE
;
386 /* MSDN CreateFileMapping() states that if PAGE_WRITECOPY is given,
387 * that the hFile must have been opened with GENERIC_READ and
388 * GENERIC_WRITE access. This is WRONG as tests show that you
389 * only need GENERIC_READ access (at least for Win9x,
390 * FIXME: what about NT?). Thus, we don't put VPROT_WRITE in
391 * PAGE_WRITECOPY and PAGE_EXECUTE_WRITECOPY.
393 vprot
= VPROT_READ
| VPROT_WRITECOPY
;
398 case PAGE_EXECUTE_READ
:
399 vprot
= VPROT_EXEC
| VPROT_READ
;
401 case PAGE_EXECUTE_READWRITE
:
402 vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITE
;
404 case PAGE_EXECUTE_WRITECOPY
:
405 /* See comment for PAGE_WRITECOPY above */
406 vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITECOPY
;
413 if (protect
& PAGE_GUARD
) vprot
|= VPROT_GUARD
;
414 if (protect
& PAGE_NOCACHE
) vprot
|= VPROT_NOCACHE
;
419 /***********************************************************************
422 * Change the protection of a range of pages.
428 static BOOL
VIRTUAL_SetProt( FILE_VIEW
*view
, /* [in] Pointer to view */
429 void *base
, /* [in] Starting address */
430 UINT size
, /* [in] Size in bytes */
431 BYTE vprot
) /* [in] Protections to use */
434 base
, (char *)base
+ size
- 1, VIRTUAL_GetProtStr( vprot
) );
436 if (mprotect( base
, size
, VIRTUAL_GetUnixProt(vprot
) ))
437 return FALSE
; /* FIXME: last error */
439 memset( view
->prot
+ (((char *)base
- (char *)view
->base
) >> page_shift
),
440 vprot
, size
>> page_shift
);
441 VIRTUAL_DEBUG_DUMP_VIEW( view
);
446 /***********************************************************************
449 * Create an anonymous mapping aligned to the allocation granularity.
451 static NTSTATUS
anon_mmap_aligned( void **addr
, unsigned int size
, int prot
, int flags
)
453 void *ptr
, *base
= *addr
;
454 unsigned int view_size
= size
+ (base
? 0 : granularity_mask
+ 1);
456 if ((ptr
= wine_anon_mmap( base
, view_size
, prot
, flags
)) == (void *)-1)
458 if (errno
== ENOMEM
) return STATUS_NO_MEMORY
;
459 return STATUS_INVALID_PARAMETER
;
464 /* Release the extra memory while keeping the range
465 * starting on the granularity boundary. */
466 if ((unsigned int)ptr
& granularity_mask
)
468 unsigned int extra
= granularity_mask
+ 1 - ((unsigned int)ptr
& granularity_mask
);
469 munmap( ptr
, extra
);
470 ptr
= (char *)ptr
+ extra
;
473 if (view_size
> size
)
474 munmap( (char *)ptr
+ size
, view_size
- size
);
476 else if (ptr
!= base
)
478 /* We couldn't get the address we wanted */
479 munmap( ptr
, view_size
);
480 return STATUS_CONFLICTING_ADDRESSES
;
483 return STATUS_SUCCESS
;
487 /***********************************************************************
490 * Apply the relocations to a mapped PE image
492 static int do_relocations( char *base
, const IMAGE_DATA_DIRECTORY
*dir
,
493 int delta
, DWORD total_size
)
495 IMAGE_BASE_RELOCATION
*rel
;
497 TRACE_(module
)( "relocating from %p-%p to %p-%p\n",
498 base
- delta
, base
- delta
+ total_size
, base
, base
+ total_size
);
500 for (rel
= (IMAGE_BASE_RELOCATION
*)(base
+ dir
->VirtualAddress
);
501 ((char *)rel
< base
+ dir
->VirtualAddress
+ dir
->Size
) && rel
->SizeOfBlock
;
502 rel
= (IMAGE_BASE_RELOCATION
*)((char*)rel
+ rel
->SizeOfBlock
) )
504 char *page
= base
+ rel
->VirtualAddress
;
505 WORD
*TypeOffset
= (WORD
*)(rel
+ 1);
506 int i
, count
= (rel
->SizeOfBlock
- sizeof(*rel
)) / sizeof(*TypeOffset
);
508 if (!count
) continue;
511 if ((char *)rel
+ rel
->SizeOfBlock
> base
+ dir
->VirtualAddress
+ dir
->Size
||
512 page
> base
+ total_size
)
514 ERR_(module
)("invalid relocation %p,%lx,%ld at %p,%lx,%lx\n",
515 rel
, rel
->VirtualAddress
, rel
->SizeOfBlock
,
516 base
, dir
->VirtualAddress
, dir
->Size
);
520 TRACE_(module
)("%ld relocations for page %lx\n", rel
->SizeOfBlock
, rel
->VirtualAddress
);
522 /* patching in reverse order */
523 for (i
= 0 ; i
< count
; i
++)
525 int offset
= TypeOffset
[i
] & 0xFFF;
526 int type
= TypeOffset
[i
] >> 12;
529 case IMAGE_REL_BASED_ABSOLUTE
:
531 case IMAGE_REL_BASED_HIGH
:
532 *(short*)(page
+offset
) += HIWORD(delta
);
534 case IMAGE_REL_BASED_LOW
:
535 *(short*)(page
+offset
) += LOWORD(delta
);
537 case IMAGE_REL_BASED_HIGHLOW
:
538 *(int*)(page
+offset
) += delta
;
539 /* FIXME: if this is an exported address, fire up enhanced logic */
542 FIXME_(module
)("Unknown/unsupported fixup type %d.\n", type
);
551 /***********************************************************************
554 * Map an executable (PE format) image into memory.
556 static NTSTATUS
map_image( HANDLE hmapping
, int fd
, char *base
, DWORD total_size
,
557 DWORD header_size
, int shared_fd
, BOOL removable
, PVOID
*addr_ptr
)
559 IMAGE_DOS_HEADER
*dos
;
560 IMAGE_NT_HEADERS
*nt
;
561 IMAGE_SECTION_HEADER
*sec
;
562 IMAGE_DATA_DIRECTORY
*imports
;
563 NTSTATUS status
= STATUS_INVALID_IMAGE_FORMAT
; /* generic error (FIXME) */
568 /* zero-map the whole range */
570 if (base
< (char *)0x110000 || /* make sure the DOS area remains free */
571 (ptr
= wine_anon_mmap( base
, total_size
,
572 PROT_READ
| PROT_WRITE
| PROT_EXEC
, 0 )) == (char *)-1)
574 ptr
= wine_anon_mmap( NULL
, total_size
,
575 PROT_READ
| PROT_WRITE
| PROT_EXEC
, 0 );
576 if (ptr
== (char *)-1)
578 ERR_(module
)("Not enough memory for module (%ld bytes)\n", total_size
);
582 TRACE_(module
)( "mapped PE file at %p-%p\n", ptr
, ptr
+ total_size
);
586 if (VIRTUAL_mmap( fd
, ptr
, header_size
, 0, 0, PROT_READ
,
587 MAP_PRIVATE
| MAP_FIXED
, &removable
) == (char *)-1) goto error
;
588 dos
= (IMAGE_DOS_HEADER
*)ptr
;
589 nt
= (IMAGE_NT_HEADERS
*)(ptr
+ dos
->e_lfanew
);
590 if ((char *)(nt
+ 1) > ptr
+ header_size
) goto error
;
592 sec
= (IMAGE_SECTION_HEADER
*)((char*)&nt
->OptionalHeader
+nt
->FileHeader
.SizeOfOptionalHeader
);
593 if ((char *)(sec
+ nt
->FileHeader
.NumberOfSections
) > ptr
+ header_size
) goto error
;
595 imports
= nt
->OptionalHeader
.DataDirectory
+ IMAGE_DIRECTORY_ENTRY_IMPORT
;
596 if (!imports
->Size
|| !imports
->VirtualAddress
) imports
= NULL
;
598 /* check the architecture */
600 if (nt
->FileHeader
.Machine
!= IMAGE_FILE_MACHINE_I386
)
602 MESSAGE("Trying to load PE image for unsupported architecture (");
603 switch (nt
->FileHeader
.Machine
)
605 case IMAGE_FILE_MACHINE_UNKNOWN
: MESSAGE("Unknown"); break;
606 case IMAGE_FILE_MACHINE_I860
: MESSAGE("I860"); break;
607 case IMAGE_FILE_MACHINE_R3000
: MESSAGE("R3000"); break;
608 case IMAGE_FILE_MACHINE_R4000
: MESSAGE("R4000"); break;
609 case IMAGE_FILE_MACHINE_R10000
: MESSAGE("R10000"); break;
610 case IMAGE_FILE_MACHINE_ALPHA
: MESSAGE("Alpha"); break;
611 case IMAGE_FILE_MACHINE_POWERPC
: MESSAGE("PowerPC"); break;
612 default: MESSAGE("Unknown-%04x", nt
->FileHeader
.Machine
); break;
618 /* map all the sections */
620 for (i
= pos
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
624 /* a few sanity checks */
625 size
= sec
->VirtualAddress
+ ROUND_SIZE( sec
->VirtualAddress
, sec
->Misc
.VirtualSize
);
626 if (sec
->VirtualAddress
> total_size
|| size
> total_size
|| size
< sec
->VirtualAddress
)
628 ERR_(module
)( "Section %.8s too large (%lx+%lx/%lx)\n",
629 sec
->Name
, sec
->VirtualAddress
, sec
->Misc
.VirtualSize
, total_size
);
633 if ((sec
->Characteristics
& IMAGE_SCN_MEM_SHARED
) &&
634 (sec
->Characteristics
& IMAGE_SCN_MEM_WRITE
))
636 size
= ROUND_SIZE( 0, sec
->Misc
.VirtualSize
);
637 TRACE_(module
)( "mapping shared section %.8s at %p off %lx (%x) size %lx (%lx) flags %lx\n",
638 sec
->Name
, ptr
+ sec
->VirtualAddress
,
639 sec
->PointerToRawData
, pos
, sec
->SizeOfRawData
,
640 size
, sec
->Characteristics
);
641 if (VIRTUAL_mmap( shared_fd
, ptr
+ sec
->VirtualAddress
, size
,
642 pos
, 0, PROT_READ
|PROT_WRITE
|PROT_EXEC
,
643 MAP_SHARED
|MAP_FIXED
, NULL
) == (void *)-1)
645 ERR_(module
)( "Could not map shared section %.8s\n", sec
->Name
);
649 /* check if the import directory falls inside this section */
650 if (imports
&& imports
->VirtualAddress
>= sec
->VirtualAddress
&&
651 imports
->VirtualAddress
< sec
->VirtualAddress
+ size
)
653 UINT_PTR base
= imports
->VirtualAddress
& ~page_mask
;
654 UINT_PTR end
= base
+ ROUND_SIZE( imports
->VirtualAddress
, imports
->Size
);
655 if (end
> sec
->VirtualAddress
+ size
) end
= sec
->VirtualAddress
+ size
;
656 if (end
> base
) VIRTUAL_mmap( shared_fd
, ptr
+ base
, end
- base
,
657 pos
+ (base
- sec
->VirtualAddress
), 0,
658 PROT_READ
|PROT_WRITE
|PROT_EXEC
,
659 MAP_PRIVATE
|MAP_FIXED
, NULL
);
665 TRACE_(module
)( "mapping section %.8s at %p off %lx size %lx flags %lx\n",
666 sec
->Name
, ptr
+ sec
->VirtualAddress
,
667 sec
->PointerToRawData
, sec
->SizeOfRawData
,
668 sec
->Characteristics
);
670 if ((sec
->Characteristics
& IMAGE_SCN_CNT_UNINITIALIZED_DATA
) &&
671 !(sec
->Characteristics
& IMAGE_SCN_CNT_INITIALIZED_DATA
)) continue;
672 if (!sec
->PointerToRawData
|| !sec
->SizeOfRawData
) continue;
674 /* Note: if the section is not aligned properly VIRTUAL_mmap will magically
675 * fall back to read(), so we don't need to check anything here.
677 if (VIRTUAL_mmap( fd
, ptr
+ sec
->VirtualAddress
, sec
->SizeOfRawData
,
678 sec
->PointerToRawData
, 0, PROT_READ
|PROT_WRITE
|PROT_EXEC
,
679 MAP_PRIVATE
| MAP_FIXED
, &removable
) == (void *)-1)
681 ERR_(module
)( "Could not map section %.8s, file probably truncated\n", sec
->Name
);
685 if ((sec
->SizeOfRawData
< sec
->Misc
.VirtualSize
) && (sec
->SizeOfRawData
& page_mask
))
687 DWORD end
= ROUND_SIZE( 0, sec
->SizeOfRawData
);
688 if (end
> sec
->Misc
.VirtualSize
) end
= sec
->Misc
.VirtualSize
;
689 TRACE_(module
)("clearing %p - %p\n",
690 ptr
+ sec
->VirtualAddress
+ sec
->SizeOfRawData
,
691 ptr
+ sec
->VirtualAddress
+ end
);
692 memset( ptr
+ sec
->VirtualAddress
+ sec
->SizeOfRawData
, 0,
693 end
- sec
->SizeOfRawData
);
698 /* perform base relocation, if necessary */
702 const IMAGE_DATA_DIRECTORY
*relocs
;
704 relocs
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
];
705 if (!relocs
->VirtualAddress
|| !relocs
->Size
)
707 if (nt
->OptionalHeader
.ImageBase
== 0x400000)
708 ERR("Standard load address for a Win32 program (0x00400000) not available - security-patched kernel ?\n");
710 ERR( "FATAL: Need to relocate module from addr %lx, but there are no relocation records\n",
711 nt
->OptionalHeader
.ImageBase
);
715 /* FIXME: If we need to relocate a system DLL (base > 2GB) we should
716 * really make sure that the *new* base address is also > 2GB.
717 * Some DLLs really check the MSB of the module handle :-/
719 if ((nt
->OptionalHeader
.ImageBase
& 0x80000000) && !((DWORD
)base
& 0x80000000))
720 ERR( "Forced to relocate system DLL (base > 2GB). This is not good.\n" );
722 if (!do_relocations( ptr
, relocs
, ptr
- base
, total_size
))
728 if (removable
) hmapping
= 0; /* don't keep handle open on removable media */
729 if (!(view
= VIRTUAL_CreateView( ptr
, total_size
, 0,
730 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITE
|
731 VPROT_EXEC
| VPROT_WRITECOPY
| VPROT_IMAGE
, hmapping
)))
733 status
= STATUS_NO_MEMORY
;
737 /* set the image protections */
739 VIRTUAL_SetProt( view
, ptr
, header_size
, VPROT_COMMITTED
| VPROT_READ
);
740 sec
= (IMAGE_SECTION_HEADER
*)((char *)&nt
->OptionalHeader
+nt
->FileHeader
.SizeOfOptionalHeader
);
741 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
743 DWORD size
= ROUND_SIZE( sec
->VirtualAddress
, sec
->Misc
.VirtualSize
);
744 BYTE vprot
= VPROT_COMMITTED
;
745 if (sec
->Characteristics
& IMAGE_SCN_MEM_READ
) vprot
|= VPROT_READ
;
746 if (sec
->Characteristics
& IMAGE_SCN_MEM_WRITE
) vprot
|= VPROT_WRITE
|VPROT_WRITECOPY
;
747 if (sec
->Characteristics
& IMAGE_SCN_MEM_EXECUTE
) vprot
|= VPROT_EXEC
;
749 /* make sure the import directory is writable */
750 if (imports
&& imports
->VirtualAddress
>= sec
->VirtualAddress
&&
751 imports
->VirtualAddress
< sec
->VirtualAddress
+ size
)
752 vprot
|= VPROT_READ
|VPROT_WRITE
|VPROT_WRITECOPY
;
754 VIRTUAL_SetProt( view
, ptr
+ sec
->VirtualAddress
, size
, vprot
);
758 return STATUS_SUCCESS
;
761 if (ptr
!= (char *)-1) munmap( ptr
, total_size
);
766 /***********************************************************************
769 * Check whether a process handle is for the current process.
771 static BOOL
is_current_process( HANDLE handle
)
775 if (handle
== GetCurrentProcess()) return TRUE
;
776 SERVER_START_REQ( get_process_info
)
778 req
->handle
= handle
;
779 if (!wine_server_call( req
))
780 ret
= ((DWORD
)reply
->pid
== GetCurrentProcessId());
787 /***********************************************************************
790 void virtual_init(void)
793 page_size
= getpagesize();
794 page_mask
= page_size
- 1;
795 /* Make sure we have a power of 2 */
796 assert( !(page_size
& page_mask
) );
798 while ((1 << page_shift
) != page_size
) page_shift
++;
799 #endif /* page_mask */
803 /***********************************************************************
804 * VIRTUAL_SetFaultHandler
806 BOOL
VIRTUAL_SetFaultHandler( LPCVOID addr
, HANDLERPROC proc
, LPVOID arg
)
810 if (!(view
= VIRTUAL_FindView( addr
))) return FALSE
;
811 view
->handlerProc
= proc
;
812 view
->handlerArg
= arg
;
816 /***********************************************************************
817 * VIRTUAL_HandleFault
819 DWORD
VIRTUAL_HandleFault( LPCVOID addr
)
821 FILE_VIEW
*view
= VIRTUAL_FindView( addr
);
822 DWORD ret
= EXCEPTION_ACCESS_VIOLATION
;
826 if (view
->handlerProc
)
828 if (view
->handlerProc(view
->handlerArg
, addr
)) ret
= 0; /* handled */
832 BYTE vprot
= view
->prot
[((char *)addr
- (char *)view
->base
) >> page_shift
];
833 void *page
= (void *)((UINT_PTR
)addr
& ~page_mask
);
834 char *stack
= NtCurrentTeb()->Tib
.StackLimit
;
835 if (vprot
& VPROT_GUARD
)
837 VIRTUAL_SetProt( view
, page
, page_mask
+ 1, vprot
& ~VPROT_GUARD
);
838 ret
= STATUS_GUARD_PAGE_VIOLATION
;
840 /* is it inside the stack guard page? */
841 if (((char *)addr
>= stack
) && ((char *)addr
< stack
+ (page_mask
+1)))
842 ret
= STATUS_STACK_OVERFLOW
;
850 /***********************************************************************
853 * Linux kernels before 2.4.x can support non page-aligned offsets, as
854 * long as the offset is aligned to the filesystem block size. This is
855 * a big performance gain so we want to take advantage of it.
857 * However, when we use 64-bit file support this doesn't work because
858 * glibc rejects unaligned offsets. Also glibc 2.1.3 mmap64 is broken
859 * in that it rounds unaligned offsets down to a page boundary. For
860 * these reasons we do a direct system call here.
862 static void *unaligned_mmap( void *addr
, size_t length
, unsigned int prot
,
863 unsigned int flags
, int fd
, unsigned int offset_low
,
864 unsigned int offset_high
)
866 #if defined(linux) && defined(__i386__) && defined(__GNUC__)
867 if (!offset_high
&& (offset_low
& page_mask
))
882 args
.length
= length
;
886 args
.offset
= offset_low
;
888 __asm__
__volatile__("push %%ebx\n\t"
893 : "0" (90), /* SYS_mmap */
895 if (ret
< 0 && ret
> -4096)
903 return mmap( addr
, length
, prot
, flags
, fd
, ((off_t
)offset_high
<< 32) | offset_low
);
907 /***********************************************************************
910 * Wrapper for mmap() that handles anonymous mappings portably,
911 * and falls back to read if mmap of a file fails.
913 static LPVOID
VIRTUAL_mmap( int fd
, LPVOID start
, DWORD size
,
914 DWORD offset_low
, DWORD offset_high
,
915 int prot
, int flags
, BOOL
*removable
)
919 BOOL is_shared_write
= FALSE
;
921 if (fd
== -1) return wine_anon_mmap( start
, size
, prot
, flags
);
923 if (prot
& PROT_WRITE
)
926 if (flags
& MAP_SHARED
) is_shared_write
= TRUE
;
929 if (!(flags
& MAP_PRIVATE
)) is_shared_write
= TRUE
;
933 if (removable
&& *removable
)
935 /* if on removable media, try using read instead of mmap */
936 if (!is_shared_write
) goto fake_mmap
;
940 if ((ret
= unaligned_mmap( start
, size
, prot
, flags
, fd
,
941 offset_low
, offset_high
)) != (LPVOID
)-1) return ret
;
943 /* mmap() failed; if this is because the file offset is not */
944 /* page-aligned (EINVAL), or because the underlying filesystem */
945 /* does not support mmap() (ENOEXEC,ENODEV), we do it by hand. */
947 if ((errno
!= ENOEXEC
) && (errno
!= EINVAL
) && (errno
!= ENODEV
)) return ret
;
948 if (is_shared_write
) return ret
; /* we cannot fake shared write mappings */
951 /* Reserve the memory with an anonymous mmap */
952 ret
= wine_anon_mmap( start
, size
, PROT_READ
| PROT_WRITE
, flags
);
953 if (ret
== (LPVOID
)-1) return ret
;
954 /* Now read in the file */
955 offset
= ((off_t
)offset_high
<< 32) | offset_low
;
956 pread( fd
, ret
, size
, offset
);
957 mprotect( ret
, size
, prot
); /* Set the right protection */
962 /***********************************************************************
963 * NtAllocateVirtualMemory (NTDLL.@)
964 * ZwAllocateVirtualMemory (NTDLL.@)
966 NTSTATUS WINAPI
NtAllocateVirtualMemory( HANDLE process
, PVOID
*ret
, PVOID addr
,
967 ULONG
*size_ptr
, ULONG type
, ULONG protect
)
972 DWORD size
= *size_ptr
;
974 if (!is_current_process( process
))
976 ERR("Unsupported on other process\n");
977 return STATUS_ACCESS_DENIED
;
980 TRACE("%p %08lx %lx %08lx\n", addr
, size
, type
, protect
);
982 /* Round parameters to a page boundary */
984 if (size
> 0x7fc00000) return STATUS_WORKING_SET_LIMIT_RANGE
; /* 2Gb - 4Mb */
988 if (type
& MEM_RESERVE
) /* Round down to 64k boundary */
989 base
= ROUND_ADDR( addr
, granularity_mask
);
991 base
= ROUND_ADDR( addr
, page_mask
);
992 size
= (((UINT_PTR
)addr
+ size
+ page_mask
) & ~page_mask
) - (UINT_PTR
)base
;
994 /* disallow low 64k, wrap-around and kernel space */
995 if (((char *)base
<= (char *)granularity_mask
) ||
996 ((char *)base
+ size
< (char *)base
) ||
997 (ADDRESS_SPACE_LIMIT
&& ((char *)base
+ size
> (char *)ADDRESS_SPACE_LIMIT
)))
998 return STATUS_INVALID_PARAMETER
;
1003 size
= (size
+ page_mask
) & ~page_mask
;
1006 if (type
& MEM_TOP_DOWN
) {
1007 /* FIXME: MEM_TOP_DOWN allocates the largest possible address.
1008 * Is there _ANY_ way to do it with UNIX mmap()?
1010 WARN("MEM_TOP_DOWN ignored\n");
1011 type
&= ~MEM_TOP_DOWN
;
1014 /* Compute the alloc type flags */
1016 if (type
& MEM_SYSTEM
)
1018 vprot
= VIRTUAL_GetProt( protect
) | VPROT_COMMITTED
;
1019 if (type
& MEM_IMAGE
) vprot
|= VPROT_IMAGE
;
1023 if (!(type
& (MEM_COMMIT
| MEM_RESERVE
)) || (type
& ~(MEM_COMMIT
| MEM_RESERVE
)))
1025 WARN("called with wrong alloc type flags (%08lx) !\n", type
);
1026 return STATUS_INVALID_PARAMETER
;
1028 if (type
& MEM_COMMIT
)
1029 vprot
= VIRTUAL_GetProt( protect
) | VPROT_COMMITTED
;
1034 /* Reserve the memory */
1036 if (type
& MEM_SYSTEM
)
1038 if (!(view
= VIRTUAL_CreateView( base
, size
, VFLAG_VALLOC
| VFLAG_SYSTEM
, vprot
, 0 )))
1039 return STATUS_NO_MEMORY
;
1041 else if ((type
& MEM_RESERVE
) || !base
)
1043 NTSTATUS res
= anon_mmap_aligned( &base
, size
, VIRTUAL_GetUnixProt( vprot
), 0 );
1044 if (res
) return res
;
1046 if (!(view
= VIRTUAL_CreateView( base
, size
, VFLAG_VALLOC
, vprot
, 0 )))
1048 munmap( base
, size
);
1049 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 /* return the values that the caller should use to unmap the area */
1102 *addr_ptr
= view
->base
;
1103 *size_ptr
= view
->size
;
1104 view
->flags
|= VFLAG_SYSTEM
;
1105 VIRTUAL_DeleteView( view
);
1106 return STATUS_SUCCESS
;
1109 if ((type
!= MEM_DECOMMIT
) && (type
!= MEM_RELEASE
))
1111 ERR("called with wrong free type flags (%08lx) !\n", type
);
1112 return STATUS_INVALID_PARAMETER
;
1115 /* Free the pages */
1117 if (type
== MEM_RELEASE
)
1119 if (size
|| (base
!= view
->base
)) return STATUS_INVALID_PARAMETER
;
1120 VIRTUAL_DeleteView( view
);
1124 /* Decommit the pages by remapping zero-pages instead */
1126 if (wine_anon_mmap( (LPVOID
)base
, size
, VIRTUAL_GetUnixProt(0), MAP_FIXED
) != (LPVOID
)base
)
1127 ERR( "Could not remap pages, expect trouble\n" );
1128 if (!VIRTUAL_SetProt( view
, base
, size
, 0 )) return STATUS_ACCESS_DENIED
; /* FIXME */
1133 return STATUS_SUCCESS
;
1137 /***********************************************************************
1138 * NtProtectVirtualMemory (NTDLL.@)
1139 * ZwProtectVirtualMemory (NTDLL.@)
1141 NTSTATUS WINAPI
NtProtectVirtualMemory( HANDLE process
, PVOID
*addr_ptr
, ULONG
*size_ptr
,
1142 ULONG new_prot
, ULONG
*old_prot
)
1148 DWORD prot
, size
= *size_ptr
;
1149 LPVOID addr
= *addr_ptr
;
1151 if (!is_current_process( process
))
1153 ERR("Unsupported on other process\n");
1154 return STATUS_ACCESS_DENIED
;
1157 TRACE("%p %08lx %08lx\n", addr
, size
, new_prot
);
1159 /* Fix the parameters */
1161 size
= ROUND_SIZE( addr
, size
);
1162 base
= ROUND_ADDR( addr
, page_mask
);
1164 if (!(view
= VIRTUAL_FindView( base
)) ||
1165 (base
+ size
> (char *)view
->base
+ view
->size
))
1166 return STATUS_INVALID_PARAMETER
;
1168 /* Make sure all the pages are committed */
1170 p
= view
->prot
+ ((base
- (char *)view
->base
) >> page_shift
);
1171 VIRTUAL_GetWin32Prot( *p
, &prot
, NULL
);
1172 for (i
= size
>> page_shift
; i
; i
--, p
++)
1174 if (!(*p
& VPROT_COMMITTED
)) return STATUS_INVALID_PARAMETER
;
1177 if (old_prot
) *old_prot
= prot
;
1178 vprot
= VIRTUAL_GetProt( new_prot
) | VPROT_COMMITTED
;
1179 if (!VIRTUAL_SetProt( view
, base
, size
, vprot
)) return STATUS_ACCESS_DENIED
;
1183 return STATUS_SUCCESS
;
1187 /***********************************************************************
1188 * NtQueryVirtualMemory (NTDLL.@)
1189 * ZwQueryVirtualMemory (NTDLL.@)
1191 NTSTATUS WINAPI
NtQueryVirtualMemory( HANDLE process
, LPCVOID addr
,
1192 MEMORY_INFORMATION_CLASS info_class
, PVOID buffer
,
1193 ULONG len
, ULONG
*res_len
)
1196 char *base
, *alloc_base
= 0;
1198 MEMORY_BASIC_INFORMATION
*info
= buffer
;
1200 if (info_class
!= MemoryBasicInformation
) return STATUS_INVALID_INFO_CLASS
;
1201 if (ADDRESS_SPACE_LIMIT
&& addr
>= ADDRESS_SPACE_LIMIT
)
1202 return STATUS_WORKING_SET_LIMIT_RANGE
; /* FIXME */
1204 if (!is_current_process( process
))
1206 ERR("Unsupported on other process\n");
1207 return STATUS_ACCESS_DENIED
;
1210 base
= ROUND_ADDR( addr
, page_mask
);
1212 /* Find the view containing the address */
1214 RtlEnterCriticalSection(&csVirtual
);
1215 view
= VIRTUAL_FirstView
;
1220 size
= (char *)ADDRESS_SPACE_LIMIT
- alloc_base
;
1223 if ((char *)view
->base
> base
)
1225 size
= (char *)view
->base
- alloc_base
;
1229 if ((char *)view
->base
+ view
->size
> base
)
1231 alloc_base
= view
->base
;
1235 alloc_base
= (char *)view
->base
+ view
->size
;
1238 RtlLeaveCriticalSection(&csVirtual
);
1240 /* Fill the info structure */
1244 info
->State
= MEM_FREE
;
1246 info
->AllocationProtect
= 0;
1251 BYTE vprot
= view
->prot
[(base
- alloc_base
) >> page_shift
];
1252 VIRTUAL_GetWin32Prot( vprot
, &info
->Protect
, &info
->State
);
1253 for (size
= base
- alloc_base
; size
< view
->size
; size
+= page_mask
+1)
1254 if (view
->prot
[size
>> page_shift
] != vprot
) break;
1255 VIRTUAL_GetWin32Prot( view
->protect
, &info
->AllocationProtect
, NULL
);
1256 if (view
->protect
& VPROT_IMAGE
) info
->Type
= MEM_IMAGE
;
1257 else if (view
->flags
& VFLAG_VALLOC
) info
->Type
= MEM_PRIVATE
;
1258 else info
->Type
= MEM_MAPPED
;
1261 info
->BaseAddress
= (LPVOID
)base
;
1262 info
->AllocationBase
= (LPVOID
)alloc_base
;
1263 info
->RegionSize
= size
- (base
- alloc_base
);
1264 if (res_len
) *res_len
= sizeof(*info
);
1265 return STATUS_SUCCESS
;
1269 /***********************************************************************
1270 * NtLockVirtualMemory (NTDLL.@)
1271 * ZwLockVirtualMemory (NTDLL.@)
1273 NTSTATUS WINAPI
NtLockVirtualMemory( HANDLE process
, PVOID
*addr
, ULONG
*size
, ULONG unknown
)
1275 if (!is_current_process( process
))
1277 ERR("Unsupported on other process\n");
1278 return STATUS_ACCESS_DENIED
;
1280 return STATUS_SUCCESS
;
1284 /***********************************************************************
1285 * NtUnlockVirtualMemory (NTDLL.@)
1286 * ZwUnlockVirtualMemory (NTDLL.@)
1288 NTSTATUS WINAPI
NtUnlockVirtualMemory( HANDLE process
, PVOID
*addr
, ULONG
*size
, ULONG unknown
)
1290 if (!is_current_process( process
))
1292 ERR("Unsupported on other process\n");
1293 return STATUS_ACCESS_DENIED
;
1295 return STATUS_SUCCESS
;
1299 /***********************************************************************
1300 * NtCreateSection (NTDLL.@)
1301 * ZwCreateSection (NTDLL.@)
1303 NTSTATUS WINAPI
NtCreateSection( HANDLE
*handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
,
1304 const LARGE_INTEGER
*size
, ULONG protect
,
1305 ULONG sec_flags
, HANDLE file
)
1309 DWORD len
= attr
->ObjectName
? attr
->ObjectName
->Length
: 0;
1311 /* Check parameters */
1313 if (len
> MAX_PATH
*sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
1315 vprot
= VIRTUAL_GetProt( protect
);
1316 if (sec_flags
& SEC_RESERVE
)
1318 if (file
) return STATUS_INVALID_PARAMETER
;
1320 else vprot
|= VPROT_COMMITTED
;
1321 if (sec_flags
& SEC_NOCACHE
) vprot
|= VPROT_NOCACHE
;
1322 if (sec_flags
& SEC_IMAGE
) vprot
|= VPROT_IMAGE
;
1324 /* Create the server object */
1326 SERVER_START_REQ( create_mapping
)
1328 req
->file_handle
= file
;
1329 req
->size_high
= size
? size
->s
.HighPart
: 0;
1330 req
->size_low
= size
? size
->s
.LowPart
: 0;
1331 req
->protect
= vprot
;
1332 req
->access
= access
;
1333 req
->inherit
= (attr
->Attributes
& OBJ_INHERIT
) != 0;
1334 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
1335 ret
= wine_server_call( req
);
1336 *handle
= reply
->handle
;
1343 /***********************************************************************
1344 * NtOpenSection (NTDLL.@)
1345 * ZwOpenSection (NTDLL.@)
1347 NTSTATUS WINAPI
NtOpenSection( HANDLE
*handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
)
1350 DWORD len
= attr
->ObjectName
->Length
;
1352 if (len
> MAX_PATH
*sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
1354 SERVER_START_REQ( open_mapping
)
1356 req
->access
= access
;
1357 req
->inherit
= (attr
->Attributes
& OBJ_INHERIT
) != 0;
1358 wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
1359 if (!(ret
= wine_server_call( req
))) *handle
= reply
->handle
;
1366 /***********************************************************************
1367 * NtMapViewOfSection (NTDLL.@)
1368 * ZwMapViewOfSection (NTDLL.@)
1370 NTSTATUS WINAPI
NtMapViewOfSection( HANDLE handle
, HANDLE process
, PVOID
*addr_ptr
, ULONG zero_bits
,
1371 ULONG commit_size
, const LARGE_INTEGER
*offset
, ULONG
*size_ptr
,
1372 SECTION_INHERIT inherit
, ULONG alloc_type
, ULONG protect
)
1377 int flags
= MAP_PRIVATE
;
1378 int unix_handle
= -1;
1380 void *base
, *ptr
= (void *)-1, *ret
;
1381 DWORD size_low
, size_high
, header_size
, shared_size
;
1385 if (!is_current_process( process
))
1387 ERR("Unsupported on other process\n");
1388 return STATUS_ACCESS_DENIED
;
1391 TRACE("handle=%p addr=%p off=%lx%08lx size=%x access=%lx\n",
1392 handle
, *addr_ptr
, offset
->s
.HighPart
, offset
->s
.LowPart
, size
, protect
);
1394 /* Check parameters */
1396 if ((offset
->s
.LowPart
& granularity_mask
) ||
1397 (*addr_ptr
&& ((UINT_PTR
)*addr_ptr
& granularity_mask
)))
1398 return STATUS_INVALID_PARAMETER
;
1400 SERVER_START_REQ( get_mapping_info
)
1402 req
->handle
= handle
;
1403 res
= wine_server_call( req
);
1404 prot
= reply
->protect
;
1406 size_low
= reply
->size_low
;
1407 size_high
= reply
->size_high
;
1408 header_size
= reply
->header_size
;
1409 shared_file
= reply
->shared_file
;
1410 shared_size
= reply
->shared_size
;
1411 removable
= reply
->removable
;
1414 if (res
) return res
;
1416 if ((res
= wine_server_handle_to_fd( handle
, 0, &unix_handle
, NULL
, NULL
))) return res
;
1418 if (prot
& VPROT_IMAGE
)
1424 if ((res
= wine_server_handle_to_fd( shared_file
, GENERIC_READ
, &shared_fd
,
1425 NULL
, NULL
))) goto error
;
1426 res
= map_image( handle
, unix_handle
, base
, size_low
, header_size
,
1427 shared_fd
, removable
, addr_ptr
);
1428 wine_server_release_fd( shared_file
, shared_fd
);
1429 NtClose( shared_file
);
1433 res
= map_image( handle
, unix_handle
, base
, size_low
, header_size
,
1434 -1, removable
, addr_ptr
);
1436 wine_server_release_fd( handle
, unix_handle
);
1437 if (!res
) *size_ptr
= size_low
;
1442 ERR("Sizes larger than 4Gb not supported\n");
1444 if ((offset
->s
.LowPart
>= size_low
) ||
1445 (*size_ptr
> size_low
- offset
->s
.LowPart
))
1447 res
= STATUS_INVALID_PARAMETER
;
1450 if (*size_ptr
) size
= ROUND_SIZE( offset
->s
.LowPart
, *size_ptr
);
1451 else size
= size_low
- offset
->s
.LowPart
;
1457 case PAGE_READWRITE
:
1458 case PAGE_EXECUTE_READWRITE
:
1459 if (!(prot
& VPROT_WRITE
))
1461 res
= STATUS_INVALID_PARAMETER
;
1467 case PAGE_WRITECOPY
:
1469 case PAGE_EXECUTE_READ
:
1470 case PAGE_EXECUTE_WRITECOPY
:
1471 if (prot
& VPROT_READ
) break;
1474 res
= STATUS_INVALID_PARAMETER
;
1478 /* FIXME: If a mapping is created with SEC_RESERVE and a process,
1479 * which has a view of this mapping commits some pages, they will
1480 * appear commited in all other processes, which have the same
1481 * view created. Since we don`t support this yet, we create the
1482 * whole mapping commited.
1484 prot
|= VPROT_COMMITTED
;
1486 /* Reserve a properly aligned area */
1488 if ((res
= anon_mmap_aligned( addr_ptr
, size
, PROT_NONE
, 0 ))) goto error
;
1493 TRACE("handle=%p size=%x offset=%lx\n", handle
, size
, offset
->s
.LowPart
);
1495 ret
= VIRTUAL_mmap( unix_handle
, ptr
, size
, offset
->s
.LowPart
, offset
->s
.HighPart
,
1496 VIRTUAL_GetUnixProt( prot
), flags
| MAP_FIXED
, &removable
);
1499 ERR( "VIRTUAL_mmap %p %x %lx%08lx failed\n",
1500 ptr
, size
, offset
->s
.HighPart
, offset
->s
.LowPart
);
1501 res
= STATUS_NO_MEMORY
; /* FIXME */
1504 if (removable
) handle
= 0; /* don't keep handle open on removable media */
1506 if (!(view
= VIRTUAL_CreateView( ptr
, size
, 0, prot
, handle
)))
1508 res
= STATUS_NO_MEMORY
;
1511 wine_server_release_fd( handle
, unix_handle
);
1513 return STATUS_SUCCESS
;
1516 wine_server_release_fd( handle
, unix_handle
);
1517 if (ptr
!= (void *)-1) munmap( ptr
, size
);
1522 /***********************************************************************
1523 * NtUnmapViewOfSection (NTDLL.@)
1524 * ZwUnmapViewOfSection (NTDLL.@)
1526 NTSTATUS WINAPI
NtUnmapViewOfSection( HANDLE process
, PVOID addr
)
1529 void *base
= ROUND_ADDR( addr
, page_mask
);
1531 if (!is_current_process( process
))
1533 ERR("Unsupported on other process\n");
1534 return STATUS_ACCESS_DENIED
;
1536 if (!(view
= VIRTUAL_FindView( base
)) || (base
!= view
->base
)) return STATUS_INVALID_PARAMETER
;
1537 VIRTUAL_DeleteView( view
);
1538 return STATUS_SUCCESS
;
1542 /***********************************************************************
1543 * NtFlushVirtualMemory (NTDLL.@)
1544 * ZwFlushVirtualMemory (NTDLL.@)
1546 NTSTATUS WINAPI
NtFlushVirtualMemory( HANDLE process
, LPCVOID
*addr_ptr
,
1547 ULONG
*size_ptr
, ULONG unknown
)
1550 void *addr
= ROUND_ADDR( *addr_ptr
, 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( addr
))) return STATUS_INVALID_PARAMETER
;
1558 if (!*size_ptr
) *size_ptr
= view
->size
;
1560 if (!msync( addr
, *size_ptr
, MS_SYNC
)) return STATUS_SUCCESS
;
1561 return STATUS_NOT_MAPPED_DATA
;
1565 /***********************************************************************
1566 * NtReadVirtualMemory (NTDLL.@)
1567 * ZwReadVirtualMemory (NTDLL.@)
1569 NTSTATUS WINAPI
NtReadVirtualMemory( HANDLE process
, const void *addr
, void *buffer
,
1570 SIZE_T size
, SIZE_T
*bytes_read
)
1574 SERVER_START_REQ( read_process_memory
)
1576 req
->handle
= process
;
1577 req
->addr
= (void *)addr
;
1578 wine_server_set_reply( req
, buffer
, size
);
1579 if ((status
= wine_server_call( req
))) size
= 0;
1582 if (bytes_read
) *bytes_read
= size
;
1587 /***********************************************************************
1588 * NtWriteVirtualMemory (NTDLL.@)
1589 * ZwWriteVirtualMemory (NTDLL.@)
1591 NTSTATUS WINAPI
NtWriteVirtualMemory( HANDLE process
, void *addr
, const void *buffer
,
1592 SIZE_T size
, SIZE_T
*bytes_written
)
1594 static const unsigned int zero
;
1595 unsigned int first_offset
, last_offset
, first_mask
, last_mask
;
1598 if (!size
) return STATUS_INVALID_PARAMETER
;
1600 /* compute the mask for the first int */
1602 first_offset
= (unsigned int)addr
% sizeof(int);
1603 memset( &first_mask
, 0, first_offset
);
1605 /* compute the mask for the last int */
1606 last_offset
= (size
+ first_offset
) % sizeof(int);
1608 memset( &last_mask
, 0xff, last_offset
? last_offset
: sizeof(int) );
1610 SERVER_START_REQ( write_process_memory
)
1612 req
->handle
= process
;
1613 req
->addr
= (char *)addr
- first_offset
;
1614 req
->first_mask
= first_mask
;
1615 req
->last_mask
= last_mask
;
1616 if (first_offset
) wine_server_add_data( req
, &zero
, first_offset
);
1617 wine_server_add_data( req
, buffer
, size
);
1618 if (last_offset
) wine_server_add_data( req
, &zero
, sizeof(int) - last_offset
);
1620 if ((status
= wine_server_call( req
))) size
= 0;
1623 if (bytes_written
) *bytes_written
= size
;