2 * Win32 virtual memory functions
4 * Copyright 1997, 2002 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
26 #ifdef HAVE_SYS_ERRNO_H
27 #include <sys/errno.h>
36 #include <sys/types.h>
37 #ifdef HAVE_SYS_MMAN_H
41 #define NONAMELESSUNION
42 #define NONAMELESSSTRUCT
46 #include "wine/library.h"
47 #include "wine/server.h"
48 #include "wine/debug.h"
49 #include "ntdll_misc.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(virtual);
52 WINE_DECLARE_DEBUG_CHANNEL(module
);
61 struct _FV
*next
; /* Next view */
62 struct _FV
*prev
; /* Prev view */
63 void *base
; /* Base address */
64 UINT size
; /* Size in bytes */
65 UINT flags
; /* Allocation flags */
66 HANDLE mapping
; /* Handle to the file mapping */
67 HANDLERPROC handlerProc
; /* Fault handler */
68 LPVOID handlerArg
; /* Fault handler argument */
69 BYTE protect
; /* Protection for all pages at allocation time */
70 BYTE prot
[1]; /* Protection byte for each page */
74 #define VFLAG_SYSTEM 0x01
75 #define VFLAG_VALLOC 0x02 /* allocated by VirtualAlloc */
77 /* Conversion from VPROT_* to Win32 flags */
78 static const BYTE VIRTUAL_Win32Flags
[16] =
80 PAGE_NOACCESS
, /* 0 */
81 PAGE_READONLY
, /* READ */
82 PAGE_READWRITE
, /* WRITE */
83 PAGE_READWRITE
, /* READ | WRITE */
84 PAGE_EXECUTE
, /* EXEC */
85 PAGE_EXECUTE_READ
, /* READ | EXEC */
86 PAGE_EXECUTE_READWRITE
, /* WRITE | EXEC */
87 PAGE_EXECUTE_READWRITE
, /* READ | WRITE | EXEC */
88 PAGE_WRITECOPY
, /* WRITECOPY */
89 PAGE_WRITECOPY
, /* READ | WRITECOPY */
90 PAGE_WRITECOPY
, /* WRITE | WRITECOPY */
91 PAGE_WRITECOPY
, /* READ | WRITE | WRITECOPY */
92 PAGE_EXECUTE_WRITECOPY
, /* EXEC | WRITECOPY */
93 PAGE_EXECUTE_WRITECOPY
, /* READ | EXEC | WRITECOPY */
94 PAGE_EXECUTE_WRITECOPY
, /* WRITE | EXEC | WRITECOPY */
95 PAGE_EXECUTE_WRITECOPY
/* READ | WRITE | EXEC | WRITECOPY */
99 static FILE_VIEW
*VIRTUAL_FirstView
;
101 static CRITICAL_SECTION csVirtual
;
102 static CRITICAL_SECTION_DEBUG critsect_debug
=
105 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
106 0, 0, { 0, (DWORD
)(__FILE__
": csVirtual") }
108 static CRITICAL_SECTION csVirtual
= { &critsect_debug
, -1, 0, 0, 0, 0 };
111 /* These are always the same on an i386, and it will be faster this way */
112 # define page_mask 0xfff
113 # define page_shift 12
114 # define page_size 0x1000
115 /* Note: ADDRESS_SPACE_LIMIT is a Windows limit, you cannot change it.
116 * If you are on Solaris you need to find a way to avoid having the system
117 * allocate things above 0xc000000. Don't touch that define.
119 # define ADDRESS_SPACE_LIMIT ((void *)0xc0000000) /* top of the user address space */
121 static UINT page_shift
;
122 static UINT page_mask
;
123 static UINT page_size
;
124 # define ADDRESS_SPACE_LIMIT 0 /* no limit needed on other platforms */
125 #endif /* __i386__ */
126 #define granularity_mask 0xffff /* Allocation granularity (usually 64k) */
128 #define ROUND_ADDR(addr,mask) \
129 ((void *)((UINT_PTR)(addr) & ~(mask)))
131 #define ROUND_SIZE(addr,size) \
132 (((UINT)(size) + ((UINT_PTR)(addr) & page_mask) + page_mask) & ~page_mask)
134 #define VIRTUAL_DEBUG_DUMP_VIEW(view) \
135 if (!TRACE_ON(virtual)); else VIRTUAL_DumpView(view)
137 static LPVOID
VIRTUAL_mmap( int fd
, LPVOID start
, DWORD size
, DWORD offset_low
,
138 DWORD offset_high
, int prot
, int flags
, BOOL
*removable
);
141 /***********************************************************************
144 static const char *VIRTUAL_GetProtStr( BYTE prot
)
146 static char buffer
[6];
147 buffer
[0] = (prot
& VPROT_COMMITTED
) ? 'c' : '-';
148 buffer
[1] = (prot
& VPROT_GUARD
) ? 'g' : '-';
149 buffer
[2] = (prot
& VPROT_READ
) ? 'r' : '-';
150 buffer
[3] = (prot
& VPROT_WRITE
) ?
151 ((prot
& VPROT_WRITECOPY
) ? 'w' : 'W') : '-';
152 buffer
[4] = (prot
& VPROT_EXEC
) ? 'x' : '-';
158 /***********************************************************************
161 static void VIRTUAL_DumpView( FILE_VIEW
*view
)
164 char *addr
= view
->base
;
165 BYTE prot
= view
->prot
[0];
167 DPRINTF( "View: %p - %p", addr
, addr
+ view
->size
- 1 );
168 if (view
->flags
& VFLAG_SYSTEM
)
169 DPRINTF( " (system)\n" );
170 else if (view
->flags
& VFLAG_VALLOC
)
171 DPRINTF( " (valloc)\n" );
172 else if (view
->mapping
)
173 DPRINTF( " %p\n", view
->mapping
);
175 DPRINTF( " (anonymous)\n");
177 for (count
= i
= 1; i
< view
->size
>> page_shift
; i
++, count
++)
179 if (view
->prot
[i
] == prot
) continue;
180 DPRINTF( " %p - %p %s\n",
181 addr
, addr
+ (count
<< page_shift
) - 1, VIRTUAL_GetProtStr(prot
) );
182 addr
+= (count
<< page_shift
);
183 prot
= view
->prot
[i
];
187 DPRINTF( " %p - %p %s\n",
188 addr
, addr
+ (count
<< page_shift
) - 1, VIRTUAL_GetProtStr(prot
) );
192 /***********************************************************************
195 void VIRTUAL_Dump(void)
198 DPRINTF( "\nDump of all virtual memory views:\n\n" );
199 RtlEnterCriticalSection(&csVirtual
);
200 view
= VIRTUAL_FirstView
;
203 VIRTUAL_DumpView( view
);
206 RtlLeaveCriticalSection(&csVirtual
);
210 /***********************************************************************
213 * Find the view containing a given address.
219 static FILE_VIEW
*VIRTUAL_FindView( const void *addr
) /* [in] Address */
223 RtlEnterCriticalSection(&csVirtual
);
224 view
= VIRTUAL_FirstView
;
227 if (view
->base
> addr
)
232 if ((char*)view
->base
+ view
->size
> (char*)addr
) break;
235 RtlLeaveCriticalSection(&csVirtual
);
240 /***********************************************************************
247 static void VIRTUAL_DeleteView( FILE_VIEW
*view
) /* [in] View */
249 RtlEnterCriticalSection(&csVirtual
);
250 if (!(view
->flags
& VFLAG_SYSTEM
))
251 munmap( (void *)view
->base
, view
->size
);
252 if (view
->next
) view
->next
->prev
= view
->prev
;
253 if (view
->prev
) view
->prev
->next
= view
->next
;
254 else VIRTUAL_FirstView
= view
->next
;
255 RtlLeaveCriticalSection(&csVirtual
);
256 if (view
->mapping
) NtClose( view
->mapping
);
261 /***********************************************************************
264 * Create a new view and add it in the linked list.
266 static FILE_VIEW
*VIRTUAL_CreateView( void *base
, UINT size
, UINT flags
,
267 BYTE vprot
, HANDLE mapping
)
269 FILE_VIEW
*view
, *prev
;
271 /* Create the view structure */
273 assert( !((unsigned int)base
& page_mask
) );
274 assert( !(size
& page_mask
) );
276 if (!(view
= (FILE_VIEW
*)malloc( sizeof(*view
) + size
- 1 ))) return NULL
;
278 view
->size
= size
<< page_shift
;
280 view
->mapping
= mapping
;
281 view
->protect
= vprot
;
282 view
->handlerProc
= NULL
;
283 memset( view
->prot
, vprot
, size
);
285 /* Duplicate the mapping handle */
288 NtDuplicateObject( GetCurrentProcess(), view
->mapping
,
289 GetCurrentProcess(), &view
->mapping
,
290 0, 0, DUPLICATE_SAME_ACCESS
))
296 /* Insert it in the linked list */
298 RtlEnterCriticalSection(&csVirtual
);
299 if (!VIRTUAL_FirstView
|| (VIRTUAL_FirstView
->base
> base
))
301 view
->next
= VIRTUAL_FirstView
;
303 if (view
->next
) view
->next
->prev
= view
;
304 VIRTUAL_FirstView
= view
;
308 prev
= VIRTUAL_FirstView
;
309 while (prev
->next
&& (prev
->next
->base
< base
)) prev
= prev
->next
;
310 view
->next
= prev
->next
;
312 if (view
->next
) view
->next
->prev
= view
;
315 /* Check for overlapping views. This can happen if the previous view
316 * was a system view that got unmapped behind our back. In that case
317 * we recover by simply deleting it. */
318 if ((char *)prev
->base
+ prev
->size
> (char *)base
)
320 TRACE( "overlapping prev view %p-%p for %p-%p\n",
321 prev
->base
, (char *)prev
->base
+ prev
->size
,
322 base
, (char *)base
+ view
->size
);
323 assert( view
->prev
->flags
& VFLAG_SYSTEM
);
324 VIRTUAL_DeleteView( view
->prev
);
327 /* check for overlap with next too */
328 if (view
->next
&& (char *)base
+ view
->size
> (char *)view
->next
->base
)
330 TRACE( "overlapping next view %p-%p for %p-%p\n",
331 view
->next
->base
, (char *)view
->next
->base
+ view
->next
->size
,
332 base
, (char *)base
+ view
->size
);
333 assert( view
->next
->flags
& VFLAG_SYSTEM
);
334 VIRTUAL_DeleteView( view
->next
);
336 RtlLeaveCriticalSection(&csVirtual
);
337 VIRTUAL_DEBUG_DUMP_VIEW( view
);
342 /***********************************************************************
343 * VIRTUAL_GetUnixProt
345 * Convert page protections to protection for mmap/mprotect.
347 static int VIRTUAL_GetUnixProt( BYTE vprot
)
350 if ((vprot
& VPROT_COMMITTED
) && !(vprot
& VPROT_GUARD
))
352 if (vprot
& VPROT_READ
) prot
|= PROT_READ
;
353 if (vprot
& VPROT_WRITE
) prot
|= PROT_WRITE
;
354 if (vprot
& VPROT_WRITECOPY
) prot
|= PROT_WRITE
;
355 if (vprot
& VPROT_EXEC
) prot
|= PROT_EXEC
;
361 /***********************************************************************
362 * VIRTUAL_GetWin32Prot
364 * Convert page protections to Win32 flags.
369 static void VIRTUAL_GetWin32Prot(
370 BYTE vprot
, /* [in] Page protection flags */
371 DWORD
*protect
, /* [out] Location to store Win32 protection flags */
372 DWORD
*state
) /* [out] Location to store mem state flag */
375 *protect
= VIRTUAL_Win32Flags
[vprot
& 0x0f];
376 /* if (vprot & VPROT_GUARD) *protect |= PAGE_GUARD;*/
377 if (vprot
& VPROT_NOCACHE
) *protect
|= PAGE_NOCACHE
;
379 if (vprot
& VPROT_GUARD
) *protect
= PAGE_NOACCESS
;
382 if (state
) *state
= (vprot
& VPROT_COMMITTED
) ? MEM_COMMIT
: MEM_RESERVE
;
386 /***********************************************************************
389 * Build page protections from Win32 flags.
392 * Value of page protection flags
394 static BYTE
VIRTUAL_GetProt( DWORD protect
) /* [in] Win32 protection flags */
398 switch(protect
& 0xff)
404 vprot
= VPROT_READ
| VPROT_WRITE
;
407 /* MSDN CreateFileMapping() states that if PAGE_WRITECOPY is given,
408 * that the hFile must have been opened with GENERIC_READ and
409 * GENERIC_WRITE access. This is WRONG as tests show that you
410 * only need GENERIC_READ access (at least for Win9x,
411 * FIXME: what about NT?). Thus, we don't put VPROT_WRITE in
412 * PAGE_WRITECOPY and PAGE_EXECUTE_WRITECOPY.
414 vprot
= VPROT_READ
| VPROT_WRITECOPY
;
419 case PAGE_EXECUTE_READ
:
420 vprot
= VPROT_EXEC
| VPROT_READ
;
422 case PAGE_EXECUTE_READWRITE
:
423 vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITE
;
425 case PAGE_EXECUTE_WRITECOPY
:
426 /* See comment for PAGE_WRITECOPY above */
427 vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITECOPY
;
434 if (protect
& PAGE_GUARD
) vprot
|= VPROT_GUARD
;
435 if (protect
& PAGE_NOCACHE
) vprot
|= VPROT_NOCACHE
;
440 /***********************************************************************
443 * Change the protection of a range of pages.
449 static BOOL
VIRTUAL_SetProt( FILE_VIEW
*view
, /* [in] Pointer to view */
450 void *base
, /* [in] Starting address */
451 UINT size
, /* [in] Size in bytes */
452 BYTE vprot
) /* [in] Protections to use */
455 base
, (char *)base
+ size
- 1, VIRTUAL_GetProtStr( vprot
) );
457 if (mprotect( base
, size
, VIRTUAL_GetUnixProt(vprot
) ))
458 return FALSE
; /* FIXME: last error */
460 memset( view
->prot
+ (((char *)base
- (char *)view
->base
) >> page_shift
),
461 vprot
, size
>> page_shift
);
462 VIRTUAL_DEBUG_DUMP_VIEW( view
);
467 /***********************************************************************
470 * Create an anonymous mapping aligned to the allocation granularity.
472 static NTSTATUS
anon_mmap_aligned( void **addr
, unsigned int size
, int prot
, int flags
)
474 void *ptr
, *base
= *addr
;
475 unsigned int view_size
= size
+ (base
? 0 : granularity_mask
+ 1);
477 if ((ptr
= wine_anon_mmap( base
, view_size
, prot
, flags
)) == (void *)-1)
479 if (errno
== ENOMEM
) return STATUS_NO_MEMORY
;
480 return STATUS_INVALID_PARAMETER
;
485 /* Release the extra memory while keeping the range
486 * starting on the granularity boundary. */
487 if ((unsigned int)ptr
& granularity_mask
)
489 unsigned int extra
= granularity_mask
+ 1 - ((unsigned int)ptr
& granularity_mask
);
490 munmap( ptr
, extra
);
491 ptr
= (char *)ptr
+ extra
;
494 if (view_size
> size
)
495 munmap( (char *)ptr
+ size
, view_size
- size
);
497 else if (ptr
!= base
)
499 /* We couldn't get the address we wanted */
500 munmap( ptr
, view_size
);
501 return STATUS_CONFLICTING_ADDRESSES
;
504 return STATUS_SUCCESS
;
508 /***********************************************************************
511 * Apply the relocations to a mapped PE image
513 static int do_relocations( char *base
, const IMAGE_DATA_DIRECTORY
*dir
,
514 int delta
, DWORD total_size
)
516 IMAGE_BASE_RELOCATION
*rel
;
518 TRACE_(module
)( "relocating from %p-%p to %p-%p\n",
519 base
- delta
, base
- delta
+ total_size
, base
, base
+ total_size
);
521 for (rel
= (IMAGE_BASE_RELOCATION
*)(base
+ dir
->VirtualAddress
);
522 ((char *)rel
< base
+ dir
->VirtualAddress
+ dir
->Size
) && rel
->SizeOfBlock
;
523 rel
= (IMAGE_BASE_RELOCATION
*)((char*)rel
+ rel
->SizeOfBlock
) )
525 char *page
= base
+ rel
->VirtualAddress
;
526 WORD
*TypeOffset
= (WORD
*)(rel
+ 1);
527 int i
, count
= (rel
->SizeOfBlock
- sizeof(*rel
)) / sizeof(*TypeOffset
);
529 if (!count
) continue;
532 if ((char *)rel
+ rel
->SizeOfBlock
> base
+ dir
->VirtualAddress
+ dir
->Size
||
533 page
> base
+ total_size
)
535 ERR_(module
)("invalid relocation %p,%lx,%ld at %p,%lx,%lx\n",
536 rel
, rel
->VirtualAddress
, rel
->SizeOfBlock
,
537 base
, dir
->VirtualAddress
, dir
->Size
);
541 TRACE_(module
)("%ld relocations for page %lx\n", rel
->SizeOfBlock
, rel
->VirtualAddress
);
543 /* patching in reverse order */
544 for (i
= 0 ; i
< count
; i
++)
546 int offset
= TypeOffset
[i
] & 0xFFF;
547 int type
= TypeOffset
[i
] >> 12;
550 case IMAGE_REL_BASED_ABSOLUTE
:
552 case IMAGE_REL_BASED_HIGH
:
553 *(short*)(page
+offset
) += HIWORD(delta
);
555 case IMAGE_REL_BASED_LOW
:
556 *(short*)(page
+offset
) += LOWORD(delta
);
558 case IMAGE_REL_BASED_HIGHLOW
:
559 *(int*)(page
+offset
) += delta
;
560 /* FIXME: if this is an exported address, fire up enhanced logic */
563 FIXME_(module
)("Unknown/unsupported fixup type %d.\n", type
);
572 /***********************************************************************
575 * Map an executable (PE format) image into memory.
577 static NTSTATUS
map_image( HANDLE hmapping
, int fd
, char *base
, DWORD total_size
,
578 DWORD header_size
, int shared_fd
, BOOL removable
, PVOID
*addr_ptr
)
580 IMAGE_DOS_HEADER
*dos
;
581 IMAGE_NT_HEADERS
*nt
;
582 IMAGE_SECTION_HEADER
*sec
;
583 IMAGE_DATA_DIRECTORY
*imports
;
584 NTSTATUS status
= STATUS_INVALID_IMAGE_FORMAT
; /* generic error (FIXME) */
589 /* zero-map the whole range */
591 if (base
< (char *)0x110000 || /* make sure the DOS area remains free */
592 (ptr
= wine_anon_mmap( base
, total_size
,
593 PROT_READ
| PROT_WRITE
| PROT_EXEC
, 0 )) == (char *)-1)
595 ptr
= wine_anon_mmap( NULL
, total_size
,
596 PROT_READ
| PROT_WRITE
| PROT_EXEC
, 0 );
597 if (ptr
== (char *)-1)
599 ERR_(module
)("Not enough memory for module (%ld bytes)\n", total_size
);
603 TRACE_(module
)( "mapped PE file at %p-%p\n", ptr
, ptr
+ total_size
);
607 if (VIRTUAL_mmap( fd
, ptr
, header_size
, 0, 0, PROT_READ
,
608 MAP_PRIVATE
| MAP_FIXED
, &removable
) == (char *)-1) goto error
;
609 dos
= (IMAGE_DOS_HEADER
*)ptr
;
610 nt
= (IMAGE_NT_HEADERS
*)(ptr
+ dos
->e_lfanew
);
611 if ((char *)(nt
+ 1) > ptr
+ header_size
) goto error
;
613 sec
= (IMAGE_SECTION_HEADER
*)((char*)&nt
->OptionalHeader
+nt
->FileHeader
.SizeOfOptionalHeader
);
614 if ((char *)(sec
+ nt
->FileHeader
.NumberOfSections
) > ptr
+ header_size
) goto error
;
616 imports
= nt
->OptionalHeader
.DataDirectory
+ IMAGE_DIRECTORY_ENTRY_IMPORT
;
617 if (!imports
->Size
|| !imports
->VirtualAddress
) imports
= NULL
;
619 /* check the architecture */
621 if (nt
->FileHeader
.Machine
!= IMAGE_FILE_MACHINE_I386
)
623 MESSAGE("Trying to load PE image for unsupported architecture (");
624 switch (nt
->FileHeader
.Machine
)
626 case IMAGE_FILE_MACHINE_UNKNOWN
: MESSAGE("Unknown"); break;
627 case IMAGE_FILE_MACHINE_I860
: MESSAGE("I860"); break;
628 case IMAGE_FILE_MACHINE_R3000
: MESSAGE("R3000"); break;
629 case IMAGE_FILE_MACHINE_R4000
: MESSAGE("R4000"); break;
630 case IMAGE_FILE_MACHINE_R10000
: MESSAGE("R10000"); break;
631 case IMAGE_FILE_MACHINE_ALPHA
: MESSAGE("Alpha"); break;
632 case IMAGE_FILE_MACHINE_POWERPC
: MESSAGE("PowerPC"); break;
633 default: MESSAGE("Unknown-%04x", nt
->FileHeader
.Machine
); break;
639 /* map all the sections */
641 for (i
= pos
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
645 /* a few sanity checks */
646 size
= sec
->VirtualAddress
+ ROUND_SIZE( sec
->VirtualAddress
, sec
->Misc
.VirtualSize
);
647 if (sec
->VirtualAddress
> total_size
|| size
> total_size
|| size
< sec
->VirtualAddress
)
649 ERR_(module
)( "Section %.8s too large (%lx+%lx/%lx)\n",
650 sec
->Name
, sec
->VirtualAddress
, sec
->Misc
.VirtualSize
, total_size
);
654 if ((sec
->Characteristics
& IMAGE_SCN_MEM_SHARED
) &&
655 (sec
->Characteristics
& IMAGE_SCN_MEM_WRITE
))
657 size
= ROUND_SIZE( 0, sec
->Misc
.VirtualSize
);
658 TRACE_(module
)( "mapping shared section %.8s at %p off %lx (%x) size %lx (%lx) flags %lx\n",
659 sec
->Name
, ptr
+ sec
->VirtualAddress
,
660 sec
->PointerToRawData
, pos
, sec
->SizeOfRawData
,
661 size
, sec
->Characteristics
);
662 if (VIRTUAL_mmap( shared_fd
, ptr
+ sec
->VirtualAddress
, size
,
663 pos
, 0, PROT_READ
|PROT_WRITE
|PROT_EXEC
,
664 MAP_SHARED
|MAP_FIXED
, NULL
) == (void *)-1)
666 ERR_(module
)( "Could not map shared section %.8s\n", sec
->Name
);
670 /* check if the import directory falls inside this section */
671 if (imports
&& imports
->VirtualAddress
>= sec
->VirtualAddress
&&
672 imports
->VirtualAddress
< sec
->VirtualAddress
+ size
)
674 UINT_PTR base
= imports
->VirtualAddress
& ~page_mask
;
675 UINT_PTR end
= base
+ ROUND_SIZE( imports
->VirtualAddress
, imports
->Size
);
676 if (end
> sec
->VirtualAddress
+ size
) end
= sec
->VirtualAddress
+ size
;
677 if (end
> base
) VIRTUAL_mmap( shared_fd
, ptr
+ base
, end
- base
,
678 pos
+ (base
- sec
->VirtualAddress
), 0,
679 PROT_READ
|PROT_WRITE
|PROT_EXEC
,
680 MAP_PRIVATE
|MAP_FIXED
, NULL
);
686 TRACE_(module
)( "mapping section %.8s at %p off %lx size %lx flags %lx\n",
687 sec
->Name
, ptr
+ sec
->VirtualAddress
,
688 sec
->PointerToRawData
, sec
->SizeOfRawData
,
689 sec
->Characteristics
);
691 if ((sec
->Characteristics
& IMAGE_SCN_CNT_UNINITIALIZED_DATA
) &&
692 !(sec
->Characteristics
& IMAGE_SCN_CNT_INITIALIZED_DATA
)) continue;
693 if (!sec
->PointerToRawData
|| !sec
->SizeOfRawData
) continue;
695 /* Note: if the section is not aligned properly VIRTUAL_mmap will magically
696 * fall back to read(), so we don't need to check anything here.
698 if (VIRTUAL_mmap( fd
, ptr
+ sec
->VirtualAddress
, sec
->SizeOfRawData
,
699 sec
->PointerToRawData
, 0, PROT_READ
|PROT_WRITE
|PROT_EXEC
,
700 MAP_PRIVATE
| MAP_FIXED
, &removable
) == (void *)-1)
702 ERR_(module
)( "Could not map section %.8s, file probably truncated\n", sec
->Name
);
706 if ((sec
->SizeOfRawData
< sec
->Misc
.VirtualSize
) && (sec
->SizeOfRawData
& page_mask
))
708 DWORD end
= ROUND_SIZE( 0, sec
->SizeOfRawData
);
709 if (end
> sec
->Misc
.VirtualSize
) end
= sec
->Misc
.VirtualSize
;
710 TRACE_(module
)("clearing %p - %p\n",
711 ptr
+ sec
->VirtualAddress
+ sec
->SizeOfRawData
,
712 ptr
+ sec
->VirtualAddress
+ end
);
713 memset( ptr
+ sec
->VirtualAddress
+ sec
->SizeOfRawData
, 0,
714 end
- sec
->SizeOfRawData
);
719 /* perform base relocation, if necessary */
723 const IMAGE_DATA_DIRECTORY
*relocs
;
725 relocs
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
];
726 if (!relocs
->VirtualAddress
|| !relocs
->Size
)
728 if (nt
->OptionalHeader
.ImageBase
== 0x400000)
729 ERR("Standard load address for a Win32 program (0x00400000) not available - security-patched kernel ?\n");
731 ERR( "FATAL: Need to relocate module from addr %lx, but there are no relocation records\n",
732 nt
->OptionalHeader
.ImageBase
);
736 /* FIXME: If we need to relocate a system DLL (base > 2GB) we should
737 * really make sure that the *new* base address is also > 2GB.
738 * Some DLLs really check the MSB of the module handle :-/
740 if ((nt
->OptionalHeader
.ImageBase
& 0x80000000) && !((DWORD
)base
& 0x80000000))
741 ERR( "Forced to relocate system DLL (base > 2GB). This is not good.\n" );
743 if (!do_relocations( ptr
, relocs
, ptr
- base
, total_size
))
749 if (removable
) hmapping
= 0; /* don't keep handle open on removable media */
750 if (!(view
= VIRTUAL_CreateView( ptr
, total_size
, 0,
751 VPROT_COMMITTED
| VPROT_READ
| VPROT_WRITE
|
752 VPROT_EXEC
| VPROT_WRITECOPY
| VPROT_IMAGE
, hmapping
)))
754 status
= STATUS_NO_MEMORY
;
758 /* set the image protections */
760 VIRTUAL_SetProt( view
, ptr
, header_size
, VPROT_COMMITTED
| VPROT_READ
);
761 sec
= (IMAGE_SECTION_HEADER
*)((char *)&nt
->OptionalHeader
+nt
->FileHeader
.SizeOfOptionalHeader
);
762 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
764 DWORD size
= ROUND_SIZE( sec
->VirtualAddress
, sec
->Misc
.VirtualSize
);
765 BYTE vprot
= VPROT_COMMITTED
;
766 if (sec
->Characteristics
& IMAGE_SCN_MEM_READ
) vprot
|= VPROT_READ
;
767 if (sec
->Characteristics
& IMAGE_SCN_MEM_WRITE
) vprot
|= VPROT_WRITE
|VPROT_WRITECOPY
;
768 if (sec
->Characteristics
& IMAGE_SCN_MEM_EXECUTE
) vprot
|= VPROT_EXEC
;
770 /* make sure the import directory is writable */
771 if (imports
&& imports
->VirtualAddress
>= sec
->VirtualAddress
&&
772 imports
->VirtualAddress
< sec
->VirtualAddress
+ size
)
773 vprot
|= VPROT_READ
|VPROT_WRITE
|VPROT_WRITECOPY
;
775 VIRTUAL_SetProt( view
, ptr
+ sec
->VirtualAddress
, size
, vprot
);
779 return STATUS_SUCCESS
;
782 if (ptr
!= (char *)-1) munmap( ptr
, total_size
);
787 /***********************************************************************
790 * Check whether a process handle is for the current process.
792 static BOOL
is_current_process( HANDLE handle
)
796 if (handle
== GetCurrentProcess()) return TRUE
;
797 SERVER_START_REQ( get_process_info
)
799 req
->handle
= handle
;
800 if (!wine_server_call( req
))
801 ret
= ((DWORD
)reply
->pid
== GetCurrentProcessId());
808 /***********************************************************************
811 void virtual_init(void)
814 page_size
= getpagesize();
815 page_mask
= page_size
- 1;
816 /* Make sure we have a power of 2 */
817 assert( !(page_size
& page_mask
) );
819 while ((1 << page_shift
) != page_size
) page_shift
++;
820 #endif /* page_mask */
824 /***********************************************************************
825 * VIRTUAL_SetFaultHandler
827 BOOL
VIRTUAL_SetFaultHandler( LPCVOID addr
, HANDLERPROC proc
, LPVOID arg
)
831 if (!(view
= VIRTUAL_FindView( addr
))) return FALSE
;
832 view
->handlerProc
= proc
;
833 view
->handlerArg
= arg
;
837 /***********************************************************************
838 * VIRTUAL_HandleFault
840 DWORD
VIRTUAL_HandleFault( LPCVOID addr
)
842 FILE_VIEW
*view
= VIRTUAL_FindView( addr
);
843 DWORD ret
= EXCEPTION_ACCESS_VIOLATION
;
847 if (view
->handlerProc
)
849 if (view
->handlerProc(view
->handlerArg
, addr
)) ret
= 0; /* handled */
853 BYTE vprot
= view
->prot
[((char *)addr
- (char *)view
->base
) >> page_shift
];
854 void *page
= (void *)((UINT_PTR
)addr
& ~page_mask
);
855 char *stack
= NtCurrentTeb()->Tib
.StackLimit
;
856 if (vprot
& VPROT_GUARD
)
858 VIRTUAL_SetProt( view
, page
, page_mask
+ 1, vprot
& ~VPROT_GUARD
);
859 ret
= STATUS_GUARD_PAGE_VIOLATION
;
861 /* is it inside the stack guard page? */
862 if (((char *)addr
>= stack
) && ((char *)addr
< stack
+ (page_mask
+1)))
863 ret
= STATUS_STACK_OVERFLOW
;
871 /***********************************************************************
874 * Linux kernels before 2.4.x can support non page-aligned offsets, as
875 * long as the offset is aligned to the filesystem block size. This is
876 * a big performance gain so we want to take advantage of it.
878 * However, when we use 64-bit file support this doesn't work because
879 * glibc rejects unaligned offsets. Also glibc 2.1.3 mmap64 is broken
880 * in that it rounds unaligned offsets down to a page boundary. For
881 * these reasons we do a direct system call here.
883 static void *unaligned_mmap( void *addr
, size_t length
, unsigned int prot
,
884 unsigned int flags
, int fd
, unsigned int offset_low
,
885 unsigned int offset_high
)
887 #if defined(linux) && defined(__i386__) && defined(__GNUC__)
888 if (!offset_high
&& (offset_low
& page_mask
))
903 args
.length
= length
;
907 args
.offset
= offset_low
;
909 __asm__
__volatile__("push %%ebx\n\t"
914 : "0" (90), /* SYS_mmap */
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_DECOMMIT
) && (type
!= MEM_RELEASE
))
1135 WARN("called with wrong free type flags (%08lx) !\n", type
);
1136 return STATUS_INVALID_PARAMETER
;
1139 /* Free the pages */
1141 if (type
== MEM_RELEASE
)
1143 if (size
|| (base
!= view
->base
)) return STATUS_INVALID_PARAMETER
;
1144 VIRTUAL_DeleteView( view
);
1148 /* Decommit the pages by remapping zero-pages instead */
1150 if (wine_anon_mmap( (LPVOID
)base
, size
, VIRTUAL_GetUnixProt(0), MAP_FIXED
) != (LPVOID
)base
)
1151 ERR( "Could not remap pages, expect trouble\n" );
1152 if (!VIRTUAL_SetProt( view
, base
, size
, 0 )) return STATUS_ACCESS_DENIED
; /* FIXME */
1157 return STATUS_SUCCESS
;
1161 /***********************************************************************
1162 * NtProtectVirtualMemory (NTDLL.@)
1163 * ZwProtectVirtualMemory (NTDLL.@)
1165 NTSTATUS WINAPI
NtProtectVirtualMemory( HANDLE process
, PVOID
*addr_ptr
, ULONG
*size_ptr
,
1166 ULONG new_prot
, ULONG
*old_prot
)
1172 DWORD prot
, size
= *size_ptr
;
1173 LPVOID addr
= *addr_ptr
;
1175 if (!is_current_process( process
))
1177 ERR("Unsupported on other process\n");
1178 return STATUS_ACCESS_DENIED
;
1181 TRACE("%p %08lx %08lx\n", addr
, size
, new_prot
);
1183 /* Fix the parameters */
1185 size
= ROUND_SIZE( addr
, size
);
1186 base
= ROUND_ADDR( addr
, page_mask
);
1188 if (!(view
= VIRTUAL_FindView( base
)) ||
1189 (base
+ size
> (char *)view
->base
+ view
->size
))
1190 return STATUS_INVALID_PARAMETER
;
1192 /* Make sure all the pages are committed */
1194 p
= view
->prot
+ ((base
- (char *)view
->base
) >> page_shift
);
1195 VIRTUAL_GetWin32Prot( *p
, &prot
, NULL
);
1196 for (i
= size
>> page_shift
; i
; i
--, p
++)
1198 if (!(*p
& VPROT_COMMITTED
)) return STATUS_NOT_COMMITTED
;
1201 if (old_prot
) *old_prot
= prot
;
1202 vprot
= VIRTUAL_GetProt( new_prot
) | VPROT_COMMITTED
;
1203 if (!VIRTUAL_SetProt( view
, base
, size
, vprot
)) return STATUS_ACCESS_DENIED
;
1207 return STATUS_SUCCESS
;
1211 /***********************************************************************
1212 * NtQueryVirtualMemory (NTDLL.@)
1213 * ZwQueryVirtualMemory (NTDLL.@)
1215 NTSTATUS WINAPI
NtQueryVirtualMemory( HANDLE process
, LPCVOID addr
,
1216 MEMORY_INFORMATION_CLASS info_class
, PVOID buffer
,
1217 ULONG len
, ULONG
*res_len
)
1220 char *base
, *alloc_base
= 0;
1222 MEMORY_BASIC_INFORMATION
*info
= buffer
;
1224 if (info_class
!= MemoryBasicInformation
) return STATUS_INVALID_INFO_CLASS
;
1225 if (ADDRESS_SPACE_LIMIT
&& addr
>= ADDRESS_SPACE_LIMIT
)
1226 return STATUS_WORKING_SET_LIMIT_RANGE
; /* FIXME */
1228 if (!is_current_process( process
))
1230 ERR("Unsupported on other process\n");
1231 return STATUS_ACCESS_DENIED
;
1234 base
= ROUND_ADDR( addr
, page_mask
);
1236 /* Find the view containing the address */
1238 RtlEnterCriticalSection(&csVirtual
);
1239 view
= VIRTUAL_FirstView
;
1244 size
= (char *)ADDRESS_SPACE_LIMIT
- alloc_base
;
1247 if ((char *)view
->base
> base
)
1249 size
= (char *)view
->base
- alloc_base
;
1253 if ((char *)view
->base
+ view
->size
> base
)
1255 alloc_base
= view
->base
;
1259 alloc_base
= (char *)view
->base
+ view
->size
;
1262 RtlLeaveCriticalSection(&csVirtual
);
1264 /* Fill the info structure */
1268 info
->State
= MEM_FREE
;
1270 info
->AllocationProtect
= 0;
1275 BYTE vprot
= view
->prot
[(base
- alloc_base
) >> page_shift
];
1276 VIRTUAL_GetWin32Prot( vprot
, &info
->Protect
, &info
->State
);
1277 for (size
= base
- alloc_base
; size
< view
->size
; size
+= page_mask
+1)
1278 if (view
->prot
[size
>> page_shift
] != vprot
) break;
1279 VIRTUAL_GetWin32Prot( view
->protect
, &info
->AllocationProtect
, NULL
);
1280 if (view
->protect
& VPROT_IMAGE
) info
->Type
= MEM_IMAGE
;
1281 else if (view
->flags
& VFLAG_VALLOC
) info
->Type
= MEM_PRIVATE
;
1282 else info
->Type
= MEM_MAPPED
;
1285 info
->BaseAddress
= (LPVOID
)base
;
1286 info
->AllocationBase
= (LPVOID
)alloc_base
;
1287 info
->RegionSize
= size
- (base
- alloc_base
);
1288 if (res_len
) *res_len
= sizeof(*info
);
1289 return STATUS_SUCCESS
;
1293 /***********************************************************************
1294 * NtLockVirtualMemory (NTDLL.@)
1295 * ZwLockVirtualMemory (NTDLL.@)
1297 NTSTATUS WINAPI
NtLockVirtualMemory( HANDLE process
, PVOID
*addr
, ULONG
*size
, ULONG unknown
)
1299 if (!is_current_process( process
))
1301 ERR("Unsupported on other process\n");
1302 return STATUS_ACCESS_DENIED
;
1304 return STATUS_SUCCESS
;
1308 /***********************************************************************
1309 * NtUnlockVirtualMemory (NTDLL.@)
1310 * ZwUnlockVirtualMemory (NTDLL.@)
1312 NTSTATUS WINAPI
NtUnlockVirtualMemory( HANDLE process
, PVOID
*addr
, ULONG
*size
, ULONG unknown
)
1314 if (!is_current_process( process
))
1316 ERR("Unsupported on other process\n");
1317 return STATUS_ACCESS_DENIED
;
1319 return STATUS_SUCCESS
;
1323 /***********************************************************************
1324 * NtCreateSection (NTDLL.@)
1325 * ZwCreateSection (NTDLL.@)
1327 NTSTATUS WINAPI
NtCreateSection( HANDLE
*handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
,
1328 const LARGE_INTEGER
*size
, ULONG protect
,
1329 ULONG sec_flags
, HANDLE file
)
1333 DWORD len
= attr
->ObjectName
? attr
->ObjectName
->Length
: 0;
1335 /* Check parameters */
1337 if (len
> MAX_PATH
*sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
1339 vprot
= VIRTUAL_GetProt( protect
);
1340 if (sec_flags
& SEC_RESERVE
)
1342 if (file
) return STATUS_INVALID_PARAMETER
;
1344 else vprot
|= VPROT_COMMITTED
;
1345 if (sec_flags
& SEC_NOCACHE
) vprot
|= VPROT_NOCACHE
;
1346 if (sec_flags
& SEC_IMAGE
) vprot
|= VPROT_IMAGE
;
1348 /* Create the server object */
1350 SERVER_START_REQ( create_mapping
)
1352 req
->file_handle
= file
;
1353 req
->size_high
= size
? size
->u
.HighPart
: 0;
1354 req
->size_low
= size
? size
->u
.LowPart
: 0;
1355 req
->protect
= vprot
;
1356 req
->access
= access
;
1357 req
->inherit
= (attr
->Attributes
& OBJ_INHERIT
) != 0;
1358 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
1359 ret
= wine_server_call( req
);
1360 *handle
= reply
->handle
;
1367 /***********************************************************************
1368 * NtOpenSection (NTDLL.@)
1369 * ZwOpenSection (NTDLL.@)
1371 NTSTATUS WINAPI
NtOpenSection( HANDLE
*handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
)
1374 DWORD len
= attr
->ObjectName
->Length
;
1376 if (len
> MAX_PATH
*sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
1378 SERVER_START_REQ( open_mapping
)
1380 req
->access
= access
;
1381 req
->inherit
= (attr
->Attributes
& OBJ_INHERIT
) != 0;
1382 wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
1383 if (!(ret
= wine_server_call( req
))) *handle
= reply
->handle
;
1390 /***********************************************************************
1391 * NtMapViewOfSection (NTDLL.@)
1392 * ZwMapViewOfSection (NTDLL.@)
1394 NTSTATUS WINAPI
NtMapViewOfSection( HANDLE handle
, HANDLE process
, PVOID
*addr_ptr
, ULONG zero_bits
,
1395 ULONG commit_size
, const LARGE_INTEGER
*offset
, ULONG
*size_ptr
,
1396 SECTION_INHERIT inherit
, ULONG alloc_type
, ULONG protect
)
1401 int flags
= MAP_PRIVATE
;
1402 int unix_handle
= -1;
1404 void *base
, *ptr
= (void *)-1, *ret
;
1405 DWORD size_low
, size_high
, header_size
, shared_size
;
1409 if (!is_current_process( process
))
1411 ERR("Unsupported on other process\n");
1412 return STATUS_ACCESS_DENIED
;
1415 TRACE("handle=%p addr=%p off=%lx%08lx size=%x access=%lx\n",
1416 handle
, *addr_ptr
, offset
->u
.HighPart
, offset
->u
.LowPart
, size
, protect
);
1418 /* Check parameters */
1420 if ((offset
->u
.LowPart
& granularity_mask
) ||
1421 (*addr_ptr
&& ((UINT_PTR
)*addr_ptr
& granularity_mask
)))
1422 return STATUS_INVALID_PARAMETER
;
1424 SERVER_START_REQ( get_mapping_info
)
1426 req
->handle
= handle
;
1427 res
= wine_server_call( req
);
1428 prot
= reply
->protect
;
1430 size_low
= reply
->size_low
;
1431 size_high
= reply
->size_high
;
1432 header_size
= reply
->header_size
;
1433 shared_file
= reply
->shared_file
;
1434 shared_size
= reply
->shared_size
;
1435 removable
= reply
->removable
;
1438 if (res
) return res
;
1440 if ((res
= wine_server_handle_to_fd( handle
, 0, &unix_handle
, NULL
, NULL
))) return res
;
1442 if (prot
& VPROT_IMAGE
)
1448 if ((res
= wine_server_handle_to_fd( shared_file
, GENERIC_READ
, &shared_fd
,
1449 NULL
, NULL
))) goto error
;
1450 res
= map_image( handle
, unix_handle
, base
, size_low
, header_size
,
1451 shared_fd
, removable
, addr_ptr
);
1452 wine_server_release_fd( shared_file
, shared_fd
);
1453 NtClose( shared_file
);
1457 res
= map_image( handle
, unix_handle
, base
, size_low
, header_size
,
1458 -1, removable
, addr_ptr
);
1460 wine_server_release_fd( handle
, unix_handle
);
1461 if (!res
) *size_ptr
= size_low
;
1466 ERR("Sizes larger than 4Gb not supported\n");
1468 if ((offset
->u
.LowPart
>= size_low
) ||
1469 (*size_ptr
> size_low
- offset
->u
.LowPart
))
1471 res
= STATUS_INVALID_PARAMETER
;
1474 if (*size_ptr
) size
= ROUND_SIZE( offset
->u
.LowPart
, *size_ptr
);
1475 else size
= size_low
- offset
->u
.LowPart
;
1481 case PAGE_READWRITE
:
1482 case PAGE_EXECUTE_READWRITE
:
1483 if (!(prot
& VPROT_WRITE
))
1485 res
= STATUS_INVALID_PARAMETER
;
1491 case PAGE_WRITECOPY
:
1493 case PAGE_EXECUTE_READ
:
1494 case PAGE_EXECUTE_WRITECOPY
:
1495 if (prot
& VPROT_READ
) break;
1498 res
= STATUS_INVALID_PARAMETER
;
1502 /* FIXME: If a mapping is created with SEC_RESERVE and a process,
1503 * which has a view of this mapping commits some pages, they will
1504 * appear commited in all other processes, which have the same
1505 * view created. Since we don`t support this yet, we create the
1506 * whole mapping commited.
1508 prot
|= VPROT_COMMITTED
;
1510 /* Reserve a properly aligned area */
1512 if ((res
= anon_mmap_aligned( addr_ptr
, size
, PROT_NONE
, 0 ))) goto error
;
1517 TRACE("handle=%p size=%x offset=%lx\n", handle
, size
, offset
->u
.LowPart
);
1519 ret
= VIRTUAL_mmap( unix_handle
, ptr
, size
, offset
->u
.LowPart
, offset
->u
.HighPart
,
1520 VIRTUAL_GetUnixProt( prot
), flags
| MAP_FIXED
, &removable
);
1523 ERR( "VIRTUAL_mmap %p %x %lx%08lx failed\n",
1524 ptr
, size
, offset
->u
.HighPart
, offset
->u
.LowPart
);
1525 res
= STATUS_NO_MEMORY
; /* FIXME */
1528 if (removable
) handle
= 0; /* don't keep handle open on removable media */
1530 if (!(view
= VIRTUAL_CreateView( ptr
, size
, 0, prot
, handle
)))
1532 res
= STATUS_NO_MEMORY
;
1535 wine_server_release_fd( handle
, unix_handle
);
1537 return STATUS_SUCCESS
;
1540 wine_server_release_fd( handle
, unix_handle
);
1541 if (ptr
!= (void *)-1) munmap( ptr
, size
);
1546 /***********************************************************************
1547 * NtUnmapViewOfSection (NTDLL.@)
1548 * ZwUnmapViewOfSection (NTDLL.@)
1550 NTSTATUS WINAPI
NtUnmapViewOfSection( HANDLE process
, PVOID addr
)
1553 void *base
= ROUND_ADDR( addr
, page_mask
);
1555 if (!is_current_process( process
))
1557 ERR("Unsupported on other process\n");
1558 return STATUS_ACCESS_DENIED
;
1560 if (!(view
= VIRTUAL_FindView( base
)) || (base
!= view
->base
)) return STATUS_INVALID_PARAMETER
;
1561 VIRTUAL_DeleteView( view
);
1562 return STATUS_SUCCESS
;
1566 /***********************************************************************
1567 * NtFlushVirtualMemory (NTDLL.@)
1568 * ZwFlushVirtualMemory (NTDLL.@)
1570 NTSTATUS WINAPI
NtFlushVirtualMemory( HANDLE process
, LPCVOID
*addr_ptr
,
1571 ULONG
*size_ptr
, ULONG unknown
)
1574 void *addr
= ROUND_ADDR( *addr_ptr
, page_mask
);
1576 if (!is_current_process( process
))
1578 ERR("Unsupported on other process\n");
1579 return STATUS_ACCESS_DENIED
;
1581 if (!(view
= VIRTUAL_FindView( addr
))) return STATUS_INVALID_PARAMETER
;
1582 if (!*size_ptr
) *size_ptr
= view
->size
;
1584 if (!msync( addr
, *size_ptr
, MS_SYNC
)) return STATUS_SUCCESS
;
1585 return STATUS_NOT_MAPPED_DATA
;
1589 /***********************************************************************
1590 * NtReadVirtualMemory (NTDLL.@)
1591 * ZwReadVirtualMemory (NTDLL.@)
1593 NTSTATUS WINAPI
NtReadVirtualMemory( HANDLE process
, const void *addr
, void *buffer
,
1594 SIZE_T size
, SIZE_T
*bytes_read
)
1598 SERVER_START_REQ( read_process_memory
)
1600 req
->handle
= process
;
1601 req
->addr
= (void *)addr
;
1602 wine_server_set_reply( req
, buffer
, size
);
1603 if ((status
= wine_server_call( req
))) size
= 0;
1606 if (bytes_read
) *bytes_read
= size
;
1611 /***********************************************************************
1612 * NtWriteVirtualMemory (NTDLL.@)
1613 * ZwWriteVirtualMemory (NTDLL.@)
1615 NTSTATUS WINAPI
NtWriteVirtualMemory( HANDLE process
, void *addr
, const void *buffer
,
1616 SIZE_T size
, SIZE_T
*bytes_written
)
1618 static const unsigned int zero
;
1619 unsigned int first_offset
, last_offset
, first_mask
, last_mask
;
1622 if (!size
) return STATUS_INVALID_PARAMETER
;
1624 /* compute the mask for the first int */
1626 first_offset
= (unsigned int)addr
% sizeof(int);
1627 memset( &first_mask
, 0, first_offset
);
1629 /* compute the mask for the last int */
1630 last_offset
= (size
+ first_offset
) % sizeof(int);
1632 memset( &last_mask
, 0xff, last_offset
? last_offset
: sizeof(int) );
1634 SERVER_START_REQ( write_process_memory
)
1636 req
->handle
= process
;
1637 req
->addr
= (char *)addr
- first_offset
;
1638 req
->first_mask
= first_mask
;
1639 req
->last_mask
= last_mask
;
1640 if (first_offset
) wine_server_add_data( req
, &zero
, first_offset
);
1641 wine_server_add_data( req
, buffer
, size
);
1642 if (last_offset
) wine_server_add_data( req
, &zero
, sizeof(int) - last_offset
);
1644 if ((status
= wine_server_call( req
))) size
= 0;
1647 if (bytes_written
) *bytes_written
= size
;