2 * Win32 virtual memory functions
4 * Copyright 1997 Alexandre Julliard
11 #ifdef HAVE_SYS_ERRNO_H
12 #include <sys/errno.h>
19 #include <sys/types.h>
20 #ifdef HAVE_SYS_MMAN_H
24 #include "wine/exception.h"
30 #include "debugtools.h"
32 DEFAULT_DEBUG_CHANNEL(virtual);
33 DECLARE_DEBUG_CHANNEL(module
);
42 struct _FV
*next
; /* Next view */
43 struct _FV
*prev
; /* Prev view */
44 UINT base
; /* Base address */
45 UINT size
; /* Size in bytes */
46 UINT flags
; /* Allocation flags */
47 HANDLE mapping
; /* Handle to the file mapping */
48 HANDLERPROC handlerProc
; /* Fault handler */
49 LPVOID handlerArg
; /* Fault handler argument */
50 BYTE protect
; /* Protection for all pages at allocation time */
51 BYTE prot
[1]; /* Protection byte for each page */
55 #define VFLAG_SYSTEM 0x01
57 /* Conversion from VPROT_* to Win32 flags */
58 static const BYTE VIRTUAL_Win32Flags
[16] =
60 PAGE_NOACCESS
, /* 0 */
61 PAGE_READONLY
, /* READ */
62 PAGE_READWRITE
, /* WRITE */
63 PAGE_READWRITE
, /* READ | WRITE */
64 PAGE_EXECUTE
, /* EXEC */
65 PAGE_EXECUTE_READ
, /* READ | EXEC */
66 PAGE_EXECUTE_READWRITE
, /* WRITE | EXEC */
67 PAGE_EXECUTE_READWRITE
, /* READ | WRITE | EXEC */
68 PAGE_WRITECOPY
, /* WRITECOPY */
69 PAGE_WRITECOPY
, /* READ | WRITECOPY */
70 PAGE_WRITECOPY
, /* WRITE | WRITECOPY */
71 PAGE_WRITECOPY
, /* READ | WRITE | WRITECOPY */
72 PAGE_EXECUTE_WRITECOPY
, /* EXEC | WRITECOPY */
73 PAGE_EXECUTE_WRITECOPY
, /* READ | EXEC | WRITECOPY */
74 PAGE_EXECUTE_WRITECOPY
, /* WRITE | EXEC | WRITECOPY */
75 PAGE_EXECUTE_WRITECOPY
/* READ | WRITE | EXEC | WRITECOPY */
79 static FILE_VIEW
*VIRTUAL_FirstView
;
82 /* These are always the same on an i386, and it will be faster this way */
83 # define page_mask 0xfff
84 # define page_shift 12
86 static UINT page_shift
;
87 static UINT page_mask
;
89 #define granularity_mask 0xffff /* Allocation granularity (usually 64k) */
91 #define ROUND_ADDR(addr) \
92 ((UINT)(addr) & ~page_mask)
94 #define ROUND_SIZE(addr,size) \
95 (((UINT)(size) + ((UINT)(addr) & page_mask) + page_mask) & ~page_mask)
97 #define VIRTUAL_DEBUG_DUMP_VIEW(view) \
98 if (!TRACE_ON(virtual)); else VIRTUAL_DumpView(view)
101 /* filter for page-fault exceptions */
102 static WINE_EXCEPTION_FILTER(page_fault
)
104 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION
)
105 return EXCEPTION_EXECUTE_HANDLER
;
106 return EXCEPTION_CONTINUE_SEARCH
;
109 /***********************************************************************
112 static const char *VIRTUAL_GetProtStr( BYTE prot
)
114 static char buffer
[6];
115 buffer
[0] = (prot
& VPROT_COMMITTED
) ? 'c' : '-';
116 buffer
[1] = (prot
& VPROT_GUARD
) ? 'g' : '-';
117 buffer
[2] = (prot
& VPROT_READ
) ? 'r' : '-';
118 buffer
[3] = (prot
& VPROT_WRITE
) ?
119 ((prot
& VPROT_WRITECOPY
) ? 'w' : 'W') : '-';
120 buffer
[4] = (prot
& VPROT_EXEC
) ? 'x' : '-';
126 /***********************************************************************
129 static void VIRTUAL_DumpView( FILE_VIEW
*view
)
132 UINT addr
= view
->base
;
133 BYTE prot
= view
->prot
[0];
135 DPRINTF( "View: %08x - %08x%s",
136 view
->base
, view
->base
+ view
->size
- 1,
137 (view
->flags
& VFLAG_SYSTEM
) ? " (system)" : "" );
139 DPRINTF( " %d\n", view
->mapping
);
141 DPRINTF( " (anonymous)\n");
143 for (count
= i
= 1; i
< view
->size
>> page_shift
; i
++, count
++)
145 if (view
->prot
[i
] == prot
) continue;
146 DPRINTF( " %08x - %08x %s\n",
147 addr
, addr
+ (count
<< page_shift
) - 1,
148 VIRTUAL_GetProtStr(prot
) );
149 addr
+= (count
<< page_shift
);
150 prot
= view
->prot
[i
];
154 DPRINTF( " %08x - %08x %s\n",
155 addr
, addr
+ (count
<< page_shift
) - 1,
156 VIRTUAL_GetProtStr(prot
) );
160 /***********************************************************************
163 void VIRTUAL_Dump(void)
165 FILE_VIEW
*view
= VIRTUAL_FirstView
;
166 DPRINTF( "\nDump of all virtual memory views:\n\n" );
169 VIRTUAL_DumpView( view
);
175 /***********************************************************************
178 * Find the view containing a given address.
184 static FILE_VIEW
*VIRTUAL_FindView(
185 UINT addr
/* [in] Address */
187 FILE_VIEW
*view
= VIRTUAL_FirstView
;
190 if (view
->base
> addr
) return NULL
;
191 if (view
->base
+ view
->size
> addr
) return view
;
198 /***********************************************************************
201 * Create a new view and add it in the linked list.
203 static FILE_VIEW
*VIRTUAL_CreateView( UINT base
, UINT size
, UINT flags
,
204 BYTE vprot
, HANDLE mapping
)
206 FILE_VIEW
*view
, *prev
;
208 /* Create the view structure */
210 assert( !(base
& page_mask
) );
211 assert( !(size
& page_mask
) );
213 if (!(view
= (FILE_VIEW
*)malloc( sizeof(*view
) + size
- 1 ))) return NULL
;
215 view
->size
= size
<< page_shift
;
217 view
->mapping
= mapping
;
218 view
->protect
= vprot
;
219 view
->handlerProc
= NULL
;
220 memset( view
->prot
, vprot
, size
);
222 /* Duplicate the mapping handle */
224 if ((view
->mapping
!= -1) &&
225 !DuplicateHandle( GetCurrentProcess(), view
->mapping
,
226 GetCurrentProcess(), &view
->mapping
,
227 0, FALSE
, DUPLICATE_SAME_ACCESS
))
233 /* Insert it in the linked list */
235 if (!VIRTUAL_FirstView
|| (VIRTUAL_FirstView
->base
> base
))
237 view
->next
= VIRTUAL_FirstView
;
239 if (view
->next
) view
->next
->prev
= view
;
240 VIRTUAL_FirstView
= view
;
244 prev
= VIRTUAL_FirstView
;
245 while (prev
->next
&& (prev
->next
->base
< base
)) prev
= prev
->next
;
246 view
->next
= prev
->next
;
248 if (view
->next
) view
->next
->prev
= view
;
251 VIRTUAL_DEBUG_DUMP_VIEW( view
);
256 /***********************************************************************
263 static void VIRTUAL_DeleteView(
264 FILE_VIEW
*view
/* [in] View */
266 if (!(view
->flags
& VFLAG_SYSTEM
))
267 FILE_munmap( (void *)view
->base
, 0, view
->size
);
268 if (view
->next
) view
->next
->prev
= view
->prev
;
269 if (view
->prev
) view
->prev
->next
= view
->next
;
270 else VIRTUAL_FirstView
= view
->next
;
271 if (view
->mapping
) CloseHandle( view
->mapping
);
276 /***********************************************************************
277 * VIRTUAL_GetUnixProt
279 * Convert page protections to protection for mmap/mprotect.
281 static int VIRTUAL_GetUnixProt( BYTE vprot
)
284 if ((vprot
& VPROT_COMMITTED
) && !(vprot
& VPROT_GUARD
))
286 if (vprot
& VPROT_READ
) prot
|= PROT_READ
;
287 if (vprot
& VPROT_WRITE
) prot
|= PROT_WRITE
;
288 if (vprot
& VPROT_WRITECOPY
) prot
|= PROT_WRITE
;
289 if (vprot
& VPROT_EXEC
) prot
|= PROT_EXEC
;
295 /***********************************************************************
296 * VIRTUAL_GetWin32Prot
298 * Convert page protections to Win32 flags.
303 static void VIRTUAL_GetWin32Prot(
304 BYTE vprot
, /* [in] Page protection flags */
305 DWORD
*protect
, /* [out] Location to store Win32 protection flags */
306 DWORD
*state
/* [out] Location to store mem state flag */
309 *protect
= VIRTUAL_Win32Flags
[vprot
& 0x0f];
310 /* if (vprot & VPROT_GUARD) *protect |= PAGE_GUARD;*/
311 if (vprot
& VPROT_NOCACHE
) *protect
|= PAGE_NOCACHE
;
313 if (vprot
& VPROT_GUARD
) *protect
= PAGE_NOACCESS
;
316 if (state
) *state
= (vprot
& VPROT_COMMITTED
) ? MEM_COMMIT
: MEM_RESERVE
;
320 /***********************************************************************
323 * Build page protections from Win32 flags.
326 * Value of page protection flags
328 static BYTE
VIRTUAL_GetProt(
329 DWORD protect
/* [in] Win32 protection flags */
333 switch(protect
& 0xff)
339 vprot
= VPROT_READ
| VPROT_WRITE
;
342 vprot
= VPROT_READ
| VPROT_WRITE
| VPROT_WRITECOPY
;
347 case PAGE_EXECUTE_READ
:
348 vprot
= VPROT_EXEC
| VPROT_READ
;
350 case PAGE_EXECUTE_READWRITE
:
351 vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITE
;
353 case PAGE_EXECUTE_WRITECOPY
:
354 vprot
= VPROT_EXEC
| VPROT_READ
| VPROT_WRITE
| VPROT_WRITECOPY
;
361 if (protect
& PAGE_GUARD
) vprot
|= VPROT_GUARD
;
362 if (protect
& PAGE_NOCACHE
) vprot
|= VPROT_NOCACHE
;
367 /***********************************************************************
370 * Change the protection of a range of pages.
376 static BOOL
VIRTUAL_SetProt(
377 FILE_VIEW
*view
, /* [in] Pointer to view */
378 UINT base
, /* [in] Starting address */
379 UINT size
, /* [in] Size in bytes */
380 BYTE vprot
/* [in] Protections to use */
382 TRACE("%08x-%08x %s\n",
383 base
, base
+ size
- 1, VIRTUAL_GetProtStr( vprot
) );
385 if (mprotect( (void *)base
, size
, VIRTUAL_GetUnixProt(vprot
) ))
386 return FALSE
; /* FIXME: last error */
388 memset( view
->prot
+ ((base
- view
->base
) >> page_shift
),
389 vprot
, size
>> page_shift
);
390 VIRTUAL_DEBUG_DUMP_VIEW( view
);
395 /***********************************************************************
398 * Map an executable (PE format) image into memory.
400 static LPVOID
map_image( HANDLE hmapping
, int fd
, char *base
, DWORD total_size
,
401 DWORD header_size
, HANDLE shared_file
, DWORD shared_size
)
403 IMAGE_DOS_HEADER
*dos
;
404 IMAGE_NT_HEADERS
*nt
;
405 IMAGE_SECTION_HEADER
*sec
;
407 DWORD err
= GetLastError();
408 FILE_VIEW
*view
= NULL
;
412 SetLastError( ERROR_BAD_EXE_FORMAT
); /* generic error */
414 /* zero-map the whole range */
416 if ((ptr
= FILE_dommap( -1, base
, 0, total_size
, 0, 0, PROT_READ
|PROT_WRITE
|PROT_EXEC
,
417 MAP_PRIVATE
)) == (char *)-1)
419 ptr
= FILE_dommap( -1, NULL
, 0, total_size
, 0, 0,
420 PROT_READ
|PROT_WRITE
|PROT_EXEC
, MAP_PRIVATE
);
421 if (ptr
== (char *)-1)
423 ERR_(module
)("Not enough memory for module (%ld bytes)\n", total_size
);
427 TRACE_(module
)( "mapped PE file at %p-%p\n", ptr
, ptr
+ total_size
);
429 if (!(view
= VIRTUAL_CreateView( (UINT
)ptr
, total_size
, 0,
430 VPROT_COMMITTED
|VPROT_READ
|VPROT_WRITE
|VPROT_WRITECOPY
,
433 FILE_munmap( ptr
, 0, total_size
);
434 SetLastError( ERROR_OUTOFMEMORY
);
440 if (FILE_dommap( fd
, ptr
, 0, header_size
, 0, 0,
441 PROT_READ
| PROT_WRITE
, MAP_PRIVATE
| MAP_FIXED
) == (char *)-1) goto error
;
442 dos
= (IMAGE_DOS_HEADER
*)ptr
;
443 nt
= (IMAGE_NT_HEADERS
*)(ptr
+ dos
->e_lfanew
);
444 if ((char *)(nt
+ 1) > ptr
+ header_size
) goto error
;
446 sec
= (IMAGE_SECTION_HEADER
*)((char*)&nt
->OptionalHeader
+nt
->FileHeader
.SizeOfOptionalHeader
);
447 if ((char *)(sec
+ nt
->FileHeader
.NumberOfSections
) > ptr
+ header_size
) goto error
;
449 /* check the architecture */
451 if (nt
->FileHeader
.Machine
!= IMAGE_FILE_MACHINE_I386
)
453 MESSAGE("Trying to load PE image for unsupported architecture (");
454 switch (nt
->FileHeader
.Machine
)
456 case IMAGE_FILE_MACHINE_UNKNOWN
: MESSAGE("Unknown"); break;
457 case IMAGE_FILE_MACHINE_I860
: MESSAGE("I860"); break;
458 case IMAGE_FILE_MACHINE_R3000
: MESSAGE("R3000"); break;
459 case IMAGE_FILE_MACHINE_R4000
: MESSAGE("R4000"); break;
460 case IMAGE_FILE_MACHINE_R10000
: MESSAGE("R10000"); break;
461 case IMAGE_FILE_MACHINE_ALPHA
: MESSAGE("Alpha"); break;
462 case IMAGE_FILE_MACHINE_POWERPC
: MESSAGE("PowerPC"); break;
463 default: MESSAGE("Unknown-%04x", nt
->FileHeader
.Machine
); break;
469 /* retrieve the shared sections file */
473 struct get_read_fd_request
*req
= get_req_buffer();
474 req
->handle
= shared_file
;
475 server_call_fd( REQ_GET_READ_FD
, -1, &shared_fd
);
476 if (shared_fd
== -1) goto error
;
477 CloseHandle( shared_file
); /* we no longer need it */
478 shared_file
= INVALID_HANDLE_VALUE
;
481 /* map all the sections */
483 for (i
= pos
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
487 /* a few sanity checks */
488 size
= sec
->VirtualAddress
+ ROUND_SIZE( sec
->VirtualAddress
, sec
->Misc
.VirtualSize
);
489 if (sec
->VirtualAddress
> total_size
|| size
> total_size
|| size
< sec
->VirtualAddress
)
491 ERR_(module
)( "Section %.8s too large (%lx+%lx/%lx)\n",
492 sec
->Name
, sec
->VirtualAddress
, sec
->Misc
.VirtualSize
, total_size
);
496 if ((sec
->Characteristics
& IMAGE_SCN_MEM_SHARED
) &&
497 (sec
->Characteristics
& IMAGE_SCN_MEM_WRITE
))
499 size
= ROUND_SIZE( 0, sec
->Misc
.VirtualSize
);
500 TRACE_(module
)( "mapping shared section %.8s at %p off %lx (%x) size %lx (%lx) flags %lx\n",
501 sec
->Name
, (char *)ptr
+ sec
->VirtualAddress
,
502 sec
->PointerToRawData
, pos
, sec
->SizeOfRawData
,
503 size
, sec
->Characteristics
);
504 if (FILE_dommap( shared_fd
, (char *)ptr
+ sec
->VirtualAddress
, 0, size
,
505 0, pos
, PROT_READ
|PROT_WRITE
|PROT_EXEC
,
506 MAP_SHARED
|MAP_FIXED
) == (void *)-1)
508 ERR_(module
)( "Could not map shared section %.8s\n", sec
->Name
);
515 if (sec
->Characteristics
& IMAGE_SCN_CNT_UNINITIALIZED_DATA
) continue;
516 if (!sec
->PointerToRawData
|| !sec
->SizeOfRawData
) continue;
518 TRACE_(module
)( "mapping section %.8s at %p off %lx size %lx flags %lx\n",
519 sec
->Name
, (char *)ptr
+ sec
->VirtualAddress
,
520 sec
->PointerToRawData
, sec
->SizeOfRawData
,
521 sec
->Characteristics
);
523 /* Note: if the section is not aligned properly FILE_dommap will magically
524 * fall back to read(), so we don't need to check anything here.
526 if (FILE_dommap( fd
, (char *)ptr
+ sec
->VirtualAddress
, 0, sec
->SizeOfRawData
,
527 0, sec
->PointerToRawData
, PROT_READ
|PROT_WRITE
|PROT_EXEC
,
528 MAP_PRIVATE
| MAP_FIXED
) == (void *)-1)
530 ERR_(module
)( "Could not map section %.8s, file probably truncated\n", sec
->Name
);
534 if ((sec
->SizeOfRawData
< sec
->Misc
.VirtualSize
) && (sec
->SizeOfRawData
& page_mask
))
536 DWORD end
= ROUND_SIZE( 0, sec
->SizeOfRawData
);
537 if (end
> sec
->Misc
.VirtualSize
) end
= sec
->Misc
.VirtualSize
;
538 TRACE_(module
)("clearing %p - %p\n",
539 (char *)ptr
+ sec
->VirtualAddress
+ sec
->SizeOfRawData
,
540 (char *)ptr
+ sec
->VirtualAddress
+ end
);
541 memset( (char *)ptr
+ sec
->VirtualAddress
+ sec
->SizeOfRawData
, 0,
542 end
- sec
->SizeOfRawData
);
546 SetLastError( err
); /* restore last error */
548 if (shared_fd
!= -1) close( shared_fd
);
552 if (view
) VIRTUAL_DeleteView( view
);
554 if (shared_fd
!= -1) close( shared_fd
);
555 if (shared_file
!= INVALID_HANDLE_VALUE
) CloseHandle( shared_file
);
560 /***********************************************************************
564 DECL_GLOBAL_CONSTRUCTOR(VIRTUAL_Init
)
568 # ifdef HAVE_GETPAGESIZE
569 page_size
= getpagesize();
572 page_size
= sysconf(_SC_PAGESIZE
);
574 # error Cannot get the page size on this platform
577 page_mask
= page_size
- 1;
578 /* Make sure we have a power of 2 */
579 assert( !(page_size
& page_mask
) );
581 while ((1 << page_shift
) != page_size
) page_shift
++;
583 #endif /* page_mask */
586 /***********************************************************************
587 * VIRTUAL_GetPageSize
589 DWORD
VIRTUAL_GetPageSize(void)
591 return 1 << page_shift
;
595 /***********************************************************************
596 * VIRTUAL_GetGranularity
598 DWORD
VIRTUAL_GetGranularity(void)
600 return granularity_mask
+ 1;
604 /***********************************************************************
605 * VIRTUAL_SetFaultHandler
607 BOOL
VIRTUAL_SetFaultHandler( LPCVOID addr
, HANDLERPROC proc
, LPVOID arg
)
611 if (!(view
= VIRTUAL_FindView((UINT
)addr
))) return FALSE
;
612 view
->handlerProc
= proc
;
613 view
->handlerArg
= arg
;
617 /***********************************************************************
618 * VIRTUAL_HandleFault
620 DWORD
VIRTUAL_HandleFault( LPCVOID addr
)
622 FILE_VIEW
*view
= VIRTUAL_FindView((UINT
)addr
);
623 DWORD ret
= EXCEPTION_ACCESS_VIOLATION
;
627 if (view
->handlerProc
)
629 if (view
->handlerProc(view
->handlerArg
, addr
)) ret
= 0; /* handled */
633 BYTE vprot
= view
->prot
[((UINT
)addr
- view
->base
) >> page_shift
];
634 UINT page
= (UINT
)addr
& ~page_mask
;
635 char *stack
= (char *)NtCurrentTeb()->stack_base
+ SIGNAL_STACK_SIZE
+ page_mask
+ 1;
636 if (vprot
& VPROT_GUARD
)
638 VIRTUAL_SetProt( view
, page
, page_mask
+ 1, vprot
& ~VPROT_GUARD
);
639 ret
= STATUS_GUARD_PAGE_VIOLATION
;
641 /* is it inside the stack guard pages? */
642 if (((char *)addr
>= stack
) && ((char *)addr
< stack
+ 2*(page_mask
+1)))
643 ret
= STATUS_STACK_OVERFLOW
;
650 /***********************************************************************
651 * VirtualAlloc (KERNEL32.548)
652 * Reserves or commits a region of pages in virtual address space
655 * Base address of allocated region of pages
658 LPVOID WINAPI
VirtualAlloc(
659 LPVOID addr
, /* [in] Address of region to reserve or commit */
660 DWORD size
, /* [in] Size of region */
661 DWORD type
, /* [in] Type of allocation */
662 DWORD protect
/* [in] Type of access protection */
665 UINT base
, ptr
, view_size
;
668 TRACE("%08x %08lx %lx %08lx\n",
669 (UINT
)addr
, size
, type
, protect
);
671 /* Round parameters to a page boundary */
673 if (size
> 0x7fc00000) /* 2Gb - 4Mb */
675 SetLastError( ERROR_OUTOFMEMORY
);
680 if (type
& MEM_RESERVE
) /* Round down to 64k boundary */
681 base
= (UINT
)addr
& ~granularity_mask
;
683 base
= ROUND_ADDR( addr
);
684 size
= (((UINT
)addr
+ size
+ page_mask
) & ~page_mask
) - base
;
685 if ((base
<= granularity_mask
) || (base
+ size
< base
))
687 /* disallow low 64k and wrap-around */
688 SetLastError( ERROR_INVALID_PARAMETER
);
695 size
= (size
+ page_mask
) & ~page_mask
;
698 if (type
& MEM_TOP_DOWN
) {
699 /* FIXME: MEM_TOP_DOWN allocates the largest possible address.
700 * Is there _ANY_ way to do it with UNIX mmap()?
702 WARN("MEM_TOP_DOWN ignored\n");
703 type
&= ~MEM_TOP_DOWN
;
705 /* Compute the protection flags */
707 if (!(type
& (MEM_COMMIT
| MEM_RESERVE
| MEM_SYSTEM
)) ||
708 (type
& ~(MEM_COMMIT
| MEM_RESERVE
| MEM_SYSTEM
)))
710 SetLastError( ERROR_INVALID_PARAMETER
);
713 if (type
& (MEM_COMMIT
| MEM_SYSTEM
))
714 vprot
= VIRTUAL_GetProt( protect
) | VPROT_COMMITTED
;
717 /* Reserve the memory */
719 if ((type
& MEM_RESERVE
) || !base
)
721 view_size
= size
+ (base
? 0 : granularity_mask
+ 1);
722 if (type
& MEM_SYSTEM
)
725 ptr
= (UINT
)FILE_dommap( -1, (LPVOID
)base
, 0, view_size
, 0, 0,
726 VIRTUAL_GetUnixProt( vprot
), MAP_PRIVATE
);
729 SetLastError( ERROR_OUTOFMEMORY
);
734 /* Release the extra memory while keeping the range */
735 /* starting on a 64k boundary. */
737 if (ptr
& granularity_mask
)
739 UINT extra
= granularity_mask
+ 1 - (ptr
& granularity_mask
);
740 FILE_munmap( (void *)ptr
, 0, extra
);
744 if (view_size
> size
)
745 FILE_munmap( (void *)(ptr
+ size
), 0, view_size
- size
);
747 else if (ptr
!= base
)
749 /* We couldn't get the address we wanted */
750 FILE_munmap( (void *)ptr
, 0, view_size
);
751 SetLastError( ERROR_INVALID_ADDRESS
);
754 if (!(view
= VIRTUAL_CreateView( ptr
, size
, (type
& MEM_SYSTEM
) ?
755 VFLAG_SYSTEM
: 0, vprot
, -1 )))
757 FILE_munmap( (void *)ptr
, 0, size
);
758 SetLastError( ERROR_OUTOFMEMORY
);
761 VIRTUAL_DEBUG_DUMP_VIEW( view
);
765 /* Commit the pages */
767 if (!(view
= VIRTUAL_FindView( base
)) ||
768 (base
+ size
> view
->base
+ view
->size
))
770 SetLastError( ERROR_INVALID_ADDRESS
);
774 if (!VIRTUAL_SetProt( view
, base
, size
, vprot
)) return NULL
;
779 /***********************************************************************
780 * VirtualFree (KERNEL32.550)
781 * Release or decommits a region of pages in virtual address space.
787 BOOL WINAPI
VirtualFree(
788 LPVOID addr
, /* [in] Address of region of committed pages */
789 DWORD size
, /* [in] Size of region */
790 DWORD type
/* [in] Type of operation */
795 TRACE("%08x %08lx %lx\n",
796 (UINT
)addr
, size
, type
);
798 /* Fix the parameters */
800 size
= ROUND_SIZE( addr
, size
);
801 base
= ROUND_ADDR( addr
);
803 if (!(view
= VIRTUAL_FindView( base
)) ||
804 (base
+ size
> view
->base
+ view
->size
))
806 SetLastError( ERROR_INVALID_PARAMETER
);
810 /* Compute the protection flags */
812 if ((type
!= MEM_DECOMMIT
) && (type
!= MEM_RELEASE
))
814 SetLastError( ERROR_INVALID_PARAMETER
);
820 if (type
== MEM_RELEASE
)
822 if (size
|| (base
!= view
->base
))
824 SetLastError( ERROR_INVALID_PARAMETER
);
827 VIRTUAL_DeleteView( view
);
831 /* Decommit the pages by remapping zero-pages instead */
833 if (FILE_dommap( -1, (LPVOID
)base
, 0, size
, 0, 0,
834 VIRTUAL_GetUnixProt( 0 ), MAP_PRIVATE
|MAP_FIXED
)
836 ERR( "Could not remap pages, expect trouble\n" );
837 return VIRTUAL_SetProt( view
, base
, size
, 0 );
841 /***********************************************************************
842 * VirtualLock (KERNEL32.551)
843 * Locks the specified region of virtual address space
846 * Always returns TRUE
852 BOOL WINAPI
VirtualLock(
853 LPVOID addr
, /* [in] Address of first byte of range to lock */
854 DWORD size
/* [in] Number of bytes in range to lock */
860 /***********************************************************************
861 * VirtualUnlock (KERNEL32.556)
862 * Unlocks a range of pages in the virtual address space
865 * Always returns TRUE
871 BOOL WINAPI
VirtualUnlock(
872 LPVOID addr
, /* [in] Address of first byte of range */
873 DWORD size
/* [in] Number of bytes in range */
879 /***********************************************************************
880 * VirtualProtect (KERNEL32.552)
881 * Changes the access protection on a region of committed pages
887 BOOL WINAPI
VirtualProtect(
888 LPVOID addr
, /* [in] Address of region of committed pages */
889 DWORD size
, /* [in] Size of region */
890 DWORD new_prot
, /* [in] Desired access protection */
891 LPDWORD old_prot
/* [out] Address of variable to get old protection */
897 TRACE("%08x %08lx %08lx\n",
898 (UINT
)addr
, size
, new_prot
);
900 /* Fix the parameters */
902 size
= ROUND_SIZE( addr
, size
);
903 base
= ROUND_ADDR( addr
);
905 if (!(view
= VIRTUAL_FindView( base
)) ||
906 (base
+ size
> view
->base
+ view
->size
))
908 SetLastError( ERROR_INVALID_PARAMETER
);
912 /* Make sure all the pages are committed */
914 p
= view
->prot
+ ((base
- view
->base
) >> page_shift
);
915 for (i
= size
>> page_shift
; i
; i
--, p
++)
917 if (!(*p
& VPROT_COMMITTED
))
919 SetLastError( ERROR_INVALID_PARAMETER
);
924 VIRTUAL_GetWin32Prot( view
->prot
[0], old_prot
, NULL
);
925 vprot
= VIRTUAL_GetProt( new_prot
) | VPROT_COMMITTED
;
926 return VIRTUAL_SetProt( view
, base
, size
, vprot
);
930 /***********************************************************************
931 * VirtualProtectEx (KERNEL32.553)
932 * Changes the access protection on a region of committed pages in the
933 * virtual address space of a specified process
939 BOOL WINAPI
VirtualProtectEx(
940 HANDLE handle
, /* [in] Handle of process */
941 LPVOID addr
, /* [in] Address of region of committed pages */
942 DWORD size
, /* [in] Size of region */
943 DWORD new_prot
, /* [in] Desired access protection */
944 LPDWORD old_prot
/* [out] Address of variable to get old protection */ )
946 if (MapProcessHandle( handle
) == GetCurrentProcessId())
947 return VirtualProtect( addr
, size
, new_prot
, old_prot
);
948 ERR("Unsupported on other process\n");
953 /***********************************************************************
954 * VirtualQuery (KERNEL32.554)
955 * Provides info about a range of pages in virtual address space
958 * Number of bytes returned in information buffer
960 DWORD WINAPI
VirtualQuery(
961 LPCVOID addr
, /* [in] Address of region */
962 LPMEMORY_BASIC_INFORMATION info
, /* [out] Address of info buffer */
963 DWORD len
/* [in] Size of buffer */
965 FILE_VIEW
*view
= VIRTUAL_FirstView
;
966 UINT base
= ROUND_ADDR( addr
);
970 /* Find the view containing the address */
976 size
= 0xffff0000 - alloc_base
;
979 if (view
->base
> base
)
981 size
= view
->base
- alloc_base
;
985 if (view
->base
+ view
->size
> base
)
987 alloc_base
= view
->base
;
991 alloc_base
= view
->base
+ view
->size
;
995 /* Fill the info structure */
999 info
->State
= MEM_FREE
;
1001 info
->AllocationProtect
= 0;
1006 BYTE vprot
= view
->prot
[(base
- alloc_base
) >> page_shift
];
1007 VIRTUAL_GetWin32Prot( vprot
, &info
->Protect
, &info
->State
);
1008 for (size
= base
- alloc_base
; size
< view
->size
; size
+= page_mask
+1)
1009 if (view
->prot
[size
>> page_shift
] != vprot
) break;
1010 info
->AllocationProtect
= view
->protect
;
1011 info
->Type
= MEM_PRIVATE
; /* FIXME */
1014 info
->BaseAddress
= (LPVOID
)base
;
1015 info
->AllocationBase
= (LPVOID
)alloc_base
;
1016 info
->RegionSize
= size
- (base
- alloc_base
);
1017 return sizeof(*info
);
1021 /***********************************************************************
1022 * VirtualQueryEx (KERNEL32.555)
1023 * Provides info about a range of pages in virtual address space of a
1027 * Number of bytes returned in information buffer
1029 DWORD WINAPI
VirtualQueryEx(
1030 HANDLE handle
, /* [in] Handle of process */
1031 LPCVOID addr
, /* [in] Address of region */
1032 LPMEMORY_BASIC_INFORMATION info
, /* [out] Address of info buffer */
1033 DWORD len
/* [in] Size of buffer */ )
1035 if (MapProcessHandle( handle
) == GetCurrentProcessId())
1036 return VirtualQuery( addr
, info
, len
);
1037 ERR("Unsupported on other process\n");
1042 /***********************************************************************
1043 * IsBadReadPtr (KERNEL32.354)
1046 * FALSE: Process has read access to entire block
1049 BOOL WINAPI
IsBadReadPtr(
1050 LPCVOID ptr
, /* Address of memory block */
1051 UINT size
) /* Size of block */
1055 volatile const char *p
= ptr
;
1056 volatile const char *end
= p
+ size
- 1;
1066 __EXCEPT(page_fault
) { return TRUE
; }
1072 /***********************************************************************
1073 * IsBadWritePtr (KERNEL32.357)
1076 * FALSE: Process has write access to entire block
1079 BOOL WINAPI
IsBadWritePtr(
1080 LPVOID ptr
, /* [in] Address of memory block */
1081 UINT size
) /* [in] Size of block in bytes */
1085 volatile char *p
= ptr
;
1086 volatile char *end
= p
+ size
- 1;
1095 __EXCEPT(page_fault
) { return TRUE
; }
1101 /***********************************************************************
1102 * IsBadHugeReadPtr (KERNEL32.352)
1104 * FALSE: Process has read access to entire block
1107 BOOL WINAPI
IsBadHugeReadPtr(
1108 LPCVOID ptr
, /* [in] Address of memory block */
1109 UINT size
/* [in] Size of block */
1111 return IsBadReadPtr( ptr
, size
);
1115 /***********************************************************************
1116 * IsBadHugeWritePtr (KERNEL32.353)
1118 * FALSE: Process has write access to entire block
1121 BOOL WINAPI
IsBadHugeWritePtr(
1122 LPVOID ptr
, /* [in] Address of memory block */
1123 UINT size
/* [in] Size of block */
1125 return IsBadWritePtr( ptr
, size
);
1129 /***********************************************************************
1130 * IsBadCodePtr (KERNEL32.351)
1133 * FALSE: Process has read access to specified memory
1136 BOOL WINAPI
IsBadCodePtr( FARPROC ptr
) /* [in] Address of function */
1138 return IsBadReadPtr( ptr
, 1 );
1142 /***********************************************************************
1143 * IsBadStringPtrA (KERNEL32.355)
1146 * FALSE: Read access to all bytes in string
1149 BOOL WINAPI
IsBadStringPtrA(
1150 LPCSTR str
, /* [in] Address of string */
1151 UINT max
) /* [in] Maximum size of string */
1155 volatile const char *p
= str
;
1156 while (p
< str
+ max
) if (!*p
++) break;
1158 __EXCEPT(page_fault
) { return TRUE
; }
1164 /***********************************************************************
1165 * IsBadStringPtrW (KERNEL32.356)
1166 * See IsBadStringPtrA
1168 BOOL WINAPI
IsBadStringPtrW( LPCWSTR str
, UINT max
)
1172 volatile const WCHAR
*p
= str
;
1173 while (p
< str
+ max
) if (!*p
++) break;
1175 __EXCEPT(page_fault
) { return TRUE
; }
1181 /***********************************************************************
1182 * CreateFileMappingA (KERNEL32.46)
1183 * Creates a named or unnamed file-mapping object for the specified file
1187 * 0: Mapping object does not exist
1190 HANDLE WINAPI
CreateFileMappingA(
1191 HFILE hFile
, /* [in] Handle of file to map */
1192 SECURITY_ATTRIBUTES
*sa
, /* [in] Optional security attributes*/
1193 DWORD protect
, /* [in] Protection for mapping object */
1194 DWORD size_high
, /* [in] High-order 32 bits of object size */
1195 DWORD size_low
, /* [in] Low-order 32 bits of object size */
1196 LPCSTR name
/* [in] Name of file-mapping object */ )
1198 struct create_mapping_request
*req
= get_req_buffer();
1201 /* Check parameters */
1203 TRACE("(%x,%p,%08lx,%08lx%08lx,%s)\n",
1204 hFile
, sa
, protect
, size_high
, size_low
, debugstr_a(name
) );
1206 vprot
= VIRTUAL_GetProt( protect
);
1207 if (protect
& SEC_RESERVE
)
1209 if (hFile
!= INVALID_HANDLE_VALUE
)
1211 SetLastError( ERROR_INVALID_PARAMETER
);
1215 else vprot
|= VPROT_COMMITTED
;
1216 if (protect
& SEC_NOCACHE
) vprot
|= VPROT_NOCACHE
;
1217 if (protect
& SEC_IMAGE
) vprot
|= VPROT_IMAGE
;
1219 /* Create the server object */
1221 req
->file_handle
= hFile
;
1222 req
->size_high
= size_high
;
1223 req
->size_low
= size_low
;
1224 req
->protect
= vprot
;
1225 req
->inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
1226 server_strcpyAtoW( req
->name
, name
);
1228 server_call( REQ_CREATE_MAPPING
);
1229 if (req
->handle
== -1) return 0;
1234 /***********************************************************************
1235 * CreateFileMappingW (KERNEL32.47)
1236 * See CreateFileMappingA
1238 HANDLE WINAPI
CreateFileMappingW( HFILE hFile
, LPSECURITY_ATTRIBUTES sa
,
1239 DWORD protect
, DWORD size_high
,
1240 DWORD size_low
, LPCWSTR name
)
1242 struct create_mapping_request
*req
= get_req_buffer();
1245 /* Check parameters */
1247 TRACE("(%x,%p,%08lx,%08lx%08lx,%s)\n",
1248 hFile
, sa
, protect
, size_high
, size_low
, debugstr_w(name
) );
1250 vprot
= VIRTUAL_GetProt( protect
);
1251 if (protect
& SEC_RESERVE
)
1253 if (hFile
!= INVALID_HANDLE_VALUE
)
1255 SetLastError( ERROR_INVALID_PARAMETER
);
1259 else vprot
|= VPROT_COMMITTED
;
1260 if (protect
& SEC_NOCACHE
) vprot
|= VPROT_NOCACHE
;
1261 if (protect
& SEC_IMAGE
) vprot
|= VPROT_IMAGE
;
1263 /* Create the server object */
1265 req
->file_handle
= hFile
;
1266 req
->size_high
= size_high
;
1267 req
->size_low
= size_low
;
1268 req
->protect
= vprot
;
1269 req
->inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
1270 server_strcpyW( req
->name
, name
);
1272 server_call( REQ_CREATE_MAPPING
);
1273 if (req
->handle
== -1) return 0;
1278 /***********************************************************************
1279 * OpenFileMappingA (KERNEL32.397)
1280 * Opens a named file-mapping object.
1286 HANDLE WINAPI
OpenFileMappingA(
1287 DWORD access
, /* [in] Access mode */
1288 BOOL inherit
, /* [in] Inherit flag */
1289 LPCSTR name
) /* [in] Name of file-mapping object */
1291 struct open_mapping_request
*req
= get_req_buffer();
1293 req
->access
= access
;
1294 req
->inherit
= inherit
;
1295 server_strcpyAtoW( req
->name
, name
);
1296 server_call( REQ_OPEN_MAPPING
);
1297 if (req
->handle
== -1) return 0; /* must return 0 on failure, not -1 */
1302 /***********************************************************************
1303 * OpenFileMappingW (KERNEL32.398)
1304 * See OpenFileMappingA
1306 HANDLE WINAPI
OpenFileMappingW( DWORD access
, BOOL inherit
, LPCWSTR name
)
1308 struct open_mapping_request
*req
= get_req_buffer();
1310 req
->access
= access
;
1311 req
->inherit
= inherit
;
1312 server_strcpyW( req
->name
, name
);
1313 server_call( REQ_OPEN_MAPPING
);
1314 if (req
->handle
== -1) return 0; /* must return 0 on failure, not -1 */
1319 /***********************************************************************
1320 * MapViewOfFile (KERNEL32.385)
1321 * Maps a view of a file into the address space
1324 * Starting address of mapped view
1327 LPVOID WINAPI
MapViewOfFile(
1328 HANDLE mapping
, /* [in] File-mapping object to map */
1329 DWORD access
, /* [in] Access mode */
1330 DWORD offset_high
, /* [in] High-order 32 bits of file offset */
1331 DWORD offset_low
, /* [in] Low-order 32 bits of file offset */
1332 DWORD count
/* [in] Number of bytes to map */
1334 return MapViewOfFileEx( mapping
, access
, offset_high
,
1335 offset_low
, count
, NULL
);
1339 /***********************************************************************
1340 * MapViewOfFileEx (KERNEL32.386)
1341 * Maps a view of a file into the address space
1344 * Starting address of mapped view
1347 LPVOID WINAPI
MapViewOfFileEx(
1348 HANDLE handle
, /* [in] File-mapping object to map */
1349 DWORD access
, /* [in] Access mode */
1350 DWORD offset_high
, /* [in] High-order 32 bits of file offset */
1351 DWORD offset_low
, /* [in] Low-order 32 bits of file offset */
1352 DWORD count
, /* [in] Number of bytes to map */
1353 LPVOID addr
/* [in] Suggested starting address for mapped view */
1356 UINT ptr
= (UINT
)-1, size
= 0;
1357 int flags
= MAP_PRIVATE
;
1358 int unix_handle
= -1;
1360 struct get_mapping_info_request
*req
= get_req_buffer();
1362 /* Check parameters */
1364 if ((offset_low
& granularity_mask
) ||
1365 (addr
&& ((UINT
)addr
& granularity_mask
)))
1367 SetLastError( ERROR_INVALID_PARAMETER
);
1371 req
->handle
= handle
;
1372 if (server_call_fd( REQ_GET_MAPPING_INFO
, -1, &unix_handle
)) goto error
;
1373 prot
= req
->protect
;
1375 if (prot
& VPROT_IMAGE
)
1376 return map_image( handle
, unix_handle
, req
->base
, req
->size_low
, req
->header_size
,
1377 req
->shared_file
, req
->shared_size
);
1379 if (req
->size_high
|| offset_high
)
1380 ERR("Offsets larger than 4Gb not supported\n");
1382 if ((offset_low
>= req
->size_low
) ||
1383 (count
> req
->size_low
- offset_low
))
1385 SetLastError( ERROR_INVALID_PARAMETER
);
1388 if (count
) size
= ROUND_SIZE( offset_low
, count
);
1389 else size
= req
->size_low
- offset_low
;
1393 case FILE_MAP_ALL_ACCESS
:
1394 case FILE_MAP_WRITE
:
1395 case FILE_MAP_WRITE
| FILE_MAP_READ
:
1396 if (!(prot
& VPROT_WRITE
))
1398 SetLastError( ERROR_INVALID_PARAMETER
);
1405 case FILE_MAP_COPY
| FILE_MAP_READ
:
1406 if (prot
& VPROT_READ
) break;
1409 SetLastError( ERROR_INVALID_PARAMETER
);
1413 /* FIXME: If a mapping is created with SEC_RESERVE and a process,
1414 * which has a view of this mapping commits some pages, they will
1415 * appear commited in all other processes, which have the same
1416 * view created. Since we don`t support this yet, we create the
1417 * whole mapping commited.
1419 prot
|= VPROT_COMMITTED
;
1423 TRACE("handle=%x size=%x offset=%lx\n", handle
, size
, offset_low
);
1425 ptr
= (UINT
)FILE_dommap( unix_handle
, addr
, 0, size
, 0, offset_low
,
1426 VIRTUAL_GetUnixProt( prot
), flags
);
1427 if (ptr
== (UINT
)-1) {
1428 /* KB: Q125713, 25-SEP-1995, "Common File Mapping Problems and
1429 * Platform Differences":
1430 * Windows NT: ERROR_INVALID_PARAMETER
1431 * Windows 95: ERROR_INVALID_ADDRESS.
1432 * FIXME: So should we add a module dependend check here? -MM
1435 SetLastError( ERROR_OUTOFMEMORY
);
1437 SetLastError( ERROR_INVALID_PARAMETER
);
1441 if (!(view
= VIRTUAL_CreateView( ptr
, size
, 0, prot
, handle
)))
1443 SetLastError( ERROR_OUTOFMEMORY
);
1446 if (unix_handle
!= -1) close( unix_handle
);
1450 if (unix_handle
!= -1) close( unix_handle
);
1451 if (ptr
!= (UINT
)-1) FILE_munmap( (void *)ptr
, 0, size
);
1456 /***********************************************************************
1457 * FlushViewOfFile (KERNEL32.262)
1458 * Writes to the disk a byte range within a mapped view of a file
1464 BOOL WINAPI
FlushViewOfFile(
1465 LPCVOID base
, /* [in] Start address of byte range to flush */
1466 DWORD cbFlush
/* [in] Number of bytes in range */
1469 UINT addr
= ROUND_ADDR( base
);
1471 TRACE("FlushViewOfFile at %p for %ld bytes\n",
1474 if (!(view
= VIRTUAL_FindView( addr
)))
1476 SetLastError( ERROR_INVALID_PARAMETER
);
1479 if (!cbFlush
) cbFlush
= view
->size
;
1480 if (!msync( (void *)addr
, cbFlush
, MS_SYNC
)) return TRUE
;
1481 SetLastError( ERROR_INVALID_PARAMETER
);
1486 /***********************************************************************
1487 * UnmapViewOfFile (KERNEL32.540)
1488 * Unmaps a mapped view of a file.
1491 * Should addr be an LPCVOID?
1497 BOOL WINAPI
UnmapViewOfFile(
1498 LPVOID addr
/* [in] Address where mapped view begins */
1501 UINT base
= ROUND_ADDR( addr
);
1502 if (!(view
= VIRTUAL_FindView( base
)) || (base
!= view
->base
))
1504 SetLastError( ERROR_INVALID_PARAMETER
);
1507 VIRTUAL_DeleteView( view
);
1511 /***********************************************************************
1514 * Helper function to map a file to memory:
1516 * [RETURN] ptr - pointer to mapped file
1518 LPVOID
VIRTUAL_MapFileW( LPCWSTR name
)
1520 HANDLE hFile
, hMapping
;
1523 hFile
= CreateFileW( name
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
1524 OPEN_EXISTING
, FILE_FLAG_RANDOM_ACCESS
, 0);
1525 if (hFile
!= INVALID_HANDLE_VALUE
)
1527 hMapping
= CreateFileMappingA( hFile
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
1528 CloseHandle( hFile
);
1531 ptr
= MapViewOfFile( hMapping
, FILE_MAP_READ
, 0, 0, 0 );
1532 CloseHandle( hMapping
);