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);
41 struct _FV
*next
; /* Next view */
42 struct _FV
*prev
; /* Prev view */
43 UINT base
; /* Base address */
44 UINT size
; /* Size in bytes */
45 UINT flags
; /* Allocation flags */
46 HANDLE mapping
; /* Handle to the file mapping */
47 HANDLERPROC handlerProc
; /* Fault handler */
48 LPVOID handlerArg
; /* Fault handler argument */
49 BYTE protect
; /* Protection for all pages at allocation time */
50 BYTE prot
[1]; /* Protection byte for each page */
54 #define VFLAG_SYSTEM 0x01
56 /* Conversion from VPROT_* to Win32 flags */
57 static const BYTE VIRTUAL_Win32Flags
[16] =
59 PAGE_NOACCESS
, /* 0 */
60 PAGE_READONLY
, /* READ */
61 PAGE_READWRITE
, /* WRITE */
62 PAGE_READWRITE
, /* READ | WRITE */
63 PAGE_EXECUTE
, /* EXEC */
64 PAGE_EXECUTE_READ
, /* READ | EXEC */
65 PAGE_EXECUTE_READWRITE
, /* WRITE | EXEC */
66 PAGE_EXECUTE_READWRITE
, /* READ | WRITE | EXEC */
67 PAGE_WRITECOPY
, /* WRITECOPY */
68 PAGE_WRITECOPY
, /* READ | WRITECOPY */
69 PAGE_WRITECOPY
, /* WRITE | WRITECOPY */
70 PAGE_WRITECOPY
, /* READ | WRITE | WRITECOPY */
71 PAGE_EXECUTE_WRITECOPY
, /* EXEC | WRITECOPY */
72 PAGE_EXECUTE_WRITECOPY
, /* READ | EXEC | WRITECOPY */
73 PAGE_EXECUTE_WRITECOPY
, /* WRITE | EXEC | WRITECOPY */
74 PAGE_EXECUTE_WRITECOPY
/* READ | WRITE | EXEC | WRITECOPY */
78 static FILE_VIEW
*VIRTUAL_FirstView
;
81 /* These are always the same on an i386, and it will be faster this way */
82 # define page_mask 0xfff
83 # define page_shift 12
85 static UINT page_shift
;
86 static UINT page_mask
;
88 #define granularity_mask 0xffff /* Allocation granularity (usually 64k) */
90 #define ROUND_ADDR(addr) \
91 ((UINT)(addr) & ~page_mask)
93 #define ROUND_SIZE(addr,size) \
94 (((UINT)(size) + ((UINT)(addr) & page_mask) + page_mask) & ~page_mask)
96 #define VIRTUAL_DEBUG_DUMP_VIEW(view) \
97 if (!TRACE_ON(virtual)); else VIRTUAL_DumpView(view)
100 /* filter for page-fault exceptions */
101 static WINE_EXCEPTION_FILTER(page_fault
)
103 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION
)
104 return EXCEPTION_EXECUTE_HANDLER
;
105 return EXCEPTION_CONTINUE_SEARCH
;
108 /***********************************************************************
111 static const char *VIRTUAL_GetProtStr( BYTE prot
)
113 static char buffer
[6];
114 buffer
[0] = (prot
& VPROT_COMMITTED
) ? 'c' : '-';
115 buffer
[1] = (prot
& VPROT_GUARD
) ? 'g' : '-';
116 buffer
[2] = (prot
& VPROT_READ
) ? 'r' : '-';
117 buffer
[3] = (prot
& VPROT_WRITE
) ?
118 ((prot
& VPROT_WRITECOPY
) ? 'w' : 'W') : '-';
119 buffer
[4] = (prot
& VPROT_EXEC
) ? 'x' : '-';
125 /***********************************************************************
128 static void VIRTUAL_DumpView( FILE_VIEW
*view
)
131 UINT addr
= view
->base
;
132 BYTE prot
= view
->prot
[0];
134 DPRINTF( "View: %08x - %08x%s",
135 view
->base
, view
->base
+ view
->size
- 1,
136 (view
->flags
& VFLAG_SYSTEM
) ? " (system)" : "" );
138 DPRINTF( " %d\n", view
->mapping
);
140 DPRINTF( " (anonymous)\n");
142 for (count
= i
= 1; i
< view
->size
>> page_shift
; i
++, count
++)
144 if (view
->prot
[i
] == prot
) continue;
145 DPRINTF( " %08x - %08x %s\n",
146 addr
, addr
+ (count
<< page_shift
) - 1,
147 VIRTUAL_GetProtStr(prot
) );
148 addr
+= (count
<< page_shift
);
149 prot
= view
->prot
[i
];
153 DPRINTF( " %08x - %08x %s\n",
154 addr
, addr
+ (count
<< page_shift
) - 1,
155 VIRTUAL_GetProtStr(prot
) );
159 /***********************************************************************
162 void VIRTUAL_Dump(void)
164 FILE_VIEW
*view
= VIRTUAL_FirstView
;
165 DPRINTF( "\nDump of all virtual memory views:\n\n" );
168 VIRTUAL_DumpView( view
);
174 /***********************************************************************
177 * Find the view containing a given address.
183 static FILE_VIEW
*VIRTUAL_FindView(
184 UINT addr
/* [in] Address */
186 FILE_VIEW
*view
= VIRTUAL_FirstView
;
189 if (view
->base
> addr
) return NULL
;
190 if (view
->base
+ view
->size
> addr
) return view
;
197 /***********************************************************************
200 * Create a new view and add it in the linked list.
202 static FILE_VIEW
*VIRTUAL_CreateView( UINT base
, UINT size
, UINT offset
,
203 UINT flags
, BYTE vprot
,
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 /***********************************************************************
399 DECL_GLOBAL_CONSTRUCTOR(VIRTUAL_Init
)
403 # ifdef HAVE_GETPAGESIZE
404 page_size
= getpagesize();
407 page_size
= sysconf(_SC_PAGESIZE
);
409 # error Cannot get the page size on this platform
412 page_mask
= page_size
- 1;
413 /* Make sure we have a power of 2 */
414 assert( !(page_size
& page_mask
) );
416 while ((1 << page_shift
) != page_size
) page_shift
++;
418 #endif /* page_mask */
421 /***********************************************************************
422 * VIRTUAL_GetPageSize
424 DWORD
VIRTUAL_GetPageSize(void)
426 return 1 << page_shift
;
430 /***********************************************************************
431 * VIRTUAL_GetGranularity
433 DWORD
VIRTUAL_GetGranularity(void)
435 return granularity_mask
+ 1;
439 /***********************************************************************
440 * VIRTUAL_SetFaultHandler
442 BOOL
VIRTUAL_SetFaultHandler( LPCVOID addr
, HANDLERPROC proc
, LPVOID arg
)
446 if (!(view
= VIRTUAL_FindView((UINT
)addr
))) return FALSE
;
447 view
->handlerProc
= proc
;
448 view
->handlerArg
= arg
;
452 /***********************************************************************
453 * VIRTUAL_HandleFault
455 DWORD
VIRTUAL_HandleFault( LPCVOID addr
)
457 FILE_VIEW
*view
= VIRTUAL_FindView((UINT
)addr
);
458 DWORD ret
= EXCEPTION_ACCESS_VIOLATION
;
462 if (view
->handlerProc
)
464 if (view
->handlerProc(view
->handlerArg
, addr
)) ret
= 0; /* handled */
468 BYTE vprot
= view
->prot
[((UINT
)addr
- view
->base
) >> page_shift
];
469 UINT page
= (UINT
)addr
& ~page_mask
;
470 char *stack
= (char *)NtCurrentTeb()->stack_base
+ SIGNAL_STACK_SIZE
+ page_mask
+ 1;
471 if (vprot
& VPROT_GUARD
)
473 VIRTUAL_SetProt( view
, page
, page_mask
+ 1, vprot
& ~VPROT_GUARD
);
474 ret
= STATUS_GUARD_PAGE_VIOLATION
;
476 /* is it inside the stack guard pages? */
477 if (((char *)addr
>= stack
) && ((char *)addr
< stack
+ 2*(page_mask
+1)))
478 ret
= STATUS_STACK_OVERFLOW
;
485 /***********************************************************************
486 * VirtualAlloc (KERNEL32.548)
487 * Reserves or commits a region of pages in virtual address space
490 * Base address of allocated region of pages
493 LPVOID WINAPI
VirtualAlloc(
494 LPVOID addr
, /* [in] Address of region to reserve or commit */
495 DWORD size
, /* [in] Size of region */
496 DWORD type
, /* [in] Type of allocation */
497 DWORD protect
/* [in] Type of access protection */
500 UINT base
, ptr
, view_size
;
503 TRACE("%08x %08lx %lx %08lx\n",
504 (UINT
)addr
, size
, type
, protect
);
506 /* Round parameters to a page boundary */
508 if (size
> 0x7fc00000) /* 2Gb - 4Mb */
510 SetLastError( ERROR_OUTOFMEMORY
);
515 if (type
& MEM_RESERVE
) /* Round down to 64k boundary */
516 base
= (UINT
)addr
& ~granularity_mask
;
518 base
= ROUND_ADDR( addr
);
519 size
= (((UINT
)addr
+ size
+ page_mask
) & ~page_mask
) - base
;
520 if ((base
<= granularity_mask
) || (base
+ size
< base
))
522 /* disallow low 64k and wrap-around */
523 SetLastError( ERROR_INVALID_PARAMETER
);
530 size
= (size
+ page_mask
) & ~page_mask
;
533 if (type
& MEM_TOP_DOWN
) {
534 /* FIXME: MEM_TOP_DOWN allocates the largest possible address.
535 * Is there _ANY_ way to do it with UNIX mmap()?
537 WARN("MEM_TOP_DOWN ignored\n");
538 type
&= ~MEM_TOP_DOWN
;
540 /* Compute the protection flags */
542 if (!(type
& (MEM_COMMIT
| MEM_RESERVE
| MEM_SYSTEM
)) ||
543 (type
& ~(MEM_COMMIT
| MEM_RESERVE
| MEM_SYSTEM
)))
545 SetLastError( ERROR_INVALID_PARAMETER
);
548 if (type
& (MEM_COMMIT
| MEM_SYSTEM
))
549 vprot
= VIRTUAL_GetProt( protect
) | VPROT_COMMITTED
;
552 /* Reserve the memory */
554 if ((type
& MEM_RESERVE
) || !base
)
556 view_size
= size
+ (base
? 0 : granularity_mask
+ 1);
557 if (type
& MEM_SYSTEM
)
560 ptr
= (UINT
)FILE_dommap( -1, (LPVOID
)base
, 0, view_size
, 0, 0,
561 VIRTUAL_GetUnixProt( vprot
), MAP_PRIVATE
);
564 SetLastError( ERROR_OUTOFMEMORY
);
569 /* Release the extra memory while keeping the range */
570 /* starting on a 64k boundary. */
572 if (ptr
& granularity_mask
)
574 UINT extra
= granularity_mask
+ 1 - (ptr
& granularity_mask
);
575 FILE_munmap( (void *)ptr
, 0, extra
);
579 if (view_size
> size
)
580 FILE_munmap( (void *)(ptr
+ size
), 0, view_size
- size
);
582 else if (ptr
!= base
)
584 /* We couldn't get the address we wanted */
585 FILE_munmap( (void *)ptr
, 0, view_size
);
586 SetLastError( ERROR_INVALID_ADDRESS
);
589 if (!(view
= VIRTUAL_CreateView( ptr
, size
, 0, (type
& MEM_SYSTEM
) ?
590 VFLAG_SYSTEM
: 0, vprot
, -1 )))
592 FILE_munmap( (void *)ptr
, 0, size
);
593 SetLastError( ERROR_OUTOFMEMORY
);
596 VIRTUAL_DEBUG_DUMP_VIEW( view
);
600 /* Commit the pages */
602 if (!(view
= VIRTUAL_FindView( base
)) ||
603 (base
+ size
> view
->base
+ view
->size
))
605 SetLastError( ERROR_INVALID_ADDRESS
);
609 if (!VIRTUAL_SetProt( view
, base
, size
, vprot
)) return NULL
;
614 /***********************************************************************
615 * VirtualFree (KERNEL32.550)
616 * Release or decommits a region of pages in virtual address space.
622 BOOL WINAPI
VirtualFree(
623 LPVOID addr
, /* [in] Address of region of committed pages */
624 DWORD size
, /* [in] Size of region */
625 DWORD type
/* [in] Type of operation */
630 TRACE("%08x %08lx %lx\n",
631 (UINT
)addr
, size
, type
);
633 /* Fix the parameters */
635 size
= ROUND_SIZE( addr
, size
);
636 base
= ROUND_ADDR( addr
);
638 if (!(view
= VIRTUAL_FindView( base
)) ||
639 (base
+ size
> view
->base
+ view
->size
))
641 SetLastError( ERROR_INVALID_PARAMETER
);
645 /* Compute the protection flags */
647 if ((type
!= MEM_DECOMMIT
) && (type
!= MEM_RELEASE
))
649 SetLastError( ERROR_INVALID_PARAMETER
);
655 if (type
== MEM_RELEASE
)
657 if (size
|| (base
!= view
->base
))
659 SetLastError( ERROR_INVALID_PARAMETER
);
662 VIRTUAL_DeleteView( view
);
666 /* Decommit the pages by remapping zero-pages instead */
668 if (FILE_dommap( -1, (LPVOID
)base
, 0, size
, 0, 0,
669 VIRTUAL_GetUnixProt( 0 ), MAP_PRIVATE
|MAP_FIXED
)
671 ERR( "Could not remap pages, expect trouble\n" );
672 return VIRTUAL_SetProt( view
, base
, size
, 0 );
676 /***********************************************************************
677 * VirtualLock (KERNEL32.551)
678 * Locks the specified region of virtual address space
681 * Always returns TRUE
687 BOOL WINAPI
VirtualLock(
688 LPVOID addr
, /* [in] Address of first byte of range to lock */
689 DWORD size
/* [in] Number of bytes in range to lock */
695 /***********************************************************************
696 * VirtualUnlock (KERNEL32.556)
697 * Unlocks a range of pages in the virtual address space
700 * Always returns TRUE
706 BOOL WINAPI
VirtualUnlock(
707 LPVOID addr
, /* [in] Address of first byte of range */
708 DWORD size
/* [in] Number of bytes in range */
714 /***********************************************************************
715 * VirtualProtect (KERNEL32.552)
716 * Changes the access protection on a region of committed pages
722 BOOL WINAPI
VirtualProtect(
723 LPVOID addr
, /* [in] Address of region of committed pages */
724 DWORD size
, /* [in] Size of region */
725 DWORD new_prot
, /* [in] Desired access protection */
726 LPDWORD old_prot
/* [out] Address of variable to get old protection */
732 TRACE("%08x %08lx %08lx\n",
733 (UINT
)addr
, size
, new_prot
);
735 /* Fix the parameters */
737 size
= ROUND_SIZE( addr
, size
);
738 base
= ROUND_ADDR( addr
);
740 if (!(view
= VIRTUAL_FindView( base
)) ||
741 (base
+ size
> view
->base
+ view
->size
))
743 SetLastError( ERROR_INVALID_PARAMETER
);
747 /* Make sure all the pages are committed */
749 p
= view
->prot
+ ((base
- view
->base
) >> page_shift
);
750 for (i
= size
>> page_shift
; i
; i
--, p
++)
752 if (!(*p
& VPROT_COMMITTED
))
754 SetLastError( ERROR_INVALID_PARAMETER
);
759 VIRTUAL_GetWin32Prot( view
->prot
[0], old_prot
, NULL
);
760 vprot
= VIRTUAL_GetProt( new_prot
) | VPROT_COMMITTED
;
761 return VIRTUAL_SetProt( view
, base
, size
, vprot
);
765 /***********************************************************************
766 * VirtualProtectEx (KERNEL32.553)
767 * Changes the access protection on a region of committed pages in the
768 * virtual address space of a specified process
774 BOOL WINAPI
VirtualProtectEx(
775 HANDLE handle
, /* [in] Handle of process */
776 LPVOID addr
, /* [in] Address of region of committed pages */
777 DWORD size
, /* [in] Size of region */
778 DWORD new_prot
, /* [in] Desired access protection */
779 LPDWORD old_prot
/* [out] Address of variable to get old protection */ )
781 if (MapProcessHandle( handle
) == GetCurrentProcessId())
782 return VirtualProtect( addr
, size
, new_prot
, old_prot
);
783 ERR("Unsupported on other process\n");
788 /***********************************************************************
789 * VirtualQuery (KERNEL32.554)
790 * Provides info about a range of pages in virtual address space
793 * Number of bytes returned in information buffer
795 DWORD WINAPI
VirtualQuery(
796 LPCVOID addr
, /* [in] Address of region */
797 LPMEMORY_BASIC_INFORMATION info
, /* [out] Address of info buffer */
798 DWORD len
/* [in] Size of buffer */
800 FILE_VIEW
*view
= VIRTUAL_FirstView
;
801 UINT base
= ROUND_ADDR( addr
);
805 /* Find the view containing the address */
811 size
= 0xffff0000 - alloc_base
;
814 if (view
->base
> base
)
816 size
= view
->base
- alloc_base
;
820 if (view
->base
+ view
->size
> base
)
822 alloc_base
= view
->base
;
826 alloc_base
= view
->base
+ view
->size
;
830 /* Fill the info structure */
834 info
->State
= MEM_FREE
;
836 info
->AllocationProtect
= 0;
841 BYTE vprot
= view
->prot
[(base
- alloc_base
) >> page_shift
];
842 VIRTUAL_GetWin32Prot( vprot
, &info
->Protect
, &info
->State
);
843 for (size
= base
- alloc_base
; size
< view
->size
; size
+= page_mask
+1)
844 if (view
->prot
[size
>> page_shift
] != vprot
) break;
845 info
->AllocationProtect
= view
->protect
;
846 info
->Type
= MEM_PRIVATE
; /* FIXME */
849 info
->BaseAddress
= (LPVOID
)base
;
850 info
->AllocationBase
= (LPVOID
)alloc_base
;
851 info
->RegionSize
= size
- (base
- alloc_base
);
852 return sizeof(*info
);
856 /***********************************************************************
857 * VirtualQueryEx (KERNEL32.555)
858 * Provides info about a range of pages in virtual address space of a
862 * Number of bytes returned in information buffer
864 DWORD WINAPI
VirtualQueryEx(
865 HANDLE handle
, /* [in] Handle of process */
866 LPCVOID addr
, /* [in] Address of region */
867 LPMEMORY_BASIC_INFORMATION info
, /* [out] Address of info buffer */
868 DWORD len
/* [in] Size of buffer */ )
870 if (MapProcessHandle( handle
) == GetCurrentProcessId())
871 return VirtualQuery( addr
, info
, len
);
872 ERR("Unsupported on other process\n");
877 /***********************************************************************
878 * IsBadReadPtr (KERNEL32.354)
881 * FALSE: Process has read access to entire block
884 BOOL WINAPI
IsBadReadPtr(
885 LPCVOID ptr
, /* Address of memory block */
886 UINT size
) /* Size of block */
890 volatile const char *p
= ptr
;
891 volatile const char *end
= p
+ size
- 1;
901 __EXCEPT(page_fault
) { return TRUE
; }
907 /***********************************************************************
908 * IsBadWritePtr (KERNEL32.357)
911 * FALSE: Process has write access to entire block
914 BOOL WINAPI
IsBadWritePtr(
915 LPVOID ptr
, /* [in] Address of memory block */
916 UINT size
) /* [in] Size of block in bytes */
920 volatile char *p
= ptr
;
921 volatile char *end
= p
+ size
- 1;
930 __EXCEPT(page_fault
) { return TRUE
; }
936 /***********************************************************************
937 * IsBadHugeReadPtr (KERNEL32.352)
939 * FALSE: Process has read access to entire block
942 BOOL WINAPI
IsBadHugeReadPtr(
943 LPCVOID ptr
, /* [in] Address of memory block */
944 UINT size
/* [in] Size of block */
946 return IsBadReadPtr( ptr
, size
);
950 /***********************************************************************
951 * IsBadHugeWritePtr (KERNEL32.353)
953 * FALSE: Process has write access to entire block
956 BOOL WINAPI
IsBadHugeWritePtr(
957 LPVOID ptr
, /* [in] Address of memory block */
958 UINT size
/* [in] Size of block */
960 return IsBadWritePtr( ptr
, size
);
964 /***********************************************************************
965 * IsBadCodePtr (KERNEL32.351)
968 * FALSE: Process has read access to specified memory
971 BOOL WINAPI
IsBadCodePtr( FARPROC ptr
) /* [in] Address of function */
973 return IsBadReadPtr( ptr
, 1 );
977 /***********************************************************************
978 * IsBadStringPtrA (KERNEL32.355)
981 * FALSE: Read access to all bytes in string
984 BOOL WINAPI
IsBadStringPtrA(
985 LPCSTR str
, /* [in] Address of string */
986 UINT max
) /* [in] Maximum size of string */
990 volatile const char *p
= str
;
991 while (p
< str
+ max
) if (!*p
++) break;
993 __EXCEPT(page_fault
) { return TRUE
; }
999 /***********************************************************************
1000 * IsBadStringPtrW (KERNEL32.356)
1001 * See IsBadStringPtrA
1003 BOOL WINAPI
IsBadStringPtrW( LPCWSTR str
, UINT max
)
1007 volatile const WCHAR
*p
= str
;
1008 while (p
< str
+ max
) if (!*p
++) break;
1010 __EXCEPT(page_fault
) { return TRUE
; }
1016 /***********************************************************************
1017 * CreateFileMappingA (KERNEL32.46)
1018 * Creates a named or unnamed file-mapping object for the specified file
1022 * 0: Mapping object does not exist
1025 HANDLE WINAPI
CreateFileMappingA(
1026 HFILE hFile
, /* [in] Handle of file to map */
1027 SECURITY_ATTRIBUTES
*sa
, /* [in] Optional security attributes*/
1028 DWORD protect
, /* [in] Protection for mapping object */
1029 DWORD size_high
, /* [in] High-order 32 bits of object size */
1030 DWORD size_low
, /* [in] Low-order 32 bits of object size */
1031 LPCSTR name
/* [in] Name of file-mapping object */ )
1033 struct create_mapping_request
*req
= get_req_buffer();
1036 /* Check parameters */
1038 TRACE("(%x,%p,%08lx,%08lx%08lx,%s)\n",
1039 hFile
, sa
, protect
, size_high
, size_low
, debugstr_a(name
) );
1041 vprot
= VIRTUAL_GetProt( protect
);
1042 if (protect
& SEC_RESERVE
)
1044 if (hFile
!= INVALID_HANDLE_VALUE
)
1046 SetLastError( ERROR_INVALID_PARAMETER
);
1050 else vprot
|= VPROT_COMMITTED
;
1051 if (protect
& SEC_NOCACHE
) vprot
|= VPROT_NOCACHE
;
1053 /* Create the server object */
1055 req
->file_handle
= hFile
;
1056 req
->size_high
= size_high
;
1057 req
->size_low
= size_low
;
1058 req
->protect
= vprot
;
1059 req
->inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
1060 server_strcpyAtoW( req
->name
, name
);
1062 server_call( REQ_CREATE_MAPPING
);
1063 if (req
->handle
== -1) return 0;
1068 /***********************************************************************
1069 * CreateFileMappingW (KERNEL32.47)
1070 * See CreateFileMappingA
1072 HANDLE WINAPI
CreateFileMappingW( HFILE hFile
, LPSECURITY_ATTRIBUTES sa
,
1073 DWORD protect
, DWORD size_high
,
1074 DWORD size_low
, LPCWSTR name
)
1076 struct create_mapping_request
*req
= get_req_buffer();
1079 /* Check parameters */
1081 TRACE("(%x,%p,%08lx,%08lx%08lx,%s)\n",
1082 hFile
, sa
, protect
, size_high
, size_low
, debugstr_w(name
) );
1084 vprot
= VIRTUAL_GetProt( protect
);
1085 if (protect
& SEC_RESERVE
)
1087 if (hFile
!= INVALID_HANDLE_VALUE
)
1089 SetLastError( ERROR_INVALID_PARAMETER
);
1093 else vprot
|= VPROT_COMMITTED
;
1094 if (protect
& SEC_NOCACHE
) vprot
|= VPROT_NOCACHE
;
1096 /* Create the server object */
1098 req
->file_handle
= hFile
;
1099 req
->size_high
= size_high
;
1100 req
->size_low
= size_low
;
1101 req
->protect
= vprot
;
1102 req
->inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
1103 server_strcpyW( req
->name
, name
);
1105 server_call( REQ_CREATE_MAPPING
);
1106 if (req
->handle
== -1) return 0;
1111 /***********************************************************************
1112 * OpenFileMappingA (KERNEL32.397)
1113 * Opens a named file-mapping object.
1119 HANDLE WINAPI
OpenFileMappingA(
1120 DWORD access
, /* [in] Access mode */
1121 BOOL inherit
, /* [in] Inherit flag */
1122 LPCSTR name
) /* [in] Name of file-mapping object */
1124 struct open_mapping_request
*req
= get_req_buffer();
1126 req
->access
= access
;
1127 req
->inherit
= inherit
;
1128 server_strcpyAtoW( req
->name
, name
);
1129 server_call( REQ_OPEN_MAPPING
);
1130 if (req
->handle
== -1) return 0; /* must return 0 on failure, not -1 */
1135 /***********************************************************************
1136 * OpenFileMappingW (KERNEL32.398)
1137 * See OpenFileMappingA
1139 HANDLE WINAPI
OpenFileMappingW( DWORD access
, BOOL inherit
, LPCWSTR name
)
1141 struct open_mapping_request
*req
= get_req_buffer();
1143 req
->access
= access
;
1144 req
->inherit
= inherit
;
1145 server_strcpyW( req
->name
, name
);
1146 server_call( REQ_OPEN_MAPPING
);
1147 if (req
->handle
== -1) return 0; /* must return 0 on failure, not -1 */
1152 /***********************************************************************
1153 * MapViewOfFile (KERNEL32.385)
1154 * Maps a view of a file into the address space
1157 * Starting address of mapped view
1160 LPVOID WINAPI
MapViewOfFile(
1161 HANDLE mapping
, /* [in] File-mapping object to map */
1162 DWORD access
, /* [in] Access mode */
1163 DWORD offset_high
, /* [in] High-order 32 bits of file offset */
1164 DWORD offset_low
, /* [in] Low-order 32 bits of file offset */
1165 DWORD count
/* [in] Number of bytes to map */
1167 return MapViewOfFileEx( mapping
, access
, offset_high
,
1168 offset_low
, count
, NULL
);
1172 /***********************************************************************
1173 * MapViewOfFileEx (KERNEL32.386)
1174 * Maps a view of a file into the address space
1177 * Starting address of mapped view
1180 LPVOID WINAPI
MapViewOfFileEx(
1181 HANDLE handle
, /* [in] File-mapping object to map */
1182 DWORD access
, /* [in] Access mode */
1183 DWORD offset_high
, /* [in] High-order 32 bits of file offset */
1184 DWORD offset_low
, /* [in] Low-order 32 bits of file offset */
1185 DWORD count
, /* [in] Number of bytes to map */
1186 LPVOID addr
/* [in] Suggested starting address for mapped view */
1189 UINT ptr
= (UINT
)-1, size
= 0;
1190 int flags
= MAP_PRIVATE
;
1191 int unix_handle
= -1;
1193 struct get_mapping_info_request
*req
= get_req_buffer();
1195 /* Check parameters */
1197 if ((offset_low
& granularity_mask
) ||
1198 (addr
&& ((UINT
)addr
& granularity_mask
)))
1200 SetLastError( ERROR_INVALID_PARAMETER
);
1204 req
->handle
= handle
;
1205 if (server_call_fd( REQ_GET_MAPPING_INFO
, -1, &unix_handle
)) goto error
;
1207 if (req
->size_high
|| offset_high
)
1208 ERR("Offsets larger than 4Gb not supported\n");
1210 if ((offset_low
>= req
->size_low
) ||
1211 (count
> req
->size_low
- offset_low
))
1213 SetLastError( ERROR_INVALID_PARAMETER
);
1216 if (count
) size
= ROUND_SIZE( offset_low
, count
);
1217 else size
= req
->size_low
- offset_low
;
1218 prot
= req
->protect
;
1222 case FILE_MAP_ALL_ACCESS
:
1223 case FILE_MAP_WRITE
:
1224 case FILE_MAP_WRITE
| FILE_MAP_READ
:
1225 if (!(prot
& VPROT_WRITE
))
1227 SetLastError( ERROR_INVALID_PARAMETER
);
1234 case FILE_MAP_COPY
| FILE_MAP_READ
:
1235 if (prot
& VPROT_READ
) break;
1238 SetLastError( ERROR_INVALID_PARAMETER
);
1244 TRACE("handle=%x size=%x offset=%lx\n", handle
, size
, offset_low
);
1246 ptr
= (UINT
)FILE_dommap( unix_handle
, addr
, 0, size
, 0, offset_low
,
1247 VIRTUAL_GetUnixProt( prot
), flags
);
1248 if (ptr
== (UINT
)-1) {
1249 /* KB: Q125713, 25-SEP-1995, "Common File Mapping Problems and
1250 * Platform Differences":
1251 * Windows NT: ERROR_INVALID_PARAMETER
1252 * Windows 95: ERROR_INVALID_ADDRESS.
1253 * FIXME: So should we add a module dependend check here? -MM
1256 SetLastError( ERROR_OUTOFMEMORY
);
1258 SetLastError( ERROR_INVALID_PARAMETER
);
1262 if (!(view
= VIRTUAL_CreateView( ptr
, size
, offset_low
, 0, prot
, handle
)))
1264 SetLastError( ERROR_OUTOFMEMORY
);
1267 if (unix_handle
!= -1) close( unix_handle
);
1271 if (unix_handle
!= -1) close( unix_handle
);
1272 if (ptr
!= (UINT
)-1) FILE_munmap( (void *)ptr
, 0, size
);
1277 /***********************************************************************
1278 * FlushViewOfFile (KERNEL32.262)
1279 * Writes to the disk a byte range within a mapped view of a file
1285 BOOL WINAPI
FlushViewOfFile(
1286 LPCVOID base
, /* [in] Start address of byte range to flush */
1287 DWORD cbFlush
/* [in] Number of bytes in range */
1290 UINT addr
= ROUND_ADDR( base
);
1292 TRACE("FlushViewOfFile at %p for %ld bytes\n",
1295 if (!(view
= VIRTUAL_FindView( addr
)))
1297 SetLastError( ERROR_INVALID_PARAMETER
);
1300 if (!cbFlush
) cbFlush
= view
->size
;
1301 if (!msync( (void *)addr
, cbFlush
, MS_SYNC
)) return TRUE
;
1302 SetLastError( ERROR_INVALID_PARAMETER
);
1307 /***********************************************************************
1308 * UnmapViewOfFile (KERNEL32.540)
1309 * Unmaps a mapped view of a file.
1312 * Should addr be an LPCVOID?
1318 BOOL WINAPI
UnmapViewOfFile(
1319 LPVOID addr
/* [in] Address where mapped view begins */
1322 UINT base
= ROUND_ADDR( addr
);
1323 if (!(view
= VIRTUAL_FindView( base
)) || (base
!= view
->base
))
1325 SetLastError( ERROR_INVALID_PARAMETER
);
1328 VIRTUAL_DeleteView( view
);
1332 /***********************************************************************
1335 * Helper function to map a file to memory:
1337 * [RETURN] ptr - pointer to mapped file
1339 LPVOID
VIRTUAL_MapFileW( LPCWSTR name
)
1341 HANDLE hFile
, hMapping
;
1344 hFile
= CreateFileW( name
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
1345 OPEN_EXISTING
, FILE_FLAG_RANDOM_ACCESS
, 0);
1346 if (hFile
!= INVALID_HANDLE_VALUE
)
1348 hMapping
= CreateFileMappingA( hFile
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
1349 CloseHandle( hFile
);
1352 ptr
= MapViewOfFile( hMapping
, FILE_MAP_READ
, 0, 0, 0 );
1353 CloseHandle( hMapping
);