2 * Win32 virtual memory functions
4 * Copyright 1997 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
42 #include "wine/exception.h"
43 #include "wine/unicode.h"
44 #include "wine/library.h"
48 #include "wine/server.h"
49 #include "msvcrt/excpt.h"
50 #include "wine/debug.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(virtual);
53 WINE_DECLARE_DEBUG_CHANNEL(module
);
62 struct _FV
*next
; /* Next view */
63 struct _FV
*prev
; /* Prev view */
64 void *base
; /* Base address */
65 UINT size
; /* Size in bytes */
66 UINT flags
; /* Allocation flags */
67 HANDLE mapping
; /* Handle to the file mapping */
68 HANDLERPROC handlerProc
; /* Fault handler */
69 LPVOID handlerArg
; /* Fault handler argument */
70 BYTE protect
; /* Protection for all pages at allocation time */
71 BYTE prot
[1]; /* Protection byte for each page */
75 #define VFLAG_SYSTEM 0x01
76 #define VFLAG_VALLOC 0x02 /* allocated by VirtualAlloc */
78 /* Conversion from VPROT_* to Win32 flags */
79 static const BYTE VIRTUAL_Win32Flags
[16] =
81 PAGE_NOACCESS
, /* 0 */
82 PAGE_READONLY
, /* READ */
83 PAGE_READWRITE
, /* WRITE */
84 PAGE_READWRITE
, /* READ | WRITE */
85 PAGE_EXECUTE
, /* EXEC */
86 PAGE_EXECUTE_READ
, /* READ | EXEC */
87 PAGE_EXECUTE_READWRITE
, /* WRITE | EXEC */
88 PAGE_EXECUTE_READWRITE
, /* READ | WRITE | EXEC */
89 PAGE_WRITECOPY
, /* WRITECOPY */
90 PAGE_WRITECOPY
, /* READ | WRITECOPY */
91 PAGE_WRITECOPY
, /* WRITE | WRITECOPY */
92 PAGE_WRITECOPY
, /* READ | WRITE | WRITECOPY */
93 PAGE_EXECUTE_WRITECOPY
, /* EXEC | WRITECOPY */
94 PAGE_EXECUTE_WRITECOPY
, /* READ | EXEC | WRITECOPY */
95 PAGE_EXECUTE_WRITECOPY
, /* WRITE | EXEC | WRITECOPY */
96 PAGE_EXECUTE_WRITECOPY
/* READ | WRITE | EXEC | WRITECOPY */
100 static FILE_VIEW
*VIRTUAL_FirstView
;
101 static CRITICAL_SECTION csVirtual
= CRITICAL_SECTION_INIT("csVirtual");
104 /* These are always the same on an i386, and it will be faster this way */
105 # define page_mask 0xfff
106 # define page_shift 12
107 # define page_size 0x1000
109 static UINT page_shift
;
110 static UINT page_mask
;
111 static UINT page_size
;
112 #endif /* __i386__ */
113 #define granularity_mask 0xffff /* Allocation granularity (usually 64k) */
115 #define ADDRESS_SPACE_LIMIT ((void *)0xc0000000) /* top of the user address space */
117 #define ROUND_ADDR(addr,mask) \
118 ((void *)((UINT_PTR)(addr) & ~(mask)))
120 #define ROUND_SIZE(addr,size) \
121 (((UINT)(size) + ((UINT_PTR)(addr) & page_mask) + page_mask) & ~page_mask)
123 #define VIRTUAL_DEBUG_DUMP_VIEW(view) \
124 if (!TRACE_ON(virtual)); else VIRTUAL_DumpView(view)
126 static LPVOID
VIRTUAL_mmap( int fd
, LPVOID start
, DWORD size
, DWORD offset_low
,
127 DWORD offset_high
, int prot
, int flags
, BOOL
*removable
);
129 /* filter for page-fault exceptions */
130 static WINE_EXCEPTION_FILTER(page_fault
)
132 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION
)
133 return EXCEPTION_EXECUTE_HANDLER
;
134 return EXCEPTION_CONTINUE_SEARCH
;
137 /***********************************************************************
140 static const char *VIRTUAL_GetProtStr( BYTE prot
)
142 static char buffer
[6];
143 buffer
[0] = (prot
& VPROT_COMMITTED
) ? 'c' : '-';
144 buffer
[1] = (prot
& VPROT_GUARD
) ? 'g' : '-';
145 buffer
[2] = (prot
& VPROT_READ
) ? 'r' : '-';
146 buffer
[3] = (prot
& VPROT_WRITE
) ?
147 ((prot
& VPROT_WRITECOPY
) ? 'w' : 'W') : '-';
148 buffer
[4] = (prot
& VPROT_EXEC
) ? 'x' : '-';
154 /***********************************************************************
157 static void VIRTUAL_DumpView( FILE_VIEW
*view
)
160 char *addr
= view
->base
;
161 BYTE prot
= view
->prot
[0];
163 DPRINTF( "View: %p - %p", addr
, addr
+ view
->size
- 1 );
164 if (view
->flags
& VFLAG_SYSTEM
)
165 DPRINTF( " (system)\n" );
166 else if (view
->flags
& VFLAG_VALLOC
)
167 DPRINTF( " (valloc)\n" );
168 else if (view
->mapping
)
169 DPRINTF( " %d\n", view
->mapping
);
171 DPRINTF( " (anonymous)\n");
173 for (count
= i
= 1; i
< view
->size
>> page_shift
; i
++, count
++)
175 if (view
->prot
[i
] == prot
) continue;
176 DPRINTF( " %p - %p %s\n",
177 addr
, addr
+ (count
<< page_shift
) - 1, VIRTUAL_GetProtStr(prot
) );
178 addr
+= (count
<< page_shift
);
179 prot
= view
->prot
[i
];
183 DPRINTF( " %p - %p %s\n",
184 addr
, addr
+ (count
<< page_shift
) - 1, VIRTUAL_GetProtStr(prot
) );
188 /***********************************************************************
191 void VIRTUAL_Dump(void)
194 DPRINTF( "\nDump of all virtual memory views:\n\n" );
195 EnterCriticalSection(&csVirtual
);
196 view
= VIRTUAL_FirstView
;
199 VIRTUAL_DumpView( view
);
202 LeaveCriticalSection(&csVirtual
);
206 /***********************************************************************
209 * Find the view containing a given address.
215 static FILE_VIEW
*VIRTUAL_FindView( const void *addr
) /* [in] Address */
219 EnterCriticalSection(&csVirtual
);
220 view
= VIRTUAL_FirstView
;
223 if (view
->base
> addr
)
228 if ((char*)view
->base
+ view
->size
> (char*)addr
) break;
231 LeaveCriticalSection(&csVirtual
);
236 /***********************************************************************
239 * Create a new view and add it in the linked list.
241 static FILE_VIEW
*VIRTUAL_CreateView( void *base
, UINT size
, UINT flags
,
242 BYTE vprot
, HANDLE mapping
)
244 FILE_VIEW
*view
, *prev
;
246 /* Create the view structure */
248 assert( !((unsigned int)base
& page_mask
) );
249 assert( !(size
& page_mask
) );
251 if (!(view
= (FILE_VIEW
*)malloc( sizeof(*view
) + size
- 1 ))) return NULL
;
253 view
->size
= size
<< page_shift
;
255 view
->mapping
= mapping
;
256 view
->protect
= vprot
;
257 view
->handlerProc
= NULL
;
258 memset( view
->prot
, vprot
, size
);
260 /* Duplicate the mapping handle */
263 !DuplicateHandle( GetCurrentProcess(), view
->mapping
,
264 GetCurrentProcess(), &view
->mapping
,
265 0, FALSE
, DUPLICATE_SAME_ACCESS
))
271 /* Insert it in the linked list */
273 EnterCriticalSection(&csVirtual
);
274 if (!VIRTUAL_FirstView
|| (VIRTUAL_FirstView
->base
> base
))
276 view
->next
= VIRTUAL_FirstView
;
278 if (view
->next
) view
->next
->prev
= view
;
279 VIRTUAL_FirstView
= view
;
283 prev
= VIRTUAL_FirstView
;
284 while (prev
->next
&& (prev
->next
->base
< base
)) prev
= prev
->next
;
285 view
->next
= prev
->next
;
287 if (view
->next
) view
->next
->prev
= view
;
290 LeaveCriticalSection(&csVirtual
);
291 VIRTUAL_DEBUG_DUMP_VIEW( view
);
296 /***********************************************************************
303 static void VIRTUAL_DeleteView(
304 FILE_VIEW
*view
/* [in] View */
306 if (!(view
->flags
& VFLAG_SYSTEM
))
307 munmap( (void *)view
->base
, view
->size
);
308 EnterCriticalSection(&csVirtual
);
309 if (view
->next
) view
->next
->prev
= view
->prev
;
310 if (view
->prev
) view
->prev
->next
= view
->next
;
311 else VIRTUAL_FirstView
= view
->next
;
312 LeaveCriticalSection(&csVirtual
);
313 if (view
->mapping
) NtClose( view
->mapping
);
318 /***********************************************************************
319 * VIRTUAL_GetUnixProt
321 * Convert page protections to protection for mmap/mprotect.
323 static int VIRTUAL_GetUnixProt( BYTE vprot
)
326 if ((vprot
& VPROT_COMMITTED
) && !(vprot
& VPROT_GUARD
))
328 if (vprot
& VPROT_READ
) prot
|= PROT_READ
;
329 if (vprot
& VPROT_WRITE
) prot
|= PROT_WRITE
;
330 if (vprot
& VPROT_WRITECOPY
) prot
|= PROT_WRITE
;
331 if (vprot
& VPROT_EXEC
) prot
|= PROT_EXEC
;
337 /***********************************************************************
338 * VIRTUAL_GetWin32Prot
340 * Convert page protections to Win32 flags.
345 static void VIRTUAL_GetWin32Prot(
346 BYTE vprot
, /* [in] Page protection flags */
347 DWORD
*protect
, /* [out] Location to store Win32 protection flags */
348 DWORD
*state
/* [out] Location to store mem state flag */
351 *protect
= VIRTUAL_Win32Flags
[vprot
& 0x0f];
352 /* if (vprot & VPROT_GUARD) *protect |= PAGE_GUARD;*/
353 if (vprot
& VPROT_NOCACHE
) *protect
|= PAGE_NOCACHE
;
355 if (vprot
& VPROT_GUARD
) *protect
= PAGE_NOACCESS
;
358 if (state
) *state
= (vprot
& VPROT_COMMITTED
) ? MEM_COMMIT
: MEM_RESERVE
;
362 /***********************************************************************
365 * Build page protections from Win32 flags.
368 * Value of page protection flags
370 static BYTE
VIRTUAL_GetProt(
371 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 void *anon_mmap_aligned( void *base
, unsigned int size
, int prot
, int flags
)
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 /* KB: Q125713, 25-SEP-1995, "Common File Mapping Problems and
457 * Platform Differences":
458 * Windows NT: ERROR_INVALID_PARAMETER
459 * Windows 95: ERROR_INVALID_ADDRESS.
461 if (errno
== ENOMEM
) SetLastError( ERROR_OUTOFMEMORY
);
464 if (GetVersion() & 0x80000000) /* win95 */
465 SetLastError( ERROR_INVALID_ADDRESS
);
467 SetLastError( ERROR_INVALID_PARAMETER
);
474 /* Release the extra memory while keeping the range
475 * starting on the granularity boundary. */
476 if ((unsigned int)ptr
& granularity_mask
)
478 unsigned int extra
= granularity_mask
+ 1 - ((unsigned int)ptr
& granularity_mask
);
479 munmap( ptr
, extra
);
480 ptr
= (char *)ptr
+ extra
;
483 if (view_size
> size
)
484 munmap( (char *)ptr
+ size
, view_size
- size
);
486 else if (ptr
!= base
)
488 /* We couldn't get the address we wanted */
489 munmap( ptr
, view_size
);
490 SetLastError( ERROR_INVALID_ADDRESS
);
497 /***********************************************************************
500 * Apply the relocations to a mapped PE image
502 static int do_relocations( char *base
, const IMAGE_DATA_DIRECTORY
*dir
,
503 int delta
, DWORD total_size
)
505 IMAGE_BASE_RELOCATION
*rel
;
507 TRACE_(module
)( "relocating from %p-%p to %p-%p\n",
508 base
- delta
, base
- delta
+ total_size
, base
, base
+ total_size
);
510 for (rel
= (IMAGE_BASE_RELOCATION
*)(base
+ dir
->VirtualAddress
);
511 ((char *)rel
< base
+ dir
->VirtualAddress
+ dir
->Size
) && rel
->SizeOfBlock
;
512 rel
= (IMAGE_BASE_RELOCATION
*)((char*)rel
+ rel
->SizeOfBlock
) )
514 char *page
= base
+ rel
->VirtualAddress
;
515 WORD
*TypeOffset
= (WORD
*)(rel
+ 1);
516 int i
, count
= (rel
->SizeOfBlock
- sizeof(*rel
)) / sizeof(*TypeOffset
);
518 if (!count
) continue;
521 if ((char *)rel
+ rel
->SizeOfBlock
> base
+ dir
->VirtualAddress
+ dir
->Size
||
522 page
> base
+ total_size
)
524 ERR_(module
)("invalid relocation %p,%lx,%ld at %p,%lx,%lx\n",
525 rel
, rel
->VirtualAddress
, rel
->SizeOfBlock
,
526 base
, dir
->VirtualAddress
, dir
->Size
);
530 TRACE_(module
)("%ld relocations for page %lx\n", rel
->SizeOfBlock
, rel
->VirtualAddress
);
532 /* patching in reverse order */
533 for (i
= 0 ; i
< count
; i
++)
535 int offset
= TypeOffset
[i
] & 0xFFF;
536 int type
= TypeOffset
[i
] >> 12;
539 case IMAGE_REL_BASED_ABSOLUTE
:
541 case IMAGE_REL_BASED_HIGH
:
542 *(short*)(page
+offset
) += HIWORD(delta
);
544 case IMAGE_REL_BASED_LOW
:
545 *(short*)(page
+offset
) += LOWORD(delta
);
547 case IMAGE_REL_BASED_HIGHLOW
:
548 *(int*)(page
+offset
) += delta
;
549 /* FIXME: if this is an exported address, fire up enhanced logic */
552 FIXME_(module
)("Unknown/unsupported fixup type %d.\n", type
);
561 /***********************************************************************
564 * Map an executable (PE format) image into memory.
566 static LPVOID
map_image( HANDLE hmapping
, int fd
, char *base
, DWORD total_size
,
567 DWORD header_size
, HANDLE shared_file
, DWORD shared_size
,
570 IMAGE_DOS_HEADER
*dos
;
571 IMAGE_NT_HEADERS
*nt
;
572 IMAGE_SECTION_HEADER
*sec
;
573 IMAGE_DATA_DIRECTORY
*imports
;
575 DWORD err
= GetLastError();
580 SetLastError( ERROR_BAD_EXE_FORMAT
); /* generic error */
582 /* zero-map the whole range */
584 if (base
< (char *)0x110000 || /* make sure the DOS area remains free */
585 (ptr
= wine_anon_mmap( base
, total_size
,
586 PROT_READ
| PROT_WRITE
| PROT_EXEC
, 0 )) == (char *)-1)
588 ptr
= wine_anon_mmap( NULL
, total_size
,
589 PROT_READ
| PROT_WRITE
| PROT_EXEC
, 0 );
590 if (ptr
== (char *)-1)
592 ERR_(module
)("Not enough memory for module (%ld bytes)\n", total_size
);
596 TRACE_(module
)( "mapped PE file at %p-%p\n", ptr
, ptr
+ total_size
);
600 if (VIRTUAL_mmap( fd
, ptr
, header_size
, 0, 0, PROT_READ
,
601 MAP_PRIVATE
| MAP_FIXED
, &removable
) == (char *)-1) goto error
;
602 dos
= (IMAGE_DOS_HEADER
*)ptr
;
603 nt
= (IMAGE_NT_HEADERS
*)(ptr
+ dos
->e_lfanew
);
604 if ((char *)(nt
+ 1) > ptr
+ header_size
) goto error
;
606 sec
= (IMAGE_SECTION_HEADER
*)((char*)&nt
->OptionalHeader
+nt
->FileHeader
.SizeOfOptionalHeader
);
607 if ((char *)(sec
+ nt
->FileHeader
.NumberOfSections
) > ptr
+ header_size
) goto error
;
609 imports
= nt
->OptionalHeader
.DataDirectory
+ IMAGE_DIRECTORY_ENTRY_IMPORT
;
610 if (!imports
->Size
|| !imports
->VirtualAddress
) imports
= NULL
;
612 /* check the architecture */
614 if (nt
->FileHeader
.Machine
!= IMAGE_FILE_MACHINE_I386
)
616 MESSAGE("Trying to load PE image for unsupported architecture (");
617 switch (nt
->FileHeader
.Machine
)
619 case IMAGE_FILE_MACHINE_UNKNOWN
: MESSAGE("Unknown"); break;
620 case IMAGE_FILE_MACHINE_I860
: MESSAGE("I860"); break;
621 case IMAGE_FILE_MACHINE_R3000
: MESSAGE("R3000"); break;
622 case IMAGE_FILE_MACHINE_R4000
: MESSAGE("R4000"); break;
623 case IMAGE_FILE_MACHINE_R10000
: MESSAGE("R10000"); break;
624 case IMAGE_FILE_MACHINE_ALPHA
: MESSAGE("Alpha"); break;
625 case IMAGE_FILE_MACHINE_POWERPC
: MESSAGE("PowerPC"); break;
626 default: MESSAGE("Unknown-%04x", nt
->FileHeader
.Machine
); break;
632 /* retrieve the shared sections file */
636 if ((shared_fd
= FILE_GetUnixHandle( shared_file
, GENERIC_READ
)) == -1) goto error
;
637 CloseHandle( shared_file
); /* we no longer need it */
641 /* map all the sections */
643 for (i
= pos
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
647 /* a few sanity checks */
648 size
= sec
->VirtualAddress
+ ROUND_SIZE( sec
->VirtualAddress
, sec
->Misc
.VirtualSize
);
649 if (sec
->VirtualAddress
> total_size
|| size
> total_size
|| size
< sec
->VirtualAddress
)
651 ERR_(module
)( "Section %.8s too large (%lx+%lx/%lx)\n",
652 sec
->Name
, sec
->VirtualAddress
, sec
->Misc
.VirtualSize
, total_size
);
656 if ((sec
->Characteristics
& IMAGE_SCN_MEM_SHARED
) &&
657 (sec
->Characteristics
& IMAGE_SCN_MEM_WRITE
))
659 size
= ROUND_SIZE( 0, sec
->Misc
.VirtualSize
);
660 TRACE_(module
)( "mapping shared section %.8s at %p off %lx (%x) size %lx (%lx) flags %lx\n",
661 sec
->Name
, ptr
+ sec
->VirtualAddress
,
662 sec
->PointerToRawData
, pos
, sec
->SizeOfRawData
,
663 size
, sec
->Characteristics
);
664 if (VIRTUAL_mmap( shared_fd
, ptr
+ sec
->VirtualAddress
, size
,
665 pos
, 0, PROT_READ
|PROT_WRITE
|PROT_EXEC
,
666 MAP_SHARED
|MAP_FIXED
, NULL
) == (void *)-1)
668 ERR_(module
)( "Could not map shared section %.8s\n", sec
->Name
);
672 /* check if the import directory falls inside this section */
673 if (imports
&& imports
->VirtualAddress
>= sec
->VirtualAddress
&&
674 imports
->VirtualAddress
< sec
->VirtualAddress
+ size
)
676 UINT_PTR base
= imports
->VirtualAddress
& ~page_mask
;
677 UINT_PTR end
= base
+ ROUND_SIZE( imports
->VirtualAddress
, imports
->Size
);
678 if (end
> sec
->VirtualAddress
+ size
) end
= sec
->VirtualAddress
+ size
;
679 if (end
> base
) VIRTUAL_mmap( shared_fd
, ptr
+ base
, end
- base
,
680 pos
, 0, PROT_READ
|PROT_WRITE
|PROT_EXEC
,
681 MAP_PRIVATE
|MAP_FIXED
, NULL
);
687 TRACE_(module
)( "mapping section %.8s at %p off %lx size %lx flags %lx\n",
688 sec
->Name
, ptr
+ sec
->VirtualAddress
,
689 sec
->PointerToRawData
, sec
->SizeOfRawData
,
690 sec
->Characteristics
);
692 if (sec
->Characteristics
& IMAGE_SCN_CNT_UNINITIALIZED_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
);
733 SetLastError( ERROR_BAD_EXE_FORMAT
);
737 /* FIXME: If we need to relocate a system DLL (base > 2GB) we should
738 * really make sure that the *new* base address is also > 2GB.
739 * Some DLLs really check the MSB of the module handle :-/
741 if ((nt
->OptionalHeader
.ImageBase
& 0x80000000) && !((DWORD
)base
& 0x80000000))
742 ERR( "Forced to relocate system DLL (base > 2GB). This is not good.\n" );
744 if (!do_relocations( ptr
, relocs
, ptr
- base
, total_size
))
746 SetLastError( ERROR_BAD_EXE_FORMAT
);
751 if (removable
) hmapping
= 0; /* don't keep handle open on removable media */
752 if (!(view
= VIRTUAL_CreateView( ptr
, total_size
, 0, VPROT_COMMITTED
|VPROT_READ
, hmapping
)))
754 SetLastError( ERROR_OUTOFMEMORY
);
758 /* set the image protections */
760 sec
= (IMAGE_SECTION_HEADER
*)((char *)&nt
->OptionalHeader
+nt
->FileHeader
.SizeOfOptionalHeader
);
761 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
763 DWORD size
= ROUND_SIZE( sec
->VirtualAddress
, sec
->Misc
.VirtualSize
);
764 BYTE vprot
= VPROT_COMMITTED
;
765 if (sec
->Characteristics
& IMAGE_SCN_MEM_READ
) vprot
|= VPROT_READ
;
766 if (sec
->Characteristics
& IMAGE_SCN_MEM_WRITE
) vprot
|= VPROT_WRITE
|VPROT_WRITECOPY
;
767 if (sec
->Characteristics
& IMAGE_SCN_MEM_EXECUTE
) vprot
|= VPROT_EXEC
;
769 /* make sure the import directory is writable */
770 if (imports
&& imports
->VirtualAddress
>= sec
->VirtualAddress
&&
771 imports
->VirtualAddress
< sec
->VirtualAddress
+ size
)
772 vprot
|= VPROT_READ
|VPROT_WRITE
|VPROT_WRITECOPY
;
774 VIRTUAL_SetProt( view
, ptr
+ sec
->VirtualAddress
, size
, vprot
);
777 SetLastError( err
); /* restore last error */
779 if (shared_fd
!= -1) close( shared_fd
);
783 if (ptr
!= (char *)-1) munmap( ptr
, total_size
);
785 if (shared_fd
!= -1) close( shared_fd
);
786 if (shared_file
) CloseHandle( shared_file
);
791 /***********************************************************************
795 DECL_GLOBAL_CONSTRUCTOR(VIRTUAL_Init
)
797 page_size
= getpagesize();
798 page_mask
= page_size
- 1;
799 /* Make sure we have a power of 2 */
800 assert( !(page_size
& page_mask
) );
802 while ((1 << page_shift
) != page_size
) page_shift
++;
804 #endif /* page_mask */
807 /***********************************************************************
808 * VIRTUAL_SetFaultHandler
810 BOOL
VIRTUAL_SetFaultHandler( LPCVOID addr
, HANDLERPROC proc
, LPVOID arg
)
814 if (!(view
= VIRTUAL_FindView( addr
))) return FALSE
;
815 view
->handlerProc
= proc
;
816 view
->handlerArg
= arg
;
820 /***********************************************************************
821 * VIRTUAL_HandleFault
823 DWORD
VIRTUAL_HandleFault( LPCVOID addr
)
825 FILE_VIEW
*view
= VIRTUAL_FindView( addr
);
826 DWORD ret
= EXCEPTION_ACCESS_VIOLATION
;
830 if (view
->handlerProc
)
832 if (view
->handlerProc(view
->handlerArg
, addr
)) ret
= 0; /* handled */
836 BYTE vprot
= view
->prot
[((char *)addr
- (char *)view
->base
) >> page_shift
];
837 void *page
= (void *)((UINT_PTR
)addr
& ~page_mask
);
838 char *stack
= (char *)NtCurrentTeb()->stack_base
+ SIGNAL_STACK_SIZE
+ page_mask
+ 1;
839 if (vprot
& VPROT_GUARD
)
841 VIRTUAL_SetProt( view
, page
, page_mask
+ 1, vprot
& ~VPROT_GUARD
);
842 ret
= STATUS_GUARD_PAGE_VIOLATION
;
844 /* is it inside the stack guard pages? */
845 if (((char *)addr
>= stack
) && ((char *)addr
< stack
+ 2*(page_mask
+1)))
846 ret
= STATUS_STACK_OVERFLOW
;
854 /***********************************************************************
857 * Linux kernels before 2.4.x can support non page-aligned offsets, as
858 * long as the offset is aligned to the filesystem block size. This is
859 * a big performance gain so we want to take advantage of it.
861 * However, when we use 64-bit file support this doesn't work because
862 * glibc rejects unaligned offsets. Also glibc 2.1.3 mmap64 is broken
863 * in that it rounds unaligned offsets down to a page boundary. For
864 * these reasons we do a direct system call here.
866 static void *unaligned_mmap( void *addr
, size_t length
, unsigned int prot
,
867 unsigned int flags
, int fd
, unsigned int offset_low
,
868 unsigned int offset_high
)
870 #if defined(linux) && defined(__i386__) && defined(__GNUC__)
871 if (!offset_high
&& (offset_low
& page_mask
))
886 args
.length
= length
;
890 args
.offset
= offset_low
;
892 __asm__
__volatile__("push %%ebx\n\t"
897 : "0" (90), /* SYS_mmap */
899 if (ret
< 0 && ret
> -4096)
907 return mmap( addr
, length
, prot
, flags
, fd
, ((off_t
)offset_high
<< 32) | offset_low
);
911 /***********************************************************************
914 * Wrapper for mmap() that handles anonymous mappings portably,
915 * and falls back to read if mmap of a file fails.
917 static LPVOID
VIRTUAL_mmap( int fd
, LPVOID start
, DWORD size
,
918 DWORD offset_low
, DWORD offset_high
,
919 int prot
, int flags
, BOOL
*removable
)
924 BOOL is_shared_write
= FALSE
;
926 if (fd
== -1) return wine_anon_mmap( start
, size
, prot
, flags
);
928 if (prot
& PROT_WRITE
)
931 if (flags
& MAP_SHARED
) is_shared_write
= TRUE
;
934 if (!(flags
& MAP_PRIVATE
)) is_shared_write
= TRUE
;
938 if (removable
&& *removable
)
940 /* if on removable media, try using read instead of mmap */
941 if (!is_shared_write
) goto fake_mmap
;
945 if ((ret
= unaligned_mmap( start
, size
, prot
, flags
, fd
,
946 offset_low
, offset_high
)) != (LPVOID
)-1) return ret
;
948 /* mmap() failed; if this is because the file offset is not */
949 /* page-aligned (EINVAL), or because the underlying filesystem */
950 /* does not support mmap() (ENOEXEC,ENODEV), we do it by hand. */
952 if ((errno
!= ENOEXEC
) && (errno
!= EINVAL
) && (errno
!= ENODEV
)) return ret
;
953 if (is_shared_write
) return ret
; /* we cannot fake shared write mappings */
956 /* Reserve the memory with an anonymous mmap */
957 ret
= wine_anon_mmap( start
, size
, PROT_READ
| PROT_WRITE
, flags
);
958 if (ret
== (LPVOID
)-1) return ret
;
959 /* Now read in the file */
960 offset
= ((off_t
)offset_high
<< 32) | offset_low
;
961 if ((pos
= lseek( fd
, offset
, SEEK_SET
)) == -1)
966 read( fd
, ret
, size
);
967 lseek( fd
, pos
, SEEK_SET
); /* Restore the file pointer */
968 mprotect( ret
, size
, prot
); /* Set the right protection */
973 /***********************************************************************
974 * VirtualAlloc (KERNEL32.@)
975 * Reserves or commits a region of pages in virtual address space
978 * Base address of allocated region of pages
981 LPVOID WINAPI
VirtualAlloc(
982 LPVOID addr
, /* [in] Address of region to reserve or commit */
983 DWORD size
, /* [in] Size of region */
984 DWORD type
, /* [in] Type of allocation */
985 DWORD protect
)/* [in] Type of access protection */
991 TRACE("%p %08lx %lx %08lx\n", addr
, size
, type
, protect
);
993 /* Round parameters to a page boundary */
995 if (size
> 0x7fc00000) /* 2Gb - 4Mb */
997 SetLastError( ERROR_OUTOFMEMORY
);
1002 if (type
& MEM_RESERVE
) /* Round down to 64k boundary */
1003 base
= ROUND_ADDR( addr
, granularity_mask
);
1005 base
= ROUND_ADDR( addr
, page_mask
);
1006 size
= (((UINT_PTR
)addr
+ size
+ page_mask
) & ~page_mask
) - (UINT_PTR
)base
;
1008 /* disallow low 64k, wrap-around and kernel space */
1009 if ((base
<= (char *)granularity_mask
) ||
1010 (base
+ size
< base
) ||
1011 (base
+ size
> (char *)ADDRESS_SPACE_LIMIT
))
1013 SetLastError( ERROR_INVALID_PARAMETER
);
1020 size
= (size
+ page_mask
) & ~page_mask
;
1023 if (type
& MEM_TOP_DOWN
) {
1024 /* FIXME: MEM_TOP_DOWN allocates the largest possible address.
1025 * Is there _ANY_ way to do it with UNIX mmap()?
1027 WARN("MEM_TOP_DOWN ignored\n");
1028 type
&= ~MEM_TOP_DOWN
;
1030 /* Compute the alloc type flags */
1032 if (!(type
& (MEM_COMMIT
| MEM_RESERVE
| MEM_SYSTEM
)) ||
1033 (type
& ~(MEM_COMMIT
| MEM_RESERVE
| MEM_SYSTEM
)))
1035 ERR("called with wrong alloc type flags (%08lx) !\n", type
);
1036 SetLastError( ERROR_INVALID_PARAMETER
);
1039 if (type
& (MEM_COMMIT
| MEM_SYSTEM
))
1040 vprot
= VIRTUAL_GetProt( protect
) | VPROT_COMMITTED
;
1043 /* Reserve the memory */
1045 if ((type
& MEM_RESERVE
) || !base
)
1047 if (type
& MEM_SYSTEM
)
1049 if (!(view
= VIRTUAL_CreateView( base
, size
, VFLAG_VALLOC
| VFLAG_SYSTEM
, vprot
, 0 )))
1051 SetLastError( ERROR_OUTOFMEMORY
);
1054 return (LPVOID
)base
;
1056 ptr
= anon_mmap_aligned( base
, size
, VIRTUAL_GetUnixProt( vprot
), 0 );
1057 if (ptr
== (void *)-1) return NULL
;
1059 if (!(view
= VIRTUAL_CreateView( ptr
, size
, VFLAG_VALLOC
, vprot
, 0 )))
1061 munmap( ptr
, size
);
1062 SetLastError( ERROR_OUTOFMEMORY
);
1068 /* Commit the pages */
1070 if (!(view
= VIRTUAL_FindView( base
)) ||
1071 (base
+ size
> (char *)view
->base
+ view
->size
))
1073 SetLastError( ERROR_INVALID_ADDRESS
);
1077 if (!VIRTUAL_SetProt( view
, base
, size
, vprot
)) return NULL
;
1078 return (LPVOID
)base
;
1082 /***********************************************************************
1083 * VirtualAllocEx (KERNEL32.@)
1085 * Seems to be just as VirtualAlloc, but with process handle.
1087 LPVOID WINAPI
VirtualAllocEx(
1088 HANDLE hProcess
, /* [in] Handle of process to do mem operation */
1089 LPVOID addr
, /* [in] Address of region to reserve or commit */
1090 DWORD size
, /* [in] Size of region */
1091 DWORD type
, /* [in] Type of allocation */
1092 DWORD protect
/* [in] Type of access protection */
1094 if (MapProcessHandle( hProcess
) == GetCurrentProcessId())
1095 return VirtualAlloc( addr
, size
, type
, protect
);
1096 ERR("Unsupported on other process\n");
1101 /***********************************************************************
1102 * VirtualFree (KERNEL32.@)
1103 * Release or decommits a region of pages in virtual address space.
1109 BOOL WINAPI
VirtualFree(
1110 LPVOID addr
, /* [in] Address of region of committed pages */
1111 DWORD size
, /* [in] Size of region */
1112 DWORD type
/* [in] Type of operation */
1117 TRACE("%p %08lx %lx\n", addr
, size
, type
);
1119 /* Fix the parameters */
1121 size
= ROUND_SIZE( addr
, size
);
1122 base
= ROUND_ADDR( addr
, page_mask
);
1124 if (!(view
= VIRTUAL_FindView( base
)) ||
1125 (base
+ size
> (char *)view
->base
+ view
->size
) ||
1126 !(view
->flags
& VFLAG_VALLOC
))
1128 SetLastError( ERROR_INVALID_PARAMETER
);
1132 /* Check the type */
1134 if (type
& MEM_SYSTEM
)
1136 view
->flags
|= VFLAG_SYSTEM
;
1137 type
&= ~MEM_SYSTEM
;
1140 if ((type
!= MEM_DECOMMIT
) && (type
!= MEM_RELEASE
))
1142 ERR("called with wrong free type flags (%08lx) !\n", type
);
1143 SetLastError( ERROR_INVALID_PARAMETER
);
1147 /* Free the pages */
1149 if (type
== MEM_RELEASE
)
1151 if (size
|| (base
!= view
->base
))
1153 SetLastError( ERROR_INVALID_PARAMETER
);
1156 VIRTUAL_DeleteView( view
);
1160 /* Decommit the pages by remapping zero-pages instead */
1162 if (wine_anon_mmap( (LPVOID
)base
, size
, VIRTUAL_GetUnixProt(0), MAP_FIXED
) != (LPVOID
)base
)
1163 ERR( "Could not remap pages, expect trouble\n" );
1164 return VIRTUAL_SetProt( view
, base
, size
, 0 );
1168 /***********************************************************************
1169 * VirtualLock (KERNEL32.@)
1170 * Locks the specified region of virtual address space
1173 * Always returns TRUE
1179 BOOL WINAPI
VirtualLock(
1180 LPVOID addr
, /* [in] Address of first byte of range to lock */
1181 DWORD size
/* [in] Number of bytes in range to lock */
1187 /***********************************************************************
1188 * VirtualUnlock (KERNEL32.@)
1189 * Unlocks a range of pages in the virtual address space
1192 * Always returns TRUE
1198 BOOL WINAPI
VirtualUnlock(
1199 LPVOID addr
, /* [in] Address of first byte of range */
1200 DWORD size
/* [in] Number of bytes in range */
1206 /***********************************************************************
1207 * VirtualProtect (KERNEL32.@)
1208 * Changes the access protection on a region of committed pages
1214 BOOL WINAPI
VirtualProtect(
1215 LPVOID addr
, /* [in] Address of region of committed pages */
1216 DWORD size
, /* [in] Size of region */
1217 DWORD new_prot
, /* [in] Desired access protection */
1218 LPDWORD old_prot
/* [out] Address of variable to get old protection */
1226 TRACE("%p %08lx %08lx\n", addr
, size
, new_prot
);
1228 /* Fix the parameters */
1230 size
= ROUND_SIZE( addr
, size
);
1231 base
= ROUND_ADDR( addr
, page_mask
);
1233 if (!(view
= VIRTUAL_FindView( base
)) ||
1234 (base
+ size
> (char *)view
->base
+ view
->size
))
1236 SetLastError( ERROR_INVALID_PARAMETER
);
1240 /* Make sure all the pages are committed */
1242 p
= view
->prot
+ ((base
- (char *)view
->base
) >> page_shift
);
1243 VIRTUAL_GetWin32Prot( *p
, &prot
, NULL
);
1244 for (i
= size
>> page_shift
; i
; i
--, p
++)
1246 if (!(*p
& VPROT_COMMITTED
))
1248 SetLastError( ERROR_INVALID_PARAMETER
);
1253 if (old_prot
) *old_prot
= prot
;
1254 vprot
= VIRTUAL_GetProt( new_prot
) | VPROT_COMMITTED
;
1255 return VIRTUAL_SetProt( view
, base
, size
, vprot
);
1259 /***********************************************************************
1260 * VirtualProtectEx (KERNEL32.@)
1261 * Changes the access protection on a region of committed pages in the
1262 * virtual address space of a specified process
1268 BOOL WINAPI
VirtualProtectEx(
1269 HANDLE handle
, /* [in] Handle of process */
1270 LPVOID addr
, /* [in] Address of region of committed pages */
1271 DWORD size
, /* [in] Size of region */
1272 DWORD new_prot
, /* [in] Desired access protection */
1273 LPDWORD old_prot
/* [out] Address of variable to get old protection */ )
1275 if (MapProcessHandle( handle
) == GetCurrentProcessId())
1276 return VirtualProtect( addr
, size
, new_prot
, old_prot
);
1277 ERR("Unsupported on other process\n");
1282 /***********************************************************************
1283 * VirtualQuery (KERNEL32.@)
1284 * Provides info about a range of pages in virtual address space
1287 * Number of bytes returned in information buffer
1288 * or 0 if addr is >= 0xc0000000 (kernel space).
1290 DWORD WINAPI
VirtualQuery(
1291 LPCVOID addr
, /* [in] Address of region */
1292 LPMEMORY_BASIC_INFORMATION info
, /* [out] Address of info buffer */
1293 DWORD len
/* [in] Size of buffer */
1296 char *base
, *alloc_base
= 0;
1299 if (addr
>= ADDRESS_SPACE_LIMIT
) return 0;
1301 base
= ROUND_ADDR( addr
, page_mask
);
1303 /* Find the view containing the address */
1305 EnterCriticalSection(&csVirtual
);
1306 view
= VIRTUAL_FirstView
;
1311 size
= (char *)ADDRESS_SPACE_LIMIT
- alloc_base
;
1314 if ((char *)view
->base
> base
)
1316 size
= (char *)view
->base
- alloc_base
;
1320 if ((char *)view
->base
+ view
->size
> base
)
1322 alloc_base
= view
->base
;
1326 alloc_base
= (char *)view
->base
+ view
->size
;
1329 LeaveCriticalSection(&csVirtual
);
1331 /* Fill the info structure */
1335 info
->State
= MEM_FREE
;
1337 info
->AllocationProtect
= 0;
1342 BYTE vprot
= view
->prot
[(base
- alloc_base
) >> page_shift
];
1343 VIRTUAL_GetWin32Prot( vprot
, &info
->Protect
, &info
->State
);
1344 for (size
= base
- alloc_base
; size
< view
->size
; size
+= page_mask
+1)
1345 if (view
->prot
[size
>> page_shift
] != vprot
) break;
1346 info
->AllocationProtect
= view
->protect
;
1347 info
->Type
= MEM_PRIVATE
; /* FIXME */
1350 info
->BaseAddress
= (LPVOID
)base
;
1351 info
->AllocationBase
= (LPVOID
)alloc_base
;
1352 info
->RegionSize
= size
- (base
- alloc_base
);
1353 return sizeof(*info
);
1357 /***********************************************************************
1358 * VirtualQueryEx (KERNEL32.@)
1359 * Provides info about a range of pages in virtual address space of a
1363 * Number of bytes returned in information buffer
1365 DWORD WINAPI
VirtualQueryEx(
1366 HANDLE handle
, /* [in] Handle of process */
1367 LPCVOID addr
, /* [in] Address of region */
1368 LPMEMORY_BASIC_INFORMATION info
, /* [out] Address of info buffer */
1369 DWORD len
/* [in] Size of buffer */ )
1371 if (MapProcessHandle( handle
) == GetCurrentProcessId())
1372 return VirtualQuery( addr
, info
, len
);
1373 ERR("Unsupported on other process\n");
1378 /***********************************************************************
1379 * IsBadReadPtr (KERNEL32.@)
1382 * FALSE: Process has read access to entire block
1385 BOOL WINAPI
IsBadReadPtr(
1386 LPCVOID ptr
, /* [in] Address of memory block */
1387 UINT size
) /* [in] Size of block */
1389 if (!size
) return FALSE
; /* handle 0 size case w/o reference */
1392 volatile const char *p
= ptr
;
1396 while (count
> page_size
)
1403 dummy
= p
[count
- 1];
1405 __EXCEPT(page_fault
) { return TRUE
; }
1411 /***********************************************************************
1412 * IsBadWritePtr (KERNEL32.@)
1415 * FALSE: Process has write access to entire block
1418 BOOL WINAPI
IsBadWritePtr(
1419 LPVOID ptr
, /* [in] Address of memory block */
1420 UINT size
) /* [in] Size of block in bytes */
1422 if (!size
) return FALSE
; /* handle 0 size case w/o reference */
1425 volatile char *p
= ptr
;
1428 while (count
> page_size
)
1437 __EXCEPT(page_fault
) { return TRUE
; }
1443 /***********************************************************************
1444 * IsBadHugeReadPtr (KERNEL32.@)
1446 * FALSE: Process has read access to entire block
1449 BOOL WINAPI
IsBadHugeReadPtr(
1450 LPCVOID ptr
, /* [in] Address of memory block */
1451 UINT size
/* [in] Size of block */
1453 return IsBadReadPtr( ptr
, size
);
1457 /***********************************************************************
1458 * IsBadHugeWritePtr (KERNEL32.@)
1460 * FALSE: Process has write access to entire block
1463 BOOL WINAPI
IsBadHugeWritePtr(
1464 LPVOID ptr
, /* [in] Address of memory block */
1465 UINT size
/* [in] Size of block */
1467 return IsBadWritePtr( ptr
, size
);
1471 /***********************************************************************
1472 * IsBadCodePtr (KERNEL32.@)
1475 * FALSE: Process has read access to specified memory
1478 BOOL WINAPI
IsBadCodePtr( FARPROC ptr
) /* [in] Address of function */
1480 return IsBadReadPtr( ptr
, 1 );
1484 /***********************************************************************
1485 * IsBadStringPtrA (KERNEL32.@)
1488 * FALSE: Read access to all bytes in string
1491 BOOL WINAPI
IsBadStringPtrA(
1492 LPCSTR str
, /* [in] Address of string */
1493 UINT max
) /* [in] Maximum size of string */
1497 volatile const char *p
= str
;
1498 while (p
!= str
+ max
) if (!*p
++) break;
1500 __EXCEPT(page_fault
) { return TRUE
; }
1506 /***********************************************************************
1507 * IsBadStringPtrW (KERNEL32.@)
1508 * See IsBadStringPtrA
1510 BOOL WINAPI
IsBadStringPtrW( LPCWSTR str
, UINT max
)
1514 volatile const WCHAR
*p
= str
;
1515 while (p
!= str
+ max
) if (!*p
++) break;
1517 __EXCEPT(page_fault
) { return TRUE
; }
1523 /***********************************************************************
1524 * CreateFileMappingA (KERNEL32.@)
1525 * Creates a named or unnamed file-mapping object for the specified file
1529 * 0: Mapping object does not exist
1532 HANDLE WINAPI
CreateFileMappingA(
1533 HANDLE hFile
, /* [in] Handle of file to map */
1534 SECURITY_ATTRIBUTES
*sa
, /* [in] Optional security attributes*/
1535 DWORD protect
, /* [in] Protection for mapping object */
1536 DWORD size_high
, /* [in] High-order 32 bits of object size */
1537 DWORD size_low
, /* [in] Low-order 32 bits of object size */
1538 LPCSTR name
/* [in] Name of file-mapping object */ )
1540 WCHAR buffer
[MAX_PATH
];
1542 if (!name
) return CreateFileMappingW( hFile
, sa
, protect
, size_high
, size_low
, NULL
);
1544 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
1546 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
1549 return CreateFileMappingW( hFile
, sa
, protect
, size_high
, size_low
, buffer
);
1553 /***********************************************************************
1554 * CreateFileMappingW (KERNEL32.@)
1555 * See CreateFileMappingA
1557 HANDLE WINAPI
CreateFileMappingW( HANDLE hFile
, LPSECURITY_ATTRIBUTES sa
,
1558 DWORD protect
, DWORD size_high
,
1559 DWORD size_low
, LPCWSTR name
)
1563 DWORD len
= name
? strlenW(name
) : 0;
1565 /* Check parameters */
1567 TRACE("(%x,%p,%08lx,%08lx%08lx,%s)\n",
1568 hFile
, sa
, protect
, size_high
, size_low
, debugstr_w(name
) );
1572 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
1576 vprot
= VIRTUAL_GetProt( protect
);
1577 if (protect
& SEC_RESERVE
)
1579 if (hFile
!= INVALID_HANDLE_VALUE
)
1581 SetLastError( ERROR_INVALID_PARAMETER
);
1585 else vprot
|= VPROT_COMMITTED
;
1586 if (protect
& SEC_NOCACHE
) vprot
|= VPROT_NOCACHE
;
1587 if (protect
& SEC_IMAGE
) vprot
|= VPROT_IMAGE
;
1589 /* Create the server object */
1591 if (hFile
== INVALID_HANDLE_VALUE
) hFile
= 0;
1592 SERVER_START_REQ( create_mapping
)
1594 req
->file_handle
= hFile
;
1595 req
->size_high
= size_high
;
1596 req
->size_low
= size_low
;
1597 req
->protect
= vprot
;
1598 req
->inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
1599 wine_server_add_data( req
, name
, len
* sizeof(WCHAR
) );
1601 wine_server_call_err( req
);
1602 ret
= reply
->handle
;
1609 /***********************************************************************
1610 * OpenFileMappingA (KERNEL32.@)
1611 * Opens a named file-mapping object.
1617 HANDLE WINAPI
OpenFileMappingA(
1618 DWORD access
, /* [in] Access mode */
1619 BOOL inherit
, /* [in] Inherit flag */
1620 LPCSTR name
) /* [in] Name of file-mapping object */
1622 WCHAR buffer
[MAX_PATH
];
1624 if (!name
) return OpenFileMappingW( access
, inherit
, NULL
);
1626 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
1628 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
1631 return OpenFileMappingW( access
, inherit
, buffer
);
1635 /***********************************************************************
1636 * OpenFileMappingW (KERNEL32.@)
1637 * See OpenFileMappingA
1639 HANDLE WINAPI
OpenFileMappingW( DWORD access
, BOOL inherit
, LPCWSTR name
)
1642 DWORD len
= name
? strlenW(name
) : 0;
1643 if (len
>= MAX_PATH
)
1645 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
1648 SERVER_START_REQ( open_mapping
)
1650 req
->access
= access
;
1651 req
->inherit
= inherit
;
1652 wine_server_add_data( req
, name
, len
* sizeof(WCHAR
) );
1653 wine_server_call_err( req
);
1654 ret
= reply
->handle
;
1661 /***********************************************************************
1662 * MapViewOfFile (KERNEL32.@)
1663 * Maps a view of a file into the address space
1666 * Starting address of mapped view
1669 LPVOID WINAPI
MapViewOfFile(
1670 HANDLE mapping
, /* [in] File-mapping object to map */
1671 DWORD access
, /* [in] Access mode */
1672 DWORD offset_high
, /* [in] High-order 32 bits of file offset */
1673 DWORD offset_low
, /* [in] Low-order 32 bits of file offset */
1674 DWORD count
/* [in] Number of bytes to map */
1676 return MapViewOfFileEx( mapping
, access
, offset_high
,
1677 offset_low
, count
, NULL
);
1681 /***********************************************************************
1682 * MapViewOfFileEx (KERNEL32.@)
1683 * Maps a view of a file into the address space
1686 * Starting address of mapped view
1689 LPVOID WINAPI
MapViewOfFileEx(
1690 HANDLE handle
, /* [in] File-mapping object to map */
1691 DWORD access
, /* [in] Access mode */
1692 DWORD offset_high
, /* [in] High-order 32 bits of file offset */
1693 DWORD offset_low
, /* [in] Low-order 32 bits of file offset */
1694 DWORD count
, /* [in] Number of bytes to map */
1695 LPVOID addr
/* [in] Suggested starting address for mapped view */
1699 int flags
= MAP_PRIVATE
;
1700 int unix_handle
= -1;
1702 void *base
, *ptr
= (void *)-1, *ret
;
1703 DWORD size_low
, size_high
, header_size
, shared_size
;
1707 /* Check parameters */
1709 if ((offset_low
& granularity_mask
) ||
1710 (addr
&& ((UINT_PTR
)addr
& granularity_mask
)))
1712 SetLastError( ERROR_INVALID_PARAMETER
);
1716 SERVER_START_REQ( get_mapping_info
)
1718 req
->handle
= handle
;
1719 res
= wine_server_call_err( req
);
1720 prot
= reply
->protect
;
1722 size_low
= reply
->size_low
;
1723 size_high
= reply
->size_high
;
1724 header_size
= reply
->header_size
;
1725 shared_file
= reply
->shared_file
;
1726 shared_size
= reply
->shared_size
;
1727 removable
= (reply
->drive_type
== DRIVE_REMOVABLE
||
1728 reply
->drive_type
== DRIVE_CDROM
);
1731 if (res
) goto error
;
1733 if ((unix_handle
= FILE_GetUnixHandle( handle
, 0 )) == -1) goto error
;
1735 if (prot
& VPROT_IMAGE
)
1736 return map_image( handle
, unix_handle
, base
, size_low
, header_size
,
1737 shared_file
, shared_size
, removable
);
1741 ERR("Sizes larger than 4Gb not supported\n");
1743 if ((offset_low
>= size_low
) ||
1744 (count
> size_low
- offset_low
))
1746 SetLastError( ERROR_INVALID_PARAMETER
);
1749 if (count
) size
= ROUND_SIZE( offset_low
, count
);
1750 else size
= size_low
- offset_low
;
1754 case FILE_MAP_ALL_ACCESS
:
1755 case FILE_MAP_WRITE
:
1756 case FILE_MAP_WRITE
| FILE_MAP_READ
:
1757 if (!(prot
& VPROT_WRITE
))
1759 SetLastError( ERROR_INVALID_PARAMETER
);
1766 case FILE_MAP_COPY
| FILE_MAP_READ
:
1767 if (prot
& VPROT_READ
) break;
1770 SetLastError( ERROR_INVALID_PARAMETER
);
1774 /* FIXME: If a mapping is created with SEC_RESERVE and a process,
1775 * which has a view of this mapping commits some pages, they will
1776 * appear commited in all other processes, which have the same
1777 * view created. Since we don`t support this yet, we create the
1778 * whole mapping commited.
1780 prot
|= VPROT_COMMITTED
;
1782 /* Reserve a properly aligned area */
1784 if ((ptr
= anon_mmap_aligned( addr
, size
, PROT_NONE
, 0 )) == (void *)-1) goto error
;
1788 TRACE("handle=%x size=%x offset=%lx\n", handle
, size
, offset_low
);
1790 ret
= VIRTUAL_mmap( unix_handle
, ptr
, size
, offset_low
, offset_high
,
1791 VIRTUAL_GetUnixProt( prot
), flags
| MAP_FIXED
, &removable
);
1794 ERR( "VIRTUAL_mmap %p %x %lx%08lx failed\n", ptr
, size
, offset_high
, offset_low
);
1797 if (removable
) handle
= 0; /* don't keep handle open on removable media */
1799 if (!(view
= VIRTUAL_CreateView( ptr
, size
, 0, prot
, handle
)))
1801 SetLastError( ERROR_OUTOFMEMORY
);
1804 if (unix_handle
!= -1) close( unix_handle
);
1808 if (unix_handle
!= -1) close( unix_handle
);
1809 if (ptr
!= (void *)-1) munmap( ptr
, size
);
1814 /***********************************************************************
1815 * FlushViewOfFile (KERNEL32.@)
1816 * Writes to the disk a byte range within a mapped view of a file
1822 BOOL WINAPI
FlushViewOfFile(
1823 LPCVOID base
, /* [in] Start address of byte range to flush */
1824 DWORD cbFlush
/* [in] Number of bytes in range */
1827 void *addr
= ROUND_ADDR( base
, page_mask
);
1829 TRACE("FlushViewOfFile at %p for %ld bytes\n",
1832 if (!(view
= VIRTUAL_FindView( addr
)))
1834 SetLastError( ERROR_INVALID_PARAMETER
);
1837 if (!cbFlush
) cbFlush
= view
->size
;
1838 if (!msync( addr
, cbFlush
, MS_SYNC
)) return TRUE
;
1839 SetLastError( ERROR_INVALID_PARAMETER
);
1844 /***********************************************************************
1845 * UnmapViewOfFile (KERNEL32.@)
1846 * Unmaps a mapped view of a file.
1849 * Should addr be an LPCVOID?
1855 BOOL WINAPI
UnmapViewOfFile(
1856 LPVOID addr
/* [in] Address where mapped view begins */
1859 void *base
= ROUND_ADDR( addr
, page_mask
);
1860 if (!(view
= VIRTUAL_FindView( base
)) || (base
!= view
->base
))
1862 SetLastError( ERROR_INVALID_PARAMETER
);
1865 VIRTUAL_DeleteView( view
);